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