1 // SPDX-License-Identifier: GPL-2.0+
3 * PCI Express Hot Plug Controller Driver
5 * Copyright (C) 1995,2001 Compaq Computer Corporation
7 * Copyright (C) 2001 IBM Corp.
8 * Copyright (C) 2003-2004 Intel Corporation
10 * All rights reserved.
16 #define dev_fmt(fmt) "pciehp: " fmt
18 #include <linux/kernel.h>
19 #include <linux/types.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/pci.h>
24 /* The following routines constitute the bulk of the
25 hotplug controller logic
28 #define SAFE_REMOVAL true
29 #define SURPRISE_REMOVAL false
31 static void set_slot_off(struct controller *ctrl)
34 * Turn off slot, turn on attention indicator, turn off power
37 if (POWER_CTRL(ctrl)) {
38 pciehp_power_off_slot(ctrl);
41 * After turning power off, we must wait for at least 1 second
42 * before taking any action that relies on power having been
43 * removed from the slot/adapter.
48 pciehp_set_indicators(ctrl, PCI_EXP_SLTCTL_PWR_IND_OFF,
49 PCI_EXP_SLTCTL_ATTN_IND_ON);
53 * board_added - Called after a board has been added to the system.
54 * @ctrl: PCIe hotplug controller where board is added
56 * Turns power on for the board.
59 static int board_added(struct controller *ctrl)
62 struct pci_bus *parent = ctrl->pcie->port->subordinate;
64 if (POWER_CTRL(ctrl)) {
66 retval = pciehp_power_on_slot(ctrl);
71 pciehp_set_indicators(ctrl, PCI_EXP_SLTCTL_PWR_IND_BLINK,
74 /* Check link training status */
75 retval = pciehp_check_link_status(ctrl);
77 ctrl_err(ctrl, "Failed to check link status\n");
81 /* Check for a power fault */
82 if (ctrl->power_fault_detected || pciehp_query_power_fault(ctrl)) {
83 ctrl_err(ctrl, "Slot(%s): Power fault\n", slot_name(ctrl));
88 retval = pciehp_configure_device(ctrl);
90 if (retval != -EEXIST) {
91 ctrl_err(ctrl, "Cannot add device at %04x:%02x:00\n",
92 pci_domain_nr(parent), parent->number);
97 pciehp_set_indicators(ctrl, PCI_EXP_SLTCTL_PWR_IND_ON,
98 PCI_EXP_SLTCTL_ATTN_IND_OFF);
107 * remove_board - Turn off slot and Power Indicator
108 * @ctrl: PCIe hotplug controller where board is being removed
109 * @safe_removal: whether the board is safely removed (versus surprise removed)
111 static void remove_board(struct controller *ctrl, bool safe_removal)
113 pciehp_unconfigure_device(ctrl, safe_removal);
115 if (POWER_CTRL(ctrl)) {
116 pciehp_power_off_slot(ctrl);
119 * After turning power off, we must wait for at least 1 second
120 * before taking any action that relies on power having been
121 * removed from the slot/adapter.
125 /* Ignore link or presence changes caused by power off */
126 atomic_and(~(PCI_EXP_SLTSTA_DLLSC | PCI_EXP_SLTSTA_PDC),
127 &ctrl->pending_events);
130 pciehp_set_indicators(ctrl, PCI_EXP_SLTCTL_PWR_IND_OFF,
134 static int pciehp_enable_slot(struct controller *ctrl);
135 static int pciehp_disable_slot(struct controller *ctrl, bool safe_removal);
137 void pciehp_request(struct controller *ctrl, int action)
139 atomic_or(action, &ctrl->pending_events);
140 if (!pciehp_poll_mode)
141 irq_wake_thread(ctrl->pcie->irq, ctrl);
144 void pciehp_queue_pushbutton_work(struct work_struct *work)
146 struct controller *ctrl = container_of(work, struct controller,
149 mutex_lock(&ctrl->state_lock);
150 switch (ctrl->state) {
151 case BLINKINGOFF_STATE:
152 pciehp_request(ctrl, DISABLE_SLOT);
154 case BLINKINGON_STATE:
155 pciehp_request(ctrl, PCI_EXP_SLTSTA_PDC);
160 mutex_unlock(&ctrl->state_lock);
163 void pciehp_handle_button_press(struct controller *ctrl)
165 mutex_lock(&ctrl->state_lock);
166 switch (ctrl->state) {
169 if (ctrl->state == ON_STATE) {
170 ctrl->state = BLINKINGOFF_STATE;
171 ctrl_info(ctrl, "Slot(%s): Powering off due to button press\n",
174 ctrl->state = BLINKINGON_STATE;
175 ctrl_info(ctrl, "Slot(%s) Powering on due to button press\n",
178 /* blink power indicator and turn off attention */
179 pciehp_set_indicators(ctrl, PCI_EXP_SLTCTL_PWR_IND_BLINK,
180 PCI_EXP_SLTCTL_ATTN_IND_OFF);
181 schedule_delayed_work(&ctrl->button_work, 5 * HZ);
183 case BLINKINGOFF_STATE:
184 case BLINKINGON_STATE:
186 * Cancel if we are still blinking; this means that we
187 * press the attention again before the 5 sec. limit
188 * expires to cancel hot-add or hot-remove
190 ctrl_info(ctrl, "Slot(%s): Button cancel\n", slot_name(ctrl));
191 cancel_delayed_work(&ctrl->button_work);
192 if (ctrl->state == BLINKINGOFF_STATE) {
193 ctrl->state = ON_STATE;
194 pciehp_set_indicators(ctrl, PCI_EXP_SLTCTL_PWR_IND_ON,
195 PCI_EXP_SLTCTL_ATTN_IND_OFF);
197 ctrl->state = OFF_STATE;
198 pciehp_set_indicators(ctrl, PCI_EXP_SLTCTL_PWR_IND_OFF,
199 PCI_EXP_SLTCTL_ATTN_IND_OFF);
201 ctrl_info(ctrl, "Slot(%s): Action canceled due to button press\n",
205 ctrl_err(ctrl, "Slot(%s): Ignoring invalid state %#x\n",
206 slot_name(ctrl), ctrl->state);
209 mutex_unlock(&ctrl->state_lock);
212 void pciehp_handle_disable_request(struct controller *ctrl)
214 mutex_lock(&ctrl->state_lock);
215 switch (ctrl->state) {
216 case BLINKINGON_STATE:
217 case BLINKINGOFF_STATE:
218 cancel_delayed_work(&ctrl->button_work);
221 ctrl->state = POWEROFF_STATE;
222 mutex_unlock(&ctrl->state_lock);
224 ctrl->request_result = pciehp_disable_slot(ctrl, SAFE_REMOVAL);
227 void pciehp_handle_presence_or_link_change(struct controller *ctrl, u32 events)
229 int present, link_active;
232 * If the slot is on and presence or link has changed, turn it off.
233 * Even if it's occupied again, we cannot assume the card is the same.
235 mutex_lock(&ctrl->state_lock);
236 switch (ctrl->state) {
237 case BLINKINGOFF_STATE:
238 cancel_delayed_work(&ctrl->button_work);
241 ctrl->state = POWEROFF_STATE;
242 mutex_unlock(&ctrl->state_lock);
243 if (events & PCI_EXP_SLTSTA_DLLSC)
244 ctrl_info(ctrl, "Slot(%s): Link Down\n",
246 if (events & PCI_EXP_SLTSTA_PDC)
247 ctrl_info(ctrl, "Slot(%s): Card not present\n",
249 pciehp_disable_slot(ctrl, SURPRISE_REMOVAL);
252 mutex_unlock(&ctrl->state_lock);
256 /* Turn the slot on if it's occupied or link is up */
257 mutex_lock(&ctrl->state_lock);
258 present = pciehp_card_present(ctrl);
259 link_active = pciehp_check_link_active(ctrl);
260 if (present <= 0 && link_active <= 0) {
261 mutex_unlock(&ctrl->state_lock);
265 switch (ctrl->state) {
266 case BLINKINGON_STATE:
267 cancel_delayed_work(&ctrl->button_work);
270 ctrl->state = POWERON_STATE;
271 mutex_unlock(&ctrl->state_lock);
273 ctrl_info(ctrl, "Slot(%s): Card present\n",
276 ctrl_info(ctrl, "Slot(%s): Link Up\n",
278 ctrl->request_result = pciehp_enable_slot(ctrl);
281 mutex_unlock(&ctrl->state_lock);
286 static int __pciehp_enable_slot(struct controller *ctrl)
290 if (MRL_SENS(ctrl)) {
291 pciehp_get_latch_status(ctrl, &getstatus);
293 ctrl_info(ctrl, "Slot(%s): Latch open\n",
299 if (POWER_CTRL(ctrl)) {
300 pciehp_get_power_status(ctrl, &getstatus);
302 ctrl_info(ctrl, "Slot(%s): Already enabled\n",
308 return board_added(ctrl);
311 static int pciehp_enable_slot(struct controller *ctrl)
315 pm_runtime_get_sync(&ctrl->pcie->port->dev);
316 ret = __pciehp_enable_slot(ctrl);
317 if (ret && ATTN_BUTTN(ctrl))
318 /* may be blinking */
319 pciehp_set_indicators(ctrl, PCI_EXP_SLTCTL_PWR_IND_OFF,
321 pm_runtime_put(&ctrl->pcie->port->dev);
323 mutex_lock(&ctrl->state_lock);
324 ctrl->state = ret ? OFF_STATE : ON_STATE;
325 mutex_unlock(&ctrl->state_lock);
330 static int __pciehp_disable_slot(struct controller *ctrl, bool safe_removal)
334 if (POWER_CTRL(ctrl)) {
335 pciehp_get_power_status(ctrl, &getstatus);
337 ctrl_info(ctrl, "Slot(%s): Already disabled\n",
343 remove_board(ctrl, safe_removal);
347 static int pciehp_disable_slot(struct controller *ctrl, bool safe_removal)
351 pm_runtime_get_sync(&ctrl->pcie->port->dev);
352 ret = __pciehp_disable_slot(ctrl, safe_removal);
353 pm_runtime_put(&ctrl->pcie->port->dev);
355 mutex_lock(&ctrl->state_lock);
356 ctrl->state = OFF_STATE;
357 mutex_unlock(&ctrl->state_lock);
362 int pciehp_sysfs_enable_slot(struct hotplug_slot *hotplug_slot)
364 struct controller *ctrl = to_ctrl(hotplug_slot);
366 mutex_lock(&ctrl->state_lock);
367 switch (ctrl->state) {
368 case BLINKINGON_STATE:
370 mutex_unlock(&ctrl->state_lock);
372 * The IRQ thread becomes a no-op if the user pulls out the
373 * card before the thread wakes up, so initialize to -ENODEV.
375 ctrl->request_result = -ENODEV;
376 pciehp_request(ctrl, PCI_EXP_SLTSTA_PDC);
377 wait_event(ctrl->requester,
378 !atomic_read(&ctrl->pending_events) &&
380 return ctrl->request_result;
382 ctrl_info(ctrl, "Slot(%s): Already in powering on state\n",
385 case BLINKINGOFF_STATE:
388 ctrl_info(ctrl, "Slot(%s): Already enabled\n",
392 ctrl_err(ctrl, "Slot(%s): Invalid state %#x\n",
393 slot_name(ctrl), ctrl->state);
396 mutex_unlock(&ctrl->state_lock);
401 int pciehp_sysfs_disable_slot(struct hotplug_slot *hotplug_slot)
403 struct controller *ctrl = to_ctrl(hotplug_slot);
405 mutex_lock(&ctrl->state_lock);
406 switch (ctrl->state) {
407 case BLINKINGOFF_STATE:
409 mutex_unlock(&ctrl->state_lock);
410 pciehp_request(ctrl, DISABLE_SLOT);
411 wait_event(ctrl->requester,
412 !atomic_read(&ctrl->pending_events) &&
414 return ctrl->request_result;
416 ctrl_info(ctrl, "Slot(%s): Already in powering off state\n",
419 case BLINKINGON_STATE:
422 ctrl_info(ctrl, "Slot(%s): Already disabled\n",
426 ctrl_err(ctrl, "Slot(%s): Invalid state %#x\n",
427 slot_name(ctrl), ctrl->state);
430 mutex_unlock(&ctrl->state_lock);