1 // SPDX-License-Identifier: GPL-2.0+
3 * PCI Express Hot Plug Controller Driver
5 * Copyright (C) 1995,2001 Compaq Computer Corporation
6 * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
7 * Copyright (C) 2001 IBM Corp.
8 * Copyright (C) 2003-2004 Intel Corporation
10 * All rights reserved.
12 * Send feedback to <greg@kroah.com>, <kristen.c.accardi@intel.com>
15 * Dan Zink <dan.zink@compaq.com>
16 * Greg Kroah-Hartman <greg@kroah.com>
17 * Dely Sy <dely.l.sy@intel.com>"
20 #include <linux/moduleparam.h>
21 #include <linux/kernel.h>
22 #include <linux/slab.h>
23 #include <linux/types.h>
24 #include <linux/pci.h>
29 /* Global variables */
31 bool pciehp_poll_mode
;
35 * not really modular, but the easiest way to keep compat with existing
36 * bootargs behaviour is to continue using module_param here.
38 module_param(pciehp_debug
, bool, 0644);
39 module_param(pciehp_poll_mode
, bool, 0644);
40 module_param(pciehp_poll_time
, int, 0644);
41 MODULE_PARM_DESC(pciehp_debug
, "Debugging mode enabled or not");
42 MODULE_PARM_DESC(pciehp_poll_mode
, "Using polling mechanism for hot-plug events or not");
43 MODULE_PARM_DESC(pciehp_poll_time
, "Polling mechanism frequency, in seconds");
45 #define PCIE_MODULE_NAME "pciehp"
47 static int set_attention_status(struct hotplug_slot
*slot
, u8 value
);
48 static int get_power_status(struct hotplug_slot
*slot
, u8
*value
);
49 static int get_latch_status(struct hotplug_slot
*slot
, u8
*value
);
50 static int get_adapter_status(struct hotplug_slot
*slot
, u8
*value
);
52 static int init_slot(struct controller
*ctrl
)
54 struct hotplug_slot_ops
*ops
;
55 char name
[SLOT_NAME_SIZE
];
58 /* Setup hotplug slot ops */
59 ops
= kzalloc(sizeof(*ops
), GFP_KERNEL
);
63 ops
->enable_slot
= pciehp_sysfs_enable_slot
;
64 ops
->disable_slot
= pciehp_sysfs_disable_slot
;
65 ops
->get_power_status
= get_power_status
;
66 ops
->get_adapter_status
= get_adapter_status
;
67 ops
->reset_slot
= pciehp_reset_slot
;
69 ops
->get_latch_status
= get_latch_status
;
71 ops
->get_attention_status
= pciehp_get_attention_status
;
72 ops
->set_attention_status
= set_attention_status
;
73 } else if (ctrl
->pcie
->port
->hotplug_user_indicators
) {
74 ops
->get_attention_status
= pciehp_get_raw_indicator_status
;
75 ops
->set_attention_status
= pciehp_set_raw_indicator_status
;
78 /* register this slot with the hotplug pci core */
79 ctrl
->hotplug_slot
.ops
= ops
;
80 snprintf(name
, SLOT_NAME_SIZE
, "%u", PSN(ctrl
));
82 retval
= pci_hp_initialize(&ctrl
->hotplug_slot
,
83 ctrl
->pcie
->port
->subordinate
, 0, name
);
85 ctrl_err(ctrl
, "pci_hp_initialize failed: error %d\n", retval
);
91 static void cleanup_slot(struct controller
*ctrl
)
93 struct hotplug_slot
*hotplug_slot
= &ctrl
->hotplug_slot
;
95 pci_hp_destroy(hotplug_slot
);
96 kfree(hotplug_slot
->ops
);
100 * set_attention_status - Turns the Amber LED for a slot on, off or blink
102 static int set_attention_status(struct hotplug_slot
*hotplug_slot
, u8 status
)
104 struct controller
*ctrl
= to_ctrl(hotplug_slot
);
105 struct pci_dev
*pdev
= ctrl
->pcie
->port
;
107 pci_config_pm_runtime_get(pdev
);
108 pciehp_set_attention_status(ctrl
, status
);
109 pci_config_pm_runtime_put(pdev
);
113 static int get_power_status(struct hotplug_slot
*hotplug_slot
, u8
*value
)
115 struct controller
*ctrl
= to_ctrl(hotplug_slot
);
116 struct pci_dev
*pdev
= ctrl
->pcie
->port
;
118 pci_config_pm_runtime_get(pdev
);
119 pciehp_get_power_status(ctrl
, value
);
120 pci_config_pm_runtime_put(pdev
);
124 static int get_latch_status(struct hotplug_slot
*hotplug_slot
, u8
*value
)
126 struct controller
*ctrl
= to_ctrl(hotplug_slot
);
127 struct pci_dev
*pdev
= ctrl
->pcie
->port
;
129 pci_config_pm_runtime_get(pdev
);
130 pciehp_get_latch_status(ctrl
, value
);
131 pci_config_pm_runtime_put(pdev
);
135 static int get_adapter_status(struct hotplug_slot
*hotplug_slot
, u8
*value
)
137 struct controller
*ctrl
= to_ctrl(hotplug_slot
);
138 struct pci_dev
*pdev
= ctrl
->pcie
->port
;
140 pci_config_pm_runtime_get(pdev
);
141 *value
= pciehp_card_present_or_link_active(ctrl
);
142 pci_config_pm_runtime_put(pdev
);
147 * pciehp_check_presence() - synthesize event if presence has changed
149 * On probe and resume, an explicit presence check is necessary to bring up an
150 * occupied slot or bring down an unoccupied slot. This can't be triggered by
151 * events in the Slot Status register, they may be stale and are therefore
152 * cleared. Secondly, sending an interrupt for "events that occur while
153 * interrupt generation is disabled [when] interrupt generation is subsequently
154 * enabled" is optional per PCIe r4.0, sec 6.7.3.4.
156 static void pciehp_check_presence(struct controller
*ctrl
)
160 down_read(&ctrl
->reset_lock
);
161 mutex_lock(&ctrl
->state_lock
);
163 occupied
= pciehp_card_present_or_link_active(ctrl
);
164 if ((occupied
&& (ctrl
->state
== OFF_STATE
||
165 ctrl
->state
== BLINKINGON_STATE
)) ||
166 (!occupied
&& (ctrl
->state
== ON_STATE
||
167 ctrl
->state
== BLINKINGOFF_STATE
)))
168 pciehp_request(ctrl
, PCI_EXP_SLTSTA_PDC
);
170 mutex_unlock(&ctrl
->state_lock
);
171 up_read(&ctrl
->reset_lock
);
174 static int pciehp_probe(struct pcie_device
*dev
)
177 struct controller
*ctrl
;
179 /* If this is not a "hotplug" service, we have no business here. */
180 if (dev
->service
!= PCIE_PORT_SERVICE_HP
)
183 if (!dev
->port
->subordinate
) {
184 /* Can happen if we run out of bus numbers during probe */
185 dev_err(&dev
->device
,
186 "Hotplug bridge without secondary bus, ignoring\n");
190 ctrl
= pcie_init(dev
);
192 dev_err(&dev
->device
, "Controller initialization failed\n");
195 set_service_data(dev
, ctrl
);
197 /* Setup the slot information structures */
198 rc
= init_slot(ctrl
);
201 ctrl_warn(ctrl
, "Slot already registered by another hotplug driver\n");
203 ctrl_err(ctrl
, "Slot initialization failed (%d)\n", rc
);
204 goto err_out_release_ctlr
;
207 /* Enable events after we have setup the data structures */
208 rc
= pcie_init_notification(ctrl
);
210 ctrl_err(ctrl
, "Notification initialization failed (%d)\n", rc
);
211 goto err_out_free_ctrl_slot
;
214 /* Publish to user space */
215 rc
= pci_hp_add(&ctrl
->hotplug_slot
);
217 ctrl_err(ctrl
, "Publication to user space failed (%d)\n", rc
);
218 goto err_out_shutdown_notification
;
221 pciehp_check_presence(ctrl
);
225 err_out_shutdown_notification
:
226 pcie_shutdown_notification(ctrl
);
227 err_out_free_ctrl_slot
:
229 err_out_release_ctlr
:
230 pciehp_release_ctrl(ctrl
);
234 static void pciehp_remove(struct pcie_device
*dev
)
236 struct controller
*ctrl
= get_service_data(dev
);
238 pci_hp_del(&ctrl
->hotplug_slot
);
239 pcie_shutdown_notification(ctrl
);
241 pciehp_release_ctrl(ctrl
);
245 static bool pme_is_native(struct pcie_device
*dev
)
247 const struct pci_host_bridge
*host
;
249 host
= pci_find_host_bridge(dev
->port
->bus
);
250 return pcie_ports_native
|| host
->native_pme
;
253 static int pciehp_suspend(struct pcie_device
*dev
)
256 * Disable hotplug interrupt so that it does not trigger
257 * immediately when the downstream link goes down.
259 if (pme_is_native(dev
))
260 pcie_disable_interrupt(get_service_data(dev
));
265 static int pciehp_resume_noirq(struct pcie_device
*dev
)
267 struct controller
*ctrl
= get_service_data(dev
);
269 /* pci_restore_state() just wrote to the Slot Control register */
270 ctrl
->cmd_started
= jiffies
;
271 ctrl
->cmd_busy
= true;
273 /* clear spurious events from rediscovery of inserted card */
274 if (ctrl
->state
== ON_STATE
|| ctrl
->state
== BLINKINGOFF_STATE
)
275 pcie_clear_hotplug_events(ctrl
);
280 static int pciehp_resume(struct pcie_device
*dev
)
282 struct controller
*ctrl
= get_service_data(dev
);
284 if (pme_is_native(dev
))
285 pcie_enable_interrupt(ctrl
);
287 pciehp_check_presence(ctrl
);
292 static int pciehp_runtime_resume(struct pcie_device
*dev
)
294 struct controller
*ctrl
= get_service_data(dev
);
296 /* pci_restore_state() just wrote to the Slot Control register */
297 ctrl
->cmd_started
= jiffies
;
298 ctrl
->cmd_busy
= true;
300 /* clear spurious events from rediscovery of inserted card */
301 if ((ctrl
->state
== ON_STATE
|| ctrl
->state
== BLINKINGOFF_STATE
) &&
303 pcie_clear_hotplug_events(ctrl
);
305 return pciehp_resume(dev
);
309 static struct pcie_port_service_driver hpdriver_portdrv
= {
310 .name
= PCIE_MODULE_NAME
,
311 .port_type
= PCIE_ANY_PORT
,
312 .service
= PCIE_PORT_SERVICE_HP
,
314 .probe
= pciehp_probe
,
315 .remove
= pciehp_remove
,
318 .suspend
= pciehp_suspend
,
319 .resume_noirq
= pciehp_resume_noirq
,
320 .resume
= pciehp_resume
,
321 .runtime_suspend
= pciehp_suspend
,
322 .runtime_resume
= pciehp_runtime_resume
,
326 int __init
pcie_hp_init(void)
330 retval
= pcie_port_service_register(&hpdriver_portdrv
);
331 dbg("pcie_port_service_register = %d\n", retval
);
333 dbg("Failure to register service\n");