1 // SPDX-License-Identifier: GPL-2.0+
5 * Incoming and outgoing message routing for an IPMI interface.
7 * Author: MontaVista Software, Inc.
8 * Corey Minyard <minyard@mvista.com>
11 * Copyright 2002 MontaVista Software Inc.
14 #define pr_fmt(fmt) "%s" fmt, "IPMI message handler: "
15 #define dev_fmt pr_fmt
17 #include <linux/module.h>
18 #include <linux/errno.h>
19 #include <linux/poll.h>
20 #include <linux/sched.h>
21 #include <linux/seq_file.h>
22 #include <linux/spinlock.h>
23 #include <linux/mutex.h>
24 #include <linux/slab.h>
25 #include <linux/ipmi.h>
26 #include <linux/ipmi_smi.h>
27 #include <linux/notifier.h>
28 #include <linux/init.h>
29 #include <linux/proc_fs.h>
30 #include <linux/rcupdate.h>
31 #include <linux/interrupt.h>
32 #include <linux/moduleparam.h>
33 #include <linux/workqueue.h>
34 #include <linux/uuid.h>
35 #include <linux/nospec.h>
37 #define IPMI_DRIVER_VERSION "39.2"
39 static struct ipmi_recv_msg *ipmi_alloc_recv_msg(void);
40 static int ipmi_init_msghandler(void);
41 static void smi_recv_tasklet(unsigned long);
42 static void handle_new_recv_msgs(struct ipmi_smi *intf);
43 static void need_waiter(struct ipmi_smi *intf);
44 static int handle_one_recv_msg(struct ipmi_smi *intf,
45 struct ipmi_smi_msg *msg);
48 static void ipmi_debug_msg(const char *title, unsigned char *data,
54 pos = snprintf(buf, sizeof(buf), "%s: ", title);
55 for (i = 0; i < len; i++)
56 pos += snprintf(buf + pos, sizeof(buf) - pos,
58 pr_debug("%s\n", buf);
61 static void ipmi_debug_msg(const char *title, unsigned char *data,
66 static bool initialized;
67 static bool drvregistered;
69 enum ipmi_panic_event_op {
70 IPMI_SEND_PANIC_EVENT_NONE,
71 IPMI_SEND_PANIC_EVENT,
72 IPMI_SEND_PANIC_EVENT_STRING
74 #ifdef CONFIG_IPMI_PANIC_STRING
75 #define IPMI_PANIC_DEFAULT IPMI_SEND_PANIC_EVENT_STRING
76 #elif defined(CONFIG_IPMI_PANIC_EVENT)
77 #define IPMI_PANIC_DEFAULT IPMI_SEND_PANIC_EVENT
79 #define IPMI_PANIC_DEFAULT IPMI_SEND_PANIC_EVENT_NONE
81 static enum ipmi_panic_event_op ipmi_send_panic_event = IPMI_PANIC_DEFAULT;
83 static int panic_op_write_handler(const char *val,
84 const struct kernel_param *kp)
89 strncpy(valcp, val, 15);
94 if (strcmp(s, "none") == 0)
95 ipmi_send_panic_event = IPMI_SEND_PANIC_EVENT_NONE;
96 else if (strcmp(s, "event") == 0)
97 ipmi_send_panic_event = IPMI_SEND_PANIC_EVENT;
98 else if (strcmp(s, "string") == 0)
99 ipmi_send_panic_event = IPMI_SEND_PANIC_EVENT_STRING;
106 static int panic_op_read_handler(char *buffer, const struct kernel_param *kp)
108 switch (ipmi_send_panic_event) {
109 case IPMI_SEND_PANIC_EVENT_NONE:
110 strcpy(buffer, "none");
113 case IPMI_SEND_PANIC_EVENT:
114 strcpy(buffer, "event");
117 case IPMI_SEND_PANIC_EVENT_STRING:
118 strcpy(buffer, "string");
122 strcpy(buffer, "???");
126 return strlen(buffer);
129 static const struct kernel_param_ops panic_op_ops = {
130 .set = panic_op_write_handler,
131 .get = panic_op_read_handler
133 module_param_cb(panic_op, &panic_op_ops, NULL, 0600);
134 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.");
137 #define MAX_EVENTS_IN_QUEUE 25
139 /* Remain in auto-maintenance mode for this amount of time (in ms). */
140 static unsigned long maintenance_mode_timeout_ms = 30000;
141 module_param(maintenance_mode_timeout_ms, ulong, 0644);
142 MODULE_PARM_DESC(maintenance_mode_timeout_ms,
143 "The time (milliseconds) after the last maintenance message that the connection stays in maintenance mode.");
146 * Don't let a message sit in a queue forever, always time it with at lest
147 * the max message timer. This is in milliseconds.
149 #define MAX_MSG_TIMEOUT 60000
152 * Timeout times below are in milliseconds, and are done off a 1
153 * second timer. So setting the value to 1000 would mean anything
154 * between 0 and 1000ms. So really the only reasonable minimum
155 * setting it 2000ms, which is between 1 and 2 seconds.
158 /* The default timeout for message retries. */
159 static unsigned long default_retry_ms = 2000;
160 module_param(default_retry_ms, ulong, 0644);
161 MODULE_PARM_DESC(default_retry_ms,
162 "The time (milliseconds) between retry sends");
164 /* The default timeout for maintenance mode message retries. */
165 static unsigned long default_maintenance_retry_ms = 3000;
166 module_param(default_maintenance_retry_ms, ulong, 0644);
167 MODULE_PARM_DESC(default_maintenance_retry_ms,
168 "The time (milliseconds) between retry sends in maintenance mode");
170 /* The default maximum number of retries */
171 static unsigned int default_max_retries = 4;
172 module_param(default_max_retries, uint, 0644);
173 MODULE_PARM_DESC(default_max_retries,
174 "The time (milliseconds) between retry sends in maintenance mode");
176 /* Call every ~1000 ms. */
177 #define IPMI_TIMEOUT_TIME 1000
179 /* How many jiffies does it take to get to the timeout time. */
180 #define IPMI_TIMEOUT_JIFFIES ((IPMI_TIMEOUT_TIME * HZ) / 1000)
183 * Request events from the queue every second (this is the number of
184 * IPMI_TIMEOUT_TIMES between event requests). Hopefully, in the
185 * future, IPMI will add a way to know immediately if an event is in
186 * the queue and this silliness can go away.
188 #define IPMI_REQUEST_EV_TIME (1000 / (IPMI_TIMEOUT_TIME))
190 /* How long should we cache dynamic device IDs? */
191 #define IPMI_DYN_DEV_ID_EXPIRY (10 * HZ)
194 * The main "user" data structure.
197 struct list_head link;
200 * Set to NULL when the user is destroyed, a pointer to myself
201 * so srcu_dereference can be used on it.
203 struct ipmi_user *self;
204 struct srcu_struct release_barrier;
206 struct kref refcount;
208 /* The upper layer that handles receive messages. */
209 const struct ipmi_user_hndl *handler;
212 /* The interface this user is bound to. */
213 struct ipmi_smi *intf;
215 /* Does this interface receive IPMI events? */
218 /* Free must run in process context for RCU cleanup. */
219 struct work_struct remove_work;
222 static struct ipmi_user *acquire_ipmi_user(struct ipmi_user *user, int *index)
223 __acquires(user->release_barrier)
225 struct ipmi_user *ruser;
227 *index = srcu_read_lock(&user->release_barrier);
228 ruser = srcu_dereference(user->self, &user->release_barrier);
230 srcu_read_unlock(&user->release_barrier, *index);
234 static void release_ipmi_user(struct ipmi_user *user, int index)
236 srcu_read_unlock(&user->release_barrier, index);
240 struct list_head link;
242 struct ipmi_user *user;
248 * This is used to form a linked lised during mass deletion.
249 * Since this is in an RCU list, we cannot use the link above
250 * or change any data until the RCU period completes. So we
251 * use this next variable during mass deletion so we can have
252 * a list and don't have to wait and restart the search on
253 * every individual deletion of a command.
255 struct cmd_rcvr *next;
259 unsigned int inuse : 1;
260 unsigned int broadcast : 1;
262 unsigned long timeout;
263 unsigned long orig_timeout;
264 unsigned int retries_left;
267 * To verify on an incoming send message response that this is
268 * the message that the response is for, we keep a sequence id
269 * and increment it every time we send a message.
274 * This is held so we can properly respond to the message on a
275 * timeout, and it is used to hold the temporary data for
276 * retransmission, too.
278 struct ipmi_recv_msg *recv_msg;
282 * Store the information in a msgid (long) to allow us to find a
283 * sequence table entry from the msgid.
285 #define STORE_SEQ_IN_MSGID(seq, seqid) \
286 ((((seq) & 0x3f) << 26) | ((seqid) & 0x3ffffff))
288 #define GET_SEQ_FROM_MSGID(msgid, seq, seqid) \
290 seq = (((msgid) >> 26) & 0x3f); \
291 seqid = ((msgid) & 0x3ffffff); \
294 #define NEXT_SEQID(seqid) (((seqid) + 1) & 0x3ffffff)
296 #define IPMI_MAX_CHANNELS 16
297 struct ipmi_channel {
298 unsigned char medium;
299 unsigned char protocol;
302 struct ipmi_channel_set {
303 struct ipmi_channel c[IPMI_MAX_CHANNELS];
306 struct ipmi_my_addrinfo {
308 * My slave address. This is initialized to IPMI_BMC_SLAVE_ADDR,
309 * but may be changed by the user.
311 unsigned char address;
314 * My LUN. This should generally stay the SMS LUN, but just in
321 * Note that the product id, manufacturer id, guid, and device id are
322 * immutable in this structure, so dyn_mutex is not required for
323 * accessing those. If those change on a BMC, a new BMC is allocated.
326 struct platform_device pdev;
327 struct list_head intfs; /* Interfaces on this BMC. */
328 struct ipmi_device_id id;
329 struct ipmi_device_id fetch_id;
331 unsigned long dyn_id_expiry;
332 struct mutex dyn_mutex; /* Protects id, intfs, & dyn* */
336 struct kref usecount;
337 struct work_struct remove_work;
339 #define to_bmc_device(x) container_of((x), struct bmc_device, pdev.dev)
341 static int bmc_get_device_id(struct ipmi_smi *intf, struct bmc_device *bmc,
342 struct ipmi_device_id *id,
343 bool *guid_set, guid_t *guid);
346 * Various statistics for IPMI, these index stats[] in the ipmi_smi
349 enum ipmi_stat_indexes {
350 /* Commands we got from the user that were invalid. */
351 IPMI_STAT_sent_invalid_commands = 0,
353 /* Commands we sent to the MC. */
354 IPMI_STAT_sent_local_commands,
356 /* Responses from the MC that were delivered to a user. */
357 IPMI_STAT_handled_local_responses,
359 /* Responses from the MC that were not delivered to a user. */
360 IPMI_STAT_unhandled_local_responses,
362 /* Commands we sent out to the IPMB bus. */
363 IPMI_STAT_sent_ipmb_commands,
365 /* Commands sent on the IPMB that had errors on the SEND CMD */
366 IPMI_STAT_sent_ipmb_command_errs,
368 /* Each retransmit increments this count. */
369 IPMI_STAT_retransmitted_ipmb_commands,
372 * When a message times out (runs out of retransmits) this is
375 IPMI_STAT_timed_out_ipmb_commands,
378 * This is like above, but for broadcasts. Broadcasts are
379 * *not* included in the above count (they are expected to
382 IPMI_STAT_timed_out_ipmb_broadcasts,
384 /* Responses I have sent to the IPMB bus. */
385 IPMI_STAT_sent_ipmb_responses,
387 /* The response was delivered to the user. */
388 IPMI_STAT_handled_ipmb_responses,
390 /* The response had invalid data in it. */
391 IPMI_STAT_invalid_ipmb_responses,
393 /* The response didn't have anyone waiting for it. */
394 IPMI_STAT_unhandled_ipmb_responses,
396 /* Commands we sent out to the IPMB bus. */
397 IPMI_STAT_sent_lan_commands,
399 /* Commands sent on the IPMB that had errors on the SEND CMD */
400 IPMI_STAT_sent_lan_command_errs,
402 /* Each retransmit increments this count. */
403 IPMI_STAT_retransmitted_lan_commands,
406 * When a message times out (runs out of retransmits) this is
409 IPMI_STAT_timed_out_lan_commands,
411 /* Responses I have sent to the IPMB bus. */
412 IPMI_STAT_sent_lan_responses,
414 /* The response was delivered to the user. */
415 IPMI_STAT_handled_lan_responses,
417 /* The response had invalid data in it. */
418 IPMI_STAT_invalid_lan_responses,
420 /* The response didn't have anyone waiting for it. */
421 IPMI_STAT_unhandled_lan_responses,
423 /* The command was delivered to the user. */
424 IPMI_STAT_handled_commands,
426 /* The command had invalid data in it. */
427 IPMI_STAT_invalid_commands,
429 /* The command didn't have anyone waiting for it. */
430 IPMI_STAT_unhandled_commands,
432 /* Invalid data in an event. */
433 IPMI_STAT_invalid_events,
435 /* Events that were received with the proper format. */
438 /* Retransmissions on IPMB that failed. */
439 IPMI_STAT_dropped_rexmit_ipmb_commands,
441 /* Retransmissions on LAN that failed. */
442 IPMI_STAT_dropped_rexmit_lan_commands,
444 /* This *must* remain last, add new values above this. */
449 #define IPMI_IPMB_NUM_SEQ 64
451 struct module *owner;
453 /* What interface number are we? */
456 struct kref refcount;
458 /* Set when the interface is being unregistered. */
461 /* Used for a list of interfaces. */
462 struct list_head link;
465 * The list of upper layers that are using me. seq_lock write
466 * protects this. Read protection is with srcu.
468 struct list_head users;
469 struct srcu_struct users_srcu;
471 /* Used for wake ups at startup. */
472 wait_queue_head_t waitq;
475 * Prevents the interface from being unregistered when the
476 * interface is used by being looked up through the BMC
479 struct mutex bmc_reg_mutex;
481 struct bmc_device tmp_bmc;
482 struct bmc_device *bmc;
484 struct list_head bmc_link;
486 bool in_bmc_register; /* Handle recursive situations. Yuck. */
487 struct work_struct bmc_reg_work;
489 const struct ipmi_smi_handlers *handlers;
492 /* Driver-model device for the system interface. */
493 struct device *si_dev;
496 * A table of sequence numbers for this interface. We use the
497 * sequence numbers for IPMB messages that go out of the
498 * interface to match them up with their responses. A routine
499 * is called periodically to time the items in this list.
502 struct seq_table seq_table[IPMI_IPMB_NUM_SEQ];
506 * Messages queued for delivery. If delivery fails (out of memory
507 * for instance), They will stay in here to be processed later in a
508 * periodic timer interrupt. The tasklet is for handling received
509 * messages directly from the handler.
511 spinlock_t waiting_rcv_msgs_lock;
512 struct list_head waiting_rcv_msgs;
513 atomic_t watchdog_pretimeouts_to_deliver;
514 struct tasklet_struct recv_tasklet;
516 spinlock_t xmit_msgs_lock;
517 struct list_head xmit_msgs;
518 struct ipmi_smi_msg *curr_msg;
519 struct list_head hp_xmit_msgs;
522 * The list of command receivers that are registered for commands
525 struct mutex cmd_rcvrs_mutex;
526 struct list_head cmd_rcvrs;
529 * Events that were queues because no one was there to receive
532 spinlock_t events_lock; /* For dealing with event stuff. */
533 struct list_head waiting_events;
534 unsigned int waiting_events_count; /* How many events in queue? */
535 char delivering_events;
536 char event_msg_printed;
538 /* How many users are waiting for events? */
539 atomic_t event_waiters;
540 unsigned int ticks_to_req_ev;
542 spinlock_t watch_lock; /* For dealing with watch stuff below. */
544 /* How many users are waiting for commands? */
545 unsigned int command_waiters;
547 /* How many users are waiting for watchdogs? */
548 unsigned int watchdog_waiters;
550 /* How many users are waiting for message responses? */
551 unsigned int response_waiters;
554 * Tells what the lower layer has last been asked to watch for,
555 * messages and/or watchdogs. Protected by watch_lock.
557 unsigned int last_watch_mask;
560 * The event receiver for my BMC, only really used at panic
561 * shutdown as a place to store this.
563 unsigned char event_receiver;
564 unsigned char event_receiver_lun;
565 unsigned char local_sel_device;
566 unsigned char local_event_generator;
568 /* For handling of maintenance mode. */
569 int maintenance_mode;
570 bool maintenance_mode_enable;
571 int auto_maintenance_timeout;
572 spinlock_t maintenance_mode_lock; /* Used in a timer... */
575 * If we are doing maintenance on something on IPMB, extend
576 * the timeout time to avoid timeouts writing firmware and
579 int ipmb_maintenance_mode_timeout;
582 * A cheap hack, if this is non-null and a message to an
583 * interface comes in with a NULL user, call this routine with
584 * it. Note that the message will still be freed by the
585 * caller. This only works on the system interface.
587 * Protected by bmc_reg_mutex.
589 void (*null_user_handler)(struct ipmi_smi *intf,
590 struct ipmi_recv_msg *msg);
593 * When we are scanning the channels for an SMI, this will
594 * tell which channel we are scanning.
598 /* Channel information */
599 struct ipmi_channel_set *channel_list;
600 unsigned int curr_working_cset; /* First index into the following. */
601 struct ipmi_channel_set wchannels[2];
602 struct ipmi_my_addrinfo addrinfo[IPMI_MAX_CHANNELS];
605 atomic_t stats[IPMI_NUM_STATS];
608 * run_to_completion duplicate of smb_info, smi_info
609 * and ipmi_serial_info structures. Used to decrease numbers of
610 * parameters passed by "low" level IPMI code.
612 int run_to_completion;
614 #define to_si_intf_from_dev(device) container_of(device, struct ipmi_smi, dev)
616 static void __get_guid(struct ipmi_smi *intf);
617 static void __ipmi_bmc_unregister(struct ipmi_smi *intf);
618 static int __ipmi_bmc_register(struct ipmi_smi *intf,
619 struct ipmi_device_id *id,
620 bool guid_set, guid_t *guid, int intf_num);
621 static int __scan_channels(struct ipmi_smi *intf, struct ipmi_device_id *id);
625 * The driver model view of the IPMI messaging driver.
627 static struct platform_driver ipmidriver = {
630 .bus = &platform_bus_type
634 * This mutex keeps us from adding the same BMC twice.
636 static DEFINE_MUTEX(ipmidriver_mutex);
638 static LIST_HEAD(ipmi_interfaces);
639 static DEFINE_MUTEX(ipmi_interfaces_mutex);
640 static struct srcu_struct ipmi_interfaces_srcu;
643 * List of watchers that want to know when smi's are added and deleted.
645 static LIST_HEAD(smi_watchers);
646 static DEFINE_MUTEX(smi_watchers_mutex);
648 #define ipmi_inc_stat(intf, stat) \
649 atomic_inc(&(intf)->stats[IPMI_STAT_ ## stat])
650 #define ipmi_get_stat(intf, stat) \
651 ((unsigned int) atomic_read(&(intf)->stats[IPMI_STAT_ ## stat]))
653 static const char * const addr_src_to_str[] = {
654 "invalid", "hotmod", "hardcoded", "SPMI", "ACPI", "SMBIOS", "PCI",
655 "device-tree", "platform"
658 const char *ipmi_addr_src_to_str(enum ipmi_addr_src src)
661 src = 0; /* Invalid */
662 return addr_src_to_str[src];
664 EXPORT_SYMBOL(ipmi_addr_src_to_str);
666 static int is_lan_addr(struct ipmi_addr *addr)
668 return addr->addr_type == IPMI_LAN_ADDR_TYPE;
671 static int is_ipmb_addr(struct ipmi_addr *addr)
673 return addr->addr_type == IPMI_IPMB_ADDR_TYPE;
676 static int is_ipmb_bcast_addr(struct ipmi_addr *addr)
678 return addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE;
681 static void free_recv_msg_list(struct list_head *q)
683 struct ipmi_recv_msg *msg, *msg2;
685 list_for_each_entry_safe(msg, msg2, q, link) {
686 list_del(&msg->link);
687 ipmi_free_recv_msg(msg);
691 static void free_smi_msg_list(struct list_head *q)
693 struct ipmi_smi_msg *msg, *msg2;
695 list_for_each_entry_safe(msg, msg2, q, link) {
696 list_del(&msg->link);
697 ipmi_free_smi_msg(msg);
701 static void clean_up_interface_data(struct ipmi_smi *intf)
704 struct cmd_rcvr *rcvr, *rcvr2;
705 struct list_head list;
707 tasklet_kill(&intf->recv_tasklet);
709 free_smi_msg_list(&intf->waiting_rcv_msgs);
710 free_recv_msg_list(&intf->waiting_events);
713 * Wholesale remove all the entries from the list in the
714 * interface and wait for RCU to know that none are in use.
716 mutex_lock(&intf->cmd_rcvrs_mutex);
717 INIT_LIST_HEAD(&list);
718 list_splice_init_rcu(&intf->cmd_rcvrs, &list, synchronize_rcu);
719 mutex_unlock(&intf->cmd_rcvrs_mutex);
721 list_for_each_entry_safe(rcvr, rcvr2, &list, link)
724 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
725 if ((intf->seq_table[i].inuse)
726 && (intf->seq_table[i].recv_msg))
727 ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
731 static void intf_free(struct kref *ref)
733 struct ipmi_smi *intf = container_of(ref, struct ipmi_smi, refcount);
735 clean_up_interface_data(intf);
739 struct watcher_entry {
741 struct ipmi_smi *intf;
742 struct list_head link;
745 int ipmi_smi_watcher_register(struct ipmi_smi_watcher *watcher)
747 struct ipmi_smi *intf;
751 * Make sure the driver is actually initialized, this handles
752 * problems with initialization order.
754 rv = ipmi_init_msghandler();
758 mutex_lock(&smi_watchers_mutex);
760 list_add(&watcher->link, &smi_watchers);
762 index = srcu_read_lock(&ipmi_interfaces_srcu);
763 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
764 int intf_num = READ_ONCE(intf->intf_num);
768 watcher->new_smi(intf_num, intf->si_dev);
770 srcu_read_unlock(&ipmi_interfaces_srcu, index);
772 mutex_unlock(&smi_watchers_mutex);
776 EXPORT_SYMBOL(ipmi_smi_watcher_register);
778 int ipmi_smi_watcher_unregister(struct ipmi_smi_watcher *watcher)
780 mutex_lock(&smi_watchers_mutex);
781 list_del(&watcher->link);
782 mutex_unlock(&smi_watchers_mutex);
785 EXPORT_SYMBOL(ipmi_smi_watcher_unregister);
788 * Must be called with smi_watchers_mutex held.
791 call_smi_watchers(int i, struct device *dev)
793 struct ipmi_smi_watcher *w;
795 mutex_lock(&smi_watchers_mutex);
796 list_for_each_entry(w, &smi_watchers, link) {
797 if (try_module_get(w->owner)) {
799 module_put(w->owner);
802 mutex_unlock(&smi_watchers_mutex);
806 ipmi_addr_equal(struct ipmi_addr *addr1, struct ipmi_addr *addr2)
808 if (addr1->addr_type != addr2->addr_type)
811 if (addr1->channel != addr2->channel)
814 if (addr1->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
815 struct ipmi_system_interface_addr *smi_addr1
816 = (struct ipmi_system_interface_addr *) addr1;
817 struct ipmi_system_interface_addr *smi_addr2
818 = (struct ipmi_system_interface_addr *) addr2;
819 return (smi_addr1->lun == smi_addr2->lun);
822 if (is_ipmb_addr(addr1) || is_ipmb_bcast_addr(addr1)) {
823 struct ipmi_ipmb_addr *ipmb_addr1
824 = (struct ipmi_ipmb_addr *) addr1;
825 struct ipmi_ipmb_addr *ipmb_addr2
826 = (struct ipmi_ipmb_addr *) addr2;
828 return ((ipmb_addr1->slave_addr == ipmb_addr2->slave_addr)
829 && (ipmb_addr1->lun == ipmb_addr2->lun));
832 if (is_lan_addr(addr1)) {
833 struct ipmi_lan_addr *lan_addr1
834 = (struct ipmi_lan_addr *) addr1;
835 struct ipmi_lan_addr *lan_addr2
836 = (struct ipmi_lan_addr *) addr2;
838 return ((lan_addr1->remote_SWID == lan_addr2->remote_SWID)
839 && (lan_addr1->local_SWID == lan_addr2->local_SWID)
840 && (lan_addr1->session_handle
841 == lan_addr2->session_handle)
842 && (lan_addr1->lun == lan_addr2->lun));
848 int ipmi_validate_addr(struct ipmi_addr *addr, int len)
850 if (len < sizeof(struct ipmi_system_interface_addr))
853 if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
854 if (addr->channel != IPMI_BMC_CHANNEL)
859 if ((addr->channel == IPMI_BMC_CHANNEL)
860 || (addr->channel >= IPMI_MAX_CHANNELS)
861 || (addr->channel < 0))
864 if (is_ipmb_addr(addr) || is_ipmb_bcast_addr(addr)) {
865 if (len < sizeof(struct ipmi_ipmb_addr))
870 if (is_lan_addr(addr)) {
871 if (len < sizeof(struct ipmi_lan_addr))
878 EXPORT_SYMBOL(ipmi_validate_addr);
880 unsigned int ipmi_addr_length(int addr_type)
882 if (addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
883 return sizeof(struct ipmi_system_interface_addr);
885 if ((addr_type == IPMI_IPMB_ADDR_TYPE)
886 || (addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE))
887 return sizeof(struct ipmi_ipmb_addr);
889 if (addr_type == IPMI_LAN_ADDR_TYPE)
890 return sizeof(struct ipmi_lan_addr);
894 EXPORT_SYMBOL(ipmi_addr_length);
896 static int deliver_response(struct ipmi_smi *intf, struct ipmi_recv_msg *msg)
901 /* Special handling for NULL users. */
902 if (intf->null_user_handler) {
903 intf->null_user_handler(intf, msg);
905 /* No handler, so give up. */
908 ipmi_free_recv_msg(msg);
909 } else if (oops_in_progress) {
911 * If we are running in the panic context, calling the
912 * receive handler doesn't much meaning and has a deadlock
913 * risk. At this moment, simply skip it in that case.
915 ipmi_free_recv_msg(msg);
918 struct ipmi_user *user = acquire_ipmi_user(msg->user, &index);
921 user->handler->ipmi_recv_hndl(msg, user->handler_data);
922 release_ipmi_user(user, index);
924 /* User went away, give up. */
925 ipmi_free_recv_msg(msg);
933 static void deliver_local_response(struct ipmi_smi *intf,
934 struct ipmi_recv_msg *msg)
936 if (deliver_response(intf, msg))
937 ipmi_inc_stat(intf, unhandled_local_responses);
939 ipmi_inc_stat(intf, handled_local_responses);
942 static void deliver_err_response(struct ipmi_smi *intf,
943 struct ipmi_recv_msg *msg, int err)
945 msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
946 msg->msg_data[0] = err;
947 msg->msg.netfn |= 1; /* Convert to a response. */
948 msg->msg.data_len = 1;
949 msg->msg.data = msg->msg_data;
950 deliver_local_response(intf, msg);
953 static void smi_add_watch(struct ipmi_smi *intf, unsigned int flags)
955 unsigned long iflags;
957 if (!intf->handlers->set_need_watch)
960 spin_lock_irqsave(&intf->watch_lock, iflags);
961 if (flags & IPMI_WATCH_MASK_CHECK_MESSAGES)
962 intf->response_waiters++;
964 if (flags & IPMI_WATCH_MASK_CHECK_WATCHDOG)
965 intf->watchdog_waiters++;
967 if (flags & IPMI_WATCH_MASK_CHECK_COMMANDS)
968 intf->command_waiters++;
970 if ((intf->last_watch_mask & flags) != flags) {
971 intf->last_watch_mask |= flags;
972 intf->handlers->set_need_watch(intf->send_info,
973 intf->last_watch_mask);
975 spin_unlock_irqrestore(&intf->watch_lock, iflags);
978 static void smi_remove_watch(struct ipmi_smi *intf, unsigned int flags)
980 unsigned long iflags;
982 if (!intf->handlers->set_need_watch)
985 spin_lock_irqsave(&intf->watch_lock, iflags);
986 if (flags & IPMI_WATCH_MASK_CHECK_MESSAGES)
987 intf->response_waiters--;
989 if (flags & IPMI_WATCH_MASK_CHECK_WATCHDOG)
990 intf->watchdog_waiters--;
992 if (flags & IPMI_WATCH_MASK_CHECK_COMMANDS)
993 intf->command_waiters--;
996 if (intf->response_waiters)
997 flags |= IPMI_WATCH_MASK_CHECK_MESSAGES;
998 if (intf->watchdog_waiters)
999 flags |= IPMI_WATCH_MASK_CHECK_WATCHDOG;
1000 if (intf->command_waiters)
1001 flags |= IPMI_WATCH_MASK_CHECK_COMMANDS;
1003 if (intf->last_watch_mask != flags) {
1004 intf->last_watch_mask = flags;
1005 intf->handlers->set_need_watch(intf->send_info,
1006 intf->last_watch_mask);
1008 spin_unlock_irqrestore(&intf->watch_lock, iflags);
1012 * Find the next sequence number not being used and add the given
1013 * message with the given timeout to the sequence table. This must be
1014 * called with the interface's seq_lock held.
1016 static int intf_next_seq(struct ipmi_smi *intf,
1017 struct ipmi_recv_msg *recv_msg,
1018 unsigned long timeout,
1028 timeout = default_retry_ms;
1030 retries = default_max_retries;
1032 for (i = intf->curr_seq; (i+1)%IPMI_IPMB_NUM_SEQ != intf->curr_seq;
1033 i = (i+1)%IPMI_IPMB_NUM_SEQ) {
1034 if (!intf->seq_table[i].inuse)
1038 if (!intf->seq_table[i].inuse) {
1039 intf->seq_table[i].recv_msg = recv_msg;
1042 * Start with the maximum timeout, when the send response
1043 * comes in we will start the real timer.
1045 intf->seq_table[i].timeout = MAX_MSG_TIMEOUT;
1046 intf->seq_table[i].orig_timeout = timeout;
1047 intf->seq_table[i].retries_left = retries;
1048 intf->seq_table[i].broadcast = broadcast;
1049 intf->seq_table[i].inuse = 1;
1050 intf->seq_table[i].seqid = NEXT_SEQID(intf->seq_table[i].seqid);
1052 *seqid = intf->seq_table[i].seqid;
1053 intf->curr_seq = (i+1)%IPMI_IPMB_NUM_SEQ;
1054 smi_add_watch(intf, IPMI_WATCH_MASK_CHECK_MESSAGES);
1064 * Return the receive message for the given sequence number and
1065 * release the sequence number so it can be reused. Some other data
1066 * is passed in to be sure the message matches up correctly (to help
1067 * guard against message coming in after their timeout and the
1068 * sequence number being reused).
1070 static int intf_find_seq(struct ipmi_smi *intf,
1074 unsigned char netfn,
1075 struct ipmi_addr *addr,
1076 struct ipmi_recv_msg **recv_msg)
1079 unsigned long flags;
1081 if (seq >= IPMI_IPMB_NUM_SEQ)
1084 spin_lock_irqsave(&intf->seq_lock, flags);
1085 if (intf->seq_table[seq].inuse) {
1086 struct ipmi_recv_msg *msg = intf->seq_table[seq].recv_msg;
1088 if ((msg->addr.channel == channel) && (msg->msg.cmd == cmd)
1089 && (msg->msg.netfn == netfn)
1090 && (ipmi_addr_equal(addr, &msg->addr))) {
1092 intf->seq_table[seq].inuse = 0;
1093 smi_remove_watch(intf, IPMI_WATCH_MASK_CHECK_MESSAGES);
1097 spin_unlock_irqrestore(&intf->seq_lock, flags);
1103 /* Start the timer for a specific sequence table entry. */
1104 static int intf_start_seq_timer(struct ipmi_smi *intf,
1108 unsigned long flags;
1110 unsigned long seqid;
1113 GET_SEQ_FROM_MSGID(msgid, seq, seqid);
1115 spin_lock_irqsave(&intf->seq_lock, flags);
1117 * We do this verification because the user can be deleted
1118 * while a message is outstanding.
1120 if ((intf->seq_table[seq].inuse)
1121 && (intf->seq_table[seq].seqid == seqid)) {
1122 struct seq_table *ent = &intf->seq_table[seq];
1123 ent->timeout = ent->orig_timeout;
1126 spin_unlock_irqrestore(&intf->seq_lock, flags);
1131 /* Got an error for the send message for a specific sequence number. */
1132 static int intf_err_seq(struct ipmi_smi *intf,
1137 unsigned long flags;
1139 unsigned long seqid;
1140 struct ipmi_recv_msg *msg = NULL;
1143 GET_SEQ_FROM_MSGID(msgid, seq, seqid);
1145 spin_lock_irqsave(&intf->seq_lock, flags);
1147 * We do this verification because the user can be deleted
1148 * while a message is outstanding.
1150 if ((intf->seq_table[seq].inuse)
1151 && (intf->seq_table[seq].seqid == seqid)) {
1152 struct seq_table *ent = &intf->seq_table[seq];
1155 smi_remove_watch(intf, IPMI_WATCH_MASK_CHECK_MESSAGES);
1156 msg = ent->recv_msg;
1159 spin_unlock_irqrestore(&intf->seq_lock, flags);
1162 deliver_err_response(intf, msg, err);
1167 static void free_user_work(struct work_struct *work)
1169 struct ipmi_user *user = container_of(work, struct ipmi_user,
1172 cleanup_srcu_struct(&user->release_barrier);
1176 int ipmi_create_user(unsigned int if_num,
1177 const struct ipmi_user_hndl *handler,
1179 struct ipmi_user **user)
1181 unsigned long flags;
1182 struct ipmi_user *new_user;
1184 struct ipmi_smi *intf;
1187 * There is no module usecount here, because it's not
1188 * required. Since this can only be used by and called from
1189 * other modules, they will implicitly use this module, and
1190 * thus this can't be removed unless the other modules are
1194 if (handler == NULL)
1198 * Make sure the driver is actually initialized, this handles
1199 * problems with initialization order.
1201 rv = ipmi_init_msghandler();
1205 new_user = kmalloc(sizeof(*new_user), GFP_KERNEL);
1209 index = srcu_read_lock(&ipmi_interfaces_srcu);
1210 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
1211 if (intf->intf_num == if_num)
1214 /* Not found, return an error */
1219 INIT_WORK(&new_user->remove_work, free_user_work);
1221 rv = init_srcu_struct(&new_user->release_barrier);
1225 if (!try_module_get(intf->owner)) {
1230 /* Note that each existing user holds a refcount to the interface. */
1231 kref_get(&intf->refcount);
1233 kref_init(&new_user->refcount);
1234 new_user->handler = handler;
1235 new_user->handler_data = handler_data;
1236 new_user->intf = intf;
1237 new_user->gets_events = false;
1239 rcu_assign_pointer(new_user->self, new_user);
1240 spin_lock_irqsave(&intf->seq_lock, flags);
1241 list_add_rcu(&new_user->link, &intf->users);
1242 spin_unlock_irqrestore(&intf->seq_lock, flags);
1243 if (handler->ipmi_watchdog_pretimeout)
1244 /* User wants pretimeouts, so make sure to watch for them. */
1245 smi_add_watch(intf, IPMI_WATCH_MASK_CHECK_WATCHDOG);
1246 srcu_read_unlock(&ipmi_interfaces_srcu, index);
1251 srcu_read_unlock(&ipmi_interfaces_srcu, index);
1255 EXPORT_SYMBOL(ipmi_create_user);
1257 int ipmi_get_smi_info(int if_num, struct ipmi_smi_info *data)
1260 struct ipmi_smi *intf;
1262 index = srcu_read_lock(&ipmi_interfaces_srcu);
1263 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
1264 if (intf->intf_num == if_num)
1267 srcu_read_unlock(&ipmi_interfaces_srcu, index);
1269 /* Not found, return an error */
1273 if (!intf->handlers->get_smi_info)
1276 rv = intf->handlers->get_smi_info(intf->send_info, data);
1277 srcu_read_unlock(&ipmi_interfaces_srcu, index);
1281 EXPORT_SYMBOL(ipmi_get_smi_info);
1283 static void free_user(struct kref *ref)
1285 struct ipmi_user *user = container_of(ref, struct ipmi_user, refcount);
1287 /* SRCU cleanup must happen in task context. */
1288 schedule_work(&user->remove_work);
1291 static void _ipmi_destroy_user(struct ipmi_user *user)
1293 struct ipmi_smi *intf = user->intf;
1295 unsigned long flags;
1296 struct cmd_rcvr *rcvr;
1297 struct cmd_rcvr *rcvrs = NULL;
1299 if (!acquire_ipmi_user(user, &i)) {
1301 * The user has already been cleaned up, just make sure
1302 * nothing is using it and return.
1304 synchronize_srcu(&user->release_barrier);
1308 rcu_assign_pointer(user->self, NULL);
1309 release_ipmi_user(user, i);
1311 synchronize_srcu(&user->release_barrier);
1313 if (user->handler->shutdown)
1314 user->handler->shutdown(user->handler_data);
1316 if (user->handler->ipmi_watchdog_pretimeout)
1317 smi_remove_watch(intf, IPMI_WATCH_MASK_CHECK_WATCHDOG);
1319 if (user->gets_events)
1320 atomic_dec(&intf->event_waiters);
1322 /* Remove the user from the interface's sequence table. */
1323 spin_lock_irqsave(&intf->seq_lock, flags);
1324 list_del_rcu(&user->link);
1326 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
1327 if (intf->seq_table[i].inuse
1328 && (intf->seq_table[i].recv_msg->user == user)) {
1329 intf->seq_table[i].inuse = 0;
1330 smi_remove_watch(intf, IPMI_WATCH_MASK_CHECK_MESSAGES);
1331 ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
1334 spin_unlock_irqrestore(&intf->seq_lock, flags);
1337 * Remove the user from the command receiver's table. First
1338 * we build a list of everything (not using the standard link,
1339 * since other things may be using it till we do
1340 * synchronize_srcu()) then free everything in that list.
1342 mutex_lock(&intf->cmd_rcvrs_mutex);
1343 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
1344 if (rcvr->user == user) {
1345 list_del_rcu(&rcvr->link);
1350 mutex_unlock(&intf->cmd_rcvrs_mutex);
1358 kref_put(&intf->refcount, intf_free);
1359 module_put(intf->owner);
1362 int ipmi_destroy_user(struct ipmi_user *user)
1364 _ipmi_destroy_user(user);
1366 kref_put(&user->refcount, free_user);
1370 EXPORT_SYMBOL(ipmi_destroy_user);
1372 int ipmi_get_version(struct ipmi_user *user,
1373 unsigned char *major,
1374 unsigned char *minor)
1376 struct ipmi_device_id id;
1379 user = acquire_ipmi_user(user, &index);
1383 rv = bmc_get_device_id(user->intf, NULL, &id, NULL, NULL);
1385 *major = ipmi_version_major(&id);
1386 *minor = ipmi_version_minor(&id);
1388 release_ipmi_user(user, index);
1392 EXPORT_SYMBOL(ipmi_get_version);
1394 int ipmi_set_my_address(struct ipmi_user *user,
1395 unsigned int channel,
1396 unsigned char address)
1400 user = acquire_ipmi_user(user, &index);
1404 if (channel >= IPMI_MAX_CHANNELS) {
1407 channel = array_index_nospec(channel, IPMI_MAX_CHANNELS);
1408 user->intf->addrinfo[channel].address = address;
1410 release_ipmi_user(user, index);
1414 EXPORT_SYMBOL(ipmi_set_my_address);
1416 int ipmi_get_my_address(struct ipmi_user *user,
1417 unsigned int channel,
1418 unsigned char *address)
1422 user = acquire_ipmi_user(user, &index);
1426 if (channel >= IPMI_MAX_CHANNELS) {
1429 channel = array_index_nospec(channel, IPMI_MAX_CHANNELS);
1430 *address = user->intf->addrinfo[channel].address;
1432 release_ipmi_user(user, index);
1436 EXPORT_SYMBOL(ipmi_get_my_address);
1438 int ipmi_set_my_LUN(struct ipmi_user *user,
1439 unsigned int channel,
1444 user = acquire_ipmi_user(user, &index);
1448 if (channel >= IPMI_MAX_CHANNELS) {
1451 channel = array_index_nospec(channel, IPMI_MAX_CHANNELS);
1452 user->intf->addrinfo[channel].lun = LUN & 0x3;
1454 release_ipmi_user(user, index);
1458 EXPORT_SYMBOL(ipmi_set_my_LUN);
1460 int ipmi_get_my_LUN(struct ipmi_user *user,
1461 unsigned int channel,
1462 unsigned char *address)
1466 user = acquire_ipmi_user(user, &index);
1470 if (channel >= IPMI_MAX_CHANNELS) {
1473 channel = array_index_nospec(channel, IPMI_MAX_CHANNELS);
1474 *address = user->intf->addrinfo[channel].lun;
1476 release_ipmi_user(user, index);
1480 EXPORT_SYMBOL(ipmi_get_my_LUN);
1482 int ipmi_get_maintenance_mode(struct ipmi_user *user)
1485 unsigned long flags;
1487 user = acquire_ipmi_user(user, &index);
1491 spin_lock_irqsave(&user->intf->maintenance_mode_lock, flags);
1492 mode = user->intf->maintenance_mode;
1493 spin_unlock_irqrestore(&user->intf->maintenance_mode_lock, flags);
1494 release_ipmi_user(user, index);
1498 EXPORT_SYMBOL(ipmi_get_maintenance_mode);
1500 static void maintenance_mode_update(struct ipmi_smi *intf)
1502 if (intf->handlers->set_maintenance_mode)
1503 intf->handlers->set_maintenance_mode(
1504 intf->send_info, intf->maintenance_mode_enable);
1507 int ipmi_set_maintenance_mode(struct ipmi_user *user, int mode)
1510 unsigned long flags;
1511 struct ipmi_smi *intf = user->intf;
1513 user = acquire_ipmi_user(user, &index);
1517 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
1518 if (intf->maintenance_mode != mode) {
1520 case IPMI_MAINTENANCE_MODE_AUTO:
1521 intf->maintenance_mode_enable
1522 = (intf->auto_maintenance_timeout > 0);
1525 case IPMI_MAINTENANCE_MODE_OFF:
1526 intf->maintenance_mode_enable = false;
1529 case IPMI_MAINTENANCE_MODE_ON:
1530 intf->maintenance_mode_enable = true;
1537 intf->maintenance_mode = mode;
1539 maintenance_mode_update(intf);
1542 spin_unlock_irqrestore(&intf->maintenance_mode_lock, flags);
1543 release_ipmi_user(user, index);
1547 EXPORT_SYMBOL(ipmi_set_maintenance_mode);
1549 int ipmi_set_gets_events(struct ipmi_user *user, bool val)
1551 unsigned long flags;
1552 struct ipmi_smi *intf = user->intf;
1553 struct ipmi_recv_msg *msg, *msg2;
1554 struct list_head msgs;
1557 user = acquire_ipmi_user(user, &index);
1561 INIT_LIST_HEAD(&msgs);
1563 spin_lock_irqsave(&intf->events_lock, flags);
1564 if (user->gets_events == val)
1567 user->gets_events = val;
1570 if (atomic_inc_return(&intf->event_waiters) == 1)
1573 atomic_dec(&intf->event_waiters);
1576 if (intf->delivering_events)
1578 * Another thread is delivering events for this, so
1579 * let it handle any new events.
1583 /* Deliver any queued events. */
1584 while (user->gets_events && !list_empty(&intf->waiting_events)) {
1585 list_for_each_entry_safe(msg, msg2, &intf->waiting_events, link)
1586 list_move_tail(&msg->link, &msgs);
1587 intf->waiting_events_count = 0;
1588 if (intf->event_msg_printed) {
1589 dev_warn(intf->si_dev, "Event queue no longer full\n");
1590 intf->event_msg_printed = 0;
1593 intf->delivering_events = 1;
1594 spin_unlock_irqrestore(&intf->events_lock, flags);
1596 list_for_each_entry_safe(msg, msg2, &msgs, link) {
1598 kref_get(&user->refcount);
1599 deliver_local_response(intf, msg);
1602 spin_lock_irqsave(&intf->events_lock, flags);
1603 intf->delivering_events = 0;
1607 spin_unlock_irqrestore(&intf->events_lock, flags);
1608 release_ipmi_user(user, index);
1612 EXPORT_SYMBOL(ipmi_set_gets_events);
1614 static struct cmd_rcvr *find_cmd_rcvr(struct ipmi_smi *intf,
1615 unsigned char netfn,
1619 struct cmd_rcvr *rcvr;
1621 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
1622 if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)
1623 && (rcvr->chans & (1 << chan)))
1629 static int is_cmd_rcvr_exclusive(struct ipmi_smi *intf,
1630 unsigned char netfn,
1634 struct cmd_rcvr *rcvr;
1636 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
1637 if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)
1638 && (rcvr->chans & chans))
1644 int ipmi_register_for_cmd(struct ipmi_user *user,
1645 unsigned char netfn,
1649 struct ipmi_smi *intf = user->intf;
1650 struct cmd_rcvr *rcvr;
1653 user = acquire_ipmi_user(user, &index);
1657 rcvr = kmalloc(sizeof(*rcvr), GFP_KERNEL);
1663 rcvr->netfn = netfn;
1664 rcvr->chans = chans;
1667 mutex_lock(&intf->cmd_rcvrs_mutex);
1668 /* Make sure the command/netfn is not already registered. */
1669 if (!is_cmd_rcvr_exclusive(intf, netfn, cmd, chans)) {
1674 smi_add_watch(intf, IPMI_WATCH_MASK_CHECK_COMMANDS);
1676 list_add_rcu(&rcvr->link, &intf->cmd_rcvrs);
1679 mutex_unlock(&intf->cmd_rcvrs_mutex);
1683 release_ipmi_user(user, index);
1687 EXPORT_SYMBOL(ipmi_register_for_cmd);
1689 int ipmi_unregister_for_cmd(struct ipmi_user *user,
1690 unsigned char netfn,
1694 struct ipmi_smi *intf = user->intf;
1695 struct cmd_rcvr *rcvr;
1696 struct cmd_rcvr *rcvrs = NULL;
1697 int i, rv = -ENOENT, index;
1699 user = acquire_ipmi_user(user, &index);
1703 mutex_lock(&intf->cmd_rcvrs_mutex);
1704 for (i = 0; i < IPMI_NUM_CHANNELS; i++) {
1705 if (((1 << i) & chans) == 0)
1707 rcvr = find_cmd_rcvr(intf, netfn, cmd, i);
1710 if (rcvr->user == user) {
1712 rcvr->chans &= ~chans;
1713 if (rcvr->chans == 0) {
1714 list_del_rcu(&rcvr->link);
1720 mutex_unlock(&intf->cmd_rcvrs_mutex);
1722 release_ipmi_user(user, index);
1724 smi_remove_watch(intf, IPMI_WATCH_MASK_CHECK_COMMANDS);
1732 EXPORT_SYMBOL(ipmi_unregister_for_cmd);
1734 static unsigned char
1735 ipmb_checksum(unsigned char *data, int size)
1737 unsigned char csum = 0;
1739 for (; size > 0; size--, data++)
1745 static inline void format_ipmb_msg(struct ipmi_smi_msg *smi_msg,
1746 struct kernel_ipmi_msg *msg,
1747 struct ipmi_ipmb_addr *ipmb_addr,
1749 unsigned char ipmb_seq,
1751 unsigned char source_address,
1752 unsigned char source_lun)
1756 /* Format the IPMB header data. */
1757 smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
1758 smi_msg->data[1] = IPMI_SEND_MSG_CMD;
1759 smi_msg->data[2] = ipmb_addr->channel;
1761 smi_msg->data[3] = 0;
1762 smi_msg->data[i+3] = ipmb_addr->slave_addr;
1763 smi_msg->data[i+4] = (msg->netfn << 2) | (ipmb_addr->lun & 0x3);
1764 smi_msg->data[i+5] = ipmb_checksum(&smi_msg->data[i + 3], 2);
1765 smi_msg->data[i+6] = source_address;
1766 smi_msg->data[i+7] = (ipmb_seq << 2) | source_lun;
1767 smi_msg->data[i+8] = msg->cmd;
1769 /* Now tack on the data to the message. */
1770 if (msg->data_len > 0)
1771 memcpy(&smi_msg->data[i + 9], msg->data, msg->data_len);
1772 smi_msg->data_size = msg->data_len + 9;
1774 /* Now calculate the checksum and tack it on. */
1775 smi_msg->data[i+smi_msg->data_size]
1776 = ipmb_checksum(&smi_msg->data[i + 6], smi_msg->data_size - 6);
1779 * Add on the checksum size and the offset from the
1782 smi_msg->data_size += 1 + i;
1784 smi_msg->msgid = msgid;
1787 static inline void format_lan_msg(struct ipmi_smi_msg *smi_msg,
1788 struct kernel_ipmi_msg *msg,
1789 struct ipmi_lan_addr *lan_addr,
1791 unsigned char ipmb_seq,
1792 unsigned char source_lun)
1794 /* Format the IPMB header data. */
1795 smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
1796 smi_msg->data[1] = IPMI_SEND_MSG_CMD;
1797 smi_msg->data[2] = lan_addr->channel;
1798 smi_msg->data[3] = lan_addr->session_handle;
1799 smi_msg->data[4] = lan_addr->remote_SWID;
1800 smi_msg->data[5] = (msg->netfn << 2) | (lan_addr->lun & 0x3);
1801 smi_msg->data[6] = ipmb_checksum(&smi_msg->data[4], 2);
1802 smi_msg->data[7] = lan_addr->local_SWID;
1803 smi_msg->data[8] = (ipmb_seq << 2) | source_lun;
1804 smi_msg->data[9] = msg->cmd;
1806 /* Now tack on the data to the message. */
1807 if (msg->data_len > 0)
1808 memcpy(&smi_msg->data[10], msg->data, msg->data_len);
1809 smi_msg->data_size = msg->data_len + 10;
1811 /* Now calculate the checksum and tack it on. */
1812 smi_msg->data[smi_msg->data_size]
1813 = ipmb_checksum(&smi_msg->data[7], smi_msg->data_size - 7);
1816 * Add on the checksum size and the offset from the
1819 smi_msg->data_size += 1;
1821 smi_msg->msgid = msgid;
1824 static struct ipmi_smi_msg *smi_add_send_msg(struct ipmi_smi *intf,
1825 struct ipmi_smi_msg *smi_msg,
1828 if (intf->curr_msg) {
1830 list_add_tail(&smi_msg->link, &intf->hp_xmit_msgs);
1832 list_add_tail(&smi_msg->link, &intf->xmit_msgs);
1835 intf->curr_msg = smi_msg;
1841 static void smi_send(struct ipmi_smi *intf,
1842 const struct ipmi_smi_handlers *handlers,
1843 struct ipmi_smi_msg *smi_msg, int priority)
1845 int run_to_completion = intf->run_to_completion;
1846 unsigned long flags = 0;
1848 if (!run_to_completion)
1849 spin_lock_irqsave(&intf->xmit_msgs_lock, flags);
1850 smi_msg = smi_add_send_msg(intf, smi_msg, priority);
1852 if (!run_to_completion)
1853 spin_unlock_irqrestore(&intf->xmit_msgs_lock, flags);
1856 handlers->sender(intf->send_info, smi_msg);
1859 static bool is_maintenance_mode_cmd(struct kernel_ipmi_msg *msg)
1861 return (((msg->netfn == IPMI_NETFN_APP_REQUEST)
1862 && ((msg->cmd == IPMI_COLD_RESET_CMD)
1863 || (msg->cmd == IPMI_WARM_RESET_CMD)))
1864 || (msg->netfn == IPMI_NETFN_FIRMWARE_REQUEST));
1867 static int i_ipmi_req_sysintf(struct ipmi_smi *intf,
1868 struct ipmi_addr *addr,
1870 struct kernel_ipmi_msg *msg,
1871 struct ipmi_smi_msg *smi_msg,
1872 struct ipmi_recv_msg *recv_msg,
1874 unsigned int retry_time_ms)
1876 struct ipmi_system_interface_addr *smi_addr;
1879 /* Responses are not allowed to the SMI. */
1882 smi_addr = (struct ipmi_system_interface_addr *) addr;
1883 if (smi_addr->lun > 3) {
1884 ipmi_inc_stat(intf, sent_invalid_commands);
1888 memcpy(&recv_msg->addr, smi_addr, sizeof(*smi_addr));
1890 if ((msg->netfn == IPMI_NETFN_APP_REQUEST)
1891 && ((msg->cmd == IPMI_SEND_MSG_CMD)
1892 || (msg->cmd == IPMI_GET_MSG_CMD)
1893 || (msg->cmd == IPMI_READ_EVENT_MSG_BUFFER_CMD))) {
1895 * We don't let the user do these, since we manage
1896 * the sequence numbers.
1898 ipmi_inc_stat(intf, sent_invalid_commands);
1902 if (is_maintenance_mode_cmd(msg)) {
1903 unsigned long flags;
1905 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
1906 intf->auto_maintenance_timeout
1907 = maintenance_mode_timeout_ms;
1908 if (!intf->maintenance_mode
1909 && !intf->maintenance_mode_enable) {
1910 intf->maintenance_mode_enable = true;
1911 maintenance_mode_update(intf);
1913 spin_unlock_irqrestore(&intf->maintenance_mode_lock,
1917 if (msg->data_len + 2 > IPMI_MAX_MSG_LENGTH) {
1918 ipmi_inc_stat(intf, sent_invalid_commands);
1922 smi_msg->data[0] = (msg->netfn << 2) | (smi_addr->lun & 0x3);
1923 smi_msg->data[1] = msg->cmd;
1924 smi_msg->msgid = msgid;
1925 smi_msg->user_data = recv_msg;
1926 if (msg->data_len > 0)
1927 memcpy(&smi_msg->data[2], msg->data, msg->data_len);
1928 smi_msg->data_size = msg->data_len + 2;
1929 ipmi_inc_stat(intf, sent_local_commands);
1934 static int i_ipmi_req_ipmb(struct ipmi_smi *intf,
1935 struct ipmi_addr *addr,
1937 struct kernel_ipmi_msg *msg,
1938 struct ipmi_smi_msg *smi_msg,
1939 struct ipmi_recv_msg *recv_msg,
1940 unsigned char source_address,
1941 unsigned char source_lun,
1943 unsigned int retry_time_ms)
1945 struct ipmi_ipmb_addr *ipmb_addr;
1946 unsigned char ipmb_seq;
1949 struct ipmi_channel *chans;
1952 if (addr->channel >= IPMI_MAX_CHANNELS) {
1953 ipmi_inc_stat(intf, sent_invalid_commands);
1957 chans = READ_ONCE(intf->channel_list)->c;
1959 if (chans[addr->channel].medium != IPMI_CHANNEL_MEDIUM_IPMB) {
1960 ipmi_inc_stat(intf, sent_invalid_commands);
1964 if (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE) {
1966 * Broadcasts add a zero at the beginning of the
1967 * message, but otherwise is the same as an IPMB
1970 addr->addr_type = IPMI_IPMB_ADDR_TYPE;
1972 retries = 0; /* Don't retry broadcasts. */
1976 * 9 for the header and 1 for the checksum, plus
1977 * possibly one for the broadcast.
1979 if ((msg->data_len + 10 + broadcast) > IPMI_MAX_MSG_LENGTH) {
1980 ipmi_inc_stat(intf, sent_invalid_commands);
1984 ipmb_addr = (struct ipmi_ipmb_addr *) addr;
1985 if (ipmb_addr->lun > 3) {
1986 ipmi_inc_stat(intf, sent_invalid_commands);
1990 memcpy(&recv_msg->addr, ipmb_addr, sizeof(*ipmb_addr));
1992 if (recv_msg->msg.netfn & 0x1) {
1994 * It's a response, so use the user's sequence
1997 ipmi_inc_stat(intf, sent_ipmb_responses);
1998 format_ipmb_msg(smi_msg, msg, ipmb_addr, msgid,
2000 source_address, source_lun);
2003 * Save the receive message so we can use it
2004 * to deliver the response.
2006 smi_msg->user_data = recv_msg;
2008 /* It's a command, so get a sequence for it. */
2009 unsigned long flags;
2011 spin_lock_irqsave(&intf->seq_lock, flags);
2013 if (is_maintenance_mode_cmd(msg))
2014 intf->ipmb_maintenance_mode_timeout =
2015 maintenance_mode_timeout_ms;
2017 if (intf->ipmb_maintenance_mode_timeout && retry_time_ms == 0)
2018 /* Different default in maintenance mode */
2019 retry_time_ms = default_maintenance_retry_ms;
2022 * Create a sequence number with a 1 second
2023 * timeout and 4 retries.
2025 rv = intf_next_seq(intf,
2034 * We have used up all the sequence numbers,
2035 * probably, so abort.
2039 ipmi_inc_stat(intf, sent_ipmb_commands);
2042 * Store the sequence number in the message,
2043 * so that when the send message response
2044 * comes back we can start the timer.
2046 format_ipmb_msg(smi_msg, msg, ipmb_addr,
2047 STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
2048 ipmb_seq, broadcast,
2049 source_address, source_lun);
2052 * Copy the message into the recv message data, so we
2053 * can retransmit it later if necessary.
2055 memcpy(recv_msg->msg_data, smi_msg->data,
2056 smi_msg->data_size);
2057 recv_msg->msg.data = recv_msg->msg_data;
2058 recv_msg->msg.data_len = smi_msg->data_size;
2061 * We don't unlock until here, because we need
2062 * to copy the completed message into the
2063 * recv_msg before we release the lock.
2064 * Otherwise, race conditions may bite us. I
2065 * know that's pretty paranoid, but I prefer
2069 spin_unlock_irqrestore(&intf->seq_lock, flags);
2075 static int i_ipmi_req_lan(struct ipmi_smi *intf,
2076 struct ipmi_addr *addr,
2078 struct kernel_ipmi_msg *msg,
2079 struct ipmi_smi_msg *smi_msg,
2080 struct ipmi_recv_msg *recv_msg,
2081 unsigned char source_lun,
2083 unsigned int retry_time_ms)
2085 struct ipmi_lan_addr *lan_addr;
2086 unsigned char ipmb_seq;
2088 struct ipmi_channel *chans;
2091 if (addr->channel >= IPMI_MAX_CHANNELS) {
2092 ipmi_inc_stat(intf, sent_invalid_commands);
2096 chans = READ_ONCE(intf->channel_list)->c;
2098 if ((chans[addr->channel].medium
2099 != IPMI_CHANNEL_MEDIUM_8023LAN)
2100 && (chans[addr->channel].medium
2101 != IPMI_CHANNEL_MEDIUM_ASYNC)) {
2102 ipmi_inc_stat(intf, sent_invalid_commands);
2106 /* 11 for the header and 1 for the checksum. */
2107 if ((msg->data_len + 12) > IPMI_MAX_MSG_LENGTH) {
2108 ipmi_inc_stat(intf, sent_invalid_commands);
2112 lan_addr = (struct ipmi_lan_addr *) addr;
2113 if (lan_addr->lun > 3) {
2114 ipmi_inc_stat(intf, sent_invalid_commands);
2118 memcpy(&recv_msg->addr, lan_addr, sizeof(*lan_addr));
2120 if (recv_msg->msg.netfn & 0x1) {
2122 * It's a response, so use the user's sequence
2125 ipmi_inc_stat(intf, sent_lan_responses);
2126 format_lan_msg(smi_msg, msg, lan_addr, msgid,
2130 * Save the receive message so we can use it
2131 * to deliver the response.
2133 smi_msg->user_data = recv_msg;
2135 /* It's a command, so get a sequence for it. */
2136 unsigned long flags;
2138 spin_lock_irqsave(&intf->seq_lock, flags);
2141 * Create a sequence number with a 1 second
2142 * timeout and 4 retries.
2144 rv = intf_next_seq(intf,
2153 * We have used up all the sequence numbers,
2154 * probably, so abort.
2158 ipmi_inc_stat(intf, sent_lan_commands);
2161 * Store the sequence number in the message,
2162 * so that when the send message response
2163 * comes back we can start the timer.
2165 format_lan_msg(smi_msg, msg, lan_addr,
2166 STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
2167 ipmb_seq, source_lun);
2170 * Copy the message into the recv message data, so we
2171 * can retransmit it later if necessary.
2173 memcpy(recv_msg->msg_data, smi_msg->data,
2174 smi_msg->data_size);
2175 recv_msg->msg.data = recv_msg->msg_data;
2176 recv_msg->msg.data_len = smi_msg->data_size;
2179 * We don't unlock until here, because we need
2180 * to copy the completed message into the
2181 * recv_msg before we release the lock.
2182 * Otherwise, race conditions may bite us. I
2183 * know that's pretty paranoid, but I prefer
2187 spin_unlock_irqrestore(&intf->seq_lock, flags);
2194 * Separate from ipmi_request so that the user does not have to be
2195 * supplied in certain circumstances (mainly at panic time). If
2196 * messages are supplied, they will be freed, even if an error
2199 static int i_ipmi_request(struct ipmi_user *user,
2200 struct ipmi_smi *intf,
2201 struct ipmi_addr *addr,
2203 struct kernel_ipmi_msg *msg,
2204 void *user_msg_data,
2206 struct ipmi_recv_msg *supplied_recv,
2208 unsigned char source_address,
2209 unsigned char source_lun,
2211 unsigned int retry_time_ms)
2213 struct ipmi_smi_msg *smi_msg;
2214 struct ipmi_recv_msg *recv_msg;
2218 recv_msg = supplied_recv;
2220 recv_msg = ipmi_alloc_recv_msg();
2221 if (recv_msg == NULL) {
2226 recv_msg->user_msg_data = user_msg_data;
2229 smi_msg = (struct ipmi_smi_msg *) supplied_smi;
2231 smi_msg = ipmi_alloc_smi_msg();
2232 if (smi_msg == NULL) {
2234 ipmi_free_recv_msg(recv_msg);
2241 if (intf->in_shutdown) {
2246 recv_msg->user = user;
2248 /* The put happens when the message is freed. */
2249 kref_get(&user->refcount);
2250 recv_msg->msgid = msgid;
2252 * Store the message to send in the receive message so timeout
2253 * responses can get the proper response data.
2255 recv_msg->msg = *msg;
2257 if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
2258 rv = i_ipmi_req_sysintf(intf, addr, msgid, msg, smi_msg,
2259 recv_msg, retries, retry_time_ms);
2260 } else if (is_ipmb_addr(addr) || is_ipmb_bcast_addr(addr)) {
2261 rv = i_ipmi_req_ipmb(intf, addr, msgid, msg, smi_msg, recv_msg,
2262 source_address, source_lun,
2263 retries, retry_time_ms);
2264 } else if (is_lan_addr(addr)) {
2265 rv = i_ipmi_req_lan(intf, addr, msgid, msg, smi_msg, recv_msg,
2266 source_lun, retries, retry_time_ms);
2268 /* Unknown address type. */
2269 ipmi_inc_stat(intf, sent_invalid_commands);
2275 ipmi_free_smi_msg(smi_msg);
2276 ipmi_free_recv_msg(recv_msg);
2278 ipmi_debug_msg("Send", smi_msg->data, smi_msg->data_size);
2280 smi_send(intf, intf->handlers, smi_msg, priority);
2288 static int check_addr(struct ipmi_smi *intf,
2289 struct ipmi_addr *addr,
2290 unsigned char *saddr,
2293 if (addr->channel >= IPMI_MAX_CHANNELS)
2295 addr->channel = array_index_nospec(addr->channel, IPMI_MAX_CHANNELS);
2296 *lun = intf->addrinfo[addr->channel].lun;
2297 *saddr = intf->addrinfo[addr->channel].address;
2301 int ipmi_request_settime(struct ipmi_user *user,
2302 struct ipmi_addr *addr,
2304 struct kernel_ipmi_msg *msg,
2305 void *user_msg_data,
2308 unsigned int retry_time_ms)
2310 unsigned char saddr = 0, lun = 0;
2316 user = acquire_ipmi_user(user, &index);
2320 rv = check_addr(user->intf, addr, &saddr, &lun);
2322 rv = i_ipmi_request(user,
2335 release_ipmi_user(user, index);
2338 EXPORT_SYMBOL(ipmi_request_settime);
2340 int ipmi_request_supply_msgs(struct ipmi_user *user,
2341 struct ipmi_addr *addr,
2343 struct kernel_ipmi_msg *msg,
2344 void *user_msg_data,
2346 struct ipmi_recv_msg *supplied_recv,
2349 unsigned char saddr = 0, lun = 0;
2355 user = acquire_ipmi_user(user, &index);
2359 rv = check_addr(user->intf, addr, &saddr, &lun);
2361 rv = i_ipmi_request(user,
2374 release_ipmi_user(user, index);
2377 EXPORT_SYMBOL(ipmi_request_supply_msgs);
2379 static void bmc_device_id_handler(struct ipmi_smi *intf,
2380 struct ipmi_recv_msg *msg)
2384 if ((msg->addr.addr_type != IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
2385 || (msg->msg.netfn != IPMI_NETFN_APP_RESPONSE)
2386 || (msg->msg.cmd != IPMI_GET_DEVICE_ID_CMD)) {
2387 dev_warn(intf->si_dev,
2388 "invalid device_id msg: addr_type=%d netfn=%x cmd=%x\n",
2389 msg->addr.addr_type, msg->msg.netfn, msg->msg.cmd);
2393 rv = ipmi_demangle_device_id(msg->msg.netfn, msg->msg.cmd,
2394 msg->msg.data, msg->msg.data_len, &intf->bmc->fetch_id);
2396 dev_warn(intf->si_dev, "device id demangle failed: %d\n", rv);
2397 intf->bmc->dyn_id_set = 0;
2400 * Make sure the id data is available before setting
2404 intf->bmc->dyn_id_set = 1;
2407 wake_up(&intf->waitq);
2411 send_get_device_id_cmd(struct ipmi_smi *intf)
2413 struct ipmi_system_interface_addr si;
2414 struct kernel_ipmi_msg msg;
2416 si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2417 si.channel = IPMI_BMC_CHANNEL;
2420 msg.netfn = IPMI_NETFN_APP_REQUEST;
2421 msg.cmd = IPMI_GET_DEVICE_ID_CMD;
2425 return i_ipmi_request(NULL,
2427 (struct ipmi_addr *) &si,
2434 intf->addrinfo[0].address,
2435 intf->addrinfo[0].lun,
2439 static int __get_device_id(struct ipmi_smi *intf, struct bmc_device *bmc)
2443 bmc->dyn_id_set = 2;
2445 intf->null_user_handler = bmc_device_id_handler;
2447 rv = send_get_device_id_cmd(intf);
2451 wait_event(intf->waitq, bmc->dyn_id_set != 2);
2453 if (!bmc->dyn_id_set)
2454 rv = -EIO; /* Something went wrong in the fetch. */
2456 /* dyn_id_set makes the id data available. */
2459 intf->null_user_handler = NULL;
2465 * Fetch the device id for the bmc/interface. You must pass in either
2466 * bmc or intf, this code will get the other one. If the data has
2467 * been recently fetched, this will just use the cached data. Otherwise
2468 * it will run a new fetch.
2470 * Except for the first time this is called (in ipmi_add_smi()),
2471 * this will always return good data;
2473 static int __bmc_get_device_id(struct ipmi_smi *intf, struct bmc_device *bmc,
2474 struct ipmi_device_id *id,
2475 bool *guid_set, guid_t *guid, int intf_num)
2478 int prev_dyn_id_set, prev_guid_set;
2479 bool intf_set = intf != NULL;
2482 mutex_lock(&bmc->dyn_mutex);
2484 if (list_empty(&bmc->intfs)) {
2485 mutex_unlock(&bmc->dyn_mutex);
2488 intf = list_first_entry(&bmc->intfs, struct ipmi_smi,
2490 kref_get(&intf->refcount);
2491 mutex_unlock(&bmc->dyn_mutex);
2492 mutex_lock(&intf->bmc_reg_mutex);
2493 mutex_lock(&bmc->dyn_mutex);
2494 if (intf != list_first_entry(&bmc->intfs, struct ipmi_smi,
2496 mutex_unlock(&intf->bmc_reg_mutex);
2497 kref_put(&intf->refcount, intf_free);
2498 goto retry_bmc_lock;
2501 mutex_lock(&intf->bmc_reg_mutex);
2503 mutex_lock(&bmc->dyn_mutex);
2504 kref_get(&intf->refcount);
2507 /* If we have a valid and current ID, just return that. */
2508 if (intf->in_bmc_register ||
2509 (bmc->dyn_id_set && time_is_after_jiffies(bmc->dyn_id_expiry)))
2510 goto out_noprocessing;
2512 prev_guid_set = bmc->dyn_guid_set;
2515 prev_dyn_id_set = bmc->dyn_id_set;
2516 rv = __get_device_id(intf, bmc);
2521 * The guid, device id, manufacturer id, and product id should
2522 * not change on a BMC. If it does we have to do some dancing.
2524 if (!intf->bmc_registered
2525 || (!prev_guid_set && bmc->dyn_guid_set)
2526 || (!prev_dyn_id_set && bmc->dyn_id_set)
2527 || (prev_guid_set && bmc->dyn_guid_set
2528 && !guid_equal(&bmc->guid, &bmc->fetch_guid))
2529 || bmc->id.device_id != bmc->fetch_id.device_id
2530 || bmc->id.manufacturer_id != bmc->fetch_id.manufacturer_id
2531 || bmc->id.product_id != bmc->fetch_id.product_id) {
2532 struct ipmi_device_id id = bmc->fetch_id;
2533 int guid_set = bmc->dyn_guid_set;
2536 guid = bmc->fetch_guid;
2537 mutex_unlock(&bmc->dyn_mutex);
2539 __ipmi_bmc_unregister(intf);
2540 /* Fill in the temporary BMC for good measure. */
2542 intf->bmc->dyn_guid_set = guid_set;
2543 intf->bmc->guid = guid;
2544 if (__ipmi_bmc_register(intf, &id, guid_set, &guid, intf_num))
2545 need_waiter(intf); /* Retry later on an error. */
2547 __scan_channels(intf, &id);
2552 * We weren't given the interface on the
2553 * command line, so restart the operation on
2554 * the next interface for the BMC.
2556 mutex_unlock(&intf->bmc_reg_mutex);
2557 mutex_lock(&bmc->dyn_mutex);
2558 goto retry_bmc_lock;
2561 /* We have a new BMC, set it up. */
2563 mutex_lock(&bmc->dyn_mutex);
2564 goto out_noprocessing;
2565 } else if (memcmp(&bmc->fetch_id, &bmc->id, sizeof(bmc->id)))
2566 /* Version info changes, scan the channels again. */
2567 __scan_channels(intf, &bmc->fetch_id);
2569 bmc->dyn_id_expiry = jiffies + IPMI_DYN_DEV_ID_EXPIRY;
2572 if (rv && prev_dyn_id_set) {
2573 rv = 0; /* Ignore failures if we have previous data. */
2574 bmc->dyn_id_set = prev_dyn_id_set;
2577 bmc->id = bmc->fetch_id;
2578 if (bmc->dyn_guid_set)
2579 bmc->guid = bmc->fetch_guid;
2580 else if (prev_guid_set)
2582 * The guid used to be valid and it failed to fetch,
2583 * just use the cached value.
2585 bmc->dyn_guid_set = prev_guid_set;
2593 *guid_set = bmc->dyn_guid_set;
2595 if (guid && bmc->dyn_guid_set)
2599 mutex_unlock(&bmc->dyn_mutex);
2600 mutex_unlock(&intf->bmc_reg_mutex);
2602 kref_put(&intf->refcount, intf_free);
2606 static int bmc_get_device_id(struct ipmi_smi *intf, struct bmc_device *bmc,
2607 struct ipmi_device_id *id,
2608 bool *guid_set, guid_t *guid)
2610 return __bmc_get_device_id(intf, bmc, id, guid_set, guid, -1);
2613 static ssize_t device_id_show(struct device *dev,
2614 struct device_attribute *attr,
2617 struct bmc_device *bmc = to_bmc_device(dev);
2618 struct ipmi_device_id id;
2621 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2625 return snprintf(buf, 10, "%u\n", id.device_id);
2627 static DEVICE_ATTR_RO(device_id);
2629 static ssize_t provides_device_sdrs_show(struct device *dev,
2630 struct device_attribute *attr,
2633 struct bmc_device *bmc = to_bmc_device(dev);
2634 struct ipmi_device_id id;
2637 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2641 return snprintf(buf, 10, "%u\n", (id.device_revision & 0x80) >> 7);
2643 static DEVICE_ATTR_RO(provides_device_sdrs);
2645 static ssize_t revision_show(struct device *dev, struct device_attribute *attr,
2648 struct bmc_device *bmc = to_bmc_device(dev);
2649 struct ipmi_device_id id;
2652 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2656 return snprintf(buf, 20, "%u\n", id.device_revision & 0x0F);
2658 static DEVICE_ATTR_RO(revision);
2660 static ssize_t firmware_revision_show(struct device *dev,
2661 struct device_attribute *attr,
2664 struct bmc_device *bmc = to_bmc_device(dev);
2665 struct ipmi_device_id id;
2668 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2672 return snprintf(buf, 20, "%u.%x\n", id.firmware_revision_1,
2673 id.firmware_revision_2);
2675 static DEVICE_ATTR_RO(firmware_revision);
2677 static ssize_t ipmi_version_show(struct device *dev,
2678 struct device_attribute *attr,
2681 struct bmc_device *bmc = to_bmc_device(dev);
2682 struct ipmi_device_id id;
2685 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2689 return snprintf(buf, 20, "%u.%u\n",
2690 ipmi_version_major(&id),
2691 ipmi_version_minor(&id));
2693 static DEVICE_ATTR_RO(ipmi_version);
2695 static ssize_t add_dev_support_show(struct device *dev,
2696 struct device_attribute *attr,
2699 struct bmc_device *bmc = to_bmc_device(dev);
2700 struct ipmi_device_id id;
2703 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2707 return snprintf(buf, 10, "0x%02x\n", id.additional_device_support);
2709 static DEVICE_ATTR(additional_device_support, S_IRUGO, add_dev_support_show,
2712 static ssize_t manufacturer_id_show(struct device *dev,
2713 struct device_attribute *attr,
2716 struct bmc_device *bmc = to_bmc_device(dev);
2717 struct ipmi_device_id id;
2720 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2724 return snprintf(buf, 20, "0x%6.6x\n", id.manufacturer_id);
2726 static DEVICE_ATTR_RO(manufacturer_id);
2728 static ssize_t product_id_show(struct device *dev,
2729 struct device_attribute *attr,
2732 struct bmc_device *bmc = to_bmc_device(dev);
2733 struct ipmi_device_id id;
2736 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2740 return snprintf(buf, 10, "0x%4.4x\n", id.product_id);
2742 static DEVICE_ATTR_RO(product_id);
2744 static ssize_t aux_firmware_rev_show(struct device *dev,
2745 struct device_attribute *attr,
2748 struct bmc_device *bmc = to_bmc_device(dev);
2749 struct ipmi_device_id id;
2752 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2756 return snprintf(buf, 21, "0x%02x 0x%02x 0x%02x 0x%02x\n",
2757 id.aux_firmware_revision[3],
2758 id.aux_firmware_revision[2],
2759 id.aux_firmware_revision[1],
2760 id.aux_firmware_revision[0]);
2762 static DEVICE_ATTR(aux_firmware_revision, S_IRUGO, aux_firmware_rev_show, NULL);
2764 static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
2767 struct bmc_device *bmc = to_bmc_device(dev);
2772 rv = bmc_get_device_id(NULL, bmc, NULL, &guid_set, &guid);
2778 return snprintf(buf, UUID_STRING_LEN + 1 + 1, "%pUl\n", &guid);
2780 static DEVICE_ATTR_RO(guid);
2782 static struct attribute *bmc_dev_attrs[] = {
2783 &dev_attr_device_id.attr,
2784 &dev_attr_provides_device_sdrs.attr,
2785 &dev_attr_revision.attr,
2786 &dev_attr_firmware_revision.attr,
2787 &dev_attr_ipmi_version.attr,
2788 &dev_attr_additional_device_support.attr,
2789 &dev_attr_manufacturer_id.attr,
2790 &dev_attr_product_id.attr,
2791 &dev_attr_aux_firmware_revision.attr,
2792 &dev_attr_guid.attr,
2796 static umode_t bmc_dev_attr_is_visible(struct kobject *kobj,
2797 struct attribute *attr, int idx)
2799 struct device *dev = kobj_to_dev(kobj);
2800 struct bmc_device *bmc = to_bmc_device(dev);
2801 umode_t mode = attr->mode;
2804 if (attr == &dev_attr_aux_firmware_revision.attr) {
2805 struct ipmi_device_id id;
2807 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2808 return (!rv && id.aux_firmware_revision_set) ? mode : 0;
2810 if (attr == &dev_attr_guid.attr) {
2813 rv = bmc_get_device_id(NULL, bmc, NULL, &guid_set, NULL);
2814 return (!rv && guid_set) ? mode : 0;
2819 static const struct attribute_group bmc_dev_attr_group = {
2820 .attrs = bmc_dev_attrs,
2821 .is_visible = bmc_dev_attr_is_visible,
2824 static const struct attribute_group *bmc_dev_attr_groups[] = {
2825 &bmc_dev_attr_group,
2829 static const struct device_type bmc_device_type = {
2830 .groups = bmc_dev_attr_groups,
2833 static int __find_bmc_guid(struct device *dev, const void *data)
2835 const guid_t *guid = data;
2836 struct bmc_device *bmc;
2839 if (dev->type != &bmc_device_type)
2842 bmc = to_bmc_device(dev);
2843 rv = bmc->dyn_guid_set && guid_equal(&bmc->guid, guid);
2845 rv = kref_get_unless_zero(&bmc->usecount);
2850 * Returns with the bmc's usecount incremented, if it is non-NULL.
2852 static struct bmc_device *ipmi_find_bmc_guid(struct device_driver *drv,
2856 struct bmc_device *bmc = NULL;
2858 dev = driver_find_device(drv, NULL, guid, __find_bmc_guid);
2860 bmc = to_bmc_device(dev);
2866 struct prod_dev_id {
2867 unsigned int product_id;
2868 unsigned char device_id;
2871 static int __find_bmc_prod_dev_id(struct device *dev, const void *data)
2873 const struct prod_dev_id *cid = data;
2874 struct bmc_device *bmc;
2877 if (dev->type != &bmc_device_type)
2880 bmc = to_bmc_device(dev);
2881 rv = (bmc->id.product_id == cid->product_id
2882 && bmc->id.device_id == cid->device_id);
2884 rv = kref_get_unless_zero(&bmc->usecount);
2889 * Returns with the bmc's usecount incremented, if it is non-NULL.
2891 static struct bmc_device *ipmi_find_bmc_prod_dev_id(
2892 struct device_driver *drv,
2893 unsigned int product_id, unsigned char device_id)
2895 struct prod_dev_id id = {
2896 .product_id = product_id,
2897 .device_id = device_id,
2900 struct bmc_device *bmc = NULL;
2902 dev = driver_find_device(drv, NULL, &id, __find_bmc_prod_dev_id);
2904 bmc = to_bmc_device(dev);
2910 static DEFINE_IDA(ipmi_bmc_ida);
2913 release_bmc_device(struct device *dev)
2915 kfree(to_bmc_device(dev));
2918 static void cleanup_bmc_work(struct work_struct *work)
2920 struct bmc_device *bmc = container_of(work, struct bmc_device,
2922 int id = bmc->pdev.id; /* Unregister overwrites id */
2924 platform_device_unregister(&bmc->pdev);
2925 ida_simple_remove(&ipmi_bmc_ida, id);
2929 cleanup_bmc_device(struct kref *ref)
2931 struct bmc_device *bmc = container_of(ref, struct bmc_device, usecount);
2934 * Remove the platform device in a work queue to avoid issues
2935 * with removing the device attributes while reading a device
2938 schedule_work(&bmc->remove_work);
2942 * Must be called with intf->bmc_reg_mutex held.
2944 static void __ipmi_bmc_unregister(struct ipmi_smi *intf)
2946 struct bmc_device *bmc = intf->bmc;
2948 if (!intf->bmc_registered)
2951 sysfs_remove_link(&intf->si_dev->kobj, "bmc");
2952 sysfs_remove_link(&bmc->pdev.dev.kobj, intf->my_dev_name);
2953 kfree(intf->my_dev_name);
2954 intf->my_dev_name = NULL;
2956 mutex_lock(&bmc->dyn_mutex);
2957 list_del(&intf->bmc_link);
2958 mutex_unlock(&bmc->dyn_mutex);
2959 intf->bmc = &intf->tmp_bmc;
2960 kref_put(&bmc->usecount, cleanup_bmc_device);
2961 intf->bmc_registered = false;
2964 static void ipmi_bmc_unregister(struct ipmi_smi *intf)
2966 mutex_lock(&intf->bmc_reg_mutex);
2967 __ipmi_bmc_unregister(intf);
2968 mutex_unlock(&intf->bmc_reg_mutex);
2972 * Must be called with intf->bmc_reg_mutex held.
2974 static int __ipmi_bmc_register(struct ipmi_smi *intf,
2975 struct ipmi_device_id *id,
2976 bool guid_set, guid_t *guid, int intf_num)
2979 struct bmc_device *bmc;
2980 struct bmc_device *old_bmc;
2983 * platform_device_register() can cause bmc_reg_mutex to
2984 * be claimed because of the is_visible functions of
2985 * the attributes. Eliminate possible recursion and
2988 intf->in_bmc_register = true;
2989 mutex_unlock(&intf->bmc_reg_mutex);
2992 * Try to find if there is an bmc_device struct
2993 * representing the interfaced BMC already
2995 mutex_lock(&ipmidriver_mutex);
2997 old_bmc = ipmi_find_bmc_guid(&ipmidriver.driver, guid);
2999 old_bmc = ipmi_find_bmc_prod_dev_id(&ipmidriver.driver,
3004 * If there is already an bmc_device, free the new one,
3005 * otherwise register the new BMC device
3010 * Note: old_bmc already has usecount incremented by
3011 * the BMC find functions.
3013 intf->bmc = old_bmc;
3014 mutex_lock(&bmc->dyn_mutex);
3015 list_add_tail(&intf->bmc_link, &bmc->intfs);
3016 mutex_unlock(&bmc->dyn_mutex);
3018 dev_info(intf->si_dev,
3019 "interfacing existing BMC (man_id: 0x%6.6x, prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
3020 bmc->id.manufacturer_id,
3024 bmc = kzalloc(sizeof(*bmc), GFP_KERNEL);
3029 INIT_LIST_HEAD(&bmc->intfs);
3030 mutex_init(&bmc->dyn_mutex);
3031 INIT_WORK(&bmc->remove_work, cleanup_bmc_work);
3034 bmc->dyn_id_set = 1;
3035 bmc->dyn_guid_set = guid_set;
3037 bmc->dyn_id_expiry = jiffies + IPMI_DYN_DEV_ID_EXPIRY;
3039 bmc->pdev.name = "ipmi_bmc";
3041 rv = ida_simple_get(&ipmi_bmc_ida, 0, 0, GFP_KERNEL);
3047 bmc->pdev.dev.driver = &ipmidriver.driver;
3049 bmc->pdev.dev.release = release_bmc_device;
3050 bmc->pdev.dev.type = &bmc_device_type;
3051 kref_init(&bmc->usecount);
3054 mutex_lock(&bmc->dyn_mutex);
3055 list_add_tail(&intf->bmc_link, &bmc->intfs);
3056 mutex_unlock(&bmc->dyn_mutex);
3058 rv = platform_device_register(&bmc->pdev);
3060 dev_err(intf->si_dev,
3061 "Unable to register bmc device: %d\n",
3066 dev_info(intf->si_dev,
3067 "Found new BMC (man_id: 0x%6.6x, prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
3068 bmc->id.manufacturer_id,
3074 * create symlink from system interface device to bmc device
3077 rv = sysfs_create_link(&intf->si_dev->kobj, &bmc->pdev.dev.kobj, "bmc");
3079 dev_err(intf->si_dev, "Unable to create bmc symlink: %d\n", rv);
3084 intf_num = intf->intf_num;
3085 intf->my_dev_name = kasprintf(GFP_KERNEL, "ipmi%d", intf_num);
3086 if (!intf->my_dev_name) {
3088 dev_err(intf->si_dev, "Unable to allocate link from BMC: %d\n",
3093 rv = sysfs_create_link(&bmc->pdev.dev.kobj, &intf->si_dev->kobj,
3096 kfree(intf->my_dev_name);
3097 intf->my_dev_name = NULL;
3098 dev_err(intf->si_dev, "Unable to create symlink to bmc: %d\n",
3100 goto out_free_my_dev_name;
3103 intf->bmc_registered = true;
3106 mutex_unlock(&ipmidriver_mutex);
3107 mutex_lock(&intf->bmc_reg_mutex);
3108 intf->in_bmc_register = false;
3112 out_free_my_dev_name:
3113 kfree(intf->my_dev_name);
3114 intf->my_dev_name = NULL;
3117 sysfs_remove_link(&intf->si_dev->kobj, "bmc");
3120 mutex_lock(&bmc->dyn_mutex);
3121 list_del(&intf->bmc_link);
3122 mutex_unlock(&bmc->dyn_mutex);
3123 intf->bmc = &intf->tmp_bmc;
3124 kref_put(&bmc->usecount, cleanup_bmc_device);
3128 mutex_lock(&bmc->dyn_mutex);
3129 list_del(&intf->bmc_link);
3130 mutex_unlock(&bmc->dyn_mutex);
3131 intf->bmc = &intf->tmp_bmc;
3132 put_device(&bmc->pdev.dev);
3137 send_guid_cmd(struct ipmi_smi *intf, int chan)
3139 struct kernel_ipmi_msg msg;
3140 struct ipmi_system_interface_addr si;
3142 si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3143 si.channel = IPMI_BMC_CHANNEL;
3146 msg.netfn = IPMI_NETFN_APP_REQUEST;
3147 msg.cmd = IPMI_GET_DEVICE_GUID_CMD;
3150 return i_ipmi_request(NULL,
3152 (struct ipmi_addr *) &si,
3159 intf->addrinfo[0].address,
3160 intf->addrinfo[0].lun,
3164 static void guid_handler(struct ipmi_smi *intf, struct ipmi_recv_msg *msg)
3166 struct bmc_device *bmc = intf->bmc;
3168 if ((msg->addr.addr_type != IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
3169 || (msg->msg.netfn != IPMI_NETFN_APP_RESPONSE)
3170 || (msg->msg.cmd != IPMI_GET_DEVICE_GUID_CMD))
3174 if (msg->msg.data[0] != 0) {
3175 /* Error from getting the GUID, the BMC doesn't have one. */
3176 bmc->dyn_guid_set = 0;
3180 if (msg->msg.data_len < UUID_SIZE + 1) {
3181 bmc->dyn_guid_set = 0;
3182 dev_warn(intf->si_dev,
3183 "The GUID response from the BMC was too short, it was %d but should have been %d. Assuming GUID is not available.\n",
3184 msg->msg.data_len, UUID_SIZE + 1);
3188 guid_copy(&bmc->fetch_guid, (guid_t *)(msg->msg.data + 1));
3190 * Make sure the guid data is available before setting
3194 bmc->dyn_guid_set = 1;
3196 wake_up(&intf->waitq);
3199 static void __get_guid(struct ipmi_smi *intf)
3202 struct bmc_device *bmc = intf->bmc;
3204 bmc->dyn_guid_set = 2;
3205 intf->null_user_handler = guid_handler;
3206 rv = send_guid_cmd(intf, 0);
3208 /* Send failed, no GUID available. */
3209 bmc->dyn_guid_set = 0;
3211 wait_event(intf->waitq, bmc->dyn_guid_set != 2);
3213 /* dyn_guid_set makes the guid data available. */
3216 intf->null_user_handler = NULL;
3220 send_channel_info_cmd(struct ipmi_smi *intf, int chan)
3222 struct kernel_ipmi_msg msg;
3223 unsigned char data[1];
3224 struct ipmi_system_interface_addr si;
3226 si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3227 si.channel = IPMI_BMC_CHANNEL;
3230 msg.netfn = IPMI_NETFN_APP_REQUEST;
3231 msg.cmd = IPMI_GET_CHANNEL_INFO_CMD;
3235 return i_ipmi_request(NULL,
3237 (struct ipmi_addr *) &si,
3244 intf->addrinfo[0].address,
3245 intf->addrinfo[0].lun,
3250 channel_handler(struct ipmi_smi *intf, struct ipmi_recv_msg *msg)
3254 unsigned int set = intf->curr_working_cset;
3255 struct ipmi_channel *chans;
3257 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
3258 && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
3259 && (msg->msg.cmd == IPMI_GET_CHANNEL_INFO_CMD)) {
3260 /* It's the one we want */
3261 if (msg->msg.data[0] != 0) {
3262 /* Got an error from the channel, just go on. */
3264 if (msg->msg.data[0] == IPMI_INVALID_COMMAND_ERR) {
3266 * If the MC does not support this
3267 * command, that is legal. We just
3268 * assume it has one IPMB at channel
3271 intf->wchannels[set].c[0].medium
3272 = IPMI_CHANNEL_MEDIUM_IPMB;
3273 intf->wchannels[set].c[0].protocol
3274 = IPMI_CHANNEL_PROTOCOL_IPMB;
3276 intf->channel_list = intf->wchannels + set;
3277 intf->channels_ready = true;
3278 wake_up(&intf->waitq);
3283 if (msg->msg.data_len < 4) {
3284 /* Message not big enough, just go on. */
3287 ch = intf->curr_channel;
3288 chans = intf->wchannels[set].c;
3289 chans[ch].medium = msg->msg.data[2] & 0x7f;
3290 chans[ch].protocol = msg->msg.data[3] & 0x1f;
3293 intf->curr_channel++;
3294 if (intf->curr_channel >= IPMI_MAX_CHANNELS) {
3295 intf->channel_list = intf->wchannels + set;
3296 intf->channels_ready = true;
3297 wake_up(&intf->waitq);
3299 intf->channel_list = intf->wchannels + set;
3300 intf->channels_ready = true;
3301 rv = send_channel_info_cmd(intf, intf->curr_channel);
3305 /* Got an error somehow, just give up. */
3306 dev_warn(intf->si_dev,
3307 "Error sending channel information for channel %d: %d\n",
3308 intf->curr_channel, rv);
3310 intf->channel_list = intf->wchannels + set;
3311 intf->channels_ready = true;
3312 wake_up(&intf->waitq);
3320 * Must be holding intf->bmc_reg_mutex to call this.
3322 static int __scan_channels(struct ipmi_smi *intf, struct ipmi_device_id *id)
3326 if (ipmi_version_major(id) > 1
3327 || (ipmi_version_major(id) == 1
3328 && ipmi_version_minor(id) >= 5)) {
3332 * Start scanning the channels to see what is
3335 set = !intf->curr_working_cset;
3336 intf->curr_working_cset = set;
3337 memset(&intf->wchannels[set], 0,
3338 sizeof(struct ipmi_channel_set));
3340 intf->null_user_handler = channel_handler;
3341 intf->curr_channel = 0;
3342 rv = send_channel_info_cmd(intf, 0);
3344 dev_warn(intf->si_dev,
3345 "Error sending channel information for channel 0, %d\n",
3350 /* Wait for the channel info to be read. */
3351 wait_event(intf->waitq, intf->channels_ready);
3352 intf->null_user_handler = NULL;
3354 unsigned int set = intf->curr_working_cset;
3356 /* Assume a single IPMB channel at zero. */
3357 intf->wchannels[set].c[0].medium = IPMI_CHANNEL_MEDIUM_IPMB;
3358 intf->wchannels[set].c[0].protocol = IPMI_CHANNEL_PROTOCOL_IPMB;
3359 intf->channel_list = intf->wchannels + set;
3360 intf->channels_ready = true;
3366 static void ipmi_poll(struct ipmi_smi *intf)
3368 if (intf->handlers->poll)
3369 intf->handlers->poll(intf->send_info);
3370 /* In case something came in */
3371 handle_new_recv_msgs(intf);
3374 void ipmi_poll_interface(struct ipmi_user *user)
3376 ipmi_poll(user->intf);
3378 EXPORT_SYMBOL(ipmi_poll_interface);
3380 static void redo_bmc_reg(struct work_struct *work)
3382 struct ipmi_smi *intf = container_of(work, struct ipmi_smi,
3385 if (!intf->in_shutdown)
3386 bmc_get_device_id(intf, NULL, NULL, NULL, NULL);
3388 kref_put(&intf->refcount, intf_free);
3391 int ipmi_add_smi(struct module *owner,
3392 const struct ipmi_smi_handlers *handlers,
3394 struct device *si_dev,
3395 unsigned char slave_addr)
3399 struct ipmi_smi *intf, *tintf;
3400 struct list_head *link;
3401 struct ipmi_device_id id;
3404 * Make sure the driver is actually initialized, this handles
3405 * problems with initialization order.
3407 rv = ipmi_init_msghandler();
3411 intf = kzalloc(sizeof(*intf), GFP_KERNEL);
3415 rv = init_srcu_struct(&intf->users_srcu);
3421 intf->owner = owner;
3422 intf->bmc = &intf->tmp_bmc;
3423 INIT_LIST_HEAD(&intf->bmc->intfs);
3424 mutex_init(&intf->bmc->dyn_mutex);
3425 INIT_LIST_HEAD(&intf->bmc_link);
3426 mutex_init(&intf->bmc_reg_mutex);
3427 intf->intf_num = -1; /* Mark it invalid for now. */
3428 kref_init(&intf->refcount);
3429 INIT_WORK(&intf->bmc_reg_work, redo_bmc_reg);
3430 intf->si_dev = si_dev;
3431 for (j = 0; j < IPMI_MAX_CHANNELS; j++) {
3432 intf->addrinfo[j].address = IPMI_BMC_SLAVE_ADDR;
3433 intf->addrinfo[j].lun = 2;
3435 if (slave_addr != 0)
3436 intf->addrinfo[0].address = slave_addr;
3437 INIT_LIST_HEAD(&intf->users);
3438 intf->handlers = handlers;
3439 intf->send_info = send_info;
3440 spin_lock_init(&intf->seq_lock);
3441 for (j = 0; j < IPMI_IPMB_NUM_SEQ; j++) {
3442 intf->seq_table[j].inuse = 0;
3443 intf->seq_table[j].seqid = 0;
3446 spin_lock_init(&intf->waiting_rcv_msgs_lock);
3447 INIT_LIST_HEAD(&intf->waiting_rcv_msgs);
3448 tasklet_init(&intf->recv_tasklet,
3450 (unsigned long) intf);
3451 atomic_set(&intf->watchdog_pretimeouts_to_deliver, 0);
3452 spin_lock_init(&intf->xmit_msgs_lock);
3453 INIT_LIST_HEAD(&intf->xmit_msgs);
3454 INIT_LIST_HEAD(&intf->hp_xmit_msgs);
3455 spin_lock_init(&intf->events_lock);
3456 spin_lock_init(&intf->watch_lock);
3457 atomic_set(&intf->event_waiters, 0);
3458 intf->ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
3459 INIT_LIST_HEAD(&intf->waiting_events);
3460 intf->waiting_events_count = 0;
3461 mutex_init(&intf->cmd_rcvrs_mutex);
3462 spin_lock_init(&intf->maintenance_mode_lock);
3463 INIT_LIST_HEAD(&intf->cmd_rcvrs);
3464 init_waitqueue_head(&intf->waitq);
3465 for (i = 0; i < IPMI_NUM_STATS; i++)
3466 atomic_set(&intf->stats[i], 0);
3468 mutex_lock(&ipmi_interfaces_mutex);
3469 /* Look for a hole in the numbers. */
3471 link = &ipmi_interfaces;
3472 list_for_each_entry_rcu(tintf, &ipmi_interfaces, link) {
3473 if (tintf->intf_num != i) {
3474 link = &tintf->link;
3479 /* Add the new interface in numeric order. */
3481 list_add_rcu(&intf->link, &ipmi_interfaces);
3483 list_add_tail_rcu(&intf->link, link);
3485 rv = handlers->start_processing(send_info, intf);
3489 rv = __bmc_get_device_id(intf, NULL, &id, NULL, NULL, i);
3491 dev_err(si_dev, "Unable to get the device id: %d\n", rv);
3492 goto out_err_started;
3495 mutex_lock(&intf->bmc_reg_mutex);
3496 rv = __scan_channels(intf, &id);
3497 mutex_unlock(&intf->bmc_reg_mutex);
3499 goto out_err_bmc_reg;
3502 * Keep memory order straight for RCU readers. Make
3503 * sure everything else is committed to memory before
3504 * setting intf_num to mark the interface valid.
3508 mutex_unlock(&ipmi_interfaces_mutex);
3510 /* After this point the interface is legal to use. */
3511 call_smi_watchers(i, intf->si_dev);
3516 ipmi_bmc_unregister(intf);
3518 if (intf->handlers->shutdown)
3519 intf->handlers->shutdown(intf->send_info);
3521 list_del_rcu(&intf->link);
3522 mutex_unlock(&ipmi_interfaces_mutex);
3523 synchronize_srcu(&ipmi_interfaces_srcu);
3524 cleanup_srcu_struct(&intf->users_srcu);
3525 kref_put(&intf->refcount, intf_free);
3529 EXPORT_SYMBOL(ipmi_add_smi);
3531 static void deliver_smi_err_response(struct ipmi_smi *intf,
3532 struct ipmi_smi_msg *msg,
3535 msg->rsp[0] = msg->data[0] | 4;
3536 msg->rsp[1] = msg->data[1];
3539 /* It's an error, so it will never requeue, no need to check return. */
3540 handle_one_recv_msg(intf, msg);
3543 static void cleanup_smi_msgs(struct ipmi_smi *intf)
3546 struct seq_table *ent;
3547 struct ipmi_smi_msg *msg;
3548 struct list_head *entry;
3549 struct list_head tmplist;
3551 /* Clear out our transmit queues and hold the messages. */
3552 INIT_LIST_HEAD(&tmplist);
3553 list_splice_tail(&intf->hp_xmit_msgs, &tmplist);
3554 list_splice_tail(&intf->xmit_msgs, &tmplist);
3556 /* Current message first, to preserve order */
3557 while (intf->curr_msg && !list_empty(&intf->waiting_rcv_msgs)) {
3558 /* Wait for the message to clear out. */
3559 schedule_timeout(1);
3562 /* No need for locks, the interface is down. */
3565 * Return errors for all pending messages in queue and in the
3566 * tables waiting for remote responses.
3568 while (!list_empty(&tmplist)) {
3569 entry = tmplist.next;
3571 msg = list_entry(entry, struct ipmi_smi_msg, link);
3572 deliver_smi_err_response(intf, msg, IPMI_ERR_UNSPECIFIED);
3575 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
3576 ent = &intf->seq_table[i];
3579 deliver_err_response(intf, ent->recv_msg, IPMI_ERR_UNSPECIFIED);
3583 void ipmi_unregister_smi(struct ipmi_smi *intf)
3585 struct ipmi_smi_watcher *w;
3586 int intf_num = intf->intf_num, index;
3588 mutex_lock(&ipmi_interfaces_mutex);
3589 intf->intf_num = -1;
3590 intf->in_shutdown = true;
3591 list_del_rcu(&intf->link);
3592 mutex_unlock(&ipmi_interfaces_mutex);
3593 synchronize_srcu(&ipmi_interfaces_srcu);
3595 /* At this point no users can be added to the interface. */
3598 * Call all the watcher interfaces to tell them that
3599 * an interface is going away.
3601 mutex_lock(&smi_watchers_mutex);
3602 list_for_each_entry(w, &smi_watchers, link)
3603 w->smi_gone(intf_num);
3604 mutex_unlock(&smi_watchers_mutex);
3606 index = srcu_read_lock(&intf->users_srcu);
3607 while (!list_empty(&intf->users)) {
3608 struct ipmi_user *user =
3609 container_of(list_next_rcu(&intf->users),
3610 struct ipmi_user, link);
3612 _ipmi_destroy_user(user);
3614 srcu_read_unlock(&intf->users_srcu, index);
3616 if (intf->handlers->shutdown)
3617 intf->handlers->shutdown(intf->send_info);
3619 cleanup_smi_msgs(intf);
3621 ipmi_bmc_unregister(intf);
3623 cleanup_srcu_struct(&intf->users_srcu);
3624 kref_put(&intf->refcount, intf_free);
3626 EXPORT_SYMBOL(ipmi_unregister_smi);
3628 static int handle_ipmb_get_msg_rsp(struct ipmi_smi *intf,
3629 struct ipmi_smi_msg *msg)
3631 struct ipmi_ipmb_addr ipmb_addr;
3632 struct ipmi_recv_msg *recv_msg;
3635 * This is 11, not 10, because the response must contain a
3638 if (msg->rsp_size < 11) {
3639 /* Message not big enough, just ignore it. */
3640 ipmi_inc_stat(intf, invalid_ipmb_responses);
3644 if (msg->rsp[2] != 0) {
3645 /* An error getting the response, just ignore it. */
3649 ipmb_addr.addr_type = IPMI_IPMB_ADDR_TYPE;
3650 ipmb_addr.slave_addr = msg->rsp[6];
3651 ipmb_addr.channel = msg->rsp[3] & 0x0f;
3652 ipmb_addr.lun = msg->rsp[7] & 3;
3655 * It's a response from a remote entity. Look up the sequence
3656 * number and handle the response.
3658 if (intf_find_seq(intf,
3662 (msg->rsp[4] >> 2) & (~1),
3663 (struct ipmi_addr *) &ipmb_addr,
3666 * We were unable to find the sequence number,
3667 * so just nuke the message.
3669 ipmi_inc_stat(intf, unhandled_ipmb_responses);
3673 memcpy(recv_msg->msg_data, &msg->rsp[9], msg->rsp_size - 9);
3675 * The other fields matched, so no need to set them, except
3676 * for netfn, which needs to be the response that was
3677 * returned, not the request value.
3679 recv_msg->msg.netfn = msg->rsp[4] >> 2;
3680 recv_msg->msg.data = recv_msg->msg_data;
3681 recv_msg->msg.data_len = msg->rsp_size - 10;
3682 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
3683 if (deliver_response(intf, recv_msg))
3684 ipmi_inc_stat(intf, unhandled_ipmb_responses);
3686 ipmi_inc_stat(intf, handled_ipmb_responses);
3691 static int handle_ipmb_get_msg_cmd(struct ipmi_smi *intf,
3692 struct ipmi_smi_msg *msg)
3694 struct cmd_rcvr *rcvr;
3696 unsigned char netfn;
3699 struct ipmi_user *user = NULL;
3700 struct ipmi_ipmb_addr *ipmb_addr;
3701 struct ipmi_recv_msg *recv_msg;
3703 if (msg->rsp_size < 10) {
3704 /* Message not big enough, just ignore it. */
3705 ipmi_inc_stat(intf, invalid_commands);
3709 if (msg->rsp[2] != 0) {
3710 /* An error getting the response, just ignore it. */
3714 netfn = msg->rsp[4] >> 2;
3716 chan = msg->rsp[3] & 0xf;
3719 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
3722 kref_get(&user->refcount);
3728 /* We didn't find a user, deliver an error response. */
3729 ipmi_inc_stat(intf, unhandled_commands);
3731 msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
3732 msg->data[1] = IPMI_SEND_MSG_CMD;
3733 msg->data[2] = msg->rsp[3];
3734 msg->data[3] = msg->rsp[6];
3735 msg->data[4] = ((netfn + 1) << 2) | (msg->rsp[7] & 0x3);
3736 msg->data[5] = ipmb_checksum(&msg->data[3], 2);
3737 msg->data[6] = intf->addrinfo[msg->rsp[3] & 0xf].address;
3739 msg->data[7] = (msg->rsp[7] & 0xfc) | (msg->rsp[4] & 0x3);
3740 msg->data[8] = msg->rsp[8]; /* cmd */
3741 msg->data[9] = IPMI_INVALID_CMD_COMPLETION_CODE;
3742 msg->data[10] = ipmb_checksum(&msg->data[6], 4);
3743 msg->data_size = 11;
3745 ipmi_debug_msg("Invalid command:", msg->data, msg->data_size);
3748 if (!intf->in_shutdown) {
3749 smi_send(intf, intf->handlers, msg, 0);
3751 * We used the message, so return the value
3752 * that causes it to not be freed or
3759 recv_msg = ipmi_alloc_recv_msg();
3762 * We couldn't allocate memory for the
3763 * message, so requeue it for handling
3767 kref_put(&user->refcount, free_user);
3769 /* Extract the source address from the data. */
3770 ipmb_addr = (struct ipmi_ipmb_addr *) &recv_msg->addr;
3771 ipmb_addr->addr_type = IPMI_IPMB_ADDR_TYPE;
3772 ipmb_addr->slave_addr = msg->rsp[6];
3773 ipmb_addr->lun = msg->rsp[7] & 3;
3774 ipmb_addr->channel = msg->rsp[3] & 0xf;
3777 * Extract the rest of the message information
3778 * from the IPMB header.
3780 recv_msg->user = user;
3781 recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
3782 recv_msg->msgid = msg->rsp[7] >> 2;
3783 recv_msg->msg.netfn = msg->rsp[4] >> 2;
3784 recv_msg->msg.cmd = msg->rsp[8];
3785 recv_msg->msg.data = recv_msg->msg_data;
3788 * We chop off 10, not 9 bytes because the checksum
3789 * at the end also needs to be removed.
3791 recv_msg->msg.data_len = msg->rsp_size - 10;
3792 memcpy(recv_msg->msg_data, &msg->rsp[9],
3793 msg->rsp_size - 10);
3794 if (deliver_response(intf, recv_msg))
3795 ipmi_inc_stat(intf, unhandled_commands);
3797 ipmi_inc_stat(intf, handled_commands);
3804 static int handle_lan_get_msg_rsp(struct ipmi_smi *intf,
3805 struct ipmi_smi_msg *msg)
3807 struct ipmi_lan_addr lan_addr;
3808 struct ipmi_recv_msg *recv_msg;
3812 * This is 13, not 12, because the response must contain a
3815 if (msg->rsp_size < 13) {
3816 /* Message not big enough, just ignore it. */
3817 ipmi_inc_stat(intf, invalid_lan_responses);
3821 if (msg->rsp[2] != 0) {
3822 /* An error getting the response, just ignore it. */
3826 lan_addr.addr_type = IPMI_LAN_ADDR_TYPE;
3827 lan_addr.session_handle = msg->rsp[4];
3828 lan_addr.remote_SWID = msg->rsp[8];
3829 lan_addr.local_SWID = msg->rsp[5];
3830 lan_addr.channel = msg->rsp[3] & 0x0f;
3831 lan_addr.privilege = msg->rsp[3] >> 4;
3832 lan_addr.lun = msg->rsp[9] & 3;
3835 * It's a response from a remote entity. Look up the sequence
3836 * number and handle the response.
3838 if (intf_find_seq(intf,
3842 (msg->rsp[6] >> 2) & (~1),
3843 (struct ipmi_addr *) &lan_addr,
3846 * We were unable to find the sequence number,
3847 * so just nuke the message.
3849 ipmi_inc_stat(intf, unhandled_lan_responses);
3853 memcpy(recv_msg->msg_data, &msg->rsp[11], msg->rsp_size - 11);
3855 * The other fields matched, so no need to set them, except
3856 * for netfn, which needs to be the response that was
3857 * returned, not the request value.
3859 recv_msg->msg.netfn = msg->rsp[6] >> 2;
3860 recv_msg->msg.data = recv_msg->msg_data;
3861 recv_msg->msg.data_len = msg->rsp_size - 12;
3862 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
3863 if (deliver_response(intf, recv_msg))
3864 ipmi_inc_stat(intf, unhandled_lan_responses);
3866 ipmi_inc_stat(intf, handled_lan_responses);
3871 static int handle_lan_get_msg_cmd(struct ipmi_smi *intf,
3872 struct ipmi_smi_msg *msg)
3874 struct cmd_rcvr *rcvr;
3876 unsigned char netfn;
3879 struct ipmi_user *user = NULL;
3880 struct ipmi_lan_addr *lan_addr;
3881 struct ipmi_recv_msg *recv_msg;
3883 if (msg->rsp_size < 12) {
3884 /* Message not big enough, just ignore it. */
3885 ipmi_inc_stat(intf, invalid_commands);
3889 if (msg->rsp[2] != 0) {
3890 /* An error getting the response, just ignore it. */
3894 netfn = msg->rsp[6] >> 2;
3896 chan = msg->rsp[3] & 0xf;
3899 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
3902 kref_get(&user->refcount);
3908 /* We didn't find a user, just give up. */
3909 ipmi_inc_stat(intf, unhandled_commands);
3912 * Don't do anything with these messages, just allow
3917 recv_msg = ipmi_alloc_recv_msg();
3920 * We couldn't allocate memory for the
3921 * message, so requeue it for handling later.
3924 kref_put(&user->refcount, free_user);
3926 /* Extract the source address from the data. */
3927 lan_addr = (struct ipmi_lan_addr *) &recv_msg->addr;
3928 lan_addr->addr_type = IPMI_LAN_ADDR_TYPE;
3929 lan_addr->session_handle = msg->rsp[4];
3930 lan_addr->remote_SWID = msg->rsp[8];
3931 lan_addr->local_SWID = msg->rsp[5];
3932 lan_addr->lun = msg->rsp[9] & 3;
3933 lan_addr->channel = msg->rsp[3] & 0xf;
3934 lan_addr->privilege = msg->rsp[3] >> 4;
3937 * Extract the rest of the message information
3938 * from the IPMB header.
3940 recv_msg->user = user;
3941 recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
3942 recv_msg->msgid = msg->rsp[9] >> 2;
3943 recv_msg->msg.netfn = msg->rsp[6] >> 2;
3944 recv_msg->msg.cmd = msg->rsp[10];
3945 recv_msg->msg.data = recv_msg->msg_data;
3948 * We chop off 12, not 11 bytes because the checksum
3949 * at the end also needs to be removed.
3951 recv_msg->msg.data_len = msg->rsp_size - 12;
3952 memcpy(recv_msg->msg_data, &msg->rsp[11],
3953 msg->rsp_size - 12);
3954 if (deliver_response(intf, recv_msg))
3955 ipmi_inc_stat(intf, unhandled_commands);
3957 ipmi_inc_stat(intf, handled_commands);
3965 * This routine will handle "Get Message" command responses with
3966 * channels that use an OEM Medium. The message format belongs to
3967 * the OEM. See IPMI 2.0 specification, Chapter 6 and
3968 * Chapter 22, sections 22.6 and 22.24 for more details.
3970 static int handle_oem_get_msg_cmd(struct ipmi_smi *intf,
3971 struct ipmi_smi_msg *msg)
3973 struct cmd_rcvr *rcvr;
3975 unsigned char netfn;
3978 struct ipmi_user *user = NULL;
3979 struct ipmi_system_interface_addr *smi_addr;
3980 struct ipmi_recv_msg *recv_msg;
3983 * We expect the OEM SW to perform error checking
3984 * so we just do some basic sanity checks
3986 if (msg->rsp_size < 4) {
3987 /* Message not big enough, just ignore it. */
3988 ipmi_inc_stat(intf, invalid_commands);
3992 if (msg->rsp[2] != 0) {
3993 /* An error getting the response, just ignore it. */
3998 * This is an OEM Message so the OEM needs to know how
3999 * handle the message. We do no interpretation.
4001 netfn = msg->rsp[0] >> 2;
4003 chan = msg->rsp[3] & 0xf;
4006 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
4009 kref_get(&user->refcount);
4015 /* We didn't find a user, just give up. */
4016 ipmi_inc_stat(intf, unhandled_commands);
4019 * Don't do anything with these messages, just allow
4025 recv_msg = ipmi_alloc_recv_msg();
4028 * We couldn't allocate memory for the
4029 * message, so requeue it for handling
4033 kref_put(&user->refcount, free_user);
4036 * OEM Messages are expected to be delivered via
4037 * the system interface to SMS software. We might
4038 * need to visit this again depending on OEM
4041 smi_addr = ((struct ipmi_system_interface_addr *)
4043 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
4044 smi_addr->channel = IPMI_BMC_CHANNEL;
4045 smi_addr->lun = msg->rsp[0] & 3;
4047 recv_msg->user = user;
4048 recv_msg->user_msg_data = NULL;
4049 recv_msg->recv_type = IPMI_OEM_RECV_TYPE;
4050 recv_msg->msg.netfn = msg->rsp[0] >> 2;
4051 recv_msg->msg.cmd = msg->rsp[1];
4052 recv_msg->msg.data = recv_msg->msg_data;
4055 * The message starts at byte 4 which follows the
4056 * the Channel Byte in the "GET MESSAGE" command
4058 recv_msg->msg.data_len = msg->rsp_size - 4;
4059 memcpy(recv_msg->msg_data, &msg->rsp[4],
4061 if (deliver_response(intf, recv_msg))
4062 ipmi_inc_stat(intf, unhandled_commands);
4064 ipmi_inc_stat(intf, handled_commands);
4071 static void copy_event_into_recv_msg(struct ipmi_recv_msg *recv_msg,
4072 struct ipmi_smi_msg *msg)
4074 struct ipmi_system_interface_addr *smi_addr;
4076 recv_msg->msgid = 0;
4077 smi_addr = (struct ipmi_system_interface_addr *) &recv_msg->addr;
4078 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
4079 smi_addr->channel = IPMI_BMC_CHANNEL;
4080 smi_addr->lun = msg->rsp[0] & 3;
4081 recv_msg->recv_type = IPMI_ASYNC_EVENT_RECV_TYPE;
4082 recv_msg->msg.netfn = msg->rsp[0] >> 2;
4083 recv_msg->msg.cmd = msg->rsp[1];
4084 memcpy(recv_msg->msg_data, &msg->rsp[3], msg->rsp_size - 3);
4085 recv_msg->msg.data = recv_msg->msg_data;
4086 recv_msg->msg.data_len = msg->rsp_size - 3;
4089 static int handle_read_event_rsp(struct ipmi_smi *intf,
4090 struct ipmi_smi_msg *msg)
4092 struct ipmi_recv_msg *recv_msg, *recv_msg2;
4093 struct list_head msgs;
4094 struct ipmi_user *user;
4095 int rv = 0, deliver_count = 0, index;
4096 unsigned long flags;
4098 if (msg->rsp_size < 19) {
4099 /* Message is too small to be an IPMB event. */
4100 ipmi_inc_stat(intf, invalid_events);
4104 if (msg->rsp[2] != 0) {
4105 /* An error getting the event, just ignore it. */
4109 INIT_LIST_HEAD(&msgs);
4111 spin_lock_irqsave(&intf->events_lock, flags);
4113 ipmi_inc_stat(intf, events);
4116 * Allocate and fill in one message for every user that is
4119 index = srcu_read_lock(&intf->users_srcu);
4120 list_for_each_entry_rcu(user, &intf->users, link) {
4121 if (!user->gets_events)
4124 recv_msg = ipmi_alloc_recv_msg();
4127 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs,
4129 list_del(&recv_msg->link);
4130 ipmi_free_recv_msg(recv_msg);
4133 * We couldn't allocate memory for the
4134 * message, so requeue it for handling
4143 copy_event_into_recv_msg(recv_msg, msg);
4144 recv_msg->user = user;
4145 kref_get(&user->refcount);
4146 list_add_tail(&recv_msg->link, &msgs);
4148 srcu_read_unlock(&intf->users_srcu, index);
4150 if (deliver_count) {
4151 /* Now deliver all the messages. */
4152 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs, link) {
4153 list_del(&recv_msg->link);
4154 deliver_local_response(intf, recv_msg);
4156 } else if (intf->waiting_events_count < MAX_EVENTS_IN_QUEUE) {
4158 * No one to receive the message, put it in queue if there's
4159 * not already too many things in the queue.
4161 recv_msg = ipmi_alloc_recv_msg();
4164 * We couldn't allocate memory for the
4165 * message, so requeue it for handling
4172 copy_event_into_recv_msg(recv_msg, msg);
4173 list_add_tail(&recv_msg->link, &intf->waiting_events);
4174 intf->waiting_events_count++;
4175 } else if (!intf->event_msg_printed) {
4177 * There's too many things in the queue, discard this
4180 dev_warn(intf->si_dev,
4181 "Event queue full, discarding incoming events\n");
4182 intf->event_msg_printed = 1;
4186 spin_unlock_irqrestore(&intf->events_lock, flags);
4191 static int handle_bmc_rsp(struct ipmi_smi *intf,
4192 struct ipmi_smi_msg *msg)
4194 struct ipmi_recv_msg *recv_msg;
4195 struct ipmi_system_interface_addr *smi_addr;
4197 recv_msg = (struct ipmi_recv_msg *) msg->user_data;
4198 if (recv_msg == NULL) {
4199 dev_warn(intf->si_dev,
4200 "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");
4204 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
4205 recv_msg->msgid = msg->msgid;
4206 smi_addr = ((struct ipmi_system_interface_addr *)
4208 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
4209 smi_addr->channel = IPMI_BMC_CHANNEL;
4210 smi_addr->lun = msg->rsp[0] & 3;
4211 recv_msg->msg.netfn = msg->rsp[0] >> 2;
4212 recv_msg->msg.cmd = msg->rsp[1];
4213 memcpy(recv_msg->msg_data, &msg->rsp[2], msg->rsp_size - 2);
4214 recv_msg->msg.data = recv_msg->msg_data;
4215 recv_msg->msg.data_len = msg->rsp_size - 2;
4216 deliver_local_response(intf, recv_msg);
4222 * Handle a received message. Return 1 if the message should be requeued,
4223 * 0 if the message should be freed, or -1 if the message should not
4224 * be freed or requeued.
4226 static int handle_one_recv_msg(struct ipmi_smi *intf,
4227 struct ipmi_smi_msg *msg)
4232 ipmi_debug_msg("Recv:", msg->rsp, msg->rsp_size);
4234 if ((msg->data_size >= 2)
4235 && (msg->data[0] == (IPMI_NETFN_APP_REQUEST << 2))
4236 && (msg->data[1] == IPMI_SEND_MSG_CMD)
4237 && (msg->user_data == NULL)) {
4239 if (intf->in_shutdown)
4243 * This is the local response to a command send, start
4244 * the timer for these. The user_data will not be
4245 * NULL if this is a response send, and we will let
4246 * response sends just go through.
4250 * Check for errors, if we get certain errors (ones
4251 * that mean basically we can try again later), we
4252 * ignore them and start the timer. Otherwise we
4253 * report the error immediately.
4255 if ((msg->rsp_size >= 3) && (msg->rsp[2] != 0)
4256 && (msg->rsp[2] != IPMI_NODE_BUSY_ERR)
4257 && (msg->rsp[2] != IPMI_LOST_ARBITRATION_ERR)
4258 && (msg->rsp[2] != IPMI_BUS_ERR)
4259 && (msg->rsp[2] != IPMI_NAK_ON_WRITE_ERR)) {
4260 int ch = msg->rsp[3] & 0xf;
4261 struct ipmi_channel *chans;
4263 /* Got an error sending the message, handle it. */
4265 chans = READ_ONCE(intf->channel_list)->c;
4266 if ((chans[ch].medium == IPMI_CHANNEL_MEDIUM_8023LAN)
4267 || (chans[ch].medium == IPMI_CHANNEL_MEDIUM_ASYNC))
4268 ipmi_inc_stat(intf, sent_lan_command_errs);
4270 ipmi_inc_stat(intf, sent_ipmb_command_errs);
4271 intf_err_seq(intf, msg->msgid, msg->rsp[2]);
4273 /* The message was sent, start the timer. */
4274 intf_start_seq_timer(intf, msg->msgid);
4279 } else if (msg->rsp_size < 2) {
4280 /* Message is too small to be correct. */
4281 dev_warn(intf->si_dev,
4282 "BMC returned too small a message for netfn %x cmd %x, got %d bytes\n",
4283 (msg->data[0] >> 2) | 1, msg->data[1], msg->rsp_size);
4285 /* Generate an error response for the message. */
4286 msg->rsp[0] = msg->data[0] | (1 << 2);
4287 msg->rsp[1] = msg->data[1];
4288 msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
4290 } else if (((msg->rsp[0] >> 2) != ((msg->data[0] >> 2) | 1))
4291 || (msg->rsp[1] != msg->data[1])) {
4293 * The NetFN and Command in the response is not even
4294 * marginally correct.
4296 dev_warn(intf->si_dev,
4297 "BMC returned incorrect response, expected netfn %x cmd %x, got netfn %x cmd %x\n",
4298 (msg->data[0] >> 2) | 1, msg->data[1],
4299 msg->rsp[0] >> 2, msg->rsp[1]);
4301 /* Generate an error response for the message. */
4302 msg->rsp[0] = msg->data[0] | (1 << 2);
4303 msg->rsp[1] = msg->data[1];
4304 msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
4308 if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
4309 && (msg->rsp[1] == IPMI_SEND_MSG_CMD)
4310 && (msg->user_data != NULL)) {
4312 * It's a response to a response we sent. For this we
4313 * deliver a send message response to the user.
4315 struct ipmi_recv_msg *recv_msg = msg->user_data;
4318 if (msg->rsp_size < 2)
4319 /* Message is too small to be correct. */
4322 chan = msg->data[2] & 0x0f;
4323 if (chan >= IPMI_MAX_CHANNELS)
4324 /* Invalid channel number */
4330 recv_msg->recv_type = IPMI_RESPONSE_RESPONSE_TYPE;
4331 recv_msg->msg.data = recv_msg->msg_data;
4332 recv_msg->msg.data_len = 1;
4333 recv_msg->msg_data[0] = msg->rsp[2];
4334 deliver_local_response(intf, recv_msg);
4335 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
4336 && (msg->rsp[1] == IPMI_GET_MSG_CMD)) {
4337 struct ipmi_channel *chans;
4339 /* It's from the receive queue. */
4340 chan = msg->rsp[3] & 0xf;
4341 if (chan >= IPMI_MAX_CHANNELS) {
4342 /* Invalid channel number */
4348 * We need to make sure the channels have been initialized.
4349 * The channel_handler routine will set the "curr_channel"
4350 * equal to or greater than IPMI_MAX_CHANNELS when all the
4351 * channels for this interface have been initialized.
4353 if (!intf->channels_ready) {
4354 requeue = 0; /* Throw the message away */
4358 chans = READ_ONCE(intf->channel_list)->c;
4360 switch (chans[chan].medium) {
4361 case IPMI_CHANNEL_MEDIUM_IPMB:
4362 if (msg->rsp[4] & 0x04) {
4364 * It's a response, so find the
4365 * requesting message and send it up.
4367 requeue = handle_ipmb_get_msg_rsp(intf, msg);
4370 * It's a command to the SMS from some other
4371 * entity. Handle that.
4373 requeue = handle_ipmb_get_msg_cmd(intf, msg);
4377 case IPMI_CHANNEL_MEDIUM_8023LAN:
4378 case IPMI_CHANNEL_MEDIUM_ASYNC:
4379 if (msg->rsp[6] & 0x04) {
4381 * It's a response, so find the
4382 * requesting message and send it up.
4384 requeue = handle_lan_get_msg_rsp(intf, msg);
4387 * It's a command to the SMS from some other
4388 * entity. Handle that.
4390 requeue = handle_lan_get_msg_cmd(intf, msg);
4395 /* Check for OEM Channels. Clients had better
4396 register for these commands. */
4397 if ((chans[chan].medium >= IPMI_CHANNEL_MEDIUM_OEM_MIN)
4398 && (chans[chan].medium
4399 <= IPMI_CHANNEL_MEDIUM_OEM_MAX)) {
4400 requeue = handle_oem_get_msg_cmd(intf, msg);
4403 * We don't handle the channel type, so just
4410 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
4411 && (msg->rsp[1] == IPMI_READ_EVENT_MSG_BUFFER_CMD)) {
4412 /* It's an asynchronous event. */
4413 requeue = handle_read_event_rsp(intf, msg);
4415 /* It's a response from the local BMC. */
4416 requeue = handle_bmc_rsp(intf, msg);
4424 * If there are messages in the queue or pretimeouts, handle them.
4426 static void handle_new_recv_msgs(struct ipmi_smi *intf)
4428 struct ipmi_smi_msg *smi_msg;
4429 unsigned long flags = 0;
4431 int run_to_completion = intf->run_to_completion;
4433 /* See if any waiting messages need to be processed. */
4434 if (!run_to_completion)
4435 spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags);
4436 while (!list_empty(&intf->waiting_rcv_msgs)) {
4437 smi_msg = list_entry(intf->waiting_rcv_msgs.next,
4438 struct ipmi_smi_msg, link);
4439 list_del(&smi_msg->link);
4440 if (!run_to_completion)
4441 spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock,
4443 rv = handle_one_recv_msg(intf, smi_msg);
4444 if (!run_to_completion)
4445 spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags);
4448 * To preserve message order, quit if we
4449 * can't handle a message. Add the message
4450 * back at the head, this is safe because this
4451 * tasklet is the only thing that pulls the
4454 list_add(&smi_msg->link, &intf->waiting_rcv_msgs);
4458 /* Message handled */
4459 ipmi_free_smi_msg(smi_msg);
4460 /* If rv < 0, fatal error, del but don't free. */
4463 if (!run_to_completion)
4464 spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock, flags);
4467 * If the pretimout count is non-zero, decrement one from it and
4468 * deliver pretimeouts to all the users.
4470 if (atomic_add_unless(&intf->watchdog_pretimeouts_to_deliver, -1, 0)) {
4471 struct ipmi_user *user;
4474 index = srcu_read_lock(&intf->users_srcu);
4475 list_for_each_entry_rcu(user, &intf->users, link) {
4476 if (user->handler->ipmi_watchdog_pretimeout)
4477 user->handler->ipmi_watchdog_pretimeout(
4478 user->handler_data);
4480 srcu_read_unlock(&intf->users_srcu, index);
4484 static void smi_recv_tasklet(unsigned long val)
4486 unsigned long flags = 0; /* keep us warning-free. */
4487 struct ipmi_smi *intf = (struct ipmi_smi *) val;
4488 int run_to_completion = intf->run_to_completion;
4489 struct ipmi_smi_msg *newmsg = NULL;
4492 * Start the next message if available.
4494 * Do this here, not in the actual receiver, because we may deadlock
4495 * because the lower layer is allowed to hold locks while calling
4501 if (!run_to_completion)
4502 spin_lock_irqsave(&intf->xmit_msgs_lock, flags);
4503 if (intf->curr_msg == NULL && !intf->in_shutdown) {
4504 struct list_head *entry = NULL;
4506 /* Pick the high priority queue first. */
4507 if (!list_empty(&intf->hp_xmit_msgs))
4508 entry = intf->hp_xmit_msgs.next;
4509 else if (!list_empty(&intf->xmit_msgs))
4510 entry = intf->xmit_msgs.next;
4514 newmsg = list_entry(entry, struct ipmi_smi_msg, link);
4515 intf->curr_msg = newmsg;
4519 if (!run_to_completion)
4520 spin_unlock_irqrestore(&intf->xmit_msgs_lock, flags);
4522 intf->handlers->sender(intf->send_info, newmsg);
4526 handle_new_recv_msgs(intf);
4529 /* Handle a new message from the lower layer. */
4530 void ipmi_smi_msg_received(struct ipmi_smi *intf,
4531 struct ipmi_smi_msg *msg)
4533 unsigned long flags = 0; /* keep us warning-free. */
4534 int run_to_completion = intf->run_to_completion;
4537 * To preserve message order, we keep a queue and deliver from
4540 if (!run_to_completion)
4541 spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags);
4542 list_add_tail(&msg->link, &intf->waiting_rcv_msgs);
4543 if (!run_to_completion)
4544 spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock,
4547 if (!run_to_completion)
4548 spin_lock_irqsave(&intf->xmit_msgs_lock, flags);
4550 * We can get an asynchronous event or receive message in addition
4551 * to commands we send.
4553 if (msg == intf->curr_msg)
4554 intf->curr_msg = NULL;
4555 if (!run_to_completion)
4556 spin_unlock_irqrestore(&intf->xmit_msgs_lock, flags);
4558 if (run_to_completion)
4559 smi_recv_tasklet((unsigned long) intf);
4561 tasklet_schedule(&intf->recv_tasklet);
4563 EXPORT_SYMBOL(ipmi_smi_msg_received);
4565 void ipmi_smi_watchdog_pretimeout(struct ipmi_smi *intf)
4567 if (intf->in_shutdown)
4570 atomic_set(&intf->watchdog_pretimeouts_to_deliver, 1);
4571 tasklet_schedule(&intf->recv_tasklet);
4573 EXPORT_SYMBOL(ipmi_smi_watchdog_pretimeout);
4575 static struct ipmi_smi_msg *
4576 smi_from_recv_msg(struct ipmi_smi *intf, struct ipmi_recv_msg *recv_msg,
4577 unsigned char seq, long seqid)
4579 struct ipmi_smi_msg *smi_msg = ipmi_alloc_smi_msg();
4582 * If we can't allocate the message, then just return, we
4583 * get 4 retries, so this should be ok.
4587 memcpy(smi_msg->data, recv_msg->msg.data, recv_msg->msg.data_len);
4588 smi_msg->data_size = recv_msg->msg.data_len;
4589 smi_msg->msgid = STORE_SEQ_IN_MSGID(seq, seqid);
4591 ipmi_debug_msg("Resend: ", smi_msg->data, smi_msg->data_size);
4596 static void check_msg_timeout(struct ipmi_smi *intf, struct seq_table *ent,
4597 struct list_head *timeouts,
4598 unsigned long timeout_period,
4599 int slot, unsigned long *flags,
4602 struct ipmi_recv_msg *msg;
4604 if (intf->in_shutdown)
4610 if (timeout_period < ent->timeout) {
4611 ent->timeout -= timeout_period;
4616 if (ent->retries_left == 0) {
4617 /* The message has used all its retries. */
4619 smi_remove_watch(intf, IPMI_WATCH_MASK_CHECK_MESSAGES);
4620 msg = ent->recv_msg;
4621 list_add_tail(&msg->link, timeouts);
4623 ipmi_inc_stat(intf, timed_out_ipmb_broadcasts);
4624 else if (is_lan_addr(&ent->recv_msg->addr))
4625 ipmi_inc_stat(intf, timed_out_lan_commands);
4627 ipmi_inc_stat(intf, timed_out_ipmb_commands);
4629 struct ipmi_smi_msg *smi_msg;
4630 /* More retries, send again. */
4635 * Start with the max timer, set to normal timer after
4636 * the message is sent.
4638 ent->timeout = MAX_MSG_TIMEOUT;
4639 ent->retries_left--;
4640 smi_msg = smi_from_recv_msg(intf, ent->recv_msg, slot,
4643 if (is_lan_addr(&ent->recv_msg->addr))
4645 dropped_rexmit_lan_commands);
4648 dropped_rexmit_ipmb_commands);
4652 spin_unlock_irqrestore(&intf->seq_lock, *flags);
4655 * Send the new message. We send with a zero
4656 * priority. It timed out, I doubt time is that
4657 * critical now, and high priority messages are really
4658 * only for messages to the local MC, which don't get
4661 if (intf->handlers) {
4662 if (is_lan_addr(&ent->recv_msg->addr))
4664 retransmitted_lan_commands);
4667 retransmitted_ipmb_commands);
4669 smi_send(intf, intf->handlers, smi_msg, 0);
4671 ipmi_free_smi_msg(smi_msg);
4673 spin_lock_irqsave(&intf->seq_lock, *flags);
4677 static bool ipmi_timeout_handler(struct ipmi_smi *intf,
4678 unsigned long timeout_period)
4680 struct list_head timeouts;
4681 struct ipmi_recv_msg *msg, *msg2;
4682 unsigned long flags;
4684 bool need_timer = false;
4686 if (!intf->bmc_registered) {
4687 kref_get(&intf->refcount);
4688 if (!schedule_work(&intf->bmc_reg_work)) {
4689 kref_put(&intf->refcount, intf_free);
4695 * Go through the seq table and find any messages that
4696 * have timed out, putting them in the timeouts
4699 INIT_LIST_HEAD(&timeouts);
4700 spin_lock_irqsave(&intf->seq_lock, flags);
4701 if (intf->ipmb_maintenance_mode_timeout) {
4702 if (intf->ipmb_maintenance_mode_timeout <= timeout_period)
4703 intf->ipmb_maintenance_mode_timeout = 0;
4705 intf->ipmb_maintenance_mode_timeout -= timeout_period;
4707 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++)
4708 check_msg_timeout(intf, &intf->seq_table[i],
4709 &timeouts, timeout_period, i,
4710 &flags, &need_timer);
4711 spin_unlock_irqrestore(&intf->seq_lock, flags);
4713 list_for_each_entry_safe(msg, msg2, &timeouts, link)
4714 deliver_err_response(intf, msg, IPMI_TIMEOUT_COMPLETION_CODE);
4717 * Maintenance mode handling. Check the timeout
4718 * optimistically before we claim the lock. It may
4719 * mean a timeout gets missed occasionally, but that
4720 * only means the timeout gets extended by one period
4721 * in that case. No big deal, and it avoids the lock
4724 if (intf->auto_maintenance_timeout > 0) {
4725 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
4726 if (intf->auto_maintenance_timeout > 0) {
4727 intf->auto_maintenance_timeout
4729 if (!intf->maintenance_mode
4730 && (intf->auto_maintenance_timeout <= 0)) {
4731 intf->maintenance_mode_enable = false;
4732 maintenance_mode_update(intf);
4735 spin_unlock_irqrestore(&intf->maintenance_mode_lock,
4739 tasklet_schedule(&intf->recv_tasklet);
4744 static void ipmi_request_event(struct ipmi_smi *intf)
4746 /* No event requests when in maintenance mode. */
4747 if (intf->maintenance_mode_enable)
4750 if (!intf->in_shutdown)
4751 intf->handlers->request_events(intf->send_info);
4754 static struct timer_list ipmi_timer;
4756 static atomic_t stop_operation;
4758 static void ipmi_timeout(struct timer_list *unused)
4760 struct ipmi_smi *intf;
4761 bool need_timer = false;
4764 if (atomic_read(&stop_operation))
4767 index = srcu_read_lock(&ipmi_interfaces_srcu);
4768 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
4769 if (atomic_read(&intf->event_waiters)) {
4770 intf->ticks_to_req_ev--;
4771 if (intf->ticks_to_req_ev == 0) {
4772 ipmi_request_event(intf);
4773 intf->ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
4778 need_timer |= ipmi_timeout_handler(intf, IPMI_TIMEOUT_TIME);
4780 srcu_read_unlock(&ipmi_interfaces_srcu, index);
4783 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
4786 static void need_waiter(struct ipmi_smi *intf)
4788 /* Racy, but worst case we start the timer twice. */
4789 if (!timer_pending(&ipmi_timer))
4790 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
4793 static atomic_t smi_msg_inuse_count = ATOMIC_INIT(0);
4794 static atomic_t recv_msg_inuse_count = ATOMIC_INIT(0);
4796 static void free_smi_msg(struct ipmi_smi_msg *msg)
4798 atomic_dec(&smi_msg_inuse_count);
4802 struct ipmi_smi_msg *ipmi_alloc_smi_msg(void)
4804 struct ipmi_smi_msg *rv;
4805 rv = kmalloc(sizeof(struct ipmi_smi_msg), GFP_ATOMIC);
4807 rv->done = free_smi_msg;
4808 rv->user_data = NULL;
4809 atomic_inc(&smi_msg_inuse_count);
4813 EXPORT_SYMBOL(ipmi_alloc_smi_msg);
4815 static void free_recv_msg(struct ipmi_recv_msg *msg)
4817 atomic_dec(&recv_msg_inuse_count);
4821 static struct ipmi_recv_msg *ipmi_alloc_recv_msg(void)
4823 struct ipmi_recv_msg *rv;
4825 rv = kmalloc(sizeof(struct ipmi_recv_msg), GFP_ATOMIC);
4828 rv->done = free_recv_msg;
4829 atomic_inc(&recv_msg_inuse_count);
4834 void ipmi_free_recv_msg(struct ipmi_recv_msg *msg)
4837 kref_put(&msg->user->refcount, free_user);
4840 EXPORT_SYMBOL(ipmi_free_recv_msg);
4842 static atomic_t panic_done_count = ATOMIC_INIT(0);
4844 static void dummy_smi_done_handler(struct ipmi_smi_msg *msg)
4846 atomic_dec(&panic_done_count);
4849 static void dummy_recv_done_handler(struct ipmi_recv_msg *msg)
4851 atomic_dec(&panic_done_count);
4855 * Inside a panic, send a message and wait for a response.
4857 static void ipmi_panic_request_and_wait(struct ipmi_smi *intf,
4858 struct ipmi_addr *addr,
4859 struct kernel_ipmi_msg *msg)
4861 struct ipmi_smi_msg smi_msg;
4862 struct ipmi_recv_msg recv_msg;
4865 smi_msg.done = dummy_smi_done_handler;
4866 recv_msg.done = dummy_recv_done_handler;
4867 atomic_add(2, &panic_done_count);
4868 rv = i_ipmi_request(NULL,
4877 intf->addrinfo[0].address,
4878 intf->addrinfo[0].lun,
4879 0, 1); /* Don't retry, and don't wait. */
4881 atomic_sub(2, &panic_done_count);
4882 else if (intf->handlers->flush_messages)
4883 intf->handlers->flush_messages(intf->send_info);
4885 while (atomic_read(&panic_done_count) != 0)
4889 static void event_receiver_fetcher(struct ipmi_smi *intf,
4890 struct ipmi_recv_msg *msg)
4892 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
4893 && (msg->msg.netfn == IPMI_NETFN_SENSOR_EVENT_RESPONSE)
4894 && (msg->msg.cmd == IPMI_GET_EVENT_RECEIVER_CMD)
4895 && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) {
4896 /* A get event receiver command, save it. */
4897 intf->event_receiver = msg->msg.data[1];
4898 intf->event_receiver_lun = msg->msg.data[2] & 0x3;
4902 static void device_id_fetcher(struct ipmi_smi *intf, struct ipmi_recv_msg *msg)
4904 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
4905 && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
4906 && (msg->msg.cmd == IPMI_GET_DEVICE_ID_CMD)
4907 && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) {
4909 * A get device id command, save if we are an event
4910 * receiver or generator.
4912 intf->local_sel_device = (msg->msg.data[6] >> 2) & 1;
4913 intf->local_event_generator = (msg->msg.data[6] >> 5) & 1;
4917 static void send_panic_events(struct ipmi_smi *intf, char *str)
4919 struct kernel_ipmi_msg msg;
4920 unsigned char data[16];
4921 struct ipmi_system_interface_addr *si;
4922 struct ipmi_addr addr;
4924 struct ipmi_ipmb_addr *ipmb;
4927 if (ipmi_send_panic_event == IPMI_SEND_PANIC_EVENT_NONE)
4930 si = (struct ipmi_system_interface_addr *) &addr;
4931 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
4932 si->channel = IPMI_BMC_CHANNEL;
4935 /* Fill in an event telling that we have failed. */
4936 msg.netfn = 0x04; /* Sensor or Event. */
4937 msg.cmd = 2; /* Platform event command. */
4940 data[0] = 0x41; /* Kernel generator ID, IPMI table 5-4 */
4941 data[1] = 0x03; /* This is for IPMI 1.0. */
4942 data[2] = 0x20; /* OS Critical Stop, IPMI table 36-3 */
4943 data[4] = 0x6f; /* Sensor specific, IPMI table 36-1 */
4944 data[5] = 0xa1; /* Runtime stop OEM bytes 2 & 3. */
4947 * Put a few breadcrumbs in. Hopefully later we can add more things
4948 * to make the panic events more useful.
4956 /* Send the event announcing the panic. */
4957 ipmi_panic_request_and_wait(intf, &addr, &msg);
4960 * On every interface, dump a bunch of OEM event holding the
4963 if (ipmi_send_panic_event != IPMI_SEND_PANIC_EVENT_STRING || !str)
4967 * intf_num is used as an marker to tell if the
4968 * interface is valid. Thus we need a read barrier to
4969 * make sure data fetched before checking intf_num
4975 * First job here is to figure out where to send the
4976 * OEM events. There's no way in IPMI to send OEM
4977 * events using an event send command, so we have to
4978 * find the SEL to put them in and stick them in
4982 /* Get capabilities from the get device id. */
4983 intf->local_sel_device = 0;
4984 intf->local_event_generator = 0;
4985 intf->event_receiver = 0;
4987 /* Request the device info from the local MC. */
4988 msg.netfn = IPMI_NETFN_APP_REQUEST;
4989 msg.cmd = IPMI_GET_DEVICE_ID_CMD;
4992 intf->null_user_handler = device_id_fetcher;
4993 ipmi_panic_request_and_wait(intf, &addr, &msg);
4995 if (intf->local_event_generator) {
4996 /* Request the event receiver from the local MC. */
4997 msg.netfn = IPMI_NETFN_SENSOR_EVENT_REQUEST;
4998 msg.cmd = IPMI_GET_EVENT_RECEIVER_CMD;
5001 intf->null_user_handler = event_receiver_fetcher;
5002 ipmi_panic_request_and_wait(intf, &addr, &msg);
5004 intf->null_user_handler = NULL;
5007 * Validate the event receiver. The low bit must not
5008 * be 1 (it must be a valid IPMB address), it cannot
5009 * be zero, and it must not be my address.
5011 if (((intf->event_receiver & 1) == 0)
5012 && (intf->event_receiver != 0)
5013 && (intf->event_receiver != intf->addrinfo[0].address)) {
5015 * The event receiver is valid, send an IPMB
5018 ipmb = (struct ipmi_ipmb_addr *) &addr;
5019 ipmb->addr_type = IPMI_IPMB_ADDR_TYPE;
5020 ipmb->channel = 0; /* FIXME - is this right? */
5021 ipmb->lun = intf->event_receiver_lun;
5022 ipmb->slave_addr = intf->event_receiver;
5023 } else if (intf->local_sel_device) {
5025 * The event receiver was not valid (or was
5026 * me), but I am an SEL device, just dump it
5029 si = (struct ipmi_system_interface_addr *) &addr;
5030 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
5031 si->channel = IPMI_BMC_CHANNEL;
5034 return; /* No where to send the event. */
5036 msg.netfn = IPMI_NETFN_STORAGE_REQUEST; /* Storage. */
5037 msg.cmd = IPMI_ADD_SEL_ENTRY_CMD;
5043 int size = strlen(p);
5049 data[2] = 0xf0; /* OEM event without timestamp. */
5050 data[3] = intf->addrinfo[0].address;
5051 data[4] = j++; /* sequence # */
5053 * Always give 11 bytes, so strncpy will fill
5054 * it with zeroes for me.
5056 strncpy(data+5, p, 11);
5059 ipmi_panic_request_and_wait(intf, &addr, &msg);
5063 static int has_panicked;
5065 static int panic_event(struct notifier_block *this,
5066 unsigned long event,
5069 struct ipmi_smi *intf;
5070 struct ipmi_user *user;
5076 /* For every registered interface, set it to run to completion. */
5077 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
5078 if (!intf->handlers || intf->intf_num == -1)
5079 /* Interface is not ready. */
5082 if (!intf->handlers->poll)
5086 * If we were interrupted while locking xmit_msgs_lock or
5087 * waiting_rcv_msgs_lock, the corresponding list may be
5088 * corrupted. In this case, drop items on the list for
5091 if (!spin_trylock(&intf->xmit_msgs_lock)) {
5092 INIT_LIST_HEAD(&intf->xmit_msgs);
5093 INIT_LIST_HEAD(&intf->hp_xmit_msgs);
5095 spin_unlock(&intf->xmit_msgs_lock);
5097 if (!spin_trylock(&intf->waiting_rcv_msgs_lock))
5098 INIT_LIST_HEAD(&intf->waiting_rcv_msgs);
5100 spin_unlock(&intf->waiting_rcv_msgs_lock);
5102 intf->run_to_completion = 1;
5103 if (intf->handlers->set_run_to_completion)
5104 intf->handlers->set_run_to_completion(intf->send_info,
5107 list_for_each_entry_rcu(user, &intf->users, link) {
5108 if (user->handler->ipmi_panic_handler)
5109 user->handler->ipmi_panic_handler(
5110 user->handler_data);
5113 send_panic_events(intf, ptr);
5119 /* Must be called with ipmi_interfaces_mutex held. */
5120 static int ipmi_register_driver(void)
5127 rv = driver_register(&ipmidriver.driver);
5129 pr_err("Could not register IPMI driver\n");
5131 drvregistered = true;
5135 static struct notifier_block panic_block = {
5136 .notifier_call = panic_event,
5138 .priority = 200 /* priority: INT_MAX >= x >= 0 */
5141 static int ipmi_init_msghandler(void)
5145 mutex_lock(&ipmi_interfaces_mutex);
5146 rv = ipmi_register_driver();
5152 init_srcu_struct(&ipmi_interfaces_srcu);
5154 timer_setup(&ipmi_timer, ipmi_timeout, 0);
5155 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
5157 atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
5162 mutex_unlock(&ipmi_interfaces_mutex);
5166 static int __init ipmi_init_msghandler_mod(void)
5170 pr_info("version " IPMI_DRIVER_VERSION "\n");
5172 mutex_lock(&ipmi_interfaces_mutex);
5173 rv = ipmi_register_driver();
5174 mutex_unlock(&ipmi_interfaces_mutex);
5179 static void __exit cleanup_ipmi(void)
5184 atomic_notifier_chain_unregister(&panic_notifier_list,
5188 * This can't be called if any interfaces exist, so no worry
5189 * about shutting down the interfaces.
5193 * Tell the timer to stop, then wait for it to stop. This
5194 * avoids problems with race conditions removing the timer
5197 atomic_set(&stop_operation, 1);
5198 del_timer_sync(&ipmi_timer);
5200 initialized = false;
5202 /* Check for buffer leaks. */
5203 count = atomic_read(&smi_msg_inuse_count);
5205 pr_warn("SMI message count %d at exit\n", count);
5206 count = atomic_read(&recv_msg_inuse_count);
5208 pr_warn("recv message count %d at exit\n", count);
5210 cleanup_srcu_struct(&ipmi_interfaces_srcu);
5213 driver_unregister(&ipmidriver.driver);
5215 module_exit(cleanup_ipmi);
5217 module_init(ipmi_init_msghandler_mod);
5218 MODULE_LICENSE("GPL");
5219 MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
5220 MODULE_DESCRIPTION("Incoming and outgoing message routing for an IPMI"
5222 MODULE_VERSION(IPMI_DRIVER_VERSION);
5223 MODULE_SOFTDEP("post: ipmi_devintf");