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);
57
58#define MY_NAME "acpiphp_glue"
59
60static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
61static void acpiphp_sanitize_bus(struct pci_bus *bus);
62static void acpiphp_set_hpp_values(struct pci_bus *bus);
63static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context);
64
65
66static acpi_status
67is_pci_dock_device(acpi_handle handle, u32 lvl, void *context, void **rv)
68{
69 int *count = (int *)context;
70
71 if (is_dock_device(handle)) {
72 (*count)++;
73 return AE_CTRL_TERMINATE;
74 } else {
75 return AE_OK;
76 }
77}
78
79
80
81
82
83
84
85
86static int post_dock_fixups(struct notifier_block *nb, unsigned long val,
87 void *v)
88{
89 struct acpiphp_func *func = container_of(nb, struct acpiphp_func, nb);
90 struct pci_bus *bus = func->slot->bridge->pci_bus;
91 u32 buses;
92
93 if (!bus->self)
94 return NOTIFY_OK;
95
96
97
98
99 pci_read_config_dword(bus->self,
100 PCI_PRIMARY_BUS,
101 &buses);
102
103 if (((buses >> 8) & 0xff) != bus->busn_res.start) {
104 buses = (buses & 0xff000000)
105 | ((unsigned int)(bus->primary) << 0)
106 | ((unsigned int)(bus->busn_res.start) << 8)
107 | ((unsigned int)(bus->busn_res.end) << 16);
108 pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
109 }
110 return NOTIFY_OK;
111}
112
113
114static const struct acpi_dock_ops acpiphp_dock_ops = {
115 .handler = handle_hotplug_event_func,
116};
117
118
119static acpi_status
120register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
121{
122 struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
123 struct acpiphp_slot *slot;
124 struct acpiphp_func *newfunc;
125 acpi_handle tmp;
126 acpi_status status = AE_OK;
127 unsigned long long adr, sun;
128 int device, function, retval;
129 struct pci_bus *pbus = bridge->pci_bus;
130 struct pci_dev *pdev;
131
132 if (!acpi_pci_check_ejectable(pbus, handle) && !is_dock_device(handle))
133 return AE_OK;
134
135 status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
136 if (ACPI_FAILURE(status)) {
137 warn("can't evaluate _ADR (%#x)\n", status);
138 return AE_OK;
139 }
140
141 device = (adr >> 16) & 0xffff;
142 function = adr & 0xffff;
143
144 pdev = pbus->self;
145 if (pdev && pci_is_pcie(pdev)) {
146 tmp = acpi_find_root_bridge_handle(pdev);
147 if (tmp) {
148 struct acpi_pci_root *root = acpi_pci_find_root(tmp);
149
150 if (root && (root->osc_control_set &
151 OSC_PCI_EXPRESS_NATIVE_HP_CONTROL))
152 return AE_OK;
153 }
154 }
155
156 newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
157 if (!newfunc)
158 return AE_NO_MEMORY;
159
160 INIT_LIST_HEAD(&newfunc->sibling);
161 newfunc->handle = handle;
162 newfunc->function = function;
163
164 if (ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp)))
165 newfunc->flags = FUNC_HAS_EJ0;
166
167 if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
168 newfunc->flags |= FUNC_HAS_STA;
169
170 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
171 newfunc->flags |= FUNC_HAS_PS0;
172
173 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
174 newfunc->flags |= FUNC_HAS_PS3;
175
176 if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp)))
177 newfunc->flags |= FUNC_HAS_DCK;
178
179 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
180 if (ACPI_FAILURE(status)) {
181
182
183
184
185 sun = bridge->nr_slots+1;
186 }
187
188
189 for (slot = bridge->slots; slot; slot = slot->next)
190 if (slot->device == device) {
191 if (slot->sun != sun)
192 warn("sibling found, but _SUN doesn't match!\n");
193 break;
194 }
195
196 if (!slot) {
197 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
198 if (!slot) {
199 kfree(newfunc);
200 return AE_NO_MEMORY;
201 }
202
203 slot->bridge = bridge;
204 slot->device = device;
205 slot->sun = sun;
206 INIT_LIST_HEAD(&slot->funcs);
207 mutex_init(&slot->crit_sect);
208
209 slot->next = bridge->slots;
210 bridge->slots = slot;
211
212 bridge->nr_slots++;
213
214 dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
215 slot->sun, pci_domain_nr(pbus), pbus->number, device);
216 retval = acpiphp_register_hotplug_slot(slot);
217 if (retval) {
218 if (retval == -EBUSY)
219 warn("Slot %llu already registered by another "
220 "hotplug driver\n", slot->sun);
221 else
222 warn("acpiphp_register_hotplug_slot failed "
223 "(err code = 0x%x)\n", retval);
224 goto err_exit;
225 }
226 }
227
228 newfunc->slot = slot;
229 list_add_tail(&newfunc->sibling, &slot->funcs);
230
231 pdev = pci_get_slot(pbus, PCI_DEVFN(device, function));
232 if (pdev) {
233 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
234 pci_dev_put(pdev);
235 }
236
237 if (is_dock_device(handle)) {
238
239
240
241
242 newfunc->flags &= ~FUNC_HAS_EJ0;
243 if (register_hotplug_dock_device(handle,
244 &acpiphp_dock_ops, newfunc))
245 dbg("failed to register dock device\n");
246
247
248
249
250
251 newfunc->nb.notifier_call = post_dock_fixups;
252 if (register_dock_notifier(&newfunc->nb))
253 dbg("failed to register a dock notifier");
254 }
255
256
257 if (!(newfunc->flags & FUNC_HAS_DCK)) {
258 status = acpi_install_notify_handler(handle,
259 ACPI_SYSTEM_NOTIFY,
260 handle_hotplug_event_func,
261 newfunc);
262
263 if (ACPI_FAILURE(status))
264 err("failed to register interrupt notify handler\n");
265 } else
266 status = AE_OK;
267
268 return status;
269
270 err_exit:
271 bridge->nr_slots--;
272 bridge->slots = slot->next;
273 kfree(slot);
274 kfree(newfunc);
275
276 return AE_OK;
277}
278
279
280
281static int detect_ejectable_slots(acpi_handle handle)
282{
283 int found = acpi_pci_detect_ejectable(handle);
284 if (!found) {
285 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
286 is_pci_dock_device, NULL, (void *)&found, NULL);
287 }
288 return found;
289}
290
291
292static void init_bridge_misc(struct acpiphp_bridge *bridge)
293{
294 acpi_status status;
295
296
297 list_add(&bridge->list, &bridge_list);
298
299
300 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
301 register_slot, NULL, bridge, NULL);
302 if (ACPI_FAILURE(status)) {
303 list_del(&bridge->list);
304 return;
305 }
306
307
308 if (bridge->type != BRIDGE_TYPE_HOST) {
309 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
310 status = acpi_remove_notify_handler(bridge->func->handle,
311 ACPI_SYSTEM_NOTIFY,
312 handle_hotplug_event_func);
313 if (ACPI_FAILURE(status))
314 err("failed to remove notify handler\n");
315 }
316 status = acpi_install_notify_handler(bridge->handle,
317 ACPI_SYSTEM_NOTIFY,
318 handle_hotplug_event_bridge,
319 bridge);
320
321 if (ACPI_FAILURE(status)) {
322 err("failed to register interrupt notify handler\n");
323 }
324 }
325}
326
327
328
329static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
330{
331 struct acpiphp_bridge *bridge;
332 struct acpiphp_slot *slot;
333 struct acpiphp_func *func;
334
335 list_for_each_entry(bridge, &bridge_list, list) {
336 for (slot = bridge->slots; slot; slot = slot->next) {
337 list_for_each_entry(func, &slot->funcs, sibling) {
338 if (func->handle == handle)
339 return func;
340 }
341 }
342 }
343
344 return NULL;
345}
346
347
348static inline void config_p2p_bridge_flags(struct acpiphp_bridge *bridge)
349{
350 acpi_handle dummy_handle;
351
352 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
353 "_STA", &dummy_handle)))
354 bridge->flags |= BRIDGE_HAS_STA;
355
356 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
357 "_EJ0", &dummy_handle)))
358 bridge->flags |= BRIDGE_HAS_EJ0;
359
360 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
361 "_PS0", &dummy_handle)))
362 bridge->flags |= BRIDGE_HAS_PS0;
363
364 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
365 "_PS3", &dummy_handle)))
366 bridge->flags |= BRIDGE_HAS_PS3;
367
368
369 if (bridge->flags & BRIDGE_HAS_EJ0) {
370 struct acpiphp_func *func;
371
372 dbg("found ejectable p2p bridge\n");
373
374
375 func = acpiphp_bridge_handle_to_function(bridge->handle);
376 if (!func)
377 return;
378 bridge->func = func;
379 func->bridge = bridge;
380 }
381}
382
383
384
385static void add_host_bridge(acpi_handle *handle)
386{
387 struct acpiphp_bridge *bridge;
388 struct acpi_pci_root *root = acpi_pci_find_root(handle);
389
390 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
391 if (bridge == NULL)
392 return;
393
394 bridge->type = BRIDGE_TYPE_HOST;
395 bridge->handle = handle;
396
397 bridge->pci_bus = root->bus;
398
399 init_bridge_misc(bridge);
400}
401
402
403
404static void add_p2p_bridge(acpi_handle *handle)
405{
406 struct acpiphp_bridge *bridge;
407
408 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
409 if (bridge == NULL) {
410 err("out of memory\n");
411 return;
412 }
413
414 bridge->type = BRIDGE_TYPE_P2P;
415 bridge->handle = handle;
416 config_p2p_bridge_flags(bridge);
417
418 bridge->pci_dev = acpi_get_pci_dev(handle);
419 bridge->pci_bus = bridge->pci_dev->subordinate;
420 if (!bridge->pci_bus) {
421 err("This is not a PCI-to-PCI bridge!\n");
422 goto err;
423 }
424
425
426
427
428
429
430 get_device(&bridge->pci_bus->dev);
431
432 init_bridge_misc(bridge);
433 return;
434 err:
435 pci_dev_put(bridge->pci_dev);
436 kfree(bridge);
437 return;
438}
439
440
441
442static acpi_status
443find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
444{
445 acpi_status status;
446 struct pci_dev *dev;
447
448 dev = acpi_get_pci_dev(handle);
449 if (!dev || !dev->subordinate)
450 goto out;
451
452
453 if ((detect_ejectable_slots(handle) > 0)) {
454 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
455 add_p2p_bridge(handle);
456 }
457
458
459 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
460 find_p2p_bridge, NULL, NULL, NULL);
461 if (ACPI_FAILURE(status))
462 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
463
464 out:
465 pci_dev_put(dev);
466 return AE_OK;
467}
468
469
470
471static int add_bridge(acpi_handle handle)
472{
473 acpi_status status;
474 unsigned long long tmp;
475 acpi_handle dummy_handle;
476
477
478 status = acpi_get_handle(handle, "_STA", &dummy_handle);
479 if (ACPI_SUCCESS(status)) {
480 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
481 if (ACPI_FAILURE(status)) {
482 dbg("%s: _STA evaluation failure\n", __func__);
483 return 0;
484 }
485 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
486
487 return 0;
488 }
489
490
491 if (detect_ejectable_slots(handle) > 0) {
492 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
493 add_host_bridge(handle);
494 }
495
496
497 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
498 find_p2p_bridge, NULL, NULL, NULL);
499
500 if (ACPI_FAILURE(status))
501 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
502
503 return 0;
504}
505
506static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
507{
508 struct acpiphp_bridge *bridge;
509
510 list_for_each_entry(bridge, &bridge_list, list)
511 if (bridge->handle == handle)
512 return bridge;
513
514 return NULL;
515}
516
517static void cleanup_bridge(struct acpiphp_bridge *bridge)
518{
519 struct acpiphp_slot *slot, *next;
520 struct acpiphp_func *func, *tmp;
521 acpi_status status;
522 acpi_handle handle = bridge->handle;
523
524 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
525 handle_hotplug_event_bridge);
526 if (ACPI_FAILURE(status))
527 err("failed to remove notify handler\n");
528
529 if ((bridge->type != BRIDGE_TYPE_HOST) &&
530 ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)) {
531 status = acpi_install_notify_handler(bridge->func->handle,
532 ACPI_SYSTEM_NOTIFY,
533 handle_hotplug_event_func,
534 bridge->func);
535 if (ACPI_FAILURE(status))
536 err("failed to install interrupt notify handler\n");
537 }
538
539 slot = bridge->slots;
540 while (slot) {
541 next = slot->next;
542 list_for_each_entry_safe(func, tmp, &slot->funcs, sibling) {
543 if (is_dock_device(func->handle)) {
544 unregister_hotplug_dock_device(func->handle);
545 unregister_dock_notifier(&func->nb);
546 }
547 if (!(func->flags & FUNC_HAS_DCK)) {
548 status = acpi_remove_notify_handler(func->handle,
549 ACPI_SYSTEM_NOTIFY,
550 handle_hotplug_event_func);
551 if (ACPI_FAILURE(status))
552 err("failed to remove notify handler\n");
553 }
554 list_del(&func->sibling);
555 kfree(func);
556 }
557 acpiphp_unregister_hotplug_slot(slot);
558 list_del(&slot->funcs);
559 kfree(slot);
560 slot = next;
561 }
562
563
564
565
566 if (bridge->pci_dev)
567 put_device(&bridge->pci_bus->dev);
568
569 pci_dev_put(bridge->pci_dev);
570 list_del(&bridge->list);
571 kfree(bridge);
572}
573
574static acpi_status
575cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
576{
577 struct acpiphp_bridge *bridge;
578
579
580
581 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
582 cleanup_p2p_bridge, NULL, NULL, NULL);
583
584 bridge = acpiphp_handle_to_bridge(handle);
585 if (bridge)
586 cleanup_bridge(bridge);
587
588 return AE_OK;
589}
590
591static void remove_bridge(acpi_handle handle)
592{
593 struct acpiphp_bridge *bridge;
594
595
596
597 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
598 (u32)1, cleanup_p2p_bridge, NULL, NULL, NULL);
599
600
601
602
603
604
605
606
607 bridge = acpiphp_handle_to_bridge(handle);
608 if (bridge)
609 cleanup_bridge(bridge);
610 else
611 acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
612 handle_hotplug_event_bridge);
613}
614
615static int power_on_slot(struct acpiphp_slot *slot)
616{
617 acpi_status status;
618 struct acpiphp_func *func;
619 int retval = 0;
620
621
622 if (slot->flags & SLOT_POWEREDON)
623 goto err_exit;
624
625 list_for_each_entry(func, &slot->funcs, sibling) {
626 if (func->flags & FUNC_HAS_PS0) {
627 dbg("%s: executing _PS0\n", __func__);
628 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
629 if (ACPI_FAILURE(status)) {
630 warn("%s: _PS0 failed\n", __func__);
631 retval = -1;
632 goto err_exit;
633 } else
634 break;
635 }
636 }
637
638
639
640 slot->flags |= SLOT_POWEREDON;
641
642 err_exit:
643 return retval;
644}
645
646
647static int power_off_slot(struct acpiphp_slot *slot)
648{
649 acpi_status status;
650 struct acpiphp_func *func;
651
652 int retval = 0;
653
654
655 if ((slot->flags & SLOT_POWEREDON) == 0)
656 goto err_exit;
657
658 list_for_each_entry(func, &slot->funcs, sibling) {
659 if (func->flags & FUNC_HAS_PS3) {
660 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
661 if (ACPI_FAILURE(status)) {
662 warn("%s: _PS3 failed\n", __func__);
663 retval = -1;
664 goto err_exit;
665 } else
666 break;
667 }
668 }
669
670
671
672 slot->flags &= (~SLOT_POWEREDON);
673
674 err_exit:
675 return retval;
676}
677
678
679
680
681
682
683
684static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
685{
686 struct list_head *tmp;
687 unsigned char max, n;
688
689
690
691
692
693
694
695
696
697 max = bus->busn_res.start;
698
699 list_for_each(tmp, &bus->children) {
700 n = pci_bus_max_busnr(pci_bus_b(tmp));
701 if (n > max)
702 max = n;
703 }
704 return max;
705}
706
707
708
709
710
711
712static int acpiphp_bus_add(struct acpiphp_func *func)
713{
714 acpi_handle phandle;
715 struct acpi_device *device, *pdevice;
716 int ret_val;
717
718 acpi_get_parent(func->handle, &phandle);
719 if (acpi_bus_get_device(phandle, &pdevice)) {
720 dbg("no parent device, assuming NULL\n");
721 pdevice = NULL;
722 }
723 if (!acpi_bus_get_device(func->handle, &device)) {
724 dbg("bus exists... trim\n");
725
726
727
728 ret_val = acpi_bus_trim(device, 1);
729 dbg("acpi_bus_trim return %x\n", ret_val);
730 }
731
732 ret_val = acpi_bus_add(&device, pdevice, func->handle,
733 ACPI_BUS_TYPE_DEVICE);
734 if (ret_val) {
735 dbg("error adding bus, %x\n",
736 -ret_val);
737 goto acpiphp_bus_add_out;
738 }
739 ret_val = acpi_bus_start(device);
740
741acpiphp_bus_add_out:
742 return ret_val;
743}
744
745
746
747
748
749
750static int acpiphp_bus_trim(acpi_handle handle)
751{
752 struct acpi_device *device;
753 int retval;
754
755 retval = acpi_bus_get_device(handle, &device);
756 if (retval) {
757 dbg("acpi_device not found\n");
758 return retval;
759 }
760
761 retval = acpi_bus_trim(device, 1);
762 if (retval)
763 err("cannot remove from acpi list\n");
764
765 return retval;
766}
767
768static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
769{
770 struct acpiphp_func *func;
771 union acpi_object params[2];
772 struct acpi_object_list arg_list;
773
774 list_for_each_entry(func, &slot->funcs, sibling) {
775 arg_list.count = 2;
776 arg_list.pointer = params;
777 params[0].type = ACPI_TYPE_INTEGER;
778 params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
779 params[1].type = ACPI_TYPE_INTEGER;
780 params[1].integer.value = 1;
781
782 acpi_evaluate_object(func->handle, "_REG", &arg_list, NULL);
783 }
784}
785
786
787
788
789
790
791
792
793static int __ref enable_device(struct acpiphp_slot *slot)
794{
795 struct pci_dev *dev;
796 struct pci_bus *bus = slot->bridge->pci_bus;
797 struct acpiphp_func *func;
798 int retval = 0;
799 int num, max, pass;
800 acpi_status status;
801
802 if (slot->flags & SLOT_ENABLED)
803 goto err_exit;
804
805 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
806 if (num == 0) {
807
808 dbg("No new device found\n");
809 goto err_exit;
810 }
811
812 max = acpiphp_max_busnr(bus);
813 for (pass = 0; pass < 2; pass++) {
814 list_for_each_entry(dev, &bus->devices, bus_list) {
815 if (PCI_SLOT(dev->devfn) != slot->device)
816 continue;
817 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
818 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
819 max = pci_scan_bridge(bus, dev, max, pass);
820 if (pass && dev->subordinate)
821 pci_bus_size_bridges(dev->subordinate);
822 }
823 }
824 }
825
826 list_for_each_entry(func, &slot->funcs, sibling)
827 acpiphp_bus_add(func);
828
829 pci_bus_assign_resources(bus);
830 acpiphp_sanitize_bus(bus);
831 acpiphp_set_hpp_values(bus);
832 acpiphp_set_acpi_region(slot);
833 pci_enable_bridges(bus);
834
835 list_for_each_entry(dev, &bus->devices, bus_list) {
836
837 if (!dev->is_added)
838 dev->current_state = PCI_D0;
839 }
840
841 pci_bus_add_devices(bus);
842
843 slot->flags |= SLOT_ENABLED;
844 list_for_each_entry(func, &slot->funcs, sibling) {
845 dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
846 func->function));
847 if (!dev) {
848
849
850 slot->flags &= (~SLOT_ENABLED);
851 continue;
852 }
853
854 if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
855 dev->hdr_type != PCI_HEADER_TYPE_CARDBUS) {
856 pci_dev_put(dev);
857 continue;
858 }
859
860 status = find_p2p_bridge(func->handle, (u32)1, bus, NULL);
861 if (ACPI_FAILURE(status))
862 warn("find_p2p_bridge failed (error code = 0x%x)\n",
863 status);
864 pci_dev_put(dev);
865 }
866
867
868 err_exit:
869 return retval;
870}
871
872static void disable_bridges(struct pci_bus *bus)
873{
874 struct pci_dev *dev;
875 list_for_each_entry(dev, &bus->devices, bus_list) {
876 if (dev->subordinate) {
877 disable_bridges(dev->subordinate);
878 pci_disable_device(dev);
879 }
880 }
881}
882
883
884static struct pci_dev *dev_in_slot(struct acpiphp_slot *slot)
885{
886 struct pci_bus *bus = slot->bridge->pci_bus;
887 struct pci_dev *dev;
888 struct pci_dev *ret = NULL;
889
890 down_read(&pci_bus_sem);
891 list_for_each_entry(dev, &bus->devices, bus_list)
892 if (PCI_SLOT(dev->devfn) == slot->device) {
893 ret = pci_dev_get(dev);
894 break;
895 }
896 up_read(&pci_bus_sem);
897
898 return ret;
899}
900
901
902
903
904
905static int disable_device(struct acpiphp_slot *slot)
906{
907 struct acpiphp_func *func;
908 struct pci_dev *pdev;
909 struct pci_bus *bus = slot->bridge->pci_bus;
910
911
912
913 pdev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
914 if (!pdev)
915 goto err_exit;
916 pci_dev_put(pdev);
917
918 list_for_each_entry(func, &slot->funcs, sibling) {
919 if (func->bridge) {
920
921 cleanup_p2p_bridge(func->bridge->handle,
922 (u32)1, NULL, NULL);
923 func->bridge = NULL;
924 }
925 }
926
927
928
929
930
931
932
933 while ((pdev = dev_in_slot(slot))) {
934 pci_stop_bus_device(pdev);
935 if (pdev->subordinate) {
936 disable_bridges(pdev->subordinate);
937 pci_disable_device(pdev);
938 }
939 __pci_remove_bus_device(pdev);
940 pci_dev_put(pdev);
941 }
942
943 list_for_each_entry(func, &slot->funcs, sibling) {
944 acpiphp_bus_trim(func->handle);
945 }
946
947 slot->flags &= (~SLOT_ENABLED);
948
949err_exit:
950 return 0;
951}
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966static unsigned int get_slot_status(struct acpiphp_slot *slot)
967{
968 acpi_status status;
969 unsigned long long sta = 0;
970 u32 dvid;
971 struct acpiphp_func *func;
972
973 list_for_each_entry(func, &slot->funcs, sibling) {
974 if (func->flags & FUNC_HAS_STA) {
975 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
976 if (ACPI_SUCCESS(status) && sta)
977 break;
978 } else {
979 pci_bus_read_config_dword(slot->bridge->pci_bus,
980 PCI_DEVFN(slot->device,
981 func->function),
982 PCI_VENDOR_ID, &dvid);
983 if (dvid != 0xffffffff) {
984 sta = ACPI_STA_ALL;
985 break;
986 }
987 }
988 }
989
990 return (unsigned int)sta;
991}
992
993
994
995
996
997int acpiphp_eject_slot(struct acpiphp_slot *slot)
998{
999 acpi_status status;
1000 struct acpiphp_func *func;
1001 struct acpi_object_list arg_list;
1002 union acpi_object arg;
1003
1004 list_for_each_entry(func, &slot->funcs, sibling) {
1005
1006 if ((func->flags & FUNC_HAS_EJ0)) {
1007
1008 arg_list.count = 1;
1009 arg_list.pointer = &arg;
1010 arg.type = ACPI_TYPE_INTEGER;
1011 arg.integer.value = 1;
1012
1013 status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
1014 if (ACPI_FAILURE(status)) {
1015 warn("%s: _EJ0 failed\n", __func__);
1016 return -1;
1017 } else
1018 break;
1019 }
1020 }
1021 return 0;
1022}
1023
1024
1025
1026
1027
1028
1029
1030
1031static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
1032{
1033 struct acpiphp_slot *slot;
1034 int retval = 0;
1035 int enabled, disabled;
1036
1037 enabled = disabled = 0;
1038
1039 for (slot = bridge->slots; slot; slot = slot->next) {
1040 unsigned int status = get_slot_status(slot);
1041 if (slot->flags & SLOT_ENABLED) {
1042 if (status == ACPI_STA_ALL)
1043 continue;
1044 retval = acpiphp_disable_slot(slot);
1045 if (retval) {
1046 err("Error occurred in disabling\n");
1047 goto err_exit;
1048 } else {
1049 acpiphp_eject_slot(slot);
1050 }
1051 disabled++;
1052 } else {
1053 if (status != ACPI_STA_ALL)
1054 continue;
1055 retval = acpiphp_enable_slot(slot);
1056 if (retval) {
1057 err("Error occurred in enabling\n");
1058 goto err_exit;
1059 }
1060 enabled++;
1061 }
1062 }
1063
1064 dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
1065
1066 err_exit:
1067 return retval;
1068}
1069
1070static void acpiphp_set_hpp_values(struct pci_bus *bus)
1071{
1072 struct pci_dev *dev;
1073
1074 list_for_each_entry(dev, &bus->devices, bus_list)
1075 pci_configure_slot(dev);
1076}
1077
1078
1079
1080
1081
1082static void acpiphp_sanitize_bus(struct pci_bus *bus)
1083{
1084 struct pci_dev *dev;
1085 int i;
1086 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
1087
1088 list_for_each_entry(dev, &bus->devices, bus_list) {
1089 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
1090 struct resource *res = &dev->resource[i];
1091 if ((res->flags & type_mask) && !res->start &&
1092 res->end) {
1093
1094
1095 pci_stop_and_remove_bus_device(dev);
1096 break;
1097 }
1098 }
1099 }
1100}
1101
1102
1103static int acpiphp_configure_bridge (acpi_handle handle)
1104{
1105 struct pci_bus *bus;
1106
1107 if (acpi_is_root_bridge(handle)) {
1108 struct acpi_pci_root *root = acpi_pci_find_root(handle);
1109 bus = root->bus;
1110 } else {
1111 struct pci_dev *pdev = acpi_get_pci_dev(handle);
1112 bus = pdev->subordinate;
1113 pci_dev_put(pdev);
1114 }
1115
1116 pci_bus_size_bridges(bus);
1117 pci_bus_assign_resources(bus);
1118 acpiphp_sanitize_bus(bus);
1119 acpiphp_set_hpp_values(bus);
1120 pci_enable_bridges(bus);
1121 return 0;
1122}
1123
1124static void handle_bridge_insertion(acpi_handle handle, u32 type)
1125{
1126 struct acpi_device *device, *pdevice;
1127 acpi_handle phandle;
1128
1129 if ((type != ACPI_NOTIFY_BUS_CHECK) &&
1130 (type != ACPI_NOTIFY_DEVICE_CHECK)) {
1131 err("unexpected notification type %d\n", type);
1132 return;
1133 }
1134
1135 acpi_get_parent(handle, &phandle);
1136 if (acpi_bus_get_device(phandle, &pdevice)) {
1137 dbg("no parent device, assuming NULL\n");
1138 pdevice = NULL;
1139 }
1140 if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
1141 err("cannot add bridge to acpi list\n");
1142 return;
1143 }
1144 if (!acpiphp_configure_bridge(handle) &&
1145 !acpi_bus_start(device))
1146 add_bridge(handle);
1147 else
1148 err("cannot configure and start bridge\n");
1149
1150}
1151
1152
1153
1154
1155
1156static acpi_status
1157count_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1158{
1159 int *count = (int *)context;
1160 struct acpiphp_bridge *bridge;
1161
1162 bridge = acpiphp_handle_to_bridge(handle);
1163 if (bridge)
1164 (*count)++;
1165 return AE_OK ;
1166}
1167
1168static acpi_status
1169check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1170{
1171 struct acpiphp_bridge *bridge;
1172 char objname[64];
1173 struct acpi_buffer buffer = { .length = sizeof(objname),
1174 .pointer = objname };
1175
1176 bridge = acpiphp_handle_to_bridge(handle);
1177 if (bridge) {
1178 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1179 dbg("%s: re-enumerating slots under %s\n",
1180 __func__, objname);
1181 acpiphp_check_bridge(bridge);
1182 }
1183 return AE_OK ;
1184}
1185
1186struct acpiphp_hp_work {
1187 struct work_struct work;
1188 acpi_handle handle;
1189 u32 type;
1190 void *context;
1191};
1192
1193static void alloc_acpiphp_hp_work(acpi_handle handle, u32 type,
1194 void *context,
1195 void (*func)(struct work_struct *work))
1196{
1197 struct acpiphp_hp_work *hp_work;
1198 int ret;
1199
1200 hp_work = kmalloc(sizeof(*hp_work), GFP_KERNEL);
1201 if (!hp_work)
1202 return;
1203
1204 hp_work->handle = handle;
1205 hp_work->type = type;
1206 hp_work->context = context;
1207
1208 INIT_WORK(&hp_work->work, func);
1209 ret = queue_work(kacpi_hotplug_wq, &hp_work->work);
1210 if (!ret)
1211 kfree(hp_work);
1212}
1213
1214static void _handle_hotplug_event_bridge(struct work_struct *work)
1215{
1216 struct acpiphp_bridge *bridge;
1217 char objname[64];
1218 struct acpi_buffer buffer = { .length = sizeof(objname),
1219 .pointer = objname };
1220 struct acpi_device *device;
1221 int num_sub_bridges = 0;
1222 struct acpiphp_hp_work *hp_work;
1223 acpi_handle handle;
1224 u32 type;
1225
1226 hp_work = container_of(work, struct acpiphp_hp_work, work);
1227 handle = hp_work->handle;
1228 type = hp_work->type;
1229
1230 if (acpi_bus_get_device(handle, &device)) {
1231
1232 handle_bridge_insertion(handle, type);
1233 goto out;
1234 }
1235
1236 bridge = acpiphp_handle_to_bridge(handle);
1237 if (type == ACPI_NOTIFY_BUS_CHECK) {
1238 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, ACPI_UINT32_MAX,
1239 count_sub_bridges, NULL, &num_sub_bridges, NULL);
1240 }
1241
1242 if (!bridge && !num_sub_bridges) {
1243 err("cannot get bridge info\n");
1244 goto out;
1245 }
1246
1247 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1248
1249 switch (type) {
1250 case ACPI_NOTIFY_BUS_CHECK:
1251
1252 dbg("%s: Bus check notify on %s\n", __func__, objname);
1253 if (bridge) {
1254 dbg("%s: re-enumerating slots under %s\n",
1255 __func__, objname);
1256 acpiphp_check_bridge(bridge);
1257 }
1258 if (num_sub_bridges)
1259 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1260 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
1261 break;
1262
1263 case ACPI_NOTIFY_DEVICE_CHECK:
1264
1265 dbg("%s: Device check notify on %s\n", __func__, objname);
1266 acpiphp_check_bridge(bridge);
1267 break;
1268
1269 case ACPI_NOTIFY_DEVICE_WAKE:
1270
1271 dbg("%s: Device wake notify on %s\n", __func__, objname);
1272 break;
1273
1274 case ACPI_NOTIFY_EJECT_REQUEST:
1275
1276 dbg("%s: Device eject notify on %s\n", __func__, objname);
1277 if ((bridge->type != BRIDGE_TYPE_HOST) &&
1278 (bridge->flags & BRIDGE_HAS_EJ0)) {
1279 struct acpiphp_slot *slot;
1280 slot = bridge->func->slot;
1281 if (!acpiphp_disable_slot(slot))
1282 acpiphp_eject_slot(slot);
1283 }
1284 break;
1285
1286 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1287 printk(KERN_ERR "Device %s cannot be configured due"
1288 " to a frequency mismatch\n", objname);
1289 break;
1290
1291 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1292 printk(KERN_ERR "Device %s cannot be configured due"
1293 " to a bus mode mismatch\n", objname);
1294 break;
1295
1296 case ACPI_NOTIFY_POWER_FAULT:
1297 printk(KERN_ERR "Device %s has suffered a power fault\n",
1298 objname);
1299 break;
1300
1301 default:
1302 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1303 break;
1304 }
1305
1306out:
1307 kfree(hp_work);
1308}
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318static void handle_hotplug_event_bridge(acpi_handle handle, u32 type,
1319 void *context)
1320{
1321
1322
1323
1324
1325
1326
1327
1328
1329 alloc_acpiphp_hp_work(handle, type, context,
1330 _handle_hotplug_event_bridge);
1331}
1332
1333static void _handle_hotplug_event_func(struct work_struct *work)
1334{
1335 struct acpiphp_func *func;
1336 char objname[64];
1337 struct acpi_buffer buffer = { .length = sizeof(objname),
1338 .pointer = objname };
1339 struct acpiphp_hp_work *hp_work;
1340 acpi_handle handle;
1341 u32 type;
1342 void *context;
1343
1344 hp_work = container_of(work, struct acpiphp_hp_work, work);
1345 handle = hp_work->handle;
1346 type = hp_work->type;
1347 context = hp_work->context;
1348
1349 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1350
1351 func = (struct acpiphp_func *)context;
1352
1353 switch (type) {
1354 case ACPI_NOTIFY_BUS_CHECK:
1355
1356 dbg("%s: Bus check notify on %s\n", __func__, objname);
1357 acpiphp_enable_slot(func->slot);
1358 break;
1359
1360 case ACPI_NOTIFY_DEVICE_CHECK:
1361
1362 dbg("%s: Device check notify on %s\n", __func__, objname);
1363 acpiphp_check_bridge(func->slot->bridge);
1364 break;
1365
1366 case ACPI_NOTIFY_DEVICE_WAKE:
1367
1368 dbg("%s: Device wake notify on %s\n", __func__, objname);
1369 break;
1370
1371 case ACPI_NOTIFY_EJECT_REQUEST:
1372
1373 dbg("%s: Device eject notify on %s\n", __func__, objname);
1374 if (!(acpiphp_disable_slot(func->slot)))
1375 acpiphp_eject_slot(func->slot);
1376 break;
1377
1378 default:
1379 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1380 break;
1381 }
1382
1383 kfree(hp_work);
1384}
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394static void handle_hotplug_event_func(acpi_handle handle, u32 type,
1395 void *context)
1396{
1397
1398
1399
1400
1401
1402
1403
1404
1405 alloc_acpiphp_hp_work(handle, type, context,
1406 _handle_hotplug_event_func);
1407}
1408
1409static acpi_status
1410find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1411{
1412 int *count = (int *)context;
1413
1414 if (!acpi_is_root_bridge(handle))
1415 return AE_OK;
1416
1417 (*count)++;
1418 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1419 handle_hotplug_event_bridge, NULL);
1420
1421 return AE_OK ;
1422}
1423
1424static struct acpi_pci_driver acpi_pci_hp_driver = {
1425 .add = add_bridge,
1426 .remove = remove_bridge,
1427};
1428
1429
1430
1431
1432int __init acpiphp_glue_init(void)
1433{
1434 int num = 0;
1435
1436 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1437 ACPI_UINT32_MAX, find_root_bridges, NULL, &num, NULL);
1438
1439 if (num <= 0)
1440 return -1;
1441 else
1442 acpi_pci_register_driver(&acpi_pci_hp_driver);
1443
1444 return 0;
1445}
1446
1447
1448
1449
1450
1451
1452
1453void acpiphp_glue_exit(void)
1454{
1455 acpi_pci_unregister_driver(&acpi_pci_hp_driver);
1456}
1457
1458
1459
1460
1461
1462int __init acpiphp_get_num_slots(void)
1463{
1464 struct acpiphp_bridge *bridge;
1465 int num_slots = 0;
1466
1467 list_for_each_entry(bridge, &bridge_list, list) {
1468 dbg("Bus %04x:%02x has %d slot%s\n",
1469 pci_domain_nr(bridge->pci_bus),
1470 bridge->pci_bus->number, bridge->nr_slots,
1471 bridge->nr_slots == 1 ? "" : "s");
1472 num_slots += bridge->nr_slots;
1473 }
1474
1475 dbg("Total %d slots\n", num_slots);
1476 return num_slots;
1477}
1478
1479
1480#if 0
1481
1482
1483
1484
1485
1486static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
1487{
1488 struct list_head *node;
1489 struct acpiphp_bridge *bridge;
1490 struct acpiphp_slot *slot;
1491 int retval = 0;
1492
1493 list_for_each (node, &bridge_list) {
1494 bridge = (struct acpiphp_bridge *)node;
1495 for (slot = bridge->slots; slot; slot = slot->next) {
1496 retval = fn(slot, data);
1497 if (!retval)
1498 goto err_exit;
1499 }
1500 }
1501
1502 err_exit:
1503 return retval;
1504}
1505#endif
1506
1507
1508
1509
1510
1511
1512int acpiphp_enable_slot(struct acpiphp_slot *slot)
1513{
1514 int retval;
1515
1516 mutex_lock(&slot->crit_sect);
1517
1518
1519 retval = power_on_slot(slot);
1520 if (retval)
1521 goto err_exit;
1522
1523 if (get_slot_status(slot) == ACPI_STA_ALL) {
1524
1525 retval = enable_device(slot);
1526 if (retval)
1527 power_off_slot(slot);
1528 } else {
1529 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
1530 power_off_slot(slot);
1531 }
1532
1533 err_exit:
1534 mutex_unlock(&slot->crit_sect);
1535 return retval;
1536}
1537
1538
1539
1540
1541
1542int acpiphp_disable_slot(struct acpiphp_slot *slot)
1543{
1544 int retval = 0;
1545
1546 mutex_lock(&slot->crit_sect);
1547
1548
1549 retval = disable_device(slot);
1550 if (retval)
1551 goto err_exit;
1552
1553
1554 retval = power_off_slot(slot);
1555 if (retval)
1556 goto err_exit;
1557
1558 err_exit:
1559 mutex_unlock(&slot->crit_sect);
1560 return retval;
1561}
1562
1563
1564
1565
1566
1567
1568u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1569{
1570 return (slot->flags & SLOT_POWEREDON);
1571}
1572
1573
1574
1575
1576
1577
1578u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1579{
1580 unsigned int sta;
1581
1582 sta = get_slot_status(slot);
1583
1584 return (sta & ACPI_STA_SHOW_IN_UI) ? 0 : 1;
1585}
1586
1587
1588
1589
1590
1591
1592u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1593{
1594 unsigned int sta;
1595
1596 sta = get_slot_status(slot);
1597
1598 return (sta == 0) ? 0 : 1;
1599}
1600