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);
59 static int handle_one_recv_msg(ipmi_smi_t intf,
60 struct ipmi_smi_msg *msg);
62 static int initialized;
65 static struct proc_dir_entry *proc_ipmi_root;
66 #endif /* CONFIG_PROC_FS */
68 /* Remain in auto-maintenance mode for this amount of time (in ms). */
69 #define IPMI_MAINTENANCE_MODE_TIMEOUT 30000
71 #define MAX_EVENTS_IN_QUEUE 25
74 * Don't let a message sit in a queue forever, always time it with at lest
75 * the max message timer. This is in milliseconds.
77 #define MAX_MSG_TIMEOUT 60000
79 /* Call every ~1000 ms. */
80 #define IPMI_TIMEOUT_TIME 1000
82 /* How many jiffies does it take to get to the timeout time. */
83 #define IPMI_TIMEOUT_JIFFIES ((IPMI_TIMEOUT_TIME * HZ) / 1000)
86 * Request events from the queue every second (this is the number of
87 * IPMI_TIMEOUT_TIMES between event requests). Hopefully, in the
88 * future, IPMI will add a way to know immediately if an event is in
89 * the queue and this silliness can go away.
91 #define IPMI_REQUEST_EV_TIME (1000 / (IPMI_TIMEOUT_TIME))
94 * The main "user" data structure.
97 struct list_head link;
99 /* Set to false when the user is destroyed. */
102 struct kref refcount;
104 /* The upper layer that handles receive messages. */
105 const struct ipmi_user_hndl *handler;
108 /* The interface this user is bound to. */
111 /* Does this interface receive IPMI events? */
116 struct list_head link;
124 * This is used to form a linked lised during mass deletion.
125 * Since this is in an RCU list, we cannot use the link above
126 * or change any data until the RCU period completes. So we
127 * use this next variable during mass deletion so we can have
128 * a list and don't have to wait and restart the search on
129 * every individual deletion of a command.
131 struct cmd_rcvr *next;
135 unsigned int inuse : 1;
136 unsigned int broadcast : 1;
138 unsigned long timeout;
139 unsigned long orig_timeout;
140 unsigned int retries_left;
143 * To verify on an incoming send message response that this is
144 * the message that the response is for, we keep a sequence id
145 * and increment it every time we send a message.
150 * This is held so we can properly respond to the message on a
151 * timeout, and it is used to hold the temporary data for
152 * retransmission, too.
154 struct ipmi_recv_msg *recv_msg;
158 * Store the information in a msgid (long) to allow us to find a
159 * sequence table entry from the msgid.
161 #define STORE_SEQ_IN_MSGID(seq, seqid) \
162 ((((seq) & 0x3f) << 26) | ((seqid) & 0x3ffffff))
164 #define GET_SEQ_FROM_MSGID(msgid, seq, seqid) \
166 seq = (((msgid) >> 26) & 0x3f); \
167 seqid = ((msgid) & 0x3ffffff); \
170 #define NEXT_SEQID(seqid) (((seqid) + 1) & 0x3ffffff)
172 struct ipmi_channel {
173 unsigned char medium;
174 unsigned char protocol;
177 * My slave address. This is initialized to IPMI_BMC_SLAVE_ADDR,
178 * but may be changed by the user.
180 unsigned char address;
183 * My LUN. This should generally stay the SMS LUN, but just in
189 #ifdef CONFIG_PROC_FS
190 struct ipmi_proc_entry {
192 struct ipmi_proc_entry *next;
197 struct platform_device pdev;
198 struct ipmi_device_id id;
199 unsigned char guid[16];
202 struct kref usecount;
204 #define to_bmc_device(x) container_of((x), struct bmc_device, pdev.dev)
207 * Various statistics for IPMI, these index stats[] in the ipmi_smi
210 enum ipmi_stat_indexes {
211 /* Commands we got from the user that were invalid. */
212 IPMI_STAT_sent_invalid_commands = 0,
214 /* Commands we sent to the MC. */
215 IPMI_STAT_sent_local_commands,
217 /* Responses from the MC that were delivered to a user. */
218 IPMI_STAT_handled_local_responses,
220 /* Responses from the MC that were not delivered to a user. */
221 IPMI_STAT_unhandled_local_responses,
223 /* Commands we sent out to the IPMB bus. */
224 IPMI_STAT_sent_ipmb_commands,
226 /* Commands sent on the IPMB that had errors on the SEND CMD */
227 IPMI_STAT_sent_ipmb_command_errs,
229 /* Each retransmit increments this count. */
230 IPMI_STAT_retransmitted_ipmb_commands,
233 * When a message times out (runs out of retransmits) this is
236 IPMI_STAT_timed_out_ipmb_commands,
239 * This is like above, but for broadcasts. Broadcasts are
240 * *not* included in the above count (they are expected to
243 IPMI_STAT_timed_out_ipmb_broadcasts,
245 /* Responses I have sent to the IPMB bus. */
246 IPMI_STAT_sent_ipmb_responses,
248 /* The response was delivered to the user. */
249 IPMI_STAT_handled_ipmb_responses,
251 /* The response had invalid data in it. */
252 IPMI_STAT_invalid_ipmb_responses,
254 /* The response didn't have anyone waiting for it. */
255 IPMI_STAT_unhandled_ipmb_responses,
257 /* Commands we sent out to the IPMB bus. */
258 IPMI_STAT_sent_lan_commands,
260 /* Commands sent on the IPMB that had errors on the SEND CMD */
261 IPMI_STAT_sent_lan_command_errs,
263 /* Each retransmit increments this count. */
264 IPMI_STAT_retransmitted_lan_commands,
267 * When a message times out (runs out of retransmits) this is
270 IPMI_STAT_timed_out_lan_commands,
272 /* Responses I have sent to the IPMB bus. */
273 IPMI_STAT_sent_lan_responses,
275 /* The response was delivered to the user. */
276 IPMI_STAT_handled_lan_responses,
278 /* The response had invalid data in it. */
279 IPMI_STAT_invalid_lan_responses,
281 /* The response didn't have anyone waiting for it. */
282 IPMI_STAT_unhandled_lan_responses,
284 /* The command was delivered to the user. */
285 IPMI_STAT_handled_commands,
287 /* The command had invalid data in it. */
288 IPMI_STAT_invalid_commands,
290 /* The command didn't have anyone waiting for it. */
291 IPMI_STAT_unhandled_commands,
293 /* Invalid data in an event. */
294 IPMI_STAT_invalid_events,
296 /* Events that were received with the proper format. */
299 /* Retransmissions on IPMB that failed. */
300 IPMI_STAT_dropped_rexmit_ipmb_commands,
302 /* Retransmissions on LAN that failed. */
303 IPMI_STAT_dropped_rexmit_lan_commands,
305 /* This *must* remain last, add new values above this. */
310 #define IPMI_IPMB_NUM_SEQ 64
311 #define IPMI_MAX_CHANNELS 16
313 /* What interface number are we? */
316 struct kref refcount;
318 /* Set when the interface is being unregistered. */
321 /* Used for a list of interfaces. */
322 struct list_head link;
325 * The list of upper layers that are using me. seq_lock
328 struct list_head users;
330 /* Information to supply to users. */
331 unsigned char ipmi_version_major;
332 unsigned char ipmi_version_minor;
334 /* Used for wake ups at startup. */
335 wait_queue_head_t waitq;
337 struct bmc_device *bmc;
341 * This is the lower-layer's sender routine. Note that you
342 * must either be holding the ipmi_interfaces_mutex or be in
343 * an umpreemptible region to use this. You must fetch the
344 * value into a local variable and make sure it is not NULL.
346 const struct ipmi_smi_handlers *handlers;
349 #ifdef CONFIG_PROC_FS
350 /* A list of proc entries for this interface. */
351 struct mutex proc_entry_lock;
352 struct ipmi_proc_entry *proc_entries;
355 /* Driver-model device for the system interface. */
356 struct device *si_dev;
359 * A table of sequence numbers for this interface. We use the
360 * sequence numbers for IPMB messages that go out of the
361 * interface to match them up with their responses. A routine
362 * is called periodically to time the items in this list.
365 struct seq_table seq_table[IPMI_IPMB_NUM_SEQ];
369 * Messages queued for delivery. If delivery fails (out of memory
370 * for instance), They will stay in here to be processed later in a
371 * periodic timer interrupt. The tasklet is for handling received
372 * messages directly from the handler.
374 spinlock_t waiting_rcv_msgs_lock;
375 struct list_head waiting_rcv_msgs;
376 atomic_t watchdog_pretimeouts_to_deliver;
377 struct tasklet_struct recv_tasklet;
379 spinlock_t xmit_msgs_lock;
380 struct list_head xmit_msgs;
381 struct ipmi_smi_msg *curr_msg;
382 struct list_head hp_xmit_msgs;
385 * The list of command receivers that are registered for commands
388 struct mutex cmd_rcvrs_mutex;
389 struct list_head cmd_rcvrs;
392 * Events that were queues because no one was there to receive
395 spinlock_t events_lock; /* For dealing with event stuff. */
396 struct list_head waiting_events;
397 unsigned int waiting_events_count; /* How many events in queue? */
398 char delivering_events;
399 char event_msg_printed;
400 atomic_t event_waiters;
401 unsigned int ticks_to_req_ev;
402 int last_needs_timer;
405 * The event receiver for my BMC, only really used at panic
406 * shutdown as a place to store this.
408 unsigned char event_receiver;
409 unsigned char event_receiver_lun;
410 unsigned char local_sel_device;
411 unsigned char local_event_generator;
413 /* For handling of maintenance mode. */
414 int maintenance_mode;
415 bool maintenance_mode_enable;
416 int auto_maintenance_timeout;
417 spinlock_t maintenance_mode_lock; /* Used in a timer... */
420 * A cheap hack, if this is non-null and a message to an
421 * interface comes in with a NULL user, call this routine with
422 * it. Note that the message will still be freed by the
423 * caller. This only works on the system interface.
425 void (*null_user_handler)(ipmi_smi_t intf, struct ipmi_recv_msg *msg);
428 * When we are scanning the channels for an SMI, this will
429 * tell which channel we are scanning.
433 /* Channel information */
434 struct ipmi_channel channels[IPMI_MAX_CHANNELS];
437 struct proc_dir_entry *proc_dir;
438 char proc_dir_name[10];
440 atomic_t stats[IPMI_NUM_STATS];
443 * run_to_completion duplicate of smb_info, smi_info
444 * and ipmi_serial_info structures. Used to decrease numbers of
445 * parameters passed by "low" level IPMI code.
447 int run_to_completion;
449 #define to_si_intf_from_dev(device) container_of(device, struct ipmi_smi, dev)
452 * The driver model view of the IPMI messaging driver.
454 static struct platform_driver ipmidriver = {
457 .bus = &platform_bus_type
460 static DEFINE_MUTEX(ipmidriver_mutex);
462 static LIST_HEAD(ipmi_interfaces);
463 static DEFINE_MUTEX(ipmi_interfaces_mutex);
466 * List of watchers that want to know when smi's are added and deleted.
468 static LIST_HEAD(smi_watchers);
469 static DEFINE_MUTEX(smi_watchers_mutex);
471 #define ipmi_inc_stat(intf, stat) \
472 atomic_inc(&(intf)->stats[IPMI_STAT_ ## stat])
473 #define ipmi_get_stat(intf, stat) \
474 ((unsigned int) atomic_read(&(intf)->stats[IPMI_STAT_ ## stat]))
476 static const char * const addr_src_to_str[] = {
477 "invalid", "hotmod", "hardcoded", "SPMI", "ACPI", "SMBIOS", "PCI",
481 const char *ipmi_addr_src_to_str(enum ipmi_addr_src src)
484 src = 0; /* Invalid */
485 return addr_src_to_str[src];
487 EXPORT_SYMBOL(ipmi_addr_src_to_str);
489 static int is_lan_addr(struct ipmi_addr *addr)
491 return addr->addr_type == IPMI_LAN_ADDR_TYPE;
494 static int is_ipmb_addr(struct ipmi_addr *addr)
496 return addr->addr_type == IPMI_IPMB_ADDR_TYPE;
499 static int is_ipmb_bcast_addr(struct ipmi_addr *addr)
501 return addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE;
504 static void free_recv_msg_list(struct list_head *q)
506 struct ipmi_recv_msg *msg, *msg2;
508 list_for_each_entry_safe(msg, msg2, q, link) {
509 list_del(&msg->link);
510 ipmi_free_recv_msg(msg);
514 static void free_smi_msg_list(struct list_head *q)
516 struct ipmi_smi_msg *msg, *msg2;
518 list_for_each_entry_safe(msg, msg2, q, link) {
519 list_del(&msg->link);
520 ipmi_free_smi_msg(msg);
524 static void clean_up_interface_data(ipmi_smi_t intf)
527 struct cmd_rcvr *rcvr, *rcvr2;
528 struct list_head list;
530 tasklet_kill(&intf->recv_tasklet);
532 free_smi_msg_list(&intf->waiting_rcv_msgs);
533 free_recv_msg_list(&intf->waiting_events);
536 * Wholesale remove all the entries from the list in the
537 * interface and wait for RCU to know that none are in use.
539 mutex_lock(&intf->cmd_rcvrs_mutex);
540 INIT_LIST_HEAD(&list);
541 list_splice_init_rcu(&intf->cmd_rcvrs, &list, synchronize_rcu);
542 mutex_unlock(&intf->cmd_rcvrs_mutex);
544 list_for_each_entry_safe(rcvr, rcvr2, &list, link)
547 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
548 if ((intf->seq_table[i].inuse)
549 && (intf->seq_table[i].recv_msg))
550 ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
554 static void intf_free(struct kref *ref)
556 ipmi_smi_t intf = container_of(ref, struct ipmi_smi, refcount);
558 clean_up_interface_data(intf);
562 struct watcher_entry {
565 struct list_head link;
568 int ipmi_smi_watcher_register(struct ipmi_smi_watcher *watcher)
571 LIST_HEAD(to_deliver);
572 struct watcher_entry *e, *e2;
574 mutex_lock(&smi_watchers_mutex);
576 mutex_lock(&ipmi_interfaces_mutex);
578 /* Build a list of things to deliver. */
579 list_for_each_entry(intf, &ipmi_interfaces, link) {
580 if (intf->intf_num == -1)
582 e = kmalloc(sizeof(*e), GFP_KERNEL);
585 kref_get(&intf->refcount);
587 e->intf_num = intf->intf_num;
588 list_add_tail(&e->link, &to_deliver);
591 /* We will succeed, so add it to the list. */
592 list_add(&watcher->link, &smi_watchers);
594 mutex_unlock(&ipmi_interfaces_mutex);
596 list_for_each_entry_safe(e, e2, &to_deliver, link) {
598 watcher->new_smi(e->intf_num, e->intf->si_dev);
599 kref_put(&e->intf->refcount, intf_free);
603 mutex_unlock(&smi_watchers_mutex);
608 mutex_unlock(&ipmi_interfaces_mutex);
609 mutex_unlock(&smi_watchers_mutex);
610 list_for_each_entry_safe(e, e2, &to_deliver, link) {
612 kref_put(&e->intf->refcount, intf_free);
617 EXPORT_SYMBOL(ipmi_smi_watcher_register);
619 int ipmi_smi_watcher_unregister(struct ipmi_smi_watcher *watcher)
621 mutex_lock(&smi_watchers_mutex);
622 list_del(&(watcher->link));
623 mutex_unlock(&smi_watchers_mutex);
626 EXPORT_SYMBOL(ipmi_smi_watcher_unregister);
629 * Must be called with smi_watchers_mutex held.
632 call_smi_watchers(int i, struct device *dev)
634 struct ipmi_smi_watcher *w;
636 list_for_each_entry(w, &smi_watchers, link) {
637 if (try_module_get(w->owner)) {
639 module_put(w->owner);
645 ipmi_addr_equal(struct ipmi_addr *addr1, struct ipmi_addr *addr2)
647 if (addr1->addr_type != addr2->addr_type)
650 if (addr1->channel != addr2->channel)
653 if (addr1->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
654 struct ipmi_system_interface_addr *smi_addr1
655 = (struct ipmi_system_interface_addr *) addr1;
656 struct ipmi_system_interface_addr *smi_addr2
657 = (struct ipmi_system_interface_addr *) addr2;
658 return (smi_addr1->lun == smi_addr2->lun);
661 if (is_ipmb_addr(addr1) || is_ipmb_bcast_addr(addr1)) {
662 struct ipmi_ipmb_addr *ipmb_addr1
663 = (struct ipmi_ipmb_addr *) addr1;
664 struct ipmi_ipmb_addr *ipmb_addr2
665 = (struct ipmi_ipmb_addr *) addr2;
667 return ((ipmb_addr1->slave_addr == ipmb_addr2->slave_addr)
668 && (ipmb_addr1->lun == ipmb_addr2->lun));
671 if (is_lan_addr(addr1)) {
672 struct ipmi_lan_addr *lan_addr1
673 = (struct ipmi_lan_addr *) addr1;
674 struct ipmi_lan_addr *lan_addr2
675 = (struct ipmi_lan_addr *) addr2;
677 return ((lan_addr1->remote_SWID == lan_addr2->remote_SWID)
678 && (lan_addr1->local_SWID == lan_addr2->local_SWID)
679 && (lan_addr1->session_handle
680 == lan_addr2->session_handle)
681 && (lan_addr1->lun == lan_addr2->lun));
687 int ipmi_validate_addr(struct ipmi_addr *addr, int len)
689 if (len < sizeof(struct ipmi_system_interface_addr))
692 if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
693 if (addr->channel != IPMI_BMC_CHANNEL)
698 if ((addr->channel == IPMI_BMC_CHANNEL)
699 || (addr->channel >= IPMI_MAX_CHANNELS)
700 || (addr->channel < 0))
703 if (is_ipmb_addr(addr) || is_ipmb_bcast_addr(addr)) {
704 if (len < sizeof(struct ipmi_ipmb_addr))
709 if (is_lan_addr(addr)) {
710 if (len < sizeof(struct ipmi_lan_addr))
717 EXPORT_SYMBOL(ipmi_validate_addr);
719 unsigned int ipmi_addr_length(int addr_type)
721 if (addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
722 return sizeof(struct ipmi_system_interface_addr);
724 if ((addr_type == IPMI_IPMB_ADDR_TYPE)
725 || (addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE))
726 return sizeof(struct ipmi_ipmb_addr);
728 if (addr_type == IPMI_LAN_ADDR_TYPE)
729 return sizeof(struct ipmi_lan_addr);
733 EXPORT_SYMBOL(ipmi_addr_length);
735 static void deliver_response(struct ipmi_recv_msg *msg)
738 ipmi_smi_t intf = msg->user_msg_data;
740 /* Special handling for NULL users. */
741 if (intf->null_user_handler) {
742 intf->null_user_handler(intf, msg);
743 ipmi_inc_stat(intf, handled_local_responses);
745 /* No handler, so give up. */
746 ipmi_inc_stat(intf, unhandled_local_responses);
748 ipmi_free_recv_msg(msg);
749 } else if (!oops_in_progress) {
751 * If we are running in the panic context, calling the
752 * receive handler doesn't much meaning and has a deadlock
753 * risk. At this moment, simply skip it in that case.
756 ipmi_user_t user = msg->user;
757 user->handler->ipmi_recv_hndl(msg, user->handler_data);
762 deliver_err_response(struct ipmi_recv_msg *msg, int err)
764 msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
765 msg->msg_data[0] = err;
766 msg->msg.netfn |= 1; /* Convert to a response. */
767 msg->msg.data_len = 1;
768 msg->msg.data = msg->msg_data;
769 deliver_response(msg);
773 * Find the next sequence number not being used and add the given
774 * message with the given timeout to the sequence table. This must be
775 * called with the interface's seq_lock held.
777 static int intf_next_seq(ipmi_smi_t intf,
778 struct ipmi_recv_msg *recv_msg,
779 unsigned long timeout,
788 for (i = intf->curr_seq; (i+1)%IPMI_IPMB_NUM_SEQ != intf->curr_seq;
789 i = (i+1)%IPMI_IPMB_NUM_SEQ) {
790 if (!intf->seq_table[i].inuse)
794 if (!intf->seq_table[i].inuse) {
795 intf->seq_table[i].recv_msg = recv_msg;
798 * Start with the maximum timeout, when the send response
799 * comes in we will start the real timer.
801 intf->seq_table[i].timeout = MAX_MSG_TIMEOUT;
802 intf->seq_table[i].orig_timeout = timeout;
803 intf->seq_table[i].retries_left = retries;
804 intf->seq_table[i].broadcast = broadcast;
805 intf->seq_table[i].inuse = 1;
806 intf->seq_table[i].seqid = NEXT_SEQID(intf->seq_table[i].seqid);
808 *seqid = intf->seq_table[i].seqid;
809 intf->curr_seq = (i+1)%IPMI_IPMB_NUM_SEQ;
819 * Return the receive message for the given sequence number and
820 * release the sequence number so it can be reused. Some other data
821 * is passed in to be sure the message matches up correctly (to help
822 * guard against message coming in after their timeout and the
823 * sequence number being reused).
825 static int intf_find_seq(ipmi_smi_t intf,
830 struct ipmi_addr *addr,
831 struct ipmi_recv_msg **recv_msg)
836 if (seq >= IPMI_IPMB_NUM_SEQ)
839 spin_lock_irqsave(&(intf->seq_lock), flags);
840 if (intf->seq_table[seq].inuse) {
841 struct ipmi_recv_msg *msg = intf->seq_table[seq].recv_msg;
843 if ((msg->addr.channel == channel) && (msg->msg.cmd == cmd)
844 && (msg->msg.netfn == netfn)
845 && (ipmi_addr_equal(addr, &(msg->addr)))) {
847 intf->seq_table[seq].inuse = 0;
851 spin_unlock_irqrestore(&(intf->seq_lock), flags);
857 /* Start the timer for a specific sequence table entry. */
858 static int intf_start_seq_timer(ipmi_smi_t intf,
867 GET_SEQ_FROM_MSGID(msgid, seq, seqid);
869 spin_lock_irqsave(&(intf->seq_lock), flags);
871 * We do this verification because the user can be deleted
872 * while a message is outstanding.
874 if ((intf->seq_table[seq].inuse)
875 && (intf->seq_table[seq].seqid == seqid)) {
876 struct seq_table *ent = &(intf->seq_table[seq]);
877 ent->timeout = ent->orig_timeout;
880 spin_unlock_irqrestore(&(intf->seq_lock), flags);
885 /* Got an error for the send message for a specific sequence number. */
886 static int intf_err_seq(ipmi_smi_t intf,
894 struct ipmi_recv_msg *msg = NULL;
897 GET_SEQ_FROM_MSGID(msgid, seq, seqid);
899 spin_lock_irqsave(&(intf->seq_lock), flags);
901 * We do this verification because the user can be deleted
902 * while a message is outstanding.
904 if ((intf->seq_table[seq].inuse)
905 && (intf->seq_table[seq].seqid == seqid)) {
906 struct seq_table *ent = &(intf->seq_table[seq]);
912 spin_unlock_irqrestore(&(intf->seq_lock), flags);
915 deliver_err_response(msg, err);
921 int ipmi_create_user(unsigned int if_num,
922 const struct ipmi_user_hndl *handler,
927 ipmi_user_t new_user;
932 * There is no module usecount here, because it's not
933 * required. Since this can only be used by and called from
934 * other modules, they will implicitly use this module, and
935 * thus this can't be removed unless the other modules are
943 * Make sure the driver is actually initialized, this handles
944 * problems with initialization order.
947 rv = ipmi_init_msghandler();
952 * The init code doesn't return an error if it was turned
953 * off, but it won't initialize. Check that.
959 new_user = kmalloc(sizeof(*new_user), GFP_KERNEL);
963 mutex_lock(&ipmi_interfaces_mutex);
964 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
965 if (intf->intf_num == if_num)
968 /* Not found, return an error */
973 /* Note that each existing user holds a refcount to the interface. */
974 kref_get(&intf->refcount);
976 kref_init(&new_user->refcount);
977 new_user->handler = handler;
978 new_user->handler_data = handler_data;
979 new_user->intf = intf;
980 new_user->gets_events = false;
982 if (!try_module_get(intf->handlers->owner)) {
987 if (intf->handlers->inc_usecount) {
988 rv = intf->handlers->inc_usecount(intf->send_info);
990 module_put(intf->handlers->owner);
996 * Hold the lock so intf->handlers is guaranteed to be good
999 mutex_unlock(&ipmi_interfaces_mutex);
1001 new_user->valid = true;
1002 spin_lock_irqsave(&intf->seq_lock, flags);
1003 list_add_rcu(&new_user->link, &intf->users);
1004 spin_unlock_irqrestore(&intf->seq_lock, flags);
1005 if (handler->ipmi_watchdog_pretimeout) {
1006 /* User wants pretimeouts, so make sure to watch for them. */
1007 if (atomic_inc_return(&intf->event_waiters) == 1)
1014 kref_put(&intf->refcount, intf_free);
1016 mutex_unlock(&ipmi_interfaces_mutex);
1020 EXPORT_SYMBOL(ipmi_create_user);
1022 int ipmi_get_smi_info(int if_num, struct ipmi_smi_info *data)
1026 const struct ipmi_smi_handlers *handlers;
1028 mutex_lock(&ipmi_interfaces_mutex);
1029 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
1030 if (intf->intf_num == if_num)
1033 /* Not found, return an error */
1035 mutex_unlock(&ipmi_interfaces_mutex);
1039 handlers = intf->handlers;
1041 if (handlers->get_smi_info)
1042 rv = handlers->get_smi_info(intf->send_info, data);
1043 mutex_unlock(&ipmi_interfaces_mutex);
1047 EXPORT_SYMBOL(ipmi_get_smi_info);
1049 static void free_user(struct kref *ref)
1051 ipmi_user_t user = container_of(ref, struct ipmi_user, refcount);
1055 int ipmi_destroy_user(ipmi_user_t user)
1057 ipmi_smi_t intf = user->intf;
1059 unsigned long flags;
1060 struct cmd_rcvr *rcvr;
1061 struct cmd_rcvr *rcvrs = NULL;
1063 user->valid = false;
1065 if (user->handler->ipmi_watchdog_pretimeout)
1066 atomic_dec(&intf->event_waiters);
1068 if (user->gets_events)
1069 atomic_dec(&intf->event_waiters);
1071 /* Remove the user from the interface's sequence table. */
1072 spin_lock_irqsave(&intf->seq_lock, flags);
1073 list_del_rcu(&user->link);
1075 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
1076 if (intf->seq_table[i].inuse
1077 && (intf->seq_table[i].recv_msg->user == user)) {
1078 intf->seq_table[i].inuse = 0;
1079 ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
1082 spin_unlock_irqrestore(&intf->seq_lock, flags);
1085 * Remove the user from the command receiver's table. First
1086 * we build a list of everything (not using the standard link,
1087 * since other things may be using it till we do
1088 * synchronize_rcu()) then free everything in that list.
1090 mutex_lock(&intf->cmd_rcvrs_mutex);
1091 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
1092 if (rcvr->user == user) {
1093 list_del_rcu(&rcvr->link);
1098 mutex_unlock(&intf->cmd_rcvrs_mutex);
1106 mutex_lock(&ipmi_interfaces_mutex);
1107 if (intf->handlers) {
1108 module_put(intf->handlers->owner);
1109 if (intf->handlers->dec_usecount)
1110 intf->handlers->dec_usecount(intf->send_info);
1112 mutex_unlock(&ipmi_interfaces_mutex);
1114 kref_put(&intf->refcount, intf_free);
1116 kref_put(&user->refcount, free_user);
1120 EXPORT_SYMBOL(ipmi_destroy_user);
1122 void ipmi_get_version(ipmi_user_t user,
1123 unsigned char *major,
1124 unsigned char *minor)
1126 *major = user->intf->ipmi_version_major;
1127 *minor = user->intf->ipmi_version_minor;
1129 EXPORT_SYMBOL(ipmi_get_version);
1131 int ipmi_set_my_address(ipmi_user_t user,
1132 unsigned int channel,
1133 unsigned char address)
1135 if (channel >= IPMI_MAX_CHANNELS)
1137 user->intf->channels[channel].address = address;
1140 EXPORT_SYMBOL(ipmi_set_my_address);
1142 int ipmi_get_my_address(ipmi_user_t user,
1143 unsigned int channel,
1144 unsigned char *address)
1146 if (channel >= IPMI_MAX_CHANNELS)
1148 *address = user->intf->channels[channel].address;
1151 EXPORT_SYMBOL(ipmi_get_my_address);
1153 int ipmi_set_my_LUN(ipmi_user_t user,
1154 unsigned int channel,
1157 if (channel >= IPMI_MAX_CHANNELS)
1159 user->intf->channels[channel].lun = LUN & 0x3;
1162 EXPORT_SYMBOL(ipmi_set_my_LUN);
1164 int ipmi_get_my_LUN(ipmi_user_t user,
1165 unsigned int channel,
1166 unsigned char *address)
1168 if (channel >= IPMI_MAX_CHANNELS)
1170 *address = user->intf->channels[channel].lun;
1173 EXPORT_SYMBOL(ipmi_get_my_LUN);
1175 int ipmi_get_maintenance_mode(ipmi_user_t user)
1178 unsigned long flags;
1180 spin_lock_irqsave(&user->intf->maintenance_mode_lock, flags);
1181 mode = user->intf->maintenance_mode;
1182 spin_unlock_irqrestore(&user->intf->maintenance_mode_lock, flags);
1186 EXPORT_SYMBOL(ipmi_get_maintenance_mode);
1188 static void maintenance_mode_update(ipmi_smi_t intf)
1190 if (intf->handlers->set_maintenance_mode)
1191 intf->handlers->set_maintenance_mode(
1192 intf->send_info, intf->maintenance_mode_enable);
1195 int ipmi_set_maintenance_mode(ipmi_user_t user, int mode)
1198 unsigned long flags;
1199 ipmi_smi_t intf = user->intf;
1201 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
1202 if (intf->maintenance_mode != mode) {
1204 case IPMI_MAINTENANCE_MODE_AUTO:
1205 intf->maintenance_mode_enable
1206 = (intf->auto_maintenance_timeout > 0);
1209 case IPMI_MAINTENANCE_MODE_OFF:
1210 intf->maintenance_mode_enable = false;
1213 case IPMI_MAINTENANCE_MODE_ON:
1214 intf->maintenance_mode_enable = true;
1221 intf->maintenance_mode = mode;
1223 maintenance_mode_update(intf);
1226 spin_unlock_irqrestore(&intf->maintenance_mode_lock, flags);
1230 EXPORT_SYMBOL(ipmi_set_maintenance_mode);
1232 int ipmi_set_gets_events(ipmi_user_t user, bool val)
1234 unsigned long flags;
1235 ipmi_smi_t intf = user->intf;
1236 struct ipmi_recv_msg *msg, *msg2;
1237 struct list_head msgs;
1239 INIT_LIST_HEAD(&msgs);
1241 spin_lock_irqsave(&intf->events_lock, flags);
1242 if (user->gets_events == val)
1245 user->gets_events = val;
1248 if (atomic_inc_return(&intf->event_waiters) == 1)
1251 atomic_dec(&intf->event_waiters);
1254 if (intf->delivering_events)
1256 * Another thread is delivering events for this, so
1257 * let it handle any new events.
1261 /* Deliver any queued events. */
1262 while (user->gets_events && !list_empty(&intf->waiting_events)) {
1263 list_for_each_entry_safe(msg, msg2, &intf->waiting_events, link)
1264 list_move_tail(&msg->link, &msgs);
1265 intf->waiting_events_count = 0;
1266 if (intf->event_msg_printed) {
1267 printk(KERN_WARNING PFX "Event queue no longer"
1269 intf->event_msg_printed = 0;
1272 intf->delivering_events = 1;
1273 spin_unlock_irqrestore(&intf->events_lock, flags);
1275 list_for_each_entry_safe(msg, msg2, &msgs, link) {
1277 kref_get(&user->refcount);
1278 deliver_response(msg);
1281 spin_lock_irqsave(&intf->events_lock, flags);
1282 intf->delivering_events = 0;
1286 spin_unlock_irqrestore(&intf->events_lock, flags);
1290 EXPORT_SYMBOL(ipmi_set_gets_events);
1292 static struct cmd_rcvr *find_cmd_rcvr(ipmi_smi_t intf,
1293 unsigned char netfn,
1297 struct cmd_rcvr *rcvr;
1299 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
1300 if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)
1301 && (rcvr->chans & (1 << chan)))
1307 static int is_cmd_rcvr_exclusive(ipmi_smi_t intf,
1308 unsigned char netfn,
1312 struct cmd_rcvr *rcvr;
1314 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
1315 if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)
1316 && (rcvr->chans & chans))
1322 int ipmi_register_for_cmd(ipmi_user_t user,
1323 unsigned char netfn,
1327 ipmi_smi_t intf = user->intf;
1328 struct cmd_rcvr *rcvr;
1332 rcvr = kmalloc(sizeof(*rcvr), GFP_KERNEL);
1336 rcvr->netfn = netfn;
1337 rcvr->chans = chans;
1340 mutex_lock(&intf->cmd_rcvrs_mutex);
1341 /* Make sure the command/netfn is not already registered. */
1342 if (!is_cmd_rcvr_exclusive(intf, netfn, cmd, chans)) {
1347 if (atomic_inc_return(&intf->event_waiters) == 1)
1350 list_add_rcu(&rcvr->link, &intf->cmd_rcvrs);
1353 mutex_unlock(&intf->cmd_rcvrs_mutex);
1359 EXPORT_SYMBOL(ipmi_register_for_cmd);
1361 int ipmi_unregister_for_cmd(ipmi_user_t user,
1362 unsigned char netfn,
1366 ipmi_smi_t intf = user->intf;
1367 struct cmd_rcvr *rcvr;
1368 struct cmd_rcvr *rcvrs = NULL;
1369 int i, rv = -ENOENT;
1371 mutex_lock(&intf->cmd_rcvrs_mutex);
1372 for (i = 0; i < IPMI_NUM_CHANNELS; i++) {
1373 if (((1 << i) & chans) == 0)
1375 rcvr = find_cmd_rcvr(intf, netfn, cmd, i);
1378 if (rcvr->user == user) {
1380 rcvr->chans &= ~chans;
1381 if (rcvr->chans == 0) {
1382 list_del_rcu(&rcvr->link);
1388 mutex_unlock(&intf->cmd_rcvrs_mutex);
1391 atomic_dec(&intf->event_waiters);
1398 EXPORT_SYMBOL(ipmi_unregister_for_cmd);
1400 static unsigned char
1401 ipmb_checksum(unsigned char *data, int size)
1403 unsigned char csum = 0;
1405 for (; size > 0; size--, data++)
1411 static inline void format_ipmb_msg(struct ipmi_smi_msg *smi_msg,
1412 struct kernel_ipmi_msg *msg,
1413 struct ipmi_ipmb_addr *ipmb_addr,
1415 unsigned char ipmb_seq,
1417 unsigned char source_address,
1418 unsigned char source_lun)
1422 /* Format the IPMB header data. */
1423 smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
1424 smi_msg->data[1] = IPMI_SEND_MSG_CMD;
1425 smi_msg->data[2] = ipmb_addr->channel;
1427 smi_msg->data[3] = 0;
1428 smi_msg->data[i+3] = ipmb_addr->slave_addr;
1429 smi_msg->data[i+4] = (msg->netfn << 2) | (ipmb_addr->lun & 0x3);
1430 smi_msg->data[i+5] = ipmb_checksum(&(smi_msg->data[i+3]), 2);
1431 smi_msg->data[i+6] = source_address;
1432 smi_msg->data[i+7] = (ipmb_seq << 2) | source_lun;
1433 smi_msg->data[i+8] = msg->cmd;
1435 /* Now tack on the data to the message. */
1436 if (msg->data_len > 0)
1437 memcpy(&(smi_msg->data[i+9]), msg->data,
1439 smi_msg->data_size = msg->data_len + 9;
1441 /* Now calculate the checksum and tack it on. */
1442 smi_msg->data[i+smi_msg->data_size]
1443 = ipmb_checksum(&(smi_msg->data[i+6]),
1444 smi_msg->data_size-6);
1447 * Add on the checksum size and the offset from the
1450 smi_msg->data_size += 1 + i;
1452 smi_msg->msgid = msgid;
1455 static inline void format_lan_msg(struct ipmi_smi_msg *smi_msg,
1456 struct kernel_ipmi_msg *msg,
1457 struct ipmi_lan_addr *lan_addr,
1459 unsigned char ipmb_seq,
1460 unsigned char source_lun)
1462 /* Format the IPMB header data. */
1463 smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
1464 smi_msg->data[1] = IPMI_SEND_MSG_CMD;
1465 smi_msg->data[2] = lan_addr->channel;
1466 smi_msg->data[3] = lan_addr->session_handle;
1467 smi_msg->data[4] = lan_addr->remote_SWID;
1468 smi_msg->data[5] = (msg->netfn << 2) | (lan_addr->lun & 0x3);
1469 smi_msg->data[6] = ipmb_checksum(&(smi_msg->data[4]), 2);
1470 smi_msg->data[7] = lan_addr->local_SWID;
1471 smi_msg->data[8] = (ipmb_seq << 2) | source_lun;
1472 smi_msg->data[9] = msg->cmd;
1474 /* Now tack on the data to the message. */
1475 if (msg->data_len > 0)
1476 memcpy(&(smi_msg->data[10]), msg->data,
1478 smi_msg->data_size = msg->data_len + 10;
1480 /* Now calculate the checksum and tack it on. */
1481 smi_msg->data[smi_msg->data_size]
1482 = ipmb_checksum(&(smi_msg->data[7]),
1483 smi_msg->data_size-7);
1486 * Add on the checksum size and the offset from the
1489 smi_msg->data_size += 1;
1491 smi_msg->msgid = msgid;
1494 static struct ipmi_smi_msg *smi_add_send_msg(ipmi_smi_t intf,
1495 struct ipmi_smi_msg *smi_msg,
1498 if (intf->curr_msg) {
1500 list_add_tail(&smi_msg->link, &intf->hp_xmit_msgs);
1502 list_add_tail(&smi_msg->link, &intf->xmit_msgs);
1505 intf->curr_msg = smi_msg;
1512 static void smi_send(ipmi_smi_t intf, const struct ipmi_smi_handlers *handlers,
1513 struct ipmi_smi_msg *smi_msg, int priority)
1515 int run_to_completion = intf->run_to_completion;
1517 if (run_to_completion) {
1518 smi_msg = smi_add_send_msg(intf, smi_msg, priority);
1520 unsigned long flags;
1522 spin_lock_irqsave(&intf->xmit_msgs_lock, flags);
1523 smi_msg = smi_add_send_msg(intf, smi_msg, priority);
1524 spin_unlock_irqrestore(&intf->xmit_msgs_lock, flags);
1528 handlers->sender(intf->send_info, smi_msg);
1532 * Separate from ipmi_request so that the user does not have to be
1533 * supplied in certain circumstances (mainly at panic time). If
1534 * messages are supplied, they will be freed, even if an error
1537 static int i_ipmi_request(ipmi_user_t user,
1539 struct ipmi_addr *addr,
1541 struct kernel_ipmi_msg *msg,
1542 void *user_msg_data,
1544 struct ipmi_recv_msg *supplied_recv,
1546 unsigned char source_address,
1547 unsigned char source_lun,
1549 unsigned int retry_time_ms)
1552 struct ipmi_smi_msg *smi_msg;
1553 struct ipmi_recv_msg *recv_msg;
1554 unsigned long flags;
1558 recv_msg = supplied_recv;
1560 recv_msg = ipmi_alloc_recv_msg();
1561 if (recv_msg == NULL)
1564 recv_msg->user_msg_data = user_msg_data;
1567 smi_msg = (struct ipmi_smi_msg *) supplied_smi;
1569 smi_msg = ipmi_alloc_smi_msg();
1570 if (smi_msg == NULL) {
1571 ipmi_free_recv_msg(recv_msg);
1577 if (intf->in_shutdown) {
1582 recv_msg->user = user;
1584 kref_get(&user->refcount);
1585 recv_msg->msgid = msgid;
1587 * Store the message to send in the receive message so timeout
1588 * responses can get the proper response data.
1590 recv_msg->msg = *msg;
1592 if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
1593 struct ipmi_system_interface_addr *smi_addr;
1595 if (msg->netfn & 1) {
1596 /* Responses are not allowed to the SMI. */
1601 smi_addr = (struct ipmi_system_interface_addr *) addr;
1602 if (smi_addr->lun > 3) {
1603 ipmi_inc_stat(intf, sent_invalid_commands);
1608 memcpy(&recv_msg->addr, smi_addr, sizeof(*smi_addr));
1610 if ((msg->netfn == IPMI_NETFN_APP_REQUEST)
1611 && ((msg->cmd == IPMI_SEND_MSG_CMD)
1612 || (msg->cmd == IPMI_GET_MSG_CMD)
1613 || (msg->cmd == IPMI_READ_EVENT_MSG_BUFFER_CMD))) {
1615 * We don't let the user do these, since we manage
1616 * the sequence numbers.
1618 ipmi_inc_stat(intf, sent_invalid_commands);
1623 if (((msg->netfn == IPMI_NETFN_APP_REQUEST)
1624 && ((msg->cmd == IPMI_COLD_RESET_CMD)
1625 || (msg->cmd == IPMI_WARM_RESET_CMD)))
1626 || (msg->netfn == IPMI_NETFN_FIRMWARE_REQUEST)) {
1627 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
1628 intf->auto_maintenance_timeout
1629 = IPMI_MAINTENANCE_MODE_TIMEOUT;
1630 if (!intf->maintenance_mode
1631 && !intf->maintenance_mode_enable) {
1632 intf->maintenance_mode_enable = true;
1633 maintenance_mode_update(intf);
1635 spin_unlock_irqrestore(&intf->maintenance_mode_lock,
1639 if ((msg->data_len + 2) > IPMI_MAX_MSG_LENGTH) {
1640 ipmi_inc_stat(intf, sent_invalid_commands);
1645 smi_msg->data[0] = (msg->netfn << 2) | (smi_addr->lun & 0x3);
1646 smi_msg->data[1] = msg->cmd;
1647 smi_msg->msgid = msgid;
1648 smi_msg->user_data = recv_msg;
1649 if (msg->data_len > 0)
1650 memcpy(&(smi_msg->data[2]), msg->data, msg->data_len);
1651 smi_msg->data_size = msg->data_len + 2;
1652 ipmi_inc_stat(intf, sent_local_commands);
1653 } else if (is_ipmb_addr(addr) || is_ipmb_bcast_addr(addr)) {
1654 struct ipmi_ipmb_addr *ipmb_addr;
1655 unsigned char ipmb_seq;
1659 if (addr->channel >= IPMI_MAX_CHANNELS) {
1660 ipmi_inc_stat(intf, sent_invalid_commands);
1665 if (intf->channels[addr->channel].medium
1666 != IPMI_CHANNEL_MEDIUM_IPMB) {
1667 ipmi_inc_stat(intf, sent_invalid_commands);
1673 if (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE)
1674 retries = 0; /* Don't retry broadcasts. */
1678 if (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE) {
1680 * Broadcasts add a zero at the beginning of the
1681 * message, but otherwise is the same as an IPMB
1684 addr->addr_type = IPMI_IPMB_ADDR_TYPE;
1689 /* Default to 1 second retries. */
1690 if (retry_time_ms == 0)
1691 retry_time_ms = 1000;
1694 * 9 for the header and 1 for the checksum, plus
1695 * possibly one for the broadcast.
1697 if ((msg->data_len + 10 + broadcast) > IPMI_MAX_MSG_LENGTH) {
1698 ipmi_inc_stat(intf, sent_invalid_commands);
1703 ipmb_addr = (struct ipmi_ipmb_addr *) addr;
1704 if (ipmb_addr->lun > 3) {
1705 ipmi_inc_stat(intf, sent_invalid_commands);
1710 memcpy(&recv_msg->addr, ipmb_addr, sizeof(*ipmb_addr));
1712 if (recv_msg->msg.netfn & 0x1) {
1714 * It's a response, so use the user's sequence
1717 ipmi_inc_stat(intf, sent_ipmb_responses);
1718 format_ipmb_msg(smi_msg, msg, ipmb_addr, msgid,
1720 source_address, source_lun);
1723 * Save the receive message so we can use it
1724 * to deliver the response.
1726 smi_msg->user_data = recv_msg;
1728 /* It's a command, so get a sequence for it. */
1730 spin_lock_irqsave(&(intf->seq_lock), flags);
1733 * Create a sequence number with a 1 second
1734 * timeout and 4 retries.
1736 rv = intf_next_seq(intf,
1745 * We have used up all the sequence numbers,
1746 * probably, so abort.
1748 spin_unlock_irqrestore(&(intf->seq_lock),
1753 ipmi_inc_stat(intf, sent_ipmb_commands);
1756 * Store the sequence number in the message,
1757 * so that when the send message response
1758 * comes back we can start the timer.
1760 format_ipmb_msg(smi_msg, msg, ipmb_addr,
1761 STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
1762 ipmb_seq, broadcast,
1763 source_address, source_lun);
1766 * Copy the message into the recv message data, so we
1767 * can retransmit it later if necessary.
1769 memcpy(recv_msg->msg_data, smi_msg->data,
1770 smi_msg->data_size);
1771 recv_msg->msg.data = recv_msg->msg_data;
1772 recv_msg->msg.data_len = smi_msg->data_size;
1775 * We don't unlock until here, because we need
1776 * to copy the completed message into the
1777 * recv_msg before we release the lock.
1778 * Otherwise, race conditions may bite us. I
1779 * know that's pretty paranoid, but I prefer
1782 spin_unlock_irqrestore(&(intf->seq_lock), flags);
1784 } else if (is_lan_addr(addr)) {
1785 struct ipmi_lan_addr *lan_addr;
1786 unsigned char ipmb_seq;
1789 if (addr->channel >= IPMI_MAX_CHANNELS) {
1790 ipmi_inc_stat(intf, sent_invalid_commands);
1795 if ((intf->channels[addr->channel].medium
1796 != IPMI_CHANNEL_MEDIUM_8023LAN)
1797 && (intf->channels[addr->channel].medium
1798 != IPMI_CHANNEL_MEDIUM_ASYNC)) {
1799 ipmi_inc_stat(intf, sent_invalid_commands);
1806 /* Default to 1 second retries. */
1807 if (retry_time_ms == 0)
1808 retry_time_ms = 1000;
1810 /* 11 for the header and 1 for the checksum. */
1811 if ((msg->data_len + 12) > IPMI_MAX_MSG_LENGTH) {
1812 ipmi_inc_stat(intf, sent_invalid_commands);
1817 lan_addr = (struct ipmi_lan_addr *) addr;
1818 if (lan_addr->lun > 3) {
1819 ipmi_inc_stat(intf, sent_invalid_commands);
1824 memcpy(&recv_msg->addr, lan_addr, sizeof(*lan_addr));
1826 if (recv_msg->msg.netfn & 0x1) {
1828 * It's a response, so use the user's sequence
1831 ipmi_inc_stat(intf, sent_lan_responses);
1832 format_lan_msg(smi_msg, msg, lan_addr, msgid,
1836 * Save the receive message so we can use it
1837 * to deliver the response.
1839 smi_msg->user_data = recv_msg;
1841 /* It's a command, so get a sequence for it. */
1843 spin_lock_irqsave(&(intf->seq_lock), flags);
1846 * Create a sequence number with a 1 second
1847 * timeout and 4 retries.
1849 rv = intf_next_seq(intf,
1858 * We have used up all the sequence numbers,
1859 * probably, so abort.
1861 spin_unlock_irqrestore(&(intf->seq_lock),
1866 ipmi_inc_stat(intf, sent_lan_commands);
1869 * Store the sequence number in the message,
1870 * so that when the send message response
1871 * comes back we can start the timer.
1873 format_lan_msg(smi_msg, msg, lan_addr,
1874 STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
1875 ipmb_seq, source_lun);
1878 * Copy the message into the recv message data, so we
1879 * can retransmit it later if necessary.
1881 memcpy(recv_msg->msg_data, smi_msg->data,
1882 smi_msg->data_size);
1883 recv_msg->msg.data = recv_msg->msg_data;
1884 recv_msg->msg.data_len = smi_msg->data_size;
1887 * We don't unlock until here, because we need
1888 * to copy the completed message into the
1889 * recv_msg before we release the lock.
1890 * Otherwise, race conditions may bite us. I
1891 * know that's pretty paranoid, but I prefer
1894 spin_unlock_irqrestore(&(intf->seq_lock), flags);
1897 /* Unknown address type. */
1898 ipmi_inc_stat(intf, sent_invalid_commands);
1906 for (m = 0; m < smi_msg->data_size; m++)
1907 printk(" %2.2x", smi_msg->data[m]);
1912 smi_send(intf, intf->handlers, smi_msg, priority);
1919 ipmi_free_smi_msg(smi_msg);
1920 ipmi_free_recv_msg(recv_msg);
1924 static int check_addr(ipmi_smi_t intf,
1925 struct ipmi_addr *addr,
1926 unsigned char *saddr,
1929 if (addr->channel >= IPMI_MAX_CHANNELS)
1931 *lun = intf->channels[addr->channel].lun;
1932 *saddr = intf->channels[addr->channel].address;
1936 int ipmi_request_settime(ipmi_user_t user,
1937 struct ipmi_addr *addr,
1939 struct kernel_ipmi_msg *msg,
1940 void *user_msg_data,
1943 unsigned int retry_time_ms)
1945 unsigned char saddr = 0, lun = 0;
1950 rv = check_addr(user->intf, addr, &saddr, &lun);
1953 return i_ipmi_request(user,
1966 EXPORT_SYMBOL(ipmi_request_settime);
1968 int ipmi_request_supply_msgs(ipmi_user_t user,
1969 struct ipmi_addr *addr,
1971 struct kernel_ipmi_msg *msg,
1972 void *user_msg_data,
1974 struct ipmi_recv_msg *supplied_recv,
1977 unsigned char saddr = 0, lun = 0;
1982 rv = check_addr(user->intf, addr, &saddr, &lun);
1985 return i_ipmi_request(user,
1998 EXPORT_SYMBOL(ipmi_request_supply_msgs);
2000 #ifdef CONFIG_PROC_FS
2001 static int smi_ipmb_proc_show(struct seq_file *m, void *v)
2003 ipmi_smi_t intf = m->private;
2006 seq_printf(m, "%x", intf->channels[0].address);
2007 for (i = 1; i < IPMI_MAX_CHANNELS; i++)
2008 seq_printf(m, " %x", intf->channels[i].address);
2014 static int smi_ipmb_proc_open(struct inode *inode, struct file *file)
2016 return single_open(file, smi_ipmb_proc_show, PDE_DATA(inode));
2019 static const struct file_operations smi_ipmb_proc_ops = {
2020 .open = smi_ipmb_proc_open,
2022 .llseek = seq_lseek,
2023 .release = single_release,
2026 static int smi_version_proc_show(struct seq_file *m, void *v)
2028 ipmi_smi_t intf = m->private;
2030 seq_printf(m, "%u.%u\n",
2031 ipmi_version_major(&intf->bmc->id),
2032 ipmi_version_minor(&intf->bmc->id));
2037 static int smi_version_proc_open(struct inode *inode, struct file *file)
2039 return single_open(file, smi_version_proc_show, PDE_DATA(inode));
2042 static const struct file_operations smi_version_proc_ops = {
2043 .open = smi_version_proc_open,
2045 .llseek = seq_lseek,
2046 .release = single_release,
2049 static int smi_stats_proc_show(struct seq_file *m, void *v)
2051 ipmi_smi_t intf = m->private;
2053 seq_printf(m, "sent_invalid_commands: %u\n",
2054 ipmi_get_stat(intf, sent_invalid_commands));
2055 seq_printf(m, "sent_local_commands: %u\n",
2056 ipmi_get_stat(intf, sent_local_commands));
2057 seq_printf(m, "handled_local_responses: %u\n",
2058 ipmi_get_stat(intf, handled_local_responses));
2059 seq_printf(m, "unhandled_local_responses: %u\n",
2060 ipmi_get_stat(intf, unhandled_local_responses));
2061 seq_printf(m, "sent_ipmb_commands: %u\n",
2062 ipmi_get_stat(intf, sent_ipmb_commands));
2063 seq_printf(m, "sent_ipmb_command_errs: %u\n",
2064 ipmi_get_stat(intf, sent_ipmb_command_errs));
2065 seq_printf(m, "retransmitted_ipmb_commands: %u\n",
2066 ipmi_get_stat(intf, retransmitted_ipmb_commands));
2067 seq_printf(m, "timed_out_ipmb_commands: %u\n",
2068 ipmi_get_stat(intf, timed_out_ipmb_commands));
2069 seq_printf(m, "timed_out_ipmb_broadcasts: %u\n",
2070 ipmi_get_stat(intf, timed_out_ipmb_broadcasts));
2071 seq_printf(m, "sent_ipmb_responses: %u\n",
2072 ipmi_get_stat(intf, sent_ipmb_responses));
2073 seq_printf(m, "handled_ipmb_responses: %u\n",
2074 ipmi_get_stat(intf, handled_ipmb_responses));
2075 seq_printf(m, "invalid_ipmb_responses: %u\n",
2076 ipmi_get_stat(intf, invalid_ipmb_responses));
2077 seq_printf(m, "unhandled_ipmb_responses: %u\n",
2078 ipmi_get_stat(intf, unhandled_ipmb_responses));
2079 seq_printf(m, "sent_lan_commands: %u\n",
2080 ipmi_get_stat(intf, sent_lan_commands));
2081 seq_printf(m, "sent_lan_command_errs: %u\n",
2082 ipmi_get_stat(intf, sent_lan_command_errs));
2083 seq_printf(m, "retransmitted_lan_commands: %u\n",
2084 ipmi_get_stat(intf, retransmitted_lan_commands));
2085 seq_printf(m, "timed_out_lan_commands: %u\n",
2086 ipmi_get_stat(intf, timed_out_lan_commands));
2087 seq_printf(m, "sent_lan_responses: %u\n",
2088 ipmi_get_stat(intf, sent_lan_responses));
2089 seq_printf(m, "handled_lan_responses: %u\n",
2090 ipmi_get_stat(intf, handled_lan_responses));
2091 seq_printf(m, "invalid_lan_responses: %u\n",
2092 ipmi_get_stat(intf, invalid_lan_responses));
2093 seq_printf(m, "unhandled_lan_responses: %u\n",
2094 ipmi_get_stat(intf, unhandled_lan_responses));
2095 seq_printf(m, "handled_commands: %u\n",
2096 ipmi_get_stat(intf, handled_commands));
2097 seq_printf(m, "invalid_commands: %u\n",
2098 ipmi_get_stat(intf, invalid_commands));
2099 seq_printf(m, "unhandled_commands: %u\n",
2100 ipmi_get_stat(intf, unhandled_commands));
2101 seq_printf(m, "invalid_events: %u\n",
2102 ipmi_get_stat(intf, invalid_events));
2103 seq_printf(m, "events: %u\n",
2104 ipmi_get_stat(intf, events));
2105 seq_printf(m, "failed rexmit LAN msgs: %u\n",
2106 ipmi_get_stat(intf, dropped_rexmit_lan_commands));
2107 seq_printf(m, "failed rexmit IPMB msgs: %u\n",
2108 ipmi_get_stat(intf, dropped_rexmit_ipmb_commands));
2112 static int smi_stats_proc_open(struct inode *inode, struct file *file)
2114 return single_open(file, smi_stats_proc_show, PDE_DATA(inode));
2117 static const struct file_operations smi_stats_proc_ops = {
2118 .open = smi_stats_proc_open,
2120 .llseek = seq_lseek,
2121 .release = single_release,
2123 #endif /* CONFIG_PROC_FS */
2125 int ipmi_smi_add_proc_entry(ipmi_smi_t smi, char *name,
2126 const struct file_operations *proc_ops,
2130 #ifdef CONFIG_PROC_FS
2131 struct proc_dir_entry *file;
2132 struct ipmi_proc_entry *entry;
2134 /* Create a list element. */
2135 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2138 entry->name = kstrdup(name, GFP_KERNEL);
2144 file = proc_create_data(name, 0, smi->proc_dir, proc_ops, data);
2150 mutex_lock(&smi->proc_entry_lock);
2151 /* Stick it on the list. */
2152 entry->next = smi->proc_entries;
2153 smi->proc_entries = entry;
2154 mutex_unlock(&smi->proc_entry_lock);
2156 #endif /* CONFIG_PROC_FS */
2160 EXPORT_SYMBOL(ipmi_smi_add_proc_entry);
2162 static int add_proc_entries(ipmi_smi_t smi, int num)
2166 #ifdef CONFIG_PROC_FS
2167 sprintf(smi->proc_dir_name, "%d", num);
2168 smi->proc_dir = proc_mkdir(smi->proc_dir_name, proc_ipmi_root);
2173 rv = ipmi_smi_add_proc_entry(smi, "stats",
2174 &smi_stats_proc_ops,
2178 rv = ipmi_smi_add_proc_entry(smi, "ipmb",
2183 rv = ipmi_smi_add_proc_entry(smi, "version",
2184 &smi_version_proc_ops,
2186 #endif /* CONFIG_PROC_FS */
2191 static void remove_proc_entries(ipmi_smi_t smi)
2193 #ifdef CONFIG_PROC_FS
2194 struct ipmi_proc_entry *entry;
2196 mutex_lock(&smi->proc_entry_lock);
2197 while (smi->proc_entries) {
2198 entry = smi->proc_entries;
2199 smi->proc_entries = entry->next;
2201 remove_proc_entry(entry->name, smi->proc_dir);
2205 mutex_unlock(&smi->proc_entry_lock);
2206 remove_proc_entry(smi->proc_dir_name, proc_ipmi_root);
2207 #endif /* CONFIG_PROC_FS */
2210 static int __find_bmc_guid(struct device *dev, void *data)
2212 unsigned char *id = data;
2213 struct bmc_device *bmc = to_bmc_device(dev);
2214 return memcmp(bmc->guid, id, 16) == 0;
2217 static struct bmc_device *ipmi_find_bmc_guid(struct device_driver *drv,
2218 unsigned char *guid)
2222 dev = driver_find_device(drv, NULL, guid, __find_bmc_guid);
2224 return to_bmc_device(dev);
2229 struct prod_dev_id {
2230 unsigned int product_id;
2231 unsigned char device_id;
2234 static int __find_bmc_prod_dev_id(struct device *dev, void *data)
2236 struct prod_dev_id *id = data;
2237 struct bmc_device *bmc = to_bmc_device(dev);
2239 return (bmc->id.product_id == id->product_id
2240 && bmc->id.device_id == id->device_id);
2243 static struct bmc_device *ipmi_find_bmc_prod_dev_id(
2244 struct device_driver *drv,
2245 unsigned int product_id, unsigned char device_id)
2247 struct prod_dev_id id = {
2248 .product_id = product_id,
2249 .device_id = device_id,
2253 dev = driver_find_device(drv, NULL, &id, __find_bmc_prod_dev_id);
2255 return to_bmc_device(dev);
2260 static ssize_t device_id_show(struct device *dev,
2261 struct device_attribute *attr,
2264 struct bmc_device *bmc = to_bmc_device(dev);
2266 return snprintf(buf, 10, "%u\n", bmc->id.device_id);
2268 static DEVICE_ATTR(device_id, S_IRUGO, device_id_show, NULL);
2270 static ssize_t provides_device_sdrs_show(struct device *dev,
2271 struct device_attribute *attr,
2274 struct bmc_device *bmc = to_bmc_device(dev);
2276 return snprintf(buf, 10, "%u\n",
2277 (bmc->id.device_revision & 0x80) >> 7);
2279 static DEVICE_ATTR(provides_device_sdrs, S_IRUGO, provides_device_sdrs_show,
2282 static ssize_t revision_show(struct device *dev, struct device_attribute *attr,
2285 struct bmc_device *bmc = to_bmc_device(dev);
2287 return snprintf(buf, 20, "%u\n",
2288 bmc->id.device_revision & 0x0F);
2290 static DEVICE_ATTR(revision, S_IRUGO, revision_show, NULL);
2292 static ssize_t firmware_revision_show(struct device *dev,
2293 struct device_attribute *attr,
2296 struct bmc_device *bmc = to_bmc_device(dev);
2298 return snprintf(buf, 20, "%u.%x\n", bmc->id.firmware_revision_1,
2299 bmc->id.firmware_revision_2);
2301 static DEVICE_ATTR(firmware_revision, S_IRUGO, firmware_revision_show, NULL);
2303 static ssize_t ipmi_version_show(struct device *dev,
2304 struct device_attribute *attr,
2307 struct bmc_device *bmc = to_bmc_device(dev);
2309 return snprintf(buf, 20, "%u.%u\n",
2310 ipmi_version_major(&bmc->id),
2311 ipmi_version_minor(&bmc->id));
2313 static DEVICE_ATTR(ipmi_version, S_IRUGO, ipmi_version_show, NULL);
2315 static ssize_t add_dev_support_show(struct device *dev,
2316 struct device_attribute *attr,
2319 struct bmc_device *bmc = to_bmc_device(dev);
2321 return snprintf(buf, 10, "0x%02x\n",
2322 bmc->id.additional_device_support);
2324 static DEVICE_ATTR(additional_device_support, S_IRUGO, add_dev_support_show,
2327 static ssize_t manufacturer_id_show(struct device *dev,
2328 struct device_attribute *attr,
2331 struct bmc_device *bmc = to_bmc_device(dev);
2333 return snprintf(buf, 20, "0x%6.6x\n", bmc->id.manufacturer_id);
2335 static DEVICE_ATTR(manufacturer_id, S_IRUGO, manufacturer_id_show, NULL);
2337 static ssize_t product_id_show(struct device *dev,
2338 struct device_attribute *attr,
2341 struct bmc_device *bmc = to_bmc_device(dev);
2343 return snprintf(buf, 10, "0x%4.4x\n", bmc->id.product_id);
2345 static DEVICE_ATTR(product_id, S_IRUGO, product_id_show, NULL);
2347 static ssize_t aux_firmware_rev_show(struct device *dev,
2348 struct device_attribute *attr,
2351 struct bmc_device *bmc = to_bmc_device(dev);
2353 return snprintf(buf, 21, "0x%02x 0x%02x 0x%02x 0x%02x\n",
2354 bmc->id.aux_firmware_revision[3],
2355 bmc->id.aux_firmware_revision[2],
2356 bmc->id.aux_firmware_revision[1],
2357 bmc->id.aux_firmware_revision[0]);
2359 static DEVICE_ATTR(aux_firmware_revision, S_IRUGO, aux_firmware_rev_show, NULL);
2361 static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
2364 struct bmc_device *bmc = to_bmc_device(dev);
2366 return snprintf(buf, 100, "%Lx%Lx\n",
2367 (long long) bmc->guid[0],
2368 (long long) bmc->guid[8]);
2370 static DEVICE_ATTR(guid, S_IRUGO, guid_show, NULL);
2372 static struct attribute *bmc_dev_attrs[] = {
2373 &dev_attr_device_id.attr,
2374 &dev_attr_provides_device_sdrs.attr,
2375 &dev_attr_revision.attr,
2376 &dev_attr_firmware_revision.attr,
2377 &dev_attr_ipmi_version.attr,
2378 &dev_attr_additional_device_support.attr,
2379 &dev_attr_manufacturer_id.attr,
2380 &dev_attr_product_id.attr,
2381 &dev_attr_aux_firmware_revision.attr,
2382 &dev_attr_guid.attr,
2386 static umode_t bmc_dev_attr_is_visible(struct kobject *kobj,
2387 struct attribute *attr, int idx)
2389 struct device *dev = kobj_to_dev(kobj);
2390 struct bmc_device *bmc = to_bmc_device(dev);
2391 umode_t mode = attr->mode;
2393 if (attr == &dev_attr_aux_firmware_revision.attr)
2394 return bmc->id.aux_firmware_revision_set ? mode : 0;
2395 if (attr == &dev_attr_guid.attr)
2396 return bmc->guid_set ? mode : 0;
2400 static const struct attribute_group bmc_dev_attr_group = {
2401 .attrs = bmc_dev_attrs,
2402 .is_visible = bmc_dev_attr_is_visible,
2405 static const struct attribute_group *bmc_dev_attr_groups[] = {
2406 &bmc_dev_attr_group,
2410 static const struct device_type bmc_device_type = {
2411 .groups = bmc_dev_attr_groups,
2415 release_bmc_device(struct device *dev)
2417 kfree(to_bmc_device(dev));
2421 cleanup_bmc_device(struct kref *ref)
2423 struct bmc_device *bmc = container_of(ref, struct bmc_device, usecount);
2425 platform_device_unregister(&bmc->pdev);
2428 static void ipmi_bmc_unregister(ipmi_smi_t intf)
2430 struct bmc_device *bmc = intf->bmc;
2432 sysfs_remove_link(&intf->si_dev->kobj, "bmc");
2433 if (intf->my_dev_name) {
2434 sysfs_remove_link(&bmc->pdev.dev.kobj, intf->my_dev_name);
2435 kfree(intf->my_dev_name);
2436 intf->my_dev_name = NULL;
2439 mutex_lock(&ipmidriver_mutex);
2440 kref_put(&bmc->usecount, cleanup_bmc_device);
2442 mutex_unlock(&ipmidriver_mutex);
2445 static int ipmi_bmc_register(ipmi_smi_t intf, int ifnum)
2448 struct bmc_device *bmc = intf->bmc;
2449 struct bmc_device *old_bmc;
2451 mutex_lock(&ipmidriver_mutex);
2454 * Try to find if there is an bmc_device struct
2455 * representing the interfaced BMC already
2458 old_bmc = ipmi_find_bmc_guid(&ipmidriver.driver, bmc->guid);
2460 old_bmc = ipmi_find_bmc_prod_dev_id(&ipmidriver.driver,
2465 * If there is already an bmc_device, free the new one,
2466 * otherwise register the new BMC device
2470 intf->bmc = old_bmc;
2473 kref_get(&bmc->usecount);
2474 mutex_unlock(&ipmidriver_mutex);
2477 "ipmi: interfacing existing BMC (man_id: 0x%6.6x,"
2478 " prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
2479 bmc->id.manufacturer_id,
2483 unsigned char orig_dev_id = bmc->id.device_id;
2484 int warn_printed = 0;
2486 snprintf(bmc->name, sizeof(bmc->name),
2487 "ipmi_bmc.%4.4x", bmc->id.product_id);
2488 bmc->pdev.name = bmc->name;
2490 while (ipmi_find_bmc_prod_dev_id(&ipmidriver.driver,
2492 bmc->id.device_id)) {
2493 if (!warn_printed) {
2494 printk(KERN_WARNING PFX
2495 "This machine has two different BMCs"
2496 " with the same product id and device"
2497 " id. This is an error in the"
2498 " firmware, but incrementing the"
2499 " device id to work around the problem."
2500 " Prod ID = 0x%x, Dev ID = 0x%x\n",
2501 bmc->id.product_id, bmc->id.device_id);
2504 bmc->id.device_id++; /* Wraps at 255 */
2505 if (bmc->id.device_id == orig_dev_id) {
2507 "Out of device ids!\n");
2512 bmc->pdev.dev.driver = &ipmidriver.driver;
2513 bmc->pdev.id = bmc->id.device_id;
2514 bmc->pdev.dev.release = release_bmc_device;
2515 bmc->pdev.dev.type = &bmc_device_type;
2516 kref_init(&bmc->usecount);
2518 rv = platform_device_register(&bmc->pdev);
2519 mutex_unlock(&ipmidriver_mutex);
2521 put_device(&bmc->pdev.dev);
2524 " Unable to register bmc device: %d\n",
2527 * Don't go to out_err, you can only do that if
2528 * the device is registered already.
2533 dev_info(intf->si_dev, "Found new BMC (man_id: 0x%6.6x, "
2534 "prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
2535 bmc->id.manufacturer_id,
2541 * create symlink from system interface device to bmc device
2544 rv = sysfs_create_link(&intf->si_dev->kobj, &bmc->pdev.dev.kobj, "bmc");
2547 "ipmi_msghandler: Unable to create bmc symlink: %d\n",
2552 intf->my_dev_name = kasprintf(GFP_KERNEL, "ipmi%d", ifnum);
2553 if (!intf->my_dev_name) {
2556 "ipmi_msghandler: allocate link from BMC: %d\n",
2561 rv = sysfs_create_link(&bmc->pdev.dev.kobj, &intf->si_dev->kobj,
2564 kfree(intf->my_dev_name);
2565 intf->my_dev_name = NULL;
2568 " Unable to create symlink to bmc: %d\n",
2576 ipmi_bmc_unregister(intf);
2581 send_guid_cmd(ipmi_smi_t intf, int chan)
2583 struct kernel_ipmi_msg msg;
2584 struct ipmi_system_interface_addr si;
2586 si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2587 si.channel = IPMI_BMC_CHANNEL;
2590 msg.netfn = IPMI_NETFN_APP_REQUEST;
2591 msg.cmd = IPMI_GET_DEVICE_GUID_CMD;
2594 return i_ipmi_request(NULL,
2596 (struct ipmi_addr *) &si,
2603 intf->channels[0].address,
2604 intf->channels[0].lun,
2609 guid_handler(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
2611 if ((msg->addr.addr_type != IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
2612 || (msg->msg.netfn != IPMI_NETFN_APP_RESPONSE)
2613 || (msg->msg.cmd != IPMI_GET_DEVICE_GUID_CMD))
2617 if (msg->msg.data[0] != 0) {
2618 /* Error from getting the GUID, the BMC doesn't have one. */
2619 intf->bmc->guid_set = 0;
2623 if (msg->msg.data_len < 17) {
2624 intf->bmc->guid_set = 0;
2625 printk(KERN_WARNING PFX
2626 "guid_handler: The GUID response from the BMC was too"
2627 " short, it was %d but should have been 17. Assuming"
2628 " GUID is not available.\n",
2633 memcpy(intf->bmc->guid, msg->msg.data, 16);
2634 intf->bmc->guid_set = 1;
2636 wake_up(&intf->waitq);
2640 get_guid(ipmi_smi_t intf)
2644 intf->bmc->guid_set = 0x2;
2645 intf->null_user_handler = guid_handler;
2646 rv = send_guid_cmd(intf, 0);
2648 /* Send failed, no GUID available. */
2649 intf->bmc->guid_set = 0;
2651 wait_event(intf->waitq, intf->bmc->guid_set != 2);
2653 intf->null_user_handler = NULL;
2657 send_channel_info_cmd(ipmi_smi_t intf, int chan)
2659 struct kernel_ipmi_msg msg;
2660 unsigned char data[1];
2661 struct ipmi_system_interface_addr si;
2663 si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2664 si.channel = IPMI_BMC_CHANNEL;
2667 msg.netfn = IPMI_NETFN_APP_REQUEST;
2668 msg.cmd = IPMI_GET_CHANNEL_INFO_CMD;
2672 return i_ipmi_request(NULL,
2674 (struct ipmi_addr *) &si,
2681 intf->channels[0].address,
2682 intf->channels[0].lun,
2687 channel_handler(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
2692 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
2693 && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
2694 && (msg->msg.cmd == IPMI_GET_CHANNEL_INFO_CMD)) {
2695 /* It's the one we want */
2696 if (msg->msg.data[0] != 0) {
2697 /* Got an error from the channel, just go on. */
2699 if (msg->msg.data[0] == IPMI_INVALID_COMMAND_ERR) {
2701 * If the MC does not support this
2702 * command, that is legal. We just
2703 * assume it has one IPMB at channel
2706 intf->channels[0].medium
2707 = IPMI_CHANNEL_MEDIUM_IPMB;
2708 intf->channels[0].protocol
2709 = IPMI_CHANNEL_PROTOCOL_IPMB;
2711 intf->curr_channel = IPMI_MAX_CHANNELS;
2712 wake_up(&intf->waitq);
2717 if (msg->msg.data_len < 4) {
2718 /* Message not big enough, just go on. */
2721 chan = intf->curr_channel;
2722 intf->channels[chan].medium = msg->msg.data[2] & 0x7f;
2723 intf->channels[chan].protocol = msg->msg.data[3] & 0x1f;
2726 intf->curr_channel++;
2727 if (intf->curr_channel >= IPMI_MAX_CHANNELS)
2728 wake_up(&intf->waitq);
2730 rv = send_channel_info_cmd(intf, intf->curr_channel);
2733 /* Got an error somehow, just give up. */
2734 printk(KERN_WARNING PFX
2735 "Error sending channel information for channel"
2736 " %d: %d\n", intf->curr_channel, rv);
2738 intf->curr_channel = IPMI_MAX_CHANNELS;
2739 wake_up(&intf->waitq);
2746 static void ipmi_poll(ipmi_smi_t intf)
2748 if (intf->handlers->poll)
2749 intf->handlers->poll(intf->send_info);
2750 /* In case something came in */
2751 handle_new_recv_msgs(intf);
2754 void ipmi_poll_interface(ipmi_user_t user)
2756 ipmi_poll(user->intf);
2758 EXPORT_SYMBOL(ipmi_poll_interface);
2760 int ipmi_register_smi(const struct ipmi_smi_handlers *handlers,
2762 struct ipmi_device_id *device_id,
2763 struct device *si_dev,
2764 unsigned char slave_addr)
2770 struct list_head *link;
2773 * Make sure the driver is actually initialized, this handles
2774 * problems with initialization order.
2777 rv = ipmi_init_msghandler();
2781 * The init code doesn't return an error if it was turned
2782 * off, but it won't initialize. Check that.
2788 intf = kzalloc(sizeof(*intf), GFP_KERNEL);
2792 intf->ipmi_version_major = ipmi_version_major(device_id);
2793 intf->ipmi_version_minor = ipmi_version_minor(device_id);
2795 intf->bmc = kzalloc(sizeof(*intf->bmc), GFP_KERNEL);
2800 intf->intf_num = -1; /* Mark it invalid for now. */
2801 kref_init(&intf->refcount);
2802 intf->bmc->id = *device_id;
2803 intf->si_dev = si_dev;
2804 for (j = 0; j < IPMI_MAX_CHANNELS; j++) {
2805 intf->channels[j].address = IPMI_BMC_SLAVE_ADDR;
2806 intf->channels[j].lun = 2;
2808 if (slave_addr != 0)
2809 intf->channels[0].address = slave_addr;
2810 INIT_LIST_HEAD(&intf->users);
2811 intf->handlers = handlers;
2812 intf->send_info = send_info;
2813 spin_lock_init(&intf->seq_lock);
2814 for (j = 0; j < IPMI_IPMB_NUM_SEQ; j++) {
2815 intf->seq_table[j].inuse = 0;
2816 intf->seq_table[j].seqid = 0;
2819 #ifdef CONFIG_PROC_FS
2820 mutex_init(&intf->proc_entry_lock);
2822 spin_lock_init(&intf->waiting_rcv_msgs_lock);
2823 INIT_LIST_HEAD(&intf->waiting_rcv_msgs);
2824 tasklet_init(&intf->recv_tasklet,
2826 (unsigned long) intf);
2827 atomic_set(&intf->watchdog_pretimeouts_to_deliver, 0);
2828 spin_lock_init(&intf->xmit_msgs_lock);
2829 INIT_LIST_HEAD(&intf->xmit_msgs);
2830 INIT_LIST_HEAD(&intf->hp_xmit_msgs);
2831 spin_lock_init(&intf->events_lock);
2832 atomic_set(&intf->event_waiters, 0);
2833 intf->ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
2834 INIT_LIST_HEAD(&intf->waiting_events);
2835 intf->waiting_events_count = 0;
2836 mutex_init(&intf->cmd_rcvrs_mutex);
2837 spin_lock_init(&intf->maintenance_mode_lock);
2838 INIT_LIST_HEAD(&intf->cmd_rcvrs);
2839 init_waitqueue_head(&intf->waitq);
2840 for (i = 0; i < IPMI_NUM_STATS; i++)
2841 atomic_set(&intf->stats[i], 0);
2843 intf->proc_dir = NULL;
2845 mutex_lock(&smi_watchers_mutex);
2846 mutex_lock(&ipmi_interfaces_mutex);
2847 /* Look for a hole in the numbers. */
2849 link = &ipmi_interfaces;
2850 list_for_each_entry_rcu(tintf, &ipmi_interfaces, link) {
2851 if (tintf->intf_num != i) {
2852 link = &tintf->link;
2857 /* Add the new interface in numeric order. */
2859 list_add_rcu(&intf->link, &ipmi_interfaces);
2861 list_add_tail_rcu(&intf->link, link);
2863 rv = handlers->start_processing(send_info, intf);
2869 if ((intf->ipmi_version_major > 1)
2870 || ((intf->ipmi_version_major == 1)
2871 && (intf->ipmi_version_minor >= 5))) {
2873 * Start scanning the channels to see what is
2876 intf->null_user_handler = channel_handler;
2877 intf->curr_channel = 0;
2878 rv = send_channel_info_cmd(intf, 0);
2880 printk(KERN_WARNING PFX
2881 "Error sending channel information for channel"
2886 /* Wait for the channel info to be read. */
2887 wait_event(intf->waitq,
2888 intf->curr_channel >= IPMI_MAX_CHANNELS);
2889 intf->null_user_handler = NULL;
2891 /* Assume a single IPMB channel at zero. */
2892 intf->channels[0].medium = IPMI_CHANNEL_MEDIUM_IPMB;
2893 intf->channels[0].protocol = IPMI_CHANNEL_PROTOCOL_IPMB;
2894 intf->curr_channel = IPMI_MAX_CHANNELS;
2897 rv = ipmi_bmc_register(intf, i);
2900 rv = add_proc_entries(intf, i);
2905 remove_proc_entries(intf);
2906 intf->handlers = NULL;
2907 list_del_rcu(&intf->link);
2908 mutex_unlock(&ipmi_interfaces_mutex);
2909 mutex_unlock(&smi_watchers_mutex);
2911 kref_put(&intf->refcount, intf_free);
2914 * Keep memory order straight for RCU readers. Make
2915 * sure everything else is committed to memory before
2916 * setting intf_num to mark the interface valid.
2920 mutex_unlock(&ipmi_interfaces_mutex);
2921 /* After this point the interface is legal to use. */
2922 call_smi_watchers(i, intf->si_dev);
2923 mutex_unlock(&smi_watchers_mutex);
2928 EXPORT_SYMBOL(ipmi_register_smi);
2930 static void deliver_smi_err_response(ipmi_smi_t intf,
2931 struct ipmi_smi_msg *msg,
2935 msg->rsp[0] = msg->data[0] | 4;
2936 msg->rsp[1] = msg->data[1];
2940 /* This will never requeue, but it may ask us to free the message. */
2941 rv = handle_one_recv_msg(intf, msg);
2943 ipmi_free_smi_msg(msg);
2946 static void cleanup_smi_msgs(ipmi_smi_t intf)
2949 struct seq_table *ent;
2950 struct ipmi_smi_msg *msg;
2951 struct list_head *entry;
2952 struct list_head tmplist;
2954 /* Clear out our transmit queues and hold the messages. */
2955 INIT_LIST_HEAD(&tmplist);
2956 list_splice_tail(&intf->hp_xmit_msgs, &tmplist);
2957 list_splice_tail(&intf->xmit_msgs, &tmplist);
2959 /* Current message first, to preserve order */
2960 while (intf->curr_msg && !list_empty(&intf->waiting_rcv_msgs)) {
2961 /* Wait for the message to clear out. */
2962 schedule_timeout(1);
2965 /* No need for locks, the interface is down. */
2968 * Return errors for all pending messages in queue and in the
2969 * tables waiting for remote responses.
2971 while (!list_empty(&tmplist)) {
2972 entry = tmplist.next;
2974 msg = list_entry(entry, struct ipmi_smi_msg, link);
2975 deliver_smi_err_response(intf, msg, IPMI_ERR_UNSPECIFIED);
2978 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
2979 ent = &(intf->seq_table[i]);
2982 deliver_err_response(ent->recv_msg, IPMI_ERR_UNSPECIFIED);
2986 int ipmi_unregister_smi(ipmi_smi_t intf)
2988 struct ipmi_smi_watcher *w;
2989 int intf_num = intf->intf_num;
2992 mutex_lock(&smi_watchers_mutex);
2993 mutex_lock(&ipmi_interfaces_mutex);
2994 intf->intf_num = -1;
2995 intf->in_shutdown = true;
2996 list_del_rcu(&intf->link);
2997 mutex_unlock(&ipmi_interfaces_mutex);
3000 cleanup_smi_msgs(intf);
3002 /* Clean up the effects of users on the lower-level software. */
3003 mutex_lock(&ipmi_interfaces_mutex);
3005 list_for_each_entry_rcu(user, &intf->users, link) {
3006 module_put(intf->handlers->owner);
3007 if (intf->handlers->dec_usecount)
3008 intf->handlers->dec_usecount(intf->send_info);
3011 intf->handlers = NULL;
3012 mutex_unlock(&ipmi_interfaces_mutex);
3014 remove_proc_entries(intf);
3015 ipmi_bmc_unregister(intf);
3018 * Call all the watcher interfaces to tell them that
3019 * an interface is gone.
3021 list_for_each_entry(w, &smi_watchers, link)
3022 w->smi_gone(intf_num);
3023 mutex_unlock(&smi_watchers_mutex);
3025 kref_put(&intf->refcount, intf_free);
3028 EXPORT_SYMBOL(ipmi_unregister_smi);
3030 static int handle_ipmb_get_msg_rsp(ipmi_smi_t intf,
3031 struct ipmi_smi_msg *msg)
3033 struct ipmi_ipmb_addr ipmb_addr;
3034 struct ipmi_recv_msg *recv_msg;
3037 * This is 11, not 10, because the response must contain a
3040 if (msg->rsp_size < 11) {
3041 /* Message not big enough, just ignore it. */
3042 ipmi_inc_stat(intf, invalid_ipmb_responses);
3046 if (msg->rsp[2] != 0) {
3047 /* An error getting the response, just ignore it. */
3051 ipmb_addr.addr_type = IPMI_IPMB_ADDR_TYPE;
3052 ipmb_addr.slave_addr = msg->rsp[6];
3053 ipmb_addr.channel = msg->rsp[3] & 0x0f;
3054 ipmb_addr.lun = msg->rsp[7] & 3;
3057 * It's a response from a remote entity. Look up the sequence
3058 * number and handle the response.
3060 if (intf_find_seq(intf,
3064 (msg->rsp[4] >> 2) & (~1),
3065 (struct ipmi_addr *) &(ipmb_addr),
3068 * We were unable to find the sequence number,
3069 * so just nuke the message.
3071 ipmi_inc_stat(intf, unhandled_ipmb_responses);
3075 memcpy(recv_msg->msg_data,
3079 * The other fields matched, so no need to set them, except
3080 * for netfn, which needs to be the response that was
3081 * returned, not the request value.
3083 recv_msg->msg.netfn = msg->rsp[4] >> 2;
3084 recv_msg->msg.data = recv_msg->msg_data;
3085 recv_msg->msg.data_len = msg->rsp_size - 10;
3086 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
3087 ipmi_inc_stat(intf, handled_ipmb_responses);
3088 deliver_response(recv_msg);
3093 static int handle_ipmb_get_msg_cmd(ipmi_smi_t intf,
3094 struct ipmi_smi_msg *msg)
3096 struct cmd_rcvr *rcvr;
3098 unsigned char netfn;
3101 ipmi_user_t user = NULL;
3102 struct ipmi_ipmb_addr *ipmb_addr;
3103 struct ipmi_recv_msg *recv_msg;
3105 if (msg->rsp_size < 10) {
3106 /* Message not big enough, just ignore it. */
3107 ipmi_inc_stat(intf, invalid_commands);
3111 if (msg->rsp[2] != 0) {
3112 /* An error getting the response, just ignore it. */
3116 netfn = msg->rsp[4] >> 2;
3118 chan = msg->rsp[3] & 0xf;
3121 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
3124 kref_get(&user->refcount);
3130 /* We didn't find a user, deliver an error response. */
3131 ipmi_inc_stat(intf, unhandled_commands);
3133 msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
3134 msg->data[1] = IPMI_SEND_MSG_CMD;
3135 msg->data[2] = msg->rsp[3];
3136 msg->data[3] = msg->rsp[6];
3137 msg->data[4] = ((netfn + 1) << 2) | (msg->rsp[7] & 0x3);
3138 msg->data[5] = ipmb_checksum(&(msg->data[3]), 2);
3139 msg->data[6] = intf->channels[msg->rsp[3] & 0xf].address;
3141 msg->data[7] = (msg->rsp[7] & 0xfc) | (msg->rsp[4] & 0x3);
3142 msg->data[8] = msg->rsp[8]; /* cmd */
3143 msg->data[9] = IPMI_INVALID_CMD_COMPLETION_CODE;
3144 msg->data[10] = ipmb_checksum(&(msg->data[6]), 4);
3145 msg->data_size = 11;
3150 printk("Invalid command:");
3151 for (m = 0; m < msg->data_size; m++)
3152 printk(" %2.2x", msg->data[m]);
3157 if (!intf->in_shutdown) {
3158 smi_send(intf, intf->handlers, msg, 0);
3160 * We used the message, so return the value
3161 * that causes it to not be freed or
3168 /* Deliver the message to the user. */
3169 ipmi_inc_stat(intf, handled_commands);
3171 recv_msg = ipmi_alloc_recv_msg();
3174 * We couldn't allocate memory for the
3175 * message, so requeue it for handling
3179 kref_put(&user->refcount, free_user);
3181 /* Extract the source address from the data. */
3182 ipmb_addr = (struct ipmi_ipmb_addr *) &recv_msg->addr;
3183 ipmb_addr->addr_type = IPMI_IPMB_ADDR_TYPE;
3184 ipmb_addr->slave_addr = msg->rsp[6];
3185 ipmb_addr->lun = msg->rsp[7] & 3;
3186 ipmb_addr->channel = msg->rsp[3] & 0xf;
3189 * Extract the rest of the message information
3190 * from the IPMB header.
3192 recv_msg->user = user;
3193 recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
3194 recv_msg->msgid = msg->rsp[7] >> 2;
3195 recv_msg->msg.netfn = msg->rsp[4] >> 2;
3196 recv_msg->msg.cmd = msg->rsp[8];
3197 recv_msg->msg.data = recv_msg->msg_data;
3200 * We chop off 10, not 9 bytes because the checksum
3201 * at the end also needs to be removed.
3203 recv_msg->msg.data_len = msg->rsp_size - 10;
3204 memcpy(recv_msg->msg_data,
3206 msg->rsp_size - 10);
3207 deliver_response(recv_msg);
3214 static int handle_lan_get_msg_rsp(ipmi_smi_t intf,
3215 struct ipmi_smi_msg *msg)
3217 struct ipmi_lan_addr lan_addr;
3218 struct ipmi_recv_msg *recv_msg;
3222 * This is 13, not 12, because the response must contain a
3225 if (msg->rsp_size < 13) {
3226 /* Message not big enough, just ignore it. */
3227 ipmi_inc_stat(intf, invalid_lan_responses);
3231 if (msg->rsp[2] != 0) {
3232 /* An error getting the response, just ignore it. */
3236 lan_addr.addr_type = IPMI_LAN_ADDR_TYPE;
3237 lan_addr.session_handle = msg->rsp[4];
3238 lan_addr.remote_SWID = msg->rsp[8];
3239 lan_addr.local_SWID = msg->rsp[5];
3240 lan_addr.channel = msg->rsp[3] & 0x0f;
3241 lan_addr.privilege = msg->rsp[3] >> 4;
3242 lan_addr.lun = msg->rsp[9] & 3;
3245 * It's a response from a remote entity. Look up the sequence
3246 * number and handle the response.
3248 if (intf_find_seq(intf,
3252 (msg->rsp[6] >> 2) & (~1),
3253 (struct ipmi_addr *) &(lan_addr),
3256 * We were unable to find the sequence number,
3257 * so just nuke the message.
3259 ipmi_inc_stat(intf, unhandled_lan_responses);
3263 memcpy(recv_msg->msg_data,
3265 msg->rsp_size - 11);
3267 * The other fields matched, so no need to set them, except
3268 * for netfn, which needs to be the response that was
3269 * returned, not the request value.
3271 recv_msg->msg.netfn = msg->rsp[6] >> 2;
3272 recv_msg->msg.data = recv_msg->msg_data;
3273 recv_msg->msg.data_len = msg->rsp_size - 12;
3274 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
3275 ipmi_inc_stat(intf, handled_lan_responses);
3276 deliver_response(recv_msg);
3281 static int handle_lan_get_msg_cmd(ipmi_smi_t intf,
3282 struct ipmi_smi_msg *msg)
3284 struct cmd_rcvr *rcvr;
3286 unsigned char netfn;
3289 ipmi_user_t user = NULL;
3290 struct ipmi_lan_addr *lan_addr;
3291 struct ipmi_recv_msg *recv_msg;
3293 if (msg->rsp_size < 12) {
3294 /* Message not big enough, just ignore it. */
3295 ipmi_inc_stat(intf, invalid_commands);
3299 if (msg->rsp[2] != 0) {
3300 /* An error getting the response, just ignore it. */
3304 netfn = msg->rsp[6] >> 2;
3306 chan = msg->rsp[3] & 0xf;
3309 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
3312 kref_get(&user->refcount);
3318 /* We didn't find a user, just give up. */
3319 ipmi_inc_stat(intf, unhandled_commands);
3322 * Don't do anything with these messages, just allow
3327 /* Deliver the message to the user. */
3328 ipmi_inc_stat(intf, handled_commands);
3330 recv_msg = ipmi_alloc_recv_msg();
3333 * We couldn't allocate memory for the
3334 * message, so requeue it for handling later.
3337 kref_put(&user->refcount, free_user);
3339 /* Extract the source address from the data. */
3340 lan_addr = (struct ipmi_lan_addr *) &recv_msg->addr;
3341 lan_addr->addr_type = IPMI_LAN_ADDR_TYPE;
3342 lan_addr->session_handle = msg->rsp[4];
3343 lan_addr->remote_SWID = msg->rsp[8];
3344 lan_addr->local_SWID = msg->rsp[5];
3345 lan_addr->lun = msg->rsp[9] & 3;
3346 lan_addr->channel = msg->rsp[3] & 0xf;
3347 lan_addr->privilege = msg->rsp[3] >> 4;
3350 * Extract the rest of the message information
3351 * from the IPMB header.
3353 recv_msg->user = user;
3354 recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
3355 recv_msg->msgid = msg->rsp[9] >> 2;
3356 recv_msg->msg.netfn = msg->rsp[6] >> 2;
3357 recv_msg->msg.cmd = msg->rsp[10];
3358 recv_msg->msg.data = recv_msg->msg_data;
3361 * We chop off 12, not 11 bytes because the checksum
3362 * at the end also needs to be removed.
3364 recv_msg->msg.data_len = msg->rsp_size - 12;
3365 memcpy(recv_msg->msg_data,
3367 msg->rsp_size - 12);
3368 deliver_response(recv_msg);
3376 * This routine will handle "Get Message" command responses with
3377 * channels that use an OEM Medium. The message format belongs to
3378 * the OEM. See IPMI 2.0 specification, Chapter 6 and
3379 * Chapter 22, sections 22.6 and 22.24 for more details.
3381 static int handle_oem_get_msg_cmd(ipmi_smi_t intf,
3382 struct ipmi_smi_msg *msg)
3384 struct cmd_rcvr *rcvr;
3386 unsigned char netfn;
3389 ipmi_user_t user = NULL;
3390 struct ipmi_system_interface_addr *smi_addr;
3391 struct ipmi_recv_msg *recv_msg;
3394 * We expect the OEM SW to perform error checking
3395 * so we just do some basic sanity checks
3397 if (msg->rsp_size < 4) {
3398 /* Message not big enough, just ignore it. */
3399 ipmi_inc_stat(intf, invalid_commands);
3403 if (msg->rsp[2] != 0) {
3404 /* An error getting the response, just ignore it. */
3409 * This is an OEM Message so the OEM needs to know how
3410 * handle the message. We do no interpretation.
3412 netfn = msg->rsp[0] >> 2;
3414 chan = msg->rsp[3] & 0xf;
3417 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
3420 kref_get(&user->refcount);
3426 /* We didn't find a user, just give up. */
3427 ipmi_inc_stat(intf, unhandled_commands);
3430 * Don't do anything with these messages, just allow
3436 /* Deliver the message to the user. */
3437 ipmi_inc_stat(intf, handled_commands);
3439 recv_msg = ipmi_alloc_recv_msg();
3442 * We couldn't allocate memory for the
3443 * message, so requeue it for handling
3447 kref_put(&user->refcount, free_user);
3450 * OEM Messages are expected to be delivered via
3451 * the system interface to SMS software. We might
3452 * need to visit this again depending on OEM
3455 smi_addr = ((struct ipmi_system_interface_addr *)
3457 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3458 smi_addr->channel = IPMI_BMC_CHANNEL;
3459 smi_addr->lun = msg->rsp[0] & 3;
3461 recv_msg->user = user;
3462 recv_msg->user_msg_data = NULL;
3463 recv_msg->recv_type = IPMI_OEM_RECV_TYPE;
3464 recv_msg->msg.netfn = msg->rsp[0] >> 2;
3465 recv_msg->msg.cmd = msg->rsp[1];
3466 recv_msg->msg.data = recv_msg->msg_data;
3469 * The message starts at byte 4 which follows the
3470 * the Channel Byte in the "GET MESSAGE" command
3472 recv_msg->msg.data_len = msg->rsp_size - 4;
3473 memcpy(recv_msg->msg_data,
3476 deliver_response(recv_msg);
3483 static void copy_event_into_recv_msg(struct ipmi_recv_msg *recv_msg,
3484 struct ipmi_smi_msg *msg)
3486 struct ipmi_system_interface_addr *smi_addr;
3488 recv_msg->msgid = 0;
3489 smi_addr = (struct ipmi_system_interface_addr *) &(recv_msg->addr);
3490 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3491 smi_addr->channel = IPMI_BMC_CHANNEL;
3492 smi_addr->lun = msg->rsp[0] & 3;
3493 recv_msg->recv_type = IPMI_ASYNC_EVENT_RECV_TYPE;
3494 recv_msg->msg.netfn = msg->rsp[0] >> 2;
3495 recv_msg->msg.cmd = msg->rsp[1];
3496 memcpy(recv_msg->msg_data, &(msg->rsp[3]), msg->rsp_size - 3);
3497 recv_msg->msg.data = recv_msg->msg_data;
3498 recv_msg->msg.data_len = msg->rsp_size - 3;
3501 static int handle_read_event_rsp(ipmi_smi_t intf,
3502 struct ipmi_smi_msg *msg)
3504 struct ipmi_recv_msg *recv_msg, *recv_msg2;
3505 struct list_head msgs;
3508 int deliver_count = 0;
3509 unsigned long flags;
3511 if (msg->rsp_size < 19) {
3512 /* Message is too small to be an IPMB event. */
3513 ipmi_inc_stat(intf, invalid_events);
3517 if (msg->rsp[2] != 0) {
3518 /* An error getting the event, just ignore it. */
3522 INIT_LIST_HEAD(&msgs);
3524 spin_lock_irqsave(&intf->events_lock, flags);
3526 ipmi_inc_stat(intf, events);
3529 * Allocate and fill in one message for every user that is
3533 list_for_each_entry_rcu(user, &intf->users, link) {
3534 if (!user->gets_events)
3537 recv_msg = ipmi_alloc_recv_msg();
3540 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs,
3542 list_del(&recv_msg->link);
3543 ipmi_free_recv_msg(recv_msg);
3546 * We couldn't allocate memory for the
3547 * message, so requeue it for handling
3556 copy_event_into_recv_msg(recv_msg, msg);
3557 recv_msg->user = user;
3558 kref_get(&user->refcount);
3559 list_add_tail(&(recv_msg->link), &msgs);
3563 if (deliver_count) {
3564 /* Now deliver all the messages. */
3565 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs, link) {
3566 list_del(&recv_msg->link);
3567 deliver_response(recv_msg);
3569 } else if (intf->waiting_events_count < MAX_EVENTS_IN_QUEUE) {
3571 * No one to receive the message, put it in queue if there's
3572 * not already too many things in the queue.
3574 recv_msg = ipmi_alloc_recv_msg();
3577 * We couldn't allocate memory for the
3578 * message, so requeue it for handling
3585 copy_event_into_recv_msg(recv_msg, msg);
3586 list_add_tail(&(recv_msg->link), &(intf->waiting_events));
3587 intf->waiting_events_count++;
3588 } else if (!intf->event_msg_printed) {
3590 * There's too many things in the queue, discard this
3593 printk(KERN_WARNING PFX "Event queue full, discarding"
3594 " incoming events\n");
3595 intf->event_msg_printed = 1;
3599 spin_unlock_irqrestore(&(intf->events_lock), flags);
3604 static int handle_bmc_rsp(ipmi_smi_t intf,
3605 struct ipmi_smi_msg *msg)
3607 struct ipmi_recv_msg *recv_msg;
3608 struct ipmi_user *user;
3610 recv_msg = (struct ipmi_recv_msg *) msg->user_data;
3611 if (recv_msg == NULL) {
3613 "IPMI message received with no owner. This\n"
3614 "could be because of a malformed message, or\n"
3615 "because of a hardware error. Contact your\n"
3616 "hardware vender for assistance\n");
3620 user = recv_msg->user;
3621 /* Make sure the user still exists. */
3622 if (user && !user->valid) {
3623 /* The user for the message went away, so give up. */
3624 ipmi_inc_stat(intf, unhandled_local_responses);
3625 ipmi_free_recv_msg(recv_msg);
3627 struct ipmi_system_interface_addr *smi_addr;
3629 ipmi_inc_stat(intf, handled_local_responses);
3630 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
3631 recv_msg->msgid = msg->msgid;
3632 smi_addr = ((struct ipmi_system_interface_addr *)
3634 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3635 smi_addr->channel = IPMI_BMC_CHANNEL;
3636 smi_addr->lun = msg->rsp[0] & 3;
3637 recv_msg->msg.netfn = msg->rsp[0] >> 2;
3638 recv_msg->msg.cmd = msg->rsp[1];
3639 memcpy(recv_msg->msg_data,
3642 recv_msg->msg.data = recv_msg->msg_data;
3643 recv_msg->msg.data_len = msg->rsp_size - 2;
3644 deliver_response(recv_msg);
3651 * Handle a received message. Return 1 if the message should be requeued,
3652 * 0 if the message should be freed, or -1 if the message should not
3653 * be freed or requeued.
3655 static int handle_one_recv_msg(ipmi_smi_t intf,
3656 struct ipmi_smi_msg *msg)
3664 for (m = 0; m < msg->rsp_size; m++)
3665 printk(" %2.2x", msg->rsp[m]);
3668 if (msg->rsp_size < 2) {
3669 /* Message is too small to be correct. */
3670 printk(KERN_WARNING PFX "BMC returned to small a message"
3671 " for netfn %x cmd %x, got %d bytes\n",
3672 (msg->data[0] >> 2) | 1, msg->data[1], msg->rsp_size);
3674 /* Generate an error response for the message. */
3675 msg->rsp[0] = msg->data[0] | (1 << 2);
3676 msg->rsp[1] = msg->data[1];
3677 msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
3679 } else if (((msg->rsp[0] >> 2) != ((msg->data[0] >> 2) | 1))
3680 || (msg->rsp[1] != msg->data[1])) {
3682 * The NetFN and Command in the response is not even
3683 * marginally correct.
3685 printk(KERN_WARNING PFX "BMC returned incorrect response,"
3686 " expected netfn %x cmd %x, got netfn %x cmd %x\n",
3687 (msg->data[0] >> 2) | 1, msg->data[1],
3688 msg->rsp[0] >> 2, msg->rsp[1]);
3690 /* Generate an error response for the message. */
3691 msg->rsp[0] = msg->data[0] | (1 << 2);
3692 msg->rsp[1] = msg->data[1];
3693 msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
3697 if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
3698 && (msg->rsp[1] == IPMI_SEND_MSG_CMD)
3699 && (msg->user_data != NULL)) {
3701 * It's a response to a response we sent. For this we
3702 * deliver a send message response to the user.
3704 struct ipmi_recv_msg *recv_msg = msg->user_data;
3707 if (msg->rsp_size < 2)
3708 /* Message is too small to be correct. */
3711 chan = msg->data[2] & 0x0f;
3712 if (chan >= IPMI_MAX_CHANNELS)
3713 /* Invalid channel number */
3719 /* Make sure the user still exists. */
3720 if (!recv_msg->user || !recv_msg->user->valid)
3723 recv_msg->recv_type = IPMI_RESPONSE_RESPONSE_TYPE;
3724 recv_msg->msg.data = recv_msg->msg_data;
3725 recv_msg->msg.data_len = 1;
3726 recv_msg->msg_data[0] = msg->rsp[2];
3727 deliver_response(recv_msg);
3728 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
3729 && (msg->rsp[1] == IPMI_GET_MSG_CMD)) {
3730 /* It's from the receive queue. */
3731 chan = msg->rsp[3] & 0xf;
3732 if (chan >= IPMI_MAX_CHANNELS) {
3733 /* Invalid channel number */
3739 * We need to make sure the channels have been initialized.
3740 * The channel_handler routine will set the "curr_channel"
3741 * equal to or greater than IPMI_MAX_CHANNELS when all the
3742 * channels for this interface have been initialized.
3744 if (intf->curr_channel < IPMI_MAX_CHANNELS) {
3745 requeue = 0; /* Throw the message away */
3749 switch (intf->channels[chan].medium) {
3750 case IPMI_CHANNEL_MEDIUM_IPMB:
3751 if (msg->rsp[4] & 0x04) {
3753 * It's a response, so find the
3754 * requesting message and send it up.
3756 requeue = handle_ipmb_get_msg_rsp(intf, msg);
3759 * It's a command to the SMS from some other
3760 * entity. Handle that.
3762 requeue = handle_ipmb_get_msg_cmd(intf, msg);
3766 case IPMI_CHANNEL_MEDIUM_8023LAN:
3767 case IPMI_CHANNEL_MEDIUM_ASYNC:
3768 if (msg->rsp[6] & 0x04) {
3770 * It's a response, so find the
3771 * requesting message and send it up.
3773 requeue = handle_lan_get_msg_rsp(intf, msg);
3776 * It's a command to the SMS from some other
3777 * entity. Handle that.
3779 requeue = handle_lan_get_msg_cmd(intf, msg);
3784 /* Check for OEM Channels. Clients had better
3785 register for these commands. */
3786 if ((intf->channels[chan].medium
3787 >= IPMI_CHANNEL_MEDIUM_OEM_MIN)
3788 && (intf->channels[chan].medium
3789 <= IPMI_CHANNEL_MEDIUM_OEM_MAX)) {
3790 requeue = handle_oem_get_msg_cmd(intf, msg);
3793 * We don't handle the channel type, so just
3800 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
3801 && (msg->rsp[1] == IPMI_READ_EVENT_MSG_BUFFER_CMD)) {
3802 /* It's an asynchronous event. */
3803 requeue = handle_read_event_rsp(intf, msg);
3805 /* It's a response from the local BMC. */
3806 requeue = handle_bmc_rsp(intf, msg);
3814 * If there are messages in the queue or pretimeouts, handle them.
3816 static void handle_new_recv_msgs(ipmi_smi_t intf)
3818 struct ipmi_smi_msg *smi_msg;
3819 unsigned long flags = 0;
3821 int run_to_completion = intf->run_to_completion;
3823 /* See if any waiting messages need to be processed. */
3824 if (!run_to_completion)
3825 spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags);
3826 while (!list_empty(&intf->waiting_rcv_msgs)) {
3827 smi_msg = list_entry(intf->waiting_rcv_msgs.next,
3828 struct ipmi_smi_msg, link);
3829 list_del(&smi_msg->link);
3830 if (!run_to_completion)
3831 spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock,
3833 rv = handle_one_recv_msg(intf, smi_msg);
3834 if (!run_to_completion)
3835 spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags);
3838 * To preserve message order, quit if we
3839 * can't handle a message. Add the message
3840 * back at the head, this is safe because this
3841 * tasklet is the only thing that pulls the
3844 list_add(&smi_msg->link, &intf->waiting_rcv_msgs);
3848 /* Message handled */
3849 ipmi_free_smi_msg(smi_msg);
3850 /* If rv < 0, fatal error, del but don't free. */
3853 if (!run_to_completion)
3854 spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock, flags);
3857 * If the pretimout count is non-zero, decrement one from it and
3858 * deliver pretimeouts to all the users.
3860 if (atomic_add_unless(&intf->watchdog_pretimeouts_to_deliver, -1, 0)) {
3864 list_for_each_entry_rcu(user, &intf->users, link) {
3865 if (user->handler->ipmi_watchdog_pretimeout)
3866 user->handler->ipmi_watchdog_pretimeout(
3867 user->handler_data);
3873 static void smi_recv_tasklet(unsigned long val)
3875 unsigned long flags = 0; /* keep us warning-free. */
3876 ipmi_smi_t intf = (ipmi_smi_t) val;
3877 int run_to_completion = intf->run_to_completion;
3878 struct ipmi_smi_msg *newmsg = NULL;
3881 * Start the next message if available.
3883 * Do this here, not in the actual receiver, because we may deadlock
3884 * because the lower layer is allowed to hold locks while calling
3890 if (!run_to_completion)
3891 spin_lock_irqsave(&intf->xmit_msgs_lock, flags);
3892 if (intf->curr_msg == NULL && !intf->in_shutdown) {
3893 struct list_head *entry = NULL;
3895 /* Pick the high priority queue first. */
3896 if (!list_empty(&intf->hp_xmit_msgs))
3897 entry = intf->hp_xmit_msgs.next;
3898 else if (!list_empty(&intf->xmit_msgs))
3899 entry = intf->xmit_msgs.next;
3903 newmsg = list_entry(entry, struct ipmi_smi_msg, link);
3904 intf->curr_msg = newmsg;
3907 if (!run_to_completion)
3908 spin_unlock_irqrestore(&intf->xmit_msgs_lock, flags);
3910 intf->handlers->sender(intf->send_info, newmsg);
3914 handle_new_recv_msgs(intf);
3917 /* Handle a new message from the lower layer. */
3918 void ipmi_smi_msg_received(ipmi_smi_t intf,
3919 struct ipmi_smi_msg *msg)
3921 unsigned long flags = 0; /* keep us warning-free. */
3922 int run_to_completion = intf->run_to_completion;
3924 if ((msg->data_size >= 2)
3925 && (msg->data[0] == (IPMI_NETFN_APP_REQUEST << 2))
3926 && (msg->data[1] == IPMI_SEND_MSG_CMD)
3927 && (msg->user_data == NULL)) {
3929 if (intf->in_shutdown)
3933 * This is the local response to a command send, start
3934 * the timer for these. The user_data will not be
3935 * NULL if this is a response send, and we will let
3936 * response sends just go through.
3940 * Check for errors, if we get certain errors (ones
3941 * that mean basically we can try again later), we
3942 * ignore them and start the timer. Otherwise we
3943 * report the error immediately.
3945 if ((msg->rsp_size >= 3) && (msg->rsp[2] != 0)
3946 && (msg->rsp[2] != IPMI_NODE_BUSY_ERR)
3947 && (msg->rsp[2] != IPMI_LOST_ARBITRATION_ERR)
3948 && (msg->rsp[2] != IPMI_BUS_ERR)
3949 && (msg->rsp[2] != IPMI_NAK_ON_WRITE_ERR)) {
3950 int chan = msg->rsp[3] & 0xf;
3952 /* Got an error sending the message, handle it. */
3953 if (chan >= IPMI_MAX_CHANNELS)
3954 ; /* This shouldn't happen */
3955 else if ((intf->channels[chan].medium
3956 == IPMI_CHANNEL_MEDIUM_8023LAN)
3957 || (intf->channels[chan].medium
3958 == IPMI_CHANNEL_MEDIUM_ASYNC))
3959 ipmi_inc_stat(intf, sent_lan_command_errs);
3961 ipmi_inc_stat(intf, sent_ipmb_command_errs);
3962 intf_err_seq(intf, msg->msgid, msg->rsp[2]);
3964 /* The message was sent, start the timer. */
3965 intf_start_seq_timer(intf, msg->msgid);
3968 ipmi_free_smi_msg(msg);
3971 * To preserve message order, we keep a queue and deliver from
3974 if (!run_to_completion)
3975 spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags);
3976 list_add_tail(&msg->link, &intf->waiting_rcv_msgs);
3977 if (!run_to_completion)
3978 spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock,
3982 if (!run_to_completion)
3983 spin_lock_irqsave(&intf->xmit_msgs_lock, flags);
3985 * We can get an asynchronous event or receive message in addition
3986 * to commands we send.
3988 if (msg == intf->curr_msg)
3989 intf->curr_msg = NULL;
3990 if (!run_to_completion)
3991 spin_unlock_irqrestore(&intf->xmit_msgs_lock, flags);
3993 if (run_to_completion)
3994 smi_recv_tasklet((unsigned long) intf);
3996 tasklet_schedule(&intf->recv_tasklet);
3998 EXPORT_SYMBOL(ipmi_smi_msg_received);
4000 void ipmi_smi_watchdog_pretimeout(ipmi_smi_t intf)
4002 if (intf->in_shutdown)
4005 atomic_set(&intf->watchdog_pretimeouts_to_deliver, 1);
4006 tasklet_schedule(&intf->recv_tasklet);
4008 EXPORT_SYMBOL(ipmi_smi_watchdog_pretimeout);
4010 static struct ipmi_smi_msg *
4011 smi_from_recv_msg(ipmi_smi_t intf, struct ipmi_recv_msg *recv_msg,
4012 unsigned char seq, long seqid)
4014 struct ipmi_smi_msg *smi_msg = ipmi_alloc_smi_msg();
4017 * If we can't allocate the message, then just return, we
4018 * get 4 retries, so this should be ok.
4022 memcpy(smi_msg->data, recv_msg->msg.data, recv_msg->msg.data_len);
4023 smi_msg->data_size = recv_msg->msg.data_len;
4024 smi_msg->msgid = STORE_SEQ_IN_MSGID(seq, seqid);
4030 for (m = 0; m < smi_msg->data_size; m++)
4031 printk(" %2.2x", smi_msg->data[m]);
4038 static void check_msg_timeout(ipmi_smi_t intf, struct seq_table *ent,
4039 struct list_head *timeouts,
4040 unsigned long timeout_period,
4041 int slot, unsigned long *flags,
4042 unsigned int *waiting_msgs)
4044 struct ipmi_recv_msg *msg;
4045 const struct ipmi_smi_handlers *handlers;
4047 if (intf->in_shutdown)
4053 if (timeout_period < ent->timeout) {
4054 ent->timeout -= timeout_period;
4059 if (ent->retries_left == 0) {
4060 /* The message has used all its retries. */
4062 msg = ent->recv_msg;
4063 list_add_tail(&msg->link, timeouts);
4065 ipmi_inc_stat(intf, timed_out_ipmb_broadcasts);
4066 else if (is_lan_addr(&ent->recv_msg->addr))
4067 ipmi_inc_stat(intf, timed_out_lan_commands);
4069 ipmi_inc_stat(intf, timed_out_ipmb_commands);
4071 struct ipmi_smi_msg *smi_msg;
4072 /* More retries, send again. */
4077 * Start with the max timer, set to normal timer after
4078 * the message is sent.
4080 ent->timeout = MAX_MSG_TIMEOUT;
4081 ent->retries_left--;
4082 smi_msg = smi_from_recv_msg(intf, ent->recv_msg, slot,
4085 if (is_lan_addr(&ent->recv_msg->addr))
4087 dropped_rexmit_lan_commands);
4090 dropped_rexmit_ipmb_commands);
4094 spin_unlock_irqrestore(&intf->seq_lock, *flags);
4097 * Send the new message. We send with a zero
4098 * priority. It timed out, I doubt time is that
4099 * critical now, and high priority messages are really
4100 * only for messages to the local MC, which don't get
4103 handlers = intf->handlers;
4105 if (is_lan_addr(&ent->recv_msg->addr))
4107 retransmitted_lan_commands);
4110 retransmitted_ipmb_commands);
4112 smi_send(intf, handlers, smi_msg, 0);
4114 ipmi_free_smi_msg(smi_msg);
4116 spin_lock_irqsave(&intf->seq_lock, *flags);
4120 static unsigned int ipmi_timeout_handler(ipmi_smi_t intf,
4121 unsigned long timeout_period)
4123 struct list_head timeouts;
4124 struct ipmi_recv_msg *msg, *msg2;
4125 unsigned long flags;
4127 unsigned int waiting_msgs = 0;
4130 * Go through the seq table and find any messages that
4131 * have timed out, putting them in the timeouts
4134 INIT_LIST_HEAD(&timeouts);
4135 spin_lock_irqsave(&intf->seq_lock, flags);
4136 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++)
4137 check_msg_timeout(intf, &(intf->seq_table[i]),
4138 &timeouts, timeout_period, i,
4139 &flags, &waiting_msgs);
4140 spin_unlock_irqrestore(&intf->seq_lock, flags);
4142 list_for_each_entry_safe(msg, msg2, &timeouts, link)
4143 deliver_err_response(msg, IPMI_TIMEOUT_COMPLETION_CODE);
4146 * Maintenance mode handling. Check the timeout
4147 * optimistically before we claim the lock. It may
4148 * mean a timeout gets missed occasionally, but that
4149 * only means the timeout gets extended by one period
4150 * in that case. No big deal, and it avoids the lock
4153 if (intf->auto_maintenance_timeout > 0) {
4154 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
4155 if (intf->auto_maintenance_timeout > 0) {
4156 intf->auto_maintenance_timeout
4158 if (!intf->maintenance_mode
4159 && (intf->auto_maintenance_timeout <= 0)) {
4160 intf->maintenance_mode_enable = false;
4161 maintenance_mode_update(intf);
4164 spin_unlock_irqrestore(&intf->maintenance_mode_lock,
4168 tasklet_schedule(&intf->recv_tasklet);
4170 return waiting_msgs;
4173 static void ipmi_request_event(ipmi_smi_t intf)
4175 /* No event requests when in maintenance mode. */
4176 if (intf->maintenance_mode_enable)
4179 if (!intf->in_shutdown)
4180 intf->handlers->request_events(intf->send_info);
4183 static struct timer_list ipmi_timer;
4185 static atomic_t stop_operation;
4187 static void ipmi_timeout(unsigned long data)
4192 if (atomic_read(&stop_operation))
4196 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
4199 if (atomic_read(&intf->event_waiters)) {
4200 intf->ticks_to_req_ev--;
4201 if (intf->ticks_to_req_ev == 0) {
4202 ipmi_request_event(intf);
4203 intf->ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
4208 lnt += ipmi_timeout_handler(intf, IPMI_TIMEOUT_TIME);
4211 if (lnt != intf->last_needs_timer &&
4212 intf->handlers->set_need_watch)
4213 intf->handlers->set_need_watch(intf->send_info, lnt);
4214 intf->last_needs_timer = lnt;
4221 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
4224 static void need_waiter(ipmi_smi_t intf)
4226 /* Racy, but worst case we start the timer twice. */
4227 if (!timer_pending(&ipmi_timer))
4228 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
4231 static atomic_t smi_msg_inuse_count = ATOMIC_INIT(0);
4232 static atomic_t recv_msg_inuse_count = ATOMIC_INIT(0);
4234 static void free_smi_msg(struct ipmi_smi_msg *msg)
4236 atomic_dec(&smi_msg_inuse_count);
4240 struct ipmi_smi_msg *ipmi_alloc_smi_msg(void)
4242 struct ipmi_smi_msg *rv;
4243 rv = kmalloc(sizeof(struct ipmi_smi_msg), GFP_ATOMIC);
4245 rv->done = free_smi_msg;
4246 rv->user_data = NULL;
4247 atomic_inc(&smi_msg_inuse_count);
4251 EXPORT_SYMBOL(ipmi_alloc_smi_msg);
4253 static void free_recv_msg(struct ipmi_recv_msg *msg)
4255 atomic_dec(&recv_msg_inuse_count);
4259 static struct ipmi_recv_msg *ipmi_alloc_recv_msg(void)
4261 struct ipmi_recv_msg *rv;
4263 rv = kmalloc(sizeof(struct ipmi_recv_msg), GFP_ATOMIC);
4266 rv->done = free_recv_msg;
4267 atomic_inc(&recv_msg_inuse_count);
4272 void ipmi_free_recv_msg(struct ipmi_recv_msg *msg)
4275 kref_put(&msg->user->refcount, free_user);
4278 EXPORT_SYMBOL(ipmi_free_recv_msg);
4280 #ifdef CONFIG_IPMI_PANIC_EVENT
4282 static atomic_t panic_done_count = ATOMIC_INIT(0);
4284 static void dummy_smi_done_handler(struct ipmi_smi_msg *msg)
4286 atomic_dec(&panic_done_count);
4289 static void dummy_recv_done_handler(struct ipmi_recv_msg *msg)
4291 atomic_dec(&panic_done_count);
4295 * Inside a panic, send a message and wait for a response.
4297 static void ipmi_panic_request_and_wait(ipmi_smi_t intf,
4298 struct ipmi_addr *addr,
4299 struct kernel_ipmi_msg *msg)
4301 struct ipmi_smi_msg smi_msg;
4302 struct ipmi_recv_msg recv_msg;
4305 smi_msg.done = dummy_smi_done_handler;
4306 recv_msg.done = dummy_recv_done_handler;
4307 atomic_add(2, &panic_done_count);
4308 rv = i_ipmi_request(NULL,
4317 intf->channels[0].address,
4318 intf->channels[0].lun,
4319 0, 1); /* Don't retry, and don't wait. */
4321 atomic_sub(2, &panic_done_count);
4322 else if (intf->handlers->flush_messages)
4323 intf->handlers->flush_messages(intf->send_info);
4325 while (atomic_read(&panic_done_count) != 0)
4329 #ifdef CONFIG_IPMI_PANIC_STRING
4330 static void event_receiver_fetcher(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
4332 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
4333 && (msg->msg.netfn == IPMI_NETFN_SENSOR_EVENT_RESPONSE)
4334 && (msg->msg.cmd == IPMI_GET_EVENT_RECEIVER_CMD)
4335 && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) {
4336 /* A get event receiver command, save it. */
4337 intf->event_receiver = msg->msg.data[1];
4338 intf->event_receiver_lun = msg->msg.data[2] & 0x3;
4342 static void device_id_fetcher(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
4344 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
4345 && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
4346 && (msg->msg.cmd == IPMI_GET_DEVICE_ID_CMD)
4347 && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) {
4349 * A get device id command, save if we are an event
4350 * receiver or generator.
4352 intf->local_sel_device = (msg->msg.data[6] >> 2) & 1;
4353 intf->local_event_generator = (msg->msg.data[6] >> 5) & 1;
4358 static void send_panic_events(char *str)
4360 struct kernel_ipmi_msg msg;
4362 unsigned char data[16];
4363 struct ipmi_system_interface_addr *si;
4364 struct ipmi_addr addr;
4366 si = (struct ipmi_system_interface_addr *) &addr;
4367 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
4368 si->channel = IPMI_BMC_CHANNEL;
4371 /* Fill in an event telling that we have failed. */
4372 msg.netfn = 0x04; /* Sensor or Event. */
4373 msg.cmd = 2; /* Platform event command. */
4376 data[0] = 0x41; /* Kernel generator ID, IPMI table 5-4 */
4377 data[1] = 0x03; /* This is for IPMI 1.0. */
4378 data[2] = 0x20; /* OS Critical Stop, IPMI table 36-3 */
4379 data[4] = 0x6f; /* Sensor specific, IPMI table 36-1 */
4380 data[5] = 0xa1; /* Runtime stop OEM bytes 2 & 3. */
4383 * Put a few breadcrumbs in. Hopefully later we can add more things
4384 * to make the panic events more useful.
4392 /* For every registered interface, send the event. */
4393 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
4394 if (!intf->handlers)
4395 /* Interface is not ready. */
4398 /* Send the event announcing the panic. */
4399 ipmi_panic_request_and_wait(intf, &addr, &msg);
4402 #ifdef CONFIG_IPMI_PANIC_STRING
4404 * On every interface, dump a bunch of OEM event holding the
4410 /* For every registered interface, send the event. */
4411 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
4413 struct ipmi_ipmb_addr *ipmb;
4416 if (intf->intf_num == -1)
4417 /* Interface was not ready yet. */
4421 * intf_num is used as an marker to tell if the
4422 * interface is valid. Thus we need a read barrier to
4423 * make sure data fetched before checking intf_num
4429 * First job here is to figure out where to send the
4430 * OEM events. There's no way in IPMI to send OEM
4431 * events using an event send command, so we have to
4432 * find the SEL to put them in and stick them in
4436 /* Get capabilities from the get device id. */
4437 intf->local_sel_device = 0;
4438 intf->local_event_generator = 0;
4439 intf->event_receiver = 0;
4441 /* Request the device info from the local MC. */
4442 msg.netfn = IPMI_NETFN_APP_REQUEST;
4443 msg.cmd = IPMI_GET_DEVICE_ID_CMD;
4446 intf->null_user_handler = device_id_fetcher;
4447 ipmi_panic_request_and_wait(intf, &addr, &msg);
4449 if (intf->local_event_generator) {
4450 /* Request the event receiver from the local MC. */
4451 msg.netfn = IPMI_NETFN_SENSOR_EVENT_REQUEST;
4452 msg.cmd = IPMI_GET_EVENT_RECEIVER_CMD;
4455 intf->null_user_handler = event_receiver_fetcher;
4456 ipmi_panic_request_and_wait(intf, &addr, &msg);
4458 intf->null_user_handler = NULL;
4461 * Validate the event receiver. The low bit must not
4462 * be 1 (it must be a valid IPMB address), it cannot
4463 * be zero, and it must not be my address.
4465 if (((intf->event_receiver & 1) == 0)
4466 && (intf->event_receiver != 0)
4467 && (intf->event_receiver != intf->channels[0].address)) {
4469 * The event receiver is valid, send an IPMB
4472 ipmb = (struct ipmi_ipmb_addr *) &addr;
4473 ipmb->addr_type = IPMI_IPMB_ADDR_TYPE;
4474 ipmb->channel = 0; /* FIXME - is this right? */
4475 ipmb->lun = intf->event_receiver_lun;
4476 ipmb->slave_addr = intf->event_receiver;
4477 } else if (intf->local_sel_device) {
4479 * The event receiver was not valid (or was
4480 * me), but I am an SEL device, just dump it
4483 si = (struct ipmi_system_interface_addr *) &addr;
4484 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
4485 si->channel = IPMI_BMC_CHANNEL;
4488 continue; /* No where to send the event. */
4490 msg.netfn = IPMI_NETFN_STORAGE_REQUEST; /* Storage. */
4491 msg.cmd = IPMI_ADD_SEL_ENTRY_CMD;
4497 int size = strlen(p);
4503 data[2] = 0xf0; /* OEM event without timestamp. */
4504 data[3] = intf->channels[0].address;
4505 data[4] = j++; /* sequence # */
4507 * Always give 11 bytes, so strncpy will fill
4508 * it with zeroes for me.
4510 strncpy(data+5, p, 11);
4513 ipmi_panic_request_and_wait(intf, &addr, &msg);
4516 #endif /* CONFIG_IPMI_PANIC_STRING */
4518 #endif /* CONFIG_IPMI_PANIC_EVENT */
4520 static int has_panicked;
4522 static int panic_event(struct notifier_block *this,
4523 unsigned long event,
4532 /* For every registered interface, set it to run to completion. */
4533 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
4534 if (!intf->handlers)
4535 /* Interface is not ready. */
4539 * If we were interrupted while locking xmit_msgs_lock or
4540 * waiting_rcv_msgs_lock, the corresponding list may be
4541 * corrupted. In this case, drop items on the list for
4544 if (!spin_trylock(&intf->xmit_msgs_lock)) {
4545 INIT_LIST_HEAD(&intf->xmit_msgs);
4546 INIT_LIST_HEAD(&intf->hp_xmit_msgs);
4548 spin_unlock(&intf->xmit_msgs_lock);
4550 if (!spin_trylock(&intf->waiting_rcv_msgs_lock))
4551 INIT_LIST_HEAD(&intf->waiting_rcv_msgs);
4553 spin_unlock(&intf->waiting_rcv_msgs_lock);
4555 intf->run_to_completion = 1;
4556 intf->handlers->set_run_to_completion(intf->send_info, 1);
4559 #ifdef CONFIG_IPMI_PANIC_EVENT
4560 send_panic_events(ptr);
4566 static struct notifier_block panic_block = {
4567 .notifier_call = panic_event,
4569 .priority = 200 /* priority: INT_MAX >= x >= 0 */
4572 static int ipmi_init_msghandler(void)
4579 rv = driver_register(&ipmidriver.driver);
4581 printk(KERN_ERR PFX "Could not register IPMI driver\n");
4585 printk(KERN_INFO "ipmi message handler version "
4586 IPMI_DRIVER_VERSION "\n");
4588 #ifdef CONFIG_PROC_FS
4589 proc_ipmi_root = proc_mkdir("ipmi", NULL);
4590 if (!proc_ipmi_root) {
4591 printk(KERN_ERR PFX "Unable to create IPMI proc dir");
4592 driver_unregister(&ipmidriver.driver);
4596 #endif /* CONFIG_PROC_FS */
4598 setup_timer(&ipmi_timer, ipmi_timeout, 0);
4599 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
4601 atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
4608 static int __init ipmi_init_msghandler_mod(void)
4610 ipmi_init_msghandler();
4614 static void __exit cleanup_ipmi(void)
4621 atomic_notifier_chain_unregister(&panic_notifier_list, &panic_block);
4624 * This can't be called if any interfaces exist, so no worry
4625 * about shutting down the interfaces.
4629 * Tell the timer to stop, then wait for it to stop. This
4630 * avoids problems with race conditions removing the timer
4633 atomic_inc(&stop_operation);
4634 del_timer_sync(&ipmi_timer);
4636 #ifdef CONFIG_PROC_FS
4637 proc_remove(proc_ipmi_root);
4638 #endif /* CONFIG_PROC_FS */
4640 driver_unregister(&ipmidriver.driver);
4644 /* Check for buffer leaks. */
4645 count = atomic_read(&smi_msg_inuse_count);
4647 printk(KERN_WARNING PFX "SMI message count %d at exit\n",
4649 count = atomic_read(&recv_msg_inuse_count);
4651 printk(KERN_WARNING PFX "recv message count %d at exit\n",
4654 module_exit(cleanup_ipmi);
4656 module_init(ipmi_init_msghandler_mod);
4657 MODULE_LICENSE("GPL");
4658 MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
4659 MODULE_DESCRIPTION("Incoming and outgoing message routing for an IPMI"
4661 MODULE_VERSION(IPMI_DRIVER_VERSION);
4662 MODULE_SOFTDEP("post: ipmi_devintf");