4 * Incoming and outgoing message routing for an IPMI interface.
6 * Author: MontaVista Software, Inc.
7 * Corey Minyard <minyard@mvista.com>
10 * Copyright 2002 MontaVista Software Inc.
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
19 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
24 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
26 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
27 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * You should have received a copy of the GNU General Public License along
30 * with this program; if not, write to the Free Software Foundation, Inc.,
31 * 675 Mass Ave, Cambridge, MA 02139, USA.
34 #include <linux/module.h>
35 #include <linux/errno.h>
36 #include <asm/system.h>
37 #include <linux/poll.h>
38 #include <linux/sched.h>
39 #include <linux/spinlock.h>
40 #include <linux/mutex.h>
41 #include <linux/slab.h>
42 #include <linux/ipmi.h>
43 #include <linux/ipmi_smi.h>
44 #include <linux/notifier.h>
45 #include <linux/init.h>
46 #include <linux/proc_fs.h>
47 #include <linux/rcupdate.h>
49 #define PFX "IPMI message handler: "
51 #define IPMI_DRIVER_VERSION "39.2"
53 static struct ipmi_recv_msg
*ipmi_alloc_recv_msg(void);
54 static int ipmi_init_msghandler(void);
56 static int initialized
;
59 static struct proc_dir_entry
*proc_ipmi_root
;
60 #endif /* CONFIG_PROC_FS */
62 /* Remain in auto-maintenance mode for this amount of time (in ms). */
63 #define IPMI_MAINTENANCE_MODE_TIMEOUT 30000
65 #define MAX_EVENTS_IN_QUEUE 25
68 * Don't let a message sit in a queue forever, always time it with at lest
69 * the max message timer. This is in milliseconds.
71 #define MAX_MSG_TIMEOUT 60000
74 * The main "user" data structure.
77 struct list_head link
;
79 /* Set to "0" when the user is destroyed. */
84 /* The upper layer that handles receive messages. */
85 struct ipmi_user_hndl
*handler
;
88 /* The interface this user is bound to. */
91 /* Does this interface receive IPMI events? */
96 struct list_head link
;
104 * This is used to form a linked lised during mass deletion.
105 * Since this is in an RCU list, we cannot use the link above
106 * or change any data until the RCU period completes. So we
107 * use this next variable during mass deletion so we can have
108 * a list and don't have to wait and restart the search on
109 * every individual deletion of a command.
111 struct cmd_rcvr
*next
;
115 unsigned int inuse
: 1;
116 unsigned int broadcast
: 1;
118 unsigned long timeout
;
119 unsigned long orig_timeout
;
120 unsigned int retries_left
;
123 * To verify on an incoming send message response that this is
124 * the message that the response is for, we keep a sequence id
125 * and increment it every time we send a message.
130 * This is held so we can properly respond to the message on a
131 * timeout, and it is used to hold the temporary data for
132 * retransmission, too.
134 struct ipmi_recv_msg
*recv_msg
;
138 * Store the information in a msgid (long) to allow us to find a
139 * sequence table entry from the msgid.
141 #define STORE_SEQ_IN_MSGID(seq, seqid) (((seq&0xff)<<26) | (seqid&0x3ffffff))
143 #define GET_SEQ_FROM_MSGID(msgid, seq, seqid) \
145 seq = ((msgid >> 26) & 0x3f); \
146 seqid = (msgid & 0x3fffff); \
149 #define NEXT_SEQID(seqid) (((seqid) + 1) & 0x3fffff)
151 struct ipmi_channel
{
152 unsigned char medium
;
153 unsigned char protocol
;
156 * My slave address. This is initialized to IPMI_BMC_SLAVE_ADDR,
157 * but may be changed by the user.
159 unsigned char address
;
162 * My LUN. This should generally stay the SMS LUN, but just in
168 #ifdef CONFIG_PROC_FS
169 struct ipmi_proc_entry
{
171 struct ipmi_proc_entry
*next
;
176 struct platform_device
*dev
;
177 struct ipmi_device_id id
;
178 unsigned char guid
[16];
181 struct kref refcount
;
183 /* bmc device attributes */
184 struct device_attribute device_id_attr
;
185 struct device_attribute provides_dev_sdrs_attr
;
186 struct device_attribute revision_attr
;
187 struct device_attribute firmware_rev_attr
;
188 struct device_attribute version_attr
;
189 struct device_attribute add_dev_support_attr
;
190 struct device_attribute manufacturer_id_attr
;
191 struct device_attribute product_id_attr
;
192 struct device_attribute guid_attr
;
193 struct device_attribute aux_firmware_rev_attr
;
197 * Various statistics for IPMI, these index stats[] in the ipmi_smi
200 enum ipmi_stat_indexes
{
201 /* Commands we got from the user that were invalid. */
202 IPMI_STAT_sent_invalid_commands
= 0,
204 /* Commands we sent to the MC. */
205 IPMI_STAT_sent_local_commands
,
207 /* Responses from the MC that were delivered to a user. */
208 IPMI_STAT_handled_local_responses
,
210 /* Responses from the MC that were not delivered to a user. */
211 IPMI_STAT_unhandled_local_responses
,
213 /* Commands we sent out to the IPMB bus. */
214 IPMI_STAT_sent_ipmb_commands
,
216 /* Commands sent on the IPMB that had errors on the SEND CMD */
217 IPMI_STAT_sent_ipmb_command_errs
,
219 /* Each retransmit increments this count. */
220 IPMI_STAT_retransmitted_ipmb_commands
,
223 * When a message times out (runs out of retransmits) this is
226 IPMI_STAT_timed_out_ipmb_commands
,
229 * This is like above, but for broadcasts. Broadcasts are
230 * *not* included in the above count (they are expected to
233 IPMI_STAT_timed_out_ipmb_broadcasts
,
235 /* Responses I have sent to the IPMB bus. */
236 IPMI_STAT_sent_ipmb_responses
,
238 /* The response was delivered to the user. */
239 IPMI_STAT_handled_ipmb_responses
,
241 /* The response had invalid data in it. */
242 IPMI_STAT_invalid_ipmb_responses
,
244 /* The response didn't have anyone waiting for it. */
245 IPMI_STAT_unhandled_ipmb_responses
,
247 /* Commands we sent out to the IPMB bus. */
248 IPMI_STAT_sent_lan_commands
,
250 /* Commands sent on the IPMB that had errors on the SEND CMD */
251 IPMI_STAT_sent_lan_command_errs
,
253 /* Each retransmit increments this count. */
254 IPMI_STAT_retransmitted_lan_commands
,
257 * When a message times out (runs out of retransmits) this is
260 IPMI_STAT_timed_out_lan_commands
,
262 /* Responses I have sent to the IPMB bus. */
263 IPMI_STAT_sent_lan_responses
,
265 /* The response was delivered to the user. */
266 IPMI_STAT_handled_lan_responses
,
268 /* The response had invalid data in it. */
269 IPMI_STAT_invalid_lan_responses
,
271 /* The response didn't have anyone waiting for it. */
272 IPMI_STAT_unhandled_lan_responses
,
274 /* The command was delivered to the user. */
275 IPMI_STAT_handled_commands
,
277 /* The command had invalid data in it. */
278 IPMI_STAT_invalid_commands
,
280 /* The command didn't have anyone waiting for it. */
281 IPMI_STAT_unhandled_commands
,
283 /* Invalid data in an event. */
284 IPMI_STAT_invalid_events
,
286 /* Events that were received with the proper format. */
289 /* Retransmissions on IPMB that failed. */
290 IPMI_STAT_dropped_rexmit_ipmb_commands
,
292 /* Retransmissions on LAN that failed. */
293 IPMI_STAT_dropped_rexmit_lan_commands
,
295 /* This *must* remain last, add new values above this. */
300 #define IPMI_IPMB_NUM_SEQ 64
301 #define IPMI_MAX_CHANNELS 16
303 /* What interface number are we? */
306 struct kref refcount
;
308 /* Used for a list of interfaces. */
309 struct list_head link
;
312 * The list of upper layers that are using me. seq_lock
315 struct list_head users
;
317 /* Information to supply to users. */
318 unsigned char ipmi_version_major
;
319 unsigned char ipmi_version_minor
;
321 /* Used for wake ups at startup. */
322 wait_queue_head_t waitq
;
324 struct bmc_device
*bmc
;
329 * This is the lower-layer's sender routine. Note that you
330 * must either be holding the ipmi_interfaces_mutex or be in
331 * an umpreemptible region to use this. You must fetch the
332 * value into a local variable and make sure it is not NULL.
334 struct ipmi_smi_handlers
*handlers
;
337 #ifdef CONFIG_PROC_FS
338 /* A list of proc entries for this interface. */
339 struct mutex proc_entry_lock
;
340 struct ipmi_proc_entry
*proc_entries
;
343 /* Driver-model device for the system interface. */
344 struct device
*si_dev
;
347 * A table of sequence numbers for this interface. We use the
348 * sequence numbers for IPMB messages that go out of the
349 * interface to match them up with their responses. A routine
350 * is called periodically to time the items in this list.
353 struct seq_table seq_table
[IPMI_IPMB_NUM_SEQ
];
357 * Messages that were delayed for some reason (out of memory,
358 * for instance), will go in here to be processed later in a
359 * periodic timer interrupt.
361 spinlock_t waiting_msgs_lock
;
362 struct list_head waiting_msgs
;
365 * The list of command receivers that are registered for commands
368 struct mutex cmd_rcvrs_mutex
;
369 struct list_head cmd_rcvrs
;
372 * Events that were queues because no one was there to receive
375 spinlock_t events_lock
; /* For dealing with event stuff. */
376 struct list_head waiting_events
;
377 unsigned int waiting_events_count
; /* How many events in queue? */
378 char delivering_events
;
379 char event_msg_printed
;
382 * The event receiver for my BMC, only really used at panic
383 * shutdown as a place to store this.
385 unsigned char event_receiver
;
386 unsigned char event_receiver_lun
;
387 unsigned char local_sel_device
;
388 unsigned char local_event_generator
;
390 /* For handling of maintenance mode. */
391 int maintenance_mode
;
392 int maintenance_mode_enable
;
393 int auto_maintenance_timeout
;
394 spinlock_t maintenance_mode_lock
; /* Used in a timer... */
397 * A cheap hack, if this is non-null and a message to an
398 * interface comes in with a NULL user, call this routine with
399 * it. Note that the message will still be freed by the
400 * caller. This only works on the system interface.
402 void (*null_user_handler
)(ipmi_smi_t intf
, struct ipmi_recv_msg
*msg
);
405 * When we are scanning the channels for an SMI, this will
406 * tell which channel we are scanning.
410 /* Channel information */
411 struct ipmi_channel channels
[IPMI_MAX_CHANNELS
];
414 struct proc_dir_entry
*proc_dir
;
415 char proc_dir_name
[10];
417 atomic_t stats
[IPMI_NUM_STATS
];
420 * run_to_completion duplicate of smb_info, smi_info
421 * and ipmi_serial_info structures. Used to decrease numbers of
422 * parameters passed by "low" level IPMI code.
424 int run_to_completion
;
426 #define to_si_intf_from_dev(device) container_of(device, struct ipmi_smi, dev)
429 * The driver model view of the IPMI messaging driver.
431 static struct platform_driver ipmidriver
= {
434 .bus
= &platform_bus_type
437 static DEFINE_MUTEX(ipmidriver_mutex
);
439 static LIST_HEAD(ipmi_interfaces
);
440 static DEFINE_MUTEX(ipmi_interfaces_mutex
);
443 * List of watchers that want to know when smi's are added and deleted.
445 static LIST_HEAD(smi_watchers
);
446 static DEFINE_MUTEX(smi_watchers_mutex
);
449 #define ipmi_inc_stat(intf, stat) \
450 atomic_inc(&(intf)->stats[IPMI_STAT_ ## stat])
451 #define ipmi_get_stat(intf, stat) \
452 ((unsigned int) atomic_read(&(intf)->stats[IPMI_STAT_ ## stat]))
454 static int is_lan_addr(struct ipmi_addr
*addr
)
456 return addr
->addr_type
== IPMI_LAN_ADDR_TYPE
;
459 static int is_ipmb_addr(struct ipmi_addr
*addr
)
461 return addr
->addr_type
== IPMI_IPMB_ADDR_TYPE
;
464 static int is_ipmb_bcast_addr(struct ipmi_addr
*addr
)
466 return addr
->addr_type
== IPMI_IPMB_BROADCAST_ADDR_TYPE
;
469 static void free_recv_msg_list(struct list_head
*q
)
471 struct ipmi_recv_msg
*msg
, *msg2
;
473 list_for_each_entry_safe(msg
, msg2
, q
, link
) {
474 list_del(&msg
->link
);
475 ipmi_free_recv_msg(msg
);
479 static void free_smi_msg_list(struct list_head
*q
)
481 struct ipmi_smi_msg
*msg
, *msg2
;
483 list_for_each_entry_safe(msg
, msg2
, q
, link
) {
484 list_del(&msg
->link
);
485 ipmi_free_smi_msg(msg
);
489 static void clean_up_interface_data(ipmi_smi_t intf
)
492 struct cmd_rcvr
*rcvr
, *rcvr2
;
493 struct list_head list
;
495 free_smi_msg_list(&intf
->waiting_msgs
);
496 free_recv_msg_list(&intf
->waiting_events
);
499 * Wholesale remove all the entries from the list in the
500 * interface and wait for RCU to know that none are in use.
502 mutex_lock(&intf
->cmd_rcvrs_mutex
);
503 INIT_LIST_HEAD(&list
);
504 list_splice_init_rcu(&intf
->cmd_rcvrs
, &list
, synchronize_rcu
);
505 mutex_unlock(&intf
->cmd_rcvrs_mutex
);
507 list_for_each_entry_safe(rcvr
, rcvr2
, &list
, link
)
510 for (i
= 0; i
< IPMI_IPMB_NUM_SEQ
; i
++) {
511 if ((intf
->seq_table
[i
].inuse
)
512 && (intf
->seq_table
[i
].recv_msg
))
513 ipmi_free_recv_msg(intf
->seq_table
[i
].recv_msg
);
517 static void intf_free(struct kref
*ref
)
519 ipmi_smi_t intf
= container_of(ref
, struct ipmi_smi
, refcount
);
521 clean_up_interface_data(intf
);
525 struct watcher_entry
{
528 struct list_head link
;
531 int ipmi_smi_watcher_register(struct ipmi_smi_watcher
*watcher
)
534 LIST_HEAD(to_deliver
);
535 struct watcher_entry
*e
, *e2
;
537 mutex_lock(&smi_watchers_mutex
);
539 mutex_lock(&ipmi_interfaces_mutex
);
541 /* Build a list of things to deliver. */
542 list_for_each_entry(intf
, &ipmi_interfaces
, link
) {
543 if (intf
->intf_num
== -1)
545 e
= kmalloc(sizeof(*e
), GFP_KERNEL
);
548 kref_get(&intf
->refcount
);
550 e
->intf_num
= intf
->intf_num
;
551 list_add_tail(&e
->link
, &to_deliver
);
554 /* We will succeed, so add it to the list. */
555 list_add(&watcher
->link
, &smi_watchers
);
557 mutex_unlock(&ipmi_interfaces_mutex
);
559 list_for_each_entry_safe(e
, e2
, &to_deliver
, link
) {
561 watcher
->new_smi(e
->intf_num
, e
->intf
->si_dev
);
562 kref_put(&e
->intf
->refcount
, intf_free
);
566 mutex_unlock(&smi_watchers_mutex
);
571 mutex_unlock(&ipmi_interfaces_mutex
);
572 mutex_unlock(&smi_watchers_mutex
);
573 list_for_each_entry_safe(e
, e2
, &to_deliver
, link
) {
575 kref_put(&e
->intf
->refcount
, intf_free
);
580 EXPORT_SYMBOL(ipmi_smi_watcher_register
);
582 int ipmi_smi_watcher_unregister(struct ipmi_smi_watcher
*watcher
)
584 mutex_lock(&smi_watchers_mutex
);
585 list_del(&(watcher
->link
));
586 mutex_unlock(&smi_watchers_mutex
);
589 EXPORT_SYMBOL(ipmi_smi_watcher_unregister
);
592 * Must be called with smi_watchers_mutex held.
595 call_smi_watchers(int i
, struct device
*dev
)
597 struct ipmi_smi_watcher
*w
;
599 list_for_each_entry(w
, &smi_watchers
, link
) {
600 if (try_module_get(w
->owner
)) {
602 module_put(w
->owner
);
608 ipmi_addr_equal(struct ipmi_addr
*addr1
, struct ipmi_addr
*addr2
)
610 if (addr1
->addr_type
!= addr2
->addr_type
)
613 if (addr1
->channel
!= addr2
->channel
)
616 if (addr1
->addr_type
== IPMI_SYSTEM_INTERFACE_ADDR_TYPE
) {
617 struct ipmi_system_interface_addr
*smi_addr1
618 = (struct ipmi_system_interface_addr
*) addr1
;
619 struct ipmi_system_interface_addr
*smi_addr2
620 = (struct ipmi_system_interface_addr
*) addr2
;
621 return (smi_addr1
->lun
== smi_addr2
->lun
);
624 if (is_ipmb_addr(addr1
) || is_ipmb_bcast_addr(addr1
)) {
625 struct ipmi_ipmb_addr
*ipmb_addr1
626 = (struct ipmi_ipmb_addr
*) addr1
;
627 struct ipmi_ipmb_addr
*ipmb_addr2
628 = (struct ipmi_ipmb_addr
*) addr2
;
630 return ((ipmb_addr1
->slave_addr
== ipmb_addr2
->slave_addr
)
631 && (ipmb_addr1
->lun
== ipmb_addr2
->lun
));
634 if (is_lan_addr(addr1
)) {
635 struct ipmi_lan_addr
*lan_addr1
636 = (struct ipmi_lan_addr
*) addr1
;
637 struct ipmi_lan_addr
*lan_addr2
638 = (struct ipmi_lan_addr
*) addr2
;
640 return ((lan_addr1
->remote_SWID
== lan_addr2
->remote_SWID
)
641 && (lan_addr1
->local_SWID
== lan_addr2
->local_SWID
)
642 && (lan_addr1
->session_handle
643 == lan_addr2
->session_handle
)
644 && (lan_addr1
->lun
== lan_addr2
->lun
));
650 int ipmi_validate_addr(struct ipmi_addr
*addr
, int len
)
652 if (len
< sizeof(struct ipmi_system_interface_addr
))
655 if (addr
->addr_type
== IPMI_SYSTEM_INTERFACE_ADDR_TYPE
) {
656 if (addr
->channel
!= IPMI_BMC_CHANNEL
)
661 if ((addr
->channel
== IPMI_BMC_CHANNEL
)
662 || (addr
->channel
>= IPMI_MAX_CHANNELS
)
663 || (addr
->channel
< 0))
666 if (is_ipmb_addr(addr
) || is_ipmb_bcast_addr(addr
)) {
667 if (len
< sizeof(struct ipmi_ipmb_addr
))
672 if (is_lan_addr(addr
)) {
673 if (len
< sizeof(struct ipmi_lan_addr
))
680 EXPORT_SYMBOL(ipmi_validate_addr
);
682 unsigned int ipmi_addr_length(int addr_type
)
684 if (addr_type
== IPMI_SYSTEM_INTERFACE_ADDR_TYPE
)
685 return sizeof(struct ipmi_system_interface_addr
);
687 if ((addr_type
== IPMI_IPMB_ADDR_TYPE
)
688 || (addr_type
== IPMI_IPMB_BROADCAST_ADDR_TYPE
))
689 return sizeof(struct ipmi_ipmb_addr
);
691 if (addr_type
== IPMI_LAN_ADDR_TYPE
)
692 return sizeof(struct ipmi_lan_addr
);
696 EXPORT_SYMBOL(ipmi_addr_length
);
698 static void deliver_response(struct ipmi_recv_msg
*msg
)
701 ipmi_smi_t intf
= msg
->user_msg_data
;
703 /* Special handling for NULL users. */
704 if (intf
->null_user_handler
) {
705 intf
->null_user_handler(intf
, msg
);
706 ipmi_inc_stat(intf
, handled_local_responses
);
708 /* No handler, so give up. */
709 ipmi_inc_stat(intf
, unhandled_local_responses
);
711 ipmi_free_recv_msg(msg
);
713 ipmi_user_t user
= msg
->user
;
714 user
->handler
->ipmi_recv_hndl(msg
, user
->handler_data
);
719 deliver_err_response(struct ipmi_recv_msg
*msg
, int err
)
721 msg
->recv_type
= IPMI_RESPONSE_RECV_TYPE
;
722 msg
->msg_data
[0] = err
;
723 msg
->msg
.netfn
|= 1; /* Convert to a response. */
724 msg
->msg
.data_len
= 1;
725 msg
->msg
.data
= msg
->msg_data
;
726 deliver_response(msg
);
730 * Find the next sequence number not being used and add the given
731 * message with the given timeout to the sequence table. This must be
732 * called with the interface's seq_lock held.
734 static int intf_next_seq(ipmi_smi_t intf
,
735 struct ipmi_recv_msg
*recv_msg
,
736 unsigned long timeout
,
745 for (i
= intf
->curr_seq
; (i
+1)%IPMI_IPMB_NUM_SEQ
!= intf
->curr_seq
;
746 i
= (i
+1)%IPMI_IPMB_NUM_SEQ
) {
747 if (!intf
->seq_table
[i
].inuse
)
751 if (!intf
->seq_table
[i
].inuse
) {
752 intf
->seq_table
[i
].recv_msg
= recv_msg
;
755 * Start with the maximum timeout, when the send response
756 * comes in we will start the real timer.
758 intf
->seq_table
[i
].timeout
= MAX_MSG_TIMEOUT
;
759 intf
->seq_table
[i
].orig_timeout
= timeout
;
760 intf
->seq_table
[i
].retries_left
= retries
;
761 intf
->seq_table
[i
].broadcast
= broadcast
;
762 intf
->seq_table
[i
].inuse
= 1;
763 intf
->seq_table
[i
].seqid
= NEXT_SEQID(intf
->seq_table
[i
].seqid
);
765 *seqid
= intf
->seq_table
[i
].seqid
;
766 intf
->curr_seq
= (i
+1)%IPMI_IPMB_NUM_SEQ
;
775 * Return the receive message for the given sequence number and
776 * release the sequence number so it can be reused. Some other data
777 * is passed in to be sure the message matches up correctly (to help
778 * guard against message coming in after their timeout and the
779 * sequence number being reused).
781 static int intf_find_seq(ipmi_smi_t intf
,
786 struct ipmi_addr
*addr
,
787 struct ipmi_recv_msg
**recv_msg
)
792 if (seq
>= IPMI_IPMB_NUM_SEQ
)
795 spin_lock_irqsave(&(intf
->seq_lock
), flags
);
796 if (intf
->seq_table
[seq
].inuse
) {
797 struct ipmi_recv_msg
*msg
= intf
->seq_table
[seq
].recv_msg
;
799 if ((msg
->addr
.channel
== channel
) && (msg
->msg
.cmd
== cmd
)
800 && (msg
->msg
.netfn
== netfn
)
801 && (ipmi_addr_equal(addr
, &(msg
->addr
)))) {
803 intf
->seq_table
[seq
].inuse
= 0;
807 spin_unlock_irqrestore(&(intf
->seq_lock
), flags
);
813 /* Start the timer for a specific sequence table entry. */
814 static int intf_start_seq_timer(ipmi_smi_t intf
,
823 GET_SEQ_FROM_MSGID(msgid
, seq
, seqid
);
825 spin_lock_irqsave(&(intf
->seq_lock
), flags
);
827 * We do this verification because the user can be deleted
828 * while a message is outstanding.
830 if ((intf
->seq_table
[seq
].inuse
)
831 && (intf
->seq_table
[seq
].seqid
== seqid
)) {
832 struct seq_table
*ent
= &(intf
->seq_table
[seq
]);
833 ent
->timeout
= ent
->orig_timeout
;
836 spin_unlock_irqrestore(&(intf
->seq_lock
), flags
);
841 /* Got an error for the send message for a specific sequence number. */
842 static int intf_err_seq(ipmi_smi_t intf
,
850 struct ipmi_recv_msg
*msg
= NULL
;
853 GET_SEQ_FROM_MSGID(msgid
, seq
, seqid
);
855 spin_lock_irqsave(&(intf
->seq_lock
), flags
);
857 * We do this verification because the user can be deleted
858 * while a message is outstanding.
860 if ((intf
->seq_table
[seq
].inuse
)
861 && (intf
->seq_table
[seq
].seqid
== seqid
)) {
862 struct seq_table
*ent
= &(intf
->seq_table
[seq
]);
868 spin_unlock_irqrestore(&(intf
->seq_lock
), flags
);
871 deliver_err_response(msg
, err
);
877 int ipmi_create_user(unsigned int if_num
,
878 struct ipmi_user_hndl
*handler
,
883 ipmi_user_t new_user
;
888 * There is no module usecount here, because it's not
889 * required. Since this can only be used by and called from
890 * other modules, they will implicitly use this module, and
891 * thus this can't be removed unless the other modules are
899 * Make sure the driver is actually initialized, this handles
900 * problems with initialization order.
903 rv
= ipmi_init_msghandler();
908 * The init code doesn't return an error if it was turned
909 * off, but it won't initialize. Check that.
915 new_user
= kmalloc(sizeof(*new_user
), GFP_KERNEL
);
919 mutex_lock(&ipmi_interfaces_mutex
);
920 list_for_each_entry_rcu(intf
, &ipmi_interfaces
, link
) {
921 if (intf
->intf_num
== if_num
)
924 /* Not found, return an error */
929 /* Note that each existing user holds a refcount to the interface. */
930 kref_get(&intf
->refcount
);
932 kref_init(&new_user
->refcount
);
933 new_user
->handler
= handler
;
934 new_user
->handler_data
= handler_data
;
935 new_user
->intf
= intf
;
936 new_user
->gets_events
= 0;
938 if (!try_module_get(intf
->handlers
->owner
)) {
943 if (intf
->handlers
->inc_usecount
) {
944 rv
= intf
->handlers
->inc_usecount(intf
->send_info
);
946 module_put(intf
->handlers
->owner
);
952 * Hold the lock so intf->handlers is guaranteed to be good
955 mutex_unlock(&ipmi_interfaces_mutex
);
958 spin_lock_irqsave(&intf
->seq_lock
, flags
);
959 list_add_rcu(&new_user
->link
, &intf
->users
);
960 spin_unlock_irqrestore(&intf
->seq_lock
, flags
);
965 kref_put(&intf
->refcount
, intf_free
);
967 mutex_unlock(&ipmi_interfaces_mutex
);
971 EXPORT_SYMBOL(ipmi_create_user
);
973 int ipmi_get_smi_info(int if_num
, struct ipmi_smi_info
*data
)
977 struct ipmi_smi_handlers
*handlers
;
979 mutex_lock(&ipmi_interfaces_mutex
);
980 list_for_each_entry_rcu(intf
, &ipmi_interfaces
, link
) {
981 if (intf
->intf_num
== if_num
)
984 /* Not found, return an error */
986 mutex_unlock(&ipmi_interfaces_mutex
);
990 handlers
= intf
->handlers
;
992 if (handlers
->get_smi_info
)
993 rv
= handlers
->get_smi_info(intf
->send_info
, data
);
994 mutex_unlock(&ipmi_interfaces_mutex
);
998 EXPORT_SYMBOL(ipmi_get_smi_info
);
1000 static void free_user(struct kref
*ref
)
1002 ipmi_user_t user
= container_of(ref
, struct ipmi_user
, refcount
);
1006 int ipmi_destroy_user(ipmi_user_t user
)
1008 ipmi_smi_t intf
= user
->intf
;
1010 unsigned long flags
;
1011 struct cmd_rcvr
*rcvr
;
1012 struct cmd_rcvr
*rcvrs
= NULL
;
1016 /* Remove the user from the interface's sequence table. */
1017 spin_lock_irqsave(&intf
->seq_lock
, flags
);
1018 list_del_rcu(&user
->link
);
1020 for (i
= 0; i
< IPMI_IPMB_NUM_SEQ
; i
++) {
1021 if (intf
->seq_table
[i
].inuse
1022 && (intf
->seq_table
[i
].recv_msg
->user
== user
)) {
1023 intf
->seq_table
[i
].inuse
= 0;
1024 ipmi_free_recv_msg(intf
->seq_table
[i
].recv_msg
);
1027 spin_unlock_irqrestore(&intf
->seq_lock
, flags
);
1030 * Remove the user from the command receiver's table. First
1031 * we build a list of everything (not using the standard link,
1032 * since other things may be using it till we do
1033 * synchronize_rcu()) then free everything in that list.
1035 mutex_lock(&intf
->cmd_rcvrs_mutex
);
1036 list_for_each_entry_rcu(rcvr
, &intf
->cmd_rcvrs
, link
) {
1037 if (rcvr
->user
== user
) {
1038 list_del_rcu(&rcvr
->link
);
1043 mutex_unlock(&intf
->cmd_rcvrs_mutex
);
1051 mutex_lock(&ipmi_interfaces_mutex
);
1052 if (intf
->handlers
) {
1053 module_put(intf
->handlers
->owner
);
1054 if (intf
->handlers
->dec_usecount
)
1055 intf
->handlers
->dec_usecount(intf
->send_info
);
1057 mutex_unlock(&ipmi_interfaces_mutex
);
1059 kref_put(&intf
->refcount
, intf_free
);
1061 kref_put(&user
->refcount
, free_user
);
1065 EXPORT_SYMBOL(ipmi_destroy_user
);
1067 void ipmi_get_version(ipmi_user_t user
,
1068 unsigned char *major
,
1069 unsigned char *minor
)
1071 *major
= user
->intf
->ipmi_version_major
;
1072 *minor
= user
->intf
->ipmi_version_minor
;
1074 EXPORT_SYMBOL(ipmi_get_version
);
1076 int ipmi_set_my_address(ipmi_user_t user
,
1077 unsigned int channel
,
1078 unsigned char address
)
1080 if (channel
>= IPMI_MAX_CHANNELS
)
1082 user
->intf
->channels
[channel
].address
= address
;
1085 EXPORT_SYMBOL(ipmi_set_my_address
);
1087 int ipmi_get_my_address(ipmi_user_t user
,
1088 unsigned int channel
,
1089 unsigned char *address
)
1091 if (channel
>= IPMI_MAX_CHANNELS
)
1093 *address
= user
->intf
->channels
[channel
].address
;
1096 EXPORT_SYMBOL(ipmi_get_my_address
);
1098 int ipmi_set_my_LUN(ipmi_user_t user
,
1099 unsigned int channel
,
1102 if (channel
>= IPMI_MAX_CHANNELS
)
1104 user
->intf
->channels
[channel
].lun
= LUN
& 0x3;
1107 EXPORT_SYMBOL(ipmi_set_my_LUN
);
1109 int ipmi_get_my_LUN(ipmi_user_t user
,
1110 unsigned int channel
,
1111 unsigned char *address
)
1113 if (channel
>= IPMI_MAX_CHANNELS
)
1115 *address
= user
->intf
->channels
[channel
].lun
;
1118 EXPORT_SYMBOL(ipmi_get_my_LUN
);
1120 int ipmi_get_maintenance_mode(ipmi_user_t user
)
1123 unsigned long flags
;
1125 spin_lock_irqsave(&user
->intf
->maintenance_mode_lock
, flags
);
1126 mode
= user
->intf
->maintenance_mode
;
1127 spin_unlock_irqrestore(&user
->intf
->maintenance_mode_lock
, flags
);
1131 EXPORT_SYMBOL(ipmi_get_maintenance_mode
);
1133 static void maintenance_mode_update(ipmi_smi_t intf
)
1135 if (intf
->handlers
->set_maintenance_mode
)
1136 intf
->handlers
->set_maintenance_mode(
1137 intf
->send_info
, intf
->maintenance_mode_enable
);
1140 int ipmi_set_maintenance_mode(ipmi_user_t user
, int mode
)
1143 unsigned long flags
;
1144 ipmi_smi_t intf
= user
->intf
;
1146 spin_lock_irqsave(&intf
->maintenance_mode_lock
, flags
);
1147 if (intf
->maintenance_mode
!= mode
) {
1149 case IPMI_MAINTENANCE_MODE_AUTO
:
1150 intf
->maintenance_mode
= mode
;
1151 intf
->maintenance_mode_enable
1152 = (intf
->auto_maintenance_timeout
> 0);
1155 case IPMI_MAINTENANCE_MODE_OFF
:
1156 intf
->maintenance_mode
= mode
;
1157 intf
->maintenance_mode_enable
= 0;
1160 case IPMI_MAINTENANCE_MODE_ON
:
1161 intf
->maintenance_mode
= mode
;
1162 intf
->maintenance_mode_enable
= 1;
1170 maintenance_mode_update(intf
);
1173 spin_unlock_irqrestore(&intf
->maintenance_mode_lock
, flags
);
1177 EXPORT_SYMBOL(ipmi_set_maintenance_mode
);
1179 int ipmi_set_gets_events(ipmi_user_t user
, int val
)
1181 unsigned long flags
;
1182 ipmi_smi_t intf
= user
->intf
;
1183 struct ipmi_recv_msg
*msg
, *msg2
;
1184 struct list_head msgs
;
1186 INIT_LIST_HEAD(&msgs
);
1188 spin_lock_irqsave(&intf
->events_lock
, flags
);
1189 user
->gets_events
= val
;
1191 if (intf
->delivering_events
)
1193 * Another thread is delivering events for this, so
1194 * let it handle any new events.
1198 /* Deliver any queued events. */
1199 while (user
->gets_events
&& !list_empty(&intf
->waiting_events
)) {
1200 list_for_each_entry_safe(msg
, msg2
, &intf
->waiting_events
, link
)
1201 list_move_tail(&msg
->link
, &msgs
);
1202 intf
->waiting_events_count
= 0;
1203 if (intf
->event_msg_printed
) {
1204 printk(KERN_WARNING PFX
"Event queue no longer"
1206 intf
->event_msg_printed
= 0;
1209 intf
->delivering_events
= 1;
1210 spin_unlock_irqrestore(&intf
->events_lock
, flags
);
1212 list_for_each_entry_safe(msg
, msg2
, &msgs
, link
) {
1214 kref_get(&user
->refcount
);
1215 deliver_response(msg
);
1218 spin_lock_irqsave(&intf
->events_lock
, flags
);
1219 intf
->delivering_events
= 0;
1223 spin_unlock_irqrestore(&intf
->events_lock
, flags
);
1227 EXPORT_SYMBOL(ipmi_set_gets_events
);
1229 static struct cmd_rcvr
*find_cmd_rcvr(ipmi_smi_t intf
,
1230 unsigned char netfn
,
1234 struct cmd_rcvr
*rcvr
;
1236 list_for_each_entry_rcu(rcvr
, &intf
->cmd_rcvrs
, link
) {
1237 if ((rcvr
->netfn
== netfn
) && (rcvr
->cmd
== cmd
)
1238 && (rcvr
->chans
& (1 << chan
)))
1244 static int is_cmd_rcvr_exclusive(ipmi_smi_t intf
,
1245 unsigned char netfn
,
1249 struct cmd_rcvr
*rcvr
;
1251 list_for_each_entry_rcu(rcvr
, &intf
->cmd_rcvrs
, link
) {
1252 if ((rcvr
->netfn
== netfn
) && (rcvr
->cmd
== cmd
)
1253 && (rcvr
->chans
& chans
))
1259 int ipmi_register_for_cmd(ipmi_user_t user
,
1260 unsigned char netfn
,
1264 ipmi_smi_t intf
= user
->intf
;
1265 struct cmd_rcvr
*rcvr
;
1269 rcvr
= kmalloc(sizeof(*rcvr
), GFP_KERNEL
);
1273 rcvr
->netfn
= netfn
;
1274 rcvr
->chans
= chans
;
1277 mutex_lock(&intf
->cmd_rcvrs_mutex
);
1278 /* Make sure the command/netfn is not already registered. */
1279 if (!is_cmd_rcvr_exclusive(intf
, netfn
, cmd
, chans
)) {
1284 list_add_rcu(&rcvr
->link
, &intf
->cmd_rcvrs
);
1287 mutex_unlock(&intf
->cmd_rcvrs_mutex
);
1293 EXPORT_SYMBOL(ipmi_register_for_cmd
);
1295 int ipmi_unregister_for_cmd(ipmi_user_t user
,
1296 unsigned char netfn
,
1300 ipmi_smi_t intf
= user
->intf
;
1301 struct cmd_rcvr
*rcvr
;
1302 struct cmd_rcvr
*rcvrs
= NULL
;
1303 int i
, rv
= -ENOENT
;
1305 mutex_lock(&intf
->cmd_rcvrs_mutex
);
1306 for (i
= 0; i
< IPMI_NUM_CHANNELS
; i
++) {
1307 if (((1 << i
) & chans
) == 0)
1309 rcvr
= find_cmd_rcvr(intf
, netfn
, cmd
, i
);
1312 if (rcvr
->user
== user
) {
1314 rcvr
->chans
&= ~chans
;
1315 if (rcvr
->chans
== 0) {
1316 list_del_rcu(&rcvr
->link
);
1322 mutex_unlock(&intf
->cmd_rcvrs_mutex
);
1331 EXPORT_SYMBOL(ipmi_unregister_for_cmd
);
1333 static unsigned char
1334 ipmb_checksum(unsigned char *data
, int size
)
1336 unsigned char csum
= 0;
1338 for (; size
> 0; size
--, data
++)
1344 static inline void format_ipmb_msg(struct ipmi_smi_msg
*smi_msg
,
1345 struct kernel_ipmi_msg
*msg
,
1346 struct ipmi_ipmb_addr
*ipmb_addr
,
1348 unsigned char ipmb_seq
,
1350 unsigned char source_address
,
1351 unsigned char source_lun
)
1355 /* Format the IPMB header data. */
1356 smi_msg
->data
[0] = (IPMI_NETFN_APP_REQUEST
<< 2);
1357 smi_msg
->data
[1] = IPMI_SEND_MSG_CMD
;
1358 smi_msg
->data
[2] = ipmb_addr
->channel
;
1360 smi_msg
->data
[3] = 0;
1361 smi_msg
->data
[i
+3] = ipmb_addr
->slave_addr
;
1362 smi_msg
->data
[i
+4] = (msg
->netfn
<< 2) | (ipmb_addr
->lun
& 0x3);
1363 smi_msg
->data
[i
+5] = ipmb_checksum(&(smi_msg
->data
[i
+3]), 2);
1364 smi_msg
->data
[i
+6] = source_address
;
1365 smi_msg
->data
[i
+7] = (ipmb_seq
<< 2) | source_lun
;
1366 smi_msg
->data
[i
+8] = msg
->cmd
;
1368 /* Now tack on the data to the message. */
1369 if (msg
->data_len
> 0)
1370 memcpy(&(smi_msg
->data
[i
+9]), msg
->data
,
1372 smi_msg
->data_size
= msg
->data_len
+ 9;
1374 /* Now calculate the checksum and tack it on. */
1375 smi_msg
->data
[i
+smi_msg
->data_size
]
1376 = ipmb_checksum(&(smi_msg
->data
[i
+6]),
1377 smi_msg
->data_size
-6);
1380 * Add on the checksum size and the offset from the
1383 smi_msg
->data_size
+= 1 + i
;
1385 smi_msg
->msgid
= msgid
;
1388 static inline void format_lan_msg(struct ipmi_smi_msg
*smi_msg
,
1389 struct kernel_ipmi_msg
*msg
,
1390 struct ipmi_lan_addr
*lan_addr
,
1392 unsigned char ipmb_seq
,
1393 unsigned char source_lun
)
1395 /* Format the IPMB header data. */
1396 smi_msg
->data
[0] = (IPMI_NETFN_APP_REQUEST
<< 2);
1397 smi_msg
->data
[1] = IPMI_SEND_MSG_CMD
;
1398 smi_msg
->data
[2] = lan_addr
->channel
;
1399 smi_msg
->data
[3] = lan_addr
->session_handle
;
1400 smi_msg
->data
[4] = lan_addr
->remote_SWID
;
1401 smi_msg
->data
[5] = (msg
->netfn
<< 2) | (lan_addr
->lun
& 0x3);
1402 smi_msg
->data
[6] = ipmb_checksum(&(smi_msg
->data
[4]), 2);
1403 smi_msg
->data
[7] = lan_addr
->local_SWID
;
1404 smi_msg
->data
[8] = (ipmb_seq
<< 2) | source_lun
;
1405 smi_msg
->data
[9] = msg
->cmd
;
1407 /* Now tack on the data to the message. */
1408 if (msg
->data_len
> 0)
1409 memcpy(&(smi_msg
->data
[10]), msg
->data
,
1411 smi_msg
->data_size
= msg
->data_len
+ 10;
1413 /* Now calculate the checksum and tack it on. */
1414 smi_msg
->data
[smi_msg
->data_size
]
1415 = ipmb_checksum(&(smi_msg
->data
[7]),
1416 smi_msg
->data_size
-7);
1419 * Add on the checksum size and the offset from the
1422 smi_msg
->data_size
+= 1;
1424 smi_msg
->msgid
= msgid
;
1428 * Separate from ipmi_request so that the user does not have to be
1429 * supplied in certain circumstances (mainly at panic time). If
1430 * messages are supplied, they will be freed, even if an error
1433 static int i_ipmi_request(ipmi_user_t user
,
1435 struct ipmi_addr
*addr
,
1437 struct kernel_ipmi_msg
*msg
,
1438 void *user_msg_data
,
1440 struct ipmi_recv_msg
*supplied_recv
,
1442 unsigned char source_address
,
1443 unsigned char source_lun
,
1445 unsigned int retry_time_ms
)
1448 struct ipmi_smi_msg
*smi_msg
;
1449 struct ipmi_recv_msg
*recv_msg
;
1450 unsigned long flags
;
1451 struct ipmi_smi_handlers
*handlers
;
1455 recv_msg
= supplied_recv
;
1457 recv_msg
= ipmi_alloc_recv_msg();
1458 if (recv_msg
== NULL
)
1461 recv_msg
->user_msg_data
= user_msg_data
;
1464 smi_msg
= (struct ipmi_smi_msg
*) supplied_smi
;
1466 smi_msg
= ipmi_alloc_smi_msg();
1467 if (smi_msg
== NULL
) {
1468 ipmi_free_recv_msg(recv_msg
);
1474 handlers
= intf
->handlers
;
1480 recv_msg
->user
= user
;
1482 kref_get(&user
->refcount
);
1483 recv_msg
->msgid
= msgid
;
1485 * Store the message to send in the receive message so timeout
1486 * responses can get the proper response data.
1488 recv_msg
->msg
= *msg
;
1490 if (addr
->addr_type
== IPMI_SYSTEM_INTERFACE_ADDR_TYPE
) {
1491 struct ipmi_system_interface_addr
*smi_addr
;
1493 if (msg
->netfn
& 1) {
1494 /* Responses are not allowed to the SMI. */
1499 smi_addr
= (struct ipmi_system_interface_addr
*) addr
;
1500 if (smi_addr
->lun
> 3) {
1501 ipmi_inc_stat(intf
, sent_invalid_commands
);
1506 memcpy(&recv_msg
->addr
, smi_addr
, sizeof(*smi_addr
));
1508 if ((msg
->netfn
== IPMI_NETFN_APP_REQUEST
)
1509 && ((msg
->cmd
== IPMI_SEND_MSG_CMD
)
1510 || (msg
->cmd
== IPMI_GET_MSG_CMD
)
1511 || (msg
->cmd
== IPMI_READ_EVENT_MSG_BUFFER_CMD
))) {
1513 * We don't let the user do these, since we manage
1514 * the sequence numbers.
1516 ipmi_inc_stat(intf
, sent_invalid_commands
);
1521 if (((msg
->netfn
== IPMI_NETFN_APP_REQUEST
)
1522 && ((msg
->cmd
== IPMI_COLD_RESET_CMD
)
1523 || (msg
->cmd
== IPMI_WARM_RESET_CMD
)))
1524 || (msg
->netfn
== IPMI_NETFN_FIRMWARE_REQUEST
)) {
1525 spin_lock_irqsave(&intf
->maintenance_mode_lock
, flags
);
1526 intf
->auto_maintenance_timeout
1527 = IPMI_MAINTENANCE_MODE_TIMEOUT
;
1528 if (!intf
->maintenance_mode
1529 && !intf
->maintenance_mode_enable
) {
1530 intf
->maintenance_mode_enable
= 1;
1531 maintenance_mode_update(intf
);
1533 spin_unlock_irqrestore(&intf
->maintenance_mode_lock
,
1537 if ((msg
->data_len
+ 2) > IPMI_MAX_MSG_LENGTH
) {
1538 ipmi_inc_stat(intf
, sent_invalid_commands
);
1543 smi_msg
->data
[0] = (msg
->netfn
<< 2) | (smi_addr
->lun
& 0x3);
1544 smi_msg
->data
[1] = msg
->cmd
;
1545 smi_msg
->msgid
= msgid
;
1546 smi_msg
->user_data
= recv_msg
;
1547 if (msg
->data_len
> 0)
1548 memcpy(&(smi_msg
->data
[2]), msg
->data
, msg
->data_len
);
1549 smi_msg
->data_size
= msg
->data_len
+ 2;
1550 ipmi_inc_stat(intf
, sent_local_commands
);
1551 } else if (is_ipmb_addr(addr
) || is_ipmb_bcast_addr(addr
)) {
1552 struct ipmi_ipmb_addr
*ipmb_addr
;
1553 unsigned char ipmb_seq
;
1557 if (addr
->channel
>= IPMI_MAX_CHANNELS
) {
1558 ipmi_inc_stat(intf
, sent_invalid_commands
);
1563 if (intf
->channels
[addr
->channel
].medium
1564 != IPMI_CHANNEL_MEDIUM_IPMB
) {
1565 ipmi_inc_stat(intf
, sent_invalid_commands
);
1571 if (addr
->addr_type
== IPMI_IPMB_BROADCAST_ADDR_TYPE
)
1572 retries
= 0; /* Don't retry broadcasts. */
1576 if (addr
->addr_type
== IPMI_IPMB_BROADCAST_ADDR_TYPE
) {
1578 * Broadcasts add a zero at the beginning of the
1579 * message, but otherwise is the same as an IPMB
1582 addr
->addr_type
= IPMI_IPMB_ADDR_TYPE
;
1587 /* Default to 1 second retries. */
1588 if (retry_time_ms
== 0)
1589 retry_time_ms
= 1000;
1592 * 9 for the header and 1 for the checksum, plus
1593 * possibly one for the broadcast.
1595 if ((msg
->data_len
+ 10 + broadcast
) > IPMI_MAX_MSG_LENGTH
) {
1596 ipmi_inc_stat(intf
, sent_invalid_commands
);
1601 ipmb_addr
= (struct ipmi_ipmb_addr
*) addr
;
1602 if (ipmb_addr
->lun
> 3) {
1603 ipmi_inc_stat(intf
, sent_invalid_commands
);
1608 memcpy(&recv_msg
->addr
, ipmb_addr
, sizeof(*ipmb_addr
));
1610 if (recv_msg
->msg
.netfn
& 0x1) {
1612 * It's a response, so use the user's sequence
1615 ipmi_inc_stat(intf
, sent_ipmb_responses
);
1616 format_ipmb_msg(smi_msg
, msg
, ipmb_addr
, msgid
,
1618 source_address
, source_lun
);
1621 * Save the receive message so we can use it
1622 * to deliver the response.
1624 smi_msg
->user_data
= recv_msg
;
1626 /* It's a command, so get a sequence for it. */
1628 spin_lock_irqsave(&(intf
->seq_lock
), flags
);
1631 * Create a sequence number with a 1 second
1632 * timeout and 4 retries.
1634 rv
= intf_next_seq(intf
,
1643 * We have used up all the sequence numbers,
1644 * probably, so abort.
1646 spin_unlock_irqrestore(&(intf
->seq_lock
),
1651 ipmi_inc_stat(intf
, sent_ipmb_commands
);
1654 * Store the sequence number in the message,
1655 * so that when the send message response
1656 * comes back we can start the timer.
1658 format_ipmb_msg(smi_msg
, msg
, ipmb_addr
,
1659 STORE_SEQ_IN_MSGID(ipmb_seq
, seqid
),
1660 ipmb_seq
, broadcast
,
1661 source_address
, source_lun
);
1664 * Copy the message into the recv message data, so we
1665 * can retransmit it later if necessary.
1667 memcpy(recv_msg
->msg_data
, smi_msg
->data
,
1668 smi_msg
->data_size
);
1669 recv_msg
->msg
.data
= recv_msg
->msg_data
;
1670 recv_msg
->msg
.data_len
= smi_msg
->data_size
;
1673 * We don't unlock until here, because we need
1674 * to copy the completed message into the
1675 * recv_msg before we release the lock.
1676 * Otherwise, race conditions may bite us. I
1677 * know that's pretty paranoid, but I prefer
1680 spin_unlock_irqrestore(&(intf
->seq_lock
), flags
);
1682 } else if (is_lan_addr(addr
)) {
1683 struct ipmi_lan_addr
*lan_addr
;
1684 unsigned char ipmb_seq
;
1687 if (addr
->channel
>= IPMI_MAX_CHANNELS
) {
1688 ipmi_inc_stat(intf
, sent_invalid_commands
);
1693 if ((intf
->channels
[addr
->channel
].medium
1694 != IPMI_CHANNEL_MEDIUM_8023LAN
)
1695 && (intf
->channels
[addr
->channel
].medium
1696 != IPMI_CHANNEL_MEDIUM_ASYNC
)) {
1697 ipmi_inc_stat(intf
, sent_invalid_commands
);
1704 /* Default to 1 second retries. */
1705 if (retry_time_ms
== 0)
1706 retry_time_ms
= 1000;
1708 /* 11 for the header and 1 for the checksum. */
1709 if ((msg
->data_len
+ 12) > IPMI_MAX_MSG_LENGTH
) {
1710 ipmi_inc_stat(intf
, sent_invalid_commands
);
1715 lan_addr
= (struct ipmi_lan_addr
*) addr
;
1716 if (lan_addr
->lun
> 3) {
1717 ipmi_inc_stat(intf
, sent_invalid_commands
);
1722 memcpy(&recv_msg
->addr
, lan_addr
, sizeof(*lan_addr
));
1724 if (recv_msg
->msg
.netfn
& 0x1) {
1726 * It's a response, so use the user's sequence
1729 ipmi_inc_stat(intf
, sent_lan_responses
);
1730 format_lan_msg(smi_msg
, msg
, lan_addr
, msgid
,
1734 * Save the receive message so we can use it
1735 * to deliver the response.
1737 smi_msg
->user_data
= recv_msg
;
1739 /* It's a command, so get a sequence for it. */
1741 spin_lock_irqsave(&(intf
->seq_lock
), flags
);
1744 * Create a sequence number with a 1 second
1745 * timeout and 4 retries.
1747 rv
= intf_next_seq(intf
,
1756 * We have used up all the sequence numbers,
1757 * probably, so abort.
1759 spin_unlock_irqrestore(&(intf
->seq_lock
),
1764 ipmi_inc_stat(intf
, sent_lan_commands
);
1767 * Store the sequence number in the message,
1768 * so that when the send message response
1769 * comes back we can start the timer.
1771 format_lan_msg(smi_msg
, msg
, lan_addr
,
1772 STORE_SEQ_IN_MSGID(ipmb_seq
, seqid
),
1773 ipmb_seq
, source_lun
);
1776 * Copy the message into the recv message data, so we
1777 * can retransmit it later if necessary.
1779 memcpy(recv_msg
->msg_data
, smi_msg
->data
,
1780 smi_msg
->data_size
);
1781 recv_msg
->msg
.data
= recv_msg
->msg_data
;
1782 recv_msg
->msg
.data_len
= smi_msg
->data_size
;
1785 * We don't unlock until here, because we need
1786 * to copy the completed message into the
1787 * recv_msg before we release the lock.
1788 * Otherwise, race conditions may bite us. I
1789 * know that's pretty paranoid, but I prefer
1792 spin_unlock_irqrestore(&(intf
->seq_lock
), flags
);
1795 /* Unknown address type. */
1796 ipmi_inc_stat(intf
, sent_invalid_commands
);
1804 for (m
= 0; m
< smi_msg
->data_size
; m
++)
1805 printk(" %2.2x", smi_msg
->data
[m
]);
1810 handlers
->sender(intf
->send_info
, smi_msg
, priority
);
1817 ipmi_free_smi_msg(smi_msg
);
1818 ipmi_free_recv_msg(recv_msg
);
1822 static int check_addr(ipmi_smi_t intf
,
1823 struct ipmi_addr
*addr
,
1824 unsigned char *saddr
,
1827 if (addr
->channel
>= IPMI_MAX_CHANNELS
)
1829 *lun
= intf
->channels
[addr
->channel
].lun
;
1830 *saddr
= intf
->channels
[addr
->channel
].address
;
1834 int ipmi_request_settime(ipmi_user_t user
,
1835 struct ipmi_addr
*addr
,
1837 struct kernel_ipmi_msg
*msg
,
1838 void *user_msg_data
,
1841 unsigned int retry_time_ms
)
1843 unsigned char saddr
, lun
;
1848 rv
= check_addr(user
->intf
, addr
, &saddr
, &lun
);
1851 return i_ipmi_request(user
,
1864 EXPORT_SYMBOL(ipmi_request_settime
);
1866 int ipmi_request_supply_msgs(ipmi_user_t user
,
1867 struct ipmi_addr
*addr
,
1869 struct kernel_ipmi_msg
*msg
,
1870 void *user_msg_data
,
1872 struct ipmi_recv_msg
*supplied_recv
,
1875 unsigned char saddr
, lun
;
1880 rv
= check_addr(user
->intf
, addr
, &saddr
, &lun
);
1883 return i_ipmi_request(user
,
1896 EXPORT_SYMBOL(ipmi_request_supply_msgs
);
1898 #ifdef CONFIG_PROC_FS
1899 static int ipmb_file_read_proc(char *page
, char **start
, off_t off
,
1900 int count
, int *eof
, void *data
)
1902 char *out
= (char *) page
;
1903 ipmi_smi_t intf
= data
;
1907 for (i
= 0; i
< IPMI_MAX_CHANNELS
; i
++)
1908 rv
+= sprintf(out
+rv
, "%x ", intf
->channels
[i
].address
);
1909 out
[rv
-1] = '\n'; /* Replace the final space with a newline */
1915 static int version_file_read_proc(char *page
, char **start
, off_t off
,
1916 int count
, int *eof
, void *data
)
1918 char *out
= (char *) page
;
1919 ipmi_smi_t intf
= data
;
1921 return sprintf(out
, "%u.%u\n",
1922 ipmi_version_major(&intf
->bmc
->id
),
1923 ipmi_version_minor(&intf
->bmc
->id
));
1926 static int stat_file_read_proc(char *page
, char **start
, off_t off
,
1927 int count
, int *eof
, void *data
)
1929 char *out
= (char *) page
;
1930 ipmi_smi_t intf
= data
;
1932 out
+= sprintf(out
, "sent_invalid_commands: %u\n",
1933 ipmi_get_stat(intf
, sent_invalid_commands
));
1934 out
+= sprintf(out
, "sent_local_commands: %u\n",
1935 ipmi_get_stat(intf
, sent_local_commands
));
1936 out
+= sprintf(out
, "handled_local_responses: %u\n",
1937 ipmi_get_stat(intf
, handled_local_responses
));
1938 out
+= sprintf(out
, "unhandled_local_responses: %u\n",
1939 ipmi_get_stat(intf
, unhandled_local_responses
));
1940 out
+= sprintf(out
, "sent_ipmb_commands: %u\n",
1941 ipmi_get_stat(intf
, sent_ipmb_commands
));
1942 out
+= sprintf(out
, "sent_ipmb_command_errs: %u\n",
1943 ipmi_get_stat(intf
, sent_ipmb_command_errs
));
1944 out
+= sprintf(out
, "retransmitted_ipmb_commands: %u\n",
1945 ipmi_get_stat(intf
, retransmitted_ipmb_commands
));
1946 out
+= sprintf(out
, "timed_out_ipmb_commands: %u\n",
1947 ipmi_get_stat(intf
, timed_out_ipmb_commands
));
1948 out
+= sprintf(out
, "timed_out_ipmb_broadcasts: %u\n",
1949 ipmi_get_stat(intf
, timed_out_ipmb_broadcasts
));
1950 out
+= sprintf(out
, "sent_ipmb_responses: %u\n",
1951 ipmi_get_stat(intf
, sent_ipmb_responses
));
1952 out
+= sprintf(out
, "handled_ipmb_responses: %u\n",
1953 ipmi_get_stat(intf
, handled_ipmb_responses
));
1954 out
+= sprintf(out
, "invalid_ipmb_responses: %u\n",
1955 ipmi_get_stat(intf
, invalid_ipmb_responses
));
1956 out
+= sprintf(out
, "unhandled_ipmb_responses: %u\n",
1957 ipmi_get_stat(intf
, unhandled_ipmb_responses
));
1958 out
+= sprintf(out
, "sent_lan_commands: %u\n",
1959 ipmi_get_stat(intf
, sent_lan_commands
));
1960 out
+= sprintf(out
, "sent_lan_command_errs: %u\n",
1961 ipmi_get_stat(intf
, sent_lan_command_errs
));
1962 out
+= sprintf(out
, "retransmitted_lan_commands: %u\n",
1963 ipmi_get_stat(intf
, retransmitted_lan_commands
));
1964 out
+= sprintf(out
, "timed_out_lan_commands: %u\n",
1965 ipmi_get_stat(intf
, timed_out_lan_commands
));
1966 out
+= sprintf(out
, "sent_lan_responses: %u\n",
1967 ipmi_get_stat(intf
, sent_lan_responses
));
1968 out
+= sprintf(out
, "handled_lan_responses: %u\n",
1969 ipmi_get_stat(intf
, handled_lan_responses
));
1970 out
+= sprintf(out
, "invalid_lan_responses: %u\n",
1971 ipmi_get_stat(intf
, invalid_lan_responses
));
1972 out
+= sprintf(out
, "unhandled_lan_responses: %u\n",
1973 ipmi_get_stat(intf
, unhandled_lan_responses
));
1974 out
+= sprintf(out
, "handled_commands: %u\n",
1975 ipmi_get_stat(intf
, handled_commands
));
1976 out
+= sprintf(out
, "invalid_commands: %u\n",
1977 ipmi_get_stat(intf
, invalid_commands
));
1978 out
+= sprintf(out
, "unhandled_commands: %u\n",
1979 ipmi_get_stat(intf
, unhandled_commands
));
1980 out
+= sprintf(out
, "invalid_events: %u\n",
1981 ipmi_get_stat(intf
, invalid_events
));
1982 out
+= sprintf(out
, "events: %u\n",
1983 ipmi_get_stat(intf
, events
));
1984 out
+= sprintf(out
, "failed rexmit LAN msgs: %u\n",
1985 ipmi_get_stat(intf
, dropped_rexmit_lan_commands
));
1986 out
+= sprintf(out
, "failed rexmit IPMB msgs: %u\n",
1987 ipmi_get_stat(intf
, dropped_rexmit_ipmb_commands
));
1989 return (out
- ((char *) page
));
1991 #endif /* CONFIG_PROC_FS */
1993 int ipmi_smi_add_proc_entry(ipmi_smi_t smi
, char *name
,
1994 read_proc_t
*read_proc
,
1998 #ifdef CONFIG_PROC_FS
1999 struct proc_dir_entry
*file
;
2000 struct ipmi_proc_entry
*entry
;
2002 /* Create a list element. */
2003 entry
= kmalloc(sizeof(*entry
), GFP_KERNEL
);
2006 entry
->name
= kmalloc(strlen(name
)+1, GFP_KERNEL
);
2011 strcpy(entry
->name
, name
);
2013 file
= create_proc_entry(name
, 0, smi
->proc_dir
);
2020 file
->read_proc
= read_proc
;
2022 mutex_lock(&smi
->proc_entry_lock
);
2023 /* Stick it on the list. */
2024 entry
->next
= smi
->proc_entries
;
2025 smi
->proc_entries
= entry
;
2026 mutex_unlock(&smi
->proc_entry_lock
);
2028 #endif /* CONFIG_PROC_FS */
2032 EXPORT_SYMBOL(ipmi_smi_add_proc_entry
);
2034 static int add_proc_entries(ipmi_smi_t smi
, int num
)
2038 #ifdef CONFIG_PROC_FS
2039 sprintf(smi
->proc_dir_name
, "%d", num
);
2040 smi
->proc_dir
= proc_mkdir(smi
->proc_dir_name
, proc_ipmi_root
);
2045 rv
= ipmi_smi_add_proc_entry(smi
, "stats",
2046 stat_file_read_proc
,
2050 rv
= ipmi_smi_add_proc_entry(smi
, "ipmb",
2051 ipmb_file_read_proc
,
2055 rv
= ipmi_smi_add_proc_entry(smi
, "version",
2056 version_file_read_proc
,
2058 #endif /* CONFIG_PROC_FS */
2063 static void remove_proc_entries(ipmi_smi_t smi
)
2065 #ifdef CONFIG_PROC_FS
2066 struct ipmi_proc_entry
*entry
;
2068 mutex_lock(&smi
->proc_entry_lock
);
2069 while (smi
->proc_entries
) {
2070 entry
= smi
->proc_entries
;
2071 smi
->proc_entries
= entry
->next
;
2073 remove_proc_entry(entry
->name
, smi
->proc_dir
);
2077 mutex_unlock(&smi
->proc_entry_lock
);
2078 remove_proc_entry(smi
->proc_dir_name
, proc_ipmi_root
);
2079 #endif /* CONFIG_PROC_FS */
2082 static int __find_bmc_guid(struct device
*dev
, void *data
)
2084 unsigned char *id
= data
;
2085 struct bmc_device
*bmc
= dev_get_drvdata(dev
);
2086 return memcmp(bmc
->guid
, id
, 16) == 0;
2089 static struct bmc_device
*ipmi_find_bmc_guid(struct device_driver
*drv
,
2090 unsigned char *guid
)
2094 dev
= driver_find_device(drv
, NULL
, guid
, __find_bmc_guid
);
2096 return dev_get_drvdata(dev
);
2101 struct prod_dev_id
{
2102 unsigned int product_id
;
2103 unsigned char device_id
;
2106 static int __find_bmc_prod_dev_id(struct device
*dev
, void *data
)
2108 struct prod_dev_id
*id
= data
;
2109 struct bmc_device
*bmc
= dev_get_drvdata(dev
);
2111 return (bmc
->id
.product_id
== id
->product_id
2112 && bmc
->id
.device_id
== id
->device_id
);
2115 static struct bmc_device
*ipmi_find_bmc_prod_dev_id(
2116 struct device_driver
*drv
,
2117 unsigned int product_id
, unsigned char device_id
)
2119 struct prod_dev_id id
= {
2120 .product_id
= product_id
,
2121 .device_id
= device_id
,
2125 dev
= driver_find_device(drv
, NULL
, &id
, __find_bmc_prod_dev_id
);
2127 return dev_get_drvdata(dev
);
2132 static ssize_t
device_id_show(struct device
*dev
,
2133 struct device_attribute
*attr
,
2136 struct bmc_device
*bmc
= dev_get_drvdata(dev
);
2138 return snprintf(buf
, 10, "%u\n", bmc
->id
.device_id
);
2141 static ssize_t
provides_dev_sdrs_show(struct device
*dev
,
2142 struct device_attribute
*attr
,
2145 struct bmc_device
*bmc
= dev_get_drvdata(dev
);
2147 return snprintf(buf
, 10, "%u\n",
2148 (bmc
->id
.device_revision
& 0x80) >> 7);
2151 static ssize_t
revision_show(struct device
*dev
, struct device_attribute
*attr
,
2154 struct bmc_device
*bmc
= dev_get_drvdata(dev
);
2156 return snprintf(buf
, 20, "%u\n",
2157 bmc
->id
.device_revision
& 0x0F);
2160 static ssize_t
firmware_rev_show(struct device
*dev
,
2161 struct device_attribute
*attr
,
2164 struct bmc_device
*bmc
= dev_get_drvdata(dev
);
2166 return snprintf(buf
, 20, "%u.%x\n", bmc
->id
.firmware_revision_1
,
2167 bmc
->id
.firmware_revision_2
);
2170 static ssize_t
ipmi_version_show(struct device
*dev
,
2171 struct device_attribute
*attr
,
2174 struct bmc_device
*bmc
= dev_get_drvdata(dev
);
2176 return snprintf(buf
, 20, "%u.%u\n",
2177 ipmi_version_major(&bmc
->id
),
2178 ipmi_version_minor(&bmc
->id
));
2181 static ssize_t
add_dev_support_show(struct device
*dev
,
2182 struct device_attribute
*attr
,
2185 struct bmc_device
*bmc
= dev_get_drvdata(dev
);
2187 return snprintf(buf
, 10, "0x%02x\n",
2188 bmc
->id
.additional_device_support
);
2191 static ssize_t
manufacturer_id_show(struct device
*dev
,
2192 struct device_attribute
*attr
,
2195 struct bmc_device
*bmc
= dev_get_drvdata(dev
);
2197 return snprintf(buf
, 20, "0x%6.6x\n", bmc
->id
.manufacturer_id
);
2200 static ssize_t
product_id_show(struct device
*dev
,
2201 struct device_attribute
*attr
,
2204 struct bmc_device
*bmc
= dev_get_drvdata(dev
);
2206 return snprintf(buf
, 10, "0x%4.4x\n", bmc
->id
.product_id
);
2209 static ssize_t
aux_firmware_rev_show(struct device
*dev
,
2210 struct device_attribute
*attr
,
2213 struct bmc_device
*bmc
= dev_get_drvdata(dev
);
2215 return snprintf(buf
, 21, "0x%02x 0x%02x 0x%02x 0x%02x\n",
2216 bmc
->id
.aux_firmware_revision
[3],
2217 bmc
->id
.aux_firmware_revision
[2],
2218 bmc
->id
.aux_firmware_revision
[1],
2219 bmc
->id
.aux_firmware_revision
[0]);
2222 static ssize_t
guid_show(struct device
*dev
, struct device_attribute
*attr
,
2225 struct bmc_device
*bmc
= dev_get_drvdata(dev
);
2227 return snprintf(buf
, 100, "%Lx%Lx\n",
2228 (long long) bmc
->guid
[0],
2229 (long long) bmc
->guid
[8]);
2232 static void remove_files(struct bmc_device
*bmc
)
2237 device_remove_file(&bmc
->dev
->dev
,
2238 &bmc
->device_id_attr
);
2239 device_remove_file(&bmc
->dev
->dev
,
2240 &bmc
->provides_dev_sdrs_attr
);
2241 device_remove_file(&bmc
->dev
->dev
,
2242 &bmc
->revision_attr
);
2243 device_remove_file(&bmc
->dev
->dev
,
2244 &bmc
->firmware_rev_attr
);
2245 device_remove_file(&bmc
->dev
->dev
,
2246 &bmc
->version_attr
);
2247 device_remove_file(&bmc
->dev
->dev
,
2248 &bmc
->add_dev_support_attr
);
2249 device_remove_file(&bmc
->dev
->dev
,
2250 &bmc
->manufacturer_id_attr
);
2251 device_remove_file(&bmc
->dev
->dev
,
2252 &bmc
->product_id_attr
);
2254 if (bmc
->id
.aux_firmware_revision_set
)
2255 device_remove_file(&bmc
->dev
->dev
,
2256 &bmc
->aux_firmware_rev_attr
);
2258 device_remove_file(&bmc
->dev
->dev
,
2263 cleanup_bmc_device(struct kref
*ref
)
2265 struct bmc_device
*bmc
;
2267 bmc
= container_of(ref
, struct bmc_device
, refcount
);
2270 platform_device_unregister(bmc
->dev
);
2274 static void ipmi_bmc_unregister(ipmi_smi_t intf
)
2276 struct bmc_device
*bmc
= intf
->bmc
;
2278 if (intf
->sysfs_name
) {
2279 sysfs_remove_link(&intf
->si_dev
->kobj
, intf
->sysfs_name
);
2280 kfree(intf
->sysfs_name
);
2281 intf
->sysfs_name
= NULL
;
2283 if (intf
->my_dev_name
) {
2284 sysfs_remove_link(&bmc
->dev
->dev
.kobj
, intf
->my_dev_name
);
2285 kfree(intf
->my_dev_name
);
2286 intf
->my_dev_name
= NULL
;
2289 mutex_lock(&ipmidriver_mutex
);
2290 kref_put(&bmc
->refcount
, cleanup_bmc_device
);
2292 mutex_unlock(&ipmidriver_mutex
);
2295 static int create_files(struct bmc_device
*bmc
)
2299 bmc
->device_id_attr
.attr
.name
= "device_id";
2300 bmc
->device_id_attr
.attr
.mode
= S_IRUGO
;
2301 bmc
->device_id_attr
.show
= device_id_show
;
2302 sysfs_attr_init(&bmc
->device_id_attr
.attr
);
2304 bmc
->provides_dev_sdrs_attr
.attr
.name
= "provides_device_sdrs";
2305 bmc
->provides_dev_sdrs_attr
.attr
.mode
= S_IRUGO
;
2306 bmc
->provides_dev_sdrs_attr
.show
= provides_dev_sdrs_show
;
2307 sysfs_attr_init(&bmc
->provides_dev_sdrs_attr
.attr
);
2309 bmc
->revision_attr
.attr
.name
= "revision";
2310 bmc
->revision_attr
.attr
.mode
= S_IRUGO
;
2311 bmc
->revision_attr
.show
= revision_show
;
2312 sysfs_attr_init(&bmc
->revision_attr
.attr
);
2314 bmc
->firmware_rev_attr
.attr
.name
= "firmware_revision";
2315 bmc
->firmware_rev_attr
.attr
.mode
= S_IRUGO
;
2316 bmc
->firmware_rev_attr
.show
= firmware_rev_show
;
2317 sysfs_attr_init(&bmc
->firmware_rev_attr
.attr
);
2319 bmc
->version_attr
.attr
.name
= "ipmi_version";
2320 bmc
->version_attr
.attr
.mode
= S_IRUGO
;
2321 bmc
->version_attr
.show
= ipmi_version_show
;
2322 sysfs_attr_init(&bmc
->version_attr
.attr
);
2324 bmc
->add_dev_support_attr
.attr
.name
= "additional_device_support";
2325 bmc
->add_dev_support_attr
.attr
.mode
= S_IRUGO
;
2326 bmc
->add_dev_support_attr
.show
= add_dev_support_show
;
2327 sysfs_attr_init(&bmc
->add_dev_support_attr
.attr
);
2329 bmc
->manufacturer_id_attr
.attr
.name
= "manufacturer_id";
2330 bmc
->manufacturer_id_attr
.attr
.mode
= S_IRUGO
;
2331 bmc
->manufacturer_id_attr
.show
= manufacturer_id_show
;
2332 sysfs_attr_init(&bmc
->manufacturer_id_attr
.attr
);
2334 bmc
->product_id_attr
.attr
.name
= "product_id";
2335 bmc
->product_id_attr
.attr
.mode
= S_IRUGO
;
2336 bmc
->product_id_attr
.show
= product_id_show
;
2337 sysfs_attr_init(&bmc
->product_id_attr
.attr
);
2339 bmc
->guid_attr
.attr
.name
= "guid";
2340 bmc
->guid_attr
.attr
.mode
= S_IRUGO
;
2341 bmc
->guid_attr
.show
= guid_show
;
2342 sysfs_attr_init(&bmc
->guid_attr
.attr
);
2344 bmc
->aux_firmware_rev_attr
.attr
.name
= "aux_firmware_revision";
2345 bmc
->aux_firmware_rev_attr
.attr
.mode
= S_IRUGO
;
2346 bmc
->aux_firmware_rev_attr
.show
= aux_firmware_rev_show
;
2347 sysfs_attr_init(&bmc
->aux_firmware_rev_attr
.attr
);
2349 err
= device_create_file(&bmc
->dev
->dev
,
2350 &bmc
->device_id_attr
);
2353 err
= device_create_file(&bmc
->dev
->dev
,
2354 &bmc
->provides_dev_sdrs_attr
);
2357 err
= device_create_file(&bmc
->dev
->dev
,
2358 &bmc
->revision_attr
);
2361 err
= device_create_file(&bmc
->dev
->dev
,
2362 &bmc
->firmware_rev_attr
);
2365 err
= device_create_file(&bmc
->dev
->dev
,
2366 &bmc
->version_attr
);
2369 err
= device_create_file(&bmc
->dev
->dev
,
2370 &bmc
->add_dev_support_attr
);
2373 err
= device_create_file(&bmc
->dev
->dev
,
2374 &bmc
->manufacturer_id_attr
);
2377 err
= device_create_file(&bmc
->dev
->dev
,
2378 &bmc
->product_id_attr
);
2381 if (bmc
->id
.aux_firmware_revision_set
) {
2382 err
= device_create_file(&bmc
->dev
->dev
,
2383 &bmc
->aux_firmware_rev_attr
);
2387 if (bmc
->guid_set
) {
2388 err
= device_create_file(&bmc
->dev
->dev
,
2397 if (bmc
->id
.aux_firmware_revision_set
)
2398 device_remove_file(&bmc
->dev
->dev
,
2399 &bmc
->aux_firmware_rev_attr
);
2401 device_remove_file(&bmc
->dev
->dev
,
2402 &bmc
->product_id_attr
);
2404 device_remove_file(&bmc
->dev
->dev
,
2405 &bmc
->manufacturer_id_attr
);
2407 device_remove_file(&bmc
->dev
->dev
,
2408 &bmc
->add_dev_support_attr
);
2410 device_remove_file(&bmc
->dev
->dev
,
2411 &bmc
->version_attr
);
2413 device_remove_file(&bmc
->dev
->dev
,
2414 &bmc
->firmware_rev_attr
);
2416 device_remove_file(&bmc
->dev
->dev
,
2417 &bmc
->revision_attr
);
2419 device_remove_file(&bmc
->dev
->dev
,
2420 &bmc
->provides_dev_sdrs_attr
);
2422 device_remove_file(&bmc
->dev
->dev
,
2423 &bmc
->device_id_attr
);
2428 static int ipmi_bmc_register(ipmi_smi_t intf
, int ifnum
,
2429 const char *sysfs_name
)
2432 struct bmc_device
*bmc
= intf
->bmc
;
2433 struct bmc_device
*old_bmc
;
2437 mutex_lock(&ipmidriver_mutex
);
2440 * Try to find if there is an bmc_device struct
2441 * representing the interfaced BMC already
2444 old_bmc
= ipmi_find_bmc_guid(&ipmidriver
.driver
, bmc
->guid
);
2446 old_bmc
= ipmi_find_bmc_prod_dev_id(&ipmidriver
.driver
,
2451 * If there is already an bmc_device, free the new one,
2452 * otherwise register the new BMC device
2456 intf
->bmc
= old_bmc
;
2459 kref_get(&bmc
->refcount
);
2460 mutex_unlock(&ipmidriver_mutex
);
2463 "ipmi: interfacing existing BMC (man_id: 0x%6.6x,"
2464 " prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
2465 bmc
->id
.manufacturer_id
,
2470 unsigned char orig_dev_id
= bmc
->id
.device_id
;
2471 int warn_printed
= 0;
2473 snprintf(name
, sizeof(name
),
2474 "ipmi_bmc.%4.4x", bmc
->id
.product_id
);
2476 while (ipmi_find_bmc_prod_dev_id(&ipmidriver
.driver
,
2478 bmc
->id
.device_id
)) {
2479 if (!warn_printed
) {
2480 printk(KERN_WARNING PFX
2481 "This machine has two different BMCs"
2482 " with the same product id and device"
2483 " id. This is an error in the"
2484 " firmware, but incrementing the"
2485 " device id to work around the problem."
2486 " Prod ID = 0x%x, Dev ID = 0x%x\n",
2487 bmc
->id
.product_id
, bmc
->id
.device_id
);
2490 bmc
->id
.device_id
++; /* Wraps at 255 */
2491 if (bmc
->id
.device_id
== orig_dev_id
) {
2493 "Out of device ids!\n");
2498 bmc
->dev
= platform_device_alloc(name
, bmc
->id
.device_id
);
2500 mutex_unlock(&ipmidriver_mutex
);
2503 " Unable to allocate platform device\n");
2506 bmc
->dev
->dev
.driver
= &ipmidriver
.driver
;
2507 dev_set_drvdata(&bmc
->dev
->dev
, bmc
);
2508 kref_init(&bmc
->refcount
);
2510 rv
= platform_device_add(bmc
->dev
);
2511 mutex_unlock(&ipmidriver_mutex
);
2513 platform_device_put(bmc
->dev
);
2517 " Unable to register bmc device: %d\n",
2520 * Don't go to out_err, you can only do that if
2521 * the device is registered already.
2526 rv
= create_files(bmc
);
2528 mutex_lock(&ipmidriver_mutex
);
2529 platform_device_unregister(bmc
->dev
);
2530 mutex_unlock(&ipmidriver_mutex
);
2535 dev_info(intf
->si_dev
, "Found new BMC (man_id: 0x%6.6x, "
2536 "prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
2537 bmc
->id
.manufacturer_id
,
2543 * create symlink from system interface device to bmc device
2546 intf
->sysfs_name
= kstrdup(sysfs_name
, GFP_KERNEL
);
2547 if (!intf
->sysfs_name
) {
2550 "ipmi_msghandler: allocate link to BMC: %d\n",
2555 rv
= sysfs_create_link(&intf
->si_dev
->kobj
,
2556 &bmc
->dev
->dev
.kobj
, intf
->sysfs_name
);
2558 kfree(intf
->sysfs_name
);
2559 intf
->sysfs_name
= NULL
;
2561 "ipmi_msghandler: Unable to create bmc symlink: %d\n",
2566 size
= snprintf(dummy
, 0, "ipmi%d", ifnum
);
2567 intf
->my_dev_name
= kmalloc(size
+1, GFP_KERNEL
);
2568 if (!intf
->my_dev_name
) {
2569 kfree(intf
->sysfs_name
);
2570 intf
->sysfs_name
= NULL
;
2573 "ipmi_msghandler: allocate link from BMC: %d\n",
2577 snprintf(intf
->my_dev_name
, size
+1, "ipmi%d", ifnum
);
2579 rv
= sysfs_create_link(&bmc
->dev
->dev
.kobj
, &intf
->si_dev
->kobj
,
2582 kfree(intf
->sysfs_name
);
2583 intf
->sysfs_name
= NULL
;
2584 kfree(intf
->my_dev_name
);
2585 intf
->my_dev_name
= NULL
;
2588 " Unable to create symlink to bmc: %d\n",
2596 ipmi_bmc_unregister(intf
);
2601 send_guid_cmd(ipmi_smi_t intf
, int chan
)
2603 struct kernel_ipmi_msg msg
;
2604 struct ipmi_system_interface_addr si
;
2606 si
.addr_type
= IPMI_SYSTEM_INTERFACE_ADDR_TYPE
;
2607 si
.channel
= IPMI_BMC_CHANNEL
;
2610 msg
.netfn
= IPMI_NETFN_APP_REQUEST
;
2611 msg
.cmd
= IPMI_GET_DEVICE_GUID_CMD
;
2614 return i_ipmi_request(NULL
,
2616 (struct ipmi_addr
*) &si
,
2623 intf
->channels
[0].address
,
2624 intf
->channels
[0].lun
,
2629 guid_handler(ipmi_smi_t intf
, struct ipmi_recv_msg
*msg
)
2631 if ((msg
->addr
.addr_type
!= IPMI_SYSTEM_INTERFACE_ADDR_TYPE
)
2632 || (msg
->msg
.netfn
!= IPMI_NETFN_APP_RESPONSE
)
2633 || (msg
->msg
.cmd
!= IPMI_GET_DEVICE_GUID_CMD
))
2637 if (msg
->msg
.data
[0] != 0) {
2638 /* Error from getting the GUID, the BMC doesn't have one. */
2639 intf
->bmc
->guid_set
= 0;
2643 if (msg
->msg
.data_len
< 17) {
2644 intf
->bmc
->guid_set
= 0;
2645 printk(KERN_WARNING PFX
2646 "guid_handler: The GUID response from the BMC was too"
2647 " short, it was %d but should have been 17. Assuming"
2648 " GUID is not available.\n",
2653 memcpy(intf
->bmc
->guid
, msg
->msg
.data
, 16);
2654 intf
->bmc
->guid_set
= 1;
2656 wake_up(&intf
->waitq
);
2660 get_guid(ipmi_smi_t intf
)
2664 intf
->bmc
->guid_set
= 0x2;
2665 intf
->null_user_handler
= guid_handler
;
2666 rv
= send_guid_cmd(intf
, 0);
2668 /* Send failed, no GUID available. */
2669 intf
->bmc
->guid_set
= 0;
2670 wait_event(intf
->waitq
, intf
->bmc
->guid_set
!= 2);
2671 intf
->null_user_handler
= NULL
;
2675 send_channel_info_cmd(ipmi_smi_t intf
, int chan
)
2677 struct kernel_ipmi_msg msg
;
2678 unsigned char data
[1];
2679 struct ipmi_system_interface_addr si
;
2681 si
.addr_type
= IPMI_SYSTEM_INTERFACE_ADDR_TYPE
;
2682 si
.channel
= IPMI_BMC_CHANNEL
;
2685 msg
.netfn
= IPMI_NETFN_APP_REQUEST
;
2686 msg
.cmd
= IPMI_GET_CHANNEL_INFO_CMD
;
2690 return i_ipmi_request(NULL
,
2692 (struct ipmi_addr
*) &si
,
2699 intf
->channels
[0].address
,
2700 intf
->channels
[0].lun
,
2705 channel_handler(ipmi_smi_t intf
, struct ipmi_recv_msg
*msg
)
2710 if ((msg
->addr
.addr_type
== IPMI_SYSTEM_INTERFACE_ADDR_TYPE
)
2711 && (msg
->msg
.netfn
== IPMI_NETFN_APP_RESPONSE
)
2712 && (msg
->msg
.cmd
== IPMI_GET_CHANNEL_INFO_CMD
)) {
2713 /* It's the one we want */
2714 if (msg
->msg
.data
[0] != 0) {
2715 /* Got an error from the channel, just go on. */
2717 if (msg
->msg
.data
[0] == IPMI_INVALID_COMMAND_ERR
) {
2719 * If the MC does not support this
2720 * command, that is legal. We just
2721 * assume it has one IPMB at channel
2724 intf
->channels
[0].medium
2725 = IPMI_CHANNEL_MEDIUM_IPMB
;
2726 intf
->channels
[0].protocol
2727 = IPMI_CHANNEL_PROTOCOL_IPMB
;
2730 intf
->curr_channel
= IPMI_MAX_CHANNELS
;
2731 wake_up(&intf
->waitq
);
2736 if (msg
->msg
.data_len
< 4) {
2737 /* Message not big enough, just go on. */
2740 chan
= intf
->curr_channel
;
2741 intf
->channels
[chan
].medium
= msg
->msg
.data
[2] & 0x7f;
2742 intf
->channels
[chan
].protocol
= msg
->msg
.data
[3] & 0x1f;
2745 intf
->curr_channel
++;
2746 if (intf
->curr_channel
>= IPMI_MAX_CHANNELS
)
2747 wake_up(&intf
->waitq
);
2749 rv
= send_channel_info_cmd(intf
, intf
->curr_channel
);
2752 /* Got an error somehow, just give up. */
2753 intf
->curr_channel
= IPMI_MAX_CHANNELS
;
2754 wake_up(&intf
->waitq
);
2756 printk(KERN_WARNING PFX
2757 "Error sending channel information: %d\n",
2765 void ipmi_poll_interface(ipmi_user_t user
)
2767 ipmi_smi_t intf
= user
->intf
;
2769 if (intf
->handlers
->poll
)
2770 intf
->handlers
->poll(intf
->send_info
);
2772 EXPORT_SYMBOL(ipmi_poll_interface
);
2774 int ipmi_register_smi(struct ipmi_smi_handlers
*handlers
,
2776 struct ipmi_device_id
*device_id
,
2777 struct device
*si_dev
,
2778 const char *sysfs_name
,
2779 unsigned char slave_addr
)
2785 struct list_head
*link
;
2788 * Make sure the driver is actually initialized, this handles
2789 * problems with initialization order.
2792 rv
= ipmi_init_msghandler();
2796 * The init code doesn't return an error if it was turned
2797 * off, but it won't initialize. Check that.
2803 intf
= kzalloc(sizeof(*intf
), GFP_KERNEL
);
2807 intf
->ipmi_version_major
= ipmi_version_major(device_id
);
2808 intf
->ipmi_version_minor
= ipmi_version_minor(device_id
);
2810 intf
->bmc
= kzalloc(sizeof(*intf
->bmc
), GFP_KERNEL
);
2815 intf
->intf_num
= -1; /* Mark it invalid for now. */
2816 kref_init(&intf
->refcount
);
2817 intf
->bmc
->id
= *device_id
;
2818 intf
->si_dev
= si_dev
;
2819 for (j
= 0; j
< IPMI_MAX_CHANNELS
; j
++) {
2820 intf
->channels
[j
].address
= IPMI_BMC_SLAVE_ADDR
;
2821 intf
->channels
[j
].lun
= 2;
2823 if (slave_addr
!= 0)
2824 intf
->channels
[0].address
= slave_addr
;
2825 INIT_LIST_HEAD(&intf
->users
);
2826 intf
->handlers
= handlers
;
2827 intf
->send_info
= send_info
;
2828 spin_lock_init(&intf
->seq_lock
);
2829 for (j
= 0; j
< IPMI_IPMB_NUM_SEQ
; j
++) {
2830 intf
->seq_table
[j
].inuse
= 0;
2831 intf
->seq_table
[j
].seqid
= 0;
2834 #ifdef CONFIG_PROC_FS
2835 mutex_init(&intf
->proc_entry_lock
);
2837 spin_lock_init(&intf
->waiting_msgs_lock
);
2838 INIT_LIST_HEAD(&intf
->waiting_msgs
);
2839 spin_lock_init(&intf
->events_lock
);
2840 INIT_LIST_HEAD(&intf
->waiting_events
);
2841 intf
->waiting_events_count
= 0;
2842 mutex_init(&intf
->cmd_rcvrs_mutex
);
2843 spin_lock_init(&intf
->maintenance_mode_lock
);
2844 INIT_LIST_HEAD(&intf
->cmd_rcvrs
);
2845 init_waitqueue_head(&intf
->waitq
);
2846 for (i
= 0; i
< IPMI_NUM_STATS
; i
++)
2847 atomic_set(&intf
->stats
[i
], 0);
2849 intf
->proc_dir
= NULL
;
2851 mutex_lock(&smi_watchers_mutex
);
2852 mutex_lock(&ipmi_interfaces_mutex
);
2853 /* Look for a hole in the numbers. */
2855 link
= &ipmi_interfaces
;
2856 list_for_each_entry_rcu(tintf
, &ipmi_interfaces
, link
) {
2857 if (tintf
->intf_num
!= i
) {
2858 link
= &tintf
->link
;
2863 /* Add the new interface in numeric order. */
2865 list_add_rcu(&intf
->link
, &ipmi_interfaces
);
2867 list_add_tail_rcu(&intf
->link
, link
);
2869 rv
= handlers
->start_processing(send_info
, intf
);
2875 if ((intf
->ipmi_version_major
> 1)
2876 || ((intf
->ipmi_version_major
== 1)
2877 && (intf
->ipmi_version_minor
>= 5))) {
2879 * Start scanning the channels to see what is
2882 intf
->null_user_handler
= channel_handler
;
2883 intf
->curr_channel
= 0;
2884 rv
= send_channel_info_cmd(intf
, 0);
2888 /* Wait for the channel info to be read. */
2889 wait_event(intf
->waitq
,
2890 intf
->curr_channel
>= IPMI_MAX_CHANNELS
);
2891 intf
->null_user_handler
= NULL
;
2893 /* Assume a single IPMB channel at zero. */
2894 intf
->channels
[0].medium
= IPMI_CHANNEL_MEDIUM_IPMB
;
2895 intf
->channels
[0].protocol
= IPMI_CHANNEL_PROTOCOL_IPMB
;
2896 intf
->curr_channel
= IPMI_MAX_CHANNELS
;
2900 rv
= add_proc_entries(intf
, i
);
2902 rv
= ipmi_bmc_register(intf
, i
, sysfs_name
);
2907 remove_proc_entries(intf
);
2908 intf
->handlers
= NULL
;
2909 list_del_rcu(&intf
->link
);
2910 mutex_unlock(&ipmi_interfaces_mutex
);
2911 mutex_unlock(&smi_watchers_mutex
);
2913 kref_put(&intf
->refcount
, intf_free
);
2916 * Keep memory order straight for RCU readers. Make
2917 * sure everything else is committed to memory before
2918 * setting intf_num to mark the interface valid.
2922 mutex_unlock(&ipmi_interfaces_mutex
);
2923 /* After this point the interface is legal to use. */
2924 call_smi_watchers(i
, intf
->si_dev
);
2925 mutex_unlock(&smi_watchers_mutex
);
2930 EXPORT_SYMBOL(ipmi_register_smi
);
2932 static void cleanup_smi_msgs(ipmi_smi_t intf
)
2935 struct seq_table
*ent
;
2937 /* No need for locks, the interface is down. */
2938 for (i
= 0; i
< IPMI_IPMB_NUM_SEQ
; i
++) {
2939 ent
= &(intf
->seq_table
[i
]);
2942 deliver_err_response(ent
->recv_msg
, IPMI_ERR_UNSPECIFIED
);
2946 int ipmi_unregister_smi(ipmi_smi_t intf
)
2948 struct ipmi_smi_watcher
*w
;
2949 int intf_num
= intf
->intf_num
;
2951 ipmi_bmc_unregister(intf
);
2953 mutex_lock(&smi_watchers_mutex
);
2954 mutex_lock(&ipmi_interfaces_mutex
);
2955 intf
->intf_num
= -1;
2956 intf
->handlers
= NULL
;
2957 list_del_rcu(&intf
->link
);
2958 mutex_unlock(&ipmi_interfaces_mutex
);
2961 cleanup_smi_msgs(intf
);
2963 remove_proc_entries(intf
);
2966 * Call all the watcher interfaces to tell them that
2967 * an interface is gone.
2969 list_for_each_entry(w
, &smi_watchers
, link
)
2970 w
->smi_gone(intf_num
);
2971 mutex_unlock(&smi_watchers_mutex
);
2973 kref_put(&intf
->refcount
, intf_free
);
2976 EXPORT_SYMBOL(ipmi_unregister_smi
);
2978 static int handle_ipmb_get_msg_rsp(ipmi_smi_t intf
,
2979 struct ipmi_smi_msg
*msg
)
2981 struct ipmi_ipmb_addr ipmb_addr
;
2982 struct ipmi_recv_msg
*recv_msg
;
2985 * This is 11, not 10, because the response must contain a
2988 if (msg
->rsp_size
< 11) {
2989 /* Message not big enough, just ignore it. */
2990 ipmi_inc_stat(intf
, invalid_ipmb_responses
);
2994 if (msg
->rsp
[2] != 0) {
2995 /* An error getting the response, just ignore it. */
2999 ipmb_addr
.addr_type
= IPMI_IPMB_ADDR_TYPE
;
3000 ipmb_addr
.slave_addr
= msg
->rsp
[6];
3001 ipmb_addr
.channel
= msg
->rsp
[3] & 0x0f;
3002 ipmb_addr
.lun
= msg
->rsp
[7] & 3;
3005 * It's a response from a remote entity. Look up the sequence
3006 * number and handle the response.
3008 if (intf_find_seq(intf
,
3012 (msg
->rsp
[4] >> 2) & (~1),
3013 (struct ipmi_addr
*) &(ipmb_addr
),
3016 * We were unable to find the sequence number,
3017 * so just nuke the message.
3019 ipmi_inc_stat(intf
, unhandled_ipmb_responses
);
3023 memcpy(recv_msg
->msg_data
,
3027 * The other fields matched, so no need to set them, except
3028 * for netfn, which needs to be the response that was
3029 * returned, not the request value.
3031 recv_msg
->msg
.netfn
= msg
->rsp
[4] >> 2;
3032 recv_msg
->msg
.data
= recv_msg
->msg_data
;
3033 recv_msg
->msg
.data_len
= msg
->rsp_size
- 10;
3034 recv_msg
->recv_type
= IPMI_RESPONSE_RECV_TYPE
;
3035 ipmi_inc_stat(intf
, handled_ipmb_responses
);
3036 deliver_response(recv_msg
);
3041 static int handle_ipmb_get_msg_cmd(ipmi_smi_t intf
,
3042 struct ipmi_smi_msg
*msg
)
3044 struct cmd_rcvr
*rcvr
;
3046 unsigned char netfn
;
3049 ipmi_user_t user
= NULL
;
3050 struct ipmi_ipmb_addr
*ipmb_addr
;
3051 struct ipmi_recv_msg
*recv_msg
;
3052 struct ipmi_smi_handlers
*handlers
;
3054 if (msg
->rsp_size
< 10) {
3055 /* Message not big enough, just ignore it. */
3056 ipmi_inc_stat(intf
, invalid_commands
);
3060 if (msg
->rsp
[2] != 0) {
3061 /* An error getting the response, just ignore it. */
3065 netfn
= msg
->rsp
[4] >> 2;
3067 chan
= msg
->rsp
[3] & 0xf;
3070 rcvr
= find_cmd_rcvr(intf
, netfn
, cmd
, chan
);
3073 kref_get(&user
->refcount
);
3079 /* We didn't find a user, deliver an error response. */
3080 ipmi_inc_stat(intf
, unhandled_commands
);
3082 msg
->data
[0] = (IPMI_NETFN_APP_REQUEST
<< 2);
3083 msg
->data
[1] = IPMI_SEND_MSG_CMD
;
3084 msg
->data
[2] = msg
->rsp
[3];
3085 msg
->data
[3] = msg
->rsp
[6];
3086 msg
->data
[4] = ((netfn
+ 1) << 2) | (msg
->rsp
[7] & 0x3);
3087 msg
->data
[5] = ipmb_checksum(&(msg
->data
[3]), 2);
3088 msg
->data
[6] = intf
->channels
[msg
->rsp
[3] & 0xf].address
;
3090 msg
->data
[7] = (msg
->rsp
[7] & 0xfc) | (msg
->rsp
[4] & 0x3);
3091 msg
->data
[8] = msg
->rsp
[8]; /* cmd */
3092 msg
->data
[9] = IPMI_INVALID_CMD_COMPLETION_CODE
;
3093 msg
->data
[10] = ipmb_checksum(&(msg
->data
[6]), 4);
3094 msg
->data_size
= 11;
3099 printk("Invalid command:");
3100 for (m
= 0; m
< msg
->data_size
; m
++)
3101 printk(" %2.2x", msg
->data
[m
]);
3106 handlers
= intf
->handlers
;
3108 handlers
->sender(intf
->send_info
, msg
, 0);
3110 * We used the message, so return the value
3111 * that causes it to not be freed or
3118 /* Deliver the message to the user. */
3119 ipmi_inc_stat(intf
, handled_commands
);
3121 recv_msg
= ipmi_alloc_recv_msg();
3124 * We couldn't allocate memory for the
3125 * message, so requeue it for handling
3129 kref_put(&user
->refcount
, free_user
);
3131 /* Extract the source address from the data. */
3132 ipmb_addr
= (struct ipmi_ipmb_addr
*) &recv_msg
->addr
;
3133 ipmb_addr
->addr_type
= IPMI_IPMB_ADDR_TYPE
;
3134 ipmb_addr
->slave_addr
= msg
->rsp
[6];
3135 ipmb_addr
->lun
= msg
->rsp
[7] & 3;
3136 ipmb_addr
->channel
= msg
->rsp
[3] & 0xf;
3139 * Extract the rest of the message information
3140 * from the IPMB header.
3142 recv_msg
->user
= user
;
3143 recv_msg
->recv_type
= IPMI_CMD_RECV_TYPE
;
3144 recv_msg
->msgid
= msg
->rsp
[7] >> 2;
3145 recv_msg
->msg
.netfn
= msg
->rsp
[4] >> 2;
3146 recv_msg
->msg
.cmd
= msg
->rsp
[8];
3147 recv_msg
->msg
.data
= recv_msg
->msg_data
;
3150 * We chop off 10, not 9 bytes because the checksum
3151 * at the end also needs to be removed.
3153 recv_msg
->msg
.data_len
= msg
->rsp_size
- 10;
3154 memcpy(recv_msg
->msg_data
,
3156 msg
->rsp_size
- 10);
3157 deliver_response(recv_msg
);
3164 static int handle_lan_get_msg_rsp(ipmi_smi_t intf
,
3165 struct ipmi_smi_msg
*msg
)
3167 struct ipmi_lan_addr lan_addr
;
3168 struct ipmi_recv_msg
*recv_msg
;
3172 * This is 13, not 12, because the response must contain a
3175 if (msg
->rsp_size
< 13) {
3176 /* Message not big enough, just ignore it. */
3177 ipmi_inc_stat(intf
, invalid_lan_responses
);
3181 if (msg
->rsp
[2] != 0) {
3182 /* An error getting the response, just ignore it. */
3186 lan_addr
.addr_type
= IPMI_LAN_ADDR_TYPE
;
3187 lan_addr
.session_handle
= msg
->rsp
[4];
3188 lan_addr
.remote_SWID
= msg
->rsp
[8];
3189 lan_addr
.local_SWID
= msg
->rsp
[5];
3190 lan_addr
.channel
= msg
->rsp
[3] & 0x0f;
3191 lan_addr
.privilege
= msg
->rsp
[3] >> 4;
3192 lan_addr
.lun
= msg
->rsp
[9] & 3;
3195 * It's a response from a remote entity. Look up the sequence
3196 * number and handle the response.
3198 if (intf_find_seq(intf
,
3202 (msg
->rsp
[6] >> 2) & (~1),
3203 (struct ipmi_addr
*) &(lan_addr
),
3206 * We were unable to find the sequence number,
3207 * so just nuke the message.
3209 ipmi_inc_stat(intf
, unhandled_lan_responses
);
3213 memcpy(recv_msg
->msg_data
,
3215 msg
->rsp_size
- 11);
3217 * The other fields matched, so no need to set them, except
3218 * for netfn, which needs to be the response that was
3219 * returned, not the request value.
3221 recv_msg
->msg
.netfn
= msg
->rsp
[6] >> 2;
3222 recv_msg
->msg
.data
= recv_msg
->msg_data
;
3223 recv_msg
->msg
.data_len
= msg
->rsp_size
- 12;
3224 recv_msg
->recv_type
= IPMI_RESPONSE_RECV_TYPE
;
3225 ipmi_inc_stat(intf
, handled_lan_responses
);
3226 deliver_response(recv_msg
);
3231 static int handle_lan_get_msg_cmd(ipmi_smi_t intf
,
3232 struct ipmi_smi_msg
*msg
)
3234 struct cmd_rcvr
*rcvr
;
3236 unsigned char netfn
;
3239 ipmi_user_t user
= NULL
;
3240 struct ipmi_lan_addr
*lan_addr
;
3241 struct ipmi_recv_msg
*recv_msg
;
3243 if (msg
->rsp_size
< 12) {
3244 /* Message not big enough, just ignore it. */
3245 ipmi_inc_stat(intf
, invalid_commands
);
3249 if (msg
->rsp
[2] != 0) {
3250 /* An error getting the response, just ignore it. */
3254 netfn
= msg
->rsp
[6] >> 2;
3256 chan
= msg
->rsp
[3] & 0xf;
3259 rcvr
= find_cmd_rcvr(intf
, netfn
, cmd
, chan
);
3262 kref_get(&user
->refcount
);
3268 /* We didn't find a user, just give up. */
3269 ipmi_inc_stat(intf
, unhandled_commands
);
3272 * Don't do anything with these messages, just allow
3277 /* Deliver the message to the user. */
3278 ipmi_inc_stat(intf
, handled_commands
);
3280 recv_msg
= ipmi_alloc_recv_msg();
3283 * We couldn't allocate memory for the
3284 * message, so requeue it for handling later.
3287 kref_put(&user
->refcount
, free_user
);
3289 /* Extract the source address from the data. */
3290 lan_addr
= (struct ipmi_lan_addr
*) &recv_msg
->addr
;
3291 lan_addr
->addr_type
= IPMI_LAN_ADDR_TYPE
;
3292 lan_addr
->session_handle
= msg
->rsp
[4];
3293 lan_addr
->remote_SWID
= msg
->rsp
[8];
3294 lan_addr
->local_SWID
= msg
->rsp
[5];
3295 lan_addr
->lun
= msg
->rsp
[9] & 3;
3296 lan_addr
->channel
= msg
->rsp
[3] & 0xf;
3297 lan_addr
->privilege
= msg
->rsp
[3] >> 4;
3300 * Extract the rest of the message information
3301 * from the IPMB header.
3303 recv_msg
->user
= user
;
3304 recv_msg
->recv_type
= IPMI_CMD_RECV_TYPE
;
3305 recv_msg
->msgid
= msg
->rsp
[9] >> 2;
3306 recv_msg
->msg
.netfn
= msg
->rsp
[6] >> 2;
3307 recv_msg
->msg
.cmd
= msg
->rsp
[10];
3308 recv_msg
->msg
.data
= recv_msg
->msg_data
;
3311 * We chop off 12, not 11 bytes because the checksum
3312 * at the end also needs to be removed.
3314 recv_msg
->msg
.data_len
= msg
->rsp_size
- 12;
3315 memcpy(recv_msg
->msg_data
,
3317 msg
->rsp_size
- 12);
3318 deliver_response(recv_msg
);
3326 * This routine will handle "Get Message" command responses with
3327 * channels that use an OEM Medium. The message format belongs to
3328 * the OEM. See IPMI 2.0 specification, Chapter 6 and
3329 * Chapter 22, sections 22.6 and 22.24 for more details.
3331 static int handle_oem_get_msg_cmd(ipmi_smi_t intf
,
3332 struct ipmi_smi_msg
*msg
)
3334 struct cmd_rcvr
*rcvr
;
3336 unsigned char netfn
;
3339 ipmi_user_t user
= NULL
;
3340 struct ipmi_system_interface_addr
*smi_addr
;
3341 struct ipmi_recv_msg
*recv_msg
;
3344 * We expect the OEM SW to perform error checking
3345 * so we just do some basic sanity checks
3347 if (msg
->rsp_size
< 4) {
3348 /* Message not big enough, just ignore it. */
3349 ipmi_inc_stat(intf
, invalid_commands
);
3353 if (msg
->rsp
[2] != 0) {
3354 /* An error getting the response, just ignore it. */
3359 * This is an OEM Message so the OEM needs to know how
3360 * handle the message. We do no interpretation.
3362 netfn
= msg
->rsp
[0] >> 2;
3364 chan
= msg
->rsp
[3] & 0xf;
3367 rcvr
= find_cmd_rcvr(intf
, netfn
, cmd
, chan
);
3370 kref_get(&user
->refcount
);
3376 /* We didn't find a user, just give up. */
3377 ipmi_inc_stat(intf
, unhandled_commands
);
3380 * Don't do anything with these messages, just allow
3386 /* Deliver the message to the user. */
3387 ipmi_inc_stat(intf
, handled_commands
);
3389 recv_msg
= ipmi_alloc_recv_msg();
3392 * We couldn't allocate memory for the
3393 * message, so requeue it for handling
3397 kref_put(&user
->refcount
, free_user
);
3400 * OEM Messages are expected to be delivered via
3401 * the system interface to SMS software. We might
3402 * need to visit this again depending on OEM
3405 smi_addr
= ((struct ipmi_system_interface_addr
*)
3407 smi_addr
->addr_type
= IPMI_SYSTEM_INTERFACE_ADDR_TYPE
;
3408 smi_addr
->channel
= IPMI_BMC_CHANNEL
;
3409 smi_addr
->lun
= msg
->rsp
[0] & 3;
3411 recv_msg
->user
= user
;
3412 recv_msg
->user_msg_data
= NULL
;
3413 recv_msg
->recv_type
= IPMI_OEM_RECV_TYPE
;
3414 recv_msg
->msg
.netfn
= msg
->rsp
[0] >> 2;
3415 recv_msg
->msg
.cmd
= msg
->rsp
[1];
3416 recv_msg
->msg
.data
= recv_msg
->msg_data
;
3419 * The message starts at byte 4 which follows the
3420 * the Channel Byte in the "GET MESSAGE" command
3422 recv_msg
->msg
.data_len
= msg
->rsp_size
- 4;
3423 memcpy(recv_msg
->msg_data
,
3426 deliver_response(recv_msg
);
3433 static void copy_event_into_recv_msg(struct ipmi_recv_msg
*recv_msg
,
3434 struct ipmi_smi_msg
*msg
)
3436 struct ipmi_system_interface_addr
*smi_addr
;
3438 recv_msg
->msgid
= 0;
3439 smi_addr
= (struct ipmi_system_interface_addr
*) &(recv_msg
->addr
);
3440 smi_addr
->addr_type
= IPMI_SYSTEM_INTERFACE_ADDR_TYPE
;
3441 smi_addr
->channel
= IPMI_BMC_CHANNEL
;
3442 smi_addr
->lun
= msg
->rsp
[0] & 3;
3443 recv_msg
->recv_type
= IPMI_ASYNC_EVENT_RECV_TYPE
;
3444 recv_msg
->msg
.netfn
= msg
->rsp
[0] >> 2;
3445 recv_msg
->msg
.cmd
= msg
->rsp
[1];
3446 memcpy(recv_msg
->msg_data
, &(msg
->rsp
[3]), msg
->rsp_size
- 3);
3447 recv_msg
->msg
.data
= recv_msg
->msg_data
;
3448 recv_msg
->msg
.data_len
= msg
->rsp_size
- 3;
3451 static int handle_read_event_rsp(ipmi_smi_t intf
,
3452 struct ipmi_smi_msg
*msg
)
3454 struct ipmi_recv_msg
*recv_msg
, *recv_msg2
;
3455 struct list_head msgs
;
3458 int deliver_count
= 0;
3459 unsigned long flags
;
3461 if (msg
->rsp_size
< 19) {
3462 /* Message is too small to be an IPMB event. */
3463 ipmi_inc_stat(intf
, invalid_events
);
3467 if (msg
->rsp
[2] != 0) {
3468 /* An error getting the event, just ignore it. */
3472 INIT_LIST_HEAD(&msgs
);
3474 spin_lock_irqsave(&intf
->events_lock
, flags
);
3476 ipmi_inc_stat(intf
, events
);
3479 * Allocate and fill in one message for every user that is
3483 list_for_each_entry_rcu(user
, &intf
->users
, link
) {
3484 if (!user
->gets_events
)
3487 recv_msg
= ipmi_alloc_recv_msg();
3490 list_for_each_entry_safe(recv_msg
, recv_msg2
, &msgs
,
3492 list_del(&recv_msg
->link
);
3493 ipmi_free_recv_msg(recv_msg
);
3496 * We couldn't allocate memory for the
3497 * message, so requeue it for handling
3506 copy_event_into_recv_msg(recv_msg
, msg
);
3507 recv_msg
->user
= user
;
3508 kref_get(&user
->refcount
);
3509 list_add_tail(&(recv_msg
->link
), &msgs
);
3513 if (deliver_count
) {
3514 /* Now deliver all the messages. */
3515 list_for_each_entry_safe(recv_msg
, recv_msg2
, &msgs
, link
) {
3516 list_del(&recv_msg
->link
);
3517 deliver_response(recv_msg
);
3519 } else if (intf
->waiting_events_count
< MAX_EVENTS_IN_QUEUE
) {
3521 * No one to receive the message, put it in queue if there's
3522 * not already too many things in the queue.
3524 recv_msg
= ipmi_alloc_recv_msg();
3527 * We couldn't allocate memory for the
3528 * message, so requeue it for handling
3535 copy_event_into_recv_msg(recv_msg
, msg
);
3536 list_add_tail(&(recv_msg
->link
), &(intf
->waiting_events
));
3537 intf
->waiting_events_count
++;
3538 } else if (!intf
->event_msg_printed
) {
3540 * There's too many things in the queue, discard this
3543 printk(KERN_WARNING PFX
"Event queue full, discarding"
3544 " incoming events\n");
3545 intf
->event_msg_printed
= 1;
3549 spin_unlock_irqrestore(&(intf
->events_lock
), flags
);
3554 static int handle_bmc_rsp(ipmi_smi_t intf
,
3555 struct ipmi_smi_msg
*msg
)
3557 struct ipmi_recv_msg
*recv_msg
;
3558 struct ipmi_user
*user
;
3560 recv_msg
= (struct ipmi_recv_msg
*) msg
->user_data
;
3561 if (recv_msg
== NULL
) {
3563 "IPMI message received with no owner. This\n"
3564 "could be because of a malformed message, or\n"
3565 "because of a hardware error. Contact your\n"
3566 "hardware vender for assistance\n");
3570 user
= recv_msg
->user
;
3571 /* Make sure the user still exists. */
3572 if (user
&& !user
->valid
) {
3573 /* The user for the message went away, so give up. */
3574 ipmi_inc_stat(intf
, unhandled_local_responses
);
3575 ipmi_free_recv_msg(recv_msg
);
3577 struct ipmi_system_interface_addr
*smi_addr
;
3579 ipmi_inc_stat(intf
, handled_local_responses
);
3580 recv_msg
->recv_type
= IPMI_RESPONSE_RECV_TYPE
;
3581 recv_msg
->msgid
= msg
->msgid
;
3582 smi_addr
= ((struct ipmi_system_interface_addr
*)
3584 smi_addr
->addr_type
= IPMI_SYSTEM_INTERFACE_ADDR_TYPE
;
3585 smi_addr
->channel
= IPMI_BMC_CHANNEL
;
3586 smi_addr
->lun
= msg
->rsp
[0] & 3;
3587 recv_msg
->msg
.netfn
= msg
->rsp
[0] >> 2;
3588 recv_msg
->msg
.cmd
= msg
->rsp
[1];
3589 memcpy(recv_msg
->msg_data
,
3592 recv_msg
->msg
.data
= recv_msg
->msg_data
;
3593 recv_msg
->msg
.data_len
= msg
->rsp_size
- 2;
3594 deliver_response(recv_msg
);
3601 * Handle a new message. Return 1 if the message should be requeued,
3602 * 0 if the message should be freed, or -1 if the message should not
3603 * be freed or requeued.
3605 static int handle_new_recv_msg(ipmi_smi_t intf
,
3606 struct ipmi_smi_msg
*msg
)
3614 for (m
= 0; m
< msg
->rsp_size
; m
++)
3615 printk(" %2.2x", msg
->rsp
[m
]);
3618 if (msg
->rsp_size
< 2) {
3619 /* Message is too small to be correct. */
3620 printk(KERN_WARNING PFX
"BMC returned to small a message"
3621 " for netfn %x cmd %x, got %d bytes\n",
3622 (msg
->data
[0] >> 2) | 1, msg
->data
[1], msg
->rsp_size
);
3624 /* Generate an error response for the message. */
3625 msg
->rsp
[0] = msg
->data
[0] | (1 << 2);
3626 msg
->rsp
[1] = msg
->data
[1];
3627 msg
->rsp
[2] = IPMI_ERR_UNSPECIFIED
;
3629 } else if (((msg
->rsp
[0] >> 2) != ((msg
->data
[0] >> 2) | 1))
3630 || (msg
->rsp
[1] != msg
->data
[1])) {
3632 * The NetFN and Command in the response is not even
3633 * marginally correct.
3635 printk(KERN_WARNING PFX
"BMC returned incorrect response,"
3636 " expected netfn %x cmd %x, got netfn %x cmd %x\n",
3637 (msg
->data
[0] >> 2) | 1, msg
->data
[1],
3638 msg
->rsp
[0] >> 2, msg
->rsp
[1]);
3640 /* Generate an error response for the message. */
3641 msg
->rsp
[0] = msg
->data
[0] | (1 << 2);
3642 msg
->rsp
[1] = msg
->data
[1];
3643 msg
->rsp
[2] = IPMI_ERR_UNSPECIFIED
;
3647 if ((msg
->rsp
[0] == ((IPMI_NETFN_APP_REQUEST
|1) << 2))
3648 && (msg
->rsp
[1] == IPMI_SEND_MSG_CMD
)
3649 && (msg
->user_data
!= NULL
)) {
3651 * It's a response to a response we sent. For this we
3652 * deliver a send message response to the user.
3654 struct ipmi_recv_msg
*recv_msg
= msg
->user_data
;
3657 if (msg
->rsp_size
< 2)
3658 /* Message is too small to be correct. */
3661 chan
= msg
->data
[2] & 0x0f;
3662 if (chan
>= IPMI_MAX_CHANNELS
)
3663 /* Invalid channel number */
3669 /* Make sure the user still exists. */
3670 if (!recv_msg
->user
|| !recv_msg
->user
->valid
)
3673 recv_msg
->recv_type
= IPMI_RESPONSE_RESPONSE_TYPE
;
3674 recv_msg
->msg
.data
= recv_msg
->msg_data
;
3675 recv_msg
->msg
.data_len
= 1;
3676 recv_msg
->msg_data
[0] = msg
->rsp
[2];
3677 deliver_response(recv_msg
);
3678 } else if ((msg
->rsp
[0] == ((IPMI_NETFN_APP_REQUEST
|1) << 2))
3679 && (msg
->rsp
[1] == IPMI_GET_MSG_CMD
)) {
3680 /* It's from the receive queue. */
3681 chan
= msg
->rsp
[3] & 0xf;
3682 if (chan
>= IPMI_MAX_CHANNELS
) {
3683 /* Invalid channel number */
3689 * We need to make sure the channels have been initialized.
3690 * The channel_handler routine will set the "curr_channel"
3691 * equal to or greater than IPMI_MAX_CHANNELS when all the
3692 * channels for this interface have been initialized.
3694 if (intf
->curr_channel
< IPMI_MAX_CHANNELS
) {
3695 requeue
= 0; /* Throw the message away */
3699 switch (intf
->channels
[chan
].medium
) {
3700 case IPMI_CHANNEL_MEDIUM_IPMB
:
3701 if (msg
->rsp
[4] & 0x04) {
3703 * It's a response, so find the
3704 * requesting message and send it up.
3706 requeue
= handle_ipmb_get_msg_rsp(intf
, msg
);
3709 * It's a command to the SMS from some other
3710 * entity. Handle that.
3712 requeue
= handle_ipmb_get_msg_cmd(intf
, msg
);
3716 case IPMI_CHANNEL_MEDIUM_8023LAN
:
3717 case IPMI_CHANNEL_MEDIUM_ASYNC
:
3718 if (msg
->rsp
[6] & 0x04) {
3720 * It's a response, so find the
3721 * requesting message and send it up.
3723 requeue
= handle_lan_get_msg_rsp(intf
, msg
);
3726 * It's a command to the SMS from some other
3727 * entity. Handle that.
3729 requeue
= handle_lan_get_msg_cmd(intf
, msg
);
3734 /* Check for OEM Channels. Clients had better
3735 register for these commands. */
3736 if ((intf
->channels
[chan
].medium
3737 >= IPMI_CHANNEL_MEDIUM_OEM_MIN
)
3738 && (intf
->channels
[chan
].medium
3739 <= IPMI_CHANNEL_MEDIUM_OEM_MAX
)) {
3740 requeue
= handle_oem_get_msg_cmd(intf
, msg
);
3743 * We don't handle the channel type, so just
3750 } else if ((msg
->rsp
[0] == ((IPMI_NETFN_APP_REQUEST
|1) << 2))
3751 && (msg
->rsp
[1] == IPMI_READ_EVENT_MSG_BUFFER_CMD
)) {
3752 /* It's an asyncronous event. */
3753 requeue
= handle_read_event_rsp(intf
, msg
);
3755 /* It's a response from the local BMC. */
3756 requeue
= handle_bmc_rsp(intf
, msg
);
3763 /* Handle a new message from the lower layer. */
3764 void ipmi_smi_msg_received(ipmi_smi_t intf
,
3765 struct ipmi_smi_msg
*msg
)
3767 unsigned long flags
= 0; /* keep us warning-free. */
3769 int run_to_completion
;
3772 if ((msg
->data_size
>= 2)
3773 && (msg
->data
[0] == (IPMI_NETFN_APP_REQUEST
<< 2))
3774 && (msg
->data
[1] == IPMI_SEND_MSG_CMD
)
3775 && (msg
->user_data
== NULL
)) {
3777 * This is the local response to a command send, start
3778 * the timer for these. The user_data will not be
3779 * NULL if this is a response send, and we will let
3780 * response sends just go through.
3784 * Check for errors, if we get certain errors (ones
3785 * that mean basically we can try again later), we
3786 * ignore them and start the timer. Otherwise we
3787 * report the error immediately.
3789 if ((msg
->rsp_size
>= 3) && (msg
->rsp
[2] != 0)
3790 && (msg
->rsp
[2] != IPMI_NODE_BUSY_ERR
)
3791 && (msg
->rsp
[2] != IPMI_LOST_ARBITRATION_ERR
)
3792 && (msg
->rsp
[2] != IPMI_BUS_ERR
)
3793 && (msg
->rsp
[2] != IPMI_NAK_ON_WRITE_ERR
)) {
3794 int chan
= msg
->rsp
[3] & 0xf;
3796 /* Got an error sending the message, handle it. */
3797 if (chan
>= IPMI_MAX_CHANNELS
)
3798 ; /* This shouldn't happen */
3799 else if ((intf
->channels
[chan
].medium
3800 == IPMI_CHANNEL_MEDIUM_8023LAN
)
3801 || (intf
->channels
[chan
].medium
3802 == IPMI_CHANNEL_MEDIUM_ASYNC
))
3803 ipmi_inc_stat(intf
, sent_lan_command_errs
);
3805 ipmi_inc_stat(intf
, sent_ipmb_command_errs
);
3806 intf_err_seq(intf
, msg
->msgid
, msg
->rsp
[2]);
3808 /* The message was sent, start the timer. */
3809 intf_start_seq_timer(intf
, msg
->msgid
);
3811 ipmi_free_smi_msg(msg
);
3816 * To preserve message order, if the list is not empty, we
3817 * tack this message onto the end of the list.
3819 run_to_completion
= intf
->run_to_completion
;
3820 if (!run_to_completion
)
3821 spin_lock_irqsave(&intf
->waiting_msgs_lock
, flags
);
3822 if (!list_empty(&intf
->waiting_msgs
)) {
3823 list_add_tail(&msg
->link
, &intf
->waiting_msgs
);
3824 if (!run_to_completion
)
3825 spin_unlock_irqrestore(&intf
->waiting_msgs_lock
, flags
);
3828 if (!run_to_completion
)
3829 spin_unlock_irqrestore(&intf
->waiting_msgs_lock
, flags
);
3831 rv
= handle_new_recv_msg(intf
, msg
);
3834 * Could not handle the message now, just add it to a
3835 * list to handle later.
3837 run_to_completion
= intf
->run_to_completion
;
3838 if (!run_to_completion
)
3839 spin_lock_irqsave(&intf
->waiting_msgs_lock
, flags
);
3840 list_add_tail(&msg
->link
, &intf
->waiting_msgs
);
3841 if (!run_to_completion
)
3842 spin_unlock_irqrestore(&intf
->waiting_msgs_lock
, flags
);
3843 } else if (rv
== 0) {
3844 ipmi_free_smi_msg(msg
);
3850 EXPORT_SYMBOL(ipmi_smi_msg_received
);
3852 void ipmi_smi_watchdog_pretimeout(ipmi_smi_t intf
)
3857 list_for_each_entry_rcu(user
, &intf
->users
, link
) {
3858 if (!user
->handler
->ipmi_watchdog_pretimeout
)
3861 user
->handler
->ipmi_watchdog_pretimeout(user
->handler_data
);
3865 EXPORT_SYMBOL(ipmi_smi_watchdog_pretimeout
);
3867 static struct ipmi_smi_msg
*
3868 smi_from_recv_msg(ipmi_smi_t intf
, struct ipmi_recv_msg
*recv_msg
,
3869 unsigned char seq
, long seqid
)
3871 struct ipmi_smi_msg
*smi_msg
= ipmi_alloc_smi_msg();
3874 * If we can't allocate the message, then just return, we
3875 * get 4 retries, so this should be ok.
3879 memcpy(smi_msg
->data
, recv_msg
->msg
.data
, recv_msg
->msg
.data_len
);
3880 smi_msg
->data_size
= recv_msg
->msg
.data_len
;
3881 smi_msg
->msgid
= STORE_SEQ_IN_MSGID(seq
, seqid
);
3887 for (m
= 0; m
< smi_msg
->data_size
; m
++)
3888 printk(" %2.2x", smi_msg
->data
[m
]);
3895 static void check_msg_timeout(ipmi_smi_t intf
, struct seq_table
*ent
,
3896 struct list_head
*timeouts
, long timeout_period
,
3897 int slot
, unsigned long *flags
)
3899 struct ipmi_recv_msg
*msg
;
3900 struct ipmi_smi_handlers
*handlers
;
3902 if (intf
->intf_num
== -1)
3908 ent
->timeout
-= timeout_period
;
3909 if (ent
->timeout
> 0)
3912 if (ent
->retries_left
== 0) {
3913 /* The message has used all its retries. */
3915 msg
= ent
->recv_msg
;
3916 list_add_tail(&msg
->link
, timeouts
);
3918 ipmi_inc_stat(intf
, timed_out_ipmb_broadcasts
);
3919 else if (is_lan_addr(&ent
->recv_msg
->addr
))
3920 ipmi_inc_stat(intf
, timed_out_lan_commands
);
3922 ipmi_inc_stat(intf
, timed_out_ipmb_commands
);
3924 struct ipmi_smi_msg
*smi_msg
;
3925 /* More retries, send again. */
3928 * Start with the max timer, set to normal timer after
3929 * the message is sent.
3931 ent
->timeout
= MAX_MSG_TIMEOUT
;
3932 ent
->retries_left
--;
3933 smi_msg
= smi_from_recv_msg(intf
, ent
->recv_msg
, slot
,
3936 if (is_lan_addr(&ent
->recv_msg
->addr
))
3938 dropped_rexmit_lan_commands
);
3941 dropped_rexmit_ipmb_commands
);
3945 spin_unlock_irqrestore(&intf
->seq_lock
, *flags
);
3948 * Send the new message. We send with a zero
3949 * priority. It timed out, I doubt time is that
3950 * critical now, and high priority messages are really
3951 * only for messages to the local MC, which don't get
3954 handlers
= intf
->handlers
;
3956 if (is_lan_addr(&ent
->recv_msg
->addr
))
3958 retransmitted_lan_commands
);
3961 retransmitted_ipmb_commands
);
3963 intf
->handlers
->sender(intf
->send_info
,
3966 ipmi_free_smi_msg(smi_msg
);
3968 spin_lock_irqsave(&intf
->seq_lock
, *flags
);
3972 static void ipmi_timeout_handler(long timeout_period
)
3975 struct list_head timeouts
;
3976 struct ipmi_recv_msg
*msg
, *msg2
;
3977 struct ipmi_smi_msg
*smi_msg
, *smi_msg2
;
3978 unsigned long flags
;
3982 list_for_each_entry_rcu(intf
, &ipmi_interfaces
, link
) {
3983 /* See if any waiting messages need to be processed. */
3984 spin_lock_irqsave(&intf
->waiting_msgs_lock
, flags
);
3985 list_for_each_entry_safe(smi_msg
, smi_msg2
,
3986 &intf
->waiting_msgs
, link
) {
3987 if (!handle_new_recv_msg(intf
, smi_msg
)) {
3988 list_del(&smi_msg
->link
);
3989 ipmi_free_smi_msg(smi_msg
);
3992 * To preserve message order, quit if we
3993 * can't handle a message.
3998 spin_unlock_irqrestore(&intf
->waiting_msgs_lock
, flags
);
4001 * Go through the seq table and find any messages that
4002 * have timed out, putting them in the timeouts
4005 INIT_LIST_HEAD(&timeouts
);
4006 spin_lock_irqsave(&intf
->seq_lock
, flags
);
4007 for (i
= 0; i
< IPMI_IPMB_NUM_SEQ
; i
++)
4008 check_msg_timeout(intf
, &(intf
->seq_table
[i
]),
4009 &timeouts
, timeout_period
, i
,
4011 spin_unlock_irqrestore(&intf
->seq_lock
, flags
);
4013 list_for_each_entry_safe(msg
, msg2
, &timeouts
, link
)
4014 deliver_err_response(msg
, IPMI_TIMEOUT_COMPLETION_CODE
);
4017 * Maintenance mode handling. Check the timeout
4018 * optimistically before we claim the lock. It may
4019 * mean a timeout gets missed occasionally, but that
4020 * only means the timeout gets extended by one period
4021 * in that case. No big deal, and it avoids the lock
4024 if (intf
->auto_maintenance_timeout
> 0) {
4025 spin_lock_irqsave(&intf
->maintenance_mode_lock
, flags
);
4026 if (intf
->auto_maintenance_timeout
> 0) {
4027 intf
->auto_maintenance_timeout
4029 if (!intf
->maintenance_mode
4030 && (intf
->auto_maintenance_timeout
<= 0)) {
4031 intf
->maintenance_mode_enable
= 0;
4032 maintenance_mode_update(intf
);
4035 spin_unlock_irqrestore(&intf
->maintenance_mode_lock
,
4042 static void ipmi_request_event(void)
4045 struct ipmi_smi_handlers
*handlers
;
4049 * Called from the timer, no need to check if handlers is
4052 list_for_each_entry_rcu(intf
, &ipmi_interfaces
, link
) {
4053 /* No event requests when in maintenance mode. */
4054 if (intf
->maintenance_mode_enable
)
4057 handlers
= intf
->handlers
;
4059 handlers
->request_events(intf
->send_info
);
4064 static struct timer_list ipmi_timer
;
4066 /* Call every ~1000 ms. */
4067 #define IPMI_TIMEOUT_TIME 1000
4069 /* How many jiffies does it take to get to the timeout time. */
4070 #define IPMI_TIMEOUT_JIFFIES ((IPMI_TIMEOUT_TIME * HZ) / 1000)
4073 * Request events from the queue every second (this is the number of
4074 * IPMI_TIMEOUT_TIMES between event requests). Hopefully, in the
4075 * future, IPMI will add a way to know immediately if an event is in
4076 * the queue and this silliness can go away.
4078 #define IPMI_REQUEST_EV_TIME (1000 / (IPMI_TIMEOUT_TIME))
4080 static atomic_t stop_operation
;
4081 static unsigned int ticks_to_req_ev
= IPMI_REQUEST_EV_TIME
;
4083 static void ipmi_timeout(unsigned long data
)
4085 if (atomic_read(&stop_operation
))
4089 if (ticks_to_req_ev
== 0) {
4090 ipmi_request_event();
4091 ticks_to_req_ev
= IPMI_REQUEST_EV_TIME
;
4094 ipmi_timeout_handler(IPMI_TIMEOUT_TIME
);
4096 mod_timer(&ipmi_timer
, jiffies
+ IPMI_TIMEOUT_JIFFIES
);
4100 static atomic_t smi_msg_inuse_count
= ATOMIC_INIT(0);
4101 static atomic_t recv_msg_inuse_count
= ATOMIC_INIT(0);
4103 /* FIXME - convert these to slabs. */
4104 static void free_smi_msg(struct ipmi_smi_msg
*msg
)
4106 atomic_dec(&smi_msg_inuse_count
);
4110 struct ipmi_smi_msg
*ipmi_alloc_smi_msg(void)
4112 struct ipmi_smi_msg
*rv
;
4113 rv
= kmalloc(sizeof(struct ipmi_smi_msg
), GFP_ATOMIC
);
4115 rv
->done
= free_smi_msg
;
4116 rv
->user_data
= NULL
;
4117 atomic_inc(&smi_msg_inuse_count
);
4121 EXPORT_SYMBOL(ipmi_alloc_smi_msg
);
4123 static void free_recv_msg(struct ipmi_recv_msg
*msg
)
4125 atomic_dec(&recv_msg_inuse_count
);
4129 static struct ipmi_recv_msg
*ipmi_alloc_recv_msg(void)
4131 struct ipmi_recv_msg
*rv
;
4133 rv
= kmalloc(sizeof(struct ipmi_recv_msg
), GFP_ATOMIC
);
4136 rv
->done
= free_recv_msg
;
4137 atomic_inc(&recv_msg_inuse_count
);
4142 void ipmi_free_recv_msg(struct ipmi_recv_msg
*msg
)
4145 kref_put(&msg
->user
->refcount
, free_user
);
4148 EXPORT_SYMBOL(ipmi_free_recv_msg
);
4150 #ifdef CONFIG_IPMI_PANIC_EVENT
4152 static void dummy_smi_done_handler(struct ipmi_smi_msg
*msg
)
4156 static void dummy_recv_done_handler(struct ipmi_recv_msg
*msg
)
4160 #ifdef CONFIG_IPMI_PANIC_STRING
4161 static void event_receiver_fetcher(ipmi_smi_t intf
, struct ipmi_recv_msg
*msg
)
4163 if ((msg
->addr
.addr_type
== IPMI_SYSTEM_INTERFACE_ADDR_TYPE
)
4164 && (msg
->msg
.netfn
== IPMI_NETFN_SENSOR_EVENT_RESPONSE
)
4165 && (msg
->msg
.cmd
== IPMI_GET_EVENT_RECEIVER_CMD
)
4166 && (msg
->msg
.data
[0] == IPMI_CC_NO_ERROR
)) {
4167 /* A get event receiver command, save it. */
4168 intf
->event_receiver
= msg
->msg
.data
[1];
4169 intf
->event_receiver_lun
= msg
->msg
.data
[2] & 0x3;
4173 static void device_id_fetcher(ipmi_smi_t intf
, struct ipmi_recv_msg
*msg
)
4175 if ((msg
->addr
.addr_type
== IPMI_SYSTEM_INTERFACE_ADDR_TYPE
)
4176 && (msg
->msg
.netfn
== IPMI_NETFN_APP_RESPONSE
)
4177 && (msg
->msg
.cmd
== IPMI_GET_DEVICE_ID_CMD
)
4178 && (msg
->msg
.data
[0] == IPMI_CC_NO_ERROR
)) {
4180 * A get device id command, save if we are an event
4181 * receiver or generator.
4183 intf
->local_sel_device
= (msg
->msg
.data
[6] >> 2) & 1;
4184 intf
->local_event_generator
= (msg
->msg
.data
[6] >> 5) & 1;
4189 static void send_panic_events(char *str
)
4191 struct kernel_ipmi_msg msg
;
4193 unsigned char data
[16];
4194 struct ipmi_system_interface_addr
*si
;
4195 struct ipmi_addr addr
;
4196 struct ipmi_smi_msg smi_msg
;
4197 struct ipmi_recv_msg recv_msg
;
4199 si
= (struct ipmi_system_interface_addr
*) &addr
;
4200 si
->addr_type
= IPMI_SYSTEM_INTERFACE_ADDR_TYPE
;
4201 si
->channel
= IPMI_BMC_CHANNEL
;
4204 /* Fill in an event telling that we have failed. */
4205 msg
.netfn
= 0x04; /* Sensor or Event. */
4206 msg
.cmd
= 2; /* Platform event command. */
4209 data
[0] = 0x41; /* Kernel generator ID, IPMI table 5-4 */
4210 data
[1] = 0x03; /* This is for IPMI 1.0. */
4211 data
[2] = 0x20; /* OS Critical Stop, IPMI table 36-3 */
4212 data
[4] = 0x6f; /* Sensor specific, IPMI table 36-1 */
4213 data
[5] = 0xa1; /* Runtime stop OEM bytes 2 & 3. */
4216 * Put a few breadcrumbs in. Hopefully later we can add more things
4217 * to make the panic events more useful.
4225 smi_msg
.done
= dummy_smi_done_handler
;
4226 recv_msg
.done
= dummy_recv_done_handler
;
4228 /* For every registered interface, send the event. */
4229 list_for_each_entry_rcu(intf
, &ipmi_interfaces
, link
) {
4230 if (!intf
->handlers
)
4231 /* Interface is not ready. */
4234 intf
->run_to_completion
= 1;
4235 /* Send the event announcing the panic. */
4236 intf
->handlers
->set_run_to_completion(intf
->send_info
, 1);
4237 i_ipmi_request(NULL
,
4246 intf
->channels
[0].address
,
4247 intf
->channels
[0].lun
,
4248 0, 1); /* Don't retry, and don't wait. */
4251 #ifdef CONFIG_IPMI_PANIC_STRING
4253 * On every interface, dump a bunch of OEM event holding the
4259 /* For every registered interface, send the event. */
4260 list_for_each_entry_rcu(intf
, &ipmi_interfaces
, link
) {
4262 struct ipmi_ipmb_addr
*ipmb
;
4265 if (intf
->intf_num
== -1)
4266 /* Interface was not ready yet. */
4270 * intf_num is used as an marker to tell if the
4271 * interface is valid. Thus we need a read barrier to
4272 * make sure data fetched before checking intf_num
4278 * First job here is to figure out where to send the
4279 * OEM events. There's no way in IPMI to send OEM
4280 * events using an event send command, so we have to
4281 * find the SEL to put them in and stick them in
4285 /* Get capabilities from the get device id. */
4286 intf
->local_sel_device
= 0;
4287 intf
->local_event_generator
= 0;
4288 intf
->event_receiver
= 0;
4290 /* Request the device info from the local MC. */
4291 msg
.netfn
= IPMI_NETFN_APP_REQUEST
;
4292 msg
.cmd
= IPMI_GET_DEVICE_ID_CMD
;
4295 intf
->null_user_handler
= device_id_fetcher
;
4296 i_ipmi_request(NULL
,
4305 intf
->channels
[0].address
,
4306 intf
->channels
[0].lun
,
4307 0, 1); /* Don't retry, and don't wait. */
4309 if (intf
->local_event_generator
) {
4310 /* Request the event receiver from the local MC. */
4311 msg
.netfn
= IPMI_NETFN_SENSOR_EVENT_REQUEST
;
4312 msg
.cmd
= IPMI_GET_EVENT_RECEIVER_CMD
;
4315 intf
->null_user_handler
= event_receiver_fetcher
;
4316 i_ipmi_request(NULL
,
4325 intf
->channels
[0].address
,
4326 intf
->channels
[0].lun
,
4327 0, 1); /* no retry, and no wait. */
4329 intf
->null_user_handler
= NULL
;
4332 * Validate the event receiver. The low bit must not
4333 * be 1 (it must be a valid IPMB address), it cannot
4334 * be zero, and it must not be my address.
4336 if (((intf
->event_receiver
& 1) == 0)
4337 && (intf
->event_receiver
!= 0)
4338 && (intf
->event_receiver
!= intf
->channels
[0].address
)) {
4340 * The event receiver is valid, send an IPMB
4343 ipmb
= (struct ipmi_ipmb_addr
*) &addr
;
4344 ipmb
->addr_type
= IPMI_IPMB_ADDR_TYPE
;
4345 ipmb
->channel
= 0; /* FIXME - is this right? */
4346 ipmb
->lun
= intf
->event_receiver_lun
;
4347 ipmb
->slave_addr
= intf
->event_receiver
;
4348 } else if (intf
->local_sel_device
) {
4350 * The event receiver was not valid (or was
4351 * me), but I am an SEL device, just dump it
4354 si
= (struct ipmi_system_interface_addr
*) &addr
;
4355 si
->addr_type
= IPMI_SYSTEM_INTERFACE_ADDR_TYPE
;
4356 si
->channel
= IPMI_BMC_CHANNEL
;
4359 continue; /* No where to send the event. */
4361 msg
.netfn
= IPMI_NETFN_STORAGE_REQUEST
; /* Storage. */
4362 msg
.cmd
= IPMI_ADD_SEL_ENTRY_CMD
;
4368 int size
= strlen(p
);
4374 data
[2] = 0xf0; /* OEM event without timestamp. */
4375 data
[3] = intf
->channels
[0].address
;
4376 data
[4] = j
++; /* sequence # */
4378 * Always give 11 bytes, so strncpy will fill
4379 * it with zeroes for me.
4381 strncpy(data
+5, p
, 11);
4384 i_ipmi_request(NULL
,
4393 intf
->channels
[0].address
,
4394 intf
->channels
[0].lun
,
4395 0, 1); /* no retry, and no wait. */
4398 #endif /* CONFIG_IPMI_PANIC_STRING */
4400 #endif /* CONFIG_IPMI_PANIC_EVENT */
4402 static int has_panicked
;
4404 static int panic_event(struct notifier_block
*this,
4405 unsigned long event
,
4414 /* For every registered interface, set it to run to completion. */
4415 list_for_each_entry_rcu(intf
, &ipmi_interfaces
, link
) {
4416 if (!intf
->handlers
)
4417 /* Interface is not ready. */
4420 intf
->run_to_completion
= 1;
4421 intf
->handlers
->set_run_to_completion(intf
->send_info
, 1);
4424 #ifdef CONFIG_IPMI_PANIC_EVENT
4425 send_panic_events(ptr
);
4431 static struct notifier_block panic_block
= {
4432 .notifier_call
= panic_event
,
4434 .priority
= 200 /* priority: INT_MAX >= x >= 0 */
4437 static int ipmi_init_msghandler(void)
4444 rv
= driver_register(&ipmidriver
.driver
);
4446 printk(KERN_ERR PFX
"Could not register IPMI driver\n");
4450 printk(KERN_INFO
"ipmi message handler version "
4451 IPMI_DRIVER_VERSION
"\n");
4453 #ifdef CONFIG_PROC_FS
4454 proc_ipmi_root
= proc_mkdir("ipmi", NULL
);
4455 if (!proc_ipmi_root
) {
4456 printk(KERN_ERR PFX
"Unable to create IPMI proc dir");
4460 #endif /* CONFIG_PROC_FS */
4462 setup_timer(&ipmi_timer
, ipmi_timeout
, 0);
4463 mod_timer(&ipmi_timer
, jiffies
+ IPMI_TIMEOUT_JIFFIES
);
4465 atomic_notifier_chain_register(&panic_notifier_list
, &panic_block
);
4472 static int __init
ipmi_init_msghandler_mod(void)
4474 ipmi_init_msghandler();
4478 static void __exit
cleanup_ipmi(void)
4485 atomic_notifier_chain_unregister(&panic_notifier_list
, &panic_block
);
4488 * This can't be called if any interfaces exist, so no worry
4489 * about shutting down the interfaces.
4493 * Tell the timer to stop, then wait for it to stop. This
4494 * avoids problems with race conditions removing the timer
4497 atomic_inc(&stop_operation
);
4498 del_timer_sync(&ipmi_timer
);
4500 #ifdef CONFIG_PROC_FS
4501 remove_proc_entry(proc_ipmi_root
->name
, NULL
);
4502 #endif /* CONFIG_PROC_FS */
4504 driver_unregister(&ipmidriver
.driver
);
4508 /* Check for buffer leaks. */
4509 count
= atomic_read(&smi_msg_inuse_count
);
4511 printk(KERN_WARNING PFX
"SMI message count %d at exit\n",
4513 count
= atomic_read(&recv_msg_inuse_count
);
4515 printk(KERN_WARNING PFX
"recv message count %d at exit\n",
4518 module_exit(cleanup_ipmi
);
4520 module_init(ipmi_init_msghandler_mod
);
4521 MODULE_LICENSE("GPL");
4522 MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
4523 MODULE_DESCRIPTION("Incoming and outgoing message routing for an IPMI"
4525 MODULE_VERSION(IPMI_DRIVER_VERSION
);