2 * CompactPCI Hot Plug Driver
4 * Copyright (c) 2002 SOMA Networks, Inc.
5 * Copyright (c) 2001 Greg Kroah-Hartman (greg@kroah.com)
6 * Copyright (c) 2001 IBM Corp.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or (at
13 * your option) any later version.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
18 * NON INFRINGEMENT. See the GNU General Public License for more
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 * Send feedback to <scottm@somanetworks.com>
28 #include <linux/config.h>
29 #include <linux/module.h>
30 #include <linux/kernel.h>
31 #include <linux/slab.h>
32 #include <linux/pci.h>
33 #include <linux/init.h>
34 #include <linux/interrupt.h>
35 #include <linux/smp_lock.h>
36 #include "pci_hotplug.h"
37 #include "cpci_hotplug.h"
39 #define DRIVER_VERSION "0.2"
40 #define DRIVER_AUTHOR "Scott Murray <scottm@somanetworks.com>"
41 #define DRIVER_DESC "CompactPCI Hot Plug Core"
43 #if !defined(CONFIG_HOTPLUG_CPCI_MODULE)
44 #define MY_NAME "cpci_hotplug"
46 #define MY_NAME THIS_MODULE->name
49 #define dbg(format, arg...) \
52 printk (KERN_DEBUG "%s: " format "\n", \
55 #define err(format, arg...) printk(KERN_ERR "%s: " format "\n", MY_NAME , ## arg)
56 #define info(format, arg...) printk(KERN_INFO "%s: " format "\n", MY_NAME , ## arg)
57 #define warn(format, arg...) printk(KERN_WARNING "%s: " format "\n", MY_NAME , ## arg)
60 static spinlock_t list_lock
;
61 static LIST_HEAD(slot_list
);
64 static struct cpci_hp_controller
*controller
;
65 static struct semaphore event_semaphore
; /* mutex for process loop (up if something to process) */
66 static struct semaphore thread_exit
; /* guard ensure thread has exited before calling it quits */
67 static int thread_finished
= 1;
69 static int enable_slot(struct hotplug_slot
*slot
);
70 static int disable_slot(struct hotplug_slot
*slot
);
71 static int set_attention_status(struct hotplug_slot
*slot
, u8 value
);
72 static int get_power_status(struct hotplug_slot
*slot
, u8
* value
);
73 static int get_attention_status(struct hotplug_slot
*slot
, u8
* value
);
74 static int get_latch_status(struct hotplug_slot
*slot
, u8
* value
);
75 static int get_adapter_status(struct hotplug_slot
*slot
, u8
* value
);
77 static struct hotplug_slot_ops cpci_hotplug_slot_ops
= {
79 .enable_slot
= enable_slot
,
80 .disable_slot
= disable_slot
,
81 .set_attention_status
= set_attention_status
,
82 .hardware_test
= NULL
,
83 .get_power_status
= get_power_status
,
84 .get_attention_status
= get_attention_status
,
85 .get_latch_status
= get_latch_status
,
86 .get_adapter_status
= get_adapter_status
,
89 /* Inline functions to check the sanity of a pointer that is passed to us */
91 slot_paranoia_check(struct slot
*slot
, const char *function
)
94 dbg("%s - slot == NULL", function
);
97 if(slot
->magic
!= SLOT_MAGIC
) {
98 dbg("%s - bad magic number for slot", function
);
101 if(!slot
->hotplug_slot
) {
102 dbg("%s - slot->hotplug_slot == NULL!", function
);
108 static inline struct slot
*
109 get_slot(struct hotplug_slot
*hotplug_slot
, const char *function
)
114 dbg("%s - hotplug_slot == NULL", function
);
118 slot
= (struct slot
*) hotplug_slot
->private;
119 if(slot_paranoia_check(slot
, function
))
125 update_latch_status(struct hotplug_slot
*hotplug_slot
, u8 value
)
127 struct hotplug_slot_info info
;
129 if(!(hotplug_slot
&& hotplug_slot
->info
))
131 memcpy(&info
, hotplug_slot
->info
, sizeof(struct hotplug_slot_info
));
132 info
.latch_status
= value
;
133 return pci_hp_change_slot_info(hotplug_slot
, &info
);
137 update_adapter_status(struct hotplug_slot
*hotplug_slot
, u8 value
)
139 struct hotplug_slot_info info
;
141 if(!(hotplug_slot
&& hotplug_slot
->info
))
143 memcpy(&info
, hotplug_slot
->info
, sizeof(struct hotplug_slot_info
));
144 info
.adapter_status
= value
;
145 return pci_hp_change_slot_info(hotplug_slot
, &info
);
149 enable_slot(struct hotplug_slot
*hotplug_slot
)
151 struct slot
*slot
= get_slot(hotplug_slot
, __FUNCTION__
);
157 dbg("%s - physical_slot = %s", __FUNCTION__
, hotplug_slot
->name
);
159 if(controller
->ops
->set_power
) {
160 retval
= controller
->ops
->set_power(slot
, 1);
167 disable_slot(struct hotplug_slot
*hotplug_slot
)
169 struct slot
*slot
= get_slot(hotplug_slot
, __FUNCTION__
);
175 dbg("%s - physical_slot = %s", __FUNCTION__
, hotplug_slot
->name
);
177 /* Unconfigure device */
178 dbg("%s - unconfiguring slot %s",
179 __FUNCTION__
, slot
->hotplug_slot
->name
);
180 if((retval
= cpci_unconfigure_slot(slot
))) {
181 err("%s - could not unconfigure slot %s",
182 __FUNCTION__
, slot
->hotplug_slot
->name
);
185 dbg("%s - finished unconfiguring slot %s",
186 __FUNCTION__
, slot
->hotplug_slot
->name
);
188 /* Clear EXT (by setting it) */
189 if(cpci_clear_ext(slot
)) {
190 err("%s - could not clear EXT for slot %s",
191 __FUNCTION__
, slot
->hotplug_slot
->name
);
196 if(controller
->ops
->set_power
) {
197 retval
= controller
->ops
->set_power(slot
, 0);
200 if(update_adapter_status(slot
->hotplug_slot
, 0)) {
201 warn("failure to update adapter file");
204 slot
->extracting
= 0;
210 cpci_get_power_status(struct slot
*slot
)
214 if(controller
->ops
->get_power
) {
215 power
= controller
->ops
->get_power(slot
);
221 get_power_status(struct hotplug_slot
*hotplug_slot
, u8
* value
)
223 struct slot
*slot
= get_slot(hotplug_slot
, __FUNCTION__
);
227 *value
= cpci_get_power_status(slot
);
232 get_attention_status(struct hotplug_slot
*hotplug_slot
, u8
* value
)
234 struct slot
*slot
= get_slot(hotplug_slot
, __FUNCTION__
);
238 *value
= cpci_get_attention_status(slot
);
243 set_attention_status(struct hotplug_slot
*hotplug_slot
, u8 status
)
245 struct slot
*slot
= get_slot(hotplug_slot
, __FUNCTION__
);
251 cpci_set_attention_status(slot
, 0);
256 cpci_set_attention_status(slot
, 1);
264 get_latch_status(struct hotplug_slot
*hotplug_slot
, u8
* value
)
266 if(hotplug_slot
== NULL
|| hotplug_slot
->info
== NULL
)
268 *value
= hotplug_slot
->info
->latch_status
;
273 get_adapter_status(struct hotplug_slot
*hotplug_slot
, u8
* value
)
275 if(hotplug_slot
== NULL
|| hotplug_slot
->info
== NULL
)
277 *value
= hotplug_slot
->info
->adapter_status
;
281 static void release_slot(struct hotplug_slot
*hotplug_slot
)
283 struct slot
*slot
= get_slot(hotplug_slot
, __FUNCTION__
);
288 kfree(slot
->hotplug_slot
->info
);
289 kfree(slot
->hotplug_slot
->name
);
290 kfree(slot
->hotplug_slot
);
294 #define SLOT_NAME_SIZE 6
296 make_slot_name(struct slot
*slot
)
298 snprintf(slot
->hotplug_slot
->name
,
299 SLOT_NAME_SIZE
, "%02x:%02x", slot
->bus
->number
, slot
->number
);
303 cpci_hp_register_bus(struct pci_bus
*bus
, u8 first
, u8 last
)
306 struct hotplug_slot
*hotplug_slot
;
307 struct hotplug_slot_info
*info
;
312 if(!(controller
&& bus
)) {
320 * Create a structure for each slot, and register that slot
321 * with the pci_hotplug subsystem.
323 for (i
= first
; i
<= last
; ++i
) {
324 slot
= kmalloc(sizeof (struct slot
), GFP_KERNEL
);
327 memset(slot
, 0, sizeof (struct slot
));
330 kmalloc(sizeof (struct hotplug_slot
), GFP_KERNEL
);
335 memset(hotplug_slot
, 0, sizeof (struct hotplug_slot
));
336 slot
->hotplug_slot
= hotplug_slot
;
338 info
= kmalloc(sizeof (struct hotplug_slot_info
), GFP_KERNEL
);
344 memset(info
, 0, sizeof (struct hotplug_slot_info
));
345 hotplug_slot
->info
= info
;
347 name
= kmalloc(SLOT_NAME_SIZE
, GFP_KERNEL
);
354 hotplug_slot
->name
= name
;
356 slot
->magic
= SLOT_MAGIC
;
359 slot
->devfn
= PCI_DEVFN(i
, 0);
361 hotplug_slot
->private = slot
;
362 hotplug_slot
->release
= &release_slot
;
363 make_slot_name(slot
);
364 hotplug_slot
->ops
= &cpci_hotplug_slot_ops
;
367 * Initialize the slot info structure with some known
370 dbg("initializing slot %s", slot
->hotplug_slot
->name
);
371 info
->power_status
= cpci_get_power_status(slot
);
372 info
->attention_status
= cpci_get_attention_status(slot
);
374 dbg("registering slot %s", slot
->hotplug_slot
->name
);
375 status
= pci_hp_register(slot
->hotplug_slot
);
377 err("pci_hp_register failed with error %d", status
);
385 /* Add slot to our internal list */
386 spin_lock(&list_lock
);
387 list_add(&slot
->slot_list
, &slot_list
);
389 spin_unlock(&list_lock
);
395 cpci_hp_unregister_bus(struct pci_bus
*bus
)
398 struct list_head
*tmp
;
399 struct list_head
*next
;
406 spin_lock(&list_lock
);
408 spin_unlock(&list_lock
);
411 list_for_each_safe(tmp
, next
, &slot_list
) {
412 slot
= list_entry(tmp
, struct slot
, slot_list
);
413 if(slot
->bus
== bus
) {
414 dbg("deregistering slot %s", slot
->hotplug_slot
->name
);
415 status
= pci_hp_deregister(slot
->hotplug_slot
);
417 err("pci_hp_deregister failed with error %d",
422 list_del(&slot
->slot_list
);
426 spin_unlock(&list_lock
);
431 cpci_find_slot(struct pci_bus
*bus
, unsigned int devfn
)
435 struct list_head
*tmp
;
441 spin_lock(&list_lock
);
443 spin_unlock(&list_lock
);
447 list_for_each(tmp
, &slot_list
) {
448 slot
= list_entry(tmp
, struct slot
, slot_list
);
449 if(slot
->bus
== bus
&& slot
->devfn
== devfn
) {
454 spin_unlock(&list_lock
);
458 /* This is the interrupt mode interrupt handler */
460 cpci_hp_intr(int irq
, void *data
, struct pt_regs
*regs
)
462 dbg("entered cpci_hp_intr");
464 /* Check to see if it was our interrupt */
465 if((controller
->irq_flags
& SA_SHIRQ
) &&
466 !controller
->ops
->check_irq(controller
->dev_id
)) {
467 dbg("exited cpci_hp_intr, not our interrupt");
471 /* Disable ENUM interrupt */
472 controller
->ops
->disable_irq();
474 /* Trigger processing by the event thread */
475 dbg("Signal event_semaphore");
476 up(&event_semaphore
);
477 dbg("exited cpci_hp_intr");
482 * According to PICMG 2.12 R2.0, section 6.3.2, upon
483 * initialization, the system driver shall clear the
484 * INS bits of the cold-inserted devices.
490 struct list_head
*tmp
;
493 dbg("%s - enter", __FUNCTION__
);
494 spin_lock(&list_lock
);
496 spin_unlock(&list_lock
);
499 list_for_each(tmp
, &slot_list
) {
500 slot
= list_entry(tmp
, struct slot
, slot_list
);
501 dbg("%s - looking at slot %s",
502 __FUNCTION__
, slot
->hotplug_slot
->name
);
503 if(cpci_check_and_clear_ins(slot
)) {
504 dbg("%s - cleared INS for slot %s",
505 __FUNCTION__
, slot
->hotplug_slot
->name
);
506 dev
= pci_find_slot(slot
->bus
->number
, PCI_DEVFN(slot
->number
, 0));
508 if(update_adapter_status(slot
->hotplug_slot
, 1)) {
509 warn("failure to update adapter file");
511 if(update_latch_status(slot
->hotplug_slot
, 1)) {
512 warn("failure to update latch file");
516 err("%s - no driver attached to device in slot %s",
517 __FUNCTION__
, slot
->hotplug_slot
->name
);
521 spin_unlock(&list_lock
);
522 dbg("%s - exit", __FUNCTION__
);
530 struct list_head
*tmp
;
534 spin_lock(&list_lock
);
536 spin_unlock(&list_lock
);
537 err("no slots registered, shutting down");
540 extracted
= inserted
= 0;
541 list_for_each(tmp
, &slot_list
) {
542 slot
= list_entry(tmp
, struct slot
, slot_list
);
543 dbg("%s - looking at slot %s",
544 __FUNCTION__
, slot
->hotplug_slot
->name
);
545 if(cpci_check_and_clear_ins(slot
)) {
548 /* Some broken hardware (e.g. PLX 9054AB) asserts ENUM# twice... */
550 warn("slot %s already inserted", slot
->hotplug_slot
->name
);
555 /* Process insertion */
556 dbg("%s - slot %s inserted",
557 __FUNCTION__
, slot
->hotplug_slot
->name
);
560 hs_csr
= cpci_get_hs_csr(slot
);
561 dbg("%s - slot %s HS_CSR (1) = %04x",
562 __FUNCTION__
, slot
->hotplug_slot
->name
, hs_csr
);
564 /* Configure device */
565 dbg("%s - configuring slot %s",
566 __FUNCTION__
, slot
->hotplug_slot
->name
);
567 if(cpci_configure_slot(slot
)) {
568 err("%s - could not configure slot %s",
569 __FUNCTION__
, slot
->hotplug_slot
->name
);
572 dbg("%s - finished configuring slot %s",
573 __FUNCTION__
, slot
->hotplug_slot
->name
);
576 hs_csr
= cpci_get_hs_csr(slot
);
577 dbg("%s - slot %s HS_CSR (2) = %04x",
578 __FUNCTION__
, slot
->hotplug_slot
->name
, hs_csr
);
580 if(update_latch_status(slot
->hotplug_slot
, 1)) {
581 warn("failure to update latch file");
584 if(update_adapter_status(slot
->hotplug_slot
, 1)) {
585 warn("failure to update adapter file");
591 hs_csr
= cpci_get_hs_csr(slot
);
592 dbg("%s - slot %s HS_CSR (3) = %04x",
593 __FUNCTION__
, slot
->hotplug_slot
->name
, hs_csr
);
596 } else if(cpci_check_ext(slot
)) {
599 /* Process extraction request */
600 dbg("%s - slot %s extracted",
601 __FUNCTION__
, slot
->hotplug_slot
->name
);
604 hs_csr
= cpci_get_hs_csr(slot
);
605 dbg("%s - slot %s HS_CSR = %04x",
606 __FUNCTION__
, slot
->hotplug_slot
->name
, hs_csr
);
608 if(!slot
->extracting
) {
609 if(update_latch_status(slot
->hotplug_slot
, 0)) {
610 warn("failure to update latch file");
612 slot
->extracting
= 1;
617 spin_unlock(&list_lock
);
618 if(inserted
|| extracted
) {
622 err("cannot find ENUM# source, shutting down");
627 /* This is the interrupt mode worker thread body */
629 event_thread(void *data
)
633 struct list_head
*tmp
;
636 daemonize("cpci_hp_eventd");
639 dbg("%s - event thread started", __FUNCTION__
);
641 dbg("event thread sleeping");
642 down_interruptible(&event_semaphore
);
643 dbg("event thread woken, thread_finished = %d",
645 if(thread_finished
|| signal_pending(current
))
647 while(controller
->ops
->query_enum()) {
650 /* Give userspace a chance to handle extraction */
651 set_current_state(TASK_INTERRUPTIBLE
);
652 schedule_timeout(HZ
/ 2);
654 dbg("%s - error checking slots", __FUNCTION__
);
659 /* Check for someone yanking out a board */
660 list_for_each(tmp
, &slot_list
) {
661 slot
= list_entry(tmp
, struct slot
, slot_list
);
662 if(slot
->extracting
) {
664 * Hmmm, we're likely hosed at this point, should we
665 * bother trying to tell the driver or not?
667 err("card in slot %s was improperly removed",
668 slot
->hotplug_slot
->name
);
669 if(update_adapter_status(slot
->hotplug_slot
, 0)) {
670 warn("failure to update adapter file");
672 slot
->extracting
= 0;
676 /* Re-enable ENUM# interrupt */
677 dbg("%s - re-enabling irq", __FUNCTION__
);
678 controller
->ops
->enable_irq();
681 dbg("%s - event thread signals exit", __FUNCTION__
);
686 /* This is the polling mode worker thread body */
688 poll_thread(void *data
)
692 struct list_head
*tmp
;
695 daemonize("cpci_hp_polld");
699 if(thread_finished
|| signal_pending(current
))
702 while(controller
->ops
->query_enum()) {
705 /* Give userspace a chance to handle extraction */
706 set_current_state(TASK_INTERRUPTIBLE
);
707 schedule_timeout(HZ
/ 2);
709 dbg("%s - error checking slots", __FUNCTION__
);
714 /* Check for someone yanking out a board */
715 list_for_each(tmp
, &slot_list
) {
716 slot
= list_entry(tmp
, struct slot
, slot_list
);
717 if(slot
->extracting
) {
719 * Hmmm, we're likely hosed at this point, should we
720 * bother trying to tell the driver or not?
722 err("card in slot %s was improperly removed",
723 slot
->hotplug_slot
->name
);
724 if(update_adapter_status(slot
->hotplug_slot
, 0)) {
725 warn("failure to update adapter file");
727 slot
->extracting
= 0;
731 set_current_state(TASK_INTERRUPTIBLE
);
732 schedule_timeout(HZ
/ 10);
734 dbg("poll thread signals exit");
740 cpci_start_thread(void)
744 /* initialize our semaphores */
745 init_MUTEX_LOCKED(&event_semaphore
);
746 init_MUTEX_LOCKED(&thread_exit
);
749 if(controller
->irq
) {
750 pid
= kernel_thread(event_thread
, 0, 0);
752 pid
= kernel_thread(poll_thread
, 0, 0);
755 err("Can't start up our thread");
758 dbg("Our thread pid = %d", pid
);
763 cpci_stop_thread(void)
766 dbg("thread finish command given");
767 if(controller
->irq
) {
768 up(&event_semaphore
);
770 dbg("wait for thread to exit");
775 cpci_hp_register_controller(struct cpci_hp_controller
*new_controller
)
780 controller
= new_controller
;
781 if(controller
->irq
) {
782 if(request_irq(controller
->irq
,
784 controller
->irq_flags
,
785 MY_NAME
, controller
->dev_id
)) {
786 err("Can't get irq %d for the hotplug cPCI controller", controller
->irq
);
789 dbg("%s - acquired controller irq %d", __FUNCTION__
,
793 err("cPCI hotplug controller already registered");
800 cpci_hp_unregister_controller(struct cpci_hp_controller
*old_controller
)
805 if(!thread_finished
) {
808 if(controller
->irq
) {
809 free_irq(controller
->irq
, controller
->dev_id
);
821 static int first
= 1;
824 dbg("%s - enter", __FUNCTION__
);
829 spin_lock(&list_lock
);
831 spin_unlock(&list_lock
);
834 spin_unlock(&list_lock
);
837 status
= init_slots();
844 status
= cpci_start_thread();
848 dbg("%s - thread started", __FUNCTION__
);
850 if(controller
->irq
) {
851 /* Start enum interrupt processing */
852 dbg("%s - enabling irq", __FUNCTION__
);
853 controller
->ops
->enable_irq();
855 dbg("%s - exit", __FUNCTION__
);
866 if(controller
->irq
) {
867 /* Stop enum interrupt processing */
868 dbg("%s - disabling irq", __FUNCTION__
);
869 controller
->ops
->disable_irq();
878 struct list_head
*tmp
;
882 * Unregister all of our slots with the pci_hotplug subsystem,
883 * and free up all memory that we had allocated.
885 spin_lock(&list_lock
);
889 list_for_each(tmp
, &slot_list
) {
890 slot
= list_entry(tmp
, struct slot
, slot_list
);
891 list_del(&slot
->slot_list
);
892 pci_hp_deregister(slot
->hotplug_slot
);
893 kfree(slot
->hotplug_slot
->info
);
894 kfree(slot
->hotplug_slot
->name
);
895 kfree(slot
->hotplug_slot
);
899 spin_unlock(&list_lock
);
904 cpci_hotplug_init(int debug
)
906 spin_lock_init(&list_lock
);
909 info(DRIVER_DESC
" version: " DRIVER_VERSION
);
914 cpci_hotplug_exit(void)
917 * Clean everything up.
923 EXPORT_SYMBOL_GPL(cpci_hp_register_controller
);
924 EXPORT_SYMBOL_GPL(cpci_hp_unregister_controller
);
925 EXPORT_SYMBOL_GPL(cpci_hp_register_bus
);
926 EXPORT_SYMBOL_GPL(cpci_hp_unregister_bus
);
927 EXPORT_SYMBOL_GPL(cpci_find_slot
);
928 EXPORT_SYMBOL_GPL(cpci_hp_start
);
929 EXPORT_SYMBOL_GPL(cpci_hp_stop
);