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 <linux/poll.h>
37 #include <linux/sched.h>
38 #include <linux/seq_file.h>
39 #include <linux/spinlock.h>
40 #include <linux/mutex.h>
41 #include <linux/slab.h>
42 #include <linux/ipmi.h>
43 #include <linux/ipmi_smi.h>
44 #include <linux/notifier.h>
45 #include <linux/init.h>
46 #include <linux/proc_fs.h>
47 #include <linux/rcupdate.h>
48 #include <linux/interrupt.h>
50 #define PFX "IPMI message handler: "
52 #define IPMI_DRIVER_VERSION "39.2"
54 static struct ipmi_recv_msg *ipmi_alloc_recv_msg(void);
55 static int ipmi_init_msghandler(void);
56 static void smi_recv_tasklet(unsigned long);
57 static void handle_new_recv_msgs(ipmi_smi_t intf);
58 static void need_waiter(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
77 /* Call every ~1000 ms. */
78 #define IPMI_TIMEOUT_TIME 1000
80 /* How many jiffies does it take to get to the timeout time. */
81 #define IPMI_TIMEOUT_JIFFIES ((IPMI_TIMEOUT_TIME * HZ) / 1000)
84 * Request events from the queue every second (this is the number of
85 * IPMI_TIMEOUT_TIMES between event requests). Hopefully, in the
86 * future, IPMI will add a way to know immediately if an event is in
87 * the queue and this silliness can go away.
89 #define IPMI_REQUEST_EV_TIME (1000 / (IPMI_TIMEOUT_TIME))
92 * The main "user" data structure.
95 struct list_head link;
97 /* Set to false when the user is destroyed. */
100 struct kref refcount;
102 /* The upper layer that handles receive messages. */
103 struct ipmi_user_hndl *handler;
106 /* The interface this user is bound to. */
109 /* Does this interface receive IPMI events? */
114 struct list_head link;
122 * This is used to form a linked lised during mass deletion.
123 * Since this is in an RCU list, we cannot use the link above
124 * or change any data until the RCU period completes. So we
125 * use this next variable during mass deletion so we can have
126 * a list and don't have to wait and restart the search on
127 * every individual deletion of a command.
129 struct cmd_rcvr *next;
133 unsigned int inuse : 1;
134 unsigned int broadcast : 1;
136 unsigned long timeout;
137 unsigned long orig_timeout;
138 unsigned int retries_left;
141 * To verify on an incoming send message response that this is
142 * the message that the response is for, we keep a sequence id
143 * and increment it every time we send a message.
148 * This is held so we can properly respond to the message on a
149 * timeout, and it is used to hold the temporary data for
150 * retransmission, too.
152 struct ipmi_recv_msg *recv_msg;
156 * Store the information in a msgid (long) to allow us to find a
157 * sequence table entry from the msgid.
159 #define STORE_SEQ_IN_MSGID(seq, seqid) (((seq&0xff)<<26) | (seqid&0x3ffffff))
161 #define GET_SEQ_FROM_MSGID(msgid, seq, seqid) \
163 seq = ((msgid >> 26) & 0x3f); \
164 seqid = (msgid & 0x3fffff); \
167 #define NEXT_SEQID(seqid) (((seqid) + 1) & 0x3fffff)
169 struct ipmi_channel {
170 unsigned char medium;
171 unsigned char protocol;
174 * My slave address. This is initialized to IPMI_BMC_SLAVE_ADDR,
175 * but may be changed by the user.
177 unsigned char address;
180 * My LUN. This should generally stay the SMS LUN, but just in
186 #ifdef CONFIG_PROC_FS
187 struct ipmi_proc_entry {
189 struct ipmi_proc_entry *next;
194 struct platform_device *dev;
195 struct ipmi_device_id id;
196 unsigned char guid[16];
199 struct kref refcount;
201 /* bmc device attributes */
202 struct device_attribute device_id_attr;
203 struct device_attribute provides_dev_sdrs_attr;
204 struct device_attribute revision_attr;
205 struct device_attribute firmware_rev_attr;
206 struct device_attribute version_attr;
207 struct device_attribute add_dev_support_attr;
208 struct device_attribute manufacturer_id_attr;
209 struct device_attribute product_id_attr;
210 struct device_attribute guid_attr;
211 struct device_attribute aux_firmware_rev_attr;
215 * Various statistics for IPMI, these index stats[] in the ipmi_smi
218 enum ipmi_stat_indexes {
219 /* Commands we got from the user that were invalid. */
220 IPMI_STAT_sent_invalid_commands = 0,
222 /* Commands we sent to the MC. */
223 IPMI_STAT_sent_local_commands,
225 /* Responses from the MC that were delivered to a user. */
226 IPMI_STAT_handled_local_responses,
228 /* Responses from the MC that were not delivered to a user. */
229 IPMI_STAT_unhandled_local_responses,
231 /* Commands we sent out to the IPMB bus. */
232 IPMI_STAT_sent_ipmb_commands,
234 /* Commands sent on the IPMB that had errors on the SEND CMD */
235 IPMI_STAT_sent_ipmb_command_errs,
237 /* Each retransmit increments this count. */
238 IPMI_STAT_retransmitted_ipmb_commands,
241 * When a message times out (runs out of retransmits) this is
244 IPMI_STAT_timed_out_ipmb_commands,
247 * This is like above, but for broadcasts. Broadcasts are
248 * *not* included in the above count (they are expected to
251 IPMI_STAT_timed_out_ipmb_broadcasts,
253 /* Responses I have sent to the IPMB bus. */
254 IPMI_STAT_sent_ipmb_responses,
256 /* The response was delivered to the user. */
257 IPMI_STAT_handled_ipmb_responses,
259 /* The response had invalid data in it. */
260 IPMI_STAT_invalid_ipmb_responses,
262 /* The response didn't have anyone waiting for it. */
263 IPMI_STAT_unhandled_ipmb_responses,
265 /* Commands we sent out to the IPMB bus. */
266 IPMI_STAT_sent_lan_commands,
268 /* Commands sent on the IPMB that had errors on the SEND CMD */
269 IPMI_STAT_sent_lan_command_errs,
271 /* Each retransmit increments this count. */
272 IPMI_STAT_retransmitted_lan_commands,
275 * When a message times out (runs out of retransmits) this is
278 IPMI_STAT_timed_out_lan_commands,
280 /* Responses I have sent to the IPMB bus. */
281 IPMI_STAT_sent_lan_responses,
283 /* The response was delivered to the user. */
284 IPMI_STAT_handled_lan_responses,
286 /* The response had invalid data in it. */
287 IPMI_STAT_invalid_lan_responses,
289 /* The response didn't have anyone waiting for it. */
290 IPMI_STAT_unhandled_lan_responses,
292 /* The command was delivered to the user. */
293 IPMI_STAT_handled_commands,
295 /* The command had invalid data in it. */
296 IPMI_STAT_invalid_commands,
298 /* The command didn't have anyone waiting for it. */
299 IPMI_STAT_unhandled_commands,
301 /* Invalid data in an event. */
302 IPMI_STAT_invalid_events,
304 /* Events that were received with the proper format. */
307 /* Retransmissions on IPMB that failed. */
308 IPMI_STAT_dropped_rexmit_ipmb_commands,
310 /* Retransmissions on LAN that failed. */
311 IPMI_STAT_dropped_rexmit_lan_commands,
313 /* This *must* remain last, add new values above this. */
318 #define IPMI_IPMB_NUM_SEQ 64
319 #define IPMI_MAX_CHANNELS 16
321 /* What interface number are we? */
324 struct kref refcount;
326 /* Used for a list of interfaces. */
327 struct list_head link;
330 * The list of upper layers that are using me. seq_lock
333 struct list_head users;
335 /* Information to supply to users. */
336 unsigned char ipmi_version_major;
337 unsigned char ipmi_version_minor;
339 /* Used for wake ups at startup. */
340 wait_queue_head_t waitq;
342 struct bmc_device *bmc;
347 * This is the lower-layer's sender routine. Note that you
348 * must either be holding the ipmi_interfaces_mutex or be in
349 * an umpreemptible region to use this. You must fetch the
350 * value into a local variable and make sure it is not NULL.
352 struct ipmi_smi_handlers *handlers;
355 #ifdef CONFIG_PROC_FS
356 /* A list of proc entries for this interface. */
357 struct mutex proc_entry_lock;
358 struct ipmi_proc_entry *proc_entries;
361 /* Driver-model device for the system interface. */
362 struct device *si_dev;
365 * A table of sequence numbers for this interface. We use the
366 * sequence numbers for IPMB messages that go out of the
367 * interface to match them up with their responses. A routine
368 * is called periodically to time the items in this list.
371 struct seq_table seq_table[IPMI_IPMB_NUM_SEQ];
375 * Messages queued for delivery. If delivery fails (out of memory
376 * for instance), They will stay in here to be processed later in a
377 * periodic timer interrupt. The tasklet is for handling received
378 * messages directly from the handler.
380 spinlock_t waiting_msgs_lock;
381 struct list_head waiting_msgs;
382 atomic_t watchdog_pretimeouts_to_deliver;
383 struct tasklet_struct recv_tasklet;
386 * The list of command receivers that are registered for commands
389 struct mutex cmd_rcvrs_mutex;
390 struct list_head cmd_rcvrs;
393 * Events that were queues because no one was there to receive
396 spinlock_t events_lock; /* For dealing with event stuff. */
397 struct list_head waiting_events;
398 unsigned int waiting_events_count; /* How many events in queue? */
399 char delivering_events;
400 char event_msg_printed;
401 atomic_t event_waiters;
402 unsigned int ticks_to_req_ev;
403 int last_needs_timer;
406 * The event receiver for my BMC, only really used at panic
407 * shutdown as a place to store this.
409 unsigned char event_receiver;
410 unsigned char event_receiver_lun;
411 unsigned char local_sel_device;
412 unsigned char local_event_generator;
414 /* For handling of maintenance mode. */
415 int maintenance_mode;
416 bool maintenance_mode_enable;
417 int auto_maintenance_timeout;
418 spinlock_t maintenance_mode_lock; /* Used in a timer... */
421 * A cheap hack, if this is non-null and a message to an
422 * interface comes in with a NULL user, call this routine with
423 * it. Note that the message will still be freed by the
424 * caller. This only works on the system interface.
426 void (*null_user_handler)(ipmi_smi_t intf, struct ipmi_recv_msg *msg);
429 * When we are scanning the channels for an SMI, this will
430 * tell which channel we are scanning.
434 /* Channel information */
435 struct ipmi_channel channels[IPMI_MAX_CHANNELS];
438 struct proc_dir_entry *proc_dir;
439 char proc_dir_name[10];
441 atomic_t stats[IPMI_NUM_STATS];
444 * run_to_completion duplicate of smb_info, smi_info
445 * and ipmi_serial_info structures. Used to decrease numbers of
446 * parameters passed by "low" level IPMI code.
448 int run_to_completion;
450 #define to_si_intf_from_dev(device) container_of(device, struct ipmi_smi, dev)
453 * The driver model view of the IPMI messaging driver.
455 static struct platform_driver ipmidriver = {
458 .bus = &platform_bus_type
461 static DEFINE_MUTEX(ipmidriver_mutex);
463 static LIST_HEAD(ipmi_interfaces);
464 static DEFINE_MUTEX(ipmi_interfaces_mutex);
467 * List of watchers that want to know when smi's are added and deleted.
469 static LIST_HEAD(smi_watchers);
470 static DEFINE_MUTEX(smi_watchers_mutex);
472 #define ipmi_inc_stat(intf, stat) \
473 atomic_inc(&(intf)->stats[IPMI_STAT_ ## stat])
474 #define ipmi_get_stat(intf, stat) \
475 ((unsigned int) atomic_read(&(intf)->stats[IPMI_STAT_ ## stat]))
477 static int is_lan_addr(struct ipmi_addr *addr)
479 return addr->addr_type == IPMI_LAN_ADDR_TYPE;
482 static int is_ipmb_addr(struct ipmi_addr *addr)
484 return addr->addr_type == IPMI_IPMB_ADDR_TYPE;
487 static int is_ipmb_bcast_addr(struct ipmi_addr *addr)
489 return addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE;
492 static void free_recv_msg_list(struct list_head *q)
494 struct ipmi_recv_msg *msg, *msg2;
496 list_for_each_entry_safe(msg, msg2, q, link) {
497 list_del(&msg->link);
498 ipmi_free_recv_msg(msg);
502 static void free_smi_msg_list(struct list_head *q)
504 struct ipmi_smi_msg *msg, *msg2;
506 list_for_each_entry_safe(msg, msg2, q, link) {
507 list_del(&msg->link);
508 ipmi_free_smi_msg(msg);
512 static void clean_up_interface_data(ipmi_smi_t intf)
515 struct cmd_rcvr *rcvr, *rcvr2;
516 struct list_head list;
518 tasklet_kill(&intf->recv_tasklet);
520 free_smi_msg_list(&intf->waiting_msgs);
521 free_recv_msg_list(&intf->waiting_events);
524 * Wholesale remove all the entries from the list in the
525 * interface and wait for RCU to know that none are in use.
527 mutex_lock(&intf->cmd_rcvrs_mutex);
528 INIT_LIST_HEAD(&list);
529 list_splice_init_rcu(&intf->cmd_rcvrs, &list, synchronize_rcu);
530 mutex_unlock(&intf->cmd_rcvrs_mutex);
532 list_for_each_entry_safe(rcvr, rcvr2, &list, link)
535 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
536 if ((intf->seq_table[i].inuse)
537 && (intf->seq_table[i].recv_msg))
538 ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
542 static void intf_free(struct kref *ref)
544 ipmi_smi_t intf = container_of(ref, struct ipmi_smi, refcount);
546 clean_up_interface_data(intf);
550 struct watcher_entry {
553 struct list_head link;
556 int ipmi_smi_watcher_register(struct ipmi_smi_watcher *watcher)
559 LIST_HEAD(to_deliver);
560 struct watcher_entry *e, *e2;
562 mutex_lock(&smi_watchers_mutex);
564 mutex_lock(&ipmi_interfaces_mutex);
566 /* Build a list of things to deliver. */
567 list_for_each_entry(intf, &ipmi_interfaces, link) {
568 if (intf->intf_num == -1)
570 e = kmalloc(sizeof(*e), GFP_KERNEL);
573 kref_get(&intf->refcount);
575 e->intf_num = intf->intf_num;
576 list_add_tail(&e->link, &to_deliver);
579 /* We will succeed, so add it to the list. */
580 list_add(&watcher->link, &smi_watchers);
582 mutex_unlock(&ipmi_interfaces_mutex);
584 list_for_each_entry_safe(e, e2, &to_deliver, link) {
586 watcher->new_smi(e->intf_num, e->intf->si_dev);
587 kref_put(&e->intf->refcount, intf_free);
591 mutex_unlock(&smi_watchers_mutex);
596 mutex_unlock(&ipmi_interfaces_mutex);
597 mutex_unlock(&smi_watchers_mutex);
598 list_for_each_entry_safe(e, e2, &to_deliver, link) {
600 kref_put(&e->intf->refcount, intf_free);
605 EXPORT_SYMBOL(ipmi_smi_watcher_register);
607 int ipmi_smi_watcher_unregister(struct ipmi_smi_watcher *watcher)
609 mutex_lock(&smi_watchers_mutex);
610 list_del(&(watcher->link));
611 mutex_unlock(&smi_watchers_mutex);
614 EXPORT_SYMBOL(ipmi_smi_watcher_unregister);
617 * Must be called with smi_watchers_mutex held.
620 call_smi_watchers(int i, struct device *dev)
622 struct ipmi_smi_watcher *w;
624 list_for_each_entry(w, &smi_watchers, link) {
625 if (try_module_get(w->owner)) {
627 module_put(w->owner);
633 ipmi_addr_equal(struct ipmi_addr *addr1, struct ipmi_addr *addr2)
635 if (addr1->addr_type != addr2->addr_type)
638 if (addr1->channel != addr2->channel)
641 if (addr1->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
642 struct ipmi_system_interface_addr *smi_addr1
643 = (struct ipmi_system_interface_addr *) addr1;
644 struct ipmi_system_interface_addr *smi_addr2
645 = (struct ipmi_system_interface_addr *) addr2;
646 return (smi_addr1->lun == smi_addr2->lun);
649 if (is_ipmb_addr(addr1) || is_ipmb_bcast_addr(addr1)) {
650 struct ipmi_ipmb_addr *ipmb_addr1
651 = (struct ipmi_ipmb_addr *) addr1;
652 struct ipmi_ipmb_addr *ipmb_addr2
653 = (struct ipmi_ipmb_addr *) addr2;
655 return ((ipmb_addr1->slave_addr == ipmb_addr2->slave_addr)
656 && (ipmb_addr1->lun == ipmb_addr2->lun));
659 if (is_lan_addr(addr1)) {
660 struct ipmi_lan_addr *lan_addr1
661 = (struct ipmi_lan_addr *) addr1;
662 struct ipmi_lan_addr *lan_addr2
663 = (struct ipmi_lan_addr *) addr2;
665 return ((lan_addr1->remote_SWID == lan_addr2->remote_SWID)
666 && (lan_addr1->local_SWID == lan_addr2->local_SWID)
667 && (lan_addr1->session_handle
668 == lan_addr2->session_handle)
669 && (lan_addr1->lun == lan_addr2->lun));
675 int ipmi_validate_addr(struct ipmi_addr *addr, int len)
677 if (len < sizeof(struct ipmi_system_interface_addr))
680 if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
681 if (addr->channel != IPMI_BMC_CHANNEL)
686 if ((addr->channel == IPMI_BMC_CHANNEL)
687 || (addr->channel >= IPMI_MAX_CHANNELS)
688 || (addr->channel < 0))
691 if (is_ipmb_addr(addr) || is_ipmb_bcast_addr(addr)) {
692 if (len < sizeof(struct ipmi_ipmb_addr))
697 if (is_lan_addr(addr)) {
698 if (len < sizeof(struct ipmi_lan_addr))
705 EXPORT_SYMBOL(ipmi_validate_addr);
707 unsigned int ipmi_addr_length(int addr_type)
709 if (addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
710 return sizeof(struct ipmi_system_interface_addr);
712 if ((addr_type == IPMI_IPMB_ADDR_TYPE)
713 || (addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE))
714 return sizeof(struct ipmi_ipmb_addr);
716 if (addr_type == IPMI_LAN_ADDR_TYPE)
717 return sizeof(struct ipmi_lan_addr);
721 EXPORT_SYMBOL(ipmi_addr_length);
723 static void deliver_response(struct ipmi_recv_msg *msg)
726 ipmi_smi_t intf = msg->user_msg_data;
728 /* Special handling for NULL users. */
729 if (intf->null_user_handler) {
730 intf->null_user_handler(intf, msg);
731 ipmi_inc_stat(intf, handled_local_responses);
733 /* No handler, so give up. */
734 ipmi_inc_stat(intf, unhandled_local_responses);
736 ipmi_free_recv_msg(msg);
738 ipmi_user_t user = msg->user;
739 user->handler->ipmi_recv_hndl(msg, user->handler_data);
744 deliver_err_response(struct ipmi_recv_msg *msg, int err)
746 msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
747 msg->msg_data[0] = err;
748 msg->msg.netfn |= 1; /* Convert to a response. */
749 msg->msg.data_len = 1;
750 msg->msg.data = msg->msg_data;
751 deliver_response(msg);
755 * Find the next sequence number not being used and add the given
756 * message with the given timeout to the sequence table. This must be
757 * called with the interface's seq_lock held.
759 static int intf_next_seq(ipmi_smi_t intf,
760 struct ipmi_recv_msg *recv_msg,
761 unsigned long timeout,
770 for (i = intf->curr_seq; (i+1)%IPMI_IPMB_NUM_SEQ != intf->curr_seq;
771 i = (i+1)%IPMI_IPMB_NUM_SEQ) {
772 if (!intf->seq_table[i].inuse)
776 if (!intf->seq_table[i].inuse) {
777 intf->seq_table[i].recv_msg = recv_msg;
780 * Start with the maximum timeout, when the send response
781 * comes in we will start the real timer.
783 intf->seq_table[i].timeout = MAX_MSG_TIMEOUT;
784 intf->seq_table[i].orig_timeout = timeout;
785 intf->seq_table[i].retries_left = retries;
786 intf->seq_table[i].broadcast = broadcast;
787 intf->seq_table[i].inuse = 1;
788 intf->seq_table[i].seqid = NEXT_SEQID(intf->seq_table[i].seqid);
790 *seqid = intf->seq_table[i].seqid;
791 intf->curr_seq = (i+1)%IPMI_IPMB_NUM_SEQ;
801 * Return the receive message for the given sequence number and
802 * release the sequence number so it can be reused. Some other data
803 * is passed in to be sure the message matches up correctly (to help
804 * guard against message coming in after their timeout and the
805 * sequence number being reused).
807 static int intf_find_seq(ipmi_smi_t intf,
812 struct ipmi_addr *addr,
813 struct ipmi_recv_msg **recv_msg)
818 if (seq >= IPMI_IPMB_NUM_SEQ)
821 spin_lock_irqsave(&(intf->seq_lock), flags);
822 if (intf->seq_table[seq].inuse) {
823 struct ipmi_recv_msg *msg = intf->seq_table[seq].recv_msg;
825 if ((msg->addr.channel == channel) && (msg->msg.cmd == cmd)
826 && (msg->msg.netfn == netfn)
827 && (ipmi_addr_equal(addr, &(msg->addr)))) {
829 intf->seq_table[seq].inuse = 0;
833 spin_unlock_irqrestore(&(intf->seq_lock), flags);
839 /* Start the timer for a specific sequence table entry. */
840 static int intf_start_seq_timer(ipmi_smi_t intf,
849 GET_SEQ_FROM_MSGID(msgid, seq, seqid);
851 spin_lock_irqsave(&(intf->seq_lock), flags);
853 * We do this verification because the user can be deleted
854 * while a message is outstanding.
856 if ((intf->seq_table[seq].inuse)
857 && (intf->seq_table[seq].seqid == seqid)) {
858 struct seq_table *ent = &(intf->seq_table[seq]);
859 ent->timeout = ent->orig_timeout;
862 spin_unlock_irqrestore(&(intf->seq_lock), flags);
867 /* Got an error for the send message for a specific sequence number. */
868 static int intf_err_seq(ipmi_smi_t intf,
876 struct ipmi_recv_msg *msg = NULL;
879 GET_SEQ_FROM_MSGID(msgid, seq, seqid);
881 spin_lock_irqsave(&(intf->seq_lock), flags);
883 * We do this verification because the user can be deleted
884 * while a message is outstanding.
886 if ((intf->seq_table[seq].inuse)
887 && (intf->seq_table[seq].seqid == seqid)) {
888 struct seq_table *ent = &(intf->seq_table[seq]);
894 spin_unlock_irqrestore(&(intf->seq_lock), flags);
897 deliver_err_response(msg, err);
903 int ipmi_create_user(unsigned int if_num,
904 struct ipmi_user_hndl *handler,
909 ipmi_user_t new_user;
914 * There is no module usecount here, because it's not
915 * required. Since this can only be used by and called from
916 * other modules, they will implicitly use this module, and
917 * thus this can't be removed unless the other modules are
925 * Make sure the driver is actually initialized, this handles
926 * problems with initialization order.
929 rv = ipmi_init_msghandler();
934 * The init code doesn't return an error if it was turned
935 * off, but it won't initialize. Check that.
941 new_user = kmalloc(sizeof(*new_user), GFP_KERNEL);
945 mutex_lock(&ipmi_interfaces_mutex);
946 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
947 if (intf->intf_num == if_num)
950 /* Not found, return an error */
955 /* Note that each existing user holds a refcount to the interface. */
956 kref_get(&intf->refcount);
958 kref_init(&new_user->refcount);
959 new_user->handler = handler;
960 new_user->handler_data = handler_data;
961 new_user->intf = intf;
962 new_user->gets_events = false;
964 if (!try_module_get(intf->handlers->owner)) {
969 if (intf->handlers->inc_usecount) {
970 rv = intf->handlers->inc_usecount(intf->send_info);
972 module_put(intf->handlers->owner);
978 * Hold the lock so intf->handlers is guaranteed to be good
981 mutex_unlock(&ipmi_interfaces_mutex);
983 new_user->valid = true;
984 spin_lock_irqsave(&intf->seq_lock, flags);
985 list_add_rcu(&new_user->link, &intf->users);
986 spin_unlock_irqrestore(&intf->seq_lock, flags);
987 if (handler->ipmi_watchdog_pretimeout) {
988 /* User wants pretimeouts, so make sure to watch for them. */
989 if (atomic_inc_return(&intf->event_waiters) == 1)
996 kref_put(&intf->refcount, intf_free);
998 mutex_unlock(&ipmi_interfaces_mutex);
1002 EXPORT_SYMBOL(ipmi_create_user);
1004 int ipmi_get_smi_info(int if_num, struct ipmi_smi_info *data)
1008 struct ipmi_smi_handlers *handlers;
1010 mutex_lock(&ipmi_interfaces_mutex);
1011 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
1012 if (intf->intf_num == if_num)
1015 /* Not found, return an error */
1017 mutex_unlock(&ipmi_interfaces_mutex);
1021 handlers = intf->handlers;
1023 if (handlers->get_smi_info)
1024 rv = handlers->get_smi_info(intf->send_info, data);
1025 mutex_unlock(&ipmi_interfaces_mutex);
1029 EXPORT_SYMBOL(ipmi_get_smi_info);
1031 static void free_user(struct kref *ref)
1033 ipmi_user_t user = container_of(ref, struct ipmi_user, refcount);
1037 int ipmi_destroy_user(ipmi_user_t user)
1039 ipmi_smi_t intf = user->intf;
1041 unsigned long flags;
1042 struct cmd_rcvr *rcvr;
1043 struct cmd_rcvr *rcvrs = NULL;
1045 user->valid = false;
1047 if (user->handler->ipmi_watchdog_pretimeout)
1048 atomic_dec(&intf->event_waiters);
1050 if (user->gets_events)
1051 atomic_dec(&intf->event_waiters);
1053 /* Remove the user from the interface's sequence table. */
1054 spin_lock_irqsave(&intf->seq_lock, flags);
1055 list_del_rcu(&user->link);
1057 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
1058 if (intf->seq_table[i].inuse
1059 && (intf->seq_table[i].recv_msg->user == user)) {
1060 intf->seq_table[i].inuse = 0;
1061 ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
1064 spin_unlock_irqrestore(&intf->seq_lock, flags);
1067 * Remove the user from the command receiver's table. First
1068 * we build a list of everything (not using the standard link,
1069 * since other things may be using it till we do
1070 * synchronize_rcu()) then free everything in that list.
1072 mutex_lock(&intf->cmd_rcvrs_mutex);
1073 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
1074 if (rcvr->user == user) {
1075 list_del_rcu(&rcvr->link);
1080 mutex_unlock(&intf->cmd_rcvrs_mutex);
1088 mutex_lock(&ipmi_interfaces_mutex);
1089 if (intf->handlers) {
1090 module_put(intf->handlers->owner);
1091 if (intf->handlers->dec_usecount)
1092 intf->handlers->dec_usecount(intf->send_info);
1094 mutex_unlock(&ipmi_interfaces_mutex);
1096 kref_put(&intf->refcount, intf_free);
1098 kref_put(&user->refcount, free_user);
1102 EXPORT_SYMBOL(ipmi_destroy_user);
1104 void ipmi_get_version(ipmi_user_t user,
1105 unsigned char *major,
1106 unsigned char *minor)
1108 *major = user->intf->ipmi_version_major;
1109 *minor = user->intf->ipmi_version_minor;
1111 EXPORT_SYMBOL(ipmi_get_version);
1113 int ipmi_set_my_address(ipmi_user_t user,
1114 unsigned int channel,
1115 unsigned char address)
1117 if (channel >= IPMI_MAX_CHANNELS)
1119 user->intf->channels[channel].address = address;
1122 EXPORT_SYMBOL(ipmi_set_my_address);
1124 int ipmi_get_my_address(ipmi_user_t user,
1125 unsigned int channel,
1126 unsigned char *address)
1128 if (channel >= IPMI_MAX_CHANNELS)
1130 *address = user->intf->channels[channel].address;
1133 EXPORT_SYMBOL(ipmi_get_my_address);
1135 int ipmi_set_my_LUN(ipmi_user_t user,
1136 unsigned int channel,
1139 if (channel >= IPMI_MAX_CHANNELS)
1141 user->intf->channels[channel].lun = LUN & 0x3;
1144 EXPORT_SYMBOL(ipmi_set_my_LUN);
1146 int ipmi_get_my_LUN(ipmi_user_t user,
1147 unsigned int channel,
1148 unsigned char *address)
1150 if (channel >= IPMI_MAX_CHANNELS)
1152 *address = user->intf->channels[channel].lun;
1155 EXPORT_SYMBOL(ipmi_get_my_LUN);
1157 int ipmi_get_maintenance_mode(ipmi_user_t user)
1160 unsigned long flags;
1162 spin_lock_irqsave(&user->intf->maintenance_mode_lock, flags);
1163 mode = user->intf->maintenance_mode;
1164 spin_unlock_irqrestore(&user->intf->maintenance_mode_lock, flags);
1168 EXPORT_SYMBOL(ipmi_get_maintenance_mode);
1170 static void maintenance_mode_update(ipmi_smi_t intf)
1172 if (intf->handlers->set_maintenance_mode)
1173 intf->handlers->set_maintenance_mode(
1174 intf->send_info, intf->maintenance_mode_enable);
1177 int ipmi_set_maintenance_mode(ipmi_user_t user, int mode)
1180 unsigned long flags;
1181 ipmi_smi_t intf = user->intf;
1183 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
1184 if (intf->maintenance_mode != mode) {
1186 case IPMI_MAINTENANCE_MODE_AUTO:
1187 intf->maintenance_mode_enable
1188 = (intf->auto_maintenance_timeout > 0);
1191 case IPMI_MAINTENANCE_MODE_OFF:
1192 intf->maintenance_mode_enable = false;
1195 case IPMI_MAINTENANCE_MODE_ON:
1196 intf->maintenance_mode_enable = true;
1203 intf->maintenance_mode = mode;
1205 maintenance_mode_update(intf);
1208 spin_unlock_irqrestore(&intf->maintenance_mode_lock, flags);
1212 EXPORT_SYMBOL(ipmi_set_maintenance_mode);
1214 int ipmi_set_gets_events(ipmi_user_t user, bool val)
1216 unsigned long flags;
1217 ipmi_smi_t intf = user->intf;
1218 struct ipmi_recv_msg *msg, *msg2;
1219 struct list_head msgs;
1221 INIT_LIST_HEAD(&msgs);
1223 spin_lock_irqsave(&intf->events_lock, flags);
1224 if (user->gets_events == val)
1227 user->gets_events = val;
1230 if (atomic_inc_return(&intf->event_waiters) == 1)
1233 atomic_dec(&intf->event_waiters);
1236 if (intf->delivering_events)
1238 * Another thread is delivering events for this, so
1239 * let it handle any new events.
1243 /* Deliver any queued events. */
1244 while (user->gets_events && !list_empty(&intf->waiting_events)) {
1245 list_for_each_entry_safe(msg, msg2, &intf->waiting_events, link)
1246 list_move_tail(&msg->link, &msgs);
1247 intf->waiting_events_count = 0;
1248 if (intf->event_msg_printed) {
1249 printk(KERN_WARNING PFX "Event queue no longer"
1251 intf->event_msg_printed = 0;
1254 intf->delivering_events = 1;
1255 spin_unlock_irqrestore(&intf->events_lock, flags);
1257 list_for_each_entry_safe(msg, msg2, &msgs, link) {
1259 kref_get(&user->refcount);
1260 deliver_response(msg);
1263 spin_lock_irqsave(&intf->events_lock, flags);
1264 intf->delivering_events = 0;
1268 spin_unlock_irqrestore(&intf->events_lock, flags);
1272 EXPORT_SYMBOL(ipmi_set_gets_events);
1274 static struct cmd_rcvr *find_cmd_rcvr(ipmi_smi_t intf,
1275 unsigned char netfn,
1279 struct cmd_rcvr *rcvr;
1281 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
1282 if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)
1283 && (rcvr->chans & (1 << chan)))
1289 static int is_cmd_rcvr_exclusive(ipmi_smi_t intf,
1290 unsigned char netfn,
1294 struct cmd_rcvr *rcvr;
1296 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
1297 if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)
1298 && (rcvr->chans & chans))
1304 int ipmi_register_for_cmd(ipmi_user_t user,
1305 unsigned char netfn,
1309 ipmi_smi_t intf = user->intf;
1310 struct cmd_rcvr *rcvr;
1314 rcvr = kmalloc(sizeof(*rcvr), GFP_KERNEL);
1318 rcvr->netfn = netfn;
1319 rcvr->chans = chans;
1322 mutex_lock(&intf->cmd_rcvrs_mutex);
1323 /* Make sure the command/netfn is not already registered. */
1324 if (!is_cmd_rcvr_exclusive(intf, netfn, cmd, chans)) {
1329 if (atomic_inc_return(&intf->event_waiters) == 1)
1332 list_add_rcu(&rcvr->link, &intf->cmd_rcvrs);
1335 mutex_unlock(&intf->cmd_rcvrs_mutex);
1341 EXPORT_SYMBOL(ipmi_register_for_cmd);
1343 int ipmi_unregister_for_cmd(ipmi_user_t user,
1344 unsigned char netfn,
1348 ipmi_smi_t intf = user->intf;
1349 struct cmd_rcvr *rcvr;
1350 struct cmd_rcvr *rcvrs = NULL;
1351 int i, rv = -ENOENT;
1353 mutex_lock(&intf->cmd_rcvrs_mutex);
1354 for (i = 0; i < IPMI_NUM_CHANNELS; i++) {
1355 if (((1 << i) & chans) == 0)
1357 rcvr = find_cmd_rcvr(intf, netfn, cmd, i);
1360 if (rcvr->user == user) {
1362 rcvr->chans &= ~chans;
1363 if (rcvr->chans == 0) {
1364 list_del_rcu(&rcvr->link);
1370 mutex_unlock(&intf->cmd_rcvrs_mutex);
1373 atomic_dec(&intf->event_waiters);
1380 EXPORT_SYMBOL(ipmi_unregister_for_cmd);
1382 static unsigned char
1383 ipmb_checksum(unsigned char *data, int size)
1385 unsigned char csum = 0;
1387 for (; size > 0; size--, data++)
1393 static inline void format_ipmb_msg(struct ipmi_smi_msg *smi_msg,
1394 struct kernel_ipmi_msg *msg,
1395 struct ipmi_ipmb_addr *ipmb_addr,
1397 unsigned char ipmb_seq,
1399 unsigned char source_address,
1400 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] = ipmb_addr->channel;
1409 smi_msg->data[3] = 0;
1410 smi_msg->data[i+3] = ipmb_addr->slave_addr;
1411 smi_msg->data[i+4] = (msg->netfn << 2) | (ipmb_addr->lun & 0x3);
1412 smi_msg->data[i+5] = ipmb_checksum(&(smi_msg->data[i+3]), 2);
1413 smi_msg->data[i+6] = source_address;
1414 smi_msg->data[i+7] = (ipmb_seq << 2) | source_lun;
1415 smi_msg->data[i+8] = msg->cmd;
1417 /* Now tack on the data to the message. */
1418 if (msg->data_len > 0)
1419 memcpy(&(smi_msg->data[i+9]), msg->data,
1421 smi_msg->data_size = msg->data_len + 9;
1423 /* Now calculate the checksum and tack it on. */
1424 smi_msg->data[i+smi_msg->data_size]
1425 = ipmb_checksum(&(smi_msg->data[i+6]),
1426 smi_msg->data_size-6);
1429 * Add on the checksum size and the offset from the
1432 smi_msg->data_size += 1 + i;
1434 smi_msg->msgid = msgid;
1437 static inline void format_lan_msg(struct ipmi_smi_msg *smi_msg,
1438 struct kernel_ipmi_msg *msg,
1439 struct ipmi_lan_addr *lan_addr,
1441 unsigned char ipmb_seq,
1442 unsigned char source_lun)
1444 /* Format the IPMB header data. */
1445 smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
1446 smi_msg->data[1] = IPMI_SEND_MSG_CMD;
1447 smi_msg->data[2] = lan_addr->channel;
1448 smi_msg->data[3] = lan_addr->session_handle;
1449 smi_msg->data[4] = lan_addr->remote_SWID;
1450 smi_msg->data[5] = (msg->netfn << 2) | (lan_addr->lun & 0x3);
1451 smi_msg->data[6] = ipmb_checksum(&(smi_msg->data[4]), 2);
1452 smi_msg->data[7] = lan_addr->local_SWID;
1453 smi_msg->data[8] = (ipmb_seq << 2) | source_lun;
1454 smi_msg->data[9] = msg->cmd;
1456 /* Now tack on the data to the message. */
1457 if (msg->data_len > 0)
1458 memcpy(&(smi_msg->data[10]), msg->data,
1460 smi_msg->data_size = msg->data_len + 10;
1462 /* Now calculate the checksum and tack it on. */
1463 smi_msg->data[smi_msg->data_size]
1464 = ipmb_checksum(&(smi_msg->data[7]),
1465 smi_msg->data_size-7);
1468 * Add on the checksum size and the offset from the
1471 smi_msg->data_size += 1;
1473 smi_msg->msgid = msgid;
1477 * Separate from ipmi_request so that the user does not have to be
1478 * supplied in certain circumstances (mainly at panic time). If
1479 * messages are supplied, they will be freed, even if an error
1482 static int i_ipmi_request(ipmi_user_t user,
1484 struct ipmi_addr *addr,
1486 struct kernel_ipmi_msg *msg,
1487 void *user_msg_data,
1489 struct ipmi_recv_msg *supplied_recv,
1491 unsigned char source_address,
1492 unsigned char source_lun,
1494 unsigned int retry_time_ms)
1497 struct ipmi_smi_msg *smi_msg;
1498 struct ipmi_recv_msg *recv_msg;
1499 unsigned long flags;
1500 struct ipmi_smi_handlers *handlers;
1504 recv_msg = supplied_recv;
1506 recv_msg = ipmi_alloc_recv_msg();
1507 if (recv_msg == NULL)
1510 recv_msg->user_msg_data = user_msg_data;
1513 smi_msg = (struct ipmi_smi_msg *) supplied_smi;
1515 smi_msg = ipmi_alloc_smi_msg();
1516 if (smi_msg == NULL) {
1517 ipmi_free_recv_msg(recv_msg);
1523 handlers = intf->handlers;
1529 recv_msg->user = user;
1531 kref_get(&user->refcount);
1532 recv_msg->msgid = msgid;
1534 * Store the message to send in the receive message so timeout
1535 * responses can get the proper response data.
1537 recv_msg->msg = *msg;
1539 if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
1540 struct ipmi_system_interface_addr *smi_addr;
1542 if (msg->netfn & 1) {
1543 /* Responses are not allowed to the SMI. */
1548 smi_addr = (struct ipmi_system_interface_addr *) addr;
1549 if (smi_addr->lun > 3) {
1550 ipmi_inc_stat(intf, sent_invalid_commands);
1555 memcpy(&recv_msg->addr, smi_addr, sizeof(*smi_addr));
1557 if ((msg->netfn == IPMI_NETFN_APP_REQUEST)
1558 && ((msg->cmd == IPMI_SEND_MSG_CMD)
1559 || (msg->cmd == IPMI_GET_MSG_CMD)
1560 || (msg->cmd == IPMI_READ_EVENT_MSG_BUFFER_CMD))) {
1562 * We don't let the user do these, since we manage
1563 * the sequence numbers.
1565 ipmi_inc_stat(intf, sent_invalid_commands);
1570 if (((msg->netfn == IPMI_NETFN_APP_REQUEST)
1571 && ((msg->cmd == IPMI_COLD_RESET_CMD)
1572 || (msg->cmd == IPMI_WARM_RESET_CMD)))
1573 || (msg->netfn == IPMI_NETFN_FIRMWARE_REQUEST)) {
1574 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
1575 intf->auto_maintenance_timeout
1576 = IPMI_MAINTENANCE_MODE_TIMEOUT;
1577 if (!intf->maintenance_mode
1578 && !intf->maintenance_mode_enable) {
1579 intf->maintenance_mode_enable = true;
1580 maintenance_mode_update(intf);
1582 spin_unlock_irqrestore(&intf->maintenance_mode_lock,
1586 if ((msg->data_len + 2) > IPMI_MAX_MSG_LENGTH) {
1587 ipmi_inc_stat(intf, sent_invalid_commands);
1592 smi_msg->data[0] = (msg->netfn << 2) | (smi_addr->lun & 0x3);
1593 smi_msg->data[1] = msg->cmd;
1594 smi_msg->msgid = msgid;
1595 smi_msg->user_data = recv_msg;
1596 if (msg->data_len > 0)
1597 memcpy(&(smi_msg->data[2]), msg->data, msg->data_len);
1598 smi_msg->data_size = msg->data_len + 2;
1599 ipmi_inc_stat(intf, sent_local_commands);
1600 } else if (is_ipmb_addr(addr) || is_ipmb_bcast_addr(addr)) {
1601 struct ipmi_ipmb_addr *ipmb_addr;
1602 unsigned char ipmb_seq;
1606 if (addr->channel >= IPMI_MAX_CHANNELS) {
1607 ipmi_inc_stat(intf, sent_invalid_commands);
1612 if (intf->channels[addr->channel].medium
1613 != IPMI_CHANNEL_MEDIUM_IPMB) {
1614 ipmi_inc_stat(intf, sent_invalid_commands);
1620 if (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE)
1621 retries = 0; /* Don't retry broadcasts. */
1625 if (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE) {
1627 * Broadcasts add a zero at the beginning of the
1628 * message, but otherwise is the same as an IPMB
1631 addr->addr_type = IPMI_IPMB_ADDR_TYPE;
1636 /* Default to 1 second retries. */
1637 if (retry_time_ms == 0)
1638 retry_time_ms = 1000;
1641 * 9 for the header and 1 for the checksum, plus
1642 * possibly one for the broadcast.
1644 if ((msg->data_len + 10 + broadcast) > IPMI_MAX_MSG_LENGTH) {
1645 ipmi_inc_stat(intf, sent_invalid_commands);
1650 ipmb_addr = (struct ipmi_ipmb_addr *) addr;
1651 if (ipmb_addr->lun > 3) {
1652 ipmi_inc_stat(intf, sent_invalid_commands);
1657 memcpy(&recv_msg->addr, ipmb_addr, sizeof(*ipmb_addr));
1659 if (recv_msg->msg.netfn & 0x1) {
1661 * It's a response, so use the user's sequence
1664 ipmi_inc_stat(intf, sent_ipmb_responses);
1665 format_ipmb_msg(smi_msg, msg, ipmb_addr, msgid,
1667 source_address, source_lun);
1670 * Save the receive message so we can use it
1671 * to deliver the response.
1673 smi_msg->user_data = recv_msg;
1675 /* It's a command, so get a sequence for it. */
1677 spin_lock_irqsave(&(intf->seq_lock), flags);
1680 * Create a sequence number with a 1 second
1681 * timeout and 4 retries.
1683 rv = intf_next_seq(intf,
1692 * We have used up all the sequence numbers,
1693 * probably, so abort.
1695 spin_unlock_irqrestore(&(intf->seq_lock),
1700 ipmi_inc_stat(intf, sent_ipmb_commands);
1703 * Store the sequence number in the message,
1704 * so that when the send message response
1705 * comes back we can start the timer.
1707 format_ipmb_msg(smi_msg, msg, ipmb_addr,
1708 STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
1709 ipmb_seq, broadcast,
1710 source_address, source_lun);
1713 * Copy the message into the recv message data, so we
1714 * can retransmit it later if necessary.
1716 memcpy(recv_msg->msg_data, smi_msg->data,
1717 smi_msg->data_size);
1718 recv_msg->msg.data = recv_msg->msg_data;
1719 recv_msg->msg.data_len = smi_msg->data_size;
1722 * We don't unlock until here, because we need
1723 * to copy the completed message into the
1724 * recv_msg before we release the lock.
1725 * Otherwise, race conditions may bite us. I
1726 * know that's pretty paranoid, but I prefer
1729 spin_unlock_irqrestore(&(intf->seq_lock), flags);
1731 } else if (is_lan_addr(addr)) {
1732 struct ipmi_lan_addr *lan_addr;
1733 unsigned char ipmb_seq;
1736 if (addr->channel >= IPMI_MAX_CHANNELS) {
1737 ipmi_inc_stat(intf, sent_invalid_commands);
1742 if ((intf->channels[addr->channel].medium
1743 != IPMI_CHANNEL_MEDIUM_8023LAN)
1744 && (intf->channels[addr->channel].medium
1745 != IPMI_CHANNEL_MEDIUM_ASYNC)) {
1746 ipmi_inc_stat(intf, sent_invalid_commands);
1753 /* Default to 1 second retries. */
1754 if (retry_time_ms == 0)
1755 retry_time_ms = 1000;
1757 /* 11 for the header and 1 for the checksum. */
1758 if ((msg->data_len + 12) > IPMI_MAX_MSG_LENGTH) {
1759 ipmi_inc_stat(intf, sent_invalid_commands);
1764 lan_addr = (struct ipmi_lan_addr *) addr;
1765 if (lan_addr->lun > 3) {
1766 ipmi_inc_stat(intf, sent_invalid_commands);
1771 memcpy(&recv_msg->addr, lan_addr, sizeof(*lan_addr));
1773 if (recv_msg->msg.netfn & 0x1) {
1775 * It's a response, so use the user's sequence
1778 ipmi_inc_stat(intf, sent_lan_responses);
1779 format_lan_msg(smi_msg, msg, lan_addr, msgid,
1783 * Save the receive message so we can use it
1784 * to deliver the response.
1786 smi_msg->user_data = recv_msg;
1788 /* It's a command, so get a sequence for it. */
1790 spin_lock_irqsave(&(intf->seq_lock), flags);
1793 * Create a sequence number with a 1 second
1794 * timeout and 4 retries.
1796 rv = intf_next_seq(intf,
1805 * We have used up all the sequence numbers,
1806 * probably, so abort.
1808 spin_unlock_irqrestore(&(intf->seq_lock),
1813 ipmi_inc_stat(intf, sent_lan_commands);
1816 * Store the sequence number in the message,
1817 * so that when the send message response
1818 * comes back we can start the timer.
1820 format_lan_msg(smi_msg, msg, lan_addr,
1821 STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
1822 ipmb_seq, source_lun);
1825 * Copy the message into the recv message data, so we
1826 * can retransmit it later if necessary.
1828 memcpy(recv_msg->msg_data, smi_msg->data,
1829 smi_msg->data_size);
1830 recv_msg->msg.data = recv_msg->msg_data;
1831 recv_msg->msg.data_len = smi_msg->data_size;
1834 * We don't unlock until here, because we need
1835 * to copy the completed message into the
1836 * recv_msg before we release the lock.
1837 * Otherwise, race conditions may bite us. I
1838 * know that's pretty paranoid, but I prefer
1841 spin_unlock_irqrestore(&(intf->seq_lock), flags);
1844 /* Unknown address type. */
1845 ipmi_inc_stat(intf, sent_invalid_commands);
1853 for (m = 0; m < smi_msg->data_size; m++)
1854 printk(" %2.2x", smi_msg->data[m]);
1859 handlers->sender(intf->send_info, smi_msg, priority);
1866 ipmi_free_smi_msg(smi_msg);
1867 ipmi_free_recv_msg(recv_msg);
1871 static int check_addr(ipmi_smi_t intf,
1872 struct ipmi_addr *addr,
1873 unsigned char *saddr,
1876 if (addr->channel >= IPMI_MAX_CHANNELS)
1878 *lun = intf->channels[addr->channel].lun;
1879 *saddr = intf->channels[addr->channel].address;
1883 int ipmi_request_settime(ipmi_user_t user,
1884 struct ipmi_addr *addr,
1886 struct kernel_ipmi_msg *msg,
1887 void *user_msg_data,
1890 unsigned int retry_time_ms)
1892 unsigned char saddr = 0, lun = 0;
1897 rv = check_addr(user->intf, addr, &saddr, &lun);
1900 return i_ipmi_request(user,
1913 EXPORT_SYMBOL(ipmi_request_settime);
1915 int ipmi_request_supply_msgs(ipmi_user_t user,
1916 struct ipmi_addr *addr,
1918 struct kernel_ipmi_msg *msg,
1919 void *user_msg_data,
1921 struct ipmi_recv_msg *supplied_recv,
1924 unsigned char saddr = 0, lun = 0;
1929 rv = check_addr(user->intf, addr, &saddr, &lun);
1932 return i_ipmi_request(user,
1945 EXPORT_SYMBOL(ipmi_request_supply_msgs);
1947 #ifdef CONFIG_PROC_FS
1948 static int smi_ipmb_proc_show(struct seq_file *m, void *v)
1950 ipmi_smi_t intf = m->private;
1953 seq_printf(m, "%x", intf->channels[0].address);
1954 for (i = 1; i < IPMI_MAX_CHANNELS; i++)
1955 seq_printf(m, " %x", intf->channels[i].address);
1956 return seq_putc(m, '\n');
1959 static int smi_ipmb_proc_open(struct inode *inode, struct file *file)
1961 return single_open(file, smi_ipmb_proc_show, PDE_DATA(inode));
1964 static const struct file_operations smi_ipmb_proc_ops = {
1965 .open = smi_ipmb_proc_open,
1967 .llseek = seq_lseek,
1968 .release = single_release,
1971 static int smi_version_proc_show(struct seq_file *m, void *v)
1973 ipmi_smi_t intf = m->private;
1975 return seq_printf(m, "%u.%u\n",
1976 ipmi_version_major(&intf->bmc->id),
1977 ipmi_version_minor(&intf->bmc->id));
1980 static int smi_version_proc_open(struct inode *inode, struct file *file)
1982 return single_open(file, smi_version_proc_show, PDE_DATA(inode));
1985 static const struct file_operations smi_version_proc_ops = {
1986 .open = smi_version_proc_open,
1988 .llseek = seq_lseek,
1989 .release = single_release,
1992 static int smi_stats_proc_show(struct seq_file *m, void *v)
1994 ipmi_smi_t intf = m->private;
1996 seq_printf(m, "sent_invalid_commands: %u\n",
1997 ipmi_get_stat(intf, sent_invalid_commands));
1998 seq_printf(m, "sent_local_commands: %u\n",
1999 ipmi_get_stat(intf, sent_local_commands));
2000 seq_printf(m, "handled_local_responses: %u\n",
2001 ipmi_get_stat(intf, handled_local_responses));
2002 seq_printf(m, "unhandled_local_responses: %u\n",
2003 ipmi_get_stat(intf, unhandled_local_responses));
2004 seq_printf(m, "sent_ipmb_commands: %u\n",
2005 ipmi_get_stat(intf, sent_ipmb_commands));
2006 seq_printf(m, "sent_ipmb_command_errs: %u\n",
2007 ipmi_get_stat(intf, sent_ipmb_command_errs));
2008 seq_printf(m, "retransmitted_ipmb_commands: %u\n",
2009 ipmi_get_stat(intf, retransmitted_ipmb_commands));
2010 seq_printf(m, "timed_out_ipmb_commands: %u\n",
2011 ipmi_get_stat(intf, timed_out_ipmb_commands));
2012 seq_printf(m, "timed_out_ipmb_broadcasts: %u\n",
2013 ipmi_get_stat(intf, timed_out_ipmb_broadcasts));
2014 seq_printf(m, "sent_ipmb_responses: %u\n",
2015 ipmi_get_stat(intf, sent_ipmb_responses));
2016 seq_printf(m, "handled_ipmb_responses: %u\n",
2017 ipmi_get_stat(intf, handled_ipmb_responses));
2018 seq_printf(m, "invalid_ipmb_responses: %u\n",
2019 ipmi_get_stat(intf, invalid_ipmb_responses));
2020 seq_printf(m, "unhandled_ipmb_responses: %u\n",
2021 ipmi_get_stat(intf, unhandled_ipmb_responses));
2022 seq_printf(m, "sent_lan_commands: %u\n",
2023 ipmi_get_stat(intf, sent_lan_commands));
2024 seq_printf(m, "sent_lan_command_errs: %u\n",
2025 ipmi_get_stat(intf, sent_lan_command_errs));
2026 seq_printf(m, "retransmitted_lan_commands: %u\n",
2027 ipmi_get_stat(intf, retransmitted_lan_commands));
2028 seq_printf(m, "timed_out_lan_commands: %u\n",
2029 ipmi_get_stat(intf, timed_out_lan_commands));
2030 seq_printf(m, "sent_lan_responses: %u\n",
2031 ipmi_get_stat(intf, sent_lan_responses));
2032 seq_printf(m, "handled_lan_responses: %u\n",
2033 ipmi_get_stat(intf, handled_lan_responses));
2034 seq_printf(m, "invalid_lan_responses: %u\n",
2035 ipmi_get_stat(intf, invalid_lan_responses));
2036 seq_printf(m, "unhandled_lan_responses: %u\n",
2037 ipmi_get_stat(intf, unhandled_lan_responses));
2038 seq_printf(m, "handled_commands: %u\n",
2039 ipmi_get_stat(intf, handled_commands));
2040 seq_printf(m, "invalid_commands: %u\n",
2041 ipmi_get_stat(intf, invalid_commands));
2042 seq_printf(m, "unhandled_commands: %u\n",
2043 ipmi_get_stat(intf, unhandled_commands));
2044 seq_printf(m, "invalid_events: %u\n",
2045 ipmi_get_stat(intf, invalid_events));
2046 seq_printf(m, "events: %u\n",
2047 ipmi_get_stat(intf, events));
2048 seq_printf(m, "failed rexmit LAN msgs: %u\n",
2049 ipmi_get_stat(intf, dropped_rexmit_lan_commands));
2050 seq_printf(m, "failed rexmit IPMB msgs: %u\n",
2051 ipmi_get_stat(intf, dropped_rexmit_ipmb_commands));
2055 static int smi_stats_proc_open(struct inode *inode, struct file *file)
2057 return single_open(file, smi_stats_proc_show, PDE_DATA(inode));
2060 static const struct file_operations smi_stats_proc_ops = {
2061 .open = smi_stats_proc_open,
2063 .llseek = seq_lseek,
2064 .release = single_release,
2066 #endif /* CONFIG_PROC_FS */
2068 int ipmi_smi_add_proc_entry(ipmi_smi_t smi, char *name,
2069 const struct file_operations *proc_ops,
2073 #ifdef CONFIG_PROC_FS
2074 struct proc_dir_entry *file;
2075 struct ipmi_proc_entry *entry;
2077 /* Create a list element. */
2078 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2081 entry->name = kstrdup(name, GFP_KERNEL);
2087 file = proc_create_data(name, 0, smi->proc_dir, proc_ops, data);
2093 mutex_lock(&smi->proc_entry_lock);
2094 /* Stick it on the list. */
2095 entry->next = smi->proc_entries;
2096 smi->proc_entries = entry;
2097 mutex_unlock(&smi->proc_entry_lock);
2099 #endif /* CONFIG_PROC_FS */
2103 EXPORT_SYMBOL(ipmi_smi_add_proc_entry);
2105 static int add_proc_entries(ipmi_smi_t smi, int num)
2109 #ifdef CONFIG_PROC_FS
2110 sprintf(smi->proc_dir_name, "%d", num);
2111 smi->proc_dir = proc_mkdir(smi->proc_dir_name, proc_ipmi_root);
2116 rv = ipmi_smi_add_proc_entry(smi, "stats",
2117 &smi_stats_proc_ops,
2121 rv = ipmi_smi_add_proc_entry(smi, "ipmb",
2126 rv = ipmi_smi_add_proc_entry(smi, "version",
2127 &smi_version_proc_ops,
2129 #endif /* CONFIG_PROC_FS */
2134 static void remove_proc_entries(ipmi_smi_t smi)
2136 #ifdef CONFIG_PROC_FS
2137 struct ipmi_proc_entry *entry;
2139 mutex_lock(&smi->proc_entry_lock);
2140 while (smi->proc_entries) {
2141 entry = smi->proc_entries;
2142 smi->proc_entries = entry->next;
2144 remove_proc_entry(entry->name, smi->proc_dir);
2148 mutex_unlock(&smi->proc_entry_lock);
2149 remove_proc_entry(smi->proc_dir_name, proc_ipmi_root);
2150 #endif /* CONFIG_PROC_FS */
2153 static int __find_bmc_guid(struct device *dev, void *data)
2155 unsigned char *id = data;
2156 struct bmc_device *bmc = dev_get_drvdata(dev);
2157 return memcmp(bmc->guid, id, 16) == 0;
2160 static struct bmc_device *ipmi_find_bmc_guid(struct device_driver *drv,
2161 unsigned char *guid)
2165 dev = driver_find_device(drv, NULL, guid, __find_bmc_guid);
2167 return dev_get_drvdata(dev);
2172 struct prod_dev_id {
2173 unsigned int product_id;
2174 unsigned char device_id;
2177 static int __find_bmc_prod_dev_id(struct device *dev, void *data)
2179 struct prod_dev_id *id = data;
2180 struct bmc_device *bmc = dev_get_drvdata(dev);
2182 return (bmc->id.product_id == id->product_id
2183 && bmc->id.device_id == id->device_id);
2186 static struct bmc_device *ipmi_find_bmc_prod_dev_id(
2187 struct device_driver *drv,
2188 unsigned int product_id, unsigned char device_id)
2190 struct prod_dev_id id = {
2191 .product_id = product_id,
2192 .device_id = device_id,
2196 dev = driver_find_device(drv, NULL, &id, __find_bmc_prod_dev_id);
2198 return dev_get_drvdata(dev);
2203 static ssize_t device_id_show(struct device *dev,
2204 struct device_attribute *attr,
2207 struct bmc_device *bmc = dev_get_drvdata(dev);
2209 return snprintf(buf, 10, "%u\n", bmc->id.device_id);
2212 static ssize_t provides_dev_sdrs_show(struct device *dev,
2213 struct device_attribute *attr,
2216 struct bmc_device *bmc = dev_get_drvdata(dev);
2218 return snprintf(buf, 10, "%u\n",
2219 (bmc->id.device_revision & 0x80) >> 7);
2222 static ssize_t revision_show(struct device *dev, struct device_attribute *attr,
2225 struct bmc_device *bmc = dev_get_drvdata(dev);
2227 return snprintf(buf, 20, "%u\n",
2228 bmc->id.device_revision & 0x0F);
2231 static ssize_t firmware_rev_show(struct device *dev,
2232 struct device_attribute *attr,
2235 struct bmc_device *bmc = dev_get_drvdata(dev);
2237 return snprintf(buf, 20, "%u.%x\n", bmc->id.firmware_revision_1,
2238 bmc->id.firmware_revision_2);
2241 static ssize_t ipmi_version_show(struct device *dev,
2242 struct device_attribute *attr,
2245 struct bmc_device *bmc = dev_get_drvdata(dev);
2247 return snprintf(buf, 20, "%u.%u\n",
2248 ipmi_version_major(&bmc->id),
2249 ipmi_version_minor(&bmc->id));
2252 static ssize_t add_dev_support_show(struct device *dev,
2253 struct device_attribute *attr,
2256 struct bmc_device *bmc = dev_get_drvdata(dev);
2258 return snprintf(buf, 10, "0x%02x\n",
2259 bmc->id.additional_device_support);
2262 static ssize_t manufacturer_id_show(struct device *dev,
2263 struct device_attribute *attr,
2266 struct bmc_device *bmc = dev_get_drvdata(dev);
2268 return snprintf(buf, 20, "0x%6.6x\n", bmc->id.manufacturer_id);
2271 static ssize_t product_id_show(struct device *dev,
2272 struct device_attribute *attr,
2275 struct bmc_device *bmc = dev_get_drvdata(dev);
2277 return snprintf(buf, 10, "0x%4.4x\n", bmc->id.product_id);
2280 static ssize_t aux_firmware_rev_show(struct device *dev,
2281 struct device_attribute *attr,
2284 struct bmc_device *bmc = dev_get_drvdata(dev);
2286 return snprintf(buf, 21, "0x%02x 0x%02x 0x%02x 0x%02x\n",
2287 bmc->id.aux_firmware_revision[3],
2288 bmc->id.aux_firmware_revision[2],
2289 bmc->id.aux_firmware_revision[1],
2290 bmc->id.aux_firmware_revision[0]);
2293 static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
2296 struct bmc_device *bmc = dev_get_drvdata(dev);
2298 return snprintf(buf, 100, "%Lx%Lx\n",
2299 (long long) bmc->guid[0],
2300 (long long) bmc->guid[8]);
2303 static void remove_files(struct bmc_device *bmc)
2308 device_remove_file(&bmc->dev->dev,
2309 &bmc->device_id_attr);
2310 device_remove_file(&bmc->dev->dev,
2311 &bmc->provides_dev_sdrs_attr);
2312 device_remove_file(&bmc->dev->dev,
2313 &bmc->revision_attr);
2314 device_remove_file(&bmc->dev->dev,
2315 &bmc->firmware_rev_attr);
2316 device_remove_file(&bmc->dev->dev,
2317 &bmc->version_attr);
2318 device_remove_file(&bmc->dev->dev,
2319 &bmc->add_dev_support_attr);
2320 device_remove_file(&bmc->dev->dev,
2321 &bmc->manufacturer_id_attr);
2322 device_remove_file(&bmc->dev->dev,
2323 &bmc->product_id_attr);
2325 if (bmc->id.aux_firmware_revision_set)
2326 device_remove_file(&bmc->dev->dev,
2327 &bmc->aux_firmware_rev_attr);
2329 device_remove_file(&bmc->dev->dev,
2334 cleanup_bmc_device(struct kref *ref)
2336 struct bmc_device *bmc;
2338 bmc = container_of(ref, struct bmc_device, refcount);
2341 platform_device_unregister(bmc->dev);
2345 static void ipmi_bmc_unregister(ipmi_smi_t intf)
2347 struct bmc_device *bmc = intf->bmc;
2349 if (intf->sysfs_name) {
2350 sysfs_remove_link(&intf->si_dev->kobj, intf->sysfs_name);
2351 kfree(intf->sysfs_name);
2352 intf->sysfs_name = NULL;
2354 if (intf->my_dev_name) {
2355 sysfs_remove_link(&bmc->dev->dev.kobj, intf->my_dev_name);
2356 kfree(intf->my_dev_name);
2357 intf->my_dev_name = NULL;
2360 mutex_lock(&ipmidriver_mutex);
2361 kref_put(&bmc->refcount, cleanup_bmc_device);
2363 mutex_unlock(&ipmidriver_mutex);
2366 static int create_files(struct bmc_device *bmc)
2370 bmc->device_id_attr.attr.name = "device_id";
2371 bmc->device_id_attr.attr.mode = S_IRUGO;
2372 bmc->device_id_attr.show = device_id_show;
2373 sysfs_attr_init(&bmc->device_id_attr.attr);
2375 bmc->provides_dev_sdrs_attr.attr.name = "provides_device_sdrs";
2376 bmc->provides_dev_sdrs_attr.attr.mode = S_IRUGO;
2377 bmc->provides_dev_sdrs_attr.show = provides_dev_sdrs_show;
2378 sysfs_attr_init(&bmc->provides_dev_sdrs_attr.attr);
2380 bmc->revision_attr.attr.name = "revision";
2381 bmc->revision_attr.attr.mode = S_IRUGO;
2382 bmc->revision_attr.show = revision_show;
2383 sysfs_attr_init(&bmc->revision_attr.attr);
2385 bmc->firmware_rev_attr.attr.name = "firmware_revision";
2386 bmc->firmware_rev_attr.attr.mode = S_IRUGO;
2387 bmc->firmware_rev_attr.show = firmware_rev_show;
2388 sysfs_attr_init(&bmc->firmware_rev_attr.attr);
2390 bmc->version_attr.attr.name = "ipmi_version";
2391 bmc->version_attr.attr.mode = S_IRUGO;
2392 bmc->version_attr.show = ipmi_version_show;
2393 sysfs_attr_init(&bmc->version_attr.attr);
2395 bmc->add_dev_support_attr.attr.name = "additional_device_support";
2396 bmc->add_dev_support_attr.attr.mode = S_IRUGO;
2397 bmc->add_dev_support_attr.show = add_dev_support_show;
2398 sysfs_attr_init(&bmc->add_dev_support_attr.attr);
2400 bmc->manufacturer_id_attr.attr.name = "manufacturer_id";
2401 bmc->manufacturer_id_attr.attr.mode = S_IRUGO;
2402 bmc->manufacturer_id_attr.show = manufacturer_id_show;
2403 sysfs_attr_init(&bmc->manufacturer_id_attr.attr);
2405 bmc->product_id_attr.attr.name = "product_id";
2406 bmc->product_id_attr.attr.mode = S_IRUGO;
2407 bmc->product_id_attr.show = product_id_show;
2408 sysfs_attr_init(&bmc->product_id_attr.attr);
2410 bmc->guid_attr.attr.name = "guid";
2411 bmc->guid_attr.attr.mode = S_IRUGO;
2412 bmc->guid_attr.show = guid_show;
2413 sysfs_attr_init(&bmc->guid_attr.attr);
2415 bmc->aux_firmware_rev_attr.attr.name = "aux_firmware_revision";
2416 bmc->aux_firmware_rev_attr.attr.mode = S_IRUGO;
2417 bmc->aux_firmware_rev_attr.show = aux_firmware_rev_show;
2418 sysfs_attr_init(&bmc->aux_firmware_rev_attr.attr);
2420 err = device_create_file(&bmc->dev->dev,
2421 &bmc->device_id_attr);
2424 err = device_create_file(&bmc->dev->dev,
2425 &bmc->provides_dev_sdrs_attr);
2428 err = device_create_file(&bmc->dev->dev,
2429 &bmc->revision_attr);
2432 err = device_create_file(&bmc->dev->dev,
2433 &bmc->firmware_rev_attr);
2436 err = device_create_file(&bmc->dev->dev,
2437 &bmc->version_attr);
2440 err = device_create_file(&bmc->dev->dev,
2441 &bmc->add_dev_support_attr);
2444 err = device_create_file(&bmc->dev->dev,
2445 &bmc->manufacturer_id_attr);
2448 err = device_create_file(&bmc->dev->dev,
2449 &bmc->product_id_attr);
2452 if (bmc->id.aux_firmware_revision_set) {
2453 err = device_create_file(&bmc->dev->dev,
2454 &bmc->aux_firmware_rev_attr);
2458 if (bmc->guid_set) {
2459 err = device_create_file(&bmc->dev->dev,
2468 if (bmc->id.aux_firmware_revision_set)
2469 device_remove_file(&bmc->dev->dev,
2470 &bmc->aux_firmware_rev_attr);
2472 device_remove_file(&bmc->dev->dev,
2473 &bmc->product_id_attr);
2475 device_remove_file(&bmc->dev->dev,
2476 &bmc->manufacturer_id_attr);
2478 device_remove_file(&bmc->dev->dev,
2479 &bmc->add_dev_support_attr);
2481 device_remove_file(&bmc->dev->dev,
2482 &bmc->version_attr);
2484 device_remove_file(&bmc->dev->dev,
2485 &bmc->firmware_rev_attr);
2487 device_remove_file(&bmc->dev->dev,
2488 &bmc->revision_attr);
2490 device_remove_file(&bmc->dev->dev,
2491 &bmc->provides_dev_sdrs_attr);
2493 device_remove_file(&bmc->dev->dev,
2494 &bmc->device_id_attr);
2499 static int ipmi_bmc_register(ipmi_smi_t intf, int ifnum,
2500 const char *sysfs_name)
2503 struct bmc_device *bmc = intf->bmc;
2504 struct bmc_device *old_bmc;
2508 mutex_lock(&ipmidriver_mutex);
2511 * Try to find if there is an bmc_device struct
2512 * representing the interfaced BMC already
2515 old_bmc = ipmi_find_bmc_guid(&ipmidriver.driver, bmc->guid);
2517 old_bmc = ipmi_find_bmc_prod_dev_id(&ipmidriver.driver,
2522 * If there is already an bmc_device, free the new one,
2523 * otherwise register the new BMC device
2527 intf->bmc = old_bmc;
2530 kref_get(&bmc->refcount);
2531 mutex_unlock(&ipmidriver_mutex);
2534 "ipmi: interfacing existing BMC (man_id: 0x%6.6x,"
2535 " prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
2536 bmc->id.manufacturer_id,
2541 unsigned char orig_dev_id = bmc->id.device_id;
2542 int warn_printed = 0;
2544 snprintf(name, sizeof(name),
2545 "ipmi_bmc.%4.4x", bmc->id.product_id);
2547 while (ipmi_find_bmc_prod_dev_id(&ipmidriver.driver,
2549 bmc->id.device_id)) {
2550 if (!warn_printed) {
2551 printk(KERN_WARNING PFX
2552 "This machine has two different BMCs"
2553 " with the same product id and device"
2554 " id. This is an error in the"
2555 " firmware, but incrementing the"
2556 " device id to work around the problem."
2557 " Prod ID = 0x%x, Dev ID = 0x%x\n",
2558 bmc->id.product_id, bmc->id.device_id);
2561 bmc->id.device_id++; /* Wraps at 255 */
2562 if (bmc->id.device_id == orig_dev_id) {
2564 "Out of device ids!\n");
2569 bmc->dev = platform_device_alloc(name, bmc->id.device_id);
2571 mutex_unlock(&ipmidriver_mutex);
2574 " Unable to allocate platform device\n");
2577 bmc->dev->dev.driver = &ipmidriver.driver;
2578 dev_set_drvdata(&bmc->dev->dev, bmc);
2579 kref_init(&bmc->refcount);
2581 rv = platform_device_add(bmc->dev);
2582 mutex_unlock(&ipmidriver_mutex);
2584 platform_device_put(bmc->dev);
2588 " Unable to register bmc device: %d\n",
2591 * Don't go to out_err, you can only do that if
2592 * the device is registered already.
2597 rv = create_files(bmc);
2599 mutex_lock(&ipmidriver_mutex);
2600 platform_device_unregister(bmc->dev);
2601 mutex_unlock(&ipmidriver_mutex);
2606 dev_info(intf->si_dev, "Found new BMC (man_id: 0x%6.6x, "
2607 "prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
2608 bmc->id.manufacturer_id,
2614 * create symlink from system interface device to bmc device
2617 intf->sysfs_name = kstrdup(sysfs_name, GFP_KERNEL);
2618 if (!intf->sysfs_name) {
2621 "ipmi_msghandler: allocate link to BMC: %d\n",
2626 rv = sysfs_create_link(&intf->si_dev->kobj,
2627 &bmc->dev->dev.kobj, intf->sysfs_name);
2629 kfree(intf->sysfs_name);
2630 intf->sysfs_name = NULL;
2632 "ipmi_msghandler: Unable to create bmc symlink: %d\n",
2637 size = snprintf(dummy, 0, "ipmi%d", ifnum);
2638 intf->my_dev_name = kmalloc(size+1, GFP_KERNEL);
2639 if (!intf->my_dev_name) {
2640 kfree(intf->sysfs_name);
2641 intf->sysfs_name = NULL;
2644 "ipmi_msghandler: allocate link from BMC: %d\n",
2648 snprintf(intf->my_dev_name, size+1, "ipmi%d", ifnum);
2650 rv = sysfs_create_link(&bmc->dev->dev.kobj, &intf->si_dev->kobj,
2653 kfree(intf->sysfs_name);
2654 intf->sysfs_name = NULL;
2655 kfree(intf->my_dev_name);
2656 intf->my_dev_name = NULL;
2659 " Unable to create symlink to bmc: %d\n",
2667 ipmi_bmc_unregister(intf);
2672 send_guid_cmd(ipmi_smi_t intf, int chan)
2674 struct kernel_ipmi_msg msg;
2675 struct ipmi_system_interface_addr si;
2677 si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2678 si.channel = IPMI_BMC_CHANNEL;
2681 msg.netfn = IPMI_NETFN_APP_REQUEST;
2682 msg.cmd = IPMI_GET_DEVICE_GUID_CMD;
2685 return i_ipmi_request(NULL,
2687 (struct ipmi_addr *) &si,
2694 intf->channels[0].address,
2695 intf->channels[0].lun,
2700 guid_handler(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
2702 if ((msg->addr.addr_type != IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
2703 || (msg->msg.netfn != IPMI_NETFN_APP_RESPONSE)
2704 || (msg->msg.cmd != IPMI_GET_DEVICE_GUID_CMD))
2708 if (msg->msg.data[0] != 0) {
2709 /* Error from getting the GUID, the BMC doesn't have one. */
2710 intf->bmc->guid_set = 0;
2714 if (msg->msg.data_len < 17) {
2715 intf->bmc->guid_set = 0;
2716 printk(KERN_WARNING PFX
2717 "guid_handler: The GUID response from the BMC was too"
2718 " short, it was %d but should have been 17. Assuming"
2719 " GUID is not available.\n",
2724 memcpy(intf->bmc->guid, msg->msg.data, 16);
2725 intf->bmc->guid_set = 1;
2727 wake_up(&intf->waitq);
2731 get_guid(ipmi_smi_t intf)
2735 intf->bmc->guid_set = 0x2;
2736 intf->null_user_handler = guid_handler;
2737 rv = send_guid_cmd(intf, 0);
2739 /* Send failed, no GUID available. */
2740 intf->bmc->guid_set = 0;
2741 wait_event(intf->waitq, intf->bmc->guid_set != 2);
2742 intf->null_user_handler = NULL;
2746 send_channel_info_cmd(ipmi_smi_t intf, int chan)
2748 struct kernel_ipmi_msg msg;
2749 unsigned char data[1];
2750 struct ipmi_system_interface_addr si;
2752 si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2753 si.channel = IPMI_BMC_CHANNEL;
2756 msg.netfn = IPMI_NETFN_APP_REQUEST;
2757 msg.cmd = IPMI_GET_CHANNEL_INFO_CMD;
2761 return i_ipmi_request(NULL,
2763 (struct ipmi_addr *) &si,
2770 intf->channels[0].address,
2771 intf->channels[0].lun,
2776 channel_handler(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
2781 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
2782 && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
2783 && (msg->msg.cmd == IPMI_GET_CHANNEL_INFO_CMD)) {
2784 /* It's the one we want */
2785 if (msg->msg.data[0] != 0) {
2786 /* Got an error from the channel, just go on. */
2788 if (msg->msg.data[0] == IPMI_INVALID_COMMAND_ERR) {
2790 * If the MC does not support this
2791 * command, that is legal. We just
2792 * assume it has one IPMB at channel
2795 intf->channels[0].medium
2796 = IPMI_CHANNEL_MEDIUM_IPMB;
2797 intf->channels[0].protocol
2798 = IPMI_CHANNEL_PROTOCOL_IPMB;
2801 intf->curr_channel = IPMI_MAX_CHANNELS;
2802 wake_up(&intf->waitq);
2807 if (msg->msg.data_len < 4) {
2808 /* Message not big enough, just go on. */
2811 chan = intf->curr_channel;
2812 intf->channels[chan].medium = msg->msg.data[2] & 0x7f;
2813 intf->channels[chan].protocol = msg->msg.data[3] & 0x1f;
2816 intf->curr_channel++;
2817 if (intf->curr_channel >= IPMI_MAX_CHANNELS)
2818 wake_up(&intf->waitq);
2820 rv = send_channel_info_cmd(intf, intf->curr_channel);
2823 /* Got an error somehow, just give up. */
2824 intf->curr_channel = IPMI_MAX_CHANNELS;
2825 wake_up(&intf->waitq);
2827 printk(KERN_WARNING PFX
2828 "Error sending channel information: %d\n",
2836 static void ipmi_poll(ipmi_smi_t intf)
2838 if (intf->handlers->poll)
2839 intf->handlers->poll(intf->send_info);
2840 /* In case something came in */
2841 handle_new_recv_msgs(intf);
2844 void ipmi_poll_interface(ipmi_user_t user)
2846 ipmi_poll(user->intf);
2848 EXPORT_SYMBOL(ipmi_poll_interface);
2850 int ipmi_register_smi(struct ipmi_smi_handlers *handlers,
2852 struct ipmi_device_id *device_id,
2853 struct device *si_dev,
2854 const char *sysfs_name,
2855 unsigned char slave_addr)
2861 struct list_head *link;
2864 * Make sure the driver is actually initialized, this handles
2865 * problems with initialization order.
2868 rv = ipmi_init_msghandler();
2872 * The init code doesn't return an error if it was turned
2873 * off, but it won't initialize. Check that.
2879 intf = kzalloc(sizeof(*intf), GFP_KERNEL);
2883 intf->ipmi_version_major = ipmi_version_major(device_id);
2884 intf->ipmi_version_minor = ipmi_version_minor(device_id);
2886 intf->bmc = kzalloc(sizeof(*intf->bmc), GFP_KERNEL);
2891 intf->intf_num = -1; /* Mark it invalid for now. */
2892 kref_init(&intf->refcount);
2893 intf->bmc->id = *device_id;
2894 intf->si_dev = si_dev;
2895 for (j = 0; j < IPMI_MAX_CHANNELS; j++) {
2896 intf->channels[j].address = IPMI_BMC_SLAVE_ADDR;
2897 intf->channels[j].lun = 2;
2899 if (slave_addr != 0)
2900 intf->channels[0].address = slave_addr;
2901 INIT_LIST_HEAD(&intf->users);
2902 intf->handlers = handlers;
2903 intf->send_info = send_info;
2904 spin_lock_init(&intf->seq_lock);
2905 for (j = 0; j < IPMI_IPMB_NUM_SEQ; j++) {
2906 intf->seq_table[j].inuse = 0;
2907 intf->seq_table[j].seqid = 0;
2910 #ifdef CONFIG_PROC_FS
2911 mutex_init(&intf->proc_entry_lock);
2913 spin_lock_init(&intf->waiting_msgs_lock);
2914 INIT_LIST_HEAD(&intf->waiting_msgs);
2915 tasklet_init(&intf->recv_tasklet,
2917 (unsigned long) intf);
2918 atomic_set(&intf->watchdog_pretimeouts_to_deliver, 0);
2919 spin_lock_init(&intf->events_lock);
2920 atomic_set(&intf->event_waiters, 0);
2921 intf->ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
2922 INIT_LIST_HEAD(&intf->waiting_events);
2923 intf->waiting_events_count = 0;
2924 mutex_init(&intf->cmd_rcvrs_mutex);
2925 spin_lock_init(&intf->maintenance_mode_lock);
2926 INIT_LIST_HEAD(&intf->cmd_rcvrs);
2927 init_waitqueue_head(&intf->waitq);
2928 for (i = 0; i < IPMI_NUM_STATS; i++)
2929 atomic_set(&intf->stats[i], 0);
2931 intf->proc_dir = NULL;
2933 mutex_lock(&smi_watchers_mutex);
2934 mutex_lock(&ipmi_interfaces_mutex);
2935 /* Look for a hole in the numbers. */
2937 link = &ipmi_interfaces;
2938 list_for_each_entry_rcu(tintf, &ipmi_interfaces, link) {
2939 if (tintf->intf_num != i) {
2940 link = &tintf->link;
2945 /* Add the new interface in numeric order. */
2947 list_add_rcu(&intf->link, &ipmi_interfaces);
2949 list_add_tail_rcu(&intf->link, link);
2951 rv = handlers->start_processing(send_info, intf);
2957 if ((intf->ipmi_version_major > 1)
2958 || ((intf->ipmi_version_major == 1)
2959 && (intf->ipmi_version_minor >= 5))) {
2961 * Start scanning the channels to see what is
2964 intf->null_user_handler = channel_handler;
2965 intf->curr_channel = 0;
2966 rv = send_channel_info_cmd(intf, 0);
2970 /* Wait for the channel info to be read. */
2971 wait_event(intf->waitq,
2972 intf->curr_channel >= IPMI_MAX_CHANNELS);
2973 intf->null_user_handler = NULL;
2975 /* Assume a single IPMB channel at zero. */
2976 intf->channels[0].medium = IPMI_CHANNEL_MEDIUM_IPMB;
2977 intf->channels[0].protocol = IPMI_CHANNEL_PROTOCOL_IPMB;
2978 intf->curr_channel = IPMI_MAX_CHANNELS;
2982 rv = add_proc_entries(intf, i);
2984 rv = ipmi_bmc_register(intf, i, sysfs_name);
2989 remove_proc_entries(intf);
2990 intf->handlers = NULL;
2991 list_del_rcu(&intf->link);
2992 mutex_unlock(&ipmi_interfaces_mutex);
2993 mutex_unlock(&smi_watchers_mutex);
2995 kref_put(&intf->refcount, intf_free);
2998 * Keep memory order straight for RCU readers. Make
2999 * sure everything else is committed to memory before
3000 * setting intf_num to mark the interface valid.
3004 mutex_unlock(&ipmi_interfaces_mutex);
3005 /* After this point the interface is legal to use. */
3006 call_smi_watchers(i, intf->si_dev);
3007 mutex_unlock(&smi_watchers_mutex);
3012 EXPORT_SYMBOL(ipmi_register_smi);
3014 static void cleanup_smi_msgs(ipmi_smi_t intf)
3017 struct seq_table *ent;
3019 /* No need for locks, the interface is down. */
3020 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
3021 ent = &(intf->seq_table[i]);
3024 deliver_err_response(ent->recv_msg, IPMI_ERR_UNSPECIFIED);
3028 int ipmi_unregister_smi(ipmi_smi_t intf)
3030 struct ipmi_smi_watcher *w;
3031 int intf_num = intf->intf_num;
3033 ipmi_bmc_unregister(intf);
3035 mutex_lock(&smi_watchers_mutex);
3036 mutex_lock(&ipmi_interfaces_mutex);
3037 intf->intf_num = -1;
3038 intf->handlers = NULL;
3039 list_del_rcu(&intf->link);
3040 mutex_unlock(&ipmi_interfaces_mutex);
3043 cleanup_smi_msgs(intf);
3045 remove_proc_entries(intf);
3048 * Call all the watcher interfaces to tell them that
3049 * an interface is gone.
3051 list_for_each_entry(w, &smi_watchers, link)
3052 w->smi_gone(intf_num);
3053 mutex_unlock(&smi_watchers_mutex);
3055 kref_put(&intf->refcount, intf_free);
3058 EXPORT_SYMBOL(ipmi_unregister_smi);
3060 static int handle_ipmb_get_msg_rsp(ipmi_smi_t intf,
3061 struct ipmi_smi_msg *msg)
3063 struct ipmi_ipmb_addr ipmb_addr;
3064 struct ipmi_recv_msg *recv_msg;
3067 * This is 11, not 10, because the response must contain a
3070 if (msg->rsp_size < 11) {
3071 /* Message not big enough, just ignore it. */
3072 ipmi_inc_stat(intf, invalid_ipmb_responses);
3076 if (msg->rsp[2] != 0) {
3077 /* An error getting the response, just ignore it. */
3081 ipmb_addr.addr_type = IPMI_IPMB_ADDR_TYPE;
3082 ipmb_addr.slave_addr = msg->rsp[6];
3083 ipmb_addr.channel = msg->rsp[3] & 0x0f;
3084 ipmb_addr.lun = msg->rsp[7] & 3;
3087 * It's a response from a remote entity. Look up the sequence
3088 * number and handle the response.
3090 if (intf_find_seq(intf,
3094 (msg->rsp[4] >> 2) & (~1),
3095 (struct ipmi_addr *) &(ipmb_addr),
3098 * We were unable to find the sequence number,
3099 * so just nuke the message.
3101 ipmi_inc_stat(intf, unhandled_ipmb_responses);
3105 memcpy(recv_msg->msg_data,
3109 * The other fields matched, so no need to set them, except
3110 * for netfn, which needs to be the response that was
3111 * returned, not the request value.
3113 recv_msg->msg.netfn = msg->rsp[4] >> 2;
3114 recv_msg->msg.data = recv_msg->msg_data;
3115 recv_msg->msg.data_len = msg->rsp_size - 10;
3116 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
3117 ipmi_inc_stat(intf, handled_ipmb_responses);
3118 deliver_response(recv_msg);
3123 static int handle_ipmb_get_msg_cmd(ipmi_smi_t intf,
3124 struct ipmi_smi_msg *msg)
3126 struct cmd_rcvr *rcvr;
3128 unsigned char netfn;
3131 ipmi_user_t user = NULL;
3132 struct ipmi_ipmb_addr *ipmb_addr;
3133 struct ipmi_recv_msg *recv_msg;
3134 struct ipmi_smi_handlers *handlers;
3136 if (msg->rsp_size < 10) {
3137 /* Message not big enough, just ignore it. */
3138 ipmi_inc_stat(intf, invalid_commands);
3142 if (msg->rsp[2] != 0) {
3143 /* An error getting the response, just ignore it. */
3147 netfn = msg->rsp[4] >> 2;
3149 chan = msg->rsp[3] & 0xf;
3152 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
3155 kref_get(&user->refcount);
3161 /* We didn't find a user, deliver an error response. */
3162 ipmi_inc_stat(intf, unhandled_commands);
3164 msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
3165 msg->data[1] = IPMI_SEND_MSG_CMD;
3166 msg->data[2] = msg->rsp[3];
3167 msg->data[3] = msg->rsp[6];
3168 msg->data[4] = ((netfn + 1) << 2) | (msg->rsp[7] & 0x3);
3169 msg->data[5] = ipmb_checksum(&(msg->data[3]), 2);
3170 msg->data[6] = intf->channels[msg->rsp[3] & 0xf].address;
3172 msg->data[7] = (msg->rsp[7] & 0xfc) | (msg->rsp[4] & 0x3);
3173 msg->data[8] = msg->rsp[8]; /* cmd */
3174 msg->data[9] = IPMI_INVALID_CMD_COMPLETION_CODE;
3175 msg->data[10] = ipmb_checksum(&(msg->data[6]), 4);
3176 msg->data_size = 11;
3181 printk("Invalid command:");
3182 for (m = 0; m < msg->data_size; m++)
3183 printk(" %2.2x", msg->data[m]);
3188 handlers = intf->handlers;
3190 handlers->sender(intf->send_info, msg, 0);
3192 * We used the message, so return the value
3193 * that causes it to not be freed or
3200 /* Deliver the message to the user. */
3201 ipmi_inc_stat(intf, handled_commands);
3203 recv_msg = ipmi_alloc_recv_msg();
3206 * We couldn't allocate memory for the
3207 * message, so requeue it for handling
3211 kref_put(&user->refcount, free_user);
3213 /* Extract the source address from the data. */
3214 ipmb_addr = (struct ipmi_ipmb_addr *) &recv_msg->addr;
3215 ipmb_addr->addr_type = IPMI_IPMB_ADDR_TYPE;
3216 ipmb_addr->slave_addr = msg->rsp[6];
3217 ipmb_addr->lun = msg->rsp[7] & 3;
3218 ipmb_addr->channel = msg->rsp[3] & 0xf;
3221 * Extract the rest of the message information
3222 * from the IPMB header.
3224 recv_msg->user = user;
3225 recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
3226 recv_msg->msgid = msg->rsp[7] >> 2;
3227 recv_msg->msg.netfn = msg->rsp[4] >> 2;
3228 recv_msg->msg.cmd = msg->rsp[8];
3229 recv_msg->msg.data = recv_msg->msg_data;
3232 * We chop off 10, not 9 bytes because the checksum
3233 * at the end also needs to be removed.
3235 recv_msg->msg.data_len = msg->rsp_size - 10;
3236 memcpy(recv_msg->msg_data,
3238 msg->rsp_size - 10);
3239 deliver_response(recv_msg);
3246 static int handle_lan_get_msg_rsp(ipmi_smi_t intf,
3247 struct ipmi_smi_msg *msg)
3249 struct ipmi_lan_addr lan_addr;
3250 struct ipmi_recv_msg *recv_msg;
3254 * This is 13, not 12, because the response must contain a
3257 if (msg->rsp_size < 13) {
3258 /* Message not big enough, just ignore it. */
3259 ipmi_inc_stat(intf, invalid_lan_responses);
3263 if (msg->rsp[2] != 0) {
3264 /* An error getting the response, just ignore it. */
3268 lan_addr.addr_type = IPMI_LAN_ADDR_TYPE;
3269 lan_addr.session_handle = msg->rsp[4];
3270 lan_addr.remote_SWID = msg->rsp[8];
3271 lan_addr.local_SWID = msg->rsp[5];
3272 lan_addr.channel = msg->rsp[3] & 0x0f;
3273 lan_addr.privilege = msg->rsp[3] >> 4;
3274 lan_addr.lun = msg->rsp[9] & 3;
3277 * It's a response from a remote entity. Look up the sequence
3278 * number and handle the response.
3280 if (intf_find_seq(intf,
3284 (msg->rsp[6] >> 2) & (~1),
3285 (struct ipmi_addr *) &(lan_addr),
3288 * We were unable to find the sequence number,
3289 * so just nuke the message.
3291 ipmi_inc_stat(intf, unhandled_lan_responses);
3295 memcpy(recv_msg->msg_data,
3297 msg->rsp_size - 11);
3299 * The other fields matched, so no need to set them, except
3300 * for netfn, which needs to be the response that was
3301 * returned, not the request value.
3303 recv_msg->msg.netfn = msg->rsp[6] >> 2;
3304 recv_msg->msg.data = recv_msg->msg_data;
3305 recv_msg->msg.data_len = msg->rsp_size - 12;
3306 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
3307 ipmi_inc_stat(intf, handled_lan_responses);
3308 deliver_response(recv_msg);
3313 static int handle_lan_get_msg_cmd(ipmi_smi_t intf,
3314 struct ipmi_smi_msg *msg)
3316 struct cmd_rcvr *rcvr;
3318 unsigned char netfn;
3321 ipmi_user_t user = NULL;
3322 struct ipmi_lan_addr *lan_addr;
3323 struct ipmi_recv_msg *recv_msg;
3325 if (msg->rsp_size < 12) {
3326 /* Message not big enough, just ignore it. */
3327 ipmi_inc_stat(intf, invalid_commands);
3331 if (msg->rsp[2] != 0) {
3332 /* An error getting the response, just ignore it. */
3336 netfn = msg->rsp[6] >> 2;
3338 chan = msg->rsp[3] & 0xf;
3341 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
3344 kref_get(&user->refcount);
3350 /* We didn't find a user, just give up. */
3351 ipmi_inc_stat(intf, unhandled_commands);
3354 * Don't do anything with these messages, just allow
3359 /* Deliver the message to the user. */
3360 ipmi_inc_stat(intf, handled_commands);
3362 recv_msg = ipmi_alloc_recv_msg();
3365 * We couldn't allocate memory for the
3366 * message, so requeue it for handling later.
3369 kref_put(&user->refcount, free_user);
3371 /* Extract the source address from the data. */
3372 lan_addr = (struct ipmi_lan_addr *) &recv_msg->addr;
3373 lan_addr->addr_type = IPMI_LAN_ADDR_TYPE;
3374 lan_addr->session_handle = msg->rsp[4];
3375 lan_addr->remote_SWID = msg->rsp[8];
3376 lan_addr->local_SWID = msg->rsp[5];
3377 lan_addr->lun = msg->rsp[9] & 3;
3378 lan_addr->channel = msg->rsp[3] & 0xf;
3379 lan_addr->privilege = msg->rsp[3] >> 4;
3382 * Extract the rest of the message information
3383 * from the IPMB header.
3385 recv_msg->user = user;
3386 recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
3387 recv_msg->msgid = msg->rsp[9] >> 2;
3388 recv_msg->msg.netfn = msg->rsp[6] >> 2;
3389 recv_msg->msg.cmd = msg->rsp[10];
3390 recv_msg->msg.data = recv_msg->msg_data;
3393 * We chop off 12, not 11 bytes because the checksum
3394 * at the end also needs to be removed.
3396 recv_msg->msg.data_len = msg->rsp_size - 12;
3397 memcpy(recv_msg->msg_data,
3399 msg->rsp_size - 12);
3400 deliver_response(recv_msg);
3408 * This routine will handle "Get Message" command responses with
3409 * channels that use an OEM Medium. The message format belongs to
3410 * the OEM. See IPMI 2.0 specification, Chapter 6 and
3411 * Chapter 22, sections 22.6 and 22.24 for more details.
3413 static int handle_oem_get_msg_cmd(ipmi_smi_t intf,
3414 struct ipmi_smi_msg *msg)
3416 struct cmd_rcvr *rcvr;
3418 unsigned char netfn;
3421 ipmi_user_t user = NULL;
3422 struct ipmi_system_interface_addr *smi_addr;
3423 struct ipmi_recv_msg *recv_msg;
3426 * We expect the OEM SW to perform error checking
3427 * so we just do some basic sanity checks
3429 if (msg->rsp_size < 4) {
3430 /* Message not big enough, just ignore it. */
3431 ipmi_inc_stat(intf, invalid_commands);
3435 if (msg->rsp[2] != 0) {
3436 /* An error getting the response, just ignore it. */
3441 * This is an OEM Message so the OEM needs to know how
3442 * handle the message. We do no interpretation.
3444 netfn = msg->rsp[0] >> 2;
3446 chan = msg->rsp[3] & 0xf;
3449 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
3452 kref_get(&user->refcount);
3458 /* We didn't find a user, just give up. */
3459 ipmi_inc_stat(intf, unhandled_commands);
3462 * Don't do anything with these messages, just allow
3468 /* Deliver the message to the user. */
3469 ipmi_inc_stat(intf, handled_commands);
3471 recv_msg = ipmi_alloc_recv_msg();
3474 * We couldn't allocate memory for the
3475 * message, so requeue it for handling
3479 kref_put(&user->refcount, free_user);
3482 * OEM Messages are expected to be delivered via
3483 * the system interface to SMS software. We might
3484 * need to visit this again depending on OEM
3487 smi_addr = ((struct ipmi_system_interface_addr *)
3489 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3490 smi_addr->channel = IPMI_BMC_CHANNEL;
3491 smi_addr->lun = msg->rsp[0] & 3;
3493 recv_msg->user = user;
3494 recv_msg->user_msg_data = NULL;
3495 recv_msg->recv_type = IPMI_OEM_RECV_TYPE;
3496 recv_msg->msg.netfn = msg->rsp[0] >> 2;
3497 recv_msg->msg.cmd = msg->rsp[1];
3498 recv_msg->msg.data = recv_msg->msg_data;
3501 * The message starts at byte 4 which follows the
3502 * the Channel Byte in the "GET MESSAGE" command
3504 recv_msg->msg.data_len = msg->rsp_size - 4;
3505 memcpy(recv_msg->msg_data,
3508 deliver_response(recv_msg);
3515 static void copy_event_into_recv_msg(struct ipmi_recv_msg *recv_msg,
3516 struct ipmi_smi_msg *msg)
3518 struct ipmi_system_interface_addr *smi_addr;
3520 recv_msg->msgid = 0;
3521 smi_addr = (struct ipmi_system_interface_addr *) &(recv_msg->addr);
3522 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3523 smi_addr->channel = IPMI_BMC_CHANNEL;
3524 smi_addr->lun = msg->rsp[0] & 3;
3525 recv_msg->recv_type = IPMI_ASYNC_EVENT_RECV_TYPE;
3526 recv_msg->msg.netfn = msg->rsp[0] >> 2;
3527 recv_msg->msg.cmd = msg->rsp[1];
3528 memcpy(recv_msg->msg_data, &(msg->rsp[3]), msg->rsp_size - 3);
3529 recv_msg->msg.data = recv_msg->msg_data;
3530 recv_msg->msg.data_len = msg->rsp_size - 3;
3533 static int handle_read_event_rsp(ipmi_smi_t intf,
3534 struct ipmi_smi_msg *msg)
3536 struct ipmi_recv_msg *recv_msg, *recv_msg2;
3537 struct list_head msgs;
3540 int deliver_count = 0;
3541 unsigned long flags;
3543 if (msg->rsp_size < 19) {
3544 /* Message is too small to be an IPMB event. */
3545 ipmi_inc_stat(intf, invalid_events);
3549 if (msg->rsp[2] != 0) {
3550 /* An error getting the event, just ignore it. */
3554 INIT_LIST_HEAD(&msgs);
3556 spin_lock_irqsave(&intf->events_lock, flags);
3558 ipmi_inc_stat(intf, events);
3561 * Allocate and fill in one message for every user that is
3565 list_for_each_entry_rcu(user, &intf->users, link) {
3566 if (!user->gets_events)
3569 recv_msg = ipmi_alloc_recv_msg();
3572 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs,
3574 list_del(&recv_msg->link);
3575 ipmi_free_recv_msg(recv_msg);
3578 * We couldn't allocate memory for the
3579 * message, so requeue it for handling
3588 copy_event_into_recv_msg(recv_msg, msg);
3589 recv_msg->user = user;
3590 kref_get(&user->refcount);
3591 list_add_tail(&(recv_msg->link), &msgs);
3595 if (deliver_count) {
3596 /* Now deliver all the messages. */
3597 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs, link) {
3598 list_del(&recv_msg->link);
3599 deliver_response(recv_msg);
3601 } else if (intf->waiting_events_count < MAX_EVENTS_IN_QUEUE) {
3603 * No one to receive the message, put it in queue if there's
3604 * not already too many things in the queue.
3606 recv_msg = ipmi_alloc_recv_msg();
3609 * We couldn't allocate memory for the
3610 * message, so requeue it for handling
3617 copy_event_into_recv_msg(recv_msg, msg);
3618 list_add_tail(&(recv_msg->link), &(intf->waiting_events));
3619 intf->waiting_events_count++;
3620 } else if (!intf->event_msg_printed) {
3622 * There's too many things in the queue, discard this
3625 printk(KERN_WARNING PFX "Event queue full, discarding"
3626 " incoming events\n");
3627 intf->event_msg_printed = 1;
3631 spin_unlock_irqrestore(&(intf->events_lock), flags);
3636 static int handle_bmc_rsp(ipmi_smi_t intf,
3637 struct ipmi_smi_msg *msg)
3639 struct ipmi_recv_msg *recv_msg;
3640 struct ipmi_user *user;
3642 recv_msg = (struct ipmi_recv_msg *) msg->user_data;
3643 if (recv_msg == NULL) {
3645 "IPMI message received with no owner. This\n"
3646 "could be because of a malformed message, or\n"
3647 "because of a hardware error. Contact your\n"
3648 "hardware vender for assistance\n");
3652 user = recv_msg->user;
3653 /* Make sure the user still exists. */
3654 if (user && !user->valid) {
3655 /* The user for the message went away, so give up. */
3656 ipmi_inc_stat(intf, unhandled_local_responses);
3657 ipmi_free_recv_msg(recv_msg);
3659 struct ipmi_system_interface_addr *smi_addr;
3661 ipmi_inc_stat(intf, handled_local_responses);
3662 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
3663 recv_msg->msgid = msg->msgid;
3664 smi_addr = ((struct ipmi_system_interface_addr *)
3666 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3667 smi_addr->channel = IPMI_BMC_CHANNEL;
3668 smi_addr->lun = msg->rsp[0] & 3;
3669 recv_msg->msg.netfn = msg->rsp[0] >> 2;
3670 recv_msg->msg.cmd = msg->rsp[1];
3671 memcpy(recv_msg->msg_data,
3674 recv_msg->msg.data = recv_msg->msg_data;
3675 recv_msg->msg.data_len = msg->rsp_size - 2;
3676 deliver_response(recv_msg);
3683 * Handle a received message. Return 1 if the message should be requeued,
3684 * 0 if the message should be freed, or -1 if the message should not
3685 * be freed or requeued.
3687 static int handle_one_recv_msg(ipmi_smi_t intf,
3688 struct ipmi_smi_msg *msg)
3696 for (m = 0; m < msg->rsp_size; m++)
3697 printk(" %2.2x", msg->rsp[m]);
3700 if (msg->rsp_size < 2) {
3701 /* Message is too small to be correct. */
3702 printk(KERN_WARNING PFX "BMC returned to small a message"
3703 " for netfn %x cmd %x, got %d bytes\n",
3704 (msg->data[0] >> 2) | 1, msg->data[1], msg->rsp_size);
3706 /* Generate an error response for the message. */
3707 msg->rsp[0] = msg->data[0] | (1 << 2);
3708 msg->rsp[1] = msg->data[1];
3709 msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
3711 } else if (((msg->rsp[0] >> 2) != ((msg->data[0] >> 2) | 1))
3712 || (msg->rsp[1] != msg->data[1])) {
3714 * The NetFN and Command in the response is not even
3715 * marginally correct.
3717 printk(KERN_WARNING PFX "BMC returned incorrect response,"
3718 " expected netfn %x cmd %x, got netfn %x cmd %x\n",
3719 (msg->data[0] >> 2) | 1, msg->data[1],
3720 msg->rsp[0] >> 2, msg->rsp[1]);
3722 /* Generate an error response for the message. */
3723 msg->rsp[0] = msg->data[0] | (1 << 2);
3724 msg->rsp[1] = msg->data[1];
3725 msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
3729 if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
3730 && (msg->rsp[1] == IPMI_SEND_MSG_CMD)
3731 && (msg->user_data != NULL)) {
3733 * It's a response to a response we sent. For this we
3734 * deliver a send message response to the user.
3736 struct ipmi_recv_msg *recv_msg = msg->user_data;
3739 if (msg->rsp_size < 2)
3740 /* Message is too small to be correct. */
3743 chan = msg->data[2] & 0x0f;
3744 if (chan >= IPMI_MAX_CHANNELS)
3745 /* Invalid channel number */
3751 /* Make sure the user still exists. */
3752 if (!recv_msg->user || !recv_msg->user->valid)
3755 recv_msg->recv_type = IPMI_RESPONSE_RESPONSE_TYPE;
3756 recv_msg->msg.data = recv_msg->msg_data;
3757 recv_msg->msg.data_len = 1;
3758 recv_msg->msg_data[0] = msg->rsp[2];
3759 deliver_response(recv_msg);
3760 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
3761 && (msg->rsp[1] == IPMI_GET_MSG_CMD)) {
3762 /* It's from the receive queue. */
3763 chan = msg->rsp[3] & 0xf;
3764 if (chan >= IPMI_MAX_CHANNELS) {
3765 /* Invalid channel number */
3771 * We need to make sure the channels have been initialized.
3772 * The channel_handler routine will set the "curr_channel"
3773 * equal to or greater than IPMI_MAX_CHANNELS when all the
3774 * channels for this interface have been initialized.
3776 if (intf->curr_channel < IPMI_MAX_CHANNELS) {
3777 requeue = 0; /* Throw the message away */
3781 switch (intf->channels[chan].medium) {
3782 case IPMI_CHANNEL_MEDIUM_IPMB:
3783 if (msg->rsp[4] & 0x04) {
3785 * It's a response, so find the
3786 * requesting message and send it up.
3788 requeue = handle_ipmb_get_msg_rsp(intf, msg);
3791 * It's a command to the SMS from some other
3792 * entity. Handle that.
3794 requeue = handle_ipmb_get_msg_cmd(intf, msg);
3798 case IPMI_CHANNEL_MEDIUM_8023LAN:
3799 case IPMI_CHANNEL_MEDIUM_ASYNC:
3800 if (msg->rsp[6] & 0x04) {
3802 * It's a response, so find the
3803 * requesting message and send it up.
3805 requeue = handle_lan_get_msg_rsp(intf, msg);
3808 * It's a command to the SMS from some other
3809 * entity. Handle that.
3811 requeue = handle_lan_get_msg_cmd(intf, msg);
3816 /* Check for OEM Channels. Clients had better
3817 register for these commands. */
3818 if ((intf->channels[chan].medium
3819 >= IPMI_CHANNEL_MEDIUM_OEM_MIN)
3820 && (intf->channels[chan].medium
3821 <= IPMI_CHANNEL_MEDIUM_OEM_MAX)) {
3822 requeue = handle_oem_get_msg_cmd(intf, msg);
3825 * We don't handle the channel type, so just
3832 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
3833 && (msg->rsp[1] == IPMI_READ_EVENT_MSG_BUFFER_CMD)) {
3834 /* It's an asynchronous event. */
3835 requeue = handle_read_event_rsp(intf, msg);
3837 /* It's a response from the local BMC. */
3838 requeue = handle_bmc_rsp(intf, msg);
3846 * If there are messages in the queue or pretimeouts, handle them.
3848 static void handle_new_recv_msgs(ipmi_smi_t intf)
3850 struct ipmi_smi_msg *smi_msg;
3851 unsigned long flags = 0;
3853 int run_to_completion = intf->run_to_completion;
3855 /* See if any waiting messages need to be processed. */
3856 if (!run_to_completion)
3857 spin_lock_irqsave(&intf->waiting_msgs_lock, flags);
3858 while (!list_empty(&intf->waiting_msgs)) {
3859 smi_msg = list_entry(intf->waiting_msgs.next,
3860 struct ipmi_smi_msg, link);
3861 list_del(&smi_msg->link);
3862 if (!run_to_completion)
3863 spin_unlock_irqrestore(&intf->waiting_msgs_lock, flags);
3864 rv = handle_one_recv_msg(intf, smi_msg);
3865 if (!run_to_completion)
3866 spin_lock_irqsave(&intf->waiting_msgs_lock, flags);
3868 /* Message handled */
3869 ipmi_free_smi_msg(smi_msg);
3870 } else if (rv < 0) {
3871 /* Fatal error on the message, del but don't free. */
3874 * To preserve message order, quit if we
3875 * can't handle a message.
3877 list_add(&smi_msg->link, &intf->waiting_msgs);
3881 if (!run_to_completion)
3882 spin_unlock_irqrestore(&intf->waiting_msgs_lock, flags);
3885 * If the pretimout count is non-zero, decrement one from it and
3886 * deliver pretimeouts to all the users.
3888 if (atomic_add_unless(&intf->watchdog_pretimeouts_to_deliver, -1, 0)) {
3892 list_for_each_entry_rcu(user, &intf->users, link) {
3893 if (user->handler->ipmi_watchdog_pretimeout)
3894 user->handler->ipmi_watchdog_pretimeout(
3895 user->handler_data);
3901 static void smi_recv_tasklet(unsigned long val)
3903 handle_new_recv_msgs((ipmi_smi_t) val);
3906 /* Handle a new message from the lower layer. */
3907 void ipmi_smi_msg_received(ipmi_smi_t intf,
3908 struct ipmi_smi_msg *msg)
3910 unsigned long flags = 0; /* keep us warning-free. */
3911 int run_to_completion;
3914 if ((msg->data_size >= 2)
3915 && (msg->data[0] == (IPMI_NETFN_APP_REQUEST << 2))
3916 && (msg->data[1] == IPMI_SEND_MSG_CMD)
3917 && (msg->user_data == NULL)) {
3919 * This is the local response to a command send, start
3920 * the timer for these. The user_data will not be
3921 * NULL if this is a response send, and we will let
3922 * response sends just go through.
3926 * Check for errors, if we get certain errors (ones
3927 * that mean basically we can try again later), we
3928 * ignore them and start the timer. Otherwise we
3929 * report the error immediately.
3931 if ((msg->rsp_size >= 3) && (msg->rsp[2] != 0)
3932 && (msg->rsp[2] != IPMI_NODE_BUSY_ERR)
3933 && (msg->rsp[2] != IPMI_LOST_ARBITRATION_ERR)
3934 && (msg->rsp[2] != IPMI_BUS_ERR)
3935 && (msg->rsp[2] != IPMI_NAK_ON_WRITE_ERR)) {
3936 int chan = msg->rsp[3] & 0xf;
3938 /* Got an error sending the message, handle it. */
3939 if (chan >= IPMI_MAX_CHANNELS)
3940 ; /* This shouldn't happen */
3941 else if ((intf->channels[chan].medium
3942 == IPMI_CHANNEL_MEDIUM_8023LAN)
3943 || (intf->channels[chan].medium
3944 == IPMI_CHANNEL_MEDIUM_ASYNC))
3945 ipmi_inc_stat(intf, sent_lan_command_errs);
3947 ipmi_inc_stat(intf, sent_ipmb_command_errs);
3948 intf_err_seq(intf, msg->msgid, msg->rsp[2]);
3950 /* The message was sent, start the timer. */
3951 intf_start_seq_timer(intf, msg->msgid);
3953 ipmi_free_smi_msg(msg);
3958 * To preserve message order, if the list is not empty, we
3959 * tack this message onto the end of the list.
3961 run_to_completion = intf->run_to_completion;
3962 if (!run_to_completion)
3963 spin_lock_irqsave(&intf->waiting_msgs_lock, flags);
3964 list_add_tail(&msg->link, &intf->waiting_msgs);
3965 if (!run_to_completion)
3966 spin_unlock_irqrestore(&intf->waiting_msgs_lock, flags);
3968 tasklet_schedule(&intf->recv_tasklet);
3972 EXPORT_SYMBOL(ipmi_smi_msg_received);
3974 void ipmi_smi_watchdog_pretimeout(ipmi_smi_t intf)
3976 atomic_set(&intf->watchdog_pretimeouts_to_deliver, 1);
3977 tasklet_schedule(&intf->recv_tasklet);
3979 EXPORT_SYMBOL(ipmi_smi_watchdog_pretimeout);
3981 static struct ipmi_smi_msg *
3982 smi_from_recv_msg(ipmi_smi_t intf, struct ipmi_recv_msg *recv_msg,
3983 unsigned char seq, long seqid)
3985 struct ipmi_smi_msg *smi_msg = ipmi_alloc_smi_msg();
3988 * If we can't allocate the message, then just return, we
3989 * get 4 retries, so this should be ok.
3993 memcpy(smi_msg->data, recv_msg->msg.data, recv_msg->msg.data_len);
3994 smi_msg->data_size = recv_msg->msg.data_len;
3995 smi_msg->msgid = STORE_SEQ_IN_MSGID(seq, seqid);
4001 for (m = 0; m < smi_msg->data_size; m++)
4002 printk(" %2.2x", smi_msg->data[m]);
4009 static void check_msg_timeout(ipmi_smi_t intf, struct seq_table *ent,
4010 struct list_head *timeouts, long timeout_period,
4011 int slot, unsigned long *flags,
4012 unsigned int *waiting_msgs)
4014 struct ipmi_recv_msg *msg;
4015 struct ipmi_smi_handlers *handlers;
4017 if (intf->intf_num == -1)
4023 ent->timeout -= timeout_period;
4024 if (ent->timeout > 0) {
4029 if (ent->retries_left == 0) {
4030 /* The message has used all its retries. */
4032 msg = ent->recv_msg;
4033 list_add_tail(&msg->link, timeouts);
4035 ipmi_inc_stat(intf, timed_out_ipmb_broadcasts);
4036 else if (is_lan_addr(&ent->recv_msg->addr))
4037 ipmi_inc_stat(intf, timed_out_lan_commands);
4039 ipmi_inc_stat(intf, timed_out_ipmb_commands);
4041 struct ipmi_smi_msg *smi_msg;
4042 /* More retries, send again. */
4047 * Start with the max timer, set to normal timer after
4048 * the message is sent.
4050 ent->timeout = MAX_MSG_TIMEOUT;
4051 ent->retries_left--;
4052 smi_msg = smi_from_recv_msg(intf, ent->recv_msg, slot,
4055 if (is_lan_addr(&ent->recv_msg->addr))
4057 dropped_rexmit_lan_commands);
4060 dropped_rexmit_ipmb_commands);
4064 spin_unlock_irqrestore(&intf->seq_lock, *flags);
4067 * Send the new message. We send with a zero
4068 * priority. It timed out, I doubt time is that
4069 * critical now, and high priority messages are really
4070 * only for messages to the local MC, which don't get
4073 handlers = intf->handlers;
4075 if (is_lan_addr(&ent->recv_msg->addr))
4077 retransmitted_lan_commands);
4080 retransmitted_ipmb_commands);
4082 intf->handlers->sender(intf->send_info,
4085 ipmi_free_smi_msg(smi_msg);
4087 spin_lock_irqsave(&intf->seq_lock, *flags);
4091 static unsigned int ipmi_timeout_handler(ipmi_smi_t intf, long timeout_period)
4093 struct list_head timeouts;
4094 struct ipmi_recv_msg *msg, *msg2;
4095 unsigned long flags;
4097 unsigned int waiting_msgs = 0;
4100 * Go through the seq table and find any messages that
4101 * have timed out, putting them in the timeouts
4104 INIT_LIST_HEAD(&timeouts);
4105 spin_lock_irqsave(&intf->seq_lock, flags);
4106 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++)
4107 check_msg_timeout(intf, &(intf->seq_table[i]),
4108 &timeouts, timeout_period, i,
4109 &flags, &waiting_msgs);
4110 spin_unlock_irqrestore(&intf->seq_lock, flags);
4112 list_for_each_entry_safe(msg, msg2, &timeouts, link)
4113 deliver_err_response(msg, IPMI_TIMEOUT_COMPLETION_CODE);
4116 * Maintenance mode handling. Check the timeout
4117 * optimistically before we claim the lock. It may
4118 * mean a timeout gets missed occasionally, but that
4119 * only means the timeout gets extended by one period
4120 * in that case. No big deal, and it avoids the lock
4123 if (intf->auto_maintenance_timeout > 0) {
4124 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
4125 if (intf->auto_maintenance_timeout > 0) {
4126 intf->auto_maintenance_timeout
4128 if (!intf->maintenance_mode
4129 && (intf->auto_maintenance_timeout <= 0)) {
4130 intf->maintenance_mode_enable = false;
4131 maintenance_mode_update(intf);
4134 spin_unlock_irqrestore(&intf->maintenance_mode_lock,
4138 tasklet_schedule(&intf->recv_tasklet);
4140 return waiting_msgs;
4143 static void ipmi_request_event(ipmi_smi_t intf)
4145 struct ipmi_smi_handlers *handlers;
4147 /* No event requests when in maintenance mode. */
4148 if (intf->maintenance_mode_enable)
4151 handlers = intf->handlers;
4153 handlers->request_events(intf->send_info);
4156 static struct timer_list ipmi_timer;
4158 static atomic_t stop_operation;
4160 static void ipmi_timeout(unsigned long data)
4165 if (atomic_read(&stop_operation))
4169 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
4172 if (atomic_read(&intf->event_waiters)) {
4173 intf->ticks_to_req_ev--;
4174 if (intf->ticks_to_req_ev == 0) {
4175 ipmi_request_event(intf);
4176 intf->ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
4181 lnt += ipmi_timeout_handler(intf, IPMI_TIMEOUT_TIME);
4184 if (lnt != intf->last_needs_timer &&
4185 intf->handlers->set_need_watch)
4186 intf->handlers->set_need_watch(intf->send_info, lnt);
4187 intf->last_needs_timer = lnt;
4194 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
4197 static void need_waiter(ipmi_smi_t intf)
4199 /* Racy, but worst case we start the timer twice. */
4200 if (!timer_pending(&ipmi_timer))
4201 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
4204 static atomic_t smi_msg_inuse_count = ATOMIC_INIT(0);
4205 static atomic_t recv_msg_inuse_count = ATOMIC_INIT(0);
4207 /* FIXME - convert these to slabs. */
4208 static void free_smi_msg(struct ipmi_smi_msg *msg)
4210 atomic_dec(&smi_msg_inuse_count);
4214 struct ipmi_smi_msg *ipmi_alloc_smi_msg(void)
4216 struct ipmi_smi_msg *rv;
4217 rv = kmalloc(sizeof(struct ipmi_smi_msg), GFP_ATOMIC);
4219 rv->done = free_smi_msg;
4220 rv->user_data = NULL;
4221 atomic_inc(&smi_msg_inuse_count);
4225 EXPORT_SYMBOL(ipmi_alloc_smi_msg);
4227 static void free_recv_msg(struct ipmi_recv_msg *msg)
4229 atomic_dec(&recv_msg_inuse_count);
4233 static struct ipmi_recv_msg *ipmi_alloc_recv_msg(void)
4235 struct ipmi_recv_msg *rv;
4237 rv = kmalloc(sizeof(struct ipmi_recv_msg), GFP_ATOMIC);
4240 rv->done = free_recv_msg;
4241 atomic_inc(&recv_msg_inuse_count);
4246 void ipmi_free_recv_msg(struct ipmi_recv_msg *msg)
4249 kref_put(&msg->user->refcount, free_user);
4252 EXPORT_SYMBOL(ipmi_free_recv_msg);
4254 #ifdef CONFIG_IPMI_PANIC_EVENT
4256 static atomic_t panic_done_count = ATOMIC_INIT(0);
4258 static void dummy_smi_done_handler(struct ipmi_smi_msg *msg)
4260 atomic_dec(&panic_done_count);
4263 static void dummy_recv_done_handler(struct ipmi_recv_msg *msg)
4265 atomic_dec(&panic_done_count);
4269 * Inside a panic, send a message and wait for a response.
4271 static void ipmi_panic_request_and_wait(ipmi_smi_t intf,
4272 struct ipmi_addr *addr,
4273 struct kernel_ipmi_msg *msg)
4275 struct ipmi_smi_msg smi_msg;
4276 struct ipmi_recv_msg recv_msg;
4279 smi_msg.done = dummy_smi_done_handler;
4280 recv_msg.done = dummy_recv_done_handler;
4281 atomic_add(2, &panic_done_count);
4282 rv = i_ipmi_request(NULL,
4291 intf->channels[0].address,
4292 intf->channels[0].lun,
4293 0, 1); /* Don't retry, and don't wait. */
4295 atomic_sub(2, &panic_done_count);
4296 while (atomic_read(&panic_done_count) != 0)
4300 #ifdef CONFIG_IPMI_PANIC_STRING
4301 static void event_receiver_fetcher(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
4303 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
4304 && (msg->msg.netfn == IPMI_NETFN_SENSOR_EVENT_RESPONSE)
4305 && (msg->msg.cmd == IPMI_GET_EVENT_RECEIVER_CMD)
4306 && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) {
4307 /* A get event receiver command, save it. */
4308 intf->event_receiver = msg->msg.data[1];
4309 intf->event_receiver_lun = msg->msg.data[2] & 0x3;
4313 static void device_id_fetcher(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
4315 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
4316 && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
4317 && (msg->msg.cmd == IPMI_GET_DEVICE_ID_CMD)
4318 && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) {
4320 * A get device id command, save if we are an event
4321 * receiver or generator.
4323 intf->local_sel_device = (msg->msg.data[6] >> 2) & 1;
4324 intf->local_event_generator = (msg->msg.data[6] >> 5) & 1;
4329 static void send_panic_events(char *str)
4331 struct kernel_ipmi_msg msg;
4333 unsigned char data[16];
4334 struct ipmi_system_interface_addr *si;
4335 struct ipmi_addr addr;
4337 si = (struct ipmi_system_interface_addr *) &addr;
4338 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
4339 si->channel = IPMI_BMC_CHANNEL;
4342 /* Fill in an event telling that we have failed. */
4343 msg.netfn = 0x04; /* Sensor or Event. */
4344 msg.cmd = 2; /* Platform event command. */
4347 data[0] = 0x41; /* Kernel generator ID, IPMI table 5-4 */
4348 data[1] = 0x03; /* This is for IPMI 1.0. */
4349 data[2] = 0x20; /* OS Critical Stop, IPMI table 36-3 */
4350 data[4] = 0x6f; /* Sensor specific, IPMI table 36-1 */
4351 data[5] = 0xa1; /* Runtime stop OEM bytes 2 & 3. */
4354 * Put a few breadcrumbs in. Hopefully later we can add more things
4355 * to make the panic events more useful.
4363 /* For every registered interface, send the event. */
4364 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
4365 if (!intf->handlers)
4366 /* Interface is not ready. */
4369 intf->run_to_completion = 1;
4370 /* Send the event announcing the panic. */
4371 intf->handlers->set_run_to_completion(intf->send_info, 1);
4372 ipmi_panic_request_and_wait(intf, &addr, &msg);
4375 #ifdef CONFIG_IPMI_PANIC_STRING
4377 * On every interface, dump a bunch of OEM event holding the
4383 /* For every registered interface, send the event. */
4384 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
4386 struct ipmi_ipmb_addr *ipmb;
4389 if (intf->intf_num == -1)
4390 /* Interface was not ready yet. */
4394 * intf_num is used as an marker to tell if the
4395 * interface is valid. Thus we need a read barrier to
4396 * make sure data fetched before checking intf_num
4402 * First job here is to figure out where to send the
4403 * OEM events. There's no way in IPMI to send OEM
4404 * events using an event send command, so we have to
4405 * find the SEL to put them in and stick them in
4409 /* Get capabilities from the get device id. */
4410 intf->local_sel_device = 0;
4411 intf->local_event_generator = 0;
4412 intf->event_receiver = 0;
4414 /* Request the device info from the local MC. */
4415 msg.netfn = IPMI_NETFN_APP_REQUEST;
4416 msg.cmd = IPMI_GET_DEVICE_ID_CMD;
4419 intf->null_user_handler = device_id_fetcher;
4420 ipmi_panic_request_and_wait(intf, &addr, &msg);
4422 if (intf->local_event_generator) {
4423 /* Request the event receiver from the local MC. */
4424 msg.netfn = IPMI_NETFN_SENSOR_EVENT_REQUEST;
4425 msg.cmd = IPMI_GET_EVENT_RECEIVER_CMD;
4428 intf->null_user_handler = event_receiver_fetcher;
4429 ipmi_panic_request_and_wait(intf, &addr, &msg);
4431 intf->null_user_handler = NULL;
4434 * Validate the event receiver. The low bit must not
4435 * be 1 (it must be a valid IPMB address), it cannot
4436 * be zero, and it must not be my address.
4438 if (((intf->event_receiver & 1) == 0)
4439 && (intf->event_receiver != 0)
4440 && (intf->event_receiver != intf->channels[0].address)) {
4442 * The event receiver is valid, send an IPMB
4445 ipmb = (struct ipmi_ipmb_addr *) &addr;
4446 ipmb->addr_type = IPMI_IPMB_ADDR_TYPE;
4447 ipmb->channel = 0; /* FIXME - is this right? */
4448 ipmb->lun = intf->event_receiver_lun;
4449 ipmb->slave_addr = intf->event_receiver;
4450 } else if (intf->local_sel_device) {
4452 * The event receiver was not valid (or was
4453 * me), but I am an SEL device, just dump it
4456 si = (struct ipmi_system_interface_addr *) &addr;
4457 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
4458 si->channel = IPMI_BMC_CHANNEL;
4461 continue; /* No where to send the event. */
4463 msg.netfn = IPMI_NETFN_STORAGE_REQUEST; /* Storage. */
4464 msg.cmd = IPMI_ADD_SEL_ENTRY_CMD;
4470 int size = strlen(p);
4476 data[2] = 0xf0; /* OEM event without timestamp. */
4477 data[3] = intf->channels[0].address;
4478 data[4] = j++; /* sequence # */
4480 * Always give 11 bytes, so strncpy will fill
4481 * it with zeroes for me.
4483 strncpy(data+5, p, 11);
4486 ipmi_panic_request_and_wait(intf, &addr, &msg);
4489 #endif /* CONFIG_IPMI_PANIC_STRING */
4491 #endif /* CONFIG_IPMI_PANIC_EVENT */
4493 static int has_panicked;
4495 static int panic_event(struct notifier_block *this,
4496 unsigned long event,
4505 /* For every registered interface, set it to run to completion. */
4506 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
4507 if (!intf->handlers)
4508 /* Interface is not ready. */
4511 intf->run_to_completion = 1;
4512 intf->handlers->set_run_to_completion(intf->send_info, 1);
4515 #ifdef CONFIG_IPMI_PANIC_EVENT
4516 send_panic_events(ptr);
4522 static struct notifier_block panic_block = {
4523 .notifier_call = panic_event,
4525 .priority = 200 /* priority: INT_MAX >= x >= 0 */
4528 static int ipmi_init_msghandler(void)
4535 rv = driver_register(&ipmidriver.driver);
4537 printk(KERN_ERR PFX "Could not register IPMI driver\n");
4541 printk(KERN_INFO "ipmi message handler version "
4542 IPMI_DRIVER_VERSION "\n");
4544 #ifdef CONFIG_PROC_FS
4545 proc_ipmi_root = proc_mkdir("ipmi", NULL);
4546 if (!proc_ipmi_root) {
4547 printk(KERN_ERR PFX "Unable to create IPMI proc dir");
4551 #endif /* CONFIG_PROC_FS */
4553 setup_timer(&ipmi_timer, ipmi_timeout, 0);
4554 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
4556 atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
4563 static int __init ipmi_init_msghandler_mod(void)
4565 ipmi_init_msghandler();
4569 static void __exit cleanup_ipmi(void)
4576 atomic_notifier_chain_unregister(&panic_notifier_list, &panic_block);
4579 * This can't be called if any interfaces exist, so no worry
4580 * about shutting down the interfaces.
4584 * Tell the timer to stop, then wait for it to stop. This
4585 * avoids problems with race conditions removing the timer
4588 atomic_inc(&stop_operation);
4589 del_timer_sync(&ipmi_timer);
4591 #ifdef CONFIG_PROC_FS
4592 proc_remove(proc_ipmi_root);
4593 #endif /* CONFIG_PROC_FS */
4595 driver_unregister(&ipmidriver.driver);
4599 /* Check for buffer leaks. */
4600 count = atomic_read(&smi_msg_inuse_count);
4602 printk(KERN_WARNING PFX "SMI message count %d at exit\n",
4604 count = atomic_read(&recv_msg_inuse_count);
4606 printk(KERN_WARNING PFX "recv message count %d at exit\n",
4609 module_exit(cleanup_ipmi);
4611 module_init(ipmi_init_msghandler_mod);
4612 MODULE_LICENSE("GPL");
4613 MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
4614 MODULE_DESCRIPTION("Incoming and outgoing message routing for an IPMI"
4616 MODULE_VERSION(IPMI_DRIVER_VERSION);