VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / char / ipmi / ipmi_msghandler.c
1 /*
2  * ipmi_msghandler.c
3  *
4  * Incoming and outgoing message routing for an IPMI interface.
5  *
6  * Author: MontaVista Software, Inc.
7  *         Corey Minyard <minyard@mvista.com>
8  *         source@mvista.com
9  *
10  * Copyright 2002 MontaVista Software Inc.
11  *
12  *  This program is free software; you can redistribute it and/or modify it
13  *  under the terms of the GNU General Public License as published by the
14  *  Free Software Foundation; either version 2 of the License, or (at your
15  *  option) any later version.
16  *
17  *
18  *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
19  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
24  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
26  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
27  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  *  You should have received a copy of the GNU General Public License along
30  *  with this program; if not, write to the Free Software Foundation, Inc.,
31  *  675 Mass Ave, Cambridge, MA 02139, USA.
32  */
33
34 #include <linux/config.h>
35 #include <linux/module.h>
36 #include <linux/errno.h>
37 #include <asm/system.h>
38 #include <linux/sched.h>
39 #include <linux/poll.h>
40 #include <linux/spinlock.h>
41 #include <linux/rwsem.h>
42 #include <linux/slab.h>
43 #include <linux/ipmi.h>
44 #include <linux/ipmi_smi.h>
45 #include <linux/notifier.h>
46 #include <linux/init.h>
47 #include <linux/proc_fs.h>
48
49 #define IPMI_MSGHANDLER_VERSION "v32"
50
51 struct ipmi_recv_msg *ipmi_alloc_recv_msg(void);
52 static int ipmi_init_msghandler(void);
53
54 static int initialized = 0;
55
56 static struct proc_dir_entry *proc_ipmi_root = NULL;
57
58 #define MAX_EVENTS_IN_QUEUE     25
59
60 /* Don't let a message sit in a queue forever, always time it with at lest
61    the max message timer.  This is in milliseconds. */
62 #define MAX_MSG_TIMEOUT         60000
63
64 struct ipmi_user
65 {
66         struct list_head link;
67
68         /* The upper layer that handles receive messages. */
69         struct ipmi_user_hndl *handler;
70         void             *handler_data;
71
72         /* The interface this user is bound to. */
73         ipmi_smi_t intf;
74
75         /* Does this interface receive IPMI events? */
76         int gets_events;
77 };
78
79 struct cmd_rcvr
80 {
81         struct list_head link;
82
83         ipmi_user_t   user;
84         unsigned char netfn;
85         unsigned char cmd;
86 };
87
88 struct seq_table
89 {
90         unsigned int         inuse : 1;
91         unsigned int         broadcast : 1;
92
93         unsigned long        timeout;
94         unsigned long        orig_timeout;
95         unsigned int         retries_left;
96
97         /* To verify on an incoming send message response that this is
98            the message that the response is for, we keep a sequence id
99            and increment it every time we send a message. */
100         long                 seqid;
101
102         /* This is held so we can properly respond to the message on a
103            timeout, and it is used to hold the temporary data for
104            retransmission, too. */
105         struct ipmi_recv_msg *recv_msg;
106 };
107
108 /* Store the information in a msgid (long) to allow us to find a
109    sequence table entry from the msgid. */
110 #define STORE_SEQ_IN_MSGID(seq, seqid) (((seq&0xff)<<26) | (seqid&0x3ffffff))
111
112 #define GET_SEQ_FROM_MSGID(msgid, seq, seqid) \
113         do {                                                            \
114                 seq = ((msgid >> 26) & 0x3f);                           \
115                 seqid = (msgid & 0x3fffff);                             \
116         } while(0)
117
118 #define NEXT_SEQID(seqid) (((seqid) + 1) & 0x3fffff)
119
120 struct ipmi_channel
121 {
122         unsigned char medium;
123         unsigned char protocol;
124 };
125
126 struct ipmi_proc_entry
127 {
128         char                   *name;
129         struct ipmi_proc_entry *next;
130 };
131
132 #define IPMI_IPMB_NUM_SEQ       64
133 #define IPMI_MAX_CHANNELS       8
134 struct ipmi_smi
135 {
136         /* What interface number are we? */
137         int intf_num;
138
139         /* The list of upper layers that are using me.  We read-lock
140            this when delivering messages to the upper layer to keep
141            the user from going away while we are processing the
142            message.  This means that you cannot add or delete a user
143            from the receive callback. */
144         rwlock_t                users_lock;
145         struct list_head        users;
146
147         /* Used for wake ups at startup. */
148         wait_queue_head_t waitq;
149
150         /* The IPMI version of the BMC on the other end. */
151         unsigned char       version_major;
152         unsigned char       version_minor;
153
154         /* This is the lower-layer's sender routine. */
155         struct ipmi_smi_handlers *handlers;
156         void                     *send_info;
157
158         /* A list of proc entries for this interface.  This does not
159            need a lock, only one thread creates it and only one thread
160            destroys it. */
161         struct ipmi_proc_entry *proc_entries;
162
163         /* A table of sequence numbers for this interface.  We use the
164            sequence numbers for IPMB messages that go out of the
165            interface to match them up with their responses.  A routine
166            is called periodically to time the items in this list. */
167         spinlock_t       seq_lock;
168         struct seq_table seq_table[IPMI_IPMB_NUM_SEQ];
169         int curr_seq;
170
171         /* Messages that were delayed for some reason (out of memory,
172            for instance), will go in here to be processed later in a
173            periodic timer interrupt. */
174         spinlock_t       waiting_msgs_lock;
175         struct list_head waiting_msgs;
176
177         /* The list of command receivers that are registered for commands
178            on this interface. */
179         rwlock_t         cmd_rcvr_lock;
180         struct list_head cmd_rcvrs;
181
182         /* Events that were queues because no one was there to receive
183            them. */
184         spinlock_t       events_lock; /* For dealing with event stuff. */
185         struct list_head waiting_events;
186         unsigned int     waiting_events_count; /* How many events in queue? */
187
188         /* This will be non-null if someone registers to receive all
189            IPMI commands (this is for interface emulation).  There
190            may not be any things in the cmd_rcvrs list above when
191            this is registered. */
192         ipmi_user_t all_cmd_rcvr;
193
194         /* My slave address.  This is initialized to IPMI_BMC_SLAVE_ADDR,
195            but may be changed by the user. */
196         unsigned char my_address;
197
198         /* My LUN.  This should generally stay the SMS LUN, but just in
199            case... */
200         unsigned char my_lun;
201
202         /* The event receiver for my BMC, only really used at panic
203            shutdown as a place to store this. */
204         unsigned char event_receiver;
205         unsigned char event_receiver_lun;
206         unsigned char local_sel_device;
207         unsigned char local_event_generator;
208
209         /* A cheap hack, if this is non-null and a message to an
210            interface comes in with a NULL user, call this routine with
211            it.  Note that the message will still be freed by the
212            caller.  This only works on the system interface. */
213         void (*null_user_handler)(ipmi_smi_t intf, struct ipmi_smi_msg *msg);
214
215         /* When we are scanning the channels for an SMI, this will
216            tell which channel we are scanning. */
217         int curr_channel;
218
219         /* Channel information */
220         struct ipmi_channel channels[IPMI_MAX_CHANNELS];
221
222         /* Proc FS stuff. */
223         struct proc_dir_entry *proc_dir;
224         char                  proc_dir_name[10];
225
226         spinlock_t   counter_lock; /* For making counters atomic. */
227
228         /* Commands we got that were invalid. */
229         unsigned int sent_invalid_commands;
230
231         /* Commands we sent to the MC. */
232         unsigned int sent_local_commands;
233         /* Responses from the MC that were delivered to a user. */
234         unsigned int handled_local_responses;
235         /* Responses from the MC that were not delivered to a user. */
236         unsigned int unhandled_local_responses;
237
238         /* Commands we sent out to the IPMB bus. */
239         unsigned int sent_ipmb_commands;
240         /* Commands sent on the IPMB that had errors on the SEND CMD */
241         unsigned int sent_ipmb_command_errs;
242         /* Each retransmit increments this count. */
243         unsigned int retransmitted_ipmb_commands;
244         /* When a message times out (runs out of retransmits) this is
245            incremented. */
246         unsigned int timed_out_ipmb_commands;
247
248         /* This is like above, but for broadcasts.  Broadcasts are
249            *not* included in the above count (they are expected to
250            time out). */
251         unsigned int timed_out_ipmb_broadcasts;
252
253         /* Responses I have sent to the IPMB bus. */
254         unsigned int sent_ipmb_responses;
255
256         /* The response was delivered to the user. */
257         unsigned int handled_ipmb_responses;
258         /* The response had invalid data in it. */
259         unsigned int invalid_ipmb_responses;
260         /* The response didn't have anyone waiting for it. */
261         unsigned int unhandled_ipmb_responses;
262
263         /* Commands we sent out to the IPMB bus. */
264         unsigned int sent_lan_commands;
265         /* Commands sent on the IPMB that had errors on the SEND CMD */
266         unsigned int sent_lan_command_errs;
267         /* Each retransmit increments this count. */
268         unsigned int retransmitted_lan_commands;
269         /* When a message times out (runs out of retransmits) this is
270            incremented. */
271         unsigned int timed_out_lan_commands;
272
273         /* Responses I have sent to the IPMB bus. */
274         unsigned int sent_lan_responses;
275
276         /* The response was delivered to the user. */
277         unsigned int handled_lan_responses;
278         /* The response had invalid data in it. */
279         unsigned int invalid_lan_responses;
280         /* The response didn't have anyone waiting for it. */
281         unsigned int unhandled_lan_responses;
282
283         /* The command was delivered to the user. */
284         unsigned int handled_commands;
285         /* The command had invalid data in it. */
286         unsigned int invalid_commands;
287         /* The command didn't have anyone waiting for it. */
288         unsigned int unhandled_commands;
289
290         /* Invalid data in an event. */
291         unsigned int invalid_events;
292         /* Events that were received with the proper format. */
293         unsigned int events;
294 };
295
296 int
297 ipmi_register_all_cmd_rcvr(ipmi_user_t user)
298 {
299         unsigned long flags;
300         int           rv = -EBUSY;
301
302         write_lock_irqsave(&(user->intf->users_lock), flags);
303         write_lock(&(user->intf->cmd_rcvr_lock));
304         if ((user->intf->all_cmd_rcvr == NULL)
305             && (list_empty(&(user->intf->cmd_rcvrs))))
306         {
307                 user->intf->all_cmd_rcvr = user;
308                 rv = 0;
309         }
310         write_unlock(&(user->intf->cmd_rcvr_lock));
311         write_unlock_irqrestore(&(user->intf->users_lock), flags);
312         return rv;
313 }
314
315 int
316 ipmi_unregister_all_cmd_rcvr(ipmi_user_t user)
317 {
318         unsigned long flags;
319         int           rv = -EINVAL;
320
321         write_lock_irqsave(&(user->intf->users_lock), flags);
322         write_lock(&(user->intf->cmd_rcvr_lock));
323         if (user->intf->all_cmd_rcvr == user)
324         {
325                 user->intf->all_cmd_rcvr = NULL;
326                 rv = 0;
327         }
328         write_unlock(&(user->intf->cmd_rcvr_lock));
329         write_unlock_irqrestore(&(user->intf->users_lock), flags);
330         return rv;
331 }
332
333
334 #define MAX_IPMI_INTERFACES 4
335 static ipmi_smi_t ipmi_interfaces[MAX_IPMI_INTERFACES];
336
337 /* Used to keep interfaces from going away while operations are
338    operating on interfaces.  Grab read if you are not modifying the
339    interfaces, write if you are. */
340 static DECLARE_RWSEM(interfaces_sem);
341
342 /* Directly protects the ipmi_interfaces data structure.  This is
343    claimed in the timer interrupt. */
344 static spinlock_t interfaces_lock = SPIN_LOCK_UNLOCKED;
345
346 /* List of watchers that want to know when smi's are added and
347    deleted. */
348 static struct list_head smi_watchers = LIST_HEAD_INIT(smi_watchers);
349 static DECLARE_RWSEM(smi_watchers_sem);
350
351 int ipmi_smi_watcher_register(struct ipmi_smi_watcher *watcher)
352 {
353         int i;
354
355         down_read(&interfaces_sem);
356         down_write(&smi_watchers_sem);
357         list_add(&(watcher->link), &smi_watchers);
358         for (i=0; i<MAX_IPMI_INTERFACES; i++) {
359                 if (ipmi_interfaces[i] != NULL) {
360                         watcher->new_smi(i);
361                 }
362         }
363         up_write(&smi_watchers_sem);
364         up_read(&interfaces_sem);
365         return 0;
366 }
367
368 int ipmi_smi_watcher_unregister(struct ipmi_smi_watcher *watcher)
369 {
370         down_write(&smi_watchers_sem);
371         list_del(&(watcher->link));
372         up_write(&smi_watchers_sem);
373         return 0;
374 }
375
376 static void
377 call_smi_watchers(int i)
378 {
379         struct ipmi_smi_watcher *w;
380
381         down_read(&smi_watchers_sem);
382         list_for_each_entry(w, &smi_watchers, link) {
383                 if (try_module_get(w->owner)) {
384                         w->new_smi(i);
385                         module_put(w->owner);
386                 }
387         }
388         up_read(&smi_watchers_sem);
389 }
390
391 int
392 ipmi_addr_equal(struct ipmi_addr *addr1, struct ipmi_addr *addr2)
393 {
394         if (addr1->addr_type != addr2->addr_type)
395                 return 0;
396
397         if (addr1->channel != addr2->channel)
398                 return 0;
399
400         if (addr1->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
401                 struct ipmi_system_interface_addr *smi_addr1
402                     = (struct ipmi_system_interface_addr *) addr1;
403                 struct ipmi_system_interface_addr *smi_addr2
404                     = (struct ipmi_system_interface_addr *) addr2;
405                 return (smi_addr1->lun == smi_addr2->lun);
406         }
407
408         if ((addr1->addr_type == IPMI_IPMB_ADDR_TYPE)
409             || (addr1->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE))
410         {
411                 struct ipmi_ipmb_addr *ipmb_addr1
412                     = (struct ipmi_ipmb_addr *) addr1;
413                 struct ipmi_ipmb_addr *ipmb_addr2
414                     = (struct ipmi_ipmb_addr *) addr2;
415
416                 return ((ipmb_addr1->slave_addr == ipmb_addr2->slave_addr)
417                         && (ipmb_addr1->lun == ipmb_addr2->lun));
418         }
419
420         if (addr1->addr_type == IPMI_LAN_ADDR_TYPE) {
421                 struct ipmi_lan_addr *lan_addr1
422                         = (struct ipmi_lan_addr *) addr1;
423                 struct ipmi_lan_addr *lan_addr2
424                     = (struct ipmi_lan_addr *) addr2;
425
426                 return ((lan_addr1->remote_SWID == lan_addr2->remote_SWID)
427                         && (lan_addr1->local_SWID == lan_addr2->local_SWID)
428                         && (lan_addr1->session_handle
429                             == lan_addr2->session_handle)
430                         && (lan_addr1->lun == lan_addr2->lun));
431         }
432
433         return 1;
434 }
435
436 int ipmi_validate_addr(struct ipmi_addr *addr, int len)
437 {
438         if (len < sizeof(struct ipmi_system_interface_addr)) {
439                 return -EINVAL;
440         }
441
442         if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
443                 if (addr->channel != IPMI_BMC_CHANNEL)
444                         return -EINVAL;
445                 return 0;
446         }
447
448         if ((addr->channel == IPMI_BMC_CHANNEL)
449             || (addr->channel >= IPMI_NUM_CHANNELS)
450             || (addr->channel < 0))
451                 return -EINVAL;
452
453         if ((addr->addr_type == IPMI_IPMB_ADDR_TYPE)
454             || (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE))
455         {
456                 if (len < sizeof(struct ipmi_ipmb_addr)) {
457                         return -EINVAL;
458                 }
459                 return 0;
460         }
461
462         if (addr->addr_type == IPMI_LAN_ADDR_TYPE) {
463                 if (len < sizeof(struct ipmi_lan_addr)) {
464                         return -EINVAL;
465                 }
466                 return 0;
467         }
468
469         return -EINVAL;
470 }
471
472 unsigned int ipmi_addr_length(int addr_type)
473 {
474         if (addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
475                 return sizeof(struct ipmi_system_interface_addr);
476
477         if ((addr_type == IPMI_IPMB_ADDR_TYPE)
478             || (addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE))
479         {
480                 return sizeof(struct ipmi_ipmb_addr);
481         }
482
483         return 0;
484 }
485
486 static void deliver_response(struct ipmi_recv_msg *msg)
487 {
488         msg->user->handler->ipmi_recv_hndl(msg, msg->user->handler_data);
489 }
490
491 /* Find the next sequence number not being used and add the given
492    message with the given timeout to the sequence table.  This must be
493    called with the interface's seq_lock held. */
494 static int intf_next_seq(ipmi_smi_t           intf,
495                          struct ipmi_recv_msg *recv_msg,
496                          unsigned long        timeout,
497                          int                  retries,
498                          int                  broadcast,
499                          unsigned char        *seq,
500                          long                 *seqid)
501 {
502         int          rv = 0;
503         unsigned int i;
504
505         for (i=intf->curr_seq;
506              (i+1)%IPMI_IPMB_NUM_SEQ != intf->curr_seq;
507              i=(i+1)%IPMI_IPMB_NUM_SEQ)
508         {
509                 if (! intf->seq_table[i].inuse)
510                         break;
511         }
512
513         if (! intf->seq_table[i].inuse) {
514                 intf->seq_table[i].recv_msg = recv_msg;
515
516                 /* Start with the maximum timeout, when the send response
517                    comes in we will start the real timer. */
518                 intf->seq_table[i].timeout = MAX_MSG_TIMEOUT;
519                 intf->seq_table[i].orig_timeout = timeout;
520                 intf->seq_table[i].retries_left = retries;
521                 intf->seq_table[i].broadcast = broadcast;
522                 intf->seq_table[i].inuse = 1;
523                 intf->seq_table[i].seqid = NEXT_SEQID(intf->seq_table[i].seqid);
524                 *seq = i;
525                 *seqid = intf->seq_table[i].seqid;
526                 intf->curr_seq = (i+1)%IPMI_IPMB_NUM_SEQ;
527         } else {
528                 rv = -EAGAIN;
529         }
530         
531         return rv;
532 }
533
534 /* Return the receive message for the given sequence number and
535    release the sequence number so it can be reused.  Some other data
536    is passed in to be sure the message matches up correctly (to help
537    guard against message coming in after their timeout and the
538    sequence number being reused). */
539 static int intf_find_seq(ipmi_smi_t           intf,
540                          unsigned char        seq,
541                          short                channel,
542                          unsigned char        cmd,
543                          unsigned char        netfn,
544                          struct ipmi_addr     *addr,
545                          struct ipmi_recv_msg **recv_msg)
546 {
547         int           rv = -ENODEV;
548         unsigned long flags;
549
550         if (seq >= IPMI_IPMB_NUM_SEQ)
551                 return -EINVAL;
552
553         spin_lock_irqsave(&(intf->seq_lock), flags);
554         if (intf->seq_table[seq].inuse) {
555                 struct ipmi_recv_msg *msg = intf->seq_table[seq].recv_msg;
556
557                 if ((msg->addr.channel == channel)
558                     && (msg->msg.cmd == cmd)
559                     && (msg->msg.netfn == netfn)
560                     && (ipmi_addr_equal(addr, &(msg->addr))))
561                 {
562                         *recv_msg = msg;
563                         intf->seq_table[seq].inuse = 0;
564                         rv = 0;
565                 }
566         }
567         spin_unlock_irqrestore(&(intf->seq_lock), flags);
568
569         return rv;
570 }
571
572
573 /* Start the timer for a specific sequence table entry. */
574 static int intf_start_seq_timer(ipmi_smi_t intf,
575                                 long       msgid)
576 {
577         int           rv = -ENODEV;
578         unsigned long flags;
579         unsigned char seq;
580         unsigned long seqid;
581
582
583         GET_SEQ_FROM_MSGID(msgid, seq, seqid);
584
585         spin_lock_irqsave(&(intf->seq_lock), flags);
586         /* We do this verification because the user can be deleted
587            while a message is outstanding. */
588         if ((intf->seq_table[seq].inuse)
589             && (intf->seq_table[seq].seqid == seqid))
590         {
591                 struct seq_table *ent = &(intf->seq_table[seq]);
592                 ent->timeout = ent->orig_timeout;
593                 rv = 0;
594         }
595         spin_unlock_irqrestore(&(intf->seq_lock), flags);
596
597         return rv;
598 }
599
600 /* Got an error for the send message for a specific sequence number. */
601 static int intf_err_seq(ipmi_smi_t   intf,
602                         long         msgid,
603                         unsigned int err)
604 {
605         int                  rv = -ENODEV;
606         unsigned long        flags;
607         unsigned char        seq;
608         unsigned long        seqid;
609         struct ipmi_recv_msg *msg = NULL;
610
611
612         GET_SEQ_FROM_MSGID(msgid, seq, seqid);
613
614         spin_lock_irqsave(&(intf->seq_lock), flags);
615         /* We do this verification because the user can be deleted
616            while a message is outstanding. */
617         if ((intf->seq_table[seq].inuse)
618             && (intf->seq_table[seq].seqid == seqid))
619         {
620                 struct seq_table *ent = &(intf->seq_table[seq]);
621
622                 ent->inuse = 0;
623                 msg = ent->recv_msg;
624                 rv = 0;
625         }
626         spin_unlock_irqrestore(&(intf->seq_lock), flags);
627
628         if (msg) {
629                 msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
630                 msg->msg_data[0] = err;
631                 msg->msg.netfn |= 1; /* Convert to a response. */
632                 msg->msg.data_len = 1;
633                 msg->msg.data = msg->msg_data;
634                 deliver_response(msg);
635         }
636
637         return rv;
638 }
639
640
641 int ipmi_create_user(unsigned int          if_num,
642                      struct ipmi_user_hndl *handler,
643                      void                  *handler_data,
644                      ipmi_user_t           *user)
645 {
646         unsigned long flags;
647         ipmi_user_t   new_user;
648         int           rv = 0;
649
650         /* There is no module usecount here, because it's not
651            required.  Since this can only be used by and called from
652            other modules, they will implicitly use this module, and
653            thus this can't be removed unless the other modules are
654            removed. */
655
656         if (handler == NULL)
657                 return -EINVAL;
658
659         /* Make sure the driver is actually initialized, this handles
660            problems with initialization order. */
661         if (!initialized) {
662                 rv = ipmi_init_msghandler();
663                 if (rv)
664                         return rv;
665
666                 /* The init code doesn't return an error if it was turned
667                    off, but it won't initialize.  Check that. */
668                 if (!initialized)
669                         return -ENODEV;
670         }
671
672         new_user = kmalloc(sizeof(*new_user), GFP_KERNEL);
673         if (! new_user)
674                 return -ENOMEM;
675
676         down_read(&interfaces_sem);
677         if ((if_num > MAX_IPMI_INTERFACES) || ipmi_interfaces[if_num] == NULL)
678         {
679                 rv = -EINVAL;
680                 goto out_unlock;
681         }
682
683         new_user->handler = handler;
684         new_user->handler_data = handler_data;
685         new_user->intf = ipmi_interfaces[if_num];
686         new_user->gets_events = 0;
687
688         if (!try_module_get(new_user->intf->handlers->owner)) {
689                 rv = -ENODEV;
690                 goto out_unlock;
691         }
692
693         write_lock_irqsave(&new_user->intf->users_lock, flags);
694         list_add_tail(&new_user->link, &new_user->intf->users);
695         write_unlock_irqrestore(&new_user->intf->users_lock, flags);
696
697  out_unlock:    
698         if (rv) {
699                 kfree(new_user);
700         } else {
701                 *user = new_user;
702         }
703
704         up_read(&interfaces_sem);
705         return rv;
706 }
707
708 static int ipmi_destroy_user_nolock(ipmi_user_t user)
709 {
710         int              rv = -ENODEV;
711         ipmi_user_t      t_user;
712         struct cmd_rcvr  *rcvr, *rcvr2;
713         int              i;
714         unsigned long    flags;
715
716         /* Find the user and delete them from the list. */
717         list_for_each_entry(t_user, &(user->intf->users), link) {
718                 if (t_user == user) {
719                         list_del(&t_user->link);
720                         rv = 0;
721                         break;
722                 }
723         }
724
725         if (rv) {
726                 goto out_unlock;
727         }
728
729         /* Remove the user from the interfaces sequence table. */
730         spin_lock_irqsave(&(user->intf->seq_lock), flags);
731         for (i=0; i<IPMI_IPMB_NUM_SEQ; i++) {
732                 if (user->intf->seq_table[i].inuse
733                     && (user->intf->seq_table[i].recv_msg->user == user))
734                 {
735                         user->intf->seq_table[i].inuse = 0;
736                 }
737         }
738         spin_unlock_irqrestore(&(user->intf->seq_lock), flags);
739
740         /* Remove the user from the command receiver's table. */
741         write_lock_irqsave(&(user->intf->cmd_rcvr_lock), flags);
742         list_for_each_entry_safe(rcvr, rcvr2, &(user->intf->cmd_rcvrs), link) {
743                 if (rcvr->user == user) {
744                         list_del(&rcvr->link);
745                         kfree(rcvr);
746                 }
747         }
748         write_unlock_irqrestore(&(user->intf->cmd_rcvr_lock), flags);
749
750         kfree(user);
751
752  out_unlock:
753
754         return rv;
755 }
756
757 int ipmi_destroy_user(ipmi_user_t user)
758 {
759         int           rv;
760         ipmi_smi_t    intf = user->intf;
761         unsigned long flags;
762
763         down_read(&interfaces_sem);
764         write_lock_irqsave(&intf->users_lock, flags);
765         rv = ipmi_destroy_user_nolock(user);
766         if (!rv)
767                 module_put(intf->handlers->owner);
768                 
769         write_unlock_irqrestore(&intf->users_lock, flags);
770         up_read(&interfaces_sem);
771         return rv;
772 }
773
774 void ipmi_get_version(ipmi_user_t   user,
775                       unsigned char *major,
776                       unsigned char *minor)
777 {
778         *major = user->intf->version_major;
779         *minor = user->intf->version_minor;
780 }
781
782 void ipmi_set_my_address(ipmi_user_t   user,
783                          unsigned char address)
784 {
785         user->intf->my_address = address;
786 }
787
788 unsigned char ipmi_get_my_address(ipmi_user_t user)
789 {
790         return user->intf->my_address;
791 }
792
793 void ipmi_set_my_LUN(ipmi_user_t   user,
794                      unsigned char LUN)
795 {
796         user->intf->my_lun = LUN & 0x3;
797 }
798
799 unsigned char ipmi_get_my_LUN(ipmi_user_t user)
800 {
801         return user->intf->my_lun;
802 }
803
804 int ipmi_set_gets_events(ipmi_user_t user, int val)
805 {
806         unsigned long         flags;
807         struct ipmi_recv_msg  *msg, *msg2;
808
809         read_lock(&(user->intf->users_lock));
810         spin_lock_irqsave(&(user->intf->events_lock), flags);
811         user->gets_events = val;
812
813         if (val) {
814                 /* Deliver any queued events. */
815                 list_for_each_entry_safe(msg, msg2, &(user->intf->waiting_events), link) {
816                         list_del(&msg->link);
817                         msg->user = user;
818                         deliver_response(msg);
819                 }
820         }
821         
822         spin_unlock_irqrestore(&(user->intf->events_lock), flags);
823         read_unlock(&(user->intf->users_lock));
824
825         return 0;
826 }
827
828 int ipmi_register_for_cmd(ipmi_user_t   user,
829                           unsigned char netfn,
830                           unsigned char cmd)
831 {
832         struct cmd_rcvr  *cmp;
833         unsigned long    flags;
834         struct cmd_rcvr  *rcvr;
835         int              rv = 0;
836
837
838         rcvr = kmalloc(sizeof(*rcvr), GFP_KERNEL);
839         if (! rcvr)
840                 return -ENOMEM;
841
842         read_lock(&(user->intf->users_lock));
843         write_lock_irqsave(&(user->intf->cmd_rcvr_lock), flags);
844         if (user->intf->all_cmd_rcvr != NULL) {
845                 rv = -EBUSY;
846                 goto out_unlock;
847         }
848
849         /* Make sure the command/netfn is not already registered. */
850         list_for_each_entry(cmp, &(user->intf->cmd_rcvrs), link) {
851                 if ((cmp->netfn == netfn) && (cmp->cmd == cmd)) {
852                         rv = -EBUSY;
853                         break;
854                 }
855         }
856
857         if (! rv) {
858                 rcvr->cmd = cmd;
859                 rcvr->netfn = netfn;
860                 rcvr->user = user;
861                 list_add_tail(&(rcvr->link), &(user->intf->cmd_rcvrs));
862         }
863  out_unlock:
864         write_unlock_irqrestore(&(user->intf->cmd_rcvr_lock), flags);
865         read_unlock(&(user->intf->users_lock));
866
867         if (rv)
868                 kfree(rcvr);
869
870         return rv;
871 }
872
873 int ipmi_unregister_for_cmd(ipmi_user_t   user,
874                             unsigned char netfn,
875                             unsigned char cmd)
876 {
877         unsigned long    flags;
878         struct cmd_rcvr  *rcvr;
879         int              rv = -ENOENT;
880
881         read_lock(&(user->intf->users_lock));
882         write_lock_irqsave(&(user->intf->cmd_rcvr_lock), flags);
883         /* Make sure the command/netfn is not already registered. */
884         list_for_each_entry(rcvr, &(user->intf->cmd_rcvrs), link) {
885                 if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)) {
886                         rv = 0;
887                         list_del(&rcvr->link);
888                         kfree(rcvr);
889                         break;
890                 }
891         }
892         write_unlock_irqrestore(&(user->intf->cmd_rcvr_lock), flags);
893         read_unlock(&(user->intf->users_lock));
894
895         return rv;
896 }
897
898 static unsigned char
899 ipmb_checksum(unsigned char *data, int size)
900 {
901         unsigned char csum = 0;
902         
903         for (; size > 0; size--, data++)
904                 csum += *data;
905
906         return -csum;
907 }
908
909 static inline void format_ipmb_msg(struct ipmi_smi_msg   *smi_msg,
910                                    struct kernel_ipmi_msg *msg,
911                                    struct ipmi_ipmb_addr *ipmb_addr,
912                                    long                  msgid,
913                                    unsigned char         ipmb_seq,
914                                    int                   broadcast,
915                                    unsigned char         source_address,
916                                    unsigned char         source_lun)
917 {
918         int i = broadcast;
919
920         /* Format the IPMB header data. */
921         smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
922         smi_msg->data[1] = IPMI_SEND_MSG_CMD;
923         smi_msg->data[2] = ipmb_addr->channel;
924         if (broadcast)
925                 smi_msg->data[3] = 0;
926         smi_msg->data[i+3] = ipmb_addr->slave_addr;
927         smi_msg->data[i+4] = (msg->netfn << 2) | (ipmb_addr->lun & 0x3);
928         smi_msg->data[i+5] = ipmb_checksum(&(smi_msg->data[i+3]), 2);
929         smi_msg->data[i+6] = source_address;
930         smi_msg->data[i+7] = (ipmb_seq << 2) | source_lun;
931         smi_msg->data[i+8] = msg->cmd;
932
933         /* Now tack on the data to the message. */
934         if (msg->data_len > 0)
935                 memcpy(&(smi_msg->data[i+9]), msg->data,
936                        msg->data_len);
937         smi_msg->data_size = msg->data_len + 9;
938
939         /* Now calculate the checksum and tack it on. */
940         smi_msg->data[i+smi_msg->data_size]
941                 = ipmb_checksum(&(smi_msg->data[i+6]),
942                                 smi_msg->data_size-6);
943
944         /* Add on the checksum size and the offset from the
945            broadcast. */
946         smi_msg->data_size += 1 + i;
947
948         smi_msg->msgid = msgid;
949 }
950
951 static inline void format_lan_msg(struct ipmi_smi_msg   *smi_msg,
952                                   struct kernel_ipmi_msg *msg,
953                                   struct ipmi_lan_addr  *lan_addr,
954                                   long                  msgid,
955                                   unsigned char         ipmb_seq,
956                                   unsigned char         source_lun)
957 {
958         /* Format the IPMB header data. */
959         smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
960         smi_msg->data[1] = IPMI_SEND_MSG_CMD;
961         smi_msg->data[2] = lan_addr->channel;
962         smi_msg->data[3] = lan_addr->session_handle;
963         smi_msg->data[4] = lan_addr->remote_SWID;
964         smi_msg->data[5] = (msg->netfn << 2) | (lan_addr->lun & 0x3);
965         smi_msg->data[6] = ipmb_checksum(&(smi_msg->data[4]), 2);
966         smi_msg->data[7] = lan_addr->local_SWID;
967         smi_msg->data[8] = (ipmb_seq << 2) | source_lun;
968         smi_msg->data[9] = msg->cmd;
969
970         /* Now tack on the data to the message. */
971         if (msg->data_len > 0)
972                 memcpy(&(smi_msg->data[10]), msg->data,
973                        msg->data_len);
974         smi_msg->data_size = msg->data_len + 10;
975
976         /* Now calculate the checksum and tack it on. */
977         smi_msg->data[smi_msg->data_size]
978                 = ipmb_checksum(&(smi_msg->data[7]),
979                                 smi_msg->data_size-7);
980
981         /* Add on the checksum size and the offset from the
982            broadcast. */
983         smi_msg->data_size += 1;
984
985         smi_msg->msgid = msgid;
986 }
987
988 /* Separate from ipmi_request so that the user does not have to be
989    supplied in certain circumstances (mainly at panic time).  If
990    messages are supplied, they will be freed, even if an error
991    occurs. */
992 static inline int i_ipmi_request(ipmi_user_t          user,
993                                  ipmi_smi_t           intf,
994                                  struct ipmi_addr     *addr,
995                                  long                 msgid,
996                                  struct kernel_ipmi_msg *msg,
997                                  void                 *user_msg_data,
998                                  void                 *supplied_smi,
999                                  struct ipmi_recv_msg *supplied_recv,
1000                                  int                  priority,
1001                                  unsigned char        source_address,
1002                                  unsigned char        source_lun,
1003                                  int                  retries,
1004                                  unsigned int         retry_time_ms)
1005 {
1006         int                  rv = 0;
1007         struct ipmi_smi_msg  *smi_msg;
1008         struct ipmi_recv_msg *recv_msg;
1009         unsigned long        flags;
1010
1011
1012         if (supplied_recv) {
1013                 recv_msg = supplied_recv;
1014         } else {
1015                 recv_msg = ipmi_alloc_recv_msg();
1016                 if (recv_msg == NULL) {
1017                         return -ENOMEM;
1018                 }
1019         }
1020         recv_msg->user_msg_data = user_msg_data;
1021
1022         if (supplied_smi) {
1023                 smi_msg = (struct ipmi_smi_msg *) supplied_smi;
1024         } else {
1025                 smi_msg = ipmi_alloc_smi_msg();
1026                 if (smi_msg == NULL) {
1027                         ipmi_free_recv_msg(recv_msg);
1028                         return -ENOMEM;
1029                 }
1030         }
1031
1032         recv_msg->user = user;
1033         recv_msg->msgid = msgid;
1034         /* Store the message to send in the receive message so timeout
1035            responses can get the proper response data. */
1036         recv_msg->msg = *msg;
1037
1038         if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
1039                 struct ipmi_system_interface_addr *smi_addr;
1040
1041                 if (msg->netfn & 1) {
1042                         /* Responses are not allowed to the SMI. */
1043                         rv = -EINVAL;
1044                         goto out_err;
1045                 }
1046
1047                 smi_addr = (struct ipmi_system_interface_addr *) addr;
1048                 if (smi_addr->lun > 3) {
1049                         spin_lock_irqsave(&intf->counter_lock, flags);
1050                         intf->sent_invalid_commands++;
1051                         spin_unlock_irqrestore(&intf->counter_lock, flags);
1052                         rv = -EINVAL;
1053                         goto out_err;
1054                 }
1055
1056                 memcpy(&recv_msg->addr, smi_addr, sizeof(*smi_addr));
1057
1058                 if ((msg->netfn == IPMI_NETFN_APP_REQUEST)
1059                     && ((msg->cmd == IPMI_SEND_MSG_CMD)
1060                         || (msg->cmd == IPMI_GET_MSG_CMD)
1061                         || (msg->cmd == IPMI_READ_EVENT_MSG_BUFFER_CMD)))
1062                 {
1063                         /* We don't let the user do these, since we manage
1064                            the sequence numbers. */
1065                         spin_lock_irqsave(&intf->counter_lock, flags);
1066                         intf->sent_invalid_commands++;
1067                         spin_unlock_irqrestore(&intf->counter_lock, flags);
1068                         rv = -EINVAL;
1069                         goto out_err;
1070                 }
1071
1072                 if ((msg->data_len + 2) > IPMI_MAX_MSG_LENGTH) {
1073                         spin_lock_irqsave(&intf->counter_lock, flags);
1074                         intf->sent_invalid_commands++;
1075                         spin_unlock_irqrestore(&intf->counter_lock, flags);
1076                         rv = -EMSGSIZE;
1077                         goto out_err;
1078                 }
1079
1080                 smi_msg->data[0] = (msg->netfn << 2) | (smi_addr->lun & 0x3);
1081                 smi_msg->data[1] = msg->cmd;
1082                 smi_msg->msgid = msgid;
1083                 smi_msg->user_data = recv_msg;
1084                 if (msg->data_len > 0)
1085                         memcpy(&(smi_msg->data[2]), msg->data, msg->data_len);
1086                 smi_msg->data_size = msg->data_len + 2;
1087                 spin_lock_irqsave(&intf->counter_lock, flags);
1088                 intf->sent_local_commands++;
1089                 spin_unlock_irqrestore(&intf->counter_lock, flags);
1090         } else if ((addr->addr_type == IPMI_IPMB_ADDR_TYPE)
1091                    || (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE))
1092         {
1093                 struct ipmi_ipmb_addr *ipmb_addr;
1094                 unsigned char         ipmb_seq;
1095                 long                  seqid;
1096                 int                   broadcast = 0;
1097
1098                 if (addr->channel > IPMI_NUM_CHANNELS) {
1099                         spin_lock_irqsave(&intf->counter_lock, flags);
1100                         intf->sent_invalid_commands++;
1101                         spin_unlock_irqrestore(&intf->counter_lock, flags);
1102                         rv = -EINVAL;
1103                         goto out_err;
1104                 }
1105
1106                 if (intf->channels[addr->channel].medium
1107                     != IPMI_CHANNEL_MEDIUM_IPMB)
1108                 {
1109                         spin_lock_irqsave(&intf->counter_lock, flags);
1110                         intf->sent_invalid_commands++;
1111                         spin_unlock_irqrestore(&intf->counter_lock, flags);
1112                         rv = -EINVAL;
1113                         goto out_err;
1114                 }
1115
1116                 if (retries < 0) {
1117                     if (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE)
1118                         retries = 0; /* Don't retry broadcasts. */
1119                     else
1120                         retries = 4;
1121                 }
1122                 if (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE) {
1123                     /* Broadcasts add a zero at the beginning of the
1124                        message, but otherwise is the same as an IPMB
1125                        address. */
1126                     addr->addr_type = IPMI_IPMB_ADDR_TYPE;
1127                     broadcast = 1;
1128                 }
1129
1130
1131                 /* Default to 1 second retries. */
1132                 if (retry_time_ms == 0)
1133                     retry_time_ms = 1000;
1134
1135                 /* 9 for the header and 1 for the checksum, plus
1136                    possibly one for the broadcast. */
1137                 if ((msg->data_len + 10 + broadcast) > IPMI_MAX_MSG_LENGTH) {
1138                         spin_lock_irqsave(&intf->counter_lock, flags);
1139                         intf->sent_invalid_commands++;
1140                         spin_unlock_irqrestore(&intf->counter_lock, flags);
1141                         rv = -EMSGSIZE;
1142                         goto out_err;
1143                 }
1144
1145                 ipmb_addr = (struct ipmi_ipmb_addr *) addr;
1146                 if (ipmb_addr->lun > 3) {
1147                         spin_lock_irqsave(&intf->counter_lock, flags);
1148                         intf->sent_invalid_commands++;
1149                         spin_unlock_irqrestore(&intf->counter_lock, flags);
1150                         rv = -EINVAL;
1151                         goto out_err;
1152                 }
1153
1154                 memcpy(&recv_msg->addr, ipmb_addr, sizeof(*ipmb_addr));
1155
1156                 if (recv_msg->msg.netfn & 0x1) {
1157                         /* It's a response, so use the user's sequence
1158                            from msgid. */
1159                         spin_lock_irqsave(&intf->counter_lock, flags);
1160                         intf->sent_ipmb_responses++;
1161                         spin_unlock_irqrestore(&intf->counter_lock, flags);
1162                         format_ipmb_msg(smi_msg, msg, ipmb_addr, msgid,
1163                                         msgid, broadcast,
1164                                         source_address, source_lun);
1165
1166                         /* Save the receive message so we can use it
1167                            to deliver the response. */
1168                         smi_msg->user_data = recv_msg;
1169                 } else {
1170                         /* It's a command, so get a sequence for it. */
1171
1172                         spin_lock_irqsave(&(intf->seq_lock), flags);
1173
1174                         spin_lock(&intf->counter_lock);
1175                         intf->sent_ipmb_commands++;
1176                         spin_unlock(&intf->counter_lock);
1177
1178                         /* Create a sequence number with a 1 second
1179                            timeout and 4 retries. */
1180                         rv = intf_next_seq(intf,
1181                                            recv_msg,
1182                                            retry_time_ms,
1183                                            retries,
1184                                            broadcast,
1185                                            &ipmb_seq,
1186                                            &seqid);
1187                         if (rv) {
1188                                 /* We have used up all the sequence numbers,
1189                                    probably, so abort. */
1190                                 spin_unlock_irqrestore(&(intf->seq_lock),
1191                                                        flags);
1192                                 goto out_err;
1193                         }
1194
1195                         /* Store the sequence number in the message,
1196                            so that when the send message response
1197                            comes back we can start the timer. */
1198                         format_ipmb_msg(smi_msg, msg, ipmb_addr,
1199                                         STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
1200                                         ipmb_seq, broadcast,
1201                                         source_address, source_lun);
1202
1203                         /* Copy the message into the recv message data, so we
1204                            can retransmit it later if necessary. */
1205                         memcpy(recv_msg->msg_data, smi_msg->data,
1206                                smi_msg->data_size);
1207                         recv_msg->msg.data = recv_msg->msg_data;
1208                         recv_msg->msg.data_len = smi_msg->data_size;
1209
1210                         /* We don't unlock until here, because we need
1211                            to copy the completed message into the
1212                            recv_msg before we release the lock.
1213                            Otherwise, race conditions may bite us.  I
1214                            know that's pretty paranoid, but I prefer
1215                            to be correct. */
1216                         spin_unlock_irqrestore(&(intf->seq_lock), flags);
1217                 }
1218         } else if (addr->addr_type == IPMI_LAN_ADDR_TYPE) {
1219                 struct ipmi_lan_addr  *lan_addr;
1220                 unsigned char         ipmb_seq;
1221                 long                  seqid;
1222
1223                 if (addr->channel > IPMI_NUM_CHANNELS) {
1224                         spin_lock_irqsave(&intf->counter_lock, flags);
1225                         intf->sent_invalid_commands++;
1226                         spin_unlock_irqrestore(&intf->counter_lock, flags);
1227                         rv = -EINVAL;
1228                         goto out_err;
1229                 }
1230
1231                 if ((intf->channels[addr->channel].medium
1232                     != IPMI_CHANNEL_MEDIUM_8023LAN)
1233                     && (intf->channels[addr->channel].medium
1234                         != IPMI_CHANNEL_MEDIUM_ASYNC))
1235                 {
1236                         spin_lock_irqsave(&intf->counter_lock, flags);
1237                         intf->sent_invalid_commands++;
1238                         spin_unlock_irqrestore(&intf->counter_lock, flags);
1239                         rv = -EINVAL;
1240                         goto out_err;
1241                 }
1242
1243                 retries = 4;
1244
1245                 /* Default to 1 second retries. */
1246                 if (retry_time_ms == 0)
1247                     retry_time_ms = 1000;
1248
1249                 /* 11 for the header and 1 for the checksum. */
1250                 if ((msg->data_len + 12) > IPMI_MAX_MSG_LENGTH) {
1251                         spin_lock_irqsave(&intf->counter_lock, flags);
1252                         intf->sent_invalid_commands++;
1253                         spin_unlock_irqrestore(&intf->counter_lock, flags);
1254                         rv = -EMSGSIZE;
1255                         goto out_err;
1256                 }
1257
1258                 lan_addr = (struct ipmi_lan_addr *) addr;
1259                 if (lan_addr->lun > 3) {
1260                         spin_lock_irqsave(&intf->counter_lock, flags);
1261                         intf->sent_invalid_commands++;
1262                         spin_unlock_irqrestore(&intf->counter_lock, flags);
1263                         rv = -EINVAL;
1264                         goto out_err;
1265                 }
1266
1267                 memcpy(&recv_msg->addr, lan_addr, sizeof(*lan_addr));
1268
1269                 if (recv_msg->msg.netfn & 0x1) {
1270                         /* It's a response, so use the user's sequence
1271                            from msgid. */
1272                         spin_lock_irqsave(&intf->counter_lock, flags);
1273                         intf->sent_lan_responses++;
1274                         spin_unlock_irqrestore(&intf->counter_lock, flags);
1275                         format_lan_msg(smi_msg, msg, lan_addr, msgid,
1276                                        msgid, source_lun);
1277
1278                         /* Save the receive message so we can use it
1279                            to deliver the response. */
1280                         smi_msg->user_data = recv_msg;
1281                 } else {
1282                         /* It's a command, so get a sequence for it. */
1283
1284                         spin_lock_irqsave(&(intf->seq_lock), flags);
1285
1286                         spin_lock(&intf->counter_lock);
1287                         intf->sent_lan_commands++;
1288                         spin_unlock(&intf->counter_lock);
1289
1290                         /* Create a sequence number with a 1 second
1291                            timeout and 4 retries. */
1292                         rv = intf_next_seq(intf,
1293                                            recv_msg,
1294                                            retry_time_ms,
1295                                            retries,
1296                                            0,
1297                                            &ipmb_seq,
1298                                            &seqid);
1299                         if (rv) {
1300                                 /* We have used up all the sequence numbers,
1301                                    probably, so abort. */
1302                                 spin_unlock_irqrestore(&(intf->seq_lock),
1303                                                        flags);
1304                                 goto out_err;
1305                         }
1306
1307                         /* Store the sequence number in the message,
1308                            so that when the send message response
1309                            comes back we can start the timer. */
1310                         format_lan_msg(smi_msg, msg, lan_addr,
1311                                        STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
1312                                        ipmb_seq, source_lun);
1313
1314                         /* Copy the message into the recv message data, so we
1315                            can retransmit it later if necessary. */
1316                         memcpy(recv_msg->msg_data, smi_msg->data,
1317                                smi_msg->data_size);
1318                         recv_msg->msg.data = recv_msg->msg_data;
1319                         recv_msg->msg.data_len = smi_msg->data_size;
1320
1321                         /* We don't unlock until here, because we need
1322                            to copy the completed message into the
1323                            recv_msg before we release the lock.
1324                            Otherwise, race conditions may bite us.  I
1325                            know that's pretty paranoid, but I prefer
1326                            to be correct. */
1327                         spin_unlock_irqrestore(&(intf->seq_lock), flags);
1328                 }
1329         } else {
1330             /* Unknown address type. */
1331                 spin_lock_irqsave(&intf->counter_lock, flags);
1332                 intf->sent_invalid_commands++;
1333                 spin_unlock_irqrestore(&intf->counter_lock, flags);
1334                 rv = -EINVAL;
1335                 goto out_err;
1336         }
1337
1338 #ifdef DEBUG_MSGING
1339         {
1340                 int m;
1341                 for (m=0; m<smi_msg->data_size; m++)
1342                         printk(" %2.2x", smi_msg->data[m]);
1343                 printk("\n");
1344         }
1345 #endif
1346         intf->handlers->sender(intf->send_info, smi_msg, priority);
1347
1348         return 0;
1349
1350  out_err:
1351         ipmi_free_smi_msg(smi_msg);
1352         ipmi_free_recv_msg(recv_msg);
1353         return rv;
1354 }
1355
1356 int ipmi_request(ipmi_user_t      user,
1357                  struct ipmi_addr *addr,
1358                  long             msgid,
1359                  struct kernel_ipmi_msg  *msg,
1360                  void             *user_msg_data,
1361                  int              priority)
1362 {
1363         return i_ipmi_request(user,
1364                               user->intf,
1365                               addr,
1366                               msgid,
1367                               msg,
1368                               user_msg_data,
1369                               NULL, NULL,
1370                               priority,
1371                               user->intf->my_address,
1372                               user->intf->my_lun,
1373                               -1, 0);
1374 }
1375
1376 int ipmi_request_settime(ipmi_user_t      user,
1377                          struct ipmi_addr *addr,
1378                          long             msgid,
1379                          struct kernel_ipmi_msg  *msg,
1380                          void             *user_msg_data,
1381                          int              priority,
1382                          int              retries,
1383                          unsigned int     retry_time_ms)
1384 {
1385         return i_ipmi_request(user,
1386                               user->intf,
1387                               addr,
1388                               msgid,
1389                               msg,
1390                               user_msg_data,
1391                               NULL, NULL,
1392                               priority,
1393                               user->intf->my_address,
1394                               user->intf->my_lun,
1395                               retries,
1396                               retry_time_ms);
1397 }
1398
1399 int ipmi_request_supply_msgs(ipmi_user_t          user,
1400                              struct ipmi_addr     *addr,
1401                              long                 msgid,
1402                              struct kernel_ipmi_msg *msg,
1403                              void                 *user_msg_data,
1404                              void                 *supplied_smi,
1405                              struct ipmi_recv_msg *supplied_recv,
1406                              int                  priority)
1407 {
1408         return i_ipmi_request(user,
1409                               user->intf,
1410                               addr,
1411                               msgid,
1412                               msg,
1413                               user_msg_data,
1414                               supplied_smi,
1415                               supplied_recv,
1416                               priority,
1417                               user->intf->my_address,
1418                               user->intf->my_lun,
1419                               -1, 0);
1420 }
1421
1422 int ipmi_request_with_source(ipmi_user_t      user,
1423                              struct ipmi_addr *addr,
1424                              long             msgid,
1425                              struct kernel_ipmi_msg  *msg,
1426                              void             *user_msg_data,
1427                              int              priority,
1428                              unsigned char    source_address,
1429                              unsigned char    source_lun)
1430 {
1431         return i_ipmi_request(user,
1432                               user->intf,
1433                               addr,
1434                               msgid,
1435                               msg,
1436                               user_msg_data,
1437                               NULL, NULL,
1438                               priority,
1439                               source_address,
1440                               source_lun,
1441                               -1, 0);
1442 }
1443
1444 static int ipmb_file_read_proc(char *page, char **start, off_t off,
1445                                int count, int *eof, void *data)
1446 {
1447         char       *out = (char *) page;
1448         ipmi_smi_t intf = data;
1449
1450         return sprintf(out, "%x\n", intf->my_address);
1451 }
1452
1453 static int version_file_read_proc(char *page, char **start, off_t off,
1454                                   int count, int *eof, void *data)
1455 {
1456         char       *out = (char *) page;
1457         ipmi_smi_t intf = data;
1458
1459         return sprintf(out, "%d.%d\n",
1460                        intf->version_major, intf->version_minor);
1461 }
1462
1463 static int stat_file_read_proc(char *page, char **start, off_t off,
1464                                int count, int *eof, void *data)
1465 {
1466         char       *out = (char *) page;
1467         ipmi_smi_t intf = data;
1468
1469         out += sprintf(out, "sent_invalid_commands:       %d\n",
1470                        intf->sent_invalid_commands);
1471         out += sprintf(out, "sent_local_commands:         %d\n",
1472                        intf->sent_local_commands);
1473         out += sprintf(out, "handled_local_responses:     %d\n",
1474                        intf->handled_local_responses);
1475         out += sprintf(out, "unhandled_local_responses:   %d\n",
1476                        intf->unhandled_local_responses);
1477         out += sprintf(out, "sent_ipmb_commands:          %d\n",
1478                        intf->sent_ipmb_commands);
1479         out += sprintf(out, "sent_ipmb_command_errs:      %d\n",
1480                        intf->sent_ipmb_command_errs);
1481         out += sprintf(out, "retransmitted_ipmb_commands: %d\n",
1482                        intf->retransmitted_ipmb_commands);
1483         out += sprintf(out, "timed_out_ipmb_commands:     %d\n",
1484                        intf->timed_out_ipmb_commands);
1485         out += sprintf(out, "timed_out_ipmb_broadcasts:   %d\n",
1486                        intf->timed_out_ipmb_broadcasts);
1487         out += sprintf(out, "sent_ipmb_responses:         %d\n",
1488                        intf->sent_ipmb_responses);
1489         out += sprintf(out, "handled_ipmb_responses:      %d\n",
1490                        intf->handled_ipmb_responses);
1491         out += sprintf(out, "invalid_ipmb_responses:      %d\n",
1492                        intf->invalid_ipmb_responses);
1493         out += sprintf(out, "unhandled_ipmb_responses:    %d\n",
1494                        intf->unhandled_ipmb_responses);
1495         out += sprintf(out, "sent_lan_commands:           %d\n",
1496                        intf->sent_lan_commands);
1497         out += sprintf(out, "sent_lan_command_errs:       %d\n",
1498                        intf->sent_lan_command_errs);
1499         out += sprintf(out, "retransmitted_lan_commands:  %d\n",
1500                        intf->retransmitted_lan_commands);
1501         out += sprintf(out, "timed_out_lan_commands:      %d\n",
1502                        intf->timed_out_lan_commands);
1503         out += sprintf(out, "sent_lan_responses:          %d\n",
1504                        intf->sent_lan_responses);
1505         out += sprintf(out, "handled_lan_responses:       %d\n",
1506                        intf->handled_lan_responses);
1507         out += sprintf(out, "invalid_lan_responses:       %d\n",
1508                        intf->invalid_lan_responses);
1509         out += sprintf(out, "unhandled_lan_responses:     %d\n",
1510                        intf->unhandled_lan_responses);
1511         out += sprintf(out, "handled_commands:            %d\n",
1512                        intf->handled_commands);
1513         out += sprintf(out, "invalid_commands:            %d\n",
1514                        intf->invalid_commands);
1515         out += sprintf(out, "unhandled_commands:          %d\n",
1516                        intf->unhandled_commands);
1517         out += sprintf(out, "invalid_events:              %d\n",
1518                        intf->invalid_events);
1519         out += sprintf(out, "events:                      %d\n",
1520                        intf->events);
1521
1522         return (out - ((char *) page));
1523 }
1524
1525 int ipmi_smi_add_proc_entry(ipmi_smi_t smi, char *name,
1526                             read_proc_t *read_proc, write_proc_t *write_proc,
1527                             void *data, struct module *owner)
1528 {
1529         struct proc_dir_entry  *file;
1530         int                    rv = 0;
1531         struct ipmi_proc_entry *entry;
1532
1533         /* Create a list element. */
1534         entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1535         if (!entry)
1536                 return -ENOMEM;
1537         entry->name = kmalloc(strlen(name)+1, GFP_KERNEL);
1538         if (!entry->name) {
1539                 kfree(entry);
1540                 return -ENOMEM;
1541         }
1542         strcpy(entry->name, name);
1543
1544         file = create_proc_entry(name, 0, smi->proc_dir);
1545         if (!file) {
1546                 kfree(entry->name);
1547                 kfree(entry);
1548                 rv = -ENOMEM;
1549         } else {
1550                 file->nlink = 1;
1551                 file->data = data;
1552                 file->read_proc = read_proc;
1553                 file->write_proc = write_proc;
1554                 file->owner = owner;
1555
1556                 /* Stick it on the list. */
1557                 entry->next = smi->proc_entries;
1558                 smi->proc_entries = entry;
1559         }
1560
1561         return rv;
1562 }
1563
1564 static int add_proc_entries(ipmi_smi_t smi, int num)
1565 {
1566         int rv = 0;
1567
1568         sprintf(smi->proc_dir_name, "%d", num);
1569         smi->proc_dir = proc_mkdir(smi->proc_dir_name, proc_ipmi_root);
1570         if (!smi->proc_dir)
1571                 rv = -ENOMEM;
1572         else {
1573                 smi->proc_dir->owner = THIS_MODULE;
1574         }
1575
1576         if (rv == 0)
1577                 rv = ipmi_smi_add_proc_entry(smi, "stats",
1578                                              stat_file_read_proc, NULL,
1579                                              smi, THIS_MODULE);
1580
1581         if (rv == 0)
1582                 rv = ipmi_smi_add_proc_entry(smi, "ipmb",
1583                                              ipmb_file_read_proc, NULL,
1584                                              smi, THIS_MODULE);
1585
1586         if (rv == 0)
1587                 rv = ipmi_smi_add_proc_entry(smi, "version",
1588                                              version_file_read_proc, NULL,
1589                                              smi, THIS_MODULE);
1590
1591         return rv;
1592 }
1593
1594 static void remove_proc_entries(ipmi_smi_t smi)
1595 {
1596         struct ipmi_proc_entry *entry;
1597
1598         while (smi->proc_entries) {
1599                 entry = smi->proc_entries;
1600                 smi->proc_entries = entry->next;
1601
1602                 remove_proc_entry(entry->name, smi->proc_dir);
1603                 kfree(entry->name);
1604                 kfree(entry);
1605         }
1606         remove_proc_entry(smi->proc_dir_name, proc_ipmi_root);
1607 }
1608
1609 static int
1610 send_channel_info_cmd(ipmi_smi_t intf, int chan)
1611 {
1612         struct kernel_ipmi_msg            msg;
1613         unsigned char                     data[1];
1614         struct ipmi_system_interface_addr si;
1615
1616         si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
1617         si.channel = IPMI_BMC_CHANNEL;
1618         si.lun = 0;
1619
1620         msg.netfn = IPMI_NETFN_APP_REQUEST;
1621         msg.cmd = IPMI_GET_CHANNEL_INFO_CMD;
1622         msg.data = data;
1623         msg.data_len = 1;
1624         data[0] = chan;
1625         return i_ipmi_request(NULL,
1626                               intf,
1627                               (struct ipmi_addr *) &si,
1628                               0,
1629                               &msg,
1630                               NULL,
1631                               NULL,
1632                               NULL,
1633                               0,
1634                               intf->my_address,
1635                               intf->my_lun,
1636                               -1, 0);
1637 }
1638
1639 static void
1640 channel_handler(ipmi_smi_t intf, struct ipmi_smi_msg *msg)
1641 {
1642         int rv = 0;
1643         int chan;
1644
1645         if ((msg->rsp[0] == (IPMI_NETFN_APP_RESPONSE << 2))
1646             && (msg->rsp[1] == IPMI_GET_CHANNEL_INFO_CMD))
1647         {
1648                 /* It's the one we want */
1649                 if (msg->rsp[2] != 0) {
1650                         /* Got an error from the channel, just go on. */
1651
1652                         if (msg->rsp[2] == IPMI_INVALID_COMMAND_ERR) {
1653                                 /* If the MC does not support this
1654                                    command, that is legal.  We just
1655                                    assume it has one IPMB at channel
1656                                    zero. */
1657                                 intf->channels[0].medium
1658                                         = IPMI_CHANNEL_MEDIUM_IPMB;
1659                                 intf->channels[0].protocol
1660                                         = IPMI_CHANNEL_PROTOCOL_IPMB;
1661                                 rv = -ENOSYS;
1662
1663                                 intf->curr_channel = IPMI_MAX_CHANNELS;
1664                                 wake_up(&intf->waitq);
1665                                 goto out;
1666                         }
1667                         goto next_channel;
1668                 }
1669                 if (msg->rsp_size < 6) {
1670                         /* Message not big enough, just go on. */
1671                         goto next_channel;
1672                 }
1673                 chan = intf->curr_channel;
1674                 intf->channels[chan].medium = msg->rsp[4] & 0x7f;
1675                 intf->channels[chan].protocol = msg->rsp[5] & 0x1f;
1676
1677         next_channel:
1678                 intf->curr_channel++;
1679                 if (intf->curr_channel >= IPMI_MAX_CHANNELS)
1680                         wake_up(&intf->waitq);
1681                 else
1682                         rv = send_channel_info_cmd(intf, intf->curr_channel);
1683
1684                 if (rv) {
1685                         /* Got an error somehow, just give up. */
1686                         intf->curr_channel = IPMI_MAX_CHANNELS;
1687                         wake_up(&intf->waitq);
1688
1689                         printk(KERN_WARNING "ipmi_msghandler: Error sending"
1690                                "channel information: %d\n",
1691                                rv);
1692                 }
1693         }
1694  out:
1695         return;
1696 }
1697
1698 void ipmi_poll_interface(ipmi_user_t user)
1699 {
1700         ipmi_smi_t intf = user->intf;
1701
1702         if (intf->handlers->poll)
1703                 intf->handlers->poll(intf->send_info);
1704 }
1705
1706 int ipmi_register_smi(struct ipmi_smi_handlers *handlers,
1707                       void                     *send_info,
1708                       unsigned char            version_major,
1709                       unsigned char            version_minor,
1710                       ipmi_smi_t               *intf)
1711 {
1712         int              i, j;
1713         int              rv;
1714         ipmi_smi_t       new_intf;
1715         unsigned long    flags;
1716
1717
1718         /* Make sure the driver is actually initialized, this handles
1719            problems with initialization order. */
1720         if (!initialized) {
1721                 rv = ipmi_init_msghandler();
1722                 if (rv)
1723                         return rv;
1724                 /* The init code doesn't return an error if it was turned
1725                    off, but it won't initialize.  Check that. */
1726                 if (!initialized)
1727                         return -ENODEV;
1728         }
1729
1730         new_intf = kmalloc(sizeof(*new_intf), GFP_KERNEL);
1731         if (!new_intf)
1732                 return -ENOMEM;
1733         memset(new_intf, 0, sizeof(*new_intf));
1734
1735         new_intf->proc_dir = NULL;
1736
1737         rv = -ENOMEM;
1738
1739         down_write(&interfaces_sem);
1740         for (i=0; i<MAX_IPMI_INTERFACES; i++) {
1741                 if (ipmi_interfaces[i] == NULL) {
1742                         new_intf->intf_num = i;
1743                         new_intf->version_major = version_major;
1744                         new_intf->version_minor = version_minor;
1745                         new_intf->my_address = IPMI_BMC_SLAVE_ADDR;
1746                         new_intf->my_lun = 2;  /* the SMS LUN. */
1747                         rwlock_init(&(new_intf->users_lock));
1748                         INIT_LIST_HEAD(&(new_intf->users));
1749                         new_intf->handlers = handlers;
1750                         new_intf->send_info = send_info;
1751                         spin_lock_init(&(new_intf->seq_lock));
1752                         for (j=0; j<IPMI_IPMB_NUM_SEQ; j++) {
1753                                 new_intf->seq_table[j].inuse = 0;
1754                                 new_intf->seq_table[j].seqid = 0;
1755                         }
1756                         new_intf->curr_seq = 0;
1757                         spin_lock_init(&(new_intf->waiting_msgs_lock));
1758                         INIT_LIST_HEAD(&(new_intf->waiting_msgs));
1759                         spin_lock_init(&(new_intf->events_lock));
1760                         INIT_LIST_HEAD(&(new_intf->waiting_events));
1761                         new_intf->waiting_events_count = 0;
1762                         rwlock_init(&(new_intf->cmd_rcvr_lock));
1763                         init_waitqueue_head(&new_intf->waitq);
1764                         INIT_LIST_HEAD(&(new_intf->cmd_rcvrs));
1765                         new_intf->all_cmd_rcvr = NULL;
1766
1767                         spin_lock_init(&(new_intf->counter_lock));
1768
1769                         spin_lock_irqsave(&interfaces_lock, flags);
1770                         ipmi_interfaces[i] = new_intf;
1771                         spin_unlock_irqrestore(&interfaces_lock, flags);
1772
1773                         rv = 0;
1774                         *intf = new_intf;
1775                         break;
1776                 }
1777         }
1778
1779         downgrade_write(&interfaces_sem);
1780
1781         if (rv == 0)
1782                 rv = add_proc_entries(*intf, i);
1783
1784         if (rv == 0) {
1785                 if ((version_major > 1)
1786                     || ((version_major == 1) && (version_minor >= 5)))
1787                 {
1788                         /* Start scanning the channels to see what is
1789                            available. */
1790                         (*intf)->null_user_handler = channel_handler;
1791                         (*intf)->curr_channel = 0;
1792                         rv = send_channel_info_cmd(*intf, 0);
1793                         if (rv)
1794                                 goto out;
1795
1796                         /* Wait for the channel info to be read. */
1797                         up_read(&interfaces_sem);
1798                         wait_event((*intf)->waitq,
1799                                    ((*intf)->curr_channel>=IPMI_MAX_CHANNELS));
1800                         down_read(&interfaces_sem);
1801
1802                         if (ipmi_interfaces[i] != new_intf)
1803                                 /* Well, it went away.  Just return. */
1804                                 goto out;
1805                 } else {
1806                         /* Assume a single IPMB channel at zero. */
1807                         (*intf)->channels[0].medium = IPMI_CHANNEL_MEDIUM_IPMB;
1808                         (*intf)->channels[0].protocol
1809                                 = IPMI_CHANNEL_PROTOCOL_IPMB;
1810                 }
1811
1812                 /* Call all the watcher interfaces to tell
1813                    them that a new interface is available. */
1814                 call_smi_watchers(i);
1815         }
1816
1817  out:
1818         up_read(&interfaces_sem);
1819
1820         if (rv) {
1821                 if (new_intf->proc_dir)
1822                         remove_proc_entries(new_intf);
1823                 kfree(new_intf);
1824         }
1825
1826         return rv;
1827 }
1828
1829 static void free_recv_msg_list(struct list_head *q)
1830 {
1831         struct ipmi_recv_msg *msg, *msg2;
1832
1833         list_for_each_entry_safe(msg, msg2, q, link) {
1834                 list_del(&msg->link);
1835                 ipmi_free_recv_msg(msg);
1836         }
1837 }
1838
1839 static void free_cmd_rcvr_list(struct list_head *q)
1840 {
1841         struct cmd_rcvr  *rcvr, *rcvr2;
1842
1843         list_for_each_entry_safe(rcvr, rcvr2, q, link) {
1844                 list_del(&rcvr->link);
1845                 kfree(rcvr);
1846         }
1847 }
1848
1849 static void clean_up_interface_data(ipmi_smi_t intf)
1850 {
1851         int i;
1852
1853         free_recv_msg_list(&(intf->waiting_msgs));
1854         free_recv_msg_list(&(intf->waiting_events));
1855         free_cmd_rcvr_list(&(intf->cmd_rcvrs));
1856
1857         for (i=0; i<IPMI_IPMB_NUM_SEQ; i++) {
1858                 if ((intf->seq_table[i].inuse)
1859                     && (intf->seq_table[i].recv_msg))
1860                 {
1861                         ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
1862                 }       
1863         }
1864 }
1865
1866 int ipmi_unregister_smi(ipmi_smi_t intf)
1867 {
1868         int                     rv = -ENODEV;
1869         int                     i;
1870         struct ipmi_smi_watcher *w;
1871         unsigned long           flags;
1872
1873         down_write(&interfaces_sem);
1874         if (list_empty(&(intf->users)))
1875         {
1876                 for (i=0; i<MAX_IPMI_INTERFACES; i++) {
1877                         if (ipmi_interfaces[i] == intf) {
1878                                 remove_proc_entries(intf);
1879                                 spin_lock_irqsave(&interfaces_lock, flags);
1880                                 ipmi_interfaces[i] = NULL;
1881                                 clean_up_interface_data(intf);
1882                                 spin_unlock_irqrestore(&interfaces_lock,flags);
1883                                 kfree(intf);
1884                                 rv = 0;
1885                                 goto out_call_watcher;
1886                         }
1887                 }
1888         } else {
1889                 rv = -EBUSY;
1890         }
1891         up_write(&interfaces_sem);
1892
1893         return rv;
1894
1895  out_call_watcher:
1896         downgrade_write(&interfaces_sem);
1897
1898         /* Call all the watcher interfaces to tell them that
1899            an interface is gone. */
1900         down_read(&smi_watchers_sem);
1901         list_for_each_entry(w, &smi_watchers, link) {
1902                 w->smi_gone(i);
1903         }
1904         up_read(&smi_watchers_sem);
1905         up_read(&interfaces_sem);
1906         return 0;
1907 }
1908
1909 static int handle_ipmb_get_msg_rsp(ipmi_smi_t          intf,
1910                                    struct ipmi_smi_msg *msg)
1911 {
1912         struct ipmi_ipmb_addr ipmb_addr;
1913         struct ipmi_recv_msg  *recv_msg;
1914         unsigned long         flags;
1915
1916         
1917         /* This is 11, not 10, because the response must contain a
1918          * completion code. */
1919         if (msg->rsp_size < 11) {
1920                 /* Message not big enough, just ignore it. */
1921                 spin_lock_irqsave(&intf->counter_lock, flags);
1922                 intf->invalid_ipmb_responses++;
1923                 spin_unlock_irqrestore(&intf->counter_lock, flags);
1924                 return 0;
1925         }
1926
1927         if (msg->rsp[2] != 0) {
1928                 /* An error getting the response, just ignore it. */
1929                 return 0;
1930         }
1931
1932         ipmb_addr.addr_type = IPMI_IPMB_ADDR_TYPE;
1933         ipmb_addr.slave_addr = msg->rsp[6];
1934         ipmb_addr.channel = msg->rsp[3] & 0x0f;
1935         ipmb_addr.lun = msg->rsp[7] & 3;
1936
1937         /* It's a response from a remote entity.  Look up the sequence
1938            number and handle the response. */
1939         if (intf_find_seq(intf,
1940                           msg->rsp[7] >> 2,
1941                           msg->rsp[3] & 0x0f,
1942                           msg->rsp[8],
1943                           (msg->rsp[4] >> 2) & (~1),
1944                           (struct ipmi_addr *) &(ipmb_addr),
1945                           &recv_msg))
1946         {
1947                 /* We were unable to find the sequence number,
1948                    so just nuke the message. */
1949                 spin_lock_irqsave(&intf->counter_lock, flags);
1950                 intf->unhandled_ipmb_responses++;
1951                 spin_unlock_irqrestore(&intf->counter_lock, flags);
1952                 return 0;
1953         }
1954
1955         memcpy(recv_msg->msg_data,
1956                &(msg->rsp[9]),
1957                msg->rsp_size - 9);
1958         /* THe other fields matched, so no need to set them, except
1959            for netfn, which needs to be the response that was
1960            returned, not the request value. */
1961         recv_msg->msg.netfn = msg->rsp[4] >> 2;
1962         recv_msg->msg.data = recv_msg->msg_data;
1963         recv_msg->msg.data_len = msg->rsp_size - 10;
1964         recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
1965         spin_lock_irqsave(&intf->counter_lock, flags);
1966         intf->handled_ipmb_responses++;
1967         spin_unlock_irqrestore(&intf->counter_lock, flags);
1968         deliver_response(recv_msg);
1969
1970         return 0;
1971 }
1972
1973 static int handle_ipmb_get_msg_cmd(ipmi_smi_t          intf,
1974                                    struct ipmi_smi_msg *msg)
1975 {
1976         struct cmd_rcvr       *rcvr;
1977         int                   rv = 0;
1978         unsigned char         netfn;
1979         unsigned char         cmd;
1980         ipmi_user_t           user = NULL;
1981         struct ipmi_ipmb_addr *ipmb_addr;
1982         struct ipmi_recv_msg  *recv_msg;
1983         unsigned long         flags;
1984
1985         if (msg->rsp_size < 10) {
1986                 /* Message not big enough, just ignore it. */
1987                 spin_lock_irqsave(&intf->counter_lock, flags);
1988                 intf->invalid_commands++;
1989                 spin_unlock_irqrestore(&intf->counter_lock, flags);
1990                 return 0;
1991         }
1992
1993         if (msg->rsp[2] != 0) {
1994                 /* An error getting the response, just ignore it. */
1995                 return 0;
1996         }
1997
1998         netfn = msg->rsp[4] >> 2;
1999         cmd = msg->rsp[8];
2000
2001         read_lock(&(intf->cmd_rcvr_lock));
2002         
2003         if (intf->all_cmd_rcvr) {
2004                 user = intf->all_cmd_rcvr;
2005         } else {
2006                 /* Find the command/netfn. */
2007                 list_for_each_entry(rcvr, &(intf->cmd_rcvrs), link) {
2008                         if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)) {
2009                                 user = rcvr->user;
2010                                 break;
2011                         }
2012                 }
2013         }
2014         read_unlock(&(intf->cmd_rcvr_lock));
2015
2016         if (user == NULL) {
2017                 /* We didn't find a user, deliver an error response. */
2018                 spin_lock_irqsave(&intf->counter_lock, flags);
2019                 intf->unhandled_commands++;
2020                 spin_unlock_irqrestore(&intf->counter_lock, flags);
2021
2022                 msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
2023                 msg->data[1] = IPMI_SEND_MSG_CMD;
2024                 msg->data[2] = msg->rsp[3];
2025                 msg->data[3] = msg->rsp[6];
2026                 msg->data[4] = ((netfn + 1) << 2) | (msg->rsp[7] & 0x3);
2027                 msg->data[5] = ipmb_checksum(&(msg->data[3]), 2);
2028                 msg->data[6] = intf->my_address;
2029                 /* rqseq/lun */
2030                 msg->data[7] = (msg->rsp[7] & 0xfc) | (msg->rsp[4] & 0x3);
2031                 msg->data[8] = msg->rsp[8]; /* cmd */
2032                 msg->data[9] = IPMI_INVALID_CMD_COMPLETION_CODE;
2033                 msg->data[10] = ipmb_checksum(&(msg->data[6]), 4);
2034                 msg->data_size = 11;
2035
2036 #ifdef DEBUG_MSGING
2037         {
2038                 int m;
2039                 printk("Invalid command:");
2040                 for (m=0; m<msg->data_size; m++)
2041                         printk(" %2.2x", msg->data[m]);
2042                 printk("\n");
2043         }
2044 #endif
2045                 intf->handlers->sender(intf->send_info, msg, 0);
2046
2047                 rv = -1; /* We used the message, so return the value that
2048                             causes it to not be freed or queued. */
2049         } else {
2050                 /* Deliver the message to the user. */
2051                 spin_lock_irqsave(&intf->counter_lock, flags);
2052                 intf->handled_commands++;
2053                 spin_unlock_irqrestore(&intf->counter_lock, flags);
2054
2055                 recv_msg = ipmi_alloc_recv_msg();
2056                 if (! recv_msg) {
2057                         /* We couldn't allocate memory for the
2058                            message, so requeue it for handling
2059                            later. */
2060                         rv = 1;
2061                 } else {
2062                         /* Extract the source address from the data. */
2063                         ipmb_addr = (struct ipmi_ipmb_addr *) &recv_msg->addr;
2064                         ipmb_addr->addr_type = IPMI_IPMB_ADDR_TYPE;
2065                         ipmb_addr->slave_addr = msg->rsp[6];
2066                         ipmb_addr->lun = msg->rsp[7] & 3;
2067                         ipmb_addr->channel = msg->rsp[3] & 0xf;
2068
2069                         /* Extract the rest of the message information
2070                            from the IPMB header.*/
2071                         recv_msg->user = user;
2072                         recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
2073                         recv_msg->msgid = msg->rsp[7] >> 2;
2074                         recv_msg->msg.netfn = msg->rsp[4] >> 2;
2075                         recv_msg->msg.cmd = msg->rsp[8];
2076                         recv_msg->msg.data = recv_msg->msg_data;
2077
2078                         /* We chop off 10, not 9 bytes because the checksum
2079                            at the end also needs to be removed. */
2080                         recv_msg->msg.data_len = msg->rsp_size - 10;
2081                         memcpy(recv_msg->msg_data,
2082                                &(msg->rsp[9]),
2083                                msg->rsp_size - 10);
2084                         deliver_response(recv_msg);
2085                 }
2086         }
2087
2088         return rv;
2089 }
2090
2091 static int handle_lan_get_msg_rsp(ipmi_smi_t          intf,
2092                                   struct ipmi_smi_msg *msg)
2093 {
2094         struct ipmi_lan_addr  lan_addr;
2095         struct ipmi_recv_msg  *recv_msg;
2096         unsigned long         flags;
2097
2098
2099         /* This is 13, not 12, because the response must contain a
2100          * completion code. */
2101         if (msg->rsp_size < 13) {
2102                 /* Message not big enough, just ignore it. */
2103                 spin_lock_irqsave(&intf->counter_lock, flags);
2104                 intf->invalid_lan_responses++;
2105                 spin_unlock_irqrestore(&intf->counter_lock, flags);
2106                 return 0;
2107         }
2108
2109         if (msg->rsp[2] != 0) {
2110                 /* An error getting the response, just ignore it. */
2111                 return 0;
2112         }
2113
2114         lan_addr.addr_type = IPMI_LAN_ADDR_TYPE;
2115         lan_addr.session_handle = msg->rsp[4];
2116         lan_addr.remote_SWID = msg->rsp[8];
2117         lan_addr.local_SWID = msg->rsp[5];
2118         lan_addr.channel = msg->rsp[3] & 0x0f;
2119         lan_addr.privilege = msg->rsp[3] >> 4;
2120         lan_addr.lun = msg->rsp[9] & 3;
2121
2122         /* It's a response from a remote entity.  Look up the sequence
2123            number and handle the response. */
2124         if (intf_find_seq(intf,
2125                           msg->rsp[9] >> 2,
2126                           msg->rsp[3] & 0x0f,
2127                           msg->rsp[10],
2128                           (msg->rsp[6] >> 2) & (~1),
2129                           (struct ipmi_addr *) &(lan_addr),
2130                           &recv_msg))
2131         {
2132                 /* We were unable to find the sequence number,
2133                    so just nuke the message. */
2134                 spin_lock_irqsave(&intf->counter_lock, flags);
2135                 intf->unhandled_lan_responses++;
2136                 spin_unlock_irqrestore(&intf->counter_lock, flags);
2137                 return 0;
2138         }
2139
2140         memcpy(recv_msg->msg_data,
2141                &(msg->rsp[11]),
2142                msg->rsp_size - 11);
2143         /* The other fields matched, so no need to set them, except
2144            for netfn, which needs to be the response that was
2145            returned, not the request value. */
2146         recv_msg->msg.netfn = msg->rsp[6] >> 2;
2147         recv_msg->msg.data = recv_msg->msg_data;
2148         recv_msg->msg.data_len = msg->rsp_size - 12;
2149         recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
2150         spin_lock_irqsave(&intf->counter_lock, flags);
2151         intf->handled_lan_responses++;
2152         spin_unlock_irqrestore(&intf->counter_lock, flags);
2153         deliver_response(recv_msg);
2154
2155         return 0;
2156 }
2157
2158 static int handle_lan_get_msg_cmd(ipmi_smi_t          intf,
2159                                   struct ipmi_smi_msg *msg)
2160 {
2161         struct cmd_rcvr       *rcvr;
2162         int                   rv = 0;
2163         unsigned char         netfn;
2164         unsigned char         cmd;
2165         ipmi_user_t           user = NULL;
2166         struct ipmi_lan_addr  *lan_addr;
2167         struct ipmi_recv_msg  *recv_msg;
2168         unsigned long         flags;
2169
2170         if (msg->rsp_size < 12) {
2171                 /* Message not big enough, just ignore it. */
2172                 spin_lock_irqsave(&intf->counter_lock, flags);
2173                 intf->invalid_commands++;
2174                 spin_unlock_irqrestore(&intf->counter_lock, flags);
2175                 return 0;
2176         }
2177
2178         if (msg->rsp[2] != 0) {
2179                 /* An error getting the response, just ignore it. */
2180                 return 0;
2181         }
2182
2183         netfn = msg->rsp[6] >> 2;
2184         cmd = msg->rsp[10];
2185
2186         read_lock(&(intf->cmd_rcvr_lock));
2187
2188         if (intf->all_cmd_rcvr) {
2189                 user = intf->all_cmd_rcvr;
2190         } else {
2191                 /* Find the command/netfn. */
2192                 list_for_each_entry(rcvr, &(intf->cmd_rcvrs), link) {
2193                         if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)) {
2194                                 user = rcvr->user;
2195                                 break;
2196                         }
2197                 }
2198         }
2199         read_unlock(&(intf->cmd_rcvr_lock));
2200
2201         if (user == NULL) {
2202                 /* We didn't find a user, deliver an error response. */
2203                 spin_lock_irqsave(&intf->counter_lock, flags);
2204                 intf->unhandled_commands++;
2205                 spin_unlock_irqrestore(&intf->counter_lock, flags);
2206
2207                 rv = 0; /* Don't do anything with these messages, just
2208                            allow them to be freed. */
2209         } else {
2210                 /* Deliver the message to the user. */
2211                 spin_lock_irqsave(&intf->counter_lock, flags);
2212                 intf->handled_commands++;
2213                 spin_unlock_irqrestore(&intf->counter_lock, flags);
2214
2215                 recv_msg = ipmi_alloc_recv_msg();
2216                 if (! recv_msg) {
2217                         /* We couldn't allocate memory for the
2218                            message, so requeue it for handling
2219                            later. */
2220                         rv = 1;
2221                 } else {
2222                         /* Extract the source address from the data. */
2223                         lan_addr = (struct ipmi_lan_addr *) &recv_msg->addr;
2224                         lan_addr->addr_type = IPMI_LAN_ADDR_TYPE;
2225                         lan_addr->session_handle = msg->rsp[4];
2226                         lan_addr->remote_SWID = msg->rsp[8];
2227                         lan_addr->local_SWID = msg->rsp[5];
2228                         lan_addr->lun = msg->rsp[9] & 3;
2229                         lan_addr->channel = msg->rsp[3] & 0xf;
2230                         lan_addr->privilege = msg->rsp[3] >> 4;
2231
2232                         /* Extract the rest of the message information
2233                            from the IPMB header.*/
2234                         recv_msg->user = user;
2235                         recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
2236                         recv_msg->msgid = msg->rsp[9] >> 2;
2237                         recv_msg->msg.netfn = msg->rsp[6] >> 2;
2238                         recv_msg->msg.cmd = msg->rsp[10];
2239                         recv_msg->msg.data = recv_msg->msg_data;
2240
2241                         /* We chop off 12, not 11 bytes because the checksum
2242                            at the end also needs to be removed. */
2243                         recv_msg->msg.data_len = msg->rsp_size - 12;
2244                         memcpy(recv_msg->msg_data,
2245                                &(msg->rsp[11]),
2246                                msg->rsp_size - 12);
2247                         deliver_response(recv_msg);
2248                 }
2249         }
2250
2251         return rv;
2252 }
2253
2254 static void copy_event_into_recv_msg(struct ipmi_recv_msg *recv_msg,
2255                                      struct ipmi_smi_msg  *msg)
2256 {
2257         struct ipmi_system_interface_addr *smi_addr;
2258         
2259         recv_msg->msgid = 0;
2260         smi_addr = (struct ipmi_system_interface_addr *) &(recv_msg->addr);
2261         smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2262         smi_addr->channel = IPMI_BMC_CHANNEL;
2263         smi_addr->lun = msg->rsp[0] & 3;
2264         recv_msg->recv_type = IPMI_ASYNC_EVENT_RECV_TYPE;
2265         recv_msg->msg.netfn = msg->rsp[0] >> 2;
2266         recv_msg->msg.cmd = msg->rsp[1];
2267         memcpy(recv_msg->msg_data, &(msg->rsp[3]), msg->rsp_size - 3);
2268         recv_msg->msg.data = recv_msg->msg_data;
2269         recv_msg->msg.data_len = msg->rsp_size - 3;
2270 }
2271
2272 /* This will be called with the intf->users_lock read-locked, so no need
2273    to do that here. */
2274 static int handle_read_event_rsp(ipmi_smi_t          intf,
2275                                  struct ipmi_smi_msg *msg)
2276 {
2277         struct ipmi_recv_msg *recv_msg, *recv_msg2;
2278         struct list_head     msgs;
2279         ipmi_user_t          user;
2280         int                  rv = 0;
2281         int                  deliver_count = 0;
2282         unsigned long        flags;
2283
2284         if (msg->rsp_size < 19) {
2285                 /* Message is too small to be an IPMB event. */
2286                 spin_lock_irqsave(&intf->counter_lock, flags);
2287                 intf->invalid_events++;
2288                 spin_unlock_irqrestore(&intf->counter_lock, flags);
2289                 return 0;
2290         }
2291
2292         if (msg->rsp[2] != 0) {
2293                 /* An error getting the event, just ignore it. */
2294                 return 0;
2295         }
2296
2297         INIT_LIST_HEAD(&msgs);
2298
2299         spin_lock_irqsave(&(intf->events_lock), flags);
2300
2301         spin_lock(&intf->counter_lock);
2302         intf->events++;
2303         spin_unlock(&intf->counter_lock);
2304
2305         /* Allocate and fill in one message for every user that is getting
2306            events. */
2307         list_for_each_entry(user, &(intf->users), link) {
2308                 if (! user->gets_events)
2309                         continue;
2310
2311                 recv_msg = ipmi_alloc_recv_msg();
2312                 if (! recv_msg) {
2313                         list_for_each_entry_safe(recv_msg, recv_msg2, &msgs, link) {
2314                                 list_del(&recv_msg->link);
2315                                 ipmi_free_recv_msg(recv_msg);
2316                         }
2317                         /* We couldn't allocate memory for the
2318                            message, so requeue it for handling
2319                            later. */
2320                         rv = 1;
2321                         goto out;
2322                 }
2323
2324                 deliver_count++;
2325
2326                 copy_event_into_recv_msg(recv_msg, msg);
2327                 recv_msg->user = user;
2328                 list_add_tail(&(recv_msg->link), &msgs);
2329         }
2330
2331         if (deliver_count) {
2332                 /* Now deliver all the messages. */
2333                 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs, link) {
2334                         list_del(&recv_msg->link);
2335                         deliver_response(recv_msg);
2336                 }
2337         } else if (intf->waiting_events_count < MAX_EVENTS_IN_QUEUE) {
2338                 /* No one to receive the message, put it in queue if there's
2339                    not already too many things in the queue. */
2340                 recv_msg = ipmi_alloc_recv_msg();
2341                 if (! recv_msg) {
2342                         /* We couldn't allocate memory for the
2343                            message, so requeue it for handling
2344                            later. */
2345                         rv = 1;
2346                         goto out;
2347                 }
2348
2349                 copy_event_into_recv_msg(recv_msg, msg);
2350                 list_add_tail(&(recv_msg->link), &(intf->waiting_events));
2351         } else {
2352                 /* There's too many things in the queue, discard this
2353                    message. */
2354                 printk(KERN_WARNING "ipmi: Event queue full, discarding an"
2355                        " incoming event\n");
2356         }
2357
2358  out:
2359         spin_unlock_irqrestore(&(intf->events_lock), flags);
2360
2361         return rv;
2362 }
2363
2364 static int handle_bmc_rsp(ipmi_smi_t          intf,
2365                           struct ipmi_smi_msg *msg)
2366 {
2367         struct ipmi_recv_msg *recv_msg;
2368         int                  found = 0;
2369         struct ipmi_user     *user;
2370         unsigned long        flags;
2371
2372         recv_msg = (struct ipmi_recv_msg *) msg->user_data;
2373
2374         /* Make sure the user still exists. */
2375         list_for_each_entry(user, &(intf->users), link) {
2376                 if (user == recv_msg->user) {
2377                         /* Found it, so we can deliver it */
2378                         found = 1;
2379                         break;
2380                 }
2381         }
2382
2383         if (!found) {
2384                 /* Special handling for NULL users. */
2385                 if (!recv_msg->user && intf->null_user_handler)
2386                         intf->null_user_handler(intf, msg);
2387                 /* The user for the message went away, so give up. */
2388                 spin_lock_irqsave(&intf->counter_lock, flags);
2389                 intf->unhandled_local_responses++;
2390                 spin_unlock_irqrestore(&intf->counter_lock, flags);
2391                 ipmi_free_recv_msg(recv_msg);
2392         } else {
2393                 struct ipmi_system_interface_addr *smi_addr;
2394
2395                 spin_lock_irqsave(&intf->counter_lock, flags);
2396                 intf->handled_local_responses++;
2397                 spin_unlock_irqrestore(&intf->counter_lock, flags);
2398                 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
2399                 recv_msg->msgid = msg->msgid;
2400                 smi_addr = ((struct ipmi_system_interface_addr *)
2401                             &(recv_msg->addr));
2402                 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2403                 smi_addr->channel = IPMI_BMC_CHANNEL;
2404                 smi_addr->lun = msg->rsp[0] & 3;
2405                 recv_msg->msg.netfn = msg->rsp[0] >> 2;
2406                 recv_msg->msg.cmd = msg->rsp[1];
2407                 memcpy(recv_msg->msg_data,
2408                        &(msg->rsp[2]),
2409                        msg->rsp_size - 2);
2410                 recv_msg->msg.data = recv_msg->msg_data;
2411                 recv_msg->msg.data_len = msg->rsp_size - 2;
2412                 deliver_response(recv_msg);
2413         }
2414
2415         return 0;
2416 }
2417
2418 /* Handle a new message.  Return 1 if the message should be requeued,
2419    0 if the message should be freed, or -1 if the message should not
2420    be freed or requeued. */
2421 static int handle_new_recv_msg(ipmi_smi_t          intf,
2422                                struct ipmi_smi_msg *msg)
2423 {
2424         int requeue;
2425         int chan;
2426
2427 #ifdef DEBUG_MSGING
2428         int m;
2429         printk("Recv:");
2430         for (m=0; m<msg->rsp_size; m++)
2431                 printk(" %2.2x", msg->rsp[m]);
2432         printk("\n");
2433 #endif
2434         if (msg->rsp_size < 2) {
2435                 /* Message is too small to be correct. */
2436                 requeue = 0;
2437         } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
2438                    && (msg->rsp[1] == IPMI_SEND_MSG_CMD)
2439                    && (msg->user_data != NULL))
2440         {
2441                 /* It's a response to a response we sent.  For this we
2442                    deliver a send message response to the user. */
2443                 struct ipmi_recv_msg *recv_msg = msg->user_data;
2444
2445                 requeue = 0;
2446                 if (msg->rsp_size < 2)
2447                         /* Message is too small to be correct. */
2448                         goto out;
2449
2450                 chan = msg->data[2] & 0x0f;
2451                 if (chan >= IPMI_MAX_CHANNELS)
2452                         /* Invalid channel number */
2453                         goto out;
2454
2455                 if (recv_msg) {
2456                         recv_msg->recv_type = IPMI_RESPONSE_RESPONSE_TYPE;
2457                         recv_msg->msg.data = recv_msg->msg_data;
2458                         recv_msg->msg.data_len = 1;
2459                         recv_msg->msg_data[0] = msg->rsp[2];
2460                         deliver_response(recv_msg);
2461                 }
2462         } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
2463                    && (msg->rsp[1] == IPMI_GET_MSG_CMD))
2464         {
2465                 /* It's from the receive queue. */
2466                 chan = msg->rsp[3] & 0xf;
2467                 if (chan >= IPMI_MAX_CHANNELS) {
2468                         /* Invalid channel number */
2469                         requeue = 0;
2470                         goto out;
2471                 }
2472
2473                 switch (intf->channels[chan].medium) {
2474                 case IPMI_CHANNEL_MEDIUM_IPMB:
2475                         if (msg->rsp[4] & 0x04) {
2476                                 /* It's a response, so find the
2477                                    requesting message and send it up. */
2478                                 requeue = handle_ipmb_get_msg_rsp(intf, msg);
2479                         } else {
2480                                 /* It's a command to the SMS from some other
2481                                    entity.  Handle that. */
2482                                 requeue = handle_ipmb_get_msg_cmd(intf, msg);
2483                         }
2484                         break;
2485
2486                 case IPMI_CHANNEL_MEDIUM_8023LAN:
2487                 case IPMI_CHANNEL_MEDIUM_ASYNC:
2488                         if (msg->rsp[6] & 0x04) {
2489                                 /* It's a response, so find the
2490                                    requesting message and send it up. */
2491                                 requeue = handle_lan_get_msg_rsp(intf, msg);
2492                         } else {
2493                                 /* It's a command to the SMS from some other
2494                                    entity.  Handle that. */
2495                                 requeue = handle_lan_get_msg_cmd(intf, msg);
2496                         }
2497                         break;
2498
2499                 default:
2500                         /* We don't handle the channel type, so just
2501                          * free the message. */
2502                         requeue = 0;
2503                 }
2504
2505         } else if (msg->rsp[1] == IPMI_READ_EVENT_MSG_BUFFER_CMD) {
2506                 /* It's an asyncronous event. */
2507                 requeue = handle_read_event_rsp(intf, msg);
2508         } else {
2509                 /* It's a response from the local BMC. */
2510                 requeue = handle_bmc_rsp(intf, msg);
2511         }
2512
2513  out:
2514         return requeue;
2515 }
2516
2517 /* Handle a new message from the lower layer. */
2518 void ipmi_smi_msg_received(ipmi_smi_t          intf,
2519                            struct ipmi_smi_msg *msg)
2520 {
2521         unsigned long flags;
2522         int           rv;
2523
2524
2525         /* Lock the user lock so the user can't go away while we are
2526            working on it. */
2527         read_lock(&(intf->users_lock));
2528
2529         if ((msg->data_size >= 2)
2530             && (msg->data[0] == (IPMI_NETFN_APP_REQUEST << 2))
2531             && (msg->data[1] == IPMI_SEND_MSG_CMD)
2532             && (msg->user_data == NULL)) {
2533                 /* This is the local response to a command send, start
2534                    the timer for these.  The user_data will not be
2535                    NULL if this is a response send, and we will let
2536                    response sends just go through. */
2537
2538                 /* Check for errors, if we get certain errors (ones
2539                    that mean basically we can try again later), we
2540                    ignore them and start the timer.  Otherwise we
2541                    report the error immediately. */
2542                 if ((msg->rsp_size >= 3) && (msg->rsp[2] != 0)
2543                     && (msg->rsp[2] != IPMI_NODE_BUSY_ERR)
2544                     && (msg->rsp[2] != IPMI_LOST_ARBITRATION_ERR))
2545                 {
2546                         int chan = msg->rsp[3] & 0xf;
2547
2548                         /* Got an error sending the message, handle it. */
2549                         spin_lock_irqsave(&intf->counter_lock, flags);
2550                         if (chan >= IPMI_MAX_CHANNELS)
2551                                 ; /* This shouldn't happen */
2552                         else if ((intf->channels[chan].medium
2553                                   == IPMI_CHANNEL_MEDIUM_8023LAN)
2554                                  || (intf->channels[chan].medium
2555                                      == IPMI_CHANNEL_MEDIUM_ASYNC))
2556                                 intf->sent_lan_command_errs++;
2557                         else
2558                                 intf->sent_ipmb_command_errs++;
2559                         spin_unlock_irqrestore(&intf->counter_lock, flags);
2560                         intf_err_seq(intf, msg->msgid, msg->rsp[2]);
2561                 } else {
2562                         /* The message was sent, start the timer. */
2563                         intf_start_seq_timer(intf, msg->msgid);
2564                 }
2565
2566                 ipmi_free_smi_msg(msg);
2567                 goto out_unlock;
2568         }
2569
2570         /* To preserve message order, if the list is not empty, we
2571            tack this message onto the end of the list. */
2572         spin_lock_irqsave(&(intf->waiting_msgs_lock), flags);
2573         if (!list_empty(&(intf->waiting_msgs))) {
2574                 list_add_tail(&(msg->link), &(intf->waiting_msgs));
2575                 spin_unlock(&(intf->waiting_msgs_lock));
2576                 goto out_unlock;
2577         }
2578         spin_unlock_irqrestore(&(intf->waiting_msgs_lock), flags);
2579                 
2580         rv = handle_new_recv_msg(intf, msg);
2581         if (rv > 0) {
2582                 /* Could not handle the message now, just add it to a
2583                    list to handle later. */
2584                 spin_lock(&(intf->waiting_msgs_lock));
2585                 list_add_tail(&(msg->link), &(intf->waiting_msgs));
2586                 spin_unlock(&(intf->waiting_msgs_lock));
2587         } else if (rv == 0) {
2588                 ipmi_free_smi_msg(msg);
2589         }
2590
2591  out_unlock:
2592         read_unlock(&(intf->users_lock));
2593 }
2594
2595 void ipmi_smi_watchdog_pretimeout(ipmi_smi_t intf)
2596 {
2597         ipmi_user_t user;
2598
2599         read_lock(&(intf->users_lock));
2600         list_for_each_entry(user, &(intf->users), link) {
2601                 if (! user->handler->ipmi_watchdog_pretimeout)
2602                         continue;
2603
2604                 user->handler->ipmi_watchdog_pretimeout(user->handler_data);
2605         }
2606         read_unlock(&(intf->users_lock));
2607 }
2608
2609 static void
2610 handle_msg_timeout(struct ipmi_recv_msg *msg)
2611 {
2612         msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
2613         msg->msg_data[0] = IPMI_TIMEOUT_COMPLETION_CODE;
2614         msg->msg.netfn |= 1; /* Convert to a response. */
2615         msg->msg.data_len = 1;
2616         msg->msg.data = msg->msg_data;
2617         deliver_response(msg);
2618 }
2619
2620 static void
2621 send_from_recv_msg(ipmi_smi_t intf, struct ipmi_recv_msg *recv_msg,
2622                    struct ipmi_smi_msg *smi_msg,
2623                    unsigned char seq, long seqid)
2624 {
2625         if (!smi_msg)
2626                 smi_msg = ipmi_alloc_smi_msg();
2627         if (!smi_msg)
2628                 /* If we can't allocate the message, then just return, we
2629                    get 4 retries, so this should be ok. */
2630                 return;
2631
2632         memcpy(smi_msg->data, recv_msg->msg.data, recv_msg->msg.data_len);
2633         smi_msg->data_size = recv_msg->msg.data_len;
2634         smi_msg->msgid = STORE_SEQ_IN_MSGID(seq, seqid);
2635                 
2636         /* Send the new message.  We send with a zero priority.  It
2637            timed out, I doubt time is that critical now, and high
2638            priority messages are really only for messages to the local
2639            MC, which don't get resent. */
2640         intf->handlers->sender(intf->send_info, smi_msg, 0);
2641
2642 #ifdef DEBUG_MSGING
2643         {
2644                 int m;
2645                 printk("Resend: ");
2646                 for (m=0; m<smi_msg->data_size; m++)
2647                         printk(" %2.2x", smi_msg->data[m]);
2648                 printk("\n");
2649         }
2650 #endif
2651 }
2652
2653 static void
2654 ipmi_timeout_handler(long timeout_period)
2655 {
2656         ipmi_smi_t           intf;
2657         struct list_head     timeouts;
2658         struct ipmi_recv_msg *msg, *msg2;
2659         struct ipmi_smi_msg  *smi_msg, *smi_msg2;
2660         unsigned long        flags;
2661         int                  i, j;
2662
2663         INIT_LIST_HEAD(&timeouts);
2664
2665         spin_lock(&interfaces_lock);
2666         for (i=0; i<MAX_IPMI_INTERFACES; i++) {
2667                 intf = ipmi_interfaces[i];
2668                 if (intf == NULL)
2669                         continue;
2670
2671                 read_lock(&(intf->users_lock));
2672
2673                 /* See if any waiting messages need to be processed. */
2674                 spin_lock_irqsave(&(intf->waiting_msgs_lock), flags);
2675                 list_for_each_entry_safe(smi_msg, smi_msg2, &(intf->waiting_msgs), link) {
2676                         if (! handle_new_recv_msg(intf, smi_msg)) {
2677                                 list_del(&smi_msg->link);
2678                                 ipmi_free_smi_msg(smi_msg);
2679                         } else {
2680                                 /* To preserve message order, quit if we
2681                                    can't handle a message. */
2682                                 break;
2683                         }
2684                 }
2685                 spin_unlock_irqrestore(&(intf->waiting_msgs_lock), flags);
2686
2687                 /* Go through the seq table and find any messages that
2688                    have timed out, putting them in the timeouts
2689                    list. */
2690                 spin_lock_irqsave(&(intf->seq_lock), flags);
2691                 for (j=0; j<IPMI_IPMB_NUM_SEQ; j++) {
2692                         struct seq_table *ent = &(intf->seq_table[j]);
2693                         if (!ent->inuse)
2694                                 continue;
2695
2696                         ent->timeout -= timeout_period;
2697                         if (ent->timeout > 0)
2698                                 continue;
2699
2700                         if (ent->retries_left == 0) {
2701                                 /* The message has used all its retries. */
2702                                 ent->inuse = 0;
2703                                 msg = ent->recv_msg;
2704                                 list_add_tail(&(msg->link), &timeouts);
2705                                 spin_lock(&intf->counter_lock);
2706                                 if (ent->broadcast)
2707                                         intf->timed_out_ipmb_broadcasts++;
2708                                 else if (ent->recv_msg->addr.addr_type
2709                                          == IPMI_LAN_ADDR_TYPE)
2710                                         intf->timed_out_lan_commands++;
2711                                 else
2712                                         intf->timed_out_ipmb_commands++;
2713                                 spin_unlock(&intf->counter_lock);
2714                         } else {
2715                                 /* More retries, send again. */
2716
2717                                 /* Start with the max timer, set to normal
2718                                    timer after the message is sent. */
2719                                 ent->timeout = MAX_MSG_TIMEOUT;
2720                                 ent->retries_left--;
2721                                 send_from_recv_msg(intf, ent->recv_msg, NULL,
2722                                                    j, ent->seqid);
2723                                 spin_lock(&intf->counter_lock);
2724                                 if (ent->recv_msg->addr.addr_type
2725                                     == IPMI_LAN_ADDR_TYPE)
2726                                         intf->retransmitted_lan_commands++;
2727                                 else
2728                                         intf->retransmitted_ipmb_commands++;
2729                                 spin_unlock(&intf->counter_lock);
2730                         }
2731                 }
2732                 spin_unlock_irqrestore(&(intf->seq_lock), flags);
2733
2734                 list_for_each_entry_safe(msg, msg2, &timeouts, link) {
2735                         handle_msg_timeout(msg);
2736                 }
2737
2738                 read_unlock(&(intf->users_lock));
2739         }
2740         spin_unlock(&interfaces_lock);
2741 }
2742
2743 static void ipmi_request_event(void)
2744 {
2745         ipmi_smi_t intf;
2746         int        i;
2747
2748         spin_lock(&interfaces_lock);
2749         for (i=0; i<MAX_IPMI_INTERFACES; i++) {
2750                 intf = ipmi_interfaces[i];
2751                 if (intf == NULL)
2752                         continue;
2753
2754                 intf->handlers->request_events(intf->send_info);
2755         }
2756         spin_unlock(&interfaces_lock);
2757 }
2758
2759 static struct timer_list ipmi_timer;
2760
2761 /* Call every ~100 ms. */
2762 #define IPMI_TIMEOUT_TIME       100
2763
2764 /* How many jiffies does it take to get to the timeout time. */
2765 #define IPMI_TIMEOUT_JIFFIES    ((IPMI_TIMEOUT_TIME * HZ) / 1000)
2766
2767 /* Request events from the queue every second (this is the number of
2768    IPMI_TIMEOUT_TIMES between event requests).  Hopefully, in the
2769    future, IPMI will add a way to know immediately if an event is in
2770    the queue and this silliness can go away. */
2771 #define IPMI_REQUEST_EV_TIME    (1000 / (IPMI_TIMEOUT_TIME))
2772
2773 static volatile int stop_operation = 0;
2774 static volatile int timer_stopped = 0;
2775 static unsigned int ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
2776
2777 static void ipmi_timeout(unsigned long data)
2778 {
2779         if (stop_operation) {
2780                 timer_stopped = 1;
2781                 return;
2782         }
2783
2784         ticks_to_req_ev--;
2785         if (ticks_to_req_ev == 0) {
2786                 ipmi_request_event();
2787                 ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
2788         }
2789
2790         ipmi_timeout_handler(IPMI_TIMEOUT_TIME);
2791
2792         ipmi_timer.expires += IPMI_TIMEOUT_JIFFIES;
2793         add_timer(&ipmi_timer);
2794 }
2795
2796
2797 static atomic_t smi_msg_inuse_count = ATOMIC_INIT(0);
2798 static atomic_t recv_msg_inuse_count = ATOMIC_INIT(0);
2799
2800 /* FIXME - convert these to slabs. */
2801 static void free_smi_msg(struct ipmi_smi_msg *msg)
2802 {
2803         atomic_dec(&smi_msg_inuse_count);
2804         kfree(msg);
2805 }
2806
2807 struct ipmi_smi_msg *ipmi_alloc_smi_msg(void)
2808 {
2809         struct ipmi_smi_msg *rv;
2810         rv = kmalloc(sizeof(struct ipmi_smi_msg), GFP_ATOMIC);
2811         if (rv) {
2812                 rv->done = free_smi_msg;
2813                 rv->user_data = NULL;
2814                 atomic_inc(&smi_msg_inuse_count);
2815         }
2816         return rv;
2817 }
2818
2819 static void free_recv_msg(struct ipmi_recv_msg *msg)
2820 {
2821         atomic_dec(&recv_msg_inuse_count);
2822         kfree(msg);
2823 }
2824
2825 struct ipmi_recv_msg *ipmi_alloc_recv_msg(void)
2826 {
2827         struct ipmi_recv_msg *rv;
2828
2829         rv = kmalloc(sizeof(struct ipmi_recv_msg), GFP_ATOMIC);
2830         if (rv) {
2831                 rv->done = free_recv_msg;
2832                 atomic_inc(&recv_msg_inuse_count);
2833         }
2834         return rv;
2835 }
2836
2837 #ifdef CONFIG_IPMI_PANIC_EVENT
2838
2839 static void dummy_smi_done_handler(struct ipmi_smi_msg *msg)
2840 {
2841 }
2842
2843 static void dummy_recv_done_handler(struct ipmi_recv_msg *msg)
2844 {
2845 }
2846
2847 #ifdef CONFIG_IPMI_PANIC_STRING
2848 static void event_receiver_fetcher(ipmi_smi_t intf, struct ipmi_smi_msg *msg)
2849 {
2850         if ((msg->rsp[0] == (IPMI_NETFN_SENSOR_EVENT_RESPONSE << 2))
2851             && (msg->rsp[1] == IPMI_GET_EVENT_RECEIVER_CMD)
2852             && (msg->rsp[2] == IPMI_CC_NO_ERROR))
2853         {
2854                 /* A get event receiver command, save it. */
2855                 intf->event_receiver = msg->rsp[3];
2856                 intf->event_receiver_lun = msg->rsp[4] & 0x3;
2857         }
2858 }
2859
2860 static void device_id_fetcher(ipmi_smi_t intf, struct ipmi_smi_msg *msg)
2861 {
2862         if ((msg->rsp[0] == (IPMI_NETFN_APP_RESPONSE << 2))
2863             && (msg->rsp[1] == IPMI_GET_DEVICE_ID_CMD)
2864             && (msg->rsp[2] == IPMI_CC_NO_ERROR))
2865         {
2866                 /* A get device id command, save if we are an event
2867                    receiver or generator. */
2868                 intf->local_sel_device = (msg->rsp[8] >> 2) & 1;
2869                 intf->local_event_generator = (msg->rsp[8] >> 5) & 1;
2870         }
2871 }
2872 #endif
2873
2874 static void send_panic_events(char *str)
2875 {
2876         struct kernel_ipmi_msg            msg;
2877         ipmi_smi_t                        intf;
2878         unsigned char                     data[16];
2879         int                               i;
2880         struct ipmi_system_interface_addr *si;
2881         struct ipmi_addr                  addr;
2882         struct ipmi_smi_msg               smi_msg;
2883         struct ipmi_recv_msg              recv_msg;
2884
2885         si = (struct ipmi_system_interface_addr *) &addr;
2886         si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2887         si->channel = IPMI_BMC_CHANNEL;
2888         si->lun = 0;
2889
2890         /* Fill in an event telling that we have failed. */
2891         msg.netfn = 0x04; /* Sensor or Event. */
2892         msg.cmd = 2; /* Platform event command. */
2893         msg.data = data;
2894         msg.data_len = 8;
2895         data[0] = 0x21; /* Kernel generator ID, IPMI table 5-4 */
2896         data[1] = 0x03; /* This is for IPMI 1.0. */
2897         data[2] = 0x20; /* OS Critical Stop, IPMI table 36-3 */
2898         data[4] = 0x6f; /* Sensor specific, IPMI table 36-1 */
2899         data[5] = 0xa1; /* Runtime stop OEM bytes 2 & 3. */
2900
2901         /* Put a few breadcrumbs in.  Hopefully later we can add more things
2902            to make the panic events more useful. */
2903         if (str) {
2904                 data[3] = str[0];
2905                 data[6] = str[1];
2906                 data[7] = str[2];
2907         }
2908
2909         smi_msg.done = dummy_smi_done_handler;
2910         recv_msg.done = dummy_recv_done_handler;
2911
2912         /* For every registered interface, send the event. */
2913         for (i=0; i<MAX_IPMI_INTERFACES; i++) {
2914                 intf = ipmi_interfaces[i];
2915                 if (intf == NULL)
2916                         continue;
2917
2918                 /* Send the event announcing the panic. */
2919                 intf->handlers->set_run_to_completion(intf->send_info, 1);
2920                 i_ipmi_request(NULL,
2921                                intf,
2922                                &addr,
2923                                0,
2924                                &msg,
2925                                NULL,
2926                                &smi_msg,
2927                                &recv_msg,
2928                                0,
2929                                intf->my_address,
2930                                intf->my_lun,
2931                                0, 1); /* Don't retry, and don't wait. */
2932         }
2933
2934 #ifdef CONFIG_IPMI_PANIC_STRING
2935         /* On every interface, dump a bunch of OEM event holding the
2936            string. */
2937         if (!str) 
2938                 return;
2939
2940         for (i=0; i<MAX_IPMI_INTERFACES; i++) {
2941                 char                  *p = str;
2942                 struct ipmi_ipmb_addr *ipmb;
2943                 int                   j;
2944
2945                 intf = ipmi_interfaces[i];
2946                 if (intf == NULL)
2947                         continue;
2948
2949                 /* First job here is to figure out where to send the
2950                    OEM events.  There's no way in IPMI to send OEM
2951                    events using an event send command, so we have to
2952                    find the SEL to put them in and stick them in
2953                    there. */
2954
2955                 /* Get capabilities from the get device id. */
2956                 intf->local_sel_device = 0;
2957                 intf->local_event_generator = 0;
2958                 intf->event_receiver = 0;
2959
2960                 /* Request the device info from the local MC. */
2961                 msg.netfn = IPMI_NETFN_APP_REQUEST;
2962                 msg.cmd = IPMI_GET_DEVICE_ID_CMD;
2963                 msg.data = NULL;
2964                 msg.data_len = 0;
2965                 intf->null_user_handler = device_id_fetcher;
2966                 i_ipmi_request(NULL,
2967                                intf,
2968                                &addr,
2969                                0,
2970                                &msg,
2971                                NULL,
2972                                &smi_msg,
2973                                &recv_msg,
2974                                0,
2975                                intf->my_address,
2976                                intf->my_lun,
2977                                0, 1); /* Don't retry, and don't wait. */
2978
2979                 if (intf->local_event_generator) {
2980                         /* Request the event receiver from the local MC. */
2981                         msg.netfn = IPMI_NETFN_SENSOR_EVENT_REQUEST;
2982                         msg.cmd = IPMI_GET_EVENT_RECEIVER_CMD;
2983                         msg.data = NULL;
2984                         msg.data_len = 0;
2985                         intf->null_user_handler = event_receiver_fetcher;
2986                         i_ipmi_request(NULL,
2987                                        intf,
2988                                        &addr,
2989                                        0,
2990                                        &msg,
2991                                        NULL,
2992                                        &smi_msg,
2993                                        &recv_msg,
2994                                        0,
2995                                        intf->my_address,
2996                                        intf->my_lun,
2997                                        0, 1); /* no retry, and no wait. */
2998                 }
2999                 intf->null_user_handler = NULL;
3000
3001                 /* Validate the event receiver.  The low bit must not
3002                    be 1 (it must be a valid IPMB address), it cannot
3003                    be zero, and it must not be my address. */
3004                 if (((intf->event_receiver & 1) == 0)
3005                     && (intf->event_receiver != 0)
3006                     && (intf->event_receiver != intf->my_address))
3007                 {
3008                         /* The event receiver is valid, send an IPMB
3009                            message. */
3010                         ipmb = (struct ipmi_ipmb_addr *) &addr;
3011                         ipmb->addr_type = IPMI_IPMB_ADDR_TYPE;
3012                         ipmb->channel = 0; /* FIXME - is this right? */
3013                         ipmb->lun = intf->event_receiver_lun;
3014                         ipmb->slave_addr = intf->event_receiver;
3015                 } else if (intf->local_sel_device) {
3016                         /* The event receiver was not valid (or was
3017                            me), but I am an SEL device, just dump it
3018                            in my SEL. */
3019                         si = (struct ipmi_system_interface_addr *) &addr;
3020                         si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3021                         si->channel = IPMI_BMC_CHANNEL;
3022                         si->lun = 0;
3023                 } else
3024                         continue; /* No where to send the event. */
3025
3026                 
3027                 msg.netfn = IPMI_NETFN_STORAGE_REQUEST; /* Storage. */
3028                 msg.cmd = IPMI_ADD_SEL_ENTRY_CMD;
3029                 msg.data = data;
3030                 msg.data_len = 16;
3031
3032                 j = 0;
3033                 while (*p) {
3034                         int size = strlen(p);
3035
3036                         if (size > 11)
3037                                 size = 11;
3038                         data[0] = 0;
3039                         data[1] = 0;
3040                         data[2] = 0xf0; /* OEM event without timestamp. */
3041                         data[3] = intf->my_address;
3042                         data[4] = j++; /* sequence # */
3043                         /* Always give 11 bytes, so strncpy will fill
3044                            it with zeroes for me. */
3045                         strncpy(data+5, p, 11);
3046                         p += size;
3047
3048                         i_ipmi_request(NULL,
3049                                        intf,
3050                                        &addr,
3051                                        0,
3052                                        &msg,
3053                                        NULL,
3054                                        &smi_msg,
3055                                        &recv_msg,
3056                                        0,
3057                                        intf->my_address,
3058                                        intf->my_lun,
3059                                        0, 1); /* no retry, and no wait. */
3060                 }
3061         }       
3062 #endif /* CONFIG_IPMI_PANIC_STRING */
3063 }
3064 #endif /* CONFIG_IPMI_PANIC_EVENT */
3065
3066 static int has_paniced = 0;
3067
3068 static int panic_event(struct notifier_block *this,
3069                        unsigned long         event,
3070                        void                  *ptr)
3071 {
3072         int        i;
3073         ipmi_smi_t intf;
3074
3075         if (has_paniced)
3076                 return NOTIFY_DONE;
3077         has_paniced = 1;
3078
3079         /* For every registered interface, set it to run to completion. */
3080         for (i=0; i<MAX_IPMI_INTERFACES; i++) {
3081                 intf = ipmi_interfaces[i];
3082                 if (intf == NULL)
3083                         continue;
3084
3085                 intf->handlers->set_run_to_completion(intf->send_info, 1);
3086         }
3087
3088 #ifdef CONFIG_IPMI_PANIC_EVENT
3089         send_panic_events(ptr);
3090 #endif
3091
3092         return NOTIFY_DONE;
3093 }
3094
3095 static struct notifier_block panic_block = {
3096         panic_event,
3097         NULL,
3098         200   /* priority: INT_MAX >= x >= 0 */
3099 };
3100
3101 static int ipmi_init_msghandler(void)
3102 {
3103         int i;
3104
3105         if (initialized)
3106                 return 0;
3107
3108         printk(KERN_INFO "ipmi message handler version "
3109                IPMI_MSGHANDLER_VERSION "\n");
3110
3111         for (i=0; i<MAX_IPMI_INTERFACES; i++) {
3112                 ipmi_interfaces[i] = NULL;
3113         }
3114
3115         proc_ipmi_root = proc_mkdir("ipmi", NULL);
3116         if (!proc_ipmi_root) {
3117             printk("Unable to create IPMI proc dir");
3118             return -ENOMEM;
3119         }
3120
3121         proc_ipmi_root->owner = THIS_MODULE;
3122
3123         init_timer(&ipmi_timer);
3124         ipmi_timer.data = 0;
3125         ipmi_timer.function = ipmi_timeout;
3126         ipmi_timer.expires = jiffies + IPMI_TIMEOUT_JIFFIES;
3127         add_timer(&ipmi_timer);
3128
3129         notifier_chain_register(&panic_notifier_list, &panic_block);
3130
3131         initialized = 1;
3132
3133         return 0;
3134 }
3135
3136 static __init int ipmi_init_msghandler_mod(void)
3137 {
3138         ipmi_init_msghandler();
3139         return 0;
3140 }
3141
3142 static __exit void cleanup_ipmi(void)
3143 {
3144         int count;
3145
3146         if (!initialized)
3147                 return;
3148
3149         notifier_chain_unregister(&panic_notifier_list, &panic_block);
3150
3151         /* This can't be called if any interfaces exist, so no worry about
3152            shutting down the interfaces. */
3153
3154         /* Tell the timer to stop, then wait for it to stop.  This avoids
3155            problems with race conditions removing the timer here. */
3156         stop_operation = 1;
3157         while (!timer_stopped) {
3158                 set_current_state(TASK_UNINTERRUPTIBLE);
3159                 schedule_timeout(1);
3160         }
3161
3162         remove_proc_entry(proc_ipmi_root->name, &proc_root);
3163
3164         initialized = 0;
3165
3166         /* Check for buffer leaks. */
3167         count = atomic_read(&smi_msg_inuse_count);
3168         if (count != 0)
3169                 printk("ipmi_msghandler: SMI message count %d at exit\n",
3170                        count);
3171         count = atomic_read(&recv_msg_inuse_count);
3172         if (count != 0)
3173                 printk("ipmi_msghandler: recv message count %d at exit\n",
3174                        count);
3175 }
3176 module_exit(cleanup_ipmi);
3177
3178 module_init(ipmi_init_msghandler_mod);
3179 MODULE_LICENSE("GPL");
3180
3181 EXPORT_SYMBOL(ipmi_alloc_recv_msg);
3182 EXPORT_SYMBOL(ipmi_create_user);
3183 EXPORT_SYMBOL(ipmi_destroy_user);
3184 EXPORT_SYMBOL(ipmi_get_version);
3185 EXPORT_SYMBOL(ipmi_request);
3186 EXPORT_SYMBOL(ipmi_request_settime);
3187 EXPORT_SYMBOL(ipmi_request_supply_msgs);
3188 EXPORT_SYMBOL(ipmi_request_with_source);
3189 EXPORT_SYMBOL(ipmi_poll_interface);
3190 EXPORT_SYMBOL(ipmi_register_smi);
3191 EXPORT_SYMBOL(ipmi_unregister_smi);
3192 EXPORT_SYMBOL(ipmi_register_for_cmd);
3193 EXPORT_SYMBOL(ipmi_unregister_for_cmd);
3194 EXPORT_SYMBOL(ipmi_smi_msg_received);
3195 EXPORT_SYMBOL(ipmi_smi_watchdog_pretimeout);
3196 EXPORT_SYMBOL(ipmi_alloc_smi_msg);
3197 EXPORT_SYMBOL(ipmi_register_all_cmd_rcvr);
3198 EXPORT_SYMBOL(ipmi_unregister_all_cmd_rcvr);
3199 EXPORT_SYMBOL(ipmi_addr_length);
3200 EXPORT_SYMBOL(ipmi_validate_addr);
3201 EXPORT_SYMBOL(ipmi_set_gets_events);
3202 EXPORT_SYMBOL(ipmi_addr_equal);
3203 EXPORT_SYMBOL(ipmi_smi_watcher_register);
3204 EXPORT_SYMBOL(ipmi_smi_watcher_unregister);
3205 EXPORT_SYMBOL(ipmi_set_my_address);
3206 EXPORT_SYMBOL(ipmi_get_my_address);
3207 EXPORT_SYMBOL(ipmi_set_my_LUN);
3208 EXPORT_SYMBOL(ipmi_get_my_LUN);
3209 EXPORT_SYMBOL(ipmi_smi_add_proc_entry);