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/module.h>
35 #include <linux/errno.h>
36 #include <asm/system.h>
37 #include <linux/poll.h>
38 #include <linux/sched.h>
39 #include <linux/seq_file.h>
40 #include <linux/spinlock.h>
41 #include <linux/mutex.h>
42 #include <linux/slab.h>
43 #include <linux/ipmi.h>
44 #include <linux/ipmi_smi.h>
45 #include <linux/notifier.h>
46 #include <linux/init.h>
47 #include <linux/proc_fs.h>
48 #include <linux/rcupdate.h>
49 #include <linux/interrupt.h>
51 #define PFX "IPMI message handler: "
53 #define IPMI_DRIVER_VERSION "39.2"
55 static struct ipmi_recv_msg *ipmi_alloc_recv_msg(void);
56 static int ipmi_init_msghandler(void);
57 static void smi_recv_tasklet(unsigned long);
58 static void handle_new_recv_msgs(ipmi_smi_t intf);
60 static int initialized;
63 static struct proc_dir_entry *proc_ipmi_root;
64 #endif /* CONFIG_PROC_FS */
66 /* Remain in auto-maintenance mode for this amount of time (in ms). */
67 #define IPMI_MAINTENANCE_MODE_TIMEOUT 30000
69 #define MAX_EVENTS_IN_QUEUE 25
72 * Don't let a message sit in a queue forever, always time it with at lest
73 * the max message timer. This is in milliseconds.
75 #define MAX_MSG_TIMEOUT 60000
78 * The main "user" data structure.
81 struct list_head link;
83 /* Set to "0" when the user is destroyed. */
88 /* The upper layer that handles receive messages. */
89 struct ipmi_user_hndl *handler;
92 /* The interface this user is bound to. */
95 /* Does this interface receive IPMI events? */
100 struct list_head link;
108 * This is used to form a linked lised during mass deletion.
109 * Since this is in an RCU list, we cannot use the link above
110 * or change any data until the RCU period completes. So we
111 * use this next variable during mass deletion so we can have
112 * a list and don't have to wait and restart the search on
113 * every individual deletion of a command.
115 struct cmd_rcvr *next;
119 unsigned int inuse : 1;
120 unsigned int broadcast : 1;
122 unsigned long timeout;
123 unsigned long orig_timeout;
124 unsigned int retries_left;
127 * To verify on an incoming send message response that this is
128 * the message that the response is for, we keep a sequence id
129 * and increment it every time we send a message.
134 * This is held so we can properly respond to the message on a
135 * timeout, and it is used to hold the temporary data for
136 * retransmission, too.
138 struct ipmi_recv_msg *recv_msg;
142 * Store the information in a msgid (long) to allow us to find a
143 * sequence table entry from the msgid.
145 #define STORE_SEQ_IN_MSGID(seq, seqid) (((seq&0xff)<<26) | (seqid&0x3ffffff))
147 #define GET_SEQ_FROM_MSGID(msgid, seq, seqid) \
149 seq = ((msgid >> 26) & 0x3f); \
150 seqid = (msgid & 0x3fffff); \
153 #define NEXT_SEQID(seqid) (((seqid) + 1) & 0x3fffff)
155 struct ipmi_channel {
156 unsigned char medium;
157 unsigned char protocol;
160 * My slave address. This is initialized to IPMI_BMC_SLAVE_ADDR,
161 * but may be changed by the user.
163 unsigned char address;
166 * My LUN. This should generally stay the SMS LUN, but just in
172 #ifdef CONFIG_PROC_FS
173 struct ipmi_proc_entry {
175 struct ipmi_proc_entry *next;
180 struct platform_device *dev;
181 struct ipmi_device_id id;
182 unsigned char guid[16];
185 struct kref refcount;
187 /* bmc device attributes */
188 struct device_attribute device_id_attr;
189 struct device_attribute provides_dev_sdrs_attr;
190 struct device_attribute revision_attr;
191 struct device_attribute firmware_rev_attr;
192 struct device_attribute version_attr;
193 struct device_attribute add_dev_support_attr;
194 struct device_attribute manufacturer_id_attr;
195 struct device_attribute product_id_attr;
196 struct device_attribute guid_attr;
197 struct device_attribute aux_firmware_rev_attr;
201 * Various statistics for IPMI, these index stats[] in the ipmi_smi
204 enum ipmi_stat_indexes {
205 /* Commands we got from the user that were invalid. */
206 IPMI_STAT_sent_invalid_commands = 0,
208 /* Commands we sent to the MC. */
209 IPMI_STAT_sent_local_commands,
211 /* Responses from the MC that were delivered to a user. */
212 IPMI_STAT_handled_local_responses,
214 /* Responses from the MC that were not delivered to a user. */
215 IPMI_STAT_unhandled_local_responses,
217 /* Commands we sent out to the IPMB bus. */
218 IPMI_STAT_sent_ipmb_commands,
220 /* Commands sent on the IPMB that had errors on the SEND CMD */
221 IPMI_STAT_sent_ipmb_command_errs,
223 /* Each retransmit increments this count. */
224 IPMI_STAT_retransmitted_ipmb_commands,
227 * When a message times out (runs out of retransmits) this is
230 IPMI_STAT_timed_out_ipmb_commands,
233 * This is like above, but for broadcasts. Broadcasts are
234 * *not* included in the above count (they are expected to
237 IPMI_STAT_timed_out_ipmb_broadcasts,
239 /* Responses I have sent to the IPMB bus. */
240 IPMI_STAT_sent_ipmb_responses,
242 /* The response was delivered to the user. */
243 IPMI_STAT_handled_ipmb_responses,
245 /* The response had invalid data in it. */
246 IPMI_STAT_invalid_ipmb_responses,
248 /* The response didn't have anyone waiting for it. */
249 IPMI_STAT_unhandled_ipmb_responses,
251 /* Commands we sent out to the IPMB bus. */
252 IPMI_STAT_sent_lan_commands,
254 /* Commands sent on the IPMB that had errors on the SEND CMD */
255 IPMI_STAT_sent_lan_command_errs,
257 /* Each retransmit increments this count. */
258 IPMI_STAT_retransmitted_lan_commands,
261 * When a message times out (runs out of retransmits) this is
264 IPMI_STAT_timed_out_lan_commands,
266 /* Responses I have sent to the IPMB bus. */
267 IPMI_STAT_sent_lan_responses,
269 /* The response was delivered to the user. */
270 IPMI_STAT_handled_lan_responses,
272 /* The response had invalid data in it. */
273 IPMI_STAT_invalid_lan_responses,
275 /* The response didn't have anyone waiting for it. */
276 IPMI_STAT_unhandled_lan_responses,
278 /* The command was delivered to the user. */
279 IPMI_STAT_handled_commands,
281 /* The command had invalid data in it. */
282 IPMI_STAT_invalid_commands,
284 /* The command didn't have anyone waiting for it. */
285 IPMI_STAT_unhandled_commands,
287 /* Invalid data in an event. */
288 IPMI_STAT_invalid_events,
290 /* Events that were received with the proper format. */
293 /* Retransmissions on IPMB that failed. */
294 IPMI_STAT_dropped_rexmit_ipmb_commands,
296 /* Retransmissions on LAN that failed. */
297 IPMI_STAT_dropped_rexmit_lan_commands,
299 /* This *must* remain last, add new values above this. */
304 #define IPMI_IPMB_NUM_SEQ 64
305 #define IPMI_MAX_CHANNELS 16
307 /* What interface number are we? */
310 struct kref refcount;
312 /* Used for a list of interfaces. */
313 struct list_head link;
316 * The list of upper layers that are using me. seq_lock
319 struct list_head users;
321 /* Information to supply to users. */
322 unsigned char ipmi_version_major;
323 unsigned char ipmi_version_minor;
325 /* Used for wake ups at startup. */
326 wait_queue_head_t waitq;
328 struct bmc_device *bmc;
333 * This is the lower-layer's sender routine. Note that you
334 * must either be holding the ipmi_interfaces_mutex or be in
335 * an umpreemptible region to use this. You must fetch the
336 * value into a local variable and make sure it is not NULL.
338 struct ipmi_smi_handlers *handlers;
341 #ifdef CONFIG_PROC_FS
342 /* A list of proc entries for this interface. */
343 struct mutex proc_entry_lock;
344 struct ipmi_proc_entry *proc_entries;
347 /* Driver-model device for the system interface. */
348 struct device *si_dev;
351 * A table of sequence numbers for this interface. We use the
352 * sequence numbers for IPMB messages that go out of the
353 * interface to match them up with their responses. A routine
354 * is called periodically to time the items in this list.
357 struct seq_table seq_table[IPMI_IPMB_NUM_SEQ];
361 * Messages queued for delivery. If delivery fails (out of memory
362 * for instance), They will stay in here to be processed later in a
363 * periodic timer interrupt. The tasklet is for handling received
364 * messages directly from the handler.
366 spinlock_t waiting_msgs_lock;
367 struct list_head waiting_msgs;
368 atomic_t watchdog_pretimeouts_to_deliver;
369 struct tasklet_struct recv_tasklet;
372 * The list of command receivers that are registered for commands
375 struct mutex cmd_rcvrs_mutex;
376 struct list_head cmd_rcvrs;
379 * Events that were queues because no one was there to receive
382 spinlock_t events_lock; /* For dealing with event stuff. */
383 struct list_head waiting_events;
384 unsigned int waiting_events_count; /* How many events in queue? */
385 char delivering_events;
386 char event_msg_printed;
389 * The event receiver for my BMC, only really used at panic
390 * shutdown as a place to store this.
392 unsigned char event_receiver;
393 unsigned char event_receiver_lun;
394 unsigned char local_sel_device;
395 unsigned char local_event_generator;
397 /* For handling of maintenance mode. */
398 int maintenance_mode;
399 int maintenance_mode_enable;
400 int auto_maintenance_timeout;
401 spinlock_t maintenance_mode_lock; /* Used in a timer... */
404 * A cheap hack, if this is non-null and a message to an
405 * interface comes in with a NULL user, call this routine with
406 * it. Note that the message will still be freed by the
407 * caller. This only works on the system interface.
409 void (*null_user_handler)(ipmi_smi_t intf, struct ipmi_recv_msg *msg);
412 * When we are scanning the channels for an SMI, this will
413 * tell which channel we are scanning.
417 /* Channel information */
418 struct ipmi_channel channels[IPMI_MAX_CHANNELS];
421 struct proc_dir_entry *proc_dir;
422 char proc_dir_name[10];
424 atomic_t stats[IPMI_NUM_STATS];
427 * run_to_completion duplicate of smb_info, smi_info
428 * and ipmi_serial_info structures. Used to decrease numbers of
429 * parameters passed by "low" level IPMI code.
431 int run_to_completion;
433 #define to_si_intf_from_dev(device) container_of(device, struct ipmi_smi, dev)
436 * The driver model view of the IPMI messaging driver.
438 static struct platform_driver ipmidriver = {
441 .bus = &platform_bus_type
444 static DEFINE_MUTEX(ipmidriver_mutex);
446 static LIST_HEAD(ipmi_interfaces);
447 static DEFINE_MUTEX(ipmi_interfaces_mutex);
450 * List of watchers that want to know when smi's are added and deleted.
452 static LIST_HEAD(smi_watchers);
453 static DEFINE_MUTEX(smi_watchers_mutex);
456 #define ipmi_inc_stat(intf, stat) \
457 atomic_inc(&(intf)->stats[IPMI_STAT_ ## stat])
458 #define ipmi_get_stat(intf, stat) \
459 ((unsigned int) atomic_read(&(intf)->stats[IPMI_STAT_ ## stat]))
461 static int is_lan_addr(struct ipmi_addr *addr)
463 return addr->addr_type == IPMI_LAN_ADDR_TYPE;
466 static int is_ipmb_addr(struct ipmi_addr *addr)
468 return addr->addr_type == IPMI_IPMB_ADDR_TYPE;
471 static int is_ipmb_bcast_addr(struct ipmi_addr *addr)
473 return addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE;
476 static void free_recv_msg_list(struct list_head *q)
478 struct ipmi_recv_msg *msg, *msg2;
480 list_for_each_entry_safe(msg, msg2, q, link) {
481 list_del(&msg->link);
482 ipmi_free_recv_msg(msg);
486 static void free_smi_msg_list(struct list_head *q)
488 struct ipmi_smi_msg *msg, *msg2;
490 list_for_each_entry_safe(msg, msg2, q, link) {
491 list_del(&msg->link);
492 ipmi_free_smi_msg(msg);
496 static void clean_up_interface_data(ipmi_smi_t intf)
499 struct cmd_rcvr *rcvr, *rcvr2;
500 struct list_head list;
502 tasklet_kill(&intf->recv_tasklet);
504 free_smi_msg_list(&intf->waiting_msgs);
505 free_recv_msg_list(&intf->waiting_events);
508 * Wholesale remove all the entries from the list in the
509 * interface and wait for RCU to know that none are in use.
511 mutex_lock(&intf->cmd_rcvrs_mutex);
512 INIT_LIST_HEAD(&list);
513 list_splice_init_rcu(&intf->cmd_rcvrs, &list, synchronize_rcu);
514 mutex_unlock(&intf->cmd_rcvrs_mutex);
516 list_for_each_entry_safe(rcvr, rcvr2, &list, link)
519 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
520 if ((intf->seq_table[i].inuse)
521 && (intf->seq_table[i].recv_msg))
522 ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
526 static void intf_free(struct kref *ref)
528 ipmi_smi_t intf = container_of(ref, struct ipmi_smi, refcount);
530 clean_up_interface_data(intf);
534 struct watcher_entry {
537 struct list_head link;
540 int ipmi_smi_watcher_register(struct ipmi_smi_watcher *watcher)
543 LIST_HEAD(to_deliver);
544 struct watcher_entry *e, *e2;
546 mutex_lock(&smi_watchers_mutex);
548 mutex_lock(&ipmi_interfaces_mutex);
550 /* Build a list of things to deliver. */
551 list_for_each_entry(intf, &ipmi_interfaces, link) {
552 if (intf->intf_num == -1)
554 e = kmalloc(sizeof(*e), GFP_KERNEL);
557 kref_get(&intf->refcount);
559 e->intf_num = intf->intf_num;
560 list_add_tail(&e->link, &to_deliver);
563 /* We will succeed, so add it to the list. */
564 list_add(&watcher->link, &smi_watchers);
566 mutex_unlock(&ipmi_interfaces_mutex);
568 list_for_each_entry_safe(e, e2, &to_deliver, link) {
570 watcher->new_smi(e->intf_num, e->intf->si_dev);
571 kref_put(&e->intf->refcount, intf_free);
575 mutex_unlock(&smi_watchers_mutex);
580 mutex_unlock(&ipmi_interfaces_mutex);
581 mutex_unlock(&smi_watchers_mutex);
582 list_for_each_entry_safe(e, e2, &to_deliver, link) {
584 kref_put(&e->intf->refcount, intf_free);
589 EXPORT_SYMBOL(ipmi_smi_watcher_register);
591 int ipmi_smi_watcher_unregister(struct ipmi_smi_watcher *watcher)
593 mutex_lock(&smi_watchers_mutex);
594 list_del(&(watcher->link));
595 mutex_unlock(&smi_watchers_mutex);
598 EXPORT_SYMBOL(ipmi_smi_watcher_unregister);
601 * Must be called with smi_watchers_mutex held.
604 call_smi_watchers(int i, struct device *dev)
606 struct ipmi_smi_watcher *w;
608 list_for_each_entry(w, &smi_watchers, link) {
609 if (try_module_get(w->owner)) {
611 module_put(w->owner);
617 ipmi_addr_equal(struct ipmi_addr *addr1, struct ipmi_addr *addr2)
619 if (addr1->addr_type != addr2->addr_type)
622 if (addr1->channel != addr2->channel)
625 if (addr1->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
626 struct ipmi_system_interface_addr *smi_addr1
627 = (struct ipmi_system_interface_addr *) addr1;
628 struct ipmi_system_interface_addr *smi_addr2
629 = (struct ipmi_system_interface_addr *) addr2;
630 return (smi_addr1->lun == smi_addr2->lun);
633 if (is_ipmb_addr(addr1) || is_ipmb_bcast_addr(addr1)) {
634 struct ipmi_ipmb_addr *ipmb_addr1
635 = (struct ipmi_ipmb_addr *) addr1;
636 struct ipmi_ipmb_addr *ipmb_addr2
637 = (struct ipmi_ipmb_addr *) addr2;
639 return ((ipmb_addr1->slave_addr == ipmb_addr2->slave_addr)
640 && (ipmb_addr1->lun == ipmb_addr2->lun));
643 if (is_lan_addr(addr1)) {
644 struct ipmi_lan_addr *lan_addr1
645 = (struct ipmi_lan_addr *) addr1;
646 struct ipmi_lan_addr *lan_addr2
647 = (struct ipmi_lan_addr *) addr2;
649 return ((lan_addr1->remote_SWID == lan_addr2->remote_SWID)
650 && (lan_addr1->local_SWID == lan_addr2->local_SWID)
651 && (lan_addr1->session_handle
652 == lan_addr2->session_handle)
653 && (lan_addr1->lun == lan_addr2->lun));
659 int ipmi_validate_addr(struct ipmi_addr *addr, int len)
661 if (len < sizeof(struct ipmi_system_interface_addr))
664 if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
665 if (addr->channel != IPMI_BMC_CHANNEL)
670 if ((addr->channel == IPMI_BMC_CHANNEL)
671 || (addr->channel >= IPMI_MAX_CHANNELS)
672 || (addr->channel < 0))
675 if (is_ipmb_addr(addr) || is_ipmb_bcast_addr(addr)) {
676 if (len < sizeof(struct ipmi_ipmb_addr))
681 if (is_lan_addr(addr)) {
682 if (len < sizeof(struct ipmi_lan_addr))
689 EXPORT_SYMBOL(ipmi_validate_addr);
691 unsigned int ipmi_addr_length(int addr_type)
693 if (addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
694 return sizeof(struct ipmi_system_interface_addr);
696 if ((addr_type == IPMI_IPMB_ADDR_TYPE)
697 || (addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE))
698 return sizeof(struct ipmi_ipmb_addr);
700 if (addr_type == IPMI_LAN_ADDR_TYPE)
701 return sizeof(struct ipmi_lan_addr);
705 EXPORT_SYMBOL(ipmi_addr_length);
707 static void deliver_response(struct ipmi_recv_msg *msg)
710 ipmi_smi_t intf = msg->user_msg_data;
712 /* Special handling for NULL users. */
713 if (intf->null_user_handler) {
714 intf->null_user_handler(intf, msg);
715 ipmi_inc_stat(intf, handled_local_responses);
717 /* No handler, so give up. */
718 ipmi_inc_stat(intf, unhandled_local_responses);
720 ipmi_free_recv_msg(msg);
722 ipmi_user_t user = msg->user;
723 user->handler->ipmi_recv_hndl(msg, user->handler_data);
728 deliver_err_response(struct ipmi_recv_msg *msg, int err)
730 msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
731 msg->msg_data[0] = err;
732 msg->msg.netfn |= 1; /* Convert to a response. */
733 msg->msg.data_len = 1;
734 msg->msg.data = msg->msg_data;
735 deliver_response(msg);
739 * Find the next sequence number not being used and add the given
740 * message with the given timeout to the sequence table. This must be
741 * called with the interface's seq_lock held.
743 static int intf_next_seq(ipmi_smi_t intf,
744 struct ipmi_recv_msg *recv_msg,
745 unsigned long timeout,
754 for (i = intf->curr_seq; (i+1)%IPMI_IPMB_NUM_SEQ != intf->curr_seq;
755 i = (i+1)%IPMI_IPMB_NUM_SEQ) {
756 if (!intf->seq_table[i].inuse)
760 if (!intf->seq_table[i].inuse) {
761 intf->seq_table[i].recv_msg = recv_msg;
764 * Start with the maximum timeout, when the send response
765 * comes in we will start the real timer.
767 intf->seq_table[i].timeout = MAX_MSG_TIMEOUT;
768 intf->seq_table[i].orig_timeout = timeout;
769 intf->seq_table[i].retries_left = retries;
770 intf->seq_table[i].broadcast = broadcast;
771 intf->seq_table[i].inuse = 1;
772 intf->seq_table[i].seqid = NEXT_SEQID(intf->seq_table[i].seqid);
774 *seqid = intf->seq_table[i].seqid;
775 intf->curr_seq = (i+1)%IPMI_IPMB_NUM_SEQ;
784 * Return the receive message for the given sequence number and
785 * release the sequence number so it can be reused. Some other data
786 * is passed in to be sure the message matches up correctly (to help
787 * guard against message coming in after their timeout and the
788 * sequence number being reused).
790 static int intf_find_seq(ipmi_smi_t intf,
795 struct ipmi_addr *addr,
796 struct ipmi_recv_msg **recv_msg)
801 if (seq >= IPMI_IPMB_NUM_SEQ)
804 spin_lock_irqsave(&(intf->seq_lock), flags);
805 if (intf->seq_table[seq].inuse) {
806 struct ipmi_recv_msg *msg = intf->seq_table[seq].recv_msg;
808 if ((msg->addr.channel == channel) && (msg->msg.cmd == cmd)
809 && (msg->msg.netfn == netfn)
810 && (ipmi_addr_equal(addr, &(msg->addr)))) {
812 intf->seq_table[seq].inuse = 0;
816 spin_unlock_irqrestore(&(intf->seq_lock), flags);
822 /* Start the timer for a specific sequence table entry. */
823 static int intf_start_seq_timer(ipmi_smi_t intf,
832 GET_SEQ_FROM_MSGID(msgid, seq, seqid);
834 spin_lock_irqsave(&(intf->seq_lock), flags);
836 * We do this verification because the user can be deleted
837 * while a message is outstanding.
839 if ((intf->seq_table[seq].inuse)
840 && (intf->seq_table[seq].seqid == seqid)) {
841 struct seq_table *ent = &(intf->seq_table[seq]);
842 ent->timeout = ent->orig_timeout;
845 spin_unlock_irqrestore(&(intf->seq_lock), flags);
850 /* Got an error for the send message for a specific sequence number. */
851 static int intf_err_seq(ipmi_smi_t intf,
859 struct ipmi_recv_msg *msg = NULL;
862 GET_SEQ_FROM_MSGID(msgid, seq, seqid);
864 spin_lock_irqsave(&(intf->seq_lock), flags);
866 * We do this verification because the user can be deleted
867 * while a message is outstanding.
869 if ((intf->seq_table[seq].inuse)
870 && (intf->seq_table[seq].seqid == seqid)) {
871 struct seq_table *ent = &(intf->seq_table[seq]);
877 spin_unlock_irqrestore(&(intf->seq_lock), flags);
880 deliver_err_response(msg, err);
886 int ipmi_create_user(unsigned int if_num,
887 struct ipmi_user_hndl *handler,
892 ipmi_user_t new_user;
897 * There is no module usecount here, because it's not
898 * required. Since this can only be used by and called from
899 * other modules, they will implicitly use this module, and
900 * thus this can't be removed unless the other modules are
908 * Make sure the driver is actually initialized, this handles
909 * problems with initialization order.
912 rv = ipmi_init_msghandler();
917 * The init code doesn't return an error if it was turned
918 * off, but it won't initialize. Check that.
924 new_user = kmalloc(sizeof(*new_user), GFP_KERNEL);
928 mutex_lock(&ipmi_interfaces_mutex);
929 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
930 if (intf->intf_num == if_num)
933 /* Not found, return an error */
938 /* Note that each existing user holds a refcount to the interface. */
939 kref_get(&intf->refcount);
941 kref_init(&new_user->refcount);
942 new_user->handler = handler;
943 new_user->handler_data = handler_data;
944 new_user->intf = intf;
945 new_user->gets_events = 0;
947 if (!try_module_get(intf->handlers->owner)) {
952 if (intf->handlers->inc_usecount) {
953 rv = intf->handlers->inc_usecount(intf->send_info);
955 module_put(intf->handlers->owner);
961 * Hold the lock so intf->handlers is guaranteed to be good
964 mutex_unlock(&ipmi_interfaces_mutex);
967 spin_lock_irqsave(&intf->seq_lock, flags);
968 list_add_rcu(&new_user->link, &intf->users);
969 spin_unlock_irqrestore(&intf->seq_lock, flags);
974 kref_put(&intf->refcount, intf_free);
976 mutex_unlock(&ipmi_interfaces_mutex);
980 EXPORT_SYMBOL(ipmi_create_user);
982 int ipmi_get_smi_info(int if_num, struct ipmi_smi_info *data)
986 struct ipmi_smi_handlers *handlers;
988 mutex_lock(&ipmi_interfaces_mutex);
989 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
990 if (intf->intf_num == if_num)
993 /* Not found, return an error */
995 mutex_unlock(&ipmi_interfaces_mutex);
999 handlers = intf->handlers;
1001 if (handlers->get_smi_info)
1002 rv = handlers->get_smi_info(intf->send_info, data);
1003 mutex_unlock(&ipmi_interfaces_mutex);
1007 EXPORT_SYMBOL(ipmi_get_smi_info);
1009 static void free_user(struct kref *ref)
1011 ipmi_user_t user = container_of(ref, struct ipmi_user, refcount);
1015 int ipmi_destroy_user(ipmi_user_t user)
1017 ipmi_smi_t intf = user->intf;
1019 unsigned long flags;
1020 struct cmd_rcvr *rcvr;
1021 struct cmd_rcvr *rcvrs = NULL;
1025 /* Remove the user from the interface's sequence table. */
1026 spin_lock_irqsave(&intf->seq_lock, flags);
1027 list_del_rcu(&user->link);
1029 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
1030 if (intf->seq_table[i].inuse
1031 && (intf->seq_table[i].recv_msg->user == user)) {
1032 intf->seq_table[i].inuse = 0;
1033 ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
1036 spin_unlock_irqrestore(&intf->seq_lock, flags);
1039 * Remove the user from the command receiver's table. First
1040 * we build a list of everything (not using the standard link,
1041 * since other things may be using it till we do
1042 * synchronize_rcu()) then free everything in that list.
1044 mutex_lock(&intf->cmd_rcvrs_mutex);
1045 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
1046 if (rcvr->user == user) {
1047 list_del_rcu(&rcvr->link);
1052 mutex_unlock(&intf->cmd_rcvrs_mutex);
1060 mutex_lock(&ipmi_interfaces_mutex);
1061 if (intf->handlers) {
1062 module_put(intf->handlers->owner);
1063 if (intf->handlers->dec_usecount)
1064 intf->handlers->dec_usecount(intf->send_info);
1066 mutex_unlock(&ipmi_interfaces_mutex);
1068 kref_put(&intf->refcount, intf_free);
1070 kref_put(&user->refcount, free_user);
1074 EXPORT_SYMBOL(ipmi_destroy_user);
1076 void ipmi_get_version(ipmi_user_t user,
1077 unsigned char *major,
1078 unsigned char *minor)
1080 *major = user->intf->ipmi_version_major;
1081 *minor = user->intf->ipmi_version_minor;
1083 EXPORT_SYMBOL(ipmi_get_version);
1085 int ipmi_set_my_address(ipmi_user_t user,
1086 unsigned int channel,
1087 unsigned char address)
1089 if (channel >= IPMI_MAX_CHANNELS)
1091 user->intf->channels[channel].address = address;
1094 EXPORT_SYMBOL(ipmi_set_my_address);
1096 int ipmi_get_my_address(ipmi_user_t user,
1097 unsigned int channel,
1098 unsigned char *address)
1100 if (channel >= IPMI_MAX_CHANNELS)
1102 *address = user->intf->channels[channel].address;
1105 EXPORT_SYMBOL(ipmi_get_my_address);
1107 int ipmi_set_my_LUN(ipmi_user_t user,
1108 unsigned int channel,
1111 if (channel >= IPMI_MAX_CHANNELS)
1113 user->intf->channels[channel].lun = LUN & 0x3;
1116 EXPORT_SYMBOL(ipmi_set_my_LUN);
1118 int ipmi_get_my_LUN(ipmi_user_t user,
1119 unsigned int channel,
1120 unsigned char *address)
1122 if (channel >= IPMI_MAX_CHANNELS)
1124 *address = user->intf->channels[channel].lun;
1127 EXPORT_SYMBOL(ipmi_get_my_LUN);
1129 int ipmi_get_maintenance_mode(ipmi_user_t user)
1132 unsigned long flags;
1134 spin_lock_irqsave(&user->intf->maintenance_mode_lock, flags);
1135 mode = user->intf->maintenance_mode;
1136 spin_unlock_irqrestore(&user->intf->maintenance_mode_lock, flags);
1140 EXPORT_SYMBOL(ipmi_get_maintenance_mode);
1142 static void maintenance_mode_update(ipmi_smi_t intf)
1144 if (intf->handlers->set_maintenance_mode)
1145 intf->handlers->set_maintenance_mode(
1146 intf->send_info, intf->maintenance_mode_enable);
1149 int ipmi_set_maintenance_mode(ipmi_user_t user, int mode)
1152 unsigned long flags;
1153 ipmi_smi_t intf = user->intf;
1155 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
1156 if (intf->maintenance_mode != mode) {
1158 case IPMI_MAINTENANCE_MODE_AUTO:
1159 intf->maintenance_mode = mode;
1160 intf->maintenance_mode_enable
1161 = (intf->auto_maintenance_timeout > 0);
1164 case IPMI_MAINTENANCE_MODE_OFF:
1165 intf->maintenance_mode = mode;
1166 intf->maintenance_mode_enable = 0;
1169 case IPMI_MAINTENANCE_MODE_ON:
1170 intf->maintenance_mode = mode;
1171 intf->maintenance_mode_enable = 1;
1179 maintenance_mode_update(intf);
1182 spin_unlock_irqrestore(&intf->maintenance_mode_lock, flags);
1186 EXPORT_SYMBOL(ipmi_set_maintenance_mode);
1188 int ipmi_set_gets_events(ipmi_user_t user, int val)
1190 unsigned long flags;
1191 ipmi_smi_t intf = user->intf;
1192 struct ipmi_recv_msg *msg, *msg2;
1193 struct list_head msgs;
1195 INIT_LIST_HEAD(&msgs);
1197 spin_lock_irqsave(&intf->events_lock, flags);
1198 user->gets_events = val;
1200 if (intf->delivering_events)
1202 * Another thread is delivering events for this, so
1203 * let it handle any new events.
1207 /* Deliver any queued events. */
1208 while (user->gets_events && !list_empty(&intf->waiting_events)) {
1209 list_for_each_entry_safe(msg, msg2, &intf->waiting_events, link)
1210 list_move_tail(&msg->link, &msgs);
1211 intf->waiting_events_count = 0;
1212 if (intf->event_msg_printed) {
1213 printk(KERN_WARNING PFX "Event queue no longer"
1215 intf->event_msg_printed = 0;
1218 intf->delivering_events = 1;
1219 spin_unlock_irqrestore(&intf->events_lock, flags);
1221 list_for_each_entry_safe(msg, msg2, &msgs, link) {
1223 kref_get(&user->refcount);
1224 deliver_response(msg);
1227 spin_lock_irqsave(&intf->events_lock, flags);
1228 intf->delivering_events = 0;
1232 spin_unlock_irqrestore(&intf->events_lock, flags);
1236 EXPORT_SYMBOL(ipmi_set_gets_events);
1238 static struct cmd_rcvr *find_cmd_rcvr(ipmi_smi_t intf,
1239 unsigned char netfn,
1243 struct cmd_rcvr *rcvr;
1245 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
1246 if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)
1247 && (rcvr->chans & (1 << chan)))
1253 static int is_cmd_rcvr_exclusive(ipmi_smi_t intf,
1254 unsigned char netfn,
1258 struct cmd_rcvr *rcvr;
1260 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
1261 if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)
1262 && (rcvr->chans & chans))
1268 int ipmi_register_for_cmd(ipmi_user_t user,
1269 unsigned char netfn,
1273 ipmi_smi_t intf = user->intf;
1274 struct cmd_rcvr *rcvr;
1278 rcvr = kmalloc(sizeof(*rcvr), GFP_KERNEL);
1282 rcvr->netfn = netfn;
1283 rcvr->chans = chans;
1286 mutex_lock(&intf->cmd_rcvrs_mutex);
1287 /* Make sure the command/netfn is not already registered. */
1288 if (!is_cmd_rcvr_exclusive(intf, netfn, cmd, chans)) {
1293 list_add_rcu(&rcvr->link, &intf->cmd_rcvrs);
1296 mutex_unlock(&intf->cmd_rcvrs_mutex);
1302 EXPORT_SYMBOL(ipmi_register_for_cmd);
1304 int ipmi_unregister_for_cmd(ipmi_user_t user,
1305 unsigned char netfn,
1309 ipmi_smi_t intf = user->intf;
1310 struct cmd_rcvr *rcvr;
1311 struct cmd_rcvr *rcvrs = NULL;
1312 int i, rv = -ENOENT;
1314 mutex_lock(&intf->cmd_rcvrs_mutex);
1315 for (i = 0; i < IPMI_NUM_CHANNELS; i++) {
1316 if (((1 << i) & chans) == 0)
1318 rcvr = find_cmd_rcvr(intf, netfn, cmd, i);
1321 if (rcvr->user == user) {
1323 rcvr->chans &= ~chans;
1324 if (rcvr->chans == 0) {
1325 list_del_rcu(&rcvr->link);
1331 mutex_unlock(&intf->cmd_rcvrs_mutex);
1340 EXPORT_SYMBOL(ipmi_unregister_for_cmd);
1342 static unsigned char
1343 ipmb_checksum(unsigned char *data, int size)
1345 unsigned char csum = 0;
1347 for (; size > 0; size--, data++)
1353 static inline void format_ipmb_msg(struct ipmi_smi_msg *smi_msg,
1354 struct kernel_ipmi_msg *msg,
1355 struct ipmi_ipmb_addr *ipmb_addr,
1357 unsigned char ipmb_seq,
1359 unsigned char source_address,
1360 unsigned char source_lun)
1364 /* Format the IPMB header data. */
1365 smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
1366 smi_msg->data[1] = IPMI_SEND_MSG_CMD;
1367 smi_msg->data[2] = ipmb_addr->channel;
1369 smi_msg->data[3] = 0;
1370 smi_msg->data[i+3] = ipmb_addr->slave_addr;
1371 smi_msg->data[i+4] = (msg->netfn << 2) | (ipmb_addr->lun & 0x3);
1372 smi_msg->data[i+5] = ipmb_checksum(&(smi_msg->data[i+3]), 2);
1373 smi_msg->data[i+6] = source_address;
1374 smi_msg->data[i+7] = (ipmb_seq << 2) | source_lun;
1375 smi_msg->data[i+8] = msg->cmd;
1377 /* Now tack on the data to the message. */
1378 if (msg->data_len > 0)
1379 memcpy(&(smi_msg->data[i+9]), msg->data,
1381 smi_msg->data_size = msg->data_len + 9;
1383 /* Now calculate the checksum and tack it on. */
1384 smi_msg->data[i+smi_msg->data_size]
1385 = ipmb_checksum(&(smi_msg->data[i+6]),
1386 smi_msg->data_size-6);
1389 * Add on the checksum size and the offset from the
1392 smi_msg->data_size += 1 + i;
1394 smi_msg->msgid = msgid;
1397 static inline void format_lan_msg(struct ipmi_smi_msg *smi_msg,
1398 struct kernel_ipmi_msg *msg,
1399 struct ipmi_lan_addr *lan_addr,
1401 unsigned char ipmb_seq,
1402 unsigned char source_lun)
1404 /* Format the IPMB header data. */
1405 smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
1406 smi_msg->data[1] = IPMI_SEND_MSG_CMD;
1407 smi_msg->data[2] = lan_addr->channel;
1408 smi_msg->data[3] = lan_addr->session_handle;
1409 smi_msg->data[4] = lan_addr->remote_SWID;
1410 smi_msg->data[5] = (msg->netfn << 2) | (lan_addr->lun & 0x3);
1411 smi_msg->data[6] = ipmb_checksum(&(smi_msg->data[4]), 2);
1412 smi_msg->data[7] = lan_addr->local_SWID;
1413 smi_msg->data[8] = (ipmb_seq << 2) | source_lun;
1414 smi_msg->data[9] = msg->cmd;
1416 /* Now tack on the data to the message. */
1417 if (msg->data_len > 0)
1418 memcpy(&(smi_msg->data[10]), msg->data,
1420 smi_msg->data_size = msg->data_len + 10;
1422 /* Now calculate the checksum and tack it on. */
1423 smi_msg->data[smi_msg->data_size]
1424 = ipmb_checksum(&(smi_msg->data[7]),
1425 smi_msg->data_size-7);
1428 * Add on the checksum size and the offset from the
1431 smi_msg->data_size += 1;
1433 smi_msg->msgid = msgid;
1437 * Separate from ipmi_request so that the user does not have to be
1438 * supplied in certain circumstances (mainly at panic time). If
1439 * messages are supplied, they will be freed, even if an error
1442 static int i_ipmi_request(ipmi_user_t user,
1444 struct ipmi_addr *addr,
1446 struct kernel_ipmi_msg *msg,
1447 void *user_msg_data,
1449 struct ipmi_recv_msg *supplied_recv,
1451 unsigned char source_address,
1452 unsigned char source_lun,
1454 unsigned int retry_time_ms)
1457 struct ipmi_smi_msg *smi_msg;
1458 struct ipmi_recv_msg *recv_msg;
1459 unsigned long flags;
1460 struct ipmi_smi_handlers *handlers;
1464 recv_msg = supplied_recv;
1466 recv_msg = ipmi_alloc_recv_msg();
1467 if (recv_msg == NULL)
1470 recv_msg->user_msg_data = user_msg_data;
1473 smi_msg = (struct ipmi_smi_msg *) supplied_smi;
1475 smi_msg = ipmi_alloc_smi_msg();
1476 if (smi_msg == NULL) {
1477 ipmi_free_recv_msg(recv_msg);
1483 handlers = intf->handlers;
1489 recv_msg->user = user;
1491 kref_get(&user->refcount);
1492 recv_msg->msgid = msgid;
1494 * Store the message to send in the receive message so timeout
1495 * responses can get the proper response data.
1497 recv_msg->msg = *msg;
1499 if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
1500 struct ipmi_system_interface_addr *smi_addr;
1502 if (msg->netfn & 1) {
1503 /* Responses are not allowed to the SMI. */
1508 smi_addr = (struct ipmi_system_interface_addr *) addr;
1509 if (smi_addr->lun > 3) {
1510 ipmi_inc_stat(intf, sent_invalid_commands);
1515 memcpy(&recv_msg->addr, smi_addr, sizeof(*smi_addr));
1517 if ((msg->netfn == IPMI_NETFN_APP_REQUEST)
1518 && ((msg->cmd == IPMI_SEND_MSG_CMD)
1519 || (msg->cmd == IPMI_GET_MSG_CMD)
1520 || (msg->cmd == IPMI_READ_EVENT_MSG_BUFFER_CMD))) {
1522 * We don't let the user do these, since we manage
1523 * the sequence numbers.
1525 ipmi_inc_stat(intf, sent_invalid_commands);
1530 if (((msg->netfn == IPMI_NETFN_APP_REQUEST)
1531 && ((msg->cmd == IPMI_COLD_RESET_CMD)
1532 || (msg->cmd == IPMI_WARM_RESET_CMD)))
1533 || (msg->netfn == IPMI_NETFN_FIRMWARE_REQUEST)) {
1534 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
1535 intf->auto_maintenance_timeout
1536 = IPMI_MAINTENANCE_MODE_TIMEOUT;
1537 if (!intf->maintenance_mode
1538 && !intf->maintenance_mode_enable) {
1539 intf->maintenance_mode_enable = 1;
1540 maintenance_mode_update(intf);
1542 spin_unlock_irqrestore(&intf->maintenance_mode_lock,
1546 if ((msg->data_len + 2) > IPMI_MAX_MSG_LENGTH) {
1547 ipmi_inc_stat(intf, sent_invalid_commands);
1552 smi_msg->data[0] = (msg->netfn << 2) | (smi_addr->lun & 0x3);
1553 smi_msg->data[1] = msg->cmd;
1554 smi_msg->msgid = msgid;
1555 smi_msg->user_data = recv_msg;
1556 if (msg->data_len > 0)
1557 memcpy(&(smi_msg->data[2]), msg->data, msg->data_len);
1558 smi_msg->data_size = msg->data_len + 2;
1559 ipmi_inc_stat(intf, sent_local_commands);
1560 } else if (is_ipmb_addr(addr) || is_ipmb_bcast_addr(addr)) {
1561 struct ipmi_ipmb_addr *ipmb_addr;
1562 unsigned char ipmb_seq;
1566 if (addr->channel >= IPMI_MAX_CHANNELS) {
1567 ipmi_inc_stat(intf, sent_invalid_commands);
1572 if (intf->channels[addr->channel].medium
1573 != IPMI_CHANNEL_MEDIUM_IPMB) {
1574 ipmi_inc_stat(intf, sent_invalid_commands);
1580 if (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE)
1581 retries = 0; /* Don't retry broadcasts. */
1585 if (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE) {
1587 * Broadcasts add a zero at the beginning of the
1588 * message, but otherwise is the same as an IPMB
1591 addr->addr_type = IPMI_IPMB_ADDR_TYPE;
1596 /* Default to 1 second retries. */
1597 if (retry_time_ms == 0)
1598 retry_time_ms = 1000;
1601 * 9 for the header and 1 for the checksum, plus
1602 * possibly one for the broadcast.
1604 if ((msg->data_len + 10 + broadcast) > IPMI_MAX_MSG_LENGTH) {
1605 ipmi_inc_stat(intf, sent_invalid_commands);
1610 ipmb_addr = (struct ipmi_ipmb_addr *) addr;
1611 if (ipmb_addr->lun > 3) {
1612 ipmi_inc_stat(intf, sent_invalid_commands);
1617 memcpy(&recv_msg->addr, ipmb_addr, sizeof(*ipmb_addr));
1619 if (recv_msg->msg.netfn & 0x1) {
1621 * It's a response, so use the user's sequence
1624 ipmi_inc_stat(intf, sent_ipmb_responses);
1625 format_ipmb_msg(smi_msg, msg, ipmb_addr, msgid,
1627 source_address, source_lun);
1630 * Save the receive message so we can use it
1631 * to deliver the response.
1633 smi_msg->user_data = recv_msg;
1635 /* It's a command, so get a sequence for it. */
1637 spin_lock_irqsave(&(intf->seq_lock), flags);
1640 * Create a sequence number with a 1 second
1641 * timeout and 4 retries.
1643 rv = intf_next_seq(intf,
1652 * We have used up all the sequence numbers,
1653 * probably, so abort.
1655 spin_unlock_irqrestore(&(intf->seq_lock),
1660 ipmi_inc_stat(intf, sent_ipmb_commands);
1663 * Store the sequence number in the message,
1664 * so that when the send message response
1665 * comes back we can start the timer.
1667 format_ipmb_msg(smi_msg, msg, ipmb_addr,
1668 STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
1669 ipmb_seq, broadcast,
1670 source_address, source_lun);
1673 * Copy the message into the recv message data, so we
1674 * can retransmit it later if necessary.
1676 memcpy(recv_msg->msg_data, smi_msg->data,
1677 smi_msg->data_size);
1678 recv_msg->msg.data = recv_msg->msg_data;
1679 recv_msg->msg.data_len = smi_msg->data_size;
1682 * We don't unlock until here, because we need
1683 * to copy the completed message into the
1684 * recv_msg before we release the lock.
1685 * Otherwise, race conditions may bite us. I
1686 * know that's pretty paranoid, but I prefer
1689 spin_unlock_irqrestore(&(intf->seq_lock), flags);
1691 } else if (is_lan_addr(addr)) {
1692 struct ipmi_lan_addr *lan_addr;
1693 unsigned char ipmb_seq;
1696 if (addr->channel >= IPMI_MAX_CHANNELS) {
1697 ipmi_inc_stat(intf, sent_invalid_commands);
1702 if ((intf->channels[addr->channel].medium
1703 != IPMI_CHANNEL_MEDIUM_8023LAN)
1704 && (intf->channels[addr->channel].medium
1705 != IPMI_CHANNEL_MEDIUM_ASYNC)) {
1706 ipmi_inc_stat(intf, sent_invalid_commands);
1713 /* Default to 1 second retries. */
1714 if (retry_time_ms == 0)
1715 retry_time_ms = 1000;
1717 /* 11 for the header and 1 for the checksum. */
1718 if ((msg->data_len + 12) > IPMI_MAX_MSG_LENGTH) {
1719 ipmi_inc_stat(intf, sent_invalid_commands);
1724 lan_addr = (struct ipmi_lan_addr *) addr;
1725 if (lan_addr->lun > 3) {
1726 ipmi_inc_stat(intf, sent_invalid_commands);
1731 memcpy(&recv_msg->addr, lan_addr, sizeof(*lan_addr));
1733 if (recv_msg->msg.netfn & 0x1) {
1735 * It's a response, so use the user's sequence
1738 ipmi_inc_stat(intf, sent_lan_responses);
1739 format_lan_msg(smi_msg, msg, lan_addr, msgid,
1743 * Save the receive message so we can use it
1744 * to deliver the response.
1746 smi_msg->user_data = recv_msg;
1748 /* It's a command, so get a sequence for it. */
1750 spin_lock_irqsave(&(intf->seq_lock), flags);
1753 * Create a sequence number with a 1 second
1754 * timeout and 4 retries.
1756 rv = intf_next_seq(intf,
1765 * We have used up all the sequence numbers,
1766 * probably, so abort.
1768 spin_unlock_irqrestore(&(intf->seq_lock),
1773 ipmi_inc_stat(intf, sent_lan_commands);
1776 * Store the sequence number in the message,
1777 * so that when the send message response
1778 * comes back we can start the timer.
1780 format_lan_msg(smi_msg, msg, lan_addr,
1781 STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
1782 ipmb_seq, source_lun);
1785 * Copy the message into the recv message data, so we
1786 * can retransmit it later if necessary.
1788 memcpy(recv_msg->msg_data, smi_msg->data,
1789 smi_msg->data_size);
1790 recv_msg->msg.data = recv_msg->msg_data;
1791 recv_msg->msg.data_len = smi_msg->data_size;
1794 * We don't unlock until here, because we need
1795 * to copy the completed message into the
1796 * recv_msg before we release the lock.
1797 * Otherwise, race conditions may bite us. I
1798 * know that's pretty paranoid, but I prefer
1801 spin_unlock_irqrestore(&(intf->seq_lock), flags);
1804 /* Unknown address type. */
1805 ipmi_inc_stat(intf, sent_invalid_commands);
1813 for (m = 0; m < smi_msg->data_size; m++)
1814 printk(" %2.2x", smi_msg->data[m]);
1819 handlers->sender(intf->send_info, smi_msg, priority);
1826 ipmi_free_smi_msg(smi_msg);
1827 ipmi_free_recv_msg(recv_msg);
1831 static int check_addr(ipmi_smi_t intf,
1832 struct ipmi_addr *addr,
1833 unsigned char *saddr,
1836 if (addr->channel >= IPMI_MAX_CHANNELS)
1838 *lun = intf->channels[addr->channel].lun;
1839 *saddr = intf->channels[addr->channel].address;
1843 int ipmi_request_settime(ipmi_user_t user,
1844 struct ipmi_addr *addr,
1846 struct kernel_ipmi_msg *msg,
1847 void *user_msg_data,
1850 unsigned int retry_time_ms)
1852 unsigned char saddr, lun;
1857 rv = check_addr(user->intf, addr, &saddr, &lun);
1860 return i_ipmi_request(user,
1873 EXPORT_SYMBOL(ipmi_request_settime);
1875 int ipmi_request_supply_msgs(ipmi_user_t user,
1876 struct ipmi_addr *addr,
1878 struct kernel_ipmi_msg *msg,
1879 void *user_msg_data,
1881 struct ipmi_recv_msg *supplied_recv,
1884 unsigned char saddr, lun;
1889 rv = check_addr(user->intf, addr, &saddr, &lun);
1892 return i_ipmi_request(user,
1905 EXPORT_SYMBOL(ipmi_request_supply_msgs);
1907 #ifdef CONFIG_PROC_FS
1908 static int smi_ipmb_proc_show(struct seq_file *m, void *v)
1910 ipmi_smi_t intf = m->private;
1913 seq_printf(m, "%x", intf->channels[0].address);
1914 for (i = 1; i < IPMI_MAX_CHANNELS; i++)
1915 seq_printf(m, " %x", intf->channels[i].address);
1916 return seq_putc(m, '\n');
1919 static int smi_ipmb_proc_open(struct inode *inode, struct file *file)
1921 return single_open(file, smi_ipmb_proc_show, PDE(inode)->data);
1924 static const struct file_operations smi_ipmb_proc_ops = {
1925 .open = smi_ipmb_proc_open,
1927 .llseek = seq_lseek,
1928 .release = single_release,
1931 static int smi_version_proc_show(struct seq_file *m, void *v)
1933 ipmi_smi_t intf = m->private;
1935 return seq_printf(m, "%u.%u\n",
1936 ipmi_version_major(&intf->bmc->id),
1937 ipmi_version_minor(&intf->bmc->id));
1940 static int smi_version_proc_open(struct inode *inode, struct file *file)
1942 return single_open(file, smi_version_proc_show, PDE(inode)->data);
1945 static const struct file_operations smi_version_proc_ops = {
1946 .open = smi_version_proc_open,
1948 .llseek = seq_lseek,
1949 .release = single_release,
1952 static int smi_stats_proc_show(struct seq_file *m, void *v)
1954 ipmi_smi_t intf = m->private;
1956 seq_printf(m, "sent_invalid_commands: %u\n",
1957 ipmi_get_stat(intf, sent_invalid_commands));
1958 seq_printf(m, "sent_local_commands: %u\n",
1959 ipmi_get_stat(intf, sent_local_commands));
1960 seq_printf(m, "handled_local_responses: %u\n",
1961 ipmi_get_stat(intf, handled_local_responses));
1962 seq_printf(m, "unhandled_local_responses: %u\n",
1963 ipmi_get_stat(intf, unhandled_local_responses));
1964 seq_printf(m, "sent_ipmb_commands: %u\n",
1965 ipmi_get_stat(intf, sent_ipmb_commands));
1966 seq_printf(m, "sent_ipmb_command_errs: %u\n",
1967 ipmi_get_stat(intf, sent_ipmb_command_errs));
1968 seq_printf(m, "retransmitted_ipmb_commands: %u\n",
1969 ipmi_get_stat(intf, retransmitted_ipmb_commands));
1970 seq_printf(m, "timed_out_ipmb_commands: %u\n",
1971 ipmi_get_stat(intf, timed_out_ipmb_commands));
1972 seq_printf(m, "timed_out_ipmb_broadcasts: %u\n",
1973 ipmi_get_stat(intf, timed_out_ipmb_broadcasts));
1974 seq_printf(m, "sent_ipmb_responses: %u\n",
1975 ipmi_get_stat(intf, sent_ipmb_responses));
1976 seq_printf(m, "handled_ipmb_responses: %u\n",
1977 ipmi_get_stat(intf, handled_ipmb_responses));
1978 seq_printf(m, "invalid_ipmb_responses: %u\n",
1979 ipmi_get_stat(intf, invalid_ipmb_responses));
1980 seq_printf(m, "unhandled_ipmb_responses: %u\n",
1981 ipmi_get_stat(intf, unhandled_ipmb_responses));
1982 seq_printf(m, "sent_lan_commands: %u\n",
1983 ipmi_get_stat(intf, sent_lan_commands));
1984 seq_printf(m, "sent_lan_command_errs: %u\n",
1985 ipmi_get_stat(intf, sent_lan_command_errs));
1986 seq_printf(m, "retransmitted_lan_commands: %u\n",
1987 ipmi_get_stat(intf, retransmitted_lan_commands));
1988 seq_printf(m, "timed_out_lan_commands: %u\n",
1989 ipmi_get_stat(intf, timed_out_lan_commands));
1990 seq_printf(m, "sent_lan_responses: %u\n",
1991 ipmi_get_stat(intf, sent_lan_responses));
1992 seq_printf(m, "handled_lan_responses: %u\n",
1993 ipmi_get_stat(intf, handled_lan_responses));
1994 seq_printf(m, "invalid_lan_responses: %u\n",
1995 ipmi_get_stat(intf, invalid_lan_responses));
1996 seq_printf(m, "unhandled_lan_responses: %u\n",
1997 ipmi_get_stat(intf, unhandled_lan_responses));
1998 seq_printf(m, "handled_commands: %u\n",
1999 ipmi_get_stat(intf, handled_commands));
2000 seq_printf(m, "invalid_commands: %u\n",
2001 ipmi_get_stat(intf, invalid_commands));
2002 seq_printf(m, "unhandled_commands: %u\n",
2003 ipmi_get_stat(intf, unhandled_commands));
2004 seq_printf(m, "invalid_events: %u\n",
2005 ipmi_get_stat(intf, invalid_events));
2006 seq_printf(m, "events: %u\n",
2007 ipmi_get_stat(intf, events));
2008 seq_printf(m, "failed rexmit LAN msgs: %u\n",
2009 ipmi_get_stat(intf, dropped_rexmit_lan_commands));
2010 seq_printf(m, "failed rexmit IPMB msgs: %u\n",
2011 ipmi_get_stat(intf, dropped_rexmit_ipmb_commands));
2015 static int smi_stats_proc_open(struct inode *inode, struct file *file)
2017 return single_open(file, smi_stats_proc_show, PDE(inode)->data);
2020 static const struct file_operations smi_stats_proc_ops = {
2021 .open = smi_stats_proc_open,
2023 .llseek = seq_lseek,
2024 .release = single_release,
2026 #endif /* CONFIG_PROC_FS */
2028 int ipmi_smi_add_proc_entry(ipmi_smi_t smi, char *name,
2029 const struct file_operations *proc_ops,
2033 #ifdef CONFIG_PROC_FS
2034 struct proc_dir_entry *file;
2035 struct ipmi_proc_entry *entry;
2037 /* Create a list element. */
2038 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2041 entry->name = kmalloc(strlen(name)+1, GFP_KERNEL);
2046 strcpy(entry->name, name);
2048 file = proc_create_data(name, 0, smi->proc_dir, proc_ops, data);
2054 mutex_lock(&smi->proc_entry_lock);
2055 /* Stick it on the list. */
2056 entry->next = smi->proc_entries;
2057 smi->proc_entries = entry;
2058 mutex_unlock(&smi->proc_entry_lock);
2060 #endif /* CONFIG_PROC_FS */
2064 EXPORT_SYMBOL(ipmi_smi_add_proc_entry);
2066 static int add_proc_entries(ipmi_smi_t smi, int num)
2070 #ifdef CONFIG_PROC_FS
2071 sprintf(smi->proc_dir_name, "%d", num);
2072 smi->proc_dir = proc_mkdir(smi->proc_dir_name, proc_ipmi_root);
2077 rv = ipmi_smi_add_proc_entry(smi, "stats",
2078 &smi_stats_proc_ops,
2082 rv = ipmi_smi_add_proc_entry(smi, "ipmb",
2087 rv = ipmi_smi_add_proc_entry(smi, "version",
2088 &smi_version_proc_ops,
2090 #endif /* CONFIG_PROC_FS */
2095 static void remove_proc_entries(ipmi_smi_t smi)
2097 #ifdef CONFIG_PROC_FS
2098 struct ipmi_proc_entry *entry;
2100 mutex_lock(&smi->proc_entry_lock);
2101 while (smi->proc_entries) {
2102 entry = smi->proc_entries;
2103 smi->proc_entries = entry->next;
2105 remove_proc_entry(entry->name, smi->proc_dir);
2109 mutex_unlock(&smi->proc_entry_lock);
2110 remove_proc_entry(smi->proc_dir_name, proc_ipmi_root);
2111 #endif /* CONFIG_PROC_FS */
2114 static int __find_bmc_guid(struct device *dev, void *data)
2116 unsigned char *id = data;
2117 struct bmc_device *bmc = dev_get_drvdata(dev);
2118 return memcmp(bmc->guid, id, 16) == 0;
2121 static struct bmc_device *ipmi_find_bmc_guid(struct device_driver *drv,
2122 unsigned char *guid)
2126 dev = driver_find_device(drv, NULL, guid, __find_bmc_guid);
2128 return dev_get_drvdata(dev);
2133 struct prod_dev_id {
2134 unsigned int product_id;
2135 unsigned char device_id;
2138 static int __find_bmc_prod_dev_id(struct device *dev, void *data)
2140 struct prod_dev_id *id = data;
2141 struct bmc_device *bmc = dev_get_drvdata(dev);
2143 return (bmc->id.product_id == id->product_id
2144 && bmc->id.device_id == id->device_id);
2147 static struct bmc_device *ipmi_find_bmc_prod_dev_id(
2148 struct device_driver *drv,
2149 unsigned int product_id, unsigned char device_id)
2151 struct prod_dev_id id = {
2152 .product_id = product_id,
2153 .device_id = device_id,
2157 dev = driver_find_device(drv, NULL, &id, __find_bmc_prod_dev_id);
2159 return dev_get_drvdata(dev);
2164 static ssize_t device_id_show(struct device *dev,
2165 struct device_attribute *attr,
2168 struct bmc_device *bmc = dev_get_drvdata(dev);
2170 return snprintf(buf, 10, "%u\n", bmc->id.device_id);
2173 static ssize_t provides_dev_sdrs_show(struct device *dev,
2174 struct device_attribute *attr,
2177 struct bmc_device *bmc = dev_get_drvdata(dev);
2179 return snprintf(buf, 10, "%u\n",
2180 (bmc->id.device_revision & 0x80) >> 7);
2183 static ssize_t revision_show(struct device *dev, struct device_attribute *attr,
2186 struct bmc_device *bmc = dev_get_drvdata(dev);
2188 return snprintf(buf, 20, "%u\n",
2189 bmc->id.device_revision & 0x0F);
2192 static ssize_t firmware_rev_show(struct device *dev,
2193 struct device_attribute *attr,
2196 struct bmc_device *bmc = dev_get_drvdata(dev);
2198 return snprintf(buf, 20, "%u.%x\n", bmc->id.firmware_revision_1,
2199 bmc->id.firmware_revision_2);
2202 static ssize_t ipmi_version_show(struct device *dev,
2203 struct device_attribute *attr,
2206 struct bmc_device *bmc = dev_get_drvdata(dev);
2208 return snprintf(buf, 20, "%u.%u\n",
2209 ipmi_version_major(&bmc->id),
2210 ipmi_version_minor(&bmc->id));
2213 static ssize_t add_dev_support_show(struct device *dev,
2214 struct device_attribute *attr,
2217 struct bmc_device *bmc = dev_get_drvdata(dev);
2219 return snprintf(buf, 10, "0x%02x\n",
2220 bmc->id.additional_device_support);
2223 static ssize_t manufacturer_id_show(struct device *dev,
2224 struct device_attribute *attr,
2227 struct bmc_device *bmc = dev_get_drvdata(dev);
2229 return snprintf(buf, 20, "0x%6.6x\n", bmc->id.manufacturer_id);
2232 static ssize_t product_id_show(struct device *dev,
2233 struct device_attribute *attr,
2236 struct bmc_device *bmc = dev_get_drvdata(dev);
2238 return snprintf(buf, 10, "0x%4.4x\n", bmc->id.product_id);
2241 static ssize_t aux_firmware_rev_show(struct device *dev,
2242 struct device_attribute *attr,
2245 struct bmc_device *bmc = dev_get_drvdata(dev);
2247 return snprintf(buf, 21, "0x%02x 0x%02x 0x%02x 0x%02x\n",
2248 bmc->id.aux_firmware_revision[3],
2249 bmc->id.aux_firmware_revision[2],
2250 bmc->id.aux_firmware_revision[1],
2251 bmc->id.aux_firmware_revision[0]);
2254 static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
2257 struct bmc_device *bmc = dev_get_drvdata(dev);
2259 return snprintf(buf, 100, "%Lx%Lx\n",
2260 (long long) bmc->guid[0],
2261 (long long) bmc->guid[8]);
2264 static void remove_files(struct bmc_device *bmc)
2269 device_remove_file(&bmc->dev->dev,
2270 &bmc->device_id_attr);
2271 device_remove_file(&bmc->dev->dev,
2272 &bmc->provides_dev_sdrs_attr);
2273 device_remove_file(&bmc->dev->dev,
2274 &bmc->revision_attr);
2275 device_remove_file(&bmc->dev->dev,
2276 &bmc->firmware_rev_attr);
2277 device_remove_file(&bmc->dev->dev,
2278 &bmc->version_attr);
2279 device_remove_file(&bmc->dev->dev,
2280 &bmc->add_dev_support_attr);
2281 device_remove_file(&bmc->dev->dev,
2282 &bmc->manufacturer_id_attr);
2283 device_remove_file(&bmc->dev->dev,
2284 &bmc->product_id_attr);
2286 if (bmc->id.aux_firmware_revision_set)
2287 device_remove_file(&bmc->dev->dev,
2288 &bmc->aux_firmware_rev_attr);
2290 device_remove_file(&bmc->dev->dev,
2295 cleanup_bmc_device(struct kref *ref)
2297 struct bmc_device *bmc;
2299 bmc = container_of(ref, struct bmc_device, refcount);
2302 platform_device_unregister(bmc->dev);
2306 static void ipmi_bmc_unregister(ipmi_smi_t intf)
2308 struct bmc_device *bmc = intf->bmc;
2310 if (intf->sysfs_name) {
2311 sysfs_remove_link(&intf->si_dev->kobj, intf->sysfs_name);
2312 kfree(intf->sysfs_name);
2313 intf->sysfs_name = NULL;
2315 if (intf->my_dev_name) {
2316 sysfs_remove_link(&bmc->dev->dev.kobj, intf->my_dev_name);
2317 kfree(intf->my_dev_name);
2318 intf->my_dev_name = NULL;
2321 mutex_lock(&ipmidriver_mutex);
2322 kref_put(&bmc->refcount, cleanup_bmc_device);
2324 mutex_unlock(&ipmidriver_mutex);
2327 static int create_files(struct bmc_device *bmc)
2331 bmc->device_id_attr.attr.name = "device_id";
2332 bmc->device_id_attr.attr.mode = S_IRUGO;
2333 bmc->device_id_attr.show = device_id_show;
2334 sysfs_attr_init(&bmc->device_id_attr.attr);
2336 bmc->provides_dev_sdrs_attr.attr.name = "provides_device_sdrs";
2337 bmc->provides_dev_sdrs_attr.attr.mode = S_IRUGO;
2338 bmc->provides_dev_sdrs_attr.show = provides_dev_sdrs_show;
2339 sysfs_attr_init(&bmc->provides_dev_sdrs_attr.attr);
2341 bmc->revision_attr.attr.name = "revision";
2342 bmc->revision_attr.attr.mode = S_IRUGO;
2343 bmc->revision_attr.show = revision_show;
2344 sysfs_attr_init(&bmc->revision_attr.attr);
2346 bmc->firmware_rev_attr.attr.name = "firmware_revision";
2347 bmc->firmware_rev_attr.attr.mode = S_IRUGO;
2348 bmc->firmware_rev_attr.show = firmware_rev_show;
2349 sysfs_attr_init(&bmc->firmware_rev_attr.attr);
2351 bmc->version_attr.attr.name = "ipmi_version";
2352 bmc->version_attr.attr.mode = S_IRUGO;
2353 bmc->version_attr.show = ipmi_version_show;
2354 sysfs_attr_init(&bmc->version_attr.attr);
2356 bmc->add_dev_support_attr.attr.name = "additional_device_support";
2357 bmc->add_dev_support_attr.attr.mode = S_IRUGO;
2358 bmc->add_dev_support_attr.show = add_dev_support_show;
2359 sysfs_attr_init(&bmc->add_dev_support_attr.attr);
2361 bmc->manufacturer_id_attr.attr.name = "manufacturer_id";
2362 bmc->manufacturer_id_attr.attr.mode = S_IRUGO;
2363 bmc->manufacturer_id_attr.show = manufacturer_id_show;
2364 sysfs_attr_init(&bmc->manufacturer_id_attr.attr);
2366 bmc->product_id_attr.attr.name = "product_id";
2367 bmc->product_id_attr.attr.mode = S_IRUGO;
2368 bmc->product_id_attr.show = product_id_show;
2369 sysfs_attr_init(&bmc->product_id_attr.attr);
2371 bmc->guid_attr.attr.name = "guid";
2372 bmc->guid_attr.attr.mode = S_IRUGO;
2373 bmc->guid_attr.show = guid_show;
2374 sysfs_attr_init(&bmc->guid_attr.attr);
2376 bmc->aux_firmware_rev_attr.attr.name = "aux_firmware_revision";
2377 bmc->aux_firmware_rev_attr.attr.mode = S_IRUGO;
2378 bmc->aux_firmware_rev_attr.show = aux_firmware_rev_show;
2379 sysfs_attr_init(&bmc->aux_firmware_rev_attr.attr);
2381 err = device_create_file(&bmc->dev->dev,
2382 &bmc->device_id_attr);
2385 err = device_create_file(&bmc->dev->dev,
2386 &bmc->provides_dev_sdrs_attr);
2389 err = device_create_file(&bmc->dev->dev,
2390 &bmc->revision_attr);
2393 err = device_create_file(&bmc->dev->dev,
2394 &bmc->firmware_rev_attr);
2397 err = device_create_file(&bmc->dev->dev,
2398 &bmc->version_attr);
2401 err = device_create_file(&bmc->dev->dev,
2402 &bmc->add_dev_support_attr);
2405 err = device_create_file(&bmc->dev->dev,
2406 &bmc->manufacturer_id_attr);
2409 err = device_create_file(&bmc->dev->dev,
2410 &bmc->product_id_attr);
2413 if (bmc->id.aux_firmware_revision_set) {
2414 err = device_create_file(&bmc->dev->dev,
2415 &bmc->aux_firmware_rev_attr);
2419 if (bmc->guid_set) {
2420 err = device_create_file(&bmc->dev->dev,
2429 if (bmc->id.aux_firmware_revision_set)
2430 device_remove_file(&bmc->dev->dev,
2431 &bmc->aux_firmware_rev_attr);
2433 device_remove_file(&bmc->dev->dev,
2434 &bmc->product_id_attr);
2436 device_remove_file(&bmc->dev->dev,
2437 &bmc->manufacturer_id_attr);
2439 device_remove_file(&bmc->dev->dev,
2440 &bmc->add_dev_support_attr);
2442 device_remove_file(&bmc->dev->dev,
2443 &bmc->version_attr);
2445 device_remove_file(&bmc->dev->dev,
2446 &bmc->firmware_rev_attr);
2448 device_remove_file(&bmc->dev->dev,
2449 &bmc->revision_attr);
2451 device_remove_file(&bmc->dev->dev,
2452 &bmc->provides_dev_sdrs_attr);
2454 device_remove_file(&bmc->dev->dev,
2455 &bmc->device_id_attr);
2460 static int ipmi_bmc_register(ipmi_smi_t intf, int ifnum,
2461 const char *sysfs_name)
2464 struct bmc_device *bmc = intf->bmc;
2465 struct bmc_device *old_bmc;
2469 mutex_lock(&ipmidriver_mutex);
2472 * Try to find if there is an bmc_device struct
2473 * representing the interfaced BMC already
2476 old_bmc = ipmi_find_bmc_guid(&ipmidriver.driver, bmc->guid);
2478 old_bmc = ipmi_find_bmc_prod_dev_id(&ipmidriver.driver,
2483 * If there is already an bmc_device, free the new one,
2484 * otherwise register the new BMC device
2488 intf->bmc = old_bmc;
2491 kref_get(&bmc->refcount);
2492 mutex_unlock(&ipmidriver_mutex);
2495 "ipmi: interfacing existing BMC (man_id: 0x%6.6x,"
2496 " prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
2497 bmc->id.manufacturer_id,
2502 unsigned char orig_dev_id = bmc->id.device_id;
2503 int warn_printed = 0;
2505 snprintf(name, sizeof(name),
2506 "ipmi_bmc.%4.4x", bmc->id.product_id);
2508 while (ipmi_find_bmc_prod_dev_id(&ipmidriver.driver,
2510 bmc->id.device_id)) {
2511 if (!warn_printed) {
2512 printk(KERN_WARNING PFX
2513 "This machine has two different BMCs"
2514 " with the same product id and device"
2515 " id. This is an error in the"
2516 " firmware, but incrementing the"
2517 " device id to work around the problem."
2518 " Prod ID = 0x%x, Dev ID = 0x%x\n",
2519 bmc->id.product_id, bmc->id.device_id);
2522 bmc->id.device_id++; /* Wraps at 255 */
2523 if (bmc->id.device_id == orig_dev_id) {
2525 "Out of device ids!\n");
2530 bmc->dev = platform_device_alloc(name, bmc->id.device_id);
2532 mutex_unlock(&ipmidriver_mutex);
2535 " Unable to allocate platform device\n");
2538 bmc->dev->dev.driver = &ipmidriver.driver;
2539 dev_set_drvdata(&bmc->dev->dev, bmc);
2540 kref_init(&bmc->refcount);
2542 rv = platform_device_add(bmc->dev);
2543 mutex_unlock(&ipmidriver_mutex);
2545 platform_device_put(bmc->dev);
2549 " Unable to register bmc device: %d\n",
2552 * Don't go to out_err, you can only do that if
2553 * the device is registered already.
2558 rv = create_files(bmc);
2560 mutex_lock(&ipmidriver_mutex);
2561 platform_device_unregister(bmc->dev);
2562 mutex_unlock(&ipmidriver_mutex);
2567 dev_info(intf->si_dev, "Found new BMC (man_id: 0x%6.6x, "
2568 "prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
2569 bmc->id.manufacturer_id,
2575 * create symlink from system interface device to bmc device
2578 intf->sysfs_name = kstrdup(sysfs_name, GFP_KERNEL);
2579 if (!intf->sysfs_name) {
2582 "ipmi_msghandler: allocate link to BMC: %d\n",
2587 rv = sysfs_create_link(&intf->si_dev->kobj,
2588 &bmc->dev->dev.kobj, intf->sysfs_name);
2590 kfree(intf->sysfs_name);
2591 intf->sysfs_name = NULL;
2593 "ipmi_msghandler: Unable to create bmc symlink: %d\n",
2598 size = snprintf(dummy, 0, "ipmi%d", ifnum);
2599 intf->my_dev_name = kmalloc(size+1, GFP_KERNEL);
2600 if (!intf->my_dev_name) {
2601 kfree(intf->sysfs_name);
2602 intf->sysfs_name = NULL;
2605 "ipmi_msghandler: allocate link from BMC: %d\n",
2609 snprintf(intf->my_dev_name, size+1, "ipmi%d", ifnum);
2611 rv = sysfs_create_link(&bmc->dev->dev.kobj, &intf->si_dev->kobj,
2614 kfree(intf->sysfs_name);
2615 intf->sysfs_name = NULL;
2616 kfree(intf->my_dev_name);
2617 intf->my_dev_name = NULL;
2620 " Unable to create symlink to bmc: %d\n",
2628 ipmi_bmc_unregister(intf);
2633 send_guid_cmd(ipmi_smi_t intf, int chan)
2635 struct kernel_ipmi_msg msg;
2636 struct ipmi_system_interface_addr si;
2638 si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2639 si.channel = IPMI_BMC_CHANNEL;
2642 msg.netfn = IPMI_NETFN_APP_REQUEST;
2643 msg.cmd = IPMI_GET_DEVICE_GUID_CMD;
2646 return i_ipmi_request(NULL,
2648 (struct ipmi_addr *) &si,
2655 intf->channels[0].address,
2656 intf->channels[0].lun,
2661 guid_handler(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
2663 if ((msg->addr.addr_type != IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
2664 || (msg->msg.netfn != IPMI_NETFN_APP_RESPONSE)
2665 || (msg->msg.cmd != IPMI_GET_DEVICE_GUID_CMD))
2669 if (msg->msg.data[0] != 0) {
2670 /* Error from getting the GUID, the BMC doesn't have one. */
2671 intf->bmc->guid_set = 0;
2675 if (msg->msg.data_len < 17) {
2676 intf->bmc->guid_set = 0;
2677 printk(KERN_WARNING PFX
2678 "guid_handler: The GUID response from the BMC was too"
2679 " short, it was %d but should have been 17. Assuming"
2680 " GUID is not available.\n",
2685 memcpy(intf->bmc->guid, msg->msg.data, 16);
2686 intf->bmc->guid_set = 1;
2688 wake_up(&intf->waitq);
2692 get_guid(ipmi_smi_t intf)
2696 intf->bmc->guid_set = 0x2;
2697 intf->null_user_handler = guid_handler;
2698 rv = send_guid_cmd(intf, 0);
2700 /* Send failed, no GUID available. */
2701 intf->bmc->guid_set = 0;
2702 wait_event(intf->waitq, intf->bmc->guid_set != 2);
2703 intf->null_user_handler = NULL;
2707 send_channel_info_cmd(ipmi_smi_t intf, int chan)
2709 struct kernel_ipmi_msg msg;
2710 unsigned char data[1];
2711 struct ipmi_system_interface_addr si;
2713 si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2714 si.channel = IPMI_BMC_CHANNEL;
2717 msg.netfn = IPMI_NETFN_APP_REQUEST;
2718 msg.cmd = IPMI_GET_CHANNEL_INFO_CMD;
2722 return i_ipmi_request(NULL,
2724 (struct ipmi_addr *) &si,
2731 intf->channels[0].address,
2732 intf->channels[0].lun,
2737 channel_handler(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
2742 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
2743 && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
2744 && (msg->msg.cmd == IPMI_GET_CHANNEL_INFO_CMD)) {
2745 /* It's the one we want */
2746 if (msg->msg.data[0] != 0) {
2747 /* Got an error from the channel, just go on. */
2749 if (msg->msg.data[0] == IPMI_INVALID_COMMAND_ERR) {
2751 * If the MC does not support this
2752 * command, that is legal. We just
2753 * assume it has one IPMB at channel
2756 intf->channels[0].medium
2757 = IPMI_CHANNEL_MEDIUM_IPMB;
2758 intf->channels[0].protocol
2759 = IPMI_CHANNEL_PROTOCOL_IPMB;
2762 intf->curr_channel = IPMI_MAX_CHANNELS;
2763 wake_up(&intf->waitq);
2768 if (msg->msg.data_len < 4) {
2769 /* Message not big enough, just go on. */
2772 chan = intf->curr_channel;
2773 intf->channels[chan].medium = msg->msg.data[2] & 0x7f;
2774 intf->channels[chan].protocol = msg->msg.data[3] & 0x1f;
2777 intf->curr_channel++;
2778 if (intf->curr_channel >= IPMI_MAX_CHANNELS)
2779 wake_up(&intf->waitq);
2781 rv = send_channel_info_cmd(intf, intf->curr_channel);
2784 /* Got an error somehow, just give up. */
2785 intf->curr_channel = IPMI_MAX_CHANNELS;
2786 wake_up(&intf->waitq);
2788 printk(KERN_WARNING PFX
2789 "Error sending channel information: %d\n",
2797 static void ipmi_poll(ipmi_smi_t intf)
2799 if (intf->handlers->poll)
2800 intf->handlers->poll(intf->send_info);
2801 /* In case something came in */
2802 handle_new_recv_msgs(intf);
2805 void ipmi_poll_interface(ipmi_user_t user)
2807 ipmi_poll(user->intf);
2809 EXPORT_SYMBOL(ipmi_poll_interface);
2811 int ipmi_register_smi(struct ipmi_smi_handlers *handlers,
2813 struct ipmi_device_id *device_id,
2814 struct device *si_dev,
2815 const char *sysfs_name,
2816 unsigned char slave_addr)
2822 struct list_head *link;
2825 * Make sure the driver is actually initialized, this handles
2826 * problems with initialization order.
2829 rv = ipmi_init_msghandler();
2833 * The init code doesn't return an error if it was turned
2834 * off, but it won't initialize. Check that.
2840 intf = kzalloc(sizeof(*intf), GFP_KERNEL);
2844 intf->ipmi_version_major = ipmi_version_major(device_id);
2845 intf->ipmi_version_minor = ipmi_version_minor(device_id);
2847 intf->bmc = kzalloc(sizeof(*intf->bmc), GFP_KERNEL);
2852 intf->intf_num = -1; /* Mark it invalid for now. */
2853 kref_init(&intf->refcount);
2854 intf->bmc->id = *device_id;
2855 intf->si_dev = si_dev;
2856 for (j = 0; j < IPMI_MAX_CHANNELS; j++) {
2857 intf->channels[j].address = IPMI_BMC_SLAVE_ADDR;
2858 intf->channels[j].lun = 2;
2860 if (slave_addr != 0)
2861 intf->channels[0].address = slave_addr;
2862 INIT_LIST_HEAD(&intf->users);
2863 intf->handlers = handlers;
2864 intf->send_info = send_info;
2865 spin_lock_init(&intf->seq_lock);
2866 for (j = 0; j < IPMI_IPMB_NUM_SEQ; j++) {
2867 intf->seq_table[j].inuse = 0;
2868 intf->seq_table[j].seqid = 0;
2871 #ifdef CONFIG_PROC_FS
2872 mutex_init(&intf->proc_entry_lock);
2874 spin_lock_init(&intf->waiting_msgs_lock);
2875 INIT_LIST_HEAD(&intf->waiting_msgs);
2876 tasklet_init(&intf->recv_tasklet,
2878 (unsigned long) intf);
2879 atomic_set(&intf->watchdog_pretimeouts_to_deliver, 0);
2880 spin_lock_init(&intf->events_lock);
2881 INIT_LIST_HEAD(&intf->waiting_events);
2882 intf->waiting_events_count = 0;
2883 mutex_init(&intf->cmd_rcvrs_mutex);
2884 spin_lock_init(&intf->maintenance_mode_lock);
2885 INIT_LIST_HEAD(&intf->cmd_rcvrs);
2886 init_waitqueue_head(&intf->waitq);
2887 for (i = 0; i < IPMI_NUM_STATS; i++)
2888 atomic_set(&intf->stats[i], 0);
2890 intf->proc_dir = NULL;
2892 mutex_lock(&smi_watchers_mutex);
2893 mutex_lock(&ipmi_interfaces_mutex);
2894 /* Look for a hole in the numbers. */
2896 link = &ipmi_interfaces;
2897 list_for_each_entry_rcu(tintf, &ipmi_interfaces, link) {
2898 if (tintf->intf_num != i) {
2899 link = &tintf->link;
2904 /* Add the new interface in numeric order. */
2906 list_add_rcu(&intf->link, &ipmi_interfaces);
2908 list_add_tail_rcu(&intf->link, link);
2910 rv = handlers->start_processing(send_info, intf);
2916 if ((intf->ipmi_version_major > 1)
2917 || ((intf->ipmi_version_major == 1)
2918 && (intf->ipmi_version_minor >= 5))) {
2920 * Start scanning the channels to see what is
2923 intf->null_user_handler = channel_handler;
2924 intf->curr_channel = 0;
2925 rv = send_channel_info_cmd(intf, 0);
2929 /* Wait for the channel info to be read. */
2930 wait_event(intf->waitq,
2931 intf->curr_channel >= IPMI_MAX_CHANNELS);
2932 intf->null_user_handler = NULL;
2934 /* Assume a single IPMB channel at zero. */
2935 intf->channels[0].medium = IPMI_CHANNEL_MEDIUM_IPMB;
2936 intf->channels[0].protocol = IPMI_CHANNEL_PROTOCOL_IPMB;
2937 intf->curr_channel = IPMI_MAX_CHANNELS;
2941 rv = add_proc_entries(intf, i);
2943 rv = ipmi_bmc_register(intf, i, sysfs_name);
2948 remove_proc_entries(intf);
2949 intf->handlers = NULL;
2950 list_del_rcu(&intf->link);
2951 mutex_unlock(&ipmi_interfaces_mutex);
2952 mutex_unlock(&smi_watchers_mutex);
2954 kref_put(&intf->refcount, intf_free);
2957 * Keep memory order straight for RCU readers. Make
2958 * sure everything else is committed to memory before
2959 * setting intf_num to mark the interface valid.
2963 mutex_unlock(&ipmi_interfaces_mutex);
2964 /* After this point the interface is legal to use. */
2965 call_smi_watchers(i, intf->si_dev);
2966 mutex_unlock(&smi_watchers_mutex);
2971 EXPORT_SYMBOL(ipmi_register_smi);
2973 static void cleanup_smi_msgs(ipmi_smi_t intf)
2976 struct seq_table *ent;
2978 /* No need for locks, the interface is down. */
2979 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
2980 ent = &(intf->seq_table[i]);
2983 deliver_err_response(ent->recv_msg, IPMI_ERR_UNSPECIFIED);
2987 int ipmi_unregister_smi(ipmi_smi_t intf)
2989 struct ipmi_smi_watcher *w;
2990 int intf_num = intf->intf_num;
2992 ipmi_bmc_unregister(intf);
2994 mutex_lock(&smi_watchers_mutex);
2995 mutex_lock(&ipmi_interfaces_mutex);
2996 intf->intf_num = -1;
2997 intf->handlers = NULL;
2998 list_del_rcu(&intf->link);
2999 mutex_unlock(&ipmi_interfaces_mutex);
3002 cleanup_smi_msgs(intf);
3004 remove_proc_entries(intf);
3007 * Call all the watcher interfaces to tell them that
3008 * an interface is gone.
3010 list_for_each_entry(w, &smi_watchers, link)
3011 w->smi_gone(intf_num);
3012 mutex_unlock(&smi_watchers_mutex);
3014 kref_put(&intf->refcount, intf_free);
3017 EXPORT_SYMBOL(ipmi_unregister_smi);
3019 static int handle_ipmb_get_msg_rsp(ipmi_smi_t intf,
3020 struct ipmi_smi_msg *msg)
3022 struct ipmi_ipmb_addr ipmb_addr;
3023 struct ipmi_recv_msg *recv_msg;
3026 * This is 11, not 10, because the response must contain a
3029 if (msg->rsp_size < 11) {
3030 /* Message not big enough, just ignore it. */
3031 ipmi_inc_stat(intf, invalid_ipmb_responses);
3035 if (msg->rsp[2] != 0) {
3036 /* An error getting the response, just ignore it. */
3040 ipmb_addr.addr_type = IPMI_IPMB_ADDR_TYPE;
3041 ipmb_addr.slave_addr = msg->rsp[6];
3042 ipmb_addr.channel = msg->rsp[3] & 0x0f;
3043 ipmb_addr.lun = msg->rsp[7] & 3;
3046 * It's a response from a remote entity. Look up the sequence
3047 * number and handle the response.
3049 if (intf_find_seq(intf,
3053 (msg->rsp[4] >> 2) & (~1),
3054 (struct ipmi_addr *) &(ipmb_addr),
3057 * We were unable to find the sequence number,
3058 * so just nuke the message.
3060 ipmi_inc_stat(intf, unhandled_ipmb_responses);
3064 memcpy(recv_msg->msg_data,
3068 * The other fields matched, so no need to set them, except
3069 * for netfn, which needs to be the response that was
3070 * returned, not the request value.
3072 recv_msg->msg.netfn = msg->rsp[4] >> 2;
3073 recv_msg->msg.data = recv_msg->msg_data;
3074 recv_msg->msg.data_len = msg->rsp_size - 10;
3075 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
3076 ipmi_inc_stat(intf, handled_ipmb_responses);
3077 deliver_response(recv_msg);
3082 static int handle_ipmb_get_msg_cmd(ipmi_smi_t intf,
3083 struct ipmi_smi_msg *msg)
3085 struct cmd_rcvr *rcvr;
3087 unsigned char netfn;
3090 ipmi_user_t user = NULL;
3091 struct ipmi_ipmb_addr *ipmb_addr;
3092 struct ipmi_recv_msg *recv_msg;
3093 struct ipmi_smi_handlers *handlers;
3095 if (msg->rsp_size < 10) {
3096 /* Message not big enough, just ignore it. */
3097 ipmi_inc_stat(intf, invalid_commands);
3101 if (msg->rsp[2] != 0) {
3102 /* An error getting the response, just ignore it. */
3106 netfn = msg->rsp[4] >> 2;
3108 chan = msg->rsp[3] & 0xf;
3111 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
3114 kref_get(&user->refcount);
3120 /* We didn't find a user, deliver an error response. */
3121 ipmi_inc_stat(intf, unhandled_commands);
3123 msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
3124 msg->data[1] = IPMI_SEND_MSG_CMD;
3125 msg->data[2] = msg->rsp[3];
3126 msg->data[3] = msg->rsp[6];
3127 msg->data[4] = ((netfn + 1) << 2) | (msg->rsp[7] & 0x3);
3128 msg->data[5] = ipmb_checksum(&(msg->data[3]), 2);
3129 msg->data[6] = intf->channels[msg->rsp[3] & 0xf].address;
3131 msg->data[7] = (msg->rsp[7] & 0xfc) | (msg->rsp[4] & 0x3);
3132 msg->data[8] = msg->rsp[8]; /* cmd */
3133 msg->data[9] = IPMI_INVALID_CMD_COMPLETION_CODE;
3134 msg->data[10] = ipmb_checksum(&(msg->data[6]), 4);
3135 msg->data_size = 11;
3140 printk("Invalid command:");
3141 for (m = 0; m < msg->data_size; m++)
3142 printk(" %2.2x", msg->data[m]);
3147 handlers = intf->handlers;
3149 handlers->sender(intf->send_info, msg, 0);
3151 * We used the message, so return the value
3152 * that causes it to not be freed or
3159 /* Deliver the message to the user. */
3160 ipmi_inc_stat(intf, handled_commands);
3162 recv_msg = ipmi_alloc_recv_msg();
3165 * We couldn't allocate memory for the
3166 * message, so requeue it for handling
3170 kref_put(&user->refcount, free_user);
3172 /* Extract the source address from the data. */
3173 ipmb_addr = (struct ipmi_ipmb_addr *) &recv_msg->addr;
3174 ipmb_addr->addr_type = IPMI_IPMB_ADDR_TYPE;
3175 ipmb_addr->slave_addr = msg->rsp[6];
3176 ipmb_addr->lun = msg->rsp[7] & 3;
3177 ipmb_addr->channel = msg->rsp[3] & 0xf;
3180 * Extract the rest of the message information
3181 * from the IPMB header.
3183 recv_msg->user = user;
3184 recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
3185 recv_msg->msgid = msg->rsp[7] >> 2;
3186 recv_msg->msg.netfn = msg->rsp[4] >> 2;
3187 recv_msg->msg.cmd = msg->rsp[8];
3188 recv_msg->msg.data = recv_msg->msg_data;
3191 * We chop off 10, not 9 bytes because the checksum
3192 * at the end also needs to be removed.
3194 recv_msg->msg.data_len = msg->rsp_size - 10;
3195 memcpy(recv_msg->msg_data,
3197 msg->rsp_size - 10);
3198 deliver_response(recv_msg);
3205 static int handle_lan_get_msg_rsp(ipmi_smi_t intf,
3206 struct ipmi_smi_msg *msg)
3208 struct ipmi_lan_addr lan_addr;
3209 struct ipmi_recv_msg *recv_msg;
3213 * This is 13, not 12, because the response must contain a
3216 if (msg->rsp_size < 13) {
3217 /* Message not big enough, just ignore it. */
3218 ipmi_inc_stat(intf, invalid_lan_responses);
3222 if (msg->rsp[2] != 0) {
3223 /* An error getting the response, just ignore it. */
3227 lan_addr.addr_type = IPMI_LAN_ADDR_TYPE;
3228 lan_addr.session_handle = msg->rsp[4];
3229 lan_addr.remote_SWID = msg->rsp[8];
3230 lan_addr.local_SWID = msg->rsp[5];
3231 lan_addr.channel = msg->rsp[3] & 0x0f;
3232 lan_addr.privilege = msg->rsp[3] >> 4;
3233 lan_addr.lun = msg->rsp[9] & 3;
3236 * It's a response from a remote entity. Look up the sequence
3237 * number and handle the response.
3239 if (intf_find_seq(intf,
3243 (msg->rsp[6] >> 2) & (~1),
3244 (struct ipmi_addr *) &(lan_addr),
3247 * We were unable to find the sequence number,
3248 * so just nuke the message.
3250 ipmi_inc_stat(intf, unhandled_lan_responses);
3254 memcpy(recv_msg->msg_data,
3256 msg->rsp_size - 11);
3258 * The other fields matched, so no need to set them, except
3259 * for netfn, which needs to be the response that was
3260 * returned, not the request value.
3262 recv_msg->msg.netfn = msg->rsp[6] >> 2;
3263 recv_msg->msg.data = recv_msg->msg_data;
3264 recv_msg->msg.data_len = msg->rsp_size - 12;
3265 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
3266 ipmi_inc_stat(intf, handled_lan_responses);
3267 deliver_response(recv_msg);
3272 static int handle_lan_get_msg_cmd(ipmi_smi_t intf,
3273 struct ipmi_smi_msg *msg)
3275 struct cmd_rcvr *rcvr;
3277 unsigned char netfn;
3280 ipmi_user_t user = NULL;
3281 struct ipmi_lan_addr *lan_addr;
3282 struct ipmi_recv_msg *recv_msg;
3284 if (msg->rsp_size < 12) {
3285 /* Message not big enough, just ignore it. */
3286 ipmi_inc_stat(intf, invalid_commands);
3290 if (msg->rsp[2] != 0) {
3291 /* An error getting the response, just ignore it. */
3295 netfn = msg->rsp[6] >> 2;
3297 chan = msg->rsp[3] & 0xf;
3300 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
3303 kref_get(&user->refcount);
3309 /* We didn't find a user, just give up. */
3310 ipmi_inc_stat(intf, unhandled_commands);
3313 * Don't do anything with these messages, just allow
3318 /* Deliver the message to the user. */
3319 ipmi_inc_stat(intf, handled_commands);
3321 recv_msg = ipmi_alloc_recv_msg();
3324 * We couldn't allocate memory for the
3325 * message, so requeue it for handling later.
3328 kref_put(&user->refcount, free_user);
3330 /* Extract the source address from the data. */
3331 lan_addr = (struct ipmi_lan_addr *) &recv_msg->addr;
3332 lan_addr->addr_type = IPMI_LAN_ADDR_TYPE;
3333 lan_addr->session_handle = msg->rsp[4];
3334 lan_addr->remote_SWID = msg->rsp[8];
3335 lan_addr->local_SWID = msg->rsp[5];
3336 lan_addr->lun = msg->rsp[9] & 3;
3337 lan_addr->channel = msg->rsp[3] & 0xf;
3338 lan_addr->privilege = msg->rsp[3] >> 4;
3341 * Extract the rest of the message information
3342 * from the IPMB header.
3344 recv_msg->user = user;
3345 recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
3346 recv_msg->msgid = msg->rsp[9] >> 2;
3347 recv_msg->msg.netfn = msg->rsp[6] >> 2;
3348 recv_msg->msg.cmd = msg->rsp[10];
3349 recv_msg->msg.data = recv_msg->msg_data;
3352 * We chop off 12, not 11 bytes because the checksum
3353 * at the end also needs to be removed.
3355 recv_msg->msg.data_len = msg->rsp_size - 12;
3356 memcpy(recv_msg->msg_data,
3358 msg->rsp_size - 12);
3359 deliver_response(recv_msg);
3367 * This routine will handle "Get Message" command responses with
3368 * channels that use an OEM Medium. The message format belongs to
3369 * the OEM. See IPMI 2.0 specification, Chapter 6 and
3370 * Chapter 22, sections 22.6 and 22.24 for more details.
3372 static int handle_oem_get_msg_cmd(ipmi_smi_t intf,
3373 struct ipmi_smi_msg *msg)
3375 struct cmd_rcvr *rcvr;
3377 unsigned char netfn;
3380 ipmi_user_t user = NULL;
3381 struct ipmi_system_interface_addr *smi_addr;
3382 struct ipmi_recv_msg *recv_msg;
3385 * We expect the OEM SW to perform error checking
3386 * so we just do some basic sanity checks
3388 if (msg->rsp_size < 4) {
3389 /* Message not big enough, just ignore it. */
3390 ipmi_inc_stat(intf, invalid_commands);
3394 if (msg->rsp[2] != 0) {
3395 /* An error getting the response, just ignore it. */
3400 * This is an OEM Message so the OEM needs to know how
3401 * handle the message. We do no interpretation.
3403 netfn = msg->rsp[0] >> 2;
3405 chan = msg->rsp[3] & 0xf;
3408 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
3411 kref_get(&user->refcount);
3417 /* We didn't find a user, just give up. */
3418 ipmi_inc_stat(intf, unhandled_commands);
3421 * Don't do anything with these messages, just allow
3427 /* Deliver the message to the user. */
3428 ipmi_inc_stat(intf, handled_commands);
3430 recv_msg = ipmi_alloc_recv_msg();
3433 * We couldn't allocate memory for the
3434 * message, so requeue it for handling
3438 kref_put(&user->refcount, free_user);
3441 * OEM Messages are expected to be delivered via
3442 * the system interface to SMS software. We might
3443 * need to visit this again depending on OEM
3446 smi_addr = ((struct ipmi_system_interface_addr *)
3448 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3449 smi_addr->channel = IPMI_BMC_CHANNEL;
3450 smi_addr->lun = msg->rsp[0] & 3;
3452 recv_msg->user = user;
3453 recv_msg->user_msg_data = NULL;
3454 recv_msg->recv_type = IPMI_OEM_RECV_TYPE;
3455 recv_msg->msg.netfn = msg->rsp[0] >> 2;
3456 recv_msg->msg.cmd = msg->rsp[1];
3457 recv_msg->msg.data = recv_msg->msg_data;
3460 * The message starts at byte 4 which follows the
3461 * the Channel Byte in the "GET MESSAGE" command
3463 recv_msg->msg.data_len = msg->rsp_size - 4;
3464 memcpy(recv_msg->msg_data,
3467 deliver_response(recv_msg);
3474 static void copy_event_into_recv_msg(struct ipmi_recv_msg *recv_msg,
3475 struct ipmi_smi_msg *msg)
3477 struct ipmi_system_interface_addr *smi_addr;
3479 recv_msg->msgid = 0;
3480 smi_addr = (struct ipmi_system_interface_addr *) &(recv_msg->addr);
3481 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3482 smi_addr->channel = IPMI_BMC_CHANNEL;
3483 smi_addr->lun = msg->rsp[0] & 3;
3484 recv_msg->recv_type = IPMI_ASYNC_EVENT_RECV_TYPE;
3485 recv_msg->msg.netfn = msg->rsp[0] >> 2;
3486 recv_msg->msg.cmd = msg->rsp[1];
3487 memcpy(recv_msg->msg_data, &(msg->rsp[3]), msg->rsp_size - 3);
3488 recv_msg->msg.data = recv_msg->msg_data;
3489 recv_msg->msg.data_len = msg->rsp_size - 3;
3492 static int handle_read_event_rsp(ipmi_smi_t intf,
3493 struct ipmi_smi_msg *msg)
3495 struct ipmi_recv_msg *recv_msg, *recv_msg2;
3496 struct list_head msgs;
3499 int deliver_count = 0;
3500 unsigned long flags;
3502 if (msg->rsp_size < 19) {
3503 /* Message is too small to be an IPMB event. */
3504 ipmi_inc_stat(intf, invalid_events);
3508 if (msg->rsp[2] != 0) {
3509 /* An error getting the event, just ignore it. */
3513 INIT_LIST_HEAD(&msgs);
3515 spin_lock_irqsave(&intf->events_lock, flags);
3517 ipmi_inc_stat(intf, events);
3520 * Allocate and fill in one message for every user that is
3524 list_for_each_entry_rcu(user, &intf->users, link) {
3525 if (!user->gets_events)
3528 recv_msg = ipmi_alloc_recv_msg();
3531 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs,
3533 list_del(&recv_msg->link);
3534 ipmi_free_recv_msg(recv_msg);
3537 * We couldn't allocate memory for the
3538 * message, so requeue it for handling
3547 copy_event_into_recv_msg(recv_msg, msg);
3548 recv_msg->user = user;
3549 kref_get(&user->refcount);
3550 list_add_tail(&(recv_msg->link), &msgs);
3554 if (deliver_count) {
3555 /* Now deliver all the messages. */
3556 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs, link) {
3557 list_del(&recv_msg->link);
3558 deliver_response(recv_msg);
3560 } else if (intf->waiting_events_count < MAX_EVENTS_IN_QUEUE) {
3562 * No one to receive the message, put it in queue if there's
3563 * not already too many things in the queue.
3565 recv_msg = ipmi_alloc_recv_msg();
3568 * We couldn't allocate memory for the
3569 * message, so requeue it for handling
3576 copy_event_into_recv_msg(recv_msg, msg);
3577 list_add_tail(&(recv_msg->link), &(intf->waiting_events));
3578 intf->waiting_events_count++;
3579 } else if (!intf->event_msg_printed) {
3581 * There's too many things in the queue, discard this
3584 printk(KERN_WARNING PFX "Event queue full, discarding"
3585 " incoming events\n");
3586 intf->event_msg_printed = 1;
3590 spin_unlock_irqrestore(&(intf->events_lock), flags);
3595 static int handle_bmc_rsp(ipmi_smi_t intf,
3596 struct ipmi_smi_msg *msg)
3598 struct ipmi_recv_msg *recv_msg;
3599 struct ipmi_user *user;
3601 recv_msg = (struct ipmi_recv_msg *) msg->user_data;
3602 if (recv_msg == NULL) {
3604 "IPMI message received with no owner. This\n"
3605 "could be because of a malformed message, or\n"
3606 "because of a hardware error. Contact your\n"
3607 "hardware vender for assistance\n");
3611 user = recv_msg->user;
3612 /* Make sure the user still exists. */
3613 if (user && !user->valid) {
3614 /* The user for the message went away, so give up. */
3615 ipmi_inc_stat(intf, unhandled_local_responses);
3616 ipmi_free_recv_msg(recv_msg);
3618 struct ipmi_system_interface_addr *smi_addr;
3620 ipmi_inc_stat(intf, handled_local_responses);
3621 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
3622 recv_msg->msgid = msg->msgid;
3623 smi_addr = ((struct ipmi_system_interface_addr *)
3625 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3626 smi_addr->channel = IPMI_BMC_CHANNEL;
3627 smi_addr->lun = msg->rsp[0] & 3;
3628 recv_msg->msg.netfn = msg->rsp[0] >> 2;
3629 recv_msg->msg.cmd = msg->rsp[1];
3630 memcpy(recv_msg->msg_data,
3633 recv_msg->msg.data = recv_msg->msg_data;
3634 recv_msg->msg.data_len = msg->rsp_size - 2;
3635 deliver_response(recv_msg);
3642 * Handle a received message. Return 1 if the message should be requeued,
3643 * 0 if the message should be freed, or -1 if the message should not
3644 * be freed or requeued.
3646 static int handle_one_recv_msg(ipmi_smi_t intf,
3647 struct ipmi_smi_msg *msg)
3655 for (m = 0; m < msg->rsp_size; m++)
3656 printk(" %2.2x", msg->rsp[m]);
3659 if (msg->rsp_size < 2) {
3660 /* Message is too small to be correct. */
3661 printk(KERN_WARNING PFX "BMC returned to small a message"
3662 " for netfn %x cmd %x, got %d bytes\n",
3663 (msg->data[0] >> 2) | 1, msg->data[1], msg->rsp_size);
3665 /* Generate an error response for the message. */
3666 msg->rsp[0] = msg->data[0] | (1 << 2);
3667 msg->rsp[1] = msg->data[1];
3668 msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
3670 } else if (((msg->rsp[0] >> 2) != ((msg->data[0] >> 2) | 1))
3671 || (msg->rsp[1] != msg->data[1])) {
3673 * The NetFN and Command in the response is not even
3674 * marginally correct.
3676 printk(KERN_WARNING PFX "BMC returned incorrect response,"
3677 " expected netfn %x cmd %x, got netfn %x cmd %x\n",
3678 (msg->data[0] >> 2) | 1, msg->data[1],
3679 msg->rsp[0] >> 2, msg->rsp[1]);
3681 /* Generate an error response for the message. */
3682 msg->rsp[0] = msg->data[0] | (1 << 2);
3683 msg->rsp[1] = msg->data[1];
3684 msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
3688 if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
3689 && (msg->rsp[1] == IPMI_SEND_MSG_CMD)
3690 && (msg->user_data != NULL)) {
3692 * It's a response to a response we sent. For this we
3693 * deliver a send message response to the user.
3695 struct ipmi_recv_msg *recv_msg = msg->user_data;
3698 if (msg->rsp_size < 2)
3699 /* Message is too small to be correct. */
3702 chan = msg->data[2] & 0x0f;
3703 if (chan >= IPMI_MAX_CHANNELS)
3704 /* Invalid channel number */
3710 /* Make sure the user still exists. */
3711 if (!recv_msg->user || !recv_msg->user->valid)
3714 recv_msg->recv_type = IPMI_RESPONSE_RESPONSE_TYPE;
3715 recv_msg->msg.data = recv_msg->msg_data;
3716 recv_msg->msg.data_len = 1;
3717 recv_msg->msg_data[0] = msg->rsp[2];
3718 deliver_response(recv_msg);
3719 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
3720 && (msg->rsp[1] == IPMI_GET_MSG_CMD)) {
3721 /* It's from the receive queue. */
3722 chan = msg->rsp[3] & 0xf;
3723 if (chan >= IPMI_MAX_CHANNELS) {
3724 /* Invalid channel number */
3730 * We need to make sure the channels have been initialized.
3731 * The channel_handler routine will set the "curr_channel"
3732 * equal to or greater than IPMI_MAX_CHANNELS when all the
3733 * channels for this interface have been initialized.
3735 if (intf->curr_channel < IPMI_MAX_CHANNELS) {
3736 requeue = 0; /* Throw the message away */
3740 switch (intf->channels[chan].medium) {
3741 case IPMI_CHANNEL_MEDIUM_IPMB:
3742 if (msg->rsp[4] & 0x04) {
3744 * It's a response, so find the
3745 * requesting message and send it up.
3747 requeue = handle_ipmb_get_msg_rsp(intf, msg);
3750 * It's a command to the SMS from some other
3751 * entity. Handle that.
3753 requeue = handle_ipmb_get_msg_cmd(intf, msg);
3757 case IPMI_CHANNEL_MEDIUM_8023LAN:
3758 case IPMI_CHANNEL_MEDIUM_ASYNC:
3759 if (msg->rsp[6] & 0x04) {
3761 * It's a response, so find the
3762 * requesting message and send it up.
3764 requeue = handle_lan_get_msg_rsp(intf, msg);
3767 * It's a command to the SMS from some other
3768 * entity. Handle that.
3770 requeue = handle_lan_get_msg_cmd(intf, msg);
3775 /* Check for OEM Channels. Clients had better
3776 register for these commands. */
3777 if ((intf->channels[chan].medium
3778 >= IPMI_CHANNEL_MEDIUM_OEM_MIN)
3779 && (intf->channels[chan].medium
3780 <= IPMI_CHANNEL_MEDIUM_OEM_MAX)) {
3781 requeue = handle_oem_get_msg_cmd(intf, msg);
3784 * We don't handle the channel type, so just
3791 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
3792 && (msg->rsp[1] == IPMI_READ_EVENT_MSG_BUFFER_CMD)) {
3793 /* It's an asyncronous event. */
3794 requeue = handle_read_event_rsp(intf, msg);
3796 /* It's a response from the local BMC. */
3797 requeue = handle_bmc_rsp(intf, msg);
3805 * If there are messages in the queue or pretimeouts, handle them.
3807 static void handle_new_recv_msgs(ipmi_smi_t intf)
3809 struct ipmi_smi_msg *smi_msg;
3810 unsigned long flags = 0;
3812 int run_to_completion = intf->run_to_completion;
3814 /* See if any waiting messages need to be processed. */
3815 if (!run_to_completion)
3816 spin_lock_irqsave(&intf->waiting_msgs_lock, flags);
3817 while (!list_empty(&intf->waiting_msgs)) {
3818 smi_msg = list_entry(intf->waiting_msgs.next,
3819 struct ipmi_smi_msg, link);
3820 list_del(&smi_msg->link);
3821 if (!run_to_completion)
3822 spin_unlock_irqrestore(&intf->waiting_msgs_lock, flags);
3823 rv = handle_one_recv_msg(intf, smi_msg);
3824 if (!run_to_completion)
3825 spin_lock_irqsave(&intf->waiting_msgs_lock, flags);
3827 /* Message handled */
3828 ipmi_free_smi_msg(smi_msg);
3829 } else if (rv < 0) {
3830 /* Fatal error on the message, del but don't free. */
3833 * To preserve message order, quit if we
3834 * can't handle a message.
3836 list_add(&smi_msg->link, &intf->waiting_msgs);
3840 if (!run_to_completion)
3841 spin_unlock_irqrestore(&intf->waiting_msgs_lock, flags);
3844 * If the pretimout count is non-zero, decrement one from it and
3845 * deliver pretimeouts to all the users.
3847 if (atomic_add_unless(&intf->watchdog_pretimeouts_to_deliver, -1, 0)) {
3851 list_for_each_entry_rcu(user, &intf->users, link) {
3852 if (user->handler->ipmi_watchdog_pretimeout)
3853 user->handler->ipmi_watchdog_pretimeout(
3854 user->handler_data);
3860 static void smi_recv_tasklet(unsigned long val)
3862 handle_new_recv_msgs((ipmi_smi_t) val);
3865 /* Handle a new message from the lower layer. */
3866 void ipmi_smi_msg_received(ipmi_smi_t intf,
3867 struct ipmi_smi_msg *msg)
3869 unsigned long flags = 0; /* keep us warning-free. */
3870 int run_to_completion;
3873 if ((msg->data_size >= 2)
3874 && (msg->data[0] == (IPMI_NETFN_APP_REQUEST << 2))
3875 && (msg->data[1] == IPMI_SEND_MSG_CMD)
3876 && (msg->user_data == NULL)) {
3878 * This is the local response to a command send, start
3879 * the timer for these. The user_data will not be
3880 * NULL if this is a response send, and we will let
3881 * response sends just go through.
3885 * Check for errors, if we get certain errors (ones
3886 * that mean basically we can try again later), we
3887 * ignore them and start the timer. Otherwise we
3888 * report the error immediately.
3890 if ((msg->rsp_size >= 3) && (msg->rsp[2] != 0)
3891 && (msg->rsp[2] != IPMI_NODE_BUSY_ERR)
3892 && (msg->rsp[2] != IPMI_LOST_ARBITRATION_ERR)
3893 && (msg->rsp[2] != IPMI_BUS_ERR)
3894 && (msg->rsp[2] != IPMI_NAK_ON_WRITE_ERR)) {
3895 int chan = msg->rsp[3] & 0xf;
3897 /* Got an error sending the message, handle it. */
3898 if (chan >= IPMI_MAX_CHANNELS)
3899 ; /* This shouldn't happen */
3900 else if ((intf->channels[chan].medium
3901 == IPMI_CHANNEL_MEDIUM_8023LAN)
3902 || (intf->channels[chan].medium
3903 == IPMI_CHANNEL_MEDIUM_ASYNC))
3904 ipmi_inc_stat(intf, sent_lan_command_errs);
3906 ipmi_inc_stat(intf, sent_ipmb_command_errs);
3907 intf_err_seq(intf, msg->msgid, msg->rsp[2]);
3909 /* The message was sent, start the timer. */
3910 intf_start_seq_timer(intf, msg->msgid);
3912 ipmi_free_smi_msg(msg);
3917 * To preserve message order, if the list is not empty, we
3918 * tack this message onto the end of the list.
3920 run_to_completion = intf->run_to_completion;
3921 if (!run_to_completion)
3922 spin_lock_irqsave(&intf->waiting_msgs_lock, flags);
3923 list_add_tail(&msg->link, &intf->waiting_msgs);
3924 if (!run_to_completion)
3925 spin_unlock_irqrestore(&intf->waiting_msgs_lock, flags);
3927 tasklet_schedule(&intf->recv_tasklet);
3931 EXPORT_SYMBOL(ipmi_smi_msg_received);
3933 void ipmi_smi_watchdog_pretimeout(ipmi_smi_t intf)
3935 atomic_set(&intf->watchdog_pretimeouts_to_deliver, 1);
3936 tasklet_schedule(&intf->recv_tasklet);
3938 EXPORT_SYMBOL(ipmi_smi_watchdog_pretimeout);
3940 static struct ipmi_smi_msg *
3941 smi_from_recv_msg(ipmi_smi_t intf, struct ipmi_recv_msg *recv_msg,
3942 unsigned char seq, long seqid)
3944 struct ipmi_smi_msg *smi_msg = ipmi_alloc_smi_msg();
3947 * If we can't allocate the message, then just return, we
3948 * get 4 retries, so this should be ok.
3952 memcpy(smi_msg->data, recv_msg->msg.data, recv_msg->msg.data_len);
3953 smi_msg->data_size = recv_msg->msg.data_len;
3954 smi_msg->msgid = STORE_SEQ_IN_MSGID(seq, seqid);
3960 for (m = 0; m < smi_msg->data_size; m++)
3961 printk(" %2.2x", smi_msg->data[m]);
3968 static void check_msg_timeout(ipmi_smi_t intf, struct seq_table *ent,
3969 struct list_head *timeouts, long timeout_period,
3970 int slot, unsigned long *flags)
3972 struct ipmi_recv_msg *msg;
3973 struct ipmi_smi_handlers *handlers;
3975 if (intf->intf_num == -1)
3981 ent->timeout -= timeout_period;
3982 if (ent->timeout > 0)
3985 if (ent->retries_left == 0) {
3986 /* The message has used all its retries. */
3988 msg = ent->recv_msg;
3989 list_add_tail(&msg->link, timeouts);
3991 ipmi_inc_stat(intf, timed_out_ipmb_broadcasts);
3992 else if (is_lan_addr(&ent->recv_msg->addr))
3993 ipmi_inc_stat(intf, timed_out_lan_commands);
3995 ipmi_inc_stat(intf, timed_out_ipmb_commands);
3997 struct ipmi_smi_msg *smi_msg;
3998 /* More retries, send again. */
4001 * Start with the max timer, set to normal timer after
4002 * the message is sent.
4004 ent->timeout = MAX_MSG_TIMEOUT;
4005 ent->retries_left--;
4006 smi_msg = smi_from_recv_msg(intf, ent->recv_msg, slot,
4009 if (is_lan_addr(&ent->recv_msg->addr))
4011 dropped_rexmit_lan_commands);
4014 dropped_rexmit_ipmb_commands);
4018 spin_unlock_irqrestore(&intf->seq_lock, *flags);
4021 * Send the new message. We send with a zero
4022 * priority. It timed out, I doubt time is that
4023 * critical now, and high priority messages are really
4024 * only for messages to the local MC, which don't get
4027 handlers = intf->handlers;
4029 if (is_lan_addr(&ent->recv_msg->addr))
4031 retransmitted_lan_commands);
4034 retransmitted_ipmb_commands);
4036 intf->handlers->sender(intf->send_info,
4039 ipmi_free_smi_msg(smi_msg);
4041 spin_lock_irqsave(&intf->seq_lock, *flags);
4045 static void ipmi_timeout_handler(long timeout_period)
4048 struct list_head timeouts;
4049 struct ipmi_recv_msg *msg, *msg2;
4050 unsigned long flags;
4054 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
4055 tasklet_schedule(&intf->recv_tasklet);
4058 * Go through the seq table and find any messages that
4059 * have timed out, putting them in the timeouts
4062 INIT_LIST_HEAD(&timeouts);
4063 spin_lock_irqsave(&intf->seq_lock, flags);
4064 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++)
4065 check_msg_timeout(intf, &(intf->seq_table[i]),
4066 &timeouts, timeout_period, i,
4068 spin_unlock_irqrestore(&intf->seq_lock, flags);
4070 list_for_each_entry_safe(msg, msg2, &timeouts, link)
4071 deliver_err_response(msg, IPMI_TIMEOUT_COMPLETION_CODE);
4074 * Maintenance mode handling. Check the timeout
4075 * optimistically before we claim the lock. It may
4076 * mean a timeout gets missed occasionally, but that
4077 * only means the timeout gets extended by one period
4078 * in that case. No big deal, and it avoids the lock
4081 if (intf->auto_maintenance_timeout > 0) {
4082 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
4083 if (intf->auto_maintenance_timeout > 0) {
4084 intf->auto_maintenance_timeout
4086 if (!intf->maintenance_mode
4087 && (intf->auto_maintenance_timeout <= 0)) {
4088 intf->maintenance_mode_enable = 0;
4089 maintenance_mode_update(intf);
4092 spin_unlock_irqrestore(&intf->maintenance_mode_lock,
4099 static void ipmi_request_event(void)
4102 struct ipmi_smi_handlers *handlers;
4106 * Called from the timer, no need to check if handlers is
4109 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
4110 /* No event requests when in maintenance mode. */
4111 if (intf->maintenance_mode_enable)
4114 handlers = intf->handlers;
4116 handlers->request_events(intf->send_info);
4121 static struct timer_list ipmi_timer;
4123 /* Call every ~1000 ms. */
4124 #define IPMI_TIMEOUT_TIME 1000
4126 /* How many jiffies does it take to get to the timeout time. */
4127 #define IPMI_TIMEOUT_JIFFIES ((IPMI_TIMEOUT_TIME * HZ) / 1000)
4130 * Request events from the queue every second (this is the number of
4131 * IPMI_TIMEOUT_TIMES between event requests). Hopefully, in the
4132 * future, IPMI will add a way to know immediately if an event is in
4133 * the queue and this silliness can go away.
4135 #define IPMI_REQUEST_EV_TIME (1000 / (IPMI_TIMEOUT_TIME))
4137 static atomic_t stop_operation;
4138 static unsigned int ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
4140 static void ipmi_timeout(unsigned long data)
4142 if (atomic_read(&stop_operation))
4146 if (ticks_to_req_ev == 0) {
4147 ipmi_request_event();
4148 ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
4151 ipmi_timeout_handler(IPMI_TIMEOUT_TIME);
4153 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
4157 static atomic_t smi_msg_inuse_count = ATOMIC_INIT(0);
4158 static atomic_t recv_msg_inuse_count = ATOMIC_INIT(0);
4160 /* FIXME - convert these to slabs. */
4161 static void free_smi_msg(struct ipmi_smi_msg *msg)
4163 atomic_dec(&smi_msg_inuse_count);
4167 struct ipmi_smi_msg *ipmi_alloc_smi_msg(void)
4169 struct ipmi_smi_msg *rv;
4170 rv = kmalloc(sizeof(struct ipmi_smi_msg), GFP_ATOMIC);
4172 rv->done = free_smi_msg;
4173 rv->user_data = NULL;
4174 atomic_inc(&smi_msg_inuse_count);
4178 EXPORT_SYMBOL(ipmi_alloc_smi_msg);
4180 static void free_recv_msg(struct ipmi_recv_msg *msg)
4182 atomic_dec(&recv_msg_inuse_count);
4186 static struct ipmi_recv_msg *ipmi_alloc_recv_msg(void)
4188 struct ipmi_recv_msg *rv;
4190 rv = kmalloc(sizeof(struct ipmi_recv_msg), GFP_ATOMIC);
4193 rv->done = free_recv_msg;
4194 atomic_inc(&recv_msg_inuse_count);
4199 void ipmi_free_recv_msg(struct ipmi_recv_msg *msg)
4202 kref_put(&msg->user->refcount, free_user);
4205 EXPORT_SYMBOL(ipmi_free_recv_msg);
4207 #ifdef CONFIG_IPMI_PANIC_EVENT
4209 static atomic_t panic_done_count = ATOMIC_INIT(0);
4211 static void dummy_smi_done_handler(struct ipmi_smi_msg *msg)
4213 atomic_dec(&panic_done_count);
4216 static void dummy_recv_done_handler(struct ipmi_recv_msg *msg)
4218 atomic_dec(&panic_done_count);
4222 * Inside a panic, send a message and wait for a response.
4224 static void ipmi_panic_request_and_wait(ipmi_smi_t intf,
4225 struct ipmi_addr *addr,
4226 struct kernel_ipmi_msg *msg)
4228 struct ipmi_smi_msg smi_msg;
4229 struct ipmi_recv_msg recv_msg;
4232 smi_msg.done = dummy_smi_done_handler;
4233 recv_msg.done = dummy_recv_done_handler;
4234 atomic_add(2, &panic_done_count);
4235 rv = i_ipmi_request(NULL,
4244 intf->channels[0].address,
4245 intf->channels[0].lun,
4246 0, 1); /* Don't retry, and don't wait. */
4248 atomic_sub(2, &panic_done_count);
4249 while (atomic_read(&panic_done_count) != 0)
4253 #ifdef CONFIG_IPMI_PANIC_STRING
4254 static void event_receiver_fetcher(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
4256 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
4257 && (msg->msg.netfn == IPMI_NETFN_SENSOR_EVENT_RESPONSE)
4258 && (msg->msg.cmd == IPMI_GET_EVENT_RECEIVER_CMD)
4259 && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) {
4260 /* A get event receiver command, save it. */
4261 intf->event_receiver = msg->msg.data[1];
4262 intf->event_receiver_lun = msg->msg.data[2] & 0x3;
4266 static void device_id_fetcher(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
4268 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
4269 && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
4270 && (msg->msg.cmd == IPMI_GET_DEVICE_ID_CMD)
4271 && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) {
4273 * A get device id command, save if we are an event
4274 * receiver or generator.
4276 intf->local_sel_device = (msg->msg.data[6] >> 2) & 1;
4277 intf->local_event_generator = (msg->msg.data[6] >> 5) & 1;
4282 static void send_panic_events(char *str)
4284 struct kernel_ipmi_msg msg;
4286 unsigned char data[16];
4287 struct ipmi_system_interface_addr *si;
4288 struct ipmi_addr addr;
4290 si = (struct ipmi_system_interface_addr *) &addr;
4291 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
4292 si->channel = IPMI_BMC_CHANNEL;
4295 /* Fill in an event telling that we have failed. */
4296 msg.netfn = 0x04; /* Sensor or Event. */
4297 msg.cmd = 2; /* Platform event command. */
4300 data[0] = 0x41; /* Kernel generator ID, IPMI table 5-4 */
4301 data[1] = 0x03; /* This is for IPMI 1.0. */
4302 data[2] = 0x20; /* OS Critical Stop, IPMI table 36-3 */
4303 data[4] = 0x6f; /* Sensor specific, IPMI table 36-1 */
4304 data[5] = 0xa1; /* Runtime stop OEM bytes 2 & 3. */
4307 * Put a few breadcrumbs in. Hopefully later we can add more things
4308 * to make the panic events more useful.
4316 /* For every registered interface, send the event. */
4317 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
4318 if (!intf->handlers)
4319 /* Interface is not ready. */
4322 intf->run_to_completion = 1;
4323 /* Send the event announcing the panic. */
4324 intf->handlers->set_run_to_completion(intf->send_info, 1);
4325 ipmi_panic_request_and_wait(intf, &addr, &msg);
4328 #ifdef CONFIG_IPMI_PANIC_STRING
4330 * On every interface, dump a bunch of OEM event holding the
4336 /* For every registered interface, send the event. */
4337 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
4339 struct ipmi_ipmb_addr *ipmb;
4342 if (intf->intf_num == -1)
4343 /* Interface was not ready yet. */
4347 * intf_num is used as an marker to tell if the
4348 * interface is valid. Thus we need a read barrier to
4349 * make sure data fetched before checking intf_num
4355 * First job here is to figure out where to send the
4356 * OEM events. There's no way in IPMI to send OEM
4357 * events using an event send command, so we have to
4358 * find the SEL to put them in and stick them in
4362 /* Get capabilities from the get device id. */
4363 intf->local_sel_device = 0;
4364 intf->local_event_generator = 0;
4365 intf->event_receiver = 0;
4367 /* Request the device info from the local MC. */
4368 msg.netfn = IPMI_NETFN_APP_REQUEST;
4369 msg.cmd = IPMI_GET_DEVICE_ID_CMD;
4372 intf->null_user_handler = device_id_fetcher;
4373 ipmi_panic_request_and_wait(intf, &addr, &msg);
4375 if (intf->local_event_generator) {
4376 /* Request the event receiver from the local MC. */
4377 msg.netfn = IPMI_NETFN_SENSOR_EVENT_REQUEST;
4378 msg.cmd = IPMI_GET_EVENT_RECEIVER_CMD;
4381 intf->null_user_handler = event_receiver_fetcher;
4382 ipmi_panic_request_and_wait(intf, &addr, &msg);
4384 intf->null_user_handler = NULL;
4387 * Validate the event receiver. The low bit must not
4388 * be 1 (it must be a valid IPMB address), it cannot
4389 * be zero, and it must not be my address.
4391 if (((intf->event_receiver & 1) == 0)
4392 && (intf->event_receiver != 0)
4393 && (intf->event_receiver != intf->channels[0].address)) {
4395 * The event receiver is valid, send an IPMB
4398 ipmb = (struct ipmi_ipmb_addr *) &addr;
4399 ipmb->addr_type = IPMI_IPMB_ADDR_TYPE;
4400 ipmb->channel = 0; /* FIXME - is this right? */
4401 ipmb->lun = intf->event_receiver_lun;
4402 ipmb->slave_addr = intf->event_receiver;
4403 } else if (intf->local_sel_device) {
4405 * The event receiver was not valid (or was
4406 * me), but I am an SEL device, just dump it
4409 si = (struct ipmi_system_interface_addr *) &addr;
4410 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
4411 si->channel = IPMI_BMC_CHANNEL;
4414 continue; /* No where to send the event. */
4416 msg.netfn = IPMI_NETFN_STORAGE_REQUEST; /* Storage. */
4417 msg.cmd = IPMI_ADD_SEL_ENTRY_CMD;
4423 int size = strlen(p);
4429 data[2] = 0xf0; /* OEM event without timestamp. */
4430 data[3] = intf->channels[0].address;
4431 data[4] = j++; /* sequence # */
4433 * Always give 11 bytes, so strncpy will fill
4434 * it with zeroes for me.
4436 strncpy(data+5, p, 11);
4439 ipmi_panic_request_and_wait(intf, &addr, &msg);
4442 #endif /* CONFIG_IPMI_PANIC_STRING */
4444 #endif /* CONFIG_IPMI_PANIC_EVENT */
4446 static int has_panicked;
4448 static int panic_event(struct notifier_block *this,
4449 unsigned long event,
4458 /* For every registered interface, set it to run to completion. */
4459 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
4460 if (!intf->handlers)
4461 /* Interface is not ready. */
4464 intf->run_to_completion = 1;
4465 intf->handlers->set_run_to_completion(intf->send_info, 1);
4468 #ifdef CONFIG_IPMI_PANIC_EVENT
4469 send_panic_events(ptr);
4475 static struct notifier_block panic_block = {
4476 .notifier_call = panic_event,
4478 .priority = 200 /* priority: INT_MAX >= x >= 0 */
4481 static int ipmi_init_msghandler(void)
4488 rv = driver_register(&ipmidriver.driver);
4490 printk(KERN_ERR PFX "Could not register IPMI driver\n");
4494 printk(KERN_INFO "ipmi message handler version "
4495 IPMI_DRIVER_VERSION "\n");
4497 #ifdef CONFIG_PROC_FS
4498 proc_ipmi_root = proc_mkdir("ipmi", NULL);
4499 if (!proc_ipmi_root) {
4500 printk(KERN_ERR PFX "Unable to create IPMI proc dir");
4504 #endif /* CONFIG_PROC_FS */
4506 setup_timer(&ipmi_timer, ipmi_timeout, 0);
4507 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
4509 atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
4516 static int __init ipmi_init_msghandler_mod(void)
4518 ipmi_init_msghandler();
4522 static void __exit cleanup_ipmi(void)
4529 atomic_notifier_chain_unregister(&panic_notifier_list, &panic_block);
4532 * This can't be called if any interfaces exist, so no worry
4533 * about shutting down the interfaces.
4537 * Tell the timer to stop, then wait for it to stop. This
4538 * avoids problems with race conditions removing the timer
4541 atomic_inc(&stop_operation);
4542 del_timer_sync(&ipmi_timer);
4544 #ifdef CONFIG_PROC_FS
4545 remove_proc_entry(proc_ipmi_root->name, NULL);
4546 #endif /* CONFIG_PROC_FS */
4548 driver_unregister(&ipmidriver.driver);
4552 /* Check for buffer leaks. */
4553 count = atomic_read(&smi_msg_inuse_count);
4555 printk(KERN_WARNING PFX "SMI message count %d at exit\n",
4557 count = atomic_read(&recv_msg_inuse_count);
4559 printk(KERN_WARNING PFX "recv message count %d at exit\n",
4562 module_exit(cleanup_ipmi);
4564 module_init(ipmi_init_msghandler_mod);
4565 MODULE_LICENSE("GPL");
4566 MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
4567 MODULE_DESCRIPTION("Incoming and outgoing message routing for an IPMI"
4569 MODULE_VERSION(IPMI_DRIVER_VERSION);