Linux 4.19.133
[linux/fpc-iii.git] / drivers / char / ipmi / ipmi_si_intf.c
blob006d765256782c0cd46b87f2c70b7c0838ff256b
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * ipmi_si.c
5 * The interface to the IPMI driver for the system interfaces (KCS, SMIC,
6 * BT).
8 * Author: MontaVista Software, Inc.
9 * Corey Minyard <minyard@mvista.com>
10 * source@mvista.com
12 * Copyright 2002 MontaVista Software Inc.
13 * Copyright 2006 IBM Corp., Christian Krafft <krafft@de.ibm.com>
17 * This file holds the "policy" for the interface to the SMI state
18 * machine. It does the configuration, handles timers and interrupts,
19 * and drives the real SMI state machine.
22 #include <linux/module.h>
23 #include <linux/moduleparam.h>
24 #include <linux/sched.h>
25 #include <linux/seq_file.h>
26 #include <linux/timer.h>
27 #include <linux/errno.h>
28 #include <linux/spinlock.h>
29 #include <linux/slab.h>
30 #include <linux/delay.h>
31 #include <linux/list.h>
32 #include <linux/notifier.h>
33 #include <linux/mutex.h>
34 #include <linux/kthread.h>
35 #include <asm/irq.h>
36 #include <linux/interrupt.h>
37 #include <linux/rcupdate.h>
38 #include <linux/ipmi.h>
39 #include <linux/ipmi_smi.h>
40 #include "ipmi_si.h"
41 #include <linux/string.h>
42 #include <linux/ctype.h>
44 #define PFX "ipmi_si: "
46 /* Measure times between events in the driver. */
47 #undef DEBUG_TIMING
49 /* Call every 10 ms. */
50 #define SI_TIMEOUT_TIME_USEC 10000
51 #define SI_USEC_PER_JIFFY (1000000/HZ)
52 #define SI_TIMEOUT_JIFFIES (SI_TIMEOUT_TIME_USEC/SI_USEC_PER_JIFFY)
53 #define SI_SHORT_TIMEOUT_USEC 250 /* .25ms when the SM request a
54 short timeout */
56 enum si_intf_state {
57 SI_NORMAL,
58 SI_GETTING_FLAGS,
59 SI_GETTING_EVENTS,
60 SI_CLEARING_FLAGS,
61 SI_GETTING_MESSAGES,
62 SI_CHECKING_ENABLES,
63 SI_SETTING_ENABLES
64 /* FIXME - add watchdog stuff. */
67 /* Some BT-specific defines we need here. */
68 #define IPMI_BT_INTMASK_REG 2
69 #define IPMI_BT_INTMASK_CLEAR_IRQ_BIT 2
70 #define IPMI_BT_INTMASK_ENABLE_IRQ_BIT 1
72 static const char * const si_to_str[] = { "invalid", "kcs", "smic", "bt" };
74 static int initialized;
77 * Indexes into stats[] in smi_info below.
79 enum si_stat_indexes {
81 * Number of times the driver requested a timer while an operation
82 * was in progress.
84 SI_STAT_short_timeouts = 0,
87 * Number of times the driver requested a timer while nothing was in
88 * progress.
90 SI_STAT_long_timeouts,
92 /* Number of times the interface was idle while being polled. */
93 SI_STAT_idles,
95 /* Number of interrupts the driver handled. */
96 SI_STAT_interrupts,
98 /* Number of time the driver got an ATTN from the hardware. */
99 SI_STAT_attentions,
101 /* Number of times the driver requested flags from the hardware. */
102 SI_STAT_flag_fetches,
104 /* Number of times the hardware didn't follow the state machine. */
105 SI_STAT_hosed_count,
107 /* Number of completed messages. */
108 SI_STAT_complete_transactions,
110 /* Number of IPMI events received from the hardware. */
111 SI_STAT_events,
113 /* Number of watchdog pretimeouts. */
114 SI_STAT_watchdog_pretimeouts,
116 /* Number of asynchronous messages received. */
117 SI_STAT_incoming_messages,
120 /* This *must* remain last, add new values above this. */
121 SI_NUM_STATS
124 struct smi_info {
125 int si_num;
126 struct ipmi_smi *intf;
127 struct si_sm_data *si_sm;
128 const struct si_sm_handlers *handlers;
129 spinlock_t si_lock;
130 struct ipmi_smi_msg *waiting_msg;
131 struct ipmi_smi_msg *curr_msg;
132 enum si_intf_state si_state;
135 * Used to handle the various types of I/O that can occur with
136 * IPMI
138 struct si_sm_io io;
141 * Per-OEM handler, called from handle_flags(). Returns 1
142 * when handle_flags() needs to be re-run or 0 indicating it
143 * set si_state itself.
145 int (*oem_data_avail_handler)(struct smi_info *smi_info);
148 * Flags from the last GET_MSG_FLAGS command, used when an ATTN
149 * is set to hold the flags until we are done handling everything
150 * from the flags.
152 #define RECEIVE_MSG_AVAIL 0x01
153 #define EVENT_MSG_BUFFER_FULL 0x02
154 #define WDT_PRE_TIMEOUT_INT 0x08
155 #define OEM0_DATA_AVAIL 0x20
156 #define OEM1_DATA_AVAIL 0x40
157 #define OEM2_DATA_AVAIL 0x80
158 #define OEM_DATA_AVAIL (OEM0_DATA_AVAIL | \
159 OEM1_DATA_AVAIL | \
160 OEM2_DATA_AVAIL)
161 unsigned char msg_flags;
163 /* Does the BMC have an event buffer? */
164 bool has_event_buffer;
167 * If set to true, this will request events the next time the
168 * state machine is idle.
170 atomic_t req_events;
173 * If true, run the state machine to completion on every send
174 * call. Generally used after a panic to make sure stuff goes
175 * out.
177 bool run_to_completion;
179 /* The timer for this si. */
180 struct timer_list si_timer;
182 /* This flag is set, if the timer can be set */
183 bool timer_can_start;
185 /* This flag is set, if the timer is running (timer_pending() isn't enough) */
186 bool timer_running;
188 /* The time (in jiffies) the last timeout occurred at. */
189 unsigned long last_timeout_jiffies;
191 /* Are we waiting for the events, pretimeouts, received msgs? */
192 atomic_t need_watch;
195 * The driver will disable interrupts when it gets into a
196 * situation where it cannot handle messages due to lack of
197 * memory. Once that situation clears up, it will re-enable
198 * interrupts.
200 bool interrupt_disabled;
203 * Does the BMC support events?
205 bool supports_event_msg_buff;
208 * Can we disable interrupts the global enables receive irq
209 * bit? There are currently two forms of brokenness, some
210 * systems cannot disable the bit (which is technically within
211 * the spec but a bad idea) and some systems have the bit
212 * forced to zero even though interrupts work (which is
213 * clearly outside the spec). The next bool tells which form
214 * of brokenness is present.
216 bool cannot_disable_irq;
219 * Some systems are broken and cannot set the irq enable
220 * bit, even if they support interrupts.
222 bool irq_enable_broken;
224 /* Is the driver in maintenance mode? */
225 bool in_maintenance_mode;
228 * Did we get an attention that we did not handle?
230 bool got_attn;
232 /* From the get device id response... */
233 struct ipmi_device_id device_id;
235 /* Default driver model device. */
236 struct platform_device *pdev;
238 /* Have we added the device group to the device? */
239 bool dev_group_added;
241 /* Have we added the platform device? */
242 bool pdev_registered;
244 /* Counters and things for the proc filesystem. */
245 atomic_t stats[SI_NUM_STATS];
247 struct task_struct *thread;
249 struct list_head link;
252 #define smi_inc_stat(smi, stat) \
253 atomic_inc(&(smi)->stats[SI_STAT_ ## stat])
254 #define smi_get_stat(smi, stat) \
255 ((unsigned int) atomic_read(&(smi)->stats[SI_STAT_ ## stat]))
257 #define IPMI_MAX_INTFS 4
258 static int force_kipmid[IPMI_MAX_INTFS];
259 static int num_force_kipmid;
261 static unsigned int kipmid_max_busy_us[IPMI_MAX_INTFS];
262 static int num_max_busy_us;
264 static bool unload_when_empty = true;
266 static int try_smi_init(struct smi_info *smi);
267 static void cleanup_one_si(struct smi_info *smi_info);
268 static void cleanup_ipmi_si(void);
270 #ifdef DEBUG_TIMING
271 void debug_timestamp(char *msg)
273 struct timespec64 t;
275 getnstimeofday64(&t);
276 pr_debug("**%s: %lld.%9.9ld\n", msg, (long long) t.tv_sec, t.tv_nsec);
278 #else
279 #define debug_timestamp(x)
280 #endif
282 static ATOMIC_NOTIFIER_HEAD(xaction_notifier_list);
283 static int register_xaction_notifier(struct notifier_block *nb)
285 return atomic_notifier_chain_register(&xaction_notifier_list, nb);
288 static void deliver_recv_msg(struct smi_info *smi_info,
289 struct ipmi_smi_msg *msg)
291 /* Deliver the message to the upper layer. */
292 ipmi_smi_msg_received(smi_info->intf, msg);
295 static void return_hosed_msg(struct smi_info *smi_info, int cCode)
297 struct ipmi_smi_msg *msg = smi_info->curr_msg;
299 if (cCode < 0 || cCode > IPMI_ERR_UNSPECIFIED)
300 cCode = IPMI_ERR_UNSPECIFIED;
301 /* else use it as is */
303 /* Make it a response */
304 msg->rsp[0] = msg->data[0] | 4;
305 msg->rsp[1] = msg->data[1];
306 msg->rsp[2] = cCode;
307 msg->rsp_size = 3;
309 smi_info->curr_msg = NULL;
310 deliver_recv_msg(smi_info, msg);
313 static enum si_sm_result start_next_msg(struct smi_info *smi_info)
315 int rv;
317 if (!smi_info->waiting_msg) {
318 smi_info->curr_msg = NULL;
319 rv = SI_SM_IDLE;
320 } else {
321 int err;
323 smi_info->curr_msg = smi_info->waiting_msg;
324 smi_info->waiting_msg = NULL;
325 debug_timestamp("Start2");
326 err = atomic_notifier_call_chain(&xaction_notifier_list,
327 0, smi_info);
328 if (err & NOTIFY_STOP_MASK) {
329 rv = SI_SM_CALL_WITHOUT_DELAY;
330 goto out;
332 err = smi_info->handlers->start_transaction(
333 smi_info->si_sm,
334 smi_info->curr_msg->data,
335 smi_info->curr_msg->data_size);
336 if (err)
337 return_hosed_msg(smi_info, err);
339 rv = SI_SM_CALL_WITHOUT_DELAY;
341 out:
342 return rv;
345 static void smi_mod_timer(struct smi_info *smi_info, unsigned long new_val)
347 if (!smi_info->timer_can_start)
348 return;
349 smi_info->last_timeout_jiffies = jiffies;
350 mod_timer(&smi_info->si_timer, new_val);
351 smi_info->timer_running = true;
355 * Start a new message and (re)start the timer and thread.
357 static void start_new_msg(struct smi_info *smi_info, unsigned char *msg,
358 unsigned int size)
360 smi_mod_timer(smi_info, jiffies + SI_TIMEOUT_JIFFIES);
362 if (smi_info->thread)
363 wake_up_process(smi_info->thread);
365 smi_info->handlers->start_transaction(smi_info->si_sm, msg, size);
368 static void start_check_enables(struct smi_info *smi_info)
370 unsigned char msg[2];
372 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
373 msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
375 start_new_msg(smi_info, msg, 2);
376 smi_info->si_state = SI_CHECKING_ENABLES;
379 static void start_clear_flags(struct smi_info *smi_info)
381 unsigned char msg[3];
383 /* Make sure the watchdog pre-timeout flag is not set at startup. */
384 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
385 msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
386 msg[2] = WDT_PRE_TIMEOUT_INT;
388 start_new_msg(smi_info, msg, 3);
389 smi_info->si_state = SI_CLEARING_FLAGS;
392 static void start_getting_msg_queue(struct smi_info *smi_info)
394 smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
395 smi_info->curr_msg->data[1] = IPMI_GET_MSG_CMD;
396 smi_info->curr_msg->data_size = 2;
398 start_new_msg(smi_info, smi_info->curr_msg->data,
399 smi_info->curr_msg->data_size);
400 smi_info->si_state = SI_GETTING_MESSAGES;
403 static void start_getting_events(struct smi_info *smi_info)
405 smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
406 smi_info->curr_msg->data[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD;
407 smi_info->curr_msg->data_size = 2;
409 start_new_msg(smi_info, smi_info->curr_msg->data,
410 smi_info->curr_msg->data_size);
411 smi_info->si_state = SI_GETTING_EVENTS;
415 * When we have a situtaion where we run out of memory and cannot
416 * allocate messages, we just leave them in the BMC and run the system
417 * polled until we can allocate some memory. Once we have some
418 * memory, we will re-enable the interrupt.
420 * Note that we cannot just use disable_irq(), since the interrupt may
421 * be shared.
423 static inline bool disable_si_irq(struct smi_info *smi_info)
425 if ((smi_info->io.irq) && (!smi_info->interrupt_disabled)) {
426 smi_info->interrupt_disabled = true;
427 start_check_enables(smi_info);
428 return true;
430 return false;
433 static inline bool enable_si_irq(struct smi_info *smi_info)
435 if ((smi_info->io.irq) && (smi_info->interrupt_disabled)) {
436 smi_info->interrupt_disabled = false;
437 start_check_enables(smi_info);
438 return true;
440 return false;
444 * Allocate a message. If unable to allocate, start the interrupt
445 * disable process and return NULL. If able to allocate but
446 * interrupts are disabled, free the message and return NULL after
447 * starting the interrupt enable process.
449 static struct ipmi_smi_msg *alloc_msg_handle_irq(struct smi_info *smi_info)
451 struct ipmi_smi_msg *msg;
453 msg = ipmi_alloc_smi_msg();
454 if (!msg) {
455 if (!disable_si_irq(smi_info))
456 smi_info->si_state = SI_NORMAL;
457 } else if (enable_si_irq(smi_info)) {
458 ipmi_free_smi_msg(msg);
459 msg = NULL;
461 return msg;
464 static void handle_flags(struct smi_info *smi_info)
466 retry:
467 if (smi_info->msg_flags & WDT_PRE_TIMEOUT_INT) {
468 /* Watchdog pre-timeout */
469 smi_inc_stat(smi_info, watchdog_pretimeouts);
471 start_clear_flags(smi_info);
472 smi_info->msg_flags &= ~WDT_PRE_TIMEOUT_INT;
473 ipmi_smi_watchdog_pretimeout(smi_info->intf);
474 } else if (smi_info->msg_flags & RECEIVE_MSG_AVAIL) {
475 /* Messages available. */
476 smi_info->curr_msg = alloc_msg_handle_irq(smi_info);
477 if (!smi_info->curr_msg)
478 return;
480 start_getting_msg_queue(smi_info);
481 } else if (smi_info->msg_flags & EVENT_MSG_BUFFER_FULL) {
482 /* Events available. */
483 smi_info->curr_msg = alloc_msg_handle_irq(smi_info);
484 if (!smi_info->curr_msg)
485 return;
487 start_getting_events(smi_info);
488 } else if (smi_info->msg_flags & OEM_DATA_AVAIL &&
489 smi_info->oem_data_avail_handler) {
490 if (smi_info->oem_data_avail_handler(smi_info))
491 goto retry;
492 } else
493 smi_info->si_state = SI_NORMAL;
497 * Global enables we care about.
499 #define GLOBAL_ENABLES_MASK (IPMI_BMC_EVT_MSG_BUFF | IPMI_BMC_RCV_MSG_INTR | \
500 IPMI_BMC_EVT_MSG_INTR)
502 static u8 current_global_enables(struct smi_info *smi_info, u8 base,
503 bool *irq_on)
505 u8 enables = 0;
507 if (smi_info->supports_event_msg_buff)
508 enables |= IPMI_BMC_EVT_MSG_BUFF;
510 if (((smi_info->io.irq && !smi_info->interrupt_disabled) ||
511 smi_info->cannot_disable_irq) &&
512 !smi_info->irq_enable_broken)
513 enables |= IPMI_BMC_RCV_MSG_INTR;
515 if (smi_info->supports_event_msg_buff &&
516 smi_info->io.irq && !smi_info->interrupt_disabled &&
517 !smi_info->irq_enable_broken)
518 enables |= IPMI_BMC_EVT_MSG_INTR;
520 *irq_on = enables & (IPMI_BMC_EVT_MSG_INTR | IPMI_BMC_RCV_MSG_INTR);
522 return enables;
525 static void check_bt_irq(struct smi_info *smi_info, bool irq_on)
527 u8 irqstate = smi_info->io.inputb(&smi_info->io, IPMI_BT_INTMASK_REG);
529 irqstate &= IPMI_BT_INTMASK_ENABLE_IRQ_BIT;
531 if ((bool)irqstate == irq_on)
532 return;
534 if (irq_on)
535 smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG,
536 IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
537 else
538 smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG, 0);
541 static void handle_transaction_done(struct smi_info *smi_info)
543 struct ipmi_smi_msg *msg;
545 debug_timestamp("Done");
546 switch (smi_info->si_state) {
547 case SI_NORMAL:
548 if (!smi_info->curr_msg)
549 break;
551 smi_info->curr_msg->rsp_size
552 = smi_info->handlers->get_result(
553 smi_info->si_sm,
554 smi_info->curr_msg->rsp,
555 IPMI_MAX_MSG_LENGTH);
558 * Do this here becase deliver_recv_msg() releases the
559 * lock, and a new message can be put in during the
560 * time the lock is released.
562 msg = smi_info->curr_msg;
563 smi_info->curr_msg = NULL;
564 deliver_recv_msg(smi_info, msg);
565 break;
567 case SI_GETTING_FLAGS:
569 unsigned char msg[4];
570 unsigned int len;
572 /* We got the flags from the SMI, now handle them. */
573 len = smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
574 if (msg[2] != 0) {
575 /* Error fetching flags, just give up for now. */
576 smi_info->si_state = SI_NORMAL;
577 } else if (len < 4) {
579 * Hmm, no flags. That's technically illegal, but
580 * don't use uninitialized data.
582 smi_info->si_state = SI_NORMAL;
583 } else {
584 smi_info->msg_flags = msg[3];
585 handle_flags(smi_info);
587 break;
590 case SI_CLEARING_FLAGS:
592 unsigned char msg[3];
594 /* We cleared the flags. */
595 smi_info->handlers->get_result(smi_info->si_sm, msg, 3);
596 if (msg[2] != 0) {
597 /* Error clearing flags */
598 dev_warn(smi_info->io.dev,
599 "Error clearing flags: %2.2x\n", msg[2]);
601 smi_info->si_state = SI_NORMAL;
602 break;
605 case SI_GETTING_EVENTS:
607 smi_info->curr_msg->rsp_size
608 = smi_info->handlers->get_result(
609 smi_info->si_sm,
610 smi_info->curr_msg->rsp,
611 IPMI_MAX_MSG_LENGTH);
614 * Do this here becase deliver_recv_msg() releases the
615 * lock, and a new message can be put in during the
616 * time the lock is released.
618 msg = smi_info->curr_msg;
619 smi_info->curr_msg = NULL;
620 if (msg->rsp[2] != 0) {
621 /* Error getting event, probably done. */
622 msg->done(msg);
624 /* Take off the event flag. */
625 smi_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
626 handle_flags(smi_info);
627 } else {
628 smi_inc_stat(smi_info, events);
631 * Do this before we deliver the message
632 * because delivering the message releases the
633 * lock and something else can mess with the
634 * state.
636 handle_flags(smi_info);
638 deliver_recv_msg(smi_info, msg);
640 break;
643 case SI_GETTING_MESSAGES:
645 smi_info->curr_msg->rsp_size
646 = smi_info->handlers->get_result(
647 smi_info->si_sm,
648 smi_info->curr_msg->rsp,
649 IPMI_MAX_MSG_LENGTH);
652 * Do this here becase deliver_recv_msg() releases the
653 * lock, and a new message can be put in during the
654 * time the lock is released.
656 msg = smi_info->curr_msg;
657 smi_info->curr_msg = NULL;
658 if (msg->rsp[2] != 0) {
659 /* Error getting event, probably done. */
660 msg->done(msg);
662 /* Take off the msg flag. */
663 smi_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
664 handle_flags(smi_info);
665 } else {
666 smi_inc_stat(smi_info, incoming_messages);
669 * Do this before we deliver the message
670 * because delivering the message releases the
671 * lock and something else can mess with the
672 * state.
674 handle_flags(smi_info);
676 deliver_recv_msg(smi_info, msg);
678 break;
681 case SI_CHECKING_ENABLES:
683 unsigned char msg[4];
684 u8 enables;
685 bool irq_on;
687 /* We got the flags from the SMI, now handle them. */
688 smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
689 if (msg[2] != 0) {
690 dev_warn(smi_info->io.dev,
691 "Couldn't get irq info: %x.\n", msg[2]);
692 dev_warn(smi_info->io.dev,
693 "Maybe ok, but ipmi might run very slowly.\n");
694 smi_info->si_state = SI_NORMAL;
695 break;
697 enables = current_global_enables(smi_info, 0, &irq_on);
698 if (smi_info->io.si_type == SI_BT)
699 /* BT has its own interrupt enable bit. */
700 check_bt_irq(smi_info, irq_on);
701 if (enables != (msg[3] & GLOBAL_ENABLES_MASK)) {
702 /* Enables are not correct, fix them. */
703 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
704 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
705 msg[2] = enables | (msg[3] & ~GLOBAL_ENABLES_MASK);
706 smi_info->handlers->start_transaction(
707 smi_info->si_sm, msg, 3);
708 smi_info->si_state = SI_SETTING_ENABLES;
709 } else if (smi_info->supports_event_msg_buff) {
710 smi_info->curr_msg = ipmi_alloc_smi_msg();
711 if (!smi_info->curr_msg) {
712 smi_info->si_state = SI_NORMAL;
713 break;
715 start_getting_events(smi_info);
716 } else {
717 smi_info->si_state = SI_NORMAL;
719 break;
722 case SI_SETTING_ENABLES:
724 unsigned char msg[4];
726 smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
727 if (msg[2] != 0)
728 dev_warn(smi_info->io.dev,
729 "Could not set the global enables: 0x%x.\n",
730 msg[2]);
732 if (smi_info->supports_event_msg_buff) {
733 smi_info->curr_msg = ipmi_alloc_smi_msg();
734 if (!smi_info->curr_msg) {
735 smi_info->si_state = SI_NORMAL;
736 break;
738 start_getting_events(smi_info);
739 } else {
740 smi_info->si_state = SI_NORMAL;
742 break;
748 * Called on timeouts and events. Timeouts should pass the elapsed
749 * time, interrupts should pass in zero. Must be called with
750 * si_lock held and interrupts disabled.
752 static enum si_sm_result smi_event_handler(struct smi_info *smi_info,
753 int time)
755 enum si_sm_result si_sm_result;
757 restart:
759 * There used to be a loop here that waited a little while
760 * (around 25us) before giving up. That turned out to be
761 * pointless, the minimum delays I was seeing were in the 300us
762 * range, which is far too long to wait in an interrupt. So
763 * we just run until the state machine tells us something
764 * happened or it needs a delay.
766 si_sm_result = smi_info->handlers->event(smi_info->si_sm, time);
767 time = 0;
768 while (si_sm_result == SI_SM_CALL_WITHOUT_DELAY)
769 si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
771 if (si_sm_result == SI_SM_TRANSACTION_COMPLETE) {
772 smi_inc_stat(smi_info, complete_transactions);
774 handle_transaction_done(smi_info);
775 goto restart;
776 } else if (si_sm_result == SI_SM_HOSED) {
777 smi_inc_stat(smi_info, hosed_count);
780 * Do the before return_hosed_msg, because that
781 * releases the lock.
783 smi_info->si_state = SI_NORMAL;
784 if (smi_info->curr_msg != NULL) {
786 * If we were handling a user message, format
787 * a response to send to the upper layer to
788 * tell it about the error.
790 return_hosed_msg(smi_info, IPMI_ERR_UNSPECIFIED);
792 goto restart;
796 * We prefer handling attn over new messages. But don't do
797 * this if there is not yet an upper layer to handle anything.
799 if (si_sm_result == SI_SM_ATTN || smi_info->got_attn) {
800 unsigned char msg[2];
802 if (smi_info->si_state != SI_NORMAL) {
804 * We got an ATTN, but we are doing something else.
805 * Handle the ATTN later.
807 smi_info->got_attn = true;
808 } else {
809 smi_info->got_attn = false;
810 smi_inc_stat(smi_info, attentions);
813 * Got a attn, send down a get message flags to see
814 * what's causing it. It would be better to handle
815 * this in the upper layer, but due to the way
816 * interrupts work with the SMI, that's not really
817 * possible.
819 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
820 msg[1] = IPMI_GET_MSG_FLAGS_CMD;
822 start_new_msg(smi_info, msg, 2);
823 smi_info->si_state = SI_GETTING_FLAGS;
824 goto restart;
828 /* If we are currently idle, try to start the next message. */
829 if (si_sm_result == SI_SM_IDLE) {
830 smi_inc_stat(smi_info, idles);
832 si_sm_result = start_next_msg(smi_info);
833 if (si_sm_result != SI_SM_IDLE)
834 goto restart;
837 if ((si_sm_result == SI_SM_IDLE)
838 && (atomic_read(&smi_info->req_events))) {
840 * We are idle and the upper layer requested that I fetch
841 * events, so do so.
843 atomic_set(&smi_info->req_events, 0);
846 * Take this opportunity to check the interrupt and
847 * message enable state for the BMC. The BMC can be
848 * asynchronously reset, and may thus get interrupts
849 * disable and messages disabled.
851 if (smi_info->supports_event_msg_buff || smi_info->io.irq) {
852 start_check_enables(smi_info);
853 } else {
854 smi_info->curr_msg = alloc_msg_handle_irq(smi_info);
855 if (!smi_info->curr_msg)
856 goto out;
858 start_getting_events(smi_info);
860 goto restart;
863 if (si_sm_result == SI_SM_IDLE && smi_info->timer_running) {
864 /* Ok it if fails, the timer will just go off. */
865 if (del_timer(&smi_info->si_timer))
866 smi_info->timer_running = false;
869 out:
870 return si_sm_result;
873 static void check_start_timer_thread(struct smi_info *smi_info)
875 if (smi_info->si_state == SI_NORMAL && smi_info->curr_msg == NULL) {
876 smi_mod_timer(smi_info, jiffies + SI_TIMEOUT_JIFFIES);
878 if (smi_info->thread)
879 wake_up_process(smi_info->thread);
881 start_next_msg(smi_info);
882 smi_event_handler(smi_info, 0);
886 static void flush_messages(void *send_info)
888 struct smi_info *smi_info = send_info;
889 enum si_sm_result result;
892 * Currently, this function is called only in run-to-completion
893 * mode. This means we are single-threaded, no need for locks.
895 result = smi_event_handler(smi_info, 0);
896 while (result != SI_SM_IDLE) {
897 udelay(SI_SHORT_TIMEOUT_USEC);
898 result = smi_event_handler(smi_info, SI_SHORT_TIMEOUT_USEC);
902 static void sender(void *send_info,
903 struct ipmi_smi_msg *msg)
905 struct smi_info *smi_info = send_info;
906 unsigned long flags;
908 debug_timestamp("Enqueue");
910 if (smi_info->run_to_completion) {
912 * If we are running to completion, start it. Upper
913 * layer will call flush_messages to clear it out.
915 smi_info->waiting_msg = msg;
916 return;
919 spin_lock_irqsave(&smi_info->si_lock, flags);
921 * The following two lines don't need to be under the lock for
922 * the lock's sake, but they do need SMP memory barriers to
923 * avoid getting things out of order. We are already claiming
924 * the lock, anyway, so just do it under the lock to avoid the
925 * ordering problem.
927 BUG_ON(smi_info->waiting_msg);
928 smi_info->waiting_msg = msg;
929 check_start_timer_thread(smi_info);
930 spin_unlock_irqrestore(&smi_info->si_lock, flags);
933 static void set_run_to_completion(void *send_info, bool i_run_to_completion)
935 struct smi_info *smi_info = send_info;
937 smi_info->run_to_completion = i_run_to_completion;
938 if (i_run_to_completion)
939 flush_messages(smi_info);
943 * Use -1 in the nsec value of the busy waiting timespec to tell that
944 * we are spinning in kipmid looking for something and not delaying
945 * between checks
947 static inline void ipmi_si_set_not_busy(struct timespec64 *ts)
949 ts->tv_nsec = -1;
951 static inline int ipmi_si_is_busy(struct timespec64 *ts)
953 return ts->tv_nsec != -1;
956 static inline int ipmi_thread_busy_wait(enum si_sm_result smi_result,
957 const struct smi_info *smi_info,
958 struct timespec64 *busy_until)
960 unsigned int max_busy_us = 0;
962 if (smi_info->si_num < num_max_busy_us)
963 max_busy_us = kipmid_max_busy_us[smi_info->si_num];
964 if (max_busy_us == 0 || smi_result != SI_SM_CALL_WITH_DELAY)
965 ipmi_si_set_not_busy(busy_until);
966 else if (!ipmi_si_is_busy(busy_until)) {
967 getnstimeofday64(busy_until);
968 timespec64_add_ns(busy_until, max_busy_us*NSEC_PER_USEC);
969 } else {
970 struct timespec64 now;
972 getnstimeofday64(&now);
973 if (unlikely(timespec64_compare(&now, busy_until) > 0)) {
974 ipmi_si_set_not_busy(busy_until);
975 return 0;
978 return 1;
983 * A busy-waiting loop for speeding up IPMI operation.
985 * Lousy hardware makes this hard. This is only enabled for systems
986 * that are not BT and do not have interrupts. It starts spinning
987 * when an operation is complete or until max_busy tells it to stop
988 * (if that is enabled). See the paragraph on kimid_max_busy_us in
989 * Documentation/IPMI.txt for details.
991 static int ipmi_thread(void *data)
993 struct smi_info *smi_info = data;
994 unsigned long flags;
995 enum si_sm_result smi_result;
996 struct timespec64 busy_until;
998 ipmi_si_set_not_busy(&busy_until);
999 set_user_nice(current, MAX_NICE);
1000 while (!kthread_should_stop()) {
1001 int busy_wait;
1003 spin_lock_irqsave(&(smi_info->si_lock), flags);
1004 smi_result = smi_event_handler(smi_info, 0);
1007 * If the driver is doing something, there is a possible
1008 * race with the timer. If the timer handler see idle,
1009 * and the thread here sees something else, the timer
1010 * handler won't restart the timer even though it is
1011 * required. So start it here if necessary.
1013 if (smi_result != SI_SM_IDLE && !smi_info->timer_running)
1014 smi_mod_timer(smi_info, jiffies + SI_TIMEOUT_JIFFIES);
1016 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
1017 busy_wait = ipmi_thread_busy_wait(smi_result, smi_info,
1018 &busy_until);
1019 if (smi_result == SI_SM_CALL_WITHOUT_DELAY) {
1020 ; /* do nothing */
1021 } else if (smi_result == SI_SM_CALL_WITH_DELAY && busy_wait) {
1023 * In maintenance mode we run as fast as
1024 * possible to allow firmware updates to
1025 * complete as fast as possible, but normally
1026 * don't bang on the scheduler.
1028 if (smi_info->in_maintenance_mode)
1029 schedule();
1030 else
1031 usleep_range(100, 200);
1032 } else if (smi_result == SI_SM_IDLE) {
1033 if (atomic_read(&smi_info->need_watch)) {
1034 schedule_timeout_interruptible(100);
1035 } else {
1036 /* Wait to be woken up when we are needed. */
1037 __set_current_state(TASK_INTERRUPTIBLE);
1038 schedule();
1040 } else {
1041 schedule_timeout_interruptible(1);
1044 return 0;
1048 static void poll(void *send_info)
1050 struct smi_info *smi_info = send_info;
1051 unsigned long flags = 0;
1052 bool run_to_completion = smi_info->run_to_completion;
1055 * Make sure there is some delay in the poll loop so we can
1056 * drive time forward and timeout things.
1058 udelay(10);
1059 if (!run_to_completion)
1060 spin_lock_irqsave(&smi_info->si_lock, flags);
1061 smi_event_handler(smi_info, 10);
1062 if (!run_to_completion)
1063 spin_unlock_irqrestore(&smi_info->si_lock, flags);
1066 static void request_events(void *send_info)
1068 struct smi_info *smi_info = send_info;
1070 if (!smi_info->has_event_buffer)
1071 return;
1073 atomic_set(&smi_info->req_events, 1);
1076 static void set_need_watch(void *send_info, bool enable)
1078 struct smi_info *smi_info = send_info;
1079 unsigned long flags;
1081 atomic_set(&smi_info->need_watch, enable);
1082 spin_lock_irqsave(&smi_info->si_lock, flags);
1083 check_start_timer_thread(smi_info);
1084 spin_unlock_irqrestore(&smi_info->si_lock, flags);
1087 static void smi_timeout(struct timer_list *t)
1089 struct smi_info *smi_info = from_timer(smi_info, t, si_timer);
1090 enum si_sm_result smi_result;
1091 unsigned long flags;
1092 unsigned long jiffies_now;
1093 long time_diff;
1094 long timeout;
1096 spin_lock_irqsave(&(smi_info->si_lock), flags);
1097 debug_timestamp("Timer");
1099 jiffies_now = jiffies;
1100 time_diff = (((long)jiffies_now - (long)smi_info->last_timeout_jiffies)
1101 * SI_USEC_PER_JIFFY);
1102 smi_result = smi_event_handler(smi_info, time_diff);
1104 if ((smi_info->io.irq) && (!smi_info->interrupt_disabled)) {
1105 /* Running with interrupts, only do long timeouts. */
1106 timeout = jiffies + SI_TIMEOUT_JIFFIES;
1107 smi_inc_stat(smi_info, long_timeouts);
1108 goto do_mod_timer;
1112 * If the state machine asks for a short delay, then shorten
1113 * the timer timeout.
1115 if (smi_result == SI_SM_CALL_WITH_DELAY) {
1116 smi_inc_stat(smi_info, short_timeouts);
1117 timeout = jiffies + 1;
1118 } else {
1119 smi_inc_stat(smi_info, long_timeouts);
1120 timeout = jiffies + SI_TIMEOUT_JIFFIES;
1123 do_mod_timer:
1124 if (smi_result != SI_SM_IDLE)
1125 smi_mod_timer(smi_info, timeout);
1126 else
1127 smi_info->timer_running = false;
1128 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
1131 irqreturn_t ipmi_si_irq_handler(int irq, void *data)
1133 struct smi_info *smi_info = data;
1134 unsigned long flags;
1136 if (smi_info->io.si_type == SI_BT)
1137 /* We need to clear the IRQ flag for the BT interface. */
1138 smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG,
1139 IPMI_BT_INTMASK_CLEAR_IRQ_BIT
1140 | IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
1142 spin_lock_irqsave(&(smi_info->si_lock), flags);
1144 smi_inc_stat(smi_info, interrupts);
1146 debug_timestamp("Interrupt");
1148 smi_event_handler(smi_info, 0);
1149 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
1150 return IRQ_HANDLED;
1153 static int smi_start_processing(void *send_info,
1154 struct ipmi_smi *intf)
1156 struct smi_info *new_smi = send_info;
1157 int enable = 0;
1159 new_smi->intf = intf;
1161 /* Set up the timer that drives the interface. */
1162 timer_setup(&new_smi->si_timer, smi_timeout, 0);
1163 new_smi->timer_can_start = true;
1164 smi_mod_timer(new_smi, jiffies + SI_TIMEOUT_JIFFIES);
1166 /* Try to claim any interrupts. */
1167 if (new_smi->io.irq_setup) {
1168 new_smi->io.irq_handler_data = new_smi;
1169 new_smi->io.irq_setup(&new_smi->io);
1173 * Check if the user forcefully enabled the daemon.
1175 if (new_smi->si_num < num_force_kipmid)
1176 enable = force_kipmid[new_smi->si_num];
1178 * The BT interface is efficient enough to not need a thread,
1179 * and there is no need for a thread if we have interrupts.
1181 else if ((new_smi->io.si_type != SI_BT) && (!new_smi->io.irq))
1182 enable = 1;
1184 if (enable) {
1185 new_smi->thread = kthread_run(ipmi_thread, new_smi,
1186 "kipmi%d", new_smi->si_num);
1187 if (IS_ERR(new_smi->thread)) {
1188 dev_notice(new_smi->io.dev, "Could not start"
1189 " kernel thread due to error %ld, only using"
1190 " timers to drive the interface\n",
1191 PTR_ERR(new_smi->thread));
1192 new_smi->thread = NULL;
1196 return 0;
1199 static int get_smi_info(void *send_info, struct ipmi_smi_info *data)
1201 struct smi_info *smi = send_info;
1203 data->addr_src = smi->io.addr_source;
1204 data->dev = smi->io.dev;
1205 data->addr_info = smi->io.addr_info;
1206 get_device(smi->io.dev);
1208 return 0;
1211 static void set_maintenance_mode(void *send_info, bool enable)
1213 struct smi_info *smi_info = send_info;
1215 if (!enable)
1216 atomic_set(&smi_info->req_events, 0);
1217 smi_info->in_maintenance_mode = enable;
1220 static void shutdown_smi(void *send_info);
1221 static const struct ipmi_smi_handlers handlers = {
1222 .owner = THIS_MODULE,
1223 .start_processing = smi_start_processing,
1224 .shutdown = shutdown_smi,
1225 .get_smi_info = get_smi_info,
1226 .sender = sender,
1227 .request_events = request_events,
1228 .set_need_watch = set_need_watch,
1229 .set_maintenance_mode = set_maintenance_mode,
1230 .set_run_to_completion = set_run_to_completion,
1231 .flush_messages = flush_messages,
1232 .poll = poll,
1235 static LIST_HEAD(smi_infos);
1236 static DEFINE_MUTEX(smi_infos_lock);
1237 static int smi_num; /* Used to sequence the SMIs */
1239 static const char * const addr_space_to_str[] = { "i/o", "mem" };
1241 module_param_array(force_kipmid, int, &num_force_kipmid, 0);
1242 MODULE_PARM_DESC(force_kipmid, "Force the kipmi daemon to be enabled (1) or"
1243 " disabled(0). Normally the IPMI driver auto-detects"
1244 " this, but the value may be overridden by this parm.");
1245 module_param(unload_when_empty, bool, 0);
1246 MODULE_PARM_DESC(unload_when_empty, "Unload the module if no interfaces are"
1247 " specified or found, default is 1. Setting to 0"
1248 " is useful for hot add of devices using hotmod.");
1249 module_param_array(kipmid_max_busy_us, uint, &num_max_busy_us, 0644);
1250 MODULE_PARM_DESC(kipmid_max_busy_us,
1251 "Max time (in microseconds) to busy-wait for IPMI data before"
1252 " sleeping. 0 (default) means to wait forever. Set to 100-500"
1253 " if kipmid is using up a lot of CPU time.");
1255 void ipmi_irq_finish_setup(struct si_sm_io *io)
1257 if (io->si_type == SI_BT)
1258 /* Enable the interrupt in the BT interface. */
1259 io->outputb(io, IPMI_BT_INTMASK_REG,
1260 IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
1263 void ipmi_irq_start_cleanup(struct si_sm_io *io)
1265 if (io->si_type == SI_BT)
1266 /* Disable the interrupt in the BT interface. */
1267 io->outputb(io, IPMI_BT_INTMASK_REG, 0);
1270 static void std_irq_cleanup(struct si_sm_io *io)
1272 ipmi_irq_start_cleanup(io);
1273 free_irq(io->irq, io->irq_handler_data);
1276 int ipmi_std_irq_setup(struct si_sm_io *io)
1278 int rv;
1280 if (!io->irq)
1281 return 0;
1283 rv = request_irq(io->irq,
1284 ipmi_si_irq_handler,
1285 IRQF_SHARED,
1286 DEVICE_NAME,
1287 io->irq_handler_data);
1288 if (rv) {
1289 dev_warn(io->dev, "%s unable to claim interrupt %d,"
1290 " running polled\n",
1291 DEVICE_NAME, io->irq);
1292 io->irq = 0;
1293 } else {
1294 io->irq_cleanup = std_irq_cleanup;
1295 ipmi_irq_finish_setup(io);
1296 dev_info(io->dev, "Using irq %d\n", io->irq);
1299 return rv;
1302 static int wait_for_msg_done(struct smi_info *smi_info)
1304 enum si_sm_result smi_result;
1306 smi_result = smi_info->handlers->event(smi_info->si_sm, 0);
1307 for (;;) {
1308 if (smi_result == SI_SM_CALL_WITH_DELAY ||
1309 smi_result == SI_SM_CALL_WITH_TICK_DELAY) {
1310 schedule_timeout_uninterruptible(1);
1311 smi_result = smi_info->handlers->event(
1312 smi_info->si_sm, jiffies_to_usecs(1));
1313 } else if (smi_result == SI_SM_CALL_WITHOUT_DELAY) {
1314 smi_result = smi_info->handlers->event(
1315 smi_info->si_sm, 0);
1316 } else
1317 break;
1319 if (smi_result == SI_SM_HOSED)
1321 * We couldn't get the state machine to run, so whatever's at
1322 * the port is probably not an IPMI SMI interface.
1324 return -ENODEV;
1326 return 0;
1329 static int try_get_dev_id(struct smi_info *smi_info)
1331 unsigned char msg[2];
1332 unsigned char *resp;
1333 unsigned long resp_len;
1334 int rv = 0;
1336 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1337 if (!resp)
1338 return -ENOMEM;
1341 * Do a Get Device ID command, since it comes back with some
1342 * useful info.
1344 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1345 msg[1] = IPMI_GET_DEVICE_ID_CMD;
1346 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
1348 rv = wait_for_msg_done(smi_info);
1349 if (rv)
1350 goto out;
1352 resp_len = smi_info->handlers->get_result(smi_info->si_sm,
1353 resp, IPMI_MAX_MSG_LENGTH);
1355 /* Check and record info from the get device id, in case we need it. */
1356 rv = ipmi_demangle_device_id(resp[0] >> 2, resp[1],
1357 resp + 2, resp_len - 2, &smi_info->device_id);
1359 out:
1360 kfree(resp);
1361 return rv;
1364 static int get_global_enables(struct smi_info *smi_info, u8 *enables)
1366 unsigned char msg[3];
1367 unsigned char *resp;
1368 unsigned long resp_len;
1369 int rv;
1371 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1372 if (!resp)
1373 return -ENOMEM;
1375 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1376 msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
1377 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
1379 rv = wait_for_msg_done(smi_info);
1380 if (rv) {
1381 dev_warn(smi_info->io.dev,
1382 "Error getting response from get global enables command: %d\n",
1383 rv);
1384 goto out;
1387 resp_len = smi_info->handlers->get_result(smi_info->si_sm,
1388 resp, IPMI_MAX_MSG_LENGTH);
1390 if (resp_len < 4 ||
1391 resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
1392 resp[1] != IPMI_GET_BMC_GLOBAL_ENABLES_CMD ||
1393 resp[2] != 0) {
1394 dev_warn(smi_info->io.dev,
1395 "Invalid return from get global enables command: %ld %x %x %x\n",
1396 resp_len, resp[0], resp[1], resp[2]);
1397 rv = -EINVAL;
1398 goto out;
1399 } else {
1400 *enables = resp[3];
1403 out:
1404 kfree(resp);
1405 return rv;
1409 * Returns 1 if it gets an error from the command.
1411 static int set_global_enables(struct smi_info *smi_info, u8 enables)
1413 unsigned char msg[3];
1414 unsigned char *resp;
1415 unsigned long resp_len;
1416 int rv;
1418 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1419 if (!resp)
1420 return -ENOMEM;
1422 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1423 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
1424 msg[2] = enables;
1425 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 3);
1427 rv = wait_for_msg_done(smi_info);
1428 if (rv) {
1429 dev_warn(smi_info->io.dev,
1430 "Error getting response from set global enables command: %d\n",
1431 rv);
1432 goto out;
1435 resp_len = smi_info->handlers->get_result(smi_info->si_sm,
1436 resp, IPMI_MAX_MSG_LENGTH);
1438 if (resp_len < 3 ||
1439 resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
1440 resp[1] != IPMI_SET_BMC_GLOBAL_ENABLES_CMD) {
1441 dev_warn(smi_info->io.dev,
1442 "Invalid return from set global enables command: %ld %x %x\n",
1443 resp_len, resp[0], resp[1]);
1444 rv = -EINVAL;
1445 goto out;
1448 if (resp[2] != 0)
1449 rv = 1;
1451 out:
1452 kfree(resp);
1453 return rv;
1457 * Some BMCs do not support clearing the receive irq bit in the global
1458 * enables (even if they don't support interrupts on the BMC). Check
1459 * for this and handle it properly.
1461 static void check_clr_rcv_irq(struct smi_info *smi_info)
1463 u8 enables = 0;
1464 int rv;
1466 rv = get_global_enables(smi_info, &enables);
1467 if (!rv) {
1468 if ((enables & IPMI_BMC_RCV_MSG_INTR) == 0)
1469 /* Already clear, should work ok. */
1470 return;
1472 enables &= ~IPMI_BMC_RCV_MSG_INTR;
1473 rv = set_global_enables(smi_info, enables);
1476 if (rv < 0) {
1477 dev_err(smi_info->io.dev,
1478 "Cannot check clearing the rcv irq: %d\n", rv);
1479 return;
1482 if (rv) {
1484 * An error when setting the event buffer bit means
1485 * clearing the bit is not supported.
1487 dev_warn(smi_info->io.dev,
1488 "The BMC does not support clearing the recv irq bit, compensating, but the BMC needs to be fixed.\n");
1489 smi_info->cannot_disable_irq = true;
1494 * Some BMCs do not support setting the interrupt bits in the global
1495 * enables even if they support interrupts. Clearly bad, but we can
1496 * compensate.
1498 static void check_set_rcv_irq(struct smi_info *smi_info)
1500 u8 enables = 0;
1501 int rv;
1503 if (!smi_info->io.irq)
1504 return;
1506 rv = get_global_enables(smi_info, &enables);
1507 if (!rv) {
1508 enables |= IPMI_BMC_RCV_MSG_INTR;
1509 rv = set_global_enables(smi_info, enables);
1512 if (rv < 0) {
1513 dev_err(smi_info->io.dev,
1514 "Cannot check setting the rcv irq: %d\n", rv);
1515 return;
1518 if (rv) {
1520 * An error when setting the event buffer bit means
1521 * setting the bit is not supported.
1523 dev_warn(smi_info->io.dev,
1524 "The BMC does not support setting the recv irq bit, compensating, but the BMC needs to be fixed.\n");
1525 smi_info->cannot_disable_irq = true;
1526 smi_info->irq_enable_broken = true;
1530 static int try_enable_event_buffer(struct smi_info *smi_info)
1532 unsigned char msg[3];
1533 unsigned char *resp;
1534 unsigned long resp_len;
1535 int rv = 0;
1537 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1538 if (!resp)
1539 return -ENOMEM;
1541 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1542 msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
1543 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
1545 rv = wait_for_msg_done(smi_info);
1546 if (rv) {
1547 pr_warn(PFX "Error getting response from get global enables command, the event buffer is not enabled.\n");
1548 goto out;
1551 resp_len = smi_info->handlers->get_result(smi_info->si_sm,
1552 resp, IPMI_MAX_MSG_LENGTH);
1554 if (resp_len < 4 ||
1555 resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
1556 resp[1] != IPMI_GET_BMC_GLOBAL_ENABLES_CMD ||
1557 resp[2] != 0) {
1558 pr_warn(PFX "Invalid return from get global enables command, cannot enable the event buffer.\n");
1559 rv = -EINVAL;
1560 goto out;
1563 if (resp[3] & IPMI_BMC_EVT_MSG_BUFF) {
1564 /* buffer is already enabled, nothing to do. */
1565 smi_info->supports_event_msg_buff = true;
1566 goto out;
1569 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1570 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
1571 msg[2] = resp[3] | IPMI_BMC_EVT_MSG_BUFF;
1572 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 3);
1574 rv = wait_for_msg_done(smi_info);
1575 if (rv) {
1576 pr_warn(PFX "Error getting response from set global, enables command, the event buffer is not enabled.\n");
1577 goto out;
1580 resp_len = smi_info->handlers->get_result(smi_info->si_sm,
1581 resp, IPMI_MAX_MSG_LENGTH);
1583 if (resp_len < 3 ||
1584 resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
1585 resp[1] != IPMI_SET_BMC_GLOBAL_ENABLES_CMD) {
1586 pr_warn(PFX "Invalid return from get global, enables command, not enable the event buffer.\n");
1587 rv = -EINVAL;
1588 goto out;
1591 if (resp[2] != 0)
1593 * An error when setting the event buffer bit means
1594 * that the event buffer is not supported.
1596 rv = -ENOENT;
1597 else
1598 smi_info->supports_event_msg_buff = true;
1600 out:
1601 kfree(resp);
1602 return rv;
1605 #define IPMI_SI_ATTR(name) \
1606 static ssize_t ipmi_##name##_show(struct device *dev, \
1607 struct device_attribute *attr, \
1608 char *buf) \
1610 struct smi_info *smi_info = dev_get_drvdata(dev); \
1612 return snprintf(buf, 10, "%u\n", smi_get_stat(smi_info, name)); \
1614 static DEVICE_ATTR(name, S_IRUGO, ipmi_##name##_show, NULL)
1616 static ssize_t ipmi_type_show(struct device *dev,
1617 struct device_attribute *attr,
1618 char *buf)
1620 struct smi_info *smi_info = dev_get_drvdata(dev);
1622 return snprintf(buf, 10, "%s\n", si_to_str[smi_info->io.si_type]);
1624 static DEVICE_ATTR(type, S_IRUGO, ipmi_type_show, NULL);
1626 static ssize_t ipmi_interrupts_enabled_show(struct device *dev,
1627 struct device_attribute *attr,
1628 char *buf)
1630 struct smi_info *smi_info = dev_get_drvdata(dev);
1631 int enabled = smi_info->io.irq && !smi_info->interrupt_disabled;
1633 return snprintf(buf, 10, "%d\n", enabled);
1635 static DEVICE_ATTR(interrupts_enabled, S_IRUGO,
1636 ipmi_interrupts_enabled_show, NULL);
1638 IPMI_SI_ATTR(short_timeouts);
1639 IPMI_SI_ATTR(long_timeouts);
1640 IPMI_SI_ATTR(idles);
1641 IPMI_SI_ATTR(interrupts);
1642 IPMI_SI_ATTR(attentions);
1643 IPMI_SI_ATTR(flag_fetches);
1644 IPMI_SI_ATTR(hosed_count);
1645 IPMI_SI_ATTR(complete_transactions);
1646 IPMI_SI_ATTR(events);
1647 IPMI_SI_ATTR(watchdog_pretimeouts);
1648 IPMI_SI_ATTR(incoming_messages);
1650 static ssize_t ipmi_params_show(struct device *dev,
1651 struct device_attribute *attr,
1652 char *buf)
1654 struct smi_info *smi_info = dev_get_drvdata(dev);
1656 return snprintf(buf, 200,
1657 "%s,%s,0x%lx,rsp=%d,rsi=%d,rsh=%d,irq=%d,ipmb=%d\n",
1658 si_to_str[smi_info->io.si_type],
1659 addr_space_to_str[smi_info->io.addr_type],
1660 smi_info->io.addr_data,
1661 smi_info->io.regspacing,
1662 smi_info->io.regsize,
1663 smi_info->io.regshift,
1664 smi_info->io.irq,
1665 smi_info->io.slave_addr);
1667 static DEVICE_ATTR(params, S_IRUGO, ipmi_params_show, NULL);
1669 static struct attribute *ipmi_si_dev_attrs[] = {
1670 &dev_attr_type.attr,
1671 &dev_attr_interrupts_enabled.attr,
1672 &dev_attr_short_timeouts.attr,
1673 &dev_attr_long_timeouts.attr,
1674 &dev_attr_idles.attr,
1675 &dev_attr_interrupts.attr,
1676 &dev_attr_attentions.attr,
1677 &dev_attr_flag_fetches.attr,
1678 &dev_attr_hosed_count.attr,
1679 &dev_attr_complete_transactions.attr,
1680 &dev_attr_events.attr,
1681 &dev_attr_watchdog_pretimeouts.attr,
1682 &dev_attr_incoming_messages.attr,
1683 &dev_attr_params.attr,
1684 NULL
1687 static const struct attribute_group ipmi_si_dev_attr_group = {
1688 .attrs = ipmi_si_dev_attrs,
1692 * oem_data_avail_to_receive_msg_avail
1693 * @info - smi_info structure with msg_flags set
1695 * Converts flags from OEM_DATA_AVAIL to RECEIVE_MSG_AVAIL
1696 * Returns 1 indicating need to re-run handle_flags().
1698 static int oem_data_avail_to_receive_msg_avail(struct smi_info *smi_info)
1700 smi_info->msg_flags = ((smi_info->msg_flags & ~OEM_DATA_AVAIL) |
1701 RECEIVE_MSG_AVAIL);
1702 return 1;
1706 * setup_dell_poweredge_oem_data_handler
1707 * @info - smi_info.device_id must be populated
1709 * Systems that match, but have firmware version < 1.40 may assert
1710 * OEM0_DATA_AVAIL on their own, without being told via Set Flags that
1711 * it's safe to do so. Such systems will de-assert OEM1_DATA_AVAIL
1712 * upon receipt of IPMI_GET_MSG_CMD, so we should treat these flags
1713 * as RECEIVE_MSG_AVAIL instead.
1715 * As Dell has no plans to release IPMI 1.5 firmware that *ever*
1716 * assert the OEM[012] bits, and if it did, the driver would have to
1717 * change to handle that properly, we don't actually check for the
1718 * firmware version.
1719 * Device ID = 0x20 BMC on PowerEdge 8G servers
1720 * Device Revision = 0x80
1721 * Firmware Revision1 = 0x01 BMC version 1.40
1722 * Firmware Revision2 = 0x40 BCD encoded
1723 * IPMI Version = 0x51 IPMI 1.5
1724 * Manufacturer ID = A2 02 00 Dell IANA
1726 * Additionally, PowerEdge systems with IPMI < 1.5 may also assert
1727 * OEM0_DATA_AVAIL and needs to be treated as RECEIVE_MSG_AVAIL.
1730 #define DELL_POWEREDGE_8G_BMC_DEVICE_ID 0x20
1731 #define DELL_POWEREDGE_8G_BMC_DEVICE_REV 0x80
1732 #define DELL_POWEREDGE_8G_BMC_IPMI_VERSION 0x51
1733 #define DELL_IANA_MFR_ID 0x0002a2
1734 static void setup_dell_poweredge_oem_data_handler(struct smi_info *smi_info)
1736 struct ipmi_device_id *id = &smi_info->device_id;
1737 if (id->manufacturer_id == DELL_IANA_MFR_ID) {
1738 if (id->device_id == DELL_POWEREDGE_8G_BMC_DEVICE_ID &&
1739 id->device_revision == DELL_POWEREDGE_8G_BMC_DEVICE_REV &&
1740 id->ipmi_version == DELL_POWEREDGE_8G_BMC_IPMI_VERSION) {
1741 smi_info->oem_data_avail_handler =
1742 oem_data_avail_to_receive_msg_avail;
1743 } else if (ipmi_version_major(id) < 1 ||
1744 (ipmi_version_major(id) == 1 &&
1745 ipmi_version_minor(id) < 5)) {
1746 smi_info->oem_data_avail_handler =
1747 oem_data_avail_to_receive_msg_avail;
1752 #define CANNOT_RETURN_REQUESTED_LENGTH 0xCA
1753 static void return_hosed_msg_badsize(struct smi_info *smi_info)
1755 struct ipmi_smi_msg *msg = smi_info->curr_msg;
1757 /* Make it a response */
1758 msg->rsp[0] = msg->data[0] | 4;
1759 msg->rsp[1] = msg->data[1];
1760 msg->rsp[2] = CANNOT_RETURN_REQUESTED_LENGTH;
1761 msg->rsp_size = 3;
1762 smi_info->curr_msg = NULL;
1763 deliver_recv_msg(smi_info, msg);
1767 * dell_poweredge_bt_xaction_handler
1768 * @info - smi_info.device_id must be populated
1770 * Dell PowerEdge servers with the BT interface (x6xx and 1750) will
1771 * not respond to a Get SDR command if the length of the data
1772 * requested is exactly 0x3A, which leads to command timeouts and no
1773 * data returned. This intercepts such commands, and causes userspace
1774 * callers to try again with a different-sized buffer, which succeeds.
1777 #define STORAGE_NETFN 0x0A
1778 #define STORAGE_CMD_GET_SDR 0x23
1779 static int dell_poweredge_bt_xaction_handler(struct notifier_block *self,
1780 unsigned long unused,
1781 void *in)
1783 struct smi_info *smi_info = in;
1784 unsigned char *data = smi_info->curr_msg->data;
1785 unsigned int size = smi_info->curr_msg->data_size;
1786 if (size >= 8 &&
1787 (data[0]>>2) == STORAGE_NETFN &&
1788 data[1] == STORAGE_CMD_GET_SDR &&
1789 data[7] == 0x3A) {
1790 return_hosed_msg_badsize(smi_info);
1791 return NOTIFY_STOP;
1793 return NOTIFY_DONE;
1796 static struct notifier_block dell_poweredge_bt_xaction_notifier = {
1797 .notifier_call = dell_poweredge_bt_xaction_handler,
1801 * setup_dell_poweredge_bt_xaction_handler
1802 * @info - smi_info.device_id must be filled in already
1804 * Fills in smi_info.device_id.start_transaction_pre_hook
1805 * when we know what function to use there.
1807 static void
1808 setup_dell_poweredge_bt_xaction_handler(struct smi_info *smi_info)
1810 struct ipmi_device_id *id = &smi_info->device_id;
1811 if (id->manufacturer_id == DELL_IANA_MFR_ID &&
1812 smi_info->io.si_type == SI_BT)
1813 register_xaction_notifier(&dell_poweredge_bt_xaction_notifier);
1817 * setup_oem_data_handler
1818 * @info - smi_info.device_id must be filled in already
1820 * Fills in smi_info.device_id.oem_data_available_handler
1821 * when we know what function to use there.
1824 static void setup_oem_data_handler(struct smi_info *smi_info)
1826 setup_dell_poweredge_oem_data_handler(smi_info);
1829 static void setup_xaction_handlers(struct smi_info *smi_info)
1831 setup_dell_poweredge_bt_xaction_handler(smi_info);
1834 static void check_for_broken_irqs(struct smi_info *smi_info)
1836 check_clr_rcv_irq(smi_info);
1837 check_set_rcv_irq(smi_info);
1840 static inline void stop_timer_and_thread(struct smi_info *smi_info)
1842 if (smi_info->thread != NULL) {
1843 kthread_stop(smi_info->thread);
1844 smi_info->thread = NULL;
1847 smi_info->timer_can_start = false;
1848 if (smi_info->timer_running)
1849 del_timer_sync(&smi_info->si_timer);
1852 static struct smi_info *find_dup_si(struct smi_info *info)
1854 struct smi_info *e;
1856 list_for_each_entry(e, &smi_infos, link) {
1857 if (e->io.addr_type != info->io.addr_type)
1858 continue;
1859 if (e->io.addr_data == info->io.addr_data) {
1861 * This is a cheap hack, ACPI doesn't have a defined
1862 * slave address but SMBIOS does. Pick it up from
1863 * any source that has it available.
1865 if (info->io.slave_addr && !e->io.slave_addr)
1866 e->io.slave_addr = info->io.slave_addr;
1867 return e;
1871 return NULL;
1874 int ipmi_si_add_smi(struct si_sm_io *io)
1876 int rv = 0;
1877 struct smi_info *new_smi, *dup;
1880 * If the user gave us a hard-coded device at the same
1881 * address, they presumably want us to use it and not what is
1882 * in the firmware.
1884 if (io->addr_source != SI_HARDCODED &&
1885 ipmi_si_hardcode_match(io->addr_type, io->addr_data)) {
1886 dev_info(io->dev,
1887 "Hard-coded device at this address already exists");
1888 return -ENODEV;
1891 if (!io->io_setup) {
1892 if (io->addr_type == IPMI_IO_ADDR_SPACE) {
1893 io->io_setup = ipmi_si_port_setup;
1894 } else if (io->addr_type == IPMI_MEM_ADDR_SPACE) {
1895 io->io_setup = ipmi_si_mem_setup;
1896 } else {
1897 return -EINVAL;
1901 new_smi = kzalloc(sizeof(*new_smi), GFP_KERNEL);
1902 if (!new_smi)
1903 return -ENOMEM;
1904 spin_lock_init(&new_smi->si_lock);
1906 new_smi->io = *io;
1908 mutex_lock(&smi_infos_lock);
1909 dup = find_dup_si(new_smi);
1910 if (dup) {
1911 if (new_smi->io.addr_source == SI_ACPI &&
1912 dup->io.addr_source == SI_SMBIOS) {
1913 /* We prefer ACPI over SMBIOS. */
1914 dev_info(dup->io.dev,
1915 "Removing SMBIOS-specified %s state machine in favor of ACPI\n",
1916 si_to_str[new_smi->io.si_type]);
1917 cleanup_one_si(dup);
1918 } else {
1919 dev_info(new_smi->io.dev,
1920 "%s-specified %s state machine: duplicate\n",
1921 ipmi_addr_src_to_str(new_smi->io.addr_source),
1922 si_to_str[new_smi->io.si_type]);
1923 rv = -EBUSY;
1924 kfree(new_smi);
1925 goto out_err;
1929 pr_info(PFX "Adding %s-specified %s state machine\n",
1930 ipmi_addr_src_to_str(new_smi->io.addr_source),
1931 si_to_str[new_smi->io.si_type]);
1933 list_add_tail(&new_smi->link, &smi_infos);
1935 if (initialized)
1936 rv = try_smi_init(new_smi);
1937 out_err:
1938 mutex_unlock(&smi_infos_lock);
1939 return rv;
1943 * Try to start up an interface. Must be called with smi_infos_lock
1944 * held, primarily to keep smi_num consistent, we only one to do these
1945 * one at a time.
1947 static int try_smi_init(struct smi_info *new_smi)
1949 int rv = 0;
1950 int i;
1951 char *init_name = NULL;
1953 pr_info(PFX "Trying %s-specified %s state machine at %s address 0x%lx, slave address 0x%x, irq %d\n",
1954 ipmi_addr_src_to_str(new_smi->io.addr_source),
1955 si_to_str[new_smi->io.si_type],
1956 addr_space_to_str[new_smi->io.addr_type],
1957 new_smi->io.addr_data,
1958 new_smi->io.slave_addr, new_smi->io.irq);
1960 switch (new_smi->io.si_type) {
1961 case SI_KCS:
1962 new_smi->handlers = &kcs_smi_handlers;
1963 break;
1965 case SI_SMIC:
1966 new_smi->handlers = &smic_smi_handlers;
1967 break;
1969 case SI_BT:
1970 new_smi->handlers = &bt_smi_handlers;
1971 break;
1973 default:
1974 /* No support for anything else yet. */
1975 rv = -EIO;
1976 goto out_err;
1979 new_smi->si_num = smi_num;
1981 /* Do this early so it's available for logs. */
1982 if (!new_smi->io.dev) {
1983 init_name = kasprintf(GFP_KERNEL, "ipmi_si.%d",
1984 new_smi->si_num);
1987 * If we don't already have a device from something
1988 * else (like PCI), then register a new one.
1990 new_smi->pdev = platform_device_alloc("ipmi_si",
1991 new_smi->si_num);
1992 if (!new_smi->pdev) {
1993 pr_err(PFX "Unable to allocate platform device\n");
1994 rv = -ENOMEM;
1995 goto out_err;
1997 new_smi->io.dev = &new_smi->pdev->dev;
1998 new_smi->io.dev->driver = &ipmi_platform_driver.driver;
1999 /* Nulled by device_add() */
2000 new_smi->io.dev->init_name = init_name;
2003 /* Allocate the state machine's data and initialize it. */
2004 new_smi->si_sm = kmalloc(new_smi->handlers->size(), GFP_KERNEL);
2005 if (!new_smi->si_sm) {
2006 rv = -ENOMEM;
2007 goto out_err;
2009 new_smi->io.io_size = new_smi->handlers->init_data(new_smi->si_sm,
2010 &new_smi->io);
2012 /* Now that we know the I/O size, we can set up the I/O. */
2013 rv = new_smi->io.io_setup(&new_smi->io);
2014 if (rv) {
2015 dev_err(new_smi->io.dev, "Could not set up I/O space\n");
2016 goto out_err;
2019 /* Do low-level detection first. */
2020 if (new_smi->handlers->detect(new_smi->si_sm)) {
2021 if (new_smi->io.addr_source)
2022 dev_err(new_smi->io.dev,
2023 "Interface detection failed\n");
2024 rv = -ENODEV;
2025 goto out_err;
2029 * Attempt a get device id command. If it fails, we probably
2030 * don't have a BMC here.
2032 rv = try_get_dev_id(new_smi);
2033 if (rv) {
2034 if (new_smi->io.addr_source)
2035 dev_err(new_smi->io.dev,
2036 "There appears to be no BMC at this location\n");
2037 goto out_err;
2040 setup_oem_data_handler(new_smi);
2041 setup_xaction_handlers(new_smi);
2042 check_for_broken_irqs(new_smi);
2044 new_smi->waiting_msg = NULL;
2045 new_smi->curr_msg = NULL;
2046 atomic_set(&new_smi->req_events, 0);
2047 new_smi->run_to_completion = false;
2048 for (i = 0; i < SI_NUM_STATS; i++)
2049 atomic_set(&new_smi->stats[i], 0);
2051 new_smi->interrupt_disabled = true;
2052 atomic_set(&new_smi->need_watch, 0);
2054 rv = try_enable_event_buffer(new_smi);
2055 if (rv == 0)
2056 new_smi->has_event_buffer = true;
2059 * Start clearing the flags before we enable interrupts or the
2060 * timer to avoid racing with the timer.
2062 start_clear_flags(new_smi);
2065 * IRQ is defined to be set when non-zero. req_events will
2066 * cause a global flags check that will enable interrupts.
2068 if (new_smi->io.irq) {
2069 new_smi->interrupt_disabled = false;
2070 atomic_set(&new_smi->req_events, 1);
2073 if (new_smi->pdev && !new_smi->pdev_registered) {
2074 rv = platform_device_add(new_smi->pdev);
2075 if (rv) {
2076 dev_err(new_smi->io.dev,
2077 "Unable to register system interface device: %d\n",
2078 rv);
2079 goto out_err;
2081 new_smi->pdev_registered = true;
2084 dev_set_drvdata(new_smi->io.dev, new_smi);
2085 rv = device_add_group(new_smi->io.dev, &ipmi_si_dev_attr_group);
2086 if (rv) {
2087 dev_err(new_smi->io.dev,
2088 "Unable to add device attributes: error %d\n",
2089 rv);
2090 goto out_err;
2092 new_smi->dev_group_added = true;
2094 rv = ipmi_register_smi(&handlers,
2095 new_smi,
2096 new_smi->io.dev,
2097 new_smi->io.slave_addr);
2098 if (rv) {
2099 dev_err(new_smi->io.dev,
2100 "Unable to register device: error %d\n",
2101 rv);
2102 goto out_err;
2105 /* Don't increment till we know we have succeeded. */
2106 smi_num++;
2108 dev_info(new_smi->io.dev, "IPMI %s interface initialized\n",
2109 si_to_str[new_smi->io.si_type]);
2111 WARN_ON(new_smi->io.dev->init_name != NULL);
2113 out_err:
2114 if (rv && new_smi->io.io_cleanup) {
2115 new_smi->io.io_cleanup(&new_smi->io);
2116 new_smi->io.io_cleanup = NULL;
2119 kfree(init_name);
2120 return rv;
2123 static int __init init_ipmi_si(void)
2125 struct smi_info *e;
2126 enum ipmi_addr_src type = SI_INVALID;
2128 if (initialized)
2129 return 0;
2131 ipmi_hardcode_init();
2132 pr_info("IPMI System Interface driver.\n");
2134 ipmi_si_platform_init();
2136 ipmi_si_pci_init();
2138 ipmi_si_parisc_init();
2140 /* We prefer devices with interrupts, but in the case of a machine
2141 with multiple BMCs we assume that there will be several instances
2142 of a given type so if we succeed in registering a type then also
2143 try to register everything else of the same type */
2144 mutex_lock(&smi_infos_lock);
2145 list_for_each_entry(e, &smi_infos, link) {
2146 /* Try to register a device if it has an IRQ and we either
2147 haven't successfully registered a device yet or this
2148 device has the same type as one we successfully registered */
2149 if (e->io.irq && (!type || e->io.addr_source == type)) {
2150 if (!try_smi_init(e)) {
2151 type = e->io.addr_source;
2156 /* type will only have been set if we successfully registered an si */
2157 if (type)
2158 goto skip_fallback_noirq;
2160 /* Fall back to the preferred device */
2162 list_for_each_entry(e, &smi_infos, link) {
2163 if (!e->io.irq && (!type || e->io.addr_source == type)) {
2164 if (!try_smi_init(e)) {
2165 type = e->io.addr_source;
2170 skip_fallback_noirq:
2171 initialized = 1;
2172 mutex_unlock(&smi_infos_lock);
2174 if (type)
2175 return 0;
2177 mutex_lock(&smi_infos_lock);
2178 if (unload_when_empty && list_empty(&smi_infos)) {
2179 mutex_unlock(&smi_infos_lock);
2180 cleanup_ipmi_si();
2181 pr_warn(PFX "Unable to find any System Interface(s)\n");
2182 return -ENODEV;
2183 } else {
2184 mutex_unlock(&smi_infos_lock);
2185 return 0;
2188 module_init(init_ipmi_si);
2190 static void shutdown_smi(void *send_info)
2192 struct smi_info *smi_info = send_info;
2194 if (smi_info->dev_group_added) {
2195 device_remove_group(smi_info->io.dev, &ipmi_si_dev_attr_group);
2196 smi_info->dev_group_added = false;
2198 if (smi_info->io.dev)
2199 dev_set_drvdata(smi_info->io.dev, NULL);
2202 * Make sure that interrupts, the timer and the thread are
2203 * stopped and will not run again.
2205 smi_info->interrupt_disabled = true;
2206 if (smi_info->io.irq_cleanup) {
2207 smi_info->io.irq_cleanup(&smi_info->io);
2208 smi_info->io.irq_cleanup = NULL;
2210 stop_timer_and_thread(smi_info);
2213 * Wait until we know that we are out of any interrupt
2214 * handlers might have been running before we freed the
2215 * interrupt.
2217 synchronize_sched();
2220 * Timeouts are stopped, now make sure the interrupts are off
2221 * in the BMC. Note that timers and CPU interrupts are off,
2222 * so no need for locks.
2224 while (smi_info->curr_msg || (smi_info->si_state != SI_NORMAL)) {
2225 poll(smi_info);
2226 schedule_timeout_uninterruptible(1);
2228 if (smi_info->handlers)
2229 disable_si_irq(smi_info);
2230 while (smi_info->curr_msg || (smi_info->si_state != SI_NORMAL)) {
2231 poll(smi_info);
2232 schedule_timeout_uninterruptible(1);
2234 if (smi_info->handlers)
2235 smi_info->handlers->cleanup(smi_info->si_sm);
2237 if (smi_info->io.addr_source_cleanup) {
2238 smi_info->io.addr_source_cleanup(&smi_info->io);
2239 smi_info->io.addr_source_cleanup = NULL;
2241 if (smi_info->io.io_cleanup) {
2242 smi_info->io.io_cleanup(&smi_info->io);
2243 smi_info->io.io_cleanup = NULL;
2246 kfree(smi_info->si_sm);
2247 smi_info->si_sm = NULL;
2249 smi_info->intf = NULL;
2253 * Must be called with smi_infos_lock held, to serialize the
2254 * smi_info->intf check.
2256 static void cleanup_one_si(struct smi_info *smi_info)
2258 if (!smi_info)
2259 return;
2261 list_del(&smi_info->link);
2263 if (smi_info->intf)
2264 ipmi_unregister_smi(smi_info->intf);
2266 if (smi_info->pdev) {
2267 if (smi_info->pdev_registered)
2268 platform_device_unregister(smi_info->pdev);
2269 else
2270 platform_device_put(smi_info->pdev);
2273 kfree(smi_info);
2276 int ipmi_si_remove_by_dev(struct device *dev)
2278 struct smi_info *e;
2279 int rv = -ENOENT;
2281 mutex_lock(&smi_infos_lock);
2282 list_for_each_entry(e, &smi_infos, link) {
2283 if (e->io.dev == dev) {
2284 cleanup_one_si(e);
2285 rv = 0;
2286 break;
2289 mutex_unlock(&smi_infos_lock);
2291 return rv;
2294 void ipmi_si_remove_by_data(int addr_space, enum si_type si_type,
2295 unsigned long addr)
2297 /* remove */
2298 struct smi_info *e, *tmp_e;
2300 mutex_lock(&smi_infos_lock);
2301 list_for_each_entry_safe(e, tmp_e, &smi_infos, link) {
2302 if (e->io.addr_type != addr_space)
2303 continue;
2304 if (e->io.si_type != si_type)
2305 continue;
2306 if (e->io.addr_data == addr)
2307 cleanup_one_si(e);
2309 mutex_unlock(&smi_infos_lock);
2312 static void cleanup_ipmi_si(void)
2314 struct smi_info *e, *tmp_e;
2316 if (!initialized)
2317 return;
2319 ipmi_si_pci_shutdown();
2321 ipmi_si_parisc_shutdown();
2323 ipmi_si_platform_shutdown();
2325 mutex_lock(&smi_infos_lock);
2326 list_for_each_entry_safe(e, tmp_e, &smi_infos, link)
2327 cleanup_one_si(e);
2328 mutex_unlock(&smi_infos_lock);
2330 ipmi_si_hardcode_exit();
2332 module_exit(cleanup_ipmi_si);
2334 MODULE_ALIAS("platform:dmi-ipmi-si");
2335 MODULE_LICENSE("GPL");
2336 MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
2337 MODULE_DESCRIPTION("Interface to the IPMI driver for the KCS, SMIC, and BT"
2338 " system interfaces.");