Linux 2.6.34-rc3
[pohmelfs.git] / drivers / char / ipmi / ipmi_msghandler.c
blobc6ad4234378d7e5cd0818d839e81c3b641e1d426
1 /*
2 * ipmi_msghandler.c
4 * Incoming and outgoing message routing for an IPMI interface.
6 * Author: MontaVista Software, Inc.
7 * Corey Minyard <minyard@mvista.com>
8 * source@mvista.com
10 * Copyright 2002 MontaVista Software Inc.
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
19 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
24 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
26 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
27 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * You should have received a copy of the GNU General Public License along
30 * with this program; if not, write to the Free Software Foundation, Inc.,
31 * 675 Mass Ave, Cambridge, MA 02139, USA.
34 #include <linux/module.h>
35 #include <linux/errno.h>
36 #include <asm/system.h>
37 #include <linux/poll.h>
38 #include <linux/sched.h>
39 #include <linux/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>
49 #define PFX "IPMI message handler: "
51 #define IPMI_DRIVER_VERSION "39.2"
53 static struct ipmi_recv_msg *ipmi_alloc_recv_msg(void);
54 static int ipmi_init_msghandler(void);
56 static int initialized;
58 #ifdef CONFIG_PROC_FS
59 static struct proc_dir_entry *proc_ipmi_root;
60 #endif /* CONFIG_PROC_FS */
62 /* Remain in auto-maintenance mode for this amount of time (in ms). */
63 #define IPMI_MAINTENANCE_MODE_TIMEOUT 30000
65 #define MAX_EVENTS_IN_QUEUE 25
68 * Don't let a message sit in a queue forever, always time it with at lest
69 * the max message timer. This is in milliseconds.
71 #define MAX_MSG_TIMEOUT 60000
74 * The main "user" data structure.
76 struct ipmi_user {
77 struct list_head link;
79 /* Set to "0" when the user is destroyed. */
80 int valid;
82 struct kref refcount;
84 /* The upper layer that handles receive messages. */
85 struct ipmi_user_hndl *handler;
86 void *handler_data;
88 /* The interface this user is bound to. */
89 ipmi_smi_t intf;
91 /* Does this interface receive IPMI events? */
92 int gets_events;
95 struct cmd_rcvr {
96 struct list_head link;
98 ipmi_user_t user;
99 unsigned char netfn;
100 unsigned char cmd;
101 unsigned int chans;
104 * This is used to form a linked lised during mass deletion.
105 * Since this is in an RCU list, we cannot use the link above
106 * or change any data until the RCU period completes. So we
107 * use this next variable during mass deletion so we can have
108 * a list and don't have to wait and restart the search on
109 * every individual deletion of a command.
111 struct cmd_rcvr *next;
114 struct seq_table {
115 unsigned int inuse : 1;
116 unsigned int broadcast : 1;
118 unsigned long timeout;
119 unsigned long orig_timeout;
120 unsigned int retries_left;
123 * To verify on an incoming send message response that this is
124 * the message that the response is for, we keep a sequence id
125 * and increment it every time we send a message.
127 long seqid;
130 * This is held so we can properly respond to the message on a
131 * timeout, and it is used to hold the temporary data for
132 * retransmission, too.
134 struct ipmi_recv_msg *recv_msg;
138 * Store the information in a msgid (long) to allow us to find a
139 * sequence table entry from the msgid.
141 #define STORE_SEQ_IN_MSGID(seq, seqid) (((seq&0xff)<<26) | (seqid&0x3ffffff))
143 #define GET_SEQ_FROM_MSGID(msgid, seq, seqid) \
144 do { \
145 seq = ((msgid >> 26) & 0x3f); \
146 seqid = (msgid & 0x3fffff); \
147 } while (0)
149 #define NEXT_SEQID(seqid) (((seqid) + 1) & 0x3fffff)
151 struct ipmi_channel {
152 unsigned char medium;
153 unsigned char protocol;
156 * My slave address. This is initialized to IPMI_BMC_SLAVE_ADDR,
157 * but may be changed by the user.
159 unsigned char address;
162 * My LUN. This should generally stay the SMS LUN, but just in
163 * case...
165 unsigned char lun;
168 #ifdef CONFIG_PROC_FS
169 struct ipmi_proc_entry {
170 char *name;
171 struct ipmi_proc_entry *next;
173 #endif
175 struct bmc_device {
176 struct platform_device *dev;
177 struct ipmi_device_id id;
178 unsigned char guid[16];
179 int guid_set;
181 struct kref refcount;
183 /* bmc device attributes */
184 struct device_attribute device_id_attr;
185 struct device_attribute provides_dev_sdrs_attr;
186 struct device_attribute revision_attr;
187 struct device_attribute firmware_rev_attr;
188 struct device_attribute version_attr;
189 struct device_attribute add_dev_support_attr;
190 struct device_attribute manufacturer_id_attr;
191 struct device_attribute product_id_attr;
192 struct device_attribute guid_attr;
193 struct device_attribute aux_firmware_rev_attr;
197 * Various statistics for IPMI, these index stats[] in the ipmi_smi
198 * structure.
200 enum ipmi_stat_indexes {
201 /* Commands we got from the user that were invalid. */
202 IPMI_STAT_sent_invalid_commands = 0,
204 /* Commands we sent to the MC. */
205 IPMI_STAT_sent_local_commands,
207 /* Responses from the MC that were delivered to a user. */
208 IPMI_STAT_handled_local_responses,
210 /* Responses from the MC that were not delivered to a user. */
211 IPMI_STAT_unhandled_local_responses,
213 /* Commands we sent out to the IPMB bus. */
214 IPMI_STAT_sent_ipmb_commands,
216 /* Commands sent on the IPMB that had errors on the SEND CMD */
217 IPMI_STAT_sent_ipmb_command_errs,
219 /* Each retransmit increments this count. */
220 IPMI_STAT_retransmitted_ipmb_commands,
223 * When a message times out (runs out of retransmits) this is
224 * incremented.
226 IPMI_STAT_timed_out_ipmb_commands,
229 * This is like above, but for broadcasts. Broadcasts are
230 * *not* included in the above count (they are expected to
231 * time out).
233 IPMI_STAT_timed_out_ipmb_broadcasts,
235 /* Responses I have sent to the IPMB bus. */
236 IPMI_STAT_sent_ipmb_responses,
238 /* The response was delivered to the user. */
239 IPMI_STAT_handled_ipmb_responses,
241 /* The response had invalid data in it. */
242 IPMI_STAT_invalid_ipmb_responses,
244 /* The response didn't have anyone waiting for it. */
245 IPMI_STAT_unhandled_ipmb_responses,
247 /* Commands we sent out to the IPMB bus. */
248 IPMI_STAT_sent_lan_commands,
250 /* Commands sent on the IPMB that had errors on the SEND CMD */
251 IPMI_STAT_sent_lan_command_errs,
253 /* Each retransmit increments this count. */
254 IPMI_STAT_retransmitted_lan_commands,
257 * When a message times out (runs out of retransmits) this is
258 * incremented.
260 IPMI_STAT_timed_out_lan_commands,
262 /* Responses I have sent to the IPMB bus. */
263 IPMI_STAT_sent_lan_responses,
265 /* The response was delivered to the user. */
266 IPMI_STAT_handled_lan_responses,
268 /* The response had invalid data in it. */
269 IPMI_STAT_invalid_lan_responses,
271 /* The response didn't have anyone waiting for it. */
272 IPMI_STAT_unhandled_lan_responses,
274 /* The command was delivered to the user. */
275 IPMI_STAT_handled_commands,
277 /* The command had invalid data in it. */
278 IPMI_STAT_invalid_commands,
280 /* The command didn't have anyone waiting for it. */
281 IPMI_STAT_unhandled_commands,
283 /* Invalid data in an event. */
284 IPMI_STAT_invalid_events,
286 /* Events that were received with the proper format. */
287 IPMI_STAT_events,
289 /* Retransmissions on IPMB that failed. */
290 IPMI_STAT_dropped_rexmit_ipmb_commands,
292 /* Retransmissions on LAN that failed. */
293 IPMI_STAT_dropped_rexmit_lan_commands,
295 /* This *must* remain last, add new values above this. */
296 IPMI_NUM_STATS
300 #define IPMI_IPMB_NUM_SEQ 64
301 #define IPMI_MAX_CHANNELS 16
302 struct ipmi_smi {
303 /* What interface number are we? */
304 int intf_num;
306 struct kref refcount;
308 /* Used for a list of interfaces. */
309 struct list_head link;
312 * The list of upper layers that are using me. seq_lock
313 * protects this.
315 struct list_head users;
317 /* Information to supply to users. */
318 unsigned char ipmi_version_major;
319 unsigned char ipmi_version_minor;
321 /* Used for wake ups at startup. */
322 wait_queue_head_t waitq;
324 struct bmc_device *bmc;
325 char *my_dev_name;
326 char *sysfs_name;
329 * This is the lower-layer's sender routine. Note that you
330 * must either be holding the ipmi_interfaces_mutex or be in
331 * an umpreemptible region to use this. You must fetch the
332 * value into a local variable and make sure it is not NULL.
334 struct ipmi_smi_handlers *handlers;
335 void *send_info;
337 #ifdef CONFIG_PROC_FS
338 /* A list of proc entries for this interface. */
339 struct mutex proc_entry_lock;
340 struct ipmi_proc_entry *proc_entries;
341 #endif
343 /* Driver-model device for the system interface. */
344 struct device *si_dev;
347 * A table of sequence numbers for this interface. We use the
348 * sequence numbers for IPMB messages that go out of the
349 * interface to match them up with their responses. A routine
350 * is called periodically to time the items in this list.
352 spinlock_t seq_lock;
353 struct seq_table seq_table[IPMI_IPMB_NUM_SEQ];
354 int curr_seq;
357 * Messages that were delayed for some reason (out of memory,
358 * for instance), will go in here to be processed later in a
359 * periodic timer interrupt.
361 spinlock_t waiting_msgs_lock;
362 struct list_head waiting_msgs;
365 * The list of command receivers that are registered for commands
366 * on this interface.
368 struct mutex cmd_rcvrs_mutex;
369 struct list_head cmd_rcvrs;
372 * Events that were queues because no one was there to receive
373 * them.
375 spinlock_t events_lock; /* For dealing with event stuff. */
376 struct list_head waiting_events;
377 unsigned int waiting_events_count; /* How many events in queue? */
378 char delivering_events;
379 char event_msg_printed;
382 * The event receiver for my BMC, only really used at panic
383 * shutdown as a place to store this.
385 unsigned char event_receiver;
386 unsigned char event_receiver_lun;
387 unsigned char local_sel_device;
388 unsigned char local_event_generator;
390 /* For handling of maintenance mode. */
391 int maintenance_mode;
392 int maintenance_mode_enable;
393 int auto_maintenance_timeout;
394 spinlock_t maintenance_mode_lock; /* Used in a timer... */
397 * A cheap hack, if this is non-null and a message to an
398 * interface comes in with a NULL user, call this routine with
399 * it. Note that the message will still be freed by the
400 * caller. This only works on the system interface.
402 void (*null_user_handler)(ipmi_smi_t intf, struct ipmi_recv_msg *msg);
405 * When we are scanning the channels for an SMI, this will
406 * tell which channel we are scanning.
408 int curr_channel;
410 /* Channel information */
411 struct ipmi_channel channels[IPMI_MAX_CHANNELS];
413 /* Proc FS stuff. */
414 struct proc_dir_entry *proc_dir;
415 char proc_dir_name[10];
417 atomic_t stats[IPMI_NUM_STATS];
420 * run_to_completion duplicate of smb_info, smi_info
421 * and ipmi_serial_info structures. Used to decrease numbers of
422 * parameters passed by "low" level IPMI code.
424 int run_to_completion;
426 #define to_si_intf_from_dev(device) container_of(device, struct ipmi_smi, dev)
429 * The driver model view of the IPMI messaging driver.
431 static struct platform_driver ipmidriver = {
432 .driver = {
433 .name = "ipmi",
434 .bus = &platform_bus_type
437 static DEFINE_MUTEX(ipmidriver_mutex);
439 static LIST_HEAD(ipmi_interfaces);
440 static DEFINE_MUTEX(ipmi_interfaces_mutex);
443 * List of watchers that want to know when smi's are added and deleted.
445 static LIST_HEAD(smi_watchers);
446 static DEFINE_MUTEX(smi_watchers_mutex);
449 #define ipmi_inc_stat(intf, stat) \
450 atomic_inc(&(intf)->stats[IPMI_STAT_ ## stat])
451 #define ipmi_get_stat(intf, stat) \
452 ((unsigned int) atomic_read(&(intf)->stats[IPMI_STAT_ ## stat]))
454 static int is_lan_addr(struct ipmi_addr *addr)
456 return addr->addr_type == IPMI_LAN_ADDR_TYPE;
459 static int is_ipmb_addr(struct ipmi_addr *addr)
461 return addr->addr_type == IPMI_IPMB_ADDR_TYPE;
464 static int is_ipmb_bcast_addr(struct ipmi_addr *addr)
466 return addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE;
469 static void free_recv_msg_list(struct list_head *q)
471 struct ipmi_recv_msg *msg, *msg2;
473 list_for_each_entry_safe(msg, msg2, q, link) {
474 list_del(&msg->link);
475 ipmi_free_recv_msg(msg);
479 static void free_smi_msg_list(struct list_head *q)
481 struct ipmi_smi_msg *msg, *msg2;
483 list_for_each_entry_safe(msg, msg2, q, link) {
484 list_del(&msg->link);
485 ipmi_free_smi_msg(msg);
489 static void clean_up_interface_data(ipmi_smi_t intf)
491 int i;
492 struct cmd_rcvr *rcvr, *rcvr2;
493 struct list_head list;
495 free_smi_msg_list(&intf->waiting_msgs);
496 free_recv_msg_list(&intf->waiting_events);
499 * Wholesale remove all the entries from the list in the
500 * interface and wait for RCU to know that none are in use.
502 mutex_lock(&intf->cmd_rcvrs_mutex);
503 INIT_LIST_HEAD(&list);
504 list_splice_init_rcu(&intf->cmd_rcvrs, &list, synchronize_rcu);
505 mutex_unlock(&intf->cmd_rcvrs_mutex);
507 list_for_each_entry_safe(rcvr, rcvr2, &list, link)
508 kfree(rcvr);
510 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
511 if ((intf->seq_table[i].inuse)
512 && (intf->seq_table[i].recv_msg))
513 ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
517 static void intf_free(struct kref *ref)
519 ipmi_smi_t intf = container_of(ref, struct ipmi_smi, refcount);
521 clean_up_interface_data(intf);
522 kfree(intf);
525 struct watcher_entry {
526 int intf_num;
527 ipmi_smi_t intf;
528 struct list_head link;
531 int ipmi_smi_watcher_register(struct ipmi_smi_watcher *watcher)
533 ipmi_smi_t intf;
534 LIST_HEAD(to_deliver);
535 struct watcher_entry *e, *e2;
537 mutex_lock(&smi_watchers_mutex);
539 mutex_lock(&ipmi_interfaces_mutex);
541 /* Build a list of things to deliver. */
542 list_for_each_entry(intf, &ipmi_interfaces, link) {
543 if (intf->intf_num == -1)
544 continue;
545 e = kmalloc(sizeof(*e), GFP_KERNEL);
546 if (!e)
547 goto out_err;
548 kref_get(&intf->refcount);
549 e->intf = intf;
550 e->intf_num = intf->intf_num;
551 list_add_tail(&e->link, &to_deliver);
554 /* We will succeed, so add it to the list. */
555 list_add(&watcher->link, &smi_watchers);
557 mutex_unlock(&ipmi_interfaces_mutex);
559 list_for_each_entry_safe(e, e2, &to_deliver, link) {
560 list_del(&e->link);
561 watcher->new_smi(e->intf_num, e->intf->si_dev);
562 kref_put(&e->intf->refcount, intf_free);
563 kfree(e);
566 mutex_unlock(&smi_watchers_mutex);
568 return 0;
570 out_err:
571 mutex_unlock(&ipmi_interfaces_mutex);
572 mutex_unlock(&smi_watchers_mutex);
573 list_for_each_entry_safe(e, e2, &to_deliver, link) {
574 list_del(&e->link);
575 kref_put(&e->intf->refcount, intf_free);
576 kfree(e);
578 return -ENOMEM;
580 EXPORT_SYMBOL(ipmi_smi_watcher_register);
582 int ipmi_smi_watcher_unregister(struct ipmi_smi_watcher *watcher)
584 mutex_lock(&smi_watchers_mutex);
585 list_del(&(watcher->link));
586 mutex_unlock(&smi_watchers_mutex);
587 return 0;
589 EXPORT_SYMBOL(ipmi_smi_watcher_unregister);
592 * Must be called with smi_watchers_mutex held.
594 static void
595 call_smi_watchers(int i, struct device *dev)
597 struct ipmi_smi_watcher *w;
599 list_for_each_entry(w, &smi_watchers, link) {
600 if (try_module_get(w->owner)) {
601 w->new_smi(i, dev);
602 module_put(w->owner);
607 static int
608 ipmi_addr_equal(struct ipmi_addr *addr1, struct ipmi_addr *addr2)
610 if (addr1->addr_type != addr2->addr_type)
611 return 0;
613 if (addr1->channel != addr2->channel)
614 return 0;
616 if (addr1->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
617 struct ipmi_system_interface_addr *smi_addr1
618 = (struct ipmi_system_interface_addr *) addr1;
619 struct ipmi_system_interface_addr *smi_addr2
620 = (struct ipmi_system_interface_addr *) addr2;
621 return (smi_addr1->lun == smi_addr2->lun);
624 if (is_ipmb_addr(addr1) || is_ipmb_bcast_addr(addr1)) {
625 struct ipmi_ipmb_addr *ipmb_addr1
626 = (struct ipmi_ipmb_addr *) addr1;
627 struct ipmi_ipmb_addr *ipmb_addr2
628 = (struct ipmi_ipmb_addr *) addr2;
630 return ((ipmb_addr1->slave_addr == ipmb_addr2->slave_addr)
631 && (ipmb_addr1->lun == ipmb_addr2->lun));
634 if (is_lan_addr(addr1)) {
635 struct ipmi_lan_addr *lan_addr1
636 = (struct ipmi_lan_addr *) addr1;
637 struct ipmi_lan_addr *lan_addr2
638 = (struct ipmi_lan_addr *) addr2;
640 return ((lan_addr1->remote_SWID == lan_addr2->remote_SWID)
641 && (lan_addr1->local_SWID == lan_addr2->local_SWID)
642 && (lan_addr1->session_handle
643 == lan_addr2->session_handle)
644 && (lan_addr1->lun == lan_addr2->lun));
647 return 1;
650 int ipmi_validate_addr(struct ipmi_addr *addr, int len)
652 if (len < sizeof(struct ipmi_system_interface_addr))
653 return -EINVAL;
655 if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
656 if (addr->channel != IPMI_BMC_CHANNEL)
657 return -EINVAL;
658 return 0;
661 if ((addr->channel == IPMI_BMC_CHANNEL)
662 || (addr->channel >= IPMI_MAX_CHANNELS)
663 || (addr->channel < 0))
664 return -EINVAL;
666 if (is_ipmb_addr(addr) || is_ipmb_bcast_addr(addr)) {
667 if (len < sizeof(struct ipmi_ipmb_addr))
668 return -EINVAL;
669 return 0;
672 if (is_lan_addr(addr)) {
673 if (len < sizeof(struct ipmi_lan_addr))
674 return -EINVAL;
675 return 0;
678 return -EINVAL;
680 EXPORT_SYMBOL(ipmi_validate_addr);
682 unsigned int ipmi_addr_length(int addr_type)
684 if (addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
685 return sizeof(struct ipmi_system_interface_addr);
687 if ((addr_type == IPMI_IPMB_ADDR_TYPE)
688 || (addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE))
689 return sizeof(struct ipmi_ipmb_addr);
691 if (addr_type == IPMI_LAN_ADDR_TYPE)
692 return sizeof(struct ipmi_lan_addr);
694 return 0;
696 EXPORT_SYMBOL(ipmi_addr_length);
698 static void deliver_response(struct ipmi_recv_msg *msg)
700 if (!msg->user) {
701 ipmi_smi_t intf = msg->user_msg_data;
703 /* Special handling for NULL users. */
704 if (intf->null_user_handler) {
705 intf->null_user_handler(intf, msg);
706 ipmi_inc_stat(intf, handled_local_responses);
707 } else {
708 /* No handler, so give up. */
709 ipmi_inc_stat(intf, unhandled_local_responses);
711 ipmi_free_recv_msg(msg);
712 } else {
713 ipmi_user_t user = msg->user;
714 user->handler->ipmi_recv_hndl(msg, user->handler_data);
718 static void
719 deliver_err_response(struct ipmi_recv_msg *msg, int err)
721 msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
722 msg->msg_data[0] = err;
723 msg->msg.netfn |= 1; /* Convert to a response. */
724 msg->msg.data_len = 1;
725 msg->msg.data = msg->msg_data;
726 deliver_response(msg);
730 * Find the next sequence number not being used and add the given
731 * message with the given timeout to the sequence table. This must be
732 * called with the interface's seq_lock held.
734 static int intf_next_seq(ipmi_smi_t intf,
735 struct ipmi_recv_msg *recv_msg,
736 unsigned long timeout,
737 int retries,
738 int broadcast,
739 unsigned char *seq,
740 long *seqid)
742 int rv = 0;
743 unsigned int i;
745 for (i = intf->curr_seq; (i+1)%IPMI_IPMB_NUM_SEQ != intf->curr_seq;
746 i = (i+1)%IPMI_IPMB_NUM_SEQ) {
747 if (!intf->seq_table[i].inuse)
748 break;
751 if (!intf->seq_table[i].inuse) {
752 intf->seq_table[i].recv_msg = recv_msg;
755 * Start with the maximum timeout, when the send response
756 * comes in we will start the real timer.
758 intf->seq_table[i].timeout = MAX_MSG_TIMEOUT;
759 intf->seq_table[i].orig_timeout = timeout;
760 intf->seq_table[i].retries_left = retries;
761 intf->seq_table[i].broadcast = broadcast;
762 intf->seq_table[i].inuse = 1;
763 intf->seq_table[i].seqid = NEXT_SEQID(intf->seq_table[i].seqid);
764 *seq = i;
765 *seqid = intf->seq_table[i].seqid;
766 intf->curr_seq = (i+1)%IPMI_IPMB_NUM_SEQ;
767 } else {
768 rv = -EAGAIN;
771 return rv;
775 * Return the receive message for the given sequence number and
776 * release the sequence number so it can be reused. Some other data
777 * is passed in to be sure the message matches up correctly (to help
778 * guard against message coming in after their timeout and the
779 * sequence number being reused).
781 static int intf_find_seq(ipmi_smi_t intf,
782 unsigned char seq,
783 short channel,
784 unsigned char cmd,
785 unsigned char netfn,
786 struct ipmi_addr *addr,
787 struct ipmi_recv_msg **recv_msg)
789 int rv = -ENODEV;
790 unsigned long flags;
792 if (seq >= IPMI_IPMB_NUM_SEQ)
793 return -EINVAL;
795 spin_lock_irqsave(&(intf->seq_lock), flags);
796 if (intf->seq_table[seq].inuse) {
797 struct ipmi_recv_msg *msg = intf->seq_table[seq].recv_msg;
799 if ((msg->addr.channel == channel) && (msg->msg.cmd == cmd)
800 && (msg->msg.netfn == netfn)
801 && (ipmi_addr_equal(addr, &(msg->addr)))) {
802 *recv_msg = msg;
803 intf->seq_table[seq].inuse = 0;
804 rv = 0;
807 spin_unlock_irqrestore(&(intf->seq_lock), flags);
809 return rv;
813 /* Start the timer for a specific sequence table entry. */
814 static int intf_start_seq_timer(ipmi_smi_t intf,
815 long msgid)
817 int rv = -ENODEV;
818 unsigned long flags;
819 unsigned char seq;
820 unsigned long seqid;
823 GET_SEQ_FROM_MSGID(msgid, seq, seqid);
825 spin_lock_irqsave(&(intf->seq_lock), flags);
827 * We do this verification because the user can be deleted
828 * while a message is outstanding.
830 if ((intf->seq_table[seq].inuse)
831 && (intf->seq_table[seq].seqid == seqid)) {
832 struct seq_table *ent = &(intf->seq_table[seq]);
833 ent->timeout = ent->orig_timeout;
834 rv = 0;
836 spin_unlock_irqrestore(&(intf->seq_lock), flags);
838 return rv;
841 /* Got an error for the send message for a specific sequence number. */
842 static int intf_err_seq(ipmi_smi_t intf,
843 long msgid,
844 unsigned int err)
846 int rv = -ENODEV;
847 unsigned long flags;
848 unsigned char seq;
849 unsigned long seqid;
850 struct ipmi_recv_msg *msg = NULL;
853 GET_SEQ_FROM_MSGID(msgid, seq, seqid);
855 spin_lock_irqsave(&(intf->seq_lock), flags);
857 * We do this verification because the user can be deleted
858 * while a message is outstanding.
860 if ((intf->seq_table[seq].inuse)
861 && (intf->seq_table[seq].seqid == seqid)) {
862 struct seq_table *ent = &(intf->seq_table[seq]);
864 ent->inuse = 0;
865 msg = ent->recv_msg;
866 rv = 0;
868 spin_unlock_irqrestore(&(intf->seq_lock), flags);
870 if (msg)
871 deliver_err_response(msg, err);
873 return rv;
877 int ipmi_create_user(unsigned int if_num,
878 struct ipmi_user_hndl *handler,
879 void *handler_data,
880 ipmi_user_t *user)
882 unsigned long flags;
883 ipmi_user_t new_user;
884 int rv = 0;
885 ipmi_smi_t intf;
888 * There is no module usecount here, because it's not
889 * required. Since this can only be used by and called from
890 * other modules, they will implicitly use this module, and
891 * thus this can't be removed unless the other modules are
892 * removed.
895 if (handler == NULL)
896 return -EINVAL;
899 * Make sure the driver is actually initialized, this handles
900 * problems with initialization order.
902 if (!initialized) {
903 rv = ipmi_init_msghandler();
904 if (rv)
905 return rv;
908 * The init code doesn't return an error if it was turned
909 * off, but it won't initialize. Check that.
911 if (!initialized)
912 return -ENODEV;
915 new_user = kmalloc(sizeof(*new_user), GFP_KERNEL);
916 if (!new_user)
917 return -ENOMEM;
919 mutex_lock(&ipmi_interfaces_mutex);
920 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
921 if (intf->intf_num == if_num)
922 goto found;
924 /* Not found, return an error */
925 rv = -EINVAL;
926 goto out_kfree;
928 found:
929 /* Note that each existing user holds a refcount to the interface. */
930 kref_get(&intf->refcount);
932 kref_init(&new_user->refcount);
933 new_user->handler = handler;
934 new_user->handler_data = handler_data;
935 new_user->intf = intf;
936 new_user->gets_events = 0;
938 if (!try_module_get(intf->handlers->owner)) {
939 rv = -ENODEV;
940 goto out_kref;
943 if (intf->handlers->inc_usecount) {
944 rv = intf->handlers->inc_usecount(intf->send_info);
945 if (rv) {
946 module_put(intf->handlers->owner);
947 goto out_kref;
952 * Hold the lock so intf->handlers is guaranteed to be good
953 * until now
955 mutex_unlock(&ipmi_interfaces_mutex);
957 new_user->valid = 1;
958 spin_lock_irqsave(&intf->seq_lock, flags);
959 list_add_rcu(&new_user->link, &intf->users);
960 spin_unlock_irqrestore(&intf->seq_lock, flags);
961 *user = new_user;
962 return 0;
964 out_kref:
965 kref_put(&intf->refcount, intf_free);
966 out_kfree:
967 mutex_unlock(&ipmi_interfaces_mutex);
968 kfree(new_user);
969 return rv;
971 EXPORT_SYMBOL(ipmi_create_user);
973 static void free_user(struct kref *ref)
975 ipmi_user_t user = container_of(ref, struct ipmi_user, refcount);
976 kfree(user);
979 int ipmi_destroy_user(ipmi_user_t user)
981 ipmi_smi_t intf = user->intf;
982 int i;
983 unsigned long flags;
984 struct cmd_rcvr *rcvr;
985 struct cmd_rcvr *rcvrs = NULL;
987 user->valid = 0;
989 /* Remove the user from the interface's sequence table. */
990 spin_lock_irqsave(&intf->seq_lock, flags);
991 list_del_rcu(&user->link);
993 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
994 if (intf->seq_table[i].inuse
995 && (intf->seq_table[i].recv_msg->user == user)) {
996 intf->seq_table[i].inuse = 0;
997 ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
1000 spin_unlock_irqrestore(&intf->seq_lock, flags);
1003 * Remove the user from the command receiver's table. First
1004 * we build a list of everything (not using the standard link,
1005 * since other things may be using it till we do
1006 * synchronize_rcu()) then free everything in that list.
1008 mutex_lock(&intf->cmd_rcvrs_mutex);
1009 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
1010 if (rcvr->user == user) {
1011 list_del_rcu(&rcvr->link);
1012 rcvr->next = rcvrs;
1013 rcvrs = rcvr;
1016 mutex_unlock(&intf->cmd_rcvrs_mutex);
1017 synchronize_rcu();
1018 while (rcvrs) {
1019 rcvr = rcvrs;
1020 rcvrs = rcvr->next;
1021 kfree(rcvr);
1024 mutex_lock(&ipmi_interfaces_mutex);
1025 if (intf->handlers) {
1026 module_put(intf->handlers->owner);
1027 if (intf->handlers->dec_usecount)
1028 intf->handlers->dec_usecount(intf->send_info);
1030 mutex_unlock(&ipmi_interfaces_mutex);
1032 kref_put(&intf->refcount, intf_free);
1034 kref_put(&user->refcount, free_user);
1036 return 0;
1038 EXPORT_SYMBOL(ipmi_destroy_user);
1040 void ipmi_get_version(ipmi_user_t user,
1041 unsigned char *major,
1042 unsigned char *minor)
1044 *major = user->intf->ipmi_version_major;
1045 *minor = user->intf->ipmi_version_minor;
1047 EXPORT_SYMBOL(ipmi_get_version);
1049 int ipmi_set_my_address(ipmi_user_t user,
1050 unsigned int channel,
1051 unsigned char address)
1053 if (channel >= IPMI_MAX_CHANNELS)
1054 return -EINVAL;
1055 user->intf->channels[channel].address = address;
1056 return 0;
1058 EXPORT_SYMBOL(ipmi_set_my_address);
1060 int ipmi_get_my_address(ipmi_user_t user,
1061 unsigned int channel,
1062 unsigned char *address)
1064 if (channel >= IPMI_MAX_CHANNELS)
1065 return -EINVAL;
1066 *address = user->intf->channels[channel].address;
1067 return 0;
1069 EXPORT_SYMBOL(ipmi_get_my_address);
1071 int ipmi_set_my_LUN(ipmi_user_t user,
1072 unsigned int channel,
1073 unsigned char LUN)
1075 if (channel >= IPMI_MAX_CHANNELS)
1076 return -EINVAL;
1077 user->intf->channels[channel].lun = LUN & 0x3;
1078 return 0;
1080 EXPORT_SYMBOL(ipmi_set_my_LUN);
1082 int ipmi_get_my_LUN(ipmi_user_t user,
1083 unsigned int channel,
1084 unsigned char *address)
1086 if (channel >= IPMI_MAX_CHANNELS)
1087 return -EINVAL;
1088 *address = user->intf->channels[channel].lun;
1089 return 0;
1091 EXPORT_SYMBOL(ipmi_get_my_LUN);
1093 int ipmi_get_maintenance_mode(ipmi_user_t user)
1095 int mode;
1096 unsigned long flags;
1098 spin_lock_irqsave(&user->intf->maintenance_mode_lock, flags);
1099 mode = user->intf->maintenance_mode;
1100 spin_unlock_irqrestore(&user->intf->maintenance_mode_lock, flags);
1102 return mode;
1104 EXPORT_SYMBOL(ipmi_get_maintenance_mode);
1106 static void maintenance_mode_update(ipmi_smi_t intf)
1108 if (intf->handlers->set_maintenance_mode)
1109 intf->handlers->set_maintenance_mode(
1110 intf->send_info, intf->maintenance_mode_enable);
1113 int ipmi_set_maintenance_mode(ipmi_user_t user, int mode)
1115 int rv = 0;
1116 unsigned long flags;
1117 ipmi_smi_t intf = user->intf;
1119 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
1120 if (intf->maintenance_mode != mode) {
1121 switch (mode) {
1122 case IPMI_MAINTENANCE_MODE_AUTO:
1123 intf->maintenance_mode = mode;
1124 intf->maintenance_mode_enable
1125 = (intf->auto_maintenance_timeout > 0);
1126 break;
1128 case IPMI_MAINTENANCE_MODE_OFF:
1129 intf->maintenance_mode = mode;
1130 intf->maintenance_mode_enable = 0;
1131 break;
1133 case IPMI_MAINTENANCE_MODE_ON:
1134 intf->maintenance_mode = mode;
1135 intf->maintenance_mode_enable = 1;
1136 break;
1138 default:
1139 rv = -EINVAL;
1140 goto out_unlock;
1143 maintenance_mode_update(intf);
1145 out_unlock:
1146 spin_unlock_irqrestore(&intf->maintenance_mode_lock, flags);
1148 return rv;
1150 EXPORT_SYMBOL(ipmi_set_maintenance_mode);
1152 int ipmi_set_gets_events(ipmi_user_t user, int val)
1154 unsigned long flags;
1155 ipmi_smi_t intf = user->intf;
1156 struct ipmi_recv_msg *msg, *msg2;
1157 struct list_head msgs;
1159 INIT_LIST_HEAD(&msgs);
1161 spin_lock_irqsave(&intf->events_lock, flags);
1162 user->gets_events = val;
1164 if (intf->delivering_events)
1166 * Another thread is delivering events for this, so
1167 * let it handle any new events.
1169 goto out;
1171 /* Deliver any queued events. */
1172 while (user->gets_events && !list_empty(&intf->waiting_events)) {
1173 list_for_each_entry_safe(msg, msg2, &intf->waiting_events, link)
1174 list_move_tail(&msg->link, &msgs);
1175 intf->waiting_events_count = 0;
1176 if (intf->event_msg_printed) {
1177 printk(KERN_WARNING PFX "Event queue no longer"
1178 " full\n");
1179 intf->event_msg_printed = 0;
1182 intf->delivering_events = 1;
1183 spin_unlock_irqrestore(&intf->events_lock, flags);
1185 list_for_each_entry_safe(msg, msg2, &msgs, link) {
1186 msg->user = user;
1187 kref_get(&user->refcount);
1188 deliver_response(msg);
1191 spin_lock_irqsave(&intf->events_lock, flags);
1192 intf->delivering_events = 0;
1195 out:
1196 spin_unlock_irqrestore(&intf->events_lock, flags);
1198 return 0;
1200 EXPORT_SYMBOL(ipmi_set_gets_events);
1202 static struct cmd_rcvr *find_cmd_rcvr(ipmi_smi_t intf,
1203 unsigned char netfn,
1204 unsigned char cmd,
1205 unsigned char chan)
1207 struct cmd_rcvr *rcvr;
1209 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
1210 if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)
1211 && (rcvr->chans & (1 << chan)))
1212 return rcvr;
1214 return NULL;
1217 static int is_cmd_rcvr_exclusive(ipmi_smi_t intf,
1218 unsigned char netfn,
1219 unsigned char cmd,
1220 unsigned int chans)
1222 struct cmd_rcvr *rcvr;
1224 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
1225 if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)
1226 && (rcvr->chans & chans))
1227 return 0;
1229 return 1;
1232 int ipmi_register_for_cmd(ipmi_user_t user,
1233 unsigned char netfn,
1234 unsigned char cmd,
1235 unsigned int chans)
1237 ipmi_smi_t intf = user->intf;
1238 struct cmd_rcvr *rcvr;
1239 int rv = 0;
1242 rcvr = kmalloc(sizeof(*rcvr), GFP_KERNEL);
1243 if (!rcvr)
1244 return -ENOMEM;
1245 rcvr->cmd = cmd;
1246 rcvr->netfn = netfn;
1247 rcvr->chans = chans;
1248 rcvr->user = user;
1250 mutex_lock(&intf->cmd_rcvrs_mutex);
1251 /* Make sure the command/netfn is not already registered. */
1252 if (!is_cmd_rcvr_exclusive(intf, netfn, cmd, chans)) {
1253 rv = -EBUSY;
1254 goto out_unlock;
1257 list_add_rcu(&rcvr->link, &intf->cmd_rcvrs);
1259 out_unlock:
1260 mutex_unlock(&intf->cmd_rcvrs_mutex);
1261 if (rv)
1262 kfree(rcvr);
1264 return rv;
1266 EXPORT_SYMBOL(ipmi_register_for_cmd);
1268 int ipmi_unregister_for_cmd(ipmi_user_t user,
1269 unsigned char netfn,
1270 unsigned char cmd,
1271 unsigned int chans)
1273 ipmi_smi_t intf = user->intf;
1274 struct cmd_rcvr *rcvr;
1275 struct cmd_rcvr *rcvrs = NULL;
1276 int i, rv = -ENOENT;
1278 mutex_lock(&intf->cmd_rcvrs_mutex);
1279 for (i = 0; i < IPMI_NUM_CHANNELS; i++) {
1280 if (((1 << i) & chans) == 0)
1281 continue;
1282 rcvr = find_cmd_rcvr(intf, netfn, cmd, i);
1283 if (rcvr == NULL)
1284 continue;
1285 if (rcvr->user == user) {
1286 rv = 0;
1287 rcvr->chans &= ~chans;
1288 if (rcvr->chans == 0) {
1289 list_del_rcu(&rcvr->link);
1290 rcvr->next = rcvrs;
1291 rcvrs = rcvr;
1295 mutex_unlock(&intf->cmd_rcvrs_mutex);
1296 synchronize_rcu();
1297 while (rcvrs) {
1298 rcvr = rcvrs;
1299 rcvrs = rcvr->next;
1300 kfree(rcvr);
1302 return rv;
1304 EXPORT_SYMBOL(ipmi_unregister_for_cmd);
1306 static unsigned char
1307 ipmb_checksum(unsigned char *data, int size)
1309 unsigned char csum = 0;
1311 for (; size > 0; size--, data++)
1312 csum += *data;
1314 return -csum;
1317 static inline void format_ipmb_msg(struct ipmi_smi_msg *smi_msg,
1318 struct kernel_ipmi_msg *msg,
1319 struct ipmi_ipmb_addr *ipmb_addr,
1320 long msgid,
1321 unsigned char ipmb_seq,
1322 int broadcast,
1323 unsigned char source_address,
1324 unsigned char source_lun)
1326 int i = broadcast;
1328 /* Format the IPMB header data. */
1329 smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
1330 smi_msg->data[1] = IPMI_SEND_MSG_CMD;
1331 smi_msg->data[2] = ipmb_addr->channel;
1332 if (broadcast)
1333 smi_msg->data[3] = 0;
1334 smi_msg->data[i+3] = ipmb_addr->slave_addr;
1335 smi_msg->data[i+4] = (msg->netfn << 2) | (ipmb_addr->lun & 0x3);
1336 smi_msg->data[i+5] = ipmb_checksum(&(smi_msg->data[i+3]), 2);
1337 smi_msg->data[i+6] = source_address;
1338 smi_msg->data[i+7] = (ipmb_seq << 2) | source_lun;
1339 smi_msg->data[i+8] = msg->cmd;
1341 /* Now tack on the data to the message. */
1342 if (msg->data_len > 0)
1343 memcpy(&(smi_msg->data[i+9]), msg->data,
1344 msg->data_len);
1345 smi_msg->data_size = msg->data_len + 9;
1347 /* Now calculate the checksum and tack it on. */
1348 smi_msg->data[i+smi_msg->data_size]
1349 = ipmb_checksum(&(smi_msg->data[i+6]),
1350 smi_msg->data_size-6);
1353 * Add on the checksum size and the offset from the
1354 * broadcast.
1356 smi_msg->data_size += 1 + i;
1358 smi_msg->msgid = msgid;
1361 static inline void format_lan_msg(struct ipmi_smi_msg *smi_msg,
1362 struct kernel_ipmi_msg *msg,
1363 struct ipmi_lan_addr *lan_addr,
1364 long msgid,
1365 unsigned char ipmb_seq,
1366 unsigned char source_lun)
1368 /* Format the IPMB header data. */
1369 smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
1370 smi_msg->data[1] = IPMI_SEND_MSG_CMD;
1371 smi_msg->data[2] = lan_addr->channel;
1372 smi_msg->data[3] = lan_addr->session_handle;
1373 smi_msg->data[4] = lan_addr->remote_SWID;
1374 smi_msg->data[5] = (msg->netfn << 2) | (lan_addr->lun & 0x3);
1375 smi_msg->data[6] = ipmb_checksum(&(smi_msg->data[4]), 2);
1376 smi_msg->data[7] = lan_addr->local_SWID;
1377 smi_msg->data[8] = (ipmb_seq << 2) | source_lun;
1378 smi_msg->data[9] = msg->cmd;
1380 /* Now tack on the data to the message. */
1381 if (msg->data_len > 0)
1382 memcpy(&(smi_msg->data[10]), msg->data,
1383 msg->data_len);
1384 smi_msg->data_size = msg->data_len + 10;
1386 /* Now calculate the checksum and tack it on. */
1387 smi_msg->data[smi_msg->data_size]
1388 = ipmb_checksum(&(smi_msg->data[7]),
1389 smi_msg->data_size-7);
1392 * Add on the checksum size and the offset from the
1393 * broadcast.
1395 smi_msg->data_size += 1;
1397 smi_msg->msgid = msgid;
1401 * Separate from ipmi_request so that the user does not have to be
1402 * supplied in certain circumstances (mainly at panic time). If
1403 * messages are supplied, they will be freed, even if an error
1404 * occurs.
1406 static int i_ipmi_request(ipmi_user_t user,
1407 ipmi_smi_t intf,
1408 struct ipmi_addr *addr,
1409 long msgid,
1410 struct kernel_ipmi_msg *msg,
1411 void *user_msg_data,
1412 void *supplied_smi,
1413 struct ipmi_recv_msg *supplied_recv,
1414 int priority,
1415 unsigned char source_address,
1416 unsigned char source_lun,
1417 int retries,
1418 unsigned int retry_time_ms)
1420 int rv = 0;
1421 struct ipmi_smi_msg *smi_msg;
1422 struct ipmi_recv_msg *recv_msg;
1423 unsigned long flags;
1424 struct ipmi_smi_handlers *handlers;
1427 if (supplied_recv)
1428 recv_msg = supplied_recv;
1429 else {
1430 recv_msg = ipmi_alloc_recv_msg();
1431 if (recv_msg == NULL)
1432 return -ENOMEM;
1434 recv_msg->user_msg_data = user_msg_data;
1436 if (supplied_smi)
1437 smi_msg = (struct ipmi_smi_msg *) supplied_smi;
1438 else {
1439 smi_msg = ipmi_alloc_smi_msg();
1440 if (smi_msg == NULL) {
1441 ipmi_free_recv_msg(recv_msg);
1442 return -ENOMEM;
1446 rcu_read_lock();
1447 handlers = intf->handlers;
1448 if (!handlers) {
1449 rv = -ENODEV;
1450 goto out_err;
1453 recv_msg->user = user;
1454 if (user)
1455 kref_get(&user->refcount);
1456 recv_msg->msgid = msgid;
1458 * Store the message to send in the receive message so timeout
1459 * responses can get the proper response data.
1461 recv_msg->msg = *msg;
1463 if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
1464 struct ipmi_system_interface_addr *smi_addr;
1466 if (msg->netfn & 1) {
1467 /* Responses are not allowed to the SMI. */
1468 rv = -EINVAL;
1469 goto out_err;
1472 smi_addr = (struct ipmi_system_interface_addr *) addr;
1473 if (smi_addr->lun > 3) {
1474 ipmi_inc_stat(intf, sent_invalid_commands);
1475 rv = -EINVAL;
1476 goto out_err;
1479 memcpy(&recv_msg->addr, smi_addr, sizeof(*smi_addr));
1481 if ((msg->netfn == IPMI_NETFN_APP_REQUEST)
1482 && ((msg->cmd == IPMI_SEND_MSG_CMD)
1483 || (msg->cmd == IPMI_GET_MSG_CMD)
1484 || (msg->cmd == IPMI_READ_EVENT_MSG_BUFFER_CMD))) {
1486 * We don't let the user do these, since we manage
1487 * the sequence numbers.
1489 ipmi_inc_stat(intf, sent_invalid_commands);
1490 rv = -EINVAL;
1491 goto out_err;
1494 if (((msg->netfn == IPMI_NETFN_APP_REQUEST)
1495 && ((msg->cmd == IPMI_COLD_RESET_CMD)
1496 || (msg->cmd == IPMI_WARM_RESET_CMD)))
1497 || (msg->netfn == IPMI_NETFN_FIRMWARE_REQUEST)) {
1498 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
1499 intf->auto_maintenance_timeout
1500 = IPMI_MAINTENANCE_MODE_TIMEOUT;
1501 if (!intf->maintenance_mode
1502 && !intf->maintenance_mode_enable) {
1503 intf->maintenance_mode_enable = 1;
1504 maintenance_mode_update(intf);
1506 spin_unlock_irqrestore(&intf->maintenance_mode_lock,
1507 flags);
1510 if ((msg->data_len + 2) > IPMI_MAX_MSG_LENGTH) {
1511 ipmi_inc_stat(intf, sent_invalid_commands);
1512 rv = -EMSGSIZE;
1513 goto out_err;
1516 smi_msg->data[0] = (msg->netfn << 2) | (smi_addr->lun & 0x3);
1517 smi_msg->data[1] = msg->cmd;
1518 smi_msg->msgid = msgid;
1519 smi_msg->user_data = recv_msg;
1520 if (msg->data_len > 0)
1521 memcpy(&(smi_msg->data[2]), msg->data, msg->data_len);
1522 smi_msg->data_size = msg->data_len + 2;
1523 ipmi_inc_stat(intf, sent_local_commands);
1524 } else if (is_ipmb_addr(addr) || is_ipmb_bcast_addr(addr)) {
1525 struct ipmi_ipmb_addr *ipmb_addr;
1526 unsigned char ipmb_seq;
1527 long seqid;
1528 int broadcast = 0;
1530 if (addr->channel >= IPMI_MAX_CHANNELS) {
1531 ipmi_inc_stat(intf, sent_invalid_commands);
1532 rv = -EINVAL;
1533 goto out_err;
1536 if (intf->channels[addr->channel].medium
1537 != IPMI_CHANNEL_MEDIUM_IPMB) {
1538 ipmi_inc_stat(intf, sent_invalid_commands);
1539 rv = -EINVAL;
1540 goto out_err;
1543 if (retries < 0) {
1544 if (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE)
1545 retries = 0; /* Don't retry broadcasts. */
1546 else
1547 retries = 4;
1549 if (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE) {
1551 * Broadcasts add a zero at the beginning of the
1552 * message, but otherwise is the same as an IPMB
1553 * address.
1555 addr->addr_type = IPMI_IPMB_ADDR_TYPE;
1556 broadcast = 1;
1560 /* Default to 1 second retries. */
1561 if (retry_time_ms == 0)
1562 retry_time_ms = 1000;
1565 * 9 for the header and 1 for the checksum, plus
1566 * possibly one for the broadcast.
1568 if ((msg->data_len + 10 + broadcast) > IPMI_MAX_MSG_LENGTH) {
1569 ipmi_inc_stat(intf, sent_invalid_commands);
1570 rv = -EMSGSIZE;
1571 goto out_err;
1574 ipmb_addr = (struct ipmi_ipmb_addr *) addr;
1575 if (ipmb_addr->lun > 3) {
1576 ipmi_inc_stat(intf, sent_invalid_commands);
1577 rv = -EINVAL;
1578 goto out_err;
1581 memcpy(&recv_msg->addr, ipmb_addr, sizeof(*ipmb_addr));
1583 if (recv_msg->msg.netfn & 0x1) {
1585 * It's a response, so use the user's sequence
1586 * from msgid.
1588 ipmi_inc_stat(intf, sent_ipmb_responses);
1589 format_ipmb_msg(smi_msg, msg, ipmb_addr, msgid,
1590 msgid, broadcast,
1591 source_address, source_lun);
1594 * Save the receive message so we can use it
1595 * to deliver the response.
1597 smi_msg->user_data = recv_msg;
1598 } else {
1599 /* It's a command, so get a sequence for it. */
1601 spin_lock_irqsave(&(intf->seq_lock), flags);
1604 * Create a sequence number with a 1 second
1605 * timeout and 4 retries.
1607 rv = intf_next_seq(intf,
1608 recv_msg,
1609 retry_time_ms,
1610 retries,
1611 broadcast,
1612 &ipmb_seq,
1613 &seqid);
1614 if (rv) {
1616 * We have used up all the sequence numbers,
1617 * probably, so abort.
1619 spin_unlock_irqrestore(&(intf->seq_lock),
1620 flags);
1621 goto out_err;
1624 ipmi_inc_stat(intf, sent_ipmb_commands);
1627 * Store the sequence number in the message,
1628 * so that when the send message response
1629 * comes back we can start the timer.
1631 format_ipmb_msg(smi_msg, msg, ipmb_addr,
1632 STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
1633 ipmb_seq, broadcast,
1634 source_address, source_lun);
1637 * Copy the message into the recv message data, so we
1638 * can retransmit it later if necessary.
1640 memcpy(recv_msg->msg_data, smi_msg->data,
1641 smi_msg->data_size);
1642 recv_msg->msg.data = recv_msg->msg_data;
1643 recv_msg->msg.data_len = smi_msg->data_size;
1646 * We don't unlock until here, because we need
1647 * to copy the completed message into the
1648 * recv_msg before we release the lock.
1649 * Otherwise, race conditions may bite us. I
1650 * know that's pretty paranoid, but I prefer
1651 * to be correct.
1653 spin_unlock_irqrestore(&(intf->seq_lock), flags);
1655 } else if (is_lan_addr(addr)) {
1656 struct ipmi_lan_addr *lan_addr;
1657 unsigned char ipmb_seq;
1658 long seqid;
1660 if (addr->channel >= IPMI_MAX_CHANNELS) {
1661 ipmi_inc_stat(intf, sent_invalid_commands);
1662 rv = -EINVAL;
1663 goto out_err;
1666 if ((intf->channels[addr->channel].medium
1667 != IPMI_CHANNEL_MEDIUM_8023LAN)
1668 && (intf->channels[addr->channel].medium
1669 != IPMI_CHANNEL_MEDIUM_ASYNC)) {
1670 ipmi_inc_stat(intf, sent_invalid_commands);
1671 rv = -EINVAL;
1672 goto out_err;
1675 retries = 4;
1677 /* Default to 1 second retries. */
1678 if (retry_time_ms == 0)
1679 retry_time_ms = 1000;
1681 /* 11 for the header and 1 for the checksum. */
1682 if ((msg->data_len + 12) > IPMI_MAX_MSG_LENGTH) {
1683 ipmi_inc_stat(intf, sent_invalid_commands);
1684 rv = -EMSGSIZE;
1685 goto out_err;
1688 lan_addr = (struct ipmi_lan_addr *) addr;
1689 if (lan_addr->lun > 3) {
1690 ipmi_inc_stat(intf, sent_invalid_commands);
1691 rv = -EINVAL;
1692 goto out_err;
1695 memcpy(&recv_msg->addr, lan_addr, sizeof(*lan_addr));
1697 if (recv_msg->msg.netfn & 0x1) {
1699 * It's a response, so use the user's sequence
1700 * from msgid.
1702 ipmi_inc_stat(intf, sent_lan_responses);
1703 format_lan_msg(smi_msg, msg, lan_addr, msgid,
1704 msgid, source_lun);
1707 * Save the receive message so we can use it
1708 * to deliver the response.
1710 smi_msg->user_data = recv_msg;
1711 } else {
1712 /* It's a command, so get a sequence for it. */
1714 spin_lock_irqsave(&(intf->seq_lock), flags);
1717 * Create a sequence number with a 1 second
1718 * timeout and 4 retries.
1720 rv = intf_next_seq(intf,
1721 recv_msg,
1722 retry_time_ms,
1723 retries,
1725 &ipmb_seq,
1726 &seqid);
1727 if (rv) {
1729 * We have used up all the sequence numbers,
1730 * probably, so abort.
1732 spin_unlock_irqrestore(&(intf->seq_lock),
1733 flags);
1734 goto out_err;
1737 ipmi_inc_stat(intf, sent_lan_commands);
1740 * Store the sequence number in the message,
1741 * so that when the send message response
1742 * comes back we can start the timer.
1744 format_lan_msg(smi_msg, msg, lan_addr,
1745 STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
1746 ipmb_seq, source_lun);
1749 * Copy the message into the recv message data, so we
1750 * can retransmit it later if necessary.
1752 memcpy(recv_msg->msg_data, smi_msg->data,
1753 smi_msg->data_size);
1754 recv_msg->msg.data = recv_msg->msg_data;
1755 recv_msg->msg.data_len = smi_msg->data_size;
1758 * We don't unlock until here, because we need
1759 * to copy the completed message into the
1760 * recv_msg before we release the lock.
1761 * Otherwise, race conditions may bite us. I
1762 * know that's pretty paranoid, but I prefer
1763 * to be correct.
1765 spin_unlock_irqrestore(&(intf->seq_lock), flags);
1767 } else {
1768 /* Unknown address type. */
1769 ipmi_inc_stat(intf, sent_invalid_commands);
1770 rv = -EINVAL;
1771 goto out_err;
1774 #ifdef DEBUG_MSGING
1776 int m;
1777 for (m = 0; m < smi_msg->data_size; m++)
1778 printk(" %2.2x", smi_msg->data[m]);
1779 printk("\n");
1781 #endif
1783 handlers->sender(intf->send_info, smi_msg, priority);
1784 rcu_read_unlock();
1786 return 0;
1788 out_err:
1789 rcu_read_unlock();
1790 ipmi_free_smi_msg(smi_msg);
1791 ipmi_free_recv_msg(recv_msg);
1792 return rv;
1795 static int check_addr(ipmi_smi_t intf,
1796 struct ipmi_addr *addr,
1797 unsigned char *saddr,
1798 unsigned char *lun)
1800 if (addr->channel >= IPMI_MAX_CHANNELS)
1801 return -EINVAL;
1802 *lun = intf->channels[addr->channel].lun;
1803 *saddr = intf->channels[addr->channel].address;
1804 return 0;
1807 int ipmi_request_settime(ipmi_user_t user,
1808 struct ipmi_addr *addr,
1809 long msgid,
1810 struct kernel_ipmi_msg *msg,
1811 void *user_msg_data,
1812 int priority,
1813 int retries,
1814 unsigned int retry_time_ms)
1816 unsigned char saddr, lun;
1817 int rv;
1819 if (!user)
1820 return -EINVAL;
1821 rv = check_addr(user->intf, addr, &saddr, &lun);
1822 if (rv)
1823 return rv;
1824 return i_ipmi_request(user,
1825 user->intf,
1826 addr,
1827 msgid,
1828 msg,
1829 user_msg_data,
1830 NULL, NULL,
1831 priority,
1832 saddr,
1833 lun,
1834 retries,
1835 retry_time_ms);
1837 EXPORT_SYMBOL(ipmi_request_settime);
1839 int ipmi_request_supply_msgs(ipmi_user_t user,
1840 struct ipmi_addr *addr,
1841 long msgid,
1842 struct kernel_ipmi_msg *msg,
1843 void *user_msg_data,
1844 void *supplied_smi,
1845 struct ipmi_recv_msg *supplied_recv,
1846 int priority)
1848 unsigned char saddr, lun;
1849 int rv;
1851 if (!user)
1852 return -EINVAL;
1853 rv = check_addr(user->intf, addr, &saddr, &lun);
1854 if (rv)
1855 return rv;
1856 return i_ipmi_request(user,
1857 user->intf,
1858 addr,
1859 msgid,
1860 msg,
1861 user_msg_data,
1862 supplied_smi,
1863 supplied_recv,
1864 priority,
1865 saddr,
1866 lun,
1867 -1, 0);
1869 EXPORT_SYMBOL(ipmi_request_supply_msgs);
1871 #ifdef CONFIG_PROC_FS
1872 static int ipmb_file_read_proc(char *page, char **start, off_t off,
1873 int count, int *eof, void *data)
1875 char *out = (char *) page;
1876 ipmi_smi_t intf = data;
1877 int i;
1878 int rv = 0;
1880 for (i = 0; i < IPMI_MAX_CHANNELS; i++)
1881 rv += sprintf(out+rv, "%x ", intf->channels[i].address);
1882 out[rv-1] = '\n'; /* Replace the final space with a newline */
1883 out[rv] = '\0';
1884 rv++;
1885 return rv;
1888 static int version_file_read_proc(char *page, char **start, off_t off,
1889 int count, int *eof, void *data)
1891 char *out = (char *) page;
1892 ipmi_smi_t intf = data;
1894 return sprintf(out, "%u.%u\n",
1895 ipmi_version_major(&intf->bmc->id),
1896 ipmi_version_minor(&intf->bmc->id));
1899 static int stat_file_read_proc(char *page, char **start, off_t off,
1900 int count, int *eof, void *data)
1902 char *out = (char *) page;
1903 ipmi_smi_t intf = data;
1905 out += sprintf(out, "sent_invalid_commands: %u\n",
1906 ipmi_get_stat(intf, sent_invalid_commands));
1907 out += sprintf(out, "sent_local_commands: %u\n",
1908 ipmi_get_stat(intf, sent_local_commands));
1909 out += sprintf(out, "handled_local_responses: %u\n",
1910 ipmi_get_stat(intf, handled_local_responses));
1911 out += sprintf(out, "unhandled_local_responses: %u\n",
1912 ipmi_get_stat(intf, unhandled_local_responses));
1913 out += sprintf(out, "sent_ipmb_commands: %u\n",
1914 ipmi_get_stat(intf, sent_ipmb_commands));
1915 out += sprintf(out, "sent_ipmb_command_errs: %u\n",
1916 ipmi_get_stat(intf, sent_ipmb_command_errs));
1917 out += sprintf(out, "retransmitted_ipmb_commands: %u\n",
1918 ipmi_get_stat(intf, retransmitted_ipmb_commands));
1919 out += sprintf(out, "timed_out_ipmb_commands: %u\n",
1920 ipmi_get_stat(intf, timed_out_ipmb_commands));
1921 out += sprintf(out, "timed_out_ipmb_broadcasts: %u\n",
1922 ipmi_get_stat(intf, timed_out_ipmb_broadcasts));
1923 out += sprintf(out, "sent_ipmb_responses: %u\n",
1924 ipmi_get_stat(intf, sent_ipmb_responses));
1925 out += sprintf(out, "handled_ipmb_responses: %u\n",
1926 ipmi_get_stat(intf, handled_ipmb_responses));
1927 out += sprintf(out, "invalid_ipmb_responses: %u\n",
1928 ipmi_get_stat(intf, invalid_ipmb_responses));
1929 out += sprintf(out, "unhandled_ipmb_responses: %u\n",
1930 ipmi_get_stat(intf, unhandled_ipmb_responses));
1931 out += sprintf(out, "sent_lan_commands: %u\n",
1932 ipmi_get_stat(intf, sent_lan_commands));
1933 out += sprintf(out, "sent_lan_command_errs: %u\n",
1934 ipmi_get_stat(intf, sent_lan_command_errs));
1935 out += sprintf(out, "retransmitted_lan_commands: %u\n",
1936 ipmi_get_stat(intf, retransmitted_lan_commands));
1937 out += sprintf(out, "timed_out_lan_commands: %u\n",
1938 ipmi_get_stat(intf, timed_out_lan_commands));
1939 out += sprintf(out, "sent_lan_responses: %u\n",
1940 ipmi_get_stat(intf, sent_lan_responses));
1941 out += sprintf(out, "handled_lan_responses: %u\n",
1942 ipmi_get_stat(intf, handled_lan_responses));
1943 out += sprintf(out, "invalid_lan_responses: %u\n",
1944 ipmi_get_stat(intf, invalid_lan_responses));
1945 out += sprintf(out, "unhandled_lan_responses: %u\n",
1946 ipmi_get_stat(intf, unhandled_lan_responses));
1947 out += sprintf(out, "handled_commands: %u\n",
1948 ipmi_get_stat(intf, handled_commands));
1949 out += sprintf(out, "invalid_commands: %u\n",
1950 ipmi_get_stat(intf, invalid_commands));
1951 out += sprintf(out, "unhandled_commands: %u\n",
1952 ipmi_get_stat(intf, unhandled_commands));
1953 out += sprintf(out, "invalid_events: %u\n",
1954 ipmi_get_stat(intf, invalid_events));
1955 out += sprintf(out, "events: %u\n",
1956 ipmi_get_stat(intf, events));
1957 out += sprintf(out, "failed rexmit LAN msgs: %u\n",
1958 ipmi_get_stat(intf, dropped_rexmit_lan_commands));
1959 out += sprintf(out, "failed rexmit IPMB msgs: %u\n",
1960 ipmi_get_stat(intf, dropped_rexmit_ipmb_commands));
1962 return (out - ((char *) page));
1964 #endif /* CONFIG_PROC_FS */
1966 int ipmi_smi_add_proc_entry(ipmi_smi_t smi, char *name,
1967 read_proc_t *read_proc,
1968 void *data)
1970 int rv = 0;
1971 #ifdef CONFIG_PROC_FS
1972 struct proc_dir_entry *file;
1973 struct ipmi_proc_entry *entry;
1975 /* Create a list element. */
1976 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1977 if (!entry)
1978 return -ENOMEM;
1979 entry->name = kmalloc(strlen(name)+1, GFP_KERNEL);
1980 if (!entry->name) {
1981 kfree(entry);
1982 return -ENOMEM;
1984 strcpy(entry->name, name);
1986 file = create_proc_entry(name, 0, smi->proc_dir);
1987 if (!file) {
1988 kfree(entry->name);
1989 kfree(entry);
1990 rv = -ENOMEM;
1991 } else {
1992 file->data = data;
1993 file->read_proc = read_proc;
1995 mutex_lock(&smi->proc_entry_lock);
1996 /* Stick it on the list. */
1997 entry->next = smi->proc_entries;
1998 smi->proc_entries = entry;
1999 mutex_unlock(&smi->proc_entry_lock);
2001 #endif /* CONFIG_PROC_FS */
2003 return rv;
2005 EXPORT_SYMBOL(ipmi_smi_add_proc_entry);
2007 static int add_proc_entries(ipmi_smi_t smi, int num)
2009 int rv = 0;
2011 #ifdef CONFIG_PROC_FS
2012 sprintf(smi->proc_dir_name, "%d", num);
2013 smi->proc_dir = proc_mkdir(smi->proc_dir_name, proc_ipmi_root);
2014 if (!smi->proc_dir)
2015 rv = -ENOMEM;
2017 if (rv == 0)
2018 rv = ipmi_smi_add_proc_entry(smi, "stats",
2019 stat_file_read_proc,
2020 smi);
2022 if (rv == 0)
2023 rv = ipmi_smi_add_proc_entry(smi, "ipmb",
2024 ipmb_file_read_proc,
2025 smi);
2027 if (rv == 0)
2028 rv = ipmi_smi_add_proc_entry(smi, "version",
2029 version_file_read_proc,
2030 smi);
2031 #endif /* CONFIG_PROC_FS */
2033 return rv;
2036 static void remove_proc_entries(ipmi_smi_t smi)
2038 #ifdef CONFIG_PROC_FS
2039 struct ipmi_proc_entry *entry;
2041 mutex_lock(&smi->proc_entry_lock);
2042 while (smi->proc_entries) {
2043 entry = smi->proc_entries;
2044 smi->proc_entries = entry->next;
2046 remove_proc_entry(entry->name, smi->proc_dir);
2047 kfree(entry->name);
2048 kfree(entry);
2050 mutex_unlock(&smi->proc_entry_lock);
2051 remove_proc_entry(smi->proc_dir_name, proc_ipmi_root);
2052 #endif /* CONFIG_PROC_FS */
2055 static int __find_bmc_guid(struct device *dev, void *data)
2057 unsigned char *id = data;
2058 struct bmc_device *bmc = dev_get_drvdata(dev);
2059 return memcmp(bmc->guid, id, 16) == 0;
2062 static struct bmc_device *ipmi_find_bmc_guid(struct device_driver *drv,
2063 unsigned char *guid)
2065 struct device *dev;
2067 dev = driver_find_device(drv, NULL, guid, __find_bmc_guid);
2068 if (dev)
2069 return dev_get_drvdata(dev);
2070 else
2071 return NULL;
2074 struct prod_dev_id {
2075 unsigned int product_id;
2076 unsigned char device_id;
2079 static int __find_bmc_prod_dev_id(struct device *dev, void *data)
2081 struct prod_dev_id *id = data;
2082 struct bmc_device *bmc = dev_get_drvdata(dev);
2084 return (bmc->id.product_id == id->product_id
2085 && bmc->id.device_id == id->device_id);
2088 static struct bmc_device *ipmi_find_bmc_prod_dev_id(
2089 struct device_driver *drv,
2090 unsigned int product_id, unsigned char device_id)
2092 struct prod_dev_id id = {
2093 .product_id = product_id,
2094 .device_id = device_id,
2096 struct device *dev;
2098 dev = driver_find_device(drv, NULL, &id, __find_bmc_prod_dev_id);
2099 if (dev)
2100 return dev_get_drvdata(dev);
2101 else
2102 return NULL;
2105 static ssize_t device_id_show(struct device *dev,
2106 struct device_attribute *attr,
2107 char *buf)
2109 struct bmc_device *bmc = dev_get_drvdata(dev);
2111 return snprintf(buf, 10, "%u\n", bmc->id.device_id);
2114 static ssize_t provides_dev_sdrs_show(struct device *dev,
2115 struct device_attribute *attr,
2116 char *buf)
2118 struct bmc_device *bmc = dev_get_drvdata(dev);
2120 return snprintf(buf, 10, "%u\n",
2121 (bmc->id.device_revision & 0x80) >> 7);
2124 static ssize_t revision_show(struct device *dev, struct device_attribute *attr,
2125 char *buf)
2127 struct bmc_device *bmc = dev_get_drvdata(dev);
2129 return snprintf(buf, 20, "%u\n",
2130 bmc->id.device_revision & 0x0F);
2133 static ssize_t firmware_rev_show(struct device *dev,
2134 struct device_attribute *attr,
2135 char *buf)
2137 struct bmc_device *bmc = dev_get_drvdata(dev);
2139 return snprintf(buf, 20, "%u.%x\n", bmc->id.firmware_revision_1,
2140 bmc->id.firmware_revision_2);
2143 static ssize_t ipmi_version_show(struct device *dev,
2144 struct device_attribute *attr,
2145 char *buf)
2147 struct bmc_device *bmc = dev_get_drvdata(dev);
2149 return snprintf(buf, 20, "%u.%u\n",
2150 ipmi_version_major(&bmc->id),
2151 ipmi_version_minor(&bmc->id));
2154 static ssize_t add_dev_support_show(struct device *dev,
2155 struct device_attribute *attr,
2156 char *buf)
2158 struct bmc_device *bmc = dev_get_drvdata(dev);
2160 return snprintf(buf, 10, "0x%02x\n",
2161 bmc->id.additional_device_support);
2164 static ssize_t manufacturer_id_show(struct device *dev,
2165 struct device_attribute *attr,
2166 char *buf)
2168 struct bmc_device *bmc = dev_get_drvdata(dev);
2170 return snprintf(buf, 20, "0x%6.6x\n", bmc->id.manufacturer_id);
2173 static ssize_t product_id_show(struct device *dev,
2174 struct device_attribute *attr,
2175 char *buf)
2177 struct bmc_device *bmc = dev_get_drvdata(dev);
2179 return snprintf(buf, 10, "0x%4.4x\n", bmc->id.product_id);
2182 static ssize_t aux_firmware_rev_show(struct device *dev,
2183 struct device_attribute *attr,
2184 char *buf)
2186 struct bmc_device *bmc = dev_get_drvdata(dev);
2188 return snprintf(buf, 21, "0x%02x 0x%02x 0x%02x 0x%02x\n",
2189 bmc->id.aux_firmware_revision[3],
2190 bmc->id.aux_firmware_revision[2],
2191 bmc->id.aux_firmware_revision[1],
2192 bmc->id.aux_firmware_revision[0]);
2195 static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
2196 char *buf)
2198 struct bmc_device *bmc = dev_get_drvdata(dev);
2200 return snprintf(buf, 100, "%Lx%Lx\n",
2201 (long long) bmc->guid[0],
2202 (long long) bmc->guid[8]);
2205 static void remove_files(struct bmc_device *bmc)
2207 if (!bmc->dev)
2208 return;
2210 device_remove_file(&bmc->dev->dev,
2211 &bmc->device_id_attr);
2212 device_remove_file(&bmc->dev->dev,
2213 &bmc->provides_dev_sdrs_attr);
2214 device_remove_file(&bmc->dev->dev,
2215 &bmc->revision_attr);
2216 device_remove_file(&bmc->dev->dev,
2217 &bmc->firmware_rev_attr);
2218 device_remove_file(&bmc->dev->dev,
2219 &bmc->version_attr);
2220 device_remove_file(&bmc->dev->dev,
2221 &bmc->add_dev_support_attr);
2222 device_remove_file(&bmc->dev->dev,
2223 &bmc->manufacturer_id_attr);
2224 device_remove_file(&bmc->dev->dev,
2225 &bmc->product_id_attr);
2227 if (bmc->id.aux_firmware_revision_set)
2228 device_remove_file(&bmc->dev->dev,
2229 &bmc->aux_firmware_rev_attr);
2230 if (bmc->guid_set)
2231 device_remove_file(&bmc->dev->dev,
2232 &bmc->guid_attr);
2235 static void
2236 cleanup_bmc_device(struct kref *ref)
2238 struct bmc_device *bmc;
2240 bmc = container_of(ref, struct bmc_device, refcount);
2242 remove_files(bmc);
2243 platform_device_unregister(bmc->dev);
2244 kfree(bmc);
2247 static void ipmi_bmc_unregister(ipmi_smi_t intf)
2249 struct bmc_device *bmc = intf->bmc;
2251 if (intf->sysfs_name) {
2252 sysfs_remove_link(&intf->si_dev->kobj, intf->sysfs_name);
2253 kfree(intf->sysfs_name);
2254 intf->sysfs_name = NULL;
2256 if (intf->my_dev_name) {
2257 sysfs_remove_link(&bmc->dev->dev.kobj, intf->my_dev_name);
2258 kfree(intf->my_dev_name);
2259 intf->my_dev_name = NULL;
2262 mutex_lock(&ipmidriver_mutex);
2263 kref_put(&bmc->refcount, cleanup_bmc_device);
2264 intf->bmc = NULL;
2265 mutex_unlock(&ipmidriver_mutex);
2268 static int create_files(struct bmc_device *bmc)
2270 int err;
2272 bmc->device_id_attr.attr.name = "device_id";
2273 bmc->device_id_attr.attr.mode = S_IRUGO;
2274 bmc->device_id_attr.show = device_id_show;
2275 sysfs_attr_init(&bmc->device_id_attr.attr);
2277 bmc->provides_dev_sdrs_attr.attr.name = "provides_device_sdrs";
2278 bmc->provides_dev_sdrs_attr.attr.mode = S_IRUGO;
2279 bmc->provides_dev_sdrs_attr.show = provides_dev_sdrs_show;
2280 sysfs_attr_init(&bmc->provides_dev_sdrs_attr.attr);
2282 bmc->revision_attr.attr.name = "revision";
2283 bmc->revision_attr.attr.mode = S_IRUGO;
2284 bmc->revision_attr.show = revision_show;
2285 sysfs_attr_init(&bmc->revision_attr.attr);
2287 bmc->firmware_rev_attr.attr.name = "firmware_revision";
2288 bmc->firmware_rev_attr.attr.mode = S_IRUGO;
2289 bmc->firmware_rev_attr.show = firmware_rev_show;
2290 sysfs_attr_init(&bmc->firmware_rev_attr.attr);
2292 bmc->version_attr.attr.name = "ipmi_version";
2293 bmc->version_attr.attr.mode = S_IRUGO;
2294 bmc->version_attr.show = ipmi_version_show;
2295 sysfs_attr_init(&bmc->version_attr.attr);
2297 bmc->add_dev_support_attr.attr.name = "additional_device_support";
2298 bmc->add_dev_support_attr.attr.mode = S_IRUGO;
2299 bmc->add_dev_support_attr.show = add_dev_support_show;
2300 sysfs_attr_init(&bmc->add_dev_support_attr.attr);
2302 bmc->manufacturer_id_attr.attr.name = "manufacturer_id";
2303 bmc->manufacturer_id_attr.attr.mode = S_IRUGO;
2304 bmc->manufacturer_id_attr.show = manufacturer_id_show;
2305 sysfs_attr_init(&bmc->manufacturer_id_attr.attr);
2307 bmc->product_id_attr.attr.name = "product_id";
2308 bmc->product_id_attr.attr.mode = S_IRUGO;
2309 bmc->product_id_attr.show = product_id_show;
2310 sysfs_attr_init(&bmc->product_id_attr.attr);
2312 bmc->guid_attr.attr.name = "guid";
2313 bmc->guid_attr.attr.mode = S_IRUGO;
2314 bmc->guid_attr.show = guid_show;
2315 sysfs_attr_init(&bmc->guid_attr.attr);
2317 bmc->aux_firmware_rev_attr.attr.name = "aux_firmware_revision";
2318 bmc->aux_firmware_rev_attr.attr.mode = S_IRUGO;
2319 bmc->aux_firmware_rev_attr.show = aux_firmware_rev_show;
2320 sysfs_attr_init(&bmc->aux_firmware_rev_attr.attr);
2322 err = device_create_file(&bmc->dev->dev,
2323 &bmc->device_id_attr);
2324 if (err)
2325 goto out;
2326 err = device_create_file(&bmc->dev->dev,
2327 &bmc->provides_dev_sdrs_attr);
2328 if (err)
2329 goto out_devid;
2330 err = device_create_file(&bmc->dev->dev,
2331 &bmc->revision_attr);
2332 if (err)
2333 goto out_sdrs;
2334 err = device_create_file(&bmc->dev->dev,
2335 &bmc->firmware_rev_attr);
2336 if (err)
2337 goto out_rev;
2338 err = device_create_file(&bmc->dev->dev,
2339 &bmc->version_attr);
2340 if (err)
2341 goto out_firm;
2342 err = device_create_file(&bmc->dev->dev,
2343 &bmc->add_dev_support_attr);
2344 if (err)
2345 goto out_version;
2346 err = device_create_file(&bmc->dev->dev,
2347 &bmc->manufacturer_id_attr);
2348 if (err)
2349 goto out_add_dev;
2350 err = device_create_file(&bmc->dev->dev,
2351 &bmc->product_id_attr);
2352 if (err)
2353 goto out_manu;
2354 if (bmc->id.aux_firmware_revision_set) {
2355 err = device_create_file(&bmc->dev->dev,
2356 &bmc->aux_firmware_rev_attr);
2357 if (err)
2358 goto out_prod_id;
2360 if (bmc->guid_set) {
2361 err = device_create_file(&bmc->dev->dev,
2362 &bmc->guid_attr);
2363 if (err)
2364 goto out_aux_firm;
2367 return 0;
2369 out_aux_firm:
2370 if (bmc->id.aux_firmware_revision_set)
2371 device_remove_file(&bmc->dev->dev,
2372 &bmc->aux_firmware_rev_attr);
2373 out_prod_id:
2374 device_remove_file(&bmc->dev->dev,
2375 &bmc->product_id_attr);
2376 out_manu:
2377 device_remove_file(&bmc->dev->dev,
2378 &bmc->manufacturer_id_attr);
2379 out_add_dev:
2380 device_remove_file(&bmc->dev->dev,
2381 &bmc->add_dev_support_attr);
2382 out_version:
2383 device_remove_file(&bmc->dev->dev,
2384 &bmc->version_attr);
2385 out_firm:
2386 device_remove_file(&bmc->dev->dev,
2387 &bmc->firmware_rev_attr);
2388 out_rev:
2389 device_remove_file(&bmc->dev->dev,
2390 &bmc->revision_attr);
2391 out_sdrs:
2392 device_remove_file(&bmc->dev->dev,
2393 &bmc->provides_dev_sdrs_attr);
2394 out_devid:
2395 device_remove_file(&bmc->dev->dev,
2396 &bmc->device_id_attr);
2397 out:
2398 return err;
2401 static int ipmi_bmc_register(ipmi_smi_t intf, int ifnum,
2402 const char *sysfs_name)
2404 int rv;
2405 struct bmc_device *bmc = intf->bmc;
2406 struct bmc_device *old_bmc;
2407 int size;
2408 char dummy[1];
2410 mutex_lock(&ipmidriver_mutex);
2413 * Try to find if there is an bmc_device struct
2414 * representing the interfaced BMC already
2416 if (bmc->guid_set)
2417 old_bmc = ipmi_find_bmc_guid(&ipmidriver.driver, bmc->guid);
2418 else
2419 old_bmc = ipmi_find_bmc_prod_dev_id(&ipmidriver.driver,
2420 bmc->id.product_id,
2421 bmc->id.device_id);
2424 * If there is already an bmc_device, free the new one,
2425 * otherwise register the new BMC device
2427 if (old_bmc) {
2428 kfree(bmc);
2429 intf->bmc = old_bmc;
2430 bmc = old_bmc;
2432 kref_get(&bmc->refcount);
2433 mutex_unlock(&ipmidriver_mutex);
2435 printk(KERN_INFO
2436 "ipmi: interfacing existing BMC (man_id: 0x%6.6x,"
2437 " prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
2438 bmc->id.manufacturer_id,
2439 bmc->id.product_id,
2440 bmc->id.device_id);
2441 } else {
2442 char name[14];
2443 unsigned char orig_dev_id = bmc->id.device_id;
2444 int warn_printed = 0;
2446 snprintf(name, sizeof(name),
2447 "ipmi_bmc.%4.4x", bmc->id.product_id);
2449 while (ipmi_find_bmc_prod_dev_id(&ipmidriver.driver,
2450 bmc->id.product_id,
2451 bmc->id.device_id)) {
2452 if (!warn_printed) {
2453 printk(KERN_WARNING PFX
2454 "This machine has two different BMCs"
2455 " with the same product id and device"
2456 " id. This is an error in the"
2457 " firmware, but incrementing the"
2458 " device id to work around the problem."
2459 " Prod ID = 0x%x, Dev ID = 0x%x\n",
2460 bmc->id.product_id, bmc->id.device_id);
2461 warn_printed = 1;
2463 bmc->id.device_id++; /* Wraps at 255 */
2464 if (bmc->id.device_id == orig_dev_id) {
2465 printk(KERN_ERR PFX
2466 "Out of device ids!\n");
2467 break;
2471 bmc->dev = platform_device_alloc(name, bmc->id.device_id);
2472 if (!bmc->dev) {
2473 mutex_unlock(&ipmidriver_mutex);
2474 printk(KERN_ERR
2475 "ipmi_msghandler:"
2476 " Unable to allocate platform device\n");
2477 return -ENOMEM;
2479 bmc->dev->dev.driver = &ipmidriver.driver;
2480 dev_set_drvdata(&bmc->dev->dev, bmc);
2481 kref_init(&bmc->refcount);
2483 rv = platform_device_add(bmc->dev);
2484 mutex_unlock(&ipmidriver_mutex);
2485 if (rv) {
2486 platform_device_put(bmc->dev);
2487 bmc->dev = NULL;
2488 printk(KERN_ERR
2489 "ipmi_msghandler:"
2490 " Unable to register bmc device: %d\n",
2491 rv);
2493 * Don't go to out_err, you can only do that if
2494 * the device is registered already.
2496 return rv;
2499 rv = create_files(bmc);
2500 if (rv) {
2501 mutex_lock(&ipmidriver_mutex);
2502 platform_device_unregister(bmc->dev);
2503 mutex_unlock(&ipmidriver_mutex);
2505 return rv;
2508 printk(KERN_INFO
2509 "ipmi: Found new BMC (man_id: 0x%6.6x, "
2510 " prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
2511 bmc->id.manufacturer_id,
2512 bmc->id.product_id,
2513 bmc->id.device_id);
2517 * create symlink from system interface device to bmc device
2518 * and back.
2520 intf->sysfs_name = kstrdup(sysfs_name, GFP_KERNEL);
2521 if (!intf->sysfs_name) {
2522 rv = -ENOMEM;
2523 printk(KERN_ERR
2524 "ipmi_msghandler: allocate link to BMC: %d\n",
2525 rv);
2526 goto out_err;
2529 rv = sysfs_create_link(&intf->si_dev->kobj,
2530 &bmc->dev->dev.kobj, intf->sysfs_name);
2531 if (rv) {
2532 kfree(intf->sysfs_name);
2533 intf->sysfs_name = NULL;
2534 printk(KERN_ERR
2535 "ipmi_msghandler: Unable to create bmc symlink: %d\n",
2536 rv);
2537 goto out_err;
2540 size = snprintf(dummy, 0, "ipmi%d", ifnum);
2541 intf->my_dev_name = kmalloc(size+1, GFP_KERNEL);
2542 if (!intf->my_dev_name) {
2543 kfree(intf->sysfs_name);
2544 intf->sysfs_name = NULL;
2545 rv = -ENOMEM;
2546 printk(KERN_ERR
2547 "ipmi_msghandler: allocate link from BMC: %d\n",
2548 rv);
2549 goto out_err;
2551 snprintf(intf->my_dev_name, size+1, "ipmi%d", ifnum);
2553 rv = sysfs_create_link(&bmc->dev->dev.kobj, &intf->si_dev->kobj,
2554 intf->my_dev_name);
2555 if (rv) {
2556 kfree(intf->sysfs_name);
2557 intf->sysfs_name = NULL;
2558 kfree(intf->my_dev_name);
2559 intf->my_dev_name = NULL;
2560 printk(KERN_ERR
2561 "ipmi_msghandler:"
2562 " Unable to create symlink to bmc: %d\n",
2563 rv);
2564 goto out_err;
2567 return 0;
2569 out_err:
2570 ipmi_bmc_unregister(intf);
2571 return rv;
2574 static int
2575 send_guid_cmd(ipmi_smi_t intf, int chan)
2577 struct kernel_ipmi_msg msg;
2578 struct ipmi_system_interface_addr si;
2580 si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2581 si.channel = IPMI_BMC_CHANNEL;
2582 si.lun = 0;
2584 msg.netfn = IPMI_NETFN_APP_REQUEST;
2585 msg.cmd = IPMI_GET_DEVICE_GUID_CMD;
2586 msg.data = NULL;
2587 msg.data_len = 0;
2588 return i_ipmi_request(NULL,
2589 intf,
2590 (struct ipmi_addr *) &si,
2592 &msg,
2593 intf,
2594 NULL,
2595 NULL,
2597 intf->channels[0].address,
2598 intf->channels[0].lun,
2599 -1, 0);
2602 static void
2603 guid_handler(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
2605 if ((msg->addr.addr_type != IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
2606 || (msg->msg.netfn != IPMI_NETFN_APP_RESPONSE)
2607 || (msg->msg.cmd != IPMI_GET_DEVICE_GUID_CMD))
2608 /* Not for me */
2609 return;
2611 if (msg->msg.data[0] != 0) {
2612 /* Error from getting the GUID, the BMC doesn't have one. */
2613 intf->bmc->guid_set = 0;
2614 goto out;
2617 if (msg->msg.data_len < 17) {
2618 intf->bmc->guid_set = 0;
2619 printk(KERN_WARNING PFX
2620 "guid_handler: The GUID response from the BMC was too"
2621 " short, it was %d but should have been 17. Assuming"
2622 " GUID is not available.\n",
2623 msg->msg.data_len);
2624 goto out;
2627 memcpy(intf->bmc->guid, msg->msg.data, 16);
2628 intf->bmc->guid_set = 1;
2629 out:
2630 wake_up(&intf->waitq);
2633 static void
2634 get_guid(ipmi_smi_t intf)
2636 int rv;
2638 intf->bmc->guid_set = 0x2;
2639 intf->null_user_handler = guid_handler;
2640 rv = send_guid_cmd(intf, 0);
2641 if (rv)
2642 /* Send failed, no GUID available. */
2643 intf->bmc->guid_set = 0;
2644 wait_event(intf->waitq, intf->bmc->guid_set != 2);
2645 intf->null_user_handler = NULL;
2648 static int
2649 send_channel_info_cmd(ipmi_smi_t intf, int chan)
2651 struct kernel_ipmi_msg msg;
2652 unsigned char data[1];
2653 struct ipmi_system_interface_addr si;
2655 si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2656 si.channel = IPMI_BMC_CHANNEL;
2657 si.lun = 0;
2659 msg.netfn = IPMI_NETFN_APP_REQUEST;
2660 msg.cmd = IPMI_GET_CHANNEL_INFO_CMD;
2661 msg.data = data;
2662 msg.data_len = 1;
2663 data[0] = chan;
2664 return i_ipmi_request(NULL,
2665 intf,
2666 (struct ipmi_addr *) &si,
2668 &msg,
2669 intf,
2670 NULL,
2671 NULL,
2673 intf->channels[0].address,
2674 intf->channels[0].lun,
2675 -1, 0);
2678 static void
2679 channel_handler(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
2681 int rv = 0;
2682 int chan;
2684 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
2685 && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
2686 && (msg->msg.cmd == IPMI_GET_CHANNEL_INFO_CMD)) {
2687 /* It's the one we want */
2688 if (msg->msg.data[0] != 0) {
2689 /* Got an error from the channel, just go on. */
2691 if (msg->msg.data[0] == IPMI_INVALID_COMMAND_ERR) {
2693 * If the MC does not support this
2694 * command, that is legal. We just
2695 * assume it has one IPMB at channel
2696 * zero.
2698 intf->channels[0].medium
2699 = IPMI_CHANNEL_MEDIUM_IPMB;
2700 intf->channels[0].protocol
2701 = IPMI_CHANNEL_PROTOCOL_IPMB;
2702 rv = -ENOSYS;
2704 intf->curr_channel = IPMI_MAX_CHANNELS;
2705 wake_up(&intf->waitq);
2706 goto out;
2708 goto next_channel;
2710 if (msg->msg.data_len < 4) {
2711 /* Message not big enough, just go on. */
2712 goto next_channel;
2714 chan = intf->curr_channel;
2715 intf->channels[chan].medium = msg->msg.data[2] & 0x7f;
2716 intf->channels[chan].protocol = msg->msg.data[3] & 0x1f;
2718 next_channel:
2719 intf->curr_channel++;
2720 if (intf->curr_channel >= IPMI_MAX_CHANNELS)
2721 wake_up(&intf->waitq);
2722 else
2723 rv = send_channel_info_cmd(intf, intf->curr_channel);
2725 if (rv) {
2726 /* Got an error somehow, just give up. */
2727 intf->curr_channel = IPMI_MAX_CHANNELS;
2728 wake_up(&intf->waitq);
2730 printk(KERN_WARNING PFX
2731 "Error sending channel information: %d\n",
2732 rv);
2735 out:
2736 return;
2739 void ipmi_poll_interface(ipmi_user_t user)
2741 ipmi_smi_t intf = user->intf;
2743 if (intf->handlers->poll)
2744 intf->handlers->poll(intf->send_info);
2746 EXPORT_SYMBOL(ipmi_poll_interface);
2748 int ipmi_register_smi(struct ipmi_smi_handlers *handlers,
2749 void *send_info,
2750 struct ipmi_device_id *device_id,
2751 struct device *si_dev,
2752 const char *sysfs_name,
2753 unsigned char slave_addr)
2755 int i, j;
2756 int rv;
2757 ipmi_smi_t intf;
2758 ipmi_smi_t tintf;
2759 struct list_head *link;
2762 * Make sure the driver is actually initialized, this handles
2763 * problems with initialization order.
2765 if (!initialized) {
2766 rv = ipmi_init_msghandler();
2767 if (rv)
2768 return rv;
2770 * The init code doesn't return an error if it was turned
2771 * off, but it won't initialize. Check that.
2773 if (!initialized)
2774 return -ENODEV;
2777 intf = kzalloc(sizeof(*intf), GFP_KERNEL);
2778 if (!intf)
2779 return -ENOMEM;
2781 intf->ipmi_version_major = ipmi_version_major(device_id);
2782 intf->ipmi_version_minor = ipmi_version_minor(device_id);
2784 intf->bmc = kzalloc(sizeof(*intf->bmc), GFP_KERNEL);
2785 if (!intf->bmc) {
2786 kfree(intf);
2787 return -ENOMEM;
2789 intf->intf_num = -1; /* Mark it invalid for now. */
2790 kref_init(&intf->refcount);
2791 intf->bmc->id = *device_id;
2792 intf->si_dev = si_dev;
2793 for (j = 0; j < IPMI_MAX_CHANNELS; j++) {
2794 intf->channels[j].address = IPMI_BMC_SLAVE_ADDR;
2795 intf->channels[j].lun = 2;
2797 if (slave_addr != 0)
2798 intf->channels[0].address = slave_addr;
2799 INIT_LIST_HEAD(&intf->users);
2800 intf->handlers = handlers;
2801 intf->send_info = send_info;
2802 spin_lock_init(&intf->seq_lock);
2803 for (j = 0; j < IPMI_IPMB_NUM_SEQ; j++) {
2804 intf->seq_table[j].inuse = 0;
2805 intf->seq_table[j].seqid = 0;
2807 intf->curr_seq = 0;
2808 #ifdef CONFIG_PROC_FS
2809 mutex_init(&intf->proc_entry_lock);
2810 #endif
2811 spin_lock_init(&intf->waiting_msgs_lock);
2812 INIT_LIST_HEAD(&intf->waiting_msgs);
2813 spin_lock_init(&intf->events_lock);
2814 INIT_LIST_HEAD(&intf->waiting_events);
2815 intf->waiting_events_count = 0;
2816 mutex_init(&intf->cmd_rcvrs_mutex);
2817 spin_lock_init(&intf->maintenance_mode_lock);
2818 INIT_LIST_HEAD(&intf->cmd_rcvrs);
2819 init_waitqueue_head(&intf->waitq);
2820 for (i = 0; i < IPMI_NUM_STATS; i++)
2821 atomic_set(&intf->stats[i], 0);
2823 intf->proc_dir = NULL;
2825 mutex_lock(&smi_watchers_mutex);
2826 mutex_lock(&ipmi_interfaces_mutex);
2827 /* Look for a hole in the numbers. */
2828 i = 0;
2829 link = &ipmi_interfaces;
2830 list_for_each_entry_rcu(tintf, &ipmi_interfaces, link) {
2831 if (tintf->intf_num != i) {
2832 link = &tintf->link;
2833 break;
2835 i++;
2837 /* Add the new interface in numeric order. */
2838 if (i == 0)
2839 list_add_rcu(&intf->link, &ipmi_interfaces);
2840 else
2841 list_add_tail_rcu(&intf->link, link);
2843 rv = handlers->start_processing(send_info, intf);
2844 if (rv)
2845 goto out;
2847 get_guid(intf);
2849 if ((intf->ipmi_version_major > 1)
2850 || ((intf->ipmi_version_major == 1)
2851 && (intf->ipmi_version_minor >= 5))) {
2853 * Start scanning the channels to see what is
2854 * available.
2856 intf->null_user_handler = channel_handler;
2857 intf->curr_channel = 0;
2858 rv = send_channel_info_cmd(intf, 0);
2859 if (rv)
2860 goto out;
2862 /* Wait for the channel info to be read. */
2863 wait_event(intf->waitq,
2864 intf->curr_channel >= IPMI_MAX_CHANNELS);
2865 intf->null_user_handler = NULL;
2866 } else {
2867 /* Assume a single IPMB channel at zero. */
2868 intf->channels[0].medium = IPMI_CHANNEL_MEDIUM_IPMB;
2869 intf->channels[0].protocol = IPMI_CHANNEL_PROTOCOL_IPMB;
2870 intf->curr_channel = IPMI_MAX_CHANNELS;
2873 if (rv == 0)
2874 rv = add_proc_entries(intf, i);
2876 rv = ipmi_bmc_register(intf, i, sysfs_name);
2878 out:
2879 if (rv) {
2880 if (intf->proc_dir)
2881 remove_proc_entries(intf);
2882 intf->handlers = NULL;
2883 list_del_rcu(&intf->link);
2884 mutex_unlock(&ipmi_interfaces_mutex);
2885 mutex_unlock(&smi_watchers_mutex);
2886 synchronize_rcu();
2887 kref_put(&intf->refcount, intf_free);
2888 } else {
2890 * Keep memory order straight for RCU readers. Make
2891 * sure everything else is committed to memory before
2892 * setting intf_num to mark the interface valid.
2894 smp_wmb();
2895 intf->intf_num = i;
2896 mutex_unlock(&ipmi_interfaces_mutex);
2897 /* After this point the interface is legal to use. */
2898 call_smi_watchers(i, intf->si_dev);
2899 mutex_unlock(&smi_watchers_mutex);
2902 return rv;
2904 EXPORT_SYMBOL(ipmi_register_smi);
2906 static void cleanup_smi_msgs(ipmi_smi_t intf)
2908 int i;
2909 struct seq_table *ent;
2911 /* No need for locks, the interface is down. */
2912 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
2913 ent = &(intf->seq_table[i]);
2914 if (!ent->inuse)
2915 continue;
2916 deliver_err_response(ent->recv_msg, IPMI_ERR_UNSPECIFIED);
2920 int ipmi_unregister_smi(ipmi_smi_t intf)
2922 struct ipmi_smi_watcher *w;
2923 int intf_num = intf->intf_num;
2925 ipmi_bmc_unregister(intf);
2927 mutex_lock(&smi_watchers_mutex);
2928 mutex_lock(&ipmi_interfaces_mutex);
2929 intf->intf_num = -1;
2930 intf->handlers = NULL;
2931 list_del_rcu(&intf->link);
2932 mutex_unlock(&ipmi_interfaces_mutex);
2933 synchronize_rcu();
2935 cleanup_smi_msgs(intf);
2937 remove_proc_entries(intf);
2940 * Call all the watcher interfaces to tell them that
2941 * an interface is gone.
2943 list_for_each_entry(w, &smi_watchers, link)
2944 w->smi_gone(intf_num);
2945 mutex_unlock(&smi_watchers_mutex);
2947 kref_put(&intf->refcount, intf_free);
2948 return 0;
2950 EXPORT_SYMBOL(ipmi_unregister_smi);
2952 static int handle_ipmb_get_msg_rsp(ipmi_smi_t intf,
2953 struct ipmi_smi_msg *msg)
2955 struct ipmi_ipmb_addr ipmb_addr;
2956 struct ipmi_recv_msg *recv_msg;
2959 * This is 11, not 10, because the response must contain a
2960 * completion code.
2962 if (msg->rsp_size < 11) {
2963 /* Message not big enough, just ignore it. */
2964 ipmi_inc_stat(intf, invalid_ipmb_responses);
2965 return 0;
2968 if (msg->rsp[2] != 0) {
2969 /* An error getting the response, just ignore it. */
2970 return 0;
2973 ipmb_addr.addr_type = IPMI_IPMB_ADDR_TYPE;
2974 ipmb_addr.slave_addr = msg->rsp[6];
2975 ipmb_addr.channel = msg->rsp[3] & 0x0f;
2976 ipmb_addr.lun = msg->rsp[7] & 3;
2979 * It's a response from a remote entity. Look up the sequence
2980 * number and handle the response.
2982 if (intf_find_seq(intf,
2983 msg->rsp[7] >> 2,
2984 msg->rsp[3] & 0x0f,
2985 msg->rsp[8],
2986 (msg->rsp[4] >> 2) & (~1),
2987 (struct ipmi_addr *) &(ipmb_addr),
2988 &recv_msg)) {
2990 * We were unable to find the sequence number,
2991 * so just nuke the message.
2993 ipmi_inc_stat(intf, unhandled_ipmb_responses);
2994 return 0;
2997 memcpy(recv_msg->msg_data,
2998 &(msg->rsp[9]),
2999 msg->rsp_size - 9);
3001 * The other fields matched, so no need to set them, except
3002 * for netfn, which needs to be the response that was
3003 * returned, not the request value.
3005 recv_msg->msg.netfn = msg->rsp[4] >> 2;
3006 recv_msg->msg.data = recv_msg->msg_data;
3007 recv_msg->msg.data_len = msg->rsp_size - 10;
3008 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
3009 ipmi_inc_stat(intf, handled_ipmb_responses);
3010 deliver_response(recv_msg);
3012 return 0;
3015 static int handle_ipmb_get_msg_cmd(ipmi_smi_t intf,
3016 struct ipmi_smi_msg *msg)
3018 struct cmd_rcvr *rcvr;
3019 int rv = 0;
3020 unsigned char netfn;
3021 unsigned char cmd;
3022 unsigned char chan;
3023 ipmi_user_t user = NULL;
3024 struct ipmi_ipmb_addr *ipmb_addr;
3025 struct ipmi_recv_msg *recv_msg;
3026 struct ipmi_smi_handlers *handlers;
3028 if (msg->rsp_size < 10) {
3029 /* Message not big enough, just ignore it. */
3030 ipmi_inc_stat(intf, invalid_commands);
3031 return 0;
3034 if (msg->rsp[2] != 0) {
3035 /* An error getting the response, just ignore it. */
3036 return 0;
3039 netfn = msg->rsp[4] >> 2;
3040 cmd = msg->rsp[8];
3041 chan = msg->rsp[3] & 0xf;
3043 rcu_read_lock();
3044 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
3045 if (rcvr) {
3046 user = rcvr->user;
3047 kref_get(&user->refcount);
3048 } else
3049 user = NULL;
3050 rcu_read_unlock();
3052 if (user == NULL) {
3053 /* We didn't find a user, deliver an error response. */
3054 ipmi_inc_stat(intf, unhandled_commands);
3056 msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
3057 msg->data[1] = IPMI_SEND_MSG_CMD;
3058 msg->data[2] = msg->rsp[3];
3059 msg->data[3] = msg->rsp[6];
3060 msg->data[4] = ((netfn + 1) << 2) | (msg->rsp[7] & 0x3);
3061 msg->data[5] = ipmb_checksum(&(msg->data[3]), 2);
3062 msg->data[6] = intf->channels[msg->rsp[3] & 0xf].address;
3063 /* rqseq/lun */
3064 msg->data[7] = (msg->rsp[7] & 0xfc) | (msg->rsp[4] & 0x3);
3065 msg->data[8] = msg->rsp[8]; /* cmd */
3066 msg->data[9] = IPMI_INVALID_CMD_COMPLETION_CODE;
3067 msg->data[10] = ipmb_checksum(&(msg->data[6]), 4);
3068 msg->data_size = 11;
3070 #ifdef DEBUG_MSGING
3072 int m;
3073 printk("Invalid command:");
3074 for (m = 0; m < msg->data_size; m++)
3075 printk(" %2.2x", msg->data[m]);
3076 printk("\n");
3078 #endif
3079 rcu_read_lock();
3080 handlers = intf->handlers;
3081 if (handlers) {
3082 handlers->sender(intf->send_info, msg, 0);
3084 * We used the message, so return the value
3085 * that causes it to not be freed or
3086 * queued.
3088 rv = -1;
3090 rcu_read_unlock();
3091 } else {
3092 /* Deliver the message to the user. */
3093 ipmi_inc_stat(intf, handled_commands);
3095 recv_msg = ipmi_alloc_recv_msg();
3096 if (!recv_msg) {
3098 * We couldn't allocate memory for the
3099 * message, so requeue it for handling
3100 * later.
3102 rv = 1;
3103 kref_put(&user->refcount, free_user);
3104 } else {
3105 /* Extract the source address from the data. */
3106 ipmb_addr = (struct ipmi_ipmb_addr *) &recv_msg->addr;
3107 ipmb_addr->addr_type = IPMI_IPMB_ADDR_TYPE;
3108 ipmb_addr->slave_addr = msg->rsp[6];
3109 ipmb_addr->lun = msg->rsp[7] & 3;
3110 ipmb_addr->channel = msg->rsp[3] & 0xf;
3113 * Extract the rest of the message information
3114 * from the IPMB header.
3116 recv_msg->user = user;
3117 recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
3118 recv_msg->msgid = msg->rsp[7] >> 2;
3119 recv_msg->msg.netfn = msg->rsp[4] >> 2;
3120 recv_msg->msg.cmd = msg->rsp[8];
3121 recv_msg->msg.data = recv_msg->msg_data;
3124 * We chop off 10, not 9 bytes because the checksum
3125 * at the end also needs to be removed.
3127 recv_msg->msg.data_len = msg->rsp_size - 10;
3128 memcpy(recv_msg->msg_data,
3129 &(msg->rsp[9]),
3130 msg->rsp_size - 10);
3131 deliver_response(recv_msg);
3135 return rv;
3138 static int handle_lan_get_msg_rsp(ipmi_smi_t intf,
3139 struct ipmi_smi_msg *msg)
3141 struct ipmi_lan_addr lan_addr;
3142 struct ipmi_recv_msg *recv_msg;
3146 * This is 13, not 12, because the response must contain a
3147 * completion code.
3149 if (msg->rsp_size < 13) {
3150 /* Message not big enough, just ignore it. */
3151 ipmi_inc_stat(intf, invalid_lan_responses);
3152 return 0;
3155 if (msg->rsp[2] != 0) {
3156 /* An error getting the response, just ignore it. */
3157 return 0;
3160 lan_addr.addr_type = IPMI_LAN_ADDR_TYPE;
3161 lan_addr.session_handle = msg->rsp[4];
3162 lan_addr.remote_SWID = msg->rsp[8];
3163 lan_addr.local_SWID = msg->rsp[5];
3164 lan_addr.channel = msg->rsp[3] & 0x0f;
3165 lan_addr.privilege = msg->rsp[3] >> 4;
3166 lan_addr.lun = msg->rsp[9] & 3;
3169 * It's a response from a remote entity. Look up the sequence
3170 * number and handle the response.
3172 if (intf_find_seq(intf,
3173 msg->rsp[9] >> 2,
3174 msg->rsp[3] & 0x0f,
3175 msg->rsp[10],
3176 (msg->rsp[6] >> 2) & (~1),
3177 (struct ipmi_addr *) &(lan_addr),
3178 &recv_msg)) {
3180 * We were unable to find the sequence number,
3181 * so just nuke the message.
3183 ipmi_inc_stat(intf, unhandled_lan_responses);
3184 return 0;
3187 memcpy(recv_msg->msg_data,
3188 &(msg->rsp[11]),
3189 msg->rsp_size - 11);
3191 * The other fields matched, so no need to set them, except
3192 * for netfn, which needs to be the response that was
3193 * returned, not the request value.
3195 recv_msg->msg.netfn = msg->rsp[6] >> 2;
3196 recv_msg->msg.data = recv_msg->msg_data;
3197 recv_msg->msg.data_len = msg->rsp_size - 12;
3198 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
3199 ipmi_inc_stat(intf, handled_lan_responses);
3200 deliver_response(recv_msg);
3202 return 0;
3205 static int handle_lan_get_msg_cmd(ipmi_smi_t intf,
3206 struct ipmi_smi_msg *msg)
3208 struct cmd_rcvr *rcvr;
3209 int rv = 0;
3210 unsigned char netfn;
3211 unsigned char cmd;
3212 unsigned char chan;
3213 ipmi_user_t user = NULL;
3214 struct ipmi_lan_addr *lan_addr;
3215 struct ipmi_recv_msg *recv_msg;
3217 if (msg->rsp_size < 12) {
3218 /* Message not big enough, just ignore it. */
3219 ipmi_inc_stat(intf, invalid_commands);
3220 return 0;
3223 if (msg->rsp[2] != 0) {
3224 /* An error getting the response, just ignore it. */
3225 return 0;
3228 netfn = msg->rsp[6] >> 2;
3229 cmd = msg->rsp[10];
3230 chan = msg->rsp[3] & 0xf;
3232 rcu_read_lock();
3233 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
3234 if (rcvr) {
3235 user = rcvr->user;
3236 kref_get(&user->refcount);
3237 } else
3238 user = NULL;
3239 rcu_read_unlock();
3241 if (user == NULL) {
3242 /* We didn't find a user, just give up. */
3243 ipmi_inc_stat(intf, unhandled_commands);
3246 * Don't do anything with these messages, just allow
3247 * them to be freed.
3249 rv = 0;
3250 } else {
3251 /* Deliver the message to the user. */
3252 ipmi_inc_stat(intf, handled_commands);
3254 recv_msg = ipmi_alloc_recv_msg();
3255 if (!recv_msg) {
3257 * We couldn't allocate memory for the
3258 * message, so requeue it for handling later.
3260 rv = 1;
3261 kref_put(&user->refcount, free_user);
3262 } else {
3263 /* Extract the source address from the data. */
3264 lan_addr = (struct ipmi_lan_addr *) &recv_msg->addr;
3265 lan_addr->addr_type = IPMI_LAN_ADDR_TYPE;
3266 lan_addr->session_handle = msg->rsp[4];
3267 lan_addr->remote_SWID = msg->rsp[8];
3268 lan_addr->local_SWID = msg->rsp[5];
3269 lan_addr->lun = msg->rsp[9] & 3;
3270 lan_addr->channel = msg->rsp[3] & 0xf;
3271 lan_addr->privilege = msg->rsp[3] >> 4;
3274 * Extract the rest of the message information
3275 * from the IPMB header.
3277 recv_msg->user = user;
3278 recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
3279 recv_msg->msgid = msg->rsp[9] >> 2;
3280 recv_msg->msg.netfn = msg->rsp[6] >> 2;
3281 recv_msg->msg.cmd = msg->rsp[10];
3282 recv_msg->msg.data = recv_msg->msg_data;
3285 * We chop off 12, not 11 bytes because the checksum
3286 * at the end also needs to be removed.
3288 recv_msg->msg.data_len = msg->rsp_size - 12;
3289 memcpy(recv_msg->msg_data,
3290 &(msg->rsp[11]),
3291 msg->rsp_size - 12);
3292 deliver_response(recv_msg);
3296 return rv;
3300 * This routine will handle "Get Message" command responses with
3301 * channels that use an OEM Medium. The message format belongs to
3302 * the OEM. See IPMI 2.0 specification, Chapter 6 and
3303 * Chapter 22, sections 22.6 and 22.24 for more details.
3305 static int handle_oem_get_msg_cmd(ipmi_smi_t intf,
3306 struct ipmi_smi_msg *msg)
3308 struct cmd_rcvr *rcvr;
3309 int rv = 0;
3310 unsigned char netfn;
3311 unsigned char cmd;
3312 unsigned char chan;
3313 ipmi_user_t user = NULL;
3314 struct ipmi_system_interface_addr *smi_addr;
3315 struct ipmi_recv_msg *recv_msg;
3318 * We expect the OEM SW to perform error checking
3319 * so we just do some basic sanity checks
3321 if (msg->rsp_size < 4) {
3322 /* Message not big enough, just ignore it. */
3323 ipmi_inc_stat(intf, invalid_commands);
3324 return 0;
3327 if (msg->rsp[2] != 0) {
3328 /* An error getting the response, just ignore it. */
3329 return 0;
3333 * This is an OEM Message so the OEM needs to know how
3334 * handle the message. We do no interpretation.
3336 netfn = msg->rsp[0] >> 2;
3337 cmd = msg->rsp[1];
3338 chan = msg->rsp[3] & 0xf;
3340 rcu_read_lock();
3341 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
3342 if (rcvr) {
3343 user = rcvr->user;
3344 kref_get(&user->refcount);
3345 } else
3346 user = NULL;
3347 rcu_read_unlock();
3349 if (user == NULL) {
3350 /* We didn't find a user, just give up. */
3351 ipmi_inc_stat(intf, unhandled_commands);
3354 * Don't do anything with these messages, just allow
3355 * them to be freed.
3358 rv = 0;
3359 } else {
3360 /* Deliver the message to the user. */
3361 ipmi_inc_stat(intf, handled_commands);
3363 recv_msg = ipmi_alloc_recv_msg();
3364 if (!recv_msg) {
3366 * We couldn't allocate memory for the
3367 * message, so requeue it for handling
3368 * later.
3370 rv = 1;
3371 kref_put(&user->refcount, free_user);
3372 } else {
3374 * OEM Messages are expected to be delivered via
3375 * the system interface to SMS software. We might
3376 * need to visit this again depending on OEM
3377 * requirements
3379 smi_addr = ((struct ipmi_system_interface_addr *)
3380 &(recv_msg->addr));
3381 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3382 smi_addr->channel = IPMI_BMC_CHANNEL;
3383 smi_addr->lun = msg->rsp[0] & 3;
3385 recv_msg->user = user;
3386 recv_msg->user_msg_data = NULL;
3387 recv_msg->recv_type = IPMI_OEM_RECV_TYPE;
3388 recv_msg->msg.netfn = msg->rsp[0] >> 2;
3389 recv_msg->msg.cmd = msg->rsp[1];
3390 recv_msg->msg.data = recv_msg->msg_data;
3393 * The message starts at byte 4 which follows the
3394 * the Channel Byte in the "GET MESSAGE" command
3396 recv_msg->msg.data_len = msg->rsp_size - 4;
3397 memcpy(recv_msg->msg_data,
3398 &(msg->rsp[4]),
3399 msg->rsp_size - 4);
3400 deliver_response(recv_msg);
3404 return rv;
3407 static void copy_event_into_recv_msg(struct ipmi_recv_msg *recv_msg,
3408 struct ipmi_smi_msg *msg)
3410 struct ipmi_system_interface_addr *smi_addr;
3412 recv_msg->msgid = 0;
3413 smi_addr = (struct ipmi_system_interface_addr *) &(recv_msg->addr);
3414 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3415 smi_addr->channel = IPMI_BMC_CHANNEL;
3416 smi_addr->lun = msg->rsp[0] & 3;
3417 recv_msg->recv_type = IPMI_ASYNC_EVENT_RECV_TYPE;
3418 recv_msg->msg.netfn = msg->rsp[0] >> 2;
3419 recv_msg->msg.cmd = msg->rsp[1];
3420 memcpy(recv_msg->msg_data, &(msg->rsp[3]), msg->rsp_size - 3);
3421 recv_msg->msg.data = recv_msg->msg_data;
3422 recv_msg->msg.data_len = msg->rsp_size - 3;
3425 static int handle_read_event_rsp(ipmi_smi_t intf,
3426 struct ipmi_smi_msg *msg)
3428 struct ipmi_recv_msg *recv_msg, *recv_msg2;
3429 struct list_head msgs;
3430 ipmi_user_t user;
3431 int rv = 0;
3432 int deliver_count = 0;
3433 unsigned long flags;
3435 if (msg->rsp_size < 19) {
3436 /* Message is too small to be an IPMB event. */
3437 ipmi_inc_stat(intf, invalid_events);
3438 return 0;
3441 if (msg->rsp[2] != 0) {
3442 /* An error getting the event, just ignore it. */
3443 return 0;
3446 INIT_LIST_HEAD(&msgs);
3448 spin_lock_irqsave(&intf->events_lock, flags);
3450 ipmi_inc_stat(intf, events);
3453 * Allocate and fill in one message for every user that is
3454 * getting events.
3456 rcu_read_lock();
3457 list_for_each_entry_rcu(user, &intf->users, link) {
3458 if (!user->gets_events)
3459 continue;
3461 recv_msg = ipmi_alloc_recv_msg();
3462 if (!recv_msg) {
3463 rcu_read_unlock();
3464 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs,
3465 link) {
3466 list_del(&recv_msg->link);
3467 ipmi_free_recv_msg(recv_msg);
3470 * We couldn't allocate memory for the
3471 * message, so requeue it for handling
3472 * later.
3474 rv = 1;
3475 goto out;
3478 deliver_count++;
3480 copy_event_into_recv_msg(recv_msg, msg);
3481 recv_msg->user = user;
3482 kref_get(&user->refcount);
3483 list_add_tail(&(recv_msg->link), &msgs);
3485 rcu_read_unlock();
3487 if (deliver_count) {
3488 /* Now deliver all the messages. */
3489 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs, link) {
3490 list_del(&recv_msg->link);
3491 deliver_response(recv_msg);
3493 } else if (intf->waiting_events_count < MAX_EVENTS_IN_QUEUE) {
3495 * No one to receive the message, put it in queue if there's
3496 * not already too many things in the queue.
3498 recv_msg = ipmi_alloc_recv_msg();
3499 if (!recv_msg) {
3501 * We couldn't allocate memory for the
3502 * message, so requeue it for handling
3503 * later.
3505 rv = 1;
3506 goto out;
3509 copy_event_into_recv_msg(recv_msg, msg);
3510 list_add_tail(&(recv_msg->link), &(intf->waiting_events));
3511 intf->waiting_events_count++;
3512 } else if (!intf->event_msg_printed) {
3514 * There's too many things in the queue, discard this
3515 * message.
3517 printk(KERN_WARNING PFX "Event queue full, discarding"
3518 " incoming events\n");
3519 intf->event_msg_printed = 1;
3522 out:
3523 spin_unlock_irqrestore(&(intf->events_lock), flags);
3525 return rv;
3528 static int handle_bmc_rsp(ipmi_smi_t intf,
3529 struct ipmi_smi_msg *msg)
3531 struct ipmi_recv_msg *recv_msg;
3532 struct ipmi_user *user;
3534 recv_msg = (struct ipmi_recv_msg *) msg->user_data;
3535 if (recv_msg == NULL) {
3536 printk(KERN_WARNING
3537 "IPMI message received with no owner. This\n"
3538 "could be because of a malformed message, or\n"
3539 "because of a hardware error. Contact your\n"
3540 "hardware vender for assistance\n");
3541 return 0;
3544 user = recv_msg->user;
3545 /* Make sure the user still exists. */
3546 if (user && !user->valid) {
3547 /* The user for the message went away, so give up. */
3548 ipmi_inc_stat(intf, unhandled_local_responses);
3549 ipmi_free_recv_msg(recv_msg);
3550 } else {
3551 struct ipmi_system_interface_addr *smi_addr;
3553 ipmi_inc_stat(intf, handled_local_responses);
3554 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
3555 recv_msg->msgid = msg->msgid;
3556 smi_addr = ((struct ipmi_system_interface_addr *)
3557 &(recv_msg->addr));
3558 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3559 smi_addr->channel = IPMI_BMC_CHANNEL;
3560 smi_addr->lun = msg->rsp[0] & 3;
3561 recv_msg->msg.netfn = msg->rsp[0] >> 2;
3562 recv_msg->msg.cmd = msg->rsp[1];
3563 memcpy(recv_msg->msg_data,
3564 &(msg->rsp[2]),
3565 msg->rsp_size - 2);
3566 recv_msg->msg.data = recv_msg->msg_data;
3567 recv_msg->msg.data_len = msg->rsp_size - 2;
3568 deliver_response(recv_msg);
3571 return 0;
3575 * Handle a new message. Return 1 if the message should be requeued,
3576 * 0 if the message should be freed, or -1 if the message should not
3577 * be freed or requeued.
3579 static int handle_new_recv_msg(ipmi_smi_t intf,
3580 struct ipmi_smi_msg *msg)
3582 int requeue;
3583 int chan;
3585 #ifdef DEBUG_MSGING
3586 int m;
3587 printk("Recv:");
3588 for (m = 0; m < msg->rsp_size; m++)
3589 printk(" %2.2x", msg->rsp[m]);
3590 printk("\n");
3591 #endif
3592 if (msg->rsp_size < 2) {
3593 /* Message is too small to be correct. */
3594 printk(KERN_WARNING PFX "BMC returned to small a message"
3595 " for netfn %x cmd %x, got %d bytes\n",
3596 (msg->data[0] >> 2) | 1, msg->data[1], msg->rsp_size);
3598 /* Generate an error response for the message. */
3599 msg->rsp[0] = msg->data[0] | (1 << 2);
3600 msg->rsp[1] = msg->data[1];
3601 msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
3602 msg->rsp_size = 3;
3603 } else if (((msg->rsp[0] >> 2) != ((msg->data[0] >> 2) | 1))
3604 || (msg->rsp[1] != msg->data[1])) {
3606 * The NetFN and Command in the response is not even
3607 * marginally correct.
3609 printk(KERN_WARNING PFX "BMC returned incorrect response,"
3610 " expected netfn %x cmd %x, got netfn %x cmd %x\n",
3611 (msg->data[0] >> 2) | 1, msg->data[1],
3612 msg->rsp[0] >> 2, msg->rsp[1]);
3614 /* Generate an error response for the message. */
3615 msg->rsp[0] = msg->data[0] | (1 << 2);
3616 msg->rsp[1] = msg->data[1];
3617 msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
3618 msg->rsp_size = 3;
3621 if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
3622 && (msg->rsp[1] == IPMI_SEND_MSG_CMD)
3623 && (msg->user_data != NULL)) {
3625 * It's a response to a response we sent. For this we
3626 * deliver a send message response to the user.
3628 struct ipmi_recv_msg *recv_msg = msg->user_data;
3630 requeue = 0;
3631 if (msg->rsp_size < 2)
3632 /* Message is too small to be correct. */
3633 goto out;
3635 chan = msg->data[2] & 0x0f;
3636 if (chan >= IPMI_MAX_CHANNELS)
3637 /* Invalid channel number */
3638 goto out;
3640 if (!recv_msg)
3641 goto out;
3643 /* Make sure the user still exists. */
3644 if (!recv_msg->user || !recv_msg->user->valid)
3645 goto out;
3647 recv_msg->recv_type = IPMI_RESPONSE_RESPONSE_TYPE;
3648 recv_msg->msg.data = recv_msg->msg_data;
3649 recv_msg->msg.data_len = 1;
3650 recv_msg->msg_data[0] = msg->rsp[2];
3651 deliver_response(recv_msg);
3652 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
3653 && (msg->rsp[1] == IPMI_GET_MSG_CMD)) {
3654 /* It's from the receive queue. */
3655 chan = msg->rsp[3] & 0xf;
3656 if (chan >= IPMI_MAX_CHANNELS) {
3657 /* Invalid channel number */
3658 requeue = 0;
3659 goto out;
3663 * We need to make sure the channels have been initialized.
3664 * The channel_handler routine will set the "curr_channel"
3665 * equal to or greater than IPMI_MAX_CHANNELS when all the
3666 * channels for this interface have been initialized.
3668 if (intf->curr_channel < IPMI_MAX_CHANNELS) {
3669 requeue = 0; /* Throw the message away */
3670 goto out;
3673 switch (intf->channels[chan].medium) {
3674 case IPMI_CHANNEL_MEDIUM_IPMB:
3675 if (msg->rsp[4] & 0x04) {
3677 * It's a response, so find the
3678 * requesting message and send it up.
3680 requeue = handle_ipmb_get_msg_rsp(intf, msg);
3681 } else {
3683 * It's a command to the SMS from some other
3684 * entity. Handle that.
3686 requeue = handle_ipmb_get_msg_cmd(intf, msg);
3688 break;
3690 case IPMI_CHANNEL_MEDIUM_8023LAN:
3691 case IPMI_CHANNEL_MEDIUM_ASYNC:
3692 if (msg->rsp[6] & 0x04) {
3694 * It's a response, so find the
3695 * requesting message and send it up.
3697 requeue = handle_lan_get_msg_rsp(intf, msg);
3698 } else {
3700 * It's a command to the SMS from some other
3701 * entity. Handle that.
3703 requeue = handle_lan_get_msg_cmd(intf, msg);
3705 break;
3707 default:
3708 /* Check for OEM Channels. Clients had better
3709 register for these commands. */
3710 if ((intf->channels[chan].medium
3711 >= IPMI_CHANNEL_MEDIUM_OEM_MIN)
3712 && (intf->channels[chan].medium
3713 <= IPMI_CHANNEL_MEDIUM_OEM_MAX)) {
3714 requeue = handle_oem_get_msg_cmd(intf, msg);
3715 } else {
3717 * We don't handle the channel type, so just
3718 * free the message.
3720 requeue = 0;
3724 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
3725 && (msg->rsp[1] == IPMI_READ_EVENT_MSG_BUFFER_CMD)) {
3726 /* It's an asyncronous event. */
3727 requeue = handle_read_event_rsp(intf, msg);
3728 } else {
3729 /* It's a response from the local BMC. */
3730 requeue = handle_bmc_rsp(intf, msg);
3733 out:
3734 return requeue;
3737 /* Handle a new message from the lower layer. */
3738 void ipmi_smi_msg_received(ipmi_smi_t intf,
3739 struct ipmi_smi_msg *msg)
3741 unsigned long flags = 0; /* keep us warning-free. */
3742 int rv;
3743 int run_to_completion;
3746 if ((msg->data_size >= 2)
3747 && (msg->data[0] == (IPMI_NETFN_APP_REQUEST << 2))
3748 && (msg->data[1] == IPMI_SEND_MSG_CMD)
3749 && (msg->user_data == NULL)) {
3751 * This is the local response to a command send, start
3752 * the timer for these. The user_data will not be
3753 * NULL if this is a response send, and we will let
3754 * response sends just go through.
3758 * Check for errors, if we get certain errors (ones
3759 * that mean basically we can try again later), we
3760 * ignore them and start the timer. Otherwise we
3761 * report the error immediately.
3763 if ((msg->rsp_size >= 3) && (msg->rsp[2] != 0)
3764 && (msg->rsp[2] != IPMI_NODE_BUSY_ERR)
3765 && (msg->rsp[2] != IPMI_LOST_ARBITRATION_ERR)
3766 && (msg->rsp[2] != IPMI_BUS_ERR)
3767 && (msg->rsp[2] != IPMI_NAK_ON_WRITE_ERR)) {
3768 int chan = msg->rsp[3] & 0xf;
3770 /* Got an error sending the message, handle it. */
3771 if (chan >= IPMI_MAX_CHANNELS)
3772 ; /* This shouldn't happen */
3773 else if ((intf->channels[chan].medium
3774 == IPMI_CHANNEL_MEDIUM_8023LAN)
3775 || (intf->channels[chan].medium
3776 == IPMI_CHANNEL_MEDIUM_ASYNC))
3777 ipmi_inc_stat(intf, sent_lan_command_errs);
3778 else
3779 ipmi_inc_stat(intf, sent_ipmb_command_errs);
3780 intf_err_seq(intf, msg->msgid, msg->rsp[2]);
3781 } else
3782 /* The message was sent, start the timer. */
3783 intf_start_seq_timer(intf, msg->msgid);
3785 ipmi_free_smi_msg(msg);
3786 goto out;
3790 * To preserve message order, if the list is not empty, we
3791 * tack this message onto the end of the list.
3793 run_to_completion = intf->run_to_completion;
3794 if (!run_to_completion)
3795 spin_lock_irqsave(&intf->waiting_msgs_lock, flags);
3796 if (!list_empty(&intf->waiting_msgs)) {
3797 list_add_tail(&msg->link, &intf->waiting_msgs);
3798 if (!run_to_completion)
3799 spin_unlock_irqrestore(&intf->waiting_msgs_lock, flags);
3800 goto out;
3802 if (!run_to_completion)
3803 spin_unlock_irqrestore(&intf->waiting_msgs_lock, flags);
3805 rv = handle_new_recv_msg(intf, msg);
3806 if (rv > 0) {
3808 * Could not handle the message now, just add it to a
3809 * list to handle later.
3811 run_to_completion = intf->run_to_completion;
3812 if (!run_to_completion)
3813 spin_lock_irqsave(&intf->waiting_msgs_lock, flags);
3814 list_add_tail(&msg->link, &intf->waiting_msgs);
3815 if (!run_to_completion)
3816 spin_unlock_irqrestore(&intf->waiting_msgs_lock, flags);
3817 } else if (rv == 0) {
3818 ipmi_free_smi_msg(msg);
3821 out:
3822 return;
3824 EXPORT_SYMBOL(ipmi_smi_msg_received);
3826 void ipmi_smi_watchdog_pretimeout(ipmi_smi_t intf)
3828 ipmi_user_t user;
3830 rcu_read_lock();
3831 list_for_each_entry_rcu(user, &intf->users, link) {
3832 if (!user->handler->ipmi_watchdog_pretimeout)
3833 continue;
3835 user->handler->ipmi_watchdog_pretimeout(user->handler_data);
3837 rcu_read_unlock();
3839 EXPORT_SYMBOL(ipmi_smi_watchdog_pretimeout);
3841 static struct ipmi_smi_msg *
3842 smi_from_recv_msg(ipmi_smi_t intf, struct ipmi_recv_msg *recv_msg,
3843 unsigned char seq, long seqid)
3845 struct ipmi_smi_msg *smi_msg = ipmi_alloc_smi_msg();
3846 if (!smi_msg)
3848 * If we can't allocate the message, then just return, we
3849 * get 4 retries, so this should be ok.
3851 return NULL;
3853 memcpy(smi_msg->data, recv_msg->msg.data, recv_msg->msg.data_len);
3854 smi_msg->data_size = recv_msg->msg.data_len;
3855 smi_msg->msgid = STORE_SEQ_IN_MSGID(seq, seqid);
3857 #ifdef DEBUG_MSGING
3859 int m;
3860 printk("Resend: ");
3861 for (m = 0; m < smi_msg->data_size; m++)
3862 printk(" %2.2x", smi_msg->data[m]);
3863 printk("\n");
3865 #endif
3866 return smi_msg;
3869 static void check_msg_timeout(ipmi_smi_t intf, struct seq_table *ent,
3870 struct list_head *timeouts, long timeout_period,
3871 int slot, unsigned long *flags)
3873 struct ipmi_recv_msg *msg;
3874 struct ipmi_smi_handlers *handlers;
3876 if (intf->intf_num == -1)
3877 return;
3879 if (!ent->inuse)
3880 return;
3882 ent->timeout -= timeout_period;
3883 if (ent->timeout > 0)
3884 return;
3886 if (ent->retries_left == 0) {
3887 /* The message has used all its retries. */
3888 ent->inuse = 0;
3889 msg = ent->recv_msg;
3890 list_add_tail(&msg->link, timeouts);
3891 if (ent->broadcast)
3892 ipmi_inc_stat(intf, timed_out_ipmb_broadcasts);
3893 else if (is_lan_addr(&ent->recv_msg->addr))
3894 ipmi_inc_stat(intf, timed_out_lan_commands);
3895 else
3896 ipmi_inc_stat(intf, timed_out_ipmb_commands);
3897 } else {
3898 struct ipmi_smi_msg *smi_msg;
3899 /* More retries, send again. */
3902 * Start with the max timer, set to normal timer after
3903 * the message is sent.
3905 ent->timeout = MAX_MSG_TIMEOUT;
3906 ent->retries_left--;
3907 smi_msg = smi_from_recv_msg(intf, ent->recv_msg, slot,
3908 ent->seqid);
3909 if (!smi_msg) {
3910 if (is_lan_addr(&ent->recv_msg->addr))
3911 ipmi_inc_stat(intf,
3912 dropped_rexmit_lan_commands);
3913 else
3914 ipmi_inc_stat(intf,
3915 dropped_rexmit_ipmb_commands);
3916 return;
3919 spin_unlock_irqrestore(&intf->seq_lock, *flags);
3922 * Send the new message. We send with a zero
3923 * priority. It timed out, I doubt time is that
3924 * critical now, and high priority messages are really
3925 * only for messages to the local MC, which don't get
3926 * resent.
3928 handlers = intf->handlers;
3929 if (handlers) {
3930 if (is_lan_addr(&ent->recv_msg->addr))
3931 ipmi_inc_stat(intf,
3932 retransmitted_lan_commands);
3933 else
3934 ipmi_inc_stat(intf,
3935 retransmitted_ipmb_commands);
3937 intf->handlers->sender(intf->send_info,
3938 smi_msg, 0);
3939 } else
3940 ipmi_free_smi_msg(smi_msg);
3942 spin_lock_irqsave(&intf->seq_lock, *flags);
3946 static void ipmi_timeout_handler(long timeout_period)
3948 ipmi_smi_t intf;
3949 struct list_head timeouts;
3950 struct ipmi_recv_msg *msg, *msg2;
3951 struct ipmi_smi_msg *smi_msg, *smi_msg2;
3952 unsigned long flags;
3953 int i;
3955 rcu_read_lock();
3956 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
3957 /* See if any waiting messages need to be processed. */
3958 spin_lock_irqsave(&intf->waiting_msgs_lock, flags);
3959 list_for_each_entry_safe(smi_msg, smi_msg2,
3960 &intf->waiting_msgs, link) {
3961 if (!handle_new_recv_msg(intf, smi_msg)) {
3962 list_del(&smi_msg->link);
3963 ipmi_free_smi_msg(smi_msg);
3964 } else {
3966 * To preserve message order, quit if we
3967 * can't handle a message.
3969 break;
3972 spin_unlock_irqrestore(&intf->waiting_msgs_lock, flags);
3975 * Go through the seq table and find any messages that
3976 * have timed out, putting them in the timeouts
3977 * list.
3979 INIT_LIST_HEAD(&timeouts);
3980 spin_lock_irqsave(&intf->seq_lock, flags);
3981 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++)
3982 check_msg_timeout(intf, &(intf->seq_table[i]),
3983 &timeouts, timeout_period, i,
3984 &flags);
3985 spin_unlock_irqrestore(&intf->seq_lock, flags);
3987 list_for_each_entry_safe(msg, msg2, &timeouts, link)
3988 deliver_err_response(msg, IPMI_TIMEOUT_COMPLETION_CODE);
3991 * Maintenance mode handling. Check the timeout
3992 * optimistically before we claim the lock. It may
3993 * mean a timeout gets missed occasionally, but that
3994 * only means the timeout gets extended by one period
3995 * in that case. No big deal, and it avoids the lock
3996 * most of the time.
3998 if (intf->auto_maintenance_timeout > 0) {
3999 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
4000 if (intf->auto_maintenance_timeout > 0) {
4001 intf->auto_maintenance_timeout
4002 -= timeout_period;
4003 if (!intf->maintenance_mode
4004 && (intf->auto_maintenance_timeout <= 0)) {
4005 intf->maintenance_mode_enable = 0;
4006 maintenance_mode_update(intf);
4009 spin_unlock_irqrestore(&intf->maintenance_mode_lock,
4010 flags);
4013 rcu_read_unlock();
4016 static void ipmi_request_event(void)
4018 ipmi_smi_t intf;
4019 struct ipmi_smi_handlers *handlers;
4021 rcu_read_lock();
4023 * Called from the timer, no need to check if handlers is
4024 * valid.
4026 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
4027 /* No event requests when in maintenance mode. */
4028 if (intf->maintenance_mode_enable)
4029 continue;
4031 handlers = intf->handlers;
4032 if (handlers)
4033 handlers->request_events(intf->send_info);
4035 rcu_read_unlock();
4038 static struct timer_list ipmi_timer;
4040 /* Call every ~100 ms. */
4041 #define IPMI_TIMEOUT_TIME 100
4043 /* How many jiffies does it take to get to the timeout time. */
4044 #define IPMI_TIMEOUT_JIFFIES ((IPMI_TIMEOUT_TIME * HZ) / 1000)
4047 * Request events from the queue every second (this is the number of
4048 * IPMI_TIMEOUT_TIMES between event requests). Hopefully, in the
4049 * future, IPMI will add a way to know immediately if an event is in
4050 * the queue and this silliness can go away.
4052 #define IPMI_REQUEST_EV_TIME (1000 / (IPMI_TIMEOUT_TIME))
4054 static atomic_t stop_operation;
4055 static unsigned int ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
4057 static void ipmi_timeout(unsigned long data)
4059 if (atomic_read(&stop_operation))
4060 return;
4062 ticks_to_req_ev--;
4063 if (ticks_to_req_ev == 0) {
4064 ipmi_request_event();
4065 ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
4068 ipmi_timeout_handler(IPMI_TIMEOUT_TIME);
4070 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
4074 static atomic_t smi_msg_inuse_count = ATOMIC_INIT(0);
4075 static atomic_t recv_msg_inuse_count = ATOMIC_INIT(0);
4077 /* FIXME - convert these to slabs. */
4078 static void free_smi_msg(struct ipmi_smi_msg *msg)
4080 atomic_dec(&smi_msg_inuse_count);
4081 kfree(msg);
4084 struct ipmi_smi_msg *ipmi_alloc_smi_msg(void)
4086 struct ipmi_smi_msg *rv;
4087 rv = kmalloc(sizeof(struct ipmi_smi_msg), GFP_ATOMIC);
4088 if (rv) {
4089 rv->done = free_smi_msg;
4090 rv->user_data = NULL;
4091 atomic_inc(&smi_msg_inuse_count);
4093 return rv;
4095 EXPORT_SYMBOL(ipmi_alloc_smi_msg);
4097 static void free_recv_msg(struct ipmi_recv_msg *msg)
4099 atomic_dec(&recv_msg_inuse_count);
4100 kfree(msg);
4103 static struct ipmi_recv_msg *ipmi_alloc_recv_msg(void)
4105 struct ipmi_recv_msg *rv;
4107 rv = kmalloc(sizeof(struct ipmi_recv_msg), GFP_ATOMIC);
4108 if (rv) {
4109 rv->user = NULL;
4110 rv->done = free_recv_msg;
4111 atomic_inc(&recv_msg_inuse_count);
4113 return rv;
4116 void ipmi_free_recv_msg(struct ipmi_recv_msg *msg)
4118 if (msg->user)
4119 kref_put(&msg->user->refcount, free_user);
4120 msg->done(msg);
4122 EXPORT_SYMBOL(ipmi_free_recv_msg);
4124 #ifdef CONFIG_IPMI_PANIC_EVENT
4126 static void dummy_smi_done_handler(struct ipmi_smi_msg *msg)
4130 static void dummy_recv_done_handler(struct ipmi_recv_msg *msg)
4134 #ifdef CONFIG_IPMI_PANIC_STRING
4135 static void event_receiver_fetcher(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
4137 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
4138 && (msg->msg.netfn == IPMI_NETFN_SENSOR_EVENT_RESPONSE)
4139 && (msg->msg.cmd == IPMI_GET_EVENT_RECEIVER_CMD)
4140 && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) {
4141 /* A get event receiver command, save it. */
4142 intf->event_receiver = msg->msg.data[1];
4143 intf->event_receiver_lun = msg->msg.data[2] & 0x3;
4147 static void device_id_fetcher(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
4149 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
4150 && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
4151 && (msg->msg.cmd == IPMI_GET_DEVICE_ID_CMD)
4152 && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) {
4154 * A get device id command, save if we are an event
4155 * receiver or generator.
4157 intf->local_sel_device = (msg->msg.data[6] >> 2) & 1;
4158 intf->local_event_generator = (msg->msg.data[6] >> 5) & 1;
4161 #endif
4163 static void send_panic_events(char *str)
4165 struct kernel_ipmi_msg msg;
4166 ipmi_smi_t intf;
4167 unsigned char data[16];
4168 struct ipmi_system_interface_addr *si;
4169 struct ipmi_addr addr;
4170 struct ipmi_smi_msg smi_msg;
4171 struct ipmi_recv_msg recv_msg;
4173 si = (struct ipmi_system_interface_addr *) &addr;
4174 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
4175 si->channel = IPMI_BMC_CHANNEL;
4176 si->lun = 0;
4178 /* Fill in an event telling that we have failed. */
4179 msg.netfn = 0x04; /* Sensor or Event. */
4180 msg.cmd = 2; /* Platform event command. */
4181 msg.data = data;
4182 msg.data_len = 8;
4183 data[0] = 0x41; /* Kernel generator ID, IPMI table 5-4 */
4184 data[1] = 0x03; /* This is for IPMI 1.0. */
4185 data[2] = 0x20; /* OS Critical Stop, IPMI table 36-3 */
4186 data[4] = 0x6f; /* Sensor specific, IPMI table 36-1 */
4187 data[5] = 0xa1; /* Runtime stop OEM bytes 2 & 3. */
4190 * Put a few breadcrumbs in. Hopefully later we can add more things
4191 * to make the panic events more useful.
4193 if (str) {
4194 data[3] = str[0];
4195 data[6] = str[1];
4196 data[7] = str[2];
4199 smi_msg.done = dummy_smi_done_handler;
4200 recv_msg.done = dummy_recv_done_handler;
4202 /* For every registered interface, send the event. */
4203 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
4204 if (!intf->handlers)
4205 /* Interface is not ready. */
4206 continue;
4208 intf->run_to_completion = 1;
4209 /* Send the event announcing the panic. */
4210 intf->handlers->set_run_to_completion(intf->send_info, 1);
4211 i_ipmi_request(NULL,
4212 intf,
4213 &addr,
4215 &msg,
4216 intf,
4217 &smi_msg,
4218 &recv_msg,
4220 intf->channels[0].address,
4221 intf->channels[0].lun,
4222 0, 1); /* Don't retry, and don't wait. */
4225 #ifdef CONFIG_IPMI_PANIC_STRING
4227 * On every interface, dump a bunch of OEM event holding the
4228 * string.
4230 if (!str)
4231 return;
4233 /* For every registered interface, send the event. */
4234 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
4235 char *p = str;
4236 struct ipmi_ipmb_addr *ipmb;
4237 int j;
4239 if (intf->intf_num == -1)
4240 /* Interface was not ready yet. */
4241 continue;
4244 * intf_num is used as an marker to tell if the
4245 * interface is valid. Thus we need a read barrier to
4246 * make sure data fetched before checking intf_num
4247 * won't be used.
4249 smp_rmb();
4252 * First job here is to figure out where to send the
4253 * OEM events. There's no way in IPMI to send OEM
4254 * events using an event send command, so we have to
4255 * find the SEL to put them in and stick them in
4256 * there.
4259 /* Get capabilities from the get device id. */
4260 intf->local_sel_device = 0;
4261 intf->local_event_generator = 0;
4262 intf->event_receiver = 0;
4264 /* Request the device info from the local MC. */
4265 msg.netfn = IPMI_NETFN_APP_REQUEST;
4266 msg.cmd = IPMI_GET_DEVICE_ID_CMD;
4267 msg.data = NULL;
4268 msg.data_len = 0;
4269 intf->null_user_handler = device_id_fetcher;
4270 i_ipmi_request(NULL,
4271 intf,
4272 &addr,
4274 &msg,
4275 intf,
4276 &smi_msg,
4277 &recv_msg,
4279 intf->channels[0].address,
4280 intf->channels[0].lun,
4281 0, 1); /* Don't retry, and don't wait. */
4283 if (intf->local_event_generator) {
4284 /* Request the event receiver from the local MC. */
4285 msg.netfn = IPMI_NETFN_SENSOR_EVENT_REQUEST;
4286 msg.cmd = IPMI_GET_EVENT_RECEIVER_CMD;
4287 msg.data = NULL;
4288 msg.data_len = 0;
4289 intf->null_user_handler = event_receiver_fetcher;
4290 i_ipmi_request(NULL,
4291 intf,
4292 &addr,
4294 &msg,
4295 intf,
4296 &smi_msg,
4297 &recv_msg,
4299 intf->channels[0].address,
4300 intf->channels[0].lun,
4301 0, 1); /* no retry, and no wait. */
4303 intf->null_user_handler = NULL;
4306 * Validate the event receiver. The low bit must not
4307 * be 1 (it must be a valid IPMB address), it cannot
4308 * be zero, and it must not be my address.
4310 if (((intf->event_receiver & 1) == 0)
4311 && (intf->event_receiver != 0)
4312 && (intf->event_receiver != intf->channels[0].address)) {
4314 * The event receiver is valid, send an IPMB
4315 * message.
4317 ipmb = (struct ipmi_ipmb_addr *) &addr;
4318 ipmb->addr_type = IPMI_IPMB_ADDR_TYPE;
4319 ipmb->channel = 0; /* FIXME - is this right? */
4320 ipmb->lun = intf->event_receiver_lun;
4321 ipmb->slave_addr = intf->event_receiver;
4322 } else if (intf->local_sel_device) {
4324 * The event receiver was not valid (or was
4325 * me), but I am an SEL device, just dump it
4326 * in my SEL.
4328 si = (struct ipmi_system_interface_addr *) &addr;
4329 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
4330 si->channel = IPMI_BMC_CHANNEL;
4331 si->lun = 0;
4332 } else
4333 continue; /* No where to send the event. */
4335 msg.netfn = IPMI_NETFN_STORAGE_REQUEST; /* Storage. */
4336 msg.cmd = IPMI_ADD_SEL_ENTRY_CMD;
4337 msg.data = data;
4338 msg.data_len = 16;
4340 j = 0;
4341 while (*p) {
4342 int size = strlen(p);
4344 if (size > 11)
4345 size = 11;
4346 data[0] = 0;
4347 data[1] = 0;
4348 data[2] = 0xf0; /* OEM event without timestamp. */
4349 data[3] = intf->channels[0].address;
4350 data[4] = j++; /* sequence # */
4352 * Always give 11 bytes, so strncpy will fill
4353 * it with zeroes for me.
4355 strncpy(data+5, p, 11);
4356 p += size;
4358 i_ipmi_request(NULL,
4359 intf,
4360 &addr,
4362 &msg,
4363 intf,
4364 &smi_msg,
4365 &recv_msg,
4367 intf->channels[0].address,
4368 intf->channels[0].lun,
4369 0, 1); /* no retry, and no wait. */
4372 #endif /* CONFIG_IPMI_PANIC_STRING */
4374 #endif /* CONFIG_IPMI_PANIC_EVENT */
4376 static int has_panicked;
4378 static int panic_event(struct notifier_block *this,
4379 unsigned long event,
4380 void *ptr)
4382 ipmi_smi_t intf;
4384 if (has_panicked)
4385 return NOTIFY_DONE;
4386 has_panicked = 1;
4388 /* For every registered interface, set it to run to completion. */
4389 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
4390 if (!intf->handlers)
4391 /* Interface is not ready. */
4392 continue;
4394 intf->run_to_completion = 1;
4395 intf->handlers->set_run_to_completion(intf->send_info, 1);
4398 #ifdef CONFIG_IPMI_PANIC_EVENT
4399 send_panic_events(ptr);
4400 #endif
4402 return NOTIFY_DONE;
4405 static struct notifier_block panic_block = {
4406 .notifier_call = panic_event,
4407 .next = NULL,
4408 .priority = 200 /* priority: INT_MAX >= x >= 0 */
4411 static int ipmi_init_msghandler(void)
4413 int rv;
4415 if (initialized)
4416 return 0;
4418 rv = driver_register(&ipmidriver.driver);
4419 if (rv) {
4420 printk(KERN_ERR PFX "Could not register IPMI driver\n");
4421 return rv;
4424 printk(KERN_INFO "ipmi message handler version "
4425 IPMI_DRIVER_VERSION "\n");
4427 #ifdef CONFIG_PROC_FS
4428 proc_ipmi_root = proc_mkdir("ipmi", NULL);
4429 if (!proc_ipmi_root) {
4430 printk(KERN_ERR PFX "Unable to create IPMI proc dir");
4431 return -ENOMEM;
4434 #endif /* CONFIG_PROC_FS */
4436 setup_timer(&ipmi_timer, ipmi_timeout, 0);
4437 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
4439 atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
4441 initialized = 1;
4443 return 0;
4446 static __init int ipmi_init_msghandler_mod(void)
4448 ipmi_init_msghandler();
4449 return 0;
4452 static __exit void cleanup_ipmi(void)
4454 int count;
4456 if (!initialized)
4457 return;
4459 atomic_notifier_chain_unregister(&panic_notifier_list, &panic_block);
4462 * This can't be called if any interfaces exist, so no worry
4463 * about shutting down the interfaces.
4467 * Tell the timer to stop, then wait for it to stop. This
4468 * avoids problems with race conditions removing the timer
4469 * here.
4471 atomic_inc(&stop_operation);
4472 del_timer_sync(&ipmi_timer);
4474 #ifdef CONFIG_PROC_FS
4475 remove_proc_entry(proc_ipmi_root->name, NULL);
4476 #endif /* CONFIG_PROC_FS */
4478 driver_unregister(&ipmidriver.driver);
4480 initialized = 0;
4482 /* Check for buffer leaks. */
4483 count = atomic_read(&smi_msg_inuse_count);
4484 if (count != 0)
4485 printk(KERN_WARNING PFX "SMI message count %d at exit\n",
4486 count);
4487 count = atomic_read(&recv_msg_inuse_count);
4488 if (count != 0)
4489 printk(KERN_WARNING PFX "recv message count %d at exit\n",
4490 count);
4492 module_exit(cleanup_ipmi);
4494 module_init(ipmi_init_msghandler_mod);
4495 MODULE_LICENSE("GPL");
4496 MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
4497 MODULE_DESCRIPTION("Incoming and outgoing message routing for an IPMI"
4498 " interface.");
4499 MODULE_VERSION(IPMI_DRIVER_VERSION);