xref: /openbmc/linux/drivers/char/ipmi/ipmi_si_intf.c (revision cc095f0ac1f7c200e51a5c2a78a43c9f42049dbb)
1243ac210SCorey Minyard // SPDX-License-Identifier: GPL-2.0+
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * ipmi_si.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * The interface to the IPMI driver for the system interfaces (KCS, SMIC,
61da177e4SLinus Torvalds  * BT).
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * Author: MontaVista Software, Inc.
91da177e4SLinus Torvalds  *         Corey Minyard <minyard@mvista.com>
101da177e4SLinus Torvalds  *         source@mvista.com
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  * Copyright 2002 MontaVista Software Inc.
13dba9b4f6SCorey Minyard  * Copyright 2006 IBM Corp., Christian Krafft <krafft@de.ibm.com>
141da177e4SLinus Torvalds  */
151da177e4SLinus Torvalds 
161da177e4SLinus Torvalds /*
171da177e4SLinus Torvalds  * This file holds the "policy" for the interface to the SMI state
181da177e4SLinus Torvalds  * machine.  It does the configuration, handles timers and interrupts,
191da177e4SLinus Torvalds  * and drives the real SMI state machine.
201da177e4SLinus Torvalds  */
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds #include <linux/module.h>
231da177e4SLinus Torvalds #include <linux/moduleparam.h>
241da177e4SLinus Torvalds #include <linux/sched.h>
2507412736SAlexey Dobriyan #include <linux/seq_file.h>
261da177e4SLinus Torvalds #include <linux/timer.h>
271da177e4SLinus Torvalds #include <linux/errno.h>
281da177e4SLinus Torvalds #include <linux/spinlock.h>
291da177e4SLinus Torvalds #include <linux/slab.h>
301da177e4SLinus Torvalds #include <linux/delay.h>
311da177e4SLinus Torvalds #include <linux/list.h>
32ea94027bSCorey Minyard #include <linux/notifier.h>
33b0defcdbSCorey Minyard #include <linux/mutex.h>
34e9a705a0SMatt Domsch #include <linux/kthread.h>
351da177e4SLinus Torvalds #include <asm/irq.h>
361da177e4SLinus Torvalds #include <linux/interrupt.h>
371da177e4SLinus Torvalds #include <linux/rcupdate.h>
3816f4232cSZhao Yakui #include <linux/ipmi.h>
391da177e4SLinus Torvalds #include <linux/ipmi_smi.h>
401e89a499SCorey Minyard #include "ipmi_si.h"
41b361e27bSCorey Minyard #include <linux/string.h>
42b361e27bSCorey Minyard #include <linux/ctype.h>
43dba9b4f6SCorey Minyard 
44b361e27bSCorey Minyard #define PFX "ipmi_si: "
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds /* Measure times between events in the driver. */
471da177e4SLinus Torvalds #undef DEBUG_TIMING
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds /* Call every 10 ms. */
501da177e4SLinus Torvalds #define SI_TIMEOUT_TIME_USEC	10000
511da177e4SLinus Torvalds #define SI_USEC_PER_JIFFY	(1000000/HZ)
521da177e4SLinus Torvalds #define SI_TIMEOUT_JIFFIES	(SI_TIMEOUT_TIME_USEC/SI_USEC_PER_JIFFY)
531da177e4SLinus Torvalds #define SI_SHORT_TIMEOUT_USEC  250 /* .25ms when the SM request a
541da177e4SLinus Torvalds 				      short timeout */
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds enum si_intf_state {
571da177e4SLinus Torvalds 	SI_NORMAL,
581da177e4SLinus Torvalds 	SI_GETTING_FLAGS,
591da177e4SLinus Torvalds 	SI_GETTING_EVENTS,
601da177e4SLinus Torvalds 	SI_CLEARING_FLAGS,
611da177e4SLinus Torvalds 	SI_GETTING_MESSAGES,
62d9b7e4f7SCorey Minyard 	SI_CHECKING_ENABLES,
63d9b7e4f7SCorey Minyard 	SI_SETTING_ENABLES
641da177e4SLinus Torvalds 	/* FIXME - add watchdog stuff. */
651da177e4SLinus Torvalds };
661da177e4SLinus Torvalds 
679dbf68f9SCorey Minyard /* Some BT-specific defines we need here. */
689dbf68f9SCorey Minyard #define IPMI_BT_INTMASK_REG		2
699dbf68f9SCorey Minyard #define IPMI_BT_INTMASK_CLEAR_IRQ_BIT	2
709dbf68f9SCorey Minyard #define IPMI_BT_INTMASK_ENABLE_IRQ_BIT	1
719dbf68f9SCorey Minyard 
7295e300c0SCorey Minyard static const char * const si_to_str[] = { "invalid", "kcs", "smic", "bt" };
731da177e4SLinus Torvalds 
74bb398a4cSCorey Minyard static int initialized;
75bb398a4cSCorey Minyard 
7664959e2dSCorey Minyard /*
7764959e2dSCorey Minyard  * Indexes into stats[] in smi_info below.
7864959e2dSCorey Minyard  */
79ba8ff1c6SCorey Minyard enum si_stat_indexes {
80ba8ff1c6SCorey Minyard 	/*
81ba8ff1c6SCorey Minyard 	 * Number of times the driver requested a timer while an operation
82ba8ff1c6SCorey Minyard 	 * was in progress.
83ba8ff1c6SCorey Minyard 	 */
84ba8ff1c6SCorey Minyard 	SI_STAT_short_timeouts = 0,
8564959e2dSCorey Minyard 
86ba8ff1c6SCorey Minyard 	/*
87ba8ff1c6SCorey Minyard 	 * Number of times the driver requested a timer while nothing was in
88ba8ff1c6SCorey Minyard 	 * progress.
89ba8ff1c6SCorey Minyard 	 */
90ba8ff1c6SCorey Minyard 	SI_STAT_long_timeouts,
9164959e2dSCorey Minyard 
92ba8ff1c6SCorey Minyard 	/* Number of times the interface was idle while being polled. */
93ba8ff1c6SCorey Minyard 	SI_STAT_idles,
94ba8ff1c6SCorey Minyard 
95ba8ff1c6SCorey Minyard 	/* Number of interrupts the driver handled. */
96ba8ff1c6SCorey Minyard 	SI_STAT_interrupts,
97ba8ff1c6SCorey Minyard 
98ba8ff1c6SCorey Minyard 	/* Number of time the driver got an ATTN from the hardware. */
99ba8ff1c6SCorey Minyard 	SI_STAT_attentions,
100ba8ff1c6SCorey Minyard 
101ba8ff1c6SCorey Minyard 	/* Number of times the driver requested flags from the hardware. */
102ba8ff1c6SCorey Minyard 	SI_STAT_flag_fetches,
103ba8ff1c6SCorey Minyard 
104ba8ff1c6SCorey Minyard 	/* Number of times the hardware didn't follow the state machine. */
105ba8ff1c6SCorey Minyard 	SI_STAT_hosed_count,
106ba8ff1c6SCorey Minyard 
107ba8ff1c6SCorey Minyard 	/* Number of completed messages. */
108ba8ff1c6SCorey Minyard 	SI_STAT_complete_transactions,
109ba8ff1c6SCorey Minyard 
110ba8ff1c6SCorey Minyard 	/* Number of IPMI events received from the hardware. */
111ba8ff1c6SCorey Minyard 	SI_STAT_events,
112ba8ff1c6SCorey Minyard 
113ba8ff1c6SCorey Minyard 	/* Number of watchdog pretimeouts. */
114ba8ff1c6SCorey Minyard 	SI_STAT_watchdog_pretimeouts,
115ba8ff1c6SCorey Minyard 
116b3834be5SAdam Buchbinder 	/* Number of asynchronous messages received. */
117ba8ff1c6SCorey Minyard 	SI_STAT_incoming_messages,
118ba8ff1c6SCorey Minyard 
119ba8ff1c6SCorey Minyard 
120ba8ff1c6SCorey Minyard 	/* This *must* remain last, add new values above this. */
121ba8ff1c6SCorey Minyard 	SI_NUM_STATS
122ba8ff1c6SCorey Minyard };
12364959e2dSCorey Minyard 
124c305e3d3SCorey Minyard struct smi_info {
125a9a2c44fSCorey Minyard 	int                    intf_num;
1261da177e4SLinus Torvalds 	ipmi_smi_t             intf;
1271da177e4SLinus Torvalds 	struct si_sm_data      *si_sm;
12881d02b7fSCorey Minyard 	const struct si_sm_handlers *handlers;
1291da177e4SLinus Torvalds 	spinlock_t             si_lock;
130b874b985SCorey Minyard 	struct ipmi_smi_msg    *waiting_msg;
1311da177e4SLinus Torvalds 	struct ipmi_smi_msg    *curr_msg;
1321da177e4SLinus Torvalds 	enum si_intf_state     si_state;
1331da177e4SLinus Torvalds 
134c305e3d3SCorey Minyard 	/*
135c305e3d3SCorey Minyard 	 * Used to handle the various types of I/O that can occur with
136c305e3d3SCorey Minyard 	 * IPMI
137c305e3d3SCorey Minyard 	 */
1381da177e4SLinus Torvalds 	struct si_sm_io io;
1391da177e4SLinus Torvalds 
140c305e3d3SCorey Minyard 	/*
141c305e3d3SCorey Minyard 	 * Per-OEM handler, called from handle_flags().  Returns 1
142c305e3d3SCorey Minyard 	 * when handle_flags() needs to be re-run or 0 indicating it
143c305e3d3SCorey Minyard 	 * set si_state itself.
1443ae0e0f9SCorey Minyard 	 */
1453ae0e0f9SCorey Minyard 	int (*oem_data_avail_handler)(struct smi_info *smi_info);
1463ae0e0f9SCorey Minyard 
147c305e3d3SCorey Minyard 	/*
148c305e3d3SCorey Minyard 	 * Flags from the last GET_MSG_FLAGS command, used when an ATTN
149c305e3d3SCorey Minyard 	 * is set to hold the flags until we are done handling everything
150c305e3d3SCorey Minyard 	 * from the flags.
151c305e3d3SCorey Minyard 	 */
1521da177e4SLinus Torvalds #define RECEIVE_MSG_AVAIL	0x01
1531da177e4SLinus Torvalds #define EVENT_MSG_BUFFER_FULL	0x02
1541da177e4SLinus Torvalds #define WDT_PRE_TIMEOUT_INT	0x08
1553ae0e0f9SCorey Minyard #define OEM0_DATA_AVAIL     0x20
1563ae0e0f9SCorey Minyard #define OEM1_DATA_AVAIL     0x40
1573ae0e0f9SCorey Minyard #define OEM2_DATA_AVAIL     0x80
1583ae0e0f9SCorey Minyard #define OEM_DATA_AVAIL      (OEM0_DATA_AVAIL | \
1593ae0e0f9SCorey Minyard 			     OEM1_DATA_AVAIL | \
1603ae0e0f9SCorey Minyard 			     OEM2_DATA_AVAIL)
1611da177e4SLinus Torvalds 	unsigned char       msg_flags;
1621da177e4SLinus Torvalds 
16340112ae7SCorey Minyard 	/* Does the BMC have an event buffer? */
1647aefac26SCorey Minyard 	bool		    has_event_buffer;
16540112ae7SCorey Minyard 
166c305e3d3SCorey Minyard 	/*
167c305e3d3SCorey Minyard 	 * If set to true, this will request events the next time the
168c305e3d3SCorey Minyard 	 * state machine is idle.
169c305e3d3SCorey Minyard 	 */
1701da177e4SLinus Torvalds 	atomic_t            req_events;
1711da177e4SLinus Torvalds 
172c305e3d3SCorey Minyard 	/*
173c305e3d3SCorey Minyard 	 * If true, run the state machine to completion on every send
174c305e3d3SCorey Minyard 	 * call.  Generally used after a panic to make sure stuff goes
175c305e3d3SCorey Minyard 	 * out.
176c305e3d3SCorey Minyard 	 */
1777aefac26SCorey Minyard 	bool                run_to_completion;
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds 	/* The timer for this si. */
1801da177e4SLinus Torvalds 	struct timer_list   si_timer;
1811da177e4SLinus Torvalds 
1824f7f5551SMasamitsu Yamazaki 	/* This flag is set, if the timer can be set */
1834f7f5551SMasamitsu Yamazaki 	bool		    timer_can_start;
1844f7f5551SMasamitsu Yamazaki 
18548e8ac29SBodo Stroesser 	/* This flag is set, if the timer is running (timer_pending() isn't enough) */
18648e8ac29SBodo Stroesser 	bool		    timer_running;
18748e8ac29SBodo Stroesser 
1881da177e4SLinus Torvalds 	/* The time (in jiffies) the last timeout occurred at. */
1891da177e4SLinus Torvalds 	unsigned long       last_timeout_jiffies;
1901da177e4SLinus Torvalds 
19189986496SCorey Minyard 	/* Are we waiting for the events, pretimeouts, received msgs? */
19289986496SCorey Minyard 	atomic_t            need_watch;
19389986496SCorey Minyard 
194c305e3d3SCorey Minyard 	/*
195c305e3d3SCorey Minyard 	 * The driver will disable interrupts when it gets into a
196c305e3d3SCorey Minyard 	 * situation where it cannot handle messages due to lack of
197c305e3d3SCorey Minyard 	 * memory.  Once that situation clears up, it will re-enable
198c305e3d3SCorey Minyard 	 * interrupts.
199c305e3d3SCorey Minyard 	 */
2007aefac26SCorey Minyard 	bool interrupt_disabled;
2011da177e4SLinus Torvalds 
202d9b7e4f7SCorey Minyard 	/*
203d9b7e4f7SCorey Minyard 	 * Does the BMC support events?
204d9b7e4f7SCorey Minyard 	 */
205d9b7e4f7SCorey Minyard 	bool supports_event_msg_buff;
206d9b7e4f7SCorey Minyard 
207a8df150cSCorey Minyard 	/*
208d0882897SCorey Minyard 	 * Can we disable interrupts the global enables receive irq
209d0882897SCorey Minyard 	 * bit?  There are currently two forms of brokenness, some
210d0882897SCorey Minyard 	 * systems cannot disable the bit (which is technically within
211d0882897SCorey Minyard 	 * the spec but a bad idea) and some systems have the bit
212d0882897SCorey Minyard 	 * forced to zero even though interrupts work (which is
213d0882897SCorey Minyard 	 * clearly outside the spec).  The next bool tells which form
214d0882897SCorey Minyard 	 * of brokenness is present.
2151e7d6a45SCorey Minyard 	 */
216d0882897SCorey Minyard 	bool cannot_disable_irq;
217d0882897SCorey Minyard 
218d0882897SCorey Minyard 	/*
219d0882897SCorey Minyard 	 * Some systems are broken and cannot set the irq enable
220d0882897SCorey Minyard 	 * bit, even if they support interrupts.
221d0882897SCorey Minyard 	 */
222d0882897SCorey Minyard 	bool irq_enable_broken;
2231e7d6a45SCorey Minyard 
2241e7d6a45SCorey Minyard 	/*
225a8df150cSCorey Minyard 	 * Did we get an attention that we did not handle?
226a8df150cSCorey Minyard 	 */
227a8df150cSCorey Minyard 	bool got_attn;
228a8df150cSCorey Minyard 
22950c812b2SCorey Minyard 	/* From the get device id response... */
2303ae0e0f9SCorey Minyard 	struct ipmi_device_id device_id;
2311da177e4SLinus Torvalds 
232910840f2SCorey Minyard 	/* Default driver model device. */
23350c812b2SCorey Minyard 	struct platform_device *pdev;
23450c812b2SCorey Minyard 
235*cc095f0aSCorey Minyard 	/* Have we added the device group to the device? */
236*cc095f0aSCorey Minyard 	bool dev_group_added;
237*cc095f0aSCorey Minyard 
2381da177e4SLinus Torvalds 	/* Counters and things for the proc filesystem. */
23964959e2dSCorey Minyard 	atomic_t stats[SI_NUM_STATS];
240a9a2c44fSCorey Minyard 
241e9a705a0SMatt Domsch 	struct task_struct *thread;
242b0defcdbSCorey Minyard 
243b0defcdbSCorey Minyard 	struct list_head link;
2441da177e4SLinus Torvalds };
2451da177e4SLinus Torvalds 
24664959e2dSCorey Minyard #define smi_inc_stat(smi, stat) \
24764959e2dSCorey Minyard 	atomic_inc(&(smi)->stats[SI_STAT_ ## stat])
24864959e2dSCorey Minyard #define smi_get_stat(smi, stat) \
24964959e2dSCorey Minyard 	((unsigned int) atomic_read(&(smi)->stats[SI_STAT_ ## stat]))
25064959e2dSCorey Minyard 
2517a453308SCorey Minyard #define IPMI_MAX_INTFS 4
2527a453308SCorey Minyard static int force_kipmid[IPMI_MAX_INTFS];
253a51f4a81SCorey Minyard static int num_force_kipmid;
254a51f4a81SCorey Minyard 
2557a453308SCorey Minyard static unsigned int kipmid_max_busy_us[IPMI_MAX_INTFS];
256ae74e823SMartin Wilck static int num_max_busy_us;
257ae74e823SMartin Wilck 
2587aefac26SCorey Minyard static bool unload_when_empty = true;
259b361e27bSCorey Minyard 
260b0defcdbSCorey Minyard static int try_smi_init(struct smi_info *smi);
261b361e27bSCorey Minyard static void cleanup_one_si(struct smi_info *to_clean);
262d2478521SCorey Minyard static void cleanup_ipmi_si(void);
263b0defcdbSCorey Minyard 
264f93aae9fSJohn Stultz #ifdef DEBUG_TIMING
265f93aae9fSJohn Stultz void debug_timestamp(char *msg)
266f93aae9fSJohn Stultz {
26748862ea2SJohn Stultz 	struct timespec64 t;
268f93aae9fSJohn Stultz 
26948862ea2SJohn Stultz 	getnstimeofday64(&t);
27048862ea2SJohn Stultz 	pr_debug("**%s: %lld.%9.9ld\n", msg, (long long) t.tv_sec, t.tv_nsec);
271f93aae9fSJohn Stultz }
272f93aae9fSJohn Stultz #else
273f93aae9fSJohn Stultz #define debug_timestamp(x)
274f93aae9fSJohn Stultz #endif
275f93aae9fSJohn Stultz 
276e041c683SAlan Stern static ATOMIC_NOTIFIER_HEAD(xaction_notifier_list);
277ea94027bSCorey Minyard static int register_xaction_notifier(struct notifier_block *nb)
278ea94027bSCorey Minyard {
279e041c683SAlan Stern 	return atomic_notifier_chain_register(&xaction_notifier_list, nb);
280ea94027bSCorey Minyard }
281ea94027bSCorey Minyard 
2821da177e4SLinus Torvalds static void deliver_recv_msg(struct smi_info *smi_info,
2831da177e4SLinus Torvalds 			     struct ipmi_smi_msg *msg)
2841da177e4SLinus Torvalds {
2857adf579cSCorey Minyard 	/* Deliver the message to the upper layer. */
286968bf7ccSCorey Minyard 	if (smi_info->intf)
287a747c5abSJiri Kosina 		ipmi_smi_msg_received(smi_info->intf, msg);
288968bf7ccSCorey Minyard 	else
289968bf7ccSCorey Minyard 		ipmi_free_smi_msg(msg);
290a747c5abSJiri Kosina }
2911da177e4SLinus Torvalds 
2924d7cbac7SCorey Minyard static void return_hosed_msg(struct smi_info *smi_info, int cCode)
2931da177e4SLinus Torvalds {
2941da177e4SLinus Torvalds 	struct ipmi_smi_msg *msg = smi_info->curr_msg;
2951da177e4SLinus Torvalds 
2964d7cbac7SCorey Minyard 	if (cCode < 0 || cCode > IPMI_ERR_UNSPECIFIED)
2974d7cbac7SCorey Minyard 		cCode = IPMI_ERR_UNSPECIFIED;
2984d7cbac7SCorey Minyard 	/* else use it as is */
2994d7cbac7SCorey Minyard 
30025985edcSLucas De Marchi 	/* Make it a response */
3011da177e4SLinus Torvalds 	msg->rsp[0] = msg->data[0] | 4;
3021da177e4SLinus Torvalds 	msg->rsp[1] = msg->data[1];
3034d7cbac7SCorey Minyard 	msg->rsp[2] = cCode;
3041da177e4SLinus Torvalds 	msg->rsp_size = 3;
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds 	smi_info->curr_msg = NULL;
3071da177e4SLinus Torvalds 	deliver_recv_msg(smi_info, msg);
3081da177e4SLinus Torvalds }
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds static enum si_sm_result start_next_msg(struct smi_info *smi_info)
3111da177e4SLinus Torvalds {
3121da177e4SLinus Torvalds 	int              rv;
3131da177e4SLinus Torvalds 
314b874b985SCorey Minyard 	if (!smi_info->waiting_msg) {
3151da177e4SLinus Torvalds 		smi_info->curr_msg = NULL;
3161da177e4SLinus Torvalds 		rv = SI_SM_IDLE;
3171da177e4SLinus Torvalds 	} else {
3181da177e4SLinus Torvalds 		int err;
3191da177e4SLinus Torvalds 
320b874b985SCorey Minyard 		smi_info->curr_msg = smi_info->waiting_msg;
321b874b985SCorey Minyard 		smi_info->waiting_msg = NULL;
322f93aae9fSJohn Stultz 		debug_timestamp("Start2");
323e041c683SAlan Stern 		err = atomic_notifier_call_chain(&xaction_notifier_list,
324e041c683SAlan Stern 				0, smi_info);
325ea94027bSCorey Minyard 		if (err & NOTIFY_STOP_MASK) {
326ea94027bSCorey Minyard 			rv = SI_SM_CALL_WITHOUT_DELAY;
327ea94027bSCorey Minyard 			goto out;
328ea94027bSCorey Minyard 		}
3291da177e4SLinus Torvalds 		err = smi_info->handlers->start_transaction(
3301da177e4SLinus Torvalds 			smi_info->si_sm,
3311da177e4SLinus Torvalds 			smi_info->curr_msg->data,
3321da177e4SLinus Torvalds 			smi_info->curr_msg->data_size);
333c305e3d3SCorey Minyard 		if (err)
3344d7cbac7SCorey Minyard 			return_hosed_msg(smi_info, err);
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 		rv = SI_SM_CALL_WITHOUT_DELAY;
3371da177e4SLinus Torvalds 	}
338ea94027bSCorey Minyard out:
3391da177e4SLinus Torvalds 	return rv;
3401da177e4SLinus Torvalds }
3411da177e4SLinus Torvalds 
3420cfec916SCorey Minyard static void smi_mod_timer(struct smi_info *smi_info, unsigned long new_val)
3430cfec916SCorey Minyard {
3444f7f5551SMasamitsu Yamazaki 	if (!smi_info->timer_can_start)
3454f7f5551SMasamitsu Yamazaki 		return;
3460cfec916SCorey Minyard 	smi_info->last_timeout_jiffies = jiffies;
3470cfec916SCorey Minyard 	mod_timer(&smi_info->si_timer, new_val);
3480cfec916SCorey Minyard 	smi_info->timer_running = true;
3490cfec916SCorey Minyard }
3500cfec916SCorey Minyard 
3510cfec916SCorey Minyard /*
3520cfec916SCorey Minyard  * Start a new message and (re)start the timer and thread.
3530cfec916SCorey Minyard  */
3540cfec916SCorey Minyard static void start_new_msg(struct smi_info *smi_info, unsigned char *msg,
3550cfec916SCorey Minyard 			  unsigned int size)
3560cfec916SCorey Minyard {
3570cfec916SCorey Minyard 	smi_mod_timer(smi_info, jiffies + SI_TIMEOUT_JIFFIES);
3580cfec916SCorey Minyard 
3590cfec916SCorey Minyard 	if (smi_info->thread)
3600cfec916SCorey Minyard 		wake_up_process(smi_info->thread);
3610cfec916SCorey Minyard 
3620cfec916SCorey Minyard 	smi_info->handlers->start_transaction(smi_info->si_sm, msg, size);
3630cfec916SCorey Minyard }
3640cfec916SCorey Minyard 
3654f7f5551SMasamitsu Yamazaki static void start_check_enables(struct smi_info *smi_info)
366ee6cd5f8SCorey Minyard {
367ee6cd5f8SCorey Minyard 	unsigned char msg[2];
368ee6cd5f8SCorey Minyard 
369ee6cd5f8SCorey Minyard 	msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
370ee6cd5f8SCorey Minyard 	msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
371ee6cd5f8SCorey Minyard 
3720cfec916SCorey Minyard 	start_new_msg(smi_info, msg, 2);
373d9b7e4f7SCorey Minyard 	smi_info->si_state = SI_CHECKING_ENABLES;
374ee6cd5f8SCorey Minyard }
375ee6cd5f8SCorey Minyard 
3764f7f5551SMasamitsu Yamazaki static void start_clear_flags(struct smi_info *smi_info)
3771da177e4SLinus Torvalds {
3781da177e4SLinus Torvalds 	unsigned char msg[3];
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds 	/* Make sure the watchdog pre-timeout flag is not set at startup. */
3811da177e4SLinus Torvalds 	msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
3821da177e4SLinus Torvalds 	msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
3831da177e4SLinus Torvalds 	msg[2] = WDT_PRE_TIMEOUT_INT;
3841da177e4SLinus Torvalds 
3850cfec916SCorey Minyard 	start_new_msg(smi_info, msg, 3);
3861da177e4SLinus Torvalds 	smi_info->si_state = SI_CLEARING_FLAGS;
3871da177e4SLinus Torvalds }
3881da177e4SLinus Torvalds 
389968bf7ccSCorey Minyard static void start_getting_msg_queue(struct smi_info *smi_info)
390968bf7ccSCorey Minyard {
391968bf7ccSCorey Minyard 	smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
392968bf7ccSCorey Minyard 	smi_info->curr_msg->data[1] = IPMI_GET_MSG_CMD;
393968bf7ccSCorey Minyard 	smi_info->curr_msg->data_size = 2;
394968bf7ccSCorey Minyard 
3950cfec916SCorey Minyard 	start_new_msg(smi_info, smi_info->curr_msg->data,
396968bf7ccSCorey Minyard 		      smi_info->curr_msg->data_size);
397968bf7ccSCorey Minyard 	smi_info->si_state = SI_GETTING_MESSAGES;
398968bf7ccSCorey Minyard }
399968bf7ccSCorey Minyard 
400968bf7ccSCorey Minyard static void start_getting_events(struct smi_info *smi_info)
401968bf7ccSCorey Minyard {
402968bf7ccSCorey Minyard 	smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
403968bf7ccSCorey Minyard 	smi_info->curr_msg->data[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD;
404968bf7ccSCorey Minyard 	smi_info->curr_msg->data_size = 2;
405968bf7ccSCorey Minyard 
4060cfec916SCorey Minyard 	start_new_msg(smi_info, smi_info->curr_msg->data,
407968bf7ccSCorey Minyard 		      smi_info->curr_msg->data_size);
408968bf7ccSCorey Minyard 	smi_info->si_state = SI_GETTING_EVENTS;
409968bf7ccSCorey Minyard }
410968bf7ccSCorey Minyard 
411c305e3d3SCorey Minyard /*
412c305e3d3SCorey Minyard  * When we have a situtaion where we run out of memory and cannot
413c305e3d3SCorey Minyard  * allocate messages, we just leave them in the BMC and run the system
414c305e3d3SCorey Minyard  * polled until we can allocate some memory.  Once we have some
415c305e3d3SCorey Minyard  * memory, we will re-enable the interrupt.
4161e7d6a45SCorey Minyard  *
4171e7d6a45SCorey Minyard  * Note that we cannot just use disable_irq(), since the interrupt may
4181e7d6a45SCorey Minyard  * be shared.
419c305e3d3SCorey Minyard  */
4204f7f5551SMasamitsu Yamazaki static inline bool disable_si_irq(struct smi_info *smi_info)
4211da177e4SLinus Torvalds {
422910840f2SCorey Minyard 	if ((smi_info->io.irq) && (!smi_info->interrupt_disabled)) {
4237aefac26SCorey Minyard 		smi_info->interrupt_disabled = true;
4244f7f5551SMasamitsu Yamazaki 		start_check_enables(smi_info);
425968bf7ccSCorey Minyard 		return true;
4261da177e4SLinus Torvalds 	}
427968bf7ccSCorey Minyard 	return false;
4281da177e4SLinus Torvalds }
4291da177e4SLinus Torvalds 
430968bf7ccSCorey Minyard static inline bool enable_si_irq(struct smi_info *smi_info)
4311da177e4SLinus Torvalds {
432910840f2SCorey Minyard 	if ((smi_info->io.irq) && (smi_info->interrupt_disabled)) {
4337aefac26SCorey Minyard 		smi_info->interrupt_disabled = false;
4344f7f5551SMasamitsu Yamazaki 		start_check_enables(smi_info);
435968bf7ccSCorey Minyard 		return true;
4361da177e4SLinus Torvalds 	}
437968bf7ccSCorey Minyard 	return false;
438968bf7ccSCorey Minyard }
439968bf7ccSCorey Minyard 
440968bf7ccSCorey Minyard /*
441968bf7ccSCorey Minyard  * Allocate a message.  If unable to allocate, start the interrupt
442968bf7ccSCorey Minyard  * disable process and return NULL.  If able to allocate but
443968bf7ccSCorey Minyard  * interrupts are disabled, free the message and return NULL after
444968bf7ccSCorey Minyard  * starting the interrupt enable process.
445968bf7ccSCorey Minyard  */
446968bf7ccSCorey Minyard static struct ipmi_smi_msg *alloc_msg_handle_irq(struct smi_info *smi_info)
447968bf7ccSCorey Minyard {
448968bf7ccSCorey Minyard 	struct ipmi_smi_msg *msg;
449968bf7ccSCorey Minyard 
450968bf7ccSCorey Minyard 	msg = ipmi_alloc_smi_msg();
451968bf7ccSCorey Minyard 	if (!msg) {
4524f7f5551SMasamitsu Yamazaki 		if (!disable_si_irq(smi_info))
453968bf7ccSCorey Minyard 			smi_info->si_state = SI_NORMAL;
454968bf7ccSCorey Minyard 	} else if (enable_si_irq(smi_info)) {
455968bf7ccSCorey Minyard 		ipmi_free_smi_msg(msg);
456968bf7ccSCorey Minyard 		msg = NULL;
457968bf7ccSCorey Minyard 	}
458968bf7ccSCorey Minyard 	return msg;
4591da177e4SLinus Torvalds }
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds static void handle_flags(struct smi_info *smi_info)
4621da177e4SLinus Torvalds {
4633ae0e0f9SCorey Minyard retry:
4641da177e4SLinus Torvalds 	if (smi_info->msg_flags & WDT_PRE_TIMEOUT_INT) {
4651da177e4SLinus Torvalds 		/* Watchdog pre-timeout */
46664959e2dSCorey Minyard 		smi_inc_stat(smi_info, watchdog_pretimeouts);
4671da177e4SLinus Torvalds 
4684f7f5551SMasamitsu Yamazaki 		start_clear_flags(smi_info);
4691da177e4SLinus Torvalds 		smi_info->msg_flags &= ~WDT_PRE_TIMEOUT_INT;
470968bf7ccSCorey Minyard 		if (smi_info->intf)
4711da177e4SLinus Torvalds 			ipmi_smi_watchdog_pretimeout(smi_info->intf);
4721da177e4SLinus Torvalds 	} else if (smi_info->msg_flags & RECEIVE_MSG_AVAIL) {
4731da177e4SLinus Torvalds 		/* Messages available. */
474968bf7ccSCorey Minyard 		smi_info->curr_msg = alloc_msg_handle_irq(smi_info);
475968bf7ccSCorey Minyard 		if (!smi_info->curr_msg)
4761da177e4SLinus Torvalds 			return;
4771da177e4SLinus Torvalds 
478968bf7ccSCorey Minyard 		start_getting_msg_queue(smi_info);
4791da177e4SLinus Torvalds 	} else if (smi_info->msg_flags & EVENT_MSG_BUFFER_FULL) {
4801da177e4SLinus Torvalds 		/* Events available. */
481968bf7ccSCorey Minyard 		smi_info->curr_msg = alloc_msg_handle_irq(smi_info);
482968bf7ccSCorey Minyard 		if (!smi_info->curr_msg)
4831da177e4SLinus Torvalds 			return;
4841da177e4SLinus Torvalds 
485968bf7ccSCorey Minyard 		start_getting_events(smi_info);
4864064d5efSCorey Minyard 	} else if (smi_info->msg_flags & OEM_DATA_AVAIL &&
4874064d5efSCorey Minyard 		   smi_info->oem_data_avail_handler) {
4883ae0e0f9SCorey Minyard 		if (smi_info->oem_data_avail_handler(smi_info))
4893ae0e0f9SCorey Minyard 			goto retry;
490c305e3d3SCorey Minyard 	} else
4911da177e4SLinus Torvalds 		smi_info->si_state = SI_NORMAL;
4921da177e4SLinus Torvalds }
4931da177e4SLinus Torvalds 
494d9b7e4f7SCorey Minyard /*
495d9b7e4f7SCorey Minyard  * Global enables we care about.
496d9b7e4f7SCorey Minyard  */
497d9b7e4f7SCorey Minyard #define GLOBAL_ENABLES_MASK (IPMI_BMC_EVT_MSG_BUFF | IPMI_BMC_RCV_MSG_INTR | \
498d9b7e4f7SCorey Minyard 			     IPMI_BMC_EVT_MSG_INTR)
499d9b7e4f7SCorey Minyard 
50095c97b59SCorey Minyard static u8 current_global_enables(struct smi_info *smi_info, u8 base,
50195c97b59SCorey Minyard 				 bool *irq_on)
502d9b7e4f7SCorey Minyard {
503d9b7e4f7SCorey Minyard 	u8 enables = 0;
504d9b7e4f7SCorey Minyard 
505d9b7e4f7SCorey Minyard 	if (smi_info->supports_event_msg_buff)
506d9b7e4f7SCorey Minyard 		enables |= IPMI_BMC_EVT_MSG_BUFF;
507d9b7e4f7SCorey Minyard 
508910840f2SCorey Minyard 	if (((smi_info->io.irq && !smi_info->interrupt_disabled) ||
509d0882897SCorey Minyard 	     smi_info->cannot_disable_irq) &&
510d0882897SCorey Minyard 	    !smi_info->irq_enable_broken)
511d9b7e4f7SCorey Minyard 		enables |= IPMI_BMC_RCV_MSG_INTR;
512d9b7e4f7SCorey Minyard 
513d9b7e4f7SCorey Minyard 	if (smi_info->supports_event_msg_buff &&
514910840f2SCorey Minyard 	    smi_info->io.irq && !smi_info->interrupt_disabled &&
515d0882897SCorey Minyard 	    !smi_info->irq_enable_broken)
516d9b7e4f7SCorey Minyard 		enables |= IPMI_BMC_EVT_MSG_INTR;
517d9b7e4f7SCorey Minyard 
51895c97b59SCorey Minyard 	*irq_on = enables & (IPMI_BMC_EVT_MSG_INTR | IPMI_BMC_RCV_MSG_INTR);
51995c97b59SCorey Minyard 
520d9b7e4f7SCorey Minyard 	return enables;
521d9b7e4f7SCorey Minyard }
522d9b7e4f7SCorey Minyard 
52395c97b59SCorey Minyard static void check_bt_irq(struct smi_info *smi_info, bool irq_on)
52495c97b59SCorey Minyard {
52595c97b59SCorey Minyard 	u8 irqstate = smi_info->io.inputb(&smi_info->io, IPMI_BT_INTMASK_REG);
52695c97b59SCorey Minyard 
52795c97b59SCorey Minyard 	irqstate &= IPMI_BT_INTMASK_ENABLE_IRQ_BIT;
52895c97b59SCorey Minyard 
52995c97b59SCorey Minyard 	if ((bool)irqstate == irq_on)
53095c97b59SCorey Minyard 		return;
53195c97b59SCorey Minyard 
53295c97b59SCorey Minyard 	if (irq_on)
53395c97b59SCorey Minyard 		smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG,
53495c97b59SCorey Minyard 				     IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
53595c97b59SCorey Minyard 	else
53695c97b59SCorey Minyard 		smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG, 0);
53795c97b59SCorey Minyard }
53895c97b59SCorey Minyard 
5391da177e4SLinus Torvalds static void handle_transaction_done(struct smi_info *smi_info)
5401da177e4SLinus Torvalds {
5411da177e4SLinus Torvalds 	struct ipmi_smi_msg *msg;
5421da177e4SLinus Torvalds 
543f93aae9fSJohn Stultz 	debug_timestamp("Done");
5441da177e4SLinus Torvalds 	switch (smi_info->si_state) {
5451da177e4SLinus Torvalds 	case SI_NORMAL:
5461da177e4SLinus Torvalds 		if (!smi_info->curr_msg)
5471da177e4SLinus Torvalds 			break;
5481da177e4SLinus Torvalds 
5491da177e4SLinus Torvalds 		smi_info->curr_msg->rsp_size
5501da177e4SLinus Torvalds 			= smi_info->handlers->get_result(
5511da177e4SLinus Torvalds 				smi_info->si_sm,
5521da177e4SLinus Torvalds 				smi_info->curr_msg->rsp,
5531da177e4SLinus Torvalds 				IPMI_MAX_MSG_LENGTH);
5541da177e4SLinus Torvalds 
555c305e3d3SCorey Minyard 		/*
556c305e3d3SCorey Minyard 		 * Do this here becase deliver_recv_msg() releases the
557c305e3d3SCorey Minyard 		 * lock, and a new message can be put in during the
558c305e3d3SCorey Minyard 		 * time the lock is released.
559c305e3d3SCorey Minyard 		 */
5601da177e4SLinus Torvalds 		msg = smi_info->curr_msg;
5611da177e4SLinus Torvalds 		smi_info->curr_msg = NULL;
5621da177e4SLinus Torvalds 		deliver_recv_msg(smi_info, msg);
5631da177e4SLinus Torvalds 		break;
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds 	case SI_GETTING_FLAGS:
5661da177e4SLinus Torvalds 	{
5671da177e4SLinus Torvalds 		unsigned char msg[4];
5681da177e4SLinus Torvalds 		unsigned int  len;
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds 		/* We got the flags from the SMI, now handle them. */
5711da177e4SLinus Torvalds 		len = smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
5721da177e4SLinus Torvalds 		if (msg[2] != 0) {
573c305e3d3SCorey Minyard 			/* Error fetching flags, just give up for now. */
5741da177e4SLinus Torvalds 			smi_info->si_state = SI_NORMAL;
5751da177e4SLinus Torvalds 		} else if (len < 4) {
576c305e3d3SCorey Minyard 			/*
577c305e3d3SCorey Minyard 			 * Hmm, no flags.  That's technically illegal, but
578c305e3d3SCorey Minyard 			 * don't use uninitialized data.
579c305e3d3SCorey Minyard 			 */
5801da177e4SLinus Torvalds 			smi_info->si_state = SI_NORMAL;
5811da177e4SLinus Torvalds 		} else {
5821da177e4SLinus Torvalds 			smi_info->msg_flags = msg[3];
5831da177e4SLinus Torvalds 			handle_flags(smi_info);
5841da177e4SLinus Torvalds 		}
5851da177e4SLinus Torvalds 		break;
5861da177e4SLinus Torvalds 	}
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds 	case SI_CLEARING_FLAGS:
5891da177e4SLinus Torvalds 	{
5901da177e4SLinus Torvalds 		unsigned char msg[3];
5911da177e4SLinus Torvalds 
5921da177e4SLinus Torvalds 		/* We cleared the flags. */
5931da177e4SLinus Torvalds 		smi_info->handlers->get_result(smi_info->si_sm, msg, 3);
5941da177e4SLinus Torvalds 		if (msg[2] != 0) {
5951da177e4SLinus Torvalds 			/* Error clearing flags */
596910840f2SCorey Minyard 			dev_warn(smi_info->io.dev,
597279fbd0cSMyron Stowe 				 "Error clearing flags: %2.2x\n", msg[2]);
5981da177e4SLinus Torvalds 		}
5991da177e4SLinus Torvalds 		smi_info->si_state = SI_NORMAL;
6001da177e4SLinus Torvalds 		break;
6011da177e4SLinus Torvalds 	}
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds 	case SI_GETTING_EVENTS:
6041da177e4SLinus Torvalds 	{
6051da177e4SLinus Torvalds 		smi_info->curr_msg->rsp_size
6061da177e4SLinus Torvalds 			= smi_info->handlers->get_result(
6071da177e4SLinus Torvalds 				smi_info->si_sm,
6081da177e4SLinus Torvalds 				smi_info->curr_msg->rsp,
6091da177e4SLinus Torvalds 				IPMI_MAX_MSG_LENGTH);
6101da177e4SLinus Torvalds 
611c305e3d3SCorey Minyard 		/*
612c305e3d3SCorey Minyard 		 * Do this here becase deliver_recv_msg() releases the
613c305e3d3SCorey Minyard 		 * lock, and a new message can be put in during the
614c305e3d3SCorey Minyard 		 * time the lock is released.
615c305e3d3SCorey Minyard 		 */
6161da177e4SLinus Torvalds 		msg = smi_info->curr_msg;
6171da177e4SLinus Torvalds 		smi_info->curr_msg = NULL;
6181da177e4SLinus Torvalds 		if (msg->rsp[2] != 0) {
6191da177e4SLinus Torvalds 			/* Error getting event, probably done. */
6201da177e4SLinus Torvalds 			msg->done(msg);
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds 			/* Take off the event flag. */
6231da177e4SLinus Torvalds 			smi_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
6241da177e4SLinus Torvalds 			handle_flags(smi_info);
6251da177e4SLinus Torvalds 		} else {
62664959e2dSCorey Minyard 			smi_inc_stat(smi_info, events);
6271da177e4SLinus Torvalds 
628c305e3d3SCorey Minyard 			/*
629c305e3d3SCorey Minyard 			 * Do this before we deliver the message
630c305e3d3SCorey Minyard 			 * because delivering the message releases the
631c305e3d3SCorey Minyard 			 * lock and something else can mess with the
632c305e3d3SCorey Minyard 			 * state.
633c305e3d3SCorey Minyard 			 */
6341da177e4SLinus Torvalds 			handle_flags(smi_info);
6351da177e4SLinus Torvalds 
6361da177e4SLinus Torvalds 			deliver_recv_msg(smi_info, msg);
6371da177e4SLinus Torvalds 		}
6381da177e4SLinus Torvalds 		break;
6391da177e4SLinus Torvalds 	}
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds 	case SI_GETTING_MESSAGES:
6421da177e4SLinus Torvalds 	{
6431da177e4SLinus Torvalds 		smi_info->curr_msg->rsp_size
6441da177e4SLinus Torvalds 			= smi_info->handlers->get_result(
6451da177e4SLinus Torvalds 				smi_info->si_sm,
6461da177e4SLinus Torvalds 				smi_info->curr_msg->rsp,
6471da177e4SLinus Torvalds 				IPMI_MAX_MSG_LENGTH);
6481da177e4SLinus Torvalds 
649c305e3d3SCorey Minyard 		/*
650c305e3d3SCorey Minyard 		 * Do this here becase deliver_recv_msg() releases the
651c305e3d3SCorey Minyard 		 * lock, and a new message can be put in during the
652c305e3d3SCorey Minyard 		 * time the lock is released.
653c305e3d3SCorey Minyard 		 */
6541da177e4SLinus Torvalds 		msg = smi_info->curr_msg;
6551da177e4SLinus Torvalds 		smi_info->curr_msg = NULL;
6561da177e4SLinus Torvalds 		if (msg->rsp[2] != 0) {
6571da177e4SLinus Torvalds 			/* Error getting event, probably done. */
6581da177e4SLinus Torvalds 			msg->done(msg);
6591da177e4SLinus Torvalds 
6601da177e4SLinus Torvalds 			/* Take off the msg flag. */
6611da177e4SLinus Torvalds 			smi_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
6621da177e4SLinus Torvalds 			handle_flags(smi_info);
6631da177e4SLinus Torvalds 		} else {
66464959e2dSCorey Minyard 			smi_inc_stat(smi_info, incoming_messages);
6651da177e4SLinus Torvalds 
666c305e3d3SCorey Minyard 			/*
667c305e3d3SCorey Minyard 			 * Do this before we deliver the message
668c305e3d3SCorey Minyard 			 * because delivering the message releases the
669c305e3d3SCorey Minyard 			 * lock and something else can mess with the
670c305e3d3SCorey Minyard 			 * state.
671c305e3d3SCorey Minyard 			 */
6721da177e4SLinus Torvalds 			handle_flags(smi_info);
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds 			deliver_recv_msg(smi_info, msg);
6751da177e4SLinus Torvalds 		}
6761da177e4SLinus Torvalds 		break;
6771da177e4SLinus Torvalds 	}
6781da177e4SLinus Torvalds 
679d9b7e4f7SCorey Minyard 	case SI_CHECKING_ENABLES:
6801da177e4SLinus Torvalds 	{
6811da177e4SLinus Torvalds 		unsigned char msg[4];
682d9b7e4f7SCorey Minyard 		u8 enables;
68395c97b59SCorey Minyard 		bool irq_on;
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds 		/* We got the flags from the SMI, now handle them. */
6861da177e4SLinus Torvalds 		smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
6871da177e4SLinus Torvalds 		if (msg[2] != 0) {
688910840f2SCorey Minyard 			dev_warn(smi_info->io.dev,
6890849bfecSCorey Minyard 				 "Couldn't get irq info: %x.\n", msg[2]);
690910840f2SCorey Minyard 			dev_warn(smi_info->io.dev,
6910849bfecSCorey Minyard 				 "Maybe ok, but ipmi might run very slowly.\n");
6921da177e4SLinus Torvalds 			smi_info->si_state = SI_NORMAL;
693d9b7e4f7SCorey Minyard 			break;
694d9b7e4f7SCorey Minyard 		}
69595c97b59SCorey Minyard 		enables = current_global_enables(smi_info, 0, &irq_on);
696910840f2SCorey Minyard 		if (smi_info->io.si_type == SI_BT)
69795c97b59SCorey Minyard 			/* BT has its own interrupt enable bit. */
69895c97b59SCorey Minyard 			check_bt_irq(smi_info, irq_on);
699d9b7e4f7SCorey Minyard 		if (enables != (msg[3] & GLOBAL_ENABLES_MASK)) {
700d9b7e4f7SCorey Minyard 			/* Enables are not correct, fix them. */
7011da177e4SLinus Torvalds 			msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
7021da177e4SLinus Torvalds 			msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
703d9b7e4f7SCorey Minyard 			msg[2] = enables | (msg[3] & ~GLOBAL_ENABLES_MASK);
7041da177e4SLinus Torvalds 			smi_info->handlers->start_transaction(
7051da177e4SLinus Torvalds 				smi_info->si_sm, msg, 3);
706d9b7e4f7SCorey Minyard 			smi_info->si_state = SI_SETTING_ENABLES;
707d9b7e4f7SCorey Minyard 		} else if (smi_info->supports_event_msg_buff) {
708d9b7e4f7SCorey Minyard 			smi_info->curr_msg = ipmi_alloc_smi_msg();
709d9b7e4f7SCorey Minyard 			if (!smi_info->curr_msg) {
710ee6cd5f8SCorey Minyard 				smi_info->si_state = SI_NORMAL;
711d9b7e4f7SCorey Minyard 				break;
712d9b7e4f7SCorey Minyard 			}
7135ac7b2fcSCorey Minyard 			start_getting_events(smi_info);
714ee6cd5f8SCorey Minyard 		} else {
715d9b7e4f7SCorey Minyard 			smi_info->si_state = SI_NORMAL;
716ee6cd5f8SCorey Minyard 		}
717ee6cd5f8SCorey Minyard 		break;
718ee6cd5f8SCorey Minyard 	}
719ee6cd5f8SCorey Minyard 
720d9b7e4f7SCorey Minyard 	case SI_SETTING_ENABLES:
721ee6cd5f8SCorey Minyard 	{
722ee6cd5f8SCorey Minyard 		unsigned char msg[4];
723ee6cd5f8SCorey Minyard 
724ee6cd5f8SCorey Minyard 		smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
725d9b7e4f7SCorey Minyard 		if (msg[2] != 0)
726910840f2SCorey Minyard 			dev_warn(smi_info->io.dev,
727d9b7e4f7SCorey Minyard 				 "Could not set the global enables: 0x%x.\n",
728d9b7e4f7SCorey Minyard 				 msg[2]);
729d9b7e4f7SCorey Minyard 
730d9b7e4f7SCorey Minyard 		if (smi_info->supports_event_msg_buff) {
731d9b7e4f7SCorey Minyard 			smi_info->curr_msg = ipmi_alloc_smi_msg();
732d9b7e4f7SCorey Minyard 			if (!smi_info->curr_msg) {
733ee6cd5f8SCorey Minyard 				smi_info->si_state = SI_NORMAL;
734ee6cd5f8SCorey Minyard 				break;
735ee6cd5f8SCorey Minyard 			}
7365ac7b2fcSCorey Minyard 			start_getting_events(smi_info);
737d9b7e4f7SCorey Minyard 		} else {
738d9b7e4f7SCorey Minyard 			smi_info->si_state = SI_NORMAL;
739d9b7e4f7SCorey Minyard 		}
740d9b7e4f7SCorey Minyard 		break;
741d9b7e4f7SCorey Minyard 	}
7421da177e4SLinus Torvalds 	}
7431da177e4SLinus Torvalds }
7441da177e4SLinus Torvalds 
745c305e3d3SCorey Minyard /*
746c305e3d3SCorey Minyard  * Called on timeouts and events.  Timeouts should pass the elapsed
747c305e3d3SCorey Minyard  * time, interrupts should pass in zero.  Must be called with
748c305e3d3SCorey Minyard  * si_lock held and interrupts disabled.
749c305e3d3SCorey Minyard  */
7501da177e4SLinus Torvalds static enum si_sm_result smi_event_handler(struct smi_info *smi_info,
7511da177e4SLinus Torvalds 					   int time)
7521da177e4SLinus Torvalds {
7531da177e4SLinus Torvalds 	enum si_sm_result si_sm_result;
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds restart:
756c305e3d3SCorey Minyard 	/*
757c305e3d3SCorey Minyard 	 * There used to be a loop here that waited a little while
758c305e3d3SCorey Minyard 	 * (around 25us) before giving up.  That turned out to be
759c305e3d3SCorey Minyard 	 * pointless, the minimum delays I was seeing were in the 300us
760c305e3d3SCorey Minyard 	 * range, which is far too long to wait in an interrupt.  So
761c305e3d3SCorey Minyard 	 * we just run until the state machine tells us something
762c305e3d3SCorey Minyard 	 * happened or it needs a delay.
763c305e3d3SCorey Minyard 	 */
7641da177e4SLinus Torvalds 	si_sm_result = smi_info->handlers->event(smi_info->si_sm, time);
7651da177e4SLinus Torvalds 	time = 0;
7661da177e4SLinus Torvalds 	while (si_sm_result == SI_SM_CALL_WITHOUT_DELAY)
7671da177e4SLinus Torvalds 		si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
7681da177e4SLinus Torvalds 
769c305e3d3SCorey Minyard 	if (si_sm_result == SI_SM_TRANSACTION_COMPLETE) {
77064959e2dSCorey Minyard 		smi_inc_stat(smi_info, complete_transactions);
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds 		handle_transaction_done(smi_info);
773d9dffd2aSCorey Minyard 		goto restart;
774c305e3d3SCorey Minyard 	} else if (si_sm_result == SI_SM_HOSED) {
77564959e2dSCorey Minyard 		smi_inc_stat(smi_info, hosed_count);
7761da177e4SLinus Torvalds 
777c305e3d3SCorey Minyard 		/*
778c305e3d3SCorey Minyard 		 * Do the before return_hosed_msg, because that
779c305e3d3SCorey Minyard 		 * releases the lock.
780c305e3d3SCorey Minyard 		 */
7811da177e4SLinus Torvalds 		smi_info->si_state = SI_NORMAL;
7821da177e4SLinus Torvalds 		if (smi_info->curr_msg != NULL) {
783c305e3d3SCorey Minyard 			/*
784c305e3d3SCorey Minyard 			 * If we were handling a user message, format
785c305e3d3SCorey Minyard 			 * a response to send to the upper layer to
786c305e3d3SCorey Minyard 			 * tell it about the error.
787c305e3d3SCorey Minyard 			 */
7884d7cbac7SCorey Minyard 			return_hosed_msg(smi_info, IPMI_ERR_UNSPECIFIED);
7891da177e4SLinus Torvalds 		}
790d9dffd2aSCorey Minyard 		goto restart;
7911da177e4SLinus Torvalds 	}
7921da177e4SLinus Torvalds 
7934ea18425SCorey Minyard 	/*
7944ea18425SCorey Minyard 	 * We prefer handling attn over new messages.  But don't do
7954ea18425SCorey Minyard 	 * this if there is not yet an upper layer to handle anything.
7964ea18425SCorey Minyard 	 */
797a8df150cSCorey Minyard 	if (likely(smi_info->intf) &&
798a8df150cSCorey Minyard 	    (si_sm_result == SI_SM_ATTN || smi_info->got_attn)) {
7991da177e4SLinus Torvalds 		unsigned char msg[2];
8001da177e4SLinus Torvalds 
801a8df150cSCorey Minyard 		if (smi_info->si_state != SI_NORMAL) {
802a8df150cSCorey Minyard 			/*
803a8df150cSCorey Minyard 			 * We got an ATTN, but we are doing something else.
804a8df150cSCorey Minyard 			 * Handle the ATTN later.
805a8df150cSCorey Minyard 			 */
806a8df150cSCorey Minyard 			smi_info->got_attn = true;
807a8df150cSCorey Minyard 		} else {
808a8df150cSCorey Minyard 			smi_info->got_attn = false;
80964959e2dSCorey Minyard 			smi_inc_stat(smi_info, attentions);
8101da177e4SLinus Torvalds 
811c305e3d3SCorey Minyard 			/*
812c305e3d3SCorey Minyard 			 * Got a attn, send down a get message flags to see
813c305e3d3SCorey Minyard 			 * what's causing it.  It would be better to handle
814c305e3d3SCorey Minyard 			 * this in the upper layer, but due to the way
815c305e3d3SCorey Minyard 			 * interrupts work with the SMI, that's not really
816c305e3d3SCorey Minyard 			 * possible.
817c305e3d3SCorey Minyard 			 */
8181da177e4SLinus Torvalds 			msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
8191da177e4SLinus Torvalds 			msg[1] = IPMI_GET_MSG_FLAGS_CMD;
8201da177e4SLinus Torvalds 
8210cfec916SCorey Minyard 			start_new_msg(smi_info, msg, 2);
8221da177e4SLinus Torvalds 			smi_info->si_state = SI_GETTING_FLAGS;
8231da177e4SLinus Torvalds 			goto restart;
8241da177e4SLinus Torvalds 		}
825a8df150cSCorey Minyard 	}
8261da177e4SLinus Torvalds 
8271da177e4SLinus Torvalds 	/* If we are currently idle, try to start the next message. */
8281da177e4SLinus Torvalds 	if (si_sm_result == SI_SM_IDLE) {
82964959e2dSCorey Minyard 		smi_inc_stat(smi_info, idles);
8301da177e4SLinus Torvalds 
8311da177e4SLinus Torvalds 		si_sm_result = start_next_msg(smi_info);
8321da177e4SLinus Torvalds 		if (si_sm_result != SI_SM_IDLE)
8331da177e4SLinus Torvalds 			goto restart;
8341da177e4SLinus Torvalds 	}
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds 	if ((si_sm_result == SI_SM_IDLE)
837c305e3d3SCorey Minyard 	    && (atomic_read(&smi_info->req_events))) {
838c305e3d3SCorey Minyard 		/*
839c305e3d3SCorey Minyard 		 * We are idle and the upper layer requested that I fetch
840c305e3d3SCorey Minyard 		 * events, so do so.
841c305e3d3SCorey Minyard 		 */
8421da177e4SLinus Torvalds 		atomic_set(&smi_info->req_events, 0);
84355162fb1SCorey Minyard 
844d9b7e4f7SCorey Minyard 		/*
845d9b7e4f7SCorey Minyard 		 * Take this opportunity to check the interrupt and
846d9b7e4f7SCorey Minyard 		 * message enable state for the BMC.  The BMC can be
847d9b7e4f7SCorey Minyard 		 * asynchronously reset, and may thus get interrupts
848d9b7e4f7SCorey Minyard 		 * disable and messages disabled.
849d9b7e4f7SCorey Minyard 		 */
850910840f2SCorey Minyard 		if (smi_info->supports_event_msg_buff || smi_info->io.irq) {
8514f7f5551SMasamitsu Yamazaki 			start_check_enables(smi_info);
852d9b7e4f7SCorey Minyard 		} else {
853d9b7e4f7SCorey Minyard 			smi_info->curr_msg = alloc_msg_handle_irq(smi_info);
85455162fb1SCorey Minyard 			if (!smi_info->curr_msg)
85555162fb1SCorey Minyard 				goto out;
85655162fb1SCorey Minyard 
857d9b7e4f7SCorey Minyard 			start_getting_events(smi_info);
858d9b7e4f7SCorey Minyard 		}
8591da177e4SLinus Torvalds 		goto restart;
8601da177e4SLinus Torvalds 	}
861314ef52fSCorey Minyard 
862314ef52fSCorey Minyard 	if (si_sm_result == SI_SM_IDLE && smi_info->timer_running) {
863314ef52fSCorey Minyard 		/* Ok it if fails, the timer will just go off. */
864314ef52fSCorey Minyard 		if (del_timer(&smi_info->si_timer))
865314ef52fSCorey Minyard 			smi_info->timer_running = false;
866314ef52fSCorey Minyard 	}
867314ef52fSCorey Minyard 
86855162fb1SCorey Minyard out:
8691da177e4SLinus Torvalds 	return si_sm_result;
8701da177e4SLinus Torvalds }
8711da177e4SLinus Torvalds 
87289986496SCorey Minyard static void check_start_timer_thread(struct smi_info *smi_info)
87389986496SCorey Minyard {
87489986496SCorey Minyard 	if (smi_info->si_state == SI_NORMAL && smi_info->curr_msg == NULL) {
87589986496SCorey Minyard 		smi_mod_timer(smi_info, jiffies + SI_TIMEOUT_JIFFIES);
87689986496SCorey Minyard 
87789986496SCorey Minyard 		if (smi_info->thread)
87889986496SCorey Minyard 			wake_up_process(smi_info->thread);
87989986496SCorey Minyard 
88089986496SCorey Minyard 		start_next_msg(smi_info);
88189986496SCorey Minyard 		smi_event_handler(smi_info, 0);
88289986496SCorey Minyard 	}
88389986496SCorey Minyard }
88489986496SCorey Minyard 
88582802f96SHidehiro Kawai static void flush_messages(void *send_info)
886e45361d7SHidehiro Kawai {
88782802f96SHidehiro Kawai 	struct smi_info *smi_info = send_info;
888e45361d7SHidehiro Kawai 	enum si_sm_result result;
889e45361d7SHidehiro Kawai 
890e45361d7SHidehiro Kawai 	/*
891e45361d7SHidehiro Kawai 	 * Currently, this function is called only in run-to-completion
892e45361d7SHidehiro Kawai 	 * mode.  This means we are single-threaded, no need for locks.
893e45361d7SHidehiro Kawai 	 */
894e45361d7SHidehiro Kawai 	result = smi_event_handler(smi_info, 0);
895e45361d7SHidehiro Kawai 	while (result != SI_SM_IDLE) {
896e45361d7SHidehiro Kawai 		udelay(SI_SHORT_TIMEOUT_USEC);
897e45361d7SHidehiro Kawai 		result = smi_event_handler(smi_info, SI_SHORT_TIMEOUT_USEC);
898e45361d7SHidehiro Kawai 	}
899e45361d7SHidehiro Kawai }
900e45361d7SHidehiro Kawai 
9011da177e4SLinus Torvalds static void sender(void                *send_info,
90299ab32f3SCorey Minyard 		   struct ipmi_smi_msg *msg)
9031da177e4SLinus Torvalds {
9041da177e4SLinus Torvalds 	struct smi_info   *smi_info = send_info;
9051da177e4SLinus Torvalds 	unsigned long     flags;
9061da177e4SLinus Torvalds 
907f93aae9fSJohn Stultz 	debug_timestamp("Enqueue");
9081da177e4SLinus Torvalds 
9091da177e4SLinus Torvalds 	if (smi_info->run_to_completion) {
910bda4c30aSCorey Minyard 		/*
91182802f96SHidehiro Kawai 		 * If we are running to completion, start it.  Upper
91282802f96SHidehiro Kawai 		 * layer will call flush_messages to clear it out.
913bda4c30aSCorey Minyard 		 */
9149f812704SHidehiro Kawai 		smi_info->waiting_msg = msg;
9151da177e4SLinus Torvalds 		return;
9161da177e4SLinus Torvalds 	}
9171da177e4SLinus Torvalds 
918f60adf42SCorey Minyard 	spin_lock_irqsave(&smi_info->si_lock, flags);
9191d86e29bSCorey Minyard 	/*
9201d86e29bSCorey Minyard 	 * The following two lines don't need to be under the lock for
9211d86e29bSCorey Minyard 	 * the lock's sake, but they do need SMP memory barriers to
9221d86e29bSCorey Minyard 	 * avoid getting things out of order.  We are already claiming
9231d86e29bSCorey Minyard 	 * the lock, anyway, so just do it under the lock to avoid the
9241d86e29bSCorey Minyard 	 * ordering problem.
9251d86e29bSCorey Minyard 	 */
9261d86e29bSCorey Minyard 	BUG_ON(smi_info->waiting_msg);
9271d86e29bSCorey Minyard 	smi_info->waiting_msg = msg;
92889986496SCorey Minyard 	check_start_timer_thread(smi_info);
929bda4c30aSCorey Minyard 	spin_unlock_irqrestore(&smi_info->si_lock, flags);
9301da177e4SLinus Torvalds }
9311da177e4SLinus Torvalds 
9327aefac26SCorey Minyard static void set_run_to_completion(void *send_info, bool i_run_to_completion)
9331da177e4SLinus Torvalds {
9341da177e4SLinus Torvalds 	struct smi_info   *smi_info = send_info;
9351da177e4SLinus Torvalds 
9361da177e4SLinus Torvalds 	smi_info->run_to_completion = i_run_to_completion;
937e45361d7SHidehiro Kawai 	if (i_run_to_completion)
938e45361d7SHidehiro Kawai 		flush_messages(smi_info);
9391da177e4SLinus Torvalds }
9401da177e4SLinus Torvalds 
941ae74e823SMartin Wilck /*
942ae74e823SMartin Wilck  * Use -1 in the nsec value of the busy waiting timespec to tell that
943ae74e823SMartin Wilck  * we are spinning in kipmid looking for something and not delaying
944ae74e823SMartin Wilck  * between checks
945ae74e823SMartin Wilck  */
94648862ea2SJohn Stultz static inline void ipmi_si_set_not_busy(struct timespec64 *ts)
947ae74e823SMartin Wilck {
948ae74e823SMartin Wilck 	ts->tv_nsec = -1;
949ae74e823SMartin Wilck }
95048862ea2SJohn Stultz static inline int ipmi_si_is_busy(struct timespec64 *ts)
951ae74e823SMartin Wilck {
952ae74e823SMartin Wilck 	return ts->tv_nsec != -1;
953ae74e823SMartin Wilck }
954ae74e823SMartin Wilck 
955cc4cbe90SArnd Bergmann static inline int ipmi_thread_busy_wait(enum si_sm_result smi_result,
956ae74e823SMartin Wilck 					const struct smi_info *smi_info,
95748862ea2SJohn Stultz 					struct timespec64 *busy_until)
958ae74e823SMartin Wilck {
959ae74e823SMartin Wilck 	unsigned int max_busy_us = 0;
960ae74e823SMartin Wilck 
961ae74e823SMartin Wilck 	if (smi_info->intf_num < num_max_busy_us)
962ae74e823SMartin Wilck 		max_busy_us = kipmid_max_busy_us[smi_info->intf_num];
963ae74e823SMartin Wilck 	if (max_busy_us == 0 || smi_result != SI_SM_CALL_WITH_DELAY)
964ae74e823SMartin Wilck 		ipmi_si_set_not_busy(busy_until);
965ae74e823SMartin Wilck 	else if (!ipmi_si_is_busy(busy_until)) {
96648862ea2SJohn Stultz 		getnstimeofday64(busy_until);
96748862ea2SJohn Stultz 		timespec64_add_ns(busy_until, max_busy_us*NSEC_PER_USEC);
968ae74e823SMartin Wilck 	} else {
96948862ea2SJohn Stultz 		struct timespec64 now;
97048862ea2SJohn Stultz 
97148862ea2SJohn Stultz 		getnstimeofday64(&now);
97248862ea2SJohn Stultz 		if (unlikely(timespec64_compare(&now, busy_until) > 0)) {
973ae74e823SMartin Wilck 			ipmi_si_set_not_busy(busy_until);
974ae74e823SMartin Wilck 			return 0;
975ae74e823SMartin Wilck 		}
976ae74e823SMartin Wilck 	}
977ae74e823SMartin Wilck 	return 1;
978ae74e823SMartin Wilck }
979ae74e823SMartin Wilck 
980ae74e823SMartin Wilck 
981ae74e823SMartin Wilck /*
982ae74e823SMartin Wilck  * A busy-waiting loop for speeding up IPMI operation.
983ae74e823SMartin Wilck  *
984ae74e823SMartin Wilck  * Lousy hardware makes this hard.  This is only enabled for systems
985ae74e823SMartin Wilck  * that are not BT and do not have interrupts.  It starts spinning
986ae74e823SMartin Wilck  * when an operation is complete or until max_busy tells it to stop
987ae74e823SMartin Wilck  * (if that is enabled).  See the paragraph on kimid_max_busy_us in
988ae74e823SMartin Wilck  * Documentation/IPMI.txt for details.
989ae74e823SMartin Wilck  */
990a9a2c44fSCorey Minyard static int ipmi_thread(void *data)
991a9a2c44fSCorey Minyard {
992a9a2c44fSCorey Minyard 	struct smi_info *smi_info = data;
993e9a705a0SMatt Domsch 	unsigned long flags;
994a9a2c44fSCorey Minyard 	enum si_sm_result smi_result;
99548862ea2SJohn Stultz 	struct timespec64 busy_until;
996a9a2c44fSCorey Minyard 
997ae74e823SMartin Wilck 	ipmi_si_set_not_busy(&busy_until);
9988698a745SDongsheng Yang 	set_user_nice(current, MAX_NICE);
999e9a705a0SMatt Domsch 	while (!kthread_should_stop()) {
1000ae74e823SMartin Wilck 		int busy_wait;
1001ae74e823SMartin Wilck 
1002a9a2c44fSCorey Minyard 		spin_lock_irqsave(&(smi_info->si_lock), flags);
1003a9a2c44fSCorey Minyard 		smi_result = smi_event_handler(smi_info, 0);
100448e8ac29SBodo Stroesser 
100548e8ac29SBodo Stroesser 		/*
100648e8ac29SBodo Stroesser 		 * If the driver is doing something, there is a possible
100748e8ac29SBodo Stroesser 		 * race with the timer.  If the timer handler see idle,
100848e8ac29SBodo Stroesser 		 * and the thread here sees something else, the timer
100948e8ac29SBodo Stroesser 		 * handler won't restart the timer even though it is
101048e8ac29SBodo Stroesser 		 * required.  So start it here if necessary.
101148e8ac29SBodo Stroesser 		 */
101248e8ac29SBodo Stroesser 		if (smi_result != SI_SM_IDLE && !smi_info->timer_running)
101348e8ac29SBodo Stroesser 			smi_mod_timer(smi_info, jiffies + SI_TIMEOUT_JIFFIES);
101448e8ac29SBodo Stroesser 
1015a9a2c44fSCorey Minyard 		spin_unlock_irqrestore(&(smi_info->si_lock), flags);
1016ae74e823SMartin Wilck 		busy_wait = ipmi_thread_busy_wait(smi_result, smi_info,
1017ae74e823SMartin Wilck 						  &busy_until);
1018c305e3d3SCorey Minyard 		if (smi_result == SI_SM_CALL_WITHOUT_DELAY)
1019c305e3d3SCorey Minyard 			; /* do nothing */
1020ae74e823SMartin Wilck 		else if (smi_result == SI_SM_CALL_WITH_DELAY && busy_wait)
102133979734Sakpm@osdl.org 			schedule();
102289986496SCorey Minyard 		else if (smi_result == SI_SM_IDLE) {
102389986496SCorey Minyard 			if (atomic_read(&smi_info->need_watch)) {
10243326f4f2SMatthew Garrett 				schedule_timeout_interruptible(100);
102589986496SCorey Minyard 			} else {
102689986496SCorey Minyard 				/* Wait to be woken up when we are needed. */
102789986496SCorey Minyard 				__set_current_state(TASK_INTERRUPTIBLE);
102889986496SCorey Minyard 				schedule();
102989986496SCorey Minyard 			}
103089986496SCorey Minyard 		} else
10318d1f66dcSMartin Wilck 			schedule_timeout_interruptible(1);
1032a9a2c44fSCorey Minyard 	}
1033a9a2c44fSCorey Minyard 	return 0;
1034a9a2c44fSCorey Minyard }
1035a9a2c44fSCorey Minyard 
1036a9a2c44fSCorey Minyard 
10371da177e4SLinus Torvalds static void poll(void *send_info)
10381da177e4SLinus Torvalds {
10391da177e4SLinus Torvalds 	struct smi_info *smi_info = send_info;
1040f60adf42SCorey Minyard 	unsigned long flags = 0;
10417aefac26SCorey Minyard 	bool run_to_completion = smi_info->run_to_completion;
10421da177e4SLinus Torvalds 
104315c62e10SCorey Minyard 	/*
104415c62e10SCorey Minyard 	 * Make sure there is some delay in the poll loop so we can
104515c62e10SCorey Minyard 	 * drive time forward and timeout things.
104615c62e10SCorey Minyard 	 */
104715c62e10SCorey Minyard 	udelay(10);
1048f60adf42SCorey Minyard 	if (!run_to_completion)
1049fcfa4724SCorey Minyard 		spin_lock_irqsave(&smi_info->si_lock, flags);
105015c62e10SCorey Minyard 	smi_event_handler(smi_info, 10);
1051f60adf42SCorey Minyard 	if (!run_to_completion)
1052fcfa4724SCorey Minyard 		spin_unlock_irqrestore(&smi_info->si_lock, flags);
10531da177e4SLinus Torvalds }
10541da177e4SLinus Torvalds 
10551da177e4SLinus Torvalds static void request_events(void *send_info)
10561da177e4SLinus Torvalds {
10571da177e4SLinus Torvalds 	struct smi_info *smi_info = send_info;
10581da177e4SLinus Torvalds 
1059b874b985SCorey Minyard 	if (!smi_info->has_event_buffer)
1060b361e27bSCorey Minyard 		return;
1061b361e27bSCorey Minyard 
10621da177e4SLinus Torvalds 	atomic_set(&smi_info->req_events, 1);
10631da177e4SLinus Torvalds }
10641da177e4SLinus Torvalds 
10657aefac26SCorey Minyard static void set_need_watch(void *send_info, bool enable)
106689986496SCorey Minyard {
106789986496SCorey Minyard 	struct smi_info *smi_info = send_info;
106889986496SCorey Minyard 	unsigned long flags;
106989986496SCorey Minyard 
107089986496SCorey Minyard 	atomic_set(&smi_info->need_watch, enable);
107189986496SCorey Minyard 	spin_lock_irqsave(&smi_info->si_lock, flags);
107289986496SCorey Minyard 	check_start_timer_thread(smi_info);
107389986496SCorey Minyard 	spin_unlock_irqrestore(&smi_info->si_lock, flags);
107489986496SCorey Minyard }
107589986496SCorey Minyard 
1076e99e88a9SKees Cook static void smi_timeout(struct timer_list *t)
10771da177e4SLinus Torvalds {
1078e99e88a9SKees Cook 	struct smi_info   *smi_info = from_timer(smi_info, t, si_timer);
10791da177e4SLinus Torvalds 	enum si_sm_result smi_result;
10801da177e4SLinus Torvalds 	unsigned long     flags;
10811da177e4SLinus Torvalds 	unsigned long     jiffies_now;
1082c4edff1cSCorey Minyard 	long              time_diff;
10833326f4f2SMatthew Garrett 	long		  timeout;
10841da177e4SLinus Torvalds 
10851da177e4SLinus Torvalds 	spin_lock_irqsave(&(smi_info->si_lock), flags);
1086f93aae9fSJohn Stultz 	debug_timestamp("Timer");
1087f93aae9fSJohn Stultz 
10881da177e4SLinus Torvalds 	jiffies_now = jiffies;
1089c4edff1cSCorey Minyard 	time_diff = (((long)jiffies_now - (long)smi_info->last_timeout_jiffies)
10901da177e4SLinus Torvalds 		     * SI_USEC_PER_JIFFY);
10911da177e4SLinus Torvalds 	smi_result = smi_event_handler(smi_info, time_diff);
10921da177e4SLinus Torvalds 
1093910840f2SCorey Minyard 	if ((smi_info->io.irq) && (!smi_info->interrupt_disabled)) {
10941da177e4SLinus Torvalds 		/* Running with interrupts, only do long timeouts. */
10953326f4f2SMatthew Garrett 		timeout = jiffies + SI_TIMEOUT_JIFFIES;
109664959e2dSCorey Minyard 		smi_inc_stat(smi_info, long_timeouts);
10973326f4f2SMatthew Garrett 		goto do_mod_timer;
10981da177e4SLinus Torvalds 	}
10991da177e4SLinus Torvalds 
1100c305e3d3SCorey Minyard 	/*
1101c305e3d3SCorey Minyard 	 * If the state machine asks for a short delay, then shorten
1102c305e3d3SCorey Minyard 	 * the timer timeout.
1103c305e3d3SCorey Minyard 	 */
11041da177e4SLinus Torvalds 	if (smi_result == SI_SM_CALL_WITH_DELAY) {
110564959e2dSCorey Minyard 		smi_inc_stat(smi_info, short_timeouts);
11063326f4f2SMatthew Garrett 		timeout = jiffies + 1;
11071da177e4SLinus Torvalds 	} else {
110864959e2dSCorey Minyard 		smi_inc_stat(smi_info, long_timeouts);
11093326f4f2SMatthew Garrett 		timeout = jiffies + SI_TIMEOUT_JIFFIES;
11101da177e4SLinus Torvalds 	}
11111da177e4SLinus Torvalds 
11123326f4f2SMatthew Garrett do_mod_timer:
11133326f4f2SMatthew Garrett 	if (smi_result != SI_SM_IDLE)
111448e8ac29SBodo Stroesser 		smi_mod_timer(smi_info, timeout);
111548e8ac29SBodo Stroesser 	else
111648e8ac29SBodo Stroesser 		smi_info->timer_running = false;
111748e8ac29SBodo Stroesser 	spin_unlock_irqrestore(&(smi_info->si_lock), flags);
11181da177e4SLinus Torvalds }
11191da177e4SLinus Torvalds 
11204f3e8199SCorey Minyard irqreturn_t ipmi_si_irq_handler(int irq, void *data)
11211da177e4SLinus Torvalds {
11221da177e4SLinus Torvalds 	struct smi_info *smi_info = data;
11231da177e4SLinus Torvalds 	unsigned long   flags;
11241da177e4SLinus Torvalds 
11254f3e8199SCorey Minyard 	if (smi_info->io.si_type == SI_BT)
11264f3e8199SCorey Minyard 		/* We need to clear the IRQ flag for the BT interface. */
11274f3e8199SCorey Minyard 		smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG,
11284f3e8199SCorey Minyard 				     IPMI_BT_INTMASK_CLEAR_IRQ_BIT
11294f3e8199SCorey Minyard 				     | IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
11304f3e8199SCorey Minyard 
11311da177e4SLinus Torvalds 	spin_lock_irqsave(&(smi_info->si_lock), flags);
11321da177e4SLinus Torvalds 
113364959e2dSCorey Minyard 	smi_inc_stat(smi_info, interrupts);
11341da177e4SLinus Torvalds 
1135f93aae9fSJohn Stultz 	debug_timestamp("Interrupt");
1136f93aae9fSJohn Stultz 
11371da177e4SLinus Torvalds 	smi_event_handler(smi_info, 0);
11381da177e4SLinus Torvalds 	spin_unlock_irqrestore(&(smi_info->si_lock), flags);
11391da177e4SLinus Torvalds 	return IRQ_HANDLED;
11401da177e4SLinus Torvalds }
11411da177e4SLinus Torvalds 
1142453823baSCorey Minyard static int smi_start_processing(void       *send_info,
1143453823baSCorey Minyard 				ipmi_smi_t intf)
1144453823baSCorey Minyard {
1145453823baSCorey Minyard 	struct smi_info *new_smi = send_info;
1146a51f4a81SCorey Minyard 	int             enable = 0;
1147453823baSCorey Minyard 
1148453823baSCorey Minyard 	new_smi->intf = intf;
1149453823baSCorey Minyard 
1150453823baSCorey Minyard 	/* Set up the timer that drives the interface. */
1151e99e88a9SKees Cook 	timer_setup(&new_smi->si_timer, smi_timeout, 0);
11524f7f5551SMasamitsu Yamazaki 	new_smi->timer_can_start = true;
115348e8ac29SBodo Stroesser 	smi_mod_timer(new_smi, jiffies + SI_TIMEOUT_JIFFIES);
1154453823baSCorey Minyard 
115527f972d3SJan Stancek 	/* Try to claim any interrupts. */
11564f3e8199SCorey Minyard 	if (new_smi->io.irq_setup) {
11574f3e8199SCorey Minyard 		new_smi->io.irq_handler_data = new_smi;
11584f3e8199SCorey Minyard 		new_smi->io.irq_setup(&new_smi->io);
11594f3e8199SCorey Minyard 	}
116027f972d3SJan Stancek 
1161df3fe8deSCorey Minyard 	/*
1162a51f4a81SCorey Minyard 	 * Check if the user forcefully enabled the daemon.
1163a51f4a81SCorey Minyard 	 */
1164a51f4a81SCorey Minyard 	if (new_smi->intf_num < num_force_kipmid)
1165a51f4a81SCorey Minyard 		enable = force_kipmid[new_smi->intf_num];
1166a51f4a81SCorey Minyard 	/*
1167df3fe8deSCorey Minyard 	 * The BT interface is efficient enough to not need a thread,
1168df3fe8deSCorey Minyard 	 * and there is no need for a thread if we have interrupts.
1169df3fe8deSCorey Minyard 	 */
1170910840f2SCorey Minyard 	else if ((new_smi->io.si_type != SI_BT) && (!new_smi->io.irq))
1171a51f4a81SCorey Minyard 		enable = 1;
1172a51f4a81SCorey Minyard 
1173a51f4a81SCorey Minyard 	if (enable) {
1174453823baSCorey Minyard 		new_smi->thread = kthread_run(ipmi_thread, new_smi,
1175453823baSCorey Minyard 					      "kipmi%d", new_smi->intf_num);
1176453823baSCorey Minyard 		if (IS_ERR(new_smi->thread)) {
1177910840f2SCorey Minyard 			dev_notice(new_smi->io.dev, "Could not start"
1178453823baSCorey Minyard 				   " kernel thread due to error %ld, only using"
1179453823baSCorey Minyard 				   " timers to drive the interface\n",
1180453823baSCorey Minyard 				   PTR_ERR(new_smi->thread));
1181453823baSCorey Minyard 			new_smi->thread = NULL;
1182453823baSCorey Minyard 		}
1183453823baSCorey Minyard 	}
1184453823baSCorey Minyard 
1185453823baSCorey Minyard 	return 0;
1186453823baSCorey Minyard }
11879dbf68f9SCorey Minyard 
118816f4232cSZhao Yakui static int get_smi_info(void *send_info, struct ipmi_smi_info *data)
118916f4232cSZhao Yakui {
119016f4232cSZhao Yakui 	struct smi_info *smi = send_info;
119116f4232cSZhao Yakui 
1192910840f2SCorey Minyard 	data->addr_src = smi->io.addr_source;
1193910840f2SCorey Minyard 	data->dev = smi->io.dev;
1194bb398a4cSCorey Minyard 	data->addr_info = smi->io.addr_info;
1195910840f2SCorey Minyard 	get_device(smi->io.dev);
119616f4232cSZhao Yakui 
119716f4232cSZhao Yakui 	return 0;
119816f4232cSZhao Yakui }
119916f4232cSZhao Yakui 
12007aefac26SCorey Minyard static void set_maintenance_mode(void *send_info, bool enable)
1201b9675136SCorey Minyard {
1202b9675136SCorey Minyard 	struct smi_info   *smi_info = send_info;
1203b9675136SCorey Minyard 
1204b9675136SCorey Minyard 	if (!enable)
1205b9675136SCorey Minyard 		atomic_set(&smi_info->req_events, 0);
1206b9675136SCorey Minyard }
1207b9675136SCorey Minyard 
120881d02b7fSCorey Minyard static const struct ipmi_smi_handlers handlers = {
12091da177e4SLinus Torvalds 	.owner                  = THIS_MODULE,
1210453823baSCorey Minyard 	.start_processing       = smi_start_processing,
121116f4232cSZhao Yakui 	.get_smi_info		= get_smi_info,
12121da177e4SLinus Torvalds 	.sender			= sender,
12131da177e4SLinus Torvalds 	.request_events		= request_events,
121489986496SCorey Minyard 	.set_need_watch		= set_need_watch,
1215b9675136SCorey Minyard 	.set_maintenance_mode   = set_maintenance_mode,
12161da177e4SLinus Torvalds 	.set_run_to_completion  = set_run_to_completion,
121782802f96SHidehiro Kawai 	.flush_messages		= flush_messages,
12181da177e4SLinus Torvalds 	.poll			= poll,
12191da177e4SLinus Torvalds };
12201da177e4SLinus Torvalds 
1221b0defcdbSCorey Minyard static LIST_HEAD(smi_infos);
1222d6dfd131SCorey Minyard static DEFINE_MUTEX(smi_infos_lock);
1223b0defcdbSCorey Minyard static int smi_num; /* Used to sequence the SMIs */
12241da177e4SLinus Torvalds 
122599ee6735SLABBE Corentin static const char * const addr_space_to_str[] = { "i/o", "mem" };
1226b361e27bSCorey Minyard 
1227a51f4a81SCorey Minyard module_param_array(force_kipmid, int, &num_force_kipmid, 0);
1228a51f4a81SCorey Minyard MODULE_PARM_DESC(force_kipmid, "Force the kipmi daemon to be enabled (1) or"
1229a51f4a81SCorey Minyard 		 " disabled(0).  Normally the IPMI driver auto-detects"
1230a51f4a81SCorey Minyard 		 " this, but the value may be overridden by this parm.");
12317aefac26SCorey Minyard module_param(unload_when_empty, bool, 0);
1232b361e27bSCorey Minyard MODULE_PARM_DESC(unload_when_empty, "Unload the module if no interfaces are"
1233b361e27bSCorey Minyard 		 " specified or found, default is 1.  Setting to 0"
1234b361e27bSCorey Minyard 		 " is useful for hot add of devices using hotmod.");
1235ae74e823SMartin Wilck module_param_array(kipmid_max_busy_us, uint, &num_max_busy_us, 0644);
1236ae74e823SMartin Wilck MODULE_PARM_DESC(kipmid_max_busy_us,
1237ae74e823SMartin Wilck 		 "Max time (in microseconds) to busy-wait for IPMI data before"
1238ae74e823SMartin Wilck 		 " sleeping. 0 (default) means to wait forever. Set to 100-500"
1239ae74e823SMartin Wilck 		 " if kipmid is using up a lot of CPU time.");
12401da177e4SLinus Torvalds 
12414f3e8199SCorey Minyard void ipmi_irq_finish_setup(struct si_sm_io *io)
12421da177e4SLinus Torvalds {
12434f3e8199SCorey Minyard 	if (io->si_type == SI_BT)
12444f3e8199SCorey Minyard 		/* Enable the interrupt in the BT interface. */
12454f3e8199SCorey Minyard 		io->outputb(io, IPMI_BT_INTMASK_REG,
12464f3e8199SCorey Minyard 			    IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
12471da177e4SLinus Torvalds }
12481da177e4SLinus Torvalds 
12494f3e8199SCorey Minyard void ipmi_irq_start_cleanup(struct si_sm_io *io)
12504f3e8199SCorey Minyard {
12514f3e8199SCorey Minyard 	if (io->si_type == SI_BT)
12524f3e8199SCorey Minyard 		/* Disable the interrupt in the BT interface. */
12534f3e8199SCorey Minyard 		io->outputb(io, IPMI_BT_INTMASK_REG, 0);
12544f3e8199SCorey Minyard }
12554f3e8199SCorey Minyard 
12564f3e8199SCorey Minyard static void std_irq_cleanup(struct si_sm_io *io)
12574f3e8199SCorey Minyard {
12584f3e8199SCorey Minyard 	ipmi_irq_start_cleanup(io);
12594f3e8199SCorey Minyard 	free_irq(io->irq, io->irq_handler_data);
12604f3e8199SCorey Minyard }
12614f3e8199SCorey Minyard 
12624f3e8199SCorey Minyard int ipmi_std_irq_setup(struct si_sm_io *io)
12631da177e4SLinus Torvalds {
12641da177e4SLinus Torvalds 	int rv;
12651da177e4SLinus Torvalds 
12664f3e8199SCorey Minyard 	if (!io->irq)
12671da177e4SLinus Torvalds 		return 0;
12681da177e4SLinus Torvalds 
12694f3e8199SCorey Minyard 	rv = request_irq(io->irq,
12704f3e8199SCorey Minyard 			 ipmi_si_irq_handler,
1271aa5b2babSMichael Opdenacker 			 IRQF_SHARED,
12729dbf68f9SCorey Minyard 			 DEVICE_NAME,
12734f3e8199SCorey Minyard 			 io->irq_handler_data);
12741da177e4SLinus Torvalds 	if (rv) {
12754f3e8199SCorey Minyard 		dev_warn(io->dev, "%s unable to claim interrupt %d,"
12761da177e4SLinus Torvalds 			 " running polled\n",
12774f3e8199SCorey Minyard 			 DEVICE_NAME, io->irq);
12784f3e8199SCorey Minyard 		io->irq = 0;
12791da177e4SLinus Torvalds 	} else {
12804f3e8199SCorey Minyard 		io->irq_cleanup = std_irq_cleanup;
12814f3e8199SCorey Minyard 		ipmi_irq_finish_setup(io);
12824f3e8199SCorey Minyard 		dev_info(io->dev, "Using irq %d\n", io->irq);
12831da177e4SLinus Torvalds 	}
12841da177e4SLinus Torvalds 
12851da177e4SLinus Torvalds 	return rv;
12861da177e4SLinus Torvalds }
12871da177e4SLinus Torvalds 
128840112ae7SCorey Minyard static int wait_for_msg_done(struct smi_info *smi_info)
12891da177e4SLinus Torvalds {
12901da177e4SLinus Torvalds 	enum si_sm_result     smi_result;
12911da177e4SLinus Torvalds 
12921da177e4SLinus Torvalds 	smi_result = smi_info->handlers->event(smi_info->si_sm, 0);
1293c305e3d3SCorey Minyard 	for (;;) {
1294c3e7e791SCorey Minyard 		if (smi_result == SI_SM_CALL_WITH_DELAY ||
1295c3e7e791SCorey Minyard 		    smi_result == SI_SM_CALL_WITH_TICK_DELAY) {
1296da4cd8dfSNishanth Aravamudan 			schedule_timeout_uninterruptible(1);
12971da177e4SLinus Torvalds 			smi_result = smi_info->handlers->event(
1298e21404dcSXie XiuQi 				smi_info->si_sm, jiffies_to_usecs(1));
1299c305e3d3SCorey Minyard 		} else if (smi_result == SI_SM_CALL_WITHOUT_DELAY) {
13001da177e4SLinus Torvalds 			smi_result = smi_info->handlers->event(
13011da177e4SLinus Torvalds 				smi_info->si_sm, 0);
1302c305e3d3SCorey Minyard 		} else
13031da177e4SLinus Torvalds 			break;
13041da177e4SLinus Torvalds 	}
130540112ae7SCorey Minyard 	if (smi_result == SI_SM_HOSED)
1306c305e3d3SCorey Minyard 		/*
1307c305e3d3SCorey Minyard 		 * We couldn't get the state machine to run, so whatever's at
1308c305e3d3SCorey Minyard 		 * the port is probably not an IPMI SMI interface.
1309c305e3d3SCorey Minyard 		 */
131040112ae7SCorey Minyard 		return -ENODEV;
131140112ae7SCorey Minyard 
131240112ae7SCorey Minyard 	return 0;
13131da177e4SLinus Torvalds }
13141da177e4SLinus Torvalds 
131540112ae7SCorey Minyard static int try_get_dev_id(struct smi_info *smi_info)
131640112ae7SCorey Minyard {
131740112ae7SCorey Minyard 	unsigned char         msg[2];
131840112ae7SCorey Minyard 	unsigned char         *resp;
131940112ae7SCorey Minyard 	unsigned long         resp_len;
132040112ae7SCorey Minyard 	int                   rv = 0;
132140112ae7SCorey Minyard 
132240112ae7SCorey Minyard 	resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
132340112ae7SCorey Minyard 	if (!resp)
132440112ae7SCorey Minyard 		return -ENOMEM;
132540112ae7SCorey Minyard 
132640112ae7SCorey Minyard 	/*
132740112ae7SCorey Minyard 	 * Do a Get Device ID command, since it comes back with some
132840112ae7SCorey Minyard 	 * useful info.
132940112ae7SCorey Minyard 	 */
133040112ae7SCorey Minyard 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
133140112ae7SCorey Minyard 	msg[1] = IPMI_GET_DEVICE_ID_CMD;
133240112ae7SCorey Minyard 	smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
133340112ae7SCorey Minyard 
133440112ae7SCorey Minyard 	rv = wait_for_msg_done(smi_info);
133540112ae7SCorey Minyard 	if (rv)
133640112ae7SCorey Minyard 		goto out;
133740112ae7SCorey Minyard 
13381da177e4SLinus Torvalds 	resp_len = smi_info->handlers->get_result(smi_info->si_sm,
13391da177e4SLinus Torvalds 						  resp, IPMI_MAX_MSG_LENGTH);
13401da177e4SLinus Torvalds 
1341d8c98618SCorey Minyard 	/* Check and record info from the get device id, in case we need it. */
1342c468f911SJeremy Kerr 	rv = ipmi_demangle_device_id(resp[0] >> 2, resp[1],
1343c468f911SJeremy Kerr 			resp + 2, resp_len - 2, &smi_info->device_id);
13441da177e4SLinus Torvalds 
13451da177e4SLinus Torvalds out:
13461da177e4SLinus Torvalds 	kfree(resp);
13471da177e4SLinus Torvalds 	return rv;
13481da177e4SLinus Torvalds }
13491da177e4SLinus Torvalds 
1350d0882897SCorey Minyard static int get_global_enables(struct smi_info *smi_info, u8 *enables)
13511e7d6a45SCorey Minyard {
13521e7d6a45SCorey Minyard 	unsigned char         msg[3];
13531e7d6a45SCorey Minyard 	unsigned char         *resp;
13541e7d6a45SCorey Minyard 	unsigned long         resp_len;
13551e7d6a45SCorey Minyard 	int                   rv;
13561e7d6a45SCorey Minyard 
13571e7d6a45SCorey Minyard 	resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1358d0882897SCorey Minyard 	if (!resp)
1359d0882897SCorey Minyard 		return -ENOMEM;
13601e7d6a45SCorey Minyard 
13611e7d6a45SCorey Minyard 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
13621e7d6a45SCorey Minyard 	msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
13631e7d6a45SCorey Minyard 	smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
13641e7d6a45SCorey Minyard 
13651e7d6a45SCorey Minyard 	rv = wait_for_msg_done(smi_info);
13661e7d6a45SCorey Minyard 	if (rv) {
1367910840f2SCorey Minyard 		dev_warn(smi_info->io.dev,
1368d0882897SCorey Minyard 			 "Error getting response from get global enables command: %d\n",
1369d0882897SCorey Minyard 			 rv);
13701e7d6a45SCorey Minyard 		goto out;
13711e7d6a45SCorey Minyard 	}
13721e7d6a45SCorey Minyard 
13731e7d6a45SCorey Minyard 	resp_len = smi_info->handlers->get_result(smi_info->si_sm,
13741e7d6a45SCorey Minyard 						  resp, IPMI_MAX_MSG_LENGTH);
13751e7d6a45SCorey Minyard 
13761e7d6a45SCorey Minyard 	if (resp_len < 4 ||
13771e7d6a45SCorey Minyard 			resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
13781e7d6a45SCorey Minyard 			resp[1] != IPMI_GET_BMC_GLOBAL_ENABLES_CMD   ||
13791e7d6a45SCorey Minyard 			resp[2] != 0) {
1380910840f2SCorey Minyard 		dev_warn(smi_info->io.dev,
1381d0882897SCorey Minyard 			 "Invalid return from get global enables command: %ld %x %x %x\n",
1382d0882897SCorey Minyard 			 resp_len, resp[0], resp[1], resp[2]);
13831e7d6a45SCorey Minyard 		rv = -EINVAL;
13841e7d6a45SCorey Minyard 		goto out;
1385d0882897SCorey Minyard 	} else {
1386d0882897SCorey Minyard 		*enables = resp[3];
13871e7d6a45SCorey Minyard 	}
13881e7d6a45SCorey Minyard 
1389d0882897SCorey Minyard out:
1390d0882897SCorey Minyard 	kfree(resp);
1391d0882897SCorey Minyard 	return rv;
1392d0882897SCorey Minyard }
1393d0882897SCorey Minyard 
1394d0882897SCorey Minyard /*
1395d0882897SCorey Minyard  * Returns 1 if it gets an error from the command.
1396d0882897SCorey Minyard  */
1397d0882897SCorey Minyard static int set_global_enables(struct smi_info *smi_info, u8 enables)
1398d0882897SCorey Minyard {
1399d0882897SCorey Minyard 	unsigned char         msg[3];
1400d0882897SCorey Minyard 	unsigned char         *resp;
1401d0882897SCorey Minyard 	unsigned long         resp_len;
1402d0882897SCorey Minyard 	int                   rv;
1403d0882897SCorey Minyard 
1404d0882897SCorey Minyard 	resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1405d0882897SCorey Minyard 	if (!resp)
1406d0882897SCorey Minyard 		return -ENOMEM;
14071e7d6a45SCorey Minyard 
14081e7d6a45SCorey Minyard 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
14091e7d6a45SCorey Minyard 	msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
1410d0882897SCorey Minyard 	msg[2] = enables;
14111e7d6a45SCorey Minyard 	smi_info->handlers->start_transaction(smi_info->si_sm, msg, 3);
14121e7d6a45SCorey Minyard 
14131e7d6a45SCorey Minyard 	rv = wait_for_msg_done(smi_info);
14141e7d6a45SCorey Minyard 	if (rv) {
1415910840f2SCorey Minyard 		dev_warn(smi_info->io.dev,
1416d0882897SCorey Minyard 			 "Error getting response from set global enables command: %d\n",
1417d0882897SCorey Minyard 			 rv);
14181e7d6a45SCorey Minyard 		goto out;
14191e7d6a45SCorey Minyard 	}
14201e7d6a45SCorey Minyard 
14211e7d6a45SCorey Minyard 	resp_len = smi_info->handlers->get_result(smi_info->si_sm,
14221e7d6a45SCorey Minyard 						  resp, IPMI_MAX_MSG_LENGTH);
14231e7d6a45SCorey Minyard 
14241e7d6a45SCorey Minyard 	if (resp_len < 3 ||
14251e7d6a45SCorey Minyard 			resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
14261e7d6a45SCorey Minyard 			resp[1] != IPMI_SET_BMC_GLOBAL_ENABLES_CMD) {
1427910840f2SCorey Minyard 		dev_warn(smi_info->io.dev,
1428d0882897SCorey Minyard 			 "Invalid return from set global enables command: %ld %x %x\n",
1429d0882897SCorey Minyard 			 resp_len, resp[0], resp[1]);
14301e7d6a45SCorey Minyard 		rv = -EINVAL;
14311e7d6a45SCorey Minyard 		goto out;
14321e7d6a45SCorey Minyard 	}
14331e7d6a45SCorey Minyard 
1434d0882897SCorey Minyard 	if (resp[2] != 0)
1435d0882897SCorey Minyard 		rv = 1;
1436d0882897SCorey Minyard 
1437d0882897SCorey Minyard out:
1438d0882897SCorey Minyard 	kfree(resp);
1439d0882897SCorey Minyard 	return rv;
1440d0882897SCorey Minyard }
1441d0882897SCorey Minyard 
1442d0882897SCorey Minyard /*
1443d0882897SCorey Minyard  * Some BMCs do not support clearing the receive irq bit in the global
1444d0882897SCorey Minyard  * enables (even if they don't support interrupts on the BMC).  Check
1445d0882897SCorey Minyard  * for this and handle it properly.
1446d0882897SCorey Minyard  */
1447d0882897SCorey Minyard static void check_clr_rcv_irq(struct smi_info *smi_info)
1448d0882897SCorey Minyard {
1449d0882897SCorey Minyard 	u8 enables = 0;
1450d0882897SCorey Minyard 	int rv;
1451d0882897SCorey Minyard 
1452d0882897SCorey Minyard 	rv = get_global_enables(smi_info, &enables);
1453d0882897SCorey Minyard 	if (!rv) {
1454d0882897SCorey Minyard 		if ((enables & IPMI_BMC_RCV_MSG_INTR) == 0)
1455d0882897SCorey Minyard 			/* Already clear, should work ok. */
1456d0882897SCorey Minyard 			return;
1457d0882897SCorey Minyard 
1458d0882897SCorey Minyard 		enables &= ~IPMI_BMC_RCV_MSG_INTR;
1459d0882897SCorey Minyard 		rv = set_global_enables(smi_info, enables);
1460d0882897SCorey Minyard 	}
1461d0882897SCorey Minyard 
1462d0882897SCorey Minyard 	if (rv < 0) {
1463910840f2SCorey Minyard 		dev_err(smi_info->io.dev,
1464d0882897SCorey Minyard 			"Cannot check clearing the rcv irq: %d\n", rv);
1465d0882897SCorey Minyard 		return;
1466d0882897SCorey Minyard 	}
1467d0882897SCorey Minyard 
1468d0882897SCorey Minyard 	if (rv) {
14691e7d6a45SCorey Minyard 		/*
14701e7d6a45SCorey Minyard 		 * An error when setting the event buffer bit means
14711e7d6a45SCorey Minyard 		 * clearing the bit is not supported.
14721e7d6a45SCorey Minyard 		 */
1473910840f2SCorey Minyard 		dev_warn(smi_info->io.dev,
1474d0882897SCorey Minyard 			 "The BMC does not support clearing the recv irq bit, compensating, but the BMC needs to be fixed.\n");
1475d0882897SCorey Minyard 		smi_info->cannot_disable_irq = true;
14761e7d6a45SCorey Minyard 	}
1477d0882897SCorey Minyard }
1478d0882897SCorey Minyard 
1479d0882897SCorey Minyard /*
1480d0882897SCorey Minyard  * Some BMCs do not support setting the interrupt bits in the global
1481d0882897SCorey Minyard  * enables even if they support interrupts.  Clearly bad, but we can
1482d0882897SCorey Minyard  * compensate.
1483d0882897SCorey Minyard  */
1484d0882897SCorey Minyard static void check_set_rcv_irq(struct smi_info *smi_info)
1485d0882897SCorey Minyard {
1486d0882897SCorey Minyard 	u8 enables = 0;
1487d0882897SCorey Minyard 	int rv;
1488d0882897SCorey Minyard 
1489910840f2SCorey Minyard 	if (!smi_info->io.irq)
1490d0882897SCorey Minyard 		return;
1491d0882897SCorey Minyard 
1492d0882897SCorey Minyard 	rv = get_global_enables(smi_info, &enables);
1493d0882897SCorey Minyard 	if (!rv) {
1494d0882897SCorey Minyard 		enables |= IPMI_BMC_RCV_MSG_INTR;
1495d0882897SCorey Minyard 		rv = set_global_enables(smi_info, enables);
1496d0882897SCorey Minyard 	}
1497d0882897SCorey Minyard 
1498d0882897SCorey Minyard 	if (rv < 0) {
1499910840f2SCorey Minyard 		dev_err(smi_info->io.dev,
1500d0882897SCorey Minyard 			"Cannot check setting the rcv irq: %d\n", rv);
1501d0882897SCorey Minyard 		return;
1502d0882897SCorey Minyard 	}
1503d0882897SCorey Minyard 
1504d0882897SCorey Minyard 	if (rv) {
1505d0882897SCorey Minyard 		/*
1506d0882897SCorey Minyard 		 * An error when setting the event buffer bit means
1507d0882897SCorey Minyard 		 * setting the bit is not supported.
1508d0882897SCorey Minyard 		 */
1509910840f2SCorey Minyard 		dev_warn(smi_info->io.dev,
1510d0882897SCorey Minyard 			 "The BMC does not support setting the recv irq bit, compensating, but the BMC needs to be fixed.\n");
1511d0882897SCorey Minyard 		smi_info->cannot_disable_irq = true;
1512d0882897SCorey Minyard 		smi_info->irq_enable_broken = true;
1513d0882897SCorey Minyard 	}
15141e7d6a45SCorey Minyard }
15151e7d6a45SCorey Minyard 
151640112ae7SCorey Minyard static int try_enable_event_buffer(struct smi_info *smi_info)
151740112ae7SCorey Minyard {
151840112ae7SCorey Minyard 	unsigned char         msg[3];
151940112ae7SCorey Minyard 	unsigned char         *resp;
152040112ae7SCorey Minyard 	unsigned long         resp_len;
152140112ae7SCorey Minyard 	int                   rv = 0;
152240112ae7SCorey Minyard 
152340112ae7SCorey Minyard 	resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
152440112ae7SCorey Minyard 	if (!resp)
152540112ae7SCorey Minyard 		return -ENOMEM;
152640112ae7SCorey Minyard 
152740112ae7SCorey Minyard 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
152840112ae7SCorey Minyard 	msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
152940112ae7SCorey Minyard 	smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
153040112ae7SCorey Minyard 
153140112ae7SCorey Minyard 	rv = wait_for_msg_done(smi_info);
153240112ae7SCorey Minyard 	if (rv) {
1533bb2a08c0SCorey Minyard 		pr_warn(PFX "Error getting response from get global enables command, the event buffer is not enabled.\n");
153440112ae7SCorey Minyard 		goto out;
153540112ae7SCorey Minyard 	}
153640112ae7SCorey Minyard 
153740112ae7SCorey Minyard 	resp_len = smi_info->handlers->get_result(smi_info->si_sm,
153840112ae7SCorey Minyard 						  resp, IPMI_MAX_MSG_LENGTH);
153940112ae7SCorey Minyard 
154040112ae7SCorey Minyard 	if (resp_len < 4 ||
154140112ae7SCorey Minyard 			resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
154240112ae7SCorey Minyard 			resp[1] != IPMI_GET_BMC_GLOBAL_ENABLES_CMD   ||
154340112ae7SCorey Minyard 			resp[2] != 0) {
1544bb2a08c0SCorey Minyard 		pr_warn(PFX "Invalid return from get global enables command, cannot enable the event buffer.\n");
154540112ae7SCorey Minyard 		rv = -EINVAL;
154640112ae7SCorey Minyard 		goto out;
154740112ae7SCorey Minyard 	}
154840112ae7SCorey Minyard 
1549d9b7e4f7SCorey Minyard 	if (resp[3] & IPMI_BMC_EVT_MSG_BUFF) {
155040112ae7SCorey Minyard 		/* buffer is already enabled, nothing to do. */
1551d9b7e4f7SCorey Minyard 		smi_info->supports_event_msg_buff = true;
155240112ae7SCorey Minyard 		goto out;
1553d9b7e4f7SCorey Minyard 	}
155440112ae7SCorey Minyard 
155540112ae7SCorey Minyard 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
155640112ae7SCorey Minyard 	msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
155740112ae7SCorey Minyard 	msg[2] = resp[3] | IPMI_BMC_EVT_MSG_BUFF;
155840112ae7SCorey Minyard 	smi_info->handlers->start_transaction(smi_info->si_sm, msg, 3);
155940112ae7SCorey Minyard 
156040112ae7SCorey Minyard 	rv = wait_for_msg_done(smi_info);
156140112ae7SCorey Minyard 	if (rv) {
1562bb2a08c0SCorey Minyard 		pr_warn(PFX "Error getting response from set global, enables command, the event buffer is not enabled.\n");
156340112ae7SCorey Minyard 		goto out;
156440112ae7SCorey Minyard 	}
156540112ae7SCorey Minyard 
156640112ae7SCorey Minyard 	resp_len = smi_info->handlers->get_result(smi_info->si_sm,
156740112ae7SCorey Minyard 						  resp, IPMI_MAX_MSG_LENGTH);
156840112ae7SCorey Minyard 
156940112ae7SCorey Minyard 	if (resp_len < 3 ||
157040112ae7SCorey Minyard 			resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
157140112ae7SCorey Minyard 			resp[1] != IPMI_SET_BMC_GLOBAL_ENABLES_CMD) {
1572bb2a08c0SCorey Minyard 		pr_warn(PFX "Invalid return from get global, enables command, not enable the event buffer.\n");
157340112ae7SCorey Minyard 		rv = -EINVAL;
157440112ae7SCorey Minyard 		goto out;
157540112ae7SCorey Minyard 	}
157640112ae7SCorey Minyard 
157740112ae7SCorey Minyard 	if (resp[2] != 0)
157840112ae7SCorey Minyard 		/*
157940112ae7SCorey Minyard 		 * An error when setting the event buffer bit means
158040112ae7SCorey Minyard 		 * that the event buffer is not supported.
158140112ae7SCorey Minyard 		 */
158240112ae7SCorey Minyard 		rv = -ENOENT;
1583d9b7e4f7SCorey Minyard 	else
1584d9b7e4f7SCorey Minyard 		smi_info->supports_event_msg_buff = true;
1585d9b7e4f7SCorey Minyard 
158640112ae7SCorey Minyard out:
158740112ae7SCorey Minyard 	kfree(resp);
158840112ae7SCorey Minyard 	return rv;
158940112ae7SCorey Minyard }
159040112ae7SCorey Minyard 
159155f91cb6SCorey Minyard #ifdef CONFIG_IPMI_PROC_INTERFACE
159207412736SAlexey Dobriyan static int smi_type_proc_show(struct seq_file *m, void *v)
15931da177e4SLinus Torvalds {
159407412736SAlexey Dobriyan 	struct smi_info *smi = m->private;
15951da177e4SLinus Torvalds 
1596910840f2SCorey Minyard 	seq_printf(m, "%s\n", si_to_str[smi->io.si_type]);
1597d6c5dc18SJoe Perches 
15985e33cd0cSJoe Perches 	return 0;
15991da177e4SLinus Torvalds }
16001da177e4SLinus Torvalds 
160107412736SAlexey Dobriyan static int smi_type_proc_open(struct inode *inode, struct file *file)
16021da177e4SLinus Torvalds {
1603d9dda78bSAl Viro 	return single_open(file, smi_type_proc_show, PDE_DATA(inode));
160407412736SAlexey Dobriyan }
16051da177e4SLinus Torvalds 
160607412736SAlexey Dobriyan static const struct file_operations smi_type_proc_ops = {
160707412736SAlexey Dobriyan 	.open		= smi_type_proc_open,
160807412736SAlexey Dobriyan 	.read		= seq_read,
160907412736SAlexey Dobriyan 	.llseek		= seq_lseek,
161007412736SAlexey Dobriyan 	.release	= single_release,
161107412736SAlexey Dobriyan };
161207412736SAlexey Dobriyan 
161307412736SAlexey Dobriyan static int smi_si_stats_proc_show(struct seq_file *m, void *v)
161407412736SAlexey Dobriyan {
161507412736SAlexey Dobriyan 	struct smi_info *smi = m->private;
161607412736SAlexey Dobriyan 
161707412736SAlexey Dobriyan 	seq_printf(m, "interrupts_enabled:    %d\n",
1618910840f2SCorey Minyard 		       smi->io.irq && !smi->interrupt_disabled);
161907412736SAlexey Dobriyan 	seq_printf(m, "short_timeouts:        %u\n",
162064959e2dSCorey Minyard 		       smi_get_stat(smi, short_timeouts));
162107412736SAlexey Dobriyan 	seq_printf(m, "long_timeouts:         %u\n",
162264959e2dSCorey Minyard 		       smi_get_stat(smi, long_timeouts));
162307412736SAlexey Dobriyan 	seq_printf(m, "idles:                 %u\n",
162464959e2dSCorey Minyard 		       smi_get_stat(smi, idles));
162507412736SAlexey Dobriyan 	seq_printf(m, "interrupts:            %u\n",
162664959e2dSCorey Minyard 		       smi_get_stat(smi, interrupts));
162707412736SAlexey Dobriyan 	seq_printf(m, "attentions:            %u\n",
162864959e2dSCorey Minyard 		       smi_get_stat(smi, attentions));
162907412736SAlexey Dobriyan 	seq_printf(m, "flag_fetches:          %u\n",
163064959e2dSCorey Minyard 		       smi_get_stat(smi, flag_fetches));
163107412736SAlexey Dobriyan 	seq_printf(m, "hosed_count:           %u\n",
163264959e2dSCorey Minyard 		       smi_get_stat(smi, hosed_count));
163307412736SAlexey Dobriyan 	seq_printf(m, "complete_transactions: %u\n",
163464959e2dSCorey Minyard 		       smi_get_stat(smi, complete_transactions));
163507412736SAlexey Dobriyan 	seq_printf(m, "events:                %u\n",
163664959e2dSCorey Minyard 		       smi_get_stat(smi, events));
163707412736SAlexey Dobriyan 	seq_printf(m, "watchdog_pretimeouts:  %u\n",
163864959e2dSCorey Minyard 		       smi_get_stat(smi, watchdog_pretimeouts));
163907412736SAlexey Dobriyan 	seq_printf(m, "incoming_messages:     %u\n",
164064959e2dSCorey Minyard 		       smi_get_stat(smi, incoming_messages));
164107412736SAlexey Dobriyan 	return 0;
1642b361e27bSCorey Minyard }
1643b361e27bSCorey Minyard 
164407412736SAlexey Dobriyan static int smi_si_stats_proc_open(struct inode *inode, struct file *file)
1645b361e27bSCorey Minyard {
1646d9dda78bSAl Viro 	return single_open(file, smi_si_stats_proc_show, PDE_DATA(inode));
164707412736SAlexey Dobriyan }
1648b361e27bSCorey Minyard 
164907412736SAlexey Dobriyan static const struct file_operations smi_si_stats_proc_ops = {
165007412736SAlexey Dobriyan 	.open		= smi_si_stats_proc_open,
165107412736SAlexey Dobriyan 	.read		= seq_read,
165207412736SAlexey Dobriyan 	.llseek		= seq_lseek,
165307412736SAlexey Dobriyan 	.release	= single_release,
165407412736SAlexey Dobriyan };
165507412736SAlexey Dobriyan 
165607412736SAlexey Dobriyan static int smi_params_proc_show(struct seq_file *m, void *v)
165707412736SAlexey Dobriyan {
165807412736SAlexey Dobriyan 	struct smi_info *smi = m->private;
165907412736SAlexey Dobriyan 
1660d6c5dc18SJoe Perches 	seq_printf(m,
1661b361e27bSCorey Minyard 		   "%s,%s,0x%lx,rsp=%d,rsi=%d,rsh=%d,irq=%d,ipmb=%d\n",
1662910840f2SCorey Minyard 		   si_to_str[smi->io.si_type],
1663b361e27bSCorey Minyard 		   addr_space_to_str[smi->io.addr_type],
1664b361e27bSCorey Minyard 		   smi->io.addr_data,
1665b361e27bSCorey Minyard 		   smi->io.regspacing,
1666b361e27bSCorey Minyard 		   smi->io.regsize,
1667b361e27bSCorey Minyard 		   smi->io.regshift,
1668910840f2SCorey Minyard 		   smi->io.irq,
1669910840f2SCorey Minyard 		   smi->io.slave_addr);
1670d6c5dc18SJoe Perches 
16715e33cd0cSJoe Perches 	return 0;
16721da177e4SLinus Torvalds }
16731da177e4SLinus Torvalds 
167407412736SAlexey Dobriyan static int smi_params_proc_open(struct inode *inode, struct file *file)
167507412736SAlexey Dobriyan {
1676d9dda78bSAl Viro 	return single_open(file, smi_params_proc_show, PDE_DATA(inode));
167707412736SAlexey Dobriyan }
167807412736SAlexey Dobriyan 
167907412736SAlexey Dobriyan static const struct file_operations smi_params_proc_ops = {
168007412736SAlexey Dobriyan 	.open		= smi_params_proc_open,
168107412736SAlexey Dobriyan 	.read		= seq_read,
168207412736SAlexey Dobriyan 	.llseek		= seq_lseek,
168307412736SAlexey Dobriyan 	.release	= single_release,
168407412736SAlexey Dobriyan };
168555f91cb6SCorey Minyard #endif
168607412736SAlexey Dobriyan 
16873dd377b5SCorey Minyard #define IPMI_SI_ATTR(name) \
16883dd377b5SCorey Minyard static ssize_t ipmi_##name##_show(struct device *dev,			\
16893dd377b5SCorey Minyard 				  struct device_attribute *attr,	\
16903dd377b5SCorey Minyard 				  char *buf)				\
16913dd377b5SCorey Minyard {									\
16923dd377b5SCorey Minyard 	struct smi_info *smi_info = dev_get_drvdata(dev);		\
16933dd377b5SCorey Minyard 									\
16943dd377b5SCorey Minyard 	return snprintf(buf, 10, "%u\n", smi_get_stat(smi_info, name));	\
16953dd377b5SCorey Minyard }									\
16963dd377b5SCorey Minyard static DEVICE_ATTR(name, S_IRUGO, ipmi_##name##_show, NULL)
16973dd377b5SCorey Minyard 
16983dd377b5SCorey Minyard static ssize_t ipmi_type_show(struct device *dev,
16993dd377b5SCorey Minyard 			      struct device_attribute *attr,
17003dd377b5SCorey Minyard 			      char *buf)
17013dd377b5SCorey Minyard {
17023dd377b5SCorey Minyard 	struct smi_info *smi_info = dev_get_drvdata(dev);
17033dd377b5SCorey Minyard 
17043dd377b5SCorey Minyard 	return snprintf(buf, 10, "%s\n", si_to_str[smi_info->io.si_type]);
17053dd377b5SCorey Minyard }
17063dd377b5SCorey Minyard static DEVICE_ATTR(type, S_IRUGO, ipmi_type_show, NULL);
17073dd377b5SCorey Minyard 
17083dd377b5SCorey Minyard static ssize_t ipmi_interrupts_enabled_show(struct device *dev,
17093dd377b5SCorey Minyard 					    struct device_attribute *attr,
17103dd377b5SCorey Minyard 					    char *buf)
17113dd377b5SCorey Minyard {
17123dd377b5SCorey Minyard 	struct smi_info *smi_info = dev_get_drvdata(dev);
17133dd377b5SCorey Minyard 	int enabled = smi_info->io.irq && !smi_info->interrupt_disabled;
17143dd377b5SCorey Minyard 
17153dd377b5SCorey Minyard 	return snprintf(buf, 10, "%d\n", enabled);
17163dd377b5SCorey Minyard }
17173dd377b5SCorey Minyard static DEVICE_ATTR(interrupts_enabled, S_IRUGO,
17183dd377b5SCorey Minyard 		   ipmi_interrupts_enabled_show, NULL);
17193dd377b5SCorey Minyard 
17203dd377b5SCorey Minyard IPMI_SI_ATTR(short_timeouts);
17213dd377b5SCorey Minyard IPMI_SI_ATTR(long_timeouts);
17223dd377b5SCorey Minyard IPMI_SI_ATTR(idles);
17233dd377b5SCorey Minyard IPMI_SI_ATTR(interrupts);
17243dd377b5SCorey Minyard IPMI_SI_ATTR(attentions);
17253dd377b5SCorey Minyard IPMI_SI_ATTR(flag_fetches);
17263dd377b5SCorey Minyard IPMI_SI_ATTR(hosed_count);
17273dd377b5SCorey Minyard IPMI_SI_ATTR(complete_transactions);
17283dd377b5SCorey Minyard IPMI_SI_ATTR(events);
17293dd377b5SCorey Minyard IPMI_SI_ATTR(watchdog_pretimeouts);
17303dd377b5SCorey Minyard IPMI_SI_ATTR(incoming_messages);
17313dd377b5SCorey Minyard 
17323dd377b5SCorey Minyard static ssize_t ipmi_params_show(struct device *dev,
17333dd377b5SCorey Minyard 				struct device_attribute *attr,
17343dd377b5SCorey Minyard 				char *buf)
17353dd377b5SCorey Minyard {
17363dd377b5SCorey Minyard 	struct smi_info *smi_info = dev_get_drvdata(dev);
17373dd377b5SCorey Minyard 
17383dd377b5SCorey Minyard 	return snprintf(buf, 200,
17393dd377b5SCorey Minyard 			"%s,%s,0x%lx,rsp=%d,rsi=%d,rsh=%d,irq=%d,ipmb=%d\n",
17403dd377b5SCorey Minyard 			si_to_str[smi_info->io.si_type],
17413dd377b5SCorey Minyard 			addr_space_to_str[smi_info->io.addr_type],
17423dd377b5SCorey Minyard 			smi_info->io.addr_data,
17433dd377b5SCorey Minyard 			smi_info->io.regspacing,
17443dd377b5SCorey Minyard 			smi_info->io.regsize,
17453dd377b5SCorey Minyard 			smi_info->io.regshift,
17463dd377b5SCorey Minyard 			smi_info->io.irq,
17473dd377b5SCorey Minyard 			smi_info->io.slave_addr);
17483dd377b5SCorey Minyard }
17493dd377b5SCorey Minyard static DEVICE_ATTR(params, S_IRUGO, ipmi_params_show, NULL);
17503dd377b5SCorey Minyard 
17513dd377b5SCorey Minyard static struct attribute *ipmi_si_dev_attrs[] = {
17523dd377b5SCorey Minyard 	&dev_attr_type.attr,
17533dd377b5SCorey Minyard 	&dev_attr_interrupts_enabled.attr,
17543dd377b5SCorey Minyard 	&dev_attr_short_timeouts.attr,
17553dd377b5SCorey Minyard 	&dev_attr_long_timeouts.attr,
17563dd377b5SCorey Minyard 	&dev_attr_idles.attr,
17573dd377b5SCorey Minyard 	&dev_attr_interrupts.attr,
17583dd377b5SCorey Minyard 	&dev_attr_attentions.attr,
17593dd377b5SCorey Minyard 	&dev_attr_flag_fetches.attr,
17603dd377b5SCorey Minyard 	&dev_attr_hosed_count.attr,
17613dd377b5SCorey Minyard 	&dev_attr_complete_transactions.attr,
17623dd377b5SCorey Minyard 	&dev_attr_events.attr,
17633dd377b5SCorey Minyard 	&dev_attr_watchdog_pretimeouts.attr,
17643dd377b5SCorey Minyard 	&dev_attr_incoming_messages.attr,
17653dd377b5SCorey Minyard 	&dev_attr_params.attr,
17663dd377b5SCorey Minyard 	NULL
17673dd377b5SCorey Minyard };
17683dd377b5SCorey Minyard 
17693dd377b5SCorey Minyard static const struct attribute_group ipmi_si_dev_attr_group = {
17703dd377b5SCorey Minyard 	.attrs		= ipmi_si_dev_attrs,
17713dd377b5SCorey Minyard };
17723dd377b5SCorey Minyard 
17733ae0e0f9SCorey Minyard /*
17743ae0e0f9SCorey Minyard  * oem_data_avail_to_receive_msg_avail
17753ae0e0f9SCorey Minyard  * @info - smi_info structure with msg_flags set
17763ae0e0f9SCorey Minyard  *
17773ae0e0f9SCorey Minyard  * Converts flags from OEM_DATA_AVAIL to RECEIVE_MSG_AVAIL
17783ae0e0f9SCorey Minyard  * Returns 1 indicating need to re-run handle_flags().
17793ae0e0f9SCorey Minyard  */
17803ae0e0f9SCorey Minyard static int oem_data_avail_to_receive_msg_avail(struct smi_info *smi_info)
17813ae0e0f9SCorey Minyard {
1782e8b33617SCorey Minyard 	smi_info->msg_flags = ((smi_info->msg_flags & ~OEM_DATA_AVAIL) |
1783e8b33617SCorey Minyard 			       RECEIVE_MSG_AVAIL);
17843ae0e0f9SCorey Minyard 	return 1;
17853ae0e0f9SCorey Minyard }
17863ae0e0f9SCorey Minyard 
17873ae0e0f9SCorey Minyard /*
17883ae0e0f9SCorey Minyard  * setup_dell_poweredge_oem_data_handler
17893ae0e0f9SCorey Minyard  * @info - smi_info.device_id must be populated
17903ae0e0f9SCorey Minyard  *
17913ae0e0f9SCorey Minyard  * Systems that match, but have firmware version < 1.40 may assert
17923ae0e0f9SCorey Minyard  * OEM0_DATA_AVAIL on their own, without being told via Set Flags that
17933ae0e0f9SCorey Minyard  * it's safe to do so.  Such systems will de-assert OEM1_DATA_AVAIL
17943ae0e0f9SCorey Minyard  * upon receipt of IPMI_GET_MSG_CMD, so we should treat these flags
17953ae0e0f9SCorey Minyard  * as RECEIVE_MSG_AVAIL instead.
17963ae0e0f9SCorey Minyard  *
17973ae0e0f9SCorey Minyard  * As Dell has no plans to release IPMI 1.5 firmware that *ever*
17983ae0e0f9SCorey Minyard  * assert the OEM[012] bits, and if it did, the driver would have to
17993ae0e0f9SCorey Minyard  * change to handle that properly, we don't actually check for the
18003ae0e0f9SCorey Minyard  * firmware version.
18013ae0e0f9SCorey Minyard  * Device ID = 0x20                BMC on PowerEdge 8G servers
18023ae0e0f9SCorey Minyard  * Device Revision = 0x80
18033ae0e0f9SCorey Minyard  * Firmware Revision1 = 0x01       BMC version 1.40
18043ae0e0f9SCorey Minyard  * Firmware Revision2 = 0x40       BCD encoded
18053ae0e0f9SCorey Minyard  * IPMI Version = 0x51             IPMI 1.5
18063ae0e0f9SCorey Minyard  * Manufacturer ID = A2 02 00      Dell IANA
18073ae0e0f9SCorey Minyard  *
1808d5a2b89aSCorey Minyard  * Additionally, PowerEdge systems with IPMI < 1.5 may also assert
1809d5a2b89aSCorey Minyard  * OEM0_DATA_AVAIL and needs to be treated as RECEIVE_MSG_AVAIL.
1810d5a2b89aSCorey Minyard  *
18113ae0e0f9SCorey Minyard  */
18123ae0e0f9SCorey Minyard #define DELL_POWEREDGE_8G_BMC_DEVICE_ID  0x20
18133ae0e0f9SCorey Minyard #define DELL_POWEREDGE_8G_BMC_DEVICE_REV 0x80
18143ae0e0f9SCorey Minyard #define DELL_POWEREDGE_8G_BMC_IPMI_VERSION 0x51
181550c812b2SCorey Minyard #define DELL_IANA_MFR_ID 0x0002a2
18163ae0e0f9SCorey Minyard static void setup_dell_poweredge_oem_data_handler(struct smi_info *smi_info)
18173ae0e0f9SCorey Minyard {
18183ae0e0f9SCorey Minyard 	struct ipmi_device_id *id = &smi_info->device_id;
181950c812b2SCorey Minyard 	if (id->manufacturer_id == DELL_IANA_MFR_ID) {
1820d5a2b89aSCorey Minyard 		if (id->device_id       == DELL_POWEREDGE_8G_BMC_DEVICE_ID  &&
1821d5a2b89aSCorey Minyard 		    id->device_revision == DELL_POWEREDGE_8G_BMC_DEVICE_REV &&
1822d5a2b89aSCorey Minyard 		    id->ipmi_version   == DELL_POWEREDGE_8G_BMC_IPMI_VERSION) {
18233ae0e0f9SCorey Minyard 			smi_info->oem_data_avail_handler =
18243ae0e0f9SCorey Minyard 				oem_data_avail_to_receive_msg_avail;
1825c305e3d3SCorey Minyard 		} else if (ipmi_version_major(id) < 1 ||
1826d5a2b89aSCorey Minyard 			   (ipmi_version_major(id) == 1 &&
1827d5a2b89aSCorey Minyard 			    ipmi_version_minor(id) < 5)) {
1828d5a2b89aSCorey Minyard 			smi_info->oem_data_avail_handler =
1829d5a2b89aSCorey Minyard 				oem_data_avail_to_receive_msg_avail;
1830d5a2b89aSCorey Minyard 		}
1831d5a2b89aSCorey Minyard 	}
18323ae0e0f9SCorey Minyard }
18333ae0e0f9SCorey Minyard 
1834ea94027bSCorey Minyard #define CANNOT_RETURN_REQUESTED_LENGTH 0xCA
1835ea94027bSCorey Minyard static void return_hosed_msg_badsize(struct smi_info *smi_info)
1836ea94027bSCorey Minyard {
1837ea94027bSCorey Minyard 	struct ipmi_smi_msg *msg = smi_info->curr_msg;
1838ea94027bSCorey Minyard 
183925985edcSLucas De Marchi 	/* Make it a response */
1840ea94027bSCorey Minyard 	msg->rsp[0] = msg->data[0] | 4;
1841ea94027bSCorey Minyard 	msg->rsp[1] = msg->data[1];
1842ea94027bSCorey Minyard 	msg->rsp[2] = CANNOT_RETURN_REQUESTED_LENGTH;
1843ea94027bSCorey Minyard 	msg->rsp_size = 3;
1844ea94027bSCorey Minyard 	smi_info->curr_msg = NULL;
1845ea94027bSCorey Minyard 	deliver_recv_msg(smi_info, msg);
1846ea94027bSCorey Minyard }
1847ea94027bSCorey Minyard 
1848ea94027bSCorey Minyard /*
1849ea94027bSCorey Minyard  * dell_poweredge_bt_xaction_handler
1850ea94027bSCorey Minyard  * @info - smi_info.device_id must be populated
1851ea94027bSCorey Minyard  *
1852ea94027bSCorey Minyard  * Dell PowerEdge servers with the BT interface (x6xx and 1750) will
1853ea94027bSCorey Minyard  * not respond to a Get SDR command if the length of the data
1854ea94027bSCorey Minyard  * requested is exactly 0x3A, which leads to command timeouts and no
1855ea94027bSCorey Minyard  * data returned.  This intercepts such commands, and causes userspace
1856ea94027bSCorey Minyard  * callers to try again with a different-sized buffer, which succeeds.
1857ea94027bSCorey Minyard  */
1858ea94027bSCorey Minyard 
1859ea94027bSCorey Minyard #define STORAGE_NETFN 0x0A
1860ea94027bSCorey Minyard #define STORAGE_CMD_GET_SDR 0x23
1861ea94027bSCorey Minyard static int dell_poweredge_bt_xaction_handler(struct notifier_block *self,
1862ea94027bSCorey Minyard 					     unsigned long unused,
1863ea94027bSCorey Minyard 					     void *in)
1864ea94027bSCorey Minyard {
1865ea94027bSCorey Minyard 	struct smi_info *smi_info = in;
1866ea94027bSCorey Minyard 	unsigned char *data = smi_info->curr_msg->data;
1867ea94027bSCorey Minyard 	unsigned int size   = smi_info->curr_msg->data_size;
1868ea94027bSCorey Minyard 	if (size >= 8 &&
1869ea94027bSCorey Minyard 	    (data[0]>>2) == STORAGE_NETFN &&
1870ea94027bSCorey Minyard 	    data[1] == STORAGE_CMD_GET_SDR &&
1871ea94027bSCorey Minyard 	    data[7] == 0x3A) {
1872ea94027bSCorey Minyard 		return_hosed_msg_badsize(smi_info);
1873ea94027bSCorey Minyard 		return NOTIFY_STOP;
1874ea94027bSCorey Minyard 	}
1875ea94027bSCorey Minyard 	return NOTIFY_DONE;
1876ea94027bSCorey Minyard }
1877ea94027bSCorey Minyard 
1878ea94027bSCorey Minyard static struct notifier_block dell_poweredge_bt_xaction_notifier = {
1879ea94027bSCorey Minyard 	.notifier_call	= dell_poweredge_bt_xaction_handler,
1880ea94027bSCorey Minyard };
1881ea94027bSCorey Minyard 
1882ea94027bSCorey Minyard /*
1883ea94027bSCorey Minyard  * setup_dell_poweredge_bt_xaction_handler
1884ea94027bSCorey Minyard  * @info - smi_info.device_id must be filled in already
1885ea94027bSCorey Minyard  *
1886ea94027bSCorey Minyard  * Fills in smi_info.device_id.start_transaction_pre_hook
1887ea94027bSCorey Minyard  * when we know what function to use there.
1888ea94027bSCorey Minyard  */
1889ea94027bSCorey Minyard static void
1890ea94027bSCorey Minyard setup_dell_poweredge_bt_xaction_handler(struct smi_info *smi_info)
1891ea94027bSCorey Minyard {
1892ea94027bSCorey Minyard 	struct ipmi_device_id *id = &smi_info->device_id;
189350c812b2SCorey Minyard 	if (id->manufacturer_id == DELL_IANA_MFR_ID &&
1894910840f2SCorey Minyard 	    smi_info->io.si_type == SI_BT)
1895ea94027bSCorey Minyard 		register_xaction_notifier(&dell_poweredge_bt_xaction_notifier);
1896ea94027bSCorey Minyard }
1897ea94027bSCorey Minyard 
18983ae0e0f9SCorey Minyard /*
18993ae0e0f9SCorey Minyard  * setup_oem_data_handler
19003ae0e0f9SCorey Minyard  * @info - smi_info.device_id must be filled in already
19013ae0e0f9SCorey Minyard  *
19023ae0e0f9SCorey Minyard  * Fills in smi_info.device_id.oem_data_available_handler
19033ae0e0f9SCorey Minyard  * when we know what function to use there.
19043ae0e0f9SCorey Minyard  */
19053ae0e0f9SCorey Minyard 
19063ae0e0f9SCorey Minyard static void setup_oem_data_handler(struct smi_info *smi_info)
19073ae0e0f9SCorey Minyard {
19083ae0e0f9SCorey Minyard 	setup_dell_poweredge_oem_data_handler(smi_info);
19093ae0e0f9SCorey Minyard }
19103ae0e0f9SCorey Minyard 
1911ea94027bSCorey Minyard static void setup_xaction_handlers(struct smi_info *smi_info)
1912ea94027bSCorey Minyard {
1913ea94027bSCorey Minyard 	setup_dell_poweredge_bt_xaction_handler(smi_info);
1914ea94027bSCorey Minyard }
1915ea94027bSCorey Minyard 
1916d0882897SCorey Minyard static void check_for_broken_irqs(struct smi_info *smi_info)
1917d0882897SCorey Minyard {
1918d0882897SCorey Minyard 	check_clr_rcv_irq(smi_info);
1919d0882897SCorey Minyard 	check_set_rcv_irq(smi_info);
1920d0882897SCorey Minyard }
1921d0882897SCorey Minyard 
19224f7f5551SMasamitsu Yamazaki static inline void stop_timer_and_thread(struct smi_info *smi_info)
1923a9a2c44fSCorey Minyard {
1924bd1c06a4SMasamitsu Yamazaki 	if (smi_info->thread != NULL) {
1925e9a705a0SMatt Domsch 		kthread_stop(smi_info->thread);
1926bd1c06a4SMasamitsu Yamazaki 		smi_info->thread = NULL;
1927bd1c06a4SMasamitsu Yamazaki 	}
19284f7f5551SMasamitsu Yamazaki 
19294f7f5551SMasamitsu Yamazaki 	smi_info->timer_can_start = false;
1930b874b985SCorey Minyard 	if (smi_info->timer_running)
1931a9a2c44fSCorey Minyard 		del_timer_sync(&smi_info->si_timer);
1932a9a2c44fSCorey Minyard }
1933a9a2c44fSCorey Minyard 
19347e030d6dSCorey Minyard static struct smi_info *find_dup_si(struct smi_info *info)
1935b0defcdbSCorey Minyard {
1936b0defcdbSCorey Minyard 	struct smi_info *e;
1937b0defcdbSCorey Minyard 
1938b0defcdbSCorey Minyard 	list_for_each_entry(e, &smi_infos, link) {
1939b0defcdbSCorey Minyard 		if (e->io.addr_type != info->io.addr_type)
1940b0defcdbSCorey Minyard 			continue;
194194671710SCorey Minyard 		if (e->io.addr_data == info->io.addr_data) {
194294671710SCorey Minyard 			/*
194394671710SCorey Minyard 			 * This is a cheap hack, ACPI doesn't have a defined
194494671710SCorey Minyard 			 * slave address but SMBIOS does.  Pick it up from
194594671710SCorey Minyard 			 * any source that has it available.
194694671710SCorey Minyard 			 */
1947910840f2SCorey Minyard 			if (info->io.slave_addr && !e->io.slave_addr)
1948910840f2SCorey Minyard 				e->io.slave_addr = info->io.slave_addr;
19497e030d6dSCorey Minyard 			return e;
1950b0defcdbSCorey Minyard 		}
195194671710SCorey Minyard 	}
1952b0defcdbSCorey Minyard 
19537e030d6dSCorey Minyard 	return NULL;
1954b0defcdbSCorey Minyard }
1955b0defcdbSCorey Minyard 
1956bb398a4cSCorey Minyard int ipmi_si_add_smi(struct si_sm_io *io)
19572407d77aSMatthew Garrett {
19582407d77aSMatthew Garrett 	int rv = 0;
1959bb398a4cSCorey Minyard 	struct smi_info *new_smi, *dup;
19602407d77aSMatthew Garrett 
1961bb398a4cSCorey Minyard 	if (!io->io_setup) {
1962bb398a4cSCorey Minyard 		if (io->addr_type == IPMI_IO_ADDR_SPACE) {
196358e27635SCorey Minyard 			io->io_setup = ipmi_si_port_setup;
1964bb398a4cSCorey Minyard 		} else if (io->addr_type == IPMI_MEM_ADDR_SPACE) {
196558e27635SCorey Minyard 			io->io_setup = ipmi_si_mem_setup;
1966e1eeb7f8SCorey Minyard 		} else {
1967e1eeb7f8SCorey Minyard 			return -EINVAL;
1968e1eeb7f8SCorey Minyard 		}
1969e1eeb7f8SCorey Minyard 	}
1970e1eeb7f8SCorey Minyard 
197167f4fb02SCorey Minyard 	new_smi = kzalloc(sizeof(*new_smi), GFP_KERNEL);
1972bb398a4cSCorey Minyard 	if (!new_smi)
1973bb398a4cSCorey Minyard 		return -ENOMEM;
197467f4fb02SCorey Minyard 	spin_lock_init(&new_smi->si_lock);
1975bb398a4cSCorey Minyard 
1976bb398a4cSCorey Minyard 	new_smi->io = *io;
1977bb398a4cSCorey Minyard 
19782407d77aSMatthew Garrett 	mutex_lock(&smi_infos_lock);
19797e030d6dSCorey Minyard 	dup = find_dup_si(new_smi);
19807e030d6dSCorey Minyard 	if (dup) {
1981910840f2SCorey Minyard 		if (new_smi->io.addr_source == SI_ACPI &&
1982910840f2SCorey Minyard 		    dup->io.addr_source == SI_SMBIOS) {
19837e030d6dSCorey Minyard 			/* We prefer ACPI over SMBIOS. */
1984910840f2SCorey Minyard 			dev_info(dup->io.dev,
19857e030d6dSCorey Minyard 				 "Removing SMBIOS-specified %s state machine in favor of ACPI\n",
1986910840f2SCorey Minyard 				 si_to_str[new_smi->io.si_type]);
19877e030d6dSCorey Minyard 			cleanup_one_si(dup);
19887e030d6dSCorey Minyard 		} else {
1989910840f2SCorey Minyard 			dev_info(new_smi->io.dev,
19907e030d6dSCorey Minyard 				 "%s-specified %s state machine: duplicate\n",
1991910840f2SCorey Minyard 				 ipmi_addr_src_to_str(new_smi->io.addr_source),
1992910840f2SCorey Minyard 				 si_to_str[new_smi->io.si_type]);
19932407d77aSMatthew Garrett 			rv = -EBUSY;
1994c0a32fe1SColin Ian King 			kfree(new_smi);
19952407d77aSMatthew Garrett 			goto out_err;
19962407d77aSMatthew Garrett 		}
19977e030d6dSCorey Minyard 	}
19982407d77aSMatthew Garrett 
1999bb2a08c0SCorey Minyard 	pr_info(PFX "Adding %s-specified %s state machine\n",
2000910840f2SCorey Minyard 		ipmi_addr_src_to_str(new_smi->io.addr_source),
2001910840f2SCorey Minyard 		si_to_str[new_smi->io.si_type]);
20022407d77aSMatthew Garrett 
20032407d77aSMatthew Garrett 	/* So we know not to free it unless we have allocated one. */
20042407d77aSMatthew Garrett 	new_smi->intf = NULL;
20052407d77aSMatthew Garrett 	new_smi->si_sm = NULL;
20062407d77aSMatthew Garrett 	new_smi->handlers = NULL;
20072407d77aSMatthew Garrett 
20082407d77aSMatthew Garrett 	list_add_tail(&new_smi->link, &smi_infos);
20092407d77aSMatthew Garrett 
2010bb398a4cSCorey Minyard 	if (initialized) {
2011bb398a4cSCorey Minyard 		rv = try_smi_init(new_smi);
2012bb398a4cSCorey Minyard 		if (rv) {
2013bb398a4cSCorey Minyard 			cleanup_one_si(new_smi);
2014*cc095f0aSCorey Minyard 			mutex_unlock(&smi_infos_lock);
2015bb398a4cSCorey Minyard 			return rv;
2016bb398a4cSCorey Minyard 		}
2017bb398a4cSCorey Minyard 	}
20182407d77aSMatthew Garrett out_err:
20192407d77aSMatthew Garrett 	mutex_unlock(&smi_infos_lock);
20202407d77aSMatthew Garrett 	return rv;
20212407d77aSMatthew Garrett }
20222407d77aSMatthew Garrett 
20233f724c40STony Camuso /*
20243f724c40STony Camuso  * Try to start up an interface.  Must be called with smi_infos_lock
20253f724c40STony Camuso  * held, primarily to keep smi_num consistent, we only one to do these
20263f724c40STony Camuso  * one at a time.
20273f724c40STony Camuso  */
2028b0defcdbSCorey Minyard static int try_smi_init(struct smi_info *new_smi)
20291da177e4SLinus Torvalds {
20302407d77aSMatthew Garrett 	int rv = 0;
203164959e2dSCorey Minyard 	int i;
20321abf71eeSCorey Minyard 	char *init_name = NULL;
2033174134acSCorey Minyard 	bool platform_device_registered = false;
20341da177e4SLinus Torvalds 
2035bb2a08c0SCorey Minyard 	pr_info(PFX "Trying %s-specified %s state machine at %s address 0x%lx, slave address 0x%x, irq %d\n",
2036910840f2SCorey Minyard 		ipmi_addr_src_to_str(new_smi->io.addr_source),
2037910840f2SCorey Minyard 		si_to_str[new_smi->io.si_type],
2038b0defcdbSCorey Minyard 		addr_space_to_str[new_smi->io.addr_type],
2039b0defcdbSCorey Minyard 		new_smi->io.addr_data,
2040910840f2SCorey Minyard 		new_smi->io.slave_addr, new_smi->io.irq);
20411da177e4SLinus Torvalds 
2042910840f2SCorey Minyard 	switch (new_smi->io.si_type) {
2043b0defcdbSCorey Minyard 	case SI_KCS:
20441da177e4SLinus Torvalds 		new_smi->handlers = &kcs_smi_handlers;
2045b0defcdbSCorey Minyard 		break;
2046b0defcdbSCorey Minyard 
2047b0defcdbSCorey Minyard 	case SI_SMIC:
20481da177e4SLinus Torvalds 		new_smi->handlers = &smic_smi_handlers;
2049b0defcdbSCorey Minyard 		break;
2050b0defcdbSCorey Minyard 
2051b0defcdbSCorey Minyard 	case SI_BT:
20521da177e4SLinus Torvalds 		new_smi->handlers = &bt_smi_handlers;
2053b0defcdbSCorey Minyard 		break;
2054b0defcdbSCorey Minyard 
2055b0defcdbSCorey Minyard 	default:
20561da177e4SLinus Torvalds 		/* No support for anything else yet. */
20571da177e4SLinus Torvalds 		rv = -EIO;
20581da177e4SLinus Torvalds 		goto out_err;
20591da177e4SLinus Torvalds 	}
20601da177e4SLinus Torvalds 
20613f724c40STony Camuso 	new_smi->intf_num = smi_num;
20623f724c40STony Camuso 
20631abf71eeSCorey Minyard 	/* Do this early so it's available for logs. */
2064910840f2SCorey Minyard 	if (!new_smi->io.dev) {
20653f724c40STony Camuso 		init_name = kasprintf(GFP_KERNEL, "ipmi_si.%d",
20663f724c40STony Camuso 				      new_smi->intf_num);
20671abf71eeSCorey Minyard 
20681abf71eeSCorey Minyard 		/*
20691abf71eeSCorey Minyard 		 * If we don't already have a device from something
20701abf71eeSCorey Minyard 		 * else (like PCI), then register a new one.
20711abf71eeSCorey Minyard 		 */
20721abf71eeSCorey Minyard 		new_smi->pdev = platform_device_alloc("ipmi_si",
20731abf71eeSCorey Minyard 						      new_smi->intf_num);
20741abf71eeSCorey Minyard 		if (!new_smi->pdev) {
20751abf71eeSCorey Minyard 			pr_err(PFX "Unable to allocate platform device\n");
20761abf71eeSCorey Minyard 			goto out_err;
20771abf71eeSCorey Minyard 		}
2078910840f2SCorey Minyard 		new_smi->io.dev = &new_smi->pdev->dev;
20799d70029eSCorey Minyard 		new_smi->io.dev->driver = &ipmi_platform_driver.driver;
20801abf71eeSCorey Minyard 		/* Nulled by device_add() */
2081910840f2SCorey Minyard 		new_smi->io.dev->init_name = init_name;
20821abf71eeSCorey Minyard 	}
20831abf71eeSCorey Minyard 
20841da177e4SLinus Torvalds 	/* Allocate the state machine's data and initialize it. */
20851da177e4SLinus Torvalds 	new_smi->si_sm = kmalloc(new_smi->handlers->size(), GFP_KERNEL);
20861da177e4SLinus Torvalds 	if (!new_smi->si_sm) {
20871da177e4SLinus Torvalds 		rv = -ENOMEM;
20881da177e4SLinus Torvalds 		goto out_err;
20891da177e4SLinus Torvalds 	}
2090e1eeb7f8SCorey Minyard 	new_smi->io.io_size = new_smi->handlers->init_data(new_smi->si_sm,
20911da177e4SLinus Torvalds 							   &new_smi->io);
20921da177e4SLinus Torvalds 
20931da177e4SLinus Torvalds 	/* Now that we know the I/O size, we can set up the I/O. */
2094e1eeb7f8SCorey Minyard 	rv = new_smi->io.io_setup(&new_smi->io);
20951da177e4SLinus Torvalds 	if (rv) {
2096910840f2SCorey Minyard 		dev_err(new_smi->io.dev, "Could not set up I/O space\n");
20971da177e4SLinus Torvalds 		goto out_err;
20981da177e4SLinus Torvalds 	}
20991da177e4SLinus Torvalds 
21001da177e4SLinus Torvalds 	/* Do low-level detection first. */
21011da177e4SLinus Torvalds 	if (new_smi->handlers->detect(new_smi->si_sm)) {
2102910840f2SCorey Minyard 		if (new_smi->io.addr_source)
2103910840f2SCorey Minyard 			dev_err(new_smi->io.dev,
2104910840f2SCorey Minyard 				"Interface detection failed\n");
21051da177e4SLinus Torvalds 		rv = -ENODEV;
21061da177e4SLinus Torvalds 		goto out_err;
21071da177e4SLinus Torvalds 	}
21081da177e4SLinus Torvalds 
2109c305e3d3SCorey Minyard 	/*
2110c305e3d3SCorey Minyard 	 * Attempt a get device id command.  If it fails, we probably
2111c305e3d3SCorey Minyard 	 * don't have a BMC here.
2112c305e3d3SCorey Minyard 	 */
21131da177e4SLinus Torvalds 	rv = try_get_dev_id(new_smi);
2114b0defcdbSCorey Minyard 	if (rv) {
2115910840f2SCorey Minyard 		if (new_smi->io.addr_source)
2116910840f2SCorey Minyard 			dev_err(new_smi->io.dev,
2117910840f2SCorey Minyard 			       "There appears to be no BMC at this location\n");
21181da177e4SLinus Torvalds 		goto out_err;
2119b0defcdbSCorey Minyard 	}
21201da177e4SLinus Torvalds 
21213ae0e0f9SCorey Minyard 	setup_oem_data_handler(new_smi);
2122ea94027bSCorey Minyard 	setup_xaction_handlers(new_smi);
2123d0882897SCorey Minyard 	check_for_broken_irqs(new_smi);
21243ae0e0f9SCorey Minyard 
2125b874b985SCorey Minyard 	new_smi->waiting_msg = NULL;
21261da177e4SLinus Torvalds 	new_smi->curr_msg = NULL;
21271da177e4SLinus Torvalds 	atomic_set(&new_smi->req_events, 0);
21287aefac26SCorey Minyard 	new_smi->run_to_completion = false;
212964959e2dSCorey Minyard 	for (i = 0; i < SI_NUM_STATS; i++)
213064959e2dSCorey Minyard 		atomic_set(&new_smi->stats[i], 0);
21311da177e4SLinus Torvalds 
21327aefac26SCorey Minyard 	new_smi->interrupt_disabled = true;
213389986496SCorey Minyard 	atomic_set(&new_smi->need_watch, 0);
21341da177e4SLinus Torvalds 
213540112ae7SCorey Minyard 	rv = try_enable_event_buffer(new_smi);
213640112ae7SCorey Minyard 	if (rv == 0)
21377aefac26SCorey Minyard 		new_smi->has_event_buffer = true;
213840112ae7SCorey Minyard 
2139c305e3d3SCorey Minyard 	/*
2140c305e3d3SCorey Minyard 	 * Start clearing the flags before we enable interrupts or the
2141c305e3d3SCorey Minyard 	 * timer to avoid racing with the timer.
2142c305e3d3SCorey Minyard 	 */
21434f7f5551SMasamitsu Yamazaki 	start_clear_flags(new_smi);
2144d9b7e4f7SCorey Minyard 
2145d9b7e4f7SCorey Minyard 	/*
2146d9b7e4f7SCorey Minyard 	 * IRQ is defined to be set when non-zero.  req_events will
2147d9b7e4f7SCorey Minyard 	 * cause a global flags check that will enable interrupts.
2148d9b7e4f7SCorey Minyard 	 */
2149910840f2SCorey Minyard 	if (new_smi->io.irq) {
2150d9b7e4f7SCorey Minyard 		new_smi->interrupt_disabled = false;
2151d9b7e4f7SCorey Minyard 		atomic_set(&new_smi->req_events, 1);
2152d9b7e4f7SCorey Minyard 	}
21531da177e4SLinus Torvalds 
21541abf71eeSCorey Minyard 	if (new_smi->pdev) {
2155b48f5457SZhang, Yanmin 		rv = platform_device_add(new_smi->pdev);
215650c812b2SCorey Minyard 		if (rv) {
2157910840f2SCorey Minyard 			dev_err(new_smi->io.dev,
2158bb2a08c0SCorey Minyard 				"Unable to register system interface device: %d\n",
215950c812b2SCorey Minyard 				rv);
2160453823baSCorey Minyard 			goto out_err;
216150c812b2SCorey Minyard 		}
2162174134acSCorey Minyard 		platform_device_registered = true;
216350c812b2SCorey Minyard 	}
216450c812b2SCorey Minyard 
21653dd377b5SCorey Minyard 	dev_set_drvdata(new_smi->io.dev, new_smi);
21663dd377b5SCorey Minyard 	rv = device_add_group(new_smi->io.dev, &ipmi_si_dev_attr_group);
21673dd377b5SCorey Minyard 	if (rv) {
21683dd377b5SCorey Minyard 		dev_err(new_smi->io.dev,
21693dd377b5SCorey Minyard 			"Unable to add device attributes: error %d\n",
21703dd377b5SCorey Minyard 			rv);
21713dd377b5SCorey Minyard 		goto out_err_stop_timer;
21723dd377b5SCorey Minyard 	}
2173*cc095f0aSCorey Minyard 	new_smi->dev_group_added = true;
21743dd377b5SCorey Minyard 
21751da177e4SLinus Torvalds 	rv = ipmi_register_smi(&handlers,
21761da177e4SLinus Torvalds 			       new_smi,
2177910840f2SCorey Minyard 			       new_smi->io.dev,
2178910840f2SCorey Minyard 			       new_smi->io.slave_addr);
21791da177e4SLinus Torvalds 	if (rv) {
2180910840f2SCorey Minyard 		dev_err(new_smi->io.dev,
2181910840f2SCorey Minyard 			"Unable to register device: error %d\n",
21821da177e4SLinus Torvalds 			rv);
21833dd377b5SCorey Minyard 		goto out_err_remove_attrs;
21841da177e4SLinus Torvalds 	}
21851da177e4SLinus Torvalds 
218655f91cb6SCorey Minyard #ifdef CONFIG_IPMI_PROC_INTERFACE
21871da177e4SLinus Torvalds 	rv = ipmi_smi_add_proc_entry(new_smi->intf, "type",
218807412736SAlexey Dobriyan 				     &smi_type_proc_ops,
218999b76233SAlexey Dobriyan 				     new_smi);
21901da177e4SLinus Torvalds 	if (rv) {
2191910840f2SCorey Minyard 		dev_err(new_smi->io.dev,
2192910840f2SCorey Minyard 			"Unable to create proc entry: %d\n", rv);
21931da177e4SLinus Torvalds 		goto out_err_stop_timer;
21941da177e4SLinus Torvalds 	}
21951da177e4SLinus Torvalds 
21961da177e4SLinus Torvalds 	rv = ipmi_smi_add_proc_entry(new_smi->intf, "si_stats",
219707412736SAlexey Dobriyan 				     &smi_si_stats_proc_ops,
219899b76233SAlexey Dobriyan 				     new_smi);
21991da177e4SLinus Torvalds 	if (rv) {
2200910840f2SCorey Minyard 		dev_err(new_smi->io.dev,
2201910840f2SCorey Minyard 			"Unable to create proc entry: %d\n", rv);
22021da177e4SLinus Torvalds 		goto out_err_stop_timer;
22031da177e4SLinus Torvalds 	}
22041da177e4SLinus Torvalds 
2205b361e27bSCorey Minyard 	rv = ipmi_smi_add_proc_entry(new_smi->intf, "params",
220607412736SAlexey Dobriyan 				     &smi_params_proc_ops,
220799b76233SAlexey Dobriyan 				     new_smi);
2208b361e27bSCorey Minyard 	if (rv) {
2209910840f2SCorey Minyard 		dev_err(new_smi->io.dev,
2210910840f2SCorey Minyard 			"Unable to create proc entry: %d\n", rv);
2211b361e27bSCorey Minyard 		goto out_err_stop_timer;
2212b361e27bSCorey Minyard 	}
221355f91cb6SCorey Minyard #endif
2214b361e27bSCorey Minyard 
22153f724c40STony Camuso 	/* Don't increment till we know we have succeeded. */
22163f724c40STony Camuso 	smi_num++;
22173f724c40STony Camuso 
2218910840f2SCorey Minyard 	dev_info(new_smi->io.dev, "IPMI %s interface initialized\n",
2219910840f2SCorey Minyard 		 si_to_str[new_smi->io.si_type]);
22201da177e4SLinus Torvalds 
2221910840f2SCorey Minyard 	WARN_ON(new_smi->io.dev->init_name != NULL);
22221abf71eeSCorey Minyard 	kfree(init_name);
22231abf71eeSCorey Minyard 
22241da177e4SLinus Torvalds 	return 0;
22251da177e4SLinus Torvalds 
22263dd377b5SCorey Minyard out_err_remove_attrs:
2227*cc095f0aSCorey Minyard 	if (new_smi->dev_group_added) {
22283dd377b5SCorey Minyard 		device_remove_group(new_smi->io.dev, &ipmi_si_dev_attr_group);
2229*cc095f0aSCorey Minyard 		new_smi->dev_group_added = false;
2230*cc095f0aSCorey Minyard 	}
22313dd377b5SCorey Minyard 	dev_set_drvdata(new_smi->io.dev, NULL);
22323dd377b5SCorey Minyard 
22331da177e4SLinus Torvalds out_err_stop_timer:
22344f7f5551SMasamitsu Yamazaki 	stop_timer_and_thread(new_smi);
22351da177e4SLinus Torvalds 
22361da177e4SLinus Torvalds out_err:
22377aefac26SCorey Minyard 	new_smi->interrupt_disabled = true;
22381da177e4SLinus Torvalds 
22392407d77aSMatthew Garrett 	if (new_smi->intf) {
2240b874b985SCorey Minyard 		ipmi_smi_t intf = new_smi->intf;
22412407d77aSMatthew Garrett 		new_smi->intf = NULL;
2242b874b985SCorey Minyard 		ipmi_unregister_smi(intf);
22432407d77aSMatthew Garrett 	}
22442407d77aSMatthew Garrett 
22454f3e8199SCorey Minyard 	if (new_smi->io.irq_cleanup) {
22464f3e8199SCorey Minyard 		new_smi->io.irq_cleanup(&new_smi->io);
22474f3e8199SCorey Minyard 		new_smi->io.irq_cleanup = NULL;
22482407d77aSMatthew Garrett 	}
22491da177e4SLinus Torvalds 
2250c305e3d3SCorey Minyard 	/*
2251c305e3d3SCorey Minyard 	 * Wait until we know that we are out of any interrupt
2252c305e3d3SCorey Minyard 	 * handlers might have been running before we freed the
2253c305e3d3SCorey Minyard 	 * interrupt.
2254c305e3d3SCorey Minyard 	 */
2255fbd568a3SPaul E. McKenney 	synchronize_sched();
22561da177e4SLinus Torvalds 
22571da177e4SLinus Torvalds 	if (new_smi->si_sm) {
22581da177e4SLinus Torvalds 		if (new_smi->handlers)
22591da177e4SLinus Torvalds 			new_smi->handlers->cleanup(new_smi->si_sm);
22601da177e4SLinus Torvalds 		kfree(new_smi->si_sm);
22612407d77aSMatthew Garrett 		new_smi->si_sm = NULL;
22621da177e4SLinus Torvalds 	}
2263910840f2SCorey Minyard 	if (new_smi->io.addr_source_cleanup) {
2264910840f2SCorey Minyard 		new_smi->io.addr_source_cleanup(&new_smi->io);
2265910840f2SCorey Minyard 		new_smi->io.addr_source_cleanup = NULL;
22662407d77aSMatthew Garrett 	}
2267e1eeb7f8SCorey Minyard 	if (new_smi->io.io_cleanup) {
2268e1eeb7f8SCorey Minyard 		new_smi->io.io_cleanup(&new_smi->io);
2269e1eeb7f8SCorey Minyard 		new_smi->io.io_cleanup = NULL;
22702407d77aSMatthew Garrett 	}
22711da177e4SLinus Torvalds 
2272910840f2SCorey Minyard 	if (new_smi->pdev) {
2273174134acSCorey Minyard 		if (platform_device_registered)
227450c812b2SCorey Minyard 			platform_device_unregister(new_smi->pdev);
2275174134acSCorey Minyard 		else
22761abf71eeSCorey Minyard 			platform_device_put(new_smi->pdev);
2277174134acSCorey Minyard 		new_smi->pdev = NULL;
2278*cc095f0aSCorey Minyard 		new_smi->io.dev = NULL;
22792407d77aSMatthew Garrett 	}
2280b0defcdbSCorey Minyard 
22811abf71eeSCorey Minyard 	kfree(init_name);
22821abf71eeSCorey Minyard 
22831da177e4SLinus Torvalds 	return rv;
22841da177e4SLinus Torvalds }
22851da177e4SLinus Torvalds 
22862223cbecSBill Pemberton static int init_ipmi_si(void)
22871da177e4SLinus Torvalds {
22882407d77aSMatthew Garrett 	struct smi_info *e;
228906ee4594SMatthew Garrett 	enum ipmi_addr_src type = SI_INVALID;
22901da177e4SLinus Torvalds 
22911da177e4SLinus Torvalds 	if (initialized)
22921da177e4SLinus Torvalds 		return 0;
22931da177e4SLinus Torvalds 
2294bb2a08c0SCorey Minyard 	pr_info("IPMI System Interface driver.\n");
22951da177e4SLinus Torvalds 
2296d8cc5267SMatthew Garrett 	/* If the user gave us a device, they presumably want us to use it */
22977a453308SCorey Minyard 	if (!ipmi_si_hardcode_find_bmc())
22987a453308SCorey Minyard 		goto do_scan;
2299d8cc5267SMatthew Garrett 
23009d70029eSCorey Minyard 	ipmi_si_platform_init();
23019d70029eSCorey Minyard 
230213d0b35cSCorey Minyard 	ipmi_si_pci_init();
2303b0defcdbSCorey Minyard 
2304c6f85a75SCorey Minyard 	ipmi_si_parisc_init();
2305fdbeb7deSThomas Bogendoerfer 
230606ee4594SMatthew Garrett 	/* We prefer devices with interrupts, but in the case of a machine
230706ee4594SMatthew Garrett 	   with multiple BMCs we assume that there will be several instances
230806ee4594SMatthew Garrett 	   of a given type so if we succeed in registering a type then also
230906ee4594SMatthew Garrett 	   try to register everything else of the same type */
23107a453308SCorey Minyard do_scan:
23112407d77aSMatthew Garrett 	mutex_lock(&smi_infos_lock);
23122407d77aSMatthew Garrett 	list_for_each_entry(e, &smi_infos, link) {
231306ee4594SMatthew Garrett 		/* Try to register a device if it has an IRQ and we either
231406ee4594SMatthew Garrett 		   haven't successfully registered a device yet or this
231506ee4594SMatthew Garrett 		   device has the same type as one we successfully registered */
2316910840f2SCorey Minyard 		if (e->io.irq && (!type || e->io.addr_source == type)) {
2317d8cc5267SMatthew Garrett 			if (!try_smi_init(e)) {
2318910840f2SCorey Minyard 				type = e->io.addr_source;
231906ee4594SMatthew Garrett 			}
232006ee4594SMatthew Garrett 		}
232106ee4594SMatthew Garrett 	}
232206ee4594SMatthew Garrett 
232306ee4594SMatthew Garrett 	/* type will only have been set if we successfully registered an si */
2324bb398a4cSCorey Minyard 	if (type)
2325bb398a4cSCorey Minyard 		goto skip_fallback_noirq;
2326d8cc5267SMatthew Garrett 
2327d8cc5267SMatthew Garrett 	/* Fall back to the preferred device */
2328d8cc5267SMatthew Garrett 
2329d8cc5267SMatthew Garrett 	list_for_each_entry(e, &smi_infos, link) {
2330910840f2SCorey Minyard 		if (!e->io.irq && (!type || e->io.addr_source == type)) {
2331d8cc5267SMatthew Garrett 			if (!try_smi_init(e)) {
2332910840f2SCorey Minyard 				type = e->io.addr_source;
233306ee4594SMatthew Garrett 			}
233406ee4594SMatthew Garrett 		}
233506ee4594SMatthew Garrett 	}
2336bb398a4cSCorey Minyard 
2337bb398a4cSCorey Minyard skip_fallback_noirq:
2338bb398a4cSCorey Minyard 	initialized = 1;
2339d8cc5267SMatthew Garrett 	mutex_unlock(&smi_infos_lock);
234006ee4594SMatthew Garrett 
234106ee4594SMatthew Garrett 	if (type)
2342d8cc5267SMatthew Garrett 		return 0;
23432407d77aSMatthew Garrett 
2344d6dfd131SCorey Minyard 	mutex_lock(&smi_infos_lock);
2345b361e27bSCorey Minyard 	if (unload_when_empty && list_empty(&smi_infos)) {
2346d6dfd131SCorey Minyard 		mutex_unlock(&smi_infos_lock);
2347d2478521SCorey Minyard 		cleanup_ipmi_si();
2348bb2a08c0SCorey Minyard 		pr_warn(PFX "Unable to find any System Interface(s)\n");
23491da177e4SLinus Torvalds 		return -ENODEV;
2350b0defcdbSCorey Minyard 	} else {
2351d6dfd131SCorey Minyard 		mutex_unlock(&smi_infos_lock);
23521da177e4SLinus Torvalds 		return 0;
23531da177e4SLinus Torvalds 	}
2354b0defcdbSCorey Minyard }
23551da177e4SLinus Torvalds module_init(init_ipmi_si);
23561da177e4SLinus Torvalds 
2357b361e27bSCorey Minyard static void cleanup_one_si(struct smi_info *to_clean)
23581da177e4SLinus Torvalds {
23592407d77aSMatthew Garrett 	int           rv = 0;
23601da177e4SLinus Torvalds 
23611da177e4SLinus Torvalds 	if (!to_clean)
23621da177e4SLinus Torvalds 		return;
23631da177e4SLinus Torvalds 
2364b874b985SCorey Minyard 	if (to_clean->intf) {
2365b874b985SCorey Minyard 		ipmi_smi_t intf = to_clean->intf;
2366b874b985SCorey Minyard 
2367b874b985SCorey Minyard 		to_clean->intf = NULL;
2368b874b985SCorey Minyard 		rv = ipmi_unregister_smi(intf);
2369b874b985SCorey Minyard 		if (rv) {
2370b874b985SCorey Minyard 			pr_err(PFX "Unable to unregister device: errno=%d\n",
2371b874b985SCorey Minyard 			       rv);
2372b874b985SCorey Minyard 		}
2373b874b985SCorey Minyard 	}
2374b874b985SCorey Minyard 
2375*cc095f0aSCorey Minyard 	if (to_clean->dev_group_added)
23763dd377b5SCorey Minyard 		device_remove_group(to_clean->io.dev, &ipmi_si_dev_attr_group);
2377*cc095f0aSCorey Minyard 	if (to_clean->io.dev)
23783dd377b5SCorey Minyard 		dev_set_drvdata(to_clean->io.dev, NULL);
23793dd377b5SCorey Minyard 
2380b0defcdbSCorey Minyard 	list_del(&to_clean->link);
2381b0defcdbSCorey Minyard 
2382c305e3d3SCorey Minyard 	/*
2383b874b985SCorey Minyard 	 * Make sure that interrupts, the timer and the thread are
2384b874b985SCorey Minyard 	 * stopped and will not run again.
2385c305e3d3SCorey Minyard 	 */
23864f3e8199SCorey Minyard 	if (to_clean->io.irq_cleanup)
23874f3e8199SCorey Minyard 		to_clean->io.irq_cleanup(&to_clean->io);
23884f7f5551SMasamitsu Yamazaki 	stop_timer_and_thread(to_clean);
23891da177e4SLinus Torvalds 
2390c305e3d3SCorey Minyard 	/*
2391c305e3d3SCorey Minyard 	 * Timeouts are stopped, now make sure the interrupts are off
2392b874b985SCorey Minyard 	 * in the BMC.  Note that timers and CPU interrupts are off,
2393b874b985SCorey Minyard 	 * so no need for locks.
2394c305e3d3SCorey Minyard 	 */
2395ee6cd5f8SCorey Minyard 	while (to_clean->curr_msg || (to_clean->si_state != SI_NORMAL)) {
2396ee6cd5f8SCorey Minyard 		poll(to_clean);
2397ee6cd5f8SCorey Minyard 		schedule_timeout_uninterruptible(1);
2398ee6cd5f8SCorey Minyard 	}
23997e030d6dSCorey Minyard 	if (to_clean->handlers)
24004f7f5551SMasamitsu Yamazaki 		disable_si_irq(to_clean);
2401ee6cd5f8SCorey Minyard 	while (to_clean->curr_msg || (to_clean->si_state != SI_NORMAL)) {
2402ee6cd5f8SCorey Minyard 		poll(to_clean);
2403ee6cd5f8SCorey Minyard 		schedule_timeout_uninterruptible(1);
2404ee6cd5f8SCorey Minyard 	}
2405ee6cd5f8SCorey Minyard 
24062407d77aSMatthew Garrett 	if (to_clean->handlers)
24071da177e4SLinus Torvalds 		to_clean->handlers->cleanup(to_clean->si_sm);
24081da177e4SLinus Torvalds 
24091da177e4SLinus Torvalds 	kfree(to_clean->si_sm);
24101da177e4SLinus Torvalds 
2411910840f2SCorey Minyard 	if (to_clean->io.addr_source_cleanup)
2412910840f2SCorey Minyard 		to_clean->io.addr_source_cleanup(&to_clean->io);
2413e1eeb7f8SCorey Minyard 	if (to_clean->io.io_cleanup)
2414e1eeb7f8SCorey Minyard 		to_clean->io.io_cleanup(&to_clean->io);
241550c812b2SCorey Minyard 
2416910840f2SCorey Minyard 	if (to_clean->pdev)
241750c812b2SCorey Minyard 		platform_device_unregister(to_clean->pdev);
241850c812b2SCorey Minyard 
241950c812b2SCorey Minyard 	kfree(to_clean);
24201da177e4SLinus Torvalds }
24211da177e4SLinus Torvalds 
2422bb398a4cSCorey Minyard int ipmi_si_remove_by_dev(struct device *dev)
2423bb398a4cSCorey Minyard {
2424bb398a4cSCorey Minyard 	struct smi_info *e;
2425bb398a4cSCorey Minyard 	int rv = -ENOENT;
2426bb398a4cSCorey Minyard 
2427bb398a4cSCorey Minyard 	mutex_lock(&smi_infos_lock);
2428bb398a4cSCorey Minyard 	list_for_each_entry(e, &smi_infos, link) {
2429bb398a4cSCorey Minyard 		if (e->io.dev == dev) {
2430bb398a4cSCorey Minyard 			cleanup_one_si(e);
2431bb398a4cSCorey Minyard 			rv = 0;
2432bb398a4cSCorey Minyard 			break;
2433bb398a4cSCorey Minyard 		}
2434bb398a4cSCorey Minyard 	}
2435bb398a4cSCorey Minyard 	mutex_unlock(&smi_infos_lock);
2436bb398a4cSCorey Minyard 
2437bb398a4cSCorey Minyard 	return rv;
2438bb398a4cSCorey Minyard }
2439bb398a4cSCorey Minyard 
244044814ec9SCorey Minyard void ipmi_si_remove_by_data(int addr_space, enum si_type si_type,
244144814ec9SCorey Minyard 			    unsigned long addr)
244244814ec9SCorey Minyard {
244344814ec9SCorey Minyard 	/* remove */
244444814ec9SCorey Minyard 	struct smi_info *e, *tmp_e;
244544814ec9SCorey Minyard 
244644814ec9SCorey Minyard 	mutex_lock(&smi_infos_lock);
244744814ec9SCorey Minyard 	list_for_each_entry_safe(e, tmp_e, &smi_infos, link) {
244844814ec9SCorey Minyard 		if (e->io.addr_type != addr_space)
244944814ec9SCorey Minyard 			continue;
245044814ec9SCorey Minyard 		if (e->io.si_type != si_type)
245144814ec9SCorey Minyard 			continue;
245244814ec9SCorey Minyard 		if (e->io.addr_data == addr)
245344814ec9SCorey Minyard 			cleanup_one_si(e);
245444814ec9SCorey Minyard 	}
245544814ec9SCorey Minyard 	mutex_unlock(&smi_infos_lock);
245644814ec9SCorey Minyard }
245744814ec9SCorey Minyard 
24580dcf334cSSergey Senozhatsky static void cleanup_ipmi_si(void)
24591da177e4SLinus Torvalds {
2460b0defcdbSCorey Minyard 	struct smi_info *e, *tmp_e;
24611da177e4SLinus Torvalds 
24621da177e4SLinus Torvalds 	if (!initialized)
24631da177e4SLinus Torvalds 		return;
24641da177e4SLinus Torvalds 
246513d0b35cSCorey Minyard 	ipmi_si_pci_shutdown();
2466c6f85a75SCorey Minyard 
2467c6f85a75SCorey Minyard 	ipmi_si_parisc_shutdown();
2468b0defcdbSCorey Minyard 
24699d70029eSCorey Minyard 	ipmi_si_platform_shutdown();
2470dba9b4f6SCorey Minyard 
2471d6dfd131SCorey Minyard 	mutex_lock(&smi_infos_lock);
2472b0defcdbSCorey Minyard 	list_for_each_entry_safe(e, tmp_e, &smi_infos, link)
2473b0defcdbSCorey Minyard 		cleanup_one_si(e);
2474d6dfd131SCorey Minyard 	mutex_unlock(&smi_infos_lock);
24751da177e4SLinus Torvalds }
24761da177e4SLinus Torvalds module_exit(cleanup_ipmi_si);
24771da177e4SLinus Torvalds 
24780944d889SCorey Minyard MODULE_ALIAS("platform:dmi-ipmi-si");
24791da177e4SLinus Torvalds MODULE_LICENSE("GPL");
24801fdd75bdSCorey Minyard MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
2481c305e3d3SCorey Minyard MODULE_DESCRIPTION("Interface to the IPMI driver for the KCS, SMIC, and BT"
2482c305e3d3SCorey Minyard 		   " system interfaces.");
2483