4 * Incoming and outgoing message routing for an IPMI interface.
6 * Author: MontaVista Software, Inc.
7 * Corey Minyard <minyard@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 IPMI_MSGHANDLER_VERSION "v32"
51 struct ipmi_recv_msg *ipmi_alloc_recv_msg(void);
52 static int ipmi_init_msghandler(void);
54 static int initialized = 0;
56 static struct proc_dir_entry *proc_ipmi_root = NULL;
58 #define MAX_EVENTS_IN_QUEUE 25
60 /* Don't let a message sit in a queue forever, always time it with at lest
61 the max message timer. This is in milliseconds. */
62 #define MAX_MSG_TIMEOUT 60000
66 struct list_head link;
68 /* The upper layer that handles receive messages. */
69 struct ipmi_user_hndl *handler;
72 /* The interface this user is bound to. */
75 /* Does this interface receive IPMI events? */
81 struct list_head link;
90 unsigned int inuse : 1;
91 unsigned int broadcast : 1;
93 unsigned long timeout;
94 unsigned long orig_timeout;
95 unsigned int retries_left;
97 /* To verify on an incoming send message response that this is
98 the message that the response is for, we keep a sequence id
99 and increment it every time we send a message. */
102 /* This is held so we can properly respond to the message on a
103 timeout, and it is used to hold the temporary data for
104 retransmission, too. */
105 struct ipmi_recv_msg *recv_msg;
108 /* Store the information in a msgid (long) to allow us to find a
109 sequence table entry from the msgid. */
110 #define STORE_SEQ_IN_MSGID(seq, seqid) (((seq&0xff)<<26) | (seqid&0x3ffffff))
112 #define GET_SEQ_FROM_MSGID(msgid, seq, seqid) \
114 seq = ((msgid >> 26) & 0x3f); \
115 seqid = (msgid & 0x3fffff); \
118 #define NEXT_SEQID(seqid) (((seqid) + 1) & 0x3fffff)
122 unsigned char medium;
123 unsigned char protocol;
126 struct ipmi_proc_entry
129 struct ipmi_proc_entry *next;
132 #define IPMI_IPMB_NUM_SEQ 64
133 #define IPMI_MAX_CHANNELS 8
136 /* What interface number are we? */
139 /* The list of upper layers that are using me. We read-lock
140 this when delivering messages to the upper layer to keep
141 the user from going away while we are processing the
142 message. This means that you cannot add or delete a user
143 from the receive callback. */
145 struct list_head users;
147 /* Used for wake ups at startup. */
148 wait_queue_head_t waitq;
150 /* The IPMI version of the BMC on the other end. */
151 unsigned char version_major;
152 unsigned char version_minor;
154 /* This is the lower-layer's sender routine. */
155 struct ipmi_smi_handlers *handlers;
158 /* A list of proc entries for this interface. This does not
159 need a lock, only one thread creates it and only one thread
161 struct ipmi_proc_entry *proc_entries;
163 /* A table of sequence numbers for this interface. We use the
164 sequence numbers for IPMB messages that go out of the
165 interface to match them up with their responses. A routine
166 is called periodically to time the items in this list. */
168 struct seq_table seq_table[IPMI_IPMB_NUM_SEQ];
171 /* Messages that were delayed for some reason (out of memory,
172 for instance), will go in here to be processed later in a
173 periodic timer interrupt. */
174 spinlock_t waiting_msgs_lock;
175 struct list_head waiting_msgs;
177 /* The list of command receivers that are registered for commands
178 on this interface. */
179 rwlock_t cmd_rcvr_lock;
180 struct list_head cmd_rcvrs;
182 /* Events that were queues because no one was there to receive
184 spinlock_t events_lock; /* For dealing with event stuff. */
185 struct list_head waiting_events;
186 unsigned int waiting_events_count; /* How many events in queue? */
188 /* This will be non-null if someone registers to receive all
189 IPMI commands (this is for interface emulation). There
190 may not be any things in the cmd_rcvrs list above when
191 this is registered. */
192 ipmi_user_t all_cmd_rcvr;
194 /* My slave address. This is initialized to IPMI_BMC_SLAVE_ADDR,
195 but may be changed by the user. */
196 unsigned char my_address;
198 /* My LUN. This should generally stay the SMS LUN, but just in
200 unsigned char my_lun;
202 /* The event receiver for my BMC, only really used at panic
203 shutdown as a place to store this. */
204 unsigned char event_receiver;
205 unsigned char event_receiver_lun;
206 unsigned char local_sel_device;
207 unsigned char local_event_generator;
209 /* A cheap hack, if this is non-null and a message to an
210 interface comes in with a NULL user, call this routine with
211 it. Note that the message will still be freed by the
212 caller. This only works on the system interface. */
213 void (*null_user_handler)(ipmi_smi_t intf, struct ipmi_smi_msg *msg);
215 /* When we are scanning the channels for an SMI, this will
216 tell which channel we are scanning. */
219 /* Channel information */
220 struct ipmi_channel channels[IPMI_MAX_CHANNELS];
223 struct proc_dir_entry *proc_dir;
224 char proc_dir_name[10];
226 spinlock_t counter_lock; /* For making counters atomic. */
228 /* Commands we got that were invalid. */
229 unsigned int sent_invalid_commands;
231 /* Commands we sent to the MC. */
232 unsigned int sent_local_commands;
233 /* Responses from the MC that were delivered to a user. */
234 unsigned int handled_local_responses;
235 /* Responses from the MC that were not delivered to a user. */
236 unsigned int unhandled_local_responses;
238 /* Commands we sent out to the IPMB bus. */
239 unsigned int sent_ipmb_commands;
240 /* Commands sent on the IPMB that had errors on the SEND CMD */
241 unsigned int sent_ipmb_command_errs;
242 /* Each retransmit increments this count. */
243 unsigned int retransmitted_ipmb_commands;
244 /* When a message times out (runs out of retransmits) this is
246 unsigned int timed_out_ipmb_commands;
248 /* This is like above, but for broadcasts. Broadcasts are
249 *not* included in the above count (they are expected to
251 unsigned int timed_out_ipmb_broadcasts;
253 /* Responses I have sent to the IPMB bus. */
254 unsigned int sent_ipmb_responses;
256 /* The response was delivered to the user. */
257 unsigned int handled_ipmb_responses;
258 /* The response had invalid data in it. */
259 unsigned int invalid_ipmb_responses;
260 /* The response didn't have anyone waiting for it. */
261 unsigned int unhandled_ipmb_responses;
263 /* Commands we sent out to the IPMB bus. */
264 unsigned int sent_lan_commands;
265 /* Commands sent on the IPMB that had errors on the SEND CMD */
266 unsigned int sent_lan_command_errs;
267 /* Each retransmit increments this count. */
268 unsigned int retransmitted_lan_commands;
269 /* When a message times out (runs out of retransmits) this is
271 unsigned int timed_out_lan_commands;
273 /* Responses I have sent to the IPMB bus. */
274 unsigned int sent_lan_responses;
276 /* The response was delivered to the user. */
277 unsigned int handled_lan_responses;
278 /* The response had invalid data in it. */
279 unsigned int invalid_lan_responses;
280 /* The response didn't have anyone waiting for it. */
281 unsigned int unhandled_lan_responses;
283 /* The command was delivered to the user. */
284 unsigned int handled_commands;
285 /* The command had invalid data in it. */
286 unsigned int invalid_commands;
287 /* The command didn't have anyone waiting for it. */
288 unsigned int unhandled_commands;
290 /* Invalid data in an event. */
291 unsigned int invalid_events;
292 /* Events that were received with the proper format. */
297 ipmi_register_all_cmd_rcvr(ipmi_user_t user)
302 write_lock_irqsave(&(user->intf->users_lock), flags);
303 write_lock(&(user->intf->cmd_rcvr_lock));
304 if ((user->intf->all_cmd_rcvr == NULL)
305 && (list_empty(&(user->intf->cmd_rcvrs))))
307 user->intf->all_cmd_rcvr = user;
310 write_unlock(&(user->intf->cmd_rcvr_lock));
311 write_unlock_irqrestore(&(user->intf->users_lock), flags);
316 ipmi_unregister_all_cmd_rcvr(ipmi_user_t user)
321 write_lock_irqsave(&(user->intf->users_lock), flags);
322 write_lock(&(user->intf->cmd_rcvr_lock));
323 if (user->intf->all_cmd_rcvr == user)
325 user->intf->all_cmd_rcvr = NULL;
328 write_unlock(&(user->intf->cmd_rcvr_lock));
329 write_unlock_irqrestore(&(user->intf->users_lock), flags);
334 #define MAX_IPMI_INTERFACES 4
335 static ipmi_smi_t ipmi_interfaces[MAX_IPMI_INTERFACES];
337 /* Used to keep interfaces from going away while operations are
338 operating on interfaces. Grab read if you are not modifying the
339 interfaces, write if you are. */
340 static DECLARE_RWSEM(interfaces_sem);
342 /* Directly protects the ipmi_interfaces data structure. This is
343 claimed in the timer interrupt. */
344 static spinlock_t interfaces_lock = SPIN_LOCK_UNLOCKED;
346 /* List of watchers that want to know when smi's are added and
348 static struct list_head smi_watchers = LIST_HEAD_INIT(smi_watchers);
349 static DECLARE_RWSEM(smi_watchers_sem);
351 int ipmi_smi_watcher_register(struct ipmi_smi_watcher *watcher)
355 down_read(&interfaces_sem);
356 down_write(&smi_watchers_sem);
357 list_add(&(watcher->link), &smi_watchers);
358 for (i=0; i<MAX_IPMI_INTERFACES; i++) {
359 if (ipmi_interfaces[i] != NULL) {
363 up_write(&smi_watchers_sem);
364 up_read(&interfaces_sem);
368 int ipmi_smi_watcher_unregister(struct ipmi_smi_watcher *watcher)
370 down_write(&smi_watchers_sem);
371 list_del(&(watcher->link));
372 up_write(&smi_watchers_sem);
377 call_smi_watchers(int i)
379 struct ipmi_smi_watcher *w;
381 down_read(&smi_watchers_sem);
382 list_for_each_entry(w, &smi_watchers, link) {
383 if (try_module_get(w->owner)) {
385 module_put(w->owner);
388 up_read(&smi_watchers_sem);
392 ipmi_addr_equal(struct ipmi_addr *addr1, struct ipmi_addr *addr2)
394 if (addr1->addr_type != addr2->addr_type)
397 if (addr1->channel != addr2->channel)
400 if (addr1->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
401 struct ipmi_system_interface_addr *smi_addr1
402 = (struct ipmi_system_interface_addr *) addr1;
403 struct ipmi_system_interface_addr *smi_addr2
404 = (struct ipmi_system_interface_addr *) addr2;
405 return (smi_addr1->lun == smi_addr2->lun);
408 if ((addr1->addr_type == IPMI_IPMB_ADDR_TYPE)
409 || (addr1->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE))
411 struct ipmi_ipmb_addr *ipmb_addr1
412 = (struct ipmi_ipmb_addr *) addr1;
413 struct ipmi_ipmb_addr *ipmb_addr2
414 = (struct ipmi_ipmb_addr *) addr2;
416 return ((ipmb_addr1->slave_addr == ipmb_addr2->slave_addr)
417 && (ipmb_addr1->lun == ipmb_addr2->lun));
420 if (addr1->addr_type == IPMI_LAN_ADDR_TYPE) {
421 struct ipmi_lan_addr *lan_addr1
422 = (struct ipmi_lan_addr *) addr1;
423 struct ipmi_lan_addr *lan_addr2
424 = (struct ipmi_lan_addr *) addr2;
426 return ((lan_addr1->remote_SWID == lan_addr2->remote_SWID)
427 && (lan_addr1->local_SWID == lan_addr2->local_SWID)
428 && (lan_addr1->session_handle
429 == lan_addr2->session_handle)
430 && (lan_addr1->lun == lan_addr2->lun));
436 int ipmi_validate_addr(struct ipmi_addr *addr, int len)
438 if (len < sizeof(struct ipmi_system_interface_addr)) {
442 if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
443 if (addr->channel != IPMI_BMC_CHANNEL)
448 if ((addr->channel == IPMI_BMC_CHANNEL)
449 || (addr->channel >= IPMI_NUM_CHANNELS)
450 || (addr->channel < 0))
453 if ((addr->addr_type == IPMI_IPMB_ADDR_TYPE)
454 || (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE))
456 if (len < sizeof(struct ipmi_ipmb_addr)) {
462 if (addr->addr_type == IPMI_LAN_ADDR_TYPE) {
463 if (len < sizeof(struct ipmi_lan_addr)) {
472 unsigned int ipmi_addr_length(int addr_type)
474 if (addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
475 return sizeof(struct ipmi_system_interface_addr);
477 if ((addr_type == IPMI_IPMB_ADDR_TYPE)
478 || (addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE))
480 return sizeof(struct ipmi_ipmb_addr);
486 static void deliver_response(struct ipmi_recv_msg *msg)
488 msg->user->handler->ipmi_recv_hndl(msg, msg->user->handler_data);
491 /* Find the next sequence number not being used and add the given
492 message with the given timeout to the sequence table. This must be
493 called with the interface's seq_lock held. */
494 static int intf_next_seq(ipmi_smi_t intf,
495 struct ipmi_recv_msg *recv_msg,
496 unsigned long timeout,
505 for (i=intf->curr_seq;
506 (i+1)%IPMI_IPMB_NUM_SEQ != intf->curr_seq;
507 i=(i+1)%IPMI_IPMB_NUM_SEQ)
509 if (! intf->seq_table[i].inuse)
513 if (! intf->seq_table[i].inuse) {
514 intf->seq_table[i].recv_msg = recv_msg;
516 /* Start with the maximum timeout, when the send response
517 comes in we will start the real timer. */
518 intf->seq_table[i].timeout = MAX_MSG_TIMEOUT;
519 intf->seq_table[i].orig_timeout = timeout;
520 intf->seq_table[i].retries_left = retries;
521 intf->seq_table[i].broadcast = broadcast;
522 intf->seq_table[i].inuse = 1;
523 intf->seq_table[i].seqid = NEXT_SEQID(intf->seq_table[i].seqid);
525 *seqid = intf->seq_table[i].seqid;
526 intf->curr_seq = (i+1)%IPMI_IPMB_NUM_SEQ;
534 /* Return the receive message for the given sequence number and
535 release the sequence number so it can be reused. Some other data
536 is passed in to be sure the message matches up correctly (to help
537 guard against message coming in after their timeout and the
538 sequence number being reused). */
539 static int intf_find_seq(ipmi_smi_t intf,
544 struct ipmi_addr *addr,
545 struct ipmi_recv_msg **recv_msg)
550 if (seq >= IPMI_IPMB_NUM_SEQ)
553 spin_lock_irqsave(&(intf->seq_lock), flags);
554 if (intf->seq_table[seq].inuse) {
555 struct ipmi_recv_msg *msg = intf->seq_table[seq].recv_msg;
557 if ((msg->addr.channel == channel)
558 && (msg->msg.cmd == cmd)
559 && (msg->msg.netfn == netfn)
560 && (ipmi_addr_equal(addr, &(msg->addr))))
563 intf->seq_table[seq].inuse = 0;
567 spin_unlock_irqrestore(&(intf->seq_lock), flags);
573 /* Start the timer for a specific sequence table entry. */
574 static int intf_start_seq_timer(ipmi_smi_t intf,
583 GET_SEQ_FROM_MSGID(msgid, seq, seqid);
585 spin_lock_irqsave(&(intf->seq_lock), flags);
586 /* We do this verification because the user can be deleted
587 while a message is outstanding. */
588 if ((intf->seq_table[seq].inuse)
589 && (intf->seq_table[seq].seqid == seqid))
591 struct seq_table *ent = &(intf->seq_table[seq]);
592 ent->timeout = ent->orig_timeout;
595 spin_unlock_irqrestore(&(intf->seq_lock), flags);
600 /* Got an error for the send message for a specific sequence number. */
601 static int intf_err_seq(ipmi_smi_t intf,
609 struct ipmi_recv_msg *msg = NULL;
612 GET_SEQ_FROM_MSGID(msgid, seq, seqid);
614 spin_lock_irqsave(&(intf->seq_lock), flags);
615 /* We do this verification because the user can be deleted
616 while a message is outstanding. */
617 if ((intf->seq_table[seq].inuse)
618 && (intf->seq_table[seq].seqid == seqid))
620 struct seq_table *ent = &(intf->seq_table[seq]);
626 spin_unlock_irqrestore(&(intf->seq_lock), flags);
629 msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
630 msg->msg_data[0] = err;
631 msg->msg.netfn |= 1; /* Convert to a response. */
632 msg->msg.data_len = 1;
633 msg->msg.data = msg->msg_data;
634 deliver_response(msg);
641 int ipmi_create_user(unsigned int if_num,
642 struct ipmi_user_hndl *handler,
647 ipmi_user_t new_user;
650 /* There is no module usecount here, because it's not
651 required. Since this can only be used by and called from
652 other modules, they will implicitly use this module, and
653 thus this can't be removed unless the other modules are
659 /* Make sure the driver is actually initialized, this handles
660 problems with initialization order. */
662 rv = ipmi_init_msghandler();
666 /* The init code doesn't return an error if it was turned
667 off, but it won't initialize. Check that. */
672 new_user = kmalloc(sizeof(*new_user), GFP_KERNEL);
676 down_read(&interfaces_sem);
677 if ((if_num > MAX_IPMI_INTERFACES) || ipmi_interfaces[if_num] == NULL)
683 new_user->handler = handler;
684 new_user->handler_data = handler_data;
685 new_user->intf = ipmi_interfaces[if_num];
686 new_user->gets_events = 0;
688 if (!try_module_get(new_user->intf->handlers->owner)) {
693 write_lock_irqsave(&new_user->intf->users_lock, flags);
694 list_add_tail(&new_user->link, &new_user->intf->users);
695 write_unlock_irqrestore(&new_user->intf->users_lock, flags);
704 up_read(&interfaces_sem);
708 static int ipmi_destroy_user_nolock(ipmi_user_t user)
712 struct cmd_rcvr *rcvr, *rcvr2;
716 /* Find the user and delete them from the list. */
717 list_for_each_entry(t_user, &(user->intf->users), link) {
718 if (t_user == user) {
719 list_del(&t_user->link);
729 /* Remove the user from the interfaces sequence table. */
730 spin_lock_irqsave(&(user->intf->seq_lock), flags);
731 for (i=0; i<IPMI_IPMB_NUM_SEQ; i++) {
732 if (user->intf->seq_table[i].inuse
733 && (user->intf->seq_table[i].recv_msg->user == user))
735 user->intf->seq_table[i].inuse = 0;
738 spin_unlock_irqrestore(&(user->intf->seq_lock), flags);
740 /* Remove the user from the command receiver's table. */
741 write_lock_irqsave(&(user->intf->cmd_rcvr_lock), flags);
742 list_for_each_entry_safe(rcvr, rcvr2, &(user->intf->cmd_rcvrs), link) {
743 if (rcvr->user == user) {
744 list_del(&rcvr->link);
748 write_unlock_irqrestore(&(user->intf->cmd_rcvr_lock), flags);
757 int ipmi_destroy_user(ipmi_user_t user)
760 ipmi_smi_t intf = user->intf;
763 down_read(&interfaces_sem);
764 write_lock_irqsave(&intf->users_lock, flags);
765 rv = ipmi_destroy_user_nolock(user);
767 module_put(intf->handlers->owner);
769 write_unlock_irqrestore(&intf->users_lock, flags);
770 up_read(&interfaces_sem);
774 void ipmi_get_version(ipmi_user_t user,
775 unsigned char *major,
776 unsigned char *minor)
778 *major = user->intf->version_major;
779 *minor = user->intf->version_minor;
782 void ipmi_set_my_address(ipmi_user_t user,
783 unsigned char address)
785 user->intf->my_address = address;
788 unsigned char ipmi_get_my_address(ipmi_user_t user)
790 return user->intf->my_address;
793 void ipmi_set_my_LUN(ipmi_user_t user,
796 user->intf->my_lun = LUN & 0x3;
799 unsigned char ipmi_get_my_LUN(ipmi_user_t user)
801 return user->intf->my_lun;
804 int ipmi_set_gets_events(ipmi_user_t user, int val)
807 struct ipmi_recv_msg *msg, *msg2;
809 read_lock(&(user->intf->users_lock));
810 spin_lock_irqsave(&(user->intf->events_lock), flags);
811 user->gets_events = val;
814 /* Deliver any queued events. */
815 list_for_each_entry_safe(msg, msg2, &(user->intf->waiting_events), link) {
816 list_del(&msg->link);
818 deliver_response(msg);
822 spin_unlock_irqrestore(&(user->intf->events_lock), flags);
823 read_unlock(&(user->intf->users_lock));
828 int ipmi_register_for_cmd(ipmi_user_t user,
832 struct cmd_rcvr *cmp;
834 struct cmd_rcvr *rcvr;
838 rcvr = kmalloc(sizeof(*rcvr), GFP_KERNEL);
842 read_lock(&(user->intf->users_lock));
843 write_lock_irqsave(&(user->intf->cmd_rcvr_lock), flags);
844 if (user->intf->all_cmd_rcvr != NULL) {
849 /* Make sure the command/netfn is not already registered. */
850 list_for_each_entry(cmp, &(user->intf->cmd_rcvrs), link) {
851 if ((cmp->netfn == netfn) && (cmp->cmd == cmd)) {
861 list_add_tail(&(rcvr->link), &(user->intf->cmd_rcvrs));
864 write_unlock_irqrestore(&(user->intf->cmd_rcvr_lock), flags);
865 read_unlock(&(user->intf->users_lock));
873 int ipmi_unregister_for_cmd(ipmi_user_t user,
878 struct cmd_rcvr *rcvr;
881 read_lock(&(user->intf->users_lock));
882 write_lock_irqsave(&(user->intf->cmd_rcvr_lock), flags);
883 /* Make sure the command/netfn is not already registered. */
884 list_for_each_entry(rcvr, &(user->intf->cmd_rcvrs), link) {
885 if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)) {
887 list_del(&rcvr->link);
892 write_unlock_irqrestore(&(user->intf->cmd_rcvr_lock), flags);
893 read_unlock(&(user->intf->users_lock));
899 ipmb_checksum(unsigned char *data, int size)
901 unsigned char csum = 0;
903 for (; size > 0; size--, data++)
909 static inline void format_ipmb_msg(struct ipmi_smi_msg *smi_msg,
910 struct kernel_ipmi_msg *msg,
911 struct ipmi_ipmb_addr *ipmb_addr,
913 unsigned char ipmb_seq,
915 unsigned char source_address,
916 unsigned char source_lun)
920 /* Format the IPMB header data. */
921 smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
922 smi_msg->data[1] = IPMI_SEND_MSG_CMD;
923 smi_msg->data[2] = ipmb_addr->channel;
925 smi_msg->data[3] = 0;
926 smi_msg->data[i+3] = ipmb_addr->slave_addr;
927 smi_msg->data[i+4] = (msg->netfn << 2) | (ipmb_addr->lun & 0x3);
928 smi_msg->data[i+5] = ipmb_checksum(&(smi_msg->data[i+3]), 2);
929 smi_msg->data[i+6] = source_address;
930 smi_msg->data[i+7] = (ipmb_seq << 2) | source_lun;
931 smi_msg->data[i+8] = msg->cmd;
933 /* Now tack on the data to the message. */
934 if (msg->data_len > 0)
935 memcpy(&(smi_msg->data[i+9]), msg->data,
937 smi_msg->data_size = msg->data_len + 9;
939 /* Now calculate the checksum and tack it on. */
940 smi_msg->data[i+smi_msg->data_size]
941 = ipmb_checksum(&(smi_msg->data[i+6]),
942 smi_msg->data_size-6);
944 /* Add on the checksum size and the offset from the
946 smi_msg->data_size += 1 + i;
948 smi_msg->msgid = msgid;
951 static inline void format_lan_msg(struct ipmi_smi_msg *smi_msg,
952 struct kernel_ipmi_msg *msg,
953 struct ipmi_lan_addr *lan_addr,
955 unsigned char ipmb_seq,
956 unsigned char source_lun)
958 /* Format the IPMB header data. */
959 smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
960 smi_msg->data[1] = IPMI_SEND_MSG_CMD;
961 smi_msg->data[2] = lan_addr->channel;
962 smi_msg->data[3] = lan_addr->session_handle;
963 smi_msg->data[4] = lan_addr->remote_SWID;
964 smi_msg->data[5] = (msg->netfn << 2) | (lan_addr->lun & 0x3);
965 smi_msg->data[6] = ipmb_checksum(&(smi_msg->data[4]), 2);
966 smi_msg->data[7] = lan_addr->local_SWID;
967 smi_msg->data[8] = (ipmb_seq << 2) | source_lun;
968 smi_msg->data[9] = msg->cmd;
970 /* Now tack on the data to the message. */
971 if (msg->data_len > 0)
972 memcpy(&(smi_msg->data[10]), msg->data,
974 smi_msg->data_size = msg->data_len + 10;
976 /* Now calculate the checksum and tack it on. */
977 smi_msg->data[smi_msg->data_size]
978 = ipmb_checksum(&(smi_msg->data[7]),
979 smi_msg->data_size-7);
981 /* Add on the checksum size and the offset from the
983 smi_msg->data_size += 1;
985 smi_msg->msgid = msgid;
988 /* Separate from ipmi_request so that the user does not have to be
989 supplied in certain circumstances (mainly at panic time). If
990 messages are supplied, they will be freed, even if an error
992 static inline int i_ipmi_request(ipmi_user_t user,
994 struct ipmi_addr *addr,
996 struct kernel_ipmi_msg *msg,
999 struct ipmi_recv_msg *supplied_recv,
1001 unsigned char source_address,
1002 unsigned char source_lun,
1004 unsigned int retry_time_ms)
1007 struct ipmi_smi_msg *smi_msg;
1008 struct ipmi_recv_msg *recv_msg;
1009 unsigned long flags;
1012 if (supplied_recv) {
1013 recv_msg = supplied_recv;
1015 recv_msg = ipmi_alloc_recv_msg();
1016 if (recv_msg == NULL) {
1020 recv_msg->user_msg_data = user_msg_data;
1023 smi_msg = (struct ipmi_smi_msg *) supplied_smi;
1025 smi_msg = ipmi_alloc_smi_msg();
1026 if (smi_msg == NULL) {
1027 ipmi_free_recv_msg(recv_msg);
1032 recv_msg->user = user;
1033 recv_msg->msgid = msgid;
1034 /* Store the message to send in the receive message so timeout
1035 responses can get the proper response data. */
1036 recv_msg->msg = *msg;
1038 if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
1039 struct ipmi_system_interface_addr *smi_addr;
1041 if (msg->netfn & 1) {
1042 /* Responses are not allowed to the SMI. */
1047 smi_addr = (struct ipmi_system_interface_addr *) addr;
1048 if (smi_addr->lun > 3) {
1049 spin_lock_irqsave(&intf->counter_lock, flags);
1050 intf->sent_invalid_commands++;
1051 spin_unlock_irqrestore(&intf->counter_lock, flags);
1056 memcpy(&recv_msg->addr, smi_addr, sizeof(*smi_addr));
1058 if ((msg->netfn == IPMI_NETFN_APP_REQUEST)
1059 && ((msg->cmd == IPMI_SEND_MSG_CMD)
1060 || (msg->cmd == IPMI_GET_MSG_CMD)
1061 || (msg->cmd == IPMI_READ_EVENT_MSG_BUFFER_CMD)))
1063 /* We don't let the user do these, since we manage
1064 the sequence numbers. */
1065 spin_lock_irqsave(&intf->counter_lock, flags);
1066 intf->sent_invalid_commands++;
1067 spin_unlock_irqrestore(&intf->counter_lock, flags);
1072 if ((msg->data_len + 2) > IPMI_MAX_MSG_LENGTH) {
1073 spin_lock_irqsave(&intf->counter_lock, flags);
1074 intf->sent_invalid_commands++;
1075 spin_unlock_irqrestore(&intf->counter_lock, flags);
1080 smi_msg->data[0] = (msg->netfn << 2) | (smi_addr->lun & 0x3);
1081 smi_msg->data[1] = msg->cmd;
1082 smi_msg->msgid = msgid;
1083 smi_msg->user_data = recv_msg;
1084 if (msg->data_len > 0)
1085 memcpy(&(smi_msg->data[2]), msg->data, msg->data_len);
1086 smi_msg->data_size = msg->data_len + 2;
1087 spin_lock_irqsave(&intf->counter_lock, flags);
1088 intf->sent_local_commands++;
1089 spin_unlock_irqrestore(&intf->counter_lock, flags);
1090 } else if ((addr->addr_type == IPMI_IPMB_ADDR_TYPE)
1091 || (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE))
1093 struct ipmi_ipmb_addr *ipmb_addr;
1094 unsigned char ipmb_seq;
1098 if (addr->channel > IPMI_NUM_CHANNELS) {
1099 spin_lock_irqsave(&intf->counter_lock, flags);
1100 intf->sent_invalid_commands++;
1101 spin_unlock_irqrestore(&intf->counter_lock, flags);
1106 if (intf->channels[addr->channel].medium
1107 != IPMI_CHANNEL_MEDIUM_IPMB)
1109 spin_lock_irqsave(&intf->counter_lock, flags);
1110 intf->sent_invalid_commands++;
1111 spin_unlock_irqrestore(&intf->counter_lock, flags);
1117 if (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE)
1118 retries = 0; /* Don't retry broadcasts. */
1122 if (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE) {
1123 /* Broadcasts add a zero at the beginning of the
1124 message, but otherwise is the same as an IPMB
1126 addr->addr_type = IPMI_IPMB_ADDR_TYPE;
1131 /* Default to 1 second retries. */
1132 if (retry_time_ms == 0)
1133 retry_time_ms = 1000;
1135 /* 9 for the header and 1 for the checksum, plus
1136 possibly one for the broadcast. */
1137 if ((msg->data_len + 10 + broadcast) > IPMI_MAX_MSG_LENGTH) {
1138 spin_lock_irqsave(&intf->counter_lock, flags);
1139 intf->sent_invalid_commands++;
1140 spin_unlock_irqrestore(&intf->counter_lock, flags);
1145 ipmb_addr = (struct ipmi_ipmb_addr *) addr;
1146 if (ipmb_addr->lun > 3) {
1147 spin_lock_irqsave(&intf->counter_lock, flags);
1148 intf->sent_invalid_commands++;
1149 spin_unlock_irqrestore(&intf->counter_lock, flags);
1154 memcpy(&recv_msg->addr, ipmb_addr, sizeof(*ipmb_addr));
1156 if (recv_msg->msg.netfn & 0x1) {
1157 /* It's a response, so use the user's sequence
1159 spin_lock_irqsave(&intf->counter_lock, flags);
1160 intf->sent_ipmb_responses++;
1161 spin_unlock_irqrestore(&intf->counter_lock, flags);
1162 format_ipmb_msg(smi_msg, msg, ipmb_addr, msgid,
1164 source_address, source_lun);
1166 /* Save the receive message so we can use it
1167 to deliver the response. */
1168 smi_msg->user_data = recv_msg;
1170 /* It's a command, so get a sequence for it. */
1172 spin_lock_irqsave(&(intf->seq_lock), flags);
1174 spin_lock(&intf->counter_lock);
1175 intf->sent_ipmb_commands++;
1176 spin_unlock(&intf->counter_lock);
1178 /* Create a sequence number with a 1 second
1179 timeout and 4 retries. */
1180 rv = intf_next_seq(intf,
1188 /* We have used up all the sequence numbers,
1189 probably, so abort. */
1190 spin_unlock_irqrestore(&(intf->seq_lock),
1195 /* Store the sequence number in the message,
1196 so that when the send message response
1197 comes back we can start the timer. */
1198 format_ipmb_msg(smi_msg, msg, ipmb_addr,
1199 STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
1200 ipmb_seq, broadcast,
1201 source_address, source_lun);
1203 /* Copy the message into the recv message data, so we
1204 can retransmit it later if necessary. */
1205 memcpy(recv_msg->msg_data, smi_msg->data,
1206 smi_msg->data_size);
1207 recv_msg->msg.data = recv_msg->msg_data;
1208 recv_msg->msg.data_len = smi_msg->data_size;
1210 /* We don't unlock until here, because we need
1211 to copy the completed message into the
1212 recv_msg before we release the lock.
1213 Otherwise, race conditions may bite us. I
1214 know that's pretty paranoid, but I prefer
1216 spin_unlock_irqrestore(&(intf->seq_lock), flags);
1218 } else if (addr->addr_type == IPMI_LAN_ADDR_TYPE) {
1219 struct ipmi_lan_addr *lan_addr;
1220 unsigned char ipmb_seq;
1223 if (addr->channel > IPMI_NUM_CHANNELS) {
1224 spin_lock_irqsave(&intf->counter_lock, flags);
1225 intf->sent_invalid_commands++;
1226 spin_unlock_irqrestore(&intf->counter_lock, flags);
1231 if ((intf->channels[addr->channel].medium
1232 != IPMI_CHANNEL_MEDIUM_8023LAN)
1233 && (intf->channels[addr->channel].medium
1234 != IPMI_CHANNEL_MEDIUM_ASYNC))
1236 spin_lock_irqsave(&intf->counter_lock, flags);
1237 intf->sent_invalid_commands++;
1238 spin_unlock_irqrestore(&intf->counter_lock, flags);
1245 /* Default to 1 second retries. */
1246 if (retry_time_ms == 0)
1247 retry_time_ms = 1000;
1249 /* 11 for the header and 1 for the checksum. */
1250 if ((msg->data_len + 12) > IPMI_MAX_MSG_LENGTH) {
1251 spin_lock_irqsave(&intf->counter_lock, flags);
1252 intf->sent_invalid_commands++;
1253 spin_unlock_irqrestore(&intf->counter_lock, flags);
1258 lan_addr = (struct ipmi_lan_addr *) addr;
1259 if (lan_addr->lun > 3) {
1260 spin_lock_irqsave(&intf->counter_lock, flags);
1261 intf->sent_invalid_commands++;
1262 spin_unlock_irqrestore(&intf->counter_lock, flags);
1267 memcpy(&recv_msg->addr, lan_addr, sizeof(*lan_addr));
1269 if (recv_msg->msg.netfn & 0x1) {
1270 /* It's a response, so use the user's sequence
1272 spin_lock_irqsave(&intf->counter_lock, flags);
1273 intf->sent_lan_responses++;
1274 spin_unlock_irqrestore(&intf->counter_lock, flags);
1275 format_lan_msg(smi_msg, msg, lan_addr, msgid,
1278 /* Save the receive message so we can use it
1279 to deliver the response. */
1280 smi_msg->user_data = recv_msg;
1282 /* It's a command, so get a sequence for it. */
1284 spin_lock_irqsave(&(intf->seq_lock), flags);
1286 spin_lock(&intf->counter_lock);
1287 intf->sent_lan_commands++;
1288 spin_unlock(&intf->counter_lock);
1290 /* Create a sequence number with a 1 second
1291 timeout and 4 retries. */
1292 rv = intf_next_seq(intf,
1300 /* We have used up all the sequence numbers,
1301 probably, so abort. */
1302 spin_unlock_irqrestore(&(intf->seq_lock),
1307 /* Store the sequence number in the message,
1308 so that when the send message response
1309 comes back we can start the timer. */
1310 format_lan_msg(smi_msg, msg, lan_addr,
1311 STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
1312 ipmb_seq, source_lun);
1314 /* Copy the message into the recv message data, so we
1315 can retransmit it later if necessary. */
1316 memcpy(recv_msg->msg_data, smi_msg->data,
1317 smi_msg->data_size);
1318 recv_msg->msg.data = recv_msg->msg_data;
1319 recv_msg->msg.data_len = smi_msg->data_size;
1321 /* We don't unlock until here, because we need
1322 to copy the completed message into the
1323 recv_msg before we release the lock.
1324 Otherwise, race conditions may bite us. I
1325 know that's pretty paranoid, but I prefer
1327 spin_unlock_irqrestore(&(intf->seq_lock), flags);
1330 /* Unknown address type. */
1331 spin_lock_irqsave(&intf->counter_lock, flags);
1332 intf->sent_invalid_commands++;
1333 spin_unlock_irqrestore(&intf->counter_lock, flags);
1341 for (m=0; m<smi_msg->data_size; m++)
1342 printk(" %2.2x", smi_msg->data[m]);
1346 intf->handlers->sender(intf->send_info, smi_msg, priority);
1351 ipmi_free_smi_msg(smi_msg);
1352 ipmi_free_recv_msg(recv_msg);
1356 int ipmi_request(ipmi_user_t user,
1357 struct ipmi_addr *addr,
1359 struct kernel_ipmi_msg *msg,
1360 void *user_msg_data,
1363 return i_ipmi_request(user,
1371 user->intf->my_address,
1376 int ipmi_request_settime(ipmi_user_t user,
1377 struct ipmi_addr *addr,
1379 struct kernel_ipmi_msg *msg,
1380 void *user_msg_data,
1383 unsigned int retry_time_ms)
1385 return i_ipmi_request(user,
1393 user->intf->my_address,
1399 int ipmi_request_supply_msgs(ipmi_user_t user,
1400 struct ipmi_addr *addr,
1402 struct kernel_ipmi_msg *msg,
1403 void *user_msg_data,
1405 struct ipmi_recv_msg *supplied_recv,
1408 return i_ipmi_request(user,
1417 user->intf->my_address,
1422 int ipmi_request_with_source(ipmi_user_t user,
1423 struct ipmi_addr *addr,
1425 struct kernel_ipmi_msg *msg,
1426 void *user_msg_data,
1428 unsigned char source_address,
1429 unsigned char source_lun)
1431 return i_ipmi_request(user,
1444 static int ipmb_file_read_proc(char *page, char **start, off_t off,
1445 int count, int *eof, void *data)
1447 char *out = (char *) page;
1448 ipmi_smi_t intf = data;
1450 return sprintf(out, "%x\n", intf->my_address);
1453 static int version_file_read_proc(char *page, char **start, off_t off,
1454 int count, int *eof, void *data)
1456 char *out = (char *) page;
1457 ipmi_smi_t intf = data;
1459 return sprintf(out, "%d.%d\n",
1460 intf->version_major, intf->version_minor);
1463 static int stat_file_read_proc(char *page, char **start, off_t off,
1464 int count, int *eof, void *data)
1466 char *out = (char *) page;
1467 ipmi_smi_t intf = data;
1469 out += sprintf(out, "sent_invalid_commands: %d\n",
1470 intf->sent_invalid_commands);
1471 out += sprintf(out, "sent_local_commands: %d\n",
1472 intf->sent_local_commands);
1473 out += sprintf(out, "handled_local_responses: %d\n",
1474 intf->handled_local_responses);
1475 out += sprintf(out, "unhandled_local_responses: %d\n",
1476 intf->unhandled_local_responses);
1477 out += sprintf(out, "sent_ipmb_commands: %d\n",
1478 intf->sent_ipmb_commands);
1479 out += sprintf(out, "sent_ipmb_command_errs: %d\n",
1480 intf->sent_ipmb_command_errs);
1481 out += sprintf(out, "retransmitted_ipmb_commands: %d\n",
1482 intf->retransmitted_ipmb_commands);
1483 out += sprintf(out, "timed_out_ipmb_commands: %d\n",
1484 intf->timed_out_ipmb_commands);
1485 out += sprintf(out, "timed_out_ipmb_broadcasts: %d\n",
1486 intf->timed_out_ipmb_broadcasts);
1487 out += sprintf(out, "sent_ipmb_responses: %d\n",
1488 intf->sent_ipmb_responses);
1489 out += sprintf(out, "handled_ipmb_responses: %d\n",
1490 intf->handled_ipmb_responses);
1491 out += sprintf(out, "invalid_ipmb_responses: %d\n",
1492 intf->invalid_ipmb_responses);
1493 out += sprintf(out, "unhandled_ipmb_responses: %d\n",
1494 intf->unhandled_ipmb_responses);
1495 out += sprintf(out, "sent_lan_commands: %d\n",
1496 intf->sent_lan_commands);
1497 out += sprintf(out, "sent_lan_command_errs: %d\n",
1498 intf->sent_lan_command_errs);
1499 out += sprintf(out, "retransmitted_lan_commands: %d\n",
1500 intf->retransmitted_lan_commands);
1501 out += sprintf(out, "timed_out_lan_commands: %d\n",
1502 intf->timed_out_lan_commands);
1503 out += sprintf(out, "sent_lan_responses: %d\n",
1504 intf->sent_lan_responses);
1505 out += sprintf(out, "handled_lan_responses: %d\n",
1506 intf->handled_lan_responses);
1507 out += sprintf(out, "invalid_lan_responses: %d\n",
1508 intf->invalid_lan_responses);
1509 out += sprintf(out, "unhandled_lan_responses: %d\n",
1510 intf->unhandled_lan_responses);
1511 out += sprintf(out, "handled_commands: %d\n",
1512 intf->handled_commands);
1513 out += sprintf(out, "invalid_commands: %d\n",
1514 intf->invalid_commands);
1515 out += sprintf(out, "unhandled_commands: %d\n",
1516 intf->unhandled_commands);
1517 out += sprintf(out, "invalid_events: %d\n",
1518 intf->invalid_events);
1519 out += sprintf(out, "events: %d\n",
1522 return (out - ((char *) page));
1525 int ipmi_smi_add_proc_entry(ipmi_smi_t smi, char *name,
1526 read_proc_t *read_proc, write_proc_t *write_proc,
1527 void *data, struct module *owner)
1529 struct proc_dir_entry *file;
1531 struct ipmi_proc_entry *entry;
1533 /* Create a list element. */
1534 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1537 entry->name = kmalloc(strlen(name)+1, GFP_KERNEL);
1542 strcpy(entry->name, name);
1544 file = create_proc_entry(name, 0, smi->proc_dir);
1552 file->read_proc = read_proc;
1553 file->write_proc = write_proc;
1554 file->owner = owner;
1556 /* Stick it on the list. */
1557 entry->next = smi->proc_entries;
1558 smi->proc_entries = entry;
1564 static int add_proc_entries(ipmi_smi_t smi, int num)
1568 sprintf(smi->proc_dir_name, "%d", num);
1569 smi->proc_dir = proc_mkdir(smi->proc_dir_name, proc_ipmi_root);
1573 smi->proc_dir->owner = THIS_MODULE;
1577 rv = ipmi_smi_add_proc_entry(smi, "stats",
1578 stat_file_read_proc, NULL,
1582 rv = ipmi_smi_add_proc_entry(smi, "ipmb",
1583 ipmb_file_read_proc, NULL,
1587 rv = ipmi_smi_add_proc_entry(smi, "version",
1588 version_file_read_proc, NULL,
1594 static void remove_proc_entries(ipmi_smi_t smi)
1596 struct ipmi_proc_entry *entry;
1598 while (smi->proc_entries) {
1599 entry = smi->proc_entries;
1600 smi->proc_entries = entry->next;
1602 remove_proc_entry(entry->name, smi->proc_dir);
1606 remove_proc_entry(smi->proc_dir_name, proc_ipmi_root);
1610 send_channel_info_cmd(ipmi_smi_t intf, int chan)
1612 struct kernel_ipmi_msg msg;
1613 unsigned char data[1];
1614 struct ipmi_system_interface_addr si;
1616 si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
1617 si.channel = IPMI_BMC_CHANNEL;
1620 msg.netfn = IPMI_NETFN_APP_REQUEST;
1621 msg.cmd = IPMI_GET_CHANNEL_INFO_CMD;
1625 return i_ipmi_request(NULL,
1627 (struct ipmi_addr *) &si,
1640 channel_handler(ipmi_smi_t intf, struct ipmi_smi_msg *msg)
1645 if ((msg->rsp[0] == (IPMI_NETFN_APP_RESPONSE << 2))
1646 && (msg->rsp[1] == IPMI_GET_CHANNEL_INFO_CMD))
1648 /* It's the one we want */
1649 if (msg->rsp[2] != 0) {
1650 /* Got an error from the channel, just go on. */
1652 if (msg->rsp[2] == IPMI_INVALID_COMMAND_ERR) {
1653 /* If the MC does not support this
1654 command, that is legal. We just
1655 assume it has one IPMB at channel
1657 intf->channels[0].medium
1658 = IPMI_CHANNEL_MEDIUM_IPMB;
1659 intf->channels[0].protocol
1660 = IPMI_CHANNEL_PROTOCOL_IPMB;
1663 intf->curr_channel = IPMI_MAX_CHANNELS;
1664 wake_up(&intf->waitq);
1669 if (msg->rsp_size < 6) {
1670 /* Message not big enough, just go on. */
1673 chan = intf->curr_channel;
1674 intf->channels[chan].medium = msg->rsp[4] & 0x7f;
1675 intf->channels[chan].protocol = msg->rsp[5] & 0x1f;
1678 intf->curr_channel++;
1679 if (intf->curr_channel >= IPMI_MAX_CHANNELS)
1680 wake_up(&intf->waitq);
1682 rv = send_channel_info_cmd(intf, intf->curr_channel);
1685 /* Got an error somehow, just give up. */
1686 intf->curr_channel = IPMI_MAX_CHANNELS;
1687 wake_up(&intf->waitq);
1689 printk(KERN_WARNING "ipmi_msghandler: Error sending"
1690 "channel information: %d\n",
1698 void ipmi_poll_interface(ipmi_user_t user)
1700 ipmi_smi_t intf = user->intf;
1702 if (intf->handlers->poll)
1703 intf->handlers->poll(intf->send_info);
1706 int ipmi_register_smi(struct ipmi_smi_handlers *handlers,
1708 unsigned char version_major,
1709 unsigned char version_minor,
1714 ipmi_smi_t new_intf;
1715 unsigned long flags;
1718 /* Make sure the driver is actually initialized, this handles
1719 problems with initialization order. */
1721 rv = ipmi_init_msghandler();
1724 /* The init code doesn't return an error if it was turned
1725 off, but it won't initialize. Check that. */
1730 new_intf = kmalloc(sizeof(*new_intf), GFP_KERNEL);
1733 memset(new_intf, 0, sizeof(*new_intf));
1735 new_intf->proc_dir = NULL;
1739 down_write(&interfaces_sem);
1740 for (i=0; i<MAX_IPMI_INTERFACES; i++) {
1741 if (ipmi_interfaces[i] == NULL) {
1742 new_intf->intf_num = i;
1743 new_intf->version_major = version_major;
1744 new_intf->version_minor = version_minor;
1745 new_intf->my_address = IPMI_BMC_SLAVE_ADDR;
1746 new_intf->my_lun = 2; /* the SMS LUN. */
1747 rwlock_init(&(new_intf->users_lock));
1748 INIT_LIST_HEAD(&(new_intf->users));
1749 new_intf->handlers = handlers;
1750 new_intf->send_info = send_info;
1751 spin_lock_init(&(new_intf->seq_lock));
1752 for (j=0; j<IPMI_IPMB_NUM_SEQ; j++) {
1753 new_intf->seq_table[j].inuse = 0;
1754 new_intf->seq_table[j].seqid = 0;
1756 new_intf->curr_seq = 0;
1757 spin_lock_init(&(new_intf->waiting_msgs_lock));
1758 INIT_LIST_HEAD(&(new_intf->waiting_msgs));
1759 spin_lock_init(&(new_intf->events_lock));
1760 INIT_LIST_HEAD(&(new_intf->waiting_events));
1761 new_intf->waiting_events_count = 0;
1762 rwlock_init(&(new_intf->cmd_rcvr_lock));
1763 init_waitqueue_head(&new_intf->waitq);
1764 INIT_LIST_HEAD(&(new_intf->cmd_rcvrs));
1765 new_intf->all_cmd_rcvr = NULL;
1767 spin_lock_init(&(new_intf->counter_lock));
1769 spin_lock_irqsave(&interfaces_lock, flags);
1770 ipmi_interfaces[i] = new_intf;
1771 spin_unlock_irqrestore(&interfaces_lock, flags);
1779 downgrade_write(&interfaces_sem);
1782 rv = add_proc_entries(*intf, i);
1785 if ((version_major > 1)
1786 || ((version_major == 1) && (version_minor >= 5)))
1788 /* Start scanning the channels to see what is
1790 (*intf)->null_user_handler = channel_handler;
1791 (*intf)->curr_channel = 0;
1792 rv = send_channel_info_cmd(*intf, 0);
1796 /* Wait for the channel info to be read. */
1797 up_read(&interfaces_sem);
1798 wait_event((*intf)->waitq,
1799 ((*intf)->curr_channel>=IPMI_MAX_CHANNELS));
1800 down_read(&interfaces_sem);
1802 if (ipmi_interfaces[i] != new_intf)
1803 /* Well, it went away. Just return. */
1806 /* Assume a single IPMB channel at zero. */
1807 (*intf)->channels[0].medium = IPMI_CHANNEL_MEDIUM_IPMB;
1808 (*intf)->channels[0].protocol
1809 = IPMI_CHANNEL_PROTOCOL_IPMB;
1812 /* Call all the watcher interfaces to tell
1813 them that a new interface is available. */
1814 call_smi_watchers(i);
1818 up_read(&interfaces_sem);
1821 if (new_intf->proc_dir)
1822 remove_proc_entries(new_intf);
1829 static void free_recv_msg_list(struct list_head *q)
1831 struct ipmi_recv_msg *msg, *msg2;
1833 list_for_each_entry_safe(msg, msg2, q, link) {
1834 list_del(&msg->link);
1835 ipmi_free_recv_msg(msg);
1839 static void free_cmd_rcvr_list(struct list_head *q)
1841 struct cmd_rcvr *rcvr, *rcvr2;
1843 list_for_each_entry_safe(rcvr, rcvr2, q, link) {
1844 list_del(&rcvr->link);
1849 static void clean_up_interface_data(ipmi_smi_t intf)
1853 free_recv_msg_list(&(intf->waiting_msgs));
1854 free_recv_msg_list(&(intf->waiting_events));
1855 free_cmd_rcvr_list(&(intf->cmd_rcvrs));
1857 for (i=0; i<IPMI_IPMB_NUM_SEQ; i++) {
1858 if ((intf->seq_table[i].inuse)
1859 && (intf->seq_table[i].recv_msg))
1861 ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
1866 int ipmi_unregister_smi(ipmi_smi_t intf)
1870 struct ipmi_smi_watcher *w;
1871 unsigned long flags;
1873 down_write(&interfaces_sem);
1874 if (list_empty(&(intf->users)))
1876 for (i=0; i<MAX_IPMI_INTERFACES; i++) {
1877 if (ipmi_interfaces[i] == intf) {
1878 remove_proc_entries(intf);
1879 spin_lock_irqsave(&interfaces_lock, flags);
1880 ipmi_interfaces[i] = NULL;
1881 clean_up_interface_data(intf);
1882 spin_unlock_irqrestore(&interfaces_lock,flags);
1885 goto out_call_watcher;
1891 up_write(&interfaces_sem);
1896 downgrade_write(&interfaces_sem);
1898 /* Call all the watcher interfaces to tell them that
1899 an interface is gone. */
1900 down_read(&smi_watchers_sem);
1901 list_for_each_entry(w, &smi_watchers, link) {
1904 up_read(&smi_watchers_sem);
1905 up_read(&interfaces_sem);
1909 static int handle_ipmb_get_msg_rsp(ipmi_smi_t intf,
1910 struct ipmi_smi_msg *msg)
1912 struct ipmi_ipmb_addr ipmb_addr;
1913 struct ipmi_recv_msg *recv_msg;
1914 unsigned long flags;
1917 /* This is 11, not 10, because the response must contain a
1918 * completion code. */
1919 if (msg->rsp_size < 11) {
1920 /* Message not big enough, just ignore it. */
1921 spin_lock_irqsave(&intf->counter_lock, flags);
1922 intf->invalid_ipmb_responses++;
1923 spin_unlock_irqrestore(&intf->counter_lock, flags);
1927 if (msg->rsp[2] != 0) {
1928 /* An error getting the response, just ignore it. */
1932 ipmb_addr.addr_type = IPMI_IPMB_ADDR_TYPE;
1933 ipmb_addr.slave_addr = msg->rsp[6];
1934 ipmb_addr.channel = msg->rsp[3] & 0x0f;
1935 ipmb_addr.lun = msg->rsp[7] & 3;
1937 /* It's a response from a remote entity. Look up the sequence
1938 number and handle the response. */
1939 if (intf_find_seq(intf,
1943 (msg->rsp[4] >> 2) & (~1),
1944 (struct ipmi_addr *) &(ipmb_addr),
1947 /* We were unable to find the sequence number,
1948 so just nuke the message. */
1949 spin_lock_irqsave(&intf->counter_lock, flags);
1950 intf->unhandled_ipmb_responses++;
1951 spin_unlock_irqrestore(&intf->counter_lock, flags);
1955 memcpy(recv_msg->msg_data,
1958 /* THe other fields matched, so no need to set them, except
1959 for netfn, which needs to be the response that was
1960 returned, not the request value. */
1961 recv_msg->msg.netfn = msg->rsp[4] >> 2;
1962 recv_msg->msg.data = recv_msg->msg_data;
1963 recv_msg->msg.data_len = msg->rsp_size - 10;
1964 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
1965 spin_lock_irqsave(&intf->counter_lock, flags);
1966 intf->handled_ipmb_responses++;
1967 spin_unlock_irqrestore(&intf->counter_lock, flags);
1968 deliver_response(recv_msg);
1973 static int handle_ipmb_get_msg_cmd(ipmi_smi_t intf,
1974 struct ipmi_smi_msg *msg)
1976 struct cmd_rcvr *rcvr;
1978 unsigned char netfn;
1980 ipmi_user_t user = NULL;
1981 struct ipmi_ipmb_addr *ipmb_addr;
1982 struct ipmi_recv_msg *recv_msg;
1983 unsigned long flags;
1985 if (msg->rsp_size < 10) {
1986 /* Message not big enough, just ignore it. */
1987 spin_lock_irqsave(&intf->counter_lock, flags);
1988 intf->invalid_commands++;
1989 spin_unlock_irqrestore(&intf->counter_lock, flags);
1993 if (msg->rsp[2] != 0) {
1994 /* An error getting the response, just ignore it. */
1998 netfn = msg->rsp[4] >> 2;
2001 read_lock(&(intf->cmd_rcvr_lock));
2003 if (intf->all_cmd_rcvr) {
2004 user = intf->all_cmd_rcvr;
2006 /* Find the command/netfn. */
2007 list_for_each_entry(rcvr, &(intf->cmd_rcvrs), link) {
2008 if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)) {
2014 read_unlock(&(intf->cmd_rcvr_lock));
2017 /* We didn't find a user, deliver an error response. */
2018 spin_lock_irqsave(&intf->counter_lock, flags);
2019 intf->unhandled_commands++;
2020 spin_unlock_irqrestore(&intf->counter_lock, flags);
2022 msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
2023 msg->data[1] = IPMI_SEND_MSG_CMD;
2024 msg->data[2] = msg->rsp[3];
2025 msg->data[3] = msg->rsp[6];
2026 msg->data[4] = ((netfn + 1) << 2) | (msg->rsp[7] & 0x3);
2027 msg->data[5] = ipmb_checksum(&(msg->data[3]), 2);
2028 msg->data[6] = intf->my_address;
2030 msg->data[7] = (msg->rsp[7] & 0xfc) | (msg->rsp[4] & 0x3);
2031 msg->data[8] = msg->rsp[8]; /* cmd */
2032 msg->data[9] = IPMI_INVALID_CMD_COMPLETION_CODE;
2033 msg->data[10] = ipmb_checksum(&(msg->data[6]), 4);
2034 msg->data_size = 11;
2039 printk("Invalid command:");
2040 for (m=0; m<msg->data_size; m++)
2041 printk(" %2.2x", msg->data[m]);
2045 intf->handlers->sender(intf->send_info, msg, 0);
2047 rv = -1; /* We used the message, so return the value that
2048 causes it to not be freed or queued. */
2050 /* Deliver the message to the user. */
2051 spin_lock_irqsave(&intf->counter_lock, flags);
2052 intf->handled_commands++;
2053 spin_unlock_irqrestore(&intf->counter_lock, flags);
2055 recv_msg = ipmi_alloc_recv_msg();
2057 /* We couldn't allocate memory for the
2058 message, so requeue it for handling
2062 /* Extract the source address from the data. */
2063 ipmb_addr = (struct ipmi_ipmb_addr *) &recv_msg->addr;
2064 ipmb_addr->addr_type = IPMI_IPMB_ADDR_TYPE;
2065 ipmb_addr->slave_addr = msg->rsp[6];
2066 ipmb_addr->lun = msg->rsp[7] & 3;
2067 ipmb_addr->channel = msg->rsp[3] & 0xf;
2069 /* Extract the rest of the message information
2070 from the IPMB header.*/
2071 recv_msg->user = user;
2072 recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
2073 recv_msg->msgid = msg->rsp[7] >> 2;
2074 recv_msg->msg.netfn = msg->rsp[4] >> 2;
2075 recv_msg->msg.cmd = msg->rsp[8];
2076 recv_msg->msg.data = recv_msg->msg_data;
2078 /* We chop off 10, not 9 bytes because the checksum
2079 at the end also needs to be removed. */
2080 recv_msg->msg.data_len = msg->rsp_size - 10;
2081 memcpy(recv_msg->msg_data,
2083 msg->rsp_size - 10);
2084 deliver_response(recv_msg);
2091 static int handle_lan_get_msg_rsp(ipmi_smi_t intf,
2092 struct ipmi_smi_msg *msg)
2094 struct ipmi_lan_addr lan_addr;
2095 struct ipmi_recv_msg *recv_msg;
2096 unsigned long flags;
2099 /* This is 13, not 12, because the response must contain a
2100 * completion code. */
2101 if (msg->rsp_size < 13) {
2102 /* Message not big enough, just ignore it. */
2103 spin_lock_irqsave(&intf->counter_lock, flags);
2104 intf->invalid_lan_responses++;
2105 spin_unlock_irqrestore(&intf->counter_lock, flags);
2109 if (msg->rsp[2] != 0) {
2110 /* An error getting the response, just ignore it. */
2114 lan_addr.addr_type = IPMI_LAN_ADDR_TYPE;
2115 lan_addr.session_handle = msg->rsp[4];
2116 lan_addr.remote_SWID = msg->rsp[8];
2117 lan_addr.local_SWID = msg->rsp[5];
2118 lan_addr.channel = msg->rsp[3] & 0x0f;
2119 lan_addr.privilege = msg->rsp[3] >> 4;
2120 lan_addr.lun = msg->rsp[9] & 3;
2122 /* It's a response from a remote entity. Look up the sequence
2123 number and handle the response. */
2124 if (intf_find_seq(intf,
2128 (msg->rsp[6] >> 2) & (~1),
2129 (struct ipmi_addr *) &(lan_addr),
2132 /* We were unable to find the sequence number,
2133 so just nuke the message. */
2134 spin_lock_irqsave(&intf->counter_lock, flags);
2135 intf->unhandled_lan_responses++;
2136 spin_unlock_irqrestore(&intf->counter_lock, flags);
2140 memcpy(recv_msg->msg_data,
2142 msg->rsp_size - 11);
2143 /* The other fields matched, so no need to set them, except
2144 for netfn, which needs to be the response that was
2145 returned, not the request value. */
2146 recv_msg->msg.netfn = msg->rsp[6] >> 2;
2147 recv_msg->msg.data = recv_msg->msg_data;
2148 recv_msg->msg.data_len = msg->rsp_size - 12;
2149 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
2150 spin_lock_irqsave(&intf->counter_lock, flags);
2151 intf->handled_lan_responses++;
2152 spin_unlock_irqrestore(&intf->counter_lock, flags);
2153 deliver_response(recv_msg);
2158 static int handle_lan_get_msg_cmd(ipmi_smi_t intf,
2159 struct ipmi_smi_msg *msg)
2161 struct cmd_rcvr *rcvr;
2163 unsigned char netfn;
2165 ipmi_user_t user = NULL;
2166 struct ipmi_lan_addr *lan_addr;
2167 struct ipmi_recv_msg *recv_msg;
2168 unsigned long flags;
2170 if (msg->rsp_size < 12) {
2171 /* Message not big enough, just ignore it. */
2172 spin_lock_irqsave(&intf->counter_lock, flags);
2173 intf->invalid_commands++;
2174 spin_unlock_irqrestore(&intf->counter_lock, flags);
2178 if (msg->rsp[2] != 0) {
2179 /* An error getting the response, just ignore it. */
2183 netfn = msg->rsp[6] >> 2;
2186 read_lock(&(intf->cmd_rcvr_lock));
2188 if (intf->all_cmd_rcvr) {
2189 user = intf->all_cmd_rcvr;
2191 /* Find the command/netfn. */
2192 list_for_each_entry(rcvr, &(intf->cmd_rcvrs), link) {
2193 if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)) {
2199 read_unlock(&(intf->cmd_rcvr_lock));
2202 /* We didn't find a user, deliver an error response. */
2203 spin_lock_irqsave(&intf->counter_lock, flags);
2204 intf->unhandled_commands++;
2205 spin_unlock_irqrestore(&intf->counter_lock, flags);
2207 rv = 0; /* Don't do anything with these messages, just
2208 allow them to be freed. */
2210 /* Deliver the message to the user. */
2211 spin_lock_irqsave(&intf->counter_lock, flags);
2212 intf->handled_commands++;
2213 spin_unlock_irqrestore(&intf->counter_lock, flags);
2215 recv_msg = ipmi_alloc_recv_msg();
2217 /* We couldn't allocate memory for the
2218 message, so requeue it for handling
2222 /* Extract the source address from the data. */
2223 lan_addr = (struct ipmi_lan_addr *) &recv_msg->addr;
2224 lan_addr->addr_type = IPMI_LAN_ADDR_TYPE;
2225 lan_addr->session_handle = msg->rsp[4];
2226 lan_addr->remote_SWID = msg->rsp[8];
2227 lan_addr->local_SWID = msg->rsp[5];
2228 lan_addr->lun = msg->rsp[9] & 3;
2229 lan_addr->channel = msg->rsp[3] & 0xf;
2230 lan_addr->privilege = msg->rsp[3] >> 4;
2232 /* Extract the rest of the message information
2233 from the IPMB header.*/
2234 recv_msg->user = user;
2235 recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
2236 recv_msg->msgid = msg->rsp[9] >> 2;
2237 recv_msg->msg.netfn = msg->rsp[6] >> 2;
2238 recv_msg->msg.cmd = msg->rsp[10];
2239 recv_msg->msg.data = recv_msg->msg_data;
2241 /* We chop off 12, not 11 bytes because the checksum
2242 at the end also needs to be removed. */
2243 recv_msg->msg.data_len = msg->rsp_size - 12;
2244 memcpy(recv_msg->msg_data,
2246 msg->rsp_size - 12);
2247 deliver_response(recv_msg);
2254 static void copy_event_into_recv_msg(struct ipmi_recv_msg *recv_msg,
2255 struct ipmi_smi_msg *msg)
2257 struct ipmi_system_interface_addr *smi_addr;
2259 recv_msg->msgid = 0;
2260 smi_addr = (struct ipmi_system_interface_addr *) &(recv_msg->addr);
2261 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2262 smi_addr->channel = IPMI_BMC_CHANNEL;
2263 smi_addr->lun = msg->rsp[0] & 3;
2264 recv_msg->recv_type = IPMI_ASYNC_EVENT_RECV_TYPE;
2265 recv_msg->msg.netfn = msg->rsp[0] >> 2;
2266 recv_msg->msg.cmd = msg->rsp[1];
2267 memcpy(recv_msg->msg_data, &(msg->rsp[3]), msg->rsp_size - 3);
2268 recv_msg->msg.data = recv_msg->msg_data;
2269 recv_msg->msg.data_len = msg->rsp_size - 3;
2272 /* This will be called with the intf->users_lock read-locked, so no need
2274 static int handle_read_event_rsp(ipmi_smi_t intf,
2275 struct ipmi_smi_msg *msg)
2277 struct ipmi_recv_msg *recv_msg, *recv_msg2;
2278 struct list_head msgs;
2281 int deliver_count = 0;
2282 unsigned long flags;
2284 if (msg->rsp_size < 19) {
2285 /* Message is too small to be an IPMB event. */
2286 spin_lock_irqsave(&intf->counter_lock, flags);
2287 intf->invalid_events++;
2288 spin_unlock_irqrestore(&intf->counter_lock, flags);
2292 if (msg->rsp[2] != 0) {
2293 /* An error getting the event, just ignore it. */
2297 INIT_LIST_HEAD(&msgs);
2299 spin_lock_irqsave(&(intf->events_lock), flags);
2301 spin_lock(&intf->counter_lock);
2303 spin_unlock(&intf->counter_lock);
2305 /* Allocate and fill in one message for every user that is getting
2307 list_for_each_entry(user, &(intf->users), link) {
2308 if (! user->gets_events)
2311 recv_msg = ipmi_alloc_recv_msg();
2313 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs, link) {
2314 list_del(&recv_msg->link);
2315 ipmi_free_recv_msg(recv_msg);
2317 /* We couldn't allocate memory for the
2318 message, so requeue it for handling
2326 copy_event_into_recv_msg(recv_msg, msg);
2327 recv_msg->user = user;
2328 list_add_tail(&(recv_msg->link), &msgs);
2331 if (deliver_count) {
2332 /* Now deliver all the messages. */
2333 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs, link) {
2334 list_del(&recv_msg->link);
2335 deliver_response(recv_msg);
2337 } else if (intf->waiting_events_count < MAX_EVENTS_IN_QUEUE) {
2338 /* No one to receive the message, put it in queue if there's
2339 not already too many things in the queue. */
2340 recv_msg = ipmi_alloc_recv_msg();
2342 /* We couldn't allocate memory for the
2343 message, so requeue it for handling
2349 copy_event_into_recv_msg(recv_msg, msg);
2350 list_add_tail(&(recv_msg->link), &(intf->waiting_events));
2352 /* There's too many things in the queue, discard this
2354 printk(KERN_WARNING "ipmi: Event queue full, discarding an"
2355 " incoming event\n");
2359 spin_unlock_irqrestore(&(intf->events_lock), flags);
2364 static int handle_bmc_rsp(ipmi_smi_t intf,
2365 struct ipmi_smi_msg *msg)
2367 struct ipmi_recv_msg *recv_msg;
2369 struct ipmi_user *user;
2370 unsigned long flags;
2372 recv_msg = (struct ipmi_recv_msg *) msg->user_data;
2374 /* Make sure the user still exists. */
2375 list_for_each_entry(user, &(intf->users), link) {
2376 if (user == recv_msg->user) {
2377 /* Found it, so we can deliver it */
2384 /* Special handling for NULL users. */
2385 if (!recv_msg->user && intf->null_user_handler)
2386 intf->null_user_handler(intf, msg);
2387 /* The user for the message went away, so give up. */
2388 spin_lock_irqsave(&intf->counter_lock, flags);
2389 intf->unhandled_local_responses++;
2390 spin_unlock_irqrestore(&intf->counter_lock, flags);
2391 ipmi_free_recv_msg(recv_msg);
2393 struct ipmi_system_interface_addr *smi_addr;
2395 spin_lock_irqsave(&intf->counter_lock, flags);
2396 intf->handled_local_responses++;
2397 spin_unlock_irqrestore(&intf->counter_lock, flags);
2398 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
2399 recv_msg->msgid = msg->msgid;
2400 smi_addr = ((struct ipmi_system_interface_addr *)
2402 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2403 smi_addr->channel = IPMI_BMC_CHANNEL;
2404 smi_addr->lun = msg->rsp[0] & 3;
2405 recv_msg->msg.netfn = msg->rsp[0] >> 2;
2406 recv_msg->msg.cmd = msg->rsp[1];
2407 memcpy(recv_msg->msg_data,
2410 recv_msg->msg.data = recv_msg->msg_data;
2411 recv_msg->msg.data_len = msg->rsp_size - 2;
2412 deliver_response(recv_msg);
2418 /* Handle a new message. Return 1 if the message should be requeued,
2419 0 if the message should be freed, or -1 if the message should not
2420 be freed or requeued. */
2421 static int handle_new_recv_msg(ipmi_smi_t intf,
2422 struct ipmi_smi_msg *msg)
2430 for (m=0; m<msg->rsp_size; m++)
2431 printk(" %2.2x", msg->rsp[m]);
2434 if (msg->rsp_size < 2) {
2435 /* Message is too small to be correct. */
2437 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
2438 && (msg->rsp[1] == IPMI_SEND_MSG_CMD)
2439 && (msg->user_data != NULL))
2441 /* It's a response to a response we sent. For this we
2442 deliver a send message response to the user. */
2443 struct ipmi_recv_msg *recv_msg = msg->user_data;
2446 if (msg->rsp_size < 2)
2447 /* Message is too small to be correct. */
2450 chan = msg->data[2] & 0x0f;
2451 if (chan >= IPMI_MAX_CHANNELS)
2452 /* Invalid channel number */
2456 recv_msg->recv_type = IPMI_RESPONSE_RESPONSE_TYPE;
2457 recv_msg->msg.data = recv_msg->msg_data;
2458 recv_msg->msg.data_len = 1;
2459 recv_msg->msg_data[0] = msg->rsp[2];
2460 deliver_response(recv_msg);
2462 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
2463 && (msg->rsp[1] == IPMI_GET_MSG_CMD))
2465 /* It's from the receive queue. */
2466 chan = msg->rsp[3] & 0xf;
2467 if (chan >= IPMI_MAX_CHANNELS) {
2468 /* Invalid channel number */
2473 switch (intf->channels[chan].medium) {
2474 case IPMI_CHANNEL_MEDIUM_IPMB:
2475 if (msg->rsp[4] & 0x04) {
2476 /* It's a response, so find the
2477 requesting message and send it up. */
2478 requeue = handle_ipmb_get_msg_rsp(intf, msg);
2480 /* It's a command to the SMS from some other
2481 entity. Handle that. */
2482 requeue = handle_ipmb_get_msg_cmd(intf, msg);
2486 case IPMI_CHANNEL_MEDIUM_8023LAN:
2487 case IPMI_CHANNEL_MEDIUM_ASYNC:
2488 if (msg->rsp[6] & 0x04) {
2489 /* It's a response, so find the
2490 requesting message and send it up. */
2491 requeue = handle_lan_get_msg_rsp(intf, msg);
2493 /* It's a command to the SMS from some other
2494 entity. Handle that. */
2495 requeue = handle_lan_get_msg_cmd(intf, msg);
2500 /* We don't handle the channel type, so just
2501 * free the message. */
2505 } else if (msg->rsp[1] == IPMI_READ_EVENT_MSG_BUFFER_CMD) {
2506 /* It's an asyncronous event. */
2507 requeue = handle_read_event_rsp(intf, msg);
2509 /* It's a response from the local BMC. */
2510 requeue = handle_bmc_rsp(intf, msg);
2517 /* Handle a new message from the lower layer. */
2518 void ipmi_smi_msg_received(ipmi_smi_t intf,
2519 struct ipmi_smi_msg *msg)
2521 unsigned long flags;
2525 /* Lock the user lock so the user can't go away while we are
2527 read_lock(&(intf->users_lock));
2529 if ((msg->data_size >= 2)
2530 && (msg->data[0] == (IPMI_NETFN_APP_REQUEST << 2))
2531 && (msg->data[1] == IPMI_SEND_MSG_CMD)
2532 && (msg->user_data == NULL)) {
2533 /* This is the local response to a command send, start
2534 the timer for these. The user_data will not be
2535 NULL if this is a response send, and we will let
2536 response sends just go through. */
2538 /* Check for errors, if we get certain errors (ones
2539 that mean basically we can try again later), we
2540 ignore them and start the timer. Otherwise we
2541 report the error immediately. */
2542 if ((msg->rsp_size >= 3) && (msg->rsp[2] != 0)
2543 && (msg->rsp[2] != IPMI_NODE_BUSY_ERR)
2544 && (msg->rsp[2] != IPMI_LOST_ARBITRATION_ERR))
2546 int chan = msg->rsp[3] & 0xf;
2548 /* Got an error sending the message, handle it. */
2549 spin_lock_irqsave(&intf->counter_lock, flags);
2550 if (chan >= IPMI_MAX_CHANNELS)
2551 ; /* This shouldn't happen */
2552 else if ((intf->channels[chan].medium
2553 == IPMI_CHANNEL_MEDIUM_8023LAN)
2554 || (intf->channels[chan].medium
2555 == IPMI_CHANNEL_MEDIUM_ASYNC))
2556 intf->sent_lan_command_errs++;
2558 intf->sent_ipmb_command_errs++;
2559 spin_unlock_irqrestore(&intf->counter_lock, flags);
2560 intf_err_seq(intf, msg->msgid, msg->rsp[2]);
2562 /* The message was sent, start the timer. */
2563 intf_start_seq_timer(intf, msg->msgid);
2566 ipmi_free_smi_msg(msg);
2570 /* To preserve message order, if the list is not empty, we
2571 tack this message onto the end of the list. */
2572 spin_lock_irqsave(&(intf->waiting_msgs_lock), flags);
2573 if (!list_empty(&(intf->waiting_msgs))) {
2574 list_add_tail(&(msg->link), &(intf->waiting_msgs));
2575 spin_unlock(&(intf->waiting_msgs_lock));
2578 spin_unlock_irqrestore(&(intf->waiting_msgs_lock), flags);
2580 rv = handle_new_recv_msg(intf, msg);
2582 /* Could not handle the message now, just add it to a
2583 list to handle later. */
2584 spin_lock(&(intf->waiting_msgs_lock));
2585 list_add_tail(&(msg->link), &(intf->waiting_msgs));
2586 spin_unlock(&(intf->waiting_msgs_lock));
2587 } else if (rv == 0) {
2588 ipmi_free_smi_msg(msg);
2592 read_unlock(&(intf->users_lock));
2595 void ipmi_smi_watchdog_pretimeout(ipmi_smi_t intf)
2599 read_lock(&(intf->users_lock));
2600 list_for_each_entry(user, &(intf->users), link) {
2601 if (! user->handler->ipmi_watchdog_pretimeout)
2604 user->handler->ipmi_watchdog_pretimeout(user->handler_data);
2606 read_unlock(&(intf->users_lock));
2610 handle_msg_timeout(struct ipmi_recv_msg *msg)
2612 msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
2613 msg->msg_data[0] = IPMI_TIMEOUT_COMPLETION_CODE;
2614 msg->msg.netfn |= 1; /* Convert to a response. */
2615 msg->msg.data_len = 1;
2616 msg->msg.data = msg->msg_data;
2617 deliver_response(msg);
2621 send_from_recv_msg(ipmi_smi_t intf, struct ipmi_recv_msg *recv_msg,
2622 struct ipmi_smi_msg *smi_msg,
2623 unsigned char seq, long seqid)
2626 smi_msg = ipmi_alloc_smi_msg();
2628 /* If we can't allocate the message, then just return, we
2629 get 4 retries, so this should be ok. */
2632 memcpy(smi_msg->data, recv_msg->msg.data, recv_msg->msg.data_len);
2633 smi_msg->data_size = recv_msg->msg.data_len;
2634 smi_msg->msgid = STORE_SEQ_IN_MSGID(seq, seqid);
2636 /* Send the new message. We send with a zero priority. It
2637 timed out, I doubt time is that critical now, and high
2638 priority messages are really only for messages to the local
2639 MC, which don't get resent. */
2640 intf->handlers->sender(intf->send_info, smi_msg, 0);
2646 for (m=0; m<smi_msg->data_size; m++)
2647 printk(" %2.2x", smi_msg->data[m]);
2654 ipmi_timeout_handler(long timeout_period)
2657 struct list_head timeouts;
2658 struct ipmi_recv_msg *msg, *msg2;
2659 struct ipmi_smi_msg *smi_msg, *smi_msg2;
2660 unsigned long flags;
2663 INIT_LIST_HEAD(&timeouts);
2665 spin_lock(&interfaces_lock);
2666 for (i=0; i<MAX_IPMI_INTERFACES; i++) {
2667 intf = ipmi_interfaces[i];
2671 read_lock(&(intf->users_lock));
2673 /* See if any waiting messages need to be processed. */
2674 spin_lock_irqsave(&(intf->waiting_msgs_lock), flags);
2675 list_for_each_entry_safe(smi_msg, smi_msg2, &(intf->waiting_msgs), link) {
2676 if (! handle_new_recv_msg(intf, smi_msg)) {
2677 list_del(&smi_msg->link);
2678 ipmi_free_smi_msg(smi_msg);
2680 /* To preserve message order, quit if we
2681 can't handle a message. */
2685 spin_unlock_irqrestore(&(intf->waiting_msgs_lock), flags);
2687 /* Go through the seq table and find any messages that
2688 have timed out, putting them in the timeouts
2690 spin_lock_irqsave(&(intf->seq_lock), flags);
2691 for (j=0; j<IPMI_IPMB_NUM_SEQ; j++) {
2692 struct seq_table *ent = &(intf->seq_table[j]);
2696 ent->timeout -= timeout_period;
2697 if (ent->timeout > 0)
2700 if (ent->retries_left == 0) {
2701 /* The message has used all its retries. */
2703 msg = ent->recv_msg;
2704 list_add_tail(&(msg->link), &timeouts);
2705 spin_lock(&intf->counter_lock);
2707 intf->timed_out_ipmb_broadcasts++;
2708 else if (ent->recv_msg->addr.addr_type
2709 == IPMI_LAN_ADDR_TYPE)
2710 intf->timed_out_lan_commands++;
2712 intf->timed_out_ipmb_commands++;
2713 spin_unlock(&intf->counter_lock);
2715 /* More retries, send again. */
2717 /* Start with the max timer, set to normal
2718 timer after the message is sent. */
2719 ent->timeout = MAX_MSG_TIMEOUT;
2720 ent->retries_left--;
2721 send_from_recv_msg(intf, ent->recv_msg, NULL,
2723 spin_lock(&intf->counter_lock);
2724 if (ent->recv_msg->addr.addr_type
2725 == IPMI_LAN_ADDR_TYPE)
2726 intf->retransmitted_lan_commands++;
2728 intf->retransmitted_ipmb_commands++;
2729 spin_unlock(&intf->counter_lock);
2732 spin_unlock_irqrestore(&(intf->seq_lock), flags);
2734 list_for_each_entry_safe(msg, msg2, &timeouts, link) {
2735 handle_msg_timeout(msg);
2738 read_unlock(&(intf->users_lock));
2740 spin_unlock(&interfaces_lock);
2743 static void ipmi_request_event(void)
2748 spin_lock(&interfaces_lock);
2749 for (i=0; i<MAX_IPMI_INTERFACES; i++) {
2750 intf = ipmi_interfaces[i];
2754 intf->handlers->request_events(intf->send_info);
2756 spin_unlock(&interfaces_lock);
2759 static struct timer_list ipmi_timer;
2761 /* Call every ~100 ms. */
2762 #define IPMI_TIMEOUT_TIME 100
2764 /* How many jiffies does it take to get to the timeout time. */
2765 #define IPMI_TIMEOUT_JIFFIES ((IPMI_TIMEOUT_TIME * HZ) / 1000)
2767 /* Request events from the queue every second (this is the number of
2768 IPMI_TIMEOUT_TIMES between event requests). Hopefully, in the
2769 future, IPMI will add a way to know immediately if an event is in
2770 the queue and this silliness can go away. */
2771 #define IPMI_REQUEST_EV_TIME (1000 / (IPMI_TIMEOUT_TIME))
2773 static volatile int stop_operation = 0;
2774 static volatile int timer_stopped = 0;
2775 static unsigned int ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
2777 static void ipmi_timeout(unsigned long data)
2779 if (stop_operation) {
2785 if (ticks_to_req_ev == 0) {
2786 ipmi_request_event();
2787 ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
2790 ipmi_timeout_handler(IPMI_TIMEOUT_TIME);
2792 ipmi_timer.expires += IPMI_TIMEOUT_JIFFIES;
2793 add_timer(&ipmi_timer);
2797 static atomic_t smi_msg_inuse_count = ATOMIC_INIT(0);
2798 static atomic_t recv_msg_inuse_count = ATOMIC_INIT(0);
2800 /* FIXME - convert these to slabs. */
2801 static void free_smi_msg(struct ipmi_smi_msg *msg)
2803 atomic_dec(&smi_msg_inuse_count);
2807 struct ipmi_smi_msg *ipmi_alloc_smi_msg(void)
2809 struct ipmi_smi_msg *rv;
2810 rv = kmalloc(sizeof(struct ipmi_smi_msg), GFP_ATOMIC);
2812 rv->done = free_smi_msg;
2813 rv->user_data = NULL;
2814 atomic_inc(&smi_msg_inuse_count);
2819 static void free_recv_msg(struct ipmi_recv_msg *msg)
2821 atomic_dec(&recv_msg_inuse_count);
2825 struct ipmi_recv_msg *ipmi_alloc_recv_msg(void)
2827 struct ipmi_recv_msg *rv;
2829 rv = kmalloc(sizeof(struct ipmi_recv_msg), GFP_ATOMIC);
2831 rv->done = free_recv_msg;
2832 atomic_inc(&recv_msg_inuse_count);
2837 #ifdef CONFIG_IPMI_PANIC_EVENT
2839 static void dummy_smi_done_handler(struct ipmi_smi_msg *msg)
2843 static void dummy_recv_done_handler(struct ipmi_recv_msg *msg)
2847 #ifdef CONFIG_IPMI_PANIC_STRING
2848 static void event_receiver_fetcher(ipmi_smi_t intf, struct ipmi_smi_msg *msg)
2850 if ((msg->rsp[0] == (IPMI_NETFN_SENSOR_EVENT_RESPONSE << 2))
2851 && (msg->rsp[1] == IPMI_GET_EVENT_RECEIVER_CMD)
2852 && (msg->rsp[2] == IPMI_CC_NO_ERROR))
2854 /* A get event receiver command, save it. */
2855 intf->event_receiver = msg->rsp[3];
2856 intf->event_receiver_lun = msg->rsp[4] & 0x3;
2860 static void device_id_fetcher(ipmi_smi_t intf, struct ipmi_smi_msg *msg)
2862 if ((msg->rsp[0] == (IPMI_NETFN_APP_RESPONSE << 2))
2863 && (msg->rsp[1] == IPMI_GET_DEVICE_ID_CMD)
2864 && (msg->rsp[2] == IPMI_CC_NO_ERROR))
2866 /* A get device id command, save if we are an event
2867 receiver or generator. */
2868 intf->local_sel_device = (msg->rsp[8] >> 2) & 1;
2869 intf->local_event_generator = (msg->rsp[8] >> 5) & 1;
2874 static void send_panic_events(char *str)
2876 struct kernel_ipmi_msg msg;
2878 unsigned char data[16];
2880 struct ipmi_system_interface_addr *si;
2881 struct ipmi_addr addr;
2882 struct ipmi_smi_msg smi_msg;
2883 struct ipmi_recv_msg recv_msg;
2885 si = (struct ipmi_system_interface_addr *) &addr;
2886 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2887 si->channel = IPMI_BMC_CHANNEL;
2890 /* Fill in an event telling that we have failed. */
2891 msg.netfn = 0x04; /* Sensor or Event. */
2892 msg.cmd = 2; /* Platform event command. */
2895 data[0] = 0x21; /* Kernel generator ID, IPMI table 5-4 */
2896 data[1] = 0x03; /* This is for IPMI 1.0. */
2897 data[2] = 0x20; /* OS Critical Stop, IPMI table 36-3 */
2898 data[4] = 0x6f; /* Sensor specific, IPMI table 36-1 */
2899 data[5] = 0xa1; /* Runtime stop OEM bytes 2 & 3. */
2901 /* Put a few breadcrumbs in. Hopefully later we can add more things
2902 to make the panic events more useful. */
2909 smi_msg.done = dummy_smi_done_handler;
2910 recv_msg.done = dummy_recv_done_handler;
2912 /* For every registered interface, send the event. */
2913 for (i=0; i<MAX_IPMI_INTERFACES; i++) {
2914 intf = ipmi_interfaces[i];
2918 /* Send the event announcing the panic. */
2919 intf->handlers->set_run_to_completion(intf->send_info, 1);
2920 i_ipmi_request(NULL,
2931 0, 1); /* Don't retry, and don't wait. */
2934 #ifdef CONFIG_IPMI_PANIC_STRING
2935 /* On every interface, dump a bunch of OEM event holding the
2940 for (i=0; i<MAX_IPMI_INTERFACES; i++) {
2942 struct ipmi_ipmb_addr *ipmb;
2945 intf = ipmi_interfaces[i];
2949 /* First job here is to figure out where to send the
2950 OEM events. There's no way in IPMI to send OEM
2951 events using an event send command, so we have to
2952 find the SEL to put them in and stick them in
2955 /* Get capabilities from the get device id. */
2956 intf->local_sel_device = 0;
2957 intf->local_event_generator = 0;
2958 intf->event_receiver = 0;
2960 /* Request the device info from the local MC. */
2961 msg.netfn = IPMI_NETFN_APP_REQUEST;
2962 msg.cmd = IPMI_GET_DEVICE_ID_CMD;
2965 intf->null_user_handler = device_id_fetcher;
2966 i_ipmi_request(NULL,
2977 0, 1); /* Don't retry, and don't wait. */
2979 if (intf->local_event_generator) {
2980 /* Request the event receiver from the local MC. */
2981 msg.netfn = IPMI_NETFN_SENSOR_EVENT_REQUEST;
2982 msg.cmd = IPMI_GET_EVENT_RECEIVER_CMD;
2985 intf->null_user_handler = event_receiver_fetcher;
2986 i_ipmi_request(NULL,
2997 0, 1); /* no retry, and no wait. */
2999 intf->null_user_handler = NULL;
3001 /* Validate the event receiver. The low bit must not
3002 be 1 (it must be a valid IPMB address), it cannot
3003 be zero, and it must not be my address. */
3004 if (((intf->event_receiver & 1) == 0)
3005 && (intf->event_receiver != 0)
3006 && (intf->event_receiver != intf->my_address))
3008 /* The event receiver is valid, send an IPMB
3010 ipmb = (struct ipmi_ipmb_addr *) &addr;
3011 ipmb->addr_type = IPMI_IPMB_ADDR_TYPE;
3012 ipmb->channel = 0; /* FIXME - is this right? */
3013 ipmb->lun = intf->event_receiver_lun;
3014 ipmb->slave_addr = intf->event_receiver;
3015 } else if (intf->local_sel_device) {
3016 /* The event receiver was not valid (or was
3017 me), but I am an SEL device, just dump it
3019 si = (struct ipmi_system_interface_addr *) &addr;
3020 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3021 si->channel = IPMI_BMC_CHANNEL;
3024 continue; /* No where to send the event. */
3027 msg.netfn = IPMI_NETFN_STORAGE_REQUEST; /* Storage. */
3028 msg.cmd = IPMI_ADD_SEL_ENTRY_CMD;
3034 int size = strlen(p);
3040 data[2] = 0xf0; /* OEM event without timestamp. */
3041 data[3] = intf->my_address;
3042 data[4] = j++; /* sequence # */
3043 /* Always give 11 bytes, so strncpy will fill
3044 it with zeroes for me. */
3045 strncpy(data+5, p, 11);
3048 i_ipmi_request(NULL,
3059 0, 1); /* no retry, and no wait. */
3062 #endif /* CONFIG_IPMI_PANIC_STRING */
3064 #endif /* CONFIG_IPMI_PANIC_EVENT */
3066 static int has_paniced = 0;
3068 static int panic_event(struct notifier_block *this,
3069 unsigned long event,
3079 /* For every registered interface, set it to run to completion. */
3080 for (i=0; i<MAX_IPMI_INTERFACES; i++) {
3081 intf = ipmi_interfaces[i];
3085 intf->handlers->set_run_to_completion(intf->send_info, 1);
3088 #ifdef CONFIG_IPMI_PANIC_EVENT
3089 send_panic_events(ptr);
3095 static struct notifier_block panic_block = {
3098 200 /* priority: INT_MAX >= x >= 0 */
3101 static int ipmi_init_msghandler(void)
3108 printk(KERN_INFO "ipmi message handler version "
3109 IPMI_MSGHANDLER_VERSION "\n");
3111 for (i=0; i<MAX_IPMI_INTERFACES; i++) {
3112 ipmi_interfaces[i] = NULL;
3115 proc_ipmi_root = proc_mkdir("ipmi", NULL);
3116 if (!proc_ipmi_root) {
3117 printk("Unable to create IPMI proc dir");
3121 proc_ipmi_root->owner = THIS_MODULE;
3123 init_timer(&ipmi_timer);
3124 ipmi_timer.data = 0;
3125 ipmi_timer.function = ipmi_timeout;
3126 ipmi_timer.expires = jiffies + IPMI_TIMEOUT_JIFFIES;
3127 add_timer(&ipmi_timer);
3129 notifier_chain_register(&panic_notifier_list, &panic_block);
3136 static __init int ipmi_init_msghandler_mod(void)
3138 ipmi_init_msghandler();
3142 static __exit void cleanup_ipmi(void)
3149 notifier_chain_unregister(&panic_notifier_list, &panic_block);
3151 /* This can't be called if any interfaces exist, so no worry about
3152 shutting down the interfaces. */
3154 /* Tell the timer to stop, then wait for it to stop. This avoids
3155 problems with race conditions removing the timer here. */
3157 while (!timer_stopped) {
3158 set_current_state(TASK_UNINTERRUPTIBLE);
3159 schedule_timeout(1);
3162 remove_proc_entry(proc_ipmi_root->name, &proc_root);
3166 /* Check for buffer leaks. */
3167 count = atomic_read(&smi_msg_inuse_count);
3169 printk("ipmi_msghandler: SMI message count %d at exit\n",
3171 count = atomic_read(&recv_msg_inuse_count);
3173 printk("ipmi_msghandler: recv message count %d at exit\n",
3176 module_exit(cleanup_ipmi);
3178 module_init(ipmi_init_msghandler_mod);
3179 MODULE_LICENSE("GPL");
3181 EXPORT_SYMBOL(ipmi_alloc_recv_msg);
3182 EXPORT_SYMBOL(ipmi_create_user);
3183 EXPORT_SYMBOL(ipmi_destroy_user);
3184 EXPORT_SYMBOL(ipmi_get_version);
3185 EXPORT_SYMBOL(ipmi_request);
3186 EXPORT_SYMBOL(ipmi_request_settime);
3187 EXPORT_SYMBOL(ipmi_request_supply_msgs);
3188 EXPORT_SYMBOL(ipmi_request_with_source);
3189 EXPORT_SYMBOL(ipmi_poll_interface);
3190 EXPORT_SYMBOL(ipmi_register_smi);
3191 EXPORT_SYMBOL(ipmi_unregister_smi);
3192 EXPORT_SYMBOL(ipmi_register_for_cmd);
3193 EXPORT_SYMBOL(ipmi_unregister_for_cmd);
3194 EXPORT_SYMBOL(ipmi_smi_msg_received);
3195 EXPORT_SYMBOL(ipmi_smi_watchdog_pretimeout);
3196 EXPORT_SYMBOL(ipmi_alloc_smi_msg);
3197 EXPORT_SYMBOL(ipmi_register_all_cmd_rcvr);
3198 EXPORT_SYMBOL(ipmi_unregister_all_cmd_rcvr);
3199 EXPORT_SYMBOL(ipmi_addr_length);
3200 EXPORT_SYMBOL(ipmi_validate_addr);
3201 EXPORT_SYMBOL(ipmi_set_gets_events);
3202 EXPORT_SYMBOL(ipmi_addr_equal);
3203 EXPORT_SYMBOL(ipmi_smi_watcher_register);
3204 EXPORT_SYMBOL(ipmi_smi_watcher_unregister);
3205 EXPORT_SYMBOL(ipmi_set_my_address);
3206 EXPORT_SYMBOL(ipmi_get_my_address);
3207 EXPORT_SYMBOL(ipmi_set_my_LUN);
3208 EXPORT_SYMBOL(ipmi_get_my_LUN);
3209 EXPORT_SYMBOL(ipmi_smi_add_proc_entry);