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 , __func__ , ## 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_kset;
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 /* these strings match up with the values in pci_bus_speed */
100 static char *pci_bus_speed_strings[] = {
101 "33 MHz PCI", /* 0x00 */
102 "66 MHz PCI", /* 0x01 */
103 "66 MHz PCIX", /* 0x02 */
104 "100 MHz PCIX", /* 0x03 */
105 "133 MHz PCIX", /* 0x04 */
110 "66 MHz PCIX 266", /* 0x09 */
111 "100 MHz PCIX 266", /* 0x0a */
112 "133 MHz PCIX 266", /* 0x0b */
118 "66 MHz PCIX 533", /* 0x11 */
119 "100 MHz PCIX 533", /* 0x12 */
120 "133 MHz PCIX 533", /* 0x13 */
121 "25 GBps PCI-E", /* 0x14 */
124 #ifdef CONFIG_HOTPLUG_PCI_CPCI
125 extern int cpci_hotplug_init(int debug);
126 extern void cpci_hotplug_exit(void);
128 static inline int cpci_hotplug_init(int debug) { return 0; }
129 static inline void cpci_hotplug_exit(void) { }
132 /* Weee, fun with macros... */
133 #define GET_STATUS(name,type) \
134 static int get_##name (struct hotplug_slot *slot, type *value) \
136 struct hotplug_slot_ops *ops = slot->ops; \
138 if (try_module_get(ops->owner)) { \
139 if (ops->get_##name) \
140 retval = ops->get_##name(slot, value); \
142 *value = slot->info->name; \
143 module_put(ops->owner); \
148 GET_STATUS(power_status, u8)
149 GET_STATUS(attention_status, u8)
150 GET_STATUS(latch_status, u8)
151 GET_STATUS(adapter_status, u8)
152 GET_STATUS(address, u32)
153 GET_STATUS(max_bus_speed, enum pci_bus_speed)
154 GET_STATUS(cur_bus_speed, enum pci_bus_speed)
156 static ssize_t power_read_file (struct hotplug_slot *slot, char *buf)
161 retval = get_power_status (slot, &value);
164 retval = sprintf (buf, "%d\n", value);
169 static ssize_t power_write_file (struct hotplug_slot *slot, const char *buf,
172 unsigned long lpower;
176 lpower = simple_strtoul (buf, NULL, 10);
177 power = (u8)(lpower & 0xff);
178 dbg ("power = %d\n", power);
180 if (!try_module_get(slot->ops->owner)) {
186 if (slot->ops->disable_slot)
187 retval = slot->ops->disable_slot(slot);
191 if (slot->ops->enable_slot)
192 retval = slot->ops->enable_slot(slot);
196 err ("Illegal value specified for power\n");
199 module_put(slot->ops->owner);
207 static struct hotplug_slot_attribute hotplug_slot_attr_power = {
208 .attr = {.name = "power", .mode = S_IFREG | S_IRUGO | S_IWUSR},
209 .show = power_read_file,
210 .store = power_write_file
213 static ssize_t attention_read_file (struct hotplug_slot *slot, char *buf)
218 retval = get_attention_status (slot, &value);
221 retval = sprintf (buf, "%d\n", value);
227 static ssize_t attention_write_file (struct hotplug_slot *slot, const char *buf,
230 unsigned long lattention;
234 lattention = simple_strtoul (buf, NULL, 10);
235 attention = (u8)(lattention & 0xff);
236 dbg (" - attention = %d\n", attention);
238 if (!try_module_get(slot->ops->owner)) {
242 if (slot->ops->set_attention_status)
243 retval = slot->ops->set_attention_status(slot, attention);
244 module_put(slot->ops->owner);
252 static struct hotplug_slot_attribute hotplug_slot_attr_attention = {
253 .attr = {.name = "attention", .mode = S_IFREG | S_IRUGO | S_IWUSR},
254 .show = attention_read_file,
255 .store = attention_write_file
258 static ssize_t latch_read_file (struct hotplug_slot *slot, char *buf)
263 retval = get_latch_status (slot, &value);
266 retval = sprintf (buf, "%d\n", value);
272 static struct hotplug_slot_attribute hotplug_slot_attr_latch = {
273 .attr = {.name = "latch", .mode = S_IFREG | S_IRUGO},
274 .show = latch_read_file,
277 static ssize_t presence_read_file (struct hotplug_slot *slot, char *buf)
282 retval = get_adapter_status (slot, &value);
285 retval = sprintf (buf, "%d\n", value);
291 static struct hotplug_slot_attribute hotplug_slot_attr_presence = {
292 .attr = {.name = "adapter", .mode = S_IFREG | S_IRUGO},
293 .show = presence_read_file,
296 static ssize_t address_read_file (struct hotplug_slot *slot, char *buf)
301 retval = get_address (slot, &address);
304 retval = sprintf (buf, "%04x:%02x:%02x\n",
305 (address >> 16) & 0xffff,
306 (address >> 8) & 0xff,
313 static struct hotplug_slot_attribute hotplug_slot_attr_address = {
314 .attr = {.name = "address", .mode = S_IFREG | S_IRUGO},
315 .show = address_read_file,
318 static char *unknown_speed = "Unknown bus speed";
320 static ssize_t max_bus_speed_read_file (struct hotplug_slot *slot, char *buf)
324 enum pci_bus_speed value;
326 retval = get_max_bus_speed (slot, &value);
330 if (value == PCI_SPEED_UNKNOWN)
331 speed_string = unknown_speed;
333 speed_string = pci_bus_speed_strings[value];
335 retval = sprintf (buf, "%s\n", speed_string);
341 static struct hotplug_slot_attribute hotplug_slot_attr_max_bus_speed = {
342 .attr = {.name = "max_bus_speed", .mode = S_IFREG | S_IRUGO},
343 .show = max_bus_speed_read_file,
346 static ssize_t cur_bus_speed_read_file (struct hotplug_slot *slot, char *buf)
350 enum pci_bus_speed value;
352 retval = get_cur_bus_speed (slot, &value);
356 if (value == PCI_SPEED_UNKNOWN)
357 speed_string = unknown_speed;
359 speed_string = pci_bus_speed_strings[value];
361 retval = sprintf (buf, "%s\n", speed_string);
367 static struct hotplug_slot_attribute hotplug_slot_attr_cur_bus_speed = {
368 .attr = {.name = "cur_bus_speed", .mode = S_IFREG | S_IRUGO},
369 .show = cur_bus_speed_read_file,
372 static ssize_t test_write_file (struct hotplug_slot *slot, const char *buf,
379 ltest = simple_strtoul (buf, NULL, 10);
380 test = (u32)(ltest & 0xffffffff);
381 dbg ("test = %d\n", test);
383 if (!try_module_get(slot->ops->owner)) {
387 if (slot->ops->hardware_test)
388 retval = slot->ops->hardware_test(slot, test);
389 module_put(slot->ops->owner);
397 static struct hotplug_slot_attribute hotplug_slot_attr_test = {
398 .attr = {.name = "test", .mode = S_IFREG | S_IRUGO | S_IWUSR},
399 .store = test_write_file
402 static int has_power_file (struct hotplug_slot *slot)
404 if ((!slot) || (!slot->ops))
406 if ((slot->ops->enable_slot) ||
407 (slot->ops->disable_slot) ||
408 (slot->ops->get_power_status))
413 static int has_attention_file (struct hotplug_slot *slot)
415 if ((!slot) || (!slot->ops))
417 if ((slot->ops->set_attention_status) ||
418 (slot->ops->get_attention_status))
423 static int has_latch_file (struct hotplug_slot *slot)
425 if ((!slot) || (!slot->ops))
427 if (slot->ops->get_latch_status)
432 static int has_adapter_file (struct hotplug_slot *slot)
434 if ((!slot) || (!slot->ops))
436 if (slot->ops->get_adapter_status)
441 static int has_address_file (struct hotplug_slot *slot)
443 if ((!slot) || (!slot->ops))
445 if (slot->ops->get_address)
450 static int has_max_bus_speed_file (struct hotplug_slot *slot)
452 if ((!slot) || (!slot->ops))
454 if (slot->ops->get_max_bus_speed)
459 static int has_cur_bus_speed_file (struct hotplug_slot *slot)
461 if ((!slot) || (!slot->ops))
463 if (slot->ops->get_cur_bus_speed)
468 static int has_test_file (struct hotplug_slot *slot)
470 if ((!slot) || (!slot->ops))
472 if (slot->ops->hardware_test)
477 static int fs_add_slot (struct hotplug_slot *slot)
481 if (has_power_file(slot) == 0) {
482 retval = sysfs_create_file(&slot->kobj, &hotplug_slot_attr_power.attr);
487 if (has_attention_file(slot) == 0) {
488 retval = sysfs_create_file(&slot->kobj,
489 &hotplug_slot_attr_attention.attr);
494 if (has_latch_file(slot) == 0) {
495 retval = sysfs_create_file(&slot->kobj,
496 &hotplug_slot_attr_latch.attr);
501 if (has_adapter_file(slot) == 0) {
502 retval = sysfs_create_file(&slot->kobj,
503 &hotplug_slot_attr_presence.attr);
508 if (has_address_file(slot) == 0) {
509 retval = sysfs_create_file(&slot->kobj,
510 &hotplug_slot_attr_address.attr);
515 if (has_max_bus_speed_file(slot) == 0) {
516 retval = sysfs_create_file(&slot->kobj,
517 &hotplug_slot_attr_max_bus_speed.attr);
522 if (has_cur_bus_speed_file(slot) == 0) {
523 retval = sysfs_create_file(&slot->kobj,
524 &hotplug_slot_attr_cur_bus_speed.attr);
529 if (has_test_file(slot) == 0) {
530 retval = sysfs_create_file(&slot->kobj,
531 &hotplug_slot_attr_test.attr);
539 if (has_cur_bus_speed_file(slot) == 0)
540 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_cur_bus_speed.attr);
543 if (has_max_bus_speed_file(slot) == 0)
544 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_max_bus_speed.attr);
547 if (has_address_file(slot) == 0)
548 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_address.attr);
551 if (has_adapter_file(slot) == 0)
552 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_presence.attr);
555 if (has_latch_file(slot) == 0)
556 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_latch.attr);
559 if (has_attention_file(slot) == 0)
560 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_attention.attr);
563 if (has_power_file(slot) == 0)
564 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_power.attr);
570 static void fs_remove_slot (struct hotplug_slot *slot)
572 if (has_power_file(slot) == 0)
573 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_power.attr);
575 if (has_attention_file(slot) == 0)
576 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_attention.attr);
578 if (has_latch_file(slot) == 0)
579 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_latch.attr);
581 if (has_adapter_file(slot) == 0)
582 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_presence.attr);
584 if (has_address_file(slot) == 0)
585 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_address.attr);
587 if (has_max_bus_speed_file(slot) == 0)
588 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_max_bus_speed.attr);
590 if (has_cur_bus_speed_file(slot) == 0)
591 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_cur_bus_speed.attr);
593 if (has_test_file(slot) == 0)
594 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_test.attr);
597 static struct hotplug_slot *get_slot_from_name (const char *name)
599 struct hotplug_slot *slot;
600 struct list_head *tmp;
602 list_for_each (tmp, &pci_hotplug_slot_list) {
603 slot = list_entry (tmp, struct hotplug_slot, slot_list);
604 if (strcmp(slot->name, name) == 0)
611 * pci_hp_register - register a hotplug_slot with the PCI hotplug subsystem
612 * @slot: pointer to the &struct hotplug_slot to register
614 * Registers a hotplug slot with the pci hotplug subsystem, which will allow
615 * userspace interaction to the slot.
617 * Returns 0 if successful, anything else for an error.
619 int pci_hp_register (struct hotplug_slot *slot)
622 struct hotplug_slot *tmp;
626 if ((slot->info == NULL) || (slot->ops == NULL))
628 if (slot->release == NULL) {
629 dbg("Why are you trying to register a hotplug slot "
630 "without a proper release function?\n");
634 /* Check if we have already registered a slot with the same name. */
635 tmp = get_slot_from_name(slot->name);
639 slot->kobj.kset = pci_hotplug_slots_kset;
640 result = kobject_init_and_add(&slot->kobj, &hotplug_slot_ktype, NULL,
643 err("Unable to register kobject '%s'", slot->name);
647 list_add (&slot->slot_list, &pci_hotplug_slot_list);
649 result = fs_add_slot (slot);
650 kobject_uevent(&slot->kobj, KOBJ_ADD);
651 dbg ("Added slot %s to the list\n", slot->name);
656 * pci_hp_deregister - deregister a hotplug_slot with the PCI hotplug subsystem
657 * @slot: pointer to the &struct hotplug_slot to deregister
659 * The @slot must have been registered with the pci hotplug subsystem
660 * previously with a call to pci_hp_register().
662 * Returns 0 if successful, anything else for an error.
664 int pci_hp_deregister (struct hotplug_slot *slot)
666 struct hotplug_slot *temp;
671 temp = get_slot_from_name (slot->name);
675 list_del (&slot->slot_list);
677 fs_remove_slot (slot);
678 dbg ("Removed slot %s from the list\n", slot->name);
679 kobject_put(&slot->kobj);
684 * pci_hp_change_slot_info - changes the slot's information structure in the core
685 * @slot: pointer to the slot whose info has changed
686 * @info: pointer to the info copy into the slot's info structure
688 * @slot must have been registered with the pci
689 * hotplug subsystem previously with a call to pci_hp_register().
691 * Returns 0 if successful, anything else for an error.
693 int __must_check pci_hp_change_slot_info(struct hotplug_slot *slot,
694 struct hotplug_slot_info *info)
696 if ((slot == NULL) || (info == NULL))
699 memcpy (slot->info, info, sizeof (struct hotplug_slot_info));
704 static int __init pci_hotplug_init (void)
707 struct kset *pci_bus_kset;
709 pci_bus_kset = bus_get_kset(&pci_bus_type);
711 pci_hotplug_slots_kset = kset_create_and_add("slots", NULL,
712 &pci_bus_kset->kobj);
713 if (!pci_hotplug_slots_kset) {
715 err("Register subsys error\n");
718 result = cpci_hotplug_init(debug);
720 err ("cpci_hotplug_init with error %d\n", result);
724 info (DRIVER_DESC " version: " DRIVER_VERSION "\n");
728 kset_unregister(pci_hotplug_slots_kset);
733 static void __exit pci_hotplug_exit (void)
736 kset_unregister(pci_hotplug_slots_kset);
739 module_init(pci_hotplug_init);
740 module_exit(pci_hotplug_exit);
742 MODULE_AUTHOR(DRIVER_AUTHOR);
743 MODULE_DESCRIPTION(DRIVER_DESC);
744 MODULE_LICENSE("GPL");
745 module_param(debug, bool, 0644);
746 MODULE_PARM_DESC(debug, "Debugging mode enabled or not");
748 EXPORT_SYMBOL_GPL(pci_hotplug_slots_kset);
749 EXPORT_SYMBOL_GPL(pci_hp_register);
750 EXPORT_SYMBOL_GPL(pci_hp_deregister);
751 EXPORT_SYMBOL_GPL(pci_hp_change_slot_info);