[PATCH] ipmi: fix panic ipmb response
[linux/fpc-iii.git] / drivers / char / ipmi / ipmi_msghandler.c
blobd0ed25278cbbcb04a104750ba41cf8bc63303c9c
1 /*
2 * ipmi_msghandler.c
4 * Incoming and outgoing message routing for an IPMI interface.
6 * Author: MontaVista Software, Inc.
7 * Corey Minyard <minyard@mvista.com>
8 * source@mvista.com
10 * Copyright 2002 MontaVista Software Inc.
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.
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.
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.
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>
49 #define PFX "IPMI message handler: "
51 #define IPMI_DRIVER_VERSION "36.0"
53 static struct ipmi_recv_msg *ipmi_alloc_recv_msg(void);
54 static int ipmi_init_msghandler(void);
56 static int initialized = 0;
58 #ifdef CONFIG_PROC_FS
59 struct proc_dir_entry *proc_ipmi_root = NULL;
60 #endif /* CONFIG_PROC_FS */
62 #define MAX_EVENTS_IN_QUEUE 25
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
68 struct ipmi_user
70 struct list_head link;
72 /* The upper layer that handles receive messages. */
73 struct ipmi_user_hndl *handler;
74 void *handler_data;
76 /* The interface this user is bound to. */
77 ipmi_smi_t intf;
79 /* Does this interface receive IPMI events? */
80 int gets_events;
83 struct cmd_rcvr
85 struct list_head link;
87 ipmi_user_t user;
88 unsigned char netfn;
89 unsigned char cmd;
92 struct seq_table
94 unsigned int inuse : 1;
95 unsigned int broadcast : 1;
97 unsigned long timeout;
98 unsigned long orig_timeout;
99 unsigned int retries_left;
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;
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;
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))
116 #define GET_SEQ_FROM_MSGID(msgid, seq, seqid) \
117 do { \
118 seq = ((msgid >> 26) & 0x3f); \
119 seqid = (msgid & 0x3fffff); \
120 } while(0)
122 #define NEXT_SEQID(seqid) (((seqid) + 1) & 0x3fffff)
124 struct ipmi_channel
126 unsigned char medium;
127 unsigned char protocol;
129 /* My slave address. This is initialized to IPMI_BMC_SLAVE_ADDR,
130 but may be changed by the user. */
131 unsigned char address;
133 /* My LUN. This should generally stay the SMS LUN, but just in
134 case... */
135 unsigned char lun;
138 #ifdef CONFIG_PROC_FS
139 struct ipmi_proc_entry
141 char *name;
142 struct ipmi_proc_entry *next;
144 #endif
146 #define IPMI_IPMB_NUM_SEQ 64
147 #define IPMI_MAX_CHANNELS 16
148 struct ipmi_smi
150 /* What interface number are we? */
151 int intf_num;
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;
161 /* Used for wake ups at startup. */
162 wait_queue_head_t waitq;
164 /* The IPMI version of the BMC on the other end. */
165 unsigned char version_major;
166 unsigned char version_minor;
168 /* This is the lower-layer's sender routine. */
169 struct ipmi_smi_handlers *handlers;
170 void *send_info;
172 #ifdef CONFIG_PROC_FS
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. */
176 spinlock_t proc_entry_lock;
177 struct ipmi_proc_entry *proc_entries;
178 #endif
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;
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;
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;
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? */
205 /* This will be non-null if someone registers to receive all
206 IPMI commands (this is for interface emulation). There
207 may not be any things in the cmd_rcvrs list above when
208 this is registered. */
209 ipmi_user_t all_cmd_rcvr;
211 /* The event receiver for my BMC, only really used at panic
212 shutdown as a place to store this. */
213 unsigned char event_receiver;
214 unsigned char event_receiver_lun;
215 unsigned char local_sel_device;
216 unsigned char local_event_generator;
218 /* A cheap hack, if this is non-null and a message to an
219 interface comes in with a NULL user, call this routine with
220 it. Note that the message will still be freed by the
221 caller. This only works on the system interface. */
222 void (*null_user_handler)(ipmi_smi_t intf, struct ipmi_recv_msg *msg);
224 /* When we are scanning the channels for an SMI, this will
225 tell which channel we are scanning. */
226 int curr_channel;
228 /* Channel information */
229 struct ipmi_channel channels[IPMI_MAX_CHANNELS];
231 /* Proc FS stuff. */
232 struct proc_dir_entry *proc_dir;
233 char proc_dir_name[10];
235 spinlock_t counter_lock; /* For making counters atomic. */
237 /* Commands we got that were invalid. */
238 unsigned int sent_invalid_commands;
240 /* Commands we sent to the MC. */
241 unsigned int sent_local_commands;
242 /* Responses from the MC that were delivered to a user. */
243 unsigned int handled_local_responses;
244 /* Responses from the MC that were not delivered to a user. */
245 unsigned int unhandled_local_responses;
247 /* Commands we sent out to the IPMB bus. */
248 unsigned int sent_ipmb_commands;
249 /* Commands sent on the IPMB that had errors on the SEND CMD */
250 unsigned int sent_ipmb_command_errs;
251 /* Each retransmit increments this count. */
252 unsigned int retransmitted_ipmb_commands;
253 /* When a message times out (runs out of retransmits) this is
254 incremented. */
255 unsigned int timed_out_ipmb_commands;
257 /* This is like above, but for broadcasts. Broadcasts are
258 *not* included in the above count (they are expected to
259 time out). */
260 unsigned int timed_out_ipmb_broadcasts;
262 /* Responses I have sent to the IPMB bus. */
263 unsigned int sent_ipmb_responses;
265 /* The response was delivered to the user. */
266 unsigned int handled_ipmb_responses;
267 /* The response had invalid data in it. */
268 unsigned int invalid_ipmb_responses;
269 /* The response didn't have anyone waiting for it. */
270 unsigned int unhandled_ipmb_responses;
272 /* Commands we sent out to the IPMB bus. */
273 unsigned int sent_lan_commands;
274 /* Commands sent on the IPMB that had errors on the SEND CMD */
275 unsigned int sent_lan_command_errs;
276 /* Each retransmit increments this count. */
277 unsigned int retransmitted_lan_commands;
278 /* When a message times out (runs out of retransmits) this is
279 incremented. */
280 unsigned int timed_out_lan_commands;
282 /* Responses I have sent to the IPMB bus. */
283 unsigned int sent_lan_responses;
285 /* The response was delivered to the user. */
286 unsigned int handled_lan_responses;
287 /* The response had invalid data in it. */
288 unsigned int invalid_lan_responses;
289 /* The response didn't have anyone waiting for it. */
290 unsigned int unhandled_lan_responses;
292 /* The command was delivered to the user. */
293 unsigned int handled_commands;
294 /* The command had invalid data in it. */
295 unsigned int invalid_commands;
296 /* The command didn't have anyone waiting for it. */
297 unsigned int unhandled_commands;
299 /* Invalid data in an event. */
300 unsigned int invalid_events;
301 /* Events that were received with the proper format. */
302 unsigned int events;
305 #define MAX_IPMI_INTERFACES 4
306 static ipmi_smi_t ipmi_interfaces[MAX_IPMI_INTERFACES];
308 /* Used to keep interfaces from going away while operations are
309 operating on interfaces. Grab read if you are not modifying the
310 interfaces, write if you are. */
311 static DECLARE_RWSEM(interfaces_sem);
313 /* Directly protects the ipmi_interfaces data structure. This is
314 claimed in the timer interrupt. */
315 static DEFINE_SPINLOCK(interfaces_lock);
317 /* List of watchers that want to know when smi's are added and
318 deleted. */
319 static struct list_head smi_watchers = LIST_HEAD_INIT(smi_watchers);
320 static DECLARE_RWSEM(smi_watchers_sem);
322 int ipmi_smi_watcher_register(struct ipmi_smi_watcher *watcher)
324 int i;
326 down_read(&interfaces_sem);
327 down_write(&smi_watchers_sem);
328 list_add(&(watcher->link), &smi_watchers);
329 for (i=0; i<MAX_IPMI_INTERFACES; i++) {
330 if (ipmi_interfaces[i] != NULL) {
331 watcher->new_smi(i);
334 up_write(&smi_watchers_sem);
335 up_read(&interfaces_sem);
336 return 0;
339 int ipmi_smi_watcher_unregister(struct ipmi_smi_watcher *watcher)
341 down_write(&smi_watchers_sem);
342 list_del(&(watcher->link));
343 up_write(&smi_watchers_sem);
344 return 0;
347 static void
348 call_smi_watchers(int i)
350 struct ipmi_smi_watcher *w;
352 down_read(&smi_watchers_sem);
353 list_for_each_entry(w, &smi_watchers, link) {
354 if (try_module_get(w->owner)) {
355 w->new_smi(i);
356 module_put(w->owner);
359 up_read(&smi_watchers_sem);
362 static int
363 ipmi_addr_equal(struct ipmi_addr *addr1, struct ipmi_addr *addr2)
365 if (addr1->addr_type != addr2->addr_type)
366 return 0;
368 if (addr1->channel != addr2->channel)
369 return 0;
371 if (addr1->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
372 struct ipmi_system_interface_addr *smi_addr1
373 = (struct ipmi_system_interface_addr *) addr1;
374 struct ipmi_system_interface_addr *smi_addr2
375 = (struct ipmi_system_interface_addr *) addr2;
376 return (smi_addr1->lun == smi_addr2->lun);
379 if ((addr1->addr_type == IPMI_IPMB_ADDR_TYPE)
380 || (addr1->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE))
382 struct ipmi_ipmb_addr *ipmb_addr1
383 = (struct ipmi_ipmb_addr *) addr1;
384 struct ipmi_ipmb_addr *ipmb_addr2
385 = (struct ipmi_ipmb_addr *) addr2;
387 return ((ipmb_addr1->slave_addr == ipmb_addr2->slave_addr)
388 && (ipmb_addr1->lun == ipmb_addr2->lun));
391 if (addr1->addr_type == IPMI_LAN_ADDR_TYPE) {
392 struct ipmi_lan_addr *lan_addr1
393 = (struct ipmi_lan_addr *) addr1;
394 struct ipmi_lan_addr *lan_addr2
395 = (struct ipmi_lan_addr *) addr2;
397 return ((lan_addr1->remote_SWID == lan_addr2->remote_SWID)
398 && (lan_addr1->local_SWID == lan_addr2->local_SWID)
399 && (lan_addr1->session_handle
400 == lan_addr2->session_handle)
401 && (lan_addr1->lun == lan_addr2->lun));
404 return 1;
407 int ipmi_validate_addr(struct ipmi_addr *addr, int len)
409 if (len < sizeof(struct ipmi_system_interface_addr)) {
410 return -EINVAL;
413 if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
414 if (addr->channel != IPMI_BMC_CHANNEL)
415 return -EINVAL;
416 return 0;
419 if ((addr->channel == IPMI_BMC_CHANNEL)
420 || (addr->channel >= IPMI_NUM_CHANNELS)
421 || (addr->channel < 0))
422 return -EINVAL;
424 if ((addr->addr_type == IPMI_IPMB_ADDR_TYPE)
425 || (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE))
427 if (len < sizeof(struct ipmi_ipmb_addr)) {
428 return -EINVAL;
430 return 0;
433 if (addr->addr_type == IPMI_LAN_ADDR_TYPE) {
434 if (len < sizeof(struct ipmi_lan_addr)) {
435 return -EINVAL;
437 return 0;
440 return -EINVAL;
443 unsigned int ipmi_addr_length(int addr_type)
445 if (addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
446 return sizeof(struct ipmi_system_interface_addr);
448 if ((addr_type == IPMI_IPMB_ADDR_TYPE)
449 || (addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE))
451 return sizeof(struct ipmi_ipmb_addr);
454 if (addr_type == IPMI_LAN_ADDR_TYPE)
455 return sizeof(struct ipmi_lan_addr);
457 return 0;
460 static void deliver_response(struct ipmi_recv_msg *msg)
462 if (! msg->user) {
463 ipmi_smi_t intf = msg->user_msg_data;
464 unsigned long flags;
466 /* Special handling for NULL users. */
467 if (intf->null_user_handler) {
468 intf->null_user_handler(intf, msg);
469 spin_lock_irqsave(&intf->counter_lock, flags);
470 intf->handled_local_responses++;
471 spin_unlock_irqrestore(&intf->counter_lock, flags);
472 } else {
473 /* No handler, so give up. */
474 spin_lock_irqsave(&intf->counter_lock, flags);
475 intf->unhandled_local_responses++;
476 spin_unlock_irqrestore(&intf->counter_lock, flags);
478 ipmi_free_recv_msg(msg);
479 } else {
480 msg->user->handler->ipmi_recv_hndl(msg,
481 msg->user->handler_data);
485 /* Find the next sequence number not being used and add the given
486 message with the given timeout to the sequence table. This must be
487 called with the interface's seq_lock held. */
488 static int intf_next_seq(ipmi_smi_t intf,
489 struct ipmi_recv_msg *recv_msg,
490 unsigned long timeout,
491 int retries,
492 int broadcast,
493 unsigned char *seq,
494 long *seqid)
496 int rv = 0;
497 unsigned int i;
499 for (i=intf->curr_seq;
500 (i+1)%IPMI_IPMB_NUM_SEQ != intf->curr_seq;
501 i=(i+1)%IPMI_IPMB_NUM_SEQ)
503 if (! intf->seq_table[i].inuse)
504 break;
507 if (! intf->seq_table[i].inuse) {
508 intf->seq_table[i].recv_msg = recv_msg;
510 /* Start with the maximum timeout, when the send response
511 comes in we will start the real timer. */
512 intf->seq_table[i].timeout = MAX_MSG_TIMEOUT;
513 intf->seq_table[i].orig_timeout = timeout;
514 intf->seq_table[i].retries_left = retries;
515 intf->seq_table[i].broadcast = broadcast;
516 intf->seq_table[i].inuse = 1;
517 intf->seq_table[i].seqid = NEXT_SEQID(intf->seq_table[i].seqid);
518 *seq = i;
519 *seqid = intf->seq_table[i].seqid;
520 intf->curr_seq = (i+1)%IPMI_IPMB_NUM_SEQ;
521 } else {
522 rv = -EAGAIN;
525 return rv;
528 /* Return the receive message for the given sequence number and
529 release the sequence number so it can be reused. Some other data
530 is passed in to be sure the message matches up correctly (to help
531 guard against message coming in after their timeout and the
532 sequence number being reused). */
533 static int intf_find_seq(ipmi_smi_t intf,
534 unsigned char seq,
535 short channel,
536 unsigned char cmd,
537 unsigned char netfn,
538 struct ipmi_addr *addr,
539 struct ipmi_recv_msg **recv_msg)
541 int rv = -ENODEV;
542 unsigned long flags;
544 if (seq >= IPMI_IPMB_NUM_SEQ)
545 return -EINVAL;
547 spin_lock_irqsave(&(intf->seq_lock), flags);
548 if (intf->seq_table[seq].inuse) {
549 struct ipmi_recv_msg *msg = intf->seq_table[seq].recv_msg;
551 if ((msg->addr.channel == channel)
552 && (msg->msg.cmd == cmd)
553 && (msg->msg.netfn == netfn)
554 && (ipmi_addr_equal(addr, &(msg->addr))))
556 *recv_msg = msg;
557 intf->seq_table[seq].inuse = 0;
558 rv = 0;
561 spin_unlock_irqrestore(&(intf->seq_lock), flags);
563 return rv;
567 /* Start the timer for a specific sequence table entry. */
568 static int intf_start_seq_timer(ipmi_smi_t intf,
569 long msgid)
571 int rv = -ENODEV;
572 unsigned long flags;
573 unsigned char seq;
574 unsigned long seqid;
577 GET_SEQ_FROM_MSGID(msgid, seq, seqid);
579 spin_lock_irqsave(&(intf->seq_lock), flags);
580 /* We do this verification because the user can be deleted
581 while a message is outstanding. */
582 if ((intf->seq_table[seq].inuse)
583 && (intf->seq_table[seq].seqid == seqid))
585 struct seq_table *ent = &(intf->seq_table[seq]);
586 ent->timeout = ent->orig_timeout;
587 rv = 0;
589 spin_unlock_irqrestore(&(intf->seq_lock), flags);
591 return rv;
594 /* Got an error for the send message for a specific sequence number. */
595 static int intf_err_seq(ipmi_smi_t intf,
596 long msgid,
597 unsigned int err)
599 int rv = -ENODEV;
600 unsigned long flags;
601 unsigned char seq;
602 unsigned long seqid;
603 struct ipmi_recv_msg *msg = NULL;
606 GET_SEQ_FROM_MSGID(msgid, seq, seqid);
608 spin_lock_irqsave(&(intf->seq_lock), flags);
609 /* We do this verification because the user can be deleted
610 while a message is outstanding. */
611 if ((intf->seq_table[seq].inuse)
612 && (intf->seq_table[seq].seqid == seqid))
614 struct seq_table *ent = &(intf->seq_table[seq]);
616 ent->inuse = 0;
617 msg = ent->recv_msg;
618 rv = 0;
620 spin_unlock_irqrestore(&(intf->seq_lock), flags);
622 if (msg) {
623 msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
624 msg->msg_data[0] = err;
625 msg->msg.netfn |= 1; /* Convert to a response. */
626 msg->msg.data_len = 1;
627 msg->msg.data = msg->msg_data;
628 deliver_response(msg);
631 return rv;
635 int ipmi_create_user(unsigned int if_num,
636 struct ipmi_user_hndl *handler,
637 void *handler_data,
638 ipmi_user_t *user)
640 unsigned long flags;
641 ipmi_user_t new_user;
642 int rv = 0;
643 ipmi_smi_t intf;
645 /* There is no module usecount here, because it's not
646 required. Since this can only be used by and called from
647 other modules, they will implicitly use this module, and
648 thus this can't be removed unless the other modules are
649 removed. */
651 if (handler == NULL)
652 return -EINVAL;
654 /* Make sure the driver is actually initialized, this handles
655 problems with initialization order. */
656 if (!initialized) {
657 rv = ipmi_init_msghandler();
658 if (rv)
659 return rv;
661 /* The init code doesn't return an error if it was turned
662 off, but it won't initialize. Check that. */
663 if (!initialized)
664 return -ENODEV;
667 new_user = kmalloc(sizeof(*new_user), GFP_KERNEL);
668 if (! new_user)
669 return -ENOMEM;
671 down_read(&interfaces_sem);
672 if ((if_num >= MAX_IPMI_INTERFACES) || ipmi_interfaces[if_num] == NULL)
674 rv = -EINVAL;
675 goto out_unlock;
678 intf = ipmi_interfaces[if_num];
680 new_user->handler = handler;
681 new_user->handler_data = handler_data;
682 new_user->intf = intf;
683 new_user->gets_events = 0;
685 if (!try_module_get(intf->handlers->owner)) {
686 rv = -ENODEV;
687 goto out_unlock;
690 if (intf->handlers->inc_usecount) {
691 rv = intf->handlers->inc_usecount(intf->send_info);
692 if (rv) {
693 module_put(intf->handlers->owner);
694 goto out_unlock;
698 write_lock_irqsave(&intf->users_lock, flags);
699 list_add_tail(&new_user->link, &intf->users);
700 write_unlock_irqrestore(&intf->users_lock, flags);
702 out_unlock:
703 if (rv) {
704 kfree(new_user);
705 } else {
706 *user = new_user;
709 up_read(&interfaces_sem);
710 return rv;
713 static int ipmi_destroy_user_nolock(ipmi_user_t user)
715 int rv = -ENODEV;
716 ipmi_user_t t_user;
717 struct cmd_rcvr *rcvr, *rcvr2;
718 int i;
719 unsigned long flags;
721 /* Find the user and delete them from the list. */
722 list_for_each_entry(t_user, &(user->intf->users), link) {
723 if (t_user == user) {
724 list_del(&t_user->link);
725 rv = 0;
726 break;
730 if (rv) {
731 goto out_unlock;
734 /* Remove the user from the interfaces sequence table. */
735 spin_lock_irqsave(&(user->intf->seq_lock), flags);
736 for (i=0; i<IPMI_IPMB_NUM_SEQ; i++) {
737 if (user->intf->seq_table[i].inuse
738 && (user->intf->seq_table[i].recv_msg->user == user))
740 user->intf->seq_table[i].inuse = 0;
743 spin_unlock_irqrestore(&(user->intf->seq_lock), flags);
745 /* Remove the user from the command receiver's table. */
746 write_lock_irqsave(&(user->intf->cmd_rcvr_lock), flags);
747 list_for_each_entry_safe(rcvr, rcvr2, &(user->intf->cmd_rcvrs), link) {
748 if (rcvr->user == user) {
749 list_del(&rcvr->link);
750 kfree(rcvr);
753 write_unlock_irqrestore(&(user->intf->cmd_rcvr_lock), flags);
755 kfree(user);
757 out_unlock:
759 return rv;
762 int ipmi_destroy_user(ipmi_user_t user)
764 int rv;
765 ipmi_smi_t intf = user->intf;
766 unsigned long flags;
768 down_read(&interfaces_sem);
769 write_lock_irqsave(&intf->users_lock, flags);
770 rv = ipmi_destroy_user_nolock(user);
771 if (!rv) {
772 module_put(intf->handlers->owner);
773 if (intf->handlers->dec_usecount)
774 intf->handlers->dec_usecount(intf->send_info);
777 write_unlock_irqrestore(&intf->users_lock, flags);
778 up_read(&interfaces_sem);
779 return rv;
782 void ipmi_get_version(ipmi_user_t user,
783 unsigned char *major,
784 unsigned char *minor)
786 *major = user->intf->version_major;
787 *minor = user->intf->version_minor;
790 int ipmi_set_my_address(ipmi_user_t user,
791 unsigned int channel,
792 unsigned char address)
794 if (channel >= IPMI_MAX_CHANNELS)
795 return -EINVAL;
796 user->intf->channels[channel].address = address;
797 return 0;
800 int ipmi_get_my_address(ipmi_user_t user,
801 unsigned int channel,
802 unsigned char *address)
804 if (channel >= IPMI_MAX_CHANNELS)
805 return -EINVAL;
806 *address = user->intf->channels[channel].address;
807 return 0;
810 int ipmi_set_my_LUN(ipmi_user_t user,
811 unsigned int channel,
812 unsigned char LUN)
814 if (channel >= IPMI_MAX_CHANNELS)
815 return -EINVAL;
816 user->intf->channels[channel].lun = LUN & 0x3;
817 return 0;
820 int ipmi_get_my_LUN(ipmi_user_t user,
821 unsigned int channel,
822 unsigned char *address)
824 if (channel >= IPMI_MAX_CHANNELS)
825 return -EINVAL;
826 *address = user->intf->channels[channel].lun;
827 return 0;
830 int ipmi_set_gets_events(ipmi_user_t user, int val)
832 unsigned long flags;
833 struct ipmi_recv_msg *msg, *msg2;
835 read_lock(&(user->intf->users_lock));
836 spin_lock_irqsave(&(user->intf->events_lock), flags);
837 user->gets_events = val;
839 if (val) {
840 /* Deliver any queued events. */
841 list_for_each_entry_safe(msg, msg2, &(user->intf->waiting_events), link) {
842 list_del(&msg->link);
843 msg->user = user;
844 deliver_response(msg);
848 spin_unlock_irqrestore(&(user->intf->events_lock), flags);
849 read_unlock(&(user->intf->users_lock));
851 return 0;
854 int ipmi_register_for_cmd(ipmi_user_t user,
855 unsigned char netfn,
856 unsigned char cmd)
858 struct cmd_rcvr *cmp;
859 unsigned long flags;
860 struct cmd_rcvr *rcvr;
861 int rv = 0;
864 rcvr = kmalloc(sizeof(*rcvr), GFP_KERNEL);
865 if (! rcvr)
866 return -ENOMEM;
868 read_lock(&(user->intf->users_lock));
869 write_lock_irqsave(&(user->intf->cmd_rcvr_lock), flags);
870 if (user->intf->all_cmd_rcvr != NULL) {
871 rv = -EBUSY;
872 goto out_unlock;
875 /* Make sure the command/netfn is not already registered. */
876 list_for_each_entry(cmp, &(user->intf->cmd_rcvrs), link) {
877 if ((cmp->netfn == netfn) && (cmp->cmd == cmd)) {
878 rv = -EBUSY;
879 break;
883 if (! rv) {
884 rcvr->cmd = cmd;
885 rcvr->netfn = netfn;
886 rcvr->user = user;
887 list_add_tail(&(rcvr->link), &(user->intf->cmd_rcvrs));
889 out_unlock:
890 write_unlock_irqrestore(&(user->intf->cmd_rcvr_lock), flags);
891 read_unlock(&(user->intf->users_lock));
893 if (rv)
894 kfree(rcvr);
896 return rv;
899 int ipmi_unregister_for_cmd(ipmi_user_t user,
900 unsigned char netfn,
901 unsigned char cmd)
903 unsigned long flags;
904 struct cmd_rcvr *rcvr;
905 int rv = -ENOENT;
907 read_lock(&(user->intf->users_lock));
908 write_lock_irqsave(&(user->intf->cmd_rcvr_lock), flags);
909 /* Make sure the command/netfn is not already registered. */
910 list_for_each_entry(rcvr, &(user->intf->cmd_rcvrs), link) {
911 if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)) {
912 rv = 0;
913 list_del(&rcvr->link);
914 kfree(rcvr);
915 break;
918 write_unlock_irqrestore(&(user->intf->cmd_rcvr_lock), flags);
919 read_unlock(&(user->intf->users_lock));
921 return rv;
924 void ipmi_user_set_run_to_completion(ipmi_user_t user, int val)
926 user->intf->handlers->set_run_to_completion(user->intf->send_info,
927 val);
930 static unsigned char
931 ipmb_checksum(unsigned char *data, int size)
933 unsigned char csum = 0;
935 for (; size > 0; size--, data++)
936 csum += *data;
938 return -csum;
941 static inline void format_ipmb_msg(struct ipmi_smi_msg *smi_msg,
942 struct kernel_ipmi_msg *msg,
943 struct ipmi_ipmb_addr *ipmb_addr,
944 long msgid,
945 unsigned char ipmb_seq,
946 int broadcast,
947 unsigned char source_address,
948 unsigned char source_lun)
950 int i = broadcast;
952 /* Format the IPMB header data. */
953 smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
954 smi_msg->data[1] = IPMI_SEND_MSG_CMD;
955 smi_msg->data[2] = ipmb_addr->channel;
956 if (broadcast)
957 smi_msg->data[3] = 0;
958 smi_msg->data[i+3] = ipmb_addr->slave_addr;
959 smi_msg->data[i+4] = (msg->netfn << 2) | (ipmb_addr->lun & 0x3);
960 smi_msg->data[i+5] = ipmb_checksum(&(smi_msg->data[i+3]), 2);
961 smi_msg->data[i+6] = source_address;
962 smi_msg->data[i+7] = (ipmb_seq << 2) | source_lun;
963 smi_msg->data[i+8] = msg->cmd;
965 /* Now tack on the data to the message. */
966 if (msg->data_len > 0)
967 memcpy(&(smi_msg->data[i+9]), msg->data,
968 msg->data_len);
969 smi_msg->data_size = msg->data_len + 9;
971 /* Now calculate the checksum and tack it on. */
972 smi_msg->data[i+smi_msg->data_size]
973 = ipmb_checksum(&(smi_msg->data[i+6]),
974 smi_msg->data_size-6);
976 /* Add on the checksum size and the offset from the
977 broadcast. */
978 smi_msg->data_size += 1 + i;
980 smi_msg->msgid = msgid;
983 static inline void format_lan_msg(struct ipmi_smi_msg *smi_msg,
984 struct kernel_ipmi_msg *msg,
985 struct ipmi_lan_addr *lan_addr,
986 long msgid,
987 unsigned char ipmb_seq,
988 unsigned char source_lun)
990 /* Format the IPMB header data. */
991 smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
992 smi_msg->data[1] = IPMI_SEND_MSG_CMD;
993 smi_msg->data[2] = lan_addr->channel;
994 smi_msg->data[3] = lan_addr->session_handle;
995 smi_msg->data[4] = lan_addr->remote_SWID;
996 smi_msg->data[5] = (msg->netfn << 2) | (lan_addr->lun & 0x3);
997 smi_msg->data[6] = ipmb_checksum(&(smi_msg->data[4]), 2);
998 smi_msg->data[7] = lan_addr->local_SWID;
999 smi_msg->data[8] = (ipmb_seq << 2) | source_lun;
1000 smi_msg->data[9] = msg->cmd;
1002 /* Now tack on the data to the message. */
1003 if (msg->data_len > 0)
1004 memcpy(&(smi_msg->data[10]), msg->data,
1005 msg->data_len);
1006 smi_msg->data_size = msg->data_len + 10;
1008 /* Now calculate the checksum and tack it on. */
1009 smi_msg->data[smi_msg->data_size]
1010 = ipmb_checksum(&(smi_msg->data[7]),
1011 smi_msg->data_size-7);
1013 /* Add on the checksum size and the offset from the
1014 broadcast. */
1015 smi_msg->data_size += 1;
1017 smi_msg->msgid = msgid;
1020 /* Separate from ipmi_request so that the user does not have to be
1021 supplied in certain circumstances (mainly at panic time). If
1022 messages are supplied, they will be freed, even if an error
1023 occurs. */
1024 static inline int i_ipmi_request(ipmi_user_t user,
1025 ipmi_smi_t intf,
1026 struct ipmi_addr *addr,
1027 long msgid,
1028 struct kernel_ipmi_msg *msg,
1029 void *user_msg_data,
1030 void *supplied_smi,
1031 struct ipmi_recv_msg *supplied_recv,
1032 int priority,
1033 unsigned char source_address,
1034 unsigned char source_lun,
1035 int retries,
1036 unsigned int retry_time_ms)
1038 int rv = 0;
1039 struct ipmi_smi_msg *smi_msg;
1040 struct ipmi_recv_msg *recv_msg;
1041 unsigned long flags;
1044 if (supplied_recv) {
1045 recv_msg = supplied_recv;
1046 } else {
1047 recv_msg = ipmi_alloc_recv_msg();
1048 if (recv_msg == NULL) {
1049 return -ENOMEM;
1052 recv_msg->user_msg_data = user_msg_data;
1054 if (supplied_smi) {
1055 smi_msg = (struct ipmi_smi_msg *) supplied_smi;
1056 } else {
1057 smi_msg = ipmi_alloc_smi_msg();
1058 if (smi_msg == NULL) {
1059 ipmi_free_recv_msg(recv_msg);
1060 return -ENOMEM;
1064 recv_msg->user = user;
1065 recv_msg->msgid = msgid;
1066 /* Store the message to send in the receive message so timeout
1067 responses can get the proper response data. */
1068 recv_msg->msg = *msg;
1070 if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
1071 struct ipmi_system_interface_addr *smi_addr;
1073 if (msg->netfn & 1) {
1074 /* Responses are not allowed to the SMI. */
1075 rv = -EINVAL;
1076 goto out_err;
1079 smi_addr = (struct ipmi_system_interface_addr *) addr;
1080 if (smi_addr->lun > 3) {
1081 spin_lock_irqsave(&intf->counter_lock, flags);
1082 intf->sent_invalid_commands++;
1083 spin_unlock_irqrestore(&intf->counter_lock, flags);
1084 rv = -EINVAL;
1085 goto out_err;
1088 memcpy(&recv_msg->addr, smi_addr, sizeof(*smi_addr));
1090 if ((msg->netfn == IPMI_NETFN_APP_REQUEST)
1091 && ((msg->cmd == IPMI_SEND_MSG_CMD)
1092 || (msg->cmd == IPMI_GET_MSG_CMD)
1093 || (msg->cmd == IPMI_READ_EVENT_MSG_BUFFER_CMD)))
1095 /* We don't let the user do these, since we manage
1096 the sequence numbers. */
1097 spin_lock_irqsave(&intf->counter_lock, flags);
1098 intf->sent_invalid_commands++;
1099 spin_unlock_irqrestore(&intf->counter_lock, flags);
1100 rv = -EINVAL;
1101 goto out_err;
1104 if ((msg->data_len + 2) > IPMI_MAX_MSG_LENGTH) {
1105 spin_lock_irqsave(&intf->counter_lock, flags);
1106 intf->sent_invalid_commands++;
1107 spin_unlock_irqrestore(&intf->counter_lock, flags);
1108 rv = -EMSGSIZE;
1109 goto out_err;
1112 smi_msg->data[0] = (msg->netfn << 2) | (smi_addr->lun & 0x3);
1113 smi_msg->data[1] = msg->cmd;
1114 smi_msg->msgid = msgid;
1115 smi_msg->user_data = recv_msg;
1116 if (msg->data_len > 0)
1117 memcpy(&(smi_msg->data[2]), msg->data, msg->data_len);
1118 smi_msg->data_size = msg->data_len + 2;
1119 spin_lock_irqsave(&intf->counter_lock, flags);
1120 intf->sent_local_commands++;
1121 spin_unlock_irqrestore(&intf->counter_lock, flags);
1122 } else if ((addr->addr_type == IPMI_IPMB_ADDR_TYPE)
1123 || (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE))
1125 struct ipmi_ipmb_addr *ipmb_addr;
1126 unsigned char ipmb_seq;
1127 long seqid;
1128 int broadcast = 0;
1130 if (addr->channel >= IPMI_MAX_CHANNELS) {
1131 spin_lock_irqsave(&intf->counter_lock, flags);
1132 intf->sent_invalid_commands++;
1133 spin_unlock_irqrestore(&intf->counter_lock, flags);
1134 rv = -EINVAL;
1135 goto out_err;
1138 if (intf->channels[addr->channel].medium
1139 != IPMI_CHANNEL_MEDIUM_IPMB)
1141 spin_lock_irqsave(&intf->counter_lock, flags);
1142 intf->sent_invalid_commands++;
1143 spin_unlock_irqrestore(&intf->counter_lock, flags);
1144 rv = -EINVAL;
1145 goto out_err;
1148 if (retries < 0) {
1149 if (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE)
1150 retries = 0; /* Don't retry broadcasts. */
1151 else
1152 retries = 4;
1154 if (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE) {
1155 /* Broadcasts add a zero at the beginning of the
1156 message, but otherwise is the same as an IPMB
1157 address. */
1158 addr->addr_type = IPMI_IPMB_ADDR_TYPE;
1159 broadcast = 1;
1163 /* Default to 1 second retries. */
1164 if (retry_time_ms == 0)
1165 retry_time_ms = 1000;
1167 /* 9 for the header and 1 for the checksum, plus
1168 possibly one for the broadcast. */
1169 if ((msg->data_len + 10 + broadcast) > IPMI_MAX_MSG_LENGTH) {
1170 spin_lock_irqsave(&intf->counter_lock, flags);
1171 intf->sent_invalid_commands++;
1172 spin_unlock_irqrestore(&intf->counter_lock, flags);
1173 rv = -EMSGSIZE;
1174 goto out_err;
1177 ipmb_addr = (struct ipmi_ipmb_addr *) addr;
1178 if (ipmb_addr->lun > 3) {
1179 spin_lock_irqsave(&intf->counter_lock, flags);
1180 intf->sent_invalid_commands++;
1181 spin_unlock_irqrestore(&intf->counter_lock, flags);
1182 rv = -EINVAL;
1183 goto out_err;
1186 memcpy(&recv_msg->addr, ipmb_addr, sizeof(*ipmb_addr));
1188 if (recv_msg->msg.netfn & 0x1) {
1189 /* It's a response, so use the user's sequence
1190 from msgid. */
1191 spin_lock_irqsave(&intf->counter_lock, flags);
1192 intf->sent_ipmb_responses++;
1193 spin_unlock_irqrestore(&intf->counter_lock, flags);
1194 format_ipmb_msg(smi_msg, msg, ipmb_addr, msgid,
1195 msgid, broadcast,
1196 source_address, source_lun);
1198 /* Save the receive message so we can use it
1199 to deliver the response. */
1200 smi_msg->user_data = recv_msg;
1201 } else {
1202 /* It's a command, so get a sequence for it. */
1204 spin_lock_irqsave(&(intf->seq_lock), flags);
1206 spin_lock(&intf->counter_lock);
1207 intf->sent_ipmb_commands++;
1208 spin_unlock(&intf->counter_lock);
1210 /* Create a sequence number with a 1 second
1211 timeout and 4 retries. */
1212 rv = intf_next_seq(intf,
1213 recv_msg,
1214 retry_time_ms,
1215 retries,
1216 broadcast,
1217 &ipmb_seq,
1218 &seqid);
1219 if (rv) {
1220 /* We have used up all the sequence numbers,
1221 probably, so abort. */
1222 spin_unlock_irqrestore(&(intf->seq_lock),
1223 flags);
1224 goto out_err;
1227 /* Store the sequence number in the message,
1228 so that when the send message response
1229 comes back we can start the timer. */
1230 format_ipmb_msg(smi_msg, msg, ipmb_addr,
1231 STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
1232 ipmb_seq, broadcast,
1233 source_address, source_lun);
1235 /* Copy the message into the recv message data, so we
1236 can retransmit it later if necessary. */
1237 memcpy(recv_msg->msg_data, smi_msg->data,
1238 smi_msg->data_size);
1239 recv_msg->msg.data = recv_msg->msg_data;
1240 recv_msg->msg.data_len = smi_msg->data_size;
1242 /* We don't unlock until here, because we need
1243 to copy the completed message into the
1244 recv_msg before we release the lock.
1245 Otherwise, race conditions may bite us. I
1246 know that's pretty paranoid, but I prefer
1247 to be correct. */
1248 spin_unlock_irqrestore(&(intf->seq_lock), flags);
1250 } else if (addr->addr_type == IPMI_LAN_ADDR_TYPE) {
1251 struct ipmi_lan_addr *lan_addr;
1252 unsigned char ipmb_seq;
1253 long seqid;
1255 if (addr->channel >= IPMI_NUM_CHANNELS) {
1256 spin_lock_irqsave(&intf->counter_lock, flags);
1257 intf->sent_invalid_commands++;
1258 spin_unlock_irqrestore(&intf->counter_lock, flags);
1259 rv = -EINVAL;
1260 goto out_err;
1263 if ((intf->channels[addr->channel].medium
1264 != IPMI_CHANNEL_MEDIUM_8023LAN)
1265 && (intf->channels[addr->channel].medium
1266 != IPMI_CHANNEL_MEDIUM_ASYNC))
1268 spin_lock_irqsave(&intf->counter_lock, flags);
1269 intf->sent_invalid_commands++;
1270 spin_unlock_irqrestore(&intf->counter_lock, flags);
1271 rv = -EINVAL;
1272 goto out_err;
1275 retries = 4;
1277 /* Default to 1 second retries. */
1278 if (retry_time_ms == 0)
1279 retry_time_ms = 1000;
1281 /* 11 for the header and 1 for the checksum. */
1282 if ((msg->data_len + 12) > IPMI_MAX_MSG_LENGTH) {
1283 spin_lock_irqsave(&intf->counter_lock, flags);
1284 intf->sent_invalid_commands++;
1285 spin_unlock_irqrestore(&intf->counter_lock, flags);
1286 rv = -EMSGSIZE;
1287 goto out_err;
1290 lan_addr = (struct ipmi_lan_addr *) addr;
1291 if (lan_addr->lun > 3) {
1292 spin_lock_irqsave(&intf->counter_lock, flags);
1293 intf->sent_invalid_commands++;
1294 spin_unlock_irqrestore(&intf->counter_lock, flags);
1295 rv = -EINVAL;
1296 goto out_err;
1299 memcpy(&recv_msg->addr, lan_addr, sizeof(*lan_addr));
1301 if (recv_msg->msg.netfn & 0x1) {
1302 /* It's a response, so use the user's sequence
1303 from msgid. */
1304 spin_lock_irqsave(&intf->counter_lock, flags);
1305 intf->sent_lan_responses++;
1306 spin_unlock_irqrestore(&intf->counter_lock, flags);
1307 format_lan_msg(smi_msg, msg, lan_addr, msgid,
1308 msgid, source_lun);
1310 /* Save the receive message so we can use it
1311 to deliver the response. */
1312 smi_msg->user_data = recv_msg;
1313 } else {
1314 /* It's a command, so get a sequence for it. */
1316 spin_lock_irqsave(&(intf->seq_lock), flags);
1318 spin_lock(&intf->counter_lock);
1319 intf->sent_lan_commands++;
1320 spin_unlock(&intf->counter_lock);
1322 /* Create a sequence number with a 1 second
1323 timeout and 4 retries. */
1324 rv = intf_next_seq(intf,
1325 recv_msg,
1326 retry_time_ms,
1327 retries,
1329 &ipmb_seq,
1330 &seqid);
1331 if (rv) {
1332 /* We have used up all the sequence numbers,
1333 probably, so abort. */
1334 spin_unlock_irqrestore(&(intf->seq_lock),
1335 flags);
1336 goto out_err;
1339 /* Store the sequence number in the message,
1340 so that when the send message response
1341 comes back we can start the timer. */
1342 format_lan_msg(smi_msg, msg, lan_addr,
1343 STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
1344 ipmb_seq, source_lun);
1346 /* Copy the message into the recv message data, so we
1347 can retransmit it later if necessary. */
1348 memcpy(recv_msg->msg_data, smi_msg->data,
1349 smi_msg->data_size);
1350 recv_msg->msg.data = recv_msg->msg_data;
1351 recv_msg->msg.data_len = smi_msg->data_size;
1353 /* We don't unlock until here, because we need
1354 to copy the completed message into the
1355 recv_msg before we release the lock.
1356 Otherwise, race conditions may bite us. I
1357 know that's pretty paranoid, but I prefer
1358 to be correct. */
1359 spin_unlock_irqrestore(&(intf->seq_lock), flags);
1361 } else {
1362 /* Unknown address type. */
1363 spin_lock_irqsave(&intf->counter_lock, flags);
1364 intf->sent_invalid_commands++;
1365 spin_unlock_irqrestore(&intf->counter_lock, flags);
1366 rv = -EINVAL;
1367 goto out_err;
1370 #ifdef DEBUG_MSGING
1372 int m;
1373 for (m=0; m<smi_msg->data_size; m++)
1374 printk(" %2.2x", smi_msg->data[m]);
1375 printk("\n");
1377 #endif
1378 intf->handlers->sender(intf->send_info, smi_msg, priority);
1380 return 0;
1382 out_err:
1383 ipmi_free_smi_msg(smi_msg);
1384 ipmi_free_recv_msg(recv_msg);
1385 return rv;
1388 static int check_addr(ipmi_smi_t intf,
1389 struct ipmi_addr *addr,
1390 unsigned char *saddr,
1391 unsigned char *lun)
1393 if (addr->channel >= IPMI_MAX_CHANNELS)
1394 return -EINVAL;
1395 *lun = intf->channels[addr->channel].lun;
1396 *saddr = intf->channels[addr->channel].address;
1397 return 0;
1400 int ipmi_request_settime(ipmi_user_t user,
1401 struct ipmi_addr *addr,
1402 long msgid,
1403 struct kernel_ipmi_msg *msg,
1404 void *user_msg_data,
1405 int priority,
1406 int retries,
1407 unsigned int retry_time_ms)
1409 unsigned char saddr, lun;
1410 int rv;
1412 if (! user)
1413 return -EINVAL;
1414 rv = check_addr(user->intf, addr, &saddr, &lun);
1415 if (rv)
1416 return rv;
1417 return i_ipmi_request(user,
1418 user->intf,
1419 addr,
1420 msgid,
1421 msg,
1422 user_msg_data,
1423 NULL, NULL,
1424 priority,
1425 saddr,
1426 lun,
1427 retries,
1428 retry_time_ms);
1431 int ipmi_request_supply_msgs(ipmi_user_t user,
1432 struct ipmi_addr *addr,
1433 long msgid,
1434 struct kernel_ipmi_msg *msg,
1435 void *user_msg_data,
1436 void *supplied_smi,
1437 struct ipmi_recv_msg *supplied_recv,
1438 int priority)
1440 unsigned char saddr, lun;
1441 int rv;
1443 if (! user)
1444 return -EINVAL;
1445 rv = check_addr(user->intf, addr, &saddr, &lun);
1446 if (rv)
1447 return rv;
1448 return i_ipmi_request(user,
1449 user->intf,
1450 addr,
1451 msgid,
1452 msg,
1453 user_msg_data,
1454 supplied_smi,
1455 supplied_recv,
1456 priority,
1457 saddr,
1458 lun,
1459 -1, 0);
1462 static int ipmb_file_read_proc(char *page, char **start, off_t off,
1463 int count, int *eof, void *data)
1465 char *out = (char *) page;
1466 ipmi_smi_t intf = data;
1467 int i;
1468 int rv= 0;
1470 for (i=0; i<IPMI_MAX_CHANNELS; i++)
1471 rv += sprintf(out+rv, "%x ", intf->channels[i].address);
1472 out[rv-1] = '\n'; /* Replace the final space with a newline */
1473 out[rv] = '\0';
1474 rv++;
1475 return rv;
1478 static int version_file_read_proc(char *page, char **start, off_t off,
1479 int count, int *eof, void *data)
1481 char *out = (char *) page;
1482 ipmi_smi_t intf = data;
1484 return sprintf(out, "%d.%d\n",
1485 intf->version_major, intf->version_minor);
1488 static int stat_file_read_proc(char *page, char **start, off_t off,
1489 int count, int *eof, void *data)
1491 char *out = (char *) page;
1492 ipmi_smi_t intf = data;
1494 out += sprintf(out, "sent_invalid_commands: %d\n",
1495 intf->sent_invalid_commands);
1496 out += sprintf(out, "sent_local_commands: %d\n",
1497 intf->sent_local_commands);
1498 out += sprintf(out, "handled_local_responses: %d\n",
1499 intf->handled_local_responses);
1500 out += sprintf(out, "unhandled_local_responses: %d\n",
1501 intf->unhandled_local_responses);
1502 out += sprintf(out, "sent_ipmb_commands: %d\n",
1503 intf->sent_ipmb_commands);
1504 out += sprintf(out, "sent_ipmb_command_errs: %d\n",
1505 intf->sent_ipmb_command_errs);
1506 out += sprintf(out, "retransmitted_ipmb_commands: %d\n",
1507 intf->retransmitted_ipmb_commands);
1508 out += sprintf(out, "timed_out_ipmb_commands: %d\n",
1509 intf->timed_out_ipmb_commands);
1510 out += sprintf(out, "timed_out_ipmb_broadcasts: %d\n",
1511 intf->timed_out_ipmb_broadcasts);
1512 out += sprintf(out, "sent_ipmb_responses: %d\n",
1513 intf->sent_ipmb_responses);
1514 out += sprintf(out, "handled_ipmb_responses: %d\n",
1515 intf->handled_ipmb_responses);
1516 out += sprintf(out, "invalid_ipmb_responses: %d\n",
1517 intf->invalid_ipmb_responses);
1518 out += sprintf(out, "unhandled_ipmb_responses: %d\n",
1519 intf->unhandled_ipmb_responses);
1520 out += sprintf(out, "sent_lan_commands: %d\n",
1521 intf->sent_lan_commands);
1522 out += sprintf(out, "sent_lan_command_errs: %d\n",
1523 intf->sent_lan_command_errs);
1524 out += sprintf(out, "retransmitted_lan_commands: %d\n",
1525 intf->retransmitted_lan_commands);
1526 out += sprintf(out, "timed_out_lan_commands: %d\n",
1527 intf->timed_out_lan_commands);
1528 out += sprintf(out, "sent_lan_responses: %d\n",
1529 intf->sent_lan_responses);
1530 out += sprintf(out, "handled_lan_responses: %d\n",
1531 intf->handled_lan_responses);
1532 out += sprintf(out, "invalid_lan_responses: %d\n",
1533 intf->invalid_lan_responses);
1534 out += sprintf(out, "unhandled_lan_responses: %d\n",
1535 intf->unhandled_lan_responses);
1536 out += sprintf(out, "handled_commands: %d\n",
1537 intf->handled_commands);
1538 out += sprintf(out, "invalid_commands: %d\n",
1539 intf->invalid_commands);
1540 out += sprintf(out, "unhandled_commands: %d\n",
1541 intf->unhandled_commands);
1542 out += sprintf(out, "invalid_events: %d\n",
1543 intf->invalid_events);
1544 out += sprintf(out, "events: %d\n",
1545 intf->events);
1547 return (out - ((char *) page));
1550 int ipmi_smi_add_proc_entry(ipmi_smi_t smi, char *name,
1551 read_proc_t *read_proc, write_proc_t *write_proc,
1552 void *data, struct module *owner)
1554 int rv = 0;
1555 #ifdef CONFIG_PROC_FS
1556 struct proc_dir_entry *file;
1557 struct ipmi_proc_entry *entry;
1559 /* Create a list element. */
1560 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1561 if (!entry)
1562 return -ENOMEM;
1563 entry->name = kmalloc(strlen(name)+1, GFP_KERNEL);
1564 if (!entry->name) {
1565 kfree(entry);
1566 return -ENOMEM;
1568 strcpy(entry->name, name);
1570 file = create_proc_entry(name, 0, smi->proc_dir);
1571 if (!file) {
1572 kfree(entry->name);
1573 kfree(entry);
1574 rv = -ENOMEM;
1575 } else {
1576 file->nlink = 1;
1577 file->data = data;
1578 file->read_proc = read_proc;
1579 file->write_proc = write_proc;
1580 file->owner = owner;
1582 spin_lock(&smi->proc_entry_lock);
1583 /* Stick it on the list. */
1584 entry->next = smi->proc_entries;
1585 smi->proc_entries = entry;
1586 spin_unlock(&smi->proc_entry_lock);
1588 #endif /* CONFIG_PROC_FS */
1590 return rv;
1593 static int add_proc_entries(ipmi_smi_t smi, int num)
1595 int rv = 0;
1597 #ifdef CONFIG_PROC_FS
1598 sprintf(smi->proc_dir_name, "%d", num);
1599 smi->proc_dir = proc_mkdir(smi->proc_dir_name, proc_ipmi_root);
1600 if (!smi->proc_dir)
1601 rv = -ENOMEM;
1602 else {
1603 smi->proc_dir->owner = THIS_MODULE;
1606 if (rv == 0)
1607 rv = ipmi_smi_add_proc_entry(smi, "stats",
1608 stat_file_read_proc, NULL,
1609 smi, THIS_MODULE);
1611 if (rv == 0)
1612 rv = ipmi_smi_add_proc_entry(smi, "ipmb",
1613 ipmb_file_read_proc, NULL,
1614 smi, THIS_MODULE);
1616 if (rv == 0)
1617 rv = ipmi_smi_add_proc_entry(smi, "version",
1618 version_file_read_proc, NULL,
1619 smi, THIS_MODULE);
1620 #endif /* CONFIG_PROC_FS */
1622 return rv;
1625 static void remove_proc_entries(ipmi_smi_t smi)
1627 #ifdef CONFIG_PROC_FS
1628 struct ipmi_proc_entry *entry;
1630 spin_lock(&smi->proc_entry_lock);
1631 while (smi->proc_entries) {
1632 entry = smi->proc_entries;
1633 smi->proc_entries = entry->next;
1635 remove_proc_entry(entry->name, smi->proc_dir);
1636 kfree(entry->name);
1637 kfree(entry);
1639 spin_unlock(&smi->proc_entry_lock);
1640 remove_proc_entry(smi->proc_dir_name, proc_ipmi_root);
1641 #endif /* CONFIG_PROC_FS */
1644 static int
1645 send_channel_info_cmd(ipmi_smi_t intf, int chan)
1647 struct kernel_ipmi_msg msg;
1648 unsigned char data[1];
1649 struct ipmi_system_interface_addr si;
1651 si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
1652 si.channel = IPMI_BMC_CHANNEL;
1653 si.lun = 0;
1655 msg.netfn = IPMI_NETFN_APP_REQUEST;
1656 msg.cmd = IPMI_GET_CHANNEL_INFO_CMD;
1657 msg.data = data;
1658 msg.data_len = 1;
1659 data[0] = chan;
1660 return i_ipmi_request(NULL,
1661 intf,
1662 (struct ipmi_addr *) &si,
1664 &msg,
1665 intf,
1666 NULL,
1667 NULL,
1669 intf->channels[0].address,
1670 intf->channels[0].lun,
1671 -1, 0);
1674 static void
1675 channel_handler(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
1677 int rv = 0;
1678 int chan;
1680 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
1681 && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
1682 && (msg->msg.cmd == IPMI_GET_CHANNEL_INFO_CMD))
1684 /* It's the one we want */
1685 if (msg->msg.data[0] != 0) {
1686 /* Got an error from the channel, just go on. */
1688 if (msg->msg.data[0] == IPMI_INVALID_COMMAND_ERR) {
1689 /* If the MC does not support this
1690 command, that is legal. We just
1691 assume it has one IPMB at channel
1692 zero. */
1693 intf->channels[0].medium
1694 = IPMI_CHANNEL_MEDIUM_IPMB;
1695 intf->channels[0].protocol
1696 = IPMI_CHANNEL_PROTOCOL_IPMB;
1697 rv = -ENOSYS;
1699 intf->curr_channel = IPMI_MAX_CHANNELS;
1700 wake_up(&intf->waitq);
1701 goto out;
1703 goto next_channel;
1705 if (msg->msg.data_len < 4) {
1706 /* Message not big enough, just go on. */
1707 goto next_channel;
1709 chan = intf->curr_channel;
1710 intf->channels[chan].medium = msg->msg.data[2] & 0x7f;
1711 intf->channels[chan].protocol = msg->msg.data[3] & 0x1f;
1713 next_channel:
1714 intf->curr_channel++;
1715 if (intf->curr_channel >= IPMI_MAX_CHANNELS)
1716 wake_up(&intf->waitq);
1717 else
1718 rv = send_channel_info_cmd(intf, intf->curr_channel);
1720 if (rv) {
1721 /* Got an error somehow, just give up. */
1722 intf->curr_channel = IPMI_MAX_CHANNELS;
1723 wake_up(&intf->waitq);
1725 printk(KERN_WARNING PFX
1726 "Error sending channel information: %d\n",
1727 rv);
1730 out:
1731 return;
1734 int ipmi_register_smi(struct ipmi_smi_handlers *handlers,
1735 void *send_info,
1736 unsigned char version_major,
1737 unsigned char version_minor,
1738 unsigned char slave_addr,
1739 ipmi_smi_t *intf)
1741 int i, j;
1742 int rv;
1743 ipmi_smi_t new_intf;
1744 unsigned long flags;
1747 /* Make sure the driver is actually initialized, this handles
1748 problems with initialization order. */
1749 if (!initialized) {
1750 rv = ipmi_init_msghandler();
1751 if (rv)
1752 return rv;
1753 /* The init code doesn't return an error if it was turned
1754 off, but it won't initialize. Check that. */
1755 if (!initialized)
1756 return -ENODEV;
1759 new_intf = kmalloc(sizeof(*new_intf), GFP_KERNEL);
1760 if (!new_intf)
1761 return -ENOMEM;
1762 memset(new_intf, 0, sizeof(*new_intf));
1764 new_intf->proc_dir = NULL;
1766 rv = -ENOMEM;
1768 down_write(&interfaces_sem);
1769 for (i=0; i<MAX_IPMI_INTERFACES; i++) {
1770 if (ipmi_interfaces[i] == NULL) {
1771 new_intf->intf_num = i;
1772 new_intf->version_major = version_major;
1773 new_intf->version_minor = version_minor;
1774 for (j=0; j<IPMI_MAX_CHANNELS; j++) {
1775 new_intf->channels[j].address
1776 = IPMI_BMC_SLAVE_ADDR;
1777 new_intf->channels[j].lun = 2;
1779 if (slave_addr != 0)
1780 new_intf->channels[0].address = slave_addr;
1781 rwlock_init(&(new_intf->users_lock));
1782 INIT_LIST_HEAD(&(new_intf->users));
1783 new_intf->handlers = handlers;
1784 new_intf->send_info = send_info;
1785 spin_lock_init(&(new_intf->seq_lock));
1786 for (j=0; j<IPMI_IPMB_NUM_SEQ; j++) {
1787 new_intf->seq_table[j].inuse = 0;
1788 new_intf->seq_table[j].seqid = 0;
1790 new_intf->curr_seq = 0;
1791 #ifdef CONFIG_PROC_FS
1792 spin_lock_init(&(new_intf->proc_entry_lock));
1793 #endif
1794 spin_lock_init(&(new_intf->waiting_msgs_lock));
1795 INIT_LIST_HEAD(&(new_intf->waiting_msgs));
1796 spin_lock_init(&(new_intf->events_lock));
1797 INIT_LIST_HEAD(&(new_intf->waiting_events));
1798 new_intf->waiting_events_count = 0;
1799 rwlock_init(&(new_intf->cmd_rcvr_lock));
1800 init_waitqueue_head(&new_intf->waitq);
1801 INIT_LIST_HEAD(&(new_intf->cmd_rcvrs));
1802 new_intf->all_cmd_rcvr = NULL;
1804 spin_lock_init(&(new_intf->counter_lock));
1806 spin_lock_irqsave(&interfaces_lock, flags);
1807 ipmi_interfaces[i] = new_intf;
1808 spin_unlock_irqrestore(&interfaces_lock, flags);
1810 rv = 0;
1811 *intf = new_intf;
1812 break;
1816 downgrade_write(&interfaces_sem);
1818 if (rv == 0)
1819 rv = add_proc_entries(*intf, i);
1821 if (rv == 0) {
1822 if ((version_major > 1)
1823 || ((version_major == 1) && (version_minor >= 5)))
1825 /* Start scanning the channels to see what is
1826 available. */
1827 (*intf)->null_user_handler = channel_handler;
1828 (*intf)->curr_channel = 0;
1829 rv = send_channel_info_cmd(*intf, 0);
1830 if (rv)
1831 goto out;
1833 /* Wait for the channel info to be read. */
1834 up_read(&interfaces_sem);
1835 wait_event((*intf)->waitq,
1836 ((*intf)->curr_channel>=IPMI_MAX_CHANNELS));
1837 down_read(&interfaces_sem);
1839 if (ipmi_interfaces[i] != new_intf)
1840 /* Well, it went away. Just return. */
1841 goto out;
1842 } else {
1843 /* Assume a single IPMB channel at zero. */
1844 (*intf)->channels[0].medium = IPMI_CHANNEL_MEDIUM_IPMB;
1845 (*intf)->channels[0].protocol
1846 = IPMI_CHANNEL_PROTOCOL_IPMB;
1849 /* Call all the watcher interfaces to tell
1850 them that a new interface is available. */
1851 call_smi_watchers(i);
1854 out:
1855 up_read(&interfaces_sem);
1857 if (rv) {
1858 if (new_intf->proc_dir)
1859 remove_proc_entries(new_intf);
1860 kfree(new_intf);
1863 return rv;
1866 static void free_recv_msg_list(struct list_head *q)
1868 struct ipmi_recv_msg *msg, *msg2;
1870 list_for_each_entry_safe(msg, msg2, q, link) {
1871 list_del(&msg->link);
1872 ipmi_free_recv_msg(msg);
1876 static void free_cmd_rcvr_list(struct list_head *q)
1878 struct cmd_rcvr *rcvr, *rcvr2;
1880 list_for_each_entry_safe(rcvr, rcvr2, q, link) {
1881 list_del(&rcvr->link);
1882 kfree(rcvr);
1886 static void clean_up_interface_data(ipmi_smi_t intf)
1888 int i;
1890 free_recv_msg_list(&(intf->waiting_msgs));
1891 free_recv_msg_list(&(intf->waiting_events));
1892 free_cmd_rcvr_list(&(intf->cmd_rcvrs));
1894 for (i=0; i<IPMI_IPMB_NUM_SEQ; i++) {
1895 if ((intf->seq_table[i].inuse)
1896 && (intf->seq_table[i].recv_msg))
1898 ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
1903 int ipmi_unregister_smi(ipmi_smi_t intf)
1905 int rv = -ENODEV;
1906 int i;
1907 struct ipmi_smi_watcher *w;
1908 unsigned long flags;
1910 down_write(&interfaces_sem);
1911 if (list_empty(&(intf->users)))
1913 for (i=0; i<MAX_IPMI_INTERFACES; i++) {
1914 if (ipmi_interfaces[i] == intf) {
1915 remove_proc_entries(intf);
1916 spin_lock_irqsave(&interfaces_lock, flags);
1917 ipmi_interfaces[i] = NULL;
1918 clean_up_interface_data(intf);
1919 spin_unlock_irqrestore(&interfaces_lock,flags);
1920 kfree(intf);
1921 rv = 0;
1922 goto out_call_watcher;
1925 } else {
1926 rv = -EBUSY;
1928 up_write(&interfaces_sem);
1930 return rv;
1932 out_call_watcher:
1933 downgrade_write(&interfaces_sem);
1935 /* Call all the watcher interfaces to tell them that
1936 an interface is gone. */
1937 down_read(&smi_watchers_sem);
1938 list_for_each_entry(w, &smi_watchers, link) {
1939 w->smi_gone(i);
1941 up_read(&smi_watchers_sem);
1942 up_read(&interfaces_sem);
1943 return 0;
1946 static int handle_ipmb_get_msg_rsp(ipmi_smi_t intf,
1947 struct ipmi_smi_msg *msg)
1949 struct ipmi_ipmb_addr ipmb_addr;
1950 struct ipmi_recv_msg *recv_msg;
1951 unsigned long flags;
1954 /* This is 11, not 10, because the response must contain a
1955 * completion code. */
1956 if (msg->rsp_size < 11) {
1957 /* Message not big enough, just ignore it. */
1958 spin_lock_irqsave(&intf->counter_lock, flags);
1959 intf->invalid_ipmb_responses++;
1960 spin_unlock_irqrestore(&intf->counter_lock, flags);
1961 return 0;
1964 if (msg->rsp[2] != 0) {
1965 /* An error getting the response, just ignore it. */
1966 return 0;
1969 ipmb_addr.addr_type = IPMI_IPMB_ADDR_TYPE;
1970 ipmb_addr.slave_addr = msg->rsp[6];
1971 ipmb_addr.channel = msg->rsp[3] & 0x0f;
1972 ipmb_addr.lun = msg->rsp[7] & 3;
1974 /* It's a response from a remote entity. Look up the sequence
1975 number and handle the response. */
1976 if (intf_find_seq(intf,
1977 msg->rsp[7] >> 2,
1978 msg->rsp[3] & 0x0f,
1979 msg->rsp[8],
1980 (msg->rsp[4] >> 2) & (~1),
1981 (struct ipmi_addr *) &(ipmb_addr),
1982 &recv_msg))
1984 /* We were unable to find the sequence number,
1985 so just nuke the message. */
1986 spin_lock_irqsave(&intf->counter_lock, flags);
1987 intf->unhandled_ipmb_responses++;
1988 spin_unlock_irqrestore(&intf->counter_lock, flags);
1989 return 0;
1992 memcpy(recv_msg->msg_data,
1993 &(msg->rsp[9]),
1994 msg->rsp_size - 9);
1995 /* THe other fields matched, so no need to set them, except
1996 for netfn, which needs to be the response that was
1997 returned, not the request value. */
1998 recv_msg->msg.netfn = msg->rsp[4] >> 2;
1999 recv_msg->msg.data = recv_msg->msg_data;
2000 recv_msg->msg.data_len = msg->rsp_size - 10;
2001 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
2002 spin_lock_irqsave(&intf->counter_lock, flags);
2003 intf->handled_ipmb_responses++;
2004 spin_unlock_irqrestore(&intf->counter_lock, flags);
2005 deliver_response(recv_msg);
2007 return 0;
2010 static int handle_ipmb_get_msg_cmd(ipmi_smi_t intf,
2011 struct ipmi_smi_msg *msg)
2013 struct cmd_rcvr *rcvr;
2014 int rv = 0;
2015 unsigned char netfn;
2016 unsigned char cmd;
2017 ipmi_user_t user = NULL;
2018 struct ipmi_ipmb_addr *ipmb_addr;
2019 struct ipmi_recv_msg *recv_msg;
2020 unsigned long flags;
2022 if (msg->rsp_size < 10) {
2023 /* Message not big enough, just ignore it. */
2024 spin_lock_irqsave(&intf->counter_lock, flags);
2025 intf->invalid_commands++;
2026 spin_unlock_irqrestore(&intf->counter_lock, flags);
2027 return 0;
2030 if (msg->rsp[2] != 0) {
2031 /* An error getting the response, just ignore it. */
2032 return 0;
2035 netfn = msg->rsp[4] >> 2;
2036 cmd = msg->rsp[8];
2038 read_lock(&(intf->cmd_rcvr_lock));
2040 if (intf->all_cmd_rcvr) {
2041 user = intf->all_cmd_rcvr;
2042 } else {
2043 /* Find the command/netfn. */
2044 list_for_each_entry(rcvr, &(intf->cmd_rcvrs), link) {
2045 if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)) {
2046 user = rcvr->user;
2047 break;
2051 read_unlock(&(intf->cmd_rcvr_lock));
2053 if (user == NULL) {
2054 /* We didn't find a user, deliver an error response. */
2055 spin_lock_irqsave(&intf->counter_lock, flags);
2056 intf->unhandled_commands++;
2057 spin_unlock_irqrestore(&intf->counter_lock, flags);
2059 msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
2060 msg->data[1] = IPMI_SEND_MSG_CMD;
2061 msg->data[2] = msg->rsp[3];
2062 msg->data[3] = msg->rsp[6];
2063 msg->data[4] = ((netfn + 1) << 2) | (msg->rsp[7] & 0x3);
2064 msg->data[5] = ipmb_checksum(&(msg->data[3]), 2);
2065 msg->data[6] = intf->channels[msg->rsp[3] & 0xf].address;
2066 /* rqseq/lun */
2067 msg->data[7] = (msg->rsp[7] & 0xfc) | (msg->rsp[4] & 0x3);
2068 msg->data[8] = msg->rsp[8]; /* cmd */
2069 msg->data[9] = IPMI_INVALID_CMD_COMPLETION_CODE;
2070 msg->data[10] = ipmb_checksum(&(msg->data[6]), 4);
2071 msg->data_size = 11;
2073 #ifdef DEBUG_MSGING
2075 int m;
2076 printk("Invalid command:");
2077 for (m=0; m<msg->data_size; m++)
2078 printk(" %2.2x", msg->data[m]);
2079 printk("\n");
2081 #endif
2082 intf->handlers->sender(intf->send_info, msg, 0);
2084 rv = -1; /* We used the message, so return the value that
2085 causes it to not be freed or queued. */
2086 } else {
2087 /* Deliver the message to the user. */
2088 spin_lock_irqsave(&intf->counter_lock, flags);
2089 intf->handled_commands++;
2090 spin_unlock_irqrestore(&intf->counter_lock, flags);
2092 recv_msg = ipmi_alloc_recv_msg();
2093 if (! recv_msg) {
2094 /* We couldn't allocate memory for the
2095 message, so requeue it for handling
2096 later. */
2097 rv = 1;
2098 } else {
2099 /* Extract the source address from the data. */
2100 ipmb_addr = (struct ipmi_ipmb_addr *) &recv_msg->addr;
2101 ipmb_addr->addr_type = IPMI_IPMB_ADDR_TYPE;
2102 ipmb_addr->slave_addr = msg->rsp[6];
2103 ipmb_addr->lun = msg->rsp[7] & 3;
2104 ipmb_addr->channel = msg->rsp[3] & 0xf;
2106 /* Extract the rest of the message information
2107 from the IPMB header.*/
2108 recv_msg->user = user;
2109 recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
2110 recv_msg->msgid = msg->rsp[7] >> 2;
2111 recv_msg->msg.netfn = msg->rsp[4] >> 2;
2112 recv_msg->msg.cmd = msg->rsp[8];
2113 recv_msg->msg.data = recv_msg->msg_data;
2115 /* We chop off 10, not 9 bytes because the checksum
2116 at the end also needs to be removed. */
2117 recv_msg->msg.data_len = msg->rsp_size - 10;
2118 memcpy(recv_msg->msg_data,
2119 &(msg->rsp[9]),
2120 msg->rsp_size - 10);
2121 deliver_response(recv_msg);
2125 return rv;
2128 static int handle_lan_get_msg_rsp(ipmi_smi_t intf,
2129 struct ipmi_smi_msg *msg)
2131 struct ipmi_lan_addr lan_addr;
2132 struct ipmi_recv_msg *recv_msg;
2133 unsigned long flags;
2136 /* This is 13, not 12, because the response must contain a
2137 * completion code. */
2138 if (msg->rsp_size < 13) {
2139 /* Message not big enough, just ignore it. */
2140 spin_lock_irqsave(&intf->counter_lock, flags);
2141 intf->invalid_lan_responses++;
2142 spin_unlock_irqrestore(&intf->counter_lock, flags);
2143 return 0;
2146 if (msg->rsp[2] != 0) {
2147 /* An error getting the response, just ignore it. */
2148 return 0;
2151 lan_addr.addr_type = IPMI_LAN_ADDR_TYPE;
2152 lan_addr.session_handle = msg->rsp[4];
2153 lan_addr.remote_SWID = msg->rsp[8];
2154 lan_addr.local_SWID = msg->rsp[5];
2155 lan_addr.channel = msg->rsp[3] & 0x0f;
2156 lan_addr.privilege = msg->rsp[3] >> 4;
2157 lan_addr.lun = msg->rsp[9] & 3;
2159 /* It's a response from a remote entity. Look up the sequence
2160 number and handle the response. */
2161 if (intf_find_seq(intf,
2162 msg->rsp[9] >> 2,
2163 msg->rsp[3] & 0x0f,
2164 msg->rsp[10],
2165 (msg->rsp[6] >> 2) & (~1),
2166 (struct ipmi_addr *) &(lan_addr),
2167 &recv_msg))
2169 /* We were unable to find the sequence number,
2170 so just nuke the message. */
2171 spin_lock_irqsave(&intf->counter_lock, flags);
2172 intf->unhandled_lan_responses++;
2173 spin_unlock_irqrestore(&intf->counter_lock, flags);
2174 return 0;
2177 memcpy(recv_msg->msg_data,
2178 &(msg->rsp[11]),
2179 msg->rsp_size - 11);
2180 /* The other fields matched, so no need to set them, except
2181 for netfn, which needs to be the response that was
2182 returned, not the request value. */
2183 recv_msg->msg.netfn = msg->rsp[6] >> 2;
2184 recv_msg->msg.data = recv_msg->msg_data;
2185 recv_msg->msg.data_len = msg->rsp_size - 12;
2186 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
2187 spin_lock_irqsave(&intf->counter_lock, flags);
2188 intf->handled_lan_responses++;
2189 spin_unlock_irqrestore(&intf->counter_lock, flags);
2190 deliver_response(recv_msg);
2192 return 0;
2195 static int handle_lan_get_msg_cmd(ipmi_smi_t intf,
2196 struct ipmi_smi_msg *msg)
2198 struct cmd_rcvr *rcvr;
2199 int rv = 0;
2200 unsigned char netfn;
2201 unsigned char cmd;
2202 ipmi_user_t user = NULL;
2203 struct ipmi_lan_addr *lan_addr;
2204 struct ipmi_recv_msg *recv_msg;
2205 unsigned long flags;
2207 if (msg->rsp_size < 12) {
2208 /* Message not big enough, just ignore it. */
2209 spin_lock_irqsave(&intf->counter_lock, flags);
2210 intf->invalid_commands++;
2211 spin_unlock_irqrestore(&intf->counter_lock, flags);
2212 return 0;
2215 if (msg->rsp[2] != 0) {
2216 /* An error getting the response, just ignore it. */
2217 return 0;
2220 netfn = msg->rsp[6] >> 2;
2221 cmd = msg->rsp[10];
2223 read_lock(&(intf->cmd_rcvr_lock));
2225 if (intf->all_cmd_rcvr) {
2226 user = intf->all_cmd_rcvr;
2227 } else {
2228 /* Find the command/netfn. */
2229 list_for_each_entry(rcvr, &(intf->cmd_rcvrs), link) {
2230 if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)) {
2231 user = rcvr->user;
2232 break;
2236 read_unlock(&(intf->cmd_rcvr_lock));
2238 if (user == NULL) {
2239 /* We didn't find a user, deliver an error response. */
2240 spin_lock_irqsave(&intf->counter_lock, flags);
2241 intf->unhandled_commands++;
2242 spin_unlock_irqrestore(&intf->counter_lock, flags);
2244 rv = 0; /* Don't do anything with these messages, just
2245 allow them to be freed. */
2246 } else {
2247 /* Deliver the message to the user. */
2248 spin_lock_irqsave(&intf->counter_lock, flags);
2249 intf->handled_commands++;
2250 spin_unlock_irqrestore(&intf->counter_lock, flags);
2252 recv_msg = ipmi_alloc_recv_msg();
2253 if (! recv_msg) {
2254 /* We couldn't allocate memory for the
2255 message, so requeue it for handling
2256 later. */
2257 rv = 1;
2258 } else {
2259 /* Extract the source address from the data. */
2260 lan_addr = (struct ipmi_lan_addr *) &recv_msg->addr;
2261 lan_addr->addr_type = IPMI_LAN_ADDR_TYPE;
2262 lan_addr->session_handle = msg->rsp[4];
2263 lan_addr->remote_SWID = msg->rsp[8];
2264 lan_addr->local_SWID = msg->rsp[5];
2265 lan_addr->lun = msg->rsp[9] & 3;
2266 lan_addr->channel = msg->rsp[3] & 0xf;
2267 lan_addr->privilege = msg->rsp[3] >> 4;
2269 /* Extract the rest of the message information
2270 from the IPMB header.*/
2271 recv_msg->user = user;
2272 recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
2273 recv_msg->msgid = msg->rsp[9] >> 2;
2274 recv_msg->msg.netfn = msg->rsp[6] >> 2;
2275 recv_msg->msg.cmd = msg->rsp[10];
2276 recv_msg->msg.data = recv_msg->msg_data;
2278 /* We chop off 12, not 11 bytes because the checksum
2279 at the end also needs to be removed. */
2280 recv_msg->msg.data_len = msg->rsp_size - 12;
2281 memcpy(recv_msg->msg_data,
2282 &(msg->rsp[11]),
2283 msg->rsp_size - 12);
2284 deliver_response(recv_msg);
2288 return rv;
2291 static void copy_event_into_recv_msg(struct ipmi_recv_msg *recv_msg,
2292 struct ipmi_smi_msg *msg)
2294 struct ipmi_system_interface_addr *smi_addr;
2296 recv_msg->msgid = 0;
2297 smi_addr = (struct ipmi_system_interface_addr *) &(recv_msg->addr);
2298 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2299 smi_addr->channel = IPMI_BMC_CHANNEL;
2300 smi_addr->lun = msg->rsp[0] & 3;
2301 recv_msg->recv_type = IPMI_ASYNC_EVENT_RECV_TYPE;
2302 recv_msg->msg.netfn = msg->rsp[0] >> 2;
2303 recv_msg->msg.cmd = msg->rsp[1];
2304 memcpy(recv_msg->msg_data, &(msg->rsp[3]), msg->rsp_size - 3);
2305 recv_msg->msg.data = recv_msg->msg_data;
2306 recv_msg->msg.data_len = msg->rsp_size - 3;
2309 /* This will be called with the intf->users_lock read-locked, so no need
2310 to do that here. */
2311 static int handle_read_event_rsp(ipmi_smi_t intf,
2312 struct ipmi_smi_msg *msg)
2314 struct ipmi_recv_msg *recv_msg, *recv_msg2;
2315 struct list_head msgs;
2316 ipmi_user_t user;
2317 int rv = 0;
2318 int deliver_count = 0;
2319 unsigned long flags;
2321 if (msg->rsp_size < 19) {
2322 /* Message is too small to be an IPMB event. */
2323 spin_lock_irqsave(&intf->counter_lock, flags);
2324 intf->invalid_events++;
2325 spin_unlock_irqrestore(&intf->counter_lock, flags);
2326 return 0;
2329 if (msg->rsp[2] != 0) {
2330 /* An error getting the event, just ignore it. */
2331 return 0;
2334 INIT_LIST_HEAD(&msgs);
2336 spin_lock_irqsave(&(intf->events_lock), flags);
2338 spin_lock(&intf->counter_lock);
2339 intf->events++;
2340 spin_unlock(&intf->counter_lock);
2342 /* Allocate and fill in one message for every user that is getting
2343 events. */
2344 list_for_each_entry(user, &(intf->users), link) {
2345 if (! user->gets_events)
2346 continue;
2348 recv_msg = ipmi_alloc_recv_msg();
2349 if (! recv_msg) {
2350 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs, link) {
2351 list_del(&recv_msg->link);
2352 ipmi_free_recv_msg(recv_msg);
2354 /* We couldn't allocate memory for the
2355 message, so requeue it for handling
2356 later. */
2357 rv = 1;
2358 goto out;
2361 deliver_count++;
2363 copy_event_into_recv_msg(recv_msg, msg);
2364 recv_msg->user = user;
2365 list_add_tail(&(recv_msg->link), &msgs);
2368 if (deliver_count) {
2369 /* Now deliver all the messages. */
2370 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs, link) {
2371 list_del(&recv_msg->link);
2372 deliver_response(recv_msg);
2374 } else if (intf->waiting_events_count < MAX_EVENTS_IN_QUEUE) {
2375 /* No one to receive the message, put it in queue if there's
2376 not already too many things in the queue. */
2377 recv_msg = ipmi_alloc_recv_msg();
2378 if (! recv_msg) {
2379 /* We couldn't allocate memory for the
2380 message, so requeue it for handling
2381 later. */
2382 rv = 1;
2383 goto out;
2386 copy_event_into_recv_msg(recv_msg, msg);
2387 list_add_tail(&(recv_msg->link), &(intf->waiting_events));
2388 } else {
2389 /* There's too many things in the queue, discard this
2390 message. */
2391 printk(KERN_WARNING PFX "Event queue full, discarding an"
2392 " incoming event\n");
2395 out:
2396 spin_unlock_irqrestore(&(intf->events_lock), flags);
2398 return rv;
2401 static int handle_bmc_rsp(ipmi_smi_t intf,
2402 struct ipmi_smi_msg *msg)
2404 struct ipmi_recv_msg *recv_msg;
2405 int found = 0;
2406 struct ipmi_user *user;
2407 unsigned long flags;
2409 recv_msg = (struct ipmi_recv_msg *) msg->user_data;
2410 if (recv_msg == NULL)
2412 printk(KERN_WARNING"IPMI message received with no owner. This\n"
2413 "could be because of a malformed message, or\n"
2414 "because of a hardware error. Contact your\n"
2415 "hardware vender for assistance\n");
2416 return 0;
2419 /* Make sure the user still exists. */
2420 list_for_each_entry(user, &(intf->users), link) {
2421 if (user == recv_msg->user) {
2422 /* Found it, so we can deliver it */
2423 found = 1;
2424 break;
2428 if ((! found) && recv_msg->user) {
2429 /* The user for the message went away, so give up. */
2430 spin_lock_irqsave(&intf->counter_lock, flags);
2431 intf->unhandled_local_responses++;
2432 spin_unlock_irqrestore(&intf->counter_lock, flags);
2433 ipmi_free_recv_msg(recv_msg);
2434 } else {
2435 struct ipmi_system_interface_addr *smi_addr;
2437 spin_lock_irqsave(&intf->counter_lock, flags);
2438 intf->handled_local_responses++;
2439 spin_unlock_irqrestore(&intf->counter_lock, flags);
2440 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
2441 recv_msg->msgid = msg->msgid;
2442 smi_addr = ((struct ipmi_system_interface_addr *)
2443 &(recv_msg->addr));
2444 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2445 smi_addr->channel = IPMI_BMC_CHANNEL;
2446 smi_addr->lun = msg->rsp[0] & 3;
2447 recv_msg->msg.netfn = msg->rsp[0] >> 2;
2448 recv_msg->msg.cmd = msg->rsp[1];
2449 memcpy(recv_msg->msg_data,
2450 &(msg->rsp[2]),
2451 msg->rsp_size - 2);
2452 recv_msg->msg.data = recv_msg->msg_data;
2453 recv_msg->msg.data_len = msg->rsp_size - 2;
2454 deliver_response(recv_msg);
2457 return 0;
2460 /* Handle a new message. Return 1 if the message should be requeued,
2461 0 if the message should be freed, or -1 if the message should not
2462 be freed or requeued. */
2463 static int handle_new_recv_msg(ipmi_smi_t intf,
2464 struct ipmi_smi_msg *msg)
2466 int requeue;
2467 int chan;
2469 #ifdef DEBUG_MSGING
2470 int m;
2471 printk("Recv:");
2472 for (m=0; m<msg->rsp_size; m++)
2473 printk(" %2.2x", msg->rsp[m]);
2474 printk("\n");
2475 #endif
2476 if (msg->rsp_size < 2) {
2477 /* Message is too small to be correct. */
2478 printk(KERN_WARNING PFX "BMC returned to small a message"
2479 " for netfn %x cmd %x, got %d bytes\n",
2480 (msg->data[0] >> 2) | 1, msg->data[1], msg->rsp_size);
2482 /* Generate an error response for the message. */
2483 msg->rsp[0] = msg->data[0] | (1 << 2);
2484 msg->rsp[1] = msg->data[1];
2485 msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
2486 msg->rsp_size = 3;
2487 } else if (((msg->rsp[0] >> 2) != ((msg->data[0] >> 2) | 1))/* Netfn */
2488 || (msg->rsp[1] != msg->data[1])) /* Command */
2490 /* The response is not even marginally correct. */
2491 printk(KERN_WARNING PFX "BMC returned incorrect response,"
2492 " expected netfn %x cmd %x, got netfn %x cmd %x\n",
2493 (msg->data[0] >> 2) | 1, msg->data[1],
2494 msg->rsp[0] >> 2, msg->rsp[1]);
2496 /* Generate an error response for the message. */
2497 msg->rsp[0] = msg->data[0] | (1 << 2);
2498 msg->rsp[1] = msg->data[1];
2499 msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
2500 msg->rsp_size = 3;
2503 if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
2504 && (msg->rsp[1] == IPMI_SEND_MSG_CMD)
2505 && (msg->user_data != NULL))
2507 /* It's a response to a response we sent. For this we
2508 deliver a send message response to the user. */
2509 struct ipmi_recv_msg *recv_msg = msg->user_data;
2511 requeue = 0;
2512 if (msg->rsp_size < 2)
2513 /* Message is too small to be correct. */
2514 goto out;
2516 chan = msg->data[2] & 0x0f;
2517 if (chan >= IPMI_MAX_CHANNELS)
2518 /* Invalid channel number */
2519 goto out;
2521 if (recv_msg) {
2522 recv_msg->recv_type = IPMI_RESPONSE_RESPONSE_TYPE;
2523 recv_msg->msg.data = recv_msg->msg_data;
2524 recv_msg->msg.data_len = 1;
2525 recv_msg->msg_data[0] = msg->rsp[2];
2526 deliver_response(recv_msg);
2528 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
2529 && (msg->rsp[1] == IPMI_GET_MSG_CMD))
2531 /* It's from the receive queue. */
2532 chan = msg->rsp[3] & 0xf;
2533 if (chan >= IPMI_MAX_CHANNELS) {
2534 /* Invalid channel number */
2535 requeue = 0;
2536 goto out;
2539 switch (intf->channels[chan].medium) {
2540 case IPMI_CHANNEL_MEDIUM_IPMB:
2541 if (msg->rsp[4] & 0x04) {
2542 /* It's a response, so find the
2543 requesting message and send it up. */
2544 requeue = handle_ipmb_get_msg_rsp(intf, msg);
2545 } else {
2546 /* It's a command to the SMS from some other
2547 entity. Handle that. */
2548 requeue = handle_ipmb_get_msg_cmd(intf, msg);
2550 break;
2552 case IPMI_CHANNEL_MEDIUM_8023LAN:
2553 case IPMI_CHANNEL_MEDIUM_ASYNC:
2554 if (msg->rsp[6] & 0x04) {
2555 /* It's a response, so find the
2556 requesting message and send it up. */
2557 requeue = handle_lan_get_msg_rsp(intf, msg);
2558 } else {
2559 /* It's a command to the SMS from some other
2560 entity. Handle that. */
2561 requeue = handle_lan_get_msg_cmd(intf, msg);
2563 break;
2565 default:
2566 /* We don't handle the channel type, so just
2567 * free the message. */
2568 requeue = 0;
2571 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
2572 && (msg->rsp[1] == IPMI_READ_EVENT_MSG_BUFFER_CMD))
2574 /* It's an asyncronous event. */
2575 requeue = handle_read_event_rsp(intf, msg);
2576 } else {
2577 /* It's a response from the local BMC. */
2578 requeue = handle_bmc_rsp(intf, msg);
2581 out:
2582 return requeue;
2585 /* Handle a new message from the lower layer. */
2586 void ipmi_smi_msg_received(ipmi_smi_t intf,
2587 struct ipmi_smi_msg *msg)
2589 unsigned long flags;
2590 int rv;
2593 /* Lock the user lock so the user can't go away while we are
2594 working on it. */
2595 read_lock(&(intf->users_lock));
2597 if ((msg->data_size >= 2)
2598 && (msg->data[0] == (IPMI_NETFN_APP_REQUEST << 2))
2599 && (msg->data[1] == IPMI_SEND_MSG_CMD)
2600 && (msg->user_data == NULL)) {
2601 /* This is the local response to a command send, start
2602 the timer for these. The user_data will not be
2603 NULL if this is a response send, and we will let
2604 response sends just go through. */
2606 /* Check for errors, if we get certain errors (ones
2607 that mean basically we can try again later), we
2608 ignore them and start the timer. Otherwise we
2609 report the error immediately. */
2610 if ((msg->rsp_size >= 3) && (msg->rsp[2] != 0)
2611 && (msg->rsp[2] != IPMI_NODE_BUSY_ERR)
2612 && (msg->rsp[2] != IPMI_LOST_ARBITRATION_ERR))
2614 int chan = msg->rsp[3] & 0xf;
2616 /* Got an error sending the message, handle it. */
2617 spin_lock_irqsave(&intf->counter_lock, flags);
2618 if (chan >= IPMI_MAX_CHANNELS)
2619 ; /* This shouldn't happen */
2620 else if ((intf->channels[chan].medium
2621 == IPMI_CHANNEL_MEDIUM_8023LAN)
2622 || (intf->channels[chan].medium
2623 == IPMI_CHANNEL_MEDIUM_ASYNC))
2624 intf->sent_lan_command_errs++;
2625 else
2626 intf->sent_ipmb_command_errs++;
2627 spin_unlock_irqrestore(&intf->counter_lock, flags);
2628 intf_err_seq(intf, msg->msgid, msg->rsp[2]);
2629 } else {
2630 /* The message was sent, start the timer. */
2631 intf_start_seq_timer(intf, msg->msgid);
2634 ipmi_free_smi_msg(msg);
2635 goto out_unlock;
2638 /* To preserve message order, if the list is not empty, we
2639 tack this message onto the end of the list. */
2640 spin_lock_irqsave(&(intf->waiting_msgs_lock), flags);
2641 if (!list_empty(&(intf->waiting_msgs))) {
2642 list_add_tail(&(msg->link), &(intf->waiting_msgs));
2643 spin_unlock(&(intf->waiting_msgs_lock));
2644 goto out_unlock;
2646 spin_unlock_irqrestore(&(intf->waiting_msgs_lock), flags);
2648 rv = handle_new_recv_msg(intf, msg);
2649 if (rv > 0) {
2650 /* Could not handle the message now, just add it to a
2651 list to handle later. */
2652 spin_lock(&(intf->waiting_msgs_lock));
2653 list_add_tail(&(msg->link), &(intf->waiting_msgs));
2654 spin_unlock(&(intf->waiting_msgs_lock));
2655 } else if (rv == 0) {
2656 ipmi_free_smi_msg(msg);
2659 out_unlock:
2660 read_unlock(&(intf->users_lock));
2663 void ipmi_smi_watchdog_pretimeout(ipmi_smi_t intf)
2665 ipmi_user_t user;
2667 read_lock(&(intf->users_lock));
2668 list_for_each_entry(user, &(intf->users), link) {
2669 if (! user->handler->ipmi_watchdog_pretimeout)
2670 continue;
2672 user->handler->ipmi_watchdog_pretimeout(user->handler_data);
2674 read_unlock(&(intf->users_lock));
2677 static void
2678 handle_msg_timeout(struct ipmi_recv_msg *msg)
2680 msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
2681 msg->msg_data[0] = IPMI_TIMEOUT_COMPLETION_CODE;
2682 msg->msg.netfn |= 1; /* Convert to a response. */
2683 msg->msg.data_len = 1;
2684 msg->msg.data = msg->msg_data;
2685 deliver_response(msg);
2688 static struct ipmi_smi_msg *
2689 smi_from_recv_msg(ipmi_smi_t intf, struct ipmi_recv_msg *recv_msg,
2690 unsigned char seq, long seqid)
2692 struct ipmi_smi_msg *smi_msg = ipmi_alloc_smi_msg();
2693 if (!smi_msg)
2694 /* If we can't allocate the message, then just return, we
2695 get 4 retries, so this should be ok. */
2696 return NULL;
2698 memcpy(smi_msg->data, recv_msg->msg.data, recv_msg->msg.data_len);
2699 smi_msg->data_size = recv_msg->msg.data_len;
2700 smi_msg->msgid = STORE_SEQ_IN_MSGID(seq, seqid);
2702 #ifdef DEBUG_MSGING
2704 int m;
2705 printk("Resend: ");
2706 for (m=0; m<smi_msg->data_size; m++)
2707 printk(" %2.2x", smi_msg->data[m]);
2708 printk("\n");
2710 #endif
2711 return smi_msg;
2714 static void
2715 ipmi_timeout_handler(long timeout_period)
2717 ipmi_smi_t intf;
2718 struct list_head timeouts;
2719 struct ipmi_recv_msg *msg, *msg2;
2720 struct ipmi_smi_msg *smi_msg, *smi_msg2;
2721 unsigned long flags;
2722 int i, j;
2724 INIT_LIST_HEAD(&timeouts);
2726 spin_lock(&interfaces_lock);
2727 for (i=0; i<MAX_IPMI_INTERFACES; i++) {
2728 intf = ipmi_interfaces[i];
2729 if (intf == NULL)
2730 continue;
2732 read_lock(&(intf->users_lock));
2734 /* See if any waiting messages need to be processed. */
2735 spin_lock_irqsave(&(intf->waiting_msgs_lock), flags);
2736 list_for_each_entry_safe(smi_msg, smi_msg2, &(intf->waiting_msgs), link) {
2737 if (! handle_new_recv_msg(intf, smi_msg)) {
2738 list_del(&smi_msg->link);
2739 ipmi_free_smi_msg(smi_msg);
2740 } else {
2741 /* To preserve message order, quit if we
2742 can't handle a message. */
2743 break;
2746 spin_unlock_irqrestore(&(intf->waiting_msgs_lock), flags);
2748 /* Go through the seq table and find any messages that
2749 have timed out, putting them in the timeouts
2750 list. */
2751 spin_lock_irqsave(&(intf->seq_lock), flags);
2752 for (j=0; j<IPMI_IPMB_NUM_SEQ; j++) {
2753 struct seq_table *ent = &(intf->seq_table[j]);
2754 if (!ent->inuse)
2755 continue;
2757 ent->timeout -= timeout_period;
2758 if (ent->timeout > 0)
2759 continue;
2761 if (ent->retries_left == 0) {
2762 /* The message has used all its retries. */
2763 ent->inuse = 0;
2764 msg = ent->recv_msg;
2765 list_add_tail(&(msg->link), &timeouts);
2766 spin_lock(&intf->counter_lock);
2767 if (ent->broadcast)
2768 intf->timed_out_ipmb_broadcasts++;
2769 else if (ent->recv_msg->addr.addr_type
2770 == IPMI_LAN_ADDR_TYPE)
2771 intf->timed_out_lan_commands++;
2772 else
2773 intf->timed_out_ipmb_commands++;
2774 spin_unlock(&intf->counter_lock);
2775 } else {
2776 struct ipmi_smi_msg *smi_msg;
2777 /* More retries, send again. */
2779 /* Start with the max timer, set to normal
2780 timer after the message is sent. */
2781 ent->timeout = MAX_MSG_TIMEOUT;
2782 ent->retries_left--;
2783 spin_lock(&intf->counter_lock);
2784 if (ent->recv_msg->addr.addr_type
2785 == IPMI_LAN_ADDR_TYPE)
2786 intf->retransmitted_lan_commands++;
2787 else
2788 intf->retransmitted_ipmb_commands++;
2789 spin_unlock(&intf->counter_lock);
2790 smi_msg = smi_from_recv_msg(intf,
2791 ent->recv_msg, j, ent->seqid);
2792 if(!smi_msg)
2793 continue;
2795 spin_unlock_irqrestore(&(intf->seq_lock),flags);
2796 /* Send the new message. We send with a zero
2797 * priority. It timed out, I doubt time is
2798 * that critical now, and high priority
2799 * messages are really only for messages to the
2800 * local MC, which don't get resent. */
2801 intf->handlers->sender(intf->send_info,
2802 smi_msg, 0);
2803 spin_lock_irqsave(&(intf->seq_lock), flags);
2806 spin_unlock_irqrestore(&(intf->seq_lock), flags);
2808 list_for_each_entry_safe(msg, msg2, &timeouts, link) {
2809 handle_msg_timeout(msg);
2812 read_unlock(&(intf->users_lock));
2814 spin_unlock(&interfaces_lock);
2817 static void ipmi_request_event(void)
2819 ipmi_smi_t intf;
2820 int i;
2822 spin_lock(&interfaces_lock);
2823 for (i=0; i<MAX_IPMI_INTERFACES; i++) {
2824 intf = ipmi_interfaces[i];
2825 if (intf == NULL)
2826 continue;
2828 intf->handlers->request_events(intf->send_info);
2830 spin_unlock(&interfaces_lock);
2833 static struct timer_list ipmi_timer;
2835 /* Call every ~100 ms. */
2836 #define IPMI_TIMEOUT_TIME 100
2838 /* How many jiffies does it take to get to the timeout time. */
2839 #define IPMI_TIMEOUT_JIFFIES ((IPMI_TIMEOUT_TIME * HZ) / 1000)
2841 /* Request events from the queue every second (this is the number of
2842 IPMI_TIMEOUT_TIMES between event requests). Hopefully, in the
2843 future, IPMI will add a way to know immediately if an event is in
2844 the queue and this silliness can go away. */
2845 #define IPMI_REQUEST_EV_TIME (1000 / (IPMI_TIMEOUT_TIME))
2847 static atomic_t stop_operation;
2848 static unsigned int ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
2850 static void ipmi_timeout(unsigned long data)
2852 if (atomic_read(&stop_operation))
2853 return;
2855 ticks_to_req_ev--;
2856 if (ticks_to_req_ev == 0) {
2857 ipmi_request_event();
2858 ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
2861 ipmi_timeout_handler(IPMI_TIMEOUT_TIME);
2863 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
2867 static atomic_t smi_msg_inuse_count = ATOMIC_INIT(0);
2868 static atomic_t recv_msg_inuse_count = ATOMIC_INIT(0);
2870 /* FIXME - convert these to slabs. */
2871 static void free_smi_msg(struct ipmi_smi_msg *msg)
2873 atomic_dec(&smi_msg_inuse_count);
2874 kfree(msg);
2877 struct ipmi_smi_msg *ipmi_alloc_smi_msg(void)
2879 struct ipmi_smi_msg *rv;
2880 rv = kmalloc(sizeof(struct ipmi_smi_msg), GFP_ATOMIC);
2881 if (rv) {
2882 rv->done = free_smi_msg;
2883 rv->user_data = NULL;
2884 atomic_inc(&smi_msg_inuse_count);
2886 return rv;
2889 static void free_recv_msg(struct ipmi_recv_msg *msg)
2891 atomic_dec(&recv_msg_inuse_count);
2892 kfree(msg);
2895 struct ipmi_recv_msg *ipmi_alloc_recv_msg(void)
2897 struct ipmi_recv_msg *rv;
2899 rv = kmalloc(sizeof(struct ipmi_recv_msg), GFP_ATOMIC);
2900 if (rv) {
2901 rv->done = free_recv_msg;
2902 atomic_inc(&recv_msg_inuse_count);
2904 return rv;
2907 #ifdef CONFIG_IPMI_PANIC_EVENT
2909 static void dummy_smi_done_handler(struct ipmi_smi_msg *msg)
2913 static void dummy_recv_done_handler(struct ipmi_recv_msg *msg)
2917 #ifdef CONFIG_IPMI_PANIC_STRING
2918 static void event_receiver_fetcher(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
2920 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
2921 && (msg->msg.netfn == IPMI_NETFN_SENSOR_EVENT_RESPONSE)
2922 && (msg->msg.cmd == IPMI_GET_EVENT_RECEIVER_CMD)
2923 && (msg->msg.data[0] == IPMI_CC_NO_ERROR))
2925 /* A get event receiver command, save it. */
2926 intf->event_receiver = msg->msg.data[1];
2927 intf->event_receiver_lun = msg->msg.data[2] & 0x3;
2931 static void device_id_fetcher(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
2933 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
2934 && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
2935 && (msg->msg.cmd == IPMI_GET_DEVICE_ID_CMD)
2936 && (msg->msg.data[0] == IPMI_CC_NO_ERROR))
2938 /* A get device id command, save if we are an event
2939 receiver or generator. */
2940 intf->local_sel_device = (msg->msg.data[6] >> 2) & 1;
2941 intf->local_event_generator = (msg->msg.data[6] >> 5) & 1;
2944 #endif
2946 static void send_panic_events(char *str)
2948 struct kernel_ipmi_msg msg;
2949 ipmi_smi_t intf;
2950 unsigned char data[16];
2951 int i;
2952 struct ipmi_system_interface_addr *si;
2953 struct ipmi_addr addr;
2954 struct ipmi_smi_msg smi_msg;
2955 struct ipmi_recv_msg recv_msg;
2957 si = (struct ipmi_system_interface_addr *) &addr;
2958 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2959 si->channel = IPMI_BMC_CHANNEL;
2960 si->lun = 0;
2962 /* Fill in an event telling that we have failed. */
2963 msg.netfn = 0x04; /* Sensor or Event. */
2964 msg.cmd = 2; /* Platform event command. */
2965 msg.data = data;
2966 msg.data_len = 8;
2967 data[0] = 0x21; /* Kernel generator ID, IPMI table 5-4 */
2968 data[1] = 0x03; /* This is for IPMI 1.0. */
2969 data[2] = 0x20; /* OS Critical Stop, IPMI table 36-3 */
2970 data[4] = 0x6f; /* Sensor specific, IPMI table 36-1 */
2971 data[5] = 0xa1; /* Runtime stop OEM bytes 2 & 3. */
2973 /* Put a few breadcrumbs in. Hopefully later we can add more things
2974 to make the panic events more useful. */
2975 if (str) {
2976 data[3] = str[0];
2977 data[6] = str[1];
2978 data[7] = str[2];
2981 smi_msg.done = dummy_smi_done_handler;
2982 recv_msg.done = dummy_recv_done_handler;
2984 /* For every registered interface, send the event. */
2985 for (i=0; i<MAX_IPMI_INTERFACES; i++) {
2986 intf = ipmi_interfaces[i];
2987 if (intf == NULL)
2988 continue;
2990 /* Send the event announcing the panic. */
2991 intf->handlers->set_run_to_completion(intf->send_info, 1);
2992 i_ipmi_request(NULL,
2993 intf,
2994 &addr,
2996 &msg,
2997 intf,
2998 &smi_msg,
2999 &recv_msg,
3001 intf->channels[0].address,
3002 intf->channels[0].lun,
3003 0, 1); /* Don't retry, and don't wait. */
3006 #ifdef CONFIG_IPMI_PANIC_STRING
3007 /* On every interface, dump a bunch of OEM event holding the
3008 string. */
3009 if (!str)
3010 return;
3012 for (i=0; i<MAX_IPMI_INTERFACES; i++) {
3013 char *p = str;
3014 struct ipmi_ipmb_addr *ipmb;
3015 int j;
3017 intf = ipmi_interfaces[i];
3018 if (intf == NULL)
3019 continue;
3021 /* First job here is to figure out where to send the
3022 OEM events. There's no way in IPMI to send OEM
3023 events using an event send command, so we have to
3024 find the SEL to put them in and stick them in
3025 there. */
3027 /* Get capabilities from the get device id. */
3028 intf->local_sel_device = 0;
3029 intf->local_event_generator = 0;
3030 intf->event_receiver = 0;
3032 /* Request the device info from the local MC. */
3033 msg.netfn = IPMI_NETFN_APP_REQUEST;
3034 msg.cmd = IPMI_GET_DEVICE_ID_CMD;
3035 msg.data = NULL;
3036 msg.data_len = 0;
3037 intf->null_user_handler = device_id_fetcher;
3038 i_ipmi_request(NULL,
3039 intf,
3040 &addr,
3042 &msg,
3043 intf,
3044 &smi_msg,
3045 &recv_msg,
3047 intf->channels[0].address,
3048 intf->channels[0].lun,
3049 0, 1); /* Don't retry, and don't wait. */
3051 if (intf->local_event_generator) {
3052 /* Request the event receiver from the local MC. */
3053 msg.netfn = IPMI_NETFN_SENSOR_EVENT_REQUEST;
3054 msg.cmd = IPMI_GET_EVENT_RECEIVER_CMD;
3055 msg.data = NULL;
3056 msg.data_len = 0;
3057 intf->null_user_handler = event_receiver_fetcher;
3058 i_ipmi_request(NULL,
3059 intf,
3060 &addr,
3062 &msg,
3063 intf,
3064 &smi_msg,
3065 &recv_msg,
3067 intf->channels[0].address,
3068 intf->channels[0].lun,
3069 0, 1); /* no retry, and no wait. */
3071 intf->null_user_handler = NULL;
3073 /* Validate the event receiver. The low bit must not
3074 be 1 (it must be a valid IPMB address), it cannot
3075 be zero, and it must not be my address. */
3076 if (((intf->event_receiver & 1) == 0)
3077 && (intf->event_receiver != 0)
3078 && (intf->event_receiver != intf->channels[0].address))
3080 /* The event receiver is valid, send an IPMB
3081 message. */
3082 ipmb = (struct ipmi_ipmb_addr *) &addr;
3083 ipmb->addr_type = IPMI_IPMB_ADDR_TYPE;
3084 ipmb->channel = 0; /* FIXME - is this right? */
3085 ipmb->lun = intf->event_receiver_lun;
3086 ipmb->slave_addr = intf->event_receiver;
3087 } else if (intf->local_sel_device) {
3088 /* The event receiver was not valid (or was
3089 me), but I am an SEL device, just dump it
3090 in my SEL. */
3091 si = (struct ipmi_system_interface_addr *) &addr;
3092 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3093 si->channel = IPMI_BMC_CHANNEL;
3094 si->lun = 0;
3095 } else
3096 continue; /* No where to send the event. */
3099 msg.netfn = IPMI_NETFN_STORAGE_REQUEST; /* Storage. */
3100 msg.cmd = IPMI_ADD_SEL_ENTRY_CMD;
3101 msg.data = data;
3102 msg.data_len = 16;
3104 j = 0;
3105 while (*p) {
3106 int size = strlen(p);
3108 if (size > 11)
3109 size = 11;
3110 data[0] = 0;
3111 data[1] = 0;
3112 data[2] = 0xf0; /* OEM event without timestamp. */
3113 data[3] = intf->channels[0].address;
3114 data[4] = j++; /* sequence # */
3115 /* Always give 11 bytes, so strncpy will fill
3116 it with zeroes for me. */
3117 strncpy(data+5, p, 11);
3118 p += size;
3120 i_ipmi_request(NULL,
3121 intf,
3122 &addr,
3124 &msg,
3125 intf,
3126 &smi_msg,
3127 &recv_msg,
3129 intf->channels[0].address,
3130 intf->channels[0].lun,
3131 0, 1); /* no retry, and no wait. */
3134 #endif /* CONFIG_IPMI_PANIC_STRING */
3136 #endif /* CONFIG_IPMI_PANIC_EVENT */
3138 static int has_paniced = 0;
3140 static int panic_event(struct notifier_block *this,
3141 unsigned long event,
3142 void *ptr)
3144 int i;
3145 ipmi_smi_t intf;
3147 if (has_paniced)
3148 return NOTIFY_DONE;
3149 has_paniced = 1;
3151 /* For every registered interface, set it to run to completion. */
3152 for (i=0; i<MAX_IPMI_INTERFACES; i++) {
3153 intf = ipmi_interfaces[i];
3154 if (intf == NULL)
3155 continue;
3157 intf->handlers->set_run_to_completion(intf->send_info, 1);
3160 #ifdef CONFIG_IPMI_PANIC_EVENT
3161 send_panic_events(ptr);
3162 #endif
3164 return NOTIFY_DONE;
3167 static struct notifier_block panic_block = {
3168 .notifier_call = panic_event,
3169 .next = NULL,
3170 .priority = 200 /* priority: INT_MAX >= x >= 0 */
3173 static int ipmi_init_msghandler(void)
3175 int i;
3177 if (initialized)
3178 return 0;
3180 printk(KERN_INFO "ipmi message handler version "
3181 IPMI_DRIVER_VERSION "\n");
3183 for (i=0; i<MAX_IPMI_INTERFACES; i++) {
3184 ipmi_interfaces[i] = NULL;
3187 #ifdef CONFIG_PROC_FS
3188 proc_ipmi_root = proc_mkdir("ipmi", NULL);
3189 if (!proc_ipmi_root) {
3190 printk(KERN_ERR PFX "Unable to create IPMI proc dir");
3191 return -ENOMEM;
3194 proc_ipmi_root->owner = THIS_MODULE;
3195 #endif /* CONFIG_PROC_FS */
3197 init_timer(&ipmi_timer);
3198 ipmi_timer.data = 0;
3199 ipmi_timer.function = ipmi_timeout;
3200 ipmi_timer.expires = jiffies + IPMI_TIMEOUT_JIFFIES;
3201 add_timer(&ipmi_timer);
3203 notifier_chain_register(&panic_notifier_list, &panic_block);
3205 initialized = 1;
3207 return 0;
3210 static __init int ipmi_init_msghandler_mod(void)
3212 ipmi_init_msghandler();
3213 return 0;
3216 static __exit void cleanup_ipmi(void)
3218 int count;
3220 if (!initialized)
3221 return;
3223 notifier_chain_unregister(&panic_notifier_list, &panic_block);
3225 /* This can't be called if any interfaces exist, so no worry about
3226 shutting down the interfaces. */
3228 /* Tell the timer to stop, then wait for it to stop. This avoids
3229 problems with race conditions removing the timer here. */
3230 atomic_inc(&stop_operation);
3231 del_timer_sync(&ipmi_timer);
3233 #ifdef CONFIG_PROC_FS
3234 remove_proc_entry(proc_ipmi_root->name, &proc_root);
3235 #endif /* CONFIG_PROC_FS */
3237 initialized = 0;
3239 /* Check for buffer leaks. */
3240 count = atomic_read(&smi_msg_inuse_count);
3241 if (count != 0)
3242 printk(KERN_WARNING PFX "SMI message count %d at exit\n",
3243 count);
3244 count = atomic_read(&recv_msg_inuse_count);
3245 if (count != 0)
3246 printk(KERN_WARNING PFX "recv message count %d at exit\n",
3247 count);
3249 module_exit(cleanup_ipmi);
3251 module_init(ipmi_init_msghandler_mod);
3252 MODULE_LICENSE("GPL");
3253 MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
3254 MODULE_DESCRIPTION("Incoming and outgoing message routing for an IPMI interface.");
3255 MODULE_VERSION(IPMI_DRIVER_VERSION);
3257 EXPORT_SYMBOL(ipmi_create_user);
3258 EXPORT_SYMBOL(ipmi_destroy_user);
3259 EXPORT_SYMBOL(ipmi_get_version);
3260 EXPORT_SYMBOL(ipmi_request_settime);
3261 EXPORT_SYMBOL(ipmi_request_supply_msgs);
3262 EXPORT_SYMBOL(ipmi_register_smi);
3263 EXPORT_SYMBOL(ipmi_unregister_smi);
3264 EXPORT_SYMBOL(ipmi_register_for_cmd);
3265 EXPORT_SYMBOL(ipmi_unregister_for_cmd);
3266 EXPORT_SYMBOL(ipmi_smi_msg_received);
3267 EXPORT_SYMBOL(ipmi_smi_watchdog_pretimeout);
3268 EXPORT_SYMBOL(ipmi_alloc_smi_msg);
3269 EXPORT_SYMBOL(ipmi_addr_length);
3270 EXPORT_SYMBOL(ipmi_validate_addr);
3271 EXPORT_SYMBOL(ipmi_set_gets_events);
3272 EXPORT_SYMBOL(ipmi_smi_watcher_register);
3273 EXPORT_SYMBOL(ipmi_smi_watcher_unregister);
3274 EXPORT_SYMBOL(ipmi_set_my_address);
3275 EXPORT_SYMBOL(ipmi_get_my_address);
3276 EXPORT_SYMBOL(ipmi_set_my_LUN);
3277 EXPORT_SYMBOL(ipmi_get_my_LUN);
3278 EXPORT_SYMBOL(ipmi_smi_add_proc_entry);
3279 EXPORT_SYMBOL(proc_ipmi_root);
3280 EXPORT_SYMBOL(ipmi_user_set_run_to_completion);