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/panic_notifier.h>
20 #include <linux/poll.h>
21 #include <linux/sched.h>
22 #include <linux/seq_file.h>
23 #include <linux/spinlock.h>
24 #include <linux/mutex.h>
25 #include <linux/slab.h>
26 #include <linux/ipmi.h>
27 #include <linux/ipmi_smi.h>
28 #include <linux/notifier.h>
29 #include <linux/init.h>
30 #include <linux/proc_fs.h>
31 #include <linux/rcupdate.h>
32 #include <linux/interrupt.h>
33 #include <linux/moduleparam.h>
34 #include <linux/workqueue.h>
35 #include <linux/uuid.h>
36 #include <linux/nospec.h>
37 #include <linux/vmalloc.h>
38 #include <linux/delay.h>
40 #define IPMI_DRIVER_VERSION "39.2"
42 static struct ipmi_recv_msg *ipmi_alloc_recv_msg(void);
43 static int ipmi_init_msghandler(void);
44 static void smi_recv_tasklet(struct tasklet_struct *t);
45 static void handle_new_recv_msgs(struct ipmi_smi *intf);
46 static void need_waiter(struct ipmi_smi *intf);
47 static int handle_one_recv_msg(struct ipmi_smi *intf,
48 struct ipmi_smi_msg *msg);
50 static bool initialized;
51 static bool drvregistered;
53 /* Numbers in this enumerator should be mapped to ipmi_panic_event_str */
54 enum ipmi_panic_event_op {
55 IPMI_SEND_PANIC_EVENT_NONE,
56 IPMI_SEND_PANIC_EVENT,
57 IPMI_SEND_PANIC_EVENT_STRING,
58 IPMI_SEND_PANIC_EVENT_MAX
61 /* Indices in this array should be mapped to enum ipmi_panic_event_op */
62 static const char *const ipmi_panic_event_str[] = { "none", "event", "string", NULL };
64 #ifdef CONFIG_IPMI_PANIC_STRING
65 #define IPMI_PANIC_DEFAULT IPMI_SEND_PANIC_EVENT_STRING
66 #elif defined(CONFIG_IPMI_PANIC_EVENT)
67 #define IPMI_PANIC_DEFAULT IPMI_SEND_PANIC_EVENT
69 #define IPMI_PANIC_DEFAULT IPMI_SEND_PANIC_EVENT_NONE
72 static enum ipmi_panic_event_op ipmi_send_panic_event = IPMI_PANIC_DEFAULT;
74 static int panic_op_write_handler(const char *val,
75 const struct kernel_param *kp)
80 strscpy(valcp, val, sizeof(valcp));
81 e = match_string(ipmi_panic_event_str, -1, strstrip(valcp));
85 ipmi_send_panic_event = e;
89 static int panic_op_read_handler(char *buffer, const struct kernel_param *kp)
91 const char *event_str;
93 if (ipmi_send_panic_event >= IPMI_SEND_PANIC_EVENT_MAX)
96 event_str = ipmi_panic_event_str[ipmi_send_panic_event];
98 return sprintf(buffer, "%s\n", event_str);
101 static const struct kernel_param_ops panic_op_ops = {
102 .set = panic_op_write_handler,
103 .get = panic_op_read_handler
105 module_param_cb(panic_op, &panic_op_ops, NULL, 0600);
106 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.");
109 #define MAX_EVENTS_IN_QUEUE 25
111 /* Remain in auto-maintenance mode for this amount of time (in ms). */
112 static unsigned long maintenance_mode_timeout_ms = 30000;
113 module_param(maintenance_mode_timeout_ms, ulong, 0644);
114 MODULE_PARM_DESC(maintenance_mode_timeout_ms,
115 "The time (milliseconds) after the last maintenance message that the connection stays in maintenance mode.");
118 * Don't let a message sit in a queue forever, always time it with at lest
119 * the max message timer. This is in milliseconds.
121 #define MAX_MSG_TIMEOUT 60000
124 * Timeout times below are in milliseconds, and are done off a 1
125 * second timer. So setting the value to 1000 would mean anything
126 * between 0 and 1000ms. So really the only reasonable minimum
127 * setting it 2000ms, which is between 1 and 2 seconds.
130 /* The default timeout for message retries. */
131 static unsigned long default_retry_ms = 2000;
132 module_param(default_retry_ms, ulong, 0644);
133 MODULE_PARM_DESC(default_retry_ms,
134 "The time (milliseconds) between retry sends");
136 /* The default timeout for maintenance mode message retries. */
137 static unsigned long default_maintenance_retry_ms = 3000;
138 module_param(default_maintenance_retry_ms, ulong, 0644);
139 MODULE_PARM_DESC(default_maintenance_retry_ms,
140 "The time (milliseconds) between retry sends in maintenance mode");
142 /* The default maximum number of retries */
143 static unsigned int default_max_retries = 4;
144 module_param(default_max_retries, uint, 0644);
145 MODULE_PARM_DESC(default_max_retries,
146 "The time (milliseconds) between retry sends in maintenance mode");
148 /* Call every ~1000 ms. */
149 #define IPMI_TIMEOUT_TIME 1000
151 /* How many jiffies does it take to get to the timeout time. */
152 #define IPMI_TIMEOUT_JIFFIES ((IPMI_TIMEOUT_TIME * HZ) / 1000)
155 * Request events from the queue every second (this is the number of
156 * IPMI_TIMEOUT_TIMES between event requests). Hopefully, in the
157 * future, IPMI will add a way to know immediately if an event is in
158 * the queue and this silliness can go away.
160 #define IPMI_REQUEST_EV_TIME (1000 / (IPMI_TIMEOUT_TIME))
162 /* How long should we cache dynamic device IDs? */
163 #define IPMI_DYN_DEV_ID_EXPIRY (10 * HZ)
166 * The main "user" data structure.
169 struct list_head link;
172 * Set to NULL when the user is destroyed, a pointer to myself
173 * so srcu_dereference can be used on it.
175 struct ipmi_user *self;
176 struct srcu_struct release_barrier;
178 struct kref refcount;
180 /* The upper layer that handles receive messages. */
181 const struct ipmi_user_hndl *handler;
184 /* The interface this user is bound to. */
185 struct ipmi_smi *intf;
187 /* Does this interface receive IPMI events? */
190 /* Free must run in process context for RCU cleanup. */
191 struct work_struct remove_work;
194 static struct ipmi_user *acquire_ipmi_user(struct ipmi_user *user, int *index)
195 __acquires(user->release_barrier)
197 struct ipmi_user *ruser;
199 *index = srcu_read_lock(&user->release_barrier);
200 ruser = srcu_dereference(user->self, &user->release_barrier);
202 srcu_read_unlock(&user->release_barrier, *index);
206 static void release_ipmi_user(struct ipmi_user *user, int index)
208 srcu_read_unlock(&user->release_barrier, index);
212 struct list_head link;
214 struct ipmi_user *user;
220 * This is used to form a linked lised during mass deletion.
221 * Since this is in an RCU list, we cannot use the link above
222 * or change any data until the RCU period completes. So we
223 * use this next variable during mass deletion so we can have
224 * a list and don't have to wait and restart the search on
225 * every individual deletion of a command.
227 struct cmd_rcvr *next;
231 unsigned int inuse : 1;
232 unsigned int broadcast : 1;
234 unsigned long timeout;
235 unsigned long orig_timeout;
236 unsigned int retries_left;
239 * To verify on an incoming send message response that this is
240 * the message that the response is for, we keep a sequence id
241 * and increment it every time we send a message.
246 * This is held so we can properly respond to the message on a
247 * timeout, and it is used to hold the temporary data for
248 * retransmission, too.
250 struct ipmi_recv_msg *recv_msg;
254 * Store the information in a msgid (long) to allow us to find a
255 * sequence table entry from the msgid.
257 #define STORE_SEQ_IN_MSGID(seq, seqid) \
258 ((((seq) & 0x3f) << 26) | ((seqid) & 0x3ffffff))
260 #define GET_SEQ_FROM_MSGID(msgid, seq, seqid) \
262 seq = (((msgid) >> 26) & 0x3f); \
263 seqid = ((msgid) & 0x3ffffff); \
266 #define NEXT_SEQID(seqid) (((seqid) + 1) & 0x3ffffff)
268 #define IPMI_MAX_CHANNELS 16
269 struct ipmi_channel {
270 unsigned char medium;
271 unsigned char protocol;
274 struct ipmi_channel_set {
275 struct ipmi_channel c[IPMI_MAX_CHANNELS];
278 struct ipmi_my_addrinfo {
280 * My slave address. This is initialized to IPMI_BMC_SLAVE_ADDR,
281 * but may be changed by the user.
283 unsigned char address;
286 * My LUN. This should generally stay the SMS LUN, but just in
293 * Note that the product id, manufacturer id, guid, and device id are
294 * immutable in this structure, so dyn_mutex is not required for
295 * accessing those. If those change on a BMC, a new BMC is allocated.
298 struct platform_device pdev;
299 struct list_head intfs; /* Interfaces on this BMC. */
300 struct ipmi_device_id id;
301 struct ipmi_device_id fetch_id;
303 unsigned long dyn_id_expiry;
304 struct mutex dyn_mutex; /* Protects id, intfs, & dyn* */
308 struct kref usecount;
309 struct work_struct remove_work;
310 unsigned char cc; /* completion code */
312 #define to_bmc_device(x) container_of((x), struct bmc_device, pdev.dev)
314 static int bmc_get_device_id(struct ipmi_smi *intf, struct bmc_device *bmc,
315 struct ipmi_device_id *id,
316 bool *guid_set, guid_t *guid);
319 * Various statistics for IPMI, these index stats[] in the ipmi_smi
322 enum ipmi_stat_indexes {
323 /* Commands we got from the user that were invalid. */
324 IPMI_STAT_sent_invalid_commands = 0,
326 /* Commands we sent to the MC. */
327 IPMI_STAT_sent_local_commands,
329 /* Responses from the MC that were delivered to a user. */
330 IPMI_STAT_handled_local_responses,
332 /* Responses from the MC that were not delivered to a user. */
333 IPMI_STAT_unhandled_local_responses,
335 /* Commands we sent out to the IPMB bus. */
336 IPMI_STAT_sent_ipmb_commands,
338 /* Commands sent on the IPMB that had errors on the SEND CMD */
339 IPMI_STAT_sent_ipmb_command_errs,
341 /* Each retransmit increments this count. */
342 IPMI_STAT_retransmitted_ipmb_commands,
345 * When a message times out (runs out of retransmits) this is
348 IPMI_STAT_timed_out_ipmb_commands,
351 * This is like above, but for broadcasts. Broadcasts are
352 * *not* included in the above count (they are expected to
355 IPMI_STAT_timed_out_ipmb_broadcasts,
357 /* Responses I have sent to the IPMB bus. */
358 IPMI_STAT_sent_ipmb_responses,
360 /* The response was delivered to the user. */
361 IPMI_STAT_handled_ipmb_responses,
363 /* The response had invalid data in it. */
364 IPMI_STAT_invalid_ipmb_responses,
366 /* The response didn't have anyone waiting for it. */
367 IPMI_STAT_unhandled_ipmb_responses,
369 /* Commands we sent out to the IPMB bus. */
370 IPMI_STAT_sent_lan_commands,
372 /* Commands sent on the IPMB that had errors on the SEND CMD */
373 IPMI_STAT_sent_lan_command_errs,
375 /* Each retransmit increments this count. */
376 IPMI_STAT_retransmitted_lan_commands,
379 * When a message times out (runs out of retransmits) this is
382 IPMI_STAT_timed_out_lan_commands,
384 /* Responses I have sent to the IPMB bus. */
385 IPMI_STAT_sent_lan_responses,
387 /* The response was delivered to the user. */
388 IPMI_STAT_handled_lan_responses,
390 /* The response had invalid data in it. */
391 IPMI_STAT_invalid_lan_responses,
393 /* The response didn't have anyone waiting for it. */
394 IPMI_STAT_unhandled_lan_responses,
396 /* The command was delivered to the user. */
397 IPMI_STAT_handled_commands,
399 /* The command had invalid data in it. */
400 IPMI_STAT_invalid_commands,
402 /* The command didn't have anyone waiting for it. */
403 IPMI_STAT_unhandled_commands,
405 /* Invalid data in an event. */
406 IPMI_STAT_invalid_events,
408 /* Events that were received with the proper format. */
411 /* Retransmissions on IPMB that failed. */
412 IPMI_STAT_dropped_rexmit_ipmb_commands,
414 /* Retransmissions on LAN that failed. */
415 IPMI_STAT_dropped_rexmit_lan_commands,
417 /* This *must* remain last, add new values above this. */
422 #define IPMI_IPMB_NUM_SEQ 64
424 struct module *owner;
426 /* What interface number are we? */
429 struct kref refcount;
431 /* Set when the interface is being unregistered. */
434 /* Used for a list of interfaces. */
435 struct list_head link;
438 * The list of upper layers that are using me. seq_lock write
439 * protects this. Read protection is with srcu.
441 struct list_head users;
442 struct srcu_struct users_srcu;
444 /* Used for wake ups at startup. */
445 wait_queue_head_t waitq;
448 * Prevents the interface from being unregistered when the
449 * interface is used by being looked up through the BMC
452 struct mutex bmc_reg_mutex;
454 struct bmc_device tmp_bmc;
455 struct bmc_device *bmc;
457 struct list_head bmc_link;
459 bool in_bmc_register; /* Handle recursive situations. Yuck. */
460 struct work_struct bmc_reg_work;
462 const struct ipmi_smi_handlers *handlers;
465 /* Driver-model device for the system interface. */
466 struct device *si_dev;
469 * A table of sequence numbers for this interface. We use the
470 * sequence numbers for IPMB messages that go out of the
471 * interface to match them up with their responses. A routine
472 * is called periodically to time the items in this list.
475 struct seq_table seq_table[IPMI_IPMB_NUM_SEQ];
479 * Messages queued for delivery. If delivery fails (out of memory
480 * for instance), They will stay in here to be processed later in a
481 * periodic timer interrupt. The tasklet is for handling received
482 * messages directly from the handler.
484 spinlock_t waiting_rcv_msgs_lock;
485 struct list_head waiting_rcv_msgs;
486 atomic_t watchdog_pretimeouts_to_deliver;
487 struct tasklet_struct recv_tasklet;
489 spinlock_t xmit_msgs_lock;
490 struct list_head xmit_msgs;
491 struct ipmi_smi_msg *curr_msg;
492 struct list_head hp_xmit_msgs;
495 * The list of command receivers that are registered for commands
498 struct mutex cmd_rcvrs_mutex;
499 struct list_head cmd_rcvrs;
502 * Events that were queues because no one was there to receive
505 spinlock_t events_lock; /* For dealing with event stuff. */
506 struct list_head waiting_events;
507 unsigned int waiting_events_count; /* How many events in queue? */
508 char delivering_events;
509 char event_msg_printed;
511 /* How many users are waiting for events? */
512 atomic_t event_waiters;
513 unsigned int ticks_to_req_ev;
515 spinlock_t watch_lock; /* For dealing with watch stuff below. */
517 /* How many users are waiting for commands? */
518 unsigned int command_waiters;
520 /* How many users are waiting for watchdogs? */
521 unsigned int watchdog_waiters;
523 /* How many users are waiting for message responses? */
524 unsigned int response_waiters;
527 * Tells what the lower layer has last been asked to watch for,
528 * messages and/or watchdogs. Protected by watch_lock.
530 unsigned int last_watch_mask;
533 * The event receiver for my BMC, only really used at panic
534 * shutdown as a place to store this.
536 unsigned char event_receiver;
537 unsigned char event_receiver_lun;
538 unsigned char local_sel_device;
539 unsigned char local_event_generator;
541 /* For handling of maintenance mode. */
542 int maintenance_mode;
543 bool maintenance_mode_enable;
544 int auto_maintenance_timeout;
545 spinlock_t maintenance_mode_lock; /* Used in a timer... */
548 * If we are doing maintenance on something on IPMB, extend
549 * the timeout time to avoid timeouts writing firmware and
552 int ipmb_maintenance_mode_timeout;
555 * A cheap hack, if this is non-null and a message to an
556 * interface comes in with a NULL user, call this routine with
557 * it. Note that the message will still be freed by the
558 * caller. This only works on the system interface.
560 * Protected by bmc_reg_mutex.
562 void (*null_user_handler)(struct ipmi_smi *intf,
563 struct ipmi_recv_msg *msg);
566 * When we are scanning the channels for an SMI, this will
567 * tell which channel we are scanning.
571 /* Channel information */
572 struct ipmi_channel_set *channel_list;
573 unsigned int curr_working_cset; /* First index into the following. */
574 struct ipmi_channel_set wchannels[2];
575 struct ipmi_my_addrinfo addrinfo[IPMI_MAX_CHANNELS];
578 atomic_t stats[IPMI_NUM_STATS];
581 * run_to_completion duplicate of smb_info, smi_info
582 * and ipmi_serial_info structures. Used to decrease numbers of
583 * parameters passed by "low" level IPMI code.
585 int run_to_completion;
587 #define to_si_intf_from_dev(device) container_of(device, struct ipmi_smi, dev)
589 static void __get_guid(struct ipmi_smi *intf);
590 static void __ipmi_bmc_unregister(struct ipmi_smi *intf);
591 static int __ipmi_bmc_register(struct ipmi_smi *intf,
592 struct ipmi_device_id *id,
593 bool guid_set, guid_t *guid, int intf_num);
594 static int __scan_channels(struct ipmi_smi *intf, struct ipmi_device_id *id);
598 * The driver model view of the IPMI messaging driver.
600 static struct platform_driver ipmidriver = {
603 .bus = &platform_bus_type
607 * This mutex keeps us from adding the same BMC twice.
609 static DEFINE_MUTEX(ipmidriver_mutex);
611 static LIST_HEAD(ipmi_interfaces);
612 static DEFINE_MUTEX(ipmi_interfaces_mutex);
613 #define ipmi_interfaces_mutex_held() \
614 lockdep_is_held(&ipmi_interfaces_mutex)
615 static struct srcu_struct ipmi_interfaces_srcu;
618 * List of watchers that want to know when smi's are added and deleted.
620 static LIST_HEAD(smi_watchers);
621 static DEFINE_MUTEX(smi_watchers_mutex);
623 #define ipmi_inc_stat(intf, stat) \
624 atomic_inc(&(intf)->stats[IPMI_STAT_ ## stat])
625 #define ipmi_get_stat(intf, stat) \
626 ((unsigned int) atomic_read(&(intf)->stats[IPMI_STAT_ ## stat]))
628 static const char * const addr_src_to_str[] = {
629 "invalid", "hotmod", "hardcoded", "SPMI", "ACPI", "SMBIOS", "PCI",
630 "device-tree", "platform"
633 const char *ipmi_addr_src_to_str(enum ipmi_addr_src src)
636 src = 0; /* Invalid */
637 return addr_src_to_str[src];
639 EXPORT_SYMBOL(ipmi_addr_src_to_str);
641 static int is_lan_addr(struct ipmi_addr *addr)
643 return addr->addr_type == IPMI_LAN_ADDR_TYPE;
646 static int is_ipmb_addr(struct ipmi_addr *addr)
648 return addr->addr_type == IPMI_IPMB_ADDR_TYPE;
651 static int is_ipmb_bcast_addr(struct ipmi_addr *addr)
653 return addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE;
656 static void free_recv_msg_list(struct list_head *q)
658 struct ipmi_recv_msg *msg, *msg2;
660 list_for_each_entry_safe(msg, msg2, q, link) {
661 list_del(&msg->link);
662 ipmi_free_recv_msg(msg);
666 static void free_smi_msg_list(struct list_head *q)
668 struct ipmi_smi_msg *msg, *msg2;
670 list_for_each_entry_safe(msg, msg2, q, link) {
671 list_del(&msg->link);
672 ipmi_free_smi_msg(msg);
676 static void clean_up_interface_data(struct ipmi_smi *intf)
679 struct cmd_rcvr *rcvr, *rcvr2;
680 struct list_head list;
682 tasklet_kill(&intf->recv_tasklet);
684 free_smi_msg_list(&intf->waiting_rcv_msgs);
685 free_recv_msg_list(&intf->waiting_events);
688 * Wholesale remove all the entries from the list in the
689 * interface and wait for RCU to know that none are in use.
691 mutex_lock(&intf->cmd_rcvrs_mutex);
692 INIT_LIST_HEAD(&list);
693 list_splice_init_rcu(&intf->cmd_rcvrs, &list, synchronize_rcu);
694 mutex_unlock(&intf->cmd_rcvrs_mutex);
696 list_for_each_entry_safe(rcvr, rcvr2, &list, link)
699 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
700 if ((intf->seq_table[i].inuse)
701 && (intf->seq_table[i].recv_msg))
702 ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
706 static void intf_free(struct kref *ref)
708 struct ipmi_smi *intf = container_of(ref, struct ipmi_smi, refcount);
710 clean_up_interface_data(intf);
714 struct watcher_entry {
716 struct ipmi_smi *intf;
717 struct list_head link;
720 int ipmi_smi_watcher_register(struct ipmi_smi_watcher *watcher)
722 struct ipmi_smi *intf;
726 * Make sure the driver is actually initialized, this handles
727 * problems with initialization order.
729 rv = ipmi_init_msghandler();
733 mutex_lock(&smi_watchers_mutex);
735 list_add(&watcher->link, &smi_watchers);
737 index = srcu_read_lock(&ipmi_interfaces_srcu);
738 list_for_each_entry_rcu(intf, &ipmi_interfaces, link,
739 lockdep_is_held(&smi_watchers_mutex)) {
740 int intf_num = READ_ONCE(intf->intf_num);
744 watcher->new_smi(intf_num, intf->si_dev);
746 srcu_read_unlock(&ipmi_interfaces_srcu, index);
748 mutex_unlock(&smi_watchers_mutex);
752 EXPORT_SYMBOL(ipmi_smi_watcher_register);
754 int ipmi_smi_watcher_unregister(struct ipmi_smi_watcher *watcher)
756 mutex_lock(&smi_watchers_mutex);
757 list_del(&watcher->link);
758 mutex_unlock(&smi_watchers_mutex);
761 EXPORT_SYMBOL(ipmi_smi_watcher_unregister);
764 * Must be called with smi_watchers_mutex held.
767 call_smi_watchers(int i, struct device *dev)
769 struct ipmi_smi_watcher *w;
771 mutex_lock(&smi_watchers_mutex);
772 list_for_each_entry(w, &smi_watchers, link) {
773 if (try_module_get(w->owner)) {
775 module_put(w->owner);
778 mutex_unlock(&smi_watchers_mutex);
782 ipmi_addr_equal(struct ipmi_addr *addr1, struct ipmi_addr *addr2)
784 if (addr1->addr_type != addr2->addr_type)
787 if (addr1->channel != addr2->channel)
790 if (addr1->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
791 struct ipmi_system_interface_addr *smi_addr1
792 = (struct ipmi_system_interface_addr *) addr1;
793 struct ipmi_system_interface_addr *smi_addr2
794 = (struct ipmi_system_interface_addr *) addr2;
795 return (smi_addr1->lun == smi_addr2->lun);
798 if (is_ipmb_addr(addr1) || is_ipmb_bcast_addr(addr1)) {
799 struct ipmi_ipmb_addr *ipmb_addr1
800 = (struct ipmi_ipmb_addr *) addr1;
801 struct ipmi_ipmb_addr *ipmb_addr2
802 = (struct ipmi_ipmb_addr *) addr2;
804 return ((ipmb_addr1->slave_addr == ipmb_addr2->slave_addr)
805 && (ipmb_addr1->lun == ipmb_addr2->lun));
808 if (is_lan_addr(addr1)) {
809 struct ipmi_lan_addr *lan_addr1
810 = (struct ipmi_lan_addr *) addr1;
811 struct ipmi_lan_addr *lan_addr2
812 = (struct ipmi_lan_addr *) addr2;
814 return ((lan_addr1->remote_SWID == lan_addr2->remote_SWID)
815 && (lan_addr1->local_SWID == lan_addr2->local_SWID)
816 && (lan_addr1->session_handle
817 == lan_addr2->session_handle)
818 && (lan_addr1->lun == lan_addr2->lun));
824 int ipmi_validate_addr(struct ipmi_addr *addr, int len)
826 if (len < sizeof(struct ipmi_system_interface_addr))
829 if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
830 if (addr->channel != IPMI_BMC_CHANNEL)
835 if ((addr->channel == IPMI_BMC_CHANNEL)
836 || (addr->channel >= IPMI_MAX_CHANNELS)
837 || (addr->channel < 0))
840 if (is_ipmb_addr(addr) || is_ipmb_bcast_addr(addr)) {
841 if (len < sizeof(struct ipmi_ipmb_addr))
846 if (is_lan_addr(addr)) {
847 if (len < sizeof(struct ipmi_lan_addr))
854 EXPORT_SYMBOL(ipmi_validate_addr);
856 unsigned int ipmi_addr_length(int addr_type)
858 if (addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
859 return sizeof(struct ipmi_system_interface_addr);
861 if ((addr_type == IPMI_IPMB_ADDR_TYPE)
862 || (addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE))
863 return sizeof(struct ipmi_ipmb_addr);
865 if (addr_type == IPMI_LAN_ADDR_TYPE)
866 return sizeof(struct ipmi_lan_addr);
870 EXPORT_SYMBOL(ipmi_addr_length);
872 static int deliver_response(struct ipmi_smi *intf, struct ipmi_recv_msg *msg)
877 /* Special handling for NULL users. */
878 if (intf->null_user_handler) {
879 intf->null_user_handler(intf, msg);
881 /* No handler, so give up. */
884 ipmi_free_recv_msg(msg);
885 } else if (oops_in_progress) {
887 * If we are running in the panic context, calling the
888 * receive handler doesn't much meaning and has a deadlock
889 * risk. At this moment, simply skip it in that case.
891 ipmi_free_recv_msg(msg);
894 struct ipmi_user *user = acquire_ipmi_user(msg->user, &index);
897 user->handler->ipmi_recv_hndl(msg, user->handler_data);
898 release_ipmi_user(user, index);
900 /* User went away, give up. */
901 ipmi_free_recv_msg(msg);
909 static void deliver_local_response(struct ipmi_smi *intf,
910 struct ipmi_recv_msg *msg)
912 if (deliver_response(intf, msg))
913 ipmi_inc_stat(intf, unhandled_local_responses);
915 ipmi_inc_stat(intf, handled_local_responses);
918 static void deliver_err_response(struct ipmi_smi *intf,
919 struct ipmi_recv_msg *msg, int err)
921 msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
922 msg->msg_data[0] = err;
923 msg->msg.netfn |= 1; /* Convert to a response. */
924 msg->msg.data_len = 1;
925 msg->msg.data = msg->msg_data;
926 deliver_local_response(intf, msg);
929 static void smi_add_watch(struct ipmi_smi *intf, unsigned int flags)
931 unsigned long iflags;
933 if (!intf->handlers->set_need_watch)
936 spin_lock_irqsave(&intf->watch_lock, iflags);
937 if (flags & IPMI_WATCH_MASK_CHECK_MESSAGES)
938 intf->response_waiters++;
940 if (flags & IPMI_WATCH_MASK_CHECK_WATCHDOG)
941 intf->watchdog_waiters++;
943 if (flags & IPMI_WATCH_MASK_CHECK_COMMANDS)
944 intf->command_waiters++;
946 if ((intf->last_watch_mask & flags) != flags) {
947 intf->last_watch_mask |= flags;
948 intf->handlers->set_need_watch(intf->send_info,
949 intf->last_watch_mask);
951 spin_unlock_irqrestore(&intf->watch_lock, iflags);
954 static void smi_remove_watch(struct ipmi_smi *intf, unsigned int flags)
956 unsigned long iflags;
958 if (!intf->handlers->set_need_watch)
961 spin_lock_irqsave(&intf->watch_lock, iflags);
962 if (flags & IPMI_WATCH_MASK_CHECK_MESSAGES)
963 intf->response_waiters--;
965 if (flags & IPMI_WATCH_MASK_CHECK_WATCHDOG)
966 intf->watchdog_waiters--;
968 if (flags & IPMI_WATCH_MASK_CHECK_COMMANDS)
969 intf->command_waiters--;
972 if (intf->response_waiters)
973 flags |= IPMI_WATCH_MASK_CHECK_MESSAGES;
974 if (intf->watchdog_waiters)
975 flags |= IPMI_WATCH_MASK_CHECK_WATCHDOG;
976 if (intf->command_waiters)
977 flags |= IPMI_WATCH_MASK_CHECK_COMMANDS;
979 if (intf->last_watch_mask != flags) {
980 intf->last_watch_mask = flags;
981 intf->handlers->set_need_watch(intf->send_info,
982 intf->last_watch_mask);
984 spin_unlock_irqrestore(&intf->watch_lock, iflags);
988 * Find the next sequence number not being used and add the given
989 * message with the given timeout to the sequence table. This must be
990 * called with the interface's seq_lock held.
992 static int intf_next_seq(struct ipmi_smi *intf,
993 struct ipmi_recv_msg *recv_msg,
994 unsigned long timeout,
1004 timeout = default_retry_ms;
1006 retries = default_max_retries;
1008 for (i = intf->curr_seq; (i+1)%IPMI_IPMB_NUM_SEQ != intf->curr_seq;
1009 i = (i+1)%IPMI_IPMB_NUM_SEQ) {
1010 if (!intf->seq_table[i].inuse)
1014 if (!intf->seq_table[i].inuse) {
1015 intf->seq_table[i].recv_msg = recv_msg;
1018 * Start with the maximum timeout, when the send response
1019 * comes in we will start the real timer.
1021 intf->seq_table[i].timeout = MAX_MSG_TIMEOUT;
1022 intf->seq_table[i].orig_timeout = timeout;
1023 intf->seq_table[i].retries_left = retries;
1024 intf->seq_table[i].broadcast = broadcast;
1025 intf->seq_table[i].inuse = 1;
1026 intf->seq_table[i].seqid = NEXT_SEQID(intf->seq_table[i].seqid);
1028 *seqid = intf->seq_table[i].seqid;
1029 intf->curr_seq = (i+1)%IPMI_IPMB_NUM_SEQ;
1030 smi_add_watch(intf, IPMI_WATCH_MASK_CHECK_MESSAGES);
1040 * Return the receive message for the given sequence number and
1041 * release the sequence number so it can be reused. Some other data
1042 * is passed in to be sure the message matches up correctly (to help
1043 * guard against message coming in after their timeout and the
1044 * sequence number being reused).
1046 static int intf_find_seq(struct ipmi_smi *intf,
1050 unsigned char netfn,
1051 struct ipmi_addr *addr,
1052 struct ipmi_recv_msg **recv_msg)
1055 unsigned long flags;
1057 if (seq >= IPMI_IPMB_NUM_SEQ)
1060 spin_lock_irqsave(&intf->seq_lock, flags);
1061 if (intf->seq_table[seq].inuse) {
1062 struct ipmi_recv_msg *msg = intf->seq_table[seq].recv_msg;
1064 if ((msg->addr.channel == channel) && (msg->msg.cmd == cmd)
1065 && (msg->msg.netfn == netfn)
1066 && (ipmi_addr_equal(addr, &msg->addr))) {
1068 intf->seq_table[seq].inuse = 0;
1069 smi_remove_watch(intf, IPMI_WATCH_MASK_CHECK_MESSAGES);
1073 spin_unlock_irqrestore(&intf->seq_lock, flags);
1079 /* Start the timer for a specific sequence table entry. */
1080 static int intf_start_seq_timer(struct ipmi_smi *intf,
1084 unsigned long flags;
1086 unsigned long seqid;
1089 GET_SEQ_FROM_MSGID(msgid, seq, seqid);
1091 spin_lock_irqsave(&intf->seq_lock, flags);
1093 * We do this verification because the user can be deleted
1094 * while a message is outstanding.
1096 if ((intf->seq_table[seq].inuse)
1097 && (intf->seq_table[seq].seqid == seqid)) {
1098 struct seq_table *ent = &intf->seq_table[seq];
1099 ent->timeout = ent->orig_timeout;
1102 spin_unlock_irqrestore(&intf->seq_lock, flags);
1107 /* Got an error for the send message for a specific sequence number. */
1108 static int intf_err_seq(struct ipmi_smi *intf,
1113 unsigned long flags;
1115 unsigned long seqid;
1116 struct ipmi_recv_msg *msg = NULL;
1119 GET_SEQ_FROM_MSGID(msgid, seq, seqid);
1121 spin_lock_irqsave(&intf->seq_lock, flags);
1123 * We do this verification because the user can be deleted
1124 * while a message is outstanding.
1126 if ((intf->seq_table[seq].inuse)
1127 && (intf->seq_table[seq].seqid == seqid)) {
1128 struct seq_table *ent = &intf->seq_table[seq];
1131 smi_remove_watch(intf, IPMI_WATCH_MASK_CHECK_MESSAGES);
1132 msg = ent->recv_msg;
1135 spin_unlock_irqrestore(&intf->seq_lock, flags);
1138 deliver_err_response(intf, msg, err);
1143 static void free_user_work(struct work_struct *work)
1145 struct ipmi_user *user = container_of(work, struct ipmi_user,
1148 cleanup_srcu_struct(&user->release_barrier);
1152 int ipmi_create_user(unsigned int if_num,
1153 const struct ipmi_user_hndl *handler,
1155 struct ipmi_user **user)
1157 unsigned long flags;
1158 struct ipmi_user *new_user;
1160 struct ipmi_smi *intf;
1163 * There is no module usecount here, because it's not
1164 * required. Since this can only be used by and called from
1165 * other modules, they will implicitly use this module, and
1166 * thus this can't be removed unless the other modules are
1170 if (handler == NULL)
1174 * Make sure the driver is actually initialized, this handles
1175 * problems with initialization order.
1177 rv = ipmi_init_msghandler();
1181 new_user = vzalloc(sizeof(*new_user));
1185 index = srcu_read_lock(&ipmi_interfaces_srcu);
1186 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
1187 if (intf->intf_num == if_num)
1190 /* Not found, return an error */
1195 INIT_WORK(&new_user->remove_work, free_user_work);
1197 rv = init_srcu_struct(&new_user->release_barrier);
1201 if (!try_module_get(intf->owner)) {
1206 /* Note that each existing user holds a refcount to the interface. */
1207 kref_get(&intf->refcount);
1209 kref_init(&new_user->refcount);
1210 new_user->handler = handler;
1211 new_user->handler_data = handler_data;
1212 new_user->intf = intf;
1213 new_user->gets_events = false;
1215 rcu_assign_pointer(new_user->self, new_user);
1216 spin_lock_irqsave(&intf->seq_lock, flags);
1217 list_add_rcu(&new_user->link, &intf->users);
1218 spin_unlock_irqrestore(&intf->seq_lock, flags);
1219 if (handler->ipmi_watchdog_pretimeout)
1220 /* User wants pretimeouts, so make sure to watch for them. */
1221 smi_add_watch(intf, IPMI_WATCH_MASK_CHECK_WATCHDOG);
1222 srcu_read_unlock(&ipmi_interfaces_srcu, index);
1227 srcu_read_unlock(&ipmi_interfaces_srcu, index);
1231 EXPORT_SYMBOL(ipmi_create_user);
1233 int ipmi_get_smi_info(int if_num, struct ipmi_smi_info *data)
1236 struct ipmi_smi *intf;
1238 index = srcu_read_lock(&ipmi_interfaces_srcu);
1239 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
1240 if (intf->intf_num == if_num)
1243 srcu_read_unlock(&ipmi_interfaces_srcu, index);
1245 /* Not found, return an error */
1249 if (!intf->handlers->get_smi_info)
1252 rv = intf->handlers->get_smi_info(intf->send_info, data);
1253 srcu_read_unlock(&ipmi_interfaces_srcu, index);
1257 EXPORT_SYMBOL(ipmi_get_smi_info);
1259 static void free_user(struct kref *ref)
1261 struct ipmi_user *user = container_of(ref, struct ipmi_user, refcount);
1263 /* SRCU cleanup must happen in task context. */
1264 schedule_work(&user->remove_work);
1267 static void _ipmi_destroy_user(struct ipmi_user *user)
1269 struct ipmi_smi *intf = user->intf;
1271 unsigned long flags;
1272 struct cmd_rcvr *rcvr;
1273 struct cmd_rcvr *rcvrs = NULL;
1275 if (!acquire_ipmi_user(user, &i)) {
1277 * The user has already been cleaned up, just make sure
1278 * nothing is using it and return.
1280 synchronize_srcu(&user->release_barrier);
1284 rcu_assign_pointer(user->self, NULL);
1285 release_ipmi_user(user, i);
1287 synchronize_srcu(&user->release_barrier);
1289 if (user->handler->shutdown)
1290 user->handler->shutdown(user->handler_data);
1292 if (user->handler->ipmi_watchdog_pretimeout)
1293 smi_remove_watch(intf, IPMI_WATCH_MASK_CHECK_WATCHDOG);
1295 if (user->gets_events)
1296 atomic_dec(&intf->event_waiters);
1298 /* Remove the user from the interface's sequence table. */
1299 spin_lock_irqsave(&intf->seq_lock, flags);
1300 list_del_rcu(&user->link);
1302 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
1303 if (intf->seq_table[i].inuse
1304 && (intf->seq_table[i].recv_msg->user == user)) {
1305 intf->seq_table[i].inuse = 0;
1306 smi_remove_watch(intf, IPMI_WATCH_MASK_CHECK_MESSAGES);
1307 ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
1310 spin_unlock_irqrestore(&intf->seq_lock, flags);
1313 * Remove the user from the command receiver's table. First
1314 * we build a list of everything (not using the standard link,
1315 * since other things may be using it till we do
1316 * synchronize_srcu()) then free everything in that list.
1318 mutex_lock(&intf->cmd_rcvrs_mutex);
1319 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link,
1320 lockdep_is_held(&intf->cmd_rcvrs_mutex)) {
1321 if (rcvr->user == user) {
1322 list_del_rcu(&rcvr->link);
1327 mutex_unlock(&intf->cmd_rcvrs_mutex);
1335 kref_put(&intf->refcount, intf_free);
1336 module_put(intf->owner);
1339 int ipmi_destroy_user(struct ipmi_user *user)
1341 _ipmi_destroy_user(user);
1343 kref_put(&user->refcount, free_user);
1347 EXPORT_SYMBOL(ipmi_destroy_user);
1349 int ipmi_get_version(struct ipmi_user *user,
1350 unsigned char *major,
1351 unsigned char *minor)
1353 struct ipmi_device_id id;
1356 user = acquire_ipmi_user(user, &index);
1360 rv = bmc_get_device_id(user->intf, NULL, &id, NULL, NULL);
1362 *major = ipmi_version_major(&id);
1363 *minor = ipmi_version_minor(&id);
1365 release_ipmi_user(user, index);
1369 EXPORT_SYMBOL(ipmi_get_version);
1371 int ipmi_set_my_address(struct ipmi_user *user,
1372 unsigned int channel,
1373 unsigned char address)
1377 user = acquire_ipmi_user(user, &index);
1381 if (channel >= IPMI_MAX_CHANNELS) {
1384 channel = array_index_nospec(channel, IPMI_MAX_CHANNELS);
1385 user->intf->addrinfo[channel].address = address;
1387 release_ipmi_user(user, index);
1391 EXPORT_SYMBOL(ipmi_set_my_address);
1393 int ipmi_get_my_address(struct ipmi_user *user,
1394 unsigned int channel,
1395 unsigned char *address)
1399 user = acquire_ipmi_user(user, &index);
1403 if (channel >= IPMI_MAX_CHANNELS) {
1406 channel = array_index_nospec(channel, IPMI_MAX_CHANNELS);
1407 *address = user->intf->addrinfo[channel].address;
1409 release_ipmi_user(user, index);
1413 EXPORT_SYMBOL(ipmi_get_my_address);
1415 int ipmi_set_my_LUN(struct ipmi_user *user,
1416 unsigned int channel,
1421 user = acquire_ipmi_user(user, &index);
1425 if (channel >= IPMI_MAX_CHANNELS) {
1428 channel = array_index_nospec(channel, IPMI_MAX_CHANNELS);
1429 user->intf->addrinfo[channel].lun = LUN & 0x3;
1431 release_ipmi_user(user, index);
1435 EXPORT_SYMBOL(ipmi_set_my_LUN);
1437 int ipmi_get_my_LUN(struct ipmi_user *user,
1438 unsigned int channel,
1439 unsigned char *address)
1443 user = acquire_ipmi_user(user, &index);
1447 if (channel >= IPMI_MAX_CHANNELS) {
1450 channel = array_index_nospec(channel, IPMI_MAX_CHANNELS);
1451 *address = user->intf->addrinfo[channel].lun;
1453 release_ipmi_user(user, index);
1457 EXPORT_SYMBOL(ipmi_get_my_LUN);
1459 int ipmi_get_maintenance_mode(struct ipmi_user *user)
1462 unsigned long flags;
1464 user = acquire_ipmi_user(user, &index);
1468 spin_lock_irqsave(&user->intf->maintenance_mode_lock, flags);
1469 mode = user->intf->maintenance_mode;
1470 spin_unlock_irqrestore(&user->intf->maintenance_mode_lock, flags);
1471 release_ipmi_user(user, index);
1475 EXPORT_SYMBOL(ipmi_get_maintenance_mode);
1477 static void maintenance_mode_update(struct ipmi_smi *intf)
1479 if (intf->handlers->set_maintenance_mode)
1480 intf->handlers->set_maintenance_mode(
1481 intf->send_info, intf->maintenance_mode_enable);
1484 int ipmi_set_maintenance_mode(struct ipmi_user *user, int mode)
1487 unsigned long flags;
1488 struct ipmi_smi *intf = user->intf;
1490 user = acquire_ipmi_user(user, &index);
1494 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
1495 if (intf->maintenance_mode != mode) {
1497 case IPMI_MAINTENANCE_MODE_AUTO:
1498 intf->maintenance_mode_enable
1499 = (intf->auto_maintenance_timeout > 0);
1502 case IPMI_MAINTENANCE_MODE_OFF:
1503 intf->maintenance_mode_enable = false;
1506 case IPMI_MAINTENANCE_MODE_ON:
1507 intf->maintenance_mode_enable = true;
1514 intf->maintenance_mode = mode;
1516 maintenance_mode_update(intf);
1519 spin_unlock_irqrestore(&intf->maintenance_mode_lock, flags);
1520 release_ipmi_user(user, index);
1524 EXPORT_SYMBOL(ipmi_set_maintenance_mode);
1526 int ipmi_set_gets_events(struct ipmi_user *user, bool val)
1528 unsigned long flags;
1529 struct ipmi_smi *intf = user->intf;
1530 struct ipmi_recv_msg *msg, *msg2;
1531 struct list_head msgs;
1534 user = acquire_ipmi_user(user, &index);
1538 INIT_LIST_HEAD(&msgs);
1540 spin_lock_irqsave(&intf->events_lock, flags);
1541 if (user->gets_events == val)
1544 user->gets_events = val;
1547 if (atomic_inc_return(&intf->event_waiters) == 1)
1550 atomic_dec(&intf->event_waiters);
1553 if (intf->delivering_events)
1555 * Another thread is delivering events for this, so
1556 * let it handle any new events.
1560 /* Deliver any queued events. */
1561 while (user->gets_events && !list_empty(&intf->waiting_events)) {
1562 list_for_each_entry_safe(msg, msg2, &intf->waiting_events, link)
1563 list_move_tail(&msg->link, &msgs);
1564 intf->waiting_events_count = 0;
1565 if (intf->event_msg_printed) {
1566 dev_warn(intf->si_dev, "Event queue no longer full\n");
1567 intf->event_msg_printed = 0;
1570 intf->delivering_events = 1;
1571 spin_unlock_irqrestore(&intf->events_lock, flags);
1573 list_for_each_entry_safe(msg, msg2, &msgs, link) {
1575 kref_get(&user->refcount);
1576 deliver_local_response(intf, msg);
1579 spin_lock_irqsave(&intf->events_lock, flags);
1580 intf->delivering_events = 0;
1584 spin_unlock_irqrestore(&intf->events_lock, flags);
1585 release_ipmi_user(user, index);
1589 EXPORT_SYMBOL(ipmi_set_gets_events);
1591 static struct cmd_rcvr *find_cmd_rcvr(struct ipmi_smi *intf,
1592 unsigned char netfn,
1596 struct cmd_rcvr *rcvr;
1598 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link,
1599 lockdep_is_held(&intf->cmd_rcvrs_mutex)) {
1600 if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)
1601 && (rcvr->chans & (1 << chan)))
1607 static int is_cmd_rcvr_exclusive(struct ipmi_smi *intf,
1608 unsigned char netfn,
1612 struct cmd_rcvr *rcvr;
1614 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link,
1615 lockdep_is_held(&intf->cmd_rcvrs_mutex)) {
1616 if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)
1617 && (rcvr->chans & chans))
1623 int ipmi_register_for_cmd(struct ipmi_user *user,
1624 unsigned char netfn,
1628 struct ipmi_smi *intf = user->intf;
1629 struct cmd_rcvr *rcvr;
1632 user = acquire_ipmi_user(user, &index);
1636 rcvr = kmalloc(sizeof(*rcvr), GFP_KERNEL);
1642 rcvr->netfn = netfn;
1643 rcvr->chans = chans;
1646 mutex_lock(&intf->cmd_rcvrs_mutex);
1647 /* Make sure the command/netfn is not already registered. */
1648 if (!is_cmd_rcvr_exclusive(intf, netfn, cmd, chans)) {
1653 smi_add_watch(intf, IPMI_WATCH_MASK_CHECK_COMMANDS);
1655 list_add_rcu(&rcvr->link, &intf->cmd_rcvrs);
1658 mutex_unlock(&intf->cmd_rcvrs_mutex);
1662 release_ipmi_user(user, index);
1666 EXPORT_SYMBOL(ipmi_register_for_cmd);
1668 int ipmi_unregister_for_cmd(struct ipmi_user *user,
1669 unsigned char netfn,
1673 struct ipmi_smi *intf = user->intf;
1674 struct cmd_rcvr *rcvr;
1675 struct cmd_rcvr *rcvrs = NULL;
1676 int i, rv = -ENOENT, index;
1678 user = acquire_ipmi_user(user, &index);
1682 mutex_lock(&intf->cmd_rcvrs_mutex);
1683 for (i = 0; i < IPMI_NUM_CHANNELS; i++) {
1684 if (((1 << i) & chans) == 0)
1686 rcvr = find_cmd_rcvr(intf, netfn, cmd, i);
1689 if (rcvr->user == user) {
1691 rcvr->chans &= ~chans;
1692 if (rcvr->chans == 0) {
1693 list_del_rcu(&rcvr->link);
1699 mutex_unlock(&intf->cmd_rcvrs_mutex);
1701 release_ipmi_user(user, index);
1703 smi_remove_watch(intf, IPMI_WATCH_MASK_CHECK_COMMANDS);
1711 EXPORT_SYMBOL(ipmi_unregister_for_cmd);
1713 static unsigned char
1714 ipmb_checksum(unsigned char *data, int size)
1716 unsigned char csum = 0;
1718 for (; size > 0; size--, data++)
1724 static inline void format_ipmb_msg(struct ipmi_smi_msg *smi_msg,
1725 struct kernel_ipmi_msg *msg,
1726 struct ipmi_ipmb_addr *ipmb_addr,
1728 unsigned char ipmb_seq,
1730 unsigned char source_address,
1731 unsigned char source_lun)
1735 /* Format the IPMB header data. */
1736 smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
1737 smi_msg->data[1] = IPMI_SEND_MSG_CMD;
1738 smi_msg->data[2] = ipmb_addr->channel;
1740 smi_msg->data[3] = 0;
1741 smi_msg->data[i+3] = ipmb_addr->slave_addr;
1742 smi_msg->data[i+4] = (msg->netfn << 2) | (ipmb_addr->lun & 0x3);
1743 smi_msg->data[i+5] = ipmb_checksum(&smi_msg->data[i + 3], 2);
1744 smi_msg->data[i+6] = source_address;
1745 smi_msg->data[i+7] = (ipmb_seq << 2) | source_lun;
1746 smi_msg->data[i+8] = msg->cmd;
1748 /* Now tack on the data to the message. */
1749 if (msg->data_len > 0)
1750 memcpy(&smi_msg->data[i + 9], msg->data, msg->data_len);
1751 smi_msg->data_size = msg->data_len + 9;
1753 /* Now calculate the checksum and tack it on. */
1754 smi_msg->data[i+smi_msg->data_size]
1755 = ipmb_checksum(&smi_msg->data[i + 6], smi_msg->data_size - 6);
1758 * Add on the checksum size and the offset from the
1761 smi_msg->data_size += 1 + i;
1763 smi_msg->msgid = msgid;
1766 static inline void format_lan_msg(struct ipmi_smi_msg *smi_msg,
1767 struct kernel_ipmi_msg *msg,
1768 struct ipmi_lan_addr *lan_addr,
1770 unsigned char ipmb_seq,
1771 unsigned char source_lun)
1773 /* Format the IPMB header data. */
1774 smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
1775 smi_msg->data[1] = IPMI_SEND_MSG_CMD;
1776 smi_msg->data[2] = lan_addr->channel;
1777 smi_msg->data[3] = lan_addr->session_handle;
1778 smi_msg->data[4] = lan_addr->remote_SWID;
1779 smi_msg->data[5] = (msg->netfn << 2) | (lan_addr->lun & 0x3);
1780 smi_msg->data[6] = ipmb_checksum(&smi_msg->data[4], 2);
1781 smi_msg->data[7] = lan_addr->local_SWID;
1782 smi_msg->data[8] = (ipmb_seq << 2) | source_lun;
1783 smi_msg->data[9] = msg->cmd;
1785 /* Now tack on the data to the message. */
1786 if (msg->data_len > 0)
1787 memcpy(&smi_msg->data[10], msg->data, msg->data_len);
1788 smi_msg->data_size = msg->data_len + 10;
1790 /* Now calculate the checksum and tack it on. */
1791 smi_msg->data[smi_msg->data_size]
1792 = ipmb_checksum(&smi_msg->data[7], smi_msg->data_size - 7);
1795 * Add on the checksum size and the offset from the
1798 smi_msg->data_size += 1;
1800 smi_msg->msgid = msgid;
1803 static struct ipmi_smi_msg *smi_add_send_msg(struct ipmi_smi *intf,
1804 struct ipmi_smi_msg *smi_msg,
1807 if (intf->curr_msg) {
1809 list_add_tail(&smi_msg->link, &intf->hp_xmit_msgs);
1811 list_add_tail(&smi_msg->link, &intf->xmit_msgs);
1814 intf->curr_msg = smi_msg;
1820 static void smi_send(struct ipmi_smi *intf,
1821 const struct ipmi_smi_handlers *handlers,
1822 struct ipmi_smi_msg *smi_msg, int priority)
1824 int run_to_completion = intf->run_to_completion;
1825 unsigned long flags = 0;
1827 if (!run_to_completion)
1828 spin_lock_irqsave(&intf->xmit_msgs_lock, flags);
1829 smi_msg = smi_add_send_msg(intf, smi_msg, priority);
1831 if (!run_to_completion)
1832 spin_unlock_irqrestore(&intf->xmit_msgs_lock, flags);
1835 handlers->sender(intf->send_info, smi_msg);
1838 static bool is_maintenance_mode_cmd(struct kernel_ipmi_msg *msg)
1840 return (((msg->netfn == IPMI_NETFN_APP_REQUEST)
1841 && ((msg->cmd == IPMI_COLD_RESET_CMD)
1842 || (msg->cmd == IPMI_WARM_RESET_CMD)))
1843 || (msg->netfn == IPMI_NETFN_FIRMWARE_REQUEST));
1846 static int i_ipmi_req_sysintf(struct ipmi_smi *intf,
1847 struct ipmi_addr *addr,
1849 struct kernel_ipmi_msg *msg,
1850 struct ipmi_smi_msg *smi_msg,
1851 struct ipmi_recv_msg *recv_msg,
1853 unsigned int retry_time_ms)
1855 struct ipmi_system_interface_addr *smi_addr;
1858 /* Responses are not allowed to the SMI. */
1861 smi_addr = (struct ipmi_system_interface_addr *) addr;
1862 if (smi_addr->lun > 3) {
1863 ipmi_inc_stat(intf, sent_invalid_commands);
1867 memcpy(&recv_msg->addr, smi_addr, sizeof(*smi_addr));
1869 if ((msg->netfn == IPMI_NETFN_APP_REQUEST)
1870 && ((msg->cmd == IPMI_SEND_MSG_CMD)
1871 || (msg->cmd == IPMI_GET_MSG_CMD)
1872 || (msg->cmd == IPMI_READ_EVENT_MSG_BUFFER_CMD))) {
1874 * We don't let the user do these, since we manage
1875 * the sequence numbers.
1877 ipmi_inc_stat(intf, sent_invalid_commands);
1881 if (is_maintenance_mode_cmd(msg)) {
1882 unsigned long flags;
1884 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
1885 intf->auto_maintenance_timeout
1886 = maintenance_mode_timeout_ms;
1887 if (!intf->maintenance_mode
1888 && !intf->maintenance_mode_enable) {
1889 intf->maintenance_mode_enable = true;
1890 maintenance_mode_update(intf);
1892 spin_unlock_irqrestore(&intf->maintenance_mode_lock,
1896 if (msg->data_len + 2 > IPMI_MAX_MSG_LENGTH) {
1897 ipmi_inc_stat(intf, sent_invalid_commands);
1901 smi_msg->data[0] = (msg->netfn << 2) | (smi_addr->lun & 0x3);
1902 smi_msg->data[1] = msg->cmd;
1903 smi_msg->msgid = msgid;
1904 smi_msg->user_data = recv_msg;
1905 if (msg->data_len > 0)
1906 memcpy(&smi_msg->data[2], msg->data, msg->data_len);
1907 smi_msg->data_size = msg->data_len + 2;
1908 ipmi_inc_stat(intf, sent_local_commands);
1913 static int i_ipmi_req_ipmb(struct ipmi_smi *intf,
1914 struct ipmi_addr *addr,
1916 struct kernel_ipmi_msg *msg,
1917 struct ipmi_smi_msg *smi_msg,
1918 struct ipmi_recv_msg *recv_msg,
1919 unsigned char source_address,
1920 unsigned char source_lun,
1922 unsigned int retry_time_ms)
1924 struct ipmi_ipmb_addr *ipmb_addr;
1925 unsigned char ipmb_seq;
1928 struct ipmi_channel *chans;
1931 if (addr->channel >= IPMI_MAX_CHANNELS) {
1932 ipmi_inc_stat(intf, sent_invalid_commands);
1936 chans = READ_ONCE(intf->channel_list)->c;
1938 if (chans[addr->channel].medium != IPMI_CHANNEL_MEDIUM_IPMB) {
1939 ipmi_inc_stat(intf, sent_invalid_commands);
1943 if (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE) {
1945 * Broadcasts add a zero at the beginning of the
1946 * message, but otherwise is the same as an IPMB
1949 addr->addr_type = IPMI_IPMB_ADDR_TYPE;
1951 retries = 0; /* Don't retry broadcasts. */
1955 * 9 for the header and 1 for the checksum, plus
1956 * possibly one for the broadcast.
1958 if ((msg->data_len + 10 + broadcast) > IPMI_MAX_MSG_LENGTH) {
1959 ipmi_inc_stat(intf, sent_invalid_commands);
1963 ipmb_addr = (struct ipmi_ipmb_addr *) addr;
1964 if (ipmb_addr->lun > 3) {
1965 ipmi_inc_stat(intf, sent_invalid_commands);
1969 memcpy(&recv_msg->addr, ipmb_addr, sizeof(*ipmb_addr));
1971 if (recv_msg->msg.netfn & 0x1) {
1973 * It's a response, so use the user's sequence
1976 ipmi_inc_stat(intf, sent_ipmb_responses);
1977 format_ipmb_msg(smi_msg, msg, ipmb_addr, msgid,
1979 source_address, source_lun);
1982 * Save the receive message so we can use it
1983 * to deliver the response.
1985 smi_msg->user_data = recv_msg;
1987 /* It's a command, so get a sequence for it. */
1988 unsigned long flags;
1990 spin_lock_irqsave(&intf->seq_lock, flags);
1992 if (is_maintenance_mode_cmd(msg))
1993 intf->ipmb_maintenance_mode_timeout =
1994 maintenance_mode_timeout_ms;
1996 if (intf->ipmb_maintenance_mode_timeout && retry_time_ms == 0)
1997 /* Different default in maintenance mode */
1998 retry_time_ms = default_maintenance_retry_ms;
2001 * Create a sequence number with a 1 second
2002 * timeout and 4 retries.
2004 rv = intf_next_seq(intf,
2013 * We have used up all the sequence numbers,
2014 * probably, so abort.
2018 ipmi_inc_stat(intf, sent_ipmb_commands);
2021 * Store the sequence number in the message,
2022 * so that when the send message response
2023 * comes back we can start the timer.
2025 format_ipmb_msg(smi_msg, msg, ipmb_addr,
2026 STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
2027 ipmb_seq, broadcast,
2028 source_address, source_lun);
2031 * Copy the message into the recv message data, so we
2032 * can retransmit it later if necessary.
2034 memcpy(recv_msg->msg_data, smi_msg->data,
2035 smi_msg->data_size);
2036 recv_msg->msg.data = recv_msg->msg_data;
2037 recv_msg->msg.data_len = smi_msg->data_size;
2040 * We don't unlock until here, because we need
2041 * to copy the completed message into the
2042 * recv_msg before we release the lock.
2043 * Otherwise, race conditions may bite us. I
2044 * know that's pretty paranoid, but I prefer
2048 spin_unlock_irqrestore(&intf->seq_lock, flags);
2054 static int i_ipmi_req_lan(struct ipmi_smi *intf,
2055 struct ipmi_addr *addr,
2057 struct kernel_ipmi_msg *msg,
2058 struct ipmi_smi_msg *smi_msg,
2059 struct ipmi_recv_msg *recv_msg,
2060 unsigned char source_lun,
2062 unsigned int retry_time_ms)
2064 struct ipmi_lan_addr *lan_addr;
2065 unsigned char ipmb_seq;
2067 struct ipmi_channel *chans;
2070 if (addr->channel >= IPMI_MAX_CHANNELS) {
2071 ipmi_inc_stat(intf, sent_invalid_commands);
2075 chans = READ_ONCE(intf->channel_list)->c;
2077 if ((chans[addr->channel].medium
2078 != IPMI_CHANNEL_MEDIUM_8023LAN)
2079 && (chans[addr->channel].medium
2080 != IPMI_CHANNEL_MEDIUM_ASYNC)) {
2081 ipmi_inc_stat(intf, sent_invalid_commands);
2085 /* 11 for the header and 1 for the checksum. */
2086 if ((msg->data_len + 12) > IPMI_MAX_MSG_LENGTH) {
2087 ipmi_inc_stat(intf, sent_invalid_commands);
2091 lan_addr = (struct ipmi_lan_addr *) addr;
2092 if (lan_addr->lun > 3) {
2093 ipmi_inc_stat(intf, sent_invalid_commands);
2097 memcpy(&recv_msg->addr, lan_addr, sizeof(*lan_addr));
2099 if (recv_msg->msg.netfn & 0x1) {
2101 * It's a response, so use the user's sequence
2104 ipmi_inc_stat(intf, sent_lan_responses);
2105 format_lan_msg(smi_msg, msg, lan_addr, msgid,
2109 * Save the receive message so we can use it
2110 * to deliver the response.
2112 smi_msg->user_data = recv_msg;
2114 /* It's a command, so get a sequence for it. */
2115 unsigned long flags;
2117 spin_lock_irqsave(&intf->seq_lock, flags);
2120 * Create a sequence number with a 1 second
2121 * timeout and 4 retries.
2123 rv = intf_next_seq(intf,
2132 * We have used up all the sequence numbers,
2133 * probably, so abort.
2137 ipmi_inc_stat(intf, sent_lan_commands);
2140 * Store the sequence number in the message,
2141 * so that when the send message response
2142 * comes back we can start the timer.
2144 format_lan_msg(smi_msg, msg, lan_addr,
2145 STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
2146 ipmb_seq, source_lun);
2149 * Copy the message into the recv message data, so we
2150 * can retransmit it later if necessary.
2152 memcpy(recv_msg->msg_data, smi_msg->data,
2153 smi_msg->data_size);
2154 recv_msg->msg.data = recv_msg->msg_data;
2155 recv_msg->msg.data_len = smi_msg->data_size;
2158 * We don't unlock until here, because we need
2159 * to copy the completed message into the
2160 * recv_msg before we release the lock.
2161 * Otherwise, race conditions may bite us. I
2162 * know that's pretty paranoid, but I prefer
2166 spin_unlock_irqrestore(&intf->seq_lock, flags);
2173 * Separate from ipmi_request so that the user does not have to be
2174 * supplied in certain circumstances (mainly at panic time). If
2175 * messages are supplied, they will be freed, even if an error
2178 static int i_ipmi_request(struct ipmi_user *user,
2179 struct ipmi_smi *intf,
2180 struct ipmi_addr *addr,
2182 struct kernel_ipmi_msg *msg,
2183 void *user_msg_data,
2185 struct ipmi_recv_msg *supplied_recv,
2187 unsigned char source_address,
2188 unsigned char source_lun,
2190 unsigned int retry_time_ms)
2192 struct ipmi_smi_msg *smi_msg;
2193 struct ipmi_recv_msg *recv_msg;
2197 recv_msg = supplied_recv;
2199 recv_msg = ipmi_alloc_recv_msg();
2200 if (recv_msg == NULL) {
2205 recv_msg->user_msg_data = user_msg_data;
2208 smi_msg = (struct ipmi_smi_msg *) supplied_smi;
2210 smi_msg = ipmi_alloc_smi_msg();
2211 if (smi_msg == NULL) {
2213 ipmi_free_recv_msg(recv_msg);
2220 if (intf->in_shutdown) {
2225 recv_msg->user = user;
2227 /* The put happens when the message is freed. */
2228 kref_get(&user->refcount);
2229 recv_msg->msgid = msgid;
2231 * Store the message to send in the receive message so timeout
2232 * responses can get the proper response data.
2234 recv_msg->msg = *msg;
2236 if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
2237 rv = i_ipmi_req_sysintf(intf, addr, msgid, msg, smi_msg,
2238 recv_msg, retries, retry_time_ms);
2239 } else if (is_ipmb_addr(addr) || is_ipmb_bcast_addr(addr)) {
2240 rv = i_ipmi_req_ipmb(intf, addr, msgid, msg, smi_msg, recv_msg,
2241 source_address, source_lun,
2242 retries, retry_time_ms);
2243 } else if (is_lan_addr(addr)) {
2244 rv = i_ipmi_req_lan(intf, addr, msgid, msg, smi_msg, recv_msg,
2245 source_lun, retries, retry_time_ms);
2247 /* Unknown address type. */
2248 ipmi_inc_stat(intf, sent_invalid_commands);
2254 ipmi_free_smi_msg(smi_msg);
2255 ipmi_free_recv_msg(recv_msg);
2257 pr_debug("Send: %*ph\n", smi_msg->data_size, smi_msg->data);
2259 smi_send(intf, intf->handlers, smi_msg, priority);
2267 static int check_addr(struct ipmi_smi *intf,
2268 struct ipmi_addr *addr,
2269 unsigned char *saddr,
2272 if (addr->channel >= IPMI_MAX_CHANNELS)
2274 addr->channel = array_index_nospec(addr->channel, IPMI_MAX_CHANNELS);
2275 *lun = intf->addrinfo[addr->channel].lun;
2276 *saddr = intf->addrinfo[addr->channel].address;
2280 int ipmi_request_settime(struct ipmi_user *user,
2281 struct ipmi_addr *addr,
2283 struct kernel_ipmi_msg *msg,
2284 void *user_msg_data,
2287 unsigned int retry_time_ms)
2289 unsigned char saddr = 0, lun = 0;
2295 user = acquire_ipmi_user(user, &index);
2299 rv = check_addr(user->intf, addr, &saddr, &lun);
2301 rv = i_ipmi_request(user,
2314 release_ipmi_user(user, index);
2317 EXPORT_SYMBOL(ipmi_request_settime);
2319 int ipmi_request_supply_msgs(struct ipmi_user *user,
2320 struct ipmi_addr *addr,
2322 struct kernel_ipmi_msg *msg,
2323 void *user_msg_data,
2325 struct ipmi_recv_msg *supplied_recv,
2328 unsigned char saddr = 0, lun = 0;
2334 user = acquire_ipmi_user(user, &index);
2338 rv = check_addr(user->intf, addr, &saddr, &lun);
2340 rv = i_ipmi_request(user,
2353 release_ipmi_user(user, index);
2356 EXPORT_SYMBOL(ipmi_request_supply_msgs);
2358 static void bmc_device_id_handler(struct ipmi_smi *intf,
2359 struct ipmi_recv_msg *msg)
2363 if ((msg->addr.addr_type != IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
2364 || (msg->msg.netfn != IPMI_NETFN_APP_RESPONSE)
2365 || (msg->msg.cmd != IPMI_GET_DEVICE_ID_CMD)) {
2366 dev_warn(intf->si_dev,
2367 "invalid device_id msg: addr_type=%d netfn=%x cmd=%x\n",
2368 msg->addr.addr_type, msg->msg.netfn, msg->msg.cmd);
2372 rv = ipmi_demangle_device_id(msg->msg.netfn, msg->msg.cmd,
2373 msg->msg.data, msg->msg.data_len, &intf->bmc->fetch_id);
2375 dev_warn(intf->si_dev, "device id demangle failed: %d\n", rv);
2376 /* record completion code when error */
2377 intf->bmc->cc = msg->msg.data[0];
2378 intf->bmc->dyn_id_set = 0;
2381 * Make sure the id data is available before setting
2385 intf->bmc->dyn_id_set = 1;
2388 wake_up(&intf->waitq);
2392 send_get_device_id_cmd(struct ipmi_smi *intf)
2394 struct ipmi_system_interface_addr si;
2395 struct kernel_ipmi_msg msg;
2397 si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2398 si.channel = IPMI_BMC_CHANNEL;
2401 msg.netfn = IPMI_NETFN_APP_REQUEST;
2402 msg.cmd = IPMI_GET_DEVICE_ID_CMD;
2406 return i_ipmi_request(NULL,
2408 (struct ipmi_addr *) &si,
2415 intf->addrinfo[0].address,
2416 intf->addrinfo[0].lun,
2420 static int __get_device_id(struct ipmi_smi *intf, struct bmc_device *bmc)
2423 unsigned int retry_count = 0;
2425 intf->null_user_handler = bmc_device_id_handler;
2429 bmc->dyn_id_set = 2;
2431 rv = send_get_device_id_cmd(intf);
2433 goto out_reset_handler;
2435 wait_event(intf->waitq, bmc->dyn_id_set != 2);
2437 if (!bmc->dyn_id_set) {
2438 if (bmc->cc != IPMI_CC_NO_ERROR &&
2439 ++retry_count <= GET_DEVICE_ID_MAX_RETRY) {
2441 dev_warn(intf->si_dev,
2442 "BMC returned 0x%2.2x, retry get bmc device id\n",
2447 rv = -EIO; /* Something went wrong in the fetch. */
2450 /* dyn_id_set makes the id data available. */
2454 intf->null_user_handler = NULL;
2460 * Fetch the device id for the bmc/interface. You must pass in either
2461 * bmc or intf, this code will get the other one. If the data has
2462 * been recently fetched, this will just use the cached data. Otherwise
2463 * it will run a new fetch.
2465 * Except for the first time this is called (in ipmi_add_smi()),
2466 * this will always return good data;
2468 static int __bmc_get_device_id(struct ipmi_smi *intf, struct bmc_device *bmc,
2469 struct ipmi_device_id *id,
2470 bool *guid_set, guid_t *guid, int intf_num)
2473 int prev_dyn_id_set, prev_guid_set;
2474 bool intf_set = intf != NULL;
2477 mutex_lock(&bmc->dyn_mutex);
2479 if (list_empty(&bmc->intfs)) {
2480 mutex_unlock(&bmc->dyn_mutex);
2483 intf = list_first_entry(&bmc->intfs, struct ipmi_smi,
2485 kref_get(&intf->refcount);
2486 mutex_unlock(&bmc->dyn_mutex);
2487 mutex_lock(&intf->bmc_reg_mutex);
2488 mutex_lock(&bmc->dyn_mutex);
2489 if (intf != list_first_entry(&bmc->intfs, struct ipmi_smi,
2491 mutex_unlock(&intf->bmc_reg_mutex);
2492 kref_put(&intf->refcount, intf_free);
2493 goto retry_bmc_lock;
2496 mutex_lock(&intf->bmc_reg_mutex);
2498 mutex_lock(&bmc->dyn_mutex);
2499 kref_get(&intf->refcount);
2502 /* If we have a valid and current ID, just return that. */
2503 if (intf->in_bmc_register ||
2504 (bmc->dyn_id_set && time_is_after_jiffies(bmc->dyn_id_expiry)))
2505 goto out_noprocessing;
2507 prev_guid_set = bmc->dyn_guid_set;
2510 prev_dyn_id_set = bmc->dyn_id_set;
2511 rv = __get_device_id(intf, bmc);
2516 * The guid, device id, manufacturer id, and product id should
2517 * not change on a BMC. If it does we have to do some dancing.
2519 if (!intf->bmc_registered
2520 || (!prev_guid_set && bmc->dyn_guid_set)
2521 || (!prev_dyn_id_set && bmc->dyn_id_set)
2522 || (prev_guid_set && bmc->dyn_guid_set
2523 && !guid_equal(&bmc->guid, &bmc->fetch_guid))
2524 || bmc->id.device_id != bmc->fetch_id.device_id
2525 || bmc->id.manufacturer_id != bmc->fetch_id.manufacturer_id
2526 || bmc->id.product_id != bmc->fetch_id.product_id) {
2527 struct ipmi_device_id id = bmc->fetch_id;
2528 int guid_set = bmc->dyn_guid_set;
2531 guid = bmc->fetch_guid;
2532 mutex_unlock(&bmc->dyn_mutex);
2534 __ipmi_bmc_unregister(intf);
2535 /* Fill in the temporary BMC for good measure. */
2537 intf->bmc->dyn_guid_set = guid_set;
2538 intf->bmc->guid = guid;
2539 if (__ipmi_bmc_register(intf, &id, guid_set, &guid, intf_num))
2540 need_waiter(intf); /* Retry later on an error. */
2542 __scan_channels(intf, &id);
2547 * We weren't given the interface on the
2548 * command line, so restart the operation on
2549 * the next interface for the BMC.
2551 mutex_unlock(&intf->bmc_reg_mutex);
2552 mutex_lock(&bmc->dyn_mutex);
2553 goto retry_bmc_lock;
2556 /* We have a new BMC, set it up. */
2558 mutex_lock(&bmc->dyn_mutex);
2559 goto out_noprocessing;
2560 } else if (memcmp(&bmc->fetch_id, &bmc->id, sizeof(bmc->id)))
2561 /* Version info changes, scan the channels again. */
2562 __scan_channels(intf, &bmc->fetch_id);
2564 bmc->dyn_id_expiry = jiffies + IPMI_DYN_DEV_ID_EXPIRY;
2567 if (rv && prev_dyn_id_set) {
2568 rv = 0; /* Ignore failures if we have previous data. */
2569 bmc->dyn_id_set = prev_dyn_id_set;
2572 bmc->id = bmc->fetch_id;
2573 if (bmc->dyn_guid_set)
2574 bmc->guid = bmc->fetch_guid;
2575 else if (prev_guid_set)
2577 * The guid used to be valid and it failed to fetch,
2578 * just use the cached value.
2580 bmc->dyn_guid_set = prev_guid_set;
2588 *guid_set = bmc->dyn_guid_set;
2590 if (guid && bmc->dyn_guid_set)
2594 mutex_unlock(&bmc->dyn_mutex);
2595 mutex_unlock(&intf->bmc_reg_mutex);
2597 kref_put(&intf->refcount, intf_free);
2601 static int bmc_get_device_id(struct ipmi_smi *intf, struct bmc_device *bmc,
2602 struct ipmi_device_id *id,
2603 bool *guid_set, guid_t *guid)
2605 return __bmc_get_device_id(intf, bmc, id, guid_set, guid, -1);
2608 static ssize_t device_id_show(struct device *dev,
2609 struct device_attribute *attr,
2612 struct bmc_device *bmc = to_bmc_device(dev);
2613 struct ipmi_device_id id;
2616 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2620 return snprintf(buf, 10, "%u\n", id.device_id);
2622 static DEVICE_ATTR_RO(device_id);
2624 static ssize_t provides_device_sdrs_show(struct device *dev,
2625 struct device_attribute *attr,
2628 struct bmc_device *bmc = to_bmc_device(dev);
2629 struct ipmi_device_id id;
2632 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2636 return snprintf(buf, 10, "%u\n", (id.device_revision & 0x80) >> 7);
2638 static DEVICE_ATTR_RO(provides_device_sdrs);
2640 static ssize_t revision_show(struct device *dev, struct device_attribute *attr,
2643 struct bmc_device *bmc = to_bmc_device(dev);
2644 struct ipmi_device_id id;
2647 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2651 return snprintf(buf, 20, "%u\n", id.device_revision & 0x0F);
2653 static DEVICE_ATTR_RO(revision);
2655 static ssize_t firmware_revision_show(struct device *dev,
2656 struct device_attribute *attr,
2659 struct bmc_device *bmc = to_bmc_device(dev);
2660 struct ipmi_device_id id;
2663 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2667 return snprintf(buf, 20, "%u.%x\n", id.firmware_revision_1,
2668 id.firmware_revision_2);
2670 static DEVICE_ATTR_RO(firmware_revision);
2672 static ssize_t ipmi_version_show(struct device *dev,
2673 struct device_attribute *attr,
2676 struct bmc_device *bmc = to_bmc_device(dev);
2677 struct ipmi_device_id id;
2680 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2684 return snprintf(buf, 20, "%u.%u\n",
2685 ipmi_version_major(&id),
2686 ipmi_version_minor(&id));
2688 static DEVICE_ATTR_RO(ipmi_version);
2690 static ssize_t add_dev_support_show(struct device *dev,
2691 struct device_attribute *attr,
2694 struct bmc_device *bmc = to_bmc_device(dev);
2695 struct ipmi_device_id id;
2698 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2702 return snprintf(buf, 10, "0x%02x\n", id.additional_device_support);
2704 static DEVICE_ATTR(additional_device_support, S_IRUGO, add_dev_support_show,
2707 static ssize_t manufacturer_id_show(struct device *dev,
2708 struct device_attribute *attr,
2711 struct bmc_device *bmc = to_bmc_device(dev);
2712 struct ipmi_device_id id;
2715 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2719 return snprintf(buf, 20, "0x%6.6x\n", id.manufacturer_id);
2721 static DEVICE_ATTR_RO(manufacturer_id);
2723 static ssize_t product_id_show(struct device *dev,
2724 struct device_attribute *attr,
2727 struct bmc_device *bmc = to_bmc_device(dev);
2728 struct ipmi_device_id id;
2731 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2735 return snprintf(buf, 10, "0x%4.4x\n", id.product_id);
2737 static DEVICE_ATTR_RO(product_id);
2739 static ssize_t aux_firmware_rev_show(struct device *dev,
2740 struct device_attribute *attr,
2743 struct bmc_device *bmc = to_bmc_device(dev);
2744 struct ipmi_device_id id;
2747 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2751 return snprintf(buf, 21, "0x%02x 0x%02x 0x%02x 0x%02x\n",
2752 id.aux_firmware_revision[3],
2753 id.aux_firmware_revision[2],
2754 id.aux_firmware_revision[1],
2755 id.aux_firmware_revision[0]);
2757 static DEVICE_ATTR(aux_firmware_revision, S_IRUGO, aux_firmware_rev_show, NULL);
2759 static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
2762 struct bmc_device *bmc = to_bmc_device(dev);
2767 rv = bmc_get_device_id(NULL, bmc, NULL, &guid_set, &guid);
2773 return snprintf(buf, UUID_STRING_LEN + 1 + 1, "%pUl\n", &guid);
2775 static DEVICE_ATTR_RO(guid);
2777 static struct attribute *bmc_dev_attrs[] = {
2778 &dev_attr_device_id.attr,
2779 &dev_attr_provides_device_sdrs.attr,
2780 &dev_attr_revision.attr,
2781 &dev_attr_firmware_revision.attr,
2782 &dev_attr_ipmi_version.attr,
2783 &dev_attr_additional_device_support.attr,
2784 &dev_attr_manufacturer_id.attr,
2785 &dev_attr_product_id.attr,
2786 &dev_attr_aux_firmware_revision.attr,
2787 &dev_attr_guid.attr,
2791 static umode_t bmc_dev_attr_is_visible(struct kobject *kobj,
2792 struct attribute *attr, int idx)
2794 struct device *dev = kobj_to_dev(kobj);
2795 struct bmc_device *bmc = to_bmc_device(dev);
2796 umode_t mode = attr->mode;
2799 if (attr == &dev_attr_aux_firmware_revision.attr) {
2800 struct ipmi_device_id id;
2802 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2803 return (!rv && id.aux_firmware_revision_set) ? mode : 0;
2805 if (attr == &dev_attr_guid.attr) {
2808 rv = bmc_get_device_id(NULL, bmc, NULL, &guid_set, NULL);
2809 return (!rv && guid_set) ? mode : 0;
2814 static const struct attribute_group bmc_dev_attr_group = {
2815 .attrs = bmc_dev_attrs,
2816 .is_visible = bmc_dev_attr_is_visible,
2819 static const struct attribute_group *bmc_dev_attr_groups[] = {
2820 &bmc_dev_attr_group,
2824 static const struct device_type bmc_device_type = {
2825 .groups = bmc_dev_attr_groups,
2828 static int __find_bmc_guid(struct device *dev, const void *data)
2830 const guid_t *guid = data;
2831 struct bmc_device *bmc;
2834 if (dev->type != &bmc_device_type)
2837 bmc = to_bmc_device(dev);
2838 rv = bmc->dyn_guid_set && guid_equal(&bmc->guid, guid);
2840 rv = kref_get_unless_zero(&bmc->usecount);
2845 * Returns with the bmc's usecount incremented, if it is non-NULL.
2847 static struct bmc_device *ipmi_find_bmc_guid(struct device_driver *drv,
2851 struct bmc_device *bmc = NULL;
2853 dev = driver_find_device(drv, NULL, guid, __find_bmc_guid);
2855 bmc = to_bmc_device(dev);
2861 struct prod_dev_id {
2862 unsigned int product_id;
2863 unsigned char device_id;
2866 static int __find_bmc_prod_dev_id(struct device *dev, const void *data)
2868 const struct prod_dev_id *cid = data;
2869 struct bmc_device *bmc;
2872 if (dev->type != &bmc_device_type)
2875 bmc = to_bmc_device(dev);
2876 rv = (bmc->id.product_id == cid->product_id
2877 && bmc->id.device_id == cid->device_id);
2879 rv = kref_get_unless_zero(&bmc->usecount);
2884 * Returns with the bmc's usecount incremented, if it is non-NULL.
2886 static struct bmc_device *ipmi_find_bmc_prod_dev_id(
2887 struct device_driver *drv,
2888 unsigned int product_id, unsigned char device_id)
2890 struct prod_dev_id id = {
2891 .product_id = product_id,
2892 .device_id = device_id,
2895 struct bmc_device *bmc = NULL;
2897 dev = driver_find_device(drv, NULL, &id, __find_bmc_prod_dev_id);
2899 bmc = to_bmc_device(dev);
2905 static DEFINE_IDA(ipmi_bmc_ida);
2908 release_bmc_device(struct device *dev)
2910 kfree(to_bmc_device(dev));
2913 static void cleanup_bmc_work(struct work_struct *work)
2915 struct bmc_device *bmc = container_of(work, struct bmc_device,
2917 int id = bmc->pdev.id; /* Unregister overwrites id */
2919 platform_device_unregister(&bmc->pdev);
2920 ida_simple_remove(&ipmi_bmc_ida, id);
2924 cleanup_bmc_device(struct kref *ref)
2926 struct bmc_device *bmc = container_of(ref, struct bmc_device, usecount);
2929 * Remove the platform device in a work queue to avoid issues
2930 * with removing the device attributes while reading a device
2933 schedule_work(&bmc->remove_work);
2937 * Must be called with intf->bmc_reg_mutex held.
2939 static void __ipmi_bmc_unregister(struct ipmi_smi *intf)
2941 struct bmc_device *bmc = intf->bmc;
2943 if (!intf->bmc_registered)
2946 sysfs_remove_link(&intf->si_dev->kobj, "bmc");
2947 sysfs_remove_link(&bmc->pdev.dev.kobj, intf->my_dev_name);
2948 kfree(intf->my_dev_name);
2949 intf->my_dev_name = NULL;
2951 mutex_lock(&bmc->dyn_mutex);
2952 list_del(&intf->bmc_link);
2953 mutex_unlock(&bmc->dyn_mutex);
2954 intf->bmc = &intf->tmp_bmc;
2955 kref_put(&bmc->usecount, cleanup_bmc_device);
2956 intf->bmc_registered = false;
2959 static void ipmi_bmc_unregister(struct ipmi_smi *intf)
2961 mutex_lock(&intf->bmc_reg_mutex);
2962 __ipmi_bmc_unregister(intf);
2963 mutex_unlock(&intf->bmc_reg_mutex);
2967 * Must be called with intf->bmc_reg_mutex held.
2969 static int __ipmi_bmc_register(struct ipmi_smi *intf,
2970 struct ipmi_device_id *id,
2971 bool guid_set, guid_t *guid, int intf_num)
2974 struct bmc_device *bmc;
2975 struct bmc_device *old_bmc;
2978 * platform_device_register() can cause bmc_reg_mutex to
2979 * be claimed because of the is_visible functions of
2980 * the attributes. Eliminate possible recursion and
2983 intf->in_bmc_register = true;
2984 mutex_unlock(&intf->bmc_reg_mutex);
2987 * Try to find if there is an bmc_device struct
2988 * representing the interfaced BMC already
2990 mutex_lock(&ipmidriver_mutex);
2992 old_bmc = ipmi_find_bmc_guid(&ipmidriver.driver, guid);
2994 old_bmc = ipmi_find_bmc_prod_dev_id(&ipmidriver.driver,
2999 * If there is already an bmc_device, free the new one,
3000 * otherwise register the new BMC device
3005 * Note: old_bmc already has usecount incremented by
3006 * the BMC find functions.
3008 intf->bmc = old_bmc;
3009 mutex_lock(&bmc->dyn_mutex);
3010 list_add_tail(&intf->bmc_link, &bmc->intfs);
3011 mutex_unlock(&bmc->dyn_mutex);
3013 dev_info(intf->si_dev,
3014 "interfacing existing BMC (man_id: 0x%6.6x, prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
3015 bmc->id.manufacturer_id,
3019 bmc = kzalloc(sizeof(*bmc), GFP_KERNEL);
3024 INIT_LIST_HEAD(&bmc->intfs);
3025 mutex_init(&bmc->dyn_mutex);
3026 INIT_WORK(&bmc->remove_work, cleanup_bmc_work);
3029 bmc->dyn_id_set = 1;
3030 bmc->dyn_guid_set = guid_set;
3032 bmc->dyn_id_expiry = jiffies + IPMI_DYN_DEV_ID_EXPIRY;
3034 bmc->pdev.name = "ipmi_bmc";
3036 rv = ida_simple_get(&ipmi_bmc_ida, 0, 0, GFP_KERNEL);
3042 bmc->pdev.dev.driver = &ipmidriver.driver;
3044 bmc->pdev.dev.release = release_bmc_device;
3045 bmc->pdev.dev.type = &bmc_device_type;
3046 kref_init(&bmc->usecount);
3049 mutex_lock(&bmc->dyn_mutex);
3050 list_add_tail(&intf->bmc_link, &bmc->intfs);
3051 mutex_unlock(&bmc->dyn_mutex);
3053 rv = platform_device_register(&bmc->pdev);
3055 dev_err(intf->si_dev,
3056 "Unable to register bmc device: %d\n",
3061 dev_info(intf->si_dev,
3062 "Found new BMC (man_id: 0x%6.6x, prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
3063 bmc->id.manufacturer_id,
3069 * create symlink from system interface device to bmc device
3072 rv = sysfs_create_link(&intf->si_dev->kobj, &bmc->pdev.dev.kobj, "bmc");
3074 dev_err(intf->si_dev, "Unable to create bmc symlink: %d\n", rv);
3079 intf_num = intf->intf_num;
3080 intf->my_dev_name = kasprintf(GFP_KERNEL, "ipmi%d", intf_num);
3081 if (!intf->my_dev_name) {
3083 dev_err(intf->si_dev, "Unable to allocate link from BMC: %d\n",
3088 rv = sysfs_create_link(&bmc->pdev.dev.kobj, &intf->si_dev->kobj,
3091 dev_err(intf->si_dev, "Unable to create symlink to bmc: %d\n",
3093 goto out_free_my_dev_name;
3096 intf->bmc_registered = true;
3099 mutex_unlock(&ipmidriver_mutex);
3100 mutex_lock(&intf->bmc_reg_mutex);
3101 intf->in_bmc_register = false;
3105 out_free_my_dev_name:
3106 kfree(intf->my_dev_name);
3107 intf->my_dev_name = NULL;
3110 sysfs_remove_link(&intf->si_dev->kobj, "bmc");
3113 mutex_lock(&bmc->dyn_mutex);
3114 list_del(&intf->bmc_link);
3115 mutex_unlock(&bmc->dyn_mutex);
3116 intf->bmc = &intf->tmp_bmc;
3117 kref_put(&bmc->usecount, cleanup_bmc_device);
3121 mutex_lock(&bmc->dyn_mutex);
3122 list_del(&intf->bmc_link);
3123 mutex_unlock(&bmc->dyn_mutex);
3124 intf->bmc = &intf->tmp_bmc;
3125 put_device(&bmc->pdev.dev);
3130 send_guid_cmd(struct ipmi_smi *intf, int chan)
3132 struct kernel_ipmi_msg msg;
3133 struct ipmi_system_interface_addr si;
3135 si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3136 si.channel = IPMI_BMC_CHANNEL;
3139 msg.netfn = IPMI_NETFN_APP_REQUEST;
3140 msg.cmd = IPMI_GET_DEVICE_GUID_CMD;
3143 return i_ipmi_request(NULL,
3145 (struct ipmi_addr *) &si,
3152 intf->addrinfo[0].address,
3153 intf->addrinfo[0].lun,
3157 static void guid_handler(struct ipmi_smi *intf, struct ipmi_recv_msg *msg)
3159 struct bmc_device *bmc = intf->bmc;
3161 if ((msg->addr.addr_type != IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
3162 || (msg->msg.netfn != IPMI_NETFN_APP_RESPONSE)
3163 || (msg->msg.cmd != IPMI_GET_DEVICE_GUID_CMD))
3167 if (msg->msg.data[0] != 0) {
3168 /* Error from getting the GUID, the BMC doesn't have one. */
3169 bmc->dyn_guid_set = 0;
3173 if (msg->msg.data_len < UUID_SIZE + 1) {
3174 bmc->dyn_guid_set = 0;
3175 dev_warn(intf->si_dev,
3176 "The GUID response from the BMC was too short, it was %d but should have been %d. Assuming GUID is not available.\n",
3177 msg->msg.data_len, UUID_SIZE + 1);
3181 import_guid(&bmc->fetch_guid, msg->msg.data + 1);
3183 * Make sure the guid data is available before setting
3187 bmc->dyn_guid_set = 1;
3189 wake_up(&intf->waitq);
3192 static void __get_guid(struct ipmi_smi *intf)
3195 struct bmc_device *bmc = intf->bmc;
3197 bmc->dyn_guid_set = 2;
3198 intf->null_user_handler = guid_handler;
3199 rv = send_guid_cmd(intf, 0);
3201 /* Send failed, no GUID available. */
3202 bmc->dyn_guid_set = 0;
3204 wait_event(intf->waitq, bmc->dyn_guid_set != 2);
3206 /* dyn_guid_set makes the guid data available. */
3209 intf->null_user_handler = NULL;
3213 send_channel_info_cmd(struct ipmi_smi *intf, int chan)
3215 struct kernel_ipmi_msg msg;
3216 unsigned char data[1];
3217 struct ipmi_system_interface_addr si;
3219 si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3220 si.channel = IPMI_BMC_CHANNEL;
3223 msg.netfn = IPMI_NETFN_APP_REQUEST;
3224 msg.cmd = IPMI_GET_CHANNEL_INFO_CMD;
3228 return i_ipmi_request(NULL,
3230 (struct ipmi_addr *) &si,
3237 intf->addrinfo[0].address,
3238 intf->addrinfo[0].lun,
3243 channel_handler(struct ipmi_smi *intf, struct ipmi_recv_msg *msg)
3247 unsigned int set = intf->curr_working_cset;
3248 struct ipmi_channel *chans;
3250 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
3251 && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
3252 && (msg->msg.cmd == IPMI_GET_CHANNEL_INFO_CMD)) {
3253 /* It's the one we want */
3254 if (msg->msg.data[0] != 0) {
3255 /* Got an error from the channel, just go on. */
3256 if (msg->msg.data[0] == IPMI_INVALID_COMMAND_ERR) {
3258 * If the MC does not support this
3259 * command, that is legal. We just
3260 * assume it has one IPMB at channel
3263 intf->wchannels[set].c[0].medium
3264 = IPMI_CHANNEL_MEDIUM_IPMB;
3265 intf->wchannels[set].c[0].protocol
3266 = IPMI_CHANNEL_PROTOCOL_IPMB;
3268 intf->channel_list = intf->wchannels + set;
3269 intf->channels_ready = true;
3270 wake_up(&intf->waitq);
3275 if (msg->msg.data_len < 4) {
3276 /* Message not big enough, just go on. */
3279 ch = intf->curr_channel;
3280 chans = intf->wchannels[set].c;
3281 chans[ch].medium = msg->msg.data[2] & 0x7f;
3282 chans[ch].protocol = msg->msg.data[3] & 0x1f;
3285 intf->curr_channel++;
3286 if (intf->curr_channel >= IPMI_MAX_CHANNELS) {
3287 intf->channel_list = intf->wchannels + set;
3288 intf->channels_ready = true;
3289 wake_up(&intf->waitq);
3291 intf->channel_list = intf->wchannels + set;
3292 intf->channels_ready = true;
3293 rv = send_channel_info_cmd(intf, intf->curr_channel);
3297 /* Got an error somehow, just give up. */
3298 dev_warn(intf->si_dev,
3299 "Error sending channel information for channel %d: %d\n",
3300 intf->curr_channel, rv);
3302 intf->channel_list = intf->wchannels + set;
3303 intf->channels_ready = true;
3304 wake_up(&intf->waitq);
3312 * Must be holding intf->bmc_reg_mutex to call this.
3314 static int __scan_channels(struct ipmi_smi *intf, struct ipmi_device_id *id)
3318 if (ipmi_version_major(id) > 1
3319 || (ipmi_version_major(id) == 1
3320 && ipmi_version_minor(id) >= 5)) {
3324 * Start scanning the channels to see what is
3327 set = !intf->curr_working_cset;
3328 intf->curr_working_cset = set;
3329 memset(&intf->wchannels[set], 0,
3330 sizeof(struct ipmi_channel_set));
3332 intf->null_user_handler = channel_handler;
3333 intf->curr_channel = 0;
3334 rv = send_channel_info_cmd(intf, 0);
3336 dev_warn(intf->si_dev,
3337 "Error sending channel information for channel 0, %d\n",
3339 intf->null_user_handler = NULL;
3343 /* Wait for the channel info to be read. */
3344 wait_event(intf->waitq, intf->channels_ready);
3345 intf->null_user_handler = NULL;
3347 unsigned int set = intf->curr_working_cset;
3349 /* Assume a single IPMB channel at zero. */
3350 intf->wchannels[set].c[0].medium = IPMI_CHANNEL_MEDIUM_IPMB;
3351 intf->wchannels[set].c[0].protocol = IPMI_CHANNEL_PROTOCOL_IPMB;
3352 intf->channel_list = intf->wchannels + set;
3353 intf->channels_ready = true;
3359 static void ipmi_poll(struct ipmi_smi *intf)
3361 if (intf->handlers->poll)
3362 intf->handlers->poll(intf->send_info);
3363 /* In case something came in */
3364 handle_new_recv_msgs(intf);
3367 void ipmi_poll_interface(struct ipmi_user *user)
3369 ipmi_poll(user->intf);
3371 EXPORT_SYMBOL(ipmi_poll_interface);
3373 static void redo_bmc_reg(struct work_struct *work)
3375 struct ipmi_smi *intf = container_of(work, struct ipmi_smi,
3378 if (!intf->in_shutdown)
3379 bmc_get_device_id(intf, NULL, NULL, NULL, NULL);
3381 kref_put(&intf->refcount, intf_free);
3384 int ipmi_add_smi(struct module *owner,
3385 const struct ipmi_smi_handlers *handlers,
3387 struct device *si_dev,
3388 unsigned char slave_addr)
3392 struct ipmi_smi *intf, *tintf;
3393 struct list_head *link;
3394 struct ipmi_device_id id;
3397 * Make sure the driver is actually initialized, this handles
3398 * problems with initialization order.
3400 rv = ipmi_init_msghandler();
3404 intf = kzalloc(sizeof(*intf), GFP_KERNEL);
3408 rv = init_srcu_struct(&intf->users_srcu);
3414 intf->owner = owner;
3415 intf->bmc = &intf->tmp_bmc;
3416 INIT_LIST_HEAD(&intf->bmc->intfs);
3417 mutex_init(&intf->bmc->dyn_mutex);
3418 INIT_LIST_HEAD(&intf->bmc_link);
3419 mutex_init(&intf->bmc_reg_mutex);
3420 intf->intf_num = -1; /* Mark it invalid for now. */
3421 kref_init(&intf->refcount);
3422 INIT_WORK(&intf->bmc_reg_work, redo_bmc_reg);
3423 intf->si_dev = si_dev;
3424 for (j = 0; j < IPMI_MAX_CHANNELS; j++) {
3425 intf->addrinfo[j].address = IPMI_BMC_SLAVE_ADDR;
3426 intf->addrinfo[j].lun = 2;
3428 if (slave_addr != 0)
3429 intf->addrinfo[0].address = slave_addr;
3430 INIT_LIST_HEAD(&intf->users);
3431 intf->handlers = handlers;
3432 intf->send_info = send_info;
3433 spin_lock_init(&intf->seq_lock);
3434 for (j = 0; j < IPMI_IPMB_NUM_SEQ; j++) {
3435 intf->seq_table[j].inuse = 0;
3436 intf->seq_table[j].seqid = 0;
3439 spin_lock_init(&intf->waiting_rcv_msgs_lock);
3440 INIT_LIST_HEAD(&intf->waiting_rcv_msgs);
3441 tasklet_setup(&intf->recv_tasklet,
3443 atomic_set(&intf->watchdog_pretimeouts_to_deliver, 0);
3444 spin_lock_init(&intf->xmit_msgs_lock);
3445 INIT_LIST_HEAD(&intf->xmit_msgs);
3446 INIT_LIST_HEAD(&intf->hp_xmit_msgs);
3447 spin_lock_init(&intf->events_lock);
3448 spin_lock_init(&intf->watch_lock);
3449 atomic_set(&intf->event_waiters, 0);
3450 intf->ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
3451 INIT_LIST_HEAD(&intf->waiting_events);
3452 intf->waiting_events_count = 0;
3453 mutex_init(&intf->cmd_rcvrs_mutex);
3454 spin_lock_init(&intf->maintenance_mode_lock);
3455 INIT_LIST_HEAD(&intf->cmd_rcvrs);
3456 init_waitqueue_head(&intf->waitq);
3457 for (i = 0; i < IPMI_NUM_STATS; i++)
3458 atomic_set(&intf->stats[i], 0);
3460 mutex_lock(&ipmi_interfaces_mutex);
3461 /* Look for a hole in the numbers. */
3463 link = &ipmi_interfaces;
3464 list_for_each_entry_rcu(tintf, &ipmi_interfaces, link,
3465 ipmi_interfaces_mutex_held()) {
3466 if (tintf->intf_num != i) {
3467 link = &tintf->link;
3472 /* Add the new interface in numeric order. */
3474 list_add_rcu(&intf->link, &ipmi_interfaces);
3476 list_add_tail_rcu(&intf->link, link);
3478 rv = handlers->start_processing(send_info, intf);
3482 rv = __bmc_get_device_id(intf, NULL, &id, NULL, NULL, i);
3484 dev_err(si_dev, "Unable to get the device id: %d\n", rv);
3485 goto out_err_started;
3488 mutex_lock(&intf->bmc_reg_mutex);
3489 rv = __scan_channels(intf, &id);
3490 mutex_unlock(&intf->bmc_reg_mutex);
3492 goto out_err_bmc_reg;
3495 * Keep memory order straight for RCU readers. Make
3496 * sure everything else is committed to memory before
3497 * setting intf_num to mark the interface valid.
3501 mutex_unlock(&ipmi_interfaces_mutex);
3503 /* After this point the interface is legal to use. */
3504 call_smi_watchers(i, intf->si_dev);
3509 ipmi_bmc_unregister(intf);
3511 if (intf->handlers->shutdown)
3512 intf->handlers->shutdown(intf->send_info);
3514 list_del_rcu(&intf->link);
3515 mutex_unlock(&ipmi_interfaces_mutex);
3516 synchronize_srcu(&ipmi_interfaces_srcu);
3517 cleanup_srcu_struct(&intf->users_srcu);
3518 kref_put(&intf->refcount, intf_free);
3522 EXPORT_SYMBOL(ipmi_add_smi);
3524 static void deliver_smi_err_response(struct ipmi_smi *intf,
3525 struct ipmi_smi_msg *msg,
3528 msg->rsp[0] = msg->data[0] | 4;
3529 msg->rsp[1] = msg->data[1];
3532 /* It's an error, so it will never requeue, no need to check return. */
3533 handle_one_recv_msg(intf, msg);
3536 static void cleanup_smi_msgs(struct ipmi_smi *intf)
3539 struct seq_table *ent;
3540 struct ipmi_smi_msg *msg;
3541 struct list_head *entry;
3542 struct list_head tmplist;
3544 /* Clear out our transmit queues and hold the messages. */
3545 INIT_LIST_HEAD(&tmplist);
3546 list_splice_tail(&intf->hp_xmit_msgs, &tmplist);
3547 list_splice_tail(&intf->xmit_msgs, &tmplist);
3549 /* Current message first, to preserve order */
3550 while (intf->curr_msg && !list_empty(&intf->waiting_rcv_msgs)) {
3551 /* Wait for the message to clear out. */
3552 schedule_timeout(1);
3555 /* No need for locks, the interface is down. */
3558 * Return errors for all pending messages in queue and in the
3559 * tables waiting for remote responses.
3561 while (!list_empty(&tmplist)) {
3562 entry = tmplist.next;
3564 msg = list_entry(entry, struct ipmi_smi_msg, link);
3565 deliver_smi_err_response(intf, msg, IPMI_ERR_UNSPECIFIED);
3568 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
3569 ent = &intf->seq_table[i];
3572 deliver_err_response(intf, ent->recv_msg, IPMI_ERR_UNSPECIFIED);
3576 void ipmi_unregister_smi(struct ipmi_smi *intf)
3578 struct ipmi_smi_watcher *w;
3579 int intf_num = intf->intf_num, index;
3581 mutex_lock(&ipmi_interfaces_mutex);
3582 intf->intf_num = -1;
3583 intf->in_shutdown = true;
3584 list_del_rcu(&intf->link);
3585 mutex_unlock(&ipmi_interfaces_mutex);
3586 synchronize_srcu(&ipmi_interfaces_srcu);
3588 /* At this point no users can be added to the interface. */
3591 * Call all the watcher interfaces to tell them that
3592 * an interface is going away.
3594 mutex_lock(&smi_watchers_mutex);
3595 list_for_each_entry(w, &smi_watchers, link)
3596 w->smi_gone(intf_num);
3597 mutex_unlock(&smi_watchers_mutex);
3599 index = srcu_read_lock(&intf->users_srcu);
3600 while (!list_empty(&intf->users)) {
3601 struct ipmi_user *user =
3602 container_of(list_next_rcu(&intf->users),
3603 struct ipmi_user, link);
3605 _ipmi_destroy_user(user);
3607 srcu_read_unlock(&intf->users_srcu, index);
3609 if (intf->handlers->shutdown)
3610 intf->handlers->shutdown(intf->send_info);
3612 cleanup_smi_msgs(intf);
3614 ipmi_bmc_unregister(intf);
3616 cleanup_srcu_struct(&intf->users_srcu);
3617 kref_put(&intf->refcount, intf_free);
3619 EXPORT_SYMBOL(ipmi_unregister_smi);
3621 static int handle_ipmb_get_msg_rsp(struct ipmi_smi *intf,
3622 struct ipmi_smi_msg *msg)
3624 struct ipmi_ipmb_addr ipmb_addr;
3625 struct ipmi_recv_msg *recv_msg;
3628 * This is 11, not 10, because the response must contain a
3631 if (msg->rsp_size < 11) {
3632 /* Message not big enough, just ignore it. */
3633 ipmi_inc_stat(intf, invalid_ipmb_responses);
3637 if (msg->rsp[2] != 0) {
3638 /* An error getting the response, just ignore it. */
3642 ipmb_addr.addr_type = IPMI_IPMB_ADDR_TYPE;
3643 ipmb_addr.slave_addr = msg->rsp[6];
3644 ipmb_addr.channel = msg->rsp[3] & 0x0f;
3645 ipmb_addr.lun = msg->rsp[7] & 3;
3648 * It's a response from a remote entity. Look up the sequence
3649 * number and handle the response.
3651 if (intf_find_seq(intf,
3655 (msg->rsp[4] >> 2) & (~1),
3656 (struct ipmi_addr *) &ipmb_addr,
3659 * We were unable to find the sequence number,
3660 * so just nuke the message.
3662 ipmi_inc_stat(intf, unhandled_ipmb_responses);
3666 memcpy(recv_msg->msg_data, &msg->rsp[9], msg->rsp_size - 9);
3668 * The other fields matched, so no need to set them, except
3669 * for netfn, which needs to be the response that was
3670 * returned, not the request value.
3672 recv_msg->msg.netfn = msg->rsp[4] >> 2;
3673 recv_msg->msg.data = recv_msg->msg_data;
3674 recv_msg->msg.data_len = msg->rsp_size - 10;
3675 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
3676 if (deliver_response(intf, recv_msg))
3677 ipmi_inc_stat(intf, unhandled_ipmb_responses);
3679 ipmi_inc_stat(intf, handled_ipmb_responses);
3684 static int handle_ipmb_get_msg_cmd(struct ipmi_smi *intf,
3685 struct ipmi_smi_msg *msg)
3687 struct cmd_rcvr *rcvr;
3689 unsigned char netfn;
3692 struct ipmi_user *user = NULL;
3693 struct ipmi_ipmb_addr *ipmb_addr;
3694 struct ipmi_recv_msg *recv_msg;
3696 if (msg->rsp_size < 10) {
3697 /* Message not big enough, just ignore it. */
3698 ipmi_inc_stat(intf, invalid_commands);
3702 if (msg->rsp[2] != 0) {
3703 /* An error getting the response, just ignore it. */
3707 netfn = msg->rsp[4] >> 2;
3709 chan = msg->rsp[3] & 0xf;
3712 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
3715 kref_get(&user->refcount);
3721 /* We didn't find a user, deliver an error response. */
3722 ipmi_inc_stat(intf, unhandled_commands);
3724 msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
3725 msg->data[1] = IPMI_SEND_MSG_CMD;
3726 msg->data[2] = msg->rsp[3];
3727 msg->data[3] = msg->rsp[6];
3728 msg->data[4] = ((netfn + 1) << 2) | (msg->rsp[7] & 0x3);
3729 msg->data[5] = ipmb_checksum(&msg->data[3], 2);
3730 msg->data[6] = intf->addrinfo[msg->rsp[3] & 0xf].address;
3732 msg->data[7] = (msg->rsp[7] & 0xfc) | (msg->rsp[4] & 0x3);
3733 msg->data[8] = msg->rsp[8]; /* cmd */
3734 msg->data[9] = IPMI_INVALID_CMD_COMPLETION_CODE;
3735 msg->data[10] = ipmb_checksum(&msg->data[6], 4);
3736 msg->data_size = 11;
3738 pr_debug("Invalid command: %*ph\n", msg->data_size, msg->data);
3741 if (!intf->in_shutdown) {
3742 smi_send(intf, intf->handlers, msg, 0);
3744 * We used the message, so return the value
3745 * that causes it to not be freed or
3752 recv_msg = ipmi_alloc_recv_msg();
3755 * We couldn't allocate memory for the
3756 * message, so requeue it for handling
3760 kref_put(&user->refcount, free_user);
3762 /* Extract the source address from the data. */
3763 ipmb_addr = (struct ipmi_ipmb_addr *) &recv_msg->addr;
3764 ipmb_addr->addr_type = IPMI_IPMB_ADDR_TYPE;
3765 ipmb_addr->slave_addr = msg->rsp[6];
3766 ipmb_addr->lun = msg->rsp[7] & 3;
3767 ipmb_addr->channel = msg->rsp[3] & 0xf;
3770 * Extract the rest of the message information
3771 * from the IPMB header.
3773 recv_msg->user = user;
3774 recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
3775 recv_msg->msgid = msg->rsp[7] >> 2;
3776 recv_msg->msg.netfn = msg->rsp[4] >> 2;
3777 recv_msg->msg.cmd = msg->rsp[8];
3778 recv_msg->msg.data = recv_msg->msg_data;
3781 * We chop off 10, not 9 bytes because the checksum
3782 * at the end also needs to be removed.
3784 recv_msg->msg.data_len = msg->rsp_size - 10;
3785 memcpy(recv_msg->msg_data, &msg->rsp[9],
3786 msg->rsp_size - 10);
3787 if (deliver_response(intf, recv_msg))
3788 ipmi_inc_stat(intf, unhandled_commands);
3790 ipmi_inc_stat(intf, handled_commands);
3797 static int handle_lan_get_msg_rsp(struct ipmi_smi *intf,
3798 struct ipmi_smi_msg *msg)
3800 struct ipmi_lan_addr lan_addr;
3801 struct ipmi_recv_msg *recv_msg;
3805 * This is 13, not 12, because the response must contain a
3808 if (msg->rsp_size < 13) {
3809 /* Message not big enough, just ignore it. */
3810 ipmi_inc_stat(intf, invalid_lan_responses);
3814 if (msg->rsp[2] != 0) {
3815 /* An error getting the response, just ignore it. */
3819 lan_addr.addr_type = IPMI_LAN_ADDR_TYPE;
3820 lan_addr.session_handle = msg->rsp[4];
3821 lan_addr.remote_SWID = msg->rsp[8];
3822 lan_addr.local_SWID = msg->rsp[5];
3823 lan_addr.channel = msg->rsp[3] & 0x0f;
3824 lan_addr.privilege = msg->rsp[3] >> 4;
3825 lan_addr.lun = msg->rsp[9] & 3;
3828 * It's a response from a remote entity. Look up the sequence
3829 * number and handle the response.
3831 if (intf_find_seq(intf,
3835 (msg->rsp[6] >> 2) & (~1),
3836 (struct ipmi_addr *) &lan_addr,
3839 * We were unable to find the sequence number,
3840 * so just nuke the message.
3842 ipmi_inc_stat(intf, unhandled_lan_responses);
3846 memcpy(recv_msg->msg_data, &msg->rsp[11], msg->rsp_size - 11);
3848 * The other fields matched, so no need to set them, except
3849 * for netfn, which needs to be the response that was
3850 * returned, not the request value.
3852 recv_msg->msg.netfn = msg->rsp[6] >> 2;
3853 recv_msg->msg.data = recv_msg->msg_data;
3854 recv_msg->msg.data_len = msg->rsp_size - 12;
3855 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
3856 if (deliver_response(intf, recv_msg))
3857 ipmi_inc_stat(intf, unhandled_lan_responses);
3859 ipmi_inc_stat(intf, handled_lan_responses);
3864 static int handle_lan_get_msg_cmd(struct ipmi_smi *intf,
3865 struct ipmi_smi_msg *msg)
3867 struct cmd_rcvr *rcvr;
3869 unsigned char netfn;
3872 struct ipmi_user *user = NULL;
3873 struct ipmi_lan_addr *lan_addr;
3874 struct ipmi_recv_msg *recv_msg;
3876 if (msg->rsp_size < 12) {
3877 /* Message not big enough, just ignore it. */
3878 ipmi_inc_stat(intf, invalid_commands);
3882 if (msg->rsp[2] != 0) {
3883 /* An error getting the response, just ignore it. */
3887 netfn = msg->rsp[6] >> 2;
3889 chan = msg->rsp[3] & 0xf;
3892 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
3895 kref_get(&user->refcount);
3901 /* We didn't find a user, just give up. */
3902 ipmi_inc_stat(intf, unhandled_commands);
3905 * Don't do anything with these messages, just allow
3910 recv_msg = ipmi_alloc_recv_msg();
3913 * We couldn't allocate memory for the
3914 * message, so requeue it for handling later.
3917 kref_put(&user->refcount, free_user);
3919 /* Extract the source address from the data. */
3920 lan_addr = (struct ipmi_lan_addr *) &recv_msg->addr;
3921 lan_addr->addr_type = IPMI_LAN_ADDR_TYPE;
3922 lan_addr->session_handle = msg->rsp[4];
3923 lan_addr->remote_SWID = msg->rsp[8];
3924 lan_addr->local_SWID = msg->rsp[5];
3925 lan_addr->lun = msg->rsp[9] & 3;
3926 lan_addr->channel = msg->rsp[3] & 0xf;
3927 lan_addr->privilege = msg->rsp[3] >> 4;
3930 * Extract the rest of the message information
3931 * from the IPMB header.
3933 recv_msg->user = user;
3934 recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
3935 recv_msg->msgid = msg->rsp[9] >> 2;
3936 recv_msg->msg.netfn = msg->rsp[6] >> 2;
3937 recv_msg->msg.cmd = msg->rsp[10];
3938 recv_msg->msg.data = recv_msg->msg_data;
3941 * We chop off 12, not 11 bytes because the checksum
3942 * at the end also needs to be removed.
3944 recv_msg->msg.data_len = msg->rsp_size - 12;
3945 memcpy(recv_msg->msg_data, &msg->rsp[11],
3946 msg->rsp_size - 12);
3947 if (deliver_response(intf, recv_msg))
3948 ipmi_inc_stat(intf, unhandled_commands);
3950 ipmi_inc_stat(intf, handled_commands);
3958 * This routine will handle "Get Message" command responses with
3959 * channels that use an OEM Medium. The message format belongs to
3960 * the OEM. See IPMI 2.0 specification, Chapter 6 and
3961 * Chapter 22, sections 22.6 and 22.24 for more details.
3963 static int handle_oem_get_msg_cmd(struct ipmi_smi *intf,
3964 struct ipmi_smi_msg *msg)
3966 struct cmd_rcvr *rcvr;
3968 unsigned char netfn;
3971 struct ipmi_user *user = NULL;
3972 struct ipmi_system_interface_addr *smi_addr;
3973 struct ipmi_recv_msg *recv_msg;
3976 * We expect the OEM SW to perform error checking
3977 * so we just do some basic sanity checks
3979 if (msg->rsp_size < 4) {
3980 /* Message not big enough, just ignore it. */
3981 ipmi_inc_stat(intf, invalid_commands);
3985 if (msg->rsp[2] != 0) {
3986 /* An error getting the response, just ignore it. */
3991 * This is an OEM Message so the OEM needs to know how
3992 * handle the message. We do no interpretation.
3994 netfn = msg->rsp[0] >> 2;
3996 chan = msg->rsp[3] & 0xf;
3999 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
4002 kref_get(&user->refcount);
4008 /* We didn't find a user, just give up. */
4009 ipmi_inc_stat(intf, unhandled_commands);
4012 * Don't do anything with these messages, just allow
4018 recv_msg = ipmi_alloc_recv_msg();
4021 * We couldn't allocate memory for the
4022 * message, so requeue it for handling
4026 kref_put(&user->refcount, free_user);
4029 * OEM Messages are expected to be delivered via
4030 * the system interface to SMS software. We might
4031 * need to visit this again depending on OEM
4034 smi_addr = ((struct ipmi_system_interface_addr *)
4036 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
4037 smi_addr->channel = IPMI_BMC_CHANNEL;
4038 smi_addr->lun = msg->rsp[0] & 3;
4040 recv_msg->user = user;
4041 recv_msg->user_msg_data = NULL;
4042 recv_msg->recv_type = IPMI_OEM_RECV_TYPE;
4043 recv_msg->msg.netfn = msg->rsp[0] >> 2;
4044 recv_msg->msg.cmd = msg->rsp[1];
4045 recv_msg->msg.data = recv_msg->msg_data;
4048 * The message starts at byte 4 which follows the
4049 * the Channel Byte in the "GET MESSAGE" command
4051 recv_msg->msg.data_len = msg->rsp_size - 4;
4052 memcpy(recv_msg->msg_data, &msg->rsp[4],
4054 if (deliver_response(intf, recv_msg))
4055 ipmi_inc_stat(intf, unhandled_commands);
4057 ipmi_inc_stat(intf, handled_commands);
4064 static void copy_event_into_recv_msg(struct ipmi_recv_msg *recv_msg,
4065 struct ipmi_smi_msg *msg)
4067 struct ipmi_system_interface_addr *smi_addr;
4069 recv_msg->msgid = 0;
4070 smi_addr = (struct ipmi_system_interface_addr *) &recv_msg->addr;
4071 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
4072 smi_addr->channel = IPMI_BMC_CHANNEL;
4073 smi_addr->lun = msg->rsp[0] & 3;
4074 recv_msg->recv_type = IPMI_ASYNC_EVENT_RECV_TYPE;
4075 recv_msg->msg.netfn = msg->rsp[0] >> 2;
4076 recv_msg->msg.cmd = msg->rsp[1];
4077 memcpy(recv_msg->msg_data, &msg->rsp[3], msg->rsp_size - 3);
4078 recv_msg->msg.data = recv_msg->msg_data;
4079 recv_msg->msg.data_len = msg->rsp_size - 3;
4082 static int handle_read_event_rsp(struct ipmi_smi *intf,
4083 struct ipmi_smi_msg *msg)
4085 struct ipmi_recv_msg *recv_msg, *recv_msg2;
4086 struct list_head msgs;
4087 struct ipmi_user *user;
4088 int rv = 0, deliver_count = 0, index;
4089 unsigned long flags;
4091 if (msg->rsp_size < 19) {
4092 /* Message is too small to be an IPMB event. */
4093 ipmi_inc_stat(intf, invalid_events);
4097 if (msg->rsp[2] != 0) {
4098 /* An error getting the event, just ignore it. */
4102 INIT_LIST_HEAD(&msgs);
4104 spin_lock_irqsave(&intf->events_lock, flags);
4106 ipmi_inc_stat(intf, events);
4109 * Allocate and fill in one message for every user that is
4112 index = srcu_read_lock(&intf->users_srcu);
4113 list_for_each_entry_rcu(user, &intf->users, link) {
4114 if (!user->gets_events)
4117 recv_msg = ipmi_alloc_recv_msg();
4120 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs,
4122 list_del(&recv_msg->link);
4123 ipmi_free_recv_msg(recv_msg);
4126 * We couldn't allocate memory for the
4127 * message, so requeue it for handling
4136 copy_event_into_recv_msg(recv_msg, msg);
4137 recv_msg->user = user;
4138 kref_get(&user->refcount);
4139 list_add_tail(&recv_msg->link, &msgs);
4141 srcu_read_unlock(&intf->users_srcu, index);
4143 if (deliver_count) {
4144 /* Now deliver all the messages. */
4145 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs, link) {
4146 list_del(&recv_msg->link);
4147 deliver_local_response(intf, recv_msg);
4149 } else if (intf->waiting_events_count < MAX_EVENTS_IN_QUEUE) {
4151 * No one to receive the message, put it in queue if there's
4152 * not already too many things in the queue.
4154 recv_msg = ipmi_alloc_recv_msg();
4157 * We couldn't allocate memory for the
4158 * message, so requeue it for handling
4165 copy_event_into_recv_msg(recv_msg, msg);
4166 list_add_tail(&recv_msg->link, &intf->waiting_events);
4167 intf->waiting_events_count++;
4168 } else if (!intf->event_msg_printed) {
4170 * There's too many things in the queue, discard this
4173 dev_warn(intf->si_dev,
4174 "Event queue full, discarding incoming events\n");
4175 intf->event_msg_printed = 1;
4179 spin_unlock_irqrestore(&intf->events_lock, flags);
4184 static int handle_bmc_rsp(struct ipmi_smi *intf,
4185 struct ipmi_smi_msg *msg)
4187 struct ipmi_recv_msg *recv_msg;
4188 struct ipmi_system_interface_addr *smi_addr;
4190 recv_msg = (struct ipmi_recv_msg *) msg->user_data;
4191 if (recv_msg == NULL) {
4192 dev_warn(intf->si_dev,
4193 "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");
4197 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
4198 recv_msg->msgid = msg->msgid;
4199 smi_addr = ((struct ipmi_system_interface_addr *)
4201 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
4202 smi_addr->channel = IPMI_BMC_CHANNEL;
4203 smi_addr->lun = msg->rsp[0] & 3;
4204 recv_msg->msg.netfn = msg->rsp[0] >> 2;
4205 recv_msg->msg.cmd = msg->rsp[1];
4206 memcpy(recv_msg->msg_data, &msg->rsp[2], msg->rsp_size - 2);
4207 recv_msg->msg.data = recv_msg->msg_data;
4208 recv_msg->msg.data_len = msg->rsp_size - 2;
4209 deliver_local_response(intf, recv_msg);
4215 * Handle a received message. Return 1 if the message should be requeued,
4216 * 0 if the message should be freed, or -1 if the message should not
4217 * be freed or requeued.
4219 static int handle_one_recv_msg(struct ipmi_smi *intf,
4220 struct ipmi_smi_msg *msg)
4225 pr_debug("Recv: %*ph\n", msg->rsp_size, msg->rsp);
4227 if ((msg->data_size >= 2)
4228 && (msg->data[0] == (IPMI_NETFN_APP_REQUEST << 2))
4229 && (msg->data[1] == IPMI_SEND_MSG_CMD)
4230 && (msg->user_data == NULL)) {
4232 if (intf->in_shutdown)
4236 * This is the local response to a command send, start
4237 * the timer for these. The user_data will not be
4238 * NULL if this is a response send, and we will let
4239 * response sends just go through.
4243 * Check for errors, if we get certain errors (ones
4244 * that mean basically we can try again later), we
4245 * ignore them and start the timer. Otherwise we
4246 * report the error immediately.
4248 if ((msg->rsp_size >= 3) && (msg->rsp[2] != 0)
4249 && (msg->rsp[2] != IPMI_NODE_BUSY_ERR)
4250 && (msg->rsp[2] != IPMI_LOST_ARBITRATION_ERR)
4251 && (msg->rsp[2] != IPMI_BUS_ERR)
4252 && (msg->rsp[2] != IPMI_NAK_ON_WRITE_ERR)) {
4253 int ch = msg->rsp[3] & 0xf;
4254 struct ipmi_channel *chans;
4256 /* Got an error sending the message, handle it. */
4258 chans = READ_ONCE(intf->channel_list)->c;
4259 if ((chans[ch].medium == IPMI_CHANNEL_MEDIUM_8023LAN)
4260 || (chans[ch].medium == IPMI_CHANNEL_MEDIUM_ASYNC))
4261 ipmi_inc_stat(intf, sent_lan_command_errs);
4263 ipmi_inc_stat(intf, sent_ipmb_command_errs);
4264 intf_err_seq(intf, msg->msgid, msg->rsp[2]);
4266 /* The message was sent, start the timer. */
4267 intf_start_seq_timer(intf, msg->msgid);
4272 } else if (msg->rsp_size < 2) {
4273 /* Message is too small to be correct. */
4274 dev_warn(intf->si_dev,
4275 "BMC returned too small a message for netfn %x cmd %x, got %d bytes\n",
4276 (msg->data[0] >> 2) | 1, msg->data[1], msg->rsp_size);
4278 /* Generate an error response for the message. */
4279 msg->rsp[0] = msg->data[0] | (1 << 2);
4280 msg->rsp[1] = msg->data[1];
4281 msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
4283 } else if (((msg->rsp[0] >> 2) != ((msg->data[0] >> 2) | 1))
4284 || (msg->rsp[1] != msg->data[1])) {
4286 * The NetFN and Command in the response is not even
4287 * marginally correct.
4289 dev_warn(intf->si_dev,
4290 "BMC returned incorrect response, expected netfn %x cmd %x, got netfn %x cmd %x\n",
4291 (msg->data[0] >> 2) | 1, msg->data[1],
4292 msg->rsp[0] >> 2, msg->rsp[1]);
4294 /* Generate an error response for the message. */
4295 msg->rsp[0] = msg->data[0] | (1 << 2);
4296 msg->rsp[1] = msg->data[1];
4297 msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
4301 if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
4302 && (msg->rsp[1] == IPMI_SEND_MSG_CMD)
4303 && (msg->user_data != NULL)) {
4305 * It's a response to a response we sent. For this we
4306 * deliver a send message response to the user.
4308 struct ipmi_recv_msg *recv_msg = msg->user_data;
4311 if (msg->rsp_size < 2)
4312 /* Message is too small to be correct. */
4315 chan = msg->data[2] & 0x0f;
4316 if (chan >= IPMI_MAX_CHANNELS)
4317 /* Invalid channel number */
4323 recv_msg->recv_type = IPMI_RESPONSE_RESPONSE_TYPE;
4324 recv_msg->msg.data = recv_msg->msg_data;
4325 recv_msg->msg.data_len = 1;
4326 recv_msg->msg_data[0] = msg->rsp[2];
4327 deliver_local_response(intf, recv_msg);
4328 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
4329 && (msg->rsp[1] == IPMI_GET_MSG_CMD)) {
4330 struct ipmi_channel *chans;
4332 /* It's from the receive queue. */
4333 chan = msg->rsp[3] & 0xf;
4334 if (chan >= IPMI_MAX_CHANNELS) {
4335 /* Invalid channel number */
4341 * We need to make sure the channels have been initialized.
4342 * The channel_handler routine will set the "curr_channel"
4343 * equal to or greater than IPMI_MAX_CHANNELS when all the
4344 * channels for this interface have been initialized.
4346 if (!intf->channels_ready) {
4347 requeue = 0; /* Throw the message away */
4351 chans = READ_ONCE(intf->channel_list)->c;
4353 switch (chans[chan].medium) {
4354 case IPMI_CHANNEL_MEDIUM_IPMB:
4355 if (msg->rsp[4] & 0x04) {
4357 * It's a response, so find the
4358 * requesting message and send it up.
4360 requeue = handle_ipmb_get_msg_rsp(intf, msg);
4363 * It's a command to the SMS from some other
4364 * entity. Handle that.
4366 requeue = handle_ipmb_get_msg_cmd(intf, msg);
4370 case IPMI_CHANNEL_MEDIUM_8023LAN:
4371 case IPMI_CHANNEL_MEDIUM_ASYNC:
4372 if (msg->rsp[6] & 0x04) {
4374 * It's a response, so find the
4375 * requesting message and send it up.
4377 requeue = handle_lan_get_msg_rsp(intf, msg);
4380 * It's a command to the SMS from some other
4381 * entity. Handle that.
4383 requeue = handle_lan_get_msg_cmd(intf, msg);
4388 /* Check for OEM Channels. Clients had better
4389 register for these commands. */
4390 if ((chans[chan].medium >= IPMI_CHANNEL_MEDIUM_OEM_MIN)
4391 && (chans[chan].medium
4392 <= IPMI_CHANNEL_MEDIUM_OEM_MAX)) {
4393 requeue = handle_oem_get_msg_cmd(intf, msg);
4396 * We don't handle the channel type, so just
4403 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
4404 && (msg->rsp[1] == IPMI_READ_EVENT_MSG_BUFFER_CMD)) {
4405 /* It's an asynchronous event. */
4406 requeue = handle_read_event_rsp(intf, msg);
4408 /* It's a response from the local BMC. */
4409 requeue = handle_bmc_rsp(intf, msg);
4417 * If there are messages in the queue or pretimeouts, handle them.
4419 static void handle_new_recv_msgs(struct ipmi_smi *intf)
4421 struct ipmi_smi_msg *smi_msg;
4422 unsigned long flags = 0;
4424 int run_to_completion = intf->run_to_completion;
4426 /* See if any waiting messages need to be processed. */
4427 if (!run_to_completion)
4428 spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags);
4429 while (!list_empty(&intf->waiting_rcv_msgs)) {
4430 smi_msg = list_entry(intf->waiting_rcv_msgs.next,
4431 struct ipmi_smi_msg, link);
4432 list_del(&smi_msg->link);
4433 if (!run_to_completion)
4434 spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock,
4436 rv = handle_one_recv_msg(intf, smi_msg);
4437 if (!run_to_completion)
4438 spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags);
4441 * To preserve message order, quit if we
4442 * can't handle a message. Add the message
4443 * back at the head, this is safe because this
4444 * tasklet is the only thing that pulls the
4447 list_add(&smi_msg->link, &intf->waiting_rcv_msgs);
4451 /* Message handled */
4452 ipmi_free_smi_msg(smi_msg);
4453 /* If rv < 0, fatal error, del but don't free. */
4456 if (!run_to_completion)
4457 spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock, flags);
4460 * If the pretimout count is non-zero, decrement one from it and
4461 * deliver pretimeouts to all the users.
4463 if (atomic_add_unless(&intf->watchdog_pretimeouts_to_deliver, -1, 0)) {
4464 struct ipmi_user *user;
4467 index = srcu_read_lock(&intf->users_srcu);
4468 list_for_each_entry_rcu(user, &intf->users, link) {
4469 if (user->handler->ipmi_watchdog_pretimeout)
4470 user->handler->ipmi_watchdog_pretimeout(
4471 user->handler_data);
4473 srcu_read_unlock(&intf->users_srcu, index);
4477 static void smi_recv_tasklet(struct tasklet_struct *t)
4479 unsigned long flags = 0; /* keep us warning-free. */
4480 struct ipmi_smi *intf = from_tasklet(intf, t, recv_tasklet);
4481 int run_to_completion = intf->run_to_completion;
4482 struct ipmi_smi_msg *newmsg = NULL;
4485 * Start the next message if available.
4487 * Do this here, not in the actual receiver, because we may deadlock
4488 * because the lower layer is allowed to hold locks while calling
4494 if (!run_to_completion)
4495 spin_lock_irqsave(&intf->xmit_msgs_lock, flags);
4496 if (intf->curr_msg == NULL && !intf->in_shutdown) {
4497 struct list_head *entry = NULL;
4499 /* Pick the high priority queue first. */
4500 if (!list_empty(&intf->hp_xmit_msgs))
4501 entry = intf->hp_xmit_msgs.next;
4502 else if (!list_empty(&intf->xmit_msgs))
4503 entry = intf->xmit_msgs.next;
4507 newmsg = list_entry(entry, struct ipmi_smi_msg, link);
4508 intf->curr_msg = newmsg;
4512 if (!run_to_completion)
4513 spin_unlock_irqrestore(&intf->xmit_msgs_lock, flags);
4515 intf->handlers->sender(intf->send_info, newmsg);
4519 handle_new_recv_msgs(intf);
4522 /* Handle a new message from the lower layer. */
4523 void ipmi_smi_msg_received(struct ipmi_smi *intf,
4524 struct ipmi_smi_msg *msg)
4526 unsigned long flags = 0; /* keep us warning-free. */
4527 int run_to_completion = intf->run_to_completion;
4530 * To preserve message order, we keep a queue and deliver from
4533 if (!run_to_completion)
4534 spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags);
4535 list_add_tail(&msg->link, &intf->waiting_rcv_msgs);
4536 if (!run_to_completion)
4537 spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock,
4540 if (!run_to_completion)
4541 spin_lock_irqsave(&intf->xmit_msgs_lock, flags);
4543 * We can get an asynchronous event or receive message in addition
4544 * to commands we send.
4546 if (msg == intf->curr_msg)
4547 intf->curr_msg = NULL;
4548 if (!run_to_completion)
4549 spin_unlock_irqrestore(&intf->xmit_msgs_lock, flags);
4551 if (run_to_completion)
4552 smi_recv_tasklet(&intf->recv_tasklet);
4554 tasklet_schedule(&intf->recv_tasklet);
4556 EXPORT_SYMBOL(ipmi_smi_msg_received);
4558 void ipmi_smi_watchdog_pretimeout(struct ipmi_smi *intf)
4560 if (intf->in_shutdown)
4563 atomic_set(&intf->watchdog_pretimeouts_to_deliver, 1);
4564 tasklet_schedule(&intf->recv_tasklet);
4566 EXPORT_SYMBOL(ipmi_smi_watchdog_pretimeout);
4568 static struct ipmi_smi_msg *
4569 smi_from_recv_msg(struct ipmi_smi *intf, struct ipmi_recv_msg *recv_msg,
4570 unsigned char seq, long seqid)
4572 struct ipmi_smi_msg *smi_msg = ipmi_alloc_smi_msg();
4575 * If we can't allocate the message, then just return, we
4576 * get 4 retries, so this should be ok.
4580 memcpy(smi_msg->data, recv_msg->msg.data, recv_msg->msg.data_len);
4581 smi_msg->data_size = recv_msg->msg.data_len;
4582 smi_msg->msgid = STORE_SEQ_IN_MSGID(seq, seqid);
4584 pr_debug("Resend: %*ph\n", smi_msg->data_size, smi_msg->data);
4589 static void check_msg_timeout(struct ipmi_smi *intf, struct seq_table *ent,
4590 struct list_head *timeouts,
4591 unsigned long timeout_period,
4592 int slot, unsigned long *flags,
4595 struct ipmi_recv_msg *msg;
4597 if (intf->in_shutdown)
4603 if (timeout_period < ent->timeout) {
4604 ent->timeout -= timeout_period;
4609 if (ent->retries_left == 0) {
4610 /* The message has used all its retries. */
4612 smi_remove_watch(intf, IPMI_WATCH_MASK_CHECK_MESSAGES);
4613 msg = ent->recv_msg;
4614 list_add_tail(&msg->link, timeouts);
4616 ipmi_inc_stat(intf, timed_out_ipmb_broadcasts);
4617 else if (is_lan_addr(&ent->recv_msg->addr))
4618 ipmi_inc_stat(intf, timed_out_lan_commands);
4620 ipmi_inc_stat(intf, timed_out_ipmb_commands);
4622 struct ipmi_smi_msg *smi_msg;
4623 /* More retries, send again. */
4628 * Start with the max timer, set to normal timer after
4629 * the message is sent.
4631 ent->timeout = MAX_MSG_TIMEOUT;
4632 ent->retries_left--;
4633 smi_msg = smi_from_recv_msg(intf, ent->recv_msg, slot,
4636 if (is_lan_addr(&ent->recv_msg->addr))
4638 dropped_rexmit_lan_commands);
4641 dropped_rexmit_ipmb_commands);
4645 spin_unlock_irqrestore(&intf->seq_lock, *flags);
4648 * Send the new message. We send with a zero
4649 * priority. It timed out, I doubt time is that
4650 * critical now, and high priority messages are really
4651 * only for messages to the local MC, which don't get
4654 if (intf->handlers) {
4655 if (is_lan_addr(&ent->recv_msg->addr))
4657 retransmitted_lan_commands);
4660 retransmitted_ipmb_commands);
4662 smi_send(intf, intf->handlers, smi_msg, 0);
4664 ipmi_free_smi_msg(smi_msg);
4666 spin_lock_irqsave(&intf->seq_lock, *flags);
4670 static bool ipmi_timeout_handler(struct ipmi_smi *intf,
4671 unsigned long timeout_period)
4673 struct list_head timeouts;
4674 struct ipmi_recv_msg *msg, *msg2;
4675 unsigned long flags;
4677 bool need_timer = false;
4679 if (!intf->bmc_registered) {
4680 kref_get(&intf->refcount);
4681 if (!schedule_work(&intf->bmc_reg_work)) {
4682 kref_put(&intf->refcount, intf_free);
4688 * Go through the seq table and find any messages that
4689 * have timed out, putting them in the timeouts
4692 INIT_LIST_HEAD(&timeouts);
4693 spin_lock_irqsave(&intf->seq_lock, flags);
4694 if (intf->ipmb_maintenance_mode_timeout) {
4695 if (intf->ipmb_maintenance_mode_timeout <= timeout_period)
4696 intf->ipmb_maintenance_mode_timeout = 0;
4698 intf->ipmb_maintenance_mode_timeout -= timeout_period;
4700 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++)
4701 check_msg_timeout(intf, &intf->seq_table[i],
4702 &timeouts, timeout_period, i,
4703 &flags, &need_timer);
4704 spin_unlock_irqrestore(&intf->seq_lock, flags);
4706 list_for_each_entry_safe(msg, msg2, &timeouts, link)
4707 deliver_err_response(intf, msg, IPMI_TIMEOUT_COMPLETION_CODE);
4710 * Maintenance mode handling. Check the timeout
4711 * optimistically before we claim the lock. It may
4712 * mean a timeout gets missed occasionally, but that
4713 * only means the timeout gets extended by one period
4714 * in that case. No big deal, and it avoids the lock
4717 if (intf->auto_maintenance_timeout > 0) {
4718 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
4719 if (intf->auto_maintenance_timeout > 0) {
4720 intf->auto_maintenance_timeout
4722 if (!intf->maintenance_mode
4723 && (intf->auto_maintenance_timeout <= 0)) {
4724 intf->maintenance_mode_enable = false;
4725 maintenance_mode_update(intf);
4728 spin_unlock_irqrestore(&intf->maintenance_mode_lock,
4732 tasklet_schedule(&intf->recv_tasklet);
4737 static void ipmi_request_event(struct ipmi_smi *intf)
4739 /* No event requests when in maintenance mode. */
4740 if (intf->maintenance_mode_enable)
4743 if (!intf->in_shutdown)
4744 intf->handlers->request_events(intf->send_info);
4747 static struct timer_list ipmi_timer;
4749 static atomic_t stop_operation;
4751 static void ipmi_timeout(struct timer_list *unused)
4753 struct ipmi_smi *intf;
4754 bool need_timer = false;
4757 if (atomic_read(&stop_operation))
4760 index = srcu_read_lock(&ipmi_interfaces_srcu);
4761 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
4762 if (atomic_read(&intf->event_waiters)) {
4763 intf->ticks_to_req_ev--;
4764 if (intf->ticks_to_req_ev == 0) {
4765 ipmi_request_event(intf);
4766 intf->ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
4771 need_timer |= ipmi_timeout_handler(intf, IPMI_TIMEOUT_TIME);
4773 srcu_read_unlock(&ipmi_interfaces_srcu, index);
4776 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
4779 static void need_waiter(struct ipmi_smi *intf)
4781 /* Racy, but worst case we start the timer twice. */
4782 if (!timer_pending(&ipmi_timer))
4783 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
4786 static atomic_t smi_msg_inuse_count = ATOMIC_INIT(0);
4787 static atomic_t recv_msg_inuse_count = ATOMIC_INIT(0);
4789 static void free_smi_msg(struct ipmi_smi_msg *msg)
4791 atomic_dec(&smi_msg_inuse_count);
4795 struct ipmi_smi_msg *ipmi_alloc_smi_msg(void)
4797 struct ipmi_smi_msg *rv;
4798 rv = kmalloc(sizeof(struct ipmi_smi_msg), GFP_ATOMIC);
4800 rv->done = free_smi_msg;
4801 rv->user_data = NULL;
4802 atomic_inc(&smi_msg_inuse_count);
4806 EXPORT_SYMBOL(ipmi_alloc_smi_msg);
4808 static void free_recv_msg(struct ipmi_recv_msg *msg)
4810 atomic_dec(&recv_msg_inuse_count);
4814 static struct ipmi_recv_msg *ipmi_alloc_recv_msg(void)
4816 struct ipmi_recv_msg *rv;
4818 rv = kmalloc(sizeof(struct ipmi_recv_msg), GFP_ATOMIC);
4821 rv->done = free_recv_msg;
4822 atomic_inc(&recv_msg_inuse_count);
4827 void ipmi_free_recv_msg(struct ipmi_recv_msg *msg)
4830 kref_put(&msg->user->refcount, free_user);
4833 EXPORT_SYMBOL(ipmi_free_recv_msg);
4835 static atomic_t panic_done_count = ATOMIC_INIT(0);
4837 static void dummy_smi_done_handler(struct ipmi_smi_msg *msg)
4839 atomic_dec(&panic_done_count);
4842 static void dummy_recv_done_handler(struct ipmi_recv_msg *msg)
4844 atomic_dec(&panic_done_count);
4848 * Inside a panic, send a message and wait for a response.
4850 static void ipmi_panic_request_and_wait(struct ipmi_smi *intf,
4851 struct ipmi_addr *addr,
4852 struct kernel_ipmi_msg *msg)
4854 struct ipmi_smi_msg smi_msg;
4855 struct ipmi_recv_msg recv_msg;
4858 smi_msg.done = dummy_smi_done_handler;
4859 recv_msg.done = dummy_recv_done_handler;
4860 atomic_add(2, &panic_done_count);
4861 rv = i_ipmi_request(NULL,
4870 intf->addrinfo[0].address,
4871 intf->addrinfo[0].lun,
4872 0, 1); /* Don't retry, and don't wait. */
4874 atomic_sub(2, &panic_done_count);
4875 else if (intf->handlers->flush_messages)
4876 intf->handlers->flush_messages(intf->send_info);
4878 while (atomic_read(&panic_done_count) != 0)
4882 static void event_receiver_fetcher(struct ipmi_smi *intf,
4883 struct ipmi_recv_msg *msg)
4885 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
4886 && (msg->msg.netfn == IPMI_NETFN_SENSOR_EVENT_RESPONSE)
4887 && (msg->msg.cmd == IPMI_GET_EVENT_RECEIVER_CMD)
4888 && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) {
4889 /* A get event receiver command, save it. */
4890 intf->event_receiver = msg->msg.data[1];
4891 intf->event_receiver_lun = msg->msg.data[2] & 0x3;
4895 static void device_id_fetcher(struct ipmi_smi *intf, struct ipmi_recv_msg *msg)
4897 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
4898 && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
4899 && (msg->msg.cmd == IPMI_GET_DEVICE_ID_CMD)
4900 && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) {
4902 * A get device id command, save if we are an event
4903 * receiver or generator.
4905 intf->local_sel_device = (msg->msg.data[6] >> 2) & 1;
4906 intf->local_event_generator = (msg->msg.data[6] >> 5) & 1;
4910 static void send_panic_events(struct ipmi_smi *intf, char *str)
4912 struct kernel_ipmi_msg msg;
4913 unsigned char data[16];
4914 struct ipmi_system_interface_addr *si;
4915 struct ipmi_addr addr;
4917 struct ipmi_ipmb_addr *ipmb;
4920 if (ipmi_send_panic_event == IPMI_SEND_PANIC_EVENT_NONE)
4923 si = (struct ipmi_system_interface_addr *) &addr;
4924 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
4925 si->channel = IPMI_BMC_CHANNEL;
4928 /* Fill in an event telling that we have failed. */
4929 msg.netfn = 0x04; /* Sensor or Event. */
4930 msg.cmd = 2; /* Platform event command. */
4933 data[0] = 0x41; /* Kernel generator ID, IPMI table 5-4 */
4934 data[1] = 0x03; /* This is for IPMI 1.0. */
4935 data[2] = 0x20; /* OS Critical Stop, IPMI table 36-3 */
4936 data[4] = 0x6f; /* Sensor specific, IPMI table 36-1 */
4937 data[5] = 0xa1; /* Runtime stop OEM bytes 2 & 3. */
4940 * Put a few breadcrumbs in. Hopefully later we can add more things
4941 * to make the panic events more useful.
4949 /* Send the event announcing the panic. */
4950 ipmi_panic_request_and_wait(intf, &addr, &msg);
4953 * On every interface, dump a bunch of OEM event holding the
4956 if (ipmi_send_panic_event != IPMI_SEND_PANIC_EVENT_STRING || !str)
4960 * intf_num is used as an marker to tell if the
4961 * interface is valid. Thus we need a read barrier to
4962 * make sure data fetched before checking intf_num
4968 * First job here is to figure out where to send the
4969 * OEM events. There's no way in IPMI to send OEM
4970 * events using an event send command, so we have to
4971 * find the SEL to put them in and stick them in
4975 /* Get capabilities from the get device id. */
4976 intf->local_sel_device = 0;
4977 intf->local_event_generator = 0;
4978 intf->event_receiver = 0;
4980 /* Request the device info from the local MC. */
4981 msg.netfn = IPMI_NETFN_APP_REQUEST;
4982 msg.cmd = IPMI_GET_DEVICE_ID_CMD;
4985 intf->null_user_handler = device_id_fetcher;
4986 ipmi_panic_request_and_wait(intf, &addr, &msg);
4988 if (intf->local_event_generator) {
4989 /* Request the event receiver from the local MC. */
4990 msg.netfn = IPMI_NETFN_SENSOR_EVENT_REQUEST;
4991 msg.cmd = IPMI_GET_EVENT_RECEIVER_CMD;
4994 intf->null_user_handler = event_receiver_fetcher;
4995 ipmi_panic_request_and_wait(intf, &addr, &msg);
4997 intf->null_user_handler = NULL;
5000 * Validate the event receiver. The low bit must not
5001 * be 1 (it must be a valid IPMB address), it cannot
5002 * be zero, and it must not be my address.
5004 if (((intf->event_receiver & 1) == 0)
5005 && (intf->event_receiver != 0)
5006 && (intf->event_receiver != intf->addrinfo[0].address)) {
5008 * The event receiver is valid, send an IPMB
5011 ipmb = (struct ipmi_ipmb_addr *) &addr;
5012 ipmb->addr_type = IPMI_IPMB_ADDR_TYPE;
5013 ipmb->channel = 0; /* FIXME - is this right? */
5014 ipmb->lun = intf->event_receiver_lun;
5015 ipmb->slave_addr = intf->event_receiver;
5016 } else if (intf->local_sel_device) {
5018 * The event receiver was not valid (or was
5019 * me), but I am an SEL device, just dump it
5022 si = (struct ipmi_system_interface_addr *) &addr;
5023 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
5024 si->channel = IPMI_BMC_CHANNEL;
5027 return; /* No where to send the event. */
5029 msg.netfn = IPMI_NETFN_STORAGE_REQUEST; /* Storage. */
5030 msg.cmd = IPMI_ADD_SEL_ENTRY_CMD;
5036 int size = strlen(p);
5042 data[2] = 0xf0; /* OEM event without timestamp. */
5043 data[3] = intf->addrinfo[0].address;
5044 data[4] = j++; /* sequence # */
5046 * Always give 11 bytes, so strncpy will fill
5047 * it with zeroes for me.
5049 strncpy(data+5, p, 11);
5052 ipmi_panic_request_and_wait(intf, &addr, &msg);
5056 static int has_panicked;
5058 static int panic_event(struct notifier_block *this,
5059 unsigned long event,
5062 struct ipmi_smi *intf;
5063 struct ipmi_user *user;
5069 /* For every registered interface, set it to run to completion. */
5070 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
5071 if (!intf->handlers || intf->intf_num == -1)
5072 /* Interface is not ready. */
5075 if (!intf->handlers->poll)
5079 * If we were interrupted while locking xmit_msgs_lock or
5080 * waiting_rcv_msgs_lock, the corresponding list may be
5081 * corrupted. In this case, drop items on the list for
5084 if (!spin_trylock(&intf->xmit_msgs_lock)) {
5085 INIT_LIST_HEAD(&intf->xmit_msgs);
5086 INIT_LIST_HEAD(&intf->hp_xmit_msgs);
5088 spin_unlock(&intf->xmit_msgs_lock);
5090 if (!spin_trylock(&intf->waiting_rcv_msgs_lock))
5091 INIT_LIST_HEAD(&intf->waiting_rcv_msgs);
5093 spin_unlock(&intf->waiting_rcv_msgs_lock);
5095 intf->run_to_completion = 1;
5096 if (intf->handlers->set_run_to_completion)
5097 intf->handlers->set_run_to_completion(intf->send_info,
5100 list_for_each_entry_rcu(user, &intf->users, link) {
5101 if (user->handler->ipmi_panic_handler)
5102 user->handler->ipmi_panic_handler(
5103 user->handler_data);
5106 send_panic_events(intf, ptr);
5112 /* Must be called with ipmi_interfaces_mutex held. */
5113 static int ipmi_register_driver(void)
5120 rv = driver_register(&ipmidriver.driver);
5122 pr_err("Could not register IPMI driver\n");
5124 drvregistered = true;
5128 static struct notifier_block panic_block = {
5129 .notifier_call = panic_event,
5131 .priority = 200 /* priority: INT_MAX >= x >= 0 */
5134 static int ipmi_init_msghandler(void)
5138 mutex_lock(&ipmi_interfaces_mutex);
5139 rv = ipmi_register_driver();
5145 init_srcu_struct(&ipmi_interfaces_srcu);
5147 timer_setup(&ipmi_timer, ipmi_timeout, 0);
5148 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
5150 atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
5155 mutex_unlock(&ipmi_interfaces_mutex);
5159 static int __init ipmi_init_msghandler_mod(void)
5163 pr_info("version " IPMI_DRIVER_VERSION "\n");
5165 mutex_lock(&ipmi_interfaces_mutex);
5166 rv = ipmi_register_driver();
5167 mutex_unlock(&ipmi_interfaces_mutex);
5172 static void __exit cleanup_ipmi(void)
5177 atomic_notifier_chain_unregister(&panic_notifier_list,
5181 * This can't be called if any interfaces exist, so no worry
5182 * about shutting down the interfaces.
5186 * Tell the timer to stop, then wait for it to stop. This
5187 * avoids problems with race conditions removing the timer
5190 atomic_set(&stop_operation, 1);
5191 del_timer_sync(&ipmi_timer);
5193 initialized = false;
5195 /* Check for buffer leaks. */
5196 count = atomic_read(&smi_msg_inuse_count);
5198 pr_warn("SMI message count %d at exit\n", count);
5199 count = atomic_read(&recv_msg_inuse_count);
5201 pr_warn("recv message count %d at exit\n", count);
5203 cleanup_srcu_struct(&ipmi_interfaces_srcu);
5206 driver_unregister(&ipmidriver.driver);
5208 module_exit(cleanup_ipmi);
5210 module_init(ipmi_init_msghandler_mod);
5211 MODULE_LICENSE("GPL");
5212 MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
5213 MODULE_DESCRIPTION("Incoming and outgoing message routing for an IPMI interface.");
5214 MODULE_VERSION(IPMI_DRIVER_VERSION);
5215 MODULE_SOFTDEP("post: ipmi_devintf");