xref: /openbmc/linux/drivers/char/ipmi/ipmi_si_intf.c (revision c6f85a753df858018b063f5ab89b277179c3387f)
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>
521da177e4SLinus Torvalds #include <linux/ioport.h>
53ea94027bSCorey Minyard #include <linux/notifier.h>
54b0defcdbSCorey Minyard #include <linux/mutex.h>
55e9a705a0SMatt Domsch #include <linux/kthread.h>
561da177e4SLinus Torvalds #include <asm/irq.h>
571da177e4SLinus Torvalds #include <linux/interrupt.h>
581da177e4SLinus Torvalds #include <linux/rcupdate.h>
5916f4232cSZhao Yakui #include <linux/ipmi.h>
601da177e4SLinus Torvalds #include <linux/ipmi_smi.h>
611da177e4SLinus Torvalds #include <asm/io.h>
621e89a499SCorey Minyard #include "ipmi_si.h"
63b361e27bSCorey Minyard #include <linux/string.h>
64b361e27bSCorey Minyard #include <linux/ctype.h>
65dba9b4f6SCorey Minyard 
66b361e27bSCorey Minyard #define PFX "ipmi_si: "
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds /* Measure times between events in the driver. */
691da177e4SLinus Torvalds #undef DEBUG_TIMING
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds /* Call every 10 ms. */
721da177e4SLinus Torvalds #define SI_TIMEOUT_TIME_USEC	10000
731da177e4SLinus Torvalds #define SI_USEC_PER_JIFFY	(1000000/HZ)
741da177e4SLinus Torvalds #define SI_TIMEOUT_JIFFIES	(SI_TIMEOUT_TIME_USEC/SI_USEC_PER_JIFFY)
751da177e4SLinus Torvalds #define SI_SHORT_TIMEOUT_USEC  250 /* .25ms when the SM request a
761da177e4SLinus Torvalds 				      short timeout */
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds enum si_intf_state {
791da177e4SLinus Torvalds 	SI_NORMAL,
801da177e4SLinus Torvalds 	SI_GETTING_FLAGS,
811da177e4SLinus Torvalds 	SI_GETTING_EVENTS,
821da177e4SLinus Torvalds 	SI_CLEARING_FLAGS,
831da177e4SLinus Torvalds 	SI_GETTING_MESSAGES,
84d9b7e4f7SCorey Minyard 	SI_CHECKING_ENABLES,
85d9b7e4f7SCorey Minyard 	SI_SETTING_ENABLES
861da177e4SLinus Torvalds 	/* FIXME - add watchdog stuff. */
871da177e4SLinus Torvalds };
881da177e4SLinus Torvalds 
899dbf68f9SCorey Minyard /* Some BT-specific defines we need here. */
909dbf68f9SCorey Minyard #define IPMI_BT_INTMASK_REG		2
919dbf68f9SCorey Minyard #define IPMI_BT_INTMASK_CLEAR_IRQ_BIT	2
929dbf68f9SCorey Minyard #define IPMI_BT_INTMASK_ENABLE_IRQ_BIT	1
939dbf68f9SCorey Minyard 
9499ee6735SLABBE Corentin static const char * const si_to_str[] = { "kcs", "smic", "bt" };
951da177e4SLinus Torvalds 
96bb398a4cSCorey Minyard static int initialized;
97bb398a4cSCorey Minyard 
9864959e2dSCorey Minyard /*
9964959e2dSCorey Minyard  * Indexes into stats[] in smi_info below.
10064959e2dSCorey Minyard  */
101ba8ff1c6SCorey Minyard enum si_stat_indexes {
102ba8ff1c6SCorey Minyard 	/*
103ba8ff1c6SCorey Minyard 	 * Number of times the driver requested a timer while an operation
104ba8ff1c6SCorey Minyard 	 * was in progress.
105ba8ff1c6SCorey Minyard 	 */
106ba8ff1c6SCorey Minyard 	SI_STAT_short_timeouts = 0,
10764959e2dSCorey Minyard 
108ba8ff1c6SCorey Minyard 	/*
109ba8ff1c6SCorey Minyard 	 * Number of times the driver requested a timer while nothing was in
110ba8ff1c6SCorey Minyard 	 * progress.
111ba8ff1c6SCorey Minyard 	 */
112ba8ff1c6SCorey Minyard 	SI_STAT_long_timeouts,
11364959e2dSCorey Minyard 
114ba8ff1c6SCorey Minyard 	/* Number of times the interface was idle while being polled. */
115ba8ff1c6SCorey Minyard 	SI_STAT_idles,
116ba8ff1c6SCorey Minyard 
117ba8ff1c6SCorey Minyard 	/* Number of interrupts the driver handled. */
118ba8ff1c6SCorey Minyard 	SI_STAT_interrupts,
119ba8ff1c6SCorey Minyard 
120ba8ff1c6SCorey Minyard 	/* Number of time the driver got an ATTN from the hardware. */
121ba8ff1c6SCorey Minyard 	SI_STAT_attentions,
122ba8ff1c6SCorey Minyard 
123ba8ff1c6SCorey Minyard 	/* Number of times the driver requested flags from the hardware. */
124ba8ff1c6SCorey Minyard 	SI_STAT_flag_fetches,
125ba8ff1c6SCorey Minyard 
126ba8ff1c6SCorey Minyard 	/* Number of times the hardware didn't follow the state machine. */
127ba8ff1c6SCorey Minyard 	SI_STAT_hosed_count,
128ba8ff1c6SCorey Minyard 
129ba8ff1c6SCorey Minyard 	/* Number of completed messages. */
130ba8ff1c6SCorey Minyard 	SI_STAT_complete_transactions,
131ba8ff1c6SCorey Minyard 
132ba8ff1c6SCorey Minyard 	/* Number of IPMI events received from the hardware. */
133ba8ff1c6SCorey Minyard 	SI_STAT_events,
134ba8ff1c6SCorey Minyard 
135ba8ff1c6SCorey Minyard 	/* Number of watchdog pretimeouts. */
136ba8ff1c6SCorey Minyard 	SI_STAT_watchdog_pretimeouts,
137ba8ff1c6SCorey Minyard 
138b3834be5SAdam Buchbinder 	/* Number of asynchronous messages received. */
139ba8ff1c6SCorey Minyard 	SI_STAT_incoming_messages,
140ba8ff1c6SCorey Minyard 
141ba8ff1c6SCorey Minyard 
142ba8ff1c6SCorey Minyard 	/* This *must* remain last, add new values above this. */
143ba8ff1c6SCorey Minyard 	SI_NUM_STATS
144ba8ff1c6SCorey Minyard };
14564959e2dSCorey Minyard 
146c305e3d3SCorey Minyard struct smi_info {
147a9a2c44fSCorey Minyard 	int                    intf_num;
1481da177e4SLinus Torvalds 	ipmi_smi_t             intf;
1491da177e4SLinus Torvalds 	struct si_sm_data      *si_sm;
15081d02b7fSCorey Minyard 	const struct si_sm_handlers *handlers;
1511da177e4SLinus Torvalds 	spinlock_t             si_lock;
152b874b985SCorey Minyard 	struct ipmi_smi_msg    *waiting_msg;
1531da177e4SLinus Torvalds 	struct ipmi_smi_msg    *curr_msg;
1541da177e4SLinus Torvalds 	enum si_intf_state     si_state;
1551da177e4SLinus Torvalds 
156c305e3d3SCorey Minyard 	/*
157c305e3d3SCorey Minyard 	 * Used to handle the various types of I/O that can occur with
158c305e3d3SCorey Minyard 	 * IPMI
159c305e3d3SCorey Minyard 	 */
1601da177e4SLinus Torvalds 	struct si_sm_io io;
1611da177e4SLinus Torvalds 
162c305e3d3SCorey Minyard 	/*
163c305e3d3SCorey Minyard 	 * Per-OEM handler, called from handle_flags().  Returns 1
164c305e3d3SCorey Minyard 	 * when handle_flags() needs to be re-run or 0 indicating it
165c305e3d3SCorey Minyard 	 * set si_state itself.
1663ae0e0f9SCorey Minyard 	 */
1673ae0e0f9SCorey Minyard 	int (*oem_data_avail_handler)(struct smi_info *smi_info);
1683ae0e0f9SCorey Minyard 
169c305e3d3SCorey Minyard 	/*
170c305e3d3SCorey Minyard 	 * Flags from the last GET_MSG_FLAGS command, used when an ATTN
171c305e3d3SCorey Minyard 	 * is set to hold the flags until we are done handling everything
172c305e3d3SCorey Minyard 	 * from the flags.
173c305e3d3SCorey Minyard 	 */
1741da177e4SLinus Torvalds #define RECEIVE_MSG_AVAIL	0x01
1751da177e4SLinus Torvalds #define EVENT_MSG_BUFFER_FULL	0x02
1761da177e4SLinus Torvalds #define WDT_PRE_TIMEOUT_INT	0x08
1773ae0e0f9SCorey Minyard #define OEM0_DATA_AVAIL     0x20
1783ae0e0f9SCorey Minyard #define OEM1_DATA_AVAIL     0x40
1793ae0e0f9SCorey Minyard #define OEM2_DATA_AVAIL     0x80
1803ae0e0f9SCorey Minyard #define OEM_DATA_AVAIL      (OEM0_DATA_AVAIL | \
1813ae0e0f9SCorey Minyard 			     OEM1_DATA_AVAIL | \
1823ae0e0f9SCorey Minyard 			     OEM2_DATA_AVAIL)
1831da177e4SLinus Torvalds 	unsigned char       msg_flags;
1841da177e4SLinus Torvalds 
18540112ae7SCorey Minyard 	/* Does the BMC have an event buffer? */
1867aefac26SCorey Minyard 	bool		    has_event_buffer;
18740112ae7SCorey Minyard 
188c305e3d3SCorey Minyard 	/*
189c305e3d3SCorey Minyard 	 * If set to true, this will request events the next time the
190c305e3d3SCorey Minyard 	 * state machine is idle.
191c305e3d3SCorey Minyard 	 */
1921da177e4SLinus Torvalds 	atomic_t            req_events;
1931da177e4SLinus Torvalds 
194c305e3d3SCorey Minyard 	/*
195c305e3d3SCorey Minyard 	 * If true, run the state machine to completion on every send
196c305e3d3SCorey Minyard 	 * call.  Generally used after a panic to make sure stuff goes
197c305e3d3SCorey Minyard 	 * out.
198c305e3d3SCorey Minyard 	 */
1997aefac26SCorey Minyard 	bool                run_to_completion;
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds 	/* The I/O port of an SI interface. */
2021da177e4SLinus Torvalds 	int                 port;
2031da177e4SLinus Torvalds 
204c305e3d3SCorey Minyard 	/*
205c305e3d3SCorey Minyard 	 * The space between start addresses of the two ports.  For
206c305e3d3SCorey Minyard 	 * instance, if the first port is 0xca2 and the spacing is 4, then
207c305e3d3SCorey Minyard 	 * the second port is 0xca6.
208c305e3d3SCorey Minyard 	 */
2091da177e4SLinus Torvalds 	unsigned int        spacing;
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds 	/* The timer for this si. */
2121da177e4SLinus Torvalds 	struct timer_list   si_timer;
2131da177e4SLinus Torvalds 
21448e8ac29SBodo Stroesser 	/* This flag is set, if the timer is running (timer_pending() isn't enough) */
21548e8ac29SBodo Stroesser 	bool		    timer_running;
21648e8ac29SBodo Stroesser 
2171da177e4SLinus Torvalds 	/* The time (in jiffies) the last timeout occurred at. */
2181da177e4SLinus Torvalds 	unsigned long       last_timeout_jiffies;
2191da177e4SLinus Torvalds 
22089986496SCorey Minyard 	/* Are we waiting for the events, pretimeouts, received msgs? */
22189986496SCorey Minyard 	atomic_t            need_watch;
22289986496SCorey Minyard 
223c305e3d3SCorey Minyard 	/*
224c305e3d3SCorey Minyard 	 * The driver will disable interrupts when it gets into a
225c305e3d3SCorey Minyard 	 * situation where it cannot handle messages due to lack of
226c305e3d3SCorey Minyard 	 * memory.  Once that situation clears up, it will re-enable
227c305e3d3SCorey Minyard 	 * interrupts.
228c305e3d3SCorey Minyard 	 */
2297aefac26SCorey Minyard 	bool interrupt_disabled;
2301da177e4SLinus Torvalds 
231d9b7e4f7SCorey Minyard 	/*
232d9b7e4f7SCorey Minyard 	 * Does the BMC support events?
233d9b7e4f7SCorey Minyard 	 */
234d9b7e4f7SCorey Minyard 	bool supports_event_msg_buff;
235d9b7e4f7SCorey Minyard 
236a8df150cSCorey Minyard 	/*
237d0882897SCorey Minyard 	 * Can we disable interrupts the global enables receive irq
238d0882897SCorey Minyard 	 * bit?  There are currently two forms of brokenness, some
239d0882897SCorey Minyard 	 * systems cannot disable the bit (which is technically within
240d0882897SCorey Minyard 	 * the spec but a bad idea) and some systems have the bit
241d0882897SCorey Minyard 	 * forced to zero even though interrupts work (which is
242d0882897SCorey Minyard 	 * clearly outside the spec).  The next bool tells which form
243d0882897SCorey Minyard 	 * of brokenness is present.
2441e7d6a45SCorey Minyard 	 */
245d0882897SCorey Minyard 	bool cannot_disable_irq;
246d0882897SCorey Minyard 
247d0882897SCorey Minyard 	/*
248d0882897SCorey Minyard 	 * Some systems are broken and cannot set the irq enable
249d0882897SCorey Minyard 	 * bit, even if they support interrupts.
250d0882897SCorey Minyard 	 */
251d0882897SCorey Minyard 	bool irq_enable_broken;
2521e7d6a45SCorey Minyard 
2531e7d6a45SCorey Minyard 	/*
254a8df150cSCorey Minyard 	 * Did we get an attention that we did not handle?
255a8df150cSCorey Minyard 	 */
256a8df150cSCorey Minyard 	bool got_attn;
257a8df150cSCorey Minyard 
25850c812b2SCorey Minyard 	/* From the get device id response... */
2593ae0e0f9SCorey Minyard 	struct ipmi_device_id device_id;
2601da177e4SLinus Torvalds 
261910840f2SCorey Minyard 	/* Default driver model device. */
26250c812b2SCorey Minyard 	struct platform_device *pdev;
26350c812b2SCorey Minyard 
2641da177e4SLinus Torvalds 	/* Counters and things for the proc filesystem. */
26564959e2dSCorey Minyard 	atomic_t stats[SI_NUM_STATS];
266a9a2c44fSCorey Minyard 
267e9a705a0SMatt Domsch 	struct task_struct *thread;
268b0defcdbSCorey Minyard 
269b0defcdbSCorey Minyard 	struct list_head link;
2701da177e4SLinus Torvalds };
2711da177e4SLinus Torvalds 
27264959e2dSCorey Minyard #define smi_inc_stat(smi, stat) \
27364959e2dSCorey Minyard 	atomic_inc(&(smi)->stats[SI_STAT_ ## stat])
27464959e2dSCorey Minyard #define smi_get_stat(smi, stat) \
27564959e2dSCorey Minyard 	((unsigned int) atomic_read(&(smi)->stats[SI_STAT_ ## stat]))
27664959e2dSCorey Minyard 
2777a453308SCorey Minyard #define IPMI_MAX_INTFS 4
2787a453308SCorey Minyard static int force_kipmid[IPMI_MAX_INTFS];
279a51f4a81SCorey Minyard static int num_force_kipmid;
280a51f4a81SCorey Minyard 
2817a453308SCorey Minyard static unsigned int kipmid_max_busy_us[IPMI_MAX_INTFS];
282ae74e823SMartin Wilck static int num_max_busy_us;
283ae74e823SMartin Wilck 
2847aefac26SCorey Minyard static bool unload_when_empty = true;
285b361e27bSCorey Minyard 
286b0defcdbSCorey Minyard static int try_smi_init(struct smi_info *smi);
287b361e27bSCorey Minyard static void cleanup_one_si(struct smi_info *to_clean);
288d2478521SCorey Minyard static void cleanup_ipmi_si(void);
289b0defcdbSCorey Minyard 
290f93aae9fSJohn Stultz #ifdef DEBUG_TIMING
291f93aae9fSJohn Stultz void debug_timestamp(char *msg)
292f93aae9fSJohn Stultz {
29348862ea2SJohn Stultz 	struct timespec64 t;
294f93aae9fSJohn Stultz 
29548862ea2SJohn Stultz 	getnstimeofday64(&t);
29648862ea2SJohn Stultz 	pr_debug("**%s: %lld.%9.9ld\n", msg, (long long) t.tv_sec, t.tv_nsec);
297f93aae9fSJohn Stultz }
298f93aae9fSJohn Stultz #else
299f93aae9fSJohn Stultz #define debug_timestamp(x)
300f93aae9fSJohn Stultz #endif
301f93aae9fSJohn Stultz 
302e041c683SAlan Stern static ATOMIC_NOTIFIER_HEAD(xaction_notifier_list);
303ea94027bSCorey Minyard static int register_xaction_notifier(struct notifier_block *nb)
304ea94027bSCorey Minyard {
305e041c683SAlan Stern 	return atomic_notifier_chain_register(&xaction_notifier_list, nb);
306ea94027bSCorey Minyard }
307ea94027bSCorey Minyard 
3081da177e4SLinus Torvalds static void deliver_recv_msg(struct smi_info *smi_info,
3091da177e4SLinus Torvalds 			     struct ipmi_smi_msg *msg)
3101da177e4SLinus Torvalds {
3117adf579cSCorey Minyard 	/* Deliver the message to the upper layer. */
312968bf7ccSCorey Minyard 	if (smi_info->intf)
313a747c5abSJiri Kosina 		ipmi_smi_msg_received(smi_info->intf, msg);
314968bf7ccSCorey Minyard 	else
315968bf7ccSCorey Minyard 		ipmi_free_smi_msg(msg);
316a747c5abSJiri Kosina }
3171da177e4SLinus Torvalds 
3184d7cbac7SCorey Minyard static void return_hosed_msg(struct smi_info *smi_info, int cCode)
3191da177e4SLinus Torvalds {
3201da177e4SLinus Torvalds 	struct ipmi_smi_msg *msg = smi_info->curr_msg;
3211da177e4SLinus Torvalds 
3224d7cbac7SCorey Minyard 	if (cCode < 0 || cCode > IPMI_ERR_UNSPECIFIED)
3234d7cbac7SCorey Minyard 		cCode = IPMI_ERR_UNSPECIFIED;
3244d7cbac7SCorey Minyard 	/* else use it as is */
3254d7cbac7SCorey Minyard 
32625985edcSLucas De Marchi 	/* Make it a response */
3271da177e4SLinus Torvalds 	msg->rsp[0] = msg->data[0] | 4;
3281da177e4SLinus Torvalds 	msg->rsp[1] = msg->data[1];
3294d7cbac7SCorey Minyard 	msg->rsp[2] = cCode;
3301da177e4SLinus Torvalds 	msg->rsp_size = 3;
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds 	smi_info->curr_msg = NULL;
3331da177e4SLinus Torvalds 	deliver_recv_msg(smi_info, msg);
3341da177e4SLinus Torvalds }
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds static enum si_sm_result start_next_msg(struct smi_info *smi_info)
3371da177e4SLinus Torvalds {
3381da177e4SLinus Torvalds 	int              rv;
3391da177e4SLinus Torvalds 
340b874b985SCorey Minyard 	if (!smi_info->waiting_msg) {
3411da177e4SLinus Torvalds 		smi_info->curr_msg = NULL;
3421da177e4SLinus Torvalds 		rv = SI_SM_IDLE;
3431da177e4SLinus Torvalds 	} else {
3441da177e4SLinus Torvalds 		int err;
3451da177e4SLinus Torvalds 
346b874b985SCorey Minyard 		smi_info->curr_msg = smi_info->waiting_msg;
347b874b985SCorey Minyard 		smi_info->waiting_msg = NULL;
348f93aae9fSJohn Stultz 		debug_timestamp("Start2");
349e041c683SAlan Stern 		err = atomic_notifier_call_chain(&xaction_notifier_list,
350e041c683SAlan Stern 				0, smi_info);
351ea94027bSCorey Minyard 		if (err & NOTIFY_STOP_MASK) {
352ea94027bSCorey Minyard 			rv = SI_SM_CALL_WITHOUT_DELAY;
353ea94027bSCorey Minyard 			goto out;
354ea94027bSCorey Minyard 		}
3551da177e4SLinus Torvalds 		err = smi_info->handlers->start_transaction(
3561da177e4SLinus Torvalds 			smi_info->si_sm,
3571da177e4SLinus Torvalds 			smi_info->curr_msg->data,
3581da177e4SLinus Torvalds 			smi_info->curr_msg->data_size);
359c305e3d3SCorey Minyard 		if (err)
3604d7cbac7SCorey Minyard 			return_hosed_msg(smi_info, err);
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds 		rv = SI_SM_CALL_WITHOUT_DELAY;
3631da177e4SLinus Torvalds 	}
364ea94027bSCorey Minyard out:
3651da177e4SLinus Torvalds 	return rv;
3661da177e4SLinus Torvalds }
3671da177e4SLinus Torvalds 
3680cfec916SCorey Minyard static void smi_mod_timer(struct smi_info *smi_info, unsigned long new_val)
3690cfec916SCorey Minyard {
3700cfec916SCorey Minyard 	smi_info->last_timeout_jiffies = jiffies;
3710cfec916SCorey Minyard 	mod_timer(&smi_info->si_timer, new_val);
3720cfec916SCorey Minyard 	smi_info->timer_running = true;
3730cfec916SCorey Minyard }
3740cfec916SCorey Minyard 
3750cfec916SCorey Minyard /*
3760cfec916SCorey Minyard  * Start a new message and (re)start the timer and thread.
3770cfec916SCorey Minyard  */
3780cfec916SCorey Minyard static void start_new_msg(struct smi_info *smi_info, unsigned char *msg,
3790cfec916SCorey Minyard 			  unsigned int size)
3800cfec916SCorey Minyard {
3810cfec916SCorey Minyard 	smi_mod_timer(smi_info, jiffies + SI_TIMEOUT_JIFFIES);
3820cfec916SCorey Minyard 
3830cfec916SCorey Minyard 	if (smi_info->thread)
3840cfec916SCorey Minyard 		wake_up_process(smi_info->thread);
3850cfec916SCorey Minyard 
3860cfec916SCorey Minyard 	smi_info->handlers->start_transaction(smi_info->si_sm, msg, size);
3870cfec916SCorey Minyard }
3880cfec916SCorey Minyard 
3890cfec916SCorey Minyard static void start_check_enables(struct smi_info *smi_info, bool start_timer)
390ee6cd5f8SCorey Minyard {
391ee6cd5f8SCorey Minyard 	unsigned char msg[2];
392ee6cd5f8SCorey Minyard 
393ee6cd5f8SCorey Minyard 	msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
394ee6cd5f8SCorey Minyard 	msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
395ee6cd5f8SCorey Minyard 
3960cfec916SCorey Minyard 	if (start_timer)
3970cfec916SCorey Minyard 		start_new_msg(smi_info, msg, 2);
3980cfec916SCorey Minyard 	else
399ee6cd5f8SCorey Minyard 		smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
400d9b7e4f7SCorey Minyard 	smi_info->si_state = SI_CHECKING_ENABLES;
401ee6cd5f8SCorey Minyard }
402ee6cd5f8SCorey Minyard 
4030cfec916SCorey Minyard static void start_clear_flags(struct smi_info *smi_info, bool start_timer)
4041da177e4SLinus Torvalds {
4051da177e4SLinus Torvalds 	unsigned char msg[3];
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds 	/* Make sure the watchdog pre-timeout flag is not set at startup. */
4081da177e4SLinus Torvalds 	msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
4091da177e4SLinus Torvalds 	msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
4101da177e4SLinus Torvalds 	msg[2] = WDT_PRE_TIMEOUT_INT;
4111da177e4SLinus Torvalds 
4120cfec916SCorey Minyard 	if (start_timer)
4130cfec916SCorey Minyard 		start_new_msg(smi_info, msg, 3);
4140cfec916SCorey Minyard 	else
4151da177e4SLinus Torvalds 		smi_info->handlers->start_transaction(smi_info->si_sm, msg, 3);
4161da177e4SLinus Torvalds 	smi_info->si_state = SI_CLEARING_FLAGS;
4171da177e4SLinus Torvalds }
4181da177e4SLinus Torvalds 
419968bf7ccSCorey Minyard static void start_getting_msg_queue(struct smi_info *smi_info)
420968bf7ccSCorey Minyard {
421968bf7ccSCorey Minyard 	smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
422968bf7ccSCorey Minyard 	smi_info->curr_msg->data[1] = IPMI_GET_MSG_CMD;
423968bf7ccSCorey Minyard 	smi_info->curr_msg->data_size = 2;
424968bf7ccSCorey Minyard 
4250cfec916SCorey Minyard 	start_new_msg(smi_info, smi_info->curr_msg->data,
426968bf7ccSCorey Minyard 		      smi_info->curr_msg->data_size);
427968bf7ccSCorey Minyard 	smi_info->si_state = SI_GETTING_MESSAGES;
428968bf7ccSCorey Minyard }
429968bf7ccSCorey Minyard 
430968bf7ccSCorey Minyard static void start_getting_events(struct smi_info *smi_info)
431968bf7ccSCorey Minyard {
432968bf7ccSCorey Minyard 	smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
433968bf7ccSCorey Minyard 	smi_info->curr_msg->data[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD;
434968bf7ccSCorey Minyard 	smi_info->curr_msg->data_size = 2;
435968bf7ccSCorey Minyard 
4360cfec916SCorey Minyard 	start_new_msg(smi_info, smi_info->curr_msg->data,
437968bf7ccSCorey Minyard 		      smi_info->curr_msg->data_size);
438968bf7ccSCorey Minyard 	smi_info->si_state = SI_GETTING_EVENTS;
439968bf7ccSCorey Minyard }
440968bf7ccSCorey Minyard 
441c305e3d3SCorey Minyard /*
442c305e3d3SCorey Minyard  * When we have a situtaion where we run out of memory and cannot
443c305e3d3SCorey Minyard  * allocate messages, we just leave them in the BMC and run the system
444c305e3d3SCorey Minyard  * polled until we can allocate some memory.  Once we have some
445c305e3d3SCorey Minyard  * memory, we will re-enable the interrupt.
4461e7d6a45SCorey Minyard  *
4471e7d6a45SCorey Minyard  * Note that we cannot just use disable_irq(), since the interrupt may
4481e7d6a45SCorey Minyard  * be shared.
449c305e3d3SCorey Minyard  */
4500cfec916SCorey Minyard static inline bool disable_si_irq(struct smi_info *smi_info, bool start_timer)
4511da177e4SLinus Torvalds {
452910840f2SCorey Minyard 	if ((smi_info->io.irq) && (!smi_info->interrupt_disabled)) {
4537aefac26SCorey Minyard 		smi_info->interrupt_disabled = true;
4540cfec916SCorey Minyard 		start_check_enables(smi_info, start_timer);
455968bf7ccSCorey Minyard 		return true;
4561da177e4SLinus Torvalds 	}
457968bf7ccSCorey Minyard 	return false;
4581da177e4SLinus Torvalds }
4591da177e4SLinus Torvalds 
460968bf7ccSCorey Minyard static inline bool enable_si_irq(struct smi_info *smi_info)
4611da177e4SLinus Torvalds {
462910840f2SCorey Minyard 	if ((smi_info->io.irq) && (smi_info->interrupt_disabled)) {
4637aefac26SCorey Minyard 		smi_info->interrupt_disabled = false;
4640cfec916SCorey Minyard 		start_check_enables(smi_info, true);
465968bf7ccSCorey Minyard 		return true;
4661da177e4SLinus Torvalds 	}
467968bf7ccSCorey Minyard 	return false;
468968bf7ccSCorey Minyard }
469968bf7ccSCorey Minyard 
470968bf7ccSCorey Minyard /*
471968bf7ccSCorey Minyard  * Allocate a message.  If unable to allocate, start the interrupt
472968bf7ccSCorey Minyard  * disable process and return NULL.  If able to allocate but
473968bf7ccSCorey Minyard  * interrupts are disabled, free the message and return NULL after
474968bf7ccSCorey Minyard  * starting the interrupt enable process.
475968bf7ccSCorey Minyard  */
476968bf7ccSCorey Minyard static struct ipmi_smi_msg *alloc_msg_handle_irq(struct smi_info *smi_info)
477968bf7ccSCorey Minyard {
478968bf7ccSCorey Minyard 	struct ipmi_smi_msg *msg;
479968bf7ccSCorey Minyard 
480968bf7ccSCorey Minyard 	msg = ipmi_alloc_smi_msg();
481968bf7ccSCorey Minyard 	if (!msg) {
4820cfec916SCorey Minyard 		if (!disable_si_irq(smi_info, true))
483968bf7ccSCorey Minyard 			smi_info->si_state = SI_NORMAL;
484968bf7ccSCorey Minyard 	} else if (enable_si_irq(smi_info)) {
485968bf7ccSCorey Minyard 		ipmi_free_smi_msg(msg);
486968bf7ccSCorey Minyard 		msg = NULL;
487968bf7ccSCorey Minyard 	}
488968bf7ccSCorey Minyard 	return msg;
4891da177e4SLinus Torvalds }
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds static void handle_flags(struct smi_info *smi_info)
4921da177e4SLinus Torvalds {
4933ae0e0f9SCorey Minyard retry:
4941da177e4SLinus Torvalds 	if (smi_info->msg_flags & WDT_PRE_TIMEOUT_INT) {
4951da177e4SLinus Torvalds 		/* Watchdog pre-timeout */
49664959e2dSCorey Minyard 		smi_inc_stat(smi_info, watchdog_pretimeouts);
4971da177e4SLinus Torvalds 
4980cfec916SCorey Minyard 		start_clear_flags(smi_info, true);
4991da177e4SLinus Torvalds 		smi_info->msg_flags &= ~WDT_PRE_TIMEOUT_INT;
500968bf7ccSCorey Minyard 		if (smi_info->intf)
5011da177e4SLinus Torvalds 			ipmi_smi_watchdog_pretimeout(smi_info->intf);
5021da177e4SLinus Torvalds 	} else if (smi_info->msg_flags & RECEIVE_MSG_AVAIL) {
5031da177e4SLinus Torvalds 		/* Messages available. */
504968bf7ccSCorey Minyard 		smi_info->curr_msg = alloc_msg_handle_irq(smi_info);
505968bf7ccSCorey Minyard 		if (!smi_info->curr_msg)
5061da177e4SLinus Torvalds 			return;
5071da177e4SLinus Torvalds 
508968bf7ccSCorey Minyard 		start_getting_msg_queue(smi_info);
5091da177e4SLinus Torvalds 	} else if (smi_info->msg_flags & EVENT_MSG_BUFFER_FULL) {
5101da177e4SLinus Torvalds 		/* Events available. */
511968bf7ccSCorey Minyard 		smi_info->curr_msg = alloc_msg_handle_irq(smi_info);
512968bf7ccSCorey Minyard 		if (!smi_info->curr_msg)
5131da177e4SLinus Torvalds 			return;
5141da177e4SLinus Torvalds 
515968bf7ccSCorey Minyard 		start_getting_events(smi_info);
5164064d5efSCorey Minyard 	} else if (smi_info->msg_flags & OEM_DATA_AVAIL &&
5174064d5efSCorey Minyard 		   smi_info->oem_data_avail_handler) {
5183ae0e0f9SCorey Minyard 		if (smi_info->oem_data_avail_handler(smi_info))
5193ae0e0f9SCorey Minyard 			goto retry;
520c305e3d3SCorey Minyard 	} else
5211da177e4SLinus Torvalds 		smi_info->si_state = SI_NORMAL;
5221da177e4SLinus Torvalds }
5231da177e4SLinus Torvalds 
524d9b7e4f7SCorey Minyard /*
525d9b7e4f7SCorey Minyard  * Global enables we care about.
526d9b7e4f7SCorey Minyard  */
527d9b7e4f7SCorey Minyard #define GLOBAL_ENABLES_MASK (IPMI_BMC_EVT_MSG_BUFF | IPMI_BMC_RCV_MSG_INTR | \
528d9b7e4f7SCorey Minyard 			     IPMI_BMC_EVT_MSG_INTR)
529d9b7e4f7SCorey Minyard 
53095c97b59SCorey Minyard static u8 current_global_enables(struct smi_info *smi_info, u8 base,
53195c97b59SCorey Minyard 				 bool *irq_on)
532d9b7e4f7SCorey Minyard {
533d9b7e4f7SCorey Minyard 	u8 enables = 0;
534d9b7e4f7SCorey Minyard 
535d9b7e4f7SCorey Minyard 	if (smi_info->supports_event_msg_buff)
536d9b7e4f7SCorey Minyard 		enables |= IPMI_BMC_EVT_MSG_BUFF;
537d9b7e4f7SCorey Minyard 
538910840f2SCorey Minyard 	if (((smi_info->io.irq && !smi_info->interrupt_disabled) ||
539d0882897SCorey Minyard 	     smi_info->cannot_disable_irq) &&
540d0882897SCorey Minyard 	    !smi_info->irq_enable_broken)
541d9b7e4f7SCorey Minyard 		enables |= IPMI_BMC_RCV_MSG_INTR;
542d9b7e4f7SCorey Minyard 
543d9b7e4f7SCorey Minyard 	if (smi_info->supports_event_msg_buff &&
544910840f2SCorey Minyard 	    smi_info->io.irq && !smi_info->interrupt_disabled &&
545d0882897SCorey Minyard 	    !smi_info->irq_enable_broken)
546d9b7e4f7SCorey Minyard 		enables |= IPMI_BMC_EVT_MSG_INTR;
547d9b7e4f7SCorey Minyard 
54895c97b59SCorey Minyard 	*irq_on = enables & (IPMI_BMC_EVT_MSG_INTR | IPMI_BMC_RCV_MSG_INTR);
54995c97b59SCorey Minyard 
550d9b7e4f7SCorey Minyard 	return enables;
551d9b7e4f7SCorey Minyard }
552d9b7e4f7SCorey Minyard 
55395c97b59SCorey Minyard static void check_bt_irq(struct smi_info *smi_info, bool irq_on)
55495c97b59SCorey Minyard {
55595c97b59SCorey Minyard 	u8 irqstate = smi_info->io.inputb(&smi_info->io, IPMI_BT_INTMASK_REG);
55695c97b59SCorey Minyard 
55795c97b59SCorey Minyard 	irqstate &= IPMI_BT_INTMASK_ENABLE_IRQ_BIT;
55895c97b59SCorey Minyard 
55995c97b59SCorey Minyard 	if ((bool)irqstate == irq_on)
56095c97b59SCorey Minyard 		return;
56195c97b59SCorey Minyard 
56295c97b59SCorey Minyard 	if (irq_on)
56395c97b59SCorey Minyard 		smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG,
56495c97b59SCorey Minyard 				     IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
56595c97b59SCorey Minyard 	else
56695c97b59SCorey Minyard 		smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG, 0);
56795c97b59SCorey Minyard }
56895c97b59SCorey Minyard 
5691da177e4SLinus Torvalds static void handle_transaction_done(struct smi_info *smi_info)
5701da177e4SLinus Torvalds {
5711da177e4SLinus Torvalds 	struct ipmi_smi_msg *msg;
5721da177e4SLinus Torvalds 
573f93aae9fSJohn Stultz 	debug_timestamp("Done");
5741da177e4SLinus Torvalds 	switch (smi_info->si_state) {
5751da177e4SLinus Torvalds 	case SI_NORMAL:
5761da177e4SLinus Torvalds 		if (!smi_info->curr_msg)
5771da177e4SLinus Torvalds 			break;
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds 		smi_info->curr_msg->rsp_size
5801da177e4SLinus Torvalds 			= smi_info->handlers->get_result(
5811da177e4SLinus Torvalds 				smi_info->si_sm,
5821da177e4SLinus Torvalds 				smi_info->curr_msg->rsp,
5831da177e4SLinus Torvalds 				IPMI_MAX_MSG_LENGTH);
5841da177e4SLinus Torvalds 
585c305e3d3SCorey Minyard 		/*
586c305e3d3SCorey Minyard 		 * Do this here becase deliver_recv_msg() releases the
587c305e3d3SCorey Minyard 		 * lock, and a new message can be put in during the
588c305e3d3SCorey Minyard 		 * time the lock is released.
589c305e3d3SCorey Minyard 		 */
5901da177e4SLinus Torvalds 		msg = smi_info->curr_msg;
5911da177e4SLinus Torvalds 		smi_info->curr_msg = NULL;
5921da177e4SLinus Torvalds 		deliver_recv_msg(smi_info, msg);
5931da177e4SLinus Torvalds 		break;
5941da177e4SLinus Torvalds 
5951da177e4SLinus Torvalds 	case SI_GETTING_FLAGS:
5961da177e4SLinus Torvalds 	{
5971da177e4SLinus Torvalds 		unsigned char msg[4];
5981da177e4SLinus Torvalds 		unsigned int  len;
5991da177e4SLinus Torvalds 
6001da177e4SLinus Torvalds 		/* We got the flags from the SMI, now handle them. */
6011da177e4SLinus Torvalds 		len = smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
6021da177e4SLinus Torvalds 		if (msg[2] != 0) {
603c305e3d3SCorey Minyard 			/* Error fetching flags, just give up for now. */
6041da177e4SLinus Torvalds 			smi_info->si_state = SI_NORMAL;
6051da177e4SLinus Torvalds 		} else if (len < 4) {
606c305e3d3SCorey Minyard 			/*
607c305e3d3SCorey Minyard 			 * Hmm, no flags.  That's technically illegal, but
608c305e3d3SCorey Minyard 			 * don't use uninitialized data.
609c305e3d3SCorey Minyard 			 */
6101da177e4SLinus Torvalds 			smi_info->si_state = SI_NORMAL;
6111da177e4SLinus Torvalds 		} else {
6121da177e4SLinus Torvalds 			smi_info->msg_flags = msg[3];
6131da177e4SLinus Torvalds 			handle_flags(smi_info);
6141da177e4SLinus Torvalds 		}
6151da177e4SLinus Torvalds 		break;
6161da177e4SLinus Torvalds 	}
6171da177e4SLinus Torvalds 
6181da177e4SLinus Torvalds 	case SI_CLEARING_FLAGS:
6191da177e4SLinus Torvalds 	{
6201da177e4SLinus Torvalds 		unsigned char msg[3];
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds 		/* We cleared the flags. */
6231da177e4SLinus Torvalds 		smi_info->handlers->get_result(smi_info->si_sm, msg, 3);
6241da177e4SLinus Torvalds 		if (msg[2] != 0) {
6251da177e4SLinus Torvalds 			/* Error clearing flags */
626910840f2SCorey Minyard 			dev_warn(smi_info->io.dev,
627279fbd0cSMyron Stowe 				 "Error clearing flags: %2.2x\n", msg[2]);
6281da177e4SLinus Torvalds 		}
6291da177e4SLinus Torvalds 		smi_info->si_state = SI_NORMAL;
6301da177e4SLinus Torvalds 		break;
6311da177e4SLinus Torvalds 	}
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds 	case SI_GETTING_EVENTS:
6341da177e4SLinus Torvalds 	{
6351da177e4SLinus Torvalds 		smi_info->curr_msg->rsp_size
6361da177e4SLinus Torvalds 			= smi_info->handlers->get_result(
6371da177e4SLinus Torvalds 				smi_info->si_sm,
6381da177e4SLinus Torvalds 				smi_info->curr_msg->rsp,
6391da177e4SLinus Torvalds 				IPMI_MAX_MSG_LENGTH);
6401da177e4SLinus Torvalds 
641c305e3d3SCorey Minyard 		/*
642c305e3d3SCorey Minyard 		 * Do this here becase deliver_recv_msg() releases the
643c305e3d3SCorey Minyard 		 * lock, and a new message can be put in during the
644c305e3d3SCorey Minyard 		 * time the lock is released.
645c305e3d3SCorey Minyard 		 */
6461da177e4SLinus Torvalds 		msg = smi_info->curr_msg;
6471da177e4SLinus Torvalds 		smi_info->curr_msg = NULL;
6481da177e4SLinus Torvalds 		if (msg->rsp[2] != 0) {
6491da177e4SLinus Torvalds 			/* Error getting event, probably done. */
6501da177e4SLinus Torvalds 			msg->done(msg);
6511da177e4SLinus Torvalds 
6521da177e4SLinus Torvalds 			/* Take off the event flag. */
6531da177e4SLinus Torvalds 			smi_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
6541da177e4SLinus Torvalds 			handle_flags(smi_info);
6551da177e4SLinus Torvalds 		} else {
65664959e2dSCorey Minyard 			smi_inc_stat(smi_info, events);
6571da177e4SLinus Torvalds 
658c305e3d3SCorey Minyard 			/*
659c305e3d3SCorey Minyard 			 * Do this before we deliver the message
660c305e3d3SCorey Minyard 			 * because delivering the message releases the
661c305e3d3SCorey Minyard 			 * lock and something else can mess with the
662c305e3d3SCorey Minyard 			 * state.
663c305e3d3SCorey Minyard 			 */
6641da177e4SLinus Torvalds 			handle_flags(smi_info);
6651da177e4SLinus Torvalds 
6661da177e4SLinus Torvalds 			deliver_recv_msg(smi_info, msg);
6671da177e4SLinus Torvalds 		}
6681da177e4SLinus Torvalds 		break;
6691da177e4SLinus Torvalds 	}
6701da177e4SLinus Torvalds 
6711da177e4SLinus Torvalds 	case SI_GETTING_MESSAGES:
6721da177e4SLinus Torvalds 	{
6731da177e4SLinus Torvalds 		smi_info->curr_msg->rsp_size
6741da177e4SLinus Torvalds 			= smi_info->handlers->get_result(
6751da177e4SLinus Torvalds 				smi_info->si_sm,
6761da177e4SLinus Torvalds 				smi_info->curr_msg->rsp,
6771da177e4SLinus Torvalds 				IPMI_MAX_MSG_LENGTH);
6781da177e4SLinus Torvalds 
679c305e3d3SCorey Minyard 		/*
680c305e3d3SCorey Minyard 		 * Do this here becase deliver_recv_msg() releases the
681c305e3d3SCorey Minyard 		 * lock, and a new message can be put in during the
682c305e3d3SCorey Minyard 		 * time the lock is released.
683c305e3d3SCorey Minyard 		 */
6841da177e4SLinus Torvalds 		msg = smi_info->curr_msg;
6851da177e4SLinus Torvalds 		smi_info->curr_msg = NULL;
6861da177e4SLinus Torvalds 		if (msg->rsp[2] != 0) {
6871da177e4SLinus Torvalds 			/* Error getting event, probably done. */
6881da177e4SLinus Torvalds 			msg->done(msg);
6891da177e4SLinus Torvalds 
6901da177e4SLinus Torvalds 			/* Take off the msg flag. */
6911da177e4SLinus Torvalds 			smi_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
6921da177e4SLinus Torvalds 			handle_flags(smi_info);
6931da177e4SLinus Torvalds 		} else {
69464959e2dSCorey Minyard 			smi_inc_stat(smi_info, incoming_messages);
6951da177e4SLinus Torvalds 
696c305e3d3SCorey Minyard 			/*
697c305e3d3SCorey Minyard 			 * Do this before we deliver the message
698c305e3d3SCorey Minyard 			 * because delivering the message releases the
699c305e3d3SCorey Minyard 			 * lock and something else can mess with the
700c305e3d3SCorey Minyard 			 * state.
701c305e3d3SCorey Minyard 			 */
7021da177e4SLinus Torvalds 			handle_flags(smi_info);
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds 			deliver_recv_msg(smi_info, msg);
7051da177e4SLinus Torvalds 		}
7061da177e4SLinus Torvalds 		break;
7071da177e4SLinus Torvalds 	}
7081da177e4SLinus Torvalds 
709d9b7e4f7SCorey Minyard 	case SI_CHECKING_ENABLES:
7101da177e4SLinus Torvalds 	{
7111da177e4SLinus Torvalds 		unsigned char msg[4];
712d9b7e4f7SCorey Minyard 		u8 enables;
71395c97b59SCorey Minyard 		bool irq_on;
7141da177e4SLinus Torvalds 
7151da177e4SLinus Torvalds 		/* We got the flags from the SMI, now handle them. */
7161da177e4SLinus Torvalds 		smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
7171da177e4SLinus Torvalds 		if (msg[2] != 0) {
718910840f2SCorey Minyard 			dev_warn(smi_info->io.dev,
7190849bfecSCorey Minyard 				 "Couldn't get irq info: %x.\n", msg[2]);
720910840f2SCorey Minyard 			dev_warn(smi_info->io.dev,
7210849bfecSCorey Minyard 				 "Maybe ok, but ipmi might run very slowly.\n");
7221da177e4SLinus Torvalds 			smi_info->si_state = SI_NORMAL;
723d9b7e4f7SCorey Minyard 			break;
724d9b7e4f7SCorey Minyard 		}
72595c97b59SCorey Minyard 		enables = current_global_enables(smi_info, 0, &irq_on);
726910840f2SCorey Minyard 		if (smi_info->io.si_type == SI_BT)
72795c97b59SCorey Minyard 			/* BT has its own interrupt enable bit. */
72895c97b59SCorey Minyard 			check_bt_irq(smi_info, irq_on);
729d9b7e4f7SCorey Minyard 		if (enables != (msg[3] & GLOBAL_ENABLES_MASK)) {
730d9b7e4f7SCorey Minyard 			/* Enables are not correct, fix them. */
7311da177e4SLinus Torvalds 			msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
7321da177e4SLinus Torvalds 			msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
733d9b7e4f7SCorey Minyard 			msg[2] = enables | (msg[3] & ~GLOBAL_ENABLES_MASK);
7341da177e4SLinus Torvalds 			smi_info->handlers->start_transaction(
7351da177e4SLinus Torvalds 				smi_info->si_sm, msg, 3);
736d9b7e4f7SCorey Minyard 			smi_info->si_state = SI_SETTING_ENABLES;
737d9b7e4f7SCorey Minyard 		} else if (smi_info->supports_event_msg_buff) {
738d9b7e4f7SCorey Minyard 			smi_info->curr_msg = ipmi_alloc_smi_msg();
739d9b7e4f7SCorey Minyard 			if (!smi_info->curr_msg) {
740ee6cd5f8SCorey Minyard 				smi_info->si_state = SI_NORMAL;
741d9b7e4f7SCorey Minyard 				break;
742d9b7e4f7SCorey Minyard 			}
7435ac7b2fcSCorey Minyard 			start_getting_events(smi_info);
744ee6cd5f8SCorey Minyard 		} else {
745d9b7e4f7SCorey Minyard 			smi_info->si_state = SI_NORMAL;
746ee6cd5f8SCorey Minyard 		}
747ee6cd5f8SCorey Minyard 		break;
748ee6cd5f8SCorey Minyard 	}
749ee6cd5f8SCorey Minyard 
750d9b7e4f7SCorey Minyard 	case SI_SETTING_ENABLES:
751ee6cd5f8SCorey Minyard 	{
752ee6cd5f8SCorey Minyard 		unsigned char msg[4];
753ee6cd5f8SCorey Minyard 
754ee6cd5f8SCorey Minyard 		smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
755d9b7e4f7SCorey Minyard 		if (msg[2] != 0)
756910840f2SCorey Minyard 			dev_warn(smi_info->io.dev,
757d9b7e4f7SCorey Minyard 				 "Could not set the global enables: 0x%x.\n",
758d9b7e4f7SCorey Minyard 				 msg[2]);
759d9b7e4f7SCorey Minyard 
760d9b7e4f7SCorey Minyard 		if (smi_info->supports_event_msg_buff) {
761d9b7e4f7SCorey Minyard 			smi_info->curr_msg = ipmi_alloc_smi_msg();
762d9b7e4f7SCorey Minyard 			if (!smi_info->curr_msg) {
763ee6cd5f8SCorey Minyard 				smi_info->si_state = SI_NORMAL;
764ee6cd5f8SCorey Minyard 				break;
765ee6cd5f8SCorey Minyard 			}
7665ac7b2fcSCorey Minyard 			start_getting_events(smi_info);
767d9b7e4f7SCorey Minyard 		} else {
768d9b7e4f7SCorey Minyard 			smi_info->si_state = SI_NORMAL;
769d9b7e4f7SCorey Minyard 		}
770d9b7e4f7SCorey Minyard 		break;
771d9b7e4f7SCorey Minyard 	}
7721da177e4SLinus Torvalds 	}
7731da177e4SLinus Torvalds }
7741da177e4SLinus Torvalds 
775c305e3d3SCorey Minyard /*
776c305e3d3SCorey Minyard  * Called on timeouts and events.  Timeouts should pass the elapsed
777c305e3d3SCorey Minyard  * time, interrupts should pass in zero.  Must be called with
778c305e3d3SCorey Minyard  * si_lock held and interrupts disabled.
779c305e3d3SCorey Minyard  */
7801da177e4SLinus Torvalds static enum si_sm_result smi_event_handler(struct smi_info *smi_info,
7811da177e4SLinus Torvalds 					   int time)
7821da177e4SLinus Torvalds {
7831da177e4SLinus Torvalds 	enum si_sm_result si_sm_result;
7841da177e4SLinus Torvalds 
7851da177e4SLinus Torvalds restart:
786c305e3d3SCorey Minyard 	/*
787c305e3d3SCorey Minyard 	 * There used to be a loop here that waited a little while
788c305e3d3SCorey Minyard 	 * (around 25us) before giving up.  That turned out to be
789c305e3d3SCorey Minyard 	 * pointless, the minimum delays I was seeing were in the 300us
790c305e3d3SCorey Minyard 	 * range, which is far too long to wait in an interrupt.  So
791c305e3d3SCorey Minyard 	 * we just run until the state machine tells us something
792c305e3d3SCorey Minyard 	 * happened or it needs a delay.
793c305e3d3SCorey Minyard 	 */
7941da177e4SLinus Torvalds 	si_sm_result = smi_info->handlers->event(smi_info->si_sm, time);
7951da177e4SLinus Torvalds 	time = 0;
7961da177e4SLinus Torvalds 	while (si_sm_result == SI_SM_CALL_WITHOUT_DELAY)
7971da177e4SLinus Torvalds 		si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
7981da177e4SLinus Torvalds 
799c305e3d3SCorey Minyard 	if (si_sm_result == SI_SM_TRANSACTION_COMPLETE) {
80064959e2dSCorey Minyard 		smi_inc_stat(smi_info, complete_transactions);
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds 		handle_transaction_done(smi_info);
803d9dffd2aSCorey Minyard 		goto restart;
804c305e3d3SCorey Minyard 	} else if (si_sm_result == SI_SM_HOSED) {
80564959e2dSCorey Minyard 		smi_inc_stat(smi_info, hosed_count);
8061da177e4SLinus Torvalds 
807c305e3d3SCorey Minyard 		/*
808c305e3d3SCorey Minyard 		 * Do the before return_hosed_msg, because that
809c305e3d3SCorey Minyard 		 * releases the lock.
810c305e3d3SCorey Minyard 		 */
8111da177e4SLinus Torvalds 		smi_info->si_state = SI_NORMAL;
8121da177e4SLinus Torvalds 		if (smi_info->curr_msg != NULL) {
813c305e3d3SCorey Minyard 			/*
814c305e3d3SCorey Minyard 			 * If we were handling a user message, format
815c305e3d3SCorey Minyard 			 * a response to send to the upper layer to
816c305e3d3SCorey Minyard 			 * tell it about the error.
817c305e3d3SCorey Minyard 			 */
8184d7cbac7SCorey Minyard 			return_hosed_msg(smi_info, IPMI_ERR_UNSPECIFIED);
8191da177e4SLinus Torvalds 		}
820d9dffd2aSCorey Minyard 		goto restart;
8211da177e4SLinus Torvalds 	}
8221da177e4SLinus Torvalds 
8234ea18425SCorey Minyard 	/*
8244ea18425SCorey Minyard 	 * We prefer handling attn over new messages.  But don't do
8254ea18425SCorey Minyard 	 * this if there is not yet an upper layer to handle anything.
8264ea18425SCorey Minyard 	 */
827a8df150cSCorey Minyard 	if (likely(smi_info->intf) &&
828a8df150cSCorey Minyard 	    (si_sm_result == SI_SM_ATTN || smi_info->got_attn)) {
8291da177e4SLinus Torvalds 		unsigned char msg[2];
8301da177e4SLinus Torvalds 
831a8df150cSCorey Minyard 		if (smi_info->si_state != SI_NORMAL) {
832a8df150cSCorey Minyard 			/*
833a8df150cSCorey Minyard 			 * We got an ATTN, but we are doing something else.
834a8df150cSCorey Minyard 			 * Handle the ATTN later.
835a8df150cSCorey Minyard 			 */
836a8df150cSCorey Minyard 			smi_info->got_attn = true;
837a8df150cSCorey Minyard 		} else {
838a8df150cSCorey Minyard 			smi_info->got_attn = false;
83964959e2dSCorey Minyard 			smi_inc_stat(smi_info, attentions);
8401da177e4SLinus Torvalds 
841c305e3d3SCorey Minyard 			/*
842c305e3d3SCorey Minyard 			 * Got a attn, send down a get message flags to see
843c305e3d3SCorey Minyard 			 * what's causing it.  It would be better to handle
844c305e3d3SCorey Minyard 			 * this in the upper layer, but due to the way
845c305e3d3SCorey Minyard 			 * interrupts work with the SMI, that's not really
846c305e3d3SCorey Minyard 			 * possible.
847c305e3d3SCorey Minyard 			 */
8481da177e4SLinus Torvalds 			msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
8491da177e4SLinus Torvalds 			msg[1] = IPMI_GET_MSG_FLAGS_CMD;
8501da177e4SLinus Torvalds 
8510cfec916SCorey Minyard 			start_new_msg(smi_info, msg, 2);
8521da177e4SLinus Torvalds 			smi_info->si_state = SI_GETTING_FLAGS;
8531da177e4SLinus Torvalds 			goto restart;
8541da177e4SLinus Torvalds 		}
855a8df150cSCorey Minyard 	}
8561da177e4SLinus Torvalds 
8571da177e4SLinus Torvalds 	/* If we are currently idle, try to start the next message. */
8581da177e4SLinus Torvalds 	if (si_sm_result == SI_SM_IDLE) {
85964959e2dSCorey Minyard 		smi_inc_stat(smi_info, idles);
8601da177e4SLinus Torvalds 
8611da177e4SLinus Torvalds 		si_sm_result = start_next_msg(smi_info);
8621da177e4SLinus Torvalds 		if (si_sm_result != SI_SM_IDLE)
8631da177e4SLinus Torvalds 			goto restart;
8641da177e4SLinus Torvalds 	}
8651da177e4SLinus Torvalds 
8661da177e4SLinus Torvalds 	if ((si_sm_result == SI_SM_IDLE)
867c305e3d3SCorey Minyard 	    && (atomic_read(&smi_info->req_events))) {
868c305e3d3SCorey Minyard 		/*
869c305e3d3SCorey Minyard 		 * We are idle and the upper layer requested that I fetch
870c305e3d3SCorey Minyard 		 * events, so do so.
871c305e3d3SCorey Minyard 		 */
8721da177e4SLinus Torvalds 		atomic_set(&smi_info->req_events, 0);
87355162fb1SCorey Minyard 
874d9b7e4f7SCorey Minyard 		/*
875d9b7e4f7SCorey Minyard 		 * Take this opportunity to check the interrupt and
876d9b7e4f7SCorey Minyard 		 * message enable state for the BMC.  The BMC can be
877d9b7e4f7SCorey Minyard 		 * asynchronously reset, and may thus get interrupts
878d9b7e4f7SCorey Minyard 		 * disable and messages disabled.
879d9b7e4f7SCorey Minyard 		 */
880910840f2SCorey Minyard 		if (smi_info->supports_event_msg_buff || smi_info->io.irq) {
8810cfec916SCorey Minyard 			start_check_enables(smi_info, true);
882d9b7e4f7SCorey Minyard 		} else {
883d9b7e4f7SCorey Minyard 			smi_info->curr_msg = alloc_msg_handle_irq(smi_info);
88455162fb1SCorey Minyard 			if (!smi_info->curr_msg)
88555162fb1SCorey Minyard 				goto out;
88655162fb1SCorey Minyard 
887d9b7e4f7SCorey Minyard 			start_getting_events(smi_info);
888d9b7e4f7SCorey Minyard 		}
8891da177e4SLinus Torvalds 		goto restart;
8901da177e4SLinus Torvalds 	}
891314ef52fSCorey Minyard 
892314ef52fSCorey Minyard 	if (si_sm_result == SI_SM_IDLE && smi_info->timer_running) {
893314ef52fSCorey Minyard 		/* Ok it if fails, the timer will just go off. */
894314ef52fSCorey Minyard 		if (del_timer(&smi_info->si_timer))
895314ef52fSCorey Minyard 			smi_info->timer_running = false;
896314ef52fSCorey Minyard 	}
897314ef52fSCorey Minyard 
89855162fb1SCorey Minyard out:
8991da177e4SLinus Torvalds 	return si_sm_result;
9001da177e4SLinus Torvalds }
9011da177e4SLinus Torvalds 
90289986496SCorey Minyard static void check_start_timer_thread(struct smi_info *smi_info)
90389986496SCorey Minyard {
90489986496SCorey Minyard 	if (smi_info->si_state == SI_NORMAL && smi_info->curr_msg == NULL) {
90589986496SCorey Minyard 		smi_mod_timer(smi_info, jiffies + SI_TIMEOUT_JIFFIES);
90689986496SCorey Minyard 
90789986496SCorey Minyard 		if (smi_info->thread)
90889986496SCorey Minyard 			wake_up_process(smi_info->thread);
90989986496SCorey Minyard 
91089986496SCorey Minyard 		start_next_msg(smi_info);
91189986496SCorey Minyard 		smi_event_handler(smi_info, 0);
91289986496SCorey Minyard 	}
91389986496SCorey Minyard }
91489986496SCorey Minyard 
91582802f96SHidehiro Kawai static void flush_messages(void *send_info)
916e45361d7SHidehiro Kawai {
91782802f96SHidehiro Kawai 	struct smi_info *smi_info = send_info;
918e45361d7SHidehiro Kawai 	enum si_sm_result result;
919e45361d7SHidehiro Kawai 
920e45361d7SHidehiro Kawai 	/*
921e45361d7SHidehiro Kawai 	 * Currently, this function is called only in run-to-completion
922e45361d7SHidehiro Kawai 	 * mode.  This means we are single-threaded, no need for locks.
923e45361d7SHidehiro Kawai 	 */
924e45361d7SHidehiro Kawai 	result = smi_event_handler(smi_info, 0);
925e45361d7SHidehiro Kawai 	while (result != SI_SM_IDLE) {
926e45361d7SHidehiro Kawai 		udelay(SI_SHORT_TIMEOUT_USEC);
927e45361d7SHidehiro Kawai 		result = smi_event_handler(smi_info, SI_SHORT_TIMEOUT_USEC);
928e45361d7SHidehiro Kawai 	}
929e45361d7SHidehiro Kawai }
930e45361d7SHidehiro Kawai 
9311da177e4SLinus Torvalds static void sender(void                *send_info,
93299ab32f3SCorey Minyard 		   struct ipmi_smi_msg *msg)
9331da177e4SLinus Torvalds {
9341da177e4SLinus Torvalds 	struct smi_info   *smi_info = send_info;
9351da177e4SLinus Torvalds 	unsigned long     flags;
9361da177e4SLinus Torvalds 
937f93aae9fSJohn Stultz 	debug_timestamp("Enqueue");
9381da177e4SLinus Torvalds 
9391da177e4SLinus Torvalds 	if (smi_info->run_to_completion) {
940bda4c30aSCorey Minyard 		/*
94182802f96SHidehiro Kawai 		 * If we are running to completion, start it.  Upper
94282802f96SHidehiro Kawai 		 * layer will call flush_messages to clear it out.
943bda4c30aSCorey Minyard 		 */
9449f812704SHidehiro Kawai 		smi_info->waiting_msg = msg;
9451da177e4SLinus Torvalds 		return;
9461da177e4SLinus Torvalds 	}
9471da177e4SLinus Torvalds 
948f60adf42SCorey Minyard 	spin_lock_irqsave(&smi_info->si_lock, flags);
9491d86e29bSCorey Minyard 	/*
9501d86e29bSCorey Minyard 	 * The following two lines don't need to be under the lock for
9511d86e29bSCorey Minyard 	 * the lock's sake, but they do need SMP memory barriers to
9521d86e29bSCorey Minyard 	 * avoid getting things out of order.  We are already claiming
9531d86e29bSCorey Minyard 	 * the lock, anyway, so just do it under the lock to avoid the
9541d86e29bSCorey Minyard 	 * ordering problem.
9551d86e29bSCorey Minyard 	 */
9561d86e29bSCorey Minyard 	BUG_ON(smi_info->waiting_msg);
9571d86e29bSCorey Minyard 	smi_info->waiting_msg = msg;
95889986496SCorey Minyard 	check_start_timer_thread(smi_info);
959bda4c30aSCorey Minyard 	spin_unlock_irqrestore(&smi_info->si_lock, flags);
9601da177e4SLinus Torvalds }
9611da177e4SLinus Torvalds 
9627aefac26SCorey Minyard static void set_run_to_completion(void *send_info, bool i_run_to_completion)
9631da177e4SLinus Torvalds {
9641da177e4SLinus Torvalds 	struct smi_info   *smi_info = send_info;
9651da177e4SLinus Torvalds 
9661da177e4SLinus Torvalds 	smi_info->run_to_completion = i_run_to_completion;
967e45361d7SHidehiro Kawai 	if (i_run_to_completion)
968e45361d7SHidehiro Kawai 		flush_messages(smi_info);
9691da177e4SLinus Torvalds }
9701da177e4SLinus Torvalds 
971ae74e823SMartin Wilck /*
972ae74e823SMartin Wilck  * Use -1 in the nsec value of the busy waiting timespec to tell that
973ae74e823SMartin Wilck  * we are spinning in kipmid looking for something and not delaying
974ae74e823SMartin Wilck  * between checks
975ae74e823SMartin Wilck  */
97648862ea2SJohn Stultz static inline void ipmi_si_set_not_busy(struct timespec64 *ts)
977ae74e823SMartin Wilck {
978ae74e823SMartin Wilck 	ts->tv_nsec = -1;
979ae74e823SMartin Wilck }
98048862ea2SJohn Stultz static inline int ipmi_si_is_busy(struct timespec64 *ts)
981ae74e823SMartin Wilck {
982ae74e823SMartin Wilck 	return ts->tv_nsec != -1;
983ae74e823SMartin Wilck }
984ae74e823SMartin Wilck 
985cc4cbe90SArnd Bergmann static inline int ipmi_thread_busy_wait(enum si_sm_result smi_result,
986ae74e823SMartin Wilck 					const struct smi_info *smi_info,
98748862ea2SJohn Stultz 					struct timespec64 *busy_until)
988ae74e823SMartin Wilck {
989ae74e823SMartin Wilck 	unsigned int max_busy_us = 0;
990ae74e823SMartin Wilck 
991ae74e823SMartin Wilck 	if (smi_info->intf_num < num_max_busy_us)
992ae74e823SMartin Wilck 		max_busy_us = kipmid_max_busy_us[smi_info->intf_num];
993ae74e823SMartin Wilck 	if (max_busy_us == 0 || smi_result != SI_SM_CALL_WITH_DELAY)
994ae74e823SMartin Wilck 		ipmi_si_set_not_busy(busy_until);
995ae74e823SMartin Wilck 	else if (!ipmi_si_is_busy(busy_until)) {
99648862ea2SJohn Stultz 		getnstimeofday64(busy_until);
99748862ea2SJohn Stultz 		timespec64_add_ns(busy_until, max_busy_us*NSEC_PER_USEC);
998ae74e823SMartin Wilck 	} else {
99948862ea2SJohn Stultz 		struct timespec64 now;
100048862ea2SJohn Stultz 
100148862ea2SJohn Stultz 		getnstimeofday64(&now);
100248862ea2SJohn Stultz 		if (unlikely(timespec64_compare(&now, busy_until) > 0)) {
1003ae74e823SMartin Wilck 			ipmi_si_set_not_busy(busy_until);
1004ae74e823SMartin Wilck 			return 0;
1005ae74e823SMartin Wilck 		}
1006ae74e823SMartin Wilck 	}
1007ae74e823SMartin Wilck 	return 1;
1008ae74e823SMartin Wilck }
1009ae74e823SMartin Wilck 
1010ae74e823SMartin Wilck 
1011ae74e823SMartin Wilck /*
1012ae74e823SMartin Wilck  * A busy-waiting loop for speeding up IPMI operation.
1013ae74e823SMartin Wilck  *
1014ae74e823SMartin Wilck  * Lousy hardware makes this hard.  This is only enabled for systems
1015ae74e823SMartin Wilck  * that are not BT and do not have interrupts.  It starts spinning
1016ae74e823SMartin Wilck  * when an operation is complete or until max_busy tells it to stop
1017ae74e823SMartin Wilck  * (if that is enabled).  See the paragraph on kimid_max_busy_us in
1018ae74e823SMartin Wilck  * Documentation/IPMI.txt for details.
1019ae74e823SMartin Wilck  */
1020a9a2c44fSCorey Minyard static int ipmi_thread(void *data)
1021a9a2c44fSCorey Minyard {
1022a9a2c44fSCorey Minyard 	struct smi_info *smi_info = data;
1023e9a705a0SMatt Domsch 	unsigned long flags;
1024a9a2c44fSCorey Minyard 	enum si_sm_result smi_result;
102548862ea2SJohn Stultz 	struct timespec64 busy_until;
1026a9a2c44fSCorey Minyard 
1027ae74e823SMartin Wilck 	ipmi_si_set_not_busy(&busy_until);
10288698a745SDongsheng Yang 	set_user_nice(current, MAX_NICE);
1029e9a705a0SMatt Domsch 	while (!kthread_should_stop()) {
1030ae74e823SMartin Wilck 		int busy_wait;
1031ae74e823SMartin Wilck 
1032a9a2c44fSCorey Minyard 		spin_lock_irqsave(&(smi_info->si_lock), flags);
1033a9a2c44fSCorey Minyard 		smi_result = smi_event_handler(smi_info, 0);
103448e8ac29SBodo Stroesser 
103548e8ac29SBodo Stroesser 		/*
103648e8ac29SBodo Stroesser 		 * If the driver is doing something, there is a possible
103748e8ac29SBodo Stroesser 		 * race with the timer.  If the timer handler see idle,
103848e8ac29SBodo Stroesser 		 * and the thread here sees something else, the timer
103948e8ac29SBodo Stroesser 		 * handler won't restart the timer even though it is
104048e8ac29SBodo Stroesser 		 * required.  So start it here if necessary.
104148e8ac29SBodo Stroesser 		 */
104248e8ac29SBodo Stroesser 		if (smi_result != SI_SM_IDLE && !smi_info->timer_running)
104348e8ac29SBodo Stroesser 			smi_mod_timer(smi_info, jiffies + SI_TIMEOUT_JIFFIES);
104448e8ac29SBodo Stroesser 
1045a9a2c44fSCorey Minyard 		spin_unlock_irqrestore(&(smi_info->si_lock), flags);
1046ae74e823SMartin Wilck 		busy_wait = ipmi_thread_busy_wait(smi_result, smi_info,
1047ae74e823SMartin Wilck 						  &busy_until);
1048c305e3d3SCorey Minyard 		if (smi_result == SI_SM_CALL_WITHOUT_DELAY)
1049c305e3d3SCorey Minyard 			; /* do nothing */
1050ae74e823SMartin Wilck 		else if (smi_result == SI_SM_CALL_WITH_DELAY && busy_wait)
105133979734Sakpm@osdl.org 			schedule();
105289986496SCorey Minyard 		else if (smi_result == SI_SM_IDLE) {
105389986496SCorey Minyard 			if (atomic_read(&smi_info->need_watch)) {
10543326f4f2SMatthew Garrett 				schedule_timeout_interruptible(100);
105589986496SCorey Minyard 			} else {
105689986496SCorey Minyard 				/* Wait to be woken up when we are needed. */
105789986496SCorey Minyard 				__set_current_state(TASK_INTERRUPTIBLE);
105889986496SCorey Minyard 				schedule();
105989986496SCorey Minyard 			}
106089986496SCorey Minyard 		} else
10618d1f66dcSMartin Wilck 			schedule_timeout_interruptible(1);
1062a9a2c44fSCorey Minyard 	}
1063a9a2c44fSCorey Minyard 	return 0;
1064a9a2c44fSCorey Minyard }
1065a9a2c44fSCorey Minyard 
1066a9a2c44fSCorey Minyard 
10671da177e4SLinus Torvalds static void poll(void *send_info)
10681da177e4SLinus Torvalds {
10691da177e4SLinus Torvalds 	struct smi_info *smi_info = send_info;
1070f60adf42SCorey Minyard 	unsigned long flags = 0;
10717aefac26SCorey Minyard 	bool run_to_completion = smi_info->run_to_completion;
10721da177e4SLinus Torvalds 
107315c62e10SCorey Minyard 	/*
107415c62e10SCorey Minyard 	 * Make sure there is some delay in the poll loop so we can
107515c62e10SCorey Minyard 	 * drive time forward and timeout things.
107615c62e10SCorey Minyard 	 */
107715c62e10SCorey Minyard 	udelay(10);
1078f60adf42SCorey Minyard 	if (!run_to_completion)
1079fcfa4724SCorey Minyard 		spin_lock_irqsave(&smi_info->si_lock, flags);
108015c62e10SCorey Minyard 	smi_event_handler(smi_info, 10);
1081f60adf42SCorey Minyard 	if (!run_to_completion)
1082fcfa4724SCorey Minyard 		spin_unlock_irqrestore(&smi_info->si_lock, flags);
10831da177e4SLinus Torvalds }
10841da177e4SLinus Torvalds 
10851da177e4SLinus Torvalds static void request_events(void *send_info)
10861da177e4SLinus Torvalds {
10871da177e4SLinus Torvalds 	struct smi_info *smi_info = send_info;
10881da177e4SLinus Torvalds 
1089b874b985SCorey Minyard 	if (!smi_info->has_event_buffer)
1090b361e27bSCorey Minyard 		return;
1091b361e27bSCorey Minyard 
10921da177e4SLinus Torvalds 	atomic_set(&smi_info->req_events, 1);
10931da177e4SLinus Torvalds }
10941da177e4SLinus Torvalds 
10957aefac26SCorey Minyard static void set_need_watch(void *send_info, bool enable)
109689986496SCorey Minyard {
109789986496SCorey Minyard 	struct smi_info *smi_info = send_info;
109889986496SCorey Minyard 	unsigned long flags;
109989986496SCorey Minyard 
110089986496SCorey Minyard 	atomic_set(&smi_info->need_watch, enable);
110189986496SCorey Minyard 	spin_lock_irqsave(&smi_info->si_lock, flags);
110289986496SCorey Minyard 	check_start_timer_thread(smi_info);
110389986496SCorey Minyard 	spin_unlock_irqrestore(&smi_info->si_lock, flags);
110489986496SCorey Minyard }
110589986496SCorey Minyard 
11061da177e4SLinus Torvalds static void smi_timeout(unsigned long data)
11071da177e4SLinus Torvalds {
11081da177e4SLinus Torvalds 	struct smi_info   *smi_info = (struct smi_info *) data;
11091da177e4SLinus Torvalds 	enum si_sm_result smi_result;
11101da177e4SLinus Torvalds 	unsigned long     flags;
11111da177e4SLinus Torvalds 	unsigned long     jiffies_now;
1112c4edff1cSCorey Minyard 	long              time_diff;
11133326f4f2SMatthew Garrett 	long		  timeout;
11141da177e4SLinus Torvalds 
11151da177e4SLinus Torvalds 	spin_lock_irqsave(&(smi_info->si_lock), flags);
1116f93aae9fSJohn Stultz 	debug_timestamp("Timer");
1117f93aae9fSJohn Stultz 
11181da177e4SLinus Torvalds 	jiffies_now = jiffies;
1119c4edff1cSCorey Minyard 	time_diff = (((long)jiffies_now - (long)smi_info->last_timeout_jiffies)
11201da177e4SLinus Torvalds 		     * SI_USEC_PER_JIFFY);
11211da177e4SLinus Torvalds 	smi_result = smi_event_handler(smi_info, time_diff);
11221da177e4SLinus Torvalds 
1123910840f2SCorey Minyard 	if ((smi_info->io.irq) && (!smi_info->interrupt_disabled)) {
11241da177e4SLinus Torvalds 		/* Running with interrupts, only do long timeouts. */
11253326f4f2SMatthew Garrett 		timeout = jiffies + SI_TIMEOUT_JIFFIES;
112664959e2dSCorey Minyard 		smi_inc_stat(smi_info, long_timeouts);
11273326f4f2SMatthew Garrett 		goto do_mod_timer;
11281da177e4SLinus Torvalds 	}
11291da177e4SLinus Torvalds 
1130c305e3d3SCorey Minyard 	/*
1131c305e3d3SCorey Minyard 	 * If the state machine asks for a short delay, then shorten
1132c305e3d3SCorey Minyard 	 * the timer timeout.
1133c305e3d3SCorey Minyard 	 */
11341da177e4SLinus Torvalds 	if (smi_result == SI_SM_CALL_WITH_DELAY) {
113564959e2dSCorey Minyard 		smi_inc_stat(smi_info, short_timeouts);
11363326f4f2SMatthew Garrett 		timeout = jiffies + 1;
11371da177e4SLinus Torvalds 	} else {
113864959e2dSCorey Minyard 		smi_inc_stat(smi_info, long_timeouts);
11393326f4f2SMatthew Garrett 		timeout = jiffies + SI_TIMEOUT_JIFFIES;
11401da177e4SLinus Torvalds 	}
11411da177e4SLinus Torvalds 
11423326f4f2SMatthew Garrett do_mod_timer:
11433326f4f2SMatthew Garrett 	if (smi_result != SI_SM_IDLE)
114448e8ac29SBodo Stroesser 		smi_mod_timer(smi_info, timeout);
114548e8ac29SBodo Stroesser 	else
114648e8ac29SBodo Stroesser 		smi_info->timer_running = false;
114748e8ac29SBodo Stroesser 	spin_unlock_irqrestore(&(smi_info->si_lock), flags);
11481da177e4SLinus Torvalds }
11491da177e4SLinus Torvalds 
11504f3e8199SCorey Minyard irqreturn_t ipmi_si_irq_handler(int irq, void *data)
11511da177e4SLinus Torvalds {
11521da177e4SLinus Torvalds 	struct smi_info *smi_info = data;
11531da177e4SLinus Torvalds 	unsigned long   flags;
11541da177e4SLinus Torvalds 
11554f3e8199SCorey Minyard 	if (smi_info->io.si_type == SI_BT)
11564f3e8199SCorey Minyard 		/* We need to clear the IRQ flag for the BT interface. */
11574f3e8199SCorey Minyard 		smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG,
11584f3e8199SCorey Minyard 				     IPMI_BT_INTMASK_CLEAR_IRQ_BIT
11594f3e8199SCorey Minyard 				     | IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
11604f3e8199SCorey Minyard 
11611da177e4SLinus Torvalds 	spin_lock_irqsave(&(smi_info->si_lock), flags);
11621da177e4SLinus Torvalds 
116364959e2dSCorey Minyard 	smi_inc_stat(smi_info, interrupts);
11641da177e4SLinus Torvalds 
1165f93aae9fSJohn Stultz 	debug_timestamp("Interrupt");
1166f93aae9fSJohn Stultz 
11671da177e4SLinus Torvalds 	smi_event_handler(smi_info, 0);
11681da177e4SLinus Torvalds 	spin_unlock_irqrestore(&(smi_info->si_lock), flags);
11691da177e4SLinus Torvalds 	return IRQ_HANDLED;
11701da177e4SLinus Torvalds }
11711da177e4SLinus Torvalds 
1172453823baSCorey Minyard static int smi_start_processing(void       *send_info,
1173453823baSCorey Minyard 				ipmi_smi_t intf)
1174453823baSCorey Minyard {
1175453823baSCorey Minyard 	struct smi_info *new_smi = send_info;
1176a51f4a81SCorey Minyard 	int             enable = 0;
1177453823baSCorey Minyard 
1178453823baSCorey Minyard 	new_smi->intf = intf;
1179453823baSCorey Minyard 
1180453823baSCorey Minyard 	/* Set up the timer that drives the interface. */
1181453823baSCorey Minyard 	setup_timer(&new_smi->si_timer, smi_timeout, (long)new_smi);
118248e8ac29SBodo Stroesser 	smi_mod_timer(new_smi, jiffies + SI_TIMEOUT_JIFFIES);
1183453823baSCorey Minyard 
118427f972d3SJan Stancek 	/* Try to claim any interrupts. */
11854f3e8199SCorey Minyard 	if (new_smi->io.irq_setup) {
11864f3e8199SCorey Minyard 		new_smi->io.irq_handler_data = new_smi;
11874f3e8199SCorey Minyard 		new_smi->io.irq_setup(&new_smi->io);
11884f3e8199SCorey Minyard 	}
118927f972d3SJan Stancek 
1190df3fe8deSCorey Minyard 	/*
1191a51f4a81SCorey Minyard 	 * Check if the user forcefully enabled the daemon.
1192a51f4a81SCorey Minyard 	 */
1193a51f4a81SCorey Minyard 	if (new_smi->intf_num < num_force_kipmid)
1194a51f4a81SCorey Minyard 		enable = force_kipmid[new_smi->intf_num];
1195a51f4a81SCorey Minyard 	/*
1196df3fe8deSCorey Minyard 	 * The BT interface is efficient enough to not need a thread,
1197df3fe8deSCorey Minyard 	 * and there is no need for a thread if we have interrupts.
1198df3fe8deSCorey Minyard 	 */
1199910840f2SCorey Minyard 	else if ((new_smi->io.si_type != SI_BT) && (!new_smi->io.irq))
1200a51f4a81SCorey Minyard 		enable = 1;
1201a51f4a81SCorey Minyard 
1202a51f4a81SCorey Minyard 	if (enable) {
1203453823baSCorey Minyard 		new_smi->thread = kthread_run(ipmi_thread, new_smi,
1204453823baSCorey Minyard 					      "kipmi%d", new_smi->intf_num);
1205453823baSCorey Minyard 		if (IS_ERR(new_smi->thread)) {
1206910840f2SCorey Minyard 			dev_notice(new_smi->io.dev, "Could not start"
1207453823baSCorey Minyard 				   " kernel thread due to error %ld, only using"
1208453823baSCorey Minyard 				   " timers to drive the interface\n",
1209453823baSCorey Minyard 				   PTR_ERR(new_smi->thread));
1210453823baSCorey Minyard 			new_smi->thread = NULL;
1211453823baSCorey Minyard 		}
1212453823baSCorey Minyard 	}
1213453823baSCorey Minyard 
1214453823baSCorey Minyard 	return 0;
1215453823baSCorey Minyard }
12169dbf68f9SCorey Minyard 
121716f4232cSZhao Yakui static int get_smi_info(void *send_info, struct ipmi_smi_info *data)
121816f4232cSZhao Yakui {
121916f4232cSZhao Yakui 	struct smi_info *smi = send_info;
122016f4232cSZhao Yakui 
1221910840f2SCorey Minyard 	data->addr_src = smi->io.addr_source;
1222910840f2SCorey Minyard 	data->dev = smi->io.dev;
1223bb398a4cSCorey Minyard 	data->addr_info = smi->io.addr_info;
1224910840f2SCorey Minyard 	get_device(smi->io.dev);
122516f4232cSZhao Yakui 
122616f4232cSZhao Yakui 	return 0;
122716f4232cSZhao Yakui }
122816f4232cSZhao Yakui 
12297aefac26SCorey Minyard static void set_maintenance_mode(void *send_info, bool enable)
1230b9675136SCorey Minyard {
1231b9675136SCorey Minyard 	struct smi_info   *smi_info = send_info;
1232b9675136SCorey Minyard 
1233b9675136SCorey Minyard 	if (!enable)
1234b9675136SCorey Minyard 		atomic_set(&smi_info->req_events, 0);
1235b9675136SCorey Minyard }
1236b9675136SCorey Minyard 
123781d02b7fSCorey Minyard static const struct ipmi_smi_handlers handlers = {
12381da177e4SLinus Torvalds 	.owner                  = THIS_MODULE,
1239453823baSCorey Minyard 	.start_processing       = smi_start_processing,
124016f4232cSZhao Yakui 	.get_smi_info		= get_smi_info,
12411da177e4SLinus Torvalds 	.sender			= sender,
12421da177e4SLinus Torvalds 	.request_events		= request_events,
124389986496SCorey Minyard 	.set_need_watch		= set_need_watch,
1244b9675136SCorey Minyard 	.set_maintenance_mode   = set_maintenance_mode,
12451da177e4SLinus Torvalds 	.set_run_to_completion  = set_run_to_completion,
124682802f96SHidehiro Kawai 	.flush_messages		= flush_messages,
12471da177e4SLinus Torvalds 	.poll			= poll,
12481da177e4SLinus Torvalds };
12491da177e4SLinus Torvalds 
1250b0defcdbSCorey Minyard static LIST_HEAD(smi_infos);
1251d6dfd131SCorey Minyard static DEFINE_MUTEX(smi_infos_lock);
1252b0defcdbSCorey Minyard static int smi_num; /* Used to sequence the SMIs */
12531da177e4SLinus Torvalds 
125499ee6735SLABBE Corentin static const char * const addr_space_to_str[] = { "i/o", "mem" };
1255b361e27bSCorey Minyard 
1256a51f4a81SCorey Minyard module_param_array(force_kipmid, int, &num_force_kipmid, 0);
1257a51f4a81SCorey Minyard MODULE_PARM_DESC(force_kipmid, "Force the kipmi daemon to be enabled (1) or"
1258a51f4a81SCorey Minyard 		 " disabled(0).  Normally the IPMI driver auto-detects"
1259a51f4a81SCorey Minyard 		 " this, but the value may be overridden by this parm.");
12607aefac26SCorey Minyard module_param(unload_when_empty, bool, 0);
1261b361e27bSCorey Minyard MODULE_PARM_DESC(unload_when_empty, "Unload the module if no interfaces are"
1262b361e27bSCorey Minyard 		 " specified or found, default is 1.  Setting to 0"
1263b361e27bSCorey Minyard 		 " is useful for hot add of devices using hotmod.");
1264ae74e823SMartin Wilck module_param_array(kipmid_max_busy_us, uint, &num_max_busy_us, 0644);
1265ae74e823SMartin Wilck MODULE_PARM_DESC(kipmid_max_busy_us,
1266ae74e823SMartin Wilck 		 "Max time (in microseconds) to busy-wait for IPMI data before"
1267ae74e823SMartin Wilck 		 " sleeping. 0 (default) means to wait forever. Set to 100-500"
1268ae74e823SMartin Wilck 		 " if kipmid is using up a lot of CPU time.");
12691da177e4SLinus Torvalds 
12704f3e8199SCorey Minyard void ipmi_irq_finish_setup(struct si_sm_io *io)
12711da177e4SLinus Torvalds {
12724f3e8199SCorey Minyard 	if (io->si_type == SI_BT)
12734f3e8199SCorey Minyard 		/* Enable the interrupt in the BT interface. */
12744f3e8199SCorey Minyard 		io->outputb(io, IPMI_BT_INTMASK_REG,
12754f3e8199SCorey Minyard 			    IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
12761da177e4SLinus Torvalds }
12771da177e4SLinus Torvalds 
12784f3e8199SCorey Minyard void ipmi_irq_start_cleanup(struct si_sm_io *io)
12794f3e8199SCorey Minyard {
12804f3e8199SCorey Minyard 	if (io->si_type == SI_BT)
12814f3e8199SCorey Minyard 		/* Disable the interrupt in the BT interface. */
12824f3e8199SCorey Minyard 		io->outputb(io, IPMI_BT_INTMASK_REG, 0);
12834f3e8199SCorey Minyard }
12844f3e8199SCorey Minyard 
12854f3e8199SCorey Minyard static void std_irq_cleanup(struct si_sm_io *io)
12864f3e8199SCorey Minyard {
12874f3e8199SCorey Minyard 	ipmi_irq_start_cleanup(io);
12884f3e8199SCorey Minyard 	free_irq(io->irq, io->irq_handler_data);
12894f3e8199SCorey Minyard }
12904f3e8199SCorey Minyard 
12914f3e8199SCorey Minyard int ipmi_std_irq_setup(struct si_sm_io *io)
12921da177e4SLinus Torvalds {
12931da177e4SLinus Torvalds 	int rv;
12941da177e4SLinus Torvalds 
12954f3e8199SCorey Minyard 	if (!io->irq)
12961da177e4SLinus Torvalds 		return 0;
12971da177e4SLinus Torvalds 
12984f3e8199SCorey Minyard 	rv = request_irq(io->irq,
12994f3e8199SCorey Minyard 			 ipmi_si_irq_handler,
1300aa5b2babSMichael Opdenacker 			 IRQF_SHARED,
13019dbf68f9SCorey Minyard 			 DEVICE_NAME,
13024f3e8199SCorey Minyard 			 io->irq_handler_data);
13031da177e4SLinus Torvalds 	if (rv) {
13044f3e8199SCorey Minyard 		dev_warn(io->dev, "%s unable to claim interrupt %d,"
13051da177e4SLinus Torvalds 			 " running polled\n",
13064f3e8199SCorey Minyard 			 DEVICE_NAME, io->irq);
13074f3e8199SCorey Minyard 		io->irq = 0;
13081da177e4SLinus Torvalds 	} else {
13094f3e8199SCorey Minyard 		io->irq_cleanup = std_irq_cleanup;
13104f3e8199SCorey Minyard 		ipmi_irq_finish_setup(io);
13114f3e8199SCorey Minyard 		dev_info(io->dev, "Using irq %d\n", io->irq);
13121da177e4SLinus Torvalds 	}
13131da177e4SLinus Torvalds 
13141da177e4SLinus Torvalds 	return rv;
13151da177e4SLinus Torvalds }
13161da177e4SLinus Torvalds 
131781d02b7fSCorey Minyard static unsigned char port_inb(const struct si_sm_io *io, unsigned int offset)
13181da177e4SLinus Torvalds {
1319b0defcdbSCorey Minyard 	unsigned int addr = io->addr_data;
13201da177e4SLinus Torvalds 
1321b0defcdbSCorey Minyard 	return inb(addr + (offset * io->regspacing));
13221da177e4SLinus Torvalds }
13231da177e4SLinus Torvalds 
132481d02b7fSCorey Minyard static void port_outb(const struct si_sm_io *io, unsigned int offset,
13251da177e4SLinus Torvalds 		      unsigned char b)
13261da177e4SLinus Torvalds {
1327b0defcdbSCorey Minyard 	unsigned int addr = io->addr_data;
13281da177e4SLinus Torvalds 
1329b0defcdbSCorey Minyard 	outb(b, addr + (offset * io->regspacing));
13301da177e4SLinus Torvalds }
13311da177e4SLinus Torvalds 
133281d02b7fSCorey Minyard static unsigned char port_inw(const struct si_sm_io *io, unsigned int offset)
13331da177e4SLinus Torvalds {
1334b0defcdbSCorey Minyard 	unsigned int addr = io->addr_data;
13351da177e4SLinus Torvalds 
1336b0defcdbSCorey Minyard 	return (inw(addr + (offset * io->regspacing)) >> io->regshift) & 0xff;
13371da177e4SLinus Torvalds }
13381da177e4SLinus Torvalds 
133981d02b7fSCorey Minyard static void port_outw(const struct si_sm_io *io, unsigned int offset,
13401da177e4SLinus Torvalds 		      unsigned char b)
13411da177e4SLinus Torvalds {
1342b0defcdbSCorey Minyard 	unsigned int addr = io->addr_data;
13431da177e4SLinus Torvalds 
1344b0defcdbSCorey Minyard 	outw(b << io->regshift, addr + (offset * io->regspacing));
13451da177e4SLinus Torvalds }
13461da177e4SLinus Torvalds 
134781d02b7fSCorey Minyard static unsigned char port_inl(const struct si_sm_io *io, unsigned int offset)
13481da177e4SLinus Torvalds {
1349b0defcdbSCorey Minyard 	unsigned int addr = io->addr_data;
13501da177e4SLinus Torvalds 
1351b0defcdbSCorey Minyard 	return (inl(addr + (offset * io->regspacing)) >> io->regshift) & 0xff;
13521da177e4SLinus Torvalds }
13531da177e4SLinus Torvalds 
135481d02b7fSCorey Minyard static void port_outl(const struct si_sm_io *io, unsigned int offset,
13551da177e4SLinus Torvalds 		      unsigned char b)
13561da177e4SLinus Torvalds {
1357b0defcdbSCorey Minyard 	unsigned int addr = io->addr_data;
13581da177e4SLinus Torvalds 
1359b0defcdbSCorey Minyard 	outl(b << io->regshift, addr+(offset * io->regspacing));
13601da177e4SLinus Torvalds }
13611da177e4SLinus Torvalds 
1362e1eeb7f8SCorey Minyard static void port_cleanup(struct si_sm_io *io)
13631da177e4SLinus Torvalds {
1364e1eeb7f8SCorey Minyard 	unsigned int addr = io->addr_data;
1365d61a3eadSCorey Minyard 	int          idx;
13661da177e4SLinus Torvalds 
1367b0defcdbSCorey Minyard 	if (addr) {
1368e1eeb7f8SCorey Minyard 		for (idx = 0; idx < io->io_size; idx++)
1369e1eeb7f8SCorey Minyard 			release_region(addr + idx * io->regspacing,
1370e1eeb7f8SCorey Minyard 				       io->regsize);
1371d61a3eadSCorey Minyard 	}
13721da177e4SLinus Torvalds }
13731da177e4SLinus Torvalds 
1374e1eeb7f8SCorey Minyard static int port_setup(struct si_sm_io *io)
13751da177e4SLinus Torvalds {
1376e1eeb7f8SCorey Minyard 	unsigned int addr = io->addr_data;
1377d61a3eadSCorey Minyard 	int          idx;
13781da177e4SLinus Torvalds 
1379b0defcdbSCorey Minyard 	if (!addr)
13801da177e4SLinus Torvalds 		return -ENODEV;
13811da177e4SLinus Torvalds 
1382e1eeb7f8SCorey Minyard 	io->io_cleanup = port_cleanup;
13831da177e4SLinus Torvalds 
1384c305e3d3SCorey Minyard 	/*
1385c305e3d3SCorey Minyard 	 * Figure out the actual inb/inw/inl/etc routine to use based
1386c305e3d3SCorey Minyard 	 * upon the register size.
1387c305e3d3SCorey Minyard 	 */
1388e1eeb7f8SCorey Minyard 	switch (io->regsize) {
13891da177e4SLinus Torvalds 	case 1:
1390e1eeb7f8SCorey Minyard 		io->inputb = port_inb;
1391e1eeb7f8SCorey Minyard 		io->outputb = port_outb;
13921da177e4SLinus Torvalds 		break;
13931da177e4SLinus Torvalds 	case 2:
1394e1eeb7f8SCorey Minyard 		io->inputb = port_inw;
1395e1eeb7f8SCorey Minyard 		io->outputb = port_outw;
13961da177e4SLinus Torvalds 		break;
13971da177e4SLinus Torvalds 	case 4:
1398e1eeb7f8SCorey Minyard 		io->inputb = port_inl;
1399e1eeb7f8SCorey Minyard 		io->outputb = port_outl;
14001da177e4SLinus Torvalds 		break;
14011da177e4SLinus Torvalds 	default:
1402e1eeb7f8SCorey Minyard 		dev_warn(io->dev, "Invalid register size: %d\n",
1403e1eeb7f8SCorey Minyard 			 io->regsize);
14041da177e4SLinus Torvalds 		return -EINVAL;
14051da177e4SLinus Torvalds 	}
14061da177e4SLinus Torvalds 
1407c305e3d3SCorey Minyard 	/*
1408c305e3d3SCorey Minyard 	 * Some BIOSes reserve disjoint I/O regions in their ACPI
1409d61a3eadSCorey Minyard 	 * tables.  This causes problems when trying to register the
1410d61a3eadSCorey Minyard 	 * entire I/O region.  Therefore we must register each I/O
1411d61a3eadSCorey Minyard 	 * port separately.
1412d61a3eadSCorey Minyard 	 */
1413e1eeb7f8SCorey Minyard 	for (idx = 0; idx < io->io_size; idx++) {
1414e1eeb7f8SCorey Minyard 		if (request_region(addr + idx * io->regspacing,
1415e1eeb7f8SCorey Minyard 				   io->regsize, DEVICE_NAME) == NULL) {
1416d61a3eadSCorey Minyard 			/* Undo allocations */
141776824852SCorey Minyard 			while (idx--)
1418e1eeb7f8SCorey Minyard 				release_region(addr + idx * io->regspacing,
1419e1eeb7f8SCorey Minyard 					       io->regsize);
14201da177e4SLinus Torvalds 			return -EIO;
1421d61a3eadSCorey Minyard 		}
1422d61a3eadSCorey Minyard 	}
14231da177e4SLinus Torvalds 	return 0;
14241da177e4SLinus Torvalds }
14251da177e4SLinus Torvalds 
142681d02b7fSCorey Minyard static unsigned char intf_mem_inb(const struct si_sm_io *io,
142781d02b7fSCorey Minyard 				  unsigned int offset)
14281da177e4SLinus Torvalds {
14291da177e4SLinus Torvalds 	return readb((io->addr)+(offset * io->regspacing));
14301da177e4SLinus Torvalds }
14311da177e4SLinus Torvalds 
143281d02b7fSCorey Minyard static void intf_mem_outb(const struct si_sm_io *io, unsigned int offset,
14331da177e4SLinus Torvalds 			  unsigned char b)
14341da177e4SLinus Torvalds {
14351da177e4SLinus Torvalds 	writeb(b, (io->addr)+(offset * io->regspacing));
14361da177e4SLinus Torvalds }
14371da177e4SLinus Torvalds 
143881d02b7fSCorey Minyard static unsigned char intf_mem_inw(const struct si_sm_io *io,
143981d02b7fSCorey Minyard 				  unsigned int offset)
14401da177e4SLinus Torvalds {
14411da177e4SLinus Torvalds 	return (readw((io->addr)+(offset * io->regspacing)) >> io->regshift)
144264d9fe69SAlexey Dobriyan 		& 0xff;
14431da177e4SLinus Torvalds }
14441da177e4SLinus Torvalds 
144581d02b7fSCorey Minyard static void intf_mem_outw(const struct si_sm_io *io, unsigned int offset,
14461da177e4SLinus Torvalds 			  unsigned char b)
14471da177e4SLinus Torvalds {
14481da177e4SLinus Torvalds 	writeb(b << io->regshift, (io->addr)+(offset * io->regspacing));
14491da177e4SLinus Torvalds }
14501da177e4SLinus Torvalds 
145181d02b7fSCorey Minyard static unsigned char intf_mem_inl(const struct si_sm_io *io,
145281d02b7fSCorey Minyard 				  unsigned int offset)
14531da177e4SLinus Torvalds {
14541da177e4SLinus Torvalds 	return (readl((io->addr)+(offset * io->regspacing)) >> io->regshift)
145564d9fe69SAlexey Dobriyan 		& 0xff;
14561da177e4SLinus Torvalds }
14571da177e4SLinus Torvalds 
145881d02b7fSCorey Minyard static void intf_mem_outl(const struct si_sm_io *io, unsigned int offset,
14591da177e4SLinus Torvalds 			  unsigned char b)
14601da177e4SLinus Torvalds {
14611da177e4SLinus Torvalds 	writel(b << io->regshift, (io->addr)+(offset * io->regspacing));
14621da177e4SLinus Torvalds }
14631da177e4SLinus Torvalds 
14641da177e4SLinus Torvalds #ifdef readq
146581d02b7fSCorey Minyard static unsigned char mem_inq(const struct si_sm_io *io, unsigned int offset)
14661da177e4SLinus Torvalds {
14671da177e4SLinus Torvalds 	return (readq((io->addr)+(offset * io->regspacing)) >> io->regshift)
146864d9fe69SAlexey Dobriyan 		& 0xff;
14691da177e4SLinus Torvalds }
14701da177e4SLinus Torvalds 
147181d02b7fSCorey Minyard static void mem_outq(const struct si_sm_io *io, unsigned int offset,
14721da177e4SLinus Torvalds 		     unsigned char b)
14731da177e4SLinus Torvalds {
14741da177e4SLinus Torvalds 	writeq(b << io->regshift, (io->addr)+(offset * io->regspacing));
14751da177e4SLinus Torvalds }
14761da177e4SLinus Torvalds #endif
14771da177e4SLinus Torvalds 
1478e1eeb7f8SCorey Minyard static void mem_region_cleanup(struct si_sm_io *io, int num)
14791da177e4SLinus Torvalds {
1480e1eeb7f8SCorey Minyard 	unsigned long addr = io->addr_data;
148157a38f13SCorey Minyard 	int idx;
14821da177e4SLinus Torvalds 
148357a38f13SCorey Minyard 	for (idx = 0; idx < num; idx++)
1484e1eeb7f8SCorey Minyard 		release_mem_region(addr + idx * io->regspacing,
1485e1eeb7f8SCorey Minyard 				   io->regsize);
148657a38f13SCorey Minyard }
148757a38f13SCorey Minyard 
1488e1eeb7f8SCorey Minyard static void mem_cleanup(struct si_sm_io *io)
148957a38f13SCorey Minyard {
1490e1eeb7f8SCorey Minyard 	if (io->addr) {
1491e1eeb7f8SCorey Minyard 		iounmap(io->addr);
1492e1eeb7f8SCorey Minyard 		mem_region_cleanup(io, io->io_size);
14931da177e4SLinus Torvalds 	}
14941da177e4SLinus Torvalds }
14951da177e4SLinus Torvalds 
1496e1eeb7f8SCorey Minyard static int mem_setup(struct si_sm_io *io)
14971da177e4SLinus Torvalds {
1498e1eeb7f8SCorey Minyard 	unsigned long addr = io->addr_data;
149957a38f13SCorey Minyard 	int           mapsize, idx;
15001da177e4SLinus Torvalds 
1501b0defcdbSCorey Minyard 	if (!addr)
15021da177e4SLinus Torvalds 		return -ENODEV;
15031da177e4SLinus Torvalds 
1504e1eeb7f8SCorey Minyard 	io->io_cleanup = mem_cleanup;
15051da177e4SLinus Torvalds 
1506c305e3d3SCorey Minyard 	/*
1507c305e3d3SCorey Minyard 	 * Figure out the actual readb/readw/readl/etc routine to use based
1508c305e3d3SCorey Minyard 	 * upon the register size.
1509c305e3d3SCorey Minyard 	 */
1510e1eeb7f8SCorey Minyard 	switch (io->regsize) {
15111da177e4SLinus Torvalds 	case 1:
1512e1eeb7f8SCorey Minyard 		io->inputb = intf_mem_inb;
1513e1eeb7f8SCorey Minyard 		io->outputb = intf_mem_outb;
15141da177e4SLinus Torvalds 		break;
15151da177e4SLinus Torvalds 	case 2:
1516e1eeb7f8SCorey Minyard 		io->inputb = intf_mem_inw;
1517e1eeb7f8SCorey Minyard 		io->outputb = intf_mem_outw;
15181da177e4SLinus Torvalds 		break;
15191da177e4SLinus Torvalds 	case 4:
1520e1eeb7f8SCorey Minyard 		io->inputb = intf_mem_inl;
1521e1eeb7f8SCorey Minyard 		io->outputb = intf_mem_outl;
15221da177e4SLinus Torvalds 		break;
15231da177e4SLinus Torvalds #ifdef readq
15241da177e4SLinus Torvalds 	case 8:
1525e1eeb7f8SCorey Minyard 		io->inputb = mem_inq;
1526e1eeb7f8SCorey Minyard 		io->outputb = mem_outq;
15271da177e4SLinus Torvalds 		break;
15281da177e4SLinus Torvalds #endif
15291da177e4SLinus Torvalds 	default:
1530e1eeb7f8SCorey Minyard 		dev_warn(io->dev, "Invalid register size: %d\n",
1531e1eeb7f8SCorey Minyard 			 io->regsize);
15321da177e4SLinus Torvalds 		return -EINVAL;
15331da177e4SLinus Torvalds 	}
15341da177e4SLinus Torvalds 
1535c305e3d3SCorey Minyard 	/*
153657a38f13SCorey Minyard 	 * Some BIOSes reserve disjoint memory regions in their ACPI
153757a38f13SCorey Minyard 	 * tables.  This causes problems when trying to request the
153857a38f13SCorey Minyard 	 * entire region.  Therefore we must request each register
153957a38f13SCorey Minyard 	 * separately.
154057a38f13SCorey Minyard 	 */
1541e1eeb7f8SCorey Minyard 	for (idx = 0; idx < io->io_size; idx++) {
1542e1eeb7f8SCorey Minyard 		if (request_mem_region(addr + idx * io->regspacing,
1543e1eeb7f8SCorey Minyard 				       io->regsize, DEVICE_NAME) == NULL) {
154457a38f13SCorey Minyard 			/* Undo allocations */
1545e1eeb7f8SCorey Minyard 			mem_region_cleanup(io, idx);
154657a38f13SCorey Minyard 			return -EIO;
154757a38f13SCorey Minyard 		}
154857a38f13SCorey Minyard 	}
154957a38f13SCorey Minyard 
155057a38f13SCorey Minyard 	/*
1551c305e3d3SCorey Minyard 	 * Calculate the total amount of memory to claim.  This is an
15521da177e4SLinus Torvalds 	 * unusual looking calculation, but it avoids claiming any
15531da177e4SLinus Torvalds 	 * more memory than it has to.  It will claim everything
15541da177e4SLinus Torvalds 	 * between the first address to the end of the last full
1555c305e3d3SCorey Minyard 	 * register.
1556c305e3d3SCorey Minyard 	 */
1557e1eeb7f8SCorey Minyard 	mapsize = ((io->io_size * io->regspacing)
1558e1eeb7f8SCorey Minyard 		   - (io->regspacing - io->regsize));
1559e1eeb7f8SCorey Minyard 	io->addr = ioremap(addr, mapsize);
1560e1eeb7f8SCorey Minyard 	if (io->addr == NULL) {
1561e1eeb7f8SCorey Minyard 		mem_region_cleanup(io, io->io_size);
15621da177e4SLinus Torvalds 		return -EIO;
15631da177e4SLinus Torvalds 	}
15641da177e4SLinus Torvalds 	return 0;
15651da177e4SLinus Torvalds }
15661da177e4SLinus Torvalds 
1567de5e2ddfSEric Dumazet static struct smi_info *smi_info_alloc(void)
1568de5e2ddfSEric Dumazet {
1569de5e2ddfSEric Dumazet 	struct smi_info *info = kzalloc(sizeof(*info), GFP_KERNEL);
1570de5e2ddfSEric Dumazet 
1571f60adf42SCorey Minyard 	if (info)
1572de5e2ddfSEric Dumazet 		spin_lock_init(&info->si_lock);
1573de5e2ddfSEric Dumazet 	return info;
1574de5e2ddfSEric Dumazet }
1575de5e2ddfSEric Dumazet 
157640112ae7SCorey Minyard static int wait_for_msg_done(struct smi_info *smi_info)
15771da177e4SLinus Torvalds {
15781da177e4SLinus Torvalds 	enum si_sm_result     smi_result;
15791da177e4SLinus Torvalds 
15801da177e4SLinus Torvalds 	smi_result = smi_info->handlers->event(smi_info->si_sm, 0);
1581c305e3d3SCorey Minyard 	for (;;) {
1582c3e7e791SCorey Minyard 		if (smi_result == SI_SM_CALL_WITH_DELAY ||
1583c3e7e791SCorey Minyard 		    smi_result == SI_SM_CALL_WITH_TICK_DELAY) {
1584da4cd8dfSNishanth Aravamudan 			schedule_timeout_uninterruptible(1);
15851da177e4SLinus Torvalds 			smi_result = smi_info->handlers->event(
1586e21404dcSXie XiuQi 				smi_info->si_sm, jiffies_to_usecs(1));
1587c305e3d3SCorey Minyard 		} else if (smi_result == SI_SM_CALL_WITHOUT_DELAY) {
15881da177e4SLinus Torvalds 			smi_result = smi_info->handlers->event(
15891da177e4SLinus Torvalds 				smi_info->si_sm, 0);
1590c305e3d3SCorey Minyard 		} else
15911da177e4SLinus Torvalds 			break;
15921da177e4SLinus Torvalds 	}
159340112ae7SCorey Minyard 	if (smi_result == SI_SM_HOSED)
1594c305e3d3SCorey Minyard 		/*
1595c305e3d3SCorey Minyard 		 * We couldn't get the state machine to run, so whatever's at
1596c305e3d3SCorey Minyard 		 * the port is probably not an IPMI SMI interface.
1597c305e3d3SCorey Minyard 		 */
159840112ae7SCorey Minyard 		return -ENODEV;
159940112ae7SCorey Minyard 
160040112ae7SCorey Minyard 	return 0;
16011da177e4SLinus Torvalds }
16021da177e4SLinus Torvalds 
160340112ae7SCorey Minyard static int try_get_dev_id(struct smi_info *smi_info)
160440112ae7SCorey Minyard {
160540112ae7SCorey Minyard 	unsigned char         msg[2];
160640112ae7SCorey Minyard 	unsigned char         *resp;
160740112ae7SCorey Minyard 	unsigned long         resp_len;
160840112ae7SCorey Minyard 	int                   rv = 0;
160940112ae7SCorey Minyard 
161040112ae7SCorey Minyard 	resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
161140112ae7SCorey Minyard 	if (!resp)
161240112ae7SCorey Minyard 		return -ENOMEM;
161340112ae7SCorey Minyard 
161440112ae7SCorey Minyard 	/*
161540112ae7SCorey Minyard 	 * Do a Get Device ID command, since it comes back with some
161640112ae7SCorey Minyard 	 * useful info.
161740112ae7SCorey Minyard 	 */
161840112ae7SCorey Minyard 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
161940112ae7SCorey Minyard 	msg[1] = IPMI_GET_DEVICE_ID_CMD;
162040112ae7SCorey Minyard 	smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
162140112ae7SCorey Minyard 
162240112ae7SCorey Minyard 	rv = wait_for_msg_done(smi_info);
162340112ae7SCorey Minyard 	if (rv)
162440112ae7SCorey Minyard 		goto out;
162540112ae7SCorey Minyard 
16261da177e4SLinus Torvalds 	resp_len = smi_info->handlers->get_result(smi_info->si_sm,
16271da177e4SLinus Torvalds 						  resp, IPMI_MAX_MSG_LENGTH);
16281da177e4SLinus Torvalds 
1629d8c98618SCorey Minyard 	/* Check and record info from the get device id, in case we need it. */
1630c468f911SJeremy Kerr 	rv = ipmi_demangle_device_id(resp[0] >> 2, resp[1],
1631c468f911SJeremy Kerr 			resp + 2, resp_len - 2, &smi_info->device_id);
16321da177e4SLinus Torvalds 
16331da177e4SLinus Torvalds out:
16341da177e4SLinus Torvalds 	kfree(resp);
16351da177e4SLinus Torvalds 	return rv;
16361da177e4SLinus Torvalds }
16371da177e4SLinus Torvalds 
1638d0882897SCorey Minyard static int get_global_enables(struct smi_info *smi_info, u8 *enables)
16391e7d6a45SCorey Minyard {
16401e7d6a45SCorey Minyard 	unsigned char         msg[3];
16411e7d6a45SCorey Minyard 	unsigned char         *resp;
16421e7d6a45SCorey Minyard 	unsigned long         resp_len;
16431e7d6a45SCorey Minyard 	int                   rv;
16441e7d6a45SCorey Minyard 
16451e7d6a45SCorey Minyard 	resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1646d0882897SCorey Minyard 	if (!resp)
1647d0882897SCorey Minyard 		return -ENOMEM;
16481e7d6a45SCorey Minyard 
16491e7d6a45SCorey Minyard 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
16501e7d6a45SCorey Minyard 	msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
16511e7d6a45SCorey Minyard 	smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
16521e7d6a45SCorey Minyard 
16531e7d6a45SCorey Minyard 	rv = wait_for_msg_done(smi_info);
16541e7d6a45SCorey Minyard 	if (rv) {
1655910840f2SCorey Minyard 		dev_warn(smi_info->io.dev,
1656d0882897SCorey Minyard 			 "Error getting response from get global enables command: %d\n",
1657d0882897SCorey Minyard 			 rv);
16581e7d6a45SCorey Minyard 		goto out;
16591e7d6a45SCorey Minyard 	}
16601e7d6a45SCorey Minyard 
16611e7d6a45SCorey Minyard 	resp_len = smi_info->handlers->get_result(smi_info->si_sm,
16621e7d6a45SCorey Minyard 						  resp, IPMI_MAX_MSG_LENGTH);
16631e7d6a45SCorey Minyard 
16641e7d6a45SCorey Minyard 	if (resp_len < 4 ||
16651e7d6a45SCorey Minyard 			resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
16661e7d6a45SCorey Minyard 			resp[1] != IPMI_GET_BMC_GLOBAL_ENABLES_CMD   ||
16671e7d6a45SCorey Minyard 			resp[2] != 0) {
1668910840f2SCorey Minyard 		dev_warn(smi_info->io.dev,
1669d0882897SCorey Minyard 			 "Invalid return from get global enables command: %ld %x %x %x\n",
1670d0882897SCorey Minyard 			 resp_len, resp[0], resp[1], resp[2]);
16711e7d6a45SCorey Minyard 		rv = -EINVAL;
16721e7d6a45SCorey Minyard 		goto out;
1673d0882897SCorey Minyard 	} else {
1674d0882897SCorey Minyard 		*enables = resp[3];
16751e7d6a45SCorey Minyard 	}
16761e7d6a45SCorey Minyard 
1677d0882897SCorey Minyard out:
1678d0882897SCorey Minyard 	kfree(resp);
1679d0882897SCorey Minyard 	return rv;
1680d0882897SCorey Minyard }
1681d0882897SCorey Minyard 
1682d0882897SCorey Minyard /*
1683d0882897SCorey Minyard  * Returns 1 if it gets an error from the command.
1684d0882897SCorey Minyard  */
1685d0882897SCorey Minyard static int set_global_enables(struct smi_info *smi_info, u8 enables)
1686d0882897SCorey Minyard {
1687d0882897SCorey Minyard 	unsigned char         msg[3];
1688d0882897SCorey Minyard 	unsigned char         *resp;
1689d0882897SCorey Minyard 	unsigned long         resp_len;
1690d0882897SCorey Minyard 	int                   rv;
1691d0882897SCorey Minyard 
1692d0882897SCorey Minyard 	resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1693d0882897SCorey Minyard 	if (!resp)
1694d0882897SCorey Minyard 		return -ENOMEM;
16951e7d6a45SCorey Minyard 
16961e7d6a45SCorey Minyard 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
16971e7d6a45SCorey Minyard 	msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
1698d0882897SCorey Minyard 	msg[2] = enables;
16991e7d6a45SCorey Minyard 	smi_info->handlers->start_transaction(smi_info->si_sm, msg, 3);
17001e7d6a45SCorey Minyard 
17011e7d6a45SCorey Minyard 	rv = wait_for_msg_done(smi_info);
17021e7d6a45SCorey Minyard 	if (rv) {
1703910840f2SCorey Minyard 		dev_warn(smi_info->io.dev,
1704d0882897SCorey Minyard 			 "Error getting response from set global enables command: %d\n",
1705d0882897SCorey Minyard 			 rv);
17061e7d6a45SCorey Minyard 		goto out;
17071e7d6a45SCorey Minyard 	}
17081e7d6a45SCorey Minyard 
17091e7d6a45SCorey Minyard 	resp_len = smi_info->handlers->get_result(smi_info->si_sm,
17101e7d6a45SCorey Minyard 						  resp, IPMI_MAX_MSG_LENGTH);
17111e7d6a45SCorey Minyard 
17121e7d6a45SCorey Minyard 	if (resp_len < 3 ||
17131e7d6a45SCorey Minyard 			resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
17141e7d6a45SCorey Minyard 			resp[1] != IPMI_SET_BMC_GLOBAL_ENABLES_CMD) {
1715910840f2SCorey Minyard 		dev_warn(smi_info->io.dev,
1716d0882897SCorey Minyard 			 "Invalid return from set global enables command: %ld %x %x\n",
1717d0882897SCorey Minyard 			 resp_len, resp[0], resp[1]);
17181e7d6a45SCorey Minyard 		rv = -EINVAL;
17191e7d6a45SCorey Minyard 		goto out;
17201e7d6a45SCorey Minyard 	}
17211e7d6a45SCorey Minyard 
1722d0882897SCorey Minyard 	if (resp[2] != 0)
1723d0882897SCorey Minyard 		rv = 1;
1724d0882897SCorey Minyard 
1725d0882897SCorey Minyard out:
1726d0882897SCorey Minyard 	kfree(resp);
1727d0882897SCorey Minyard 	return rv;
1728d0882897SCorey Minyard }
1729d0882897SCorey Minyard 
1730d0882897SCorey Minyard /*
1731d0882897SCorey Minyard  * Some BMCs do not support clearing the receive irq bit in the global
1732d0882897SCorey Minyard  * enables (even if they don't support interrupts on the BMC).  Check
1733d0882897SCorey Minyard  * for this and handle it properly.
1734d0882897SCorey Minyard  */
1735d0882897SCorey Minyard static void check_clr_rcv_irq(struct smi_info *smi_info)
1736d0882897SCorey Minyard {
1737d0882897SCorey Minyard 	u8 enables = 0;
1738d0882897SCorey Minyard 	int rv;
1739d0882897SCorey Minyard 
1740d0882897SCorey Minyard 	rv = get_global_enables(smi_info, &enables);
1741d0882897SCorey Minyard 	if (!rv) {
1742d0882897SCorey Minyard 		if ((enables & IPMI_BMC_RCV_MSG_INTR) == 0)
1743d0882897SCorey Minyard 			/* Already clear, should work ok. */
1744d0882897SCorey Minyard 			return;
1745d0882897SCorey Minyard 
1746d0882897SCorey Minyard 		enables &= ~IPMI_BMC_RCV_MSG_INTR;
1747d0882897SCorey Minyard 		rv = set_global_enables(smi_info, enables);
1748d0882897SCorey Minyard 	}
1749d0882897SCorey Minyard 
1750d0882897SCorey Minyard 	if (rv < 0) {
1751910840f2SCorey Minyard 		dev_err(smi_info->io.dev,
1752d0882897SCorey Minyard 			"Cannot check clearing the rcv irq: %d\n", rv);
1753d0882897SCorey Minyard 		return;
1754d0882897SCorey Minyard 	}
1755d0882897SCorey Minyard 
1756d0882897SCorey Minyard 	if (rv) {
17571e7d6a45SCorey Minyard 		/*
17581e7d6a45SCorey Minyard 		 * An error when setting the event buffer bit means
17591e7d6a45SCorey Minyard 		 * clearing the bit is not supported.
17601e7d6a45SCorey Minyard 		 */
1761910840f2SCorey Minyard 		dev_warn(smi_info->io.dev,
1762d0882897SCorey Minyard 			 "The BMC does not support clearing the recv irq bit, compensating, but the BMC needs to be fixed.\n");
1763d0882897SCorey Minyard 		smi_info->cannot_disable_irq = true;
17641e7d6a45SCorey Minyard 	}
1765d0882897SCorey Minyard }
1766d0882897SCorey Minyard 
1767d0882897SCorey Minyard /*
1768d0882897SCorey Minyard  * Some BMCs do not support setting the interrupt bits in the global
1769d0882897SCorey Minyard  * enables even if they support interrupts.  Clearly bad, but we can
1770d0882897SCorey Minyard  * compensate.
1771d0882897SCorey Minyard  */
1772d0882897SCorey Minyard static void check_set_rcv_irq(struct smi_info *smi_info)
1773d0882897SCorey Minyard {
1774d0882897SCorey Minyard 	u8 enables = 0;
1775d0882897SCorey Minyard 	int rv;
1776d0882897SCorey Minyard 
1777910840f2SCorey Minyard 	if (!smi_info->io.irq)
1778d0882897SCorey Minyard 		return;
1779d0882897SCorey Minyard 
1780d0882897SCorey Minyard 	rv = get_global_enables(smi_info, &enables);
1781d0882897SCorey Minyard 	if (!rv) {
1782d0882897SCorey Minyard 		enables |= IPMI_BMC_RCV_MSG_INTR;
1783d0882897SCorey Minyard 		rv = set_global_enables(smi_info, enables);
1784d0882897SCorey Minyard 	}
1785d0882897SCorey Minyard 
1786d0882897SCorey Minyard 	if (rv < 0) {
1787910840f2SCorey Minyard 		dev_err(smi_info->io.dev,
1788d0882897SCorey Minyard 			"Cannot check setting the rcv irq: %d\n", rv);
1789d0882897SCorey Minyard 		return;
1790d0882897SCorey Minyard 	}
1791d0882897SCorey Minyard 
1792d0882897SCorey Minyard 	if (rv) {
1793d0882897SCorey Minyard 		/*
1794d0882897SCorey Minyard 		 * An error when setting the event buffer bit means
1795d0882897SCorey Minyard 		 * setting the bit is not supported.
1796d0882897SCorey Minyard 		 */
1797910840f2SCorey Minyard 		dev_warn(smi_info->io.dev,
1798d0882897SCorey Minyard 			 "The BMC does not support setting the recv irq bit, compensating, but the BMC needs to be fixed.\n");
1799d0882897SCorey Minyard 		smi_info->cannot_disable_irq = true;
1800d0882897SCorey Minyard 		smi_info->irq_enable_broken = true;
1801d0882897SCorey Minyard 	}
18021e7d6a45SCorey Minyard }
18031e7d6a45SCorey Minyard 
180440112ae7SCorey Minyard static int try_enable_event_buffer(struct smi_info *smi_info)
180540112ae7SCorey Minyard {
180640112ae7SCorey Minyard 	unsigned char         msg[3];
180740112ae7SCorey Minyard 	unsigned char         *resp;
180840112ae7SCorey Minyard 	unsigned long         resp_len;
180940112ae7SCorey Minyard 	int                   rv = 0;
181040112ae7SCorey Minyard 
181140112ae7SCorey Minyard 	resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
181240112ae7SCorey Minyard 	if (!resp)
181340112ae7SCorey Minyard 		return -ENOMEM;
181440112ae7SCorey Minyard 
181540112ae7SCorey Minyard 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
181640112ae7SCorey Minyard 	msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
181740112ae7SCorey Minyard 	smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
181840112ae7SCorey Minyard 
181940112ae7SCorey Minyard 	rv = wait_for_msg_done(smi_info);
182040112ae7SCorey Minyard 	if (rv) {
1821bb2a08c0SCorey Minyard 		pr_warn(PFX "Error getting response from get global enables command, the event buffer is not enabled.\n");
182240112ae7SCorey Minyard 		goto out;
182340112ae7SCorey Minyard 	}
182440112ae7SCorey Minyard 
182540112ae7SCorey Minyard 	resp_len = smi_info->handlers->get_result(smi_info->si_sm,
182640112ae7SCorey Minyard 						  resp, IPMI_MAX_MSG_LENGTH);
182740112ae7SCorey Minyard 
182840112ae7SCorey Minyard 	if (resp_len < 4 ||
182940112ae7SCorey Minyard 			resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
183040112ae7SCorey Minyard 			resp[1] != IPMI_GET_BMC_GLOBAL_ENABLES_CMD   ||
183140112ae7SCorey Minyard 			resp[2] != 0) {
1832bb2a08c0SCorey Minyard 		pr_warn(PFX "Invalid return from get global enables command, cannot enable the event buffer.\n");
183340112ae7SCorey Minyard 		rv = -EINVAL;
183440112ae7SCorey Minyard 		goto out;
183540112ae7SCorey Minyard 	}
183640112ae7SCorey Minyard 
1837d9b7e4f7SCorey Minyard 	if (resp[3] & IPMI_BMC_EVT_MSG_BUFF) {
183840112ae7SCorey Minyard 		/* buffer is already enabled, nothing to do. */
1839d9b7e4f7SCorey Minyard 		smi_info->supports_event_msg_buff = true;
184040112ae7SCorey Minyard 		goto out;
1841d9b7e4f7SCorey Minyard 	}
184240112ae7SCorey Minyard 
184340112ae7SCorey Minyard 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
184440112ae7SCorey Minyard 	msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
184540112ae7SCorey Minyard 	msg[2] = resp[3] | IPMI_BMC_EVT_MSG_BUFF;
184640112ae7SCorey Minyard 	smi_info->handlers->start_transaction(smi_info->si_sm, msg, 3);
184740112ae7SCorey Minyard 
184840112ae7SCorey Minyard 	rv = wait_for_msg_done(smi_info);
184940112ae7SCorey Minyard 	if (rv) {
1850bb2a08c0SCorey Minyard 		pr_warn(PFX "Error getting response from set global, enables command, the event buffer is not enabled.\n");
185140112ae7SCorey Minyard 		goto out;
185240112ae7SCorey Minyard 	}
185340112ae7SCorey Minyard 
185440112ae7SCorey Minyard 	resp_len = smi_info->handlers->get_result(smi_info->si_sm,
185540112ae7SCorey Minyard 						  resp, IPMI_MAX_MSG_LENGTH);
185640112ae7SCorey Minyard 
185740112ae7SCorey Minyard 	if (resp_len < 3 ||
185840112ae7SCorey Minyard 			resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
185940112ae7SCorey Minyard 			resp[1] != IPMI_SET_BMC_GLOBAL_ENABLES_CMD) {
1860bb2a08c0SCorey Minyard 		pr_warn(PFX "Invalid return from get global, enables command, not enable the event buffer.\n");
186140112ae7SCorey Minyard 		rv = -EINVAL;
186240112ae7SCorey Minyard 		goto out;
186340112ae7SCorey Minyard 	}
186440112ae7SCorey Minyard 
186540112ae7SCorey Minyard 	if (resp[2] != 0)
186640112ae7SCorey Minyard 		/*
186740112ae7SCorey Minyard 		 * An error when setting the event buffer bit means
186840112ae7SCorey Minyard 		 * that the event buffer is not supported.
186940112ae7SCorey Minyard 		 */
187040112ae7SCorey Minyard 		rv = -ENOENT;
1871d9b7e4f7SCorey Minyard 	else
1872d9b7e4f7SCorey Minyard 		smi_info->supports_event_msg_buff = true;
1873d9b7e4f7SCorey Minyard 
187440112ae7SCorey Minyard out:
187540112ae7SCorey Minyard 	kfree(resp);
187640112ae7SCorey Minyard 	return rv;
187740112ae7SCorey Minyard }
187840112ae7SCorey Minyard 
187907412736SAlexey Dobriyan static int smi_type_proc_show(struct seq_file *m, void *v)
18801da177e4SLinus Torvalds {
188107412736SAlexey Dobriyan 	struct smi_info *smi = m->private;
18821da177e4SLinus Torvalds 
1883910840f2SCorey Minyard 	seq_printf(m, "%s\n", si_to_str[smi->io.si_type]);
1884d6c5dc18SJoe Perches 
18855e33cd0cSJoe Perches 	return 0;
18861da177e4SLinus Torvalds }
18871da177e4SLinus Torvalds 
188807412736SAlexey Dobriyan static int smi_type_proc_open(struct inode *inode, struct file *file)
18891da177e4SLinus Torvalds {
1890d9dda78bSAl Viro 	return single_open(file, smi_type_proc_show, PDE_DATA(inode));
189107412736SAlexey Dobriyan }
18921da177e4SLinus Torvalds 
189307412736SAlexey Dobriyan static const struct file_operations smi_type_proc_ops = {
189407412736SAlexey Dobriyan 	.open		= smi_type_proc_open,
189507412736SAlexey Dobriyan 	.read		= seq_read,
189607412736SAlexey Dobriyan 	.llseek		= seq_lseek,
189707412736SAlexey Dobriyan 	.release	= single_release,
189807412736SAlexey Dobriyan };
189907412736SAlexey Dobriyan 
190007412736SAlexey Dobriyan static int smi_si_stats_proc_show(struct seq_file *m, void *v)
190107412736SAlexey Dobriyan {
190207412736SAlexey Dobriyan 	struct smi_info *smi = m->private;
190307412736SAlexey Dobriyan 
190407412736SAlexey Dobriyan 	seq_printf(m, "interrupts_enabled:    %d\n",
1905910840f2SCorey Minyard 		       smi->io.irq && !smi->interrupt_disabled);
190607412736SAlexey Dobriyan 	seq_printf(m, "short_timeouts:        %u\n",
190764959e2dSCorey Minyard 		       smi_get_stat(smi, short_timeouts));
190807412736SAlexey Dobriyan 	seq_printf(m, "long_timeouts:         %u\n",
190964959e2dSCorey Minyard 		       smi_get_stat(smi, long_timeouts));
191007412736SAlexey Dobriyan 	seq_printf(m, "idles:                 %u\n",
191164959e2dSCorey Minyard 		       smi_get_stat(smi, idles));
191207412736SAlexey Dobriyan 	seq_printf(m, "interrupts:            %u\n",
191364959e2dSCorey Minyard 		       smi_get_stat(smi, interrupts));
191407412736SAlexey Dobriyan 	seq_printf(m, "attentions:            %u\n",
191564959e2dSCorey Minyard 		       smi_get_stat(smi, attentions));
191607412736SAlexey Dobriyan 	seq_printf(m, "flag_fetches:          %u\n",
191764959e2dSCorey Minyard 		       smi_get_stat(smi, flag_fetches));
191807412736SAlexey Dobriyan 	seq_printf(m, "hosed_count:           %u\n",
191964959e2dSCorey Minyard 		       smi_get_stat(smi, hosed_count));
192007412736SAlexey Dobriyan 	seq_printf(m, "complete_transactions: %u\n",
192164959e2dSCorey Minyard 		       smi_get_stat(smi, complete_transactions));
192207412736SAlexey Dobriyan 	seq_printf(m, "events:                %u\n",
192364959e2dSCorey Minyard 		       smi_get_stat(smi, events));
192407412736SAlexey Dobriyan 	seq_printf(m, "watchdog_pretimeouts:  %u\n",
192564959e2dSCorey Minyard 		       smi_get_stat(smi, watchdog_pretimeouts));
192607412736SAlexey Dobriyan 	seq_printf(m, "incoming_messages:     %u\n",
192764959e2dSCorey Minyard 		       smi_get_stat(smi, incoming_messages));
192807412736SAlexey Dobriyan 	return 0;
1929b361e27bSCorey Minyard }
1930b361e27bSCorey Minyard 
193107412736SAlexey Dobriyan static int smi_si_stats_proc_open(struct inode *inode, struct file *file)
1932b361e27bSCorey Minyard {
1933d9dda78bSAl Viro 	return single_open(file, smi_si_stats_proc_show, PDE_DATA(inode));
193407412736SAlexey Dobriyan }
1935b361e27bSCorey Minyard 
193607412736SAlexey Dobriyan static const struct file_operations smi_si_stats_proc_ops = {
193707412736SAlexey Dobriyan 	.open		= smi_si_stats_proc_open,
193807412736SAlexey Dobriyan 	.read		= seq_read,
193907412736SAlexey Dobriyan 	.llseek		= seq_lseek,
194007412736SAlexey Dobriyan 	.release	= single_release,
194107412736SAlexey Dobriyan };
194207412736SAlexey Dobriyan 
194307412736SAlexey Dobriyan static int smi_params_proc_show(struct seq_file *m, void *v)
194407412736SAlexey Dobriyan {
194507412736SAlexey Dobriyan 	struct smi_info *smi = m->private;
194607412736SAlexey Dobriyan 
1947d6c5dc18SJoe Perches 	seq_printf(m,
1948b361e27bSCorey Minyard 		   "%s,%s,0x%lx,rsp=%d,rsi=%d,rsh=%d,irq=%d,ipmb=%d\n",
1949910840f2SCorey Minyard 		   si_to_str[smi->io.si_type],
1950b361e27bSCorey Minyard 		   addr_space_to_str[smi->io.addr_type],
1951b361e27bSCorey Minyard 		   smi->io.addr_data,
1952b361e27bSCorey Minyard 		   smi->io.regspacing,
1953b361e27bSCorey Minyard 		   smi->io.regsize,
1954b361e27bSCorey Minyard 		   smi->io.regshift,
1955910840f2SCorey Minyard 		   smi->io.irq,
1956910840f2SCorey Minyard 		   smi->io.slave_addr);
1957d6c5dc18SJoe Perches 
19585e33cd0cSJoe Perches 	return 0;
19591da177e4SLinus Torvalds }
19601da177e4SLinus Torvalds 
196107412736SAlexey Dobriyan static int smi_params_proc_open(struct inode *inode, struct file *file)
196207412736SAlexey Dobriyan {
1963d9dda78bSAl Viro 	return single_open(file, smi_params_proc_show, PDE_DATA(inode));
196407412736SAlexey Dobriyan }
196507412736SAlexey Dobriyan 
196607412736SAlexey Dobriyan static const struct file_operations smi_params_proc_ops = {
196707412736SAlexey Dobriyan 	.open		= smi_params_proc_open,
196807412736SAlexey Dobriyan 	.read		= seq_read,
196907412736SAlexey Dobriyan 	.llseek		= seq_lseek,
197007412736SAlexey Dobriyan 	.release	= single_release,
197107412736SAlexey Dobriyan };
197207412736SAlexey Dobriyan 
19733ae0e0f9SCorey Minyard /*
19743ae0e0f9SCorey Minyard  * oem_data_avail_to_receive_msg_avail
19753ae0e0f9SCorey Minyard  * @info - smi_info structure with msg_flags set
19763ae0e0f9SCorey Minyard  *
19773ae0e0f9SCorey Minyard  * Converts flags from OEM_DATA_AVAIL to RECEIVE_MSG_AVAIL
19783ae0e0f9SCorey Minyard  * Returns 1 indicating need to re-run handle_flags().
19793ae0e0f9SCorey Minyard  */
19803ae0e0f9SCorey Minyard static int oem_data_avail_to_receive_msg_avail(struct smi_info *smi_info)
19813ae0e0f9SCorey Minyard {
1982e8b33617SCorey Minyard 	smi_info->msg_flags = ((smi_info->msg_flags & ~OEM_DATA_AVAIL) |
1983e8b33617SCorey Minyard 			       RECEIVE_MSG_AVAIL);
19843ae0e0f9SCorey Minyard 	return 1;
19853ae0e0f9SCorey Minyard }
19863ae0e0f9SCorey Minyard 
19873ae0e0f9SCorey Minyard /*
19883ae0e0f9SCorey Minyard  * setup_dell_poweredge_oem_data_handler
19893ae0e0f9SCorey Minyard  * @info - smi_info.device_id must be populated
19903ae0e0f9SCorey Minyard  *
19913ae0e0f9SCorey Minyard  * Systems that match, but have firmware version < 1.40 may assert
19923ae0e0f9SCorey Minyard  * OEM0_DATA_AVAIL on their own, without being told via Set Flags that
19933ae0e0f9SCorey Minyard  * it's safe to do so.  Such systems will de-assert OEM1_DATA_AVAIL
19943ae0e0f9SCorey Minyard  * upon receipt of IPMI_GET_MSG_CMD, so we should treat these flags
19953ae0e0f9SCorey Minyard  * as RECEIVE_MSG_AVAIL instead.
19963ae0e0f9SCorey Minyard  *
19973ae0e0f9SCorey Minyard  * As Dell has no plans to release IPMI 1.5 firmware that *ever*
19983ae0e0f9SCorey Minyard  * assert the OEM[012] bits, and if it did, the driver would have to
19993ae0e0f9SCorey Minyard  * change to handle that properly, we don't actually check for the
20003ae0e0f9SCorey Minyard  * firmware version.
20013ae0e0f9SCorey Minyard  * Device ID = 0x20                BMC on PowerEdge 8G servers
20023ae0e0f9SCorey Minyard  * Device Revision = 0x80
20033ae0e0f9SCorey Minyard  * Firmware Revision1 = 0x01       BMC version 1.40
20043ae0e0f9SCorey Minyard  * Firmware Revision2 = 0x40       BCD encoded
20053ae0e0f9SCorey Minyard  * IPMI Version = 0x51             IPMI 1.5
20063ae0e0f9SCorey Minyard  * Manufacturer ID = A2 02 00      Dell IANA
20073ae0e0f9SCorey Minyard  *
2008d5a2b89aSCorey Minyard  * Additionally, PowerEdge systems with IPMI < 1.5 may also assert
2009d5a2b89aSCorey Minyard  * OEM0_DATA_AVAIL and needs to be treated as RECEIVE_MSG_AVAIL.
2010d5a2b89aSCorey Minyard  *
20113ae0e0f9SCorey Minyard  */
20123ae0e0f9SCorey Minyard #define DELL_POWEREDGE_8G_BMC_DEVICE_ID  0x20
20133ae0e0f9SCorey Minyard #define DELL_POWEREDGE_8G_BMC_DEVICE_REV 0x80
20143ae0e0f9SCorey Minyard #define DELL_POWEREDGE_8G_BMC_IPMI_VERSION 0x51
201550c812b2SCorey Minyard #define DELL_IANA_MFR_ID 0x0002a2
20163ae0e0f9SCorey Minyard static void setup_dell_poweredge_oem_data_handler(struct smi_info *smi_info)
20173ae0e0f9SCorey Minyard {
20183ae0e0f9SCorey Minyard 	struct ipmi_device_id *id = &smi_info->device_id;
201950c812b2SCorey Minyard 	if (id->manufacturer_id == DELL_IANA_MFR_ID) {
2020d5a2b89aSCorey Minyard 		if (id->device_id       == DELL_POWEREDGE_8G_BMC_DEVICE_ID  &&
2021d5a2b89aSCorey Minyard 		    id->device_revision == DELL_POWEREDGE_8G_BMC_DEVICE_REV &&
2022d5a2b89aSCorey Minyard 		    id->ipmi_version   == DELL_POWEREDGE_8G_BMC_IPMI_VERSION) {
20233ae0e0f9SCorey Minyard 			smi_info->oem_data_avail_handler =
20243ae0e0f9SCorey Minyard 				oem_data_avail_to_receive_msg_avail;
2025c305e3d3SCorey Minyard 		} else if (ipmi_version_major(id) < 1 ||
2026d5a2b89aSCorey Minyard 			   (ipmi_version_major(id) == 1 &&
2027d5a2b89aSCorey Minyard 			    ipmi_version_minor(id) < 5)) {
2028d5a2b89aSCorey Minyard 			smi_info->oem_data_avail_handler =
2029d5a2b89aSCorey Minyard 				oem_data_avail_to_receive_msg_avail;
2030d5a2b89aSCorey Minyard 		}
2031d5a2b89aSCorey Minyard 	}
20323ae0e0f9SCorey Minyard }
20333ae0e0f9SCorey Minyard 
2034ea94027bSCorey Minyard #define CANNOT_RETURN_REQUESTED_LENGTH 0xCA
2035ea94027bSCorey Minyard static void return_hosed_msg_badsize(struct smi_info *smi_info)
2036ea94027bSCorey Minyard {
2037ea94027bSCorey Minyard 	struct ipmi_smi_msg *msg = smi_info->curr_msg;
2038ea94027bSCorey Minyard 
203925985edcSLucas De Marchi 	/* Make it a response */
2040ea94027bSCorey Minyard 	msg->rsp[0] = msg->data[0] | 4;
2041ea94027bSCorey Minyard 	msg->rsp[1] = msg->data[1];
2042ea94027bSCorey Minyard 	msg->rsp[2] = CANNOT_RETURN_REQUESTED_LENGTH;
2043ea94027bSCorey Minyard 	msg->rsp_size = 3;
2044ea94027bSCorey Minyard 	smi_info->curr_msg = NULL;
2045ea94027bSCorey Minyard 	deliver_recv_msg(smi_info, msg);
2046ea94027bSCorey Minyard }
2047ea94027bSCorey Minyard 
2048ea94027bSCorey Minyard /*
2049ea94027bSCorey Minyard  * dell_poweredge_bt_xaction_handler
2050ea94027bSCorey Minyard  * @info - smi_info.device_id must be populated
2051ea94027bSCorey Minyard  *
2052ea94027bSCorey Minyard  * Dell PowerEdge servers with the BT interface (x6xx and 1750) will
2053ea94027bSCorey Minyard  * not respond to a Get SDR command if the length of the data
2054ea94027bSCorey Minyard  * requested is exactly 0x3A, which leads to command timeouts and no
2055ea94027bSCorey Minyard  * data returned.  This intercepts such commands, and causes userspace
2056ea94027bSCorey Minyard  * callers to try again with a different-sized buffer, which succeeds.
2057ea94027bSCorey Minyard  */
2058ea94027bSCorey Minyard 
2059ea94027bSCorey Minyard #define STORAGE_NETFN 0x0A
2060ea94027bSCorey Minyard #define STORAGE_CMD_GET_SDR 0x23
2061ea94027bSCorey Minyard static int dell_poweredge_bt_xaction_handler(struct notifier_block *self,
2062ea94027bSCorey Minyard 					     unsigned long unused,
2063ea94027bSCorey Minyard 					     void *in)
2064ea94027bSCorey Minyard {
2065ea94027bSCorey Minyard 	struct smi_info *smi_info = in;
2066ea94027bSCorey Minyard 	unsigned char *data = smi_info->curr_msg->data;
2067ea94027bSCorey Minyard 	unsigned int size   = smi_info->curr_msg->data_size;
2068ea94027bSCorey Minyard 	if (size >= 8 &&
2069ea94027bSCorey Minyard 	    (data[0]>>2) == STORAGE_NETFN &&
2070ea94027bSCorey Minyard 	    data[1] == STORAGE_CMD_GET_SDR &&
2071ea94027bSCorey Minyard 	    data[7] == 0x3A) {
2072ea94027bSCorey Minyard 		return_hosed_msg_badsize(smi_info);
2073ea94027bSCorey Minyard 		return NOTIFY_STOP;
2074ea94027bSCorey Minyard 	}
2075ea94027bSCorey Minyard 	return NOTIFY_DONE;
2076ea94027bSCorey Minyard }
2077ea94027bSCorey Minyard 
2078ea94027bSCorey Minyard static struct notifier_block dell_poweredge_bt_xaction_notifier = {
2079ea94027bSCorey Minyard 	.notifier_call	= dell_poweredge_bt_xaction_handler,
2080ea94027bSCorey Minyard };
2081ea94027bSCorey Minyard 
2082ea94027bSCorey Minyard /*
2083ea94027bSCorey Minyard  * setup_dell_poweredge_bt_xaction_handler
2084ea94027bSCorey Minyard  * @info - smi_info.device_id must be filled in already
2085ea94027bSCorey Minyard  *
2086ea94027bSCorey Minyard  * Fills in smi_info.device_id.start_transaction_pre_hook
2087ea94027bSCorey Minyard  * when we know what function to use there.
2088ea94027bSCorey Minyard  */
2089ea94027bSCorey Minyard static void
2090ea94027bSCorey Minyard setup_dell_poweredge_bt_xaction_handler(struct smi_info *smi_info)
2091ea94027bSCorey Minyard {
2092ea94027bSCorey Minyard 	struct ipmi_device_id *id = &smi_info->device_id;
209350c812b2SCorey Minyard 	if (id->manufacturer_id == DELL_IANA_MFR_ID &&
2094910840f2SCorey Minyard 	    smi_info->io.si_type == SI_BT)
2095ea94027bSCorey Minyard 		register_xaction_notifier(&dell_poweredge_bt_xaction_notifier);
2096ea94027bSCorey Minyard }
2097ea94027bSCorey Minyard 
20983ae0e0f9SCorey Minyard /*
20993ae0e0f9SCorey Minyard  * setup_oem_data_handler
21003ae0e0f9SCorey Minyard  * @info - smi_info.device_id must be filled in already
21013ae0e0f9SCorey Minyard  *
21023ae0e0f9SCorey Minyard  * Fills in smi_info.device_id.oem_data_available_handler
21033ae0e0f9SCorey Minyard  * when we know what function to use there.
21043ae0e0f9SCorey Minyard  */
21053ae0e0f9SCorey Minyard 
21063ae0e0f9SCorey Minyard static void setup_oem_data_handler(struct smi_info *smi_info)
21073ae0e0f9SCorey Minyard {
21083ae0e0f9SCorey Minyard 	setup_dell_poweredge_oem_data_handler(smi_info);
21093ae0e0f9SCorey Minyard }
21103ae0e0f9SCorey Minyard 
2111ea94027bSCorey Minyard static void setup_xaction_handlers(struct smi_info *smi_info)
2112ea94027bSCorey Minyard {
2113ea94027bSCorey Minyard 	setup_dell_poweredge_bt_xaction_handler(smi_info);
2114ea94027bSCorey Minyard }
2115ea94027bSCorey Minyard 
2116d0882897SCorey Minyard static void check_for_broken_irqs(struct smi_info *smi_info)
2117d0882897SCorey Minyard {
2118d0882897SCorey Minyard 	check_clr_rcv_irq(smi_info);
2119d0882897SCorey Minyard 	check_set_rcv_irq(smi_info);
2120d0882897SCorey Minyard }
2121d0882897SCorey Minyard 
2122a9a2c44fSCorey Minyard static inline void wait_for_timer_and_thread(struct smi_info *smi_info)
2123a9a2c44fSCorey Minyard {
2124453823baSCorey Minyard 	if (smi_info->thread != NULL)
2125e9a705a0SMatt Domsch 		kthread_stop(smi_info->thread);
2126b874b985SCorey Minyard 	if (smi_info->timer_running)
2127a9a2c44fSCorey Minyard 		del_timer_sync(&smi_info->si_timer);
2128a9a2c44fSCorey Minyard }
2129a9a2c44fSCorey Minyard 
21307e030d6dSCorey Minyard static struct smi_info *find_dup_si(struct smi_info *info)
2131b0defcdbSCorey Minyard {
2132b0defcdbSCorey Minyard 	struct smi_info *e;
2133b0defcdbSCorey Minyard 
2134b0defcdbSCorey Minyard 	list_for_each_entry(e, &smi_infos, link) {
2135b0defcdbSCorey Minyard 		if (e->io.addr_type != info->io.addr_type)
2136b0defcdbSCorey Minyard 			continue;
213794671710SCorey Minyard 		if (e->io.addr_data == info->io.addr_data) {
213894671710SCorey Minyard 			/*
213994671710SCorey Minyard 			 * This is a cheap hack, ACPI doesn't have a defined
214094671710SCorey Minyard 			 * slave address but SMBIOS does.  Pick it up from
214194671710SCorey Minyard 			 * any source that has it available.
214294671710SCorey Minyard 			 */
2143910840f2SCorey Minyard 			if (info->io.slave_addr && !e->io.slave_addr)
2144910840f2SCorey Minyard 				e->io.slave_addr = info->io.slave_addr;
21457e030d6dSCorey Minyard 			return e;
2146b0defcdbSCorey Minyard 		}
214794671710SCorey Minyard 	}
2148b0defcdbSCorey Minyard 
21497e030d6dSCorey Minyard 	return NULL;
2150b0defcdbSCorey Minyard }
2151b0defcdbSCorey Minyard 
2152bb398a4cSCorey Minyard int ipmi_si_add_smi(struct si_sm_io *io)
21532407d77aSMatthew Garrett {
21542407d77aSMatthew Garrett 	int rv = 0;
2155bb398a4cSCorey Minyard 	struct smi_info *new_smi, *dup;
21562407d77aSMatthew Garrett 
2157bb398a4cSCorey Minyard 	if (!io->io_setup) {
2158bb398a4cSCorey Minyard 		if (io->addr_type == IPMI_IO_ADDR_SPACE) {
2159bb398a4cSCorey Minyard 			io->io_setup = port_setup;
2160bb398a4cSCorey Minyard 		} else if (io->addr_type == IPMI_MEM_ADDR_SPACE) {
2161bb398a4cSCorey Minyard 			io->io_setup = mem_setup;
2162e1eeb7f8SCorey Minyard 		} else {
2163e1eeb7f8SCorey Minyard 			return -EINVAL;
2164e1eeb7f8SCorey Minyard 		}
2165e1eeb7f8SCorey Minyard 	}
2166e1eeb7f8SCorey Minyard 
2167bb398a4cSCorey Minyard 	new_smi = smi_info_alloc();
2168bb398a4cSCorey Minyard 	if (!new_smi)
2169bb398a4cSCorey Minyard 		return -ENOMEM;
2170bb398a4cSCorey Minyard 
2171bb398a4cSCorey Minyard 	new_smi->io = *io;
2172bb398a4cSCorey Minyard 
21732407d77aSMatthew Garrett 	mutex_lock(&smi_infos_lock);
21747e030d6dSCorey Minyard 	dup = find_dup_si(new_smi);
21757e030d6dSCorey Minyard 	if (dup) {
2176910840f2SCorey Minyard 		if (new_smi->io.addr_source == SI_ACPI &&
2177910840f2SCorey Minyard 		    dup->io.addr_source == SI_SMBIOS) {
21787e030d6dSCorey Minyard 			/* We prefer ACPI over SMBIOS. */
2179910840f2SCorey Minyard 			dev_info(dup->io.dev,
21807e030d6dSCorey Minyard 				 "Removing SMBIOS-specified %s state machine in favor of ACPI\n",
2181910840f2SCorey Minyard 				 si_to_str[new_smi->io.si_type]);
21827e030d6dSCorey Minyard 			cleanup_one_si(dup);
21837e030d6dSCorey Minyard 		} else {
2184910840f2SCorey Minyard 			dev_info(new_smi->io.dev,
21857e030d6dSCorey Minyard 				 "%s-specified %s state machine: duplicate\n",
2186910840f2SCorey Minyard 				 ipmi_addr_src_to_str(new_smi->io.addr_source),
2187910840f2SCorey Minyard 				 si_to_str[new_smi->io.si_type]);
21882407d77aSMatthew Garrett 			rv = -EBUSY;
21892407d77aSMatthew Garrett 			goto out_err;
21902407d77aSMatthew Garrett 		}
21917e030d6dSCorey Minyard 	}
21922407d77aSMatthew Garrett 
2193bb2a08c0SCorey Minyard 	pr_info(PFX "Adding %s-specified %s state machine\n",
2194910840f2SCorey Minyard 		ipmi_addr_src_to_str(new_smi->io.addr_source),
2195910840f2SCorey Minyard 		si_to_str[new_smi->io.si_type]);
21962407d77aSMatthew Garrett 
21972407d77aSMatthew Garrett 	/* So we know not to free it unless we have allocated one. */
21982407d77aSMatthew Garrett 	new_smi->intf = NULL;
21992407d77aSMatthew Garrett 	new_smi->si_sm = NULL;
22002407d77aSMatthew Garrett 	new_smi->handlers = NULL;
22012407d77aSMatthew Garrett 
22022407d77aSMatthew Garrett 	list_add_tail(&new_smi->link, &smi_infos);
22032407d77aSMatthew Garrett 
2204bb398a4cSCorey Minyard 	if (initialized) {
2205bb398a4cSCorey Minyard 		rv = try_smi_init(new_smi);
2206bb398a4cSCorey Minyard 		if (rv) {
2207bb398a4cSCorey Minyard 			mutex_unlock(&smi_infos_lock);
2208bb398a4cSCorey Minyard 			cleanup_one_si(new_smi);
2209bb398a4cSCorey Minyard 			return rv;
2210bb398a4cSCorey Minyard 		}
2211bb398a4cSCorey Minyard 	}
22122407d77aSMatthew Garrett out_err:
22132407d77aSMatthew Garrett 	mutex_unlock(&smi_infos_lock);
22142407d77aSMatthew Garrett 	return rv;
22152407d77aSMatthew Garrett }
22162407d77aSMatthew Garrett 
22173f724c40STony Camuso /*
22183f724c40STony Camuso  * Try to start up an interface.  Must be called with smi_infos_lock
22193f724c40STony Camuso  * held, primarily to keep smi_num consistent, we only one to do these
22203f724c40STony Camuso  * one at a time.
22213f724c40STony Camuso  */
2222b0defcdbSCorey Minyard static int try_smi_init(struct smi_info *new_smi)
22231da177e4SLinus Torvalds {
22242407d77aSMatthew Garrett 	int rv = 0;
222564959e2dSCorey Minyard 	int i;
22261abf71eeSCorey Minyard 	char *init_name = NULL;
22271da177e4SLinus Torvalds 
2228bb2a08c0SCorey Minyard 	pr_info(PFX "Trying %s-specified %s state machine at %s address 0x%lx, slave address 0x%x, irq %d\n",
2229910840f2SCorey Minyard 		ipmi_addr_src_to_str(new_smi->io.addr_source),
2230910840f2SCorey Minyard 		si_to_str[new_smi->io.si_type],
2231b0defcdbSCorey Minyard 		addr_space_to_str[new_smi->io.addr_type],
2232b0defcdbSCorey Minyard 		new_smi->io.addr_data,
2233910840f2SCorey Minyard 		new_smi->io.slave_addr, new_smi->io.irq);
22341da177e4SLinus Torvalds 
2235910840f2SCorey Minyard 	switch (new_smi->io.si_type) {
2236b0defcdbSCorey Minyard 	case SI_KCS:
22371da177e4SLinus Torvalds 		new_smi->handlers = &kcs_smi_handlers;
2238b0defcdbSCorey Minyard 		break;
2239b0defcdbSCorey Minyard 
2240b0defcdbSCorey Minyard 	case SI_SMIC:
22411da177e4SLinus Torvalds 		new_smi->handlers = &smic_smi_handlers;
2242b0defcdbSCorey Minyard 		break;
2243b0defcdbSCorey Minyard 
2244b0defcdbSCorey Minyard 	case SI_BT:
22451da177e4SLinus Torvalds 		new_smi->handlers = &bt_smi_handlers;
2246b0defcdbSCorey Minyard 		break;
2247b0defcdbSCorey Minyard 
2248b0defcdbSCorey Minyard 	default:
22491da177e4SLinus Torvalds 		/* No support for anything else yet. */
22501da177e4SLinus Torvalds 		rv = -EIO;
22511da177e4SLinus Torvalds 		goto out_err;
22521da177e4SLinus Torvalds 	}
22531da177e4SLinus Torvalds 
22543f724c40STony Camuso 	new_smi->intf_num = smi_num;
22553f724c40STony Camuso 
22561abf71eeSCorey Minyard 	/* Do this early so it's available for logs. */
2257910840f2SCorey Minyard 	if (!new_smi->io.dev) {
22583f724c40STony Camuso 		init_name = kasprintf(GFP_KERNEL, "ipmi_si.%d",
22593f724c40STony Camuso 				      new_smi->intf_num);
22601abf71eeSCorey Minyard 
22611abf71eeSCorey Minyard 		/*
22621abf71eeSCorey Minyard 		 * If we don't already have a device from something
22631abf71eeSCorey Minyard 		 * else (like PCI), then register a new one.
22641abf71eeSCorey Minyard 		 */
22651abf71eeSCorey Minyard 		new_smi->pdev = platform_device_alloc("ipmi_si",
22661abf71eeSCorey Minyard 						      new_smi->intf_num);
22671abf71eeSCorey Minyard 		if (!new_smi->pdev) {
22681abf71eeSCorey Minyard 			pr_err(PFX "Unable to allocate platform device\n");
22691abf71eeSCorey Minyard 			goto out_err;
22701abf71eeSCorey Minyard 		}
2271910840f2SCorey Minyard 		new_smi->io.dev = &new_smi->pdev->dev;
22729d70029eSCorey Minyard 		new_smi->io.dev->driver = &ipmi_platform_driver.driver;
22731abf71eeSCorey Minyard 		/* Nulled by device_add() */
2274910840f2SCorey Minyard 		new_smi->io.dev->init_name = init_name;
22751abf71eeSCorey Minyard 	}
22761abf71eeSCorey Minyard 
22771da177e4SLinus Torvalds 	/* Allocate the state machine's data and initialize it. */
22781da177e4SLinus Torvalds 	new_smi->si_sm = kmalloc(new_smi->handlers->size(), GFP_KERNEL);
22791da177e4SLinus Torvalds 	if (!new_smi->si_sm) {
2280bb2a08c0SCorey Minyard 		pr_err(PFX "Could not allocate state machine memory\n");
22811da177e4SLinus Torvalds 		rv = -ENOMEM;
22821da177e4SLinus Torvalds 		goto out_err;
22831da177e4SLinus Torvalds 	}
2284e1eeb7f8SCorey Minyard 	new_smi->io.io_size = new_smi->handlers->init_data(new_smi->si_sm,
22851da177e4SLinus Torvalds 							   &new_smi->io);
22861da177e4SLinus Torvalds 
22871da177e4SLinus Torvalds 	/* Now that we know the I/O size, we can set up the I/O. */
2288e1eeb7f8SCorey Minyard 	rv = new_smi->io.io_setup(&new_smi->io);
22891da177e4SLinus Torvalds 	if (rv) {
2290910840f2SCorey Minyard 		dev_err(new_smi->io.dev, "Could not set up I/O space\n");
22911da177e4SLinus Torvalds 		goto out_err;
22921da177e4SLinus Torvalds 	}
22931da177e4SLinus Torvalds 
22941da177e4SLinus Torvalds 	/* Do low-level detection first. */
22951da177e4SLinus Torvalds 	if (new_smi->handlers->detect(new_smi->si_sm)) {
2296910840f2SCorey Minyard 		if (new_smi->io.addr_source)
2297910840f2SCorey Minyard 			dev_err(new_smi->io.dev,
2298910840f2SCorey Minyard 				"Interface detection failed\n");
22991da177e4SLinus Torvalds 		rv = -ENODEV;
23001da177e4SLinus Torvalds 		goto out_err;
23011da177e4SLinus Torvalds 	}
23021da177e4SLinus Torvalds 
2303c305e3d3SCorey Minyard 	/*
2304c305e3d3SCorey Minyard 	 * Attempt a get device id command.  If it fails, we probably
2305c305e3d3SCorey Minyard 	 * don't have a BMC here.
2306c305e3d3SCorey Minyard 	 */
23071da177e4SLinus Torvalds 	rv = try_get_dev_id(new_smi);
2308b0defcdbSCorey Minyard 	if (rv) {
2309910840f2SCorey Minyard 		if (new_smi->io.addr_source)
2310910840f2SCorey Minyard 			dev_err(new_smi->io.dev,
2311910840f2SCorey Minyard 			       "There appears to be no BMC at this location\n");
23121da177e4SLinus Torvalds 		goto out_err;
2313b0defcdbSCorey Minyard 	}
23141da177e4SLinus Torvalds 
23153ae0e0f9SCorey Minyard 	setup_oem_data_handler(new_smi);
2316ea94027bSCorey Minyard 	setup_xaction_handlers(new_smi);
2317d0882897SCorey Minyard 	check_for_broken_irqs(new_smi);
23183ae0e0f9SCorey Minyard 
2319b874b985SCorey Minyard 	new_smi->waiting_msg = NULL;
23201da177e4SLinus Torvalds 	new_smi->curr_msg = NULL;
23211da177e4SLinus Torvalds 	atomic_set(&new_smi->req_events, 0);
23227aefac26SCorey Minyard 	new_smi->run_to_completion = false;
232364959e2dSCorey Minyard 	for (i = 0; i < SI_NUM_STATS; i++)
232464959e2dSCorey Minyard 		atomic_set(&new_smi->stats[i], 0);
23251da177e4SLinus Torvalds 
23267aefac26SCorey Minyard 	new_smi->interrupt_disabled = true;
232789986496SCorey Minyard 	atomic_set(&new_smi->need_watch, 0);
23281da177e4SLinus Torvalds 
232940112ae7SCorey Minyard 	rv = try_enable_event_buffer(new_smi);
233040112ae7SCorey Minyard 	if (rv == 0)
23317aefac26SCorey Minyard 		new_smi->has_event_buffer = true;
233240112ae7SCorey Minyard 
2333c305e3d3SCorey Minyard 	/*
2334c305e3d3SCorey Minyard 	 * Start clearing the flags before we enable interrupts or the
2335c305e3d3SCorey Minyard 	 * timer to avoid racing with the timer.
2336c305e3d3SCorey Minyard 	 */
23370cfec916SCorey Minyard 	start_clear_flags(new_smi, false);
2338d9b7e4f7SCorey Minyard 
2339d9b7e4f7SCorey Minyard 	/*
2340d9b7e4f7SCorey Minyard 	 * IRQ is defined to be set when non-zero.  req_events will
2341d9b7e4f7SCorey Minyard 	 * cause a global flags check that will enable interrupts.
2342d9b7e4f7SCorey Minyard 	 */
2343910840f2SCorey Minyard 	if (new_smi->io.irq) {
2344d9b7e4f7SCorey Minyard 		new_smi->interrupt_disabled = false;
2345d9b7e4f7SCorey Minyard 		atomic_set(&new_smi->req_events, 1);
2346d9b7e4f7SCorey Minyard 	}
23471da177e4SLinus Torvalds 
23481abf71eeSCorey Minyard 	if (new_smi->pdev) {
2349b48f5457SZhang, Yanmin 		rv = platform_device_add(new_smi->pdev);
235050c812b2SCorey Minyard 		if (rv) {
2351910840f2SCorey Minyard 			dev_err(new_smi->io.dev,
2352bb2a08c0SCorey Minyard 				"Unable to register system interface device: %d\n",
235350c812b2SCorey Minyard 				rv);
2354453823baSCorey Minyard 			goto out_err;
235550c812b2SCorey Minyard 		}
235650c812b2SCorey Minyard 	}
235750c812b2SCorey Minyard 
23581da177e4SLinus Torvalds 	rv = ipmi_register_smi(&handlers,
23591da177e4SLinus Torvalds 			       new_smi,
2360910840f2SCorey Minyard 			       new_smi->io.dev,
2361910840f2SCorey Minyard 			       new_smi->io.slave_addr);
23621da177e4SLinus Torvalds 	if (rv) {
2363910840f2SCorey Minyard 		dev_err(new_smi->io.dev,
2364910840f2SCorey Minyard 			"Unable to register device: error %d\n",
23651da177e4SLinus Torvalds 			rv);
23661da177e4SLinus Torvalds 		goto out_err_stop_timer;
23671da177e4SLinus Torvalds 	}
23681da177e4SLinus Torvalds 
23691da177e4SLinus Torvalds 	rv = ipmi_smi_add_proc_entry(new_smi->intf, "type",
237007412736SAlexey Dobriyan 				     &smi_type_proc_ops,
237199b76233SAlexey Dobriyan 				     new_smi);
23721da177e4SLinus Torvalds 	if (rv) {
2373910840f2SCorey Minyard 		dev_err(new_smi->io.dev,
2374910840f2SCorey Minyard 			"Unable to create proc entry: %d\n", rv);
23751da177e4SLinus Torvalds 		goto out_err_stop_timer;
23761da177e4SLinus Torvalds 	}
23771da177e4SLinus Torvalds 
23781da177e4SLinus Torvalds 	rv = ipmi_smi_add_proc_entry(new_smi->intf, "si_stats",
237907412736SAlexey Dobriyan 				     &smi_si_stats_proc_ops,
238099b76233SAlexey Dobriyan 				     new_smi);
23811da177e4SLinus Torvalds 	if (rv) {
2382910840f2SCorey Minyard 		dev_err(new_smi->io.dev,
2383910840f2SCorey Minyard 			"Unable to create proc entry: %d\n", rv);
23841da177e4SLinus Torvalds 		goto out_err_stop_timer;
23851da177e4SLinus Torvalds 	}
23861da177e4SLinus Torvalds 
2387b361e27bSCorey Minyard 	rv = ipmi_smi_add_proc_entry(new_smi->intf, "params",
238807412736SAlexey Dobriyan 				     &smi_params_proc_ops,
238999b76233SAlexey Dobriyan 				     new_smi);
2390b361e27bSCorey Minyard 	if (rv) {
2391910840f2SCorey Minyard 		dev_err(new_smi->io.dev,
2392910840f2SCorey Minyard 			"Unable to create proc entry: %d\n", rv);
2393b361e27bSCorey Minyard 		goto out_err_stop_timer;
2394b361e27bSCorey Minyard 	}
2395b361e27bSCorey Minyard 
23963f724c40STony Camuso 	/* Don't increment till we know we have succeeded. */
23973f724c40STony Camuso 	smi_num++;
23983f724c40STony Camuso 
2399910840f2SCorey Minyard 	dev_info(new_smi->io.dev, "IPMI %s interface initialized\n",
2400910840f2SCorey Minyard 		 si_to_str[new_smi->io.si_type]);
24011da177e4SLinus Torvalds 
2402910840f2SCorey Minyard 	WARN_ON(new_smi->io.dev->init_name != NULL);
24031abf71eeSCorey Minyard 	kfree(init_name);
24041abf71eeSCorey Minyard 
24051da177e4SLinus Torvalds 	return 0;
24061da177e4SLinus Torvalds 
24071da177e4SLinus Torvalds out_err_stop_timer:
2408a9a2c44fSCorey Minyard 	wait_for_timer_and_thread(new_smi);
24091da177e4SLinus Torvalds 
24101da177e4SLinus Torvalds out_err:
24117aefac26SCorey Minyard 	new_smi->interrupt_disabled = true;
24121da177e4SLinus Torvalds 
24132407d77aSMatthew Garrett 	if (new_smi->intf) {
2414b874b985SCorey Minyard 		ipmi_smi_t intf = new_smi->intf;
24152407d77aSMatthew Garrett 		new_smi->intf = NULL;
2416b874b985SCorey Minyard 		ipmi_unregister_smi(intf);
24172407d77aSMatthew Garrett 	}
24182407d77aSMatthew Garrett 
24194f3e8199SCorey Minyard 	if (new_smi->io.irq_cleanup) {
24204f3e8199SCorey Minyard 		new_smi->io.irq_cleanup(&new_smi->io);
24214f3e8199SCorey Minyard 		new_smi->io.irq_cleanup = NULL;
24222407d77aSMatthew Garrett 	}
24231da177e4SLinus Torvalds 
2424c305e3d3SCorey Minyard 	/*
2425c305e3d3SCorey Minyard 	 * Wait until we know that we are out of any interrupt
2426c305e3d3SCorey Minyard 	 * handlers might have been running before we freed the
2427c305e3d3SCorey Minyard 	 * interrupt.
2428c305e3d3SCorey Minyard 	 */
2429fbd568a3SPaul E. McKenney 	synchronize_sched();
24301da177e4SLinus Torvalds 
24311da177e4SLinus Torvalds 	if (new_smi->si_sm) {
24321da177e4SLinus Torvalds 		if (new_smi->handlers)
24331da177e4SLinus Torvalds 			new_smi->handlers->cleanup(new_smi->si_sm);
24341da177e4SLinus Torvalds 		kfree(new_smi->si_sm);
24352407d77aSMatthew Garrett 		new_smi->si_sm = NULL;
24361da177e4SLinus Torvalds 	}
2437910840f2SCorey Minyard 	if (new_smi->io.addr_source_cleanup) {
2438910840f2SCorey Minyard 		new_smi->io.addr_source_cleanup(&new_smi->io);
2439910840f2SCorey Minyard 		new_smi->io.addr_source_cleanup = NULL;
24402407d77aSMatthew Garrett 	}
2441e1eeb7f8SCorey Minyard 	if (new_smi->io.io_cleanup) {
2442e1eeb7f8SCorey Minyard 		new_smi->io.io_cleanup(&new_smi->io);
2443e1eeb7f8SCorey Minyard 		new_smi->io.io_cleanup = NULL;
24442407d77aSMatthew Garrett 	}
24451da177e4SLinus Torvalds 
2446910840f2SCorey Minyard 	if (new_smi->pdev) {
244750c812b2SCorey Minyard 		platform_device_unregister(new_smi->pdev);
24481abf71eeSCorey Minyard 		new_smi->pdev = NULL;
24491abf71eeSCorey Minyard 	} else if (new_smi->pdev) {
24501abf71eeSCorey Minyard 		platform_device_put(new_smi->pdev);
24512407d77aSMatthew Garrett 	}
2452b0defcdbSCorey Minyard 
24531abf71eeSCorey Minyard 	kfree(init_name);
24541abf71eeSCorey Minyard 
24551da177e4SLinus Torvalds 	return rv;
24561da177e4SLinus Torvalds }
24571da177e4SLinus Torvalds 
24582223cbecSBill Pemberton static int init_ipmi_si(void)
24591da177e4SLinus Torvalds {
24602407d77aSMatthew Garrett 	struct smi_info *e;
246106ee4594SMatthew Garrett 	enum ipmi_addr_src type = SI_INVALID;
24621da177e4SLinus Torvalds 
24631da177e4SLinus Torvalds 	if (initialized)
24641da177e4SLinus Torvalds 		return 0;
24651da177e4SLinus Torvalds 
2466bb2a08c0SCorey Minyard 	pr_info("IPMI System Interface driver.\n");
24671da177e4SLinus Torvalds 
2468d8cc5267SMatthew Garrett 	/* If the user gave us a device, they presumably want us to use it */
24697a453308SCorey Minyard 	if (!ipmi_si_hardcode_find_bmc())
24707a453308SCorey Minyard 		goto do_scan;
2471d8cc5267SMatthew Garrett 
24729d70029eSCorey Minyard 	ipmi_si_platform_init();
24739d70029eSCorey Minyard 
247413d0b35cSCorey Minyard 	ipmi_si_pci_init();
2475b0defcdbSCorey Minyard 
2476*c6f85a75SCorey Minyard 	ipmi_si_parisc_init();
2477fdbeb7deSThomas Bogendoerfer 
247806ee4594SMatthew Garrett 	/* We prefer devices with interrupts, but in the case of a machine
247906ee4594SMatthew Garrett 	   with multiple BMCs we assume that there will be several instances
248006ee4594SMatthew Garrett 	   of a given type so if we succeed in registering a type then also
248106ee4594SMatthew Garrett 	   try to register everything else of the same type */
24827a453308SCorey Minyard do_scan:
24832407d77aSMatthew Garrett 	mutex_lock(&smi_infos_lock);
24842407d77aSMatthew Garrett 	list_for_each_entry(e, &smi_infos, link) {
248506ee4594SMatthew Garrett 		/* Try to register a device if it has an IRQ and we either
248606ee4594SMatthew Garrett 		   haven't successfully registered a device yet or this
248706ee4594SMatthew Garrett 		   device has the same type as one we successfully registered */
2488910840f2SCorey Minyard 		if (e->io.irq && (!type || e->io.addr_source == type)) {
2489d8cc5267SMatthew Garrett 			if (!try_smi_init(e)) {
2490910840f2SCorey Minyard 				type = e->io.addr_source;
249106ee4594SMatthew Garrett 			}
249206ee4594SMatthew Garrett 		}
249306ee4594SMatthew Garrett 	}
249406ee4594SMatthew Garrett 
249506ee4594SMatthew Garrett 	/* type will only have been set if we successfully registered an si */
2496bb398a4cSCorey Minyard 	if (type)
2497bb398a4cSCorey Minyard 		goto skip_fallback_noirq;
2498d8cc5267SMatthew Garrett 
2499d8cc5267SMatthew Garrett 	/* Fall back to the preferred device */
2500d8cc5267SMatthew Garrett 
2501d8cc5267SMatthew Garrett 	list_for_each_entry(e, &smi_infos, link) {
2502910840f2SCorey Minyard 		if (!e->io.irq && (!type || e->io.addr_source == type)) {
2503d8cc5267SMatthew Garrett 			if (!try_smi_init(e)) {
2504910840f2SCorey Minyard 				type = e->io.addr_source;
250506ee4594SMatthew Garrett 			}
250606ee4594SMatthew Garrett 		}
250706ee4594SMatthew Garrett 	}
2508bb398a4cSCorey Minyard 
2509bb398a4cSCorey Minyard skip_fallback_noirq:
2510bb398a4cSCorey Minyard 	initialized = 1;
2511d8cc5267SMatthew Garrett 	mutex_unlock(&smi_infos_lock);
251206ee4594SMatthew Garrett 
251306ee4594SMatthew Garrett 	if (type)
2514d8cc5267SMatthew Garrett 		return 0;
25152407d77aSMatthew Garrett 
2516d6dfd131SCorey Minyard 	mutex_lock(&smi_infos_lock);
2517b361e27bSCorey Minyard 	if (unload_when_empty && list_empty(&smi_infos)) {
2518d6dfd131SCorey Minyard 		mutex_unlock(&smi_infos_lock);
2519d2478521SCorey Minyard 		cleanup_ipmi_si();
2520bb2a08c0SCorey Minyard 		pr_warn(PFX "Unable to find any System Interface(s)\n");
25211da177e4SLinus Torvalds 		return -ENODEV;
2522b0defcdbSCorey Minyard 	} else {
2523d6dfd131SCorey Minyard 		mutex_unlock(&smi_infos_lock);
25241da177e4SLinus Torvalds 		return 0;
25251da177e4SLinus Torvalds 	}
2526b0defcdbSCorey Minyard }
25271da177e4SLinus Torvalds module_init(init_ipmi_si);
25281da177e4SLinus Torvalds 
2529b361e27bSCorey Minyard static void cleanup_one_si(struct smi_info *to_clean)
25301da177e4SLinus Torvalds {
25312407d77aSMatthew Garrett 	int           rv = 0;
25321da177e4SLinus Torvalds 
25331da177e4SLinus Torvalds 	if (!to_clean)
25341da177e4SLinus Torvalds 		return;
25351da177e4SLinus Torvalds 
2536b874b985SCorey Minyard 	if (to_clean->intf) {
2537b874b985SCorey Minyard 		ipmi_smi_t intf = to_clean->intf;
2538b874b985SCorey Minyard 
2539b874b985SCorey Minyard 		to_clean->intf = NULL;
2540b874b985SCorey Minyard 		rv = ipmi_unregister_smi(intf);
2541b874b985SCorey Minyard 		if (rv) {
2542b874b985SCorey Minyard 			pr_err(PFX "Unable to unregister device: errno=%d\n",
2543b874b985SCorey Minyard 			       rv);
2544b874b985SCorey Minyard 		}
2545b874b985SCorey Minyard 	}
2546b874b985SCorey Minyard 
2547b0defcdbSCorey Minyard 	list_del(&to_clean->link);
2548b0defcdbSCorey Minyard 
2549c305e3d3SCorey Minyard 	/*
2550b874b985SCorey Minyard 	 * Make sure that interrupts, the timer and the thread are
2551b874b985SCorey Minyard 	 * stopped and will not run again.
2552c305e3d3SCorey Minyard 	 */
25534f3e8199SCorey Minyard 	if (to_clean->io.irq_cleanup)
25544f3e8199SCorey Minyard 		to_clean->io.irq_cleanup(&to_clean->io);
2555a9a2c44fSCorey Minyard 	wait_for_timer_and_thread(to_clean);
25561da177e4SLinus Torvalds 
2557c305e3d3SCorey Minyard 	/*
2558c305e3d3SCorey Minyard 	 * Timeouts are stopped, now make sure the interrupts are off
2559b874b985SCorey Minyard 	 * in the BMC.  Note that timers and CPU interrupts are off,
2560b874b985SCorey Minyard 	 * so no need for locks.
2561c305e3d3SCorey Minyard 	 */
2562ee6cd5f8SCorey Minyard 	while (to_clean->curr_msg || (to_clean->si_state != SI_NORMAL)) {
2563ee6cd5f8SCorey Minyard 		poll(to_clean);
2564ee6cd5f8SCorey Minyard 		schedule_timeout_uninterruptible(1);
2565ee6cd5f8SCorey Minyard 	}
25667e030d6dSCorey Minyard 	if (to_clean->handlers)
25670cfec916SCorey Minyard 		disable_si_irq(to_clean, false);
2568ee6cd5f8SCorey Minyard 	while (to_clean->curr_msg || (to_clean->si_state != SI_NORMAL)) {
2569ee6cd5f8SCorey Minyard 		poll(to_clean);
2570ee6cd5f8SCorey Minyard 		schedule_timeout_uninterruptible(1);
2571ee6cd5f8SCorey Minyard 	}
2572ee6cd5f8SCorey Minyard 
25732407d77aSMatthew Garrett 	if (to_clean->handlers)
25741da177e4SLinus Torvalds 		to_clean->handlers->cleanup(to_clean->si_sm);
25751da177e4SLinus Torvalds 
25761da177e4SLinus Torvalds 	kfree(to_clean->si_sm);
25771da177e4SLinus Torvalds 
2578910840f2SCorey Minyard 	if (to_clean->io.addr_source_cleanup)
2579910840f2SCorey Minyard 		to_clean->io.addr_source_cleanup(&to_clean->io);
2580e1eeb7f8SCorey Minyard 	if (to_clean->io.io_cleanup)
2581e1eeb7f8SCorey Minyard 		to_clean->io.io_cleanup(&to_clean->io);
258250c812b2SCorey Minyard 
2583910840f2SCorey Minyard 	if (to_clean->pdev)
258450c812b2SCorey Minyard 		platform_device_unregister(to_clean->pdev);
258550c812b2SCorey Minyard 
258650c812b2SCorey Minyard 	kfree(to_clean);
25871da177e4SLinus Torvalds }
25881da177e4SLinus Torvalds 
2589bb398a4cSCorey Minyard int ipmi_si_remove_by_dev(struct device *dev)
2590bb398a4cSCorey Minyard {
2591bb398a4cSCorey Minyard 	struct smi_info *e;
2592bb398a4cSCorey Minyard 	int rv = -ENOENT;
2593bb398a4cSCorey Minyard 
2594bb398a4cSCorey Minyard 	mutex_lock(&smi_infos_lock);
2595bb398a4cSCorey Minyard 	list_for_each_entry(e, &smi_infos, link) {
2596bb398a4cSCorey Minyard 		if (e->io.dev == dev) {
2597bb398a4cSCorey Minyard 			cleanup_one_si(e);
2598bb398a4cSCorey Minyard 			rv = 0;
2599bb398a4cSCorey Minyard 			break;
2600bb398a4cSCorey Minyard 		}
2601bb398a4cSCorey Minyard 	}
2602bb398a4cSCorey Minyard 	mutex_unlock(&smi_infos_lock);
2603bb398a4cSCorey Minyard 
2604bb398a4cSCorey Minyard 	return rv;
2605bb398a4cSCorey Minyard }
2606bb398a4cSCorey Minyard 
260744814ec9SCorey Minyard void ipmi_si_remove_by_data(int addr_space, enum si_type si_type,
260844814ec9SCorey Minyard 			    unsigned long addr)
260944814ec9SCorey Minyard {
261044814ec9SCorey Minyard 	/* remove */
261144814ec9SCorey Minyard 	struct smi_info *e, *tmp_e;
261244814ec9SCorey Minyard 
261344814ec9SCorey Minyard 	mutex_lock(&smi_infos_lock);
261444814ec9SCorey Minyard 	list_for_each_entry_safe(e, tmp_e, &smi_infos, link) {
261544814ec9SCorey Minyard 		if (e->io.addr_type != addr_space)
261644814ec9SCorey Minyard 			continue;
261744814ec9SCorey Minyard 		if (e->io.si_type != si_type)
261844814ec9SCorey Minyard 			continue;
261944814ec9SCorey Minyard 		if (e->io.addr_data == addr)
262044814ec9SCorey Minyard 			cleanup_one_si(e);
262144814ec9SCorey Minyard 	}
262244814ec9SCorey Minyard 	mutex_unlock(&smi_infos_lock);
262344814ec9SCorey Minyard }
262444814ec9SCorey Minyard 
26250dcf334cSSergey Senozhatsky static void cleanup_ipmi_si(void)
26261da177e4SLinus Torvalds {
2627b0defcdbSCorey Minyard 	struct smi_info *e, *tmp_e;
26281da177e4SLinus Torvalds 
26291da177e4SLinus Torvalds 	if (!initialized)
26301da177e4SLinus Torvalds 		return;
26311da177e4SLinus Torvalds 
263213d0b35cSCorey Minyard 	ipmi_si_pci_shutdown();
2633*c6f85a75SCorey Minyard 
2634*c6f85a75SCorey Minyard 	ipmi_si_parisc_shutdown();
2635b0defcdbSCorey Minyard 
26369d70029eSCorey Minyard 	ipmi_si_platform_shutdown();
2637dba9b4f6SCorey Minyard 
2638d6dfd131SCorey Minyard 	mutex_lock(&smi_infos_lock);
2639b0defcdbSCorey Minyard 	list_for_each_entry_safe(e, tmp_e, &smi_infos, link)
2640b0defcdbSCorey Minyard 		cleanup_one_si(e);
2641d6dfd131SCorey Minyard 	mutex_unlock(&smi_infos_lock);
26421da177e4SLinus Torvalds }
26431da177e4SLinus Torvalds module_exit(cleanup_ipmi_si);
26441da177e4SLinus Torvalds 
26450944d889SCorey Minyard MODULE_ALIAS("platform:dmi-ipmi-si");
26461da177e4SLinus Torvalds MODULE_LICENSE("GPL");
26471fdd75bdSCorey Minyard MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
2648c305e3d3SCorey Minyard MODULE_DESCRIPTION("Interface to the IPMI driver for the KCS, SMIC, and BT"
2649c305e3d3SCorey Minyard 		   " system interfaces.");
2650