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>
45 #define MY_NAME "pci_hotplug"
47 #define dbg(fmt, arg...) do { if (debug) printk(KERN_DEBUG "%s: %s: " fmt , MY_NAME , __func__ , ## 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
);
64 static DEFINE_SPINLOCK(pci_hotplug_slot_list_lock
);
66 /* these strings match up with the values in pci_bus_speed */
67 static char *pci_bus_speed_strings
[] = {
68 "33 MHz PCI", /* 0x00 */
69 "66 MHz PCI", /* 0x01 */
70 "66 MHz PCIX", /* 0x02 */
71 "100 MHz PCIX", /* 0x03 */
72 "133 MHz PCIX", /* 0x04 */
77 "66 MHz PCIX 266", /* 0x09 */
78 "100 MHz PCIX 266", /* 0x0a */
79 "133 MHz PCIX 266", /* 0x0b */
85 "66 MHz PCIX 533", /* 0x11 */
86 "100 MHz PCIX 533", /* 0x12 */
87 "133 MHz PCIX 533", /* 0x13 */
88 "25 GBps PCI-E", /* 0x14 */
91 #ifdef CONFIG_HOTPLUG_PCI_CPCI
92 extern int cpci_hotplug_init(int debug
);
93 extern void cpci_hotplug_exit(void);
95 static inline int cpci_hotplug_init(int debug
) { return 0; }
96 static inline void cpci_hotplug_exit(void) { }
99 /* Weee, fun with macros... */
100 #define GET_STATUS(name,type) \
101 static int get_##name (struct hotplug_slot *slot, type *value) \
103 struct hotplug_slot_ops *ops = slot->ops; \
105 if (try_module_get(ops->owner)) { \
106 if (ops->get_##name) \
107 retval = ops->get_##name(slot, value); \
109 *value = slot->info->name; \
110 module_put(ops->owner); \
115 GET_STATUS(power_status
, u8
)
116 GET_STATUS(attention_status
, u8
)
117 GET_STATUS(latch_status
, u8
)
118 GET_STATUS(adapter_status
, u8
)
119 GET_STATUS(max_bus_speed
, enum pci_bus_speed
)
120 GET_STATUS(cur_bus_speed
, enum pci_bus_speed
)
122 static ssize_t
power_read_file(struct pci_slot
*slot
, char *buf
)
127 retval
= get_power_status(slot
->hotplug
, &value
);
130 retval
= sprintf (buf
, "%d\n", value
);
135 static ssize_t
power_write_file(struct pci_slot
*pci_slot
, const char *buf
,
138 struct hotplug_slot
*slot
= pci_slot
->hotplug
;
139 unsigned long lpower
;
143 lpower
= simple_strtoul (buf
, NULL
, 10);
144 power
= (u8
)(lpower
& 0xff);
145 dbg ("power = %d\n", power
);
147 if (!try_module_get(slot
->ops
->owner
)) {
153 if (slot
->ops
->disable_slot
)
154 retval
= slot
->ops
->disable_slot(slot
);
158 if (slot
->ops
->enable_slot
)
159 retval
= slot
->ops
->enable_slot(slot
);
163 err ("Illegal value specified for power\n");
166 module_put(slot
->ops
->owner
);
174 static struct pci_slot_attribute hotplug_slot_attr_power
= {
175 .attr
= {.name
= "power", .mode
= S_IFREG
| S_IRUGO
| S_IWUSR
},
176 .show
= power_read_file
,
177 .store
= power_write_file
180 static ssize_t
attention_read_file(struct pci_slot
*slot
, char *buf
)
185 retval
= get_attention_status(slot
->hotplug
, &value
);
188 retval
= sprintf(buf
, "%d\n", value
);
194 static ssize_t
attention_write_file(struct pci_slot
*slot
, const char *buf
,
197 struct hotplug_slot_ops
*ops
= slot
->hotplug
->ops
;
198 unsigned long lattention
;
202 lattention
= simple_strtoul (buf
, NULL
, 10);
203 attention
= (u8
)(lattention
& 0xff);
204 dbg (" - attention = %d\n", attention
);
206 if (!try_module_get(ops
->owner
)) {
210 if (ops
->set_attention_status
)
211 retval
= ops
->set_attention_status(slot
->hotplug
, attention
);
212 module_put(ops
->owner
);
220 static struct pci_slot_attribute hotplug_slot_attr_attention
= {
221 .attr
= {.name
= "attention", .mode
= S_IFREG
| S_IRUGO
| S_IWUSR
},
222 .show
= attention_read_file
,
223 .store
= attention_write_file
226 static ssize_t
latch_read_file(struct pci_slot
*slot
, char *buf
)
231 retval
= get_latch_status(slot
->hotplug
, &value
);
234 retval
= sprintf (buf
, "%d\n", value
);
240 static struct pci_slot_attribute hotplug_slot_attr_latch
= {
241 .attr
= {.name
= "latch", .mode
= S_IFREG
| S_IRUGO
},
242 .show
= latch_read_file
,
245 static ssize_t
presence_read_file(struct pci_slot
*slot
, char *buf
)
250 retval
= get_adapter_status(slot
->hotplug
, &value
);
253 retval
= sprintf (buf
, "%d\n", value
);
259 static struct pci_slot_attribute hotplug_slot_attr_presence
= {
260 .attr
= {.name
= "adapter", .mode
= S_IFREG
| S_IRUGO
},
261 .show
= presence_read_file
,
264 static char *unknown_speed
= "Unknown bus speed";
266 static ssize_t
max_bus_speed_read_file(struct pci_slot
*slot
, char *buf
)
270 enum pci_bus_speed value
;
272 retval
= get_max_bus_speed(slot
->hotplug
, &value
);
276 if (value
== PCI_SPEED_UNKNOWN
)
277 speed_string
= unknown_speed
;
279 speed_string
= pci_bus_speed_strings
[value
];
281 retval
= sprintf (buf
, "%s\n", speed_string
);
287 static struct pci_slot_attribute hotplug_slot_attr_max_bus_speed
= {
288 .attr
= {.name
= "max_bus_speed", .mode
= S_IFREG
| S_IRUGO
},
289 .show
= max_bus_speed_read_file
,
292 static ssize_t
cur_bus_speed_read_file(struct pci_slot
*slot
, char *buf
)
296 enum pci_bus_speed value
;
298 retval
= get_cur_bus_speed(slot
->hotplug
, &value
);
302 if (value
== PCI_SPEED_UNKNOWN
)
303 speed_string
= unknown_speed
;
305 speed_string
= pci_bus_speed_strings
[value
];
307 retval
= sprintf (buf
, "%s\n", speed_string
);
313 static struct pci_slot_attribute hotplug_slot_attr_cur_bus_speed
= {
314 .attr
= {.name
= "cur_bus_speed", .mode
= S_IFREG
| S_IRUGO
},
315 .show
= cur_bus_speed_read_file
,
318 static ssize_t
test_write_file(struct pci_slot
*pci_slot
, const char *buf
,
321 struct hotplug_slot
*slot
= pci_slot
->hotplug
;
326 ltest
= simple_strtoul (buf
, NULL
, 10);
327 test
= (u32
)(ltest
& 0xffffffff);
328 dbg ("test = %d\n", test
);
330 if (!try_module_get(slot
->ops
->owner
)) {
334 if (slot
->ops
->hardware_test
)
335 retval
= slot
->ops
->hardware_test(slot
, test
);
336 module_put(slot
->ops
->owner
);
344 static struct pci_slot_attribute hotplug_slot_attr_test
= {
345 .attr
= {.name
= "test", .mode
= S_IFREG
| S_IRUGO
| S_IWUSR
},
346 .store
= test_write_file
349 static int has_power_file(struct pci_slot
*pci_slot
)
351 struct hotplug_slot
*slot
= pci_slot
->hotplug
;
352 if ((!slot
) || (!slot
->ops
))
354 if ((slot
->ops
->enable_slot
) ||
355 (slot
->ops
->disable_slot
) ||
356 (slot
->ops
->get_power_status
))
361 static int has_attention_file(struct pci_slot
*pci_slot
)
363 struct hotplug_slot
*slot
= pci_slot
->hotplug
;
364 if ((!slot
) || (!slot
->ops
))
366 if ((slot
->ops
->set_attention_status
) ||
367 (slot
->ops
->get_attention_status
))
372 static int has_latch_file(struct pci_slot
*pci_slot
)
374 struct hotplug_slot
*slot
= pci_slot
->hotplug
;
375 if ((!slot
) || (!slot
->ops
))
377 if (slot
->ops
->get_latch_status
)
382 static int has_adapter_file(struct pci_slot
*pci_slot
)
384 struct hotplug_slot
*slot
= pci_slot
->hotplug
;
385 if ((!slot
) || (!slot
->ops
))
387 if (slot
->ops
->get_adapter_status
)
392 static int has_max_bus_speed_file(struct pci_slot
*pci_slot
)
394 struct hotplug_slot
*slot
= pci_slot
->hotplug
;
395 if ((!slot
) || (!slot
->ops
))
397 if (slot
->ops
->get_max_bus_speed
)
402 static int has_cur_bus_speed_file(struct pci_slot
*pci_slot
)
404 struct hotplug_slot
*slot
= pci_slot
->hotplug
;
405 if ((!slot
) || (!slot
->ops
))
407 if (slot
->ops
->get_cur_bus_speed
)
412 static int has_test_file(struct pci_slot
*pci_slot
)
414 struct hotplug_slot
*slot
= pci_slot
->hotplug
;
415 if ((!slot
) || (!slot
->ops
))
417 if (slot
->ops
->hardware_test
)
422 static int fs_add_slot(struct pci_slot
*slot
)
426 if (has_power_file(slot
) == 0) {
427 retval
= sysfs_create_file(&slot
->kobj
, &hotplug_slot_attr_power
.attr
);
432 if (has_attention_file(slot
) == 0) {
433 retval
= sysfs_create_file(&slot
->kobj
,
434 &hotplug_slot_attr_attention
.attr
);
439 if (has_latch_file(slot
) == 0) {
440 retval
= sysfs_create_file(&slot
->kobj
,
441 &hotplug_slot_attr_latch
.attr
);
446 if (has_adapter_file(slot
) == 0) {
447 retval
= sysfs_create_file(&slot
->kobj
,
448 &hotplug_slot_attr_presence
.attr
);
453 if (has_max_bus_speed_file(slot
) == 0) {
454 retval
= sysfs_create_file(&slot
->kobj
,
455 &hotplug_slot_attr_max_bus_speed
.attr
);
460 if (has_cur_bus_speed_file(slot
) == 0) {
461 retval
= sysfs_create_file(&slot
->kobj
,
462 &hotplug_slot_attr_cur_bus_speed
.attr
);
467 if (has_test_file(slot
) == 0) {
468 retval
= sysfs_create_file(&slot
->kobj
,
469 &hotplug_slot_attr_test
.attr
);
477 if (has_cur_bus_speed_file(slot
) == 0)
478 sysfs_remove_file(&slot
->kobj
, &hotplug_slot_attr_cur_bus_speed
.attr
);
481 if (has_max_bus_speed_file(slot
) == 0)
482 sysfs_remove_file(&slot
->kobj
, &hotplug_slot_attr_max_bus_speed
.attr
);
485 if (has_adapter_file(slot
) == 0)
486 sysfs_remove_file(&slot
->kobj
, &hotplug_slot_attr_presence
.attr
);
489 if (has_latch_file(slot
) == 0)
490 sysfs_remove_file(&slot
->kobj
, &hotplug_slot_attr_latch
.attr
);
493 if (has_attention_file(slot
) == 0)
494 sysfs_remove_file(&slot
->kobj
, &hotplug_slot_attr_attention
.attr
);
497 if (has_power_file(slot
) == 0)
498 sysfs_remove_file(&slot
->kobj
, &hotplug_slot_attr_power
.attr
);
504 static void fs_remove_slot(struct pci_slot
*slot
)
506 if (has_power_file(slot
) == 0)
507 sysfs_remove_file(&slot
->kobj
, &hotplug_slot_attr_power
.attr
);
509 if (has_attention_file(slot
) == 0)
510 sysfs_remove_file(&slot
->kobj
, &hotplug_slot_attr_attention
.attr
);
512 if (has_latch_file(slot
) == 0)
513 sysfs_remove_file(&slot
->kobj
, &hotplug_slot_attr_latch
.attr
);
515 if (has_adapter_file(slot
) == 0)
516 sysfs_remove_file(&slot
->kobj
, &hotplug_slot_attr_presence
.attr
);
518 if (has_max_bus_speed_file(slot
) == 0)
519 sysfs_remove_file(&slot
->kobj
, &hotplug_slot_attr_max_bus_speed
.attr
);
521 if (has_cur_bus_speed_file(slot
) == 0)
522 sysfs_remove_file(&slot
->kobj
, &hotplug_slot_attr_cur_bus_speed
.attr
);
524 if (has_test_file(slot
) == 0)
525 sysfs_remove_file(&slot
->kobj
, &hotplug_slot_attr_test
.attr
);
528 static struct hotplug_slot
*get_slot_from_name (const char *name
)
530 struct hotplug_slot
*slot
;
531 struct list_head
*tmp
;
533 spin_lock(&pci_hotplug_slot_list_lock
);
534 list_for_each (tmp
, &pci_hotplug_slot_list
) {
535 slot
= list_entry (tmp
, struct hotplug_slot
, slot_list
);
536 if (strcmp(slot
->name
, name
) == 0)
541 spin_unlock(&pci_hotplug_slot_list_lock
);
546 * pci_hp_register - register a hotplug_slot with the PCI hotplug subsystem
547 * @bus: bus this slot is on
548 * @slot: pointer to the &struct hotplug_slot to register
549 * @slot_nr: slot number
551 * Registers a hotplug slot with the pci hotplug subsystem, which will allow
552 * userspace interaction to the slot.
554 * Returns 0 if successful, anything else for an error.
556 int pci_hp_register(struct hotplug_slot
*slot
, struct pci_bus
*bus
, int slot_nr
)
559 struct pci_slot
*pci_slot
;
563 if ((slot
->info
== NULL
) || (slot
->ops
== NULL
))
565 if (slot
->release
== NULL
) {
566 dbg("Why are you trying to register a hotplug slot "
567 "without a proper release function?\n");
571 /* Check if we have already registered a slot with the same name. */
572 if (get_slot_from_name(slot
->name
))
576 * No problems if we call this interface from both ACPI_PCI_SLOT
577 * driver and call it here again. If we've already created the
578 * pci_slot, the interface will simply bump the refcount.
580 pci_slot
= pci_create_slot(bus
, slot_nr
, slot
->name
);
581 if (IS_ERR(pci_slot
))
582 return PTR_ERR(pci_slot
);
584 if (pci_slot
->hotplug
) {
585 dbg("%s: already claimed\n", __func__
);
586 pci_destroy_slot(pci_slot
);
590 slot
->pci_slot
= pci_slot
;
591 pci_slot
->hotplug
= slot
;
594 * Allow pcihp drivers to override the ACPI_PCI_SLOT name.
596 if (strcmp(kobject_name(&pci_slot
->kobj
), slot
->name
)) {
597 result
= kobject_rename(&pci_slot
->kobj
, slot
->name
);
599 pci_destroy_slot(pci_slot
);
604 spin_lock(&pci_hotplug_slot_list_lock
);
605 list_add(&slot
->slot_list
, &pci_hotplug_slot_list
);
606 spin_unlock(&pci_hotplug_slot_list_lock
);
608 result
= fs_add_slot(pci_slot
);
609 kobject_uevent(&pci_slot
->kobj
, KOBJ_ADD
);
610 dbg("Added slot %s to the list\n", slot
->name
);
617 * pci_hp_deregister - deregister a hotplug_slot with the PCI hotplug subsystem
618 * @hotplug: pointer to the &struct hotplug_slot to deregister
620 * The @slot must have been registered with the pci hotplug subsystem
621 * previously with a call to pci_hp_register().
623 * Returns 0 if successful, anything else for an error.
625 int pci_hp_deregister(struct hotplug_slot
*hotplug
)
627 struct hotplug_slot
*temp
;
628 struct pci_slot
*slot
;
633 temp
= get_slot_from_name(hotplug
->name
);
637 spin_lock(&pci_hotplug_slot_list_lock
);
638 list_del(&hotplug
->slot_list
);
639 spin_unlock(&pci_hotplug_slot_list_lock
);
641 slot
= hotplug
->pci_slot
;
642 fs_remove_slot(slot
);
643 dbg("Removed slot %s from the list\n", hotplug
->name
);
645 hotplug
->release(hotplug
);
646 slot
->hotplug
= NULL
;
647 pci_destroy_slot(slot
);
653 * pci_hp_change_slot_info - changes the slot's information structure in the core
654 * @hotplug: pointer to the slot whose info has changed
655 * @info: pointer to the info copy into the slot's info structure
657 * @slot must have been registered with the pci
658 * hotplug subsystem previously with a call to pci_hp_register().
660 * Returns 0 if successful, anything else for an error.
662 int __must_check
pci_hp_change_slot_info(struct hotplug_slot
*hotplug
,
663 struct hotplug_slot_info
*info
)
665 struct pci_slot
*slot
;
666 if (!hotplug
|| !info
)
668 slot
= hotplug
->pci_slot
;
670 memcpy(hotplug
->info
, info
, sizeof(struct hotplug_slot_info
));
675 static int __init
pci_hotplug_init (void)
679 result
= cpci_hotplug_init(debug
);
681 err ("cpci_hotplug_init with error %d\n", result
);
685 info (DRIVER_DESC
" version: " DRIVER_VERSION
"\n");
691 static void __exit
pci_hotplug_exit (void)
696 module_init(pci_hotplug_init
);
697 module_exit(pci_hotplug_exit
);
699 MODULE_AUTHOR(DRIVER_AUTHOR
);
700 MODULE_DESCRIPTION(DRIVER_DESC
);
701 MODULE_LICENSE("GPL");
702 module_param(debug
, bool, 0644);
703 MODULE_PARM_DESC(debug
, "Debugging mode enabled or not");
705 EXPORT_SYMBOL_GPL(pci_hp_register
);
706 EXPORT_SYMBOL_GPL(pci_hp_deregister
);
707 EXPORT_SYMBOL_GPL(pci_hp_change_slot_info
);