xref: /openbmc/linux/drivers/char/ipmi/ipmi_si_intf.c (revision 4f7f5551a760eb0124267be65763008169db7087)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * ipmi_si.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * The interface to the IPMI driver for the system interfaces (KCS, SMIC,
51da177e4SLinus Torvalds  * BT).
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * Author: MontaVista Software, Inc.
81da177e4SLinus Torvalds  *         Corey Minyard <minyard@mvista.com>
91da177e4SLinus Torvalds  *         source@mvista.com
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  * Copyright 2002 MontaVista Software Inc.
12dba9b4f6SCorey Minyard  * Copyright 2006 IBM Corp., Christian Krafft <krafft@de.ibm.com>
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  *  This program is free software; you can redistribute it and/or modify it
151da177e4SLinus Torvalds  *  under the terms of the GNU General Public License as published by the
161da177e4SLinus Torvalds  *  Free Software Foundation; either version 2 of the License, or (at your
171da177e4SLinus Torvalds  *  option) any later version.
181da177e4SLinus Torvalds  *
191da177e4SLinus Torvalds  *
201da177e4SLinus Torvalds  *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
211da177e4SLinus Torvalds  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
221da177e4SLinus Torvalds  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
231da177e4SLinus Torvalds  *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
241da177e4SLinus Torvalds  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
251da177e4SLinus Torvalds  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
261da177e4SLinus Torvalds  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
271da177e4SLinus Torvalds  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
281da177e4SLinus Torvalds  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
291da177e4SLinus Torvalds  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
301da177e4SLinus Torvalds  *
311da177e4SLinus Torvalds  *  You should have received a copy of the GNU General Public License along
321da177e4SLinus Torvalds  *  with this program; if not, write to the Free Software Foundation, Inc.,
331da177e4SLinus Torvalds  *  675 Mass Ave, Cambridge, MA 02139, USA.
341da177e4SLinus Torvalds  */
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds /*
371da177e4SLinus Torvalds  * This file holds the "policy" for the interface to the SMI state
381da177e4SLinus Torvalds  * machine.  It does the configuration, handles timers and interrupts,
391da177e4SLinus Torvalds  * and drives the real SMI state machine.
401da177e4SLinus Torvalds  */
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds #include <linux/module.h>
431da177e4SLinus Torvalds #include <linux/moduleparam.h>
441da177e4SLinus Torvalds #include <linux/sched.h>
4507412736SAlexey Dobriyan #include <linux/seq_file.h>
461da177e4SLinus Torvalds #include <linux/timer.h>
471da177e4SLinus Torvalds #include <linux/errno.h>
481da177e4SLinus Torvalds #include <linux/spinlock.h>
491da177e4SLinus Torvalds #include <linux/slab.h>
501da177e4SLinus Torvalds #include <linux/delay.h>
511da177e4SLinus Torvalds #include <linux/list.h>
52ea94027bSCorey Minyard #include <linux/notifier.h>
53b0defcdbSCorey Minyard #include <linux/mutex.h>
54e9a705a0SMatt Domsch #include <linux/kthread.h>
551da177e4SLinus Torvalds #include <asm/irq.h>
561da177e4SLinus Torvalds #include <linux/interrupt.h>
571da177e4SLinus Torvalds #include <linux/rcupdate.h>
5816f4232cSZhao Yakui #include <linux/ipmi.h>
591da177e4SLinus Torvalds #include <linux/ipmi_smi.h>
601e89a499SCorey Minyard #include "ipmi_si.h"
61b361e27bSCorey Minyard #include <linux/string.h>
62b361e27bSCorey Minyard #include <linux/ctype.h>
63dba9b4f6SCorey Minyard 
64b361e27bSCorey Minyard #define PFX "ipmi_si: "
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds /* Measure times between events in the driver. */
671da177e4SLinus Torvalds #undef DEBUG_TIMING
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds /* Call every 10 ms. */
701da177e4SLinus Torvalds #define SI_TIMEOUT_TIME_USEC	10000
711da177e4SLinus Torvalds #define SI_USEC_PER_JIFFY	(1000000/HZ)
721da177e4SLinus Torvalds #define SI_TIMEOUT_JIFFIES	(SI_TIMEOUT_TIME_USEC/SI_USEC_PER_JIFFY)
731da177e4SLinus Torvalds #define SI_SHORT_TIMEOUT_USEC  250 /* .25ms when the SM request a
741da177e4SLinus Torvalds 				      short timeout */
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds enum si_intf_state {
771da177e4SLinus Torvalds 	SI_NORMAL,
781da177e4SLinus Torvalds 	SI_GETTING_FLAGS,
791da177e4SLinus Torvalds 	SI_GETTING_EVENTS,
801da177e4SLinus Torvalds 	SI_CLEARING_FLAGS,
811da177e4SLinus Torvalds 	SI_GETTING_MESSAGES,
82d9b7e4f7SCorey Minyard 	SI_CHECKING_ENABLES,
83d9b7e4f7SCorey Minyard 	SI_SETTING_ENABLES
841da177e4SLinus Torvalds 	/* FIXME - add watchdog stuff. */
851da177e4SLinus Torvalds };
861da177e4SLinus Torvalds 
879dbf68f9SCorey Minyard /* Some BT-specific defines we need here. */
889dbf68f9SCorey Minyard #define IPMI_BT_INTMASK_REG		2
899dbf68f9SCorey Minyard #define IPMI_BT_INTMASK_CLEAR_IRQ_BIT	2
909dbf68f9SCorey Minyard #define IPMI_BT_INTMASK_ENABLE_IRQ_BIT	1
919dbf68f9SCorey Minyard 
9295e300c0SCorey Minyard static const char * const si_to_str[] = { "invalid", "kcs", "smic", "bt" };
931da177e4SLinus Torvalds 
94bb398a4cSCorey Minyard static int initialized;
95bb398a4cSCorey Minyard 
9664959e2dSCorey Minyard /*
9764959e2dSCorey Minyard  * Indexes into stats[] in smi_info below.
9864959e2dSCorey Minyard  */
99ba8ff1c6SCorey Minyard enum si_stat_indexes {
100ba8ff1c6SCorey Minyard 	/*
101ba8ff1c6SCorey Minyard 	 * Number of times the driver requested a timer while an operation
102ba8ff1c6SCorey Minyard 	 * was in progress.
103ba8ff1c6SCorey Minyard 	 */
104ba8ff1c6SCorey Minyard 	SI_STAT_short_timeouts = 0,
10564959e2dSCorey Minyard 
106ba8ff1c6SCorey Minyard 	/*
107ba8ff1c6SCorey Minyard 	 * Number of times the driver requested a timer while nothing was in
108ba8ff1c6SCorey Minyard 	 * progress.
109ba8ff1c6SCorey Minyard 	 */
110ba8ff1c6SCorey Minyard 	SI_STAT_long_timeouts,
11164959e2dSCorey Minyard 
112ba8ff1c6SCorey Minyard 	/* Number of times the interface was idle while being polled. */
113ba8ff1c6SCorey Minyard 	SI_STAT_idles,
114ba8ff1c6SCorey Minyard 
115ba8ff1c6SCorey Minyard 	/* Number of interrupts the driver handled. */
116ba8ff1c6SCorey Minyard 	SI_STAT_interrupts,
117ba8ff1c6SCorey Minyard 
118ba8ff1c6SCorey Minyard 	/* Number of time the driver got an ATTN from the hardware. */
119ba8ff1c6SCorey Minyard 	SI_STAT_attentions,
120ba8ff1c6SCorey Minyard 
121ba8ff1c6SCorey Minyard 	/* Number of times the driver requested flags from the hardware. */
122ba8ff1c6SCorey Minyard 	SI_STAT_flag_fetches,
123ba8ff1c6SCorey Minyard 
124ba8ff1c6SCorey Minyard 	/* Number of times the hardware didn't follow the state machine. */
125ba8ff1c6SCorey Minyard 	SI_STAT_hosed_count,
126ba8ff1c6SCorey Minyard 
127ba8ff1c6SCorey Minyard 	/* Number of completed messages. */
128ba8ff1c6SCorey Minyard 	SI_STAT_complete_transactions,
129ba8ff1c6SCorey Minyard 
130ba8ff1c6SCorey Minyard 	/* Number of IPMI events received from the hardware. */
131ba8ff1c6SCorey Minyard 	SI_STAT_events,
132ba8ff1c6SCorey Minyard 
133ba8ff1c6SCorey Minyard 	/* Number of watchdog pretimeouts. */
134ba8ff1c6SCorey Minyard 	SI_STAT_watchdog_pretimeouts,
135ba8ff1c6SCorey Minyard 
136b3834be5SAdam Buchbinder 	/* Number of asynchronous messages received. */
137ba8ff1c6SCorey Minyard 	SI_STAT_incoming_messages,
138ba8ff1c6SCorey Minyard 
139ba8ff1c6SCorey Minyard 
140ba8ff1c6SCorey Minyard 	/* This *must* remain last, add new values above this. */
141ba8ff1c6SCorey Minyard 	SI_NUM_STATS
142ba8ff1c6SCorey Minyard };
14364959e2dSCorey Minyard 
144c305e3d3SCorey Minyard struct smi_info {
145a9a2c44fSCorey Minyard 	int                    intf_num;
1461da177e4SLinus Torvalds 	ipmi_smi_t             intf;
1471da177e4SLinus Torvalds 	struct si_sm_data      *si_sm;
14881d02b7fSCorey Minyard 	const struct si_sm_handlers *handlers;
1491da177e4SLinus Torvalds 	spinlock_t             si_lock;
150b874b985SCorey Minyard 	struct ipmi_smi_msg    *waiting_msg;
1511da177e4SLinus Torvalds 	struct ipmi_smi_msg    *curr_msg;
1521da177e4SLinus Torvalds 	enum si_intf_state     si_state;
1531da177e4SLinus Torvalds 
154c305e3d3SCorey Minyard 	/*
155c305e3d3SCorey Minyard 	 * Used to handle the various types of I/O that can occur with
156c305e3d3SCorey Minyard 	 * IPMI
157c305e3d3SCorey Minyard 	 */
1581da177e4SLinus Torvalds 	struct si_sm_io io;
1591da177e4SLinus Torvalds 
160c305e3d3SCorey Minyard 	/*
161c305e3d3SCorey Minyard 	 * Per-OEM handler, called from handle_flags().  Returns 1
162c305e3d3SCorey Minyard 	 * when handle_flags() needs to be re-run or 0 indicating it
163c305e3d3SCorey Minyard 	 * set si_state itself.
1643ae0e0f9SCorey Minyard 	 */
1653ae0e0f9SCorey Minyard 	int (*oem_data_avail_handler)(struct smi_info *smi_info);
1663ae0e0f9SCorey Minyard 
167c305e3d3SCorey Minyard 	/*
168c305e3d3SCorey Minyard 	 * Flags from the last GET_MSG_FLAGS command, used when an ATTN
169c305e3d3SCorey Minyard 	 * is set to hold the flags until we are done handling everything
170c305e3d3SCorey Minyard 	 * from the flags.
171c305e3d3SCorey Minyard 	 */
1721da177e4SLinus Torvalds #define RECEIVE_MSG_AVAIL	0x01
1731da177e4SLinus Torvalds #define EVENT_MSG_BUFFER_FULL	0x02
1741da177e4SLinus Torvalds #define WDT_PRE_TIMEOUT_INT	0x08
1753ae0e0f9SCorey Minyard #define OEM0_DATA_AVAIL     0x20
1763ae0e0f9SCorey Minyard #define OEM1_DATA_AVAIL     0x40
1773ae0e0f9SCorey Minyard #define OEM2_DATA_AVAIL     0x80
1783ae0e0f9SCorey Minyard #define OEM_DATA_AVAIL      (OEM0_DATA_AVAIL | \
1793ae0e0f9SCorey Minyard 			     OEM1_DATA_AVAIL | \
1803ae0e0f9SCorey Minyard 			     OEM2_DATA_AVAIL)
1811da177e4SLinus Torvalds 	unsigned char       msg_flags;
1821da177e4SLinus Torvalds 
18340112ae7SCorey Minyard 	/* Does the BMC have an event buffer? */
1847aefac26SCorey Minyard 	bool		    has_event_buffer;
18540112ae7SCorey Minyard 
186c305e3d3SCorey Minyard 	/*
187c305e3d3SCorey Minyard 	 * If set to true, this will request events the next time the
188c305e3d3SCorey Minyard 	 * state machine is idle.
189c305e3d3SCorey Minyard 	 */
1901da177e4SLinus Torvalds 	atomic_t            req_events;
1911da177e4SLinus Torvalds 
192c305e3d3SCorey Minyard 	/*
193c305e3d3SCorey Minyard 	 * If true, run the state machine to completion on every send
194c305e3d3SCorey Minyard 	 * call.  Generally used after a panic to make sure stuff goes
195c305e3d3SCorey Minyard 	 * out.
196c305e3d3SCorey Minyard 	 */
1977aefac26SCorey Minyard 	bool                run_to_completion;
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds 	/* The timer for this si. */
2001da177e4SLinus Torvalds 	struct timer_list   si_timer;
2011da177e4SLinus Torvalds 
202*4f7f5551SMasamitsu Yamazaki 	/* This flag is set, if the timer can be set */
203*4f7f5551SMasamitsu Yamazaki 	bool		    timer_can_start;
204*4f7f5551SMasamitsu Yamazaki 
20548e8ac29SBodo Stroesser 	/* This flag is set, if the timer is running (timer_pending() isn't enough) */
20648e8ac29SBodo Stroesser 	bool		    timer_running;
20748e8ac29SBodo Stroesser 
2081da177e4SLinus Torvalds 	/* The time (in jiffies) the last timeout occurred at. */
2091da177e4SLinus Torvalds 	unsigned long       last_timeout_jiffies;
2101da177e4SLinus Torvalds 
21189986496SCorey Minyard 	/* Are we waiting for the events, pretimeouts, received msgs? */
21289986496SCorey Minyard 	atomic_t            need_watch;
21389986496SCorey Minyard 
214c305e3d3SCorey Minyard 	/*
215c305e3d3SCorey Minyard 	 * The driver will disable interrupts when it gets into a
216c305e3d3SCorey Minyard 	 * situation where it cannot handle messages due to lack of
217c305e3d3SCorey Minyard 	 * memory.  Once that situation clears up, it will re-enable
218c305e3d3SCorey Minyard 	 * interrupts.
219c305e3d3SCorey Minyard 	 */
2207aefac26SCorey Minyard 	bool interrupt_disabled;
2211da177e4SLinus Torvalds 
222d9b7e4f7SCorey Minyard 	/*
223d9b7e4f7SCorey Minyard 	 * Does the BMC support events?
224d9b7e4f7SCorey Minyard 	 */
225d9b7e4f7SCorey Minyard 	bool supports_event_msg_buff;
226d9b7e4f7SCorey Minyard 
227a8df150cSCorey Minyard 	/*
228d0882897SCorey Minyard 	 * Can we disable interrupts the global enables receive irq
229d0882897SCorey Minyard 	 * bit?  There are currently two forms of brokenness, some
230d0882897SCorey Minyard 	 * systems cannot disable the bit (which is technically within
231d0882897SCorey Minyard 	 * the spec but a bad idea) and some systems have the bit
232d0882897SCorey Minyard 	 * forced to zero even though interrupts work (which is
233d0882897SCorey Minyard 	 * clearly outside the spec).  The next bool tells which form
234d0882897SCorey Minyard 	 * of brokenness is present.
2351e7d6a45SCorey Minyard 	 */
236d0882897SCorey Minyard 	bool cannot_disable_irq;
237d0882897SCorey Minyard 
238d0882897SCorey Minyard 	/*
239d0882897SCorey Minyard 	 * Some systems are broken and cannot set the irq enable
240d0882897SCorey Minyard 	 * bit, even if they support interrupts.
241d0882897SCorey Minyard 	 */
242d0882897SCorey Minyard 	bool irq_enable_broken;
2431e7d6a45SCorey Minyard 
2441e7d6a45SCorey Minyard 	/*
245a8df150cSCorey Minyard 	 * Did we get an attention that we did not handle?
246a8df150cSCorey Minyard 	 */
247a8df150cSCorey Minyard 	bool got_attn;
248a8df150cSCorey Minyard 
24950c812b2SCorey Minyard 	/* From the get device id response... */
2503ae0e0f9SCorey Minyard 	struct ipmi_device_id device_id;
2511da177e4SLinus Torvalds 
252910840f2SCorey Minyard 	/* Default driver model device. */
25350c812b2SCorey Minyard 	struct platform_device *pdev;
25450c812b2SCorey Minyard 
2551da177e4SLinus Torvalds 	/* Counters and things for the proc filesystem. */
25664959e2dSCorey Minyard 	atomic_t stats[SI_NUM_STATS];
257a9a2c44fSCorey Minyard 
258e9a705a0SMatt Domsch 	struct task_struct *thread;
259b0defcdbSCorey Minyard 
260b0defcdbSCorey Minyard 	struct list_head link;
2611da177e4SLinus Torvalds };
2621da177e4SLinus Torvalds 
26364959e2dSCorey Minyard #define smi_inc_stat(smi, stat) \
26464959e2dSCorey Minyard 	atomic_inc(&(smi)->stats[SI_STAT_ ## stat])
26564959e2dSCorey Minyard #define smi_get_stat(smi, stat) \
26664959e2dSCorey Minyard 	((unsigned int) atomic_read(&(smi)->stats[SI_STAT_ ## stat]))
26764959e2dSCorey Minyard 
2687a453308SCorey Minyard #define IPMI_MAX_INTFS 4
2697a453308SCorey Minyard static int force_kipmid[IPMI_MAX_INTFS];
270a51f4a81SCorey Minyard static int num_force_kipmid;
271a51f4a81SCorey Minyard 
2727a453308SCorey Minyard static unsigned int kipmid_max_busy_us[IPMI_MAX_INTFS];
273ae74e823SMartin Wilck static int num_max_busy_us;
274ae74e823SMartin Wilck 
2757aefac26SCorey Minyard static bool unload_when_empty = true;
276b361e27bSCorey Minyard 
277b0defcdbSCorey Minyard static int try_smi_init(struct smi_info *smi);
278b361e27bSCorey Minyard static void cleanup_one_si(struct smi_info *to_clean);
279d2478521SCorey Minyard static void cleanup_ipmi_si(void);
280b0defcdbSCorey Minyard 
281f93aae9fSJohn Stultz #ifdef DEBUG_TIMING
282f93aae9fSJohn Stultz void debug_timestamp(char *msg)
283f93aae9fSJohn Stultz {
28448862ea2SJohn Stultz 	struct timespec64 t;
285f93aae9fSJohn Stultz 
28648862ea2SJohn Stultz 	getnstimeofday64(&t);
28748862ea2SJohn Stultz 	pr_debug("**%s: %lld.%9.9ld\n", msg, (long long) t.tv_sec, t.tv_nsec);
288f93aae9fSJohn Stultz }
289f93aae9fSJohn Stultz #else
290f93aae9fSJohn Stultz #define debug_timestamp(x)
291f93aae9fSJohn Stultz #endif
292f93aae9fSJohn Stultz 
293e041c683SAlan Stern static ATOMIC_NOTIFIER_HEAD(xaction_notifier_list);
294ea94027bSCorey Minyard static int register_xaction_notifier(struct notifier_block *nb)
295ea94027bSCorey Minyard {
296e041c683SAlan Stern 	return atomic_notifier_chain_register(&xaction_notifier_list, nb);
297ea94027bSCorey Minyard }
298ea94027bSCorey Minyard 
2991da177e4SLinus Torvalds static void deliver_recv_msg(struct smi_info *smi_info,
3001da177e4SLinus Torvalds 			     struct ipmi_smi_msg *msg)
3011da177e4SLinus Torvalds {
3027adf579cSCorey Minyard 	/* Deliver the message to the upper layer. */
303968bf7ccSCorey Minyard 	if (smi_info->intf)
304a747c5abSJiri Kosina 		ipmi_smi_msg_received(smi_info->intf, msg);
305968bf7ccSCorey Minyard 	else
306968bf7ccSCorey Minyard 		ipmi_free_smi_msg(msg);
307a747c5abSJiri Kosina }
3081da177e4SLinus Torvalds 
3094d7cbac7SCorey Minyard static void return_hosed_msg(struct smi_info *smi_info, int cCode)
3101da177e4SLinus Torvalds {
3111da177e4SLinus Torvalds 	struct ipmi_smi_msg *msg = smi_info->curr_msg;
3121da177e4SLinus Torvalds 
3134d7cbac7SCorey Minyard 	if (cCode < 0 || cCode > IPMI_ERR_UNSPECIFIED)
3144d7cbac7SCorey Minyard 		cCode = IPMI_ERR_UNSPECIFIED;
3154d7cbac7SCorey Minyard 	/* else use it as is */
3164d7cbac7SCorey Minyard 
31725985edcSLucas De Marchi 	/* Make it a response */
3181da177e4SLinus Torvalds 	msg->rsp[0] = msg->data[0] | 4;
3191da177e4SLinus Torvalds 	msg->rsp[1] = msg->data[1];
3204d7cbac7SCorey Minyard 	msg->rsp[2] = cCode;
3211da177e4SLinus Torvalds 	msg->rsp_size = 3;
3221da177e4SLinus Torvalds 
3231da177e4SLinus Torvalds 	smi_info->curr_msg = NULL;
3241da177e4SLinus Torvalds 	deliver_recv_msg(smi_info, msg);
3251da177e4SLinus Torvalds }
3261da177e4SLinus Torvalds 
3271da177e4SLinus Torvalds static enum si_sm_result start_next_msg(struct smi_info *smi_info)
3281da177e4SLinus Torvalds {
3291da177e4SLinus Torvalds 	int              rv;
3301da177e4SLinus Torvalds 
331b874b985SCorey Minyard 	if (!smi_info->waiting_msg) {
3321da177e4SLinus Torvalds 		smi_info->curr_msg = NULL;
3331da177e4SLinus Torvalds 		rv = SI_SM_IDLE;
3341da177e4SLinus Torvalds 	} else {
3351da177e4SLinus Torvalds 		int err;
3361da177e4SLinus Torvalds 
337b874b985SCorey Minyard 		smi_info->curr_msg = smi_info->waiting_msg;
338b874b985SCorey Minyard 		smi_info->waiting_msg = NULL;
339f93aae9fSJohn Stultz 		debug_timestamp("Start2");
340e041c683SAlan Stern 		err = atomic_notifier_call_chain(&xaction_notifier_list,
341e041c683SAlan Stern 				0, smi_info);
342ea94027bSCorey Minyard 		if (err & NOTIFY_STOP_MASK) {
343ea94027bSCorey Minyard 			rv = SI_SM_CALL_WITHOUT_DELAY;
344ea94027bSCorey Minyard 			goto out;
345ea94027bSCorey Minyard 		}
3461da177e4SLinus Torvalds 		err = smi_info->handlers->start_transaction(
3471da177e4SLinus Torvalds 			smi_info->si_sm,
3481da177e4SLinus Torvalds 			smi_info->curr_msg->data,
3491da177e4SLinus Torvalds 			smi_info->curr_msg->data_size);
350c305e3d3SCorey Minyard 		if (err)
3514d7cbac7SCorey Minyard 			return_hosed_msg(smi_info, err);
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds 		rv = SI_SM_CALL_WITHOUT_DELAY;
3541da177e4SLinus Torvalds 	}
355ea94027bSCorey Minyard out:
3561da177e4SLinus Torvalds 	return rv;
3571da177e4SLinus Torvalds }
3581da177e4SLinus Torvalds 
3590cfec916SCorey Minyard static void smi_mod_timer(struct smi_info *smi_info, unsigned long new_val)
3600cfec916SCorey Minyard {
361*4f7f5551SMasamitsu Yamazaki 	if (!smi_info->timer_can_start)
362*4f7f5551SMasamitsu Yamazaki 		return;
3630cfec916SCorey Minyard 	smi_info->last_timeout_jiffies = jiffies;
3640cfec916SCorey Minyard 	mod_timer(&smi_info->si_timer, new_val);
3650cfec916SCorey Minyard 	smi_info->timer_running = true;
3660cfec916SCorey Minyard }
3670cfec916SCorey Minyard 
3680cfec916SCorey Minyard /*
3690cfec916SCorey Minyard  * Start a new message and (re)start the timer and thread.
3700cfec916SCorey Minyard  */
3710cfec916SCorey Minyard static void start_new_msg(struct smi_info *smi_info, unsigned char *msg,
3720cfec916SCorey Minyard 			  unsigned int size)
3730cfec916SCorey Minyard {
3740cfec916SCorey Minyard 	smi_mod_timer(smi_info, jiffies + SI_TIMEOUT_JIFFIES);
3750cfec916SCorey Minyard 
3760cfec916SCorey Minyard 	if (smi_info->thread)
3770cfec916SCorey Minyard 		wake_up_process(smi_info->thread);
3780cfec916SCorey Minyard 
3790cfec916SCorey Minyard 	smi_info->handlers->start_transaction(smi_info->si_sm, msg, size);
3800cfec916SCorey Minyard }
3810cfec916SCorey Minyard 
382*4f7f5551SMasamitsu Yamazaki static void start_check_enables(struct smi_info *smi_info)
383ee6cd5f8SCorey Minyard {
384ee6cd5f8SCorey Minyard 	unsigned char msg[2];
385ee6cd5f8SCorey Minyard 
386ee6cd5f8SCorey Minyard 	msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
387ee6cd5f8SCorey Minyard 	msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
388ee6cd5f8SCorey Minyard 
3890cfec916SCorey Minyard 	start_new_msg(smi_info, msg, 2);
390d9b7e4f7SCorey Minyard 	smi_info->si_state = SI_CHECKING_ENABLES;
391ee6cd5f8SCorey Minyard }
392ee6cd5f8SCorey Minyard 
393*4f7f5551SMasamitsu Yamazaki static void start_clear_flags(struct smi_info *smi_info)
3941da177e4SLinus Torvalds {
3951da177e4SLinus Torvalds 	unsigned char msg[3];
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds 	/* Make sure the watchdog pre-timeout flag is not set at startup. */
3981da177e4SLinus Torvalds 	msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
3991da177e4SLinus Torvalds 	msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
4001da177e4SLinus Torvalds 	msg[2] = WDT_PRE_TIMEOUT_INT;
4011da177e4SLinus Torvalds 
4020cfec916SCorey Minyard 	start_new_msg(smi_info, msg, 3);
4031da177e4SLinus Torvalds 	smi_info->si_state = SI_CLEARING_FLAGS;
4041da177e4SLinus Torvalds }
4051da177e4SLinus Torvalds 
406968bf7ccSCorey Minyard static void start_getting_msg_queue(struct smi_info *smi_info)
407968bf7ccSCorey Minyard {
408968bf7ccSCorey Minyard 	smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
409968bf7ccSCorey Minyard 	smi_info->curr_msg->data[1] = IPMI_GET_MSG_CMD;
410968bf7ccSCorey Minyard 	smi_info->curr_msg->data_size = 2;
411968bf7ccSCorey Minyard 
4120cfec916SCorey Minyard 	start_new_msg(smi_info, smi_info->curr_msg->data,
413968bf7ccSCorey Minyard 		      smi_info->curr_msg->data_size);
414968bf7ccSCorey Minyard 	smi_info->si_state = SI_GETTING_MESSAGES;
415968bf7ccSCorey Minyard }
416968bf7ccSCorey Minyard 
417968bf7ccSCorey Minyard static void start_getting_events(struct smi_info *smi_info)
418968bf7ccSCorey Minyard {
419968bf7ccSCorey Minyard 	smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
420968bf7ccSCorey Minyard 	smi_info->curr_msg->data[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD;
421968bf7ccSCorey Minyard 	smi_info->curr_msg->data_size = 2;
422968bf7ccSCorey Minyard 
4230cfec916SCorey Minyard 	start_new_msg(smi_info, smi_info->curr_msg->data,
424968bf7ccSCorey Minyard 		      smi_info->curr_msg->data_size);
425968bf7ccSCorey Minyard 	smi_info->si_state = SI_GETTING_EVENTS;
426968bf7ccSCorey Minyard }
427968bf7ccSCorey Minyard 
428c305e3d3SCorey Minyard /*
429c305e3d3SCorey Minyard  * When we have a situtaion where we run out of memory and cannot
430c305e3d3SCorey Minyard  * allocate messages, we just leave them in the BMC and run the system
431c305e3d3SCorey Minyard  * polled until we can allocate some memory.  Once we have some
432c305e3d3SCorey Minyard  * memory, we will re-enable the interrupt.
4331e7d6a45SCorey Minyard  *
4341e7d6a45SCorey Minyard  * Note that we cannot just use disable_irq(), since the interrupt may
4351e7d6a45SCorey Minyard  * be shared.
436c305e3d3SCorey Minyard  */
437*4f7f5551SMasamitsu Yamazaki static inline bool disable_si_irq(struct smi_info *smi_info)
4381da177e4SLinus Torvalds {
439910840f2SCorey Minyard 	if ((smi_info->io.irq) && (!smi_info->interrupt_disabled)) {
4407aefac26SCorey Minyard 		smi_info->interrupt_disabled = true;
441*4f7f5551SMasamitsu Yamazaki 		start_check_enables(smi_info);
442968bf7ccSCorey Minyard 		return true;
4431da177e4SLinus Torvalds 	}
444968bf7ccSCorey Minyard 	return false;
4451da177e4SLinus Torvalds }
4461da177e4SLinus Torvalds 
447968bf7ccSCorey Minyard static inline bool enable_si_irq(struct smi_info *smi_info)
4481da177e4SLinus Torvalds {
449910840f2SCorey Minyard 	if ((smi_info->io.irq) && (smi_info->interrupt_disabled)) {
4507aefac26SCorey Minyard 		smi_info->interrupt_disabled = false;
451*4f7f5551SMasamitsu Yamazaki 		start_check_enables(smi_info);
452968bf7ccSCorey Minyard 		return true;
4531da177e4SLinus Torvalds 	}
454968bf7ccSCorey Minyard 	return false;
455968bf7ccSCorey Minyard }
456968bf7ccSCorey Minyard 
457968bf7ccSCorey Minyard /*
458968bf7ccSCorey Minyard  * Allocate a message.  If unable to allocate, start the interrupt
459968bf7ccSCorey Minyard  * disable process and return NULL.  If able to allocate but
460968bf7ccSCorey Minyard  * interrupts are disabled, free the message and return NULL after
461968bf7ccSCorey Minyard  * starting the interrupt enable process.
462968bf7ccSCorey Minyard  */
463968bf7ccSCorey Minyard static struct ipmi_smi_msg *alloc_msg_handle_irq(struct smi_info *smi_info)
464968bf7ccSCorey Minyard {
465968bf7ccSCorey Minyard 	struct ipmi_smi_msg *msg;
466968bf7ccSCorey Minyard 
467968bf7ccSCorey Minyard 	msg = ipmi_alloc_smi_msg();
468968bf7ccSCorey Minyard 	if (!msg) {
469*4f7f5551SMasamitsu Yamazaki 		if (!disable_si_irq(smi_info))
470968bf7ccSCorey Minyard 			smi_info->si_state = SI_NORMAL;
471968bf7ccSCorey Minyard 	} else if (enable_si_irq(smi_info)) {
472968bf7ccSCorey Minyard 		ipmi_free_smi_msg(msg);
473968bf7ccSCorey Minyard 		msg = NULL;
474968bf7ccSCorey Minyard 	}
475968bf7ccSCorey Minyard 	return msg;
4761da177e4SLinus Torvalds }
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds static void handle_flags(struct smi_info *smi_info)
4791da177e4SLinus Torvalds {
4803ae0e0f9SCorey Minyard retry:
4811da177e4SLinus Torvalds 	if (smi_info->msg_flags & WDT_PRE_TIMEOUT_INT) {
4821da177e4SLinus Torvalds 		/* Watchdog pre-timeout */
48364959e2dSCorey Minyard 		smi_inc_stat(smi_info, watchdog_pretimeouts);
4841da177e4SLinus Torvalds 
485*4f7f5551SMasamitsu Yamazaki 		start_clear_flags(smi_info);
4861da177e4SLinus Torvalds 		smi_info->msg_flags &= ~WDT_PRE_TIMEOUT_INT;
487968bf7ccSCorey Minyard 		if (smi_info->intf)
4881da177e4SLinus Torvalds 			ipmi_smi_watchdog_pretimeout(smi_info->intf);
4891da177e4SLinus Torvalds 	} else if (smi_info->msg_flags & RECEIVE_MSG_AVAIL) {
4901da177e4SLinus Torvalds 		/* Messages available. */
491968bf7ccSCorey Minyard 		smi_info->curr_msg = alloc_msg_handle_irq(smi_info);
492968bf7ccSCorey Minyard 		if (!smi_info->curr_msg)
4931da177e4SLinus Torvalds 			return;
4941da177e4SLinus Torvalds 
495968bf7ccSCorey Minyard 		start_getting_msg_queue(smi_info);
4961da177e4SLinus Torvalds 	} else if (smi_info->msg_flags & EVENT_MSG_BUFFER_FULL) {
4971da177e4SLinus Torvalds 		/* Events available. */
498968bf7ccSCorey Minyard 		smi_info->curr_msg = alloc_msg_handle_irq(smi_info);
499968bf7ccSCorey Minyard 		if (!smi_info->curr_msg)
5001da177e4SLinus Torvalds 			return;
5011da177e4SLinus Torvalds 
502968bf7ccSCorey Minyard 		start_getting_events(smi_info);
5034064d5efSCorey Minyard 	} else if (smi_info->msg_flags & OEM_DATA_AVAIL &&
5044064d5efSCorey Minyard 		   smi_info->oem_data_avail_handler) {
5053ae0e0f9SCorey Minyard 		if (smi_info->oem_data_avail_handler(smi_info))
5063ae0e0f9SCorey Minyard 			goto retry;
507c305e3d3SCorey Minyard 	} else
5081da177e4SLinus Torvalds 		smi_info->si_state = SI_NORMAL;
5091da177e4SLinus Torvalds }
5101da177e4SLinus Torvalds 
511d9b7e4f7SCorey Minyard /*
512d9b7e4f7SCorey Minyard  * Global enables we care about.
513d9b7e4f7SCorey Minyard  */
514d9b7e4f7SCorey Minyard #define GLOBAL_ENABLES_MASK (IPMI_BMC_EVT_MSG_BUFF | IPMI_BMC_RCV_MSG_INTR | \
515d9b7e4f7SCorey Minyard 			     IPMI_BMC_EVT_MSG_INTR)
516d9b7e4f7SCorey Minyard 
51795c97b59SCorey Minyard static u8 current_global_enables(struct smi_info *smi_info, u8 base,
51895c97b59SCorey Minyard 				 bool *irq_on)
519d9b7e4f7SCorey Minyard {
520d9b7e4f7SCorey Minyard 	u8 enables = 0;
521d9b7e4f7SCorey Minyard 
522d9b7e4f7SCorey Minyard 	if (smi_info->supports_event_msg_buff)
523d9b7e4f7SCorey Minyard 		enables |= IPMI_BMC_EVT_MSG_BUFF;
524d9b7e4f7SCorey Minyard 
525910840f2SCorey Minyard 	if (((smi_info->io.irq && !smi_info->interrupt_disabled) ||
526d0882897SCorey Minyard 	     smi_info->cannot_disable_irq) &&
527d0882897SCorey Minyard 	    !smi_info->irq_enable_broken)
528d9b7e4f7SCorey Minyard 		enables |= IPMI_BMC_RCV_MSG_INTR;
529d9b7e4f7SCorey Minyard 
530d9b7e4f7SCorey Minyard 	if (smi_info->supports_event_msg_buff &&
531910840f2SCorey Minyard 	    smi_info->io.irq && !smi_info->interrupt_disabled &&
532d0882897SCorey Minyard 	    !smi_info->irq_enable_broken)
533d9b7e4f7SCorey Minyard 		enables |= IPMI_BMC_EVT_MSG_INTR;
534d9b7e4f7SCorey Minyard 
53595c97b59SCorey Minyard 	*irq_on = enables & (IPMI_BMC_EVT_MSG_INTR | IPMI_BMC_RCV_MSG_INTR);
53695c97b59SCorey Minyard 
537d9b7e4f7SCorey Minyard 	return enables;
538d9b7e4f7SCorey Minyard }
539d9b7e4f7SCorey Minyard 
54095c97b59SCorey Minyard static void check_bt_irq(struct smi_info *smi_info, bool irq_on)
54195c97b59SCorey Minyard {
54295c97b59SCorey Minyard 	u8 irqstate = smi_info->io.inputb(&smi_info->io, IPMI_BT_INTMASK_REG);
54395c97b59SCorey Minyard 
54495c97b59SCorey Minyard 	irqstate &= IPMI_BT_INTMASK_ENABLE_IRQ_BIT;
54595c97b59SCorey Minyard 
54695c97b59SCorey Minyard 	if ((bool)irqstate == irq_on)
54795c97b59SCorey Minyard 		return;
54895c97b59SCorey Minyard 
54995c97b59SCorey Minyard 	if (irq_on)
55095c97b59SCorey Minyard 		smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG,
55195c97b59SCorey Minyard 				     IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
55295c97b59SCorey Minyard 	else
55395c97b59SCorey Minyard 		smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG, 0);
55495c97b59SCorey Minyard }
55595c97b59SCorey Minyard 
5561da177e4SLinus Torvalds static void handle_transaction_done(struct smi_info *smi_info)
5571da177e4SLinus Torvalds {
5581da177e4SLinus Torvalds 	struct ipmi_smi_msg *msg;
5591da177e4SLinus Torvalds 
560f93aae9fSJohn Stultz 	debug_timestamp("Done");
5611da177e4SLinus Torvalds 	switch (smi_info->si_state) {
5621da177e4SLinus Torvalds 	case SI_NORMAL:
5631da177e4SLinus Torvalds 		if (!smi_info->curr_msg)
5641da177e4SLinus Torvalds 			break;
5651da177e4SLinus Torvalds 
5661da177e4SLinus Torvalds 		smi_info->curr_msg->rsp_size
5671da177e4SLinus Torvalds 			= smi_info->handlers->get_result(
5681da177e4SLinus Torvalds 				smi_info->si_sm,
5691da177e4SLinus Torvalds 				smi_info->curr_msg->rsp,
5701da177e4SLinus Torvalds 				IPMI_MAX_MSG_LENGTH);
5711da177e4SLinus Torvalds 
572c305e3d3SCorey Minyard 		/*
573c305e3d3SCorey Minyard 		 * Do this here becase deliver_recv_msg() releases the
574c305e3d3SCorey Minyard 		 * lock, and a new message can be put in during the
575c305e3d3SCorey Minyard 		 * time the lock is released.
576c305e3d3SCorey Minyard 		 */
5771da177e4SLinus Torvalds 		msg = smi_info->curr_msg;
5781da177e4SLinus Torvalds 		smi_info->curr_msg = NULL;
5791da177e4SLinus Torvalds 		deliver_recv_msg(smi_info, msg);
5801da177e4SLinus Torvalds 		break;
5811da177e4SLinus Torvalds 
5821da177e4SLinus Torvalds 	case SI_GETTING_FLAGS:
5831da177e4SLinus Torvalds 	{
5841da177e4SLinus Torvalds 		unsigned char msg[4];
5851da177e4SLinus Torvalds 		unsigned int  len;
5861da177e4SLinus Torvalds 
5871da177e4SLinus Torvalds 		/* We got the flags from the SMI, now handle them. */
5881da177e4SLinus Torvalds 		len = smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
5891da177e4SLinus Torvalds 		if (msg[2] != 0) {
590c305e3d3SCorey Minyard 			/* Error fetching flags, just give up for now. */
5911da177e4SLinus Torvalds 			smi_info->si_state = SI_NORMAL;
5921da177e4SLinus Torvalds 		} else if (len < 4) {
593c305e3d3SCorey Minyard 			/*
594c305e3d3SCorey Minyard 			 * Hmm, no flags.  That's technically illegal, but
595c305e3d3SCorey Minyard 			 * don't use uninitialized data.
596c305e3d3SCorey Minyard 			 */
5971da177e4SLinus Torvalds 			smi_info->si_state = SI_NORMAL;
5981da177e4SLinus Torvalds 		} else {
5991da177e4SLinus Torvalds 			smi_info->msg_flags = msg[3];
6001da177e4SLinus Torvalds 			handle_flags(smi_info);
6011da177e4SLinus Torvalds 		}
6021da177e4SLinus Torvalds 		break;
6031da177e4SLinus Torvalds 	}
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds 	case SI_CLEARING_FLAGS:
6061da177e4SLinus Torvalds 	{
6071da177e4SLinus Torvalds 		unsigned char msg[3];
6081da177e4SLinus Torvalds 
6091da177e4SLinus Torvalds 		/* We cleared the flags. */
6101da177e4SLinus Torvalds 		smi_info->handlers->get_result(smi_info->si_sm, msg, 3);
6111da177e4SLinus Torvalds 		if (msg[2] != 0) {
6121da177e4SLinus Torvalds 			/* Error clearing flags */
613910840f2SCorey Minyard 			dev_warn(smi_info->io.dev,
614279fbd0cSMyron Stowe 				 "Error clearing flags: %2.2x\n", msg[2]);
6151da177e4SLinus Torvalds 		}
6161da177e4SLinus Torvalds 		smi_info->si_state = SI_NORMAL;
6171da177e4SLinus Torvalds 		break;
6181da177e4SLinus Torvalds 	}
6191da177e4SLinus Torvalds 
6201da177e4SLinus Torvalds 	case SI_GETTING_EVENTS:
6211da177e4SLinus Torvalds 	{
6221da177e4SLinus Torvalds 		smi_info->curr_msg->rsp_size
6231da177e4SLinus Torvalds 			= smi_info->handlers->get_result(
6241da177e4SLinus Torvalds 				smi_info->si_sm,
6251da177e4SLinus Torvalds 				smi_info->curr_msg->rsp,
6261da177e4SLinus Torvalds 				IPMI_MAX_MSG_LENGTH);
6271da177e4SLinus Torvalds 
628c305e3d3SCorey Minyard 		/*
629c305e3d3SCorey Minyard 		 * Do this here becase deliver_recv_msg() releases the
630c305e3d3SCorey Minyard 		 * lock, and a new message can be put in during the
631c305e3d3SCorey Minyard 		 * time the lock is released.
632c305e3d3SCorey Minyard 		 */
6331da177e4SLinus Torvalds 		msg = smi_info->curr_msg;
6341da177e4SLinus Torvalds 		smi_info->curr_msg = NULL;
6351da177e4SLinus Torvalds 		if (msg->rsp[2] != 0) {
6361da177e4SLinus Torvalds 			/* Error getting event, probably done. */
6371da177e4SLinus Torvalds 			msg->done(msg);
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds 			/* Take off the event flag. */
6401da177e4SLinus Torvalds 			smi_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
6411da177e4SLinus Torvalds 			handle_flags(smi_info);
6421da177e4SLinus Torvalds 		} else {
64364959e2dSCorey Minyard 			smi_inc_stat(smi_info, events);
6441da177e4SLinus Torvalds 
645c305e3d3SCorey Minyard 			/*
646c305e3d3SCorey Minyard 			 * Do this before we deliver the message
647c305e3d3SCorey Minyard 			 * because delivering the message releases the
648c305e3d3SCorey Minyard 			 * lock and something else can mess with the
649c305e3d3SCorey Minyard 			 * state.
650c305e3d3SCorey Minyard 			 */
6511da177e4SLinus Torvalds 			handle_flags(smi_info);
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds 			deliver_recv_msg(smi_info, msg);
6541da177e4SLinus Torvalds 		}
6551da177e4SLinus Torvalds 		break;
6561da177e4SLinus Torvalds 	}
6571da177e4SLinus Torvalds 
6581da177e4SLinus Torvalds 	case SI_GETTING_MESSAGES:
6591da177e4SLinus Torvalds 	{
6601da177e4SLinus Torvalds 		smi_info->curr_msg->rsp_size
6611da177e4SLinus Torvalds 			= smi_info->handlers->get_result(
6621da177e4SLinus Torvalds 				smi_info->si_sm,
6631da177e4SLinus Torvalds 				smi_info->curr_msg->rsp,
6641da177e4SLinus Torvalds 				IPMI_MAX_MSG_LENGTH);
6651da177e4SLinus Torvalds 
666c305e3d3SCorey Minyard 		/*
667c305e3d3SCorey Minyard 		 * Do this here becase deliver_recv_msg() releases the
668c305e3d3SCorey Minyard 		 * lock, and a new message can be put in during the
669c305e3d3SCorey Minyard 		 * time the lock is released.
670c305e3d3SCorey Minyard 		 */
6711da177e4SLinus Torvalds 		msg = smi_info->curr_msg;
6721da177e4SLinus Torvalds 		smi_info->curr_msg = NULL;
6731da177e4SLinus Torvalds 		if (msg->rsp[2] != 0) {
6741da177e4SLinus Torvalds 			/* Error getting event, probably done. */
6751da177e4SLinus Torvalds 			msg->done(msg);
6761da177e4SLinus Torvalds 
6771da177e4SLinus Torvalds 			/* Take off the msg flag. */
6781da177e4SLinus Torvalds 			smi_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
6791da177e4SLinus Torvalds 			handle_flags(smi_info);
6801da177e4SLinus Torvalds 		} else {
68164959e2dSCorey Minyard 			smi_inc_stat(smi_info, incoming_messages);
6821da177e4SLinus Torvalds 
683c305e3d3SCorey Minyard 			/*
684c305e3d3SCorey Minyard 			 * Do this before we deliver the message
685c305e3d3SCorey Minyard 			 * because delivering the message releases the
686c305e3d3SCorey Minyard 			 * lock and something else can mess with the
687c305e3d3SCorey Minyard 			 * state.
688c305e3d3SCorey Minyard 			 */
6891da177e4SLinus Torvalds 			handle_flags(smi_info);
6901da177e4SLinus Torvalds 
6911da177e4SLinus Torvalds 			deliver_recv_msg(smi_info, msg);
6921da177e4SLinus Torvalds 		}
6931da177e4SLinus Torvalds 		break;
6941da177e4SLinus Torvalds 	}
6951da177e4SLinus Torvalds 
696d9b7e4f7SCorey Minyard 	case SI_CHECKING_ENABLES:
6971da177e4SLinus Torvalds 	{
6981da177e4SLinus Torvalds 		unsigned char msg[4];
699d9b7e4f7SCorey Minyard 		u8 enables;
70095c97b59SCorey Minyard 		bool irq_on;
7011da177e4SLinus Torvalds 
7021da177e4SLinus Torvalds 		/* We got the flags from the SMI, now handle them. */
7031da177e4SLinus Torvalds 		smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
7041da177e4SLinus Torvalds 		if (msg[2] != 0) {
705910840f2SCorey Minyard 			dev_warn(smi_info->io.dev,
7060849bfecSCorey Minyard 				 "Couldn't get irq info: %x.\n", msg[2]);
707910840f2SCorey Minyard 			dev_warn(smi_info->io.dev,
7080849bfecSCorey Minyard 				 "Maybe ok, but ipmi might run very slowly.\n");
7091da177e4SLinus Torvalds 			smi_info->si_state = SI_NORMAL;
710d9b7e4f7SCorey Minyard 			break;
711d9b7e4f7SCorey Minyard 		}
71295c97b59SCorey Minyard 		enables = current_global_enables(smi_info, 0, &irq_on);
713910840f2SCorey Minyard 		if (smi_info->io.si_type == SI_BT)
71495c97b59SCorey Minyard 			/* BT has its own interrupt enable bit. */
71595c97b59SCorey Minyard 			check_bt_irq(smi_info, irq_on);
716d9b7e4f7SCorey Minyard 		if (enables != (msg[3] & GLOBAL_ENABLES_MASK)) {
717d9b7e4f7SCorey Minyard 			/* Enables are not correct, fix them. */
7181da177e4SLinus Torvalds 			msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
7191da177e4SLinus Torvalds 			msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
720d9b7e4f7SCorey Minyard 			msg[2] = enables | (msg[3] & ~GLOBAL_ENABLES_MASK);
7211da177e4SLinus Torvalds 			smi_info->handlers->start_transaction(
7221da177e4SLinus Torvalds 				smi_info->si_sm, msg, 3);
723d9b7e4f7SCorey Minyard 			smi_info->si_state = SI_SETTING_ENABLES;
724d9b7e4f7SCorey Minyard 		} else if (smi_info->supports_event_msg_buff) {
725d9b7e4f7SCorey Minyard 			smi_info->curr_msg = ipmi_alloc_smi_msg();
726d9b7e4f7SCorey Minyard 			if (!smi_info->curr_msg) {
727ee6cd5f8SCorey Minyard 				smi_info->si_state = SI_NORMAL;
728d9b7e4f7SCorey Minyard 				break;
729d9b7e4f7SCorey Minyard 			}
7305ac7b2fcSCorey Minyard 			start_getting_events(smi_info);
731ee6cd5f8SCorey Minyard 		} else {
732d9b7e4f7SCorey Minyard 			smi_info->si_state = SI_NORMAL;
733ee6cd5f8SCorey Minyard 		}
734ee6cd5f8SCorey Minyard 		break;
735ee6cd5f8SCorey Minyard 	}
736ee6cd5f8SCorey Minyard 
737d9b7e4f7SCorey Minyard 	case SI_SETTING_ENABLES:
738ee6cd5f8SCorey Minyard 	{
739ee6cd5f8SCorey Minyard 		unsigned char msg[4];
740ee6cd5f8SCorey Minyard 
741ee6cd5f8SCorey Minyard 		smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
742d9b7e4f7SCorey Minyard 		if (msg[2] != 0)
743910840f2SCorey Minyard 			dev_warn(smi_info->io.dev,
744d9b7e4f7SCorey Minyard 				 "Could not set the global enables: 0x%x.\n",
745d9b7e4f7SCorey Minyard 				 msg[2]);
746d9b7e4f7SCorey Minyard 
747d9b7e4f7SCorey Minyard 		if (smi_info->supports_event_msg_buff) {
748d9b7e4f7SCorey Minyard 			smi_info->curr_msg = ipmi_alloc_smi_msg();
749d9b7e4f7SCorey Minyard 			if (!smi_info->curr_msg) {
750ee6cd5f8SCorey Minyard 				smi_info->si_state = SI_NORMAL;
751ee6cd5f8SCorey Minyard 				break;
752ee6cd5f8SCorey Minyard 			}
7535ac7b2fcSCorey Minyard 			start_getting_events(smi_info);
754d9b7e4f7SCorey Minyard 		} else {
755d9b7e4f7SCorey Minyard 			smi_info->si_state = SI_NORMAL;
756d9b7e4f7SCorey Minyard 		}
757d9b7e4f7SCorey Minyard 		break;
758d9b7e4f7SCorey Minyard 	}
7591da177e4SLinus Torvalds 	}
7601da177e4SLinus Torvalds }
7611da177e4SLinus Torvalds 
762c305e3d3SCorey Minyard /*
763c305e3d3SCorey Minyard  * Called on timeouts and events.  Timeouts should pass the elapsed
764c305e3d3SCorey Minyard  * time, interrupts should pass in zero.  Must be called with
765c305e3d3SCorey Minyard  * si_lock held and interrupts disabled.
766c305e3d3SCorey Minyard  */
7671da177e4SLinus Torvalds static enum si_sm_result smi_event_handler(struct smi_info *smi_info,
7681da177e4SLinus Torvalds 					   int time)
7691da177e4SLinus Torvalds {
7701da177e4SLinus Torvalds 	enum si_sm_result si_sm_result;
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds restart:
773c305e3d3SCorey Minyard 	/*
774c305e3d3SCorey Minyard 	 * There used to be a loop here that waited a little while
775c305e3d3SCorey Minyard 	 * (around 25us) before giving up.  That turned out to be
776c305e3d3SCorey Minyard 	 * pointless, the minimum delays I was seeing were in the 300us
777c305e3d3SCorey Minyard 	 * range, which is far too long to wait in an interrupt.  So
778c305e3d3SCorey Minyard 	 * we just run until the state machine tells us something
779c305e3d3SCorey Minyard 	 * happened or it needs a delay.
780c305e3d3SCorey Minyard 	 */
7811da177e4SLinus Torvalds 	si_sm_result = smi_info->handlers->event(smi_info->si_sm, time);
7821da177e4SLinus Torvalds 	time = 0;
7831da177e4SLinus Torvalds 	while (si_sm_result == SI_SM_CALL_WITHOUT_DELAY)
7841da177e4SLinus Torvalds 		si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
7851da177e4SLinus Torvalds 
786c305e3d3SCorey Minyard 	if (si_sm_result == SI_SM_TRANSACTION_COMPLETE) {
78764959e2dSCorey Minyard 		smi_inc_stat(smi_info, complete_transactions);
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds 		handle_transaction_done(smi_info);
790d9dffd2aSCorey Minyard 		goto restart;
791c305e3d3SCorey Minyard 	} else if (si_sm_result == SI_SM_HOSED) {
79264959e2dSCorey Minyard 		smi_inc_stat(smi_info, hosed_count);
7931da177e4SLinus Torvalds 
794c305e3d3SCorey Minyard 		/*
795c305e3d3SCorey Minyard 		 * Do the before return_hosed_msg, because that
796c305e3d3SCorey Minyard 		 * releases the lock.
797c305e3d3SCorey Minyard 		 */
7981da177e4SLinus Torvalds 		smi_info->si_state = SI_NORMAL;
7991da177e4SLinus Torvalds 		if (smi_info->curr_msg != NULL) {
800c305e3d3SCorey Minyard 			/*
801c305e3d3SCorey Minyard 			 * If we were handling a user message, format
802c305e3d3SCorey Minyard 			 * a response to send to the upper layer to
803c305e3d3SCorey Minyard 			 * tell it about the error.
804c305e3d3SCorey Minyard 			 */
8054d7cbac7SCorey Minyard 			return_hosed_msg(smi_info, IPMI_ERR_UNSPECIFIED);
8061da177e4SLinus Torvalds 		}
807d9dffd2aSCorey Minyard 		goto restart;
8081da177e4SLinus Torvalds 	}
8091da177e4SLinus Torvalds 
8104ea18425SCorey Minyard 	/*
8114ea18425SCorey Minyard 	 * We prefer handling attn over new messages.  But don't do
8124ea18425SCorey Minyard 	 * this if there is not yet an upper layer to handle anything.
8134ea18425SCorey Minyard 	 */
814a8df150cSCorey Minyard 	if (likely(smi_info->intf) &&
815a8df150cSCorey Minyard 	    (si_sm_result == SI_SM_ATTN || smi_info->got_attn)) {
8161da177e4SLinus Torvalds 		unsigned char msg[2];
8171da177e4SLinus Torvalds 
818a8df150cSCorey Minyard 		if (smi_info->si_state != SI_NORMAL) {
819a8df150cSCorey Minyard 			/*
820a8df150cSCorey Minyard 			 * We got an ATTN, but we are doing something else.
821a8df150cSCorey Minyard 			 * Handle the ATTN later.
822a8df150cSCorey Minyard 			 */
823a8df150cSCorey Minyard 			smi_info->got_attn = true;
824a8df150cSCorey Minyard 		} else {
825a8df150cSCorey Minyard 			smi_info->got_attn = false;
82664959e2dSCorey Minyard 			smi_inc_stat(smi_info, attentions);
8271da177e4SLinus Torvalds 
828c305e3d3SCorey Minyard 			/*
829c305e3d3SCorey Minyard 			 * Got a attn, send down a get message flags to see
830c305e3d3SCorey Minyard 			 * what's causing it.  It would be better to handle
831c305e3d3SCorey Minyard 			 * this in the upper layer, but due to the way
832c305e3d3SCorey Minyard 			 * interrupts work with the SMI, that's not really
833c305e3d3SCorey Minyard 			 * possible.
834c305e3d3SCorey Minyard 			 */
8351da177e4SLinus Torvalds 			msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
8361da177e4SLinus Torvalds 			msg[1] = IPMI_GET_MSG_FLAGS_CMD;
8371da177e4SLinus Torvalds 
8380cfec916SCorey Minyard 			start_new_msg(smi_info, msg, 2);
8391da177e4SLinus Torvalds 			smi_info->si_state = SI_GETTING_FLAGS;
8401da177e4SLinus Torvalds 			goto restart;
8411da177e4SLinus Torvalds 		}
842a8df150cSCorey Minyard 	}
8431da177e4SLinus Torvalds 
8441da177e4SLinus Torvalds 	/* If we are currently idle, try to start the next message. */
8451da177e4SLinus Torvalds 	if (si_sm_result == SI_SM_IDLE) {
84664959e2dSCorey Minyard 		smi_inc_stat(smi_info, idles);
8471da177e4SLinus Torvalds 
8481da177e4SLinus Torvalds 		si_sm_result = start_next_msg(smi_info);
8491da177e4SLinus Torvalds 		if (si_sm_result != SI_SM_IDLE)
8501da177e4SLinus Torvalds 			goto restart;
8511da177e4SLinus Torvalds 	}
8521da177e4SLinus Torvalds 
8531da177e4SLinus Torvalds 	if ((si_sm_result == SI_SM_IDLE)
854c305e3d3SCorey Minyard 	    && (atomic_read(&smi_info->req_events))) {
855c305e3d3SCorey Minyard 		/*
856c305e3d3SCorey Minyard 		 * We are idle and the upper layer requested that I fetch
857c305e3d3SCorey Minyard 		 * events, so do so.
858c305e3d3SCorey Minyard 		 */
8591da177e4SLinus Torvalds 		atomic_set(&smi_info->req_events, 0);
86055162fb1SCorey Minyard 
861d9b7e4f7SCorey Minyard 		/*
862d9b7e4f7SCorey Minyard 		 * Take this opportunity to check the interrupt and
863d9b7e4f7SCorey Minyard 		 * message enable state for the BMC.  The BMC can be
864d9b7e4f7SCorey Minyard 		 * asynchronously reset, and may thus get interrupts
865d9b7e4f7SCorey Minyard 		 * disable and messages disabled.
866d9b7e4f7SCorey Minyard 		 */
867910840f2SCorey Minyard 		if (smi_info->supports_event_msg_buff || smi_info->io.irq) {
868*4f7f5551SMasamitsu Yamazaki 			start_check_enables(smi_info);
869d9b7e4f7SCorey Minyard 		} else {
870d9b7e4f7SCorey Minyard 			smi_info->curr_msg = alloc_msg_handle_irq(smi_info);
87155162fb1SCorey Minyard 			if (!smi_info->curr_msg)
87255162fb1SCorey Minyard 				goto out;
87355162fb1SCorey Minyard 
874d9b7e4f7SCorey Minyard 			start_getting_events(smi_info);
875d9b7e4f7SCorey Minyard 		}
8761da177e4SLinus Torvalds 		goto restart;
8771da177e4SLinus Torvalds 	}
878314ef52fSCorey Minyard 
879314ef52fSCorey Minyard 	if (si_sm_result == SI_SM_IDLE && smi_info->timer_running) {
880314ef52fSCorey Minyard 		/* Ok it if fails, the timer will just go off. */
881314ef52fSCorey Minyard 		if (del_timer(&smi_info->si_timer))
882314ef52fSCorey Minyard 			smi_info->timer_running = false;
883314ef52fSCorey Minyard 	}
884314ef52fSCorey Minyard 
88555162fb1SCorey Minyard out:
8861da177e4SLinus Torvalds 	return si_sm_result;
8871da177e4SLinus Torvalds }
8881da177e4SLinus Torvalds 
88989986496SCorey Minyard static void check_start_timer_thread(struct smi_info *smi_info)
89089986496SCorey Minyard {
89189986496SCorey Minyard 	if (smi_info->si_state == SI_NORMAL && smi_info->curr_msg == NULL) {
89289986496SCorey Minyard 		smi_mod_timer(smi_info, jiffies + SI_TIMEOUT_JIFFIES);
89389986496SCorey Minyard 
89489986496SCorey Minyard 		if (smi_info->thread)
89589986496SCorey Minyard 			wake_up_process(smi_info->thread);
89689986496SCorey Minyard 
89789986496SCorey Minyard 		start_next_msg(smi_info);
89889986496SCorey Minyard 		smi_event_handler(smi_info, 0);
89989986496SCorey Minyard 	}
90089986496SCorey Minyard }
90189986496SCorey Minyard 
90282802f96SHidehiro Kawai static void flush_messages(void *send_info)
903e45361d7SHidehiro Kawai {
90482802f96SHidehiro Kawai 	struct smi_info *smi_info = send_info;
905e45361d7SHidehiro Kawai 	enum si_sm_result result;
906e45361d7SHidehiro Kawai 
907e45361d7SHidehiro Kawai 	/*
908e45361d7SHidehiro Kawai 	 * Currently, this function is called only in run-to-completion
909e45361d7SHidehiro Kawai 	 * mode.  This means we are single-threaded, no need for locks.
910e45361d7SHidehiro Kawai 	 */
911e45361d7SHidehiro Kawai 	result = smi_event_handler(smi_info, 0);
912e45361d7SHidehiro Kawai 	while (result != SI_SM_IDLE) {
913e45361d7SHidehiro Kawai 		udelay(SI_SHORT_TIMEOUT_USEC);
914e45361d7SHidehiro Kawai 		result = smi_event_handler(smi_info, SI_SHORT_TIMEOUT_USEC);
915e45361d7SHidehiro Kawai 	}
916e45361d7SHidehiro Kawai }
917e45361d7SHidehiro Kawai 
9181da177e4SLinus Torvalds static void sender(void                *send_info,
91999ab32f3SCorey Minyard 		   struct ipmi_smi_msg *msg)
9201da177e4SLinus Torvalds {
9211da177e4SLinus Torvalds 	struct smi_info   *smi_info = send_info;
9221da177e4SLinus Torvalds 	unsigned long     flags;
9231da177e4SLinus Torvalds 
924f93aae9fSJohn Stultz 	debug_timestamp("Enqueue");
9251da177e4SLinus Torvalds 
9261da177e4SLinus Torvalds 	if (smi_info->run_to_completion) {
927bda4c30aSCorey Minyard 		/*
92882802f96SHidehiro Kawai 		 * If we are running to completion, start it.  Upper
92982802f96SHidehiro Kawai 		 * layer will call flush_messages to clear it out.
930bda4c30aSCorey Minyard 		 */
9319f812704SHidehiro Kawai 		smi_info->waiting_msg = msg;
9321da177e4SLinus Torvalds 		return;
9331da177e4SLinus Torvalds 	}
9341da177e4SLinus Torvalds 
935f60adf42SCorey Minyard 	spin_lock_irqsave(&smi_info->si_lock, flags);
9361d86e29bSCorey Minyard 	/*
9371d86e29bSCorey Minyard 	 * The following two lines don't need to be under the lock for
9381d86e29bSCorey Minyard 	 * the lock's sake, but they do need SMP memory barriers to
9391d86e29bSCorey Minyard 	 * avoid getting things out of order.  We are already claiming
9401d86e29bSCorey Minyard 	 * the lock, anyway, so just do it under the lock to avoid the
9411d86e29bSCorey Minyard 	 * ordering problem.
9421d86e29bSCorey Minyard 	 */
9431d86e29bSCorey Minyard 	BUG_ON(smi_info->waiting_msg);
9441d86e29bSCorey Minyard 	smi_info->waiting_msg = msg;
94589986496SCorey Minyard 	check_start_timer_thread(smi_info);
946bda4c30aSCorey Minyard 	spin_unlock_irqrestore(&smi_info->si_lock, flags);
9471da177e4SLinus Torvalds }
9481da177e4SLinus Torvalds 
9497aefac26SCorey Minyard static void set_run_to_completion(void *send_info, bool i_run_to_completion)
9501da177e4SLinus Torvalds {
9511da177e4SLinus Torvalds 	struct smi_info   *smi_info = send_info;
9521da177e4SLinus Torvalds 
9531da177e4SLinus Torvalds 	smi_info->run_to_completion = i_run_to_completion;
954e45361d7SHidehiro Kawai 	if (i_run_to_completion)
955e45361d7SHidehiro Kawai 		flush_messages(smi_info);
9561da177e4SLinus Torvalds }
9571da177e4SLinus Torvalds 
958ae74e823SMartin Wilck /*
959ae74e823SMartin Wilck  * Use -1 in the nsec value of the busy waiting timespec to tell that
960ae74e823SMartin Wilck  * we are spinning in kipmid looking for something and not delaying
961ae74e823SMartin Wilck  * between checks
962ae74e823SMartin Wilck  */
96348862ea2SJohn Stultz static inline void ipmi_si_set_not_busy(struct timespec64 *ts)
964ae74e823SMartin Wilck {
965ae74e823SMartin Wilck 	ts->tv_nsec = -1;
966ae74e823SMartin Wilck }
96748862ea2SJohn Stultz static inline int ipmi_si_is_busy(struct timespec64 *ts)
968ae74e823SMartin Wilck {
969ae74e823SMartin Wilck 	return ts->tv_nsec != -1;
970ae74e823SMartin Wilck }
971ae74e823SMartin Wilck 
972cc4cbe90SArnd Bergmann static inline int ipmi_thread_busy_wait(enum si_sm_result smi_result,
973ae74e823SMartin Wilck 					const struct smi_info *smi_info,
97448862ea2SJohn Stultz 					struct timespec64 *busy_until)
975ae74e823SMartin Wilck {
976ae74e823SMartin Wilck 	unsigned int max_busy_us = 0;
977ae74e823SMartin Wilck 
978ae74e823SMartin Wilck 	if (smi_info->intf_num < num_max_busy_us)
979ae74e823SMartin Wilck 		max_busy_us = kipmid_max_busy_us[smi_info->intf_num];
980ae74e823SMartin Wilck 	if (max_busy_us == 0 || smi_result != SI_SM_CALL_WITH_DELAY)
981ae74e823SMartin Wilck 		ipmi_si_set_not_busy(busy_until);
982ae74e823SMartin Wilck 	else if (!ipmi_si_is_busy(busy_until)) {
98348862ea2SJohn Stultz 		getnstimeofday64(busy_until);
98448862ea2SJohn Stultz 		timespec64_add_ns(busy_until, max_busy_us*NSEC_PER_USEC);
985ae74e823SMartin Wilck 	} else {
98648862ea2SJohn Stultz 		struct timespec64 now;
98748862ea2SJohn Stultz 
98848862ea2SJohn Stultz 		getnstimeofday64(&now);
98948862ea2SJohn Stultz 		if (unlikely(timespec64_compare(&now, busy_until) > 0)) {
990ae74e823SMartin Wilck 			ipmi_si_set_not_busy(busy_until);
991ae74e823SMartin Wilck 			return 0;
992ae74e823SMartin Wilck 		}
993ae74e823SMartin Wilck 	}
994ae74e823SMartin Wilck 	return 1;
995ae74e823SMartin Wilck }
996ae74e823SMartin Wilck 
997ae74e823SMartin Wilck 
998ae74e823SMartin Wilck /*
999ae74e823SMartin Wilck  * A busy-waiting loop for speeding up IPMI operation.
1000ae74e823SMartin Wilck  *
1001ae74e823SMartin Wilck  * Lousy hardware makes this hard.  This is only enabled for systems
1002ae74e823SMartin Wilck  * that are not BT and do not have interrupts.  It starts spinning
1003ae74e823SMartin Wilck  * when an operation is complete or until max_busy tells it to stop
1004ae74e823SMartin Wilck  * (if that is enabled).  See the paragraph on kimid_max_busy_us in
1005ae74e823SMartin Wilck  * Documentation/IPMI.txt for details.
1006ae74e823SMartin Wilck  */
1007a9a2c44fSCorey Minyard static int ipmi_thread(void *data)
1008a9a2c44fSCorey Minyard {
1009a9a2c44fSCorey Minyard 	struct smi_info *smi_info = data;
1010e9a705a0SMatt Domsch 	unsigned long flags;
1011a9a2c44fSCorey Minyard 	enum si_sm_result smi_result;
101248862ea2SJohn Stultz 	struct timespec64 busy_until;
1013a9a2c44fSCorey Minyard 
1014ae74e823SMartin Wilck 	ipmi_si_set_not_busy(&busy_until);
10158698a745SDongsheng Yang 	set_user_nice(current, MAX_NICE);
1016e9a705a0SMatt Domsch 	while (!kthread_should_stop()) {
1017ae74e823SMartin Wilck 		int busy_wait;
1018ae74e823SMartin Wilck 
1019a9a2c44fSCorey Minyard 		spin_lock_irqsave(&(smi_info->si_lock), flags);
1020a9a2c44fSCorey Minyard 		smi_result = smi_event_handler(smi_info, 0);
102148e8ac29SBodo Stroesser 
102248e8ac29SBodo Stroesser 		/*
102348e8ac29SBodo Stroesser 		 * If the driver is doing something, there is a possible
102448e8ac29SBodo Stroesser 		 * race with the timer.  If the timer handler see idle,
102548e8ac29SBodo Stroesser 		 * and the thread here sees something else, the timer
102648e8ac29SBodo Stroesser 		 * handler won't restart the timer even though it is
102748e8ac29SBodo Stroesser 		 * required.  So start it here if necessary.
102848e8ac29SBodo Stroesser 		 */
102948e8ac29SBodo Stroesser 		if (smi_result != SI_SM_IDLE && !smi_info->timer_running)
103048e8ac29SBodo Stroesser 			smi_mod_timer(smi_info, jiffies + SI_TIMEOUT_JIFFIES);
103148e8ac29SBodo Stroesser 
1032a9a2c44fSCorey Minyard 		spin_unlock_irqrestore(&(smi_info->si_lock), flags);
1033ae74e823SMartin Wilck 		busy_wait = ipmi_thread_busy_wait(smi_result, smi_info,
1034ae74e823SMartin Wilck 						  &busy_until);
1035c305e3d3SCorey Minyard 		if (smi_result == SI_SM_CALL_WITHOUT_DELAY)
1036c305e3d3SCorey Minyard 			; /* do nothing */
1037ae74e823SMartin Wilck 		else if (smi_result == SI_SM_CALL_WITH_DELAY && busy_wait)
103833979734Sakpm@osdl.org 			schedule();
103989986496SCorey Minyard 		else if (smi_result == SI_SM_IDLE) {
104089986496SCorey Minyard 			if (atomic_read(&smi_info->need_watch)) {
10413326f4f2SMatthew Garrett 				schedule_timeout_interruptible(100);
104289986496SCorey Minyard 			} else {
104389986496SCorey Minyard 				/* Wait to be woken up when we are needed. */
104489986496SCorey Minyard 				__set_current_state(TASK_INTERRUPTIBLE);
104589986496SCorey Minyard 				schedule();
104689986496SCorey Minyard 			}
104789986496SCorey Minyard 		} else
10488d1f66dcSMartin Wilck 			schedule_timeout_interruptible(1);
1049a9a2c44fSCorey Minyard 	}
1050a9a2c44fSCorey Minyard 	return 0;
1051a9a2c44fSCorey Minyard }
1052a9a2c44fSCorey Minyard 
1053a9a2c44fSCorey Minyard 
10541da177e4SLinus Torvalds static void poll(void *send_info)
10551da177e4SLinus Torvalds {
10561da177e4SLinus Torvalds 	struct smi_info *smi_info = send_info;
1057f60adf42SCorey Minyard 	unsigned long flags = 0;
10587aefac26SCorey Minyard 	bool run_to_completion = smi_info->run_to_completion;
10591da177e4SLinus Torvalds 
106015c62e10SCorey Minyard 	/*
106115c62e10SCorey Minyard 	 * Make sure there is some delay in the poll loop so we can
106215c62e10SCorey Minyard 	 * drive time forward and timeout things.
106315c62e10SCorey Minyard 	 */
106415c62e10SCorey Minyard 	udelay(10);
1065f60adf42SCorey Minyard 	if (!run_to_completion)
1066fcfa4724SCorey Minyard 		spin_lock_irqsave(&smi_info->si_lock, flags);
106715c62e10SCorey Minyard 	smi_event_handler(smi_info, 10);
1068f60adf42SCorey Minyard 	if (!run_to_completion)
1069fcfa4724SCorey Minyard 		spin_unlock_irqrestore(&smi_info->si_lock, flags);
10701da177e4SLinus Torvalds }
10711da177e4SLinus Torvalds 
10721da177e4SLinus Torvalds static void request_events(void *send_info)
10731da177e4SLinus Torvalds {
10741da177e4SLinus Torvalds 	struct smi_info *smi_info = send_info;
10751da177e4SLinus Torvalds 
1076b874b985SCorey Minyard 	if (!smi_info->has_event_buffer)
1077b361e27bSCorey Minyard 		return;
1078b361e27bSCorey Minyard 
10791da177e4SLinus Torvalds 	atomic_set(&smi_info->req_events, 1);
10801da177e4SLinus Torvalds }
10811da177e4SLinus Torvalds 
10827aefac26SCorey Minyard static void set_need_watch(void *send_info, bool enable)
108389986496SCorey Minyard {
108489986496SCorey Minyard 	struct smi_info *smi_info = send_info;
108589986496SCorey Minyard 	unsigned long flags;
108689986496SCorey Minyard 
108789986496SCorey Minyard 	atomic_set(&smi_info->need_watch, enable);
108889986496SCorey Minyard 	spin_lock_irqsave(&smi_info->si_lock, flags);
108989986496SCorey Minyard 	check_start_timer_thread(smi_info);
109089986496SCorey Minyard 	spin_unlock_irqrestore(&smi_info->si_lock, flags);
109189986496SCorey Minyard }
109289986496SCorey Minyard 
10931da177e4SLinus Torvalds static void smi_timeout(unsigned long data)
10941da177e4SLinus Torvalds {
10951da177e4SLinus Torvalds 	struct smi_info   *smi_info = (struct smi_info *) data;
10961da177e4SLinus Torvalds 	enum si_sm_result smi_result;
10971da177e4SLinus Torvalds 	unsigned long     flags;
10981da177e4SLinus Torvalds 	unsigned long     jiffies_now;
1099c4edff1cSCorey Minyard 	long              time_diff;
11003326f4f2SMatthew Garrett 	long		  timeout;
11011da177e4SLinus Torvalds 
11021da177e4SLinus Torvalds 	spin_lock_irqsave(&(smi_info->si_lock), flags);
1103f93aae9fSJohn Stultz 	debug_timestamp("Timer");
1104f93aae9fSJohn Stultz 
11051da177e4SLinus Torvalds 	jiffies_now = jiffies;
1106c4edff1cSCorey Minyard 	time_diff = (((long)jiffies_now - (long)smi_info->last_timeout_jiffies)
11071da177e4SLinus Torvalds 		     * SI_USEC_PER_JIFFY);
11081da177e4SLinus Torvalds 	smi_result = smi_event_handler(smi_info, time_diff);
11091da177e4SLinus Torvalds 
1110910840f2SCorey Minyard 	if ((smi_info->io.irq) && (!smi_info->interrupt_disabled)) {
11111da177e4SLinus Torvalds 		/* Running with interrupts, only do long timeouts. */
11123326f4f2SMatthew Garrett 		timeout = jiffies + SI_TIMEOUT_JIFFIES;
111364959e2dSCorey Minyard 		smi_inc_stat(smi_info, long_timeouts);
11143326f4f2SMatthew Garrett 		goto do_mod_timer;
11151da177e4SLinus Torvalds 	}
11161da177e4SLinus Torvalds 
1117c305e3d3SCorey Minyard 	/*
1118c305e3d3SCorey Minyard 	 * If the state machine asks for a short delay, then shorten
1119c305e3d3SCorey Minyard 	 * the timer timeout.
1120c305e3d3SCorey Minyard 	 */
11211da177e4SLinus Torvalds 	if (smi_result == SI_SM_CALL_WITH_DELAY) {
112264959e2dSCorey Minyard 		smi_inc_stat(smi_info, short_timeouts);
11233326f4f2SMatthew Garrett 		timeout = jiffies + 1;
11241da177e4SLinus Torvalds 	} else {
112564959e2dSCorey Minyard 		smi_inc_stat(smi_info, long_timeouts);
11263326f4f2SMatthew Garrett 		timeout = jiffies + SI_TIMEOUT_JIFFIES;
11271da177e4SLinus Torvalds 	}
11281da177e4SLinus Torvalds 
11293326f4f2SMatthew Garrett do_mod_timer:
11303326f4f2SMatthew Garrett 	if (smi_result != SI_SM_IDLE)
113148e8ac29SBodo Stroesser 		smi_mod_timer(smi_info, timeout);
113248e8ac29SBodo Stroesser 	else
113348e8ac29SBodo Stroesser 		smi_info->timer_running = false;
113448e8ac29SBodo Stroesser 	spin_unlock_irqrestore(&(smi_info->si_lock), flags);
11351da177e4SLinus Torvalds }
11361da177e4SLinus Torvalds 
11374f3e8199SCorey Minyard irqreturn_t ipmi_si_irq_handler(int irq, void *data)
11381da177e4SLinus Torvalds {
11391da177e4SLinus Torvalds 	struct smi_info *smi_info = data;
11401da177e4SLinus Torvalds 	unsigned long   flags;
11411da177e4SLinus Torvalds 
11424f3e8199SCorey Minyard 	if (smi_info->io.si_type == SI_BT)
11434f3e8199SCorey Minyard 		/* We need to clear the IRQ flag for the BT interface. */
11444f3e8199SCorey Minyard 		smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG,
11454f3e8199SCorey Minyard 				     IPMI_BT_INTMASK_CLEAR_IRQ_BIT
11464f3e8199SCorey Minyard 				     | IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
11474f3e8199SCorey Minyard 
11481da177e4SLinus Torvalds 	spin_lock_irqsave(&(smi_info->si_lock), flags);
11491da177e4SLinus Torvalds 
115064959e2dSCorey Minyard 	smi_inc_stat(smi_info, interrupts);
11511da177e4SLinus Torvalds 
1152f93aae9fSJohn Stultz 	debug_timestamp("Interrupt");
1153f93aae9fSJohn Stultz 
11541da177e4SLinus Torvalds 	smi_event_handler(smi_info, 0);
11551da177e4SLinus Torvalds 	spin_unlock_irqrestore(&(smi_info->si_lock), flags);
11561da177e4SLinus Torvalds 	return IRQ_HANDLED;
11571da177e4SLinus Torvalds }
11581da177e4SLinus Torvalds 
1159453823baSCorey Minyard static int smi_start_processing(void       *send_info,
1160453823baSCorey Minyard 				ipmi_smi_t intf)
1161453823baSCorey Minyard {
1162453823baSCorey Minyard 	struct smi_info *new_smi = send_info;
1163a51f4a81SCorey Minyard 	int             enable = 0;
1164453823baSCorey Minyard 
1165453823baSCorey Minyard 	new_smi->intf = intf;
1166453823baSCorey Minyard 
1167453823baSCorey Minyard 	/* Set up the timer that drives the interface. */
1168453823baSCorey Minyard 	setup_timer(&new_smi->si_timer, smi_timeout, (long)new_smi);
1169*4f7f5551SMasamitsu Yamazaki 	new_smi->timer_can_start = true;
117048e8ac29SBodo Stroesser 	smi_mod_timer(new_smi, jiffies + SI_TIMEOUT_JIFFIES);
1171453823baSCorey Minyard 
117227f972d3SJan Stancek 	/* Try to claim any interrupts. */
11734f3e8199SCorey Minyard 	if (new_smi->io.irq_setup) {
11744f3e8199SCorey Minyard 		new_smi->io.irq_handler_data = new_smi;
11754f3e8199SCorey Minyard 		new_smi->io.irq_setup(&new_smi->io);
11764f3e8199SCorey Minyard 	}
117727f972d3SJan Stancek 
1178df3fe8deSCorey Minyard 	/*
1179a51f4a81SCorey Minyard 	 * Check if the user forcefully enabled the daemon.
1180a51f4a81SCorey Minyard 	 */
1181a51f4a81SCorey Minyard 	if (new_smi->intf_num < num_force_kipmid)
1182a51f4a81SCorey Minyard 		enable = force_kipmid[new_smi->intf_num];
1183a51f4a81SCorey Minyard 	/*
1184df3fe8deSCorey Minyard 	 * The BT interface is efficient enough to not need a thread,
1185df3fe8deSCorey Minyard 	 * and there is no need for a thread if we have interrupts.
1186df3fe8deSCorey Minyard 	 */
1187910840f2SCorey Minyard 	else if ((new_smi->io.si_type != SI_BT) && (!new_smi->io.irq))
1188a51f4a81SCorey Minyard 		enable = 1;
1189a51f4a81SCorey Minyard 
1190a51f4a81SCorey Minyard 	if (enable) {
1191453823baSCorey Minyard 		new_smi->thread = kthread_run(ipmi_thread, new_smi,
1192453823baSCorey Minyard 					      "kipmi%d", new_smi->intf_num);
1193453823baSCorey Minyard 		if (IS_ERR(new_smi->thread)) {
1194910840f2SCorey Minyard 			dev_notice(new_smi->io.dev, "Could not start"
1195453823baSCorey Minyard 				   " kernel thread due to error %ld, only using"
1196453823baSCorey Minyard 				   " timers to drive the interface\n",
1197453823baSCorey Minyard 				   PTR_ERR(new_smi->thread));
1198453823baSCorey Minyard 			new_smi->thread = NULL;
1199453823baSCorey Minyard 		}
1200453823baSCorey Minyard 	}
1201453823baSCorey Minyard 
1202453823baSCorey Minyard 	return 0;
1203453823baSCorey Minyard }
12049dbf68f9SCorey Minyard 
120516f4232cSZhao Yakui static int get_smi_info(void *send_info, struct ipmi_smi_info *data)
120616f4232cSZhao Yakui {
120716f4232cSZhao Yakui 	struct smi_info *smi = send_info;
120816f4232cSZhao Yakui 
1209910840f2SCorey Minyard 	data->addr_src = smi->io.addr_source;
1210910840f2SCorey Minyard 	data->dev = smi->io.dev;
1211bb398a4cSCorey Minyard 	data->addr_info = smi->io.addr_info;
1212910840f2SCorey Minyard 	get_device(smi->io.dev);
121316f4232cSZhao Yakui 
121416f4232cSZhao Yakui 	return 0;
121516f4232cSZhao Yakui }
121616f4232cSZhao Yakui 
12177aefac26SCorey Minyard static void set_maintenance_mode(void *send_info, bool enable)
1218b9675136SCorey Minyard {
1219b9675136SCorey Minyard 	struct smi_info   *smi_info = send_info;
1220b9675136SCorey Minyard 
1221b9675136SCorey Minyard 	if (!enable)
1222b9675136SCorey Minyard 		atomic_set(&smi_info->req_events, 0);
1223b9675136SCorey Minyard }
1224b9675136SCorey Minyard 
122581d02b7fSCorey Minyard static const struct ipmi_smi_handlers handlers = {
12261da177e4SLinus Torvalds 	.owner                  = THIS_MODULE,
1227453823baSCorey Minyard 	.start_processing       = smi_start_processing,
122816f4232cSZhao Yakui 	.get_smi_info		= get_smi_info,
12291da177e4SLinus Torvalds 	.sender			= sender,
12301da177e4SLinus Torvalds 	.request_events		= request_events,
123189986496SCorey Minyard 	.set_need_watch		= set_need_watch,
1232b9675136SCorey Minyard 	.set_maintenance_mode   = set_maintenance_mode,
12331da177e4SLinus Torvalds 	.set_run_to_completion  = set_run_to_completion,
123482802f96SHidehiro Kawai 	.flush_messages		= flush_messages,
12351da177e4SLinus Torvalds 	.poll			= poll,
12361da177e4SLinus Torvalds };
12371da177e4SLinus Torvalds 
1238b0defcdbSCorey Minyard static LIST_HEAD(smi_infos);
1239d6dfd131SCorey Minyard static DEFINE_MUTEX(smi_infos_lock);
1240b0defcdbSCorey Minyard static int smi_num; /* Used to sequence the SMIs */
12411da177e4SLinus Torvalds 
124299ee6735SLABBE Corentin static const char * const addr_space_to_str[] = { "i/o", "mem" };
1243b361e27bSCorey Minyard 
1244a51f4a81SCorey Minyard module_param_array(force_kipmid, int, &num_force_kipmid, 0);
1245a51f4a81SCorey Minyard MODULE_PARM_DESC(force_kipmid, "Force the kipmi daemon to be enabled (1) or"
1246a51f4a81SCorey Minyard 		 " disabled(0).  Normally the IPMI driver auto-detects"
1247a51f4a81SCorey Minyard 		 " this, but the value may be overridden by this parm.");
12487aefac26SCorey Minyard module_param(unload_when_empty, bool, 0);
1249b361e27bSCorey Minyard MODULE_PARM_DESC(unload_when_empty, "Unload the module if no interfaces are"
1250b361e27bSCorey Minyard 		 " specified or found, default is 1.  Setting to 0"
1251b361e27bSCorey Minyard 		 " is useful for hot add of devices using hotmod.");
1252ae74e823SMartin Wilck module_param_array(kipmid_max_busy_us, uint, &num_max_busy_us, 0644);
1253ae74e823SMartin Wilck MODULE_PARM_DESC(kipmid_max_busy_us,
1254ae74e823SMartin Wilck 		 "Max time (in microseconds) to busy-wait for IPMI data before"
1255ae74e823SMartin Wilck 		 " sleeping. 0 (default) means to wait forever. Set to 100-500"
1256ae74e823SMartin Wilck 		 " if kipmid is using up a lot of CPU time.");
12571da177e4SLinus Torvalds 
12584f3e8199SCorey Minyard void ipmi_irq_finish_setup(struct si_sm_io *io)
12591da177e4SLinus Torvalds {
12604f3e8199SCorey Minyard 	if (io->si_type == SI_BT)
12614f3e8199SCorey Minyard 		/* Enable the interrupt in the BT interface. */
12624f3e8199SCorey Minyard 		io->outputb(io, IPMI_BT_INTMASK_REG,
12634f3e8199SCorey Minyard 			    IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
12641da177e4SLinus Torvalds }
12651da177e4SLinus Torvalds 
12664f3e8199SCorey Minyard void ipmi_irq_start_cleanup(struct si_sm_io *io)
12674f3e8199SCorey Minyard {
12684f3e8199SCorey Minyard 	if (io->si_type == SI_BT)
12694f3e8199SCorey Minyard 		/* Disable the interrupt in the BT interface. */
12704f3e8199SCorey Minyard 		io->outputb(io, IPMI_BT_INTMASK_REG, 0);
12714f3e8199SCorey Minyard }
12724f3e8199SCorey Minyard 
12734f3e8199SCorey Minyard static void std_irq_cleanup(struct si_sm_io *io)
12744f3e8199SCorey Minyard {
12754f3e8199SCorey Minyard 	ipmi_irq_start_cleanup(io);
12764f3e8199SCorey Minyard 	free_irq(io->irq, io->irq_handler_data);
12774f3e8199SCorey Minyard }
12784f3e8199SCorey Minyard 
12794f3e8199SCorey Minyard int ipmi_std_irq_setup(struct si_sm_io *io)
12801da177e4SLinus Torvalds {
12811da177e4SLinus Torvalds 	int rv;
12821da177e4SLinus Torvalds 
12834f3e8199SCorey Minyard 	if (!io->irq)
12841da177e4SLinus Torvalds 		return 0;
12851da177e4SLinus Torvalds 
12864f3e8199SCorey Minyard 	rv = request_irq(io->irq,
12874f3e8199SCorey Minyard 			 ipmi_si_irq_handler,
1288aa5b2babSMichael Opdenacker 			 IRQF_SHARED,
12899dbf68f9SCorey Minyard 			 DEVICE_NAME,
12904f3e8199SCorey Minyard 			 io->irq_handler_data);
12911da177e4SLinus Torvalds 	if (rv) {
12924f3e8199SCorey Minyard 		dev_warn(io->dev, "%s unable to claim interrupt %d,"
12931da177e4SLinus Torvalds 			 " running polled\n",
12944f3e8199SCorey Minyard 			 DEVICE_NAME, io->irq);
12954f3e8199SCorey Minyard 		io->irq = 0;
12961da177e4SLinus Torvalds 	} else {
12974f3e8199SCorey Minyard 		io->irq_cleanup = std_irq_cleanup;
12984f3e8199SCorey Minyard 		ipmi_irq_finish_setup(io);
12994f3e8199SCorey Minyard 		dev_info(io->dev, "Using irq %d\n", io->irq);
13001da177e4SLinus Torvalds 	}
13011da177e4SLinus Torvalds 
13021da177e4SLinus Torvalds 	return rv;
13031da177e4SLinus Torvalds }
13041da177e4SLinus Torvalds 
130540112ae7SCorey Minyard static int wait_for_msg_done(struct smi_info *smi_info)
13061da177e4SLinus Torvalds {
13071da177e4SLinus Torvalds 	enum si_sm_result     smi_result;
13081da177e4SLinus Torvalds 
13091da177e4SLinus Torvalds 	smi_result = smi_info->handlers->event(smi_info->si_sm, 0);
1310c305e3d3SCorey Minyard 	for (;;) {
1311c3e7e791SCorey Minyard 		if (smi_result == SI_SM_CALL_WITH_DELAY ||
1312c3e7e791SCorey Minyard 		    smi_result == SI_SM_CALL_WITH_TICK_DELAY) {
1313da4cd8dfSNishanth Aravamudan 			schedule_timeout_uninterruptible(1);
13141da177e4SLinus Torvalds 			smi_result = smi_info->handlers->event(
1315e21404dcSXie XiuQi 				smi_info->si_sm, jiffies_to_usecs(1));
1316c305e3d3SCorey Minyard 		} else if (smi_result == SI_SM_CALL_WITHOUT_DELAY) {
13171da177e4SLinus Torvalds 			smi_result = smi_info->handlers->event(
13181da177e4SLinus Torvalds 				smi_info->si_sm, 0);
1319c305e3d3SCorey Minyard 		} else
13201da177e4SLinus Torvalds 			break;
13211da177e4SLinus Torvalds 	}
132240112ae7SCorey Minyard 	if (smi_result == SI_SM_HOSED)
1323c305e3d3SCorey Minyard 		/*
1324c305e3d3SCorey Minyard 		 * We couldn't get the state machine to run, so whatever's at
1325c305e3d3SCorey Minyard 		 * the port is probably not an IPMI SMI interface.
1326c305e3d3SCorey Minyard 		 */
132740112ae7SCorey Minyard 		return -ENODEV;
132840112ae7SCorey Minyard 
132940112ae7SCorey Minyard 	return 0;
13301da177e4SLinus Torvalds }
13311da177e4SLinus Torvalds 
133240112ae7SCorey Minyard static int try_get_dev_id(struct smi_info *smi_info)
133340112ae7SCorey Minyard {
133440112ae7SCorey Minyard 	unsigned char         msg[2];
133540112ae7SCorey Minyard 	unsigned char         *resp;
133640112ae7SCorey Minyard 	unsigned long         resp_len;
133740112ae7SCorey Minyard 	int                   rv = 0;
133840112ae7SCorey Minyard 
133940112ae7SCorey Minyard 	resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
134040112ae7SCorey Minyard 	if (!resp)
134140112ae7SCorey Minyard 		return -ENOMEM;
134240112ae7SCorey Minyard 
134340112ae7SCorey Minyard 	/*
134440112ae7SCorey Minyard 	 * Do a Get Device ID command, since it comes back with some
134540112ae7SCorey Minyard 	 * useful info.
134640112ae7SCorey Minyard 	 */
134740112ae7SCorey Minyard 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
134840112ae7SCorey Minyard 	msg[1] = IPMI_GET_DEVICE_ID_CMD;
134940112ae7SCorey Minyard 	smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
135040112ae7SCorey Minyard 
135140112ae7SCorey Minyard 	rv = wait_for_msg_done(smi_info);
135240112ae7SCorey Minyard 	if (rv)
135340112ae7SCorey Minyard 		goto out;
135440112ae7SCorey Minyard 
13551da177e4SLinus Torvalds 	resp_len = smi_info->handlers->get_result(smi_info->si_sm,
13561da177e4SLinus Torvalds 						  resp, IPMI_MAX_MSG_LENGTH);
13571da177e4SLinus Torvalds 
1358d8c98618SCorey Minyard 	/* Check and record info from the get device id, in case we need it. */
1359c468f911SJeremy Kerr 	rv = ipmi_demangle_device_id(resp[0] >> 2, resp[1],
1360c468f911SJeremy Kerr 			resp + 2, resp_len - 2, &smi_info->device_id);
13611da177e4SLinus Torvalds 
13621da177e4SLinus Torvalds out:
13631da177e4SLinus Torvalds 	kfree(resp);
13641da177e4SLinus Torvalds 	return rv;
13651da177e4SLinus Torvalds }
13661da177e4SLinus Torvalds 
1367d0882897SCorey Minyard static int get_global_enables(struct smi_info *smi_info, u8 *enables)
13681e7d6a45SCorey Minyard {
13691e7d6a45SCorey Minyard 	unsigned char         msg[3];
13701e7d6a45SCorey Minyard 	unsigned char         *resp;
13711e7d6a45SCorey Minyard 	unsigned long         resp_len;
13721e7d6a45SCorey Minyard 	int                   rv;
13731e7d6a45SCorey Minyard 
13741e7d6a45SCorey Minyard 	resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1375d0882897SCorey Minyard 	if (!resp)
1376d0882897SCorey Minyard 		return -ENOMEM;
13771e7d6a45SCorey Minyard 
13781e7d6a45SCorey Minyard 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
13791e7d6a45SCorey Minyard 	msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
13801e7d6a45SCorey Minyard 	smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
13811e7d6a45SCorey Minyard 
13821e7d6a45SCorey Minyard 	rv = wait_for_msg_done(smi_info);
13831e7d6a45SCorey Minyard 	if (rv) {
1384910840f2SCorey Minyard 		dev_warn(smi_info->io.dev,
1385d0882897SCorey Minyard 			 "Error getting response from get global enables command: %d\n",
1386d0882897SCorey Minyard 			 rv);
13871e7d6a45SCorey Minyard 		goto out;
13881e7d6a45SCorey Minyard 	}
13891e7d6a45SCorey Minyard 
13901e7d6a45SCorey Minyard 	resp_len = smi_info->handlers->get_result(smi_info->si_sm,
13911e7d6a45SCorey Minyard 						  resp, IPMI_MAX_MSG_LENGTH);
13921e7d6a45SCorey Minyard 
13931e7d6a45SCorey Minyard 	if (resp_len < 4 ||
13941e7d6a45SCorey Minyard 			resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
13951e7d6a45SCorey Minyard 			resp[1] != IPMI_GET_BMC_GLOBAL_ENABLES_CMD   ||
13961e7d6a45SCorey Minyard 			resp[2] != 0) {
1397910840f2SCorey Minyard 		dev_warn(smi_info->io.dev,
1398d0882897SCorey Minyard 			 "Invalid return from get global enables command: %ld %x %x %x\n",
1399d0882897SCorey Minyard 			 resp_len, resp[0], resp[1], resp[2]);
14001e7d6a45SCorey Minyard 		rv = -EINVAL;
14011e7d6a45SCorey Minyard 		goto out;
1402d0882897SCorey Minyard 	} else {
1403d0882897SCorey Minyard 		*enables = resp[3];
14041e7d6a45SCorey Minyard 	}
14051e7d6a45SCorey Minyard 
1406d0882897SCorey Minyard out:
1407d0882897SCorey Minyard 	kfree(resp);
1408d0882897SCorey Minyard 	return rv;
1409d0882897SCorey Minyard }
1410d0882897SCorey Minyard 
1411d0882897SCorey Minyard /*
1412d0882897SCorey Minyard  * Returns 1 if it gets an error from the command.
1413d0882897SCorey Minyard  */
1414d0882897SCorey Minyard static int set_global_enables(struct smi_info *smi_info, u8 enables)
1415d0882897SCorey Minyard {
1416d0882897SCorey Minyard 	unsigned char         msg[3];
1417d0882897SCorey Minyard 	unsigned char         *resp;
1418d0882897SCorey Minyard 	unsigned long         resp_len;
1419d0882897SCorey Minyard 	int                   rv;
1420d0882897SCorey Minyard 
1421d0882897SCorey Minyard 	resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1422d0882897SCorey Minyard 	if (!resp)
1423d0882897SCorey Minyard 		return -ENOMEM;
14241e7d6a45SCorey Minyard 
14251e7d6a45SCorey Minyard 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
14261e7d6a45SCorey Minyard 	msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
1427d0882897SCorey Minyard 	msg[2] = enables;
14281e7d6a45SCorey Minyard 	smi_info->handlers->start_transaction(smi_info->si_sm, msg, 3);
14291e7d6a45SCorey Minyard 
14301e7d6a45SCorey Minyard 	rv = wait_for_msg_done(smi_info);
14311e7d6a45SCorey Minyard 	if (rv) {
1432910840f2SCorey Minyard 		dev_warn(smi_info->io.dev,
1433d0882897SCorey Minyard 			 "Error getting response from set global enables command: %d\n",
1434d0882897SCorey Minyard 			 rv);
14351e7d6a45SCorey Minyard 		goto out;
14361e7d6a45SCorey Minyard 	}
14371e7d6a45SCorey Minyard 
14381e7d6a45SCorey Minyard 	resp_len = smi_info->handlers->get_result(smi_info->si_sm,
14391e7d6a45SCorey Minyard 						  resp, IPMI_MAX_MSG_LENGTH);
14401e7d6a45SCorey Minyard 
14411e7d6a45SCorey Minyard 	if (resp_len < 3 ||
14421e7d6a45SCorey Minyard 			resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
14431e7d6a45SCorey Minyard 			resp[1] != IPMI_SET_BMC_GLOBAL_ENABLES_CMD) {
1444910840f2SCorey Minyard 		dev_warn(smi_info->io.dev,
1445d0882897SCorey Minyard 			 "Invalid return from set global enables command: %ld %x %x\n",
1446d0882897SCorey Minyard 			 resp_len, resp[0], resp[1]);
14471e7d6a45SCorey Minyard 		rv = -EINVAL;
14481e7d6a45SCorey Minyard 		goto out;
14491e7d6a45SCorey Minyard 	}
14501e7d6a45SCorey Minyard 
1451d0882897SCorey Minyard 	if (resp[2] != 0)
1452d0882897SCorey Minyard 		rv = 1;
1453d0882897SCorey Minyard 
1454d0882897SCorey Minyard out:
1455d0882897SCorey Minyard 	kfree(resp);
1456d0882897SCorey Minyard 	return rv;
1457d0882897SCorey Minyard }
1458d0882897SCorey Minyard 
1459d0882897SCorey Minyard /*
1460d0882897SCorey Minyard  * Some BMCs do not support clearing the receive irq bit in the global
1461d0882897SCorey Minyard  * enables (even if they don't support interrupts on the BMC).  Check
1462d0882897SCorey Minyard  * for this and handle it properly.
1463d0882897SCorey Minyard  */
1464d0882897SCorey Minyard static void check_clr_rcv_irq(struct smi_info *smi_info)
1465d0882897SCorey Minyard {
1466d0882897SCorey Minyard 	u8 enables = 0;
1467d0882897SCorey Minyard 	int rv;
1468d0882897SCorey Minyard 
1469d0882897SCorey Minyard 	rv = get_global_enables(smi_info, &enables);
1470d0882897SCorey Minyard 	if (!rv) {
1471d0882897SCorey Minyard 		if ((enables & IPMI_BMC_RCV_MSG_INTR) == 0)
1472d0882897SCorey Minyard 			/* Already clear, should work ok. */
1473d0882897SCorey Minyard 			return;
1474d0882897SCorey Minyard 
1475d0882897SCorey Minyard 		enables &= ~IPMI_BMC_RCV_MSG_INTR;
1476d0882897SCorey Minyard 		rv = set_global_enables(smi_info, enables);
1477d0882897SCorey Minyard 	}
1478d0882897SCorey Minyard 
1479d0882897SCorey Minyard 	if (rv < 0) {
1480910840f2SCorey Minyard 		dev_err(smi_info->io.dev,
1481d0882897SCorey Minyard 			"Cannot check clearing the rcv irq: %d\n", rv);
1482d0882897SCorey Minyard 		return;
1483d0882897SCorey Minyard 	}
1484d0882897SCorey Minyard 
1485d0882897SCorey Minyard 	if (rv) {
14861e7d6a45SCorey Minyard 		/*
14871e7d6a45SCorey Minyard 		 * An error when setting the event buffer bit means
14881e7d6a45SCorey Minyard 		 * clearing the bit is not supported.
14891e7d6a45SCorey Minyard 		 */
1490910840f2SCorey Minyard 		dev_warn(smi_info->io.dev,
1491d0882897SCorey Minyard 			 "The BMC does not support clearing the recv irq bit, compensating, but the BMC needs to be fixed.\n");
1492d0882897SCorey Minyard 		smi_info->cannot_disable_irq = true;
14931e7d6a45SCorey Minyard 	}
1494d0882897SCorey Minyard }
1495d0882897SCorey Minyard 
1496d0882897SCorey Minyard /*
1497d0882897SCorey Minyard  * Some BMCs do not support setting the interrupt bits in the global
1498d0882897SCorey Minyard  * enables even if they support interrupts.  Clearly bad, but we can
1499d0882897SCorey Minyard  * compensate.
1500d0882897SCorey Minyard  */
1501d0882897SCorey Minyard static void check_set_rcv_irq(struct smi_info *smi_info)
1502d0882897SCorey Minyard {
1503d0882897SCorey Minyard 	u8 enables = 0;
1504d0882897SCorey Minyard 	int rv;
1505d0882897SCorey Minyard 
1506910840f2SCorey Minyard 	if (!smi_info->io.irq)
1507d0882897SCorey Minyard 		return;
1508d0882897SCorey Minyard 
1509d0882897SCorey Minyard 	rv = get_global_enables(smi_info, &enables);
1510d0882897SCorey Minyard 	if (!rv) {
1511d0882897SCorey Minyard 		enables |= IPMI_BMC_RCV_MSG_INTR;
1512d0882897SCorey Minyard 		rv = set_global_enables(smi_info, enables);
1513d0882897SCorey Minyard 	}
1514d0882897SCorey Minyard 
1515d0882897SCorey Minyard 	if (rv < 0) {
1516910840f2SCorey Minyard 		dev_err(smi_info->io.dev,
1517d0882897SCorey Minyard 			"Cannot check setting the rcv irq: %d\n", rv);
1518d0882897SCorey Minyard 		return;
1519d0882897SCorey Minyard 	}
1520d0882897SCorey Minyard 
1521d0882897SCorey Minyard 	if (rv) {
1522d0882897SCorey Minyard 		/*
1523d0882897SCorey Minyard 		 * An error when setting the event buffer bit means
1524d0882897SCorey Minyard 		 * setting the bit is not supported.
1525d0882897SCorey Minyard 		 */
1526910840f2SCorey Minyard 		dev_warn(smi_info->io.dev,
1527d0882897SCorey Minyard 			 "The BMC does not support setting the recv irq bit, compensating, but the BMC needs to be fixed.\n");
1528d0882897SCorey Minyard 		smi_info->cannot_disable_irq = true;
1529d0882897SCorey Minyard 		smi_info->irq_enable_broken = true;
1530d0882897SCorey Minyard 	}
15311e7d6a45SCorey Minyard }
15321e7d6a45SCorey Minyard 
153340112ae7SCorey Minyard static int try_enable_event_buffer(struct smi_info *smi_info)
153440112ae7SCorey Minyard {
153540112ae7SCorey Minyard 	unsigned char         msg[3];
153640112ae7SCorey Minyard 	unsigned char         *resp;
153740112ae7SCorey Minyard 	unsigned long         resp_len;
153840112ae7SCorey Minyard 	int                   rv = 0;
153940112ae7SCorey Minyard 
154040112ae7SCorey Minyard 	resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
154140112ae7SCorey Minyard 	if (!resp)
154240112ae7SCorey Minyard 		return -ENOMEM;
154340112ae7SCorey Minyard 
154440112ae7SCorey Minyard 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
154540112ae7SCorey Minyard 	msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
154640112ae7SCorey Minyard 	smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
154740112ae7SCorey Minyard 
154840112ae7SCorey Minyard 	rv = wait_for_msg_done(smi_info);
154940112ae7SCorey Minyard 	if (rv) {
1550bb2a08c0SCorey Minyard 		pr_warn(PFX "Error getting response from get global enables command, the event buffer is not enabled.\n");
155140112ae7SCorey Minyard 		goto out;
155240112ae7SCorey Minyard 	}
155340112ae7SCorey Minyard 
155440112ae7SCorey Minyard 	resp_len = smi_info->handlers->get_result(smi_info->si_sm,
155540112ae7SCorey Minyard 						  resp, IPMI_MAX_MSG_LENGTH);
155640112ae7SCorey Minyard 
155740112ae7SCorey Minyard 	if (resp_len < 4 ||
155840112ae7SCorey Minyard 			resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
155940112ae7SCorey Minyard 			resp[1] != IPMI_GET_BMC_GLOBAL_ENABLES_CMD   ||
156040112ae7SCorey Minyard 			resp[2] != 0) {
1561bb2a08c0SCorey Minyard 		pr_warn(PFX "Invalid return from get global enables command, cannot enable the event buffer.\n");
156240112ae7SCorey Minyard 		rv = -EINVAL;
156340112ae7SCorey Minyard 		goto out;
156440112ae7SCorey Minyard 	}
156540112ae7SCorey Minyard 
1566d9b7e4f7SCorey Minyard 	if (resp[3] & IPMI_BMC_EVT_MSG_BUFF) {
156740112ae7SCorey Minyard 		/* buffer is already enabled, nothing to do. */
1568d9b7e4f7SCorey Minyard 		smi_info->supports_event_msg_buff = true;
156940112ae7SCorey Minyard 		goto out;
1570d9b7e4f7SCorey Minyard 	}
157140112ae7SCorey Minyard 
157240112ae7SCorey Minyard 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
157340112ae7SCorey Minyard 	msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
157440112ae7SCorey Minyard 	msg[2] = resp[3] | IPMI_BMC_EVT_MSG_BUFF;
157540112ae7SCorey Minyard 	smi_info->handlers->start_transaction(smi_info->si_sm, msg, 3);
157640112ae7SCorey Minyard 
157740112ae7SCorey Minyard 	rv = wait_for_msg_done(smi_info);
157840112ae7SCorey Minyard 	if (rv) {
1579bb2a08c0SCorey Minyard 		pr_warn(PFX "Error getting response from set global, enables command, the event buffer is not enabled.\n");
158040112ae7SCorey Minyard 		goto out;
158140112ae7SCorey Minyard 	}
158240112ae7SCorey Minyard 
158340112ae7SCorey Minyard 	resp_len = smi_info->handlers->get_result(smi_info->si_sm,
158440112ae7SCorey Minyard 						  resp, IPMI_MAX_MSG_LENGTH);
158540112ae7SCorey Minyard 
158640112ae7SCorey Minyard 	if (resp_len < 3 ||
158740112ae7SCorey Minyard 			resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
158840112ae7SCorey Minyard 			resp[1] != IPMI_SET_BMC_GLOBAL_ENABLES_CMD) {
1589bb2a08c0SCorey Minyard 		pr_warn(PFX "Invalid return from get global, enables command, not enable the event buffer.\n");
159040112ae7SCorey Minyard 		rv = -EINVAL;
159140112ae7SCorey Minyard 		goto out;
159240112ae7SCorey Minyard 	}
159340112ae7SCorey Minyard 
159440112ae7SCorey Minyard 	if (resp[2] != 0)
159540112ae7SCorey Minyard 		/*
159640112ae7SCorey Minyard 		 * An error when setting the event buffer bit means
159740112ae7SCorey Minyard 		 * that the event buffer is not supported.
159840112ae7SCorey Minyard 		 */
159940112ae7SCorey Minyard 		rv = -ENOENT;
1600d9b7e4f7SCorey Minyard 	else
1601d9b7e4f7SCorey Minyard 		smi_info->supports_event_msg_buff = true;
1602d9b7e4f7SCorey Minyard 
160340112ae7SCorey Minyard out:
160440112ae7SCorey Minyard 	kfree(resp);
160540112ae7SCorey Minyard 	return rv;
160640112ae7SCorey Minyard }
160740112ae7SCorey Minyard 
160855f91cb6SCorey Minyard #ifdef CONFIG_IPMI_PROC_INTERFACE
160907412736SAlexey Dobriyan static int smi_type_proc_show(struct seq_file *m, void *v)
16101da177e4SLinus Torvalds {
161107412736SAlexey Dobriyan 	struct smi_info *smi = m->private;
16121da177e4SLinus Torvalds 
1613910840f2SCorey Minyard 	seq_printf(m, "%s\n", si_to_str[smi->io.si_type]);
1614d6c5dc18SJoe Perches 
16155e33cd0cSJoe Perches 	return 0;
16161da177e4SLinus Torvalds }
16171da177e4SLinus Torvalds 
161807412736SAlexey Dobriyan static int smi_type_proc_open(struct inode *inode, struct file *file)
16191da177e4SLinus Torvalds {
1620d9dda78bSAl Viro 	return single_open(file, smi_type_proc_show, PDE_DATA(inode));
162107412736SAlexey Dobriyan }
16221da177e4SLinus Torvalds 
162307412736SAlexey Dobriyan static const struct file_operations smi_type_proc_ops = {
162407412736SAlexey Dobriyan 	.open		= smi_type_proc_open,
162507412736SAlexey Dobriyan 	.read		= seq_read,
162607412736SAlexey Dobriyan 	.llseek		= seq_lseek,
162707412736SAlexey Dobriyan 	.release	= single_release,
162807412736SAlexey Dobriyan };
162907412736SAlexey Dobriyan 
163007412736SAlexey Dobriyan static int smi_si_stats_proc_show(struct seq_file *m, void *v)
163107412736SAlexey Dobriyan {
163207412736SAlexey Dobriyan 	struct smi_info *smi = m->private;
163307412736SAlexey Dobriyan 
163407412736SAlexey Dobriyan 	seq_printf(m, "interrupts_enabled:    %d\n",
1635910840f2SCorey Minyard 		       smi->io.irq && !smi->interrupt_disabled);
163607412736SAlexey Dobriyan 	seq_printf(m, "short_timeouts:        %u\n",
163764959e2dSCorey Minyard 		       smi_get_stat(smi, short_timeouts));
163807412736SAlexey Dobriyan 	seq_printf(m, "long_timeouts:         %u\n",
163964959e2dSCorey Minyard 		       smi_get_stat(smi, long_timeouts));
164007412736SAlexey Dobriyan 	seq_printf(m, "idles:                 %u\n",
164164959e2dSCorey Minyard 		       smi_get_stat(smi, idles));
164207412736SAlexey Dobriyan 	seq_printf(m, "interrupts:            %u\n",
164364959e2dSCorey Minyard 		       smi_get_stat(smi, interrupts));
164407412736SAlexey Dobriyan 	seq_printf(m, "attentions:            %u\n",
164564959e2dSCorey Minyard 		       smi_get_stat(smi, attentions));
164607412736SAlexey Dobriyan 	seq_printf(m, "flag_fetches:          %u\n",
164764959e2dSCorey Minyard 		       smi_get_stat(smi, flag_fetches));
164807412736SAlexey Dobriyan 	seq_printf(m, "hosed_count:           %u\n",
164964959e2dSCorey Minyard 		       smi_get_stat(smi, hosed_count));
165007412736SAlexey Dobriyan 	seq_printf(m, "complete_transactions: %u\n",
165164959e2dSCorey Minyard 		       smi_get_stat(smi, complete_transactions));
165207412736SAlexey Dobriyan 	seq_printf(m, "events:                %u\n",
165364959e2dSCorey Minyard 		       smi_get_stat(smi, events));
165407412736SAlexey Dobriyan 	seq_printf(m, "watchdog_pretimeouts:  %u\n",
165564959e2dSCorey Minyard 		       smi_get_stat(smi, watchdog_pretimeouts));
165607412736SAlexey Dobriyan 	seq_printf(m, "incoming_messages:     %u\n",
165764959e2dSCorey Minyard 		       smi_get_stat(smi, incoming_messages));
165807412736SAlexey Dobriyan 	return 0;
1659b361e27bSCorey Minyard }
1660b361e27bSCorey Minyard 
166107412736SAlexey Dobriyan static int smi_si_stats_proc_open(struct inode *inode, struct file *file)
1662b361e27bSCorey Minyard {
1663d9dda78bSAl Viro 	return single_open(file, smi_si_stats_proc_show, PDE_DATA(inode));
166407412736SAlexey Dobriyan }
1665b361e27bSCorey Minyard 
166607412736SAlexey Dobriyan static const struct file_operations smi_si_stats_proc_ops = {
166707412736SAlexey Dobriyan 	.open		= smi_si_stats_proc_open,
166807412736SAlexey Dobriyan 	.read		= seq_read,
166907412736SAlexey Dobriyan 	.llseek		= seq_lseek,
167007412736SAlexey Dobriyan 	.release	= single_release,
167107412736SAlexey Dobriyan };
167207412736SAlexey Dobriyan 
167307412736SAlexey Dobriyan static int smi_params_proc_show(struct seq_file *m, void *v)
167407412736SAlexey Dobriyan {
167507412736SAlexey Dobriyan 	struct smi_info *smi = m->private;
167607412736SAlexey Dobriyan 
1677d6c5dc18SJoe Perches 	seq_printf(m,
1678b361e27bSCorey Minyard 		   "%s,%s,0x%lx,rsp=%d,rsi=%d,rsh=%d,irq=%d,ipmb=%d\n",
1679910840f2SCorey Minyard 		   si_to_str[smi->io.si_type],
1680b361e27bSCorey Minyard 		   addr_space_to_str[smi->io.addr_type],
1681b361e27bSCorey Minyard 		   smi->io.addr_data,
1682b361e27bSCorey Minyard 		   smi->io.regspacing,
1683b361e27bSCorey Minyard 		   smi->io.regsize,
1684b361e27bSCorey Minyard 		   smi->io.regshift,
1685910840f2SCorey Minyard 		   smi->io.irq,
1686910840f2SCorey Minyard 		   smi->io.slave_addr);
1687d6c5dc18SJoe Perches 
16885e33cd0cSJoe Perches 	return 0;
16891da177e4SLinus Torvalds }
16901da177e4SLinus Torvalds 
169107412736SAlexey Dobriyan static int smi_params_proc_open(struct inode *inode, struct file *file)
169207412736SAlexey Dobriyan {
1693d9dda78bSAl Viro 	return single_open(file, smi_params_proc_show, PDE_DATA(inode));
169407412736SAlexey Dobriyan }
169507412736SAlexey Dobriyan 
169607412736SAlexey Dobriyan static const struct file_operations smi_params_proc_ops = {
169707412736SAlexey Dobriyan 	.open		= smi_params_proc_open,
169807412736SAlexey Dobriyan 	.read		= seq_read,
169907412736SAlexey Dobriyan 	.llseek		= seq_lseek,
170007412736SAlexey Dobriyan 	.release	= single_release,
170107412736SAlexey Dobriyan };
170255f91cb6SCorey Minyard #endif
170307412736SAlexey Dobriyan 
17043dd377b5SCorey Minyard #define IPMI_SI_ATTR(name) \
17053dd377b5SCorey Minyard static ssize_t ipmi_##name##_show(struct device *dev,			\
17063dd377b5SCorey Minyard 				  struct device_attribute *attr,	\
17073dd377b5SCorey Minyard 				  char *buf)				\
17083dd377b5SCorey Minyard {									\
17093dd377b5SCorey Minyard 	struct smi_info *smi_info = dev_get_drvdata(dev);		\
17103dd377b5SCorey Minyard 									\
17113dd377b5SCorey Minyard 	return snprintf(buf, 10, "%u\n", smi_get_stat(smi_info, name));	\
17123dd377b5SCorey Minyard }									\
17133dd377b5SCorey Minyard static DEVICE_ATTR(name, S_IRUGO, ipmi_##name##_show, NULL)
17143dd377b5SCorey Minyard 
17153dd377b5SCorey Minyard static ssize_t ipmi_type_show(struct device *dev,
17163dd377b5SCorey Minyard 			      struct device_attribute *attr,
17173dd377b5SCorey Minyard 			      char *buf)
17183dd377b5SCorey Minyard {
17193dd377b5SCorey Minyard 	struct smi_info *smi_info = dev_get_drvdata(dev);
17203dd377b5SCorey Minyard 
17213dd377b5SCorey Minyard 	return snprintf(buf, 10, "%s\n", si_to_str[smi_info->io.si_type]);
17223dd377b5SCorey Minyard }
17233dd377b5SCorey Minyard static DEVICE_ATTR(type, S_IRUGO, ipmi_type_show, NULL);
17243dd377b5SCorey Minyard 
17253dd377b5SCorey Minyard static ssize_t ipmi_interrupts_enabled_show(struct device *dev,
17263dd377b5SCorey Minyard 					    struct device_attribute *attr,
17273dd377b5SCorey Minyard 					    char *buf)
17283dd377b5SCorey Minyard {
17293dd377b5SCorey Minyard 	struct smi_info *smi_info = dev_get_drvdata(dev);
17303dd377b5SCorey Minyard 	int enabled = smi_info->io.irq && !smi_info->interrupt_disabled;
17313dd377b5SCorey Minyard 
17323dd377b5SCorey Minyard 	return snprintf(buf, 10, "%d\n", enabled);
17333dd377b5SCorey Minyard }
17343dd377b5SCorey Minyard static DEVICE_ATTR(interrupts_enabled, S_IRUGO,
17353dd377b5SCorey Minyard 		   ipmi_interrupts_enabled_show, NULL);
17363dd377b5SCorey Minyard 
17373dd377b5SCorey Minyard IPMI_SI_ATTR(short_timeouts);
17383dd377b5SCorey Minyard IPMI_SI_ATTR(long_timeouts);
17393dd377b5SCorey Minyard IPMI_SI_ATTR(idles);
17403dd377b5SCorey Minyard IPMI_SI_ATTR(interrupts);
17413dd377b5SCorey Minyard IPMI_SI_ATTR(attentions);
17423dd377b5SCorey Minyard IPMI_SI_ATTR(flag_fetches);
17433dd377b5SCorey Minyard IPMI_SI_ATTR(hosed_count);
17443dd377b5SCorey Minyard IPMI_SI_ATTR(complete_transactions);
17453dd377b5SCorey Minyard IPMI_SI_ATTR(events);
17463dd377b5SCorey Minyard IPMI_SI_ATTR(watchdog_pretimeouts);
17473dd377b5SCorey Minyard IPMI_SI_ATTR(incoming_messages);
17483dd377b5SCorey Minyard 
17493dd377b5SCorey Minyard static ssize_t ipmi_params_show(struct device *dev,
17503dd377b5SCorey Minyard 				struct device_attribute *attr,
17513dd377b5SCorey Minyard 				char *buf)
17523dd377b5SCorey Minyard {
17533dd377b5SCorey Minyard 	struct smi_info *smi_info = dev_get_drvdata(dev);
17543dd377b5SCorey Minyard 
17553dd377b5SCorey Minyard 	return snprintf(buf, 200,
17563dd377b5SCorey Minyard 			"%s,%s,0x%lx,rsp=%d,rsi=%d,rsh=%d,irq=%d,ipmb=%d\n",
17573dd377b5SCorey Minyard 			si_to_str[smi_info->io.si_type],
17583dd377b5SCorey Minyard 			addr_space_to_str[smi_info->io.addr_type],
17593dd377b5SCorey Minyard 			smi_info->io.addr_data,
17603dd377b5SCorey Minyard 			smi_info->io.regspacing,
17613dd377b5SCorey Minyard 			smi_info->io.regsize,
17623dd377b5SCorey Minyard 			smi_info->io.regshift,
17633dd377b5SCorey Minyard 			smi_info->io.irq,
17643dd377b5SCorey Minyard 			smi_info->io.slave_addr);
17653dd377b5SCorey Minyard }
17663dd377b5SCorey Minyard static DEVICE_ATTR(params, S_IRUGO, ipmi_params_show, NULL);
17673dd377b5SCorey Minyard 
17683dd377b5SCorey Minyard static struct attribute *ipmi_si_dev_attrs[] = {
17693dd377b5SCorey Minyard 	&dev_attr_type.attr,
17703dd377b5SCorey Minyard 	&dev_attr_interrupts_enabled.attr,
17713dd377b5SCorey Minyard 	&dev_attr_short_timeouts.attr,
17723dd377b5SCorey Minyard 	&dev_attr_long_timeouts.attr,
17733dd377b5SCorey Minyard 	&dev_attr_idles.attr,
17743dd377b5SCorey Minyard 	&dev_attr_interrupts.attr,
17753dd377b5SCorey Minyard 	&dev_attr_attentions.attr,
17763dd377b5SCorey Minyard 	&dev_attr_flag_fetches.attr,
17773dd377b5SCorey Minyard 	&dev_attr_hosed_count.attr,
17783dd377b5SCorey Minyard 	&dev_attr_complete_transactions.attr,
17793dd377b5SCorey Minyard 	&dev_attr_events.attr,
17803dd377b5SCorey Minyard 	&dev_attr_watchdog_pretimeouts.attr,
17813dd377b5SCorey Minyard 	&dev_attr_incoming_messages.attr,
17823dd377b5SCorey Minyard 	&dev_attr_params.attr,
17833dd377b5SCorey Minyard 	NULL
17843dd377b5SCorey Minyard };
17853dd377b5SCorey Minyard 
17863dd377b5SCorey Minyard static const struct attribute_group ipmi_si_dev_attr_group = {
17873dd377b5SCorey Minyard 	.attrs		= ipmi_si_dev_attrs,
17883dd377b5SCorey Minyard };
17893dd377b5SCorey Minyard 
17903ae0e0f9SCorey Minyard /*
17913ae0e0f9SCorey Minyard  * oem_data_avail_to_receive_msg_avail
17923ae0e0f9SCorey Minyard  * @info - smi_info structure with msg_flags set
17933ae0e0f9SCorey Minyard  *
17943ae0e0f9SCorey Minyard  * Converts flags from OEM_DATA_AVAIL to RECEIVE_MSG_AVAIL
17953ae0e0f9SCorey Minyard  * Returns 1 indicating need to re-run handle_flags().
17963ae0e0f9SCorey Minyard  */
17973ae0e0f9SCorey Minyard static int oem_data_avail_to_receive_msg_avail(struct smi_info *smi_info)
17983ae0e0f9SCorey Minyard {
1799e8b33617SCorey Minyard 	smi_info->msg_flags = ((smi_info->msg_flags & ~OEM_DATA_AVAIL) |
1800e8b33617SCorey Minyard 			       RECEIVE_MSG_AVAIL);
18013ae0e0f9SCorey Minyard 	return 1;
18023ae0e0f9SCorey Minyard }
18033ae0e0f9SCorey Minyard 
18043ae0e0f9SCorey Minyard /*
18053ae0e0f9SCorey Minyard  * setup_dell_poweredge_oem_data_handler
18063ae0e0f9SCorey Minyard  * @info - smi_info.device_id must be populated
18073ae0e0f9SCorey Minyard  *
18083ae0e0f9SCorey Minyard  * Systems that match, but have firmware version < 1.40 may assert
18093ae0e0f9SCorey Minyard  * OEM0_DATA_AVAIL on their own, without being told via Set Flags that
18103ae0e0f9SCorey Minyard  * it's safe to do so.  Such systems will de-assert OEM1_DATA_AVAIL
18113ae0e0f9SCorey Minyard  * upon receipt of IPMI_GET_MSG_CMD, so we should treat these flags
18123ae0e0f9SCorey Minyard  * as RECEIVE_MSG_AVAIL instead.
18133ae0e0f9SCorey Minyard  *
18143ae0e0f9SCorey Minyard  * As Dell has no plans to release IPMI 1.5 firmware that *ever*
18153ae0e0f9SCorey Minyard  * assert the OEM[012] bits, and if it did, the driver would have to
18163ae0e0f9SCorey Minyard  * change to handle that properly, we don't actually check for the
18173ae0e0f9SCorey Minyard  * firmware version.
18183ae0e0f9SCorey Minyard  * Device ID = 0x20                BMC on PowerEdge 8G servers
18193ae0e0f9SCorey Minyard  * Device Revision = 0x80
18203ae0e0f9SCorey Minyard  * Firmware Revision1 = 0x01       BMC version 1.40
18213ae0e0f9SCorey Minyard  * Firmware Revision2 = 0x40       BCD encoded
18223ae0e0f9SCorey Minyard  * IPMI Version = 0x51             IPMI 1.5
18233ae0e0f9SCorey Minyard  * Manufacturer ID = A2 02 00      Dell IANA
18243ae0e0f9SCorey Minyard  *
1825d5a2b89aSCorey Minyard  * Additionally, PowerEdge systems with IPMI < 1.5 may also assert
1826d5a2b89aSCorey Minyard  * OEM0_DATA_AVAIL and needs to be treated as RECEIVE_MSG_AVAIL.
1827d5a2b89aSCorey Minyard  *
18283ae0e0f9SCorey Minyard  */
18293ae0e0f9SCorey Minyard #define DELL_POWEREDGE_8G_BMC_DEVICE_ID  0x20
18303ae0e0f9SCorey Minyard #define DELL_POWEREDGE_8G_BMC_DEVICE_REV 0x80
18313ae0e0f9SCorey Minyard #define DELL_POWEREDGE_8G_BMC_IPMI_VERSION 0x51
183250c812b2SCorey Minyard #define DELL_IANA_MFR_ID 0x0002a2
18333ae0e0f9SCorey Minyard static void setup_dell_poweredge_oem_data_handler(struct smi_info *smi_info)
18343ae0e0f9SCorey Minyard {
18353ae0e0f9SCorey Minyard 	struct ipmi_device_id *id = &smi_info->device_id;
183650c812b2SCorey Minyard 	if (id->manufacturer_id == DELL_IANA_MFR_ID) {
1837d5a2b89aSCorey Minyard 		if (id->device_id       == DELL_POWEREDGE_8G_BMC_DEVICE_ID  &&
1838d5a2b89aSCorey Minyard 		    id->device_revision == DELL_POWEREDGE_8G_BMC_DEVICE_REV &&
1839d5a2b89aSCorey Minyard 		    id->ipmi_version   == DELL_POWEREDGE_8G_BMC_IPMI_VERSION) {
18403ae0e0f9SCorey Minyard 			smi_info->oem_data_avail_handler =
18413ae0e0f9SCorey Minyard 				oem_data_avail_to_receive_msg_avail;
1842c305e3d3SCorey Minyard 		} else if (ipmi_version_major(id) < 1 ||
1843d5a2b89aSCorey Minyard 			   (ipmi_version_major(id) == 1 &&
1844d5a2b89aSCorey Minyard 			    ipmi_version_minor(id) < 5)) {
1845d5a2b89aSCorey Minyard 			smi_info->oem_data_avail_handler =
1846d5a2b89aSCorey Minyard 				oem_data_avail_to_receive_msg_avail;
1847d5a2b89aSCorey Minyard 		}
1848d5a2b89aSCorey Minyard 	}
18493ae0e0f9SCorey Minyard }
18503ae0e0f9SCorey Minyard 
1851ea94027bSCorey Minyard #define CANNOT_RETURN_REQUESTED_LENGTH 0xCA
1852ea94027bSCorey Minyard static void return_hosed_msg_badsize(struct smi_info *smi_info)
1853ea94027bSCorey Minyard {
1854ea94027bSCorey Minyard 	struct ipmi_smi_msg *msg = smi_info->curr_msg;
1855ea94027bSCorey Minyard 
185625985edcSLucas De Marchi 	/* Make it a response */
1857ea94027bSCorey Minyard 	msg->rsp[0] = msg->data[0] | 4;
1858ea94027bSCorey Minyard 	msg->rsp[1] = msg->data[1];
1859ea94027bSCorey Minyard 	msg->rsp[2] = CANNOT_RETURN_REQUESTED_LENGTH;
1860ea94027bSCorey Minyard 	msg->rsp_size = 3;
1861ea94027bSCorey Minyard 	smi_info->curr_msg = NULL;
1862ea94027bSCorey Minyard 	deliver_recv_msg(smi_info, msg);
1863ea94027bSCorey Minyard }
1864ea94027bSCorey Minyard 
1865ea94027bSCorey Minyard /*
1866ea94027bSCorey Minyard  * dell_poweredge_bt_xaction_handler
1867ea94027bSCorey Minyard  * @info - smi_info.device_id must be populated
1868ea94027bSCorey Minyard  *
1869ea94027bSCorey Minyard  * Dell PowerEdge servers with the BT interface (x6xx and 1750) will
1870ea94027bSCorey Minyard  * not respond to a Get SDR command if the length of the data
1871ea94027bSCorey Minyard  * requested is exactly 0x3A, which leads to command timeouts and no
1872ea94027bSCorey Minyard  * data returned.  This intercepts such commands, and causes userspace
1873ea94027bSCorey Minyard  * callers to try again with a different-sized buffer, which succeeds.
1874ea94027bSCorey Minyard  */
1875ea94027bSCorey Minyard 
1876ea94027bSCorey Minyard #define STORAGE_NETFN 0x0A
1877ea94027bSCorey Minyard #define STORAGE_CMD_GET_SDR 0x23
1878ea94027bSCorey Minyard static int dell_poweredge_bt_xaction_handler(struct notifier_block *self,
1879ea94027bSCorey Minyard 					     unsigned long unused,
1880ea94027bSCorey Minyard 					     void *in)
1881ea94027bSCorey Minyard {
1882ea94027bSCorey Minyard 	struct smi_info *smi_info = in;
1883ea94027bSCorey Minyard 	unsigned char *data = smi_info->curr_msg->data;
1884ea94027bSCorey Minyard 	unsigned int size   = smi_info->curr_msg->data_size;
1885ea94027bSCorey Minyard 	if (size >= 8 &&
1886ea94027bSCorey Minyard 	    (data[0]>>2) == STORAGE_NETFN &&
1887ea94027bSCorey Minyard 	    data[1] == STORAGE_CMD_GET_SDR &&
1888ea94027bSCorey Minyard 	    data[7] == 0x3A) {
1889ea94027bSCorey Minyard 		return_hosed_msg_badsize(smi_info);
1890ea94027bSCorey Minyard 		return NOTIFY_STOP;
1891ea94027bSCorey Minyard 	}
1892ea94027bSCorey Minyard 	return NOTIFY_DONE;
1893ea94027bSCorey Minyard }
1894ea94027bSCorey Minyard 
1895ea94027bSCorey Minyard static struct notifier_block dell_poweredge_bt_xaction_notifier = {
1896ea94027bSCorey Minyard 	.notifier_call	= dell_poweredge_bt_xaction_handler,
1897ea94027bSCorey Minyard };
1898ea94027bSCorey Minyard 
1899ea94027bSCorey Minyard /*
1900ea94027bSCorey Minyard  * setup_dell_poweredge_bt_xaction_handler
1901ea94027bSCorey Minyard  * @info - smi_info.device_id must be filled in already
1902ea94027bSCorey Minyard  *
1903ea94027bSCorey Minyard  * Fills in smi_info.device_id.start_transaction_pre_hook
1904ea94027bSCorey Minyard  * when we know what function to use there.
1905ea94027bSCorey Minyard  */
1906ea94027bSCorey Minyard static void
1907ea94027bSCorey Minyard setup_dell_poweredge_bt_xaction_handler(struct smi_info *smi_info)
1908ea94027bSCorey Minyard {
1909ea94027bSCorey Minyard 	struct ipmi_device_id *id = &smi_info->device_id;
191050c812b2SCorey Minyard 	if (id->manufacturer_id == DELL_IANA_MFR_ID &&
1911910840f2SCorey Minyard 	    smi_info->io.si_type == SI_BT)
1912ea94027bSCorey Minyard 		register_xaction_notifier(&dell_poweredge_bt_xaction_notifier);
1913ea94027bSCorey Minyard }
1914ea94027bSCorey Minyard 
19153ae0e0f9SCorey Minyard /*
19163ae0e0f9SCorey Minyard  * setup_oem_data_handler
19173ae0e0f9SCorey Minyard  * @info - smi_info.device_id must be filled in already
19183ae0e0f9SCorey Minyard  *
19193ae0e0f9SCorey Minyard  * Fills in smi_info.device_id.oem_data_available_handler
19203ae0e0f9SCorey Minyard  * when we know what function to use there.
19213ae0e0f9SCorey Minyard  */
19223ae0e0f9SCorey Minyard 
19233ae0e0f9SCorey Minyard static void setup_oem_data_handler(struct smi_info *smi_info)
19243ae0e0f9SCorey Minyard {
19253ae0e0f9SCorey Minyard 	setup_dell_poweredge_oem_data_handler(smi_info);
19263ae0e0f9SCorey Minyard }
19273ae0e0f9SCorey Minyard 
1928ea94027bSCorey Minyard static void setup_xaction_handlers(struct smi_info *smi_info)
1929ea94027bSCorey Minyard {
1930ea94027bSCorey Minyard 	setup_dell_poweredge_bt_xaction_handler(smi_info);
1931ea94027bSCorey Minyard }
1932ea94027bSCorey Minyard 
1933d0882897SCorey Minyard static void check_for_broken_irqs(struct smi_info *smi_info)
1934d0882897SCorey Minyard {
1935d0882897SCorey Minyard 	check_clr_rcv_irq(smi_info);
1936d0882897SCorey Minyard 	check_set_rcv_irq(smi_info);
1937d0882897SCorey Minyard }
1938d0882897SCorey Minyard 
1939*4f7f5551SMasamitsu Yamazaki static inline void stop_timer_and_thread(struct smi_info *smi_info)
1940a9a2c44fSCorey Minyard {
1941453823baSCorey Minyard 	if (smi_info->thread != NULL)
1942e9a705a0SMatt Domsch 		kthread_stop(smi_info->thread);
1943*4f7f5551SMasamitsu Yamazaki 
1944*4f7f5551SMasamitsu Yamazaki 	smi_info->timer_can_start = false;
1945b874b985SCorey Minyard 	if (smi_info->timer_running)
1946a9a2c44fSCorey Minyard 		del_timer_sync(&smi_info->si_timer);
1947a9a2c44fSCorey Minyard }
1948a9a2c44fSCorey Minyard 
19497e030d6dSCorey Minyard static struct smi_info *find_dup_si(struct smi_info *info)
1950b0defcdbSCorey Minyard {
1951b0defcdbSCorey Minyard 	struct smi_info *e;
1952b0defcdbSCorey Minyard 
1953b0defcdbSCorey Minyard 	list_for_each_entry(e, &smi_infos, link) {
1954b0defcdbSCorey Minyard 		if (e->io.addr_type != info->io.addr_type)
1955b0defcdbSCorey Minyard 			continue;
195694671710SCorey Minyard 		if (e->io.addr_data == info->io.addr_data) {
195794671710SCorey Minyard 			/*
195894671710SCorey Minyard 			 * This is a cheap hack, ACPI doesn't have a defined
195994671710SCorey Minyard 			 * slave address but SMBIOS does.  Pick it up from
196094671710SCorey Minyard 			 * any source that has it available.
196194671710SCorey Minyard 			 */
1962910840f2SCorey Minyard 			if (info->io.slave_addr && !e->io.slave_addr)
1963910840f2SCorey Minyard 				e->io.slave_addr = info->io.slave_addr;
19647e030d6dSCorey Minyard 			return e;
1965b0defcdbSCorey Minyard 		}
196694671710SCorey Minyard 	}
1967b0defcdbSCorey Minyard 
19687e030d6dSCorey Minyard 	return NULL;
1969b0defcdbSCorey Minyard }
1970b0defcdbSCorey Minyard 
1971bb398a4cSCorey Minyard int ipmi_si_add_smi(struct si_sm_io *io)
19722407d77aSMatthew Garrett {
19732407d77aSMatthew Garrett 	int rv = 0;
1974bb398a4cSCorey Minyard 	struct smi_info *new_smi, *dup;
19752407d77aSMatthew Garrett 
1976bb398a4cSCorey Minyard 	if (!io->io_setup) {
1977bb398a4cSCorey Minyard 		if (io->addr_type == IPMI_IO_ADDR_SPACE) {
197858e27635SCorey Minyard 			io->io_setup = ipmi_si_port_setup;
1979bb398a4cSCorey Minyard 		} else if (io->addr_type == IPMI_MEM_ADDR_SPACE) {
198058e27635SCorey Minyard 			io->io_setup = ipmi_si_mem_setup;
1981e1eeb7f8SCorey Minyard 		} else {
1982e1eeb7f8SCorey Minyard 			return -EINVAL;
1983e1eeb7f8SCorey Minyard 		}
1984e1eeb7f8SCorey Minyard 	}
1985e1eeb7f8SCorey Minyard 
198667f4fb02SCorey Minyard 	new_smi = kzalloc(sizeof(*new_smi), GFP_KERNEL);
1987bb398a4cSCorey Minyard 	if (!new_smi)
1988bb398a4cSCorey Minyard 		return -ENOMEM;
198967f4fb02SCorey Minyard 	spin_lock_init(&new_smi->si_lock);
1990bb398a4cSCorey Minyard 
1991bb398a4cSCorey Minyard 	new_smi->io = *io;
1992bb398a4cSCorey Minyard 
19932407d77aSMatthew Garrett 	mutex_lock(&smi_infos_lock);
19947e030d6dSCorey Minyard 	dup = find_dup_si(new_smi);
19957e030d6dSCorey Minyard 	if (dup) {
1996910840f2SCorey Minyard 		if (new_smi->io.addr_source == SI_ACPI &&
1997910840f2SCorey Minyard 		    dup->io.addr_source == SI_SMBIOS) {
19987e030d6dSCorey Minyard 			/* We prefer ACPI over SMBIOS. */
1999910840f2SCorey Minyard 			dev_info(dup->io.dev,
20007e030d6dSCorey Minyard 				 "Removing SMBIOS-specified %s state machine in favor of ACPI\n",
2001910840f2SCorey Minyard 				 si_to_str[new_smi->io.si_type]);
20027e030d6dSCorey Minyard 			cleanup_one_si(dup);
20037e030d6dSCorey Minyard 		} else {
2004910840f2SCorey Minyard 			dev_info(new_smi->io.dev,
20057e030d6dSCorey Minyard 				 "%s-specified %s state machine: duplicate\n",
2006910840f2SCorey Minyard 				 ipmi_addr_src_to_str(new_smi->io.addr_source),
2007910840f2SCorey Minyard 				 si_to_str[new_smi->io.si_type]);
20082407d77aSMatthew Garrett 			rv = -EBUSY;
2009c0a32fe1SColin Ian King 			kfree(new_smi);
20102407d77aSMatthew Garrett 			goto out_err;
20112407d77aSMatthew Garrett 		}
20127e030d6dSCorey Minyard 	}
20132407d77aSMatthew Garrett 
2014bb2a08c0SCorey Minyard 	pr_info(PFX "Adding %s-specified %s state machine\n",
2015910840f2SCorey Minyard 		ipmi_addr_src_to_str(new_smi->io.addr_source),
2016910840f2SCorey Minyard 		si_to_str[new_smi->io.si_type]);
20172407d77aSMatthew Garrett 
20182407d77aSMatthew Garrett 	/* So we know not to free it unless we have allocated one. */
20192407d77aSMatthew Garrett 	new_smi->intf = NULL;
20202407d77aSMatthew Garrett 	new_smi->si_sm = NULL;
20212407d77aSMatthew Garrett 	new_smi->handlers = NULL;
20222407d77aSMatthew Garrett 
20232407d77aSMatthew Garrett 	list_add_tail(&new_smi->link, &smi_infos);
20242407d77aSMatthew Garrett 
2025bb398a4cSCorey Minyard 	if (initialized) {
2026bb398a4cSCorey Minyard 		rv = try_smi_init(new_smi);
2027bb398a4cSCorey Minyard 		if (rv) {
2028bb398a4cSCorey Minyard 			mutex_unlock(&smi_infos_lock);
2029bb398a4cSCorey Minyard 			cleanup_one_si(new_smi);
2030bb398a4cSCorey Minyard 			return rv;
2031bb398a4cSCorey Minyard 		}
2032bb398a4cSCorey Minyard 	}
20332407d77aSMatthew Garrett out_err:
20342407d77aSMatthew Garrett 	mutex_unlock(&smi_infos_lock);
20352407d77aSMatthew Garrett 	return rv;
20362407d77aSMatthew Garrett }
20372407d77aSMatthew Garrett 
20383f724c40STony Camuso /*
20393f724c40STony Camuso  * Try to start up an interface.  Must be called with smi_infos_lock
20403f724c40STony Camuso  * held, primarily to keep smi_num consistent, we only one to do these
20413f724c40STony Camuso  * one at a time.
20423f724c40STony Camuso  */
2043b0defcdbSCorey Minyard static int try_smi_init(struct smi_info *new_smi)
20441da177e4SLinus Torvalds {
20452407d77aSMatthew Garrett 	int rv = 0;
204664959e2dSCorey Minyard 	int i;
20471abf71eeSCorey Minyard 	char *init_name = NULL;
20481da177e4SLinus Torvalds 
2049bb2a08c0SCorey Minyard 	pr_info(PFX "Trying %s-specified %s state machine at %s address 0x%lx, slave address 0x%x, irq %d\n",
2050910840f2SCorey Minyard 		ipmi_addr_src_to_str(new_smi->io.addr_source),
2051910840f2SCorey Minyard 		si_to_str[new_smi->io.si_type],
2052b0defcdbSCorey Minyard 		addr_space_to_str[new_smi->io.addr_type],
2053b0defcdbSCorey Minyard 		new_smi->io.addr_data,
2054910840f2SCorey Minyard 		new_smi->io.slave_addr, new_smi->io.irq);
20551da177e4SLinus Torvalds 
2056910840f2SCorey Minyard 	switch (new_smi->io.si_type) {
2057b0defcdbSCorey Minyard 	case SI_KCS:
20581da177e4SLinus Torvalds 		new_smi->handlers = &kcs_smi_handlers;
2059b0defcdbSCorey Minyard 		break;
2060b0defcdbSCorey Minyard 
2061b0defcdbSCorey Minyard 	case SI_SMIC:
20621da177e4SLinus Torvalds 		new_smi->handlers = &smic_smi_handlers;
2063b0defcdbSCorey Minyard 		break;
2064b0defcdbSCorey Minyard 
2065b0defcdbSCorey Minyard 	case SI_BT:
20661da177e4SLinus Torvalds 		new_smi->handlers = &bt_smi_handlers;
2067b0defcdbSCorey Minyard 		break;
2068b0defcdbSCorey Minyard 
2069b0defcdbSCorey Minyard 	default:
20701da177e4SLinus Torvalds 		/* No support for anything else yet. */
20711da177e4SLinus Torvalds 		rv = -EIO;
20721da177e4SLinus Torvalds 		goto out_err;
20731da177e4SLinus Torvalds 	}
20741da177e4SLinus Torvalds 
20753f724c40STony Camuso 	new_smi->intf_num = smi_num;
20763f724c40STony Camuso 
20771abf71eeSCorey Minyard 	/* Do this early so it's available for logs. */
2078910840f2SCorey Minyard 	if (!new_smi->io.dev) {
20793f724c40STony Camuso 		init_name = kasprintf(GFP_KERNEL, "ipmi_si.%d",
20803f724c40STony Camuso 				      new_smi->intf_num);
20811abf71eeSCorey Minyard 
20821abf71eeSCorey Minyard 		/*
20831abf71eeSCorey Minyard 		 * If we don't already have a device from something
20841abf71eeSCorey Minyard 		 * else (like PCI), then register a new one.
20851abf71eeSCorey Minyard 		 */
20861abf71eeSCorey Minyard 		new_smi->pdev = platform_device_alloc("ipmi_si",
20871abf71eeSCorey Minyard 						      new_smi->intf_num);
20881abf71eeSCorey Minyard 		if (!new_smi->pdev) {
20891abf71eeSCorey Minyard 			pr_err(PFX "Unable to allocate platform device\n");
20901abf71eeSCorey Minyard 			goto out_err;
20911abf71eeSCorey Minyard 		}
2092910840f2SCorey Minyard 		new_smi->io.dev = &new_smi->pdev->dev;
20939d70029eSCorey Minyard 		new_smi->io.dev->driver = &ipmi_platform_driver.driver;
20941abf71eeSCorey Minyard 		/* Nulled by device_add() */
2095910840f2SCorey Minyard 		new_smi->io.dev->init_name = init_name;
20961abf71eeSCorey Minyard 	}
20971abf71eeSCorey Minyard 
20981da177e4SLinus Torvalds 	/* Allocate the state machine's data and initialize it. */
20991da177e4SLinus Torvalds 	new_smi->si_sm = kmalloc(new_smi->handlers->size(), GFP_KERNEL);
21001da177e4SLinus Torvalds 	if (!new_smi->si_sm) {
21011da177e4SLinus Torvalds 		rv = -ENOMEM;
21021da177e4SLinus Torvalds 		goto out_err;
21031da177e4SLinus Torvalds 	}
2104e1eeb7f8SCorey Minyard 	new_smi->io.io_size = new_smi->handlers->init_data(new_smi->si_sm,
21051da177e4SLinus Torvalds 							   &new_smi->io);
21061da177e4SLinus Torvalds 
21071da177e4SLinus Torvalds 	/* Now that we know the I/O size, we can set up the I/O. */
2108e1eeb7f8SCorey Minyard 	rv = new_smi->io.io_setup(&new_smi->io);
21091da177e4SLinus Torvalds 	if (rv) {
2110910840f2SCorey Minyard 		dev_err(new_smi->io.dev, "Could not set up I/O space\n");
21111da177e4SLinus Torvalds 		goto out_err;
21121da177e4SLinus Torvalds 	}
21131da177e4SLinus Torvalds 
21141da177e4SLinus Torvalds 	/* Do low-level detection first. */
21151da177e4SLinus Torvalds 	if (new_smi->handlers->detect(new_smi->si_sm)) {
2116910840f2SCorey Minyard 		if (new_smi->io.addr_source)
2117910840f2SCorey Minyard 			dev_err(new_smi->io.dev,
2118910840f2SCorey Minyard 				"Interface detection failed\n");
21191da177e4SLinus Torvalds 		rv = -ENODEV;
21201da177e4SLinus Torvalds 		goto out_err;
21211da177e4SLinus Torvalds 	}
21221da177e4SLinus Torvalds 
2123c305e3d3SCorey Minyard 	/*
2124c305e3d3SCorey Minyard 	 * Attempt a get device id command.  If it fails, we probably
2125c305e3d3SCorey Minyard 	 * don't have a BMC here.
2126c305e3d3SCorey Minyard 	 */
21271da177e4SLinus Torvalds 	rv = try_get_dev_id(new_smi);
2128b0defcdbSCorey Minyard 	if (rv) {
2129910840f2SCorey Minyard 		if (new_smi->io.addr_source)
2130910840f2SCorey Minyard 			dev_err(new_smi->io.dev,
2131910840f2SCorey Minyard 			       "There appears to be no BMC at this location\n");
21321da177e4SLinus Torvalds 		goto out_err;
2133b0defcdbSCorey Minyard 	}
21341da177e4SLinus Torvalds 
21353ae0e0f9SCorey Minyard 	setup_oem_data_handler(new_smi);
2136ea94027bSCorey Minyard 	setup_xaction_handlers(new_smi);
2137d0882897SCorey Minyard 	check_for_broken_irqs(new_smi);
21383ae0e0f9SCorey Minyard 
2139b874b985SCorey Minyard 	new_smi->waiting_msg = NULL;
21401da177e4SLinus Torvalds 	new_smi->curr_msg = NULL;
21411da177e4SLinus Torvalds 	atomic_set(&new_smi->req_events, 0);
21427aefac26SCorey Minyard 	new_smi->run_to_completion = false;
214364959e2dSCorey Minyard 	for (i = 0; i < SI_NUM_STATS; i++)
214464959e2dSCorey Minyard 		atomic_set(&new_smi->stats[i], 0);
21451da177e4SLinus Torvalds 
21467aefac26SCorey Minyard 	new_smi->interrupt_disabled = true;
214789986496SCorey Minyard 	atomic_set(&new_smi->need_watch, 0);
21481da177e4SLinus Torvalds 
214940112ae7SCorey Minyard 	rv = try_enable_event_buffer(new_smi);
215040112ae7SCorey Minyard 	if (rv == 0)
21517aefac26SCorey Minyard 		new_smi->has_event_buffer = true;
215240112ae7SCorey Minyard 
2153c305e3d3SCorey Minyard 	/*
2154c305e3d3SCorey Minyard 	 * Start clearing the flags before we enable interrupts or the
2155c305e3d3SCorey Minyard 	 * timer to avoid racing with the timer.
2156c305e3d3SCorey Minyard 	 */
2157*4f7f5551SMasamitsu Yamazaki 	start_clear_flags(new_smi);
2158d9b7e4f7SCorey Minyard 
2159d9b7e4f7SCorey Minyard 	/*
2160d9b7e4f7SCorey Minyard 	 * IRQ is defined to be set when non-zero.  req_events will
2161d9b7e4f7SCorey Minyard 	 * cause a global flags check that will enable interrupts.
2162d9b7e4f7SCorey Minyard 	 */
2163910840f2SCorey Minyard 	if (new_smi->io.irq) {
2164d9b7e4f7SCorey Minyard 		new_smi->interrupt_disabled = false;
2165d9b7e4f7SCorey Minyard 		atomic_set(&new_smi->req_events, 1);
2166d9b7e4f7SCorey Minyard 	}
21671da177e4SLinus Torvalds 
21681abf71eeSCorey Minyard 	if (new_smi->pdev) {
2169b48f5457SZhang, Yanmin 		rv = platform_device_add(new_smi->pdev);
217050c812b2SCorey Minyard 		if (rv) {
2171910840f2SCorey Minyard 			dev_err(new_smi->io.dev,
2172bb2a08c0SCorey Minyard 				"Unable to register system interface device: %d\n",
217350c812b2SCorey Minyard 				rv);
2174453823baSCorey Minyard 			goto out_err;
217550c812b2SCorey Minyard 		}
217650c812b2SCorey Minyard 	}
217750c812b2SCorey Minyard 
21783dd377b5SCorey Minyard 	dev_set_drvdata(new_smi->io.dev, new_smi);
21793dd377b5SCorey Minyard 	rv = device_add_group(new_smi->io.dev, &ipmi_si_dev_attr_group);
21803dd377b5SCorey Minyard 	if (rv) {
21813dd377b5SCorey Minyard 		dev_err(new_smi->io.dev,
21823dd377b5SCorey Minyard 			"Unable to add device attributes: error %d\n",
21833dd377b5SCorey Minyard 			rv);
21843dd377b5SCorey Minyard 		goto out_err_stop_timer;
21853dd377b5SCorey Minyard 	}
21863dd377b5SCorey Minyard 
21871da177e4SLinus Torvalds 	rv = ipmi_register_smi(&handlers,
21881da177e4SLinus Torvalds 			       new_smi,
2189910840f2SCorey Minyard 			       new_smi->io.dev,
2190910840f2SCorey Minyard 			       new_smi->io.slave_addr);
21911da177e4SLinus Torvalds 	if (rv) {
2192910840f2SCorey Minyard 		dev_err(new_smi->io.dev,
2193910840f2SCorey Minyard 			"Unable to register device: error %d\n",
21941da177e4SLinus Torvalds 			rv);
21953dd377b5SCorey Minyard 		goto out_err_remove_attrs;
21961da177e4SLinus Torvalds 	}
21971da177e4SLinus Torvalds 
219855f91cb6SCorey Minyard #ifdef CONFIG_IPMI_PROC_INTERFACE
21991da177e4SLinus Torvalds 	rv = ipmi_smi_add_proc_entry(new_smi->intf, "type",
220007412736SAlexey Dobriyan 				     &smi_type_proc_ops,
220199b76233SAlexey Dobriyan 				     new_smi);
22021da177e4SLinus Torvalds 	if (rv) {
2203910840f2SCorey Minyard 		dev_err(new_smi->io.dev,
2204910840f2SCorey Minyard 			"Unable to create proc entry: %d\n", rv);
22051da177e4SLinus Torvalds 		goto out_err_stop_timer;
22061da177e4SLinus Torvalds 	}
22071da177e4SLinus Torvalds 
22081da177e4SLinus Torvalds 	rv = ipmi_smi_add_proc_entry(new_smi->intf, "si_stats",
220907412736SAlexey Dobriyan 				     &smi_si_stats_proc_ops,
221099b76233SAlexey Dobriyan 				     new_smi);
22111da177e4SLinus Torvalds 	if (rv) {
2212910840f2SCorey Minyard 		dev_err(new_smi->io.dev,
2213910840f2SCorey Minyard 			"Unable to create proc entry: %d\n", rv);
22141da177e4SLinus Torvalds 		goto out_err_stop_timer;
22151da177e4SLinus Torvalds 	}
22161da177e4SLinus Torvalds 
2217b361e27bSCorey Minyard 	rv = ipmi_smi_add_proc_entry(new_smi->intf, "params",
221807412736SAlexey Dobriyan 				     &smi_params_proc_ops,
221999b76233SAlexey Dobriyan 				     new_smi);
2220b361e27bSCorey Minyard 	if (rv) {
2221910840f2SCorey Minyard 		dev_err(new_smi->io.dev,
2222910840f2SCorey Minyard 			"Unable to create proc entry: %d\n", rv);
2223b361e27bSCorey Minyard 		goto out_err_stop_timer;
2224b361e27bSCorey Minyard 	}
222555f91cb6SCorey Minyard #endif
2226b361e27bSCorey Minyard 
22273f724c40STony Camuso 	/* Don't increment till we know we have succeeded. */
22283f724c40STony Camuso 	smi_num++;
22293f724c40STony Camuso 
2230910840f2SCorey Minyard 	dev_info(new_smi->io.dev, "IPMI %s interface initialized\n",
2231910840f2SCorey Minyard 		 si_to_str[new_smi->io.si_type]);
22321da177e4SLinus Torvalds 
2233910840f2SCorey Minyard 	WARN_ON(new_smi->io.dev->init_name != NULL);
22341abf71eeSCorey Minyard 	kfree(init_name);
22351abf71eeSCorey Minyard 
22361da177e4SLinus Torvalds 	return 0;
22371da177e4SLinus Torvalds 
22383dd377b5SCorey Minyard out_err_remove_attrs:
22393dd377b5SCorey Minyard 	device_remove_group(new_smi->io.dev, &ipmi_si_dev_attr_group);
22403dd377b5SCorey Minyard 	dev_set_drvdata(new_smi->io.dev, NULL);
22413dd377b5SCorey Minyard 
22421da177e4SLinus Torvalds out_err_stop_timer:
2243*4f7f5551SMasamitsu Yamazaki 	stop_timer_and_thread(new_smi);
22441da177e4SLinus Torvalds 
22451da177e4SLinus Torvalds out_err:
22467aefac26SCorey Minyard 	new_smi->interrupt_disabled = true;
22471da177e4SLinus Torvalds 
22482407d77aSMatthew Garrett 	if (new_smi->intf) {
2249b874b985SCorey Minyard 		ipmi_smi_t intf = new_smi->intf;
22502407d77aSMatthew Garrett 		new_smi->intf = NULL;
2251b874b985SCorey Minyard 		ipmi_unregister_smi(intf);
22522407d77aSMatthew Garrett 	}
22532407d77aSMatthew Garrett 
22544f3e8199SCorey Minyard 	if (new_smi->io.irq_cleanup) {
22554f3e8199SCorey Minyard 		new_smi->io.irq_cleanup(&new_smi->io);
22564f3e8199SCorey Minyard 		new_smi->io.irq_cleanup = NULL;
22572407d77aSMatthew Garrett 	}
22581da177e4SLinus Torvalds 
2259c305e3d3SCorey Minyard 	/*
2260c305e3d3SCorey Minyard 	 * Wait until we know that we are out of any interrupt
2261c305e3d3SCorey Minyard 	 * handlers might have been running before we freed the
2262c305e3d3SCorey Minyard 	 * interrupt.
2263c305e3d3SCorey Minyard 	 */
2264fbd568a3SPaul E. McKenney 	synchronize_sched();
22651da177e4SLinus Torvalds 
22661da177e4SLinus Torvalds 	if (new_smi->si_sm) {
22671da177e4SLinus Torvalds 		if (new_smi->handlers)
22681da177e4SLinus Torvalds 			new_smi->handlers->cleanup(new_smi->si_sm);
22691da177e4SLinus Torvalds 		kfree(new_smi->si_sm);
22702407d77aSMatthew Garrett 		new_smi->si_sm = NULL;
22711da177e4SLinus Torvalds 	}
2272910840f2SCorey Minyard 	if (new_smi->io.addr_source_cleanup) {
2273910840f2SCorey Minyard 		new_smi->io.addr_source_cleanup(&new_smi->io);
2274910840f2SCorey Minyard 		new_smi->io.addr_source_cleanup = NULL;
22752407d77aSMatthew Garrett 	}
2276e1eeb7f8SCorey Minyard 	if (new_smi->io.io_cleanup) {
2277e1eeb7f8SCorey Minyard 		new_smi->io.io_cleanup(&new_smi->io);
2278e1eeb7f8SCorey Minyard 		new_smi->io.io_cleanup = NULL;
22792407d77aSMatthew Garrett 	}
22801da177e4SLinus Torvalds 
2281910840f2SCorey Minyard 	if (new_smi->pdev) {
228250c812b2SCorey Minyard 		platform_device_unregister(new_smi->pdev);
22831abf71eeSCorey Minyard 		new_smi->pdev = NULL;
22841abf71eeSCorey Minyard 	} else if (new_smi->pdev) {
22851abf71eeSCorey Minyard 		platform_device_put(new_smi->pdev);
22862407d77aSMatthew Garrett 	}
2287b0defcdbSCorey Minyard 
22881abf71eeSCorey Minyard 	kfree(init_name);
22891abf71eeSCorey Minyard 
22901da177e4SLinus Torvalds 	return rv;
22911da177e4SLinus Torvalds }
22921da177e4SLinus Torvalds 
22932223cbecSBill Pemberton static int init_ipmi_si(void)
22941da177e4SLinus Torvalds {
22952407d77aSMatthew Garrett 	struct smi_info *e;
229606ee4594SMatthew Garrett 	enum ipmi_addr_src type = SI_INVALID;
22971da177e4SLinus Torvalds 
22981da177e4SLinus Torvalds 	if (initialized)
22991da177e4SLinus Torvalds 		return 0;
23001da177e4SLinus Torvalds 
2301bb2a08c0SCorey Minyard 	pr_info("IPMI System Interface driver.\n");
23021da177e4SLinus Torvalds 
2303d8cc5267SMatthew Garrett 	/* If the user gave us a device, they presumably want us to use it */
23047a453308SCorey Minyard 	if (!ipmi_si_hardcode_find_bmc())
23057a453308SCorey Minyard 		goto do_scan;
2306d8cc5267SMatthew Garrett 
23079d70029eSCorey Minyard 	ipmi_si_platform_init();
23089d70029eSCorey Minyard 
230913d0b35cSCorey Minyard 	ipmi_si_pci_init();
2310b0defcdbSCorey Minyard 
2311c6f85a75SCorey Minyard 	ipmi_si_parisc_init();
2312fdbeb7deSThomas Bogendoerfer 
231306ee4594SMatthew Garrett 	/* We prefer devices with interrupts, but in the case of a machine
231406ee4594SMatthew Garrett 	   with multiple BMCs we assume that there will be several instances
231506ee4594SMatthew Garrett 	   of a given type so if we succeed in registering a type then also
231606ee4594SMatthew Garrett 	   try to register everything else of the same type */
23177a453308SCorey Minyard do_scan:
23182407d77aSMatthew Garrett 	mutex_lock(&smi_infos_lock);
23192407d77aSMatthew Garrett 	list_for_each_entry(e, &smi_infos, link) {
232006ee4594SMatthew Garrett 		/* Try to register a device if it has an IRQ and we either
232106ee4594SMatthew Garrett 		   haven't successfully registered a device yet or this
232206ee4594SMatthew Garrett 		   device has the same type as one we successfully registered */
2323910840f2SCorey Minyard 		if (e->io.irq && (!type || e->io.addr_source == type)) {
2324d8cc5267SMatthew Garrett 			if (!try_smi_init(e)) {
2325910840f2SCorey Minyard 				type = e->io.addr_source;
232606ee4594SMatthew Garrett 			}
232706ee4594SMatthew Garrett 		}
232806ee4594SMatthew Garrett 	}
232906ee4594SMatthew Garrett 
233006ee4594SMatthew Garrett 	/* type will only have been set if we successfully registered an si */
2331bb398a4cSCorey Minyard 	if (type)
2332bb398a4cSCorey Minyard 		goto skip_fallback_noirq;
2333d8cc5267SMatthew Garrett 
2334d8cc5267SMatthew Garrett 	/* Fall back to the preferred device */
2335d8cc5267SMatthew Garrett 
2336d8cc5267SMatthew Garrett 	list_for_each_entry(e, &smi_infos, link) {
2337910840f2SCorey Minyard 		if (!e->io.irq && (!type || e->io.addr_source == type)) {
2338d8cc5267SMatthew Garrett 			if (!try_smi_init(e)) {
2339910840f2SCorey Minyard 				type = e->io.addr_source;
234006ee4594SMatthew Garrett 			}
234106ee4594SMatthew Garrett 		}
234206ee4594SMatthew Garrett 	}
2343bb398a4cSCorey Minyard 
2344bb398a4cSCorey Minyard skip_fallback_noirq:
2345bb398a4cSCorey Minyard 	initialized = 1;
2346d8cc5267SMatthew Garrett 	mutex_unlock(&smi_infos_lock);
234706ee4594SMatthew Garrett 
234806ee4594SMatthew Garrett 	if (type)
2349d8cc5267SMatthew Garrett 		return 0;
23502407d77aSMatthew Garrett 
2351d6dfd131SCorey Minyard 	mutex_lock(&smi_infos_lock);
2352b361e27bSCorey Minyard 	if (unload_when_empty && list_empty(&smi_infos)) {
2353d6dfd131SCorey Minyard 		mutex_unlock(&smi_infos_lock);
2354d2478521SCorey Minyard 		cleanup_ipmi_si();
2355bb2a08c0SCorey Minyard 		pr_warn(PFX "Unable to find any System Interface(s)\n");
23561da177e4SLinus Torvalds 		return -ENODEV;
2357b0defcdbSCorey Minyard 	} else {
2358d6dfd131SCorey Minyard 		mutex_unlock(&smi_infos_lock);
23591da177e4SLinus Torvalds 		return 0;
23601da177e4SLinus Torvalds 	}
2361b0defcdbSCorey Minyard }
23621da177e4SLinus Torvalds module_init(init_ipmi_si);
23631da177e4SLinus Torvalds 
2364b361e27bSCorey Minyard static void cleanup_one_si(struct smi_info *to_clean)
23651da177e4SLinus Torvalds {
23662407d77aSMatthew Garrett 	int           rv = 0;
23671da177e4SLinus Torvalds 
23681da177e4SLinus Torvalds 	if (!to_clean)
23691da177e4SLinus Torvalds 		return;
23701da177e4SLinus Torvalds 
2371b874b985SCorey Minyard 	if (to_clean->intf) {
2372b874b985SCorey Minyard 		ipmi_smi_t intf = to_clean->intf;
2373b874b985SCorey Minyard 
2374b874b985SCorey Minyard 		to_clean->intf = NULL;
2375b874b985SCorey Minyard 		rv = ipmi_unregister_smi(intf);
2376b874b985SCorey Minyard 		if (rv) {
2377b874b985SCorey Minyard 			pr_err(PFX "Unable to unregister device: errno=%d\n",
2378b874b985SCorey Minyard 			       rv);
2379b874b985SCorey Minyard 		}
2380b874b985SCorey Minyard 	}
2381b874b985SCorey Minyard 
23823dd377b5SCorey Minyard 	device_remove_group(to_clean->io.dev, &ipmi_si_dev_attr_group);
23833dd377b5SCorey Minyard 	dev_set_drvdata(to_clean->io.dev, NULL);
23843dd377b5SCorey Minyard 
2385b0defcdbSCorey Minyard 	list_del(&to_clean->link);
2386b0defcdbSCorey Minyard 
2387c305e3d3SCorey Minyard 	/*
2388b874b985SCorey Minyard 	 * Make sure that interrupts, the timer and the thread are
2389b874b985SCorey Minyard 	 * stopped and will not run again.
2390c305e3d3SCorey Minyard 	 */
23914f3e8199SCorey Minyard 	if (to_clean->io.irq_cleanup)
23924f3e8199SCorey Minyard 		to_clean->io.irq_cleanup(&to_clean->io);
2393*4f7f5551SMasamitsu Yamazaki 	stop_timer_and_thread(to_clean);
23941da177e4SLinus Torvalds 
2395c305e3d3SCorey Minyard 	/*
2396c305e3d3SCorey Minyard 	 * Timeouts are stopped, now make sure the interrupts are off
2397b874b985SCorey Minyard 	 * in the BMC.  Note that timers and CPU interrupts are off,
2398b874b985SCorey Minyard 	 * so no need for locks.
2399c305e3d3SCorey Minyard 	 */
2400ee6cd5f8SCorey Minyard 	while (to_clean->curr_msg || (to_clean->si_state != SI_NORMAL)) {
2401ee6cd5f8SCorey Minyard 		poll(to_clean);
2402ee6cd5f8SCorey Minyard 		schedule_timeout_uninterruptible(1);
2403ee6cd5f8SCorey Minyard 	}
24047e030d6dSCorey Minyard 	if (to_clean->handlers)
2405*4f7f5551SMasamitsu Yamazaki 		disable_si_irq(to_clean);
2406ee6cd5f8SCorey Minyard 	while (to_clean->curr_msg || (to_clean->si_state != SI_NORMAL)) {
2407ee6cd5f8SCorey Minyard 		poll(to_clean);
2408ee6cd5f8SCorey Minyard 		schedule_timeout_uninterruptible(1);
2409ee6cd5f8SCorey Minyard 	}
2410ee6cd5f8SCorey Minyard 
24112407d77aSMatthew Garrett 	if (to_clean->handlers)
24121da177e4SLinus Torvalds 		to_clean->handlers->cleanup(to_clean->si_sm);
24131da177e4SLinus Torvalds 
24141da177e4SLinus Torvalds 	kfree(to_clean->si_sm);
24151da177e4SLinus Torvalds 
2416910840f2SCorey Minyard 	if (to_clean->io.addr_source_cleanup)
2417910840f2SCorey Minyard 		to_clean->io.addr_source_cleanup(&to_clean->io);
2418e1eeb7f8SCorey Minyard 	if (to_clean->io.io_cleanup)
2419e1eeb7f8SCorey Minyard 		to_clean->io.io_cleanup(&to_clean->io);
242050c812b2SCorey Minyard 
2421910840f2SCorey Minyard 	if (to_clean->pdev)
242250c812b2SCorey Minyard 		platform_device_unregister(to_clean->pdev);
242350c812b2SCorey Minyard 
242450c812b2SCorey Minyard 	kfree(to_clean);
24251da177e4SLinus Torvalds }
24261da177e4SLinus Torvalds 
2427bb398a4cSCorey Minyard int ipmi_si_remove_by_dev(struct device *dev)
2428bb398a4cSCorey Minyard {
2429bb398a4cSCorey Minyard 	struct smi_info *e;
2430bb398a4cSCorey Minyard 	int rv = -ENOENT;
2431bb398a4cSCorey Minyard 
2432bb398a4cSCorey Minyard 	mutex_lock(&smi_infos_lock);
2433bb398a4cSCorey Minyard 	list_for_each_entry(e, &smi_infos, link) {
2434bb398a4cSCorey Minyard 		if (e->io.dev == dev) {
2435bb398a4cSCorey Minyard 			cleanup_one_si(e);
2436bb398a4cSCorey Minyard 			rv = 0;
2437bb398a4cSCorey Minyard 			break;
2438bb398a4cSCorey Minyard 		}
2439bb398a4cSCorey Minyard 	}
2440bb398a4cSCorey Minyard 	mutex_unlock(&smi_infos_lock);
2441bb398a4cSCorey Minyard 
2442bb398a4cSCorey Minyard 	return rv;
2443bb398a4cSCorey Minyard }
2444bb398a4cSCorey Minyard 
244544814ec9SCorey Minyard void ipmi_si_remove_by_data(int addr_space, enum si_type si_type,
244644814ec9SCorey Minyard 			    unsigned long addr)
244744814ec9SCorey Minyard {
244844814ec9SCorey Minyard 	/* remove */
244944814ec9SCorey Minyard 	struct smi_info *e, *tmp_e;
245044814ec9SCorey Minyard 
245144814ec9SCorey Minyard 	mutex_lock(&smi_infos_lock);
245244814ec9SCorey Minyard 	list_for_each_entry_safe(e, tmp_e, &smi_infos, link) {
245344814ec9SCorey Minyard 		if (e->io.addr_type != addr_space)
245444814ec9SCorey Minyard 			continue;
245544814ec9SCorey Minyard 		if (e->io.si_type != si_type)
245644814ec9SCorey Minyard 			continue;
245744814ec9SCorey Minyard 		if (e->io.addr_data == addr)
245844814ec9SCorey Minyard 			cleanup_one_si(e);
245944814ec9SCorey Minyard 	}
246044814ec9SCorey Minyard 	mutex_unlock(&smi_infos_lock);
246144814ec9SCorey Minyard }
246244814ec9SCorey Minyard 
24630dcf334cSSergey Senozhatsky static void cleanup_ipmi_si(void)
24641da177e4SLinus Torvalds {
2465b0defcdbSCorey Minyard 	struct smi_info *e, *tmp_e;
24661da177e4SLinus Torvalds 
24671da177e4SLinus Torvalds 	if (!initialized)
24681da177e4SLinus Torvalds 		return;
24691da177e4SLinus Torvalds 
247013d0b35cSCorey Minyard 	ipmi_si_pci_shutdown();
2471c6f85a75SCorey Minyard 
2472c6f85a75SCorey Minyard 	ipmi_si_parisc_shutdown();
2473b0defcdbSCorey Minyard 
24749d70029eSCorey Minyard 	ipmi_si_platform_shutdown();
2475dba9b4f6SCorey Minyard 
2476d6dfd131SCorey Minyard 	mutex_lock(&smi_infos_lock);
2477b0defcdbSCorey Minyard 	list_for_each_entry_safe(e, tmp_e, &smi_infos, link)
2478b0defcdbSCorey Minyard 		cleanup_one_si(e);
2479d6dfd131SCorey Minyard 	mutex_unlock(&smi_infos_lock);
24801da177e4SLinus Torvalds }
24811da177e4SLinus Torvalds module_exit(cleanup_ipmi_si);
24821da177e4SLinus Torvalds 
24830944d889SCorey Minyard MODULE_ALIAS("platform:dmi-ipmi-si");
24841da177e4SLinus Torvalds MODULE_LICENSE("GPL");
24851fdd75bdSCorey Minyard MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
2486c305e3d3SCorey Minyard MODULE_DESCRIPTION("Interface to the IPMI driver for the KCS, SMIC, and BT"
2487c305e3d3SCorey Minyard 		   " system interfaces.");
2488