blob: 2574700db461559717a95e0623c1c58c5821c687 [file] [log] [blame]
Kenji Kaneshigec9ffa5a2008-12-17 12:07:38 +09001/*
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>
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +090027#include <linux/pci.h>
28#include <linux/pci_hotplug.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Kenji Kaneshigec9ffa5a2008-12-17 12:07:38 +090030#include "pciehp.h"
31
32#define PCIEHP_DETECT_PCIE (0)
33#define PCIEHP_DETECT_ACPI (1)
Kenji Kaneshigee046cbd2008-12-17 12:08:15 +090034#define PCIEHP_DETECT_AUTO (2)
35#define PCIEHP_DETECT_DEFAULT PCIEHP_DETECT_AUTO
Kenji Kaneshigec9ffa5a2008-12-17 12:07:38 +090036
Kenji Kaneshige8720d272009-09-15 17:24:46 +090037struct dummy_slot {
38 u32 number;
39 struct list_head list;
40};
41
Kenji Kaneshigec9ffa5a2008-12-17 12:07:38 +090042static int slot_detection_mode;
43static char *pciehp_detect_mode;
44module_param(pciehp_detect_mode, charp, 0444);
45MODULE_PARM_DESC(pciehp_detect_mode,
Kenji Kaneshigee046cbd2008-12-17 12:08:15 +090046 "Slot detection mode: pcie, acpi, auto\n"
47 " pcie - Use PCIe based slot detection\n"
48 " acpi - Use ACPI for slot detection\n"
49 " auto(default) - Auto select mode. Use acpi option if duplicate\n"
50 " slot ids are found. Otherwise, use pcie option\n");
Kenji Kaneshigec9ffa5a2008-12-17 12:07:38 +090051
Kenji Kaneshigec9ffa5a2008-12-17 12:07:38 +090052int pciehp_acpi_slot_detection_check(struct pci_dev *dev)
53{
54 if (slot_detection_mode != PCIEHP_DETECT_ACPI)
55 return 0;
Alex Chiang7f538662009-09-10 12:34:09 -060056 if (acpi_pci_detect_ejectable(DEVICE_ACPI_HANDLE(&dev->dev)))
Kenji Kaneshigec9ffa5a2008-12-17 12:07:38 +090057 return 0;
58 return -ENODEV;
59}
60
61static int __init parse_detect_mode(void)
62{
63 if (!pciehp_detect_mode)
64 return PCIEHP_DETECT_DEFAULT;
65 if (!strcmp(pciehp_detect_mode, "pcie"))
66 return PCIEHP_DETECT_PCIE;
67 if (!strcmp(pciehp_detect_mode, "acpi"))
68 return PCIEHP_DETECT_ACPI;
Kenji Kaneshigee046cbd2008-12-17 12:08:15 +090069 if (!strcmp(pciehp_detect_mode, "auto"))
70 return PCIEHP_DETECT_AUTO;
Kenji Kaneshigec9ffa5a2008-12-17 12:07:38 +090071 warn("bad specifier '%s' for pciehp_detect_mode. Use default\n",
72 pciehp_detect_mode);
73 return PCIEHP_DETECT_DEFAULT;
74}
75
Kenji Kaneshigee046cbd2008-12-17 12:08:15 +090076static int __initdata dup_slot_id;
77static int __initdata acpi_slot_detected;
78static struct list_head __initdata dummy_slots = LIST_HEAD_INIT(dummy_slots);
79
80/* Dummy driver for dumplicate name detection */
Rafael J. Wysocki0516c8b2009-01-13 14:44:19 +010081static int __init dummy_probe(struct pcie_device *dev)
Kenji Kaneshigee046cbd2008-12-17 12:08:15 +090082{
83 int pos;
84 u32 slot_cap;
Alex Chiang7f538662009-09-10 12:34:09 -060085 acpi_handle handle;
Kenji Kaneshige8720d272009-09-15 17:24:46 +090086 struct dummy_slot *slot, *tmp;
Kenji Kaneshigee046cbd2008-12-17 12:08:15 +090087 struct pci_dev *pdev = dev->port;
Rafael J. Wysocki28eb5f22010-08-21 22:02:38 +020088
Kenji Kaneshige1518c172009-11-11 14:34:52 +090089 pos = pci_pcie_cap(pdev);
90 if (!pos)
Kenji Kaneshigee046cbd2008-12-17 12:08:15 +090091 return -ENODEV;
92 pci_read_config_dword(pdev, pos + PCI_EXP_SLTCAP, &slot_cap);
Julia Lawall0b3e7382009-02-08 22:45:24 +010093 slot = kzalloc(sizeof(*slot), GFP_KERNEL);
94 if (!slot)
95 return -ENOMEM;
Kenji Kaneshigee046cbd2008-12-17 12:08:15 +090096 slot->number = slot_cap >> 19;
Kenji Kaneshige8720d272009-09-15 17:24:46 +090097 list_for_each_entry(tmp, &dummy_slots, list) {
Kenji Kaneshigee046cbd2008-12-17 12:08:15 +090098 if (tmp->number == slot->number)
99 dup_slot_id++;
100 }
Kenji Kaneshige8720d272009-09-15 17:24:46 +0900101 list_add_tail(&slot->list, &dummy_slots);
Alex Chiang7f538662009-09-10 12:34:09 -0600102 handle = DEVICE_ACPI_HANDLE(&pdev->dev);
103 if (!acpi_slot_detected && acpi_pci_detect_ejectable(handle))
Kenji Kaneshigee046cbd2008-12-17 12:08:15 +0900104 acpi_slot_detected = 1;
105 return -ENODEV; /* dummy driver always returns error */
106}
107
108static struct pcie_port_service_driver __initdata dummy_driver = {
109 .name = "pciehp_dummy",
Rafael J. Wysocki22106362009-01-13 14:46:46 +0100110 .port_type = PCIE_ANY_PORT,
111 .service = PCIE_PORT_SERVICE_HP,
Kenji Kaneshigee046cbd2008-12-17 12:08:15 +0900112 .probe = dummy_probe,
113};
114
115static int __init select_detection_mode(void)
116{
Kenji Kaneshige8720d272009-09-15 17:24:46 +0900117 struct dummy_slot *slot, *tmp;
Kenji Kaneshigee046cbd2008-12-17 12:08:15 +0900118 pcie_port_service_register(&dummy_driver);
119 pcie_port_service_unregister(&dummy_driver);
Kenji Kaneshige8720d272009-09-15 17:24:46 +0900120 list_for_each_entry_safe(slot, tmp, &dummy_slots, list) {
121 list_del(&slot->list);
Kenji Kaneshigee046cbd2008-12-17 12:08:15 +0900122 kfree(slot);
123 }
124 if (acpi_slot_detected && dup_slot_id)
125 return PCIEHP_DETECT_ACPI;
126 return PCIEHP_DETECT_PCIE;
127}
128
Kenji Kaneshigec9ffa5a2008-12-17 12:07:38 +0900129void __init pciehp_acpi_slot_detection_init(void)
130{
131 slot_detection_mode = parse_detect_mode();
Kenji Kaneshigee046cbd2008-12-17 12:08:15 +0900132 if (slot_detection_mode != PCIEHP_DETECT_AUTO)
133 goto out;
134 slot_detection_mode = select_detection_mode();
135out:
136 if (slot_detection_mode == PCIEHP_DETECT_ACPI)
137 info("Using ACPI for slot detection.\n");
Kenji Kaneshigec9ffa5a2008-12-17 12:07:38 +0900138}