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/moduleparam.h>
33 #include <linux/kernel.h>
34 #include <linux/types.h>
35 #include <linux/list.h>
36 #include <linux/pagemap.h>
37 #include <linux/slab.h>
38 #include <linux/smp_lock.h>
39 #include <linux/init.h>
40 #include <linux/mount.h>
41 #include <linux/namei.h>
42 #include <linux/pci.h>
43 #include <asm/uaccess.h>
44 #include <linux/kobject.h>
45 #include <linux/sysfs.h>
46 #include "pci_hotplug.h"
49 #define MY_NAME "pci_hotplug"
51 #define dbg(fmt, arg...) do { if (debug) printk(KERN_DEBUG "%s: %s: " fmt , MY_NAME , __FUNCTION__ , ## arg); } while (0)
52 #define err(format, arg...) printk(KERN_ERR "%s: " format , MY_NAME , ## arg)
53 #define info(format, arg...) printk(KERN_INFO "%s: " format , MY_NAME , ## arg)
54 #define warn(format, arg...) printk(KERN_WARNING "%s: " format , MY_NAME , ## arg)
60 #define DRIVER_VERSION "0.5"
61 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Scott Murray <scottm@somanetworks.com>"
62 #define DRIVER_DESC "PCI Hot Plug PCI Core"
65 //////////////////////////////////////////////////////////////////
67 static LIST_HEAD(pci_hotplug_slot_list
);
69 struct subsystem pci_hotplug_slots_subsys
;
71 static ssize_t
hotplug_slot_attr_show(struct kobject
*kobj
,
72 struct attribute
*attr
, char *buf
)
74 struct hotplug_slot
*slot
= to_hotplug_slot(kobj
);
75 struct hotplug_slot_attribute
*attribute
= to_hotplug_attr(attr
);
76 return attribute
->show
? attribute
->show(slot
, buf
) : 0;
79 static ssize_t
hotplug_slot_attr_store(struct kobject
*kobj
,
80 struct attribute
*attr
, const char *buf
, size_t len
)
82 struct hotplug_slot
*slot
= to_hotplug_slot(kobj
);
83 struct hotplug_slot_attribute
*attribute
= to_hotplug_attr(attr
);
84 return attribute
->store
? attribute
->store(slot
, buf
, len
) : 0;
87 static struct sysfs_ops hotplug_slot_sysfs_ops
= {
88 .show
= hotplug_slot_attr_show
,
89 .store
= hotplug_slot_attr_store
,
92 static void hotplug_slot_release(struct kobject
*kobj
)
94 struct hotplug_slot
*slot
= to_hotplug_slot(kobj
);
99 static struct kobj_type hotplug_slot_ktype
= {
100 .sysfs_ops
= &hotplug_slot_sysfs_ops
,
101 .release
= &hotplug_slot_release
,
104 decl_subsys_name(pci_hotplug_slots
, slots
, &hotplug_slot_ktype
, NULL
);
106 /* these strings match up with the values in pci_bus_speed */
107 static char *pci_bus_speed_strings
[] = {
108 "33 MHz PCI", /* 0x00 */
109 "66 MHz PCI", /* 0x01 */
110 "66 MHz PCIX", /* 0x02 */
111 "100 MHz PCIX", /* 0x03 */
112 "133 MHz PCIX", /* 0x04 */
117 "66 MHz PCIX 266", /* 0x09 */
118 "100 MHz PCIX 266", /* 0x0a */
119 "133 MHz PCIX 266", /* 0x0b */
125 "66 MHz PCIX 533", /* 0x11 */
126 "100 MHz PCIX 533", /* 0x12 */
127 "133 MHz PCIX 533", /* 0x13 */
128 "25 GBps PCI-E", /* 0x14 */
131 #ifdef CONFIG_HOTPLUG_PCI_CPCI
132 extern int cpci_hotplug_init(int debug
);
133 extern void cpci_hotplug_exit(void);
135 static inline int cpci_hotplug_init(int debug
) { return 0; }
136 static inline void cpci_hotplug_exit(void) { }
139 /* Weee, fun with macros... */
140 #define GET_STATUS(name,type) \
141 static int get_##name (struct hotplug_slot *slot, type *value) \
143 struct hotplug_slot_ops *ops = slot->ops; \
145 if (try_module_get(ops->owner)) { \
146 if (ops->get_##name) \
147 retval = ops->get_##name (slot, value); \
149 *value = slot->info->name; \
150 module_put(ops->owner); \
155 GET_STATUS(power_status
, u8
)
156 GET_STATUS(attention_status
, u8
)
157 GET_STATUS(latch_status
, u8
)
158 GET_STATUS(adapter_status
, u8
)
159 GET_STATUS(address
, u32
)
160 GET_STATUS(max_bus_speed
, enum pci_bus_speed
)
161 GET_STATUS(cur_bus_speed
, enum pci_bus_speed
)
163 static ssize_t
power_read_file (struct hotplug_slot
*slot
, char *buf
)
168 retval
= get_power_status (slot
, &value
);
171 retval
= sprintf (buf
, "%d\n", value
);
176 static ssize_t
power_write_file (struct hotplug_slot
*slot
, const char *buf
,
179 unsigned long lpower
;
183 lpower
= simple_strtoul (buf
, NULL
, 10);
184 power
= (u8
)(lpower
& 0xff);
185 dbg ("power = %d\n", power
);
187 if (!try_module_get(slot
->ops
->owner
)) {
193 if (slot
->ops
->disable_slot
)
194 retval
= slot
->ops
->disable_slot(slot
);
198 if (slot
->ops
->enable_slot
)
199 retval
= slot
->ops
->enable_slot(slot
);
203 err ("Illegal value specified for power\n");
206 module_put(slot
->ops
->owner
);
214 static struct hotplug_slot_attribute hotplug_slot_attr_power
= {
215 .attr
= {.name
= "power", .mode
= S_IFREG
| S_IRUGO
| S_IWUSR
},
216 .show
= power_read_file
,
217 .store
= power_write_file
220 static ssize_t
attention_read_file (struct hotplug_slot
*slot
, char *buf
)
225 retval
= get_attention_status (slot
, &value
);
228 retval
= sprintf (buf
, "%d\n", value
);
234 static ssize_t
attention_write_file (struct hotplug_slot
*slot
, const char *buf
,
237 unsigned long lattention
;
241 lattention
= simple_strtoul (buf
, NULL
, 10);
242 attention
= (u8
)(lattention
& 0xff);
243 dbg (" - attention = %d\n", attention
);
245 if (!try_module_get(slot
->ops
->owner
)) {
249 if (slot
->ops
->set_attention_status
)
250 retval
= slot
->ops
->set_attention_status(slot
, attention
);
251 module_put(slot
->ops
->owner
);
259 static struct hotplug_slot_attribute hotplug_slot_attr_attention
= {
260 .attr
= {.name
= "attention", .mode
= S_IFREG
| S_IRUGO
| S_IWUSR
},
261 .show
= attention_read_file
,
262 .store
= attention_write_file
265 static ssize_t
latch_read_file (struct hotplug_slot
*slot
, char *buf
)
270 retval
= get_latch_status (slot
, &value
);
273 retval
= sprintf (buf
, "%d\n", value
);
279 static struct hotplug_slot_attribute hotplug_slot_attr_latch
= {
280 .attr
= {.name
= "latch", .mode
= S_IFREG
| S_IRUGO
},
281 .show
= latch_read_file
,
284 static ssize_t
presence_read_file (struct hotplug_slot
*slot
, char *buf
)
289 retval
= get_adapter_status (slot
, &value
);
292 retval
= sprintf (buf
, "%d\n", value
);
298 static struct hotplug_slot_attribute hotplug_slot_attr_presence
= {
299 .attr
= {.name
= "adapter", .mode
= S_IFREG
| S_IRUGO
},
300 .show
= presence_read_file
,
303 static ssize_t
address_read_file (struct hotplug_slot
*slot
, char *buf
)
308 retval
= get_address (slot
, &address
);
311 retval
= sprintf (buf
, "%04x:%02x:%02x\n",
312 (address
>> 16) & 0xffff,
313 (address
>> 8) & 0xff,
320 static struct hotplug_slot_attribute hotplug_slot_attr_address
= {
321 .attr
= {.name
= "address", .mode
= S_IFREG
| S_IRUGO
},
322 .show
= address_read_file
,
325 static char *unknown_speed
= "Unknown bus speed";
327 static ssize_t
max_bus_speed_read_file (struct hotplug_slot
*slot
, char *buf
)
331 enum pci_bus_speed value
;
333 retval
= get_max_bus_speed (slot
, &value
);
337 if (value
== PCI_SPEED_UNKNOWN
)
338 speed_string
= unknown_speed
;
340 speed_string
= pci_bus_speed_strings
[value
];
342 retval
= sprintf (buf
, "%s\n", speed_string
);
348 static struct hotplug_slot_attribute hotplug_slot_attr_max_bus_speed
= {
349 .attr
= {.name
= "max_bus_speed", .mode
= S_IFREG
| S_IRUGO
},
350 .show
= max_bus_speed_read_file
,
353 static ssize_t
cur_bus_speed_read_file (struct hotplug_slot
*slot
, char *buf
)
357 enum pci_bus_speed value
;
359 retval
= get_cur_bus_speed (slot
, &value
);
363 if (value
== PCI_SPEED_UNKNOWN
)
364 speed_string
= unknown_speed
;
366 speed_string
= pci_bus_speed_strings
[value
];
368 retval
= sprintf (buf
, "%s\n", speed_string
);
374 static struct hotplug_slot_attribute hotplug_slot_attr_cur_bus_speed
= {
375 .attr
= {.name
= "cur_bus_speed", .mode
= S_IFREG
| S_IRUGO
},
376 .show
= cur_bus_speed_read_file
,
379 static ssize_t
test_write_file (struct hotplug_slot
*slot
, const char *buf
,
386 ltest
= simple_strtoul (buf
, NULL
, 10);
387 test
= (u32
)(ltest
& 0xffffffff);
388 dbg ("test = %d\n", test
);
390 if (!try_module_get(slot
->ops
->owner
)) {
394 if (slot
->ops
->hardware_test
)
395 retval
= slot
->ops
->hardware_test(slot
, test
);
396 module_put(slot
->ops
->owner
);
404 static struct hotplug_slot_attribute hotplug_slot_attr_test
= {
405 .attr
= {.name
= "test", .mode
= S_IFREG
| S_IRUGO
| S_IWUSR
},
406 .store
= test_write_file
409 static int has_power_file (struct hotplug_slot
*slot
)
411 if ((!slot
) || (!slot
->ops
))
413 if ((slot
->ops
->enable_slot
) ||
414 (slot
->ops
->disable_slot
) ||
415 (slot
->ops
->get_power_status
))
420 static int has_attention_file (struct hotplug_slot
*slot
)
422 if ((!slot
) || (!slot
->ops
))
424 if ((slot
->ops
->set_attention_status
) ||
425 (slot
->ops
->get_attention_status
))
430 static int has_latch_file (struct hotplug_slot
*slot
)
432 if ((!slot
) || (!slot
->ops
))
434 if (slot
->ops
->get_latch_status
)
439 static int has_adapter_file (struct hotplug_slot
*slot
)
441 if ((!slot
) || (!slot
->ops
))
443 if (slot
->ops
->get_adapter_status
)
448 static int has_address_file (struct hotplug_slot
*slot
)
450 if ((!slot
) || (!slot
->ops
))
452 if (slot
->ops
->get_address
)
457 static int has_max_bus_speed_file (struct hotplug_slot
*slot
)
459 if ((!slot
) || (!slot
->ops
))
461 if (slot
->ops
->get_max_bus_speed
)
466 static int has_cur_bus_speed_file (struct hotplug_slot
*slot
)
468 if ((!slot
) || (!slot
->ops
))
470 if (slot
->ops
->get_cur_bus_speed
)
475 static int has_test_file (struct hotplug_slot
*slot
)
477 if ((!slot
) || (!slot
->ops
))
479 if (slot
->ops
->hardware_test
)
484 static int fs_add_slot (struct hotplug_slot
*slot
)
486 if (has_power_file(slot
) == 0)
487 sysfs_create_file(&slot
->kobj
, &hotplug_slot_attr_power
.attr
);
489 if (has_attention_file(slot
) == 0)
490 sysfs_create_file(&slot
->kobj
, &hotplug_slot_attr_attention
.attr
);
492 if (has_latch_file(slot
) == 0)
493 sysfs_create_file(&slot
->kobj
, &hotplug_slot_attr_latch
.attr
);
495 if (has_adapter_file(slot
) == 0)
496 sysfs_create_file(&slot
->kobj
, &hotplug_slot_attr_presence
.attr
);
498 if (has_address_file(slot
) == 0)
499 sysfs_create_file(&slot
->kobj
, &hotplug_slot_attr_address
.attr
);
501 if (has_max_bus_speed_file(slot
) == 0)
502 sysfs_create_file(&slot
->kobj
, &hotplug_slot_attr_max_bus_speed
.attr
);
504 if (has_cur_bus_speed_file(slot
) == 0)
505 sysfs_create_file(&slot
->kobj
, &hotplug_slot_attr_cur_bus_speed
.attr
);
507 if (has_test_file(slot
) == 0)
508 sysfs_create_file(&slot
->kobj
, &hotplug_slot_attr_test
.attr
);
513 static void fs_remove_slot (struct hotplug_slot
*slot
)
515 if (has_power_file(slot
) == 0)
516 sysfs_remove_file(&slot
->kobj
, &hotplug_slot_attr_power
.attr
);
518 if (has_attention_file(slot
) == 0)
519 sysfs_remove_file(&slot
->kobj
, &hotplug_slot_attr_attention
.attr
);
521 if (has_latch_file(slot
) == 0)
522 sysfs_remove_file(&slot
->kobj
, &hotplug_slot_attr_latch
.attr
);
524 if (has_adapter_file(slot
) == 0)
525 sysfs_remove_file(&slot
->kobj
, &hotplug_slot_attr_presence
.attr
);
527 if (has_address_file(slot
) == 0)
528 sysfs_remove_file(&slot
->kobj
, &hotplug_slot_attr_address
.attr
);
530 if (has_max_bus_speed_file(slot
) == 0)
531 sysfs_remove_file(&slot
->kobj
, &hotplug_slot_attr_max_bus_speed
.attr
);
533 if (has_cur_bus_speed_file(slot
) == 0)
534 sysfs_remove_file(&slot
->kobj
, &hotplug_slot_attr_cur_bus_speed
.attr
);
536 if (has_test_file(slot
) == 0)
537 sysfs_remove_file(&slot
->kobj
, &hotplug_slot_attr_test
.attr
);
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(slot
->name
, name
) == 0)
554 * pci_hp_register - register a hotplug_slot with the PCI hotplug subsystem
555 * @slot: pointer to the &struct hotplug_slot to register
557 * Registers a hotplug slot with the pci hotplug subsystem, which will allow
558 * userspace interaction to the slot.
560 * Returns 0 if successful, anything else for an error.
562 int pci_hp_register (struct hotplug_slot
*slot
)
568 if ((slot
->info
== NULL
) || (slot
->ops
== NULL
))
570 if (slot
->release
== NULL
) {
571 dbg("Why are you trying to register a hotplug slot"
572 "without a proper release function?\n");
576 kobject_set_name(&slot
->kobj
, "%s", slot
->name
);
577 kobj_set_kset_s(slot
, pci_hotplug_slots_subsys
);
579 /* this can fail if we have already registered a slot with the same name */
580 if (kobject_register(&slot
->kobj
)) {
581 err("Unable to register kobject");
585 list_add (&slot
->slot_list
, &pci_hotplug_slot_list
);
587 result
= fs_add_slot (slot
);
588 dbg ("Added slot %s to the list\n", slot
->name
);
593 * pci_hp_deregister - deregister a hotplug_slot with the PCI hotplug subsystem
594 * @slot: pointer to the &struct hotplug_slot to deregister
596 * The @slot must have been registered with the pci hotplug subsystem
597 * previously with a call to pci_hp_register().
599 * Returns 0 if successful, anything else for an error.
601 int pci_hp_deregister (struct hotplug_slot
*slot
)
603 struct hotplug_slot
*temp
;
608 temp
= get_slot_from_name (slot
->name
);
612 list_del (&slot
->slot_list
);
614 fs_remove_slot (slot
);
615 dbg ("Removed slot %s from the list\n", slot
->name
);
616 kobject_unregister(&slot
->kobj
);
621 * pci_hp_change_slot_info - changes the slot's information structure in the core
622 * @slot: pointer to the slot whose info has changed
623 * @info: pointer to the info copy into the slot's info structure
625 * @slot must have been registered with the pci
626 * hotplug subsystem previously with a call to pci_hp_register().
628 * Returns 0 if successful, anything else for an error.
630 int pci_hp_change_slot_info (struct hotplug_slot
*slot
, struct hotplug_slot_info
*info
)
632 if ((slot
== NULL
) || (info
== NULL
))
636 * check all fields in the info structure, and update timestamps
637 * for the files referring to the fields that have now changed.
639 if ((has_power_file(slot
) == 0) &&
640 (slot
->info
->power_status
!= info
->power_status
))
641 sysfs_update_file(&slot
->kobj
, &hotplug_slot_attr_power
.attr
);
643 if ((has_attention_file(slot
) == 0) &&
644 (slot
->info
->attention_status
!= info
->attention_status
))
645 sysfs_update_file(&slot
->kobj
, &hotplug_slot_attr_attention
.attr
);
647 if ((has_latch_file(slot
) == 0) &&
648 (slot
->info
->latch_status
!= info
->latch_status
))
649 sysfs_update_file(&slot
->kobj
, &hotplug_slot_attr_latch
.attr
);
651 if ((has_adapter_file(slot
) == 0) &&
652 (slot
->info
->adapter_status
!= info
->adapter_status
))
653 sysfs_update_file(&slot
->kobj
, &hotplug_slot_attr_presence
.attr
);
655 if ((has_address_file(slot
) == 0) &&
656 (slot
->info
->address
!= info
->address
))
657 sysfs_update_file(&slot
->kobj
, &hotplug_slot_attr_address
.attr
);
659 if ((has_max_bus_speed_file(slot
) == 0) &&
660 (slot
->info
->max_bus_speed
!= info
->max_bus_speed
))
661 sysfs_update_file(&slot
->kobj
, &hotplug_slot_attr_max_bus_speed
.attr
);
663 if ((has_cur_bus_speed_file(slot
) == 0) &&
664 (slot
->info
->cur_bus_speed
!= info
->cur_bus_speed
))
665 sysfs_update_file(&slot
->kobj
, &hotplug_slot_attr_cur_bus_speed
.attr
);
667 memcpy (slot
->info
, info
, sizeof (struct hotplug_slot_info
));
672 static int __init
pci_hotplug_init (void)
676 kset_set_kset_s(&pci_hotplug_slots_subsys
, pci_bus_type
.subsys
);
677 result
= subsystem_register(&pci_hotplug_slots_subsys
);
679 err("Register subsys with error %d\n", result
);
682 result
= cpci_hotplug_init(debug
);
684 err ("cpci_hotplug_init with error %d\n", result
);
688 info (DRIVER_DESC
" version: " DRIVER_VERSION
"\n");
692 subsystem_unregister(&pci_hotplug_slots_subsys
);
697 static void __exit
pci_hotplug_exit (void)
700 subsystem_unregister(&pci_hotplug_slots_subsys
);
703 module_init(pci_hotplug_init
);
704 module_exit(pci_hotplug_exit
);
706 MODULE_AUTHOR(DRIVER_AUTHOR
);
707 MODULE_DESCRIPTION(DRIVER_DESC
);
708 MODULE_LICENSE("GPL");
709 module_param(debug
, bool, 0644);
710 MODULE_PARM_DESC(debug
, "Debugging mode enabled or not");
712 EXPORT_SYMBOL_GPL(pci_hotplug_slots_subsys
);
713 EXPORT_SYMBOL_GPL(pci_hp_register
);
714 EXPORT_SYMBOL_GPL(pci_hp_deregister
);
715 EXPORT_SYMBOL_GPL(pci_hp_change_slot_info
);