[PATCH] acpi hotplug: fix slot power-down problem with acpiphp
[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  *
10  * All rights reserved.
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or (at
15  * your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful, but
18  * WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
20  * NON INFRINGEMENT.  See the GNU General Public License for more
21  * details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26  *
27  * Send feedback to <t-kochi@bq.jp.nec.com>
28  *
29  */
30
31 /*
32  * Lifetime rules for pci_dev:
33  *  - The one in acpiphp_func has its refcount elevated by pci_get_slot()
34  *    when the driver is loaded or when an insertion event occurs.  It loses
35  *    a refcount when its ejected or the driver unloads.
36  *  - The one in acpiphp_bridge has its refcount elevated by pci_get_slot()
37  *    when the bridge is scanned and it loses a refcount when the bridge
38  *    is removed.
39  */
40
41 #include <linux/init.h>
42 #include <linux/module.h>
43
44 #include <linux/kernel.h>
45 #include <linux/pci.h>
46 #include <linux/smp_lock.h>
47 #include <asm/semaphore.h>
48
49 #include "../pci.h"
50 #include "pci_hotplug.h"
51 #include "acpiphp.h"
52
53 static LIST_HEAD(bridge_list);
54
55 #define MY_NAME "acpiphp_glue"
56
57 static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
58 static void handle_hotplug_event_func (acpi_handle, u32, void *);
59
60 /*
61  * initialization & terminatation routines
62  */
63
64 /**
65  * is_ejectable - determine if a slot is ejectable
66  * @handle: handle to acpi namespace
67  *
68  * Ejectable slot should satisfy at least these conditions:
69  *
70  *  1. has _ADR method
71  *  2. has _EJ0 method
72  *
73  * optionally
74  *
75  *  1. has _STA method
76  *  2. has _PS0 method
77  *  3. has _PS3 method
78  *  4. ..
79  *
80  */
81 static int is_ejectable(acpi_handle handle)
82 {
83         acpi_status status;
84         acpi_handle tmp;
85
86         status = acpi_get_handle(handle, "_ADR", &tmp);
87         if (ACPI_FAILURE(status)) {
88                 return 0;
89         }
90
91         status = acpi_get_handle(handle, "_EJ0", &tmp);
92         if (ACPI_FAILURE(status)) {
93                 return 0;
94         }
95
96         return 1;
97 }
98
99
100 /* callback routine to check the existence of ejectable slots */
101 static acpi_status
102 is_ejectable_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
103 {
104         int *count = (int *)context;
105
106         if (is_ejectable(handle)) {
107                 (*count)++;
108                 /* only one ejectable slot is enough */
109                 return AE_CTRL_TERMINATE;
110         } else {
111                 return AE_OK;
112         }
113 }
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 adr, sun;
126         int device, function;
127         static int num_slots = 0;       /* XXX if we support I/O node hotplug... */
128
129         status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
130
131         if (ACPI_FAILURE(status))
132                 return AE_OK;
133
134         status = acpi_get_handle(handle, "_EJ0", &tmp);
135
136         if (ACPI_FAILURE(status))
137                 return AE_OK;
138
139         device = (adr >> 16) & 0xffff;
140         function = adr & 0xffff;
141
142         newfunc = kmalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
143         if (!newfunc)
144                 return AE_NO_MEMORY;
145         memset(newfunc, 0, sizeof(struct acpiphp_func));
146
147         INIT_LIST_HEAD(&newfunc->sibling);
148         newfunc->handle = handle;
149         newfunc->function = function;
150         newfunc->flags = FUNC_HAS_EJ0;
151
152         if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
153                 newfunc->flags |= FUNC_HAS_STA;
154
155         if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
156                 newfunc->flags |= FUNC_HAS_PS0;
157
158         if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
159                 newfunc->flags |= FUNC_HAS_PS3;
160
161         status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
162         if (ACPI_FAILURE(status))
163                 sun = -1;
164
165         /* search for objects that share the same slot */
166         for (slot = bridge->slots; slot; slot = slot->next)
167                 if (slot->device == device) {
168                         if (slot->sun != sun)
169                                 warn("sibling found, but _SUN doesn't match!\n");
170                         break;
171                 }
172
173         if (!slot) {
174                 slot = kmalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
175                 if (!slot) {
176                         kfree(newfunc);
177                         return AE_NO_MEMORY;
178                 }
179
180                 memset(slot, 0, sizeof(struct acpiphp_slot));
181                 slot->bridge = bridge;
182                 slot->id = num_slots++;
183                 slot->device = device;
184                 slot->sun = sun;
185                 INIT_LIST_HEAD(&slot->funcs);
186                 init_MUTEX(&slot->crit_sect);
187
188                 slot->next = bridge->slots;
189                 bridge->slots = slot;
190
191                 bridge->nr_slots++;
192
193                 dbg("found ACPI PCI Hotplug slot %d at PCI %04x:%02x:%02x\n",
194                                 slot->sun, pci_domain_nr(bridge->pci_bus),
195                                 bridge->pci_bus->number, slot->device);
196         }
197
198         newfunc->slot = slot;
199         list_add_tail(&newfunc->sibling, &slot->funcs);
200
201         /* associate corresponding pci_dev */
202         newfunc->pci_dev = pci_get_slot(bridge->pci_bus,
203                                          PCI_DEVFN(device, function));
204         if (newfunc->pci_dev) {
205                 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
206         }
207
208         /* install notify handler */
209         status = acpi_install_notify_handler(handle,
210                                              ACPI_SYSTEM_NOTIFY,
211                                              handle_hotplug_event_func,
212                                              newfunc);
213
214         if (ACPI_FAILURE(status)) {
215                 err("failed to register interrupt notify handler\n");
216                 return status;
217         }
218
219         return AE_OK;
220 }
221
222
223 /* see if it's worth looking at this bridge */
224 static int detect_ejectable_slots(acpi_handle *bridge_handle)
225 {
226         acpi_status status;
227         int count;
228
229         count = 0;
230
231         /* only check slots defined directly below bridge object */
232         status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle, (u32)1,
233                                      is_ejectable_slot, (void *)&count, NULL);
234
235         return count;
236 }
237
238
239 /* decode ACPI 2.0 _HPP hot plug parameters */
240 static void decode_hpp(struct acpiphp_bridge *bridge)
241 {
242         acpi_status status;
243         struct acpi_buffer buffer = { .length = ACPI_ALLOCATE_BUFFER,
244                                       .pointer = NULL};
245         union acpi_object *package;
246         int i;
247
248         /* default numbers */
249         bridge->hpp.cache_line_size = 0x10;
250         bridge->hpp.latency_timer = 0x40;
251         bridge->hpp.enable_SERR = 0;
252         bridge->hpp.enable_PERR = 0;
253
254         status = acpi_evaluate_object(bridge->handle, "_HPP", NULL, &buffer);
255
256         if (ACPI_FAILURE(status)) {
257                 dbg("_HPP evaluation failed\n");
258                 return;
259         }
260
261         package = (union acpi_object *) buffer.pointer;
262
263         if (!package || package->type != ACPI_TYPE_PACKAGE ||
264             package->package.count != 4 || !package->package.elements) {
265                 err("invalid _HPP object; ignoring\n");
266                 goto err_exit;
267         }
268
269         for (i = 0; i < 4; i++) {
270                 if (package->package.elements[i].type != ACPI_TYPE_INTEGER) {
271                         err("invalid _HPP parameter type; ignoring\n");
272                         goto err_exit;
273                 }
274         }
275
276         bridge->hpp.cache_line_size = package->package.elements[0].integer.value;
277         bridge->hpp.latency_timer = package->package.elements[1].integer.value;
278         bridge->hpp.enable_SERR = package->package.elements[2].integer.value;
279         bridge->hpp.enable_PERR = package->package.elements[3].integer.value;
280
281         dbg("_HPP parameter = (%02x, %02x, %02x, %02x)\n",
282                 bridge->hpp.cache_line_size,
283                 bridge->hpp.latency_timer,
284                 bridge->hpp.enable_SERR,
285                 bridge->hpp.enable_PERR);
286
287         bridge->flags |= BRIDGE_HAS_HPP;
288
289  err_exit:
290         kfree(buffer.pointer);
291 }
292
293
294 /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
295 static void init_bridge_misc(struct acpiphp_bridge *bridge)
296 {
297         acpi_status status;
298
299         /* decode ACPI 2.0 _HPP (hot plug parameters) */
300         decode_hpp(bridge);
301
302         /* register all slot objects under this bridge */
303         status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
304                                      register_slot, bridge, NULL);
305
306         /* install notify handler */
307         status = acpi_install_notify_handler(bridge->handle,
308                                              ACPI_SYSTEM_NOTIFY,
309                                              handle_hotplug_event_bridge,
310                                              bridge);
311
312         if (ACPI_FAILURE(status)) {
313                 err("failed to register interrupt notify handler\n");
314         }
315
316         list_add(&bridge->list, &bridge_list);
317 }
318
319
320 /* allocate and initialize host bridge data structure */
321 static void add_host_bridge(acpi_handle *handle, struct pci_bus *pci_bus)
322 {
323         struct acpiphp_bridge *bridge;
324
325         bridge = kmalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
326         if (bridge == NULL)
327                 return;
328
329         memset(bridge, 0, sizeof(struct acpiphp_bridge));
330
331         bridge->type = BRIDGE_TYPE_HOST;
332         bridge->handle = handle;
333
334         bridge->pci_bus = pci_bus;
335
336         spin_lock_init(&bridge->res_lock);
337
338         init_bridge_misc(bridge);
339 }
340
341
342 /* allocate and initialize PCI-to-PCI bridge data structure */
343 static void add_p2p_bridge(acpi_handle *handle, struct pci_dev *pci_dev)
344 {
345         struct acpiphp_bridge *bridge;
346
347         bridge = kmalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
348         if (bridge == NULL) {
349                 err("out of memory\n");
350                 return;
351         }
352
353         memset(bridge, 0, sizeof(struct acpiphp_bridge));
354
355         bridge->type = BRIDGE_TYPE_P2P;
356         bridge->handle = handle;
357
358         bridge->pci_dev = pci_dev_get(pci_dev);
359         bridge->pci_bus = pci_dev->subordinate;
360         if (!bridge->pci_bus) {
361                 err("This is not a PCI-to-PCI bridge!\n");
362                 goto err;
363         }
364
365         spin_lock_init(&bridge->res_lock);
366
367         init_bridge_misc(bridge);
368         return;
369  err:
370         pci_dev_put(pci_dev);
371         kfree(bridge);
372         return;
373 }
374
375
376 /* callback routine to find P2P bridges */
377 static acpi_status
378 find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
379 {
380         acpi_status status;
381         acpi_handle dummy_handle;
382         unsigned long tmp;
383         int device, function;
384         struct pci_dev *dev;
385         struct pci_bus *pci_bus = context;
386
387         status = acpi_get_handle(handle, "_ADR", &dummy_handle);
388         if (ACPI_FAILURE(status))
389                 return AE_OK;           /* continue */
390
391         status = acpi_evaluate_integer(handle, "_ADR", NULL, &tmp);
392         if (ACPI_FAILURE(status)) {
393                 dbg("%s: _ADR evaluation failure\n", __FUNCTION__);
394                 return AE_OK;
395         }
396
397         device = (tmp >> 16) & 0xffff;
398         function = tmp & 0xffff;
399
400         dev = pci_get_slot(pci_bus, PCI_DEVFN(device, function));
401
402         if (!dev || !dev->subordinate)
403                 goto out;
404
405         /* check if this bridge has ejectable slots */
406         if (detect_ejectable_slots(handle) > 0) {
407                 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
408                 add_p2p_bridge(handle, dev);
409         }
410
411  out:
412         pci_dev_put(dev);
413         return AE_OK;
414 }
415
416
417 /* find hot-pluggable slots, and then find P2P bridge */
418 static int add_bridge(acpi_handle handle)
419 {
420         acpi_status status;
421         unsigned long tmp;
422         int seg, bus;
423         acpi_handle dummy_handle;
424         struct pci_bus *pci_bus;
425
426         /* if the bridge doesn't have _STA, we assume it is always there */
427         status = acpi_get_handle(handle, "_STA", &dummy_handle);
428         if (ACPI_SUCCESS(status)) {
429                 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
430                 if (ACPI_FAILURE(status)) {
431                         dbg("%s: _STA evaluation failure\n", __FUNCTION__);
432                         return 0;
433                 }
434                 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
435                         /* don't register this object */
436                         return 0;
437         }
438
439         /* get PCI segment number */
440         status = acpi_evaluate_integer(handle, "_SEG", NULL, &tmp);
441
442         seg = ACPI_SUCCESS(status) ? tmp : 0;
443
444         /* get PCI bus number */
445         status = acpi_evaluate_integer(handle, "_BBN", NULL, &tmp);
446
447         if (ACPI_SUCCESS(status)) {
448                 bus = tmp;
449         } else {
450                 warn("can't get bus number, assuming 0\n");
451                 bus = 0;
452         }
453
454         pci_bus = pci_find_bus(seg, bus);
455         if (!pci_bus) {
456                 err("Can't find bus %04x:%02x\n", seg, bus);
457                 return 0;
458         }
459
460         /* check if this bridge has ejectable slots */
461         if (detect_ejectable_slots(handle) > 0) {
462                 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
463                 add_host_bridge(handle, pci_bus);
464                 return 0;
465         }
466
467         /* search P2P bridges under this host bridge */
468         status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
469                                      find_p2p_bridge, pci_bus, NULL);
470
471         if (ACPI_FAILURE(status))
472                 warn("find_p2p_bridge faied (error code = 0x%x)\n",status);
473
474         return 0;
475 }
476
477 static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
478 {
479         struct list_head *head;
480         list_for_each(head, &bridge_list) {
481                 struct acpiphp_bridge *bridge = list_entry(head,
482                                                 struct acpiphp_bridge, list);
483                 if (bridge->handle == handle)
484                         return bridge;
485         }
486
487         return NULL;
488 }
489
490 static void cleanup_bridge(struct acpiphp_bridge *bridge)
491 {
492         struct list_head *list, *tmp;
493         struct acpiphp_slot *slot;
494         acpi_status status;
495         acpi_handle handle = bridge->handle;
496
497         status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
498                                             handle_hotplug_event_bridge);
499         if (ACPI_FAILURE(status))
500                 err("failed to remove notify handler\n");
501
502         slot = bridge->slots;
503         while (slot) {
504                 struct acpiphp_slot *next = slot->next;
505                 list_for_each_safe (list, tmp, &slot->funcs) {
506                         struct acpiphp_func *func;
507                         func = list_entry(list, struct acpiphp_func, sibling);
508                         status = acpi_remove_notify_handler(func->handle,
509                                                 ACPI_SYSTEM_NOTIFY,
510                                                 handle_hotplug_event_func);
511                         if (ACPI_FAILURE(status))
512                                 err("failed to remove notify handler\n");
513                         pci_dev_put(func->pci_dev);
514                         list_del(list);
515                         kfree(func);
516                 }
517                 kfree(slot);
518                 slot = next;
519         }
520
521         pci_dev_put(bridge->pci_dev);
522         list_del(&bridge->list);
523         kfree(bridge);
524 }
525
526 static acpi_status
527 cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
528 {
529         struct acpiphp_bridge *bridge;
530
531         if (!(bridge = acpiphp_handle_to_bridge(handle)))
532                 return AE_OK;
533         cleanup_bridge(bridge);
534         return AE_OK;
535 }
536
537 static void remove_bridge(acpi_handle handle)
538 {
539         struct acpiphp_bridge *bridge;
540
541         bridge = acpiphp_handle_to_bridge(handle);
542         if (bridge) {
543                 cleanup_bridge(bridge);
544         } else {
545                 /* clean-up p2p bridges under this host bridge */
546                 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
547                                 (u32)1, cleanup_p2p_bridge, NULL, NULL);
548         }
549 }
550
551 static int power_on_slot(struct acpiphp_slot *slot)
552 {
553         acpi_status status;
554         struct acpiphp_func *func;
555         struct list_head *l;
556         int retval = 0;
557
558         /* if already enabled, just skip */
559         if (slot->flags & SLOT_POWEREDON)
560                 goto err_exit;
561
562         list_for_each (l, &slot->funcs) {
563                 func = list_entry(l, struct acpiphp_func, sibling);
564
565                 if (func->flags & FUNC_HAS_PS0) {
566                         dbg("%s: executing _PS0\n", __FUNCTION__);
567                         status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
568                         if (ACPI_FAILURE(status)) {
569                                 warn("%s: _PS0 failed\n", __FUNCTION__);
570                                 retval = -1;
571                                 goto err_exit;
572                         } else
573                                 break;
574                 }
575         }
576
577         /* TBD: evaluate _STA to check if the slot is enabled */
578
579         slot->flags |= SLOT_POWEREDON;
580
581  err_exit:
582         return retval;
583 }
584
585
586 static int power_off_slot(struct acpiphp_slot *slot)
587 {
588         acpi_status status;
589         struct acpiphp_func *func;
590         struct list_head *l;
591         struct acpi_object_list arg_list;
592         union acpi_object arg;
593
594         int retval = 0;
595
596         /* if already disabled, just skip */
597         if ((slot->flags & SLOT_POWEREDON) == 0)
598                 goto err_exit;
599
600         list_for_each (l, &slot->funcs) {
601                 func = list_entry(l, struct acpiphp_func, sibling);
602
603                 if (func->flags & FUNC_HAS_PS3) {
604                         status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
605                         if (ACPI_FAILURE(status)) {
606                                 warn("%s: _PS3 failed\n", __FUNCTION__);
607                                 retval = -1;
608                                 goto err_exit;
609                         } else
610                                 break;
611                 }
612         }
613
614         list_for_each (l, &slot->funcs) {
615                 func = list_entry(l, struct acpiphp_func, sibling);
616
617                 /* We don't want to call _EJ0 on non-existing functions. */
618                 if (func->flags & FUNC_HAS_EJ0) {
619                         /* _EJ0 method take one argument */
620                         arg_list.count = 1;
621                         arg_list.pointer = &arg;
622                         arg.type = ACPI_TYPE_INTEGER;
623                         arg.integer.value = 1;
624
625                         status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
626                         if (ACPI_FAILURE(status)) {
627                                 warn("%s: _EJ0 failed\n", __FUNCTION__);
628                                 retval = -1;
629                                 goto err_exit;
630                         } else
631                                 break;
632                 }
633         }
634
635         /* TBD: evaluate _STA to check if the slot is disabled */
636
637         slot->flags &= (~SLOT_POWEREDON);
638
639  err_exit:
640         return retval;
641 }
642
643
644 /**
645  * enable_device - enable, configure a slot
646  * @slot: slot to be enabled
647  *
648  * This function should be called per *physical slot*,
649  * not per each slot object in ACPI namespace.
650  *
651  */
652 static int enable_device(struct acpiphp_slot *slot)
653 {
654         struct pci_dev *dev;
655         struct pci_bus *bus = slot->bridge->pci_bus;
656         struct list_head *l;
657         struct acpiphp_func *func;
658         int retval = 0;
659         int num, max, pass;
660
661         if (slot->flags & SLOT_ENABLED)
662                 goto err_exit;
663
664         /* sanity check: dev should be NULL when hot-plugged in */
665         dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
666         if (dev) {
667                 /* This case shouldn't happen */
668                 err("pci_dev structure already exists.\n");
669                 pci_dev_put(dev);
670                 retval = -1;
671                 goto err_exit;
672         }
673
674         num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
675         if (num == 0) {
676                 err("No new device found\n");
677                 retval = -1;
678                 goto err_exit;
679         }
680
681         max = bus->secondary;
682         for (pass = 0; pass < 2; pass++) {
683                 list_for_each_entry(dev, &bus->devices, bus_list) {
684                         if (PCI_SLOT(dev->devfn) != slot->device)
685                                 continue;
686                         if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
687                             dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
688                                 max = pci_scan_bridge(bus, dev, max, pass);
689                 }
690         }
691
692         pci_bus_assign_resources(bus);
693         pci_bus_add_devices(bus);
694
695         /* associate pci_dev to our representation */
696         list_for_each (l, &slot->funcs) {
697                 func = list_entry(l, struct acpiphp_func, sibling);
698                 func->pci_dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
699                                                         func->function));
700         }
701
702         slot->flags |= SLOT_ENABLED;
703
704  err_exit:
705         return retval;
706 }
707
708
709 /**
710  * disable_device - disable a slot
711  */
712 static int disable_device(struct acpiphp_slot *slot)
713 {
714         int retval = 0;
715         struct acpiphp_func *func;
716         struct list_head *l;
717
718         /* is this slot already disabled? */
719         if (!(slot->flags & SLOT_ENABLED))
720                 goto err_exit;
721
722         list_for_each (l, &slot->funcs) {
723                 func = list_entry(l, struct acpiphp_func, sibling);
724                 if (!func->pci_dev)
725                         continue;
726
727                 pci_remove_bus_device(func->pci_dev);
728                 pci_dev_put(func->pci_dev);
729                 func->pci_dev = NULL;
730         }
731
732         slot->flags &= (~SLOT_ENABLED);
733
734  err_exit:
735         return retval;
736 }
737
738
739 /**
740  * get_slot_status - get ACPI slot status
741  *
742  * if a slot has _STA for each function and if any one of them
743  * returned non-zero status, return it
744  *
745  * if a slot doesn't have _STA and if any one of its functions'
746  * configuration space is configured, return 0x0f as a _STA
747  *
748  * otherwise return 0
749  */
750 static unsigned int get_slot_status(struct acpiphp_slot *slot)
751 {
752         acpi_status status;
753         unsigned long sta = 0;
754         u32 dvid;
755         struct list_head *l;
756         struct acpiphp_func *func;
757
758         list_for_each (l, &slot->funcs) {
759                 func = list_entry(l, struct acpiphp_func, sibling);
760
761                 if (func->flags & FUNC_HAS_STA) {
762                         status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
763                         if (ACPI_SUCCESS(status) && sta)
764                                 break;
765                 } else {
766                         pci_bus_read_config_dword(slot->bridge->pci_bus,
767                                                   PCI_DEVFN(slot->device,
768                                                             func->function),
769                                                   PCI_VENDOR_ID, &dvid);
770                         if (dvid != 0xffffffff) {
771                                 sta = ACPI_STA_ALL;
772                                 break;
773                         }
774                 }
775         }
776
777         return (unsigned int)sta;
778 }
779
780 /**
781  * acpiphp_check_bridge - re-enumerate devices
782  *
783  * Iterate over all slots under this bridge and make sure that if a
784  * card is present they are enabled, and if not they are disabled.
785  */
786 static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
787 {
788         struct acpiphp_slot *slot;
789         int retval = 0;
790         int enabled, disabled;
791
792         enabled = disabled = 0;
793
794         for (slot = bridge->slots; slot; slot = slot->next) {
795                 unsigned int status = get_slot_status(slot);
796                 if (slot->flags & SLOT_ENABLED) {
797                         if (status == ACPI_STA_ALL)
798                                 continue;
799                         retval = acpiphp_disable_slot(slot);
800                         if (retval) {
801                                 err("Error occurred in disabling\n");
802                                 goto err_exit;
803                         }
804                         disabled++;
805                 } else {
806                         if (status != ACPI_STA_ALL)
807                                 continue;
808                         retval = acpiphp_enable_slot(slot);
809                         if (retval) {
810                                 err("Error occurred in enabling\n");
811                                 goto err_exit;
812                         }
813                         enabled++;
814                 }
815         }
816
817         dbg("%s: %d enabled, %d disabled\n", __FUNCTION__, enabled, disabled);
818
819  err_exit:
820         return retval;
821 }
822
823 /*
824  * ACPI event handlers
825  */
826
827 /**
828  * handle_hotplug_event_bridge - handle ACPI event on bridges
829  *
830  * @handle: Notify()'ed acpi_handle
831  * @type: Notify code
832  * @context: pointer to acpiphp_bridge structure
833  *
834  * handles ACPI event notification on {host,p2p} bridges
835  *
836  */
837 static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context)
838 {
839         struct acpiphp_bridge *bridge;
840         char objname[64];
841         struct acpi_buffer buffer = { .length = sizeof(objname),
842                                       .pointer = objname };
843
844         bridge = (struct acpiphp_bridge *)context;
845
846         acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
847
848         switch (type) {
849         case ACPI_NOTIFY_BUS_CHECK:
850                 /* bus re-enumerate */
851                 dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
852                 acpiphp_check_bridge(bridge);
853                 break;
854
855         case ACPI_NOTIFY_DEVICE_CHECK:
856                 /* device check */
857                 dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
858                 acpiphp_check_bridge(bridge);
859                 break;
860
861         case ACPI_NOTIFY_DEVICE_WAKE:
862                 /* wake event */
863                 dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
864                 break;
865
866         case ACPI_NOTIFY_EJECT_REQUEST:
867                 /* request device eject */
868                 dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
869                 break;
870
871         case ACPI_NOTIFY_FREQUENCY_MISMATCH:
872                 printk(KERN_ERR "Device %s cannot be configured due"
873                                 " to a frequency mismatch\n", objname);
874                 break;
875
876         case ACPI_NOTIFY_BUS_MODE_MISMATCH:
877                 printk(KERN_ERR "Device %s cannot be configured due"
878                                 " to a bus mode mismatch\n", objname);
879                 break;
880
881         case ACPI_NOTIFY_POWER_FAULT:
882                 printk(KERN_ERR "Device %s has suffered a power fault\n",
883                                 objname);
884                 break;
885
886         default:
887                 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
888                 break;
889         }
890 }
891
892
893 /**
894  * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
895  *
896  * @handle: Notify()'ed acpi_handle
897  * @type: Notify code
898  * @context: pointer to acpiphp_func structure
899  *
900  * handles ACPI event notification on slots
901  *
902  */
903 static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context)
904 {
905         struct acpiphp_func *func;
906         char objname[64];
907         struct acpi_buffer buffer = { .length = sizeof(objname),
908                                       .pointer = objname };
909
910         acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
911
912         func = (struct acpiphp_func *)context;
913
914         switch (type) {
915         case ACPI_NOTIFY_BUS_CHECK:
916                 /* bus re-enumerate */
917                 dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
918                 acpiphp_enable_slot(func->slot);
919                 break;
920
921         case ACPI_NOTIFY_DEVICE_CHECK:
922                 /* device check : re-enumerate from parent bus */
923                 dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
924                 acpiphp_check_bridge(func->slot->bridge);
925                 break;
926
927         case ACPI_NOTIFY_DEVICE_WAKE:
928                 /* wake event */
929                 dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
930                 break;
931
932         case ACPI_NOTIFY_EJECT_REQUEST:
933                 /* request device eject */
934                 dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
935                 acpiphp_disable_slot(func->slot);
936                 break;
937
938         default:
939                 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
940                 break;
941         }
942 }
943
944
945 static struct acpi_pci_driver acpi_pci_hp_driver = {
946         .add =          add_bridge,
947         .remove =       remove_bridge,
948 };
949
950 /**
951  * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
952  *
953  */
954 int __init acpiphp_glue_init(void)
955 {
956         int num;
957
958         if (list_empty(&pci_root_buses))
959                 return -1;
960
961         num = acpi_pci_register_driver(&acpi_pci_hp_driver);
962
963         if (num <= 0)
964                 return -1;
965
966         return 0;
967 }
968
969
970 /**
971  * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
972  *
973  * This function frees all data allocated in acpiphp_glue_init()
974  */
975 void __exit acpiphp_glue_exit(void)
976 {
977         acpi_pci_unregister_driver(&acpi_pci_hp_driver);
978 }
979
980
981 /**
982  * acpiphp_get_num_slots - count number of slots in a system
983  */
984 int __init acpiphp_get_num_slots(void)
985 {
986         struct list_head *node;
987         struct acpiphp_bridge *bridge;
988         int num_slots;
989
990         num_slots = 0;
991
992         list_for_each (node, &bridge_list) {
993                 bridge = (struct acpiphp_bridge *)node;
994                 dbg("Bus %04x:%02x has %d slot%s\n",
995                                 pci_domain_nr(bridge->pci_bus),
996                                 bridge->pci_bus->number, bridge->nr_slots,
997                                 bridge->nr_slots == 1 ? "" : "s");
998                 num_slots += bridge->nr_slots;
999         }
1000
1001         dbg("Total %d slots\n", num_slots);
1002         return num_slots;
1003 }
1004
1005
1006 #if 0
1007 /**
1008  * acpiphp_for_each_slot - call function for each slot
1009  * @fn: callback function
1010  * @data: context to be passed to callback function
1011  *
1012  */
1013 static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
1014 {
1015         struct list_head *node;
1016         struct acpiphp_bridge *bridge;
1017         struct acpiphp_slot *slot;
1018         int retval = 0;
1019
1020         list_for_each (node, &bridge_list) {
1021                 bridge = (struct acpiphp_bridge *)node;
1022                 for (slot = bridge->slots; slot; slot = slot->next) {
1023                         retval = fn(slot, data);
1024                         if (!retval)
1025                                 goto err_exit;
1026                 }
1027         }
1028
1029  err_exit:
1030         return retval;
1031 }
1032 #endif
1033
1034 /* search matching slot from id  */
1035 struct acpiphp_slot *get_slot_from_id(int id)
1036 {
1037         struct list_head *node;
1038         struct acpiphp_bridge *bridge;
1039         struct acpiphp_slot *slot;
1040
1041         list_for_each (node, &bridge_list) {
1042                 bridge = (struct acpiphp_bridge *)node;
1043                 for (slot = bridge->slots; slot; slot = slot->next)
1044                         if (slot->id == id)
1045                                 return slot;
1046         }
1047
1048         /* should never happen! */
1049         err("%s: no object for id %d\n", __FUNCTION__, id);
1050         WARN_ON(1);
1051         return NULL;
1052 }
1053
1054
1055 /**
1056  * acpiphp_enable_slot - power on slot
1057  */
1058 int acpiphp_enable_slot(struct acpiphp_slot *slot)
1059 {
1060         int retval;
1061
1062         down(&slot->crit_sect);
1063
1064         /* wake up all functions */
1065         retval = power_on_slot(slot);
1066         if (retval)
1067                 goto err_exit;
1068
1069         if (get_slot_status(slot) == ACPI_STA_ALL)
1070                 /* configure all functions */
1071                 retval = enable_device(slot);
1072
1073  err_exit:
1074         up(&slot->crit_sect);
1075         return retval;
1076 }
1077
1078
1079 /**
1080  * acpiphp_disable_slot - power off slot
1081  */
1082 int acpiphp_disable_slot(struct acpiphp_slot *slot)
1083 {
1084         int retval = 0;
1085
1086         down(&slot->crit_sect);
1087
1088         /* unconfigure all functions */
1089         retval = disable_device(slot);
1090         if (retval)
1091                 goto err_exit;
1092
1093         /* power off all functions */
1094         retval = power_off_slot(slot);
1095         if (retval)
1096                 goto err_exit;
1097
1098  err_exit:
1099         up(&slot->crit_sect);
1100         return retval;
1101 }
1102
1103
1104 /*
1105  * slot enabled:  1
1106  * slot disabled: 0
1107  */
1108 u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1109 {
1110         unsigned int sta;
1111
1112         sta = get_slot_status(slot);
1113
1114         return (sta & ACPI_STA_ENABLED) ? 1 : 0;
1115 }
1116
1117
1118 /*
1119  * latch closed:  1
1120  * latch   open:  0
1121  */
1122 u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1123 {
1124         unsigned int sta;
1125
1126         sta = get_slot_status(slot);
1127
1128         return (sta & ACPI_STA_SHOW_IN_UI) ? 1 : 0;
1129 }
1130
1131
1132 /*
1133  * adapter presence : 1
1134  *          absence : 0
1135  */
1136 u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1137 {
1138         unsigned int sta;
1139
1140         sta = get_slot_status(slot);
1141
1142         return (sta == 0) ? 0 : 1;
1143 }
1144
1145
1146 /*
1147  * pci address (seg/bus/dev)
1148  */
1149 u32 acpiphp_get_address(struct acpiphp_slot *slot)
1150 {
1151         u32 address;
1152         struct pci_bus *pci_bus = slot->bridge->pci_bus;
1153
1154         address = (pci_domain_nr(pci_bus) << 16) |
1155                   (pci_bus->number << 8) |
1156                   slot->device;
1157
1158         return address;
1159 }