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