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 <greg@kroah.com>
26 * Filesystem portion based on work done by Pat Mochel on ddfs/driverfs
30 #include <linux/module.h>
31 #include <linux/moduleparam.h>
32 #include <linux/kernel.h>
33 #include <linux/types.h>
34 #include <linux/list.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 <asm/uaccess.h>
43 #include <linux/kobject.h>
44 #include <linux/sysfs.h>
45 #include "pci_hotplug.h"
48 #define MY_NAME "pci_hotplug"
50 #define dbg(fmt, arg...) do { if (debug) printk(KERN_DEBUG "%s: %s: " fmt , MY_NAME , __FUNCTION__ , ## arg); } while (0)
51 #define err(format, arg...) printk(KERN_ERR "%s: " format , MY_NAME , ## arg)
52 #define info(format, arg...) printk(KERN_INFO "%s: " format , MY_NAME , ## arg)
53 #define warn(format, arg...) printk(KERN_WARNING "%s: " format , MY_NAME , ## arg)
59 #define DRIVER_VERSION "0.5"
60 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Scott Murray <scottm@somanetworks.com>"
61 #define DRIVER_DESC "PCI Hot Plug PCI Core"
64 //////////////////////////////////////////////////////////////////
66 static LIST_HEAD(pci_hotplug_slot_list);
68 struct subsystem pci_hotplug_slots_subsys;
70 static ssize_t hotplug_slot_attr_show(struct kobject *kobj,
71 struct attribute *attr, char *buf)
73 struct hotplug_slot *slot = to_hotplug_slot(kobj);
74 struct hotplug_slot_attribute *attribute = to_hotplug_attr(attr);
75 return attribute->show ? attribute->show(slot, buf) : -EIO;
78 static ssize_t hotplug_slot_attr_store(struct kobject *kobj,
79 struct attribute *attr, const char *buf, size_t len)
81 struct hotplug_slot *slot = to_hotplug_slot(kobj);
82 struct hotplug_slot_attribute *attribute = to_hotplug_attr(attr);
83 return attribute->store ? attribute->store(slot, buf, len) : -EIO;
86 static struct sysfs_ops hotplug_slot_sysfs_ops = {
87 .show = hotplug_slot_attr_show,
88 .store = hotplug_slot_attr_store,
91 static void hotplug_slot_release(struct kobject *kobj)
93 struct hotplug_slot *slot = to_hotplug_slot(kobj);
98 static struct kobj_type hotplug_slot_ktype = {
99 .sysfs_ops = &hotplug_slot_sysfs_ops,
100 .release = &hotplug_slot_release,
103 decl_subsys_name(pci_hotplug_slots, slots, &hotplug_slot_ktype, NULL);
105 /* these strings match up with the values in pci_bus_speed */
106 static char *pci_bus_speed_strings[] = {
107 "33 MHz PCI", /* 0x00 */
108 "66 MHz PCI", /* 0x01 */
109 "66 MHz PCIX", /* 0x02 */
110 "100 MHz PCIX", /* 0x03 */
111 "133 MHz PCIX", /* 0x04 */
116 "66 MHz PCIX 266", /* 0x09 */
117 "100 MHz PCIX 266", /* 0x0a */
118 "133 MHz PCIX 266", /* 0x0b */
124 "66 MHz PCIX 533", /* 0x11 */
125 "100 MHz PCIX 533", /* 0x12 */
126 "133 MHz PCIX 533", /* 0x13 */
127 "25 GBps PCI-E", /* 0x14 */
130 #ifdef CONFIG_HOTPLUG_PCI_CPCI
131 extern int cpci_hotplug_init(int debug);
132 extern void cpci_hotplug_exit(void);
134 static inline int cpci_hotplug_init(int debug) { return 0; }
135 static inline void cpci_hotplug_exit(void) { }
138 /* Weee, fun with macros... */
139 #define GET_STATUS(name,type) \
140 static int get_##name (struct hotplug_slot *slot, type *value) \
142 struct hotplug_slot_ops *ops = slot->ops; \
144 if (try_module_get(ops->owner)) { \
145 if (ops->get_##name) \
146 retval = ops->get_##name (slot, value); \
148 *value = slot->info->name; \
149 module_put(ops->owner); \
154 GET_STATUS(power_status, u8)
155 GET_STATUS(attention_status, u8)
156 GET_STATUS(latch_status, u8)
157 GET_STATUS(adapter_status, u8)
158 GET_STATUS(address, u32)
159 GET_STATUS(max_bus_speed, enum pci_bus_speed)
160 GET_STATUS(cur_bus_speed, enum pci_bus_speed)
162 static ssize_t power_read_file (struct hotplug_slot *slot, char *buf)
167 retval = get_power_status (slot, &value);
170 retval = sprintf (buf, "%d\n", value);
175 static ssize_t power_write_file (struct hotplug_slot *slot, const char *buf,
178 unsigned long lpower;
182 lpower = simple_strtoul (buf, NULL, 10);
183 power = (u8)(lpower & 0xff);
184 dbg ("power = %d\n", power);
186 if (!try_module_get(slot->ops->owner)) {
192 if (slot->ops->disable_slot)
193 retval = slot->ops->disable_slot(slot);
197 if (slot->ops->enable_slot)
198 retval = slot->ops->enable_slot(slot);
202 err ("Illegal value specified for power\n");
205 module_put(slot->ops->owner);
213 static struct hotplug_slot_attribute hotplug_slot_attr_power = {
214 .attr = {.name = "power", .mode = S_IFREG | S_IRUGO | S_IWUSR},
215 .show = power_read_file,
216 .store = power_write_file
219 static ssize_t attention_read_file (struct hotplug_slot *slot, char *buf)
224 retval = get_attention_status (slot, &value);
227 retval = sprintf (buf, "%d\n", value);
233 static ssize_t attention_write_file (struct hotplug_slot *slot, const char *buf,
236 unsigned long lattention;
240 lattention = simple_strtoul (buf, NULL, 10);
241 attention = (u8)(lattention & 0xff);
242 dbg (" - attention = %d\n", attention);
244 if (!try_module_get(slot->ops->owner)) {
248 if (slot->ops->set_attention_status)
249 retval = slot->ops->set_attention_status(slot, attention);
250 module_put(slot->ops->owner);
258 static struct hotplug_slot_attribute hotplug_slot_attr_attention = {
259 .attr = {.name = "attention", .mode = S_IFREG | S_IRUGO | S_IWUSR},
260 .show = attention_read_file,
261 .store = attention_write_file
264 static ssize_t latch_read_file (struct hotplug_slot *slot, char *buf)
269 retval = get_latch_status (slot, &value);
272 retval = sprintf (buf, "%d\n", value);
278 static struct hotplug_slot_attribute hotplug_slot_attr_latch = {
279 .attr = {.name = "latch", .mode = S_IFREG | S_IRUGO},
280 .show = latch_read_file,
283 static ssize_t presence_read_file (struct hotplug_slot *slot, char *buf)
288 retval = get_adapter_status (slot, &value);
291 retval = sprintf (buf, "%d\n", value);
297 static struct hotplug_slot_attribute hotplug_slot_attr_presence = {
298 .attr = {.name = "adapter", .mode = S_IFREG | S_IRUGO},
299 .show = presence_read_file,
302 static ssize_t address_read_file (struct hotplug_slot *slot, char *buf)
307 retval = get_address (slot, &address);
310 retval = sprintf (buf, "%04x:%02x:%02x\n",
311 (address >> 16) & 0xffff,
312 (address >> 8) & 0xff,
319 static struct hotplug_slot_attribute hotplug_slot_attr_address = {
320 .attr = {.name = "address", .mode = S_IFREG | S_IRUGO},
321 .show = address_read_file,
324 static char *unknown_speed = "Unknown bus speed";
326 static ssize_t max_bus_speed_read_file (struct hotplug_slot *slot, char *buf)
330 enum pci_bus_speed value;
332 retval = get_max_bus_speed (slot, &value);
336 if (value == PCI_SPEED_UNKNOWN)
337 speed_string = unknown_speed;
339 speed_string = pci_bus_speed_strings[value];
341 retval = sprintf (buf, "%s\n", speed_string);
347 static struct hotplug_slot_attribute hotplug_slot_attr_max_bus_speed = {
348 .attr = {.name = "max_bus_speed", .mode = S_IFREG | S_IRUGO},
349 .show = max_bus_speed_read_file,
352 static ssize_t cur_bus_speed_read_file (struct hotplug_slot *slot, char *buf)
356 enum pci_bus_speed value;
358 retval = get_cur_bus_speed (slot, &value);
362 if (value == PCI_SPEED_UNKNOWN)
363 speed_string = unknown_speed;
365 speed_string = pci_bus_speed_strings[value];
367 retval = sprintf (buf, "%s\n", speed_string);
373 static struct hotplug_slot_attribute hotplug_slot_attr_cur_bus_speed = {
374 .attr = {.name = "cur_bus_speed", .mode = S_IFREG | S_IRUGO},
375 .show = cur_bus_speed_read_file,
378 static ssize_t test_write_file (struct hotplug_slot *slot, const char *buf,
385 ltest = simple_strtoul (buf, NULL, 10);
386 test = (u32)(ltest & 0xffffffff);
387 dbg ("test = %d\n", test);
389 if (!try_module_get(slot->ops->owner)) {
393 if (slot->ops->hardware_test)
394 retval = slot->ops->hardware_test(slot, test);
395 module_put(slot->ops->owner);
403 static struct hotplug_slot_attribute hotplug_slot_attr_test = {
404 .attr = {.name = "test", .mode = S_IFREG | S_IRUGO | S_IWUSR},
405 .store = test_write_file
408 static int has_power_file (struct hotplug_slot *slot)
410 if ((!slot) || (!slot->ops))
412 if ((slot->ops->enable_slot) ||
413 (slot->ops->disable_slot) ||
414 (slot->ops->get_power_status))
419 static int has_attention_file (struct hotplug_slot *slot)
421 if ((!slot) || (!slot->ops))
423 if ((slot->ops->set_attention_status) ||
424 (slot->ops->get_attention_status))
429 static int has_latch_file (struct hotplug_slot *slot)
431 if ((!slot) || (!slot->ops))
433 if (slot->ops->get_latch_status)
438 static int has_adapter_file (struct hotplug_slot *slot)
440 if ((!slot) || (!slot->ops))
442 if (slot->ops->get_adapter_status)
447 static int has_address_file (struct hotplug_slot *slot)
449 if ((!slot) || (!slot->ops))
451 if (slot->ops->get_address)
456 static int has_max_bus_speed_file (struct hotplug_slot *slot)
458 if ((!slot) || (!slot->ops))
460 if (slot->ops->get_max_bus_speed)
465 static int has_cur_bus_speed_file (struct hotplug_slot *slot)
467 if ((!slot) || (!slot->ops))
469 if (slot->ops->get_cur_bus_speed)
474 static int has_test_file (struct hotplug_slot *slot)
476 if ((!slot) || (!slot->ops))
478 if (slot->ops->hardware_test)
483 static int fs_add_slot (struct hotplug_slot *slot)
485 if (has_power_file(slot) == 0)
486 sysfs_create_file(&slot->kobj, &hotplug_slot_attr_power.attr);
488 if (has_attention_file(slot) == 0)
489 sysfs_create_file(&slot->kobj, &hotplug_slot_attr_attention.attr);
491 if (has_latch_file(slot) == 0)
492 sysfs_create_file(&slot->kobj, &hotplug_slot_attr_latch.attr);
494 if (has_adapter_file(slot) == 0)
495 sysfs_create_file(&slot->kobj, &hotplug_slot_attr_presence.attr);
497 if (has_address_file(slot) == 0)
498 sysfs_create_file(&slot->kobj, &hotplug_slot_attr_address.attr);
500 if (has_max_bus_speed_file(slot) == 0)
501 sysfs_create_file(&slot->kobj, &hotplug_slot_attr_max_bus_speed.attr);
503 if (has_cur_bus_speed_file(slot) == 0)
504 sysfs_create_file(&slot->kobj, &hotplug_slot_attr_cur_bus_speed.attr);
506 if (has_test_file(slot) == 0)
507 sysfs_create_file(&slot->kobj, &hotplug_slot_attr_test.attr);
512 static void fs_remove_slot (struct hotplug_slot *slot)
514 if (has_power_file(slot) == 0)
515 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_power.attr);
517 if (has_attention_file(slot) == 0)
518 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_attention.attr);
520 if (has_latch_file(slot) == 0)
521 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_latch.attr);
523 if (has_adapter_file(slot) == 0)
524 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_presence.attr);
526 if (has_address_file(slot) == 0)
527 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_address.attr);
529 if (has_max_bus_speed_file(slot) == 0)
530 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_max_bus_speed.attr);
532 if (has_cur_bus_speed_file(slot) == 0)
533 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_cur_bus_speed.attr);
535 if (has_test_file(slot) == 0)
536 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_test.attr);
539 static struct hotplug_slot *get_slot_from_name (const char *name)
541 struct hotplug_slot *slot;
542 struct list_head *tmp;
544 list_for_each (tmp, &pci_hotplug_slot_list) {
545 slot = list_entry (tmp, struct hotplug_slot, slot_list);
546 if (strcmp(slot->name, name) == 0)
553 * pci_hp_register - register a hotplug_slot with the PCI hotplug subsystem
554 * @slot: pointer to the &struct hotplug_slot to register
556 * Registers a hotplug slot with the pci hotplug subsystem, which will allow
557 * userspace interaction to the slot.
559 * Returns 0 if successful, anything else for an error.
561 int pci_hp_register (struct hotplug_slot *slot)
567 if ((slot->info == NULL) || (slot->ops == NULL))
569 if (slot->release == NULL) {
570 dbg("Why are you trying to register a hotplug slot"
571 "without a proper release function?\n");
575 kobject_set_name(&slot->kobj, "%s", slot->name);
576 kobj_set_kset_s(slot, pci_hotplug_slots_subsys);
578 /* this can fail if we have already registered a slot with the same name */
579 if (kobject_register(&slot->kobj)) {
580 err("Unable to register kobject");
584 list_add (&slot->slot_list, &pci_hotplug_slot_list);
586 result = fs_add_slot (slot);
587 dbg ("Added slot %s to the list\n", slot->name);
592 * pci_hp_deregister - deregister a hotplug_slot with the PCI hotplug subsystem
593 * @slot: pointer to the &struct hotplug_slot to deregister
595 * The @slot must have been registered with the pci hotplug subsystem
596 * previously with a call to pci_hp_register().
598 * Returns 0 if successful, anything else for an error.
600 int pci_hp_deregister (struct hotplug_slot *slot)
602 struct hotplug_slot *temp;
607 temp = get_slot_from_name (slot->name);
611 list_del (&slot->slot_list);
613 fs_remove_slot (slot);
614 dbg ("Removed slot %s from the list\n", slot->name);
615 kobject_unregister(&slot->kobj);
620 * pci_hp_change_slot_info - changes the slot's information structure in the core
621 * @slot: pointer to the slot whose info has changed
622 * @info: pointer to the info copy into the slot's info structure
624 * @slot must have been registered with the pci
625 * hotplug subsystem previously with a call to pci_hp_register().
627 * Returns 0 if successful, anything else for an error.
629 int pci_hp_change_slot_info (struct hotplug_slot *slot, struct hotplug_slot_info *info)
631 if ((slot == NULL) || (info == NULL))
635 * check all fields in the info structure, and update timestamps
636 * for the files referring to the fields that have now changed.
638 if ((has_power_file(slot) == 0) &&
639 (slot->info->power_status != info->power_status))
640 sysfs_update_file(&slot->kobj, &hotplug_slot_attr_power.attr);
642 if ((has_attention_file(slot) == 0) &&
643 (slot->info->attention_status != info->attention_status))
644 sysfs_update_file(&slot->kobj, &hotplug_slot_attr_attention.attr);
646 if ((has_latch_file(slot) == 0) &&
647 (slot->info->latch_status != info->latch_status))
648 sysfs_update_file(&slot->kobj, &hotplug_slot_attr_latch.attr);
650 if ((has_adapter_file(slot) == 0) &&
651 (slot->info->adapter_status != info->adapter_status))
652 sysfs_update_file(&slot->kobj, &hotplug_slot_attr_presence.attr);
654 if ((has_address_file(slot) == 0) &&
655 (slot->info->address != info->address))
656 sysfs_update_file(&slot->kobj, &hotplug_slot_attr_address.attr);
658 if ((has_max_bus_speed_file(slot) == 0) &&
659 (slot->info->max_bus_speed != info->max_bus_speed))
660 sysfs_update_file(&slot->kobj, &hotplug_slot_attr_max_bus_speed.attr);
662 if ((has_cur_bus_speed_file(slot) == 0) &&
663 (slot->info->cur_bus_speed != info->cur_bus_speed))
664 sysfs_update_file(&slot->kobj, &hotplug_slot_attr_cur_bus_speed.attr);
666 memcpy (slot->info, info, sizeof (struct hotplug_slot_info));
671 static int __init pci_hotplug_init (void)
675 kset_set_kset_s(&pci_hotplug_slots_subsys, pci_bus_type.subsys);
676 result = subsystem_register(&pci_hotplug_slots_subsys);
678 err("Register subsys with error %d\n", result);
681 result = cpci_hotplug_init(debug);
683 err ("cpci_hotplug_init with error %d\n", result);
687 info (DRIVER_DESC " version: " DRIVER_VERSION "\n");
691 subsystem_unregister(&pci_hotplug_slots_subsys);
696 static void __exit pci_hotplug_exit (void)
699 subsystem_unregister(&pci_hotplug_slots_subsys);
702 module_init(pci_hotplug_init);
703 module_exit(pci_hotplug_exit);
705 MODULE_AUTHOR(DRIVER_AUTHOR);
706 MODULE_DESCRIPTION(DRIVER_DESC);
707 MODULE_LICENSE("GPL");
708 module_param(debug, bool, 0644);
709 MODULE_PARM_DESC(debug, "Debugging mode enabled or not");
711 EXPORT_SYMBOL_GPL(pci_hotplug_slots_subsys);
712 EXPORT_SYMBOL_GPL(pci_hp_register);
713 EXPORT_SYMBOL_GPL(pci_hp_deregister);
714 EXPORT_SYMBOL_GPL(pci_hp_change_slot_info);