1 // SPDX-License-Identifier: GPL-2.0+
3 * CompactPCI Hot Plug Driver
5 * Copyright (C) 2002,2005 SOMA Networks, Inc.
6 * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
7 * Copyright (C) 2001 IBM Corp.
11 * Send feedback to <scottm@somanetworks.com>
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/sched/signal.h>
17 #include <linux/slab.h>
18 #include <linux/pci.h>
19 #include <linux/pci_hotplug.h>
20 #include <linux/init.h>
21 #include <linux/interrupt.h>
22 #include <linux/atomic.h>
23 #include <linux/delay.h>
24 #include <linux/kthread.h>
25 #include "cpci_hotplug.h"
27 #define DRIVER_AUTHOR "Scott Murray <scottm@somanetworks.com>"
28 #define DRIVER_DESC "CompactPCI Hot Plug Core"
30 #define MY_NAME "cpci_hotplug"
32 #define dbg(format, arg...) \
35 printk(KERN_DEBUG "%s: " format "\n", \
38 #define err(format, arg...) printk(KERN_ERR "%s: " format "\n", MY_NAME, ## arg)
39 #define info(format, arg...) printk(KERN_INFO "%s: " format "\n", MY_NAME, ## arg)
40 #define warn(format, arg...) printk(KERN_WARNING "%s: " format "\n", MY_NAME, ## arg)
43 static DECLARE_RWSEM(list_rwsem
);
44 static LIST_HEAD(slot_list
);
46 static atomic_t extracting
;
48 static struct cpci_hp_controller
*controller
;
49 static struct task_struct
*cpci_thread
;
50 static int thread_finished
;
52 static int enable_slot(struct hotplug_slot
*slot
);
53 static int disable_slot(struct hotplug_slot
*slot
);
54 static int set_attention_status(struct hotplug_slot
*slot
, u8 value
);
55 static int get_power_status(struct hotplug_slot
*slot
, u8
*value
);
56 static int get_attention_status(struct hotplug_slot
*slot
, u8
*value
);
57 static int get_adapter_status(struct hotplug_slot
*slot
, u8
*value
);
58 static int get_latch_status(struct hotplug_slot
*slot
, u8
*value
);
60 static struct hotplug_slot_ops cpci_hotplug_slot_ops
= {
61 .enable_slot
= enable_slot
,
62 .disable_slot
= disable_slot
,
63 .set_attention_status
= set_attention_status
,
64 .get_power_status
= get_power_status
,
65 .get_attention_status
= get_attention_status
,
66 .get_adapter_status
= get_adapter_status
,
67 .get_latch_status
= get_latch_status
,
71 update_latch_status(struct hotplug_slot
*hotplug_slot
, u8 value
)
73 struct hotplug_slot_info info
;
75 memcpy(&info
, hotplug_slot
->info
, sizeof(struct hotplug_slot_info
));
76 info
.latch_status
= value
;
77 return pci_hp_change_slot_info(hotplug_slot
, &info
);
81 update_adapter_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
.adapter_status
= value
;
87 return pci_hp_change_slot_info(hotplug_slot
, &info
);
91 enable_slot(struct hotplug_slot
*hotplug_slot
)
93 struct slot
*slot
= hotplug_slot
->private;
96 dbg("%s - physical_slot = %s", __func__
, slot_name(slot
));
98 if (controller
->ops
->set_power
)
99 retval
= controller
->ops
->set_power(slot
, 1);
104 disable_slot(struct hotplug_slot
*hotplug_slot
)
106 struct slot
*slot
= hotplug_slot
->private;
109 dbg("%s - physical_slot = %s", __func__
, slot_name(slot
));
111 down_write(&list_rwsem
);
113 /* Unconfigure device */
114 dbg("%s - unconfiguring slot %s", __func__
, slot_name(slot
));
115 retval
= cpci_unconfigure_slot(slot
);
117 err("%s - could not unconfigure slot %s",
118 __func__
, slot_name(slot
));
121 dbg("%s - finished unconfiguring slot %s", __func__
, slot_name(slot
));
123 /* Clear EXT (by setting it) */
124 if (cpci_clear_ext(slot
)) {
125 err("%s - could not clear EXT for slot %s",
126 __func__
, slot_name(slot
));
132 if (controller
->ops
->set_power
) {
133 retval
= controller
->ops
->set_power(slot
, 0);
138 if (update_adapter_status(slot
->hotplug_slot
, 0))
139 warn("failure to update adapter file");
141 if (slot
->extracting
) {
142 slot
->extracting
= 0;
143 atomic_dec(&extracting
);
146 up_write(&list_rwsem
);
151 cpci_get_power_status(struct slot
*slot
)
155 if (controller
->ops
->get_power
)
156 power
= controller
->ops
->get_power(slot
);
161 get_power_status(struct hotplug_slot
*hotplug_slot
, u8
*value
)
163 struct slot
*slot
= hotplug_slot
->private;
165 *value
= cpci_get_power_status(slot
);
170 get_attention_status(struct hotplug_slot
*hotplug_slot
, u8
*value
)
172 struct slot
*slot
= hotplug_slot
->private;
174 *value
= cpci_get_attention_status(slot
);
179 set_attention_status(struct hotplug_slot
*hotplug_slot
, u8 status
)
181 return cpci_set_attention_status(hotplug_slot
->private, status
);
185 get_adapter_status(struct hotplug_slot
*hotplug_slot
, u8
*value
)
187 *value
= hotplug_slot
->info
->adapter_status
;
192 get_latch_status(struct hotplug_slot
*hotplug_slot
, u8
*value
)
194 *value
= hotplug_slot
->info
->latch_status
;
198 static void release_slot(struct hotplug_slot
*hotplug_slot
)
200 struct slot
*slot
= hotplug_slot
->private;
202 kfree(slot
->hotplug_slot
->info
);
203 kfree(slot
->hotplug_slot
);
204 pci_dev_put(slot
->dev
);
208 #define SLOT_NAME_SIZE 6
211 cpci_hp_register_bus(struct pci_bus
*bus
, u8 first
, u8 last
)
214 struct hotplug_slot
*hotplug_slot
;
215 struct hotplug_slot_info
*info
;
216 char name
[SLOT_NAME_SIZE
];
220 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
= kzalloc(sizeof(struct slot
), GFP_KERNEL
);
235 kzalloc(sizeof(struct hotplug_slot
), GFP_KERNEL
);
240 slot
->hotplug_slot
= hotplug_slot
;
242 info
= kzalloc(sizeof(struct hotplug_slot_info
), GFP_KERNEL
);
247 hotplug_slot
->info
= info
;
251 slot
->devfn
= PCI_DEVFN(i
, 0);
253 snprintf(name
, SLOT_NAME_SIZE
, "%02x:%02x", bus
->number
, i
);
255 hotplug_slot
->private = slot
;
256 hotplug_slot
->release
= &release_slot
;
257 hotplug_slot
->ops
= &cpci_hotplug_slot_ops
;
260 * Initialize the slot info structure with some known
263 dbg("initializing slot %s", name
);
264 info
->power_status
= cpci_get_power_status(slot
);
265 info
->attention_status
= cpci_get_attention_status(slot
);
267 dbg("registering slot %s", name
);
268 status
= pci_hp_register(slot
->hotplug_slot
, bus
, i
, name
);
270 err("pci_hp_register failed with error %d", status
);
273 dbg("slot registered with name: %s", slot_name(slot
));
275 /* Add slot to our internal list */
276 down_write(&list_rwsem
);
277 list_add(&slot
->slot_list
, &slot_list
);
279 up_write(&list_rwsem
);
291 EXPORT_SYMBOL_GPL(cpci_hp_register_bus
);
294 cpci_hp_unregister_bus(struct pci_bus
*bus
)
300 down_write(&list_rwsem
);
302 up_write(&list_rwsem
);
305 list_for_each_entry_safe(slot
, tmp
, &slot_list
, slot_list
) {
306 if (slot
->bus
== bus
) {
307 list_del(&slot
->slot_list
);
310 dbg("deregistering slot %s", slot_name(slot
));
311 status
= pci_hp_deregister(slot
->hotplug_slot
);
313 err("pci_hp_deregister failed with error %d",
319 up_write(&list_rwsem
);
322 EXPORT_SYMBOL_GPL(cpci_hp_unregister_bus
);
324 /* This is the interrupt mode interrupt handler */
326 cpci_hp_intr(int irq
, void *data
)
328 dbg("entered cpci_hp_intr");
330 /* Check to see if it was our interrupt */
331 if ((controller
->irq_flags
& IRQF_SHARED
) &&
332 !controller
->ops
->check_irq(controller
->dev_id
)) {
333 dbg("exited cpci_hp_intr, not our interrupt");
337 /* Disable ENUM interrupt */
338 controller
->ops
->disable_irq();
340 /* Trigger processing by the event thread */
341 wake_up_process(cpci_thread
);
346 * According to PICMG 2.1 R2.0, section 6.3.2, upon
347 * initialization, the system driver shall clear the
348 * INS bits of the cold-inserted devices.
351 init_slots(int clear_ins
)
356 dbg("%s - enter", __func__
);
357 down_read(&list_rwsem
);
359 up_read(&list_rwsem
);
362 list_for_each_entry(slot
, &slot_list
, slot_list
) {
363 dbg("%s - looking at slot %s", __func__
, slot_name(slot
));
364 if (clear_ins
&& cpci_check_and_clear_ins(slot
))
365 dbg("%s - cleared INS for slot %s",
366 __func__
, slot_name(slot
));
367 dev
= pci_get_slot(slot
->bus
, PCI_DEVFN(slot
->number
, 0));
369 if (update_adapter_status(slot
->hotplug_slot
, 1))
370 warn("failure to update adapter file");
371 if (update_latch_status(slot
->hotplug_slot
, 1))
372 warn("failure to update latch file");
376 up_read(&list_rwsem
);
377 dbg("%s - exit", __func__
);
389 down_read(&list_rwsem
);
391 up_read(&list_rwsem
);
392 err("no slots registered, shutting down");
395 extracted
= inserted
= 0;
396 list_for_each_entry(slot
, &slot_list
, slot_list
) {
397 dbg("%s - looking at slot %s", __func__
, slot_name(slot
));
398 if (cpci_check_and_clear_ins(slot
)) {
400 * Some broken hardware (e.g. PLX 9054AB) asserts
404 warn("slot %s already inserted",
410 /* Process insertion */
411 dbg("%s - slot %s inserted", __func__
, slot_name(slot
));
414 hs_csr
= cpci_get_hs_csr(slot
);
415 dbg("%s - slot %s HS_CSR (1) = %04x",
416 __func__
, slot_name(slot
), hs_csr
);
418 /* Configure device */
419 dbg("%s - configuring slot %s",
420 __func__
, slot_name(slot
));
421 if (cpci_configure_slot(slot
)) {
422 err("%s - could not configure slot %s",
423 __func__
, slot_name(slot
));
426 dbg("%s - finished configuring slot %s",
427 __func__
, slot_name(slot
));
430 hs_csr
= cpci_get_hs_csr(slot
);
431 dbg("%s - slot %s HS_CSR (2) = %04x",
432 __func__
, slot_name(slot
), hs_csr
);
434 if (update_latch_status(slot
->hotplug_slot
, 1))
435 warn("failure to update latch file");
437 if (update_adapter_status(slot
->hotplug_slot
, 1))
438 warn("failure to update adapter file");
443 hs_csr
= cpci_get_hs_csr(slot
);
444 dbg("%s - slot %s HS_CSR (3) = %04x",
445 __func__
, slot_name(slot
), hs_csr
);
448 } else if (cpci_check_ext(slot
)) {
449 /* Process extraction request */
450 dbg("%s - slot %s extracted",
451 __func__
, slot_name(slot
));
454 hs_csr
= cpci_get_hs_csr(slot
);
455 dbg("%s - slot %s HS_CSR = %04x",
456 __func__
, slot_name(slot
), hs_csr
);
458 if (!slot
->extracting
) {
459 if (update_latch_status(slot
->hotplug_slot
, 0))
460 warn("failure to update latch file");
462 slot
->extracting
= 1;
463 atomic_inc(&extracting
);
466 } else if (slot
->extracting
) {
467 hs_csr
= cpci_get_hs_csr(slot
);
468 if (hs_csr
== 0xffff) {
470 * Hmmm, we're likely hosed at this point, should we
471 * bother trying to tell the driver or not?
473 err("card in slot %s was improperly removed",
475 if (update_adapter_status(slot
->hotplug_slot
, 0))
476 warn("failure to update adapter file");
477 slot
->extracting
= 0;
478 atomic_dec(&extracting
);
482 up_read(&list_rwsem
);
483 dbg("inserted=%d, extracted=%d, extracting=%d",
484 inserted
, extracted
, atomic_read(&extracting
));
485 if (inserted
|| extracted
)
487 else if (!atomic_read(&extracting
)) {
488 err("cannot find ENUM# source, shutting down");
494 /* This is the interrupt mode worker thread body */
496 event_thread(void *data
)
500 dbg("%s - event thread started", __func__
);
502 dbg("event thread sleeping");
503 set_current_state(TASK_INTERRUPTIBLE
);
505 if (kthread_should_stop())
510 /* Give userspace a chance to handle extraction */
513 dbg("%s - error checking slots", __func__
);
517 } while (atomic_read(&extracting
) && !kthread_should_stop());
518 if (kthread_should_stop())
521 /* Re-enable ENUM# interrupt */
522 dbg("%s - re-enabling irq", __func__
);
523 controller
->ops
->enable_irq();
529 /* This is the polling mode worker thread body */
531 poll_thread(void *data
)
536 if (kthread_should_stop() || signal_pending(current
))
538 if (controller
->ops
->query_enum()) {
542 /* Give userspace a chance to handle extraction */
545 dbg("%s - error checking slots", __func__
);
549 } while (atomic_read(&extracting
) && !kthread_should_stop());
558 cpci_start_thread(void)
561 cpci_thread
= kthread_run(event_thread
, NULL
, "cpci_hp_eventd");
563 cpci_thread
= kthread_run(poll_thread
, NULL
, "cpci_hp_polld");
564 if (IS_ERR(cpci_thread
)) {
565 err("Can't start up our thread");
566 return PTR_ERR(cpci_thread
);
573 cpci_stop_thread(void)
575 kthread_stop(cpci_thread
);
580 cpci_hp_register_controller(struct cpci_hp_controller
*new_controller
)
586 if (!(new_controller
&& new_controller
->ops
))
588 if (new_controller
->irq
) {
589 if (!(new_controller
->ops
->enable_irq
&&
590 new_controller
->ops
->disable_irq
))
592 if (request_irq(new_controller
->irq
,
594 new_controller
->irq_flags
,
596 new_controller
->dev_id
)) {
597 err("Can't get irq %d for the hotplug cPCI controller",
598 new_controller
->irq
);
601 dbg("%s - acquired controller irq %d",
602 __func__
, new_controller
->irq
);
605 controller
= new_controller
;
608 EXPORT_SYMBOL_GPL(cpci_hp_register_controller
);
617 * Unregister all of our slots with the pci_hotplug subsystem,
618 * and free up all memory that we had allocated.
620 down_write(&list_rwsem
);
623 list_for_each_entry_safe(slot
, tmp
, &slot_list
, slot_list
) {
624 list_del(&slot
->slot_list
);
625 pci_hp_deregister(slot
->hotplug_slot
);
628 up_write(&list_rwsem
);
633 cpci_hp_unregister_controller(struct cpci_hp_controller
*old_controller
)
638 if (!thread_finished
)
641 free_irq(controller
->irq
, controller
->dev_id
);
648 EXPORT_SYMBOL_GPL(cpci_hp_unregister_controller
);
653 static int first
= 1;
656 dbg("%s - enter", __func__
);
660 down_read(&list_rwsem
);
661 if (list_empty(&slot_list
)) {
662 up_read(&list_rwsem
);
665 up_read(&list_rwsem
);
667 status
= init_slots(first
);
673 status
= cpci_start_thread();
676 dbg("%s - thread started", __func__
);
678 if (controller
->irq
) {
679 /* Start enum interrupt processing */
680 dbg("%s - enabling irq", __func__
);
681 controller
->ops
->enable_irq();
683 dbg("%s - exit", __func__
);
686 EXPORT_SYMBOL_GPL(cpci_hp_start
);
693 if (controller
->irq
) {
694 /* Stop enum interrupt processing */
695 dbg("%s - disabling irq", __func__
);
696 controller
->ops
->disable_irq();
701 EXPORT_SYMBOL_GPL(cpci_hp_stop
);
704 cpci_hotplug_init(int debug
)