1 // SPDX-License-Identifier: GPL-2.0+
3 * PCI HotPlug Controller Core
6 * Copyright (C) 2001-2002 IBM Corp.
17 #include <linux/module.h> /* try_module_get & module_put */
18 #include <linux/moduleparam.h>
19 #include <linux/kernel.h>
20 #include <linux/types.h>
21 #include <linux/list.h>
22 #include <linux/kobject.h>
23 #include <linux/sysfs.h>
24 #include <linux/pagemap.h>
25 #include <linux/init.h>
26 #include <linux/mount.h>
27 #include <linux/namei.h>
28 #include <linux/mutex.h>
29 #include <linux/pci.h>
30 #include <linux/pci_hotplug.h>
31 #include <linux/uaccess.h>
33 #include "cpci_hotplug.h"
35 #define MY_NAME "pci_hotplug"
37 #define dbg(fmt, arg...) do { if (debug) printk(KERN_DEBUG "%s: %s: " fmt, MY_NAME, __func__, ## arg); } while (0)
38 #define err(format, arg...) printk(KERN_ERR "%s: " format, MY_NAME, ## arg)
39 #define info(format, arg...) printk(KERN_INFO "%s: " format, MY_NAME, ## arg)
40 #define warn(format, arg...) printk(KERN_WARNING "%s: " format, MY_NAME, ## arg)
45 static LIST_HEAD(pci_hotplug_slot_list);
46 static DEFINE_MUTEX(pci_hp_mutex);
48 /* Weee, fun with macros... */
49 #define GET_STATUS(name, type) \
50 static int get_##name(struct hotplug_slot *slot, type *value) \
52 const struct hotplug_slot_ops *ops = slot->ops; \
54 if (!try_module_get(slot->owner)) \
56 if (ops->get_##name) \
57 retval = ops->get_##name(slot, value); \
58 module_put(slot->owner); \
62 GET_STATUS(power_status, u8)
63 GET_STATUS(attention_status, u8)
64 GET_STATUS(latch_status, u8)
65 GET_STATUS(adapter_status, u8)
67 static ssize_t power_read_file(struct pci_slot *pci_slot, char *buf)
72 retval = get_power_status(pci_slot->hotplug, &value);
76 return sprintf(buf, "%d\n", value);
79 static ssize_t power_write_file(struct pci_slot *pci_slot, const char *buf,
82 struct hotplug_slot *slot = pci_slot->hotplug;
87 lpower = simple_strtoul(buf, NULL, 10);
88 power = (u8)(lpower & 0xff);
89 dbg("power = %d\n", power);
91 if (!try_module_get(slot->owner)) {
97 if (slot->ops->disable_slot)
98 retval = slot->ops->disable_slot(slot);
102 if (slot->ops->enable_slot)
103 retval = slot->ops->enable_slot(slot);
107 err("Illegal value specified for power\n");
110 module_put(slot->owner);
118 static struct pci_slot_attribute hotplug_slot_attr_power = {
119 .attr = {.name = "power", .mode = S_IFREG | S_IRUGO | S_IWUSR},
120 .show = power_read_file,
121 .store = power_write_file
124 static ssize_t attention_read_file(struct pci_slot *pci_slot, char *buf)
129 retval = get_attention_status(pci_slot->hotplug, &value);
133 return sprintf(buf, "%d\n", value);
136 static ssize_t attention_write_file(struct pci_slot *pci_slot, const char *buf,
139 struct hotplug_slot *slot = pci_slot->hotplug;
140 const struct hotplug_slot_ops *ops = slot->ops;
141 unsigned long lattention;
145 lattention = simple_strtoul(buf, NULL, 10);
146 attention = (u8)(lattention & 0xff);
147 dbg(" - attention = %d\n", attention);
149 if (!try_module_get(slot->owner)) {
153 if (ops->set_attention_status)
154 retval = ops->set_attention_status(slot, attention);
155 module_put(slot->owner);
163 static struct pci_slot_attribute hotplug_slot_attr_attention = {
164 .attr = {.name = "attention", .mode = S_IFREG | S_IRUGO | S_IWUSR},
165 .show = attention_read_file,
166 .store = attention_write_file
169 static ssize_t latch_read_file(struct pci_slot *pci_slot, char *buf)
174 retval = get_latch_status(pci_slot->hotplug, &value);
178 return sprintf(buf, "%d\n", value);
181 static struct pci_slot_attribute hotplug_slot_attr_latch = {
182 .attr = {.name = "latch", .mode = S_IFREG | S_IRUGO},
183 .show = latch_read_file,
186 static ssize_t presence_read_file(struct pci_slot *pci_slot, char *buf)
191 retval = get_adapter_status(pci_slot->hotplug, &value);
195 return sprintf(buf, "%d\n", value);
198 static struct pci_slot_attribute hotplug_slot_attr_presence = {
199 .attr = {.name = "adapter", .mode = S_IFREG | S_IRUGO},
200 .show = presence_read_file,
203 static ssize_t test_write_file(struct pci_slot *pci_slot, const char *buf,
206 struct hotplug_slot *slot = pci_slot->hotplug;
211 ltest = simple_strtoul(buf, NULL, 10);
212 test = (u32)(ltest & 0xffffffff);
213 dbg("test = %d\n", test);
215 if (!try_module_get(slot->owner)) {
219 if (slot->ops->hardware_test)
220 retval = slot->ops->hardware_test(slot, test);
221 module_put(slot->owner);
229 static struct pci_slot_attribute hotplug_slot_attr_test = {
230 .attr = {.name = "test", .mode = S_IFREG | S_IRUGO | S_IWUSR},
231 .store = test_write_file
234 static bool has_power_file(struct pci_slot *pci_slot)
236 struct hotplug_slot *slot = pci_slot->hotplug;
238 if ((!slot) || (!slot->ops))
240 if ((slot->ops->enable_slot) ||
241 (slot->ops->disable_slot) ||
242 (slot->ops->get_power_status))
247 static bool has_attention_file(struct pci_slot *pci_slot)
249 struct hotplug_slot *slot = pci_slot->hotplug;
251 if ((!slot) || (!slot->ops))
253 if ((slot->ops->set_attention_status) ||
254 (slot->ops->get_attention_status))
259 static bool has_latch_file(struct pci_slot *pci_slot)
261 struct hotplug_slot *slot = pci_slot->hotplug;
263 if ((!slot) || (!slot->ops))
265 if (slot->ops->get_latch_status)
270 static bool has_adapter_file(struct pci_slot *pci_slot)
272 struct hotplug_slot *slot = pci_slot->hotplug;
274 if ((!slot) || (!slot->ops))
276 if (slot->ops->get_adapter_status)
281 static bool has_test_file(struct pci_slot *pci_slot)
283 struct hotplug_slot *slot = pci_slot->hotplug;
285 if ((!slot) || (!slot->ops))
287 if (slot->ops->hardware_test)
292 static int fs_add_slot(struct pci_slot *pci_slot)
296 /* Create symbolic link to the hotplug driver module */
297 pci_hp_create_module_link(pci_slot);
299 if (has_power_file(pci_slot)) {
300 retval = sysfs_create_file(&pci_slot->kobj,
301 &hotplug_slot_attr_power.attr);
306 if (has_attention_file(pci_slot)) {
307 retval = sysfs_create_file(&pci_slot->kobj,
308 &hotplug_slot_attr_attention.attr);
313 if (has_latch_file(pci_slot)) {
314 retval = sysfs_create_file(&pci_slot->kobj,
315 &hotplug_slot_attr_latch.attr);
320 if (has_adapter_file(pci_slot)) {
321 retval = sysfs_create_file(&pci_slot->kobj,
322 &hotplug_slot_attr_presence.attr);
327 if (has_test_file(pci_slot)) {
328 retval = sysfs_create_file(&pci_slot->kobj,
329 &hotplug_slot_attr_test.attr);
337 if (has_adapter_file(pci_slot))
338 sysfs_remove_file(&pci_slot->kobj,
339 &hotplug_slot_attr_presence.attr);
341 if (has_latch_file(pci_slot))
342 sysfs_remove_file(&pci_slot->kobj, &hotplug_slot_attr_latch.attr);
344 if (has_attention_file(pci_slot))
345 sysfs_remove_file(&pci_slot->kobj,
346 &hotplug_slot_attr_attention.attr);
348 if (has_power_file(pci_slot))
349 sysfs_remove_file(&pci_slot->kobj, &hotplug_slot_attr_power.attr);
351 pci_hp_remove_module_link(pci_slot);
356 static void fs_remove_slot(struct pci_slot *pci_slot)
358 if (has_power_file(pci_slot))
359 sysfs_remove_file(&pci_slot->kobj, &hotplug_slot_attr_power.attr);
361 if (has_attention_file(pci_slot))
362 sysfs_remove_file(&pci_slot->kobj,
363 &hotplug_slot_attr_attention.attr);
365 if (has_latch_file(pci_slot))
366 sysfs_remove_file(&pci_slot->kobj, &hotplug_slot_attr_latch.attr);
368 if (has_adapter_file(pci_slot))
369 sysfs_remove_file(&pci_slot->kobj,
370 &hotplug_slot_attr_presence.attr);
372 if (has_test_file(pci_slot))
373 sysfs_remove_file(&pci_slot->kobj, &hotplug_slot_attr_test.attr);
375 pci_hp_remove_module_link(pci_slot);
378 static struct hotplug_slot *get_slot_from_name(const char *name)
380 struct hotplug_slot *slot;
382 list_for_each_entry(slot, &pci_hotplug_slot_list, slot_list) {
383 if (strcmp(hotplug_slot_name(slot), name) == 0)
390 * __pci_hp_register - register a hotplug_slot with the PCI hotplug subsystem
391 * @bus: bus this slot is on
392 * @slot: pointer to the &struct hotplug_slot to register
393 * @devnr: device number
394 * @name: name registered with kobject core
395 * @owner: caller module owner
396 * @mod_name: caller module name
398 * Prepares a hotplug slot for in-kernel use and immediately publishes it to
399 * user space in one go. Drivers may alternatively carry out the two steps
400 * separately by invoking pci_hp_initialize() and pci_hp_add().
402 * Returns 0 if successful, anything else for an error.
404 int __pci_hp_register(struct hotplug_slot *slot, struct pci_bus *bus,
405 int devnr, const char *name,
406 struct module *owner, const char *mod_name)
410 result = __pci_hp_initialize(slot, bus, devnr, name, owner, mod_name);
414 result = pci_hp_add(slot);
416 pci_hp_destroy(slot);
420 EXPORT_SYMBOL_GPL(__pci_hp_register);
423 * __pci_hp_initialize - prepare hotplug slot for in-kernel use
424 * @slot: pointer to the &struct hotplug_slot to initialize
425 * @bus: bus this slot is on
426 * @devnr: slot number
427 * @name: name registered with kobject core
428 * @owner: caller module owner
429 * @mod_name: caller module name
431 * Allocate and fill in a PCI slot for use by a hotplug driver. Once this has
432 * been called, the driver may invoke hotplug_slot_name() to get the slot's
433 * unique name. The driver must be prepared to handle a ->reset_slot callback
434 * from this point on.
436 * Returns 0 on success or a negative int on error.
438 int __pci_hp_initialize(struct hotplug_slot *slot, struct pci_bus *bus,
439 int devnr, const char *name, struct module *owner,
440 const char *mod_name)
442 struct pci_slot *pci_slot;
446 if (slot->ops == NULL)
450 slot->mod_name = mod_name;
453 * No problems if we call this interface from both ACPI_PCI_SLOT
454 * driver and call it here again. If we've already created the
455 * pci_slot, the interface will simply bump the refcount.
457 pci_slot = pci_create_slot(bus, devnr, name, slot);
458 if (IS_ERR(pci_slot))
459 return PTR_ERR(pci_slot);
461 slot->pci_slot = pci_slot;
462 pci_slot->hotplug = slot;
465 EXPORT_SYMBOL_GPL(__pci_hp_initialize);
468 * pci_hp_add - publish hotplug slot to user space
469 * @slot: pointer to the &struct hotplug_slot to publish
471 * Make a hotplug slot's sysfs interface available and inform user space of its
472 * addition by sending a uevent. The hotplug driver must be prepared to handle
473 * all &struct hotplug_slot_ops callbacks from this point on.
475 * Returns 0 on success or a negative int on error.
477 int pci_hp_add(struct hotplug_slot *slot)
479 struct pci_slot *pci_slot = slot->pci_slot;
482 result = fs_add_slot(pci_slot);
486 kobject_uevent(&pci_slot->kobj, KOBJ_ADD);
487 mutex_lock(&pci_hp_mutex);
488 list_add(&slot->slot_list, &pci_hotplug_slot_list);
489 mutex_unlock(&pci_hp_mutex);
490 dbg("Added slot %s to the list\n", hotplug_slot_name(slot));
493 EXPORT_SYMBOL_GPL(pci_hp_add);
496 * pci_hp_deregister - deregister a hotplug_slot with the PCI hotplug subsystem
497 * @slot: pointer to the &struct hotplug_slot to deregister
499 * The @slot must have been registered with the pci hotplug subsystem
500 * previously with a call to pci_hp_register().
502 * Returns 0 if successful, anything else for an error.
504 void pci_hp_deregister(struct hotplug_slot *slot)
507 pci_hp_destroy(slot);
509 EXPORT_SYMBOL_GPL(pci_hp_deregister);
512 * pci_hp_del - unpublish hotplug slot from user space
513 * @slot: pointer to the &struct hotplug_slot to unpublish
515 * Remove a hotplug slot's sysfs interface.
517 * Returns 0 on success or a negative int on error.
519 void pci_hp_del(struct hotplug_slot *slot)
521 struct hotplug_slot *temp;
526 mutex_lock(&pci_hp_mutex);
527 temp = get_slot_from_name(hotplug_slot_name(slot));
528 if (WARN_ON(temp != slot)) {
529 mutex_unlock(&pci_hp_mutex);
533 list_del(&slot->slot_list);
534 mutex_unlock(&pci_hp_mutex);
535 dbg("Removed slot %s from the list\n", hotplug_slot_name(slot));
536 fs_remove_slot(slot->pci_slot);
538 EXPORT_SYMBOL_GPL(pci_hp_del);
541 * pci_hp_destroy - remove hotplug slot from in-kernel use
542 * @slot: pointer to the &struct hotplug_slot to destroy
544 * Destroy a PCI slot used by a hotplug driver. Once this has been called,
545 * the driver may no longer invoke hotplug_slot_name() to get the slot's
546 * unique name. The driver no longer needs to handle a ->reset_slot callback
547 * from this point on.
549 * Returns 0 on success or a negative int on error.
551 void pci_hp_destroy(struct hotplug_slot *slot)
553 struct pci_slot *pci_slot = slot->pci_slot;
555 slot->pci_slot = NULL;
556 pci_slot->hotplug = NULL;
557 pci_destroy_slot(pci_slot);
559 EXPORT_SYMBOL_GPL(pci_hp_destroy);
561 static int __init pci_hotplug_init(void)
565 result = cpci_hotplug_init(debug);
567 err("cpci_hotplug_init with error %d\n", result);
573 device_initcall(pci_hotplug_init);
576 * not really modular, but the easiest way to keep compat with existing
577 * bootargs behaviour is to continue using module_param here.
579 module_param(debug, bool, 0644);
580 MODULE_PARM_DESC(debug, "Debugging mode enabled or not");