Linux 4.16.11
[linux/fpc-iii.git] / drivers / char / ipmi / ipmi_si_intf.c
blobf5b2d69316a1c21ed393ce570cfc9ba2c4b11c94
1 /*
2 * ipmi_si.c
4 * The interface to the IPMI driver for the system interfaces (KCS, SMIC,
5 * BT).
7 * Author: MontaVista Software, Inc.
8 * Corey Minyard <minyard@mvista.com>
9 * source@mvista.com
11 * Copyright 2002 MontaVista Software Inc.
12 * Copyright 2006 IBM Corp., Christian Krafft <krafft@de.ibm.com>
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version.
20 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
21 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
28 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
29 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * You should have received a copy of the GNU General Public License along
32 * with this program; if not, write to the Free Software Foundation, Inc.,
33 * 675 Mass Ave, Cambridge, MA 02139, USA.
37 * This file holds the "policy" for the interface to the SMI state
38 * machine. It does the configuration, handles timers and interrupts,
39 * and drives the real SMI state machine.
42 #include <linux/module.h>
43 #include <linux/moduleparam.h>
44 #include <linux/sched.h>
45 #include <linux/seq_file.h>
46 #include <linux/timer.h>
47 #include <linux/errno.h>
48 #include <linux/spinlock.h>
49 #include <linux/slab.h>
50 #include <linux/delay.h>
51 #include <linux/list.h>
52 #include <linux/notifier.h>
53 #include <linux/mutex.h>
54 #include <linux/kthread.h>
55 #include <asm/irq.h>
56 #include <linux/interrupt.h>
57 #include <linux/rcupdate.h>
58 #include <linux/ipmi.h>
59 #include <linux/ipmi_smi.h>
60 #include "ipmi_si.h"
61 #include <linux/string.h>
62 #include <linux/ctype.h>
64 #define PFX "ipmi_si: "
66 /* Measure times between events in the driver. */
67 #undef DEBUG_TIMING
69 /* Call every 10 ms. */
70 #define SI_TIMEOUT_TIME_USEC 10000
71 #define SI_USEC_PER_JIFFY (1000000/HZ)
72 #define SI_TIMEOUT_JIFFIES (SI_TIMEOUT_TIME_USEC/SI_USEC_PER_JIFFY)
73 #define SI_SHORT_TIMEOUT_USEC 250 /* .25ms when the SM request a
74 short timeout */
76 enum si_intf_state {
77 SI_NORMAL,
78 SI_GETTING_FLAGS,
79 SI_GETTING_EVENTS,
80 SI_CLEARING_FLAGS,
81 SI_GETTING_MESSAGES,
82 SI_CHECKING_ENABLES,
83 SI_SETTING_ENABLES
84 /* FIXME - add watchdog stuff. */
87 /* Some BT-specific defines we need here. */
88 #define IPMI_BT_INTMASK_REG 2
89 #define IPMI_BT_INTMASK_CLEAR_IRQ_BIT 2
90 #define IPMI_BT_INTMASK_ENABLE_IRQ_BIT 1
92 static const char * const si_to_str[] = { "invalid", "kcs", "smic", "bt" };
94 static int initialized;
97 * Indexes into stats[] in smi_info below.
99 enum si_stat_indexes {
101 * Number of times the driver requested a timer while an operation
102 * was in progress.
104 SI_STAT_short_timeouts = 0,
107 * Number of times the driver requested a timer while nothing was in
108 * progress.
110 SI_STAT_long_timeouts,
112 /* Number of times the interface was idle while being polled. */
113 SI_STAT_idles,
115 /* Number of interrupts the driver handled. */
116 SI_STAT_interrupts,
118 /* Number of time the driver got an ATTN from the hardware. */
119 SI_STAT_attentions,
121 /* Number of times the driver requested flags from the hardware. */
122 SI_STAT_flag_fetches,
124 /* Number of times the hardware didn't follow the state machine. */
125 SI_STAT_hosed_count,
127 /* Number of completed messages. */
128 SI_STAT_complete_transactions,
130 /* Number of IPMI events received from the hardware. */
131 SI_STAT_events,
133 /* Number of watchdog pretimeouts. */
134 SI_STAT_watchdog_pretimeouts,
136 /* Number of asynchronous messages received. */
137 SI_STAT_incoming_messages,
140 /* This *must* remain last, add new values above this. */
141 SI_NUM_STATS
144 struct smi_info {
145 int intf_num;
146 ipmi_smi_t intf;
147 struct si_sm_data *si_sm;
148 const struct si_sm_handlers *handlers;
149 spinlock_t si_lock;
150 struct ipmi_smi_msg *waiting_msg;
151 struct ipmi_smi_msg *curr_msg;
152 enum si_intf_state si_state;
155 * Used to handle the various types of I/O that can occur with
156 * IPMI
158 struct si_sm_io io;
161 * Per-OEM handler, called from handle_flags(). Returns 1
162 * when handle_flags() needs to be re-run or 0 indicating it
163 * set si_state itself.
165 int (*oem_data_avail_handler)(struct smi_info *smi_info);
168 * Flags from the last GET_MSG_FLAGS command, used when an ATTN
169 * is set to hold the flags until we are done handling everything
170 * from the flags.
172 #define RECEIVE_MSG_AVAIL 0x01
173 #define EVENT_MSG_BUFFER_FULL 0x02
174 #define WDT_PRE_TIMEOUT_INT 0x08
175 #define OEM0_DATA_AVAIL 0x20
176 #define OEM1_DATA_AVAIL 0x40
177 #define OEM2_DATA_AVAIL 0x80
178 #define OEM_DATA_AVAIL (OEM0_DATA_AVAIL | \
179 OEM1_DATA_AVAIL | \
180 OEM2_DATA_AVAIL)
181 unsigned char msg_flags;
183 /* Does the BMC have an event buffer? */
184 bool has_event_buffer;
187 * If set to true, this will request events the next time the
188 * state machine is idle.
190 atomic_t req_events;
193 * If true, run the state machine to completion on every send
194 * call. Generally used after a panic to make sure stuff goes
195 * out.
197 bool run_to_completion;
199 /* The timer for this si. */
200 struct timer_list si_timer;
202 /* This flag is set, if the timer can be set */
203 bool timer_can_start;
205 /* This flag is set, if the timer is running (timer_pending() isn't enough) */
206 bool timer_running;
208 /* The time (in jiffies) the last timeout occurred at. */
209 unsigned long last_timeout_jiffies;
211 /* Are we waiting for the events, pretimeouts, received msgs? */
212 atomic_t need_watch;
215 * The driver will disable interrupts when it gets into a
216 * situation where it cannot handle messages due to lack of
217 * memory. Once that situation clears up, it will re-enable
218 * interrupts.
220 bool interrupt_disabled;
223 * Does the BMC support events?
225 bool supports_event_msg_buff;
228 * Can we disable interrupts the global enables receive irq
229 * bit? There are currently two forms of brokenness, some
230 * systems cannot disable the bit (which is technically within
231 * the spec but a bad idea) and some systems have the bit
232 * forced to zero even though interrupts work (which is
233 * clearly outside the spec). The next bool tells which form
234 * of brokenness is present.
236 bool cannot_disable_irq;
239 * Some systems are broken and cannot set the irq enable
240 * bit, even if they support interrupts.
242 bool irq_enable_broken;
245 * Did we get an attention that we did not handle?
247 bool got_attn;
249 /* From the get device id response... */
250 struct ipmi_device_id device_id;
252 /* Default driver model device. */
253 struct platform_device *pdev;
255 /* Have we added the device group to the device? */
256 bool dev_group_added;
258 /* Counters and things for the proc filesystem. */
259 atomic_t stats[SI_NUM_STATS];
261 struct task_struct *thread;
263 struct list_head link;
266 #define smi_inc_stat(smi, stat) \
267 atomic_inc(&(smi)->stats[SI_STAT_ ## stat])
268 #define smi_get_stat(smi, stat) \
269 ((unsigned int) atomic_read(&(smi)->stats[SI_STAT_ ## stat]))
271 #define IPMI_MAX_INTFS 4
272 static int force_kipmid[IPMI_MAX_INTFS];
273 static int num_force_kipmid;
275 static unsigned int kipmid_max_busy_us[IPMI_MAX_INTFS];
276 static int num_max_busy_us;
278 static bool unload_when_empty = true;
280 static int try_smi_init(struct smi_info *smi);
281 static void cleanup_one_si(struct smi_info *to_clean);
282 static void cleanup_ipmi_si(void);
284 #ifdef DEBUG_TIMING
285 void debug_timestamp(char *msg)
287 struct timespec64 t;
289 getnstimeofday64(&t);
290 pr_debug("**%s: %lld.%9.9ld\n", msg, (long long) t.tv_sec, t.tv_nsec);
292 #else
293 #define debug_timestamp(x)
294 #endif
296 static ATOMIC_NOTIFIER_HEAD(xaction_notifier_list);
297 static int register_xaction_notifier(struct notifier_block *nb)
299 return atomic_notifier_chain_register(&xaction_notifier_list, nb);
302 static void deliver_recv_msg(struct smi_info *smi_info,
303 struct ipmi_smi_msg *msg)
305 /* Deliver the message to the upper layer. */
306 if (smi_info->intf)
307 ipmi_smi_msg_received(smi_info->intf, msg);
308 else
309 ipmi_free_smi_msg(msg);
312 static void return_hosed_msg(struct smi_info *smi_info, int cCode)
314 struct ipmi_smi_msg *msg = smi_info->curr_msg;
316 if (cCode < 0 || cCode > IPMI_ERR_UNSPECIFIED)
317 cCode = IPMI_ERR_UNSPECIFIED;
318 /* else use it as is */
320 /* Make it a response */
321 msg->rsp[0] = msg->data[0] | 4;
322 msg->rsp[1] = msg->data[1];
323 msg->rsp[2] = cCode;
324 msg->rsp_size = 3;
326 smi_info->curr_msg = NULL;
327 deliver_recv_msg(smi_info, msg);
330 static enum si_sm_result start_next_msg(struct smi_info *smi_info)
332 int rv;
334 if (!smi_info->waiting_msg) {
335 smi_info->curr_msg = NULL;
336 rv = SI_SM_IDLE;
337 } else {
338 int err;
340 smi_info->curr_msg = smi_info->waiting_msg;
341 smi_info->waiting_msg = NULL;
342 debug_timestamp("Start2");
343 err = atomic_notifier_call_chain(&xaction_notifier_list,
344 0, smi_info);
345 if (err & NOTIFY_STOP_MASK) {
346 rv = SI_SM_CALL_WITHOUT_DELAY;
347 goto out;
349 err = smi_info->handlers->start_transaction(
350 smi_info->si_sm,
351 smi_info->curr_msg->data,
352 smi_info->curr_msg->data_size);
353 if (err)
354 return_hosed_msg(smi_info, err);
356 rv = SI_SM_CALL_WITHOUT_DELAY;
358 out:
359 return rv;
362 static void smi_mod_timer(struct smi_info *smi_info, unsigned long new_val)
364 if (!smi_info->timer_can_start)
365 return;
366 smi_info->last_timeout_jiffies = jiffies;
367 mod_timer(&smi_info->si_timer, new_val);
368 smi_info->timer_running = true;
372 * Start a new message and (re)start the timer and thread.
374 static void start_new_msg(struct smi_info *smi_info, unsigned char *msg,
375 unsigned int size)
377 smi_mod_timer(smi_info, jiffies + SI_TIMEOUT_JIFFIES);
379 if (smi_info->thread)
380 wake_up_process(smi_info->thread);
382 smi_info->handlers->start_transaction(smi_info->si_sm, msg, size);
385 static void start_check_enables(struct smi_info *smi_info)
387 unsigned char msg[2];
389 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
390 msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
392 start_new_msg(smi_info, msg, 2);
393 smi_info->si_state = SI_CHECKING_ENABLES;
396 static void start_clear_flags(struct smi_info *smi_info)
398 unsigned char msg[3];
400 /* Make sure the watchdog pre-timeout flag is not set at startup. */
401 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
402 msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
403 msg[2] = WDT_PRE_TIMEOUT_INT;
405 start_new_msg(smi_info, msg, 3);
406 smi_info->si_state = SI_CLEARING_FLAGS;
409 static void start_getting_msg_queue(struct smi_info *smi_info)
411 smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
412 smi_info->curr_msg->data[1] = IPMI_GET_MSG_CMD;
413 smi_info->curr_msg->data_size = 2;
415 start_new_msg(smi_info, smi_info->curr_msg->data,
416 smi_info->curr_msg->data_size);
417 smi_info->si_state = SI_GETTING_MESSAGES;
420 static void start_getting_events(struct smi_info *smi_info)
422 smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
423 smi_info->curr_msg->data[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD;
424 smi_info->curr_msg->data_size = 2;
426 start_new_msg(smi_info, smi_info->curr_msg->data,
427 smi_info->curr_msg->data_size);
428 smi_info->si_state = SI_GETTING_EVENTS;
432 * When we have a situtaion where we run out of memory and cannot
433 * allocate messages, we just leave them in the BMC and run the system
434 * polled until we can allocate some memory. Once we have some
435 * memory, we will re-enable the interrupt.
437 * Note that we cannot just use disable_irq(), since the interrupt may
438 * be shared.
440 static inline bool disable_si_irq(struct smi_info *smi_info)
442 if ((smi_info->io.irq) && (!smi_info->interrupt_disabled)) {
443 smi_info->interrupt_disabled = true;
444 start_check_enables(smi_info);
445 return true;
447 return false;
450 static inline bool enable_si_irq(struct smi_info *smi_info)
452 if ((smi_info->io.irq) && (smi_info->interrupt_disabled)) {
453 smi_info->interrupt_disabled = false;
454 start_check_enables(smi_info);
455 return true;
457 return false;
461 * Allocate a message. If unable to allocate, start the interrupt
462 * disable process and return NULL. If able to allocate but
463 * interrupts are disabled, free the message and return NULL after
464 * starting the interrupt enable process.
466 static struct ipmi_smi_msg *alloc_msg_handle_irq(struct smi_info *smi_info)
468 struct ipmi_smi_msg *msg;
470 msg = ipmi_alloc_smi_msg();
471 if (!msg) {
472 if (!disable_si_irq(smi_info))
473 smi_info->si_state = SI_NORMAL;
474 } else if (enable_si_irq(smi_info)) {
475 ipmi_free_smi_msg(msg);
476 msg = NULL;
478 return msg;
481 static void handle_flags(struct smi_info *smi_info)
483 retry:
484 if (smi_info->msg_flags & WDT_PRE_TIMEOUT_INT) {
485 /* Watchdog pre-timeout */
486 smi_inc_stat(smi_info, watchdog_pretimeouts);
488 start_clear_flags(smi_info);
489 smi_info->msg_flags &= ~WDT_PRE_TIMEOUT_INT;
490 if (smi_info->intf)
491 ipmi_smi_watchdog_pretimeout(smi_info->intf);
492 } else if (smi_info->msg_flags & RECEIVE_MSG_AVAIL) {
493 /* Messages available. */
494 smi_info->curr_msg = alloc_msg_handle_irq(smi_info);
495 if (!smi_info->curr_msg)
496 return;
498 start_getting_msg_queue(smi_info);
499 } else if (smi_info->msg_flags & EVENT_MSG_BUFFER_FULL) {
500 /* Events available. */
501 smi_info->curr_msg = alloc_msg_handle_irq(smi_info);
502 if (!smi_info->curr_msg)
503 return;
505 start_getting_events(smi_info);
506 } else if (smi_info->msg_flags & OEM_DATA_AVAIL &&
507 smi_info->oem_data_avail_handler) {
508 if (smi_info->oem_data_avail_handler(smi_info))
509 goto retry;
510 } else
511 smi_info->si_state = SI_NORMAL;
515 * Global enables we care about.
517 #define GLOBAL_ENABLES_MASK (IPMI_BMC_EVT_MSG_BUFF | IPMI_BMC_RCV_MSG_INTR | \
518 IPMI_BMC_EVT_MSG_INTR)
520 static u8 current_global_enables(struct smi_info *smi_info, u8 base,
521 bool *irq_on)
523 u8 enables = 0;
525 if (smi_info->supports_event_msg_buff)
526 enables |= IPMI_BMC_EVT_MSG_BUFF;
528 if (((smi_info->io.irq && !smi_info->interrupt_disabled) ||
529 smi_info->cannot_disable_irq) &&
530 !smi_info->irq_enable_broken)
531 enables |= IPMI_BMC_RCV_MSG_INTR;
533 if (smi_info->supports_event_msg_buff &&
534 smi_info->io.irq && !smi_info->interrupt_disabled &&
535 !smi_info->irq_enable_broken)
536 enables |= IPMI_BMC_EVT_MSG_INTR;
538 *irq_on = enables & (IPMI_BMC_EVT_MSG_INTR | IPMI_BMC_RCV_MSG_INTR);
540 return enables;
543 static void check_bt_irq(struct smi_info *smi_info, bool irq_on)
545 u8 irqstate = smi_info->io.inputb(&smi_info->io, IPMI_BT_INTMASK_REG);
547 irqstate &= IPMI_BT_INTMASK_ENABLE_IRQ_BIT;
549 if ((bool)irqstate == irq_on)
550 return;
552 if (irq_on)
553 smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG,
554 IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
555 else
556 smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG, 0);
559 static void handle_transaction_done(struct smi_info *smi_info)
561 struct ipmi_smi_msg *msg;
563 debug_timestamp("Done");
564 switch (smi_info->si_state) {
565 case SI_NORMAL:
566 if (!smi_info->curr_msg)
567 break;
569 smi_info->curr_msg->rsp_size
570 = smi_info->handlers->get_result(
571 smi_info->si_sm,
572 smi_info->curr_msg->rsp,
573 IPMI_MAX_MSG_LENGTH);
576 * Do this here becase deliver_recv_msg() releases the
577 * lock, and a new message can be put in during the
578 * time the lock is released.
580 msg = smi_info->curr_msg;
581 smi_info->curr_msg = NULL;
582 deliver_recv_msg(smi_info, msg);
583 break;
585 case SI_GETTING_FLAGS:
587 unsigned char msg[4];
588 unsigned int len;
590 /* We got the flags from the SMI, now handle them. */
591 len = smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
592 if (msg[2] != 0) {
593 /* Error fetching flags, just give up for now. */
594 smi_info->si_state = SI_NORMAL;
595 } else if (len < 4) {
597 * Hmm, no flags. That's technically illegal, but
598 * don't use uninitialized data.
600 smi_info->si_state = SI_NORMAL;
601 } else {
602 smi_info->msg_flags = msg[3];
603 handle_flags(smi_info);
605 break;
608 case SI_CLEARING_FLAGS:
610 unsigned char msg[3];
612 /* We cleared the flags. */
613 smi_info->handlers->get_result(smi_info->si_sm, msg, 3);
614 if (msg[2] != 0) {
615 /* Error clearing flags */
616 dev_warn(smi_info->io.dev,
617 "Error clearing flags: %2.2x\n", msg[2]);
619 smi_info->si_state = SI_NORMAL;
620 break;
623 case SI_GETTING_EVENTS:
625 smi_info->curr_msg->rsp_size
626 = smi_info->handlers->get_result(
627 smi_info->si_sm,
628 smi_info->curr_msg->rsp,
629 IPMI_MAX_MSG_LENGTH);
632 * Do this here becase deliver_recv_msg() releases the
633 * lock, and a new message can be put in during the
634 * time the lock is released.
636 msg = smi_info->curr_msg;
637 smi_info->curr_msg = NULL;
638 if (msg->rsp[2] != 0) {
639 /* Error getting event, probably done. */
640 msg->done(msg);
642 /* Take off the event flag. */
643 smi_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
644 handle_flags(smi_info);
645 } else {
646 smi_inc_stat(smi_info, events);
649 * Do this before we deliver the message
650 * because delivering the message releases the
651 * lock and something else can mess with the
652 * state.
654 handle_flags(smi_info);
656 deliver_recv_msg(smi_info, msg);
658 break;
661 case SI_GETTING_MESSAGES:
663 smi_info->curr_msg->rsp_size
664 = smi_info->handlers->get_result(
665 smi_info->si_sm,
666 smi_info->curr_msg->rsp,
667 IPMI_MAX_MSG_LENGTH);
670 * Do this here becase deliver_recv_msg() releases the
671 * lock, and a new message can be put in during the
672 * time the lock is released.
674 msg = smi_info->curr_msg;
675 smi_info->curr_msg = NULL;
676 if (msg->rsp[2] != 0) {
677 /* Error getting event, probably done. */
678 msg->done(msg);
680 /* Take off the msg flag. */
681 smi_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
682 handle_flags(smi_info);
683 } else {
684 smi_inc_stat(smi_info, incoming_messages);
687 * Do this before we deliver the message
688 * because delivering the message releases the
689 * lock and something else can mess with the
690 * state.
692 handle_flags(smi_info);
694 deliver_recv_msg(smi_info, msg);
696 break;
699 case SI_CHECKING_ENABLES:
701 unsigned char msg[4];
702 u8 enables;
703 bool irq_on;
705 /* We got the flags from the SMI, now handle them. */
706 smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
707 if (msg[2] != 0) {
708 dev_warn(smi_info->io.dev,
709 "Couldn't get irq info: %x.\n", msg[2]);
710 dev_warn(smi_info->io.dev,
711 "Maybe ok, but ipmi might run very slowly.\n");
712 smi_info->si_state = SI_NORMAL;
713 break;
715 enables = current_global_enables(smi_info, 0, &irq_on);
716 if (smi_info->io.si_type == SI_BT)
717 /* BT has its own interrupt enable bit. */
718 check_bt_irq(smi_info, irq_on);
719 if (enables != (msg[3] & GLOBAL_ENABLES_MASK)) {
720 /* Enables are not correct, fix them. */
721 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
722 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
723 msg[2] = enables | (msg[3] & ~GLOBAL_ENABLES_MASK);
724 smi_info->handlers->start_transaction(
725 smi_info->si_sm, msg, 3);
726 smi_info->si_state = SI_SETTING_ENABLES;
727 } else if (smi_info->supports_event_msg_buff) {
728 smi_info->curr_msg = ipmi_alloc_smi_msg();
729 if (!smi_info->curr_msg) {
730 smi_info->si_state = SI_NORMAL;
731 break;
733 start_getting_events(smi_info);
734 } else {
735 smi_info->si_state = SI_NORMAL;
737 break;
740 case SI_SETTING_ENABLES:
742 unsigned char msg[4];
744 smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
745 if (msg[2] != 0)
746 dev_warn(smi_info->io.dev,
747 "Could not set the global enables: 0x%x.\n",
748 msg[2]);
750 if (smi_info->supports_event_msg_buff) {
751 smi_info->curr_msg = ipmi_alloc_smi_msg();
752 if (!smi_info->curr_msg) {
753 smi_info->si_state = SI_NORMAL;
754 break;
756 start_getting_events(smi_info);
757 } else {
758 smi_info->si_state = SI_NORMAL;
760 break;
766 * Called on timeouts and events. Timeouts should pass the elapsed
767 * time, interrupts should pass in zero. Must be called with
768 * si_lock held and interrupts disabled.
770 static enum si_sm_result smi_event_handler(struct smi_info *smi_info,
771 int time)
773 enum si_sm_result si_sm_result;
775 restart:
777 * There used to be a loop here that waited a little while
778 * (around 25us) before giving up. That turned out to be
779 * pointless, the minimum delays I was seeing were in the 300us
780 * range, which is far too long to wait in an interrupt. So
781 * we just run until the state machine tells us something
782 * happened or it needs a delay.
784 si_sm_result = smi_info->handlers->event(smi_info->si_sm, time);
785 time = 0;
786 while (si_sm_result == SI_SM_CALL_WITHOUT_DELAY)
787 si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
789 if (si_sm_result == SI_SM_TRANSACTION_COMPLETE) {
790 smi_inc_stat(smi_info, complete_transactions);
792 handle_transaction_done(smi_info);
793 goto restart;
794 } else if (si_sm_result == SI_SM_HOSED) {
795 smi_inc_stat(smi_info, hosed_count);
798 * Do the before return_hosed_msg, because that
799 * releases the lock.
801 smi_info->si_state = SI_NORMAL;
802 if (smi_info->curr_msg != NULL) {
804 * If we were handling a user message, format
805 * a response to send to the upper layer to
806 * tell it about the error.
808 return_hosed_msg(smi_info, IPMI_ERR_UNSPECIFIED);
810 goto restart;
814 * We prefer handling attn over new messages. But don't do
815 * this if there is not yet an upper layer to handle anything.
817 if (likely(smi_info->intf) &&
818 (si_sm_result == SI_SM_ATTN || smi_info->got_attn)) {
819 unsigned char msg[2];
821 if (smi_info->si_state != SI_NORMAL) {
823 * We got an ATTN, but we are doing something else.
824 * Handle the ATTN later.
826 smi_info->got_attn = true;
827 } else {
828 smi_info->got_attn = false;
829 smi_inc_stat(smi_info, attentions);
832 * Got a attn, send down a get message flags to see
833 * what's causing it. It would be better to handle
834 * this in the upper layer, but due to the way
835 * interrupts work with the SMI, that's not really
836 * possible.
838 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
839 msg[1] = IPMI_GET_MSG_FLAGS_CMD;
841 start_new_msg(smi_info, msg, 2);
842 smi_info->si_state = SI_GETTING_FLAGS;
843 goto restart;
847 /* If we are currently idle, try to start the next message. */
848 if (si_sm_result == SI_SM_IDLE) {
849 smi_inc_stat(smi_info, idles);
851 si_sm_result = start_next_msg(smi_info);
852 if (si_sm_result != SI_SM_IDLE)
853 goto restart;
856 if ((si_sm_result == SI_SM_IDLE)
857 && (atomic_read(&smi_info->req_events))) {
859 * We are idle and the upper layer requested that I fetch
860 * events, so do so.
862 atomic_set(&smi_info->req_events, 0);
865 * Take this opportunity to check the interrupt and
866 * message enable state for the BMC. The BMC can be
867 * asynchronously reset, and may thus get interrupts
868 * disable and messages disabled.
870 if (smi_info->supports_event_msg_buff || smi_info->io.irq) {
871 start_check_enables(smi_info);
872 } else {
873 smi_info->curr_msg = alloc_msg_handle_irq(smi_info);
874 if (!smi_info->curr_msg)
875 goto out;
877 start_getting_events(smi_info);
879 goto restart;
882 if (si_sm_result == SI_SM_IDLE && smi_info->timer_running) {
883 /* Ok it if fails, the timer will just go off. */
884 if (del_timer(&smi_info->si_timer))
885 smi_info->timer_running = false;
888 out:
889 return si_sm_result;
892 static void check_start_timer_thread(struct smi_info *smi_info)
894 if (smi_info->si_state == SI_NORMAL && smi_info->curr_msg == NULL) {
895 smi_mod_timer(smi_info, jiffies + SI_TIMEOUT_JIFFIES);
897 if (smi_info->thread)
898 wake_up_process(smi_info->thread);
900 start_next_msg(smi_info);
901 smi_event_handler(smi_info, 0);
905 static void flush_messages(void *send_info)
907 struct smi_info *smi_info = send_info;
908 enum si_sm_result result;
911 * Currently, this function is called only in run-to-completion
912 * mode. This means we are single-threaded, no need for locks.
914 result = smi_event_handler(smi_info, 0);
915 while (result != SI_SM_IDLE) {
916 udelay(SI_SHORT_TIMEOUT_USEC);
917 result = smi_event_handler(smi_info, SI_SHORT_TIMEOUT_USEC);
921 static void sender(void *send_info,
922 struct ipmi_smi_msg *msg)
924 struct smi_info *smi_info = send_info;
925 unsigned long flags;
927 debug_timestamp("Enqueue");
929 if (smi_info->run_to_completion) {
931 * If we are running to completion, start it. Upper
932 * layer will call flush_messages to clear it out.
934 smi_info->waiting_msg = msg;
935 return;
938 spin_lock_irqsave(&smi_info->si_lock, flags);
940 * The following two lines don't need to be under the lock for
941 * the lock's sake, but they do need SMP memory barriers to
942 * avoid getting things out of order. We are already claiming
943 * the lock, anyway, so just do it under the lock to avoid the
944 * ordering problem.
946 BUG_ON(smi_info->waiting_msg);
947 smi_info->waiting_msg = msg;
948 check_start_timer_thread(smi_info);
949 spin_unlock_irqrestore(&smi_info->si_lock, flags);
952 static void set_run_to_completion(void *send_info, bool i_run_to_completion)
954 struct smi_info *smi_info = send_info;
956 smi_info->run_to_completion = i_run_to_completion;
957 if (i_run_to_completion)
958 flush_messages(smi_info);
962 * Use -1 in the nsec value of the busy waiting timespec to tell that
963 * we are spinning in kipmid looking for something and not delaying
964 * between checks
966 static inline void ipmi_si_set_not_busy(struct timespec64 *ts)
968 ts->tv_nsec = -1;
970 static inline int ipmi_si_is_busy(struct timespec64 *ts)
972 return ts->tv_nsec != -1;
975 static inline int ipmi_thread_busy_wait(enum si_sm_result smi_result,
976 const struct smi_info *smi_info,
977 struct timespec64 *busy_until)
979 unsigned int max_busy_us = 0;
981 if (smi_info->intf_num < num_max_busy_us)
982 max_busy_us = kipmid_max_busy_us[smi_info->intf_num];
983 if (max_busy_us == 0 || smi_result != SI_SM_CALL_WITH_DELAY)
984 ipmi_si_set_not_busy(busy_until);
985 else if (!ipmi_si_is_busy(busy_until)) {
986 getnstimeofday64(busy_until);
987 timespec64_add_ns(busy_until, max_busy_us*NSEC_PER_USEC);
988 } else {
989 struct timespec64 now;
991 getnstimeofday64(&now);
992 if (unlikely(timespec64_compare(&now, busy_until) > 0)) {
993 ipmi_si_set_not_busy(busy_until);
994 return 0;
997 return 1;
1002 * A busy-waiting loop for speeding up IPMI operation.
1004 * Lousy hardware makes this hard. This is only enabled for systems
1005 * that are not BT and do not have interrupts. It starts spinning
1006 * when an operation is complete or until max_busy tells it to stop
1007 * (if that is enabled). See the paragraph on kimid_max_busy_us in
1008 * Documentation/IPMI.txt for details.
1010 static int ipmi_thread(void *data)
1012 struct smi_info *smi_info = data;
1013 unsigned long flags;
1014 enum si_sm_result smi_result;
1015 struct timespec64 busy_until;
1017 ipmi_si_set_not_busy(&busy_until);
1018 set_user_nice(current, MAX_NICE);
1019 while (!kthread_should_stop()) {
1020 int busy_wait;
1022 spin_lock_irqsave(&(smi_info->si_lock), flags);
1023 smi_result = smi_event_handler(smi_info, 0);
1026 * If the driver is doing something, there is a possible
1027 * race with the timer. If the timer handler see idle,
1028 * and the thread here sees something else, the timer
1029 * handler won't restart the timer even though it is
1030 * required. So start it here if necessary.
1032 if (smi_result != SI_SM_IDLE && !smi_info->timer_running)
1033 smi_mod_timer(smi_info, jiffies + SI_TIMEOUT_JIFFIES);
1035 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
1036 busy_wait = ipmi_thread_busy_wait(smi_result, smi_info,
1037 &busy_until);
1038 if (smi_result == SI_SM_CALL_WITHOUT_DELAY)
1039 ; /* do nothing */
1040 else if (smi_result == SI_SM_CALL_WITH_DELAY && busy_wait)
1041 schedule();
1042 else if (smi_result == SI_SM_IDLE) {
1043 if (atomic_read(&smi_info->need_watch)) {
1044 schedule_timeout_interruptible(100);
1045 } else {
1046 /* Wait to be woken up when we are needed. */
1047 __set_current_state(TASK_INTERRUPTIBLE);
1048 schedule();
1050 } else
1051 schedule_timeout_interruptible(1);
1053 return 0;
1057 static void poll(void *send_info)
1059 struct smi_info *smi_info = send_info;
1060 unsigned long flags = 0;
1061 bool run_to_completion = smi_info->run_to_completion;
1064 * Make sure there is some delay in the poll loop so we can
1065 * drive time forward and timeout things.
1067 udelay(10);
1068 if (!run_to_completion)
1069 spin_lock_irqsave(&smi_info->si_lock, flags);
1070 smi_event_handler(smi_info, 10);
1071 if (!run_to_completion)
1072 spin_unlock_irqrestore(&smi_info->si_lock, flags);
1075 static void request_events(void *send_info)
1077 struct smi_info *smi_info = send_info;
1079 if (!smi_info->has_event_buffer)
1080 return;
1082 atomic_set(&smi_info->req_events, 1);
1085 static void set_need_watch(void *send_info, bool enable)
1087 struct smi_info *smi_info = send_info;
1088 unsigned long flags;
1090 atomic_set(&smi_info->need_watch, enable);
1091 spin_lock_irqsave(&smi_info->si_lock, flags);
1092 check_start_timer_thread(smi_info);
1093 spin_unlock_irqrestore(&smi_info->si_lock, flags);
1096 static void smi_timeout(struct timer_list *t)
1098 struct smi_info *smi_info = from_timer(smi_info, t, si_timer);
1099 enum si_sm_result smi_result;
1100 unsigned long flags;
1101 unsigned long jiffies_now;
1102 long time_diff;
1103 long timeout;
1105 spin_lock_irqsave(&(smi_info->si_lock), flags);
1106 debug_timestamp("Timer");
1108 jiffies_now = jiffies;
1109 time_diff = (((long)jiffies_now - (long)smi_info->last_timeout_jiffies)
1110 * SI_USEC_PER_JIFFY);
1111 smi_result = smi_event_handler(smi_info, time_diff);
1113 if ((smi_info->io.irq) && (!smi_info->interrupt_disabled)) {
1114 /* Running with interrupts, only do long timeouts. */
1115 timeout = jiffies + SI_TIMEOUT_JIFFIES;
1116 smi_inc_stat(smi_info, long_timeouts);
1117 goto do_mod_timer;
1121 * If the state machine asks for a short delay, then shorten
1122 * the timer timeout.
1124 if (smi_result == SI_SM_CALL_WITH_DELAY) {
1125 smi_inc_stat(smi_info, short_timeouts);
1126 timeout = jiffies + 1;
1127 } else {
1128 smi_inc_stat(smi_info, long_timeouts);
1129 timeout = jiffies + SI_TIMEOUT_JIFFIES;
1132 do_mod_timer:
1133 if (smi_result != SI_SM_IDLE)
1134 smi_mod_timer(smi_info, timeout);
1135 else
1136 smi_info->timer_running = false;
1137 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
1140 irqreturn_t ipmi_si_irq_handler(int irq, void *data)
1142 struct smi_info *smi_info = data;
1143 unsigned long flags;
1145 if (smi_info->io.si_type == SI_BT)
1146 /* We need to clear the IRQ flag for the BT interface. */
1147 smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG,
1148 IPMI_BT_INTMASK_CLEAR_IRQ_BIT
1149 | IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
1151 spin_lock_irqsave(&(smi_info->si_lock), flags);
1153 smi_inc_stat(smi_info, interrupts);
1155 debug_timestamp("Interrupt");
1157 smi_event_handler(smi_info, 0);
1158 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
1159 return IRQ_HANDLED;
1162 static int smi_start_processing(void *send_info,
1163 ipmi_smi_t intf)
1165 struct smi_info *new_smi = send_info;
1166 int enable = 0;
1168 new_smi->intf = intf;
1170 /* Set up the timer that drives the interface. */
1171 timer_setup(&new_smi->si_timer, smi_timeout, 0);
1172 new_smi->timer_can_start = true;
1173 smi_mod_timer(new_smi, jiffies + SI_TIMEOUT_JIFFIES);
1175 /* Try to claim any interrupts. */
1176 if (new_smi->io.irq_setup) {
1177 new_smi->io.irq_handler_data = new_smi;
1178 new_smi->io.irq_setup(&new_smi->io);
1182 * Check if the user forcefully enabled the daemon.
1184 if (new_smi->intf_num < num_force_kipmid)
1185 enable = force_kipmid[new_smi->intf_num];
1187 * The BT interface is efficient enough to not need a thread,
1188 * and there is no need for a thread if we have interrupts.
1190 else if ((new_smi->io.si_type != SI_BT) && (!new_smi->io.irq))
1191 enable = 1;
1193 if (enable) {
1194 new_smi->thread = kthread_run(ipmi_thread, new_smi,
1195 "kipmi%d", new_smi->intf_num);
1196 if (IS_ERR(new_smi->thread)) {
1197 dev_notice(new_smi->io.dev, "Could not start"
1198 " kernel thread due to error %ld, only using"
1199 " timers to drive the interface\n",
1200 PTR_ERR(new_smi->thread));
1201 new_smi->thread = NULL;
1205 return 0;
1208 static int get_smi_info(void *send_info, struct ipmi_smi_info *data)
1210 struct smi_info *smi = send_info;
1212 data->addr_src = smi->io.addr_source;
1213 data->dev = smi->io.dev;
1214 data->addr_info = smi->io.addr_info;
1215 get_device(smi->io.dev);
1217 return 0;
1220 static void set_maintenance_mode(void *send_info, bool enable)
1222 struct smi_info *smi_info = send_info;
1224 if (!enable)
1225 atomic_set(&smi_info->req_events, 0);
1228 static const struct ipmi_smi_handlers handlers = {
1229 .owner = THIS_MODULE,
1230 .start_processing = smi_start_processing,
1231 .get_smi_info = get_smi_info,
1232 .sender = sender,
1233 .request_events = request_events,
1234 .set_need_watch = set_need_watch,
1235 .set_maintenance_mode = set_maintenance_mode,
1236 .set_run_to_completion = set_run_to_completion,
1237 .flush_messages = flush_messages,
1238 .poll = poll,
1241 static LIST_HEAD(smi_infos);
1242 static DEFINE_MUTEX(smi_infos_lock);
1243 static int smi_num; /* Used to sequence the SMIs */
1245 static const char * const addr_space_to_str[] = { "i/o", "mem" };
1247 module_param_array(force_kipmid, int, &num_force_kipmid, 0);
1248 MODULE_PARM_DESC(force_kipmid, "Force the kipmi daemon to be enabled (1) or"
1249 " disabled(0). Normally the IPMI driver auto-detects"
1250 " this, but the value may be overridden by this parm.");
1251 module_param(unload_when_empty, bool, 0);
1252 MODULE_PARM_DESC(unload_when_empty, "Unload the module if no interfaces are"
1253 " specified or found, default is 1. Setting to 0"
1254 " is useful for hot add of devices using hotmod.");
1255 module_param_array(kipmid_max_busy_us, uint, &num_max_busy_us, 0644);
1256 MODULE_PARM_DESC(kipmid_max_busy_us,
1257 "Max time (in microseconds) to busy-wait for IPMI data before"
1258 " sleeping. 0 (default) means to wait forever. Set to 100-500"
1259 " if kipmid is using up a lot of CPU time.");
1261 void ipmi_irq_finish_setup(struct si_sm_io *io)
1263 if (io->si_type == SI_BT)
1264 /* Enable the interrupt in the BT interface. */
1265 io->outputb(io, IPMI_BT_INTMASK_REG,
1266 IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
1269 void ipmi_irq_start_cleanup(struct si_sm_io *io)
1271 if (io->si_type == SI_BT)
1272 /* Disable the interrupt in the BT interface. */
1273 io->outputb(io, IPMI_BT_INTMASK_REG, 0);
1276 static void std_irq_cleanup(struct si_sm_io *io)
1278 ipmi_irq_start_cleanup(io);
1279 free_irq(io->irq, io->irq_handler_data);
1282 int ipmi_std_irq_setup(struct si_sm_io *io)
1284 int rv;
1286 if (!io->irq)
1287 return 0;
1289 rv = request_irq(io->irq,
1290 ipmi_si_irq_handler,
1291 IRQF_SHARED,
1292 DEVICE_NAME,
1293 io->irq_handler_data);
1294 if (rv) {
1295 dev_warn(io->dev, "%s unable to claim interrupt %d,"
1296 " running polled\n",
1297 DEVICE_NAME, io->irq);
1298 io->irq = 0;
1299 } else {
1300 io->irq_cleanup = std_irq_cleanup;
1301 ipmi_irq_finish_setup(io);
1302 dev_info(io->dev, "Using irq %d\n", io->irq);
1305 return rv;
1308 static int wait_for_msg_done(struct smi_info *smi_info)
1310 enum si_sm_result smi_result;
1312 smi_result = smi_info->handlers->event(smi_info->si_sm, 0);
1313 for (;;) {
1314 if (smi_result == SI_SM_CALL_WITH_DELAY ||
1315 smi_result == SI_SM_CALL_WITH_TICK_DELAY) {
1316 schedule_timeout_uninterruptible(1);
1317 smi_result = smi_info->handlers->event(
1318 smi_info->si_sm, jiffies_to_usecs(1));
1319 } else if (smi_result == SI_SM_CALL_WITHOUT_DELAY) {
1320 smi_result = smi_info->handlers->event(
1321 smi_info->si_sm, 0);
1322 } else
1323 break;
1325 if (smi_result == SI_SM_HOSED)
1327 * We couldn't get the state machine to run, so whatever's at
1328 * the port is probably not an IPMI SMI interface.
1330 return -ENODEV;
1332 return 0;
1335 static int try_get_dev_id(struct smi_info *smi_info)
1337 unsigned char msg[2];
1338 unsigned char *resp;
1339 unsigned long resp_len;
1340 int rv = 0;
1342 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1343 if (!resp)
1344 return -ENOMEM;
1347 * Do a Get Device ID command, since it comes back with some
1348 * useful info.
1350 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1351 msg[1] = IPMI_GET_DEVICE_ID_CMD;
1352 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
1354 rv = wait_for_msg_done(smi_info);
1355 if (rv)
1356 goto out;
1358 resp_len = smi_info->handlers->get_result(smi_info->si_sm,
1359 resp, IPMI_MAX_MSG_LENGTH);
1361 /* Check and record info from the get device id, in case we need it. */
1362 rv = ipmi_demangle_device_id(resp[0] >> 2, resp[1],
1363 resp + 2, resp_len - 2, &smi_info->device_id);
1365 out:
1366 kfree(resp);
1367 return rv;
1370 static int get_global_enables(struct smi_info *smi_info, u8 *enables)
1372 unsigned char msg[3];
1373 unsigned char *resp;
1374 unsigned long resp_len;
1375 int rv;
1377 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1378 if (!resp)
1379 return -ENOMEM;
1381 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1382 msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
1383 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
1385 rv = wait_for_msg_done(smi_info);
1386 if (rv) {
1387 dev_warn(smi_info->io.dev,
1388 "Error getting response from get global enables command: %d\n",
1389 rv);
1390 goto out;
1393 resp_len = smi_info->handlers->get_result(smi_info->si_sm,
1394 resp, IPMI_MAX_MSG_LENGTH);
1396 if (resp_len < 4 ||
1397 resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
1398 resp[1] != IPMI_GET_BMC_GLOBAL_ENABLES_CMD ||
1399 resp[2] != 0) {
1400 dev_warn(smi_info->io.dev,
1401 "Invalid return from get global enables command: %ld %x %x %x\n",
1402 resp_len, resp[0], resp[1], resp[2]);
1403 rv = -EINVAL;
1404 goto out;
1405 } else {
1406 *enables = resp[3];
1409 out:
1410 kfree(resp);
1411 return rv;
1415 * Returns 1 if it gets an error from the command.
1417 static int set_global_enables(struct smi_info *smi_info, u8 enables)
1419 unsigned char msg[3];
1420 unsigned char *resp;
1421 unsigned long resp_len;
1422 int rv;
1424 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1425 if (!resp)
1426 return -ENOMEM;
1428 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1429 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
1430 msg[2] = enables;
1431 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 3);
1433 rv = wait_for_msg_done(smi_info);
1434 if (rv) {
1435 dev_warn(smi_info->io.dev,
1436 "Error getting response from set global enables command: %d\n",
1437 rv);
1438 goto out;
1441 resp_len = smi_info->handlers->get_result(smi_info->si_sm,
1442 resp, IPMI_MAX_MSG_LENGTH);
1444 if (resp_len < 3 ||
1445 resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
1446 resp[1] != IPMI_SET_BMC_GLOBAL_ENABLES_CMD) {
1447 dev_warn(smi_info->io.dev,
1448 "Invalid return from set global enables command: %ld %x %x\n",
1449 resp_len, resp[0], resp[1]);
1450 rv = -EINVAL;
1451 goto out;
1454 if (resp[2] != 0)
1455 rv = 1;
1457 out:
1458 kfree(resp);
1459 return rv;
1463 * Some BMCs do not support clearing the receive irq bit in the global
1464 * enables (even if they don't support interrupts on the BMC). Check
1465 * for this and handle it properly.
1467 static void check_clr_rcv_irq(struct smi_info *smi_info)
1469 u8 enables = 0;
1470 int rv;
1472 rv = get_global_enables(smi_info, &enables);
1473 if (!rv) {
1474 if ((enables & IPMI_BMC_RCV_MSG_INTR) == 0)
1475 /* Already clear, should work ok. */
1476 return;
1478 enables &= ~IPMI_BMC_RCV_MSG_INTR;
1479 rv = set_global_enables(smi_info, enables);
1482 if (rv < 0) {
1483 dev_err(smi_info->io.dev,
1484 "Cannot check clearing the rcv irq: %d\n", rv);
1485 return;
1488 if (rv) {
1490 * An error when setting the event buffer bit means
1491 * clearing the bit is not supported.
1493 dev_warn(smi_info->io.dev,
1494 "The BMC does not support clearing the recv irq bit, compensating, but the BMC needs to be fixed.\n");
1495 smi_info->cannot_disable_irq = true;
1500 * Some BMCs do not support setting the interrupt bits in the global
1501 * enables even if they support interrupts. Clearly bad, but we can
1502 * compensate.
1504 static void check_set_rcv_irq(struct smi_info *smi_info)
1506 u8 enables = 0;
1507 int rv;
1509 if (!smi_info->io.irq)
1510 return;
1512 rv = get_global_enables(smi_info, &enables);
1513 if (!rv) {
1514 enables |= IPMI_BMC_RCV_MSG_INTR;
1515 rv = set_global_enables(smi_info, enables);
1518 if (rv < 0) {
1519 dev_err(smi_info->io.dev,
1520 "Cannot check setting the rcv irq: %d\n", rv);
1521 return;
1524 if (rv) {
1526 * An error when setting the event buffer bit means
1527 * setting the bit is not supported.
1529 dev_warn(smi_info->io.dev,
1530 "The BMC does not support setting the recv irq bit, compensating, but the BMC needs to be fixed.\n");
1531 smi_info->cannot_disable_irq = true;
1532 smi_info->irq_enable_broken = true;
1536 static int try_enable_event_buffer(struct smi_info *smi_info)
1538 unsigned char msg[3];
1539 unsigned char *resp;
1540 unsigned long resp_len;
1541 int rv = 0;
1543 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1544 if (!resp)
1545 return -ENOMEM;
1547 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1548 msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
1549 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
1551 rv = wait_for_msg_done(smi_info);
1552 if (rv) {
1553 pr_warn(PFX "Error getting response from get global enables command, the event buffer is not enabled.\n");
1554 goto out;
1557 resp_len = smi_info->handlers->get_result(smi_info->si_sm,
1558 resp, IPMI_MAX_MSG_LENGTH);
1560 if (resp_len < 4 ||
1561 resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
1562 resp[1] != IPMI_GET_BMC_GLOBAL_ENABLES_CMD ||
1563 resp[2] != 0) {
1564 pr_warn(PFX "Invalid return from get global enables command, cannot enable the event buffer.\n");
1565 rv = -EINVAL;
1566 goto out;
1569 if (resp[3] & IPMI_BMC_EVT_MSG_BUFF) {
1570 /* buffer is already enabled, nothing to do. */
1571 smi_info->supports_event_msg_buff = true;
1572 goto out;
1575 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1576 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
1577 msg[2] = resp[3] | IPMI_BMC_EVT_MSG_BUFF;
1578 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 3);
1580 rv = wait_for_msg_done(smi_info);
1581 if (rv) {
1582 pr_warn(PFX "Error getting response from set global, enables command, the event buffer is not enabled.\n");
1583 goto out;
1586 resp_len = smi_info->handlers->get_result(smi_info->si_sm,
1587 resp, IPMI_MAX_MSG_LENGTH);
1589 if (resp_len < 3 ||
1590 resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
1591 resp[1] != IPMI_SET_BMC_GLOBAL_ENABLES_CMD) {
1592 pr_warn(PFX "Invalid return from get global, enables command, not enable the event buffer.\n");
1593 rv = -EINVAL;
1594 goto out;
1597 if (resp[2] != 0)
1599 * An error when setting the event buffer bit means
1600 * that the event buffer is not supported.
1602 rv = -ENOENT;
1603 else
1604 smi_info->supports_event_msg_buff = true;
1606 out:
1607 kfree(resp);
1608 return rv;
1611 #ifdef CONFIG_IPMI_PROC_INTERFACE
1612 static int smi_type_proc_show(struct seq_file *m, void *v)
1614 struct smi_info *smi = m->private;
1616 seq_printf(m, "%s\n", si_to_str[smi->io.si_type]);
1618 return 0;
1621 static int smi_type_proc_open(struct inode *inode, struct file *file)
1623 return single_open(file, smi_type_proc_show, PDE_DATA(inode));
1626 static const struct file_operations smi_type_proc_ops = {
1627 .open = smi_type_proc_open,
1628 .read = seq_read,
1629 .llseek = seq_lseek,
1630 .release = single_release,
1633 static int smi_si_stats_proc_show(struct seq_file *m, void *v)
1635 struct smi_info *smi = m->private;
1637 seq_printf(m, "interrupts_enabled: %d\n",
1638 smi->io.irq && !smi->interrupt_disabled);
1639 seq_printf(m, "short_timeouts: %u\n",
1640 smi_get_stat(smi, short_timeouts));
1641 seq_printf(m, "long_timeouts: %u\n",
1642 smi_get_stat(smi, long_timeouts));
1643 seq_printf(m, "idles: %u\n",
1644 smi_get_stat(smi, idles));
1645 seq_printf(m, "interrupts: %u\n",
1646 smi_get_stat(smi, interrupts));
1647 seq_printf(m, "attentions: %u\n",
1648 smi_get_stat(smi, attentions));
1649 seq_printf(m, "flag_fetches: %u\n",
1650 smi_get_stat(smi, flag_fetches));
1651 seq_printf(m, "hosed_count: %u\n",
1652 smi_get_stat(smi, hosed_count));
1653 seq_printf(m, "complete_transactions: %u\n",
1654 smi_get_stat(smi, complete_transactions));
1655 seq_printf(m, "events: %u\n",
1656 smi_get_stat(smi, events));
1657 seq_printf(m, "watchdog_pretimeouts: %u\n",
1658 smi_get_stat(smi, watchdog_pretimeouts));
1659 seq_printf(m, "incoming_messages: %u\n",
1660 smi_get_stat(smi, incoming_messages));
1661 return 0;
1664 static int smi_si_stats_proc_open(struct inode *inode, struct file *file)
1666 return single_open(file, smi_si_stats_proc_show, PDE_DATA(inode));
1669 static const struct file_operations smi_si_stats_proc_ops = {
1670 .open = smi_si_stats_proc_open,
1671 .read = seq_read,
1672 .llseek = seq_lseek,
1673 .release = single_release,
1676 static int smi_params_proc_show(struct seq_file *m, void *v)
1678 struct smi_info *smi = m->private;
1680 seq_printf(m,
1681 "%s,%s,0x%lx,rsp=%d,rsi=%d,rsh=%d,irq=%d,ipmb=%d\n",
1682 si_to_str[smi->io.si_type],
1683 addr_space_to_str[smi->io.addr_type],
1684 smi->io.addr_data,
1685 smi->io.regspacing,
1686 smi->io.regsize,
1687 smi->io.regshift,
1688 smi->io.irq,
1689 smi->io.slave_addr);
1691 return 0;
1694 static int smi_params_proc_open(struct inode *inode, struct file *file)
1696 return single_open(file, smi_params_proc_show, PDE_DATA(inode));
1699 static const struct file_operations smi_params_proc_ops = {
1700 .open = smi_params_proc_open,
1701 .read = seq_read,
1702 .llseek = seq_lseek,
1703 .release = single_release,
1705 #endif
1707 #define IPMI_SI_ATTR(name) \
1708 static ssize_t ipmi_##name##_show(struct device *dev, \
1709 struct device_attribute *attr, \
1710 char *buf) \
1712 struct smi_info *smi_info = dev_get_drvdata(dev); \
1714 return snprintf(buf, 10, "%u\n", smi_get_stat(smi_info, name)); \
1716 static DEVICE_ATTR(name, S_IRUGO, ipmi_##name##_show, NULL)
1718 static ssize_t ipmi_type_show(struct device *dev,
1719 struct device_attribute *attr,
1720 char *buf)
1722 struct smi_info *smi_info = dev_get_drvdata(dev);
1724 return snprintf(buf, 10, "%s\n", si_to_str[smi_info->io.si_type]);
1726 static DEVICE_ATTR(type, S_IRUGO, ipmi_type_show, NULL);
1728 static ssize_t ipmi_interrupts_enabled_show(struct device *dev,
1729 struct device_attribute *attr,
1730 char *buf)
1732 struct smi_info *smi_info = dev_get_drvdata(dev);
1733 int enabled = smi_info->io.irq && !smi_info->interrupt_disabled;
1735 return snprintf(buf, 10, "%d\n", enabled);
1737 static DEVICE_ATTR(interrupts_enabled, S_IRUGO,
1738 ipmi_interrupts_enabled_show, NULL);
1740 IPMI_SI_ATTR(short_timeouts);
1741 IPMI_SI_ATTR(long_timeouts);
1742 IPMI_SI_ATTR(idles);
1743 IPMI_SI_ATTR(interrupts);
1744 IPMI_SI_ATTR(attentions);
1745 IPMI_SI_ATTR(flag_fetches);
1746 IPMI_SI_ATTR(hosed_count);
1747 IPMI_SI_ATTR(complete_transactions);
1748 IPMI_SI_ATTR(events);
1749 IPMI_SI_ATTR(watchdog_pretimeouts);
1750 IPMI_SI_ATTR(incoming_messages);
1752 static ssize_t ipmi_params_show(struct device *dev,
1753 struct device_attribute *attr,
1754 char *buf)
1756 struct smi_info *smi_info = dev_get_drvdata(dev);
1758 return snprintf(buf, 200,
1759 "%s,%s,0x%lx,rsp=%d,rsi=%d,rsh=%d,irq=%d,ipmb=%d\n",
1760 si_to_str[smi_info->io.si_type],
1761 addr_space_to_str[smi_info->io.addr_type],
1762 smi_info->io.addr_data,
1763 smi_info->io.regspacing,
1764 smi_info->io.regsize,
1765 smi_info->io.regshift,
1766 smi_info->io.irq,
1767 smi_info->io.slave_addr);
1769 static DEVICE_ATTR(params, S_IRUGO, ipmi_params_show, NULL);
1771 static struct attribute *ipmi_si_dev_attrs[] = {
1772 &dev_attr_type.attr,
1773 &dev_attr_interrupts_enabled.attr,
1774 &dev_attr_short_timeouts.attr,
1775 &dev_attr_long_timeouts.attr,
1776 &dev_attr_idles.attr,
1777 &dev_attr_interrupts.attr,
1778 &dev_attr_attentions.attr,
1779 &dev_attr_flag_fetches.attr,
1780 &dev_attr_hosed_count.attr,
1781 &dev_attr_complete_transactions.attr,
1782 &dev_attr_events.attr,
1783 &dev_attr_watchdog_pretimeouts.attr,
1784 &dev_attr_incoming_messages.attr,
1785 &dev_attr_params.attr,
1786 NULL
1789 static const struct attribute_group ipmi_si_dev_attr_group = {
1790 .attrs = ipmi_si_dev_attrs,
1794 * oem_data_avail_to_receive_msg_avail
1795 * @info - smi_info structure with msg_flags set
1797 * Converts flags from OEM_DATA_AVAIL to RECEIVE_MSG_AVAIL
1798 * Returns 1 indicating need to re-run handle_flags().
1800 static int oem_data_avail_to_receive_msg_avail(struct smi_info *smi_info)
1802 smi_info->msg_flags = ((smi_info->msg_flags & ~OEM_DATA_AVAIL) |
1803 RECEIVE_MSG_AVAIL);
1804 return 1;
1808 * setup_dell_poweredge_oem_data_handler
1809 * @info - smi_info.device_id must be populated
1811 * Systems that match, but have firmware version < 1.40 may assert
1812 * OEM0_DATA_AVAIL on their own, without being told via Set Flags that
1813 * it's safe to do so. Such systems will de-assert OEM1_DATA_AVAIL
1814 * upon receipt of IPMI_GET_MSG_CMD, so we should treat these flags
1815 * as RECEIVE_MSG_AVAIL instead.
1817 * As Dell has no plans to release IPMI 1.5 firmware that *ever*
1818 * assert the OEM[012] bits, and if it did, the driver would have to
1819 * change to handle that properly, we don't actually check for the
1820 * firmware version.
1821 * Device ID = 0x20 BMC on PowerEdge 8G servers
1822 * Device Revision = 0x80
1823 * Firmware Revision1 = 0x01 BMC version 1.40
1824 * Firmware Revision2 = 0x40 BCD encoded
1825 * IPMI Version = 0x51 IPMI 1.5
1826 * Manufacturer ID = A2 02 00 Dell IANA
1828 * Additionally, PowerEdge systems with IPMI < 1.5 may also assert
1829 * OEM0_DATA_AVAIL and needs to be treated as RECEIVE_MSG_AVAIL.
1832 #define DELL_POWEREDGE_8G_BMC_DEVICE_ID 0x20
1833 #define DELL_POWEREDGE_8G_BMC_DEVICE_REV 0x80
1834 #define DELL_POWEREDGE_8G_BMC_IPMI_VERSION 0x51
1835 #define DELL_IANA_MFR_ID 0x0002a2
1836 static void setup_dell_poweredge_oem_data_handler(struct smi_info *smi_info)
1838 struct ipmi_device_id *id = &smi_info->device_id;
1839 if (id->manufacturer_id == DELL_IANA_MFR_ID) {
1840 if (id->device_id == DELL_POWEREDGE_8G_BMC_DEVICE_ID &&
1841 id->device_revision == DELL_POWEREDGE_8G_BMC_DEVICE_REV &&
1842 id->ipmi_version == DELL_POWEREDGE_8G_BMC_IPMI_VERSION) {
1843 smi_info->oem_data_avail_handler =
1844 oem_data_avail_to_receive_msg_avail;
1845 } else if (ipmi_version_major(id) < 1 ||
1846 (ipmi_version_major(id) == 1 &&
1847 ipmi_version_minor(id) < 5)) {
1848 smi_info->oem_data_avail_handler =
1849 oem_data_avail_to_receive_msg_avail;
1854 #define CANNOT_RETURN_REQUESTED_LENGTH 0xCA
1855 static void return_hosed_msg_badsize(struct smi_info *smi_info)
1857 struct ipmi_smi_msg *msg = smi_info->curr_msg;
1859 /* Make it a response */
1860 msg->rsp[0] = msg->data[0] | 4;
1861 msg->rsp[1] = msg->data[1];
1862 msg->rsp[2] = CANNOT_RETURN_REQUESTED_LENGTH;
1863 msg->rsp_size = 3;
1864 smi_info->curr_msg = NULL;
1865 deliver_recv_msg(smi_info, msg);
1869 * dell_poweredge_bt_xaction_handler
1870 * @info - smi_info.device_id must be populated
1872 * Dell PowerEdge servers with the BT interface (x6xx and 1750) will
1873 * not respond to a Get SDR command if the length of the data
1874 * requested is exactly 0x3A, which leads to command timeouts and no
1875 * data returned. This intercepts such commands, and causes userspace
1876 * callers to try again with a different-sized buffer, which succeeds.
1879 #define STORAGE_NETFN 0x0A
1880 #define STORAGE_CMD_GET_SDR 0x23
1881 static int dell_poweredge_bt_xaction_handler(struct notifier_block *self,
1882 unsigned long unused,
1883 void *in)
1885 struct smi_info *smi_info = in;
1886 unsigned char *data = smi_info->curr_msg->data;
1887 unsigned int size = smi_info->curr_msg->data_size;
1888 if (size >= 8 &&
1889 (data[0]>>2) == STORAGE_NETFN &&
1890 data[1] == STORAGE_CMD_GET_SDR &&
1891 data[7] == 0x3A) {
1892 return_hosed_msg_badsize(smi_info);
1893 return NOTIFY_STOP;
1895 return NOTIFY_DONE;
1898 static struct notifier_block dell_poweredge_bt_xaction_notifier = {
1899 .notifier_call = dell_poweredge_bt_xaction_handler,
1903 * setup_dell_poweredge_bt_xaction_handler
1904 * @info - smi_info.device_id must be filled in already
1906 * Fills in smi_info.device_id.start_transaction_pre_hook
1907 * when we know what function to use there.
1909 static void
1910 setup_dell_poweredge_bt_xaction_handler(struct smi_info *smi_info)
1912 struct ipmi_device_id *id = &smi_info->device_id;
1913 if (id->manufacturer_id == DELL_IANA_MFR_ID &&
1914 smi_info->io.si_type == SI_BT)
1915 register_xaction_notifier(&dell_poweredge_bt_xaction_notifier);
1919 * setup_oem_data_handler
1920 * @info - smi_info.device_id must be filled in already
1922 * Fills in smi_info.device_id.oem_data_available_handler
1923 * when we know what function to use there.
1926 static void setup_oem_data_handler(struct smi_info *smi_info)
1928 setup_dell_poweredge_oem_data_handler(smi_info);
1931 static void setup_xaction_handlers(struct smi_info *smi_info)
1933 setup_dell_poweredge_bt_xaction_handler(smi_info);
1936 static void check_for_broken_irqs(struct smi_info *smi_info)
1938 check_clr_rcv_irq(smi_info);
1939 check_set_rcv_irq(smi_info);
1942 static inline void stop_timer_and_thread(struct smi_info *smi_info)
1944 if (smi_info->thread != NULL) {
1945 kthread_stop(smi_info->thread);
1946 smi_info->thread = NULL;
1949 smi_info->timer_can_start = false;
1950 if (smi_info->timer_running)
1951 del_timer_sync(&smi_info->si_timer);
1954 static struct smi_info *find_dup_si(struct smi_info *info)
1956 struct smi_info *e;
1958 list_for_each_entry(e, &smi_infos, link) {
1959 if (e->io.addr_type != info->io.addr_type)
1960 continue;
1961 if (e->io.addr_data == info->io.addr_data) {
1963 * This is a cheap hack, ACPI doesn't have a defined
1964 * slave address but SMBIOS does. Pick it up from
1965 * any source that has it available.
1967 if (info->io.slave_addr && !e->io.slave_addr)
1968 e->io.slave_addr = info->io.slave_addr;
1969 return e;
1973 return NULL;
1976 int ipmi_si_add_smi(struct si_sm_io *io)
1978 int rv = 0;
1979 struct smi_info *new_smi, *dup;
1981 if (!io->io_setup) {
1982 if (io->addr_type == IPMI_IO_ADDR_SPACE) {
1983 io->io_setup = ipmi_si_port_setup;
1984 } else if (io->addr_type == IPMI_MEM_ADDR_SPACE) {
1985 io->io_setup = ipmi_si_mem_setup;
1986 } else {
1987 return -EINVAL;
1991 new_smi = kzalloc(sizeof(*new_smi), GFP_KERNEL);
1992 if (!new_smi)
1993 return -ENOMEM;
1994 spin_lock_init(&new_smi->si_lock);
1996 new_smi->io = *io;
1998 mutex_lock(&smi_infos_lock);
1999 dup = find_dup_si(new_smi);
2000 if (dup) {
2001 if (new_smi->io.addr_source == SI_ACPI &&
2002 dup->io.addr_source == SI_SMBIOS) {
2003 /* We prefer ACPI over SMBIOS. */
2004 dev_info(dup->io.dev,
2005 "Removing SMBIOS-specified %s state machine in favor of ACPI\n",
2006 si_to_str[new_smi->io.si_type]);
2007 cleanup_one_si(dup);
2008 } else {
2009 dev_info(new_smi->io.dev,
2010 "%s-specified %s state machine: duplicate\n",
2011 ipmi_addr_src_to_str(new_smi->io.addr_source),
2012 si_to_str[new_smi->io.si_type]);
2013 rv = -EBUSY;
2014 kfree(new_smi);
2015 goto out_err;
2019 pr_info(PFX "Adding %s-specified %s state machine\n",
2020 ipmi_addr_src_to_str(new_smi->io.addr_source),
2021 si_to_str[new_smi->io.si_type]);
2023 /* So we know not to free it unless we have allocated one. */
2024 new_smi->intf = NULL;
2025 new_smi->si_sm = NULL;
2026 new_smi->handlers = NULL;
2028 list_add_tail(&new_smi->link, &smi_infos);
2030 if (initialized) {
2031 rv = try_smi_init(new_smi);
2032 if (rv) {
2033 cleanup_one_si(new_smi);
2034 mutex_unlock(&smi_infos_lock);
2035 return rv;
2038 out_err:
2039 mutex_unlock(&smi_infos_lock);
2040 return rv;
2044 * Try to start up an interface. Must be called with smi_infos_lock
2045 * held, primarily to keep smi_num consistent, we only one to do these
2046 * one at a time.
2048 static int try_smi_init(struct smi_info *new_smi)
2050 int rv = 0;
2051 int i;
2052 char *init_name = NULL;
2053 bool platform_device_registered = false;
2055 pr_info(PFX "Trying %s-specified %s state machine at %s address 0x%lx, slave address 0x%x, irq %d\n",
2056 ipmi_addr_src_to_str(new_smi->io.addr_source),
2057 si_to_str[new_smi->io.si_type],
2058 addr_space_to_str[new_smi->io.addr_type],
2059 new_smi->io.addr_data,
2060 new_smi->io.slave_addr, new_smi->io.irq);
2062 switch (new_smi->io.si_type) {
2063 case SI_KCS:
2064 new_smi->handlers = &kcs_smi_handlers;
2065 break;
2067 case SI_SMIC:
2068 new_smi->handlers = &smic_smi_handlers;
2069 break;
2071 case SI_BT:
2072 new_smi->handlers = &bt_smi_handlers;
2073 break;
2075 default:
2076 /* No support for anything else yet. */
2077 rv = -EIO;
2078 goto out_err;
2081 new_smi->intf_num = smi_num;
2083 /* Do this early so it's available for logs. */
2084 if (!new_smi->io.dev) {
2085 init_name = kasprintf(GFP_KERNEL, "ipmi_si.%d",
2086 new_smi->intf_num);
2089 * If we don't already have a device from something
2090 * else (like PCI), then register a new one.
2092 new_smi->pdev = platform_device_alloc("ipmi_si",
2093 new_smi->intf_num);
2094 if (!new_smi->pdev) {
2095 pr_err(PFX "Unable to allocate platform device\n");
2096 goto out_err;
2098 new_smi->io.dev = &new_smi->pdev->dev;
2099 new_smi->io.dev->driver = &ipmi_platform_driver.driver;
2100 /* Nulled by device_add() */
2101 new_smi->io.dev->init_name = init_name;
2104 /* Allocate the state machine's data and initialize it. */
2105 new_smi->si_sm = kmalloc(new_smi->handlers->size(), GFP_KERNEL);
2106 if (!new_smi->si_sm) {
2107 rv = -ENOMEM;
2108 goto out_err;
2110 new_smi->io.io_size = new_smi->handlers->init_data(new_smi->si_sm,
2111 &new_smi->io);
2113 /* Now that we know the I/O size, we can set up the I/O. */
2114 rv = new_smi->io.io_setup(&new_smi->io);
2115 if (rv) {
2116 dev_err(new_smi->io.dev, "Could not set up I/O space\n");
2117 goto out_err;
2120 /* Do low-level detection first. */
2121 if (new_smi->handlers->detect(new_smi->si_sm)) {
2122 if (new_smi->io.addr_source)
2123 dev_err(new_smi->io.dev,
2124 "Interface detection failed\n");
2125 rv = -ENODEV;
2126 goto out_err;
2130 * Attempt a get device id command. If it fails, we probably
2131 * don't have a BMC here.
2133 rv = try_get_dev_id(new_smi);
2134 if (rv) {
2135 if (new_smi->io.addr_source)
2136 dev_err(new_smi->io.dev,
2137 "There appears to be no BMC at this location\n");
2138 goto out_err;
2141 setup_oem_data_handler(new_smi);
2142 setup_xaction_handlers(new_smi);
2143 check_for_broken_irqs(new_smi);
2145 new_smi->waiting_msg = NULL;
2146 new_smi->curr_msg = NULL;
2147 atomic_set(&new_smi->req_events, 0);
2148 new_smi->run_to_completion = false;
2149 for (i = 0; i < SI_NUM_STATS; i++)
2150 atomic_set(&new_smi->stats[i], 0);
2152 new_smi->interrupt_disabled = true;
2153 atomic_set(&new_smi->need_watch, 0);
2155 rv = try_enable_event_buffer(new_smi);
2156 if (rv == 0)
2157 new_smi->has_event_buffer = true;
2160 * Start clearing the flags before we enable interrupts or the
2161 * timer to avoid racing with the timer.
2163 start_clear_flags(new_smi);
2166 * IRQ is defined to be set when non-zero. req_events will
2167 * cause a global flags check that will enable interrupts.
2169 if (new_smi->io.irq) {
2170 new_smi->interrupt_disabled = false;
2171 atomic_set(&new_smi->req_events, 1);
2174 if (new_smi->pdev) {
2175 rv = platform_device_add(new_smi->pdev);
2176 if (rv) {
2177 dev_err(new_smi->io.dev,
2178 "Unable to register system interface device: %d\n",
2179 rv);
2180 goto out_err;
2182 platform_device_registered = true;
2185 dev_set_drvdata(new_smi->io.dev, new_smi);
2186 rv = device_add_group(new_smi->io.dev, &ipmi_si_dev_attr_group);
2187 if (rv) {
2188 dev_err(new_smi->io.dev,
2189 "Unable to add device attributes: error %d\n",
2190 rv);
2191 goto out_err_stop_timer;
2193 new_smi->dev_group_added = true;
2195 rv = ipmi_register_smi(&handlers,
2196 new_smi,
2197 new_smi->io.dev,
2198 new_smi->io.slave_addr);
2199 if (rv) {
2200 dev_err(new_smi->io.dev,
2201 "Unable to register device: error %d\n",
2202 rv);
2203 goto out_err_remove_attrs;
2206 #ifdef CONFIG_IPMI_PROC_INTERFACE
2207 rv = ipmi_smi_add_proc_entry(new_smi->intf, "type",
2208 &smi_type_proc_ops,
2209 new_smi);
2210 if (rv) {
2211 dev_err(new_smi->io.dev,
2212 "Unable to create proc entry: %d\n", rv);
2213 goto out_err_stop_timer;
2216 rv = ipmi_smi_add_proc_entry(new_smi->intf, "si_stats",
2217 &smi_si_stats_proc_ops,
2218 new_smi);
2219 if (rv) {
2220 dev_err(new_smi->io.dev,
2221 "Unable to create proc entry: %d\n", rv);
2222 goto out_err_stop_timer;
2225 rv = ipmi_smi_add_proc_entry(new_smi->intf, "params",
2226 &smi_params_proc_ops,
2227 new_smi);
2228 if (rv) {
2229 dev_err(new_smi->io.dev,
2230 "Unable to create proc entry: %d\n", rv);
2231 goto out_err_stop_timer;
2233 #endif
2235 /* Don't increment till we know we have succeeded. */
2236 smi_num++;
2238 dev_info(new_smi->io.dev, "IPMI %s interface initialized\n",
2239 si_to_str[new_smi->io.si_type]);
2241 WARN_ON(new_smi->io.dev->init_name != NULL);
2242 kfree(init_name);
2244 return 0;
2246 out_err_remove_attrs:
2247 if (new_smi->dev_group_added) {
2248 device_remove_group(new_smi->io.dev, &ipmi_si_dev_attr_group);
2249 new_smi->dev_group_added = false;
2251 dev_set_drvdata(new_smi->io.dev, NULL);
2253 out_err_stop_timer:
2254 stop_timer_and_thread(new_smi);
2256 out_err:
2257 new_smi->interrupt_disabled = true;
2259 if (new_smi->intf) {
2260 ipmi_smi_t intf = new_smi->intf;
2261 new_smi->intf = NULL;
2262 ipmi_unregister_smi(intf);
2265 if (new_smi->io.irq_cleanup) {
2266 new_smi->io.irq_cleanup(&new_smi->io);
2267 new_smi->io.irq_cleanup = NULL;
2271 * Wait until we know that we are out of any interrupt
2272 * handlers might have been running before we freed the
2273 * interrupt.
2275 synchronize_sched();
2277 if (new_smi->si_sm) {
2278 if (new_smi->handlers)
2279 new_smi->handlers->cleanup(new_smi->si_sm);
2280 kfree(new_smi->si_sm);
2281 new_smi->si_sm = NULL;
2283 if (new_smi->io.addr_source_cleanup) {
2284 new_smi->io.addr_source_cleanup(&new_smi->io);
2285 new_smi->io.addr_source_cleanup = NULL;
2287 if (new_smi->io.io_cleanup) {
2288 new_smi->io.io_cleanup(&new_smi->io);
2289 new_smi->io.io_cleanup = NULL;
2292 if (new_smi->pdev) {
2293 if (platform_device_registered)
2294 platform_device_unregister(new_smi->pdev);
2295 else
2296 platform_device_put(new_smi->pdev);
2297 new_smi->pdev = NULL;
2298 new_smi->io.dev = NULL;
2301 kfree(init_name);
2303 return rv;
2306 static int init_ipmi_si(void)
2308 struct smi_info *e;
2309 enum ipmi_addr_src type = SI_INVALID;
2311 if (initialized)
2312 return 0;
2314 pr_info("IPMI System Interface driver.\n");
2316 /* If the user gave us a device, they presumably want us to use it */
2317 if (!ipmi_si_hardcode_find_bmc())
2318 goto do_scan;
2320 ipmi_si_platform_init();
2322 ipmi_si_pci_init();
2324 ipmi_si_parisc_init();
2326 /* We prefer devices with interrupts, but in the case of a machine
2327 with multiple BMCs we assume that there will be several instances
2328 of a given type so if we succeed in registering a type then also
2329 try to register everything else of the same type */
2330 do_scan:
2331 mutex_lock(&smi_infos_lock);
2332 list_for_each_entry(e, &smi_infos, link) {
2333 /* Try to register a device if it has an IRQ and we either
2334 haven't successfully registered a device yet or this
2335 device has the same type as one we successfully registered */
2336 if (e->io.irq && (!type || e->io.addr_source == type)) {
2337 if (!try_smi_init(e)) {
2338 type = e->io.addr_source;
2343 /* type will only have been set if we successfully registered an si */
2344 if (type)
2345 goto skip_fallback_noirq;
2347 /* Fall back to the preferred device */
2349 list_for_each_entry(e, &smi_infos, link) {
2350 if (!e->io.irq && (!type || e->io.addr_source == type)) {
2351 if (!try_smi_init(e)) {
2352 type = e->io.addr_source;
2357 skip_fallback_noirq:
2358 initialized = 1;
2359 mutex_unlock(&smi_infos_lock);
2361 if (type)
2362 return 0;
2364 mutex_lock(&smi_infos_lock);
2365 if (unload_when_empty && list_empty(&smi_infos)) {
2366 mutex_unlock(&smi_infos_lock);
2367 cleanup_ipmi_si();
2368 pr_warn(PFX "Unable to find any System Interface(s)\n");
2369 return -ENODEV;
2370 } else {
2371 mutex_unlock(&smi_infos_lock);
2372 return 0;
2375 module_init(init_ipmi_si);
2377 static void cleanup_one_si(struct smi_info *to_clean)
2379 int rv = 0;
2381 if (!to_clean)
2382 return;
2384 if (to_clean->intf) {
2385 ipmi_smi_t intf = to_clean->intf;
2387 to_clean->intf = NULL;
2388 rv = ipmi_unregister_smi(intf);
2389 if (rv) {
2390 pr_err(PFX "Unable to unregister device: errno=%d\n",
2391 rv);
2395 if (to_clean->dev_group_added)
2396 device_remove_group(to_clean->io.dev, &ipmi_si_dev_attr_group);
2397 if (to_clean->io.dev)
2398 dev_set_drvdata(to_clean->io.dev, NULL);
2400 list_del(&to_clean->link);
2403 * Make sure that interrupts, the timer and the thread are
2404 * stopped and will not run again.
2406 if (to_clean->io.irq_cleanup)
2407 to_clean->io.irq_cleanup(&to_clean->io);
2408 stop_timer_and_thread(to_clean);
2411 * Timeouts are stopped, now make sure the interrupts are off
2412 * in the BMC. Note that timers and CPU interrupts are off,
2413 * so no need for locks.
2415 while (to_clean->curr_msg || (to_clean->si_state != SI_NORMAL)) {
2416 poll(to_clean);
2417 schedule_timeout_uninterruptible(1);
2419 if (to_clean->handlers)
2420 disable_si_irq(to_clean);
2421 while (to_clean->curr_msg || (to_clean->si_state != SI_NORMAL)) {
2422 poll(to_clean);
2423 schedule_timeout_uninterruptible(1);
2426 if (to_clean->handlers)
2427 to_clean->handlers->cleanup(to_clean->si_sm);
2429 kfree(to_clean->si_sm);
2431 if (to_clean->io.addr_source_cleanup)
2432 to_clean->io.addr_source_cleanup(&to_clean->io);
2433 if (to_clean->io.io_cleanup)
2434 to_clean->io.io_cleanup(&to_clean->io);
2436 if (to_clean->pdev)
2437 platform_device_unregister(to_clean->pdev);
2439 kfree(to_clean);
2442 int ipmi_si_remove_by_dev(struct device *dev)
2444 struct smi_info *e;
2445 int rv = -ENOENT;
2447 mutex_lock(&smi_infos_lock);
2448 list_for_each_entry(e, &smi_infos, link) {
2449 if (e->io.dev == dev) {
2450 cleanup_one_si(e);
2451 rv = 0;
2452 break;
2455 mutex_unlock(&smi_infos_lock);
2457 return rv;
2460 void ipmi_si_remove_by_data(int addr_space, enum si_type si_type,
2461 unsigned long addr)
2463 /* remove */
2464 struct smi_info *e, *tmp_e;
2466 mutex_lock(&smi_infos_lock);
2467 list_for_each_entry_safe(e, tmp_e, &smi_infos, link) {
2468 if (e->io.addr_type != addr_space)
2469 continue;
2470 if (e->io.si_type != si_type)
2471 continue;
2472 if (e->io.addr_data == addr)
2473 cleanup_one_si(e);
2475 mutex_unlock(&smi_infos_lock);
2478 static void cleanup_ipmi_si(void)
2480 struct smi_info *e, *tmp_e;
2482 if (!initialized)
2483 return;
2485 ipmi_si_pci_shutdown();
2487 ipmi_si_parisc_shutdown();
2489 ipmi_si_platform_shutdown();
2491 mutex_lock(&smi_infos_lock);
2492 list_for_each_entry_safe(e, tmp_e, &smi_infos, link)
2493 cleanup_one_si(e);
2494 mutex_unlock(&smi_infos_lock);
2496 module_exit(cleanup_ipmi_si);
2498 MODULE_ALIAS("platform:dmi-ipmi-si");
2499 MODULE_LICENSE("GPL");
2500 MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
2501 MODULE_DESCRIPTION("Interface to the IPMI driver for the KCS, SMIC, and BT"
2502 " system interfaces.");