blob: 60f2f968b6893ddc19941fc25d24512d9c2caf8a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ipmi_si.c
3 *
4 * The interface to the IPMI driver for the system interfaces (KCS, SMIC,
5 * BT).
6 *
7 * Author: MontaVista Software, Inc.
8 * Corey Minyard <minyard@mvista.com>
9 * source@mvista.com
10 *
11 * Copyright 2002 MontaVista Software Inc.
12 *
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.
17 *
18 *
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.
29 *
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.
33 */
34
35/*
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.
39 */
40
41#include <linux/config.h>
42#include <linux/module.h>
43#include <linux/moduleparam.h>
44#include <asm/system.h>
45#include <linux/sched.h>
46#include <linux/timer.h>
47#include <linux/errno.h>
48#include <linux/spinlock.h>
49#include <linux/slab.h>
50#include <linux/delay.h>
51#include <linux/list.h>
52#include <linux/pci.h>
53#include <linux/ioport.h>
54#include <asm/irq.h>
55#ifdef CONFIG_HIGH_RES_TIMERS
56#include <linux/hrtime.h>
57# if defined(schedule_next_int)
58/* Old high-res timer code, do translations. */
59# define get_arch_cycles(a) quick_update_jiffies_sub(a)
60# define arch_cycles_per_jiffy cycles_per_jiffies
61# endif
62static inline void add_usec_to_timer(struct timer_list *t, long v)
63{
Corey Minyard75b07682005-09-06 15:18:38 -070064 t->arch_cycle_expires += nsec_to_arch_cycle(v * 1000);
65 while (t->arch_cycle_expires >= arch_cycles_per_jiffy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 {
67 t->expires++;
Corey Minyard75b07682005-09-06 15:18:38 -070068 t->arch_cycle_expires -= arch_cycles_per_jiffy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 }
70}
71#endif
72#include <linux/interrupt.h>
73#include <linux/rcupdate.h>
74#include <linux/ipmi_smi.h>
75#include <asm/io.h>
76#include "ipmi_si_sm.h"
77#include <linux/init.h>
Andrey Paninb224cd32005-09-06 15:18:37 -070078#include <linux/dmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80#define IPMI_SI_VERSION "v33"
81
82/* Measure times between events in the driver. */
83#undef DEBUG_TIMING
84
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
90 short timeout */
91
92enum si_intf_state {
93 SI_NORMAL,
94 SI_GETTING_FLAGS,
95 SI_GETTING_EVENTS,
96 SI_CLEARING_FLAGS,
97 SI_CLEARING_FLAGS_THEN_SET_IRQ,
98 SI_GETTING_MESSAGES,
99 SI_ENABLE_INTERRUPTS1,
100 SI_ENABLE_INTERRUPTS2
101 /* FIXME - add watchdog stuff. */
102};
103
Corey Minyard9dbf68f2005-05-01 08:59:11 -0700104/* 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
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109enum si_type {
110 SI_KCS, SI_SMIC, SI_BT
111};
112
113struct smi_info
114{
115 ipmi_smi_t intf;
116 struct si_sm_data *si_sm;
117 struct si_sm_handlers *handlers;
118 enum si_type si_type;
119 spinlock_t si_lock;
120 spinlock_t msg_lock;
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;
125
126 /* Used to handle the various types of I/O that can occur with
127 IPMI */
128 struct si_sm_io io;
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;
134
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
137 from the flags. */
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;
142
143 /* If set to true, this will request events the next time the
144 state machine is idle. */
145 atomic_t req_events;
146
147 /* If true, run the state machine to completion on every send
148 call. Generally used after a panic to make sure stuff goes
149 out. */
150 int run_to_completion;
151
152 /* The I/O port of an SI interface. */
153 int port;
154
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;
159
160 /* zero if no irq; */
161 int irq;
162
163 /* The timer for this si. */
164 struct timer_list si_timer;
165
166 /* The time (in jiffies) the last timeout occurred at. */
167 unsigned long last_timeout_jiffies;
168
169 /* Used to gracefully stop the timer without race conditions. */
170 volatile int stop_operation;
171 volatile int timer_stopped;
172
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
176 interrupts. */
177 int interrupt_disabled;
178
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;
184
185 /* Slave address, could be reported from DMI. */
186 unsigned char slave_addr;
187
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;
193 unsigned long idles;
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;
202};
203
204static void si_restart_short_timer(struct smi_info *smi_info);
205
206static void deliver_recv_msg(struct smi_info *smi_info,
207 struct ipmi_smi_msg *msg)
208{
209 /* Deliver the message to the upper layer with the lock
210 released. */
211 spin_unlock(&(smi_info->si_lock));
212 ipmi_smi_msg_received(smi_info->intf, msg);
213 spin_lock(&(smi_info->si_lock));
214}
215
216static void return_hosed_msg(struct smi_info *smi_info)
217{
218 struct ipmi_smi_msg *msg = smi_info->curr_msg;
219
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. */
224 msg->rsp_size = 3;
225
226 smi_info->curr_msg = NULL;
227 deliver_recv_msg(smi_info, msg);
228}
229
230static enum si_sm_result start_next_msg(struct smi_info *smi_info)
231{
232 int rv;
233 struct list_head *entry = NULL;
234#ifdef DEBUG_TIMING
235 struct timeval t;
236#endif
237
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));
241
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;
247 }
248
249 if (!entry) {
250 smi_info->curr_msg = NULL;
251 rv = SI_SM_IDLE;
252 } else {
253 int err;
254
255 list_del(entry);
256 smi_info->curr_msg = list_entry(entry,
257 struct ipmi_smi_msg,
258 link);
259#ifdef DEBUG_TIMING
260 do_gettimeofday(&t);
261 printk("**Start2: %d.%9.9d\n", t.tv_sec, t.tv_usec);
262#endif
263 err = smi_info->handlers->start_transaction(
264 smi_info->si_sm,
265 smi_info->curr_msg->data,
266 smi_info->curr_msg->data_size);
267 if (err) {
268 return_hosed_msg(smi_info);
269 }
270
271 rv = SI_SM_CALL_WITHOUT_DELAY;
272 }
273 spin_unlock(&(smi_info->msg_lock));
274
275 return rv;
276}
277
278static void start_enable_irq(struct smi_info *smi_info)
279{
280 unsigned char msg[2];
281
282 /* If we are enabling interrupts, we have to tell the
283 BMC to use them. */
284 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
285 msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
286
287 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
288 smi_info->si_state = SI_ENABLE_INTERRUPTS1;
289}
290
291static void start_clear_flags(struct smi_info *smi_info)
292{
293 unsigned char msg[3];
294
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;
299
300 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 3);
301 smi_info->si_state = SI_CLEARING_FLAGS;
302}
303
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. */
308static inline void disable_si_irq(struct smi_info *smi_info)
309{
310 if ((smi_info->irq) && (!smi_info->interrupt_disabled)) {
311 disable_irq_nosync(smi_info->irq);
312 smi_info->interrupt_disabled = 1;
313 }
314}
315
316static inline void enable_si_irq(struct smi_info *smi_info)
317{
318 if ((smi_info->irq) && (smi_info->interrupt_disabled)) {
319 enable_irq(smi_info->irq);
320 smi_info->interrupt_disabled = 0;
321 }
322}
323
324static void handle_flags(struct smi_info *smi_info)
325{
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);
331
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;
343 return;
344 }
345 enable_si_irq(smi_info);
346
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;
350
351 smi_info->handlers->start_transaction(
352 smi_info->si_sm,
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;
362 return;
363 }
364 enable_si_irq(smi_info);
365
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;
369
370 smi_info->handlers->start_transaction(
371 smi_info->si_sm,
372 smi_info->curr_msg->data,
373 smi_info->curr_msg->data_size);
374 smi_info->si_state = SI_GETTING_EVENTS;
375 } else {
376 smi_info->si_state = SI_NORMAL;
377 }
378}
379
380static void handle_transaction_done(struct smi_info *smi_info)
381{
382 struct ipmi_smi_msg *msg;
383#ifdef DEBUG_TIMING
384 struct timeval t;
385
386 do_gettimeofday(&t);
387 printk("**Done: %d.%9.9d\n", t.tv_sec, t.tv_usec);
388#endif
389 switch (smi_info->si_state) {
390 case SI_NORMAL:
391 if (!smi_info->curr_msg)
392 break;
393
394 smi_info->curr_msg->rsp_size
395 = smi_info->handlers->get_result(
396 smi_info->si_sm,
397 smi_info->curr_msg->rsp,
398 IPMI_MAX_MSG_LENGTH);
399
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);
406 break;
407
408 case SI_GETTING_FLAGS:
409 {
410 unsigned char msg[4];
411 unsigned int len;
412
413 /* We got the flags from the SMI, now handle them. */
414 len = smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
415 if (msg[2] != 0) {
416 /* Error fetching flags, just give up for
417 now. */
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;
423 } else {
424 smi_info->msg_flags = msg[3];
425 handle_flags(smi_info);
426 }
427 break;
428 }
429
430 case SI_CLEARING_FLAGS:
431 case SI_CLEARING_FLAGS_THEN_SET_IRQ:
432 {
433 unsigned char msg[3];
434
435 /* We cleared the flags. */
436 smi_info->handlers->get_result(smi_info->si_sm, msg, 3);
437 if (msg[2] != 0) {
438 /* Error clearing flags */
439 printk(KERN_WARNING
440 "ipmi_si: Error clearing flags: %2.2x\n",
441 msg[2]);
442 }
443 if (smi_info->si_state == SI_CLEARING_FLAGS_THEN_SET_IRQ)
444 start_enable_irq(smi_info);
445 else
446 smi_info->si_state = SI_NORMAL;
447 break;
448 }
449
450 case SI_GETTING_EVENTS:
451 {
452 smi_info->curr_msg->rsp_size
453 = smi_info->handlers->get_result(
454 smi_info->si_sm,
455 smi_info->curr_msg->rsp,
456 IPMI_MAX_MSG_LENGTH);
457
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. */
465 msg->done(msg);
466
467 /* Take off the event flag. */
468 smi_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
469 handle_flags(smi_info);
470 } else {
471 spin_lock(&smi_info->count_lock);
472 smi_info->events++;
473 spin_unlock(&smi_info->count_lock);
474
475 /* Do this before we deliver the message
476 because delivering the message releases the
477 lock and something else can mess with the
478 state. */
479 handle_flags(smi_info);
480
481 deliver_recv_msg(smi_info, msg);
482 }
483 break;
484 }
485
486 case SI_GETTING_MESSAGES:
487 {
488 smi_info->curr_msg->rsp_size
489 = smi_info->handlers->get_result(
490 smi_info->si_sm,
491 smi_info->curr_msg->rsp,
492 IPMI_MAX_MSG_LENGTH);
493
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. */
501 msg->done(msg);
502
503 /* Take off the msg flag. */
504 smi_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
505 handle_flags(smi_info);
506 } else {
507 spin_lock(&smi_info->count_lock);
508 smi_info->incoming_messages++;
509 spin_unlock(&smi_info->count_lock);
510
511 /* Do this before we deliver the message
512 because delivering the message releases the
513 lock and something else can mess with the
514 state. */
515 handle_flags(smi_info);
516
517 deliver_recv_msg(smi_info, msg);
518 }
519 break;
520 }
521
522 case SI_ENABLE_INTERRUPTS1:
523 {
524 unsigned char msg[4];
525
526 /* We got the flags from the SMI, now handle them. */
527 smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
528 if (msg[2] != 0) {
529 printk(KERN_WARNING
530 "ipmi_si: Could not enable interrupts"
531 ", failed get, using polled mode.\n");
532 smi_info->si_state = SI_NORMAL;
533 } else {
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;
540 }
541 break;
542 }
543
544 case SI_ENABLE_INTERRUPTS2:
545 {
546 unsigned char msg[4];
547
548 /* We got the flags from the SMI, now handle them. */
549 smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
550 if (msg[2] != 0) {
551 printk(KERN_WARNING
552 "ipmi_si: Could not enable interrupts"
553 ", failed set, using polled mode.\n");
554 }
555 smi_info->si_state = SI_NORMAL;
556 break;
557 }
558 }
559}
560
561/* Called on timeouts and events. Timeouts should pass the elapsed
562 time, interrupts should pass in zero. */
563static enum si_sm_result smi_event_handler(struct smi_info *smi_info,
564 int time)
565{
566 enum si_sm_result si_sm_result;
567
568 restart:
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);
576 time = 0;
577 while (si_sm_result == SI_SM_CALL_WITHOUT_DELAY)
578 {
579 si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
580 }
581
582 if (si_sm_result == SI_SM_TRANSACTION_COMPLETE)
583 {
584 spin_lock(&smi_info->count_lock);
585 smi_info->complete_transactions++;
586 spin_unlock(&smi_info->count_lock);
587
588 handle_transaction_done(smi_info);
589 si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
590 }
591 else if (si_sm_result == SI_SM_HOSED)
592 {
593 spin_lock(&smi_info->count_lock);
594 smi_info->hosed_count++;
595 spin_unlock(&smi_info->count_lock);
596
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);
605 }
606 si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
607 }
608
609 /* We prefer handling attn over new messages. */
610 if (si_sm_result == SI_SM_ATTN)
611 {
612 unsigned char msg[2];
613
614 spin_lock(&smi_info->count_lock);
615 smi_info->attentions++;
616 spin_unlock(&smi_info->count_lock);
617
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
622 possible. */
623 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
624 msg[1] = IPMI_GET_MSG_FLAGS_CMD;
625
626 smi_info->handlers->start_transaction(
627 smi_info->si_sm, msg, 2);
628 smi_info->si_state = SI_GETTING_FLAGS;
629 goto restart;
630 }
631
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);
635 smi_info->idles++;
636 spin_unlock(&smi_info->count_lock);
637
638 si_sm_result = start_next_msg(smi_info);
639 if (si_sm_result != SI_SM_IDLE)
640 goto restart;
641 }
642
643 if ((si_sm_result == SI_SM_IDLE)
644 && (atomic_read(&smi_info->req_events)))
645 {
646 /* We are idle and the upper layer requested that I fetch
647 events, so do so. */
648 unsigned char msg[2];
649
650 spin_lock(&smi_info->count_lock);
651 smi_info->flag_fetches++;
652 spin_unlock(&smi_info->count_lock);
653
654 atomic_set(&smi_info->req_events, 0);
655 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
656 msg[1] = IPMI_GET_MSG_FLAGS_CMD;
657
658 smi_info->handlers->start_transaction(
659 smi_info->si_sm, msg, 2);
660 smi_info->si_state = SI_GETTING_FLAGS;
661 goto restart;
662 }
663
664 return si_sm_result;
665}
666
667static void sender(void *send_info,
668 struct ipmi_smi_msg *msg,
669 int priority)
670{
671 struct smi_info *smi_info = send_info;
672 enum si_sm_result result;
673 unsigned long flags;
674#ifdef DEBUG_TIMING
675 struct timeval t;
676#endif
677
678 spin_lock_irqsave(&(smi_info->msg_lock), flags);
679#ifdef DEBUG_TIMING
680 do_gettimeofday(&t);
681 printk("**Enqueue: %d.%9.9d\n", t.tv_sec, t.tv_usec);
682#endif
683
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));
689
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);
693
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);
700 }
701 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
702 return;
703 } else {
704 if (priority > 0) {
705 list_add_tail(&(msg->link), &(smi_info->hp_xmit_msgs));
706 } else {
707 list_add_tail(&(msg->link), &(smi_info->xmit_msgs));
708 }
709 }
710 spin_unlock_irqrestore(&(smi_info->msg_lock), flags);
711
712 spin_lock_irqsave(&(smi_info->si_lock), flags);
713 if ((smi_info->si_state == SI_NORMAL)
714 && (smi_info->curr_msg == NULL))
715 {
716 start_next_msg(smi_info);
717 si_restart_short_timer(smi_info);
718 }
719 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
720}
721
722static void set_run_to_completion(void *send_info, int i_run_to_completion)
723{
724 struct smi_info *smi_info = send_info;
725 enum si_sm_result result;
726 unsigned long flags;
727
728 spin_lock_irqsave(&(smi_info->si_lock), flags);
729
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);
737 }
738 }
739
740 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
741}
742
743static void poll(void *send_info)
744{
745 struct smi_info *smi_info = send_info;
746
747 smi_event_handler(smi_info, 0);
748}
749
750static void request_events(void *send_info)
751{
752 struct smi_info *smi_info = send_info;
753
754 atomic_set(&smi_info->req_events, 1);
755}
756
757static int initialized = 0;
758
759/* Must be called with interrupts off and with the si_lock held. */
760static void si_restart_short_timer(struct smi_info *smi_info)
761{
762#if defined(CONFIG_HIGH_RES_TIMERS)
763 unsigned long flags;
764 unsigned long jiffies_now;
Corey Minyard75b07682005-09-06 15:18:38 -0700765 unsigned long seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
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. */
771
Corey Minyard75b07682005-09-06 15:18:38 -0700772 do {
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));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 add_usec_to_timer(&smi_info->si_timer, SI_SHORT_TIMEOUT_USEC);
781
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);
786 }
787#endif
788}
789
790static void smi_timeout(unsigned long data)
791{
792 struct smi_info *smi_info = (struct smi_info *) data;
793 enum si_sm_result smi_result;
794 unsigned long flags;
795 unsigned long jiffies_now;
796 unsigned long time_diff;
797#ifdef DEBUG_TIMING
798 struct timeval t;
799#endif
800
801 if (smi_info->stop_operation) {
802 smi_info->timer_stopped = 1;
803 return;
804 }
805
806 spin_lock_irqsave(&(smi_info->si_lock), flags);
807#ifdef DEBUG_TIMING
808 do_gettimeofday(&t);
809 printk("**Timer: %d.%9.9d\n", t.tv_sec, t.tv_usec);
810#endif
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);
815
816 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
817
818 smi_info->last_timeout_jiffies = jiffies_now;
819
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);
826 goto do_add_timer;
827 }
828
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) {
Corey Minyard75b07682005-09-06 15:18:38 -0700832#if defined(CONFIG_HIGH_RES_TIMERS)
833 unsigned long seq;
834#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 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)
Corey Minyard75b07682005-09-06 15:18:38 -0700839 do {
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));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 add_usec_to_timer(&smi_info->si_timer, SI_SHORT_TIMEOUT_USEC);
846#else
847 smi_info->si_timer.expires = jiffies + 1;
848#endif
849 } else {
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)
Corey Minyard75b07682005-09-06 15:18:38 -0700855 smi_info->si_timer.arch_cycle_expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856#endif
857 }
858
859 do_add_timer:
860 add_timer(&(smi_info->si_timer));
861}
862
863static irqreturn_t si_irq_handler(int irq, void *data, struct pt_regs *regs)
864{
865 struct smi_info *smi_info = data;
866 unsigned long flags;
867#ifdef DEBUG_TIMING
868 struct timeval t;
869#endif
870
871 spin_lock_irqsave(&(smi_info->si_lock), flags);
872
873 spin_lock(&smi_info->count_lock);
874 smi_info->interrupts++;
875 spin_unlock(&smi_info->count_lock);
876
877 if (smi_info->stop_operation)
878 goto out;
879
880#ifdef DEBUG_TIMING
881 do_gettimeofday(&t);
882 printk("**Interrupt: %d.%9.9d\n", t.tv_sec, t.tv_usec);
883#endif
884 smi_event_handler(smi_info, 0);
885 out:
886 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
887 return IRQ_HANDLED;
888}
889
Corey Minyard9dbf68f2005-05-01 08:59:11 -0700890static irqreturn_t si_bt_irq_handler(int irq, void *data, struct pt_regs *regs)
891{
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);
898}
899
900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901static struct ipmi_smi_handlers handlers =
902{
903 .owner = THIS_MODULE,
904 .sender = sender,
905 .request_events = request_events,
906 .set_run_to_completion = set_run_to_completion,
907 .poll = poll,
908};
909
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 */
912
913#define SI_MAX_PARMS 4
914#define SI_MAX_DRIVERS ((SI_MAX_PARMS * 2) + 2)
915static struct smi_info *smi_infos[SI_MAX_DRIVERS] =
916{ NULL, NULL, NULL, NULL };
917
918#define DEVICE_NAME "ipmi_si"
919
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
924
925static int si_trydefaults = 1;
926static char *si_type[SI_MAX_PARMS];
927#define MAX_SI_TYPE_STR 30
928static char si_type_str[MAX_SI_TYPE_STR];
929static unsigned long addrs[SI_MAX_PARMS];
930static int num_addrs;
931static unsigned int ports[SI_MAX_PARMS];
932static int num_ports;
933static int irqs[SI_MAX_PARMS];
934static int num_irqs;
935static int regspacings[SI_MAX_PARMS];
936static int num_regspacings = 0;
937static int regsizes[SI_MAX_PARMS];
938static int num_regsizes = 0;
939static int regshifts[SI_MAX_PARMS];
940static int num_regshifts = 0;
941static int slave_addrs[SI_MAX_PARMS];
942static int num_slave_addrs = 0;
943
944
945module_param_named(trydefaults, si_trydefaults, bool, 0);
946MODULE_PARM_DESC(trydefaults, "Setting this to 'false' will disable the"
947 " default scan of the KCS and SMIC interface at the standard"
948 " address");
949module_param_string(type, si_type_str, MAX_SI_TYPE_STR, 0);
950MODULE_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");
954module_param_array(addrs, long, &num_addrs, 0);
955MODULE_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"
958 " it blank.");
959module_param_array(ports, int, &num_ports, 0);
960MODULE_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"
963 " it blank.");
964module_param_array(irqs, int, &num_irqs, 0);
965MODULE_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"
968 " it blank.");
969module_param_array(regspacings, int, &num_regspacings, 0);
970MODULE_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"
974 " to 1.");
975module_param_array(regsizes, int, &num_regsizes, 0);
976MODULE_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"
980 " register.");
981module_param_array(regshifts, int, &num_regshifts, 0);
982MODULE_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");
986module_param_array(slave_addrs, int, &num_slave_addrs, 0);
987MODULE_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.");
991
992
993#define IPMI_MEM_ADDR_SPACE 1
994#define IPMI_IO_ADDR_SPACE 2
995
996#if defined(CONFIG_ACPI_INTERPRETER) || defined(CONFIG_X86) || defined(CONFIG_PCI)
997static int is_new_interface(int intf, u8 addr_space, unsigned long base_addr)
998{
999 int i;
1000
1001 for (i = 0; i < SI_MAX_PARMS; ++i) {
1002 /* Don't check our address. */
1003 if (i == intf)
1004 continue;
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]))
1010 return 0;
1011 }
1012 else
1013 break;
1014 }
1015
1016 return 1;
1017}
1018#endif
1019
1020static int std_irq_setup(struct smi_info *info)
1021{
1022 int rv;
1023
1024 if (!info->irq)
1025 return 0;
1026
Corey Minyard9dbf68f2005-05-01 08:59:11 -07001027 if (info->si_type == SI_BT) {
1028 rv = request_irq(info->irq,
1029 si_bt_irq_handler,
1030 SA_INTERRUPT,
1031 DEVICE_NAME,
1032 info);
1033 if (!rv)
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);
1037 } else
1038 rv = request_irq(info->irq,
1039 si_irq_handler,
1040 SA_INTERRUPT,
1041 DEVICE_NAME,
1042 info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 if (rv) {
1044 printk(KERN_WARNING
1045 "ipmi_si: %s unable to claim interrupt %d,"
1046 " running polled\n",
1047 DEVICE_NAME, info->irq);
1048 info->irq = 0;
1049 } else {
1050 printk(" Using irq %d\n", info->irq);
1051 }
1052
1053 return rv;
1054}
1055
1056static void std_irq_cleanup(struct smi_info *info)
1057{
1058 if (!info->irq)
1059 return;
1060
Corey Minyard9dbf68f2005-05-01 08:59:11 -07001061 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 free_irq(info->irq, info);
1065}
1066
1067static unsigned char port_inb(struct si_sm_io *io, unsigned int offset)
1068{
1069 unsigned int *addr = io->info;
1070
1071 return inb((*addr)+(offset*io->regspacing));
1072}
1073
1074static void port_outb(struct si_sm_io *io, unsigned int offset,
1075 unsigned char b)
1076{
1077 unsigned int *addr = io->info;
1078
1079 outb(b, (*addr)+(offset * io->regspacing));
1080}
1081
1082static unsigned char port_inw(struct si_sm_io *io, unsigned int offset)
1083{
1084 unsigned int *addr = io->info;
1085
1086 return (inw((*addr)+(offset * io->regspacing)) >> io->regshift) & 0xff;
1087}
1088
1089static void port_outw(struct si_sm_io *io, unsigned int offset,
1090 unsigned char b)
1091{
1092 unsigned int *addr = io->info;
1093
1094 outw(b << io->regshift, (*addr)+(offset * io->regspacing));
1095}
1096
1097static unsigned char port_inl(struct si_sm_io *io, unsigned int offset)
1098{
1099 unsigned int *addr = io->info;
1100
1101 return (inl((*addr)+(offset * io->regspacing)) >> io->regshift) & 0xff;
1102}
1103
1104static void port_outl(struct si_sm_io *io, unsigned int offset,
1105 unsigned char b)
1106{
1107 unsigned int *addr = io->info;
1108
1109 outl(b << io->regshift, (*addr)+(offset * io->regspacing));
1110}
1111
1112static void port_cleanup(struct smi_info *info)
1113{
1114 unsigned int *addr = info->io.info;
1115 int mapsize;
1116
1117 if (addr && (*addr)) {
1118 mapsize = ((info->io_size * info->io.regspacing)
1119 - (info->io.regspacing - info->io.regsize));
1120
1121 release_region (*addr, mapsize);
1122 }
1123 kfree(info);
1124}
1125
1126static int port_setup(struct smi_info *info)
1127{
1128 unsigned int *addr = info->io.info;
1129 int mapsize;
1130
1131 if (!addr || (!*addr))
1132 return -ENODEV;
1133
1134 info->io_cleanup = port_cleanup;
1135
1136 /* Figure out the actual inb/inw/inl/etc routine to use based
1137 upon the register size. */
1138 switch (info->io.regsize) {
1139 case 1:
1140 info->io.inputb = port_inb;
1141 info->io.outputb = port_outb;
1142 break;
1143 case 2:
1144 info->io.inputb = port_inw;
1145 info->io.outputb = port_outw;
1146 break;
1147 case 4:
1148 info->io.inputb = port_inl;
1149 info->io.outputb = port_outl;
1150 break;
1151 default:
1152 printk("ipmi_si: Invalid register size: %d\n",
1153 info->io.regsize);
1154 return -EINVAL;
1155 }
1156
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
1161 * register. */
1162 mapsize = ((info->io_size * info->io.regspacing)
1163 - (info->io.regspacing - info->io.regsize));
1164
1165 if (request_region(*addr, mapsize, DEVICE_NAME) == NULL)
1166 return -EIO;
1167 return 0;
1168}
1169
1170static int try_init_port(int intf_num, struct smi_info **new_info)
1171{
1172 struct smi_info *info;
1173
1174 if (!ports[intf_num])
1175 return -ENODEV;
1176
1177 if (!is_new_interface(intf_num, IPMI_IO_ADDR_SPACE,
1178 ports[intf_num]))
1179 return -ENODEV;
1180
1181 info = kmalloc(sizeof(*info), GFP_KERNEL);
1182 if (!info) {
1183 printk(KERN_ERR "ipmi_si: Could not allocate SI data (1)\n");
1184 return -ENOMEM;
1185 }
1186 memset(info, 0, sizeof(*info));
1187
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];
1198 info->irq = 0;
1199 info->irq_setup = NULL;
1200 *new_info = info;
1201
1202 if (si_type[intf_num] == NULL)
1203 si_type[intf_num] = "kcs";
1204
1205 printk("ipmi_si: Trying \"%s\" at I/O port 0x%x\n",
1206 si_type[intf_num], ports[intf_num]);
1207 return 0;
1208}
1209
1210static unsigned char mem_inb(struct si_sm_io *io, unsigned int offset)
1211{
1212 return readb((io->addr)+(offset * io->regspacing));
1213}
1214
1215static void mem_outb(struct si_sm_io *io, unsigned int offset,
1216 unsigned char b)
1217{
1218 writeb(b, (io->addr)+(offset * io->regspacing));
1219}
1220
1221static unsigned char mem_inw(struct si_sm_io *io, unsigned int offset)
1222{
1223 return (readw((io->addr)+(offset * io->regspacing)) >> io->regshift)
1224 && 0xff;
1225}
1226
1227static void mem_outw(struct si_sm_io *io, unsigned int offset,
1228 unsigned char b)
1229{
1230 writeb(b << io->regshift, (io->addr)+(offset * io->regspacing));
1231}
1232
1233static unsigned char mem_inl(struct si_sm_io *io, unsigned int offset)
1234{
1235 return (readl((io->addr)+(offset * io->regspacing)) >> io->regshift)
1236 && 0xff;
1237}
1238
1239static void mem_outl(struct si_sm_io *io, unsigned int offset,
1240 unsigned char b)
1241{
1242 writel(b << io->regshift, (io->addr)+(offset * io->regspacing));
1243}
1244
1245#ifdef readq
1246static unsigned char mem_inq(struct si_sm_io *io, unsigned int offset)
1247{
1248 return (readq((io->addr)+(offset * io->regspacing)) >> io->regshift)
1249 && 0xff;
1250}
1251
1252static void mem_outq(struct si_sm_io *io, unsigned int offset,
1253 unsigned char b)
1254{
1255 writeq(b << io->regshift, (io->addr)+(offset * io->regspacing));
1256}
1257#endif
1258
1259static void mem_cleanup(struct smi_info *info)
1260{
1261 unsigned long *addr = info->io.info;
1262 int mapsize;
1263
1264 if (info->io.addr) {
1265 iounmap(info->io.addr);
1266
1267 mapsize = ((info->io_size * info->io.regspacing)
1268 - (info->io.regspacing - info->io.regsize));
1269
1270 release_mem_region(*addr, mapsize);
1271 }
1272 kfree(info);
1273}
1274
1275static int mem_setup(struct smi_info *info)
1276{
1277 unsigned long *addr = info->io.info;
1278 int mapsize;
1279
1280 if (!addr || (!*addr))
1281 return -ENODEV;
1282
1283 info->io_cleanup = mem_cleanup;
1284
1285 /* Figure out the actual readb/readw/readl/etc routine to use based
1286 upon the register size. */
1287 switch (info->io.regsize) {
1288 case 1:
1289 info->io.inputb = mem_inb;
1290 info->io.outputb = mem_outb;
1291 break;
1292 case 2:
1293 info->io.inputb = mem_inw;
1294 info->io.outputb = mem_outw;
1295 break;
1296 case 4:
1297 info->io.inputb = mem_inl;
1298 info->io.outputb = mem_outl;
1299 break;
1300#ifdef readq
1301 case 8:
1302 info->io.inputb = mem_inq;
1303 info->io.outputb = mem_outq;
1304 break;
1305#endif
1306 default:
1307 printk("ipmi_si: Invalid register size: %d\n",
1308 info->io.regsize);
1309 return -EINVAL;
1310 }
1311
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
1316 * register. */
1317 mapsize = ((info->io_size * info->io.regspacing)
1318 - (info->io.regspacing - info->io.regsize));
1319
1320 if (request_mem_region(*addr, mapsize, DEVICE_NAME) == NULL)
1321 return -EIO;
1322
1323 info->io.addr = ioremap(*addr, mapsize);
1324 if (info->io.addr == NULL) {
1325 release_mem_region(*addr, mapsize);
1326 return -EIO;
1327 }
1328 return 0;
1329}
1330
1331static int try_init_mem(int intf_num, struct smi_info **new_info)
1332{
1333 struct smi_info *info;
1334
1335 if (!addrs[intf_num])
1336 return -ENODEV;
1337
1338 if (!is_new_interface(intf_num, IPMI_MEM_ADDR_SPACE,
1339 addrs[intf_num]))
1340 return -ENODEV;
1341
1342 info = kmalloc(sizeof(*info), GFP_KERNEL);
1343 if (!info) {
1344 printk(KERN_ERR "ipmi_si: Could not allocate SI data (2)\n");
1345 return -ENOMEM;
1346 }
1347 memset(info, 0, sizeof(*info));
1348
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];
1359 info->irq = 0;
1360 info->irq_setup = NULL;
1361 *new_info = info;
1362
1363 if (si_type[intf_num] == NULL)
1364 si_type[intf_num] = "kcs";
1365
1366 printk("ipmi_si: Trying \"%s\" at memory address 0x%lx\n",
1367 si_type[intf_num], addrs[intf_num]);
1368 return 0;
1369}
1370
1371
1372#ifdef CONFIG_ACPI_INTERPRETER
1373
1374#include <linux/acpi.h>
1375
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
1378 are no more. */
1379static int acpi_failure = 0;
1380
1381/* For GPE-type interrupts. */
1382static u32 ipmi_acpi_gpe(void *context)
1383{
1384 struct smi_info *smi_info = context;
1385 unsigned long flags;
1386#ifdef DEBUG_TIMING
1387 struct timeval t;
1388#endif
1389
1390 spin_lock_irqsave(&(smi_info->si_lock), flags);
1391
1392 spin_lock(&smi_info->count_lock);
1393 smi_info->interrupts++;
1394 spin_unlock(&smi_info->count_lock);
1395
1396 if (smi_info->stop_operation)
1397 goto out;
1398
1399#ifdef DEBUG_TIMING
1400 do_gettimeofday(&t);
1401 printk("**ACPI_GPE: %d.%9.9d\n", t.tv_sec, t.tv_usec);
1402#endif
1403 smi_event_handler(smi_info, 0);
1404 out:
1405 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
1406
1407 return ACPI_INTERRUPT_HANDLED;
1408}
1409
1410static int acpi_gpe_irq_setup(struct smi_info *info)
1411{
1412 acpi_status status;
1413
1414 if (!info->irq)
1415 return 0;
1416
1417 /* FIXME - is level triggered right? */
1418 status = acpi_install_gpe_handler(NULL,
1419 info->irq,
1420 ACPI_GPE_LEVEL_TRIGGERED,
1421 &ipmi_acpi_gpe,
1422 info);
1423 if (status != AE_OK) {
1424 printk(KERN_WARNING
1425 "ipmi_si: %s unable to claim ACPI GPE %d,"
1426 " running polled\n",
1427 DEVICE_NAME, info->irq);
1428 info->irq = 0;
1429 return -EINVAL;
1430 } else {
1431 printk(" Using ACPI GPE %d\n", info->irq);
1432 return 0;
1433 }
1434}
1435
1436static void acpi_gpe_irq_cleanup(struct smi_info *info)
1437{
1438 if (!info->irq)
1439 return;
1440
1441 acpi_remove_gpe_handler(NULL, info->irq, &ipmi_acpi_gpe);
1442}
1443
1444/*
1445 * Defined at
1446 * http://h21007.www2.hp.com/dspp/files/unprotected/devresource/Docs/TechPapers/IA64/hpspmi.pdf
1447 */
1448struct SPMITable {
1449 s8 Signature[4];
1450 u32 Length;
1451 u8 Revision;
1452 u8 Checksum;
1453 s8 OEMID[6];
1454 s8 OEMTableID[8];
1455 s8 OEMRevision[4];
1456 s8 CreatorID[4];
1457 s8 CreatorRevision[4];
1458 u8 InterfaceType;
1459 u8 IPMIlegacy;
1460 s16 SpecificationRevision;
1461
1462 /*
1463 * Bit 0 - SCI interrupt supported
1464 * Bit 1 - I/O APIC/SAPIC
1465 */
1466 u8 InterruptType;
1467
1468 /* If bit 0 of InterruptType is set, then this is the SCI
1469 interrupt in the GPEx_STS register. */
1470 u8 GPE;
1471
1472 s16 Reserved;
1473
1474 /* If bit 1 of InterruptType is set, then this is the I/O
1475 APIC/SAPIC interrupt. */
1476 u32 GlobalSystemInterrupt;
1477
1478 /* The actual register address. */
1479 struct acpi_generic_address addr;
1480
1481 u8 UID[4];
1482
1483 s8 spmi_id[1]; /* A '\0' terminated array starts here. */
1484};
1485
1486static int try_init_acpi(int intf_num, struct smi_info **new_info)
1487{
1488 struct smi_info *info;
1489 acpi_status status;
1490 struct SPMITable *spmi;
1491 char *io_type;
1492 u8 addr_space;
1493
1494 if (acpi_failure)
1495 return -ENODEV;
1496
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) {
1501 acpi_failure = 1;
1502 return -ENODEV;
1503 }
1504
1505 if (spmi->IPMIlegacy != 1) {
1506 printk(KERN_INFO "IPMI: Bad SPMI legacy %d\n", spmi->IPMIlegacy);
1507 return -ENODEV;
1508 }
1509
1510 if (spmi->addr.address_space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
1511 addr_space = IPMI_MEM_ADDR_SPACE;
1512 else
1513 addr_space = IPMI_IO_ADDR_SPACE;
1514 if (!is_new_interface(-1, addr_space, spmi->addr.address))
1515 return -ENODEV;
1516
1517 if (!spmi->addr.register_bit_width) {
1518 acpi_failure = 1;
1519 return -ENODEV;
1520 }
1521
1522 /* Figure out the interface type. */
1523 switch (spmi->InterfaceType)
1524 {
1525 case 1: /* KCS */
1526 si_type[intf_num] = "kcs";
1527 break;
1528
1529 case 2: /* SMIC */
1530 si_type[intf_num] = "smic";
1531 break;
1532
1533 case 3: /* BT */
1534 si_type[intf_num] = "bt";
1535 break;
1536
1537 default:
1538 printk(KERN_INFO "ipmi_si: Unknown ACPI/SPMI SI type %d\n",
1539 spmi->InterfaceType);
1540 return -EIO;
1541 }
1542
1543 info = kmalloc(sizeof(*info), GFP_KERNEL);
1544 if (!info) {
1545 printk(KERN_ERR "ipmi_si: Could not allocate SI data (3)\n");
1546 return -ENOMEM;
1547 }
1548 memset(info, 0, sizeof(*info));
1549
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;
1560 } else {
1561 /* Use the default interrupt setting. */
1562 info->irq = 0;
1563 info->irq_setup = NULL;
1564 }
1565
Corey Minyard35bc37a2005-05-01 08:59:10 -07001566 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;
1570 } else {
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;
1576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 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];
1581
1582 if (spmi->addr.address_space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
1583 io_type = "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) {
1588 io_type = "I/O";
1589 info->io_setup = port_setup;
1590 ports[intf_num] = spmi->addr.address;
1591 info->io.info = &(ports[intf_num]);
1592 } else {
1593 kfree(info);
1594 printk("ipmi_si: Unknown ACPI I/O Address type\n");
1595 return -EIO;
1596 }
1597
1598 *new_info = info;
1599
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);
1602 return 0;
1603}
1604#endif
1605
1606#ifdef CONFIG_X86
1607typedef struct dmi_ipmi_data
1608{
1609 u8 type;
1610 u8 addr_space;
1611 unsigned long base_addr;
1612 u8 irq;
1613 u8 offset;
1614 u8 slave_addr;
1615} dmi_ipmi_data_t;
1616
1617static dmi_ipmi_data_t dmi_data[SI_MAX_DRIVERS];
1618static int dmi_data_entries;
1619
Andrey Paninb224cd32005-09-06 15:18:37 -07001620static int __init decode_dmi(struct dmi_header *dm, int intf_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621{
Andrey Paninb224cd32005-09-06 15:18:37 -07001622 u8 *data = (u8 *)dm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 unsigned long base_addr;
1624 u8 reg_spacing;
Andrey Paninb224cd32005-09-06 15:18:37 -07001625 u8 len = dm->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 dmi_ipmi_data_t *ipmi_data = dmi_data+intf_num;
1627
Andrey Paninb224cd32005-09-06 15:18:37 -07001628 ipmi_data->type = data[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
1630 memcpy(&base_addr, data+8, sizeof(unsigned long));
1631 if (len >= 0x11) {
1632 if (base_addr & 1) {
1633 /* I/O */
1634 base_addr &= 0xFFFE;
1635 ipmi_data->addr_space = IPMI_IO_ADDR_SPACE;
1636 }
1637 else {
1638 /* Memory */
1639 ipmi_data->addr_space = IPMI_MEM_ADDR_SPACE;
1640 }
1641 /* If bit 4 of byte 0x10 is set, then the lsb for the address
1642 is odd. */
Andrey Paninb224cd32005-09-06 15:18:37 -07001643 ipmi_data->base_addr = base_addr | ((data[0x10] & 0x10) >> 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
Andrey Paninb224cd32005-09-06 15:18:37 -07001645 ipmi_data->irq = data[0x11];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
1647 /* The top two bits of byte 0x10 hold the register spacing. */
Andrey Paninb224cd32005-09-06 15:18:37 -07001648 reg_spacing = (data[0x10] & 0xC0) >> 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 switch(reg_spacing){
1650 case 0x00: /* Byte boundaries */
1651 ipmi_data->offset = 1;
1652 break;
1653 case 0x01: /* 32-bit boundaries */
1654 ipmi_data->offset = 4;
1655 break;
1656 case 0x02: /* 16-byte boundaries */
1657 ipmi_data->offset = 16;
1658 break;
1659 default:
1660 /* Some other interface, just ignore it. */
1661 return -EIO;
1662 }
1663 } else {
1664 /* Old DMI spec. */
Corey Minyard92068802005-05-01 08:59:10 -07001665 /* 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 ipmi_data->addr_space = IPMI_IO_ADDR_SPACE;
1673 ipmi_data->offset = 1;
1674 }
1675
Andrey Paninb224cd32005-09-06 15:18:37 -07001676 ipmi_data->slave_addr = data[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
1678 if (is_new_interface(-1, ipmi_data->addr_space,ipmi_data->base_addr)) {
1679 dmi_data_entries++;
1680 return 0;
1681 }
1682
1683 memset(ipmi_data, 0, sizeof(dmi_ipmi_data_t));
1684
1685 return -1;
1686}
1687
Andrey Paninb224cd32005-09-06 15:18:37 -07001688static void __init dmi_find_bmc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689{
Andrey Paninb224cd32005-09-06 15:18:37 -07001690 struct dmi_device *dev = NULL;
1691 int intf_num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Andrey Paninb224cd32005-09-06 15:18:37 -07001693 while ((dev = dmi_find_device(DMI_DEV_TYPE_IPMI, NULL, dev))) {
1694 if (intf_num >= SI_MAX_DRIVERS)
1695 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Andrey Paninb224cd32005-09-06 15:18:37 -07001697 decode_dmi((struct dmi_header *) dev->device_data, intf_num++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699}
1700
1701static int try_init_smbios(int intf_num, struct smi_info **new_info)
1702{
1703 struct smi_info *info;
1704 dmi_ipmi_data_t *ipmi_data = dmi_data+intf_num;
1705 char *io_type;
1706
1707 if (intf_num >= dmi_data_entries)
1708 return -ENODEV;
1709
1710 switch(ipmi_data->type) {
1711 case 0x01: /* KCS */
1712 si_type[intf_num] = "kcs";
1713 break;
1714 case 0x02: /* SMIC */
1715 si_type[intf_num] = "smic";
1716 break;
1717 case 0x03: /* BT */
1718 si_type[intf_num] = "bt";
1719 break;
1720 default:
1721 return -EIO;
1722 }
1723
1724 info = kmalloc(sizeof(*info), GFP_KERNEL);
1725 if (!info) {
1726 printk(KERN_ERR "ipmi_si: Could not allocate SI data (4)\n");
1727 return -ENOMEM;
1728 }
1729 memset(info, 0, sizeof(*info));
1730
1731 if (ipmi_data->addr_space == 1) {
1732 io_type = "memory";
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) {
1737 io_type = "I/O";
1738 info->io_setup = port_setup;
1739 ports[intf_num] = ipmi_data->base_addr;
1740 info->io.info = &(ports[intf_num]);
1741 } else {
1742 kfree(info);
1743 printk("ipmi_si: Unknown SMBIOS I/O Address type.\n");
1744 return -EIO;
1745 }
1746
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];
1753
1754 info->slave_addr = ipmi_data->slave_addr;
1755
1756 irqs[intf_num] = ipmi_data->irq;
1757
1758 *new_info = info;
1759
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);
1764 return 0;
1765}
1766#endif /* CONFIG_X86 */
1767
1768#ifdef CONFIG_PCI
1769
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
1774
1775/* Avoid more than one attempt to probe pci smic. */
1776static int pci_smic_checked = 0;
1777
1778static int find_pci_smic(int intf_num, struct smi_info **new_info)
1779{
1780 struct smi_info *info;
1781 int error;
1782 struct pci_dev *pci_dev = NULL;
1783 u16 base_addr;
1784 int fe_rmc = 0;
1785
1786 if (pci_smic_checked)
1787 return -ENODEV;
1788
1789 pci_smic_checked = 1;
1790
1791 if ((pci_dev = pci_get_device(PCI_HP_VENDOR_ID, PCI_MMC_DEVICE_ID,
1792 NULL)))
1793 ;
1794 else if ((pci_dev = pci_get_class(PCI_ERMC_CLASSCODE, NULL)) &&
1795 pci_dev->subsystem_vendor == PCI_HP_VENDOR_ID)
1796 fe_rmc = 1;
1797 else
1798 return -ENODEV;
1799
1800 error = pci_read_config_word(pci_dev, PCI_MMC_ADDR_CW, &base_addr);
1801 if (error)
1802 {
1803 pci_dev_put(pci_dev);
1804 printk(KERN_ERR
1805 "ipmi_si: pci_read_config_word() failed (%d).\n",
1806 error);
1807 return -ENODEV;
1808 }
1809
1810 /* Bit 0: 1 specifies programmed I/O, 0 specifies memory mapped I/O */
1811 if (!(base_addr & 0x0001))
1812 {
1813 pci_dev_put(pci_dev);
1814 printk(KERN_ERR
1815 "ipmi_si: memory mapped I/O not supported for PCI"
1816 " smic.\n");
1817 return -ENODEV;
1818 }
1819
1820 base_addr &= 0xFFFE;
1821 if (!fe_rmc)
1822 /* Data register starts at base address + 1 in eRMC */
1823 ++base_addr;
1824
1825 if (!is_new_interface(-1, IPMI_IO_ADDR_SPACE, base_addr)) {
1826 pci_dev_put(pci_dev);
1827 return -ENODEV;
1828 }
1829
1830 info = kmalloc(sizeof(*info), GFP_KERNEL);
1831 if (!info) {
1832 pci_dev_put(pci_dev);
1833 printk(KERN_ERR "ipmi_si: Could not allocate SI data (5)\n");
1834 return -ENOMEM;
1835 }
1836 memset(info, 0, sizeof(*info));
1837
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];
1846
1847 *new_info = info;
1848
1849 irqs[intf_num] = pci_dev->irq;
1850 si_type[intf_num] = "smic";
1851
1852 printk("ipmi_si: Found PCI SMIC at I/O address 0x%lx\n",
1853 (long unsigned int) base_addr);
1854
1855 pci_dev_put(pci_dev);
1856 return 0;
1857}
1858#endif /* CONFIG_PCI */
1859
1860static int try_init_plug_and_play(int intf_num, struct smi_info **new_info)
1861{
1862#ifdef CONFIG_PCI
1863 if (find_pci_smic(intf_num, new_info)==0)
1864 return 0;
1865#endif
1866 /* Include other methods here. */
1867
1868 return -ENODEV;
1869}
1870
1871
1872static int try_get_dev_id(struct smi_info *smi_info)
1873{
1874 unsigned char msg[2];
1875 unsigned char *resp;
1876 unsigned long resp_len;
1877 enum si_sm_result smi_result;
1878 int rv = 0;
1879
1880 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1881 if (!resp)
1882 return -ENOMEM;
1883
1884 /* Do a Get Device ID command, since it comes back with some
1885 useful info. */
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);
1889
1890 smi_result = smi_info->handlers->event(smi_info->si_sm, 0);
1891 for (;;)
1892 {
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);
1898 }
1899 else if (smi_result == SI_SM_CALL_WITHOUT_DELAY)
1900 {
1901 smi_result = smi_info->handlers->event(
1902 smi_info->si_sm, 0);
1903 }
1904 else
1905 break;
1906 }
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. */
1910 rv = -ENODEV;
1911 goto out;
1912 }
1913
1914 /* Otherwise, we got some data. */
1915 resp_len = smi_info->handlers->get_result(smi_info->si_sm,
1916 resp, IPMI_MAX_MSG_LENGTH);
1917 if (resp_len < 6) {
1918 /* That's odd, it should be longer. */
1919 rv = -EINVAL;
1920 goto out;
1921 }
1922
1923 if ((resp[1] != IPMI_GET_DEVICE_ID_CMD) || (resp[2] != 0)) {
1924 /* That's odd, it shouldn't be able to fail. */
1925 rv = -EINVAL;
1926 goto out;
1927 }
1928
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;
1935
1936 out:
1937 kfree(resp);
1938 return rv;
1939}
1940
1941static int type_file_read_proc(char *page, char **start, off_t off,
1942 int count, int *eof, void *data)
1943{
1944 char *out = (char *) page;
1945 struct smi_info *smi = data;
1946
1947 switch (smi->si_type) {
1948 case SI_KCS:
1949 return sprintf(out, "kcs\n");
1950 case SI_SMIC:
1951 return sprintf(out, "smic\n");
1952 case SI_BT:
1953 return sprintf(out, "bt\n");
1954 default:
1955 return 0;
1956 }
1957}
1958
1959static int stat_file_read_proc(char *page, char **start, off_t off,
1960 int count, int *eof, void *data)
1961{
1962 char *out = (char *) page;
1963 struct smi_info *smi = data;
1964
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",
1974 smi->idles);
1975 out += sprintf(out, "interrupts: %ld\n",
1976 smi->interrupts);
1977 out += sprintf(out, "attentions: %ld\n",
1978 smi->attentions);
1979 out += sprintf(out, "flag_fetches: %ld\n",
1980 smi->flag_fetches);
1981 out += sprintf(out, "hosed_count: %ld\n",
1982 smi->hosed_count);
1983 out += sprintf(out, "complete_transactions: %ld\n",
1984 smi->complete_transactions);
1985 out += sprintf(out, "events: %ld\n",
1986 smi->events);
1987 out += sprintf(out, "watchdog_pretimeouts: %ld\n",
1988 smi->watchdog_pretimeouts);
1989 out += sprintf(out, "incoming_messages: %ld\n",
1990 smi->incoming_messages);
1991
1992 return (out - ((char *) page));
1993}
1994
1995/* Returns 0 if initialized, or negative on an error. */
1996static int init_one_smi(int intf_num, struct smi_info **smi)
1997{
1998 int rv;
1999 struct smi_info *new_smi;
2000
2001
2002 rv = try_init_mem(intf_num, &new_smi);
2003 if (rv)
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);
2008 }
2009#endif
2010#ifdef CONFIG_X86
2011 if ((rv) && (si_trydefaults)) {
2012 rv = try_init_smbios(intf_num, &new_smi);
2013 }
2014#endif
2015 if ((rv) && (si_trydefaults)) {
2016 rv = try_init_plug_and_play(intf_num, &new_smi);
2017 }
2018
2019
2020 if (rv)
2021 return rv;
2022
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;
2027
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;
2032 }
2033
2034 /* Default to KCS if no type is specified. */
2035 if (si_type[intf_num] == NULL) {
2036 if (si_trydefaults)
2037 si_type[intf_num] = "kcs";
2038 else {
2039 rv = -EINVAL;
2040 goto out_err;
2041 }
2042 }
2043
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;
2054 } else {
2055 /* No support for anything else yet. */
2056 rv = -EIO;
2057 goto out_err;
2058 }
2059
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");
2064 rv = -ENOMEM;
2065 goto out_err;
2066 }
2067 new_smi->io_size = new_smi->handlers->init_data(new_smi->si_sm,
2068 &new_smi->io);
2069
2070 /* Now that we know the I/O size, we can set up the I/O. */
2071 rv = new_smi->io_setup(new_smi);
2072 if (rv) {
2073 printk(" Could not set up I/O space\n");
2074 goto out_err;
2075 }
2076
2077 spin_lock_init(&(new_smi->si_lock));
2078 spin_lock_init(&(new_smi->msg_lock));
2079 spin_lock_init(&(new_smi->count_lock));
2080
2081 /* Do low-level detection first. */
2082 if (new_smi->handlers->detect(new_smi->si_sm)) {
2083 rv = -ENODEV;
2084 goto out_err;
2085 }
2086
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);
2090 if (rv)
2091 goto out_err;
2092
2093 /* Try to claim any interrupts. */
2094 new_smi->irq_setup(new_smi);
2095
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;
2101
2102 new_smi->interrupt_disabled = 0;
2103 new_smi->timer_stopped = 0;
2104 new_smi->stop_operation = 0;
2105
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. */
2110 if (new_smi->irq)
2111 new_smi->si_state = SI_CLEARING_FLAGS_THEN_SET_IRQ;
2112
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));
2123
2124 rv = ipmi_register_smi(&handlers,
2125 new_smi,
2126 new_smi->ipmi_version_major,
2127 new_smi->ipmi_version_minor,
2128 new_smi->slave_addr,
2129 &(new_smi->intf));
2130 if (rv) {
2131 printk(KERN_ERR
2132 "ipmi_si: Unable to register device: error %d\n",
2133 rv);
2134 goto out_err_stop_timer;
2135 }
2136
2137 rv = ipmi_smi_add_proc_entry(new_smi->intf, "type",
2138 type_file_read_proc, NULL,
2139 new_smi, THIS_MODULE);
2140 if (rv) {
2141 printk(KERN_ERR
2142 "ipmi_si: Unable to create proc entry: %d\n",
2143 rv);
2144 goto out_err_stop_timer;
2145 }
2146
2147 rv = ipmi_smi_add_proc_entry(new_smi->intf, "si_stats",
2148 stat_file_read_proc, NULL,
2149 new_smi, THIS_MODULE);
2150 if (rv) {
2151 printk(KERN_ERR
2152 "ipmi_si: Unable to create proc entry: %d\n",
2153 rv);
2154 goto out_err_stop_timer;
2155 }
2156
2157 *smi = new_smi;
2158
2159 printk(" IPMI %s interface initialized\n", si_type[intf_num]);
2160
2161 return 0;
2162
2163 out_err_stop_timer:
2164 new_smi->stop_operation = 1;
2165
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);
2171 }
2172
2173 out_err:
2174 if (new_smi->intf)
2175 ipmi_unregister_smi(new_smi->intf);
2176
2177 new_smi->irq_cleanup(new_smi);
2178
2179 /* Wait until we know that we are out of any interrupt
2180 handlers might have been running before we freed the
2181 interrupt. */
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002182 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
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);
2188 }
2189 new_smi->io_cleanup(new_smi);
2190
2191 return rv;
2192}
2193
2194static __init int init_ipmi_si(void)
2195{
2196 int rv = 0;
2197 int pos = 0;
2198 int i;
2199 char *str;
2200
2201 if (initialized)
2202 return 0;
2203 initialized = 1;
2204
2205 /* Parse out the si_type string into its components. */
2206 str = si_type_str;
2207 if (*str != '\0') {
2208 for (i=0; (i<SI_MAX_PARMS) && (*str != '\0'); i++) {
2209 si_type[i] = str;
2210 str = strchr(str, ',');
2211 if (str) {
2212 *str = '\0';
2213 str++;
2214 } else {
2215 break;
2216 }
2217 }
2218 }
2219
2220 printk(KERN_INFO "IPMI System Interface driver version "
2221 IPMI_SI_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);
2228 printk("\n");
2229
2230#ifdef CONFIG_X86
Andrey Paninb224cd32005-09-06 15:18:37 -07002231 dmi_find_bmc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232#endif
2233
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. */
2238 si_type[0] = "kcs";
2239 ports[0] = DEFAULT_KCS_IO_PORT;
2240 rv = init_one_smi(0, &(smi_infos[pos]));
2241 if (rv) {
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]));
2246 }
2247 if (rv) {
2248 /* No SMIC - try BT */
2249 si_type[0] = "bt";
2250 ports[0] = DEFAULT_BT_IO_PORT;
2251 rv = init_one_smi(0, &(smi_infos[pos]));
2252 }
2253 }
2254 if (rv == 0)
2255 pos++;
2256
2257 for (i=1; i < SI_MAX_PARMS; i++) {
2258 rv = init_one_smi(i, &(smi_infos[pos]));
2259 if (rv == 0)
2260 pos++;
2261 }
2262
2263 if (smi_infos[0] == NULL) {
2264 printk("ipmi_si: Unable to find any System Interface(s)\n");
2265 return -ENODEV;
2266 }
2267
2268 return 0;
2269}
2270module_init(init_ipmi_si);
2271
2272static void __exit cleanup_one_si(struct smi_info *to_clean)
2273{
2274 int rv;
2275 unsigned long flags;
2276
2277 if (! to_clean)
2278 return;
2279
2280 /* Tell the timer and interrupt handlers that we are shutting
2281 down. */
2282 spin_lock_irqsave(&(to_clean->si_lock), flags);
2283 spin_lock(&(to_clean->msg_lock));
2284
2285 to_clean->stop_operation = 1;
2286
2287 to_clean->irq_cleanup(to_clean);
2288
2289 spin_unlock(&(to_clean->msg_lock));
2290 spin_unlock_irqrestore(&(to_clean->si_lock), flags);
2291
2292 /* Wait until we know that we are out of any interrupt
2293 handlers might have been running before we freed the
2294 interrupt. */
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002295 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
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);
2302 }
2303
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)) {
2307 poll(to_clean);
2308 set_current_state(TASK_UNINTERRUPTIBLE);
2309 schedule_timeout(1);
2310 }
2311
2312 rv = ipmi_unregister_smi(to_clean->intf);
2313 if (rv) {
2314 printk(KERN_ERR
2315 "ipmi_si: Unable to unregister device: errno=%d\n",
2316 rv);
2317 }
2318
2319 to_clean->handlers->cleanup(to_clean->si_sm);
2320
2321 kfree(to_clean->si_sm);
2322
2323 to_clean->io_cleanup(to_clean);
2324}
2325
2326static __exit void cleanup_ipmi_si(void)
2327{
2328 int i;
2329
2330 if (!initialized)
2331 return;
2332
2333 for (i=0; i<SI_MAX_DRIVERS; i++) {
2334 cleanup_one_si(smi_infos[i]);
2335 }
2336}
2337module_exit(cleanup_ipmi_si);
2338
2339MODULE_LICENSE("GPL");