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>
26 #include <linux/interrupt.h>
27 #include <linux/time.h>
31 /* Global variables */
33 bool pciehp_poll_mode
;
37 * not really modular, but the easiest way to keep compat with existing
38 * bootargs behaviour is to continue using module_param here.
40 module_param(pciehp_debug
, bool, 0644);
41 module_param(pciehp_poll_mode
, bool, 0644);
42 module_param(pciehp_poll_time
, int, 0644);
43 MODULE_PARM_DESC(pciehp_debug
, "Debugging mode enabled or not");
44 MODULE_PARM_DESC(pciehp_poll_mode
, "Using polling mechanism for hot-plug events or not");
45 MODULE_PARM_DESC(pciehp_poll_time
, "Polling mechanism frequency, in seconds");
47 #define PCIE_MODULE_NAME "pciehp"
49 static int set_attention_status(struct hotplug_slot
*slot
, u8 value
);
50 static int enable_slot(struct hotplug_slot
*slot
);
51 static int disable_slot(struct hotplug_slot
*slot
);
52 static int get_power_status(struct hotplug_slot
*slot
, u8
*value
);
53 static int get_attention_status(struct hotplug_slot
*slot
, u8
*value
);
54 static int get_latch_status(struct hotplug_slot
*slot
, u8
*value
);
55 static int get_adapter_status(struct hotplug_slot
*slot
, u8
*value
);
56 static int reset_slot(struct hotplug_slot
*slot
, int probe
);
58 static int init_slot(struct controller
*ctrl
)
60 struct slot
*slot
= ctrl
->slot
;
61 struct hotplug_slot
*hotplug
= NULL
;
62 struct hotplug_slot_info
*info
= NULL
;
63 struct hotplug_slot_ops
*ops
= NULL
;
64 char name
[SLOT_NAME_SIZE
];
67 hotplug
= kzalloc(sizeof(*hotplug
), GFP_KERNEL
);
71 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
75 /* Setup hotplug slot ops */
76 ops
= kzalloc(sizeof(*ops
), GFP_KERNEL
);
80 ops
->enable_slot
= enable_slot
;
81 ops
->disable_slot
= disable_slot
;
82 ops
->get_power_status
= get_power_status
;
83 ops
->get_adapter_status
= get_adapter_status
;
84 ops
->reset_slot
= reset_slot
;
86 ops
->get_latch_status
= get_latch_status
;
88 ops
->get_attention_status
= get_attention_status
;
89 ops
->set_attention_status
= set_attention_status
;
90 } else if (ctrl
->pcie
->port
->hotplug_user_indicators
) {
91 ops
->get_attention_status
= pciehp_get_raw_indicator_status
;
92 ops
->set_attention_status
= pciehp_set_raw_indicator_status
;
95 /* register this slot with the hotplug pci core */
97 hotplug
->private = slot
;
99 slot
->hotplug_slot
= hotplug
;
100 snprintf(name
, SLOT_NAME_SIZE
, "%u", PSN(ctrl
));
102 retval
= pci_hp_initialize(hotplug
,
103 ctrl
->pcie
->port
->subordinate
, 0, name
);
105 ctrl_err(ctrl
, "pci_hp_initialize failed: error %d\n", retval
);
115 static void cleanup_slot(struct controller
*ctrl
)
117 struct hotplug_slot
*hotplug_slot
= ctrl
->slot
->hotplug_slot
;
119 pci_hp_destroy(hotplug_slot
);
120 kfree(hotplug_slot
->ops
);
121 kfree(hotplug_slot
->info
);
126 * set_attention_status - Turns the Amber LED for a slot on, off or blink
128 static int set_attention_status(struct hotplug_slot
*hotplug_slot
, u8 status
)
130 struct slot
*slot
= hotplug_slot
->private;
131 struct pci_dev
*pdev
= slot
->ctrl
->pcie
->port
;
133 pci_config_pm_runtime_get(pdev
);
134 pciehp_set_attention_status(slot
, status
);
135 pci_config_pm_runtime_put(pdev
);
140 static int enable_slot(struct hotplug_slot
*hotplug_slot
)
142 struct slot
*slot
= hotplug_slot
->private;
144 return pciehp_sysfs_enable_slot(slot
);
148 static int disable_slot(struct hotplug_slot
*hotplug_slot
)
150 struct slot
*slot
= hotplug_slot
->private;
152 return pciehp_sysfs_disable_slot(slot
);
155 static int get_power_status(struct hotplug_slot
*hotplug_slot
, u8
*value
)
157 struct slot
*slot
= hotplug_slot
->private;
158 struct pci_dev
*pdev
= slot
->ctrl
->pcie
->port
;
160 pci_config_pm_runtime_get(pdev
);
161 pciehp_get_power_status(slot
, value
);
162 pci_config_pm_runtime_put(pdev
);
166 static int get_attention_status(struct hotplug_slot
*hotplug_slot
, u8
*value
)
168 struct slot
*slot
= hotplug_slot
->private;
170 pciehp_get_attention_status(slot
, value
);
174 static int get_latch_status(struct hotplug_slot
*hotplug_slot
, u8
*value
)
176 struct slot
*slot
= hotplug_slot
->private;
177 struct pci_dev
*pdev
= slot
->ctrl
->pcie
->port
;
179 pci_config_pm_runtime_get(pdev
);
180 pciehp_get_latch_status(slot
, value
);
181 pci_config_pm_runtime_put(pdev
);
185 static int get_adapter_status(struct hotplug_slot
*hotplug_slot
, u8
*value
)
187 struct slot
*slot
= hotplug_slot
->private;
188 struct pci_dev
*pdev
= slot
->ctrl
->pcie
->port
;
190 pci_config_pm_runtime_get(pdev
);
191 pciehp_get_adapter_status(slot
, value
);
192 pci_config_pm_runtime_put(pdev
);
196 static int reset_slot(struct hotplug_slot
*hotplug_slot
, int probe
)
198 struct slot
*slot
= hotplug_slot
->private;
200 return pciehp_reset_slot(slot
, probe
);
204 * pciehp_check_presence() - synthesize event if presence has changed
206 * On probe and resume, an explicit presence check is necessary to bring up an
207 * occupied slot or bring down an unoccupied slot. This can't be triggered by
208 * events in the Slot Status register, they may be stale and are therefore
209 * cleared. Secondly, sending an interrupt for "events that occur while
210 * interrupt generation is disabled [when] interrupt generation is subsequently
211 * enabled" is optional per PCIe r4.0, sec 6.7.3.4.
213 static void pciehp_check_presence(struct controller
*ctrl
)
215 struct slot
*slot
= ctrl
->slot
;
218 down_read(&ctrl
->reset_lock
);
219 mutex_lock(&slot
->lock
);
221 pciehp_get_adapter_status(slot
, &occupied
);
222 if ((occupied
&& (slot
->state
== OFF_STATE
||
223 slot
->state
== BLINKINGON_STATE
)) ||
224 (!occupied
&& (slot
->state
== ON_STATE
||
225 slot
->state
== BLINKINGOFF_STATE
)))
226 pciehp_request(ctrl
, PCI_EXP_SLTSTA_PDC
);
228 mutex_unlock(&slot
->lock
);
229 up_read(&ctrl
->reset_lock
);
232 static int pciehp_probe(struct pcie_device
*dev
)
235 struct controller
*ctrl
;
238 /* If this is not a "hotplug" service, we have no business here. */
239 if (dev
->service
!= PCIE_PORT_SERVICE_HP
)
242 if (!dev
->port
->subordinate
) {
243 /* Can happen if we run out of bus numbers during probe */
244 dev_err(&dev
->device
,
245 "Hotplug bridge without secondary bus, ignoring\n");
249 ctrl
= pcie_init(dev
);
251 dev_err(&dev
->device
, "Controller initialization failed\n");
254 set_service_data(dev
, ctrl
);
256 /* Setup the slot information structures */
257 rc
= init_slot(ctrl
);
260 ctrl_warn(ctrl
, "Slot already registered by another hotplug driver\n");
262 ctrl_err(ctrl
, "Slot initialization failed (%d)\n", rc
);
263 goto err_out_release_ctlr
;
266 /* Enable events after we have setup the data structures */
267 rc
= pcie_init_notification(ctrl
);
269 ctrl_err(ctrl
, "Notification initialization failed (%d)\n", rc
);
270 goto err_out_free_ctrl_slot
;
273 /* Publish to user space */
275 rc
= pci_hp_add(slot
->hotplug_slot
);
277 ctrl_err(ctrl
, "Publication to user space failed (%d)\n", rc
);
278 goto err_out_shutdown_notification
;
281 pciehp_check_presence(ctrl
);
285 err_out_shutdown_notification
:
286 pcie_shutdown_notification(ctrl
);
287 err_out_free_ctrl_slot
:
289 err_out_release_ctlr
:
290 pciehp_release_ctrl(ctrl
);
294 static void pciehp_remove(struct pcie_device
*dev
)
296 struct controller
*ctrl
= get_service_data(dev
);
298 pci_hp_del(ctrl
->slot
->hotplug_slot
);
299 pcie_shutdown_notification(ctrl
);
301 pciehp_release_ctrl(ctrl
);
305 static int pciehp_suspend(struct pcie_device
*dev
)
310 static int pciehp_resume_noirq(struct pcie_device
*dev
)
312 struct controller
*ctrl
= get_service_data(dev
);
313 struct slot
*slot
= ctrl
->slot
;
315 /* pci_restore_state() just wrote to the Slot Control register */
316 ctrl
->cmd_started
= jiffies
;
317 ctrl
->cmd_busy
= true;
319 /* clear spurious events from rediscovery of inserted card */
320 if (slot
->state
== ON_STATE
|| slot
->state
== BLINKINGOFF_STATE
)
321 pcie_clear_hotplug_events(ctrl
);
326 static int pciehp_resume(struct pcie_device
*dev
)
328 struct controller
*ctrl
= get_service_data(dev
);
330 pciehp_check_presence(ctrl
);
336 static struct pcie_port_service_driver hpdriver_portdrv
= {
337 .name
= PCIE_MODULE_NAME
,
338 .port_type
= PCIE_ANY_PORT
,
339 .service
= PCIE_PORT_SERVICE_HP
,
341 .probe
= pciehp_probe
,
342 .remove
= pciehp_remove
,
345 .suspend
= pciehp_suspend
,
346 .resume_noirq
= pciehp_resume_noirq
,
347 .resume
= pciehp_resume
,
351 static int __init
pcied_init(void)
355 retval
= pcie_port_service_register(&hpdriver_portdrv
);
356 dbg("pcie_port_service_register = %d\n", retval
);
358 dbg("Failure to register service\n");
362 device_initcall(pcied_init
);