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