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