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/config.h>
31 #include <linux/module.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 #if !defined(CONFIG_HOTPLUG_PCI_MODULE)
49 #define MY_NAME "pci_hotplug"
51 #define MY_NAME THIS_MODULE->name
54 #define dbg(fmt, arg...) do { if (debug) printk(KERN_DEBUG "%s: %s: " fmt , MY_NAME , __FUNCTION__ , ## arg); } while (0)
55 #define err(format, arg...) printk(KERN_ERR "%s: " format , MY_NAME , ## arg)
56 #define info(format, arg...) printk(KERN_INFO "%s: " format , MY_NAME , ## arg)
57 #define warn(format, arg...) printk(KERN_WARNING "%s: " format , MY_NAME , ## arg)
63 #define DRIVER_VERSION "0.5"
64 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Scott Murray <scottm@somanetworks.com>"
65 #define DRIVER_DESC "PCI Hot Plug PCI Core"
68 //////////////////////////////////////////////////////////////////
70 static LIST_HEAD(pci_hotplug_slot_list);
72 struct subsystem pci_hotplug_slots_subsys;
74 static ssize_t hotplug_slot_attr_show(struct kobject *kobj,
75 struct attribute *attr, char *buf)
77 struct hotplug_slot *slot = to_hotplug_slot(kobj);
78 struct hotplug_slot_attribute *attribute = to_hotplug_attr(attr);
79 return attribute->show ? attribute->show(slot, buf) : 0;
82 static ssize_t hotplug_slot_attr_store(struct kobject *kobj,
83 struct attribute *attr, const char *buf, size_t len)
85 struct hotplug_slot *slot = to_hotplug_slot(kobj);
86 struct hotplug_slot_attribute *attribute = to_hotplug_attr(attr);
87 return attribute->store ? attribute->store(slot, buf, len) : 0;
90 static struct sysfs_ops hotplug_slot_sysfs_ops = {
91 .show = hotplug_slot_attr_show,
92 .store = hotplug_slot_attr_store,
95 static void hotplug_slot_release(struct kobject *kobj)
97 struct hotplug_slot *slot = to_hotplug_slot(kobj);
102 static struct kobj_type hotplug_slot_ktype = {
103 .sysfs_ops = &hotplug_slot_sysfs_ops,
104 .release = &hotplug_slot_release,
107 decl_subsys_name(pci_hotplug_slots, slots, &hotplug_slot_ktype, NULL);
109 /* these strings match up with the values in pci_bus_speed */
110 static char *pci_bus_speed_strings[] = {
111 "33 MHz PCI", /* 0x00 */
112 "66 MHz PCI", /* 0x01 */
113 "66 MHz PCIX", /* 0x02 */
114 "100 MHz PCIX", /* 0x03 */
115 "133 MHz PCIX", /* 0x04 */
120 "66 MHz PCIX 266", /* 0x09 */
121 "100 MHz PCIX 266", /* 0x0a */
122 "133 MHz PCIX 266", /* 0x0b */
128 "66 MHz PCIX 533", /* 0x11 */
129 "100 MHz PCIX 533", /* 0x12 */
130 "133 MHz PCIX 533", /* 0x13 */
131 "25 GBps PCI-E", /* 0x14 */
134 #ifdef CONFIG_HOTPLUG_PCI_CPCI
135 extern int cpci_hotplug_init(int debug);
136 extern void cpci_hotplug_exit(void);
138 static inline int cpci_hotplug_init(int debug) { return 0; }
139 static inline void cpci_hotplug_exit(void) { }
142 /* Weee, fun with macros... */
143 #define GET_STATUS(name,type) \
144 static int get_##name (struct hotplug_slot *slot, type *value) \
146 struct hotplug_slot_ops *ops = slot->ops; \
148 if (try_module_get(ops->owner)) { \
149 if (ops->get_##name) \
150 retval = ops->get_##name (slot, value); \
152 *value = slot->info->name; \
153 module_put(ops->owner); \
158 GET_STATUS(power_status, u8)
159 GET_STATUS(attention_status, u8)
160 GET_STATUS(latch_status, u8)
161 GET_STATUS(adapter_status, u8)
162 GET_STATUS(address, u32)
163 GET_STATUS(max_bus_speed, enum pci_bus_speed)
164 GET_STATUS(cur_bus_speed, enum pci_bus_speed)
166 static ssize_t power_read_file (struct hotplug_slot *slot, char *buf)
171 retval = get_power_status (slot, &value);
174 retval = sprintf (buf, "%d\n", value);
179 static ssize_t power_write_file (struct hotplug_slot *slot, const char *buf,
182 unsigned long lpower;
186 lpower = simple_strtoul (buf, NULL, 10);
187 power = (u8)(lpower & 0xff);
188 dbg ("power = %d\n", power);
190 if (!try_module_get(slot->ops->owner)) {
196 if (slot->ops->disable_slot)
197 retval = slot->ops->disable_slot(slot);
201 if (slot->ops->enable_slot)
202 retval = slot->ops->enable_slot(slot);
206 err ("Illegal value specified for power\n");
209 module_put(slot->ops->owner);
217 static struct hotplug_slot_attribute hotplug_slot_attr_power = {
218 .attr = {.name = "power", .mode = S_IFREG | S_IRUGO | S_IWUSR},
219 .show = power_read_file,
220 .store = power_write_file
223 static ssize_t attention_read_file (struct hotplug_slot *slot, char *buf)
228 retval = get_attention_status (slot, &value);
231 retval = sprintf (buf, "%d\n", value);
237 static ssize_t attention_write_file (struct hotplug_slot *slot, const char *buf,
240 unsigned long lattention;
244 lattention = simple_strtoul (buf, NULL, 10);
245 attention = (u8)(lattention & 0xff);
246 dbg (" - attention = %d\n", attention);
248 if (!try_module_get(slot->ops->owner)) {
252 if (slot->ops->set_attention_status)
253 retval = slot->ops->set_attention_status(slot, attention);
254 module_put(slot->ops->owner);
262 static struct hotplug_slot_attribute hotplug_slot_attr_attention = {
263 .attr = {.name = "attention", .mode = S_IFREG | S_IRUGO | S_IWUSR},
264 .show = attention_read_file,
265 .store = attention_write_file
268 static ssize_t latch_read_file (struct hotplug_slot *slot, char *buf)
273 retval = get_latch_status (slot, &value);
276 retval = sprintf (buf, "%d\n", value);
282 static struct hotplug_slot_attribute hotplug_slot_attr_latch = {
283 .attr = {.name = "latch", .mode = S_IFREG | S_IRUGO},
284 .show = latch_read_file,
287 static ssize_t presence_read_file (struct hotplug_slot *slot, char *buf)
292 retval = get_adapter_status (slot, &value);
295 retval = sprintf (buf, "%d\n", value);
301 static struct hotplug_slot_attribute hotplug_slot_attr_presence = {
302 .attr = {.name = "adapter", .mode = S_IFREG | S_IRUGO},
303 .show = presence_read_file,
306 static ssize_t address_read_file (struct hotplug_slot *slot, char *buf)
311 retval = get_address (slot, &address);
314 retval = sprintf (buf, "%04x:%02x:%02x\n",
315 (address >> 16) & 0xffff,
316 (address >> 8) & 0xff,
323 static struct hotplug_slot_attribute hotplug_slot_attr_address = {
324 .attr = {.name = "address", .mode = S_IFREG | S_IRUGO},
325 .show = address_read_file,
328 static char *unknown_speed = "Unknown bus speed";
330 static ssize_t max_bus_speed_read_file (struct hotplug_slot *slot, char *buf)
334 enum pci_bus_speed value;
336 retval = get_max_bus_speed (slot, &value);
340 if (value == PCI_SPEED_UNKNOWN)
341 speed_string = unknown_speed;
343 speed_string = pci_bus_speed_strings[value];
345 retval = sprintf (buf, "%s\n", speed_string);
351 static struct hotplug_slot_attribute hotplug_slot_attr_max_bus_speed = {
352 .attr = {.name = "max_bus_speed", .mode = S_IFREG | S_IRUGO},
353 .show = max_bus_speed_read_file,
356 static ssize_t cur_bus_speed_read_file (struct hotplug_slot *slot, char *buf)
360 enum pci_bus_speed value;
362 retval = get_cur_bus_speed (slot, &value);
366 if (value == PCI_SPEED_UNKNOWN)
367 speed_string = unknown_speed;
369 speed_string = pci_bus_speed_strings[value];
371 retval = sprintf (buf, "%s\n", speed_string);
377 static struct hotplug_slot_attribute hotplug_slot_attr_cur_bus_speed = {
378 .attr = {.name = "cur_bus_speed", .mode = S_IFREG | S_IRUGO},
379 .show = cur_bus_speed_read_file,
382 static ssize_t test_write_file (struct hotplug_slot *slot, const char *buf,
389 ltest = simple_strtoul (buf, NULL, 10);
390 test = (u32)(ltest & 0xffffffff);
391 dbg ("test = %d\n", test);
393 if (!try_module_get(slot->ops->owner)) {
397 if (slot->ops->hardware_test)
398 retval = slot->ops->hardware_test(slot, test);
399 module_put(slot->ops->owner);
407 static struct hotplug_slot_attribute hotplug_slot_attr_test = {
408 .attr = {.name = "test", .mode = S_IFREG | S_IRUGO | S_IWUSR},
409 .store = test_write_file
412 static int has_power_file (struct hotplug_slot *slot)
414 if ((!slot) || (!slot->ops))
416 if ((slot->ops->enable_slot) ||
417 (slot->ops->disable_slot) ||
418 (slot->ops->get_power_status))
423 static int has_attention_file (struct hotplug_slot *slot)
425 if ((!slot) || (!slot->ops))
427 if ((slot->ops->set_attention_status) ||
428 (slot->ops->get_attention_status))
433 static int has_latch_file (struct hotplug_slot *slot)
435 if ((!slot) || (!slot->ops))
437 if (slot->ops->get_latch_status)
442 static int has_adapter_file (struct hotplug_slot *slot)
444 if ((!slot) || (!slot->ops))
446 if (slot->ops->get_adapter_status)
451 static int has_address_file (struct hotplug_slot *slot)
453 if ((!slot) || (!slot->ops))
455 if (slot->ops->get_address)
460 static int has_max_bus_speed_file (struct hotplug_slot *slot)
462 if ((!slot) || (!slot->ops))
464 if (slot->ops->get_max_bus_speed)
469 static int has_cur_bus_speed_file (struct hotplug_slot *slot)
471 if ((!slot) || (!slot->ops))
473 if (slot->ops->get_cur_bus_speed)
478 static int has_test_file (struct hotplug_slot *slot)
480 if ((!slot) || (!slot->ops))
482 if (slot->ops->hardware_test)
487 static int fs_add_slot (struct hotplug_slot *slot)
489 if (has_power_file(slot) == 0)
490 sysfs_create_file(&slot->kobj, &hotplug_slot_attr_power.attr);
492 if (has_attention_file(slot) == 0)
493 sysfs_create_file(&slot->kobj, &hotplug_slot_attr_attention.attr);
495 if (has_latch_file(slot) == 0)
496 sysfs_create_file(&slot->kobj, &hotplug_slot_attr_latch.attr);
498 if (has_adapter_file(slot) == 0)
499 sysfs_create_file(&slot->kobj, &hotplug_slot_attr_presence.attr);
501 if (has_address_file(slot) == 0)
502 sysfs_create_file(&slot->kobj, &hotplug_slot_attr_address.attr);
504 if (has_max_bus_speed_file(slot) == 0)
505 sysfs_create_file(&slot->kobj, &hotplug_slot_attr_max_bus_speed.attr);
507 if (has_cur_bus_speed_file(slot) == 0)
508 sysfs_create_file(&slot->kobj, &hotplug_slot_attr_cur_bus_speed.attr);
510 if (has_test_file(slot) == 0)
511 sysfs_create_file(&slot->kobj, &hotplug_slot_attr_test.attr);
516 static void fs_remove_slot (struct hotplug_slot *slot)
518 if (has_power_file(slot) == 0)
519 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_power.attr);
521 if (has_attention_file(slot) == 0)
522 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_attention.attr);
524 if (has_latch_file(slot) == 0)
525 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_latch.attr);
527 if (has_adapter_file(slot) == 0)
528 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_presence.attr);
530 if (has_address_file(slot) == 0)
531 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_address.attr);
533 if (has_max_bus_speed_file(slot) == 0)
534 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_max_bus_speed.attr);
536 if (has_cur_bus_speed_file(slot) == 0)
537 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_cur_bus_speed.attr);
539 if (has_test_file(slot) == 0)
540 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_test.attr);
543 static struct hotplug_slot *get_slot_from_name (const char *name)
545 struct hotplug_slot *slot;
546 struct list_head *tmp;
548 list_for_each (tmp, &pci_hotplug_slot_list) {
549 slot = list_entry (tmp, struct hotplug_slot, slot_list);
550 if (strcmp(slot->name, name) == 0)
557 * pci_hp_register - register a hotplug_slot with the PCI hotplug subsystem
558 * @slot: pointer to the &struct hotplug_slot to register
560 * Registers a hotplug slot with the pci hotplug subsystem, which will allow
561 * userspace interaction to the slot.
563 * Returns 0 if successful, anything else for an error.
565 int pci_hp_register (struct hotplug_slot *slot)
571 if ((slot->info == NULL) || (slot->ops == NULL))
574 kobject_set_name(&slot->kobj, slot->name);
575 kobj_set_kset_s(slot, pci_hotplug_slots_subsys);
577 /* this can fail if we have already registered a slot with the same name */
578 if (kobject_register(&slot->kobj)) {
579 err("Unable to register kobject");
583 list_add (&slot->slot_list, &pci_hotplug_slot_list);
585 result = fs_add_slot (slot);
586 dbg ("Added slot %s to the list\n", slot->name);
591 * pci_hp_deregister - deregister a hotplug_slot with the PCI hotplug subsystem
592 * @slot: pointer to the &struct hotplug_slot to deregister
594 * The @slot must have been registered with the pci hotplug subsystem
595 * previously with a call to pci_hp_register().
597 * Returns 0 if successful, anything else for an error.
599 int pci_hp_deregister (struct hotplug_slot *slot)
601 struct hotplug_slot *temp;
606 temp = get_slot_from_name (slot->name);
610 list_del (&slot->slot_list);
612 fs_remove_slot (slot);
613 dbg ("Removed slot %s from the list\n", slot->name);
614 kobject_unregister(&slot->kobj);
619 * pci_hp_change_slot_info - changes the slot's information structure in the core
620 * @slot: pointer to the slot whose info has changed
621 * @info: pointer to the info copy into the slot's info structure
623 * @slot must have been registered with the pci
624 * hotplug subsystem previously with a call to pci_hp_register().
626 * Returns 0 if successful, anything else for an error.
628 int pci_hp_change_slot_info (struct hotplug_slot *slot, struct hotplug_slot_info *info)
630 if ((slot == NULL) || (info == NULL))
634 * check all fields in the info structure, and update timestamps
635 * for the files referring to the fields that have now changed.
637 if ((has_power_file(slot) == 0) &&
638 (slot->info->power_status != info->power_status))
639 sysfs_update_file(&slot->kobj, &hotplug_slot_attr_power.attr);
641 if ((has_attention_file(slot) == 0) &&
642 (slot->info->attention_status != info->attention_status))
643 sysfs_update_file(&slot->kobj, &hotplug_slot_attr_attention.attr);
645 if ((has_latch_file(slot) == 0) &&
646 (slot->info->latch_status != info->latch_status))
647 sysfs_update_file(&slot->kobj, &hotplug_slot_attr_latch.attr);
649 if ((has_adapter_file(slot) == 0) &&
650 (slot->info->adapter_status != info->adapter_status))
651 sysfs_update_file(&slot->kobj, &hotplug_slot_attr_presence.attr);
653 if ((has_address_file(slot) == 0) &&
654 (slot->info->address != info->address))
655 sysfs_update_file(&slot->kobj, &hotplug_slot_attr_address.attr);
657 if ((has_max_bus_speed_file(slot) == 0) &&
658 (slot->info->max_bus_speed != info->max_bus_speed))
659 sysfs_update_file(&slot->kobj, &hotplug_slot_attr_max_bus_speed.attr);
661 if ((has_cur_bus_speed_file(slot) == 0) &&
662 (slot->info->cur_bus_speed != info->cur_bus_speed))
663 sysfs_update_file(&slot->kobj, &hotplug_slot_attr_cur_bus_speed.attr);
665 memcpy (slot->info, info, sizeof (struct hotplug_slot_info));
670 static int __init pci_hotplug_init (void)
674 kset_set_kset_s(&pci_hotplug_slots_subsys, pci_bus_type.subsys);
675 result = subsystem_register(&pci_hotplug_slots_subsys);
677 err("Register subsys with error %d\n", result);
680 result = cpci_hotplug_init(debug);
682 err ("cpci_hotplug_init with error %d\n", result);
686 info (DRIVER_DESC " version: " DRIVER_VERSION "\n");
690 subsystem_unregister(&pci_hotplug_slots_subsys);
695 static void __exit pci_hotplug_exit (void)
698 subsystem_unregister(&pci_hotplug_slots_subsys);
701 module_init(pci_hotplug_init);
702 module_exit(pci_hotplug_exit);
704 MODULE_AUTHOR(DRIVER_AUTHOR);
705 MODULE_DESCRIPTION(DRIVER_DESC);
706 MODULE_LICENSE("GPL");
707 MODULE_PARM(debug, "i");
708 MODULE_PARM_DESC(debug, "Debugging mode enabled or not");
710 EXPORT_SYMBOL_GPL(pci_hotplug_slots_subsys);
711 EXPORT_SYMBOL_GPL(pci_hp_register);
712 EXPORT_SYMBOL_GPL(pci_hp_deregister);
713 EXPORT_SYMBOL_GPL(pci_hp_change_slot_info);