Merge master.kernel.org:/pub/scm/linux/kernel/git/gregkh/pci-2.6
[sfrench/cifs-2.6.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 <kristen.c.accardi@intel.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/pci_hotplug.h>
49 #include <linux/smp_lock.h>
50 #include <linux/mutex.h>
51
52 #include "../pci.h"
53 #include "acpiphp.h"
54
55 static LIST_HEAD(bridge_list);
56 static LIST_HEAD(ioapic_list);
57 static DEFINE_SPINLOCK(ioapic_list_lock);
58
59 #define MY_NAME "acpiphp_glue"
60
61 static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
62 static void acpiphp_sanitize_bus(struct pci_bus *bus);
63 static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus);
64 static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context);
65
66
67 /*
68  * initialization & terminatation routines
69  */
70
71 /**
72  * is_ejectable - determine if a slot is ejectable
73  * @handle: handle to acpi namespace
74  *
75  * Ejectable slot should satisfy at least these conditions:
76  *
77  *  1. has _ADR method
78  *  2. has _EJ0 method
79  *
80  * optionally
81  *
82  *  1. has _STA method
83  *  2. has _PS0 method
84  *  3. has _PS3 method
85  *  4. ..
86  *
87  */
88 static int is_ejectable(acpi_handle handle)
89 {
90         acpi_status status;
91         acpi_handle tmp;
92
93         status = acpi_get_handle(handle, "_ADR", &tmp);
94         if (ACPI_FAILURE(status)) {
95                 return 0;
96         }
97
98         status = acpi_get_handle(handle, "_EJ0", &tmp);
99         if (ACPI_FAILURE(status)) {
100                 return 0;
101         }
102
103         return 1;
104 }
105
106
107 /* callback routine to check the existence of ejectable slots */
108 static acpi_status
109 is_ejectable_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
110 {
111         int *count = (int *)context;
112
113         if (is_ejectable(handle)) {
114                 (*count)++;
115                 /* only one ejectable slot is enough */
116                 return AE_CTRL_TERMINATE;
117         } else {
118                 return AE_OK;
119         }
120 }
121
122 /* callback routine to check for the existance of a pci dock device */
123 static acpi_status
124 is_pci_dock_device(acpi_handle handle, u32 lvl, void *context, void **rv)
125 {
126         int *count = (int *)context;
127
128         if (is_dock_device(handle)) {
129                 (*count)++;
130                 return AE_CTRL_TERMINATE;
131         } else {
132                 return AE_OK;
133         }
134 }
135
136
137
138
139 /*
140  * the _DCK method can do funny things... and sometimes not
141  * hah-hah funny.
142  *
143  * TBD - figure out a way to only call fixups for
144  * systems that require them.
145  */
146 static int post_dock_fixups(struct notifier_block *nb, unsigned long val,
147         void *v)
148 {
149         struct acpiphp_func *func = container_of(nb, struct acpiphp_func, nb);
150         struct pci_bus *bus = func->slot->bridge->pci_bus;
151         u32 buses;
152
153         if (!bus->self)
154                 return  NOTIFY_OK;
155
156         /* fixup bad _DCK function that rewrites
157          * secondary bridge on slot
158          */
159         pci_read_config_dword(bus->self,
160                         PCI_PRIMARY_BUS,
161                         &buses);
162
163         if (((buses >> 8) & 0xff) != bus->secondary) {
164                 buses = (buses & 0xff000000)
165                         | ((unsigned int)(bus->primary)     <<  0)
166                         | ((unsigned int)(bus->secondary)   <<  8)
167                         | ((unsigned int)(bus->subordinate) << 16);
168                 pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
169         }
170         return NOTIFY_OK;
171 }
172
173
174
175
176 /* callback routine to register each ACPI PCI slot object */
177 static acpi_status
178 register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
179 {
180         struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
181         struct acpiphp_slot *slot;
182         struct acpiphp_func *newfunc;
183         acpi_handle tmp;
184         acpi_status status = AE_OK;
185         unsigned long adr, sun;
186         int device, function, retval;
187
188         status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
189
190         if (ACPI_FAILURE(status))
191                 return AE_OK;
192
193         status = acpi_get_handle(handle, "_EJ0", &tmp);
194
195         if (ACPI_FAILURE(status) && !(is_dock_device(handle)))
196                 return AE_OK;
197
198         device = (adr >> 16) & 0xffff;
199         function = adr & 0xffff;
200
201         newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
202         if (!newfunc)
203                 return AE_NO_MEMORY;
204
205         INIT_LIST_HEAD(&newfunc->sibling);
206         newfunc->handle = handle;
207         newfunc->function = function;
208         if (ACPI_SUCCESS(status))
209                 newfunc->flags = FUNC_HAS_EJ0;
210
211         if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
212                 newfunc->flags |= FUNC_HAS_STA;
213
214         if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
215                 newfunc->flags |= FUNC_HAS_PS0;
216
217         if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
218                 newfunc->flags |= FUNC_HAS_PS3;
219
220         if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp)))
221                 newfunc->flags |= FUNC_HAS_DCK;
222
223         status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
224         if (ACPI_FAILURE(status)) {
225                 /*
226                  * use the count of the number of slots we've found
227                  * for the number of the slot
228                  */
229                 sun = bridge->nr_slots+1;
230         }
231
232         /* search for objects that share the same slot */
233         for (slot = bridge->slots; slot; slot = slot->next)
234                 if (slot->device == device) {
235                         if (slot->sun != sun)
236                                 warn("sibling found, but _SUN doesn't match!\n");
237                         break;
238                 }
239
240         if (!slot) {
241                 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
242                 if (!slot) {
243                         kfree(newfunc);
244                         return AE_NO_MEMORY;
245                 }
246
247                 slot->bridge = bridge;
248                 slot->device = device;
249                 slot->sun = sun;
250                 INIT_LIST_HEAD(&slot->funcs);
251                 mutex_init(&slot->crit_sect);
252
253                 slot->next = bridge->slots;
254                 bridge->slots = slot;
255
256                 bridge->nr_slots++;
257
258                 dbg("found ACPI PCI Hotplug slot %d at PCI %04x:%02x:%02x\n",
259                                 slot->sun, pci_domain_nr(bridge->pci_bus),
260                                 bridge->pci_bus->number, slot->device);
261                 retval = acpiphp_register_hotplug_slot(slot);
262                 if (retval) {
263                         warn("acpiphp_register_hotplug_slot failed(err code = 0x%x)\n", retval);
264                         goto err_exit;
265                 }
266         }
267
268         newfunc->slot = slot;
269         list_add_tail(&newfunc->sibling, &slot->funcs);
270
271         /* associate corresponding pci_dev */
272         newfunc->pci_dev = pci_get_slot(bridge->pci_bus,
273                                          PCI_DEVFN(device, function));
274         if (newfunc->pci_dev) {
275                 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
276         }
277
278         if (is_dock_device(handle)) {
279                 /* we don't want to call this device's _EJ0
280                  * because we want the dock notify handler
281                  * to call it after it calls _DCK
282                  */
283                 newfunc->flags &= ~FUNC_HAS_EJ0;
284                 if (register_hotplug_dock_device(handle,
285                         handle_hotplug_event_func, newfunc))
286                         dbg("failed to register dock device\n");
287
288                 /* we need to be notified when dock events happen
289                  * outside of the hotplug operation, since we may
290                  * need to do fixups before we can hotplug.
291                  */
292                 newfunc->nb.notifier_call = post_dock_fixups;
293                 if (register_dock_notifier(&newfunc->nb))
294                         dbg("failed to register a dock notifier");
295         }
296
297         /* install notify handler */
298         if (!(newfunc->flags & FUNC_HAS_DCK)) {
299                 status = acpi_install_notify_handler(handle,
300                                              ACPI_SYSTEM_NOTIFY,
301                                              handle_hotplug_event_func,
302                                              newfunc);
303
304                 if (ACPI_FAILURE(status))
305                         err("failed to register interrupt notify handler\n");
306         } else
307                 status = AE_OK;
308
309         return status;
310
311  err_exit:
312         bridge->nr_slots--;
313         bridge->slots = slot->next;
314         kfree(slot);
315         kfree(newfunc);
316
317         return AE_OK;
318 }
319
320
321 /* see if it's worth looking at this bridge */
322 static int detect_ejectable_slots(acpi_handle *bridge_handle)
323 {
324         acpi_status status;
325         int count;
326
327         count = 0;
328
329         /* only check slots defined directly below bridge object */
330         status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle, (u32)1,
331                                      is_ejectable_slot, (void *)&count, NULL);
332
333         /*
334          * we also need to add this bridge if there is a dock bridge or
335          * other pci device on a dock station (removable)
336          */
337         if (!count)
338                 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle,
339                                 (u32)1, is_pci_dock_device, (void *)&count,
340                                 NULL);
341
342         return count;
343 }
344
345
346 /* decode ACPI 2.0 _HPP hot plug parameters */
347 static void decode_hpp(struct acpiphp_bridge *bridge)
348 {
349         acpi_status status;
350
351         status = acpi_get_hp_params_from_firmware(bridge->pci_bus, &bridge->hpp);
352         if (ACPI_FAILURE(status) ||
353             !bridge->hpp.t0 || (bridge->hpp.t0->revision > 1)) {
354                 /* use default numbers */
355                 printk(KERN_WARNING
356                        "%s: Could not get hotplug parameters. Use defaults\n",
357                        __FUNCTION__);
358                 bridge->hpp.t0 = &bridge->hpp.type0_data;
359                 bridge->hpp.t0->revision = 0;
360                 bridge->hpp.t0->cache_line_size = 0x10;
361                 bridge->hpp.t0->latency_timer = 0x40;
362                 bridge->hpp.t0->enable_serr = 0;
363                 bridge->hpp.t0->enable_perr = 0;
364         }
365 }
366
367
368
369 /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
370 static void init_bridge_misc(struct acpiphp_bridge *bridge)
371 {
372         acpi_status status;
373
374         /* decode ACPI 2.0 _HPP (hot plug parameters) */
375         decode_hpp(bridge);
376
377         /* must be added to the list prior to calling register_slot */
378         list_add(&bridge->list, &bridge_list);
379
380         /* register all slot objects under this bridge */
381         status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
382                                      register_slot, bridge, NULL);
383         if (ACPI_FAILURE(status)) {
384                 list_del(&bridge->list);
385                 return;
386         }
387
388         /* install notify handler */
389         if (bridge->type != BRIDGE_TYPE_HOST) {
390                 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
391                         status = acpi_remove_notify_handler(bridge->func->handle,
392                                                 ACPI_SYSTEM_NOTIFY,
393                                                 handle_hotplug_event_func);
394                         if (ACPI_FAILURE(status))
395                                 err("failed to remove notify handler\n");
396                 }
397                 status = acpi_install_notify_handler(bridge->handle,
398                                              ACPI_SYSTEM_NOTIFY,
399                                              handle_hotplug_event_bridge,
400                                              bridge);
401
402                 if (ACPI_FAILURE(status)) {
403                         err("failed to register interrupt notify handler\n");
404                 }
405         }
406 }
407
408
409 /* find acpiphp_func from acpiphp_bridge */
410 static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
411 {
412         struct list_head *node, *l;
413         struct acpiphp_bridge *bridge;
414         struct acpiphp_slot *slot;
415         struct acpiphp_func *func;
416
417         list_for_each(node, &bridge_list) {
418                 bridge = list_entry(node, struct acpiphp_bridge, list);
419                 for (slot = bridge->slots; slot; slot = slot->next) {
420                         list_for_each(l, &slot->funcs) {
421                                 func = list_entry(l, struct acpiphp_func,
422                                                         sibling);
423                                 if (func->handle == handle)
424                                         return func;
425                         }
426                 }
427         }
428
429         return NULL;
430 }
431
432
433 static inline void config_p2p_bridge_flags(struct acpiphp_bridge *bridge)
434 {
435         acpi_handle dummy_handle;
436
437         if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
438                                         "_STA", &dummy_handle)))
439                 bridge->flags |= BRIDGE_HAS_STA;
440
441         if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
442                                         "_EJ0", &dummy_handle)))
443                 bridge->flags |= BRIDGE_HAS_EJ0;
444
445         if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
446                                         "_PS0", &dummy_handle)))
447                 bridge->flags |= BRIDGE_HAS_PS0;
448
449         if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
450                                         "_PS3", &dummy_handle)))
451                 bridge->flags |= BRIDGE_HAS_PS3;
452
453         /* is this ejectable p2p bridge? */
454         if (bridge->flags & BRIDGE_HAS_EJ0) {
455                 struct acpiphp_func *func;
456
457                 dbg("found ejectable p2p bridge\n");
458
459                 /* make link between PCI bridge and PCI function */
460                 func = acpiphp_bridge_handle_to_function(bridge->handle);
461                 if (!func)
462                         return;
463                 bridge->func = func;
464                 func->bridge = bridge;
465         }
466 }
467
468
469 /* allocate and initialize host bridge data structure */
470 static void add_host_bridge(acpi_handle *handle, struct pci_bus *pci_bus)
471 {
472         struct acpiphp_bridge *bridge;
473
474         bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
475         if (bridge == NULL)
476                 return;
477
478         bridge->type = BRIDGE_TYPE_HOST;
479         bridge->handle = handle;
480
481         bridge->pci_bus = pci_bus;
482
483         spin_lock_init(&bridge->res_lock);
484
485         init_bridge_misc(bridge);
486 }
487
488
489 /* allocate and initialize PCI-to-PCI bridge data structure */
490 static void add_p2p_bridge(acpi_handle *handle, struct pci_dev *pci_dev)
491 {
492         struct acpiphp_bridge *bridge;
493
494         bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
495         if (bridge == NULL) {
496                 err("out of memory\n");
497                 return;
498         }
499
500         bridge->type = BRIDGE_TYPE_P2P;
501         bridge->handle = handle;
502         config_p2p_bridge_flags(bridge);
503
504         bridge->pci_dev = pci_dev_get(pci_dev);
505         bridge->pci_bus = pci_dev->subordinate;
506         if (!bridge->pci_bus) {
507                 err("This is not a PCI-to-PCI bridge!\n");
508                 goto err;
509         }
510
511         spin_lock_init(&bridge->res_lock);
512
513         init_bridge_misc(bridge);
514         return;
515  err:
516         pci_dev_put(pci_dev);
517         kfree(bridge);
518         return;
519 }
520
521
522 /* callback routine to find P2P bridges */
523 static acpi_status
524 find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
525 {
526         acpi_status status;
527         acpi_handle dummy_handle;
528         unsigned long tmp;
529         int device, function;
530         struct pci_dev *dev;
531         struct pci_bus *pci_bus = context;
532
533         status = acpi_get_handle(handle, "_ADR", &dummy_handle);
534         if (ACPI_FAILURE(status))
535                 return AE_OK;           /* continue */
536
537         status = acpi_evaluate_integer(handle, "_ADR", NULL, &tmp);
538         if (ACPI_FAILURE(status)) {
539                 dbg("%s: _ADR evaluation failure\n", __FUNCTION__);
540                 return AE_OK;
541         }
542
543         device = (tmp >> 16) & 0xffff;
544         function = tmp & 0xffff;
545
546         dev = pci_get_slot(pci_bus, PCI_DEVFN(device, function));
547
548         if (!dev || !dev->subordinate)
549                 goto out;
550
551         /* check if this bridge has ejectable slots */
552         if ((detect_ejectable_slots(handle) > 0)) {
553                 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
554                 add_p2p_bridge(handle, dev);
555         }
556
557         /* search P2P bridges under this p2p bridge */
558         status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
559                                      find_p2p_bridge, dev->subordinate, NULL);
560         if (ACPI_FAILURE(status))
561                 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
562
563  out:
564         pci_dev_put(dev);
565         return AE_OK;
566 }
567
568
569 /* find hot-pluggable slots, and then find P2P bridge */
570 static int add_bridge(acpi_handle handle)
571 {
572         acpi_status status;
573         unsigned long tmp;
574         int seg, bus;
575         acpi_handle dummy_handle;
576         struct pci_bus *pci_bus;
577
578         /* if the bridge doesn't have _STA, we assume it is always there */
579         status = acpi_get_handle(handle, "_STA", &dummy_handle);
580         if (ACPI_SUCCESS(status)) {
581                 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
582                 if (ACPI_FAILURE(status)) {
583                         dbg("%s: _STA evaluation failure\n", __FUNCTION__);
584                         return 0;
585                 }
586                 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
587                         /* don't register this object */
588                         return 0;
589         }
590
591         /* get PCI segment number */
592         status = acpi_evaluate_integer(handle, "_SEG", NULL, &tmp);
593
594         seg = ACPI_SUCCESS(status) ? tmp : 0;
595
596         /* get PCI bus number */
597         status = acpi_evaluate_integer(handle, "_BBN", NULL, &tmp);
598
599         if (ACPI_SUCCESS(status)) {
600                 bus = tmp;
601         } else {
602                 warn("can't get bus number, assuming 0\n");
603                 bus = 0;
604         }
605
606         pci_bus = pci_find_bus(seg, bus);
607         if (!pci_bus) {
608                 err("Can't find bus %04x:%02x\n", seg, bus);
609                 return 0;
610         }
611
612         /* check if this bridge has ejectable slots */
613         if (detect_ejectable_slots(handle) > 0) {
614                 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
615                 add_host_bridge(handle, pci_bus);
616         }
617
618         /* search P2P bridges under this host bridge */
619         status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
620                                      find_p2p_bridge, pci_bus, NULL);
621
622         if (ACPI_FAILURE(status))
623                 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
624
625         return 0;
626 }
627
628 static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
629 {
630         struct list_head *head;
631         list_for_each(head, &bridge_list) {
632                 struct acpiphp_bridge *bridge = list_entry(head,
633                                                 struct acpiphp_bridge, list);
634                 if (bridge->handle == handle)
635                         return bridge;
636         }
637
638         return NULL;
639 }
640
641 static void cleanup_bridge(struct acpiphp_bridge *bridge)
642 {
643         struct list_head *list, *tmp;
644         struct acpiphp_slot *slot;
645         acpi_status status;
646         acpi_handle handle = bridge->handle;
647
648         status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
649                                             handle_hotplug_event_bridge);
650         if (ACPI_FAILURE(status))
651                 err("failed to remove notify handler\n");
652
653         if ((bridge->type != BRIDGE_TYPE_HOST) &&
654             ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)) {
655                 status = acpi_install_notify_handler(bridge->func->handle,
656                                                 ACPI_SYSTEM_NOTIFY,
657                                                 handle_hotplug_event_func,
658                                                 bridge->func);
659                 if (ACPI_FAILURE(status))
660                         err("failed to install interrupt notify handler\n");
661         }
662
663         slot = bridge->slots;
664         while (slot) {
665                 struct acpiphp_slot *next = slot->next;
666                 list_for_each_safe (list, tmp, &slot->funcs) {
667                         struct acpiphp_func *func;
668                         func = list_entry(list, struct acpiphp_func, sibling);
669                         if (is_dock_device(func->handle)) {
670                                 unregister_hotplug_dock_device(func->handle);
671                                 unregister_dock_notifier(&func->nb);
672                         }
673                         if (!(func->flags & FUNC_HAS_DCK)) {
674                                 status = acpi_remove_notify_handler(func->handle,
675                                                 ACPI_SYSTEM_NOTIFY,
676                                                 handle_hotplug_event_func);
677                                 if (ACPI_FAILURE(status))
678                                         err("failed to remove notify handler\n");
679                         }
680                         pci_dev_put(func->pci_dev);
681                         list_del(list);
682                         kfree(func);
683                 }
684                 acpiphp_unregister_hotplug_slot(slot);
685                 list_del(&slot->funcs);
686                 kfree(slot);
687                 slot = next;
688         }
689
690         pci_dev_put(bridge->pci_dev);
691         list_del(&bridge->list);
692         kfree(bridge);
693 }
694
695 static acpi_status
696 cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
697 {
698         struct acpiphp_bridge *bridge;
699
700         /* cleanup p2p bridges under this P2P bridge
701            in a depth-first manner */
702         acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
703                                 cleanup_p2p_bridge, NULL, NULL);
704
705         if (!(bridge = acpiphp_handle_to_bridge(handle)))
706                 return AE_OK;
707         cleanup_bridge(bridge);
708         return AE_OK;
709 }
710
711 static void remove_bridge(acpi_handle handle)
712 {
713         struct acpiphp_bridge *bridge;
714
715         /* cleanup p2p bridges under this host bridge
716            in a depth-first manner */
717         acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
718                                 (u32)1, cleanup_p2p_bridge, NULL, NULL);
719
720         bridge = acpiphp_handle_to_bridge(handle);
721         if (bridge)
722                 cleanup_bridge(bridge);
723 }
724
725 static struct pci_dev * get_apic_pci_info(acpi_handle handle)
726 {
727         struct acpi_pci_id id;
728         struct pci_bus *bus;
729         struct pci_dev *dev;
730
731         if (ACPI_FAILURE(acpi_get_pci_id(handle, &id)))
732                 return NULL;
733
734         bus = pci_find_bus(id.segment, id.bus);
735         if (!bus)
736                 return NULL;
737
738         dev = pci_get_slot(bus, PCI_DEVFN(id.device, id.function));
739         if (!dev)
740                 return NULL;
741
742         if ((dev->class != PCI_CLASS_SYSTEM_PIC_IOAPIC) &&
743             (dev->class != PCI_CLASS_SYSTEM_PIC_IOXAPIC))
744         {
745                 pci_dev_put(dev);
746                 return NULL;
747         }
748
749         return dev;
750 }
751
752 static int get_gsi_base(acpi_handle handle, u32 *gsi_base)
753 {
754         acpi_status status;
755         int result = -1;
756         unsigned long gsb;
757         struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
758         union acpi_object *obj;
759         void *table;
760
761         status = acpi_evaluate_integer(handle, "_GSB", NULL, &gsb);
762         if (ACPI_SUCCESS(status)) {
763                 *gsi_base = (u32)gsb;
764                 return 0;
765         }
766
767         status = acpi_evaluate_object(handle, "_MAT", NULL, &buffer);
768         if (ACPI_FAILURE(status) || !buffer.length || !buffer.pointer)
769                 return -1;
770
771         obj = buffer.pointer;
772         if (obj->type != ACPI_TYPE_BUFFER)
773                 goto out;
774
775         table = obj->buffer.pointer;
776         switch (((acpi_table_entry_header *)table)->type) {
777         case ACPI_MADT_IOSAPIC:
778                 *gsi_base = ((struct acpi_table_iosapic *)table)->global_irq_base;
779                 result = 0;
780                 break;
781         case ACPI_MADT_IOAPIC:
782                 *gsi_base = ((struct acpi_table_ioapic *)table)->global_irq_base;
783                 result = 0;
784                 break;
785         default:
786                 break;
787         }
788  out:
789         kfree(buffer.pointer);
790         return result;
791 }
792
793 static acpi_status
794 ioapic_add(acpi_handle handle, u32 lvl, void *context, void **rv)
795 {
796         acpi_status status;
797         unsigned long sta;
798         acpi_handle tmp;
799         struct pci_dev *pdev;
800         u32 gsi_base;
801         u64 phys_addr;
802         struct acpiphp_ioapic *ioapic;
803
804         /* Evaluate _STA if present */
805         status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
806         if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
807                 return AE_CTRL_DEPTH;
808
809         /* Scan only PCI bus scope */
810         status = acpi_get_handle(handle, "_HID", &tmp);
811         if (ACPI_SUCCESS(status))
812                 return AE_CTRL_DEPTH;
813
814         if (get_gsi_base(handle, &gsi_base))
815                 return AE_OK;
816
817         ioapic = kmalloc(sizeof(*ioapic), GFP_KERNEL);
818         if (!ioapic)
819                 return AE_NO_MEMORY;
820
821         pdev = get_apic_pci_info(handle);
822         if (!pdev)
823                 goto exit_kfree;
824
825         if (pci_enable_device(pdev))
826                 goto exit_pci_dev_put;
827
828         pci_set_master(pdev);
829
830         if (pci_request_region(pdev, 0, "I/O APIC(acpiphp)"))
831                 goto exit_pci_disable_device;
832
833         phys_addr = pci_resource_start(pdev, 0);
834         if (acpi_register_ioapic(handle, phys_addr, gsi_base))
835                 goto exit_pci_release_region;
836
837         ioapic->gsi_base = gsi_base;
838         ioapic->dev = pdev;
839         spin_lock(&ioapic_list_lock);
840         list_add_tail(&ioapic->list, &ioapic_list);
841         spin_unlock(&ioapic_list_lock);
842
843         return AE_OK;
844
845  exit_pci_release_region:
846         pci_release_region(pdev, 0);
847  exit_pci_disable_device:
848         pci_disable_device(pdev);
849  exit_pci_dev_put:
850         pci_dev_put(pdev);
851  exit_kfree:
852         kfree(ioapic);
853
854         return AE_OK;
855 }
856
857 static acpi_status
858 ioapic_remove(acpi_handle handle, u32 lvl, void *context, void **rv)
859 {
860         acpi_status status;
861         unsigned long sta;
862         acpi_handle tmp;
863         u32 gsi_base;
864         struct acpiphp_ioapic *pos, *n, *ioapic = NULL;
865
866         /* Evaluate _STA if present */
867         status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
868         if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
869                 return AE_CTRL_DEPTH;
870
871         /* Scan only PCI bus scope */
872         status = acpi_get_handle(handle, "_HID", &tmp);
873         if (ACPI_SUCCESS(status))
874                 return AE_CTRL_DEPTH;
875
876         if (get_gsi_base(handle, &gsi_base))
877                 return AE_OK;
878
879         acpi_unregister_ioapic(handle, gsi_base);
880
881         spin_lock(&ioapic_list_lock);
882         list_for_each_entry_safe(pos, n, &ioapic_list, list) {
883                 if (pos->gsi_base != gsi_base)
884                         continue;
885                 ioapic = pos;
886                 list_del(&ioapic->list);
887                 break;
888         }
889         spin_unlock(&ioapic_list_lock);
890
891         if (!ioapic)
892                 return AE_OK;
893
894         pci_release_region(ioapic->dev, 0);
895         pci_disable_device(ioapic->dev);
896         pci_dev_put(ioapic->dev);
897         kfree(ioapic);
898
899         return AE_OK;
900 }
901
902 static int acpiphp_configure_ioapics(acpi_handle handle)
903 {
904         ioapic_add(handle, 0, NULL, NULL);
905         acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
906                             ACPI_UINT32_MAX, ioapic_add, NULL, NULL);
907         return 0;
908 }
909
910 static int acpiphp_unconfigure_ioapics(acpi_handle handle)
911 {
912         ioapic_remove(handle, 0, NULL, NULL);
913         acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
914                             ACPI_UINT32_MAX, ioapic_remove, NULL, NULL);
915         return 0;
916 }
917
918 static int power_on_slot(struct acpiphp_slot *slot)
919 {
920         acpi_status status;
921         struct acpiphp_func *func;
922         struct list_head *l;
923         int retval = 0;
924
925         /* if already enabled, just skip */
926         if (slot->flags & SLOT_POWEREDON)
927                 goto err_exit;
928
929         list_for_each (l, &slot->funcs) {
930                 func = list_entry(l, struct acpiphp_func, sibling);
931
932                 if (func->flags & FUNC_HAS_PS0) {
933                         dbg("%s: executing _PS0\n", __FUNCTION__);
934                         status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
935                         if (ACPI_FAILURE(status)) {
936                                 warn("%s: _PS0 failed\n", __FUNCTION__);
937                                 retval = -1;
938                                 goto err_exit;
939                         } else
940                                 break;
941                 }
942         }
943
944         /* TBD: evaluate _STA to check if the slot is enabled */
945
946         slot->flags |= SLOT_POWEREDON;
947
948  err_exit:
949         return retval;
950 }
951
952
953 static int power_off_slot(struct acpiphp_slot *slot)
954 {
955         acpi_status status;
956         struct acpiphp_func *func;
957         struct list_head *l;
958
959         int retval = 0;
960
961         /* if already disabled, just skip */
962         if ((slot->flags & SLOT_POWEREDON) == 0)
963                 goto err_exit;
964
965         list_for_each (l, &slot->funcs) {
966                 func = list_entry(l, struct acpiphp_func, sibling);
967
968                 if (func->flags & FUNC_HAS_PS3) {
969                         status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
970                         if (ACPI_FAILURE(status)) {
971                                 warn("%s: _PS3 failed\n", __FUNCTION__);
972                                 retval = -1;
973                                 goto err_exit;
974                         } else
975                                 break;
976                 }
977         }
978
979         /* TBD: evaluate _STA to check if the slot is disabled */
980
981         slot->flags &= (~SLOT_POWEREDON);
982
983  err_exit:
984         return retval;
985 }
986
987
988
989 /**
990  * acpiphp_max_busnr - return the highest reserved bus number under
991  * the given bus.
992  * @bus: bus to start search with
993  *
994  */
995 static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
996 {
997         struct list_head *tmp;
998         unsigned char max, n;
999
1000         /*
1001          * pci_bus_max_busnr will return the highest
1002          * reserved busnr for all these children.
1003          * that is equivalent to the bus->subordinate
1004          * value.  We don't want to use the parent's
1005          * bus->subordinate value because it could have
1006          * padding in it.
1007          */
1008         max = bus->secondary;
1009
1010         list_for_each(tmp, &bus->children) {
1011                 n = pci_bus_max_busnr(pci_bus_b(tmp));
1012                 if (n > max)
1013                         max = n;
1014         }
1015         return max;
1016 }
1017
1018
1019 /**
1020  * acpiphp_bus_add - add a new bus to acpi subsystem
1021  * @func: acpiphp_func of the bridge
1022  *
1023  */
1024 static int acpiphp_bus_add(struct acpiphp_func *func)
1025 {
1026         acpi_handle phandle;
1027         struct acpi_device *device, *pdevice;
1028         int ret_val;
1029
1030         acpi_get_parent(func->handle, &phandle);
1031         if (acpi_bus_get_device(phandle, &pdevice)) {
1032                 dbg("no parent device, assuming NULL\n");
1033                 pdevice = NULL;
1034         }
1035         if (!acpi_bus_get_device(func->handle, &device)) {
1036                 dbg("bus exists... trim\n");
1037                 /* this shouldn't be in here, so remove
1038                  * the bus then re-add it...
1039                  */
1040                 ret_val = acpi_bus_trim(device, 1);
1041                 dbg("acpi_bus_trim return %x\n", ret_val);
1042         }
1043
1044         ret_val = acpi_bus_add(&device, pdevice, func->handle,
1045                 ACPI_BUS_TYPE_DEVICE);
1046         if (ret_val) {
1047                 dbg("error adding bus, %x\n",
1048                         -ret_val);
1049                 goto acpiphp_bus_add_out;
1050         }
1051         /*
1052          * try to start anyway.  We could have failed to add
1053          * simply because this bus had previously been added
1054          * on another add.  Don't bother with the return value
1055          * we just keep going.
1056          */
1057         ret_val = acpi_bus_start(device);
1058
1059 acpiphp_bus_add_out:
1060         return ret_val;
1061 }
1062
1063
1064 /**
1065  * acpiphp_bus_trim - trim a bus from acpi subsystem
1066  * @handle: handle to acpi namespace
1067  *
1068  */
1069 static int acpiphp_bus_trim(acpi_handle handle)
1070 {
1071         struct acpi_device *device;
1072         int retval;
1073
1074         retval = acpi_bus_get_device(handle, &device);
1075         if (retval) {
1076                 dbg("acpi_device not found\n");
1077                 return retval;
1078         }
1079
1080         retval = acpi_bus_trim(device, 1);
1081         if (retval)
1082                 err("cannot remove from acpi list\n");
1083
1084         return retval;
1085 }
1086
1087 /**
1088  * enable_device - enable, configure a slot
1089  * @slot: slot to be enabled
1090  *
1091  * This function should be called per *physical slot*,
1092  * not per each slot object in ACPI namespace.
1093  *
1094  */
1095 static int enable_device(struct acpiphp_slot *slot)
1096 {
1097         struct pci_dev *dev;
1098         struct pci_bus *bus = slot->bridge->pci_bus;
1099         struct list_head *l;
1100         struct acpiphp_func *func;
1101         int retval = 0;
1102         int num, max, pass;
1103         acpi_status status;
1104
1105         if (slot->flags & SLOT_ENABLED)
1106                 goto err_exit;
1107
1108         /* sanity check: dev should be NULL when hot-plugged in */
1109         dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
1110         if (dev) {
1111                 /* This case shouldn't happen */
1112                 err("pci_dev structure already exists.\n");
1113                 pci_dev_put(dev);
1114                 retval = -1;
1115                 goto err_exit;
1116         }
1117
1118         num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
1119         if (num == 0) {
1120                 err("No new device found\n");
1121                 retval = -1;
1122                 goto err_exit;
1123         }
1124
1125         max = acpiphp_max_busnr(bus);
1126         for (pass = 0; pass < 2; pass++) {
1127                 list_for_each_entry(dev, &bus->devices, bus_list) {
1128                         if (PCI_SLOT(dev->devfn) != slot->device)
1129                                 continue;
1130                         if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
1131                             dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
1132                                 max = pci_scan_bridge(bus, dev, max, pass);
1133                                 if (pass && dev->subordinate)
1134                                         pci_bus_size_bridges(dev->subordinate);
1135                         }
1136                 }
1137         }
1138
1139         list_for_each (l, &slot->funcs) {
1140                 func = list_entry(l, struct acpiphp_func, sibling);
1141                 acpiphp_bus_add(func);
1142         }
1143
1144         pci_bus_assign_resources(bus);
1145         acpiphp_sanitize_bus(bus);
1146         acpiphp_set_hpp_values(slot->bridge->handle, bus);
1147         list_for_each_entry(func, &slot->funcs, sibling)
1148                 acpiphp_configure_ioapics(func->handle);
1149         pci_enable_bridges(bus);
1150         pci_bus_add_devices(bus);
1151
1152         /* associate pci_dev to our representation */
1153         list_for_each (l, &slot->funcs) {
1154                 func = list_entry(l, struct acpiphp_func, sibling);
1155                 func->pci_dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
1156                                                         func->function));
1157                 if (!func->pci_dev)
1158                         continue;
1159
1160                 if (func->pci_dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
1161                     func->pci_dev->hdr_type != PCI_HEADER_TYPE_CARDBUS)
1162                         continue;
1163
1164                 status = find_p2p_bridge(func->handle, (u32)1, bus, NULL);
1165                 if (ACPI_FAILURE(status))
1166                         warn("find_p2p_bridge failed (error code = 0x%x)\n",
1167                                 status);
1168         }
1169
1170         slot->flags |= SLOT_ENABLED;
1171
1172  err_exit:
1173         return retval;
1174 }
1175
1176 static void disable_bridges(struct pci_bus *bus)
1177 {
1178         struct pci_dev *dev;
1179         list_for_each_entry(dev, &bus->devices, bus_list) {
1180                 if (dev->subordinate) {
1181                         disable_bridges(dev->subordinate);
1182                         pci_disable_device(dev);
1183                 }
1184         }
1185 }
1186
1187 /**
1188  * disable_device - disable a slot
1189  */
1190 static int disable_device(struct acpiphp_slot *slot)
1191 {
1192         int retval = 0;
1193         struct acpiphp_func *func;
1194         struct list_head *l;
1195
1196         /* is this slot already disabled? */
1197         if (!(slot->flags & SLOT_ENABLED))
1198                 goto err_exit;
1199
1200         list_for_each (l, &slot->funcs) {
1201                 func = list_entry(l, struct acpiphp_func, sibling);
1202
1203                 if (func->bridge) {
1204                         /* cleanup p2p bridges under this P2P bridge */
1205                         cleanup_p2p_bridge(func->bridge->handle,
1206                                                 (u32)1, NULL, NULL);
1207                         func->bridge = NULL;
1208                 }
1209
1210                 if (func->pci_dev) {
1211                         pci_stop_bus_device(func->pci_dev);
1212                         if (func->pci_dev->subordinate) {
1213                                 disable_bridges(func->pci_dev->subordinate);
1214                                 pci_disable_device(func->pci_dev);
1215                         }
1216                 }
1217         }
1218
1219         list_for_each (l, &slot->funcs) {
1220                 func = list_entry(l, struct acpiphp_func, sibling);
1221
1222                 acpiphp_unconfigure_ioapics(func->handle);
1223                 acpiphp_bus_trim(func->handle);
1224                 /* try to remove anyway.
1225                  * acpiphp_bus_add might have been failed */
1226
1227                 if (!func->pci_dev)
1228                         continue;
1229
1230                 pci_remove_bus_device(func->pci_dev);
1231                 pci_dev_put(func->pci_dev);
1232                 func->pci_dev = NULL;
1233         }
1234
1235         slot->flags &= (~SLOT_ENABLED);
1236
1237  err_exit:
1238         return retval;
1239 }
1240
1241
1242 /**
1243  * get_slot_status - get ACPI slot status
1244  *
1245  * if a slot has _STA for each function and if any one of them
1246  * returned non-zero status, return it
1247  *
1248  * if a slot doesn't have _STA and if any one of its functions'
1249  * configuration space is configured, return 0x0f as a _STA
1250  *
1251  * otherwise return 0
1252  */
1253 static unsigned int get_slot_status(struct acpiphp_slot *slot)
1254 {
1255         acpi_status status;
1256         unsigned long sta = 0;
1257         u32 dvid;
1258         struct list_head *l;
1259         struct acpiphp_func *func;
1260
1261         list_for_each (l, &slot->funcs) {
1262                 func = list_entry(l, struct acpiphp_func, sibling);
1263
1264                 if (func->flags & FUNC_HAS_STA) {
1265                         status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
1266                         if (ACPI_SUCCESS(status) && sta)
1267                                 break;
1268                 } else {
1269                         pci_bus_read_config_dword(slot->bridge->pci_bus,
1270                                                   PCI_DEVFN(slot->device,
1271                                                             func->function),
1272                                                   PCI_VENDOR_ID, &dvid);
1273                         if (dvid != 0xffffffff) {
1274                                 sta = ACPI_STA_ALL;
1275                                 break;
1276                         }
1277                 }
1278         }
1279
1280         return (unsigned int)sta;
1281 }
1282
1283 /**
1284  * acpiphp_eject_slot - physically eject the slot
1285  */
1286 static int acpiphp_eject_slot(struct acpiphp_slot *slot)
1287 {
1288         acpi_status status;
1289         struct acpiphp_func *func;
1290         struct list_head *l;
1291         struct acpi_object_list arg_list;
1292         union acpi_object arg;
1293
1294         list_for_each (l, &slot->funcs) {
1295                 func = list_entry(l, struct acpiphp_func, sibling);
1296
1297                 /* We don't want to call _EJ0 on non-existing functions. */
1298                 if ((func->flags & FUNC_HAS_EJ0)) {
1299                         /* _EJ0 method take one argument */
1300                         arg_list.count = 1;
1301                         arg_list.pointer = &arg;
1302                         arg.type = ACPI_TYPE_INTEGER;
1303                         arg.integer.value = 1;
1304
1305                         status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
1306                         if (ACPI_FAILURE(status)) {
1307                                 warn("%s: _EJ0 failed\n", __FUNCTION__);
1308                                 return -1;
1309                         } else
1310                                 break;
1311                 }
1312         }
1313         return 0;
1314 }
1315
1316 /**
1317  * acpiphp_check_bridge - re-enumerate devices
1318  *
1319  * Iterate over all slots under this bridge and make sure that if a
1320  * card is present they are enabled, and if not they are disabled.
1321  */
1322 static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
1323 {
1324         struct acpiphp_slot *slot;
1325         int retval = 0;
1326         int enabled, disabled;
1327
1328         enabled = disabled = 0;
1329
1330         for (slot = bridge->slots; slot; slot = slot->next) {
1331                 unsigned int status = get_slot_status(slot);
1332                 if (slot->flags & SLOT_ENABLED) {
1333                         if (status == ACPI_STA_ALL)
1334                                 continue;
1335                         retval = acpiphp_disable_slot(slot);
1336                         if (retval) {
1337                                 err("Error occurred in disabling\n");
1338                                 goto err_exit;
1339                         } else {
1340                                 acpiphp_eject_slot(slot);
1341                         }
1342                         disabled++;
1343                 } else {
1344                         if (status != ACPI_STA_ALL)
1345                                 continue;
1346                         retval = acpiphp_enable_slot(slot);
1347                         if (retval) {
1348                                 err("Error occurred in enabling\n");
1349                                 goto err_exit;
1350                         }
1351                         enabled++;
1352                 }
1353         }
1354
1355         dbg("%s: %d enabled, %d disabled\n", __FUNCTION__, enabled, disabled);
1356
1357  err_exit:
1358         return retval;
1359 }
1360
1361 static void program_hpp(struct pci_dev *dev, struct acpiphp_bridge *bridge)
1362 {
1363         u16 pci_cmd, pci_bctl;
1364         struct pci_dev *cdev;
1365
1366         /* Program hpp values for this device */
1367         if (!(dev->hdr_type == PCI_HEADER_TYPE_NORMAL ||
1368                         (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
1369                         (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)))
1370                 return;
1371
1372         pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
1373                         bridge->hpp.t0->cache_line_size);
1374         pci_write_config_byte(dev, PCI_LATENCY_TIMER,
1375                         bridge->hpp.t0->latency_timer);
1376         pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
1377         if (bridge->hpp.t0->enable_serr)
1378                 pci_cmd |= PCI_COMMAND_SERR;
1379         else
1380                 pci_cmd &= ~PCI_COMMAND_SERR;
1381         if (bridge->hpp.t0->enable_perr)
1382                 pci_cmd |= PCI_COMMAND_PARITY;
1383         else
1384                 pci_cmd &= ~PCI_COMMAND_PARITY;
1385         pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
1386
1387         /* Program bridge control value and child devices */
1388         if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
1389                 pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
1390                                 bridge->hpp.t0->latency_timer);
1391                 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
1392                 if (bridge->hpp.t0->enable_serr)
1393                         pci_bctl |= PCI_BRIDGE_CTL_SERR;
1394                 else
1395                         pci_bctl &= ~PCI_BRIDGE_CTL_SERR;
1396                 if (bridge->hpp.t0->enable_perr)
1397                         pci_bctl |= PCI_BRIDGE_CTL_PARITY;
1398                 else
1399                         pci_bctl &= ~PCI_BRIDGE_CTL_PARITY;
1400                 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
1401                 if (dev->subordinate) {
1402                         list_for_each_entry(cdev, &dev->subordinate->devices,
1403                                         bus_list)
1404                                 program_hpp(cdev, bridge);
1405                 }
1406         }
1407 }
1408
1409 static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus)
1410 {
1411         struct acpiphp_bridge bridge;
1412         struct pci_dev *dev;
1413
1414         memset(&bridge, 0, sizeof(bridge));
1415         bridge.handle = handle;
1416         bridge.pci_bus = bus;
1417         bridge.pci_dev = bus->self;
1418         decode_hpp(&bridge);
1419         list_for_each_entry(dev, &bus->devices, bus_list)
1420                 program_hpp(dev, &bridge);
1421
1422 }
1423
1424 /*
1425  * Remove devices for which we could not assign resources, call
1426  * arch specific code to fix-up the bus
1427  */
1428 static void acpiphp_sanitize_bus(struct pci_bus *bus)
1429 {
1430         struct pci_dev *dev;
1431         int i;
1432         unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
1433
1434         list_for_each_entry(dev, &bus->devices, bus_list) {
1435                 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
1436                         struct resource *res = &dev->resource[i];
1437                         if ((res->flags & type_mask) && !res->start &&
1438                                         res->end) {
1439                                 /* Could not assign a required resources
1440                                  * for this device, remove it */
1441                                 pci_remove_bus_device(dev);
1442                                 break;
1443                         }
1444                 }
1445         }
1446 }
1447
1448 /* Program resources in newly inserted bridge */
1449 static int acpiphp_configure_bridge (acpi_handle handle)
1450 {
1451         struct acpi_pci_id pci_id;
1452         struct pci_bus *bus;
1453
1454         if (ACPI_FAILURE(acpi_get_pci_id(handle, &pci_id))) {
1455                 err("cannot get PCI domain and bus number for bridge\n");
1456                 return -EINVAL;
1457         }
1458         bus = pci_find_bus(pci_id.segment, pci_id.bus);
1459         if (!bus) {
1460                 err("cannot find bus %d:%d\n",
1461                                 pci_id.segment, pci_id.bus);
1462                 return -EINVAL;
1463         }
1464
1465         pci_bus_size_bridges(bus);
1466         pci_bus_assign_resources(bus);
1467         acpiphp_sanitize_bus(bus);
1468         acpiphp_set_hpp_values(handle, bus);
1469         pci_enable_bridges(bus);
1470         acpiphp_configure_ioapics(handle);
1471         return 0;
1472 }
1473
1474 static void handle_bridge_insertion(acpi_handle handle, u32 type)
1475 {
1476         struct acpi_device *device, *pdevice;
1477         acpi_handle phandle;
1478
1479         if ((type != ACPI_NOTIFY_BUS_CHECK) &&
1480                         (type != ACPI_NOTIFY_DEVICE_CHECK)) {
1481                 err("unexpected notification type %d\n", type);
1482                 return;
1483         }
1484
1485         acpi_get_parent(handle, &phandle);
1486         if (acpi_bus_get_device(phandle, &pdevice)) {
1487                 dbg("no parent device, assuming NULL\n");
1488                 pdevice = NULL;
1489         }
1490         if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
1491                 err("cannot add bridge to acpi list\n");
1492                 return;
1493         }
1494         if (!acpiphp_configure_bridge(handle) &&
1495                 !acpi_bus_start(device))
1496                 add_bridge(handle);
1497         else
1498                 err("cannot configure and start bridge\n");
1499
1500 }
1501
1502 /*
1503  * ACPI event handlers
1504  */
1505
1506 /**
1507  * handle_hotplug_event_bridge - handle ACPI event on bridges
1508  *
1509  * @handle: Notify()'ed acpi_handle
1510  * @type: Notify code
1511  * @context: pointer to acpiphp_bridge structure
1512  *
1513  * handles ACPI event notification on {host,p2p} bridges
1514  *
1515  */
1516 static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context)
1517 {
1518         struct acpiphp_bridge *bridge;
1519         char objname[64];
1520         struct acpi_buffer buffer = { .length = sizeof(objname),
1521                                       .pointer = objname };
1522         struct acpi_device *device;
1523
1524         if (acpi_bus_get_device(handle, &device)) {
1525                 /* This bridge must have just been physically inserted */
1526                 handle_bridge_insertion(handle, type);
1527                 return;
1528         }
1529
1530         bridge = acpiphp_handle_to_bridge(handle);
1531         if (!bridge) {
1532                 err("cannot get bridge info\n");
1533                 return;
1534         }
1535
1536         acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1537
1538         switch (type) {
1539         case ACPI_NOTIFY_BUS_CHECK:
1540                 /* bus re-enumerate */
1541                 dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
1542                 acpiphp_check_bridge(bridge);
1543                 break;
1544
1545         case ACPI_NOTIFY_DEVICE_CHECK:
1546                 /* device check */
1547                 dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
1548                 acpiphp_check_bridge(bridge);
1549                 break;
1550
1551         case ACPI_NOTIFY_DEVICE_WAKE:
1552                 /* wake event */
1553                 dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
1554                 break;
1555
1556         case ACPI_NOTIFY_EJECT_REQUEST:
1557                 /* request device eject */
1558                 dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
1559                 if ((bridge->type != BRIDGE_TYPE_HOST) &&
1560                     (bridge->flags & BRIDGE_HAS_EJ0)) {
1561                         struct acpiphp_slot *slot;
1562                         slot = bridge->func->slot;
1563                         if (!acpiphp_disable_slot(slot))
1564                                 acpiphp_eject_slot(slot);
1565                 }
1566                 break;
1567
1568         case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1569                 printk(KERN_ERR "Device %s cannot be configured due"
1570                                 " to a frequency mismatch\n", objname);
1571                 break;
1572
1573         case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1574                 printk(KERN_ERR "Device %s cannot be configured due"
1575                                 " to a bus mode mismatch\n", objname);
1576                 break;
1577
1578         case ACPI_NOTIFY_POWER_FAULT:
1579                 printk(KERN_ERR "Device %s has suffered a power fault\n",
1580                                 objname);
1581                 break;
1582
1583         default:
1584                 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1585                 break;
1586         }
1587 }
1588
1589 /**
1590  * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1591  *
1592  * @handle: Notify()'ed acpi_handle
1593  * @type: Notify code
1594  * @context: pointer to acpiphp_func structure
1595  *
1596  * handles ACPI event notification on slots
1597  *
1598  */
1599 static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context)
1600 {
1601         struct acpiphp_func *func;
1602         char objname[64];
1603         struct acpi_buffer buffer = { .length = sizeof(objname),
1604                                       .pointer = objname };
1605
1606         acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1607
1608         func = (struct acpiphp_func *)context;
1609
1610         switch (type) {
1611         case ACPI_NOTIFY_BUS_CHECK:
1612                 /* bus re-enumerate */
1613                 dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
1614                 acpiphp_enable_slot(func->slot);
1615                 break;
1616
1617         case ACPI_NOTIFY_DEVICE_CHECK:
1618                 /* device check : re-enumerate from parent bus */
1619                 dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
1620                 acpiphp_check_bridge(func->slot->bridge);
1621                 break;
1622
1623         case ACPI_NOTIFY_DEVICE_WAKE:
1624                 /* wake event */
1625                 dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
1626                 break;
1627
1628         case ACPI_NOTIFY_EJECT_REQUEST:
1629                 /* request device eject */
1630                 dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
1631                 if (!(acpiphp_disable_slot(func->slot)))
1632                         acpiphp_eject_slot(func->slot);
1633                 break;
1634
1635         default:
1636                 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1637                 break;
1638         }
1639 }
1640
1641
1642 static acpi_status
1643 find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1644 {
1645         int *count = (int *)context;
1646
1647         if (acpi_root_bridge(handle)) {
1648                 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1649                                 handle_hotplug_event_bridge, NULL);
1650                         (*count)++;
1651         }
1652         return AE_OK ;
1653 }
1654
1655 static struct acpi_pci_driver acpi_pci_hp_driver = {
1656         .add =          add_bridge,
1657         .remove =       remove_bridge,
1658 };
1659
1660 /**
1661  * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
1662  *
1663  */
1664 int __init acpiphp_glue_init(void)
1665 {
1666         int num = 0;
1667
1668         acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1669                         ACPI_UINT32_MAX, find_root_bridges, &num, NULL);
1670
1671         if (num <= 0)
1672                 return -1;
1673         else
1674                 acpi_pci_register_driver(&acpi_pci_hp_driver);
1675
1676         return 0;
1677 }
1678
1679
1680 /**
1681  * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1682  *
1683  * This function frees all data allocated in acpiphp_glue_init()
1684  */
1685 void __exit acpiphp_glue_exit(void)
1686 {
1687         acpi_pci_unregister_driver(&acpi_pci_hp_driver);
1688 }
1689
1690
1691 /**
1692  * acpiphp_get_num_slots - count number of slots in a system
1693  */
1694 int __init acpiphp_get_num_slots(void)
1695 {
1696         struct acpiphp_bridge *bridge;
1697         int num_slots = 0;
1698
1699         list_for_each_entry (bridge, &bridge_list, list) {
1700                 dbg("Bus %04x:%02x has %d slot%s\n",
1701                                 pci_domain_nr(bridge->pci_bus),
1702                                 bridge->pci_bus->number, bridge->nr_slots,
1703                                 bridge->nr_slots == 1 ? "" : "s");
1704                 num_slots += bridge->nr_slots;
1705         }
1706
1707         dbg("Total %d slots\n", num_slots);
1708         return num_slots;
1709 }
1710
1711
1712 #if 0
1713 /**
1714  * acpiphp_for_each_slot - call function for each slot
1715  * @fn: callback function
1716  * @data: context to be passed to callback function
1717  *
1718  */
1719 static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
1720 {
1721         struct list_head *node;
1722         struct acpiphp_bridge *bridge;
1723         struct acpiphp_slot *slot;
1724         int retval = 0;
1725
1726         list_for_each (node, &bridge_list) {
1727                 bridge = (struct acpiphp_bridge *)node;
1728                 for (slot = bridge->slots; slot; slot = slot->next) {
1729                         retval = fn(slot, data);
1730                         if (!retval)
1731                                 goto err_exit;
1732                 }
1733         }
1734
1735  err_exit:
1736         return retval;
1737 }
1738 #endif
1739
1740
1741 /**
1742  * acpiphp_enable_slot - power on slot
1743  */
1744 int acpiphp_enable_slot(struct acpiphp_slot *slot)
1745 {
1746         int retval;
1747
1748         mutex_lock(&slot->crit_sect);
1749
1750         /* wake up all functions */
1751         retval = power_on_slot(slot);
1752         if (retval)
1753                 goto err_exit;
1754
1755         if (get_slot_status(slot) == ACPI_STA_ALL) {
1756                 /* configure all functions */
1757                 retval = enable_device(slot);
1758                 if (retval)
1759                         power_off_slot(slot);
1760         } else {
1761                 dbg("%s: Slot status is not ACPI_STA_ALL\n", __FUNCTION__);
1762                 power_off_slot(slot);
1763         }
1764
1765  err_exit:
1766         mutex_unlock(&slot->crit_sect);
1767         return retval;
1768 }
1769
1770 /**
1771  * acpiphp_disable_slot - power off slot
1772  */
1773 int acpiphp_disable_slot(struct acpiphp_slot *slot)
1774 {
1775         int retval = 0;
1776
1777         mutex_lock(&slot->crit_sect);
1778
1779         /* unconfigure all functions */
1780         retval = disable_device(slot);
1781         if (retval)
1782                 goto err_exit;
1783
1784         /* power off all functions */
1785         retval = power_off_slot(slot);
1786         if (retval)
1787                 goto err_exit;
1788
1789  err_exit:
1790         mutex_unlock(&slot->crit_sect);
1791         return retval;
1792 }
1793
1794
1795 /*
1796  * slot enabled:  1
1797  * slot disabled: 0
1798  */
1799 u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1800 {
1801         return (slot->flags & SLOT_POWEREDON);
1802 }
1803
1804
1805 /*
1806  * latch   open:  1
1807  * latch closed:  0
1808  */
1809 u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1810 {
1811         unsigned int sta;
1812
1813         sta = get_slot_status(slot);
1814
1815         return (sta & ACPI_STA_SHOW_IN_UI) ? 0 : 1;
1816 }
1817
1818
1819 /*
1820  * adapter presence : 1
1821  *          absence : 0
1822  */
1823 u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1824 {
1825         unsigned int sta;
1826
1827         sta = get_slot_status(slot);
1828
1829         return (sta == 0) ? 0 : 1;
1830 }
1831
1832
1833 /*
1834  * pci address (seg/bus/dev)
1835  */
1836 u32 acpiphp_get_address(struct acpiphp_slot *slot)
1837 {
1838         u32 address;
1839         struct pci_bus *pci_bus = slot->bridge->pci_bus;
1840
1841         address = (pci_domain_nr(pci_bus) << 16) |
1842                   (pci_bus->number << 8) |
1843                   slot->device;
1844
1845         return address;
1846 }