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