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/mutex.h>
41 #include <linux/pci.h>
42 #include <linux/pci_hotplug.h>
43 #include <asm/uaccess.h>
46 #define MY_NAME "pci_hotplug"
48 #define dbg(fmt, arg...) do { if (debug) printk(KERN_DEBUG "%s: %s: " fmt , MY_NAME , __func__ , ## arg); } while (0)
49 #define err(format, arg...) printk(KERN_ERR "%s: " format , MY_NAME , ## arg)
50 #define info(format, arg...) printk(KERN_INFO "%s: " format , MY_NAME , ## arg)
51 #define warn(format, arg...) printk(KERN_WARNING "%s: " format , MY_NAME , ## arg)
57 #define DRIVER_VERSION "0.5"
58 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Scott Murray <scottm@somanetworks.com>"
59 #define DRIVER_DESC "PCI Hot Plug PCI Core"
62 //////////////////////////////////////////////////////////////////
64 static LIST_HEAD(pci_hotplug_slot_list
);
65 static DEFINE_MUTEX(pci_hp_mutex
);
67 /* these strings match up with the values in pci_bus_speed */
68 static char *pci_bus_speed_strings
[] = {
69 "33 MHz PCI", /* 0x00 */
70 "66 MHz PCI", /* 0x01 */
71 "66 MHz PCIX", /* 0x02 */
72 "100 MHz PCIX", /* 0x03 */
73 "133 MHz PCIX", /* 0x04 */
78 "66 MHz PCIX 266", /* 0x09 */
79 "100 MHz PCIX 266", /* 0x0a */
80 "133 MHz PCIX 266", /* 0x0b */
86 "66 MHz PCIX 533", /* 0x11 */
87 "100 MHz PCIX 533", /* 0x12 */
88 "133 MHz PCIX 533", /* 0x13 */
89 "2.5 GT/s PCI-E", /* 0x14 */
90 "5.0 GT/s PCI-E", /* 0x15 */
93 #ifdef CONFIG_HOTPLUG_PCI_CPCI
94 extern int cpci_hotplug_init(int debug
);
95 extern void cpci_hotplug_exit(void);
97 static inline int cpci_hotplug_init(int debug
) { return 0; }
98 static inline void cpci_hotplug_exit(void) { }
101 /* Weee, fun with macros... */
102 #define GET_STATUS(name,type) \
103 static int get_##name (struct hotplug_slot *slot, type *value) \
105 struct hotplug_slot_ops *ops = slot->ops; \
107 if (!try_module_get(ops->owner)) \
109 if (ops->get_##name) \
110 retval = ops->get_##name(slot, value); \
112 *value = slot->info->name; \
113 module_put(ops->owner); \
117 GET_STATUS(power_status
, u8
)
118 GET_STATUS(attention_status
, u8
)
119 GET_STATUS(latch_status
, u8
)
120 GET_STATUS(adapter_status
, u8
)
121 GET_STATUS(max_bus_speed
, enum pci_bus_speed
)
122 GET_STATUS(cur_bus_speed
, enum pci_bus_speed
)
124 static ssize_t
power_read_file(struct pci_slot
*slot
, char *buf
)
129 retval
= get_power_status(slot
->hotplug
, &value
);
132 retval
= sprintf (buf
, "%d\n", value
);
137 static ssize_t
power_write_file(struct pci_slot
*pci_slot
, const char *buf
,
140 struct hotplug_slot
*slot
= pci_slot
->hotplug
;
141 unsigned long lpower
;
145 lpower
= simple_strtoul (buf
, NULL
, 10);
146 power
= (u8
)(lpower
& 0xff);
147 dbg ("power = %d\n", power
);
149 if (!try_module_get(slot
->ops
->owner
)) {
155 if (slot
->ops
->disable_slot
)
156 retval
= slot
->ops
->disable_slot(slot
);
160 if (slot
->ops
->enable_slot
)
161 retval
= slot
->ops
->enable_slot(slot
);
165 err ("Illegal value specified for power\n");
168 module_put(slot
->ops
->owner
);
176 static struct pci_slot_attribute hotplug_slot_attr_power
= {
177 .attr
= {.name
= "power", .mode
= S_IFREG
| S_IRUGO
| S_IWUSR
},
178 .show
= power_read_file
,
179 .store
= power_write_file
182 static ssize_t
attention_read_file(struct pci_slot
*slot
, char *buf
)
187 retval
= get_attention_status(slot
->hotplug
, &value
);
190 retval
= sprintf(buf
, "%d\n", value
);
196 static ssize_t
attention_write_file(struct pci_slot
*slot
, const char *buf
,
199 struct hotplug_slot_ops
*ops
= slot
->hotplug
->ops
;
200 unsigned long lattention
;
204 lattention
= simple_strtoul (buf
, NULL
, 10);
205 attention
= (u8
)(lattention
& 0xff);
206 dbg (" - attention = %d\n", attention
);
208 if (!try_module_get(ops
->owner
)) {
212 if (ops
->set_attention_status
)
213 retval
= ops
->set_attention_status(slot
->hotplug
, attention
);
214 module_put(ops
->owner
);
222 static struct pci_slot_attribute hotplug_slot_attr_attention
= {
223 .attr
= {.name
= "attention", .mode
= S_IFREG
| S_IRUGO
| S_IWUSR
},
224 .show
= attention_read_file
,
225 .store
= attention_write_file
228 static ssize_t
latch_read_file(struct pci_slot
*slot
, char *buf
)
233 retval
= get_latch_status(slot
->hotplug
, &value
);
236 retval
= sprintf (buf
, "%d\n", value
);
242 static struct pci_slot_attribute hotplug_slot_attr_latch
= {
243 .attr
= {.name
= "latch", .mode
= S_IFREG
| S_IRUGO
},
244 .show
= latch_read_file
,
247 static ssize_t
presence_read_file(struct pci_slot
*slot
, char *buf
)
252 retval
= get_adapter_status(slot
->hotplug
, &value
);
255 retval
= sprintf (buf
, "%d\n", value
);
261 static struct pci_slot_attribute hotplug_slot_attr_presence
= {
262 .attr
= {.name
= "adapter", .mode
= S_IFREG
| S_IRUGO
},
263 .show
= presence_read_file
,
266 static char *unknown_speed
= "Unknown bus speed";
268 static ssize_t
max_bus_speed_read_file(struct pci_slot
*slot
, char *buf
)
272 enum pci_bus_speed value
;
274 retval
= get_max_bus_speed(slot
->hotplug
, &value
);
278 if (value
== PCI_SPEED_UNKNOWN
)
279 speed_string
= unknown_speed
;
281 speed_string
= pci_bus_speed_strings
[value
];
283 retval
= sprintf (buf
, "%s\n", speed_string
);
289 static struct pci_slot_attribute hotplug_slot_attr_max_bus_speed
= {
290 .attr
= {.name
= "max_bus_speed", .mode
= S_IFREG
| S_IRUGO
},
291 .show
= max_bus_speed_read_file
,
294 static ssize_t
cur_bus_speed_read_file(struct pci_slot
*slot
, char *buf
)
298 enum pci_bus_speed value
;
300 retval
= get_cur_bus_speed(slot
->hotplug
, &value
);
304 if (value
== PCI_SPEED_UNKNOWN
)
305 speed_string
= unknown_speed
;
307 speed_string
= pci_bus_speed_strings
[value
];
309 retval
= sprintf (buf
, "%s\n", speed_string
);
315 static struct pci_slot_attribute hotplug_slot_attr_cur_bus_speed
= {
316 .attr
= {.name
= "cur_bus_speed", .mode
= S_IFREG
| S_IRUGO
},
317 .show
= cur_bus_speed_read_file
,
320 static ssize_t
test_write_file(struct pci_slot
*pci_slot
, const char *buf
,
323 struct hotplug_slot
*slot
= pci_slot
->hotplug
;
328 ltest
= simple_strtoul (buf
, NULL
, 10);
329 test
= (u32
)(ltest
& 0xffffffff);
330 dbg ("test = %d\n", test
);
332 if (!try_module_get(slot
->ops
->owner
)) {
336 if (slot
->ops
->hardware_test
)
337 retval
= slot
->ops
->hardware_test(slot
, test
);
338 module_put(slot
->ops
->owner
);
346 static struct pci_slot_attribute hotplug_slot_attr_test
= {
347 .attr
= {.name
= "test", .mode
= S_IFREG
| S_IRUGO
| S_IWUSR
},
348 .store
= test_write_file
351 static bool has_power_file(struct pci_slot
*pci_slot
)
353 struct hotplug_slot
*slot
= pci_slot
->hotplug
;
354 if ((!slot
) || (!slot
->ops
))
356 if ((slot
->ops
->enable_slot
) ||
357 (slot
->ops
->disable_slot
) ||
358 (slot
->ops
->get_power_status
))
363 static bool has_attention_file(struct pci_slot
*pci_slot
)
365 struct hotplug_slot
*slot
= pci_slot
->hotplug
;
366 if ((!slot
) || (!slot
->ops
))
368 if ((slot
->ops
->set_attention_status
) ||
369 (slot
->ops
->get_attention_status
))
374 static bool has_latch_file(struct pci_slot
*pci_slot
)
376 struct hotplug_slot
*slot
= pci_slot
->hotplug
;
377 if ((!slot
) || (!slot
->ops
))
379 if (slot
->ops
->get_latch_status
)
384 static bool has_adapter_file(struct pci_slot
*pci_slot
)
386 struct hotplug_slot
*slot
= pci_slot
->hotplug
;
387 if ((!slot
) || (!slot
->ops
))
389 if (slot
->ops
->get_adapter_status
)
394 static bool has_max_bus_speed_file(struct pci_slot
*pci_slot
)
396 struct hotplug_slot
*slot
= pci_slot
->hotplug
;
397 if ((!slot
) || (!slot
->ops
))
399 if (slot
->ops
->get_max_bus_speed
)
404 static bool has_cur_bus_speed_file(struct pci_slot
*pci_slot
)
406 struct hotplug_slot
*slot
= pci_slot
->hotplug
;
407 if ((!slot
) || (!slot
->ops
))
409 if (slot
->ops
->get_cur_bus_speed
)
414 static bool has_test_file(struct pci_slot
*pci_slot
)
416 struct hotplug_slot
*slot
= pci_slot
->hotplug
;
417 if ((!slot
) || (!slot
->ops
))
419 if (slot
->ops
->hardware_test
)
424 static int fs_add_slot(struct pci_slot
*slot
)
428 /* Create symbolic link to the hotplug driver module */
429 pci_hp_create_module_link(slot
);
431 if (has_power_file(slot
)) {
432 retval
= sysfs_create_file(&slot
->kobj
,
433 &hotplug_slot_attr_power
.attr
);
438 if (has_attention_file(slot
)) {
439 retval
= sysfs_create_file(&slot
->kobj
,
440 &hotplug_slot_attr_attention
.attr
);
445 if (has_latch_file(slot
)) {
446 retval
= sysfs_create_file(&slot
->kobj
,
447 &hotplug_slot_attr_latch
.attr
);
452 if (has_adapter_file(slot
)) {
453 retval
= sysfs_create_file(&slot
->kobj
,
454 &hotplug_slot_attr_presence
.attr
);
459 if (has_max_bus_speed_file(slot
)) {
460 retval
= sysfs_create_file(&slot
->kobj
,
461 &hotplug_slot_attr_max_bus_speed
.attr
);
466 if (has_cur_bus_speed_file(slot
)) {
467 retval
= sysfs_create_file(&slot
->kobj
,
468 &hotplug_slot_attr_cur_bus_speed
.attr
);
473 if (has_test_file(slot
)) {
474 retval
= sysfs_create_file(&slot
->kobj
,
475 &hotplug_slot_attr_test
.attr
);
483 if (has_cur_bus_speed_file(slot
))
484 sysfs_remove_file(&slot
->kobj
,
485 &hotplug_slot_attr_cur_bus_speed
.attr
);
487 if (has_max_bus_speed_file(slot
))
488 sysfs_remove_file(&slot
->kobj
,
489 &hotplug_slot_attr_max_bus_speed
.attr
);
491 if (has_adapter_file(slot
))
492 sysfs_remove_file(&slot
->kobj
,
493 &hotplug_slot_attr_presence
.attr
);
495 if (has_latch_file(slot
))
496 sysfs_remove_file(&slot
->kobj
, &hotplug_slot_attr_latch
.attr
);
498 if (has_attention_file(slot
))
499 sysfs_remove_file(&slot
->kobj
,
500 &hotplug_slot_attr_attention
.attr
);
502 if (has_power_file(slot
))
503 sysfs_remove_file(&slot
->kobj
, &hotplug_slot_attr_power
.attr
);
505 pci_hp_remove_module_link(slot
);
510 static void fs_remove_slot(struct pci_slot
*slot
)
512 if (has_power_file(slot
))
513 sysfs_remove_file(&slot
->kobj
, &hotplug_slot_attr_power
.attr
);
515 if (has_attention_file(slot
))
516 sysfs_remove_file(&slot
->kobj
,
517 &hotplug_slot_attr_attention
.attr
);
519 if (has_latch_file(slot
))
520 sysfs_remove_file(&slot
->kobj
, &hotplug_slot_attr_latch
.attr
);
522 if (has_adapter_file(slot
))
523 sysfs_remove_file(&slot
->kobj
,
524 &hotplug_slot_attr_presence
.attr
);
526 if (has_max_bus_speed_file(slot
))
527 sysfs_remove_file(&slot
->kobj
,
528 &hotplug_slot_attr_max_bus_speed
.attr
);
530 if (has_cur_bus_speed_file(slot
))
531 sysfs_remove_file(&slot
->kobj
,
532 &hotplug_slot_attr_cur_bus_speed
.attr
);
534 if (has_test_file(slot
))
535 sysfs_remove_file(&slot
->kobj
, &hotplug_slot_attr_test
.attr
);
537 pci_hp_remove_module_link(slot
);
540 static struct hotplug_slot
*get_slot_from_name (const char *name
)
542 struct hotplug_slot
*slot
;
543 struct list_head
*tmp
;
545 list_for_each (tmp
, &pci_hotplug_slot_list
) {
546 slot
= list_entry (tmp
, struct hotplug_slot
, slot_list
);
547 if (strcmp(hotplug_slot_name(slot
), name
) == 0)
554 * __pci_hp_register - register a hotplug_slot with the PCI hotplug subsystem
555 * @bus: bus this slot is on
556 * @slot: pointer to the &struct hotplug_slot to register
557 * @devnr: device number
558 * @name: name registered with kobject core
559 * @owner: caller module owner
560 * @mod_name: caller module name
562 * Registers a hotplug slot with the pci hotplug subsystem, which will allow
563 * userspace interaction to the slot.
565 * Returns 0 if successful, anything else for an error.
567 int __pci_hp_register(struct hotplug_slot
*slot
, struct pci_bus
*bus
,
568 int devnr
, const char *name
,
569 struct module
*owner
, const char *mod_name
)
572 struct pci_slot
*pci_slot
;
576 if ((slot
->info
== NULL
) || (slot
->ops
== NULL
))
578 if (slot
->release
== NULL
) {
579 dbg("Why are you trying to register a hotplug slot "
580 "without a proper release function?\n");
584 slot
->ops
->owner
= owner
;
585 slot
->ops
->mod_name
= mod_name
;
587 mutex_lock(&pci_hp_mutex
);
589 * No problems if we call this interface from both ACPI_PCI_SLOT
590 * driver and call it here again. If we've already created the
591 * pci_slot, the interface will simply bump the refcount.
593 pci_slot
= pci_create_slot(bus
, devnr
, name
, slot
);
594 if (IS_ERR(pci_slot
)) {
595 result
= PTR_ERR(pci_slot
);
599 slot
->pci_slot
= pci_slot
;
600 pci_slot
->hotplug
= slot
;
602 list_add(&slot
->slot_list
, &pci_hotplug_slot_list
);
604 result
= fs_add_slot(pci_slot
);
605 kobject_uevent(&pci_slot
->kobj
, KOBJ_ADD
);
606 dbg("Added slot %s to the list\n", name
);
608 mutex_unlock(&pci_hp_mutex
);
613 * pci_hp_deregister - deregister a hotplug_slot with the PCI hotplug subsystem
614 * @hotplug: pointer to the &struct hotplug_slot to deregister
616 * The @slot must have been registered with the pci hotplug subsystem
617 * previously with a call to pci_hp_register().
619 * Returns 0 if successful, anything else for an error.
621 int pci_hp_deregister(struct hotplug_slot
*hotplug
)
623 struct hotplug_slot
*temp
;
624 struct pci_slot
*slot
;
629 mutex_lock(&pci_hp_mutex
);
630 temp
= get_slot_from_name(hotplug_slot_name(hotplug
));
631 if (temp
!= hotplug
) {
632 mutex_unlock(&pci_hp_mutex
);
636 list_del(&hotplug
->slot_list
);
638 slot
= hotplug
->pci_slot
;
639 fs_remove_slot(slot
);
640 dbg("Removed slot %s from the list\n", hotplug_slot_name(hotplug
));
642 hotplug
->release(hotplug
);
643 slot
->hotplug
= NULL
;
644 pci_destroy_slot(slot
);
645 mutex_unlock(&pci_hp_mutex
);
651 * pci_hp_change_slot_info - changes the slot's information structure in the core
652 * @hotplug: pointer to the slot whose info has changed
653 * @info: pointer to the info copy into the slot's info structure
655 * @slot must have been registered with the pci
656 * hotplug subsystem previously with a call to pci_hp_register().
658 * Returns 0 if successful, anything else for an error.
660 int __must_check
pci_hp_change_slot_info(struct hotplug_slot
*hotplug
,
661 struct hotplug_slot_info
*info
)
663 struct pci_slot
*slot
;
664 if (!hotplug
|| !info
)
666 slot
= hotplug
->pci_slot
;
668 memcpy(hotplug
->info
, info
, sizeof(struct hotplug_slot_info
));
673 static int __init
pci_hotplug_init (void)
677 result
= cpci_hotplug_init(debug
);
679 err ("cpci_hotplug_init with error %d\n", result
);
683 info (DRIVER_DESC
" version: " DRIVER_VERSION
"\n");
689 static void __exit
pci_hotplug_exit (void)
694 module_init(pci_hotplug_init
);
695 module_exit(pci_hotplug_exit
);
697 MODULE_AUTHOR(DRIVER_AUTHOR
);
698 MODULE_DESCRIPTION(DRIVER_DESC
);
699 MODULE_LICENSE("GPL");
700 module_param(debug
, bool, 0644);
701 MODULE_PARM_DESC(debug
, "Debugging mode enabled or not");
703 EXPORT_SYMBOL_GPL(__pci_hp_register
);
704 EXPORT_SYMBOL_GPL(pci_hp_deregister
);
705 EXPORT_SYMBOL_GPL(pci_hp_change_slot_info
);