xref: /openbmc/linux/drivers/char/ipmi/ipmi_si_intf.c (revision dd7450cab71174637329e5ef57d8474fc7ff2ea0)
1243ac210SCorey Minyard // SPDX-License-Identifier: GPL-2.0+
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * ipmi_si.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * The interface to the IPMI driver for the system interfaces (KCS, SMIC,
61da177e4SLinus Torvalds  * BT).
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * Author: MontaVista Software, Inc.
91da177e4SLinus Torvalds  *         Corey Minyard <minyard@mvista.com>
101da177e4SLinus Torvalds  *         source@mvista.com
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  * Copyright 2002 MontaVista Software Inc.
13dba9b4f6SCorey Minyard  * Copyright 2006 IBM Corp., Christian Krafft <krafft@de.ibm.com>
141da177e4SLinus Torvalds  */
151da177e4SLinus Torvalds 
161da177e4SLinus Torvalds /*
171da177e4SLinus Torvalds  * This file holds the "policy" for the interface to the SMI state
181da177e4SLinus Torvalds  * machine.  It does the configuration, handles timers and interrupts,
191da177e4SLinus Torvalds  * and drives the real SMI state machine.
201da177e4SLinus Torvalds  */
211da177e4SLinus Torvalds 
2225880f7dSJoe Perches #define pr_fmt(fmt) "ipmi_si: " fmt
2325880f7dSJoe Perches 
241da177e4SLinus Torvalds #include <linux/module.h>
251da177e4SLinus Torvalds #include <linux/moduleparam.h>
261da177e4SLinus Torvalds #include <linux/sched.h>
2707412736SAlexey Dobriyan #include <linux/seq_file.h>
281da177e4SLinus Torvalds #include <linux/timer.h>
291da177e4SLinus Torvalds #include <linux/errno.h>
301da177e4SLinus Torvalds #include <linux/spinlock.h>
311da177e4SLinus Torvalds #include <linux/slab.h>
321da177e4SLinus Torvalds #include <linux/delay.h>
331da177e4SLinus Torvalds #include <linux/list.h>
34ea94027bSCorey Minyard #include <linux/notifier.h>
35b0defcdbSCorey Minyard #include <linux/mutex.h>
36e9a705a0SMatt Domsch #include <linux/kthread.h>
371da177e4SLinus Torvalds #include <asm/irq.h>
381da177e4SLinus Torvalds #include <linux/interrupt.h>
391da177e4SLinus Torvalds #include <linux/rcupdate.h>
4016f4232cSZhao Yakui #include <linux/ipmi.h>
411da177e4SLinus Torvalds #include <linux/ipmi_smi.h>
421e89a499SCorey Minyard #include "ipmi_si.h"
43b361e27bSCorey Minyard #include <linux/string.h>
44b361e27bSCorey Minyard #include <linux/ctype.h>
45dba9b4f6SCorey Minyard 
461da177e4SLinus Torvalds /* Measure times between events in the driver. */
471da177e4SLinus Torvalds #undef DEBUG_TIMING
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds /* Call every 10 ms. */
501da177e4SLinus Torvalds #define SI_TIMEOUT_TIME_USEC	10000
511da177e4SLinus Torvalds #define SI_USEC_PER_JIFFY	(1000000/HZ)
521da177e4SLinus Torvalds #define SI_TIMEOUT_JIFFIES	(SI_TIMEOUT_TIME_USEC/SI_USEC_PER_JIFFY)
531da177e4SLinus Torvalds #define SI_SHORT_TIMEOUT_USEC  250 /* .25ms when the SM request a
541da177e4SLinus Torvalds 				      short timeout */
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds enum si_intf_state {
571da177e4SLinus Torvalds 	SI_NORMAL,
581da177e4SLinus Torvalds 	SI_GETTING_FLAGS,
591da177e4SLinus Torvalds 	SI_GETTING_EVENTS,
601da177e4SLinus Torvalds 	SI_CLEARING_FLAGS,
611da177e4SLinus Torvalds 	SI_GETTING_MESSAGES,
62d9b7e4f7SCorey Minyard 	SI_CHECKING_ENABLES,
63d9b7e4f7SCorey Minyard 	SI_SETTING_ENABLES
641da177e4SLinus Torvalds 	/* FIXME - add watchdog stuff. */
651da177e4SLinus Torvalds };
661da177e4SLinus Torvalds 
679dbf68f9SCorey Minyard /* Some BT-specific defines we need here. */
689dbf68f9SCorey Minyard #define IPMI_BT_INTMASK_REG		2
699dbf68f9SCorey Minyard #define IPMI_BT_INTMASK_CLEAR_IRQ_BIT	2
709dbf68f9SCorey Minyard #define IPMI_BT_INTMASK_ENABLE_IRQ_BIT	1
719dbf68f9SCorey Minyard 
7295e300c0SCorey Minyard static const char * const si_to_str[] = { "invalid", "kcs", "smic", "bt" };
731da177e4SLinus Torvalds 
74*dd7450caSKefeng Wang static bool initialized;
75bb398a4cSCorey Minyard 
7664959e2dSCorey Minyard /*
7764959e2dSCorey Minyard  * Indexes into stats[] in smi_info below.
7864959e2dSCorey Minyard  */
79ba8ff1c6SCorey Minyard enum si_stat_indexes {
80ba8ff1c6SCorey Minyard 	/*
81ba8ff1c6SCorey Minyard 	 * Number of times the driver requested a timer while an operation
82ba8ff1c6SCorey Minyard 	 * was in progress.
83ba8ff1c6SCorey Minyard 	 */
84ba8ff1c6SCorey Minyard 	SI_STAT_short_timeouts = 0,
8564959e2dSCorey Minyard 
86ba8ff1c6SCorey Minyard 	/*
87ba8ff1c6SCorey Minyard 	 * Number of times the driver requested a timer while nothing was in
88ba8ff1c6SCorey Minyard 	 * progress.
89ba8ff1c6SCorey Minyard 	 */
90ba8ff1c6SCorey Minyard 	SI_STAT_long_timeouts,
9164959e2dSCorey Minyard 
92ba8ff1c6SCorey Minyard 	/* Number of times the interface was idle while being polled. */
93ba8ff1c6SCorey Minyard 	SI_STAT_idles,
94ba8ff1c6SCorey Minyard 
95ba8ff1c6SCorey Minyard 	/* Number of interrupts the driver handled. */
96ba8ff1c6SCorey Minyard 	SI_STAT_interrupts,
97ba8ff1c6SCorey Minyard 
98ba8ff1c6SCorey Minyard 	/* Number of time the driver got an ATTN from the hardware. */
99ba8ff1c6SCorey Minyard 	SI_STAT_attentions,
100ba8ff1c6SCorey Minyard 
101ba8ff1c6SCorey Minyard 	/* Number of times the driver requested flags from the hardware. */
102ba8ff1c6SCorey Minyard 	SI_STAT_flag_fetches,
103ba8ff1c6SCorey Minyard 
104ba8ff1c6SCorey Minyard 	/* Number of times the hardware didn't follow the state machine. */
105ba8ff1c6SCorey Minyard 	SI_STAT_hosed_count,
106ba8ff1c6SCorey Minyard 
107ba8ff1c6SCorey Minyard 	/* Number of completed messages. */
108ba8ff1c6SCorey Minyard 	SI_STAT_complete_transactions,
109ba8ff1c6SCorey Minyard 
110ba8ff1c6SCorey Minyard 	/* Number of IPMI events received from the hardware. */
111ba8ff1c6SCorey Minyard 	SI_STAT_events,
112ba8ff1c6SCorey Minyard 
113ba8ff1c6SCorey Minyard 	/* Number of watchdog pretimeouts. */
114ba8ff1c6SCorey Minyard 	SI_STAT_watchdog_pretimeouts,
115ba8ff1c6SCorey Minyard 
116b3834be5SAdam Buchbinder 	/* Number of asynchronous messages received. */
117ba8ff1c6SCorey Minyard 	SI_STAT_incoming_messages,
118ba8ff1c6SCorey Minyard 
119ba8ff1c6SCorey Minyard 
120ba8ff1c6SCorey Minyard 	/* This *must* remain last, add new values above this. */
121ba8ff1c6SCorey Minyard 	SI_NUM_STATS
122ba8ff1c6SCorey Minyard };
12364959e2dSCorey Minyard 
124c305e3d3SCorey Minyard struct smi_info {
12557bccb4eSCorey Minyard 	int                    si_num;
126a567b623SCorey Minyard 	struct ipmi_smi        *intf;
1271da177e4SLinus Torvalds 	struct si_sm_data      *si_sm;
12881d02b7fSCorey Minyard 	const struct si_sm_handlers *handlers;
1291da177e4SLinus Torvalds 	spinlock_t             si_lock;
130b874b985SCorey Minyard 	struct ipmi_smi_msg    *waiting_msg;
1311da177e4SLinus Torvalds 	struct ipmi_smi_msg    *curr_msg;
1321da177e4SLinus Torvalds 	enum si_intf_state     si_state;
1331da177e4SLinus Torvalds 
134c305e3d3SCorey Minyard 	/*
135c305e3d3SCorey Minyard 	 * Used to handle the various types of I/O that can occur with
136c305e3d3SCorey Minyard 	 * IPMI
137c305e3d3SCorey Minyard 	 */
1381da177e4SLinus Torvalds 	struct si_sm_io io;
1391da177e4SLinus Torvalds 
140c305e3d3SCorey Minyard 	/*
141c305e3d3SCorey Minyard 	 * Per-OEM handler, called from handle_flags().  Returns 1
142c305e3d3SCorey Minyard 	 * when handle_flags() needs to be re-run or 0 indicating it
143c305e3d3SCorey Minyard 	 * set si_state itself.
1443ae0e0f9SCorey Minyard 	 */
1453ae0e0f9SCorey Minyard 	int (*oem_data_avail_handler)(struct smi_info *smi_info);
1463ae0e0f9SCorey Minyard 
147c305e3d3SCorey Minyard 	/*
148c305e3d3SCorey Minyard 	 * Flags from the last GET_MSG_FLAGS command, used when an ATTN
149c305e3d3SCorey Minyard 	 * is set to hold the flags until we are done handling everything
150c305e3d3SCorey Minyard 	 * from the flags.
151c305e3d3SCorey Minyard 	 */
1521da177e4SLinus Torvalds #define RECEIVE_MSG_AVAIL	0x01
1531da177e4SLinus Torvalds #define EVENT_MSG_BUFFER_FULL	0x02
1541da177e4SLinus Torvalds #define WDT_PRE_TIMEOUT_INT	0x08
1553ae0e0f9SCorey Minyard #define OEM0_DATA_AVAIL     0x20
1563ae0e0f9SCorey Minyard #define OEM1_DATA_AVAIL     0x40
1573ae0e0f9SCorey Minyard #define OEM2_DATA_AVAIL     0x80
1583ae0e0f9SCorey Minyard #define OEM_DATA_AVAIL      (OEM0_DATA_AVAIL | \
1593ae0e0f9SCorey Minyard 			     OEM1_DATA_AVAIL | \
1603ae0e0f9SCorey Minyard 			     OEM2_DATA_AVAIL)
1611da177e4SLinus Torvalds 	unsigned char       msg_flags;
1621da177e4SLinus Torvalds 
16340112ae7SCorey Minyard 	/* Does the BMC have an event buffer? */
1647aefac26SCorey Minyard 	bool		    has_event_buffer;
16540112ae7SCorey Minyard 
166c305e3d3SCorey Minyard 	/*
167c305e3d3SCorey Minyard 	 * If set to true, this will request events the next time the
168c305e3d3SCorey Minyard 	 * state machine is idle.
169c305e3d3SCorey Minyard 	 */
1701da177e4SLinus Torvalds 	atomic_t            req_events;
1711da177e4SLinus Torvalds 
172c305e3d3SCorey Minyard 	/*
173c305e3d3SCorey Minyard 	 * If true, run the state machine to completion on every send
174c305e3d3SCorey Minyard 	 * call.  Generally used after a panic to make sure stuff goes
175c305e3d3SCorey Minyard 	 * out.
176c305e3d3SCorey Minyard 	 */
1777aefac26SCorey Minyard 	bool                run_to_completion;
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds 	/* The timer for this si. */
1801da177e4SLinus Torvalds 	struct timer_list   si_timer;
1811da177e4SLinus Torvalds 
1824f7f5551SMasamitsu Yamazaki 	/* This flag is set, if the timer can be set */
1834f7f5551SMasamitsu Yamazaki 	bool		    timer_can_start;
1844f7f5551SMasamitsu Yamazaki 
18548e8ac29SBodo Stroesser 	/* This flag is set, if the timer is running (timer_pending() isn't enough) */
18648e8ac29SBodo Stroesser 	bool		    timer_running;
18748e8ac29SBodo Stroesser 
1881da177e4SLinus Torvalds 	/* The time (in jiffies) the last timeout occurred at. */
1891da177e4SLinus Torvalds 	unsigned long       last_timeout_jiffies;
1901da177e4SLinus Torvalds 
19189986496SCorey Minyard 	/* Are we waiting for the events, pretimeouts, received msgs? */
19289986496SCorey Minyard 	atomic_t            need_watch;
19389986496SCorey Minyard 
194c305e3d3SCorey Minyard 	/*
195c305e3d3SCorey Minyard 	 * The driver will disable interrupts when it gets into a
196c305e3d3SCorey Minyard 	 * situation where it cannot handle messages due to lack of
197c305e3d3SCorey Minyard 	 * memory.  Once that situation clears up, it will re-enable
198c305e3d3SCorey Minyard 	 * interrupts.
199c305e3d3SCorey Minyard 	 */
2007aefac26SCorey Minyard 	bool interrupt_disabled;
2011da177e4SLinus Torvalds 
202d9b7e4f7SCorey Minyard 	/*
203d9b7e4f7SCorey Minyard 	 * Does the BMC support events?
204d9b7e4f7SCorey Minyard 	 */
205d9b7e4f7SCorey Minyard 	bool supports_event_msg_buff;
206d9b7e4f7SCorey Minyard 
207a8df150cSCorey Minyard 	/*
208d0882897SCorey Minyard 	 * Can we disable interrupts the global enables receive irq
209d0882897SCorey Minyard 	 * bit?  There are currently two forms of brokenness, some
210d0882897SCorey Minyard 	 * systems cannot disable the bit (which is technically within
211d0882897SCorey Minyard 	 * the spec but a bad idea) and some systems have the bit
212d0882897SCorey Minyard 	 * forced to zero even though interrupts work (which is
213d0882897SCorey Minyard 	 * clearly outside the spec).  The next bool tells which form
214d0882897SCorey Minyard 	 * of brokenness is present.
2151e7d6a45SCorey Minyard 	 */
216d0882897SCorey Minyard 	bool cannot_disable_irq;
217d0882897SCorey Minyard 
218d0882897SCorey Minyard 	/*
219d0882897SCorey Minyard 	 * Some systems are broken and cannot set the irq enable
220d0882897SCorey Minyard 	 * bit, even if they support interrupts.
221d0882897SCorey Minyard 	 */
222d0882897SCorey Minyard 	bool irq_enable_broken;
2231e7d6a45SCorey Minyard 
2241e7d6a45SCorey Minyard 	/*
225a8df150cSCorey Minyard 	 * Did we get an attention that we did not handle?
226a8df150cSCorey Minyard 	 */
227a8df150cSCorey Minyard 	bool got_attn;
228a8df150cSCorey Minyard 
22950c812b2SCorey Minyard 	/* From the get device id response... */
2303ae0e0f9SCorey Minyard 	struct ipmi_device_id device_id;
2311da177e4SLinus Torvalds 
232cc095f0aSCorey Minyard 	/* Have we added the device group to the device? */
233cc095f0aSCorey Minyard 	bool dev_group_added;
234cc095f0aSCorey Minyard 
2351da177e4SLinus Torvalds 	/* Counters and things for the proc filesystem. */
23664959e2dSCorey Minyard 	atomic_t stats[SI_NUM_STATS];
237a9a2c44fSCorey Minyard 
238e9a705a0SMatt Domsch 	struct task_struct *thread;
239b0defcdbSCorey Minyard 
240b0defcdbSCorey Minyard 	struct list_head link;
2411da177e4SLinus Torvalds };
2421da177e4SLinus Torvalds 
24364959e2dSCorey Minyard #define smi_inc_stat(smi, stat) \
24464959e2dSCorey Minyard 	atomic_inc(&(smi)->stats[SI_STAT_ ## stat])
24564959e2dSCorey Minyard #define smi_get_stat(smi, stat) \
24664959e2dSCorey Minyard 	((unsigned int) atomic_read(&(smi)->stats[SI_STAT_ ## stat]))
24764959e2dSCorey Minyard 
2487a453308SCorey Minyard #define IPMI_MAX_INTFS 4
2497a453308SCorey Minyard static int force_kipmid[IPMI_MAX_INTFS];
250a51f4a81SCorey Minyard static int num_force_kipmid;
251a51f4a81SCorey Minyard 
2527a453308SCorey Minyard static unsigned int kipmid_max_busy_us[IPMI_MAX_INTFS];
253ae74e823SMartin Wilck static int num_max_busy_us;
254ae74e823SMartin Wilck 
2557aefac26SCorey Minyard static bool unload_when_empty = true;
256b361e27bSCorey Minyard 
257b0defcdbSCorey Minyard static int try_smi_init(struct smi_info *smi);
25871404a2fSCorey Minyard static void cleanup_one_si(struct smi_info *smi_info);
259d2478521SCorey Minyard static void cleanup_ipmi_si(void);
260b0defcdbSCorey Minyard 
261f93aae9fSJohn Stultz #ifdef DEBUG_TIMING
262f93aae9fSJohn Stultz void debug_timestamp(char *msg)
263f93aae9fSJohn Stultz {
26448862ea2SJohn Stultz 	struct timespec64 t;
265f93aae9fSJohn Stultz 
266dd3535b9SArnd Bergmann 	ktime_get_ts64(&t);
26748862ea2SJohn Stultz 	pr_debug("**%s: %lld.%9.9ld\n", msg, (long long) t.tv_sec, t.tv_nsec);
268f93aae9fSJohn Stultz }
269f93aae9fSJohn Stultz #else
270f93aae9fSJohn Stultz #define debug_timestamp(x)
271f93aae9fSJohn Stultz #endif
272f93aae9fSJohn Stultz 
273e041c683SAlan Stern static ATOMIC_NOTIFIER_HEAD(xaction_notifier_list);
274ea94027bSCorey Minyard static int register_xaction_notifier(struct notifier_block *nb)
275ea94027bSCorey Minyard {
276e041c683SAlan Stern 	return atomic_notifier_chain_register(&xaction_notifier_list, nb);
277ea94027bSCorey Minyard }
278ea94027bSCorey Minyard 
2791da177e4SLinus Torvalds static void deliver_recv_msg(struct smi_info *smi_info,
2801da177e4SLinus Torvalds 			     struct ipmi_smi_msg *msg)
2811da177e4SLinus Torvalds {
2827adf579cSCorey Minyard 	/* Deliver the message to the upper layer. */
283a747c5abSJiri Kosina 	ipmi_smi_msg_received(smi_info->intf, msg);
284a747c5abSJiri Kosina }
2851da177e4SLinus Torvalds 
2864d7cbac7SCorey Minyard static void return_hosed_msg(struct smi_info *smi_info, int cCode)
2871da177e4SLinus Torvalds {
2881da177e4SLinus Torvalds 	struct ipmi_smi_msg *msg = smi_info->curr_msg;
2891da177e4SLinus Torvalds 
2904d7cbac7SCorey Minyard 	if (cCode < 0 || cCode > IPMI_ERR_UNSPECIFIED)
2914d7cbac7SCorey Minyard 		cCode = IPMI_ERR_UNSPECIFIED;
2924d7cbac7SCorey Minyard 	/* else use it as is */
2934d7cbac7SCorey Minyard 
29425985edcSLucas De Marchi 	/* Make it a response */
2951da177e4SLinus Torvalds 	msg->rsp[0] = msg->data[0] | 4;
2961da177e4SLinus Torvalds 	msg->rsp[1] = msg->data[1];
2974d7cbac7SCorey Minyard 	msg->rsp[2] = cCode;
2981da177e4SLinus Torvalds 	msg->rsp_size = 3;
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds 	smi_info->curr_msg = NULL;
3011da177e4SLinus Torvalds 	deliver_recv_msg(smi_info, msg);
3021da177e4SLinus Torvalds }
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds static enum si_sm_result start_next_msg(struct smi_info *smi_info)
3051da177e4SLinus Torvalds {
3061da177e4SLinus Torvalds 	int              rv;
3071da177e4SLinus Torvalds 
308b874b985SCorey Minyard 	if (!smi_info->waiting_msg) {
3091da177e4SLinus Torvalds 		smi_info->curr_msg = NULL;
3101da177e4SLinus Torvalds 		rv = SI_SM_IDLE;
3111da177e4SLinus Torvalds 	} else {
3121da177e4SLinus Torvalds 		int err;
3131da177e4SLinus Torvalds 
314b874b985SCorey Minyard 		smi_info->curr_msg = smi_info->waiting_msg;
315b874b985SCorey Minyard 		smi_info->waiting_msg = NULL;
316f93aae9fSJohn Stultz 		debug_timestamp("Start2");
317e041c683SAlan Stern 		err = atomic_notifier_call_chain(&xaction_notifier_list,
318e041c683SAlan Stern 				0, smi_info);
319ea94027bSCorey Minyard 		if (err & NOTIFY_STOP_MASK) {
320ea94027bSCorey Minyard 			rv = SI_SM_CALL_WITHOUT_DELAY;
321ea94027bSCorey Minyard 			goto out;
322ea94027bSCorey Minyard 		}
3231da177e4SLinus Torvalds 		err = smi_info->handlers->start_transaction(
3241da177e4SLinus Torvalds 			smi_info->si_sm,
3251da177e4SLinus Torvalds 			smi_info->curr_msg->data,
3261da177e4SLinus Torvalds 			smi_info->curr_msg->data_size);
327c305e3d3SCorey Minyard 		if (err)
3284d7cbac7SCorey Minyard 			return_hosed_msg(smi_info, err);
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 		rv = SI_SM_CALL_WITHOUT_DELAY;
3311da177e4SLinus Torvalds 	}
332ea94027bSCorey Minyard out:
3331da177e4SLinus Torvalds 	return rv;
3341da177e4SLinus Torvalds }
3351da177e4SLinus Torvalds 
3360cfec916SCorey Minyard static void smi_mod_timer(struct smi_info *smi_info, unsigned long new_val)
3370cfec916SCorey Minyard {
3384f7f5551SMasamitsu Yamazaki 	if (!smi_info->timer_can_start)
3394f7f5551SMasamitsu Yamazaki 		return;
3400cfec916SCorey Minyard 	smi_info->last_timeout_jiffies = jiffies;
3410cfec916SCorey Minyard 	mod_timer(&smi_info->si_timer, new_val);
3420cfec916SCorey Minyard 	smi_info->timer_running = true;
3430cfec916SCorey Minyard }
3440cfec916SCorey Minyard 
3450cfec916SCorey Minyard /*
3460cfec916SCorey Minyard  * Start a new message and (re)start the timer and thread.
3470cfec916SCorey Minyard  */
3480cfec916SCorey Minyard static void start_new_msg(struct smi_info *smi_info, unsigned char *msg,
3490cfec916SCorey Minyard 			  unsigned int size)
3500cfec916SCorey Minyard {
3510cfec916SCorey Minyard 	smi_mod_timer(smi_info, jiffies + SI_TIMEOUT_JIFFIES);
3520cfec916SCorey Minyard 
3530cfec916SCorey Minyard 	if (smi_info->thread)
3540cfec916SCorey Minyard 		wake_up_process(smi_info->thread);
3550cfec916SCorey Minyard 
3560cfec916SCorey Minyard 	smi_info->handlers->start_transaction(smi_info->si_sm, msg, size);
3570cfec916SCorey Minyard }
3580cfec916SCorey Minyard 
3594f7f5551SMasamitsu Yamazaki static void start_check_enables(struct smi_info *smi_info)
360ee6cd5f8SCorey Minyard {
361ee6cd5f8SCorey Minyard 	unsigned char msg[2];
362ee6cd5f8SCorey Minyard 
363ee6cd5f8SCorey Minyard 	msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
364ee6cd5f8SCorey Minyard 	msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
365ee6cd5f8SCorey Minyard 
3660cfec916SCorey Minyard 	start_new_msg(smi_info, msg, 2);
367d9b7e4f7SCorey Minyard 	smi_info->si_state = SI_CHECKING_ENABLES;
368ee6cd5f8SCorey Minyard }
369ee6cd5f8SCorey Minyard 
3704f7f5551SMasamitsu Yamazaki static void start_clear_flags(struct smi_info *smi_info)
3711da177e4SLinus Torvalds {
3721da177e4SLinus Torvalds 	unsigned char msg[3];
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds 	/* Make sure the watchdog pre-timeout flag is not set at startup. */
3751da177e4SLinus Torvalds 	msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
3761da177e4SLinus Torvalds 	msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
3771da177e4SLinus Torvalds 	msg[2] = WDT_PRE_TIMEOUT_INT;
3781da177e4SLinus Torvalds 
3790cfec916SCorey Minyard 	start_new_msg(smi_info, msg, 3);
3801da177e4SLinus Torvalds 	smi_info->si_state = SI_CLEARING_FLAGS;
3811da177e4SLinus Torvalds }
3821da177e4SLinus Torvalds 
383968bf7ccSCorey Minyard static void start_getting_msg_queue(struct smi_info *smi_info)
384968bf7ccSCorey Minyard {
385968bf7ccSCorey Minyard 	smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
386968bf7ccSCorey Minyard 	smi_info->curr_msg->data[1] = IPMI_GET_MSG_CMD;
387968bf7ccSCorey Minyard 	smi_info->curr_msg->data_size = 2;
388968bf7ccSCorey Minyard 
3890cfec916SCorey Minyard 	start_new_msg(smi_info, smi_info->curr_msg->data,
390968bf7ccSCorey Minyard 		      smi_info->curr_msg->data_size);
391968bf7ccSCorey Minyard 	smi_info->si_state = SI_GETTING_MESSAGES;
392968bf7ccSCorey Minyard }
393968bf7ccSCorey Minyard 
394968bf7ccSCorey Minyard static void start_getting_events(struct smi_info *smi_info)
395968bf7ccSCorey Minyard {
396968bf7ccSCorey Minyard 	smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
397968bf7ccSCorey Minyard 	smi_info->curr_msg->data[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD;
398968bf7ccSCorey Minyard 	smi_info->curr_msg->data_size = 2;
399968bf7ccSCorey Minyard 
4000cfec916SCorey Minyard 	start_new_msg(smi_info, smi_info->curr_msg->data,
401968bf7ccSCorey Minyard 		      smi_info->curr_msg->data_size);
402968bf7ccSCorey Minyard 	smi_info->si_state = SI_GETTING_EVENTS;
403968bf7ccSCorey Minyard }
404968bf7ccSCorey Minyard 
405c305e3d3SCorey Minyard /*
406c305e3d3SCorey Minyard  * When we have a situtaion where we run out of memory and cannot
407c305e3d3SCorey Minyard  * allocate messages, we just leave them in the BMC and run the system
408c305e3d3SCorey Minyard  * polled until we can allocate some memory.  Once we have some
409c305e3d3SCorey Minyard  * memory, we will re-enable the interrupt.
4101e7d6a45SCorey Minyard  *
4111e7d6a45SCorey Minyard  * Note that we cannot just use disable_irq(), since the interrupt may
4121e7d6a45SCorey Minyard  * be shared.
413c305e3d3SCorey Minyard  */
4144f7f5551SMasamitsu Yamazaki static inline bool disable_si_irq(struct smi_info *smi_info)
4151da177e4SLinus Torvalds {
416910840f2SCorey Minyard 	if ((smi_info->io.irq) && (!smi_info->interrupt_disabled)) {
4177aefac26SCorey Minyard 		smi_info->interrupt_disabled = true;
4184f7f5551SMasamitsu Yamazaki 		start_check_enables(smi_info);
419968bf7ccSCorey Minyard 		return true;
4201da177e4SLinus Torvalds 	}
421968bf7ccSCorey Minyard 	return false;
4221da177e4SLinus Torvalds }
4231da177e4SLinus Torvalds 
424968bf7ccSCorey Minyard static inline bool enable_si_irq(struct smi_info *smi_info)
4251da177e4SLinus Torvalds {
426910840f2SCorey Minyard 	if ((smi_info->io.irq) && (smi_info->interrupt_disabled)) {
4277aefac26SCorey Minyard 		smi_info->interrupt_disabled = false;
4284f7f5551SMasamitsu Yamazaki 		start_check_enables(smi_info);
429968bf7ccSCorey Minyard 		return true;
4301da177e4SLinus Torvalds 	}
431968bf7ccSCorey Minyard 	return false;
432968bf7ccSCorey Minyard }
433968bf7ccSCorey Minyard 
434968bf7ccSCorey Minyard /*
435968bf7ccSCorey Minyard  * Allocate a message.  If unable to allocate, start the interrupt
436968bf7ccSCorey Minyard  * disable process and return NULL.  If able to allocate but
437968bf7ccSCorey Minyard  * interrupts are disabled, free the message and return NULL after
438968bf7ccSCorey Minyard  * starting the interrupt enable process.
439968bf7ccSCorey Minyard  */
440968bf7ccSCorey Minyard static struct ipmi_smi_msg *alloc_msg_handle_irq(struct smi_info *smi_info)
441968bf7ccSCorey Minyard {
442968bf7ccSCorey Minyard 	struct ipmi_smi_msg *msg;
443968bf7ccSCorey Minyard 
444968bf7ccSCorey Minyard 	msg = ipmi_alloc_smi_msg();
445968bf7ccSCorey Minyard 	if (!msg) {
4464f7f5551SMasamitsu Yamazaki 		if (!disable_si_irq(smi_info))
447968bf7ccSCorey Minyard 			smi_info->si_state = SI_NORMAL;
448968bf7ccSCorey Minyard 	} else if (enable_si_irq(smi_info)) {
449968bf7ccSCorey Minyard 		ipmi_free_smi_msg(msg);
450968bf7ccSCorey Minyard 		msg = NULL;
451968bf7ccSCorey Minyard 	}
452968bf7ccSCorey Minyard 	return msg;
4531da177e4SLinus Torvalds }
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds static void handle_flags(struct smi_info *smi_info)
4561da177e4SLinus Torvalds {
4573ae0e0f9SCorey Minyard retry:
4581da177e4SLinus Torvalds 	if (smi_info->msg_flags & WDT_PRE_TIMEOUT_INT) {
4591da177e4SLinus Torvalds 		/* Watchdog pre-timeout */
46064959e2dSCorey Minyard 		smi_inc_stat(smi_info, watchdog_pretimeouts);
4611da177e4SLinus Torvalds 
4624f7f5551SMasamitsu Yamazaki 		start_clear_flags(smi_info);
4631da177e4SLinus Torvalds 		smi_info->msg_flags &= ~WDT_PRE_TIMEOUT_INT;
4641da177e4SLinus Torvalds 		ipmi_smi_watchdog_pretimeout(smi_info->intf);
4651da177e4SLinus Torvalds 	} else if (smi_info->msg_flags & RECEIVE_MSG_AVAIL) {
4661da177e4SLinus Torvalds 		/* Messages available. */
467968bf7ccSCorey Minyard 		smi_info->curr_msg = alloc_msg_handle_irq(smi_info);
468968bf7ccSCorey Minyard 		if (!smi_info->curr_msg)
4691da177e4SLinus Torvalds 			return;
4701da177e4SLinus Torvalds 
471968bf7ccSCorey Minyard 		start_getting_msg_queue(smi_info);
4721da177e4SLinus Torvalds 	} else if (smi_info->msg_flags & EVENT_MSG_BUFFER_FULL) {
4731da177e4SLinus Torvalds 		/* Events available. */
474968bf7ccSCorey Minyard 		smi_info->curr_msg = alloc_msg_handle_irq(smi_info);
475968bf7ccSCorey Minyard 		if (!smi_info->curr_msg)
4761da177e4SLinus Torvalds 			return;
4771da177e4SLinus Torvalds 
478968bf7ccSCorey Minyard 		start_getting_events(smi_info);
4794064d5efSCorey Minyard 	} else if (smi_info->msg_flags & OEM_DATA_AVAIL &&
4804064d5efSCorey Minyard 		   smi_info->oem_data_avail_handler) {
4813ae0e0f9SCorey Minyard 		if (smi_info->oem_data_avail_handler(smi_info))
4823ae0e0f9SCorey Minyard 			goto retry;
483c305e3d3SCorey Minyard 	} else
4841da177e4SLinus Torvalds 		smi_info->si_state = SI_NORMAL;
4851da177e4SLinus Torvalds }
4861da177e4SLinus Torvalds 
487d9b7e4f7SCorey Minyard /*
488d9b7e4f7SCorey Minyard  * Global enables we care about.
489d9b7e4f7SCorey Minyard  */
490d9b7e4f7SCorey Minyard #define GLOBAL_ENABLES_MASK (IPMI_BMC_EVT_MSG_BUFF | IPMI_BMC_RCV_MSG_INTR | \
491d9b7e4f7SCorey Minyard 			     IPMI_BMC_EVT_MSG_INTR)
492d9b7e4f7SCorey Minyard 
49395c97b59SCorey Minyard static u8 current_global_enables(struct smi_info *smi_info, u8 base,
49495c97b59SCorey Minyard 				 bool *irq_on)
495d9b7e4f7SCorey Minyard {
496d9b7e4f7SCorey Minyard 	u8 enables = 0;
497d9b7e4f7SCorey Minyard 
498d9b7e4f7SCorey Minyard 	if (smi_info->supports_event_msg_buff)
499d9b7e4f7SCorey Minyard 		enables |= IPMI_BMC_EVT_MSG_BUFF;
500d9b7e4f7SCorey Minyard 
501910840f2SCorey Minyard 	if (((smi_info->io.irq && !smi_info->interrupt_disabled) ||
502d0882897SCorey Minyard 	     smi_info->cannot_disable_irq) &&
503d0882897SCorey Minyard 	    !smi_info->irq_enable_broken)
504d9b7e4f7SCorey Minyard 		enables |= IPMI_BMC_RCV_MSG_INTR;
505d9b7e4f7SCorey Minyard 
506d9b7e4f7SCorey Minyard 	if (smi_info->supports_event_msg_buff &&
507910840f2SCorey Minyard 	    smi_info->io.irq && !smi_info->interrupt_disabled &&
508d0882897SCorey Minyard 	    !smi_info->irq_enable_broken)
509d9b7e4f7SCorey Minyard 		enables |= IPMI_BMC_EVT_MSG_INTR;
510d9b7e4f7SCorey Minyard 
51195c97b59SCorey Minyard 	*irq_on = enables & (IPMI_BMC_EVT_MSG_INTR | IPMI_BMC_RCV_MSG_INTR);
51295c97b59SCorey Minyard 
513d9b7e4f7SCorey Minyard 	return enables;
514d9b7e4f7SCorey Minyard }
515d9b7e4f7SCorey Minyard 
51695c97b59SCorey Minyard static void check_bt_irq(struct smi_info *smi_info, bool irq_on)
51795c97b59SCorey Minyard {
51895c97b59SCorey Minyard 	u8 irqstate = smi_info->io.inputb(&smi_info->io, IPMI_BT_INTMASK_REG);
51995c97b59SCorey Minyard 
52095c97b59SCorey Minyard 	irqstate &= IPMI_BT_INTMASK_ENABLE_IRQ_BIT;
52195c97b59SCorey Minyard 
52295c97b59SCorey Minyard 	if ((bool)irqstate == irq_on)
52395c97b59SCorey Minyard 		return;
52495c97b59SCorey Minyard 
52595c97b59SCorey Minyard 	if (irq_on)
52695c97b59SCorey Minyard 		smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG,
52795c97b59SCorey Minyard 				     IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
52895c97b59SCorey Minyard 	else
52995c97b59SCorey Minyard 		smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG, 0);
53095c97b59SCorey Minyard }
53195c97b59SCorey Minyard 
5321da177e4SLinus Torvalds static void handle_transaction_done(struct smi_info *smi_info)
5331da177e4SLinus Torvalds {
5341da177e4SLinus Torvalds 	struct ipmi_smi_msg *msg;
5351da177e4SLinus Torvalds 
536f93aae9fSJohn Stultz 	debug_timestamp("Done");
5371da177e4SLinus Torvalds 	switch (smi_info->si_state) {
5381da177e4SLinus Torvalds 	case SI_NORMAL:
5391da177e4SLinus Torvalds 		if (!smi_info->curr_msg)
5401da177e4SLinus Torvalds 			break;
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds 		smi_info->curr_msg->rsp_size
5431da177e4SLinus Torvalds 			= smi_info->handlers->get_result(
5441da177e4SLinus Torvalds 				smi_info->si_sm,
5451da177e4SLinus Torvalds 				smi_info->curr_msg->rsp,
5461da177e4SLinus Torvalds 				IPMI_MAX_MSG_LENGTH);
5471da177e4SLinus Torvalds 
548c305e3d3SCorey Minyard 		/*
549c305e3d3SCorey Minyard 		 * Do this here becase deliver_recv_msg() releases the
550c305e3d3SCorey Minyard 		 * lock, and a new message can be put in during the
551c305e3d3SCorey Minyard 		 * time the lock is released.
552c305e3d3SCorey Minyard 		 */
5531da177e4SLinus Torvalds 		msg = smi_info->curr_msg;
5541da177e4SLinus Torvalds 		smi_info->curr_msg = NULL;
5551da177e4SLinus Torvalds 		deliver_recv_msg(smi_info, msg);
5561da177e4SLinus Torvalds 		break;
5571da177e4SLinus Torvalds 
5581da177e4SLinus Torvalds 	case SI_GETTING_FLAGS:
5591da177e4SLinus Torvalds 	{
5601da177e4SLinus Torvalds 		unsigned char msg[4];
5611da177e4SLinus Torvalds 		unsigned int  len;
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds 		/* We got the flags from the SMI, now handle them. */
5641da177e4SLinus Torvalds 		len = smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
5651da177e4SLinus Torvalds 		if (msg[2] != 0) {
566c305e3d3SCorey Minyard 			/* Error fetching flags, just give up for now. */
5671da177e4SLinus Torvalds 			smi_info->si_state = SI_NORMAL;
5681da177e4SLinus Torvalds 		} else if (len < 4) {
569c305e3d3SCorey Minyard 			/*
570c305e3d3SCorey Minyard 			 * Hmm, no flags.  That's technically illegal, but
571c305e3d3SCorey Minyard 			 * don't use uninitialized data.
572c305e3d3SCorey Minyard 			 */
5731da177e4SLinus Torvalds 			smi_info->si_state = SI_NORMAL;
5741da177e4SLinus Torvalds 		} else {
5751da177e4SLinus Torvalds 			smi_info->msg_flags = msg[3];
5761da177e4SLinus Torvalds 			handle_flags(smi_info);
5771da177e4SLinus Torvalds 		}
5781da177e4SLinus Torvalds 		break;
5791da177e4SLinus Torvalds 	}
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds 	case SI_CLEARING_FLAGS:
5821da177e4SLinus Torvalds 	{
5831da177e4SLinus Torvalds 		unsigned char msg[3];
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds 		/* We cleared the flags. */
5861da177e4SLinus Torvalds 		smi_info->handlers->get_result(smi_info->si_sm, msg, 3);
5871da177e4SLinus Torvalds 		if (msg[2] != 0) {
5881da177e4SLinus Torvalds 			/* Error clearing flags */
589910840f2SCorey Minyard 			dev_warn(smi_info->io.dev,
590279fbd0cSMyron Stowe 				 "Error clearing flags: %2.2x\n", msg[2]);
5911da177e4SLinus Torvalds 		}
5921da177e4SLinus Torvalds 		smi_info->si_state = SI_NORMAL;
5931da177e4SLinus Torvalds 		break;
5941da177e4SLinus Torvalds 	}
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds 	case SI_GETTING_EVENTS:
5971da177e4SLinus Torvalds 	{
5981da177e4SLinus Torvalds 		smi_info->curr_msg->rsp_size
5991da177e4SLinus Torvalds 			= smi_info->handlers->get_result(
6001da177e4SLinus Torvalds 				smi_info->si_sm,
6011da177e4SLinus Torvalds 				smi_info->curr_msg->rsp,
6021da177e4SLinus Torvalds 				IPMI_MAX_MSG_LENGTH);
6031da177e4SLinus Torvalds 
604c305e3d3SCorey Minyard 		/*
605c305e3d3SCorey Minyard 		 * Do this here becase deliver_recv_msg() releases the
606c305e3d3SCorey Minyard 		 * lock, and a new message can be put in during the
607c305e3d3SCorey Minyard 		 * time the lock is released.
608c305e3d3SCorey Minyard 		 */
6091da177e4SLinus Torvalds 		msg = smi_info->curr_msg;
6101da177e4SLinus Torvalds 		smi_info->curr_msg = NULL;
6111da177e4SLinus Torvalds 		if (msg->rsp[2] != 0) {
6121da177e4SLinus Torvalds 			/* Error getting event, probably done. */
6131da177e4SLinus Torvalds 			msg->done(msg);
6141da177e4SLinus Torvalds 
6151da177e4SLinus Torvalds 			/* Take off the event flag. */
6161da177e4SLinus Torvalds 			smi_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
6171da177e4SLinus Torvalds 			handle_flags(smi_info);
6181da177e4SLinus Torvalds 		} else {
61964959e2dSCorey Minyard 			smi_inc_stat(smi_info, events);
6201da177e4SLinus Torvalds 
621c305e3d3SCorey Minyard 			/*
622c305e3d3SCorey Minyard 			 * Do this before we deliver the message
623c305e3d3SCorey Minyard 			 * because delivering the message releases the
624c305e3d3SCorey Minyard 			 * lock and something else can mess with the
625c305e3d3SCorey Minyard 			 * state.
626c305e3d3SCorey Minyard 			 */
6271da177e4SLinus Torvalds 			handle_flags(smi_info);
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds 			deliver_recv_msg(smi_info, msg);
6301da177e4SLinus Torvalds 		}
6311da177e4SLinus Torvalds 		break;
6321da177e4SLinus Torvalds 	}
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds 	case SI_GETTING_MESSAGES:
6351da177e4SLinus Torvalds 	{
6361da177e4SLinus Torvalds 		smi_info->curr_msg->rsp_size
6371da177e4SLinus Torvalds 			= smi_info->handlers->get_result(
6381da177e4SLinus Torvalds 				smi_info->si_sm,
6391da177e4SLinus Torvalds 				smi_info->curr_msg->rsp,
6401da177e4SLinus Torvalds 				IPMI_MAX_MSG_LENGTH);
6411da177e4SLinus Torvalds 
642c305e3d3SCorey Minyard 		/*
643c305e3d3SCorey Minyard 		 * Do this here becase deliver_recv_msg() releases the
644c305e3d3SCorey Minyard 		 * lock, and a new message can be put in during the
645c305e3d3SCorey Minyard 		 * time the lock is released.
646c305e3d3SCorey Minyard 		 */
6471da177e4SLinus Torvalds 		msg = smi_info->curr_msg;
6481da177e4SLinus Torvalds 		smi_info->curr_msg = NULL;
6491da177e4SLinus Torvalds 		if (msg->rsp[2] != 0) {
6501da177e4SLinus Torvalds 			/* Error getting event, probably done. */
6511da177e4SLinus Torvalds 			msg->done(msg);
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds 			/* Take off the msg flag. */
6541da177e4SLinus Torvalds 			smi_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
6551da177e4SLinus Torvalds 			handle_flags(smi_info);
6561da177e4SLinus Torvalds 		} else {
65764959e2dSCorey Minyard 			smi_inc_stat(smi_info, incoming_messages);
6581da177e4SLinus Torvalds 
659c305e3d3SCorey Minyard 			/*
660c305e3d3SCorey Minyard 			 * Do this before we deliver the message
661c305e3d3SCorey Minyard 			 * because delivering the message releases the
662c305e3d3SCorey Minyard 			 * lock and something else can mess with the
663c305e3d3SCorey Minyard 			 * state.
664c305e3d3SCorey Minyard 			 */
6651da177e4SLinus Torvalds 			handle_flags(smi_info);
6661da177e4SLinus Torvalds 
6671da177e4SLinus Torvalds 			deliver_recv_msg(smi_info, msg);
6681da177e4SLinus Torvalds 		}
6691da177e4SLinus Torvalds 		break;
6701da177e4SLinus Torvalds 	}
6711da177e4SLinus Torvalds 
672d9b7e4f7SCorey Minyard 	case SI_CHECKING_ENABLES:
6731da177e4SLinus Torvalds 	{
6741da177e4SLinus Torvalds 		unsigned char msg[4];
675d9b7e4f7SCorey Minyard 		u8 enables;
67695c97b59SCorey Minyard 		bool irq_on;
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds 		/* We got the flags from the SMI, now handle them. */
6791da177e4SLinus Torvalds 		smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
6801da177e4SLinus Torvalds 		if (msg[2] != 0) {
681910840f2SCorey Minyard 			dev_warn(smi_info->io.dev,
6820849bfecSCorey Minyard 				 "Couldn't get irq info: %x.\n", msg[2]);
683910840f2SCorey Minyard 			dev_warn(smi_info->io.dev,
6840849bfecSCorey Minyard 				 "Maybe ok, but ipmi might run very slowly.\n");
6851da177e4SLinus Torvalds 			smi_info->si_state = SI_NORMAL;
686d9b7e4f7SCorey Minyard 			break;
687d9b7e4f7SCorey Minyard 		}
68895c97b59SCorey Minyard 		enables = current_global_enables(smi_info, 0, &irq_on);
689910840f2SCorey Minyard 		if (smi_info->io.si_type == SI_BT)
69095c97b59SCorey Minyard 			/* BT has its own interrupt enable bit. */
69195c97b59SCorey Minyard 			check_bt_irq(smi_info, irq_on);
692d9b7e4f7SCorey Minyard 		if (enables != (msg[3] & GLOBAL_ENABLES_MASK)) {
693d9b7e4f7SCorey Minyard 			/* Enables are not correct, fix them. */
6941da177e4SLinus Torvalds 			msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
6951da177e4SLinus Torvalds 			msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
696d9b7e4f7SCorey Minyard 			msg[2] = enables | (msg[3] & ~GLOBAL_ENABLES_MASK);
6971da177e4SLinus Torvalds 			smi_info->handlers->start_transaction(
6981da177e4SLinus Torvalds 				smi_info->si_sm, msg, 3);
699d9b7e4f7SCorey Minyard 			smi_info->si_state = SI_SETTING_ENABLES;
700d9b7e4f7SCorey Minyard 		} else if (smi_info->supports_event_msg_buff) {
701d9b7e4f7SCorey Minyard 			smi_info->curr_msg = ipmi_alloc_smi_msg();
702d9b7e4f7SCorey Minyard 			if (!smi_info->curr_msg) {
703ee6cd5f8SCorey Minyard 				smi_info->si_state = SI_NORMAL;
704d9b7e4f7SCorey Minyard 				break;
705d9b7e4f7SCorey Minyard 			}
7065ac7b2fcSCorey Minyard 			start_getting_events(smi_info);
707ee6cd5f8SCorey Minyard 		} else {
708d9b7e4f7SCorey Minyard 			smi_info->si_state = SI_NORMAL;
709ee6cd5f8SCorey Minyard 		}
710ee6cd5f8SCorey Minyard 		break;
711ee6cd5f8SCorey Minyard 	}
712ee6cd5f8SCorey Minyard 
713d9b7e4f7SCorey Minyard 	case SI_SETTING_ENABLES:
714ee6cd5f8SCorey Minyard 	{
715ee6cd5f8SCorey Minyard 		unsigned char msg[4];
716ee6cd5f8SCorey Minyard 
717ee6cd5f8SCorey Minyard 		smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
718d9b7e4f7SCorey Minyard 		if (msg[2] != 0)
719910840f2SCorey Minyard 			dev_warn(smi_info->io.dev,
720d9b7e4f7SCorey Minyard 				 "Could not set the global enables: 0x%x.\n",
721d9b7e4f7SCorey Minyard 				 msg[2]);
722d9b7e4f7SCorey Minyard 
723d9b7e4f7SCorey Minyard 		if (smi_info->supports_event_msg_buff) {
724d9b7e4f7SCorey Minyard 			smi_info->curr_msg = ipmi_alloc_smi_msg();
725d9b7e4f7SCorey Minyard 			if (!smi_info->curr_msg) {
726ee6cd5f8SCorey Minyard 				smi_info->si_state = SI_NORMAL;
727ee6cd5f8SCorey Minyard 				break;
728ee6cd5f8SCorey Minyard 			}
7295ac7b2fcSCorey Minyard 			start_getting_events(smi_info);
730d9b7e4f7SCorey Minyard 		} else {
731d9b7e4f7SCorey Minyard 			smi_info->si_state = SI_NORMAL;
732d9b7e4f7SCorey Minyard 		}
733d9b7e4f7SCorey Minyard 		break;
734d9b7e4f7SCorey Minyard 	}
7351da177e4SLinus Torvalds 	}
7361da177e4SLinus Torvalds }
7371da177e4SLinus Torvalds 
738c305e3d3SCorey Minyard /*
739c305e3d3SCorey Minyard  * Called on timeouts and events.  Timeouts should pass the elapsed
740c305e3d3SCorey Minyard  * time, interrupts should pass in zero.  Must be called with
741c305e3d3SCorey Minyard  * si_lock held and interrupts disabled.
742c305e3d3SCorey Minyard  */
7431da177e4SLinus Torvalds static enum si_sm_result smi_event_handler(struct smi_info *smi_info,
7441da177e4SLinus Torvalds 					   int time)
7451da177e4SLinus Torvalds {
7461da177e4SLinus Torvalds 	enum si_sm_result si_sm_result;
7471da177e4SLinus Torvalds 
7481da177e4SLinus Torvalds restart:
749c305e3d3SCorey Minyard 	/*
750c305e3d3SCorey Minyard 	 * There used to be a loop here that waited a little while
751c305e3d3SCorey Minyard 	 * (around 25us) before giving up.  That turned out to be
752c305e3d3SCorey Minyard 	 * pointless, the minimum delays I was seeing were in the 300us
753c305e3d3SCorey Minyard 	 * range, which is far too long to wait in an interrupt.  So
754c305e3d3SCorey Minyard 	 * we just run until the state machine tells us something
755c305e3d3SCorey Minyard 	 * happened or it needs a delay.
756c305e3d3SCorey Minyard 	 */
7571da177e4SLinus Torvalds 	si_sm_result = smi_info->handlers->event(smi_info->si_sm, time);
7581da177e4SLinus Torvalds 	time = 0;
7591da177e4SLinus Torvalds 	while (si_sm_result == SI_SM_CALL_WITHOUT_DELAY)
7601da177e4SLinus Torvalds 		si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
7611da177e4SLinus Torvalds 
762c305e3d3SCorey Minyard 	if (si_sm_result == SI_SM_TRANSACTION_COMPLETE) {
76364959e2dSCorey Minyard 		smi_inc_stat(smi_info, complete_transactions);
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds 		handle_transaction_done(smi_info);
766d9dffd2aSCorey Minyard 		goto restart;
767c305e3d3SCorey Minyard 	} else if (si_sm_result == SI_SM_HOSED) {
76864959e2dSCorey Minyard 		smi_inc_stat(smi_info, hosed_count);
7691da177e4SLinus Torvalds 
770c305e3d3SCorey Minyard 		/*
771c305e3d3SCorey Minyard 		 * Do the before return_hosed_msg, because that
772c305e3d3SCorey Minyard 		 * releases the lock.
773c305e3d3SCorey Minyard 		 */
7741da177e4SLinus Torvalds 		smi_info->si_state = SI_NORMAL;
7751da177e4SLinus Torvalds 		if (smi_info->curr_msg != NULL) {
776c305e3d3SCorey Minyard 			/*
777c305e3d3SCorey Minyard 			 * If we were handling a user message, format
778c305e3d3SCorey Minyard 			 * a response to send to the upper layer to
779c305e3d3SCorey Minyard 			 * tell it about the error.
780c305e3d3SCorey Minyard 			 */
7814d7cbac7SCorey Minyard 			return_hosed_msg(smi_info, IPMI_ERR_UNSPECIFIED);
7821da177e4SLinus Torvalds 		}
783d9dffd2aSCorey Minyard 		goto restart;
7841da177e4SLinus Torvalds 	}
7851da177e4SLinus Torvalds 
7864ea18425SCorey Minyard 	/*
7874ea18425SCorey Minyard 	 * We prefer handling attn over new messages.  But don't do
7884ea18425SCorey Minyard 	 * this if there is not yet an upper layer to handle anything.
7894ea18425SCorey Minyard 	 */
7900fbecb4fSCorey Minyard 	if (si_sm_result == SI_SM_ATTN || smi_info->got_attn) {
7911da177e4SLinus Torvalds 		unsigned char msg[2];
7921da177e4SLinus Torvalds 
793a8df150cSCorey Minyard 		if (smi_info->si_state != SI_NORMAL) {
794a8df150cSCorey Minyard 			/*
795a8df150cSCorey Minyard 			 * We got an ATTN, but we are doing something else.
796a8df150cSCorey Minyard 			 * Handle the ATTN later.
797a8df150cSCorey Minyard 			 */
798a8df150cSCorey Minyard 			smi_info->got_attn = true;
799a8df150cSCorey Minyard 		} else {
800a8df150cSCorey Minyard 			smi_info->got_attn = false;
80164959e2dSCorey Minyard 			smi_inc_stat(smi_info, attentions);
8021da177e4SLinus Torvalds 
803c305e3d3SCorey Minyard 			/*
804c305e3d3SCorey Minyard 			 * Got a attn, send down a get message flags to see
805c305e3d3SCorey Minyard 			 * what's causing it.  It would be better to handle
806c305e3d3SCorey Minyard 			 * this in the upper layer, but due to the way
807c305e3d3SCorey Minyard 			 * interrupts work with the SMI, that's not really
808c305e3d3SCorey Minyard 			 * possible.
809c305e3d3SCorey Minyard 			 */
8101da177e4SLinus Torvalds 			msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
8111da177e4SLinus Torvalds 			msg[1] = IPMI_GET_MSG_FLAGS_CMD;
8121da177e4SLinus Torvalds 
8130cfec916SCorey Minyard 			start_new_msg(smi_info, msg, 2);
8141da177e4SLinus Torvalds 			smi_info->si_state = SI_GETTING_FLAGS;
8151da177e4SLinus Torvalds 			goto restart;
8161da177e4SLinus Torvalds 		}
817a8df150cSCorey Minyard 	}
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds 	/* If we are currently idle, try to start the next message. */
8201da177e4SLinus Torvalds 	if (si_sm_result == SI_SM_IDLE) {
82164959e2dSCorey Minyard 		smi_inc_stat(smi_info, idles);
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds 		si_sm_result = start_next_msg(smi_info);
8241da177e4SLinus Torvalds 		if (si_sm_result != SI_SM_IDLE)
8251da177e4SLinus Torvalds 			goto restart;
8261da177e4SLinus Torvalds 	}
8271da177e4SLinus Torvalds 
8281da177e4SLinus Torvalds 	if ((si_sm_result == SI_SM_IDLE)
829c305e3d3SCorey Minyard 	    && (atomic_read(&smi_info->req_events))) {
830c305e3d3SCorey Minyard 		/*
831c305e3d3SCorey Minyard 		 * We are idle and the upper layer requested that I fetch
832c305e3d3SCorey Minyard 		 * events, so do so.
833c305e3d3SCorey Minyard 		 */
8341da177e4SLinus Torvalds 		atomic_set(&smi_info->req_events, 0);
83555162fb1SCorey Minyard 
836d9b7e4f7SCorey Minyard 		/*
837d9b7e4f7SCorey Minyard 		 * Take this opportunity to check the interrupt and
838d9b7e4f7SCorey Minyard 		 * message enable state for the BMC.  The BMC can be
839d9b7e4f7SCorey Minyard 		 * asynchronously reset, and may thus get interrupts
840d9b7e4f7SCorey Minyard 		 * disable and messages disabled.
841d9b7e4f7SCorey Minyard 		 */
842910840f2SCorey Minyard 		if (smi_info->supports_event_msg_buff || smi_info->io.irq) {
8434f7f5551SMasamitsu Yamazaki 			start_check_enables(smi_info);
844d9b7e4f7SCorey Minyard 		} else {
845d9b7e4f7SCorey Minyard 			smi_info->curr_msg = alloc_msg_handle_irq(smi_info);
84655162fb1SCorey Minyard 			if (!smi_info->curr_msg)
84755162fb1SCorey Minyard 				goto out;
84855162fb1SCorey Minyard 
849d9b7e4f7SCorey Minyard 			start_getting_events(smi_info);
850d9b7e4f7SCorey Minyard 		}
8511da177e4SLinus Torvalds 		goto restart;
8521da177e4SLinus Torvalds 	}
853314ef52fSCorey Minyard 
854314ef52fSCorey Minyard 	if (si_sm_result == SI_SM_IDLE && smi_info->timer_running) {
855314ef52fSCorey Minyard 		/* Ok it if fails, the timer will just go off. */
856314ef52fSCorey Minyard 		if (del_timer(&smi_info->si_timer))
857314ef52fSCorey Minyard 			smi_info->timer_running = false;
858314ef52fSCorey Minyard 	}
859314ef52fSCorey Minyard 
86055162fb1SCorey Minyard out:
8611da177e4SLinus Torvalds 	return si_sm_result;
8621da177e4SLinus Torvalds }
8631da177e4SLinus Torvalds 
86489986496SCorey Minyard static void check_start_timer_thread(struct smi_info *smi_info)
86589986496SCorey Minyard {
86689986496SCorey Minyard 	if (smi_info->si_state == SI_NORMAL && smi_info->curr_msg == NULL) {
86789986496SCorey Minyard 		smi_mod_timer(smi_info, jiffies + SI_TIMEOUT_JIFFIES);
86889986496SCorey Minyard 
86989986496SCorey Minyard 		if (smi_info->thread)
87089986496SCorey Minyard 			wake_up_process(smi_info->thread);
87189986496SCorey Minyard 
87289986496SCorey Minyard 		start_next_msg(smi_info);
87389986496SCorey Minyard 		smi_event_handler(smi_info, 0);
87489986496SCorey Minyard 	}
87589986496SCorey Minyard }
87689986496SCorey Minyard 
87782802f96SHidehiro Kawai static void flush_messages(void *send_info)
878e45361d7SHidehiro Kawai {
87982802f96SHidehiro Kawai 	struct smi_info *smi_info = send_info;
880e45361d7SHidehiro Kawai 	enum si_sm_result result;
881e45361d7SHidehiro Kawai 
882e45361d7SHidehiro Kawai 	/*
883e45361d7SHidehiro Kawai 	 * Currently, this function is called only in run-to-completion
884e45361d7SHidehiro Kawai 	 * mode.  This means we are single-threaded, no need for locks.
885e45361d7SHidehiro Kawai 	 */
886e45361d7SHidehiro Kawai 	result = smi_event_handler(smi_info, 0);
887e45361d7SHidehiro Kawai 	while (result != SI_SM_IDLE) {
888e45361d7SHidehiro Kawai 		udelay(SI_SHORT_TIMEOUT_USEC);
889e45361d7SHidehiro Kawai 		result = smi_event_handler(smi_info, SI_SHORT_TIMEOUT_USEC);
890e45361d7SHidehiro Kawai 	}
891e45361d7SHidehiro Kawai }
892e45361d7SHidehiro Kawai 
8931da177e4SLinus Torvalds static void sender(void                *send_info,
89499ab32f3SCorey Minyard 		   struct ipmi_smi_msg *msg)
8951da177e4SLinus Torvalds {
8961da177e4SLinus Torvalds 	struct smi_info   *smi_info = send_info;
8971da177e4SLinus Torvalds 	unsigned long     flags;
8981da177e4SLinus Torvalds 
899f93aae9fSJohn Stultz 	debug_timestamp("Enqueue");
9001da177e4SLinus Torvalds 
9011da177e4SLinus Torvalds 	if (smi_info->run_to_completion) {
902bda4c30aSCorey Minyard 		/*
90382802f96SHidehiro Kawai 		 * If we are running to completion, start it.  Upper
90482802f96SHidehiro Kawai 		 * layer will call flush_messages to clear it out.
905bda4c30aSCorey Minyard 		 */
9069f812704SHidehiro Kawai 		smi_info->waiting_msg = msg;
9071da177e4SLinus Torvalds 		return;
9081da177e4SLinus Torvalds 	}
9091da177e4SLinus Torvalds 
910f60adf42SCorey Minyard 	spin_lock_irqsave(&smi_info->si_lock, flags);
9111d86e29bSCorey Minyard 	/*
9121d86e29bSCorey Minyard 	 * The following two lines don't need to be under the lock for
9131d86e29bSCorey Minyard 	 * the lock's sake, but they do need SMP memory barriers to
9141d86e29bSCorey Minyard 	 * avoid getting things out of order.  We are already claiming
9151d86e29bSCorey Minyard 	 * the lock, anyway, so just do it under the lock to avoid the
9161d86e29bSCorey Minyard 	 * ordering problem.
9171d86e29bSCorey Minyard 	 */
9181d86e29bSCorey Minyard 	BUG_ON(smi_info->waiting_msg);
9191d86e29bSCorey Minyard 	smi_info->waiting_msg = msg;
92089986496SCorey Minyard 	check_start_timer_thread(smi_info);
921bda4c30aSCorey Minyard 	spin_unlock_irqrestore(&smi_info->si_lock, flags);
9221da177e4SLinus Torvalds }
9231da177e4SLinus Torvalds 
9247aefac26SCorey Minyard static void set_run_to_completion(void *send_info, bool i_run_to_completion)
9251da177e4SLinus Torvalds {
9261da177e4SLinus Torvalds 	struct smi_info   *smi_info = send_info;
9271da177e4SLinus Torvalds 
9281da177e4SLinus Torvalds 	smi_info->run_to_completion = i_run_to_completion;
929e45361d7SHidehiro Kawai 	if (i_run_to_completion)
930e45361d7SHidehiro Kawai 		flush_messages(smi_info);
9311da177e4SLinus Torvalds }
9321da177e4SLinus Torvalds 
933ae74e823SMartin Wilck /*
934ae74e823SMartin Wilck  * Use -1 in the nsec value of the busy waiting timespec to tell that
935ae74e823SMartin Wilck  * we are spinning in kipmid looking for something and not delaying
936ae74e823SMartin Wilck  * between checks
937ae74e823SMartin Wilck  */
93848862ea2SJohn Stultz static inline void ipmi_si_set_not_busy(struct timespec64 *ts)
939ae74e823SMartin Wilck {
940ae74e823SMartin Wilck 	ts->tv_nsec = -1;
941ae74e823SMartin Wilck }
94248862ea2SJohn Stultz static inline int ipmi_si_is_busy(struct timespec64 *ts)
943ae74e823SMartin Wilck {
944ae74e823SMartin Wilck 	return ts->tv_nsec != -1;
945ae74e823SMartin Wilck }
946ae74e823SMartin Wilck 
947cc4cbe90SArnd Bergmann static inline int ipmi_thread_busy_wait(enum si_sm_result smi_result,
948ae74e823SMartin Wilck 					const struct smi_info *smi_info,
94948862ea2SJohn Stultz 					struct timespec64 *busy_until)
950ae74e823SMartin Wilck {
951ae74e823SMartin Wilck 	unsigned int max_busy_us = 0;
952ae74e823SMartin Wilck 
95357bccb4eSCorey Minyard 	if (smi_info->si_num < num_max_busy_us)
95457bccb4eSCorey Minyard 		max_busy_us = kipmid_max_busy_us[smi_info->si_num];
955ae74e823SMartin Wilck 	if (max_busy_us == 0 || smi_result != SI_SM_CALL_WITH_DELAY)
956ae74e823SMartin Wilck 		ipmi_si_set_not_busy(busy_until);
957ae74e823SMartin Wilck 	else if (!ipmi_si_is_busy(busy_until)) {
958dd3535b9SArnd Bergmann 		ktime_get_ts64(busy_until);
95948862ea2SJohn Stultz 		timespec64_add_ns(busy_until, max_busy_us*NSEC_PER_USEC);
960ae74e823SMartin Wilck 	} else {
96148862ea2SJohn Stultz 		struct timespec64 now;
96248862ea2SJohn Stultz 
963dd3535b9SArnd Bergmann 		ktime_get_ts64(&now);
96448862ea2SJohn Stultz 		if (unlikely(timespec64_compare(&now, busy_until) > 0)) {
965ae74e823SMartin Wilck 			ipmi_si_set_not_busy(busy_until);
966ae74e823SMartin Wilck 			return 0;
967ae74e823SMartin Wilck 		}
968ae74e823SMartin Wilck 	}
969ae74e823SMartin Wilck 	return 1;
970ae74e823SMartin Wilck }
971ae74e823SMartin Wilck 
972ae74e823SMartin Wilck 
973ae74e823SMartin Wilck /*
974ae74e823SMartin Wilck  * A busy-waiting loop for speeding up IPMI operation.
975ae74e823SMartin Wilck  *
976ae74e823SMartin Wilck  * Lousy hardware makes this hard.  This is only enabled for systems
977ae74e823SMartin Wilck  * that are not BT and do not have interrupts.  It starts spinning
978ae74e823SMartin Wilck  * when an operation is complete or until max_busy tells it to stop
979ae74e823SMartin Wilck  * (if that is enabled).  See the paragraph on kimid_max_busy_us in
980ae74e823SMartin Wilck  * Documentation/IPMI.txt for details.
981ae74e823SMartin Wilck  */
982a9a2c44fSCorey Minyard static int ipmi_thread(void *data)
983a9a2c44fSCorey Minyard {
984a9a2c44fSCorey Minyard 	struct smi_info *smi_info = data;
985e9a705a0SMatt Domsch 	unsigned long flags;
986a9a2c44fSCorey Minyard 	enum si_sm_result smi_result;
98748862ea2SJohn Stultz 	struct timespec64 busy_until;
988a9a2c44fSCorey Minyard 
989ae74e823SMartin Wilck 	ipmi_si_set_not_busy(&busy_until);
9908698a745SDongsheng Yang 	set_user_nice(current, MAX_NICE);
991e9a705a0SMatt Domsch 	while (!kthread_should_stop()) {
992ae74e823SMartin Wilck 		int busy_wait;
993ae74e823SMartin Wilck 
994a9a2c44fSCorey Minyard 		spin_lock_irqsave(&(smi_info->si_lock), flags);
995a9a2c44fSCorey Minyard 		smi_result = smi_event_handler(smi_info, 0);
99648e8ac29SBodo Stroesser 
99748e8ac29SBodo Stroesser 		/*
99848e8ac29SBodo Stroesser 		 * If the driver is doing something, there is a possible
99948e8ac29SBodo Stroesser 		 * race with the timer.  If the timer handler see idle,
100048e8ac29SBodo Stroesser 		 * and the thread here sees something else, the timer
100148e8ac29SBodo Stroesser 		 * handler won't restart the timer even though it is
100248e8ac29SBodo Stroesser 		 * required.  So start it here if necessary.
100348e8ac29SBodo Stroesser 		 */
100448e8ac29SBodo Stroesser 		if (smi_result != SI_SM_IDLE && !smi_info->timer_running)
100548e8ac29SBodo Stroesser 			smi_mod_timer(smi_info, jiffies + SI_TIMEOUT_JIFFIES);
100648e8ac29SBodo Stroesser 
1007a9a2c44fSCorey Minyard 		spin_unlock_irqrestore(&(smi_info->si_lock), flags);
1008ae74e823SMartin Wilck 		busy_wait = ipmi_thread_busy_wait(smi_result, smi_info,
1009ae74e823SMartin Wilck 						  &busy_until);
1010c305e3d3SCorey Minyard 		if (smi_result == SI_SM_CALL_WITHOUT_DELAY)
1011c305e3d3SCorey Minyard 			; /* do nothing */
1012ae74e823SMartin Wilck 		else if (smi_result == SI_SM_CALL_WITH_DELAY && busy_wait)
101333979734Sakpm@osdl.org 			schedule();
101489986496SCorey Minyard 		else if (smi_result == SI_SM_IDLE) {
101589986496SCorey Minyard 			if (atomic_read(&smi_info->need_watch)) {
10163326f4f2SMatthew Garrett 				schedule_timeout_interruptible(100);
101789986496SCorey Minyard 			} else {
101889986496SCorey Minyard 				/* Wait to be woken up when we are needed. */
101989986496SCorey Minyard 				__set_current_state(TASK_INTERRUPTIBLE);
102089986496SCorey Minyard 				schedule();
102189986496SCorey Minyard 			}
102289986496SCorey Minyard 		} else
10238d1f66dcSMartin Wilck 			schedule_timeout_interruptible(1);
1024a9a2c44fSCorey Minyard 	}
1025a9a2c44fSCorey Minyard 	return 0;
1026a9a2c44fSCorey Minyard }
1027a9a2c44fSCorey Minyard 
1028a9a2c44fSCorey Minyard 
10291da177e4SLinus Torvalds static void poll(void *send_info)
10301da177e4SLinus Torvalds {
10311da177e4SLinus Torvalds 	struct smi_info *smi_info = send_info;
1032f60adf42SCorey Minyard 	unsigned long flags = 0;
10337aefac26SCorey Minyard 	bool run_to_completion = smi_info->run_to_completion;
10341da177e4SLinus Torvalds 
103515c62e10SCorey Minyard 	/*
103615c62e10SCorey Minyard 	 * Make sure there is some delay in the poll loop so we can
103715c62e10SCorey Minyard 	 * drive time forward and timeout things.
103815c62e10SCorey Minyard 	 */
103915c62e10SCorey Minyard 	udelay(10);
1040f60adf42SCorey Minyard 	if (!run_to_completion)
1041fcfa4724SCorey Minyard 		spin_lock_irqsave(&smi_info->si_lock, flags);
104215c62e10SCorey Minyard 	smi_event_handler(smi_info, 10);
1043f60adf42SCorey Minyard 	if (!run_to_completion)
1044fcfa4724SCorey Minyard 		spin_unlock_irqrestore(&smi_info->si_lock, flags);
10451da177e4SLinus Torvalds }
10461da177e4SLinus Torvalds 
10471da177e4SLinus Torvalds static void request_events(void *send_info)
10481da177e4SLinus Torvalds {
10491da177e4SLinus Torvalds 	struct smi_info *smi_info = send_info;
10501da177e4SLinus Torvalds 
1051b874b985SCorey Minyard 	if (!smi_info->has_event_buffer)
1052b361e27bSCorey Minyard 		return;
1053b361e27bSCorey Minyard 
10541da177e4SLinus Torvalds 	atomic_set(&smi_info->req_events, 1);
10551da177e4SLinus Torvalds }
10561da177e4SLinus Torvalds 
1057c65ea996SCorey Minyard static void set_need_watch(void *send_info, unsigned int watch_mask)
105889986496SCorey Minyard {
105989986496SCorey Minyard 	struct smi_info *smi_info = send_info;
106089986496SCorey Minyard 	unsigned long flags;
1061c65ea996SCorey Minyard 	int enable;
1062c65ea996SCorey Minyard 
1063e1891cffSCorey Minyard 	enable = !!watch_mask;
106489986496SCorey Minyard 
106589986496SCorey Minyard 	atomic_set(&smi_info->need_watch, enable);
106689986496SCorey Minyard 	spin_lock_irqsave(&smi_info->si_lock, flags);
106789986496SCorey Minyard 	check_start_timer_thread(smi_info);
106889986496SCorey Minyard 	spin_unlock_irqrestore(&smi_info->si_lock, flags);
106989986496SCorey Minyard }
107089986496SCorey Minyard 
1071e99e88a9SKees Cook static void smi_timeout(struct timer_list *t)
10721da177e4SLinus Torvalds {
1073e99e88a9SKees Cook 	struct smi_info   *smi_info = from_timer(smi_info, t, si_timer);
10741da177e4SLinus Torvalds 	enum si_sm_result smi_result;
10751da177e4SLinus Torvalds 	unsigned long     flags;
10761da177e4SLinus Torvalds 	unsigned long     jiffies_now;
1077c4edff1cSCorey Minyard 	long              time_diff;
10783326f4f2SMatthew Garrett 	long		  timeout;
10791da177e4SLinus Torvalds 
10801da177e4SLinus Torvalds 	spin_lock_irqsave(&(smi_info->si_lock), flags);
1081f93aae9fSJohn Stultz 	debug_timestamp("Timer");
1082f93aae9fSJohn Stultz 
10831da177e4SLinus Torvalds 	jiffies_now = jiffies;
1084c4edff1cSCorey Minyard 	time_diff = (((long)jiffies_now - (long)smi_info->last_timeout_jiffies)
10851da177e4SLinus Torvalds 		     * SI_USEC_PER_JIFFY);
10861da177e4SLinus Torvalds 	smi_result = smi_event_handler(smi_info, time_diff);
10871da177e4SLinus Torvalds 
1088910840f2SCorey Minyard 	if ((smi_info->io.irq) && (!smi_info->interrupt_disabled)) {
10891da177e4SLinus Torvalds 		/* Running with interrupts, only do long timeouts. */
10903326f4f2SMatthew Garrett 		timeout = jiffies + SI_TIMEOUT_JIFFIES;
109164959e2dSCorey Minyard 		smi_inc_stat(smi_info, long_timeouts);
10923326f4f2SMatthew Garrett 		goto do_mod_timer;
10931da177e4SLinus Torvalds 	}
10941da177e4SLinus Torvalds 
1095c305e3d3SCorey Minyard 	/*
1096c305e3d3SCorey Minyard 	 * If the state machine asks for a short delay, then shorten
1097c305e3d3SCorey Minyard 	 * the timer timeout.
1098c305e3d3SCorey Minyard 	 */
10991da177e4SLinus Torvalds 	if (smi_result == SI_SM_CALL_WITH_DELAY) {
110064959e2dSCorey Minyard 		smi_inc_stat(smi_info, short_timeouts);
11013326f4f2SMatthew Garrett 		timeout = jiffies + 1;
11021da177e4SLinus Torvalds 	} else {
110364959e2dSCorey Minyard 		smi_inc_stat(smi_info, long_timeouts);
11043326f4f2SMatthew Garrett 		timeout = jiffies + SI_TIMEOUT_JIFFIES;
11051da177e4SLinus Torvalds 	}
11061da177e4SLinus Torvalds 
11073326f4f2SMatthew Garrett do_mod_timer:
11083326f4f2SMatthew Garrett 	if (smi_result != SI_SM_IDLE)
110948e8ac29SBodo Stroesser 		smi_mod_timer(smi_info, timeout);
111048e8ac29SBodo Stroesser 	else
111148e8ac29SBodo Stroesser 		smi_info->timer_running = false;
111248e8ac29SBodo Stroesser 	spin_unlock_irqrestore(&(smi_info->si_lock), flags);
11131da177e4SLinus Torvalds }
11141da177e4SLinus Torvalds 
11154f3e8199SCorey Minyard irqreturn_t ipmi_si_irq_handler(int irq, void *data)
11161da177e4SLinus Torvalds {
11171da177e4SLinus Torvalds 	struct smi_info *smi_info = data;
11181da177e4SLinus Torvalds 	unsigned long   flags;
11191da177e4SLinus Torvalds 
11204f3e8199SCorey Minyard 	if (smi_info->io.si_type == SI_BT)
11214f3e8199SCorey Minyard 		/* We need to clear the IRQ flag for the BT interface. */
11224f3e8199SCorey Minyard 		smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG,
11234f3e8199SCorey Minyard 				     IPMI_BT_INTMASK_CLEAR_IRQ_BIT
11244f3e8199SCorey Minyard 				     | IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
11254f3e8199SCorey Minyard 
11261da177e4SLinus Torvalds 	spin_lock_irqsave(&(smi_info->si_lock), flags);
11271da177e4SLinus Torvalds 
112864959e2dSCorey Minyard 	smi_inc_stat(smi_info, interrupts);
11291da177e4SLinus Torvalds 
1130f93aae9fSJohn Stultz 	debug_timestamp("Interrupt");
1131f93aae9fSJohn Stultz 
11321da177e4SLinus Torvalds 	smi_event_handler(smi_info, 0);
11331da177e4SLinus Torvalds 	spin_unlock_irqrestore(&(smi_info->si_lock), flags);
11341da177e4SLinus Torvalds 	return IRQ_HANDLED;
11351da177e4SLinus Torvalds }
11361da177e4SLinus Torvalds 
1137453823baSCorey Minyard static int smi_start_processing(void            *send_info,
1138a567b623SCorey Minyard 				struct ipmi_smi *intf)
1139453823baSCorey Minyard {
1140453823baSCorey Minyard 	struct smi_info *new_smi = send_info;
1141a51f4a81SCorey Minyard 	int             enable = 0;
1142453823baSCorey Minyard 
1143453823baSCorey Minyard 	new_smi->intf = intf;
1144453823baSCorey Minyard 
1145453823baSCorey Minyard 	/* Set up the timer that drives the interface. */
1146e99e88a9SKees Cook 	timer_setup(&new_smi->si_timer, smi_timeout, 0);
11474f7f5551SMasamitsu Yamazaki 	new_smi->timer_can_start = true;
114848e8ac29SBodo Stroesser 	smi_mod_timer(new_smi, jiffies + SI_TIMEOUT_JIFFIES);
1149453823baSCorey Minyard 
115027f972d3SJan Stancek 	/* Try to claim any interrupts. */
11514f3e8199SCorey Minyard 	if (new_smi->io.irq_setup) {
11524f3e8199SCorey Minyard 		new_smi->io.irq_handler_data = new_smi;
11534f3e8199SCorey Minyard 		new_smi->io.irq_setup(&new_smi->io);
11544f3e8199SCorey Minyard 	}
115527f972d3SJan Stancek 
1156df3fe8deSCorey Minyard 	/*
1157a51f4a81SCorey Minyard 	 * Check if the user forcefully enabled the daemon.
1158a51f4a81SCorey Minyard 	 */
115957bccb4eSCorey Minyard 	if (new_smi->si_num < num_force_kipmid)
116057bccb4eSCorey Minyard 		enable = force_kipmid[new_smi->si_num];
1161a51f4a81SCorey Minyard 	/*
1162df3fe8deSCorey Minyard 	 * The BT interface is efficient enough to not need a thread,
1163df3fe8deSCorey Minyard 	 * and there is no need for a thread if we have interrupts.
1164df3fe8deSCorey Minyard 	 */
1165910840f2SCorey Minyard 	else if ((new_smi->io.si_type != SI_BT) && (!new_smi->io.irq))
1166a51f4a81SCorey Minyard 		enable = 1;
1167a51f4a81SCorey Minyard 
1168a51f4a81SCorey Minyard 	if (enable) {
1169453823baSCorey Minyard 		new_smi->thread = kthread_run(ipmi_thread, new_smi,
117057bccb4eSCorey Minyard 					      "kipmi%d", new_smi->si_num);
1171453823baSCorey Minyard 		if (IS_ERR(new_smi->thread)) {
1172910840f2SCorey Minyard 			dev_notice(new_smi->io.dev, "Could not start"
1173453823baSCorey Minyard 				   " kernel thread due to error %ld, only using"
1174453823baSCorey Minyard 				   " timers to drive the interface\n",
1175453823baSCorey Minyard 				   PTR_ERR(new_smi->thread));
1176453823baSCorey Minyard 			new_smi->thread = NULL;
1177453823baSCorey Minyard 		}
1178453823baSCorey Minyard 	}
1179453823baSCorey Minyard 
1180453823baSCorey Minyard 	return 0;
1181453823baSCorey Minyard }
11829dbf68f9SCorey Minyard 
118316f4232cSZhao Yakui static int get_smi_info(void *send_info, struct ipmi_smi_info *data)
118416f4232cSZhao Yakui {
118516f4232cSZhao Yakui 	struct smi_info *smi = send_info;
118616f4232cSZhao Yakui 
1187910840f2SCorey Minyard 	data->addr_src = smi->io.addr_source;
1188910840f2SCorey Minyard 	data->dev = smi->io.dev;
1189bb398a4cSCorey Minyard 	data->addr_info = smi->io.addr_info;
1190910840f2SCorey Minyard 	get_device(smi->io.dev);
119116f4232cSZhao Yakui 
119216f4232cSZhao Yakui 	return 0;
119316f4232cSZhao Yakui }
119416f4232cSZhao Yakui 
11957aefac26SCorey Minyard static void set_maintenance_mode(void *send_info, bool enable)
1196b9675136SCorey Minyard {
1197b9675136SCorey Minyard 	struct smi_info   *smi_info = send_info;
1198b9675136SCorey Minyard 
1199b9675136SCorey Minyard 	if (!enable)
1200b9675136SCorey Minyard 		atomic_set(&smi_info->req_events, 0);
1201b9675136SCorey Minyard }
1202b9675136SCorey Minyard 
12037960f18aSCorey Minyard static void shutdown_smi(void *send_info);
120481d02b7fSCorey Minyard static const struct ipmi_smi_handlers handlers = {
12051da177e4SLinus Torvalds 	.owner                  = THIS_MODULE,
1206453823baSCorey Minyard 	.start_processing       = smi_start_processing,
12077960f18aSCorey Minyard 	.shutdown               = shutdown_smi,
120816f4232cSZhao Yakui 	.get_smi_info		= get_smi_info,
12091da177e4SLinus Torvalds 	.sender			= sender,
12101da177e4SLinus Torvalds 	.request_events		= request_events,
121189986496SCorey Minyard 	.set_need_watch		= set_need_watch,
1212b9675136SCorey Minyard 	.set_maintenance_mode   = set_maintenance_mode,
12131da177e4SLinus Torvalds 	.set_run_to_completion  = set_run_to_completion,
121482802f96SHidehiro Kawai 	.flush_messages		= flush_messages,
12151da177e4SLinus Torvalds 	.poll			= poll,
12161da177e4SLinus Torvalds };
12171da177e4SLinus Torvalds 
1218b0defcdbSCorey Minyard static LIST_HEAD(smi_infos);
1219d6dfd131SCorey Minyard static DEFINE_MUTEX(smi_infos_lock);
1220b0defcdbSCorey Minyard static int smi_num; /* Used to sequence the SMIs */
12211da177e4SLinus Torvalds 
122299ee6735SLABBE Corentin static const char * const addr_space_to_str[] = { "i/o", "mem" };
1223b361e27bSCorey Minyard 
1224a51f4a81SCorey Minyard module_param_array(force_kipmid, int, &num_force_kipmid, 0);
1225a51f4a81SCorey Minyard MODULE_PARM_DESC(force_kipmid, "Force the kipmi daemon to be enabled (1) or"
1226a51f4a81SCorey Minyard 		 " disabled(0).  Normally the IPMI driver auto-detects"
1227a51f4a81SCorey Minyard 		 " this, but the value may be overridden by this parm.");
12287aefac26SCorey Minyard module_param(unload_when_empty, bool, 0);
1229b361e27bSCorey Minyard MODULE_PARM_DESC(unload_when_empty, "Unload the module if no interfaces are"
1230b361e27bSCorey Minyard 		 " specified or found, default is 1.  Setting to 0"
1231b361e27bSCorey Minyard 		 " is useful for hot add of devices using hotmod.");
1232ae74e823SMartin Wilck module_param_array(kipmid_max_busy_us, uint, &num_max_busy_us, 0644);
1233ae74e823SMartin Wilck MODULE_PARM_DESC(kipmid_max_busy_us,
1234ae74e823SMartin Wilck 		 "Max time (in microseconds) to busy-wait for IPMI data before"
1235ae74e823SMartin Wilck 		 " sleeping. 0 (default) means to wait forever. Set to 100-500"
1236ae74e823SMartin Wilck 		 " if kipmid is using up a lot of CPU time.");
12371da177e4SLinus Torvalds 
12384f3e8199SCorey Minyard void ipmi_irq_finish_setup(struct si_sm_io *io)
12391da177e4SLinus Torvalds {
12404f3e8199SCorey Minyard 	if (io->si_type == SI_BT)
12414f3e8199SCorey Minyard 		/* Enable the interrupt in the BT interface. */
12424f3e8199SCorey Minyard 		io->outputb(io, IPMI_BT_INTMASK_REG,
12434f3e8199SCorey Minyard 			    IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
12441da177e4SLinus Torvalds }
12451da177e4SLinus Torvalds 
12464f3e8199SCorey Minyard void ipmi_irq_start_cleanup(struct si_sm_io *io)
12474f3e8199SCorey Minyard {
12484f3e8199SCorey Minyard 	if (io->si_type == SI_BT)
12494f3e8199SCorey Minyard 		/* Disable the interrupt in the BT interface. */
12504f3e8199SCorey Minyard 		io->outputb(io, IPMI_BT_INTMASK_REG, 0);
12514f3e8199SCorey Minyard }
12524f3e8199SCorey Minyard 
12534f3e8199SCorey Minyard static void std_irq_cleanup(struct si_sm_io *io)
12544f3e8199SCorey Minyard {
12554f3e8199SCorey Minyard 	ipmi_irq_start_cleanup(io);
12564f3e8199SCorey Minyard 	free_irq(io->irq, io->irq_handler_data);
12574f3e8199SCorey Minyard }
12584f3e8199SCorey Minyard 
12594f3e8199SCorey Minyard int ipmi_std_irq_setup(struct si_sm_io *io)
12601da177e4SLinus Torvalds {
12611da177e4SLinus Torvalds 	int rv;
12621da177e4SLinus Torvalds 
12634f3e8199SCorey Minyard 	if (!io->irq)
12641da177e4SLinus Torvalds 		return 0;
12651da177e4SLinus Torvalds 
12664f3e8199SCorey Minyard 	rv = request_irq(io->irq,
12674f3e8199SCorey Minyard 			 ipmi_si_irq_handler,
1268aa5b2babSMichael Opdenacker 			 IRQF_SHARED,
12699dbf68f9SCorey Minyard 			 DEVICE_NAME,
12704f3e8199SCorey Minyard 			 io->irq_handler_data);
12711da177e4SLinus Torvalds 	if (rv) {
12724f3e8199SCorey Minyard 		dev_warn(io->dev, "%s unable to claim interrupt %d,"
12731da177e4SLinus Torvalds 			 " running polled\n",
12744f3e8199SCorey Minyard 			 DEVICE_NAME, io->irq);
12754f3e8199SCorey Minyard 		io->irq = 0;
12761da177e4SLinus Torvalds 	} else {
12774f3e8199SCorey Minyard 		io->irq_cleanup = std_irq_cleanup;
12784f3e8199SCorey Minyard 		ipmi_irq_finish_setup(io);
12794f3e8199SCorey Minyard 		dev_info(io->dev, "Using irq %d\n", io->irq);
12801da177e4SLinus Torvalds 	}
12811da177e4SLinus Torvalds 
12821da177e4SLinus Torvalds 	return rv;
12831da177e4SLinus Torvalds }
12841da177e4SLinus Torvalds 
128540112ae7SCorey Minyard static int wait_for_msg_done(struct smi_info *smi_info)
12861da177e4SLinus Torvalds {
12871da177e4SLinus Torvalds 	enum si_sm_result     smi_result;
12881da177e4SLinus Torvalds 
12891da177e4SLinus Torvalds 	smi_result = smi_info->handlers->event(smi_info->si_sm, 0);
1290c305e3d3SCorey Minyard 	for (;;) {
1291c3e7e791SCorey Minyard 		if (smi_result == SI_SM_CALL_WITH_DELAY ||
1292c3e7e791SCorey Minyard 		    smi_result == SI_SM_CALL_WITH_TICK_DELAY) {
1293da4cd8dfSNishanth Aravamudan 			schedule_timeout_uninterruptible(1);
12941da177e4SLinus Torvalds 			smi_result = smi_info->handlers->event(
1295e21404dcSXie XiuQi 				smi_info->si_sm, jiffies_to_usecs(1));
1296c305e3d3SCorey Minyard 		} else if (smi_result == SI_SM_CALL_WITHOUT_DELAY) {
12971da177e4SLinus Torvalds 			smi_result = smi_info->handlers->event(
12981da177e4SLinus Torvalds 				smi_info->si_sm, 0);
1299c305e3d3SCorey Minyard 		} else
13001da177e4SLinus Torvalds 			break;
13011da177e4SLinus Torvalds 	}
130240112ae7SCorey Minyard 	if (smi_result == SI_SM_HOSED)
1303c305e3d3SCorey Minyard 		/*
1304c305e3d3SCorey Minyard 		 * We couldn't get the state machine to run, so whatever's at
1305c305e3d3SCorey Minyard 		 * the port is probably not an IPMI SMI interface.
1306c305e3d3SCorey Minyard 		 */
130740112ae7SCorey Minyard 		return -ENODEV;
130840112ae7SCorey Minyard 
130940112ae7SCorey Minyard 	return 0;
13101da177e4SLinus Torvalds }
13111da177e4SLinus Torvalds 
131240112ae7SCorey Minyard static int try_get_dev_id(struct smi_info *smi_info)
131340112ae7SCorey Minyard {
131440112ae7SCorey Minyard 	unsigned char         msg[2];
131540112ae7SCorey Minyard 	unsigned char         *resp;
131640112ae7SCorey Minyard 	unsigned long         resp_len;
131740112ae7SCorey Minyard 	int                   rv = 0;
131840112ae7SCorey Minyard 
131940112ae7SCorey Minyard 	resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
132040112ae7SCorey Minyard 	if (!resp)
132140112ae7SCorey Minyard 		return -ENOMEM;
132240112ae7SCorey Minyard 
132340112ae7SCorey Minyard 	/*
132440112ae7SCorey Minyard 	 * Do a Get Device ID command, since it comes back with some
132540112ae7SCorey Minyard 	 * useful info.
132640112ae7SCorey Minyard 	 */
132740112ae7SCorey Minyard 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
132840112ae7SCorey Minyard 	msg[1] = IPMI_GET_DEVICE_ID_CMD;
132940112ae7SCorey Minyard 	smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
133040112ae7SCorey Minyard 
133140112ae7SCorey Minyard 	rv = wait_for_msg_done(smi_info);
133240112ae7SCorey Minyard 	if (rv)
133340112ae7SCorey Minyard 		goto out;
133440112ae7SCorey Minyard 
13351da177e4SLinus Torvalds 	resp_len = smi_info->handlers->get_result(smi_info->si_sm,
13361da177e4SLinus Torvalds 						  resp, IPMI_MAX_MSG_LENGTH);
13371da177e4SLinus Torvalds 
1338d8c98618SCorey Minyard 	/* Check and record info from the get device id, in case we need it. */
1339c468f911SJeremy Kerr 	rv = ipmi_demangle_device_id(resp[0] >> 2, resp[1],
1340c468f911SJeremy Kerr 			resp + 2, resp_len - 2, &smi_info->device_id);
13411da177e4SLinus Torvalds 
13421da177e4SLinus Torvalds out:
13431da177e4SLinus Torvalds 	kfree(resp);
13441da177e4SLinus Torvalds 	return rv;
13451da177e4SLinus Torvalds }
13461da177e4SLinus Torvalds 
1347d0882897SCorey Minyard static int get_global_enables(struct smi_info *smi_info, u8 *enables)
13481e7d6a45SCorey Minyard {
13491e7d6a45SCorey Minyard 	unsigned char         msg[3];
13501e7d6a45SCorey Minyard 	unsigned char         *resp;
13511e7d6a45SCorey Minyard 	unsigned long         resp_len;
13521e7d6a45SCorey Minyard 	int                   rv;
13531e7d6a45SCorey Minyard 
13541e7d6a45SCorey Minyard 	resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1355d0882897SCorey Minyard 	if (!resp)
1356d0882897SCorey Minyard 		return -ENOMEM;
13571e7d6a45SCorey Minyard 
13581e7d6a45SCorey Minyard 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
13591e7d6a45SCorey Minyard 	msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
13601e7d6a45SCorey Minyard 	smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
13611e7d6a45SCorey Minyard 
13621e7d6a45SCorey Minyard 	rv = wait_for_msg_done(smi_info);
13631e7d6a45SCorey Minyard 	if (rv) {
1364910840f2SCorey Minyard 		dev_warn(smi_info->io.dev,
1365d0882897SCorey Minyard 			 "Error getting response from get global enables command: %d\n",
1366d0882897SCorey Minyard 			 rv);
13671e7d6a45SCorey Minyard 		goto out;
13681e7d6a45SCorey Minyard 	}
13691e7d6a45SCorey Minyard 
13701e7d6a45SCorey Minyard 	resp_len = smi_info->handlers->get_result(smi_info->si_sm,
13711e7d6a45SCorey Minyard 						  resp, IPMI_MAX_MSG_LENGTH);
13721e7d6a45SCorey Minyard 
13731e7d6a45SCorey Minyard 	if (resp_len < 4 ||
13741e7d6a45SCorey Minyard 			resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
13751e7d6a45SCorey Minyard 			resp[1] != IPMI_GET_BMC_GLOBAL_ENABLES_CMD   ||
13761e7d6a45SCorey Minyard 			resp[2] != 0) {
1377910840f2SCorey Minyard 		dev_warn(smi_info->io.dev,
1378d0882897SCorey Minyard 			 "Invalid return from get global enables command: %ld %x %x %x\n",
1379d0882897SCorey Minyard 			 resp_len, resp[0], resp[1], resp[2]);
13801e7d6a45SCorey Minyard 		rv = -EINVAL;
13811e7d6a45SCorey Minyard 		goto out;
1382d0882897SCorey Minyard 	} else {
1383d0882897SCorey Minyard 		*enables = resp[3];
13841e7d6a45SCorey Minyard 	}
13851e7d6a45SCorey Minyard 
1386d0882897SCorey Minyard out:
1387d0882897SCorey Minyard 	kfree(resp);
1388d0882897SCorey Minyard 	return rv;
1389d0882897SCorey Minyard }
1390d0882897SCorey Minyard 
1391d0882897SCorey Minyard /*
1392d0882897SCorey Minyard  * Returns 1 if it gets an error from the command.
1393d0882897SCorey Minyard  */
1394d0882897SCorey Minyard static int set_global_enables(struct smi_info *smi_info, u8 enables)
1395d0882897SCorey Minyard {
1396d0882897SCorey Minyard 	unsigned char         msg[3];
1397d0882897SCorey Minyard 	unsigned char         *resp;
1398d0882897SCorey Minyard 	unsigned long         resp_len;
1399d0882897SCorey Minyard 	int                   rv;
1400d0882897SCorey Minyard 
1401d0882897SCorey Minyard 	resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1402d0882897SCorey Minyard 	if (!resp)
1403d0882897SCorey Minyard 		return -ENOMEM;
14041e7d6a45SCorey Minyard 
14051e7d6a45SCorey Minyard 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
14061e7d6a45SCorey Minyard 	msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
1407d0882897SCorey Minyard 	msg[2] = enables;
14081e7d6a45SCorey Minyard 	smi_info->handlers->start_transaction(smi_info->si_sm, msg, 3);
14091e7d6a45SCorey Minyard 
14101e7d6a45SCorey Minyard 	rv = wait_for_msg_done(smi_info);
14111e7d6a45SCorey Minyard 	if (rv) {
1412910840f2SCorey Minyard 		dev_warn(smi_info->io.dev,
1413d0882897SCorey Minyard 			 "Error getting response from set global enables command: %d\n",
1414d0882897SCorey Minyard 			 rv);
14151e7d6a45SCorey Minyard 		goto out;
14161e7d6a45SCorey Minyard 	}
14171e7d6a45SCorey Minyard 
14181e7d6a45SCorey Minyard 	resp_len = smi_info->handlers->get_result(smi_info->si_sm,
14191e7d6a45SCorey Minyard 						  resp, IPMI_MAX_MSG_LENGTH);
14201e7d6a45SCorey Minyard 
14211e7d6a45SCorey Minyard 	if (resp_len < 3 ||
14221e7d6a45SCorey Minyard 			resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
14231e7d6a45SCorey Minyard 			resp[1] != IPMI_SET_BMC_GLOBAL_ENABLES_CMD) {
1424910840f2SCorey Minyard 		dev_warn(smi_info->io.dev,
1425d0882897SCorey Minyard 			 "Invalid return from set global enables command: %ld %x %x\n",
1426d0882897SCorey Minyard 			 resp_len, resp[0], resp[1]);
14271e7d6a45SCorey Minyard 		rv = -EINVAL;
14281e7d6a45SCorey Minyard 		goto out;
14291e7d6a45SCorey Minyard 	}
14301e7d6a45SCorey Minyard 
1431d0882897SCorey Minyard 	if (resp[2] != 0)
1432d0882897SCorey Minyard 		rv = 1;
1433d0882897SCorey Minyard 
1434d0882897SCorey Minyard out:
1435d0882897SCorey Minyard 	kfree(resp);
1436d0882897SCorey Minyard 	return rv;
1437d0882897SCorey Minyard }
1438d0882897SCorey Minyard 
1439d0882897SCorey Minyard /*
1440d0882897SCorey Minyard  * Some BMCs do not support clearing the receive irq bit in the global
1441d0882897SCorey Minyard  * enables (even if they don't support interrupts on the BMC).  Check
1442d0882897SCorey Minyard  * for this and handle it properly.
1443d0882897SCorey Minyard  */
1444d0882897SCorey Minyard static void check_clr_rcv_irq(struct smi_info *smi_info)
1445d0882897SCorey Minyard {
1446d0882897SCorey Minyard 	u8 enables = 0;
1447d0882897SCorey Minyard 	int rv;
1448d0882897SCorey Minyard 
1449d0882897SCorey Minyard 	rv = get_global_enables(smi_info, &enables);
1450d0882897SCorey Minyard 	if (!rv) {
1451d0882897SCorey Minyard 		if ((enables & IPMI_BMC_RCV_MSG_INTR) == 0)
1452d0882897SCorey Minyard 			/* Already clear, should work ok. */
1453d0882897SCorey Minyard 			return;
1454d0882897SCorey Minyard 
1455d0882897SCorey Minyard 		enables &= ~IPMI_BMC_RCV_MSG_INTR;
1456d0882897SCorey Minyard 		rv = set_global_enables(smi_info, enables);
1457d0882897SCorey Minyard 	}
1458d0882897SCorey Minyard 
1459d0882897SCorey Minyard 	if (rv < 0) {
1460910840f2SCorey Minyard 		dev_err(smi_info->io.dev,
1461d0882897SCorey Minyard 			"Cannot check clearing the rcv irq: %d\n", rv);
1462d0882897SCorey Minyard 		return;
1463d0882897SCorey Minyard 	}
1464d0882897SCorey Minyard 
1465d0882897SCorey Minyard 	if (rv) {
14661e7d6a45SCorey Minyard 		/*
14671e7d6a45SCorey Minyard 		 * An error when setting the event buffer bit means
14681e7d6a45SCorey Minyard 		 * clearing the bit is not supported.
14691e7d6a45SCorey Minyard 		 */
1470910840f2SCorey Minyard 		dev_warn(smi_info->io.dev,
1471d0882897SCorey Minyard 			 "The BMC does not support clearing the recv irq bit, compensating, but the BMC needs to be fixed.\n");
1472d0882897SCorey Minyard 		smi_info->cannot_disable_irq = true;
14731e7d6a45SCorey Minyard 	}
1474d0882897SCorey Minyard }
1475d0882897SCorey Minyard 
1476d0882897SCorey Minyard /*
1477d0882897SCorey Minyard  * Some BMCs do not support setting the interrupt bits in the global
1478d0882897SCorey Minyard  * enables even if they support interrupts.  Clearly bad, but we can
1479d0882897SCorey Minyard  * compensate.
1480d0882897SCorey Minyard  */
1481d0882897SCorey Minyard static void check_set_rcv_irq(struct smi_info *smi_info)
1482d0882897SCorey Minyard {
1483d0882897SCorey Minyard 	u8 enables = 0;
1484d0882897SCorey Minyard 	int rv;
1485d0882897SCorey Minyard 
1486910840f2SCorey Minyard 	if (!smi_info->io.irq)
1487d0882897SCorey Minyard 		return;
1488d0882897SCorey Minyard 
1489d0882897SCorey Minyard 	rv = get_global_enables(smi_info, &enables);
1490d0882897SCorey Minyard 	if (!rv) {
1491d0882897SCorey Minyard 		enables |= IPMI_BMC_RCV_MSG_INTR;
1492d0882897SCorey Minyard 		rv = set_global_enables(smi_info, enables);
1493d0882897SCorey Minyard 	}
1494d0882897SCorey Minyard 
1495d0882897SCorey Minyard 	if (rv < 0) {
1496910840f2SCorey Minyard 		dev_err(smi_info->io.dev,
1497d0882897SCorey Minyard 			"Cannot check setting the rcv irq: %d\n", rv);
1498d0882897SCorey Minyard 		return;
1499d0882897SCorey Minyard 	}
1500d0882897SCorey Minyard 
1501d0882897SCorey Minyard 	if (rv) {
1502d0882897SCorey Minyard 		/*
1503d0882897SCorey Minyard 		 * An error when setting the event buffer bit means
1504d0882897SCorey Minyard 		 * setting the bit is not supported.
1505d0882897SCorey Minyard 		 */
1506910840f2SCorey Minyard 		dev_warn(smi_info->io.dev,
1507d0882897SCorey Minyard 			 "The BMC does not support setting the recv irq bit, compensating, but the BMC needs to be fixed.\n");
1508d0882897SCorey Minyard 		smi_info->cannot_disable_irq = true;
1509d0882897SCorey Minyard 		smi_info->irq_enable_broken = true;
1510d0882897SCorey Minyard 	}
15111e7d6a45SCorey Minyard }
15121e7d6a45SCorey Minyard 
151340112ae7SCorey Minyard static int try_enable_event_buffer(struct smi_info *smi_info)
151440112ae7SCorey Minyard {
151540112ae7SCorey Minyard 	unsigned char         msg[3];
151640112ae7SCorey Minyard 	unsigned char         *resp;
151740112ae7SCorey Minyard 	unsigned long         resp_len;
151840112ae7SCorey Minyard 	int                   rv = 0;
151940112ae7SCorey Minyard 
152040112ae7SCorey Minyard 	resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
152140112ae7SCorey Minyard 	if (!resp)
152240112ae7SCorey Minyard 		return -ENOMEM;
152340112ae7SCorey Minyard 
152440112ae7SCorey Minyard 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
152540112ae7SCorey Minyard 	msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
152640112ae7SCorey Minyard 	smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
152740112ae7SCorey Minyard 
152840112ae7SCorey Minyard 	rv = wait_for_msg_done(smi_info);
152940112ae7SCorey Minyard 	if (rv) {
153025880f7dSJoe Perches 		pr_warn("Error getting response from get global enables command, the event buffer is not enabled\n");
153140112ae7SCorey Minyard 		goto out;
153240112ae7SCorey Minyard 	}
153340112ae7SCorey Minyard 
153440112ae7SCorey Minyard 	resp_len = smi_info->handlers->get_result(smi_info->si_sm,
153540112ae7SCorey Minyard 						  resp, IPMI_MAX_MSG_LENGTH);
153640112ae7SCorey Minyard 
153740112ae7SCorey Minyard 	if (resp_len < 4 ||
153840112ae7SCorey Minyard 			resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
153940112ae7SCorey Minyard 			resp[1] != IPMI_GET_BMC_GLOBAL_ENABLES_CMD   ||
154040112ae7SCorey Minyard 			resp[2] != 0) {
154125880f7dSJoe Perches 		pr_warn("Invalid return from get global enables command, cannot enable the event buffer\n");
154240112ae7SCorey Minyard 		rv = -EINVAL;
154340112ae7SCorey Minyard 		goto out;
154440112ae7SCorey Minyard 	}
154540112ae7SCorey Minyard 
1546d9b7e4f7SCorey Minyard 	if (resp[3] & IPMI_BMC_EVT_MSG_BUFF) {
154740112ae7SCorey Minyard 		/* buffer is already enabled, nothing to do. */
1548d9b7e4f7SCorey Minyard 		smi_info->supports_event_msg_buff = true;
154940112ae7SCorey Minyard 		goto out;
1550d9b7e4f7SCorey Minyard 	}
155140112ae7SCorey Minyard 
155240112ae7SCorey Minyard 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
155340112ae7SCorey Minyard 	msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
155440112ae7SCorey Minyard 	msg[2] = resp[3] | IPMI_BMC_EVT_MSG_BUFF;
155540112ae7SCorey Minyard 	smi_info->handlers->start_transaction(smi_info->si_sm, msg, 3);
155640112ae7SCorey Minyard 
155740112ae7SCorey Minyard 	rv = wait_for_msg_done(smi_info);
155840112ae7SCorey Minyard 	if (rv) {
155925880f7dSJoe Perches 		pr_warn("Error getting response from set global, enables command, the event buffer is not enabled\n");
156040112ae7SCorey Minyard 		goto out;
156140112ae7SCorey Minyard 	}
156240112ae7SCorey Minyard 
156340112ae7SCorey Minyard 	resp_len = smi_info->handlers->get_result(smi_info->si_sm,
156440112ae7SCorey Minyard 						  resp, IPMI_MAX_MSG_LENGTH);
156540112ae7SCorey Minyard 
156640112ae7SCorey Minyard 	if (resp_len < 3 ||
156740112ae7SCorey Minyard 			resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
156840112ae7SCorey Minyard 			resp[1] != IPMI_SET_BMC_GLOBAL_ENABLES_CMD) {
156925880f7dSJoe Perches 		pr_warn("Invalid return from get global, enables command, not enable the event buffer\n");
157040112ae7SCorey Minyard 		rv = -EINVAL;
157140112ae7SCorey Minyard 		goto out;
157240112ae7SCorey Minyard 	}
157340112ae7SCorey Minyard 
157440112ae7SCorey Minyard 	if (resp[2] != 0)
157540112ae7SCorey Minyard 		/*
157640112ae7SCorey Minyard 		 * An error when setting the event buffer bit means
157740112ae7SCorey Minyard 		 * that the event buffer is not supported.
157840112ae7SCorey Minyard 		 */
157940112ae7SCorey Minyard 		rv = -ENOENT;
1580d9b7e4f7SCorey Minyard 	else
1581d9b7e4f7SCorey Minyard 		smi_info->supports_event_msg_buff = true;
1582d9b7e4f7SCorey Minyard 
158340112ae7SCorey Minyard out:
158440112ae7SCorey Minyard 	kfree(resp);
158540112ae7SCorey Minyard 	return rv;
158640112ae7SCorey Minyard }
158740112ae7SCorey Minyard 
15883dd377b5SCorey Minyard #define IPMI_SI_ATTR(name) \
15893dd377b5SCorey Minyard static ssize_t ipmi_##name##_show(struct device *dev,			\
15903dd377b5SCorey Minyard 				  struct device_attribute *attr,	\
15913dd377b5SCorey Minyard 				  char *buf)				\
15923dd377b5SCorey Minyard {									\
15933dd377b5SCorey Minyard 	struct smi_info *smi_info = dev_get_drvdata(dev);		\
15943dd377b5SCorey Minyard 									\
15953dd377b5SCorey Minyard 	return snprintf(buf, 10, "%u\n", smi_get_stat(smi_info, name));	\
15963dd377b5SCorey Minyard }									\
15973dd377b5SCorey Minyard static DEVICE_ATTR(name, S_IRUGO, ipmi_##name##_show, NULL)
15983dd377b5SCorey Minyard 
15993dd377b5SCorey Minyard static ssize_t ipmi_type_show(struct device *dev,
16003dd377b5SCorey Minyard 			      struct device_attribute *attr,
16013dd377b5SCorey Minyard 			      char *buf)
16023dd377b5SCorey Minyard {
16033dd377b5SCorey Minyard 	struct smi_info *smi_info = dev_get_drvdata(dev);
16043dd377b5SCorey Minyard 
16053dd377b5SCorey Minyard 	return snprintf(buf, 10, "%s\n", si_to_str[smi_info->io.si_type]);
16063dd377b5SCorey Minyard }
16073dd377b5SCorey Minyard static DEVICE_ATTR(type, S_IRUGO, ipmi_type_show, NULL);
16083dd377b5SCorey Minyard 
16093dd377b5SCorey Minyard static ssize_t ipmi_interrupts_enabled_show(struct device *dev,
16103dd377b5SCorey Minyard 					    struct device_attribute *attr,
16113dd377b5SCorey Minyard 					    char *buf)
16123dd377b5SCorey Minyard {
16133dd377b5SCorey Minyard 	struct smi_info *smi_info = dev_get_drvdata(dev);
16143dd377b5SCorey Minyard 	int enabled = smi_info->io.irq && !smi_info->interrupt_disabled;
16153dd377b5SCorey Minyard 
16163dd377b5SCorey Minyard 	return snprintf(buf, 10, "%d\n", enabled);
16173dd377b5SCorey Minyard }
16183dd377b5SCorey Minyard static DEVICE_ATTR(interrupts_enabled, S_IRUGO,
16193dd377b5SCorey Minyard 		   ipmi_interrupts_enabled_show, NULL);
16203dd377b5SCorey Minyard 
16213dd377b5SCorey Minyard IPMI_SI_ATTR(short_timeouts);
16223dd377b5SCorey Minyard IPMI_SI_ATTR(long_timeouts);
16233dd377b5SCorey Minyard IPMI_SI_ATTR(idles);
16243dd377b5SCorey Minyard IPMI_SI_ATTR(interrupts);
16253dd377b5SCorey Minyard IPMI_SI_ATTR(attentions);
16263dd377b5SCorey Minyard IPMI_SI_ATTR(flag_fetches);
16273dd377b5SCorey Minyard IPMI_SI_ATTR(hosed_count);
16283dd377b5SCorey Minyard IPMI_SI_ATTR(complete_transactions);
16293dd377b5SCorey Minyard IPMI_SI_ATTR(events);
16303dd377b5SCorey Minyard IPMI_SI_ATTR(watchdog_pretimeouts);
16313dd377b5SCorey Minyard IPMI_SI_ATTR(incoming_messages);
16323dd377b5SCorey Minyard 
16333dd377b5SCorey Minyard static ssize_t ipmi_params_show(struct device *dev,
16343dd377b5SCorey Minyard 				struct device_attribute *attr,
16353dd377b5SCorey Minyard 				char *buf)
16363dd377b5SCorey Minyard {
16373dd377b5SCorey Minyard 	struct smi_info *smi_info = dev_get_drvdata(dev);
16383dd377b5SCorey Minyard 
16393dd377b5SCorey Minyard 	return snprintf(buf, 200,
16403dd377b5SCorey Minyard 			"%s,%s,0x%lx,rsp=%d,rsi=%d,rsh=%d,irq=%d,ipmb=%d\n",
16413dd377b5SCorey Minyard 			si_to_str[smi_info->io.si_type],
1642f6296bdcSCorey Minyard 			addr_space_to_str[smi_info->io.addr_space],
16433dd377b5SCorey Minyard 			smi_info->io.addr_data,
16443dd377b5SCorey Minyard 			smi_info->io.regspacing,
16453dd377b5SCorey Minyard 			smi_info->io.regsize,
16463dd377b5SCorey Minyard 			smi_info->io.regshift,
16473dd377b5SCorey Minyard 			smi_info->io.irq,
16483dd377b5SCorey Minyard 			smi_info->io.slave_addr);
16493dd377b5SCorey Minyard }
16503dd377b5SCorey Minyard static DEVICE_ATTR(params, S_IRUGO, ipmi_params_show, NULL);
16513dd377b5SCorey Minyard 
16523dd377b5SCorey Minyard static struct attribute *ipmi_si_dev_attrs[] = {
16533dd377b5SCorey Minyard 	&dev_attr_type.attr,
16543dd377b5SCorey Minyard 	&dev_attr_interrupts_enabled.attr,
16553dd377b5SCorey Minyard 	&dev_attr_short_timeouts.attr,
16563dd377b5SCorey Minyard 	&dev_attr_long_timeouts.attr,
16573dd377b5SCorey Minyard 	&dev_attr_idles.attr,
16583dd377b5SCorey Minyard 	&dev_attr_interrupts.attr,
16593dd377b5SCorey Minyard 	&dev_attr_attentions.attr,
16603dd377b5SCorey Minyard 	&dev_attr_flag_fetches.attr,
16613dd377b5SCorey Minyard 	&dev_attr_hosed_count.attr,
16623dd377b5SCorey Minyard 	&dev_attr_complete_transactions.attr,
16633dd377b5SCorey Minyard 	&dev_attr_events.attr,
16643dd377b5SCorey Minyard 	&dev_attr_watchdog_pretimeouts.attr,
16653dd377b5SCorey Minyard 	&dev_attr_incoming_messages.attr,
16663dd377b5SCorey Minyard 	&dev_attr_params.attr,
16673dd377b5SCorey Minyard 	NULL
16683dd377b5SCorey Minyard };
16693dd377b5SCorey Minyard 
16703dd377b5SCorey Minyard static const struct attribute_group ipmi_si_dev_attr_group = {
16713dd377b5SCorey Minyard 	.attrs		= ipmi_si_dev_attrs,
16723dd377b5SCorey Minyard };
16733dd377b5SCorey Minyard 
16743ae0e0f9SCorey Minyard /*
16753ae0e0f9SCorey Minyard  * oem_data_avail_to_receive_msg_avail
16763ae0e0f9SCorey Minyard  * @info - smi_info structure with msg_flags set
16773ae0e0f9SCorey Minyard  *
16783ae0e0f9SCorey Minyard  * Converts flags from OEM_DATA_AVAIL to RECEIVE_MSG_AVAIL
16793ae0e0f9SCorey Minyard  * Returns 1 indicating need to re-run handle_flags().
16803ae0e0f9SCorey Minyard  */
16813ae0e0f9SCorey Minyard static int oem_data_avail_to_receive_msg_avail(struct smi_info *smi_info)
16823ae0e0f9SCorey Minyard {
1683e8b33617SCorey Minyard 	smi_info->msg_flags = ((smi_info->msg_flags & ~OEM_DATA_AVAIL) |
1684e8b33617SCorey Minyard 			       RECEIVE_MSG_AVAIL);
16853ae0e0f9SCorey Minyard 	return 1;
16863ae0e0f9SCorey Minyard }
16873ae0e0f9SCorey Minyard 
16883ae0e0f9SCorey Minyard /*
16893ae0e0f9SCorey Minyard  * setup_dell_poweredge_oem_data_handler
16903ae0e0f9SCorey Minyard  * @info - smi_info.device_id must be populated
16913ae0e0f9SCorey Minyard  *
16923ae0e0f9SCorey Minyard  * Systems that match, but have firmware version < 1.40 may assert
16933ae0e0f9SCorey Minyard  * OEM0_DATA_AVAIL on their own, without being told via Set Flags that
16943ae0e0f9SCorey Minyard  * it's safe to do so.  Such systems will de-assert OEM1_DATA_AVAIL
16953ae0e0f9SCorey Minyard  * upon receipt of IPMI_GET_MSG_CMD, so we should treat these flags
16963ae0e0f9SCorey Minyard  * as RECEIVE_MSG_AVAIL instead.
16973ae0e0f9SCorey Minyard  *
16983ae0e0f9SCorey Minyard  * As Dell has no plans to release IPMI 1.5 firmware that *ever*
16993ae0e0f9SCorey Minyard  * assert the OEM[012] bits, and if it did, the driver would have to
17003ae0e0f9SCorey Minyard  * change to handle that properly, we don't actually check for the
17013ae0e0f9SCorey Minyard  * firmware version.
17023ae0e0f9SCorey Minyard  * Device ID = 0x20                BMC on PowerEdge 8G servers
17033ae0e0f9SCorey Minyard  * Device Revision = 0x80
17043ae0e0f9SCorey Minyard  * Firmware Revision1 = 0x01       BMC version 1.40
17053ae0e0f9SCorey Minyard  * Firmware Revision2 = 0x40       BCD encoded
17063ae0e0f9SCorey Minyard  * IPMI Version = 0x51             IPMI 1.5
17073ae0e0f9SCorey Minyard  * Manufacturer ID = A2 02 00      Dell IANA
17083ae0e0f9SCorey Minyard  *
1709d5a2b89aSCorey Minyard  * Additionally, PowerEdge systems with IPMI < 1.5 may also assert
1710d5a2b89aSCorey Minyard  * OEM0_DATA_AVAIL and needs to be treated as RECEIVE_MSG_AVAIL.
1711d5a2b89aSCorey Minyard  *
17123ae0e0f9SCorey Minyard  */
17133ae0e0f9SCorey Minyard #define DELL_POWEREDGE_8G_BMC_DEVICE_ID  0x20
17143ae0e0f9SCorey Minyard #define DELL_POWEREDGE_8G_BMC_DEVICE_REV 0x80
17153ae0e0f9SCorey Minyard #define DELL_POWEREDGE_8G_BMC_IPMI_VERSION 0x51
171650c812b2SCorey Minyard #define DELL_IANA_MFR_ID 0x0002a2
17173ae0e0f9SCorey Minyard static void setup_dell_poweredge_oem_data_handler(struct smi_info *smi_info)
17183ae0e0f9SCorey Minyard {
17193ae0e0f9SCorey Minyard 	struct ipmi_device_id *id = &smi_info->device_id;
172050c812b2SCorey Minyard 	if (id->manufacturer_id == DELL_IANA_MFR_ID) {
1721d5a2b89aSCorey Minyard 		if (id->device_id       == DELL_POWEREDGE_8G_BMC_DEVICE_ID  &&
1722d5a2b89aSCorey Minyard 		    id->device_revision == DELL_POWEREDGE_8G_BMC_DEVICE_REV &&
1723d5a2b89aSCorey Minyard 		    id->ipmi_version   == DELL_POWEREDGE_8G_BMC_IPMI_VERSION) {
17243ae0e0f9SCorey Minyard 			smi_info->oem_data_avail_handler =
17253ae0e0f9SCorey Minyard 				oem_data_avail_to_receive_msg_avail;
1726c305e3d3SCorey Minyard 		} else if (ipmi_version_major(id) < 1 ||
1727d5a2b89aSCorey Minyard 			   (ipmi_version_major(id) == 1 &&
1728d5a2b89aSCorey Minyard 			    ipmi_version_minor(id) < 5)) {
1729d5a2b89aSCorey Minyard 			smi_info->oem_data_avail_handler =
1730d5a2b89aSCorey Minyard 				oem_data_avail_to_receive_msg_avail;
1731d5a2b89aSCorey Minyard 		}
1732d5a2b89aSCorey Minyard 	}
17333ae0e0f9SCorey Minyard }
17343ae0e0f9SCorey Minyard 
1735ea94027bSCorey Minyard #define CANNOT_RETURN_REQUESTED_LENGTH 0xCA
1736ea94027bSCorey Minyard static void return_hosed_msg_badsize(struct smi_info *smi_info)
1737ea94027bSCorey Minyard {
1738ea94027bSCorey Minyard 	struct ipmi_smi_msg *msg = smi_info->curr_msg;
1739ea94027bSCorey Minyard 
174025985edcSLucas De Marchi 	/* Make it a response */
1741ea94027bSCorey Minyard 	msg->rsp[0] = msg->data[0] | 4;
1742ea94027bSCorey Minyard 	msg->rsp[1] = msg->data[1];
1743ea94027bSCorey Minyard 	msg->rsp[2] = CANNOT_RETURN_REQUESTED_LENGTH;
1744ea94027bSCorey Minyard 	msg->rsp_size = 3;
1745ea94027bSCorey Minyard 	smi_info->curr_msg = NULL;
1746ea94027bSCorey Minyard 	deliver_recv_msg(smi_info, msg);
1747ea94027bSCorey Minyard }
1748ea94027bSCorey Minyard 
1749ea94027bSCorey Minyard /*
1750ea94027bSCorey Minyard  * dell_poweredge_bt_xaction_handler
1751ea94027bSCorey Minyard  * @info - smi_info.device_id must be populated
1752ea94027bSCorey Minyard  *
1753ea94027bSCorey Minyard  * Dell PowerEdge servers with the BT interface (x6xx and 1750) will
1754ea94027bSCorey Minyard  * not respond to a Get SDR command if the length of the data
1755ea94027bSCorey Minyard  * requested is exactly 0x3A, which leads to command timeouts and no
1756ea94027bSCorey Minyard  * data returned.  This intercepts such commands, and causes userspace
1757ea94027bSCorey Minyard  * callers to try again with a different-sized buffer, which succeeds.
1758ea94027bSCorey Minyard  */
1759ea94027bSCorey Minyard 
1760ea94027bSCorey Minyard #define STORAGE_NETFN 0x0A
1761ea94027bSCorey Minyard #define STORAGE_CMD_GET_SDR 0x23
1762ea94027bSCorey Minyard static int dell_poweredge_bt_xaction_handler(struct notifier_block *self,
1763ea94027bSCorey Minyard 					     unsigned long unused,
1764ea94027bSCorey Minyard 					     void *in)
1765ea94027bSCorey Minyard {
1766ea94027bSCorey Minyard 	struct smi_info *smi_info = in;
1767ea94027bSCorey Minyard 	unsigned char *data = smi_info->curr_msg->data;
1768ea94027bSCorey Minyard 	unsigned int size   = smi_info->curr_msg->data_size;
1769ea94027bSCorey Minyard 	if (size >= 8 &&
1770ea94027bSCorey Minyard 	    (data[0]>>2) == STORAGE_NETFN &&
1771ea94027bSCorey Minyard 	    data[1] == STORAGE_CMD_GET_SDR &&
1772ea94027bSCorey Minyard 	    data[7] == 0x3A) {
1773ea94027bSCorey Minyard 		return_hosed_msg_badsize(smi_info);
1774ea94027bSCorey Minyard 		return NOTIFY_STOP;
1775ea94027bSCorey Minyard 	}
1776ea94027bSCorey Minyard 	return NOTIFY_DONE;
1777ea94027bSCorey Minyard }
1778ea94027bSCorey Minyard 
1779ea94027bSCorey Minyard static struct notifier_block dell_poweredge_bt_xaction_notifier = {
1780ea94027bSCorey Minyard 	.notifier_call	= dell_poweredge_bt_xaction_handler,
1781ea94027bSCorey Minyard };
1782ea94027bSCorey Minyard 
1783ea94027bSCorey Minyard /*
1784ea94027bSCorey Minyard  * setup_dell_poweredge_bt_xaction_handler
1785ea94027bSCorey Minyard  * @info - smi_info.device_id must be filled in already
1786ea94027bSCorey Minyard  *
1787ea94027bSCorey Minyard  * Fills in smi_info.device_id.start_transaction_pre_hook
1788ea94027bSCorey Minyard  * when we know what function to use there.
1789ea94027bSCorey Minyard  */
1790ea94027bSCorey Minyard static void
1791ea94027bSCorey Minyard setup_dell_poweredge_bt_xaction_handler(struct smi_info *smi_info)
1792ea94027bSCorey Minyard {
1793ea94027bSCorey Minyard 	struct ipmi_device_id *id = &smi_info->device_id;
179450c812b2SCorey Minyard 	if (id->manufacturer_id == DELL_IANA_MFR_ID &&
1795910840f2SCorey Minyard 	    smi_info->io.si_type == SI_BT)
1796ea94027bSCorey Minyard 		register_xaction_notifier(&dell_poweredge_bt_xaction_notifier);
1797ea94027bSCorey Minyard }
1798ea94027bSCorey Minyard 
17993ae0e0f9SCorey Minyard /*
18003ae0e0f9SCorey Minyard  * setup_oem_data_handler
18013ae0e0f9SCorey Minyard  * @info - smi_info.device_id must be filled in already
18023ae0e0f9SCorey Minyard  *
18033ae0e0f9SCorey Minyard  * Fills in smi_info.device_id.oem_data_available_handler
18043ae0e0f9SCorey Minyard  * when we know what function to use there.
18053ae0e0f9SCorey Minyard  */
18063ae0e0f9SCorey Minyard 
18073ae0e0f9SCorey Minyard static void setup_oem_data_handler(struct smi_info *smi_info)
18083ae0e0f9SCorey Minyard {
18093ae0e0f9SCorey Minyard 	setup_dell_poweredge_oem_data_handler(smi_info);
18103ae0e0f9SCorey Minyard }
18113ae0e0f9SCorey Minyard 
1812ea94027bSCorey Minyard static void setup_xaction_handlers(struct smi_info *smi_info)
1813ea94027bSCorey Minyard {
1814ea94027bSCorey Minyard 	setup_dell_poweredge_bt_xaction_handler(smi_info);
1815ea94027bSCorey Minyard }
1816ea94027bSCorey Minyard 
1817d0882897SCorey Minyard static void check_for_broken_irqs(struct smi_info *smi_info)
1818d0882897SCorey Minyard {
1819d0882897SCorey Minyard 	check_clr_rcv_irq(smi_info);
1820d0882897SCorey Minyard 	check_set_rcv_irq(smi_info);
1821d0882897SCorey Minyard }
1822d0882897SCorey Minyard 
18234f7f5551SMasamitsu Yamazaki static inline void stop_timer_and_thread(struct smi_info *smi_info)
1824a9a2c44fSCorey Minyard {
1825bd1c06a4SMasamitsu Yamazaki 	if (smi_info->thread != NULL) {
1826e9a705a0SMatt Domsch 		kthread_stop(smi_info->thread);
1827bd1c06a4SMasamitsu Yamazaki 		smi_info->thread = NULL;
1828bd1c06a4SMasamitsu Yamazaki 	}
18294f7f5551SMasamitsu Yamazaki 
18304f7f5551SMasamitsu Yamazaki 	smi_info->timer_can_start = false;
1831b874b985SCorey Minyard 	if (smi_info->timer_running)
1832a9a2c44fSCorey Minyard 		del_timer_sync(&smi_info->si_timer);
1833a9a2c44fSCorey Minyard }
1834a9a2c44fSCorey Minyard 
18357e030d6dSCorey Minyard static struct smi_info *find_dup_si(struct smi_info *info)
1836b0defcdbSCorey Minyard {
1837b0defcdbSCorey Minyard 	struct smi_info *e;
1838b0defcdbSCorey Minyard 
1839b0defcdbSCorey Minyard 	list_for_each_entry(e, &smi_infos, link) {
1840f6296bdcSCorey Minyard 		if (e->io.addr_space != info->io.addr_space)
1841b0defcdbSCorey Minyard 			continue;
184294671710SCorey Minyard 		if (e->io.addr_data == info->io.addr_data) {
184394671710SCorey Minyard 			/*
184494671710SCorey Minyard 			 * This is a cheap hack, ACPI doesn't have a defined
184594671710SCorey Minyard 			 * slave address but SMBIOS does.  Pick it up from
184694671710SCorey Minyard 			 * any source that has it available.
184794671710SCorey Minyard 			 */
1848910840f2SCorey Minyard 			if (info->io.slave_addr && !e->io.slave_addr)
1849910840f2SCorey Minyard 				e->io.slave_addr = info->io.slave_addr;
18507e030d6dSCorey Minyard 			return e;
1851b0defcdbSCorey Minyard 		}
185294671710SCorey Minyard 	}
1853b0defcdbSCorey Minyard 
18547e030d6dSCorey Minyard 	return NULL;
1855b0defcdbSCorey Minyard }
1856b0defcdbSCorey Minyard 
1857bb398a4cSCorey Minyard int ipmi_si_add_smi(struct si_sm_io *io)
18582407d77aSMatthew Garrett {
18592407d77aSMatthew Garrett 	int rv = 0;
1860bb398a4cSCorey Minyard 	struct smi_info *new_smi, *dup;
18612407d77aSMatthew Garrett 
186241b766d6SCorey Minyard 	/*
186341b766d6SCorey Minyard 	 * If the user gave us a hard-coded device at the same
186441b766d6SCorey Minyard 	 * address, they presumably want us to use it and not what is
186541b766d6SCorey Minyard 	 * in the firmware.
186641b766d6SCorey Minyard 	 */
18673bb8ea40SCorey Minyard 	if (io->addr_source != SI_HARDCODED && io->addr_source != SI_HOTMOD &&
1868f6296bdcSCorey Minyard 	    ipmi_si_hardcode_match(io->addr_space, io->addr_data)) {
186941b766d6SCorey Minyard 		dev_info(io->dev,
187041b766d6SCorey Minyard 			 "Hard-coded device at this address already exists");
187141b766d6SCorey Minyard 		return -ENODEV;
187241b766d6SCorey Minyard 	}
187341b766d6SCorey Minyard 
1874bb398a4cSCorey Minyard 	if (!io->io_setup) {
1875f6296bdcSCorey Minyard 		if (io->addr_space == IPMI_IO_ADDR_SPACE) {
187658e27635SCorey Minyard 			io->io_setup = ipmi_si_port_setup;
1877f6296bdcSCorey Minyard 		} else if (io->addr_space == IPMI_MEM_ADDR_SPACE) {
187858e27635SCorey Minyard 			io->io_setup = ipmi_si_mem_setup;
1879e1eeb7f8SCorey Minyard 		} else {
1880e1eeb7f8SCorey Minyard 			return -EINVAL;
1881e1eeb7f8SCorey Minyard 		}
1882e1eeb7f8SCorey Minyard 	}
1883e1eeb7f8SCorey Minyard 
188467f4fb02SCorey Minyard 	new_smi = kzalloc(sizeof(*new_smi), GFP_KERNEL);
1885bb398a4cSCorey Minyard 	if (!new_smi)
1886bb398a4cSCorey Minyard 		return -ENOMEM;
188767f4fb02SCorey Minyard 	spin_lock_init(&new_smi->si_lock);
1888bb398a4cSCorey Minyard 
1889bb398a4cSCorey Minyard 	new_smi->io = *io;
1890bb398a4cSCorey Minyard 
18912407d77aSMatthew Garrett 	mutex_lock(&smi_infos_lock);
18927e030d6dSCorey Minyard 	dup = find_dup_si(new_smi);
18937e030d6dSCorey Minyard 	if (dup) {
1894910840f2SCorey Minyard 		if (new_smi->io.addr_source == SI_ACPI &&
1895910840f2SCorey Minyard 		    dup->io.addr_source == SI_SMBIOS) {
18967e030d6dSCorey Minyard 			/* We prefer ACPI over SMBIOS. */
1897910840f2SCorey Minyard 			dev_info(dup->io.dev,
18987e030d6dSCorey Minyard 				 "Removing SMBIOS-specified %s state machine in favor of ACPI\n",
1899910840f2SCorey Minyard 				 si_to_str[new_smi->io.si_type]);
19007e030d6dSCorey Minyard 			cleanup_one_si(dup);
19017e030d6dSCorey Minyard 		} else {
1902910840f2SCorey Minyard 			dev_info(new_smi->io.dev,
19037e030d6dSCorey Minyard 				 "%s-specified %s state machine: duplicate\n",
1904910840f2SCorey Minyard 				 ipmi_addr_src_to_str(new_smi->io.addr_source),
1905910840f2SCorey Minyard 				 si_to_str[new_smi->io.si_type]);
19062407d77aSMatthew Garrett 			rv = -EBUSY;
1907c0a32fe1SColin Ian King 			kfree(new_smi);
19082407d77aSMatthew Garrett 			goto out_err;
19092407d77aSMatthew Garrett 		}
19107e030d6dSCorey Minyard 	}
19112407d77aSMatthew Garrett 
191225880f7dSJoe Perches 	pr_info("Adding %s-specified %s state machine\n",
1913910840f2SCorey Minyard 		ipmi_addr_src_to_str(new_smi->io.addr_source),
1914910840f2SCorey Minyard 		si_to_str[new_smi->io.si_type]);
19152407d77aSMatthew Garrett 
19162407d77aSMatthew Garrett 	list_add_tail(&new_smi->link, &smi_infos);
19172407d77aSMatthew Garrett 
191893c303d2SCorey Minyard 	if (initialized)
1919bb398a4cSCorey Minyard 		rv = try_smi_init(new_smi);
19202407d77aSMatthew Garrett out_err:
19212407d77aSMatthew Garrett 	mutex_unlock(&smi_infos_lock);
19222407d77aSMatthew Garrett 	return rv;
19232407d77aSMatthew Garrett }
19242407d77aSMatthew Garrett 
19253f724c40STony Camuso /*
19263f724c40STony Camuso  * Try to start up an interface.  Must be called with smi_infos_lock
19273f724c40STony Camuso  * held, primarily to keep smi_num consistent, we only one to do these
19283f724c40STony Camuso  * one at a time.
19293f724c40STony Camuso  */
1930b0defcdbSCorey Minyard static int try_smi_init(struct smi_info *new_smi)
19311da177e4SLinus Torvalds {
19322407d77aSMatthew Garrett 	int rv = 0;
193364959e2dSCorey Minyard 	int i;
19341da177e4SLinus Torvalds 
193525880f7dSJoe Perches 	pr_info("Trying %s-specified %s state machine at %s address 0x%lx, slave address 0x%x, irq %d\n",
1936910840f2SCorey Minyard 		ipmi_addr_src_to_str(new_smi->io.addr_source),
1937910840f2SCorey Minyard 		si_to_str[new_smi->io.si_type],
1938f6296bdcSCorey Minyard 		addr_space_to_str[new_smi->io.addr_space],
1939b0defcdbSCorey Minyard 		new_smi->io.addr_data,
1940910840f2SCorey Minyard 		new_smi->io.slave_addr, new_smi->io.irq);
19411da177e4SLinus Torvalds 
1942910840f2SCorey Minyard 	switch (new_smi->io.si_type) {
1943b0defcdbSCorey Minyard 	case SI_KCS:
19441da177e4SLinus Torvalds 		new_smi->handlers = &kcs_smi_handlers;
1945b0defcdbSCorey Minyard 		break;
1946b0defcdbSCorey Minyard 
1947b0defcdbSCorey Minyard 	case SI_SMIC:
19481da177e4SLinus Torvalds 		new_smi->handlers = &smic_smi_handlers;
1949b0defcdbSCorey Minyard 		break;
1950b0defcdbSCorey Minyard 
1951b0defcdbSCorey Minyard 	case SI_BT:
19521da177e4SLinus Torvalds 		new_smi->handlers = &bt_smi_handlers;
1953b0defcdbSCorey Minyard 		break;
1954b0defcdbSCorey Minyard 
1955b0defcdbSCorey Minyard 	default:
19561da177e4SLinus Torvalds 		/* No support for anything else yet. */
19571da177e4SLinus Torvalds 		rv = -EIO;
19581da177e4SLinus Torvalds 		goto out_err;
19591da177e4SLinus Torvalds 	}
19601da177e4SLinus Torvalds 
196157bccb4eSCorey Minyard 	new_smi->si_num = smi_num;
19623f724c40STony Camuso 
19631abf71eeSCorey Minyard 	/* Do this early so it's available for logs. */
1964910840f2SCorey Minyard 	if (!new_smi->io.dev) {
196590b2d4f1SCorey Minyard 		pr_err("IPMI interface added with no device\n");
196690b2d4f1SCorey Minyard 		rv = EIO;
19671abf71eeSCorey Minyard 		goto out_err;
19681abf71eeSCorey Minyard 	}
19691abf71eeSCorey Minyard 
19701da177e4SLinus Torvalds 	/* Allocate the state machine's data and initialize it. */
19711da177e4SLinus Torvalds 	new_smi->si_sm = kmalloc(new_smi->handlers->size(), GFP_KERNEL);
19721da177e4SLinus Torvalds 	if (!new_smi->si_sm) {
19731da177e4SLinus Torvalds 		rv = -ENOMEM;
19741da177e4SLinus Torvalds 		goto out_err;
19751da177e4SLinus Torvalds 	}
1976e1eeb7f8SCorey Minyard 	new_smi->io.io_size = new_smi->handlers->init_data(new_smi->si_sm,
19771da177e4SLinus Torvalds 							   &new_smi->io);
19781da177e4SLinus Torvalds 
19791da177e4SLinus Torvalds 	/* Now that we know the I/O size, we can set up the I/O. */
1980e1eeb7f8SCorey Minyard 	rv = new_smi->io.io_setup(&new_smi->io);
19811da177e4SLinus Torvalds 	if (rv) {
1982910840f2SCorey Minyard 		dev_err(new_smi->io.dev, "Could not set up I/O space\n");
19831da177e4SLinus Torvalds 		goto out_err;
19841da177e4SLinus Torvalds 	}
19851da177e4SLinus Torvalds 
19861da177e4SLinus Torvalds 	/* Do low-level detection first. */
19871da177e4SLinus Torvalds 	if (new_smi->handlers->detect(new_smi->si_sm)) {
1988910840f2SCorey Minyard 		if (new_smi->io.addr_source)
1989910840f2SCorey Minyard 			dev_err(new_smi->io.dev,
1990910840f2SCorey Minyard 				"Interface detection failed\n");
19911da177e4SLinus Torvalds 		rv = -ENODEV;
19921da177e4SLinus Torvalds 		goto out_err;
19931da177e4SLinus Torvalds 	}
19941da177e4SLinus Torvalds 
1995c305e3d3SCorey Minyard 	/*
1996c305e3d3SCorey Minyard 	 * Attempt a get device id command.  If it fails, we probably
1997c305e3d3SCorey Minyard 	 * don't have a BMC here.
1998c305e3d3SCorey Minyard 	 */
19991da177e4SLinus Torvalds 	rv = try_get_dev_id(new_smi);
2000b0defcdbSCorey Minyard 	if (rv) {
2001910840f2SCorey Minyard 		if (new_smi->io.addr_source)
2002910840f2SCorey Minyard 			dev_err(new_smi->io.dev,
2003910840f2SCorey Minyard 			       "There appears to be no BMC at this location\n");
20041da177e4SLinus Torvalds 		goto out_err;
2005b0defcdbSCorey Minyard 	}
20061da177e4SLinus Torvalds 
20073ae0e0f9SCorey Minyard 	setup_oem_data_handler(new_smi);
2008ea94027bSCorey Minyard 	setup_xaction_handlers(new_smi);
2009d0882897SCorey Minyard 	check_for_broken_irqs(new_smi);
20103ae0e0f9SCorey Minyard 
2011b874b985SCorey Minyard 	new_smi->waiting_msg = NULL;
20121da177e4SLinus Torvalds 	new_smi->curr_msg = NULL;
20131da177e4SLinus Torvalds 	atomic_set(&new_smi->req_events, 0);
20147aefac26SCorey Minyard 	new_smi->run_to_completion = false;
201564959e2dSCorey Minyard 	for (i = 0; i < SI_NUM_STATS; i++)
201664959e2dSCorey Minyard 		atomic_set(&new_smi->stats[i], 0);
20171da177e4SLinus Torvalds 
20187aefac26SCorey Minyard 	new_smi->interrupt_disabled = true;
201989986496SCorey Minyard 	atomic_set(&new_smi->need_watch, 0);
20201da177e4SLinus Torvalds 
202140112ae7SCorey Minyard 	rv = try_enable_event_buffer(new_smi);
202240112ae7SCorey Minyard 	if (rv == 0)
20237aefac26SCorey Minyard 		new_smi->has_event_buffer = true;
202440112ae7SCorey Minyard 
2025c305e3d3SCorey Minyard 	/*
2026c305e3d3SCorey Minyard 	 * Start clearing the flags before we enable interrupts or the
2027c305e3d3SCorey Minyard 	 * timer to avoid racing with the timer.
2028c305e3d3SCorey Minyard 	 */
20294f7f5551SMasamitsu Yamazaki 	start_clear_flags(new_smi);
2030d9b7e4f7SCorey Minyard 
2031d9b7e4f7SCorey Minyard 	/*
2032d9b7e4f7SCorey Minyard 	 * IRQ is defined to be set when non-zero.  req_events will
2033d9b7e4f7SCorey Minyard 	 * cause a global flags check that will enable interrupts.
2034d9b7e4f7SCorey Minyard 	 */
2035910840f2SCorey Minyard 	if (new_smi->io.irq) {
2036d9b7e4f7SCorey Minyard 		new_smi->interrupt_disabled = false;
2037d9b7e4f7SCorey Minyard 		atomic_set(&new_smi->req_events, 1);
2038d9b7e4f7SCorey Minyard 	}
20391da177e4SLinus Torvalds 
20403dd377b5SCorey Minyard 	dev_set_drvdata(new_smi->io.dev, new_smi);
20413dd377b5SCorey Minyard 	rv = device_add_group(new_smi->io.dev, &ipmi_si_dev_attr_group);
20423dd377b5SCorey Minyard 	if (rv) {
20433dd377b5SCorey Minyard 		dev_err(new_smi->io.dev,
20443dd377b5SCorey Minyard 			"Unable to add device attributes: error %d\n",
20453dd377b5SCorey Minyard 			rv);
204671404a2fSCorey Minyard 		goto out_err;
20473dd377b5SCorey Minyard 	}
2048cc095f0aSCorey Minyard 	new_smi->dev_group_added = true;
20493dd377b5SCorey Minyard 
20501da177e4SLinus Torvalds 	rv = ipmi_register_smi(&handlers,
20511da177e4SLinus Torvalds 			       new_smi,
2052910840f2SCorey Minyard 			       new_smi->io.dev,
2053910840f2SCorey Minyard 			       new_smi->io.slave_addr);
20541da177e4SLinus Torvalds 	if (rv) {
2055910840f2SCorey Minyard 		dev_err(new_smi->io.dev,
2056910840f2SCorey Minyard 			"Unable to register device: error %d\n",
20571da177e4SLinus Torvalds 			rv);
205871404a2fSCorey Minyard 		goto out_err;
20591da177e4SLinus Torvalds 	}
20601da177e4SLinus Torvalds 
20613f724c40STony Camuso 	/* Don't increment till we know we have succeeded. */
20623f724c40STony Camuso 	smi_num++;
20633f724c40STony Camuso 
2064910840f2SCorey Minyard 	dev_info(new_smi->io.dev, "IPMI %s interface initialized\n",
2065910840f2SCorey Minyard 		 si_to_str[new_smi->io.si_type]);
20661da177e4SLinus Torvalds 
2067910840f2SCorey Minyard 	WARN_ON(new_smi->io.dev->init_name != NULL);
20681da177e4SLinus Torvalds 
20691da177e4SLinus Torvalds  out_err:
2070401e7e88SYang Yingliang 	if (rv && new_smi->io.io_cleanup) {
2071401e7e88SYang Yingliang 		new_smi->io.io_cleanup(&new_smi->io);
2072401e7e88SYang Yingliang 		new_smi->io.io_cleanup = NULL;
2073401e7e88SYang Yingliang 	}
2074401e7e88SYang Yingliang 
20751da177e4SLinus Torvalds 	return rv;
20761da177e4SLinus Torvalds }
20771da177e4SLinus Torvalds 
207841b766d6SCorey Minyard static int __init init_ipmi_si(void)
20791da177e4SLinus Torvalds {
20802407d77aSMatthew Garrett 	struct smi_info *e;
208106ee4594SMatthew Garrett 	enum ipmi_addr_src type = SI_INVALID;
20821da177e4SLinus Torvalds 
20831da177e4SLinus Torvalds 	if (initialized)
20841da177e4SLinus Torvalds 		return 0;
20851da177e4SLinus Torvalds 
208641b766d6SCorey Minyard 	ipmi_hardcode_init();
20871da177e4SLinus Torvalds 
208841b766d6SCorey Minyard 	pr_info("IPMI System Interface driver\n");
2089d8cc5267SMatthew Garrett 
20909d70029eSCorey Minyard 	ipmi_si_platform_init();
20919d70029eSCorey Minyard 
209213d0b35cSCorey Minyard 	ipmi_si_pci_init();
2093b0defcdbSCorey Minyard 
2094c6f85a75SCorey Minyard 	ipmi_si_parisc_init();
2095fdbeb7deSThomas Bogendoerfer 
209606ee4594SMatthew Garrett 	/* We prefer devices with interrupts, but in the case of a machine
209706ee4594SMatthew Garrett 	   with multiple BMCs we assume that there will be several instances
209806ee4594SMatthew Garrett 	   of a given type so if we succeed in registering a type then also
209906ee4594SMatthew Garrett 	   try to register everything else of the same type */
21002407d77aSMatthew Garrett 	mutex_lock(&smi_infos_lock);
21012407d77aSMatthew Garrett 	list_for_each_entry(e, &smi_infos, link) {
210206ee4594SMatthew Garrett 		/* Try to register a device if it has an IRQ and we either
210306ee4594SMatthew Garrett 		   haven't successfully registered a device yet or this
210406ee4594SMatthew Garrett 		   device has the same type as one we successfully registered */
2105910840f2SCorey Minyard 		if (e->io.irq && (!type || e->io.addr_source == type)) {
2106d8cc5267SMatthew Garrett 			if (!try_smi_init(e)) {
2107910840f2SCorey Minyard 				type = e->io.addr_source;
210806ee4594SMatthew Garrett 			}
210906ee4594SMatthew Garrett 		}
211006ee4594SMatthew Garrett 	}
211106ee4594SMatthew Garrett 
211206ee4594SMatthew Garrett 	/* type will only have been set if we successfully registered an si */
2113bb398a4cSCorey Minyard 	if (type)
2114bb398a4cSCorey Minyard 		goto skip_fallback_noirq;
2115d8cc5267SMatthew Garrett 
2116d8cc5267SMatthew Garrett 	/* Fall back to the preferred device */
2117d8cc5267SMatthew Garrett 
2118d8cc5267SMatthew Garrett 	list_for_each_entry(e, &smi_infos, link) {
2119910840f2SCorey Minyard 		if (!e->io.irq && (!type || e->io.addr_source == type)) {
2120d8cc5267SMatthew Garrett 			if (!try_smi_init(e)) {
2121910840f2SCorey Minyard 				type = e->io.addr_source;
212206ee4594SMatthew Garrett 			}
212306ee4594SMatthew Garrett 		}
212406ee4594SMatthew Garrett 	}
2125bb398a4cSCorey Minyard 
2126bb398a4cSCorey Minyard skip_fallback_noirq:
2127*dd7450caSKefeng Wang 	initialized = true;
2128d8cc5267SMatthew Garrett 	mutex_unlock(&smi_infos_lock);
212906ee4594SMatthew Garrett 
213006ee4594SMatthew Garrett 	if (type)
2131d8cc5267SMatthew Garrett 		return 0;
21322407d77aSMatthew Garrett 
2133d6dfd131SCorey Minyard 	mutex_lock(&smi_infos_lock);
2134b361e27bSCorey Minyard 	if (unload_when_empty && list_empty(&smi_infos)) {
2135d6dfd131SCorey Minyard 		mutex_unlock(&smi_infos_lock);
2136d2478521SCorey Minyard 		cleanup_ipmi_si();
213725880f7dSJoe Perches 		pr_warn("Unable to find any System Interface(s)\n");
21381da177e4SLinus Torvalds 		return -ENODEV;
2139b0defcdbSCorey Minyard 	} else {
2140d6dfd131SCorey Minyard 		mutex_unlock(&smi_infos_lock);
21411da177e4SLinus Torvalds 		return 0;
21421da177e4SLinus Torvalds 	}
2143b0defcdbSCorey Minyard }
21441da177e4SLinus Torvalds module_init(init_ipmi_si);
21451da177e4SLinus Torvalds 
21467960f18aSCorey Minyard static void shutdown_smi(void *send_info)
21471da177e4SLinus Torvalds {
21487960f18aSCorey Minyard 	struct smi_info *smi_info = send_info;
2149b874b985SCorey Minyard 
215071404a2fSCorey Minyard 	if (smi_info->dev_group_added) {
215171404a2fSCorey Minyard 		device_remove_group(smi_info->io.dev, &ipmi_si_dev_attr_group);
215271404a2fSCorey Minyard 		smi_info->dev_group_added = false;
215371404a2fSCorey Minyard 	}
215471404a2fSCorey Minyard 	if (smi_info->io.dev)
215571404a2fSCorey Minyard 		dev_set_drvdata(smi_info->io.dev, NULL);
2156b0defcdbSCorey Minyard 
2157c305e3d3SCorey Minyard 	/*
2158b874b985SCorey Minyard 	 * Make sure that interrupts, the timer and the thread are
2159b874b985SCorey Minyard 	 * stopped and will not run again.
2160c305e3d3SCorey Minyard 	 */
216171404a2fSCorey Minyard 	smi_info->interrupt_disabled = true;
216271404a2fSCorey Minyard 	if (smi_info->io.irq_cleanup) {
216371404a2fSCorey Minyard 		smi_info->io.irq_cleanup(&smi_info->io);
216471404a2fSCorey Minyard 		smi_info->io.irq_cleanup = NULL;
216571404a2fSCorey Minyard 	}
216671404a2fSCorey Minyard 	stop_timer_and_thread(smi_info);
216771404a2fSCorey Minyard 
216871404a2fSCorey Minyard 	/*
216971404a2fSCorey Minyard 	 * Wait until we know that we are out of any interrupt
217071404a2fSCorey Minyard 	 * handlers might have been running before we freed the
217171404a2fSCorey Minyard 	 * interrupt.
217271404a2fSCorey Minyard 	 */
217317c0eb74SPaul E. McKenney 	synchronize_rcu();
21741da177e4SLinus Torvalds 
2175c305e3d3SCorey Minyard 	/*
2176c305e3d3SCorey Minyard 	 * Timeouts are stopped, now make sure the interrupts are off
2177b874b985SCorey Minyard 	 * in the BMC.  Note that timers and CPU interrupts are off,
2178b874b985SCorey Minyard 	 * so no need for locks.
2179c305e3d3SCorey Minyard 	 */
218071404a2fSCorey Minyard 	while (smi_info->curr_msg || (smi_info->si_state != SI_NORMAL)) {
218171404a2fSCorey Minyard 		poll(smi_info);
2182ee6cd5f8SCorey Minyard 		schedule_timeout_uninterruptible(1);
2183ee6cd5f8SCorey Minyard 	}
218471404a2fSCorey Minyard 	if (smi_info->handlers)
218571404a2fSCorey Minyard 		disable_si_irq(smi_info);
218671404a2fSCorey Minyard 	while (smi_info->curr_msg || (smi_info->si_state != SI_NORMAL)) {
218771404a2fSCorey Minyard 		poll(smi_info);
2188ee6cd5f8SCorey Minyard 		schedule_timeout_uninterruptible(1);
2189ee6cd5f8SCorey Minyard 	}
219071404a2fSCorey Minyard 	if (smi_info->handlers)
219171404a2fSCorey Minyard 		smi_info->handlers->cleanup(smi_info->si_sm);
2192ee6cd5f8SCorey Minyard 
219371404a2fSCorey Minyard 	if (smi_info->io.addr_source_cleanup) {
219471404a2fSCorey Minyard 		smi_info->io.addr_source_cleanup(&smi_info->io);
219571404a2fSCorey Minyard 		smi_info->io.addr_source_cleanup = NULL;
219671404a2fSCorey Minyard 	}
219771404a2fSCorey Minyard 	if (smi_info->io.io_cleanup) {
219871404a2fSCorey Minyard 		smi_info->io.io_cleanup(&smi_info->io);
219971404a2fSCorey Minyard 		smi_info->io.io_cleanup = NULL;
220071404a2fSCorey Minyard 	}
22011da177e4SLinus Torvalds 
220271404a2fSCorey Minyard 	kfree(smi_info->si_sm);
220371404a2fSCorey Minyard 	smi_info->si_sm = NULL;
22042512e40eSCorey Minyard 
22052512e40eSCorey Minyard 	smi_info->intf = NULL;
220671404a2fSCorey Minyard }
22071da177e4SLinus Torvalds 
220893c303d2SCorey Minyard /*
220993c303d2SCorey Minyard  * Must be called with smi_infos_lock held, to serialize the
221093c303d2SCorey Minyard  * smi_info->intf check.
221193c303d2SCorey Minyard  */
221271404a2fSCorey Minyard static void cleanup_one_si(struct smi_info *smi_info)
221371404a2fSCorey Minyard {
221471404a2fSCorey Minyard 	if (!smi_info)
221571404a2fSCorey Minyard 		return;
221650c812b2SCorey Minyard 
221771404a2fSCorey Minyard 	list_del(&smi_info->link);
221850c812b2SCorey Minyard 
22192512e40eSCorey Minyard 	if (smi_info->intf)
222093c303d2SCorey Minyard 		ipmi_unregister_smi(smi_info->intf);
222171404a2fSCorey Minyard 
222271404a2fSCorey Minyard 	kfree(smi_info);
22231da177e4SLinus Torvalds }
22241da177e4SLinus Torvalds 
2225bb398a4cSCorey Minyard int ipmi_si_remove_by_dev(struct device *dev)
2226bb398a4cSCorey Minyard {
2227bb398a4cSCorey Minyard 	struct smi_info *e;
2228bb398a4cSCorey Minyard 	int rv = -ENOENT;
2229bb398a4cSCorey Minyard 
2230bb398a4cSCorey Minyard 	mutex_lock(&smi_infos_lock);
2231bb398a4cSCorey Minyard 	list_for_each_entry(e, &smi_infos, link) {
2232bb398a4cSCorey Minyard 		if (e->io.dev == dev) {
2233bb398a4cSCorey Minyard 			cleanup_one_si(e);
2234bb398a4cSCorey Minyard 			rv = 0;
2235bb398a4cSCorey Minyard 			break;
2236bb398a4cSCorey Minyard 		}
2237bb398a4cSCorey Minyard 	}
2238bb398a4cSCorey Minyard 	mutex_unlock(&smi_infos_lock);
2239bb398a4cSCorey Minyard 
2240bb398a4cSCorey Minyard 	return rv;
2241bb398a4cSCorey Minyard }
2242bb398a4cSCorey Minyard 
2243bdb57b7bSCorey Minyard struct device *ipmi_si_remove_by_data(int addr_space, enum si_type si_type,
224444814ec9SCorey Minyard 				      unsigned long addr)
224544814ec9SCorey Minyard {
224644814ec9SCorey Minyard 	/* remove */
224744814ec9SCorey Minyard 	struct smi_info *e, *tmp_e;
2248bdb57b7bSCorey Minyard 	struct device *dev = NULL;
224944814ec9SCorey Minyard 
225044814ec9SCorey Minyard 	mutex_lock(&smi_infos_lock);
225144814ec9SCorey Minyard 	list_for_each_entry_safe(e, tmp_e, &smi_infos, link) {
2252f6296bdcSCorey Minyard 		if (e->io.addr_space != addr_space)
225344814ec9SCorey Minyard 			continue;
225444814ec9SCorey Minyard 		if (e->io.si_type != si_type)
225544814ec9SCorey Minyard 			continue;
2256bdb57b7bSCorey Minyard 		if (e->io.addr_data == addr) {
2257bdb57b7bSCorey Minyard 			dev = get_device(e->io.dev);
225844814ec9SCorey Minyard 			cleanup_one_si(e);
225944814ec9SCorey Minyard 		}
2260bdb57b7bSCorey Minyard 	}
226144814ec9SCorey Minyard 	mutex_unlock(&smi_infos_lock);
2262bdb57b7bSCorey Minyard 
2263bdb57b7bSCorey Minyard 	return dev;
226444814ec9SCorey Minyard }
226544814ec9SCorey Minyard 
22660dcf334cSSergey Senozhatsky static void cleanup_ipmi_si(void)
22671da177e4SLinus Torvalds {
2268b0defcdbSCorey Minyard 	struct smi_info *e, *tmp_e;
22691da177e4SLinus Torvalds 
22701da177e4SLinus Torvalds 	if (!initialized)
22711da177e4SLinus Torvalds 		return;
22721da177e4SLinus Torvalds 
227313d0b35cSCorey Minyard 	ipmi_si_pci_shutdown();
2274c6f85a75SCorey Minyard 
2275c6f85a75SCorey Minyard 	ipmi_si_parisc_shutdown();
2276b0defcdbSCorey Minyard 
22779d70029eSCorey Minyard 	ipmi_si_platform_shutdown();
2278dba9b4f6SCorey Minyard 
2279d6dfd131SCorey Minyard 	mutex_lock(&smi_infos_lock);
2280b0defcdbSCorey Minyard 	list_for_each_entry_safe(e, tmp_e, &smi_infos, link)
2281b0defcdbSCorey Minyard 		cleanup_one_si(e);
2282d6dfd131SCorey Minyard 	mutex_unlock(&smi_infos_lock);
228341b766d6SCorey Minyard 
228441b766d6SCorey Minyard 	ipmi_si_hardcode_exit();
2285bdb57b7bSCorey Minyard 	ipmi_si_hotmod_exit();
22861da177e4SLinus Torvalds }
22871da177e4SLinus Torvalds module_exit(cleanup_ipmi_si);
22881da177e4SLinus Torvalds 
22890944d889SCorey Minyard MODULE_ALIAS("platform:dmi-ipmi-si");
22901da177e4SLinus Torvalds MODULE_LICENSE("GPL");
22911fdd75bdSCorey Minyard MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
2292c305e3d3SCorey Minyard MODULE_DESCRIPTION("Interface to the IPMI driver for the KCS, SMIC, and BT"
2293c305e3d3SCorey Minyard 		   " system interfaces.");
2294