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