4 * The interface to the IPMI driver for the KCS.
6 * Author: MontaVista Software, Inc.
7 * Corey Minyard <minyard@mvista.com>
10 * Copyright 2002 MontaVista Software Inc.
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
19 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
24 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
26 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
27 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * You should have received a copy of the GNU General Public License along
30 * with this program; if not, write to the Free Software Foundation, Inc.,
31 * 675 Mass Ave, Cambridge, MA 02139, USA.
35 * This file holds the "policy" for the interface to the KCS state
36 * machine. It does the configuration, handles timers and interrupts,
37 * and drives the real KCS state machine.
40 #include <linux/config.h>
41 #include <linux/module.h>
42 #include <asm/system.h>
43 #include <linux/sched.h>
44 #include <linux/timer.h>
45 #include <linux/errno.h>
46 #include <linux/spinlock.h>
47 #include <linux/slab.h>
48 #include <linux/delay.h>
49 #include <linux/list.h>
50 #include <linux/ioport.h>
51 #ifdef CONFIG_HIGH_RES_TIMERS
52 #include <linux/hrtime.h>
54 #include <linux/interrupt.h>
55 #include <linux/rcupdate.h>
56 #include <linux/ipmi_smi.h>
58 #include "ipmi_kcs_sm.h"
59 #include <linux/init.h>
61 /* Measure times between events in the driver. */
64 /* Timing parameters. Call every 10 ms when not doing anything,
65 otherwise call every KCS_SHORT_TIMEOUT_USEC microseconds. */
66 #define KCS_TIMEOUT_TIME_USEC 10000
67 #define KCS_USEC_PER_JIFFY (1000000/HZ)
68 #define KCS_TIMEOUT_JIFFIES (KCS_TIMEOUT_TIME_USEC/KCS_USEC_PER_JIFFY)
69 #define KCS_SHORT_TIMEOUT_USEC 250 /* .25ms when the SM request a
72 #ifdef CONFIG_IPMI_KCS
73 /* This forces a dependency to the config file for this option. */
81 KCS_CLEARING_FLAGS_THEN_SET_IRQ,
83 KCS_ENABLE_INTERRUPTS1,
84 KCS_ENABLE_INTERRUPTS2
85 /* FIXME - add watchdog stuff. */
91 struct kcs_data *kcs_sm;
94 struct list_head xmit_msgs;
95 struct list_head hp_xmit_msgs;
96 struct ipmi_smi_msg *curr_msg;
97 enum kcs_intf_state kcs_state;
99 /* Flags from the last GET_MSG_FLAGS command, used when an ATTN
100 is set to hold the flags until we are done handling everything
102 #define RECEIVE_MSG_AVAIL 0x01
103 #define EVENT_MSG_BUFFER_FULL 0x02
104 #define WDT_PRE_TIMEOUT_INT 0x08
105 unsigned char msg_flags;
107 /* If set to true, this will request events the next time the
108 state machine is idle. */
111 /* If true, run the state machine to completion on every send
112 call. Generally used after a panic to make sure stuff goes
114 int run_to_completion;
116 /* The I/O port of a KCS interface. */
119 /* zero if no irq; */
122 /* The physical and remapped memory addresses of a KCS interface. */
123 unsigned long physaddr;
126 /* The timer for this kcs. */
127 struct timer_list kcs_timer;
129 /* The time (in jiffies) the last timeout occurred at. */
130 unsigned long last_timeout_jiffies;
132 /* Used to gracefully stop the timer without race conditions. */
133 volatile int stop_operation;
134 volatile int timer_stopped;
136 /* The driver will disable interrupts when it gets into a
137 situation where it cannot handle messages due to lack of
138 memory. Once that situation clears up, it will re-enable
140 int interrupt_disabled;
143 static void kcs_restart_short_timer(struct kcs_info *kcs_info);
145 static void deliver_recv_msg(struct kcs_info *kcs_info, struct ipmi_smi_msg *msg)
147 /* Deliver the message to the upper layer with the lock
149 spin_unlock(&(kcs_info->kcs_lock));
150 ipmi_smi_msg_received(kcs_info->intf, msg);
151 spin_lock(&(kcs_info->kcs_lock));
154 static void return_hosed_msg(struct kcs_info *kcs_info)
156 struct ipmi_smi_msg *msg = kcs_info->curr_msg;
158 /* Make it a reponse */
159 msg->rsp[0] = msg->data[0] | 4;
160 msg->rsp[1] = msg->data[1];
161 msg->rsp[2] = 0xFF; /* Unknown error. */
164 kcs_info->curr_msg = NULL;
165 deliver_recv_msg(kcs_info, msg);
168 static enum kcs_result start_next_msg(struct kcs_info *kcs_info)
171 struct list_head *entry = NULL;
176 /* No need to save flags, we aleady have interrupts off and we
177 already hold the KCS lock. */
178 spin_lock(&(kcs_info->msg_lock));
180 /* Pick the high priority queue first. */
181 if (! list_empty(&(kcs_info->hp_xmit_msgs))) {
182 entry = kcs_info->hp_xmit_msgs.next;
183 } else if (! list_empty(&(kcs_info->xmit_msgs))) {
184 entry = kcs_info->xmit_msgs.next;
188 kcs_info->curr_msg = NULL;
194 kcs_info->curr_msg = list_entry(entry,
199 printk("**Start2: %d.%9.9d\n", t.tv_sec, t.tv_usec);
201 err = start_kcs_transaction(kcs_info->kcs_sm,
202 kcs_info->curr_msg->data,
203 kcs_info->curr_msg->data_size);
205 return_hosed_msg(kcs_info);
208 rv = KCS_CALL_WITHOUT_DELAY;
210 spin_unlock(&(kcs_info->msg_lock));
215 static void start_enable_irq(struct kcs_info *kcs_info)
217 unsigned char msg[2];
219 /* If we are enabling interrupts, we have to tell the
221 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
222 msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
224 start_kcs_transaction(kcs_info->kcs_sm, msg, 2);
225 kcs_info->kcs_state = KCS_ENABLE_INTERRUPTS1;
228 static void start_clear_flags(struct kcs_info *kcs_info)
230 unsigned char msg[3];
232 /* Make sure the watchdog pre-timeout flag is not set at startup. */
233 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
234 msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
235 msg[2] = WDT_PRE_TIMEOUT_INT;
237 start_kcs_transaction(kcs_info->kcs_sm, msg, 3);
238 kcs_info->kcs_state = KCS_CLEARING_FLAGS;
241 /* When we have a situtaion where we run out of memory and cannot
242 allocate messages, we just leave them in the BMC and run the system
243 polled until we can allocate some memory. Once we have some
244 memory, we will re-enable the interrupt. */
245 static inline void disable_kcs_irq(struct kcs_info *kcs_info)
247 if ((kcs_info->irq) && (!kcs_info->interrupt_disabled)) {
248 disable_irq_nosync(kcs_info->irq);
249 kcs_info->interrupt_disabled = 1;
253 static inline void enable_kcs_irq(struct kcs_info *kcs_info)
255 if ((kcs_info->irq) && (kcs_info->interrupt_disabled)) {
256 enable_irq(kcs_info->irq);
257 kcs_info->interrupt_disabled = 0;
261 static void handle_flags(struct kcs_info *kcs_info)
263 if (kcs_info->msg_flags & WDT_PRE_TIMEOUT_INT) {
264 /* Watchdog pre-timeout */
265 start_clear_flags(kcs_info);
266 kcs_info->msg_flags &= ~WDT_PRE_TIMEOUT_INT;
267 spin_unlock(&(kcs_info->kcs_lock));
268 ipmi_smi_watchdog_pretimeout(kcs_info->intf);
269 spin_lock(&(kcs_info->kcs_lock));
270 } else if (kcs_info->msg_flags & RECEIVE_MSG_AVAIL) {
271 /* Messages available. */
272 kcs_info->curr_msg = ipmi_alloc_smi_msg();
273 if (!kcs_info->curr_msg) {
274 disable_kcs_irq(kcs_info);
275 kcs_info->kcs_state = KCS_NORMAL;
278 enable_kcs_irq(kcs_info);
280 kcs_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
281 kcs_info->curr_msg->data[1] = IPMI_GET_MSG_CMD;
282 kcs_info->curr_msg->data_size = 2;
284 start_kcs_transaction(kcs_info->kcs_sm,
285 kcs_info->curr_msg->data,
286 kcs_info->curr_msg->data_size);
287 kcs_info->kcs_state = KCS_GETTING_MESSAGES;
288 } else if (kcs_info->msg_flags & EVENT_MSG_BUFFER_FULL) {
289 /* Events available. */
290 kcs_info->curr_msg = ipmi_alloc_smi_msg();
291 if (!kcs_info->curr_msg) {
292 disable_kcs_irq(kcs_info);
293 kcs_info->kcs_state = KCS_NORMAL;
296 enable_kcs_irq(kcs_info);
298 kcs_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
299 kcs_info->curr_msg->data[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD;
300 kcs_info->curr_msg->data_size = 2;
302 start_kcs_transaction(kcs_info->kcs_sm,
303 kcs_info->curr_msg->data,
304 kcs_info->curr_msg->data_size);
305 kcs_info->kcs_state = KCS_GETTING_EVENTS;
307 kcs_info->kcs_state = KCS_NORMAL;
311 static void handle_transaction_done(struct kcs_info *kcs_info)
313 struct ipmi_smi_msg *msg;
318 printk("**Done: %d.%9.9d\n", t.tv_sec, t.tv_usec);
320 switch (kcs_info->kcs_state) {
322 if (!kcs_info->curr_msg)
325 kcs_info->curr_msg->rsp_size
326 = kcs_get_result(kcs_info->kcs_sm,
327 kcs_info->curr_msg->rsp,
328 IPMI_MAX_MSG_LENGTH);
330 /* Do this here becase deliver_recv_msg() releases the
331 lock, and a new message can be put in during the
332 time the lock is released. */
333 msg = kcs_info->curr_msg;
334 kcs_info->curr_msg = NULL;
335 deliver_recv_msg(kcs_info, msg);
338 case KCS_GETTING_FLAGS:
340 unsigned char msg[4];
343 /* We got the flags from the KCS, now handle them. */
344 len = kcs_get_result(kcs_info->kcs_sm, msg, 4);
346 /* Error fetching flags, just give up for
348 kcs_info->kcs_state = KCS_NORMAL;
349 } else if (len < 3) {
350 /* Hmm, no flags. That's technically illegal, but
351 don't use uninitialized data. */
352 kcs_info->kcs_state = KCS_NORMAL;
354 kcs_info->msg_flags = msg[3];
355 handle_flags(kcs_info);
360 case KCS_CLEARING_FLAGS:
361 case KCS_CLEARING_FLAGS_THEN_SET_IRQ:
363 unsigned char msg[3];
365 /* We cleared the flags. */
366 kcs_get_result(kcs_info->kcs_sm, msg, 3);
368 /* Error clearing flags */
370 "ipmi_kcs: Error clearing flags: %2.2x\n",
373 if (kcs_info->kcs_state == KCS_CLEARING_FLAGS_THEN_SET_IRQ)
374 start_enable_irq(kcs_info);
376 kcs_info->kcs_state = KCS_NORMAL;
380 case KCS_GETTING_EVENTS:
382 kcs_info->curr_msg->rsp_size
383 = kcs_get_result(kcs_info->kcs_sm,
384 kcs_info->curr_msg->rsp,
385 IPMI_MAX_MSG_LENGTH);
387 /* Do this here becase deliver_recv_msg() releases the
388 lock, and a new message can be put in during the
389 time the lock is released. */
390 msg = kcs_info->curr_msg;
391 kcs_info->curr_msg = NULL;
392 if (msg->rsp[2] != 0) {
393 /* Error getting event, probably done. */
396 /* Take off the event flag. */
397 kcs_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
399 deliver_recv_msg(kcs_info, msg);
401 handle_flags(kcs_info);
405 case KCS_GETTING_MESSAGES:
407 kcs_info->curr_msg->rsp_size
408 = kcs_get_result(kcs_info->kcs_sm,
409 kcs_info->curr_msg->rsp,
410 IPMI_MAX_MSG_LENGTH);
412 /* Do this here becase deliver_recv_msg() releases the
413 lock, and a new message can be put in during the
414 time the lock is released. */
415 msg = kcs_info->curr_msg;
416 kcs_info->curr_msg = NULL;
417 if (msg->rsp[2] != 0) {
418 /* Error getting event, probably done. */
421 /* Take off the msg flag. */
422 kcs_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
424 deliver_recv_msg(kcs_info, msg);
426 handle_flags(kcs_info);
430 case KCS_ENABLE_INTERRUPTS1:
432 unsigned char msg[4];
434 /* We got the flags from the KCS, now handle them. */
435 kcs_get_result(kcs_info->kcs_sm, msg, 4);
438 "ipmi_kcs: Could not enable interrupts"
439 ", failed get, using polled mode.\n");
440 kcs_info->kcs_state = KCS_NORMAL;
442 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
443 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
444 msg[2] = msg[3] | 1; /* enable msg queue int */
445 start_kcs_transaction(kcs_info->kcs_sm, msg,3);
446 kcs_info->kcs_state = KCS_ENABLE_INTERRUPTS2;
451 case KCS_ENABLE_INTERRUPTS2:
453 unsigned char msg[4];
455 /* We got the flags from the KCS, now handle them. */
456 kcs_get_result(kcs_info->kcs_sm, msg, 4);
459 "ipmi_kcs: Could not enable interrupts"
460 ", failed set, using polled mode.\n");
462 kcs_info->kcs_state = KCS_NORMAL;
468 /* Called on timeouts and events. Timeouts should pass the elapsed
469 time, interrupts should pass in zero. */
470 static enum kcs_result kcs_event_handler(struct kcs_info *kcs_info, int time)
472 enum kcs_result kcs_result;
475 /* There used to be a loop here that waited a little while
476 (around 25us) before giving up. That turned out to be
477 pointless, the minimum delays I was seeing were in the 300us
478 range, which is far too long to wait in an interrupt. So
479 we just run until the state machine tells us something
480 happened or it needs a delay. */
481 kcs_result = kcs_event(kcs_info->kcs_sm, time);
483 while (kcs_result == KCS_CALL_WITHOUT_DELAY)
485 kcs_result = kcs_event(kcs_info->kcs_sm, 0);
488 if (kcs_result == KCS_TRANSACTION_COMPLETE)
490 handle_transaction_done(kcs_info);
491 kcs_result = kcs_event(kcs_info->kcs_sm, 0);
493 else if (kcs_result == KCS_SM_HOSED)
495 if (kcs_info->curr_msg != NULL) {
496 /* If we were handling a user message, format
497 a response to send to the upper layer to
498 tell it about the error. */
499 return_hosed_msg(kcs_info);
501 kcs_result = kcs_event(kcs_info->kcs_sm, 0);
502 kcs_info->kcs_state = KCS_NORMAL;
505 /* We prefer handling attn over new messages. */
506 if (kcs_result == KCS_ATTN)
508 unsigned char msg[2];
510 /* Got a attn, send down a get message flags to see
511 what's causing it. It would be better to handle
512 this in the upper layer, but due to the way
513 interrupts work with the KCS, that's not really
515 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
516 msg[1] = IPMI_GET_MSG_FLAGS_CMD;
518 start_kcs_transaction(kcs_info->kcs_sm, msg, 2);
519 kcs_info->kcs_state = KCS_GETTING_FLAGS;
523 /* If we are currently idle, try to start the next message. */
524 if (kcs_result == KCS_SM_IDLE) {
525 kcs_result = start_next_msg(kcs_info);
526 if (kcs_result != KCS_SM_IDLE)
530 if ((kcs_result == KCS_SM_IDLE)
531 && (atomic_read(&kcs_info->req_events)))
533 /* We are idle and the upper layer requested that I fetch
535 unsigned char msg[2];
537 atomic_set(&kcs_info->req_events, 0);
538 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
539 msg[1] = IPMI_GET_MSG_FLAGS_CMD;
541 start_kcs_transaction(kcs_info->kcs_sm, msg, 2);
542 kcs_info->kcs_state = KCS_GETTING_FLAGS;
549 static void sender(void *send_info,
550 struct ipmi_smi_msg *msg,
553 struct kcs_info *kcs_info = (struct kcs_info *) send_info;
554 enum kcs_result result;
560 spin_lock_irqsave(&(kcs_info->msg_lock), flags);
563 printk("**Enqueue: %d.%9.9d\n", t.tv_sec, t.tv_usec);
566 if (kcs_info->run_to_completion) {
567 /* If we are running to completion, then throw it in
568 the list and run transactions until everything is
569 clear. Priority doesn't matter here. */
570 list_add_tail(&(msg->link), &(kcs_info->xmit_msgs));
572 /* We have to release the msg lock and claim the kcs
573 lock in this case, because of race conditions. */
574 spin_unlock_irqrestore(&(kcs_info->msg_lock), flags);
576 spin_lock_irqsave(&(kcs_info->kcs_lock), flags);
577 result = kcs_event_handler(kcs_info, 0);
578 while (result != KCS_SM_IDLE) {
579 udelay(KCS_SHORT_TIMEOUT_USEC);
580 result = kcs_event_handler(kcs_info,
581 KCS_SHORT_TIMEOUT_USEC);
583 spin_unlock_irqrestore(&(kcs_info->kcs_lock), flags);
587 list_add_tail(&(msg->link), &(kcs_info->hp_xmit_msgs));
589 list_add_tail(&(msg->link), &(kcs_info->xmit_msgs));
592 spin_unlock_irqrestore(&(kcs_info->msg_lock), flags);
594 spin_lock_irqsave(&(kcs_info->kcs_lock), flags);
595 if ((kcs_info->kcs_state == KCS_NORMAL)
596 && (kcs_info->curr_msg == NULL))
598 start_next_msg(kcs_info);
599 kcs_restart_short_timer(kcs_info);
601 spin_unlock_irqrestore(&(kcs_info->kcs_lock), flags);
604 static void set_run_to_completion(void *send_info, int i_run_to_completion)
606 struct kcs_info *kcs_info = (struct kcs_info *) send_info;
607 enum kcs_result result;
610 spin_lock_irqsave(&(kcs_info->kcs_lock), flags);
612 kcs_info->run_to_completion = i_run_to_completion;
613 if (i_run_to_completion) {
614 result = kcs_event_handler(kcs_info, 0);
615 while (result != KCS_SM_IDLE) {
616 udelay(KCS_SHORT_TIMEOUT_USEC);
617 result = kcs_event_handler(kcs_info,
618 KCS_SHORT_TIMEOUT_USEC);
622 spin_unlock_irqrestore(&(kcs_info->kcs_lock), flags);
625 static void request_events(void *send_info)
627 struct kcs_info *kcs_info = (struct kcs_info *) send_info;
629 atomic_set(&kcs_info->req_events, 1);
632 static int initialized = 0;
634 /* Must be called with interrupts off and with the kcs_lock held. */
635 static void kcs_restart_short_timer(struct kcs_info *kcs_info)
637 if (del_timer(&(kcs_info->kcs_timer))) {
638 #ifdef CONFIG_HIGH_RES_TIMERS
639 unsigned long jiffies_now;
641 /* If we don't delete the timer, then it will go off
642 immediately, anyway. So we only process if we
643 actually delete the timer. */
645 /* We already have irqsave on, so no need for it
647 read_lock(&xtime_lock);
648 jiffies_now = jiffies;
649 kcs_info->kcs_timer.expires = jiffies_now;
651 kcs_info->kcs_timer.sub_expires
652 = quick_update_jiffies_sub(jiffies_now);
653 read_unlock(&xtime_lock);
655 kcs_info->kcs_timer.sub_expires
656 += usec_to_arch_cycles(KCS_SHORT_TIMEOUT_USEC);
657 while (kcs_info->kcs_timer.sub_expires >= cycles_per_jiffies) {
658 kcs_info->kcs_timer.expires++;
659 kcs_info->kcs_timer.sub_expires -= cycles_per_jiffies;
662 kcs_info->kcs_timer.expires = jiffies + 1;
664 add_timer(&(kcs_info->kcs_timer));
668 static void kcs_timeout(unsigned long data)
670 struct kcs_info *kcs_info = (struct kcs_info *) data;
671 enum kcs_result kcs_result;
673 unsigned long jiffies_now;
674 unsigned long time_diff;
679 if (kcs_info->stop_operation) {
680 kcs_info->timer_stopped = 1;
684 spin_lock_irqsave(&(kcs_info->kcs_lock), flags);
687 printk("**Timer: %d.%9.9d\n", t.tv_sec, t.tv_usec);
689 jiffies_now = jiffies;
691 time_diff = ((jiffies_now - kcs_info->last_timeout_jiffies)
692 * KCS_USEC_PER_JIFFY);
693 kcs_result = kcs_event_handler(kcs_info, time_diff);
695 kcs_info->last_timeout_jiffies = jiffies_now;
697 if ((kcs_info->irq) && (! kcs_info->interrupt_disabled)) {
698 /* Running with interrupts, only do long timeouts. */
699 kcs_info->kcs_timer.expires = jiffies + KCS_TIMEOUT_JIFFIES;
703 /* If the state machine asks for a short delay, then shorten
704 the timer timeout. */
705 #ifdef CONFIG_HIGH_RES_TIMERS
706 if (kcs_result == KCS_CALL_WITH_DELAY) {
707 kcs_info->kcs_timer.sub_expires
708 += usec_to_arch_cycles(KCS_SHORT_TIMEOUT_USEC);
709 while (kcs_info->kcs_timer.sub_expires >= cycles_per_jiffies) {
710 kcs_info->kcs_timer.expires++;
711 kcs_info->kcs_timer.sub_expires -= cycles_per_jiffies;
714 kcs_info->kcs_timer.expires = jiffies + KCS_TIMEOUT_JIFFIES;
715 kcs_info->kcs_timer.sub_expires = 0;
718 /* If requested, take the shortest delay possible */
719 if (kcs_result == KCS_CALL_WITH_DELAY) {
720 kcs_info->kcs_timer.expires = jiffies + 1;
722 kcs_info->kcs_timer.expires = jiffies + KCS_TIMEOUT_JIFFIES;
727 add_timer(&(kcs_info->kcs_timer));
728 spin_unlock_irqrestore(&(kcs_info->kcs_lock), flags);
731 static irqreturn_t kcs_irq_handler(int irq, void *data, struct pt_regs *regs)
733 struct kcs_info *kcs_info = (struct kcs_info *) data;
739 spin_lock_irqsave(&(kcs_info->kcs_lock), flags);
740 if (kcs_info->stop_operation)
745 printk("**Interrupt: %d.%9.9d\n", t.tv_sec, t.tv_usec);
747 kcs_event_handler(kcs_info, 0);
749 spin_unlock_irqrestore(&(kcs_info->kcs_lock), flags);
753 static struct ipmi_smi_handlers handlers =
755 .owner = THIS_MODULE,
757 .request_events = request_events,
758 .set_run_to_completion = set_run_to_completion,
761 static unsigned char ipmi_kcs_dev_rev;
762 static unsigned char ipmi_kcs_fw_rev_major;
763 static unsigned char ipmi_kcs_fw_rev_minor;
764 static unsigned char ipmi_version_major;
765 static unsigned char ipmi_version_minor;
768 static int ipmi_kcs_detect_hardware(unsigned int port,
770 struct kcs_data *data)
772 unsigned char msg[2];
773 unsigned char resp[IPMI_MAX_MSG_LENGTH];
774 unsigned long resp_len;
775 enum kcs_result kcs_result;
777 /* It's impossible for the KCS status register to be all 1's,
778 (assuming a properly functioning, self-initialized BMC)
779 but that's what you get from reading a bogus address, so we
783 if (inb(port+1) == 0xff) return -ENODEV;
785 if (readb(addr+1) == 0xff) return -ENODEV;
788 /* Do a Get Device ID command, since it comes back with some
790 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
791 msg[1] = IPMI_GET_DEVICE_ID_CMD;
792 start_kcs_transaction(data, msg, 2);
794 kcs_result = kcs_event(data, 0);
797 if (kcs_result == KCS_CALL_WITH_DELAY) {
799 kcs_result = kcs_event(data, 100);
801 else if (kcs_result == KCS_CALL_WITHOUT_DELAY)
803 kcs_result = kcs_event(data, 0);
808 if (kcs_result == KCS_SM_HOSED) {
809 /* We couldn't get the state machine to run, so whatever's at
810 the port is probably not an IPMI KCS interface. */
813 /* Otherwise, we got some data. */
814 resp_len = kcs_get_result(data, resp, IPMI_MAX_MSG_LENGTH);
816 /* That's odd, it should be longer. */
819 if ((resp[1] != IPMI_GET_DEVICE_ID_CMD) || (resp[2] != 0))
820 /* That's odd, it shouldn't be able to fail. */
823 ipmi_kcs_dev_rev = resp[4] & 0xf;
824 ipmi_kcs_fw_rev_major = resp[5] & 0x7f;
825 ipmi_kcs_fw_rev_minor = resp[6];
826 ipmi_version_major = resp[7] & 0xf;
827 ipmi_version_minor = resp[7] >> 4;
832 /* There can be 4 IO ports passed in (with or without IRQs), 4 addresses,
833 a default IO port, and 1 ACPI/SPMI address. That sets KCS_MAX_DRIVERS */
835 #define KCS_MAX_PARMS 4
836 #define KCS_MAX_DRIVERS ((KCS_MAX_PARMS * 2) + 2)
837 static struct kcs_info *kcs_infos[KCS_MAX_DRIVERS] =
838 { NULL, NULL, NULL, NULL };
840 #define DEVICE_NAME "ipmi_kcs"
842 #define DEFAULT_IO_PORT 0xca2
844 static int kcs_trydefaults = 1;
845 static unsigned long kcs_addrs[KCS_MAX_PARMS] = { 0, 0, 0, 0 };
846 static int kcs_ports[KCS_MAX_PARMS] = { 0, 0, 0, 0 };
847 static int kcs_irqs[KCS_MAX_PARMS] = { 0, 0, 0, 0 };
849 MODULE_PARM(kcs_trydefaults, "i");
850 MODULE_PARM(kcs_addrs, "1-4l");
851 MODULE_PARM(kcs_irqs, "1-4i");
852 MODULE_PARM(kcs_ports, "1-4i");
854 /* Returns 0 if initialized, or negative on an error. */
855 static int init_one_kcs(int kcs_port,
857 unsigned long kcs_physaddr,
858 struct kcs_info **kcs)
861 struct kcs_info *new_kcs;
863 /* Did anything get passed in at all? Both == zero disables the
866 if (!(kcs_port || kcs_physaddr))
869 /* Only initialize a port OR a physical address on this call.
870 Also, IRQs can go with either ports or addresses. */
872 if (kcs_port && kcs_physaddr)
875 new_kcs = kmalloc(sizeof(*new_kcs), GFP_KERNEL);
877 printk(KERN_ERR "ipmi_kcs: out of memory\n");
881 /* So we know not to free it unless we have allocated one. */
882 new_kcs->kcs_sm = NULL;
884 new_kcs->addr = NULL;
885 new_kcs->physaddr = kcs_physaddr;
886 new_kcs->port = kcs_port;
889 if (request_region(kcs_port, 2, DEVICE_NAME) == NULL) {
892 "ipmi_kcs: can't reserve port @ 0x%4.4x\n",
897 if (request_mem_region(kcs_physaddr, 2, DEVICE_NAME) == NULL) {
900 "ipmi_kcs: can't reserve memory @ 0x%lx\n",
904 if ((new_kcs->addr = ioremap(kcs_physaddr, 2)) == NULL) {
907 "ipmi_kcs: can't remap memory at 0x%lx\n",
913 new_kcs->kcs_sm = kmalloc(kcs_size(), GFP_KERNEL);
914 if (!new_kcs->kcs_sm) {
915 printk(KERN_ERR "ipmi_kcs: out of memory\n");
919 init_kcs_data(new_kcs->kcs_sm, kcs_port, new_kcs->addr);
920 spin_lock_init(&(new_kcs->kcs_lock));
921 spin_lock_init(&(new_kcs->msg_lock));
923 rv = ipmi_kcs_detect_hardware(kcs_port, new_kcs->addr, new_kcs->kcs_sm);
927 "ipmi_kcs: No KCS @ port 0x%4.4x\n",
931 "ipmi_kcs: No KCS @ addr 0x%lx\n",
937 rv = request_irq(irq,
944 "ipmi_kcs: %s unable to claim interrupt %d,"
952 INIT_LIST_HEAD(&(new_kcs->xmit_msgs));
953 INIT_LIST_HEAD(&(new_kcs->hp_xmit_msgs));
954 new_kcs->curr_msg = NULL;
955 atomic_set(&new_kcs->req_events, 0);
956 new_kcs->run_to_completion = 0;
958 start_clear_flags(new_kcs);
961 new_kcs->kcs_state = KCS_CLEARING_FLAGS_THEN_SET_IRQ;
964 "ipmi_kcs: Acquiring BMC @ port=0x%x irq=%d\n",
970 "ipmi_kcs: Acquiring BMC @ port=0x%x\n",
974 "ipmi_kcs: Acquiring BMC @ addr=0x%lx\n",
978 rv = ipmi_register_smi(&handlers,
984 free_irq(irq, new_kcs);
986 "ipmi_kcs: Unable to register device: error %d\n",
991 new_kcs->interrupt_disabled = 0;
992 new_kcs->timer_stopped = 0;
993 new_kcs->stop_operation = 0;
995 init_timer(&(new_kcs->kcs_timer));
996 new_kcs->kcs_timer.data = (long) new_kcs;
997 new_kcs->kcs_timer.function = kcs_timeout;
998 new_kcs->last_timeout_jiffies = jiffies;
999 new_kcs->kcs_timer.expires = jiffies + KCS_TIMEOUT_JIFFIES;
1000 add_timer(&(new_kcs->kcs_timer));
1008 release_region (kcs_port, 2);
1010 iounmap(new_kcs->addr);
1012 release_mem_region(kcs_physaddr, 2);
1013 if (new_kcs->kcs_sm)
1014 kfree(new_kcs->kcs_sm);
1019 #ifdef CONFIG_ACPI_INTERPRETER
1021 /* Retrieve the base physical address from ACPI tables. Originally
1022 from Hewlett-Packard simple bmc.c, a GPL KCS driver. */
1024 #include <linux/acpi.h>
1035 s8 CreatorRevision[4];
1037 s16 SpecificationRevision;
1041 u64 GlobalSystemInterrupt;
1044 } __attribute__ ((packed));
1046 static unsigned long acpi_find_bmc(void)
1049 struct acpi_table_header *spmi;
1050 static unsigned long io_base = 0;
1055 status = acpi_get_firmware_table("SPMI", 1,
1056 ACPI_LOGICAL_ADDRESSING, &spmi);
1058 if (status != AE_OK) {
1059 printk(KERN_ERR "ipmi_kcs: SPMI table not found.\n");
1063 memcpy(&io_base, ((struct SPMITable *)spmi)->BaseAddress,
1070 static __init int init_ipmi_kcs(void)
1075 #ifdef CONFIG_ACPI_INTERPRETER
1076 unsigned long physaddr = 0;
1083 /* First do the "command-line" parameters */
1085 for (i=0; i < KCS_MAX_PARMS; i++) {
1086 rv = init_one_kcs(kcs_ports[i],
1093 rv = init_one_kcs(0,
1101 /* Only try the defaults if enabled and resources are available
1102 (because they weren't already specified above). */
1104 if (kcs_trydefaults) {
1105 #ifdef CONFIG_ACPI_INTERPRETER
1106 if ((physaddr = acpi_find_bmc())) {
1107 if (!check_mem_region(physaddr, 2)) {
1108 rv = init_one_kcs(0,
1117 if (!check_region(DEFAULT_IO_PORT, 2)) {
1118 rv = init_one_kcs(DEFAULT_IO_PORT,
1127 if (kcs_infos[0] == NULL) {
1128 printk("ipmi_kcs: Unable to find any KCS interfaces\n");
1134 module_init(init_ipmi_kcs);
1137 void __exit cleanup_one_kcs(struct kcs_info *to_clean)
1140 unsigned long flags;
1145 /* Tell the timer and interrupt handlers that we are shutting
1147 spin_lock_irqsave(&(to_clean->kcs_lock), flags);
1148 spin_lock(&(to_clean->msg_lock));
1150 to_clean->stop_operation = 1;
1152 if (to_clean->irq != 0)
1153 free_irq(to_clean->irq, to_clean);
1154 if (to_clean->port) {
1156 "ipmi_kcs: Releasing BMC @ port=0x%x\n",
1158 release_region (to_clean->port, 2);
1160 if (to_clean->addr) {
1162 "ipmi_kcs: Releasing BMC @ addr=0x%lx\n",
1163 to_clean->physaddr);
1164 iounmap(to_clean->addr);
1165 release_mem_region(to_clean->physaddr, 2);
1168 spin_unlock(&(to_clean->msg_lock));
1169 spin_unlock_irqrestore(&(to_clean->kcs_lock), flags);
1171 /* Wait until we know that we are out of any interrupt
1172 handlers might have been running before we freed the
1174 synchronize_kernel();
1176 /* Wait for the timer to stop. This avoids problems with race
1177 conditions removing the timer here. */
1178 while (!to_clean->timer_stopped) {
1179 schedule_timeout(1);
1182 rv = ipmi_unregister_smi(to_clean->intf);
1185 "ipmi_kcs: Unable to unregister device: errno=%d\n",
1191 kfree(to_clean->kcs_sm);
1195 static __exit void cleanup_ipmi_kcs(void)
1202 for (i=0; i<KCS_MAX_DRIVERS; i++) {
1203 cleanup_one_kcs(kcs_infos[i]);
1206 module_exit(cleanup_ipmi_kcs);
1209 /* Unfortunately, cmdline::get_options() only returns integers, not
1210 longs. Since we need ulongs (64-bit physical addresses) parse the
1211 comma-separated list manually. Arguments can be one of these forms:
1212 m0xaabbccddeeff A physical memory address without an IRQ
1213 m0xaabbccddeeff:cc A physical memory address with an IRQ
1214 p0xaabb An IO port without an IRQ
1215 p0xaabb:cc An IO port with an IRQ
1216 nodefaults Suppress trying the default IO port or ACPI address
1218 For example, to pass one IO port with an IRQ, one address, and
1219 suppress the use of the default IO port and ACPI address,
1220 use this option string: ipmi_kcs=p0xCA2:5,m0xFF5B0022,nodefaults
1222 Remember, ipmi_kcs_setup() is passed the string after the equal sign. */
1224 static int __init ipmi_kcs_setup(char *str)
1232 cur = strsep(&str, ",");
1233 while ((cur) && (*cur) && (pos < KCS_MAX_PARMS)) {
1236 if (strcmp(cur, "nodefaults") == 0)
1237 kcs_trydefaults = 0;
1240 "ipmi_kcs: bad parameter value %s\n",
1246 val = simple_strtoul(cur + 1,
1250 kcs_ports[pos] = val;
1252 kcs_addrs[pos] = val;
1253 if (*colon == ':') {
1254 val = simple_strtoul(colon + 1,
1257 kcs_irqs[pos] = val;
1264 "ipmi_kcs: bad parameter value %s\n",
1267 cur = strsep(&str, ",");
1272 __setup("ipmi_kcs=", ipmi_kcs_setup);
1275 MODULE_LICENSE("GPL");