1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * PCI Express Hot Plug Controller Driver
4  *
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
9  *
10  * All rights reserved.
11  *
12  * Send feedback to <greg@kroah.com>, <kristen.c.accardi@intel.com>
13  *
14  * Authors:
15  *   Dan Zink <dan.zink@compaq.com>
16  *   Greg Kroah-Hartman <greg@kroah.com>
17  *   Dely Sy <dely.l.sy@intel.com>"
18  */
19 
20 #define pr_fmt(fmt) "pciehp: " fmt
21 #define dev_fmt pr_fmt
22 
23 #include <linux/moduleparam.h>
24 #include <linux/kernel.h>
25 #include <linux/slab.h>
26 #include <linux/types.h>
27 #include <linux/pci.h>
28 #include "pciehp.h"
29 
30 #include "../pci.h"
31 
32 /* Global variables */
33 bool pciehp_poll_mode;
34 int pciehp_poll_time;
35 
36 /*
37  * not really modular, but the easiest way to keep compat with existing
38  * bootargs behaviour is to continue using module_param here.
39  */
40 module_param(pciehp_poll_mode, bool, 0644);
41 module_param(pciehp_poll_time, int, 0644);
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");
44 
45 static int set_attention_status(struct hotplug_slot *slot, u8 value);
46 static int get_power_status(struct hotplug_slot *slot, u8 *value);
47 static int get_latch_status(struct hotplug_slot *slot, u8 *value);
48 static int get_adapter_status(struct hotplug_slot *slot, u8 *value);
49 
50 static int init_slot(struct controller *ctrl)
51 {
52 	struct hotplug_slot_ops *ops;
53 	char name[SLOT_NAME_SIZE];
54 	int retval;
55 
56 	/* Setup hotplug slot ops */
57 	ops = kzalloc(sizeof(*ops), GFP_KERNEL);
58 	if (!ops)
59 		return -ENOMEM;
60 
61 	ops->enable_slot = pciehp_sysfs_enable_slot;
62 	ops->disable_slot = pciehp_sysfs_disable_slot;
63 	ops->get_power_status = get_power_status;
64 	ops->get_adapter_status = get_adapter_status;
65 	ops->reset_slot = pciehp_reset_slot;
66 	if (MRL_SENS(ctrl))
67 		ops->get_latch_status = get_latch_status;
68 	if (ATTN_LED(ctrl)) {
69 		ops->get_attention_status = pciehp_get_attention_status;
70 		ops->set_attention_status = set_attention_status;
71 	} else if (ctrl->pcie->port->hotplug_user_indicators) {
72 		ops->get_attention_status = pciehp_get_raw_indicator_status;
73 		ops->set_attention_status = pciehp_set_raw_indicator_status;
74 	}
75 
76 	/* register this slot with the hotplug pci core */
77 	ctrl->hotplug_slot.ops = ops;
78 	snprintf(name, SLOT_NAME_SIZE, "%u", PSN(ctrl));
79 
80 	retval = pci_hp_initialize(&ctrl->hotplug_slot,
81 				   ctrl->pcie->port->subordinate, 0, name);
82 	if (retval) {
83 		ctrl_err(ctrl, "pci_hp_initialize failed: error %d\n", retval);
84 		kfree(ops);
85 	}
86 	return retval;
87 }
88 
89 static void cleanup_slot(struct controller *ctrl)
90 {
91 	struct hotplug_slot *hotplug_slot = &ctrl->hotplug_slot;
92 
93 	pci_hp_destroy(hotplug_slot);
94 	kfree(hotplug_slot->ops);
95 }
96 
97 /*
98  * set_attention_status - Turns the Amber LED for a slot on, off or blink
99  */
100 static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
101 {
102 	struct controller *ctrl = to_ctrl(hotplug_slot);
103 	struct pci_dev *pdev = ctrl->pcie->port;
104 
105 	pci_config_pm_runtime_get(pdev);
106 	pciehp_set_attention_status(ctrl, status);
107 	pci_config_pm_runtime_put(pdev);
108 	return 0;
109 }
110 
111 static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
112 {
113 	struct controller *ctrl = to_ctrl(hotplug_slot);
114 	struct pci_dev *pdev = ctrl->pcie->port;
115 
116 	pci_config_pm_runtime_get(pdev);
117 	pciehp_get_power_status(ctrl, value);
118 	pci_config_pm_runtime_put(pdev);
119 	return 0;
120 }
121 
122 static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
123 {
124 	struct controller *ctrl = to_ctrl(hotplug_slot);
125 	struct pci_dev *pdev = ctrl->pcie->port;
126 
127 	pci_config_pm_runtime_get(pdev);
128 	pciehp_get_latch_status(ctrl, value);
129 	pci_config_pm_runtime_put(pdev);
130 	return 0;
131 }
132 
133 static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
134 {
135 	struct controller *ctrl = to_ctrl(hotplug_slot);
136 	struct pci_dev *pdev = ctrl->pcie->port;
137 
138 	pci_config_pm_runtime_get(pdev);
139 	*value = pciehp_card_present_or_link_active(ctrl);
140 	pci_config_pm_runtime_put(pdev);
141 	return 0;
142 }
143 
144 /**
145  * pciehp_check_presence() - synthesize event if presence has changed
146  *
147  * On probe and resume, an explicit presence check is necessary to bring up an
148  * occupied slot or bring down an unoccupied slot.  This can't be triggered by
149  * events in the Slot Status register, they may be stale and are therefore
150  * cleared.  Secondly, sending an interrupt for "events that occur while
151  * interrupt generation is disabled [when] interrupt generation is subsequently
152  * enabled" is optional per PCIe r4.0, sec 6.7.3.4.
153  */
154 static void pciehp_check_presence(struct controller *ctrl)
155 {
156 	bool occupied;
157 
158 	down_read(&ctrl->reset_lock);
159 	mutex_lock(&ctrl->state_lock);
160 
161 	occupied = pciehp_card_present_or_link_active(ctrl);
162 	if ((occupied && (ctrl->state == OFF_STATE ||
163 			  ctrl->state == BLINKINGON_STATE)) ||
164 	    (!occupied && (ctrl->state == ON_STATE ||
165 			   ctrl->state == BLINKINGOFF_STATE)))
166 		pciehp_request(ctrl, PCI_EXP_SLTSTA_PDC);
167 
168 	mutex_unlock(&ctrl->state_lock);
169 	up_read(&ctrl->reset_lock);
170 }
171 
172 static int pciehp_probe(struct pcie_device *dev)
173 {
174 	int rc;
175 	struct controller *ctrl;
176 
177 	/* If this is not a "hotplug" service, we have no business here. */
178 	if (dev->service != PCIE_PORT_SERVICE_HP)
179 		return -ENODEV;
180 
181 	if (!dev->port->subordinate) {
182 		/* Can happen if we run out of bus numbers during probe */
183 		pci_err(dev->port,
184 			"Hotplug bridge without secondary bus, ignoring\n");
185 		return -ENODEV;
186 	}
187 
188 	ctrl = pcie_init(dev);
189 	if (!ctrl) {
190 		pci_err(dev->port, "Controller initialization failed\n");
191 		return -ENODEV;
192 	}
193 	set_service_data(dev, ctrl);
194 
195 	/* Setup the slot information structures */
196 	rc = init_slot(ctrl);
197 	if (rc) {
198 		if (rc == -EBUSY)
199 			ctrl_warn(ctrl, "Slot already registered by another hotplug driver\n");
200 		else
201 			ctrl_err(ctrl, "Slot initialization failed (%d)\n", rc);
202 		goto err_out_release_ctlr;
203 	}
204 
205 	/* Enable events after we have setup the data structures */
206 	rc = pcie_init_notification(ctrl);
207 	if (rc) {
208 		ctrl_err(ctrl, "Notification initialization failed (%d)\n", rc);
209 		goto err_out_free_ctrl_slot;
210 	}
211 
212 	/* Publish to user space */
213 	rc = pci_hp_add(&ctrl->hotplug_slot);
214 	if (rc) {
215 		ctrl_err(ctrl, "Publication to user space failed (%d)\n", rc);
216 		goto err_out_shutdown_notification;
217 	}
218 
219 	pciehp_check_presence(ctrl);
220 
221 	return 0;
222 
223 err_out_shutdown_notification:
224 	pcie_shutdown_notification(ctrl);
225 err_out_free_ctrl_slot:
226 	cleanup_slot(ctrl);
227 err_out_release_ctlr:
228 	pciehp_release_ctrl(ctrl);
229 	return -ENODEV;
230 }
231 
232 static void pciehp_remove(struct pcie_device *dev)
233 {
234 	struct controller *ctrl = get_service_data(dev);
235 
236 	pci_hp_del(&ctrl->hotplug_slot);
237 	pcie_shutdown_notification(ctrl);
238 	cleanup_slot(ctrl);
239 	pciehp_release_ctrl(ctrl);
240 }
241 
242 #ifdef CONFIG_PM
243 static bool pme_is_native(struct pcie_device *dev)
244 {
245 	const struct pci_host_bridge *host;
246 
247 	host = pci_find_host_bridge(dev->port->bus);
248 	return pcie_ports_native || host->native_pme;
249 }
250 
251 static int pciehp_suspend(struct pcie_device *dev)
252 {
253 	/*
254 	 * Disable hotplug interrupt so that it does not trigger
255 	 * immediately when the downstream link goes down.
256 	 */
257 	if (pme_is_native(dev))
258 		pcie_disable_interrupt(get_service_data(dev));
259 
260 	return 0;
261 }
262 
263 static int pciehp_resume_noirq(struct pcie_device *dev)
264 {
265 	struct controller *ctrl = get_service_data(dev);
266 
267 	/* pci_restore_state() just wrote to the Slot Control register */
268 	ctrl->cmd_started = jiffies;
269 	ctrl->cmd_busy = true;
270 
271 	/* clear spurious events from rediscovery of inserted card */
272 	if (ctrl->state == ON_STATE || ctrl->state == BLINKINGOFF_STATE)
273 		pcie_clear_hotplug_events(ctrl);
274 
275 	return 0;
276 }
277 
278 static int pciehp_resume(struct pcie_device *dev)
279 {
280 	struct controller *ctrl = get_service_data(dev);
281 
282 	if (pme_is_native(dev))
283 		pcie_enable_interrupt(ctrl);
284 
285 	pciehp_check_presence(ctrl);
286 
287 	return 0;
288 }
289 
290 static int pciehp_runtime_resume(struct pcie_device *dev)
291 {
292 	struct controller *ctrl = get_service_data(dev);
293 
294 	/* pci_restore_state() just wrote to the Slot Control register */
295 	ctrl->cmd_started = jiffies;
296 	ctrl->cmd_busy = true;
297 
298 	/* clear spurious events from rediscovery of inserted card */
299 	if ((ctrl->state == ON_STATE || ctrl->state == BLINKINGOFF_STATE) &&
300 	     pme_is_native(dev))
301 		pcie_clear_hotplug_events(ctrl);
302 
303 	return pciehp_resume(dev);
304 }
305 #endif /* PM */
306 
307 static struct pcie_port_service_driver hpdriver_portdrv = {
308 	.name		= "pciehp",
309 	.port_type	= PCIE_ANY_PORT,
310 	.service	= PCIE_PORT_SERVICE_HP,
311 
312 	.probe		= pciehp_probe,
313 	.remove		= pciehp_remove,
314 
315 #ifdef	CONFIG_PM
316 	.suspend	= pciehp_suspend,
317 	.resume_noirq	= pciehp_resume_noirq,
318 	.resume		= pciehp_resume,
319 	.runtime_suspend = pciehp_suspend,
320 	.runtime_resume	= pciehp_runtime_resume,
321 #endif	/* PM */
322 };
323 
324 int __init pcie_hp_init(void)
325 {
326 	int retval = 0;
327 
328 	retval = pcie_port_service_register(&hpdriver_portdrv);
329 	pr_debug("pcie_port_service_register = %d\n", retval);
330 	if (retval)
331 		pr_debug("Failure to register service\n");
332 
333 	return retval;
334 }
335