1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Standard 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  */
15 
16 #include <linux/module.h>
17 #include <linux/moduleparam.h>
18 #include <linux/kernel.h>
19 #include <linux/types.h>
20 #include <linux/slab.h>
21 #include <linux/pci.h>
22 #include "shpchp.h"
23 
24 /* Global variables */
25 bool shpchp_debug;
26 bool shpchp_poll_mode;
27 int shpchp_poll_time;
28 
29 #define DRIVER_VERSION	"0.4"
30 #define DRIVER_AUTHOR	"Dan Zink <dan.zink@compaq.com>, Greg Kroah-Hartman <greg@kroah.com>, Dely Sy <dely.l.sy@intel.com>"
31 #define DRIVER_DESC	"Standard Hot Plug PCI Controller Driver"
32 
33 MODULE_AUTHOR(DRIVER_AUTHOR);
34 MODULE_DESCRIPTION(DRIVER_DESC);
35 
36 module_param(shpchp_debug, bool, 0644);
37 module_param(shpchp_poll_mode, bool, 0644);
38 module_param(shpchp_poll_time, int, 0644);
39 MODULE_PARM_DESC(shpchp_debug, "Debugging mode enabled or not");
40 MODULE_PARM_DESC(shpchp_poll_mode, "Using polling mechanism for hot-plug events or not");
41 MODULE_PARM_DESC(shpchp_poll_time, "Polling mechanism frequency, in seconds");
42 
43 #define SHPC_MODULE_NAME "shpchp"
44 
45 static int set_attention_status(struct hotplug_slot *slot, u8 value);
46 static int enable_slot(struct hotplug_slot *slot);
47 static int disable_slot(struct hotplug_slot *slot);
48 static int get_power_status(struct hotplug_slot *slot, u8 *value);
49 static int get_attention_status(struct hotplug_slot *slot, u8 *value);
50 static int get_latch_status(struct hotplug_slot *slot, u8 *value);
51 static int get_adapter_status(struct hotplug_slot *slot, u8 *value);
52 
53 static const struct hotplug_slot_ops shpchp_hotplug_slot_ops = {
54 	.set_attention_status =	set_attention_status,
55 	.enable_slot =		enable_slot,
56 	.disable_slot =		disable_slot,
57 	.get_power_status =	get_power_status,
58 	.get_attention_status =	get_attention_status,
59 	.get_latch_status =	get_latch_status,
60 	.get_adapter_status =	get_adapter_status,
61 };
62 
init_slots(struct controller * ctrl)63 static int init_slots(struct controller *ctrl)
64 {
65 	struct slot *slot;
66 	struct hotplug_slot *hotplug_slot;
67 	char name[SLOT_NAME_SIZE];
68 	int retval;
69 	int i;
70 
71 	for (i = 0; i < ctrl->num_slots; i++) {
72 		slot = kzalloc(sizeof(*slot), GFP_KERNEL);
73 		if (!slot) {
74 			retval = -ENOMEM;
75 			goto error;
76 		}
77 
78 		hotplug_slot = &slot->hotplug_slot;
79 
80 		slot->hp_slot = i;
81 		slot->ctrl = ctrl;
82 		slot->bus = ctrl->pci_dev->subordinate->number;
83 		slot->device = ctrl->slot_device_offset + i;
84 		slot->hpc_ops = ctrl->hpc_ops;
85 		slot->number = ctrl->first_slot + (ctrl->slot_num_inc * i);
86 
87 		slot->wq = alloc_workqueue("shpchp-%d", 0, 0, slot->number);
88 		if (!slot->wq) {
89 			retval = -ENOMEM;
90 			goto error_slot;
91 		}
92 
93 		mutex_init(&slot->lock);
94 		INIT_DELAYED_WORK(&slot->work, shpchp_queue_pushbutton_work);
95 
96 		/* register this slot with the hotplug pci core */
97 		snprintf(name, SLOT_NAME_SIZE, "%d", slot->number);
98 		hotplug_slot->ops = &shpchp_hotplug_slot_ops;
99 
100 		ctrl_dbg(ctrl, "Registering domain:bus:dev=%04x:%02x:%02x hp_slot=%x sun=%x slot_device_offset=%x\n",
101 			 pci_domain_nr(ctrl->pci_dev->subordinate),
102 			 slot->bus, slot->device, slot->hp_slot, slot->number,
103 			 ctrl->slot_device_offset);
104 		retval = pci_hp_register(hotplug_slot,
105 				ctrl->pci_dev->subordinate, slot->device, name);
106 		if (retval) {
107 			ctrl_err(ctrl, "pci_hp_register failed with error %d\n",
108 				 retval);
109 			goto error_slotwq;
110 		}
111 
112 		get_power_status(hotplug_slot, &slot->pwr_save);
113 		get_attention_status(hotplug_slot, &slot->attention_save);
114 		get_latch_status(hotplug_slot, &slot->latch_save);
115 		get_adapter_status(hotplug_slot, &slot->presence_save);
116 
117 		list_add(&slot->slot_list, &ctrl->slot_list);
118 	}
119 
120 	return 0;
121 error_slotwq:
122 	destroy_workqueue(slot->wq);
123 error_slot:
124 	kfree(slot);
125 error:
126 	return retval;
127 }
128 
cleanup_slots(struct controller * ctrl)129 void cleanup_slots(struct controller *ctrl)
130 {
131 	struct slot *slot, *next;
132 
133 	list_for_each_entry_safe(slot, next, &ctrl->slot_list, slot_list) {
134 		list_del(&slot->slot_list);
135 		cancel_delayed_work(&slot->work);
136 		destroy_workqueue(slot->wq);
137 		pci_hp_deregister(&slot->hotplug_slot);
138 		kfree(slot);
139 	}
140 }
141 
142 /*
143  * set_attention_status - Turns the Amber LED for a slot on, off or blink
144  */
set_attention_status(struct hotplug_slot * hotplug_slot,u8 status)145 static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
146 {
147 	struct slot *slot = get_slot(hotplug_slot);
148 
149 	ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
150 		 __func__, slot_name(slot));
151 
152 	slot->attention_save = status;
153 	slot->hpc_ops->set_attention_status(slot, status);
154 
155 	return 0;
156 }
157 
enable_slot(struct hotplug_slot * hotplug_slot)158 static int enable_slot(struct hotplug_slot *hotplug_slot)
159 {
160 	struct slot *slot = get_slot(hotplug_slot);
161 
162 	ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
163 		 __func__, slot_name(slot));
164 
165 	return shpchp_sysfs_enable_slot(slot);
166 }
167 
disable_slot(struct hotplug_slot * hotplug_slot)168 static int disable_slot(struct hotplug_slot *hotplug_slot)
169 {
170 	struct slot *slot = get_slot(hotplug_slot);
171 
172 	ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
173 		 __func__, slot_name(slot));
174 
175 	return shpchp_sysfs_disable_slot(slot);
176 }
177 
get_power_status(struct hotplug_slot * hotplug_slot,u8 * value)178 static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
179 {
180 	struct slot *slot = get_slot(hotplug_slot);
181 	int retval;
182 
183 	ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
184 		 __func__, slot_name(slot));
185 
186 	retval = slot->hpc_ops->get_power_status(slot, value);
187 	if (retval < 0)
188 		*value = slot->pwr_save;
189 
190 	return 0;
191 }
192 
get_attention_status(struct hotplug_slot * hotplug_slot,u8 * value)193 static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value)
194 {
195 	struct slot *slot = get_slot(hotplug_slot);
196 	int retval;
197 
198 	ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
199 		 __func__, slot_name(slot));
200 
201 	retval = slot->hpc_ops->get_attention_status(slot, value);
202 	if (retval < 0)
203 		*value = slot->attention_save;
204 
205 	return 0;
206 }
207 
get_latch_status(struct hotplug_slot * hotplug_slot,u8 * value)208 static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
209 {
210 	struct slot *slot = get_slot(hotplug_slot);
211 	int retval;
212 
213 	ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
214 		 __func__, slot_name(slot));
215 
216 	retval = slot->hpc_ops->get_latch_status(slot, value);
217 	if (retval < 0)
218 		*value = slot->latch_save;
219 
220 	return 0;
221 }
222 
get_adapter_status(struct hotplug_slot * hotplug_slot,u8 * value)223 static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
224 {
225 	struct slot *slot = get_slot(hotplug_slot);
226 	int retval;
227 
228 	ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
229 		 __func__, slot_name(slot));
230 
231 	retval = slot->hpc_ops->get_adapter_status(slot, value);
232 	if (retval < 0)
233 		*value = slot->presence_save;
234 
235 	return 0;
236 }
237 
shpc_capable(struct pci_dev * bridge)238 static bool shpc_capable(struct pci_dev *bridge)
239 {
240 	/*
241 	 * It is assumed that AMD GOLAM chips support SHPC but they do not
242 	 * have SHPC capability.
243 	 */
244 	if (bridge->vendor == PCI_VENDOR_ID_AMD &&
245 	    bridge->device == PCI_DEVICE_ID_AMD_GOLAM_7450)
246 		return true;
247 
248 	if (pci_find_capability(bridge, PCI_CAP_ID_SHPC))
249 		return true;
250 
251 	return false;
252 }
253 
shpc_probe(struct pci_dev * pdev,const struct pci_device_id * ent)254 static int shpc_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
255 {
256 	int rc;
257 	struct controller *ctrl;
258 
259 	if (!shpc_capable(pdev))
260 		return -ENODEV;
261 
262 	if (acpi_get_hp_hw_control_from_firmware(pdev))
263 		return -ENODEV;
264 
265 	ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
266 	if (!ctrl)
267 		goto err_out_none;
268 
269 	INIT_LIST_HEAD(&ctrl->slot_list);
270 
271 	rc = shpc_init(ctrl, pdev);
272 	if (rc) {
273 		ctrl_dbg(ctrl, "Controller initialization failed\n");
274 		goto err_out_free_ctrl;
275 	}
276 
277 	pci_set_drvdata(pdev, ctrl);
278 
279 	/* Setup the slot information structures */
280 	rc = init_slots(ctrl);
281 	if (rc) {
282 		ctrl_err(ctrl, "Slot initialization failed\n");
283 		goto err_out_release_ctlr;
284 	}
285 
286 	rc = shpchp_create_ctrl_files(ctrl);
287 	if (rc)
288 		goto err_cleanup_slots;
289 
290 	pdev->shpc_managed = 1;
291 	return 0;
292 
293 err_cleanup_slots:
294 	cleanup_slots(ctrl);
295 err_out_release_ctlr:
296 	ctrl->hpc_ops->release_ctlr(ctrl);
297 err_out_free_ctrl:
298 	kfree(ctrl);
299 err_out_none:
300 	return -ENODEV;
301 }
302 
shpc_remove(struct pci_dev * dev)303 static void shpc_remove(struct pci_dev *dev)
304 {
305 	struct controller *ctrl = pci_get_drvdata(dev);
306 
307 	dev->shpc_managed = 0;
308 	shpchp_remove_ctrl_files(ctrl);
309 	ctrl->hpc_ops->release_ctlr(ctrl);
310 	kfree(ctrl);
311 }
312 
313 static const struct pci_device_id shpcd_pci_tbl[] = {
314 	{PCI_DEVICE_CLASS(PCI_CLASS_BRIDGE_PCI_NORMAL, ~0)},
315 	{ /* end: all zeroes */ }
316 };
317 MODULE_DEVICE_TABLE(pci, shpcd_pci_tbl);
318 
319 static struct pci_driver shpc_driver = {
320 	.name =		SHPC_MODULE_NAME,
321 	.id_table =	shpcd_pci_tbl,
322 	.probe =	shpc_probe,
323 	.remove =	shpc_remove,
324 };
325 
shpcd_init(void)326 static int __init shpcd_init(void)
327 {
328 	int retval;
329 
330 	retval = pci_register_driver(&shpc_driver);
331 	dbg("%s: pci_register_driver = %d\n", __func__, retval);
332 	info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
333 
334 	return retval;
335 }
336 
shpcd_cleanup(void)337 static void __exit shpcd_cleanup(void)
338 {
339 	dbg("unload_shpchpd()\n");
340 	pci_unregister_driver(&shpc_driver);
341 	info(DRIVER_DESC " version: " DRIVER_VERSION " unloaded\n");
342 }
343 
344 module_init(shpcd_init);
345 module_exit(shpcd_cleanup);
346