1 /*
2  * ipmi_kcs_intf.c
3  *
4  * The interface to the IPMI driver for the KCS.
5  *
6  * Author: MontaVista Software, Inc.
7  *         Corey Minyard <minyard@mvista.com>
8  *         source@mvista.com
9  *
10  * Copyright 2002 MontaVista Software Inc.
11  *
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.
16  *
17  *
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.
28  *
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.
32  */
33 
34 /*
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.
38  */
39 
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>
53 #endif
54 #include <linux/interrupt.h>
55 #include <linux/ipmi_smi.h>
56 #include <asm/io.h>
57 #include "ipmi_kcs_sm.h"
58 #include <linux/init.h>
59 
60 /* Measure times between events in the driver. */
61 #undef DEBUG_TIMING
62 
63 /* Timing parameters.  Call every 10 ms when not doing anything,
64    otherwise call every KCS_SHORT_TIMEOUT_USEC microseconds. */
65 #define KCS_TIMEOUT_TIME_USEC	10000
66 #define KCS_USEC_PER_JIFFY	(1000000/HZ)
67 #define KCS_TIMEOUT_JIFFIES	(KCS_TIMEOUT_TIME_USEC/KCS_USEC_PER_JIFFY)
68 #define KCS_SHORT_TIMEOUT_USEC  250 /* .25ms when the SM request a
69                                        short timeout */
70 
71 #ifdef CONFIG_IPMI_KCS
72 /* This forces a dependency to the config file for this option. */
73 #endif
74 
75 enum kcs_intf_state {
76 	KCS_NORMAL,
77 	KCS_GETTING_FLAGS,
78 	KCS_GETTING_EVENTS,
79 	KCS_CLEARING_FLAGS,
80 	KCS_CLEARING_FLAGS_THEN_SET_IRQ,
81 	KCS_GETTING_MESSAGES,
82 	KCS_ENABLE_INTERRUPTS1,
83 	KCS_ENABLE_INTERRUPTS2
84 	/* FIXME - add watchdog stuff. */
85 };
86 
87 struct kcs_info
88 {
89 	ipmi_smi_t          intf;
90 	struct kcs_data     *kcs_sm;
91 	spinlock_t          kcs_lock;
92 	spinlock_t          msg_lock;
93 	struct list_head    xmit_msgs;
94 	struct list_head    hp_xmit_msgs;
95 	struct ipmi_smi_msg *curr_msg;
96 	enum kcs_intf_state kcs_state;
97 
98 	/* Flags from the last GET_MSG_FLAGS command, used when an ATTN
99 	   is set to hold the flags until we are done handling everything
100 	   from the flags. */
101 #define RECEIVE_MSG_AVAIL	0x01
102 #define EVENT_MSG_BUFFER_FULL	0x02
103 #define WDT_PRE_TIMEOUT_INT	0x08
104 	unsigned char       msg_flags;
105 
106 	/* If set to true, this will request events the next time the
107 	   state machine is idle. */
108 	atomic_t            req_events;
109 
110 	/* If true, run the state machine to completion on every send
111 	   call.  Generally used after a panic to make sure stuff goes
112 	   out. */
113 	int                 run_to_completion;
114 
115 	/* The I/O port of a KCS interface. */
116 	int                 port;
117 
118 	/* zero if no irq; */
119 	int                 irq;
120 
121 	/* The physical and remapped memory addresses of a KCS interface. */
122 	unsigned long	    physaddr;
123 	unsigned char	    *addr;
124 
125 	/* The timer for this kcs. */
126 	struct timer_list   kcs_timer;
127 
128 	/* The time (in jiffies) the last timeout occurred at. */
129 	unsigned long       last_timeout_jiffies;
130 
131 	/* Used to gracefully stop the timer without race conditions. */
132 	volatile int        stop_operation;
133 	volatile int        timer_stopped;
134 
135 	/* The driver will disable interrupts when it gets into a
136 	   situation where it cannot handle messages due to lack of
137 	   memory.  Once that situation clears up, it will re-enable
138 	   interupts. */
139 	int                 interrupt_disabled;
140 };
141 
142 static void kcs_restart_short_timer(struct kcs_info *kcs_info);
143 
deliver_recv_msg(struct kcs_info * kcs_info,struct ipmi_smi_msg * msg)144 static void deliver_recv_msg(struct kcs_info *kcs_info, struct ipmi_smi_msg *msg)
145 {
146 	/* Deliver the message to the upper layer with the lock
147            released. */
148 	spin_unlock(&(kcs_info->kcs_lock));
149 	ipmi_smi_msg_received(kcs_info->intf, msg);
150 	spin_lock(&(kcs_info->kcs_lock));
151 }
152 
return_hosed_msg(struct kcs_info * kcs_info)153 static void return_hosed_msg(struct kcs_info *kcs_info)
154 {
155 	struct ipmi_smi_msg *msg = kcs_info->curr_msg;
156 
157 	/* Make it a reponse */
158 	msg->rsp[0] = msg->data[0] | 4;
159 	msg->rsp[1] = msg->data[1];
160 	msg->rsp[2] = 0xFF; /* Unknown error. */
161 	msg->rsp_size = 3;
162 
163 	kcs_info->curr_msg = NULL;
164 	deliver_recv_msg(kcs_info, msg);
165 }
166 
start_next_msg(struct kcs_info * kcs_info)167 static enum kcs_result start_next_msg(struct kcs_info *kcs_info)
168 {
169 	int              rv;
170 	struct list_head *entry = NULL;
171 #ifdef DEBUG_TIMING
172 	struct timeval t;
173 #endif
174 
175 	/* No need to save flags, we aleady have interrupts off and we
176 	   already hold the KCS lock. */
177 	spin_lock(&(kcs_info->msg_lock));
178 
179 	/* Pick the high priority queue first. */
180 	if (! list_empty(&(kcs_info->hp_xmit_msgs))) {
181 		entry = kcs_info->hp_xmit_msgs.next;
182 	} else if (! list_empty(&(kcs_info->xmit_msgs))) {
183 		entry = kcs_info->xmit_msgs.next;
184 	}
185 
186 	if (!entry) {
187 		kcs_info->curr_msg = NULL;
188 		rv = KCS_SM_IDLE;
189 	} else {
190 		int err;
191 
192 		list_del(entry);
193 		kcs_info->curr_msg = list_entry(entry,
194 						struct ipmi_smi_msg,
195 						link);
196 #ifdef DEBUG_TIMING
197 		do_gettimeofday(&t);
198 		printk("**Start2: %d.%9.9d\n", t.tv_sec, t.tv_usec);
199 #endif
200 		err = start_kcs_transaction(kcs_info->kcs_sm,
201 					   kcs_info->curr_msg->data,
202 					   kcs_info->curr_msg->data_size);
203 		if (err) {
204 			return_hosed_msg(kcs_info);
205 		}
206 
207 		rv = KCS_CALL_WITHOUT_DELAY;
208 	}
209 	spin_unlock(&(kcs_info->msg_lock));
210 
211 	return rv;
212 }
213 
start_enable_irq(struct kcs_info * kcs_info)214 static void start_enable_irq(struct kcs_info *kcs_info)
215 {
216 	unsigned char msg[2];
217 
218 	/* If we are enabling interrupts, we have to tell the
219 	   BMC to use them. */
220 	msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
221 	msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
222 
223 	start_kcs_transaction(kcs_info->kcs_sm, msg, 2);
224 	kcs_info->kcs_state = KCS_ENABLE_INTERRUPTS1;
225 }
226 
start_clear_flags(struct kcs_info * kcs_info)227 static void start_clear_flags(struct kcs_info *kcs_info)
228 {
229 	unsigned char msg[3];
230 
231 	/* Make sure the watchdog pre-timeout flag is not set at startup. */
232 	msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
233 	msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
234 	msg[2] = WDT_PRE_TIMEOUT_INT;
235 
236 	start_kcs_transaction(kcs_info->kcs_sm, msg, 3);
237 	kcs_info->kcs_state = KCS_CLEARING_FLAGS;
238 }
239 
240 /* When we have a situtaion where we run out of memory and cannot
241    allocate messages, we just leave them in the BMC and run the system
242    polled until we can allocate some memory.  Once we have some
243    memory, we will re-enable the interrupt. */
disable_kcs_irq(struct kcs_info * kcs_info)244 static inline void disable_kcs_irq(struct kcs_info *kcs_info)
245 {
246 	if ((kcs_info->irq) && (!kcs_info->interrupt_disabled)) {
247 		disable_irq_nosync(kcs_info->irq);
248 		kcs_info->interrupt_disabled = 1;
249 	}
250 }
251 
enable_kcs_irq(struct kcs_info * kcs_info)252 static inline void enable_kcs_irq(struct kcs_info *kcs_info)
253 {
254 	if ((kcs_info->irq) && (kcs_info->interrupt_disabled)) {
255 		enable_irq(kcs_info->irq);
256 		kcs_info->interrupt_disabled = 0;
257 	}
258 }
259 
handle_flags(struct kcs_info * kcs_info)260 static void handle_flags(struct kcs_info *kcs_info)
261 {
262 	if (kcs_info->msg_flags & WDT_PRE_TIMEOUT_INT) {
263 		/* Watchdog pre-timeout */
264 		start_clear_flags(kcs_info);
265 		kcs_info->msg_flags &= ~WDT_PRE_TIMEOUT_INT;
266 		spin_unlock(&(kcs_info->kcs_lock));
267 		ipmi_smi_watchdog_pretimeout(kcs_info->intf);
268 		spin_lock(&(kcs_info->kcs_lock));
269 	} else if (kcs_info->msg_flags & RECEIVE_MSG_AVAIL) {
270 		/* Messages available. */
271 		kcs_info->curr_msg = ipmi_alloc_smi_msg();
272 		if (!kcs_info->curr_msg) {
273 			disable_kcs_irq(kcs_info);
274 			kcs_info->kcs_state = KCS_NORMAL;
275 			return;
276 		}
277 		enable_kcs_irq(kcs_info);
278 
279 		kcs_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
280 		kcs_info->curr_msg->data[1] = IPMI_GET_MSG_CMD;
281 		kcs_info->curr_msg->data_size = 2;
282 
283 		start_kcs_transaction(kcs_info->kcs_sm,
284 				      kcs_info->curr_msg->data,
285 				      kcs_info->curr_msg->data_size);
286 		kcs_info->kcs_state = KCS_GETTING_MESSAGES;
287 	} else if (kcs_info->msg_flags & EVENT_MSG_BUFFER_FULL) {
288 		/* Events available. */
289 		kcs_info->curr_msg = ipmi_alloc_smi_msg();
290 		if (!kcs_info->curr_msg) {
291 			disable_kcs_irq(kcs_info);
292 			kcs_info->kcs_state = KCS_NORMAL;
293 			return;
294 		}
295 		enable_kcs_irq(kcs_info);
296 
297 		kcs_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
298 		kcs_info->curr_msg->data[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD;
299 		kcs_info->curr_msg->data_size = 2;
300 
301 		start_kcs_transaction(kcs_info->kcs_sm,
302 				      kcs_info->curr_msg->data,
303 				      kcs_info->curr_msg->data_size);
304 		kcs_info->kcs_state = KCS_GETTING_EVENTS;
305 	} else {
306 		kcs_info->kcs_state = KCS_NORMAL;
307 	}
308 }
309 
handle_transaction_done(struct kcs_info * kcs_info)310 static void handle_transaction_done(struct kcs_info *kcs_info)
311 {
312 	struct ipmi_smi_msg *msg;
313 #ifdef DEBUG_TIMING
314 	struct timeval t;
315 
316 	do_gettimeofday(&t);
317 	printk("**Done: %d.%9.9d\n", t.tv_sec, t.tv_usec);
318 #endif
319 	switch (kcs_info->kcs_state) {
320 	case KCS_NORMAL:
321 		if (!kcs_info->curr_msg)
322 			break;
323 
324 		kcs_info->curr_msg->rsp_size
325 			= kcs_get_result(kcs_info->kcs_sm,
326 					 kcs_info->curr_msg->rsp,
327 					 IPMI_MAX_MSG_LENGTH);
328 
329 		/* Do this here becase deliver_recv_msg() releases the
330 		   lock, and a new message can be put in during the
331 		   time the lock is released. */
332 		msg = kcs_info->curr_msg;
333 		kcs_info->curr_msg = NULL;
334 		deliver_recv_msg(kcs_info, msg);
335 		break;
336 
337 	case KCS_GETTING_FLAGS:
338 	{
339 		unsigned char msg[4];
340 		unsigned int  len;
341 
342 		/* We got the flags from the KCS, now handle them. */
343 		len = kcs_get_result(kcs_info->kcs_sm, msg, 4);
344 		if (msg[2] != 0) {
345 			/* Error fetching flags, just give up for
346 			   now. */
347 			kcs_info->kcs_state = KCS_NORMAL;
348 		} else if (len < 3) {
349 			/* Hmm, no flags.  That's technically illegal, but
350 			   don't use uninitialized data. */
351 			kcs_info->kcs_state = KCS_NORMAL;
352 		} else {
353 			kcs_info->msg_flags = msg[3];
354 			handle_flags(kcs_info);
355 		}
356 		break;
357 	}
358 
359 	case KCS_CLEARING_FLAGS:
360 	case KCS_CLEARING_FLAGS_THEN_SET_IRQ:
361 	{
362 		unsigned char msg[3];
363 
364 		/* We cleared the flags. */
365 		kcs_get_result(kcs_info->kcs_sm, msg, 3);
366 		if (msg[2] != 0) {
367 			/* Error clearing flags */
368 			printk(KERN_WARNING
369 			       "ipmi_kcs: Error clearing flags: %2.2x\n",
370 			       msg[2]);
371 		}
372 		if (kcs_info->kcs_state == KCS_CLEARING_FLAGS_THEN_SET_IRQ)
373 			start_enable_irq(kcs_info);
374 		else
375 			kcs_info->kcs_state = KCS_NORMAL;
376 		break;
377 	}
378 
379 	case KCS_GETTING_EVENTS:
380 	{
381 		kcs_info->curr_msg->rsp_size
382 			= kcs_get_result(kcs_info->kcs_sm,
383 					 kcs_info->curr_msg->rsp,
384 					 IPMI_MAX_MSG_LENGTH);
385 
386 		/* Do this here becase deliver_recv_msg() releases the
387 		   lock, and a new message can be put in during the
388 		   time the lock is released. */
389 		msg = kcs_info->curr_msg;
390 		kcs_info->curr_msg = NULL;
391 		if (msg->rsp[2] != 0) {
392 			/* Error getting event, probably done. */
393 			msg->done(msg);
394 
395 			/* Take off the event flag. */
396 			kcs_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
397 		} else {
398 			deliver_recv_msg(kcs_info, msg);
399 		}
400 		handle_flags(kcs_info);
401 		break;
402 	}
403 
404 	case KCS_GETTING_MESSAGES:
405 	{
406 		kcs_info->curr_msg->rsp_size
407 			= kcs_get_result(kcs_info->kcs_sm,
408 					 kcs_info->curr_msg->rsp,
409 					 IPMI_MAX_MSG_LENGTH);
410 
411 		/* Do this here becase deliver_recv_msg() releases the
412 		   lock, and a new message can be put in during the
413 		   time the lock is released. */
414 		msg = kcs_info->curr_msg;
415 		kcs_info->curr_msg = NULL;
416 		if (msg->rsp[2] != 0) {
417 			/* Error getting event, probably done. */
418 			msg->done(msg);
419 
420 			/* Take off the msg flag. */
421 			kcs_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
422 		} else {
423 			deliver_recv_msg(kcs_info, msg);
424 		}
425 		handle_flags(kcs_info);
426 		break;
427 	}
428 
429 	case KCS_ENABLE_INTERRUPTS1:
430 	{
431 		unsigned char msg[4];
432 
433 		/* We got the flags from the KCS, now handle them. */
434 		kcs_get_result(kcs_info->kcs_sm, msg, 4);
435 		if (msg[2] != 0) {
436 			printk(KERN_WARNING
437 			       "ipmi_kcs: Could not enable interrupts"
438 			       ", failed get, using polled mode.\n");
439 			kcs_info->kcs_state = KCS_NORMAL;
440 		} else {
441 			msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
442 			msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
443 			msg[2] = msg[3] | 1; /* enable msg queue int */
444 			start_kcs_transaction(kcs_info->kcs_sm, msg,3);
445 			kcs_info->kcs_state = KCS_ENABLE_INTERRUPTS2;
446 		}
447 		break;
448 	}
449 
450 	case KCS_ENABLE_INTERRUPTS2:
451 	{
452 		unsigned char msg[4];
453 
454 		/* We got the flags from the KCS, now handle them. */
455 		kcs_get_result(kcs_info->kcs_sm, msg, 4);
456 		if (msg[2] != 0) {
457 			printk(KERN_WARNING
458 			       "ipmi_kcs: Could not enable interrupts"
459 			       ", failed set, using polled mode.\n");
460 		}
461 		kcs_info->kcs_state = KCS_NORMAL;
462 		break;
463 	}
464 	}
465 }
466 
467 /* Called on timeouts and events.  Timeouts should pass the elapsed
468    time, interrupts should pass in zero. */
kcs_event_handler(struct kcs_info * kcs_info,int time)469 static enum kcs_result kcs_event_handler(struct kcs_info *kcs_info, int time)
470 {
471 	enum kcs_result kcs_result;
472 
473  restart:
474 	/* There used to be a loop here that waited a little while
475 	   (around 25us) before giving up.  That turned out to be
476 	   pointless, the minimum delays I was seeing were in the 300us
477 	   range, which is far too long to wait in an interrupt.  So
478 	   we just run until the state machine tells us something
479 	   happened or it needs a delay. */
480 	kcs_result = kcs_event(kcs_info->kcs_sm, time);
481 	time = 0;
482 	while (kcs_result == KCS_CALL_WITHOUT_DELAY)
483 	{
484 		kcs_result = kcs_event(kcs_info->kcs_sm, 0);
485 	}
486 
487 	if (kcs_result == KCS_TRANSACTION_COMPLETE)
488 	{
489 		handle_transaction_done(kcs_info);
490 		kcs_result = kcs_event(kcs_info->kcs_sm, 0);
491 	}
492 	else if (kcs_result == KCS_SM_HOSED)
493 	{
494 		if (kcs_info->curr_msg != NULL) {
495 			/* If we were handling a user message, format
496                            a response to send to the upper layer to
497                            tell it about the error. */
498 			return_hosed_msg(kcs_info);
499 		}
500 		kcs_result = kcs_event(kcs_info->kcs_sm, 0);
501 		kcs_info->kcs_state = KCS_NORMAL;
502 	}
503 
504 	/* We prefer handling attn over new messages. */
505 	if (kcs_result == KCS_ATTN)
506 	{
507 		unsigned char msg[2];
508 
509 		/* Got a attn, send down a get message flags to see
510                    what's causing it.  It would be better to handle
511                    this in the upper layer, but due to the way
512                    interrupts work with the KCS, that's not really
513                    possible. */
514 		msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
515 		msg[1] = IPMI_GET_MSG_FLAGS_CMD;
516 
517 		start_kcs_transaction(kcs_info->kcs_sm, msg, 2);
518 		kcs_info->kcs_state = KCS_GETTING_FLAGS;
519 		goto restart;
520 	}
521 
522 	/* If we are currently idle, try to start the next message. */
523 	if (kcs_result == KCS_SM_IDLE) {
524 		kcs_result = start_next_msg(kcs_info);
525 		if (kcs_result != KCS_SM_IDLE)
526 			goto restart;
527         }
528 
529 	if ((kcs_result == KCS_SM_IDLE)
530 	    && (atomic_read(&kcs_info->req_events)))
531 	{
532 		/* We are idle and the upper layer requested that I fetch
533 		   events, so do so. */
534 		unsigned char msg[2];
535 
536 		atomic_set(&kcs_info->req_events, 0);
537 		msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
538 		msg[1] = IPMI_GET_MSG_FLAGS_CMD;
539 
540 		start_kcs_transaction(kcs_info->kcs_sm, msg, 2);
541 		kcs_info->kcs_state = KCS_GETTING_FLAGS;
542 		goto restart;
543 	}
544 
545 	return kcs_result;
546 }
547 
sender(void * send_info,struct ipmi_smi_msg * msg,int priority)548 static void sender(void                *send_info,
549 		   struct ipmi_smi_msg *msg,
550 		   int                 priority)
551 {
552 	struct kcs_info *kcs_info = (struct kcs_info *) send_info;
553 	enum kcs_result result;
554 	unsigned long   flags;
555 #ifdef DEBUG_TIMING
556 	struct timeval t;
557 #endif
558 
559 	spin_lock_irqsave(&(kcs_info->msg_lock), flags);
560 #ifdef DEBUG_TIMING
561 	do_gettimeofday(&t);
562 	printk("**Enqueue: %d.%9.9d\n", t.tv_sec, t.tv_usec);
563 #endif
564 
565 	if (kcs_info->run_to_completion) {
566 		/* If we are running to completion, then throw it in
567 		   the list and run transactions until everything is
568 		   clear.  Priority doesn't matter here. */
569 		list_add_tail(&(msg->link), &(kcs_info->xmit_msgs));
570 
571 		/* We have to release the msg lock and claim the kcs
572 		   lock in this case, because of race conditions. */
573 		spin_unlock_irqrestore(&(kcs_info->msg_lock), flags);
574 
575 		spin_lock_irqsave(&(kcs_info->kcs_lock), flags);
576 		result = kcs_event_handler(kcs_info, 0);
577 		while (result != KCS_SM_IDLE) {
578 			udelay(KCS_SHORT_TIMEOUT_USEC);
579 			result = kcs_event_handler(kcs_info,
580 						   KCS_SHORT_TIMEOUT_USEC);
581 		}
582 		spin_unlock_irqrestore(&(kcs_info->kcs_lock), flags);
583 		return;
584 	} else {
585 		if (priority > 0) {
586 			list_add_tail(&(msg->link), &(kcs_info->hp_xmit_msgs));
587 		} else {
588 			list_add_tail(&(msg->link), &(kcs_info->xmit_msgs));
589 		}
590 	}
591 	spin_unlock_irqrestore(&(kcs_info->msg_lock), flags);
592 
593 	spin_lock_irqsave(&(kcs_info->kcs_lock), flags);
594 	if ((kcs_info->kcs_state == KCS_NORMAL)
595 	    && (kcs_info->curr_msg == NULL))
596 	{
597 		start_next_msg(kcs_info);
598 		kcs_restart_short_timer(kcs_info);
599 	}
600 	spin_unlock_irqrestore(&(kcs_info->kcs_lock), flags);
601 }
602 
set_run_to_completion(void * send_info,int i_run_to_completion)603 static void set_run_to_completion(void *send_info, int i_run_to_completion)
604 {
605 	struct kcs_info *kcs_info = (struct kcs_info *) send_info;
606 	enum kcs_result result;
607 	unsigned long   flags;
608 
609 	spin_lock_irqsave(&(kcs_info->kcs_lock), flags);
610 
611 	kcs_info->run_to_completion = i_run_to_completion;
612 	if (i_run_to_completion) {
613 		result = kcs_event_handler(kcs_info, 0);
614 		while (result != KCS_SM_IDLE) {
615 			udelay(KCS_SHORT_TIMEOUT_USEC);
616 			result = kcs_event_handler(kcs_info,
617 						   KCS_SHORT_TIMEOUT_USEC);
618 		}
619 	}
620 
621 	spin_unlock_irqrestore(&(kcs_info->kcs_lock), flags);
622 }
623 
request_events(void * send_info)624 static void request_events(void *send_info)
625 {
626 	struct kcs_info *kcs_info = (struct kcs_info *) send_info;
627 
628 	atomic_set(&kcs_info->req_events, 1);
629 }
630 
new_user(void * send_info)631 static int new_user(void *send_info)
632 {
633 	if (!try_inc_mod_count(THIS_MODULE))
634 		return -EBUSY;
635 	return 0;
636 }
637 
user_left(void * send_info)638 static void user_left(void *send_info)
639 {
640 	MOD_DEC_USE_COUNT;
641 }
642 
643 static int initialized = 0;
644 
645 /* Must be called with interrupts off and with the kcs_lock held. */
kcs_restart_short_timer(struct kcs_info * kcs_info)646 static void kcs_restart_short_timer(struct kcs_info *kcs_info)
647 {
648 #ifdef CONFIG_HIGH_RES_TIMERS
649 	unsigned long jiffies_now;
650 
651 	if (del_timer(&(kcs_info->kcs_timer))) {
652 		/* If we don't delete the timer, then it will go off
653 		   immediately, anyway.  So we only process if we
654 		   actually delete the timer. */
655 
656 		/* We already have irqsave on, so no need for it
657                    here. */
658 		read_lock(&xtime_lock);
659 		jiffies_now = jiffies;
660 		kcs_info->kcs_timer.expires = jiffies_now;
661 
662 		kcs_info->kcs_timer.sub_expires
663 			= quick_update_jiffies_sub(jiffies_now);
664 		read_unlock(&xtime_lock);
665 
666 		kcs_info->kcs_timer.sub_expires
667 			+= usec_to_arch_cycles(KCS_SHORT_TIMEOUT_USEC);
668 		while (kcs_info->kcs_timer.sub_expires >= cycles_per_jiffies) {
669 			kcs_info->kcs_timer.expires++;
670 			kcs_info->kcs_timer.sub_expires -= cycles_per_jiffies;
671 		}
672 		add_timer(&(kcs_info->kcs_timer));
673 	}
674 #endif
675 }
676 
kcs_timeout(unsigned long data)677 static void kcs_timeout(unsigned long data)
678 {
679 	struct kcs_info *kcs_info = (struct kcs_info *) data;
680 	enum kcs_result kcs_result;
681 	unsigned long   flags;
682 	unsigned long   jiffies_now;
683 	unsigned long   time_diff;
684 #ifdef DEBUG_TIMING
685 	struct timeval t;
686 #endif
687 
688 	if (kcs_info->stop_operation) {
689 		kcs_info->timer_stopped = 1;
690 		return;
691 	}
692 
693 	spin_lock_irqsave(&(kcs_info->kcs_lock), flags);
694 #ifdef DEBUG_TIMING
695 	do_gettimeofday(&t);
696 	printk("**Timer: %d.%9.9d\n", t.tv_sec, t.tv_usec);
697 #endif
698 	jiffies_now = jiffies;
699 
700 	time_diff = ((jiffies_now - kcs_info->last_timeout_jiffies)
701 		     * KCS_USEC_PER_JIFFY);
702 	kcs_result = kcs_event_handler(kcs_info, time_diff);
703 
704 	kcs_info->last_timeout_jiffies = jiffies_now;
705 
706 	if ((kcs_info->irq) && (! kcs_info->interrupt_disabled)) {
707 		/* Running with interrupts, only do long timeouts. */
708 		kcs_info->kcs_timer.expires = jiffies + KCS_TIMEOUT_JIFFIES;
709 		goto do_add_timer;
710 	}
711 
712 	/* If the state machine asks for a short delay, then shorten
713            the timer timeout. */
714 #ifdef CONFIG_HIGH_RES_TIMERS
715 	if (kcs_result == KCS_CALL_WITH_DELAY) {
716 		kcs_info->kcs_timer.sub_expires
717 			+= usec_to_arch_cycles(KCS_SHORT_TIMEOUT_USEC);
718 		while (kcs_info->kcs_timer.sub_expires >= cycles_per_jiffies) {
719 			kcs_info->kcs_timer.expires++;
720 			kcs_info->kcs_timer.sub_expires -= cycles_per_jiffies;
721 		}
722 	} else {
723 		kcs_info->kcs_timer.expires = jiffies + KCS_TIMEOUT_JIFFIES;
724 		kcs_info->kcs_timer.sub_expires = 0;
725 	}
726 #else
727 	/* If requested, take the shortest delay possible */
728 	if (kcs_result == KCS_CALL_WITH_DELAY) {
729 		kcs_info->kcs_timer.expires = jiffies + 1;
730 	} else {
731 		kcs_info->kcs_timer.expires = jiffies + KCS_TIMEOUT_JIFFIES;
732 	}
733 #endif
734 
735  do_add_timer:
736 	add_timer(&(kcs_info->kcs_timer));
737 	spin_unlock_irqrestore(&(kcs_info->kcs_lock), flags);
738 }
739 
kcs_irq_handler(int irq,void * data,struct pt_regs * regs)740 static void kcs_irq_handler(int irq, void *data, struct pt_regs *regs)
741 {
742 	struct kcs_info *kcs_info = (struct kcs_info *) data;
743 	unsigned long   flags;
744 #ifdef DEBUG_TIMING
745 	struct timeval t;
746 #endif
747 
748 	spin_lock_irqsave(&(kcs_info->kcs_lock), flags);
749 	if (kcs_info->stop_operation)
750 		goto out;
751 
752 #ifdef DEBUG_TIMING
753 	do_gettimeofday(&t);
754 	printk("**Interrupt: %d.%9.9d\n", t.tv_sec, t.tv_usec);
755 #endif
756 	kcs_event_handler(kcs_info, 0);
757  out:
758 	spin_unlock_irqrestore(&(kcs_info->kcs_lock), flags);
759 }
760 
761 static struct ipmi_smi_handlers handlers =
762 {
763 	sender:		       sender,
764 	request_events:        request_events,
765 	new_user:	       new_user,
766 	user_left:	       user_left,
767 	set_run_to_completion: set_run_to_completion
768 };
769 
770 static unsigned char ipmi_kcs_dev_rev;
771 static unsigned char ipmi_kcs_fw_rev_major;
772 static unsigned char ipmi_kcs_fw_rev_minor;
773 static unsigned char ipmi_version_major;
774 static unsigned char ipmi_version_minor;
775 
776 extern int kcs_dbg;
ipmi_kcs_detect_hardware(unsigned int port,unsigned char * addr,struct kcs_data * data)777 static int ipmi_kcs_detect_hardware(unsigned int port,
778 				    unsigned char *addr,
779 				    struct kcs_data *data)
780 {
781 	unsigned char   msg[2];
782 	unsigned char   resp[IPMI_MAX_MSG_LENGTH];
783 	unsigned long   resp_len;
784 	enum kcs_result kcs_result;
785 
786 	/* It's impossible for the KCS status register to be all 1's,
787 	   (assuming a properly functioning, self-initialized BMC)
788 	   but that's what you get from reading a bogus address, so we
789 	   test that first. */
790 
791 	if (port) {
792 		if (inb(port+1) == 0xff) return -ENODEV;
793 	} else {
794 		if (readb(addr+1) == 0xff) return -ENODEV;
795 	}
796 
797 	/* Do a Get Device ID command, since it comes back with some
798 	   useful info. */
799 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
800 	msg[1] = IPMI_GET_DEVICE_ID_CMD;
801 	start_kcs_transaction(data, msg, 2);
802 
803 	kcs_result = kcs_event(data, 0);
804 	for (;;)
805 	{
806 		if (kcs_result == KCS_CALL_WITH_DELAY) {
807 			udelay(100);
808 			kcs_result = kcs_event(data, 100);
809 		}
810 		else if (kcs_result == KCS_CALL_WITHOUT_DELAY)
811 		{
812 			kcs_result = kcs_event(data, 0);
813 		}
814 		else
815 			break;
816 	}
817 	if (kcs_result == KCS_SM_HOSED) {
818 		/* We couldn't get the state machine to run, so whatever's at
819 		   the port is probably not an IPMI KCS interface. */
820 		return -ENODEV;
821 	}
822 	/* Otherwise, we got some data. */
823 	resp_len = kcs_get_result(data, resp, IPMI_MAX_MSG_LENGTH);
824 	if (resp_len < 6)
825 		/* That's odd, it should be longer. */
826 		return -EINVAL;
827 
828 	if ((resp[1] != IPMI_GET_DEVICE_ID_CMD) || (resp[2] != 0))
829 		/* That's odd, it shouldn't be able to fail. */
830 		return -EINVAL;
831 
832 	ipmi_kcs_dev_rev = resp[4] & 0xf;
833 	ipmi_kcs_fw_rev_major = resp[5] & 0x7f;
834 	ipmi_kcs_fw_rev_minor = resp[6];
835 	ipmi_version_major = resp[7] & 0xf;
836 	ipmi_version_minor = resp[7] >> 4;
837 
838 	return 0;
839 }
840 
841 /* There can be 4 IO ports passed in (with or without IRQs), 4 addresses,
842    a default IO port, and 1 ACPI/SPMI address.  That sets KCS_MAX_DRIVERS */
843 
844 #define KCS_MAX_PARMS 4
845 #define KCS_MAX_DRIVERS ((KCS_MAX_PARMS * 2) + 2)
846 static struct kcs_info *kcs_infos[KCS_MAX_DRIVERS] =
847 { NULL, NULL, NULL, NULL };
848 
849 #define DEVICE_NAME "ipmi_kcs"
850 
851 #define DEFAULT_IO_PORT 0xca2
852 
853 static int kcs_trydefaults = 1;
854 static unsigned long kcs_addrs[KCS_MAX_PARMS] = { 0, 0, 0, 0 };
855 static int kcs_ports[KCS_MAX_PARMS] = { 0, 0, 0, 0 };
856 static int kcs_irqs[KCS_MAX_PARMS] = { 0, 0, 0, 0 };
857 
858 MODULE_PARM(kcs_trydefaults, "i");
859 MODULE_PARM(kcs_addrs, "1-4l");
860 MODULE_PARM(kcs_irqs, "1-4i");
861 MODULE_PARM(kcs_ports, "1-4i");
862 
863 /* Returns 0 if initialized, or negative on an error. */
init_one_kcs(int kcs_port,int irq,unsigned long kcs_physaddr,struct kcs_info ** kcs)864 static int init_one_kcs(int kcs_port,
865 			int irq,
866 			unsigned long kcs_physaddr,
867 			struct kcs_info **kcs)
868 {
869 	int		rv;
870 	struct kcs_info *new_kcs;
871 
872 	/* Did anything get passed in at all?  Both == zero disables the
873 	   driver. */
874 
875 	if (!(kcs_port || kcs_physaddr))
876 		return -ENODEV;
877 
878 	/* Only initialize a port OR a physical address on this call.
879 	   Also, IRQs can go with either ports or addresses. */
880 
881 	if (kcs_port && kcs_physaddr)
882 		return -EINVAL;
883 
884 	new_kcs = kmalloc(sizeof(*new_kcs), GFP_KERNEL);
885 	if (!new_kcs) {
886 		printk(KERN_ERR "ipmi_kcs: out of memory\n");
887 		return -ENOMEM;
888 	}
889 
890 	/* So we know not to free it unless we have allocated one. */
891 	new_kcs->kcs_sm = NULL;
892 
893 	new_kcs->addr = NULL;
894 	new_kcs->physaddr = kcs_physaddr;
895 	new_kcs->port = kcs_port;
896 
897 	if (kcs_port) {
898 		if (request_region(kcs_port, 2, DEVICE_NAME) == NULL) {
899 			kfree(new_kcs);
900 			printk(KERN_ERR
901 			       "ipmi_kcs: can't reserve port @ 0x%4.4x\n",
902 		       	       kcs_port);
903 			return -EIO;
904 		}
905 	} else {
906 		if (request_mem_region(kcs_physaddr, 2, DEVICE_NAME) == NULL) {
907 			kfree(new_kcs);
908 			printk(KERN_ERR
909 			       "ipmi_kcs: can't reserve memory @ 0x%lx\n",
910 		       	       kcs_physaddr);
911 			return -EIO;
912 		}
913 		if ((new_kcs->addr = ioremap(kcs_physaddr, 2)) == NULL) {
914 			kfree(new_kcs);
915 			printk(KERN_ERR
916 			       "ipmi_kcs: can't remap memory at 0x%lx\n",
917 		       	       kcs_physaddr);
918 			return -EIO;
919 		}
920 	}
921 
922 	new_kcs->kcs_sm = kmalloc(kcs_size(), GFP_KERNEL);
923 	if (!new_kcs->kcs_sm) {
924 		printk(KERN_ERR "ipmi_kcs: out of memory\n");
925 		rv = -ENOMEM;
926 		goto out_err;
927 	}
928 	init_kcs_data(new_kcs->kcs_sm, kcs_port, new_kcs->addr);
929 	spin_lock_init(&(new_kcs->kcs_lock));
930 	spin_lock_init(&(new_kcs->msg_lock));
931 
932 	rv = ipmi_kcs_detect_hardware(kcs_port, new_kcs->addr, new_kcs->kcs_sm);
933 	if (rv) {
934 		if (kcs_port)
935 			printk(KERN_ERR
936 			       "ipmi_kcs: No KCS @ port 0x%4.4x\n",
937 			       kcs_port);
938 		else
939 			printk(KERN_ERR
940 			       "ipmi_kcs: No KCS @ addr 0x%lx\n",
941 			       kcs_physaddr);
942 		goto out_err;
943 	}
944 
945 	if (irq != 0) {
946 		rv = request_irq(irq,
947 				 kcs_irq_handler,
948 				 SA_INTERRUPT,
949 				 DEVICE_NAME,
950 				 new_kcs);
951 		if (rv) {
952 			printk(KERN_WARNING
953 			       "ipmi_kcs: %s unable to claim interrupt %d,"
954 			       " running polled\n",
955 			       DEVICE_NAME, irq);
956 			irq = 0;
957 		}
958 	}
959 	new_kcs->irq = irq;
960 
961 	INIT_LIST_HEAD(&(new_kcs->xmit_msgs));
962 	INIT_LIST_HEAD(&(new_kcs->hp_xmit_msgs));
963 	new_kcs->curr_msg = NULL;
964 	atomic_set(&new_kcs->req_events, 0);
965 	new_kcs->run_to_completion = 0;
966 
967 	start_clear_flags(new_kcs);
968 
969 	if (irq) {
970 		new_kcs->kcs_state = KCS_CLEARING_FLAGS_THEN_SET_IRQ;
971 
972 		printk(KERN_INFO
973 		       "ipmi_kcs: Acquiring BMC @ port=0x%x irq=%d\n",
974 		       kcs_port, irq);
975 
976 	} else {
977 		if (kcs_port)
978 			printk(KERN_INFO
979 			       "ipmi_kcs: Acquiring BMC @ port=0x%x\n",
980 		       	       kcs_port);
981 		else
982 			printk(KERN_INFO
983 			       "ipmi_kcs: Acquiring BMC @ addr=0x%lx\n",
984 		       	       kcs_physaddr);
985 	}
986 
987 	rv = ipmi_register_smi(&handlers,
988 			       new_kcs,
989 			       ipmi_version_major,
990 			       ipmi_version_minor,
991 			       &(new_kcs->intf));
992 	if (rv) {
993 		free_irq(irq, new_kcs);
994 		printk(KERN_ERR
995 		       "ipmi_kcs: Unable to register device: error %d\n",
996 		       rv);
997 		goto out_err;
998 	}
999 
1000 	new_kcs->interrupt_disabled = 0;
1001 	new_kcs->timer_stopped = 0;
1002 	new_kcs->stop_operation = 0;
1003 
1004 	init_timer(&(new_kcs->kcs_timer));
1005 	new_kcs->kcs_timer.data = (long) new_kcs;
1006 	new_kcs->kcs_timer.function = kcs_timeout;
1007 	new_kcs->last_timeout_jiffies = jiffies;
1008 	new_kcs->kcs_timer.expires = jiffies + KCS_TIMEOUT_JIFFIES;
1009 	add_timer(&(new_kcs->kcs_timer));
1010 
1011 	*kcs = new_kcs;
1012 
1013 	return 0;
1014 
1015  out_err:
1016 	if (kcs_port)
1017 		release_region (kcs_port, 2);
1018 	if (new_kcs->addr)
1019 		iounmap(new_kcs->addr);
1020 	if (kcs_physaddr)
1021 		release_mem_region(kcs_physaddr, 2);
1022 	if (new_kcs->kcs_sm)
1023 		kfree(new_kcs->kcs_sm);
1024 	kfree(new_kcs);
1025 	return rv;
1026 }
1027 
1028 #ifdef CONFIG_ACPI_INTERPRETER
1029 
1030 /* Retrieve the base physical address from ACPI tables.  Originally
1031    from Hewlett-Packard simple bmc.c, a GPL KCS driver. */
1032 
1033 #include <linux/acpi.h>
1034 /* A real hack, but everything's not there yet in 2.4. */
1035 #include <acpi/acpi.h>
1036 #include <acpi/actypes.h>
1037 #include <acpi/actbl.h>
1038 
1039 struct SPMITable {
1040 	s8	Signature[4];
1041 	u32	Length;
1042 	u8	Revision;
1043 	u8	Checksum;
1044 	s8	OEMID[6];
1045 	s8	OEMTableID[8];
1046 	s8	OEMRevision[4];
1047 	s8	CreatorID[4];
1048 	s8	CreatorRevision[4];
1049 	s16	InterfaceType;
1050 	s16	SpecificationRevision;
1051 	u8	InterruptType;
1052 	u8	GPE;
1053 	s16	Reserved;
1054 	u64	GlobalSystemInterrupt;
1055 	u8	BaseAddress[12];
1056 	u8	UID[4];
1057 } __attribute__ ((packed));
1058 
acpi_find_bmc(void)1059 static unsigned long acpi_find_bmc(void)
1060 {
1061 	acpi_status       status;
1062 	struct acpi_table_header *spmi;
1063 	static unsigned long io_base = 0;
1064 
1065 	if (io_base != 0)
1066 		return io_base;
1067 
1068 	status = acpi_get_firmware_table("SPMI", 1,
1069 			ACPI_LOGICAL_ADDRESSING, &spmi);
1070 
1071 	if (status != AE_OK) {
1072 		printk(KERN_ERR "ipmi_kcs: SPMI table not found.\n");
1073 		return 0;
1074 	}
1075 
1076 	memcpy(&io_base, ((struct SPMITable *)spmi)->BaseAddress,
1077 			sizeof(io_base));
1078 
1079 	return io_base;
1080 }
1081 #endif
1082 
init_ipmi_kcs(void)1083 static __init int init_ipmi_kcs(void)
1084 {
1085 	int		rv = 0;
1086 	int		pos = 0;
1087 	int		i = 0;
1088 #ifdef CONFIG_ACPI_INTERPRETER
1089 	unsigned long	physaddr = 0;
1090 #endif
1091 
1092 	if (initialized)
1093 		return 0;
1094 	initialized = 1;
1095 
1096 	/* First do the "command-line" parameters */
1097 
1098 	for (i=0; i < KCS_MAX_PARMS; i++) {
1099 		rv = init_one_kcs(kcs_ports[i],
1100 				  kcs_irqs[i],
1101 				  0,
1102 				  &(kcs_infos[pos]));
1103 		if (rv == 0)
1104 			pos++;
1105 
1106 		rv = init_one_kcs(0,
1107 				  kcs_irqs[i],
1108 				  kcs_addrs[i],
1109 				  &(kcs_infos[pos]));
1110 		if (rv == 0)
1111 			pos++;
1112 	}
1113 
1114 	/* Only try the defaults if enabled and resources are available
1115 	   (because they weren't already specified above). */
1116 
1117 	if (kcs_trydefaults) {
1118 #ifdef CONFIG_ACPI_INTERPRETER
1119 		if ((physaddr = acpi_find_bmc())) {
1120 			if (!check_mem_region(physaddr, 2)) {
1121 				rv = init_one_kcs(0,
1122 						  0,
1123 						  physaddr,
1124 						  &(kcs_infos[pos]));
1125 				if (rv == 0)
1126 					pos++;
1127 			}
1128 		}
1129 #endif
1130 		if (!check_region(DEFAULT_IO_PORT, 2)) {
1131 			rv = init_one_kcs(DEFAULT_IO_PORT,
1132 					  0,
1133 					  0,
1134 					  &(kcs_infos[pos]));
1135 			if (rv == 0)
1136 				pos++;
1137 		}
1138 	}
1139 
1140 	if (kcs_infos[0] == NULL) {
1141 		printk("ipmi_kcs: Unable to find any KCS interfaces\n");
1142 		return -ENODEV;
1143 	}
1144 
1145 	return 0;
1146 }
1147 module_init(init_ipmi_kcs);
1148 
1149 #ifdef MODULE
cleanup_one_kcs(struct kcs_info * to_clean)1150 void __exit cleanup_one_kcs(struct kcs_info *to_clean)
1151 {
1152 	int           rv;
1153 	unsigned long flags;
1154 
1155 	if (! to_clean)
1156 		return;
1157 
1158 	/* Tell the timer and interrupt handlers that we are shutting
1159 	   down. */
1160 	spin_lock_irqsave(&(to_clean->kcs_lock), flags);
1161 	spin_lock(&(to_clean->msg_lock));
1162 
1163 	to_clean->stop_operation = 1;
1164 
1165 	if (to_clean->irq != 0)
1166 		free_irq(to_clean->irq, to_clean);
1167 	if (to_clean->port) {
1168 		printk(KERN_INFO
1169 		       "ipmi_kcs: Releasing BMC @ port=0x%x\n",
1170 		       to_clean->port);
1171 		release_region (to_clean->port, 2);
1172 	}
1173 	if (to_clean->addr) {
1174 		printk(KERN_INFO
1175 		       "ipmi_kcs: Releasing BMC @ addr=0x%lx\n",
1176 		       to_clean->physaddr);
1177 		iounmap(to_clean->addr);
1178 		release_mem_region(to_clean->physaddr, 2);
1179 	}
1180 
1181 	spin_unlock(&(to_clean->msg_lock));
1182 	spin_unlock_irqrestore(&(to_clean->kcs_lock), flags);
1183 
1184 	/* Wait for the timer to stop.  This avoids problems with race
1185 	   conditions removing the timer here.  Hopefully this will be
1186 	   long enough to avoid problems with interrupts still
1187 	   running. */
1188 	schedule_timeout(2);
1189 	while (!to_clean->timer_stopped) {
1190 		schedule_timeout(1);
1191 	}
1192 
1193 	rv = ipmi_unregister_smi(to_clean->intf);
1194 	if (rv) {
1195 		printk(KERN_ERR
1196 		       "ipmi_kcs: Unable to unregister device: errno=%d\n",
1197 		       rv);
1198 	}
1199 
1200 	initialized = 0;
1201 
1202 	kfree(to_clean->kcs_sm);
1203 	kfree(to_clean);
1204 }
1205 
cleanup_ipmi_kcs(void)1206 static __exit void cleanup_ipmi_kcs(void)
1207 {
1208 	int i;
1209 
1210 	if (!initialized)
1211 		return;
1212 
1213 	for (i=0; i<KCS_MAX_DRIVERS; i++) {
1214 		cleanup_one_kcs(kcs_infos[i]);
1215 	}
1216 }
1217 module_exit(cleanup_ipmi_kcs);
1218 #else
1219 
1220 /* Unfortunately, cmdline::get_options() only returns integers, not
1221    longs.  Since we need ulongs (64-bit physical addresses) parse the
1222    comma-separated list manually.  Arguments can be one of these forms:
1223    m0xaabbccddeeff	A physical memory address without an IRQ
1224    m0xaabbccddeeff:cc	A physical memory address with an IRQ
1225    p0xaabb		An IO port without an IRQ
1226    p0xaabb:cc		An IO port with an IRQ
1227    nodefaults		Suppress trying the default IO port or ACPI address
1228 
1229    For example, to pass one IO port with an IRQ, one address, and
1230    suppress the use of the default IO port and ACPI address,
1231    use this option string: ipmi_kcs=p0xCA2:5,m0xFF5B0022,nodefaults
1232 
1233    Remember, ipmi_kcs_setup() is passed the string after the equal sign. */
1234 
ipmi_kcs_setup(char * str)1235 static int __init ipmi_kcs_setup(char *str)
1236 {
1237 	unsigned long val;
1238 	char *cur, *colon;
1239 	int pos;
1240 
1241 	pos = 0;
1242 
1243 	cur = strsep(&str, ",");
1244 	while ((cur) && (*cur) && (pos < KCS_MAX_PARMS)) {
1245 		switch (*cur) {
1246 		case 'n':
1247 			if (strcmp(cur, "nodefaults") == 0)
1248 				kcs_trydefaults = 0;
1249 			else
1250 				printk(KERN_INFO
1251 				       "ipmi_kcs: bad parameter value %s\n",
1252 				       cur);
1253 			break;
1254 
1255 		case 'm':
1256 		case 'p':
1257 			val = simple_strtoul(cur + 1,
1258 					     &colon,
1259 					     0);
1260 			if (*cur == 'p')
1261 				kcs_ports[pos] = val;
1262 			else
1263 				kcs_addrs[pos] = val;
1264 			if (*colon == ':') {
1265 				val = simple_strtoul(colon + 1,
1266 						     &colon,
1267 						     0);
1268 				kcs_irqs[pos] = val;
1269 			}
1270 			pos++;
1271 			break;
1272 
1273 		default:
1274 			printk(KERN_INFO
1275 			       "ipmi_kcs: bad parameter value %s\n",
1276 			       cur);
1277 		}
1278 		cur = strsep(&str, ",");
1279 	}
1280 
1281 	return 1;
1282 }
1283 __setup("ipmi_kcs=", ipmi_kcs_setup);
1284 #endif
1285 
1286 MODULE_LICENSE("GPL");
1287