xref: /openbmc/linux/drivers/char/ipmi/ipmi_si_intf.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1243ac210SCorey Minyard // SPDX-License-Identifier: GPL-2.0+
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * ipmi_si.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * The interface to the IPMI driver for the system interfaces (KCS, SMIC,
61da177e4SLinus Torvalds  * BT).
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * Author: MontaVista Software, Inc.
91da177e4SLinus Torvalds  *         Corey Minyard <minyard@mvista.com>
101da177e4SLinus Torvalds  *         source@mvista.com
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  * Copyright 2002 MontaVista Software Inc.
13dba9b4f6SCorey Minyard  * Copyright 2006 IBM Corp., Christian Krafft <krafft@de.ibm.com>
141da177e4SLinus Torvalds  */
151da177e4SLinus Torvalds 
161da177e4SLinus Torvalds /*
171da177e4SLinus Torvalds  * This file holds the "policy" for the interface to the SMI state
181da177e4SLinus Torvalds  * machine.  It does the configuration, handles timers and interrupts,
191da177e4SLinus Torvalds  * and drives the real SMI state machine.
201da177e4SLinus Torvalds  */
211da177e4SLinus Torvalds 
2225880f7dSJoe Perches #define pr_fmt(fmt) "ipmi_si: " fmt
2325880f7dSJoe Perches 
241da177e4SLinus Torvalds #include <linux/module.h>
251da177e4SLinus Torvalds #include <linux/moduleparam.h>
261da177e4SLinus Torvalds #include <linux/sched.h>
2707412736SAlexey Dobriyan #include <linux/seq_file.h>
281da177e4SLinus Torvalds #include <linux/timer.h>
291da177e4SLinus Torvalds #include <linux/errno.h>
301da177e4SLinus Torvalds #include <linux/spinlock.h>
311da177e4SLinus Torvalds #include <linux/slab.h>
321da177e4SLinus Torvalds #include <linux/delay.h>
331da177e4SLinus Torvalds #include <linux/list.h>
34ea94027bSCorey Minyard #include <linux/notifier.h>
35b0defcdbSCorey Minyard #include <linux/mutex.h>
36e9a705a0SMatt Domsch #include <linux/kthread.h>
371da177e4SLinus Torvalds #include <asm/irq.h>
381da177e4SLinus Torvalds #include <linux/interrupt.h>
391da177e4SLinus Torvalds #include <linux/rcupdate.h>
4016f4232cSZhao Yakui #include <linux/ipmi.h>
411da177e4SLinus Torvalds #include <linux/ipmi_smi.h>
421e89a499SCorey Minyard #include "ipmi_si.h"
43104fb25fSCorey Minyard #include "ipmi_si_sm.h"
44b361e27bSCorey Minyard #include <linux/string.h>
45b361e27bSCorey Minyard #include <linux/ctype.h>
46dba9b4f6SCorey Minyard 
471da177e4SLinus Torvalds /* Measure times between events in the driver. */
481da177e4SLinus Torvalds #undef DEBUG_TIMING
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds /* Call every 10 ms. */
511da177e4SLinus Torvalds #define SI_TIMEOUT_TIME_USEC	10000
521da177e4SLinus Torvalds #define SI_USEC_PER_JIFFY	(1000000/HZ)
531da177e4SLinus Torvalds #define SI_TIMEOUT_JIFFIES	(SI_TIMEOUT_TIME_USEC/SI_USEC_PER_JIFFY)
541da177e4SLinus Torvalds #define SI_SHORT_TIMEOUT_USEC  250 /* .25ms when the SM request a
551da177e4SLinus Torvalds 				      short timeout */
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds enum si_intf_state {
581da177e4SLinus Torvalds 	SI_NORMAL,
591da177e4SLinus Torvalds 	SI_GETTING_FLAGS,
601da177e4SLinus Torvalds 	SI_GETTING_EVENTS,
611da177e4SLinus Torvalds 	SI_CLEARING_FLAGS,
621da177e4SLinus Torvalds 	SI_GETTING_MESSAGES,
63d9b7e4f7SCorey Minyard 	SI_CHECKING_ENABLES,
64d9b7e4f7SCorey Minyard 	SI_SETTING_ENABLES
651da177e4SLinus Torvalds 	/* FIXME - add watchdog stuff. */
661da177e4SLinus Torvalds };
671da177e4SLinus Torvalds 
689dbf68f9SCorey Minyard /* Some BT-specific defines we need here. */
699dbf68f9SCorey Minyard #define IPMI_BT_INTMASK_REG		2
709dbf68f9SCorey Minyard #define IPMI_BT_INTMASK_CLEAR_IRQ_BIT	2
719dbf68f9SCorey Minyard #define IPMI_BT_INTMASK_ENABLE_IRQ_BIT	1
729dbf68f9SCorey Minyard 
7359cdb2e7SAndy Shevchenko /* 'invalid' to allow a firmware-specified interface to be disabled */
7459cdb2e7SAndy Shevchenko const char *const si_to_str[] = { "invalid", "kcs", "smic", "bt", NULL };
751da177e4SLinus Torvalds 
76dd7450caSKefeng Wang static bool initialized;
77bb398a4cSCorey Minyard 
7864959e2dSCorey Minyard /*
7964959e2dSCorey Minyard  * Indexes into stats[] in smi_info below.
8064959e2dSCorey Minyard  */
81ba8ff1c6SCorey Minyard enum si_stat_indexes {
82ba8ff1c6SCorey Minyard 	/*
83ba8ff1c6SCorey Minyard 	 * Number of times the driver requested a timer while an operation
84ba8ff1c6SCorey Minyard 	 * was in progress.
85ba8ff1c6SCorey Minyard 	 */
86ba8ff1c6SCorey Minyard 	SI_STAT_short_timeouts = 0,
8764959e2dSCorey Minyard 
88ba8ff1c6SCorey Minyard 	/*
89ba8ff1c6SCorey Minyard 	 * Number of times the driver requested a timer while nothing was in
90ba8ff1c6SCorey Minyard 	 * progress.
91ba8ff1c6SCorey Minyard 	 */
92ba8ff1c6SCorey Minyard 	SI_STAT_long_timeouts,
9364959e2dSCorey Minyard 
94ba8ff1c6SCorey Minyard 	/* Number of times the interface was idle while being polled. */
95ba8ff1c6SCorey Minyard 	SI_STAT_idles,
96ba8ff1c6SCorey Minyard 
97ba8ff1c6SCorey Minyard 	/* Number of interrupts the driver handled. */
98ba8ff1c6SCorey Minyard 	SI_STAT_interrupts,
99ba8ff1c6SCorey Minyard 
100ba8ff1c6SCorey Minyard 	/* Number of time the driver got an ATTN from the hardware. */
101ba8ff1c6SCorey Minyard 	SI_STAT_attentions,
102ba8ff1c6SCorey Minyard 
103ba8ff1c6SCorey Minyard 	/* Number of times the driver requested flags from the hardware. */
104ba8ff1c6SCorey Minyard 	SI_STAT_flag_fetches,
105ba8ff1c6SCorey Minyard 
106ba8ff1c6SCorey Minyard 	/* Number of times the hardware didn't follow the state machine. */
107ba8ff1c6SCorey Minyard 	SI_STAT_hosed_count,
108ba8ff1c6SCorey Minyard 
109ba8ff1c6SCorey Minyard 	/* Number of completed messages. */
110ba8ff1c6SCorey Minyard 	SI_STAT_complete_transactions,
111ba8ff1c6SCorey Minyard 
112ba8ff1c6SCorey Minyard 	/* Number of IPMI events received from the hardware. */
113ba8ff1c6SCorey Minyard 	SI_STAT_events,
114ba8ff1c6SCorey Minyard 
115ba8ff1c6SCorey Minyard 	/* Number of watchdog pretimeouts. */
116ba8ff1c6SCorey Minyard 	SI_STAT_watchdog_pretimeouts,
117ba8ff1c6SCorey Minyard 
118b3834be5SAdam Buchbinder 	/* Number of asynchronous messages received. */
119ba8ff1c6SCorey Minyard 	SI_STAT_incoming_messages,
120ba8ff1c6SCorey Minyard 
121ba8ff1c6SCorey Minyard 
122ba8ff1c6SCorey Minyard 	/* This *must* remain last, add new values above this. */
123ba8ff1c6SCorey Minyard 	SI_NUM_STATS
124ba8ff1c6SCorey Minyard };
12564959e2dSCorey Minyard 
126c305e3d3SCorey Minyard struct smi_info {
12757bccb4eSCorey Minyard 	int                    si_num;
128a567b623SCorey Minyard 	struct ipmi_smi        *intf;
1291da177e4SLinus Torvalds 	struct si_sm_data      *si_sm;
13081d02b7fSCorey Minyard 	const struct si_sm_handlers *handlers;
1311da177e4SLinus Torvalds 	spinlock_t             si_lock;
132b874b985SCorey Minyard 	struct ipmi_smi_msg    *waiting_msg;
1331da177e4SLinus Torvalds 	struct ipmi_smi_msg    *curr_msg;
1341da177e4SLinus Torvalds 	enum si_intf_state     si_state;
1351da177e4SLinus Torvalds 
136c305e3d3SCorey Minyard 	/*
137c305e3d3SCorey Minyard 	 * Used to handle the various types of I/O that can occur with
138c305e3d3SCorey Minyard 	 * IPMI
139c305e3d3SCorey Minyard 	 */
1401da177e4SLinus Torvalds 	struct si_sm_io io;
1411da177e4SLinus Torvalds 
142c305e3d3SCorey Minyard 	/*
143c305e3d3SCorey Minyard 	 * Per-OEM handler, called from handle_flags().  Returns 1
144c305e3d3SCorey Minyard 	 * when handle_flags() needs to be re-run or 0 indicating it
145c305e3d3SCorey Minyard 	 * set si_state itself.
1463ae0e0f9SCorey Minyard 	 */
1473ae0e0f9SCorey Minyard 	int (*oem_data_avail_handler)(struct smi_info *smi_info);
1483ae0e0f9SCorey Minyard 
149c305e3d3SCorey Minyard 	/*
150c305e3d3SCorey Minyard 	 * Flags from the last GET_MSG_FLAGS command, used when an ATTN
151c305e3d3SCorey Minyard 	 * is set to hold the flags until we are done handling everything
152c305e3d3SCorey Minyard 	 * from the flags.
153c305e3d3SCorey Minyard 	 */
1541da177e4SLinus Torvalds #define RECEIVE_MSG_AVAIL	0x01
1551da177e4SLinus Torvalds #define EVENT_MSG_BUFFER_FULL	0x02
1561da177e4SLinus Torvalds #define WDT_PRE_TIMEOUT_INT	0x08
1573ae0e0f9SCorey Minyard #define OEM0_DATA_AVAIL     0x20
1583ae0e0f9SCorey Minyard #define OEM1_DATA_AVAIL     0x40
1593ae0e0f9SCorey Minyard #define OEM2_DATA_AVAIL     0x80
1603ae0e0f9SCorey Minyard #define OEM_DATA_AVAIL      (OEM0_DATA_AVAIL | \
1613ae0e0f9SCorey Minyard 			     OEM1_DATA_AVAIL | \
1623ae0e0f9SCorey Minyard 			     OEM2_DATA_AVAIL)
1631da177e4SLinus Torvalds 	unsigned char       msg_flags;
1641da177e4SLinus Torvalds 
16540112ae7SCorey Minyard 	/* Does the BMC have an event buffer? */
1667aefac26SCorey Minyard 	bool		    has_event_buffer;
16740112ae7SCorey Minyard 
168c305e3d3SCorey Minyard 	/*
169c305e3d3SCorey Minyard 	 * If set to true, this will request events the next time the
170c305e3d3SCorey Minyard 	 * state machine is idle.
171c305e3d3SCorey Minyard 	 */
1721da177e4SLinus Torvalds 	atomic_t            req_events;
1731da177e4SLinus Torvalds 
174c305e3d3SCorey Minyard 	/*
175c305e3d3SCorey Minyard 	 * If true, run the state machine to completion on every send
176c305e3d3SCorey Minyard 	 * call.  Generally used after a panic to make sure stuff goes
177c305e3d3SCorey Minyard 	 * out.
178c305e3d3SCorey Minyard 	 */
1797aefac26SCorey Minyard 	bool                run_to_completion;
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds 	/* The timer for this si. */
1821da177e4SLinus Torvalds 	struct timer_list   si_timer;
1831da177e4SLinus Torvalds 
1844f7f5551SMasamitsu Yamazaki 	/* This flag is set, if the timer can be set */
1854f7f5551SMasamitsu Yamazaki 	bool		    timer_can_start;
1864f7f5551SMasamitsu Yamazaki 
18748e8ac29SBodo Stroesser 	/* This flag is set, if the timer is running (timer_pending() isn't enough) */
18848e8ac29SBodo Stroesser 	bool		    timer_running;
18948e8ac29SBodo Stroesser 
1901da177e4SLinus Torvalds 	/* The time (in jiffies) the last timeout occurred at. */
1911da177e4SLinus Torvalds 	unsigned long       last_timeout_jiffies;
1921da177e4SLinus Torvalds 
19389986496SCorey Minyard 	/* Are we waiting for the events, pretimeouts, received msgs? */
19489986496SCorey Minyard 	atomic_t            need_watch;
19589986496SCorey Minyard 
196c305e3d3SCorey Minyard 	/*
197c305e3d3SCorey Minyard 	 * The driver will disable interrupts when it gets into a
198c305e3d3SCorey Minyard 	 * situation where it cannot handle messages due to lack of
199c305e3d3SCorey Minyard 	 * memory.  Once that situation clears up, it will re-enable
200c305e3d3SCorey Minyard 	 * interrupts.
201c305e3d3SCorey Minyard 	 */
2027aefac26SCorey Minyard 	bool interrupt_disabled;
2031da177e4SLinus Torvalds 
204d9b7e4f7SCorey Minyard 	/*
205d9b7e4f7SCorey Minyard 	 * Does the BMC support events?
206d9b7e4f7SCorey Minyard 	 */
207d9b7e4f7SCorey Minyard 	bool supports_event_msg_buff;
208d9b7e4f7SCorey Minyard 
209a8df150cSCorey Minyard 	/*
210d0882897SCorey Minyard 	 * Can we disable interrupts the global enables receive irq
211d0882897SCorey Minyard 	 * bit?  There are currently two forms of brokenness, some
212d0882897SCorey Minyard 	 * systems cannot disable the bit (which is technically within
213d0882897SCorey Minyard 	 * the spec but a bad idea) and some systems have the bit
214d0882897SCorey Minyard 	 * forced to zero even though interrupts work (which is
215d0882897SCorey Minyard 	 * clearly outside the spec).  The next bool tells which form
216d0882897SCorey Minyard 	 * of brokenness is present.
2171e7d6a45SCorey Minyard 	 */
218d0882897SCorey Minyard 	bool cannot_disable_irq;
219d0882897SCorey Minyard 
220d0882897SCorey Minyard 	/*
221d0882897SCorey Minyard 	 * Some systems are broken and cannot set the irq enable
222d0882897SCorey Minyard 	 * bit, even if they support interrupts.
223d0882897SCorey Minyard 	 */
224d0882897SCorey Minyard 	bool irq_enable_broken;
2251e7d6a45SCorey Minyard 
226340ff31aSCorey Minyard 	/* Is the driver in maintenance mode? */
227340ff31aSCorey Minyard 	bool in_maintenance_mode;
228340ff31aSCorey Minyard 
2291e7d6a45SCorey Minyard 	/*
230a8df150cSCorey Minyard 	 * Did we get an attention that we did not handle?
231a8df150cSCorey Minyard 	 */
232a8df150cSCorey Minyard 	bool got_attn;
233a8df150cSCorey Minyard 
23450c812b2SCorey Minyard 	/* From the get device id response... */
2353ae0e0f9SCorey Minyard 	struct ipmi_device_id device_id;
2361da177e4SLinus Torvalds 
237cc095f0aSCorey Minyard 	/* Have we added the device group to the device? */
238cc095f0aSCorey Minyard 	bool dev_group_added;
239cc095f0aSCorey Minyard 
2401da177e4SLinus Torvalds 	/* Counters and things for the proc filesystem. */
24164959e2dSCorey Minyard 	atomic_t stats[SI_NUM_STATS];
242a9a2c44fSCorey Minyard 
243e9a705a0SMatt Domsch 	struct task_struct *thread;
244b0defcdbSCorey Minyard 
245b0defcdbSCorey Minyard 	struct list_head link;
2461da177e4SLinus Torvalds };
2471da177e4SLinus Torvalds 
24864959e2dSCorey Minyard #define smi_inc_stat(smi, stat) \
24964959e2dSCorey Minyard 	atomic_inc(&(smi)->stats[SI_STAT_ ## stat])
25064959e2dSCorey Minyard #define smi_get_stat(smi, stat) \
25164959e2dSCorey Minyard 	((unsigned int) atomic_read(&(smi)->stats[SI_STAT_ ## stat]))
25264959e2dSCorey Minyard 
2537a453308SCorey Minyard #define IPMI_MAX_INTFS 4
2547a453308SCorey Minyard static int force_kipmid[IPMI_MAX_INTFS];
255a51f4a81SCorey Minyard static int num_force_kipmid;
256a51f4a81SCorey Minyard 
2577a453308SCorey Minyard static unsigned int kipmid_max_busy_us[IPMI_MAX_INTFS];
258ae74e823SMartin Wilck static int num_max_busy_us;
259ae74e823SMartin Wilck 
2607aefac26SCorey Minyard static bool unload_when_empty = true;
261b361e27bSCorey Minyard 
262b0defcdbSCorey Minyard static int try_smi_init(struct smi_info *smi);
26371404a2fSCorey Minyard static void cleanup_one_si(struct smi_info *smi_info);
264d2478521SCorey Minyard static void cleanup_ipmi_si(void);
265b0defcdbSCorey Minyard 
266f93aae9fSJohn Stultz #ifdef DEBUG_TIMING
debug_timestamp(struct smi_info * smi_info,char * msg)267be850359SCorey Minyard void debug_timestamp(struct smi_info *smi_info, char *msg)
268f93aae9fSJohn Stultz {
2698d73b2aeSArnd Bergmann 	struct timespec64 t;
270f93aae9fSJohn Stultz 
2718d73b2aeSArnd Bergmann 	ktime_get_ts64(&t);
272be850359SCorey Minyard 	dev_dbg(smi_info->io.dev, "**%s: %lld.%9.9ld\n",
273be850359SCorey Minyard 		msg, t.tv_sec, t.tv_nsec);
274f93aae9fSJohn Stultz }
275f93aae9fSJohn Stultz #else
276be850359SCorey Minyard #define debug_timestamp(smi_info, x)
277f93aae9fSJohn Stultz #endif
278f93aae9fSJohn Stultz 
279e041c683SAlan Stern static ATOMIC_NOTIFIER_HEAD(xaction_notifier_list);
register_xaction_notifier(struct notifier_block * nb)280ea94027bSCorey Minyard static int register_xaction_notifier(struct notifier_block *nb)
281ea94027bSCorey Minyard {
282e041c683SAlan Stern 	return atomic_notifier_chain_register(&xaction_notifier_list, nb);
283ea94027bSCorey Minyard }
284ea94027bSCorey Minyard 
deliver_recv_msg(struct smi_info * smi_info,struct ipmi_smi_msg * msg)2851da177e4SLinus Torvalds static void deliver_recv_msg(struct smi_info *smi_info,
2861da177e4SLinus Torvalds 			     struct ipmi_smi_msg *msg)
2871da177e4SLinus Torvalds {
2887adf579cSCorey Minyard 	/* Deliver the message to the upper layer. */
289a747c5abSJiri Kosina 	ipmi_smi_msg_received(smi_info->intf, msg);
290a747c5abSJiri Kosina }
2911da177e4SLinus Torvalds 
return_hosed_msg(struct smi_info * smi_info,int cCode)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 
start_next_msg(struct smi_info * smi_info)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;
322be850359SCorey Minyard 		debug_timestamp(smi_info, "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 
smi_mod_timer(struct smi_info * smi_info,unsigned long new_val)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  */
start_new_msg(struct smi_info * smi_info,unsigned char * msg,unsigned int size)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 
start_check_enables(struct smi_info * smi_info)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 
start_clear_flags(struct smi_info * smi_info)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 
start_getting_msg_queue(struct smi_info * smi_info)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 
start_getting_events(struct smi_info * smi_info)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  */
disable_si_irq(struct smi_info * smi_info)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 
enable_si_irq(struct smi_info * smi_info)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  */
alloc_msg_handle_irq(struct smi_info * smi_info)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 
handle_flags(struct smi_info * smi_info)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;
4701da177e4SLinus Torvalds 		ipmi_smi_watchdog_pretimeout(smi_info->intf);
4711da177e4SLinus Torvalds 	} else if (smi_info->msg_flags & RECEIVE_MSG_AVAIL) {
4721da177e4SLinus Torvalds 		/* Messages available. */
473968bf7ccSCorey Minyard 		smi_info->curr_msg = alloc_msg_handle_irq(smi_info);
474968bf7ccSCorey Minyard 		if (!smi_info->curr_msg)
4751da177e4SLinus Torvalds 			return;
4761da177e4SLinus Torvalds 
477968bf7ccSCorey Minyard 		start_getting_msg_queue(smi_info);
4781da177e4SLinus Torvalds 	} else if (smi_info->msg_flags & EVENT_MSG_BUFFER_FULL) {
4791da177e4SLinus Torvalds 		/* Events available. */
480968bf7ccSCorey Minyard 		smi_info->curr_msg = alloc_msg_handle_irq(smi_info);
481968bf7ccSCorey Minyard 		if (!smi_info->curr_msg)
4821da177e4SLinus Torvalds 			return;
4831da177e4SLinus Torvalds 
484968bf7ccSCorey Minyard 		start_getting_events(smi_info);
4854064d5efSCorey Minyard 	} else if (smi_info->msg_flags & OEM_DATA_AVAIL &&
4864064d5efSCorey Minyard 		   smi_info->oem_data_avail_handler) {
4873ae0e0f9SCorey Minyard 		if (smi_info->oem_data_avail_handler(smi_info))
4883ae0e0f9SCorey Minyard 			goto retry;
489c305e3d3SCorey Minyard 	} else
4901da177e4SLinus Torvalds 		smi_info->si_state = SI_NORMAL;
4911da177e4SLinus Torvalds }
4921da177e4SLinus Torvalds 
493d9b7e4f7SCorey Minyard /*
494d9b7e4f7SCorey Minyard  * Global enables we care about.
495d9b7e4f7SCorey Minyard  */
496d9b7e4f7SCorey Minyard #define GLOBAL_ENABLES_MASK (IPMI_BMC_EVT_MSG_BUFF | IPMI_BMC_RCV_MSG_INTR | \
497d9b7e4f7SCorey Minyard 			     IPMI_BMC_EVT_MSG_INTR)
498d9b7e4f7SCorey Minyard 
current_global_enables(struct smi_info * smi_info,u8 base,bool * irq_on)49995c97b59SCorey Minyard static u8 current_global_enables(struct smi_info *smi_info, u8 base,
50095c97b59SCorey Minyard 				 bool *irq_on)
501d9b7e4f7SCorey Minyard {
502d9b7e4f7SCorey Minyard 	u8 enables = 0;
503d9b7e4f7SCorey Minyard 
504d9b7e4f7SCorey Minyard 	if (smi_info->supports_event_msg_buff)
505d9b7e4f7SCorey Minyard 		enables |= IPMI_BMC_EVT_MSG_BUFF;
506d9b7e4f7SCorey Minyard 
507910840f2SCorey Minyard 	if (((smi_info->io.irq && !smi_info->interrupt_disabled) ||
508d0882897SCorey Minyard 	     smi_info->cannot_disable_irq) &&
509d0882897SCorey Minyard 	    !smi_info->irq_enable_broken)
510d9b7e4f7SCorey Minyard 		enables |= IPMI_BMC_RCV_MSG_INTR;
511d9b7e4f7SCorey Minyard 
512d9b7e4f7SCorey Minyard 	if (smi_info->supports_event_msg_buff &&
513910840f2SCorey Minyard 	    smi_info->io.irq && !smi_info->interrupt_disabled &&
514d0882897SCorey Minyard 	    !smi_info->irq_enable_broken)
515d9b7e4f7SCorey Minyard 		enables |= IPMI_BMC_EVT_MSG_INTR;
516d9b7e4f7SCorey Minyard 
51795c97b59SCorey Minyard 	*irq_on = enables & (IPMI_BMC_EVT_MSG_INTR | IPMI_BMC_RCV_MSG_INTR);
51895c97b59SCorey Minyard 
519d9b7e4f7SCorey Minyard 	return enables;
520d9b7e4f7SCorey Minyard }
521d9b7e4f7SCorey Minyard 
check_bt_irq(struct smi_info * smi_info,bool irq_on)52295c97b59SCorey Minyard static void check_bt_irq(struct smi_info *smi_info, bool irq_on)
52395c97b59SCorey Minyard {
52495c97b59SCorey Minyard 	u8 irqstate = smi_info->io.inputb(&smi_info->io, IPMI_BT_INTMASK_REG);
52595c97b59SCorey Minyard 
52695c97b59SCorey Minyard 	irqstate &= IPMI_BT_INTMASK_ENABLE_IRQ_BIT;
52795c97b59SCorey Minyard 
52895c97b59SCorey Minyard 	if ((bool)irqstate == irq_on)
52995c97b59SCorey Minyard 		return;
53095c97b59SCorey Minyard 
53195c97b59SCorey Minyard 	if (irq_on)
53295c97b59SCorey Minyard 		smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG,
53395c97b59SCorey Minyard 				     IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
53495c97b59SCorey Minyard 	else
53595c97b59SCorey Minyard 		smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG, 0);
53695c97b59SCorey Minyard }
53795c97b59SCorey Minyard 
handle_transaction_done(struct smi_info * smi_info)5381da177e4SLinus Torvalds static void handle_transaction_done(struct smi_info *smi_info)
5391da177e4SLinus Torvalds {
5401da177e4SLinus Torvalds 	struct ipmi_smi_msg *msg;
5411da177e4SLinus Torvalds 
542be850359SCorey Minyard 	debug_timestamp(smi_info, "Done");
5431da177e4SLinus Torvalds 	switch (smi_info->si_state) {
5441da177e4SLinus Torvalds 	case SI_NORMAL:
5451da177e4SLinus Torvalds 		if (!smi_info->curr_msg)
5461da177e4SLinus Torvalds 			break;
5471da177e4SLinus Torvalds 
5481da177e4SLinus Torvalds 		smi_info->curr_msg->rsp_size
5491da177e4SLinus Torvalds 			= smi_info->handlers->get_result(
5501da177e4SLinus Torvalds 				smi_info->si_sm,
5511da177e4SLinus Torvalds 				smi_info->curr_msg->rsp,
5521da177e4SLinus Torvalds 				IPMI_MAX_MSG_LENGTH);
5531da177e4SLinus Torvalds 
554c305e3d3SCorey Minyard 		/*
555c305e3d3SCorey Minyard 		 * Do this here becase deliver_recv_msg() releases the
556c305e3d3SCorey Minyard 		 * lock, and a new message can be put in during the
557c305e3d3SCorey Minyard 		 * time the lock is released.
558c305e3d3SCorey Minyard 		 */
5591da177e4SLinus Torvalds 		msg = smi_info->curr_msg;
5601da177e4SLinus Torvalds 		smi_info->curr_msg = NULL;
5611da177e4SLinus Torvalds 		deliver_recv_msg(smi_info, msg);
5621da177e4SLinus Torvalds 		break;
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds 	case SI_GETTING_FLAGS:
5651da177e4SLinus Torvalds 	{
5661da177e4SLinus Torvalds 		unsigned char msg[4];
5671da177e4SLinus Torvalds 		unsigned int  len;
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds 		/* We got the flags from the SMI, now handle them. */
5701da177e4SLinus Torvalds 		len = smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
5711da177e4SLinus Torvalds 		if (msg[2] != 0) {
572c305e3d3SCorey Minyard 			/* Error fetching flags, just give up for now. */
5731da177e4SLinus Torvalds 			smi_info->si_state = SI_NORMAL;
5741da177e4SLinus Torvalds 		} else if (len < 4) {
575c305e3d3SCorey Minyard 			/*
576c305e3d3SCorey Minyard 			 * Hmm, no flags.  That's technically illegal, but
577c305e3d3SCorey Minyard 			 * don't use uninitialized data.
578c305e3d3SCorey Minyard 			 */
5791da177e4SLinus Torvalds 			smi_info->si_state = SI_NORMAL;
5801da177e4SLinus Torvalds 		} else {
5811da177e4SLinus Torvalds 			smi_info->msg_flags = msg[3];
5821da177e4SLinus Torvalds 			handle_flags(smi_info);
5831da177e4SLinus Torvalds 		}
5841da177e4SLinus Torvalds 		break;
5851da177e4SLinus Torvalds 	}
5861da177e4SLinus Torvalds 
5871da177e4SLinus Torvalds 	case SI_CLEARING_FLAGS:
5881da177e4SLinus Torvalds 	{
5891da177e4SLinus Torvalds 		unsigned char msg[3];
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds 		/* We cleared the flags. */
5921da177e4SLinus Torvalds 		smi_info->handlers->get_result(smi_info->si_sm, msg, 3);
5931da177e4SLinus Torvalds 		if (msg[2] != 0) {
5941da177e4SLinus Torvalds 			/* Error clearing flags */
595ca8c1c53SWen Yang 			dev_warn_ratelimited(smi_info->io.dev,
596279fbd0cSMyron Stowe 				 "Error clearing flags: %2.2x\n", msg[2]);
5971da177e4SLinus Torvalds 		}
5981da177e4SLinus Torvalds 		smi_info->si_state = SI_NORMAL;
5991da177e4SLinus Torvalds 		break;
6001da177e4SLinus Torvalds 	}
6011da177e4SLinus Torvalds 
6021da177e4SLinus Torvalds 	case SI_GETTING_EVENTS:
6031da177e4SLinus Torvalds 	{
6041da177e4SLinus Torvalds 		smi_info->curr_msg->rsp_size
6051da177e4SLinus Torvalds 			= smi_info->handlers->get_result(
6061da177e4SLinus Torvalds 				smi_info->si_sm,
6071da177e4SLinus Torvalds 				smi_info->curr_msg->rsp,
6081da177e4SLinus Torvalds 				IPMI_MAX_MSG_LENGTH);
6091da177e4SLinus Torvalds 
610c305e3d3SCorey Minyard 		/*
611c305e3d3SCorey Minyard 		 * Do this here becase deliver_recv_msg() releases the
612c305e3d3SCorey Minyard 		 * lock, and a new message can be put in during the
613c305e3d3SCorey Minyard 		 * time the lock is released.
614c305e3d3SCorey Minyard 		 */
6151da177e4SLinus Torvalds 		msg = smi_info->curr_msg;
6161da177e4SLinus Torvalds 		smi_info->curr_msg = NULL;
6171da177e4SLinus Torvalds 		if (msg->rsp[2] != 0) {
6181da177e4SLinus Torvalds 			/* Error getting event, probably done. */
6191da177e4SLinus Torvalds 			msg->done(msg);
6201da177e4SLinus Torvalds 
6211da177e4SLinus Torvalds 			/* Take off the event flag. */
6221da177e4SLinus Torvalds 			smi_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
6231da177e4SLinus Torvalds 			handle_flags(smi_info);
6241da177e4SLinus Torvalds 		} else {
62564959e2dSCorey Minyard 			smi_inc_stat(smi_info, events);
6261da177e4SLinus Torvalds 
627c305e3d3SCorey Minyard 			/*
628c305e3d3SCorey Minyard 			 * Do this before we deliver the message
629c305e3d3SCorey Minyard 			 * because delivering the message releases the
630c305e3d3SCorey Minyard 			 * lock and something else can mess with the
631c305e3d3SCorey Minyard 			 * state.
632c305e3d3SCorey Minyard 			 */
6331da177e4SLinus Torvalds 			handle_flags(smi_info);
6341da177e4SLinus Torvalds 
6351da177e4SLinus Torvalds 			deliver_recv_msg(smi_info, msg);
6361da177e4SLinus Torvalds 		}
6371da177e4SLinus Torvalds 		break;
6381da177e4SLinus Torvalds 	}
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds 	case SI_GETTING_MESSAGES:
6411da177e4SLinus Torvalds 	{
6421da177e4SLinus Torvalds 		smi_info->curr_msg->rsp_size
6431da177e4SLinus Torvalds 			= smi_info->handlers->get_result(
6441da177e4SLinus Torvalds 				smi_info->si_sm,
6451da177e4SLinus Torvalds 				smi_info->curr_msg->rsp,
6461da177e4SLinus Torvalds 				IPMI_MAX_MSG_LENGTH);
6471da177e4SLinus Torvalds 
648c305e3d3SCorey Minyard 		/*
649c305e3d3SCorey Minyard 		 * Do this here becase deliver_recv_msg() releases the
650c305e3d3SCorey Minyard 		 * lock, and a new message can be put in during the
651c305e3d3SCorey Minyard 		 * time the lock is released.
652c305e3d3SCorey Minyard 		 */
6531da177e4SLinus Torvalds 		msg = smi_info->curr_msg;
6541da177e4SLinus Torvalds 		smi_info->curr_msg = NULL;
6551da177e4SLinus Torvalds 		if (msg->rsp[2] != 0) {
6561da177e4SLinus Torvalds 			/* Error getting event, probably done. */
6571da177e4SLinus Torvalds 			msg->done(msg);
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds 			/* Take off the msg flag. */
6601da177e4SLinus Torvalds 			smi_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
6611da177e4SLinus Torvalds 			handle_flags(smi_info);
6621da177e4SLinus Torvalds 		} else {
66364959e2dSCorey Minyard 			smi_inc_stat(smi_info, incoming_messages);
6641da177e4SLinus Torvalds 
665c305e3d3SCorey Minyard 			/*
666c305e3d3SCorey Minyard 			 * Do this before we deliver the message
667c305e3d3SCorey Minyard 			 * because delivering the message releases the
668c305e3d3SCorey Minyard 			 * lock and something else can mess with the
669c305e3d3SCorey Minyard 			 * state.
670c305e3d3SCorey Minyard 			 */
6711da177e4SLinus Torvalds 			handle_flags(smi_info);
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds 			deliver_recv_msg(smi_info, msg);
6741da177e4SLinus Torvalds 		}
6751da177e4SLinus Torvalds 		break;
6761da177e4SLinus Torvalds 	}
6771da177e4SLinus Torvalds 
678d9b7e4f7SCorey Minyard 	case SI_CHECKING_ENABLES:
6791da177e4SLinus Torvalds 	{
6801da177e4SLinus Torvalds 		unsigned char msg[4];
681d9b7e4f7SCorey Minyard 		u8 enables;
68295c97b59SCorey Minyard 		bool irq_on;
6831da177e4SLinus Torvalds 
6841da177e4SLinus Torvalds 		/* We got the flags from the SMI, now handle them. */
6851da177e4SLinus Torvalds 		smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
6861da177e4SLinus Torvalds 		if (msg[2] != 0) {
687ca8c1c53SWen Yang 			dev_warn_ratelimited(smi_info->io.dev,
688ca8c1c53SWen Yang 				"Couldn't get irq info: %x,\n"
689ca8c1c53SWen Yang 				"Maybe ok, but ipmi might run very slowly.\n",
690ca8c1c53SWen Yang 				msg[2]);
6911da177e4SLinus Torvalds 			smi_info->si_state = SI_NORMAL;
692d9b7e4f7SCorey Minyard 			break;
693d9b7e4f7SCorey Minyard 		}
69495c97b59SCorey Minyard 		enables = current_global_enables(smi_info, 0, &irq_on);
695910840f2SCorey Minyard 		if (smi_info->io.si_type == SI_BT)
69695c97b59SCorey Minyard 			/* BT has its own interrupt enable bit. */
69795c97b59SCorey Minyard 			check_bt_irq(smi_info, irq_on);
698d9b7e4f7SCorey Minyard 		if (enables != (msg[3] & GLOBAL_ENABLES_MASK)) {
699d9b7e4f7SCorey Minyard 			/* Enables are not correct, fix them. */
7001da177e4SLinus Torvalds 			msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
7011da177e4SLinus Torvalds 			msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
702d9b7e4f7SCorey Minyard 			msg[2] = enables | (msg[3] & ~GLOBAL_ENABLES_MASK);
7031da177e4SLinus Torvalds 			smi_info->handlers->start_transaction(
7041da177e4SLinus Torvalds 				smi_info->si_sm, msg, 3);
705d9b7e4f7SCorey Minyard 			smi_info->si_state = SI_SETTING_ENABLES;
706d9b7e4f7SCorey Minyard 		} else if (smi_info->supports_event_msg_buff) {
707d9b7e4f7SCorey Minyard 			smi_info->curr_msg = ipmi_alloc_smi_msg();
708d9b7e4f7SCorey Minyard 			if (!smi_info->curr_msg) {
709ee6cd5f8SCorey Minyard 				smi_info->si_state = SI_NORMAL;
710d9b7e4f7SCorey Minyard 				break;
711d9b7e4f7SCorey Minyard 			}
7125ac7b2fcSCorey Minyard 			start_getting_events(smi_info);
713ee6cd5f8SCorey Minyard 		} else {
714d9b7e4f7SCorey Minyard 			smi_info->si_state = SI_NORMAL;
715ee6cd5f8SCorey Minyard 		}
716ee6cd5f8SCorey Minyard 		break;
717ee6cd5f8SCorey Minyard 	}
718ee6cd5f8SCorey Minyard 
719d9b7e4f7SCorey Minyard 	case SI_SETTING_ENABLES:
720ee6cd5f8SCorey Minyard 	{
721ee6cd5f8SCorey Minyard 		unsigned char msg[4];
722ee6cd5f8SCorey Minyard 
723ee6cd5f8SCorey Minyard 		smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
724d9b7e4f7SCorey Minyard 		if (msg[2] != 0)
725ca8c1c53SWen Yang 			dev_warn_ratelimited(smi_info->io.dev,
726d9b7e4f7SCorey Minyard 				 "Could not set the global enables: 0x%x.\n",
727d9b7e4f7SCorey Minyard 				 msg[2]);
728d9b7e4f7SCorey Minyard 
729d9b7e4f7SCorey Minyard 		if (smi_info->supports_event_msg_buff) {
730d9b7e4f7SCorey Minyard 			smi_info->curr_msg = ipmi_alloc_smi_msg();
731d9b7e4f7SCorey Minyard 			if (!smi_info->curr_msg) {
732ee6cd5f8SCorey Minyard 				smi_info->si_state = SI_NORMAL;
733ee6cd5f8SCorey Minyard 				break;
734ee6cd5f8SCorey Minyard 			}
7355ac7b2fcSCorey Minyard 			start_getting_events(smi_info);
736d9b7e4f7SCorey Minyard 		} else {
737d9b7e4f7SCorey Minyard 			smi_info->si_state = SI_NORMAL;
738d9b7e4f7SCorey Minyard 		}
739d9b7e4f7SCorey Minyard 		break;
740d9b7e4f7SCorey Minyard 	}
7411da177e4SLinus Torvalds 	}
7421da177e4SLinus Torvalds }
7431da177e4SLinus Torvalds 
744c305e3d3SCorey Minyard /*
745c305e3d3SCorey Minyard  * Called on timeouts and events.  Timeouts should pass the elapsed
746c305e3d3SCorey Minyard  * time, interrupts should pass in zero.  Must be called with
747c305e3d3SCorey Minyard  * si_lock held and interrupts disabled.
748c305e3d3SCorey Minyard  */
smi_event_handler(struct smi_info * smi_info,int time)7491da177e4SLinus Torvalds static enum si_sm_result smi_event_handler(struct smi_info *smi_info,
7501da177e4SLinus Torvalds 					   int time)
7511da177e4SLinus Torvalds {
7521da177e4SLinus Torvalds 	enum si_sm_result si_sm_result;
7531da177e4SLinus Torvalds 
7541da177e4SLinus Torvalds restart:
755c305e3d3SCorey Minyard 	/*
756c305e3d3SCorey Minyard 	 * There used to be a loop here that waited a little while
757c305e3d3SCorey Minyard 	 * (around 25us) before giving up.  That turned out to be
758c305e3d3SCorey Minyard 	 * pointless, the minimum delays I was seeing were in the 300us
759c305e3d3SCorey Minyard 	 * range, which is far too long to wait in an interrupt.  So
760c305e3d3SCorey Minyard 	 * we just run until the state machine tells us something
761c305e3d3SCorey Minyard 	 * happened or it needs a delay.
762c305e3d3SCorey Minyard 	 */
7631da177e4SLinus Torvalds 	si_sm_result = smi_info->handlers->event(smi_info->si_sm, time);
7641da177e4SLinus Torvalds 	time = 0;
7651da177e4SLinus Torvalds 	while (si_sm_result == SI_SM_CALL_WITHOUT_DELAY)
7661da177e4SLinus Torvalds 		si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
7671da177e4SLinus Torvalds 
768c305e3d3SCorey Minyard 	if (si_sm_result == SI_SM_TRANSACTION_COMPLETE) {
76964959e2dSCorey Minyard 		smi_inc_stat(smi_info, complete_transactions);
7701da177e4SLinus Torvalds 
7711da177e4SLinus Torvalds 		handle_transaction_done(smi_info);
772d9dffd2aSCorey Minyard 		goto restart;
773c305e3d3SCorey Minyard 	} else if (si_sm_result == SI_SM_HOSED) {
77464959e2dSCorey Minyard 		smi_inc_stat(smi_info, hosed_count);
7751da177e4SLinus Torvalds 
776c305e3d3SCorey Minyard 		/*
777c305e3d3SCorey Minyard 		 * Do the before return_hosed_msg, because that
778c305e3d3SCorey Minyard 		 * releases the lock.
779c305e3d3SCorey Minyard 		 */
7801da177e4SLinus Torvalds 		smi_info->si_state = SI_NORMAL;
7811da177e4SLinus Torvalds 		if (smi_info->curr_msg != NULL) {
782c305e3d3SCorey Minyard 			/*
783c305e3d3SCorey Minyard 			 * If we were handling a user message, format
784c305e3d3SCorey Minyard 			 * a response to send to the upper layer to
785c305e3d3SCorey Minyard 			 * tell it about the error.
786c305e3d3SCorey Minyard 			 */
7874d7cbac7SCorey Minyard 			return_hosed_msg(smi_info, IPMI_ERR_UNSPECIFIED);
7881da177e4SLinus Torvalds 		}
789d9dffd2aSCorey Minyard 		goto restart;
7901da177e4SLinus Torvalds 	}
7911da177e4SLinus Torvalds 
7924ea18425SCorey Minyard 	/*
7934ea18425SCorey Minyard 	 * We prefer handling attn over new messages.  But don't do
7944ea18425SCorey Minyard 	 * this if there is not yet an upper layer to handle anything.
7954ea18425SCorey Minyard 	 */
7960fbecb4fSCorey Minyard 	if (si_sm_result == SI_SM_ATTN || smi_info->got_attn) {
7971da177e4SLinus Torvalds 		unsigned char msg[2];
7981da177e4SLinus Torvalds 
799a8df150cSCorey Minyard 		if (smi_info->si_state != SI_NORMAL) {
800a8df150cSCorey Minyard 			/*
801a8df150cSCorey Minyard 			 * We got an ATTN, but we are doing something else.
802a8df150cSCorey Minyard 			 * Handle the ATTN later.
803a8df150cSCorey Minyard 			 */
804a8df150cSCorey Minyard 			smi_info->got_attn = true;
805a8df150cSCorey Minyard 		} else {
806a8df150cSCorey Minyard 			smi_info->got_attn = false;
80764959e2dSCorey Minyard 			smi_inc_stat(smi_info, attentions);
8081da177e4SLinus Torvalds 
809c305e3d3SCorey Minyard 			/*
810c305e3d3SCorey Minyard 			 * Got a attn, send down a get message flags to see
811c305e3d3SCorey Minyard 			 * what's causing it.  It would be better to handle
812c305e3d3SCorey Minyard 			 * this in the upper layer, but due to the way
813c305e3d3SCorey Minyard 			 * interrupts work with the SMI, that's not really
814c305e3d3SCorey Minyard 			 * possible.
815c305e3d3SCorey Minyard 			 */
8161da177e4SLinus Torvalds 			msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
8171da177e4SLinus Torvalds 			msg[1] = IPMI_GET_MSG_FLAGS_CMD;
8181da177e4SLinus Torvalds 
8190cfec916SCorey Minyard 			start_new_msg(smi_info, msg, 2);
8201da177e4SLinus Torvalds 			smi_info->si_state = SI_GETTING_FLAGS;
8211da177e4SLinus Torvalds 			goto restart;
8221da177e4SLinus Torvalds 		}
823a8df150cSCorey Minyard 	}
8241da177e4SLinus Torvalds 
8251da177e4SLinus Torvalds 	/* If we are currently idle, try to start the next message. */
8261da177e4SLinus Torvalds 	if (si_sm_result == SI_SM_IDLE) {
82764959e2dSCorey Minyard 		smi_inc_stat(smi_info, idles);
8281da177e4SLinus Torvalds 
8291da177e4SLinus Torvalds 		si_sm_result = start_next_msg(smi_info);
8301da177e4SLinus Torvalds 		if (si_sm_result != SI_SM_IDLE)
8311da177e4SLinus Torvalds 			goto restart;
8321da177e4SLinus Torvalds 	}
8331da177e4SLinus Torvalds 
8341da177e4SLinus Torvalds 	if ((si_sm_result == SI_SM_IDLE)
835c305e3d3SCorey Minyard 	    && (atomic_read(&smi_info->req_events))) {
836c305e3d3SCorey Minyard 		/*
837c305e3d3SCorey Minyard 		 * We are idle and the upper layer requested that I fetch
838c305e3d3SCorey Minyard 		 * events, so do so.
839c305e3d3SCorey Minyard 		 */
8401da177e4SLinus Torvalds 		atomic_set(&smi_info->req_events, 0);
84155162fb1SCorey Minyard 
842d9b7e4f7SCorey Minyard 		/*
843d9b7e4f7SCorey Minyard 		 * Take this opportunity to check the interrupt and
844d9b7e4f7SCorey Minyard 		 * message enable state for the BMC.  The BMC can be
845d9b7e4f7SCorey Minyard 		 * asynchronously reset, and may thus get interrupts
846d9b7e4f7SCorey Minyard 		 * disable and messages disabled.
847d9b7e4f7SCorey Minyard 		 */
848910840f2SCorey Minyard 		if (smi_info->supports_event_msg_buff || smi_info->io.irq) {
8494f7f5551SMasamitsu Yamazaki 			start_check_enables(smi_info);
850d9b7e4f7SCorey Minyard 		} else {
851d9b7e4f7SCorey Minyard 			smi_info->curr_msg = alloc_msg_handle_irq(smi_info);
85255162fb1SCorey Minyard 			if (!smi_info->curr_msg)
85355162fb1SCorey Minyard 				goto out;
85455162fb1SCorey Minyard 
855d9b7e4f7SCorey Minyard 			start_getting_events(smi_info);
856d9b7e4f7SCorey Minyard 		}
8571da177e4SLinus Torvalds 		goto restart;
8581da177e4SLinus Torvalds 	}
859314ef52fSCorey Minyard 
860314ef52fSCorey Minyard 	if (si_sm_result == SI_SM_IDLE && smi_info->timer_running) {
861314ef52fSCorey Minyard 		/* Ok it if fails, the timer will just go off. */
862314ef52fSCorey Minyard 		if (del_timer(&smi_info->si_timer))
863314ef52fSCorey Minyard 			smi_info->timer_running = false;
864314ef52fSCorey Minyard 	}
865314ef52fSCorey Minyard 
86655162fb1SCorey Minyard out:
8671da177e4SLinus Torvalds 	return si_sm_result;
8681da177e4SLinus Torvalds }
8691da177e4SLinus Torvalds 
check_start_timer_thread(struct smi_info * smi_info)87089986496SCorey Minyard static void check_start_timer_thread(struct smi_info *smi_info)
87189986496SCorey Minyard {
87289986496SCorey Minyard 	if (smi_info->si_state == SI_NORMAL && smi_info->curr_msg == NULL) {
87389986496SCorey Minyard 		smi_mod_timer(smi_info, jiffies + SI_TIMEOUT_JIFFIES);
87489986496SCorey Minyard 
87589986496SCorey Minyard 		if (smi_info->thread)
87689986496SCorey Minyard 			wake_up_process(smi_info->thread);
87789986496SCorey Minyard 
87889986496SCorey Minyard 		start_next_msg(smi_info);
87989986496SCorey Minyard 		smi_event_handler(smi_info, 0);
88089986496SCorey Minyard 	}
88189986496SCorey Minyard }
88289986496SCorey Minyard 
flush_messages(void * send_info)88382802f96SHidehiro Kawai static void flush_messages(void *send_info)
884e45361d7SHidehiro Kawai {
88582802f96SHidehiro Kawai 	struct smi_info *smi_info = send_info;
886e45361d7SHidehiro Kawai 	enum si_sm_result result;
887e45361d7SHidehiro Kawai 
888e45361d7SHidehiro Kawai 	/*
889e45361d7SHidehiro Kawai 	 * Currently, this function is called only in run-to-completion
890e45361d7SHidehiro Kawai 	 * mode.  This means we are single-threaded, no need for locks.
891e45361d7SHidehiro Kawai 	 */
892e45361d7SHidehiro Kawai 	result = smi_event_handler(smi_info, 0);
893e45361d7SHidehiro Kawai 	while (result != SI_SM_IDLE) {
894e45361d7SHidehiro Kawai 		udelay(SI_SHORT_TIMEOUT_USEC);
895e45361d7SHidehiro Kawai 		result = smi_event_handler(smi_info, SI_SHORT_TIMEOUT_USEC);
896e45361d7SHidehiro Kawai 	}
897e45361d7SHidehiro Kawai }
898e45361d7SHidehiro Kawai 
sender(void * send_info,struct ipmi_smi_msg * msg)8991da177e4SLinus Torvalds static void sender(void                *send_info,
90099ab32f3SCorey Minyard 		   struct ipmi_smi_msg *msg)
9011da177e4SLinus Torvalds {
9021da177e4SLinus Torvalds 	struct smi_info   *smi_info = send_info;
9031da177e4SLinus Torvalds 	unsigned long     flags;
9041da177e4SLinus Torvalds 
905be850359SCorey Minyard 	debug_timestamp(smi_info, "Enqueue");
9061da177e4SLinus Torvalds 
9071da177e4SLinus Torvalds 	if (smi_info->run_to_completion) {
908bda4c30aSCorey Minyard 		/*
90982802f96SHidehiro Kawai 		 * If we are running to completion, start it.  Upper
91082802f96SHidehiro Kawai 		 * layer will call flush_messages to clear it out.
911bda4c30aSCorey Minyard 		 */
9129f812704SHidehiro Kawai 		smi_info->waiting_msg = msg;
9131da177e4SLinus Torvalds 		return;
9141da177e4SLinus Torvalds 	}
9151da177e4SLinus Torvalds 
916f60adf42SCorey Minyard 	spin_lock_irqsave(&smi_info->si_lock, flags);
9171d86e29bSCorey Minyard 	/*
9181d86e29bSCorey Minyard 	 * The following two lines don't need to be under the lock for
9191d86e29bSCorey Minyard 	 * the lock's sake, but they do need SMP memory barriers to
9201d86e29bSCorey Minyard 	 * avoid getting things out of order.  We are already claiming
9211d86e29bSCorey Minyard 	 * the lock, anyway, so just do it under the lock to avoid the
9221d86e29bSCorey Minyard 	 * ordering problem.
9231d86e29bSCorey Minyard 	 */
9241d86e29bSCorey Minyard 	BUG_ON(smi_info->waiting_msg);
9251d86e29bSCorey Minyard 	smi_info->waiting_msg = msg;
92689986496SCorey Minyard 	check_start_timer_thread(smi_info);
927bda4c30aSCorey Minyard 	spin_unlock_irqrestore(&smi_info->si_lock, flags);
9281da177e4SLinus Torvalds }
9291da177e4SLinus Torvalds 
set_run_to_completion(void * send_info,bool i_run_to_completion)9307aefac26SCorey Minyard static void set_run_to_completion(void *send_info, bool i_run_to_completion)
9311da177e4SLinus Torvalds {
9321da177e4SLinus Torvalds 	struct smi_info   *smi_info = send_info;
9331da177e4SLinus Torvalds 
9341da177e4SLinus Torvalds 	smi_info->run_to_completion = i_run_to_completion;
935e45361d7SHidehiro Kawai 	if (i_run_to_completion)
936e45361d7SHidehiro Kawai 		flush_messages(smi_info);
9371da177e4SLinus Torvalds }
9381da177e4SLinus Torvalds 
939ae74e823SMartin Wilck /*
9408d73b2aeSArnd Bergmann  * Use -1 as a special constant to tell that we are spinning in kipmid
9418d73b2aeSArnd Bergmann  * looking for something and not delaying between checks
942ae74e823SMartin Wilck  */
9438d73b2aeSArnd Bergmann #define IPMI_TIME_NOT_BUSY ns_to_ktime(-1ull)
ipmi_thread_busy_wait(enum si_sm_result smi_result,const struct smi_info * smi_info,ktime_t * busy_until)944cbb19cb1SCorey Minyard static inline bool ipmi_thread_busy_wait(enum si_sm_result smi_result,
945ae74e823SMartin Wilck 					 const struct smi_info *smi_info,
9468d73b2aeSArnd Bergmann 					 ktime_t *busy_until)
947ae74e823SMartin Wilck {
948ae74e823SMartin Wilck 	unsigned int max_busy_us = 0;
949ae74e823SMartin Wilck 
95057bccb4eSCorey Minyard 	if (smi_info->si_num < num_max_busy_us)
95157bccb4eSCorey Minyard 		max_busy_us = kipmid_max_busy_us[smi_info->si_num];
952ae74e823SMartin Wilck 	if (max_busy_us == 0 || smi_result != SI_SM_CALL_WITH_DELAY)
9538d73b2aeSArnd Bergmann 		*busy_until = IPMI_TIME_NOT_BUSY;
9548d73b2aeSArnd Bergmann 	else if (*busy_until == IPMI_TIME_NOT_BUSY) {
9558d73b2aeSArnd Bergmann 		*busy_until = ktime_get() + max_busy_us * NSEC_PER_USEC;
956ae74e823SMartin Wilck 	} else {
9578d73b2aeSArnd Bergmann 		if (unlikely(ktime_get() > *busy_until)) {
9588d73b2aeSArnd Bergmann 			*busy_until = IPMI_TIME_NOT_BUSY;
959cbb19cb1SCorey Minyard 			return false;
960ae74e823SMartin Wilck 		}
961ae74e823SMartin Wilck 	}
962cbb19cb1SCorey Minyard 	return true;
963ae74e823SMartin Wilck }
964ae74e823SMartin Wilck 
965ae74e823SMartin Wilck 
966ae74e823SMartin Wilck /*
967ae74e823SMartin Wilck  * A busy-waiting loop for speeding up IPMI operation.
968ae74e823SMartin Wilck  *
969ae74e823SMartin Wilck  * Lousy hardware makes this hard.  This is only enabled for systems
970ae74e823SMartin Wilck  * that are not BT and do not have interrupts.  It starts spinning
971ae74e823SMartin Wilck  * when an operation is complete or until max_busy tells it to stop
972ae74e823SMartin Wilck  * (if that is enabled).  See the paragraph on kimid_max_busy_us in
973283b69bfSMauro Carvalho Chehab  * Documentation/driver-api/ipmi.rst for details.
974ae74e823SMartin Wilck  */
ipmi_thread(void * data)975a9a2c44fSCorey Minyard static int ipmi_thread(void *data)
976a9a2c44fSCorey Minyard {
977a9a2c44fSCorey Minyard 	struct smi_info *smi_info = data;
978e9a705a0SMatt Domsch 	unsigned long flags;
979a9a2c44fSCorey Minyard 	enum si_sm_result smi_result;
9808d73b2aeSArnd Bergmann 	ktime_t busy_until = IPMI_TIME_NOT_BUSY;
981a9a2c44fSCorey Minyard 
9828698a745SDongsheng Yang 	set_user_nice(current, MAX_NICE);
983e9a705a0SMatt Domsch 	while (!kthread_should_stop()) {
984ae74e823SMartin Wilck 		int busy_wait;
985ae74e823SMartin Wilck 
986a9a2c44fSCorey Minyard 		spin_lock_irqsave(&(smi_info->si_lock), flags);
987a9a2c44fSCorey Minyard 		smi_result = smi_event_handler(smi_info, 0);
98848e8ac29SBodo Stroesser 
98948e8ac29SBodo Stroesser 		/*
99048e8ac29SBodo Stroesser 		 * If the driver is doing something, there is a possible
99148e8ac29SBodo Stroesser 		 * race with the timer.  If the timer handler see idle,
99248e8ac29SBodo Stroesser 		 * and the thread here sees something else, the timer
99348e8ac29SBodo Stroesser 		 * handler won't restart the timer even though it is
99448e8ac29SBodo Stroesser 		 * required.  So start it here if necessary.
99548e8ac29SBodo Stroesser 		 */
99648e8ac29SBodo Stroesser 		if (smi_result != SI_SM_IDLE && !smi_info->timer_running)
99748e8ac29SBodo Stroesser 			smi_mod_timer(smi_info, jiffies + SI_TIMEOUT_JIFFIES);
99848e8ac29SBodo Stroesser 
999a9a2c44fSCorey Minyard 		spin_unlock_irqrestore(&(smi_info->si_lock), flags);
1000ae74e823SMartin Wilck 		busy_wait = ipmi_thread_busy_wait(smi_result, smi_info,
1001ae74e823SMartin Wilck 						  &busy_until);
1002340ff31aSCorey Minyard 		if (smi_result == SI_SM_CALL_WITHOUT_DELAY) {
1003c305e3d3SCorey Minyard 			; /* do nothing */
1004340ff31aSCorey Minyard 		} else if (smi_result == SI_SM_CALL_WITH_DELAY && busy_wait) {
1005340ff31aSCorey Minyard 			/*
1006340ff31aSCorey Minyard 			 * In maintenance mode we run as fast as
1007340ff31aSCorey Minyard 			 * possible to allow firmware updates to
1008340ff31aSCorey Minyard 			 * complete as fast as possible, but normally
1009340ff31aSCorey Minyard 			 * don't bang on the scheduler.
1010340ff31aSCorey Minyard 			 */
1011340ff31aSCorey Minyard 			if (smi_info->in_maintenance_mode)
101233979734Sakpm@osdl.org 				schedule();
1013340ff31aSCorey Minyard 			else
1014340ff31aSCorey Minyard 				usleep_range(100, 200);
1015340ff31aSCorey Minyard 		} else if (smi_result == SI_SM_IDLE) {
101689986496SCorey Minyard 			if (atomic_read(&smi_info->need_watch)) {
10173326f4f2SMatthew Garrett 				schedule_timeout_interruptible(100);
101889986496SCorey Minyard 			} else {
101989986496SCorey Minyard 				/* Wait to be woken up when we are needed. */
102089986496SCorey Minyard 				__set_current_state(TASK_INTERRUPTIBLE);
102189986496SCorey Minyard 				schedule();
102289986496SCorey Minyard 			}
1023340ff31aSCorey Minyard 		} else {
10248d1f66dcSMartin Wilck 			schedule_timeout_interruptible(1);
1025a9a2c44fSCorey Minyard 		}
1026340ff31aSCorey Minyard 	}
1027a9a2c44fSCorey Minyard 	return 0;
1028a9a2c44fSCorey Minyard }
1029a9a2c44fSCorey Minyard 
1030a9a2c44fSCorey Minyard 
poll(void * send_info)10311da177e4SLinus Torvalds static void poll(void *send_info)
10321da177e4SLinus Torvalds {
10331da177e4SLinus Torvalds 	struct smi_info *smi_info = send_info;
1034f60adf42SCorey Minyard 	unsigned long flags = 0;
10357aefac26SCorey Minyard 	bool run_to_completion = smi_info->run_to_completion;
10361da177e4SLinus Torvalds 
103715c62e10SCorey Minyard 	/*
103815c62e10SCorey Minyard 	 * Make sure there is some delay in the poll loop so we can
103915c62e10SCorey Minyard 	 * drive time forward and timeout things.
104015c62e10SCorey Minyard 	 */
104115c62e10SCorey Minyard 	udelay(10);
1042f60adf42SCorey Minyard 	if (!run_to_completion)
1043fcfa4724SCorey Minyard 		spin_lock_irqsave(&smi_info->si_lock, flags);
104415c62e10SCorey Minyard 	smi_event_handler(smi_info, 10);
1045f60adf42SCorey Minyard 	if (!run_to_completion)
1046fcfa4724SCorey Minyard 		spin_unlock_irqrestore(&smi_info->si_lock, flags);
10471da177e4SLinus Torvalds }
10481da177e4SLinus Torvalds 
request_events(void * send_info)10491da177e4SLinus Torvalds static void request_events(void *send_info)
10501da177e4SLinus Torvalds {
10511da177e4SLinus Torvalds 	struct smi_info *smi_info = send_info;
10521da177e4SLinus Torvalds 
1053b874b985SCorey Minyard 	if (!smi_info->has_event_buffer)
1054b361e27bSCorey Minyard 		return;
1055b361e27bSCorey Minyard 
10561da177e4SLinus Torvalds 	atomic_set(&smi_info->req_events, 1);
10571da177e4SLinus Torvalds }
10581da177e4SLinus Torvalds 
set_need_watch(void * send_info,unsigned int watch_mask)1059c65ea996SCorey Minyard static void set_need_watch(void *send_info, unsigned int watch_mask)
106089986496SCorey Minyard {
106189986496SCorey Minyard 	struct smi_info *smi_info = send_info;
106289986496SCorey Minyard 	unsigned long flags;
1063c65ea996SCorey Minyard 	int enable;
1064c65ea996SCorey Minyard 
1065e1891cffSCorey Minyard 	enable = !!watch_mask;
106689986496SCorey Minyard 
106789986496SCorey Minyard 	atomic_set(&smi_info->need_watch, enable);
106889986496SCorey Minyard 	spin_lock_irqsave(&smi_info->si_lock, flags);
106989986496SCorey Minyard 	check_start_timer_thread(smi_info);
107089986496SCorey Minyard 	spin_unlock_irqrestore(&smi_info->si_lock, flags);
107189986496SCorey Minyard }
107289986496SCorey Minyard 
smi_timeout(struct timer_list * t)1073e99e88a9SKees Cook static void smi_timeout(struct timer_list *t)
10741da177e4SLinus Torvalds {
1075e99e88a9SKees Cook 	struct smi_info   *smi_info = from_timer(smi_info, t, si_timer);
10761da177e4SLinus Torvalds 	enum si_sm_result smi_result;
10771da177e4SLinus Torvalds 	unsigned long     flags;
10781da177e4SLinus Torvalds 	unsigned long     jiffies_now;
1079c4edff1cSCorey Minyard 	long              time_diff;
10803326f4f2SMatthew Garrett 	long		  timeout;
10811da177e4SLinus Torvalds 
10821da177e4SLinus Torvalds 	spin_lock_irqsave(&(smi_info->si_lock), flags);
1083be850359SCorey Minyard 	debug_timestamp(smi_info, "Timer");
1084f93aae9fSJohn Stultz 
10851da177e4SLinus Torvalds 	jiffies_now = jiffies;
1086c4edff1cSCorey Minyard 	time_diff = (((long)jiffies_now - (long)smi_info->last_timeout_jiffies)
10871da177e4SLinus Torvalds 		     * SI_USEC_PER_JIFFY);
10881da177e4SLinus Torvalds 	smi_result = smi_event_handler(smi_info, time_diff);
10891da177e4SLinus Torvalds 
1090910840f2SCorey Minyard 	if ((smi_info->io.irq) && (!smi_info->interrupt_disabled)) {
10911da177e4SLinus Torvalds 		/* Running with interrupts, only do long timeouts. */
10923326f4f2SMatthew Garrett 		timeout = jiffies + SI_TIMEOUT_JIFFIES;
109364959e2dSCorey Minyard 		smi_inc_stat(smi_info, long_timeouts);
10943326f4f2SMatthew Garrett 		goto do_mod_timer;
10951da177e4SLinus Torvalds 	}
10961da177e4SLinus Torvalds 
1097c305e3d3SCorey Minyard 	/*
1098c305e3d3SCorey Minyard 	 * If the state machine asks for a short delay, then shorten
1099c305e3d3SCorey Minyard 	 * the timer timeout.
1100c305e3d3SCorey Minyard 	 */
11011da177e4SLinus Torvalds 	if (smi_result == SI_SM_CALL_WITH_DELAY) {
110264959e2dSCorey Minyard 		smi_inc_stat(smi_info, short_timeouts);
11033326f4f2SMatthew Garrett 		timeout = jiffies + 1;
11041da177e4SLinus Torvalds 	} else {
110564959e2dSCorey Minyard 		smi_inc_stat(smi_info, long_timeouts);
11063326f4f2SMatthew Garrett 		timeout = jiffies + SI_TIMEOUT_JIFFIES;
11071da177e4SLinus Torvalds 	}
11081da177e4SLinus Torvalds 
11093326f4f2SMatthew Garrett do_mod_timer:
11103326f4f2SMatthew Garrett 	if (smi_result != SI_SM_IDLE)
111148e8ac29SBodo Stroesser 		smi_mod_timer(smi_info, timeout);
111248e8ac29SBodo Stroesser 	else
111348e8ac29SBodo Stroesser 		smi_info->timer_running = false;
111448e8ac29SBodo Stroesser 	spin_unlock_irqrestore(&(smi_info->si_lock), flags);
11151da177e4SLinus Torvalds }
11161da177e4SLinus Torvalds 
ipmi_si_irq_handler(int irq,void * data)11174f3e8199SCorey Minyard irqreturn_t ipmi_si_irq_handler(int irq, void *data)
11181da177e4SLinus Torvalds {
11191da177e4SLinus Torvalds 	struct smi_info *smi_info = data;
11201da177e4SLinus Torvalds 	unsigned long   flags;
11211da177e4SLinus Torvalds 
11224f3e8199SCorey Minyard 	if (smi_info->io.si_type == SI_BT)
11234f3e8199SCorey Minyard 		/* We need to clear the IRQ flag for the BT interface. */
11244f3e8199SCorey Minyard 		smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG,
11254f3e8199SCorey Minyard 				     IPMI_BT_INTMASK_CLEAR_IRQ_BIT
11264f3e8199SCorey Minyard 				     | IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
11274f3e8199SCorey Minyard 
11281da177e4SLinus Torvalds 	spin_lock_irqsave(&(smi_info->si_lock), flags);
11291da177e4SLinus Torvalds 
113064959e2dSCorey Minyard 	smi_inc_stat(smi_info, interrupts);
11311da177e4SLinus Torvalds 
1132be850359SCorey Minyard 	debug_timestamp(smi_info, "Interrupt");
1133f93aae9fSJohn Stultz 
11341da177e4SLinus Torvalds 	smi_event_handler(smi_info, 0);
11351da177e4SLinus Torvalds 	spin_unlock_irqrestore(&(smi_info->si_lock), flags);
11361da177e4SLinus Torvalds 	return IRQ_HANDLED;
11371da177e4SLinus Torvalds }
11381da177e4SLinus Torvalds 
smi_start_processing(void * send_info,struct ipmi_smi * intf)1139453823baSCorey Minyard static int smi_start_processing(void            *send_info,
1140a567b623SCorey Minyard 				struct ipmi_smi *intf)
1141453823baSCorey Minyard {
1142453823baSCorey Minyard 	struct smi_info *new_smi = send_info;
1143a51f4a81SCorey Minyard 	int             enable = 0;
1144453823baSCorey Minyard 
1145453823baSCorey Minyard 	new_smi->intf = intf;
1146453823baSCorey Minyard 
1147453823baSCorey Minyard 	/* Set up the timer that drives the interface. */
1148e99e88a9SKees Cook 	timer_setup(&new_smi->si_timer, smi_timeout, 0);
11494f7f5551SMasamitsu Yamazaki 	new_smi->timer_can_start = true;
115048e8ac29SBodo Stroesser 	smi_mod_timer(new_smi, jiffies + SI_TIMEOUT_JIFFIES);
1151453823baSCorey Minyard 
115227f972d3SJan Stancek 	/* Try to claim any interrupts. */
11534f3e8199SCorey Minyard 	if (new_smi->io.irq_setup) {
11544f3e8199SCorey Minyard 		new_smi->io.irq_handler_data = new_smi;
11554f3e8199SCorey Minyard 		new_smi->io.irq_setup(&new_smi->io);
11564f3e8199SCorey Minyard 	}
115727f972d3SJan Stancek 
1158df3fe8deSCorey Minyard 	/*
1159a51f4a81SCorey Minyard 	 * Check if the user forcefully enabled the daemon.
1160a51f4a81SCorey Minyard 	 */
116157bccb4eSCorey Minyard 	if (new_smi->si_num < num_force_kipmid)
116257bccb4eSCorey Minyard 		enable = force_kipmid[new_smi->si_num];
1163a51f4a81SCorey Minyard 	/*
1164df3fe8deSCorey Minyard 	 * The BT interface is efficient enough to not need a thread,
1165df3fe8deSCorey Minyard 	 * and there is no need for a thread if we have interrupts.
1166df3fe8deSCorey Minyard 	 */
1167910840f2SCorey Minyard 	else if ((new_smi->io.si_type != SI_BT) && (!new_smi->io.irq))
1168a51f4a81SCorey Minyard 		enable = 1;
1169a51f4a81SCorey Minyard 
1170a51f4a81SCorey Minyard 	if (enable) {
1171453823baSCorey Minyard 		new_smi->thread = kthread_run(ipmi_thread, new_smi,
117257bccb4eSCorey Minyard 					      "kipmi%d", new_smi->si_num);
1173453823baSCorey Minyard 		if (IS_ERR(new_smi->thread)) {
117407cbd87bSAndy Shevchenko 			dev_notice(new_smi->io.dev,
117507cbd87bSAndy Shevchenko 				   "Could not start kernel thread due to error %ld, only using timers to drive the interface\n",
1176453823baSCorey Minyard 				   PTR_ERR(new_smi->thread));
1177453823baSCorey Minyard 			new_smi->thread = NULL;
1178453823baSCorey Minyard 		}
1179453823baSCorey Minyard 	}
1180453823baSCorey Minyard 
1181453823baSCorey Minyard 	return 0;
1182453823baSCorey Minyard }
11839dbf68f9SCorey Minyard 
get_smi_info(void * send_info,struct ipmi_smi_info * data)118416f4232cSZhao Yakui static int get_smi_info(void *send_info, struct ipmi_smi_info *data)
118516f4232cSZhao Yakui {
118616f4232cSZhao Yakui 	struct smi_info *smi = send_info;
118716f4232cSZhao Yakui 
1188910840f2SCorey Minyard 	data->addr_src = smi->io.addr_source;
1189910840f2SCorey Minyard 	data->dev = smi->io.dev;
1190bb398a4cSCorey Minyard 	data->addr_info = smi->io.addr_info;
1191910840f2SCorey Minyard 	get_device(smi->io.dev);
119216f4232cSZhao Yakui 
119316f4232cSZhao Yakui 	return 0;
119416f4232cSZhao Yakui }
119516f4232cSZhao Yakui 
set_maintenance_mode(void * send_info,bool enable)11967aefac26SCorey Minyard static void set_maintenance_mode(void *send_info, bool enable)
1197b9675136SCorey Minyard {
1198b9675136SCorey Minyard 	struct smi_info   *smi_info = send_info;
1199b9675136SCorey Minyard 
1200b9675136SCorey Minyard 	if (!enable)
1201b9675136SCorey Minyard 		atomic_set(&smi_info->req_events, 0);
1202340ff31aSCorey Minyard 	smi_info->in_maintenance_mode = enable;
1203b9675136SCorey Minyard }
1204b9675136SCorey Minyard 
12057960f18aSCorey Minyard static void shutdown_smi(void *send_info);
120681d02b7fSCorey Minyard static const struct ipmi_smi_handlers handlers = {
12071da177e4SLinus Torvalds 	.owner                  = THIS_MODULE,
1208453823baSCorey Minyard 	.start_processing       = smi_start_processing,
12097960f18aSCorey Minyard 	.shutdown               = shutdown_smi,
121016f4232cSZhao Yakui 	.get_smi_info		= get_smi_info,
12111da177e4SLinus Torvalds 	.sender			= sender,
12121da177e4SLinus Torvalds 	.request_events		= request_events,
121389986496SCorey Minyard 	.set_need_watch		= set_need_watch,
1214b9675136SCorey Minyard 	.set_maintenance_mode   = set_maintenance_mode,
12151da177e4SLinus Torvalds 	.set_run_to_completion  = set_run_to_completion,
121682802f96SHidehiro Kawai 	.flush_messages		= flush_messages,
12171da177e4SLinus Torvalds 	.poll			= poll,
12181da177e4SLinus Torvalds };
12191da177e4SLinus Torvalds 
1220b0defcdbSCorey Minyard static LIST_HEAD(smi_infos);
1221d6dfd131SCorey Minyard static DEFINE_MUTEX(smi_infos_lock);
1222b0defcdbSCorey Minyard static int smi_num; /* Used to sequence the SMIs */
12231da177e4SLinus Torvalds 
122499ee6735SLABBE Corentin static const char * const addr_space_to_str[] = { "i/o", "mem" };
1225b361e27bSCorey Minyard 
1226a51f4a81SCorey Minyard module_param_array(force_kipmid, int, &num_force_kipmid, 0);
122707cbd87bSAndy Shevchenko MODULE_PARM_DESC(force_kipmid,
122807cbd87bSAndy Shevchenko 		 "Force the kipmi daemon to be enabled (1) or disabled(0).  Normally the IPMI driver auto-detects this, but the value may be overridden by this parm.");
12297aefac26SCorey Minyard module_param(unload_when_empty, bool, 0);
123007cbd87bSAndy Shevchenko MODULE_PARM_DESC(unload_when_empty,
123107cbd87bSAndy Shevchenko 		 "Unload the module if no interfaces are specified or found, default is 1.  Setting to 0 is useful for hot add of devices using hotmod.");
1232ae74e823SMartin Wilck module_param_array(kipmid_max_busy_us, uint, &num_max_busy_us, 0644);
1233ae74e823SMartin Wilck MODULE_PARM_DESC(kipmid_max_busy_us,
123407cbd87bSAndy Shevchenko 		 "Max time (in microseconds) to busy-wait for IPMI data before sleeping. 0 (default) means to wait forever. Set to 100-500 if kipmid is using up a lot of CPU time.");
12351da177e4SLinus Torvalds 
ipmi_irq_finish_setup(struct si_sm_io * io)12364f3e8199SCorey Minyard void ipmi_irq_finish_setup(struct si_sm_io *io)
12371da177e4SLinus Torvalds {
12384f3e8199SCorey Minyard 	if (io->si_type == SI_BT)
12394f3e8199SCorey Minyard 		/* Enable the interrupt in the BT interface. */
12404f3e8199SCorey Minyard 		io->outputb(io, IPMI_BT_INTMASK_REG,
12414f3e8199SCorey Minyard 			    IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
12421da177e4SLinus Torvalds }
12431da177e4SLinus Torvalds 
ipmi_irq_start_cleanup(struct si_sm_io * io)12444f3e8199SCorey Minyard void ipmi_irq_start_cleanup(struct si_sm_io *io)
12454f3e8199SCorey Minyard {
12464f3e8199SCorey Minyard 	if (io->si_type == SI_BT)
12474f3e8199SCorey Minyard 		/* Disable the interrupt in the BT interface. */
12484f3e8199SCorey Minyard 		io->outputb(io, IPMI_BT_INTMASK_REG, 0);
12494f3e8199SCorey Minyard }
12504f3e8199SCorey Minyard 
std_irq_cleanup(struct si_sm_io * io)12514f3e8199SCorey Minyard static void std_irq_cleanup(struct si_sm_io *io)
12524f3e8199SCorey Minyard {
12534f3e8199SCorey Minyard 	ipmi_irq_start_cleanup(io);
12544f3e8199SCorey Minyard 	free_irq(io->irq, io->irq_handler_data);
12554f3e8199SCorey Minyard }
12564f3e8199SCorey Minyard 
ipmi_std_irq_setup(struct si_sm_io * io)12574f3e8199SCorey Minyard int ipmi_std_irq_setup(struct si_sm_io *io)
12581da177e4SLinus Torvalds {
12591da177e4SLinus Torvalds 	int rv;
12601da177e4SLinus Torvalds 
12614f3e8199SCorey Minyard 	if (!io->irq)
12621da177e4SLinus Torvalds 		return 0;
12631da177e4SLinus Torvalds 
12644f3e8199SCorey Minyard 	rv = request_irq(io->irq,
12654f3e8199SCorey Minyard 			 ipmi_si_irq_handler,
1266aa5b2babSMichael Opdenacker 			 IRQF_SHARED,
1267104fb25fSCorey Minyard 			 SI_DEVICE_NAME,
12684f3e8199SCorey Minyard 			 io->irq_handler_data);
12691da177e4SLinus Torvalds 	if (rv) {
127007cbd87bSAndy Shevchenko 		dev_warn(io->dev, "%s unable to claim interrupt %d, running polled\n",
1271104fb25fSCorey Minyard 			 SI_DEVICE_NAME, io->irq);
12724f3e8199SCorey Minyard 		io->irq = 0;
12731da177e4SLinus Torvalds 	} else {
12744f3e8199SCorey Minyard 		io->irq_cleanup = std_irq_cleanup;
12754f3e8199SCorey Minyard 		ipmi_irq_finish_setup(io);
12764f3e8199SCorey Minyard 		dev_info(io->dev, "Using irq %d\n", io->irq);
12771da177e4SLinus Torvalds 	}
12781da177e4SLinus Torvalds 
12791da177e4SLinus Torvalds 	return rv;
12801da177e4SLinus Torvalds }
12811da177e4SLinus Torvalds 
wait_for_msg_done(struct smi_info * smi_info)128240112ae7SCorey Minyard static int wait_for_msg_done(struct smi_info *smi_info)
12831da177e4SLinus Torvalds {
12841da177e4SLinus Torvalds 	enum si_sm_result     smi_result;
12851da177e4SLinus Torvalds 
12861da177e4SLinus Torvalds 	smi_result = smi_info->handlers->event(smi_info->si_sm, 0);
1287c305e3d3SCorey Minyard 	for (;;) {
1288c3e7e791SCorey Minyard 		if (smi_result == SI_SM_CALL_WITH_DELAY ||
1289c3e7e791SCorey Minyard 		    smi_result == SI_SM_CALL_WITH_TICK_DELAY) {
1290da4cd8dfSNishanth Aravamudan 			schedule_timeout_uninterruptible(1);
12911da177e4SLinus Torvalds 			smi_result = smi_info->handlers->event(
1292e21404dcSXie XiuQi 				smi_info->si_sm, jiffies_to_usecs(1));
1293c305e3d3SCorey Minyard 		} else if (smi_result == SI_SM_CALL_WITHOUT_DELAY) {
12941da177e4SLinus Torvalds 			smi_result = smi_info->handlers->event(
12951da177e4SLinus Torvalds 				smi_info->si_sm, 0);
1296c305e3d3SCorey Minyard 		} else
12971da177e4SLinus Torvalds 			break;
12981da177e4SLinus Torvalds 	}
129940112ae7SCorey Minyard 	if (smi_result == SI_SM_HOSED)
1300c305e3d3SCorey Minyard 		/*
1301c305e3d3SCorey Minyard 		 * We couldn't get the state machine to run, so whatever's at
1302c305e3d3SCorey Minyard 		 * the port is probably not an IPMI SMI interface.
1303c305e3d3SCorey Minyard 		 */
130440112ae7SCorey Minyard 		return -ENODEV;
130540112ae7SCorey Minyard 
130640112ae7SCorey Minyard 	return 0;
13071da177e4SLinus Torvalds }
13081da177e4SLinus Torvalds 
try_get_dev_id(struct smi_info * smi_info)130940112ae7SCorey Minyard static int try_get_dev_id(struct smi_info *smi_info)
131040112ae7SCorey Minyard {
131140112ae7SCorey Minyard 	unsigned char         msg[2];
131240112ae7SCorey Minyard 	unsigned char         *resp;
131340112ae7SCorey Minyard 	unsigned long         resp_len;
131440112ae7SCorey Minyard 	int                   rv = 0;
131542d8a346SXianting Tian 	unsigned int          retry_count = 0;
131640112ae7SCorey Minyard 
131740112ae7SCorey Minyard 	resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
131840112ae7SCorey Minyard 	if (!resp)
131940112ae7SCorey Minyard 		return -ENOMEM;
132040112ae7SCorey Minyard 
132140112ae7SCorey Minyard 	/*
132240112ae7SCorey Minyard 	 * Do a Get Device ID command, since it comes back with some
132340112ae7SCorey Minyard 	 * useful info.
132440112ae7SCorey Minyard 	 */
132540112ae7SCorey Minyard 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
132640112ae7SCorey Minyard 	msg[1] = IPMI_GET_DEVICE_ID_CMD;
132742d8a346SXianting Tian 
132842d8a346SXianting Tian retry:
132940112ae7SCorey Minyard 	smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
133040112ae7SCorey Minyard 
133140112ae7SCorey Minyard 	rv = wait_for_msg_done(smi_info);
133240112ae7SCorey Minyard 	if (rv)
133340112ae7SCorey Minyard 		goto out;
133440112ae7SCorey Minyard 
13351da177e4SLinus Torvalds 	resp_len = smi_info->handlers->get_result(smi_info->si_sm,
13361da177e4SLinus Torvalds 						  resp, IPMI_MAX_MSG_LENGTH);
13371da177e4SLinus Torvalds 
1338d8c98618SCorey Minyard 	/* Check and record info from the get device id, in case we need it. */
1339c468f911SJeremy Kerr 	rv = ipmi_demangle_device_id(resp[0] >> 2, resp[1],
1340c468f911SJeremy Kerr 			resp + 2, resp_len - 2, &smi_info->device_id);
134142d8a346SXianting Tian 	if (rv) {
134242d8a346SXianting Tian 		/* record completion code */
1343c011410dSDan Carpenter 		unsigned char cc = *(resp + 2);
134442d8a346SXianting Tian 
1345c6ddd5f1STerry Duncan 		if (cc != IPMI_CC_NO_ERROR &&
1346c6ddd5f1STerry Duncan 		    ++retry_count <= GET_DEVICE_ID_MAX_RETRY) {
1347ca8c1c53SWen Yang 			dev_warn_ratelimited(smi_info->io.dev,
134842d8a346SXianting Tian 			    "BMC returned 0x%2.2x, retry get bmc device id\n",
134942d8a346SXianting Tian 			    cc);
135042d8a346SXianting Tian 			goto retry;
135142d8a346SXianting Tian 		}
135242d8a346SXianting Tian 	}
13531da177e4SLinus Torvalds 
13541da177e4SLinus Torvalds out:
13551da177e4SLinus Torvalds 	kfree(resp);
13561da177e4SLinus Torvalds 	return rv;
13571da177e4SLinus Torvalds }
13581da177e4SLinus Torvalds 
get_global_enables(struct smi_info * smi_info,u8 * enables)1359d0882897SCorey Minyard static int get_global_enables(struct smi_info *smi_info, u8 *enables)
13601e7d6a45SCorey Minyard {
13611e7d6a45SCorey Minyard 	unsigned char         msg[3];
13621e7d6a45SCorey Minyard 	unsigned char         *resp;
13631e7d6a45SCorey Minyard 	unsigned long         resp_len;
13641e7d6a45SCorey Minyard 	int                   rv;
13651e7d6a45SCorey Minyard 
13661e7d6a45SCorey Minyard 	resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1367d0882897SCorey Minyard 	if (!resp)
1368d0882897SCorey Minyard 		return -ENOMEM;
13691e7d6a45SCorey Minyard 
13701e7d6a45SCorey Minyard 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
13711e7d6a45SCorey Minyard 	msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
13721e7d6a45SCorey Minyard 	smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
13731e7d6a45SCorey Minyard 
13741e7d6a45SCorey Minyard 	rv = wait_for_msg_done(smi_info);
13751e7d6a45SCorey Minyard 	if (rv) {
1376910840f2SCorey Minyard 		dev_warn(smi_info->io.dev,
1377d0882897SCorey Minyard 			 "Error getting response from get global enables command: %d\n",
1378d0882897SCorey Minyard 			 rv);
13791e7d6a45SCorey Minyard 		goto out;
13801e7d6a45SCorey Minyard 	}
13811e7d6a45SCorey Minyard 
13821e7d6a45SCorey Minyard 	resp_len = smi_info->handlers->get_result(smi_info->si_sm,
13831e7d6a45SCorey Minyard 						  resp, IPMI_MAX_MSG_LENGTH);
13841e7d6a45SCorey Minyard 
13851e7d6a45SCorey Minyard 	if (resp_len < 4 ||
13861e7d6a45SCorey Minyard 			resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
13871e7d6a45SCorey Minyard 			resp[1] != IPMI_GET_BMC_GLOBAL_ENABLES_CMD   ||
13881e7d6a45SCorey Minyard 			resp[2] != 0) {
1389910840f2SCorey Minyard 		dev_warn(smi_info->io.dev,
1390d0882897SCorey Minyard 			 "Invalid return from get global enables command: %ld %x %x %x\n",
1391d0882897SCorey Minyard 			 resp_len, resp[0], resp[1], resp[2]);
13921e7d6a45SCorey Minyard 		rv = -EINVAL;
13931e7d6a45SCorey Minyard 		goto out;
1394d0882897SCorey Minyard 	} else {
1395d0882897SCorey Minyard 		*enables = resp[3];
13961e7d6a45SCorey Minyard 	}
13971e7d6a45SCorey Minyard 
1398d0882897SCorey Minyard out:
1399d0882897SCorey Minyard 	kfree(resp);
1400d0882897SCorey Minyard 	return rv;
1401d0882897SCorey Minyard }
1402d0882897SCorey Minyard 
1403d0882897SCorey Minyard /*
1404d0882897SCorey Minyard  * Returns 1 if it gets an error from the command.
1405d0882897SCorey Minyard  */
set_global_enables(struct smi_info * smi_info,u8 enables)1406d0882897SCorey Minyard static int set_global_enables(struct smi_info *smi_info, u8 enables)
1407d0882897SCorey Minyard {
1408d0882897SCorey Minyard 	unsigned char         msg[3];
1409d0882897SCorey Minyard 	unsigned char         *resp;
1410d0882897SCorey Minyard 	unsigned long         resp_len;
1411d0882897SCorey Minyard 	int                   rv;
1412d0882897SCorey Minyard 
1413d0882897SCorey Minyard 	resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1414d0882897SCorey Minyard 	if (!resp)
1415d0882897SCorey Minyard 		return -ENOMEM;
14161e7d6a45SCorey Minyard 
14171e7d6a45SCorey Minyard 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
14181e7d6a45SCorey Minyard 	msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
1419d0882897SCorey Minyard 	msg[2] = enables;
14201e7d6a45SCorey Minyard 	smi_info->handlers->start_transaction(smi_info->si_sm, msg, 3);
14211e7d6a45SCorey Minyard 
14221e7d6a45SCorey Minyard 	rv = wait_for_msg_done(smi_info);
14231e7d6a45SCorey Minyard 	if (rv) {
1424910840f2SCorey Minyard 		dev_warn(smi_info->io.dev,
1425d0882897SCorey Minyard 			 "Error getting response from set global enables command: %d\n",
1426d0882897SCorey Minyard 			 rv);
14271e7d6a45SCorey Minyard 		goto out;
14281e7d6a45SCorey Minyard 	}
14291e7d6a45SCorey Minyard 
14301e7d6a45SCorey Minyard 	resp_len = smi_info->handlers->get_result(smi_info->si_sm,
14311e7d6a45SCorey Minyard 						  resp, IPMI_MAX_MSG_LENGTH);
14321e7d6a45SCorey Minyard 
14331e7d6a45SCorey Minyard 	if (resp_len < 3 ||
14341e7d6a45SCorey Minyard 			resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
14351e7d6a45SCorey Minyard 			resp[1] != IPMI_SET_BMC_GLOBAL_ENABLES_CMD) {
1436910840f2SCorey Minyard 		dev_warn(smi_info->io.dev,
1437d0882897SCorey Minyard 			 "Invalid return from set global enables command: %ld %x %x\n",
1438d0882897SCorey Minyard 			 resp_len, resp[0], resp[1]);
14391e7d6a45SCorey Minyard 		rv = -EINVAL;
14401e7d6a45SCorey Minyard 		goto out;
14411e7d6a45SCorey Minyard 	}
14421e7d6a45SCorey Minyard 
1443d0882897SCorey Minyard 	if (resp[2] != 0)
1444d0882897SCorey Minyard 		rv = 1;
1445d0882897SCorey Minyard 
1446d0882897SCorey Minyard out:
1447d0882897SCorey Minyard 	kfree(resp);
1448d0882897SCorey Minyard 	return rv;
1449d0882897SCorey Minyard }
1450d0882897SCorey Minyard 
1451d0882897SCorey Minyard /*
1452d0882897SCorey Minyard  * Some BMCs do not support clearing the receive irq bit in the global
1453d0882897SCorey Minyard  * enables (even if they don't support interrupts on the BMC).  Check
1454d0882897SCorey Minyard  * for this and handle it properly.
1455d0882897SCorey Minyard  */
check_clr_rcv_irq(struct smi_info * smi_info)1456d0882897SCorey Minyard static void check_clr_rcv_irq(struct smi_info *smi_info)
1457d0882897SCorey Minyard {
1458d0882897SCorey Minyard 	u8 enables = 0;
1459d0882897SCorey Minyard 	int rv;
1460d0882897SCorey Minyard 
1461d0882897SCorey Minyard 	rv = get_global_enables(smi_info, &enables);
1462d0882897SCorey Minyard 	if (!rv) {
1463d0882897SCorey Minyard 		if ((enables & IPMI_BMC_RCV_MSG_INTR) == 0)
1464d0882897SCorey Minyard 			/* Already clear, should work ok. */
1465d0882897SCorey Minyard 			return;
1466d0882897SCorey Minyard 
1467d0882897SCorey Minyard 		enables &= ~IPMI_BMC_RCV_MSG_INTR;
1468d0882897SCorey Minyard 		rv = set_global_enables(smi_info, enables);
1469d0882897SCorey Minyard 	}
1470d0882897SCorey Minyard 
1471d0882897SCorey Minyard 	if (rv < 0) {
1472910840f2SCorey Minyard 		dev_err(smi_info->io.dev,
1473d0882897SCorey Minyard 			"Cannot check clearing the rcv irq: %d\n", rv);
1474d0882897SCorey Minyard 		return;
1475d0882897SCorey Minyard 	}
1476d0882897SCorey Minyard 
1477d0882897SCorey Minyard 	if (rv) {
14781e7d6a45SCorey Minyard 		/*
14791e7d6a45SCorey Minyard 		 * An error when setting the event buffer bit means
14801e7d6a45SCorey Minyard 		 * clearing the bit is not supported.
14811e7d6a45SCorey Minyard 		 */
1482910840f2SCorey Minyard 		dev_warn(smi_info->io.dev,
1483d0882897SCorey Minyard 			 "The BMC does not support clearing the recv irq bit, compensating, but the BMC needs to be fixed.\n");
1484d0882897SCorey Minyard 		smi_info->cannot_disable_irq = true;
14851e7d6a45SCorey Minyard 	}
1486d0882897SCorey Minyard }
1487d0882897SCorey Minyard 
1488d0882897SCorey Minyard /*
1489d0882897SCorey Minyard  * Some BMCs do not support setting the interrupt bits in the global
1490d0882897SCorey Minyard  * enables even if they support interrupts.  Clearly bad, but we can
1491d0882897SCorey Minyard  * compensate.
1492d0882897SCorey Minyard  */
check_set_rcv_irq(struct smi_info * smi_info)1493d0882897SCorey Minyard static void check_set_rcv_irq(struct smi_info *smi_info)
1494d0882897SCorey Minyard {
1495d0882897SCorey Minyard 	u8 enables = 0;
1496d0882897SCorey Minyard 	int rv;
1497d0882897SCorey Minyard 
1498910840f2SCorey Minyard 	if (!smi_info->io.irq)
1499d0882897SCorey Minyard 		return;
1500d0882897SCorey Minyard 
1501d0882897SCorey Minyard 	rv = get_global_enables(smi_info, &enables);
1502d0882897SCorey Minyard 	if (!rv) {
1503d0882897SCorey Minyard 		enables |= IPMI_BMC_RCV_MSG_INTR;
1504d0882897SCorey Minyard 		rv = set_global_enables(smi_info, enables);
1505d0882897SCorey Minyard 	}
1506d0882897SCorey Minyard 
1507d0882897SCorey Minyard 	if (rv < 0) {
1508910840f2SCorey Minyard 		dev_err(smi_info->io.dev,
1509d0882897SCorey Minyard 			"Cannot check setting the rcv irq: %d\n", rv);
1510d0882897SCorey Minyard 		return;
1511d0882897SCorey Minyard 	}
1512d0882897SCorey Minyard 
1513d0882897SCorey Minyard 	if (rv) {
1514d0882897SCorey Minyard 		/*
1515d0882897SCorey Minyard 		 * An error when setting the event buffer bit means
1516d0882897SCorey Minyard 		 * setting the bit is not supported.
1517d0882897SCorey Minyard 		 */
1518910840f2SCorey Minyard 		dev_warn(smi_info->io.dev,
1519d0882897SCorey Minyard 			 "The BMC does not support setting the recv irq bit, compensating, but the BMC needs to be fixed.\n");
1520d0882897SCorey Minyard 		smi_info->cannot_disable_irq = true;
1521d0882897SCorey Minyard 		smi_info->irq_enable_broken = true;
1522d0882897SCorey Minyard 	}
15231e7d6a45SCorey Minyard }
15241e7d6a45SCorey Minyard 
try_enable_event_buffer(struct smi_info * smi_info)152540112ae7SCorey Minyard static int try_enable_event_buffer(struct smi_info *smi_info)
152640112ae7SCorey Minyard {
152740112ae7SCorey Minyard 	unsigned char         msg[3];
152840112ae7SCorey Minyard 	unsigned char         *resp;
152940112ae7SCorey Minyard 	unsigned long         resp_len;
153040112ae7SCorey Minyard 	int                   rv = 0;
153140112ae7SCorey Minyard 
153240112ae7SCorey Minyard 	resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
153340112ae7SCorey Minyard 	if (!resp)
153440112ae7SCorey Minyard 		return -ENOMEM;
153540112ae7SCorey Minyard 
153640112ae7SCorey Minyard 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
153740112ae7SCorey Minyard 	msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
153840112ae7SCorey Minyard 	smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
153940112ae7SCorey Minyard 
154040112ae7SCorey Minyard 	rv = wait_for_msg_done(smi_info);
154140112ae7SCorey Minyard 	if (rv) {
154225880f7dSJoe Perches 		pr_warn("Error getting response from get global enables command, the event buffer is not enabled\n");
154340112ae7SCorey Minyard 		goto out;
154440112ae7SCorey Minyard 	}
154540112ae7SCorey Minyard 
154640112ae7SCorey Minyard 	resp_len = smi_info->handlers->get_result(smi_info->si_sm,
154740112ae7SCorey Minyard 						  resp, IPMI_MAX_MSG_LENGTH);
154840112ae7SCorey Minyard 
154940112ae7SCorey Minyard 	if (resp_len < 4 ||
155040112ae7SCorey Minyard 			resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
155140112ae7SCorey Minyard 			resp[1] != IPMI_GET_BMC_GLOBAL_ENABLES_CMD   ||
155240112ae7SCorey Minyard 			resp[2] != 0) {
155325880f7dSJoe Perches 		pr_warn("Invalid return from get global enables command, cannot enable the event buffer\n");
155440112ae7SCorey Minyard 		rv = -EINVAL;
155540112ae7SCorey Minyard 		goto out;
155640112ae7SCorey Minyard 	}
155740112ae7SCorey Minyard 
1558d9b7e4f7SCorey Minyard 	if (resp[3] & IPMI_BMC_EVT_MSG_BUFF) {
155940112ae7SCorey Minyard 		/* buffer is already enabled, nothing to do. */
1560d9b7e4f7SCorey Minyard 		smi_info->supports_event_msg_buff = true;
156140112ae7SCorey Minyard 		goto out;
1562d9b7e4f7SCorey Minyard 	}
156340112ae7SCorey Minyard 
156440112ae7SCorey Minyard 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
156540112ae7SCorey Minyard 	msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
156640112ae7SCorey Minyard 	msg[2] = resp[3] | IPMI_BMC_EVT_MSG_BUFF;
156740112ae7SCorey Minyard 	smi_info->handlers->start_transaction(smi_info->si_sm, msg, 3);
156840112ae7SCorey Minyard 
156940112ae7SCorey Minyard 	rv = wait_for_msg_done(smi_info);
157040112ae7SCorey Minyard 	if (rv) {
157125880f7dSJoe Perches 		pr_warn("Error getting response from set global, enables command, the event buffer is not enabled\n");
157240112ae7SCorey Minyard 		goto out;
157340112ae7SCorey Minyard 	}
157440112ae7SCorey Minyard 
157540112ae7SCorey Minyard 	resp_len = smi_info->handlers->get_result(smi_info->si_sm,
157640112ae7SCorey Minyard 						  resp, IPMI_MAX_MSG_LENGTH);
157740112ae7SCorey Minyard 
157840112ae7SCorey Minyard 	if (resp_len < 3 ||
157940112ae7SCorey Minyard 			resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
158040112ae7SCorey Minyard 			resp[1] != IPMI_SET_BMC_GLOBAL_ENABLES_CMD) {
158125880f7dSJoe Perches 		pr_warn("Invalid return from get global, enables command, not enable the event buffer\n");
158240112ae7SCorey Minyard 		rv = -EINVAL;
158340112ae7SCorey Minyard 		goto out;
158440112ae7SCorey Minyard 	}
158540112ae7SCorey Minyard 
158640112ae7SCorey Minyard 	if (resp[2] != 0)
158740112ae7SCorey Minyard 		/*
158840112ae7SCorey Minyard 		 * An error when setting the event buffer bit means
158940112ae7SCorey Minyard 		 * that the event buffer is not supported.
159040112ae7SCorey Minyard 		 */
159140112ae7SCorey Minyard 		rv = -ENOENT;
1592d9b7e4f7SCorey Minyard 	else
1593d9b7e4f7SCorey Minyard 		smi_info->supports_event_msg_buff = true;
1594d9b7e4f7SCorey Minyard 
159540112ae7SCorey Minyard out:
159640112ae7SCorey Minyard 	kfree(resp);
159740112ae7SCorey Minyard 	return rv;
159840112ae7SCorey Minyard }
159940112ae7SCorey Minyard 
16003dd377b5SCorey Minyard #define IPMI_SI_ATTR(name) \
160193b6984bSCorey Minyard static ssize_t name##_show(struct device *dev,			\
16023dd377b5SCorey Minyard 			   struct device_attribute *attr,		\
16033dd377b5SCorey Minyard 			   char *buf)					\
16043dd377b5SCorey Minyard {									\
16053dd377b5SCorey Minyard 	struct smi_info *smi_info = dev_get_drvdata(dev);		\
16063dd377b5SCorey Minyard 									\
1607fc4e7848SYe Guojin 	return sysfs_emit(buf, "%u\n", smi_get_stat(smi_info, name));	\
16083dd377b5SCorey Minyard }									\
1609bf064c7bSDwaipayan Ray static DEVICE_ATTR_RO(name)
16103dd377b5SCorey Minyard 
type_show(struct device * dev,struct device_attribute * attr,char * buf)161193b6984bSCorey Minyard static ssize_t type_show(struct device *dev,
16123dd377b5SCorey Minyard 			 struct device_attribute *attr,
16133dd377b5SCorey Minyard 			 char *buf)
16143dd377b5SCorey Minyard {
16153dd377b5SCorey Minyard 	struct smi_info *smi_info = dev_get_drvdata(dev);
16163dd377b5SCorey Minyard 
1617fc4e7848SYe Guojin 	return sysfs_emit(buf, "%s\n", si_to_str[smi_info->io.si_type]);
16183dd377b5SCorey Minyard }
1619bf064c7bSDwaipayan Ray static DEVICE_ATTR_RO(type);
16203dd377b5SCorey Minyard 
interrupts_enabled_show(struct device * dev,struct device_attribute * attr,char * buf)162193b6984bSCorey Minyard static ssize_t interrupts_enabled_show(struct device *dev,
16223dd377b5SCorey Minyard 				       struct device_attribute *attr,
16233dd377b5SCorey Minyard 				       char *buf)
16243dd377b5SCorey Minyard {
16253dd377b5SCorey Minyard 	struct smi_info *smi_info = dev_get_drvdata(dev);
16263dd377b5SCorey Minyard 	int enabled = smi_info->io.irq && !smi_info->interrupt_disabled;
16273dd377b5SCorey Minyard 
1628fc4e7848SYe Guojin 	return sysfs_emit(buf, "%d\n", enabled);
16293dd377b5SCorey Minyard }
1630bf064c7bSDwaipayan Ray static DEVICE_ATTR_RO(interrupts_enabled);
16313dd377b5SCorey Minyard 
16323dd377b5SCorey Minyard IPMI_SI_ATTR(short_timeouts);
16333dd377b5SCorey Minyard IPMI_SI_ATTR(long_timeouts);
16343dd377b5SCorey Minyard IPMI_SI_ATTR(idles);
16353dd377b5SCorey Minyard IPMI_SI_ATTR(interrupts);
16363dd377b5SCorey Minyard IPMI_SI_ATTR(attentions);
16373dd377b5SCorey Minyard IPMI_SI_ATTR(flag_fetches);
16383dd377b5SCorey Minyard IPMI_SI_ATTR(hosed_count);
16393dd377b5SCorey Minyard IPMI_SI_ATTR(complete_transactions);
16403dd377b5SCorey Minyard IPMI_SI_ATTR(events);
16413dd377b5SCorey Minyard IPMI_SI_ATTR(watchdog_pretimeouts);
16423dd377b5SCorey Minyard IPMI_SI_ATTR(incoming_messages);
16433dd377b5SCorey Minyard 
params_show(struct device * dev,struct device_attribute * attr,char * buf)164493b6984bSCorey Minyard static ssize_t params_show(struct device *dev,
16453dd377b5SCorey Minyard 			   struct device_attribute *attr,
16463dd377b5SCorey Minyard 			   char *buf)
16473dd377b5SCorey Minyard {
16483dd377b5SCorey Minyard 	struct smi_info *smi_info = dev_get_drvdata(dev);
16493dd377b5SCorey Minyard 
1650fc4e7848SYe Guojin 	return sysfs_emit(buf,
16513dd377b5SCorey Minyard 			"%s,%s,0x%lx,rsp=%d,rsi=%d,rsh=%d,irq=%d,ipmb=%d\n",
16523dd377b5SCorey Minyard 			si_to_str[smi_info->io.si_type],
1653f6296bdcSCorey Minyard 			addr_space_to_str[smi_info->io.addr_space],
16543dd377b5SCorey Minyard 			smi_info->io.addr_data,
16553dd377b5SCorey Minyard 			smi_info->io.regspacing,
16563dd377b5SCorey Minyard 			smi_info->io.regsize,
16573dd377b5SCorey Minyard 			smi_info->io.regshift,
16583dd377b5SCorey Minyard 			smi_info->io.irq,
16593dd377b5SCorey Minyard 			smi_info->io.slave_addr);
16603dd377b5SCorey Minyard }
1661bf064c7bSDwaipayan Ray static DEVICE_ATTR_RO(params);
16623dd377b5SCorey Minyard 
16633dd377b5SCorey Minyard static struct attribute *ipmi_si_dev_attrs[] = {
16643dd377b5SCorey Minyard 	&dev_attr_type.attr,
16653dd377b5SCorey Minyard 	&dev_attr_interrupts_enabled.attr,
16663dd377b5SCorey Minyard 	&dev_attr_short_timeouts.attr,
16673dd377b5SCorey Minyard 	&dev_attr_long_timeouts.attr,
16683dd377b5SCorey Minyard 	&dev_attr_idles.attr,
16693dd377b5SCorey Minyard 	&dev_attr_interrupts.attr,
16703dd377b5SCorey Minyard 	&dev_attr_attentions.attr,
16713dd377b5SCorey Minyard 	&dev_attr_flag_fetches.attr,
16723dd377b5SCorey Minyard 	&dev_attr_hosed_count.attr,
16733dd377b5SCorey Minyard 	&dev_attr_complete_transactions.attr,
16743dd377b5SCorey Minyard 	&dev_attr_events.attr,
16753dd377b5SCorey Minyard 	&dev_attr_watchdog_pretimeouts.attr,
16763dd377b5SCorey Minyard 	&dev_attr_incoming_messages.attr,
16773dd377b5SCorey Minyard 	&dev_attr_params.attr,
16783dd377b5SCorey Minyard 	NULL
16793dd377b5SCorey Minyard };
16803dd377b5SCorey Minyard 
16813dd377b5SCorey Minyard static const struct attribute_group ipmi_si_dev_attr_group = {
16823dd377b5SCorey Minyard 	.attrs		= ipmi_si_dev_attrs,
16833dd377b5SCorey Minyard };
16843dd377b5SCorey Minyard 
16853ae0e0f9SCorey Minyard /*
16863ae0e0f9SCorey Minyard  * oem_data_avail_to_receive_msg_avail
16873ae0e0f9SCorey Minyard  * @info - smi_info structure with msg_flags set
16883ae0e0f9SCorey Minyard  *
16893ae0e0f9SCorey Minyard  * Converts flags from OEM_DATA_AVAIL to RECEIVE_MSG_AVAIL
16903ae0e0f9SCorey Minyard  * Returns 1 indicating need to re-run handle_flags().
16913ae0e0f9SCorey Minyard  */
oem_data_avail_to_receive_msg_avail(struct smi_info * smi_info)16923ae0e0f9SCorey Minyard static int oem_data_avail_to_receive_msg_avail(struct smi_info *smi_info)
16933ae0e0f9SCorey Minyard {
1694e8b33617SCorey Minyard 	smi_info->msg_flags = ((smi_info->msg_flags & ~OEM_DATA_AVAIL) |
1695e8b33617SCorey Minyard 			       RECEIVE_MSG_AVAIL);
16963ae0e0f9SCorey Minyard 	return 1;
16973ae0e0f9SCorey Minyard }
16983ae0e0f9SCorey Minyard 
16993ae0e0f9SCorey Minyard /*
17003ae0e0f9SCorey Minyard  * setup_dell_poweredge_oem_data_handler
17013ae0e0f9SCorey Minyard  * @info - smi_info.device_id must be populated
17023ae0e0f9SCorey Minyard  *
17033ae0e0f9SCorey Minyard  * Systems that match, but have firmware version < 1.40 may assert
17043ae0e0f9SCorey Minyard  * OEM0_DATA_AVAIL on their own, without being told via Set Flags that
17053ae0e0f9SCorey Minyard  * it's safe to do so.  Such systems will de-assert OEM1_DATA_AVAIL
17063ae0e0f9SCorey Minyard  * upon receipt of IPMI_GET_MSG_CMD, so we should treat these flags
17073ae0e0f9SCorey Minyard  * as RECEIVE_MSG_AVAIL instead.
17083ae0e0f9SCorey Minyard  *
17093ae0e0f9SCorey Minyard  * As Dell has no plans to release IPMI 1.5 firmware that *ever*
17103ae0e0f9SCorey Minyard  * assert the OEM[012] bits, and if it did, the driver would have to
17113ae0e0f9SCorey Minyard  * change to handle that properly, we don't actually check for the
17123ae0e0f9SCorey Minyard  * firmware version.
17133ae0e0f9SCorey Minyard  * Device ID = 0x20                BMC on PowerEdge 8G servers
17143ae0e0f9SCorey Minyard  * Device Revision = 0x80
17153ae0e0f9SCorey Minyard  * Firmware Revision1 = 0x01       BMC version 1.40
17163ae0e0f9SCorey Minyard  * Firmware Revision2 = 0x40       BCD encoded
17173ae0e0f9SCorey Minyard  * IPMI Version = 0x51             IPMI 1.5
17183ae0e0f9SCorey Minyard  * Manufacturer ID = A2 02 00      Dell IANA
17193ae0e0f9SCorey Minyard  *
1720d5a2b89aSCorey Minyard  * Additionally, PowerEdge systems with IPMI < 1.5 may also assert
1721d5a2b89aSCorey Minyard  * OEM0_DATA_AVAIL and needs to be treated as RECEIVE_MSG_AVAIL.
1722d5a2b89aSCorey Minyard  *
17233ae0e0f9SCorey Minyard  */
17243ae0e0f9SCorey Minyard #define DELL_POWEREDGE_8G_BMC_DEVICE_ID  0x20
17253ae0e0f9SCorey Minyard #define DELL_POWEREDGE_8G_BMC_DEVICE_REV 0x80
17263ae0e0f9SCorey Minyard #define DELL_POWEREDGE_8G_BMC_IPMI_VERSION 0x51
172750c812b2SCorey Minyard #define DELL_IANA_MFR_ID 0x0002a2
setup_dell_poweredge_oem_data_handler(struct smi_info * smi_info)17283ae0e0f9SCorey Minyard static void setup_dell_poweredge_oem_data_handler(struct smi_info *smi_info)
17293ae0e0f9SCorey Minyard {
17303ae0e0f9SCorey Minyard 	struct ipmi_device_id *id = &smi_info->device_id;
173150c812b2SCorey Minyard 	if (id->manufacturer_id == DELL_IANA_MFR_ID) {
1732d5a2b89aSCorey Minyard 		if (id->device_id       == DELL_POWEREDGE_8G_BMC_DEVICE_ID  &&
1733d5a2b89aSCorey Minyard 		    id->device_revision == DELL_POWEREDGE_8G_BMC_DEVICE_REV &&
1734d5a2b89aSCorey Minyard 		    id->ipmi_version   == DELL_POWEREDGE_8G_BMC_IPMI_VERSION) {
17353ae0e0f9SCorey Minyard 			smi_info->oem_data_avail_handler =
17363ae0e0f9SCorey Minyard 				oem_data_avail_to_receive_msg_avail;
1737c305e3d3SCorey Minyard 		} else if (ipmi_version_major(id) < 1 ||
1738d5a2b89aSCorey Minyard 			   (ipmi_version_major(id) == 1 &&
1739d5a2b89aSCorey Minyard 			    ipmi_version_minor(id) < 5)) {
1740d5a2b89aSCorey Minyard 			smi_info->oem_data_avail_handler =
1741d5a2b89aSCorey Minyard 				oem_data_avail_to_receive_msg_avail;
1742d5a2b89aSCorey Minyard 		}
1743d5a2b89aSCorey Minyard 	}
17443ae0e0f9SCorey Minyard }
17453ae0e0f9SCorey Minyard 
1746ea94027bSCorey Minyard #define CANNOT_RETURN_REQUESTED_LENGTH 0xCA
return_hosed_msg_badsize(struct smi_info * smi_info)1747ea94027bSCorey Minyard static void return_hosed_msg_badsize(struct smi_info *smi_info)
1748ea94027bSCorey Minyard {
1749ea94027bSCorey Minyard 	struct ipmi_smi_msg *msg = smi_info->curr_msg;
1750ea94027bSCorey Minyard 
175125985edcSLucas De Marchi 	/* Make it a response */
1752ea94027bSCorey Minyard 	msg->rsp[0] = msg->data[0] | 4;
1753ea94027bSCorey Minyard 	msg->rsp[1] = msg->data[1];
1754ea94027bSCorey Minyard 	msg->rsp[2] = CANNOT_RETURN_REQUESTED_LENGTH;
1755ea94027bSCorey Minyard 	msg->rsp_size = 3;
1756ea94027bSCorey Minyard 	smi_info->curr_msg = NULL;
1757ea94027bSCorey Minyard 	deliver_recv_msg(smi_info, msg);
1758ea94027bSCorey Minyard }
1759ea94027bSCorey Minyard 
1760ea94027bSCorey Minyard /*
1761ea94027bSCorey Minyard  * dell_poweredge_bt_xaction_handler
1762ea94027bSCorey Minyard  * @info - smi_info.device_id must be populated
1763ea94027bSCorey Minyard  *
1764ea94027bSCorey Minyard  * Dell PowerEdge servers with the BT interface (x6xx and 1750) will
1765ea94027bSCorey Minyard  * not respond to a Get SDR command if the length of the data
1766ea94027bSCorey Minyard  * requested is exactly 0x3A, which leads to command timeouts and no
1767ea94027bSCorey Minyard  * data returned.  This intercepts such commands, and causes userspace
1768ea94027bSCorey Minyard  * callers to try again with a different-sized buffer, which succeeds.
1769ea94027bSCorey Minyard  */
1770ea94027bSCorey Minyard 
1771ea94027bSCorey Minyard #define STORAGE_NETFN 0x0A
1772ea94027bSCorey Minyard #define STORAGE_CMD_GET_SDR 0x23
dell_poweredge_bt_xaction_handler(struct notifier_block * self,unsigned long unused,void * in)1773ea94027bSCorey Minyard static int dell_poweredge_bt_xaction_handler(struct notifier_block *self,
1774ea94027bSCorey Minyard 					     unsigned long unused,
1775ea94027bSCorey Minyard 					     void *in)
1776ea94027bSCorey Minyard {
1777ea94027bSCorey Minyard 	struct smi_info *smi_info = in;
1778ea94027bSCorey Minyard 	unsigned char *data = smi_info->curr_msg->data;
1779ea94027bSCorey Minyard 	unsigned int size   = smi_info->curr_msg->data_size;
1780ea94027bSCorey Minyard 	if (size >= 8 &&
1781ea94027bSCorey Minyard 	    (data[0]>>2) == STORAGE_NETFN &&
1782ea94027bSCorey Minyard 	    data[1] == STORAGE_CMD_GET_SDR &&
1783ea94027bSCorey Minyard 	    data[7] == 0x3A) {
1784ea94027bSCorey Minyard 		return_hosed_msg_badsize(smi_info);
1785ea94027bSCorey Minyard 		return NOTIFY_STOP;
1786ea94027bSCorey Minyard 	}
1787ea94027bSCorey Minyard 	return NOTIFY_DONE;
1788ea94027bSCorey Minyard }
1789ea94027bSCorey Minyard 
1790ea94027bSCorey Minyard static struct notifier_block dell_poweredge_bt_xaction_notifier = {
1791ea94027bSCorey Minyard 	.notifier_call	= dell_poweredge_bt_xaction_handler,
1792ea94027bSCorey Minyard };
1793ea94027bSCorey Minyard 
1794ea94027bSCorey Minyard /*
1795ea94027bSCorey Minyard  * setup_dell_poweredge_bt_xaction_handler
1796ea94027bSCorey Minyard  * @info - smi_info.device_id must be filled in already
1797ea94027bSCorey Minyard  *
1798ea94027bSCorey Minyard  * Fills in smi_info.device_id.start_transaction_pre_hook
1799ea94027bSCorey Minyard  * when we know what function to use there.
1800ea94027bSCorey Minyard  */
1801ea94027bSCorey Minyard static void
setup_dell_poweredge_bt_xaction_handler(struct smi_info * smi_info)1802ea94027bSCorey Minyard setup_dell_poweredge_bt_xaction_handler(struct smi_info *smi_info)
1803ea94027bSCorey Minyard {
1804ea94027bSCorey Minyard 	struct ipmi_device_id *id = &smi_info->device_id;
180550c812b2SCorey Minyard 	if (id->manufacturer_id == DELL_IANA_MFR_ID &&
1806910840f2SCorey Minyard 	    smi_info->io.si_type == SI_BT)
1807ea94027bSCorey Minyard 		register_xaction_notifier(&dell_poweredge_bt_xaction_notifier);
1808ea94027bSCorey Minyard }
1809ea94027bSCorey Minyard 
18103ae0e0f9SCorey Minyard /*
18113ae0e0f9SCorey Minyard  * setup_oem_data_handler
18123ae0e0f9SCorey Minyard  * @info - smi_info.device_id must be filled in already
18133ae0e0f9SCorey Minyard  *
18143ae0e0f9SCorey Minyard  * Fills in smi_info.device_id.oem_data_available_handler
18153ae0e0f9SCorey Minyard  * when we know what function to use there.
18163ae0e0f9SCorey Minyard  */
18173ae0e0f9SCorey Minyard 
setup_oem_data_handler(struct smi_info * smi_info)18183ae0e0f9SCorey Minyard static void setup_oem_data_handler(struct smi_info *smi_info)
18193ae0e0f9SCorey Minyard {
18203ae0e0f9SCorey Minyard 	setup_dell_poweredge_oem_data_handler(smi_info);
18213ae0e0f9SCorey Minyard }
18223ae0e0f9SCorey Minyard 
setup_xaction_handlers(struct smi_info * smi_info)1823ea94027bSCorey Minyard static void setup_xaction_handlers(struct smi_info *smi_info)
1824ea94027bSCorey Minyard {
1825ea94027bSCorey Minyard 	setup_dell_poweredge_bt_xaction_handler(smi_info);
1826ea94027bSCorey Minyard }
1827ea94027bSCorey Minyard 
check_for_broken_irqs(struct smi_info * smi_info)1828d0882897SCorey Minyard static void check_for_broken_irqs(struct smi_info *smi_info)
1829d0882897SCorey Minyard {
1830d0882897SCorey Minyard 	check_clr_rcv_irq(smi_info);
1831d0882897SCorey Minyard 	check_set_rcv_irq(smi_info);
1832d0882897SCorey Minyard }
1833d0882897SCorey Minyard 
stop_timer_and_thread(struct smi_info * smi_info)18344f7f5551SMasamitsu Yamazaki static inline void stop_timer_and_thread(struct smi_info *smi_info)
1835a9a2c44fSCorey Minyard {
1836bd1c06a4SMasamitsu Yamazaki 	if (smi_info->thread != NULL) {
1837e9a705a0SMatt Domsch 		kthread_stop(smi_info->thread);
1838bd1c06a4SMasamitsu Yamazaki 		smi_info->thread = NULL;
1839bd1c06a4SMasamitsu Yamazaki 	}
18404f7f5551SMasamitsu Yamazaki 
18414f7f5551SMasamitsu Yamazaki 	smi_info->timer_can_start = false;
1842a9a2c44fSCorey Minyard 	del_timer_sync(&smi_info->si_timer);
1843a9a2c44fSCorey Minyard }
1844a9a2c44fSCorey Minyard 
find_dup_si(struct smi_info * info)18457e030d6dSCorey Minyard static struct smi_info *find_dup_si(struct smi_info *info)
1846b0defcdbSCorey Minyard {
1847b0defcdbSCorey Minyard 	struct smi_info *e;
1848b0defcdbSCorey Minyard 
1849b0defcdbSCorey Minyard 	list_for_each_entry(e, &smi_infos, link) {
1850f6296bdcSCorey Minyard 		if (e->io.addr_space != info->io.addr_space)
1851b0defcdbSCorey Minyard 			continue;
185294671710SCorey Minyard 		if (e->io.addr_data == info->io.addr_data) {
185394671710SCorey Minyard 			/*
185494671710SCorey Minyard 			 * This is a cheap hack, ACPI doesn't have a defined
185594671710SCorey Minyard 			 * slave address but SMBIOS does.  Pick it up from
185694671710SCorey Minyard 			 * any source that has it available.
185794671710SCorey Minyard 			 */
1858910840f2SCorey Minyard 			if (info->io.slave_addr && !e->io.slave_addr)
1859910840f2SCorey Minyard 				e->io.slave_addr = info->io.slave_addr;
18607e030d6dSCorey Minyard 			return e;
1861b0defcdbSCorey Minyard 		}
186294671710SCorey Minyard 	}
1863b0defcdbSCorey Minyard 
18647e030d6dSCorey Minyard 	return NULL;
1865b0defcdbSCorey Minyard }
1866b0defcdbSCorey Minyard 
ipmi_si_add_smi(struct si_sm_io * io)1867bb398a4cSCorey Minyard int ipmi_si_add_smi(struct si_sm_io *io)
18682407d77aSMatthew Garrett {
18692407d77aSMatthew Garrett 	int rv = 0;
1870bb398a4cSCorey Minyard 	struct smi_info *new_smi, *dup;
18712407d77aSMatthew Garrett 
187241b766d6SCorey Minyard 	/*
187341b766d6SCorey Minyard 	 * If the user gave us a hard-coded device at the same
187441b766d6SCorey Minyard 	 * address, they presumably want us to use it and not what is
187541b766d6SCorey Minyard 	 * in the firmware.
187641b766d6SCorey Minyard 	 */
18773bb8ea40SCorey Minyard 	if (io->addr_source != SI_HARDCODED && io->addr_source != SI_HOTMOD &&
1878f6296bdcSCorey Minyard 	    ipmi_si_hardcode_match(io->addr_space, io->addr_data)) {
187941b766d6SCorey Minyard 		dev_info(io->dev,
188041b766d6SCorey Minyard 			 "Hard-coded device at this address already exists");
188141b766d6SCorey Minyard 		return -ENODEV;
188241b766d6SCorey Minyard 	}
188341b766d6SCorey Minyard 
1884bb398a4cSCorey Minyard 	if (!io->io_setup) {
1885f6296bdcSCorey Minyard 		if (io->addr_space == IPMI_IO_ADDR_SPACE) {
188658e27635SCorey Minyard 			io->io_setup = ipmi_si_port_setup;
1887f6296bdcSCorey Minyard 		} else if (io->addr_space == IPMI_MEM_ADDR_SPACE) {
188858e27635SCorey Minyard 			io->io_setup = ipmi_si_mem_setup;
1889e1eeb7f8SCorey Minyard 		} else {
1890e1eeb7f8SCorey Minyard 			return -EINVAL;
1891e1eeb7f8SCorey Minyard 		}
1892e1eeb7f8SCorey Minyard 	}
1893e1eeb7f8SCorey Minyard 
189467f4fb02SCorey Minyard 	new_smi = kzalloc(sizeof(*new_smi), GFP_KERNEL);
1895bb398a4cSCorey Minyard 	if (!new_smi)
1896bb398a4cSCorey Minyard 		return -ENOMEM;
189767f4fb02SCorey Minyard 	spin_lock_init(&new_smi->si_lock);
1898bb398a4cSCorey Minyard 
1899bb398a4cSCorey Minyard 	new_smi->io = *io;
1900bb398a4cSCorey Minyard 
19012407d77aSMatthew Garrett 	mutex_lock(&smi_infos_lock);
19027e030d6dSCorey Minyard 	dup = find_dup_si(new_smi);
19037e030d6dSCorey Minyard 	if (dup) {
1904910840f2SCorey Minyard 		if (new_smi->io.addr_source == SI_ACPI &&
1905910840f2SCorey Minyard 		    dup->io.addr_source == SI_SMBIOS) {
19067e030d6dSCorey Minyard 			/* We prefer ACPI over SMBIOS. */
1907910840f2SCorey Minyard 			dev_info(dup->io.dev,
19087e030d6dSCorey Minyard 				 "Removing SMBIOS-specified %s state machine in favor of ACPI\n",
1909910840f2SCorey Minyard 				 si_to_str[new_smi->io.si_type]);
19107e030d6dSCorey Minyard 			cleanup_one_si(dup);
19117e030d6dSCorey Minyard 		} else {
1912910840f2SCorey Minyard 			dev_info(new_smi->io.dev,
19137e030d6dSCorey Minyard 				 "%s-specified %s state machine: duplicate\n",
1914910840f2SCorey Minyard 				 ipmi_addr_src_to_str(new_smi->io.addr_source),
1915910840f2SCorey Minyard 				 si_to_str[new_smi->io.si_type]);
19162407d77aSMatthew Garrett 			rv = -EBUSY;
1917c0a32fe1SColin Ian King 			kfree(new_smi);
19182407d77aSMatthew Garrett 			goto out_err;
19192407d77aSMatthew Garrett 		}
19207e030d6dSCorey Minyard 	}
19212407d77aSMatthew Garrett 
192225880f7dSJoe Perches 	pr_info("Adding %s-specified %s state machine\n",
1923910840f2SCorey Minyard 		ipmi_addr_src_to_str(new_smi->io.addr_source),
1924910840f2SCorey Minyard 		si_to_str[new_smi->io.si_type]);
19252407d77aSMatthew Garrett 
19262407d77aSMatthew Garrett 	list_add_tail(&new_smi->link, &smi_infos);
19272407d77aSMatthew Garrett 
192893c303d2SCorey Minyard 	if (initialized)
1929bb398a4cSCorey Minyard 		rv = try_smi_init(new_smi);
19302407d77aSMatthew Garrett out_err:
19312407d77aSMatthew Garrett 	mutex_unlock(&smi_infos_lock);
19322407d77aSMatthew Garrett 	return rv;
19332407d77aSMatthew Garrett }
19342407d77aSMatthew Garrett 
19353f724c40STony Camuso /*
19363f724c40STony Camuso  * Try to start up an interface.  Must be called with smi_infos_lock
19373f724c40STony Camuso  * held, primarily to keep smi_num consistent, we only one to do these
19383f724c40STony Camuso  * one at a time.
19393f724c40STony Camuso  */
try_smi_init(struct smi_info * new_smi)1940b0defcdbSCorey Minyard static int try_smi_init(struct smi_info *new_smi)
19411da177e4SLinus Torvalds {
19422407d77aSMatthew Garrett 	int rv = 0;
194364959e2dSCorey Minyard 	int i;
19441da177e4SLinus Torvalds 
194525880f7dSJoe Perches 	pr_info("Trying %s-specified %s state machine at %s address 0x%lx, slave address 0x%x, irq %d\n",
1946910840f2SCorey Minyard 		ipmi_addr_src_to_str(new_smi->io.addr_source),
1947910840f2SCorey Minyard 		si_to_str[new_smi->io.si_type],
1948f6296bdcSCorey Minyard 		addr_space_to_str[new_smi->io.addr_space],
1949b0defcdbSCorey Minyard 		new_smi->io.addr_data,
1950910840f2SCorey Minyard 		new_smi->io.slave_addr, new_smi->io.irq);
19511da177e4SLinus Torvalds 
1952910840f2SCorey Minyard 	switch (new_smi->io.si_type) {
1953b0defcdbSCorey Minyard 	case SI_KCS:
19541da177e4SLinus Torvalds 		new_smi->handlers = &kcs_smi_handlers;
1955b0defcdbSCorey Minyard 		break;
1956b0defcdbSCorey Minyard 
1957b0defcdbSCorey Minyard 	case SI_SMIC:
19581da177e4SLinus Torvalds 		new_smi->handlers = &smic_smi_handlers;
1959b0defcdbSCorey Minyard 		break;
1960b0defcdbSCorey Minyard 
1961b0defcdbSCorey Minyard 	case SI_BT:
19621da177e4SLinus Torvalds 		new_smi->handlers = &bt_smi_handlers;
1963b0defcdbSCorey Minyard 		break;
1964b0defcdbSCorey Minyard 
1965b0defcdbSCorey Minyard 	default:
19661da177e4SLinus Torvalds 		/* No support for anything else yet. */
19671da177e4SLinus Torvalds 		rv = -EIO;
19681da177e4SLinus Torvalds 		goto out_err;
19691da177e4SLinus Torvalds 	}
19701da177e4SLinus Torvalds 
197157bccb4eSCorey Minyard 	new_smi->si_num = smi_num;
19723f724c40STony Camuso 
19731abf71eeSCorey Minyard 	/* Do this early so it's available for logs. */
1974910840f2SCorey Minyard 	if (!new_smi->io.dev) {
197590b2d4f1SCorey Minyard 		pr_err("IPMI interface added with no device\n");
19768fe7990cSTianjia Zhang 		rv = -EIO;
19771abf71eeSCorey Minyard 		goto out_err;
19781abf71eeSCorey Minyard 	}
19791abf71eeSCorey Minyard 
19801da177e4SLinus Torvalds 	/* Allocate the state machine's data and initialize it. */
19811da177e4SLinus Torvalds 	new_smi->si_sm = kmalloc(new_smi->handlers->size(), GFP_KERNEL);
19821da177e4SLinus Torvalds 	if (!new_smi->si_sm) {
19831da177e4SLinus Torvalds 		rv = -ENOMEM;
19841da177e4SLinus Torvalds 		goto out_err;
19851da177e4SLinus Torvalds 	}
1986e1eeb7f8SCorey Minyard 	new_smi->io.io_size = new_smi->handlers->init_data(new_smi->si_sm,
19871da177e4SLinus Torvalds 							   &new_smi->io);
19881da177e4SLinus Torvalds 
19891da177e4SLinus Torvalds 	/* Now that we know the I/O size, we can set up the I/O. */
1990e1eeb7f8SCorey Minyard 	rv = new_smi->io.io_setup(&new_smi->io);
19911da177e4SLinus Torvalds 	if (rv) {
1992910840f2SCorey Minyard 		dev_err(new_smi->io.dev, "Could not set up I/O space\n");
19931da177e4SLinus Torvalds 		goto out_err;
19941da177e4SLinus Torvalds 	}
19951da177e4SLinus Torvalds 
19961da177e4SLinus Torvalds 	/* Do low-level detection first. */
19971da177e4SLinus Torvalds 	if (new_smi->handlers->detect(new_smi->si_sm)) {
1998910840f2SCorey Minyard 		if (new_smi->io.addr_source)
1999910840f2SCorey Minyard 			dev_err(new_smi->io.dev,
2000910840f2SCorey Minyard 				"Interface detection failed\n");
20011da177e4SLinus Torvalds 		rv = -ENODEV;
20021da177e4SLinus Torvalds 		goto out_err;
20031da177e4SLinus Torvalds 	}
20041da177e4SLinus Torvalds 
2005c305e3d3SCorey Minyard 	/*
2006c305e3d3SCorey Minyard 	 * Attempt a get device id command.  If it fails, we probably
2007c305e3d3SCorey Minyard 	 * don't have a BMC here.
2008c305e3d3SCorey Minyard 	 */
20091da177e4SLinus Torvalds 	rv = try_get_dev_id(new_smi);
2010b0defcdbSCorey Minyard 	if (rv) {
2011910840f2SCorey Minyard 		if (new_smi->io.addr_source)
2012910840f2SCorey Minyard 			dev_err(new_smi->io.dev,
2013910840f2SCorey Minyard 			       "There appears to be no BMC at this location\n");
20141da177e4SLinus Torvalds 		goto out_err;
2015b0defcdbSCorey Minyard 	}
20161da177e4SLinus Torvalds 
20173ae0e0f9SCorey Minyard 	setup_oem_data_handler(new_smi);
2018ea94027bSCorey Minyard 	setup_xaction_handlers(new_smi);
2019d0882897SCorey Minyard 	check_for_broken_irqs(new_smi);
20203ae0e0f9SCorey Minyard 
2021b874b985SCorey Minyard 	new_smi->waiting_msg = NULL;
20221da177e4SLinus Torvalds 	new_smi->curr_msg = NULL;
20231da177e4SLinus Torvalds 	atomic_set(&new_smi->req_events, 0);
20247aefac26SCorey Minyard 	new_smi->run_to_completion = false;
202564959e2dSCorey Minyard 	for (i = 0; i < SI_NUM_STATS; i++)
202664959e2dSCorey Minyard 		atomic_set(&new_smi->stats[i], 0);
20271da177e4SLinus Torvalds 
20287aefac26SCorey Minyard 	new_smi->interrupt_disabled = true;
202989986496SCorey Minyard 	atomic_set(&new_smi->need_watch, 0);
20301da177e4SLinus Torvalds 
203140112ae7SCorey Minyard 	rv = try_enable_event_buffer(new_smi);
203240112ae7SCorey Minyard 	if (rv == 0)
20337aefac26SCorey Minyard 		new_smi->has_event_buffer = true;
203440112ae7SCorey Minyard 
2035c305e3d3SCorey Minyard 	/*
2036c305e3d3SCorey Minyard 	 * Start clearing the flags before we enable interrupts or the
2037c305e3d3SCorey Minyard 	 * timer to avoid racing with the timer.
2038c305e3d3SCorey Minyard 	 */
20394f7f5551SMasamitsu Yamazaki 	start_clear_flags(new_smi);
2040d9b7e4f7SCorey Minyard 
2041d9b7e4f7SCorey Minyard 	/*
2042d9b7e4f7SCorey Minyard 	 * IRQ is defined to be set when non-zero.  req_events will
2043d9b7e4f7SCorey Minyard 	 * cause a global flags check that will enable interrupts.
2044d9b7e4f7SCorey Minyard 	 */
2045910840f2SCorey Minyard 	if (new_smi->io.irq) {
2046d9b7e4f7SCorey Minyard 		new_smi->interrupt_disabled = false;
2047d9b7e4f7SCorey Minyard 		atomic_set(&new_smi->req_events, 1);
2048d9b7e4f7SCorey Minyard 	}
20491da177e4SLinus Torvalds 
20503dd377b5SCorey Minyard 	dev_set_drvdata(new_smi->io.dev, new_smi);
20513dd377b5SCorey Minyard 	rv = device_add_group(new_smi->io.dev, &ipmi_si_dev_attr_group);
20523dd377b5SCorey Minyard 	if (rv) {
20533dd377b5SCorey Minyard 		dev_err(new_smi->io.dev,
20543dd377b5SCorey Minyard 			"Unable to add device attributes: error %d\n",
20553dd377b5SCorey Minyard 			rv);
205671404a2fSCorey Minyard 		goto out_err;
20573dd377b5SCorey Minyard 	}
2058cc095f0aSCorey Minyard 	new_smi->dev_group_added = true;
20593dd377b5SCorey Minyard 
20601da177e4SLinus Torvalds 	rv = ipmi_register_smi(&handlers,
20611da177e4SLinus Torvalds 			       new_smi,
2062910840f2SCorey Minyard 			       new_smi->io.dev,
2063910840f2SCorey Minyard 			       new_smi->io.slave_addr);
20641da177e4SLinus Torvalds 	if (rv) {
2065910840f2SCorey Minyard 		dev_err(new_smi->io.dev,
2066910840f2SCorey Minyard 			"Unable to register device: error %d\n",
20671da177e4SLinus Torvalds 			rv);
206871404a2fSCorey Minyard 		goto out_err;
20691da177e4SLinus Torvalds 	}
20701da177e4SLinus Torvalds 
20713f724c40STony Camuso 	/* Don't increment till we know we have succeeded. */
20723f724c40STony Camuso 	smi_num++;
20733f724c40STony Camuso 
2074910840f2SCorey Minyard 	dev_info(new_smi->io.dev, "IPMI %s interface initialized\n",
2075910840f2SCorey Minyard 		 si_to_str[new_smi->io.si_type]);
20761da177e4SLinus Torvalds 
2077910840f2SCorey Minyard 	WARN_ON(new_smi->io.dev->init_name != NULL);
20781da177e4SLinus Torvalds 
20791da177e4SLinus Torvalds  out_err:
2080401e7e88SYang Yingliang 	if (rv && new_smi->io.io_cleanup) {
2081401e7e88SYang Yingliang 		new_smi->io.io_cleanup(&new_smi->io);
2082401e7e88SYang Yingliang 		new_smi->io.io_cleanup = NULL;
2083401e7e88SYang Yingliang 	}
2084401e7e88SYang Yingliang 
2085*6cf1a126SYi Yang 	if (rv && new_smi->si_sm) {
2086*6cf1a126SYi Yang 		kfree(new_smi->si_sm);
2087*6cf1a126SYi Yang 		new_smi->si_sm = NULL;
2088*6cf1a126SYi Yang 	}
2089*6cf1a126SYi Yang 
20901da177e4SLinus Torvalds 	return rv;
20911da177e4SLinus Torvalds }
20921da177e4SLinus Torvalds 
init_ipmi_si(void)209341b766d6SCorey Minyard static int __init init_ipmi_si(void)
20941da177e4SLinus Torvalds {
20952407d77aSMatthew Garrett 	struct smi_info *e;
209606ee4594SMatthew Garrett 	enum ipmi_addr_src type = SI_INVALID;
20971da177e4SLinus Torvalds 
20981da177e4SLinus Torvalds 	if (initialized)
20991da177e4SLinus Torvalds 		return 0;
21001da177e4SLinus Torvalds 
210141b766d6SCorey Minyard 	ipmi_hardcode_init();
21021da177e4SLinus Torvalds 
210341b766d6SCorey Minyard 	pr_info("IPMI System Interface driver\n");
2104d8cc5267SMatthew Garrett 
21059d70029eSCorey Minyard 	ipmi_si_platform_init();
21069d70029eSCorey Minyard 
210713d0b35cSCorey Minyard 	ipmi_si_pci_init();
2108b0defcdbSCorey Minyard 
2109c6f85a75SCorey Minyard 	ipmi_si_parisc_init();
2110fdbeb7deSThomas Bogendoerfer 
211106ee4594SMatthew Garrett 	/* We prefer devices with interrupts, but in the case of a machine
211206ee4594SMatthew Garrett 	   with multiple BMCs we assume that there will be several instances
211306ee4594SMatthew Garrett 	   of a given type so if we succeed in registering a type then also
211406ee4594SMatthew Garrett 	   try to register everything else of the same type */
21152407d77aSMatthew Garrett 	mutex_lock(&smi_infos_lock);
21162407d77aSMatthew Garrett 	list_for_each_entry(e, &smi_infos, link) {
211706ee4594SMatthew Garrett 		/* Try to register a device if it has an IRQ and we either
211806ee4594SMatthew Garrett 		   haven't successfully registered a device yet or this
211906ee4594SMatthew Garrett 		   device has the same type as one we successfully registered */
2120910840f2SCorey Minyard 		if (e->io.irq && (!type || e->io.addr_source == type)) {
2121d8cc5267SMatthew Garrett 			if (!try_smi_init(e)) {
2122910840f2SCorey Minyard 				type = e->io.addr_source;
212306ee4594SMatthew Garrett 			}
212406ee4594SMatthew Garrett 		}
212506ee4594SMatthew Garrett 	}
212606ee4594SMatthew Garrett 
212706ee4594SMatthew Garrett 	/* type will only have been set if we successfully registered an si */
2128bb398a4cSCorey Minyard 	if (type)
2129bb398a4cSCorey Minyard 		goto skip_fallback_noirq;
2130d8cc5267SMatthew Garrett 
2131d8cc5267SMatthew Garrett 	/* Fall back to the preferred device */
2132d8cc5267SMatthew Garrett 
2133d8cc5267SMatthew Garrett 	list_for_each_entry(e, &smi_infos, link) {
2134910840f2SCorey Minyard 		if (!e->io.irq && (!type || e->io.addr_source == type)) {
2135d8cc5267SMatthew Garrett 			if (!try_smi_init(e)) {
2136910840f2SCorey Minyard 				type = e->io.addr_source;
213706ee4594SMatthew Garrett 			}
213806ee4594SMatthew Garrett 		}
213906ee4594SMatthew Garrett 	}
2140bb398a4cSCorey Minyard 
2141bb398a4cSCorey Minyard skip_fallback_noirq:
2142dd7450caSKefeng Wang 	initialized = true;
2143d8cc5267SMatthew Garrett 	mutex_unlock(&smi_infos_lock);
214406ee4594SMatthew Garrett 
214506ee4594SMatthew Garrett 	if (type)
2146d8cc5267SMatthew Garrett 		return 0;
21472407d77aSMatthew Garrett 
2148d6dfd131SCorey Minyard 	mutex_lock(&smi_infos_lock);
2149b361e27bSCorey Minyard 	if (unload_when_empty && list_empty(&smi_infos)) {
2150d6dfd131SCorey Minyard 		mutex_unlock(&smi_infos_lock);
2151d2478521SCorey Minyard 		cleanup_ipmi_si();
215225880f7dSJoe Perches 		pr_warn("Unable to find any System Interface(s)\n");
21531da177e4SLinus Torvalds 		return -ENODEV;
2154b0defcdbSCorey Minyard 	} else {
2155d6dfd131SCorey Minyard 		mutex_unlock(&smi_infos_lock);
21561da177e4SLinus Torvalds 		return 0;
21571da177e4SLinus Torvalds 	}
2158b0defcdbSCorey Minyard }
21591da177e4SLinus Torvalds module_init(init_ipmi_si);
21601da177e4SLinus Torvalds 
wait_msg_processed(struct smi_info * smi_info)2161f6f1234dSZhang Yuchen static void wait_msg_processed(struct smi_info *smi_info)
2162f6f1234dSZhang Yuchen {
2163f6f1234dSZhang Yuchen 	unsigned long jiffies_now;
2164f6f1234dSZhang Yuchen 	long time_diff;
2165f6f1234dSZhang Yuchen 
2166f6f1234dSZhang Yuchen 	while (smi_info->curr_msg || (smi_info->si_state != SI_NORMAL)) {
2167f6f1234dSZhang Yuchen 		jiffies_now = jiffies;
2168f6f1234dSZhang Yuchen 		time_diff = (((long)jiffies_now - (long)smi_info->last_timeout_jiffies)
2169f6f1234dSZhang Yuchen 		     * SI_USEC_PER_JIFFY);
2170f6f1234dSZhang Yuchen 		smi_event_handler(smi_info, time_diff);
2171f6f1234dSZhang Yuchen 		schedule_timeout_uninterruptible(1);
2172f6f1234dSZhang Yuchen 	}
2173f6f1234dSZhang Yuchen }
2174f6f1234dSZhang Yuchen 
shutdown_smi(void * send_info)21757960f18aSCorey Minyard static void shutdown_smi(void *send_info)
21761da177e4SLinus Torvalds {
21777960f18aSCorey Minyard 	struct smi_info *smi_info = send_info;
2178b874b985SCorey Minyard 
217971404a2fSCorey Minyard 	if (smi_info->dev_group_added) {
218071404a2fSCorey Minyard 		device_remove_group(smi_info->io.dev, &ipmi_si_dev_attr_group);
218171404a2fSCorey Minyard 		smi_info->dev_group_added = false;
218271404a2fSCorey Minyard 	}
218371404a2fSCorey Minyard 	if (smi_info->io.dev)
218471404a2fSCorey Minyard 		dev_set_drvdata(smi_info->io.dev, NULL);
2185b0defcdbSCorey Minyard 
2186c305e3d3SCorey Minyard 	/*
2187b874b985SCorey Minyard 	 * Make sure that interrupts, the timer and the thread are
2188b874b985SCorey Minyard 	 * stopped and will not run again.
2189c305e3d3SCorey Minyard 	 */
219071404a2fSCorey Minyard 	smi_info->interrupt_disabled = true;
219171404a2fSCorey Minyard 	if (smi_info->io.irq_cleanup) {
219271404a2fSCorey Minyard 		smi_info->io.irq_cleanup(&smi_info->io);
219371404a2fSCorey Minyard 		smi_info->io.irq_cleanup = NULL;
219471404a2fSCorey Minyard 	}
219571404a2fSCorey Minyard 	stop_timer_and_thread(smi_info);
219671404a2fSCorey Minyard 
219771404a2fSCorey Minyard 	/*
219871404a2fSCorey Minyard 	 * Wait until we know that we are out of any interrupt
219971404a2fSCorey Minyard 	 * handlers might have been running before we freed the
220071404a2fSCorey Minyard 	 * interrupt.
220171404a2fSCorey Minyard 	 */
220217c0eb74SPaul E. McKenney 	synchronize_rcu();
22031da177e4SLinus Torvalds 
2204c305e3d3SCorey Minyard 	/*
2205c305e3d3SCorey Minyard 	 * Timeouts are stopped, now make sure the interrupts are off
2206b874b985SCorey Minyard 	 * in the BMC.  Note that timers and CPU interrupts are off,
2207b874b985SCorey Minyard 	 * so no need for locks.
2208c305e3d3SCorey Minyard 	 */
2209f6f1234dSZhang Yuchen 	wait_msg_processed(smi_info);
2210f6f1234dSZhang Yuchen 
221171404a2fSCorey Minyard 	if (smi_info->handlers)
221271404a2fSCorey Minyard 		disable_si_irq(smi_info);
2213f6f1234dSZhang Yuchen 
2214f6f1234dSZhang Yuchen 	wait_msg_processed(smi_info);
2215f6f1234dSZhang Yuchen 
221671404a2fSCorey Minyard 	if (smi_info->handlers)
221771404a2fSCorey Minyard 		smi_info->handlers->cleanup(smi_info->si_sm);
2218ee6cd5f8SCorey Minyard 
221971404a2fSCorey Minyard 	if (smi_info->io.io_cleanup) {
222071404a2fSCorey Minyard 		smi_info->io.io_cleanup(&smi_info->io);
222171404a2fSCorey Minyard 		smi_info->io.io_cleanup = NULL;
222271404a2fSCorey Minyard 	}
22231da177e4SLinus Torvalds 
222471404a2fSCorey Minyard 	kfree(smi_info->si_sm);
222571404a2fSCorey Minyard 	smi_info->si_sm = NULL;
22262512e40eSCorey Minyard 
22272512e40eSCorey Minyard 	smi_info->intf = NULL;
222871404a2fSCorey Minyard }
22291da177e4SLinus Torvalds 
223093c303d2SCorey Minyard /*
223193c303d2SCorey Minyard  * Must be called with smi_infos_lock held, to serialize the
223293c303d2SCorey Minyard  * smi_info->intf check.
223393c303d2SCorey Minyard  */
cleanup_one_si(struct smi_info * smi_info)223471404a2fSCorey Minyard static void cleanup_one_si(struct smi_info *smi_info)
223571404a2fSCorey Minyard {
223671404a2fSCorey Minyard 	if (!smi_info)
223771404a2fSCorey Minyard 		return;
223850c812b2SCorey Minyard 
223971404a2fSCorey Minyard 	list_del(&smi_info->link);
224093c303d2SCorey Minyard 	ipmi_unregister_smi(smi_info->intf);
224171404a2fSCorey Minyard 	kfree(smi_info);
22421da177e4SLinus Torvalds }
22431da177e4SLinus Torvalds 
ipmi_si_remove_by_dev(struct device * dev)224487875c10SUwe Kleine-König void ipmi_si_remove_by_dev(struct device *dev)
2245bb398a4cSCorey Minyard {
2246bb398a4cSCorey Minyard 	struct smi_info *e;
2247bb398a4cSCorey Minyard 
2248bb398a4cSCorey Minyard 	mutex_lock(&smi_infos_lock);
2249bb398a4cSCorey Minyard 	list_for_each_entry(e, &smi_infos, link) {
2250bb398a4cSCorey Minyard 		if (e->io.dev == dev) {
2251bb398a4cSCorey Minyard 			cleanup_one_si(e);
2252bb398a4cSCorey Minyard 			break;
2253bb398a4cSCorey Minyard 		}
2254bb398a4cSCorey Minyard 	}
2255bb398a4cSCorey Minyard 	mutex_unlock(&smi_infos_lock);
2256bb398a4cSCorey Minyard }
2257bb398a4cSCorey Minyard 
ipmi_si_remove_by_data(int addr_space,enum si_type si_type,unsigned long addr)2258bdb57b7bSCorey Minyard struct device *ipmi_si_remove_by_data(int addr_space, enum si_type si_type,
225944814ec9SCorey Minyard 				      unsigned long addr)
226044814ec9SCorey Minyard {
226144814ec9SCorey Minyard 	/* remove */
226244814ec9SCorey Minyard 	struct smi_info *e, *tmp_e;
2263bdb57b7bSCorey Minyard 	struct device *dev = NULL;
226444814ec9SCorey Minyard 
226544814ec9SCorey Minyard 	mutex_lock(&smi_infos_lock);
226644814ec9SCorey Minyard 	list_for_each_entry_safe(e, tmp_e, &smi_infos, link) {
2267f6296bdcSCorey Minyard 		if (e->io.addr_space != addr_space)
226844814ec9SCorey Minyard 			continue;
226944814ec9SCorey Minyard 		if (e->io.si_type != si_type)
227044814ec9SCorey Minyard 			continue;
2271bdb57b7bSCorey Minyard 		if (e->io.addr_data == addr) {
2272bdb57b7bSCorey Minyard 			dev = get_device(e->io.dev);
227344814ec9SCorey Minyard 			cleanup_one_si(e);
227444814ec9SCorey Minyard 		}
2275bdb57b7bSCorey Minyard 	}
227644814ec9SCorey Minyard 	mutex_unlock(&smi_infos_lock);
2277bdb57b7bSCorey Minyard 
2278bdb57b7bSCorey Minyard 	return dev;
227944814ec9SCorey Minyard }
228044814ec9SCorey Minyard 
cleanup_ipmi_si(void)22810dcf334cSSergey Senozhatsky static void cleanup_ipmi_si(void)
22821da177e4SLinus Torvalds {
2283b0defcdbSCorey Minyard 	struct smi_info *e, *tmp_e;
22841da177e4SLinus Torvalds 
22851da177e4SLinus Torvalds 	if (!initialized)
22861da177e4SLinus Torvalds 		return;
22871da177e4SLinus Torvalds 
228813d0b35cSCorey Minyard 	ipmi_si_pci_shutdown();
2289c6f85a75SCorey Minyard 
2290c6f85a75SCorey Minyard 	ipmi_si_parisc_shutdown();
2291b0defcdbSCorey Minyard 
22929d70029eSCorey Minyard 	ipmi_si_platform_shutdown();
2293dba9b4f6SCorey Minyard 
2294d6dfd131SCorey Minyard 	mutex_lock(&smi_infos_lock);
2295b0defcdbSCorey Minyard 	list_for_each_entry_safe(e, tmp_e, &smi_infos, link)
2296b0defcdbSCorey Minyard 		cleanup_one_si(e);
2297d6dfd131SCorey Minyard 	mutex_unlock(&smi_infos_lock);
229841b766d6SCorey Minyard 
229941b766d6SCorey Minyard 	ipmi_si_hardcode_exit();
2300bdb57b7bSCorey Minyard 	ipmi_si_hotmod_exit();
23011da177e4SLinus Torvalds }
23021da177e4SLinus Torvalds module_exit(cleanup_ipmi_si);
23031da177e4SLinus Torvalds 
23040944d889SCorey Minyard MODULE_ALIAS("platform:dmi-ipmi-si");
23051da177e4SLinus Torvalds MODULE_LICENSE("GPL");
23061fdd75bdSCorey Minyard MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
230707cbd87bSAndy Shevchenko MODULE_DESCRIPTION("Interface to the IPMI driver for the KCS, SMIC, and BT system interfaces.");
2308