patch-2_6_7-vs1_9_1_12
[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 "v31"
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 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 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 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 #if 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 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 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 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 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 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                         goto next_channel;
1652                 }
1653                 if (msg->rsp_size < 6) {
1654                         /* Message not big enough, just go on. */
1655                         goto next_channel;
1656                 }
1657                 chan = intf->curr_channel;
1658                 intf->channels[chan].medium = msg->rsp[4] & 0x7f;
1659                 intf->channels[chan].protocol = msg->rsp[5] & 0x1f;
1660
1661         next_channel:
1662                 intf->curr_channel++;
1663                 if (intf->curr_channel >= IPMI_MAX_CHANNELS)
1664                         wake_up(&intf->waitq);
1665                 else
1666                         rv = send_channel_info_cmd(intf, intf->curr_channel);
1667
1668                 if (rv) {
1669                         /* Got an error somehow, just give up. */
1670                         intf->curr_channel = IPMI_MAX_CHANNELS;
1671                         wake_up(&intf->waitq);
1672
1673                         printk(KERN_WARNING "ipmi_msghandler: Error sending"
1674                                "channel information: 0x%x\n",
1675                                rv);
1676                 }
1677         }
1678 }
1679
1680 int ipmi_register_smi(struct ipmi_smi_handlers *handlers,
1681                       void                     *send_info,
1682                       unsigned char            version_major,
1683                       unsigned char            version_minor,
1684                       ipmi_smi_t               *intf)
1685 {
1686         int              i, j;
1687         int              rv;
1688         ipmi_smi_t       new_intf;
1689         unsigned long    flags;
1690
1691
1692         /* Make sure the driver is actually initialized, this handles
1693            problems with initialization order. */
1694         if (!initialized) {
1695                 rv = ipmi_init_msghandler();
1696                 if (rv)
1697                         return rv;
1698                 /* The init code doesn't return an error if it was turned
1699                    off, but it won't initialize.  Check that. */
1700                 if (!initialized)
1701                         return -ENODEV;
1702         }
1703
1704         new_intf = kmalloc(sizeof(*new_intf), GFP_KERNEL);
1705         if (!new_intf)
1706                 return -ENOMEM;
1707         memset(new_intf, 0, sizeof(*new_intf));
1708
1709         new_intf->proc_dir = NULL;
1710
1711         rv = -ENOMEM;
1712
1713         down_write(&interfaces_sem);
1714         for (i=0; i<MAX_IPMI_INTERFACES; i++) {
1715                 if (ipmi_interfaces[i] == NULL) {
1716                         new_intf->intf_num = i;
1717                         new_intf->version_major = version_major;
1718                         new_intf->version_minor = version_minor;
1719                         new_intf->my_address = IPMI_BMC_SLAVE_ADDR;
1720                         new_intf->my_lun = 2;  /* the SMS LUN. */
1721                         rwlock_init(&(new_intf->users_lock));
1722                         INIT_LIST_HEAD(&(new_intf->users));
1723                         new_intf->handlers = handlers;
1724                         new_intf->send_info = send_info;
1725                         spin_lock_init(&(new_intf->seq_lock));
1726                         for (j=0; j<IPMI_IPMB_NUM_SEQ; j++) {
1727                                 new_intf->seq_table[j].inuse = 0;
1728                                 new_intf->seq_table[j].seqid = 0;
1729                         }
1730                         new_intf->curr_seq = 0;
1731                         spin_lock_init(&(new_intf->waiting_msgs_lock));
1732                         INIT_LIST_HEAD(&(new_intf->waiting_msgs));
1733                         spin_lock_init(&(new_intf->events_lock));
1734                         INIT_LIST_HEAD(&(new_intf->waiting_events));
1735                         new_intf->waiting_events_count = 0;
1736                         rwlock_init(&(new_intf->cmd_rcvr_lock));
1737                         init_waitqueue_head(&new_intf->waitq);
1738                         INIT_LIST_HEAD(&(new_intf->cmd_rcvrs));
1739                         new_intf->all_cmd_rcvr = NULL;
1740
1741                         spin_lock_init(&(new_intf->counter_lock));
1742
1743                         spin_lock_irqsave(&interfaces_lock, flags);
1744                         ipmi_interfaces[i] = new_intf;
1745                         spin_unlock_irqrestore(&interfaces_lock, flags);
1746
1747                         rv = 0;
1748                         *intf = new_intf;
1749                         break;
1750                 }
1751         }
1752
1753         downgrade_write(&interfaces_sem);
1754
1755         if (rv == 0)
1756                 rv = add_proc_entries(*intf, i);
1757
1758         if (rv == 0) {
1759                 if ((version_major > 1)
1760                     || ((version_major == 1) && (version_minor >= 5)))
1761                 {
1762                         /* Start scanning the channels to see what is
1763                            available. */
1764                         (*intf)->null_user_handler = channel_handler;
1765                         (*intf)->curr_channel = 0;
1766                         rv = send_channel_info_cmd(*intf, 0);
1767                         if (rv)
1768                                 goto out;
1769
1770                         /* Wait for the channel info to be read. */
1771                         up_read(&interfaces_sem);
1772                         wait_event((*intf)->waitq,
1773                                    ((*intf)->curr_channel>=IPMI_MAX_CHANNELS));
1774                         down_read(&interfaces_sem);
1775
1776                         if (ipmi_interfaces[i] != new_intf)
1777                                 /* Well, it went away.  Just return. */
1778                                 goto out;
1779                 } else {
1780                         /* Assume a single IPMB channel at zero. */
1781                         (*intf)->channels[0].medium = IPMI_CHANNEL_MEDIUM_IPMB;
1782                         (*intf)->channels[0].protocol
1783                                 = IPMI_CHANNEL_PROTOCOL_IPMB;
1784                 }
1785
1786                 /* Call all the watcher interfaces to tell
1787                    them that a new interface is available. */
1788                 call_smi_watchers(i);
1789         }
1790
1791  out:
1792         up_read(&interfaces_sem);
1793
1794         if (rv) {
1795                 if (new_intf->proc_dir)
1796                         remove_proc_entries(new_intf);
1797                 kfree(new_intf);
1798         }
1799
1800         return rv;
1801 }
1802
1803 static void free_recv_msg_list(struct list_head *q)
1804 {
1805         struct ipmi_recv_msg *msg, *msg2;
1806
1807         list_for_each_entry_safe(msg, msg2, q, link) {
1808                 list_del(&msg->link);
1809                 ipmi_free_recv_msg(msg);
1810         }
1811 }
1812
1813 static void free_cmd_rcvr_list(struct list_head *q)
1814 {
1815         struct cmd_rcvr  *rcvr, *rcvr2;
1816
1817         list_for_each_entry_safe(rcvr, rcvr2, q, link) {
1818                 list_del(&rcvr->link);
1819                 kfree(rcvr);
1820         }
1821 }
1822
1823 static void clean_up_interface_data(ipmi_smi_t intf)
1824 {
1825         int i;
1826
1827         free_recv_msg_list(&(intf->waiting_msgs));
1828         free_recv_msg_list(&(intf->waiting_events));
1829         free_cmd_rcvr_list(&(intf->cmd_rcvrs));
1830
1831         for (i=0; i<IPMI_IPMB_NUM_SEQ; i++) {
1832                 if ((intf->seq_table[i].inuse)
1833                     && (intf->seq_table[i].recv_msg))
1834                 {
1835                         ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
1836                 }       
1837         }
1838 }
1839
1840 int ipmi_unregister_smi(ipmi_smi_t intf)
1841 {
1842         int                     rv = -ENODEV;
1843         int                     i;
1844         struct ipmi_smi_watcher *w;
1845         unsigned long           flags;
1846
1847         down_write(&interfaces_sem);
1848         if (list_empty(&(intf->users)))
1849         {
1850                 for (i=0; i<MAX_IPMI_INTERFACES; i++) {
1851                         if (ipmi_interfaces[i] == intf) {
1852                                 remove_proc_entries(intf);
1853                                 spin_lock_irqsave(&interfaces_lock, flags);
1854                                 ipmi_interfaces[i] = NULL;
1855                                 clean_up_interface_data(intf);
1856                                 spin_unlock_irqrestore(&interfaces_lock,flags);
1857                                 kfree(intf);
1858                                 rv = 0;
1859                                 goto out_call_watcher;
1860                         }
1861                 }
1862         } else {
1863                 rv = -EBUSY;
1864         }
1865         up_write(&interfaces_sem);
1866
1867         return rv;
1868
1869  out_call_watcher:
1870         downgrade_write(&interfaces_sem);
1871
1872         /* Call all the watcher interfaces to tell them that
1873            an interface is gone. */
1874         down_read(&smi_watchers_sem);
1875         list_for_each_entry(w, &smi_watchers, link) {
1876                 w->smi_gone(i);
1877         }
1878         up_read(&smi_watchers_sem);
1879         up_read(&interfaces_sem);
1880         return 0;
1881 }
1882
1883 static int handle_ipmb_get_msg_rsp(ipmi_smi_t          intf,
1884                                    struct ipmi_smi_msg *msg)
1885 {
1886         struct ipmi_ipmb_addr ipmb_addr;
1887         struct ipmi_recv_msg  *recv_msg;
1888         unsigned long         flags;
1889
1890         
1891         /* This is 11, not 10, because the response must contain a
1892          * completion code. */
1893         if (msg->rsp_size < 11) {
1894                 /* Message not big enough, just ignore it. */
1895                 spin_lock_irqsave(&intf->counter_lock, flags);
1896                 intf->invalid_ipmb_responses++;
1897                 spin_unlock_irqrestore(&intf->counter_lock, flags);
1898                 return 0;
1899         }
1900
1901         if (msg->rsp[2] != 0) {
1902                 /* An error getting the response, just ignore it. */
1903                 return 0;
1904         }
1905
1906         ipmb_addr.addr_type = IPMI_IPMB_ADDR_TYPE;
1907         ipmb_addr.slave_addr = msg->rsp[6];
1908         ipmb_addr.channel = msg->rsp[3] & 0x0f;
1909         ipmb_addr.lun = msg->rsp[7] & 3;
1910
1911         /* It's a response from a remote entity.  Look up the sequence
1912            number and handle the response. */
1913         if (intf_find_seq(intf,
1914                           msg->rsp[7] >> 2,
1915                           msg->rsp[3] & 0x0f,
1916                           msg->rsp[8],
1917                           (msg->rsp[4] >> 2) & (~1),
1918                           (struct ipmi_addr *) &(ipmb_addr),
1919                           &recv_msg))
1920         {
1921                 /* We were unable to find the sequence number,
1922                    so just nuke the message. */
1923                 spin_lock_irqsave(&intf->counter_lock, flags);
1924                 intf->unhandled_ipmb_responses++;
1925                 spin_unlock_irqrestore(&intf->counter_lock, flags);
1926                 return 0;
1927         }
1928
1929         memcpy(recv_msg->msg_data,
1930                &(msg->rsp[9]),
1931                msg->rsp_size - 9);
1932         /* THe other fields matched, so no need to set them, except
1933            for netfn, which needs to be the response that was
1934            returned, not the request value. */
1935         recv_msg->msg.netfn = msg->rsp[4] >> 2;
1936         recv_msg->msg.data = recv_msg->msg_data;
1937         recv_msg->msg.data_len = msg->rsp_size - 10;
1938         recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
1939         spin_lock_irqsave(&intf->counter_lock, flags);
1940         intf->handled_ipmb_responses++;
1941         spin_unlock_irqrestore(&intf->counter_lock, flags);
1942         deliver_response(recv_msg);
1943
1944         return 0;
1945 }
1946
1947 static int handle_ipmb_get_msg_cmd(ipmi_smi_t          intf,
1948                                    struct ipmi_smi_msg *msg)
1949 {
1950         struct cmd_rcvr       *rcvr;
1951         int                   rv = 0;
1952         unsigned char         netfn;
1953         unsigned char         cmd;
1954         ipmi_user_t           user = NULL;
1955         struct ipmi_ipmb_addr *ipmb_addr;
1956         struct ipmi_recv_msg  *recv_msg;
1957         unsigned long         flags;
1958
1959         if (msg->rsp_size < 10) {
1960                 /* Message not big enough, just ignore it. */
1961                 spin_lock_irqsave(&intf->counter_lock, flags);
1962                 intf->invalid_commands++;
1963                 spin_unlock_irqrestore(&intf->counter_lock, flags);
1964                 return 0;
1965         }
1966
1967         if (msg->rsp[2] != 0) {
1968                 /* An error getting the response, just ignore it. */
1969                 return 0;
1970         }
1971
1972         netfn = msg->rsp[4] >> 2;
1973         cmd = msg->rsp[8];
1974
1975         read_lock(&(intf->cmd_rcvr_lock));
1976         
1977         if (intf->all_cmd_rcvr) {
1978                 user = intf->all_cmd_rcvr;
1979         } else {
1980                 /* Find the command/netfn. */
1981                 list_for_each_entry(rcvr, &(intf->cmd_rcvrs), link) {
1982                         if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)) {
1983                                 user = rcvr->user;
1984                                 break;
1985                         }
1986                 }
1987         }
1988         read_unlock(&(intf->cmd_rcvr_lock));
1989
1990         if (user == NULL) {
1991                 /* We didn't find a user, deliver an error response. */
1992                 spin_lock_irqsave(&intf->counter_lock, flags);
1993                 intf->unhandled_commands++;
1994                 spin_unlock_irqrestore(&intf->counter_lock, flags);
1995
1996                 msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
1997                 msg->data[1] = IPMI_SEND_MSG_CMD;
1998                 msg->data[2] = msg->rsp[3];
1999                 msg->data[3] = msg->rsp[6];
2000                 msg->data[4] = ((netfn + 1) << 2) | (msg->rsp[7] & 0x3);
2001                 msg->data[5] = ipmb_checksum(&(msg->data[3]), 2);
2002                 msg->data[6] = intf->my_address;
2003                 /* rqseq/lun */
2004                 msg->data[7] = (msg->rsp[7] & 0xfc) | (msg->rsp[4] & 0x3);
2005                 msg->data[8] = msg->rsp[8]; /* cmd */
2006                 msg->data[9] = IPMI_INVALID_CMD_COMPLETION_CODE;
2007                 msg->data[10] = ipmb_checksum(&(msg->data[6]), 4);
2008                 msg->data_size = 11;
2009
2010 #if DEBUG_MSGING
2011         {
2012                 int m;
2013                 printk("Invalid command:");
2014                 for (m=0; m<msg->data_size; m++)
2015                         printk(" %2.2x", msg->data[m]);
2016                 printk("\n");
2017         }
2018 #endif
2019                 intf->handlers->sender(intf->send_info, msg, 0);
2020
2021                 rv = -1; /* We used the message, so return the value that
2022                             causes it to not be freed or queued. */
2023         } else {
2024                 /* Deliver the message to the user. */
2025                 spin_lock_irqsave(&intf->counter_lock, flags);
2026                 intf->handled_commands++;
2027                 spin_unlock_irqrestore(&intf->counter_lock, flags);
2028
2029                 recv_msg = ipmi_alloc_recv_msg();
2030                 if (! recv_msg) {
2031                         /* We couldn't allocate memory for the
2032                            message, so requeue it for handling
2033                            later. */
2034                         rv = 1;
2035                 } else {
2036                         /* Extract the source address from the data. */
2037                         ipmb_addr = (struct ipmi_ipmb_addr *) &recv_msg->addr;
2038                         ipmb_addr->addr_type = IPMI_IPMB_ADDR_TYPE;
2039                         ipmb_addr->slave_addr = msg->rsp[6];
2040                         ipmb_addr->lun = msg->rsp[7] & 3;
2041                         ipmb_addr->channel = msg->rsp[3] & 0xf;
2042
2043                         /* Extract the rest of the message information
2044                            from the IPMB header.*/
2045                         recv_msg->user = user;
2046                         recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
2047                         recv_msg->msgid = msg->rsp[7] >> 2;
2048                         recv_msg->msg.netfn = msg->rsp[4] >> 2;
2049                         recv_msg->msg.cmd = msg->rsp[8];
2050                         recv_msg->msg.data = recv_msg->msg_data;
2051
2052                         /* We chop off 10, not 9 bytes because the checksum
2053                            at the end also needs to be removed. */
2054                         recv_msg->msg.data_len = msg->rsp_size - 10;
2055                         memcpy(recv_msg->msg_data,
2056                                &(msg->rsp[9]),
2057                                msg->rsp_size - 10);
2058                         deliver_response(recv_msg);
2059                 }
2060         }
2061
2062         return rv;
2063 }
2064
2065 static int handle_lan_get_msg_rsp(ipmi_smi_t          intf,
2066                                   struct ipmi_smi_msg *msg)
2067 {
2068         struct ipmi_lan_addr  lan_addr;
2069         struct ipmi_recv_msg  *recv_msg;
2070         unsigned long         flags;
2071
2072
2073         /* This is 13, not 12, because the response must contain a
2074          * completion code. */
2075         if (msg->rsp_size < 13) {
2076                 /* Message not big enough, just ignore it. */
2077                 spin_lock_irqsave(&intf->counter_lock, flags);
2078                 intf->invalid_lan_responses++;
2079                 spin_unlock_irqrestore(&intf->counter_lock, flags);
2080                 return 0;
2081         }
2082
2083         if (msg->rsp[2] != 0) {
2084                 /* An error getting the response, just ignore it. */
2085                 return 0;
2086         }
2087
2088         lan_addr.addr_type = IPMI_LAN_ADDR_TYPE;
2089         lan_addr.session_handle = msg->rsp[4];
2090         lan_addr.remote_SWID = msg->rsp[8];
2091         lan_addr.local_SWID = msg->rsp[5];
2092         lan_addr.channel = msg->rsp[3] & 0x0f;
2093         lan_addr.privilege = msg->rsp[3] >> 4;
2094         lan_addr.lun = msg->rsp[9] & 3;
2095
2096         /* It's a response from a remote entity.  Look up the sequence
2097            number and handle the response. */
2098         if (intf_find_seq(intf,
2099                           msg->rsp[9] >> 2,
2100                           msg->rsp[3] & 0x0f,
2101                           msg->rsp[10],
2102                           (msg->rsp[6] >> 2) & (~1),
2103                           (struct ipmi_addr *) &(lan_addr),
2104                           &recv_msg))
2105         {
2106                 /* We were unable to find the sequence number,
2107                    so just nuke the message. */
2108                 spin_lock_irqsave(&intf->counter_lock, flags);
2109                 intf->unhandled_lan_responses++;
2110                 spin_unlock_irqrestore(&intf->counter_lock, flags);
2111                 return 0;
2112         }
2113
2114         memcpy(recv_msg->msg_data,
2115                &(msg->rsp[11]),
2116                msg->rsp_size - 11);
2117         /* The other fields matched, so no need to set them, except
2118            for netfn, which needs to be the response that was
2119            returned, not the request value. */
2120         recv_msg->msg.netfn = msg->rsp[6] >> 2;
2121         recv_msg->msg.data = recv_msg->msg_data;
2122         recv_msg->msg.data_len = msg->rsp_size - 12;
2123         recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
2124         spin_lock_irqsave(&intf->counter_lock, flags);
2125         intf->handled_lan_responses++;
2126         spin_unlock_irqrestore(&intf->counter_lock, flags);
2127         deliver_response(recv_msg);
2128
2129         return 0;
2130 }
2131
2132 static int handle_lan_get_msg_cmd(ipmi_smi_t          intf,
2133                                   struct ipmi_smi_msg *msg)
2134 {
2135         struct cmd_rcvr       *rcvr;
2136         int                   rv = 0;
2137         unsigned char         netfn;
2138         unsigned char         cmd;
2139         ipmi_user_t           user = NULL;
2140         struct ipmi_lan_addr  *lan_addr;
2141         struct ipmi_recv_msg  *recv_msg;
2142         unsigned long         flags;
2143
2144         if (msg->rsp_size < 12) {
2145                 /* Message not big enough, just ignore it. */
2146                 spin_lock_irqsave(&intf->counter_lock, flags);
2147                 intf->invalid_commands++;
2148                 spin_unlock_irqrestore(&intf->counter_lock, flags);
2149                 return 0;
2150         }
2151
2152         if (msg->rsp[2] != 0) {
2153                 /* An error getting the response, just ignore it. */
2154                 return 0;
2155         }
2156
2157         netfn = msg->rsp[6] >> 2;
2158         cmd = msg->rsp[10];
2159
2160         read_lock(&(intf->cmd_rcvr_lock));
2161
2162         if (intf->all_cmd_rcvr) {
2163                 user = intf->all_cmd_rcvr;
2164         } else {
2165                 /* Find the command/netfn. */
2166                 list_for_each_entry(rcvr, &(intf->cmd_rcvrs), link) {
2167                         if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)) {
2168                                 user = rcvr->user;
2169                                 break;
2170                         }
2171                 }
2172         }
2173         read_unlock(&(intf->cmd_rcvr_lock));
2174
2175         if (user == NULL) {
2176                 /* We didn't find a user, deliver an error response. */
2177                 spin_lock_irqsave(&intf->counter_lock, flags);
2178                 intf->unhandled_commands++;
2179                 spin_unlock_irqrestore(&intf->counter_lock, flags);
2180
2181                 rv = 0; /* Don't do anything with these messages, just
2182                            allow them to be freed. */
2183         } else {
2184                 /* Deliver the message to the user. */
2185                 spin_lock_irqsave(&intf->counter_lock, flags);
2186                 intf->handled_commands++;
2187                 spin_unlock_irqrestore(&intf->counter_lock, flags);
2188
2189                 recv_msg = ipmi_alloc_recv_msg();
2190                 if (! recv_msg) {
2191                         /* We couldn't allocate memory for the
2192                            message, so requeue it for handling
2193                            later. */
2194                         rv = 1;
2195                 } else {
2196                         /* Extract the source address from the data. */
2197                         lan_addr = (struct ipmi_lan_addr *) &recv_msg->addr;
2198                         lan_addr->addr_type = IPMI_LAN_ADDR_TYPE;
2199                         lan_addr->session_handle = msg->rsp[4];
2200                         lan_addr->remote_SWID = msg->rsp[8];
2201                         lan_addr->local_SWID = msg->rsp[5];
2202                         lan_addr->lun = msg->rsp[9] & 3;
2203                         lan_addr->channel = msg->rsp[3] & 0xf;
2204                         lan_addr->privilege = msg->rsp[3] >> 4;
2205
2206                         /* Extract the rest of the message information
2207                            from the IPMB header.*/
2208                         recv_msg->user = user;
2209                         recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
2210                         recv_msg->msgid = msg->rsp[9] >> 2;
2211                         recv_msg->msg.netfn = msg->rsp[6] >> 2;
2212                         recv_msg->msg.cmd = msg->rsp[10];
2213                         recv_msg->msg.data = recv_msg->msg_data;
2214
2215                         /* We chop off 12, not 11 bytes because the checksum
2216                            at the end also needs to be removed. */
2217                         recv_msg->msg.data_len = msg->rsp_size - 12;
2218                         memcpy(recv_msg->msg_data,
2219                                &(msg->rsp[11]),
2220                                msg->rsp_size - 12);
2221                         deliver_response(recv_msg);
2222                 }
2223         }
2224
2225         return rv;
2226 }
2227
2228 static void copy_event_into_recv_msg(struct ipmi_recv_msg *recv_msg,
2229                                      struct ipmi_smi_msg  *msg)
2230 {
2231         struct ipmi_system_interface_addr *smi_addr;
2232         
2233         recv_msg->msgid = 0;
2234         smi_addr = (struct ipmi_system_interface_addr *) &(recv_msg->addr);
2235         smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2236         smi_addr->channel = IPMI_BMC_CHANNEL;
2237         smi_addr->lun = msg->rsp[0] & 3;
2238         recv_msg->recv_type = IPMI_ASYNC_EVENT_RECV_TYPE;
2239         recv_msg->msg.netfn = msg->rsp[0] >> 2;
2240         recv_msg->msg.cmd = msg->rsp[1];
2241         memcpy(recv_msg->msg_data, &(msg->rsp[3]), msg->rsp_size - 3);
2242         recv_msg->msg.data = recv_msg->msg_data;
2243         recv_msg->msg.data_len = msg->rsp_size - 3;
2244 }
2245
2246 /* This will be called with the intf->users_lock read-locked, so no need
2247    to do that here. */
2248 static int handle_read_event_rsp(ipmi_smi_t          intf,
2249                                  struct ipmi_smi_msg *msg)
2250 {
2251         struct ipmi_recv_msg *recv_msg, *recv_msg2;
2252         struct list_head     msgs;
2253         ipmi_user_t          user;
2254         int                  rv = 0;
2255         int                  deliver_count = 0;
2256         unsigned long        flags;
2257
2258         if (msg->rsp_size < 19) {
2259                 /* Message is too small to be an IPMB event. */
2260                 spin_lock_irqsave(&intf->counter_lock, flags);
2261                 intf->invalid_events++;
2262                 spin_unlock_irqrestore(&intf->counter_lock, flags);
2263                 return 0;
2264         }
2265
2266         if (msg->rsp[2] != 0) {
2267                 /* An error getting the event, just ignore it. */
2268                 return 0;
2269         }
2270
2271         INIT_LIST_HEAD(&msgs);
2272
2273         spin_lock_irqsave(&(intf->events_lock), flags);
2274
2275         spin_lock(&intf->counter_lock);
2276         intf->events++;
2277         spin_unlock(&intf->counter_lock);
2278
2279         /* Allocate and fill in one message for every user that is getting
2280            events. */
2281         list_for_each_entry(user, &(intf->users), link) {
2282                 if (! user->gets_events)
2283                         continue;
2284
2285                 recv_msg = ipmi_alloc_recv_msg();
2286                 if (! recv_msg) {
2287                         list_for_each_entry_safe(recv_msg, recv_msg2, &msgs, link) {
2288                                 list_del(&recv_msg->link);
2289                                 ipmi_free_recv_msg(recv_msg);
2290                         }
2291                         /* We couldn't allocate memory for the
2292                            message, so requeue it for handling
2293                            later. */
2294                         rv = 1;
2295                         goto out;
2296                 }
2297
2298                 deliver_count++;
2299
2300                 copy_event_into_recv_msg(recv_msg, msg);
2301                 recv_msg->user = user;
2302                 list_add_tail(&(recv_msg->link), &msgs);
2303         }
2304
2305         if (deliver_count) {
2306                 /* Now deliver all the messages. */
2307                 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs, link) {
2308                         list_del(&recv_msg->link);
2309                         deliver_response(recv_msg);
2310                 }
2311         } else if (intf->waiting_events_count < MAX_EVENTS_IN_QUEUE) {
2312                 /* No one to receive the message, put it in queue if there's
2313                    not already too many things in the queue. */
2314                 recv_msg = ipmi_alloc_recv_msg();
2315                 if (! recv_msg) {
2316                         /* We couldn't allocate memory for the
2317                            message, so requeue it for handling
2318                            later. */
2319                         rv = 1;
2320                         goto out;
2321                 }
2322
2323                 copy_event_into_recv_msg(recv_msg, msg);
2324                 list_add_tail(&(recv_msg->link), &(intf->waiting_events));
2325         } else {
2326                 /* There's too many things in the queue, discard this
2327                    message. */
2328                 printk(KERN_WARNING "ipmi: Event queue full, discarding an"
2329                        " incoming event\n");
2330         }
2331
2332  out:
2333         spin_unlock_irqrestore(&(intf->events_lock), flags);
2334
2335         return rv;
2336 }
2337
2338 static int handle_bmc_rsp(ipmi_smi_t          intf,
2339                           struct ipmi_smi_msg *msg)
2340 {
2341         struct ipmi_recv_msg *recv_msg;
2342         int                  found = 0;
2343         struct ipmi_user     *user;
2344         unsigned long        flags;
2345
2346         recv_msg = (struct ipmi_recv_msg *) msg->user_data;
2347
2348         /* Make sure the user still exists. */
2349         list_for_each_entry(user, &(intf->users), link) {
2350                 if (user == recv_msg->user) {
2351                         /* Found it, so we can deliver it */
2352                         found = 1;
2353                         break;
2354                 }
2355         }
2356
2357         if (!found) {
2358                 /* Special handling for NULL users. */
2359                 if (!recv_msg->user && intf->null_user_handler)
2360                         intf->null_user_handler(intf, msg);
2361                 /* The user for the message went away, so give up. */
2362                 spin_lock_irqsave(&intf->counter_lock, flags);
2363                 intf->unhandled_local_responses++;
2364                 spin_unlock_irqrestore(&intf->counter_lock, flags);
2365                 ipmi_free_recv_msg(recv_msg);
2366         } else {
2367                 struct ipmi_system_interface_addr *smi_addr;
2368
2369                 spin_lock_irqsave(&intf->counter_lock, flags);
2370                 intf->handled_local_responses++;
2371                 spin_unlock_irqrestore(&intf->counter_lock, flags);
2372                 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
2373                 recv_msg->msgid = msg->msgid;
2374                 smi_addr = ((struct ipmi_system_interface_addr *)
2375                             &(recv_msg->addr));
2376                 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2377                 smi_addr->channel = IPMI_BMC_CHANNEL;
2378                 smi_addr->lun = msg->rsp[0] & 3;
2379                 recv_msg->msg.netfn = msg->rsp[0] >> 2;
2380                 recv_msg->msg.cmd = msg->rsp[1];
2381                 memcpy(recv_msg->msg_data,
2382                        &(msg->rsp[2]),
2383                        msg->rsp_size - 2);
2384                 recv_msg->msg.data = recv_msg->msg_data;
2385                 recv_msg->msg.data_len = msg->rsp_size - 2;
2386                 deliver_response(recv_msg);
2387         }
2388
2389         return 0;
2390 }
2391
2392 /* Handle a new message.  Return 1 if the message should be requeued,
2393    0 if the message should be freed, or -1 if the message should not
2394    be freed or requeued. */
2395 static int handle_new_recv_msg(ipmi_smi_t          intf,
2396                                struct ipmi_smi_msg *msg)
2397 {
2398         int requeue;
2399         int chan;
2400
2401 #if DEBUG_MSGING
2402         int m;
2403         printk("Recv:");
2404         for (m=0; m<msg->rsp_size; m++)
2405                 printk(" %2.2x", msg->rsp[m]);
2406         printk("\n");
2407 #endif
2408         if (msg->rsp_size < 2) {
2409                 /* Message is too small to be correct. */
2410                 requeue = 0;
2411         } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
2412                    && (msg->rsp[1] == IPMI_SEND_MSG_CMD)
2413                    && (msg->user_data != NULL))
2414         {
2415                 /* It's a response to a response we sent.  For this we
2416                    deliver a send message response to the user. */
2417                 struct ipmi_recv_msg *recv_msg = msg->user_data;
2418
2419                 requeue = 0;
2420                 if (msg->rsp_size < 2)
2421                         /* Message is too small to be correct. */
2422                         goto out;
2423
2424                 chan = msg->data[2] & 0x0f;
2425                 if (chan >= IPMI_MAX_CHANNELS)
2426                         /* Invalid channel number */
2427                         goto out;
2428
2429                 if (recv_msg) {
2430                         recv_msg->recv_type = IPMI_RESPONSE_RESPONSE_TYPE;
2431                         recv_msg->msg.data = recv_msg->msg_data;
2432                         recv_msg->msg.data_len = 1;
2433                         recv_msg->msg_data[0] = msg->rsp[2];
2434                         deliver_response(recv_msg);
2435                 }
2436         } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
2437                    && (msg->rsp[1] == IPMI_GET_MSG_CMD))
2438         {
2439                 /* It's from the receive queue. */
2440                 chan = msg->rsp[3] & 0xf;
2441                 if (chan >= IPMI_MAX_CHANNELS) {
2442                         /* Invalid channel number */
2443                         requeue = 0;
2444                         goto out;
2445                 }
2446
2447                 switch (intf->channels[chan].medium) {
2448                 case IPMI_CHANNEL_MEDIUM_IPMB:
2449                         if (msg->rsp[4] & 0x04) {
2450                                 /* It's a response, so find the
2451                                    requesting message and send it up. */
2452                                 requeue = handle_ipmb_get_msg_rsp(intf, msg);
2453                         } else {
2454                                 /* It's a command to the SMS from some other
2455                                    entity.  Handle that. */
2456                                 requeue = handle_ipmb_get_msg_cmd(intf, msg);
2457                         }
2458                         break;
2459
2460                 case IPMI_CHANNEL_MEDIUM_8023LAN:
2461                 case IPMI_CHANNEL_MEDIUM_ASYNC:
2462                         if (msg->rsp[6] & 0x04) {
2463                                 /* It's a response, so find the
2464                                    requesting message and send it up. */
2465                                 requeue = handle_lan_get_msg_rsp(intf, msg);
2466                         } else {
2467                                 /* It's a command to the SMS from some other
2468                                    entity.  Handle that. */
2469                                 requeue = handle_lan_get_msg_cmd(intf, msg);
2470                         }
2471                         break;
2472
2473                 default:
2474                         /* We don't handle the channel type, so just
2475                          * free the message. */
2476                         requeue = 0;
2477                 }
2478
2479         } else if (msg->rsp[1] == IPMI_READ_EVENT_MSG_BUFFER_CMD) {
2480                 /* It's an asyncronous event. */
2481                 requeue = handle_read_event_rsp(intf, msg);
2482         } else {
2483                 /* It's a response from the local BMC. */
2484                 requeue = handle_bmc_rsp(intf, msg);
2485         }
2486
2487  out:
2488         return requeue;
2489 }
2490
2491 /* Handle a new message from the lower layer. */
2492 void ipmi_smi_msg_received(ipmi_smi_t          intf,
2493                            struct ipmi_smi_msg *msg)
2494 {
2495         unsigned long flags;
2496         int           rv;
2497
2498
2499         /* Lock the user lock so the user can't go away while we are
2500            working on it. */
2501         read_lock(&(intf->users_lock));
2502
2503         if ((msg->data_size >= 2)
2504             && (msg->data[0] == (IPMI_NETFN_APP_REQUEST << 2))
2505             && (msg->data[1] == IPMI_SEND_MSG_CMD)
2506             && (msg->user_data == NULL)) {
2507                 /* This is the local response to a command send, start
2508                    the timer for these.  The user_data will not be
2509                    NULL if this is a response send, and we will let
2510                    response sends just go through. */
2511
2512                 /* Check for errors, if we get certain errors (ones
2513                    that mean basically we can try again later), we
2514                    ignore them and start the timer.  Otherwise we
2515                    report the error immediately. */
2516                 if ((msg->rsp_size >= 3) && (msg->rsp[2] != 0)
2517                     && (msg->rsp[2] != IPMI_NODE_BUSY_ERR)
2518                     && (msg->rsp[2] != IPMI_LOST_ARBITRATION_ERR))
2519                 {
2520                         int chan = msg->rsp[3] & 0xf;
2521
2522                         /* Got an error sending the message, handle it. */
2523                         spin_lock_irqsave(&intf->counter_lock, flags);
2524                         if (chan >= IPMI_MAX_CHANNELS)
2525                                 ; /* This shouldn't happen */
2526                         else if ((intf->channels[chan].medium
2527                                   == IPMI_CHANNEL_MEDIUM_8023LAN)
2528                                  || (intf->channels[chan].medium
2529                                      == IPMI_CHANNEL_MEDIUM_ASYNC))
2530                                 intf->sent_lan_command_errs++;
2531                         else
2532                                 intf->sent_ipmb_command_errs++;
2533                         spin_unlock_irqrestore(&intf->counter_lock, flags);
2534                         intf_err_seq(intf, msg->msgid, msg->rsp[2]);
2535                 } else {
2536                         /* The message was sent, start the timer. */
2537                         intf_start_seq_timer(intf, msg->msgid);
2538                 }
2539
2540                 ipmi_free_smi_msg(msg);
2541                 goto out_unlock;
2542         }
2543
2544         /* To preserve message order, if the list is not empty, we
2545            tack this message onto the end of the list. */
2546         spin_lock_irqsave(&(intf->waiting_msgs_lock), flags);
2547         if (!list_empty(&(intf->waiting_msgs))) {
2548                 list_add_tail(&(msg->link), &(intf->waiting_msgs));
2549                 spin_unlock(&(intf->waiting_msgs_lock));
2550                 goto out_unlock;
2551         }
2552         spin_unlock_irqrestore(&(intf->waiting_msgs_lock), flags);
2553                 
2554         rv = handle_new_recv_msg(intf, msg);
2555         if (rv > 0) {
2556                 /* Could not handle the message now, just add it to a
2557                    list to handle later. */
2558                 spin_lock(&(intf->waiting_msgs_lock));
2559                 list_add_tail(&(msg->link), &(intf->waiting_msgs));
2560                 spin_unlock(&(intf->waiting_msgs_lock));
2561         } else if (rv == 0) {
2562                 ipmi_free_smi_msg(msg);
2563         }
2564
2565  out_unlock:
2566         read_unlock(&(intf->users_lock));
2567 }
2568
2569 void ipmi_smi_watchdog_pretimeout(ipmi_smi_t intf)
2570 {
2571         ipmi_user_t user;
2572
2573         read_lock(&(intf->users_lock));
2574         list_for_each_entry(user, &(intf->users), link) {
2575                 if (! user->handler->ipmi_watchdog_pretimeout)
2576                         continue;
2577
2578                 user->handler->ipmi_watchdog_pretimeout(user->handler_data);
2579         }
2580         read_unlock(&(intf->users_lock));
2581 }
2582
2583 static void
2584 handle_msg_timeout(struct ipmi_recv_msg *msg)
2585 {
2586         msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
2587         msg->msg_data[0] = IPMI_TIMEOUT_COMPLETION_CODE;
2588         msg->msg.netfn |= 1; /* Convert to a response. */
2589         msg->msg.data_len = 1;
2590         msg->msg.data = msg->msg_data;
2591         deliver_response(msg);
2592 }
2593
2594 static void
2595 send_from_recv_msg(ipmi_smi_t intf, struct ipmi_recv_msg *recv_msg,
2596                    struct ipmi_smi_msg *smi_msg,
2597                    unsigned char seq, long seqid)
2598 {
2599         if (!smi_msg)
2600                 smi_msg = ipmi_alloc_smi_msg();
2601         if (!smi_msg)
2602                 /* If we can't allocate the message, then just return, we
2603                    get 4 retries, so this should be ok. */
2604                 return;
2605
2606         memcpy(smi_msg->data, recv_msg->msg.data, recv_msg->msg.data_len);
2607         smi_msg->data_size = recv_msg->msg.data_len;
2608         smi_msg->msgid = STORE_SEQ_IN_MSGID(seq, seqid);
2609                 
2610         /* Send the new message.  We send with a zero priority.  It
2611            timed out, I doubt time is that critical now, and high
2612            priority messages are really only for messages to the local
2613            MC, which don't get resent. */
2614         intf->handlers->sender(intf->send_info, smi_msg, 0);
2615
2616 #if DEBUG_MSGING
2617         {
2618                 int m;
2619                 printk("Resend: ");
2620                 for (m=0; m<smi_msg->data_size; m++)
2621                         printk(" %2.2x", smi_msg->data[m]);
2622                 printk("\n");
2623         }
2624 #endif
2625 }
2626
2627 static void
2628 ipmi_timeout_handler(long timeout_period)
2629 {
2630         ipmi_smi_t           intf;
2631         struct list_head     timeouts;
2632         struct ipmi_recv_msg *msg, *msg2;
2633         struct ipmi_smi_msg  *smi_msg, *smi_msg2;
2634         unsigned long        flags;
2635         int                  i, j;
2636
2637         INIT_LIST_HEAD(&timeouts);
2638
2639         spin_lock(&interfaces_lock);
2640         for (i=0; i<MAX_IPMI_INTERFACES; i++) {
2641                 intf = ipmi_interfaces[i];
2642                 if (intf == NULL)
2643                         continue;
2644
2645                 read_lock(&(intf->users_lock));
2646
2647                 /* See if any waiting messages need to be processed. */
2648                 spin_lock_irqsave(&(intf->waiting_msgs_lock), flags);
2649                 list_for_each_entry_safe(smi_msg, smi_msg2, &(intf->waiting_msgs), link) {
2650                         if (! handle_new_recv_msg(intf, smi_msg)) {
2651                                 list_del(&smi_msg->link);
2652                                 ipmi_free_smi_msg(smi_msg);
2653                         } else {
2654                                 /* To preserve message order, quit if we
2655                                    can't handle a message. */
2656                                 break;
2657                         }
2658                 }
2659                 spin_unlock_irqrestore(&(intf->waiting_msgs_lock), flags);
2660
2661                 /* Go through the seq table and find any messages that
2662                    have timed out, putting them in the timeouts
2663                    list. */
2664                 spin_lock_irqsave(&(intf->seq_lock), flags);
2665                 for (j=0; j<IPMI_IPMB_NUM_SEQ; j++) {
2666                         struct seq_table *ent = &(intf->seq_table[j]);
2667                         if (!ent->inuse)
2668                                 continue;
2669
2670                         ent->timeout -= timeout_period;
2671                         if (ent->timeout > 0)
2672                                 continue;
2673
2674                         if (ent->retries_left == 0) {
2675                                 /* The message has used all its retries. */
2676                                 ent->inuse = 0;
2677                                 msg = ent->recv_msg;
2678                                 list_add_tail(&(msg->link), &timeouts);
2679                                 spin_lock(&intf->counter_lock);
2680                                 if (ent->broadcast)
2681                                         intf->timed_out_ipmb_broadcasts++;
2682                                 else if (ent->recv_msg->addr.addr_type
2683                                          == IPMI_LAN_ADDR_TYPE)
2684                                         intf->timed_out_lan_commands++;
2685                                 else
2686                                         intf->timed_out_ipmb_commands++;
2687                                 spin_unlock(&intf->counter_lock);
2688                         } else {
2689                                 /* More retries, send again. */
2690
2691                                 /* Start with the max timer, set to normal
2692                                    timer after the message is sent. */
2693                                 ent->timeout = MAX_MSG_TIMEOUT;
2694                                 ent->retries_left--;
2695                                 send_from_recv_msg(intf, ent->recv_msg, NULL,
2696                                                    j, ent->seqid);
2697                                 spin_lock(&intf->counter_lock);
2698                                 if (ent->recv_msg->addr.addr_type
2699                                     == IPMI_LAN_ADDR_TYPE)
2700                                         intf->retransmitted_lan_commands++;
2701                                 else
2702                                         intf->retransmitted_ipmb_commands++;
2703                                 spin_unlock(&intf->counter_lock);
2704                         }
2705                 }
2706                 spin_unlock_irqrestore(&(intf->seq_lock), flags);
2707
2708                 list_for_each_entry_safe(msg, msg2, &timeouts, link) {
2709                         handle_msg_timeout(msg);
2710                 }
2711
2712                 read_unlock(&(intf->users_lock));
2713         }
2714         spin_unlock(&interfaces_lock);
2715 }
2716
2717 static void ipmi_request_event(void)
2718 {
2719         ipmi_smi_t intf;
2720         int        i;
2721
2722         spin_lock(&interfaces_lock);
2723         for (i=0; i<MAX_IPMI_INTERFACES; i++) {
2724                 intf = ipmi_interfaces[i];
2725                 if (intf == NULL)
2726                         continue;
2727
2728                 intf->handlers->request_events(intf->send_info);
2729         }
2730         spin_unlock(&interfaces_lock);
2731 }
2732
2733 static struct timer_list ipmi_timer;
2734
2735 /* Call every ~100 ms. */
2736 #define IPMI_TIMEOUT_TIME       100
2737
2738 /* How many jiffies does it take to get to the timeout time. */
2739 #define IPMI_TIMEOUT_JIFFIES    ((IPMI_TIMEOUT_TIME * HZ) / 1000)
2740
2741 /* Request events from the queue every second (this is the number of
2742    IPMI_TIMEOUT_TIMES between event requests).  Hopefully, in the
2743    future, IPMI will add a way to know immediately if an event is in
2744    the queue and this silliness can go away. */
2745 #define IPMI_REQUEST_EV_TIME    (1000 / (IPMI_TIMEOUT_TIME))
2746
2747 static volatile int stop_operation = 0;
2748 static volatile int timer_stopped = 0;
2749 static unsigned int ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
2750
2751 static void ipmi_timeout(unsigned long data)
2752 {
2753         if (stop_operation) {
2754                 timer_stopped = 1;
2755                 return;
2756         }
2757
2758         ticks_to_req_ev--;
2759         if (ticks_to_req_ev == 0) {
2760                 ipmi_request_event();
2761                 ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
2762         }
2763
2764         ipmi_timeout_handler(IPMI_TIMEOUT_TIME);
2765
2766         ipmi_timer.expires += IPMI_TIMEOUT_JIFFIES;
2767         add_timer(&ipmi_timer);
2768 }
2769
2770
2771 static atomic_t smi_msg_inuse_count = ATOMIC_INIT(0);
2772 static atomic_t recv_msg_inuse_count = ATOMIC_INIT(0);
2773
2774 /* FIXME - convert these to slabs. */
2775 static void free_smi_msg(struct ipmi_smi_msg *msg)
2776 {
2777         atomic_dec(&smi_msg_inuse_count);
2778         kfree(msg);
2779 }
2780
2781 struct ipmi_smi_msg *ipmi_alloc_smi_msg(void)
2782 {
2783         struct ipmi_smi_msg *rv;
2784         rv = kmalloc(sizeof(struct ipmi_smi_msg), GFP_ATOMIC);
2785         if (rv) {
2786                 rv->done = free_smi_msg;
2787                 rv->user_data = NULL;
2788                 atomic_inc(&smi_msg_inuse_count);
2789         }
2790         return rv;
2791 }
2792
2793 static void free_recv_msg(struct ipmi_recv_msg *msg)
2794 {
2795         atomic_dec(&recv_msg_inuse_count);
2796         kfree(msg);
2797 }
2798
2799 struct ipmi_recv_msg *ipmi_alloc_recv_msg(void)
2800 {
2801         struct ipmi_recv_msg *rv;
2802
2803         rv = kmalloc(sizeof(struct ipmi_recv_msg), GFP_ATOMIC);
2804         if (rv) {
2805                 rv->done = free_recv_msg;
2806                 atomic_inc(&recv_msg_inuse_count);
2807         }
2808         return rv;
2809 }
2810
2811 #ifdef CONFIG_IPMI_PANIC_EVENT
2812
2813 static void dummy_smi_done_handler(struct ipmi_smi_msg *msg)
2814 {
2815 }
2816
2817 static void dummy_recv_done_handler(struct ipmi_recv_msg *msg)
2818 {
2819 }
2820
2821 #ifdef CONFIG_IPMI_PANIC_STRING
2822 static void event_receiver_fetcher(ipmi_smi_t intf, struct ipmi_smi_msg *msg)
2823 {
2824         if ((msg->rsp[0] == (IPMI_NETFN_SENSOR_EVENT_RESPONSE << 2))
2825             && (msg->rsp[1] == IPMI_GET_EVENT_RECEIVER_CMD)
2826             && (msg->rsp[2] == IPMI_CC_NO_ERROR))
2827         {
2828                 /* A get event receiver command, save it. */
2829                 intf->event_receiver = msg->rsp[3];
2830                 intf->event_receiver_lun = msg->rsp[4] & 0x3;
2831         }
2832 }
2833
2834 static void device_id_fetcher(ipmi_smi_t intf, struct ipmi_smi_msg *msg)
2835 {
2836         if ((msg->rsp[0] == (IPMI_NETFN_APP_RESPONSE << 2))
2837             && (msg->rsp[1] == IPMI_GET_DEVICE_ID_CMD)
2838             && (msg->rsp[2] == IPMI_CC_NO_ERROR))
2839         {
2840                 /* A get device id command, save if we are an event
2841                    receiver or generator. */
2842                 intf->local_sel_device = (msg->rsp[8] >> 2) & 1;
2843                 intf->local_event_generator = (msg->rsp[8] >> 5) & 1;
2844         }
2845 }
2846 #endif
2847
2848 static void send_panic_events(char *str)
2849 {
2850         struct ipmi_msg                   msg;
2851         ipmi_smi_t                        intf;
2852         unsigned char                     data[16];
2853         int                               i;
2854         struct ipmi_system_interface_addr *si;
2855         struct ipmi_addr                  addr;
2856         struct ipmi_smi_msg               smi_msg;
2857         struct ipmi_recv_msg              recv_msg;
2858
2859         si = (struct ipmi_system_interface_addr *) &addr;
2860         si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2861         si->channel = IPMI_BMC_CHANNEL;
2862         si->lun = 0;
2863
2864         /* Fill in an event telling that we have failed. */
2865         msg.netfn = 0x04; /* Sensor or Event. */
2866         msg.cmd = 2; /* Platform event command. */
2867         msg.data = data;
2868         msg.data_len = 8;
2869         data[0] = 0x21; /* Kernel generator ID, IPMI table 5-4 */
2870         data[1] = 0x03; /* This is for IPMI 1.0. */
2871         data[2] = 0x20; /* OS Critical Stop, IPMI table 36-3 */
2872         data[4] = 0x6f; /* Sensor specific, IPMI table 36-1 */
2873         data[5] = 0xa1; /* Runtime stop OEM bytes 2 & 3. */
2874
2875         /* Put a few breadcrumbs in.  Hopefully later we can add more things
2876            to make the panic events more useful. */
2877         if (str) {
2878                 data[3] = str[0];
2879                 data[6] = str[1];
2880                 data[7] = str[2];
2881         }
2882
2883         smi_msg.done = dummy_smi_done_handler;
2884         recv_msg.done = dummy_recv_done_handler;
2885
2886         /* For every registered interface, send the event. */
2887         for (i=0; i<MAX_IPMI_INTERFACES; i++) {
2888                 intf = ipmi_interfaces[i];
2889                 if (intf == NULL)
2890                         continue;
2891
2892                 /* Send the event announcing the panic. */
2893                 intf->handlers->set_run_to_completion(intf->send_info, 1);
2894                 i_ipmi_request(NULL,
2895                                intf,
2896                                &addr,
2897                                0,
2898                                &msg,
2899                                NULL,
2900                                &smi_msg,
2901                                &recv_msg,
2902                                0,
2903                                intf->my_address,
2904                                intf->my_lun,
2905                                0, 1); /* Don't retry, and don't wait. */
2906         }
2907
2908 #ifdef CONFIG_IPMI_PANIC_STRING
2909         /* On every interface, dump a bunch of OEM event holding the
2910            string. */
2911         if (!str) 
2912                 return;
2913
2914         for (i=0; i<MAX_IPMI_INTERFACES; i++) {
2915                 char                  *p = str;
2916                 struct ipmi_ipmb_addr *ipmb;
2917                 int                   j;
2918
2919                 intf = ipmi_interfaces[i];
2920                 if (intf == NULL)
2921                         continue;
2922
2923                 /* First job here is to figure out where to send the
2924                    OEM events.  There's no way in IPMI to send OEM
2925                    events using an event send command, so we have to
2926                    find the SEL to put them in and stick them in
2927                    there. */
2928
2929                 /* Get capabilities from the get device id. */
2930                 intf->local_sel_device = 0;
2931                 intf->local_event_generator = 0;
2932                 intf->event_receiver = 0;
2933
2934                 /* Request the device info from the local MC. */
2935                 msg.netfn = IPMI_NETFN_APP_REQUEST;
2936                 msg.cmd = IPMI_GET_DEVICE_ID_CMD;
2937                 msg.data = NULL;
2938                 msg.data_len = 0;
2939                 intf->null_user_handler = device_id_fetcher;
2940                 i_ipmi_request(NULL,
2941                                intf,
2942                                &addr,
2943                                0,
2944                                &msg,
2945                                NULL,
2946                                &smi_msg,
2947                                &recv_msg,
2948                                0,
2949                                intf->my_address,
2950                                intf->my_lun,
2951                                0, 1); /* Don't retry, and don't wait. */
2952
2953                 if (intf->local_event_generator) {
2954                         /* Request the event receiver from the local MC. */
2955                         msg.netfn = IPMI_NETFN_SENSOR_EVENT_REQUEST;
2956                         msg.cmd = IPMI_GET_EVENT_RECEIVER_CMD;
2957                         msg.data = NULL;
2958                         msg.data_len = 0;
2959                         intf->null_user_handler = event_receiver_fetcher;
2960                         i_ipmi_request(NULL,
2961                                        intf,
2962                                        &addr,
2963                                        0,
2964                                        &msg,
2965                                        NULL,
2966                                        &smi_msg,
2967                                        &recv_msg,
2968                                        0,
2969                                        intf->my_address,
2970                                        intf->my_lun,
2971                                        0, 1); /* no retry, and no wait. */
2972                 }
2973                 intf->null_user_handler = NULL;
2974
2975                 /* Validate the event receiver.  The low bit must not
2976                    be 1 (it must be a valid IPMB address), it cannot
2977                    be zero, and it must not be my address. */
2978                 if (((intf->event_receiver & 1) == 0)
2979                     && (intf->event_receiver != 0)
2980                     && (intf->event_receiver != intf->my_address))
2981                 {
2982                         /* The event receiver is valid, send an IPMB
2983                            message. */
2984                         ipmb = (struct ipmi_ipmb_addr *) &addr;
2985                         ipmb->addr_type = IPMI_IPMB_ADDR_TYPE;
2986                         ipmb->channel = 0; /* FIXME - is this right? */
2987                         ipmb->lun = intf->event_receiver_lun;
2988                         ipmb->slave_addr = intf->event_receiver;
2989                 } else if (intf->local_sel_device) {
2990                         /* The event receiver was not valid (or was
2991                            me), but I am an SEL device, just dump it
2992                            in my SEL. */
2993                         si = (struct ipmi_system_interface_addr *) &addr;
2994                         si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2995                         si->channel = IPMI_BMC_CHANNEL;
2996                         si->lun = 0;
2997                 } else
2998                         continue; /* No where to send the event. */
2999
3000                 
3001                 msg.netfn = IPMI_NETFN_STORAGE_REQUEST; /* Storage. */
3002                 msg.cmd = IPMI_ADD_SEL_ENTRY_CMD;
3003                 msg.data = data;
3004                 msg.data_len = 16;
3005
3006                 j = 0;
3007                 while (*p) {
3008                         int size = strlen(p);
3009
3010                         if (size > 11)
3011                                 size = 11;
3012                         data[0] = 0;
3013                         data[1] = 0;
3014                         data[2] = 0xf0; /* OEM event without timestamp. */
3015                         data[3] = intf->my_address;
3016                         data[4] = j++; /* sequence # */
3017                         /* Always give 11 bytes, so strncpy will fill
3018                            it with zeroes for me. */
3019                         strncpy(data+5, p, 11);
3020                         p += size;
3021
3022                         i_ipmi_request(NULL,
3023                                        intf,
3024                                        &addr,
3025                                        0,
3026                                        &msg,
3027                                        NULL,
3028                                        &smi_msg,
3029                                        &recv_msg,
3030                                        0,
3031                                        intf->my_address,
3032                                        intf->my_lun,
3033                                        0, 1); /* no retry, and no wait. */
3034                 }
3035         }       
3036 #endif /* CONFIG_IPMI_PANIC_STRING */
3037 }
3038 #endif /* CONFIG_IPMI_PANIC_EVENT */
3039
3040 static int has_paniced = 0;
3041
3042 static int panic_event(struct notifier_block *this,
3043                        unsigned long         event,
3044                        void                  *ptr)
3045 {
3046         int        i;
3047         ipmi_smi_t intf;
3048
3049         if (has_paniced)
3050                 return NOTIFY_DONE;
3051         has_paniced = 1;
3052
3053         /* For every registered interface, set it to run to completion. */
3054         for (i=0; i<MAX_IPMI_INTERFACES; i++) {
3055                 intf = ipmi_interfaces[i];
3056                 if (intf == NULL)
3057                         continue;
3058
3059                 intf->handlers->set_run_to_completion(intf->send_info, 1);
3060         }
3061
3062 #ifdef CONFIG_IPMI_PANIC_EVENT
3063         send_panic_events(ptr);
3064 #endif
3065
3066         return NOTIFY_DONE;
3067 }
3068
3069 static struct notifier_block panic_block = {
3070         panic_event,
3071         NULL,
3072         200   /* priority: INT_MAX >= x >= 0 */
3073 };
3074
3075 static __init int ipmi_init_msghandler(void)
3076 {
3077         int i;
3078
3079         if (initialized)
3080                 return 0;
3081
3082         printk(KERN_INFO "ipmi message handler version "
3083                IPMI_MSGHANDLER_VERSION "\n");
3084
3085         for (i=0; i<MAX_IPMI_INTERFACES; i++) {
3086                 ipmi_interfaces[i] = NULL;
3087         }
3088
3089         proc_ipmi_root = proc_mkdir("ipmi", 0);
3090         if (!proc_ipmi_root) {
3091             printk("Unable to create IPMI proc dir");
3092             return -ENOMEM;
3093         }
3094
3095         proc_ipmi_root->owner = THIS_MODULE;
3096
3097         init_timer(&ipmi_timer);
3098         ipmi_timer.data = 0;
3099         ipmi_timer.function = ipmi_timeout;
3100         ipmi_timer.expires = jiffies + IPMI_TIMEOUT_JIFFIES;
3101         add_timer(&ipmi_timer);
3102
3103         notifier_chain_register(&panic_notifier_list, &panic_block);
3104
3105         initialized = 1;
3106
3107         return 0;
3108 }
3109
3110 static __exit void cleanup_ipmi(void)
3111 {
3112         int count;
3113
3114         if (!initialized)
3115                 return;
3116
3117         notifier_chain_unregister(&panic_notifier_list, &panic_block);
3118
3119         /* This can't be called if any interfaces exist, so no worry about
3120            shutting down the interfaces. */
3121
3122         /* Tell the timer to stop, then wait for it to stop.  This avoids
3123            problems with race conditions removing the timer here. */
3124         stop_operation = 1;
3125         while (!timer_stopped) {
3126                 set_current_state(TASK_UNINTERRUPTIBLE);
3127                 schedule_timeout(1);
3128         }
3129
3130         remove_proc_entry(proc_ipmi_root->name, &proc_root);
3131
3132         initialized = 0;
3133
3134         /* Check for buffer leaks. */
3135         count = atomic_read(&smi_msg_inuse_count);
3136         if (count != 0)
3137                 printk("ipmi_msghandler: SMI message count %d at exit\n",
3138                        count);
3139         count = atomic_read(&recv_msg_inuse_count);
3140         if (count != 0)
3141                 printk("ipmi_msghandler: recv message count %d at exit\n",
3142                        count);
3143 }
3144 module_exit(cleanup_ipmi);
3145
3146 module_init(ipmi_init_msghandler);
3147 MODULE_LICENSE("GPL");
3148
3149 EXPORT_SYMBOL(ipmi_alloc_recv_msg);
3150 EXPORT_SYMBOL(ipmi_create_user);
3151 EXPORT_SYMBOL(ipmi_destroy_user);
3152 EXPORT_SYMBOL(ipmi_get_version);
3153 EXPORT_SYMBOL(ipmi_request);
3154 EXPORT_SYMBOL(ipmi_request_settime);
3155 EXPORT_SYMBOL(ipmi_request_supply_msgs);
3156 EXPORT_SYMBOL(ipmi_request_with_source);
3157 EXPORT_SYMBOL(ipmi_register_smi);
3158 EXPORT_SYMBOL(ipmi_unregister_smi);
3159 EXPORT_SYMBOL(ipmi_register_for_cmd);
3160 EXPORT_SYMBOL(ipmi_unregister_for_cmd);
3161 EXPORT_SYMBOL(ipmi_smi_msg_received);
3162 EXPORT_SYMBOL(ipmi_smi_watchdog_pretimeout);
3163 EXPORT_SYMBOL(ipmi_alloc_smi_msg);
3164 EXPORT_SYMBOL(ipmi_register_all_cmd_rcvr);
3165 EXPORT_SYMBOL(ipmi_unregister_all_cmd_rcvr);
3166 EXPORT_SYMBOL(ipmi_addr_length);
3167 EXPORT_SYMBOL(ipmi_validate_addr);
3168 EXPORT_SYMBOL(ipmi_set_gets_events);
3169 EXPORT_SYMBOL(ipmi_addr_equal);
3170 EXPORT_SYMBOL(ipmi_smi_watcher_register);
3171 EXPORT_SYMBOL(ipmi_smi_watcher_unregister);
3172 EXPORT_SYMBOL(ipmi_set_my_address);
3173 EXPORT_SYMBOL(ipmi_get_my_address);
3174 EXPORT_SYMBOL(ipmi_set_my_LUN);
3175 EXPORT_SYMBOL(ipmi_get_my_LUN);
3176 EXPORT_SYMBOL(ipmi_smi_add_proc_entry);