char: constify of_device_id array
[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>
52#include <linux/pci.h>
53#include <linux/ioport.h>
ea94027b 54#include <linux/notifier.h>
b0defcdb 55#include <linux/mutex.h>
e9a705a0 56#include <linux/kthread.h>
1da177e4 57#include <asm/irq.h>
1da177e4
LT
58#include <linux/interrupt.h>
59#include <linux/rcupdate.h>
16f4232c 60#include <linux/ipmi.h>
1da177e4
LT
61#include <linux/ipmi_smi.h>
62#include <asm/io.h>
63#include "ipmi_si_sm.h"
b224cd3a 64#include <linux/dmi.h>
b361e27b
CM
65#include <linux/string.h>
66#include <linux/ctype.h>
9e368fa0 67#include <linux/pnp.h>
11c675ce
SR
68#include <linux/of_device.h>
69#include <linux/of_platform.h>
672d8eaf
RH
70#include <linux/of_address.h>
71#include <linux/of_irq.h>
dba9b4f6 72
fdbeb7de
TB
73#ifdef CONFIG_PARISC
74#include <asm/hardware.h> /* for register_parisc_driver() stuff */
75#include <asm/parisc-device.h>
76#endif
77
b361e27b 78#define PFX "ipmi_si: "
1da177e4
LT
79
80/* Measure times between events in the driver. */
81#undef DEBUG_TIMING
82
83/* Call every 10 ms. */
84#define SI_TIMEOUT_TIME_USEC 10000
85#define SI_USEC_PER_JIFFY (1000000/HZ)
86#define SI_TIMEOUT_JIFFIES (SI_TIMEOUT_TIME_USEC/SI_USEC_PER_JIFFY)
87#define SI_SHORT_TIMEOUT_USEC 250 /* .25ms when the SM request a
c305e3d3 88 short timeout */
1da177e4
LT
89
90enum si_intf_state {
91 SI_NORMAL,
92 SI_GETTING_FLAGS,
93 SI_GETTING_EVENTS,
94 SI_CLEARING_FLAGS,
1da177e4 95 SI_GETTING_MESSAGES,
d9b7e4f7
CM
96 SI_CHECKING_ENABLES,
97 SI_SETTING_ENABLES
1da177e4
LT
98 /* FIXME - add watchdog stuff. */
99};
100
9dbf68f9
CM
101/* Some BT-specific defines we need here. */
102#define IPMI_BT_INTMASK_REG 2
103#define IPMI_BT_INTMASK_CLEAR_IRQ_BIT 2
104#define IPMI_BT_INTMASK_ENABLE_IRQ_BIT 1
105
1da177e4
LT
106enum si_type {
107 SI_KCS, SI_SMIC, SI_BT
108};
b361e27b 109static char *si_to_str[] = { "kcs", "smic", "bt" };
1da177e4 110
50c812b2
CM
111#define DEVICE_NAME "ipmi_si"
112
a1e9c9dd 113static struct platform_driver ipmi_driver;
64959e2d
CM
114
115/*
116 * Indexes into stats[] in smi_info below.
117 */
ba8ff1c6
CM
118enum si_stat_indexes {
119 /*
120 * Number of times the driver requested a timer while an operation
121 * was in progress.
122 */
123 SI_STAT_short_timeouts = 0,
124
125 /*
126 * Number of times the driver requested a timer while nothing was in
127 * progress.
128 */
129 SI_STAT_long_timeouts,
130
131 /* Number of times the interface was idle while being polled. */
132 SI_STAT_idles,
133
134 /* Number of interrupts the driver handled. */
135 SI_STAT_interrupts,
136
137 /* Number of time the driver got an ATTN from the hardware. */
138 SI_STAT_attentions,
64959e2d 139
ba8ff1c6
CM
140 /* Number of times the driver requested flags from the hardware. */
141 SI_STAT_flag_fetches,
142
143 /* Number of times the hardware didn't follow the state machine. */
144 SI_STAT_hosed_count,
145
146 /* Number of completed messages. */
147 SI_STAT_complete_transactions,
148
149 /* Number of IPMI events received from the hardware. */
150 SI_STAT_events,
151
152 /* Number of watchdog pretimeouts. */
153 SI_STAT_watchdog_pretimeouts,
154
b3834be5 155 /* Number of asynchronous messages received. */
ba8ff1c6
CM
156 SI_STAT_incoming_messages,
157
158
159 /* This *must* remain last, add new values above this. */
160 SI_NUM_STATS
161};
64959e2d 162
c305e3d3 163struct smi_info {
a9a2c44f 164 int intf_num;
1da177e4
LT
165 ipmi_smi_t intf;
166 struct si_sm_data *si_sm;
167 struct si_sm_handlers *handlers;
168 enum si_type si_type;
169 spinlock_t si_lock;
b874b985 170 struct ipmi_smi_msg *waiting_msg;
1da177e4
LT
171 struct ipmi_smi_msg *curr_msg;
172 enum si_intf_state si_state;
173
c305e3d3
CM
174 /*
175 * Used to handle the various types of I/O that can occur with
176 * IPMI
177 */
1da177e4
LT
178 struct si_sm_io io;
179 int (*io_setup)(struct smi_info *info);
180 void (*io_cleanup)(struct smi_info *info);
181 int (*irq_setup)(struct smi_info *info);
182 void (*irq_cleanup)(struct smi_info *info);
183 unsigned int io_size;
5fedc4a2 184 enum ipmi_addr_src addr_source; /* ACPI, PCI, SMBIOS, hardcode, etc. */
b0defcdb
CM
185 void (*addr_source_cleanup)(struct smi_info *info);
186 void *addr_source_data;
1da177e4 187
c305e3d3
CM
188 /*
189 * Per-OEM handler, called from handle_flags(). Returns 1
190 * when handle_flags() needs to be re-run or 0 indicating it
191 * set si_state itself.
192 */
3ae0e0f9
CM
193 int (*oem_data_avail_handler)(struct smi_info *smi_info);
194
c305e3d3
CM
195 /*
196 * Flags from the last GET_MSG_FLAGS command, used when an ATTN
197 * is set to hold the flags until we are done handling everything
198 * from the flags.
199 */
1da177e4
LT
200#define RECEIVE_MSG_AVAIL 0x01
201#define EVENT_MSG_BUFFER_FULL 0x02
202#define WDT_PRE_TIMEOUT_INT 0x08
3ae0e0f9
CM
203#define OEM0_DATA_AVAIL 0x20
204#define OEM1_DATA_AVAIL 0x40
205#define OEM2_DATA_AVAIL 0x80
206#define OEM_DATA_AVAIL (OEM0_DATA_AVAIL | \
c305e3d3
CM
207 OEM1_DATA_AVAIL | \
208 OEM2_DATA_AVAIL)
1da177e4
LT
209 unsigned char msg_flags;
210
40112ae7 211 /* Does the BMC have an event buffer? */
7aefac26 212 bool has_event_buffer;
40112ae7 213
c305e3d3
CM
214 /*
215 * If set to true, this will request events the next time the
216 * state machine is idle.
217 */
1da177e4
LT
218 atomic_t req_events;
219
c305e3d3
CM
220 /*
221 * If true, run the state machine to completion on every send
222 * call. Generally used after a panic to make sure stuff goes
223 * out.
224 */
7aefac26 225 bool run_to_completion;
1da177e4
LT
226
227 /* The I/O port of an SI interface. */
228 int port;
229
c305e3d3
CM
230 /*
231 * The space between start addresses of the two ports. For
232 * instance, if the first port is 0xca2 and the spacing is 4, then
233 * the second port is 0xca6.
234 */
1da177e4
LT
235 unsigned int spacing;
236
237 /* zero if no irq; */
238 int irq;
239
240 /* The timer for this si. */
241 struct timer_list si_timer;
242
48e8ac29
BS
243 /* This flag is set, if the timer is running (timer_pending() isn't enough) */
244 bool timer_running;
245
1da177e4
LT
246 /* The time (in jiffies) the last timeout occurred at. */
247 unsigned long last_timeout_jiffies;
248
89986496
CM
249 /* Are we waiting for the events, pretimeouts, received msgs? */
250 atomic_t need_watch;
251
c305e3d3
CM
252 /*
253 * The driver will disable interrupts when it gets into a
254 * situation where it cannot handle messages due to lack of
255 * memory. Once that situation clears up, it will re-enable
256 * interrupts.
257 */
7aefac26 258 bool interrupt_disabled;
1da177e4 259
d9b7e4f7
CM
260 /*
261 * Does the BMC support events?
262 */
263 bool supports_event_msg_buff;
264
a8df150c
CM
265 /*
266 * Did we get an attention that we did not handle?
267 */
268 bool got_attn;
269
50c812b2 270 /* From the get device id response... */
3ae0e0f9 271 struct ipmi_device_id device_id;
1da177e4 272
50c812b2
CM
273 /* Driver model stuff. */
274 struct device *dev;
275 struct platform_device *pdev;
276
c305e3d3
CM
277 /*
278 * True if we allocated the device, false if it came from
279 * someplace else (like PCI).
280 */
7aefac26 281 bool dev_registered;
50c812b2 282
1da177e4
LT
283 /* Slave address, could be reported from DMI. */
284 unsigned char slave_addr;
285
286 /* Counters and things for the proc filesystem. */
64959e2d 287 atomic_t stats[SI_NUM_STATS];
a9a2c44f 288
c305e3d3 289 struct task_struct *thread;
b0defcdb
CM
290
291 struct list_head link;
16f4232c 292 union ipmi_smi_info_union addr_info;
1da177e4
LT
293};
294
64959e2d
CM
295#define smi_inc_stat(smi, stat) \
296 atomic_inc(&(smi)->stats[SI_STAT_ ## stat])
297#define smi_get_stat(smi, stat) \
298 ((unsigned int) atomic_read(&(smi)->stats[SI_STAT_ ## stat]))
299
a51f4a81
CM
300#define SI_MAX_PARMS 4
301
302static int force_kipmid[SI_MAX_PARMS];
303static int num_force_kipmid;
56480287 304#ifdef CONFIG_PCI
7aefac26 305static bool pci_registered;
56480287 306#endif
561f8182 307#ifdef CONFIG_ACPI
7aefac26 308static bool pnp_registered;
561f8182 309#endif
fdbeb7de 310#ifdef CONFIG_PARISC
7aefac26 311static bool parisc_registered;
fdbeb7de 312#endif
a51f4a81 313
ae74e823
MW
314static unsigned int kipmid_max_busy_us[SI_MAX_PARMS];
315static int num_max_busy_us;
316
7aefac26 317static bool unload_when_empty = true;
b361e27b 318
2407d77a 319static int add_smi(struct smi_info *smi);
b0defcdb 320static int try_smi_init(struct smi_info *smi);
b361e27b 321static void cleanup_one_si(struct smi_info *to_clean);
d2478521 322static void cleanup_ipmi_si(void);
b0defcdb 323
f93aae9f
JS
324#ifdef DEBUG_TIMING
325void debug_timestamp(char *msg)
326{
48862ea2 327 struct timespec64 t;
f93aae9f 328
48862ea2
JS
329 getnstimeofday64(&t);
330 pr_debug("**%s: %lld.%9.9ld\n", msg, (long long) t.tv_sec, t.tv_nsec);
f93aae9f
JS
331}
332#else
333#define debug_timestamp(x)
334#endif
335
e041c683 336static ATOMIC_NOTIFIER_HEAD(xaction_notifier_list);
c305e3d3 337static int register_xaction_notifier(struct notifier_block *nb)
ea94027b 338{
e041c683 339 return atomic_notifier_chain_register(&xaction_notifier_list, nb);
ea94027b
CM
340}
341
1da177e4
LT
342static void deliver_recv_msg(struct smi_info *smi_info,
343 struct ipmi_smi_msg *msg)
344{
7adf579c 345 /* Deliver the message to the upper layer. */
968bf7cc
CM
346 if (smi_info->intf)
347 ipmi_smi_msg_received(smi_info->intf, msg);
348 else
349 ipmi_free_smi_msg(msg);
1da177e4
LT
350}
351
4d7cbac7 352static void return_hosed_msg(struct smi_info *smi_info, int cCode)
1da177e4
LT
353{
354 struct ipmi_smi_msg *msg = smi_info->curr_msg;
355
4d7cbac7
CM
356 if (cCode < 0 || cCode > IPMI_ERR_UNSPECIFIED)
357 cCode = IPMI_ERR_UNSPECIFIED;
358 /* else use it as is */
359
25985edc 360 /* Make it a response */
1da177e4
LT
361 msg->rsp[0] = msg->data[0] | 4;
362 msg->rsp[1] = msg->data[1];
4d7cbac7 363 msg->rsp[2] = cCode;
1da177e4
LT
364 msg->rsp_size = 3;
365
366 smi_info->curr_msg = NULL;
367 deliver_recv_msg(smi_info, msg);
368}
369
370static enum si_sm_result start_next_msg(struct smi_info *smi_info)
371{
372 int rv;
1da177e4 373
b874b985 374 if (!smi_info->waiting_msg) {
1da177e4
LT
375 smi_info->curr_msg = NULL;
376 rv = SI_SM_IDLE;
377 } else {
378 int err;
379
b874b985
CM
380 smi_info->curr_msg = smi_info->waiting_msg;
381 smi_info->waiting_msg = NULL;
f93aae9f 382 debug_timestamp("Start2");
e041c683
AS
383 err = atomic_notifier_call_chain(&xaction_notifier_list,
384 0, smi_info);
ea94027b
CM
385 if (err & NOTIFY_STOP_MASK) {
386 rv = SI_SM_CALL_WITHOUT_DELAY;
387 goto out;
388 }
1da177e4
LT
389 err = smi_info->handlers->start_transaction(
390 smi_info->si_sm,
391 smi_info->curr_msg->data,
392 smi_info->curr_msg->data_size);
c305e3d3 393 if (err)
4d7cbac7 394 return_hosed_msg(smi_info, err);
1da177e4
LT
395
396 rv = SI_SM_CALL_WITHOUT_DELAY;
397 }
c305e3d3 398 out:
1da177e4
LT
399 return rv;
400}
401
d9b7e4f7 402static void start_check_enables(struct smi_info *smi_info)
ee6cd5f8
CM
403{
404 unsigned char msg[2];
405
406 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
407 msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
408
409 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
d9b7e4f7 410 smi_info->si_state = SI_CHECKING_ENABLES;
ee6cd5f8
CM
411}
412
1da177e4
LT
413static void start_clear_flags(struct smi_info *smi_info)
414{
415 unsigned char msg[3];
416
417 /* Make sure the watchdog pre-timeout flag is not set at startup. */
418 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
419 msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
420 msg[2] = WDT_PRE_TIMEOUT_INT;
421
422 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 3);
423 smi_info->si_state = SI_CLEARING_FLAGS;
424}
425
968bf7cc
CM
426static void start_getting_msg_queue(struct smi_info *smi_info)
427{
428 smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
429 smi_info->curr_msg->data[1] = IPMI_GET_MSG_CMD;
430 smi_info->curr_msg->data_size = 2;
431
432 smi_info->handlers->start_transaction(
433 smi_info->si_sm,
434 smi_info->curr_msg->data,
435 smi_info->curr_msg->data_size);
436 smi_info->si_state = SI_GETTING_MESSAGES;
437}
438
439static void start_getting_events(struct smi_info *smi_info)
440{
441 smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
442 smi_info->curr_msg->data[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD;
443 smi_info->curr_msg->data_size = 2;
444
445 smi_info->handlers->start_transaction(
446 smi_info->si_sm,
447 smi_info->curr_msg->data,
448 smi_info->curr_msg->data_size);
449 smi_info->si_state = SI_GETTING_EVENTS;
450}
451
48e8ac29
BS
452static void smi_mod_timer(struct smi_info *smi_info, unsigned long new_val)
453{
454 smi_info->last_timeout_jiffies = jiffies;
455 mod_timer(&smi_info->si_timer, new_val);
456 smi_info->timer_running = true;
457}
458
c305e3d3
CM
459/*
460 * When we have a situtaion where we run out of memory and cannot
461 * allocate messages, we just leave them in the BMC and run the system
462 * polled until we can allocate some memory. Once we have some
463 * memory, we will re-enable the interrupt.
464 */
968bf7cc 465static inline bool disable_si_irq(struct smi_info *smi_info)
1da177e4 466{
b0defcdb 467 if ((smi_info->irq) && (!smi_info->interrupt_disabled)) {
7aefac26 468 smi_info->interrupt_disabled = true;
d9b7e4f7 469 start_check_enables(smi_info);
968bf7cc 470 return true;
1da177e4 471 }
968bf7cc 472 return false;
1da177e4
LT
473}
474
968bf7cc 475static inline bool enable_si_irq(struct smi_info *smi_info)
1da177e4
LT
476{
477 if ((smi_info->irq) && (smi_info->interrupt_disabled)) {
7aefac26 478 smi_info->interrupt_disabled = false;
d9b7e4f7 479 start_check_enables(smi_info);
968bf7cc
CM
480 return true;
481 }
482 return false;
483}
484
485/*
486 * Allocate a message. If unable to allocate, start the interrupt
487 * disable process and return NULL. If able to allocate but
488 * interrupts are disabled, free the message and return NULL after
489 * starting the interrupt enable process.
490 */
491static struct ipmi_smi_msg *alloc_msg_handle_irq(struct smi_info *smi_info)
492{
493 struct ipmi_smi_msg *msg;
494
495 msg = ipmi_alloc_smi_msg();
496 if (!msg) {
497 if (!disable_si_irq(smi_info))
498 smi_info->si_state = SI_NORMAL;
499 } else if (enable_si_irq(smi_info)) {
500 ipmi_free_smi_msg(msg);
501 msg = NULL;
1da177e4 502 }
968bf7cc 503 return msg;
1da177e4
LT
504}
505
506static void handle_flags(struct smi_info *smi_info)
507{
3ae0e0f9 508 retry:
1da177e4
LT
509 if (smi_info->msg_flags & WDT_PRE_TIMEOUT_INT) {
510 /* Watchdog pre-timeout */
64959e2d 511 smi_inc_stat(smi_info, watchdog_pretimeouts);
1da177e4
LT
512
513 start_clear_flags(smi_info);
514 smi_info->msg_flags &= ~WDT_PRE_TIMEOUT_INT;
968bf7cc
CM
515 if (smi_info->intf)
516 ipmi_smi_watchdog_pretimeout(smi_info->intf);
1da177e4
LT
517 } else if (smi_info->msg_flags & RECEIVE_MSG_AVAIL) {
518 /* Messages available. */
968bf7cc
CM
519 smi_info->curr_msg = alloc_msg_handle_irq(smi_info);
520 if (!smi_info->curr_msg)
1da177e4 521 return;
1da177e4 522
968bf7cc 523 start_getting_msg_queue(smi_info);
1da177e4
LT
524 } else if (smi_info->msg_flags & EVENT_MSG_BUFFER_FULL) {
525 /* Events available. */
968bf7cc
CM
526 smi_info->curr_msg = alloc_msg_handle_irq(smi_info);
527 if (!smi_info->curr_msg)
1da177e4 528 return;
1da177e4 529
968bf7cc 530 start_getting_events(smi_info);
4064d5ef 531 } else if (smi_info->msg_flags & OEM_DATA_AVAIL &&
c305e3d3 532 smi_info->oem_data_avail_handler) {
4064d5ef
CM
533 if (smi_info->oem_data_avail_handler(smi_info))
534 goto retry;
c305e3d3 535 } else
1da177e4 536 smi_info->si_state = SI_NORMAL;
1da177e4
LT
537}
538
d9b7e4f7
CM
539/*
540 * Global enables we care about.
541 */
542#define GLOBAL_ENABLES_MASK (IPMI_BMC_EVT_MSG_BUFF | IPMI_BMC_RCV_MSG_INTR | \
543 IPMI_BMC_EVT_MSG_INTR)
544
95c97b59
CM
545static u8 current_global_enables(struct smi_info *smi_info, u8 base,
546 bool *irq_on)
d9b7e4f7
CM
547{
548 u8 enables = 0;
549
550 if (smi_info->supports_event_msg_buff)
551 enables |= IPMI_BMC_EVT_MSG_BUFF;
552 else
553 enables &= ~IPMI_BMC_EVT_MSG_BUFF;
554
555 if (smi_info->irq && !smi_info->interrupt_disabled)
556 enables |= IPMI_BMC_RCV_MSG_INTR;
557 else
558 enables &= ~IPMI_BMC_RCV_MSG_INTR;
559
560 if (smi_info->supports_event_msg_buff &&
561 smi_info->irq && !smi_info->interrupt_disabled)
562
563 enables |= IPMI_BMC_EVT_MSG_INTR;
564 else
565 enables &= ~IPMI_BMC_EVT_MSG_INTR;
566
95c97b59
CM
567 *irq_on = enables & (IPMI_BMC_EVT_MSG_INTR | IPMI_BMC_RCV_MSG_INTR);
568
d9b7e4f7
CM
569 return enables;
570}
571
95c97b59
CM
572static void check_bt_irq(struct smi_info *smi_info, bool irq_on)
573{
574 u8 irqstate = smi_info->io.inputb(&smi_info->io, IPMI_BT_INTMASK_REG);
575
576 irqstate &= IPMI_BT_INTMASK_ENABLE_IRQ_BIT;
577
578 if ((bool)irqstate == irq_on)
579 return;
580
581 if (irq_on)
582 smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG,
583 IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
584 else
585 smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG, 0);
586}
587
1da177e4
LT
588static void handle_transaction_done(struct smi_info *smi_info)
589{
590 struct ipmi_smi_msg *msg;
1da177e4 591
f93aae9f 592 debug_timestamp("Done");
1da177e4
LT
593 switch (smi_info->si_state) {
594 case SI_NORMAL:
b0defcdb 595 if (!smi_info->curr_msg)
1da177e4
LT
596 break;
597
598 smi_info->curr_msg->rsp_size
599 = smi_info->handlers->get_result(
600 smi_info->si_sm,
601 smi_info->curr_msg->rsp,
602 IPMI_MAX_MSG_LENGTH);
603
c305e3d3
CM
604 /*
605 * Do this here becase deliver_recv_msg() releases the
606 * lock, and a new message can be put in during the
607 * time the lock is released.
608 */
1da177e4
LT
609 msg = smi_info->curr_msg;
610 smi_info->curr_msg = NULL;
611 deliver_recv_msg(smi_info, msg);
612 break;
613
614 case SI_GETTING_FLAGS:
615 {
616 unsigned char msg[4];
617 unsigned int len;
618
619 /* We got the flags from the SMI, now handle them. */
620 len = smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
621 if (msg[2] != 0) {
c305e3d3 622 /* Error fetching flags, just give up for now. */
1da177e4
LT
623 smi_info->si_state = SI_NORMAL;
624 } else if (len < 4) {
c305e3d3
CM
625 /*
626 * Hmm, no flags. That's technically illegal, but
627 * don't use uninitialized data.
628 */
1da177e4
LT
629 smi_info->si_state = SI_NORMAL;
630 } else {
631 smi_info->msg_flags = msg[3];
632 handle_flags(smi_info);
633 }
634 break;
635 }
636
637 case SI_CLEARING_FLAGS:
1da177e4
LT
638 {
639 unsigned char msg[3];
640
641 /* We cleared the flags. */
642 smi_info->handlers->get_result(smi_info->si_sm, msg, 3);
643 if (msg[2] != 0) {
644 /* Error clearing flags */
279fbd0c
MS
645 dev_warn(smi_info->dev,
646 "Error clearing flags: %2.2x\n", msg[2]);
1da177e4 647 }
d9b7e4f7 648 smi_info->si_state = SI_NORMAL;
1da177e4
LT
649 break;
650 }
651
652 case SI_GETTING_EVENTS:
653 {
654 smi_info->curr_msg->rsp_size
655 = smi_info->handlers->get_result(
656 smi_info->si_sm,
657 smi_info->curr_msg->rsp,
658 IPMI_MAX_MSG_LENGTH);
659
c305e3d3
CM
660 /*
661 * Do this here becase deliver_recv_msg() releases the
662 * lock, and a new message can be put in during the
663 * time the lock is released.
664 */
1da177e4
LT
665 msg = smi_info->curr_msg;
666 smi_info->curr_msg = NULL;
667 if (msg->rsp[2] != 0) {
668 /* Error getting event, probably done. */
669 msg->done(msg);
670
671 /* Take off the event flag. */
672 smi_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
673 handle_flags(smi_info);
674 } else {
64959e2d 675 smi_inc_stat(smi_info, events);
1da177e4 676
c305e3d3
CM
677 /*
678 * Do this before we deliver the message
679 * because delivering the message releases the
680 * lock and something else can mess with the
681 * state.
682 */
1da177e4
LT
683 handle_flags(smi_info);
684
685 deliver_recv_msg(smi_info, msg);
686 }
687 break;
688 }
689
690 case SI_GETTING_MESSAGES:
691 {
692 smi_info->curr_msg->rsp_size
693 = smi_info->handlers->get_result(
694 smi_info->si_sm,
695 smi_info->curr_msg->rsp,
696 IPMI_MAX_MSG_LENGTH);
697
c305e3d3
CM
698 /*
699 * Do this here becase deliver_recv_msg() releases the
700 * lock, and a new message can be put in during the
701 * time the lock is released.
702 */
1da177e4
LT
703 msg = smi_info->curr_msg;
704 smi_info->curr_msg = NULL;
705 if (msg->rsp[2] != 0) {
706 /* Error getting event, probably done. */
707 msg->done(msg);
708
709 /* Take off the msg flag. */
710 smi_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
711 handle_flags(smi_info);
712 } else {
64959e2d 713 smi_inc_stat(smi_info, incoming_messages);
1da177e4 714
c305e3d3
CM
715 /*
716 * Do this before we deliver the message
717 * because delivering the message releases the
718 * lock and something else can mess with the
719 * state.
720 */
1da177e4
LT
721 handle_flags(smi_info);
722
723 deliver_recv_msg(smi_info, msg);
724 }
725 break;
726 }
727
d9b7e4f7 728 case SI_CHECKING_ENABLES:
1da177e4
LT
729 {
730 unsigned char msg[4];
d9b7e4f7 731 u8 enables;
95c97b59 732 bool irq_on;
1da177e4
LT
733
734 /* We got the flags from the SMI, now handle them. */
735 smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
736 if (msg[2] != 0) {
0849bfec
CM
737 dev_warn(smi_info->dev,
738 "Couldn't get irq info: %x.\n", msg[2]);
739 dev_warn(smi_info->dev,
740 "Maybe ok, but ipmi might run very slowly.\n");
1da177e4 741 smi_info->si_state = SI_NORMAL;
d9b7e4f7
CM
742 break;
743 }
95c97b59
CM
744 enables = current_global_enables(smi_info, 0, &irq_on);
745 if (smi_info->si_type == SI_BT)
746 /* BT has its own interrupt enable bit. */
747 check_bt_irq(smi_info, irq_on);
d9b7e4f7
CM
748 if (enables != (msg[3] & GLOBAL_ENABLES_MASK)) {
749 /* Enables are not correct, fix them. */
1da177e4
LT
750 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
751 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
d9b7e4f7 752 msg[2] = enables | (msg[3] & ~GLOBAL_ENABLES_MASK);
1da177e4
LT
753 smi_info->handlers->start_transaction(
754 smi_info->si_sm, msg, 3);
d9b7e4f7
CM
755 smi_info->si_state = SI_SETTING_ENABLES;
756 } else if (smi_info->supports_event_msg_buff) {
757 smi_info->curr_msg = ipmi_alloc_smi_msg();
758 if (!smi_info->curr_msg) {
759 smi_info->si_state = SI_NORMAL;
760 break;
761 }
762 start_getting_msg_queue(smi_info);
763 } else {
764 smi_info->si_state = SI_NORMAL;
1da177e4
LT
765 }
766 break;
767 }
768
d9b7e4f7 769 case SI_SETTING_ENABLES:
1da177e4
LT
770 {
771 unsigned char msg[4];
772
1da177e4 773 smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
d9b7e4f7 774 if (msg[2] != 0)
0849bfec 775 dev_warn(smi_info->dev,
d9b7e4f7
CM
776 "Could not set the global enables: 0x%x.\n",
777 msg[2]);
778
779 if (smi_info->supports_event_msg_buff) {
780 smi_info->curr_msg = ipmi_alloc_smi_msg();
781 if (!smi_info->curr_msg) {
782 smi_info->si_state = SI_NORMAL;
783 break;
784 }
785 start_getting_msg_queue(smi_info);
ee6cd5f8 786 } else {
d9b7e4f7 787 smi_info->si_state = SI_NORMAL;
ee6cd5f8 788 }
ee6cd5f8
CM
789 break;
790 }
1da177e4
LT
791 }
792}
793
c305e3d3
CM
794/*
795 * Called on timeouts and events. Timeouts should pass the elapsed
796 * time, interrupts should pass in zero. Must be called with
797 * si_lock held and interrupts disabled.
798 */
1da177e4
LT
799static enum si_sm_result smi_event_handler(struct smi_info *smi_info,
800 int time)
801{
802 enum si_sm_result si_sm_result;
803
804 restart:
c305e3d3
CM
805 /*
806 * There used to be a loop here that waited a little while
807 * (around 25us) before giving up. That turned out to be
808 * pointless, the minimum delays I was seeing were in the 300us
809 * range, which is far too long to wait in an interrupt. So
810 * we just run until the state machine tells us something
811 * happened or it needs a delay.
812 */
1da177e4
LT
813 si_sm_result = smi_info->handlers->event(smi_info->si_sm, time);
814 time = 0;
815 while (si_sm_result == SI_SM_CALL_WITHOUT_DELAY)
1da177e4 816 si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
1da177e4 817
c305e3d3 818 if (si_sm_result == SI_SM_TRANSACTION_COMPLETE) {
64959e2d 819 smi_inc_stat(smi_info, complete_transactions);
1da177e4
LT
820
821 handle_transaction_done(smi_info);
822 si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
c305e3d3 823 } else if (si_sm_result == SI_SM_HOSED) {
64959e2d 824 smi_inc_stat(smi_info, hosed_count);
1da177e4 825
c305e3d3
CM
826 /*
827 * Do the before return_hosed_msg, because that
828 * releases the lock.
829 */
1da177e4
LT
830 smi_info->si_state = SI_NORMAL;
831 if (smi_info->curr_msg != NULL) {
c305e3d3
CM
832 /*
833 * If we were handling a user message, format
834 * a response to send to the upper layer to
835 * tell it about the error.
836 */
4d7cbac7 837 return_hosed_msg(smi_info, IPMI_ERR_UNSPECIFIED);
1da177e4
LT
838 }
839 si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
840 }
841
4ea18425
CM
842 /*
843 * We prefer handling attn over new messages. But don't do
844 * this if there is not yet an upper layer to handle anything.
845 */
a8df150c
CM
846 if (likely(smi_info->intf) &&
847 (si_sm_result == SI_SM_ATTN || smi_info->got_attn)) {
1da177e4
LT
848 unsigned char msg[2];
849
a8df150c
CM
850 if (smi_info->si_state != SI_NORMAL) {
851 /*
852 * We got an ATTN, but we are doing something else.
853 * Handle the ATTN later.
854 */
855 smi_info->got_attn = true;
856 } else {
857 smi_info->got_attn = false;
858 smi_inc_stat(smi_info, attentions);
1da177e4 859
a8df150c
CM
860 /*
861 * Got a attn, send down a get message flags to see
862 * what's causing it. It would be better to handle
863 * this in the upper layer, but due to the way
864 * interrupts work with the SMI, that's not really
865 * possible.
866 */
867 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
868 msg[1] = IPMI_GET_MSG_FLAGS_CMD;
1da177e4 869
a8df150c
CM
870 smi_info->handlers->start_transaction(
871 smi_info->si_sm, msg, 2);
872 smi_info->si_state = SI_GETTING_FLAGS;
873 goto restart;
874 }
1da177e4
LT
875 }
876
877 /* If we are currently idle, try to start the next message. */
878 if (si_sm_result == SI_SM_IDLE) {
64959e2d 879 smi_inc_stat(smi_info, idles);
1da177e4
LT
880
881 si_sm_result = start_next_msg(smi_info);
882 if (si_sm_result != SI_SM_IDLE)
883 goto restart;
c305e3d3 884 }
1da177e4
LT
885
886 if ((si_sm_result == SI_SM_IDLE)
c305e3d3
CM
887 && (atomic_read(&smi_info->req_events))) {
888 /*
889 * We are idle and the upper layer requested that I fetch
890 * events, so do so.
891 */
55162fb1 892 atomic_set(&smi_info->req_events, 0);
1da177e4 893
d9b7e4f7
CM
894 /*
895 * Take this opportunity to check the interrupt and
896 * message enable state for the BMC. The BMC can be
897 * asynchronously reset, and may thus get interrupts
898 * disable and messages disabled.
899 */
900 if (smi_info->supports_event_msg_buff || smi_info->irq) {
901 start_check_enables(smi_info);
902 } else {
903 smi_info->curr_msg = alloc_msg_handle_irq(smi_info);
904 if (!smi_info->curr_msg)
905 goto out;
1da177e4 906
d9b7e4f7
CM
907 start_getting_events(smi_info);
908 }
1da177e4
LT
909 goto restart;
910 }
55162fb1 911 out:
1da177e4
LT
912 return si_sm_result;
913}
914
89986496
CM
915static void check_start_timer_thread(struct smi_info *smi_info)
916{
917 if (smi_info->si_state == SI_NORMAL && smi_info->curr_msg == NULL) {
918 smi_mod_timer(smi_info, jiffies + SI_TIMEOUT_JIFFIES);
919
920 if (smi_info->thread)
921 wake_up_process(smi_info->thread);
922
923 start_next_msg(smi_info);
924 smi_event_handler(smi_info, 0);
925 }
926}
927
1da177e4 928static void sender(void *send_info,
99ab32f3 929 struct ipmi_smi_msg *msg)
1da177e4
LT
930{
931 struct smi_info *smi_info = send_info;
932 enum si_sm_result result;
933 unsigned long flags;
1da177e4 934
f93aae9f 935 debug_timestamp("Enqueue");
1da177e4
LT
936
937 if (smi_info->run_to_completion) {
bda4c30a 938 /*
b874b985
CM
939 * If we are running to completion, start it and run
940 * transactions until everything is clear.
bda4c30a 941 */
1d86e29b 942 smi_info->curr_msg = msg;
b874b985 943 smi_info->waiting_msg = NULL;
bda4c30a
CM
944
945 /*
946 * Run to completion means we are single-threaded, no
947 * need for locks.
948 */
1da177e4 949
1da177e4
LT
950 result = smi_event_handler(smi_info, 0);
951 while (result != SI_SM_IDLE) {
952 udelay(SI_SHORT_TIMEOUT_USEC);
953 result = smi_event_handler(smi_info,
954 SI_SHORT_TIMEOUT_USEC);
955 }
1da177e4 956 return;
1da177e4 957 }
1da177e4 958
f60adf42 959 spin_lock_irqsave(&smi_info->si_lock, flags);
1d86e29b
CM
960 /*
961 * The following two lines don't need to be under the lock for
962 * the lock's sake, but they do need SMP memory barriers to
963 * avoid getting things out of order. We are already claiming
964 * the lock, anyway, so just do it under the lock to avoid the
965 * ordering problem.
966 */
967 BUG_ON(smi_info->waiting_msg);
968 smi_info->waiting_msg = msg;
89986496 969 check_start_timer_thread(smi_info);
bda4c30a 970 spin_unlock_irqrestore(&smi_info->si_lock, flags);
1da177e4
LT
971}
972
7aefac26 973static void set_run_to_completion(void *send_info, bool i_run_to_completion)
1da177e4
LT
974{
975 struct smi_info *smi_info = send_info;
976 enum si_sm_result result;
1da177e4
LT
977
978 smi_info->run_to_completion = i_run_to_completion;
979 if (i_run_to_completion) {
980 result = smi_event_handler(smi_info, 0);
981 while (result != SI_SM_IDLE) {
982 udelay(SI_SHORT_TIMEOUT_USEC);
983 result = smi_event_handler(smi_info,
984 SI_SHORT_TIMEOUT_USEC);
985 }
986 }
1da177e4
LT
987}
988
ae74e823
MW
989/*
990 * Use -1 in the nsec value of the busy waiting timespec to tell that
991 * we are spinning in kipmid looking for something and not delaying
992 * between checks
993 */
48862ea2 994static inline void ipmi_si_set_not_busy(struct timespec64 *ts)
ae74e823
MW
995{
996 ts->tv_nsec = -1;
997}
48862ea2 998static inline int ipmi_si_is_busy(struct timespec64 *ts)
ae74e823
MW
999{
1000 return ts->tv_nsec != -1;
1001}
1002
cc4cbe90
AB
1003static inline int ipmi_thread_busy_wait(enum si_sm_result smi_result,
1004 const struct smi_info *smi_info,
48862ea2 1005 struct timespec64 *busy_until)
ae74e823
MW
1006{
1007 unsigned int max_busy_us = 0;
1008
1009 if (smi_info->intf_num < num_max_busy_us)
1010 max_busy_us = kipmid_max_busy_us[smi_info->intf_num];
1011 if (max_busy_us == 0 || smi_result != SI_SM_CALL_WITH_DELAY)
1012 ipmi_si_set_not_busy(busy_until);
1013 else if (!ipmi_si_is_busy(busy_until)) {
48862ea2
JS
1014 getnstimeofday64(busy_until);
1015 timespec64_add_ns(busy_until, max_busy_us*NSEC_PER_USEC);
ae74e823 1016 } else {
48862ea2
JS
1017 struct timespec64 now;
1018
1019 getnstimeofday64(&now);
1020 if (unlikely(timespec64_compare(&now, busy_until) > 0)) {
ae74e823
MW
1021 ipmi_si_set_not_busy(busy_until);
1022 return 0;
1023 }
1024 }
1025 return 1;
1026}
1027
1028
1029/*
1030 * A busy-waiting loop for speeding up IPMI operation.
1031 *
1032 * Lousy hardware makes this hard. This is only enabled for systems
1033 * that are not BT and do not have interrupts. It starts spinning
1034 * when an operation is complete or until max_busy tells it to stop
1035 * (if that is enabled). See the paragraph on kimid_max_busy_us in
1036 * Documentation/IPMI.txt for details.
1037 */
a9a2c44f
CM
1038static int ipmi_thread(void *data)
1039{
1040 struct smi_info *smi_info = data;
e9a705a0 1041 unsigned long flags;
a9a2c44f 1042 enum si_sm_result smi_result;
48862ea2 1043 struct timespec64 busy_until;
a9a2c44f 1044
ae74e823 1045 ipmi_si_set_not_busy(&busy_until);
8698a745 1046 set_user_nice(current, MAX_NICE);
e9a705a0 1047 while (!kthread_should_stop()) {
ae74e823
MW
1048 int busy_wait;
1049
a9a2c44f 1050 spin_lock_irqsave(&(smi_info->si_lock), flags);
8a3628d5 1051 smi_result = smi_event_handler(smi_info, 0);
48e8ac29
BS
1052
1053 /*
1054 * If the driver is doing something, there is a possible
1055 * race with the timer. If the timer handler see idle,
1056 * and the thread here sees something else, the timer
1057 * handler won't restart the timer even though it is
1058 * required. So start it here if necessary.
1059 */
1060 if (smi_result != SI_SM_IDLE && !smi_info->timer_running)
1061 smi_mod_timer(smi_info, jiffies + SI_TIMEOUT_JIFFIES);
1062
a9a2c44f 1063 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
ae74e823
MW
1064 busy_wait = ipmi_thread_busy_wait(smi_result, smi_info,
1065 &busy_until);
c305e3d3
CM
1066 if (smi_result == SI_SM_CALL_WITHOUT_DELAY)
1067 ; /* do nothing */
ae74e823 1068 else if (smi_result == SI_SM_CALL_WITH_DELAY && busy_wait)
33979734 1069 schedule();
89986496
CM
1070 else if (smi_result == SI_SM_IDLE) {
1071 if (atomic_read(&smi_info->need_watch)) {
1072 schedule_timeout_interruptible(100);
1073 } else {
1074 /* Wait to be woken up when we are needed. */
1075 __set_current_state(TASK_INTERRUPTIBLE);
1076 schedule();
1077 }
1078 } else
8d1f66dc 1079 schedule_timeout_interruptible(1);
a9a2c44f 1080 }
a9a2c44f
CM
1081 return 0;
1082}
1083
1084
1da177e4
LT
1085static void poll(void *send_info)
1086{
1087 struct smi_info *smi_info = send_info;
f60adf42 1088 unsigned long flags = 0;
7aefac26 1089 bool run_to_completion = smi_info->run_to_completion;
1da177e4 1090
15c62e10
CM
1091 /*
1092 * Make sure there is some delay in the poll loop so we can
1093 * drive time forward and timeout things.
1094 */
1095 udelay(10);
f60adf42
CM
1096 if (!run_to_completion)
1097 spin_lock_irqsave(&smi_info->si_lock, flags);
15c62e10 1098 smi_event_handler(smi_info, 10);
f60adf42
CM
1099 if (!run_to_completion)
1100 spin_unlock_irqrestore(&smi_info->si_lock, flags);
1da177e4
LT
1101}
1102
1103static void request_events(void *send_info)
1104{
1105 struct smi_info *smi_info = send_info;
1106
b874b985 1107 if (!smi_info->has_event_buffer)
b361e27b
CM
1108 return;
1109
1da177e4
LT
1110 atomic_set(&smi_info->req_events, 1);
1111}
1112
7aefac26 1113static void set_need_watch(void *send_info, bool enable)
89986496
CM
1114{
1115 struct smi_info *smi_info = send_info;
1116 unsigned long flags;
1117
1118 atomic_set(&smi_info->need_watch, enable);
1119 spin_lock_irqsave(&smi_info->si_lock, flags);
1120 check_start_timer_thread(smi_info);
1121 spin_unlock_irqrestore(&smi_info->si_lock, flags);
1122}
1123
0c8204b3 1124static int initialized;
1da177e4 1125
1da177e4
LT
1126static void smi_timeout(unsigned long data)
1127{
1128 struct smi_info *smi_info = (struct smi_info *) data;
1129 enum si_sm_result smi_result;
1130 unsigned long flags;
1131 unsigned long jiffies_now;
c4edff1c 1132 long time_diff;
3326f4f2 1133 long timeout;
1da177e4 1134
1da177e4 1135 spin_lock_irqsave(&(smi_info->si_lock), flags);
f93aae9f
JS
1136 debug_timestamp("Timer");
1137
1da177e4 1138 jiffies_now = jiffies;
c4edff1c 1139 time_diff = (((long)jiffies_now - (long)smi_info->last_timeout_jiffies)
1da177e4
LT
1140 * SI_USEC_PER_JIFFY);
1141 smi_result = smi_event_handler(smi_info, time_diff);
1142
b0defcdb 1143 if ((smi_info->irq) && (!smi_info->interrupt_disabled)) {
1da177e4 1144 /* Running with interrupts, only do long timeouts. */
3326f4f2 1145 timeout = jiffies + SI_TIMEOUT_JIFFIES;
64959e2d 1146 smi_inc_stat(smi_info, long_timeouts);
3326f4f2 1147 goto do_mod_timer;
1da177e4
LT
1148 }
1149
c305e3d3
CM
1150 /*
1151 * If the state machine asks for a short delay, then shorten
1152 * the timer timeout.
1153 */
1da177e4 1154 if (smi_result == SI_SM_CALL_WITH_DELAY) {
64959e2d 1155 smi_inc_stat(smi_info, short_timeouts);
3326f4f2 1156 timeout = jiffies + 1;
1da177e4 1157 } else {
64959e2d 1158 smi_inc_stat(smi_info, long_timeouts);
3326f4f2 1159 timeout = jiffies + SI_TIMEOUT_JIFFIES;
1da177e4
LT
1160 }
1161
3326f4f2
MG
1162 do_mod_timer:
1163 if (smi_result != SI_SM_IDLE)
48e8ac29
BS
1164 smi_mod_timer(smi_info, timeout);
1165 else
1166 smi_info->timer_running = false;
1167 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
1da177e4
LT
1168}
1169
7d12e780 1170static irqreturn_t si_irq_handler(int irq, void *data)
1da177e4
LT
1171{
1172 struct smi_info *smi_info = data;
1173 unsigned long flags;
1da177e4
LT
1174
1175 spin_lock_irqsave(&(smi_info->si_lock), flags);
1176
64959e2d 1177 smi_inc_stat(smi_info, interrupts);
1da177e4 1178
f93aae9f
JS
1179 debug_timestamp("Interrupt");
1180
1da177e4 1181 smi_event_handler(smi_info, 0);
1da177e4
LT
1182 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
1183 return IRQ_HANDLED;
1184}
1185
7d12e780 1186static irqreturn_t si_bt_irq_handler(int irq, void *data)
9dbf68f9
CM
1187{
1188 struct smi_info *smi_info = data;
1189 /* We need to clear the IRQ flag for the BT interface. */
1190 smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG,
1191 IPMI_BT_INTMASK_CLEAR_IRQ_BIT
1192 | IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
7d12e780 1193 return si_irq_handler(irq, data);
9dbf68f9
CM
1194}
1195
453823ba
CM
1196static int smi_start_processing(void *send_info,
1197 ipmi_smi_t intf)
1198{
1199 struct smi_info *new_smi = send_info;
a51f4a81 1200 int enable = 0;
453823ba
CM
1201
1202 new_smi->intf = intf;
1203
c45adc39
CM
1204 /* Try to claim any interrupts. */
1205 if (new_smi->irq_setup)
1206 new_smi->irq_setup(new_smi);
1207
453823ba
CM
1208 /* Set up the timer that drives the interface. */
1209 setup_timer(&new_smi->si_timer, smi_timeout, (long)new_smi);
48e8ac29 1210 smi_mod_timer(new_smi, jiffies + SI_TIMEOUT_JIFFIES);
453823ba 1211
a51f4a81
CM
1212 /*
1213 * Check if the user forcefully enabled the daemon.
1214 */
1215 if (new_smi->intf_num < num_force_kipmid)
1216 enable = force_kipmid[new_smi->intf_num];
df3fe8de
CM
1217 /*
1218 * The BT interface is efficient enough to not need a thread,
1219 * and there is no need for a thread if we have interrupts.
1220 */
c305e3d3 1221 else if ((new_smi->si_type != SI_BT) && (!new_smi->irq))
a51f4a81
CM
1222 enable = 1;
1223
1224 if (enable) {
453823ba
CM
1225 new_smi->thread = kthread_run(ipmi_thread, new_smi,
1226 "kipmi%d", new_smi->intf_num);
1227 if (IS_ERR(new_smi->thread)) {
279fbd0c
MS
1228 dev_notice(new_smi->dev, "Could not start"
1229 " kernel thread due to error %ld, only using"
1230 " timers to drive the interface\n",
1231 PTR_ERR(new_smi->thread));
453823ba
CM
1232 new_smi->thread = NULL;
1233 }
1234 }
1235
1236 return 0;
1237}
9dbf68f9 1238
16f4232c
ZY
1239static int get_smi_info(void *send_info, struct ipmi_smi_info *data)
1240{
1241 struct smi_info *smi = send_info;
1242
1243 data->addr_src = smi->addr_source;
1244 data->dev = smi->dev;
1245 data->addr_info = smi->addr_info;
1246 get_device(smi->dev);
1247
1248 return 0;
1249}
1250
7aefac26 1251static void set_maintenance_mode(void *send_info, bool enable)
b9675136
CM
1252{
1253 struct smi_info *smi_info = send_info;
1254
1255 if (!enable)
1256 atomic_set(&smi_info->req_events, 0);
1257}
1258
c305e3d3 1259static struct ipmi_smi_handlers handlers = {
1da177e4 1260 .owner = THIS_MODULE,
453823ba 1261 .start_processing = smi_start_processing,
16f4232c 1262 .get_smi_info = get_smi_info,
1da177e4
LT
1263 .sender = sender,
1264 .request_events = request_events,
89986496 1265 .set_need_watch = set_need_watch,
b9675136 1266 .set_maintenance_mode = set_maintenance_mode,
1da177e4
LT
1267 .set_run_to_completion = set_run_to_completion,
1268 .poll = poll,
1269};
1270
c305e3d3
CM
1271/*
1272 * There can be 4 IO ports passed in (with or without IRQs), 4 addresses,
1273 * a default IO port, and 1 ACPI/SPMI address. That sets SI_MAX_DRIVERS.
1274 */
1da177e4 1275
b0defcdb 1276static LIST_HEAD(smi_infos);
d6dfd131 1277static DEFINE_MUTEX(smi_infos_lock);
b0defcdb 1278static int smi_num; /* Used to sequence the SMIs */
1da177e4 1279
1da177e4 1280#define DEFAULT_REGSPACING 1
dba9b4f6 1281#define DEFAULT_REGSIZE 1
1da177e4 1282
d941aeae
CM
1283#ifdef CONFIG_ACPI
1284static bool si_tryacpi = 1;
1285#endif
1286#ifdef CONFIG_DMI
1287static bool si_trydmi = 1;
1288#endif
f2afae46
CM
1289static bool si_tryplatform = 1;
1290#ifdef CONFIG_PCI
1291static bool si_trypci = 1;
1292#endif
0dfe6e7e 1293static bool si_trydefaults = IS_ENABLED(CONFIG_IPMI_SI_PROBE_DEFAULTS);
1da177e4
LT
1294static char *si_type[SI_MAX_PARMS];
1295#define MAX_SI_TYPE_STR 30
1296static char si_type_str[MAX_SI_TYPE_STR];
1297static unsigned long addrs[SI_MAX_PARMS];
64a6f950 1298static unsigned int num_addrs;
1da177e4 1299static unsigned int ports[SI_MAX_PARMS];
64a6f950 1300static unsigned int num_ports;
1da177e4 1301static int irqs[SI_MAX_PARMS];
64a6f950 1302static unsigned int num_irqs;
1da177e4 1303static int regspacings[SI_MAX_PARMS];
64a6f950 1304static unsigned int num_regspacings;
1da177e4 1305static int regsizes[SI_MAX_PARMS];
64a6f950 1306static unsigned int num_regsizes;
1da177e4 1307static int regshifts[SI_MAX_PARMS];
64a6f950 1308static unsigned int num_regshifts;
2f95d513 1309static int slave_addrs[SI_MAX_PARMS]; /* Leaving 0 chooses the default value */
64a6f950 1310static unsigned int num_slave_addrs;
1da177e4 1311
b361e27b
CM
1312#define IPMI_IO_ADDR_SPACE 0
1313#define IPMI_MEM_ADDR_SPACE 1
1d5636cc 1314static char *addr_space_to_str[] = { "i/o", "mem" };
b361e27b
CM
1315
1316static int hotmod_handler(const char *val, struct kernel_param *kp);
1317
1318module_param_call(hotmod, hotmod_handler, NULL, NULL, 0200);
1319MODULE_PARM_DESC(hotmod, "Add and remove interfaces. See"
1320 " Documentation/IPMI.txt in the kernel sources for the"
1321 " gory details.");
1da177e4 1322
d941aeae
CM
1323#ifdef CONFIG_ACPI
1324module_param_named(tryacpi, si_tryacpi, bool, 0);
1325MODULE_PARM_DESC(tryacpi, "Setting this to zero will disable the"
1326 " default scan of the interfaces identified via ACPI");
1327#endif
1328#ifdef CONFIG_DMI
1329module_param_named(trydmi, si_trydmi, bool, 0);
1330MODULE_PARM_DESC(trydmi, "Setting this to zero will disable the"
1331 " default scan of the interfaces identified via DMI");
1332#endif
f2afae46
CM
1333module_param_named(tryplatform, si_tryplatform, bool, 0);
1334MODULE_PARM_DESC(tryacpi, "Setting this to zero will disable the"
1335 " default scan of the interfaces identified via platform"
1336 " interfaces like openfirmware");
1337#ifdef CONFIG_PCI
1338module_param_named(trypci, si_trypci, bool, 0);
1339MODULE_PARM_DESC(tryacpi, "Setting this to zero will disable the"
1340 " default scan of the interfaces identified via pci");
1341#endif
1da177e4
LT
1342module_param_named(trydefaults, si_trydefaults, bool, 0);
1343MODULE_PARM_DESC(trydefaults, "Setting this to 'false' will disable the"
1344 " default scan of the KCS and SMIC interface at the standard"
1345 " address");
1346module_param_string(type, si_type_str, MAX_SI_TYPE_STR, 0);
1347MODULE_PARM_DESC(type, "Defines the type of each interface, each"
1348 " interface separated by commas. The types are 'kcs',"
1349 " 'smic', and 'bt'. For example si_type=kcs,bt will set"
1350 " the first interface to kcs and the second to bt");
64a6f950 1351module_param_array(addrs, ulong, &num_addrs, 0);
1da177e4
LT
1352MODULE_PARM_DESC(addrs, "Sets the memory address of each interface, the"
1353 " addresses separated by commas. Only use if an interface"
1354 " is in memory. Otherwise, set it to zero or leave"
1355 " it blank.");
64a6f950 1356module_param_array(ports, uint, &num_ports, 0);
1da177e4
LT
1357MODULE_PARM_DESC(ports, "Sets the port address of each interface, the"
1358 " addresses separated by commas. Only use if an interface"
1359 " is a port. Otherwise, set it to zero or leave"
1360 " it blank.");
1361module_param_array(irqs, int, &num_irqs, 0);
1362MODULE_PARM_DESC(irqs, "Sets the interrupt of each interface, the"
1363 " addresses separated by commas. Only use if an interface"
1364 " has an interrupt. Otherwise, set it to zero or leave"
1365 " it blank.");
1366module_param_array(regspacings, int, &num_regspacings, 0);
1367MODULE_PARM_DESC(regspacings, "The number of bytes between the start address"
1368 " and each successive register used by the interface. For"
1369 " instance, if the start address is 0xca2 and the spacing"
1370 " is 2, then the second address is at 0xca4. Defaults"
1371 " to 1.");
1372module_param_array(regsizes, int, &num_regsizes, 0);
1373MODULE_PARM_DESC(regsizes, "The size of the specific IPMI register in bytes."
1374 " This should generally be 1, 2, 4, or 8 for an 8-bit,"
1375 " 16-bit, 32-bit, or 64-bit register. Use this if you"
1376 " the 8-bit IPMI register has to be read from a larger"
1377 " register.");
1378module_param_array(regshifts, int, &num_regshifts, 0);
1379MODULE_PARM_DESC(regshifts, "The amount to shift the data read from the."
1380 " IPMI register, in bits. For instance, if the data"
1381 " is read from a 32-bit word and the IPMI data is in"
1382 " bit 8-15, then the shift would be 8");
1383module_param_array(slave_addrs, int, &num_slave_addrs, 0);
1384MODULE_PARM_DESC(slave_addrs, "Set the default IPMB slave address for"
1385 " the controller. Normally this is 0x20, but can be"
1386 " overridden by this parm. This is an array indexed"
1387 " by interface number.");
a51f4a81
CM
1388module_param_array(force_kipmid, int, &num_force_kipmid, 0);
1389MODULE_PARM_DESC(force_kipmid, "Force the kipmi daemon to be enabled (1) or"
1390 " disabled(0). Normally the IPMI driver auto-detects"
1391 " this, but the value may be overridden by this parm.");
7aefac26 1392module_param(unload_when_empty, bool, 0);
b361e27b
CM
1393MODULE_PARM_DESC(unload_when_empty, "Unload the module if no interfaces are"
1394 " specified or found, default is 1. Setting to 0"
1395 " is useful for hot add of devices using hotmod.");
ae74e823
MW
1396module_param_array(kipmid_max_busy_us, uint, &num_max_busy_us, 0644);
1397MODULE_PARM_DESC(kipmid_max_busy_us,
1398 "Max time (in microseconds) to busy-wait for IPMI data before"
1399 " sleeping. 0 (default) means to wait forever. Set to 100-500"
1400 " if kipmid is using up a lot of CPU time.");
1da177e4
LT
1401
1402
b0defcdb 1403static void std_irq_cleanup(struct smi_info *info)
1da177e4 1404{
b0defcdb
CM
1405 if (info->si_type == SI_BT)
1406 /* Disable the interrupt in the BT interface. */
1407 info->io.outputb(&info->io, IPMI_BT_INTMASK_REG, 0);
1408 free_irq(info->irq, info);
1da177e4 1409}
1da177e4
LT
1410
1411static int std_irq_setup(struct smi_info *info)
1412{
1413 int rv;
1414
b0defcdb 1415 if (!info->irq)
1da177e4
LT
1416 return 0;
1417
9dbf68f9
CM
1418 if (info->si_type == SI_BT) {
1419 rv = request_irq(info->irq,
1420 si_bt_irq_handler,
aa5b2bab 1421 IRQF_SHARED,
9dbf68f9
CM
1422 DEVICE_NAME,
1423 info);
b0defcdb 1424 if (!rv)
9dbf68f9
CM
1425 /* Enable the interrupt in the BT interface. */
1426 info->io.outputb(&info->io, IPMI_BT_INTMASK_REG,
1427 IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
1428 } else
1429 rv = request_irq(info->irq,
1430 si_irq_handler,
aa5b2bab 1431 IRQF_SHARED,
9dbf68f9
CM
1432 DEVICE_NAME,
1433 info);
1da177e4 1434 if (rv) {
279fbd0c
MS
1435 dev_warn(info->dev, "%s unable to claim interrupt %d,"
1436 " running polled\n",
1437 DEVICE_NAME, info->irq);
1da177e4
LT
1438 info->irq = 0;
1439 } else {
b0defcdb 1440 info->irq_cleanup = std_irq_cleanup;
279fbd0c 1441 dev_info(info->dev, "Using irq %d\n", info->irq);
1da177e4
LT
1442 }
1443
1444 return rv;
1445}
1446
1da177e4
LT
1447static unsigned char port_inb(struct si_sm_io *io, unsigned int offset)
1448{
b0defcdb 1449 unsigned int addr = io->addr_data;
1da177e4 1450
b0defcdb 1451 return inb(addr + (offset * io->regspacing));
1da177e4
LT
1452}
1453
1454static void port_outb(struct si_sm_io *io, unsigned int offset,
1455 unsigned char b)
1456{
b0defcdb 1457 unsigned int addr = io->addr_data;
1da177e4 1458
b0defcdb 1459 outb(b, addr + (offset * io->regspacing));
1da177e4
LT
1460}
1461
1462static unsigned char port_inw(struct si_sm_io *io, unsigned int offset)
1463{
b0defcdb 1464 unsigned int addr = io->addr_data;
1da177e4 1465
b0defcdb 1466 return (inw(addr + (offset * io->regspacing)) >> io->regshift) & 0xff;
1da177e4
LT
1467}
1468
1469static void port_outw(struct si_sm_io *io, unsigned int offset,
1470 unsigned char b)
1471{
b0defcdb 1472 unsigned int addr = io->addr_data;
1da177e4 1473
b0defcdb 1474 outw(b << io->regshift, addr + (offset * io->regspacing));
1da177e4
LT
1475}
1476
1477static unsigned char port_inl(struct si_sm_io *io, unsigned int offset)
1478{
b0defcdb 1479 unsigned int addr = io->addr_data;
1da177e4 1480
b0defcdb 1481 return (inl(addr + (offset * io->regspacing)) >> io->regshift) & 0xff;
1da177e4
LT
1482}
1483
1484static void port_outl(struct si_sm_io *io, unsigned int offset,
1485 unsigned char b)
1486{
b0defcdb 1487 unsigned int addr = io->addr_data;
1da177e4 1488
b0defcdb 1489 outl(b << io->regshift, addr+(offset * io->regspacing));
1da177e4
LT
1490}
1491
1492static void port_cleanup(struct smi_info *info)
1493{
b0defcdb 1494 unsigned int addr = info->io.addr_data;
d61a3ead 1495 int idx;
1da177e4 1496
b0defcdb 1497 if (addr) {
c305e3d3 1498 for (idx = 0; idx < info->io_size; idx++)
d61a3ead
CM
1499 release_region(addr + idx * info->io.regspacing,
1500 info->io.regsize);
1da177e4 1501 }
1da177e4
LT
1502}
1503
1504static int port_setup(struct smi_info *info)
1505{
b0defcdb 1506 unsigned int addr = info->io.addr_data;
d61a3ead 1507 int idx;
1da177e4 1508
b0defcdb 1509 if (!addr)
1da177e4
LT
1510 return -ENODEV;
1511
1512 info->io_cleanup = port_cleanup;
1513
c305e3d3
CM
1514 /*
1515 * Figure out the actual inb/inw/inl/etc routine to use based
1516 * upon the register size.
1517 */
1da177e4
LT
1518 switch (info->io.regsize) {
1519 case 1:
1520 info->io.inputb = port_inb;
1521 info->io.outputb = port_outb;
1522 break;
1523 case 2:
1524 info->io.inputb = port_inw;
1525 info->io.outputb = port_outw;
1526 break;
1527 case 4:
1528 info->io.inputb = port_inl;
1529 info->io.outputb = port_outl;
1530 break;
1531 default:
279fbd0c
MS
1532 dev_warn(info->dev, "Invalid register size: %d\n",
1533 info->io.regsize);
1da177e4
LT
1534 return -EINVAL;
1535 }
1536
c305e3d3
CM
1537 /*
1538 * Some BIOSes reserve disjoint I/O regions in their ACPI
d61a3ead
CM
1539 * tables. This causes problems when trying to register the
1540 * entire I/O region. Therefore we must register each I/O
1541 * port separately.
1542 */
c305e3d3 1543 for (idx = 0; idx < info->io_size; idx++) {
d61a3ead
CM
1544 if (request_region(addr + idx * info->io.regspacing,
1545 info->io.regsize, DEVICE_NAME) == NULL) {
1546 /* Undo allocations */
1547 while (idx--) {
1548 release_region(addr + idx * info->io.regspacing,
1549 info->io.regsize);
1550 }
1551 return -EIO;
1552 }
1553 }
1da177e4
LT
1554 return 0;
1555}
1556
546cfdf4 1557static unsigned char intf_mem_inb(struct si_sm_io *io, unsigned int offset)
1da177e4
LT
1558{
1559 return readb((io->addr)+(offset * io->regspacing));
1560}
1561
546cfdf4 1562static void intf_mem_outb(struct si_sm_io *io, unsigned int offset,
1da177e4
LT
1563 unsigned char b)
1564{
1565 writeb(b, (io->addr)+(offset * io->regspacing));
1566}
1567
546cfdf4 1568static unsigned char intf_mem_inw(struct si_sm_io *io, unsigned int offset)
1da177e4
LT
1569{
1570 return (readw((io->addr)+(offset * io->regspacing)) >> io->regshift)
64d9fe69 1571 & 0xff;
1da177e4
LT
1572}
1573
546cfdf4 1574static void intf_mem_outw(struct si_sm_io *io, unsigned int offset,
1da177e4
LT
1575 unsigned char b)
1576{
1577 writeb(b << io->regshift, (io->addr)+(offset * io->regspacing));
1578}
1579
546cfdf4 1580static unsigned char intf_mem_inl(struct si_sm_io *io, unsigned int offset)
1da177e4
LT
1581{
1582 return (readl((io->addr)+(offset * io->regspacing)) >> io->regshift)
64d9fe69 1583 & 0xff;
1da177e4
LT
1584}
1585
546cfdf4 1586static void intf_mem_outl(struct si_sm_io *io, unsigned int offset,
1da177e4
LT
1587 unsigned char b)
1588{
1589 writel(b << io->regshift, (io->addr)+(offset * io->regspacing));
1590}
1591
1592#ifdef readq
1593static unsigned char mem_inq(struct si_sm_io *io, unsigned int offset)
1594{
1595 return (readq((io->addr)+(offset * io->regspacing)) >> io->regshift)
64d9fe69 1596 & 0xff;
1da177e4
LT
1597}
1598
1599static void mem_outq(struct si_sm_io *io, unsigned int offset,
1600 unsigned char b)
1601{
1602 writeq(b << io->regshift, (io->addr)+(offset * io->regspacing));
1603}
1604#endif
1605
1606static void mem_cleanup(struct smi_info *info)
1607{
b0defcdb 1608 unsigned long addr = info->io.addr_data;
1da177e4
LT
1609 int mapsize;
1610
1611 if (info->io.addr) {
1612 iounmap(info->io.addr);
1613
1614 mapsize = ((info->io_size * info->io.regspacing)
1615 - (info->io.regspacing - info->io.regsize));
1616
b0defcdb 1617 release_mem_region(addr, mapsize);
1da177e4 1618 }
1da177e4
LT
1619}
1620
1621static int mem_setup(struct smi_info *info)
1622{
b0defcdb 1623 unsigned long addr = info->io.addr_data;
1da177e4
LT
1624 int mapsize;
1625
b0defcdb 1626 if (!addr)
1da177e4
LT
1627 return -ENODEV;
1628
1629 info->io_cleanup = mem_cleanup;
1630
c305e3d3
CM
1631 /*
1632 * Figure out the actual readb/readw/readl/etc routine to use based
1633 * upon the register size.
1634 */
1da177e4
LT
1635 switch (info->io.regsize) {
1636 case 1:
546cfdf4
AD
1637 info->io.inputb = intf_mem_inb;
1638 info->io.outputb = intf_mem_outb;
1da177e4
LT
1639 break;
1640 case 2:
546cfdf4
AD
1641 info->io.inputb = intf_mem_inw;
1642 info->io.outputb = intf_mem_outw;
1da177e4
LT
1643 break;
1644 case 4:
546cfdf4
AD
1645 info->io.inputb = intf_mem_inl;
1646 info->io.outputb = intf_mem_outl;
1da177e4
LT
1647 break;
1648#ifdef readq
1649 case 8:
1650 info->io.inputb = mem_inq;
1651 info->io.outputb = mem_outq;
1652 break;
1653#endif
1654 default:
279fbd0c
MS
1655 dev_warn(info->dev, "Invalid register size: %d\n",
1656 info->io.regsize);
1da177e4
LT
1657 return -EINVAL;
1658 }
1659
c305e3d3
CM
1660 /*
1661 * Calculate the total amount of memory to claim. This is an
1da177e4
LT
1662 * unusual looking calculation, but it avoids claiming any
1663 * more memory than it has to. It will claim everything
1664 * between the first address to the end of the last full
c305e3d3
CM
1665 * register.
1666 */
1da177e4
LT
1667 mapsize = ((info->io_size * info->io.regspacing)
1668 - (info->io.regspacing - info->io.regsize));
1669
b0defcdb 1670 if (request_mem_region(addr, mapsize, DEVICE_NAME) == NULL)
1da177e4
LT
1671 return -EIO;
1672
b0defcdb 1673 info->io.addr = ioremap(addr, mapsize);
1da177e4 1674 if (info->io.addr == NULL) {
b0defcdb 1675 release_mem_region(addr, mapsize);
1da177e4
LT
1676 return -EIO;
1677 }
1678 return 0;
1679}
1680
b361e27b
CM
1681/*
1682 * Parms come in as <op1>[:op2[:op3...]]. ops are:
1683 * add|remove,kcs|bt|smic,mem|i/o,<address>[,<opt1>[,<opt2>[,...]]]
1684 * Options are:
1685 * rsp=<regspacing>
1686 * rsi=<regsize>
1687 * rsh=<regshift>
1688 * irq=<irq>
1689 * ipmb=<ipmb addr>
1690 */
1691enum hotmod_op { HM_ADD, HM_REMOVE };
1692struct hotmod_vals {
1693 char *name;
1694 int val;
1695};
1696static struct hotmod_vals hotmod_ops[] = {
1697 { "add", HM_ADD },
1698 { "remove", HM_REMOVE },
1699 { NULL }
1700};
1701static struct hotmod_vals hotmod_si[] = {
1702 { "kcs", SI_KCS },
1703 { "smic", SI_SMIC },
1704 { "bt", SI_BT },
1705 { NULL }
1706};
1707static struct hotmod_vals hotmod_as[] = {
1708 { "mem", IPMI_MEM_ADDR_SPACE },
1709 { "i/o", IPMI_IO_ADDR_SPACE },
1710 { NULL }
1711};
1d5636cc 1712
b361e27b
CM
1713static int parse_str(struct hotmod_vals *v, int *val, char *name, char **curr)
1714{
1715 char *s;
1716 int i;
1717
1718 s = strchr(*curr, ',');
1719 if (!s) {
1720 printk(KERN_WARNING PFX "No hotmod %s given.\n", name);
1721 return -EINVAL;
1722 }
1723 *s = '\0';
1724 s++;
ceb51ca8 1725 for (i = 0; v[i].name; i++) {
1d5636cc 1726 if (strcmp(*curr, v[i].name) == 0) {
b361e27b
CM
1727 *val = v[i].val;
1728 *curr = s;
1729 return 0;
1730 }
1731 }
1732
1733 printk(KERN_WARNING PFX "Invalid hotmod %s '%s'\n", name, *curr);
1734 return -EINVAL;
1735}
1736
1d5636cc
CM
1737static int check_hotmod_int_op(const char *curr, const char *option,
1738 const char *name, int *val)
1739{
1740 char *n;
1741
1742 if (strcmp(curr, name) == 0) {
1743 if (!option) {
1744 printk(KERN_WARNING PFX
1745 "No option given for '%s'\n",
1746 curr);
1747 return -EINVAL;
1748 }
1749 *val = simple_strtoul(option, &n, 0);
1750 if ((*n != '\0') || (*option == '\0')) {
1751 printk(KERN_WARNING PFX
1752 "Bad option given for '%s'\n",
1753 curr);
1754 return -EINVAL;
1755 }
1756 return 1;
1757 }
1758 return 0;
1759}
1760
de5e2ddf
ED
1761static struct smi_info *smi_info_alloc(void)
1762{
1763 struct smi_info *info = kzalloc(sizeof(*info), GFP_KERNEL);
1764
f60adf42 1765 if (info)
de5e2ddf 1766 spin_lock_init(&info->si_lock);
de5e2ddf
ED
1767 return info;
1768}
1769
b361e27b
CM
1770static int hotmod_handler(const char *val, struct kernel_param *kp)
1771{
1772 char *str = kstrdup(val, GFP_KERNEL);
1d5636cc 1773 int rv;
b361e27b
CM
1774 char *next, *curr, *s, *n, *o;
1775 enum hotmod_op op;
1776 enum si_type si_type;
1777 int addr_space;
1778 unsigned long addr;
1779 int regspacing;
1780 int regsize;
1781 int regshift;
1782 int irq;
1783 int ipmb;
1784 int ival;
1d5636cc 1785 int len;
b361e27b
CM
1786 struct smi_info *info;
1787
1788 if (!str)
1789 return -ENOMEM;
1790
1791 /* Kill any trailing spaces, as we can get a "\n" from echo. */
1d5636cc
CM
1792 len = strlen(str);
1793 ival = len - 1;
b361e27b
CM
1794 while ((ival >= 0) && isspace(str[ival])) {
1795 str[ival] = '\0';
1796 ival--;
1797 }
1798
1799 for (curr = str; curr; curr = next) {
1800 regspacing = 1;
1801 regsize = 1;
1802 regshift = 0;
1803 irq = 0;
2f95d513 1804 ipmb = 0; /* Choose the default if not specified */
b361e27b
CM
1805
1806 next = strchr(curr, ':');
1807 if (next) {
1808 *next = '\0';
1809 next++;
1810 }
1811
1812 rv = parse_str(hotmod_ops, &ival, "operation", &curr);
1813 if (rv)
1814 break;
1815 op = ival;
1816
1817 rv = parse_str(hotmod_si, &ival, "interface type", &curr);
1818 if (rv)
1819 break;
1820 si_type = ival;
1821
1822 rv = parse_str(hotmod_as, &addr_space, "address space", &curr);
1823 if (rv)
1824 break;
1825
1826 s = strchr(curr, ',');
1827 if (s) {
1828 *s = '\0';
1829 s++;
1830 }
1831 addr = simple_strtoul(curr, &n, 0);
1832 if ((*n != '\0') || (*curr == '\0')) {
1833 printk(KERN_WARNING PFX "Invalid hotmod address"
1834 " '%s'\n", curr);
1835 break;
1836 }
1837
1838 while (s) {
1839 curr = s;
1840 s = strchr(curr, ',');
1841 if (s) {
1842 *s = '\0';
1843 s++;
1844 }
1845 o = strchr(curr, '=');
1846 if (o) {
1847 *o = '\0';
1848 o++;
1849 }
1d5636cc
CM
1850 rv = check_hotmod_int_op(curr, o, "rsp", &regspacing);
1851 if (rv < 0)
b361e27b 1852 goto out;
1d5636cc
CM
1853 else if (rv)
1854 continue;
1855 rv = check_hotmod_int_op(curr, o, "rsi", &regsize);
1856 if (rv < 0)
1857 goto out;
1858 else if (rv)
1859 continue;
1860 rv = check_hotmod_int_op(curr, o, "rsh", &regshift);
1861 if (rv < 0)
1862 goto out;
1863 else if (rv)
1864 continue;
1865 rv = check_hotmod_int_op(curr, o, "irq", &irq);
1866 if (rv < 0)
1867 goto out;
1868 else if (rv)
1869 continue;
1870 rv = check_hotmod_int_op(curr, o, "ipmb", &ipmb);
1871 if (rv < 0)
1872 goto out;
1873 else if (rv)
1874 continue;
1875
1876 rv = -EINVAL;
1877 printk(KERN_WARNING PFX
1878 "Invalid hotmod option '%s'\n",
1879 curr);
1880 goto out;
b361e27b
CM
1881 }
1882
1883 if (op == HM_ADD) {
de5e2ddf 1884 info = smi_info_alloc();
b361e27b
CM
1885 if (!info) {
1886 rv = -ENOMEM;
1887 goto out;
1888 }
1889
5fedc4a2 1890 info->addr_source = SI_HOTMOD;
b361e27b
CM
1891 info->si_type = si_type;
1892 info->io.addr_data = addr;
1893 info->io.addr_type = addr_space;
1894 if (addr_space == IPMI_MEM_ADDR_SPACE)
1895 info->io_setup = mem_setup;
1896 else
1897 info->io_setup = port_setup;
1898
1899 info->io.addr = NULL;
1900 info->io.regspacing = regspacing;
1901 if (!info->io.regspacing)
1902 info->io.regspacing = DEFAULT_REGSPACING;
1903 info->io.regsize = regsize;
1904 if (!info->io.regsize)
1905 info->io.regsize = DEFAULT_REGSPACING;
1906 info->io.regshift = regshift;
1907 info->irq = irq;
1908 if (info->irq)
1909 info->irq_setup = std_irq_setup;
1910 info->slave_addr = ipmb;
1911
d02b3709
CM
1912 rv = add_smi(info);
1913 if (rv) {
7faefea6 1914 kfree(info);
d02b3709
CM
1915 goto out;
1916 }
1917 rv = try_smi_init(info);
1918 if (rv) {
1919 cleanup_one_si(info);
1920 goto out;
7faefea6 1921 }
b361e27b
CM
1922 } else {
1923 /* remove */
1924 struct smi_info *e, *tmp_e;
1925
1926 mutex_lock(&smi_infos_lock);
1927 list_for_each_entry_safe(e, tmp_e, &smi_infos, link) {
1928 if (e->io.addr_type != addr_space)
1929 continue;
1930 if (e->si_type != si_type)
1931 continue;
1932 if (e->io.addr_data == addr)
1933 cleanup_one_si(e);
1934 }
1935 mutex_unlock(&smi_infos_lock);
1936 }
1937 }
1d5636cc 1938 rv = len;
b361e27b
CM
1939 out:
1940 kfree(str);
1941 return rv;
1942}
b0defcdb 1943
2223cbec 1944static int hardcode_find_bmc(void)
1da177e4 1945{
a1e9c9dd 1946 int ret = -ENODEV;
b0defcdb 1947 int i;
1da177e4
LT
1948 struct smi_info *info;
1949
b0defcdb
CM
1950 for (i = 0; i < SI_MAX_PARMS; i++) {
1951 if (!ports[i] && !addrs[i])
1952 continue;
1da177e4 1953
de5e2ddf 1954 info = smi_info_alloc();
b0defcdb 1955 if (!info)
a1e9c9dd 1956 return -ENOMEM;
1da177e4 1957
5fedc4a2 1958 info->addr_source = SI_HARDCODED;
279fbd0c 1959 printk(KERN_INFO PFX "probing via hardcoded address\n");
1da177e4 1960
1d5636cc 1961 if (!si_type[i] || strcmp(si_type[i], "kcs") == 0) {
b0defcdb 1962 info->si_type = SI_KCS;
1d5636cc 1963 } else if (strcmp(si_type[i], "smic") == 0) {
b0defcdb 1964 info->si_type = SI_SMIC;
1d5636cc 1965 } else if (strcmp(si_type[i], "bt") == 0) {
b0defcdb
CM
1966 info->si_type = SI_BT;
1967 } else {
279fbd0c 1968 printk(KERN_WARNING PFX "Interface type specified "
b0defcdb
CM
1969 "for interface %d, was invalid: %s\n",
1970 i, si_type[i]);
1971 kfree(info);
1972 continue;
1973 }
1da177e4 1974
b0defcdb
CM
1975 if (ports[i]) {
1976 /* An I/O port */
1977 info->io_setup = port_setup;
1978 info->io.addr_data = ports[i];
1979 info->io.addr_type = IPMI_IO_ADDR_SPACE;
1980 } else if (addrs[i]) {
1981 /* A memory port */
1982 info->io_setup = mem_setup;
1983 info->io.addr_data = addrs[i];
1984 info->io.addr_type = IPMI_MEM_ADDR_SPACE;
1985 } else {
279fbd0c
MS
1986 printk(KERN_WARNING PFX "Interface type specified "
1987 "for interface %d, but port and address were "
1988 "not set or set to zero.\n", i);
b0defcdb
CM
1989 kfree(info);
1990 continue;
1991 }
1da177e4 1992
b0defcdb
CM
1993 info->io.addr = NULL;
1994 info->io.regspacing = regspacings[i];
1995 if (!info->io.regspacing)
1996 info->io.regspacing = DEFAULT_REGSPACING;
1997 info->io.regsize = regsizes[i];
1998 if (!info->io.regsize)
1999 info->io.regsize = DEFAULT_REGSPACING;
2000 info->io.regshift = regshifts[i];
2001 info->irq = irqs[i];
2002 if (info->irq)
2003 info->irq_setup = std_irq_setup;
2f95d513 2004 info->slave_addr = slave_addrs[i];
1da177e4 2005
7faefea6 2006 if (!add_smi(info)) {
2407d77a
MG
2007 if (try_smi_init(info))
2008 cleanup_one_si(info);
a1e9c9dd 2009 ret = 0;
7faefea6
YL
2010 } else {
2011 kfree(info);
2012 }
b0defcdb 2013 }
a1e9c9dd 2014 return ret;
b0defcdb 2015}
1da177e4 2016
8466361a 2017#ifdef CONFIG_ACPI
1da177e4
LT
2018
2019#include <linux/acpi.h>
2020
c305e3d3
CM
2021/*
2022 * Once we get an ACPI failure, we don't try any more, because we go
2023 * through the tables sequentially. Once we don't find a table, there
2024 * are no more.
2025 */
0c8204b3 2026static int acpi_failure;
1da177e4
LT
2027
2028/* For GPE-type interrupts. */
8b6cd8ad
LM
2029static u32 ipmi_acpi_gpe(acpi_handle gpe_device,
2030 u32 gpe_number, void *context)
1da177e4
LT
2031{
2032 struct smi_info *smi_info = context;
2033 unsigned long flags;
1da177e4
LT
2034
2035 spin_lock_irqsave(&(smi_info->si_lock), flags);
2036
64959e2d 2037 smi_inc_stat(smi_info, interrupts);
1da177e4 2038
f93aae9f
JS
2039 debug_timestamp("ACPI_GPE");
2040
1da177e4 2041 smi_event_handler(smi_info, 0);
1da177e4
LT
2042 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
2043
2044 return ACPI_INTERRUPT_HANDLED;
2045}
2046
b0defcdb
CM
2047static void acpi_gpe_irq_cleanup(struct smi_info *info)
2048{
2049 if (!info->irq)
2050 return;
2051
2052 acpi_remove_gpe_handler(NULL, info->irq, &ipmi_acpi_gpe);
2053}
2054
1da177e4
LT
2055static int acpi_gpe_irq_setup(struct smi_info *info)
2056{
2057 acpi_status status;
2058
b0defcdb 2059 if (!info->irq)
1da177e4
LT
2060 return 0;
2061
1da177e4
LT
2062 status = acpi_install_gpe_handler(NULL,
2063 info->irq,
2064 ACPI_GPE_LEVEL_TRIGGERED,
2065 &ipmi_acpi_gpe,
2066 info);
2067 if (status != AE_OK) {
279fbd0c
MS
2068 dev_warn(info->dev, "%s unable to claim ACPI GPE %d,"
2069 " running polled\n", DEVICE_NAME, info->irq);
1da177e4
LT
2070 info->irq = 0;
2071 return -EINVAL;
2072 } else {
b0defcdb 2073 info->irq_cleanup = acpi_gpe_irq_cleanup;
279fbd0c 2074 dev_info(info->dev, "Using ACPI GPE %d\n", info->irq);
1da177e4
LT
2075 return 0;
2076 }
2077}
2078
1da177e4
LT
2079/*
2080 * Defined at
631dd1a8 2081 * http://h21007.www2.hp.com/portal/download/files/unprot/hpspmi.pdf
1da177e4
LT
2082 */
2083struct SPMITable {
2084 s8 Signature[4];
2085 u32 Length;
2086 u8 Revision;
2087 u8 Checksum;
2088 s8 OEMID[6];
2089 s8 OEMTableID[8];
2090 s8 OEMRevision[4];
2091 s8 CreatorID[4];
2092 s8 CreatorRevision[4];
2093 u8 InterfaceType;
2094 u8 IPMIlegacy;
2095 s16 SpecificationRevision;
2096
2097 /*
2098 * Bit 0 - SCI interrupt supported
2099 * Bit 1 - I/O APIC/SAPIC
2100 */
2101 u8 InterruptType;
2102
c305e3d3
CM
2103 /*
2104 * If bit 0 of InterruptType is set, then this is the SCI
2105 * interrupt in the GPEx_STS register.
2106 */
1da177e4
LT
2107 u8 GPE;
2108
2109 s16 Reserved;
2110
c305e3d3
CM
2111 /*
2112 * If bit 1 of InterruptType is set, then this is the I/O
2113 * APIC/SAPIC interrupt.
2114 */
1da177e4
LT
2115 u32 GlobalSystemInterrupt;
2116
2117 /* The actual register address. */
2118 struct acpi_generic_address addr;
2119
2120 u8 UID[4];
2121
2122 s8 spmi_id[1]; /* A '\0' terminated array starts here. */
2123};
2124
2223cbec 2125static int try_init_spmi(struct SPMITable *spmi)
1da177e4
LT
2126{
2127 struct smi_info *info;
d02b3709 2128 int rv;
1da177e4 2129
1da177e4 2130 if (spmi->IPMIlegacy != 1) {
279fbd0c
MS
2131 printk(KERN_INFO PFX "Bad SPMI legacy %d\n", spmi->IPMIlegacy);
2132 return -ENODEV;
1da177e4
LT
2133 }
2134
de5e2ddf 2135 info = smi_info_alloc();
b0defcdb 2136 if (!info) {
279fbd0c 2137 printk(KERN_ERR PFX "Could not allocate SI data (3)\n");
b0defcdb
CM
2138 return -ENOMEM;
2139 }
2140
5fedc4a2 2141 info->addr_source = SI_SPMI;
279fbd0c 2142 printk(KERN_INFO PFX "probing via SPMI\n");
1da177e4 2143
1da177e4 2144 /* Figure out the interface type. */
c305e3d3 2145 switch (spmi->InterfaceType) {
1da177e4 2146 case 1: /* KCS */
b0defcdb 2147 info->si_type = SI_KCS;
1da177e4 2148 break;
1da177e4 2149 case 2: /* SMIC */
b0defcdb 2150 info->si_type = SI_SMIC;
1da177e4 2151 break;
1da177e4 2152 case 3: /* BT */
b0defcdb 2153 info->si_type = SI_BT;
1da177e4 2154 break;
ab42bf24
CM
2155 case 4: /* SSIF, just ignore */
2156 kfree(info);
2157 return -EIO;
1da177e4 2158 default:
279fbd0c
MS
2159 printk(KERN_INFO PFX "Unknown ACPI/SPMI SI type %d\n",
2160 spmi->InterfaceType);
b0defcdb 2161 kfree(info);
1da177e4
LT
2162 return -EIO;
2163 }
2164
1da177e4
LT
2165 if (spmi->InterruptType & 1) {
2166 /* We've got a GPE interrupt. */
2167 info->irq = spmi->GPE;
2168 info->irq_setup = acpi_gpe_irq_setup;
1da177e4
LT
2169 } else if (spmi->InterruptType & 2) {
2170 /* We've got an APIC/SAPIC interrupt. */
2171 info->irq = spmi->GlobalSystemInterrupt;
2172 info->irq_setup = std_irq_setup;
1da177e4
LT
2173 } else {
2174 /* Use the default interrupt setting. */
2175 info->irq = 0;
2176 info->irq_setup = NULL;
2177 }
2178
15a58ed1 2179 if (spmi->addr.bit_width) {
35bc37a0 2180 /* A (hopefully) properly formed register bit width. */
15a58ed1 2181 info->io.regspacing = spmi->addr.bit_width / 8;
35bc37a0 2182 } else {
35bc37a0
CM
2183 info->io.regspacing = DEFAULT_REGSPACING;
2184 }
b0defcdb 2185 info->io.regsize = info->io.regspacing;
15a58ed1 2186 info->io.regshift = spmi->addr.bit_offset;
1da177e4 2187
15a58ed1 2188 if (spmi->addr.space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
1da177e4 2189 info->io_setup = mem_setup;
8fe1425a 2190 info->io.addr_type = IPMI_MEM_ADDR_SPACE;
15a58ed1 2191 } else if (spmi->addr.space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
1da177e4 2192 info->io_setup = port_setup;
8fe1425a 2193 info->io.addr_type = IPMI_IO_ADDR_SPACE;
1da177e4
LT
2194 } else {
2195 kfree(info);
279fbd0c 2196 printk(KERN_WARNING PFX "Unknown ACPI I/O Address type\n");
1da177e4
LT
2197 return -EIO;
2198 }
b0defcdb 2199 info->io.addr_data = spmi->addr.address;
1da177e4 2200
7bb671e3
YL
2201 pr_info("ipmi_si: SPMI: %s %#lx regsize %d spacing %d irq %d\n",
2202 (info->io.addr_type == IPMI_IO_ADDR_SPACE) ? "io" : "mem",
2203 info->io.addr_data, info->io.regsize, info->io.regspacing,
2204 info->irq);
2205
d02b3709
CM
2206 rv = add_smi(info);
2207 if (rv)
7faefea6 2208 kfree(info);
1da177e4 2209
d02b3709 2210 return rv;
1da177e4 2211}
b0defcdb 2212
2223cbec 2213static void spmi_find_bmc(void)
b0defcdb
CM
2214{
2215 acpi_status status;
2216 struct SPMITable *spmi;
2217 int i;
2218
2219 if (acpi_disabled)
2220 return;
2221
2222 if (acpi_failure)
2223 return;
2224
2225 for (i = 0; ; i++) {
15a58ed1
AS
2226 status = acpi_get_table(ACPI_SIG_SPMI, i+1,
2227 (struct acpi_table_header **)&spmi);
b0defcdb
CM
2228 if (status != AE_OK)
2229 return;
2230
18a3e0bf 2231 try_init_spmi(spmi);
b0defcdb
CM
2232 }
2233}
9e368fa0 2234
2223cbec 2235static int ipmi_pnp_probe(struct pnp_dev *dev,
9e368fa0
BH
2236 const struct pnp_device_id *dev_id)
2237{
2238 struct acpi_device *acpi_dev;
2239 struct smi_info *info;
a9e31765 2240 struct resource *res, *res_second;
9e368fa0
BH
2241 acpi_handle handle;
2242 acpi_status status;
2243 unsigned long long tmp;
d02b3709 2244 int rv;
9e368fa0
BH
2245
2246 acpi_dev = pnp_acpi_device(dev);
2247 if (!acpi_dev)
2248 return -ENODEV;
2249
de5e2ddf 2250 info = smi_info_alloc();
9e368fa0
BH
2251 if (!info)
2252 return -ENOMEM;
2253
5fedc4a2 2254 info->addr_source = SI_ACPI;
279fbd0c 2255 printk(KERN_INFO PFX "probing via ACPI\n");
9e368fa0
BH
2256
2257 handle = acpi_dev->handle;
16f4232c 2258 info->addr_info.acpi_info.acpi_handle = handle;
9e368fa0
BH
2259
2260 /* _IFT tells us the interface type: KCS, BT, etc */
2261 status = acpi_evaluate_integer(handle, "_IFT", NULL, &tmp);
2262 if (ACPI_FAILURE(status))
2263 goto err_free;
2264
2265 switch (tmp) {
2266 case 1:
2267 info->si_type = SI_KCS;
2268 break;
2269 case 2:
2270 info->si_type = SI_SMIC;
2271 break;
2272 case 3:
2273 info->si_type = SI_BT;
2274 break;
ab42bf24
CM
2275 case 4: /* SSIF, just ignore */
2276 goto err_free;
9e368fa0 2277 default:
279fbd0c 2278 dev_info(&dev->dev, "unknown IPMI type %lld\n", tmp);
9e368fa0
BH
2279 goto err_free;
2280 }
2281
279fbd0c
MS
2282 res = pnp_get_resource(dev, IORESOURCE_IO, 0);
2283 if (res) {
9e368fa0
BH
2284 info->io_setup = port_setup;
2285 info->io.addr_type = IPMI_IO_ADDR_SPACE;
9e368fa0 2286 } else {
279fbd0c
MS
2287 res = pnp_get_resource(dev, IORESOURCE_MEM, 0);
2288 if (res) {
2289 info->io_setup = mem_setup;
2290 info->io.addr_type = IPMI_MEM_ADDR_SPACE;
2291 }
2292 }
2293 if (!res) {
9e368fa0
BH
2294 dev_err(&dev->dev, "no I/O or memory address\n");
2295 goto err_free;
2296 }
279fbd0c 2297 info->io.addr_data = res->start;
9e368fa0
BH
2298
2299 info->io.regspacing = DEFAULT_REGSPACING;
a9e31765 2300 res_second = pnp_get_resource(dev,
d9e1b6c4
YL
2301 (info->io.addr_type == IPMI_IO_ADDR_SPACE) ?
2302 IORESOURCE_IO : IORESOURCE_MEM,
2303 1);
a9e31765
YL
2304 if (res_second) {
2305 if (res_second->start > info->io.addr_data)
2306 info->io.regspacing = res_second->start - info->io.addr_data;
d9e1b6c4 2307 }
9e368fa0
BH
2308 info->io.regsize = DEFAULT_REGSPACING;
2309 info->io.regshift = 0;
2310
2311 /* If _GPE exists, use it; otherwise use standard interrupts */
2312 status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp);
2313 if (ACPI_SUCCESS(status)) {
2314 info->irq = tmp;
2315 info->irq_setup = acpi_gpe_irq_setup;
2316 } else if (pnp_irq_valid(dev, 0)) {
2317 info->irq = pnp_irq(dev, 0);
2318 info->irq_setup = std_irq_setup;
2319 }
2320
8c8eae27 2321 info->dev = &dev->dev;
9e368fa0
BH
2322 pnp_set_drvdata(dev, info);
2323
279fbd0c
MS
2324 dev_info(info->dev, "%pR regsize %d spacing %d irq %d\n",
2325 res, info->io.regsize, info->io.regspacing,
2326 info->irq);
2327
d02b3709
CM
2328 rv = add_smi(info);
2329 if (rv)
2330 kfree(info);
7faefea6 2331
d02b3709 2332 return rv;
9e368fa0
BH
2333
2334err_free:
2335 kfree(info);
2336 return -EINVAL;
2337}
2338
39af33fc 2339static void ipmi_pnp_remove(struct pnp_dev *dev)
9e368fa0
BH
2340{
2341 struct smi_info *info = pnp_get_drvdata(dev);
2342
2343 cleanup_one_si(info);
2344}
2345
2346static const struct pnp_device_id pnp_dev_table[] = {
2347 {"IPI0001", 0},
2348 {"", 0},
2349};
2350
2351static struct pnp_driver ipmi_pnp_driver = {
2352 .name = DEVICE_NAME,
2353 .probe = ipmi_pnp_probe,
bcd2982a 2354 .remove = ipmi_pnp_remove,
9e368fa0
BH
2355 .id_table = pnp_dev_table,
2356};
a798e2d2
JD
2357
2358MODULE_DEVICE_TABLE(pnp, pnp_dev_table);
1da177e4
LT
2359#endif
2360
a9fad4cc 2361#ifdef CONFIG_DMI
c305e3d3 2362struct dmi_ipmi_data {
1da177e4
LT
2363 u8 type;
2364 u8 addr_space;
2365 unsigned long base_addr;
2366 u8 irq;
2367 u8 offset;
2368 u8 slave_addr;
b0defcdb 2369};
1da177e4 2370
2223cbec 2371static int decode_dmi(const struct dmi_header *dm,
b0defcdb 2372 struct dmi_ipmi_data *dmi)
1da177e4 2373{
1855256c 2374 const u8 *data = (const u8 *)dm;
1da177e4
LT
2375 unsigned long base_addr;
2376 u8 reg_spacing;
b224cd3a 2377 u8 len = dm->length;
1da177e4 2378
b0defcdb 2379 dmi->type = data[4];
1da177e4
LT
2380
2381 memcpy(&base_addr, data+8, sizeof(unsigned long));
2382 if (len >= 0x11) {
2383 if (base_addr & 1) {
2384 /* I/O */
2385 base_addr &= 0xFFFE;
b0defcdb 2386 dmi->addr_space = IPMI_IO_ADDR_SPACE;
c305e3d3 2387 } else
1da177e4 2388 /* Memory */
b0defcdb 2389 dmi->addr_space = IPMI_MEM_ADDR_SPACE;
c305e3d3 2390
1da177e4
LT
2391 /* If bit 4 of byte 0x10 is set, then the lsb for the address
2392 is odd. */
b0defcdb 2393 dmi->base_addr = base_addr | ((data[0x10] & 0x10) >> 4);
1da177e4 2394
b0defcdb 2395 dmi->irq = data[0x11];
1da177e4
LT
2396
2397 /* The top two bits of byte 0x10 hold the register spacing. */
b224cd3a 2398 reg_spacing = (data[0x10] & 0xC0) >> 6;
c305e3d3 2399 switch (reg_spacing) {
1da177e4 2400 case 0x00: /* Byte boundaries */
b0defcdb 2401 dmi->offset = 1;
1da177e4
LT
2402 break;
2403 case 0x01: /* 32-bit boundaries */
b0defcdb 2404 dmi->offset = 4;
1da177e4
LT
2405 break;
2406 case 0x02: /* 16-byte boundaries */
b0defcdb 2407 dmi->offset = 16;
1da177e4
LT
2408 break;
2409 default:
2410 /* Some other interface, just ignore it. */
2411 return -EIO;
2412 }
2413 } else {
2414 /* Old DMI spec. */
c305e3d3
CM
2415 /*
2416 * Note that technically, the lower bit of the base
92068801
CM
2417 * address should be 1 if the address is I/O and 0 if
2418 * the address is in memory. So many systems get that
2419 * wrong (and all that I have seen are I/O) so we just
2420 * ignore that bit and assume I/O. Systems that use
c305e3d3
CM
2421 * memory should use the newer spec, anyway.
2422 */
b0defcdb
CM
2423 dmi->base_addr = base_addr & 0xfffe;
2424 dmi->addr_space = IPMI_IO_ADDR_SPACE;
2425 dmi->offset = 1;
1da177e4
LT
2426 }
2427
b0defcdb 2428 dmi->slave_addr = data[6];
1da177e4 2429
b0defcdb 2430 return 0;
1da177e4
LT
2431}
2432
2223cbec 2433static void try_init_dmi(struct dmi_ipmi_data *ipmi_data)
1da177e4 2434{
b0defcdb 2435 struct smi_info *info;
1da177e4 2436
de5e2ddf 2437 info = smi_info_alloc();
b0defcdb 2438 if (!info) {
279fbd0c 2439 printk(KERN_ERR PFX "Could not allocate SI data\n");
b0defcdb 2440 return;
1da177e4 2441 }
1da177e4 2442
5fedc4a2 2443 info->addr_source = SI_SMBIOS;
279fbd0c 2444 printk(KERN_INFO PFX "probing via SMBIOS\n");
1da177e4 2445
e8b33617 2446 switch (ipmi_data->type) {
b0defcdb
CM
2447 case 0x01: /* KCS */
2448 info->si_type = SI_KCS;
2449 break;
2450 case 0x02: /* SMIC */
2451 info->si_type = SI_SMIC;
2452 break;
2453 case 0x03: /* BT */
2454 info->si_type = SI_BT;
2455 break;
2456 default:
80cd6920 2457 kfree(info);
b0defcdb 2458 return;
1da177e4 2459 }
1da177e4 2460
b0defcdb
CM
2461 switch (ipmi_data->addr_space) {
2462 case IPMI_MEM_ADDR_SPACE:
1da177e4 2463 info->io_setup = mem_setup;
b0defcdb
CM
2464 info->io.addr_type = IPMI_MEM_ADDR_SPACE;
2465 break;
2466
2467 case IPMI_IO_ADDR_SPACE:
1da177e4 2468 info->io_setup = port_setup;
b0defcdb
CM
2469 info->io.addr_type = IPMI_IO_ADDR_SPACE;
2470 break;
2471
2472 default:
1da177e4 2473 kfree(info);
279fbd0c 2474 printk(KERN_WARNING PFX "Unknown SMBIOS I/O Address type: %d\n",
b0defcdb
CM
2475 ipmi_data->addr_space);
2476 return;
1da177e4 2477 }
b0defcdb 2478 info->io.addr_data = ipmi_data->base_addr;
1da177e4 2479
b0defcdb
CM
2480 info->io.regspacing = ipmi_data->offset;
2481 if (!info->io.regspacing)
1da177e4
LT
2482 info->io.regspacing = DEFAULT_REGSPACING;
2483 info->io.regsize = DEFAULT_REGSPACING;
b0defcdb 2484 info->io.regshift = 0;
1da177e4
LT
2485
2486 info->slave_addr = ipmi_data->slave_addr;
2487
b0defcdb
CM
2488 info->irq = ipmi_data->irq;
2489 if (info->irq)
2490 info->irq_setup = std_irq_setup;
1da177e4 2491
7bb671e3
YL
2492 pr_info("ipmi_si: SMBIOS: %s %#lx regsize %d spacing %d irq %d\n",
2493 (info->io.addr_type == IPMI_IO_ADDR_SPACE) ? "io" : "mem",
2494 info->io.addr_data, info->io.regsize, info->io.regspacing,
2495 info->irq);
2496
7faefea6
YL
2497 if (add_smi(info))
2498 kfree(info);
b0defcdb 2499}
1da177e4 2500
2223cbec 2501static void dmi_find_bmc(void)
b0defcdb 2502{
1855256c 2503 const struct dmi_device *dev = NULL;
b0defcdb
CM
2504 struct dmi_ipmi_data data;
2505 int rv;
2506
2507 while ((dev = dmi_find_device(DMI_DEV_TYPE_IPMI, NULL, dev))) {
397f4ebf 2508 memset(&data, 0, sizeof(data));
1855256c
JG
2509 rv = decode_dmi((const struct dmi_header *) dev->device_data,
2510 &data);
b0defcdb
CM
2511 if (!rv)
2512 try_init_dmi(&data);
2513 }
1da177e4 2514}
a9fad4cc 2515#endif /* CONFIG_DMI */
1da177e4
LT
2516
2517#ifdef CONFIG_PCI
2518
b0defcdb
CM
2519#define PCI_ERMC_CLASSCODE 0x0C0700
2520#define PCI_ERMC_CLASSCODE_MASK 0xffffff00
2521#define PCI_ERMC_CLASSCODE_TYPE_MASK 0xff
2522#define PCI_ERMC_CLASSCODE_TYPE_SMIC 0x00
2523#define PCI_ERMC_CLASSCODE_TYPE_KCS 0x01
2524#define PCI_ERMC_CLASSCODE_TYPE_BT 0x02
2525
1da177e4
LT
2526#define PCI_HP_VENDOR_ID 0x103C
2527#define PCI_MMC_DEVICE_ID 0x121A
2528#define PCI_MMC_ADDR_CW 0x10
2529
b0defcdb
CM
2530static void ipmi_pci_cleanup(struct smi_info *info)
2531{
2532 struct pci_dev *pdev = info->addr_source_data;
2533
2534 pci_disable_device(pdev);
2535}
1da177e4 2536
2223cbec 2537static int ipmi_pci_probe_regspacing(struct smi_info *info)
a6c16c28
CM
2538{
2539 if (info->si_type == SI_KCS) {
2540 unsigned char status;
2541 int regspacing;
2542
2543 info->io.regsize = DEFAULT_REGSIZE;
2544 info->io.regshift = 0;
2545 info->io_size = 2;
2546 info->handlers = &kcs_smi_handlers;
2547
2548 /* detect 1, 4, 16byte spacing */
2549 for (regspacing = DEFAULT_REGSPACING; regspacing <= 16;) {
2550 info->io.regspacing = regspacing;
2551 if (info->io_setup(info)) {
2552 dev_err(info->dev,
2553 "Could not setup I/O space\n");
2554 return DEFAULT_REGSPACING;
2555 }
2556 /* write invalid cmd */
2557 info->io.outputb(&info->io, 1, 0x10);
2558 /* read status back */
2559 status = info->io.inputb(&info->io, 1);
2560 info->io_cleanup(info);
2561 if (status)
2562 return regspacing;
2563 regspacing *= 4;
2564 }
2565 }
2566 return DEFAULT_REGSPACING;
2567}
2568
2223cbec 2569static int ipmi_pci_probe(struct pci_dev *pdev,
b0defcdb 2570 const struct pci_device_id *ent)
1da177e4 2571{
b0defcdb
CM
2572 int rv;
2573 int class_type = pdev->class & PCI_ERMC_CLASSCODE_TYPE_MASK;
2574 struct smi_info *info;
1da177e4 2575
de5e2ddf 2576 info = smi_info_alloc();
b0defcdb 2577 if (!info)
1cd441f9 2578 return -ENOMEM;
1da177e4 2579
5fedc4a2 2580 info->addr_source = SI_PCI;
279fbd0c 2581 dev_info(&pdev->dev, "probing via PCI");
1da177e4 2582
b0defcdb
CM
2583 switch (class_type) {
2584 case PCI_ERMC_CLASSCODE_TYPE_SMIC:
2585 info->si_type = SI_SMIC;
2586 break;
1da177e4 2587
b0defcdb
CM
2588 case PCI_ERMC_CLASSCODE_TYPE_KCS:
2589 info->si_type = SI_KCS;
2590 break;
2591
2592 case PCI_ERMC_CLASSCODE_TYPE_BT:
2593 info->si_type = SI_BT;
2594 break;
2595
2596 default:
2597 kfree(info);
279fbd0c 2598 dev_info(&pdev->dev, "Unknown IPMI type: %d\n", class_type);
1cd441f9 2599 return -ENOMEM;
1da177e4
LT
2600 }
2601
b0defcdb
CM
2602 rv = pci_enable_device(pdev);
2603 if (rv) {
279fbd0c 2604 dev_err(&pdev->dev, "couldn't enable PCI device\n");
b0defcdb
CM
2605 kfree(info);
2606 return rv;
1da177e4
LT
2607 }
2608
b0defcdb
CM
2609 info->addr_source_cleanup = ipmi_pci_cleanup;
2610 info->addr_source_data = pdev;
1da177e4 2611
b0defcdb
CM
2612 if (pci_resource_flags(pdev, 0) & IORESOURCE_IO) {
2613 info->io_setup = port_setup;
2614 info->io.addr_type = IPMI_IO_ADDR_SPACE;
2615 } else {
2616 info->io_setup = mem_setup;
2617 info->io.addr_type = IPMI_MEM_ADDR_SPACE;
1da177e4 2618 }
b0defcdb 2619 info->io.addr_data = pci_resource_start(pdev, 0);
1da177e4 2620
a6c16c28
CM
2621 info->io.regspacing = ipmi_pci_probe_regspacing(info);
2622 info->io.regsize = DEFAULT_REGSIZE;
b0defcdb 2623 info->io.regshift = 0;
1da177e4 2624
b0defcdb
CM
2625 info->irq = pdev->irq;
2626 if (info->irq)
2627 info->irq_setup = std_irq_setup;
1da177e4 2628
50c812b2 2629 info->dev = &pdev->dev;
fca3b747 2630 pci_set_drvdata(pdev, info);
50c812b2 2631
279fbd0c
MS
2632 dev_info(&pdev->dev, "%pR regsize %d spacing %d irq %d\n",
2633 &pdev->resource[0], info->io.regsize, info->io.regspacing,
2634 info->irq);
2635
d02b3709
CM
2636 rv = add_smi(info);
2637 if (rv) {
7faefea6 2638 kfree(info);
d02b3709
CM
2639 pci_disable_device(pdev);
2640 }
7faefea6 2641
d02b3709 2642 return rv;
b0defcdb 2643}
1da177e4 2644
39af33fc 2645static void ipmi_pci_remove(struct pci_dev *pdev)
b0defcdb 2646{
fca3b747
CM
2647 struct smi_info *info = pci_get_drvdata(pdev);
2648 cleanup_one_si(info);
d02b3709 2649 pci_disable_device(pdev);
b0defcdb 2650}
1da177e4 2651
b0defcdb
CM
2652static struct pci_device_id ipmi_pci_devices[] = {
2653 { PCI_DEVICE(PCI_HP_VENDOR_ID, PCI_MMC_DEVICE_ID) },
248bdd5e
KC
2654 { PCI_DEVICE_CLASS(PCI_ERMC_CLASSCODE, PCI_ERMC_CLASSCODE_MASK) },
2655 { 0, }
b0defcdb
CM
2656};
2657MODULE_DEVICE_TABLE(pci, ipmi_pci_devices);
2658
2659static struct pci_driver ipmi_pci_driver = {
c305e3d3
CM
2660 .name = DEVICE_NAME,
2661 .id_table = ipmi_pci_devices,
2662 .probe = ipmi_pci_probe,
bcd2982a 2663 .remove = ipmi_pci_remove,
b0defcdb
CM
2664};
2665#endif /* CONFIG_PCI */
1da177e4 2666
da2ff527 2667static const struct of_device_id ipmi_match[];
2223cbec 2668static int ipmi_probe(struct platform_device *dev)
dba9b4f6 2669{
a1e9c9dd 2670#ifdef CONFIG_OF
b1608d69 2671 const struct of_device_id *match;
dba9b4f6
CM
2672 struct smi_info *info;
2673 struct resource resource;
da81c3b9 2674 const __be32 *regsize, *regspacing, *regshift;
61c7a080 2675 struct device_node *np = dev->dev.of_node;
dba9b4f6
CM
2676 int ret;
2677 int proplen;
2678
279fbd0c 2679 dev_info(&dev->dev, "probing via device tree\n");
dba9b4f6 2680
b1608d69
GL
2681 match = of_match_device(ipmi_match, &dev->dev);
2682 if (!match)
a1e9c9dd
RH
2683 return -EINVAL;
2684
08dc4169
BH
2685 if (!of_device_is_available(np))
2686 return -EINVAL;
2687
dba9b4f6
CM
2688 ret = of_address_to_resource(np, 0, &resource);
2689 if (ret) {
2690 dev_warn(&dev->dev, PFX "invalid address from OF\n");
2691 return ret;
2692 }
2693
9c25099d 2694 regsize = of_get_property(np, "reg-size", &proplen);
dba9b4f6
CM
2695 if (regsize && proplen != 4) {
2696 dev_warn(&dev->dev, PFX "invalid regsize from OF\n");
2697 return -EINVAL;
2698 }
2699
9c25099d 2700 regspacing = of_get_property(np, "reg-spacing", &proplen);
dba9b4f6
CM
2701 if (regspacing && proplen != 4) {
2702 dev_warn(&dev->dev, PFX "invalid regspacing from OF\n");
2703 return -EINVAL;
2704 }
2705
9c25099d 2706 regshift = of_get_property(np, "reg-shift", &proplen);
dba9b4f6
CM
2707 if (regshift && proplen != 4) {
2708 dev_warn(&dev->dev, PFX "invalid regshift from OF\n");
2709 return -EINVAL;
2710 }
2711
de5e2ddf 2712 info = smi_info_alloc();
dba9b4f6
CM
2713
2714 if (!info) {
2715 dev_err(&dev->dev,
279fbd0c 2716 "could not allocate memory for OF probe\n");
dba9b4f6
CM
2717 return -ENOMEM;
2718 }
2719
b1608d69 2720 info->si_type = (enum si_type) match->data;
5fedc4a2 2721 info->addr_source = SI_DEVICETREE;
dba9b4f6
CM
2722 info->irq_setup = std_irq_setup;
2723
3b7ec117
NC
2724 if (resource.flags & IORESOURCE_IO) {
2725 info->io_setup = port_setup;
2726 info->io.addr_type = IPMI_IO_ADDR_SPACE;
2727 } else {
2728 info->io_setup = mem_setup;
2729 info->io.addr_type = IPMI_MEM_ADDR_SPACE;
2730 }
2731
dba9b4f6
CM
2732 info->io.addr_data = resource.start;
2733
da81c3b9
RH
2734 info->io.regsize = regsize ? be32_to_cpup(regsize) : DEFAULT_REGSIZE;
2735 info->io.regspacing = regspacing ? be32_to_cpup(regspacing) : DEFAULT_REGSPACING;
2736 info->io.regshift = regshift ? be32_to_cpup(regshift) : 0;
dba9b4f6 2737
61c7a080 2738 info->irq = irq_of_parse_and_map(dev->dev.of_node, 0);
dba9b4f6
CM
2739 info->dev = &dev->dev;
2740
279fbd0c 2741 dev_dbg(&dev->dev, "addr 0x%lx regsize %d spacing %d irq %d\n",
dba9b4f6
CM
2742 info->io.addr_data, info->io.regsize, info->io.regspacing,
2743 info->irq);
2744
9de33df4 2745 dev_set_drvdata(&dev->dev, info);
dba9b4f6 2746
d02b3709
CM
2747 ret = add_smi(info);
2748 if (ret) {
7faefea6 2749 kfree(info);
d02b3709 2750 return ret;
7faefea6 2751 }
a1e9c9dd 2752#endif
7faefea6 2753 return 0;
dba9b4f6
CM
2754}
2755
39af33fc 2756static int ipmi_remove(struct platform_device *dev)
dba9b4f6 2757{
a1e9c9dd 2758#ifdef CONFIG_OF
9de33df4 2759 cleanup_one_si(dev_get_drvdata(&dev->dev));
a1e9c9dd 2760#endif
dba9b4f6
CM
2761 return 0;
2762}
2763
da2ff527 2764static const struct of_device_id ipmi_match[] =
dba9b4f6 2765{
c305e3d3
CM
2766 { .type = "ipmi", .compatible = "ipmi-kcs",
2767 .data = (void *)(unsigned long) SI_KCS },
2768 { .type = "ipmi", .compatible = "ipmi-smic",
2769 .data = (void *)(unsigned long) SI_SMIC },
2770 { .type = "ipmi", .compatible = "ipmi-bt",
2771 .data = (void *)(unsigned long) SI_BT },
dba9b4f6
CM
2772 {},
2773};
2774
a1e9c9dd 2775static struct platform_driver ipmi_driver = {
4018294b 2776 .driver = {
a1e9c9dd 2777 .name = DEVICE_NAME,
4018294b
GL
2778 .of_match_table = ipmi_match,
2779 },
a1e9c9dd 2780 .probe = ipmi_probe,
bcd2982a 2781 .remove = ipmi_remove,
dba9b4f6 2782};
dba9b4f6 2783
fdbeb7de
TB
2784#ifdef CONFIG_PARISC
2785static int ipmi_parisc_probe(struct parisc_device *dev)
2786{
2787 struct smi_info *info;
dfa19426 2788 int rv;
fdbeb7de
TB
2789
2790 info = smi_info_alloc();
2791
2792 if (!info) {
2793 dev_err(&dev->dev,
2794 "could not allocate memory for PARISC probe\n");
2795 return -ENOMEM;
2796 }
2797
2798 info->si_type = SI_KCS;
2799 info->addr_source = SI_DEVICETREE;
2800 info->io_setup = mem_setup;
2801 info->io.addr_type = IPMI_MEM_ADDR_SPACE;
2802 info->io.addr_data = dev->hpa.start;
2803 info->io.regsize = 1;
2804 info->io.regspacing = 1;
2805 info->io.regshift = 0;
2806 info->irq = 0; /* no interrupt */
2807 info->irq_setup = NULL;
2808 info->dev = &dev->dev;
2809
2810 dev_dbg(&dev->dev, "addr 0x%lx\n", info->io.addr_data);
2811
2812 dev_set_drvdata(&dev->dev, info);
2813
d02b3709
CM
2814 rv = add_smi(info);
2815 if (rv) {
fdbeb7de 2816 kfree(info);
d02b3709 2817 return rv;
fdbeb7de
TB
2818 }
2819
2820 return 0;
2821}
2822
2823static int ipmi_parisc_remove(struct parisc_device *dev)
2824{
2825 cleanup_one_si(dev_get_drvdata(&dev->dev));
2826 return 0;
2827}
2828
2829static struct parisc_device_id ipmi_parisc_tbl[] = {
2830 { HPHW_MC, HVERSION_REV_ANY_ID, 0x004, 0xC0 },
2831 { 0, }
2832};
2833
2834static struct parisc_driver ipmi_parisc_driver = {
2835 .name = "ipmi",
2836 .id_table = ipmi_parisc_tbl,
2837 .probe = ipmi_parisc_probe,
2838 .remove = ipmi_parisc_remove,
2839};
2840#endif /* CONFIG_PARISC */
2841
40112ae7 2842static int wait_for_msg_done(struct smi_info *smi_info)
1da177e4 2843{
50c812b2 2844 enum si_sm_result smi_result;
1da177e4
LT
2845
2846 smi_result = smi_info->handlers->event(smi_info->si_sm, 0);
c305e3d3 2847 for (;;) {
c3e7e791
CM
2848 if (smi_result == SI_SM_CALL_WITH_DELAY ||
2849 smi_result == SI_SM_CALL_WITH_TICK_DELAY) {
da4cd8df 2850 schedule_timeout_uninterruptible(1);
1da177e4 2851 smi_result = smi_info->handlers->event(
e21404dc 2852 smi_info->si_sm, jiffies_to_usecs(1));
c305e3d3 2853 } else if (smi_result == SI_SM_CALL_WITHOUT_DELAY) {
1da177e4
LT
2854 smi_result = smi_info->handlers->event(
2855 smi_info->si_sm, 0);
c305e3d3 2856 } else
1da177e4
LT
2857 break;
2858 }
40112ae7 2859 if (smi_result == SI_SM_HOSED)
c305e3d3
CM
2860 /*
2861 * We couldn't get the state machine to run, so whatever's at
2862 * the port is probably not an IPMI SMI interface.
2863 */
40112ae7
CM
2864 return -ENODEV;
2865
2866 return 0;
2867}
2868
2869static int try_get_dev_id(struct smi_info *smi_info)
2870{
2871 unsigned char msg[2];
2872 unsigned char *resp;
2873 unsigned long resp_len;
2874 int rv = 0;
2875
2876 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
2877 if (!resp)
2878 return -ENOMEM;
2879
2880 /*
2881 * Do a Get Device ID command, since it comes back with some
2882 * useful info.
2883 */
2884 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
2885 msg[1] = IPMI_GET_DEVICE_ID_CMD;
2886 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
2887
2888 rv = wait_for_msg_done(smi_info);
2889 if (rv)
1da177e4 2890 goto out;
1da177e4 2891
1da177e4
LT
2892 resp_len = smi_info->handlers->get_result(smi_info->si_sm,
2893 resp, IPMI_MAX_MSG_LENGTH);
1da177e4 2894
d8c98618
CM
2895 /* Check and record info from the get device id, in case we need it. */
2896 rv = ipmi_demangle_device_id(resp, resp_len, &smi_info->device_id);
1da177e4
LT
2897
2898 out:
2899 kfree(resp);
2900 return rv;
2901}
2902
40112ae7
CM
2903static int try_enable_event_buffer(struct smi_info *smi_info)
2904{
2905 unsigned char msg[3];
2906 unsigned char *resp;
2907 unsigned long resp_len;
2908 int rv = 0;
2909
2910 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
2911 if (!resp)
2912 return -ENOMEM;
2913
2914 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
2915 msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
2916 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
2917
2918 rv = wait_for_msg_done(smi_info);
2919 if (rv) {
279fbd0c
MS
2920 printk(KERN_WARNING PFX "Error getting response from get"
2921 " global enables command, the event buffer is not"
40112ae7
CM
2922 " enabled.\n");
2923 goto out;
2924 }
2925
2926 resp_len = smi_info->handlers->get_result(smi_info->si_sm,
2927 resp, IPMI_MAX_MSG_LENGTH);
2928
2929 if (resp_len < 4 ||
2930 resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
2931 resp[1] != IPMI_GET_BMC_GLOBAL_ENABLES_CMD ||
2932 resp[2] != 0) {
279fbd0c
MS
2933 printk(KERN_WARNING PFX "Invalid return from get global"
2934 " enables command, cannot enable the event buffer.\n");
40112ae7
CM
2935 rv = -EINVAL;
2936 goto out;
2937 }
2938
d9b7e4f7 2939 if (resp[3] & IPMI_BMC_EVT_MSG_BUFF) {
40112ae7 2940 /* buffer is already enabled, nothing to do. */
d9b7e4f7 2941 smi_info->supports_event_msg_buff = true;
40112ae7 2942 goto out;
d9b7e4f7 2943 }
40112ae7
CM
2944
2945 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
2946 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
2947 msg[2] = resp[3] | IPMI_BMC_EVT_MSG_BUFF;
2948 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 3);
2949
2950 rv = wait_for_msg_done(smi_info);
2951 if (rv) {
279fbd0c
MS
2952 printk(KERN_WARNING PFX "Error getting response from set"
2953 " global, enables command, the event buffer is not"
40112ae7
CM
2954 " enabled.\n");
2955 goto out;
2956 }
2957
2958 resp_len = smi_info->handlers->get_result(smi_info->si_sm,
2959 resp, IPMI_MAX_MSG_LENGTH);
2960
2961 if (resp_len < 3 ||
2962 resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
2963 resp[1] != IPMI_SET_BMC_GLOBAL_ENABLES_CMD) {
279fbd0c
MS
2964 printk(KERN_WARNING PFX "Invalid return from get global,"
2965 "enables command, not enable the event buffer.\n");
40112ae7
CM
2966 rv = -EINVAL;
2967 goto out;
2968 }
2969
2970 if (resp[2] != 0)
2971 /*
2972 * An error when setting the event buffer bit means
2973 * that the event buffer is not supported.
2974 */
2975 rv = -ENOENT;
d9b7e4f7
CM
2976 else
2977 smi_info->supports_event_msg_buff = true;
2978
40112ae7
CM
2979 out:
2980 kfree(resp);
2981 return rv;
2982}
2983
07412736 2984static int smi_type_proc_show(struct seq_file *m, void *v)
1da177e4 2985{
07412736 2986 struct smi_info *smi = m->private;
1da177e4 2987
d6c5dc18
JP
2988 seq_printf(m, "%s\n", si_to_str[smi->si_type]);
2989
2990 return seq_has_overflowed(m);
1da177e4
LT
2991}
2992
07412736 2993static int smi_type_proc_open(struct inode *inode, struct file *file)
1da177e4 2994{
d9dda78b 2995 return single_open(file, smi_type_proc_show, PDE_DATA(inode));
07412736
AD
2996}
2997
2998static const struct file_operations smi_type_proc_ops = {
2999 .open = smi_type_proc_open,
3000 .read = seq_read,
3001 .llseek = seq_lseek,
3002 .release = single_release,
3003};
3004
3005static int smi_si_stats_proc_show(struct seq_file *m, void *v)
3006{
3007 struct smi_info *smi = m->private;
1da177e4 3008
07412736 3009 seq_printf(m, "interrupts_enabled: %d\n",
b0defcdb 3010 smi->irq && !smi->interrupt_disabled);
07412736 3011 seq_printf(m, "short_timeouts: %u\n",
64959e2d 3012 smi_get_stat(smi, short_timeouts));
07412736 3013 seq_printf(m, "long_timeouts: %u\n",
64959e2d 3014 smi_get_stat(smi, long_timeouts));
07412736 3015 seq_printf(m, "idles: %u\n",
64959e2d 3016 smi_get_stat(smi, idles));
07412736 3017 seq_printf(m, "interrupts: %u\n",
64959e2d 3018 smi_get_stat(smi, interrupts));
07412736 3019 seq_printf(m, "attentions: %u\n",
64959e2d 3020 smi_get_stat(smi, attentions));
07412736 3021 seq_printf(m, "flag_fetches: %u\n",
64959e2d 3022 smi_get_stat(smi, flag_fetches));
07412736 3023 seq_printf(m, "hosed_count: %u\n",
64959e2d 3024 smi_get_stat(smi, hosed_count));
07412736 3025 seq_printf(m, "complete_transactions: %u\n",
64959e2d 3026 smi_get_stat(smi, complete_transactions));
07412736 3027 seq_printf(m, "events: %u\n",
64959e2d 3028 smi_get_stat(smi, events));
07412736 3029 seq_printf(m, "watchdog_pretimeouts: %u\n",
64959e2d 3030 smi_get_stat(smi, watchdog_pretimeouts));
07412736 3031 seq_printf(m, "incoming_messages: %u\n",
64959e2d 3032 smi_get_stat(smi, incoming_messages));
07412736
AD
3033 return 0;
3034}
1da177e4 3035
07412736
AD
3036static int smi_si_stats_proc_open(struct inode *inode, struct file *file)
3037{
d9dda78b 3038 return single_open(file, smi_si_stats_proc_show, PDE_DATA(inode));
b361e27b
CM
3039}
3040
07412736
AD
3041static const struct file_operations smi_si_stats_proc_ops = {
3042 .open = smi_si_stats_proc_open,
3043 .read = seq_read,
3044 .llseek = seq_lseek,
3045 .release = single_release,
3046};
3047
3048static int smi_params_proc_show(struct seq_file *m, void *v)
b361e27b 3049{
07412736 3050 struct smi_info *smi = m->private;
b361e27b 3051
d6c5dc18
JP
3052 seq_printf(m,
3053 "%s,%s,0x%lx,rsp=%d,rsi=%d,rsh=%d,irq=%d,ipmb=%d\n",
3054 si_to_str[smi->si_type],
3055 addr_space_to_str[smi->io.addr_type],
3056 smi->io.addr_data,
3057 smi->io.regspacing,
3058 smi->io.regsize,
3059 smi->io.regshift,
3060 smi->irq,
3061 smi->slave_addr);
3062
3063 return seq_has_overflowed(m);
1da177e4
LT
3064}
3065
07412736
AD
3066static int smi_params_proc_open(struct inode *inode, struct file *file)
3067{
d9dda78b 3068 return single_open(file, smi_params_proc_show, PDE_DATA(inode));
07412736
AD
3069}
3070
3071static const struct file_operations smi_params_proc_ops = {
3072 .open = smi_params_proc_open,
3073 .read = seq_read,
3074 .llseek = seq_lseek,
3075 .release = single_release,
3076};
3077
3ae0e0f9
CM
3078/*
3079 * oem_data_avail_to_receive_msg_avail
3080 * @info - smi_info structure with msg_flags set
3081 *
3082 * Converts flags from OEM_DATA_AVAIL to RECEIVE_MSG_AVAIL
3083 * Returns 1 indicating need to re-run handle_flags().
3084 */
3085static int oem_data_avail_to_receive_msg_avail(struct smi_info *smi_info)
3086{
e8b33617 3087 smi_info->msg_flags = ((smi_info->msg_flags & ~OEM_DATA_AVAIL) |
c305e3d3 3088 RECEIVE_MSG_AVAIL);
3ae0e0f9
CM
3089 return 1;
3090}
3091
3092/*
3093 * setup_dell_poweredge_oem_data_handler
3094 * @info - smi_info.device_id must be populated
3095 *
3096 * Systems that match, but have firmware version < 1.40 may assert
3097 * OEM0_DATA_AVAIL on their own, without being told via Set Flags that
3098 * it's safe to do so. Such systems will de-assert OEM1_DATA_AVAIL
3099 * upon receipt of IPMI_GET_MSG_CMD, so we should treat these flags
3100 * as RECEIVE_MSG_AVAIL instead.
3101 *
3102 * As Dell has no plans to release IPMI 1.5 firmware that *ever*
3103 * assert the OEM[012] bits, and if it did, the driver would have to
3104 * change to handle that properly, we don't actually check for the
3105 * firmware version.
3106 * Device ID = 0x20 BMC on PowerEdge 8G servers
3107 * Device Revision = 0x80
3108 * Firmware Revision1 = 0x01 BMC version 1.40
3109 * Firmware Revision2 = 0x40 BCD encoded
3110 * IPMI Version = 0x51 IPMI 1.5
3111 * Manufacturer ID = A2 02 00 Dell IANA
3112 *
d5a2b89a
CM
3113 * Additionally, PowerEdge systems with IPMI < 1.5 may also assert
3114 * OEM0_DATA_AVAIL and needs to be treated as RECEIVE_MSG_AVAIL.
3115 *
3ae0e0f9
CM
3116 */
3117#define DELL_POWEREDGE_8G_BMC_DEVICE_ID 0x20
3118#define DELL_POWEREDGE_8G_BMC_DEVICE_REV 0x80
3119#define DELL_POWEREDGE_8G_BMC_IPMI_VERSION 0x51
50c812b2 3120#define DELL_IANA_MFR_ID 0x0002a2
3ae0e0f9
CM
3121static void setup_dell_poweredge_oem_data_handler(struct smi_info *smi_info)
3122{
3123 struct ipmi_device_id *id = &smi_info->device_id;
50c812b2 3124 if (id->manufacturer_id == DELL_IANA_MFR_ID) {
d5a2b89a
CM
3125 if (id->device_id == DELL_POWEREDGE_8G_BMC_DEVICE_ID &&
3126 id->device_revision == DELL_POWEREDGE_8G_BMC_DEVICE_REV &&
50c812b2 3127 id->ipmi_version == DELL_POWEREDGE_8G_BMC_IPMI_VERSION) {
d5a2b89a
CM
3128 smi_info->oem_data_avail_handler =
3129 oem_data_avail_to_receive_msg_avail;
c305e3d3
CM
3130 } else if (ipmi_version_major(id) < 1 ||
3131 (ipmi_version_major(id) == 1 &&
3132 ipmi_version_minor(id) < 5)) {
d5a2b89a
CM
3133 smi_info->oem_data_avail_handler =
3134 oem_data_avail_to_receive_msg_avail;
3135 }
3ae0e0f9
CM
3136 }
3137}
3138
ea94027b
CM
3139#define CANNOT_RETURN_REQUESTED_LENGTH 0xCA
3140static void return_hosed_msg_badsize(struct smi_info *smi_info)
3141{
3142 struct ipmi_smi_msg *msg = smi_info->curr_msg;
3143
25985edc 3144 /* Make it a response */
ea94027b
CM
3145 msg->rsp[0] = msg->data[0] | 4;
3146 msg->rsp[1] = msg->data[1];
3147 msg->rsp[2] = CANNOT_RETURN_REQUESTED_LENGTH;
3148 msg->rsp_size = 3;
3149 smi_info->curr_msg = NULL;
3150 deliver_recv_msg(smi_info, msg);
3151}
3152
3153/*
3154 * dell_poweredge_bt_xaction_handler
3155 * @info - smi_info.device_id must be populated
3156 *
3157 * Dell PowerEdge servers with the BT interface (x6xx and 1750) will
3158 * not respond to a Get SDR command if the length of the data
3159 * requested is exactly 0x3A, which leads to command timeouts and no
3160 * data returned. This intercepts such commands, and causes userspace
3161 * callers to try again with a different-sized buffer, which succeeds.
3162 */
3163
3164#define STORAGE_NETFN 0x0A
3165#define STORAGE_CMD_GET_SDR 0x23
3166static int dell_poweredge_bt_xaction_handler(struct notifier_block *self,
3167 unsigned long unused,
3168 void *in)
3169{
3170 struct smi_info *smi_info = in;
3171 unsigned char *data = smi_info->curr_msg->data;
3172 unsigned int size = smi_info->curr_msg->data_size;
3173 if (size >= 8 &&
3174 (data[0]>>2) == STORAGE_NETFN &&
3175 data[1] == STORAGE_CMD_GET_SDR &&
3176 data[7] == 0x3A) {
3177 return_hosed_msg_badsize(smi_info);
3178 return NOTIFY_STOP;
3179 }
3180 return NOTIFY_DONE;
3181}
3182
3183static struct notifier_block dell_poweredge_bt_xaction_notifier = {
3184 .notifier_call = dell_poweredge_bt_xaction_handler,
3185};
3186
3187/*
3188 * setup_dell_poweredge_bt_xaction_handler
3189 * @info - smi_info.device_id must be filled in already
3190 *
3191 * Fills in smi_info.device_id.start_transaction_pre_hook
3192 * when we know what function to use there.
3193 */
3194static void
3195setup_dell_poweredge_bt_xaction_handler(struct smi_info *smi_info)
3196{
3197 struct ipmi_device_id *id = &smi_info->device_id;
50c812b2 3198 if (id->manufacturer_id == DELL_IANA_MFR_ID &&
ea94027b
CM
3199 smi_info->si_type == SI_BT)
3200 register_xaction_notifier(&dell_poweredge_bt_xaction_notifier);
3201}
3202
3ae0e0f9
CM
3203/*
3204 * setup_oem_data_handler
3205 * @info - smi_info.device_id must be filled in already
3206 *
3207 * Fills in smi_info.device_id.oem_data_available_handler
3208 * when we know what function to use there.
3209 */
3210
3211static void setup_oem_data_handler(struct smi_info *smi_info)
3212{
3213 setup_dell_poweredge_oem_data_handler(smi_info);
3214}
3215
ea94027b
CM
3216static void setup_xaction_handlers(struct smi_info *smi_info)
3217{
3218 setup_dell_poweredge_bt_xaction_handler(smi_info);
3219}
3220
a9a2c44f
CM
3221static inline void wait_for_timer_and_thread(struct smi_info *smi_info)
3222{
b874b985
CM
3223 if (smi_info->thread != NULL)
3224 kthread_stop(smi_info->thread);
3225 if (smi_info->timer_running)
453823ba 3226 del_timer_sync(&smi_info->si_timer);
a9a2c44f
CM
3227}
3228
0bbed20e 3229static struct ipmi_default_vals
b0defcdb
CM
3230{
3231 int type;
3232 int port;
7420884c 3233} ipmi_defaults[] =
b0defcdb
CM
3234{
3235 { .type = SI_KCS, .port = 0xca2 },
3236 { .type = SI_SMIC, .port = 0xca9 },
3237 { .type = SI_BT, .port = 0xe4 },
3238 { .port = 0 }
3239};
3240
2223cbec 3241static void default_find_bmc(void)
b0defcdb
CM
3242{
3243 struct smi_info *info;
3244 int i;
3245
3246 for (i = 0; ; i++) {
3247 if (!ipmi_defaults[i].port)
3248 break;
68e1ee62 3249#ifdef CONFIG_PPC
4ff31d77
CK
3250 if (check_legacy_ioport(ipmi_defaults[i].port))
3251 continue;
3252#endif
de5e2ddf 3253 info = smi_info_alloc();
a09f4855
AM
3254 if (!info)
3255 return;
4ff31d77 3256
5fedc4a2 3257 info->addr_source = SI_DEFAULT;
b0defcdb
CM
3258
3259 info->si_type = ipmi_defaults[i].type;
3260 info->io_setup = port_setup;
3261 info->io.addr_data = ipmi_defaults[i].port;
3262 info->io.addr_type = IPMI_IO_ADDR_SPACE;
3263
3264 info->io.addr = NULL;
3265 info->io.regspacing = DEFAULT_REGSPACING;
3266 info->io.regsize = DEFAULT_REGSPACING;
3267 info->io.regshift = 0;
3268
2407d77a
MG
3269 if (add_smi(info) == 0) {
3270 if ((try_smi_init(info)) == 0) {
3271 /* Found one... */
279fbd0c 3272 printk(KERN_INFO PFX "Found default %s"
2407d77a
MG
3273 " state machine at %s address 0x%lx\n",
3274 si_to_str[info->si_type],
3275 addr_space_to_str[info->io.addr_type],
3276 info->io.addr_data);
3277 } else
3278 cleanup_one_si(info);
7faefea6
YL
3279 } else {
3280 kfree(info);
b0defcdb
CM
3281 }
3282 }
3283}
3284
3285static int is_new_interface(struct smi_info *info)
1da177e4 3286{
b0defcdb 3287 struct smi_info *e;
1da177e4 3288
b0defcdb
CM
3289 list_for_each_entry(e, &smi_infos, link) {
3290 if (e->io.addr_type != info->io.addr_type)
3291 continue;
3292 if (e->io.addr_data == info->io.addr_data)
3293 return 0;
3294 }
1da177e4 3295
b0defcdb
CM
3296 return 1;
3297}
1da177e4 3298
2407d77a 3299static int add_smi(struct smi_info *new_smi)
b0defcdb 3300{
2407d77a 3301 int rv = 0;
b0defcdb 3302
279fbd0c 3303 printk(KERN_INFO PFX "Adding %s-specified %s state machine",
7e50387b
CM
3304 ipmi_addr_src_to_str(new_smi->addr_source),
3305 si_to_str[new_smi->si_type]);
d6dfd131 3306 mutex_lock(&smi_infos_lock);
b0defcdb 3307 if (!is_new_interface(new_smi)) {
7bb671e3 3308 printk(KERN_CONT " duplicate interface\n");
b0defcdb
CM
3309 rv = -EBUSY;
3310 goto out_err;
3311 }
1da177e4 3312
2407d77a
MG
3313 printk(KERN_CONT "\n");
3314
1da177e4
LT
3315 /* So we know not to free it unless we have allocated one. */
3316 new_smi->intf = NULL;
3317 new_smi->si_sm = NULL;
3318 new_smi->handlers = NULL;
3319
2407d77a
MG
3320 list_add_tail(&new_smi->link, &smi_infos);
3321
3322out_err:
3323 mutex_unlock(&smi_infos_lock);
3324 return rv;
3325}
3326
3327static int try_smi_init(struct smi_info *new_smi)
3328{
3329 int rv = 0;
3330 int i;
3331
279fbd0c 3332 printk(KERN_INFO PFX "Trying %s-specified %s state"
2407d77a
MG
3333 " machine at %s address 0x%lx, slave address 0x%x,"
3334 " irq %d\n",
7e50387b 3335 ipmi_addr_src_to_str(new_smi->addr_source),
2407d77a
MG
3336 si_to_str[new_smi->si_type],
3337 addr_space_to_str[new_smi->io.addr_type],
3338 new_smi->io.addr_data,
3339 new_smi->slave_addr, new_smi->irq);
3340
b0defcdb
CM
3341 switch (new_smi->si_type) {
3342 case SI_KCS:
1da177e4 3343 new_smi->handlers = &kcs_smi_handlers;
b0defcdb
CM
3344 break;
3345
3346 case SI_SMIC:
1da177e4 3347 new_smi->handlers = &smic_smi_handlers;
b0defcdb
CM
3348 break;
3349
3350 case SI_BT:
1da177e4 3351 new_smi->handlers = &bt_smi_handlers;
b0defcdb
CM
3352 break;
3353
3354 default:
1da177e4
LT
3355 /* No support for anything else yet. */
3356 rv = -EIO;
3357 goto out_err;
3358 }
3359
3360 /* Allocate the state machine's data and initialize it. */
3361 new_smi->si_sm = kmalloc(new_smi->handlers->size(), GFP_KERNEL);
b0defcdb 3362 if (!new_smi->si_sm) {
279fbd0c
MS
3363 printk(KERN_ERR PFX
3364 "Could not allocate state machine memory\n");
1da177e4
LT
3365 rv = -ENOMEM;
3366 goto out_err;
3367 }
3368 new_smi->io_size = new_smi->handlers->init_data(new_smi->si_sm,
3369 &new_smi->io);
3370
3371 /* Now that we know the I/O size, we can set up the I/O. */
3372 rv = new_smi->io_setup(new_smi);
3373 if (rv) {
279fbd0c 3374 printk(KERN_ERR PFX "Could not set up I/O space\n");
1da177e4
LT
3375 goto out_err;
3376 }
3377
1da177e4
LT
3378 /* Do low-level detection first. */
3379 if (new_smi->handlers->detect(new_smi->si_sm)) {
b0defcdb 3380 if (new_smi->addr_source)
279fbd0c 3381 printk(KERN_INFO PFX "Interface detection failed\n");
1da177e4
LT
3382 rv = -ENODEV;
3383 goto out_err;
3384 }
3385
c305e3d3
CM
3386 /*
3387 * Attempt a get device id command. If it fails, we probably
3388 * don't have a BMC here.
3389 */
1da177e4 3390 rv = try_get_dev_id(new_smi);
b0defcdb
CM
3391 if (rv) {
3392 if (new_smi->addr_source)
279fbd0c 3393 printk(KERN_INFO PFX "There appears to be no BMC"
b0defcdb 3394 " at this location\n");
1da177e4 3395 goto out_err;
b0defcdb 3396 }
1da177e4 3397
3ae0e0f9 3398 setup_oem_data_handler(new_smi);
ea94027b 3399 setup_xaction_handlers(new_smi);
3ae0e0f9 3400
b874b985 3401 new_smi->waiting_msg = NULL;
1da177e4
LT
3402 new_smi->curr_msg = NULL;
3403 atomic_set(&new_smi->req_events, 0);
7aefac26 3404 new_smi->run_to_completion = false;
64959e2d
CM
3405 for (i = 0; i < SI_NUM_STATS; i++)
3406 atomic_set(&new_smi->stats[i], 0);
1da177e4 3407
7aefac26 3408 new_smi->interrupt_disabled = true;
89986496 3409 atomic_set(&new_smi->need_watch, 0);
b0defcdb
CM
3410 new_smi->intf_num = smi_num;
3411 smi_num++;
1da177e4 3412
40112ae7
CM
3413 rv = try_enable_event_buffer(new_smi);
3414 if (rv == 0)
7aefac26 3415 new_smi->has_event_buffer = true;
40112ae7 3416
c305e3d3
CM
3417 /*
3418 * Start clearing the flags before we enable interrupts or the
3419 * timer to avoid racing with the timer.
3420 */
1da177e4 3421 start_clear_flags(new_smi);
d9b7e4f7
CM
3422
3423 /*
3424 * IRQ is defined to be set when non-zero. req_events will
3425 * cause a global flags check that will enable interrupts.
3426 */
3427 if (new_smi->irq) {
3428 new_smi->interrupt_disabled = false;
3429 atomic_set(&new_smi->req_events, 1);
3430 }
1da177e4 3431
50c812b2 3432 if (!new_smi->dev) {
c305e3d3
CM
3433 /*
3434 * If we don't already have a device from something
3435 * else (like PCI), then register a new one.
3436 */
50c812b2
CM
3437 new_smi->pdev = platform_device_alloc("ipmi_si",
3438 new_smi->intf_num);
8b32b5d0 3439 if (!new_smi->pdev) {
279fbd0c
MS
3440 printk(KERN_ERR PFX
3441 "Unable to allocate platform device\n");
453823ba 3442 goto out_err;
50c812b2
CM
3443 }
3444 new_smi->dev = &new_smi->pdev->dev;
fe2d5ffc 3445 new_smi->dev->driver = &ipmi_driver.driver;
50c812b2 3446
b48f5457 3447 rv = platform_device_add(new_smi->pdev);
50c812b2 3448 if (rv) {
279fbd0c
MS
3449 printk(KERN_ERR PFX
3450 "Unable to register system interface device:"
50c812b2
CM
3451 " %d\n",
3452 rv);
453823ba 3453 goto out_err;
50c812b2 3454 }
7aefac26 3455 new_smi->dev_registered = true;
50c812b2
CM
3456 }
3457
1da177e4
LT
3458 rv = ipmi_register_smi(&handlers,
3459 new_smi,
50c812b2
CM
3460 &new_smi->device_id,
3461 new_smi->dev,
453823ba 3462 new_smi->slave_addr);
1da177e4 3463 if (rv) {
279fbd0c
MS
3464 dev_err(new_smi->dev, "Unable to register device: error %d\n",
3465 rv);
1da177e4
LT
3466 goto out_err_stop_timer;
3467 }
3468
3469 rv = ipmi_smi_add_proc_entry(new_smi->intf, "type",
07412736 3470 &smi_type_proc_ops,
99b76233 3471 new_smi);
1da177e4 3472 if (rv) {
279fbd0c 3473 dev_err(new_smi->dev, "Unable to create proc entry: %d\n", rv);
1da177e4
LT
3474 goto out_err_stop_timer;
3475 }
3476
3477 rv = ipmi_smi_add_proc_entry(new_smi->intf, "si_stats",
07412736 3478 &smi_si_stats_proc_ops,
99b76233 3479 new_smi);
1da177e4 3480 if (rv) {
279fbd0c 3481 dev_err(new_smi->dev, "Unable to create proc entry: %d\n", rv);
1da177e4
LT
3482 goto out_err_stop_timer;
3483 }
3484
b361e27b 3485 rv = ipmi_smi_add_proc_entry(new_smi->intf, "params",
07412736 3486 &smi_params_proc_ops,
99b76233 3487 new_smi);
b361e27b 3488 if (rv) {
279fbd0c 3489 dev_err(new_smi->dev, "Unable to create proc entry: %d\n", rv);
b361e27b
CM
3490 goto out_err_stop_timer;
3491 }
3492
279fbd0c
MS
3493 dev_info(new_smi->dev, "IPMI %s interface initialized\n",
3494 si_to_str[new_smi->si_type]);
1da177e4
LT
3495
3496 return 0;
3497
3498 out_err_stop_timer:
a9a2c44f 3499 wait_for_timer_and_thread(new_smi);
1da177e4
LT
3500
3501 out_err:
7aefac26 3502 new_smi->interrupt_disabled = true;
2407d77a
MG
3503
3504 if (new_smi->intf) {
b874b985 3505 ipmi_smi_t intf = new_smi->intf;
2407d77a 3506 new_smi->intf = NULL;
b874b985 3507 ipmi_unregister_smi(intf);
2407d77a 3508 }
1da177e4 3509
2407d77a 3510 if (new_smi->irq_cleanup) {
b0defcdb 3511 new_smi->irq_cleanup(new_smi);
2407d77a
MG
3512 new_smi->irq_cleanup = NULL;
3513 }
1da177e4 3514
c305e3d3
CM
3515 /*
3516 * Wait until we know that we are out of any interrupt
3517 * handlers might have been running before we freed the
3518 * interrupt.
3519 */
fbd568a3 3520 synchronize_sched();
1da177e4
LT
3521
3522 if (new_smi->si_sm) {
3523 if (new_smi->handlers)
3524 new_smi->handlers->cleanup(new_smi->si_sm);
3525 kfree(new_smi->si_sm);
2407d77a 3526 new_smi->si_sm = NULL;
1da177e4 3527 }
2407d77a 3528 if (new_smi->addr_source_cleanup) {
b0defcdb 3529 new_smi->addr_source_cleanup(new_smi);
2407d77a
MG
3530 new_smi->addr_source_cleanup = NULL;
3531 }
3532 if (new_smi->io_cleanup) {
7767e126 3533 new_smi->io_cleanup(new_smi);
2407d77a
MG
3534 new_smi->io_cleanup = NULL;
3535 }
1da177e4 3536
2407d77a 3537 if (new_smi->dev_registered) {
50c812b2 3538 platform_device_unregister(new_smi->pdev);
7aefac26 3539 new_smi->dev_registered = false;
2407d77a 3540 }
b0defcdb 3541
1da177e4
LT
3542 return rv;
3543}
3544
2223cbec 3545static int init_ipmi_si(void)
1da177e4 3546{
1da177e4
LT
3547 int i;
3548 char *str;
50c812b2 3549 int rv;
2407d77a 3550 struct smi_info *e;
06ee4594 3551 enum ipmi_addr_src type = SI_INVALID;
1da177e4
LT
3552
3553 if (initialized)
3554 return 0;
3555 initialized = 1;
3556
f2afae46
CM
3557 if (si_tryplatform) {
3558 rv = platform_driver_register(&ipmi_driver);
3559 if (rv) {
3560 printk(KERN_ERR PFX "Unable to register "
3561 "driver: %d\n", rv);
3562 return rv;
3563 }
50c812b2
CM
3564 }
3565
1da177e4
LT
3566 /* Parse out the si_type string into its components. */
3567 str = si_type_str;
3568 if (*str != '\0') {
e8b33617 3569 for (i = 0; (i < SI_MAX_PARMS) && (*str != '\0'); i++) {
1da177e4
LT
3570 si_type[i] = str;
3571 str = strchr(str, ',');
3572 if (str) {
3573 *str = '\0';
3574 str++;
3575 } else {
3576 break;
3577 }
3578 }
3579 }
3580
1fdd75bd 3581 printk(KERN_INFO "IPMI System Interface driver.\n");
1da177e4 3582
d8cc5267 3583 /* If the user gave us a device, they presumably want us to use it */
a1e9c9dd 3584 if (!hardcode_find_bmc())
d8cc5267 3585 return 0;
d8cc5267 3586
b0defcdb 3587#ifdef CONFIG_PCI
f2afae46
CM
3588 if (si_trypci) {
3589 rv = pci_register_driver(&ipmi_pci_driver);
3590 if (rv)
3591 printk(KERN_ERR PFX "Unable to register "
3592 "PCI driver: %d\n", rv);
3593 else
7aefac26 3594 pci_registered = true;
f2afae46 3595 }
b0defcdb
CM
3596#endif
3597
754d4531 3598#ifdef CONFIG_ACPI
d941aeae
CM
3599 if (si_tryacpi) {
3600 pnp_register_driver(&ipmi_pnp_driver);
7aefac26 3601 pnp_registered = true;
d941aeae 3602 }
754d4531
MG
3603#endif
3604
3605#ifdef CONFIG_DMI
d941aeae
CM
3606 if (si_trydmi)
3607 dmi_find_bmc();
754d4531
MG
3608#endif
3609
3610#ifdef CONFIG_ACPI
d941aeae
CM
3611 if (si_tryacpi)
3612 spmi_find_bmc();
754d4531
MG
3613#endif
3614
fdbeb7de
TB
3615#ifdef CONFIG_PARISC
3616 register_parisc_driver(&ipmi_parisc_driver);
7aefac26 3617 parisc_registered = true;
fdbeb7de
TB
3618 /* poking PC IO addresses will crash machine, don't do it */
3619 si_trydefaults = 0;
3620#endif
3621
06ee4594
MG
3622 /* We prefer devices with interrupts, but in the case of a machine
3623 with multiple BMCs we assume that there will be several instances
3624 of a given type so if we succeed in registering a type then also
3625 try to register everything else of the same type */
d8cc5267 3626
2407d77a
MG
3627 mutex_lock(&smi_infos_lock);
3628 list_for_each_entry(e, &smi_infos, link) {
06ee4594
MG
3629 /* Try to register a device if it has an IRQ and we either
3630 haven't successfully registered a device yet or this
3631 device has the same type as one we successfully registered */
3632 if (e->irq && (!type || e->addr_source == type)) {
d8cc5267 3633 if (!try_smi_init(e)) {
06ee4594 3634 type = e->addr_source;
d8cc5267
MG
3635 }
3636 }
3637 }
3638
06ee4594
MG
3639 /* type will only have been set if we successfully registered an si */
3640 if (type) {
3641 mutex_unlock(&smi_infos_lock);
3642 return 0;
3643 }
3644
d8cc5267
MG
3645 /* Fall back to the preferred device */
3646
3647 list_for_each_entry(e, &smi_infos, link) {
06ee4594 3648 if (!e->irq && (!type || e->addr_source == type)) {
d8cc5267 3649 if (!try_smi_init(e)) {
06ee4594 3650 type = e->addr_source;
d8cc5267
MG
3651 }
3652 }
2407d77a
MG
3653 }
3654 mutex_unlock(&smi_infos_lock);
3655
06ee4594
MG
3656 if (type)
3657 return 0;
3658
b0defcdb 3659 if (si_trydefaults) {
d6dfd131 3660 mutex_lock(&smi_infos_lock);
b0defcdb
CM
3661 if (list_empty(&smi_infos)) {
3662 /* No BMC was found, try defaults. */
d6dfd131 3663 mutex_unlock(&smi_infos_lock);
b0defcdb 3664 default_find_bmc();
2407d77a 3665 } else
d6dfd131 3666 mutex_unlock(&smi_infos_lock);
1da177e4
LT
3667 }
3668
d6dfd131 3669 mutex_lock(&smi_infos_lock);
b361e27b 3670 if (unload_when_empty && list_empty(&smi_infos)) {
d6dfd131 3671 mutex_unlock(&smi_infos_lock);
d2478521 3672 cleanup_ipmi_si();
279fbd0c
MS
3673 printk(KERN_WARNING PFX
3674 "Unable to find any System Interface(s)\n");
1da177e4 3675 return -ENODEV;
b0defcdb 3676 } else {
d6dfd131 3677 mutex_unlock(&smi_infos_lock);
b0defcdb 3678 return 0;
1da177e4 3679 }
1da177e4
LT
3680}
3681module_init(init_ipmi_si);
3682
b361e27b 3683static void cleanup_one_si(struct smi_info *to_clean)
1da177e4 3684{
2407d77a 3685 int rv = 0;
1da177e4 3686
b0defcdb 3687 if (!to_clean)
1da177e4
LT
3688 return;
3689
b874b985
CM
3690 if (to_clean->intf) {
3691 ipmi_smi_t intf = to_clean->intf;
3692
3693 to_clean->intf = NULL;
3694 rv = ipmi_unregister_smi(intf);
3695 if (rv) {
3696 pr_err(PFX "Unable to unregister device: errno=%d\n",
3697 rv);
3698 }
3699 }
3700
567eded9
TI
3701 if (to_clean->dev)
3702 dev_set_drvdata(to_clean->dev, NULL);
3703
b0defcdb
CM
3704 list_del(&to_clean->link);
3705
c305e3d3 3706 /*
b874b985
CM
3707 * Make sure that interrupts, the timer and the thread are
3708 * stopped and will not run again.
c305e3d3 3709 */
b874b985
CM
3710 if (to_clean->irq_cleanup)
3711 to_clean->irq_cleanup(to_clean);
a9a2c44f 3712 wait_for_timer_and_thread(to_clean);
1da177e4 3713
c305e3d3
CM
3714 /*
3715 * Timeouts are stopped, now make sure the interrupts are off
b874b985
CM
3716 * in the BMC. Note that timers and CPU interrupts are off,
3717 * so no need for locks.
c305e3d3 3718 */
ee6cd5f8 3719 while (to_clean->curr_msg || (to_clean->si_state != SI_NORMAL)) {
ee6cd5f8
CM
3720 poll(to_clean);
3721 schedule_timeout_uninterruptible(1);
ee6cd5f8
CM
3722 }
3723 disable_si_irq(to_clean);
e8b33617 3724 while (to_clean->curr_msg || (to_clean->si_state != SI_NORMAL)) {
1da177e4 3725 poll(to_clean);
da4cd8df 3726 schedule_timeout_uninterruptible(1);
1da177e4
LT
3727 }
3728
2407d77a
MG
3729 if (to_clean->handlers)
3730 to_clean->handlers->cleanup(to_clean->si_sm);
1da177e4
LT
3731
3732 kfree(to_clean->si_sm);
3733
b0defcdb
CM
3734 if (to_clean->addr_source_cleanup)
3735 to_clean->addr_source_cleanup(to_clean);
7767e126
PG
3736 if (to_clean->io_cleanup)
3737 to_clean->io_cleanup(to_clean);
50c812b2
CM
3738
3739 if (to_clean->dev_registered)
3740 platform_device_unregister(to_clean->pdev);
3741
3742 kfree(to_clean);
1da177e4
LT
3743}
3744
0dcf334c 3745static void cleanup_ipmi_si(void)
1da177e4 3746{
b0defcdb 3747 struct smi_info *e, *tmp_e;
1da177e4 3748
b0defcdb 3749 if (!initialized)
1da177e4
LT
3750 return;
3751
b0defcdb 3752#ifdef CONFIG_PCI
56480287
MG
3753 if (pci_registered)
3754 pci_unregister_driver(&ipmi_pci_driver);
b0defcdb 3755#endif
27d0567a 3756#ifdef CONFIG_ACPI
561f8182
YL
3757 if (pnp_registered)
3758 pnp_unregister_driver(&ipmi_pnp_driver);
9e368fa0 3759#endif
fdbeb7de
TB
3760#ifdef CONFIG_PARISC
3761 if (parisc_registered)
3762 unregister_parisc_driver(&ipmi_parisc_driver);
3763#endif
b0defcdb 3764
a1e9c9dd 3765 platform_driver_unregister(&ipmi_driver);
dba9b4f6 3766
d6dfd131 3767 mutex_lock(&smi_infos_lock);
b0defcdb
CM
3768 list_for_each_entry_safe(e, tmp_e, &smi_infos, link)
3769 cleanup_one_si(e);
d6dfd131 3770 mutex_unlock(&smi_infos_lock);
1da177e4
LT
3771}
3772module_exit(cleanup_ipmi_si);
3773
3774MODULE_LICENSE("GPL");
1fdd75bd 3775MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
c305e3d3
CM
3776MODULE_DESCRIPTION("Interface to the IPMI driver for the KCS, SMIC, and BT"
3777 " system interfaces.");