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