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 #include <linux/moduleparam.h>
21 #include <linux/kernel.h>
22 #include <linux/slab.h>
23 #include <linux/types.h>
24 #include <linux/pci.h>
25 #include "pciehp.h"
26 
27 #include "../pci.h"
28 
29 /* Global variables */
30 bool pciehp_debug;
31 bool pciehp_poll_mode;
32 int pciehp_poll_time;
33 
34 /*
35  * not really modular, but the easiest way to keep compat with existing
36  * bootargs behaviour is to continue using module_param here.
37  */
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");
44 
45 #define PCIE_MODULE_NAME "pciehp"
46 
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);
51 
52 static int init_slot(struct controller *ctrl)
53 {
54 	struct hotplug_slot_ops *ops;
55 	char name[SLOT_NAME_SIZE];
56 	int retval;
57 
58 	/* Setup hotplug slot ops */
59 	ops = kzalloc(sizeof(*ops), GFP_KERNEL);
60 	if (!ops)
61 		return -ENOMEM;
62 
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;
68 	if (MRL_SENS(ctrl))
69 		ops->get_latch_status = get_latch_status;
70 	if (ATTN_LED(ctrl)) {
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;
76 	}
77 
78 	/* register this slot with the hotplug pci core */
79 	ctrl->hotplug_slot.ops = ops;
80 	snprintf(name, SLOT_NAME_SIZE, "%u", PSN(ctrl));
81 
82 	retval = pci_hp_initialize(&ctrl->hotplug_slot,
83 				   ctrl->pcie->port->subordinate, 0, name);
84 	if (retval) {
85 		ctrl_err(ctrl, "pci_hp_initialize failed: error %d\n", retval);
86 		kfree(ops);
87 	}
88 	return retval;
89 }
90 
91 static void cleanup_slot(struct controller *ctrl)
92 {
93 	struct hotplug_slot *hotplug_slot = &ctrl->hotplug_slot;
94 
95 	pci_hp_destroy(hotplug_slot);
96 	kfree(hotplug_slot->ops);
97 }
98 
99 /*
100  * set_attention_status - Turns the Amber LED for a slot on, off or blink
101  */
102 static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
103 {
104 	struct controller *ctrl = to_ctrl(hotplug_slot);
105 	struct pci_dev *pdev = ctrl->pcie->port;
106 
107 	pci_config_pm_runtime_get(pdev);
108 	pciehp_set_attention_status(ctrl, status);
109 	pci_config_pm_runtime_put(pdev);
110 	return 0;
111 }
112 
113 static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
114 {
115 	struct controller *ctrl = to_ctrl(hotplug_slot);
116 	struct pci_dev *pdev = ctrl->pcie->port;
117 
118 	pci_config_pm_runtime_get(pdev);
119 	pciehp_get_power_status(ctrl, value);
120 	pci_config_pm_runtime_put(pdev);
121 	return 0;
122 }
123 
124 static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
125 {
126 	struct controller *ctrl = to_ctrl(hotplug_slot);
127 	struct pci_dev *pdev = ctrl->pcie->port;
128 
129 	pci_config_pm_runtime_get(pdev);
130 	pciehp_get_latch_status(ctrl, value);
131 	pci_config_pm_runtime_put(pdev);
132 	return 0;
133 }
134 
135 static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
136 {
137 	struct controller *ctrl = to_ctrl(hotplug_slot);
138 	struct pci_dev *pdev = ctrl->pcie->port;
139 
140 	pci_config_pm_runtime_get(pdev);
141 	*value = pciehp_card_present_or_link_active(ctrl);
142 	pci_config_pm_runtime_put(pdev);
143 	return 0;
144 }
145 
146 /**
147  * pciehp_check_presence() - synthesize event if presence has changed
148  *
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.
155  */
156 static void pciehp_check_presence(struct controller *ctrl)
157 {
158 	bool occupied;
159 
160 	down_read(&ctrl->reset_lock);
161 	mutex_lock(&ctrl->state_lock);
162 
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);
169 
170 	mutex_unlock(&ctrl->state_lock);
171 	up_read(&ctrl->reset_lock);
172 }
173 
174 static int pciehp_probe(struct pcie_device *dev)
175 {
176 	int rc;
177 	struct controller *ctrl;
178 
179 	/* If this is not a "hotplug" service, we have no business here. */
180 	if (dev->service != PCIE_PORT_SERVICE_HP)
181 		return -ENODEV;
182 
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");
187 		return -ENODEV;
188 	}
189 
190 	ctrl = pcie_init(dev);
191 	if (!ctrl) {
192 		dev_err(&dev->device, "Controller initialization failed\n");
193 		return -ENODEV;
194 	}
195 	set_service_data(dev, ctrl);
196 
197 	/* Setup the slot information structures */
198 	rc = init_slot(ctrl);
199 	if (rc) {
200 		if (rc == -EBUSY)
201 			ctrl_warn(ctrl, "Slot already registered by another hotplug driver\n");
202 		else
203 			ctrl_err(ctrl, "Slot initialization failed (%d)\n", rc);
204 		goto err_out_release_ctlr;
205 	}
206 
207 	/* Enable events after we have setup the data structures */
208 	rc = pcie_init_notification(ctrl);
209 	if (rc) {
210 		ctrl_err(ctrl, "Notification initialization failed (%d)\n", rc);
211 		goto err_out_free_ctrl_slot;
212 	}
213 
214 	/* Publish to user space */
215 	rc = pci_hp_add(&ctrl->hotplug_slot);
216 	if (rc) {
217 		ctrl_err(ctrl, "Publication to user space failed (%d)\n", rc);
218 		goto err_out_shutdown_notification;
219 	}
220 
221 	pciehp_check_presence(ctrl);
222 
223 	return 0;
224 
225 err_out_shutdown_notification:
226 	pcie_shutdown_notification(ctrl);
227 err_out_free_ctrl_slot:
228 	cleanup_slot(ctrl);
229 err_out_release_ctlr:
230 	pciehp_release_ctrl(ctrl);
231 	return -ENODEV;
232 }
233 
234 static void pciehp_remove(struct pcie_device *dev)
235 {
236 	struct controller *ctrl = get_service_data(dev);
237 
238 	pci_hp_del(&ctrl->hotplug_slot);
239 	pcie_shutdown_notification(ctrl);
240 	cleanup_slot(ctrl);
241 	pciehp_release_ctrl(ctrl);
242 }
243 
244 #ifdef CONFIG_PM
245 static bool pme_is_native(struct pcie_device *dev)
246 {
247 	const struct pci_host_bridge *host;
248 
249 	host = pci_find_host_bridge(dev->port->bus);
250 	return pcie_ports_native || host->native_pme;
251 }
252 
253 static int pciehp_suspend(struct pcie_device *dev)
254 {
255 	/*
256 	 * Disable hotplug interrupt so that it does not trigger
257 	 * immediately when the downstream link goes down.
258 	 */
259 	if (pme_is_native(dev))
260 		pcie_disable_interrupt(get_service_data(dev));
261 
262 	return 0;
263 }
264 
265 static int pciehp_resume_noirq(struct pcie_device *dev)
266 {
267 	struct controller *ctrl = get_service_data(dev);
268 
269 	/* pci_restore_state() just wrote to the Slot Control register */
270 	ctrl->cmd_started = jiffies;
271 	ctrl->cmd_busy = true;
272 
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);
276 
277 	return 0;
278 }
279 
280 static int pciehp_resume(struct pcie_device *dev)
281 {
282 	struct controller *ctrl = get_service_data(dev);
283 
284 	if (pme_is_native(dev))
285 		pcie_enable_interrupt(ctrl);
286 
287 	pciehp_check_presence(ctrl);
288 
289 	return 0;
290 }
291 
292 static int pciehp_runtime_resume(struct pcie_device *dev)
293 {
294 	struct controller *ctrl = get_service_data(dev);
295 
296 	/* pci_restore_state() just wrote to the Slot Control register */
297 	ctrl->cmd_started = jiffies;
298 	ctrl->cmd_busy = true;
299 
300 	/* clear spurious events from rediscovery of inserted card */
301 	if ((ctrl->state == ON_STATE || ctrl->state == BLINKINGOFF_STATE) &&
302 	     pme_is_native(dev))
303 		pcie_clear_hotplug_events(ctrl);
304 
305 	return pciehp_resume(dev);
306 }
307 #endif /* PM */
308 
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,
313 
314 	.probe		= pciehp_probe,
315 	.remove		= pciehp_remove,
316 
317 #ifdef	CONFIG_PM
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,
323 #endif	/* PM */
324 };
325 
326 int __init pcie_hp_init(void)
327 {
328 	int retval = 0;
329 
330 	retval = pcie_port_service_register(&hpdriver_portdrv);
331 	dbg("pcie_port_service_register = %d\n", retval);
332 	if (retval)
333 		dbg("Failure to register service\n");
334 
335 	return retval;
336 }
337