ipmi_si: Get rid of unused spacing and port fields
[linux-2.6-block.git] / drivers / char / ipmi / ipmi_si_intf.c
CommitLineData
1da177e4
LT
1/*
2 * ipmi_si.c
3 *
4 * The interface to the IPMI driver for the system interfaces (KCS, SMIC,
5 * BT).
6 *
7 * Author: MontaVista Software, Inc.
8 * Corey Minyard <minyard@mvista.com>
9 * source@mvista.com
10 *
11 * Copyright 2002 MontaVista Software Inc.
dba9b4f6 12 * Copyright 2006 IBM Corp., Christian Krafft <krafft@de.ibm.com>
1da177e4
LT
13 *
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version.
18 *
19 *
20 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
21 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
28 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
29 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 * You should have received a copy of the GNU General Public License along
32 * with this program; if not, write to the Free Software Foundation, Inc.,
33 * 675 Mass Ave, Cambridge, MA 02139, USA.
34 */
35
36/*
37 * This file holds the "policy" for the interface to the SMI state
38 * machine. It does the configuration, handles timers and interrupts,
39 * and drives the real SMI state machine.
40 */
41
1da177e4
LT
42#include <linux/module.h>
43#include <linux/moduleparam.h>
1da177e4 44#include <linux/sched.h>
07412736 45#include <linux/seq_file.h>
1da177e4
LT
46#include <linux/timer.h>
47#include <linux/errno.h>
48#include <linux/spinlock.h>
49#include <linux/slab.h>
50#include <linux/delay.h>
51#include <linux/list.h>
1da177e4 52#include <linux/ioport.h>
ea94027b 53#include <linux/notifier.h>
b0defcdb 54#include <linux/mutex.h>
e9a705a0 55#include <linux/kthread.h>
1da177e4 56#include <asm/irq.h>
1da177e4
LT
57#include <linux/interrupt.h>
58#include <linux/rcupdate.h>
16f4232c 59#include <linux/ipmi.h>
1da177e4
LT
60#include <linux/ipmi_smi.h>
61#include <asm/io.h>
1e89a499 62#include "ipmi_si.h"
b361e27b
CM
63#include <linux/string.h>
64#include <linux/ctype.h>
dba9b4f6 65
b361e27b 66#define PFX "ipmi_si: "
1da177e4
LT
67
68/* Measure times between events in the driver. */
69#undef DEBUG_TIMING
70
71/* Call every 10 ms. */
72#define SI_TIMEOUT_TIME_USEC 10000
73#define SI_USEC_PER_JIFFY (1000000/HZ)
74#define SI_TIMEOUT_JIFFIES (SI_TIMEOUT_TIME_USEC/SI_USEC_PER_JIFFY)
75#define SI_SHORT_TIMEOUT_USEC 250 /* .25ms when the SM request a
c305e3d3 76 short timeout */
1da177e4
LT
77
78enum si_intf_state {
79 SI_NORMAL,
80 SI_GETTING_FLAGS,
81 SI_GETTING_EVENTS,
82 SI_CLEARING_FLAGS,
1da177e4 83 SI_GETTING_MESSAGES,
d9b7e4f7
CM
84 SI_CHECKING_ENABLES,
85 SI_SETTING_ENABLES
1da177e4
LT
86 /* FIXME - add watchdog stuff. */
87};
88
9dbf68f9
CM
89/* Some BT-specific defines we need here. */
90#define IPMI_BT_INTMASK_REG 2
91#define IPMI_BT_INTMASK_CLEAR_IRQ_BIT 2
92#define IPMI_BT_INTMASK_ENABLE_IRQ_BIT 1
93
99ee6735 94static const char * const si_to_str[] = { "kcs", "smic", "bt" };
1da177e4 95
bb398a4c
CM
96static int initialized;
97
64959e2d
CM
98/*
99 * Indexes into stats[] in smi_info below.
100 */
ba8ff1c6
CM
101enum si_stat_indexes {
102 /*
103 * Number of times the driver requested a timer while an operation
104 * was in progress.
105 */
106 SI_STAT_short_timeouts = 0,
107
108 /*
109 * Number of times the driver requested a timer while nothing was in
110 * progress.
111 */
112 SI_STAT_long_timeouts,
113
114 /* Number of times the interface was idle while being polled. */
115 SI_STAT_idles,
116
117 /* Number of interrupts the driver handled. */
118 SI_STAT_interrupts,
119
120 /* Number of time the driver got an ATTN from the hardware. */
121 SI_STAT_attentions,
64959e2d 122
ba8ff1c6
CM
123 /* Number of times the driver requested flags from the hardware. */
124 SI_STAT_flag_fetches,
125
126 /* Number of times the hardware didn't follow the state machine. */
127 SI_STAT_hosed_count,
128
129 /* Number of completed messages. */
130 SI_STAT_complete_transactions,
131
132 /* Number of IPMI events received from the hardware. */
133 SI_STAT_events,
134
135 /* Number of watchdog pretimeouts. */
136 SI_STAT_watchdog_pretimeouts,
137
b3834be5 138 /* Number of asynchronous messages received. */
ba8ff1c6
CM
139 SI_STAT_incoming_messages,
140
141
142 /* This *must* remain last, add new values above this. */
143 SI_NUM_STATS
144};
64959e2d 145
c305e3d3 146struct smi_info {
a9a2c44f 147 int intf_num;
1da177e4
LT
148 ipmi_smi_t intf;
149 struct si_sm_data *si_sm;
81d02b7f 150 const struct si_sm_handlers *handlers;
1da177e4 151 spinlock_t si_lock;
b874b985 152 struct ipmi_smi_msg *waiting_msg;
1da177e4
LT
153 struct ipmi_smi_msg *curr_msg;
154 enum si_intf_state si_state;
155
c305e3d3
CM
156 /*
157 * Used to handle the various types of I/O that can occur with
158 * IPMI
159 */
1da177e4 160 struct si_sm_io io;
1da177e4 161
c305e3d3
CM
162 /*
163 * Per-OEM handler, called from handle_flags(). Returns 1
164 * when handle_flags() needs to be re-run or 0 indicating it
165 * set si_state itself.
166 */
3ae0e0f9
CM
167 int (*oem_data_avail_handler)(struct smi_info *smi_info);
168
c305e3d3
CM
169 /*
170 * Flags from the last GET_MSG_FLAGS command, used when an ATTN
171 * is set to hold the flags until we are done handling everything
172 * from the flags.
173 */
1da177e4
LT
174#define RECEIVE_MSG_AVAIL 0x01
175#define EVENT_MSG_BUFFER_FULL 0x02
176#define WDT_PRE_TIMEOUT_INT 0x08
3ae0e0f9
CM
177#define OEM0_DATA_AVAIL 0x20
178#define OEM1_DATA_AVAIL 0x40
179#define OEM2_DATA_AVAIL 0x80
180#define OEM_DATA_AVAIL (OEM0_DATA_AVAIL | \
c305e3d3
CM
181 OEM1_DATA_AVAIL | \
182 OEM2_DATA_AVAIL)
1da177e4
LT
183 unsigned char msg_flags;
184
40112ae7 185 /* Does the BMC have an event buffer? */
7aefac26 186 bool has_event_buffer;
40112ae7 187
c305e3d3
CM
188 /*
189 * If set to true, this will request events the next time the
190 * state machine is idle.
191 */
1da177e4
LT
192 atomic_t req_events;
193
c305e3d3
CM
194 /*
195 * If true, run the state machine to completion on every send
196 * call. Generally used after a panic to make sure stuff goes
197 * out.
198 */
7aefac26 199 bool run_to_completion;
1da177e4 200
1da177e4
LT
201 /* The timer for this si. */
202 struct timer_list si_timer;
203
48e8ac29
BS
204 /* This flag is set, if the timer is running (timer_pending() isn't enough) */
205 bool timer_running;
206
1da177e4
LT
207 /* The time (in jiffies) the last timeout occurred at. */
208 unsigned long last_timeout_jiffies;
209
89986496
CM
210 /* Are we waiting for the events, pretimeouts, received msgs? */
211 atomic_t need_watch;
212
c305e3d3
CM
213 /*
214 * The driver will disable interrupts when it gets into a
215 * situation where it cannot handle messages due to lack of
216 * memory. Once that situation clears up, it will re-enable
217 * interrupts.
218 */
7aefac26 219 bool interrupt_disabled;
1da177e4 220
d9b7e4f7
CM
221 /*
222 * Does the BMC support events?
223 */
224 bool supports_event_msg_buff;
225
1e7d6a45 226 /*
d0882897
CM
227 * Can we disable interrupts the global enables receive irq
228 * bit? There are currently two forms of brokenness, some
229 * systems cannot disable the bit (which is technically within
230 * the spec but a bad idea) and some systems have the bit
231 * forced to zero even though interrupts work (which is
232 * clearly outside the spec). The next bool tells which form
233 * of brokenness is present.
1e7d6a45 234 */
d0882897
CM
235 bool cannot_disable_irq;
236
237 /*
238 * Some systems are broken and cannot set the irq enable
239 * bit, even if they support interrupts.
240 */
241 bool irq_enable_broken;
1e7d6a45 242
a8df150c
CM
243 /*
244 * Did we get an attention that we did not handle?
245 */
246 bool got_attn;
247
50c812b2 248 /* From the get device id response... */
3ae0e0f9 249 struct ipmi_device_id device_id;
1da177e4 250
910840f2 251 /* Default driver model device. */
50c812b2
CM
252 struct platform_device *pdev;
253
1da177e4 254 /* Counters and things for the proc filesystem. */
64959e2d 255 atomic_t stats[SI_NUM_STATS];
a9a2c44f 256
c305e3d3 257 struct task_struct *thread;
b0defcdb
CM
258
259 struct list_head link;
1da177e4
LT
260};
261
64959e2d
CM
262#define smi_inc_stat(smi, stat) \
263 atomic_inc(&(smi)->stats[SI_STAT_ ## stat])
264#define smi_get_stat(smi, stat) \
265 ((unsigned int) atomic_read(&(smi)->stats[SI_STAT_ ## stat]))
266
7a453308
CM
267#define IPMI_MAX_INTFS 4
268static int force_kipmid[IPMI_MAX_INTFS];
a51f4a81
CM
269static int num_force_kipmid;
270
7a453308 271static unsigned int kipmid_max_busy_us[IPMI_MAX_INTFS];
ae74e823
MW
272static int num_max_busy_us;
273
7aefac26 274static bool unload_when_empty = true;
b361e27b 275
b0defcdb 276static int try_smi_init(struct smi_info *smi);
b361e27b 277static void cleanup_one_si(struct smi_info *to_clean);
d2478521 278static void cleanup_ipmi_si(void);
b0defcdb 279
f93aae9f
JS
280#ifdef DEBUG_TIMING
281void debug_timestamp(char *msg)
282{
48862ea2 283 struct timespec64 t;
f93aae9f 284
48862ea2
JS
285 getnstimeofday64(&t);
286 pr_debug("**%s: %lld.%9.9ld\n", msg, (long long) t.tv_sec, t.tv_nsec);
f93aae9f
JS
287}
288#else
289#define debug_timestamp(x)
290#endif
291
e041c683 292static ATOMIC_NOTIFIER_HEAD(xaction_notifier_list);
c305e3d3 293static int register_xaction_notifier(struct notifier_block *nb)
ea94027b 294{
e041c683 295 return atomic_notifier_chain_register(&xaction_notifier_list, nb);
ea94027b
CM
296}
297
1da177e4
LT
298static void deliver_recv_msg(struct smi_info *smi_info,
299 struct ipmi_smi_msg *msg)
300{
7adf579c 301 /* Deliver the message to the upper layer. */
968bf7cc
CM
302 if (smi_info->intf)
303 ipmi_smi_msg_received(smi_info->intf, msg);
304 else
305 ipmi_free_smi_msg(msg);
1da177e4
LT
306}
307
4d7cbac7 308static void return_hosed_msg(struct smi_info *smi_info, int cCode)
1da177e4
LT
309{
310 struct ipmi_smi_msg *msg = smi_info->curr_msg;
311
4d7cbac7
CM
312 if (cCode < 0 || cCode > IPMI_ERR_UNSPECIFIED)
313 cCode = IPMI_ERR_UNSPECIFIED;
314 /* else use it as is */
315
25985edc 316 /* Make it a response */
1da177e4
LT
317 msg->rsp[0] = msg->data[0] | 4;
318 msg->rsp[1] = msg->data[1];
4d7cbac7 319 msg->rsp[2] = cCode;
1da177e4
LT
320 msg->rsp_size = 3;
321
322 smi_info->curr_msg = NULL;
323 deliver_recv_msg(smi_info, msg);
324}
325
326static enum si_sm_result start_next_msg(struct smi_info *smi_info)
327{
328 int rv;
1da177e4 329
b874b985 330 if (!smi_info->waiting_msg) {
1da177e4
LT
331 smi_info->curr_msg = NULL;
332 rv = SI_SM_IDLE;
333 } else {
334 int err;
335
b874b985
CM
336 smi_info->curr_msg = smi_info->waiting_msg;
337 smi_info->waiting_msg = NULL;
f93aae9f 338 debug_timestamp("Start2");
e041c683
AS
339 err = atomic_notifier_call_chain(&xaction_notifier_list,
340 0, smi_info);
ea94027b
CM
341 if (err & NOTIFY_STOP_MASK) {
342 rv = SI_SM_CALL_WITHOUT_DELAY;
343 goto out;
344 }
1da177e4
LT
345 err = smi_info->handlers->start_transaction(
346 smi_info->si_sm,
347 smi_info->curr_msg->data,
348 smi_info->curr_msg->data_size);
c305e3d3 349 if (err)
4d7cbac7 350 return_hosed_msg(smi_info, err);
1da177e4
LT
351
352 rv = SI_SM_CALL_WITHOUT_DELAY;
353 }
76824852 354out:
1da177e4
LT
355 return rv;
356}
357
0cfec916
CM
358static void smi_mod_timer(struct smi_info *smi_info, unsigned long new_val)
359{
360 smi_info->last_timeout_jiffies = jiffies;
361 mod_timer(&smi_info->si_timer, new_val);
362 smi_info->timer_running = true;
363}
364
365/*
366 * Start a new message and (re)start the timer and thread.
367 */
368static void start_new_msg(struct smi_info *smi_info, unsigned char *msg,
369 unsigned int size)
370{
371 smi_mod_timer(smi_info, jiffies + SI_TIMEOUT_JIFFIES);
372
373 if (smi_info->thread)
374 wake_up_process(smi_info->thread);
375
376 smi_info->handlers->start_transaction(smi_info->si_sm, msg, size);
377}
378
379static void start_check_enables(struct smi_info *smi_info, bool start_timer)
ee6cd5f8
CM
380{
381 unsigned char msg[2];
382
383 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
384 msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
385
0cfec916
CM
386 if (start_timer)
387 start_new_msg(smi_info, msg, 2);
388 else
389 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
d9b7e4f7 390 smi_info->si_state = SI_CHECKING_ENABLES;
ee6cd5f8
CM
391}
392
0cfec916 393static void start_clear_flags(struct smi_info *smi_info, bool start_timer)
1da177e4
LT
394{
395 unsigned char msg[3];
396
397 /* Make sure the watchdog pre-timeout flag is not set at startup. */
398 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
399 msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
400 msg[2] = WDT_PRE_TIMEOUT_INT;
401
0cfec916
CM
402 if (start_timer)
403 start_new_msg(smi_info, msg, 3);
404 else
405 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 3);
1da177e4
LT
406 smi_info->si_state = SI_CLEARING_FLAGS;
407}
408
968bf7cc
CM
409static void start_getting_msg_queue(struct smi_info *smi_info)
410{
411 smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
412 smi_info->curr_msg->data[1] = IPMI_GET_MSG_CMD;
413 smi_info->curr_msg->data_size = 2;
414
0cfec916
CM
415 start_new_msg(smi_info, smi_info->curr_msg->data,
416 smi_info->curr_msg->data_size);
968bf7cc
CM
417 smi_info->si_state = SI_GETTING_MESSAGES;
418}
419
420static void start_getting_events(struct smi_info *smi_info)
421{
422 smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
423 smi_info->curr_msg->data[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD;
424 smi_info->curr_msg->data_size = 2;
425
0cfec916
CM
426 start_new_msg(smi_info, smi_info->curr_msg->data,
427 smi_info->curr_msg->data_size);
968bf7cc
CM
428 smi_info->si_state = SI_GETTING_EVENTS;
429}
430
c305e3d3
CM
431/*
432 * When we have a situtaion where we run out of memory and cannot
433 * allocate messages, we just leave them in the BMC and run the system
434 * polled until we can allocate some memory. Once we have some
435 * memory, we will re-enable the interrupt.
1e7d6a45
CM
436 *
437 * Note that we cannot just use disable_irq(), since the interrupt may
438 * be shared.
c305e3d3 439 */
0cfec916 440static inline bool disable_si_irq(struct smi_info *smi_info, bool start_timer)
1da177e4 441{
910840f2 442 if ((smi_info->io.irq) && (!smi_info->interrupt_disabled)) {
7aefac26 443 smi_info->interrupt_disabled = true;
0cfec916 444 start_check_enables(smi_info, start_timer);
968bf7cc 445 return true;
1da177e4 446 }
968bf7cc 447 return false;
1da177e4
LT
448}
449
968bf7cc 450static inline bool enable_si_irq(struct smi_info *smi_info)
1da177e4 451{
910840f2 452 if ((smi_info->io.irq) && (smi_info->interrupt_disabled)) {
7aefac26 453 smi_info->interrupt_disabled = false;
0cfec916 454 start_check_enables(smi_info, true);
968bf7cc
CM
455 return true;
456 }
457 return false;
458}
459
460/*
461 * Allocate a message. If unable to allocate, start the interrupt
462 * disable process and return NULL. If able to allocate but
463 * interrupts are disabled, free the message and return NULL after
464 * starting the interrupt enable process.
465 */
466static struct ipmi_smi_msg *alloc_msg_handle_irq(struct smi_info *smi_info)
467{
468 struct ipmi_smi_msg *msg;
469
470 msg = ipmi_alloc_smi_msg();
471 if (!msg) {
0cfec916 472 if (!disable_si_irq(smi_info, true))
968bf7cc
CM
473 smi_info->si_state = SI_NORMAL;
474 } else if (enable_si_irq(smi_info)) {
475 ipmi_free_smi_msg(msg);
476 msg = NULL;
1da177e4 477 }
968bf7cc 478 return msg;
1da177e4
LT
479}
480
481static void handle_flags(struct smi_info *smi_info)
482{
76824852 483retry:
1da177e4
LT
484 if (smi_info->msg_flags & WDT_PRE_TIMEOUT_INT) {
485 /* Watchdog pre-timeout */
64959e2d 486 smi_inc_stat(smi_info, watchdog_pretimeouts);
1da177e4 487
0cfec916 488 start_clear_flags(smi_info, true);
1da177e4 489 smi_info->msg_flags &= ~WDT_PRE_TIMEOUT_INT;
968bf7cc
CM
490 if (smi_info->intf)
491 ipmi_smi_watchdog_pretimeout(smi_info->intf);
1da177e4
LT
492 } else if (smi_info->msg_flags & RECEIVE_MSG_AVAIL) {
493 /* Messages available. */
968bf7cc
CM
494 smi_info->curr_msg = alloc_msg_handle_irq(smi_info);
495 if (!smi_info->curr_msg)
1da177e4 496 return;
1da177e4 497
968bf7cc 498 start_getting_msg_queue(smi_info);
1da177e4
LT
499 } else if (smi_info->msg_flags & EVENT_MSG_BUFFER_FULL) {
500 /* Events available. */
968bf7cc
CM
501 smi_info->curr_msg = alloc_msg_handle_irq(smi_info);
502 if (!smi_info->curr_msg)
1da177e4 503 return;
1da177e4 504
968bf7cc 505 start_getting_events(smi_info);
4064d5ef 506 } else if (smi_info->msg_flags & OEM_DATA_AVAIL &&
c305e3d3 507 smi_info->oem_data_avail_handler) {
4064d5ef
CM
508 if (smi_info->oem_data_avail_handler(smi_info))
509 goto retry;
c305e3d3 510 } else
1da177e4 511 smi_info->si_state = SI_NORMAL;
1da177e4
LT
512}
513
d9b7e4f7
CM
514/*
515 * Global enables we care about.
516 */
517#define GLOBAL_ENABLES_MASK (IPMI_BMC_EVT_MSG_BUFF | IPMI_BMC_RCV_MSG_INTR | \
518 IPMI_BMC_EVT_MSG_INTR)
519
95c97b59
CM
520static u8 current_global_enables(struct smi_info *smi_info, u8 base,
521 bool *irq_on)
d9b7e4f7
CM
522{
523 u8 enables = 0;
524
525 if (smi_info->supports_event_msg_buff)
526 enables |= IPMI_BMC_EVT_MSG_BUFF;
d9b7e4f7 527
910840f2 528 if (((smi_info->io.irq && !smi_info->interrupt_disabled) ||
d0882897
CM
529 smi_info->cannot_disable_irq) &&
530 !smi_info->irq_enable_broken)
d9b7e4f7 531 enables |= IPMI_BMC_RCV_MSG_INTR;
d9b7e4f7
CM
532
533 if (smi_info->supports_event_msg_buff &&
910840f2 534 smi_info->io.irq && !smi_info->interrupt_disabled &&
d0882897 535 !smi_info->irq_enable_broken)
d9b7e4f7 536 enables |= IPMI_BMC_EVT_MSG_INTR;
d9b7e4f7 537
95c97b59
CM
538 *irq_on = enables & (IPMI_BMC_EVT_MSG_INTR | IPMI_BMC_RCV_MSG_INTR);
539
d9b7e4f7
CM
540 return enables;
541}
542
95c97b59
CM
543static void check_bt_irq(struct smi_info *smi_info, bool irq_on)
544{
545 u8 irqstate = smi_info->io.inputb(&smi_info->io, IPMI_BT_INTMASK_REG);
546
547 irqstate &= IPMI_BT_INTMASK_ENABLE_IRQ_BIT;
548
549 if ((bool)irqstate == irq_on)
550 return;
551
552 if (irq_on)
553 smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG,
554 IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
555 else
556 smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG, 0);
557}
558
1da177e4
LT
559static void handle_transaction_done(struct smi_info *smi_info)
560{
561 struct ipmi_smi_msg *msg;
1da177e4 562
f93aae9f 563 debug_timestamp("Done");
1da177e4
LT
564 switch (smi_info->si_state) {
565 case SI_NORMAL:
b0defcdb 566 if (!smi_info->curr_msg)
1da177e4
LT
567 break;
568
569 smi_info->curr_msg->rsp_size
570 = smi_info->handlers->get_result(
571 smi_info->si_sm,
572 smi_info->curr_msg->rsp,
573 IPMI_MAX_MSG_LENGTH);
574
c305e3d3
CM
575 /*
576 * Do this here becase deliver_recv_msg() releases the
577 * lock, and a new message can be put in during the
578 * time the lock is released.
579 */
1da177e4
LT
580 msg = smi_info->curr_msg;
581 smi_info->curr_msg = NULL;
582 deliver_recv_msg(smi_info, msg);
583 break;
584
585 case SI_GETTING_FLAGS:
586 {
587 unsigned char msg[4];
588 unsigned int len;
589
590 /* We got the flags from the SMI, now handle them. */
591 len = smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
592 if (msg[2] != 0) {
c305e3d3 593 /* Error fetching flags, just give up for now. */
1da177e4
LT
594 smi_info->si_state = SI_NORMAL;
595 } else if (len < 4) {
c305e3d3
CM
596 /*
597 * Hmm, no flags. That's technically illegal, but
598 * don't use uninitialized data.
599 */
1da177e4
LT
600 smi_info->si_state = SI_NORMAL;
601 } else {
602 smi_info->msg_flags = msg[3];
603 handle_flags(smi_info);
604 }
605 break;
606 }
607
608 case SI_CLEARING_FLAGS:
1da177e4
LT
609 {
610 unsigned char msg[3];
611
612 /* We cleared the flags. */
613 smi_info->handlers->get_result(smi_info->si_sm, msg, 3);
614 if (msg[2] != 0) {
615 /* Error clearing flags */
910840f2 616 dev_warn(smi_info->io.dev,
279fbd0c 617 "Error clearing flags: %2.2x\n", msg[2]);
1da177e4 618 }
d9b7e4f7 619 smi_info->si_state = SI_NORMAL;
1da177e4
LT
620 break;
621 }
622
623 case SI_GETTING_EVENTS:
624 {
625 smi_info->curr_msg->rsp_size
626 = smi_info->handlers->get_result(
627 smi_info->si_sm,
628 smi_info->curr_msg->rsp,
629 IPMI_MAX_MSG_LENGTH);
630
c305e3d3
CM
631 /*
632 * Do this here becase deliver_recv_msg() releases the
633 * lock, and a new message can be put in during the
634 * time the lock is released.
635 */
1da177e4
LT
636 msg = smi_info->curr_msg;
637 smi_info->curr_msg = NULL;
638 if (msg->rsp[2] != 0) {
639 /* Error getting event, probably done. */
640 msg->done(msg);
641
642 /* Take off the event flag. */
643 smi_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
644 handle_flags(smi_info);
645 } else {
64959e2d 646 smi_inc_stat(smi_info, events);
1da177e4 647
c305e3d3
CM
648 /*
649 * Do this before we deliver the message
650 * because delivering the message releases the
651 * lock and something else can mess with the
652 * state.
653 */
1da177e4
LT
654 handle_flags(smi_info);
655
656 deliver_recv_msg(smi_info, msg);
657 }
658 break;
659 }
660
661 case SI_GETTING_MESSAGES:
662 {
663 smi_info->curr_msg->rsp_size
664 = smi_info->handlers->get_result(
665 smi_info->si_sm,
666 smi_info->curr_msg->rsp,
667 IPMI_MAX_MSG_LENGTH);
668
c305e3d3
CM
669 /*
670 * Do this here becase deliver_recv_msg() releases the
671 * lock, and a new message can be put in during the
672 * time the lock is released.
673 */
1da177e4
LT
674 msg = smi_info->curr_msg;
675 smi_info->curr_msg = NULL;
676 if (msg->rsp[2] != 0) {
677 /* Error getting event, probably done. */
678 msg->done(msg);
679
680 /* Take off the msg flag. */
681 smi_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
682 handle_flags(smi_info);
683 } else {
64959e2d 684 smi_inc_stat(smi_info, incoming_messages);
1da177e4 685
c305e3d3
CM
686 /*
687 * Do this before we deliver the message
688 * because delivering the message releases the
689 * lock and something else can mess with the
690 * state.
691 */
1da177e4
LT
692 handle_flags(smi_info);
693
694 deliver_recv_msg(smi_info, msg);
695 }
696 break;
697 }
698
d9b7e4f7 699 case SI_CHECKING_ENABLES:
1da177e4
LT
700 {
701 unsigned char msg[4];
d9b7e4f7 702 u8 enables;
95c97b59 703 bool irq_on;
1da177e4
LT
704
705 /* We got the flags from the SMI, now handle them. */
706 smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
707 if (msg[2] != 0) {
910840f2 708 dev_warn(smi_info->io.dev,
0849bfec 709 "Couldn't get irq info: %x.\n", msg[2]);
910840f2 710 dev_warn(smi_info->io.dev,
0849bfec 711 "Maybe ok, but ipmi might run very slowly.\n");
1da177e4 712 smi_info->si_state = SI_NORMAL;
d9b7e4f7
CM
713 break;
714 }
95c97b59 715 enables = current_global_enables(smi_info, 0, &irq_on);
910840f2 716 if (smi_info->io.si_type == SI_BT)
95c97b59
CM
717 /* BT has its own interrupt enable bit. */
718 check_bt_irq(smi_info, irq_on);
d9b7e4f7
CM
719 if (enables != (msg[3] & GLOBAL_ENABLES_MASK)) {
720 /* Enables are not correct, fix them. */
1da177e4
LT
721 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
722 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
d9b7e4f7 723 msg[2] = enables | (msg[3] & ~GLOBAL_ENABLES_MASK);
1da177e4
LT
724 smi_info->handlers->start_transaction(
725 smi_info->si_sm, msg, 3);
d9b7e4f7
CM
726 smi_info->si_state = SI_SETTING_ENABLES;
727 } else if (smi_info->supports_event_msg_buff) {
728 smi_info->curr_msg = ipmi_alloc_smi_msg();
729 if (!smi_info->curr_msg) {
730 smi_info->si_state = SI_NORMAL;
731 break;
732 }
5ac7b2fc 733 start_getting_events(smi_info);
d9b7e4f7
CM
734 } else {
735 smi_info->si_state = SI_NORMAL;
1da177e4
LT
736 }
737 break;
738 }
739
d9b7e4f7 740 case SI_SETTING_ENABLES:
1da177e4
LT
741 {
742 unsigned char msg[4];
743
1da177e4 744 smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
d9b7e4f7 745 if (msg[2] != 0)
910840f2 746 dev_warn(smi_info->io.dev,
d9b7e4f7
CM
747 "Could not set the global enables: 0x%x.\n",
748 msg[2]);
749
750 if (smi_info->supports_event_msg_buff) {
751 smi_info->curr_msg = ipmi_alloc_smi_msg();
752 if (!smi_info->curr_msg) {
753 smi_info->si_state = SI_NORMAL;
754 break;
755 }
5ac7b2fc 756 start_getting_events(smi_info);
ee6cd5f8 757 } else {
d9b7e4f7 758 smi_info->si_state = SI_NORMAL;
ee6cd5f8 759 }
ee6cd5f8
CM
760 break;
761 }
1da177e4
LT
762 }
763}
764
c305e3d3
CM
765/*
766 * Called on timeouts and events. Timeouts should pass the elapsed
767 * time, interrupts should pass in zero. Must be called with
768 * si_lock held and interrupts disabled.
769 */
1da177e4
LT
770static enum si_sm_result smi_event_handler(struct smi_info *smi_info,
771 int time)
772{
773 enum si_sm_result si_sm_result;
774
76824852 775restart:
c305e3d3
CM
776 /*
777 * There used to be a loop here that waited a little while
778 * (around 25us) before giving up. That turned out to be
779 * pointless, the minimum delays I was seeing were in the 300us
780 * range, which is far too long to wait in an interrupt. So
781 * we just run until the state machine tells us something
782 * happened or it needs a delay.
783 */
1da177e4
LT
784 si_sm_result = smi_info->handlers->event(smi_info->si_sm, time);
785 time = 0;
786 while (si_sm_result == SI_SM_CALL_WITHOUT_DELAY)
1da177e4 787 si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
1da177e4 788
c305e3d3 789 if (si_sm_result == SI_SM_TRANSACTION_COMPLETE) {
64959e2d 790 smi_inc_stat(smi_info, complete_transactions);
1da177e4
LT
791
792 handle_transaction_done(smi_info);
d9dffd2a 793 goto restart;
c305e3d3 794 } else if (si_sm_result == SI_SM_HOSED) {
64959e2d 795 smi_inc_stat(smi_info, hosed_count);
1da177e4 796
c305e3d3
CM
797 /*
798 * Do the before return_hosed_msg, because that
799 * releases the lock.
800 */
1da177e4
LT
801 smi_info->si_state = SI_NORMAL;
802 if (smi_info->curr_msg != NULL) {
c305e3d3
CM
803 /*
804 * If we were handling a user message, format
805 * a response to send to the upper layer to
806 * tell it about the error.
807 */
4d7cbac7 808 return_hosed_msg(smi_info, IPMI_ERR_UNSPECIFIED);
1da177e4 809 }
d9dffd2a 810 goto restart;
1da177e4
LT
811 }
812
4ea18425
CM
813 /*
814 * We prefer handling attn over new messages. But don't do
815 * this if there is not yet an upper layer to handle anything.
816 */
a8df150c
CM
817 if (likely(smi_info->intf) &&
818 (si_sm_result == SI_SM_ATTN || smi_info->got_attn)) {
1da177e4
LT
819 unsigned char msg[2];
820
a8df150c
CM
821 if (smi_info->si_state != SI_NORMAL) {
822 /*
823 * We got an ATTN, but we are doing something else.
824 * Handle the ATTN later.
825 */
826 smi_info->got_attn = true;
827 } else {
828 smi_info->got_attn = false;
829 smi_inc_stat(smi_info, attentions);
1da177e4 830
a8df150c
CM
831 /*
832 * Got a attn, send down a get message flags to see
833 * what's causing it. It would be better to handle
834 * this in the upper layer, but due to the way
835 * interrupts work with the SMI, that's not really
836 * possible.
837 */
838 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
839 msg[1] = IPMI_GET_MSG_FLAGS_CMD;
1da177e4 840
0cfec916 841 start_new_msg(smi_info, msg, 2);
a8df150c
CM
842 smi_info->si_state = SI_GETTING_FLAGS;
843 goto restart;
844 }
1da177e4
LT
845 }
846
847 /* If we are currently idle, try to start the next message. */
848 if (si_sm_result == SI_SM_IDLE) {
64959e2d 849 smi_inc_stat(smi_info, idles);
1da177e4
LT
850
851 si_sm_result = start_next_msg(smi_info);
852 if (si_sm_result != SI_SM_IDLE)
853 goto restart;
c305e3d3 854 }
1da177e4
LT
855
856 if ((si_sm_result == SI_SM_IDLE)
c305e3d3
CM
857 && (atomic_read(&smi_info->req_events))) {
858 /*
859 * We are idle and the upper layer requested that I fetch
860 * events, so do so.
861 */
55162fb1 862 atomic_set(&smi_info->req_events, 0);
1da177e4 863
d9b7e4f7
CM
864 /*
865 * Take this opportunity to check the interrupt and
866 * message enable state for the BMC. The BMC can be
867 * asynchronously reset, and may thus get interrupts
868 * disable and messages disabled.
869 */
910840f2 870 if (smi_info->supports_event_msg_buff || smi_info->io.irq) {
0cfec916 871 start_check_enables(smi_info, true);
d9b7e4f7
CM
872 } else {
873 smi_info->curr_msg = alloc_msg_handle_irq(smi_info);
874 if (!smi_info->curr_msg)
875 goto out;
1da177e4 876
d9b7e4f7
CM
877 start_getting_events(smi_info);
878 }
1da177e4
LT
879 goto restart;
880 }
314ef52f
CM
881
882 if (si_sm_result == SI_SM_IDLE && smi_info->timer_running) {
883 /* Ok it if fails, the timer will just go off. */
884 if (del_timer(&smi_info->si_timer))
885 smi_info->timer_running = false;
886 }
887
76824852 888out:
1da177e4
LT
889 return si_sm_result;
890}
891
89986496
CM
892static void check_start_timer_thread(struct smi_info *smi_info)
893{
894 if (smi_info->si_state == SI_NORMAL && smi_info->curr_msg == NULL) {
895 smi_mod_timer(smi_info, jiffies + SI_TIMEOUT_JIFFIES);
896
897 if (smi_info->thread)
898 wake_up_process(smi_info->thread);
899
900 start_next_msg(smi_info);
901 smi_event_handler(smi_info, 0);
902 }
903}
904
82802f96 905static void flush_messages(void *send_info)
e45361d7 906{
82802f96 907 struct smi_info *smi_info = send_info;
e45361d7
HK
908 enum si_sm_result result;
909
910 /*
911 * Currently, this function is called only in run-to-completion
912 * mode. This means we are single-threaded, no need for locks.
913 */
914 result = smi_event_handler(smi_info, 0);
915 while (result != SI_SM_IDLE) {
916 udelay(SI_SHORT_TIMEOUT_USEC);
917 result = smi_event_handler(smi_info, SI_SHORT_TIMEOUT_USEC);
918 }
919}
920
1da177e4 921static void sender(void *send_info,
99ab32f3 922 struct ipmi_smi_msg *msg)
1da177e4
LT
923{
924 struct smi_info *smi_info = send_info;
1da177e4 925 unsigned long flags;
1da177e4 926
f93aae9f 927 debug_timestamp("Enqueue");
1da177e4
LT
928
929 if (smi_info->run_to_completion) {
bda4c30a 930 /*
82802f96
HK
931 * If we are running to completion, start it. Upper
932 * layer will call flush_messages to clear it out.
bda4c30a 933 */
9f812704 934 smi_info->waiting_msg = msg;
1da177e4 935 return;
1da177e4 936 }
1da177e4 937
f60adf42 938 spin_lock_irqsave(&smi_info->si_lock, flags);
1d86e29b
CM
939 /*
940 * The following two lines don't need to be under the lock for
941 * the lock's sake, but they do need SMP memory barriers to
942 * avoid getting things out of order. We are already claiming
943 * the lock, anyway, so just do it under the lock to avoid the
944 * ordering problem.
945 */
946 BUG_ON(smi_info->waiting_msg);
947 smi_info->waiting_msg = msg;
89986496 948 check_start_timer_thread(smi_info);
bda4c30a 949 spin_unlock_irqrestore(&smi_info->si_lock, flags);
1da177e4
LT
950}
951
7aefac26 952static void set_run_to_completion(void *send_info, bool i_run_to_completion)
1da177e4
LT
953{
954 struct smi_info *smi_info = send_info;
1da177e4
LT
955
956 smi_info->run_to_completion = i_run_to_completion;
e45361d7
HK
957 if (i_run_to_completion)
958 flush_messages(smi_info);
1da177e4
LT
959}
960
ae74e823
MW
961/*
962 * Use -1 in the nsec value of the busy waiting timespec to tell that
963 * we are spinning in kipmid looking for something and not delaying
964 * between checks
965 */
48862ea2 966static inline void ipmi_si_set_not_busy(struct timespec64 *ts)
ae74e823
MW
967{
968 ts->tv_nsec = -1;
969}
48862ea2 970static inline int ipmi_si_is_busy(struct timespec64 *ts)
ae74e823
MW
971{
972 return ts->tv_nsec != -1;
973}
974
cc4cbe90
AB
975static inline int ipmi_thread_busy_wait(enum si_sm_result smi_result,
976 const struct smi_info *smi_info,
48862ea2 977 struct timespec64 *busy_until)
ae74e823
MW
978{
979 unsigned int max_busy_us = 0;
980
981 if (smi_info->intf_num < num_max_busy_us)
982 max_busy_us = kipmid_max_busy_us[smi_info->intf_num];
983 if (max_busy_us == 0 || smi_result != SI_SM_CALL_WITH_DELAY)
984 ipmi_si_set_not_busy(busy_until);
985 else if (!ipmi_si_is_busy(busy_until)) {
48862ea2
JS
986 getnstimeofday64(busy_until);
987 timespec64_add_ns(busy_until, max_busy_us*NSEC_PER_USEC);
ae74e823 988 } else {
48862ea2
JS
989 struct timespec64 now;
990
991 getnstimeofday64(&now);
992 if (unlikely(timespec64_compare(&now, busy_until) > 0)) {
ae74e823
MW
993 ipmi_si_set_not_busy(busy_until);
994 return 0;
995 }
996 }
997 return 1;
998}
999
1000
1001/*
1002 * A busy-waiting loop for speeding up IPMI operation.
1003 *
1004 * Lousy hardware makes this hard. This is only enabled for systems
1005 * that are not BT and do not have interrupts. It starts spinning
1006 * when an operation is complete or until max_busy tells it to stop
1007 * (if that is enabled). See the paragraph on kimid_max_busy_us in
1008 * Documentation/IPMI.txt for details.
1009 */
a9a2c44f
CM
1010static int ipmi_thread(void *data)
1011{
1012 struct smi_info *smi_info = data;
e9a705a0 1013 unsigned long flags;
a9a2c44f 1014 enum si_sm_result smi_result;
48862ea2 1015 struct timespec64 busy_until;
a9a2c44f 1016
ae74e823 1017 ipmi_si_set_not_busy(&busy_until);
8698a745 1018 set_user_nice(current, MAX_NICE);
e9a705a0 1019 while (!kthread_should_stop()) {
ae74e823
MW
1020 int busy_wait;
1021
a9a2c44f 1022 spin_lock_irqsave(&(smi_info->si_lock), flags);
8a3628d5 1023 smi_result = smi_event_handler(smi_info, 0);
48e8ac29
BS
1024
1025 /*
1026 * If the driver is doing something, there is a possible
1027 * race with the timer. If the timer handler see idle,
1028 * and the thread here sees something else, the timer
1029 * handler won't restart the timer even though it is
1030 * required. So start it here if necessary.
1031 */
1032 if (smi_result != SI_SM_IDLE && !smi_info->timer_running)
1033 smi_mod_timer(smi_info, jiffies + SI_TIMEOUT_JIFFIES);
1034
a9a2c44f 1035 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
ae74e823
MW
1036 busy_wait = ipmi_thread_busy_wait(smi_result, smi_info,
1037 &busy_until);
c305e3d3
CM
1038 if (smi_result == SI_SM_CALL_WITHOUT_DELAY)
1039 ; /* do nothing */
ae74e823 1040 else if (smi_result == SI_SM_CALL_WITH_DELAY && busy_wait)
33979734 1041 schedule();
89986496
CM
1042 else if (smi_result == SI_SM_IDLE) {
1043 if (atomic_read(&smi_info->need_watch)) {
1044 schedule_timeout_interruptible(100);
1045 } else {
1046 /* Wait to be woken up when we are needed. */
1047 __set_current_state(TASK_INTERRUPTIBLE);
1048 schedule();
1049 }
1050 } else
8d1f66dc 1051 schedule_timeout_interruptible(1);
a9a2c44f 1052 }
a9a2c44f
CM
1053 return 0;
1054}
1055
1056
1da177e4
LT
1057static void poll(void *send_info)
1058{
1059 struct smi_info *smi_info = send_info;
f60adf42 1060 unsigned long flags = 0;
7aefac26 1061 bool run_to_completion = smi_info->run_to_completion;
1da177e4 1062
15c62e10
CM
1063 /*
1064 * Make sure there is some delay in the poll loop so we can
1065 * drive time forward and timeout things.
1066 */
1067 udelay(10);
f60adf42
CM
1068 if (!run_to_completion)
1069 spin_lock_irqsave(&smi_info->si_lock, flags);
15c62e10 1070 smi_event_handler(smi_info, 10);
f60adf42
CM
1071 if (!run_to_completion)
1072 spin_unlock_irqrestore(&smi_info->si_lock, flags);
1da177e4
LT
1073}
1074
1075static void request_events(void *send_info)
1076{
1077 struct smi_info *smi_info = send_info;
1078
b874b985 1079 if (!smi_info->has_event_buffer)
b361e27b
CM
1080 return;
1081
1da177e4
LT
1082 atomic_set(&smi_info->req_events, 1);
1083}
1084
7aefac26 1085static void set_need_watch(void *send_info, bool enable)
89986496
CM
1086{
1087 struct smi_info *smi_info = send_info;
1088 unsigned long flags;
1089
1090 atomic_set(&smi_info->need_watch, enable);
1091 spin_lock_irqsave(&smi_info->si_lock, flags);
1092 check_start_timer_thread(smi_info);
1093 spin_unlock_irqrestore(&smi_info->si_lock, flags);
1094}
1095
1da177e4
LT
1096static void smi_timeout(unsigned long data)
1097{
1098 struct smi_info *smi_info = (struct smi_info *) data;
1099 enum si_sm_result smi_result;
1100 unsigned long flags;
1101 unsigned long jiffies_now;
c4edff1c 1102 long time_diff;
3326f4f2 1103 long timeout;
1da177e4 1104
1da177e4 1105 spin_lock_irqsave(&(smi_info->si_lock), flags);
f93aae9f
JS
1106 debug_timestamp("Timer");
1107
1da177e4 1108 jiffies_now = jiffies;
c4edff1c 1109 time_diff = (((long)jiffies_now - (long)smi_info->last_timeout_jiffies)
1da177e4
LT
1110 * SI_USEC_PER_JIFFY);
1111 smi_result = smi_event_handler(smi_info, time_diff);
1112
910840f2 1113 if ((smi_info->io.irq) && (!smi_info->interrupt_disabled)) {
1da177e4 1114 /* Running with interrupts, only do long timeouts. */
3326f4f2 1115 timeout = jiffies + SI_TIMEOUT_JIFFIES;
64959e2d 1116 smi_inc_stat(smi_info, long_timeouts);
3326f4f2 1117 goto do_mod_timer;
1da177e4
LT
1118 }
1119
c305e3d3
CM
1120 /*
1121 * If the state machine asks for a short delay, then shorten
1122 * the timer timeout.
1123 */
1da177e4 1124 if (smi_result == SI_SM_CALL_WITH_DELAY) {
64959e2d 1125 smi_inc_stat(smi_info, short_timeouts);
3326f4f2 1126 timeout = jiffies + 1;
1da177e4 1127 } else {
64959e2d 1128 smi_inc_stat(smi_info, long_timeouts);
3326f4f2 1129 timeout = jiffies + SI_TIMEOUT_JIFFIES;
1da177e4
LT
1130 }
1131
76824852 1132do_mod_timer:
3326f4f2 1133 if (smi_result != SI_SM_IDLE)
48e8ac29
BS
1134 smi_mod_timer(smi_info, timeout);
1135 else
1136 smi_info->timer_running = false;
1137 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
1da177e4
LT
1138}
1139
4f3e8199 1140irqreturn_t ipmi_si_irq_handler(int irq, void *data)
1da177e4
LT
1141{
1142 struct smi_info *smi_info = data;
1143 unsigned long flags;
1da177e4 1144
4f3e8199
CM
1145 if (smi_info->io.si_type == SI_BT)
1146 /* We need to clear the IRQ flag for the BT interface. */
1147 smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG,
1148 IPMI_BT_INTMASK_CLEAR_IRQ_BIT
1149 | IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
1150
1da177e4
LT
1151 spin_lock_irqsave(&(smi_info->si_lock), flags);
1152
64959e2d 1153 smi_inc_stat(smi_info, interrupts);
1da177e4 1154
f93aae9f
JS
1155 debug_timestamp("Interrupt");
1156
1da177e4 1157 smi_event_handler(smi_info, 0);
1da177e4
LT
1158 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
1159 return IRQ_HANDLED;
1160}
1161
453823ba
CM
1162static int smi_start_processing(void *send_info,
1163 ipmi_smi_t intf)
1164{
1165 struct smi_info *new_smi = send_info;
a51f4a81 1166 int enable = 0;
453823ba
CM
1167
1168 new_smi->intf = intf;
1169
1170 /* Set up the timer that drives the interface. */
1171 setup_timer(&new_smi->si_timer, smi_timeout, (long)new_smi);
48e8ac29 1172 smi_mod_timer(new_smi, jiffies + SI_TIMEOUT_JIFFIES);
453823ba 1173
27f972d3 1174 /* Try to claim any interrupts. */
4f3e8199
CM
1175 if (new_smi->io.irq_setup) {
1176 new_smi->io.irq_handler_data = new_smi;
1177 new_smi->io.irq_setup(&new_smi->io);
1178 }
27f972d3 1179
a51f4a81
CM
1180 /*
1181 * Check if the user forcefully enabled the daemon.
1182 */
1183 if (new_smi->intf_num < num_force_kipmid)
1184 enable = force_kipmid[new_smi->intf_num];
df3fe8de
CM
1185 /*
1186 * The BT interface is efficient enough to not need a thread,
1187 * and there is no need for a thread if we have interrupts.
1188 */
910840f2 1189 else if ((new_smi->io.si_type != SI_BT) && (!new_smi->io.irq))
a51f4a81
CM
1190 enable = 1;
1191
1192 if (enable) {
453823ba
CM
1193 new_smi->thread = kthread_run(ipmi_thread, new_smi,
1194 "kipmi%d", new_smi->intf_num);
1195 if (IS_ERR(new_smi->thread)) {
910840f2 1196 dev_notice(new_smi->io.dev, "Could not start"
279fbd0c
MS
1197 " kernel thread due to error %ld, only using"
1198 " timers to drive the interface\n",
1199 PTR_ERR(new_smi->thread));
453823ba
CM
1200 new_smi->thread = NULL;
1201 }
1202 }
1203
1204 return 0;
1205}
9dbf68f9 1206
16f4232c
ZY
1207static int get_smi_info(void *send_info, struct ipmi_smi_info *data)
1208{
1209 struct smi_info *smi = send_info;
1210
910840f2
CM
1211 data->addr_src = smi->io.addr_source;
1212 data->dev = smi->io.dev;
bb398a4c 1213 data->addr_info = smi->io.addr_info;
910840f2 1214 get_device(smi->io.dev);
16f4232c
ZY
1215
1216 return 0;
1217}
1218
7aefac26 1219static void set_maintenance_mode(void *send_info, bool enable)
b9675136
CM
1220{
1221 struct smi_info *smi_info = send_info;
1222
1223 if (!enable)
1224 atomic_set(&smi_info->req_events, 0);
1225}
1226
81d02b7f 1227static const struct ipmi_smi_handlers handlers = {
1da177e4 1228 .owner = THIS_MODULE,
453823ba 1229 .start_processing = smi_start_processing,
16f4232c 1230 .get_smi_info = get_smi_info,
1da177e4
LT
1231 .sender = sender,
1232 .request_events = request_events,
89986496 1233 .set_need_watch = set_need_watch,
b9675136 1234 .set_maintenance_mode = set_maintenance_mode,
1da177e4 1235 .set_run_to_completion = set_run_to_completion,
82802f96 1236 .flush_messages = flush_messages,
1da177e4
LT
1237 .poll = poll,
1238};
1239
b0defcdb 1240static LIST_HEAD(smi_infos);
d6dfd131 1241static DEFINE_MUTEX(smi_infos_lock);
b0defcdb 1242static int smi_num; /* Used to sequence the SMIs */
1da177e4 1243
99ee6735 1244static const char * const addr_space_to_str[] = { "i/o", "mem" };
b361e27b 1245
a51f4a81
CM
1246module_param_array(force_kipmid, int, &num_force_kipmid, 0);
1247MODULE_PARM_DESC(force_kipmid, "Force the kipmi daemon to be enabled (1) or"
1248 " disabled(0). Normally the IPMI driver auto-detects"
1249 " this, but the value may be overridden by this parm.");
7aefac26 1250module_param(unload_when_empty, bool, 0);
b361e27b
CM
1251MODULE_PARM_DESC(unload_when_empty, "Unload the module if no interfaces are"
1252 " specified or found, default is 1. Setting to 0"
1253 " is useful for hot add of devices using hotmod.");
ae74e823
MW
1254module_param_array(kipmid_max_busy_us, uint, &num_max_busy_us, 0644);
1255MODULE_PARM_DESC(kipmid_max_busy_us,
1256 "Max time (in microseconds) to busy-wait for IPMI data before"
1257 " sleeping. 0 (default) means to wait forever. Set to 100-500"
1258 " if kipmid is using up a lot of CPU time.");
1da177e4 1259
4f3e8199
CM
1260void ipmi_irq_finish_setup(struct si_sm_io *io)
1261{
1262 if (io->si_type == SI_BT)
1263 /* Enable the interrupt in the BT interface. */
1264 io->outputb(io, IPMI_BT_INTMASK_REG,
1265 IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
1266}
1da177e4 1267
4f3e8199 1268void ipmi_irq_start_cleanup(struct si_sm_io *io)
1da177e4 1269{
4f3e8199 1270 if (io->si_type == SI_BT)
b0defcdb 1271 /* Disable the interrupt in the BT interface. */
4f3e8199
CM
1272 io->outputb(io, IPMI_BT_INTMASK_REG, 0);
1273}
1274
1275static void std_irq_cleanup(struct si_sm_io *io)
1276{
1277 ipmi_irq_start_cleanup(io);
1278 free_irq(io->irq, io->irq_handler_data);
1da177e4 1279}
1da177e4 1280
4f3e8199 1281int ipmi_std_irq_setup(struct si_sm_io *io)
1da177e4
LT
1282{
1283 int rv;
1284
4f3e8199 1285 if (!io->irq)
1da177e4
LT
1286 return 0;
1287
4f3e8199
CM
1288 rv = request_irq(io->irq,
1289 ipmi_si_irq_handler,
1290 IRQF_SHARED,
1291 DEVICE_NAME,
1292 io->irq_handler_data);
1da177e4 1293 if (rv) {
4f3e8199 1294 dev_warn(io->dev, "%s unable to claim interrupt %d,"
279fbd0c 1295 " running polled\n",
4f3e8199
CM
1296 DEVICE_NAME, io->irq);
1297 io->irq = 0;
1da177e4 1298 } else {
4f3e8199
CM
1299 io->irq_cleanup = std_irq_cleanup;
1300 ipmi_irq_finish_setup(io);
1301 dev_info(io->dev, "Using irq %d\n", io->irq);
1da177e4
LT
1302 }
1303
1304 return rv;
1305}
1306
81d02b7f 1307static unsigned char port_inb(const struct si_sm_io *io, unsigned int offset)
1da177e4 1308{
b0defcdb 1309 unsigned int addr = io->addr_data;
1da177e4 1310
b0defcdb 1311 return inb(addr + (offset * io->regspacing));
1da177e4
LT
1312}
1313
81d02b7f 1314static void port_outb(const struct si_sm_io *io, unsigned int offset,
1da177e4
LT
1315 unsigned char b)
1316{
b0defcdb 1317 unsigned int addr = io->addr_data;
1da177e4 1318
b0defcdb 1319 outb(b, addr + (offset * io->regspacing));
1da177e4
LT
1320}
1321
81d02b7f 1322static unsigned char port_inw(const struct si_sm_io *io, unsigned int offset)
1da177e4 1323{
b0defcdb 1324 unsigned int addr = io->addr_data;
1da177e4 1325
b0defcdb 1326 return (inw(addr + (offset * io->regspacing)) >> io->regshift) & 0xff;
1da177e4
LT
1327}
1328
81d02b7f 1329static void port_outw(const struct si_sm_io *io, unsigned int offset,
1da177e4
LT
1330 unsigned char b)
1331{
b0defcdb 1332 unsigned int addr = io->addr_data;
1da177e4 1333
b0defcdb 1334 outw(b << io->regshift, addr + (offset * io->regspacing));
1da177e4
LT
1335}
1336
81d02b7f 1337static unsigned char port_inl(const struct si_sm_io *io, unsigned int offset)
1da177e4 1338{
b0defcdb 1339 unsigned int addr = io->addr_data;
1da177e4 1340
b0defcdb 1341 return (inl(addr + (offset * io->regspacing)) >> io->regshift) & 0xff;
1da177e4
LT
1342}
1343
81d02b7f 1344static void port_outl(const struct si_sm_io *io, unsigned int offset,
1da177e4
LT
1345 unsigned char b)
1346{
b0defcdb 1347 unsigned int addr = io->addr_data;
1da177e4 1348
b0defcdb 1349 outl(b << io->regshift, addr+(offset * io->regspacing));
1da177e4
LT
1350}
1351
e1eeb7f8 1352static void port_cleanup(struct si_sm_io *io)
1da177e4 1353{
e1eeb7f8 1354 unsigned int addr = io->addr_data;
d61a3ead 1355 int idx;
1da177e4 1356
b0defcdb 1357 if (addr) {
e1eeb7f8
CM
1358 for (idx = 0; idx < io->io_size; idx++)
1359 release_region(addr + idx * io->regspacing,
1360 io->regsize);
1da177e4 1361 }
1da177e4
LT
1362}
1363
e1eeb7f8 1364static int port_setup(struct si_sm_io *io)
1da177e4 1365{
e1eeb7f8 1366 unsigned int addr = io->addr_data;
d61a3ead 1367 int idx;
1da177e4 1368
b0defcdb 1369 if (!addr)
1da177e4
LT
1370 return -ENODEV;
1371
e1eeb7f8 1372 io->io_cleanup = port_cleanup;
1da177e4 1373
c305e3d3
CM
1374 /*
1375 * Figure out the actual inb/inw/inl/etc routine to use based
1376 * upon the register size.
1377 */
e1eeb7f8 1378 switch (io->regsize) {
1da177e4 1379 case 1:
e1eeb7f8
CM
1380 io->inputb = port_inb;
1381 io->outputb = port_outb;
1da177e4
LT
1382 break;
1383 case 2:
e1eeb7f8
CM
1384 io->inputb = port_inw;
1385 io->outputb = port_outw;
1da177e4
LT
1386 break;
1387 case 4:
e1eeb7f8
CM
1388 io->inputb = port_inl;
1389 io->outputb = port_outl;
1da177e4
LT
1390 break;
1391 default:
e1eeb7f8
CM
1392 dev_warn(io->dev, "Invalid register size: %d\n",
1393 io->regsize);
1da177e4
LT
1394 return -EINVAL;
1395 }
1396
c305e3d3
CM
1397 /*
1398 * Some BIOSes reserve disjoint I/O regions in their ACPI
d61a3ead
CM
1399 * tables. This causes problems when trying to register the
1400 * entire I/O region. Therefore we must register each I/O
1401 * port separately.
1402 */
e1eeb7f8
CM
1403 for (idx = 0; idx < io->io_size; idx++) {
1404 if (request_region(addr + idx * io->regspacing,
1405 io->regsize, DEVICE_NAME) == NULL) {
d61a3ead 1406 /* Undo allocations */
76824852 1407 while (idx--)
e1eeb7f8
CM
1408 release_region(addr + idx * io->regspacing,
1409 io->regsize);
d61a3ead
CM
1410 return -EIO;
1411 }
1412 }
1da177e4
LT
1413 return 0;
1414}
1415
81d02b7f
CM
1416static unsigned char intf_mem_inb(const struct si_sm_io *io,
1417 unsigned int offset)
1da177e4
LT
1418{
1419 return readb((io->addr)+(offset * io->regspacing));
1420}
1421
81d02b7f
CM
1422static void intf_mem_outb(const struct si_sm_io *io, unsigned int offset,
1423 unsigned char b)
1da177e4
LT
1424{
1425 writeb(b, (io->addr)+(offset * io->regspacing));
1426}
1427
81d02b7f
CM
1428static unsigned char intf_mem_inw(const struct si_sm_io *io,
1429 unsigned int offset)
1da177e4
LT
1430{
1431 return (readw((io->addr)+(offset * io->regspacing)) >> io->regshift)
64d9fe69 1432 & 0xff;
1da177e4
LT
1433}
1434
81d02b7f
CM
1435static void intf_mem_outw(const struct si_sm_io *io, unsigned int offset,
1436 unsigned char b)
1da177e4
LT
1437{
1438 writeb(b << io->regshift, (io->addr)+(offset * io->regspacing));
1439}
1440
81d02b7f
CM
1441static unsigned char intf_mem_inl(const struct si_sm_io *io,
1442 unsigned int offset)
1da177e4
LT
1443{
1444 return (readl((io->addr)+(offset * io->regspacing)) >> io->regshift)
64d9fe69 1445 & 0xff;
1da177e4
LT
1446}
1447
81d02b7f
CM
1448static void intf_mem_outl(const struct si_sm_io *io, unsigned int offset,
1449 unsigned char b)
1da177e4
LT
1450{
1451 writel(b << io->regshift, (io->addr)+(offset * io->regspacing));
1452}
1453
1454#ifdef readq
81d02b7f 1455static unsigned char mem_inq(const struct si_sm_io *io, unsigned int offset)
1da177e4
LT
1456{
1457 return (readq((io->addr)+(offset * io->regspacing)) >> io->regshift)
64d9fe69 1458 & 0xff;
1da177e4
LT
1459}
1460
81d02b7f 1461static void mem_outq(const struct si_sm_io *io, unsigned int offset,
1da177e4
LT
1462 unsigned char b)
1463{
1464 writeq(b << io->regshift, (io->addr)+(offset * io->regspacing));
1465}
1466#endif
1467
e1eeb7f8 1468static void mem_region_cleanup(struct si_sm_io *io, int num)
1da177e4 1469{
e1eeb7f8 1470 unsigned long addr = io->addr_data;
57a38f13
CM
1471 int idx;
1472
1473 for (idx = 0; idx < num; idx++)
e1eeb7f8
CM
1474 release_mem_region(addr + idx * io->regspacing,
1475 io->regsize);
57a38f13 1476}
1da177e4 1477
e1eeb7f8 1478static void mem_cleanup(struct si_sm_io *io)
57a38f13 1479{
e1eeb7f8
CM
1480 if (io->addr) {
1481 iounmap(io->addr);
1482 mem_region_cleanup(io, io->io_size);
1da177e4 1483 }
1da177e4
LT
1484}
1485
e1eeb7f8 1486static int mem_setup(struct si_sm_io *io)
1da177e4 1487{
e1eeb7f8 1488 unsigned long addr = io->addr_data;
57a38f13 1489 int mapsize, idx;
1da177e4 1490
b0defcdb 1491 if (!addr)
1da177e4
LT
1492 return -ENODEV;
1493
e1eeb7f8 1494 io->io_cleanup = mem_cleanup;
1da177e4 1495
c305e3d3
CM
1496 /*
1497 * Figure out the actual readb/readw/readl/etc routine to use based
1498 * upon the register size.
1499 */
e1eeb7f8 1500 switch (io->regsize) {
1da177e4 1501 case 1:
e1eeb7f8
CM
1502 io->inputb = intf_mem_inb;
1503 io->outputb = intf_mem_outb;
1da177e4
LT
1504 break;
1505 case 2:
e1eeb7f8
CM
1506 io->inputb = intf_mem_inw;
1507 io->outputb = intf_mem_outw;
1da177e4
LT
1508 break;
1509 case 4:
e1eeb7f8
CM
1510 io->inputb = intf_mem_inl;
1511 io->outputb = intf_mem_outl;
1da177e4
LT
1512 break;
1513#ifdef readq
1514 case 8:
e1eeb7f8
CM
1515 io->inputb = mem_inq;
1516 io->outputb = mem_outq;
1da177e4
LT
1517 break;
1518#endif
1519 default:
e1eeb7f8
CM
1520 dev_warn(io->dev, "Invalid register size: %d\n",
1521 io->regsize);
1da177e4
LT
1522 return -EINVAL;
1523 }
1524
57a38f13
CM
1525 /*
1526 * Some BIOSes reserve disjoint memory regions in their ACPI
1527 * tables. This causes problems when trying to request the
1528 * entire region. Therefore we must request each register
1529 * separately.
1530 */
e1eeb7f8
CM
1531 for (idx = 0; idx < io->io_size; idx++) {
1532 if (request_mem_region(addr + idx * io->regspacing,
1533 io->regsize, DEVICE_NAME) == NULL) {
57a38f13 1534 /* Undo allocations */
e1eeb7f8 1535 mem_region_cleanup(io, idx);
57a38f13
CM
1536 return -EIO;
1537 }
1538 }
1539
c305e3d3
CM
1540 /*
1541 * Calculate the total amount of memory to claim. This is an
1da177e4
LT
1542 * unusual looking calculation, but it avoids claiming any
1543 * more memory than it has to. It will claim everything
1544 * between the first address to the end of the last full
c305e3d3
CM
1545 * register.
1546 */
e1eeb7f8
CM
1547 mapsize = ((io->io_size * io->regspacing)
1548 - (io->regspacing - io->regsize));
1549 io->addr = ioremap(addr, mapsize);
1550 if (io->addr == NULL) {
1551 mem_region_cleanup(io, io->io_size);
1da177e4
LT
1552 return -EIO;
1553 }
1554 return 0;
1555}
1556
de5e2ddf
ED
1557static struct smi_info *smi_info_alloc(void)
1558{
1559 struct smi_info *info = kzalloc(sizeof(*info), GFP_KERNEL);
1560
f60adf42 1561 if (info)
de5e2ddf 1562 spin_lock_init(&info->si_lock);
de5e2ddf
ED
1563 return info;
1564}
1565
40112ae7 1566static int wait_for_msg_done(struct smi_info *smi_info)
1da177e4 1567{
50c812b2 1568 enum si_sm_result smi_result;
1da177e4
LT
1569
1570 smi_result = smi_info->handlers->event(smi_info->si_sm, 0);
c305e3d3 1571 for (;;) {
c3e7e791
CM
1572 if (smi_result == SI_SM_CALL_WITH_DELAY ||
1573 smi_result == SI_SM_CALL_WITH_TICK_DELAY) {
da4cd8df 1574 schedule_timeout_uninterruptible(1);
1da177e4 1575 smi_result = smi_info->handlers->event(
e21404dc 1576 smi_info->si_sm, jiffies_to_usecs(1));
c305e3d3 1577 } else if (smi_result == SI_SM_CALL_WITHOUT_DELAY) {
1da177e4
LT
1578 smi_result = smi_info->handlers->event(
1579 smi_info->si_sm, 0);
c305e3d3 1580 } else
1da177e4
LT
1581 break;
1582 }
40112ae7 1583 if (smi_result == SI_SM_HOSED)
c305e3d3
CM
1584 /*
1585 * We couldn't get the state machine to run, so whatever's at
1586 * the port is probably not an IPMI SMI interface.
1587 */
40112ae7
CM
1588 return -ENODEV;
1589
1590 return 0;
1591}
1592
1593static int try_get_dev_id(struct smi_info *smi_info)
1594{
1595 unsigned char msg[2];
1596 unsigned char *resp;
1597 unsigned long resp_len;
1598 int rv = 0;
1599
1600 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1601 if (!resp)
1602 return -ENOMEM;
1603
1604 /*
1605 * Do a Get Device ID command, since it comes back with some
1606 * useful info.
1607 */
1608 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1609 msg[1] = IPMI_GET_DEVICE_ID_CMD;
1610 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
1611
1612 rv = wait_for_msg_done(smi_info);
1613 if (rv)
1da177e4 1614 goto out;
1da177e4 1615
1da177e4
LT
1616 resp_len = smi_info->handlers->get_result(smi_info->si_sm,
1617 resp, IPMI_MAX_MSG_LENGTH);
1da177e4 1618
d8c98618 1619 /* Check and record info from the get device id, in case we need it. */
c468f911
JK
1620 rv = ipmi_demangle_device_id(resp[0] >> 2, resp[1],
1621 resp + 2, resp_len - 2, &smi_info->device_id);
1da177e4 1622
76824852 1623out:
1da177e4
LT
1624 kfree(resp);
1625 return rv;
1626}
1627
d0882897 1628static int get_global_enables(struct smi_info *smi_info, u8 *enables)
1e7d6a45
CM
1629{
1630 unsigned char msg[3];
1631 unsigned char *resp;
1632 unsigned long resp_len;
1633 int rv;
1634
1635 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
d0882897
CM
1636 if (!resp)
1637 return -ENOMEM;
1e7d6a45
CM
1638
1639 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1640 msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
1641 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
1642
1643 rv = wait_for_msg_done(smi_info);
1644 if (rv) {
910840f2 1645 dev_warn(smi_info->io.dev,
d0882897
CM
1646 "Error getting response from get global enables command: %d\n",
1647 rv);
1e7d6a45
CM
1648 goto out;
1649 }
1650
1651 resp_len = smi_info->handlers->get_result(smi_info->si_sm,
1652 resp, IPMI_MAX_MSG_LENGTH);
1653
1654 if (resp_len < 4 ||
1655 resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
1656 resp[1] != IPMI_GET_BMC_GLOBAL_ENABLES_CMD ||
1657 resp[2] != 0) {
910840f2 1658 dev_warn(smi_info->io.dev,
d0882897
CM
1659 "Invalid return from get global enables command: %ld %x %x %x\n",
1660 resp_len, resp[0], resp[1], resp[2]);
1e7d6a45
CM
1661 rv = -EINVAL;
1662 goto out;
d0882897
CM
1663 } else {
1664 *enables = resp[3];
1e7d6a45
CM
1665 }
1666
d0882897
CM
1667out:
1668 kfree(resp);
1669 return rv;
1670}
1671
1672/*
1673 * Returns 1 if it gets an error from the command.
1674 */
1675static int set_global_enables(struct smi_info *smi_info, u8 enables)
1676{
1677 unsigned char msg[3];
1678 unsigned char *resp;
1679 unsigned long resp_len;
1680 int rv;
1681
1682 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1683 if (!resp)
1684 return -ENOMEM;
1e7d6a45
CM
1685
1686 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1687 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
d0882897 1688 msg[2] = enables;
1e7d6a45
CM
1689 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 3);
1690
1691 rv = wait_for_msg_done(smi_info);
1692 if (rv) {
910840f2 1693 dev_warn(smi_info->io.dev,
d0882897
CM
1694 "Error getting response from set global enables command: %d\n",
1695 rv);
1e7d6a45
CM
1696 goto out;
1697 }
1698
1699 resp_len = smi_info->handlers->get_result(smi_info->si_sm,
1700 resp, IPMI_MAX_MSG_LENGTH);
1701
1702 if (resp_len < 3 ||
1703 resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
1704 resp[1] != IPMI_SET_BMC_GLOBAL_ENABLES_CMD) {
910840f2 1705 dev_warn(smi_info->io.dev,
d0882897
CM
1706 "Invalid return from set global enables command: %ld %x %x\n",
1707 resp_len, resp[0], resp[1]);
1e7d6a45
CM
1708 rv = -EINVAL;
1709 goto out;
1710 }
1711
d0882897
CM
1712 if (resp[2] != 0)
1713 rv = 1;
1714
1715out:
1716 kfree(resp);
1717 return rv;
1718}
1719
1720/*
1721 * Some BMCs do not support clearing the receive irq bit in the global
1722 * enables (even if they don't support interrupts on the BMC). Check
1723 * for this and handle it properly.
1724 */
1725static void check_clr_rcv_irq(struct smi_info *smi_info)
1726{
1727 u8 enables = 0;
1728 int rv;
1729
1730 rv = get_global_enables(smi_info, &enables);
1731 if (!rv) {
1732 if ((enables & IPMI_BMC_RCV_MSG_INTR) == 0)
1733 /* Already clear, should work ok. */
1734 return;
1735
1736 enables &= ~IPMI_BMC_RCV_MSG_INTR;
1737 rv = set_global_enables(smi_info, enables);
1738 }
1739
1740 if (rv < 0) {
910840f2 1741 dev_err(smi_info->io.dev,
d0882897
CM
1742 "Cannot check clearing the rcv irq: %d\n", rv);
1743 return;
1744 }
1745
1746 if (rv) {
1e7d6a45
CM
1747 /*
1748 * An error when setting the event buffer bit means
1749 * clearing the bit is not supported.
1750 */
910840f2 1751 dev_warn(smi_info->io.dev,
d0882897
CM
1752 "The BMC does not support clearing the recv irq bit, compensating, but the BMC needs to be fixed.\n");
1753 smi_info->cannot_disable_irq = true;
1754 }
1755}
1756
1757/*
1758 * Some BMCs do not support setting the interrupt bits in the global
1759 * enables even if they support interrupts. Clearly bad, but we can
1760 * compensate.
1761 */
1762static void check_set_rcv_irq(struct smi_info *smi_info)
1763{
1764 u8 enables = 0;
1765 int rv;
1766
910840f2 1767 if (!smi_info->io.irq)
d0882897
CM
1768 return;
1769
1770 rv = get_global_enables(smi_info, &enables);
1771 if (!rv) {
1772 enables |= IPMI_BMC_RCV_MSG_INTR;
1773 rv = set_global_enables(smi_info, enables);
1774 }
1775
1776 if (rv < 0) {
910840f2 1777 dev_err(smi_info->io.dev,
d0882897
CM
1778 "Cannot check setting the rcv irq: %d\n", rv);
1779 return;
1780 }
1781
1782 if (rv) {
1783 /*
1784 * An error when setting the event buffer bit means
1785 * setting the bit is not supported.
1786 */
910840f2 1787 dev_warn(smi_info->io.dev,
d0882897
CM
1788 "The BMC does not support setting the recv irq bit, compensating, but the BMC needs to be fixed.\n");
1789 smi_info->cannot_disable_irq = true;
1790 smi_info->irq_enable_broken = true;
1e7d6a45 1791 }
1e7d6a45
CM
1792}
1793
40112ae7
CM
1794static int try_enable_event_buffer(struct smi_info *smi_info)
1795{
1796 unsigned char msg[3];
1797 unsigned char *resp;
1798 unsigned long resp_len;
1799 int rv = 0;
1800
1801 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1802 if (!resp)
1803 return -ENOMEM;
1804
1805 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1806 msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
1807 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
1808
1809 rv = wait_for_msg_done(smi_info);
1810 if (rv) {
bb2a08c0 1811 pr_warn(PFX "Error getting response from get global enables command, the event buffer is not enabled.\n");
40112ae7
CM
1812 goto out;
1813 }
1814
1815 resp_len = smi_info->handlers->get_result(smi_info->si_sm,
1816 resp, IPMI_MAX_MSG_LENGTH);
1817
1818 if (resp_len < 4 ||
1819 resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
1820 resp[1] != IPMI_GET_BMC_GLOBAL_ENABLES_CMD ||
1821 resp[2] != 0) {
bb2a08c0 1822 pr_warn(PFX "Invalid return from get global enables command, cannot enable the event buffer.\n");
40112ae7
CM
1823 rv = -EINVAL;
1824 goto out;
1825 }
1826
d9b7e4f7 1827 if (resp[3] & IPMI_BMC_EVT_MSG_BUFF) {
40112ae7 1828 /* buffer is already enabled, nothing to do. */
d9b7e4f7 1829 smi_info->supports_event_msg_buff = true;
40112ae7 1830 goto out;
d9b7e4f7 1831 }
40112ae7
CM
1832
1833 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1834 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
1835 msg[2] = resp[3] | IPMI_BMC_EVT_MSG_BUFF;
1836 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 3);
1837
1838 rv = wait_for_msg_done(smi_info);
1839 if (rv) {
bb2a08c0 1840 pr_warn(PFX "Error getting response from set global, enables command, the event buffer is not enabled.\n");
40112ae7
CM
1841 goto out;
1842 }
1843
1844 resp_len = smi_info->handlers->get_result(smi_info->si_sm,
1845 resp, IPMI_MAX_MSG_LENGTH);
1846
1847 if (resp_len < 3 ||
1848 resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
1849 resp[1] != IPMI_SET_BMC_GLOBAL_ENABLES_CMD) {
bb2a08c0 1850 pr_warn(PFX "Invalid return from get global, enables command, not enable the event buffer.\n");
40112ae7
CM
1851 rv = -EINVAL;
1852 goto out;
1853 }
1854
1855 if (resp[2] != 0)
1856 /*
1857 * An error when setting the event buffer bit means
1858 * that the event buffer is not supported.
1859 */
1860 rv = -ENOENT;
d9b7e4f7
CM
1861 else
1862 smi_info->supports_event_msg_buff = true;
1863
76824852 1864out:
40112ae7
CM
1865 kfree(resp);
1866 return rv;
1867}
1868
07412736 1869static int smi_type_proc_show(struct seq_file *m, void *v)
1da177e4 1870{
07412736 1871 struct smi_info *smi = m->private;
1da177e4 1872
910840f2 1873 seq_printf(m, "%s\n", si_to_str[smi->io.si_type]);
d6c5dc18 1874
5e33cd0c 1875 return 0;
1da177e4
LT
1876}
1877
07412736 1878static int smi_type_proc_open(struct inode *inode, struct file *file)
1da177e4 1879{
d9dda78b 1880 return single_open(file, smi_type_proc_show, PDE_DATA(inode));
07412736
AD
1881}
1882
1883static const struct file_operations smi_type_proc_ops = {
1884 .open = smi_type_proc_open,
1885 .read = seq_read,
1886 .llseek = seq_lseek,
1887 .release = single_release,
1888};
1889
1890static int smi_si_stats_proc_show(struct seq_file *m, void *v)
1891{
1892 struct smi_info *smi = m->private;
1da177e4 1893
07412736 1894 seq_printf(m, "interrupts_enabled: %d\n",
910840f2 1895 smi->io.irq && !smi->interrupt_disabled);
07412736 1896 seq_printf(m, "short_timeouts: %u\n",
64959e2d 1897 smi_get_stat(smi, short_timeouts));
07412736 1898 seq_printf(m, "long_timeouts: %u\n",
64959e2d 1899 smi_get_stat(smi, long_timeouts));
07412736 1900 seq_printf(m, "idles: %u\n",
64959e2d 1901 smi_get_stat(smi, idles));
07412736 1902 seq_printf(m, "interrupts: %u\n",
64959e2d 1903 smi_get_stat(smi, interrupts));
07412736 1904 seq_printf(m, "attentions: %u\n",
64959e2d 1905 smi_get_stat(smi, attentions));
07412736 1906 seq_printf(m, "flag_fetches: %u\n",
64959e2d 1907 smi_get_stat(smi, flag_fetches));
07412736 1908 seq_printf(m, "hosed_count: %u\n",
64959e2d 1909 smi_get_stat(smi, hosed_count));
07412736 1910 seq_printf(m, "complete_transactions: %u\n",
64959e2d 1911 smi_get_stat(smi, complete_transactions));
07412736 1912 seq_printf(m, "events: %u\n",
64959e2d 1913 smi_get_stat(smi, events));
07412736 1914 seq_printf(m, "watchdog_pretimeouts: %u\n",
64959e2d 1915 smi_get_stat(smi, watchdog_pretimeouts));
07412736 1916 seq_printf(m, "incoming_messages: %u\n",
64959e2d 1917 smi_get_stat(smi, incoming_messages));
07412736
AD
1918 return 0;
1919}
1da177e4 1920
07412736
AD
1921static int smi_si_stats_proc_open(struct inode *inode, struct file *file)
1922{
d9dda78b 1923 return single_open(file, smi_si_stats_proc_show, PDE_DATA(inode));
b361e27b
CM
1924}
1925
07412736
AD
1926static const struct file_operations smi_si_stats_proc_ops = {
1927 .open = smi_si_stats_proc_open,
1928 .read = seq_read,
1929 .llseek = seq_lseek,
1930 .release = single_release,
1931};
1932
1933static int smi_params_proc_show(struct seq_file *m, void *v)
b361e27b 1934{
07412736 1935 struct smi_info *smi = m->private;
b361e27b 1936
d6c5dc18
JP
1937 seq_printf(m,
1938 "%s,%s,0x%lx,rsp=%d,rsi=%d,rsh=%d,irq=%d,ipmb=%d\n",
910840f2 1939 si_to_str[smi->io.si_type],
d6c5dc18
JP
1940 addr_space_to_str[smi->io.addr_type],
1941 smi->io.addr_data,
1942 smi->io.regspacing,
1943 smi->io.regsize,
1944 smi->io.regshift,
910840f2
CM
1945 smi->io.irq,
1946 smi->io.slave_addr);
d6c5dc18 1947
5e33cd0c 1948 return 0;
1da177e4
LT
1949}
1950
07412736
AD
1951static int smi_params_proc_open(struct inode *inode, struct file *file)
1952{
d9dda78b 1953 return single_open(file, smi_params_proc_show, PDE_DATA(inode));
07412736
AD
1954}
1955
1956static const struct file_operations smi_params_proc_ops = {
1957 .open = smi_params_proc_open,
1958 .read = seq_read,
1959 .llseek = seq_lseek,
1960 .release = single_release,
1961};
1962
3ae0e0f9
CM
1963/*
1964 * oem_data_avail_to_receive_msg_avail
1965 * @info - smi_info structure with msg_flags set
1966 *
1967 * Converts flags from OEM_DATA_AVAIL to RECEIVE_MSG_AVAIL
1968 * Returns 1 indicating need to re-run handle_flags().
1969 */
1970static int oem_data_avail_to_receive_msg_avail(struct smi_info *smi_info)
1971{
e8b33617 1972 smi_info->msg_flags = ((smi_info->msg_flags & ~OEM_DATA_AVAIL) |
c305e3d3 1973 RECEIVE_MSG_AVAIL);
3ae0e0f9
CM
1974 return 1;
1975}
1976
1977/*
1978 * setup_dell_poweredge_oem_data_handler
1979 * @info - smi_info.device_id must be populated
1980 *
1981 * Systems that match, but have firmware version < 1.40 may assert
1982 * OEM0_DATA_AVAIL on their own, without being told via Set Flags that
1983 * it's safe to do so. Such systems will de-assert OEM1_DATA_AVAIL
1984 * upon receipt of IPMI_GET_MSG_CMD, so we should treat these flags
1985 * as RECEIVE_MSG_AVAIL instead.
1986 *
1987 * As Dell has no plans to release IPMI 1.5 firmware that *ever*
1988 * assert the OEM[012] bits, and if it did, the driver would have to
1989 * change to handle that properly, we don't actually check for the
1990 * firmware version.
1991 * Device ID = 0x20 BMC on PowerEdge 8G servers
1992 * Device Revision = 0x80
1993 * Firmware Revision1 = 0x01 BMC version 1.40
1994 * Firmware Revision2 = 0x40 BCD encoded
1995 * IPMI Version = 0x51 IPMI 1.5
1996 * Manufacturer ID = A2 02 00 Dell IANA
1997 *
d5a2b89a
CM
1998 * Additionally, PowerEdge systems with IPMI < 1.5 may also assert
1999 * OEM0_DATA_AVAIL and needs to be treated as RECEIVE_MSG_AVAIL.
2000 *
3ae0e0f9
CM
2001 */
2002#define DELL_POWEREDGE_8G_BMC_DEVICE_ID 0x20
2003#define DELL_POWEREDGE_8G_BMC_DEVICE_REV 0x80
2004#define DELL_POWEREDGE_8G_BMC_IPMI_VERSION 0x51
50c812b2 2005#define DELL_IANA_MFR_ID 0x0002a2
3ae0e0f9
CM
2006static void setup_dell_poweredge_oem_data_handler(struct smi_info *smi_info)
2007{
2008 struct ipmi_device_id *id = &smi_info->device_id;
50c812b2 2009 if (id->manufacturer_id == DELL_IANA_MFR_ID) {
d5a2b89a
CM
2010 if (id->device_id == DELL_POWEREDGE_8G_BMC_DEVICE_ID &&
2011 id->device_revision == DELL_POWEREDGE_8G_BMC_DEVICE_REV &&
50c812b2 2012 id->ipmi_version == DELL_POWEREDGE_8G_BMC_IPMI_VERSION) {
d5a2b89a
CM
2013 smi_info->oem_data_avail_handler =
2014 oem_data_avail_to_receive_msg_avail;
c305e3d3
CM
2015 } else if (ipmi_version_major(id) < 1 ||
2016 (ipmi_version_major(id) == 1 &&
2017 ipmi_version_minor(id) < 5)) {
d5a2b89a
CM
2018 smi_info->oem_data_avail_handler =
2019 oem_data_avail_to_receive_msg_avail;
2020 }
3ae0e0f9
CM
2021 }
2022}
2023
ea94027b
CM
2024#define CANNOT_RETURN_REQUESTED_LENGTH 0xCA
2025static void return_hosed_msg_badsize(struct smi_info *smi_info)
2026{
2027 struct ipmi_smi_msg *msg = smi_info->curr_msg;
2028
25985edc 2029 /* Make it a response */
ea94027b
CM
2030 msg->rsp[0] = msg->data[0] | 4;
2031 msg->rsp[1] = msg->data[1];
2032 msg->rsp[2] = CANNOT_RETURN_REQUESTED_LENGTH;
2033 msg->rsp_size = 3;
2034 smi_info->curr_msg = NULL;
2035 deliver_recv_msg(smi_info, msg);
2036}
2037
2038/*
2039 * dell_poweredge_bt_xaction_handler
2040 * @info - smi_info.device_id must be populated
2041 *
2042 * Dell PowerEdge servers with the BT interface (x6xx and 1750) will
2043 * not respond to a Get SDR command if the length of the data
2044 * requested is exactly 0x3A, which leads to command timeouts and no
2045 * data returned. This intercepts such commands, and causes userspace
2046 * callers to try again with a different-sized buffer, which succeeds.
2047 */
2048
2049#define STORAGE_NETFN 0x0A
2050#define STORAGE_CMD_GET_SDR 0x23
2051static int dell_poweredge_bt_xaction_handler(struct notifier_block *self,
2052 unsigned long unused,
2053 void *in)
2054{
2055 struct smi_info *smi_info = in;
2056 unsigned char *data = smi_info->curr_msg->data;
2057 unsigned int size = smi_info->curr_msg->data_size;
2058 if (size >= 8 &&
2059 (data[0]>>2) == STORAGE_NETFN &&
2060 data[1] == STORAGE_CMD_GET_SDR &&
2061 data[7] == 0x3A) {
2062 return_hosed_msg_badsize(smi_info);
2063 return NOTIFY_STOP;
2064 }
2065 return NOTIFY_DONE;
2066}
2067
2068static struct notifier_block dell_poweredge_bt_xaction_notifier = {
2069 .notifier_call = dell_poweredge_bt_xaction_handler,
2070};
2071
2072/*
2073 * setup_dell_poweredge_bt_xaction_handler
2074 * @info - smi_info.device_id must be filled in already
2075 *
2076 * Fills in smi_info.device_id.start_transaction_pre_hook
2077 * when we know what function to use there.
2078 */
2079static void
2080setup_dell_poweredge_bt_xaction_handler(struct smi_info *smi_info)
2081{
2082 struct ipmi_device_id *id = &smi_info->device_id;
50c812b2 2083 if (id->manufacturer_id == DELL_IANA_MFR_ID &&
910840f2 2084 smi_info->io.si_type == SI_BT)
ea94027b
CM
2085 register_xaction_notifier(&dell_poweredge_bt_xaction_notifier);
2086}
2087
3ae0e0f9
CM
2088/*
2089 * setup_oem_data_handler
2090 * @info - smi_info.device_id must be filled in already
2091 *
2092 * Fills in smi_info.device_id.oem_data_available_handler
2093 * when we know what function to use there.
2094 */
2095
2096static void setup_oem_data_handler(struct smi_info *smi_info)
2097{
2098 setup_dell_poweredge_oem_data_handler(smi_info);
2099}
2100
ea94027b
CM
2101static void setup_xaction_handlers(struct smi_info *smi_info)
2102{
2103 setup_dell_poweredge_bt_xaction_handler(smi_info);
2104}
2105
d0882897
CM
2106static void check_for_broken_irqs(struct smi_info *smi_info)
2107{
2108 check_clr_rcv_irq(smi_info);
2109 check_set_rcv_irq(smi_info);
2110}
2111
a9a2c44f
CM
2112static inline void wait_for_timer_and_thread(struct smi_info *smi_info)
2113{
b874b985
CM
2114 if (smi_info->thread != NULL)
2115 kthread_stop(smi_info->thread);
2116 if (smi_info->timer_running)
453823ba 2117 del_timer_sync(&smi_info->si_timer);
a9a2c44f
CM
2118}
2119
7e030d6d 2120static struct smi_info *find_dup_si(struct smi_info *info)
1da177e4 2121{
b0defcdb 2122 struct smi_info *e;
1da177e4 2123
b0defcdb
CM
2124 list_for_each_entry(e, &smi_infos, link) {
2125 if (e->io.addr_type != info->io.addr_type)
2126 continue;
94671710
CM
2127 if (e->io.addr_data == info->io.addr_data) {
2128 /*
2129 * This is a cheap hack, ACPI doesn't have a defined
2130 * slave address but SMBIOS does. Pick it up from
2131 * any source that has it available.
2132 */
910840f2
CM
2133 if (info->io.slave_addr && !e->io.slave_addr)
2134 e->io.slave_addr = info->io.slave_addr;
7e030d6d 2135 return e;
94671710 2136 }
b0defcdb 2137 }
1da177e4 2138
7e030d6d 2139 return NULL;
b0defcdb 2140}
1da177e4 2141
bb398a4c 2142int ipmi_si_add_smi(struct si_sm_io *io)
b0defcdb 2143{
2407d77a 2144 int rv = 0;
bb398a4c 2145 struct smi_info *new_smi, *dup;
b0defcdb 2146
bb398a4c
CM
2147 if (!io->io_setup) {
2148 if (io->addr_type == IPMI_IO_ADDR_SPACE) {
2149 io->io_setup = port_setup;
2150 } else if (io->addr_type == IPMI_MEM_ADDR_SPACE) {
2151 io->io_setup = mem_setup;
e1eeb7f8
CM
2152 } else {
2153 return -EINVAL;
2154 }
2155 }
2156
bb398a4c
CM
2157 new_smi = smi_info_alloc();
2158 if (!new_smi)
2159 return -ENOMEM;
2160
2161 new_smi->io = *io;
2162
d6dfd131 2163 mutex_lock(&smi_infos_lock);
7e030d6d
CM
2164 dup = find_dup_si(new_smi);
2165 if (dup) {
910840f2
CM
2166 if (new_smi->io.addr_source == SI_ACPI &&
2167 dup->io.addr_source == SI_SMBIOS) {
7e030d6d 2168 /* We prefer ACPI over SMBIOS. */
910840f2 2169 dev_info(dup->io.dev,
7e030d6d 2170 "Removing SMBIOS-specified %s state machine in favor of ACPI\n",
910840f2 2171 si_to_str[new_smi->io.si_type]);
7e030d6d
CM
2172 cleanup_one_si(dup);
2173 } else {
910840f2 2174 dev_info(new_smi->io.dev,
7e030d6d 2175 "%s-specified %s state machine: duplicate\n",
910840f2
CM
2176 ipmi_addr_src_to_str(new_smi->io.addr_source),
2177 si_to_str[new_smi->io.si_type]);
7e030d6d
CM
2178 rv = -EBUSY;
2179 goto out_err;
2180 }
b0defcdb 2181 }
1da177e4 2182
bb2a08c0 2183 pr_info(PFX "Adding %s-specified %s state machine\n",
910840f2
CM
2184 ipmi_addr_src_to_str(new_smi->io.addr_source),
2185 si_to_str[new_smi->io.si_type]);
2407d77a 2186
1da177e4
LT
2187 /* So we know not to free it unless we have allocated one. */
2188 new_smi->intf = NULL;
2189 new_smi->si_sm = NULL;
2190 new_smi->handlers = NULL;
2191
2407d77a
MG
2192 list_add_tail(&new_smi->link, &smi_infos);
2193
bb398a4c
CM
2194 if (initialized) {
2195 rv = try_smi_init(new_smi);
2196 if (rv) {
2197 mutex_unlock(&smi_infos_lock);
2198 cleanup_one_si(new_smi);
2199 return rv;
2200 }
2201 }
2407d77a
MG
2202out_err:
2203 mutex_unlock(&smi_infos_lock);
2204 return rv;
2205}
2206
3f724c40
TC
2207/*
2208 * Try to start up an interface. Must be called with smi_infos_lock
2209 * held, primarily to keep smi_num consistent, we only one to do these
2210 * one at a time.
2211 */
2407d77a
MG
2212static int try_smi_init(struct smi_info *new_smi)
2213{
2214 int rv = 0;
2215 int i;
1abf71ee 2216 char *init_name = NULL;
2407d77a 2217
bb2a08c0 2218 pr_info(PFX "Trying %s-specified %s state machine at %s address 0x%lx, slave address 0x%x, irq %d\n",
910840f2
CM
2219 ipmi_addr_src_to_str(new_smi->io.addr_source),
2220 si_to_str[new_smi->io.si_type],
bb2a08c0
CM
2221 addr_space_to_str[new_smi->io.addr_type],
2222 new_smi->io.addr_data,
910840f2 2223 new_smi->io.slave_addr, new_smi->io.irq);
2407d77a 2224
910840f2 2225 switch (new_smi->io.si_type) {
b0defcdb 2226 case SI_KCS:
1da177e4 2227 new_smi->handlers = &kcs_smi_handlers;
b0defcdb
CM
2228 break;
2229
2230 case SI_SMIC:
1da177e4 2231 new_smi->handlers = &smic_smi_handlers;
b0defcdb
CM
2232 break;
2233
2234 case SI_BT:
1da177e4 2235 new_smi->handlers = &bt_smi_handlers;
b0defcdb
CM
2236 break;
2237
2238 default:
1da177e4
LT
2239 /* No support for anything else yet. */
2240 rv = -EIO;
2241 goto out_err;
2242 }
2243
3f724c40
TC
2244 new_smi->intf_num = smi_num;
2245
1abf71ee 2246 /* Do this early so it's available for logs. */
910840f2 2247 if (!new_smi->io.dev) {
3f724c40
TC
2248 init_name = kasprintf(GFP_KERNEL, "ipmi_si.%d",
2249 new_smi->intf_num);
1abf71ee
CM
2250
2251 /*
2252 * If we don't already have a device from something
2253 * else (like PCI), then register a new one.
2254 */
2255 new_smi->pdev = platform_device_alloc("ipmi_si",
2256 new_smi->intf_num);
2257 if (!new_smi->pdev) {
2258 pr_err(PFX "Unable to allocate platform device\n");
2259 goto out_err;
2260 }
910840f2 2261 new_smi->io.dev = &new_smi->pdev->dev;
9d70029e 2262 new_smi->io.dev->driver = &ipmi_platform_driver.driver;
1abf71ee 2263 /* Nulled by device_add() */
910840f2 2264 new_smi->io.dev->init_name = init_name;
1abf71ee
CM
2265 }
2266
1da177e4
LT
2267 /* Allocate the state machine's data and initialize it. */
2268 new_smi->si_sm = kmalloc(new_smi->handlers->size(), GFP_KERNEL);
b0defcdb 2269 if (!new_smi->si_sm) {
bb2a08c0 2270 pr_err(PFX "Could not allocate state machine memory\n");
1da177e4
LT
2271 rv = -ENOMEM;
2272 goto out_err;
2273 }
e1eeb7f8
CM
2274 new_smi->io.io_size = new_smi->handlers->init_data(new_smi->si_sm,
2275 &new_smi->io);
1da177e4
LT
2276
2277 /* Now that we know the I/O size, we can set up the I/O. */
e1eeb7f8 2278 rv = new_smi->io.io_setup(&new_smi->io);
1da177e4 2279 if (rv) {
910840f2 2280 dev_err(new_smi->io.dev, "Could not set up I/O space\n");
1da177e4
LT
2281 goto out_err;
2282 }
2283
1da177e4
LT
2284 /* Do low-level detection first. */
2285 if (new_smi->handlers->detect(new_smi->si_sm)) {
910840f2
CM
2286 if (new_smi->io.addr_source)
2287 dev_err(new_smi->io.dev,
2288 "Interface detection failed\n");
1da177e4
LT
2289 rv = -ENODEV;
2290 goto out_err;
2291 }
2292
c305e3d3
CM
2293 /*
2294 * Attempt a get device id command. If it fails, we probably
2295 * don't have a BMC here.
2296 */
1da177e4 2297 rv = try_get_dev_id(new_smi);
b0defcdb 2298 if (rv) {
910840f2
CM
2299 if (new_smi->io.addr_source)
2300 dev_err(new_smi->io.dev,
2301 "There appears to be no BMC at this location\n");
1da177e4 2302 goto out_err;
b0defcdb 2303 }
1da177e4 2304
3ae0e0f9 2305 setup_oem_data_handler(new_smi);
ea94027b 2306 setup_xaction_handlers(new_smi);
d0882897 2307 check_for_broken_irqs(new_smi);
3ae0e0f9 2308
b874b985 2309 new_smi->waiting_msg = NULL;
1da177e4
LT
2310 new_smi->curr_msg = NULL;
2311 atomic_set(&new_smi->req_events, 0);
7aefac26 2312 new_smi->run_to_completion = false;
64959e2d
CM
2313 for (i = 0; i < SI_NUM_STATS; i++)
2314 atomic_set(&new_smi->stats[i], 0);
1da177e4 2315
7aefac26 2316 new_smi->interrupt_disabled = true;
89986496 2317 atomic_set(&new_smi->need_watch, 0);
1da177e4 2318
40112ae7
CM
2319 rv = try_enable_event_buffer(new_smi);
2320 if (rv == 0)
7aefac26 2321 new_smi->has_event_buffer = true;
40112ae7 2322
c305e3d3
CM
2323 /*
2324 * Start clearing the flags before we enable interrupts or the
2325 * timer to avoid racing with the timer.
2326 */
0cfec916 2327 start_clear_flags(new_smi, false);
d9b7e4f7
CM
2328
2329 /*
2330 * IRQ is defined to be set when non-zero. req_events will
2331 * cause a global flags check that will enable interrupts.
2332 */
910840f2 2333 if (new_smi->io.irq) {
d9b7e4f7
CM
2334 new_smi->interrupt_disabled = false;
2335 atomic_set(&new_smi->req_events, 1);
2336 }
1da177e4 2337
1abf71ee 2338 if (new_smi->pdev) {
b48f5457 2339 rv = platform_device_add(new_smi->pdev);
50c812b2 2340 if (rv) {
910840f2 2341 dev_err(new_smi->io.dev,
bb2a08c0
CM
2342 "Unable to register system interface device: %d\n",
2343 rv);
453823ba 2344 goto out_err;
50c812b2 2345 }
50c812b2
CM
2346 }
2347
1da177e4
LT
2348 rv = ipmi_register_smi(&handlers,
2349 new_smi,
910840f2
CM
2350 new_smi->io.dev,
2351 new_smi->io.slave_addr);
1da177e4 2352 if (rv) {
910840f2
CM
2353 dev_err(new_smi->io.dev,
2354 "Unable to register device: error %d\n",
279fbd0c 2355 rv);
1da177e4
LT
2356 goto out_err_stop_timer;
2357 }
2358
2359 rv = ipmi_smi_add_proc_entry(new_smi->intf, "type",
07412736 2360 &smi_type_proc_ops,
99b76233 2361 new_smi);
1da177e4 2362 if (rv) {
910840f2
CM
2363 dev_err(new_smi->io.dev,
2364 "Unable to create proc entry: %d\n", rv);
1da177e4
LT
2365 goto out_err_stop_timer;
2366 }
2367
2368 rv = ipmi_smi_add_proc_entry(new_smi->intf, "si_stats",
07412736 2369 &smi_si_stats_proc_ops,
99b76233 2370 new_smi);
1da177e4 2371 if (rv) {
910840f2
CM
2372 dev_err(new_smi->io.dev,
2373 "Unable to create proc entry: %d\n", rv);
1da177e4
LT
2374 goto out_err_stop_timer;
2375 }
2376
b361e27b 2377 rv = ipmi_smi_add_proc_entry(new_smi->intf, "params",
07412736 2378 &smi_params_proc_ops,
99b76233 2379 new_smi);
b361e27b 2380 if (rv) {
910840f2
CM
2381 dev_err(new_smi->io.dev,
2382 "Unable to create proc entry: %d\n", rv);
b361e27b
CM
2383 goto out_err_stop_timer;
2384 }
2385
3f724c40
TC
2386 /* Don't increment till we know we have succeeded. */
2387 smi_num++;
2388
910840f2
CM
2389 dev_info(new_smi->io.dev, "IPMI %s interface initialized\n",
2390 si_to_str[new_smi->io.si_type]);
1da177e4 2391
910840f2 2392 WARN_ON(new_smi->io.dev->init_name != NULL);
1abf71ee
CM
2393 kfree(init_name);
2394
1da177e4
LT
2395 return 0;
2396
76824852 2397out_err_stop_timer:
a9a2c44f 2398 wait_for_timer_and_thread(new_smi);
1da177e4 2399
76824852 2400out_err:
7aefac26 2401 new_smi->interrupt_disabled = true;
2407d77a
MG
2402
2403 if (new_smi->intf) {
b874b985 2404 ipmi_smi_t intf = new_smi->intf;
2407d77a 2405 new_smi->intf = NULL;
b874b985 2406 ipmi_unregister_smi(intf);
2407d77a 2407 }
1da177e4 2408
4f3e8199
CM
2409 if (new_smi->io.irq_cleanup) {
2410 new_smi->io.irq_cleanup(&new_smi->io);
2411 new_smi->io.irq_cleanup = NULL;
2407d77a 2412 }
1da177e4 2413
c305e3d3
CM
2414 /*
2415 * Wait until we know that we are out of any interrupt
2416 * handlers might have been running before we freed the
2417 * interrupt.
2418 */
fbd568a3 2419 synchronize_sched();
1da177e4
LT
2420
2421 if (new_smi->si_sm) {
2422 if (new_smi->handlers)
2423 new_smi->handlers->cleanup(new_smi->si_sm);
2424 kfree(new_smi->si_sm);
2407d77a 2425 new_smi->si_sm = NULL;
1da177e4 2426 }
910840f2
CM
2427 if (new_smi->io.addr_source_cleanup) {
2428 new_smi->io.addr_source_cleanup(&new_smi->io);
2429 new_smi->io.addr_source_cleanup = NULL;
2407d77a 2430 }
e1eeb7f8
CM
2431 if (new_smi->io.io_cleanup) {
2432 new_smi->io.io_cleanup(&new_smi->io);
2433 new_smi->io.io_cleanup = NULL;
2407d77a 2434 }
1da177e4 2435
910840f2 2436 if (new_smi->pdev) {
50c812b2 2437 platform_device_unregister(new_smi->pdev);
1abf71ee
CM
2438 new_smi->pdev = NULL;
2439 } else if (new_smi->pdev) {
2440 platform_device_put(new_smi->pdev);
2407d77a 2441 }
b0defcdb 2442
1abf71ee
CM
2443 kfree(init_name);
2444
1da177e4
LT
2445 return rv;
2446}
2447
2223cbec 2448static int init_ipmi_si(void)
1da177e4 2449{
2407d77a 2450 struct smi_info *e;
06ee4594 2451 enum ipmi_addr_src type = SI_INVALID;
1da177e4
LT
2452
2453 if (initialized)
2454 return 0;
1da177e4 2455
bb2a08c0 2456 pr_info("IPMI System Interface driver.\n");
1da177e4 2457
d8cc5267 2458 /* If the user gave us a device, they presumably want us to use it */
7a453308
CM
2459 if (!ipmi_si_hardcode_find_bmc())
2460 goto do_scan;
d8cc5267 2461
9d70029e
CM
2462 ipmi_si_platform_init();
2463
13d0b35c 2464 ipmi_si_pci_init();
b0defcdb 2465
c6f85a75 2466 ipmi_si_parisc_init();
fdbeb7de 2467
06ee4594
MG
2468 /* We prefer devices with interrupts, but in the case of a machine
2469 with multiple BMCs we assume that there will be several instances
2470 of a given type so if we succeed in registering a type then also
2471 try to register everything else of the same type */
7a453308 2472do_scan:
2407d77a
MG
2473 mutex_lock(&smi_infos_lock);
2474 list_for_each_entry(e, &smi_infos, link) {
06ee4594
MG
2475 /* Try to register a device if it has an IRQ and we either
2476 haven't successfully registered a device yet or this
2477 device has the same type as one we successfully registered */
910840f2 2478 if (e->io.irq && (!type || e->io.addr_source == type)) {
d8cc5267 2479 if (!try_smi_init(e)) {
910840f2 2480 type = e->io.addr_source;
d8cc5267
MG
2481 }
2482 }
2483 }
2484
06ee4594 2485 /* type will only have been set if we successfully registered an si */
bb398a4c
CM
2486 if (type)
2487 goto skip_fallback_noirq;
06ee4594 2488
d8cc5267
MG
2489 /* Fall back to the preferred device */
2490
2491 list_for_each_entry(e, &smi_infos, link) {
910840f2 2492 if (!e->io.irq && (!type || e->io.addr_source == type)) {
d8cc5267 2493 if (!try_smi_init(e)) {
910840f2 2494 type = e->io.addr_source;
d8cc5267
MG
2495 }
2496 }
2407d77a 2497 }
bb398a4c
CM
2498
2499skip_fallback_noirq:
2500 initialized = 1;
2407d77a
MG
2501 mutex_unlock(&smi_infos_lock);
2502
06ee4594
MG
2503 if (type)
2504 return 0;
2505
d6dfd131 2506 mutex_lock(&smi_infos_lock);
b361e27b 2507 if (unload_when_empty && list_empty(&smi_infos)) {
d6dfd131 2508 mutex_unlock(&smi_infos_lock);
d2478521 2509 cleanup_ipmi_si();
bb2a08c0 2510 pr_warn(PFX "Unable to find any System Interface(s)\n");
1da177e4 2511 return -ENODEV;
b0defcdb 2512 } else {
d6dfd131 2513 mutex_unlock(&smi_infos_lock);
b0defcdb 2514 return 0;
1da177e4 2515 }
1da177e4
LT
2516}
2517module_init(init_ipmi_si);
2518
b361e27b 2519static void cleanup_one_si(struct smi_info *to_clean)
1da177e4 2520{
2407d77a 2521 int rv = 0;
1da177e4 2522
b0defcdb 2523 if (!to_clean)
1da177e4
LT
2524 return;
2525
b874b985
CM
2526 if (to_clean->intf) {
2527 ipmi_smi_t intf = to_clean->intf;
2528
2529 to_clean->intf = NULL;
2530 rv = ipmi_unregister_smi(intf);
2531 if (rv) {
2532 pr_err(PFX "Unable to unregister device: errno=%d\n",
2533 rv);
2534 }
2535 }
2536
b0defcdb
CM
2537 list_del(&to_clean->link);
2538
c305e3d3 2539 /*
b874b985
CM
2540 * Make sure that interrupts, the timer and the thread are
2541 * stopped and will not run again.
c305e3d3 2542 */
4f3e8199
CM
2543 if (to_clean->io.irq_cleanup)
2544 to_clean->io.irq_cleanup(&to_clean->io);
a9a2c44f 2545 wait_for_timer_and_thread(to_clean);
1da177e4 2546
c305e3d3
CM
2547 /*
2548 * Timeouts are stopped, now make sure the interrupts are off
b874b985
CM
2549 * in the BMC. Note that timers and CPU interrupts are off,
2550 * so no need for locks.
c305e3d3 2551 */
ee6cd5f8 2552 while (to_clean->curr_msg || (to_clean->si_state != SI_NORMAL)) {
ee6cd5f8
CM
2553 poll(to_clean);
2554 schedule_timeout_uninterruptible(1);
ee6cd5f8 2555 }
7e030d6d
CM
2556 if (to_clean->handlers)
2557 disable_si_irq(to_clean, false);
e8b33617 2558 while (to_clean->curr_msg || (to_clean->si_state != SI_NORMAL)) {
1da177e4 2559 poll(to_clean);
da4cd8df 2560 schedule_timeout_uninterruptible(1);
1da177e4
LT
2561 }
2562
2407d77a
MG
2563 if (to_clean->handlers)
2564 to_clean->handlers->cleanup(to_clean->si_sm);
1da177e4
LT
2565
2566 kfree(to_clean->si_sm);
2567
910840f2
CM
2568 if (to_clean->io.addr_source_cleanup)
2569 to_clean->io.addr_source_cleanup(&to_clean->io);
e1eeb7f8
CM
2570 if (to_clean->io.io_cleanup)
2571 to_clean->io.io_cleanup(&to_clean->io);
50c812b2 2572
910840f2 2573 if (to_clean->pdev)
50c812b2
CM
2574 platform_device_unregister(to_clean->pdev);
2575
2576 kfree(to_clean);
1da177e4
LT
2577}
2578
bb398a4c
CM
2579int ipmi_si_remove_by_dev(struct device *dev)
2580{
2581 struct smi_info *e;
2582 int rv = -ENOENT;
2583
2584 mutex_lock(&smi_infos_lock);
2585 list_for_each_entry(e, &smi_infos, link) {
2586 if (e->io.dev == dev) {
2587 cleanup_one_si(e);
2588 rv = 0;
2589 break;
2590 }
2591 }
2592 mutex_unlock(&smi_infos_lock);
2593
2594 return rv;
2595}
2596
44814ec9
CM
2597void ipmi_si_remove_by_data(int addr_space, enum si_type si_type,
2598 unsigned long addr)
2599{
2600 /* remove */
2601 struct smi_info *e, *tmp_e;
2602
2603 mutex_lock(&smi_infos_lock);
2604 list_for_each_entry_safe(e, tmp_e, &smi_infos, link) {
2605 if (e->io.addr_type != addr_space)
2606 continue;
2607 if (e->io.si_type != si_type)
2608 continue;
2609 if (e->io.addr_data == addr)
2610 cleanup_one_si(e);
2611 }
2612 mutex_unlock(&smi_infos_lock);
2613}
2614
0dcf334c 2615static void cleanup_ipmi_si(void)
1da177e4 2616{
b0defcdb 2617 struct smi_info *e, *tmp_e;
1da177e4 2618
b0defcdb 2619 if (!initialized)
1da177e4
LT
2620 return;
2621
13d0b35c 2622 ipmi_si_pci_shutdown();
c6f85a75
CM
2623
2624 ipmi_si_parisc_shutdown();
b0defcdb 2625
9d70029e 2626 ipmi_si_platform_shutdown();
dba9b4f6 2627
d6dfd131 2628 mutex_lock(&smi_infos_lock);
b0defcdb
CM
2629 list_for_each_entry_safe(e, tmp_e, &smi_infos, link)
2630 cleanup_one_si(e);
d6dfd131 2631 mutex_unlock(&smi_infos_lock);
1da177e4
LT
2632}
2633module_exit(cleanup_ipmi_si);
2634
0944d889 2635MODULE_ALIAS("platform:dmi-ipmi-si");
1da177e4 2636MODULE_LICENSE("GPL");
1fdd75bd 2637MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
c305e3d3
CM
2638MODULE_DESCRIPTION("Interface to the IPMI driver for the KCS, SMIC, and BT"
2639 " system interfaces.");