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