1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42#include <linux/init.h>
43#include <linux/module.h>
44
45#include <linux/kernel.h>
46#include <linux/pci.h>
47#include <linux/pci_hotplug.h>
48#include <linux/pci-acpi.h>
49#include <linux/mutex.h>
50#include <linux/slab.h>
51#include <linux/acpi.h>
52
53#include "../pci.h"
54#include "acpiphp.h"
55
56static LIST_HEAD(bridge_list);
57static DEFINE_MUTEX(bridge_mutex);
58
59#define MY_NAME "acpiphp_glue"
60
61static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
62static void acpiphp_sanitize_bus(struct pci_bus *bus);
63static void acpiphp_set_hpp_values(struct pci_bus *bus);
64static void hotplug_event_func(acpi_handle handle, u32 type, void *context);
65static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context);
66static void free_bridge(struct kref *kref);
67
68
69static acpi_status
70is_pci_dock_device(acpi_handle handle, u32 lvl, void *context, void **rv)
71{
72 int *count = (int *)context;
73
74 if (is_dock_device(handle)) {
75 (*count)++;
76 return AE_CTRL_TERMINATE;
77 } else {
78 return AE_OK;
79 }
80}
81
82static inline void get_bridge(struct acpiphp_bridge *bridge)
83{
84 kref_get(&bridge->ref);
85}
86
87static inline void put_bridge(struct acpiphp_bridge *bridge)
88{
89 kref_put(&bridge->ref, free_bridge);
90}
91
92static void free_bridge(struct kref *kref)
93{
94 struct acpiphp_bridge *bridge;
95 struct acpiphp_slot *slot, *next;
96 struct acpiphp_func *func, *tmp;
97
98 bridge = container_of(kref, struct acpiphp_bridge, ref);
99
100 list_for_each_entry_safe(slot, next, &bridge->slots, node) {
101 list_for_each_entry_safe(func, tmp, &slot->funcs, sibling) {
102 kfree(func);
103 }
104 kfree(slot);
105 }
106
107
108 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)
109 put_bridge(bridge->func->slot->bridge);
110 put_device(&bridge->pci_bus->dev);
111 pci_dev_put(bridge->pci_dev);
112 kfree(bridge);
113}
114
115
116
117
118
119
120
121
122static int post_dock_fixups(struct notifier_block *nb, unsigned long val,
123 void *v)
124{
125 struct acpiphp_func *func = container_of(nb, struct acpiphp_func, nb);
126 struct pci_bus *bus = func->slot->bridge->pci_bus;
127 u32 buses;
128
129 if (!bus->self)
130 return NOTIFY_OK;
131
132
133
134
135 pci_read_config_dword(bus->self,
136 PCI_PRIMARY_BUS,
137 &buses);
138
139 if (((buses >> 8) & 0xff) != bus->busn_res.start) {
140 buses = (buses & 0xff000000)
141 | ((unsigned int)(bus->primary) << 0)
142 | ((unsigned int)(bus->busn_res.start) << 8)
143 | ((unsigned int)(bus->busn_res.end) << 16);
144 pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
145 }
146 return NOTIFY_OK;
147}
148
149
150static const struct acpi_dock_ops acpiphp_dock_ops = {
151 .handler = hotplug_event_func,
152};
153
154
155static bool device_is_managed_by_native_pciehp(struct pci_dev *pdev)
156{
157 u32 reg32;
158 acpi_handle tmp;
159 struct acpi_pci_root *root;
160
161
162 if (pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, ®32))
163 return false;
164 if (!(reg32 & PCI_EXP_SLTCAP_HPC))
165 return false;
166
167
168
169
170
171 tmp = acpi_find_root_bridge_handle(pdev);
172 if (!tmp)
173 return false;
174 root = acpi_pci_find_root(tmp);
175 if (!root)
176 return false;
177 if (!(root->osc_control_set & OSC_PCI_EXPRESS_NATIVE_HP_CONTROL))
178 return false;
179
180 return true;
181}
182
183static void acpiphp_dock_init(void *data)
184{
185 struct acpiphp_func *func = data;
186
187 get_bridge(func->slot->bridge);
188}
189
190static void acpiphp_dock_release(void *data)
191{
192 struct acpiphp_func *func = data;
193
194 put_bridge(func->slot->bridge);
195}
196
197
198static acpi_status
199register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
200{
201 struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
202 struct acpiphp_slot *slot;
203 struct acpiphp_func *newfunc;
204 acpi_handle tmp;
205 acpi_status status = AE_OK;
206 unsigned long long adr, sun;
207 int device, function, retval, found = 0;
208 struct pci_bus *pbus = bridge->pci_bus;
209 struct pci_dev *pdev;
210 u32 val;
211
212 if (!acpi_pci_check_ejectable(pbus, handle) && !is_dock_device(handle))
213 return AE_OK;
214
215 status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
216 if (ACPI_FAILURE(status)) {
217 warn("can't evaluate _ADR (%#x)\n", status);
218 return AE_OK;
219 }
220
221 device = (adr >> 16) & 0xffff;
222 function = adr & 0xffff;
223
224 pdev = bridge->pci_dev;
225 if (pdev && device_is_managed_by_native_pciehp(pdev))
226 return AE_OK;
227
228 newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
229 if (!newfunc)
230 return AE_NO_MEMORY;
231
232 newfunc->handle = handle;
233 newfunc->function = function;
234
235 if (ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp)))
236 newfunc->flags = FUNC_HAS_EJ0;
237
238 if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
239 newfunc->flags |= FUNC_HAS_STA;
240
241 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
242 newfunc->flags |= FUNC_HAS_PS0;
243
244 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
245 newfunc->flags |= FUNC_HAS_PS3;
246
247 if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp)))
248 newfunc->flags |= FUNC_HAS_DCK;
249
250 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
251 if (ACPI_FAILURE(status)) {
252
253
254
255
256 sun = bridge->nr_slots+1;
257 }
258
259
260 list_for_each_entry(slot, &bridge->slots, node)
261 if (slot->device == device) {
262 if (slot->sun != sun)
263 warn("sibling found, but _SUN doesn't match!\n");
264 found = 1;
265 break;
266 }
267
268 if (!found) {
269 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
270 if (!slot) {
271 kfree(newfunc);
272 return AE_NO_MEMORY;
273 }
274
275 slot->bridge = bridge;
276 slot->device = device;
277 slot->sun = sun;
278 INIT_LIST_HEAD(&slot->funcs);
279 mutex_init(&slot->crit_sect);
280
281 mutex_lock(&bridge_mutex);
282 list_add_tail(&slot->node, &bridge->slots);
283 mutex_unlock(&bridge_mutex);
284 bridge->nr_slots++;
285
286 dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
287 slot->sun, pci_domain_nr(pbus), pbus->number, device);
288 retval = acpiphp_register_hotplug_slot(slot);
289 if (retval) {
290 if (retval == -EBUSY)
291 warn("Slot %llu already registered by another "
292 "hotplug driver\n", slot->sun);
293 else
294 warn("acpiphp_register_hotplug_slot failed "
295 "(err code = 0x%x)\n", retval);
296 goto err_exit;
297 }
298 }
299
300 newfunc->slot = slot;
301 mutex_lock(&bridge_mutex);
302 list_add_tail(&newfunc->sibling, &slot->funcs);
303 mutex_unlock(&bridge_mutex);
304
305 if (pci_bus_read_dev_vendor_id(pbus, PCI_DEVFN(device, function),
306 &val, 60*1000))
307 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
308
309 if (is_dock_device(handle)) {
310
311
312
313
314 newfunc->flags &= ~FUNC_HAS_EJ0;
315 if (register_hotplug_dock_device(handle,
316 &acpiphp_dock_ops, newfunc,
317 acpiphp_dock_init, acpiphp_dock_release))
318 dbg("failed to register dock device\n");
319
320
321
322
323
324 newfunc->nb.notifier_call = post_dock_fixups;
325 if (register_dock_notifier(&newfunc->nb))
326 dbg("failed to register a dock notifier");
327 }
328
329
330 if (!(newfunc->flags & FUNC_HAS_DCK)) {
331 status = acpi_install_notify_handler(handle,
332 ACPI_SYSTEM_NOTIFY,
333 handle_hotplug_event_func,
334 newfunc);
335
336 if (ACPI_FAILURE(status))
337 err("failed to register interrupt notify handler\n");
338 } else
339 status = AE_OK;
340
341 return status;
342
343 err_exit:
344 bridge->nr_slots--;
345 mutex_lock(&bridge_mutex);
346 list_del(&slot->node);
347 mutex_unlock(&bridge_mutex);
348 kfree(slot);
349 kfree(newfunc);
350
351 return AE_OK;
352}
353
354
355
356static int detect_ejectable_slots(acpi_handle handle)
357{
358 int found = acpi_pci_detect_ejectable(handle);
359 if (!found) {
360 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
361 is_pci_dock_device, NULL, (void *)&found, NULL);
362 }
363 return found;
364}
365
366
367static void init_bridge_misc(struct acpiphp_bridge *bridge)
368{
369 acpi_status status;
370
371
372 mutex_lock(&bridge_mutex);
373 list_add(&bridge->list, &bridge_list);
374 mutex_unlock(&bridge_mutex);
375
376
377 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
378 register_slot, NULL, bridge, NULL);
379 if (ACPI_FAILURE(status)) {
380 mutex_lock(&bridge_mutex);
381 list_del(&bridge->list);
382 mutex_unlock(&bridge_mutex);
383 return;
384 }
385
386
387 if (!pci_is_root_bus(bridge->pci_bus)) {
388 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
389 status = acpi_remove_notify_handler(bridge->func->handle,
390 ACPI_SYSTEM_NOTIFY,
391 handle_hotplug_event_func);
392 if (ACPI_FAILURE(status))
393 err("failed to remove notify handler\n");
394 }
395 status = acpi_install_notify_handler(bridge->handle,
396 ACPI_SYSTEM_NOTIFY,
397 handle_hotplug_event_bridge,
398 bridge);
399
400 if (ACPI_FAILURE(status)) {
401 err("failed to register interrupt notify handler\n");
402 }
403 }
404}
405
406
407
408static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
409{
410 struct acpiphp_bridge *bridge;
411 struct acpiphp_slot *slot;
412 struct acpiphp_func *func = NULL;
413
414 mutex_lock(&bridge_mutex);
415 list_for_each_entry(bridge, &bridge_list, list) {
416 list_for_each_entry(slot, &bridge->slots, node) {
417 list_for_each_entry(func, &slot->funcs, sibling) {
418 if (func->handle == handle) {
419 get_bridge(func->slot->bridge);
420 mutex_unlock(&bridge_mutex);
421 return func;
422 }
423 }
424 }
425 }
426 mutex_unlock(&bridge_mutex);
427
428 return NULL;
429}
430
431
432static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
433{
434 struct acpiphp_bridge *bridge;
435
436 mutex_lock(&bridge_mutex);
437 list_for_each_entry(bridge, &bridge_list, list)
438 if (bridge->handle == handle) {
439 get_bridge(bridge);
440 mutex_unlock(&bridge_mutex);
441 return bridge;
442 }
443 mutex_unlock(&bridge_mutex);
444
445 return NULL;
446}
447
448static void cleanup_bridge(struct acpiphp_bridge *bridge)
449{
450 struct acpiphp_slot *slot;
451 struct acpiphp_func *func;
452 acpi_status status;
453 acpi_handle handle = bridge->handle;
454
455 if (!pci_is_root_bus(bridge->pci_bus)) {
456 status = acpi_remove_notify_handler(handle,
457 ACPI_SYSTEM_NOTIFY,
458 handle_hotplug_event_bridge);
459 if (ACPI_FAILURE(status))
460 err("failed to remove notify handler\n");
461 }
462
463 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
464 status = acpi_install_notify_handler(bridge->func->handle,
465 ACPI_SYSTEM_NOTIFY,
466 handle_hotplug_event_func,
467 bridge->func);
468 if (ACPI_FAILURE(status))
469 err("failed to install interrupt notify handler\n");
470 }
471
472 list_for_each_entry(slot, &bridge->slots, node) {
473 list_for_each_entry(func, &slot->funcs, sibling) {
474 if (is_dock_device(func->handle)) {
475 unregister_hotplug_dock_device(func->handle);
476 unregister_dock_notifier(&func->nb);
477 }
478 if (!(func->flags & FUNC_HAS_DCK)) {
479 status = acpi_remove_notify_handler(func->handle,
480 ACPI_SYSTEM_NOTIFY,
481 handle_hotplug_event_func);
482 if (ACPI_FAILURE(status))
483 err("failed to remove notify handler\n");
484 }
485 }
486 acpiphp_unregister_hotplug_slot(slot);
487 }
488
489 mutex_lock(&bridge_mutex);
490 list_del(&bridge->list);
491 mutex_unlock(&bridge_mutex);
492}
493
494static int power_on_slot(struct acpiphp_slot *slot)
495{
496 acpi_status status;
497 struct acpiphp_func *func;
498 int retval = 0;
499
500
501 if (slot->flags & SLOT_POWEREDON)
502 goto err_exit;
503
504 list_for_each_entry(func, &slot->funcs, sibling) {
505 if (func->flags & FUNC_HAS_PS0) {
506 dbg("%s: executing _PS0\n", __func__);
507 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
508 if (ACPI_FAILURE(status)) {
509 warn("%s: _PS0 failed\n", __func__);
510 retval = -1;
511 goto err_exit;
512 } else
513 break;
514 }
515 }
516
517
518
519 slot->flags |= SLOT_POWEREDON;
520
521 err_exit:
522 return retval;
523}
524
525
526static int power_off_slot(struct acpiphp_slot *slot)
527{
528 acpi_status status;
529 struct acpiphp_func *func;
530
531 int retval = 0;
532
533
534 if ((slot->flags & SLOT_POWEREDON) == 0)
535 goto err_exit;
536
537 list_for_each_entry(func, &slot->funcs, sibling) {
538 if (func->flags & FUNC_HAS_PS3) {
539 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
540 if (ACPI_FAILURE(status)) {
541 warn("%s: _PS3 failed\n", __func__);
542 retval = -1;
543 goto err_exit;
544 } else
545 break;
546 }
547 }
548
549
550
551 slot->flags &= (~SLOT_POWEREDON);
552
553 err_exit:
554 return retval;
555}
556
557
558
559
560
561
562
563static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
564{
565 struct list_head *tmp;
566 unsigned char max, n;
567
568
569
570
571
572
573
574
575
576 max = bus->busn_res.start;
577
578 list_for_each(tmp, &bus->children) {
579 n = pci_bus_max_busnr(pci_bus_b(tmp));
580 if (n > max)
581 max = n;
582 }
583 return max;
584}
585
586
587
588
589
590
591static int acpiphp_bus_add(struct acpiphp_func *func)
592{
593 struct acpi_device *device;
594 int ret_val;
595
596 if (!acpi_bus_get_device(func->handle, &device)) {
597 dbg("bus exists... trim\n");
598
599
600
601 acpi_bus_trim(device);
602 }
603
604 ret_val = acpi_bus_scan(func->handle);
605 if (!ret_val)
606 ret_val = acpi_bus_get_device(func->handle, &device);
607
608 if (ret_val)
609 dbg("error adding bus, %x\n", -ret_val);
610
611 return ret_val;
612}
613
614
615
616
617
618
619static int acpiphp_bus_trim(acpi_handle handle)
620{
621 struct acpi_device *device;
622 int retval;
623
624 retval = acpi_bus_get_device(handle, &device);
625 if (retval) {
626 dbg("acpi_device not found\n");
627 return retval;
628 }
629
630 acpi_bus_trim(device);
631 return 0;
632}
633
634static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
635{
636 struct acpiphp_func *func;
637 union acpi_object params[2];
638 struct acpi_object_list arg_list;
639
640 list_for_each_entry(func, &slot->funcs, sibling) {
641 arg_list.count = 2;
642 arg_list.pointer = params;
643 params[0].type = ACPI_TYPE_INTEGER;
644 params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
645 params[1].type = ACPI_TYPE_INTEGER;
646 params[1].integer.value = 1;
647
648 acpi_evaluate_object(func->handle, "_REG", &arg_list, NULL);
649 }
650}
651
652static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
653{
654 struct acpiphp_func *func;
655
656 if (!dev->subordinate)
657 return;
658
659
660 if (dev->is_hotplug_bridge)
661 return;
662
663 if (PCI_SLOT(dev->devfn) != slot->device)
664 return;
665
666 list_for_each_entry(func, &slot->funcs, sibling) {
667 if (PCI_FUNC(dev->devfn) == func->function) {
668
669 if ((detect_ejectable_slots(func->handle) > 0))
670 dev->is_hotplug_bridge = 1;
671 break;
672 }
673 }
674}
675
676
677
678
679
680
681
682
683static int __ref enable_device(struct acpiphp_slot *slot)
684{
685 struct pci_dev *dev;
686 struct pci_bus *bus = slot->bridge->pci_bus;
687 struct acpiphp_func *func;
688 int num, max, pass;
689 LIST_HEAD(add_list);
690
691 if (slot->flags & SLOT_ENABLED)
692 goto err_exit;
693
694 list_for_each_entry(func, &slot->funcs, sibling)
695 acpiphp_bus_add(func);
696
697 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
698 if (num == 0) {
699
700 dbg("No new device found\n");
701 goto err_exit;
702 }
703
704 max = acpiphp_max_busnr(bus);
705 for (pass = 0; pass < 2; pass++) {
706 list_for_each_entry(dev, &bus->devices, bus_list) {
707 if (PCI_SLOT(dev->devfn) != slot->device)
708 continue;
709 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
710 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
711 max = pci_scan_bridge(bus, dev, max, pass);
712 if (pass && dev->subordinate) {
713 check_hotplug_bridge(slot, dev);
714 pcibios_resource_survey_bus(dev->subordinate);
715 __pci_bus_size_bridges(dev->subordinate,
716 &add_list);
717 }
718 }
719 }
720 }
721
722 __pci_bus_assign_resources(bus, &add_list, NULL);
723 acpiphp_sanitize_bus(bus);
724 acpiphp_set_hpp_values(bus);
725 acpiphp_set_acpi_region(slot);
726 pci_enable_bridges(bus);
727
728 list_for_each_entry(dev, &bus->devices, bus_list) {
729
730 if (!dev->is_added)
731 dev->current_state = PCI_D0;
732 }
733
734 pci_bus_add_devices(bus);
735
736 slot->flags |= SLOT_ENABLED;
737 list_for_each_entry(func, &slot->funcs, sibling) {
738 dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
739 func->function));
740 if (!dev) {
741
742
743 slot->flags &= (~SLOT_ENABLED);
744 continue;
745 }
746 }
747
748
749 err_exit:
750 return 0;
751}
752
753
754static struct pci_dev *dev_in_slot(struct acpiphp_slot *slot)
755{
756 struct pci_bus *bus = slot->bridge->pci_bus;
757 struct pci_dev *dev;
758 struct pci_dev *ret = NULL;
759
760 down_read(&pci_bus_sem);
761 list_for_each_entry(dev, &bus->devices, bus_list)
762 if (PCI_SLOT(dev->devfn) == slot->device) {
763 ret = pci_dev_get(dev);
764 break;
765 }
766 up_read(&pci_bus_sem);
767
768 return ret;
769}
770
771
772
773
774
775static int disable_device(struct acpiphp_slot *slot)
776{
777 struct acpiphp_func *func;
778 struct pci_dev *pdev;
779
780
781
782
783
784
785
786 while ((pdev = dev_in_slot(slot))) {
787 pci_stop_and_remove_bus_device(pdev);
788 pci_dev_put(pdev);
789 }
790
791 list_for_each_entry(func, &slot->funcs, sibling) {
792 acpiphp_bus_trim(func->handle);
793 }
794
795 slot->flags &= (~SLOT_ENABLED);
796
797 return 0;
798}
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813static unsigned int get_slot_status(struct acpiphp_slot *slot)
814{
815 acpi_status status;
816 unsigned long long sta = 0;
817 u32 dvid;
818 struct acpiphp_func *func;
819
820 list_for_each_entry(func, &slot->funcs, sibling) {
821 if (func->flags & FUNC_HAS_STA) {
822 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
823 if (ACPI_SUCCESS(status) && sta)
824 break;
825 } else {
826 pci_bus_read_config_dword(slot->bridge->pci_bus,
827 PCI_DEVFN(slot->device,
828 func->function),
829 PCI_VENDOR_ID, &dvid);
830 if (dvid != 0xffffffff) {
831 sta = ACPI_STA_ALL;
832 break;
833 }
834 }
835 }
836
837 return (unsigned int)sta;
838}
839
840
841
842
843
844int acpiphp_eject_slot(struct acpiphp_slot *slot)
845{
846 acpi_status status;
847 struct acpiphp_func *func;
848 struct acpi_object_list arg_list;
849 union acpi_object arg;
850
851 list_for_each_entry(func, &slot->funcs, sibling) {
852
853 if ((func->flags & FUNC_HAS_EJ0)) {
854
855 arg_list.count = 1;
856 arg_list.pointer = &arg;
857 arg.type = ACPI_TYPE_INTEGER;
858 arg.integer.value = 1;
859
860 status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
861 if (ACPI_FAILURE(status)) {
862 warn("%s: _EJ0 failed\n", __func__);
863 return -1;
864 } else
865 break;
866 }
867 }
868 return 0;
869}
870
871
872
873
874
875
876
877
878static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
879{
880 struct acpiphp_slot *slot;
881 int retval = 0;
882 int enabled, disabled;
883
884 enabled = disabled = 0;
885
886 list_for_each_entry(slot, &bridge->slots, node) {
887 unsigned int status = get_slot_status(slot);
888 if (slot->flags & SLOT_ENABLED) {
889 if (status == ACPI_STA_ALL)
890 continue;
891 retval = acpiphp_disable_slot(slot);
892 if (retval) {
893 err("Error occurred in disabling\n");
894 goto err_exit;
895 } else {
896 acpiphp_eject_slot(slot);
897 }
898 disabled++;
899 } else {
900 if (status != ACPI_STA_ALL)
901 continue;
902 retval = acpiphp_enable_slot(slot);
903 if (retval) {
904 err("Error occurred in enabling\n");
905 goto err_exit;
906 }
907 enabled++;
908 }
909 }
910
911 dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
912
913 err_exit:
914 return retval;
915}
916
917static void acpiphp_set_hpp_values(struct pci_bus *bus)
918{
919 struct pci_dev *dev;
920
921 list_for_each_entry(dev, &bus->devices, bus_list)
922 pci_configure_slot(dev);
923}
924
925
926
927
928
929static void acpiphp_sanitize_bus(struct pci_bus *bus)
930{
931 struct pci_dev *dev, *tmp;
932 int i;
933 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
934
935 list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list) {
936 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
937 struct resource *res = &dev->resource[i];
938 if ((res->flags & type_mask) && !res->start &&
939 res->end) {
940
941
942 pci_stop_and_remove_bus_device(dev);
943 break;
944 }
945 }
946 }
947}
948
949
950
951
952
953static acpi_status
954check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
955{
956 struct acpiphp_bridge *bridge;
957 char objname[64];
958 struct acpi_buffer buffer = { .length = sizeof(objname),
959 .pointer = objname };
960
961 bridge = acpiphp_handle_to_bridge(handle);
962 if (bridge) {
963 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
964 dbg("%s: re-enumerating slots under %s\n",
965 __func__, objname);
966 acpiphp_check_bridge(bridge);
967 put_bridge(bridge);
968 }
969 return AE_OK ;
970}
971
972void acpiphp_check_host_bridge(acpi_handle handle)
973{
974 struct acpiphp_bridge *bridge;
975
976 bridge = acpiphp_handle_to_bridge(handle);
977 if (bridge) {
978 acpiphp_check_bridge(bridge);
979 put_bridge(bridge);
980 }
981
982 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
983 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
984}
985
986static void _handle_hotplug_event_bridge(struct work_struct *work)
987{
988 struct acpiphp_bridge *bridge;
989 char objname[64];
990 struct acpi_buffer buffer = { .length = sizeof(objname),
991 .pointer = objname };
992 struct acpi_hp_work *hp_work;
993 acpi_handle handle;
994 u32 type;
995
996 hp_work = container_of(work, struct acpi_hp_work, work);
997 handle = hp_work->handle;
998 type = hp_work->type;
999 bridge = (struct acpiphp_bridge *)hp_work->context;
1000
1001 acpi_scan_lock_acquire();
1002
1003 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1004
1005 switch (type) {
1006 case ACPI_NOTIFY_BUS_CHECK:
1007
1008 dbg("%s: Bus check notify on %s\n", __func__, objname);
1009 dbg("%s: re-enumerating slots under %s\n", __func__, objname);
1010 acpiphp_check_bridge(bridge);
1011 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1012 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
1013 break;
1014
1015 case ACPI_NOTIFY_DEVICE_CHECK:
1016
1017 dbg("%s: Device check notify on %s\n", __func__, objname);
1018 acpiphp_check_bridge(bridge);
1019 break;
1020
1021 case ACPI_NOTIFY_DEVICE_WAKE:
1022
1023 dbg("%s: Device wake notify on %s\n", __func__, objname);
1024 break;
1025
1026 case ACPI_NOTIFY_EJECT_REQUEST:
1027
1028 dbg("%s: Device eject notify on %s\n", __func__, objname);
1029 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
1030 struct acpiphp_slot *slot;
1031 slot = bridge->func->slot;
1032 if (!acpiphp_disable_slot(slot))
1033 acpiphp_eject_slot(slot);
1034 }
1035 break;
1036
1037 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1038 printk(KERN_ERR "Device %s cannot be configured due"
1039 " to a frequency mismatch\n", objname);
1040 break;
1041
1042 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1043 printk(KERN_ERR "Device %s cannot be configured due"
1044 " to a bus mode mismatch\n", objname);
1045 break;
1046
1047 case ACPI_NOTIFY_POWER_FAULT:
1048 printk(KERN_ERR "Device %s has suffered a power fault\n",
1049 objname);
1050 break;
1051
1052 default:
1053 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1054 break;
1055 }
1056
1057 acpi_scan_lock_release();
1058 kfree(hp_work);
1059 put_bridge(bridge);
1060}
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070static void handle_hotplug_event_bridge(acpi_handle handle, u32 type,
1071 void *context)
1072{
1073 struct acpiphp_bridge *bridge = context;
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083 get_bridge(bridge);
1084 alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_bridge);
1085}
1086
1087static void hotplug_event_func(acpi_handle handle, u32 type, void *context)
1088{
1089 struct acpiphp_func *func = context;
1090 char objname[64];
1091 struct acpi_buffer buffer = { .length = sizeof(objname),
1092 .pointer = objname };
1093
1094 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1095
1096 switch (type) {
1097 case ACPI_NOTIFY_BUS_CHECK:
1098
1099 dbg("%s: Bus check notify on %s\n", __func__, objname);
1100 acpiphp_enable_slot(func->slot);
1101 break;
1102
1103 case ACPI_NOTIFY_DEVICE_CHECK:
1104
1105 dbg("%s: Device check notify on %s\n", __func__, objname);
1106 acpiphp_check_bridge(func->slot->bridge);
1107 break;
1108
1109 case ACPI_NOTIFY_DEVICE_WAKE:
1110
1111 dbg("%s: Device wake notify on %s\n", __func__, objname);
1112 break;
1113
1114 case ACPI_NOTIFY_EJECT_REQUEST:
1115
1116 dbg("%s: Device eject notify on %s\n", __func__, objname);
1117 if (!(acpiphp_disable_slot(func->slot)))
1118 acpiphp_eject_slot(func->slot);
1119 break;
1120
1121 default:
1122 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1123 break;
1124 }
1125}
1126
1127static void _handle_hotplug_event_func(struct work_struct *work)
1128{
1129 struct acpi_hp_work *hp_work;
1130 struct acpiphp_func *func;
1131
1132 hp_work = container_of(work, struct acpi_hp_work, work);
1133 func = hp_work->context;
1134 acpi_scan_lock_acquire();
1135
1136 hotplug_event_func(hp_work->handle, hp_work->type, func);
1137
1138 acpi_scan_lock_release();
1139 kfree(hp_work);
1140 put_bridge(func->slot->bridge);
1141}
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151static void handle_hotplug_event_func(acpi_handle handle, u32 type,
1152 void *context)
1153{
1154 struct acpiphp_func *func = context;
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164 get_bridge(func->slot->bridge);
1165 alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_func);
1166}
1167
1168
1169
1170
1171
1172void acpiphp_enumerate_slots(struct pci_bus *bus, acpi_handle handle)
1173{
1174 acpi_handle dummy_handle;
1175 struct acpiphp_bridge *bridge;
1176
1177 if (acpiphp_disabled)
1178 return;
1179
1180 if (detect_ejectable_slots(handle) <= 0)
1181 return;
1182
1183 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
1184 if (bridge == NULL) {
1185 err("out of memory\n");
1186 return;
1187 }
1188
1189 INIT_LIST_HEAD(&bridge->slots);
1190 kref_init(&bridge->ref);
1191 bridge->handle = handle;
1192 bridge->pci_dev = pci_dev_get(bus->self);
1193 bridge->pci_bus = bus;
1194
1195
1196
1197
1198
1199
1200 get_device(&bus->dev);
1201
1202 if (!pci_is_root_bus(bridge->pci_bus) &&
1203 ACPI_SUCCESS(acpi_get_handle(bridge->handle,
1204 "_EJ0", &dummy_handle))) {
1205 dbg("found ejectable p2p bridge\n");
1206 bridge->flags |= BRIDGE_HAS_EJ0;
1207 bridge->func = acpiphp_bridge_handle_to_function(handle);
1208 }
1209
1210 init_bridge_misc(bridge);
1211}
1212
1213
1214void acpiphp_remove_slots(struct pci_bus *bus)
1215{
1216 struct acpiphp_bridge *bridge, *tmp;
1217
1218 if (acpiphp_disabled)
1219 return;
1220
1221 list_for_each_entry_safe(bridge, tmp, &bridge_list, list)
1222 if (bridge->pci_bus == bus) {
1223 cleanup_bridge(bridge);
1224 put_bridge(bridge);
1225 break;
1226 }
1227}
1228
1229
1230
1231
1232
1233int acpiphp_enable_slot(struct acpiphp_slot *slot)
1234{
1235 int retval;
1236
1237 mutex_lock(&slot->crit_sect);
1238
1239
1240 retval = power_on_slot(slot);
1241 if (retval)
1242 goto err_exit;
1243
1244 if (get_slot_status(slot) == ACPI_STA_ALL) {
1245
1246 retval = enable_device(slot);
1247 if (retval)
1248 power_off_slot(slot);
1249 } else {
1250 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
1251 power_off_slot(slot);
1252 }
1253
1254 err_exit:
1255 mutex_unlock(&slot->crit_sect);
1256 return retval;
1257}
1258
1259
1260
1261
1262
1263int acpiphp_disable_slot(struct acpiphp_slot *slot)
1264{
1265 int retval = 0;
1266
1267 mutex_lock(&slot->crit_sect);
1268
1269
1270 retval = disable_device(slot);
1271 if (retval)
1272 goto err_exit;
1273
1274
1275 retval = power_off_slot(slot);
1276 if (retval)
1277 goto err_exit;
1278
1279 err_exit:
1280 mutex_unlock(&slot->crit_sect);
1281 return retval;
1282}
1283
1284
1285
1286
1287
1288
1289u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1290{
1291 return (slot->flags & SLOT_POWEREDON);
1292}
1293
1294
1295
1296
1297
1298
1299u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1300{
1301 unsigned int sta;
1302
1303 sta = get_slot_status(slot);
1304
1305 return (sta & ACPI_STA_DEVICE_UI) ? 0 : 1;
1306}
1307
1308
1309
1310
1311
1312
1313u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1314{
1315 unsigned int sta;
1316
1317 sta = get_slot_status(slot);
1318
1319 return (sta == 0) ? 0 : 1;
1320}
1321