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 <linux/delay.h>
37 #include "pci_hotplug.h"
38 #include "cpci_hotplug.h"
40 #define DRIVER_VERSION "0.2"
41 #define DRIVER_AUTHOR "Scott Murray <scottm@somanetworks.com>"
42 #define DRIVER_DESC "CompactPCI Hot Plug Core"
44 #define MY_NAME "cpci_hotplug"
46 #define dbg(format, arg...) \
49 printk (KERN_DEBUG "%s: " format "\n", \
52 #define err(format, arg...) printk(KERN_ERR "%s: " format "\n", MY_NAME , ## arg)
53 #define info(format, arg...) printk(KERN_INFO "%s: " format "\n", MY_NAME , ## arg)
54 #define warn(format, arg...) printk(KERN_WARNING "%s: " format "\n", MY_NAME , ## arg)
57 static spinlock_t list_lock;
58 static LIST_HEAD(slot_list);
61 static struct cpci_hp_controller *controller;
62 static struct semaphore event_semaphore; /* mutex for process loop (up if something to process) */
63 static struct semaphore thread_exit; /* guard ensure thread has exited before calling it quits */
64 static int thread_finished = 1;
66 static int enable_slot(struct hotplug_slot *slot);
67 static int disable_slot(struct hotplug_slot *slot);
68 static int set_attention_status(struct hotplug_slot *slot, u8 value);
69 static int get_power_status(struct hotplug_slot *slot, u8 * value);
70 static int get_attention_status(struct hotplug_slot *slot, u8 * value);
72 static struct hotplug_slot_ops cpci_hotplug_slot_ops = {
74 .enable_slot = enable_slot,
75 .disable_slot = disable_slot,
76 .set_attention_status = set_attention_status,
77 .get_power_status = get_power_status,
78 .get_attention_status = get_attention_status,
82 update_latch_status(struct hotplug_slot *hotplug_slot, u8 value)
84 struct hotplug_slot_info info;
86 memcpy(&info, hotplug_slot->info, sizeof(struct hotplug_slot_info));
87 info.latch_status = value;
88 return pci_hp_change_slot_info(hotplug_slot, &info);
92 update_adapter_status(struct hotplug_slot *hotplug_slot, u8 value)
94 struct hotplug_slot_info info;
96 memcpy(&info, hotplug_slot->info, sizeof(struct hotplug_slot_info));
97 info.adapter_status = value;
98 return pci_hp_change_slot_info(hotplug_slot, &info);
102 enable_slot(struct hotplug_slot *hotplug_slot)
104 struct slot *slot = hotplug_slot->private;
107 dbg("%s - physical_slot = %s", __FUNCTION__, hotplug_slot->name);
109 if(controller->ops->set_power) {
110 retval = controller->ops->set_power(slot, 1);
117 disable_slot(struct hotplug_slot *hotplug_slot)
119 struct slot *slot = hotplug_slot->private;
122 dbg("%s - physical_slot = %s", __FUNCTION__, hotplug_slot->name);
124 /* Unconfigure device */
125 dbg("%s - unconfiguring slot %s",
126 __FUNCTION__, slot->hotplug_slot->name);
127 if((retval = cpci_unconfigure_slot(slot))) {
128 err("%s - could not unconfigure slot %s",
129 __FUNCTION__, slot->hotplug_slot->name);
132 dbg("%s - finished unconfiguring slot %s",
133 __FUNCTION__, slot->hotplug_slot->name);
135 /* Clear EXT (by setting it) */
136 if(cpci_clear_ext(slot)) {
137 err("%s - could not clear EXT for slot %s",
138 __FUNCTION__, slot->hotplug_slot->name);
143 if(controller->ops->set_power) {
144 retval = controller->ops->set_power(slot, 0);
147 if(update_adapter_status(slot->hotplug_slot, 0)) {
148 warn("failure to update adapter file");
151 slot->extracting = 0;
157 cpci_get_power_status(struct slot *slot)
161 if(controller->ops->get_power) {
162 power = controller->ops->get_power(slot);
168 get_power_status(struct hotplug_slot *hotplug_slot, u8 * value)
170 struct slot *slot = hotplug_slot->private;
172 *value = cpci_get_power_status(slot);
177 get_attention_status(struct hotplug_slot *hotplug_slot, u8 * value)
179 struct slot *slot = hotplug_slot->private;
181 *value = cpci_get_attention_status(slot);
186 set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
188 return cpci_set_attention_status(hotplug_slot->private, status);
191 static void release_slot(struct hotplug_slot *hotplug_slot)
193 struct slot *slot = hotplug_slot->private;
195 kfree(slot->hotplug_slot->info);
196 kfree(slot->hotplug_slot->name);
197 kfree(slot->hotplug_slot);
201 #define SLOT_NAME_SIZE 6
203 make_slot_name(struct slot *slot)
205 snprintf(slot->hotplug_slot->name,
206 SLOT_NAME_SIZE, "%02x:%02x", slot->bus->number, slot->number);
210 cpci_hp_register_bus(struct pci_bus *bus, u8 first, u8 last)
213 struct hotplug_slot *hotplug_slot;
214 struct hotplug_slot_info *info;
216 int status = -ENOMEM;
219 if(!(controller && bus)) {
224 * Create a structure for each slot, and register that slot
225 * with the pci_hotplug subsystem.
227 for (i = first; i <= last; ++i) {
228 slot = kmalloc(sizeof (struct slot), GFP_KERNEL);
231 memset(slot, 0, sizeof (struct slot));
234 kmalloc(sizeof (struct hotplug_slot), GFP_KERNEL);
237 memset(hotplug_slot, 0, sizeof (struct hotplug_slot));
238 slot->hotplug_slot = hotplug_slot;
240 info = kmalloc(sizeof (struct hotplug_slot_info), GFP_KERNEL);
243 memset(info, 0, sizeof (struct hotplug_slot_info));
244 hotplug_slot->info = info;
246 name = kmalloc(SLOT_NAME_SIZE, GFP_KERNEL);
249 hotplug_slot->name = name;
253 slot->devfn = PCI_DEVFN(i, 0);
255 hotplug_slot->private = slot;
256 hotplug_slot->release = &release_slot;
257 make_slot_name(slot);
258 hotplug_slot->ops = &cpci_hotplug_slot_ops;
261 * Initialize the slot info structure with some known
264 dbg("initializing slot %s", slot->hotplug_slot->name);
265 info->power_status = cpci_get_power_status(slot);
266 info->attention_status = cpci_get_attention_status(slot);
268 dbg("registering slot %s", slot->hotplug_slot->name);
269 status = pci_hp_register(slot->hotplug_slot);
271 err("pci_hp_register failed with error %d", status);
275 /* Add slot to our internal list */
276 spin_lock(&list_lock);
277 list_add(&slot->slot_list, &slot_list);
279 spin_unlock(&list_lock);
295 cpci_hp_unregister_bus(struct pci_bus *bus)
298 struct list_head *tmp;
299 struct list_head *next;
302 spin_lock(&list_lock);
304 spin_unlock(&list_lock);
307 list_for_each_safe(tmp, next, &slot_list) {
308 slot = list_entry(tmp, struct slot, slot_list);
309 if(slot->bus == bus) {
310 dbg("deregistering slot %s", slot->hotplug_slot->name);
311 status = pci_hp_deregister(slot->hotplug_slot);
313 err("pci_hp_deregister failed with error %d",
318 list_del(&slot->slot_list);
322 spin_unlock(&list_lock);
326 /* This is the interrupt mode interrupt handler */
328 cpci_hp_intr(int irq, void *data, struct pt_regs *regs)
330 dbg("entered cpci_hp_intr");
332 /* Check to see if it was our interrupt */
333 if((controller->irq_flags & SA_SHIRQ) &&
334 !controller->ops->check_irq(controller->dev_id)) {
335 dbg("exited cpci_hp_intr, not our interrupt");
339 /* Disable ENUM interrupt */
340 controller->ops->disable_irq();
342 /* Trigger processing by the event thread */
343 dbg("Signal event_semaphore");
344 up(&event_semaphore);
345 dbg("exited cpci_hp_intr");
350 * According to PICMG 2.12 R2.0, section 6.3.2, upon
351 * initialization, the system driver shall clear the
352 * INS bits of the cold-inserted devices.
358 struct list_head *tmp;
361 dbg("%s - enter", __FUNCTION__);
362 spin_lock(&list_lock);
364 spin_unlock(&list_lock);
367 list_for_each(tmp, &slot_list) {
368 slot = list_entry(tmp, struct slot, slot_list);
369 dbg("%s - looking at slot %s",
370 __FUNCTION__, slot->hotplug_slot->name);
371 if(cpci_check_and_clear_ins(slot)) {
372 dbg("%s - cleared INS for slot %s",
373 __FUNCTION__, slot->hotplug_slot->name);
374 dev = pci_find_slot(slot->bus->number, PCI_DEVFN(slot->number, 0));
376 if(update_adapter_status(slot->hotplug_slot, 1)) {
377 warn("failure to update adapter file");
379 if(update_latch_status(slot->hotplug_slot, 1)) {
380 warn("failure to update latch file");
384 err("%s - no driver attached to device in slot %s",
385 __FUNCTION__, slot->hotplug_slot->name);
389 spin_unlock(&list_lock);
390 dbg("%s - exit", __FUNCTION__);
398 struct list_head *tmp;
402 spin_lock(&list_lock);
404 spin_unlock(&list_lock);
405 err("no slots registered, shutting down");
408 extracted = inserted = 0;
409 list_for_each(tmp, &slot_list) {
410 slot = list_entry(tmp, struct slot, slot_list);
411 dbg("%s - looking at slot %s",
412 __FUNCTION__, slot->hotplug_slot->name);
413 if(cpci_check_and_clear_ins(slot)) {
416 /* Some broken hardware (e.g. PLX 9054AB) asserts ENUM# twice... */
418 warn("slot %s already inserted", slot->hotplug_slot->name);
423 /* Process insertion */
424 dbg("%s - slot %s inserted",
425 __FUNCTION__, slot->hotplug_slot->name);
428 hs_csr = cpci_get_hs_csr(slot);
429 dbg("%s - slot %s HS_CSR (1) = %04x",
430 __FUNCTION__, slot->hotplug_slot->name, hs_csr);
432 /* Configure device */
433 dbg("%s - configuring slot %s",
434 __FUNCTION__, slot->hotplug_slot->name);
435 if(cpci_configure_slot(slot)) {
436 err("%s - could not configure slot %s",
437 __FUNCTION__, slot->hotplug_slot->name);
440 dbg("%s - finished configuring slot %s",
441 __FUNCTION__, slot->hotplug_slot->name);
444 hs_csr = cpci_get_hs_csr(slot);
445 dbg("%s - slot %s HS_CSR (2) = %04x",
446 __FUNCTION__, slot->hotplug_slot->name, hs_csr);
448 if(update_latch_status(slot->hotplug_slot, 1)) {
449 warn("failure to update latch file");
452 if(update_adapter_status(slot->hotplug_slot, 1)) {
453 warn("failure to update adapter file");
459 hs_csr = cpci_get_hs_csr(slot);
460 dbg("%s - slot %s HS_CSR (3) = %04x",
461 __FUNCTION__, slot->hotplug_slot->name, hs_csr);
464 } else if(cpci_check_ext(slot)) {
467 /* Process extraction request */
468 dbg("%s - slot %s extracted",
469 __FUNCTION__, slot->hotplug_slot->name);
472 hs_csr = cpci_get_hs_csr(slot);
473 dbg("%s - slot %s HS_CSR = %04x",
474 __FUNCTION__, slot->hotplug_slot->name, hs_csr);
476 if(!slot->extracting) {
477 if(update_latch_status(slot->hotplug_slot, 0)) {
478 warn("failure to update latch file");
480 slot->extracting = 1;
485 spin_unlock(&list_lock);
486 if(inserted || extracted) {
490 err("cannot find ENUM# source, shutting down");
495 /* This is the interrupt mode worker thread body */
497 event_thread(void *data)
501 struct list_head *tmp;
504 daemonize("cpci_hp_eventd");
507 dbg("%s - event thread started", __FUNCTION__);
509 dbg("event thread sleeping");
510 down_interruptible(&event_semaphore);
511 dbg("event thread woken, thread_finished = %d",
513 if(thread_finished || signal_pending(current))
515 while(controller->ops->query_enum()) {
518 /* Give userspace a chance to handle extraction */
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 */
575 dbg("%s - error checking slots", __FUNCTION__);
580 /* Check for someone yanking out a board */
581 list_for_each(tmp, &slot_list) {
582 slot = list_entry(tmp, struct slot, slot_list);
583 if(slot->extracting) {
585 * Hmmm, we're likely hosed at this point, should we
586 * bother trying to tell the driver or not?
588 err("card in slot %s was improperly removed",
589 slot->hotplug_slot->name);
590 if(update_adapter_status(slot->hotplug_slot, 0)) {
591 warn("failure to update adapter file");
593 slot->extracting = 0;
599 dbg("poll thread signals exit");
605 cpci_start_thread(void)
609 /* initialize our semaphores */
610 init_MUTEX_LOCKED(&event_semaphore);
611 init_MUTEX_LOCKED(&thread_exit);
614 if(controller->irq) {
615 pid = kernel_thread(event_thread, NULL, 0);
617 pid = kernel_thread(poll_thread, NULL, 0);
620 err("Can't start up our thread");
623 dbg("Our thread pid = %d", pid);
628 cpci_stop_thread(void)
631 dbg("thread finish command given");
632 if(controller->irq) {
633 up(&event_semaphore);
635 dbg("wait for thread to exit");
640 cpci_hp_register_controller(struct cpci_hp_controller *new_controller)
645 controller = new_controller;
646 if(controller->irq) {
647 if(request_irq(controller->irq,
649 controller->irq_flags,
650 MY_NAME, controller->dev_id)) {
651 err("Can't get irq %d for the hotplug cPCI controller", controller->irq);
654 dbg("%s - acquired controller irq %d", __FUNCTION__,
658 err("cPCI hotplug controller already registered");
665 cpci_hp_unregister_controller(struct cpci_hp_controller *old_controller)
670 if(!thread_finished) {
673 if(controller->irq) {
674 free_irq(controller->irq, controller->dev_id);
686 static int first = 1;
689 dbg("%s - enter", __FUNCTION__);
694 spin_lock(&list_lock);
696 spin_unlock(&list_lock);
699 spin_unlock(&list_lock);
702 status = init_slots();
709 status = cpci_start_thread();
713 dbg("%s - thread started", __FUNCTION__);
715 if(controller->irq) {
716 /* Start enum interrupt processing */
717 dbg("%s - enabling irq", __FUNCTION__);
718 controller->ops->enable_irq();
720 dbg("%s - exit", __FUNCTION__);
731 if(controller->irq) {
732 /* Stop enum interrupt processing */
733 dbg("%s - disabling irq", __FUNCTION__);
734 controller->ops->disable_irq();
743 struct list_head *tmp;
747 * Unregister all of our slots with the pci_hotplug subsystem,
748 * and free up all memory that we had allocated.
750 spin_lock(&list_lock);
754 list_for_each(tmp, &slot_list) {
755 slot = list_entry(tmp, struct slot, slot_list);
756 list_del(&slot->slot_list);
757 pci_hp_deregister(slot->hotplug_slot);
758 kfree(slot->hotplug_slot->info);
759 kfree(slot->hotplug_slot->name);
760 kfree(slot->hotplug_slot);
764 spin_unlock(&list_lock);
769 cpci_hotplug_init(int debug)
771 spin_lock_init(&list_lock);
774 info(DRIVER_DESC " version: " DRIVER_VERSION);
779 cpci_hotplug_exit(void)
782 * Clean everything up.
787 EXPORT_SYMBOL_GPL(cpci_hp_register_controller);
788 EXPORT_SYMBOL_GPL(cpci_hp_unregister_controller);
789 EXPORT_SYMBOL_GPL(cpci_hp_register_bus);
790 EXPORT_SYMBOL_GPL(cpci_hp_unregister_bus);
791 EXPORT_SYMBOL_GPL(cpci_hp_start);
792 EXPORT_SYMBOL_GPL(cpci_hp_stop);