[TG3]: Set minimal hw interrupt mitigation.
[linux-2.6/verdex.git] / drivers / char / ipmi / ipmi_si_intf.c
blob298574e160613e67191472bf147e141546573957
1 /*
2 * ipmi_si.c
4 * The interface to the IPMI driver for the system interfaces (KCS, SMIC,
5 * BT).
7 * Author: MontaVista Software, Inc.
8 * Corey Minyard <minyard@mvista.com>
9 * source@mvista.com
11 * Copyright 2002 MontaVista Software Inc.
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
25 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
27 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
28 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * You should have received a copy of the GNU General Public License along
31 * with this program; if not, write to the Free Software Foundation, Inc.,
32 * 675 Mass Ave, Cambridge, MA 02139, USA.
36 * This file holds the "policy" for the interface to the SMI state
37 * machine. It does the configuration, handles timers and interrupts,
38 * and drives the real SMI state machine.
41 #include <linux/config.h>
42 #include <linux/module.h>
43 #include <linux/moduleparam.h>
44 #include <asm/system.h>
45 #include <linux/sched.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 <asm/irq.h>
55 #ifdef CONFIG_HIGH_RES_TIMERS
56 #include <linux/hrtime.h>
57 # if defined(schedule_next_int)
58 /* Old high-res timer code, do translations. */
59 # define get_arch_cycles(a) quick_update_jiffies_sub(a)
60 # define arch_cycles_per_jiffy cycles_per_jiffies
61 # endif
62 static inline void add_usec_to_timer(struct timer_list *t, long v)
64 t->sub_expires += nsec_to_arch_cycle(v * 1000);
65 while (t->sub_expires >= arch_cycles_per_jiffy)
67 t->expires++;
68 t->sub_expires -= arch_cycles_per_jiffy;
71 #endif
72 #include <linux/interrupt.h>
73 #include <linux/rcupdate.h>
74 #include <linux/ipmi_smi.h>
75 #include <asm/io.h>
76 #include "ipmi_si_sm.h"
77 #include <linux/init.h>
79 #define IPMI_SI_VERSION "v33"
81 /* Measure times between events in the driver. */
82 #undef DEBUG_TIMING
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 */
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 /* FIXME - add watchdog stuff. */
103 /* Some BT-specific defines we need here. */
104 #define IPMI_BT_INTMASK_REG 2
105 #define IPMI_BT_INTMASK_CLEAR_IRQ_BIT 2
106 #define IPMI_BT_INTMASK_ENABLE_IRQ_BIT 1
108 enum si_type {
109 SI_KCS, SI_SMIC, SI_BT
112 struct smi_info
114 ipmi_smi_t intf;
115 struct si_sm_data *si_sm;
116 struct si_sm_handlers *handlers;
117 enum si_type si_type;
118 spinlock_t si_lock;
119 spinlock_t msg_lock;
120 struct list_head xmit_msgs;
121 struct list_head hp_xmit_msgs;
122 struct ipmi_smi_msg *curr_msg;
123 enum si_intf_state si_state;
125 /* Used to handle the various types of I/O that can occur with
126 IPMI */
127 struct si_sm_io io;
128 int (*io_setup)(struct smi_info *info);
129 void (*io_cleanup)(struct smi_info *info);
130 int (*irq_setup)(struct smi_info *info);
131 void (*irq_cleanup)(struct smi_info *info);
132 unsigned int io_size;
134 /* Flags from the last GET_MSG_FLAGS command, used when an ATTN
135 is set to hold the flags until we are done handling everything
136 from the flags. */
137 #define RECEIVE_MSG_AVAIL 0x01
138 #define EVENT_MSG_BUFFER_FULL 0x02
139 #define WDT_PRE_TIMEOUT_INT 0x08
140 unsigned char msg_flags;
142 /* If set to true, this will request events the next time the
143 state machine is idle. */
144 atomic_t req_events;
146 /* If true, run the state machine to completion on every send
147 call. Generally used after a panic to make sure stuff goes
148 out. */
149 int run_to_completion;
151 /* The I/O port of an SI interface. */
152 int port;
154 /* The space between start addresses of the two ports. For
155 instance, if the first port is 0xca2 and the spacing is 4, then
156 the second port is 0xca6. */
157 unsigned int spacing;
159 /* zero if no irq; */
160 int irq;
162 /* The timer for this si. */
163 struct timer_list si_timer;
165 /* The time (in jiffies) the last timeout occurred at. */
166 unsigned long last_timeout_jiffies;
168 /* Used to gracefully stop the timer without race conditions. */
169 volatile int stop_operation;
170 volatile int timer_stopped;
172 /* The driver will disable interrupts when it gets into a
173 situation where it cannot handle messages due to lack of
174 memory. Once that situation clears up, it will re-enable
175 interrupts. */
176 int interrupt_disabled;
178 unsigned char ipmi_si_dev_rev;
179 unsigned char ipmi_si_fw_rev_major;
180 unsigned char ipmi_si_fw_rev_minor;
181 unsigned char ipmi_version_major;
182 unsigned char ipmi_version_minor;
184 /* Slave address, could be reported from DMI. */
185 unsigned char slave_addr;
187 /* Counters and things for the proc filesystem. */
188 spinlock_t count_lock;
189 unsigned long short_timeouts;
190 unsigned long long_timeouts;
191 unsigned long timeout_restarts;
192 unsigned long idles;
193 unsigned long interrupts;
194 unsigned long attentions;
195 unsigned long flag_fetches;
196 unsigned long hosed_count;
197 unsigned long complete_transactions;
198 unsigned long events;
199 unsigned long watchdog_pretimeouts;
200 unsigned long incoming_messages;
203 static void si_restart_short_timer(struct smi_info *smi_info);
205 static void deliver_recv_msg(struct smi_info *smi_info,
206 struct ipmi_smi_msg *msg)
208 /* Deliver the message to the upper layer with the lock
209 released. */
210 spin_unlock(&(smi_info->si_lock));
211 ipmi_smi_msg_received(smi_info->intf, msg);
212 spin_lock(&(smi_info->si_lock));
215 static void return_hosed_msg(struct smi_info *smi_info)
217 struct ipmi_smi_msg *msg = smi_info->curr_msg;
219 /* Make it a reponse */
220 msg->rsp[0] = msg->data[0] | 4;
221 msg->rsp[1] = msg->data[1];
222 msg->rsp[2] = 0xFF; /* Unknown error. */
223 msg->rsp_size = 3;
225 smi_info->curr_msg = NULL;
226 deliver_recv_msg(smi_info, msg);
229 static enum si_sm_result start_next_msg(struct smi_info *smi_info)
231 int rv;
232 struct list_head *entry = NULL;
233 #ifdef DEBUG_TIMING
234 struct timeval t;
235 #endif
237 /* No need to save flags, we aleady have interrupts off and we
238 already hold the SMI lock. */
239 spin_lock(&(smi_info->msg_lock));
241 /* Pick the high priority queue first. */
242 if (! list_empty(&(smi_info->hp_xmit_msgs))) {
243 entry = smi_info->hp_xmit_msgs.next;
244 } else if (! list_empty(&(smi_info->xmit_msgs))) {
245 entry = smi_info->xmit_msgs.next;
248 if (!entry) {
249 smi_info->curr_msg = NULL;
250 rv = SI_SM_IDLE;
251 } else {
252 int err;
254 list_del(entry);
255 smi_info->curr_msg = list_entry(entry,
256 struct ipmi_smi_msg,
257 link);
258 #ifdef DEBUG_TIMING
259 do_gettimeofday(&t);
260 printk("**Start2: %d.%9.9d\n", t.tv_sec, t.tv_usec);
261 #endif
262 err = smi_info->handlers->start_transaction(
263 smi_info->si_sm,
264 smi_info->curr_msg->data,
265 smi_info->curr_msg->data_size);
266 if (err) {
267 return_hosed_msg(smi_info);
270 rv = SI_SM_CALL_WITHOUT_DELAY;
272 spin_unlock(&(smi_info->msg_lock));
274 return rv;
277 static void start_enable_irq(struct smi_info *smi_info)
279 unsigned char msg[2];
281 /* If we are enabling interrupts, we have to tell the
282 BMC to use them. */
283 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
284 msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
286 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
287 smi_info->si_state = SI_ENABLE_INTERRUPTS1;
290 static void start_clear_flags(struct smi_info *smi_info)
292 unsigned char msg[3];
294 /* Make sure the watchdog pre-timeout flag is not set at startup. */
295 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
296 msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
297 msg[2] = WDT_PRE_TIMEOUT_INT;
299 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 3);
300 smi_info->si_state = SI_CLEARING_FLAGS;
303 /* When we have a situtaion where we run out of memory and cannot
304 allocate messages, we just leave them in the BMC and run the system
305 polled until we can allocate some memory. Once we have some
306 memory, we will re-enable the interrupt. */
307 static inline void disable_si_irq(struct smi_info *smi_info)
309 if ((smi_info->irq) && (!smi_info->interrupt_disabled)) {
310 disable_irq_nosync(smi_info->irq);
311 smi_info->interrupt_disabled = 1;
315 static inline void enable_si_irq(struct smi_info *smi_info)
317 if ((smi_info->irq) && (smi_info->interrupt_disabled)) {
318 enable_irq(smi_info->irq);
319 smi_info->interrupt_disabled = 0;
323 static void handle_flags(struct smi_info *smi_info)
325 if (smi_info->msg_flags & WDT_PRE_TIMEOUT_INT) {
326 /* Watchdog pre-timeout */
327 spin_lock(&smi_info->count_lock);
328 smi_info->watchdog_pretimeouts++;
329 spin_unlock(&smi_info->count_lock);
331 start_clear_flags(smi_info);
332 smi_info->msg_flags &= ~WDT_PRE_TIMEOUT_INT;
333 spin_unlock(&(smi_info->si_lock));
334 ipmi_smi_watchdog_pretimeout(smi_info->intf);
335 spin_lock(&(smi_info->si_lock));
336 } else if (smi_info->msg_flags & RECEIVE_MSG_AVAIL) {
337 /* Messages available. */
338 smi_info->curr_msg = ipmi_alloc_smi_msg();
339 if (!smi_info->curr_msg) {
340 disable_si_irq(smi_info);
341 smi_info->si_state = SI_NORMAL;
342 return;
344 enable_si_irq(smi_info);
346 smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
347 smi_info->curr_msg->data[1] = IPMI_GET_MSG_CMD;
348 smi_info->curr_msg->data_size = 2;
350 smi_info->handlers->start_transaction(
351 smi_info->si_sm,
352 smi_info->curr_msg->data,
353 smi_info->curr_msg->data_size);
354 smi_info->si_state = SI_GETTING_MESSAGES;
355 } else if (smi_info->msg_flags & EVENT_MSG_BUFFER_FULL) {
356 /* Events available. */
357 smi_info->curr_msg = ipmi_alloc_smi_msg();
358 if (!smi_info->curr_msg) {
359 disable_si_irq(smi_info);
360 smi_info->si_state = SI_NORMAL;
361 return;
363 enable_si_irq(smi_info);
365 smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
366 smi_info->curr_msg->data[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD;
367 smi_info->curr_msg->data_size = 2;
369 smi_info->handlers->start_transaction(
370 smi_info->si_sm,
371 smi_info->curr_msg->data,
372 smi_info->curr_msg->data_size);
373 smi_info->si_state = SI_GETTING_EVENTS;
374 } else {
375 smi_info->si_state = SI_NORMAL;
379 static void handle_transaction_done(struct smi_info *smi_info)
381 struct ipmi_smi_msg *msg;
382 #ifdef DEBUG_TIMING
383 struct timeval t;
385 do_gettimeofday(&t);
386 printk("**Done: %d.%9.9d\n", t.tv_sec, t.tv_usec);
387 #endif
388 switch (smi_info->si_state) {
389 case SI_NORMAL:
390 if (!smi_info->curr_msg)
391 break;
393 smi_info->curr_msg->rsp_size
394 = smi_info->handlers->get_result(
395 smi_info->si_sm,
396 smi_info->curr_msg->rsp,
397 IPMI_MAX_MSG_LENGTH);
399 /* Do this here becase deliver_recv_msg() releases the
400 lock, and a new message can be put in during the
401 time the lock is released. */
402 msg = smi_info->curr_msg;
403 smi_info->curr_msg = NULL;
404 deliver_recv_msg(smi_info, msg);
405 break;
407 case SI_GETTING_FLAGS:
409 unsigned char msg[4];
410 unsigned int len;
412 /* We got the flags from the SMI, now handle them. */
413 len = smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
414 if (msg[2] != 0) {
415 /* Error fetching flags, just give up for
416 now. */
417 smi_info->si_state = SI_NORMAL;
418 } else if (len < 4) {
419 /* Hmm, no flags. That's technically illegal, but
420 don't use uninitialized data. */
421 smi_info->si_state = SI_NORMAL;
422 } else {
423 smi_info->msg_flags = msg[3];
424 handle_flags(smi_info);
426 break;
429 case SI_CLEARING_FLAGS:
430 case SI_CLEARING_FLAGS_THEN_SET_IRQ:
432 unsigned char msg[3];
434 /* We cleared the flags. */
435 smi_info->handlers->get_result(smi_info->si_sm, msg, 3);
436 if (msg[2] != 0) {
437 /* Error clearing flags */
438 printk(KERN_WARNING
439 "ipmi_si: Error clearing flags: %2.2x\n",
440 msg[2]);
442 if (smi_info->si_state == SI_CLEARING_FLAGS_THEN_SET_IRQ)
443 start_enable_irq(smi_info);
444 else
445 smi_info->si_state = SI_NORMAL;
446 break;
449 case SI_GETTING_EVENTS:
451 smi_info->curr_msg->rsp_size
452 = smi_info->handlers->get_result(
453 smi_info->si_sm,
454 smi_info->curr_msg->rsp,
455 IPMI_MAX_MSG_LENGTH);
457 /* Do this here becase deliver_recv_msg() releases the
458 lock, and a new message can be put in during the
459 time the lock is released. */
460 msg = smi_info->curr_msg;
461 smi_info->curr_msg = NULL;
462 if (msg->rsp[2] != 0) {
463 /* Error getting event, probably done. */
464 msg->done(msg);
466 /* Take off the event flag. */
467 smi_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
468 handle_flags(smi_info);
469 } else {
470 spin_lock(&smi_info->count_lock);
471 smi_info->events++;
472 spin_unlock(&smi_info->count_lock);
474 /* Do this before we deliver the message
475 because delivering the message releases the
476 lock and something else can mess with the
477 state. */
478 handle_flags(smi_info);
480 deliver_recv_msg(smi_info, msg);
482 break;
485 case SI_GETTING_MESSAGES:
487 smi_info->curr_msg->rsp_size
488 = smi_info->handlers->get_result(
489 smi_info->si_sm,
490 smi_info->curr_msg->rsp,
491 IPMI_MAX_MSG_LENGTH);
493 /* Do this here becase deliver_recv_msg() releases the
494 lock, and a new message can be put in during the
495 time the lock is released. */
496 msg = smi_info->curr_msg;
497 smi_info->curr_msg = NULL;
498 if (msg->rsp[2] != 0) {
499 /* Error getting event, probably done. */
500 msg->done(msg);
502 /* Take off the msg flag. */
503 smi_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
504 handle_flags(smi_info);
505 } else {
506 spin_lock(&smi_info->count_lock);
507 smi_info->incoming_messages++;
508 spin_unlock(&smi_info->count_lock);
510 /* Do this before we deliver the message
511 because delivering the message releases the
512 lock and something else can mess with the
513 state. */
514 handle_flags(smi_info);
516 deliver_recv_msg(smi_info, msg);
518 break;
521 case SI_ENABLE_INTERRUPTS1:
523 unsigned char msg[4];
525 /* We got the flags from the SMI, now handle them. */
526 smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
527 if (msg[2] != 0) {
528 printk(KERN_WARNING
529 "ipmi_si: Could not enable interrupts"
530 ", failed get, using polled mode.\n");
531 smi_info->si_state = SI_NORMAL;
532 } else {
533 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
534 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
535 msg[2] = msg[3] | 1; /* enable msg queue int */
536 smi_info->handlers->start_transaction(
537 smi_info->si_sm, msg, 3);
538 smi_info->si_state = SI_ENABLE_INTERRUPTS2;
540 break;
543 case SI_ENABLE_INTERRUPTS2:
545 unsigned char msg[4];
547 /* We got the flags from the SMI, now handle them. */
548 smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
549 if (msg[2] != 0) {
550 printk(KERN_WARNING
551 "ipmi_si: Could not enable interrupts"
552 ", failed set, using polled mode.\n");
554 smi_info->si_state = SI_NORMAL;
555 break;
560 /* Called on timeouts and events. Timeouts should pass the elapsed
561 time, interrupts should pass in zero. */
562 static enum si_sm_result smi_event_handler(struct smi_info *smi_info,
563 int time)
565 enum si_sm_result si_sm_result;
567 restart:
568 /* There used to be a loop here that waited a little while
569 (around 25us) before giving up. That turned out to be
570 pointless, the minimum delays I was seeing were in the 300us
571 range, which is far too long to wait in an interrupt. So
572 we just run until the state machine tells us something
573 happened or it needs a delay. */
574 si_sm_result = smi_info->handlers->event(smi_info->si_sm, time);
575 time = 0;
576 while (si_sm_result == SI_SM_CALL_WITHOUT_DELAY)
578 si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
581 if (si_sm_result == SI_SM_TRANSACTION_COMPLETE)
583 spin_lock(&smi_info->count_lock);
584 smi_info->complete_transactions++;
585 spin_unlock(&smi_info->count_lock);
587 handle_transaction_done(smi_info);
588 si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
590 else if (si_sm_result == SI_SM_HOSED)
592 spin_lock(&smi_info->count_lock);
593 smi_info->hosed_count++;
594 spin_unlock(&smi_info->count_lock);
596 /* Do the before return_hosed_msg, because that
597 releases the lock. */
598 smi_info->si_state = SI_NORMAL;
599 if (smi_info->curr_msg != NULL) {
600 /* If we were handling a user message, format
601 a response to send to the upper layer to
602 tell it about the error. */
603 return_hosed_msg(smi_info);
605 si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
608 /* We prefer handling attn over new messages. */
609 if (si_sm_result == SI_SM_ATTN)
611 unsigned char msg[2];
613 spin_lock(&smi_info->count_lock);
614 smi_info->attentions++;
615 spin_unlock(&smi_info->count_lock);
617 /* Got a attn, send down a get message flags to see
618 what's causing it. It would be better to handle
619 this in the upper layer, but due to the way
620 interrupts work with the SMI, that's not really
621 possible. */
622 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
623 msg[1] = IPMI_GET_MSG_FLAGS_CMD;
625 smi_info->handlers->start_transaction(
626 smi_info->si_sm, msg, 2);
627 smi_info->si_state = SI_GETTING_FLAGS;
628 goto restart;
631 /* If we are currently idle, try to start the next message. */
632 if (si_sm_result == SI_SM_IDLE) {
633 spin_lock(&smi_info->count_lock);
634 smi_info->idles++;
635 spin_unlock(&smi_info->count_lock);
637 si_sm_result = start_next_msg(smi_info);
638 if (si_sm_result != SI_SM_IDLE)
639 goto restart;
642 if ((si_sm_result == SI_SM_IDLE)
643 && (atomic_read(&smi_info->req_events)))
645 /* We are idle and the upper layer requested that I fetch
646 events, so do so. */
647 unsigned char msg[2];
649 spin_lock(&smi_info->count_lock);
650 smi_info->flag_fetches++;
651 spin_unlock(&smi_info->count_lock);
653 atomic_set(&smi_info->req_events, 0);
654 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
655 msg[1] = IPMI_GET_MSG_FLAGS_CMD;
657 smi_info->handlers->start_transaction(
658 smi_info->si_sm, msg, 2);
659 smi_info->si_state = SI_GETTING_FLAGS;
660 goto restart;
663 return si_sm_result;
666 static void sender(void *send_info,
667 struct ipmi_smi_msg *msg,
668 int priority)
670 struct smi_info *smi_info = send_info;
671 enum si_sm_result result;
672 unsigned long flags;
673 #ifdef DEBUG_TIMING
674 struct timeval t;
675 #endif
677 spin_lock_irqsave(&(smi_info->msg_lock), flags);
678 #ifdef DEBUG_TIMING
679 do_gettimeofday(&t);
680 printk("**Enqueue: %d.%9.9d\n", t.tv_sec, t.tv_usec);
681 #endif
683 if (smi_info->run_to_completion) {
684 /* If we are running to completion, then throw it in
685 the list and run transactions until everything is
686 clear. Priority doesn't matter here. */
687 list_add_tail(&(msg->link), &(smi_info->xmit_msgs));
689 /* We have to release the msg lock and claim the smi
690 lock in this case, because of race conditions. */
691 spin_unlock_irqrestore(&(smi_info->msg_lock), flags);
693 spin_lock_irqsave(&(smi_info->si_lock), flags);
694 result = smi_event_handler(smi_info, 0);
695 while (result != SI_SM_IDLE) {
696 udelay(SI_SHORT_TIMEOUT_USEC);
697 result = smi_event_handler(smi_info,
698 SI_SHORT_TIMEOUT_USEC);
700 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
701 return;
702 } else {
703 if (priority > 0) {
704 list_add_tail(&(msg->link), &(smi_info->hp_xmit_msgs));
705 } else {
706 list_add_tail(&(msg->link), &(smi_info->xmit_msgs));
709 spin_unlock_irqrestore(&(smi_info->msg_lock), flags);
711 spin_lock_irqsave(&(smi_info->si_lock), flags);
712 if ((smi_info->si_state == SI_NORMAL)
713 && (smi_info->curr_msg == NULL))
715 start_next_msg(smi_info);
716 si_restart_short_timer(smi_info);
718 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
721 static void set_run_to_completion(void *send_info, int i_run_to_completion)
723 struct smi_info *smi_info = send_info;
724 enum si_sm_result result;
725 unsigned long flags;
727 spin_lock_irqsave(&(smi_info->si_lock), flags);
729 smi_info->run_to_completion = i_run_to_completion;
730 if (i_run_to_completion) {
731 result = smi_event_handler(smi_info, 0);
732 while (result != SI_SM_IDLE) {
733 udelay(SI_SHORT_TIMEOUT_USEC);
734 result = smi_event_handler(smi_info,
735 SI_SHORT_TIMEOUT_USEC);
739 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
742 static void poll(void *send_info)
744 struct smi_info *smi_info = send_info;
746 smi_event_handler(smi_info, 0);
749 static void request_events(void *send_info)
751 struct smi_info *smi_info = send_info;
753 atomic_set(&smi_info->req_events, 1);
756 static int initialized = 0;
758 /* Must be called with interrupts off and with the si_lock held. */
759 static void si_restart_short_timer(struct smi_info *smi_info)
761 #if defined(CONFIG_HIGH_RES_TIMERS)
762 unsigned long flags;
763 unsigned long jiffies_now;
765 if (del_timer(&(smi_info->si_timer))) {
766 /* If we don't delete the timer, then it will go off
767 immediately, anyway. So we only process if we
768 actually delete the timer. */
770 /* We already have irqsave on, so no need for it
771 here. */
772 read_lock(&xtime_lock);
773 jiffies_now = jiffies;
774 smi_info->si_timer.expires = jiffies_now;
775 smi_info->si_timer.sub_expires = get_arch_cycles(jiffies_now);
777 add_usec_to_timer(&smi_info->si_timer, SI_SHORT_TIMEOUT_USEC);
779 add_timer(&(smi_info->si_timer));
780 spin_lock_irqsave(&smi_info->count_lock, flags);
781 smi_info->timeout_restarts++;
782 spin_unlock_irqrestore(&smi_info->count_lock, flags);
784 #endif
787 static void smi_timeout(unsigned long data)
789 struct smi_info *smi_info = (struct smi_info *) data;
790 enum si_sm_result smi_result;
791 unsigned long flags;
792 unsigned long jiffies_now;
793 unsigned long time_diff;
794 #ifdef DEBUG_TIMING
795 struct timeval t;
796 #endif
798 if (smi_info->stop_operation) {
799 smi_info->timer_stopped = 1;
800 return;
803 spin_lock_irqsave(&(smi_info->si_lock), flags);
804 #ifdef DEBUG_TIMING
805 do_gettimeofday(&t);
806 printk("**Timer: %d.%9.9d\n", t.tv_sec, t.tv_usec);
807 #endif
808 jiffies_now = jiffies;
809 time_diff = ((jiffies_now - smi_info->last_timeout_jiffies)
810 * SI_USEC_PER_JIFFY);
811 smi_result = smi_event_handler(smi_info, time_diff);
813 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
815 smi_info->last_timeout_jiffies = jiffies_now;
817 if ((smi_info->irq) && (! smi_info->interrupt_disabled)) {
818 /* Running with interrupts, only do long timeouts. */
819 smi_info->si_timer.expires = jiffies + SI_TIMEOUT_JIFFIES;
820 spin_lock_irqsave(&smi_info->count_lock, flags);
821 smi_info->long_timeouts++;
822 spin_unlock_irqrestore(&smi_info->count_lock, flags);
823 goto do_add_timer;
826 /* If the state machine asks for a short delay, then shorten
827 the timer timeout. */
828 if (smi_result == SI_SM_CALL_WITH_DELAY) {
829 spin_lock_irqsave(&smi_info->count_lock, flags);
830 smi_info->short_timeouts++;
831 spin_unlock_irqrestore(&smi_info->count_lock, flags);
832 #if defined(CONFIG_HIGH_RES_TIMERS)
833 read_lock(&xtime_lock);
834 smi_info->si_timer.expires = jiffies;
835 smi_info->si_timer.sub_expires
836 = get_arch_cycles(smi_info->si_timer.expires);
837 read_unlock(&xtime_lock);
838 add_usec_to_timer(&smi_info->si_timer, SI_SHORT_TIMEOUT_USEC);
839 #else
840 smi_info->si_timer.expires = jiffies + 1;
841 #endif
842 } else {
843 spin_lock_irqsave(&smi_info->count_lock, flags);
844 smi_info->long_timeouts++;
845 spin_unlock_irqrestore(&smi_info->count_lock, flags);
846 smi_info->si_timer.expires = jiffies + SI_TIMEOUT_JIFFIES;
847 #if defined(CONFIG_HIGH_RES_TIMERS)
848 smi_info->si_timer.sub_expires = 0;
849 #endif
852 do_add_timer:
853 add_timer(&(smi_info->si_timer));
856 static irqreturn_t si_irq_handler(int irq, void *data, struct pt_regs *regs)
858 struct smi_info *smi_info = data;
859 unsigned long flags;
860 #ifdef DEBUG_TIMING
861 struct timeval t;
862 #endif
864 spin_lock_irqsave(&(smi_info->si_lock), flags);
866 spin_lock(&smi_info->count_lock);
867 smi_info->interrupts++;
868 spin_unlock(&smi_info->count_lock);
870 if (smi_info->stop_operation)
871 goto out;
873 #ifdef DEBUG_TIMING
874 do_gettimeofday(&t);
875 printk("**Interrupt: %d.%9.9d\n", t.tv_sec, t.tv_usec);
876 #endif
877 smi_event_handler(smi_info, 0);
878 out:
879 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
880 return IRQ_HANDLED;
883 static irqreturn_t si_bt_irq_handler(int irq, void *data, struct pt_regs *regs)
885 struct smi_info *smi_info = data;
886 /* We need to clear the IRQ flag for the BT interface. */
887 smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG,
888 IPMI_BT_INTMASK_CLEAR_IRQ_BIT
889 | IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
890 return si_irq_handler(irq, data, regs);
894 static struct ipmi_smi_handlers handlers =
896 .owner = THIS_MODULE,
897 .sender = sender,
898 .request_events = request_events,
899 .set_run_to_completion = set_run_to_completion,
900 .poll = poll,
903 /* There can be 4 IO ports passed in (with or without IRQs), 4 addresses,
904 a default IO port, and 1 ACPI/SPMI address. That sets SI_MAX_DRIVERS */
906 #define SI_MAX_PARMS 4
907 #define SI_MAX_DRIVERS ((SI_MAX_PARMS * 2) + 2)
908 static struct smi_info *smi_infos[SI_MAX_DRIVERS] =
909 { NULL, NULL, NULL, NULL };
911 #define DEVICE_NAME "ipmi_si"
913 #define DEFAULT_KCS_IO_PORT 0xca2
914 #define DEFAULT_SMIC_IO_PORT 0xca9
915 #define DEFAULT_BT_IO_PORT 0xe4
916 #define DEFAULT_REGSPACING 1
918 static int si_trydefaults = 1;
919 static char *si_type[SI_MAX_PARMS];
920 #define MAX_SI_TYPE_STR 30
921 static char si_type_str[MAX_SI_TYPE_STR];
922 static unsigned long addrs[SI_MAX_PARMS];
923 static int num_addrs;
924 static unsigned int ports[SI_MAX_PARMS];
925 static int num_ports;
926 static int irqs[SI_MAX_PARMS];
927 static int num_irqs;
928 static int regspacings[SI_MAX_PARMS];
929 static int num_regspacings = 0;
930 static int regsizes[SI_MAX_PARMS];
931 static int num_regsizes = 0;
932 static int regshifts[SI_MAX_PARMS];
933 static int num_regshifts = 0;
934 static int slave_addrs[SI_MAX_PARMS];
935 static int num_slave_addrs = 0;
938 module_param_named(trydefaults, si_trydefaults, bool, 0);
939 MODULE_PARM_DESC(trydefaults, "Setting this to 'false' will disable the"
940 " default scan of the KCS and SMIC interface at the standard"
941 " address");
942 module_param_string(type, si_type_str, MAX_SI_TYPE_STR, 0);
943 MODULE_PARM_DESC(type, "Defines the type of each interface, each"
944 " interface separated by commas. The types are 'kcs',"
945 " 'smic', and 'bt'. For example si_type=kcs,bt will set"
946 " the first interface to kcs and the second to bt");
947 module_param_array(addrs, long, &num_addrs, 0);
948 MODULE_PARM_DESC(addrs, "Sets the memory address of each interface, the"
949 " addresses separated by commas. Only use if an interface"
950 " is in memory. Otherwise, set it to zero or leave"
951 " it blank.");
952 module_param_array(ports, int, &num_ports, 0);
953 MODULE_PARM_DESC(ports, "Sets the port address of each interface, the"
954 " addresses separated by commas. Only use if an interface"
955 " is a port. Otherwise, set it to zero or leave"
956 " it blank.");
957 module_param_array(irqs, int, &num_irqs, 0);
958 MODULE_PARM_DESC(irqs, "Sets the interrupt of each interface, the"
959 " addresses separated by commas. Only use if an interface"
960 " has an interrupt. Otherwise, set it to zero or leave"
961 " it blank.");
962 module_param_array(regspacings, int, &num_regspacings, 0);
963 MODULE_PARM_DESC(regspacings, "The number of bytes between the start address"
964 " and each successive register used by the interface. For"
965 " instance, if the start address is 0xca2 and the spacing"
966 " is 2, then the second address is at 0xca4. Defaults"
967 " to 1.");
968 module_param_array(regsizes, int, &num_regsizes, 0);
969 MODULE_PARM_DESC(regsizes, "The size of the specific IPMI register in bytes."
970 " This should generally be 1, 2, 4, or 8 for an 8-bit,"
971 " 16-bit, 32-bit, or 64-bit register. Use this if you"
972 " the 8-bit IPMI register has to be read from a larger"
973 " register.");
974 module_param_array(regshifts, int, &num_regshifts, 0);
975 MODULE_PARM_DESC(regshifts, "The amount to shift the data read from the."
976 " IPMI register, in bits. For instance, if the data"
977 " is read from a 32-bit word and the IPMI data is in"
978 " bit 8-15, then the shift would be 8");
979 module_param_array(slave_addrs, int, &num_slave_addrs, 0);
980 MODULE_PARM_DESC(slave_addrs, "Set the default IPMB slave address for"
981 " the controller. Normally this is 0x20, but can be"
982 " overridden by this parm. This is an array indexed"
983 " by interface number.");
986 #define IPMI_MEM_ADDR_SPACE 1
987 #define IPMI_IO_ADDR_SPACE 2
989 #if defined(CONFIG_ACPI_INTERPRETER) || defined(CONFIG_X86) || defined(CONFIG_PCI)
990 static int is_new_interface(int intf, u8 addr_space, unsigned long base_addr)
992 int i;
994 for (i = 0; i < SI_MAX_PARMS; ++i) {
995 /* Don't check our address. */
996 if (i == intf)
997 continue;
998 if (si_type[i] != NULL) {
999 if ((addr_space == IPMI_MEM_ADDR_SPACE &&
1000 base_addr == addrs[i]) ||
1001 (addr_space == IPMI_IO_ADDR_SPACE &&
1002 base_addr == ports[i]))
1003 return 0;
1005 else
1006 break;
1009 return 1;
1011 #endif
1013 static int std_irq_setup(struct smi_info *info)
1015 int rv;
1017 if (!info->irq)
1018 return 0;
1020 if (info->si_type == SI_BT) {
1021 rv = request_irq(info->irq,
1022 si_bt_irq_handler,
1023 SA_INTERRUPT,
1024 DEVICE_NAME,
1025 info);
1026 if (!rv)
1027 /* Enable the interrupt in the BT interface. */
1028 info->io.outputb(&info->io, IPMI_BT_INTMASK_REG,
1029 IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
1030 } else
1031 rv = request_irq(info->irq,
1032 si_irq_handler,
1033 SA_INTERRUPT,
1034 DEVICE_NAME,
1035 info);
1036 if (rv) {
1037 printk(KERN_WARNING
1038 "ipmi_si: %s unable to claim interrupt %d,"
1039 " running polled\n",
1040 DEVICE_NAME, info->irq);
1041 info->irq = 0;
1042 } else {
1043 printk(" Using irq %d\n", info->irq);
1046 return rv;
1049 static void std_irq_cleanup(struct smi_info *info)
1051 if (!info->irq)
1052 return;
1054 if (info->si_type == SI_BT)
1055 /* Disable the interrupt in the BT interface. */
1056 info->io.outputb(&info->io, IPMI_BT_INTMASK_REG, 0);
1057 free_irq(info->irq, info);
1060 static unsigned char port_inb(struct si_sm_io *io, unsigned int offset)
1062 unsigned int *addr = io->info;
1064 return inb((*addr)+(offset*io->regspacing));
1067 static void port_outb(struct si_sm_io *io, unsigned int offset,
1068 unsigned char b)
1070 unsigned int *addr = io->info;
1072 outb(b, (*addr)+(offset * io->regspacing));
1075 static unsigned char port_inw(struct si_sm_io *io, unsigned int offset)
1077 unsigned int *addr = io->info;
1079 return (inw((*addr)+(offset * io->regspacing)) >> io->regshift) & 0xff;
1082 static void port_outw(struct si_sm_io *io, unsigned int offset,
1083 unsigned char b)
1085 unsigned int *addr = io->info;
1087 outw(b << io->regshift, (*addr)+(offset * io->regspacing));
1090 static unsigned char port_inl(struct si_sm_io *io, unsigned int offset)
1092 unsigned int *addr = io->info;
1094 return (inl((*addr)+(offset * io->regspacing)) >> io->regshift) & 0xff;
1097 static void port_outl(struct si_sm_io *io, unsigned int offset,
1098 unsigned char b)
1100 unsigned int *addr = io->info;
1102 outl(b << io->regshift, (*addr)+(offset * io->regspacing));
1105 static void port_cleanup(struct smi_info *info)
1107 unsigned int *addr = info->io.info;
1108 int mapsize;
1110 if (addr && (*addr)) {
1111 mapsize = ((info->io_size * info->io.regspacing)
1112 - (info->io.regspacing - info->io.regsize));
1114 release_region (*addr, mapsize);
1116 kfree(info);
1119 static int port_setup(struct smi_info *info)
1121 unsigned int *addr = info->io.info;
1122 int mapsize;
1124 if (!addr || (!*addr))
1125 return -ENODEV;
1127 info->io_cleanup = port_cleanup;
1129 /* Figure out the actual inb/inw/inl/etc routine to use based
1130 upon the register size. */
1131 switch (info->io.regsize) {
1132 case 1:
1133 info->io.inputb = port_inb;
1134 info->io.outputb = port_outb;
1135 break;
1136 case 2:
1137 info->io.inputb = port_inw;
1138 info->io.outputb = port_outw;
1139 break;
1140 case 4:
1141 info->io.inputb = port_inl;
1142 info->io.outputb = port_outl;
1143 break;
1144 default:
1145 printk("ipmi_si: Invalid register size: %d\n",
1146 info->io.regsize);
1147 return -EINVAL;
1150 /* Calculate the total amount of memory to claim. This is an
1151 * unusual looking calculation, but it avoids claiming any
1152 * more memory than it has to. It will claim everything
1153 * between the first address to the end of the last full
1154 * register. */
1155 mapsize = ((info->io_size * info->io.regspacing)
1156 - (info->io.regspacing - info->io.regsize));
1158 if (request_region(*addr, mapsize, DEVICE_NAME) == NULL)
1159 return -EIO;
1160 return 0;
1163 static int try_init_port(int intf_num, struct smi_info **new_info)
1165 struct smi_info *info;
1167 if (!ports[intf_num])
1168 return -ENODEV;
1170 if (!is_new_interface(intf_num, IPMI_IO_ADDR_SPACE,
1171 ports[intf_num]))
1172 return -ENODEV;
1174 info = kmalloc(sizeof(*info), GFP_KERNEL);
1175 if (!info) {
1176 printk(KERN_ERR "ipmi_si: Could not allocate SI data (1)\n");
1177 return -ENOMEM;
1179 memset(info, 0, sizeof(*info));
1181 info->io_setup = port_setup;
1182 info->io.info = &(ports[intf_num]);
1183 info->io.addr = NULL;
1184 info->io.regspacing = regspacings[intf_num];
1185 if (!info->io.regspacing)
1186 info->io.regspacing = DEFAULT_REGSPACING;
1187 info->io.regsize = regsizes[intf_num];
1188 if (!info->io.regsize)
1189 info->io.regsize = DEFAULT_REGSPACING;
1190 info->io.regshift = regshifts[intf_num];
1191 info->irq = 0;
1192 info->irq_setup = NULL;
1193 *new_info = info;
1195 if (si_type[intf_num] == NULL)
1196 si_type[intf_num] = "kcs";
1198 printk("ipmi_si: Trying \"%s\" at I/O port 0x%x\n",
1199 si_type[intf_num], ports[intf_num]);
1200 return 0;
1203 static unsigned char mem_inb(struct si_sm_io *io, unsigned int offset)
1205 return readb((io->addr)+(offset * io->regspacing));
1208 static void mem_outb(struct si_sm_io *io, unsigned int offset,
1209 unsigned char b)
1211 writeb(b, (io->addr)+(offset * io->regspacing));
1214 static unsigned char mem_inw(struct si_sm_io *io, unsigned int offset)
1216 return (readw((io->addr)+(offset * io->regspacing)) >> io->regshift)
1217 && 0xff;
1220 static void mem_outw(struct si_sm_io *io, unsigned int offset,
1221 unsigned char b)
1223 writeb(b << io->regshift, (io->addr)+(offset * io->regspacing));
1226 static unsigned char mem_inl(struct si_sm_io *io, unsigned int offset)
1228 return (readl((io->addr)+(offset * io->regspacing)) >> io->regshift)
1229 && 0xff;
1232 static void mem_outl(struct si_sm_io *io, unsigned int offset,
1233 unsigned char b)
1235 writel(b << io->regshift, (io->addr)+(offset * io->regspacing));
1238 #ifdef readq
1239 static unsigned char mem_inq(struct si_sm_io *io, unsigned int offset)
1241 return (readq((io->addr)+(offset * io->regspacing)) >> io->regshift)
1242 && 0xff;
1245 static void mem_outq(struct si_sm_io *io, unsigned int offset,
1246 unsigned char b)
1248 writeq(b << io->regshift, (io->addr)+(offset * io->regspacing));
1250 #endif
1252 static void mem_cleanup(struct smi_info *info)
1254 unsigned long *addr = info->io.info;
1255 int mapsize;
1257 if (info->io.addr) {
1258 iounmap(info->io.addr);
1260 mapsize = ((info->io_size * info->io.regspacing)
1261 - (info->io.regspacing - info->io.regsize));
1263 release_mem_region(*addr, mapsize);
1265 kfree(info);
1268 static int mem_setup(struct smi_info *info)
1270 unsigned long *addr = info->io.info;
1271 int mapsize;
1273 if (!addr || (!*addr))
1274 return -ENODEV;
1276 info->io_cleanup = mem_cleanup;
1278 /* Figure out the actual readb/readw/readl/etc routine to use based
1279 upon the register size. */
1280 switch (info->io.regsize) {
1281 case 1:
1282 info->io.inputb = mem_inb;
1283 info->io.outputb = mem_outb;
1284 break;
1285 case 2:
1286 info->io.inputb = mem_inw;
1287 info->io.outputb = mem_outw;
1288 break;
1289 case 4:
1290 info->io.inputb = mem_inl;
1291 info->io.outputb = mem_outl;
1292 break;
1293 #ifdef readq
1294 case 8:
1295 info->io.inputb = mem_inq;
1296 info->io.outputb = mem_outq;
1297 break;
1298 #endif
1299 default:
1300 printk("ipmi_si: Invalid register size: %d\n",
1301 info->io.regsize);
1302 return -EINVAL;
1305 /* Calculate the total amount of memory to claim. This is an
1306 * unusual looking calculation, but it avoids claiming any
1307 * more memory than it has to. It will claim everything
1308 * between the first address to the end of the last full
1309 * register. */
1310 mapsize = ((info->io_size * info->io.regspacing)
1311 - (info->io.regspacing - info->io.regsize));
1313 if (request_mem_region(*addr, mapsize, DEVICE_NAME) == NULL)
1314 return -EIO;
1316 info->io.addr = ioremap(*addr, mapsize);
1317 if (info->io.addr == NULL) {
1318 release_mem_region(*addr, mapsize);
1319 return -EIO;
1321 return 0;
1324 static int try_init_mem(int intf_num, struct smi_info **new_info)
1326 struct smi_info *info;
1328 if (!addrs[intf_num])
1329 return -ENODEV;
1331 if (!is_new_interface(intf_num, IPMI_MEM_ADDR_SPACE,
1332 addrs[intf_num]))
1333 return -ENODEV;
1335 info = kmalloc(sizeof(*info), GFP_KERNEL);
1336 if (!info) {
1337 printk(KERN_ERR "ipmi_si: Could not allocate SI data (2)\n");
1338 return -ENOMEM;
1340 memset(info, 0, sizeof(*info));
1342 info->io_setup = mem_setup;
1343 info->io.info = &addrs[intf_num];
1344 info->io.addr = NULL;
1345 info->io.regspacing = regspacings[intf_num];
1346 if (!info->io.regspacing)
1347 info->io.regspacing = DEFAULT_REGSPACING;
1348 info->io.regsize = regsizes[intf_num];
1349 if (!info->io.regsize)
1350 info->io.regsize = DEFAULT_REGSPACING;
1351 info->io.regshift = regshifts[intf_num];
1352 info->irq = 0;
1353 info->irq_setup = NULL;
1354 *new_info = info;
1356 if (si_type[intf_num] == NULL)
1357 si_type[intf_num] = "kcs";
1359 printk("ipmi_si: Trying \"%s\" at memory address 0x%lx\n",
1360 si_type[intf_num], addrs[intf_num]);
1361 return 0;
1365 #ifdef CONFIG_ACPI_INTERPRETER
1367 #include <linux/acpi.h>
1369 /* Once we get an ACPI failure, we don't try any more, because we go
1370 through the tables sequentially. Once we don't find a table, there
1371 are no more. */
1372 static int acpi_failure = 0;
1374 /* For GPE-type interrupts. */
1375 static u32 ipmi_acpi_gpe(void *context)
1377 struct smi_info *smi_info = context;
1378 unsigned long flags;
1379 #ifdef DEBUG_TIMING
1380 struct timeval t;
1381 #endif
1383 spin_lock_irqsave(&(smi_info->si_lock), flags);
1385 spin_lock(&smi_info->count_lock);
1386 smi_info->interrupts++;
1387 spin_unlock(&smi_info->count_lock);
1389 if (smi_info->stop_operation)
1390 goto out;
1392 #ifdef DEBUG_TIMING
1393 do_gettimeofday(&t);
1394 printk("**ACPI_GPE: %d.%9.9d\n", t.tv_sec, t.tv_usec);
1395 #endif
1396 smi_event_handler(smi_info, 0);
1397 out:
1398 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
1400 return ACPI_INTERRUPT_HANDLED;
1403 static int acpi_gpe_irq_setup(struct smi_info *info)
1405 acpi_status status;
1407 if (!info->irq)
1408 return 0;
1410 /* FIXME - is level triggered right? */
1411 status = acpi_install_gpe_handler(NULL,
1412 info->irq,
1413 ACPI_GPE_LEVEL_TRIGGERED,
1414 &ipmi_acpi_gpe,
1415 info);
1416 if (status != AE_OK) {
1417 printk(KERN_WARNING
1418 "ipmi_si: %s unable to claim ACPI GPE %d,"
1419 " running polled\n",
1420 DEVICE_NAME, info->irq);
1421 info->irq = 0;
1422 return -EINVAL;
1423 } else {
1424 printk(" Using ACPI GPE %d\n", info->irq);
1425 return 0;
1429 static void acpi_gpe_irq_cleanup(struct smi_info *info)
1431 if (!info->irq)
1432 return;
1434 acpi_remove_gpe_handler(NULL, info->irq, &ipmi_acpi_gpe);
1438 * Defined at
1439 * http://h21007.www2.hp.com/dspp/files/unprotected/devresource/Docs/TechPapers/IA64/hpspmi.pdf
1441 struct SPMITable {
1442 s8 Signature[4];
1443 u32 Length;
1444 u8 Revision;
1445 u8 Checksum;
1446 s8 OEMID[6];
1447 s8 OEMTableID[8];
1448 s8 OEMRevision[4];
1449 s8 CreatorID[4];
1450 s8 CreatorRevision[4];
1451 u8 InterfaceType;
1452 u8 IPMIlegacy;
1453 s16 SpecificationRevision;
1456 * Bit 0 - SCI interrupt supported
1457 * Bit 1 - I/O APIC/SAPIC
1459 u8 InterruptType;
1461 /* If bit 0 of InterruptType is set, then this is the SCI
1462 interrupt in the GPEx_STS register. */
1463 u8 GPE;
1465 s16 Reserved;
1467 /* If bit 1 of InterruptType is set, then this is the I/O
1468 APIC/SAPIC interrupt. */
1469 u32 GlobalSystemInterrupt;
1471 /* The actual register address. */
1472 struct acpi_generic_address addr;
1474 u8 UID[4];
1476 s8 spmi_id[1]; /* A '\0' terminated array starts here. */
1479 static int try_init_acpi(int intf_num, struct smi_info **new_info)
1481 struct smi_info *info;
1482 acpi_status status;
1483 struct SPMITable *spmi;
1484 char *io_type;
1485 u8 addr_space;
1487 if (acpi_failure)
1488 return -ENODEV;
1490 status = acpi_get_firmware_table("SPMI", intf_num+1,
1491 ACPI_LOGICAL_ADDRESSING,
1492 (struct acpi_table_header **) &spmi);
1493 if (status != AE_OK) {
1494 acpi_failure = 1;
1495 return -ENODEV;
1498 if (spmi->IPMIlegacy != 1) {
1499 printk(KERN_INFO "IPMI: Bad SPMI legacy %d\n", spmi->IPMIlegacy);
1500 return -ENODEV;
1503 if (spmi->addr.address_space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
1504 addr_space = IPMI_MEM_ADDR_SPACE;
1505 else
1506 addr_space = IPMI_IO_ADDR_SPACE;
1507 if (!is_new_interface(-1, addr_space, spmi->addr.address))
1508 return -ENODEV;
1510 if (!spmi->addr.register_bit_width) {
1511 acpi_failure = 1;
1512 return -ENODEV;
1515 /* Figure out the interface type. */
1516 switch (spmi->InterfaceType)
1518 case 1: /* KCS */
1519 si_type[intf_num] = "kcs";
1520 break;
1522 case 2: /* SMIC */
1523 si_type[intf_num] = "smic";
1524 break;
1526 case 3: /* BT */
1527 si_type[intf_num] = "bt";
1528 break;
1530 default:
1531 printk(KERN_INFO "ipmi_si: Unknown ACPI/SPMI SI type %d\n",
1532 spmi->InterfaceType);
1533 return -EIO;
1536 info = kmalloc(sizeof(*info), GFP_KERNEL);
1537 if (!info) {
1538 printk(KERN_ERR "ipmi_si: Could not allocate SI data (3)\n");
1539 return -ENOMEM;
1541 memset(info, 0, sizeof(*info));
1543 if (spmi->InterruptType & 1) {
1544 /* We've got a GPE interrupt. */
1545 info->irq = spmi->GPE;
1546 info->irq_setup = acpi_gpe_irq_setup;
1547 info->irq_cleanup = acpi_gpe_irq_cleanup;
1548 } else if (spmi->InterruptType & 2) {
1549 /* We've got an APIC/SAPIC interrupt. */
1550 info->irq = spmi->GlobalSystemInterrupt;
1551 info->irq_setup = std_irq_setup;
1552 info->irq_cleanup = std_irq_cleanup;
1553 } else {
1554 /* Use the default interrupt setting. */
1555 info->irq = 0;
1556 info->irq_setup = NULL;
1559 if (spmi->addr.register_bit_width) {
1560 /* A (hopefully) properly formed register bit width. */
1561 regspacings[intf_num] = spmi->addr.register_bit_width / 8;
1562 info->io.regspacing = spmi->addr.register_bit_width / 8;
1563 } else {
1564 /* Some broken systems get this wrong and set the value
1565 * to zero. Assume it is the default spacing. If that
1566 * is wrong, too bad, the vendor should fix the tables. */
1567 regspacings[intf_num] = DEFAULT_REGSPACING;
1568 info->io.regspacing = DEFAULT_REGSPACING;
1570 regsizes[intf_num] = regspacings[intf_num];
1571 info->io.regsize = regsizes[intf_num];
1572 regshifts[intf_num] = spmi->addr.register_bit_offset;
1573 info->io.regshift = regshifts[intf_num];
1575 if (spmi->addr.address_space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
1576 io_type = "memory";
1577 info->io_setup = mem_setup;
1578 addrs[intf_num] = spmi->addr.address;
1579 info->io.info = &(addrs[intf_num]);
1580 } else if (spmi->addr.address_space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
1581 io_type = "I/O";
1582 info->io_setup = port_setup;
1583 ports[intf_num] = spmi->addr.address;
1584 info->io.info = &(ports[intf_num]);
1585 } else {
1586 kfree(info);
1587 printk("ipmi_si: Unknown ACPI I/O Address type\n");
1588 return -EIO;
1591 *new_info = info;
1593 printk("ipmi_si: ACPI/SPMI specifies \"%s\" %s SI @ 0x%lx\n",
1594 si_type[intf_num], io_type, (unsigned long) spmi->addr.address);
1595 return 0;
1597 #endif
1599 #ifdef CONFIG_X86
1600 typedef struct dmi_ipmi_data
1602 u8 type;
1603 u8 addr_space;
1604 unsigned long base_addr;
1605 u8 irq;
1606 u8 offset;
1607 u8 slave_addr;
1608 } dmi_ipmi_data_t;
1610 static dmi_ipmi_data_t dmi_data[SI_MAX_DRIVERS];
1611 static int dmi_data_entries;
1613 typedef struct dmi_header
1615 u8 type;
1616 u8 length;
1617 u16 handle;
1618 } dmi_header_t;
1620 static int decode_dmi(dmi_header_t __iomem *dm, int intf_num)
1622 u8 __iomem *data = (u8 __iomem *)dm;
1623 unsigned long base_addr;
1624 u8 reg_spacing;
1625 u8 len = readb(&dm->length);
1626 dmi_ipmi_data_t *ipmi_data = dmi_data+intf_num;
1628 ipmi_data->type = readb(&data[4]);
1630 memcpy(&base_addr, data+8, sizeof(unsigned long));
1631 if (len >= 0x11) {
1632 if (base_addr & 1) {
1633 /* I/O */
1634 base_addr &= 0xFFFE;
1635 ipmi_data->addr_space = IPMI_IO_ADDR_SPACE;
1637 else {
1638 /* Memory */
1639 ipmi_data->addr_space = IPMI_MEM_ADDR_SPACE;
1641 /* If bit 4 of byte 0x10 is set, then the lsb for the address
1642 is odd. */
1643 ipmi_data->base_addr = base_addr | ((readb(&data[0x10]) & 0x10) >> 4);
1645 ipmi_data->irq = readb(&data[0x11]);
1647 /* The top two bits of byte 0x10 hold the register spacing. */
1648 reg_spacing = (readb(&data[0x10]) & 0xC0) >> 6;
1649 switch(reg_spacing){
1650 case 0x00: /* Byte boundaries */
1651 ipmi_data->offset = 1;
1652 break;
1653 case 0x01: /* 32-bit boundaries */
1654 ipmi_data->offset = 4;
1655 break;
1656 case 0x02: /* 16-byte boundaries */
1657 ipmi_data->offset = 16;
1658 break;
1659 default:
1660 /* Some other interface, just ignore it. */
1661 return -EIO;
1663 } else {
1664 /* Old DMI spec. */
1665 /* Note that technically, the lower bit of the base
1666 * address should be 1 if the address is I/O and 0 if
1667 * the address is in memory. So many systems get that
1668 * wrong (and all that I have seen are I/O) so we just
1669 * ignore that bit and assume I/O. Systems that use
1670 * memory should use the newer spec, anyway. */
1671 ipmi_data->base_addr = base_addr & 0xfffe;
1672 ipmi_data->addr_space = IPMI_IO_ADDR_SPACE;
1673 ipmi_data->offset = 1;
1676 ipmi_data->slave_addr = readb(&data[6]);
1678 if (is_new_interface(-1, ipmi_data->addr_space,ipmi_data->base_addr)) {
1679 dmi_data_entries++;
1680 return 0;
1683 memset(ipmi_data, 0, sizeof(dmi_ipmi_data_t));
1685 return -1;
1688 static int dmi_table(u32 base, int len, int num)
1690 u8 __iomem *buf;
1691 struct dmi_header __iomem *dm;
1692 u8 __iomem *data;
1693 int i=1;
1694 int status=-1;
1695 int intf_num = 0;
1697 buf = ioremap(base, len);
1698 if(buf==NULL)
1699 return -1;
1701 data = buf;
1703 while(i<num && (data - buf) < len)
1705 dm=(dmi_header_t __iomem *)data;
1707 if((data-buf+readb(&dm->length)) >= len)
1708 break;
1710 if (readb(&dm->type) == 38) {
1711 if (decode_dmi(dm, intf_num) == 0) {
1712 intf_num++;
1713 if (intf_num >= SI_MAX_DRIVERS)
1714 break;
1718 data+=readb(&dm->length);
1719 while((data-buf) < len && (readb(data)||readb(data+1)))
1720 data++;
1721 data+=2;
1722 i++;
1724 iounmap(buf);
1726 return status;
1729 inline static int dmi_checksum(u8 *buf)
1731 u8 sum=0;
1732 int a;
1734 for(a=0; a<15; a++)
1735 sum+=buf[a];
1736 return (sum==0);
1739 static int dmi_decode(void)
1741 u8 buf[15];
1742 u32 fp=0xF0000;
1744 #ifdef CONFIG_SIMNOW
1745 return -1;
1746 #endif
1748 while(fp < 0xFFFFF)
1750 isa_memcpy_fromio(buf, fp, 15);
1751 if(memcmp(buf, "_DMI_", 5)==0 && dmi_checksum(buf))
1753 u16 num=buf[13]<<8|buf[12];
1754 u16 len=buf[7]<<8|buf[6];
1755 u32 base=buf[11]<<24|buf[10]<<16|buf[9]<<8|buf[8];
1757 if(dmi_table(base, len, num) == 0)
1758 return 0;
1760 fp+=16;
1763 return -1;
1766 static int try_init_smbios(int intf_num, struct smi_info **new_info)
1768 struct smi_info *info;
1769 dmi_ipmi_data_t *ipmi_data = dmi_data+intf_num;
1770 char *io_type;
1772 if (intf_num >= dmi_data_entries)
1773 return -ENODEV;
1775 switch(ipmi_data->type) {
1776 case 0x01: /* KCS */
1777 si_type[intf_num] = "kcs";
1778 break;
1779 case 0x02: /* SMIC */
1780 si_type[intf_num] = "smic";
1781 break;
1782 case 0x03: /* BT */
1783 si_type[intf_num] = "bt";
1784 break;
1785 default:
1786 return -EIO;
1789 info = kmalloc(sizeof(*info), GFP_KERNEL);
1790 if (!info) {
1791 printk(KERN_ERR "ipmi_si: Could not allocate SI data (4)\n");
1792 return -ENOMEM;
1794 memset(info, 0, sizeof(*info));
1796 if (ipmi_data->addr_space == 1) {
1797 io_type = "memory";
1798 info->io_setup = mem_setup;
1799 addrs[intf_num] = ipmi_data->base_addr;
1800 info->io.info = &(addrs[intf_num]);
1801 } else if (ipmi_data->addr_space == 2) {
1802 io_type = "I/O";
1803 info->io_setup = port_setup;
1804 ports[intf_num] = ipmi_data->base_addr;
1805 info->io.info = &(ports[intf_num]);
1806 } else {
1807 kfree(info);
1808 printk("ipmi_si: Unknown SMBIOS I/O Address type.\n");
1809 return -EIO;
1812 regspacings[intf_num] = ipmi_data->offset;
1813 info->io.regspacing = regspacings[intf_num];
1814 if (!info->io.regspacing)
1815 info->io.regspacing = DEFAULT_REGSPACING;
1816 info->io.regsize = DEFAULT_REGSPACING;
1817 info->io.regshift = regshifts[intf_num];
1819 info->slave_addr = ipmi_data->slave_addr;
1821 irqs[intf_num] = ipmi_data->irq;
1823 *new_info = info;
1825 printk("ipmi_si: Found SMBIOS-specified state machine at %s"
1826 " address 0x%lx, slave address 0x%x\n",
1827 io_type, (unsigned long)ipmi_data->base_addr,
1828 ipmi_data->slave_addr);
1829 return 0;
1831 #endif /* CONFIG_X86 */
1833 #ifdef CONFIG_PCI
1835 #define PCI_ERMC_CLASSCODE 0x0C0700
1836 #define PCI_HP_VENDOR_ID 0x103C
1837 #define PCI_MMC_DEVICE_ID 0x121A
1838 #define PCI_MMC_ADDR_CW 0x10
1840 /* Avoid more than one attempt to probe pci smic. */
1841 static int pci_smic_checked = 0;
1843 static int find_pci_smic(int intf_num, struct smi_info **new_info)
1845 struct smi_info *info;
1846 int error;
1847 struct pci_dev *pci_dev = NULL;
1848 u16 base_addr;
1849 int fe_rmc = 0;
1851 if (pci_smic_checked)
1852 return -ENODEV;
1854 pci_smic_checked = 1;
1856 if ((pci_dev = pci_get_device(PCI_HP_VENDOR_ID, PCI_MMC_DEVICE_ID,
1857 NULL)))
1859 else if ((pci_dev = pci_get_class(PCI_ERMC_CLASSCODE, NULL)) &&
1860 pci_dev->subsystem_vendor == PCI_HP_VENDOR_ID)
1861 fe_rmc = 1;
1862 else
1863 return -ENODEV;
1865 error = pci_read_config_word(pci_dev, PCI_MMC_ADDR_CW, &base_addr);
1866 if (error)
1868 pci_dev_put(pci_dev);
1869 printk(KERN_ERR
1870 "ipmi_si: pci_read_config_word() failed (%d).\n",
1871 error);
1872 return -ENODEV;
1875 /* Bit 0: 1 specifies programmed I/O, 0 specifies memory mapped I/O */
1876 if (!(base_addr & 0x0001))
1878 pci_dev_put(pci_dev);
1879 printk(KERN_ERR
1880 "ipmi_si: memory mapped I/O not supported for PCI"
1881 " smic.\n");
1882 return -ENODEV;
1885 base_addr &= 0xFFFE;
1886 if (!fe_rmc)
1887 /* Data register starts at base address + 1 in eRMC */
1888 ++base_addr;
1890 if (!is_new_interface(-1, IPMI_IO_ADDR_SPACE, base_addr)) {
1891 pci_dev_put(pci_dev);
1892 return -ENODEV;
1895 info = kmalloc(sizeof(*info), GFP_KERNEL);
1896 if (!info) {
1897 pci_dev_put(pci_dev);
1898 printk(KERN_ERR "ipmi_si: Could not allocate SI data (5)\n");
1899 return -ENOMEM;
1901 memset(info, 0, sizeof(*info));
1903 info->io_setup = port_setup;
1904 ports[intf_num] = base_addr;
1905 info->io.info = &(ports[intf_num]);
1906 info->io.regspacing = regspacings[intf_num];
1907 if (!info->io.regspacing)
1908 info->io.regspacing = DEFAULT_REGSPACING;
1909 info->io.regsize = DEFAULT_REGSPACING;
1910 info->io.regshift = regshifts[intf_num];
1912 *new_info = info;
1914 irqs[intf_num] = pci_dev->irq;
1915 si_type[intf_num] = "smic";
1917 printk("ipmi_si: Found PCI SMIC at I/O address 0x%lx\n",
1918 (long unsigned int) base_addr);
1920 pci_dev_put(pci_dev);
1921 return 0;
1923 #endif /* CONFIG_PCI */
1925 static int try_init_plug_and_play(int intf_num, struct smi_info **new_info)
1927 #ifdef CONFIG_PCI
1928 if (find_pci_smic(intf_num, new_info)==0)
1929 return 0;
1930 #endif
1931 /* Include other methods here. */
1933 return -ENODEV;
1937 static int try_get_dev_id(struct smi_info *smi_info)
1939 unsigned char msg[2];
1940 unsigned char *resp;
1941 unsigned long resp_len;
1942 enum si_sm_result smi_result;
1943 int rv = 0;
1945 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1946 if (!resp)
1947 return -ENOMEM;
1949 /* Do a Get Device ID command, since it comes back with some
1950 useful info. */
1951 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1952 msg[1] = IPMI_GET_DEVICE_ID_CMD;
1953 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
1955 smi_result = smi_info->handlers->event(smi_info->si_sm, 0);
1956 for (;;)
1958 if (smi_result == SI_SM_CALL_WITH_DELAY) {
1959 set_current_state(TASK_UNINTERRUPTIBLE);
1960 schedule_timeout(1);
1961 smi_result = smi_info->handlers->event(
1962 smi_info->si_sm, 100);
1964 else if (smi_result == SI_SM_CALL_WITHOUT_DELAY)
1966 smi_result = smi_info->handlers->event(
1967 smi_info->si_sm, 0);
1969 else
1970 break;
1972 if (smi_result == SI_SM_HOSED) {
1973 /* We couldn't get the state machine to run, so whatever's at
1974 the port is probably not an IPMI SMI interface. */
1975 rv = -ENODEV;
1976 goto out;
1979 /* Otherwise, we got some data. */
1980 resp_len = smi_info->handlers->get_result(smi_info->si_sm,
1981 resp, IPMI_MAX_MSG_LENGTH);
1982 if (resp_len < 6) {
1983 /* That's odd, it should be longer. */
1984 rv = -EINVAL;
1985 goto out;
1988 if ((resp[1] != IPMI_GET_DEVICE_ID_CMD) || (resp[2] != 0)) {
1989 /* That's odd, it shouldn't be able to fail. */
1990 rv = -EINVAL;
1991 goto out;
1994 /* Record info from the get device id, in case we need it. */
1995 smi_info->ipmi_si_dev_rev = resp[4] & 0xf;
1996 smi_info->ipmi_si_fw_rev_major = resp[5] & 0x7f;
1997 smi_info->ipmi_si_fw_rev_minor = resp[6];
1998 smi_info->ipmi_version_major = resp[7] & 0xf;
1999 smi_info->ipmi_version_minor = resp[7] >> 4;
2001 out:
2002 kfree(resp);
2003 return rv;
2006 static int type_file_read_proc(char *page, char **start, off_t off,
2007 int count, int *eof, void *data)
2009 char *out = (char *) page;
2010 struct smi_info *smi = data;
2012 switch (smi->si_type) {
2013 case SI_KCS:
2014 return sprintf(out, "kcs\n");
2015 case SI_SMIC:
2016 return sprintf(out, "smic\n");
2017 case SI_BT:
2018 return sprintf(out, "bt\n");
2019 default:
2020 return 0;
2024 static int stat_file_read_proc(char *page, char **start, off_t off,
2025 int count, int *eof, void *data)
2027 char *out = (char *) page;
2028 struct smi_info *smi = data;
2030 out += sprintf(out, "interrupts_enabled: %d\n",
2031 smi->irq && !smi->interrupt_disabled);
2032 out += sprintf(out, "short_timeouts: %ld\n",
2033 smi->short_timeouts);
2034 out += sprintf(out, "long_timeouts: %ld\n",
2035 smi->long_timeouts);
2036 out += sprintf(out, "timeout_restarts: %ld\n",
2037 smi->timeout_restarts);
2038 out += sprintf(out, "idles: %ld\n",
2039 smi->idles);
2040 out += sprintf(out, "interrupts: %ld\n",
2041 smi->interrupts);
2042 out += sprintf(out, "attentions: %ld\n",
2043 smi->attentions);
2044 out += sprintf(out, "flag_fetches: %ld\n",
2045 smi->flag_fetches);
2046 out += sprintf(out, "hosed_count: %ld\n",
2047 smi->hosed_count);
2048 out += sprintf(out, "complete_transactions: %ld\n",
2049 smi->complete_transactions);
2050 out += sprintf(out, "events: %ld\n",
2051 smi->events);
2052 out += sprintf(out, "watchdog_pretimeouts: %ld\n",
2053 smi->watchdog_pretimeouts);
2054 out += sprintf(out, "incoming_messages: %ld\n",
2055 smi->incoming_messages);
2057 return (out - ((char *) page));
2060 /* Returns 0 if initialized, or negative on an error. */
2061 static int init_one_smi(int intf_num, struct smi_info **smi)
2063 int rv;
2064 struct smi_info *new_smi;
2067 rv = try_init_mem(intf_num, &new_smi);
2068 if (rv)
2069 rv = try_init_port(intf_num, &new_smi);
2070 #ifdef CONFIG_ACPI_INTERPRETER
2071 if ((rv) && (si_trydefaults)) {
2072 rv = try_init_acpi(intf_num, &new_smi);
2074 #endif
2075 #ifdef CONFIG_X86
2076 if ((rv) && (si_trydefaults)) {
2077 rv = try_init_smbios(intf_num, &new_smi);
2079 #endif
2080 if ((rv) && (si_trydefaults)) {
2081 rv = try_init_plug_and_play(intf_num, &new_smi);
2085 if (rv)
2086 return rv;
2088 /* So we know not to free it unless we have allocated one. */
2089 new_smi->intf = NULL;
2090 new_smi->si_sm = NULL;
2091 new_smi->handlers = NULL;
2093 if (!new_smi->irq_setup) {
2094 new_smi->irq = irqs[intf_num];
2095 new_smi->irq_setup = std_irq_setup;
2096 new_smi->irq_cleanup = std_irq_cleanup;
2099 /* Default to KCS if no type is specified. */
2100 if (si_type[intf_num] == NULL) {
2101 if (si_trydefaults)
2102 si_type[intf_num] = "kcs";
2103 else {
2104 rv = -EINVAL;
2105 goto out_err;
2109 /* Set up the state machine to use. */
2110 if (strcmp(si_type[intf_num], "kcs") == 0) {
2111 new_smi->handlers = &kcs_smi_handlers;
2112 new_smi->si_type = SI_KCS;
2113 } else if (strcmp(si_type[intf_num], "smic") == 0) {
2114 new_smi->handlers = &smic_smi_handlers;
2115 new_smi->si_type = SI_SMIC;
2116 } else if (strcmp(si_type[intf_num], "bt") == 0) {
2117 new_smi->handlers = &bt_smi_handlers;
2118 new_smi->si_type = SI_BT;
2119 } else {
2120 /* No support for anything else yet. */
2121 rv = -EIO;
2122 goto out_err;
2125 /* Allocate the state machine's data and initialize it. */
2126 new_smi->si_sm = kmalloc(new_smi->handlers->size(), GFP_KERNEL);
2127 if (!new_smi->si_sm) {
2128 printk(" Could not allocate state machine memory\n");
2129 rv = -ENOMEM;
2130 goto out_err;
2132 new_smi->io_size = new_smi->handlers->init_data(new_smi->si_sm,
2133 &new_smi->io);
2135 /* Now that we know the I/O size, we can set up the I/O. */
2136 rv = new_smi->io_setup(new_smi);
2137 if (rv) {
2138 printk(" Could not set up I/O space\n");
2139 goto out_err;
2142 spin_lock_init(&(new_smi->si_lock));
2143 spin_lock_init(&(new_smi->msg_lock));
2144 spin_lock_init(&(new_smi->count_lock));
2146 /* Do low-level detection first. */
2147 if (new_smi->handlers->detect(new_smi->si_sm)) {
2148 rv = -ENODEV;
2149 goto out_err;
2152 /* Attempt a get device id command. If it fails, we probably
2153 don't have a SMI here. */
2154 rv = try_get_dev_id(new_smi);
2155 if (rv)
2156 goto out_err;
2158 /* Try to claim any interrupts. */
2159 new_smi->irq_setup(new_smi);
2161 INIT_LIST_HEAD(&(new_smi->xmit_msgs));
2162 INIT_LIST_HEAD(&(new_smi->hp_xmit_msgs));
2163 new_smi->curr_msg = NULL;
2164 atomic_set(&new_smi->req_events, 0);
2165 new_smi->run_to_completion = 0;
2167 new_smi->interrupt_disabled = 0;
2168 new_smi->timer_stopped = 0;
2169 new_smi->stop_operation = 0;
2171 /* Start clearing the flags before we enable interrupts or the
2172 timer to avoid racing with the timer. */
2173 start_clear_flags(new_smi);
2174 /* IRQ is defined to be set when non-zero. */
2175 if (new_smi->irq)
2176 new_smi->si_state = SI_CLEARING_FLAGS_THEN_SET_IRQ;
2178 /* The ipmi_register_smi() code does some operations to
2179 determine the channel information, so we must be ready to
2180 handle operations before it is called. This means we have
2181 to stop the timer if we get an error after this point. */
2182 init_timer(&(new_smi->si_timer));
2183 new_smi->si_timer.data = (long) new_smi;
2184 new_smi->si_timer.function = smi_timeout;
2185 new_smi->last_timeout_jiffies = jiffies;
2186 new_smi->si_timer.expires = jiffies + SI_TIMEOUT_JIFFIES;
2187 add_timer(&(new_smi->si_timer));
2189 rv = ipmi_register_smi(&handlers,
2190 new_smi,
2191 new_smi->ipmi_version_major,
2192 new_smi->ipmi_version_minor,
2193 new_smi->slave_addr,
2194 &(new_smi->intf));
2195 if (rv) {
2196 printk(KERN_ERR
2197 "ipmi_si: Unable to register device: error %d\n",
2198 rv);
2199 goto out_err_stop_timer;
2202 rv = ipmi_smi_add_proc_entry(new_smi->intf, "type",
2203 type_file_read_proc, NULL,
2204 new_smi, THIS_MODULE);
2205 if (rv) {
2206 printk(KERN_ERR
2207 "ipmi_si: Unable to create proc entry: %d\n",
2208 rv);
2209 goto out_err_stop_timer;
2212 rv = ipmi_smi_add_proc_entry(new_smi->intf, "si_stats",
2213 stat_file_read_proc, NULL,
2214 new_smi, THIS_MODULE);
2215 if (rv) {
2216 printk(KERN_ERR
2217 "ipmi_si: Unable to create proc entry: %d\n",
2218 rv);
2219 goto out_err_stop_timer;
2222 *smi = new_smi;
2224 printk(" IPMI %s interface initialized\n", si_type[intf_num]);
2226 return 0;
2228 out_err_stop_timer:
2229 new_smi->stop_operation = 1;
2231 /* Wait for the timer to stop. This avoids problems with race
2232 conditions removing the timer here. */
2233 while (!new_smi->timer_stopped) {
2234 set_current_state(TASK_UNINTERRUPTIBLE);
2235 schedule_timeout(1);
2238 out_err:
2239 if (new_smi->intf)
2240 ipmi_unregister_smi(new_smi->intf);
2242 new_smi->irq_cleanup(new_smi);
2244 /* Wait until we know that we are out of any interrupt
2245 handlers might have been running before we freed the
2246 interrupt. */
2247 synchronize_sched();
2249 if (new_smi->si_sm) {
2250 if (new_smi->handlers)
2251 new_smi->handlers->cleanup(new_smi->si_sm);
2252 kfree(new_smi->si_sm);
2254 new_smi->io_cleanup(new_smi);
2256 return rv;
2259 static __init int init_ipmi_si(void)
2261 int rv = 0;
2262 int pos = 0;
2263 int i;
2264 char *str;
2266 if (initialized)
2267 return 0;
2268 initialized = 1;
2270 /* Parse out the si_type string into its components. */
2271 str = si_type_str;
2272 if (*str != '\0') {
2273 for (i=0; (i<SI_MAX_PARMS) && (*str != '\0'); i++) {
2274 si_type[i] = str;
2275 str = strchr(str, ',');
2276 if (str) {
2277 *str = '\0';
2278 str++;
2279 } else {
2280 break;
2285 printk(KERN_INFO "IPMI System Interface driver version "
2286 IPMI_SI_VERSION);
2287 if (kcs_smi_handlers.version)
2288 printk(", KCS version %s", kcs_smi_handlers.version);
2289 if (smic_smi_handlers.version)
2290 printk(", SMIC version %s", smic_smi_handlers.version);
2291 if (bt_smi_handlers.version)
2292 printk(", BT version %s", bt_smi_handlers.version);
2293 printk("\n");
2295 #ifdef CONFIG_X86
2296 dmi_decode();
2297 #endif
2299 rv = init_one_smi(0, &(smi_infos[pos]));
2300 if (rv && !ports[0] && si_trydefaults) {
2301 /* If we are trying defaults and the initial port is
2302 not set, then set it. */
2303 si_type[0] = "kcs";
2304 ports[0] = DEFAULT_KCS_IO_PORT;
2305 rv = init_one_smi(0, &(smi_infos[pos]));
2306 if (rv) {
2307 /* No KCS - try SMIC */
2308 si_type[0] = "smic";
2309 ports[0] = DEFAULT_SMIC_IO_PORT;
2310 rv = init_one_smi(0, &(smi_infos[pos]));
2312 if (rv) {
2313 /* No SMIC - try BT */
2314 si_type[0] = "bt";
2315 ports[0] = DEFAULT_BT_IO_PORT;
2316 rv = init_one_smi(0, &(smi_infos[pos]));
2319 if (rv == 0)
2320 pos++;
2322 for (i=1; i < SI_MAX_PARMS; i++) {
2323 rv = init_one_smi(i, &(smi_infos[pos]));
2324 if (rv == 0)
2325 pos++;
2328 if (smi_infos[0] == NULL) {
2329 printk("ipmi_si: Unable to find any System Interface(s)\n");
2330 return -ENODEV;
2333 return 0;
2335 module_init(init_ipmi_si);
2337 static void __exit cleanup_one_si(struct smi_info *to_clean)
2339 int rv;
2340 unsigned long flags;
2342 if (! to_clean)
2343 return;
2345 /* Tell the timer and interrupt handlers that we are shutting
2346 down. */
2347 spin_lock_irqsave(&(to_clean->si_lock), flags);
2348 spin_lock(&(to_clean->msg_lock));
2350 to_clean->stop_operation = 1;
2352 to_clean->irq_cleanup(to_clean);
2354 spin_unlock(&(to_clean->msg_lock));
2355 spin_unlock_irqrestore(&(to_clean->si_lock), flags);
2357 /* Wait until we know that we are out of any interrupt
2358 handlers might have been running before we freed the
2359 interrupt. */
2360 synchronize_sched();
2362 /* Wait for the timer to stop. This avoids problems with race
2363 conditions removing the timer here. */
2364 while (!to_clean->timer_stopped) {
2365 set_current_state(TASK_UNINTERRUPTIBLE);
2366 schedule_timeout(1);
2369 /* Interrupts and timeouts are stopped, now make sure the
2370 interface is in a clean state. */
2371 while ((to_clean->curr_msg) || (to_clean->si_state != SI_NORMAL)) {
2372 poll(to_clean);
2373 set_current_state(TASK_UNINTERRUPTIBLE);
2374 schedule_timeout(1);
2377 rv = ipmi_unregister_smi(to_clean->intf);
2378 if (rv) {
2379 printk(KERN_ERR
2380 "ipmi_si: Unable to unregister device: errno=%d\n",
2381 rv);
2384 to_clean->handlers->cleanup(to_clean->si_sm);
2386 kfree(to_clean->si_sm);
2388 to_clean->io_cleanup(to_clean);
2391 static __exit void cleanup_ipmi_si(void)
2393 int i;
2395 if (!initialized)
2396 return;
2398 for (i=0; i<SI_MAX_DRIVERS; i++) {
2399 cleanup_one_si(smi_infos[i]);
2402 module_exit(cleanup_ipmi_si);
2404 MODULE_LICENSE("GPL");