[ACPI] revert bad processor_core.c patch for bug 5128
[pandora-kernel.git] / drivers / char / ipmi / ipmi_si_intf.c
1 /*
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
62 static inline void add_usec_to_timer(struct timer_list *t, long v)
63 {
64         t->sub_expires += nsec_to_arch_cycle(v * 1000);
65         while (t->sub_expires >= arch_cycles_per_jiffy)
66         {
67                 t->expires++;
68                 t->sub_expires -= arch_cycles_per_jiffy;
69         }
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>
78
79 #define IPMI_SI_VERSION "v33"
80
81 /* Measure times between events in the driver. */
82 #undef DEBUG_TIMING
83
84 /* Call every 10 ms. */
85 #define SI_TIMEOUT_TIME_USEC    10000
86 #define SI_USEC_PER_JIFFY       (1000000/HZ)
87 #define SI_TIMEOUT_JIFFIES      (SI_TIMEOUT_TIME_USEC/SI_USEC_PER_JIFFY)
88 #define SI_SHORT_TIMEOUT_USEC  250 /* .25ms when the SM request a
89                                        short timeout */
90
91 enum si_intf_state {
92         SI_NORMAL,
93         SI_GETTING_FLAGS,
94         SI_GETTING_EVENTS,
95         SI_CLEARING_FLAGS,
96         SI_CLEARING_FLAGS_THEN_SET_IRQ,
97         SI_GETTING_MESSAGES,
98         SI_ENABLE_INTERRUPTS1,
99         SI_ENABLE_INTERRUPTS2
100         /* FIXME - add watchdog stuff. */
101 };
102
103 /* Some BT-specific defines we need here. */
104 #define IPMI_BT_INTMASK_REG             2
105 #define IPMI_BT_INTMASK_CLEAR_IRQ_BIT   2
106 #define IPMI_BT_INTMASK_ENABLE_IRQ_BIT  1
107
108 enum si_type {
109     SI_KCS, SI_SMIC, SI_BT
110 };
111
112 struct smi_info
113 {
114         ipmi_smi_t             intf;
115         struct si_sm_data      *si_sm;
116         struct si_sm_handlers  *handlers;
117         enum si_type           si_type;
118         spinlock_t             si_lock;
119         spinlock_t             msg_lock;
120         struct list_head       xmit_msgs;
121         struct list_head       hp_xmit_msgs;
122         struct ipmi_smi_msg    *curr_msg;
123         enum si_intf_state     si_state;
124
125         /* Used to handle the various types of I/O that can occur with
126            IPMI */
127         struct si_sm_io io;
128         int (*io_setup)(struct smi_info *info);
129         void (*io_cleanup)(struct smi_info *info);
130         int (*irq_setup)(struct smi_info *info);
131         void (*irq_cleanup)(struct smi_info *info);
132         unsigned int io_size;
133
134         /* Flags from the last GET_MSG_FLAGS command, used when an ATTN
135            is set to hold the flags until we are done handling everything
136            from the flags. */
137 #define RECEIVE_MSG_AVAIL       0x01
138 #define EVENT_MSG_BUFFER_FULL   0x02
139 #define WDT_PRE_TIMEOUT_INT     0x08
140         unsigned char       msg_flags;
141
142         /* If set to true, this will request events the next time the
143            state machine is idle. */
144         atomic_t            req_events;
145
146         /* If true, run the state machine to completion on every send
147            call.  Generally used after a panic to make sure stuff goes
148            out. */
149         int                 run_to_completion;
150
151         /* The I/O port of an SI interface. */
152         int                 port;
153
154         /* The space between start addresses of the two ports.  For
155            instance, if the first port is 0xca2 and the spacing is 4, then
156            the second port is 0xca6. */
157         unsigned int        spacing;
158
159         /* zero if no irq; */
160         int                 irq;
161
162         /* The timer for this si. */
163         struct timer_list   si_timer;
164
165         /* The time (in jiffies) the last timeout occurred at. */
166         unsigned long       last_timeout_jiffies;
167
168         /* Used to gracefully stop the timer without race conditions. */
169         volatile int        stop_operation;
170         volatile int        timer_stopped;
171
172         /* The driver will disable interrupts when it gets into a
173            situation where it cannot handle messages due to lack of
174            memory.  Once that situation clears up, it will re-enable
175            interrupts. */
176         int interrupt_disabled;
177
178         unsigned char ipmi_si_dev_rev;
179         unsigned char ipmi_si_fw_rev_major;
180         unsigned char ipmi_si_fw_rev_minor;
181         unsigned char ipmi_version_major;
182         unsigned char ipmi_version_minor;
183
184         /* Slave address, could be reported from DMI. */
185         unsigned char slave_addr;
186
187         /* Counters and things for the proc filesystem. */
188         spinlock_t count_lock;
189         unsigned long short_timeouts;
190         unsigned long long_timeouts;
191         unsigned long timeout_restarts;
192         unsigned long idles;
193         unsigned long interrupts;
194         unsigned long attentions;
195         unsigned long flag_fetches;
196         unsigned long hosed_count;
197         unsigned long complete_transactions;
198         unsigned long events;
199         unsigned long watchdog_pretimeouts;
200         unsigned long incoming_messages;
201 };
202
203 static void si_restart_short_timer(struct smi_info *smi_info);
204
205 static void deliver_recv_msg(struct smi_info *smi_info,
206                              struct ipmi_smi_msg *msg)
207 {
208         /* Deliver the message to the upper layer with the lock
209            released. */
210         spin_unlock(&(smi_info->si_lock));
211         ipmi_smi_msg_received(smi_info->intf, msg);
212         spin_lock(&(smi_info->si_lock));
213 }
214
215 static void return_hosed_msg(struct smi_info *smi_info)
216 {
217         struct ipmi_smi_msg *msg = smi_info->curr_msg;
218
219         /* Make it a reponse */
220         msg->rsp[0] = msg->data[0] | 4;
221         msg->rsp[1] = msg->data[1];
222         msg->rsp[2] = 0xFF; /* Unknown error. */
223         msg->rsp_size = 3;
224
225         smi_info->curr_msg = NULL;
226         deliver_recv_msg(smi_info, msg);
227 }
228
229 static enum si_sm_result start_next_msg(struct smi_info *smi_info)
230 {
231         int              rv;
232         struct list_head *entry = NULL;
233 #ifdef DEBUG_TIMING
234         struct timeval t;
235 #endif
236
237         /* No need to save flags, we aleady have interrupts off and we
238            already hold the SMI lock. */
239         spin_lock(&(smi_info->msg_lock));
240
241         /* Pick the high priority queue first. */
242         if (! list_empty(&(smi_info->hp_xmit_msgs))) {
243                 entry = smi_info->hp_xmit_msgs.next;
244         } else if (! list_empty(&(smi_info->xmit_msgs))) {
245                 entry = smi_info->xmit_msgs.next;
246         }
247
248         if (!entry) {
249                 smi_info->curr_msg = NULL;
250                 rv = SI_SM_IDLE;
251         } else {
252                 int err;
253
254                 list_del(entry);
255                 smi_info->curr_msg = list_entry(entry,
256                                                 struct ipmi_smi_msg,
257                                                 link);
258 #ifdef DEBUG_TIMING
259                 do_gettimeofday(&t);
260                 printk("**Start2: %d.%9.9d\n", t.tv_sec, t.tv_usec);
261 #endif
262                 err = smi_info->handlers->start_transaction(
263                         smi_info->si_sm,
264                         smi_info->curr_msg->data,
265                         smi_info->curr_msg->data_size);
266                 if (err) {
267                         return_hosed_msg(smi_info);
268                 }
269
270                 rv = SI_SM_CALL_WITHOUT_DELAY;
271         }
272         spin_unlock(&(smi_info->msg_lock));
273
274         return rv;
275 }
276
277 static void start_enable_irq(struct smi_info *smi_info)
278 {
279         unsigned char msg[2];
280
281         /* If we are enabling interrupts, we have to tell the
282            BMC to use them. */
283         msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
284         msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
285
286         smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
287         smi_info->si_state = SI_ENABLE_INTERRUPTS1;
288 }
289
290 static void start_clear_flags(struct smi_info *smi_info)
291 {
292         unsigned char msg[3];
293
294         /* Make sure the watchdog pre-timeout flag is not set at startup. */
295         msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
296         msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
297         msg[2] = WDT_PRE_TIMEOUT_INT;
298
299         smi_info->handlers->start_transaction(smi_info->si_sm, msg, 3);
300         smi_info->si_state = SI_CLEARING_FLAGS;
301 }
302
303 /* When we have a situtaion where we run out of memory and cannot
304    allocate messages, we just leave them in the BMC and run the system
305    polled until we can allocate some memory.  Once we have some
306    memory, we will re-enable the interrupt. */
307 static inline void disable_si_irq(struct smi_info *smi_info)
308 {
309         if ((smi_info->irq) && (!smi_info->interrupt_disabled)) {
310                 disable_irq_nosync(smi_info->irq);
311                 smi_info->interrupt_disabled = 1;
312         }
313 }
314
315 static inline void enable_si_irq(struct smi_info *smi_info)
316 {
317         if ((smi_info->irq) && (smi_info->interrupt_disabled)) {
318                 enable_irq(smi_info->irq);
319                 smi_info->interrupt_disabled = 0;
320         }
321 }
322
323 static void handle_flags(struct smi_info *smi_info)
324 {
325         if (smi_info->msg_flags & WDT_PRE_TIMEOUT_INT) {
326                 /* Watchdog pre-timeout */
327                 spin_lock(&smi_info->count_lock);
328                 smi_info->watchdog_pretimeouts++;
329                 spin_unlock(&smi_info->count_lock);
330
331                 start_clear_flags(smi_info);
332                 smi_info->msg_flags &= ~WDT_PRE_TIMEOUT_INT;
333                 spin_unlock(&(smi_info->si_lock));
334                 ipmi_smi_watchdog_pretimeout(smi_info->intf);
335                 spin_lock(&(smi_info->si_lock));
336         } else if (smi_info->msg_flags & RECEIVE_MSG_AVAIL) {
337                 /* Messages available. */
338                 smi_info->curr_msg = ipmi_alloc_smi_msg();
339                 if (!smi_info->curr_msg) {
340                         disable_si_irq(smi_info);
341                         smi_info->si_state = SI_NORMAL;
342                         return;
343                 }
344                 enable_si_irq(smi_info);
345
346                 smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
347                 smi_info->curr_msg->data[1] = IPMI_GET_MSG_CMD;
348                 smi_info->curr_msg->data_size = 2;
349
350                 smi_info->handlers->start_transaction(
351                         smi_info->si_sm,
352                         smi_info->curr_msg->data,
353                         smi_info->curr_msg->data_size);
354                 smi_info->si_state = SI_GETTING_MESSAGES;
355         } else if (smi_info->msg_flags & EVENT_MSG_BUFFER_FULL) {
356                 /* Events available. */
357                 smi_info->curr_msg = ipmi_alloc_smi_msg();
358                 if (!smi_info->curr_msg) {
359                         disable_si_irq(smi_info);
360                         smi_info->si_state = SI_NORMAL;
361                         return;
362                 }
363                 enable_si_irq(smi_info);
364
365                 smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
366                 smi_info->curr_msg->data[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD;
367                 smi_info->curr_msg->data_size = 2;
368
369                 smi_info->handlers->start_transaction(
370                         smi_info->si_sm,
371                         smi_info->curr_msg->data,
372                         smi_info->curr_msg->data_size);
373                 smi_info->si_state = SI_GETTING_EVENTS;
374         } else {
375                 smi_info->si_state = SI_NORMAL;
376         }
377 }
378
379 static void handle_transaction_done(struct smi_info *smi_info)
380 {
381         struct ipmi_smi_msg *msg;
382 #ifdef DEBUG_TIMING
383         struct timeval t;
384
385         do_gettimeofday(&t);
386         printk("**Done: %d.%9.9d\n", t.tv_sec, t.tv_usec);
387 #endif
388         switch (smi_info->si_state) {
389         case SI_NORMAL:
390                 if (!smi_info->curr_msg)
391                         break;
392
393                 smi_info->curr_msg->rsp_size
394                         = smi_info->handlers->get_result(
395                                 smi_info->si_sm,
396                                 smi_info->curr_msg->rsp,
397                                 IPMI_MAX_MSG_LENGTH);
398
399                 /* Do this here becase deliver_recv_msg() releases the
400                    lock, and a new message can be put in during the
401                    time the lock is released. */
402                 msg = smi_info->curr_msg;
403                 smi_info->curr_msg = NULL;
404                 deliver_recv_msg(smi_info, msg);
405                 break;
406
407         case SI_GETTING_FLAGS:
408         {
409                 unsigned char msg[4];
410                 unsigned int  len;
411
412                 /* We got the flags from the SMI, now handle them. */
413                 len = smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
414                 if (msg[2] != 0) {
415                         /* Error fetching flags, just give up for
416                            now. */
417                         smi_info->si_state = SI_NORMAL;
418                 } else if (len < 4) {
419                         /* Hmm, no flags.  That's technically illegal, but
420                            don't use uninitialized data. */
421                         smi_info->si_state = SI_NORMAL;
422                 } else {
423                         smi_info->msg_flags = msg[3];
424                         handle_flags(smi_info);
425                 }
426                 break;
427         }
428
429         case SI_CLEARING_FLAGS:
430         case SI_CLEARING_FLAGS_THEN_SET_IRQ:
431         {
432                 unsigned char msg[3];
433
434                 /* We cleared the flags. */
435                 smi_info->handlers->get_result(smi_info->si_sm, msg, 3);
436                 if (msg[2] != 0) {
437                         /* Error clearing flags */
438                         printk(KERN_WARNING
439                                "ipmi_si: Error clearing flags: %2.2x\n",
440                                msg[2]);
441                 }
442                 if (smi_info->si_state == SI_CLEARING_FLAGS_THEN_SET_IRQ)
443                         start_enable_irq(smi_info);
444                 else
445                         smi_info->si_state = SI_NORMAL;
446                 break;
447         }
448
449         case SI_GETTING_EVENTS:
450         {
451                 smi_info->curr_msg->rsp_size
452                         = smi_info->handlers->get_result(
453                                 smi_info->si_sm,
454                                 smi_info->curr_msg->rsp,
455                                 IPMI_MAX_MSG_LENGTH);
456
457                 /* Do this here becase deliver_recv_msg() releases the
458                    lock, and a new message can be put in during the
459                    time the lock is released. */
460                 msg = smi_info->curr_msg;
461                 smi_info->curr_msg = NULL;
462                 if (msg->rsp[2] != 0) {
463                         /* Error getting event, probably done. */
464                         msg->done(msg);
465
466                         /* Take off the event flag. */
467                         smi_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
468                         handle_flags(smi_info);
469                 } else {
470                         spin_lock(&smi_info->count_lock);
471                         smi_info->events++;
472                         spin_unlock(&smi_info->count_lock);
473
474                         /* Do this before we deliver the message
475                            because delivering the message releases the
476                            lock and something else can mess with the
477                            state. */
478                         handle_flags(smi_info);
479
480                         deliver_recv_msg(smi_info, msg);
481                 }
482                 break;
483         }
484
485         case SI_GETTING_MESSAGES:
486         {
487                 smi_info->curr_msg->rsp_size
488                         = smi_info->handlers->get_result(
489                                 smi_info->si_sm,
490                                 smi_info->curr_msg->rsp,
491                                 IPMI_MAX_MSG_LENGTH);
492
493                 /* Do this here becase deliver_recv_msg() releases the
494                    lock, and a new message can be put in during the
495                    time the lock is released. */
496                 msg = smi_info->curr_msg;
497                 smi_info->curr_msg = NULL;
498                 if (msg->rsp[2] != 0) {
499                         /* Error getting event, probably done. */
500                         msg->done(msg);
501
502                         /* Take off the msg flag. */
503                         smi_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
504                         handle_flags(smi_info);
505                 } else {
506                         spin_lock(&smi_info->count_lock);
507                         smi_info->incoming_messages++;
508                         spin_unlock(&smi_info->count_lock);
509
510                         /* Do this before we deliver the message
511                            because delivering the message releases the
512                            lock and something else can mess with the
513                            state. */
514                         handle_flags(smi_info);
515
516                         deliver_recv_msg(smi_info, msg);
517                 }
518                 break;
519         }
520
521         case SI_ENABLE_INTERRUPTS1:
522         {
523                 unsigned char msg[4];
524
525                 /* We got the flags from the SMI, now handle them. */
526                 smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
527                 if (msg[2] != 0) {
528                         printk(KERN_WARNING
529                                "ipmi_si: Could not enable interrupts"
530                                ", failed get, using polled mode.\n");
531                         smi_info->si_state = SI_NORMAL;
532                 } else {
533                         msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
534                         msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
535                         msg[2] = msg[3] | 1; /* enable msg queue int */
536                         smi_info->handlers->start_transaction(
537                                 smi_info->si_sm, msg, 3);
538                         smi_info->si_state = SI_ENABLE_INTERRUPTS2;
539                 }
540                 break;
541         }
542
543         case SI_ENABLE_INTERRUPTS2:
544         {
545                 unsigned char msg[4];
546
547                 /* We got the flags from the SMI, now handle them. */
548                 smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
549                 if (msg[2] != 0) {
550                         printk(KERN_WARNING
551                                "ipmi_si: Could not enable interrupts"
552                                ", failed set, using polled mode.\n");
553                 }
554                 smi_info->si_state = SI_NORMAL;
555                 break;
556         }
557         }
558 }
559
560 /* Called on timeouts and events.  Timeouts should pass the elapsed
561    time, interrupts should pass in zero. */
562 static enum si_sm_result smi_event_handler(struct smi_info *smi_info,
563                                            int time)
564 {
565         enum si_sm_result si_sm_result;
566
567  restart:
568         /* There used to be a loop here that waited a little while
569            (around 25us) before giving up.  That turned out to be
570            pointless, the minimum delays I was seeing were in the 300us
571            range, which is far too long to wait in an interrupt.  So
572            we just run until the state machine tells us something
573            happened or it needs a delay. */
574         si_sm_result = smi_info->handlers->event(smi_info->si_sm, time);
575         time = 0;
576         while (si_sm_result == SI_SM_CALL_WITHOUT_DELAY)
577         {
578                 si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
579         }
580
581         if (si_sm_result == SI_SM_TRANSACTION_COMPLETE)
582         {
583                 spin_lock(&smi_info->count_lock);
584                 smi_info->complete_transactions++;
585                 spin_unlock(&smi_info->count_lock);
586
587                 handle_transaction_done(smi_info);
588                 si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
589         }
590         else if (si_sm_result == SI_SM_HOSED)
591         {
592                 spin_lock(&smi_info->count_lock);
593                 smi_info->hosed_count++;
594                 spin_unlock(&smi_info->count_lock);
595
596                 /* Do the before return_hosed_msg, because that
597                    releases the lock. */
598                 smi_info->si_state = SI_NORMAL;
599                 if (smi_info->curr_msg != NULL) {
600                         /* If we were handling a user message, format
601                            a response to send to the upper layer to
602                            tell it about the error. */
603                         return_hosed_msg(smi_info);
604                 }
605                 si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
606         }
607
608         /* We prefer handling attn over new messages. */
609         if (si_sm_result == SI_SM_ATTN)
610         {
611                 unsigned char msg[2];
612
613                 spin_lock(&smi_info->count_lock);
614                 smi_info->attentions++;
615                 spin_unlock(&smi_info->count_lock);
616
617                 /* Got a attn, send down a get message flags to see
618                    what's causing it.  It would be better to handle
619                    this in the upper layer, but due to the way
620                    interrupts work with the SMI, that's not really
621                    possible. */
622                 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
623                 msg[1] = IPMI_GET_MSG_FLAGS_CMD;
624
625                 smi_info->handlers->start_transaction(
626                         smi_info->si_sm, msg, 2);
627                 smi_info->si_state = SI_GETTING_FLAGS;
628                 goto restart;
629         }
630
631         /* If we are currently idle, try to start the next message. */
632         if (si_sm_result == SI_SM_IDLE) {
633                 spin_lock(&smi_info->count_lock);
634                 smi_info->idles++;
635                 spin_unlock(&smi_info->count_lock);
636
637                 si_sm_result = start_next_msg(smi_info);
638                 if (si_sm_result != SI_SM_IDLE)
639                         goto restart;
640         }
641
642         if ((si_sm_result == SI_SM_IDLE)
643             && (atomic_read(&smi_info->req_events)))
644         {
645                 /* We are idle and the upper layer requested that I fetch
646                    events, so do so. */
647                 unsigned char msg[2];
648
649                 spin_lock(&smi_info->count_lock);
650                 smi_info->flag_fetches++;
651                 spin_unlock(&smi_info->count_lock);
652
653                 atomic_set(&smi_info->req_events, 0);
654                 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
655                 msg[1] = IPMI_GET_MSG_FLAGS_CMD;
656
657                 smi_info->handlers->start_transaction(
658                         smi_info->si_sm, msg, 2);
659                 smi_info->si_state = SI_GETTING_FLAGS;
660                 goto restart;
661         }
662
663         return si_sm_result;
664 }
665
666 static void sender(void                *send_info,
667                    struct ipmi_smi_msg *msg,
668                    int                 priority)
669 {
670         struct smi_info   *smi_info = send_info;
671         enum si_sm_result result;
672         unsigned long     flags;
673 #ifdef DEBUG_TIMING
674         struct timeval    t;
675 #endif
676
677         spin_lock_irqsave(&(smi_info->msg_lock), flags);
678 #ifdef DEBUG_TIMING
679         do_gettimeofday(&t);
680         printk("**Enqueue: %d.%9.9d\n", t.tv_sec, t.tv_usec);
681 #endif
682
683         if (smi_info->run_to_completion) {
684                 /* If we are running to completion, then throw it in
685                    the list and run transactions until everything is
686                    clear.  Priority doesn't matter here. */
687                 list_add_tail(&(msg->link), &(smi_info->xmit_msgs));
688
689                 /* We have to release the msg lock and claim the smi
690                    lock in this case, because of race conditions. */
691                 spin_unlock_irqrestore(&(smi_info->msg_lock), flags);
692
693                 spin_lock_irqsave(&(smi_info->si_lock), flags);
694                 result = smi_event_handler(smi_info, 0);
695                 while (result != SI_SM_IDLE) {
696                         udelay(SI_SHORT_TIMEOUT_USEC);
697                         result = smi_event_handler(smi_info,
698                                                    SI_SHORT_TIMEOUT_USEC);
699                 }
700                 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
701                 return;
702         } else {
703                 if (priority > 0) {
704                         list_add_tail(&(msg->link), &(smi_info->hp_xmit_msgs));
705                 } else {
706                         list_add_tail(&(msg->link), &(smi_info->xmit_msgs));
707                 }
708         }
709         spin_unlock_irqrestore(&(smi_info->msg_lock), flags);
710
711         spin_lock_irqsave(&(smi_info->si_lock), flags);
712         if ((smi_info->si_state == SI_NORMAL)
713             && (smi_info->curr_msg == NULL))
714         {
715                 start_next_msg(smi_info);
716                 si_restart_short_timer(smi_info);
717         }
718         spin_unlock_irqrestore(&(smi_info->si_lock), flags);
719 }
720
721 static void set_run_to_completion(void *send_info, int i_run_to_completion)
722 {
723         struct smi_info   *smi_info = send_info;
724         enum si_sm_result result;
725         unsigned long     flags;
726
727         spin_lock_irqsave(&(smi_info->si_lock), flags);
728
729         smi_info->run_to_completion = i_run_to_completion;
730         if (i_run_to_completion) {
731                 result = smi_event_handler(smi_info, 0);
732                 while (result != SI_SM_IDLE) {
733                         udelay(SI_SHORT_TIMEOUT_USEC);
734                         result = smi_event_handler(smi_info,
735                                                    SI_SHORT_TIMEOUT_USEC);
736                 }
737         }
738
739         spin_unlock_irqrestore(&(smi_info->si_lock), flags);
740 }
741
742 static void poll(void *send_info)
743 {
744         struct smi_info *smi_info = send_info;
745
746         smi_event_handler(smi_info, 0);
747 }
748
749 static void request_events(void *send_info)
750 {
751         struct smi_info *smi_info = send_info;
752
753         atomic_set(&smi_info->req_events, 1);
754 }
755
756 static int initialized = 0;
757
758 /* Must be called with interrupts off and with the si_lock held. */
759 static void si_restart_short_timer(struct smi_info *smi_info)
760 {
761 #if defined(CONFIG_HIGH_RES_TIMERS)
762         unsigned long flags;
763         unsigned long jiffies_now;
764
765         if (del_timer(&(smi_info->si_timer))) {
766                 /* If we don't delete the timer, then it will go off
767                    immediately, anyway.  So we only process if we
768                    actually delete the timer. */
769
770                 /* We already have irqsave on, so no need for it
771                    here. */
772                 read_lock(&xtime_lock);
773                 jiffies_now = jiffies;
774                 smi_info->si_timer.expires = jiffies_now;
775                 smi_info->si_timer.sub_expires = get_arch_cycles(jiffies_now);
776
777                 add_usec_to_timer(&smi_info->si_timer, SI_SHORT_TIMEOUT_USEC);
778
779                 add_timer(&(smi_info->si_timer));
780                 spin_lock_irqsave(&smi_info->count_lock, flags);
781                 smi_info->timeout_restarts++;
782                 spin_unlock_irqrestore(&smi_info->count_lock, flags);
783         }
784 #endif
785 }
786
787 static void smi_timeout(unsigned long data)
788 {
789         struct smi_info   *smi_info = (struct smi_info *) data;
790         enum si_sm_result smi_result;
791         unsigned long     flags;
792         unsigned long     jiffies_now;
793         unsigned long     time_diff;
794 #ifdef DEBUG_TIMING
795         struct timeval    t;
796 #endif
797
798         if (smi_info->stop_operation) {
799                 smi_info->timer_stopped = 1;
800                 return;
801         }
802
803         spin_lock_irqsave(&(smi_info->si_lock), flags);
804 #ifdef DEBUG_TIMING
805         do_gettimeofday(&t);
806         printk("**Timer: %d.%9.9d\n", t.tv_sec, t.tv_usec);
807 #endif
808         jiffies_now = jiffies;
809         time_diff = ((jiffies_now - smi_info->last_timeout_jiffies)
810                      * SI_USEC_PER_JIFFY);
811         smi_result = smi_event_handler(smi_info, time_diff);
812
813         spin_unlock_irqrestore(&(smi_info->si_lock), flags);
814
815         smi_info->last_timeout_jiffies = jiffies_now;
816
817         if ((smi_info->irq) && (! smi_info->interrupt_disabled)) {
818                 /* Running with interrupts, only do long timeouts. */
819                 smi_info->si_timer.expires = jiffies + SI_TIMEOUT_JIFFIES;
820                 spin_lock_irqsave(&smi_info->count_lock, flags);
821                 smi_info->long_timeouts++;
822                 spin_unlock_irqrestore(&smi_info->count_lock, flags);
823                 goto do_add_timer;
824         }
825
826         /* If the state machine asks for a short delay, then shorten
827            the timer timeout. */
828         if (smi_result == SI_SM_CALL_WITH_DELAY) {
829                 spin_lock_irqsave(&smi_info->count_lock, flags);
830                 smi_info->short_timeouts++;
831                 spin_unlock_irqrestore(&smi_info->count_lock, flags);
832 #if defined(CONFIG_HIGH_RES_TIMERS)
833                 read_lock(&xtime_lock);
834                 smi_info->si_timer.expires = jiffies;
835                 smi_info->si_timer.sub_expires
836                         = get_arch_cycles(smi_info->si_timer.expires);
837                 read_unlock(&xtime_lock);
838                 add_usec_to_timer(&smi_info->si_timer, SI_SHORT_TIMEOUT_USEC);
839 #else
840                 smi_info->si_timer.expires = jiffies + 1;
841 #endif
842         } else {
843                 spin_lock_irqsave(&smi_info->count_lock, flags);
844                 smi_info->long_timeouts++;
845                 spin_unlock_irqrestore(&smi_info->count_lock, flags);
846                 smi_info->si_timer.expires = jiffies + SI_TIMEOUT_JIFFIES;
847 #if defined(CONFIG_HIGH_RES_TIMERS)
848                 smi_info->si_timer.sub_expires = 0;
849 #endif
850         }
851
852  do_add_timer:
853         add_timer(&(smi_info->si_timer));
854 }
855
856 static irqreturn_t si_irq_handler(int irq, void *data, struct pt_regs *regs)
857 {
858         struct smi_info *smi_info = data;
859         unsigned long   flags;
860 #ifdef DEBUG_TIMING
861         struct timeval  t;
862 #endif
863
864         spin_lock_irqsave(&(smi_info->si_lock), flags);
865
866         spin_lock(&smi_info->count_lock);
867         smi_info->interrupts++;
868         spin_unlock(&smi_info->count_lock);
869
870         if (smi_info->stop_operation)
871                 goto out;
872
873 #ifdef DEBUG_TIMING
874         do_gettimeofday(&t);
875         printk("**Interrupt: %d.%9.9d\n", t.tv_sec, t.tv_usec);
876 #endif
877         smi_event_handler(smi_info, 0);
878  out:
879         spin_unlock_irqrestore(&(smi_info->si_lock), flags);
880         return IRQ_HANDLED;
881 }
882
883 static irqreturn_t si_bt_irq_handler(int irq, void *data, struct pt_regs *regs)
884 {
885         struct smi_info *smi_info = data;
886         /* We need to clear the IRQ flag for the BT interface. */
887         smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG,
888                              IPMI_BT_INTMASK_CLEAR_IRQ_BIT
889                              | IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
890         return si_irq_handler(irq, data, regs);
891 }
892
893
894 static struct ipmi_smi_handlers handlers =
895 {
896         .owner                  = THIS_MODULE,
897         .sender                 = sender,
898         .request_events         = request_events,
899         .set_run_to_completion  = set_run_to_completion,
900         .poll                   = poll,
901 };
902
903 /* There can be 4 IO ports passed in (with or without IRQs), 4 addresses,
904    a default IO port, and 1 ACPI/SPMI address.  That sets SI_MAX_DRIVERS */
905
906 #define SI_MAX_PARMS 4
907 #define SI_MAX_DRIVERS ((SI_MAX_PARMS * 2) + 2)
908 static struct smi_info *smi_infos[SI_MAX_DRIVERS] =
909 { NULL, NULL, NULL, NULL };
910
911 #define DEVICE_NAME "ipmi_si"
912
913 #define DEFAULT_KCS_IO_PORT     0xca2
914 #define DEFAULT_SMIC_IO_PORT    0xca9
915 #define DEFAULT_BT_IO_PORT      0xe4
916 #define DEFAULT_REGSPACING      1
917
918 static int           si_trydefaults = 1;
919 static char          *si_type[SI_MAX_PARMS];
920 #define MAX_SI_TYPE_STR 30
921 static char          si_type_str[MAX_SI_TYPE_STR];
922 static unsigned long addrs[SI_MAX_PARMS];
923 static int num_addrs;
924 static unsigned int  ports[SI_MAX_PARMS];
925 static int num_ports;
926 static int           irqs[SI_MAX_PARMS];
927 static int num_irqs;
928 static int           regspacings[SI_MAX_PARMS];
929 static int num_regspacings = 0;
930 static int           regsizes[SI_MAX_PARMS];
931 static int num_regsizes = 0;
932 static int           regshifts[SI_MAX_PARMS];
933 static int num_regshifts = 0;
934 static int slave_addrs[SI_MAX_PARMS];
935 static int num_slave_addrs = 0;
936
937
938 module_param_named(trydefaults, si_trydefaults, bool, 0);
939 MODULE_PARM_DESC(trydefaults, "Setting this to 'false' will disable the"
940                  " default scan of the KCS and SMIC interface at the standard"
941                  " address");
942 module_param_string(type, si_type_str, MAX_SI_TYPE_STR, 0);
943 MODULE_PARM_DESC(type, "Defines the type of each interface, each"
944                  " interface separated by commas.  The types are 'kcs',"
945                  " 'smic', and 'bt'.  For example si_type=kcs,bt will set"
946                  " the first interface to kcs and the second to bt");
947 module_param_array(addrs, long, &num_addrs, 0);
948 MODULE_PARM_DESC(addrs, "Sets the memory address of each interface, the"
949                  " addresses separated by commas.  Only use if an interface"
950                  " is in memory.  Otherwise, set it to zero or leave"
951                  " it blank.");
952 module_param_array(ports, int, &num_ports, 0);
953 MODULE_PARM_DESC(ports, "Sets the port address of each interface, the"
954                  " addresses separated by commas.  Only use if an interface"
955                  " is a port.  Otherwise, set it to zero or leave"
956                  " it blank.");
957 module_param_array(irqs, int, &num_irqs, 0);
958 MODULE_PARM_DESC(irqs, "Sets the interrupt of each interface, the"
959                  " addresses separated by commas.  Only use if an interface"
960                  " has an interrupt.  Otherwise, set it to zero or leave"
961                  " it blank.");
962 module_param_array(regspacings, int, &num_regspacings, 0);
963 MODULE_PARM_DESC(regspacings, "The number of bytes between the start address"
964                  " and each successive register used by the interface.  For"
965                  " instance, if the start address is 0xca2 and the spacing"
966                  " is 2, then the second address is at 0xca4.  Defaults"
967                  " to 1.");
968 module_param_array(regsizes, int, &num_regsizes, 0);
969 MODULE_PARM_DESC(regsizes, "The size of the specific IPMI register in bytes."
970                  " This should generally be 1, 2, 4, or 8 for an 8-bit,"
971                  " 16-bit, 32-bit, or 64-bit register.  Use this if you"
972                  " the 8-bit IPMI register has to be read from a larger"
973                  " register.");
974 module_param_array(regshifts, int, &num_regshifts, 0);
975 MODULE_PARM_DESC(regshifts, "The amount to shift the data read from the."
976                  " IPMI register, in bits.  For instance, if the data"
977                  " is read from a 32-bit word and the IPMI data is in"
978                  " bit 8-15, then the shift would be 8");
979 module_param_array(slave_addrs, int, &num_slave_addrs, 0);
980 MODULE_PARM_DESC(slave_addrs, "Set the default IPMB slave address for"
981                  " the controller.  Normally this is 0x20, but can be"
982                  " overridden by this parm.  This is an array indexed"
983                  " by interface number.");
984
985
986 #define IPMI_MEM_ADDR_SPACE 1
987 #define IPMI_IO_ADDR_SPACE  2
988
989 #if defined(CONFIG_ACPI) || defined(CONFIG_X86) || defined(CONFIG_PCI)
990 static int is_new_interface(int intf, u8 addr_space, unsigned long base_addr)
991 {
992         int i;
993
994         for (i = 0; i < SI_MAX_PARMS; ++i) {
995                 /* Don't check our address. */
996                 if (i == intf)
997                         continue;
998                 if (si_type[i] != NULL) {
999                         if ((addr_space == IPMI_MEM_ADDR_SPACE &&
1000                              base_addr == addrs[i]) ||
1001                             (addr_space == IPMI_IO_ADDR_SPACE &&
1002                              base_addr == ports[i]))
1003                                 return 0;
1004                 }
1005                 else
1006                         break;
1007         }
1008
1009         return 1;
1010 }
1011 #endif
1012
1013 static int std_irq_setup(struct smi_info *info)
1014 {
1015         int rv;
1016
1017         if (!info->irq)
1018                 return 0;
1019
1020         if (info->si_type == SI_BT) {
1021                 rv = request_irq(info->irq,
1022                                  si_bt_irq_handler,
1023                                  SA_INTERRUPT,
1024                                  DEVICE_NAME,
1025                                  info);
1026                 if (!rv)
1027                         /* Enable the interrupt in the BT interface. */
1028                         info->io.outputb(&info->io, IPMI_BT_INTMASK_REG,
1029                                          IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
1030         } else
1031                 rv = request_irq(info->irq,
1032                                  si_irq_handler,
1033                                  SA_INTERRUPT,
1034                                  DEVICE_NAME,
1035                                  info);
1036         if (rv) {
1037                 printk(KERN_WARNING
1038                        "ipmi_si: %s unable to claim interrupt %d,"
1039                        " running polled\n",
1040                        DEVICE_NAME, info->irq);
1041                 info->irq = 0;
1042         } else {
1043                 printk("  Using irq %d\n", info->irq);
1044         }
1045
1046         return rv;
1047 }
1048
1049 static void std_irq_cleanup(struct smi_info *info)
1050 {
1051         if (!info->irq)
1052                 return;
1053
1054         if (info->si_type == SI_BT)
1055                 /* Disable the interrupt in the BT interface. */
1056                 info->io.outputb(&info->io, IPMI_BT_INTMASK_REG, 0);
1057         free_irq(info->irq, info);
1058 }
1059
1060 static unsigned char port_inb(struct si_sm_io *io, unsigned int offset)
1061 {
1062         unsigned int *addr = io->info;
1063
1064         return inb((*addr)+(offset*io->regspacing));
1065 }
1066
1067 static void port_outb(struct si_sm_io *io, unsigned int offset,
1068                       unsigned char b)
1069 {
1070         unsigned int *addr = io->info;
1071
1072         outb(b, (*addr)+(offset * io->regspacing));
1073 }
1074
1075 static unsigned char port_inw(struct si_sm_io *io, unsigned int offset)
1076 {
1077         unsigned int *addr = io->info;
1078
1079         return (inw((*addr)+(offset * io->regspacing)) >> io->regshift) & 0xff;
1080 }
1081
1082 static void port_outw(struct si_sm_io *io, unsigned int offset,
1083                       unsigned char b)
1084 {
1085         unsigned int *addr = io->info;
1086
1087         outw(b << io->regshift, (*addr)+(offset * io->regspacing));
1088 }
1089
1090 static unsigned char port_inl(struct si_sm_io *io, unsigned int offset)
1091 {
1092         unsigned int *addr = io->info;
1093
1094         return (inl((*addr)+(offset * io->regspacing)) >> io->regshift) & 0xff;
1095 }
1096
1097 static void port_outl(struct si_sm_io *io, unsigned int offset,
1098                       unsigned char b)
1099 {
1100         unsigned int *addr = io->info;
1101
1102         outl(b << io->regshift, (*addr)+(offset * io->regspacing));
1103 }
1104
1105 static void port_cleanup(struct smi_info *info)
1106 {
1107         unsigned int *addr = info->io.info;
1108         int           mapsize;
1109
1110         if (addr && (*addr)) {
1111                 mapsize = ((info->io_size * info->io.regspacing)
1112                            - (info->io.regspacing - info->io.regsize));
1113
1114                 release_region (*addr, mapsize);
1115         }
1116         kfree(info);
1117 }
1118
1119 static int port_setup(struct smi_info *info)
1120 {
1121         unsigned int *addr = info->io.info;
1122         int           mapsize;
1123
1124         if (!addr || (!*addr))
1125                 return -ENODEV;
1126
1127         info->io_cleanup = port_cleanup;
1128
1129         /* Figure out the actual inb/inw/inl/etc routine to use based
1130            upon the register size. */
1131         switch (info->io.regsize) {
1132         case 1:
1133                 info->io.inputb = port_inb;
1134                 info->io.outputb = port_outb;
1135                 break;
1136         case 2:
1137                 info->io.inputb = port_inw;
1138                 info->io.outputb = port_outw;
1139                 break;
1140         case 4:
1141                 info->io.inputb = port_inl;
1142                 info->io.outputb = port_outl;
1143                 break;
1144         default:
1145                 printk("ipmi_si: Invalid register size: %d\n",
1146                        info->io.regsize);
1147                 return -EINVAL;
1148         }
1149
1150         /* Calculate the total amount of memory to claim.  This is an
1151          * unusual looking calculation, but it avoids claiming any
1152          * more memory than it has to.  It will claim everything
1153          * between the first address to the end of the last full
1154          * register. */
1155         mapsize = ((info->io_size * info->io.regspacing)
1156                    - (info->io.regspacing - info->io.regsize));
1157
1158         if (request_region(*addr, mapsize, DEVICE_NAME) == NULL)
1159                 return -EIO;
1160         return 0;
1161 }
1162
1163 static int try_init_port(int intf_num, struct smi_info **new_info)
1164 {
1165         struct smi_info *info;
1166
1167         if (!ports[intf_num])
1168                 return -ENODEV;
1169
1170         if (!is_new_interface(intf_num, IPMI_IO_ADDR_SPACE,
1171                               ports[intf_num]))
1172                 return -ENODEV;
1173
1174         info = kmalloc(sizeof(*info), GFP_KERNEL);
1175         if (!info) {
1176                 printk(KERN_ERR "ipmi_si: Could not allocate SI data (1)\n");
1177                 return -ENOMEM;
1178         }
1179         memset(info, 0, sizeof(*info));
1180
1181         info->io_setup = port_setup;
1182         info->io.info = &(ports[intf_num]);
1183         info->io.addr = NULL;
1184         info->io.regspacing = regspacings[intf_num];
1185         if (!info->io.regspacing)
1186                 info->io.regspacing = DEFAULT_REGSPACING;
1187         info->io.regsize = regsizes[intf_num];
1188         if (!info->io.regsize)
1189                 info->io.regsize = DEFAULT_REGSPACING;
1190         info->io.regshift = regshifts[intf_num];
1191         info->irq = 0;
1192         info->irq_setup = NULL;
1193         *new_info = info;
1194
1195         if (si_type[intf_num] == NULL)
1196                 si_type[intf_num] = "kcs";
1197
1198         printk("ipmi_si: Trying \"%s\" at I/O port 0x%x\n",
1199                si_type[intf_num], ports[intf_num]);
1200         return 0;
1201 }
1202
1203 static unsigned char mem_inb(struct si_sm_io *io, unsigned int offset)
1204 {
1205         return readb((io->addr)+(offset * io->regspacing));
1206 }
1207
1208 static void mem_outb(struct si_sm_io *io, unsigned int offset,
1209                      unsigned char b)
1210 {
1211         writeb(b, (io->addr)+(offset * io->regspacing));
1212 }
1213
1214 static unsigned char mem_inw(struct si_sm_io *io, unsigned int offset)
1215 {
1216         return (readw((io->addr)+(offset * io->regspacing)) >> io->regshift)
1217                 && 0xff;
1218 }
1219
1220 static void mem_outw(struct si_sm_io *io, unsigned int offset,
1221                      unsigned char b)
1222 {
1223         writeb(b << io->regshift, (io->addr)+(offset * io->regspacing));
1224 }
1225
1226 static unsigned char mem_inl(struct si_sm_io *io, unsigned int offset)
1227 {
1228         return (readl((io->addr)+(offset * io->regspacing)) >> io->regshift)
1229                 && 0xff;
1230 }
1231
1232 static void mem_outl(struct si_sm_io *io, unsigned int offset,
1233                      unsigned char b)
1234 {
1235         writel(b << io->regshift, (io->addr)+(offset * io->regspacing));
1236 }
1237
1238 #ifdef readq
1239 static unsigned char mem_inq(struct si_sm_io *io, unsigned int offset)
1240 {
1241         return (readq((io->addr)+(offset * io->regspacing)) >> io->regshift)
1242                 && 0xff;
1243 }
1244
1245 static void mem_outq(struct si_sm_io *io, unsigned int offset,
1246                      unsigned char b)
1247 {
1248         writeq(b << io->regshift, (io->addr)+(offset * io->regspacing));
1249 }
1250 #endif
1251
1252 static void mem_cleanup(struct smi_info *info)
1253 {
1254         unsigned long *addr = info->io.info;
1255         int           mapsize;
1256
1257         if (info->io.addr) {
1258                 iounmap(info->io.addr);
1259
1260                 mapsize = ((info->io_size * info->io.regspacing)
1261                            - (info->io.regspacing - info->io.regsize));
1262
1263                 release_mem_region(*addr, mapsize);
1264         }
1265         kfree(info);
1266 }
1267
1268 static int mem_setup(struct smi_info *info)
1269 {
1270         unsigned long *addr = info->io.info;
1271         int           mapsize;
1272
1273         if (!addr || (!*addr))
1274                 return -ENODEV;
1275
1276         info->io_cleanup = mem_cleanup;
1277
1278         /* Figure out the actual readb/readw/readl/etc routine to use based
1279            upon the register size. */
1280         switch (info->io.regsize) {
1281         case 1:
1282                 info->io.inputb = mem_inb;
1283                 info->io.outputb = mem_outb;
1284                 break;
1285         case 2:
1286                 info->io.inputb = mem_inw;
1287                 info->io.outputb = mem_outw;
1288                 break;
1289         case 4:
1290                 info->io.inputb = mem_inl;
1291                 info->io.outputb = mem_outl;
1292                 break;
1293 #ifdef readq
1294         case 8:
1295                 info->io.inputb = mem_inq;
1296                 info->io.outputb = mem_outq;
1297                 break;
1298 #endif
1299         default:
1300                 printk("ipmi_si: Invalid register size: %d\n",
1301                        info->io.regsize);
1302                 return -EINVAL;
1303         }
1304
1305         /* Calculate the total amount of memory to claim.  This is an
1306          * unusual looking calculation, but it avoids claiming any
1307          * more memory than it has to.  It will claim everything
1308          * between the first address to the end of the last full
1309          * register. */
1310         mapsize = ((info->io_size * info->io.regspacing)
1311                    - (info->io.regspacing - info->io.regsize));
1312
1313         if (request_mem_region(*addr, mapsize, DEVICE_NAME) == NULL)
1314                 return -EIO;
1315
1316         info->io.addr = ioremap(*addr, mapsize);
1317         if (info->io.addr == NULL) {
1318                 release_mem_region(*addr, mapsize);
1319                 return -EIO;
1320         }
1321         return 0;
1322 }
1323
1324 static int try_init_mem(int intf_num, struct smi_info **new_info)
1325 {
1326         struct smi_info *info;
1327
1328         if (!addrs[intf_num])
1329                 return -ENODEV;
1330
1331         if (!is_new_interface(intf_num, IPMI_MEM_ADDR_SPACE,
1332                               addrs[intf_num]))
1333                 return -ENODEV;
1334
1335         info = kmalloc(sizeof(*info), GFP_KERNEL);
1336         if (!info) {
1337                 printk(KERN_ERR "ipmi_si: Could not allocate SI data (2)\n");
1338                 return -ENOMEM;
1339         }
1340         memset(info, 0, sizeof(*info));
1341
1342         info->io_setup = mem_setup;
1343         info->io.info = &addrs[intf_num];
1344         info->io.addr = NULL;
1345         info->io.regspacing = regspacings[intf_num];
1346         if (!info->io.regspacing)
1347                 info->io.regspacing = DEFAULT_REGSPACING;
1348         info->io.regsize = regsizes[intf_num];
1349         if (!info->io.regsize)
1350                 info->io.regsize = DEFAULT_REGSPACING;
1351         info->io.regshift = regshifts[intf_num];
1352         info->irq = 0;
1353         info->irq_setup = NULL;
1354         *new_info = info;
1355
1356         if (si_type[intf_num] == NULL)
1357                 si_type[intf_num] = "kcs";
1358
1359         printk("ipmi_si: Trying \"%s\" at memory address 0x%lx\n",
1360                si_type[intf_num], addrs[intf_num]);
1361         return 0;
1362 }
1363
1364
1365 #ifdef CONFIG_ACPI
1366
1367 #include <linux/acpi.h>
1368
1369 /* Once we get an ACPI failure, we don't try any more, because we go
1370    through the tables sequentially.  Once we don't find a table, there
1371    are no more. */
1372 static int acpi_failure = 0;
1373
1374 /* For GPE-type interrupts. */
1375 static u32 ipmi_acpi_gpe(void *context)
1376 {
1377         struct smi_info *smi_info = context;
1378         unsigned long   flags;
1379 #ifdef DEBUG_TIMING
1380         struct timeval t;
1381 #endif
1382
1383         spin_lock_irqsave(&(smi_info->si_lock), flags);
1384
1385         spin_lock(&smi_info->count_lock);
1386         smi_info->interrupts++;
1387         spin_unlock(&smi_info->count_lock);
1388
1389         if (smi_info->stop_operation)
1390                 goto out;
1391
1392 #ifdef DEBUG_TIMING
1393         do_gettimeofday(&t);
1394         printk("**ACPI_GPE: %d.%9.9d\n", t.tv_sec, t.tv_usec);
1395 #endif
1396         smi_event_handler(smi_info, 0);
1397  out:
1398         spin_unlock_irqrestore(&(smi_info->si_lock), flags);
1399
1400         return ACPI_INTERRUPT_HANDLED;
1401 }
1402
1403 static int acpi_gpe_irq_setup(struct smi_info *info)
1404 {
1405         acpi_status status;
1406
1407         if (!info->irq)
1408                 return 0;
1409
1410         /* FIXME - is level triggered right? */
1411         status = acpi_install_gpe_handler(NULL,
1412                                           info->irq,
1413                                           ACPI_GPE_LEVEL_TRIGGERED,
1414                                           &ipmi_acpi_gpe,
1415                                           info);
1416         if (status != AE_OK) {
1417                 printk(KERN_WARNING
1418                        "ipmi_si: %s unable to claim ACPI GPE %d,"
1419                        " running polled\n",
1420                        DEVICE_NAME, info->irq);
1421                 info->irq = 0;
1422                 return -EINVAL;
1423         } else {
1424                 printk("  Using ACPI GPE %d\n", info->irq);
1425                 return 0;
1426         }
1427 }
1428
1429 static void acpi_gpe_irq_cleanup(struct smi_info *info)
1430 {
1431         if (!info->irq)
1432                 return;
1433
1434         acpi_remove_gpe_handler(NULL, info->irq, &ipmi_acpi_gpe);
1435 }
1436
1437 /*
1438  * Defined at
1439  * http://h21007.www2.hp.com/dspp/files/unprotected/devresource/Docs/TechPapers/IA64/hpspmi.pdf
1440  */
1441 struct SPMITable {
1442         s8      Signature[4];
1443         u32     Length;
1444         u8      Revision;
1445         u8      Checksum;
1446         s8      OEMID[6];
1447         s8      OEMTableID[8];
1448         s8      OEMRevision[4];
1449         s8      CreatorID[4];
1450         s8      CreatorRevision[4];
1451         u8      InterfaceType;
1452         u8      IPMIlegacy;
1453         s16     SpecificationRevision;
1454
1455         /*
1456          * Bit 0 - SCI interrupt supported
1457          * Bit 1 - I/O APIC/SAPIC
1458          */
1459         u8      InterruptType;
1460
1461         /* If bit 0 of InterruptType is set, then this is the SCI
1462            interrupt in the GPEx_STS register. */
1463         u8      GPE;
1464
1465         s16     Reserved;
1466
1467         /* If bit 1 of InterruptType is set, then this is the I/O
1468            APIC/SAPIC interrupt. */
1469         u32     GlobalSystemInterrupt;
1470
1471         /* The actual register address. */
1472         struct acpi_generic_address addr;
1473
1474         u8      UID[4];
1475
1476         s8      spmi_id[1]; /* A '\0' terminated array starts here. */
1477 };
1478
1479 static int try_init_acpi(int intf_num, struct smi_info **new_info)
1480 {
1481         struct smi_info  *info;
1482         acpi_status      status;
1483         struct SPMITable *spmi;
1484         char             *io_type;
1485         u8               addr_space;
1486
1487         if (acpi_disabled)
1488                 return -ENODEV;
1489
1490         if (acpi_failure)
1491                 return -ENODEV;
1492
1493         status = acpi_get_firmware_table("SPMI", intf_num+1,
1494                                          ACPI_LOGICAL_ADDRESSING,
1495                                          (struct acpi_table_header **) &spmi);
1496         if (status != AE_OK) {
1497                 acpi_failure = 1;
1498                 return -ENODEV;
1499         }
1500
1501         if (spmi->IPMIlegacy != 1) {
1502             printk(KERN_INFO "IPMI: Bad SPMI legacy %d\n", spmi->IPMIlegacy);
1503             return -ENODEV;
1504         }
1505
1506         if (spmi->addr.address_space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
1507                 addr_space = IPMI_MEM_ADDR_SPACE;
1508         else
1509                 addr_space = IPMI_IO_ADDR_SPACE;
1510         if (!is_new_interface(-1, addr_space, spmi->addr.address))
1511                 return -ENODEV;
1512
1513         if (!spmi->addr.register_bit_width) {
1514                 acpi_failure = 1;
1515                 return -ENODEV;
1516         }
1517
1518         /* Figure out the interface type. */
1519         switch (spmi->InterfaceType)
1520         {
1521         case 1: /* KCS */
1522                 si_type[intf_num] = "kcs";
1523                 break;
1524
1525         case 2: /* SMIC */
1526                 si_type[intf_num] = "smic";
1527                 break;
1528
1529         case 3: /* BT */
1530                 si_type[intf_num] = "bt";
1531                 break;
1532
1533         default:
1534                 printk(KERN_INFO "ipmi_si: Unknown ACPI/SPMI SI type %d\n",
1535                         spmi->InterfaceType);
1536                 return -EIO;
1537         }
1538
1539         info = kmalloc(sizeof(*info), GFP_KERNEL);
1540         if (!info) {
1541                 printk(KERN_ERR "ipmi_si: Could not allocate SI data (3)\n");
1542                 return -ENOMEM;
1543         }
1544         memset(info, 0, sizeof(*info));
1545
1546         if (spmi->InterruptType & 1) {
1547                 /* We've got a GPE interrupt. */
1548                 info->irq = spmi->GPE;
1549                 info->irq_setup = acpi_gpe_irq_setup;
1550                 info->irq_cleanup = acpi_gpe_irq_cleanup;
1551         } else if (spmi->InterruptType & 2) {
1552                 /* We've got an APIC/SAPIC interrupt. */
1553                 info->irq = spmi->GlobalSystemInterrupt;
1554                 info->irq_setup = std_irq_setup;
1555                 info->irq_cleanup = std_irq_cleanup;
1556         } else {
1557                 /* Use the default interrupt setting. */
1558                 info->irq = 0;
1559                 info->irq_setup = NULL;
1560         }
1561
1562         if (spmi->addr.register_bit_width) {
1563                 /* A (hopefully) properly formed register bit width. */
1564                 regspacings[intf_num] = spmi->addr.register_bit_width / 8;
1565                 info->io.regspacing = spmi->addr.register_bit_width / 8;
1566         } else {
1567                 /* Some broken systems get this wrong and set the value
1568                  * to zero.  Assume it is the default spacing.  If that
1569                  * is wrong, too bad, the vendor should fix the tables. */
1570                 regspacings[intf_num] = DEFAULT_REGSPACING;
1571                 info->io.regspacing = DEFAULT_REGSPACING;
1572         }
1573         regsizes[intf_num] = regspacings[intf_num];
1574         info->io.regsize = regsizes[intf_num];
1575         regshifts[intf_num] = spmi->addr.register_bit_offset;
1576         info->io.regshift = regshifts[intf_num];
1577
1578         if (spmi->addr.address_space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
1579                 io_type = "memory";
1580                 info->io_setup = mem_setup;
1581                 addrs[intf_num] = spmi->addr.address;
1582                 info->io.info = &(addrs[intf_num]);
1583         } else if (spmi->addr.address_space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
1584                 io_type = "I/O";
1585                 info->io_setup = port_setup;
1586                 ports[intf_num] = spmi->addr.address;
1587                 info->io.info = &(ports[intf_num]);
1588         } else {
1589                 kfree(info);
1590                 printk("ipmi_si: Unknown ACPI I/O Address type\n");
1591                 return -EIO;
1592         }
1593
1594         *new_info = info;
1595
1596         printk("ipmi_si: ACPI/SPMI specifies \"%s\" %s SI @ 0x%lx\n",
1597                si_type[intf_num], io_type, (unsigned long) spmi->addr.address);
1598         return 0;
1599 }
1600 #endif
1601
1602 #ifdef CONFIG_X86
1603 typedef struct dmi_ipmi_data
1604 {
1605         u8              type;
1606         u8              addr_space;
1607         unsigned long   base_addr;
1608         u8              irq;
1609         u8              offset;
1610         u8              slave_addr;
1611 } dmi_ipmi_data_t;
1612
1613 static dmi_ipmi_data_t dmi_data[SI_MAX_DRIVERS];
1614 static int dmi_data_entries;
1615
1616 typedef struct dmi_header
1617 {
1618         u8      type;
1619         u8      length;
1620         u16     handle;
1621 } dmi_header_t;
1622
1623 static int decode_dmi(dmi_header_t __iomem *dm, int intf_num)
1624 {
1625         u8              __iomem *data = (u8 __iomem *)dm;
1626         unsigned long   base_addr;
1627         u8              reg_spacing;
1628         u8              len = readb(&dm->length);
1629         dmi_ipmi_data_t *ipmi_data = dmi_data+intf_num;
1630
1631         ipmi_data->type = readb(&data[4]);
1632
1633         memcpy(&base_addr, data+8, sizeof(unsigned long));
1634         if (len >= 0x11) {
1635                 if (base_addr & 1) {
1636                         /* I/O */
1637                         base_addr &= 0xFFFE;
1638                         ipmi_data->addr_space = IPMI_IO_ADDR_SPACE;
1639                 }
1640                 else {
1641                         /* Memory */
1642                         ipmi_data->addr_space = IPMI_MEM_ADDR_SPACE;
1643                 }
1644                 /* If bit 4 of byte 0x10 is set, then the lsb for the address
1645                    is odd. */
1646                 ipmi_data->base_addr = base_addr | ((readb(&data[0x10]) & 0x10) >> 4);
1647
1648                 ipmi_data->irq = readb(&data[0x11]);
1649
1650                 /* The top two bits of byte 0x10 hold the register spacing. */
1651                 reg_spacing = (readb(&data[0x10]) & 0xC0) >> 6;
1652                 switch(reg_spacing){
1653                 case 0x00: /* Byte boundaries */
1654                     ipmi_data->offset = 1;
1655                     break;
1656                 case 0x01: /* 32-bit boundaries */
1657                     ipmi_data->offset = 4;
1658                     break;
1659                 case 0x02: /* 16-byte boundaries */
1660                     ipmi_data->offset = 16;
1661                     break;
1662                 default:
1663                     /* Some other interface, just ignore it. */
1664                     return -EIO;
1665                 }
1666         } else {
1667                 /* Old DMI spec. */
1668                 /* Note that technically, the lower bit of the base
1669                  * address should be 1 if the address is I/O and 0 if
1670                  * the address is in memory.  So many systems get that
1671                  * wrong (and all that I have seen are I/O) so we just
1672                  * ignore that bit and assume I/O.  Systems that use
1673                  * memory should use the newer spec, anyway. */
1674                 ipmi_data->base_addr = base_addr & 0xfffe;
1675                 ipmi_data->addr_space = IPMI_IO_ADDR_SPACE;
1676                 ipmi_data->offset = 1;
1677         }
1678
1679         ipmi_data->slave_addr = readb(&data[6]);
1680
1681         if (is_new_interface(-1, ipmi_data->addr_space,ipmi_data->base_addr)) {
1682                 dmi_data_entries++;
1683                 return 0;
1684         }
1685
1686         memset(ipmi_data, 0, sizeof(dmi_ipmi_data_t));
1687
1688         return -1;
1689 }
1690
1691 static int dmi_table(u32 base, int len, int num)
1692 {
1693         u8                __iomem *buf;
1694         struct dmi_header __iomem *dm;
1695         u8                __iomem *data;
1696         int               i=1;
1697         int               status=-1;
1698         int               intf_num = 0;
1699
1700         buf = ioremap(base, len);
1701         if(buf==NULL)
1702                 return -1;
1703
1704         data = buf;
1705
1706         while(i<num && (data - buf) < len)
1707         {
1708                 dm=(dmi_header_t __iomem *)data;
1709
1710                 if((data-buf+readb(&dm->length)) >= len)
1711                         break;
1712
1713                 if (readb(&dm->type) == 38) {
1714                         if (decode_dmi(dm, intf_num) == 0) {
1715                                 intf_num++;
1716                                 if (intf_num >= SI_MAX_DRIVERS)
1717                                         break;
1718                         }
1719                 }
1720
1721                 data+=readb(&dm->length);
1722                 while((data-buf) < len && (readb(data)||readb(data+1)))
1723                         data++;
1724                 data+=2;
1725                 i++;
1726         }
1727         iounmap(buf);
1728
1729         return status;
1730 }
1731
1732 static inline int dmi_checksum(u8 *buf)
1733 {
1734         u8   sum=0;
1735         int  a;
1736
1737         for(a=0; a<15; a++)
1738                 sum+=buf[a];
1739         return (sum==0);
1740 }
1741
1742 static int dmi_decode(void)
1743 {
1744         u8   buf[15];
1745         u32  fp=0xF0000;
1746
1747 #ifdef CONFIG_SIMNOW
1748         return -1;
1749 #endif
1750
1751         while(fp < 0xFFFFF)
1752         {
1753                 isa_memcpy_fromio(buf, fp, 15);
1754                 if(memcmp(buf, "_DMI_", 5)==0 && dmi_checksum(buf))
1755                 {
1756                         u16 num=buf[13]<<8|buf[12];
1757                         u16 len=buf[7]<<8|buf[6];
1758                         u32 base=buf[11]<<24|buf[10]<<16|buf[9]<<8|buf[8];
1759
1760                         if(dmi_table(base, len, num) == 0)
1761                                 return 0;
1762                 }
1763                 fp+=16;
1764         }
1765
1766         return -1;
1767 }
1768
1769 static int try_init_smbios(int intf_num, struct smi_info **new_info)
1770 {
1771         struct smi_info   *info;
1772         dmi_ipmi_data_t   *ipmi_data = dmi_data+intf_num;
1773         char              *io_type;
1774
1775         if (intf_num >= dmi_data_entries)
1776                 return -ENODEV;
1777
1778         switch(ipmi_data->type) {
1779                 case 0x01: /* KCS */
1780                         si_type[intf_num] = "kcs";
1781                         break;
1782                 case 0x02: /* SMIC */
1783                         si_type[intf_num] = "smic";
1784                         break;
1785                 case 0x03: /* BT */
1786                         si_type[intf_num] = "bt";
1787                         break;
1788                 default:
1789                         return -EIO;
1790         }
1791
1792         info = kmalloc(sizeof(*info), GFP_KERNEL);
1793         if (!info) {
1794                 printk(KERN_ERR "ipmi_si: Could not allocate SI data (4)\n");
1795                 return -ENOMEM;
1796         }
1797         memset(info, 0, sizeof(*info));
1798
1799         if (ipmi_data->addr_space == 1) {
1800                 io_type = "memory";
1801                 info->io_setup = mem_setup;
1802                 addrs[intf_num] = ipmi_data->base_addr;
1803                 info->io.info = &(addrs[intf_num]);
1804         } else if (ipmi_data->addr_space == 2) {
1805                 io_type = "I/O";
1806                 info->io_setup = port_setup;
1807                 ports[intf_num] = ipmi_data->base_addr;
1808                 info->io.info = &(ports[intf_num]);
1809         } else {
1810                 kfree(info);
1811                 printk("ipmi_si: Unknown SMBIOS I/O Address type.\n");
1812                 return -EIO;
1813         }
1814
1815         regspacings[intf_num] = ipmi_data->offset;
1816         info->io.regspacing = regspacings[intf_num];
1817         if (!info->io.regspacing)
1818                 info->io.regspacing = DEFAULT_REGSPACING;
1819         info->io.regsize = DEFAULT_REGSPACING;
1820         info->io.regshift = regshifts[intf_num];
1821
1822         info->slave_addr = ipmi_data->slave_addr;
1823
1824         irqs[intf_num] = ipmi_data->irq;
1825
1826         *new_info = info;
1827
1828         printk("ipmi_si: Found SMBIOS-specified state machine at %s"
1829                " address 0x%lx, slave address 0x%x\n",
1830                io_type, (unsigned long)ipmi_data->base_addr,
1831                ipmi_data->slave_addr);
1832         return 0;
1833 }
1834 #endif /* CONFIG_X86 */
1835
1836 #ifdef CONFIG_PCI
1837
1838 #define PCI_ERMC_CLASSCODE  0x0C0700
1839 #define PCI_HP_VENDOR_ID    0x103C
1840 #define PCI_MMC_DEVICE_ID   0x121A
1841 #define PCI_MMC_ADDR_CW     0x10
1842
1843 /* Avoid more than one attempt to probe pci smic. */
1844 static int pci_smic_checked = 0;
1845
1846 static int find_pci_smic(int intf_num, struct smi_info **new_info)
1847 {
1848         struct smi_info  *info;
1849         int              error;
1850         struct pci_dev   *pci_dev = NULL;
1851         u16              base_addr;
1852         int              fe_rmc = 0;
1853
1854         if (pci_smic_checked)
1855                 return -ENODEV;
1856
1857         pci_smic_checked = 1;
1858
1859         if ((pci_dev = pci_get_device(PCI_HP_VENDOR_ID, PCI_MMC_DEVICE_ID,
1860                                        NULL)))
1861                 ;
1862         else if ((pci_dev = pci_get_class(PCI_ERMC_CLASSCODE, NULL)) &&
1863                  pci_dev->subsystem_vendor == PCI_HP_VENDOR_ID)
1864                 fe_rmc = 1;
1865         else
1866                 return -ENODEV;
1867
1868         error = pci_read_config_word(pci_dev, PCI_MMC_ADDR_CW, &base_addr);
1869         if (error)
1870         {
1871                 pci_dev_put(pci_dev);
1872                 printk(KERN_ERR
1873                        "ipmi_si: pci_read_config_word() failed (%d).\n",
1874                        error);
1875                 return -ENODEV;
1876         }
1877
1878         /* Bit 0: 1 specifies programmed I/O, 0 specifies memory mapped I/O */
1879         if (!(base_addr & 0x0001))
1880         {
1881                 pci_dev_put(pci_dev);
1882                 printk(KERN_ERR
1883                        "ipmi_si: memory mapped I/O not supported for PCI"
1884                        " smic.\n");
1885                 return -ENODEV;
1886         }
1887
1888         base_addr &= 0xFFFE;
1889         if (!fe_rmc)
1890                 /* Data register starts at base address + 1 in eRMC */
1891                 ++base_addr;
1892
1893         if (!is_new_interface(-1, IPMI_IO_ADDR_SPACE, base_addr)) {
1894                 pci_dev_put(pci_dev);
1895                 return -ENODEV;
1896         }
1897
1898         info = kmalloc(sizeof(*info), GFP_KERNEL);
1899         if (!info) {
1900                 pci_dev_put(pci_dev);
1901                 printk(KERN_ERR "ipmi_si: Could not allocate SI data (5)\n");
1902                 return -ENOMEM;
1903         }
1904         memset(info, 0, sizeof(*info));
1905
1906         info->io_setup = port_setup;
1907         ports[intf_num] = base_addr;
1908         info->io.info = &(ports[intf_num]);
1909         info->io.regspacing = regspacings[intf_num];
1910         if (!info->io.regspacing)
1911                 info->io.regspacing = DEFAULT_REGSPACING;
1912         info->io.regsize = DEFAULT_REGSPACING;
1913         info->io.regshift = regshifts[intf_num];
1914
1915         *new_info = info;
1916
1917         irqs[intf_num] = pci_dev->irq;
1918         si_type[intf_num] = "smic";
1919
1920         printk("ipmi_si: Found PCI SMIC at I/O address 0x%lx\n",
1921                 (long unsigned int) base_addr);
1922
1923         pci_dev_put(pci_dev);
1924         return 0;
1925 }
1926 #endif /* CONFIG_PCI */
1927
1928 static int try_init_plug_and_play(int intf_num, struct smi_info **new_info)
1929 {
1930 #ifdef CONFIG_PCI
1931         if (find_pci_smic(intf_num, new_info)==0)
1932                 return 0;
1933 #endif
1934         /* Include other methods here. */
1935
1936         return -ENODEV;
1937 }
1938
1939
1940 static int try_get_dev_id(struct smi_info *smi_info)
1941 {
1942         unsigned char      msg[2];
1943         unsigned char      *resp;
1944         unsigned long      resp_len;
1945         enum si_sm_result smi_result;
1946         int               rv = 0;
1947
1948         resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1949         if (!resp)
1950                 return -ENOMEM;
1951
1952         /* Do a Get Device ID command, since it comes back with some
1953            useful info. */
1954         msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1955         msg[1] = IPMI_GET_DEVICE_ID_CMD;
1956         smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
1957
1958         smi_result = smi_info->handlers->event(smi_info->si_sm, 0);
1959         for (;;)
1960         {
1961                 if (smi_result == SI_SM_CALL_WITH_DELAY) {
1962                         set_current_state(TASK_UNINTERRUPTIBLE);
1963                         schedule_timeout(1);
1964                         smi_result = smi_info->handlers->event(
1965                                 smi_info->si_sm, 100);
1966                 }
1967                 else if (smi_result == SI_SM_CALL_WITHOUT_DELAY)
1968                 {
1969                         smi_result = smi_info->handlers->event(
1970                                 smi_info->si_sm, 0);
1971                 }
1972                 else
1973                         break;
1974         }
1975         if (smi_result == SI_SM_HOSED) {
1976                 /* We couldn't get the state machine to run, so whatever's at
1977                    the port is probably not an IPMI SMI interface. */
1978                 rv = -ENODEV;
1979                 goto out;
1980         }
1981
1982         /* Otherwise, we got some data. */
1983         resp_len = smi_info->handlers->get_result(smi_info->si_sm,
1984                                                   resp, IPMI_MAX_MSG_LENGTH);
1985         if (resp_len < 6) {
1986                 /* That's odd, it should be longer. */
1987                 rv = -EINVAL;
1988                 goto out;
1989         }
1990
1991         if ((resp[1] != IPMI_GET_DEVICE_ID_CMD) || (resp[2] != 0)) {
1992                 /* That's odd, it shouldn't be able to fail. */
1993                 rv = -EINVAL;
1994                 goto out;
1995         }
1996
1997         /* Record info from the get device id, in case we need it. */
1998         smi_info->ipmi_si_dev_rev = resp[4] & 0xf;
1999         smi_info->ipmi_si_fw_rev_major = resp[5] & 0x7f;
2000         smi_info->ipmi_si_fw_rev_minor = resp[6];
2001         smi_info->ipmi_version_major = resp[7] & 0xf;
2002         smi_info->ipmi_version_minor = resp[7] >> 4;
2003
2004  out:
2005         kfree(resp);
2006         return rv;
2007 }
2008
2009 static int type_file_read_proc(char *page, char **start, off_t off,
2010                                int count, int *eof, void *data)
2011 {
2012         char            *out = (char *) page;
2013         struct smi_info *smi = data;
2014
2015         switch (smi->si_type) {
2016             case SI_KCS:
2017                 return sprintf(out, "kcs\n");
2018             case SI_SMIC:
2019                 return sprintf(out, "smic\n");
2020             case SI_BT:
2021                 return sprintf(out, "bt\n");
2022             default:
2023                 return 0;
2024         }
2025 }
2026
2027 static int stat_file_read_proc(char *page, char **start, off_t off,
2028                                int count, int *eof, void *data)
2029 {
2030         char            *out = (char *) page;
2031         struct smi_info *smi = data;
2032
2033         out += sprintf(out, "interrupts_enabled:    %d\n",
2034                        smi->irq && !smi->interrupt_disabled);
2035         out += sprintf(out, "short_timeouts:        %ld\n",
2036                        smi->short_timeouts);
2037         out += sprintf(out, "long_timeouts:         %ld\n",
2038                        smi->long_timeouts);
2039         out += sprintf(out, "timeout_restarts:      %ld\n",
2040                        smi->timeout_restarts);
2041         out += sprintf(out, "idles:                 %ld\n",
2042                        smi->idles);
2043         out += sprintf(out, "interrupts:            %ld\n",
2044                        smi->interrupts);
2045         out += sprintf(out, "attentions:            %ld\n",
2046                        smi->attentions);
2047         out += sprintf(out, "flag_fetches:          %ld\n",
2048                        smi->flag_fetches);
2049         out += sprintf(out, "hosed_count:           %ld\n",
2050                        smi->hosed_count);
2051         out += sprintf(out, "complete_transactions: %ld\n",
2052                        smi->complete_transactions);
2053         out += sprintf(out, "events:                %ld\n",
2054                        smi->events);
2055         out += sprintf(out, "watchdog_pretimeouts:  %ld\n",
2056                        smi->watchdog_pretimeouts);
2057         out += sprintf(out, "incoming_messages:     %ld\n",
2058                        smi->incoming_messages);
2059
2060         return (out - ((char *) page));
2061 }
2062
2063 /* Returns 0 if initialized, or negative on an error. */
2064 static int init_one_smi(int intf_num, struct smi_info **smi)
2065 {
2066         int             rv;
2067         struct smi_info *new_smi;
2068
2069
2070         rv = try_init_mem(intf_num, &new_smi);
2071         if (rv)
2072                 rv = try_init_port(intf_num, &new_smi);
2073 #ifdef CONFIG_ACPI
2074         if ((rv) && (si_trydefaults)) {
2075                 rv = try_init_acpi(intf_num, &new_smi);
2076         }
2077 #endif
2078 #ifdef CONFIG_X86
2079         if ((rv) && (si_trydefaults)) {
2080                 rv = try_init_smbios(intf_num, &new_smi);
2081         }
2082 #endif
2083         if ((rv) && (si_trydefaults)) {
2084                 rv = try_init_plug_and_play(intf_num, &new_smi);
2085         }
2086
2087
2088         if (rv)
2089                 return rv;
2090
2091         /* So we know not to free it unless we have allocated one. */
2092         new_smi->intf = NULL;
2093         new_smi->si_sm = NULL;
2094         new_smi->handlers = NULL;
2095
2096         if (!new_smi->irq_setup) {
2097                 new_smi->irq = irqs[intf_num];
2098                 new_smi->irq_setup = std_irq_setup;
2099                 new_smi->irq_cleanup = std_irq_cleanup;
2100         }
2101
2102         /* Default to KCS if no type is specified. */
2103         if (si_type[intf_num] == NULL) {
2104                 if (si_trydefaults)
2105                         si_type[intf_num] = "kcs";
2106                 else {
2107                         rv = -EINVAL;
2108                         goto out_err;
2109                 }
2110         }
2111
2112         /* Set up the state machine to use. */
2113         if (strcmp(si_type[intf_num], "kcs") == 0) {
2114                 new_smi->handlers = &kcs_smi_handlers;
2115                 new_smi->si_type = SI_KCS;
2116         } else if (strcmp(si_type[intf_num], "smic") == 0) {
2117                 new_smi->handlers = &smic_smi_handlers;
2118                 new_smi->si_type = SI_SMIC;
2119         } else if (strcmp(si_type[intf_num], "bt") == 0) {
2120                 new_smi->handlers = &bt_smi_handlers;
2121                 new_smi->si_type = SI_BT;
2122         } else {
2123                 /* No support for anything else yet. */
2124                 rv = -EIO;
2125                 goto out_err;
2126         }
2127
2128         /* Allocate the state machine's data and initialize it. */
2129         new_smi->si_sm = kmalloc(new_smi->handlers->size(), GFP_KERNEL);
2130         if (!new_smi->si_sm) {
2131                 printk(" Could not allocate state machine memory\n");
2132                 rv = -ENOMEM;
2133                 goto out_err;
2134         }
2135         new_smi->io_size = new_smi->handlers->init_data(new_smi->si_sm,
2136                                                         &new_smi->io);
2137
2138         /* Now that we know the I/O size, we can set up the I/O. */
2139         rv = new_smi->io_setup(new_smi);
2140         if (rv) {
2141                 printk(" Could not set up I/O space\n");
2142                 goto out_err;
2143         }
2144
2145         spin_lock_init(&(new_smi->si_lock));
2146         spin_lock_init(&(new_smi->msg_lock));
2147         spin_lock_init(&(new_smi->count_lock));
2148
2149         /* Do low-level detection first. */
2150         if (new_smi->handlers->detect(new_smi->si_sm)) {
2151                 rv = -ENODEV;
2152                 goto out_err;
2153         }
2154
2155         /* Attempt a get device id command.  If it fails, we probably
2156            don't have a SMI here. */
2157         rv = try_get_dev_id(new_smi);
2158         if (rv)
2159                 goto out_err;
2160
2161         /* Try to claim any interrupts. */
2162         new_smi->irq_setup(new_smi);
2163
2164         INIT_LIST_HEAD(&(new_smi->xmit_msgs));
2165         INIT_LIST_HEAD(&(new_smi->hp_xmit_msgs));
2166         new_smi->curr_msg = NULL;
2167         atomic_set(&new_smi->req_events, 0);
2168         new_smi->run_to_completion = 0;
2169
2170         new_smi->interrupt_disabled = 0;
2171         new_smi->timer_stopped = 0;
2172         new_smi->stop_operation = 0;
2173
2174         /* Start clearing the flags before we enable interrupts or the
2175            timer to avoid racing with the timer. */
2176         start_clear_flags(new_smi);
2177         /* IRQ is defined to be set when non-zero. */
2178         if (new_smi->irq)
2179                 new_smi->si_state = SI_CLEARING_FLAGS_THEN_SET_IRQ;
2180
2181         /* The ipmi_register_smi() code does some operations to
2182            determine the channel information, so we must be ready to
2183            handle operations before it is called.  This means we have
2184            to stop the timer if we get an error after this point. */
2185         init_timer(&(new_smi->si_timer));
2186         new_smi->si_timer.data = (long) new_smi;
2187         new_smi->si_timer.function = smi_timeout;
2188         new_smi->last_timeout_jiffies = jiffies;
2189         new_smi->si_timer.expires = jiffies + SI_TIMEOUT_JIFFIES;
2190         add_timer(&(new_smi->si_timer));
2191
2192         rv = ipmi_register_smi(&handlers,
2193                                new_smi,
2194                                new_smi->ipmi_version_major,
2195                                new_smi->ipmi_version_minor,
2196                                new_smi->slave_addr,
2197                                &(new_smi->intf));
2198         if (rv) {
2199                 printk(KERN_ERR
2200                        "ipmi_si: Unable to register device: error %d\n",
2201                        rv);
2202                 goto out_err_stop_timer;
2203         }
2204
2205         rv = ipmi_smi_add_proc_entry(new_smi->intf, "type",
2206                                      type_file_read_proc, NULL,
2207                                      new_smi, THIS_MODULE);
2208         if (rv) {
2209                 printk(KERN_ERR
2210                        "ipmi_si: Unable to create proc entry: %d\n",
2211                        rv);
2212                 goto out_err_stop_timer;
2213         }
2214
2215         rv = ipmi_smi_add_proc_entry(new_smi->intf, "si_stats",
2216                                      stat_file_read_proc, NULL,
2217                                      new_smi, THIS_MODULE);
2218         if (rv) {
2219                 printk(KERN_ERR
2220                        "ipmi_si: Unable to create proc entry: %d\n",
2221                        rv);
2222                 goto out_err_stop_timer;
2223         }
2224
2225         *smi = new_smi;
2226
2227         printk(" IPMI %s interface initialized\n", si_type[intf_num]);
2228
2229         return 0;
2230
2231  out_err_stop_timer:
2232         new_smi->stop_operation = 1;
2233
2234         /* Wait for the timer to stop.  This avoids problems with race
2235            conditions removing the timer here. */
2236         while (!new_smi->timer_stopped) {
2237                 set_current_state(TASK_UNINTERRUPTIBLE);
2238                 schedule_timeout(1);
2239         }
2240
2241  out_err:
2242         if (new_smi->intf)
2243                 ipmi_unregister_smi(new_smi->intf);
2244
2245         new_smi->irq_cleanup(new_smi);
2246
2247         /* Wait until we know that we are out of any interrupt
2248            handlers might have been running before we freed the
2249            interrupt. */
2250         synchronize_sched();
2251
2252         if (new_smi->si_sm) {
2253                 if (new_smi->handlers)
2254                         new_smi->handlers->cleanup(new_smi->si_sm);
2255                 kfree(new_smi->si_sm);
2256         }
2257         new_smi->io_cleanup(new_smi);
2258
2259         return rv;
2260 }
2261
2262 static __init int init_ipmi_si(void)
2263 {
2264         int  rv = 0;
2265         int  pos = 0;
2266         int  i;
2267         char *str;
2268
2269         if (initialized)
2270                 return 0;
2271         initialized = 1;
2272
2273         /* Parse out the si_type string into its components. */
2274         str = si_type_str;
2275         if (*str != '\0') {
2276                 for (i=0; (i<SI_MAX_PARMS) && (*str != '\0'); i++) {
2277                         si_type[i] = str;
2278                         str = strchr(str, ',');
2279                         if (str) {
2280                                 *str = '\0';
2281                                 str++;
2282                         } else {
2283                                 break;
2284                         }
2285                 }
2286         }
2287
2288         printk(KERN_INFO "IPMI System Interface driver version "
2289                IPMI_SI_VERSION);
2290         if (kcs_smi_handlers.version)
2291                 printk(", KCS version %s", kcs_smi_handlers.version);
2292         if (smic_smi_handlers.version)
2293                 printk(", SMIC version %s", smic_smi_handlers.version);
2294         if (bt_smi_handlers.version)
2295                 printk(", BT version %s", bt_smi_handlers.version);
2296         printk("\n");
2297
2298 #ifdef CONFIG_X86
2299         dmi_decode();
2300 #endif
2301
2302         rv = init_one_smi(0, &(smi_infos[pos]));
2303         if (rv && !ports[0] && si_trydefaults) {
2304                 /* If we are trying defaults and the initial port is
2305                    not set, then set it. */
2306                 si_type[0] = "kcs";
2307                 ports[0] = DEFAULT_KCS_IO_PORT;
2308                 rv = init_one_smi(0, &(smi_infos[pos]));
2309                 if (rv) {
2310                         /* No KCS - try SMIC */
2311                         si_type[0] = "smic";
2312                         ports[0] = DEFAULT_SMIC_IO_PORT;
2313                         rv = init_one_smi(0, &(smi_infos[pos]));
2314                 }
2315                 if (rv) {
2316                         /* No SMIC - try BT */
2317                         si_type[0] = "bt";
2318                         ports[0] = DEFAULT_BT_IO_PORT;
2319                         rv = init_one_smi(0, &(smi_infos[pos]));
2320                 }
2321         }
2322         if (rv == 0)
2323                 pos++;
2324
2325         for (i=1; i < SI_MAX_PARMS; i++) {
2326                 rv = init_one_smi(i, &(smi_infos[pos]));
2327                 if (rv == 0)
2328                         pos++;
2329         }
2330
2331         if (smi_infos[0] == NULL) {
2332                 printk("ipmi_si: Unable to find any System Interface(s)\n");
2333                 return -ENODEV;
2334         }
2335
2336         return 0;
2337 }
2338 module_init(init_ipmi_si);
2339
2340 static void __exit cleanup_one_si(struct smi_info *to_clean)
2341 {
2342         int           rv;
2343         unsigned long flags;
2344
2345         if (! to_clean)
2346                 return;
2347
2348         /* Tell the timer and interrupt handlers that we are shutting
2349            down. */
2350         spin_lock_irqsave(&(to_clean->si_lock), flags);
2351         spin_lock(&(to_clean->msg_lock));
2352
2353         to_clean->stop_operation = 1;
2354
2355         to_clean->irq_cleanup(to_clean);
2356
2357         spin_unlock(&(to_clean->msg_lock));
2358         spin_unlock_irqrestore(&(to_clean->si_lock), flags);
2359
2360         /* Wait until we know that we are out of any interrupt
2361            handlers might have been running before we freed the
2362            interrupt. */
2363         synchronize_sched();
2364
2365         /* Wait for the timer to stop.  This avoids problems with race
2366            conditions removing the timer here. */
2367         while (!to_clean->timer_stopped) {
2368                 set_current_state(TASK_UNINTERRUPTIBLE);
2369                 schedule_timeout(1);
2370         }
2371
2372         /* Interrupts and timeouts are stopped, now make sure the
2373            interface is in a clean state. */
2374         while ((to_clean->curr_msg) || (to_clean->si_state != SI_NORMAL)) {
2375                 poll(to_clean);
2376                 set_current_state(TASK_UNINTERRUPTIBLE);
2377                 schedule_timeout(1);
2378         }
2379
2380         rv = ipmi_unregister_smi(to_clean->intf);
2381         if (rv) {
2382                 printk(KERN_ERR
2383                        "ipmi_si: Unable to unregister device: errno=%d\n",
2384                        rv);
2385         }
2386
2387         to_clean->handlers->cleanup(to_clean->si_sm);
2388
2389         kfree(to_clean->si_sm);
2390
2391         to_clean->io_cleanup(to_clean);
2392 }
2393
2394 static __exit void cleanup_ipmi_si(void)
2395 {
2396         int i;
2397
2398         if (!initialized)
2399                 return;
2400
2401         for (i=0; i<SI_MAX_DRIVERS; i++) {
2402                 cleanup_one_si(smi_infos[i]);
2403         }
2404 }
2405 module_exit(cleanup_ipmi_si);
2406
2407 MODULE_LICENSE("GPL");