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 "v31"
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 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 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 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 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 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 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 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 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. */
1653 if (msg->rsp_size < 6) {
1654 /* Message not big enough, just go on. */
1657 chan = intf->curr_channel;
1658 intf->channels[chan].medium = msg->rsp[4] & 0x7f;
1659 intf->channels[chan].protocol = msg->rsp[5] & 0x1f;
1662 intf->curr_channel++;
1663 if (intf->curr_channel >= IPMI_MAX_CHANNELS)
1664 wake_up(&intf->waitq);
1666 rv = send_channel_info_cmd(intf, intf->curr_channel);
1669 /* Got an error somehow, just give up. */
1670 intf->curr_channel = IPMI_MAX_CHANNELS;
1671 wake_up(&intf->waitq);
1673 printk(KERN_WARNING "ipmi_msghandler: Error sending"
1674 "channel information: 0x%x\n",
1680 int ipmi_register_smi(struct ipmi_smi_handlers *handlers,
1682 unsigned char version_major,
1683 unsigned char version_minor,
1688 ipmi_smi_t new_intf;
1689 unsigned long flags;
1692 /* Make sure the driver is actually initialized, this handles
1693 problems with initialization order. */
1695 rv = ipmi_init_msghandler();
1698 /* The init code doesn't return an error if it was turned
1699 off, but it won't initialize. Check that. */
1704 new_intf = kmalloc(sizeof(*new_intf), GFP_KERNEL);
1707 memset(new_intf, 0, sizeof(*new_intf));
1709 new_intf->proc_dir = NULL;
1713 down_write(&interfaces_sem);
1714 for (i=0; i<MAX_IPMI_INTERFACES; i++) {
1715 if (ipmi_interfaces[i] == NULL) {
1716 new_intf->intf_num = i;
1717 new_intf->version_major = version_major;
1718 new_intf->version_minor = version_minor;
1719 new_intf->my_address = IPMI_BMC_SLAVE_ADDR;
1720 new_intf->my_lun = 2; /* the SMS LUN. */
1721 rwlock_init(&(new_intf->users_lock));
1722 INIT_LIST_HEAD(&(new_intf->users));
1723 new_intf->handlers = handlers;
1724 new_intf->send_info = send_info;
1725 spin_lock_init(&(new_intf->seq_lock));
1726 for (j=0; j<IPMI_IPMB_NUM_SEQ; j++) {
1727 new_intf->seq_table[j].inuse = 0;
1728 new_intf->seq_table[j].seqid = 0;
1730 new_intf->curr_seq = 0;
1731 spin_lock_init(&(new_intf->waiting_msgs_lock));
1732 INIT_LIST_HEAD(&(new_intf->waiting_msgs));
1733 spin_lock_init(&(new_intf->events_lock));
1734 INIT_LIST_HEAD(&(new_intf->waiting_events));
1735 new_intf->waiting_events_count = 0;
1736 rwlock_init(&(new_intf->cmd_rcvr_lock));
1737 init_waitqueue_head(&new_intf->waitq);
1738 INIT_LIST_HEAD(&(new_intf->cmd_rcvrs));
1739 new_intf->all_cmd_rcvr = NULL;
1741 spin_lock_init(&(new_intf->counter_lock));
1743 spin_lock_irqsave(&interfaces_lock, flags);
1744 ipmi_interfaces[i] = new_intf;
1745 spin_unlock_irqrestore(&interfaces_lock, flags);
1753 downgrade_write(&interfaces_sem);
1756 rv = add_proc_entries(*intf, i);
1759 if ((version_major > 1)
1760 || ((version_major == 1) && (version_minor >= 5)))
1762 /* Start scanning the channels to see what is
1764 (*intf)->null_user_handler = channel_handler;
1765 (*intf)->curr_channel = 0;
1766 rv = send_channel_info_cmd(*intf, 0);
1770 /* Wait for the channel info to be read. */
1771 up_read(&interfaces_sem);
1772 wait_event((*intf)->waitq,
1773 ((*intf)->curr_channel>=IPMI_MAX_CHANNELS));
1774 down_read(&interfaces_sem);
1776 if (ipmi_interfaces[i] != new_intf)
1777 /* Well, it went away. Just return. */
1780 /* Assume a single IPMB channel at zero. */
1781 (*intf)->channels[0].medium = IPMI_CHANNEL_MEDIUM_IPMB;
1782 (*intf)->channels[0].protocol
1783 = IPMI_CHANNEL_PROTOCOL_IPMB;
1786 /* Call all the watcher interfaces to tell
1787 them that a new interface is available. */
1788 call_smi_watchers(i);
1792 up_read(&interfaces_sem);
1795 if (new_intf->proc_dir)
1796 remove_proc_entries(new_intf);
1803 static void free_recv_msg_list(struct list_head *q)
1805 struct ipmi_recv_msg *msg, *msg2;
1807 list_for_each_entry_safe(msg, msg2, q, link) {
1808 list_del(&msg->link);
1809 ipmi_free_recv_msg(msg);
1813 static void free_cmd_rcvr_list(struct list_head *q)
1815 struct cmd_rcvr *rcvr, *rcvr2;
1817 list_for_each_entry_safe(rcvr, rcvr2, q, link) {
1818 list_del(&rcvr->link);
1823 static void clean_up_interface_data(ipmi_smi_t intf)
1827 free_recv_msg_list(&(intf->waiting_msgs));
1828 free_recv_msg_list(&(intf->waiting_events));
1829 free_cmd_rcvr_list(&(intf->cmd_rcvrs));
1831 for (i=0; i<IPMI_IPMB_NUM_SEQ; i++) {
1832 if ((intf->seq_table[i].inuse)
1833 && (intf->seq_table[i].recv_msg))
1835 ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
1840 int ipmi_unregister_smi(ipmi_smi_t intf)
1844 struct ipmi_smi_watcher *w;
1845 unsigned long flags;
1847 down_write(&interfaces_sem);
1848 if (list_empty(&(intf->users)))
1850 for (i=0; i<MAX_IPMI_INTERFACES; i++) {
1851 if (ipmi_interfaces[i] == intf) {
1852 remove_proc_entries(intf);
1853 spin_lock_irqsave(&interfaces_lock, flags);
1854 ipmi_interfaces[i] = NULL;
1855 clean_up_interface_data(intf);
1856 spin_unlock_irqrestore(&interfaces_lock,flags);
1859 goto out_call_watcher;
1865 up_write(&interfaces_sem);
1870 downgrade_write(&interfaces_sem);
1872 /* Call all the watcher interfaces to tell them that
1873 an interface is gone. */
1874 down_read(&smi_watchers_sem);
1875 list_for_each_entry(w, &smi_watchers, link) {
1878 up_read(&smi_watchers_sem);
1879 up_read(&interfaces_sem);
1883 static int handle_ipmb_get_msg_rsp(ipmi_smi_t intf,
1884 struct ipmi_smi_msg *msg)
1886 struct ipmi_ipmb_addr ipmb_addr;
1887 struct ipmi_recv_msg *recv_msg;
1888 unsigned long flags;
1891 /* This is 11, not 10, because the response must contain a
1892 * completion code. */
1893 if (msg->rsp_size < 11) {
1894 /* Message not big enough, just ignore it. */
1895 spin_lock_irqsave(&intf->counter_lock, flags);
1896 intf->invalid_ipmb_responses++;
1897 spin_unlock_irqrestore(&intf->counter_lock, flags);
1901 if (msg->rsp[2] != 0) {
1902 /* An error getting the response, just ignore it. */
1906 ipmb_addr.addr_type = IPMI_IPMB_ADDR_TYPE;
1907 ipmb_addr.slave_addr = msg->rsp[6];
1908 ipmb_addr.channel = msg->rsp[3] & 0x0f;
1909 ipmb_addr.lun = msg->rsp[7] & 3;
1911 /* It's a response from a remote entity. Look up the sequence
1912 number and handle the response. */
1913 if (intf_find_seq(intf,
1917 (msg->rsp[4] >> 2) & (~1),
1918 (struct ipmi_addr *) &(ipmb_addr),
1921 /* We were unable to find the sequence number,
1922 so just nuke the message. */
1923 spin_lock_irqsave(&intf->counter_lock, flags);
1924 intf->unhandled_ipmb_responses++;
1925 spin_unlock_irqrestore(&intf->counter_lock, flags);
1929 memcpy(recv_msg->msg_data,
1932 /* THe other fields matched, so no need to set them, except
1933 for netfn, which needs to be the response that was
1934 returned, not the request value. */
1935 recv_msg->msg.netfn = msg->rsp[4] >> 2;
1936 recv_msg->msg.data = recv_msg->msg_data;
1937 recv_msg->msg.data_len = msg->rsp_size - 10;
1938 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
1939 spin_lock_irqsave(&intf->counter_lock, flags);
1940 intf->handled_ipmb_responses++;
1941 spin_unlock_irqrestore(&intf->counter_lock, flags);
1942 deliver_response(recv_msg);
1947 static int handle_ipmb_get_msg_cmd(ipmi_smi_t intf,
1948 struct ipmi_smi_msg *msg)
1950 struct cmd_rcvr *rcvr;
1952 unsigned char netfn;
1954 ipmi_user_t user = NULL;
1955 struct ipmi_ipmb_addr *ipmb_addr;
1956 struct ipmi_recv_msg *recv_msg;
1957 unsigned long flags;
1959 if (msg->rsp_size < 10) {
1960 /* Message not big enough, just ignore it. */
1961 spin_lock_irqsave(&intf->counter_lock, flags);
1962 intf->invalid_commands++;
1963 spin_unlock_irqrestore(&intf->counter_lock, flags);
1967 if (msg->rsp[2] != 0) {
1968 /* An error getting the response, just ignore it. */
1972 netfn = msg->rsp[4] >> 2;
1975 read_lock(&(intf->cmd_rcvr_lock));
1977 if (intf->all_cmd_rcvr) {
1978 user = intf->all_cmd_rcvr;
1980 /* Find the command/netfn. */
1981 list_for_each_entry(rcvr, &(intf->cmd_rcvrs), link) {
1982 if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)) {
1988 read_unlock(&(intf->cmd_rcvr_lock));
1991 /* We didn't find a user, deliver an error response. */
1992 spin_lock_irqsave(&intf->counter_lock, flags);
1993 intf->unhandled_commands++;
1994 spin_unlock_irqrestore(&intf->counter_lock, flags);
1996 msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
1997 msg->data[1] = IPMI_SEND_MSG_CMD;
1998 msg->data[2] = msg->rsp[3];
1999 msg->data[3] = msg->rsp[6];
2000 msg->data[4] = ((netfn + 1) << 2) | (msg->rsp[7] & 0x3);
2001 msg->data[5] = ipmb_checksum(&(msg->data[3]), 2);
2002 msg->data[6] = intf->my_address;
2004 msg->data[7] = (msg->rsp[7] & 0xfc) | (msg->rsp[4] & 0x3);
2005 msg->data[8] = msg->rsp[8]; /* cmd */
2006 msg->data[9] = IPMI_INVALID_CMD_COMPLETION_CODE;
2007 msg->data[10] = ipmb_checksum(&(msg->data[6]), 4);
2008 msg->data_size = 11;
2013 printk("Invalid command:");
2014 for (m=0; m<msg->data_size; m++)
2015 printk(" %2.2x", msg->data[m]);
2019 intf->handlers->sender(intf->send_info, msg, 0);
2021 rv = -1; /* We used the message, so return the value that
2022 causes it to not be freed or queued. */
2024 /* Deliver the message to the user. */
2025 spin_lock_irqsave(&intf->counter_lock, flags);
2026 intf->handled_commands++;
2027 spin_unlock_irqrestore(&intf->counter_lock, flags);
2029 recv_msg = ipmi_alloc_recv_msg();
2031 /* We couldn't allocate memory for the
2032 message, so requeue it for handling
2036 /* Extract the source address from the data. */
2037 ipmb_addr = (struct ipmi_ipmb_addr *) &recv_msg->addr;
2038 ipmb_addr->addr_type = IPMI_IPMB_ADDR_TYPE;
2039 ipmb_addr->slave_addr = msg->rsp[6];
2040 ipmb_addr->lun = msg->rsp[7] & 3;
2041 ipmb_addr->channel = msg->rsp[3] & 0xf;
2043 /* Extract the rest of the message information
2044 from the IPMB header.*/
2045 recv_msg->user = user;
2046 recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
2047 recv_msg->msgid = msg->rsp[7] >> 2;
2048 recv_msg->msg.netfn = msg->rsp[4] >> 2;
2049 recv_msg->msg.cmd = msg->rsp[8];
2050 recv_msg->msg.data = recv_msg->msg_data;
2052 /* We chop off 10, not 9 bytes because the checksum
2053 at the end also needs to be removed. */
2054 recv_msg->msg.data_len = msg->rsp_size - 10;
2055 memcpy(recv_msg->msg_data,
2057 msg->rsp_size - 10);
2058 deliver_response(recv_msg);
2065 static int handle_lan_get_msg_rsp(ipmi_smi_t intf,
2066 struct ipmi_smi_msg *msg)
2068 struct ipmi_lan_addr lan_addr;
2069 struct ipmi_recv_msg *recv_msg;
2070 unsigned long flags;
2073 /* This is 13, not 12, because the response must contain a
2074 * completion code. */
2075 if (msg->rsp_size < 13) {
2076 /* Message not big enough, just ignore it. */
2077 spin_lock_irqsave(&intf->counter_lock, flags);
2078 intf->invalid_lan_responses++;
2079 spin_unlock_irqrestore(&intf->counter_lock, flags);
2083 if (msg->rsp[2] != 0) {
2084 /* An error getting the response, just ignore it. */
2088 lan_addr.addr_type = IPMI_LAN_ADDR_TYPE;
2089 lan_addr.session_handle = msg->rsp[4];
2090 lan_addr.remote_SWID = msg->rsp[8];
2091 lan_addr.local_SWID = msg->rsp[5];
2092 lan_addr.channel = msg->rsp[3] & 0x0f;
2093 lan_addr.privilege = msg->rsp[3] >> 4;
2094 lan_addr.lun = msg->rsp[9] & 3;
2096 /* It's a response from a remote entity. Look up the sequence
2097 number and handle the response. */
2098 if (intf_find_seq(intf,
2102 (msg->rsp[6] >> 2) & (~1),
2103 (struct ipmi_addr *) &(lan_addr),
2106 /* We were unable to find the sequence number,
2107 so just nuke the message. */
2108 spin_lock_irqsave(&intf->counter_lock, flags);
2109 intf->unhandled_lan_responses++;
2110 spin_unlock_irqrestore(&intf->counter_lock, flags);
2114 memcpy(recv_msg->msg_data,
2116 msg->rsp_size - 11);
2117 /* The other fields matched, so no need to set them, except
2118 for netfn, which needs to be the response that was
2119 returned, not the request value. */
2120 recv_msg->msg.netfn = msg->rsp[6] >> 2;
2121 recv_msg->msg.data = recv_msg->msg_data;
2122 recv_msg->msg.data_len = msg->rsp_size - 12;
2123 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
2124 spin_lock_irqsave(&intf->counter_lock, flags);
2125 intf->handled_lan_responses++;
2126 spin_unlock_irqrestore(&intf->counter_lock, flags);
2127 deliver_response(recv_msg);
2132 static int handle_lan_get_msg_cmd(ipmi_smi_t intf,
2133 struct ipmi_smi_msg *msg)
2135 struct cmd_rcvr *rcvr;
2137 unsigned char netfn;
2139 ipmi_user_t user = NULL;
2140 struct ipmi_lan_addr *lan_addr;
2141 struct ipmi_recv_msg *recv_msg;
2142 unsigned long flags;
2144 if (msg->rsp_size < 12) {
2145 /* Message not big enough, just ignore it. */
2146 spin_lock_irqsave(&intf->counter_lock, flags);
2147 intf->invalid_commands++;
2148 spin_unlock_irqrestore(&intf->counter_lock, flags);
2152 if (msg->rsp[2] != 0) {
2153 /* An error getting the response, just ignore it. */
2157 netfn = msg->rsp[6] >> 2;
2160 read_lock(&(intf->cmd_rcvr_lock));
2162 if (intf->all_cmd_rcvr) {
2163 user = intf->all_cmd_rcvr;
2165 /* Find the command/netfn. */
2166 list_for_each_entry(rcvr, &(intf->cmd_rcvrs), link) {
2167 if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)) {
2173 read_unlock(&(intf->cmd_rcvr_lock));
2176 /* We didn't find a user, deliver an error response. */
2177 spin_lock_irqsave(&intf->counter_lock, flags);
2178 intf->unhandled_commands++;
2179 spin_unlock_irqrestore(&intf->counter_lock, flags);
2181 rv = 0; /* Don't do anything with these messages, just
2182 allow them to be freed. */
2184 /* Deliver the message to the user. */
2185 spin_lock_irqsave(&intf->counter_lock, flags);
2186 intf->handled_commands++;
2187 spin_unlock_irqrestore(&intf->counter_lock, flags);
2189 recv_msg = ipmi_alloc_recv_msg();
2191 /* We couldn't allocate memory for the
2192 message, so requeue it for handling
2196 /* Extract the source address from the data. */
2197 lan_addr = (struct ipmi_lan_addr *) &recv_msg->addr;
2198 lan_addr->addr_type = IPMI_LAN_ADDR_TYPE;
2199 lan_addr->session_handle = msg->rsp[4];
2200 lan_addr->remote_SWID = msg->rsp[8];
2201 lan_addr->local_SWID = msg->rsp[5];
2202 lan_addr->lun = msg->rsp[9] & 3;
2203 lan_addr->channel = msg->rsp[3] & 0xf;
2204 lan_addr->privilege = msg->rsp[3] >> 4;
2206 /* Extract the rest of the message information
2207 from the IPMB header.*/
2208 recv_msg->user = user;
2209 recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
2210 recv_msg->msgid = msg->rsp[9] >> 2;
2211 recv_msg->msg.netfn = msg->rsp[6] >> 2;
2212 recv_msg->msg.cmd = msg->rsp[10];
2213 recv_msg->msg.data = recv_msg->msg_data;
2215 /* We chop off 12, not 11 bytes because the checksum
2216 at the end also needs to be removed. */
2217 recv_msg->msg.data_len = msg->rsp_size - 12;
2218 memcpy(recv_msg->msg_data,
2220 msg->rsp_size - 12);
2221 deliver_response(recv_msg);
2228 static void copy_event_into_recv_msg(struct ipmi_recv_msg *recv_msg,
2229 struct ipmi_smi_msg *msg)
2231 struct ipmi_system_interface_addr *smi_addr;
2233 recv_msg->msgid = 0;
2234 smi_addr = (struct ipmi_system_interface_addr *) &(recv_msg->addr);
2235 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2236 smi_addr->channel = IPMI_BMC_CHANNEL;
2237 smi_addr->lun = msg->rsp[0] & 3;
2238 recv_msg->recv_type = IPMI_ASYNC_EVENT_RECV_TYPE;
2239 recv_msg->msg.netfn = msg->rsp[0] >> 2;
2240 recv_msg->msg.cmd = msg->rsp[1];
2241 memcpy(recv_msg->msg_data, &(msg->rsp[3]), msg->rsp_size - 3);
2242 recv_msg->msg.data = recv_msg->msg_data;
2243 recv_msg->msg.data_len = msg->rsp_size - 3;
2246 /* This will be called with the intf->users_lock read-locked, so no need
2248 static int handle_read_event_rsp(ipmi_smi_t intf,
2249 struct ipmi_smi_msg *msg)
2251 struct ipmi_recv_msg *recv_msg, *recv_msg2;
2252 struct list_head msgs;
2255 int deliver_count = 0;
2256 unsigned long flags;
2258 if (msg->rsp_size < 19) {
2259 /* Message is too small to be an IPMB event. */
2260 spin_lock_irqsave(&intf->counter_lock, flags);
2261 intf->invalid_events++;
2262 spin_unlock_irqrestore(&intf->counter_lock, flags);
2266 if (msg->rsp[2] != 0) {
2267 /* An error getting the event, just ignore it. */
2271 INIT_LIST_HEAD(&msgs);
2273 spin_lock_irqsave(&(intf->events_lock), flags);
2275 spin_lock(&intf->counter_lock);
2277 spin_unlock(&intf->counter_lock);
2279 /* Allocate and fill in one message for every user that is getting
2281 list_for_each_entry(user, &(intf->users), link) {
2282 if (! user->gets_events)
2285 recv_msg = ipmi_alloc_recv_msg();
2287 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs, link) {
2288 list_del(&recv_msg->link);
2289 ipmi_free_recv_msg(recv_msg);
2291 /* We couldn't allocate memory for the
2292 message, so requeue it for handling
2300 copy_event_into_recv_msg(recv_msg, msg);
2301 recv_msg->user = user;
2302 list_add_tail(&(recv_msg->link), &msgs);
2305 if (deliver_count) {
2306 /* Now deliver all the messages. */
2307 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs, link) {
2308 list_del(&recv_msg->link);
2309 deliver_response(recv_msg);
2311 } else if (intf->waiting_events_count < MAX_EVENTS_IN_QUEUE) {
2312 /* No one to receive the message, put it in queue if there's
2313 not already too many things in the queue. */
2314 recv_msg = ipmi_alloc_recv_msg();
2316 /* We couldn't allocate memory for the
2317 message, so requeue it for handling
2323 copy_event_into_recv_msg(recv_msg, msg);
2324 list_add_tail(&(recv_msg->link), &(intf->waiting_events));
2326 /* There's too many things in the queue, discard this
2328 printk(KERN_WARNING "ipmi: Event queue full, discarding an"
2329 " incoming event\n");
2333 spin_unlock_irqrestore(&(intf->events_lock), flags);
2338 static int handle_bmc_rsp(ipmi_smi_t intf,
2339 struct ipmi_smi_msg *msg)
2341 struct ipmi_recv_msg *recv_msg;
2343 struct ipmi_user *user;
2344 unsigned long flags;
2346 recv_msg = (struct ipmi_recv_msg *) msg->user_data;
2348 /* Make sure the user still exists. */
2349 list_for_each_entry(user, &(intf->users), link) {
2350 if (user == recv_msg->user) {
2351 /* Found it, so we can deliver it */
2358 /* Special handling for NULL users. */
2359 if (!recv_msg->user && intf->null_user_handler)
2360 intf->null_user_handler(intf, msg);
2361 /* The user for the message went away, so give up. */
2362 spin_lock_irqsave(&intf->counter_lock, flags);
2363 intf->unhandled_local_responses++;
2364 spin_unlock_irqrestore(&intf->counter_lock, flags);
2365 ipmi_free_recv_msg(recv_msg);
2367 struct ipmi_system_interface_addr *smi_addr;
2369 spin_lock_irqsave(&intf->counter_lock, flags);
2370 intf->handled_local_responses++;
2371 spin_unlock_irqrestore(&intf->counter_lock, flags);
2372 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
2373 recv_msg->msgid = msg->msgid;
2374 smi_addr = ((struct ipmi_system_interface_addr *)
2376 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2377 smi_addr->channel = IPMI_BMC_CHANNEL;
2378 smi_addr->lun = msg->rsp[0] & 3;
2379 recv_msg->msg.netfn = msg->rsp[0] >> 2;
2380 recv_msg->msg.cmd = msg->rsp[1];
2381 memcpy(recv_msg->msg_data,
2384 recv_msg->msg.data = recv_msg->msg_data;
2385 recv_msg->msg.data_len = msg->rsp_size - 2;
2386 deliver_response(recv_msg);
2392 /* Handle a new message. Return 1 if the message should be requeued,
2393 0 if the message should be freed, or -1 if the message should not
2394 be freed or requeued. */
2395 static int handle_new_recv_msg(ipmi_smi_t intf,
2396 struct ipmi_smi_msg *msg)
2404 for (m=0; m<msg->rsp_size; m++)
2405 printk(" %2.2x", msg->rsp[m]);
2408 if (msg->rsp_size < 2) {
2409 /* Message is too small to be correct. */
2411 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
2412 && (msg->rsp[1] == IPMI_SEND_MSG_CMD)
2413 && (msg->user_data != NULL))
2415 /* It's a response to a response we sent. For this we
2416 deliver a send message response to the user. */
2417 struct ipmi_recv_msg *recv_msg = msg->user_data;
2420 if (msg->rsp_size < 2)
2421 /* Message is too small to be correct. */
2424 chan = msg->data[2] & 0x0f;
2425 if (chan >= IPMI_MAX_CHANNELS)
2426 /* Invalid channel number */
2430 recv_msg->recv_type = IPMI_RESPONSE_RESPONSE_TYPE;
2431 recv_msg->msg.data = recv_msg->msg_data;
2432 recv_msg->msg.data_len = 1;
2433 recv_msg->msg_data[0] = msg->rsp[2];
2434 deliver_response(recv_msg);
2436 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
2437 && (msg->rsp[1] == IPMI_GET_MSG_CMD))
2439 /* It's from the receive queue. */
2440 chan = msg->rsp[3] & 0xf;
2441 if (chan >= IPMI_MAX_CHANNELS) {
2442 /* Invalid channel number */
2447 switch (intf->channels[chan].medium) {
2448 case IPMI_CHANNEL_MEDIUM_IPMB:
2449 if (msg->rsp[4] & 0x04) {
2450 /* It's a response, so find the
2451 requesting message and send it up. */
2452 requeue = handle_ipmb_get_msg_rsp(intf, msg);
2454 /* It's a command to the SMS from some other
2455 entity. Handle that. */
2456 requeue = handle_ipmb_get_msg_cmd(intf, msg);
2460 case IPMI_CHANNEL_MEDIUM_8023LAN:
2461 case IPMI_CHANNEL_MEDIUM_ASYNC:
2462 if (msg->rsp[6] & 0x04) {
2463 /* It's a response, so find the
2464 requesting message and send it up. */
2465 requeue = handle_lan_get_msg_rsp(intf, msg);
2467 /* It's a command to the SMS from some other
2468 entity. Handle that. */
2469 requeue = handle_lan_get_msg_cmd(intf, msg);
2474 /* We don't handle the channel type, so just
2475 * free the message. */
2479 } else if (msg->rsp[1] == IPMI_READ_EVENT_MSG_BUFFER_CMD) {
2480 /* It's an asyncronous event. */
2481 requeue = handle_read_event_rsp(intf, msg);
2483 /* It's a response from the local BMC. */
2484 requeue = handle_bmc_rsp(intf, msg);
2491 /* Handle a new message from the lower layer. */
2492 void ipmi_smi_msg_received(ipmi_smi_t intf,
2493 struct ipmi_smi_msg *msg)
2495 unsigned long flags;
2499 /* Lock the user lock so the user can't go away while we are
2501 read_lock(&(intf->users_lock));
2503 if ((msg->data_size >= 2)
2504 && (msg->data[0] == (IPMI_NETFN_APP_REQUEST << 2))
2505 && (msg->data[1] == IPMI_SEND_MSG_CMD)
2506 && (msg->user_data == NULL)) {
2507 /* This is the local response to a command send, start
2508 the timer for these. The user_data will not be
2509 NULL if this is a response send, and we will let
2510 response sends just go through. */
2512 /* Check for errors, if we get certain errors (ones
2513 that mean basically we can try again later), we
2514 ignore them and start the timer. Otherwise we
2515 report the error immediately. */
2516 if ((msg->rsp_size >= 3) && (msg->rsp[2] != 0)
2517 && (msg->rsp[2] != IPMI_NODE_BUSY_ERR)
2518 && (msg->rsp[2] != IPMI_LOST_ARBITRATION_ERR))
2520 int chan = msg->rsp[3] & 0xf;
2522 /* Got an error sending the message, handle it. */
2523 spin_lock_irqsave(&intf->counter_lock, flags);
2524 if (chan >= IPMI_MAX_CHANNELS)
2525 ; /* This shouldn't happen */
2526 else if ((intf->channels[chan].medium
2527 == IPMI_CHANNEL_MEDIUM_8023LAN)
2528 || (intf->channels[chan].medium
2529 == IPMI_CHANNEL_MEDIUM_ASYNC))
2530 intf->sent_lan_command_errs++;
2532 intf->sent_ipmb_command_errs++;
2533 spin_unlock_irqrestore(&intf->counter_lock, flags);
2534 intf_err_seq(intf, msg->msgid, msg->rsp[2]);
2536 /* The message was sent, start the timer. */
2537 intf_start_seq_timer(intf, msg->msgid);
2540 ipmi_free_smi_msg(msg);
2544 /* To preserve message order, if the list is not empty, we
2545 tack this message onto the end of the list. */
2546 spin_lock_irqsave(&(intf->waiting_msgs_lock), flags);
2547 if (!list_empty(&(intf->waiting_msgs))) {
2548 list_add_tail(&(msg->link), &(intf->waiting_msgs));
2549 spin_unlock(&(intf->waiting_msgs_lock));
2552 spin_unlock_irqrestore(&(intf->waiting_msgs_lock), flags);
2554 rv = handle_new_recv_msg(intf, msg);
2556 /* Could not handle the message now, just add it to a
2557 list to handle later. */
2558 spin_lock(&(intf->waiting_msgs_lock));
2559 list_add_tail(&(msg->link), &(intf->waiting_msgs));
2560 spin_unlock(&(intf->waiting_msgs_lock));
2561 } else if (rv == 0) {
2562 ipmi_free_smi_msg(msg);
2566 read_unlock(&(intf->users_lock));
2569 void ipmi_smi_watchdog_pretimeout(ipmi_smi_t intf)
2573 read_lock(&(intf->users_lock));
2574 list_for_each_entry(user, &(intf->users), link) {
2575 if (! user->handler->ipmi_watchdog_pretimeout)
2578 user->handler->ipmi_watchdog_pretimeout(user->handler_data);
2580 read_unlock(&(intf->users_lock));
2584 handle_msg_timeout(struct ipmi_recv_msg *msg)
2586 msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
2587 msg->msg_data[0] = IPMI_TIMEOUT_COMPLETION_CODE;
2588 msg->msg.netfn |= 1; /* Convert to a response. */
2589 msg->msg.data_len = 1;
2590 msg->msg.data = msg->msg_data;
2591 deliver_response(msg);
2595 send_from_recv_msg(ipmi_smi_t intf, struct ipmi_recv_msg *recv_msg,
2596 struct ipmi_smi_msg *smi_msg,
2597 unsigned char seq, long seqid)
2600 smi_msg = ipmi_alloc_smi_msg();
2602 /* If we can't allocate the message, then just return, we
2603 get 4 retries, so this should be ok. */
2606 memcpy(smi_msg->data, recv_msg->msg.data, recv_msg->msg.data_len);
2607 smi_msg->data_size = recv_msg->msg.data_len;
2608 smi_msg->msgid = STORE_SEQ_IN_MSGID(seq, seqid);
2610 /* Send the new message. We send with a zero priority. It
2611 timed out, I doubt time is that critical now, and high
2612 priority messages are really only for messages to the local
2613 MC, which don't get resent. */
2614 intf->handlers->sender(intf->send_info, smi_msg, 0);
2620 for (m=0; m<smi_msg->data_size; m++)
2621 printk(" %2.2x", smi_msg->data[m]);
2628 ipmi_timeout_handler(long timeout_period)
2631 struct list_head timeouts;
2632 struct ipmi_recv_msg *msg, *msg2;
2633 struct ipmi_smi_msg *smi_msg, *smi_msg2;
2634 unsigned long flags;
2637 INIT_LIST_HEAD(&timeouts);
2639 spin_lock(&interfaces_lock);
2640 for (i=0; i<MAX_IPMI_INTERFACES; i++) {
2641 intf = ipmi_interfaces[i];
2645 read_lock(&(intf->users_lock));
2647 /* See if any waiting messages need to be processed. */
2648 spin_lock_irqsave(&(intf->waiting_msgs_lock), flags);
2649 list_for_each_entry_safe(smi_msg, smi_msg2, &(intf->waiting_msgs), link) {
2650 if (! handle_new_recv_msg(intf, smi_msg)) {
2651 list_del(&smi_msg->link);
2652 ipmi_free_smi_msg(smi_msg);
2654 /* To preserve message order, quit if we
2655 can't handle a message. */
2659 spin_unlock_irqrestore(&(intf->waiting_msgs_lock), flags);
2661 /* Go through the seq table and find any messages that
2662 have timed out, putting them in the timeouts
2664 spin_lock_irqsave(&(intf->seq_lock), flags);
2665 for (j=0; j<IPMI_IPMB_NUM_SEQ; j++) {
2666 struct seq_table *ent = &(intf->seq_table[j]);
2670 ent->timeout -= timeout_period;
2671 if (ent->timeout > 0)
2674 if (ent->retries_left == 0) {
2675 /* The message has used all its retries. */
2677 msg = ent->recv_msg;
2678 list_add_tail(&(msg->link), &timeouts);
2679 spin_lock(&intf->counter_lock);
2681 intf->timed_out_ipmb_broadcasts++;
2682 else if (ent->recv_msg->addr.addr_type
2683 == IPMI_LAN_ADDR_TYPE)
2684 intf->timed_out_lan_commands++;
2686 intf->timed_out_ipmb_commands++;
2687 spin_unlock(&intf->counter_lock);
2689 /* More retries, send again. */
2691 /* Start with the max timer, set to normal
2692 timer after the message is sent. */
2693 ent->timeout = MAX_MSG_TIMEOUT;
2694 ent->retries_left--;
2695 send_from_recv_msg(intf, ent->recv_msg, NULL,
2697 spin_lock(&intf->counter_lock);
2698 if (ent->recv_msg->addr.addr_type
2699 == IPMI_LAN_ADDR_TYPE)
2700 intf->retransmitted_lan_commands++;
2702 intf->retransmitted_ipmb_commands++;
2703 spin_unlock(&intf->counter_lock);
2706 spin_unlock_irqrestore(&(intf->seq_lock), flags);
2708 list_for_each_entry_safe(msg, msg2, &timeouts, link) {
2709 handle_msg_timeout(msg);
2712 read_unlock(&(intf->users_lock));
2714 spin_unlock(&interfaces_lock);
2717 static void ipmi_request_event(void)
2722 spin_lock(&interfaces_lock);
2723 for (i=0; i<MAX_IPMI_INTERFACES; i++) {
2724 intf = ipmi_interfaces[i];
2728 intf->handlers->request_events(intf->send_info);
2730 spin_unlock(&interfaces_lock);
2733 static struct timer_list ipmi_timer;
2735 /* Call every ~100 ms. */
2736 #define IPMI_TIMEOUT_TIME 100
2738 /* How many jiffies does it take to get to the timeout time. */
2739 #define IPMI_TIMEOUT_JIFFIES ((IPMI_TIMEOUT_TIME * HZ) / 1000)
2741 /* Request events from the queue every second (this is the number of
2742 IPMI_TIMEOUT_TIMES between event requests). Hopefully, in the
2743 future, IPMI will add a way to know immediately if an event is in
2744 the queue and this silliness can go away. */
2745 #define IPMI_REQUEST_EV_TIME (1000 / (IPMI_TIMEOUT_TIME))
2747 static volatile int stop_operation = 0;
2748 static volatile int timer_stopped = 0;
2749 static unsigned int ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
2751 static void ipmi_timeout(unsigned long data)
2753 if (stop_operation) {
2759 if (ticks_to_req_ev == 0) {
2760 ipmi_request_event();
2761 ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
2764 ipmi_timeout_handler(IPMI_TIMEOUT_TIME);
2766 ipmi_timer.expires += IPMI_TIMEOUT_JIFFIES;
2767 add_timer(&ipmi_timer);
2771 static atomic_t smi_msg_inuse_count = ATOMIC_INIT(0);
2772 static atomic_t recv_msg_inuse_count = ATOMIC_INIT(0);
2774 /* FIXME - convert these to slabs. */
2775 static void free_smi_msg(struct ipmi_smi_msg *msg)
2777 atomic_dec(&smi_msg_inuse_count);
2781 struct ipmi_smi_msg *ipmi_alloc_smi_msg(void)
2783 struct ipmi_smi_msg *rv;
2784 rv = kmalloc(sizeof(struct ipmi_smi_msg), GFP_ATOMIC);
2786 rv->done = free_smi_msg;
2787 rv->user_data = NULL;
2788 atomic_inc(&smi_msg_inuse_count);
2793 static void free_recv_msg(struct ipmi_recv_msg *msg)
2795 atomic_dec(&recv_msg_inuse_count);
2799 struct ipmi_recv_msg *ipmi_alloc_recv_msg(void)
2801 struct ipmi_recv_msg *rv;
2803 rv = kmalloc(sizeof(struct ipmi_recv_msg), GFP_ATOMIC);
2805 rv->done = free_recv_msg;
2806 atomic_inc(&recv_msg_inuse_count);
2811 #ifdef CONFIG_IPMI_PANIC_EVENT
2813 static void dummy_smi_done_handler(struct ipmi_smi_msg *msg)
2817 static void dummy_recv_done_handler(struct ipmi_recv_msg *msg)
2821 #ifdef CONFIG_IPMI_PANIC_STRING
2822 static void event_receiver_fetcher(ipmi_smi_t intf, struct ipmi_smi_msg *msg)
2824 if ((msg->rsp[0] == (IPMI_NETFN_SENSOR_EVENT_RESPONSE << 2))
2825 && (msg->rsp[1] == IPMI_GET_EVENT_RECEIVER_CMD)
2826 && (msg->rsp[2] == IPMI_CC_NO_ERROR))
2828 /* A get event receiver command, save it. */
2829 intf->event_receiver = msg->rsp[3];
2830 intf->event_receiver_lun = msg->rsp[4] & 0x3;
2834 static void device_id_fetcher(ipmi_smi_t intf, struct ipmi_smi_msg *msg)
2836 if ((msg->rsp[0] == (IPMI_NETFN_APP_RESPONSE << 2))
2837 && (msg->rsp[1] == IPMI_GET_DEVICE_ID_CMD)
2838 && (msg->rsp[2] == IPMI_CC_NO_ERROR))
2840 /* A get device id command, save if we are an event
2841 receiver or generator. */
2842 intf->local_sel_device = (msg->rsp[8] >> 2) & 1;
2843 intf->local_event_generator = (msg->rsp[8] >> 5) & 1;
2848 static void send_panic_events(char *str)
2850 struct ipmi_msg msg;
2852 unsigned char data[16];
2854 struct ipmi_system_interface_addr *si;
2855 struct ipmi_addr addr;
2856 struct ipmi_smi_msg smi_msg;
2857 struct ipmi_recv_msg recv_msg;
2859 si = (struct ipmi_system_interface_addr *) &addr;
2860 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2861 si->channel = IPMI_BMC_CHANNEL;
2864 /* Fill in an event telling that we have failed. */
2865 msg.netfn = 0x04; /* Sensor or Event. */
2866 msg.cmd = 2; /* Platform event command. */
2869 data[0] = 0x21; /* Kernel generator ID, IPMI table 5-4 */
2870 data[1] = 0x03; /* This is for IPMI 1.0. */
2871 data[2] = 0x20; /* OS Critical Stop, IPMI table 36-3 */
2872 data[4] = 0x6f; /* Sensor specific, IPMI table 36-1 */
2873 data[5] = 0xa1; /* Runtime stop OEM bytes 2 & 3. */
2875 /* Put a few breadcrumbs in. Hopefully later we can add more things
2876 to make the panic events more useful. */
2883 smi_msg.done = dummy_smi_done_handler;
2884 recv_msg.done = dummy_recv_done_handler;
2886 /* For every registered interface, send the event. */
2887 for (i=0; i<MAX_IPMI_INTERFACES; i++) {
2888 intf = ipmi_interfaces[i];
2892 /* Send the event announcing the panic. */
2893 intf->handlers->set_run_to_completion(intf->send_info, 1);
2894 i_ipmi_request(NULL,
2905 0, 1); /* Don't retry, and don't wait. */
2908 #ifdef CONFIG_IPMI_PANIC_STRING
2909 /* On every interface, dump a bunch of OEM event holding the
2914 for (i=0; i<MAX_IPMI_INTERFACES; i++) {
2916 struct ipmi_ipmb_addr *ipmb;
2919 intf = ipmi_interfaces[i];
2923 /* First job here is to figure out where to send the
2924 OEM events. There's no way in IPMI to send OEM
2925 events using an event send command, so we have to
2926 find the SEL to put them in and stick them in
2929 /* Get capabilities from the get device id. */
2930 intf->local_sel_device = 0;
2931 intf->local_event_generator = 0;
2932 intf->event_receiver = 0;
2934 /* Request the device info from the local MC. */
2935 msg.netfn = IPMI_NETFN_APP_REQUEST;
2936 msg.cmd = IPMI_GET_DEVICE_ID_CMD;
2939 intf->null_user_handler = device_id_fetcher;
2940 i_ipmi_request(NULL,
2951 0, 1); /* Don't retry, and don't wait. */
2953 if (intf->local_event_generator) {
2954 /* Request the event receiver from the local MC. */
2955 msg.netfn = IPMI_NETFN_SENSOR_EVENT_REQUEST;
2956 msg.cmd = IPMI_GET_EVENT_RECEIVER_CMD;
2959 intf->null_user_handler = event_receiver_fetcher;
2960 i_ipmi_request(NULL,
2971 0, 1); /* no retry, and no wait. */
2973 intf->null_user_handler = NULL;
2975 /* Validate the event receiver. The low bit must not
2976 be 1 (it must be a valid IPMB address), it cannot
2977 be zero, and it must not be my address. */
2978 if (((intf->event_receiver & 1) == 0)
2979 && (intf->event_receiver != 0)
2980 && (intf->event_receiver != intf->my_address))
2982 /* The event receiver is valid, send an IPMB
2984 ipmb = (struct ipmi_ipmb_addr *) &addr;
2985 ipmb->addr_type = IPMI_IPMB_ADDR_TYPE;
2986 ipmb->channel = 0; /* FIXME - is this right? */
2987 ipmb->lun = intf->event_receiver_lun;
2988 ipmb->slave_addr = intf->event_receiver;
2989 } else if (intf->local_sel_device) {
2990 /* The event receiver was not valid (or was
2991 me), but I am an SEL device, just dump it
2993 si = (struct ipmi_system_interface_addr *) &addr;
2994 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2995 si->channel = IPMI_BMC_CHANNEL;
2998 continue; /* No where to send the event. */
3001 msg.netfn = IPMI_NETFN_STORAGE_REQUEST; /* Storage. */
3002 msg.cmd = IPMI_ADD_SEL_ENTRY_CMD;
3008 int size = strlen(p);
3014 data[2] = 0xf0; /* OEM event without timestamp. */
3015 data[3] = intf->my_address;
3016 data[4] = j++; /* sequence # */
3017 /* Always give 11 bytes, so strncpy will fill
3018 it with zeroes for me. */
3019 strncpy(data+5, p, 11);
3022 i_ipmi_request(NULL,
3033 0, 1); /* no retry, and no wait. */
3036 #endif /* CONFIG_IPMI_PANIC_STRING */
3038 #endif /* CONFIG_IPMI_PANIC_EVENT */
3040 static int has_paniced = 0;
3042 static int panic_event(struct notifier_block *this,
3043 unsigned long event,
3053 /* For every registered interface, set it to run to completion. */
3054 for (i=0; i<MAX_IPMI_INTERFACES; i++) {
3055 intf = ipmi_interfaces[i];
3059 intf->handlers->set_run_to_completion(intf->send_info, 1);
3062 #ifdef CONFIG_IPMI_PANIC_EVENT
3063 send_panic_events(ptr);
3069 static struct notifier_block panic_block = {
3072 200 /* priority: INT_MAX >= x >= 0 */
3075 static __init int ipmi_init_msghandler(void)
3082 printk(KERN_INFO "ipmi message handler version "
3083 IPMI_MSGHANDLER_VERSION "\n");
3085 for (i=0; i<MAX_IPMI_INTERFACES; i++) {
3086 ipmi_interfaces[i] = NULL;
3089 proc_ipmi_root = proc_mkdir("ipmi", 0);
3090 if (!proc_ipmi_root) {
3091 printk("Unable to create IPMI proc dir");
3095 proc_ipmi_root->owner = THIS_MODULE;
3097 init_timer(&ipmi_timer);
3098 ipmi_timer.data = 0;
3099 ipmi_timer.function = ipmi_timeout;
3100 ipmi_timer.expires = jiffies + IPMI_TIMEOUT_JIFFIES;
3101 add_timer(&ipmi_timer);
3103 notifier_chain_register(&panic_notifier_list, &panic_block);
3110 static __exit void cleanup_ipmi(void)
3117 notifier_chain_unregister(&panic_notifier_list, &panic_block);
3119 /* This can't be called if any interfaces exist, so no worry about
3120 shutting down the interfaces. */
3122 /* Tell the timer to stop, then wait for it to stop. This avoids
3123 problems with race conditions removing the timer here. */
3125 while (!timer_stopped) {
3126 set_current_state(TASK_UNINTERRUPTIBLE);
3127 schedule_timeout(1);
3130 remove_proc_entry(proc_ipmi_root->name, &proc_root);
3134 /* Check for buffer leaks. */
3135 count = atomic_read(&smi_msg_inuse_count);
3137 printk("ipmi_msghandler: SMI message count %d at exit\n",
3139 count = atomic_read(&recv_msg_inuse_count);
3141 printk("ipmi_msghandler: recv message count %d at exit\n",
3144 module_exit(cleanup_ipmi);
3146 module_init(ipmi_init_msghandler);
3147 MODULE_LICENSE("GPL");
3149 EXPORT_SYMBOL(ipmi_alloc_recv_msg);
3150 EXPORT_SYMBOL(ipmi_create_user);
3151 EXPORT_SYMBOL(ipmi_destroy_user);
3152 EXPORT_SYMBOL(ipmi_get_version);
3153 EXPORT_SYMBOL(ipmi_request);
3154 EXPORT_SYMBOL(ipmi_request_settime);
3155 EXPORT_SYMBOL(ipmi_request_supply_msgs);
3156 EXPORT_SYMBOL(ipmi_request_with_source);
3157 EXPORT_SYMBOL(ipmi_register_smi);
3158 EXPORT_SYMBOL(ipmi_unregister_smi);
3159 EXPORT_SYMBOL(ipmi_register_for_cmd);
3160 EXPORT_SYMBOL(ipmi_unregister_for_cmd);
3161 EXPORT_SYMBOL(ipmi_smi_msg_received);
3162 EXPORT_SYMBOL(ipmi_smi_watchdog_pretimeout);
3163 EXPORT_SYMBOL(ipmi_alloc_smi_msg);
3164 EXPORT_SYMBOL(ipmi_register_all_cmd_rcvr);
3165 EXPORT_SYMBOL(ipmi_unregister_all_cmd_rcvr);
3166 EXPORT_SYMBOL(ipmi_addr_length);
3167 EXPORT_SYMBOL(ipmi_validate_addr);
3168 EXPORT_SYMBOL(ipmi_set_gets_events);
3169 EXPORT_SYMBOL(ipmi_addr_equal);
3170 EXPORT_SYMBOL(ipmi_smi_watcher_register);
3171 EXPORT_SYMBOL(ipmi_smi_watcher_unregister);
3172 EXPORT_SYMBOL(ipmi_set_my_address);
3173 EXPORT_SYMBOL(ipmi_get_my_address);
3174 EXPORT_SYMBOL(ipmi_set_my_LUN);
3175 EXPORT_SYMBOL(ipmi_get_my_LUN);
3176 EXPORT_SYMBOL(ipmi_smi_add_proc_entry);