1 // SPDX-License-Identifier: GPL-2.0+
5 * Incoming and outgoing message routing for an IPMI interface.
7 * Author: MontaVista Software, Inc.
8 * Corey Minyard <minyard@mvista.com>
11 * Copyright 2002 MontaVista Software Inc.
14 #define pr_fmt(fmt) "%s" fmt, "IPMI message handler: "
15 #define dev_fmt pr_fmt
17 #include <linux/module.h>
18 #include <linux/errno.h>
19 #include <linux/poll.h>
20 #include <linux/sched.h>
21 #include <linux/seq_file.h>
22 #include <linux/spinlock.h>
23 #include <linux/mutex.h>
24 #include <linux/slab.h>
25 #include <linux/ipmi.h>
26 #include <linux/ipmi_smi.h>
27 #include <linux/notifier.h>
28 #include <linux/init.h>
29 #include <linux/proc_fs.h>
30 #include <linux/rcupdate.h>
31 #include <linux/interrupt.h>
32 #include <linux/moduleparam.h>
33 #include <linux/workqueue.h>
34 #include <linux/uuid.h>
35 #include <linux/nospec.h>
36 #include <linux/vmalloc.h>
37 #include <linux/delay.h>
39 #define IPMI_DRIVER_VERSION "39.2"
41 static struct ipmi_recv_msg
*ipmi_alloc_recv_msg(void);
42 static int ipmi_init_msghandler(void);
43 static void smi_recv_tasklet(struct tasklet_struct
*t
);
44 static void handle_new_recv_msgs(struct ipmi_smi
*intf
);
45 static void need_waiter(struct ipmi_smi
*intf
);
46 static int handle_one_recv_msg(struct ipmi_smi
*intf
,
47 struct ipmi_smi_msg
*msg
);
49 static bool initialized
;
50 static bool drvregistered
;
52 enum ipmi_panic_event_op
{
53 IPMI_SEND_PANIC_EVENT_NONE
,
54 IPMI_SEND_PANIC_EVENT
,
55 IPMI_SEND_PANIC_EVENT_STRING
57 #ifdef CONFIG_IPMI_PANIC_STRING
58 #define IPMI_PANIC_DEFAULT IPMI_SEND_PANIC_EVENT_STRING
59 #elif defined(CONFIG_IPMI_PANIC_EVENT)
60 #define IPMI_PANIC_DEFAULT IPMI_SEND_PANIC_EVENT
62 #define IPMI_PANIC_DEFAULT IPMI_SEND_PANIC_EVENT_NONE
65 static enum ipmi_panic_event_op ipmi_send_panic_event
= IPMI_PANIC_DEFAULT
;
67 static int panic_op_write_handler(const char *val
,
68 const struct kernel_param
*kp
)
73 strncpy(valcp
, val
, 15);
78 if (strcmp(s
, "none") == 0)
79 ipmi_send_panic_event
= IPMI_SEND_PANIC_EVENT_NONE
;
80 else if (strcmp(s
, "event") == 0)
81 ipmi_send_panic_event
= IPMI_SEND_PANIC_EVENT
;
82 else if (strcmp(s
, "string") == 0)
83 ipmi_send_panic_event
= IPMI_SEND_PANIC_EVENT_STRING
;
90 static int panic_op_read_handler(char *buffer
, const struct kernel_param
*kp
)
92 switch (ipmi_send_panic_event
) {
93 case IPMI_SEND_PANIC_EVENT_NONE
:
94 strcpy(buffer
, "none\n");
97 case IPMI_SEND_PANIC_EVENT
:
98 strcpy(buffer
, "event\n");
101 case IPMI_SEND_PANIC_EVENT_STRING
:
102 strcpy(buffer
, "string\n");
106 strcpy(buffer
, "???\n");
110 return strlen(buffer
);
113 static const struct kernel_param_ops panic_op_ops
= {
114 .set
= panic_op_write_handler
,
115 .get
= panic_op_read_handler
117 module_param_cb(panic_op
, &panic_op_ops
, NULL
, 0600);
118 MODULE_PARM_DESC(panic_op
, "Sets if the IPMI driver will attempt to store panic information in the event log in the event of a panic. Set to 'none' for no, 'event' for a single event, or 'string' for a generic event and the panic string in IPMI OEM events.");
121 #define MAX_EVENTS_IN_QUEUE 25
123 /* Remain in auto-maintenance mode for this amount of time (in ms). */
124 static unsigned long maintenance_mode_timeout_ms
= 30000;
125 module_param(maintenance_mode_timeout_ms
, ulong
, 0644);
126 MODULE_PARM_DESC(maintenance_mode_timeout_ms
,
127 "The time (milliseconds) after the last maintenance message that the connection stays in maintenance mode.");
130 * Don't let a message sit in a queue forever, always time it with at lest
131 * the max message timer. This is in milliseconds.
133 #define MAX_MSG_TIMEOUT 60000
136 * Timeout times below are in milliseconds, and are done off a 1
137 * second timer. So setting the value to 1000 would mean anything
138 * between 0 and 1000ms. So really the only reasonable minimum
139 * setting it 2000ms, which is between 1 and 2 seconds.
142 /* The default timeout for message retries. */
143 static unsigned long default_retry_ms
= 2000;
144 module_param(default_retry_ms
, ulong
, 0644);
145 MODULE_PARM_DESC(default_retry_ms
,
146 "The time (milliseconds) between retry sends");
148 /* The default timeout for maintenance mode message retries. */
149 static unsigned long default_maintenance_retry_ms
= 3000;
150 module_param(default_maintenance_retry_ms
, ulong
, 0644);
151 MODULE_PARM_DESC(default_maintenance_retry_ms
,
152 "The time (milliseconds) between retry sends in maintenance mode");
154 /* The default maximum number of retries */
155 static unsigned int default_max_retries
= 4;
156 module_param(default_max_retries
, uint
, 0644);
157 MODULE_PARM_DESC(default_max_retries
,
158 "The time (milliseconds) between retry sends in maintenance mode");
160 /* Call every ~1000 ms. */
161 #define IPMI_TIMEOUT_TIME 1000
163 /* How many jiffies does it take to get to the timeout time. */
164 #define IPMI_TIMEOUT_JIFFIES ((IPMI_TIMEOUT_TIME * HZ) / 1000)
167 * Request events from the queue every second (this is the number of
168 * IPMI_TIMEOUT_TIMES between event requests). Hopefully, in the
169 * future, IPMI will add a way to know immediately if an event is in
170 * the queue and this silliness can go away.
172 #define IPMI_REQUEST_EV_TIME (1000 / (IPMI_TIMEOUT_TIME))
174 /* How long should we cache dynamic device IDs? */
175 #define IPMI_DYN_DEV_ID_EXPIRY (10 * HZ)
178 * The main "user" data structure.
181 struct list_head link
;
184 * Set to NULL when the user is destroyed, a pointer to myself
185 * so srcu_dereference can be used on it.
187 struct ipmi_user
*self
;
188 struct srcu_struct release_barrier
;
190 struct kref refcount
;
192 /* The upper layer that handles receive messages. */
193 const struct ipmi_user_hndl
*handler
;
196 /* The interface this user is bound to. */
197 struct ipmi_smi
*intf
;
199 /* Does this interface receive IPMI events? */
202 /* Free must run in process context for RCU cleanup. */
203 struct work_struct remove_work
;
206 static struct ipmi_user
*acquire_ipmi_user(struct ipmi_user
*user
, int *index
)
207 __acquires(user
->release_barrier
)
209 struct ipmi_user
*ruser
;
211 *index
= srcu_read_lock(&user
->release_barrier
);
212 ruser
= srcu_dereference(user
->self
, &user
->release_barrier
);
214 srcu_read_unlock(&user
->release_barrier
, *index
);
218 static void release_ipmi_user(struct ipmi_user
*user
, int index
)
220 srcu_read_unlock(&user
->release_barrier
, index
);
224 struct list_head link
;
226 struct ipmi_user
*user
;
232 * This is used to form a linked lised during mass deletion.
233 * Since this is in an RCU list, we cannot use the link above
234 * or change any data until the RCU period completes. So we
235 * use this next variable during mass deletion so we can have
236 * a list and don't have to wait and restart the search on
237 * every individual deletion of a command.
239 struct cmd_rcvr
*next
;
243 unsigned int inuse
: 1;
244 unsigned int broadcast
: 1;
246 unsigned long timeout
;
247 unsigned long orig_timeout
;
248 unsigned int retries_left
;
251 * To verify on an incoming send message response that this is
252 * the message that the response is for, we keep a sequence id
253 * and increment it every time we send a message.
258 * This is held so we can properly respond to the message on a
259 * timeout, and it is used to hold the temporary data for
260 * retransmission, too.
262 struct ipmi_recv_msg
*recv_msg
;
266 * Store the information in a msgid (long) to allow us to find a
267 * sequence table entry from the msgid.
269 #define STORE_SEQ_IN_MSGID(seq, seqid) \
270 ((((seq) & 0x3f) << 26) | ((seqid) & 0x3ffffff))
272 #define GET_SEQ_FROM_MSGID(msgid, seq, seqid) \
274 seq = (((msgid) >> 26) & 0x3f); \
275 seqid = ((msgid) & 0x3ffffff); \
278 #define NEXT_SEQID(seqid) (((seqid) + 1) & 0x3ffffff)
280 #define IPMI_MAX_CHANNELS 16
281 struct ipmi_channel
{
282 unsigned char medium
;
283 unsigned char protocol
;
286 struct ipmi_channel_set
{
287 struct ipmi_channel c
[IPMI_MAX_CHANNELS
];
290 struct ipmi_my_addrinfo
{
292 * My slave address. This is initialized to IPMI_BMC_SLAVE_ADDR,
293 * but may be changed by the user.
295 unsigned char address
;
298 * My LUN. This should generally stay the SMS LUN, but just in
305 * Note that the product id, manufacturer id, guid, and device id are
306 * immutable in this structure, so dyn_mutex is not required for
307 * accessing those. If those change on a BMC, a new BMC is allocated.
310 struct platform_device pdev
;
311 struct list_head intfs
; /* Interfaces on this BMC. */
312 struct ipmi_device_id id
;
313 struct ipmi_device_id fetch_id
;
315 unsigned long dyn_id_expiry
;
316 struct mutex dyn_mutex
; /* Protects id, intfs, & dyn* */
320 struct kref usecount
;
321 struct work_struct remove_work
;
322 unsigned char cc
; /* completion code */
324 #define to_bmc_device(x) container_of((x), struct bmc_device, pdev.dev)
326 static int bmc_get_device_id(struct ipmi_smi
*intf
, struct bmc_device
*bmc
,
327 struct ipmi_device_id
*id
,
328 bool *guid_set
, guid_t
*guid
);
331 * Various statistics for IPMI, these index stats[] in the ipmi_smi
334 enum ipmi_stat_indexes
{
335 /* Commands we got from the user that were invalid. */
336 IPMI_STAT_sent_invalid_commands
= 0,
338 /* Commands we sent to the MC. */
339 IPMI_STAT_sent_local_commands
,
341 /* Responses from the MC that were delivered to a user. */
342 IPMI_STAT_handled_local_responses
,
344 /* Responses from the MC that were not delivered to a user. */
345 IPMI_STAT_unhandled_local_responses
,
347 /* Commands we sent out to the IPMB bus. */
348 IPMI_STAT_sent_ipmb_commands
,
350 /* Commands sent on the IPMB that had errors on the SEND CMD */
351 IPMI_STAT_sent_ipmb_command_errs
,
353 /* Each retransmit increments this count. */
354 IPMI_STAT_retransmitted_ipmb_commands
,
357 * When a message times out (runs out of retransmits) this is
360 IPMI_STAT_timed_out_ipmb_commands
,
363 * This is like above, but for broadcasts. Broadcasts are
364 * *not* included in the above count (they are expected to
367 IPMI_STAT_timed_out_ipmb_broadcasts
,
369 /* Responses I have sent to the IPMB bus. */
370 IPMI_STAT_sent_ipmb_responses
,
372 /* The response was delivered to the user. */
373 IPMI_STAT_handled_ipmb_responses
,
375 /* The response had invalid data in it. */
376 IPMI_STAT_invalid_ipmb_responses
,
378 /* The response didn't have anyone waiting for it. */
379 IPMI_STAT_unhandled_ipmb_responses
,
381 /* Commands we sent out to the IPMB bus. */
382 IPMI_STAT_sent_lan_commands
,
384 /* Commands sent on the IPMB that had errors on the SEND CMD */
385 IPMI_STAT_sent_lan_command_errs
,
387 /* Each retransmit increments this count. */
388 IPMI_STAT_retransmitted_lan_commands
,
391 * When a message times out (runs out of retransmits) this is
394 IPMI_STAT_timed_out_lan_commands
,
396 /* Responses I have sent to the IPMB bus. */
397 IPMI_STAT_sent_lan_responses
,
399 /* The response was delivered to the user. */
400 IPMI_STAT_handled_lan_responses
,
402 /* The response had invalid data in it. */
403 IPMI_STAT_invalid_lan_responses
,
405 /* The response didn't have anyone waiting for it. */
406 IPMI_STAT_unhandled_lan_responses
,
408 /* The command was delivered to the user. */
409 IPMI_STAT_handled_commands
,
411 /* The command had invalid data in it. */
412 IPMI_STAT_invalid_commands
,
414 /* The command didn't have anyone waiting for it. */
415 IPMI_STAT_unhandled_commands
,
417 /* Invalid data in an event. */
418 IPMI_STAT_invalid_events
,
420 /* Events that were received with the proper format. */
423 /* Retransmissions on IPMB that failed. */
424 IPMI_STAT_dropped_rexmit_ipmb_commands
,
426 /* Retransmissions on LAN that failed. */
427 IPMI_STAT_dropped_rexmit_lan_commands
,
429 /* This *must* remain last, add new values above this. */
434 #define IPMI_IPMB_NUM_SEQ 64
436 struct module
*owner
;
438 /* What interface number are we? */
441 struct kref refcount
;
443 /* Set when the interface is being unregistered. */
446 /* Used for a list of interfaces. */
447 struct list_head link
;
450 * The list of upper layers that are using me. seq_lock write
451 * protects this. Read protection is with srcu.
453 struct list_head users
;
454 struct srcu_struct users_srcu
;
456 /* Used for wake ups at startup. */
457 wait_queue_head_t waitq
;
460 * Prevents the interface from being unregistered when the
461 * interface is used by being looked up through the BMC
464 struct mutex bmc_reg_mutex
;
466 struct bmc_device tmp_bmc
;
467 struct bmc_device
*bmc
;
469 struct list_head bmc_link
;
471 bool in_bmc_register
; /* Handle recursive situations. Yuck. */
472 struct work_struct bmc_reg_work
;
474 const struct ipmi_smi_handlers
*handlers
;
477 /* Driver-model device for the system interface. */
478 struct device
*si_dev
;
481 * A table of sequence numbers for this interface. We use the
482 * sequence numbers for IPMB messages that go out of the
483 * interface to match them up with their responses. A routine
484 * is called periodically to time the items in this list.
487 struct seq_table seq_table
[IPMI_IPMB_NUM_SEQ
];
491 * Messages queued for delivery. If delivery fails (out of memory
492 * for instance), They will stay in here to be processed later in a
493 * periodic timer interrupt. The tasklet is for handling received
494 * messages directly from the handler.
496 spinlock_t waiting_rcv_msgs_lock
;
497 struct list_head waiting_rcv_msgs
;
498 atomic_t watchdog_pretimeouts_to_deliver
;
499 struct tasklet_struct recv_tasklet
;
501 spinlock_t xmit_msgs_lock
;
502 struct list_head xmit_msgs
;
503 struct ipmi_smi_msg
*curr_msg
;
504 struct list_head hp_xmit_msgs
;
507 * The list of command receivers that are registered for commands
510 struct mutex cmd_rcvrs_mutex
;
511 struct list_head cmd_rcvrs
;
514 * Events that were queues because no one was there to receive
517 spinlock_t events_lock
; /* For dealing with event stuff. */
518 struct list_head waiting_events
;
519 unsigned int waiting_events_count
; /* How many events in queue? */
520 char delivering_events
;
521 char event_msg_printed
;
523 /* How many users are waiting for events? */
524 atomic_t event_waiters
;
525 unsigned int ticks_to_req_ev
;
527 spinlock_t watch_lock
; /* For dealing with watch stuff below. */
529 /* How many users are waiting for commands? */
530 unsigned int command_waiters
;
532 /* How many users are waiting for watchdogs? */
533 unsigned int watchdog_waiters
;
535 /* How many users are waiting for message responses? */
536 unsigned int response_waiters
;
539 * Tells what the lower layer has last been asked to watch for,
540 * messages and/or watchdogs. Protected by watch_lock.
542 unsigned int last_watch_mask
;
545 * The event receiver for my BMC, only really used at panic
546 * shutdown as a place to store this.
548 unsigned char event_receiver
;
549 unsigned char event_receiver_lun
;
550 unsigned char local_sel_device
;
551 unsigned char local_event_generator
;
553 /* For handling of maintenance mode. */
554 int maintenance_mode
;
555 bool maintenance_mode_enable
;
556 int auto_maintenance_timeout
;
557 spinlock_t maintenance_mode_lock
; /* Used in a timer... */
560 * If we are doing maintenance on something on IPMB, extend
561 * the timeout time to avoid timeouts writing firmware and
564 int ipmb_maintenance_mode_timeout
;
567 * A cheap hack, if this is non-null and a message to an
568 * interface comes in with a NULL user, call this routine with
569 * it. Note that the message will still be freed by the
570 * caller. This only works on the system interface.
572 * Protected by bmc_reg_mutex.
574 void (*null_user_handler
)(struct ipmi_smi
*intf
,
575 struct ipmi_recv_msg
*msg
);
578 * When we are scanning the channels for an SMI, this will
579 * tell which channel we are scanning.
583 /* Channel information */
584 struct ipmi_channel_set
*channel_list
;
585 unsigned int curr_working_cset
; /* First index into the following. */
586 struct ipmi_channel_set wchannels
[2];
587 struct ipmi_my_addrinfo addrinfo
[IPMI_MAX_CHANNELS
];
590 atomic_t stats
[IPMI_NUM_STATS
];
593 * run_to_completion duplicate of smb_info, smi_info
594 * and ipmi_serial_info structures. Used to decrease numbers of
595 * parameters passed by "low" level IPMI code.
597 int run_to_completion
;
599 #define to_si_intf_from_dev(device) container_of(device, struct ipmi_smi, dev)
601 static void __get_guid(struct ipmi_smi
*intf
);
602 static void __ipmi_bmc_unregister(struct ipmi_smi
*intf
);
603 static int __ipmi_bmc_register(struct ipmi_smi
*intf
,
604 struct ipmi_device_id
*id
,
605 bool guid_set
, guid_t
*guid
, int intf_num
);
606 static int __scan_channels(struct ipmi_smi
*intf
, struct ipmi_device_id
*id
);
610 * The driver model view of the IPMI messaging driver.
612 static struct platform_driver ipmidriver
= {
615 .bus
= &platform_bus_type
619 * This mutex keeps us from adding the same BMC twice.
621 static DEFINE_MUTEX(ipmidriver_mutex
);
623 static LIST_HEAD(ipmi_interfaces
);
624 static DEFINE_MUTEX(ipmi_interfaces_mutex
);
625 #define ipmi_interfaces_mutex_held() \
626 lockdep_is_held(&ipmi_interfaces_mutex)
627 static struct srcu_struct ipmi_interfaces_srcu
;
630 * List of watchers that want to know when smi's are added and deleted.
632 static LIST_HEAD(smi_watchers
);
633 static DEFINE_MUTEX(smi_watchers_mutex
);
635 #define ipmi_inc_stat(intf, stat) \
636 atomic_inc(&(intf)->stats[IPMI_STAT_ ## stat])
637 #define ipmi_get_stat(intf, stat) \
638 ((unsigned int) atomic_read(&(intf)->stats[IPMI_STAT_ ## stat]))
640 static const char * const addr_src_to_str
[] = {
641 "invalid", "hotmod", "hardcoded", "SPMI", "ACPI", "SMBIOS", "PCI",
642 "device-tree", "platform"
645 const char *ipmi_addr_src_to_str(enum ipmi_addr_src src
)
648 src
= 0; /* Invalid */
649 return addr_src_to_str
[src
];
651 EXPORT_SYMBOL(ipmi_addr_src_to_str
);
653 static int is_lan_addr(struct ipmi_addr
*addr
)
655 return addr
->addr_type
== IPMI_LAN_ADDR_TYPE
;
658 static int is_ipmb_addr(struct ipmi_addr
*addr
)
660 return addr
->addr_type
== IPMI_IPMB_ADDR_TYPE
;
663 static int is_ipmb_bcast_addr(struct ipmi_addr
*addr
)
665 return addr
->addr_type
== IPMI_IPMB_BROADCAST_ADDR_TYPE
;
668 static void free_recv_msg_list(struct list_head
*q
)
670 struct ipmi_recv_msg
*msg
, *msg2
;
672 list_for_each_entry_safe(msg
, msg2
, q
, link
) {
673 list_del(&msg
->link
);
674 ipmi_free_recv_msg(msg
);
678 static void free_smi_msg_list(struct list_head
*q
)
680 struct ipmi_smi_msg
*msg
, *msg2
;
682 list_for_each_entry_safe(msg
, msg2
, q
, link
) {
683 list_del(&msg
->link
);
684 ipmi_free_smi_msg(msg
);
688 static void clean_up_interface_data(struct ipmi_smi
*intf
)
691 struct cmd_rcvr
*rcvr
, *rcvr2
;
692 struct list_head list
;
694 tasklet_kill(&intf
->recv_tasklet
);
696 free_smi_msg_list(&intf
->waiting_rcv_msgs
);
697 free_recv_msg_list(&intf
->waiting_events
);
700 * Wholesale remove all the entries from the list in the
701 * interface and wait for RCU to know that none are in use.
703 mutex_lock(&intf
->cmd_rcvrs_mutex
);
704 INIT_LIST_HEAD(&list
);
705 list_splice_init_rcu(&intf
->cmd_rcvrs
, &list
, synchronize_rcu
);
706 mutex_unlock(&intf
->cmd_rcvrs_mutex
);
708 list_for_each_entry_safe(rcvr
, rcvr2
, &list
, link
)
711 for (i
= 0; i
< IPMI_IPMB_NUM_SEQ
; i
++) {
712 if ((intf
->seq_table
[i
].inuse
)
713 && (intf
->seq_table
[i
].recv_msg
))
714 ipmi_free_recv_msg(intf
->seq_table
[i
].recv_msg
);
718 static void intf_free(struct kref
*ref
)
720 struct ipmi_smi
*intf
= container_of(ref
, struct ipmi_smi
, refcount
);
722 clean_up_interface_data(intf
);
726 struct watcher_entry
{
728 struct ipmi_smi
*intf
;
729 struct list_head link
;
732 int ipmi_smi_watcher_register(struct ipmi_smi_watcher
*watcher
)
734 struct ipmi_smi
*intf
;
738 * Make sure the driver is actually initialized, this handles
739 * problems with initialization order.
741 rv
= ipmi_init_msghandler();
745 mutex_lock(&smi_watchers_mutex
);
747 list_add(&watcher
->link
, &smi_watchers
);
749 index
= srcu_read_lock(&ipmi_interfaces_srcu
);
750 list_for_each_entry_rcu(intf
, &ipmi_interfaces
, link
,
751 lockdep_is_held(&smi_watchers_mutex
)) {
752 int intf_num
= READ_ONCE(intf
->intf_num
);
756 watcher
->new_smi(intf_num
, intf
->si_dev
);
758 srcu_read_unlock(&ipmi_interfaces_srcu
, index
);
760 mutex_unlock(&smi_watchers_mutex
);
764 EXPORT_SYMBOL(ipmi_smi_watcher_register
);
766 int ipmi_smi_watcher_unregister(struct ipmi_smi_watcher
*watcher
)
768 mutex_lock(&smi_watchers_mutex
);
769 list_del(&watcher
->link
);
770 mutex_unlock(&smi_watchers_mutex
);
773 EXPORT_SYMBOL(ipmi_smi_watcher_unregister
);
776 * Must be called with smi_watchers_mutex held.
779 call_smi_watchers(int i
, struct device
*dev
)
781 struct ipmi_smi_watcher
*w
;
783 mutex_lock(&smi_watchers_mutex
);
784 list_for_each_entry(w
, &smi_watchers
, link
) {
785 if (try_module_get(w
->owner
)) {
787 module_put(w
->owner
);
790 mutex_unlock(&smi_watchers_mutex
);
794 ipmi_addr_equal(struct ipmi_addr
*addr1
, struct ipmi_addr
*addr2
)
796 if (addr1
->addr_type
!= addr2
->addr_type
)
799 if (addr1
->channel
!= addr2
->channel
)
802 if (addr1
->addr_type
== IPMI_SYSTEM_INTERFACE_ADDR_TYPE
) {
803 struct ipmi_system_interface_addr
*smi_addr1
804 = (struct ipmi_system_interface_addr
*) addr1
;
805 struct ipmi_system_interface_addr
*smi_addr2
806 = (struct ipmi_system_interface_addr
*) addr2
;
807 return (smi_addr1
->lun
== smi_addr2
->lun
);
810 if (is_ipmb_addr(addr1
) || is_ipmb_bcast_addr(addr1
)) {
811 struct ipmi_ipmb_addr
*ipmb_addr1
812 = (struct ipmi_ipmb_addr
*) addr1
;
813 struct ipmi_ipmb_addr
*ipmb_addr2
814 = (struct ipmi_ipmb_addr
*) addr2
;
816 return ((ipmb_addr1
->slave_addr
== ipmb_addr2
->slave_addr
)
817 && (ipmb_addr1
->lun
== ipmb_addr2
->lun
));
820 if (is_lan_addr(addr1
)) {
821 struct ipmi_lan_addr
*lan_addr1
822 = (struct ipmi_lan_addr
*) addr1
;
823 struct ipmi_lan_addr
*lan_addr2
824 = (struct ipmi_lan_addr
*) addr2
;
826 return ((lan_addr1
->remote_SWID
== lan_addr2
->remote_SWID
)
827 && (lan_addr1
->local_SWID
== lan_addr2
->local_SWID
)
828 && (lan_addr1
->session_handle
829 == lan_addr2
->session_handle
)
830 && (lan_addr1
->lun
== lan_addr2
->lun
));
836 int ipmi_validate_addr(struct ipmi_addr
*addr
, int len
)
838 if (len
< sizeof(struct ipmi_system_interface_addr
))
841 if (addr
->addr_type
== IPMI_SYSTEM_INTERFACE_ADDR_TYPE
) {
842 if (addr
->channel
!= IPMI_BMC_CHANNEL
)
847 if ((addr
->channel
== IPMI_BMC_CHANNEL
)
848 || (addr
->channel
>= IPMI_MAX_CHANNELS
)
849 || (addr
->channel
< 0))
852 if (is_ipmb_addr(addr
) || is_ipmb_bcast_addr(addr
)) {
853 if (len
< sizeof(struct ipmi_ipmb_addr
))
858 if (is_lan_addr(addr
)) {
859 if (len
< sizeof(struct ipmi_lan_addr
))
866 EXPORT_SYMBOL(ipmi_validate_addr
);
868 unsigned int ipmi_addr_length(int addr_type
)
870 if (addr_type
== IPMI_SYSTEM_INTERFACE_ADDR_TYPE
)
871 return sizeof(struct ipmi_system_interface_addr
);
873 if ((addr_type
== IPMI_IPMB_ADDR_TYPE
)
874 || (addr_type
== IPMI_IPMB_BROADCAST_ADDR_TYPE
))
875 return sizeof(struct ipmi_ipmb_addr
);
877 if (addr_type
== IPMI_LAN_ADDR_TYPE
)
878 return sizeof(struct ipmi_lan_addr
);
882 EXPORT_SYMBOL(ipmi_addr_length
);
884 static int deliver_response(struct ipmi_smi
*intf
, struct ipmi_recv_msg
*msg
)
889 /* Special handling for NULL users. */
890 if (intf
->null_user_handler
) {
891 intf
->null_user_handler(intf
, msg
);
893 /* No handler, so give up. */
896 ipmi_free_recv_msg(msg
);
897 } else if (oops_in_progress
) {
899 * If we are running in the panic context, calling the
900 * receive handler doesn't much meaning and has a deadlock
901 * risk. At this moment, simply skip it in that case.
903 ipmi_free_recv_msg(msg
);
906 struct ipmi_user
*user
= acquire_ipmi_user(msg
->user
, &index
);
909 user
->handler
->ipmi_recv_hndl(msg
, user
->handler_data
);
910 release_ipmi_user(user
, index
);
912 /* User went away, give up. */
913 ipmi_free_recv_msg(msg
);
921 static void deliver_local_response(struct ipmi_smi
*intf
,
922 struct ipmi_recv_msg
*msg
)
924 if (deliver_response(intf
, msg
))
925 ipmi_inc_stat(intf
, unhandled_local_responses
);
927 ipmi_inc_stat(intf
, handled_local_responses
);
930 static void deliver_err_response(struct ipmi_smi
*intf
,
931 struct ipmi_recv_msg
*msg
, int err
)
933 msg
->recv_type
= IPMI_RESPONSE_RECV_TYPE
;
934 msg
->msg_data
[0] = err
;
935 msg
->msg
.netfn
|= 1; /* Convert to a response. */
936 msg
->msg
.data_len
= 1;
937 msg
->msg
.data
= msg
->msg_data
;
938 deliver_local_response(intf
, msg
);
941 static void smi_add_watch(struct ipmi_smi
*intf
, unsigned int flags
)
943 unsigned long iflags
;
945 if (!intf
->handlers
->set_need_watch
)
948 spin_lock_irqsave(&intf
->watch_lock
, iflags
);
949 if (flags
& IPMI_WATCH_MASK_CHECK_MESSAGES
)
950 intf
->response_waiters
++;
952 if (flags
& IPMI_WATCH_MASK_CHECK_WATCHDOG
)
953 intf
->watchdog_waiters
++;
955 if (flags
& IPMI_WATCH_MASK_CHECK_COMMANDS
)
956 intf
->command_waiters
++;
958 if ((intf
->last_watch_mask
& flags
) != flags
) {
959 intf
->last_watch_mask
|= flags
;
960 intf
->handlers
->set_need_watch(intf
->send_info
,
961 intf
->last_watch_mask
);
963 spin_unlock_irqrestore(&intf
->watch_lock
, iflags
);
966 static void smi_remove_watch(struct ipmi_smi
*intf
, unsigned int flags
)
968 unsigned long iflags
;
970 if (!intf
->handlers
->set_need_watch
)
973 spin_lock_irqsave(&intf
->watch_lock
, iflags
);
974 if (flags
& IPMI_WATCH_MASK_CHECK_MESSAGES
)
975 intf
->response_waiters
--;
977 if (flags
& IPMI_WATCH_MASK_CHECK_WATCHDOG
)
978 intf
->watchdog_waiters
--;
980 if (flags
& IPMI_WATCH_MASK_CHECK_COMMANDS
)
981 intf
->command_waiters
--;
984 if (intf
->response_waiters
)
985 flags
|= IPMI_WATCH_MASK_CHECK_MESSAGES
;
986 if (intf
->watchdog_waiters
)
987 flags
|= IPMI_WATCH_MASK_CHECK_WATCHDOG
;
988 if (intf
->command_waiters
)
989 flags
|= IPMI_WATCH_MASK_CHECK_COMMANDS
;
991 if (intf
->last_watch_mask
!= flags
) {
992 intf
->last_watch_mask
= flags
;
993 intf
->handlers
->set_need_watch(intf
->send_info
,
994 intf
->last_watch_mask
);
996 spin_unlock_irqrestore(&intf
->watch_lock
, iflags
);
1000 * Find the next sequence number not being used and add the given
1001 * message with the given timeout to the sequence table. This must be
1002 * called with the interface's seq_lock held.
1004 static int intf_next_seq(struct ipmi_smi
*intf
,
1005 struct ipmi_recv_msg
*recv_msg
,
1006 unsigned long timeout
,
1016 timeout
= default_retry_ms
;
1018 retries
= default_max_retries
;
1020 for (i
= intf
->curr_seq
; (i
+1)%IPMI_IPMB_NUM_SEQ
!= intf
->curr_seq
;
1021 i
= (i
+1)%IPMI_IPMB_NUM_SEQ
) {
1022 if (!intf
->seq_table
[i
].inuse
)
1026 if (!intf
->seq_table
[i
].inuse
) {
1027 intf
->seq_table
[i
].recv_msg
= recv_msg
;
1030 * Start with the maximum timeout, when the send response
1031 * comes in we will start the real timer.
1033 intf
->seq_table
[i
].timeout
= MAX_MSG_TIMEOUT
;
1034 intf
->seq_table
[i
].orig_timeout
= timeout
;
1035 intf
->seq_table
[i
].retries_left
= retries
;
1036 intf
->seq_table
[i
].broadcast
= broadcast
;
1037 intf
->seq_table
[i
].inuse
= 1;
1038 intf
->seq_table
[i
].seqid
= NEXT_SEQID(intf
->seq_table
[i
].seqid
);
1040 *seqid
= intf
->seq_table
[i
].seqid
;
1041 intf
->curr_seq
= (i
+1)%IPMI_IPMB_NUM_SEQ
;
1042 smi_add_watch(intf
, IPMI_WATCH_MASK_CHECK_MESSAGES
);
1052 * Return the receive message for the given sequence number and
1053 * release the sequence number so it can be reused. Some other data
1054 * is passed in to be sure the message matches up correctly (to help
1055 * guard against message coming in after their timeout and the
1056 * sequence number being reused).
1058 static int intf_find_seq(struct ipmi_smi
*intf
,
1062 unsigned char netfn
,
1063 struct ipmi_addr
*addr
,
1064 struct ipmi_recv_msg
**recv_msg
)
1067 unsigned long flags
;
1069 if (seq
>= IPMI_IPMB_NUM_SEQ
)
1072 spin_lock_irqsave(&intf
->seq_lock
, flags
);
1073 if (intf
->seq_table
[seq
].inuse
) {
1074 struct ipmi_recv_msg
*msg
= intf
->seq_table
[seq
].recv_msg
;
1076 if ((msg
->addr
.channel
== channel
) && (msg
->msg
.cmd
== cmd
)
1077 && (msg
->msg
.netfn
== netfn
)
1078 && (ipmi_addr_equal(addr
, &msg
->addr
))) {
1080 intf
->seq_table
[seq
].inuse
= 0;
1081 smi_remove_watch(intf
, IPMI_WATCH_MASK_CHECK_MESSAGES
);
1085 spin_unlock_irqrestore(&intf
->seq_lock
, flags
);
1091 /* Start the timer for a specific sequence table entry. */
1092 static int intf_start_seq_timer(struct ipmi_smi
*intf
,
1096 unsigned long flags
;
1098 unsigned long seqid
;
1101 GET_SEQ_FROM_MSGID(msgid
, seq
, seqid
);
1103 spin_lock_irqsave(&intf
->seq_lock
, flags
);
1105 * We do this verification because the user can be deleted
1106 * while a message is outstanding.
1108 if ((intf
->seq_table
[seq
].inuse
)
1109 && (intf
->seq_table
[seq
].seqid
== seqid
)) {
1110 struct seq_table
*ent
= &intf
->seq_table
[seq
];
1111 ent
->timeout
= ent
->orig_timeout
;
1114 spin_unlock_irqrestore(&intf
->seq_lock
, flags
);
1119 /* Got an error for the send message for a specific sequence number. */
1120 static int intf_err_seq(struct ipmi_smi
*intf
,
1125 unsigned long flags
;
1127 unsigned long seqid
;
1128 struct ipmi_recv_msg
*msg
= NULL
;
1131 GET_SEQ_FROM_MSGID(msgid
, seq
, seqid
);
1133 spin_lock_irqsave(&intf
->seq_lock
, flags
);
1135 * We do this verification because the user can be deleted
1136 * while a message is outstanding.
1138 if ((intf
->seq_table
[seq
].inuse
)
1139 && (intf
->seq_table
[seq
].seqid
== seqid
)) {
1140 struct seq_table
*ent
= &intf
->seq_table
[seq
];
1143 smi_remove_watch(intf
, IPMI_WATCH_MASK_CHECK_MESSAGES
);
1144 msg
= ent
->recv_msg
;
1147 spin_unlock_irqrestore(&intf
->seq_lock
, flags
);
1150 deliver_err_response(intf
, msg
, err
);
1155 static void free_user_work(struct work_struct
*work
)
1157 struct ipmi_user
*user
= container_of(work
, struct ipmi_user
,
1160 cleanup_srcu_struct(&user
->release_barrier
);
1164 int ipmi_create_user(unsigned int if_num
,
1165 const struct ipmi_user_hndl
*handler
,
1167 struct ipmi_user
**user
)
1169 unsigned long flags
;
1170 struct ipmi_user
*new_user
;
1172 struct ipmi_smi
*intf
;
1175 * There is no module usecount here, because it's not
1176 * required. Since this can only be used by and called from
1177 * other modules, they will implicitly use this module, and
1178 * thus this can't be removed unless the other modules are
1182 if (handler
== NULL
)
1186 * Make sure the driver is actually initialized, this handles
1187 * problems with initialization order.
1189 rv
= ipmi_init_msghandler();
1193 new_user
= vzalloc(sizeof(*new_user
));
1197 index
= srcu_read_lock(&ipmi_interfaces_srcu
);
1198 list_for_each_entry_rcu(intf
, &ipmi_interfaces
, link
) {
1199 if (intf
->intf_num
== if_num
)
1202 /* Not found, return an error */
1207 INIT_WORK(&new_user
->remove_work
, free_user_work
);
1209 rv
= init_srcu_struct(&new_user
->release_barrier
);
1213 if (!try_module_get(intf
->owner
)) {
1218 /* Note that each existing user holds a refcount to the interface. */
1219 kref_get(&intf
->refcount
);
1221 kref_init(&new_user
->refcount
);
1222 new_user
->handler
= handler
;
1223 new_user
->handler_data
= handler_data
;
1224 new_user
->intf
= intf
;
1225 new_user
->gets_events
= false;
1227 rcu_assign_pointer(new_user
->self
, new_user
);
1228 spin_lock_irqsave(&intf
->seq_lock
, flags
);
1229 list_add_rcu(&new_user
->link
, &intf
->users
);
1230 spin_unlock_irqrestore(&intf
->seq_lock
, flags
);
1231 if (handler
->ipmi_watchdog_pretimeout
)
1232 /* User wants pretimeouts, so make sure to watch for them. */
1233 smi_add_watch(intf
, IPMI_WATCH_MASK_CHECK_WATCHDOG
);
1234 srcu_read_unlock(&ipmi_interfaces_srcu
, index
);
1239 srcu_read_unlock(&ipmi_interfaces_srcu
, index
);
1243 EXPORT_SYMBOL(ipmi_create_user
);
1245 int ipmi_get_smi_info(int if_num
, struct ipmi_smi_info
*data
)
1248 struct ipmi_smi
*intf
;
1250 index
= srcu_read_lock(&ipmi_interfaces_srcu
);
1251 list_for_each_entry_rcu(intf
, &ipmi_interfaces
, link
) {
1252 if (intf
->intf_num
== if_num
)
1255 srcu_read_unlock(&ipmi_interfaces_srcu
, index
);
1257 /* Not found, return an error */
1261 if (!intf
->handlers
->get_smi_info
)
1264 rv
= intf
->handlers
->get_smi_info(intf
->send_info
, data
);
1265 srcu_read_unlock(&ipmi_interfaces_srcu
, index
);
1269 EXPORT_SYMBOL(ipmi_get_smi_info
);
1271 static void free_user(struct kref
*ref
)
1273 struct ipmi_user
*user
= container_of(ref
, struct ipmi_user
, refcount
);
1275 /* SRCU cleanup must happen in task context. */
1276 schedule_work(&user
->remove_work
);
1279 static void _ipmi_destroy_user(struct ipmi_user
*user
)
1281 struct ipmi_smi
*intf
= user
->intf
;
1283 unsigned long flags
;
1284 struct cmd_rcvr
*rcvr
;
1285 struct cmd_rcvr
*rcvrs
= NULL
;
1287 if (!acquire_ipmi_user(user
, &i
)) {
1289 * The user has already been cleaned up, just make sure
1290 * nothing is using it and return.
1292 synchronize_srcu(&user
->release_barrier
);
1296 rcu_assign_pointer(user
->self
, NULL
);
1297 release_ipmi_user(user
, i
);
1299 synchronize_srcu(&user
->release_barrier
);
1301 if (user
->handler
->shutdown
)
1302 user
->handler
->shutdown(user
->handler_data
);
1304 if (user
->handler
->ipmi_watchdog_pretimeout
)
1305 smi_remove_watch(intf
, IPMI_WATCH_MASK_CHECK_WATCHDOG
);
1307 if (user
->gets_events
)
1308 atomic_dec(&intf
->event_waiters
);
1310 /* Remove the user from the interface's sequence table. */
1311 spin_lock_irqsave(&intf
->seq_lock
, flags
);
1312 list_del_rcu(&user
->link
);
1314 for (i
= 0; i
< IPMI_IPMB_NUM_SEQ
; i
++) {
1315 if (intf
->seq_table
[i
].inuse
1316 && (intf
->seq_table
[i
].recv_msg
->user
== user
)) {
1317 intf
->seq_table
[i
].inuse
= 0;
1318 smi_remove_watch(intf
, IPMI_WATCH_MASK_CHECK_MESSAGES
);
1319 ipmi_free_recv_msg(intf
->seq_table
[i
].recv_msg
);
1322 spin_unlock_irqrestore(&intf
->seq_lock
, flags
);
1325 * Remove the user from the command receiver's table. First
1326 * we build a list of everything (not using the standard link,
1327 * since other things may be using it till we do
1328 * synchronize_srcu()) then free everything in that list.
1330 mutex_lock(&intf
->cmd_rcvrs_mutex
);
1331 list_for_each_entry_rcu(rcvr
, &intf
->cmd_rcvrs
, link
,
1332 lockdep_is_held(&intf
->cmd_rcvrs_mutex
)) {
1333 if (rcvr
->user
== user
) {
1334 list_del_rcu(&rcvr
->link
);
1339 mutex_unlock(&intf
->cmd_rcvrs_mutex
);
1347 kref_put(&intf
->refcount
, intf_free
);
1348 module_put(intf
->owner
);
1351 int ipmi_destroy_user(struct ipmi_user
*user
)
1353 _ipmi_destroy_user(user
);
1355 kref_put(&user
->refcount
, free_user
);
1359 EXPORT_SYMBOL(ipmi_destroy_user
);
1361 int ipmi_get_version(struct ipmi_user
*user
,
1362 unsigned char *major
,
1363 unsigned char *minor
)
1365 struct ipmi_device_id id
;
1368 user
= acquire_ipmi_user(user
, &index
);
1372 rv
= bmc_get_device_id(user
->intf
, NULL
, &id
, NULL
, NULL
);
1374 *major
= ipmi_version_major(&id
);
1375 *minor
= ipmi_version_minor(&id
);
1377 release_ipmi_user(user
, index
);
1381 EXPORT_SYMBOL(ipmi_get_version
);
1383 int ipmi_set_my_address(struct ipmi_user
*user
,
1384 unsigned int channel
,
1385 unsigned char address
)
1389 user
= acquire_ipmi_user(user
, &index
);
1393 if (channel
>= IPMI_MAX_CHANNELS
) {
1396 channel
= array_index_nospec(channel
, IPMI_MAX_CHANNELS
);
1397 user
->intf
->addrinfo
[channel
].address
= address
;
1399 release_ipmi_user(user
, index
);
1403 EXPORT_SYMBOL(ipmi_set_my_address
);
1405 int ipmi_get_my_address(struct ipmi_user
*user
,
1406 unsigned int channel
,
1407 unsigned char *address
)
1411 user
= acquire_ipmi_user(user
, &index
);
1415 if (channel
>= IPMI_MAX_CHANNELS
) {
1418 channel
= array_index_nospec(channel
, IPMI_MAX_CHANNELS
);
1419 *address
= user
->intf
->addrinfo
[channel
].address
;
1421 release_ipmi_user(user
, index
);
1425 EXPORT_SYMBOL(ipmi_get_my_address
);
1427 int ipmi_set_my_LUN(struct ipmi_user
*user
,
1428 unsigned int channel
,
1433 user
= acquire_ipmi_user(user
, &index
);
1437 if (channel
>= IPMI_MAX_CHANNELS
) {
1440 channel
= array_index_nospec(channel
, IPMI_MAX_CHANNELS
);
1441 user
->intf
->addrinfo
[channel
].lun
= LUN
& 0x3;
1443 release_ipmi_user(user
, index
);
1447 EXPORT_SYMBOL(ipmi_set_my_LUN
);
1449 int ipmi_get_my_LUN(struct ipmi_user
*user
,
1450 unsigned int channel
,
1451 unsigned char *address
)
1455 user
= acquire_ipmi_user(user
, &index
);
1459 if (channel
>= IPMI_MAX_CHANNELS
) {
1462 channel
= array_index_nospec(channel
, IPMI_MAX_CHANNELS
);
1463 *address
= user
->intf
->addrinfo
[channel
].lun
;
1465 release_ipmi_user(user
, index
);
1469 EXPORT_SYMBOL(ipmi_get_my_LUN
);
1471 int ipmi_get_maintenance_mode(struct ipmi_user
*user
)
1474 unsigned long flags
;
1476 user
= acquire_ipmi_user(user
, &index
);
1480 spin_lock_irqsave(&user
->intf
->maintenance_mode_lock
, flags
);
1481 mode
= user
->intf
->maintenance_mode
;
1482 spin_unlock_irqrestore(&user
->intf
->maintenance_mode_lock
, flags
);
1483 release_ipmi_user(user
, index
);
1487 EXPORT_SYMBOL(ipmi_get_maintenance_mode
);
1489 static void maintenance_mode_update(struct ipmi_smi
*intf
)
1491 if (intf
->handlers
->set_maintenance_mode
)
1492 intf
->handlers
->set_maintenance_mode(
1493 intf
->send_info
, intf
->maintenance_mode_enable
);
1496 int ipmi_set_maintenance_mode(struct ipmi_user
*user
, int mode
)
1499 unsigned long flags
;
1500 struct ipmi_smi
*intf
= user
->intf
;
1502 user
= acquire_ipmi_user(user
, &index
);
1506 spin_lock_irqsave(&intf
->maintenance_mode_lock
, flags
);
1507 if (intf
->maintenance_mode
!= mode
) {
1509 case IPMI_MAINTENANCE_MODE_AUTO
:
1510 intf
->maintenance_mode_enable
1511 = (intf
->auto_maintenance_timeout
> 0);
1514 case IPMI_MAINTENANCE_MODE_OFF
:
1515 intf
->maintenance_mode_enable
= false;
1518 case IPMI_MAINTENANCE_MODE_ON
:
1519 intf
->maintenance_mode_enable
= true;
1526 intf
->maintenance_mode
= mode
;
1528 maintenance_mode_update(intf
);
1531 spin_unlock_irqrestore(&intf
->maintenance_mode_lock
, flags
);
1532 release_ipmi_user(user
, index
);
1536 EXPORT_SYMBOL(ipmi_set_maintenance_mode
);
1538 int ipmi_set_gets_events(struct ipmi_user
*user
, bool val
)
1540 unsigned long flags
;
1541 struct ipmi_smi
*intf
= user
->intf
;
1542 struct ipmi_recv_msg
*msg
, *msg2
;
1543 struct list_head msgs
;
1546 user
= acquire_ipmi_user(user
, &index
);
1550 INIT_LIST_HEAD(&msgs
);
1552 spin_lock_irqsave(&intf
->events_lock
, flags
);
1553 if (user
->gets_events
== val
)
1556 user
->gets_events
= val
;
1559 if (atomic_inc_return(&intf
->event_waiters
) == 1)
1562 atomic_dec(&intf
->event_waiters
);
1565 if (intf
->delivering_events
)
1567 * Another thread is delivering events for this, so
1568 * let it handle any new events.
1572 /* Deliver any queued events. */
1573 while (user
->gets_events
&& !list_empty(&intf
->waiting_events
)) {
1574 list_for_each_entry_safe(msg
, msg2
, &intf
->waiting_events
, link
)
1575 list_move_tail(&msg
->link
, &msgs
);
1576 intf
->waiting_events_count
= 0;
1577 if (intf
->event_msg_printed
) {
1578 dev_warn(intf
->si_dev
, "Event queue no longer full\n");
1579 intf
->event_msg_printed
= 0;
1582 intf
->delivering_events
= 1;
1583 spin_unlock_irqrestore(&intf
->events_lock
, flags
);
1585 list_for_each_entry_safe(msg
, msg2
, &msgs
, link
) {
1587 kref_get(&user
->refcount
);
1588 deliver_local_response(intf
, msg
);
1591 spin_lock_irqsave(&intf
->events_lock
, flags
);
1592 intf
->delivering_events
= 0;
1596 spin_unlock_irqrestore(&intf
->events_lock
, flags
);
1597 release_ipmi_user(user
, index
);
1601 EXPORT_SYMBOL(ipmi_set_gets_events
);
1603 static struct cmd_rcvr
*find_cmd_rcvr(struct ipmi_smi
*intf
,
1604 unsigned char netfn
,
1608 struct cmd_rcvr
*rcvr
;
1610 list_for_each_entry_rcu(rcvr
, &intf
->cmd_rcvrs
, link
,
1611 lockdep_is_held(&intf
->cmd_rcvrs_mutex
)) {
1612 if ((rcvr
->netfn
== netfn
) && (rcvr
->cmd
== cmd
)
1613 && (rcvr
->chans
& (1 << chan
)))
1619 static int is_cmd_rcvr_exclusive(struct ipmi_smi
*intf
,
1620 unsigned char netfn
,
1624 struct cmd_rcvr
*rcvr
;
1626 list_for_each_entry_rcu(rcvr
, &intf
->cmd_rcvrs
, link
,
1627 lockdep_is_held(&intf
->cmd_rcvrs_mutex
)) {
1628 if ((rcvr
->netfn
== netfn
) && (rcvr
->cmd
== cmd
)
1629 && (rcvr
->chans
& chans
))
1635 int ipmi_register_for_cmd(struct ipmi_user
*user
,
1636 unsigned char netfn
,
1640 struct ipmi_smi
*intf
= user
->intf
;
1641 struct cmd_rcvr
*rcvr
;
1644 user
= acquire_ipmi_user(user
, &index
);
1648 rcvr
= kmalloc(sizeof(*rcvr
), GFP_KERNEL
);
1654 rcvr
->netfn
= netfn
;
1655 rcvr
->chans
= chans
;
1658 mutex_lock(&intf
->cmd_rcvrs_mutex
);
1659 /* Make sure the command/netfn is not already registered. */
1660 if (!is_cmd_rcvr_exclusive(intf
, netfn
, cmd
, chans
)) {
1665 smi_add_watch(intf
, IPMI_WATCH_MASK_CHECK_COMMANDS
);
1667 list_add_rcu(&rcvr
->link
, &intf
->cmd_rcvrs
);
1670 mutex_unlock(&intf
->cmd_rcvrs_mutex
);
1674 release_ipmi_user(user
, index
);
1678 EXPORT_SYMBOL(ipmi_register_for_cmd
);
1680 int ipmi_unregister_for_cmd(struct ipmi_user
*user
,
1681 unsigned char netfn
,
1685 struct ipmi_smi
*intf
= user
->intf
;
1686 struct cmd_rcvr
*rcvr
;
1687 struct cmd_rcvr
*rcvrs
= NULL
;
1688 int i
, rv
= -ENOENT
, index
;
1690 user
= acquire_ipmi_user(user
, &index
);
1694 mutex_lock(&intf
->cmd_rcvrs_mutex
);
1695 for (i
= 0; i
< IPMI_NUM_CHANNELS
; i
++) {
1696 if (((1 << i
) & chans
) == 0)
1698 rcvr
= find_cmd_rcvr(intf
, netfn
, cmd
, i
);
1701 if (rcvr
->user
== user
) {
1703 rcvr
->chans
&= ~chans
;
1704 if (rcvr
->chans
== 0) {
1705 list_del_rcu(&rcvr
->link
);
1711 mutex_unlock(&intf
->cmd_rcvrs_mutex
);
1713 release_ipmi_user(user
, index
);
1715 smi_remove_watch(intf
, IPMI_WATCH_MASK_CHECK_COMMANDS
);
1723 EXPORT_SYMBOL(ipmi_unregister_for_cmd
);
1725 static unsigned char
1726 ipmb_checksum(unsigned char *data
, int size
)
1728 unsigned char csum
= 0;
1730 for (; size
> 0; size
--, data
++)
1736 static inline void format_ipmb_msg(struct ipmi_smi_msg
*smi_msg
,
1737 struct kernel_ipmi_msg
*msg
,
1738 struct ipmi_ipmb_addr
*ipmb_addr
,
1740 unsigned char ipmb_seq
,
1742 unsigned char source_address
,
1743 unsigned char source_lun
)
1747 /* Format the IPMB header data. */
1748 smi_msg
->data
[0] = (IPMI_NETFN_APP_REQUEST
<< 2);
1749 smi_msg
->data
[1] = IPMI_SEND_MSG_CMD
;
1750 smi_msg
->data
[2] = ipmb_addr
->channel
;
1752 smi_msg
->data
[3] = 0;
1753 smi_msg
->data
[i
+3] = ipmb_addr
->slave_addr
;
1754 smi_msg
->data
[i
+4] = (msg
->netfn
<< 2) | (ipmb_addr
->lun
& 0x3);
1755 smi_msg
->data
[i
+5] = ipmb_checksum(&smi_msg
->data
[i
+ 3], 2);
1756 smi_msg
->data
[i
+6] = source_address
;
1757 smi_msg
->data
[i
+7] = (ipmb_seq
<< 2) | source_lun
;
1758 smi_msg
->data
[i
+8] = msg
->cmd
;
1760 /* Now tack on the data to the message. */
1761 if (msg
->data_len
> 0)
1762 memcpy(&smi_msg
->data
[i
+ 9], msg
->data
, msg
->data_len
);
1763 smi_msg
->data_size
= msg
->data_len
+ 9;
1765 /* Now calculate the checksum and tack it on. */
1766 smi_msg
->data
[i
+smi_msg
->data_size
]
1767 = ipmb_checksum(&smi_msg
->data
[i
+ 6], smi_msg
->data_size
- 6);
1770 * Add on the checksum size and the offset from the
1773 smi_msg
->data_size
+= 1 + i
;
1775 smi_msg
->msgid
= msgid
;
1778 static inline void format_lan_msg(struct ipmi_smi_msg
*smi_msg
,
1779 struct kernel_ipmi_msg
*msg
,
1780 struct ipmi_lan_addr
*lan_addr
,
1782 unsigned char ipmb_seq
,
1783 unsigned char source_lun
)
1785 /* Format the IPMB header data. */
1786 smi_msg
->data
[0] = (IPMI_NETFN_APP_REQUEST
<< 2);
1787 smi_msg
->data
[1] = IPMI_SEND_MSG_CMD
;
1788 smi_msg
->data
[2] = lan_addr
->channel
;
1789 smi_msg
->data
[3] = lan_addr
->session_handle
;
1790 smi_msg
->data
[4] = lan_addr
->remote_SWID
;
1791 smi_msg
->data
[5] = (msg
->netfn
<< 2) | (lan_addr
->lun
& 0x3);
1792 smi_msg
->data
[6] = ipmb_checksum(&smi_msg
->data
[4], 2);
1793 smi_msg
->data
[7] = lan_addr
->local_SWID
;
1794 smi_msg
->data
[8] = (ipmb_seq
<< 2) | source_lun
;
1795 smi_msg
->data
[9] = msg
->cmd
;
1797 /* Now tack on the data to the message. */
1798 if (msg
->data_len
> 0)
1799 memcpy(&smi_msg
->data
[10], msg
->data
, msg
->data_len
);
1800 smi_msg
->data_size
= msg
->data_len
+ 10;
1802 /* Now calculate the checksum and tack it on. */
1803 smi_msg
->data
[smi_msg
->data_size
]
1804 = ipmb_checksum(&smi_msg
->data
[7], smi_msg
->data_size
- 7);
1807 * Add on the checksum size and the offset from the
1810 smi_msg
->data_size
+= 1;
1812 smi_msg
->msgid
= msgid
;
1815 static struct ipmi_smi_msg
*smi_add_send_msg(struct ipmi_smi
*intf
,
1816 struct ipmi_smi_msg
*smi_msg
,
1819 if (intf
->curr_msg
) {
1821 list_add_tail(&smi_msg
->link
, &intf
->hp_xmit_msgs
);
1823 list_add_tail(&smi_msg
->link
, &intf
->xmit_msgs
);
1826 intf
->curr_msg
= smi_msg
;
1832 static void smi_send(struct ipmi_smi
*intf
,
1833 const struct ipmi_smi_handlers
*handlers
,
1834 struct ipmi_smi_msg
*smi_msg
, int priority
)
1836 int run_to_completion
= intf
->run_to_completion
;
1837 unsigned long flags
= 0;
1839 if (!run_to_completion
)
1840 spin_lock_irqsave(&intf
->xmit_msgs_lock
, flags
);
1841 smi_msg
= smi_add_send_msg(intf
, smi_msg
, priority
);
1843 if (!run_to_completion
)
1844 spin_unlock_irqrestore(&intf
->xmit_msgs_lock
, flags
);
1847 handlers
->sender(intf
->send_info
, smi_msg
);
1850 static bool is_maintenance_mode_cmd(struct kernel_ipmi_msg
*msg
)
1852 return (((msg
->netfn
== IPMI_NETFN_APP_REQUEST
)
1853 && ((msg
->cmd
== IPMI_COLD_RESET_CMD
)
1854 || (msg
->cmd
== IPMI_WARM_RESET_CMD
)))
1855 || (msg
->netfn
== IPMI_NETFN_FIRMWARE_REQUEST
));
1858 static int i_ipmi_req_sysintf(struct ipmi_smi
*intf
,
1859 struct ipmi_addr
*addr
,
1861 struct kernel_ipmi_msg
*msg
,
1862 struct ipmi_smi_msg
*smi_msg
,
1863 struct ipmi_recv_msg
*recv_msg
,
1865 unsigned int retry_time_ms
)
1867 struct ipmi_system_interface_addr
*smi_addr
;
1870 /* Responses are not allowed to the SMI. */
1873 smi_addr
= (struct ipmi_system_interface_addr
*) addr
;
1874 if (smi_addr
->lun
> 3) {
1875 ipmi_inc_stat(intf
, sent_invalid_commands
);
1879 memcpy(&recv_msg
->addr
, smi_addr
, sizeof(*smi_addr
));
1881 if ((msg
->netfn
== IPMI_NETFN_APP_REQUEST
)
1882 && ((msg
->cmd
== IPMI_SEND_MSG_CMD
)
1883 || (msg
->cmd
== IPMI_GET_MSG_CMD
)
1884 || (msg
->cmd
== IPMI_READ_EVENT_MSG_BUFFER_CMD
))) {
1886 * We don't let the user do these, since we manage
1887 * the sequence numbers.
1889 ipmi_inc_stat(intf
, sent_invalid_commands
);
1893 if (is_maintenance_mode_cmd(msg
)) {
1894 unsigned long flags
;
1896 spin_lock_irqsave(&intf
->maintenance_mode_lock
, flags
);
1897 intf
->auto_maintenance_timeout
1898 = maintenance_mode_timeout_ms
;
1899 if (!intf
->maintenance_mode
1900 && !intf
->maintenance_mode_enable
) {
1901 intf
->maintenance_mode_enable
= true;
1902 maintenance_mode_update(intf
);
1904 spin_unlock_irqrestore(&intf
->maintenance_mode_lock
,
1908 if (msg
->data_len
+ 2 > IPMI_MAX_MSG_LENGTH
) {
1909 ipmi_inc_stat(intf
, sent_invalid_commands
);
1913 smi_msg
->data
[0] = (msg
->netfn
<< 2) | (smi_addr
->lun
& 0x3);
1914 smi_msg
->data
[1] = msg
->cmd
;
1915 smi_msg
->msgid
= msgid
;
1916 smi_msg
->user_data
= recv_msg
;
1917 if (msg
->data_len
> 0)
1918 memcpy(&smi_msg
->data
[2], msg
->data
, msg
->data_len
);
1919 smi_msg
->data_size
= msg
->data_len
+ 2;
1920 ipmi_inc_stat(intf
, sent_local_commands
);
1925 static int i_ipmi_req_ipmb(struct ipmi_smi
*intf
,
1926 struct ipmi_addr
*addr
,
1928 struct kernel_ipmi_msg
*msg
,
1929 struct ipmi_smi_msg
*smi_msg
,
1930 struct ipmi_recv_msg
*recv_msg
,
1931 unsigned char source_address
,
1932 unsigned char source_lun
,
1934 unsigned int retry_time_ms
)
1936 struct ipmi_ipmb_addr
*ipmb_addr
;
1937 unsigned char ipmb_seq
;
1940 struct ipmi_channel
*chans
;
1943 if (addr
->channel
>= IPMI_MAX_CHANNELS
) {
1944 ipmi_inc_stat(intf
, sent_invalid_commands
);
1948 chans
= READ_ONCE(intf
->channel_list
)->c
;
1950 if (chans
[addr
->channel
].medium
!= IPMI_CHANNEL_MEDIUM_IPMB
) {
1951 ipmi_inc_stat(intf
, sent_invalid_commands
);
1955 if (addr
->addr_type
== IPMI_IPMB_BROADCAST_ADDR_TYPE
) {
1957 * Broadcasts add a zero at the beginning of the
1958 * message, but otherwise is the same as an IPMB
1961 addr
->addr_type
= IPMI_IPMB_ADDR_TYPE
;
1963 retries
= 0; /* Don't retry broadcasts. */
1967 * 9 for the header and 1 for the checksum, plus
1968 * possibly one for the broadcast.
1970 if ((msg
->data_len
+ 10 + broadcast
) > IPMI_MAX_MSG_LENGTH
) {
1971 ipmi_inc_stat(intf
, sent_invalid_commands
);
1975 ipmb_addr
= (struct ipmi_ipmb_addr
*) addr
;
1976 if (ipmb_addr
->lun
> 3) {
1977 ipmi_inc_stat(intf
, sent_invalid_commands
);
1981 memcpy(&recv_msg
->addr
, ipmb_addr
, sizeof(*ipmb_addr
));
1983 if (recv_msg
->msg
.netfn
& 0x1) {
1985 * It's a response, so use the user's sequence
1988 ipmi_inc_stat(intf
, sent_ipmb_responses
);
1989 format_ipmb_msg(smi_msg
, msg
, ipmb_addr
, msgid
,
1991 source_address
, source_lun
);
1994 * Save the receive message so we can use it
1995 * to deliver the response.
1997 smi_msg
->user_data
= recv_msg
;
1999 /* It's a command, so get a sequence for it. */
2000 unsigned long flags
;
2002 spin_lock_irqsave(&intf
->seq_lock
, flags
);
2004 if (is_maintenance_mode_cmd(msg
))
2005 intf
->ipmb_maintenance_mode_timeout
=
2006 maintenance_mode_timeout_ms
;
2008 if (intf
->ipmb_maintenance_mode_timeout
&& retry_time_ms
== 0)
2009 /* Different default in maintenance mode */
2010 retry_time_ms
= default_maintenance_retry_ms
;
2013 * Create a sequence number with a 1 second
2014 * timeout and 4 retries.
2016 rv
= intf_next_seq(intf
,
2025 * We have used up all the sequence numbers,
2026 * probably, so abort.
2030 ipmi_inc_stat(intf
, sent_ipmb_commands
);
2033 * Store the sequence number in the message,
2034 * so that when the send message response
2035 * comes back we can start the timer.
2037 format_ipmb_msg(smi_msg
, msg
, ipmb_addr
,
2038 STORE_SEQ_IN_MSGID(ipmb_seq
, seqid
),
2039 ipmb_seq
, broadcast
,
2040 source_address
, source_lun
);
2043 * Copy the message into the recv message data, so we
2044 * can retransmit it later if necessary.
2046 memcpy(recv_msg
->msg_data
, smi_msg
->data
,
2047 smi_msg
->data_size
);
2048 recv_msg
->msg
.data
= recv_msg
->msg_data
;
2049 recv_msg
->msg
.data_len
= smi_msg
->data_size
;
2052 * We don't unlock until here, because we need
2053 * to copy the completed message into the
2054 * recv_msg before we release the lock.
2055 * Otherwise, race conditions may bite us. I
2056 * know that's pretty paranoid, but I prefer
2060 spin_unlock_irqrestore(&intf
->seq_lock
, flags
);
2066 static int i_ipmi_req_lan(struct ipmi_smi
*intf
,
2067 struct ipmi_addr
*addr
,
2069 struct kernel_ipmi_msg
*msg
,
2070 struct ipmi_smi_msg
*smi_msg
,
2071 struct ipmi_recv_msg
*recv_msg
,
2072 unsigned char source_lun
,
2074 unsigned int retry_time_ms
)
2076 struct ipmi_lan_addr
*lan_addr
;
2077 unsigned char ipmb_seq
;
2079 struct ipmi_channel
*chans
;
2082 if (addr
->channel
>= IPMI_MAX_CHANNELS
) {
2083 ipmi_inc_stat(intf
, sent_invalid_commands
);
2087 chans
= READ_ONCE(intf
->channel_list
)->c
;
2089 if ((chans
[addr
->channel
].medium
2090 != IPMI_CHANNEL_MEDIUM_8023LAN
)
2091 && (chans
[addr
->channel
].medium
2092 != IPMI_CHANNEL_MEDIUM_ASYNC
)) {
2093 ipmi_inc_stat(intf
, sent_invalid_commands
);
2097 /* 11 for the header and 1 for the checksum. */
2098 if ((msg
->data_len
+ 12) > IPMI_MAX_MSG_LENGTH
) {
2099 ipmi_inc_stat(intf
, sent_invalid_commands
);
2103 lan_addr
= (struct ipmi_lan_addr
*) addr
;
2104 if (lan_addr
->lun
> 3) {
2105 ipmi_inc_stat(intf
, sent_invalid_commands
);
2109 memcpy(&recv_msg
->addr
, lan_addr
, sizeof(*lan_addr
));
2111 if (recv_msg
->msg
.netfn
& 0x1) {
2113 * It's a response, so use the user's sequence
2116 ipmi_inc_stat(intf
, sent_lan_responses
);
2117 format_lan_msg(smi_msg
, msg
, lan_addr
, msgid
,
2121 * Save the receive message so we can use it
2122 * to deliver the response.
2124 smi_msg
->user_data
= recv_msg
;
2126 /* It's a command, so get a sequence for it. */
2127 unsigned long flags
;
2129 spin_lock_irqsave(&intf
->seq_lock
, flags
);
2132 * Create a sequence number with a 1 second
2133 * timeout and 4 retries.
2135 rv
= intf_next_seq(intf
,
2144 * We have used up all the sequence numbers,
2145 * probably, so abort.
2149 ipmi_inc_stat(intf
, sent_lan_commands
);
2152 * Store the sequence number in the message,
2153 * so that when the send message response
2154 * comes back we can start the timer.
2156 format_lan_msg(smi_msg
, msg
, lan_addr
,
2157 STORE_SEQ_IN_MSGID(ipmb_seq
, seqid
),
2158 ipmb_seq
, source_lun
);
2161 * Copy the message into the recv message data, so we
2162 * can retransmit it later if necessary.
2164 memcpy(recv_msg
->msg_data
, smi_msg
->data
,
2165 smi_msg
->data_size
);
2166 recv_msg
->msg
.data
= recv_msg
->msg_data
;
2167 recv_msg
->msg
.data_len
= smi_msg
->data_size
;
2170 * We don't unlock until here, because we need
2171 * to copy the completed message into the
2172 * recv_msg before we release the lock.
2173 * Otherwise, race conditions may bite us. I
2174 * know that's pretty paranoid, but I prefer
2178 spin_unlock_irqrestore(&intf
->seq_lock
, flags
);
2185 * Separate from ipmi_request so that the user does not have to be
2186 * supplied in certain circumstances (mainly at panic time). If
2187 * messages are supplied, they will be freed, even if an error
2190 static int i_ipmi_request(struct ipmi_user
*user
,
2191 struct ipmi_smi
*intf
,
2192 struct ipmi_addr
*addr
,
2194 struct kernel_ipmi_msg
*msg
,
2195 void *user_msg_data
,
2197 struct ipmi_recv_msg
*supplied_recv
,
2199 unsigned char source_address
,
2200 unsigned char source_lun
,
2202 unsigned int retry_time_ms
)
2204 struct ipmi_smi_msg
*smi_msg
;
2205 struct ipmi_recv_msg
*recv_msg
;
2209 recv_msg
= supplied_recv
;
2211 recv_msg
= ipmi_alloc_recv_msg();
2212 if (recv_msg
== NULL
) {
2217 recv_msg
->user_msg_data
= user_msg_data
;
2220 smi_msg
= (struct ipmi_smi_msg
*) supplied_smi
;
2222 smi_msg
= ipmi_alloc_smi_msg();
2223 if (smi_msg
== NULL
) {
2225 ipmi_free_recv_msg(recv_msg
);
2232 if (intf
->in_shutdown
) {
2237 recv_msg
->user
= user
;
2239 /* The put happens when the message is freed. */
2240 kref_get(&user
->refcount
);
2241 recv_msg
->msgid
= msgid
;
2243 * Store the message to send in the receive message so timeout
2244 * responses can get the proper response data.
2246 recv_msg
->msg
= *msg
;
2248 if (addr
->addr_type
== IPMI_SYSTEM_INTERFACE_ADDR_TYPE
) {
2249 rv
= i_ipmi_req_sysintf(intf
, addr
, msgid
, msg
, smi_msg
,
2250 recv_msg
, retries
, retry_time_ms
);
2251 } else if (is_ipmb_addr(addr
) || is_ipmb_bcast_addr(addr
)) {
2252 rv
= i_ipmi_req_ipmb(intf
, addr
, msgid
, msg
, smi_msg
, recv_msg
,
2253 source_address
, source_lun
,
2254 retries
, retry_time_ms
);
2255 } else if (is_lan_addr(addr
)) {
2256 rv
= i_ipmi_req_lan(intf
, addr
, msgid
, msg
, smi_msg
, recv_msg
,
2257 source_lun
, retries
, retry_time_ms
);
2259 /* Unknown address type. */
2260 ipmi_inc_stat(intf
, sent_invalid_commands
);
2266 ipmi_free_smi_msg(smi_msg
);
2267 ipmi_free_recv_msg(recv_msg
);
2269 pr_debug("Send: %*ph\n", smi_msg
->data_size
, smi_msg
->data
);
2271 smi_send(intf
, intf
->handlers
, smi_msg
, priority
);
2279 static int check_addr(struct ipmi_smi
*intf
,
2280 struct ipmi_addr
*addr
,
2281 unsigned char *saddr
,
2284 if (addr
->channel
>= IPMI_MAX_CHANNELS
)
2286 addr
->channel
= array_index_nospec(addr
->channel
, IPMI_MAX_CHANNELS
);
2287 *lun
= intf
->addrinfo
[addr
->channel
].lun
;
2288 *saddr
= intf
->addrinfo
[addr
->channel
].address
;
2292 int ipmi_request_settime(struct ipmi_user
*user
,
2293 struct ipmi_addr
*addr
,
2295 struct kernel_ipmi_msg
*msg
,
2296 void *user_msg_data
,
2299 unsigned int retry_time_ms
)
2301 unsigned char saddr
= 0, lun
= 0;
2307 user
= acquire_ipmi_user(user
, &index
);
2311 rv
= check_addr(user
->intf
, addr
, &saddr
, &lun
);
2313 rv
= i_ipmi_request(user
,
2326 release_ipmi_user(user
, index
);
2329 EXPORT_SYMBOL(ipmi_request_settime
);
2331 int ipmi_request_supply_msgs(struct ipmi_user
*user
,
2332 struct ipmi_addr
*addr
,
2334 struct kernel_ipmi_msg
*msg
,
2335 void *user_msg_data
,
2337 struct ipmi_recv_msg
*supplied_recv
,
2340 unsigned char saddr
= 0, lun
= 0;
2346 user
= acquire_ipmi_user(user
, &index
);
2350 rv
= check_addr(user
->intf
, addr
, &saddr
, &lun
);
2352 rv
= i_ipmi_request(user
,
2365 release_ipmi_user(user
, index
);
2368 EXPORT_SYMBOL(ipmi_request_supply_msgs
);
2370 static void bmc_device_id_handler(struct ipmi_smi
*intf
,
2371 struct ipmi_recv_msg
*msg
)
2375 if ((msg
->addr
.addr_type
!= IPMI_SYSTEM_INTERFACE_ADDR_TYPE
)
2376 || (msg
->msg
.netfn
!= IPMI_NETFN_APP_RESPONSE
)
2377 || (msg
->msg
.cmd
!= IPMI_GET_DEVICE_ID_CMD
)) {
2378 dev_warn(intf
->si_dev
,
2379 "invalid device_id msg: addr_type=%d netfn=%x cmd=%x\n",
2380 msg
->addr
.addr_type
, msg
->msg
.netfn
, msg
->msg
.cmd
);
2384 rv
= ipmi_demangle_device_id(msg
->msg
.netfn
, msg
->msg
.cmd
,
2385 msg
->msg
.data
, msg
->msg
.data_len
, &intf
->bmc
->fetch_id
);
2387 dev_warn(intf
->si_dev
, "device id demangle failed: %d\n", rv
);
2388 /* record completion code when error */
2389 intf
->bmc
->cc
= msg
->msg
.data
[0];
2390 intf
->bmc
->dyn_id_set
= 0;
2393 * Make sure the id data is available before setting
2397 intf
->bmc
->dyn_id_set
= 1;
2400 wake_up(&intf
->waitq
);
2404 send_get_device_id_cmd(struct ipmi_smi
*intf
)
2406 struct ipmi_system_interface_addr si
;
2407 struct kernel_ipmi_msg msg
;
2409 si
.addr_type
= IPMI_SYSTEM_INTERFACE_ADDR_TYPE
;
2410 si
.channel
= IPMI_BMC_CHANNEL
;
2413 msg
.netfn
= IPMI_NETFN_APP_REQUEST
;
2414 msg
.cmd
= IPMI_GET_DEVICE_ID_CMD
;
2418 return i_ipmi_request(NULL
,
2420 (struct ipmi_addr
*) &si
,
2427 intf
->addrinfo
[0].address
,
2428 intf
->addrinfo
[0].lun
,
2432 static int __get_device_id(struct ipmi_smi
*intf
, struct bmc_device
*bmc
)
2435 unsigned int retry_count
= 0;
2437 intf
->null_user_handler
= bmc_device_id_handler
;
2441 bmc
->dyn_id_set
= 2;
2443 rv
= send_get_device_id_cmd(intf
);
2445 goto out_reset_handler
;
2447 wait_event(intf
->waitq
, bmc
->dyn_id_set
!= 2);
2449 if (!bmc
->dyn_id_set
) {
2450 if ((bmc
->cc
== IPMI_DEVICE_IN_FW_UPDATE_ERR
2451 || bmc
->cc
== IPMI_DEVICE_IN_INIT_ERR
2452 || bmc
->cc
== IPMI_NOT_IN_MY_STATE_ERR
)
2453 && ++retry_count
<= GET_DEVICE_ID_MAX_RETRY
) {
2455 dev_warn(intf
->si_dev
,
2456 "BMC returned 0x%2.2x, retry get bmc device id\n",
2461 rv
= -EIO
; /* Something went wrong in the fetch. */
2464 /* dyn_id_set makes the id data available. */
2468 intf
->null_user_handler
= NULL
;
2474 * Fetch the device id for the bmc/interface. You must pass in either
2475 * bmc or intf, this code will get the other one. If the data has
2476 * been recently fetched, this will just use the cached data. Otherwise
2477 * it will run a new fetch.
2479 * Except for the first time this is called (in ipmi_add_smi()),
2480 * this will always return good data;
2482 static int __bmc_get_device_id(struct ipmi_smi
*intf
, struct bmc_device
*bmc
,
2483 struct ipmi_device_id
*id
,
2484 bool *guid_set
, guid_t
*guid
, int intf_num
)
2487 int prev_dyn_id_set
, prev_guid_set
;
2488 bool intf_set
= intf
!= NULL
;
2491 mutex_lock(&bmc
->dyn_mutex
);
2493 if (list_empty(&bmc
->intfs
)) {
2494 mutex_unlock(&bmc
->dyn_mutex
);
2497 intf
= list_first_entry(&bmc
->intfs
, struct ipmi_smi
,
2499 kref_get(&intf
->refcount
);
2500 mutex_unlock(&bmc
->dyn_mutex
);
2501 mutex_lock(&intf
->bmc_reg_mutex
);
2502 mutex_lock(&bmc
->dyn_mutex
);
2503 if (intf
!= list_first_entry(&bmc
->intfs
, struct ipmi_smi
,
2505 mutex_unlock(&intf
->bmc_reg_mutex
);
2506 kref_put(&intf
->refcount
, intf_free
);
2507 goto retry_bmc_lock
;
2510 mutex_lock(&intf
->bmc_reg_mutex
);
2512 mutex_lock(&bmc
->dyn_mutex
);
2513 kref_get(&intf
->refcount
);
2516 /* If we have a valid and current ID, just return that. */
2517 if (intf
->in_bmc_register
||
2518 (bmc
->dyn_id_set
&& time_is_after_jiffies(bmc
->dyn_id_expiry
)))
2519 goto out_noprocessing
;
2521 prev_guid_set
= bmc
->dyn_guid_set
;
2524 prev_dyn_id_set
= bmc
->dyn_id_set
;
2525 rv
= __get_device_id(intf
, bmc
);
2530 * The guid, device id, manufacturer id, and product id should
2531 * not change on a BMC. If it does we have to do some dancing.
2533 if (!intf
->bmc_registered
2534 || (!prev_guid_set
&& bmc
->dyn_guid_set
)
2535 || (!prev_dyn_id_set
&& bmc
->dyn_id_set
)
2536 || (prev_guid_set
&& bmc
->dyn_guid_set
2537 && !guid_equal(&bmc
->guid
, &bmc
->fetch_guid
))
2538 || bmc
->id
.device_id
!= bmc
->fetch_id
.device_id
2539 || bmc
->id
.manufacturer_id
!= bmc
->fetch_id
.manufacturer_id
2540 || bmc
->id
.product_id
!= bmc
->fetch_id
.product_id
) {
2541 struct ipmi_device_id id
= bmc
->fetch_id
;
2542 int guid_set
= bmc
->dyn_guid_set
;
2545 guid
= bmc
->fetch_guid
;
2546 mutex_unlock(&bmc
->dyn_mutex
);
2548 __ipmi_bmc_unregister(intf
);
2549 /* Fill in the temporary BMC for good measure. */
2551 intf
->bmc
->dyn_guid_set
= guid_set
;
2552 intf
->bmc
->guid
= guid
;
2553 if (__ipmi_bmc_register(intf
, &id
, guid_set
, &guid
, intf_num
))
2554 need_waiter(intf
); /* Retry later on an error. */
2556 __scan_channels(intf
, &id
);
2561 * We weren't given the interface on the
2562 * command line, so restart the operation on
2563 * the next interface for the BMC.
2565 mutex_unlock(&intf
->bmc_reg_mutex
);
2566 mutex_lock(&bmc
->dyn_mutex
);
2567 goto retry_bmc_lock
;
2570 /* We have a new BMC, set it up. */
2572 mutex_lock(&bmc
->dyn_mutex
);
2573 goto out_noprocessing
;
2574 } else if (memcmp(&bmc
->fetch_id
, &bmc
->id
, sizeof(bmc
->id
)))
2575 /* Version info changes, scan the channels again. */
2576 __scan_channels(intf
, &bmc
->fetch_id
);
2578 bmc
->dyn_id_expiry
= jiffies
+ IPMI_DYN_DEV_ID_EXPIRY
;
2581 if (rv
&& prev_dyn_id_set
) {
2582 rv
= 0; /* Ignore failures if we have previous data. */
2583 bmc
->dyn_id_set
= prev_dyn_id_set
;
2586 bmc
->id
= bmc
->fetch_id
;
2587 if (bmc
->dyn_guid_set
)
2588 bmc
->guid
= bmc
->fetch_guid
;
2589 else if (prev_guid_set
)
2591 * The guid used to be valid and it failed to fetch,
2592 * just use the cached value.
2594 bmc
->dyn_guid_set
= prev_guid_set
;
2602 *guid_set
= bmc
->dyn_guid_set
;
2604 if (guid
&& bmc
->dyn_guid_set
)
2608 mutex_unlock(&bmc
->dyn_mutex
);
2609 mutex_unlock(&intf
->bmc_reg_mutex
);
2611 kref_put(&intf
->refcount
, intf_free
);
2615 static int bmc_get_device_id(struct ipmi_smi
*intf
, struct bmc_device
*bmc
,
2616 struct ipmi_device_id
*id
,
2617 bool *guid_set
, guid_t
*guid
)
2619 return __bmc_get_device_id(intf
, bmc
, id
, guid_set
, guid
, -1);
2622 static ssize_t
device_id_show(struct device
*dev
,
2623 struct device_attribute
*attr
,
2626 struct bmc_device
*bmc
= to_bmc_device(dev
);
2627 struct ipmi_device_id id
;
2630 rv
= bmc_get_device_id(NULL
, bmc
, &id
, NULL
, NULL
);
2634 return snprintf(buf
, 10, "%u\n", id
.device_id
);
2636 static DEVICE_ATTR_RO(device_id
);
2638 static ssize_t
provides_device_sdrs_show(struct device
*dev
,
2639 struct device_attribute
*attr
,
2642 struct bmc_device
*bmc
= to_bmc_device(dev
);
2643 struct ipmi_device_id id
;
2646 rv
= bmc_get_device_id(NULL
, bmc
, &id
, NULL
, NULL
);
2650 return snprintf(buf
, 10, "%u\n", (id
.device_revision
& 0x80) >> 7);
2652 static DEVICE_ATTR_RO(provides_device_sdrs
);
2654 static ssize_t
revision_show(struct device
*dev
, struct device_attribute
*attr
,
2657 struct bmc_device
*bmc
= to_bmc_device(dev
);
2658 struct ipmi_device_id id
;
2661 rv
= bmc_get_device_id(NULL
, bmc
, &id
, NULL
, NULL
);
2665 return snprintf(buf
, 20, "%u\n", id
.device_revision
& 0x0F);
2667 static DEVICE_ATTR_RO(revision
);
2669 static ssize_t
firmware_revision_show(struct device
*dev
,
2670 struct device_attribute
*attr
,
2673 struct bmc_device
*bmc
= to_bmc_device(dev
);
2674 struct ipmi_device_id id
;
2677 rv
= bmc_get_device_id(NULL
, bmc
, &id
, NULL
, NULL
);
2681 return snprintf(buf
, 20, "%u.%x\n", id
.firmware_revision_1
,
2682 id
.firmware_revision_2
);
2684 static DEVICE_ATTR_RO(firmware_revision
);
2686 static ssize_t
ipmi_version_show(struct device
*dev
,
2687 struct device_attribute
*attr
,
2690 struct bmc_device
*bmc
= to_bmc_device(dev
);
2691 struct ipmi_device_id id
;
2694 rv
= bmc_get_device_id(NULL
, bmc
, &id
, NULL
, NULL
);
2698 return snprintf(buf
, 20, "%u.%u\n",
2699 ipmi_version_major(&id
),
2700 ipmi_version_minor(&id
));
2702 static DEVICE_ATTR_RO(ipmi_version
);
2704 static ssize_t
add_dev_support_show(struct device
*dev
,
2705 struct device_attribute
*attr
,
2708 struct bmc_device
*bmc
= to_bmc_device(dev
);
2709 struct ipmi_device_id id
;
2712 rv
= bmc_get_device_id(NULL
, bmc
, &id
, NULL
, NULL
);
2716 return snprintf(buf
, 10, "0x%02x\n", id
.additional_device_support
);
2718 static DEVICE_ATTR(additional_device_support
, S_IRUGO
, add_dev_support_show
,
2721 static ssize_t
manufacturer_id_show(struct device
*dev
,
2722 struct device_attribute
*attr
,
2725 struct bmc_device
*bmc
= to_bmc_device(dev
);
2726 struct ipmi_device_id id
;
2729 rv
= bmc_get_device_id(NULL
, bmc
, &id
, NULL
, NULL
);
2733 return snprintf(buf
, 20, "0x%6.6x\n", id
.manufacturer_id
);
2735 static DEVICE_ATTR_RO(manufacturer_id
);
2737 static ssize_t
product_id_show(struct device
*dev
,
2738 struct device_attribute
*attr
,
2741 struct bmc_device
*bmc
= to_bmc_device(dev
);
2742 struct ipmi_device_id id
;
2745 rv
= bmc_get_device_id(NULL
, bmc
, &id
, NULL
, NULL
);
2749 return snprintf(buf
, 10, "0x%4.4x\n", id
.product_id
);
2751 static DEVICE_ATTR_RO(product_id
);
2753 static ssize_t
aux_firmware_rev_show(struct device
*dev
,
2754 struct device_attribute
*attr
,
2757 struct bmc_device
*bmc
= to_bmc_device(dev
);
2758 struct ipmi_device_id id
;
2761 rv
= bmc_get_device_id(NULL
, bmc
, &id
, NULL
, NULL
);
2765 return snprintf(buf
, 21, "0x%02x 0x%02x 0x%02x 0x%02x\n",
2766 id
.aux_firmware_revision
[3],
2767 id
.aux_firmware_revision
[2],
2768 id
.aux_firmware_revision
[1],
2769 id
.aux_firmware_revision
[0]);
2771 static DEVICE_ATTR(aux_firmware_revision
, S_IRUGO
, aux_firmware_rev_show
, NULL
);
2773 static ssize_t
guid_show(struct device
*dev
, struct device_attribute
*attr
,
2776 struct bmc_device
*bmc
= to_bmc_device(dev
);
2781 rv
= bmc_get_device_id(NULL
, bmc
, NULL
, &guid_set
, &guid
);
2787 return snprintf(buf
, UUID_STRING_LEN
+ 1 + 1, "%pUl\n", &guid
);
2789 static DEVICE_ATTR_RO(guid
);
2791 static struct attribute
*bmc_dev_attrs
[] = {
2792 &dev_attr_device_id
.attr
,
2793 &dev_attr_provides_device_sdrs
.attr
,
2794 &dev_attr_revision
.attr
,
2795 &dev_attr_firmware_revision
.attr
,
2796 &dev_attr_ipmi_version
.attr
,
2797 &dev_attr_additional_device_support
.attr
,
2798 &dev_attr_manufacturer_id
.attr
,
2799 &dev_attr_product_id
.attr
,
2800 &dev_attr_aux_firmware_revision
.attr
,
2801 &dev_attr_guid
.attr
,
2805 static umode_t
bmc_dev_attr_is_visible(struct kobject
*kobj
,
2806 struct attribute
*attr
, int idx
)
2808 struct device
*dev
= kobj_to_dev(kobj
);
2809 struct bmc_device
*bmc
= to_bmc_device(dev
);
2810 umode_t mode
= attr
->mode
;
2813 if (attr
== &dev_attr_aux_firmware_revision
.attr
) {
2814 struct ipmi_device_id id
;
2816 rv
= bmc_get_device_id(NULL
, bmc
, &id
, NULL
, NULL
);
2817 return (!rv
&& id
.aux_firmware_revision_set
) ? mode
: 0;
2819 if (attr
== &dev_attr_guid
.attr
) {
2822 rv
= bmc_get_device_id(NULL
, bmc
, NULL
, &guid_set
, NULL
);
2823 return (!rv
&& guid_set
) ? mode
: 0;
2828 static const struct attribute_group bmc_dev_attr_group
= {
2829 .attrs
= bmc_dev_attrs
,
2830 .is_visible
= bmc_dev_attr_is_visible
,
2833 static const struct attribute_group
*bmc_dev_attr_groups
[] = {
2834 &bmc_dev_attr_group
,
2838 static const struct device_type bmc_device_type
= {
2839 .groups
= bmc_dev_attr_groups
,
2842 static int __find_bmc_guid(struct device
*dev
, const void *data
)
2844 const guid_t
*guid
= data
;
2845 struct bmc_device
*bmc
;
2848 if (dev
->type
!= &bmc_device_type
)
2851 bmc
= to_bmc_device(dev
);
2852 rv
= bmc
->dyn_guid_set
&& guid_equal(&bmc
->guid
, guid
);
2854 rv
= kref_get_unless_zero(&bmc
->usecount
);
2859 * Returns with the bmc's usecount incremented, if it is non-NULL.
2861 static struct bmc_device
*ipmi_find_bmc_guid(struct device_driver
*drv
,
2865 struct bmc_device
*bmc
= NULL
;
2867 dev
= driver_find_device(drv
, NULL
, guid
, __find_bmc_guid
);
2869 bmc
= to_bmc_device(dev
);
2875 struct prod_dev_id
{
2876 unsigned int product_id
;
2877 unsigned char device_id
;
2880 static int __find_bmc_prod_dev_id(struct device
*dev
, const void *data
)
2882 const struct prod_dev_id
*cid
= data
;
2883 struct bmc_device
*bmc
;
2886 if (dev
->type
!= &bmc_device_type
)
2889 bmc
= to_bmc_device(dev
);
2890 rv
= (bmc
->id
.product_id
== cid
->product_id
2891 && bmc
->id
.device_id
== cid
->device_id
);
2893 rv
= kref_get_unless_zero(&bmc
->usecount
);
2898 * Returns with the bmc's usecount incremented, if it is non-NULL.
2900 static struct bmc_device
*ipmi_find_bmc_prod_dev_id(
2901 struct device_driver
*drv
,
2902 unsigned int product_id
, unsigned char device_id
)
2904 struct prod_dev_id id
= {
2905 .product_id
= product_id
,
2906 .device_id
= device_id
,
2909 struct bmc_device
*bmc
= NULL
;
2911 dev
= driver_find_device(drv
, NULL
, &id
, __find_bmc_prod_dev_id
);
2913 bmc
= to_bmc_device(dev
);
2919 static DEFINE_IDA(ipmi_bmc_ida
);
2922 release_bmc_device(struct device
*dev
)
2924 kfree(to_bmc_device(dev
));
2927 static void cleanup_bmc_work(struct work_struct
*work
)
2929 struct bmc_device
*bmc
= container_of(work
, struct bmc_device
,
2931 int id
= bmc
->pdev
.id
; /* Unregister overwrites id */
2933 platform_device_unregister(&bmc
->pdev
);
2934 ida_simple_remove(&ipmi_bmc_ida
, id
);
2938 cleanup_bmc_device(struct kref
*ref
)
2940 struct bmc_device
*bmc
= container_of(ref
, struct bmc_device
, usecount
);
2943 * Remove the platform device in a work queue to avoid issues
2944 * with removing the device attributes while reading a device
2947 schedule_work(&bmc
->remove_work
);
2951 * Must be called with intf->bmc_reg_mutex held.
2953 static void __ipmi_bmc_unregister(struct ipmi_smi
*intf
)
2955 struct bmc_device
*bmc
= intf
->bmc
;
2957 if (!intf
->bmc_registered
)
2960 sysfs_remove_link(&intf
->si_dev
->kobj
, "bmc");
2961 sysfs_remove_link(&bmc
->pdev
.dev
.kobj
, intf
->my_dev_name
);
2962 kfree(intf
->my_dev_name
);
2963 intf
->my_dev_name
= NULL
;
2965 mutex_lock(&bmc
->dyn_mutex
);
2966 list_del(&intf
->bmc_link
);
2967 mutex_unlock(&bmc
->dyn_mutex
);
2968 intf
->bmc
= &intf
->tmp_bmc
;
2969 kref_put(&bmc
->usecount
, cleanup_bmc_device
);
2970 intf
->bmc_registered
= false;
2973 static void ipmi_bmc_unregister(struct ipmi_smi
*intf
)
2975 mutex_lock(&intf
->bmc_reg_mutex
);
2976 __ipmi_bmc_unregister(intf
);
2977 mutex_unlock(&intf
->bmc_reg_mutex
);
2981 * Must be called with intf->bmc_reg_mutex held.
2983 static int __ipmi_bmc_register(struct ipmi_smi
*intf
,
2984 struct ipmi_device_id
*id
,
2985 bool guid_set
, guid_t
*guid
, int intf_num
)
2988 struct bmc_device
*bmc
;
2989 struct bmc_device
*old_bmc
;
2992 * platform_device_register() can cause bmc_reg_mutex to
2993 * be claimed because of the is_visible functions of
2994 * the attributes. Eliminate possible recursion and
2997 intf
->in_bmc_register
= true;
2998 mutex_unlock(&intf
->bmc_reg_mutex
);
3001 * Try to find if there is an bmc_device struct
3002 * representing the interfaced BMC already
3004 mutex_lock(&ipmidriver_mutex
);
3006 old_bmc
= ipmi_find_bmc_guid(&ipmidriver
.driver
, guid
);
3008 old_bmc
= ipmi_find_bmc_prod_dev_id(&ipmidriver
.driver
,
3013 * If there is already an bmc_device, free the new one,
3014 * otherwise register the new BMC device
3019 * Note: old_bmc already has usecount incremented by
3020 * the BMC find functions.
3022 intf
->bmc
= old_bmc
;
3023 mutex_lock(&bmc
->dyn_mutex
);
3024 list_add_tail(&intf
->bmc_link
, &bmc
->intfs
);
3025 mutex_unlock(&bmc
->dyn_mutex
);
3027 dev_info(intf
->si_dev
,
3028 "interfacing existing BMC (man_id: 0x%6.6x, prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
3029 bmc
->id
.manufacturer_id
,
3033 bmc
= kzalloc(sizeof(*bmc
), GFP_KERNEL
);
3038 INIT_LIST_HEAD(&bmc
->intfs
);
3039 mutex_init(&bmc
->dyn_mutex
);
3040 INIT_WORK(&bmc
->remove_work
, cleanup_bmc_work
);
3043 bmc
->dyn_id_set
= 1;
3044 bmc
->dyn_guid_set
= guid_set
;
3046 bmc
->dyn_id_expiry
= jiffies
+ IPMI_DYN_DEV_ID_EXPIRY
;
3048 bmc
->pdev
.name
= "ipmi_bmc";
3050 rv
= ida_simple_get(&ipmi_bmc_ida
, 0, 0, GFP_KERNEL
);
3056 bmc
->pdev
.dev
.driver
= &ipmidriver
.driver
;
3058 bmc
->pdev
.dev
.release
= release_bmc_device
;
3059 bmc
->pdev
.dev
.type
= &bmc_device_type
;
3060 kref_init(&bmc
->usecount
);
3063 mutex_lock(&bmc
->dyn_mutex
);
3064 list_add_tail(&intf
->bmc_link
, &bmc
->intfs
);
3065 mutex_unlock(&bmc
->dyn_mutex
);
3067 rv
= platform_device_register(&bmc
->pdev
);
3069 dev_err(intf
->si_dev
,
3070 "Unable to register bmc device: %d\n",
3075 dev_info(intf
->si_dev
,
3076 "Found new BMC (man_id: 0x%6.6x, prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
3077 bmc
->id
.manufacturer_id
,
3083 * create symlink from system interface device to bmc device
3086 rv
= sysfs_create_link(&intf
->si_dev
->kobj
, &bmc
->pdev
.dev
.kobj
, "bmc");
3088 dev_err(intf
->si_dev
, "Unable to create bmc symlink: %d\n", rv
);
3093 intf_num
= intf
->intf_num
;
3094 intf
->my_dev_name
= kasprintf(GFP_KERNEL
, "ipmi%d", intf_num
);
3095 if (!intf
->my_dev_name
) {
3097 dev_err(intf
->si_dev
, "Unable to allocate link from BMC: %d\n",
3102 rv
= sysfs_create_link(&bmc
->pdev
.dev
.kobj
, &intf
->si_dev
->kobj
,
3105 dev_err(intf
->si_dev
, "Unable to create symlink to bmc: %d\n",
3107 goto out_free_my_dev_name
;
3110 intf
->bmc_registered
= true;
3113 mutex_unlock(&ipmidriver_mutex
);
3114 mutex_lock(&intf
->bmc_reg_mutex
);
3115 intf
->in_bmc_register
= false;
3119 out_free_my_dev_name
:
3120 kfree(intf
->my_dev_name
);
3121 intf
->my_dev_name
= NULL
;
3124 sysfs_remove_link(&intf
->si_dev
->kobj
, "bmc");
3127 mutex_lock(&bmc
->dyn_mutex
);
3128 list_del(&intf
->bmc_link
);
3129 mutex_unlock(&bmc
->dyn_mutex
);
3130 intf
->bmc
= &intf
->tmp_bmc
;
3131 kref_put(&bmc
->usecount
, cleanup_bmc_device
);
3135 mutex_lock(&bmc
->dyn_mutex
);
3136 list_del(&intf
->bmc_link
);
3137 mutex_unlock(&bmc
->dyn_mutex
);
3138 intf
->bmc
= &intf
->tmp_bmc
;
3139 put_device(&bmc
->pdev
.dev
);
3144 send_guid_cmd(struct ipmi_smi
*intf
, int chan
)
3146 struct kernel_ipmi_msg msg
;
3147 struct ipmi_system_interface_addr si
;
3149 si
.addr_type
= IPMI_SYSTEM_INTERFACE_ADDR_TYPE
;
3150 si
.channel
= IPMI_BMC_CHANNEL
;
3153 msg
.netfn
= IPMI_NETFN_APP_REQUEST
;
3154 msg
.cmd
= IPMI_GET_DEVICE_GUID_CMD
;
3157 return i_ipmi_request(NULL
,
3159 (struct ipmi_addr
*) &si
,
3166 intf
->addrinfo
[0].address
,
3167 intf
->addrinfo
[0].lun
,
3171 static void guid_handler(struct ipmi_smi
*intf
, struct ipmi_recv_msg
*msg
)
3173 struct bmc_device
*bmc
= intf
->bmc
;
3175 if ((msg
->addr
.addr_type
!= IPMI_SYSTEM_INTERFACE_ADDR_TYPE
)
3176 || (msg
->msg
.netfn
!= IPMI_NETFN_APP_RESPONSE
)
3177 || (msg
->msg
.cmd
!= IPMI_GET_DEVICE_GUID_CMD
))
3181 if (msg
->msg
.data
[0] != 0) {
3182 /* Error from getting the GUID, the BMC doesn't have one. */
3183 bmc
->dyn_guid_set
= 0;
3187 if (msg
->msg
.data_len
< UUID_SIZE
+ 1) {
3188 bmc
->dyn_guid_set
= 0;
3189 dev_warn(intf
->si_dev
,
3190 "The GUID response from the BMC was too short, it was %d but should have been %d. Assuming GUID is not available.\n",
3191 msg
->msg
.data_len
, UUID_SIZE
+ 1);
3195 import_guid(&bmc
->fetch_guid
, msg
->msg
.data
+ 1);
3197 * Make sure the guid data is available before setting
3201 bmc
->dyn_guid_set
= 1;
3203 wake_up(&intf
->waitq
);
3206 static void __get_guid(struct ipmi_smi
*intf
)
3209 struct bmc_device
*bmc
= intf
->bmc
;
3211 bmc
->dyn_guid_set
= 2;
3212 intf
->null_user_handler
= guid_handler
;
3213 rv
= send_guid_cmd(intf
, 0);
3215 /* Send failed, no GUID available. */
3216 bmc
->dyn_guid_set
= 0;
3218 wait_event(intf
->waitq
, bmc
->dyn_guid_set
!= 2);
3220 /* dyn_guid_set makes the guid data available. */
3223 intf
->null_user_handler
= NULL
;
3227 send_channel_info_cmd(struct ipmi_smi
*intf
, int chan
)
3229 struct kernel_ipmi_msg msg
;
3230 unsigned char data
[1];
3231 struct ipmi_system_interface_addr si
;
3233 si
.addr_type
= IPMI_SYSTEM_INTERFACE_ADDR_TYPE
;
3234 si
.channel
= IPMI_BMC_CHANNEL
;
3237 msg
.netfn
= IPMI_NETFN_APP_REQUEST
;
3238 msg
.cmd
= IPMI_GET_CHANNEL_INFO_CMD
;
3242 return i_ipmi_request(NULL
,
3244 (struct ipmi_addr
*) &si
,
3251 intf
->addrinfo
[0].address
,
3252 intf
->addrinfo
[0].lun
,
3257 channel_handler(struct ipmi_smi
*intf
, struct ipmi_recv_msg
*msg
)
3261 unsigned int set
= intf
->curr_working_cset
;
3262 struct ipmi_channel
*chans
;
3264 if ((msg
->addr
.addr_type
== IPMI_SYSTEM_INTERFACE_ADDR_TYPE
)
3265 && (msg
->msg
.netfn
== IPMI_NETFN_APP_RESPONSE
)
3266 && (msg
->msg
.cmd
== IPMI_GET_CHANNEL_INFO_CMD
)) {
3267 /* It's the one we want */
3268 if (msg
->msg
.data
[0] != 0) {
3269 /* Got an error from the channel, just go on. */
3270 if (msg
->msg
.data
[0] == IPMI_INVALID_COMMAND_ERR
) {
3272 * If the MC does not support this
3273 * command, that is legal. We just
3274 * assume it has one IPMB at channel
3277 intf
->wchannels
[set
].c
[0].medium
3278 = IPMI_CHANNEL_MEDIUM_IPMB
;
3279 intf
->wchannels
[set
].c
[0].protocol
3280 = IPMI_CHANNEL_PROTOCOL_IPMB
;
3282 intf
->channel_list
= intf
->wchannels
+ set
;
3283 intf
->channels_ready
= true;
3284 wake_up(&intf
->waitq
);
3289 if (msg
->msg
.data_len
< 4) {
3290 /* Message not big enough, just go on. */
3293 ch
= intf
->curr_channel
;
3294 chans
= intf
->wchannels
[set
].c
;
3295 chans
[ch
].medium
= msg
->msg
.data
[2] & 0x7f;
3296 chans
[ch
].protocol
= msg
->msg
.data
[3] & 0x1f;
3299 intf
->curr_channel
++;
3300 if (intf
->curr_channel
>= IPMI_MAX_CHANNELS
) {
3301 intf
->channel_list
= intf
->wchannels
+ set
;
3302 intf
->channels_ready
= true;
3303 wake_up(&intf
->waitq
);
3305 intf
->channel_list
= intf
->wchannels
+ set
;
3306 intf
->channels_ready
= true;
3307 rv
= send_channel_info_cmd(intf
, intf
->curr_channel
);
3311 /* Got an error somehow, just give up. */
3312 dev_warn(intf
->si_dev
,
3313 "Error sending channel information for channel %d: %d\n",
3314 intf
->curr_channel
, rv
);
3316 intf
->channel_list
= intf
->wchannels
+ set
;
3317 intf
->channels_ready
= true;
3318 wake_up(&intf
->waitq
);
3326 * Must be holding intf->bmc_reg_mutex to call this.
3328 static int __scan_channels(struct ipmi_smi
*intf
, struct ipmi_device_id
*id
)
3332 if (ipmi_version_major(id
) > 1
3333 || (ipmi_version_major(id
) == 1
3334 && ipmi_version_minor(id
) >= 5)) {
3338 * Start scanning the channels to see what is
3341 set
= !intf
->curr_working_cset
;
3342 intf
->curr_working_cset
= set
;
3343 memset(&intf
->wchannels
[set
], 0,
3344 sizeof(struct ipmi_channel_set
));
3346 intf
->null_user_handler
= channel_handler
;
3347 intf
->curr_channel
= 0;
3348 rv
= send_channel_info_cmd(intf
, 0);
3350 dev_warn(intf
->si_dev
,
3351 "Error sending channel information for channel 0, %d\n",
3353 intf
->null_user_handler
= NULL
;
3357 /* Wait for the channel info to be read. */
3358 wait_event(intf
->waitq
, intf
->channels_ready
);
3359 intf
->null_user_handler
= NULL
;
3361 unsigned int set
= intf
->curr_working_cset
;
3363 /* Assume a single IPMB channel at zero. */
3364 intf
->wchannels
[set
].c
[0].medium
= IPMI_CHANNEL_MEDIUM_IPMB
;
3365 intf
->wchannels
[set
].c
[0].protocol
= IPMI_CHANNEL_PROTOCOL_IPMB
;
3366 intf
->channel_list
= intf
->wchannels
+ set
;
3367 intf
->channels_ready
= true;
3373 static void ipmi_poll(struct ipmi_smi
*intf
)
3375 if (intf
->handlers
->poll
)
3376 intf
->handlers
->poll(intf
->send_info
);
3377 /* In case something came in */
3378 handle_new_recv_msgs(intf
);
3381 void ipmi_poll_interface(struct ipmi_user
*user
)
3383 ipmi_poll(user
->intf
);
3385 EXPORT_SYMBOL(ipmi_poll_interface
);
3387 static void redo_bmc_reg(struct work_struct
*work
)
3389 struct ipmi_smi
*intf
= container_of(work
, struct ipmi_smi
,
3392 if (!intf
->in_shutdown
)
3393 bmc_get_device_id(intf
, NULL
, NULL
, NULL
, NULL
);
3395 kref_put(&intf
->refcount
, intf_free
);
3398 int ipmi_add_smi(struct module
*owner
,
3399 const struct ipmi_smi_handlers
*handlers
,
3401 struct device
*si_dev
,
3402 unsigned char slave_addr
)
3406 struct ipmi_smi
*intf
, *tintf
;
3407 struct list_head
*link
;
3408 struct ipmi_device_id id
;
3411 * Make sure the driver is actually initialized, this handles
3412 * problems with initialization order.
3414 rv
= ipmi_init_msghandler();
3418 intf
= kzalloc(sizeof(*intf
), GFP_KERNEL
);
3422 rv
= init_srcu_struct(&intf
->users_srcu
);
3428 intf
->owner
= owner
;
3429 intf
->bmc
= &intf
->tmp_bmc
;
3430 INIT_LIST_HEAD(&intf
->bmc
->intfs
);
3431 mutex_init(&intf
->bmc
->dyn_mutex
);
3432 INIT_LIST_HEAD(&intf
->bmc_link
);
3433 mutex_init(&intf
->bmc_reg_mutex
);
3434 intf
->intf_num
= -1; /* Mark it invalid for now. */
3435 kref_init(&intf
->refcount
);
3436 INIT_WORK(&intf
->bmc_reg_work
, redo_bmc_reg
);
3437 intf
->si_dev
= si_dev
;
3438 for (j
= 0; j
< IPMI_MAX_CHANNELS
; j
++) {
3439 intf
->addrinfo
[j
].address
= IPMI_BMC_SLAVE_ADDR
;
3440 intf
->addrinfo
[j
].lun
= 2;
3442 if (slave_addr
!= 0)
3443 intf
->addrinfo
[0].address
= slave_addr
;
3444 INIT_LIST_HEAD(&intf
->users
);
3445 intf
->handlers
= handlers
;
3446 intf
->send_info
= send_info
;
3447 spin_lock_init(&intf
->seq_lock
);
3448 for (j
= 0; j
< IPMI_IPMB_NUM_SEQ
; j
++) {
3449 intf
->seq_table
[j
].inuse
= 0;
3450 intf
->seq_table
[j
].seqid
= 0;
3453 spin_lock_init(&intf
->waiting_rcv_msgs_lock
);
3454 INIT_LIST_HEAD(&intf
->waiting_rcv_msgs
);
3455 tasklet_setup(&intf
->recv_tasklet
,
3457 atomic_set(&intf
->watchdog_pretimeouts_to_deliver
, 0);
3458 spin_lock_init(&intf
->xmit_msgs_lock
);
3459 INIT_LIST_HEAD(&intf
->xmit_msgs
);
3460 INIT_LIST_HEAD(&intf
->hp_xmit_msgs
);
3461 spin_lock_init(&intf
->events_lock
);
3462 spin_lock_init(&intf
->watch_lock
);
3463 atomic_set(&intf
->event_waiters
, 0);
3464 intf
->ticks_to_req_ev
= IPMI_REQUEST_EV_TIME
;
3465 INIT_LIST_HEAD(&intf
->waiting_events
);
3466 intf
->waiting_events_count
= 0;
3467 mutex_init(&intf
->cmd_rcvrs_mutex
);
3468 spin_lock_init(&intf
->maintenance_mode_lock
);
3469 INIT_LIST_HEAD(&intf
->cmd_rcvrs
);
3470 init_waitqueue_head(&intf
->waitq
);
3471 for (i
= 0; i
< IPMI_NUM_STATS
; i
++)
3472 atomic_set(&intf
->stats
[i
], 0);
3474 mutex_lock(&ipmi_interfaces_mutex
);
3475 /* Look for a hole in the numbers. */
3477 link
= &ipmi_interfaces
;
3478 list_for_each_entry_rcu(tintf
, &ipmi_interfaces
, link
,
3479 ipmi_interfaces_mutex_held()) {
3480 if (tintf
->intf_num
!= i
) {
3481 link
= &tintf
->link
;
3486 /* Add the new interface in numeric order. */
3488 list_add_rcu(&intf
->link
, &ipmi_interfaces
);
3490 list_add_tail_rcu(&intf
->link
, link
);
3492 rv
= handlers
->start_processing(send_info
, intf
);
3496 rv
= __bmc_get_device_id(intf
, NULL
, &id
, NULL
, NULL
, i
);
3498 dev_err(si_dev
, "Unable to get the device id: %d\n", rv
);
3499 goto out_err_started
;
3502 mutex_lock(&intf
->bmc_reg_mutex
);
3503 rv
= __scan_channels(intf
, &id
);
3504 mutex_unlock(&intf
->bmc_reg_mutex
);
3506 goto out_err_bmc_reg
;
3509 * Keep memory order straight for RCU readers. Make
3510 * sure everything else is committed to memory before
3511 * setting intf_num to mark the interface valid.
3515 mutex_unlock(&ipmi_interfaces_mutex
);
3517 /* After this point the interface is legal to use. */
3518 call_smi_watchers(i
, intf
->si_dev
);
3523 ipmi_bmc_unregister(intf
);
3525 if (intf
->handlers
->shutdown
)
3526 intf
->handlers
->shutdown(intf
->send_info
);
3528 list_del_rcu(&intf
->link
);
3529 mutex_unlock(&ipmi_interfaces_mutex
);
3530 synchronize_srcu(&ipmi_interfaces_srcu
);
3531 cleanup_srcu_struct(&intf
->users_srcu
);
3532 kref_put(&intf
->refcount
, intf_free
);
3536 EXPORT_SYMBOL(ipmi_add_smi
);
3538 static void deliver_smi_err_response(struct ipmi_smi
*intf
,
3539 struct ipmi_smi_msg
*msg
,
3542 msg
->rsp
[0] = msg
->data
[0] | 4;
3543 msg
->rsp
[1] = msg
->data
[1];
3546 /* It's an error, so it will never requeue, no need to check return. */
3547 handle_one_recv_msg(intf
, msg
);
3550 static void cleanup_smi_msgs(struct ipmi_smi
*intf
)
3553 struct seq_table
*ent
;
3554 struct ipmi_smi_msg
*msg
;
3555 struct list_head
*entry
;
3556 struct list_head tmplist
;
3558 /* Clear out our transmit queues and hold the messages. */
3559 INIT_LIST_HEAD(&tmplist
);
3560 list_splice_tail(&intf
->hp_xmit_msgs
, &tmplist
);
3561 list_splice_tail(&intf
->xmit_msgs
, &tmplist
);
3563 /* Current message first, to preserve order */
3564 while (intf
->curr_msg
&& !list_empty(&intf
->waiting_rcv_msgs
)) {
3565 /* Wait for the message to clear out. */
3566 schedule_timeout(1);
3569 /* No need for locks, the interface is down. */
3572 * Return errors for all pending messages in queue and in the
3573 * tables waiting for remote responses.
3575 while (!list_empty(&tmplist
)) {
3576 entry
= tmplist
.next
;
3578 msg
= list_entry(entry
, struct ipmi_smi_msg
, link
);
3579 deliver_smi_err_response(intf
, msg
, IPMI_ERR_UNSPECIFIED
);
3582 for (i
= 0; i
< IPMI_IPMB_NUM_SEQ
; i
++) {
3583 ent
= &intf
->seq_table
[i
];
3586 deliver_err_response(intf
, ent
->recv_msg
, IPMI_ERR_UNSPECIFIED
);
3590 void ipmi_unregister_smi(struct ipmi_smi
*intf
)
3592 struct ipmi_smi_watcher
*w
;
3593 int intf_num
= intf
->intf_num
, index
;
3595 mutex_lock(&ipmi_interfaces_mutex
);
3596 intf
->intf_num
= -1;
3597 intf
->in_shutdown
= true;
3598 list_del_rcu(&intf
->link
);
3599 mutex_unlock(&ipmi_interfaces_mutex
);
3600 synchronize_srcu(&ipmi_interfaces_srcu
);
3602 /* At this point no users can be added to the interface. */
3605 * Call all the watcher interfaces to tell them that
3606 * an interface is going away.
3608 mutex_lock(&smi_watchers_mutex
);
3609 list_for_each_entry(w
, &smi_watchers
, link
)
3610 w
->smi_gone(intf_num
);
3611 mutex_unlock(&smi_watchers_mutex
);
3613 index
= srcu_read_lock(&intf
->users_srcu
);
3614 while (!list_empty(&intf
->users
)) {
3615 struct ipmi_user
*user
=
3616 container_of(list_next_rcu(&intf
->users
),
3617 struct ipmi_user
, link
);
3619 _ipmi_destroy_user(user
);
3621 srcu_read_unlock(&intf
->users_srcu
, index
);
3623 if (intf
->handlers
->shutdown
)
3624 intf
->handlers
->shutdown(intf
->send_info
);
3626 cleanup_smi_msgs(intf
);
3628 ipmi_bmc_unregister(intf
);
3630 cleanup_srcu_struct(&intf
->users_srcu
);
3631 kref_put(&intf
->refcount
, intf_free
);
3633 EXPORT_SYMBOL(ipmi_unregister_smi
);
3635 static int handle_ipmb_get_msg_rsp(struct ipmi_smi
*intf
,
3636 struct ipmi_smi_msg
*msg
)
3638 struct ipmi_ipmb_addr ipmb_addr
;
3639 struct ipmi_recv_msg
*recv_msg
;
3642 * This is 11, not 10, because the response must contain a
3645 if (msg
->rsp_size
< 11) {
3646 /* Message not big enough, just ignore it. */
3647 ipmi_inc_stat(intf
, invalid_ipmb_responses
);
3651 if (msg
->rsp
[2] != 0) {
3652 /* An error getting the response, just ignore it. */
3656 ipmb_addr
.addr_type
= IPMI_IPMB_ADDR_TYPE
;
3657 ipmb_addr
.slave_addr
= msg
->rsp
[6];
3658 ipmb_addr
.channel
= msg
->rsp
[3] & 0x0f;
3659 ipmb_addr
.lun
= msg
->rsp
[7] & 3;
3662 * It's a response from a remote entity. Look up the sequence
3663 * number and handle the response.
3665 if (intf_find_seq(intf
,
3669 (msg
->rsp
[4] >> 2) & (~1),
3670 (struct ipmi_addr
*) &ipmb_addr
,
3673 * We were unable to find the sequence number,
3674 * so just nuke the message.
3676 ipmi_inc_stat(intf
, unhandled_ipmb_responses
);
3680 memcpy(recv_msg
->msg_data
, &msg
->rsp
[9], msg
->rsp_size
- 9);
3682 * The other fields matched, so no need to set them, except
3683 * for netfn, which needs to be the response that was
3684 * returned, not the request value.
3686 recv_msg
->msg
.netfn
= msg
->rsp
[4] >> 2;
3687 recv_msg
->msg
.data
= recv_msg
->msg_data
;
3688 recv_msg
->msg
.data_len
= msg
->rsp_size
- 10;
3689 recv_msg
->recv_type
= IPMI_RESPONSE_RECV_TYPE
;
3690 if (deliver_response(intf
, recv_msg
))
3691 ipmi_inc_stat(intf
, unhandled_ipmb_responses
);
3693 ipmi_inc_stat(intf
, handled_ipmb_responses
);
3698 static int handle_ipmb_get_msg_cmd(struct ipmi_smi
*intf
,
3699 struct ipmi_smi_msg
*msg
)
3701 struct cmd_rcvr
*rcvr
;
3703 unsigned char netfn
;
3706 struct ipmi_user
*user
= NULL
;
3707 struct ipmi_ipmb_addr
*ipmb_addr
;
3708 struct ipmi_recv_msg
*recv_msg
;
3710 if (msg
->rsp_size
< 10) {
3711 /* Message not big enough, just ignore it. */
3712 ipmi_inc_stat(intf
, invalid_commands
);
3716 if (msg
->rsp
[2] != 0) {
3717 /* An error getting the response, just ignore it. */
3721 netfn
= msg
->rsp
[4] >> 2;
3723 chan
= msg
->rsp
[3] & 0xf;
3726 rcvr
= find_cmd_rcvr(intf
, netfn
, cmd
, chan
);
3729 kref_get(&user
->refcount
);
3735 /* We didn't find a user, deliver an error response. */
3736 ipmi_inc_stat(intf
, unhandled_commands
);
3738 msg
->data
[0] = (IPMI_NETFN_APP_REQUEST
<< 2);
3739 msg
->data
[1] = IPMI_SEND_MSG_CMD
;
3740 msg
->data
[2] = msg
->rsp
[3];
3741 msg
->data
[3] = msg
->rsp
[6];
3742 msg
->data
[4] = ((netfn
+ 1) << 2) | (msg
->rsp
[7] & 0x3);
3743 msg
->data
[5] = ipmb_checksum(&msg
->data
[3], 2);
3744 msg
->data
[6] = intf
->addrinfo
[msg
->rsp
[3] & 0xf].address
;
3746 msg
->data
[7] = (msg
->rsp
[7] & 0xfc) | (msg
->rsp
[4] & 0x3);
3747 msg
->data
[8] = msg
->rsp
[8]; /* cmd */
3748 msg
->data
[9] = IPMI_INVALID_CMD_COMPLETION_CODE
;
3749 msg
->data
[10] = ipmb_checksum(&msg
->data
[6], 4);
3750 msg
->data_size
= 11;
3752 pr_debug("Invalid command: %*ph\n", msg
->data_size
, msg
->data
);
3755 if (!intf
->in_shutdown
) {
3756 smi_send(intf
, intf
->handlers
, msg
, 0);
3758 * We used the message, so return the value
3759 * that causes it to not be freed or
3766 recv_msg
= ipmi_alloc_recv_msg();
3769 * We couldn't allocate memory for the
3770 * message, so requeue it for handling
3774 kref_put(&user
->refcount
, free_user
);
3776 /* Extract the source address from the data. */
3777 ipmb_addr
= (struct ipmi_ipmb_addr
*) &recv_msg
->addr
;
3778 ipmb_addr
->addr_type
= IPMI_IPMB_ADDR_TYPE
;
3779 ipmb_addr
->slave_addr
= msg
->rsp
[6];
3780 ipmb_addr
->lun
= msg
->rsp
[7] & 3;
3781 ipmb_addr
->channel
= msg
->rsp
[3] & 0xf;
3784 * Extract the rest of the message information
3785 * from the IPMB header.
3787 recv_msg
->user
= user
;
3788 recv_msg
->recv_type
= IPMI_CMD_RECV_TYPE
;
3789 recv_msg
->msgid
= msg
->rsp
[7] >> 2;
3790 recv_msg
->msg
.netfn
= msg
->rsp
[4] >> 2;
3791 recv_msg
->msg
.cmd
= msg
->rsp
[8];
3792 recv_msg
->msg
.data
= recv_msg
->msg_data
;
3795 * We chop off 10, not 9 bytes because the checksum
3796 * at the end also needs to be removed.
3798 recv_msg
->msg
.data_len
= msg
->rsp_size
- 10;
3799 memcpy(recv_msg
->msg_data
, &msg
->rsp
[9],
3800 msg
->rsp_size
- 10);
3801 if (deliver_response(intf
, recv_msg
))
3802 ipmi_inc_stat(intf
, unhandled_commands
);
3804 ipmi_inc_stat(intf
, handled_commands
);
3811 static int handle_lan_get_msg_rsp(struct ipmi_smi
*intf
,
3812 struct ipmi_smi_msg
*msg
)
3814 struct ipmi_lan_addr lan_addr
;
3815 struct ipmi_recv_msg
*recv_msg
;
3819 * This is 13, not 12, because the response must contain a
3822 if (msg
->rsp_size
< 13) {
3823 /* Message not big enough, just ignore it. */
3824 ipmi_inc_stat(intf
, invalid_lan_responses
);
3828 if (msg
->rsp
[2] != 0) {
3829 /* An error getting the response, just ignore it. */
3833 lan_addr
.addr_type
= IPMI_LAN_ADDR_TYPE
;
3834 lan_addr
.session_handle
= msg
->rsp
[4];
3835 lan_addr
.remote_SWID
= msg
->rsp
[8];
3836 lan_addr
.local_SWID
= msg
->rsp
[5];
3837 lan_addr
.channel
= msg
->rsp
[3] & 0x0f;
3838 lan_addr
.privilege
= msg
->rsp
[3] >> 4;
3839 lan_addr
.lun
= msg
->rsp
[9] & 3;
3842 * It's a response from a remote entity. Look up the sequence
3843 * number and handle the response.
3845 if (intf_find_seq(intf
,
3849 (msg
->rsp
[6] >> 2) & (~1),
3850 (struct ipmi_addr
*) &lan_addr
,
3853 * We were unable to find the sequence number,
3854 * so just nuke the message.
3856 ipmi_inc_stat(intf
, unhandled_lan_responses
);
3860 memcpy(recv_msg
->msg_data
, &msg
->rsp
[11], msg
->rsp_size
- 11);
3862 * The other fields matched, so no need to set them, except
3863 * for netfn, which needs to be the response that was
3864 * returned, not the request value.
3866 recv_msg
->msg
.netfn
= msg
->rsp
[6] >> 2;
3867 recv_msg
->msg
.data
= recv_msg
->msg_data
;
3868 recv_msg
->msg
.data_len
= msg
->rsp_size
- 12;
3869 recv_msg
->recv_type
= IPMI_RESPONSE_RECV_TYPE
;
3870 if (deliver_response(intf
, recv_msg
))
3871 ipmi_inc_stat(intf
, unhandled_lan_responses
);
3873 ipmi_inc_stat(intf
, handled_lan_responses
);
3878 static int handle_lan_get_msg_cmd(struct ipmi_smi
*intf
,
3879 struct ipmi_smi_msg
*msg
)
3881 struct cmd_rcvr
*rcvr
;
3883 unsigned char netfn
;
3886 struct ipmi_user
*user
= NULL
;
3887 struct ipmi_lan_addr
*lan_addr
;
3888 struct ipmi_recv_msg
*recv_msg
;
3890 if (msg
->rsp_size
< 12) {
3891 /* Message not big enough, just ignore it. */
3892 ipmi_inc_stat(intf
, invalid_commands
);
3896 if (msg
->rsp
[2] != 0) {
3897 /* An error getting the response, just ignore it. */
3901 netfn
= msg
->rsp
[6] >> 2;
3903 chan
= msg
->rsp
[3] & 0xf;
3906 rcvr
= find_cmd_rcvr(intf
, netfn
, cmd
, chan
);
3909 kref_get(&user
->refcount
);
3915 /* We didn't find a user, just give up. */
3916 ipmi_inc_stat(intf
, unhandled_commands
);
3919 * Don't do anything with these messages, just allow
3924 recv_msg
= ipmi_alloc_recv_msg();
3927 * We couldn't allocate memory for the
3928 * message, so requeue it for handling later.
3931 kref_put(&user
->refcount
, free_user
);
3933 /* Extract the source address from the data. */
3934 lan_addr
= (struct ipmi_lan_addr
*) &recv_msg
->addr
;
3935 lan_addr
->addr_type
= IPMI_LAN_ADDR_TYPE
;
3936 lan_addr
->session_handle
= msg
->rsp
[4];
3937 lan_addr
->remote_SWID
= msg
->rsp
[8];
3938 lan_addr
->local_SWID
= msg
->rsp
[5];
3939 lan_addr
->lun
= msg
->rsp
[9] & 3;
3940 lan_addr
->channel
= msg
->rsp
[3] & 0xf;
3941 lan_addr
->privilege
= msg
->rsp
[3] >> 4;
3944 * Extract the rest of the message information
3945 * from the IPMB header.
3947 recv_msg
->user
= user
;
3948 recv_msg
->recv_type
= IPMI_CMD_RECV_TYPE
;
3949 recv_msg
->msgid
= msg
->rsp
[9] >> 2;
3950 recv_msg
->msg
.netfn
= msg
->rsp
[6] >> 2;
3951 recv_msg
->msg
.cmd
= msg
->rsp
[10];
3952 recv_msg
->msg
.data
= recv_msg
->msg_data
;
3955 * We chop off 12, not 11 bytes because the checksum
3956 * at the end also needs to be removed.
3958 recv_msg
->msg
.data_len
= msg
->rsp_size
- 12;
3959 memcpy(recv_msg
->msg_data
, &msg
->rsp
[11],
3960 msg
->rsp_size
- 12);
3961 if (deliver_response(intf
, recv_msg
))
3962 ipmi_inc_stat(intf
, unhandled_commands
);
3964 ipmi_inc_stat(intf
, handled_commands
);
3972 * This routine will handle "Get Message" command responses with
3973 * channels that use an OEM Medium. The message format belongs to
3974 * the OEM. See IPMI 2.0 specification, Chapter 6 and
3975 * Chapter 22, sections 22.6 and 22.24 for more details.
3977 static int handle_oem_get_msg_cmd(struct ipmi_smi
*intf
,
3978 struct ipmi_smi_msg
*msg
)
3980 struct cmd_rcvr
*rcvr
;
3982 unsigned char netfn
;
3985 struct ipmi_user
*user
= NULL
;
3986 struct ipmi_system_interface_addr
*smi_addr
;
3987 struct ipmi_recv_msg
*recv_msg
;
3990 * We expect the OEM SW to perform error checking
3991 * so we just do some basic sanity checks
3993 if (msg
->rsp_size
< 4) {
3994 /* Message not big enough, just ignore it. */
3995 ipmi_inc_stat(intf
, invalid_commands
);
3999 if (msg
->rsp
[2] != 0) {
4000 /* An error getting the response, just ignore it. */
4005 * This is an OEM Message so the OEM needs to know how
4006 * handle the message. We do no interpretation.
4008 netfn
= msg
->rsp
[0] >> 2;
4010 chan
= msg
->rsp
[3] & 0xf;
4013 rcvr
= find_cmd_rcvr(intf
, netfn
, cmd
, chan
);
4016 kref_get(&user
->refcount
);
4022 /* We didn't find a user, just give up. */
4023 ipmi_inc_stat(intf
, unhandled_commands
);
4026 * Don't do anything with these messages, just allow
4032 recv_msg
= ipmi_alloc_recv_msg();
4035 * We couldn't allocate memory for the
4036 * message, so requeue it for handling
4040 kref_put(&user
->refcount
, free_user
);
4043 * OEM Messages are expected to be delivered via
4044 * the system interface to SMS software. We might
4045 * need to visit this again depending on OEM
4048 smi_addr
= ((struct ipmi_system_interface_addr
*)
4050 smi_addr
->addr_type
= IPMI_SYSTEM_INTERFACE_ADDR_TYPE
;
4051 smi_addr
->channel
= IPMI_BMC_CHANNEL
;
4052 smi_addr
->lun
= msg
->rsp
[0] & 3;
4054 recv_msg
->user
= user
;
4055 recv_msg
->user_msg_data
= NULL
;
4056 recv_msg
->recv_type
= IPMI_OEM_RECV_TYPE
;
4057 recv_msg
->msg
.netfn
= msg
->rsp
[0] >> 2;
4058 recv_msg
->msg
.cmd
= msg
->rsp
[1];
4059 recv_msg
->msg
.data
= recv_msg
->msg_data
;
4062 * The message starts at byte 4 which follows the
4063 * the Channel Byte in the "GET MESSAGE" command
4065 recv_msg
->msg
.data_len
= msg
->rsp_size
- 4;
4066 memcpy(recv_msg
->msg_data
, &msg
->rsp
[4],
4068 if (deliver_response(intf
, recv_msg
))
4069 ipmi_inc_stat(intf
, unhandled_commands
);
4071 ipmi_inc_stat(intf
, handled_commands
);
4078 static void copy_event_into_recv_msg(struct ipmi_recv_msg
*recv_msg
,
4079 struct ipmi_smi_msg
*msg
)
4081 struct ipmi_system_interface_addr
*smi_addr
;
4083 recv_msg
->msgid
= 0;
4084 smi_addr
= (struct ipmi_system_interface_addr
*) &recv_msg
->addr
;
4085 smi_addr
->addr_type
= IPMI_SYSTEM_INTERFACE_ADDR_TYPE
;
4086 smi_addr
->channel
= IPMI_BMC_CHANNEL
;
4087 smi_addr
->lun
= msg
->rsp
[0] & 3;
4088 recv_msg
->recv_type
= IPMI_ASYNC_EVENT_RECV_TYPE
;
4089 recv_msg
->msg
.netfn
= msg
->rsp
[0] >> 2;
4090 recv_msg
->msg
.cmd
= msg
->rsp
[1];
4091 memcpy(recv_msg
->msg_data
, &msg
->rsp
[3], msg
->rsp_size
- 3);
4092 recv_msg
->msg
.data
= recv_msg
->msg_data
;
4093 recv_msg
->msg
.data_len
= msg
->rsp_size
- 3;
4096 static int handle_read_event_rsp(struct ipmi_smi
*intf
,
4097 struct ipmi_smi_msg
*msg
)
4099 struct ipmi_recv_msg
*recv_msg
, *recv_msg2
;
4100 struct list_head msgs
;
4101 struct ipmi_user
*user
;
4102 int rv
= 0, deliver_count
= 0, index
;
4103 unsigned long flags
;
4105 if (msg
->rsp_size
< 19) {
4106 /* Message is too small to be an IPMB event. */
4107 ipmi_inc_stat(intf
, invalid_events
);
4111 if (msg
->rsp
[2] != 0) {
4112 /* An error getting the event, just ignore it. */
4116 INIT_LIST_HEAD(&msgs
);
4118 spin_lock_irqsave(&intf
->events_lock
, flags
);
4120 ipmi_inc_stat(intf
, events
);
4123 * Allocate and fill in one message for every user that is
4126 index
= srcu_read_lock(&intf
->users_srcu
);
4127 list_for_each_entry_rcu(user
, &intf
->users
, link
) {
4128 if (!user
->gets_events
)
4131 recv_msg
= ipmi_alloc_recv_msg();
4134 list_for_each_entry_safe(recv_msg
, recv_msg2
, &msgs
,
4136 list_del(&recv_msg
->link
);
4137 ipmi_free_recv_msg(recv_msg
);
4140 * We couldn't allocate memory for the
4141 * message, so requeue it for handling
4150 copy_event_into_recv_msg(recv_msg
, msg
);
4151 recv_msg
->user
= user
;
4152 kref_get(&user
->refcount
);
4153 list_add_tail(&recv_msg
->link
, &msgs
);
4155 srcu_read_unlock(&intf
->users_srcu
, index
);
4157 if (deliver_count
) {
4158 /* Now deliver all the messages. */
4159 list_for_each_entry_safe(recv_msg
, recv_msg2
, &msgs
, link
) {
4160 list_del(&recv_msg
->link
);
4161 deliver_local_response(intf
, recv_msg
);
4163 } else if (intf
->waiting_events_count
< MAX_EVENTS_IN_QUEUE
) {
4165 * No one to receive the message, put it in queue if there's
4166 * not already too many things in the queue.
4168 recv_msg
= ipmi_alloc_recv_msg();
4171 * We couldn't allocate memory for the
4172 * message, so requeue it for handling
4179 copy_event_into_recv_msg(recv_msg
, msg
);
4180 list_add_tail(&recv_msg
->link
, &intf
->waiting_events
);
4181 intf
->waiting_events_count
++;
4182 } else if (!intf
->event_msg_printed
) {
4184 * There's too many things in the queue, discard this
4187 dev_warn(intf
->si_dev
,
4188 "Event queue full, discarding incoming events\n");
4189 intf
->event_msg_printed
= 1;
4193 spin_unlock_irqrestore(&intf
->events_lock
, flags
);
4198 static int handle_bmc_rsp(struct ipmi_smi
*intf
,
4199 struct ipmi_smi_msg
*msg
)
4201 struct ipmi_recv_msg
*recv_msg
;
4202 struct ipmi_system_interface_addr
*smi_addr
;
4204 recv_msg
= (struct ipmi_recv_msg
*) msg
->user_data
;
4205 if (recv_msg
== NULL
) {
4206 dev_warn(intf
->si_dev
,
4207 "IPMI message received with no owner. This could be because of a malformed message, or because of a hardware error. Contact your hardware vendor for assistance.\n");
4211 recv_msg
->recv_type
= IPMI_RESPONSE_RECV_TYPE
;
4212 recv_msg
->msgid
= msg
->msgid
;
4213 smi_addr
= ((struct ipmi_system_interface_addr
*)
4215 smi_addr
->addr_type
= IPMI_SYSTEM_INTERFACE_ADDR_TYPE
;
4216 smi_addr
->channel
= IPMI_BMC_CHANNEL
;
4217 smi_addr
->lun
= msg
->rsp
[0] & 3;
4218 recv_msg
->msg
.netfn
= msg
->rsp
[0] >> 2;
4219 recv_msg
->msg
.cmd
= msg
->rsp
[1];
4220 memcpy(recv_msg
->msg_data
, &msg
->rsp
[2], msg
->rsp_size
- 2);
4221 recv_msg
->msg
.data
= recv_msg
->msg_data
;
4222 recv_msg
->msg
.data_len
= msg
->rsp_size
- 2;
4223 deliver_local_response(intf
, recv_msg
);
4229 * Handle a received message. Return 1 if the message should be requeued,
4230 * 0 if the message should be freed, or -1 if the message should not
4231 * be freed or requeued.
4233 static int handle_one_recv_msg(struct ipmi_smi
*intf
,
4234 struct ipmi_smi_msg
*msg
)
4239 pr_debug("Recv: %*ph\n", msg
->rsp_size
, msg
->rsp
);
4241 if ((msg
->data_size
>= 2)
4242 && (msg
->data
[0] == (IPMI_NETFN_APP_REQUEST
<< 2))
4243 && (msg
->data
[1] == IPMI_SEND_MSG_CMD
)
4244 && (msg
->user_data
== NULL
)) {
4246 if (intf
->in_shutdown
)
4250 * This is the local response to a command send, start
4251 * the timer for these. The user_data will not be
4252 * NULL if this is a response send, and we will let
4253 * response sends just go through.
4257 * Check for errors, if we get certain errors (ones
4258 * that mean basically we can try again later), we
4259 * ignore them and start the timer. Otherwise we
4260 * report the error immediately.
4262 if ((msg
->rsp_size
>= 3) && (msg
->rsp
[2] != 0)
4263 && (msg
->rsp
[2] != IPMI_NODE_BUSY_ERR
)
4264 && (msg
->rsp
[2] != IPMI_LOST_ARBITRATION_ERR
)
4265 && (msg
->rsp
[2] != IPMI_BUS_ERR
)
4266 && (msg
->rsp
[2] != IPMI_NAK_ON_WRITE_ERR
)) {
4267 int ch
= msg
->rsp
[3] & 0xf;
4268 struct ipmi_channel
*chans
;
4270 /* Got an error sending the message, handle it. */
4272 chans
= READ_ONCE(intf
->channel_list
)->c
;
4273 if ((chans
[ch
].medium
== IPMI_CHANNEL_MEDIUM_8023LAN
)
4274 || (chans
[ch
].medium
== IPMI_CHANNEL_MEDIUM_ASYNC
))
4275 ipmi_inc_stat(intf
, sent_lan_command_errs
);
4277 ipmi_inc_stat(intf
, sent_ipmb_command_errs
);
4278 intf_err_seq(intf
, msg
->msgid
, msg
->rsp
[2]);
4280 /* The message was sent, start the timer. */
4281 intf_start_seq_timer(intf
, msg
->msgid
);
4286 } else if (msg
->rsp_size
< 2) {
4287 /* Message is too small to be correct. */
4288 dev_warn(intf
->si_dev
,
4289 "BMC returned too small a message for netfn %x cmd %x, got %d bytes\n",
4290 (msg
->data
[0] >> 2) | 1, msg
->data
[1], msg
->rsp_size
);
4292 /* Generate an error response for the message. */
4293 msg
->rsp
[0] = msg
->data
[0] | (1 << 2);
4294 msg
->rsp
[1] = msg
->data
[1];
4295 msg
->rsp
[2] = IPMI_ERR_UNSPECIFIED
;
4297 } else if (((msg
->rsp
[0] >> 2) != ((msg
->data
[0] >> 2) | 1))
4298 || (msg
->rsp
[1] != msg
->data
[1])) {
4300 * The NetFN and Command in the response is not even
4301 * marginally correct.
4303 dev_warn(intf
->si_dev
,
4304 "BMC returned incorrect response, expected netfn %x cmd %x, got netfn %x cmd %x\n",
4305 (msg
->data
[0] >> 2) | 1, msg
->data
[1],
4306 msg
->rsp
[0] >> 2, msg
->rsp
[1]);
4308 /* Generate an error response for the message. */
4309 msg
->rsp
[0] = msg
->data
[0] | (1 << 2);
4310 msg
->rsp
[1] = msg
->data
[1];
4311 msg
->rsp
[2] = IPMI_ERR_UNSPECIFIED
;
4315 if ((msg
->rsp
[0] == ((IPMI_NETFN_APP_REQUEST
|1) << 2))
4316 && (msg
->rsp
[1] == IPMI_SEND_MSG_CMD
)
4317 && (msg
->user_data
!= NULL
)) {
4319 * It's a response to a response we sent. For this we
4320 * deliver a send message response to the user.
4322 struct ipmi_recv_msg
*recv_msg
= msg
->user_data
;
4325 if (msg
->rsp_size
< 2)
4326 /* Message is too small to be correct. */
4329 chan
= msg
->data
[2] & 0x0f;
4330 if (chan
>= IPMI_MAX_CHANNELS
)
4331 /* Invalid channel number */
4337 recv_msg
->recv_type
= IPMI_RESPONSE_RESPONSE_TYPE
;
4338 recv_msg
->msg
.data
= recv_msg
->msg_data
;
4339 recv_msg
->msg
.data_len
= 1;
4340 recv_msg
->msg_data
[0] = msg
->rsp
[2];
4341 deliver_local_response(intf
, recv_msg
);
4342 } else if ((msg
->rsp
[0] == ((IPMI_NETFN_APP_REQUEST
|1) << 2))
4343 && (msg
->rsp
[1] == IPMI_GET_MSG_CMD
)) {
4344 struct ipmi_channel
*chans
;
4346 /* It's from the receive queue. */
4347 chan
= msg
->rsp
[3] & 0xf;
4348 if (chan
>= IPMI_MAX_CHANNELS
) {
4349 /* Invalid channel number */
4355 * We need to make sure the channels have been initialized.
4356 * The channel_handler routine will set the "curr_channel"
4357 * equal to or greater than IPMI_MAX_CHANNELS when all the
4358 * channels for this interface have been initialized.
4360 if (!intf
->channels_ready
) {
4361 requeue
= 0; /* Throw the message away */
4365 chans
= READ_ONCE(intf
->channel_list
)->c
;
4367 switch (chans
[chan
].medium
) {
4368 case IPMI_CHANNEL_MEDIUM_IPMB
:
4369 if (msg
->rsp
[4] & 0x04) {
4371 * It's a response, so find the
4372 * requesting message and send it up.
4374 requeue
= handle_ipmb_get_msg_rsp(intf
, msg
);
4377 * It's a command to the SMS from some other
4378 * entity. Handle that.
4380 requeue
= handle_ipmb_get_msg_cmd(intf
, msg
);
4384 case IPMI_CHANNEL_MEDIUM_8023LAN
:
4385 case IPMI_CHANNEL_MEDIUM_ASYNC
:
4386 if (msg
->rsp
[6] & 0x04) {
4388 * It's a response, so find the
4389 * requesting message and send it up.
4391 requeue
= handle_lan_get_msg_rsp(intf
, msg
);
4394 * It's a command to the SMS from some other
4395 * entity. Handle that.
4397 requeue
= handle_lan_get_msg_cmd(intf
, msg
);
4402 /* Check for OEM Channels. Clients had better
4403 register for these commands. */
4404 if ((chans
[chan
].medium
>= IPMI_CHANNEL_MEDIUM_OEM_MIN
)
4405 && (chans
[chan
].medium
4406 <= IPMI_CHANNEL_MEDIUM_OEM_MAX
)) {
4407 requeue
= handle_oem_get_msg_cmd(intf
, msg
);
4410 * We don't handle the channel type, so just
4417 } else if ((msg
->rsp
[0] == ((IPMI_NETFN_APP_REQUEST
|1) << 2))
4418 && (msg
->rsp
[1] == IPMI_READ_EVENT_MSG_BUFFER_CMD
)) {
4419 /* It's an asynchronous event. */
4420 requeue
= handle_read_event_rsp(intf
, msg
);
4422 /* It's a response from the local BMC. */
4423 requeue
= handle_bmc_rsp(intf
, msg
);
4431 * If there are messages in the queue or pretimeouts, handle them.
4433 static void handle_new_recv_msgs(struct ipmi_smi
*intf
)
4435 struct ipmi_smi_msg
*smi_msg
;
4436 unsigned long flags
= 0;
4438 int run_to_completion
= intf
->run_to_completion
;
4440 /* See if any waiting messages need to be processed. */
4441 if (!run_to_completion
)
4442 spin_lock_irqsave(&intf
->waiting_rcv_msgs_lock
, flags
);
4443 while (!list_empty(&intf
->waiting_rcv_msgs
)) {
4444 smi_msg
= list_entry(intf
->waiting_rcv_msgs
.next
,
4445 struct ipmi_smi_msg
, link
);
4446 list_del(&smi_msg
->link
);
4447 if (!run_to_completion
)
4448 spin_unlock_irqrestore(&intf
->waiting_rcv_msgs_lock
,
4450 rv
= handle_one_recv_msg(intf
, smi_msg
);
4451 if (!run_to_completion
)
4452 spin_lock_irqsave(&intf
->waiting_rcv_msgs_lock
, flags
);
4455 * To preserve message order, quit if we
4456 * can't handle a message. Add the message
4457 * back at the head, this is safe because this
4458 * tasklet is the only thing that pulls the
4461 list_add(&smi_msg
->link
, &intf
->waiting_rcv_msgs
);
4465 /* Message handled */
4466 ipmi_free_smi_msg(smi_msg
);
4467 /* If rv < 0, fatal error, del but don't free. */
4470 if (!run_to_completion
)
4471 spin_unlock_irqrestore(&intf
->waiting_rcv_msgs_lock
, flags
);
4474 * If the pretimout count is non-zero, decrement one from it and
4475 * deliver pretimeouts to all the users.
4477 if (atomic_add_unless(&intf
->watchdog_pretimeouts_to_deliver
, -1, 0)) {
4478 struct ipmi_user
*user
;
4481 index
= srcu_read_lock(&intf
->users_srcu
);
4482 list_for_each_entry_rcu(user
, &intf
->users
, link
) {
4483 if (user
->handler
->ipmi_watchdog_pretimeout
)
4484 user
->handler
->ipmi_watchdog_pretimeout(
4485 user
->handler_data
);
4487 srcu_read_unlock(&intf
->users_srcu
, index
);
4491 static void smi_recv_tasklet(struct tasklet_struct
*t
)
4493 unsigned long flags
= 0; /* keep us warning-free. */
4494 struct ipmi_smi
*intf
= from_tasklet(intf
, t
, recv_tasklet
);
4495 int run_to_completion
= intf
->run_to_completion
;
4496 struct ipmi_smi_msg
*newmsg
= NULL
;
4499 * Start the next message if available.
4501 * Do this here, not in the actual receiver, because we may deadlock
4502 * because the lower layer is allowed to hold locks while calling
4508 if (!run_to_completion
)
4509 spin_lock_irqsave(&intf
->xmit_msgs_lock
, flags
);
4510 if (intf
->curr_msg
== NULL
&& !intf
->in_shutdown
) {
4511 struct list_head
*entry
= NULL
;
4513 /* Pick the high priority queue first. */
4514 if (!list_empty(&intf
->hp_xmit_msgs
))
4515 entry
= intf
->hp_xmit_msgs
.next
;
4516 else if (!list_empty(&intf
->xmit_msgs
))
4517 entry
= intf
->xmit_msgs
.next
;
4521 newmsg
= list_entry(entry
, struct ipmi_smi_msg
, link
);
4522 intf
->curr_msg
= newmsg
;
4526 if (!run_to_completion
)
4527 spin_unlock_irqrestore(&intf
->xmit_msgs_lock
, flags
);
4529 intf
->handlers
->sender(intf
->send_info
, newmsg
);
4533 handle_new_recv_msgs(intf
);
4536 /* Handle a new message from the lower layer. */
4537 void ipmi_smi_msg_received(struct ipmi_smi
*intf
,
4538 struct ipmi_smi_msg
*msg
)
4540 unsigned long flags
= 0; /* keep us warning-free. */
4541 int run_to_completion
= intf
->run_to_completion
;
4544 * To preserve message order, we keep a queue and deliver from
4547 if (!run_to_completion
)
4548 spin_lock_irqsave(&intf
->waiting_rcv_msgs_lock
, flags
);
4549 list_add_tail(&msg
->link
, &intf
->waiting_rcv_msgs
);
4550 if (!run_to_completion
)
4551 spin_unlock_irqrestore(&intf
->waiting_rcv_msgs_lock
,
4554 if (!run_to_completion
)
4555 spin_lock_irqsave(&intf
->xmit_msgs_lock
, flags
);
4557 * We can get an asynchronous event or receive message in addition
4558 * to commands we send.
4560 if (msg
== intf
->curr_msg
)
4561 intf
->curr_msg
= NULL
;
4562 if (!run_to_completion
)
4563 spin_unlock_irqrestore(&intf
->xmit_msgs_lock
, flags
);
4565 if (run_to_completion
)
4566 smi_recv_tasklet(&intf
->recv_tasklet
);
4568 tasklet_schedule(&intf
->recv_tasklet
);
4570 EXPORT_SYMBOL(ipmi_smi_msg_received
);
4572 void ipmi_smi_watchdog_pretimeout(struct ipmi_smi
*intf
)
4574 if (intf
->in_shutdown
)
4577 atomic_set(&intf
->watchdog_pretimeouts_to_deliver
, 1);
4578 tasklet_schedule(&intf
->recv_tasklet
);
4580 EXPORT_SYMBOL(ipmi_smi_watchdog_pretimeout
);
4582 static struct ipmi_smi_msg
*
4583 smi_from_recv_msg(struct ipmi_smi
*intf
, struct ipmi_recv_msg
*recv_msg
,
4584 unsigned char seq
, long seqid
)
4586 struct ipmi_smi_msg
*smi_msg
= ipmi_alloc_smi_msg();
4589 * If we can't allocate the message, then just return, we
4590 * get 4 retries, so this should be ok.
4594 memcpy(smi_msg
->data
, recv_msg
->msg
.data
, recv_msg
->msg
.data_len
);
4595 smi_msg
->data_size
= recv_msg
->msg
.data_len
;
4596 smi_msg
->msgid
= STORE_SEQ_IN_MSGID(seq
, seqid
);
4598 pr_debug("Resend: %*ph\n", smi_msg
->data_size
, smi_msg
->data
);
4603 static void check_msg_timeout(struct ipmi_smi
*intf
, struct seq_table
*ent
,
4604 struct list_head
*timeouts
,
4605 unsigned long timeout_period
,
4606 int slot
, unsigned long *flags
,
4609 struct ipmi_recv_msg
*msg
;
4611 if (intf
->in_shutdown
)
4617 if (timeout_period
< ent
->timeout
) {
4618 ent
->timeout
-= timeout_period
;
4623 if (ent
->retries_left
== 0) {
4624 /* The message has used all its retries. */
4626 smi_remove_watch(intf
, IPMI_WATCH_MASK_CHECK_MESSAGES
);
4627 msg
= ent
->recv_msg
;
4628 list_add_tail(&msg
->link
, timeouts
);
4630 ipmi_inc_stat(intf
, timed_out_ipmb_broadcasts
);
4631 else if (is_lan_addr(&ent
->recv_msg
->addr
))
4632 ipmi_inc_stat(intf
, timed_out_lan_commands
);
4634 ipmi_inc_stat(intf
, timed_out_ipmb_commands
);
4636 struct ipmi_smi_msg
*smi_msg
;
4637 /* More retries, send again. */
4642 * Start with the max timer, set to normal timer after
4643 * the message is sent.
4645 ent
->timeout
= MAX_MSG_TIMEOUT
;
4646 ent
->retries_left
--;
4647 smi_msg
= smi_from_recv_msg(intf
, ent
->recv_msg
, slot
,
4650 if (is_lan_addr(&ent
->recv_msg
->addr
))
4652 dropped_rexmit_lan_commands
);
4655 dropped_rexmit_ipmb_commands
);
4659 spin_unlock_irqrestore(&intf
->seq_lock
, *flags
);
4662 * Send the new message. We send with a zero
4663 * priority. It timed out, I doubt time is that
4664 * critical now, and high priority messages are really
4665 * only for messages to the local MC, which don't get
4668 if (intf
->handlers
) {
4669 if (is_lan_addr(&ent
->recv_msg
->addr
))
4671 retransmitted_lan_commands
);
4674 retransmitted_ipmb_commands
);
4676 smi_send(intf
, intf
->handlers
, smi_msg
, 0);
4678 ipmi_free_smi_msg(smi_msg
);
4680 spin_lock_irqsave(&intf
->seq_lock
, *flags
);
4684 static bool ipmi_timeout_handler(struct ipmi_smi
*intf
,
4685 unsigned long timeout_period
)
4687 struct list_head timeouts
;
4688 struct ipmi_recv_msg
*msg
, *msg2
;
4689 unsigned long flags
;
4691 bool need_timer
= false;
4693 if (!intf
->bmc_registered
) {
4694 kref_get(&intf
->refcount
);
4695 if (!schedule_work(&intf
->bmc_reg_work
)) {
4696 kref_put(&intf
->refcount
, intf_free
);
4702 * Go through the seq table and find any messages that
4703 * have timed out, putting them in the timeouts
4706 INIT_LIST_HEAD(&timeouts
);
4707 spin_lock_irqsave(&intf
->seq_lock
, flags
);
4708 if (intf
->ipmb_maintenance_mode_timeout
) {
4709 if (intf
->ipmb_maintenance_mode_timeout
<= timeout_period
)
4710 intf
->ipmb_maintenance_mode_timeout
= 0;
4712 intf
->ipmb_maintenance_mode_timeout
-= timeout_period
;
4714 for (i
= 0; i
< IPMI_IPMB_NUM_SEQ
; i
++)
4715 check_msg_timeout(intf
, &intf
->seq_table
[i
],
4716 &timeouts
, timeout_period
, i
,
4717 &flags
, &need_timer
);
4718 spin_unlock_irqrestore(&intf
->seq_lock
, flags
);
4720 list_for_each_entry_safe(msg
, msg2
, &timeouts
, link
)
4721 deliver_err_response(intf
, msg
, IPMI_TIMEOUT_COMPLETION_CODE
);
4724 * Maintenance mode handling. Check the timeout
4725 * optimistically before we claim the lock. It may
4726 * mean a timeout gets missed occasionally, but that
4727 * only means the timeout gets extended by one period
4728 * in that case. No big deal, and it avoids the lock
4731 if (intf
->auto_maintenance_timeout
> 0) {
4732 spin_lock_irqsave(&intf
->maintenance_mode_lock
, flags
);
4733 if (intf
->auto_maintenance_timeout
> 0) {
4734 intf
->auto_maintenance_timeout
4736 if (!intf
->maintenance_mode
4737 && (intf
->auto_maintenance_timeout
<= 0)) {
4738 intf
->maintenance_mode_enable
= false;
4739 maintenance_mode_update(intf
);
4742 spin_unlock_irqrestore(&intf
->maintenance_mode_lock
,
4746 tasklet_schedule(&intf
->recv_tasklet
);
4751 static void ipmi_request_event(struct ipmi_smi
*intf
)
4753 /* No event requests when in maintenance mode. */
4754 if (intf
->maintenance_mode_enable
)
4757 if (!intf
->in_shutdown
)
4758 intf
->handlers
->request_events(intf
->send_info
);
4761 static struct timer_list ipmi_timer
;
4763 static atomic_t stop_operation
;
4765 static void ipmi_timeout(struct timer_list
*unused
)
4767 struct ipmi_smi
*intf
;
4768 bool need_timer
= false;
4771 if (atomic_read(&stop_operation
))
4774 index
= srcu_read_lock(&ipmi_interfaces_srcu
);
4775 list_for_each_entry_rcu(intf
, &ipmi_interfaces
, link
) {
4776 if (atomic_read(&intf
->event_waiters
)) {
4777 intf
->ticks_to_req_ev
--;
4778 if (intf
->ticks_to_req_ev
== 0) {
4779 ipmi_request_event(intf
);
4780 intf
->ticks_to_req_ev
= IPMI_REQUEST_EV_TIME
;
4785 need_timer
|= ipmi_timeout_handler(intf
, IPMI_TIMEOUT_TIME
);
4787 srcu_read_unlock(&ipmi_interfaces_srcu
, index
);
4790 mod_timer(&ipmi_timer
, jiffies
+ IPMI_TIMEOUT_JIFFIES
);
4793 static void need_waiter(struct ipmi_smi
*intf
)
4795 /* Racy, but worst case we start the timer twice. */
4796 if (!timer_pending(&ipmi_timer
))
4797 mod_timer(&ipmi_timer
, jiffies
+ IPMI_TIMEOUT_JIFFIES
);
4800 static atomic_t smi_msg_inuse_count
= ATOMIC_INIT(0);
4801 static atomic_t recv_msg_inuse_count
= ATOMIC_INIT(0);
4803 static void free_smi_msg(struct ipmi_smi_msg
*msg
)
4805 atomic_dec(&smi_msg_inuse_count
);
4809 struct ipmi_smi_msg
*ipmi_alloc_smi_msg(void)
4811 struct ipmi_smi_msg
*rv
;
4812 rv
= kmalloc(sizeof(struct ipmi_smi_msg
), GFP_ATOMIC
);
4814 rv
->done
= free_smi_msg
;
4815 rv
->user_data
= NULL
;
4816 atomic_inc(&smi_msg_inuse_count
);
4820 EXPORT_SYMBOL(ipmi_alloc_smi_msg
);
4822 static void free_recv_msg(struct ipmi_recv_msg
*msg
)
4824 atomic_dec(&recv_msg_inuse_count
);
4828 static struct ipmi_recv_msg
*ipmi_alloc_recv_msg(void)
4830 struct ipmi_recv_msg
*rv
;
4832 rv
= kmalloc(sizeof(struct ipmi_recv_msg
), GFP_ATOMIC
);
4835 rv
->done
= free_recv_msg
;
4836 atomic_inc(&recv_msg_inuse_count
);
4841 void ipmi_free_recv_msg(struct ipmi_recv_msg
*msg
)
4844 kref_put(&msg
->user
->refcount
, free_user
);
4847 EXPORT_SYMBOL(ipmi_free_recv_msg
);
4849 static atomic_t panic_done_count
= ATOMIC_INIT(0);
4851 static void dummy_smi_done_handler(struct ipmi_smi_msg
*msg
)
4853 atomic_dec(&panic_done_count
);
4856 static void dummy_recv_done_handler(struct ipmi_recv_msg
*msg
)
4858 atomic_dec(&panic_done_count
);
4862 * Inside a panic, send a message and wait for a response.
4864 static void ipmi_panic_request_and_wait(struct ipmi_smi
*intf
,
4865 struct ipmi_addr
*addr
,
4866 struct kernel_ipmi_msg
*msg
)
4868 struct ipmi_smi_msg smi_msg
;
4869 struct ipmi_recv_msg recv_msg
;
4872 smi_msg
.done
= dummy_smi_done_handler
;
4873 recv_msg
.done
= dummy_recv_done_handler
;
4874 atomic_add(2, &panic_done_count
);
4875 rv
= i_ipmi_request(NULL
,
4884 intf
->addrinfo
[0].address
,
4885 intf
->addrinfo
[0].lun
,
4886 0, 1); /* Don't retry, and don't wait. */
4888 atomic_sub(2, &panic_done_count
);
4889 else if (intf
->handlers
->flush_messages
)
4890 intf
->handlers
->flush_messages(intf
->send_info
);
4892 while (atomic_read(&panic_done_count
) != 0)
4896 static void event_receiver_fetcher(struct ipmi_smi
*intf
,
4897 struct ipmi_recv_msg
*msg
)
4899 if ((msg
->addr
.addr_type
== IPMI_SYSTEM_INTERFACE_ADDR_TYPE
)
4900 && (msg
->msg
.netfn
== IPMI_NETFN_SENSOR_EVENT_RESPONSE
)
4901 && (msg
->msg
.cmd
== IPMI_GET_EVENT_RECEIVER_CMD
)
4902 && (msg
->msg
.data
[0] == IPMI_CC_NO_ERROR
)) {
4903 /* A get event receiver command, save it. */
4904 intf
->event_receiver
= msg
->msg
.data
[1];
4905 intf
->event_receiver_lun
= msg
->msg
.data
[2] & 0x3;
4909 static void device_id_fetcher(struct ipmi_smi
*intf
, struct ipmi_recv_msg
*msg
)
4911 if ((msg
->addr
.addr_type
== IPMI_SYSTEM_INTERFACE_ADDR_TYPE
)
4912 && (msg
->msg
.netfn
== IPMI_NETFN_APP_RESPONSE
)
4913 && (msg
->msg
.cmd
== IPMI_GET_DEVICE_ID_CMD
)
4914 && (msg
->msg
.data
[0] == IPMI_CC_NO_ERROR
)) {
4916 * A get device id command, save if we are an event
4917 * receiver or generator.
4919 intf
->local_sel_device
= (msg
->msg
.data
[6] >> 2) & 1;
4920 intf
->local_event_generator
= (msg
->msg
.data
[6] >> 5) & 1;
4924 static void send_panic_events(struct ipmi_smi
*intf
, char *str
)
4926 struct kernel_ipmi_msg msg
;
4927 unsigned char data
[16];
4928 struct ipmi_system_interface_addr
*si
;
4929 struct ipmi_addr addr
;
4931 struct ipmi_ipmb_addr
*ipmb
;
4934 if (ipmi_send_panic_event
== IPMI_SEND_PANIC_EVENT_NONE
)
4937 si
= (struct ipmi_system_interface_addr
*) &addr
;
4938 si
->addr_type
= IPMI_SYSTEM_INTERFACE_ADDR_TYPE
;
4939 si
->channel
= IPMI_BMC_CHANNEL
;
4942 /* Fill in an event telling that we have failed. */
4943 msg
.netfn
= 0x04; /* Sensor or Event. */
4944 msg
.cmd
= 2; /* Platform event command. */
4947 data
[0] = 0x41; /* Kernel generator ID, IPMI table 5-4 */
4948 data
[1] = 0x03; /* This is for IPMI 1.0. */
4949 data
[2] = 0x20; /* OS Critical Stop, IPMI table 36-3 */
4950 data
[4] = 0x6f; /* Sensor specific, IPMI table 36-1 */
4951 data
[5] = 0xa1; /* Runtime stop OEM bytes 2 & 3. */
4954 * Put a few breadcrumbs in. Hopefully later we can add more things
4955 * to make the panic events more useful.
4963 /* Send the event announcing the panic. */
4964 ipmi_panic_request_and_wait(intf
, &addr
, &msg
);
4967 * On every interface, dump a bunch of OEM event holding the
4970 if (ipmi_send_panic_event
!= IPMI_SEND_PANIC_EVENT_STRING
|| !str
)
4974 * intf_num is used as an marker to tell if the
4975 * interface is valid. Thus we need a read barrier to
4976 * make sure data fetched before checking intf_num
4982 * First job here is to figure out where to send the
4983 * OEM events. There's no way in IPMI to send OEM
4984 * events using an event send command, so we have to
4985 * find the SEL to put them in and stick them in
4989 /* Get capabilities from the get device id. */
4990 intf
->local_sel_device
= 0;
4991 intf
->local_event_generator
= 0;
4992 intf
->event_receiver
= 0;
4994 /* Request the device info from the local MC. */
4995 msg
.netfn
= IPMI_NETFN_APP_REQUEST
;
4996 msg
.cmd
= IPMI_GET_DEVICE_ID_CMD
;
4999 intf
->null_user_handler
= device_id_fetcher
;
5000 ipmi_panic_request_and_wait(intf
, &addr
, &msg
);
5002 if (intf
->local_event_generator
) {
5003 /* Request the event receiver from the local MC. */
5004 msg
.netfn
= IPMI_NETFN_SENSOR_EVENT_REQUEST
;
5005 msg
.cmd
= IPMI_GET_EVENT_RECEIVER_CMD
;
5008 intf
->null_user_handler
= event_receiver_fetcher
;
5009 ipmi_panic_request_and_wait(intf
, &addr
, &msg
);
5011 intf
->null_user_handler
= NULL
;
5014 * Validate the event receiver. The low bit must not
5015 * be 1 (it must be a valid IPMB address), it cannot
5016 * be zero, and it must not be my address.
5018 if (((intf
->event_receiver
& 1) == 0)
5019 && (intf
->event_receiver
!= 0)
5020 && (intf
->event_receiver
!= intf
->addrinfo
[0].address
)) {
5022 * The event receiver is valid, send an IPMB
5025 ipmb
= (struct ipmi_ipmb_addr
*) &addr
;
5026 ipmb
->addr_type
= IPMI_IPMB_ADDR_TYPE
;
5027 ipmb
->channel
= 0; /* FIXME - is this right? */
5028 ipmb
->lun
= intf
->event_receiver_lun
;
5029 ipmb
->slave_addr
= intf
->event_receiver
;
5030 } else if (intf
->local_sel_device
) {
5032 * The event receiver was not valid (or was
5033 * me), but I am an SEL device, just dump it
5036 si
= (struct ipmi_system_interface_addr
*) &addr
;
5037 si
->addr_type
= IPMI_SYSTEM_INTERFACE_ADDR_TYPE
;
5038 si
->channel
= IPMI_BMC_CHANNEL
;
5041 return; /* No where to send the event. */
5043 msg
.netfn
= IPMI_NETFN_STORAGE_REQUEST
; /* Storage. */
5044 msg
.cmd
= IPMI_ADD_SEL_ENTRY_CMD
;
5050 int size
= strlen(p
);
5056 data
[2] = 0xf0; /* OEM event without timestamp. */
5057 data
[3] = intf
->addrinfo
[0].address
;
5058 data
[4] = j
++; /* sequence # */
5060 * Always give 11 bytes, so strncpy will fill
5061 * it with zeroes for me.
5063 strncpy(data
+5, p
, 11);
5066 ipmi_panic_request_and_wait(intf
, &addr
, &msg
);
5070 static int has_panicked
;
5072 static int panic_event(struct notifier_block
*this,
5073 unsigned long event
,
5076 struct ipmi_smi
*intf
;
5077 struct ipmi_user
*user
;
5083 /* For every registered interface, set it to run to completion. */
5084 list_for_each_entry_rcu(intf
, &ipmi_interfaces
, link
) {
5085 if (!intf
->handlers
|| intf
->intf_num
== -1)
5086 /* Interface is not ready. */
5089 if (!intf
->handlers
->poll
)
5093 * If we were interrupted while locking xmit_msgs_lock or
5094 * waiting_rcv_msgs_lock, the corresponding list may be
5095 * corrupted. In this case, drop items on the list for
5098 if (!spin_trylock(&intf
->xmit_msgs_lock
)) {
5099 INIT_LIST_HEAD(&intf
->xmit_msgs
);
5100 INIT_LIST_HEAD(&intf
->hp_xmit_msgs
);
5102 spin_unlock(&intf
->xmit_msgs_lock
);
5104 if (!spin_trylock(&intf
->waiting_rcv_msgs_lock
))
5105 INIT_LIST_HEAD(&intf
->waiting_rcv_msgs
);
5107 spin_unlock(&intf
->waiting_rcv_msgs_lock
);
5109 intf
->run_to_completion
= 1;
5110 if (intf
->handlers
->set_run_to_completion
)
5111 intf
->handlers
->set_run_to_completion(intf
->send_info
,
5114 list_for_each_entry_rcu(user
, &intf
->users
, link
) {
5115 if (user
->handler
->ipmi_panic_handler
)
5116 user
->handler
->ipmi_panic_handler(
5117 user
->handler_data
);
5120 send_panic_events(intf
, ptr
);
5126 /* Must be called with ipmi_interfaces_mutex held. */
5127 static int ipmi_register_driver(void)
5134 rv
= driver_register(&ipmidriver
.driver
);
5136 pr_err("Could not register IPMI driver\n");
5138 drvregistered
= true;
5142 static struct notifier_block panic_block
= {
5143 .notifier_call
= panic_event
,
5145 .priority
= 200 /* priority: INT_MAX >= x >= 0 */
5148 static int ipmi_init_msghandler(void)
5152 mutex_lock(&ipmi_interfaces_mutex
);
5153 rv
= ipmi_register_driver();
5159 init_srcu_struct(&ipmi_interfaces_srcu
);
5161 timer_setup(&ipmi_timer
, ipmi_timeout
, 0);
5162 mod_timer(&ipmi_timer
, jiffies
+ IPMI_TIMEOUT_JIFFIES
);
5164 atomic_notifier_chain_register(&panic_notifier_list
, &panic_block
);
5169 mutex_unlock(&ipmi_interfaces_mutex
);
5173 static int __init
ipmi_init_msghandler_mod(void)
5177 pr_info("version " IPMI_DRIVER_VERSION
"\n");
5179 mutex_lock(&ipmi_interfaces_mutex
);
5180 rv
= ipmi_register_driver();
5181 mutex_unlock(&ipmi_interfaces_mutex
);
5186 static void __exit
cleanup_ipmi(void)
5191 atomic_notifier_chain_unregister(&panic_notifier_list
,
5195 * This can't be called if any interfaces exist, so no worry
5196 * about shutting down the interfaces.
5200 * Tell the timer to stop, then wait for it to stop. This
5201 * avoids problems with race conditions removing the timer
5204 atomic_set(&stop_operation
, 1);
5205 del_timer_sync(&ipmi_timer
);
5207 initialized
= false;
5209 /* Check for buffer leaks. */
5210 count
= atomic_read(&smi_msg_inuse_count
);
5212 pr_warn("SMI message count %d at exit\n", count
);
5213 count
= atomic_read(&recv_msg_inuse_count
);
5215 pr_warn("recv message count %d at exit\n", count
);
5217 cleanup_srcu_struct(&ipmi_interfaces_srcu
);
5220 driver_unregister(&ipmidriver
.driver
);
5222 module_exit(cleanup_ipmi
);
5224 module_init(ipmi_init_msghandler_mod
);
5225 MODULE_LICENSE("GPL");
5226 MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
5227 MODULE_DESCRIPTION("Incoming and outgoing message routing for an IPMI"
5229 MODULE_VERSION(IPMI_DRIVER_VERSION
);
5230 MODULE_SOFTDEP("post: ipmi_devintf");