Kenji Kaneshige | c9ffa5a | 2008-12-17 12:07:38 +0900 | [diff] [blame] | 1 | /* |
| 2 | * ACPI related functions for PCI Express Hot Plug driver. |
| 3 | * |
| 4 | * Copyright (C) 2008 Kenji Kaneshige |
| 5 | * Copyright (C) 2008 Fujitsu Limited. |
| 6 | * |
| 7 | * All rights reserved. |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or (at |
| 12 | * your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, but |
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or |
| 17 | * NON INFRINGEMENT. See the GNU General Public License for more |
| 18 | * details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 23 | * |
| 24 | */ |
| 25 | |
| 26 | #include <linux/acpi.h> |
| 27 | #include "pciehp.h" |
| 28 | |
| 29 | #define PCIEHP_DETECT_PCIE (0) |
| 30 | #define PCIEHP_DETECT_ACPI (1) |
Kenji Kaneshige | e046cbd | 2008-12-17 12:08:15 +0900 | [diff] [blame^] | 31 | #define PCIEHP_DETECT_AUTO (2) |
| 32 | #define PCIEHP_DETECT_DEFAULT PCIEHP_DETECT_AUTO |
Kenji Kaneshige | c9ffa5a | 2008-12-17 12:07:38 +0900 | [diff] [blame] | 33 | |
| 34 | static int slot_detection_mode; |
| 35 | static char *pciehp_detect_mode; |
| 36 | module_param(pciehp_detect_mode, charp, 0444); |
| 37 | MODULE_PARM_DESC(pciehp_detect_mode, |
Kenji Kaneshige | e046cbd | 2008-12-17 12:08:15 +0900 | [diff] [blame^] | 38 | "Slot detection mode: pcie, acpi, auto\n" |
| 39 | " pcie - Use PCIe based slot detection\n" |
| 40 | " acpi - Use ACPI for slot detection\n" |
| 41 | " auto(default) - Auto select mode. Use acpi option if duplicate\n" |
| 42 | " slot ids are found. Otherwise, use pcie option\n"); |
Kenji Kaneshige | c9ffa5a | 2008-12-17 12:07:38 +0900 | [diff] [blame] | 43 | |
| 44 | static int is_ejectable(acpi_handle handle) |
| 45 | { |
| 46 | acpi_status status; |
| 47 | acpi_handle tmp; |
| 48 | unsigned long long removable; |
| 49 | status = acpi_get_handle(handle, "_ADR", &tmp); |
| 50 | if (ACPI_FAILURE(status)) |
| 51 | return 0; |
| 52 | status = acpi_get_handle(handle, "_EJ0", &tmp); |
| 53 | if (ACPI_SUCCESS(status)) |
| 54 | return 1; |
| 55 | status = acpi_evaluate_integer(handle, "_RMV", NULL, &removable); |
| 56 | if (ACPI_SUCCESS(status) && removable) |
| 57 | return 1; |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | static acpi_status |
| 62 | check_hotplug(acpi_handle handle, u32 lvl, void *context, void **rv) |
| 63 | { |
| 64 | int *found = (int *)context; |
| 65 | if (is_ejectable(handle)) { |
| 66 | *found = 1; |
| 67 | return AE_CTRL_TERMINATE; |
| 68 | } |
| 69 | return AE_OK; |
| 70 | } |
| 71 | |
| 72 | static int pciehp_detect_acpi_slot(struct pci_bus *pbus) |
| 73 | { |
| 74 | acpi_handle handle; |
| 75 | struct pci_dev *pdev = pbus->self; |
| 76 | int found = 0; |
| 77 | |
| 78 | if (!pdev){ |
| 79 | int seg = pci_domain_nr(pbus), busnr = pbus->number; |
| 80 | handle = acpi_get_pci_rootbridge_handle(seg, busnr); |
| 81 | } else |
| 82 | handle = DEVICE_ACPI_HANDLE(&(pdev->dev)); |
| 83 | |
| 84 | if (!handle) |
| 85 | return 0; |
| 86 | |
| 87 | acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1, |
| 88 | check_hotplug, (void *)&found, NULL); |
| 89 | return found; |
| 90 | } |
| 91 | |
| 92 | int pciehp_acpi_slot_detection_check(struct pci_dev *dev) |
| 93 | { |
| 94 | if (slot_detection_mode != PCIEHP_DETECT_ACPI) |
| 95 | return 0; |
| 96 | if (pciehp_detect_acpi_slot(dev->subordinate)) |
| 97 | return 0; |
| 98 | return -ENODEV; |
| 99 | } |
| 100 | |
| 101 | static int __init parse_detect_mode(void) |
| 102 | { |
| 103 | if (!pciehp_detect_mode) |
| 104 | return PCIEHP_DETECT_DEFAULT; |
| 105 | if (!strcmp(pciehp_detect_mode, "pcie")) |
| 106 | return PCIEHP_DETECT_PCIE; |
| 107 | if (!strcmp(pciehp_detect_mode, "acpi")) |
| 108 | return PCIEHP_DETECT_ACPI; |
Kenji Kaneshige | e046cbd | 2008-12-17 12:08:15 +0900 | [diff] [blame^] | 109 | if (!strcmp(pciehp_detect_mode, "auto")) |
| 110 | return PCIEHP_DETECT_AUTO; |
Kenji Kaneshige | c9ffa5a | 2008-12-17 12:07:38 +0900 | [diff] [blame] | 111 | warn("bad specifier '%s' for pciehp_detect_mode. Use default\n", |
| 112 | pciehp_detect_mode); |
| 113 | return PCIEHP_DETECT_DEFAULT; |
| 114 | } |
| 115 | |
Kenji Kaneshige | e046cbd | 2008-12-17 12:08:15 +0900 | [diff] [blame^] | 116 | static struct pcie_port_service_id __initdata port_pci_ids[] = { |
| 117 | { |
| 118 | .vendor = PCI_ANY_ID, |
| 119 | .device = PCI_ANY_ID, |
| 120 | .port_type = PCIE_ANY_PORT, |
| 121 | .service_type = PCIE_PORT_SERVICE_HP, |
| 122 | .driver_data = 0, |
| 123 | }, { /* end: all zeroes */ } |
| 124 | }; |
| 125 | |
| 126 | static int __initdata dup_slot_id; |
| 127 | static int __initdata acpi_slot_detected; |
| 128 | static struct list_head __initdata dummy_slots = LIST_HEAD_INIT(dummy_slots); |
| 129 | |
| 130 | /* Dummy driver for dumplicate name detection */ |
| 131 | static int __init dummy_probe(struct pcie_device *dev, |
| 132 | const struct pcie_port_service_id *id) |
| 133 | { |
| 134 | int pos; |
| 135 | u32 slot_cap; |
| 136 | struct slot *slot, *tmp; |
| 137 | struct pci_dev *pdev = dev->port; |
| 138 | if (!(slot = kzalloc(sizeof(*slot), GFP_KERNEL))) |
| 139 | return -ENOMEM; |
| 140 | /* Note: pciehp_detect_mode != PCIEHP_DETECT_ACPI here */ |
| 141 | if (pciehp_get_hp_hw_control_from_firmware(pdev)) |
| 142 | return -ENODEV; |
| 143 | if (!(pos = pci_find_capability(pdev, PCI_CAP_ID_EXP))) |
| 144 | return -ENODEV; |
| 145 | pci_read_config_dword(pdev, pos + PCI_EXP_SLTCAP, &slot_cap); |
| 146 | slot->number = slot_cap >> 19; |
| 147 | list_for_each_entry(tmp, &dummy_slots, slot_list) { |
| 148 | if (tmp->number == slot->number) |
| 149 | dup_slot_id++; |
| 150 | } |
| 151 | list_add_tail(&slot->slot_list, &dummy_slots); |
| 152 | if (!acpi_slot_detected && pciehp_detect_acpi_slot(pdev->subordinate)) |
| 153 | acpi_slot_detected = 1; |
| 154 | return -ENODEV; /* dummy driver always returns error */ |
| 155 | } |
| 156 | |
| 157 | static struct pcie_port_service_driver __initdata dummy_driver = { |
| 158 | .name = "pciehp_dummy", |
| 159 | .id_table = port_pci_ids, |
| 160 | .probe = dummy_probe, |
| 161 | }; |
| 162 | |
| 163 | static int __init select_detection_mode(void) |
| 164 | { |
| 165 | struct slot *slot, *tmp; |
| 166 | pcie_port_service_register(&dummy_driver); |
| 167 | pcie_port_service_unregister(&dummy_driver); |
| 168 | list_for_each_entry_safe(slot, tmp, &dummy_slots, slot_list) { |
| 169 | list_del(&slot->slot_list); |
| 170 | kfree(slot); |
| 171 | } |
| 172 | if (acpi_slot_detected && dup_slot_id) |
| 173 | return PCIEHP_DETECT_ACPI; |
| 174 | return PCIEHP_DETECT_PCIE; |
| 175 | } |
| 176 | |
Kenji Kaneshige | c9ffa5a | 2008-12-17 12:07:38 +0900 | [diff] [blame] | 177 | void __init pciehp_acpi_slot_detection_init(void) |
| 178 | { |
| 179 | slot_detection_mode = parse_detect_mode(); |
Kenji Kaneshige | e046cbd | 2008-12-17 12:08:15 +0900 | [diff] [blame^] | 180 | if (slot_detection_mode != PCIEHP_DETECT_AUTO) |
| 181 | goto out; |
| 182 | slot_detection_mode = select_detection_mode(); |
| 183 | out: |
| 184 | if (slot_detection_mode == PCIEHP_DETECT_ACPI) |
| 185 | info("Using ACPI for slot detection.\n"); |
Kenji Kaneshige | c9ffa5a | 2008-12-17 12:07:38 +0900 | [diff] [blame] | 186 | } |