4 * Incoming and outgoing message routing for an IPMI interface.
6 * Author: MontaVista Software, Inc.
7 * Corey Minyard <minyard@mvista.com>
10 * Copyright 2002 MontaVista Software Inc.
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
19 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
24 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
26 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
27 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * You should have received a copy of the GNU General Public License along
30 * with this program; if not, write to the Free Software Foundation, Inc.,
31 * 675 Mass Ave, Cambridge, MA 02139, USA.
34 #include <linux/config.h>
35 #include <linux/module.h>
36 #include <linux/errno.h>
37 #include <asm/system.h>
38 #include <linux/sched.h>
39 #include <linux/poll.h>
40 #include <linux/spinlock.h>
41 #include <linux/mutex.h>
42 #include <linux/slab.h>
43 #include <linux/ipmi.h>
44 #include <linux/ipmi_smi.h>
45 #include <linux/notifier.h>
46 #include <linux/init.h>
47 #include <linux/proc_fs.h>
48 #include <linux/rcupdate.h>
50 #define PFX "IPMI message handler: "
52 #define IPMI_DRIVER_VERSION "39.0"
54 static struct ipmi_recv_msg *ipmi_alloc_recv_msg(void);
55 static int ipmi_init_msghandler(void);
57 static int initialized = 0;
60 struct proc_dir_entry *proc_ipmi_root = NULL;
61 EXPORT_SYMBOL(proc_ipmi_root);
62 #endif /* CONFIG_PROC_FS */
64 #define MAX_EVENTS_IN_QUEUE 25
66 /* Don't let a message sit in a queue forever, always time it with at lest
67 the max message timer. This is in milliseconds. */
68 #define MAX_MSG_TIMEOUT 60000
72 * The main "user" data structure.
76 struct list_head link;
78 /* Set to "0" when the user is destroyed. */
83 /* The upper layer that handles receive messages. */
84 struct ipmi_user_hndl *handler;
87 /* The interface this user is bound to. */
90 /* Does this interface receive IPMI events? */
96 struct list_head link;
103 * This is used to form a linked lised during mass deletion.
104 * Since this is in an RCU list, we cannot use the link above
105 * or change any data until the RCU period completes. So we
106 * use this next variable during mass deletion so we can have
107 * a list and don't have to wait and restart the search on
108 * every individual deletion of a command. */
109 struct cmd_rcvr *next;
114 unsigned int inuse : 1;
115 unsigned int broadcast : 1;
117 unsigned long timeout;
118 unsigned long orig_timeout;
119 unsigned int retries_left;
121 /* To verify on an incoming send message response that this is
122 the message that the response is for, we keep a sequence id
123 and increment it every time we send a message. */
126 /* This is held so we can properly respond to the message on a
127 timeout, and it is used to hold the temporary data for
128 retransmission, too. */
129 struct ipmi_recv_msg *recv_msg;
132 /* Store the information in a msgid (long) to allow us to find a
133 sequence table entry from the msgid. */
134 #define STORE_SEQ_IN_MSGID(seq, seqid) (((seq&0xff)<<26) | (seqid&0x3ffffff))
136 #define GET_SEQ_FROM_MSGID(msgid, seq, seqid) \
138 seq = ((msgid >> 26) & 0x3f); \
139 seqid = (msgid & 0x3fffff); \
142 #define NEXT_SEQID(seqid) (((seqid) + 1) & 0x3fffff)
146 unsigned char medium;
147 unsigned char protocol;
149 /* My slave address. This is initialized to IPMI_BMC_SLAVE_ADDR,
150 but may be changed by the user. */
151 unsigned char address;
153 /* My LUN. This should generally stay the SMS LUN, but just in
158 #ifdef CONFIG_PROC_FS
159 struct ipmi_proc_entry
162 struct ipmi_proc_entry *next;
168 struct platform_device *dev;
169 struct ipmi_device_id id;
170 unsigned char guid[16];
173 struct kref refcount;
175 /* bmc device attributes */
176 struct device_attribute device_id_attr;
177 struct device_attribute provides_dev_sdrs_attr;
178 struct device_attribute revision_attr;
179 struct device_attribute firmware_rev_attr;
180 struct device_attribute version_attr;
181 struct device_attribute add_dev_support_attr;
182 struct device_attribute manufacturer_id_attr;
183 struct device_attribute product_id_attr;
184 struct device_attribute guid_attr;
185 struct device_attribute aux_firmware_rev_attr;
188 #define IPMI_IPMB_NUM_SEQ 64
189 #define IPMI_MAX_CHANNELS 16
192 /* What interface number are we? */
195 struct kref refcount;
197 /* The list of upper layers that are using me. seq_lock
199 struct list_head users;
201 /* Used for wake ups at startup. */
202 wait_queue_head_t waitq;
204 struct bmc_device *bmc;
207 /* This is the lower-layer's sender routine. */
208 struct ipmi_smi_handlers *handlers;
211 #ifdef CONFIG_PROC_FS
212 /* A list of proc entries for this interface. This does not
213 need a lock, only one thread creates it and only one thread
215 spinlock_t proc_entry_lock;
216 struct ipmi_proc_entry *proc_entries;
219 /* Driver-model device for the system interface. */
220 struct device *si_dev;
222 /* A table of sequence numbers for this interface. We use the
223 sequence numbers for IPMB messages that go out of the
224 interface to match them up with their responses. A routine
225 is called periodically to time the items in this list. */
227 struct seq_table seq_table[IPMI_IPMB_NUM_SEQ];
230 /* Messages that were delayed for some reason (out of memory,
231 for instance), will go in here to be processed later in a
232 periodic timer interrupt. */
233 spinlock_t waiting_msgs_lock;
234 struct list_head waiting_msgs;
236 /* The list of command receivers that are registered for commands
237 on this interface. */
238 struct mutex cmd_rcvrs_mutex;
239 struct list_head cmd_rcvrs;
241 /* Events that were queues because no one was there to receive
243 spinlock_t events_lock; /* For dealing with event stuff. */
244 struct list_head waiting_events;
245 unsigned int waiting_events_count; /* How many events in queue? */
247 /* The event receiver for my BMC, only really used at panic
248 shutdown as a place to store this. */
249 unsigned char event_receiver;
250 unsigned char event_receiver_lun;
251 unsigned char local_sel_device;
252 unsigned char local_event_generator;
254 /* A cheap hack, if this is non-null and a message to an
255 interface comes in with a NULL user, call this routine with
256 it. Note that the message will still be freed by the
257 caller. This only works on the system interface. */
258 void (*null_user_handler)(ipmi_smi_t intf, struct ipmi_recv_msg *msg);
260 /* When we are scanning the channels for an SMI, this will
261 tell which channel we are scanning. */
264 /* Channel information */
265 struct ipmi_channel channels[IPMI_MAX_CHANNELS];
268 struct proc_dir_entry *proc_dir;
269 char proc_dir_name[10];
271 spinlock_t counter_lock; /* For making counters atomic. */
273 /* Commands we got that were invalid. */
274 unsigned int sent_invalid_commands;
276 /* Commands we sent to the MC. */
277 unsigned int sent_local_commands;
278 /* Responses from the MC that were delivered to a user. */
279 unsigned int handled_local_responses;
280 /* Responses from the MC that were not delivered to a user. */
281 unsigned int unhandled_local_responses;
283 /* Commands we sent out to the IPMB bus. */
284 unsigned int sent_ipmb_commands;
285 /* Commands sent on the IPMB that had errors on the SEND CMD */
286 unsigned int sent_ipmb_command_errs;
287 /* Each retransmit increments this count. */
288 unsigned int retransmitted_ipmb_commands;
289 /* When a message times out (runs out of retransmits) this is
291 unsigned int timed_out_ipmb_commands;
293 /* This is like above, but for broadcasts. Broadcasts are
294 *not* included in the above count (they are expected to
296 unsigned int timed_out_ipmb_broadcasts;
298 /* Responses I have sent to the IPMB bus. */
299 unsigned int sent_ipmb_responses;
301 /* The response was delivered to the user. */
302 unsigned int handled_ipmb_responses;
303 /* The response had invalid data in it. */
304 unsigned int invalid_ipmb_responses;
305 /* The response didn't have anyone waiting for it. */
306 unsigned int unhandled_ipmb_responses;
308 /* Commands we sent out to the IPMB bus. */
309 unsigned int sent_lan_commands;
310 /* Commands sent on the IPMB that had errors on the SEND CMD */
311 unsigned int sent_lan_command_errs;
312 /* Each retransmit increments this count. */
313 unsigned int retransmitted_lan_commands;
314 /* When a message times out (runs out of retransmits) this is
316 unsigned int timed_out_lan_commands;
318 /* Responses I have sent to the IPMB bus. */
319 unsigned int sent_lan_responses;
321 /* The response was delivered to the user. */
322 unsigned int handled_lan_responses;
323 /* The response had invalid data in it. */
324 unsigned int invalid_lan_responses;
325 /* The response didn't have anyone waiting for it. */
326 unsigned int unhandled_lan_responses;
328 /* The command was delivered to the user. */
329 unsigned int handled_commands;
330 /* The command had invalid data in it. */
331 unsigned int invalid_commands;
332 /* The command didn't have anyone waiting for it. */
333 unsigned int unhandled_commands;
335 /* Invalid data in an event. */
336 unsigned int invalid_events;
337 /* Events that were received with the proper format. */
340 #define to_si_intf_from_dev(device) container_of(device, struct ipmi_smi, dev)
342 /* Used to mark an interface entry that cannot be used but is not a
343 * free entry, either, primarily used at creation and deletion time so
344 * a slot doesn't get reused too quickly. */
345 #define IPMI_INVALID_INTERFACE_ENTRY ((ipmi_smi_t) ((long) 1))
346 #define IPMI_INVALID_INTERFACE(i) (((i) == NULL) \
347 || (i == IPMI_INVALID_INTERFACE_ENTRY))
350 * The driver model view of the IPMI messaging driver.
352 static struct device_driver ipmidriver = {
354 .bus = &platform_bus_type
356 static DEFINE_MUTEX(ipmidriver_mutex);
358 #define MAX_IPMI_INTERFACES 4
359 static ipmi_smi_t ipmi_interfaces[MAX_IPMI_INTERFACES];
361 /* Directly protects the ipmi_interfaces data structure. */
362 static DEFINE_SPINLOCK(interfaces_lock);
364 /* List of watchers that want to know when smi's are added and
366 static struct list_head smi_watchers = LIST_HEAD_INIT(smi_watchers);
367 static DECLARE_RWSEM(smi_watchers_sem);
370 static void free_recv_msg_list(struct list_head *q)
372 struct ipmi_recv_msg *msg, *msg2;
374 list_for_each_entry_safe(msg, msg2, q, link) {
375 list_del(&msg->link);
376 ipmi_free_recv_msg(msg);
380 static void clean_up_interface_data(ipmi_smi_t intf)
383 struct cmd_rcvr *rcvr, *rcvr2;
384 struct list_head list;
386 free_recv_msg_list(&intf->waiting_msgs);
387 free_recv_msg_list(&intf->waiting_events);
389 /* Wholesale remove all the entries from the list in the
390 * interface and wait for RCU to know that none are in use. */
391 mutex_lock(&intf->cmd_rcvrs_mutex);
392 list_add_rcu(&list, &intf->cmd_rcvrs);
393 list_del_rcu(&intf->cmd_rcvrs);
394 mutex_unlock(&intf->cmd_rcvrs_mutex);
397 list_for_each_entry_safe(rcvr, rcvr2, &list, link)
400 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
401 if ((intf->seq_table[i].inuse)
402 && (intf->seq_table[i].recv_msg))
404 ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
409 static void intf_free(struct kref *ref)
411 ipmi_smi_t intf = container_of(ref, struct ipmi_smi, refcount);
413 clean_up_interface_data(intf);
417 int ipmi_smi_watcher_register(struct ipmi_smi_watcher *watcher)
422 down_write(&smi_watchers_sem);
423 list_add(&(watcher->link), &smi_watchers);
424 up_write(&smi_watchers_sem);
425 spin_lock_irqsave(&interfaces_lock, flags);
426 for (i = 0; i < MAX_IPMI_INTERFACES; i++) {
427 ipmi_smi_t intf = ipmi_interfaces[i];
428 if (IPMI_INVALID_INTERFACE(intf))
430 spin_unlock_irqrestore(&interfaces_lock, flags);
431 watcher->new_smi(i, intf->si_dev);
432 spin_lock_irqsave(&interfaces_lock, flags);
434 spin_unlock_irqrestore(&interfaces_lock, flags);
438 int ipmi_smi_watcher_unregister(struct ipmi_smi_watcher *watcher)
440 down_write(&smi_watchers_sem);
441 list_del(&(watcher->link));
442 up_write(&smi_watchers_sem);
447 call_smi_watchers(int i, struct device *dev)
449 struct ipmi_smi_watcher *w;
451 down_read(&smi_watchers_sem);
452 list_for_each_entry(w, &smi_watchers, link) {
453 if (try_module_get(w->owner)) {
455 module_put(w->owner);
458 up_read(&smi_watchers_sem);
462 ipmi_addr_equal(struct ipmi_addr *addr1, struct ipmi_addr *addr2)
464 if (addr1->addr_type != addr2->addr_type)
467 if (addr1->channel != addr2->channel)
470 if (addr1->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
471 struct ipmi_system_interface_addr *smi_addr1
472 = (struct ipmi_system_interface_addr *) addr1;
473 struct ipmi_system_interface_addr *smi_addr2
474 = (struct ipmi_system_interface_addr *) addr2;
475 return (smi_addr1->lun == smi_addr2->lun);
478 if ((addr1->addr_type == IPMI_IPMB_ADDR_TYPE)
479 || (addr1->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE))
481 struct ipmi_ipmb_addr *ipmb_addr1
482 = (struct ipmi_ipmb_addr *) addr1;
483 struct ipmi_ipmb_addr *ipmb_addr2
484 = (struct ipmi_ipmb_addr *) addr2;
486 return ((ipmb_addr1->slave_addr == ipmb_addr2->slave_addr)
487 && (ipmb_addr1->lun == ipmb_addr2->lun));
490 if (addr1->addr_type == IPMI_LAN_ADDR_TYPE) {
491 struct ipmi_lan_addr *lan_addr1
492 = (struct ipmi_lan_addr *) addr1;
493 struct ipmi_lan_addr *lan_addr2
494 = (struct ipmi_lan_addr *) addr2;
496 return ((lan_addr1->remote_SWID == lan_addr2->remote_SWID)
497 && (lan_addr1->local_SWID == lan_addr2->local_SWID)
498 && (lan_addr1->session_handle
499 == lan_addr2->session_handle)
500 && (lan_addr1->lun == lan_addr2->lun));
506 int ipmi_validate_addr(struct ipmi_addr *addr, int len)
508 if (len < sizeof(struct ipmi_system_interface_addr)) {
512 if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
513 if (addr->channel != IPMI_BMC_CHANNEL)
518 if ((addr->channel == IPMI_BMC_CHANNEL)
519 || (addr->channel >= IPMI_MAX_CHANNELS)
520 || (addr->channel < 0))
523 if ((addr->addr_type == IPMI_IPMB_ADDR_TYPE)
524 || (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE))
526 if (len < sizeof(struct ipmi_ipmb_addr)) {
532 if (addr->addr_type == IPMI_LAN_ADDR_TYPE) {
533 if (len < sizeof(struct ipmi_lan_addr)) {
542 unsigned int ipmi_addr_length(int addr_type)
544 if (addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
545 return sizeof(struct ipmi_system_interface_addr);
547 if ((addr_type == IPMI_IPMB_ADDR_TYPE)
548 || (addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE))
550 return sizeof(struct ipmi_ipmb_addr);
553 if (addr_type == IPMI_LAN_ADDR_TYPE)
554 return sizeof(struct ipmi_lan_addr);
559 static void deliver_response(struct ipmi_recv_msg *msg)
562 ipmi_smi_t intf = msg->user_msg_data;
565 /* Special handling for NULL users. */
566 if (intf->null_user_handler) {
567 intf->null_user_handler(intf, msg);
568 spin_lock_irqsave(&intf->counter_lock, flags);
569 intf->handled_local_responses++;
570 spin_unlock_irqrestore(&intf->counter_lock, flags);
572 /* No handler, so give up. */
573 spin_lock_irqsave(&intf->counter_lock, flags);
574 intf->unhandled_local_responses++;
575 spin_unlock_irqrestore(&intf->counter_lock, flags);
577 ipmi_free_recv_msg(msg);
579 ipmi_user_t user = msg->user;
580 user->handler->ipmi_recv_hndl(msg, user->handler_data);
584 /* Find the next sequence number not being used and add the given
585 message with the given timeout to the sequence table. This must be
586 called with the interface's seq_lock held. */
587 static int intf_next_seq(ipmi_smi_t intf,
588 struct ipmi_recv_msg *recv_msg,
589 unsigned long timeout,
598 for (i = intf->curr_seq;
599 (i+1)%IPMI_IPMB_NUM_SEQ != intf->curr_seq;
600 i = (i+1)%IPMI_IPMB_NUM_SEQ)
602 if (!intf->seq_table[i].inuse)
606 if (!intf->seq_table[i].inuse) {
607 intf->seq_table[i].recv_msg = recv_msg;
609 /* Start with the maximum timeout, when the send response
610 comes in we will start the real timer. */
611 intf->seq_table[i].timeout = MAX_MSG_TIMEOUT;
612 intf->seq_table[i].orig_timeout = timeout;
613 intf->seq_table[i].retries_left = retries;
614 intf->seq_table[i].broadcast = broadcast;
615 intf->seq_table[i].inuse = 1;
616 intf->seq_table[i].seqid = NEXT_SEQID(intf->seq_table[i].seqid);
618 *seqid = intf->seq_table[i].seqid;
619 intf->curr_seq = (i+1)%IPMI_IPMB_NUM_SEQ;
627 /* Return the receive message for the given sequence number and
628 release the sequence number so it can be reused. Some other data
629 is passed in to be sure the message matches up correctly (to help
630 guard against message coming in after their timeout and the
631 sequence number being reused). */
632 static int intf_find_seq(ipmi_smi_t intf,
637 struct ipmi_addr *addr,
638 struct ipmi_recv_msg **recv_msg)
643 if (seq >= IPMI_IPMB_NUM_SEQ)
646 spin_lock_irqsave(&(intf->seq_lock), flags);
647 if (intf->seq_table[seq].inuse) {
648 struct ipmi_recv_msg *msg = intf->seq_table[seq].recv_msg;
650 if ((msg->addr.channel == channel)
651 && (msg->msg.cmd == cmd)
652 && (msg->msg.netfn == netfn)
653 && (ipmi_addr_equal(addr, &(msg->addr))))
656 intf->seq_table[seq].inuse = 0;
660 spin_unlock_irqrestore(&(intf->seq_lock), flags);
666 /* Start the timer for a specific sequence table entry. */
667 static int intf_start_seq_timer(ipmi_smi_t intf,
676 GET_SEQ_FROM_MSGID(msgid, seq, seqid);
678 spin_lock_irqsave(&(intf->seq_lock), flags);
679 /* We do this verification because the user can be deleted
680 while a message is outstanding. */
681 if ((intf->seq_table[seq].inuse)
682 && (intf->seq_table[seq].seqid == seqid))
684 struct seq_table *ent = &(intf->seq_table[seq]);
685 ent->timeout = ent->orig_timeout;
688 spin_unlock_irqrestore(&(intf->seq_lock), flags);
693 /* Got an error for the send message for a specific sequence number. */
694 static int intf_err_seq(ipmi_smi_t intf,
702 struct ipmi_recv_msg *msg = NULL;
705 GET_SEQ_FROM_MSGID(msgid, seq, seqid);
707 spin_lock_irqsave(&(intf->seq_lock), flags);
708 /* We do this verification because the user can be deleted
709 while a message is outstanding. */
710 if ((intf->seq_table[seq].inuse)
711 && (intf->seq_table[seq].seqid == seqid))
713 struct seq_table *ent = &(intf->seq_table[seq]);
719 spin_unlock_irqrestore(&(intf->seq_lock), flags);
722 msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
723 msg->msg_data[0] = err;
724 msg->msg.netfn |= 1; /* Convert to a response. */
725 msg->msg.data_len = 1;
726 msg->msg.data = msg->msg_data;
727 deliver_response(msg);
734 int ipmi_create_user(unsigned int if_num,
735 struct ipmi_user_hndl *handler,
740 ipmi_user_t new_user;
744 /* There is no module usecount here, because it's not
745 required. Since this can only be used by and called from
746 other modules, they will implicitly use this module, and
747 thus this can't be removed unless the other modules are
753 /* Make sure the driver is actually initialized, this handles
754 problems with initialization order. */
756 rv = ipmi_init_msghandler();
760 /* The init code doesn't return an error if it was turned
761 off, but it won't initialize. Check that. */
766 new_user = kmalloc(sizeof(*new_user), GFP_KERNEL);
770 spin_lock_irqsave(&interfaces_lock, flags);
771 intf = ipmi_interfaces[if_num];
772 if ((if_num >= MAX_IPMI_INTERFACES) || IPMI_INVALID_INTERFACE(intf)) {
773 spin_unlock_irqrestore(&interfaces_lock, flags);
778 /* Note that each existing user holds a refcount to the interface. */
779 kref_get(&intf->refcount);
780 spin_unlock_irqrestore(&interfaces_lock, flags);
782 kref_init(&new_user->refcount);
783 new_user->handler = handler;
784 new_user->handler_data = handler_data;
785 new_user->intf = intf;
786 new_user->gets_events = 0;
788 if (!try_module_get(intf->handlers->owner)) {
793 if (intf->handlers->inc_usecount) {
794 rv = intf->handlers->inc_usecount(intf->send_info);
796 module_put(intf->handlers->owner);
802 spin_lock_irqsave(&intf->seq_lock, flags);
803 list_add_rcu(&new_user->link, &intf->users);
804 spin_unlock_irqrestore(&intf->seq_lock, flags);
809 kref_put(&intf->refcount, intf_free);
815 static void free_user(struct kref *ref)
817 ipmi_user_t user = container_of(ref, struct ipmi_user, refcount);
821 int ipmi_destroy_user(ipmi_user_t user)
823 ipmi_smi_t intf = user->intf;
826 struct cmd_rcvr *rcvr;
827 struct cmd_rcvr *rcvrs = NULL;
831 /* Remove the user from the interface's sequence table. */
832 spin_lock_irqsave(&intf->seq_lock, flags);
833 list_del_rcu(&user->link);
835 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
836 if (intf->seq_table[i].inuse
837 && (intf->seq_table[i].recv_msg->user == user))
839 intf->seq_table[i].inuse = 0;
842 spin_unlock_irqrestore(&intf->seq_lock, flags);
845 * Remove the user from the command receiver's table. First
846 * we build a list of everything (not using the standard link,
847 * since other things may be using it till we do
848 * synchronize_rcu()) then free everything in that list.
850 mutex_lock(&intf->cmd_rcvrs_mutex);
851 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
852 if (rcvr->user == user) {
853 list_del_rcu(&rcvr->link);
858 mutex_unlock(&intf->cmd_rcvrs_mutex);
866 module_put(intf->handlers->owner);
867 if (intf->handlers->dec_usecount)
868 intf->handlers->dec_usecount(intf->send_info);
870 kref_put(&intf->refcount, intf_free);
872 kref_put(&user->refcount, free_user);
877 void ipmi_get_version(ipmi_user_t user,
878 unsigned char *major,
879 unsigned char *minor)
881 *major = ipmi_version_major(&user->intf->bmc->id);
882 *minor = ipmi_version_minor(&user->intf->bmc->id);
885 int ipmi_set_my_address(ipmi_user_t user,
886 unsigned int channel,
887 unsigned char address)
889 if (channel >= IPMI_MAX_CHANNELS)
891 user->intf->channels[channel].address = address;
895 int ipmi_get_my_address(ipmi_user_t user,
896 unsigned int channel,
897 unsigned char *address)
899 if (channel >= IPMI_MAX_CHANNELS)
901 *address = user->intf->channels[channel].address;
905 int ipmi_set_my_LUN(ipmi_user_t user,
906 unsigned int channel,
909 if (channel >= IPMI_MAX_CHANNELS)
911 user->intf->channels[channel].lun = LUN & 0x3;
915 int ipmi_get_my_LUN(ipmi_user_t user,
916 unsigned int channel,
917 unsigned char *address)
919 if (channel >= IPMI_MAX_CHANNELS)
921 *address = user->intf->channels[channel].lun;
925 int ipmi_set_gets_events(ipmi_user_t user, int val)
928 ipmi_smi_t intf = user->intf;
929 struct ipmi_recv_msg *msg, *msg2;
930 struct list_head msgs;
932 INIT_LIST_HEAD(&msgs);
934 spin_lock_irqsave(&intf->events_lock, flags);
935 user->gets_events = val;
938 /* Deliver any queued events. */
939 list_for_each_entry_safe(msg, msg2, &intf->waiting_events,
941 list_del(&msg->link);
942 list_add_tail(&msg->link, &msgs);
944 intf->waiting_events_count = 0;
947 /* Hold the events lock while doing this to preserve order. */
948 list_for_each_entry_safe(msg, msg2, &msgs, link) {
950 kref_get(&user->refcount);
951 deliver_response(msg);
954 spin_unlock_irqrestore(&intf->events_lock, flags);
959 static struct cmd_rcvr *find_cmd_rcvr(ipmi_smi_t intf,
963 struct cmd_rcvr *rcvr;
965 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
966 if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd))
972 int ipmi_register_for_cmd(ipmi_user_t user,
976 ipmi_smi_t intf = user->intf;
977 struct cmd_rcvr *rcvr;
978 struct cmd_rcvr *entry;
982 rcvr = kmalloc(sizeof(*rcvr), GFP_KERNEL);
989 mutex_lock(&intf->cmd_rcvrs_mutex);
990 /* Make sure the command/netfn is not already registered. */
991 entry = find_cmd_rcvr(intf, netfn, cmd);
997 list_add_rcu(&rcvr->link, &intf->cmd_rcvrs);
1000 mutex_unlock(&intf->cmd_rcvrs_mutex);
1007 int ipmi_unregister_for_cmd(ipmi_user_t user,
1008 unsigned char netfn,
1011 ipmi_smi_t intf = user->intf;
1012 struct cmd_rcvr *rcvr;
1014 mutex_lock(&intf->cmd_rcvrs_mutex);
1015 /* Make sure the command/netfn is not already registered. */
1016 rcvr = find_cmd_rcvr(intf, netfn, cmd);
1017 if ((rcvr) && (rcvr->user == user)) {
1018 list_del_rcu(&rcvr->link);
1019 mutex_unlock(&intf->cmd_rcvrs_mutex);
1024 mutex_unlock(&intf->cmd_rcvrs_mutex);
1029 void ipmi_user_set_run_to_completion(ipmi_user_t user, int val)
1031 ipmi_smi_t intf = user->intf;
1032 intf->handlers->set_run_to_completion(intf->send_info, val);
1035 static unsigned char
1036 ipmb_checksum(unsigned char *data, int size)
1038 unsigned char csum = 0;
1040 for (; size > 0; size--, data++)
1046 static inline void format_ipmb_msg(struct ipmi_smi_msg *smi_msg,
1047 struct kernel_ipmi_msg *msg,
1048 struct ipmi_ipmb_addr *ipmb_addr,
1050 unsigned char ipmb_seq,
1052 unsigned char source_address,
1053 unsigned char source_lun)
1057 /* Format the IPMB header data. */
1058 smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
1059 smi_msg->data[1] = IPMI_SEND_MSG_CMD;
1060 smi_msg->data[2] = ipmb_addr->channel;
1062 smi_msg->data[3] = 0;
1063 smi_msg->data[i+3] = ipmb_addr->slave_addr;
1064 smi_msg->data[i+4] = (msg->netfn << 2) | (ipmb_addr->lun & 0x3);
1065 smi_msg->data[i+5] = ipmb_checksum(&(smi_msg->data[i+3]), 2);
1066 smi_msg->data[i+6] = source_address;
1067 smi_msg->data[i+7] = (ipmb_seq << 2) | source_lun;
1068 smi_msg->data[i+8] = msg->cmd;
1070 /* Now tack on the data to the message. */
1071 if (msg->data_len > 0)
1072 memcpy(&(smi_msg->data[i+9]), msg->data,
1074 smi_msg->data_size = msg->data_len + 9;
1076 /* Now calculate the checksum and tack it on. */
1077 smi_msg->data[i+smi_msg->data_size]
1078 = ipmb_checksum(&(smi_msg->data[i+6]),
1079 smi_msg->data_size-6);
1081 /* Add on the checksum size and the offset from the
1083 smi_msg->data_size += 1 + i;
1085 smi_msg->msgid = msgid;
1088 static inline void format_lan_msg(struct ipmi_smi_msg *smi_msg,
1089 struct kernel_ipmi_msg *msg,
1090 struct ipmi_lan_addr *lan_addr,
1092 unsigned char ipmb_seq,
1093 unsigned char source_lun)
1095 /* Format the IPMB header data. */
1096 smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
1097 smi_msg->data[1] = IPMI_SEND_MSG_CMD;
1098 smi_msg->data[2] = lan_addr->channel;
1099 smi_msg->data[3] = lan_addr->session_handle;
1100 smi_msg->data[4] = lan_addr->remote_SWID;
1101 smi_msg->data[5] = (msg->netfn << 2) | (lan_addr->lun & 0x3);
1102 smi_msg->data[6] = ipmb_checksum(&(smi_msg->data[4]), 2);
1103 smi_msg->data[7] = lan_addr->local_SWID;
1104 smi_msg->data[8] = (ipmb_seq << 2) | source_lun;
1105 smi_msg->data[9] = msg->cmd;
1107 /* Now tack on the data to the message. */
1108 if (msg->data_len > 0)
1109 memcpy(&(smi_msg->data[10]), msg->data,
1111 smi_msg->data_size = msg->data_len + 10;
1113 /* Now calculate the checksum and tack it on. */
1114 smi_msg->data[smi_msg->data_size]
1115 = ipmb_checksum(&(smi_msg->data[7]),
1116 smi_msg->data_size-7);
1118 /* Add on the checksum size and the offset from the
1120 smi_msg->data_size += 1;
1122 smi_msg->msgid = msgid;
1125 /* Separate from ipmi_request so that the user does not have to be
1126 supplied in certain circumstances (mainly at panic time). If
1127 messages are supplied, they will be freed, even if an error
1129 static int i_ipmi_request(ipmi_user_t user,
1131 struct ipmi_addr *addr,
1133 struct kernel_ipmi_msg *msg,
1134 void *user_msg_data,
1136 struct ipmi_recv_msg *supplied_recv,
1138 unsigned char source_address,
1139 unsigned char source_lun,
1141 unsigned int retry_time_ms)
1144 struct ipmi_smi_msg *smi_msg;
1145 struct ipmi_recv_msg *recv_msg;
1146 unsigned long flags;
1149 if (supplied_recv) {
1150 recv_msg = supplied_recv;
1152 recv_msg = ipmi_alloc_recv_msg();
1153 if (recv_msg == NULL) {
1157 recv_msg->user_msg_data = user_msg_data;
1160 smi_msg = (struct ipmi_smi_msg *) supplied_smi;
1162 smi_msg = ipmi_alloc_smi_msg();
1163 if (smi_msg == NULL) {
1164 ipmi_free_recv_msg(recv_msg);
1169 recv_msg->user = user;
1171 kref_get(&user->refcount);
1172 recv_msg->msgid = msgid;
1173 /* Store the message to send in the receive message so timeout
1174 responses can get the proper response data. */
1175 recv_msg->msg = *msg;
1177 if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
1178 struct ipmi_system_interface_addr *smi_addr;
1180 if (msg->netfn & 1) {
1181 /* Responses are not allowed to the SMI. */
1186 smi_addr = (struct ipmi_system_interface_addr *) addr;
1187 if (smi_addr->lun > 3) {
1188 spin_lock_irqsave(&intf->counter_lock, flags);
1189 intf->sent_invalid_commands++;
1190 spin_unlock_irqrestore(&intf->counter_lock, flags);
1195 memcpy(&recv_msg->addr, smi_addr, sizeof(*smi_addr));
1197 if ((msg->netfn == IPMI_NETFN_APP_REQUEST)
1198 && ((msg->cmd == IPMI_SEND_MSG_CMD)
1199 || (msg->cmd == IPMI_GET_MSG_CMD)
1200 || (msg->cmd == IPMI_READ_EVENT_MSG_BUFFER_CMD)))
1202 /* We don't let the user do these, since we manage
1203 the sequence numbers. */
1204 spin_lock_irqsave(&intf->counter_lock, flags);
1205 intf->sent_invalid_commands++;
1206 spin_unlock_irqrestore(&intf->counter_lock, flags);
1211 if ((msg->data_len + 2) > IPMI_MAX_MSG_LENGTH) {
1212 spin_lock_irqsave(&intf->counter_lock, flags);
1213 intf->sent_invalid_commands++;
1214 spin_unlock_irqrestore(&intf->counter_lock, flags);
1219 smi_msg->data[0] = (msg->netfn << 2) | (smi_addr->lun & 0x3);
1220 smi_msg->data[1] = msg->cmd;
1221 smi_msg->msgid = msgid;
1222 smi_msg->user_data = recv_msg;
1223 if (msg->data_len > 0)
1224 memcpy(&(smi_msg->data[2]), msg->data, msg->data_len);
1225 smi_msg->data_size = msg->data_len + 2;
1226 spin_lock_irqsave(&intf->counter_lock, flags);
1227 intf->sent_local_commands++;
1228 spin_unlock_irqrestore(&intf->counter_lock, flags);
1229 } else if ((addr->addr_type == IPMI_IPMB_ADDR_TYPE)
1230 || (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE))
1232 struct ipmi_ipmb_addr *ipmb_addr;
1233 unsigned char ipmb_seq;
1237 if (addr->channel >= IPMI_MAX_CHANNELS) {
1238 spin_lock_irqsave(&intf->counter_lock, flags);
1239 intf->sent_invalid_commands++;
1240 spin_unlock_irqrestore(&intf->counter_lock, flags);
1245 if (intf->channels[addr->channel].medium
1246 != IPMI_CHANNEL_MEDIUM_IPMB)
1248 spin_lock_irqsave(&intf->counter_lock, flags);
1249 intf->sent_invalid_commands++;
1250 spin_unlock_irqrestore(&intf->counter_lock, flags);
1256 if (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE)
1257 retries = 0; /* Don't retry broadcasts. */
1261 if (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE) {
1262 /* Broadcasts add a zero at the beginning of the
1263 message, but otherwise is the same as an IPMB
1265 addr->addr_type = IPMI_IPMB_ADDR_TYPE;
1270 /* Default to 1 second retries. */
1271 if (retry_time_ms == 0)
1272 retry_time_ms = 1000;
1274 /* 9 for the header and 1 for the checksum, plus
1275 possibly one for the broadcast. */
1276 if ((msg->data_len + 10 + broadcast) > IPMI_MAX_MSG_LENGTH) {
1277 spin_lock_irqsave(&intf->counter_lock, flags);
1278 intf->sent_invalid_commands++;
1279 spin_unlock_irqrestore(&intf->counter_lock, flags);
1284 ipmb_addr = (struct ipmi_ipmb_addr *) addr;
1285 if (ipmb_addr->lun > 3) {
1286 spin_lock_irqsave(&intf->counter_lock, flags);
1287 intf->sent_invalid_commands++;
1288 spin_unlock_irqrestore(&intf->counter_lock, flags);
1293 memcpy(&recv_msg->addr, ipmb_addr, sizeof(*ipmb_addr));
1295 if (recv_msg->msg.netfn & 0x1) {
1296 /* It's a response, so use the user's sequence
1298 spin_lock_irqsave(&intf->counter_lock, flags);
1299 intf->sent_ipmb_responses++;
1300 spin_unlock_irqrestore(&intf->counter_lock, flags);
1301 format_ipmb_msg(smi_msg, msg, ipmb_addr, msgid,
1303 source_address, source_lun);
1305 /* Save the receive message so we can use it
1306 to deliver the response. */
1307 smi_msg->user_data = recv_msg;
1309 /* It's a command, so get a sequence for it. */
1311 spin_lock_irqsave(&(intf->seq_lock), flags);
1313 spin_lock(&intf->counter_lock);
1314 intf->sent_ipmb_commands++;
1315 spin_unlock(&intf->counter_lock);
1317 /* Create a sequence number with a 1 second
1318 timeout and 4 retries. */
1319 rv = intf_next_seq(intf,
1327 /* We have used up all the sequence numbers,
1328 probably, so abort. */
1329 spin_unlock_irqrestore(&(intf->seq_lock),
1334 /* Store the sequence number in the message,
1335 so that when the send message response
1336 comes back we can start the timer. */
1337 format_ipmb_msg(smi_msg, msg, ipmb_addr,
1338 STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
1339 ipmb_seq, broadcast,
1340 source_address, source_lun);
1342 /* Copy the message into the recv message data, so we
1343 can retransmit it later if necessary. */
1344 memcpy(recv_msg->msg_data, smi_msg->data,
1345 smi_msg->data_size);
1346 recv_msg->msg.data = recv_msg->msg_data;
1347 recv_msg->msg.data_len = smi_msg->data_size;
1349 /* We don't unlock until here, because we need
1350 to copy the completed message into the
1351 recv_msg before we release the lock.
1352 Otherwise, race conditions may bite us. I
1353 know that's pretty paranoid, but I prefer
1355 spin_unlock_irqrestore(&(intf->seq_lock), flags);
1357 } else if (addr->addr_type == IPMI_LAN_ADDR_TYPE) {
1358 struct ipmi_lan_addr *lan_addr;
1359 unsigned char ipmb_seq;
1362 if (addr->channel >= IPMI_MAX_CHANNELS) {
1363 spin_lock_irqsave(&intf->counter_lock, flags);
1364 intf->sent_invalid_commands++;
1365 spin_unlock_irqrestore(&intf->counter_lock, flags);
1370 if ((intf->channels[addr->channel].medium
1371 != IPMI_CHANNEL_MEDIUM_8023LAN)
1372 && (intf->channels[addr->channel].medium
1373 != IPMI_CHANNEL_MEDIUM_ASYNC))
1375 spin_lock_irqsave(&intf->counter_lock, flags);
1376 intf->sent_invalid_commands++;
1377 spin_unlock_irqrestore(&intf->counter_lock, flags);
1384 /* Default to 1 second retries. */
1385 if (retry_time_ms == 0)
1386 retry_time_ms = 1000;
1388 /* 11 for the header and 1 for the checksum. */
1389 if ((msg->data_len + 12) > IPMI_MAX_MSG_LENGTH) {
1390 spin_lock_irqsave(&intf->counter_lock, flags);
1391 intf->sent_invalid_commands++;
1392 spin_unlock_irqrestore(&intf->counter_lock, flags);
1397 lan_addr = (struct ipmi_lan_addr *) addr;
1398 if (lan_addr->lun > 3) {
1399 spin_lock_irqsave(&intf->counter_lock, flags);
1400 intf->sent_invalid_commands++;
1401 spin_unlock_irqrestore(&intf->counter_lock, flags);
1406 memcpy(&recv_msg->addr, lan_addr, sizeof(*lan_addr));
1408 if (recv_msg->msg.netfn & 0x1) {
1409 /* It's a response, so use the user's sequence
1411 spin_lock_irqsave(&intf->counter_lock, flags);
1412 intf->sent_lan_responses++;
1413 spin_unlock_irqrestore(&intf->counter_lock, flags);
1414 format_lan_msg(smi_msg, msg, lan_addr, msgid,
1417 /* Save the receive message so we can use it
1418 to deliver the response. */
1419 smi_msg->user_data = recv_msg;
1421 /* It's a command, so get a sequence for it. */
1423 spin_lock_irqsave(&(intf->seq_lock), flags);
1425 spin_lock(&intf->counter_lock);
1426 intf->sent_lan_commands++;
1427 spin_unlock(&intf->counter_lock);
1429 /* Create a sequence number with a 1 second
1430 timeout and 4 retries. */
1431 rv = intf_next_seq(intf,
1439 /* We have used up all the sequence numbers,
1440 probably, so abort. */
1441 spin_unlock_irqrestore(&(intf->seq_lock),
1446 /* Store the sequence number in the message,
1447 so that when the send message response
1448 comes back we can start the timer. */
1449 format_lan_msg(smi_msg, msg, lan_addr,
1450 STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
1451 ipmb_seq, source_lun);
1453 /* Copy the message into the recv message data, so we
1454 can retransmit it later if necessary. */
1455 memcpy(recv_msg->msg_data, smi_msg->data,
1456 smi_msg->data_size);
1457 recv_msg->msg.data = recv_msg->msg_data;
1458 recv_msg->msg.data_len = smi_msg->data_size;
1460 /* We don't unlock until here, because we need
1461 to copy the completed message into the
1462 recv_msg before we release the lock.
1463 Otherwise, race conditions may bite us. I
1464 know that's pretty paranoid, but I prefer
1466 spin_unlock_irqrestore(&(intf->seq_lock), flags);
1469 /* Unknown address type. */
1470 spin_lock_irqsave(&intf->counter_lock, flags);
1471 intf->sent_invalid_commands++;
1472 spin_unlock_irqrestore(&intf->counter_lock, flags);
1480 for (m = 0; m < smi_msg->data_size; m++)
1481 printk(" %2.2x", smi_msg->data[m]);
1485 intf->handlers->sender(intf->send_info, smi_msg, priority);
1490 ipmi_free_smi_msg(smi_msg);
1491 ipmi_free_recv_msg(recv_msg);
1495 static int check_addr(ipmi_smi_t intf,
1496 struct ipmi_addr *addr,
1497 unsigned char *saddr,
1500 if (addr->channel >= IPMI_MAX_CHANNELS)
1502 *lun = intf->channels[addr->channel].lun;
1503 *saddr = intf->channels[addr->channel].address;
1507 int ipmi_request_settime(ipmi_user_t user,
1508 struct ipmi_addr *addr,
1510 struct kernel_ipmi_msg *msg,
1511 void *user_msg_data,
1514 unsigned int retry_time_ms)
1516 unsigned char saddr, lun;
1521 rv = check_addr(user->intf, addr, &saddr, &lun);
1524 return i_ipmi_request(user,
1538 int ipmi_request_supply_msgs(ipmi_user_t user,
1539 struct ipmi_addr *addr,
1541 struct kernel_ipmi_msg *msg,
1542 void *user_msg_data,
1544 struct ipmi_recv_msg *supplied_recv,
1547 unsigned char saddr, lun;
1552 rv = check_addr(user->intf, addr, &saddr, &lun);
1555 return i_ipmi_request(user,
1569 static int ipmb_file_read_proc(char *page, char **start, off_t off,
1570 int count, int *eof, void *data)
1572 char *out = (char *) page;
1573 ipmi_smi_t intf = data;
1577 for (i = 0; i < IPMI_MAX_CHANNELS; i++)
1578 rv += sprintf(out+rv, "%x ", intf->channels[i].address);
1579 out[rv-1] = '\n'; /* Replace the final space with a newline */
1585 static int version_file_read_proc(char *page, char **start, off_t off,
1586 int count, int *eof, void *data)
1588 char *out = (char *) page;
1589 ipmi_smi_t intf = data;
1591 return sprintf(out, "%d.%d\n",
1592 ipmi_version_major(&intf->bmc->id),
1593 ipmi_version_minor(&intf->bmc->id));
1596 static int stat_file_read_proc(char *page, char **start, off_t off,
1597 int count, int *eof, void *data)
1599 char *out = (char *) page;
1600 ipmi_smi_t intf = data;
1602 out += sprintf(out, "sent_invalid_commands: %d\n",
1603 intf->sent_invalid_commands);
1604 out += sprintf(out, "sent_local_commands: %d\n",
1605 intf->sent_local_commands);
1606 out += sprintf(out, "handled_local_responses: %d\n",
1607 intf->handled_local_responses);
1608 out += sprintf(out, "unhandled_local_responses: %d\n",
1609 intf->unhandled_local_responses);
1610 out += sprintf(out, "sent_ipmb_commands: %d\n",
1611 intf->sent_ipmb_commands);
1612 out += sprintf(out, "sent_ipmb_command_errs: %d\n",
1613 intf->sent_ipmb_command_errs);
1614 out += sprintf(out, "retransmitted_ipmb_commands: %d\n",
1615 intf->retransmitted_ipmb_commands);
1616 out += sprintf(out, "timed_out_ipmb_commands: %d\n",
1617 intf->timed_out_ipmb_commands);
1618 out += sprintf(out, "timed_out_ipmb_broadcasts: %d\n",
1619 intf->timed_out_ipmb_broadcasts);
1620 out += sprintf(out, "sent_ipmb_responses: %d\n",
1621 intf->sent_ipmb_responses);
1622 out += sprintf(out, "handled_ipmb_responses: %d\n",
1623 intf->handled_ipmb_responses);
1624 out += sprintf(out, "invalid_ipmb_responses: %d\n",
1625 intf->invalid_ipmb_responses);
1626 out += sprintf(out, "unhandled_ipmb_responses: %d\n",
1627 intf->unhandled_ipmb_responses);
1628 out += sprintf(out, "sent_lan_commands: %d\n",
1629 intf->sent_lan_commands);
1630 out += sprintf(out, "sent_lan_command_errs: %d\n",
1631 intf->sent_lan_command_errs);
1632 out += sprintf(out, "retransmitted_lan_commands: %d\n",
1633 intf->retransmitted_lan_commands);
1634 out += sprintf(out, "timed_out_lan_commands: %d\n",
1635 intf->timed_out_lan_commands);
1636 out += sprintf(out, "sent_lan_responses: %d\n",
1637 intf->sent_lan_responses);
1638 out += sprintf(out, "handled_lan_responses: %d\n",
1639 intf->handled_lan_responses);
1640 out += sprintf(out, "invalid_lan_responses: %d\n",
1641 intf->invalid_lan_responses);
1642 out += sprintf(out, "unhandled_lan_responses: %d\n",
1643 intf->unhandled_lan_responses);
1644 out += sprintf(out, "handled_commands: %d\n",
1645 intf->handled_commands);
1646 out += sprintf(out, "invalid_commands: %d\n",
1647 intf->invalid_commands);
1648 out += sprintf(out, "unhandled_commands: %d\n",
1649 intf->unhandled_commands);
1650 out += sprintf(out, "invalid_events: %d\n",
1651 intf->invalid_events);
1652 out += sprintf(out, "events: %d\n",
1655 return (out - ((char *) page));
1658 int ipmi_smi_add_proc_entry(ipmi_smi_t smi, char *name,
1659 read_proc_t *read_proc, write_proc_t *write_proc,
1660 void *data, struct module *owner)
1663 #ifdef CONFIG_PROC_FS
1664 struct proc_dir_entry *file;
1665 struct ipmi_proc_entry *entry;
1667 /* Create a list element. */
1668 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1671 entry->name = kmalloc(strlen(name)+1, GFP_KERNEL);
1676 strcpy(entry->name, name);
1678 file = create_proc_entry(name, 0, smi->proc_dir);
1686 file->read_proc = read_proc;
1687 file->write_proc = write_proc;
1688 file->owner = owner;
1690 spin_lock(&smi->proc_entry_lock);
1691 /* Stick it on the list. */
1692 entry->next = smi->proc_entries;
1693 smi->proc_entries = entry;
1694 spin_unlock(&smi->proc_entry_lock);
1696 #endif /* CONFIG_PROC_FS */
1701 static int add_proc_entries(ipmi_smi_t smi, int num)
1705 #ifdef CONFIG_PROC_FS
1706 sprintf(smi->proc_dir_name, "%d", num);
1707 smi->proc_dir = proc_mkdir(smi->proc_dir_name, proc_ipmi_root);
1711 smi->proc_dir->owner = THIS_MODULE;
1715 rv = ipmi_smi_add_proc_entry(smi, "stats",
1716 stat_file_read_proc, NULL,
1720 rv = ipmi_smi_add_proc_entry(smi, "ipmb",
1721 ipmb_file_read_proc, NULL,
1725 rv = ipmi_smi_add_proc_entry(smi, "version",
1726 version_file_read_proc, NULL,
1728 #endif /* CONFIG_PROC_FS */
1733 static void remove_proc_entries(ipmi_smi_t smi)
1735 #ifdef CONFIG_PROC_FS
1736 struct ipmi_proc_entry *entry;
1738 spin_lock(&smi->proc_entry_lock);
1739 while (smi->proc_entries) {
1740 entry = smi->proc_entries;
1741 smi->proc_entries = entry->next;
1743 remove_proc_entry(entry->name, smi->proc_dir);
1747 spin_unlock(&smi->proc_entry_lock);
1748 remove_proc_entry(smi->proc_dir_name, proc_ipmi_root);
1749 #endif /* CONFIG_PROC_FS */
1752 static int __find_bmc_guid(struct device *dev, void *data)
1754 unsigned char *id = data;
1755 struct bmc_device *bmc = dev_get_drvdata(dev);
1756 return memcmp(bmc->guid, id, 16) == 0;
1759 static struct bmc_device *ipmi_find_bmc_guid(struct device_driver *drv,
1760 unsigned char *guid)
1764 dev = driver_find_device(drv, NULL, guid, __find_bmc_guid);
1766 return dev_get_drvdata(dev);
1771 struct prod_dev_id {
1772 unsigned int product_id;
1773 unsigned char device_id;
1776 static int __find_bmc_prod_dev_id(struct device *dev, void *data)
1778 struct prod_dev_id *id = data;
1779 struct bmc_device *bmc = dev_get_drvdata(dev);
1781 return (bmc->id.product_id == id->product_id
1782 && bmc->id.product_id == id->product_id
1783 && bmc->id.device_id == id->device_id);
1786 static struct bmc_device *ipmi_find_bmc_prod_dev_id(
1787 struct device_driver *drv,
1788 unsigned char product_id, unsigned char device_id)
1790 struct prod_dev_id id = {
1791 .product_id = product_id,
1792 .device_id = device_id,
1796 dev = driver_find_device(drv, NULL, &id, __find_bmc_prod_dev_id);
1798 return dev_get_drvdata(dev);
1803 static ssize_t device_id_show(struct device *dev,
1804 struct device_attribute *attr,
1807 struct bmc_device *bmc = dev_get_drvdata(dev);
1809 return snprintf(buf, 10, "%u\n", bmc->id.device_id);
1812 static ssize_t provides_dev_sdrs_show(struct device *dev,
1813 struct device_attribute *attr,
1816 struct bmc_device *bmc = dev_get_drvdata(dev);
1818 return snprintf(buf, 10, "%u\n",
1819 bmc->id.device_revision && 0x80 >> 7);
1822 static ssize_t revision_show(struct device *dev, struct device_attribute *attr,
1825 struct bmc_device *bmc = dev_get_drvdata(dev);
1827 return snprintf(buf, 20, "%u\n",
1828 bmc->id.device_revision && 0x0F);
1831 static ssize_t firmware_rev_show(struct device *dev,
1832 struct device_attribute *attr,
1835 struct bmc_device *bmc = dev_get_drvdata(dev);
1837 return snprintf(buf, 20, "%u.%x\n", bmc->id.firmware_revision_1,
1838 bmc->id.firmware_revision_2);
1841 static ssize_t ipmi_version_show(struct device *dev,
1842 struct device_attribute *attr,
1845 struct bmc_device *bmc = dev_get_drvdata(dev);
1847 return snprintf(buf, 20, "%u.%u\n",
1848 ipmi_version_major(&bmc->id),
1849 ipmi_version_minor(&bmc->id));
1852 static ssize_t add_dev_support_show(struct device *dev,
1853 struct device_attribute *attr,
1856 struct bmc_device *bmc = dev_get_drvdata(dev);
1858 return snprintf(buf, 10, "0x%02x\n",
1859 bmc->id.additional_device_support);
1862 static ssize_t manufacturer_id_show(struct device *dev,
1863 struct device_attribute *attr,
1866 struct bmc_device *bmc = dev_get_drvdata(dev);
1868 return snprintf(buf, 20, "0x%6.6x\n", bmc->id.manufacturer_id);
1871 static ssize_t product_id_show(struct device *dev,
1872 struct device_attribute *attr,
1875 struct bmc_device *bmc = dev_get_drvdata(dev);
1877 return snprintf(buf, 10, "0x%4.4x\n", bmc->id.product_id);
1880 static ssize_t aux_firmware_rev_show(struct device *dev,
1881 struct device_attribute *attr,
1884 struct bmc_device *bmc = dev_get_drvdata(dev);
1886 return snprintf(buf, 21, "0x%02x 0x%02x 0x%02x 0x%02x\n",
1887 bmc->id.aux_firmware_revision[3],
1888 bmc->id.aux_firmware_revision[2],
1889 bmc->id.aux_firmware_revision[1],
1890 bmc->id.aux_firmware_revision[0]);
1893 static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
1896 struct bmc_device *bmc = dev_get_drvdata(dev);
1898 return snprintf(buf, 100, "%Lx%Lx\n",
1899 (long long) bmc->guid[0],
1900 (long long) bmc->guid[8]);
1904 cleanup_bmc_device(struct kref *ref)
1906 struct bmc_device *bmc;
1908 bmc = container_of(ref, struct bmc_device, refcount);
1910 device_remove_file(&bmc->dev->dev,
1911 &bmc->device_id_attr);
1912 device_remove_file(&bmc->dev->dev,
1913 &bmc->provides_dev_sdrs_attr);
1914 device_remove_file(&bmc->dev->dev,
1915 &bmc->revision_attr);
1916 device_remove_file(&bmc->dev->dev,
1917 &bmc->firmware_rev_attr);
1918 device_remove_file(&bmc->dev->dev,
1919 &bmc->version_attr);
1920 device_remove_file(&bmc->dev->dev,
1921 &bmc->add_dev_support_attr);
1922 device_remove_file(&bmc->dev->dev,
1923 &bmc->manufacturer_id_attr);
1924 device_remove_file(&bmc->dev->dev,
1925 &bmc->product_id_attr);
1926 if (bmc->id.aux_firmware_revision_set)
1927 device_remove_file(&bmc->dev->dev,
1928 &bmc->aux_firmware_rev_attr);
1930 device_remove_file(&bmc->dev->dev,
1932 platform_device_unregister(bmc->dev);
1936 static void ipmi_bmc_unregister(ipmi_smi_t intf)
1938 struct bmc_device *bmc = intf->bmc;
1940 sysfs_remove_link(&intf->si_dev->kobj, "bmc");
1941 if (intf->my_dev_name) {
1942 sysfs_remove_link(&bmc->dev->dev.kobj, intf->my_dev_name);
1943 kfree(intf->my_dev_name);
1944 intf->my_dev_name = NULL;
1947 mutex_lock(&ipmidriver_mutex);
1948 kref_put(&bmc->refcount, cleanup_bmc_device);
1949 mutex_unlock(&ipmidriver_mutex);
1952 static int ipmi_bmc_register(ipmi_smi_t intf)
1955 struct bmc_device *bmc = intf->bmc;
1956 struct bmc_device *old_bmc;
1960 mutex_lock(&ipmidriver_mutex);
1963 * Try to find if there is an bmc_device struct
1964 * representing the interfaced BMC already
1967 old_bmc = ipmi_find_bmc_guid(&ipmidriver, bmc->guid);
1969 old_bmc = ipmi_find_bmc_prod_dev_id(&ipmidriver,
1974 * If there is already an bmc_device, free the new one,
1975 * otherwise register the new BMC device
1979 intf->bmc = old_bmc;
1982 kref_get(&bmc->refcount);
1983 mutex_unlock(&ipmidriver_mutex);
1986 "ipmi: interfacing existing BMC (man_id: 0x%6.6x,"
1987 " prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
1988 bmc->id.manufacturer_id,
1992 bmc->dev = platform_device_alloc("ipmi_bmc",
1997 " Unable to allocate platform device\n");
2000 bmc->dev->dev.driver = &ipmidriver;
2001 dev_set_drvdata(&bmc->dev->dev, bmc);
2002 kref_init(&bmc->refcount);
2004 rv = platform_device_register(bmc->dev);
2005 mutex_unlock(&ipmidriver_mutex);
2009 " Unable to register bmc device: %d\n",
2011 /* Don't go to out_err, you can only do that if
2012 the device is registered already. */
2016 bmc->device_id_attr.attr.name = "device_id";
2017 bmc->device_id_attr.attr.owner = THIS_MODULE;
2018 bmc->device_id_attr.attr.mode = S_IRUGO;
2019 bmc->device_id_attr.show = device_id_show;
2021 bmc->provides_dev_sdrs_attr.attr.name = "provides_device_sdrs";
2022 bmc->provides_dev_sdrs_attr.attr.owner = THIS_MODULE;
2023 bmc->provides_dev_sdrs_attr.attr.mode = S_IRUGO;
2024 bmc->provides_dev_sdrs_attr.show = provides_dev_sdrs_show;
2027 bmc->revision_attr.attr.name = "revision";
2028 bmc->revision_attr.attr.owner = THIS_MODULE;
2029 bmc->revision_attr.attr.mode = S_IRUGO;
2030 bmc->revision_attr.show = revision_show;
2032 bmc->firmware_rev_attr.attr.name = "firmware_revision";
2033 bmc->firmware_rev_attr.attr.owner = THIS_MODULE;
2034 bmc->firmware_rev_attr.attr.mode = S_IRUGO;
2035 bmc->firmware_rev_attr.show = firmware_rev_show;
2037 bmc->version_attr.attr.name = "ipmi_version";
2038 bmc->version_attr.attr.owner = THIS_MODULE;
2039 bmc->version_attr.attr.mode = S_IRUGO;
2040 bmc->version_attr.show = ipmi_version_show;
2042 bmc->add_dev_support_attr.attr.name
2043 = "additional_device_support";
2044 bmc->add_dev_support_attr.attr.owner = THIS_MODULE;
2045 bmc->add_dev_support_attr.attr.mode = S_IRUGO;
2046 bmc->add_dev_support_attr.show = add_dev_support_show;
2048 bmc->manufacturer_id_attr.attr.name = "manufacturer_id";
2049 bmc->manufacturer_id_attr.attr.owner = THIS_MODULE;
2050 bmc->manufacturer_id_attr.attr.mode = S_IRUGO;
2051 bmc->manufacturer_id_attr.show = manufacturer_id_show;
2053 bmc->product_id_attr.attr.name = "product_id";
2054 bmc->product_id_attr.attr.owner = THIS_MODULE;
2055 bmc->product_id_attr.attr.mode = S_IRUGO;
2056 bmc->product_id_attr.show = product_id_show;
2058 bmc->guid_attr.attr.name = "guid";
2059 bmc->guid_attr.attr.owner = THIS_MODULE;
2060 bmc->guid_attr.attr.mode = S_IRUGO;
2061 bmc->guid_attr.show = guid_show;
2063 bmc->aux_firmware_rev_attr.attr.name = "aux_firmware_revision";
2064 bmc->aux_firmware_rev_attr.attr.owner = THIS_MODULE;
2065 bmc->aux_firmware_rev_attr.attr.mode = S_IRUGO;
2066 bmc->aux_firmware_rev_attr.show = aux_firmware_rev_show;
2068 device_create_file(&bmc->dev->dev,
2069 &bmc->device_id_attr);
2070 device_create_file(&bmc->dev->dev,
2071 &bmc->provides_dev_sdrs_attr);
2072 device_create_file(&bmc->dev->dev,
2073 &bmc->revision_attr);
2074 device_create_file(&bmc->dev->dev,
2075 &bmc->firmware_rev_attr);
2076 device_create_file(&bmc->dev->dev,
2077 &bmc->version_attr);
2078 device_create_file(&bmc->dev->dev,
2079 &bmc->add_dev_support_attr);
2080 device_create_file(&bmc->dev->dev,
2081 &bmc->manufacturer_id_attr);
2082 device_create_file(&bmc->dev->dev,
2083 &bmc->product_id_attr);
2084 if (bmc->id.aux_firmware_revision_set)
2085 device_create_file(&bmc->dev->dev,
2086 &bmc->aux_firmware_rev_attr);
2088 device_create_file(&bmc->dev->dev,
2092 "ipmi: Found new BMC (man_id: 0x%6.6x, "
2093 " prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
2094 bmc->id.manufacturer_id,
2100 * create symlink from system interface device to bmc device
2103 rv = sysfs_create_link(&intf->si_dev->kobj,
2104 &bmc->dev->dev.kobj, "bmc");
2107 "ipmi_msghandler: Unable to create bmc symlink: %d\n",
2112 size = snprintf(dummy, 0, "ipmi%d", intf->intf_num);
2113 intf->my_dev_name = kmalloc(size+1, GFP_KERNEL);
2114 if (!intf->my_dev_name) {
2117 "ipmi_msghandler: allocate link from BMC: %d\n",
2121 snprintf(intf->my_dev_name, size+1, "ipmi%d", intf->intf_num);
2123 rv = sysfs_create_link(&bmc->dev->dev.kobj, &intf->si_dev->kobj,
2126 kfree(intf->my_dev_name);
2127 intf->my_dev_name = NULL;
2130 " Unable to create symlink to bmc: %d\n",
2138 ipmi_bmc_unregister(intf);
2143 send_guid_cmd(ipmi_smi_t intf, int chan)
2145 struct kernel_ipmi_msg msg;
2146 struct ipmi_system_interface_addr si;
2148 si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2149 si.channel = IPMI_BMC_CHANNEL;
2152 msg.netfn = IPMI_NETFN_APP_REQUEST;
2153 msg.cmd = IPMI_GET_DEVICE_GUID_CMD;
2156 return i_ipmi_request(NULL,
2158 (struct ipmi_addr *) &si,
2165 intf->channels[0].address,
2166 intf->channels[0].lun,
2171 guid_handler(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
2173 if ((msg->addr.addr_type != IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
2174 || (msg->msg.netfn != IPMI_NETFN_APP_RESPONSE)
2175 || (msg->msg.cmd != IPMI_GET_DEVICE_GUID_CMD))
2179 if (msg->msg.data[0] != 0) {
2180 /* Error from getting the GUID, the BMC doesn't have one. */
2181 intf->bmc->guid_set = 0;
2185 if (msg->msg.data_len < 17) {
2186 intf->bmc->guid_set = 0;
2187 printk(KERN_WARNING PFX
2188 "guid_handler: The GUID response from the BMC was too"
2189 " short, it was %d but should have been 17. Assuming"
2190 " GUID is not available.\n",
2195 memcpy(intf->bmc->guid, msg->msg.data, 16);
2196 intf->bmc->guid_set = 1;
2198 wake_up(&intf->waitq);
2202 get_guid(ipmi_smi_t intf)
2206 intf->bmc->guid_set = 0x2;
2207 intf->null_user_handler = guid_handler;
2208 rv = send_guid_cmd(intf, 0);
2210 /* Send failed, no GUID available. */
2211 intf->bmc->guid_set = 0;
2212 wait_event(intf->waitq, intf->bmc->guid_set != 2);
2213 intf->null_user_handler = NULL;
2217 send_channel_info_cmd(ipmi_smi_t intf, int chan)
2219 struct kernel_ipmi_msg msg;
2220 unsigned char data[1];
2221 struct ipmi_system_interface_addr si;
2223 si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2224 si.channel = IPMI_BMC_CHANNEL;
2227 msg.netfn = IPMI_NETFN_APP_REQUEST;
2228 msg.cmd = IPMI_GET_CHANNEL_INFO_CMD;
2232 return i_ipmi_request(NULL,
2234 (struct ipmi_addr *) &si,
2241 intf->channels[0].address,
2242 intf->channels[0].lun,
2247 channel_handler(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
2252 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
2253 && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
2254 && (msg->msg.cmd == IPMI_GET_CHANNEL_INFO_CMD))
2256 /* It's the one we want */
2257 if (msg->msg.data[0] != 0) {
2258 /* Got an error from the channel, just go on. */
2260 if (msg->msg.data[0] == IPMI_INVALID_COMMAND_ERR) {
2261 /* If the MC does not support this
2262 command, that is legal. We just
2263 assume it has one IPMB at channel
2265 intf->channels[0].medium
2266 = IPMI_CHANNEL_MEDIUM_IPMB;
2267 intf->channels[0].protocol
2268 = IPMI_CHANNEL_PROTOCOL_IPMB;
2271 intf->curr_channel = IPMI_MAX_CHANNELS;
2272 wake_up(&intf->waitq);
2277 if (msg->msg.data_len < 4) {
2278 /* Message not big enough, just go on. */
2281 chan = intf->curr_channel;
2282 intf->channels[chan].medium = msg->msg.data[2] & 0x7f;
2283 intf->channels[chan].protocol = msg->msg.data[3] & 0x1f;
2286 intf->curr_channel++;
2287 if (intf->curr_channel >= IPMI_MAX_CHANNELS)
2288 wake_up(&intf->waitq);
2290 rv = send_channel_info_cmd(intf, intf->curr_channel);
2293 /* Got an error somehow, just give up. */
2294 intf->curr_channel = IPMI_MAX_CHANNELS;
2295 wake_up(&intf->waitq);
2297 printk(KERN_WARNING PFX
2298 "Error sending channel information: %d\n",
2306 int ipmi_register_smi(struct ipmi_smi_handlers *handlers,
2308 struct ipmi_device_id *device_id,
2309 struct device *si_dev,
2310 unsigned char slave_addr)
2315 unsigned long flags;
2319 version_major = ipmi_version_major(device_id);
2320 version_minor = ipmi_version_minor(device_id);
2322 /* Make sure the driver is actually initialized, this handles
2323 problems with initialization order. */
2325 rv = ipmi_init_msghandler();
2328 /* The init code doesn't return an error if it was turned
2329 off, but it won't initialize. Check that. */
2334 intf = kmalloc(sizeof(*intf), GFP_KERNEL);
2337 memset(intf, 0, sizeof(*intf));
2338 intf->bmc = kzalloc(sizeof(*intf->bmc), GFP_KERNEL);
2343 intf->intf_num = -1;
2344 kref_init(&intf->refcount);
2345 intf->bmc->id = *device_id;
2346 intf->si_dev = si_dev;
2347 for (j = 0; j < IPMI_MAX_CHANNELS; j++) {
2348 intf->channels[j].address = IPMI_BMC_SLAVE_ADDR;
2349 intf->channels[j].lun = 2;
2351 if (slave_addr != 0)
2352 intf->channels[0].address = slave_addr;
2353 INIT_LIST_HEAD(&intf->users);
2354 intf->handlers = handlers;
2355 intf->send_info = send_info;
2356 spin_lock_init(&intf->seq_lock);
2357 for (j = 0; j < IPMI_IPMB_NUM_SEQ; j++) {
2358 intf->seq_table[j].inuse = 0;
2359 intf->seq_table[j].seqid = 0;
2362 #ifdef CONFIG_PROC_FS
2363 spin_lock_init(&intf->proc_entry_lock);
2365 spin_lock_init(&intf->waiting_msgs_lock);
2366 INIT_LIST_HEAD(&intf->waiting_msgs);
2367 spin_lock_init(&intf->events_lock);
2368 INIT_LIST_HEAD(&intf->waiting_events);
2369 intf->waiting_events_count = 0;
2370 mutex_init(&intf->cmd_rcvrs_mutex);
2371 INIT_LIST_HEAD(&intf->cmd_rcvrs);
2372 init_waitqueue_head(&intf->waitq);
2374 spin_lock_init(&intf->counter_lock);
2375 intf->proc_dir = NULL;
2378 spin_lock_irqsave(&interfaces_lock, flags);
2379 for (i = 0; i < MAX_IPMI_INTERFACES; i++) {
2380 if (ipmi_interfaces[i] == NULL) {
2382 /* Reserve the entry till we are done. */
2383 ipmi_interfaces[i] = IPMI_INVALID_INTERFACE_ENTRY;
2388 spin_unlock_irqrestore(&interfaces_lock, flags);
2392 rv = handlers->start_processing(send_info, intf);
2398 if ((version_major > 1)
2399 || ((version_major == 1) && (version_minor >= 5)))
2401 /* Start scanning the channels to see what is
2403 intf->null_user_handler = channel_handler;
2404 intf->curr_channel = 0;
2405 rv = send_channel_info_cmd(intf, 0);
2409 /* Wait for the channel info to be read. */
2410 wait_event(intf->waitq,
2411 intf->curr_channel >= IPMI_MAX_CHANNELS);
2412 intf->null_user_handler = NULL;
2414 /* Assume a single IPMB channel at zero. */
2415 intf->channels[0].medium = IPMI_CHANNEL_MEDIUM_IPMB;
2416 intf->channels[0].protocol = IPMI_CHANNEL_PROTOCOL_IPMB;
2420 rv = add_proc_entries(intf, i);
2422 rv = ipmi_bmc_register(intf);
2427 remove_proc_entries(intf);
2428 kref_put(&intf->refcount, intf_free);
2429 if (i < MAX_IPMI_INTERFACES) {
2430 spin_lock_irqsave(&interfaces_lock, flags);
2431 ipmi_interfaces[i] = NULL;
2432 spin_unlock_irqrestore(&interfaces_lock, flags);
2435 spin_lock_irqsave(&interfaces_lock, flags);
2436 ipmi_interfaces[i] = intf;
2437 spin_unlock_irqrestore(&interfaces_lock, flags);
2438 call_smi_watchers(i, intf->si_dev);
2444 int ipmi_unregister_smi(ipmi_smi_t intf)
2447 struct ipmi_smi_watcher *w;
2448 unsigned long flags;
2450 ipmi_bmc_unregister(intf);
2452 spin_lock_irqsave(&interfaces_lock, flags);
2453 for (i = 0; i < MAX_IPMI_INTERFACES; i++) {
2454 if (ipmi_interfaces[i] == intf) {
2455 /* Set the interface number reserved until we
2457 ipmi_interfaces[i] = IPMI_INVALID_INTERFACE_ENTRY;
2458 intf->intf_num = -1;
2462 spin_unlock_irqrestore(&interfaces_lock,flags);
2464 if (i == MAX_IPMI_INTERFACES)
2467 remove_proc_entries(intf);
2469 /* Call all the watcher interfaces to tell them that
2470 an interface is gone. */
2471 down_read(&smi_watchers_sem);
2472 list_for_each_entry(w, &smi_watchers, link)
2474 up_read(&smi_watchers_sem);
2476 /* Allow the entry to be reused now. */
2477 spin_lock_irqsave(&interfaces_lock, flags);
2478 ipmi_interfaces[i] = NULL;
2479 spin_unlock_irqrestore(&interfaces_lock,flags);
2481 kref_put(&intf->refcount, intf_free);
2485 static int handle_ipmb_get_msg_rsp(ipmi_smi_t intf,
2486 struct ipmi_smi_msg *msg)
2488 struct ipmi_ipmb_addr ipmb_addr;
2489 struct ipmi_recv_msg *recv_msg;
2490 unsigned long flags;
2493 /* This is 11, not 10, because the response must contain a
2494 * completion code. */
2495 if (msg->rsp_size < 11) {
2496 /* Message not big enough, just ignore it. */
2497 spin_lock_irqsave(&intf->counter_lock, flags);
2498 intf->invalid_ipmb_responses++;
2499 spin_unlock_irqrestore(&intf->counter_lock, flags);
2503 if (msg->rsp[2] != 0) {
2504 /* An error getting the response, just ignore it. */
2508 ipmb_addr.addr_type = IPMI_IPMB_ADDR_TYPE;
2509 ipmb_addr.slave_addr = msg->rsp[6];
2510 ipmb_addr.channel = msg->rsp[3] & 0x0f;
2511 ipmb_addr.lun = msg->rsp[7] & 3;
2513 /* It's a response from a remote entity. Look up the sequence
2514 number and handle the response. */
2515 if (intf_find_seq(intf,
2519 (msg->rsp[4] >> 2) & (~1),
2520 (struct ipmi_addr *) &(ipmb_addr),
2523 /* We were unable to find the sequence number,
2524 so just nuke the message. */
2525 spin_lock_irqsave(&intf->counter_lock, flags);
2526 intf->unhandled_ipmb_responses++;
2527 spin_unlock_irqrestore(&intf->counter_lock, flags);
2531 memcpy(recv_msg->msg_data,
2534 /* THe other fields matched, so no need to set them, except
2535 for netfn, which needs to be the response that was
2536 returned, not the request value. */
2537 recv_msg->msg.netfn = msg->rsp[4] >> 2;
2538 recv_msg->msg.data = recv_msg->msg_data;
2539 recv_msg->msg.data_len = msg->rsp_size - 10;
2540 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
2541 spin_lock_irqsave(&intf->counter_lock, flags);
2542 intf->handled_ipmb_responses++;
2543 spin_unlock_irqrestore(&intf->counter_lock, flags);
2544 deliver_response(recv_msg);
2549 static int handle_ipmb_get_msg_cmd(ipmi_smi_t intf,
2550 struct ipmi_smi_msg *msg)
2552 struct cmd_rcvr *rcvr;
2554 unsigned char netfn;
2556 ipmi_user_t user = NULL;
2557 struct ipmi_ipmb_addr *ipmb_addr;
2558 struct ipmi_recv_msg *recv_msg;
2559 unsigned long flags;
2561 if (msg->rsp_size < 10) {
2562 /* Message not big enough, just ignore it. */
2563 spin_lock_irqsave(&intf->counter_lock, flags);
2564 intf->invalid_commands++;
2565 spin_unlock_irqrestore(&intf->counter_lock, flags);
2569 if (msg->rsp[2] != 0) {
2570 /* An error getting the response, just ignore it. */
2574 netfn = msg->rsp[4] >> 2;
2578 rcvr = find_cmd_rcvr(intf, netfn, cmd);
2581 kref_get(&user->refcount);
2587 /* We didn't find a user, deliver an error response. */
2588 spin_lock_irqsave(&intf->counter_lock, flags);
2589 intf->unhandled_commands++;
2590 spin_unlock_irqrestore(&intf->counter_lock, flags);
2592 msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
2593 msg->data[1] = IPMI_SEND_MSG_CMD;
2594 msg->data[2] = msg->rsp[3];
2595 msg->data[3] = msg->rsp[6];
2596 msg->data[4] = ((netfn + 1) << 2) | (msg->rsp[7] & 0x3);
2597 msg->data[5] = ipmb_checksum(&(msg->data[3]), 2);
2598 msg->data[6] = intf->channels[msg->rsp[3] & 0xf].address;
2600 msg->data[7] = (msg->rsp[7] & 0xfc) | (msg->rsp[4] & 0x3);
2601 msg->data[8] = msg->rsp[8]; /* cmd */
2602 msg->data[9] = IPMI_INVALID_CMD_COMPLETION_CODE;
2603 msg->data[10] = ipmb_checksum(&(msg->data[6]), 4);
2604 msg->data_size = 11;
2609 printk("Invalid command:");
2610 for (m = 0; m < msg->data_size; m++)
2611 printk(" %2.2x", msg->data[m]);
2615 intf->handlers->sender(intf->send_info, msg, 0);
2617 rv = -1; /* We used the message, so return the value that
2618 causes it to not be freed or queued. */
2620 /* Deliver the message to the user. */
2621 spin_lock_irqsave(&intf->counter_lock, flags);
2622 intf->handled_commands++;
2623 spin_unlock_irqrestore(&intf->counter_lock, flags);
2625 recv_msg = ipmi_alloc_recv_msg();
2627 /* We couldn't allocate memory for the
2628 message, so requeue it for handling
2631 kref_put(&user->refcount, free_user);
2633 /* Extract the source address from the data. */
2634 ipmb_addr = (struct ipmi_ipmb_addr *) &recv_msg->addr;
2635 ipmb_addr->addr_type = IPMI_IPMB_ADDR_TYPE;
2636 ipmb_addr->slave_addr = msg->rsp[6];
2637 ipmb_addr->lun = msg->rsp[7] & 3;
2638 ipmb_addr->channel = msg->rsp[3] & 0xf;
2640 /* Extract the rest of the message information
2641 from the IPMB header.*/
2642 recv_msg->user = user;
2643 recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
2644 recv_msg->msgid = msg->rsp[7] >> 2;
2645 recv_msg->msg.netfn = msg->rsp[4] >> 2;
2646 recv_msg->msg.cmd = msg->rsp[8];
2647 recv_msg->msg.data = recv_msg->msg_data;
2649 /* We chop off 10, not 9 bytes because the checksum
2650 at the end also needs to be removed. */
2651 recv_msg->msg.data_len = msg->rsp_size - 10;
2652 memcpy(recv_msg->msg_data,
2654 msg->rsp_size - 10);
2655 deliver_response(recv_msg);
2662 static int handle_lan_get_msg_rsp(ipmi_smi_t intf,
2663 struct ipmi_smi_msg *msg)
2665 struct ipmi_lan_addr lan_addr;
2666 struct ipmi_recv_msg *recv_msg;
2667 unsigned long flags;
2670 /* This is 13, not 12, because the response must contain a
2671 * completion code. */
2672 if (msg->rsp_size < 13) {
2673 /* Message not big enough, just ignore it. */
2674 spin_lock_irqsave(&intf->counter_lock, flags);
2675 intf->invalid_lan_responses++;
2676 spin_unlock_irqrestore(&intf->counter_lock, flags);
2680 if (msg->rsp[2] != 0) {
2681 /* An error getting the response, just ignore it. */
2685 lan_addr.addr_type = IPMI_LAN_ADDR_TYPE;
2686 lan_addr.session_handle = msg->rsp[4];
2687 lan_addr.remote_SWID = msg->rsp[8];
2688 lan_addr.local_SWID = msg->rsp[5];
2689 lan_addr.channel = msg->rsp[3] & 0x0f;
2690 lan_addr.privilege = msg->rsp[3] >> 4;
2691 lan_addr.lun = msg->rsp[9] & 3;
2693 /* It's a response from a remote entity. Look up the sequence
2694 number and handle the response. */
2695 if (intf_find_seq(intf,
2699 (msg->rsp[6] >> 2) & (~1),
2700 (struct ipmi_addr *) &(lan_addr),
2703 /* We were unable to find the sequence number,
2704 so just nuke the message. */
2705 spin_lock_irqsave(&intf->counter_lock, flags);
2706 intf->unhandled_lan_responses++;
2707 spin_unlock_irqrestore(&intf->counter_lock, flags);
2711 memcpy(recv_msg->msg_data,
2713 msg->rsp_size - 11);
2714 /* The other fields matched, so no need to set them, except
2715 for netfn, which needs to be the response that was
2716 returned, not the request value. */
2717 recv_msg->msg.netfn = msg->rsp[6] >> 2;
2718 recv_msg->msg.data = recv_msg->msg_data;
2719 recv_msg->msg.data_len = msg->rsp_size - 12;
2720 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
2721 spin_lock_irqsave(&intf->counter_lock, flags);
2722 intf->handled_lan_responses++;
2723 spin_unlock_irqrestore(&intf->counter_lock, flags);
2724 deliver_response(recv_msg);
2729 static int handle_lan_get_msg_cmd(ipmi_smi_t intf,
2730 struct ipmi_smi_msg *msg)
2732 struct cmd_rcvr *rcvr;
2734 unsigned char netfn;
2736 ipmi_user_t user = NULL;
2737 struct ipmi_lan_addr *lan_addr;
2738 struct ipmi_recv_msg *recv_msg;
2739 unsigned long flags;
2741 if (msg->rsp_size < 12) {
2742 /* Message not big enough, just ignore it. */
2743 spin_lock_irqsave(&intf->counter_lock, flags);
2744 intf->invalid_commands++;
2745 spin_unlock_irqrestore(&intf->counter_lock, flags);
2749 if (msg->rsp[2] != 0) {
2750 /* An error getting the response, just ignore it. */
2754 netfn = msg->rsp[6] >> 2;
2758 rcvr = find_cmd_rcvr(intf, netfn, cmd);
2761 kref_get(&user->refcount);
2767 /* We didn't find a user, just give up. */
2768 spin_lock_irqsave(&intf->counter_lock, flags);
2769 intf->unhandled_commands++;
2770 spin_unlock_irqrestore(&intf->counter_lock, flags);
2772 rv = 0; /* Don't do anything with these messages, just
2773 allow them to be freed. */
2775 /* Deliver the message to the user. */
2776 spin_lock_irqsave(&intf->counter_lock, flags);
2777 intf->handled_commands++;
2778 spin_unlock_irqrestore(&intf->counter_lock, flags);
2780 recv_msg = ipmi_alloc_recv_msg();
2782 /* We couldn't allocate memory for the
2783 message, so requeue it for handling
2786 kref_put(&user->refcount, free_user);
2788 /* Extract the source address from the data. */
2789 lan_addr = (struct ipmi_lan_addr *) &recv_msg->addr;
2790 lan_addr->addr_type = IPMI_LAN_ADDR_TYPE;
2791 lan_addr->session_handle = msg->rsp[4];
2792 lan_addr->remote_SWID = msg->rsp[8];
2793 lan_addr->local_SWID = msg->rsp[5];
2794 lan_addr->lun = msg->rsp[9] & 3;
2795 lan_addr->channel = msg->rsp[3] & 0xf;
2796 lan_addr->privilege = msg->rsp[3] >> 4;
2798 /* Extract the rest of the message information
2799 from the IPMB header.*/
2800 recv_msg->user = user;
2801 recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
2802 recv_msg->msgid = msg->rsp[9] >> 2;
2803 recv_msg->msg.netfn = msg->rsp[6] >> 2;
2804 recv_msg->msg.cmd = msg->rsp[10];
2805 recv_msg->msg.data = recv_msg->msg_data;
2807 /* We chop off 12, not 11 bytes because the checksum
2808 at the end also needs to be removed. */
2809 recv_msg->msg.data_len = msg->rsp_size - 12;
2810 memcpy(recv_msg->msg_data,
2812 msg->rsp_size - 12);
2813 deliver_response(recv_msg);
2820 static void copy_event_into_recv_msg(struct ipmi_recv_msg *recv_msg,
2821 struct ipmi_smi_msg *msg)
2823 struct ipmi_system_interface_addr *smi_addr;
2825 recv_msg->msgid = 0;
2826 smi_addr = (struct ipmi_system_interface_addr *) &(recv_msg->addr);
2827 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2828 smi_addr->channel = IPMI_BMC_CHANNEL;
2829 smi_addr->lun = msg->rsp[0] & 3;
2830 recv_msg->recv_type = IPMI_ASYNC_EVENT_RECV_TYPE;
2831 recv_msg->msg.netfn = msg->rsp[0] >> 2;
2832 recv_msg->msg.cmd = msg->rsp[1];
2833 memcpy(recv_msg->msg_data, &(msg->rsp[3]), msg->rsp_size - 3);
2834 recv_msg->msg.data = recv_msg->msg_data;
2835 recv_msg->msg.data_len = msg->rsp_size - 3;
2838 static int handle_read_event_rsp(ipmi_smi_t intf,
2839 struct ipmi_smi_msg *msg)
2841 struct ipmi_recv_msg *recv_msg, *recv_msg2;
2842 struct list_head msgs;
2845 int deliver_count = 0;
2846 unsigned long flags;
2848 if (msg->rsp_size < 19) {
2849 /* Message is too small to be an IPMB event. */
2850 spin_lock_irqsave(&intf->counter_lock, flags);
2851 intf->invalid_events++;
2852 spin_unlock_irqrestore(&intf->counter_lock, flags);
2856 if (msg->rsp[2] != 0) {
2857 /* An error getting the event, just ignore it. */
2861 INIT_LIST_HEAD(&msgs);
2863 spin_lock_irqsave(&intf->events_lock, flags);
2865 spin_lock(&intf->counter_lock);
2867 spin_unlock(&intf->counter_lock);
2869 /* Allocate and fill in one message for every user that is getting
2872 list_for_each_entry_rcu(user, &intf->users, link) {
2873 if (!user->gets_events)
2876 recv_msg = ipmi_alloc_recv_msg();
2879 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs,
2881 list_del(&recv_msg->link);
2882 ipmi_free_recv_msg(recv_msg);
2884 /* We couldn't allocate memory for the
2885 message, so requeue it for handling
2893 copy_event_into_recv_msg(recv_msg, msg);
2894 recv_msg->user = user;
2895 kref_get(&user->refcount);
2896 list_add_tail(&(recv_msg->link), &msgs);
2900 if (deliver_count) {
2901 /* Now deliver all the messages. */
2902 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs, link) {
2903 list_del(&recv_msg->link);
2904 deliver_response(recv_msg);
2906 } else if (intf->waiting_events_count < MAX_EVENTS_IN_QUEUE) {
2907 /* No one to receive the message, put it in queue if there's
2908 not already too many things in the queue. */
2909 recv_msg = ipmi_alloc_recv_msg();
2911 /* We couldn't allocate memory for the
2912 message, so requeue it for handling
2918 copy_event_into_recv_msg(recv_msg, msg);
2919 list_add_tail(&(recv_msg->link), &(intf->waiting_events));
2920 intf->waiting_events_count++;
2922 /* There's too many things in the queue, discard this
2924 printk(KERN_WARNING PFX "Event queue full, discarding an"
2925 " incoming event\n");
2929 spin_unlock_irqrestore(&(intf->events_lock), flags);
2934 static int handle_bmc_rsp(ipmi_smi_t intf,
2935 struct ipmi_smi_msg *msg)
2937 struct ipmi_recv_msg *recv_msg;
2938 unsigned long flags;
2939 struct ipmi_user *user;
2941 recv_msg = (struct ipmi_recv_msg *) msg->user_data;
2942 if (recv_msg == NULL)
2944 printk(KERN_WARNING"IPMI message received with no owner. This\n"
2945 "could be because of a malformed message, or\n"
2946 "because of a hardware error. Contact your\n"
2947 "hardware vender for assistance\n");
2951 user = recv_msg->user;
2952 /* Make sure the user still exists. */
2953 if (user && !user->valid) {
2954 /* The user for the message went away, so give up. */
2955 spin_lock_irqsave(&intf->counter_lock, flags);
2956 intf->unhandled_local_responses++;
2957 spin_unlock_irqrestore(&intf->counter_lock, flags);
2958 ipmi_free_recv_msg(recv_msg);
2960 struct ipmi_system_interface_addr *smi_addr;
2962 spin_lock_irqsave(&intf->counter_lock, flags);
2963 intf->handled_local_responses++;
2964 spin_unlock_irqrestore(&intf->counter_lock, flags);
2965 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
2966 recv_msg->msgid = msg->msgid;
2967 smi_addr = ((struct ipmi_system_interface_addr *)
2969 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2970 smi_addr->channel = IPMI_BMC_CHANNEL;
2971 smi_addr->lun = msg->rsp[0] & 3;
2972 recv_msg->msg.netfn = msg->rsp[0] >> 2;
2973 recv_msg->msg.cmd = msg->rsp[1];
2974 memcpy(recv_msg->msg_data,
2977 recv_msg->msg.data = recv_msg->msg_data;
2978 recv_msg->msg.data_len = msg->rsp_size - 2;
2979 deliver_response(recv_msg);
2985 /* Handle a new message. Return 1 if the message should be requeued,
2986 0 if the message should be freed, or -1 if the message should not
2987 be freed or requeued. */
2988 static int handle_new_recv_msg(ipmi_smi_t intf,
2989 struct ipmi_smi_msg *msg)
2997 for (m = 0; m < msg->rsp_size; m++)
2998 printk(" %2.2x", msg->rsp[m]);
3001 if (msg->rsp_size < 2) {
3002 /* Message is too small to be correct. */
3003 printk(KERN_WARNING PFX "BMC returned to small a message"
3004 " for netfn %x cmd %x, got %d bytes\n",
3005 (msg->data[0] >> 2) | 1, msg->data[1], msg->rsp_size);
3007 /* Generate an error response for the message. */
3008 msg->rsp[0] = msg->data[0] | (1 << 2);
3009 msg->rsp[1] = msg->data[1];
3010 msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
3012 } else if (((msg->rsp[0] >> 2) != ((msg->data[0] >> 2) | 1))/* Netfn */
3013 || (msg->rsp[1] != msg->data[1])) /* Command */
3015 /* The response is not even marginally correct. */
3016 printk(KERN_WARNING PFX "BMC returned incorrect response,"
3017 " expected netfn %x cmd %x, got netfn %x cmd %x\n",
3018 (msg->data[0] >> 2) | 1, msg->data[1],
3019 msg->rsp[0] >> 2, msg->rsp[1]);
3021 /* Generate an error response for the message. */
3022 msg->rsp[0] = msg->data[0] | (1 << 2);
3023 msg->rsp[1] = msg->data[1];
3024 msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
3028 if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
3029 && (msg->rsp[1] == IPMI_SEND_MSG_CMD)
3030 && (msg->user_data != NULL))
3032 /* It's a response to a response we sent. For this we
3033 deliver a send message response to the user. */
3034 struct ipmi_recv_msg *recv_msg = msg->user_data;
3037 if (msg->rsp_size < 2)
3038 /* Message is too small to be correct. */
3041 chan = msg->data[2] & 0x0f;
3042 if (chan >= IPMI_MAX_CHANNELS)
3043 /* Invalid channel number */
3049 /* Make sure the user still exists. */
3050 if (!recv_msg->user || !recv_msg->user->valid)
3053 recv_msg->recv_type = IPMI_RESPONSE_RESPONSE_TYPE;
3054 recv_msg->msg.data = recv_msg->msg_data;
3055 recv_msg->msg.data_len = 1;
3056 recv_msg->msg_data[0] = msg->rsp[2];
3057 deliver_response(recv_msg);
3058 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
3059 && (msg->rsp[1] == IPMI_GET_MSG_CMD))
3061 /* It's from the receive queue. */
3062 chan = msg->rsp[3] & 0xf;
3063 if (chan >= IPMI_MAX_CHANNELS) {
3064 /* Invalid channel number */
3069 switch (intf->channels[chan].medium) {
3070 case IPMI_CHANNEL_MEDIUM_IPMB:
3071 if (msg->rsp[4] & 0x04) {
3072 /* It's a response, so find the
3073 requesting message and send it up. */
3074 requeue = handle_ipmb_get_msg_rsp(intf, msg);
3076 /* It's a command to the SMS from some other
3077 entity. Handle that. */
3078 requeue = handle_ipmb_get_msg_cmd(intf, msg);
3082 case IPMI_CHANNEL_MEDIUM_8023LAN:
3083 case IPMI_CHANNEL_MEDIUM_ASYNC:
3084 if (msg->rsp[6] & 0x04) {
3085 /* It's a response, so find the
3086 requesting message and send it up. */
3087 requeue = handle_lan_get_msg_rsp(intf, msg);
3089 /* It's a command to the SMS from some other
3090 entity. Handle that. */
3091 requeue = handle_lan_get_msg_cmd(intf, msg);
3096 /* We don't handle the channel type, so just
3097 * free the message. */
3101 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
3102 && (msg->rsp[1] == IPMI_READ_EVENT_MSG_BUFFER_CMD))
3104 /* It's an asyncronous event. */
3105 requeue = handle_read_event_rsp(intf, msg);
3107 /* It's a response from the local BMC. */
3108 requeue = handle_bmc_rsp(intf, msg);
3115 /* Handle a new message from the lower layer. */
3116 void ipmi_smi_msg_received(ipmi_smi_t intf,
3117 struct ipmi_smi_msg *msg)
3119 unsigned long flags;
3123 if ((msg->data_size >= 2)
3124 && (msg->data[0] == (IPMI_NETFN_APP_REQUEST << 2))
3125 && (msg->data[1] == IPMI_SEND_MSG_CMD)
3126 && (msg->user_data == NULL))
3128 /* This is the local response to a command send, start
3129 the timer for these. The user_data will not be
3130 NULL if this is a response send, and we will let
3131 response sends just go through. */
3133 /* Check for errors, if we get certain errors (ones
3134 that mean basically we can try again later), we
3135 ignore them and start the timer. Otherwise we
3136 report the error immediately. */
3137 if ((msg->rsp_size >= 3) && (msg->rsp[2] != 0)
3138 && (msg->rsp[2] != IPMI_NODE_BUSY_ERR)
3139 && (msg->rsp[2] != IPMI_LOST_ARBITRATION_ERR))
3141 int chan = msg->rsp[3] & 0xf;
3143 /* Got an error sending the message, handle it. */
3144 spin_lock_irqsave(&intf->counter_lock, flags);
3145 if (chan >= IPMI_MAX_CHANNELS)
3146 ; /* This shouldn't happen */
3147 else if ((intf->channels[chan].medium
3148 == IPMI_CHANNEL_MEDIUM_8023LAN)
3149 || (intf->channels[chan].medium
3150 == IPMI_CHANNEL_MEDIUM_ASYNC))
3151 intf->sent_lan_command_errs++;
3153 intf->sent_ipmb_command_errs++;
3154 spin_unlock_irqrestore(&intf->counter_lock, flags);
3155 intf_err_seq(intf, msg->msgid, msg->rsp[2]);
3157 /* The message was sent, start the timer. */
3158 intf_start_seq_timer(intf, msg->msgid);
3161 ipmi_free_smi_msg(msg);
3165 /* To preserve message order, if the list is not empty, we
3166 tack this message onto the end of the list. */
3167 spin_lock_irqsave(&intf->waiting_msgs_lock, flags);
3168 if (!list_empty(&intf->waiting_msgs)) {
3169 list_add_tail(&msg->link, &intf->waiting_msgs);
3170 spin_unlock_irqrestore(&intf->waiting_msgs_lock, flags);
3173 spin_unlock_irqrestore(&intf->waiting_msgs_lock, flags);
3175 rv = handle_new_recv_msg(intf, msg);
3177 /* Could not handle the message now, just add it to a
3178 list to handle later. */
3179 spin_lock_irqsave(&intf->waiting_msgs_lock, flags);
3180 list_add_tail(&msg->link, &intf->waiting_msgs);
3181 spin_unlock_irqrestore(&intf->waiting_msgs_lock, flags);
3182 } else if (rv == 0) {
3183 ipmi_free_smi_msg(msg);
3190 void ipmi_smi_watchdog_pretimeout(ipmi_smi_t intf)
3195 list_for_each_entry_rcu(user, &intf->users, link) {
3196 if (!user->handler->ipmi_watchdog_pretimeout)
3199 user->handler->ipmi_watchdog_pretimeout(user->handler_data);
3205 handle_msg_timeout(struct ipmi_recv_msg *msg)
3207 msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
3208 msg->msg_data[0] = IPMI_TIMEOUT_COMPLETION_CODE;
3209 msg->msg.netfn |= 1; /* Convert to a response. */
3210 msg->msg.data_len = 1;
3211 msg->msg.data = msg->msg_data;
3212 deliver_response(msg);
3215 static struct ipmi_smi_msg *
3216 smi_from_recv_msg(ipmi_smi_t intf, struct ipmi_recv_msg *recv_msg,
3217 unsigned char seq, long seqid)
3219 struct ipmi_smi_msg *smi_msg = ipmi_alloc_smi_msg();
3221 /* If we can't allocate the message, then just return, we
3222 get 4 retries, so this should be ok. */
3225 memcpy(smi_msg->data, recv_msg->msg.data, recv_msg->msg.data_len);
3226 smi_msg->data_size = recv_msg->msg.data_len;
3227 smi_msg->msgid = STORE_SEQ_IN_MSGID(seq, seqid);
3233 for (m = 0; m < smi_msg->data_size; m++)
3234 printk(" %2.2x", smi_msg->data[m]);
3241 static void check_msg_timeout(ipmi_smi_t intf, struct seq_table *ent,
3242 struct list_head *timeouts, long timeout_period,
3243 int slot, unsigned long *flags)
3245 struct ipmi_recv_msg *msg;
3250 ent->timeout -= timeout_period;
3251 if (ent->timeout > 0)
3254 if (ent->retries_left == 0) {
3255 /* The message has used all its retries. */
3257 msg = ent->recv_msg;
3258 list_add_tail(&msg->link, timeouts);
3259 spin_lock(&intf->counter_lock);
3261 intf->timed_out_ipmb_broadcasts++;
3262 else if (ent->recv_msg->addr.addr_type == IPMI_LAN_ADDR_TYPE)
3263 intf->timed_out_lan_commands++;
3265 intf->timed_out_ipmb_commands++;
3266 spin_unlock(&intf->counter_lock);
3268 struct ipmi_smi_msg *smi_msg;
3269 /* More retries, send again. */
3271 /* Start with the max timer, set to normal
3272 timer after the message is sent. */
3273 ent->timeout = MAX_MSG_TIMEOUT;
3274 ent->retries_left--;
3275 spin_lock(&intf->counter_lock);
3276 if (ent->recv_msg->addr.addr_type == IPMI_LAN_ADDR_TYPE)
3277 intf->retransmitted_lan_commands++;
3279 intf->retransmitted_ipmb_commands++;
3280 spin_unlock(&intf->counter_lock);
3282 smi_msg = smi_from_recv_msg(intf, ent->recv_msg, slot,
3287 spin_unlock_irqrestore(&intf->seq_lock, *flags);
3288 /* Send the new message. We send with a zero
3289 * priority. It timed out, I doubt time is
3290 * that critical now, and high priority
3291 * messages are really only for messages to the
3292 * local MC, which don't get resent. */
3293 intf->handlers->sender(intf->send_info,
3295 spin_lock_irqsave(&intf->seq_lock, *flags);
3299 static void ipmi_timeout_handler(long timeout_period)
3302 struct list_head timeouts;
3303 struct ipmi_recv_msg *msg, *msg2;
3304 struct ipmi_smi_msg *smi_msg, *smi_msg2;
3305 unsigned long flags;
3308 INIT_LIST_HEAD(&timeouts);
3310 spin_lock(&interfaces_lock);
3311 for (i = 0; i < MAX_IPMI_INTERFACES; i++) {
3312 intf = ipmi_interfaces[i];
3313 if (IPMI_INVALID_INTERFACE(intf))
3315 kref_get(&intf->refcount);
3316 spin_unlock(&interfaces_lock);
3318 /* See if any waiting messages need to be processed. */
3319 spin_lock_irqsave(&intf->waiting_msgs_lock, flags);
3320 list_for_each_entry_safe(smi_msg, smi_msg2,
3321 &intf->waiting_msgs, link) {
3322 if (!handle_new_recv_msg(intf, smi_msg)) {
3323 list_del(&smi_msg->link);
3324 ipmi_free_smi_msg(smi_msg);
3326 /* To preserve message order, quit if we
3327 can't handle a message. */
3331 spin_unlock_irqrestore(&intf->waiting_msgs_lock, flags);
3333 /* Go through the seq table and find any messages that
3334 have timed out, putting them in the timeouts
3336 spin_lock_irqsave(&intf->seq_lock, flags);
3337 for (j = 0; j < IPMI_IPMB_NUM_SEQ; j++)
3338 check_msg_timeout(intf, &(intf->seq_table[j]),
3339 &timeouts, timeout_period, j,
3341 spin_unlock_irqrestore(&intf->seq_lock, flags);
3343 list_for_each_entry_safe(msg, msg2, &timeouts, link)
3344 handle_msg_timeout(msg);
3346 kref_put(&intf->refcount, intf_free);
3347 spin_lock(&interfaces_lock);
3349 spin_unlock(&interfaces_lock);
3352 static void ipmi_request_event(void)
3357 spin_lock(&interfaces_lock);
3358 for (i = 0; i < MAX_IPMI_INTERFACES; i++) {
3359 intf = ipmi_interfaces[i];
3360 if (IPMI_INVALID_INTERFACE(intf))
3363 intf->handlers->request_events(intf->send_info);
3365 spin_unlock(&interfaces_lock);
3368 static struct timer_list ipmi_timer;
3370 /* Call every ~100 ms. */
3371 #define IPMI_TIMEOUT_TIME 100
3373 /* How many jiffies does it take to get to the timeout time. */
3374 #define IPMI_TIMEOUT_JIFFIES ((IPMI_TIMEOUT_TIME * HZ) / 1000)
3376 /* Request events from the queue every second (this is the number of
3377 IPMI_TIMEOUT_TIMES between event requests). Hopefully, in the
3378 future, IPMI will add a way to know immediately if an event is in
3379 the queue and this silliness can go away. */
3380 #define IPMI_REQUEST_EV_TIME (1000 / (IPMI_TIMEOUT_TIME))
3382 static atomic_t stop_operation;
3383 static unsigned int ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
3385 static void ipmi_timeout(unsigned long data)
3387 if (atomic_read(&stop_operation))
3391 if (ticks_to_req_ev == 0) {
3392 ipmi_request_event();
3393 ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
3396 ipmi_timeout_handler(IPMI_TIMEOUT_TIME);
3398 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
3402 static atomic_t smi_msg_inuse_count = ATOMIC_INIT(0);
3403 static atomic_t recv_msg_inuse_count = ATOMIC_INIT(0);
3405 /* FIXME - convert these to slabs. */
3406 static void free_smi_msg(struct ipmi_smi_msg *msg)
3408 atomic_dec(&smi_msg_inuse_count);
3412 struct ipmi_smi_msg *ipmi_alloc_smi_msg(void)
3414 struct ipmi_smi_msg *rv;
3415 rv = kmalloc(sizeof(struct ipmi_smi_msg), GFP_ATOMIC);
3417 rv->done = free_smi_msg;
3418 rv->user_data = NULL;
3419 atomic_inc(&smi_msg_inuse_count);
3424 static void free_recv_msg(struct ipmi_recv_msg *msg)
3426 atomic_dec(&recv_msg_inuse_count);
3430 struct ipmi_recv_msg *ipmi_alloc_recv_msg(void)
3432 struct ipmi_recv_msg *rv;
3434 rv = kmalloc(sizeof(struct ipmi_recv_msg), GFP_ATOMIC);
3436 rv->done = free_recv_msg;
3437 atomic_inc(&recv_msg_inuse_count);
3442 void ipmi_free_recv_msg(struct ipmi_recv_msg *msg)
3445 kref_put(&msg->user->refcount, free_user);
3449 #ifdef CONFIG_IPMI_PANIC_EVENT
3451 static void dummy_smi_done_handler(struct ipmi_smi_msg *msg)
3455 static void dummy_recv_done_handler(struct ipmi_recv_msg *msg)
3459 #ifdef CONFIG_IPMI_PANIC_STRING
3460 static void event_receiver_fetcher(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
3462 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
3463 && (msg->msg.netfn == IPMI_NETFN_SENSOR_EVENT_RESPONSE)
3464 && (msg->msg.cmd == IPMI_GET_EVENT_RECEIVER_CMD)
3465 && (msg->msg.data[0] == IPMI_CC_NO_ERROR))
3467 /* A get event receiver command, save it. */
3468 intf->event_receiver = msg->msg.data[1];
3469 intf->event_receiver_lun = msg->msg.data[2] & 0x3;
3473 static void device_id_fetcher(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
3475 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
3476 && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
3477 && (msg->msg.cmd == IPMI_GET_DEVICE_ID_CMD)
3478 && (msg->msg.data[0] == IPMI_CC_NO_ERROR))
3480 /* A get device id command, save if we are an event
3481 receiver or generator. */
3482 intf->local_sel_device = (msg->msg.data[6] >> 2) & 1;
3483 intf->local_event_generator = (msg->msg.data[6] >> 5) & 1;
3488 static void send_panic_events(char *str)
3490 struct kernel_ipmi_msg msg;
3492 unsigned char data[16];
3494 struct ipmi_system_interface_addr *si;
3495 struct ipmi_addr addr;
3496 struct ipmi_smi_msg smi_msg;
3497 struct ipmi_recv_msg recv_msg;
3499 si = (struct ipmi_system_interface_addr *) &addr;
3500 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3501 si->channel = IPMI_BMC_CHANNEL;
3504 /* Fill in an event telling that we have failed. */
3505 msg.netfn = 0x04; /* Sensor or Event. */
3506 msg.cmd = 2; /* Platform event command. */
3509 data[0] = 0x41; /* Kernel generator ID, IPMI table 5-4 */
3510 data[1] = 0x03; /* This is for IPMI 1.0. */
3511 data[2] = 0x20; /* OS Critical Stop, IPMI table 36-3 */
3512 data[4] = 0x6f; /* Sensor specific, IPMI table 36-1 */
3513 data[5] = 0xa1; /* Runtime stop OEM bytes 2 & 3. */
3515 /* Put a few breadcrumbs in. Hopefully later we can add more things
3516 to make the panic events more useful. */
3523 smi_msg.done = dummy_smi_done_handler;
3524 recv_msg.done = dummy_recv_done_handler;
3526 /* For every registered interface, send the event. */
3527 for (i = 0; i < MAX_IPMI_INTERFACES; i++) {
3528 intf = ipmi_interfaces[i];
3529 if (IPMI_INVALID_INTERFACE(intf))
3532 /* Send the event announcing the panic. */
3533 intf->handlers->set_run_to_completion(intf->send_info, 1);
3534 i_ipmi_request(NULL,
3543 intf->channels[0].address,
3544 intf->channels[0].lun,
3545 0, 1); /* Don't retry, and don't wait. */
3548 #ifdef CONFIG_IPMI_PANIC_STRING
3549 /* On every interface, dump a bunch of OEM event holding the
3554 for (i = 0; i < MAX_IPMI_INTERFACES; i++) {
3556 struct ipmi_ipmb_addr *ipmb;
3559 intf = ipmi_interfaces[i];
3560 if (IPMI_INVALID_INTERFACE(intf))
3563 /* First job here is to figure out where to send the
3564 OEM events. There's no way in IPMI to send OEM
3565 events using an event send command, so we have to
3566 find the SEL to put them in and stick them in
3569 /* Get capabilities from the get device id. */
3570 intf->local_sel_device = 0;
3571 intf->local_event_generator = 0;
3572 intf->event_receiver = 0;
3574 /* Request the device info from the local MC. */
3575 msg.netfn = IPMI_NETFN_APP_REQUEST;
3576 msg.cmd = IPMI_GET_DEVICE_ID_CMD;
3579 intf->null_user_handler = device_id_fetcher;
3580 i_ipmi_request(NULL,
3589 intf->channels[0].address,
3590 intf->channels[0].lun,
3591 0, 1); /* Don't retry, and don't wait. */
3593 if (intf->local_event_generator) {
3594 /* Request the event receiver from the local MC. */
3595 msg.netfn = IPMI_NETFN_SENSOR_EVENT_REQUEST;
3596 msg.cmd = IPMI_GET_EVENT_RECEIVER_CMD;
3599 intf->null_user_handler = event_receiver_fetcher;
3600 i_ipmi_request(NULL,
3609 intf->channels[0].address,
3610 intf->channels[0].lun,
3611 0, 1); /* no retry, and no wait. */
3613 intf->null_user_handler = NULL;
3615 /* Validate the event receiver. The low bit must not
3616 be 1 (it must be a valid IPMB address), it cannot
3617 be zero, and it must not be my address. */
3618 if (((intf->event_receiver & 1) == 0)
3619 && (intf->event_receiver != 0)
3620 && (intf->event_receiver != intf->channels[0].address))
3622 /* The event receiver is valid, send an IPMB
3624 ipmb = (struct ipmi_ipmb_addr *) &addr;
3625 ipmb->addr_type = IPMI_IPMB_ADDR_TYPE;
3626 ipmb->channel = 0; /* FIXME - is this right? */
3627 ipmb->lun = intf->event_receiver_lun;
3628 ipmb->slave_addr = intf->event_receiver;
3629 } else if (intf->local_sel_device) {
3630 /* The event receiver was not valid (or was
3631 me), but I am an SEL device, just dump it
3633 si = (struct ipmi_system_interface_addr *) &addr;
3634 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3635 si->channel = IPMI_BMC_CHANNEL;
3638 continue; /* No where to send the event. */
3641 msg.netfn = IPMI_NETFN_STORAGE_REQUEST; /* Storage. */
3642 msg.cmd = IPMI_ADD_SEL_ENTRY_CMD;
3648 int size = strlen(p);
3654 data[2] = 0xf0; /* OEM event without timestamp. */
3655 data[3] = intf->channels[0].address;
3656 data[4] = j++; /* sequence # */
3657 /* Always give 11 bytes, so strncpy will fill
3658 it with zeroes for me. */
3659 strncpy(data+5, p, 11);
3662 i_ipmi_request(NULL,
3671 intf->channels[0].address,
3672 intf->channels[0].lun,
3673 0, 1); /* no retry, and no wait. */
3676 #endif /* CONFIG_IPMI_PANIC_STRING */
3678 #endif /* CONFIG_IPMI_PANIC_EVENT */
3680 static int has_paniced = 0;
3682 static int panic_event(struct notifier_block *this,
3683 unsigned long event,
3693 /* For every registered interface, set it to run to completion. */
3694 for (i = 0; i < MAX_IPMI_INTERFACES; i++) {
3695 intf = ipmi_interfaces[i];
3696 if (IPMI_INVALID_INTERFACE(intf))
3699 intf->handlers->set_run_to_completion(intf->send_info, 1);
3702 #ifdef CONFIG_IPMI_PANIC_EVENT
3703 send_panic_events(ptr);
3709 static struct notifier_block panic_block = {
3710 .notifier_call = panic_event,
3712 .priority = 200 /* priority: INT_MAX >= x >= 0 */
3715 static int ipmi_init_msghandler(void)
3723 rv = driver_register(&ipmidriver);
3725 printk(KERN_ERR PFX "Could not register IPMI driver\n");
3729 printk(KERN_INFO "ipmi message handler version "
3730 IPMI_DRIVER_VERSION "\n");
3732 for (i = 0; i < MAX_IPMI_INTERFACES; i++)
3733 ipmi_interfaces[i] = NULL;
3735 #ifdef CONFIG_PROC_FS
3736 proc_ipmi_root = proc_mkdir("ipmi", NULL);
3737 if (!proc_ipmi_root) {
3738 printk(KERN_ERR PFX "Unable to create IPMI proc dir");
3742 proc_ipmi_root->owner = THIS_MODULE;
3743 #endif /* CONFIG_PROC_FS */
3745 init_timer(&ipmi_timer);
3746 ipmi_timer.data = 0;
3747 ipmi_timer.function = ipmi_timeout;
3748 ipmi_timer.expires = jiffies + IPMI_TIMEOUT_JIFFIES;
3749 add_timer(&ipmi_timer);
3751 atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
3758 static __init int ipmi_init_msghandler_mod(void)
3760 ipmi_init_msghandler();
3764 static __exit void cleanup_ipmi(void)
3771 atomic_notifier_chain_unregister(&panic_notifier_list, &panic_block);
3773 /* This can't be called if any interfaces exist, so no worry about
3774 shutting down the interfaces. */
3776 /* Tell the timer to stop, then wait for it to stop. This avoids
3777 problems with race conditions removing the timer here. */
3778 atomic_inc(&stop_operation);
3779 del_timer_sync(&ipmi_timer);
3781 #ifdef CONFIG_PROC_FS
3782 remove_proc_entry(proc_ipmi_root->name, &proc_root);
3783 #endif /* CONFIG_PROC_FS */
3785 driver_unregister(&ipmidriver);
3789 /* Check for buffer leaks. */
3790 count = atomic_read(&smi_msg_inuse_count);
3792 printk(KERN_WARNING PFX "SMI message count %d at exit\n",
3794 count = atomic_read(&recv_msg_inuse_count);
3796 printk(KERN_WARNING PFX "recv message count %d at exit\n",
3799 module_exit(cleanup_ipmi);
3801 module_init(ipmi_init_msghandler_mod);
3802 MODULE_LICENSE("GPL");
3803 MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
3804 MODULE_DESCRIPTION("Incoming and outgoing message routing for an IPMI interface.");
3805 MODULE_VERSION(IPMI_DRIVER_VERSION);
3807 EXPORT_SYMBOL(ipmi_create_user);
3808 EXPORT_SYMBOL(ipmi_destroy_user);
3809 EXPORT_SYMBOL(ipmi_get_version);
3810 EXPORT_SYMBOL(ipmi_request_settime);
3811 EXPORT_SYMBOL(ipmi_request_supply_msgs);
3812 EXPORT_SYMBOL(ipmi_register_smi);
3813 EXPORT_SYMBOL(ipmi_unregister_smi);
3814 EXPORT_SYMBOL(ipmi_register_for_cmd);
3815 EXPORT_SYMBOL(ipmi_unregister_for_cmd);
3816 EXPORT_SYMBOL(ipmi_smi_msg_received);
3817 EXPORT_SYMBOL(ipmi_smi_watchdog_pretimeout);
3818 EXPORT_SYMBOL(ipmi_alloc_smi_msg);
3819 EXPORT_SYMBOL(ipmi_addr_length);
3820 EXPORT_SYMBOL(ipmi_validate_addr);
3821 EXPORT_SYMBOL(ipmi_set_gets_events);
3822 EXPORT_SYMBOL(ipmi_smi_watcher_register);
3823 EXPORT_SYMBOL(ipmi_smi_watcher_unregister);
3824 EXPORT_SYMBOL(ipmi_set_my_address);
3825 EXPORT_SYMBOL(ipmi_get_my_address);
3826 EXPORT_SYMBOL(ipmi_set_my_LUN);
3827 EXPORT_SYMBOL(ipmi_get_my_LUN);
3828 EXPORT_SYMBOL(ipmi_smi_add_proc_entry);
3829 EXPORT_SYMBOL(ipmi_user_set_run_to_completion);
3830 EXPORT_SYMBOL(ipmi_free_recv_msg);