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 #define MY_NAME "cpci_hotplug"
45 #define dbg(format, arg...) \
48 printk (KERN_DEBUG "%s: " format "\n", \
51 #define err(format, arg...) printk(KERN_ERR "%s: " format "\n", MY_NAME , ## arg)
52 #define info(format, arg...) printk(KERN_INFO "%s: " format "\n", MY_NAME , ## arg)
53 #define warn(format, arg...) printk(KERN_WARNING "%s: " format "\n", MY_NAME , ## arg)
56 static spinlock_t list_lock;
57 static LIST_HEAD(slot_list);
60 static struct cpci_hp_controller *controller;
61 static struct semaphore event_semaphore; /* mutex for process loop (up if something to process) */
62 static struct semaphore thread_exit; /* guard ensure thread has exited before calling it quits */
63 static int thread_finished = 1;
65 static int enable_slot(struct hotplug_slot *slot);
66 static int disable_slot(struct hotplug_slot *slot);
67 static int set_attention_status(struct hotplug_slot *slot, u8 value);
68 static int get_power_status(struct hotplug_slot *slot, u8 * value);
69 static int get_attention_status(struct hotplug_slot *slot, u8 * value);
71 static struct hotplug_slot_ops cpci_hotplug_slot_ops = {
73 .enable_slot = enable_slot,
74 .disable_slot = disable_slot,
75 .set_attention_status = set_attention_status,
76 .get_power_status = get_power_status,
77 .get_attention_status = get_attention_status,
81 update_latch_status(struct hotplug_slot *hotplug_slot, u8 value)
83 struct hotplug_slot_info info;
85 memcpy(&info, hotplug_slot->info, sizeof(struct hotplug_slot_info));
86 info.latch_status = value;
87 return pci_hp_change_slot_info(hotplug_slot, &info);
91 update_adapter_status(struct hotplug_slot *hotplug_slot, u8 value)
93 struct hotplug_slot_info info;
95 memcpy(&info, hotplug_slot->info, sizeof(struct hotplug_slot_info));
96 info.adapter_status = value;
97 return pci_hp_change_slot_info(hotplug_slot, &info);
101 enable_slot(struct hotplug_slot *hotplug_slot)
103 struct slot *slot = hotplug_slot->private;
106 dbg("%s - physical_slot = %s", __FUNCTION__, hotplug_slot->name);
108 if(controller->ops->set_power) {
109 retval = controller->ops->set_power(slot, 1);
116 disable_slot(struct hotplug_slot *hotplug_slot)
118 struct slot *slot = hotplug_slot->private;
121 dbg("%s - physical_slot = %s", __FUNCTION__, hotplug_slot->name);
123 /* Unconfigure device */
124 dbg("%s - unconfiguring slot %s",
125 __FUNCTION__, slot->hotplug_slot->name);
126 if((retval = cpci_unconfigure_slot(slot))) {
127 err("%s - could not unconfigure slot %s",
128 __FUNCTION__, slot->hotplug_slot->name);
131 dbg("%s - finished unconfiguring slot %s",
132 __FUNCTION__, slot->hotplug_slot->name);
134 /* Clear EXT (by setting it) */
135 if(cpci_clear_ext(slot)) {
136 err("%s - could not clear EXT for slot %s",
137 __FUNCTION__, slot->hotplug_slot->name);
142 if(controller->ops->set_power) {
143 retval = controller->ops->set_power(slot, 0);
146 if(update_adapter_status(slot->hotplug_slot, 0)) {
147 warn("failure to update adapter file");
150 slot->extracting = 0;
156 cpci_get_power_status(struct slot *slot)
160 if(controller->ops->get_power) {
161 power = controller->ops->get_power(slot);
167 get_power_status(struct hotplug_slot *hotplug_slot, u8 * value)
169 struct slot *slot = hotplug_slot->private;
171 *value = cpci_get_power_status(slot);
176 get_attention_status(struct hotplug_slot *hotplug_slot, u8 * value)
178 struct slot *slot = hotplug_slot->private;
180 *value = cpci_get_attention_status(slot);
185 set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
187 return cpci_set_attention_status(hotplug_slot->private, status);
190 static void release_slot(struct hotplug_slot *hotplug_slot)
192 struct slot *slot = hotplug_slot->private;
194 kfree(slot->hotplug_slot->info);
195 kfree(slot->hotplug_slot->name);
196 kfree(slot->hotplug_slot);
200 #define SLOT_NAME_SIZE 6
202 make_slot_name(struct slot *slot)
204 snprintf(slot->hotplug_slot->name,
205 SLOT_NAME_SIZE, "%02x:%02x", slot->bus->number, slot->number);
209 cpci_hp_register_bus(struct pci_bus *bus, u8 first, u8 last)
212 struct hotplug_slot *hotplug_slot;
213 struct hotplug_slot_info *info;
215 int status = -ENOMEM;
218 if(!(controller && bus)) {
223 * Create a structure for each slot, and register that slot
224 * with the pci_hotplug subsystem.
226 for (i = first; i <= last; ++i) {
227 slot = kmalloc(sizeof (struct slot), GFP_KERNEL);
230 memset(slot, 0, sizeof (struct slot));
233 kmalloc(sizeof (struct hotplug_slot), GFP_KERNEL);
236 memset(hotplug_slot, 0, sizeof (struct hotplug_slot));
237 slot->hotplug_slot = hotplug_slot;
239 info = kmalloc(sizeof (struct hotplug_slot_info), GFP_KERNEL);
242 memset(info, 0, sizeof (struct hotplug_slot_info));
243 hotplug_slot->info = info;
245 name = kmalloc(SLOT_NAME_SIZE, GFP_KERNEL);
248 hotplug_slot->name = name;
252 slot->devfn = PCI_DEVFN(i, 0);
254 hotplug_slot->private = slot;
255 hotplug_slot->release = &release_slot;
256 make_slot_name(slot);
257 hotplug_slot->ops = &cpci_hotplug_slot_ops;
260 * Initialize the slot info structure with some known
263 dbg("initializing slot %s", slot->hotplug_slot->name);
264 info->power_status = cpci_get_power_status(slot);
265 info->attention_status = cpci_get_attention_status(slot);
267 dbg("registering slot %s", slot->hotplug_slot->name);
268 status = pci_hp_register(slot->hotplug_slot);
270 err("pci_hp_register failed with error %d", status);
274 /* Add slot to our internal list */
275 spin_lock(&list_lock);
276 list_add(&slot->slot_list, &slot_list);
278 spin_unlock(&list_lock);
294 cpci_hp_unregister_bus(struct pci_bus *bus)
297 struct list_head *tmp;
298 struct list_head *next;
301 spin_lock(&list_lock);
303 spin_unlock(&list_lock);
306 list_for_each_safe(tmp, next, &slot_list) {
307 slot = list_entry(tmp, struct slot, slot_list);
308 if(slot->bus == bus) {
309 dbg("deregistering slot %s", slot->hotplug_slot->name);
310 status = pci_hp_deregister(slot->hotplug_slot);
312 err("pci_hp_deregister failed with error %d",
317 list_del(&slot->slot_list);
321 spin_unlock(&list_lock);
325 /* This is the interrupt mode interrupt handler */
327 cpci_hp_intr(int irq, void *data, struct pt_regs *regs)
329 dbg("entered cpci_hp_intr");
331 /* Check to see if it was our interrupt */
332 if((controller->irq_flags & SA_SHIRQ) &&
333 !controller->ops->check_irq(controller->dev_id)) {
334 dbg("exited cpci_hp_intr, not our interrupt");
338 /* Disable ENUM interrupt */
339 controller->ops->disable_irq();
341 /* Trigger processing by the event thread */
342 dbg("Signal event_semaphore");
343 up(&event_semaphore);
344 dbg("exited cpci_hp_intr");
349 * According to PICMG 2.12 R2.0, section 6.3.2, upon
350 * initialization, the system driver shall clear the
351 * INS bits of the cold-inserted devices.
357 struct list_head *tmp;
360 dbg("%s - enter", __FUNCTION__);
361 spin_lock(&list_lock);
363 spin_unlock(&list_lock);
366 list_for_each(tmp, &slot_list) {
367 slot = list_entry(tmp, struct slot, slot_list);
368 dbg("%s - looking at slot %s",
369 __FUNCTION__, slot->hotplug_slot->name);
370 if(cpci_check_and_clear_ins(slot)) {
371 dbg("%s - cleared INS for slot %s",
372 __FUNCTION__, slot->hotplug_slot->name);
373 dev = pci_find_slot(slot->bus->number, PCI_DEVFN(slot->number, 0));
375 if(update_adapter_status(slot->hotplug_slot, 1)) {
376 warn("failure to update adapter file");
378 if(update_latch_status(slot->hotplug_slot, 1)) {
379 warn("failure to update latch file");
383 err("%s - no driver attached to device in slot %s",
384 __FUNCTION__, slot->hotplug_slot->name);
388 spin_unlock(&list_lock);
389 dbg("%s - exit", __FUNCTION__);
397 struct list_head *tmp;
401 spin_lock(&list_lock);
403 spin_unlock(&list_lock);
404 err("no slots registered, shutting down");
407 extracted = inserted = 0;
408 list_for_each(tmp, &slot_list) {
409 slot = list_entry(tmp, struct slot, slot_list);
410 dbg("%s - looking at slot %s",
411 __FUNCTION__, slot->hotplug_slot->name);
412 if(cpci_check_and_clear_ins(slot)) {
415 /* Some broken hardware (e.g. PLX 9054AB) asserts ENUM# twice... */
417 warn("slot %s already inserted", slot->hotplug_slot->name);
422 /* Process insertion */
423 dbg("%s - slot %s inserted",
424 __FUNCTION__, slot->hotplug_slot->name);
427 hs_csr = cpci_get_hs_csr(slot);
428 dbg("%s - slot %s HS_CSR (1) = %04x",
429 __FUNCTION__, slot->hotplug_slot->name, hs_csr);
431 /* Configure device */
432 dbg("%s - configuring slot %s",
433 __FUNCTION__, slot->hotplug_slot->name);
434 if(cpci_configure_slot(slot)) {
435 err("%s - could not configure slot %s",
436 __FUNCTION__, slot->hotplug_slot->name);
439 dbg("%s - finished configuring slot %s",
440 __FUNCTION__, slot->hotplug_slot->name);
443 hs_csr = cpci_get_hs_csr(slot);
444 dbg("%s - slot %s HS_CSR (2) = %04x",
445 __FUNCTION__, slot->hotplug_slot->name, hs_csr);
447 if(update_latch_status(slot->hotplug_slot, 1)) {
448 warn("failure to update latch file");
451 if(update_adapter_status(slot->hotplug_slot, 1)) {
452 warn("failure to update adapter file");
458 hs_csr = cpci_get_hs_csr(slot);
459 dbg("%s - slot %s HS_CSR (3) = %04x",
460 __FUNCTION__, slot->hotplug_slot->name, hs_csr);
463 } else if(cpci_check_ext(slot)) {
466 /* Process extraction request */
467 dbg("%s - slot %s extracted",
468 __FUNCTION__, slot->hotplug_slot->name);
471 hs_csr = cpci_get_hs_csr(slot);
472 dbg("%s - slot %s HS_CSR = %04x",
473 __FUNCTION__, slot->hotplug_slot->name, hs_csr);
475 if(!slot->extracting) {
476 if(update_latch_status(slot->hotplug_slot, 0)) {
477 warn("failure to update latch file");
479 slot->extracting = 1;
484 spin_unlock(&list_lock);
485 if(inserted || extracted) {
489 err("cannot find ENUM# source, shutting down");
494 /* This is the interrupt mode worker thread body */
496 event_thread(void *data)
500 struct list_head *tmp;
503 daemonize("cpci_hp_eventd");
506 dbg("%s - event thread started", __FUNCTION__);
508 dbg("event thread sleeping");
509 down_interruptible(&event_semaphore);
510 dbg("event thread woken, thread_finished = %d",
512 if(thread_finished || signal_pending(current))
514 while(controller->ops->query_enum()) {
517 /* Give userspace a chance to handle extraction */
518 set_current_state(TASK_INTERRUPTIBLE);
519 schedule_timeout(HZ / 2);
521 dbg("%s - error checking slots", __FUNCTION__);
526 /* Check for someone yanking out a board */
527 list_for_each(tmp, &slot_list) {
528 slot = list_entry(tmp, struct slot, slot_list);
529 if(slot->extracting) {
531 * Hmmm, we're likely hosed at this point, should we
532 * bother trying to tell the driver or not?
534 err("card in slot %s was improperly removed",
535 slot->hotplug_slot->name);
536 if(update_adapter_status(slot->hotplug_slot, 0)) {
537 warn("failure to update adapter file");
539 slot->extracting = 0;
543 /* Re-enable ENUM# interrupt */
544 dbg("%s - re-enabling irq", __FUNCTION__);
545 controller->ops->enable_irq();
548 dbg("%s - event thread signals exit", __FUNCTION__);
553 /* This is the polling mode worker thread body */
555 poll_thread(void *data)
559 struct list_head *tmp;
562 daemonize("cpci_hp_polld");
566 if(thread_finished || signal_pending(current))
569 while(controller->ops->query_enum()) {
572 /* Give userspace a chance to handle extraction */
573 set_current_state(TASK_INTERRUPTIBLE);
574 schedule_timeout(HZ / 2);
576 dbg("%s - error checking slots", __FUNCTION__);
581 /* Check for someone yanking out a board */
582 list_for_each(tmp, &slot_list) {
583 slot = list_entry(tmp, struct slot, slot_list);
584 if(slot->extracting) {
586 * Hmmm, we're likely hosed at this point, should we
587 * bother trying to tell the driver or not?
589 err("card in slot %s was improperly removed",
590 slot->hotplug_slot->name);
591 if(update_adapter_status(slot->hotplug_slot, 0)) {
592 warn("failure to update adapter file");
594 slot->extracting = 0;
598 set_current_state(TASK_INTERRUPTIBLE);
599 schedule_timeout(HZ / 10);
601 dbg("poll thread signals exit");
607 cpci_start_thread(void)
611 /* initialize our semaphores */
612 init_MUTEX_LOCKED(&event_semaphore);
613 init_MUTEX_LOCKED(&thread_exit);
616 if(controller->irq) {
617 pid = kernel_thread(event_thread, NULL, 0);
619 pid = kernel_thread(poll_thread, NULL, 0);
622 err("Can't start up our thread");
625 dbg("Our thread pid = %d", pid);
630 cpci_stop_thread(void)
633 dbg("thread finish command given");
634 if(controller->irq) {
635 up(&event_semaphore);
637 dbg("wait for thread to exit");
642 cpci_hp_register_controller(struct cpci_hp_controller *new_controller)
647 controller = new_controller;
648 if(controller->irq) {
649 if(request_irq(controller->irq,
651 controller->irq_flags,
652 MY_NAME, controller->dev_id)) {
653 err("Can't get irq %d for the hotplug cPCI controller", controller->irq);
656 dbg("%s - acquired controller irq %d", __FUNCTION__,
660 err("cPCI hotplug controller already registered");
667 cpci_hp_unregister_controller(struct cpci_hp_controller *old_controller)
672 if(!thread_finished) {
675 if(controller->irq) {
676 free_irq(controller->irq, controller->dev_id);
688 static int first = 1;
691 dbg("%s - enter", __FUNCTION__);
696 spin_lock(&list_lock);
698 spin_unlock(&list_lock);
701 spin_unlock(&list_lock);
704 status = init_slots();
711 status = cpci_start_thread();
715 dbg("%s - thread started", __FUNCTION__);
717 if(controller->irq) {
718 /* Start enum interrupt processing */
719 dbg("%s - enabling irq", __FUNCTION__);
720 controller->ops->enable_irq();
722 dbg("%s - exit", __FUNCTION__);
733 if(controller->irq) {
734 /* Stop enum interrupt processing */
735 dbg("%s - disabling irq", __FUNCTION__);
736 controller->ops->disable_irq();
745 struct list_head *tmp;
749 * Unregister all of our slots with the pci_hotplug subsystem,
750 * and free up all memory that we had allocated.
752 spin_lock(&list_lock);
756 list_for_each(tmp, &slot_list) {
757 slot = list_entry(tmp, struct slot, slot_list);
758 list_del(&slot->slot_list);
759 pci_hp_deregister(slot->hotplug_slot);
760 kfree(slot->hotplug_slot->info);
761 kfree(slot->hotplug_slot->name);
762 kfree(slot->hotplug_slot);
766 spin_unlock(&list_lock);
771 cpci_hotplug_init(int debug)
773 spin_lock_init(&list_lock);
776 info(DRIVER_DESC " version: " DRIVER_VERSION);
781 cpci_hotplug_exit(void)
784 * Clean everything up.
789 EXPORT_SYMBOL_GPL(cpci_hp_register_controller);
790 EXPORT_SYMBOL_GPL(cpci_hp_unregister_controller);
791 EXPORT_SYMBOL_GPL(cpci_hp_register_bus);
792 EXPORT_SYMBOL_GPL(cpci_hp_unregister_bus);
793 EXPORT_SYMBOL_GPL(cpci_hp_start);
794 EXPORT_SYMBOL_GPL(cpci_hp_stop);