4 * The interface to the IPMI driver for the system interfaces (KCS, SMIC,
7 * Author: MontaVista Software, Inc.
8 * Corey Minyard <minyard@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>
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
62 static inline void add_usec_to_timer(struct timer_list *t, long v)
64 t->arch_cycle_expires += nsec_to_arch_cycle(v * 1000);
65 while (t->arch_cycle_expires >= arch_cycles_per_jiffy)
68 t->arch_cycle_expires -= arch_cycles_per_jiffy;
72 #include <linux/interrupt.h>
73 #include <linux/rcupdate.h>
74 #include <linux/ipmi_smi.h>
76 #include "ipmi_si_sm.h"
77 #include <linux/init.h>
78 #include <linux/dmi.h>
80 #define IPMI_SI_VERSION "v33"
82 /* Measure times between events in the driver. */
85 /* Call every 10 ms. */
86 #define SI_TIMEOUT_TIME_USEC 10000
87 #define SI_USEC_PER_JIFFY (1000000/HZ)
88 #define SI_TIMEOUT_JIFFIES (SI_TIMEOUT_TIME_USEC/SI_USEC_PER_JIFFY)
89 #define SI_SHORT_TIMEOUT_USEC 250 /* .25ms when the SM request a
97 SI_CLEARING_FLAGS_THEN_SET_IRQ,
99 SI_ENABLE_INTERRUPTS1,
100 SI_ENABLE_INTERRUPTS2
101 /* FIXME - add watchdog stuff. */
104 /* Some BT-specific defines we need here. */
105 #define IPMI_BT_INTMASK_REG 2
106 #define IPMI_BT_INTMASK_CLEAR_IRQ_BIT 2
107 #define IPMI_BT_INTMASK_ENABLE_IRQ_BIT 1
110 SI_KCS, SI_SMIC, SI_BT
116 struct si_sm_data *si_sm;
117 struct si_sm_handlers *handlers;
118 enum si_type si_type;
121 struct list_head xmit_msgs;
122 struct list_head hp_xmit_msgs;
123 struct ipmi_smi_msg *curr_msg;
124 enum si_intf_state si_state;
126 /* Used to handle the various types of I/O that can occur with
129 int (*io_setup)(struct smi_info *info);
130 void (*io_cleanup)(struct smi_info *info);
131 int (*irq_setup)(struct smi_info *info);
132 void (*irq_cleanup)(struct smi_info *info);
133 unsigned int io_size;
135 /* Flags from the last GET_MSG_FLAGS command, used when an ATTN
136 is set to hold the flags until we are done handling everything
138 #define RECEIVE_MSG_AVAIL 0x01
139 #define EVENT_MSG_BUFFER_FULL 0x02
140 #define WDT_PRE_TIMEOUT_INT 0x08
141 unsigned char msg_flags;
143 /* If set to true, this will request events the next time the
144 state machine is idle. */
147 /* If true, run the state machine to completion on every send
148 call. Generally used after a panic to make sure stuff goes
150 int run_to_completion;
152 /* The I/O port of an SI interface. */
155 /* The space between start addresses of the two ports. For
156 instance, if the first port is 0xca2 and the spacing is 4, then
157 the second port is 0xca6. */
158 unsigned int spacing;
160 /* zero if no irq; */
163 /* The timer for this si. */
164 struct timer_list si_timer;
166 /* The time (in jiffies) the last timeout occurred at. */
167 unsigned long last_timeout_jiffies;
169 /* Used to gracefully stop the timer without race conditions. */
170 volatile int stop_operation;
171 volatile int timer_stopped;
173 /* The driver will disable interrupts when it gets into a
174 situation where it cannot handle messages due to lack of
175 memory. Once that situation clears up, it will re-enable
177 int interrupt_disabled;
179 unsigned char ipmi_si_dev_rev;
180 unsigned char ipmi_si_fw_rev_major;
181 unsigned char ipmi_si_fw_rev_minor;
182 unsigned char ipmi_version_major;
183 unsigned char ipmi_version_minor;
185 /* Slave address, could be reported from DMI. */
186 unsigned char slave_addr;
188 /* Counters and things for the proc filesystem. */
189 spinlock_t count_lock;
190 unsigned long short_timeouts;
191 unsigned long long_timeouts;
192 unsigned long timeout_restarts;
194 unsigned long interrupts;
195 unsigned long attentions;
196 unsigned long flag_fetches;
197 unsigned long hosed_count;
198 unsigned long complete_transactions;
199 unsigned long events;
200 unsigned long watchdog_pretimeouts;
201 unsigned long incoming_messages;
204 static void si_restart_short_timer(struct smi_info *smi_info);
206 static void deliver_recv_msg(struct smi_info *smi_info,
207 struct ipmi_smi_msg *msg)
209 /* Deliver the message to the upper layer with the lock
211 spin_unlock(&(smi_info->si_lock));
212 ipmi_smi_msg_received(smi_info->intf, msg);
213 spin_lock(&(smi_info->si_lock));
216 static void return_hosed_msg(struct smi_info *smi_info)
218 struct ipmi_smi_msg *msg = smi_info->curr_msg;
220 /* Make it a reponse */
221 msg->rsp[0] = msg->data[0] | 4;
222 msg->rsp[1] = msg->data[1];
223 msg->rsp[2] = 0xFF; /* Unknown error. */
226 smi_info->curr_msg = NULL;
227 deliver_recv_msg(smi_info, msg);
230 static enum si_sm_result start_next_msg(struct smi_info *smi_info)
233 struct list_head *entry = NULL;
238 /* No need to save flags, we aleady have interrupts off and we
239 already hold the SMI lock. */
240 spin_lock(&(smi_info->msg_lock));
242 /* Pick the high priority queue first. */
243 if (! list_empty(&(smi_info->hp_xmit_msgs))) {
244 entry = smi_info->hp_xmit_msgs.next;
245 } else if (! list_empty(&(smi_info->xmit_msgs))) {
246 entry = smi_info->xmit_msgs.next;
250 smi_info->curr_msg = NULL;
256 smi_info->curr_msg = list_entry(entry,
261 printk("**Start2: %d.%9.9d\n", t.tv_sec, t.tv_usec);
263 err = smi_info->handlers->start_transaction(
265 smi_info->curr_msg->data,
266 smi_info->curr_msg->data_size);
268 return_hosed_msg(smi_info);
271 rv = SI_SM_CALL_WITHOUT_DELAY;
273 spin_unlock(&(smi_info->msg_lock));
278 static void start_enable_irq(struct smi_info *smi_info)
280 unsigned char msg[2];
282 /* If we are enabling interrupts, we have to tell the
284 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
285 msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
287 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
288 smi_info->si_state = SI_ENABLE_INTERRUPTS1;
291 static void start_clear_flags(struct smi_info *smi_info)
293 unsigned char msg[3];
295 /* Make sure the watchdog pre-timeout flag is not set at startup. */
296 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
297 msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
298 msg[2] = WDT_PRE_TIMEOUT_INT;
300 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 3);
301 smi_info->si_state = SI_CLEARING_FLAGS;
304 /* When we have a situtaion where we run out of memory and cannot
305 allocate messages, we just leave them in the BMC and run the system
306 polled until we can allocate some memory. Once we have some
307 memory, we will re-enable the interrupt. */
308 static inline void disable_si_irq(struct smi_info *smi_info)
310 if ((smi_info->irq) && (!smi_info->interrupt_disabled)) {
311 disable_irq_nosync(smi_info->irq);
312 smi_info->interrupt_disabled = 1;
316 static inline void enable_si_irq(struct smi_info *smi_info)
318 if ((smi_info->irq) && (smi_info->interrupt_disabled)) {
319 enable_irq(smi_info->irq);
320 smi_info->interrupt_disabled = 0;
324 static void handle_flags(struct smi_info *smi_info)
326 if (smi_info->msg_flags & WDT_PRE_TIMEOUT_INT) {
327 /* Watchdog pre-timeout */
328 spin_lock(&smi_info->count_lock);
329 smi_info->watchdog_pretimeouts++;
330 spin_unlock(&smi_info->count_lock);
332 start_clear_flags(smi_info);
333 smi_info->msg_flags &= ~WDT_PRE_TIMEOUT_INT;
334 spin_unlock(&(smi_info->si_lock));
335 ipmi_smi_watchdog_pretimeout(smi_info->intf);
336 spin_lock(&(smi_info->si_lock));
337 } else if (smi_info->msg_flags & RECEIVE_MSG_AVAIL) {
338 /* Messages available. */
339 smi_info->curr_msg = ipmi_alloc_smi_msg();
340 if (!smi_info->curr_msg) {
341 disable_si_irq(smi_info);
342 smi_info->si_state = SI_NORMAL;
345 enable_si_irq(smi_info);
347 smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
348 smi_info->curr_msg->data[1] = IPMI_GET_MSG_CMD;
349 smi_info->curr_msg->data_size = 2;
351 smi_info->handlers->start_transaction(
353 smi_info->curr_msg->data,
354 smi_info->curr_msg->data_size);
355 smi_info->si_state = SI_GETTING_MESSAGES;
356 } else if (smi_info->msg_flags & EVENT_MSG_BUFFER_FULL) {
357 /* Events available. */
358 smi_info->curr_msg = ipmi_alloc_smi_msg();
359 if (!smi_info->curr_msg) {
360 disable_si_irq(smi_info);
361 smi_info->si_state = SI_NORMAL;
364 enable_si_irq(smi_info);
366 smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
367 smi_info->curr_msg->data[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD;
368 smi_info->curr_msg->data_size = 2;
370 smi_info->handlers->start_transaction(
372 smi_info->curr_msg->data,
373 smi_info->curr_msg->data_size);
374 smi_info->si_state = SI_GETTING_EVENTS;
376 smi_info->si_state = SI_NORMAL;
380 static void handle_transaction_done(struct smi_info *smi_info)
382 struct ipmi_smi_msg *msg;
387 printk("**Done: %d.%9.9d\n", t.tv_sec, t.tv_usec);
389 switch (smi_info->si_state) {
391 if (!smi_info->curr_msg)
394 smi_info->curr_msg->rsp_size
395 = smi_info->handlers->get_result(
397 smi_info->curr_msg->rsp,
398 IPMI_MAX_MSG_LENGTH);
400 /* Do this here becase deliver_recv_msg() releases the
401 lock, and a new message can be put in during the
402 time the lock is released. */
403 msg = smi_info->curr_msg;
404 smi_info->curr_msg = NULL;
405 deliver_recv_msg(smi_info, msg);
408 case SI_GETTING_FLAGS:
410 unsigned char msg[4];
413 /* We got the flags from the SMI, now handle them. */
414 len = smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
416 /* Error fetching flags, just give up for
418 smi_info->si_state = SI_NORMAL;
419 } else if (len < 4) {
420 /* Hmm, no flags. That's technically illegal, but
421 don't use uninitialized data. */
422 smi_info->si_state = SI_NORMAL;
424 smi_info->msg_flags = msg[3];
425 handle_flags(smi_info);
430 case SI_CLEARING_FLAGS:
431 case SI_CLEARING_FLAGS_THEN_SET_IRQ:
433 unsigned char msg[3];
435 /* We cleared the flags. */
436 smi_info->handlers->get_result(smi_info->si_sm, msg, 3);
438 /* Error clearing flags */
440 "ipmi_si: Error clearing flags: %2.2x\n",
443 if (smi_info->si_state == SI_CLEARING_FLAGS_THEN_SET_IRQ)
444 start_enable_irq(smi_info);
446 smi_info->si_state = SI_NORMAL;
450 case SI_GETTING_EVENTS:
452 smi_info->curr_msg->rsp_size
453 = smi_info->handlers->get_result(
455 smi_info->curr_msg->rsp,
456 IPMI_MAX_MSG_LENGTH);
458 /* Do this here becase deliver_recv_msg() releases the
459 lock, and a new message can be put in during the
460 time the lock is released. */
461 msg = smi_info->curr_msg;
462 smi_info->curr_msg = NULL;
463 if (msg->rsp[2] != 0) {
464 /* Error getting event, probably done. */
467 /* Take off the event flag. */
468 smi_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
469 handle_flags(smi_info);
471 spin_lock(&smi_info->count_lock);
473 spin_unlock(&smi_info->count_lock);
475 /* Do this before we deliver the message
476 because delivering the message releases the
477 lock and something else can mess with the
479 handle_flags(smi_info);
481 deliver_recv_msg(smi_info, msg);
486 case SI_GETTING_MESSAGES:
488 smi_info->curr_msg->rsp_size
489 = smi_info->handlers->get_result(
491 smi_info->curr_msg->rsp,
492 IPMI_MAX_MSG_LENGTH);
494 /* Do this here becase deliver_recv_msg() releases the
495 lock, and a new message can be put in during the
496 time the lock is released. */
497 msg = smi_info->curr_msg;
498 smi_info->curr_msg = NULL;
499 if (msg->rsp[2] != 0) {
500 /* Error getting event, probably done. */
503 /* Take off the msg flag. */
504 smi_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
505 handle_flags(smi_info);
507 spin_lock(&smi_info->count_lock);
508 smi_info->incoming_messages++;
509 spin_unlock(&smi_info->count_lock);
511 /* Do this before we deliver the message
512 because delivering the message releases the
513 lock and something else can mess with the
515 handle_flags(smi_info);
517 deliver_recv_msg(smi_info, msg);
522 case SI_ENABLE_INTERRUPTS1:
524 unsigned char msg[4];
526 /* We got the flags from the SMI, now handle them. */
527 smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
530 "ipmi_si: Could not enable interrupts"
531 ", failed get, using polled mode.\n");
532 smi_info->si_state = SI_NORMAL;
534 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
535 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
536 msg[2] = msg[3] | 1; /* enable msg queue int */
537 smi_info->handlers->start_transaction(
538 smi_info->si_sm, msg, 3);
539 smi_info->si_state = SI_ENABLE_INTERRUPTS2;
544 case SI_ENABLE_INTERRUPTS2:
546 unsigned char msg[4];
548 /* We got the flags from the SMI, now handle them. */
549 smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
552 "ipmi_si: Could not enable interrupts"
553 ", failed set, using polled mode.\n");
555 smi_info->si_state = SI_NORMAL;
561 /* Called on timeouts and events. Timeouts should pass the elapsed
562 time, interrupts should pass in zero. */
563 static enum si_sm_result smi_event_handler(struct smi_info *smi_info,
566 enum si_sm_result si_sm_result;
569 /* There used to be a loop here that waited a little while
570 (around 25us) before giving up. That turned out to be
571 pointless, the minimum delays I was seeing were in the 300us
572 range, which is far too long to wait in an interrupt. So
573 we just run until the state machine tells us something
574 happened or it needs a delay. */
575 si_sm_result = smi_info->handlers->event(smi_info->si_sm, time);
577 while (si_sm_result == SI_SM_CALL_WITHOUT_DELAY)
579 si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
582 if (si_sm_result == SI_SM_TRANSACTION_COMPLETE)
584 spin_lock(&smi_info->count_lock);
585 smi_info->complete_transactions++;
586 spin_unlock(&smi_info->count_lock);
588 handle_transaction_done(smi_info);
589 si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
591 else if (si_sm_result == SI_SM_HOSED)
593 spin_lock(&smi_info->count_lock);
594 smi_info->hosed_count++;
595 spin_unlock(&smi_info->count_lock);
597 /* Do the before return_hosed_msg, because that
598 releases the lock. */
599 smi_info->si_state = SI_NORMAL;
600 if (smi_info->curr_msg != NULL) {
601 /* If we were handling a user message, format
602 a response to send to the upper layer to
603 tell it about the error. */
604 return_hosed_msg(smi_info);
606 si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
609 /* We prefer handling attn over new messages. */
610 if (si_sm_result == SI_SM_ATTN)
612 unsigned char msg[2];
614 spin_lock(&smi_info->count_lock);
615 smi_info->attentions++;
616 spin_unlock(&smi_info->count_lock);
618 /* Got a attn, send down a get message flags to see
619 what's causing it. It would be better to handle
620 this in the upper layer, but due to the way
621 interrupts work with the SMI, that's not really
623 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
624 msg[1] = IPMI_GET_MSG_FLAGS_CMD;
626 smi_info->handlers->start_transaction(
627 smi_info->si_sm, msg, 2);
628 smi_info->si_state = SI_GETTING_FLAGS;
632 /* If we are currently idle, try to start the next message. */
633 if (si_sm_result == SI_SM_IDLE) {
634 spin_lock(&smi_info->count_lock);
636 spin_unlock(&smi_info->count_lock);
638 si_sm_result = start_next_msg(smi_info);
639 if (si_sm_result != SI_SM_IDLE)
643 if ((si_sm_result == SI_SM_IDLE)
644 && (atomic_read(&smi_info->req_events)))
646 /* We are idle and the upper layer requested that I fetch
648 unsigned char msg[2];
650 spin_lock(&smi_info->count_lock);
651 smi_info->flag_fetches++;
652 spin_unlock(&smi_info->count_lock);
654 atomic_set(&smi_info->req_events, 0);
655 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
656 msg[1] = IPMI_GET_MSG_FLAGS_CMD;
658 smi_info->handlers->start_transaction(
659 smi_info->si_sm, msg, 2);
660 smi_info->si_state = SI_GETTING_FLAGS;
667 static void sender(void *send_info,
668 struct ipmi_smi_msg *msg,
671 struct smi_info *smi_info = send_info;
672 enum si_sm_result result;
678 spin_lock_irqsave(&(smi_info->msg_lock), flags);
681 printk("**Enqueue: %d.%9.9d\n", t.tv_sec, t.tv_usec);
684 if (smi_info->run_to_completion) {
685 /* If we are running to completion, then throw it in
686 the list and run transactions until everything is
687 clear. Priority doesn't matter here. */
688 list_add_tail(&(msg->link), &(smi_info->xmit_msgs));
690 /* We have to release the msg lock and claim the smi
691 lock in this case, because of race conditions. */
692 spin_unlock_irqrestore(&(smi_info->msg_lock), flags);
694 spin_lock_irqsave(&(smi_info->si_lock), flags);
695 result = smi_event_handler(smi_info, 0);
696 while (result != SI_SM_IDLE) {
697 udelay(SI_SHORT_TIMEOUT_USEC);
698 result = smi_event_handler(smi_info,
699 SI_SHORT_TIMEOUT_USEC);
701 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
705 list_add_tail(&(msg->link), &(smi_info->hp_xmit_msgs));
707 list_add_tail(&(msg->link), &(smi_info->xmit_msgs));
710 spin_unlock_irqrestore(&(smi_info->msg_lock), flags);
712 spin_lock_irqsave(&(smi_info->si_lock), flags);
713 if ((smi_info->si_state == SI_NORMAL)
714 && (smi_info->curr_msg == NULL))
716 start_next_msg(smi_info);
717 si_restart_short_timer(smi_info);
719 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
722 static void set_run_to_completion(void *send_info, int i_run_to_completion)
724 struct smi_info *smi_info = send_info;
725 enum si_sm_result result;
728 spin_lock_irqsave(&(smi_info->si_lock), flags);
730 smi_info->run_to_completion = i_run_to_completion;
731 if (i_run_to_completion) {
732 result = smi_event_handler(smi_info, 0);
733 while (result != SI_SM_IDLE) {
734 udelay(SI_SHORT_TIMEOUT_USEC);
735 result = smi_event_handler(smi_info,
736 SI_SHORT_TIMEOUT_USEC);
740 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
743 static void poll(void *send_info)
745 struct smi_info *smi_info = send_info;
747 smi_event_handler(smi_info, 0);
750 static void request_events(void *send_info)
752 struct smi_info *smi_info = send_info;
754 atomic_set(&smi_info->req_events, 1);
757 static int initialized = 0;
759 /* Must be called with interrupts off and with the si_lock held. */
760 static void si_restart_short_timer(struct smi_info *smi_info)
762 #if defined(CONFIG_HIGH_RES_TIMERS)
764 unsigned long jiffies_now;
767 if (del_timer(&(smi_info->si_timer))) {
768 /* If we don't delete the timer, then it will go off
769 immediately, anyway. So we only process if we
770 actually delete the timer. */
773 seq = read_seqbegin_irqsave(&xtime_lock, flags);
774 jiffies_now = jiffies;
775 smi_info->si_timer.expires = jiffies_now;
776 smi_info->si_timer.arch_cycle_expires
777 = get_arch_cycles(jiffies_now);
778 } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
780 add_usec_to_timer(&smi_info->si_timer, SI_SHORT_TIMEOUT_USEC);
782 add_timer(&(smi_info->si_timer));
783 spin_lock_irqsave(&smi_info->count_lock, flags);
784 smi_info->timeout_restarts++;
785 spin_unlock_irqrestore(&smi_info->count_lock, flags);
790 static void smi_timeout(unsigned long data)
792 struct smi_info *smi_info = (struct smi_info *) data;
793 enum si_sm_result smi_result;
795 unsigned long jiffies_now;
796 unsigned long time_diff;
801 if (smi_info->stop_operation) {
802 smi_info->timer_stopped = 1;
806 spin_lock_irqsave(&(smi_info->si_lock), flags);
809 printk("**Timer: %d.%9.9d\n", t.tv_sec, t.tv_usec);
811 jiffies_now = jiffies;
812 time_diff = ((jiffies_now - smi_info->last_timeout_jiffies)
813 * SI_USEC_PER_JIFFY);
814 smi_result = smi_event_handler(smi_info, time_diff);
816 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
818 smi_info->last_timeout_jiffies = jiffies_now;
820 if ((smi_info->irq) && (! smi_info->interrupt_disabled)) {
821 /* Running with interrupts, only do long timeouts. */
822 smi_info->si_timer.expires = jiffies + SI_TIMEOUT_JIFFIES;
823 spin_lock_irqsave(&smi_info->count_lock, flags);
824 smi_info->long_timeouts++;
825 spin_unlock_irqrestore(&smi_info->count_lock, flags);
829 /* If the state machine asks for a short delay, then shorten
830 the timer timeout. */
831 if (smi_result == SI_SM_CALL_WITH_DELAY) {
832 #if defined(CONFIG_HIGH_RES_TIMERS)
835 spin_lock_irqsave(&smi_info->count_lock, flags);
836 smi_info->short_timeouts++;
837 spin_unlock_irqrestore(&smi_info->count_lock, flags);
838 #if defined(CONFIG_HIGH_RES_TIMERS)
840 seq = read_seqbegin_irqsave(&xtime_lock, flags);
841 smi_info->si_timer.expires = jiffies;
842 smi_info->si_timer.arch_cycle_expires
843 = get_arch_cycles(smi_info->si_timer.expires);
844 } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
845 add_usec_to_timer(&smi_info->si_timer, SI_SHORT_TIMEOUT_USEC);
847 smi_info->si_timer.expires = jiffies + 1;
850 spin_lock_irqsave(&smi_info->count_lock, flags);
851 smi_info->long_timeouts++;
852 spin_unlock_irqrestore(&smi_info->count_lock, flags);
853 smi_info->si_timer.expires = jiffies + SI_TIMEOUT_JIFFIES;
854 #if defined(CONFIG_HIGH_RES_TIMERS)
855 smi_info->si_timer.arch_cycle_expires = 0;
860 add_timer(&(smi_info->si_timer));
863 static irqreturn_t si_irq_handler(int irq, void *data, struct pt_regs *regs)
865 struct smi_info *smi_info = data;
871 spin_lock_irqsave(&(smi_info->si_lock), flags);
873 spin_lock(&smi_info->count_lock);
874 smi_info->interrupts++;
875 spin_unlock(&smi_info->count_lock);
877 if (smi_info->stop_operation)
882 printk("**Interrupt: %d.%9.9d\n", t.tv_sec, t.tv_usec);
884 smi_event_handler(smi_info, 0);
886 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
890 static irqreturn_t si_bt_irq_handler(int irq, void *data, struct pt_regs *regs)
892 struct smi_info *smi_info = data;
893 /* We need to clear the IRQ flag for the BT interface. */
894 smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG,
895 IPMI_BT_INTMASK_CLEAR_IRQ_BIT
896 | IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
897 return si_irq_handler(irq, data, regs);
901 static struct ipmi_smi_handlers handlers =
903 .owner = THIS_MODULE,
905 .request_events = request_events,
906 .set_run_to_completion = set_run_to_completion,
910 /* There can be 4 IO ports passed in (with or without IRQs), 4 addresses,
911 a default IO port, and 1 ACPI/SPMI address. That sets SI_MAX_DRIVERS */
913 #define SI_MAX_PARMS 4
914 #define SI_MAX_DRIVERS ((SI_MAX_PARMS * 2) + 2)
915 static struct smi_info *smi_infos[SI_MAX_DRIVERS] =
916 { NULL, NULL, NULL, NULL };
918 #define DEVICE_NAME "ipmi_si"
920 #define DEFAULT_KCS_IO_PORT 0xca2
921 #define DEFAULT_SMIC_IO_PORT 0xca9
922 #define DEFAULT_BT_IO_PORT 0xe4
923 #define DEFAULT_REGSPACING 1
925 static int si_trydefaults = 1;
926 static char *si_type[SI_MAX_PARMS];
927 #define MAX_SI_TYPE_STR 30
928 static char si_type_str[MAX_SI_TYPE_STR];
929 static unsigned long addrs[SI_MAX_PARMS];
930 static int num_addrs;
931 static unsigned int ports[SI_MAX_PARMS];
932 static int num_ports;
933 static int irqs[SI_MAX_PARMS];
935 static int regspacings[SI_MAX_PARMS];
936 static int num_regspacings = 0;
937 static int regsizes[SI_MAX_PARMS];
938 static int num_regsizes = 0;
939 static int regshifts[SI_MAX_PARMS];
940 static int num_regshifts = 0;
941 static int slave_addrs[SI_MAX_PARMS];
942 static int num_slave_addrs = 0;
945 module_param_named(trydefaults, si_trydefaults, bool, 0);
946 MODULE_PARM_DESC(trydefaults, "Setting this to 'false' will disable the"
947 " default scan of the KCS and SMIC interface at the standard"
949 module_param_string(type, si_type_str, MAX_SI_TYPE_STR, 0);
950 MODULE_PARM_DESC(type, "Defines the type of each interface, each"
951 " interface separated by commas. The types are 'kcs',"
952 " 'smic', and 'bt'. For example si_type=kcs,bt will set"
953 " the first interface to kcs and the second to bt");
954 module_param_array(addrs, long, &num_addrs, 0);
955 MODULE_PARM_DESC(addrs, "Sets the memory address of each interface, the"
956 " addresses separated by commas. Only use if an interface"
957 " is in memory. Otherwise, set it to zero or leave"
959 module_param_array(ports, int, &num_ports, 0);
960 MODULE_PARM_DESC(ports, "Sets the port address of each interface, the"
961 " addresses separated by commas. Only use if an interface"
962 " is a port. Otherwise, set it to zero or leave"
964 module_param_array(irqs, int, &num_irqs, 0);
965 MODULE_PARM_DESC(irqs, "Sets the interrupt of each interface, the"
966 " addresses separated by commas. Only use if an interface"
967 " has an interrupt. Otherwise, set it to zero or leave"
969 module_param_array(regspacings, int, &num_regspacings, 0);
970 MODULE_PARM_DESC(regspacings, "The number of bytes between the start address"
971 " and each successive register used by the interface. For"
972 " instance, if the start address is 0xca2 and the spacing"
973 " is 2, then the second address is at 0xca4. Defaults"
975 module_param_array(regsizes, int, &num_regsizes, 0);
976 MODULE_PARM_DESC(regsizes, "The size of the specific IPMI register in bytes."
977 " This should generally be 1, 2, 4, or 8 for an 8-bit,"
978 " 16-bit, 32-bit, or 64-bit register. Use this if you"
979 " the 8-bit IPMI register has to be read from a larger"
981 module_param_array(regshifts, int, &num_regshifts, 0);
982 MODULE_PARM_DESC(regshifts, "The amount to shift the data read from the."
983 " IPMI register, in bits. For instance, if the data"
984 " is read from a 32-bit word and the IPMI data is in"
985 " bit 8-15, then the shift would be 8");
986 module_param_array(slave_addrs, int, &num_slave_addrs, 0);
987 MODULE_PARM_DESC(slave_addrs, "Set the default IPMB slave address for"
988 " the controller. Normally this is 0x20, but can be"
989 " overridden by this parm. This is an array indexed"
990 " by interface number.");
993 #define IPMI_MEM_ADDR_SPACE 1
994 #define IPMI_IO_ADDR_SPACE 2
996 #if defined(CONFIG_ACPI_INTERPRETER) || defined(CONFIG_X86) || defined(CONFIG_PCI)
997 static int is_new_interface(int intf, u8 addr_space, unsigned long base_addr)
1001 for (i = 0; i < SI_MAX_PARMS; ++i) {
1002 /* Don't check our address. */
1005 if (si_type[i] != NULL) {
1006 if ((addr_space == IPMI_MEM_ADDR_SPACE &&
1007 base_addr == addrs[i]) ||
1008 (addr_space == IPMI_IO_ADDR_SPACE &&
1009 base_addr == ports[i]))
1020 static int std_irq_setup(struct smi_info *info)
1027 if (info->si_type == SI_BT) {
1028 rv = request_irq(info->irq,
1034 /* Enable the interrupt in the BT interface. */
1035 info->io.outputb(&info->io, IPMI_BT_INTMASK_REG,
1036 IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
1038 rv = request_irq(info->irq,
1045 "ipmi_si: %s unable to claim interrupt %d,"
1046 " running polled\n",
1047 DEVICE_NAME, info->irq);
1050 printk(" Using irq %d\n", info->irq);
1056 static void std_irq_cleanup(struct smi_info *info)
1061 if (info->si_type == SI_BT)
1062 /* Disable the interrupt in the BT interface. */
1063 info->io.outputb(&info->io, IPMI_BT_INTMASK_REG, 0);
1064 free_irq(info->irq, info);
1067 static unsigned char port_inb(struct si_sm_io *io, unsigned int offset)
1069 unsigned int *addr = io->info;
1071 return inb((*addr)+(offset*io->regspacing));
1074 static void port_outb(struct si_sm_io *io, unsigned int offset,
1077 unsigned int *addr = io->info;
1079 outb(b, (*addr)+(offset * io->regspacing));
1082 static unsigned char port_inw(struct si_sm_io *io, unsigned int offset)
1084 unsigned int *addr = io->info;
1086 return (inw((*addr)+(offset * io->regspacing)) >> io->regshift) & 0xff;
1089 static void port_outw(struct si_sm_io *io, unsigned int offset,
1092 unsigned int *addr = io->info;
1094 outw(b << io->regshift, (*addr)+(offset * io->regspacing));
1097 static unsigned char port_inl(struct si_sm_io *io, unsigned int offset)
1099 unsigned int *addr = io->info;
1101 return (inl((*addr)+(offset * io->regspacing)) >> io->regshift) & 0xff;
1104 static void port_outl(struct si_sm_io *io, unsigned int offset,
1107 unsigned int *addr = io->info;
1109 outl(b << io->regshift, (*addr)+(offset * io->regspacing));
1112 static void port_cleanup(struct smi_info *info)
1114 unsigned int *addr = info->io.info;
1117 if (addr && (*addr)) {
1118 mapsize = ((info->io_size * info->io.regspacing)
1119 - (info->io.regspacing - info->io.regsize));
1121 release_region (*addr, mapsize);
1126 static int port_setup(struct smi_info *info)
1128 unsigned int *addr = info->io.info;
1131 if (!addr || (!*addr))
1134 info->io_cleanup = port_cleanup;
1136 /* Figure out the actual inb/inw/inl/etc routine to use based
1137 upon the register size. */
1138 switch (info->io.regsize) {
1140 info->io.inputb = port_inb;
1141 info->io.outputb = port_outb;
1144 info->io.inputb = port_inw;
1145 info->io.outputb = port_outw;
1148 info->io.inputb = port_inl;
1149 info->io.outputb = port_outl;
1152 printk("ipmi_si: Invalid register size: %d\n",
1157 /* Calculate the total amount of memory to claim. This is an
1158 * unusual looking calculation, but it avoids claiming any
1159 * more memory than it has to. It will claim everything
1160 * between the first address to the end of the last full
1162 mapsize = ((info->io_size * info->io.regspacing)
1163 - (info->io.regspacing - info->io.regsize));
1165 if (request_region(*addr, mapsize, DEVICE_NAME) == NULL)
1170 static int try_init_port(int intf_num, struct smi_info **new_info)
1172 struct smi_info *info;
1174 if (!ports[intf_num])
1177 if (!is_new_interface(intf_num, IPMI_IO_ADDR_SPACE,
1181 info = kmalloc(sizeof(*info), GFP_KERNEL);
1183 printk(KERN_ERR "ipmi_si: Could not allocate SI data (1)\n");
1186 memset(info, 0, sizeof(*info));
1188 info->io_setup = port_setup;
1189 info->io.info = &(ports[intf_num]);
1190 info->io.addr = NULL;
1191 info->io.regspacing = regspacings[intf_num];
1192 if (!info->io.regspacing)
1193 info->io.regspacing = DEFAULT_REGSPACING;
1194 info->io.regsize = regsizes[intf_num];
1195 if (!info->io.regsize)
1196 info->io.regsize = DEFAULT_REGSPACING;
1197 info->io.regshift = regshifts[intf_num];
1199 info->irq_setup = NULL;
1202 if (si_type[intf_num] == NULL)
1203 si_type[intf_num] = "kcs";
1205 printk("ipmi_si: Trying \"%s\" at I/O port 0x%x\n",
1206 si_type[intf_num], ports[intf_num]);
1210 static unsigned char mem_inb(struct si_sm_io *io, unsigned int offset)
1212 return readb((io->addr)+(offset * io->regspacing));
1215 static void mem_outb(struct si_sm_io *io, unsigned int offset,
1218 writeb(b, (io->addr)+(offset * io->regspacing));
1221 static unsigned char mem_inw(struct si_sm_io *io, unsigned int offset)
1223 return (readw((io->addr)+(offset * io->regspacing)) >> io->regshift)
1227 static void mem_outw(struct si_sm_io *io, unsigned int offset,
1230 writeb(b << io->regshift, (io->addr)+(offset * io->regspacing));
1233 static unsigned char mem_inl(struct si_sm_io *io, unsigned int offset)
1235 return (readl((io->addr)+(offset * io->regspacing)) >> io->regshift)
1239 static void mem_outl(struct si_sm_io *io, unsigned int offset,
1242 writel(b << io->regshift, (io->addr)+(offset * io->regspacing));
1246 static unsigned char mem_inq(struct si_sm_io *io, unsigned int offset)
1248 return (readq((io->addr)+(offset * io->regspacing)) >> io->regshift)
1252 static void mem_outq(struct si_sm_io *io, unsigned int offset,
1255 writeq(b << io->regshift, (io->addr)+(offset * io->regspacing));
1259 static void mem_cleanup(struct smi_info *info)
1261 unsigned long *addr = info->io.info;
1264 if (info->io.addr) {
1265 iounmap(info->io.addr);
1267 mapsize = ((info->io_size * info->io.regspacing)
1268 - (info->io.regspacing - info->io.regsize));
1270 release_mem_region(*addr, mapsize);
1275 static int mem_setup(struct smi_info *info)
1277 unsigned long *addr = info->io.info;
1280 if (!addr || (!*addr))
1283 info->io_cleanup = mem_cleanup;
1285 /* Figure out the actual readb/readw/readl/etc routine to use based
1286 upon the register size. */
1287 switch (info->io.regsize) {
1289 info->io.inputb = mem_inb;
1290 info->io.outputb = mem_outb;
1293 info->io.inputb = mem_inw;
1294 info->io.outputb = mem_outw;
1297 info->io.inputb = mem_inl;
1298 info->io.outputb = mem_outl;
1302 info->io.inputb = mem_inq;
1303 info->io.outputb = mem_outq;
1307 printk("ipmi_si: Invalid register size: %d\n",
1312 /* Calculate the total amount of memory to claim. This is an
1313 * unusual looking calculation, but it avoids claiming any
1314 * more memory than it has to. It will claim everything
1315 * between the first address to the end of the last full
1317 mapsize = ((info->io_size * info->io.regspacing)
1318 - (info->io.regspacing - info->io.regsize));
1320 if (request_mem_region(*addr, mapsize, DEVICE_NAME) == NULL)
1323 info->io.addr = ioremap(*addr, mapsize);
1324 if (info->io.addr == NULL) {
1325 release_mem_region(*addr, mapsize);
1331 static int try_init_mem(int intf_num, struct smi_info **new_info)
1333 struct smi_info *info;
1335 if (!addrs[intf_num])
1338 if (!is_new_interface(intf_num, IPMI_MEM_ADDR_SPACE,
1342 info = kmalloc(sizeof(*info), GFP_KERNEL);
1344 printk(KERN_ERR "ipmi_si: Could not allocate SI data (2)\n");
1347 memset(info, 0, sizeof(*info));
1349 info->io_setup = mem_setup;
1350 info->io.info = &addrs[intf_num];
1351 info->io.addr = NULL;
1352 info->io.regspacing = regspacings[intf_num];
1353 if (!info->io.regspacing)
1354 info->io.regspacing = DEFAULT_REGSPACING;
1355 info->io.regsize = regsizes[intf_num];
1356 if (!info->io.regsize)
1357 info->io.regsize = DEFAULT_REGSPACING;
1358 info->io.regshift = regshifts[intf_num];
1360 info->irq_setup = NULL;
1363 if (si_type[intf_num] == NULL)
1364 si_type[intf_num] = "kcs";
1366 printk("ipmi_si: Trying \"%s\" at memory address 0x%lx\n",
1367 si_type[intf_num], addrs[intf_num]);
1372 #ifdef CONFIG_ACPI_INTERPRETER
1374 #include <linux/acpi.h>
1376 /* Once we get an ACPI failure, we don't try any more, because we go
1377 through the tables sequentially. Once we don't find a table, there
1379 static int acpi_failure = 0;
1381 /* For GPE-type interrupts. */
1382 static u32 ipmi_acpi_gpe(void *context)
1384 struct smi_info *smi_info = context;
1385 unsigned long flags;
1390 spin_lock_irqsave(&(smi_info->si_lock), flags);
1392 spin_lock(&smi_info->count_lock);
1393 smi_info->interrupts++;
1394 spin_unlock(&smi_info->count_lock);
1396 if (smi_info->stop_operation)
1400 do_gettimeofday(&t);
1401 printk("**ACPI_GPE: %d.%9.9d\n", t.tv_sec, t.tv_usec);
1403 smi_event_handler(smi_info, 0);
1405 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
1407 return ACPI_INTERRUPT_HANDLED;
1410 static int acpi_gpe_irq_setup(struct smi_info *info)
1417 /* FIXME - is level triggered right? */
1418 status = acpi_install_gpe_handler(NULL,
1420 ACPI_GPE_LEVEL_TRIGGERED,
1423 if (status != AE_OK) {
1425 "ipmi_si: %s unable to claim ACPI GPE %d,"
1426 " running polled\n",
1427 DEVICE_NAME, info->irq);
1431 printk(" Using ACPI GPE %d\n", info->irq);
1436 static void acpi_gpe_irq_cleanup(struct smi_info *info)
1441 acpi_remove_gpe_handler(NULL, info->irq, &ipmi_acpi_gpe);
1446 * http://h21007.www2.hp.com/dspp/files/unprotected/devresource/Docs/TechPapers/IA64/hpspmi.pdf
1457 s8 CreatorRevision[4];
1460 s16 SpecificationRevision;
1463 * Bit 0 - SCI interrupt supported
1464 * Bit 1 - I/O APIC/SAPIC
1468 /* If bit 0 of InterruptType is set, then this is the SCI
1469 interrupt in the GPEx_STS register. */
1474 /* If bit 1 of InterruptType is set, then this is the I/O
1475 APIC/SAPIC interrupt. */
1476 u32 GlobalSystemInterrupt;
1478 /* The actual register address. */
1479 struct acpi_generic_address addr;
1483 s8 spmi_id[1]; /* A '\0' terminated array starts here. */
1486 static int try_init_acpi(int intf_num, struct smi_info **new_info)
1488 struct smi_info *info;
1490 struct SPMITable *spmi;
1497 status = acpi_get_firmware_table("SPMI", intf_num+1,
1498 ACPI_LOGICAL_ADDRESSING,
1499 (struct acpi_table_header **) &spmi);
1500 if (status != AE_OK) {
1505 if (spmi->IPMIlegacy != 1) {
1506 printk(KERN_INFO "IPMI: Bad SPMI legacy %d\n", spmi->IPMIlegacy);
1510 if (spmi->addr.address_space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
1511 addr_space = IPMI_MEM_ADDR_SPACE;
1513 addr_space = IPMI_IO_ADDR_SPACE;
1514 if (!is_new_interface(-1, addr_space, spmi->addr.address))
1517 if (!spmi->addr.register_bit_width) {
1522 /* Figure out the interface type. */
1523 switch (spmi->InterfaceType)
1526 si_type[intf_num] = "kcs";
1530 si_type[intf_num] = "smic";
1534 si_type[intf_num] = "bt";
1538 printk(KERN_INFO "ipmi_si: Unknown ACPI/SPMI SI type %d\n",
1539 spmi->InterfaceType);
1543 info = kmalloc(sizeof(*info), GFP_KERNEL);
1545 printk(KERN_ERR "ipmi_si: Could not allocate SI data (3)\n");
1548 memset(info, 0, sizeof(*info));
1550 if (spmi->InterruptType & 1) {
1551 /* We've got a GPE interrupt. */
1552 info->irq = spmi->GPE;
1553 info->irq_setup = acpi_gpe_irq_setup;
1554 info->irq_cleanup = acpi_gpe_irq_cleanup;
1555 } else if (spmi->InterruptType & 2) {
1556 /* We've got an APIC/SAPIC interrupt. */
1557 info->irq = spmi->GlobalSystemInterrupt;
1558 info->irq_setup = std_irq_setup;
1559 info->irq_cleanup = std_irq_cleanup;
1561 /* Use the default interrupt setting. */
1563 info->irq_setup = NULL;
1566 if (spmi->addr.register_bit_width) {
1567 /* A (hopefully) properly formed register bit width. */
1568 regspacings[intf_num] = spmi->addr.register_bit_width / 8;
1569 info->io.regspacing = spmi->addr.register_bit_width / 8;
1571 /* Some broken systems get this wrong and set the value
1572 * to zero. Assume it is the default spacing. If that
1573 * is wrong, too bad, the vendor should fix the tables. */
1574 regspacings[intf_num] = DEFAULT_REGSPACING;
1575 info->io.regspacing = DEFAULT_REGSPACING;
1577 regsizes[intf_num] = regspacings[intf_num];
1578 info->io.regsize = regsizes[intf_num];
1579 regshifts[intf_num] = spmi->addr.register_bit_offset;
1580 info->io.regshift = regshifts[intf_num];
1582 if (spmi->addr.address_space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
1584 info->io_setup = mem_setup;
1585 addrs[intf_num] = spmi->addr.address;
1586 info->io.info = &(addrs[intf_num]);
1587 } else if (spmi->addr.address_space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
1589 info->io_setup = port_setup;
1590 ports[intf_num] = spmi->addr.address;
1591 info->io.info = &(ports[intf_num]);
1594 printk("ipmi_si: Unknown ACPI I/O Address type\n");
1600 printk("ipmi_si: ACPI/SPMI specifies \"%s\" %s SI @ 0x%lx\n",
1601 si_type[intf_num], io_type, (unsigned long) spmi->addr.address);
1607 typedef struct dmi_ipmi_data
1611 unsigned long base_addr;
1617 static dmi_ipmi_data_t dmi_data[SI_MAX_DRIVERS];
1618 static int dmi_data_entries;
1620 static int __init decode_dmi(struct dmi_header *dm, int intf_num)
1622 u8 *data = (u8 *)dm;
1623 unsigned long base_addr;
1625 u8 len = dm->length;
1626 dmi_ipmi_data_t *ipmi_data = dmi_data+intf_num;
1628 ipmi_data->type = data[4];
1630 memcpy(&base_addr, data+8, sizeof(unsigned long));
1632 if (base_addr & 1) {
1634 base_addr &= 0xFFFE;
1635 ipmi_data->addr_space = IPMI_IO_ADDR_SPACE;
1639 ipmi_data->addr_space = IPMI_MEM_ADDR_SPACE;
1641 /* If bit 4 of byte 0x10 is set, then the lsb for the address
1643 ipmi_data->base_addr = base_addr | ((data[0x10] & 0x10) >> 4);
1645 ipmi_data->irq = data[0x11];
1647 /* The top two bits of byte 0x10 hold the register spacing. */
1648 reg_spacing = (data[0x10] & 0xC0) >> 6;
1649 switch(reg_spacing){
1650 case 0x00: /* Byte boundaries */
1651 ipmi_data->offset = 1;
1653 case 0x01: /* 32-bit boundaries */
1654 ipmi_data->offset = 4;
1656 case 0x02: /* 16-byte boundaries */
1657 ipmi_data->offset = 16;
1660 /* Some other interface, just ignore it. */
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 = data[6];
1678 if (is_new_interface(-1, ipmi_data->addr_space,ipmi_data->base_addr)) {
1683 memset(ipmi_data, 0, sizeof(dmi_ipmi_data_t));
1688 static void __init dmi_find_bmc(void)
1690 struct dmi_device *dev = NULL;
1693 while ((dev = dmi_find_device(DMI_DEV_TYPE_IPMI, NULL, dev))) {
1694 if (intf_num >= SI_MAX_DRIVERS)
1697 decode_dmi((struct dmi_header *) dev->device_data, intf_num++);
1701 static int try_init_smbios(int intf_num, struct smi_info **new_info)
1703 struct smi_info *info;
1704 dmi_ipmi_data_t *ipmi_data = dmi_data+intf_num;
1707 if (intf_num >= dmi_data_entries)
1710 switch(ipmi_data->type) {
1711 case 0x01: /* KCS */
1712 si_type[intf_num] = "kcs";
1714 case 0x02: /* SMIC */
1715 si_type[intf_num] = "smic";
1718 si_type[intf_num] = "bt";
1724 info = kmalloc(sizeof(*info), GFP_KERNEL);
1726 printk(KERN_ERR "ipmi_si: Could not allocate SI data (4)\n");
1729 memset(info, 0, sizeof(*info));
1731 if (ipmi_data->addr_space == 1) {
1733 info->io_setup = mem_setup;
1734 addrs[intf_num] = ipmi_data->base_addr;
1735 info->io.info = &(addrs[intf_num]);
1736 } else if (ipmi_data->addr_space == 2) {
1738 info->io_setup = port_setup;
1739 ports[intf_num] = ipmi_data->base_addr;
1740 info->io.info = &(ports[intf_num]);
1743 printk("ipmi_si: Unknown SMBIOS I/O Address type.\n");
1747 regspacings[intf_num] = ipmi_data->offset;
1748 info->io.regspacing = regspacings[intf_num];
1749 if (!info->io.regspacing)
1750 info->io.regspacing = DEFAULT_REGSPACING;
1751 info->io.regsize = DEFAULT_REGSPACING;
1752 info->io.regshift = regshifts[intf_num];
1754 info->slave_addr = ipmi_data->slave_addr;
1756 irqs[intf_num] = ipmi_data->irq;
1760 printk("ipmi_si: Found SMBIOS-specified state machine at %s"
1761 " address 0x%lx, slave address 0x%x\n",
1762 io_type, (unsigned long)ipmi_data->base_addr,
1763 ipmi_data->slave_addr);
1766 #endif /* CONFIG_X86 */
1770 #define PCI_ERMC_CLASSCODE 0x0C0700
1771 #define PCI_HP_VENDOR_ID 0x103C
1772 #define PCI_MMC_DEVICE_ID 0x121A
1773 #define PCI_MMC_ADDR_CW 0x10
1775 /* Avoid more than one attempt to probe pci smic. */
1776 static int pci_smic_checked = 0;
1778 static int find_pci_smic(int intf_num, struct smi_info **new_info)
1780 struct smi_info *info;
1782 struct pci_dev *pci_dev = NULL;
1786 if (pci_smic_checked)
1789 pci_smic_checked = 1;
1791 if ((pci_dev = pci_get_device(PCI_HP_VENDOR_ID, PCI_MMC_DEVICE_ID,
1794 else if ((pci_dev = pci_get_class(PCI_ERMC_CLASSCODE, NULL)) &&
1795 pci_dev->subsystem_vendor == PCI_HP_VENDOR_ID)
1800 error = pci_read_config_word(pci_dev, PCI_MMC_ADDR_CW, &base_addr);
1803 pci_dev_put(pci_dev);
1805 "ipmi_si: pci_read_config_word() failed (%d).\n",
1810 /* Bit 0: 1 specifies programmed I/O, 0 specifies memory mapped I/O */
1811 if (!(base_addr & 0x0001))
1813 pci_dev_put(pci_dev);
1815 "ipmi_si: memory mapped I/O not supported for PCI"
1820 base_addr &= 0xFFFE;
1822 /* Data register starts at base address + 1 in eRMC */
1825 if (!is_new_interface(-1, IPMI_IO_ADDR_SPACE, base_addr)) {
1826 pci_dev_put(pci_dev);
1830 info = kmalloc(sizeof(*info), GFP_KERNEL);
1832 pci_dev_put(pci_dev);
1833 printk(KERN_ERR "ipmi_si: Could not allocate SI data (5)\n");
1836 memset(info, 0, sizeof(*info));
1838 info->io_setup = port_setup;
1839 ports[intf_num] = base_addr;
1840 info->io.info = &(ports[intf_num]);
1841 info->io.regspacing = regspacings[intf_num];
1842 if (!info->io.regspacing)
1843 info->io.regspacing = DEFAULT_REGSPACING;
1844 info->io.regsize = DEFAULT_REGSPACING;
1845 info->io.regshift = regshifts[intf_num];
1849 irqs[intf_num] = pci_dev->irq;
1850 si_type[intf_num] = "smic";
1852 printk("ipmi_si: Found PCI SMIC at I/O address 0x%lx\n",
1853 (long unsigned int) base_addr);
1855 pci_dev_put(pci_dev);
1858 #endif /* CONFIG_PCI */
1860 static int try_init_plug_and_play(int intf_num, struct smi_info **new_info)
1863 if (find_pci_smic(intf_num, new_info)==0)
1866 /* Include other methods here. */
1872 static int try_get_dev_id(struct smi_info *smi_info)
1874 unsigned char msg[2];
1875 unsigned char *resp;
1876 unsigned long resp_len;
1877 enum si_sm_result smi_result;
1880 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1884 /* Do a Get Device ID command, since it comes back with some
1886 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1887 msg[1] = IPMI_GET_DEVICE_ID_CMD;
1888 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
1890 smi_result = smi_info->handlers->event(smi_info->si_sm, 0);
1893 if (smi_result == SI_SM_CALL_WITH_DELAY) {
1894 set_current_state(TASK_UNINTERRUPTIBLE);
1895 schedule_timeout(1);
1896 smi_result = smi_info->handlers->event(
1897 smi_info->si_sm, 100);
1899 else if (smi_result == SI_SM_CALL_WITHOUT_DELAY)
1901 smi_result = smi_info->handlers->event(
1902 smi_info->si_sm, 0);
1907 if (smi_result == SI_SM_HOSED) {
1908 /* We couldn't get the state machine to run, so whatever's at
1909 the port is probably not an IPMI SMI interface. */
1914 /* Otherwise, we got some data. */
1915 resp_len = smi_info->handlers->get_result(smi_info->si_sm,
1916 resp, IPMI_MAX_MSG_LENGTH);
1918 /* That's odd, it should be longer. */
1923 if ((resp[1] != IPMI_GET_DEVICE_ID_CMD) || (resp[2] != 0)) {
1924 /* That's odd, it shouldn't be able to fail. */
1929 /* Record info from the get device id, in case we need it. */
1930 smi_info->ipmi_si_dev_rev = resp[4] & 0xf;
1931 smi_info->ipmi_si_fw_rev_major = resp[5] & 0x7f;
1932 smi_info->ipmi_si_fw_rev_minor = resp[6];
1933 smi_info->ipmi_version_major = resp[7] & 0xf;
1934 smi_info->ipmi_version_minor = resp[7] >> 4;
1941 static int type_file_read_proc(char *page, char **start, off_t off,
1942 int count, int *eof, void *data)
1944 char *out = (char *) page;
1945 struct smi_info *smi = data;
1947 switch (smi->si_type) {
1949 return sprintf(out, "kcs\n");
1951 return sprintf(out, "smic\n");
1953 return sprintf(out, "bt\n");
1959 static int stat_file_read_proc(char *page, char **start, off_t off,
1960 int count, int *eof, void *data)
1962 char *out = (char *) page;
1963 struct smi_info *smi = data;
1965 out += sprintf(out, "interrupts_enabled: %d\n",
1966 smi->irq && !smi->interrupt_disabled);
1967 out += sprintf(out, "short_timeouts: %ld\n",
1968 smi->short_timeouts);
1969 out += sprintf(out, "long_timeouts: %ld\n",
1970 smi->long_timeouts);
1971 out += sprintf(out, "timeout_restarts: %ld\n",
1972 smi->timeout_restarts);
1973 out += sprintf(out, "idles: %ld\n",
1975 out += sprintf(out, "interrupts: %ld\n",
1977 out += sprintf(out, "attentions: %ld\n",
1979 out += sprintf(out, "flag_fetches: %ld\n",
1981 out += sprintf(out, "hosed_count: %ld\n",
1983 out += sprintf(out, "complete_transactions: %ld\n",
1984 smi->complete_transactions);
1985 out += sprintf(out, "events: %ld\n",
1987 out += sprintf(out, "watchdog_pretimeouts: %ld\n",
1988 smi->watchdog_pretimeouts);
1989 out += sprintf(out, "incoming_messages: %ld\n",
1990 smi->incoming_messages);
1992 return (out - ((char *) page));
1995 /* Returns 0 if initialized, or negative on an error. */
1996 static int init_one_smi(int intf_num, struct smi_info **smi)
1999 struct smi_info *new_smi;
2002 rv = try_init_mem(intf_num, &new_smi);
2004 rv = try_init_port(intf_num, &new_smi);
2005 #ifdef CONFIG_ACPI_INTERPRETER
2006 if ((rv) && (si_trydefaults)) {
2007 rv = try_init_acpi(intf_num, &new_smi);
2011 if ((rv) && (si_trydefaults)) {
2012 rv = try_init_smbios(intf_num, &new_smi);
2015 if ((rv) && (si_trydefaults)) {
2016 rv = try_init_plug_and_play(intf_num, &new_smi);
2023 /* So we know not to free it unless we have allocated one. */
2024 new_smi->intf = NULL;
2025 new_smi->si_sm = NULL;
2026 new_smi->handlers = NULL;
2028 if (!new_smi->irq_setup) {
2029 new_smi->irq = irqs[intf_num];
2030 new_smi->irq_setup = std_irq_setup;
2031 new_smi->irq_cleanup = std_irq_cleanup;
2034 /* Default to KCS if no type is specified. */
2035 if (si_type[intf_num] == NULL) {
2037 si_type[intf_num] = "kcs";
2044 /* Set up the state machine to use. */
2045 if (strcmp(si_type[intf_num], "kcs") == 0) {
2046 new_smi->handlers = &kcs_smi_handlers;
2047 new_smi->si_type = SI_KCS;
2048 } else if (strcmp(si_type[intf_num], "smic") == 0) {
2049 new_smi->handlers = &smic_smi_handlers;
2050 new_smi->si_type = SI_SMIC;
2051 } else if (strcmp(si_type[intf_num], "bt") == 0) {
2052 new_smi->handlers = &bt_smi_handlers;
2053 new_smi->si_type = SI_BT;
2055 /* No support for anything else yet. */
2060 /* Allocate the state machine's data and initialize it. */
2061 new_smi->si_sm = kmalloc(new_smi->handlers->size(), GFP_KERNEL);
2062 if (!new_smi->si_sm) {
2063 printk(" Could not allocate state machine memory\n");
2067 new_smi->io_size = new_smi->handlers->init_data(new_smi->si_sm,
2070 /* Now that we know the I/O size, we can set up the I/O. */
2071 rv = new_smi->io_setup(new_smi);
2073 printk(" Could not set up I/O space\n");
2077 spin_lock_init(&(new_smi->si_lock));
2078 spin_lock_init(&(new_smi->msg_lock));
2079 spin_lock_init(&(new_smi->count_lock));
2081 /* Do low-level detection first. */
2082 if (new_smi->handlers->detect(new_smi->si_sm)) {
2087 /* Attempt a get device id command. If it fails, we probably
2088 don't have a SMI here. */
2089 rv = try_get_dev_id(new_smi);
2093 /* Try to claim any interrupts. */
2094 new_smi->irq_setup(new_smi);
2096 INIT_LIST_HEAD(&(new_smi->xmit_msgs));
2097 INIT_LIST_HEAD(&(new_smi->hp_xmit_msgs));
2098 new_smi->curr_msg = NULL;
2099 atomic_set(&new_smi->req_events, 0);
2100 new_smi->run_to_completion = 0;
2102 new_smi->interrupt_disabled = 0;
2103 new_smi->timer_stopped = 0;
2104 new_smi->stop_operation = 0;
2106 /* Start clearing the flags before we enable interrupts or the
2107 timer to avoid racing with the timer. */
2108 start_clear_flags(new_smi);
2109 /* IRQ is defined to be set when non-zero. */
2111 new_smi->si_state = SI_CLEARING_FLAGS_THEN_SET_IRQ;
2113 /* The ipmi_register_smi() code does some operations to
2114 determine the channel information, so we must be ready to
2115 handle operations before it is called. This means we have
2116 to stop the timer if we get an error after this point. */
2117 init_timer(&(new_smi->si_timer));
2118 new_smi->si_timer.data = (long) new_smi;
2119 new_smi->si_timer.function = smi_timeout;
2120 new_smi->last_timeout_jiffies = jiffies;
2121 new_smi->si_timer.expires = jiffies + SI_TIMEOUT_JIFFIES;
2122 add_timer(&(new_smi->si_timer));
2124 rv = ipmi_register_smi(&handlers,
2126 new_smi->ipmi_version_major,
2127 new_smi->ipmi_version_minor,
2128 new_smi->slave_addr,
2132 "ipmi_si: Unable to register device: error %d\n",
2134 goto out_err_stop_timer;
2137 rv = ipmi_smi_add_proc_entry(new_smi->intf, "type",
2138 type_file_read_proc, NULL,
2139 new_smi, THIS_MODULE);
2142 "ipmi_si: Unable to create proc entry: %d\n",
2144 goto out_err_stop_timer;
2147 rv = ipmi_smi_add_proc_entry(new_smi->intf, "si_stats",
2148 stat_file_read_proc, NULL,
2149 new_smi, THIS_MODULE);
2152 "ipmi_si: Unable to create proc entry: %d\n",
2154 goto out_err_stop_timer;
2159 printk(" IPMI %s interface initialized\n", si_type[intf_num]);
2164 new_smi->stop_operation = 1;
2166 /* Wait for the timer to stop. This avoids problems with race
2167 conditions removing the timer here. */
2168 while (!new_smi->timer_stopped) {
2169 set_current_state(TASK_UNINTERRUPTIBLE);
2170 schedule_timeout(1);
2175 ipmi_unregister_smi(new_smi->intf);
2177 new_smi->irq_cleanup(new_smi);
2179 /* Wait until we know that we are out of any interrupt
2180 handlers might have been running before we freed the
2182 synchronize_sched();
2184 if (new_smi->si_sm) {
2185 if (new_smi->handlers)
2186 new_smi->handlers->cleanup(new_smi->si_sm);
2187 kfree(new_smi->si_sm);
2189 new_smi->io_cleanup(new_smi);
2194 static __init int init_ipmi_si(void)
2205 /* Parse out the si_type string into its components. */
2208 for (i=0; (i<SI_MAX_PARMS) && (*str != '\0'); i++) {
2210 str = strchr(str, ',');
2220 printk(KERN_INFO "IPMI System Interface driver version "
2222 if (kcs_smi_handlers.version)
2223 printk(", KCS version %s", kcs_smi_handlers.version);
2224 if (smic_smi_handlers.version)
2225 printk(", SMIC version %s", smic_smi_handlers.version);
2226 if (bt_smi_handlers.version)
2227 printk(", BT version %s", bt_smi_handlers.version);
2234 rv = init_one_smi(0, &(smi_infos[pos]));
2235 if (rv && !ports[0] && si_trydefaults) {
2236 /* If we are trying defaults and the initial port is
2237 not set, then set it. */
2239 ports[0] = DEFAULT_KCS_IO_PORT;
2240 rv = init_one_smi(0, &(smi_infos[pos]));
2242 /* No KCS - try SMIC */
2243 si_type[0] = "smic";
2244 ports[0] = DEFAULT_SMIC_IO_PORT;
2245 rv = init_one_smi(0, &(smi_infos[pos]));
2248 /* No SMIC - try BT */
2250 ports[0] = DEFAULT_BT_IO_PORT;
2251 rv = init_one_smi(0, &(smi_infos[pos]));
2257 for (i=1; i < SI_MAX_PARMS; i++) {
2258 rv = init_one_smi(i, &(smi_infos[pos]));
2263 if (smi_infos[0] == NULL) {
2264 printk("ipmi_si: Unable to find any System Interface(s)\n");
2270 module_init(init_ipmi_si);
2272 static void __exit cleanup_one_si(struct smi_info *to_clean)
2275 unsigned long flags;
2280 /* Tell the timer and interrupt handlers that we are shutting
2282 spin_lock_irqsave(&(to_clean->si_lock), flags);
2283 spin_lock(&(to_clean->msg_lock));
2285 to_clean->stop_operation = 1;
2287 to_clean->irq_cleanup(to_clean);
2289 spin_unlock(&(to_clean->msg_lock));
2290 spin_unlock_irqrestore(&(to_clean->si_lock), flags);
2292 /* Wait until we know that we are out of any interrupt
2293 handlers might have been running before we freed the
2295 synchronize_sched();
2297 /* Wait for the timer to stop. This avoids problems with race
2298 conditions removing the timer here. */
2299 while (!to_clean->timer_stopped) {
2300 set_current_state(TASK_UNINTERRUPTIBLE);
2301 schedule_timeout(1);
2304 /* Interrupts and timeouts are stopped, now make sure the
2305 interface is in a clean state. */
2306 while ((to_clean->curr_msg) || (to_clean->si_state != SI_NORMAL)) {
2308 set_current_state(TASK_UNINTERRUPTIBLE);
2309 schedule_timeout(1);
2312 rv = ipmi_unregister_smi(to_clean->intf);
2315 "ipmi_si: Unable to unregister device: errno=%d\n",
2319 to_clean->handlers->cleanup(to_clean->si_sm);
2321 kfree(to_clean->si_sm);
2323 to_clean->io_cleanup(to_clean);
2326 static __exit void cleanup_ipmi_si(void)
2333 for (i=0; i<SI_MAX_DRIVERS; i++) {
2334 cleanup_one_si(smi_infos[i]);
2337 module_exit(cleanup_ipmi_si);
2339 MODULE_LICENSE("GPL");