2 * PCI HotPlug Controller Core
4 * Copyright (C) 2001-2002 Greg Kroah-Hartman (greg@kroah.com)
5 * Copyright (C) 2001-2002 IBM Corp.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or (at
12 * your option) any later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
17 * NON INFRINGEMENT. See the GNU General Public License for more
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 * Send feedback to <kristen.c.accardi@intel.com>
28 #include <linux/module.h>
29 #include <linux/moduleparam.h>
30 #include <linux/kernel.h>
31 #include <linux/types.h>
32 #include <linux/list.h>
33 #include <linux/kobject.h>
34 #include <linux/sysfs.h>
35 #include <linux/pagemap.h>
36 #include <linux/slab.h>
37 #include <linux/init.h>
38 #include <linux/mount.h>
39 #include <linux/namei.h>
40 #include <linux/pci.h>
41 #include <linux/pci_hotplug.h>
42 #include <asm/uaccess.h>
44 #define MY_NAME "pci_hotplug"
46 #define dbg(fmt, arg...) do { if (debug) printk(KERN_DEBUG "%s: %s: " fmt , MY_NAME , __FUNCTION__ , ## arg); } while (0)
47 #define err(format, arg...) printk(KERN_ERR "%s: " format , MY_NAME , ## arg)
48 #define info(format, arg...) printk(KERN_INFO "%s: " format , MY_NAME , ## arg)
49 #define warn(format, arg...) printk(KERN_WARNING "%s: " format , MY_NAME , ## arg)
55 #define DRIVER_VERSION "0.5"
56 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Scott Murray <scottm@somanetworks.com>"
57 #define DRIVER_DESC "PCI Hot Plug PCI Core"
60 //////////////////////////////////////////////////////////////////
62 static LIST_HEAD(pci_hotplug_slot_list);
64 struct kset pci_hotplug_slots_subsys;
66 static ssize_t hotplug_slot_attr_show(struct kobject *kobj,
67 struct attribute *attr, char *buf)
69 struct hotplug_slot *slot = to_hotplug_slot(kobj);
70 struct hotplug_slot_attribute *attribute = to_hotplug_attr(attr);
71 return attribute->show ? attribute->show(slot, buf) : -EIO;
74 static ssize_t hotplug_slot_attr_store(struct kobject *kobj,
75 struct attribute *attr, const char *buf, size_t len)
77 struct hotplug_slot *slot = to_hotplug_slot(kobj);
78 struct hotplug_slot_attribute *attribute = to_hotplug_attr(attr);
79 return attribute->store ? attribute->store(slot, buf, len) : -EIO;
82 static struct sysfs_ops hotplug_slot_sysfs_ops = {
83 .show = hotplug_slot_attr_show,
84 .store = hotplug_slot_attr_store,
87 static void hotplug_slot_release(struct kobject *kobj)
89 struct hotplug_slot *slot = to_hotplug_slot(kobj);
94 static struct kobj_type hotplug_slot_ktype = {
95 .sysfs_ops = &hotplug_slot_sysfs_ops,
96 .release = &hotplug_slot_release,
99 decl_subsys_name(pci_hotplug_slots, slots, &hotplug_slot_ktype, NULL);
101 /* these strings match up with the values in pci_bus_speed */
102 static char *pci_bus_speed_strings[] = {
103 "33 MHz PCI", /* 0x00 */
104 "66 MHz PCI", /* 0x01 */
105 "66 MHz PCIX", /* 0x02 */
106 "100 MHz PCIX", /* 0x03 */
107 "133 MHz PCIX", /* 0x04 */
112 "66 MHz PCIX 266", /* 0x09 */
113 "100 MHz PCIX 266", /* 0x0a */
114 "133 MHz PCIX 266", /* 0x0b */
120 "66 MHz PCIX 533", /* 0x11 */
121 "100 MHz PCIX 533", /* 0x12 */
122 "133 MHz PCIX 533", /* 0x13 */
123 "25 GBps PCI-E", /* 0x14 */
126 #ifdef CONFIG_HOTPLUG_PCI_CPCI
127 extern int cpci_hotplug_init(int debug);
128 extern void cpci_hotplug_exit(void);
130 static inline int cpci_hotplug_init(int debug) { return 0; }
131 static inline void cpci_hotplug_exit(void) { }
134 /* Weee, fun with macros... */
135 #define GET_STATUS(name,type) \
136 static int get_##name (struct hotplug_slot *slot, type *value) \
138 struct hotplug_slot_ops *ops = slot->ops; \
140 if (try_module_get(ops->owner)) { \
141 if (ops->get_##name) \
142 retval = ops->get_##name (slot, value); \
144 *value = slot->info->name; \
145 module_put(ops->owner); \
150 GET_STATUS(power_status, u8)
151 GET_STATUS(attention_status, u8)
152 GET_STATUS(latch_status, u8)
153 GET_STATUS(adapter_status, u8)
154 GET_STATUS(address, u32)
155 GET_STATUS(max_bus_speed, enum pci_bus_speed)
156 GET_STATUS(cur_bus_speed, enum pci_bus_speed)
158 static ssize_t power_read_file (struct hotplug_slot *slot, char *buf)
163 retval = get_power_status (slot, &value);
166 retval = sprintf (buf, "%d\n", value);
171 static ssize_t power_write_file (struct hotplug_slot *slot, const char *buf,
174 unsigned long lpower;
178 lpower = simple_strtoul (buf, NULL, 10);
179 power = (u8)(lpower & 0xff);
180 dbg ("power = %d\n", power);
182 if (!try_module_get(slot->ops->owner)) {
188 if (slot->ops->disable_slot)
189 retval = slot->ops->disable_slot(slot);
193 if (slot->ops->enable_slot)
194 retval = slot->ops->enable_slot(slot);
198 err ("Illegal value specified for power\n");
201 module_put(slot->ops->owner);
209 static struct hotplug_slot_attribute hotplug_slot_attr_power = {
210 .attr = {.name = "power", .mode = S_IFREG | S_IRUGO | S_IWUSR},
211 .show = power_read_file,
212 .store = power_write_file
215 static ssize_t attention_read_file (struct hotplug_slot *slot, char *buf)
220 retval = get_attention_status (slot, &value);
223 retval = sprintf (buf, "%d\n", value);
229 static ssize_t attention_write_file (struct hotplug_slot *slot, const char *buf,
232 unsigned long lattention;
236 lattention = simple_strtoul (buf, NULL, 10);
237 attention = (u8)(lattention & 0xff);
238 dbg (" - attention = %d\n", attention);
240 if (!try_module_get(slot->ops->owner)) {
244 if (slot->ops->set_attention_status)
245 retval = slot->ops->set_attention_status(slot, attention);
246 module_put(slot->ops->owner);
254 static struct hotplug_slot_attribute hotplug_slot_attr_attention = {
255 .attr = {.name = "attention", .mode = S_IFREG | S_IRUGO | S_IWUSR},
256 .show = attention_read_file,
257 .store = attention_write_file
260 static ssize_t latch_read_file (struct hotplug_slot *slot, char *buf)
265 retval = get_latch_status (slot, &value);
268 retval = sprintf (buf, "%d\n", value);
274 static struct hotplug_slot_attribute hotplug_slot_attr_latch = {
275 .attr = {.name = "latch", .mode = S_IFREG | S_IRUGO},
276 .show = latch_read_file,
279 static ssize_t presence_read_file (struct hotplug_slot *slot, char *buf)
284 retval = get_adapter_status (slot, &value);
287 retval = sprintf (buf, "%d\n", value);
293 static struct hotplug_slot_attribute hotplug_slot_attr_presence = {
294 .attr = {.name = "adapter", .mode = S_IFREG | S_IRUGO},
295 .show = presence_read_file,
298 static ssize_t address_read_file (struct hotplug_slot *slot, char *buf)
303 retval = get_address (slot, &address);
306 retval = sprintf (buf, "%04x:%02x:%02x\n",
307 (address >> 16) & 0xffff,
308 (address >> 8) & 0xff,
315 static struct hotplug_slot_attribute hotplug_slot_attr_address = {
316 .attr = {.name = "address", .mode = S_IFREG | S_IRUGO},
317 .show = address_read_file,
320 static char *unknown_speed = "Unknown bus speed";
322 static ssize_t max_bus_speed_read_file (struct hotplug_slot *slot, char *buf)
326 enum pci_bus_speed value;
328 retval = get_max_bus_speed (slot, &value);
332 if (value == PCI_SPEED_UNKNOWN)
333 speed_string = unknown_speed;
335 speed_string = pci_bus_speed_strings[value];
337 retval = sprintf (buf, "%s\n", speed_string);
343 static struct hotplug_slot_attribute hotplug_slot_attr_max_bus_speed = {
344 .attr = {.name = "max_bus_speed", .mode = S_IFREG | S_IRUGO},
345 .show = max_bus_speed_read_file,
348 static ssize_t cur_bus_speed_read_file (struct hotplug_slot *slot, char *buf)
352 enum pci_bus_speed value;
354 retval = get_cur_bus_speed (slot, &value);
358 if (value == PCI_SPEED_UNKNOWN)
359 speed_string = unknown_speed;
361 speed_string = pci_bus_speed_strings[value];
363 retval = sprintf (buf, "%s\n", speed_string);
369 static struct hotplug_slot_attribute hotplug_slot_attr_cur_bus_speed = {
370 .attr = {.name = "cur_bus_speed", .mode = S_IFREG | S_IRUGO},
371 .show = cur_bus_speed_read_file,
374 static ssize_t test_write_file (struct hotplug_slot *slot, const char *buf,
381 ltest = simple_strtoul (buf, NULL, 10);
382 test = (u32)(ltest & 0xffffffff);
383 dbg ("test = %d\n", test);
385 if (!try_module_get(slot->ops->owner)) {
389 if (slot->ops->hardware_test)
390 retval = slot->ops->hardware_test(slot, test);
391 module_put(slot->ops->owner);
399 static struct hotplug_slot_attribute hotplug_slot_attr_test = {
400 .attr = {.name = "test", .mode = S_IFREG | S_IRUGO | S_IWUSR},
401 .store = test_write_file
404 static int has_power_file (struct hotplug_slot *slot)
406 if ((!slot) || (!slot->ops))
408 if ((slot->ops->enable_slot) ||
409 (slot->ops->disable_slot) ||
410 (slot->ops->get_power_status))
415 static int has_attention_file (struct hotplug_slot *slot)
417 if ((!slot) || (!slot->ops))
419 if ((slot->ops->set_attention_status) ||
420 (slot->ops->get_attention_status))
425 static int has_latch_file (struct hotplug_slot *slot)
427 if ((!slot) || (!slot->ops))
429 if (slot->ops->get_latch_status)
434 static int has_adapter_file (struct hotplug_slot *slot)
436 if ((!slot) || (!slot->ops))
438 if (slot->ops->get_adapter_status)
443 static int has_address_file (struct hotplug_slot *slot)
445 if ((!slot) || (!slot->ops))
447 if (slot->ops->get_address)
452 static int has_max_bus_speed_file (struct hotplug_slot *slot)
454 if ((!slot) || (!slot->ops))
456 if (slot->ops->get_max_bus_speed)
461 static int has_cur_bus_speed_file (struct hotplug_slot *slot)
463 if ((!slot) || (!slot->ops))
465 if (slot->ops->get_cur_bus_speed)
470 static int has_test_file (struct hotplug_slot *slot)
472 if ((!slot) || (!slot->ops))
474 if (slot->ops->hardware_test)
479 static int fs_add_slot (struct hotplug_slot *slot)
483 if (has_power_file(slot) == 0) {
484 retval = sysfs_create_file(&slot->kobj, &hotplug_slot_attr_power.attr);
489 if (has_attention_file(slot) == 0) {
490 retval = sysfs_create_file(&slot->kobj,
491 &hotplug_slot_attr_attention.attr);
496 if (has_latch_file(slot) == 0) {
497 retval = sysfs_create_file(&slot->kobj,
498 &hotplug_slot_attr_latch.attr);
503 if (has_adapter_file(slot) == 0) {
504 retval = sysfs_create_file(&slot->kobj,
505 &hotplug_slot_attr_presence.attr);
510 if (has_address_file(slot) == 0) {
511 retval = sysfs_create_file(&slot->kobj,
512 &hotplug_slot_attr_address.attr);
517 if (has_max_bus_speed_file(slot) == 0) {
518 retval = sysfs_create_file(&slot->kobj,
519 &hotplug_slot_attr_max_bus_speed.attr);
524 if (has_cur_bus_speed_file(slot) == 0) {
525 retval = sysfs_create_file(&slot->kobj,
526 &hotplug_slot_attr_cur_bus_speed.attr);
531 if (has_test_file(slot) == 0) {
532 retval = sysfs_create_file(&slot->kobj,
533 &hotplug_slot_attr_test.attr);
541 if (has_cur_bus_speed_file(slot) == 0)
542 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_cur_bus_speed.attr);
545 if (has_max_bus_speed_file(slot) == 0)
546 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_max_bus_speed.attr);
549 if (has_address_file(slot) == 0)
550 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_address.attr);
553 if (has_adapter_file(slot) == 0)
554 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_presence.attr);
557 if (has_latch_file(slot) == 0)
558 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_latch.attr);
561 if (has_attention_file(slot) == 0)
562 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_attention.attr);
565 if (has_power_file(slot) == 0)
566 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_power.attr);
572 static void fs_remove_slot (struct hotplug_slot *slot)
574 if (has_power_file(slot) == 0)
575 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_power.attr);
577 if (has_attention_file(slot) == 0)
578 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_attention.attr);
580 if (has_latch_file(slot) == 0)
581 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_latch.attr);
583 if (has_adapter_file(slot) == 0)
584 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_presence.attr);
586 if (has_address_file(slot) == 0)
587 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_address.attr);
589 if (has_max_bus_speed_file(slot) == 0)
590 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_max_bus_speed.attr);
592 if (has_cur_bus_speed_file(slot) == 0)
593 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_cur_bus_speed.attr);
595 if (has_test_file(slot) == 0)
596 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_test.attr);
599 static struct hotplug_slot *get_slot_from_name (const char *name)
601 struct hotplug_slot *slot;
602 struct list_head *tmp;
604 list_for_each (tmp, &pci_hotplug_slot_list) {
605 slot = list_entry (tmp, struct hotplug_slot, slot_list);
606 if (strcmp(slot->name, name) == 0)
613 * pci_hp_register - register a hotplug_slot with the PCI hotplug subsystem
614 * @slot: pointer to the &struct hotplug_slot to register
616 * Registers a hotplug slot with the pci hotplug subsystem, which will allow
617 * userspace interaction to the slot.
619 * Returns 0 if successful, anything else for an error.
621 int pci_hp_register (struct hotplug_slot *slot)
627 if ((slot->info == NULL) || (slot->ops == NULL))
629 if (slot->release == NULL) {
630 dbg("Why are you trying to register a hotplug slot"
631 "without a proper release function?\n");
635 kobject_set_name(&slot->kobj, "%s", slot->name);
636 kobj_set_kset_s(slot, pci_hotplug_slots_subsys);
638 /* this can fail if we have already registered a slot with the same name */
639 if (kobject_register(&slot->kobj)) {
640 err("Unable to register kobject");
644 list_add (&slot->slot_list, &pci_hotplug_slot_list);
646 result = fs_add_slot (slot);
647 dbg ("Added slot %s to the list\n", slot->name);
652 * pci_hp_deregister - deregister a hotplug_slot with the PCI hotplug subsystem
653 * @slot: pointer to the &struct hotplug_slot to deregister
655 * The @slot must have been registered with the pci hotplug subsystem
656 * previously with a call to pci_hp_register().
658 * Returns 0 if successful, anything else for an error.
660 int pci_hp_deregister (struct hotplug_slot *slot)
662 struct hotplug_slot *temp;
667 temp = get_slot_from_name (slot->name);
671 list_del (&slot->slot_list);
673 fs_remove_slot (slot);
674 dbg ("Removed slot %s from the list\n", slot->name);
675 kobject_unregister(&slot->kobj);
680 * pci_hp_change_slot_info - changes the slot's information structure in the core
681 * @slot: pointer to the slot whose info has changed
682 * @info: pointer to the info copy into the slot's info structure
684 * @slot must have been registered with the pci
685 * hotplug subsystem previously with a call to pci_hp_register().
687 * Returns 0 if successful, anything else for an error.
689 int __must_check pci_hp_change_slot_info(struct hotplug_slot *slot,
690 struct hotplug_slot_info *info)
692 if ((slot == NULL) || (info == NULL))
695 memcpy (slot->info, info, sizeof (struct hotplug_slot_info));
700 static int __init pci_hotplug_init (void)
704 kobj_set_kset_s(&pci_hotplug_slots_subsys, pci_bus_type.subsys);
705 result = subsystem_register(&pci_hotplug_slots_subsys);
707 err("Register subsys with error %d\n", result);
710 result = cpci_hotplug_init(debug);
712 err ("cpci_hotplug_init with error %d\n", result);
716 info (DRIVER_DESC " version: " DRIVER_VERSION "\n");
720 subsystem_unregister(&pci_hotplug_slots_subsys);
725 static void __exit pci_hotplug_exit (void)
728 subsystem_unregister(&pci_hotplug_slots_subsys);
731 module_init(pci_hotplug_init);
732 module_exit(pci_hotplug_exit);
734 MODULE_AUTHOR(DRIVER_AUTHOR);
735 MODULE_DESCRIPTION(DRIVER_DESC);
736 MODULE_LICENSE("GPL");
737 module_param(debug, bool, 0644);
738 MODULE_PARM_DESC(debug, "Debugging mode enabled or not");
740 EXPORT_SYMBOL_GPL(pci_hotplug_slots_subsys);
741 EXPORT_SYMBOL_GPL(pci_hp_register);
742 EXPORT_SYMBOL_GPL(pci_hp_deregister);
743 EXPORT_SYMBOL_GPL(pci_hp_change_slot_info);