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/smp_lock.h>
38 #include <linux/init.h>
39 #include <linux/mount.h>
40 #include <linux/namei.h>
41 #include <linux/pci.h>
42 #include <linux/pci_hotplug.h>
43 #include <asm/uaccess.h>
45 #define MY_NAME "pci_hotplug"
47 #define dbg(fmt, arg...) do { if (debug) printk(KERN_DEBUG "%s: %s: " fmt , MY_NAME , __FUNCTION__ , ## arg); } while (0)
48 #define err(format, arg...) printk(KERN_ERR "%s: " format , MY_NAME , ## arg)
49 #define info(format, arg...) printk(KERN_INFO "%s: " format , MY_NAME , ## arg)
50 #define warn(format, arg...) printk(KERN_WARNING "%s: " format , MY_NAME , ## arg)
56 #define DRIVER_VERSION "0.5"
57 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Scott Murray <scottm@somanetworks.com>"
58 #define DRIVER_DESC "PCI Hot Plug PCI Core"
61 //////////////////////////////////////////////////////////////////
63 static LIST_HEAD(pci_hotplug_slot_list);
65 struct subsystem pci_hotplug_slots_subsys;
67 static ssize_t hotplug_slot_attr_show(struct kobject *kobj,
68 struct attribute *attr, char *buf)
70 struct hotplug_slot *slot = to_hotplug_slot(kobj);
71 struct hotplug_slot_attribute *attribute = to_hotplug_attr(attr);
72 return attribute->show ? attribute->show(slot, buf) : -EIO;
75 static ssize_t hotplug_slot_attr_store(struct kobject *kobj,
76 struct attribute *attr, const char *buf, size_t len)
78 struct hotplug_slot *slot = to_hotplug_slot(kobj);
79 struct hotplug_slot_attribute *attribute = to_hotplug_attr(attr);
80 return attribute->store ? attribute->store(slot, buf, len) : -EIO;
83 static struct sysfs_ops hotplug_slot_sysfs_ops = {
84 .show = hotplug_slot_attr_show,
85 .store = hotplug_slot_attr_store,
88 static void hotplug_slot_release(struct kobject *kobj)
90 struct hotplug_slot *slot = to_hotplug_slot(kobj);
95 static struct kobj_type hotplug_slot_ktype = {
96 .sysfs_ops = &hotplug_slot_sysfs_ops,
97 .release = &hotplug_slot_release,
100 decl_subsys_name(pci_hotplug_slots, slots, &hotplug_slot_ktype, NULL);
102 /* these strings match up with the values in pci_bus_speed */
103 static char *pci_bus_speed_strings[] = {
104 "33 MHz PCI", /* 0x00 */
105 "66 MHz PCI", /* 0x01 */
106 "66 MHz PCIX", /* 0x02 */
107 "100 MHz PCIX", /* 0x03 */
108 "133 MHz PCIX", /* 0x04 */
113 "66 MHz PCIX 266", /* 0x09 */
114 "100 MHz PCIX 266", /* 0x0a */
115 "133 MHz PCIX 266", /* 0x0b */
121 "66 MHz PCIX 533", /* 0x11 */
122 "100 MHz PCIX 533", /* 0x12 */
123 "133 MHz PCIX 533", /* 0x13 */
124 "25 GBps PCI-E", /* 0x14 */
127 #ifdef CONFIG_HOTPLUG_PCI_CPCI
128 extern int cpci_hotplug_init(int debug);
129 extern void cpci_hotplug_exit(void);
131 static inline int cpci_hotplug_init(int debug) { return 0; }
132 static inline void cpci_hotplug_exit(void) { }
135 /* Weee, fun with macros... */
136 #define GET_STATUS(name,type) \
137 static int get_##name (struct hotplug_slot *slot, type *value) \
139 struct hotplug_slot_ops *ops = slot->ops; \
141 if (try_module_get(ops->owner)) { \
142 if (ops->get_##name) \
143 retval = ops->get_##name (slot, value); \
145 *value = slot->info->name; \
146 module_put(ops->owner); \
151 GET_STATUS(power_status, u8)
152 GET_STATUS(attention_status, u8)
153 GET_STATUS(latch_status, u8)
154 GET_STATUS(adapter_status, u8)
155 GET_STATUS(address, u32)
156 GET_STATUS(max_bus_speed, enum pci_bus_speed)
157 GET_STATUS(cur_bus_speed, enum pci_bus_speed)
159 static ssize_t power_read_file (struct hotplug_slot *slot, char *buf)
164 retval = get_power_status (slot, &value);
167 retval = sprintf (buf, "%d\n", value);
172 static ssize_t power_write_file (struct hotplug_slot *slot, const char *buf,
175 unsigned long lpower;
179 lpower = simple_strtoul (buf, NULL, 10);
180 power = (u8)(lpower & 0xff);
181 dbg ("power = %d\n", power);
183 if (!try_module_get(slot->ops->owner)) {
189 if (slot->ops->disable_slot)
190 retval = slot->ops->disable_slot(slot);
194 if (slot->ops->enable_slot)
195 retval = slot->ops->enable_slot(slot);
199 err ("Illegal value specified for power\n");
202 module_put(slot->ops->owner);
210 static struct hotplug_slot_attribute hotplug_slot_attr_power = {
211 .attr = {.name = "power", .mode = S_IFREG | S_IRUGO | S_IWUSR},
212 .show = power_read_file,
213 .store = power_write_file
216 static ssize_t attention_read_file (struct hotplug_slot *slot, char *buf)
221 retval = get_attention_status (slot, &value);
224 retval = sprintf (buf, "%d\n", value);
230 static ssize_t attention_write_file (struct hotplug_slot *slot, const char *buf,
233 unsigned long lattention;
237 lattention = simple_strtoul (buf, NULL, 10);
238 attention = (u8)(lattention & 0xff);
239 dbg (" - attention = %d\n", attention);
241 if (!try_module_get(slot->ops->owner)) {
245 if (slot->ops->set_attention_status)
246 retval = slot->ops->set_attention_status(slot, attention);
247 module_put(slot->ops->owner);
255 static struct hotplug_slot_attribute hotplug_slot_attr_attention = {
256 .attr = {.name = "attention", .mode = S_IFREG | S_IRUGO | S_IWUSR},
257 .show = attention_read_file,
258 .store = attention_write_file
261 static ssize_t latch_read_file (struct hotplug_slot *slot, char *buf)
266 retval = get_latch_status (slot, &value);
269 retval = sprintf (buf, "%d\n", value);
275 static struct hotplug_slot_attribute hotplug_slot_attr_latch = {
276 .attr = {.name = "latch", .mode = S_IFREG | S_IRUGO},
277 .show = latch_read_file,
280 static ssize_t presence_read_file (struct hotplug_slot *slot, char *buf)
285 retval = get_adapter_status (slot, &value);
288 retval = sprintf (buf, "%d\n", value);
294 static struct hotplug_slot_attribute hotplug_slot_attr_presence = {
295 .attr = {.name = "adapter", .mode = S_IFREG | S_IRUGO},
296 .show = presence_read_file,
299 static ssize_t address_read_file (struct hotplug_slot *slot, char *buf)
304 retval = get_address (slot, &address);
307 retval = sprintf (buf, "%04x:%02x:%02x\n",
308 (address >> 16) & 0xffff,
309 (address >> 8) & 0xff,
316 static struct hotplug_slot_attribute hotplug_slot_attr_address = {
317 .attr = {.name = "address", .mode = S_IFREG | S_IRUGO},
318 .show = address_read_file,
321 static char *unknown_speed = "Unknown bus speed";
323 static ssize_t max_bus_speed_read_file (struct hotplug_slot *slot, char *buf)
327 enum pci_bus_speed value;
329 retval = get_max_bus_speed (slot, &value);
333 if (value == PCI_SPEED_UNKNOWN)
334 speed_string = unknown_speed;
336 speed_string = pci_bus_speed_strings[value];
338 retval = sprintf (buf, "%s\n", speed_string);
344 static struct hotplug_slot_attribute hotplug_slot_attr_max_bus_speed = {
345 .attr = {.name = "max_bus_speed", .mode = S_IFREG | S_IRUGO},
346 .show = max_bus_speed_read_file,
349 static ssize_t cur_bus_speed_read_file (struct hotplug_slot *slot, char *buf)
353 enum pci_bus_speed value;
355 retval = get_cur_bus_speed (slot, &value);
359 if (value == PCI_SPEED_UNKNOWN)
360 speed_string = unknown_speed;
362 speed_string = pci_bus_speed_strings[value];
364 retval = sprintf (buf, "%s\n", speed_string);
370 static struct hotplug_slot_attribute hotplug_slot_attr_cur_bus_speed = {
371 .attr = {.name = "cur_bus_speed", .mode = S_IFREG | S_IRUGO},
372 .show = cur_bus_speed_read_file,
375 static ssize_t test_write_file (struct hotplug_slot *slot, const char *buf,
382 ltest = simple_strtoul (buf, NULL, 10);
383 test = (u32)(ltest & 0xffffffff);
384 dbg ("test = %d\n", test);
386 if (!try_module_get(slot->ops->owner)) {
390 if (slot->ops->hardware_test)
391 retval = slot->ops->hardware_test(slot, test);
392 module_put(slot->ops->owner);
400 static struct hotplug_slot_attribute hotplug_slot_attr_test = {
401 .attr = {.name = "test", .mode = S_IFREG | S_IRUGO | S_IWUSR},
402 .store = test_write_file
405 static int has_power_file (struct hotplug_slot *slot)
407 if ((!slot) || (!slot->ops))
409 if ((slot->ops->enable_slot) ||
410 (slot->ops->disable_slot) ||
411 (slot->ops->get_power_status))
416 static int has_attention_file (struct hotplug_slot *slot)
418 if ((!slot) || (!slot->ops))
420 if ((slot->ops->set_attention_status) ||
421 (slot->ops->get_attention_status))
426 static int has_latch_file (struct hotplug_slot *slot)
428 if ((!slot) || (!slot->ops))
430 if (slot->ops->get_latch_status)
435 static int has_adapter_file (struct hotplug_slot *slot)
437 if ((!slot) || (!slot->ops))
439 if (slot->ops->get_adapter_status)
444 static int has_address_file (struct hotplug_slot *slot)
446 if ((!slot) || (!slot->ops))
448 if (slot->ops->get_address)
453 static int has_max_bus_speed_file (struct hotplug_slot *slot)
455 if ((!slot) || (!slot->ops))
457 if (slot->ops->get_max_bus_speed)
462 static int has_cur_bus_speed_file (struct hotplug_slot *slot)
464 if ((!slot) || (!slot->ops))
466 if (slot->ops->get_cur_bus_speed)
471 static int has_test_file (struct hotplug_slot *slot)
473 if ((!slot) || (!slot->ops))
475 if (slot->ops->hardware_test)
480 static int fs_add_slot (struct hotplug_slot *slot)
484 if (has_power_file(slot) == 0) {
485 retval = sysfs_create_file(&slot->kobj, &hotplug_slot_attr_power.attr);
490 if (has_attention_file(slot) == 0) {
491 retval = sysfs_create_file(&slot->kobj,
492 &hotplug_slot_attr_attention.attr);
497 if (has_latch_file(slot) == 0) {
498 retval = sysfs_create_file(&slot->kobj,
499 &hotplug_slot_attr_latch.attr);
504 if (has_adapter_file(slot) == 0) {
505 retval = sysfs_create_file(&slot->kobj,
506 &hotplug_slot_attr_presence.attr);
511 if (has_address_file(slot) == 0) {
512 retval = sysfs_create_file(&slot->kobj,
513 &hotplug_slot_attr_address.attr);
518 if (has_max_bus_speed_file(slot) == 0) {
519 retval = sysfs_create_file(&slot->kobj,
520 &hotplug_slot_attr_max_bus_speed.attr);
525 if (has_cur_bus_speed_file(slot) == 0) {
526 retval = sysfs_create_file(&slot->kobj,
527 &hotplug_slot_attr_cur_bus_speed.attr);
532 if (has_test_file(slot) == 0) {
533 retval = sysfs_create_file(&slot->kobj,
534 &hotplug_slot_attr_test.attr);
542 if (has_cur_bus_speed_file(slot) == 0)
543 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_cur_bus_speed.attr);
546 if (has_max_bus_speed_file(slot) == 0)
547 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_max_bus_speed.attr);
550 if (has_address_file(slot) == 0)
551 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_address.attr);
554 if (has_adapter_file(slot) == 0)
555 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_presence.attr);
558 if (has_latch_file(slot) == 0)
559 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_latch.attr);
562 if (has_attention_file(slot) == 0)
563 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_attention.attr);
566 if (has_power_file(slot) == 0)
567 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_power.attr);
573 static void fs_remove_slot (struct hotplug_slot *slot)
575 if (has_power_file(slot) == 0)
576 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_power.attr);
578 if (has_attention_file(slot) == 0)
579 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_attention.attr);
581 if (has_latch_file(slot) == 0)
582 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_latch.attr);
584 if (has_adapter_file(slot) == 0)
585 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_presence.attr);
587 if (has_address_file(slot) == 0)
588 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_address.attr);
590 if (has_max_bus_speed_file(slot) == 0)
591 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_max_bus_speed.attr);
593 if (has_cur_bus_speed_file(slot) == 0)
594 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_cur_bus_speed.attr);
596 if (has_test_file(slot) == 0)
597 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_test.attr);
600 static struct hotplug_slot *get_slot_from_name (const char *name)
602 struct hotplug_slot *slot;
603 struct list_head *tmp;
605 list_for_each (tmp, &pci_hotplug_slot_list) {
606 slot = list_entry (tmp, struct hotplug_slot, slot_list);
607 if (strcmp(slot->name, name) == 0)
614 * pci_hp_register - register a hotplug_slot with the PCI hotplug subsystem
615 * @slot: pointer to the &struct hotplug_slot to register
617 * Registers a hotplug slot with the pci hotplug subsystem, which will allow
618 * userspace interaction to the slot.
620 * Returns 0 if successful, anything else for an error.
622 int pci_hp_register (struct hotplug_slot *slot)
628 if ((slot->info == NULL) || (slot->ops == NULL))
630 if (slot->release == NULL) {
631 dbg("Why are you trying to register a hotplug slot"
632 "without a proper release function?\n");
636 kobject_set_name(&slot->kobj, "%s", slot->name);
637 kobj_set_kset_s(slot, pci_hotplug_slots_subsys);
639 /* this can fail if we have already registered a slot with the same name */
640 if (kobject_register(&slot->kobj)) {
641 err("Unable to register kobject");
645 list_add (&slot->slot_list, &pci_hotplug_slot_list);
647 result = fs_add_slot (slot);
648 dbg ("Added slot %s to the list\n", slot->name);
653 * pci_hp_deregister - deregister a hotplug_slot with the PCI hotplug subsystem
654 * @slot: pointer to the &struct hotplug_slot to deregister
656 * The @slot must have been registered with the pci hotplug subsystem
657 * previously with a call to pci_hp_register().
659 * Returns 0 if successful, anything else for an error.
661 int pci_hp_deregister (struct hotplug_slot *slot)
663 struct hotplug_slot *temp;
668 temp = get_slot_from_name (slot->name);
672 list_del (&slot->slot_list);
674 fs_remove_slot (slot);
675 dbg ("Removed slot %s from the list\n", slot->name);
676 kobject_unregister(&slot->kobj);
681 * pci_hp_change_slot_info - changes the slot's information structure in the core
682 * @slot: pointer to the slot whose info has changed
683 * @info: pointer to the info copy into the slot's info structure
685 * @slot must have been registered with the pci
686 * hotplug subsystem previously with a call to pci_hp_register().
688 * Returns 0 if successful, anything else for an error.
690 int __must_check pci_hp_change_slot_info(struct hotplug_slot *slot,
691 struct hotplug_slot_info *info)
695 if ((slot == NULL) || (info == NULL))
699 * check all fields in the info structure, and update timestamps
700 * for the files referring to the fields that have now changed.
702 if ((has_power_file(slot) == 0) &&
703 (slot->info->power_status != info->power_status)) {
704 retval = sysfs_update_file(&slot->kobj,
705 &hotplug_slot_attr_power.attr);
710 if ((has_attention_file(slot) == 0) &&
711 (slot->info->attention_status != info->attention_status)) {
712 retval = sysfs_update_file(&slot->kobj,
713 &hotplug_slot_attr_attention.attr);
718 if ((has_latch_file(slot) == 0) &&
719 (slot->info->latch_status != info->latch_status)) {
720 retval = sysfs_update_file(&slot->kobj,
721 &hotplug_slot_attr_latch.attr);
726 if ((has_adapter_file(slot) == 0) &&
727 (slot->info->adapter_status != info->adapter_status)) {
728 retval = sysfs_update_file(&slot->kobj,
729 &hotplug_slot_attr_presence.attr);
734 if ((has_address_file(slot) == 0) &&
735 (slot->info->address != info->address)) {
736 retval = sysfs_update_file(&slot->kobj,
737 &hotplug_slot_attr_address.attr);
742 if ((has_max_bus_speed_file(slot) == 0) &&
743 (slot->info->max_bus_speed != info->max_bus_speed)) {
744 retval = sysfs_update_file(&slot->kobj,
745 &hotplug_slot_attr_max_bus_speed.attr);
750 if ((has_cur_bus_speed_file(slot) == 0) &&
751 (slot->info->cur_bus_speed != info->cur_bus_speed)) {
752 retval = sysfs_update_file(&slot->kobj,
753 &hotplug_slot_attr_cur_bus_speed.attr);
758 memcpy (slot->info, info, sizeof (struct hotplug_slot_info));
763 static int __init pci_hotplug_init (void)
767 kset_set_kset_s(&pci_hotplug_slots_subsys, pci_bus_type.subsys);
768 result = subsystem_register(&pci_hotplug_slots_subsys);
770 err("Register subsys with error %d\n", result);
773 result = cpci_hotplug_init(debug);
775 err ("cpci_hotplug_init with error %d\n", result);
779 info (DRIVER_DESC " version: " DRIVER_VERSION "\n");
783 subsystem_unregister(&pci_hotplug_slots_subsys);
788 static void __exit pci_hotplug_exit (void)
791 subsystem_unregister(&pci_hotplug_slots_subsys);
794 module_init(pci_hotplug_init);
795 module_exit(pci_hotplug_exit);
797 MODULE_AUTHOR(DRIVER_AUTHOR);
798 MODULE_DESCRIPTION(DRIVER_DESC);
799 MODULE_LICENSE("GPL");
800 module_param(debug, bool, 0644);
801 MODULE_PARM_DESC(debug, "Debugging mode enabled or not");
803 EXPORT_SYMBOL_GPL(pci_hotplug_slots_subsys);
804 EXPORT_SYMBOL_GPL(pci_hp_register);
805 EXPORT_SYMBOL_GPL(pci_hp_deregister);
806 EXPORT_SYMBOL_GPL(pci_hp_change_slot_info);