1 // SPDX-License-Identifier: GPL-2.0+
5 * Incoming and outgoing message routing for an IPMI interface.
7 * Author: MontaVista Software, Inc.
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);
47 static bool initialized;
48 static bool drvregistered;
50 enum ipmi_panic_event_op {
51 IPMI_SEND_PANIC_EVENT_NONE,
52 IPMI_SEND_PANIC_EVENT,
53 IPMI_SEND_PANIC_EVENT_STRING
55 #ifdef CONFIG_IPMI_PANIC_STRING
56 #define IPMI_PANIC_DEFAULT IPMI_SEND_PANIC_EVENT_STRING
57 #elif defined(CONFIG_IPMI_PANIC_EVENT)
58 #define IPMI_PANIC_DEFAULT IPMI_SEND_PANIC_EVENT
60 #define IPMI_PANIC_DEFAULT IPMI_SEND_PANIC_EVENT_NONE
62 static enum ipmi_panic_event_op ipmi_send_panic_event = IPMI_PANIC_DEFAULT;
64 static int panic_op_write_handler(const char *val,
65 const struct kernel_param *kp)
70 strncpy(valcp, val, 15);
75 if (strcmp(s, "none") == 0)
76 ipmi_send_panic_event = IPMI_SEND_PANIC_EVENT_NONE;
77 else if (strcmp(s, "event") == 0)
78 ipmi_send_panic_event = IPMI_SEND_PANIC_EVENT;
79 else if (strcmp(s, "string") == 0)
80 ipmi_send_panic_event = IPMI_SEND_PANIC_EVENT_STRING;
87 static int panic_op_read_handler(char *buffer, const struct kernel_param *kp)
89 switch (ipmi_send_panic_event) {
90 case IPMI_SEND_PANIC_EVENT_NONE:
91 strcpy(buffer, "none");
94 case IPMI_SEND_PANIC_EVENT:
95 strcpy(buffer, "event");
98 case IPMI_SEND_PANIC_EVENT_STRING:
99 strcpy(buffer, "string");
103 strcpy(buffer, "???");
107 return strlen(buffer);
110 static const struct kernel_param_ops panic_op_ops = {
111 .set = panic_op_write_handler,
112 .get = panic_op_read_handler
114 module_param_cb(panic_op, &panic_op_ops, NULL, 0600);
115 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.");
118 #define MAX_EVENTS_IN_QUEUE 25
120 /* Remain in auto-maintenance mode for this amount of time (in ms). */
121 static unsigned long maintenance_mode_timeout_ms = 30000;
122 module_param(maintenance_mode_timeout_ms, ulong, 0644);
123 MODULE_PARM_DESC(maintenance_mode_timeout_ms,
124 "The time (milliseconds) after the last maintenance message that the connection stays in maintenance mode.");
127 * Don't let a message sit in a queue forever, always time it with at lest
128 * the max message timer. This is in milliseconds.
130 #define MAX_MSG_TIMEOUT 60000
133 * Timeout times below are in milliseconds, and are done off a 1
134 * second timer. So setting the value to 1000 would mean anything
135 * between 0 and 1000ms. So really the only reasonable minimum
136 * setting it 2000ms, which is between 1 and 2 seconds.
139 /* The default timeout for message retries. */
140 static unsigned long default_retry_ms = 2000;
141 module_param(default_retry_ms, ulong, 0644);
142 MODULE_PARM_DESC(default_retry_ms,
143 "The time (milliseconds) between retry sends");
145 /* The default timeout for maintenance mode message retries. */
146 static unsigned long default_maintenance_retry_ms = 3000;
147 module_param(default_maintenance_retry_ms, ulong, 0644);
148 MODULE_PARM_DESC(default_maintenance_retry_ms,
149 "The time (milliseconds) between retry sends in maintenance mode");
151 /* The default maximum number of retries */
152 static unsigned int default_max_retries = 4;
153 module_param(default_max_retries, uint, 0644);
154 MODULE_PARM_DESC(default_max_retries,
155 "The time (milliseconds) between retry sends in maintenance mode");
157 /* Call every ~1000 ms. */
158 #define IPMI_TIMEOUT_TIME 1000
160 /* How many jiffies does it take to get to the timeout time. */
161 #define IPMI_TIMEOUT_JIFFIES ((IPMI_TIMEOUT_TIME * HZ) / 1000)
164 * Request events from the queue every second (this is the number of
165 * IPMI_TIMEOUT_TIMES between event requests). Hopefully, in the
166 * future, IPMI will add a way to know immediately if an event is in
167 * the queue and this silliness can go away.
169 #define IPMI_REQUEST_EV_TIME (1000 / (IPMI_TIMEOUT_TIME))
171 /* How long should we cache dynamic device IDs? */
172 #define IPMI_DYN_DEV_ID_EXPIRY (10 * HZ)
175 * The main "user" data structure.
178 struct list_head link;
181 * Set to NULL when the user is destroyed, a pointer to myself
182 * so srcu_dereference can be used on it.
184 struct ipmi_user *self;
185 struct srcu_struct release_barrier;
187 struct kref refcount;
189 /* The upper layer that handles receive messages. */
190 const struct ipmi_user_hndl *handler;
193 /* The interface this user is bound to. */
194 struct ipmi_smi *intf;
196 /* Does this interface receive IPMI events? */
199 /* Free must run in process context for RCU cleanup. */
200 struct work_struct remove_work;
203 static struct ipmi_user *acquire_ipmi_user(struct ipmi_user *user, int *index)
204 __acquires(user->release_barrier)
206 struct ipmi_user *ruser;
208 *index = srcu_read_lock(&user->release_barrier);
209 ruser = srcu_dereference(user->self, &user->release_barrier);
211 srcu_read_unlock(&user->release_barrier, *index);
215 static void release_ipmi_user(struct ipmi_user *user, int index)
217 srcu_read_unlock(&user->release_barrier, index);
221 struct list_head link;
223 struct ipmi_user *user;
229 * This is used to form a linked lised during mass deletion.
230 * Since this is in an RCU list, we cannot use the link above
231 * or change any data until the RCU period completes. So we
232 * use this next variable during mass deletion so we can have
233 * a list and don't have to wait and restart the search on
234 * every individual deletion of a command.
236 struct cmd_rcvr *next;
240 unsigned int inuse : 1;
241 unsigned int broadcast : 1;
243 unsigned long timeout;
244 unsigned long orig_timeout;
245 unsigned int retries_left;
248 * To verify on an incoming send message response that this is
249 * the message that the response is for, we keep a sequence id
250 * and increment it every time we send a message.
255 * This is held so we can properly respond to the message on a
256 * timeout, and it is used to hold the temporary data for
257 * retransmission, too.
259 struct ipmi_recv_msg *recv_msg;
263 * Store the information in a msgid (long) to allow us to find a
264 * sequence table entry from the msgid.
266 #define STORE_SEQ_IN_MSGID(seq, seqid) \
267 ((((seq) & 0x3f) << 26) | ((seqid) & 0x3ffffff))
269 #define GET_SEQ_FROM_MSGID(msgid, seq, seqid) \
271 seq = (((msgid) >> 26) & 0x3f); \
272 seqid = ((msgid) & 0x3ffffff); \
275 #define NEXT_SEQID(seqid) (((seqid) + 1) & 0x3ffffff)
277 #define IPMI_MAX_CHANNELS 16
278 struct ipmi_channel {
279 unsigned char medium;
280 unsigned char protocol;
283 struct ipmi_channel_set {
284 struct ipmi_channel c[IPMI_MAX_CHANNELS];
287 struct ipmi_my_addrinfo {
289 * My slave address. This is initialized to IPMI_BMC_SLAVE_ADDR,
290 * but may be changed by the user.
292 unsigned char address;
295 * My LUN. This should generally stay the SMS LUN, but just in
302 * Note that the product id, manufacturer id, guid, and device id are
303 * immutable in this structure, so dyn_mutex is not required for
304 * accessing those. If those change on a BMC, a new BMC is allocated.
307 struct platform_device pdev;
308 struct list_head intfs; /* Interfaces on this BMC. */
309 struct ipmi_device_id id;
310 struct ipmi_device_id fetch_id;
312 unsigned long dyn_id_expiry;
313 struct mutex dyn_mutex; /* Protects id, intfs, & dyn* */
317 struct kref usecount;
318 struct work_struct remove_work;
320 #define to_bmc_device(x) container_of((x), struct bmc_device, pdev.dev)
322 static int bmc_get_device_id(struct ipmi_smi *intf, struct bmc_device *bmc,
323 struct ipmi_device_id *id,
324 bool *guid_set, guid_t *guid);
327 * Various statistics for IPMI, these index stats[] in the ipmi_smi
330 enum ipmi_stat_indexes {
331 /* Commands we got from the user that were invalid. */
332 IPMI_STAT_sent_invalid_commands = 0,
334 /* Commands we sent to the MC. */
335 IPMI_STAT_sent_local_commands,
337 /* Responses from the MC that were delivered to a user. */
338 IPMI_STAT_handled_local_responses,
340 /* Responses from the MC that were not delivered to a user. */
341 IPMI_STAT_unhandled_local_responses,
343 /* Commands we sent out to the IPMB bus. */
344 IPMI_STAT_sent_ipmb_commands,
346 /* Commands sent on the IPMB that had errors on the SEND CMD */
347 IPMI_STAT_sent_ipmb_command_errs,
349 /* Each retransmit increments this count. */
350 IPMI_STAT_retransmitted_ipmb_commands,
353 * When a message times out (runs out of retransmits) this is
356 IPMI_STAT_timed_out_ipmb_commands,
359 * This is like above, but for broadcasts. Broadcasts are
360 * *not* included in the above count (they are expected to
363 IPMI_STAT_timed_out_ipmb_broadcasts,
365 /* Responses I have sent to the IPMB bus. */
366 IPMI_STAT_sent_ipmb_responses,
368 /* The response was delivered to the user. */
369 IPMI_STAT_handled_ipmb_responses,
371 /* The response had invalid data in it. */
372 IPMI_STAT_invalid_ipmb_responses,
374 /* The response didn't have anyone waiting for it. */
375 IPMI_STAT_unhandled_ipmb_responses,
377 /* Commands we sent out to the IPMB bus. */
378 IPMI_STAT_sent_lan_commands,
380 /* Commands sent on the IPMB that had errors on the SEND CMD */
381 IPMI_STAT_sent_lan_command_errs,
383 /* Each retransmit increments this count. */
384 IPMI_STAT_retransmitted_lan_commands,
387 * When a message times out (runs out of retransmits) this is
390 IPMI_STAT_timed_out_lan_commands,
392 /* Responses I have sent to the IPMB bus. */
393 IPMI_STAT_sent_lan_responses,
395 /* The response was delivered to the user. */
396 IPMI_STAT_handled_lan_responses,
398 /* The response had invalid data in it. */
399 IPMI_STAT_invalid_lan_responses,
401 /* The response didn't have anyone waiting for it. */
402 IPMI_STAT_unhandled_lan_responses,
404 /* The command was delivered to the user. */
405 IPMI_STAT_handled_commands,
407 /* The command had invalid data in it. */
408 IPMI_STAT_invalid_commands,
410 /* The command didn't have anyone waiting for it. */
411 IPMI_STAT_unhandled_commands,
413 /* Invalid data in an event. */
414 IPMI_STAT_invalid_events,
416 /* Events that were received with the proper format. */
419 /* Retransmissions on IPMB that failed. */
420 IPMI_STAT_dropped_rexmit_ipmb_commands,
422 /* Retransmissions on LAN that failed. */
423 IPMI_STAT_dropped_rexmit_lan_commands,
425 /* This *must* remain last, add new values above this. */
430 #define IPMI_IPMB_NUM_SEQ 64
432 struct module *owner;
434 /* What interface number are we? */
437 struct kref refcount;
439 /* Set when the interface is being unregistered. */
442 /* Used for a list of interfaces. */
443 struct list_head link;
446 * The list of upper layers that are using me. seq_lock write
447 * protects this. Read protection is with srcu.
449 struct list_head users;
450 struct srcu_struct users_srcu;
452 /* Used for wake ups at startup. */
453 wait_queue_head_t waitq;
456 * Prevents the interface from being unregistered when the
457 * interface is used by being looked up through the BMC
460 struct mutex bmc_reg_mutex;
462 struct bmc_device tmp_bmc;
463 struct bmc_device *bmc;
465 struct list_head bmc_link;
467 bool in_bmc_register; /* Handle recursive situations. Yuck. */
468 struct work_struct bmc_reg_work;
470 const struct ipmi_smi_handlers *handlers;
473 /* Driver-model device for the system interface. */
474 struct device *si_dev;
477 * A table of sequence numbers for this interface. We use the
478 * sequence numbers for IPMB messages that go out of the
479 * interface to match them up with their responses. A routine
480 * is called periodically to time the items in this list.
483 struct seq_table seq_table[IPMI_IPMB_NUM_SEQ];
487 * Messages queued for delivery. If delivery fails (out of memory
488 * for instance), They will stay in here to be processed later in a
489 * periodic timer interrupt. The tasklet is for handling received
490 * messages directly from the handler.
492 spinlock_t waiting_rcv_msgs_lock;
493 struct list_head waiting_rcv_msgs;
494 atomic_t watchdog_pretimeouts_to_deliver;
495 struct tasklet_struct recv_tasklet;
497 spinlock_t xmit_msgs_lock;
498 struct list_head xmit_msgs;
499 struct ipmi_smi_msg *curr_msg;
500 struct list_head hp_xmit_msgs;
503 * The list of command receivers that are registered for commands
506 struct mutex cmd_rcvrs_mutex;
507 struct list_head cmd_rcvrs;
510 * Events that were queues because no one was there to receive
513 spinlock_t events_lock; /* For dealing with event stuff. */
514 struct list_head waiting_events;
515 unsigned int waiting_events_count; /* How many events in queue? */
516 char delivering_events;
517 char event_msg_printed;
519 /* How many users are waiting for events? */
520 atomic_t event_waiters;
521 unsigned int ticks_to_req_ev;
523 spinlock_t watch_lock; /* For dealing with watch stuff below. */
525 /* How many users are waiting for commands? */
526 unsigned int command_waiters;
528 /* How many users are waiting for watchdogs? */
529 unsigned int watchdog_waiters;
531 /* How many users are waiting for message responses? */
532 unsigned int response_waiters;
535 * Tells what the lower layer has last been asked to watch for,
536 * messages and/or watchdogs. Protected by watch_lock.
538 unsigned int last_watch_mask;
541 * The event receiver for my BMC, only really used at panic
542 * shutdown as a place to store this.
544 unsigned char event_receiver;
545 unsigned char event_receiver_lun;
546 unsigned char local_sel_device;
547 unsigned char local_event_generator;
549 /* For handling of maintenance mode. */
550 int maintenance_mode;
551 bool maintenance_mode_enable;
552 int auto_maintenance_timeout;
553 spinlock_t maintenance_mode_lock; /* Used in a timer... */
556 * If we are doing maintenance on something on IPMB, extend
557 * the timeout time to avoid timeouts writing firmware and
560 int ipmb_maintenance_mode_timeout;
563 * A cheap hack, if this is non-null and a message to an
564 * interface comes in with a NULL user, call this routine with
565 * it. Note that the message will still be freed by the
566 * caller. This only works on the system interface.
568 * Protected by bmc_reg_mutex.
570 void (*null_user_handler)(struct ipmi_smi *intf,
571 struct ipmi_recv_msg *msg);
574 * When we are scanning the channels for an SMI, this will
575 * tell which channel we are scanning.
579 /* Channel information */
580 struct ipmi_channel_set *channel_list;
581 unsigned int curr_working_cset; /* First index into the following. */
582 struct ipmi_channel_set wchannels[2];
583 struct ipmi_my_addrinfo addrinfo[IPMI_MAX_CHANNELS];
586 atomic_t stats[IPMI_NUM_STATS];
589 * run_to_completion duplicate of smb_info, smi_info
590 * and ipmi_serial_info structures. Used to decrease numbers of
591 * parameters passed by "low" level IPMI code.
593 int run_to_completion;
595 #define to_si_intf_from_dev(device) container_of(device, struct ipmi_smi, dev)
597 static void __get_guid(struct ipmi_smi *intf);
598 static void __ipmi_bmc_unregister(struct ipmi_smi *intf);
599 static int __ipmi_bmc_register(struct ipmi_smi *intf,
600 struct ipmi_device_id *id,
601 bool guid_set, guid_t *guid, int intf_num);
602 static int __scan_channels(struct ipmi_smi *intf, struct ipmi_device_id *id);
606 * The driver model view of the IPMI messaging driver.
608 static struct platform_driver ipmidriver = {
611 .bus = &platform_bus_type
615 * This mutex keeps us from adding the same BMC twice.
617 static DEFINE_MUTEX(ipmidriver_mutex);
619 static LIST_HEAD(ipmi_interfaces);
620 static DEFINE_MUTEX(ipmi_interfaces_mutex);
621 static struct srcu_struct ipmi_interfaces_srcu;
624 * List of watchers that want to know when smi's are added and deleted.
626 static LIST_HEAD(smi_watchers);
627 static DEFINE_MUTEX(smi_watchers_mutex);
629 #define ipmi_inc_stat(intf, stat) \
630 atomic_inc(&(intf)->stats[IPMI_STAT_ ## stat])
631 #define ipmi_get_stat(intf, stat) \
632 ((unsigned int) atomic_read(&(intf)->stats[IPMI_STAT_ ## stat]))
634 static const char * const addr_src_to_str[] = {
635 "invalid", "hotmod", "hardcoded", "SPMI", "ACPI", "SMBIOS", "PCI",
636 "device-tree", "platform"
639 const char *ipmi_addr_src_to_str(enum ipmi_addr_src src)
642 src = 0; /* Invalid */
643 return addr_src_to_str[src];
645 EXPORT_SYMBOL(ipmi_addr_src_to_str);
647 static int is_lan_addr(struct ipmi_addr *addr)
649 return addr->addr_type == IPMI_LAN_ADDR_TYPE;
652 static int is_ipmb_addr(struct ipmi_addr *addr)
654 return addr->addr_type == IPMI_IPMB_ADDR_TYPE;
657 static int is_ipmb_bcast_addr(struct ipmi_addr *addr)
659 return addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE;
662 static void free_recv_msg_list(struct list_head *q)
664 struct ipmi_recv_msg *msg, *msg2;
666 list_for_each_entry_safe(msg, msg2, q, link) {
667 list_del(&msg->link);
668 ipmi_free_recv_msg(msg);
672 static void free_smi_msg_list(struct list_head *q)
674 struct ipmi_smi_msg *msg, *msg2;
676 list_for_each_entry_safe(msg, msg2, q, link) {
677 list_del(&msg->link);
678 ipmi_free_smi_msg(msg);
682 static void clean_up_interface_data(struct ipmi_smi *intf)
685 struct cmd_rcvr *rcvr, *rcvr2;
686 struct list_head list;
688 tasklet_kill(&intf->recv_tasklet);
690 free_smi_msg_list(&intf->waiting_rcv_msgs);
691 free_recv_msg_list(&intf->waiting_events);
694 * Wholesale remove all the entries from the list in the
695 * interface and wait for RCU to know that none are in use.
697 mutex_lock(&intf->cmd_rcvrs_mutex);
698 INIT_LIST_HEAD(&list);
699 list_splice_init_rcu(&intf->cmd_rcvrs, &list, synchronize_rcu);
700 mutex_unlock(&intf->cmd_rcvrs_mutex);
702 list_for_each_entry_safe(rcvr, rcvr2, &list, link)
705 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
706 if ((intf->seq_table[i].inuse)
707 && (intf->seq_table[i].recv_msg))
708 ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
712 static void intf_free(struct kref *ref)
714 struct ipmi_smi *intf = container_of(ref, struct ipmi_smi, refcount);
716 clean_up_interface_data(intf);
720 struct watcher_entry {
722 struct ipmi_smi *intf;
723 struct list_head link;
726 int ipmi_smi_watcher_register(struct ipmi_smi_watcher *watcher)
728 struct ipmi_smi *intf;
732 * Make sure the driver is actually initialized, this handles
733 * problems with initialization order.
735 rv = ipmi_init_msghandler();
739 mutex_lock(&smi_watchers_mutex);
741 list_add(&watcher->link, &smi_watchers);
743 index = srcu_read_lock(&ipmi_interfaces_srcu);
744 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
745 int intf_num = READ_ONCE(intf->intf_num);
749 watcher->new_smi(intf_num, intf->si_dev);
751 srcu_read_unlock(&ipmi_interfaces_srcu, index);
753 mutex_unlock(&smi_watchers_mutex);
757 EXPORT_SYMBOL(ipmi_smi_watcher_register);
759 int ipmi_smi_watcher_unregister(struct ipmi_smi_watcher *watcher)
761 mutex_lock(&smi_watchers_mutex);
762 list_del(&watcher->link);
763 mutex_unlock(&smi_watchers_mutex);
766 EXPORT_SYMBOL(ipmi_smi_watcher_unregister);
769 * Must be called with smi_watchers_mutex held.
772 call_smi_watchers(int i, struct device *dev)
774 struct ipmi_smi_watcher *w;
776 mutex_lock(&smi_watchers_mutex);
777 list_for_each_entry(w, &smi_watchers, link) {
778 if (try_module_get(w->owner)) {
780 module_put(w->owner);
783 mutex_unlock(&smi_watchers_mutex);
787 ipmi_addr_equal(struct ipmi_addr *addr1, struct ipmi_addr *addr2)
789 if (addr1->addr_type != addr2->addr_type)
792 if (addr1->channel != addr2->channel)
795 if (addr1->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
796 struct ipmi_system_interface_addr *smi_addr1
797 = (struct ipmi_system_interface_addr *) addr1;
798 struct ipmi_system_interface_addr *smi_addr2
799 = (struct ipmi_system_interface_addr *) addr2;
800 return (smi_addr1->lun == smi_addr2->lun);
803 if (is_ipmb_addr(addr1) || is_ipmb_bcast_addr(addr1)) {
804 struct ipmi_ipmb_addr *ipmb_addr1
805 = (struct ipmi_ipmb_addr *) addr1;
806 struct ipmi_ipmb_addr *ipmb_addr2
807 = (struct ipmi_ipmb_addr *) addr2;
809 return ((ipmb_addr1->slave_addr == ipmb_addr2->slave_addr)
810 && (ipmb_addr1->lun == ipmb_addr2->lun));
813 if (is_lan_addr(addr1)) {
814 struct ipmi_lan_addr *lan_addr1
815 = (struct ipmi_lan_addr *) addr1;
816 struct ipmi_lan_addr *lan_addr2
817 = (struct ipmi_lan_addr *) addr2;
819 return ((lan_addr1->remote_SWID == lan_addr2->remote_SWID)
820 && (lan_addr1->local_SWID == lan_addr2->local_SWID)
821 && (lan_addr1->session_handle
822 == lan_addr2->session_handle)
823 && (lan_addr1->lun == lan_addr2->lun));
829 int ipmi_validate_addr(struct ipmi_addr *addr, int len)
831 if (len < sizeof(struct ipmi_system_interface_addr))
834 if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
835 if (addr->channel != IPMI_BMC_CHANNEL)
840 if ((addr->channel == IPMI_BMC_CHANNEL)
841 || (addr->channel >= IPMI_MAX_CHANNELS)
842 || (addr->channel < 0))
845 if (is_ipmb_addr(addr) || is_ipmb_bcast_addr(addr)) {
846 if (len < sizeof(struct ipmi_ipmb_addr))
851 if (is_lan_addr(addr)) {
852 if (len < sizeof(struct ipmi_lan_addr))
859 EXPORT_SYMBOL(ipmi_validate_addr);
861 unsigned int ipmi_addr_length(int addr_type)
863 if (addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
864 return sizeof(struct ipmi_system_interface_addr);
866 if ((addr_type == IPMI_IPMB_ADDR_TYPE)
867 || (addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE))
868 return sizeof(struct ipmi_ipmb_addr);
870 if (addr_type == IPMI_LAN_ADDR_TYPE)
871 return sizeof(struct ipmi_lan_addr);
875 EXPORT_SYMBOL(ipmi_addr_length);
877 static int deliver_response(struct ipmi_smi *intf, struct ipmi_recv_msg *msg)
882 /* Special handling for NULL users. */
883 if (intf->null_user_handler) {
884 intf->null_user_handler(intf, msg);
886 /* No handler, so give up. */
889 ipmi_free_recv_msg(msg);
890 } else if (oops_in_progress) {
892 * If we are running in the panic context, calling the
893 * receive handler doesn't much meaning and has a deadlock
894 * risk. At this moment, simply skip it in that case.
896 ipmi_free_recv_msg(msg);
899 struct ipmi_user *user = acquire_ipmi_user(msg->user, &index);
902 user->handler->ipmi_recv_hndl(msg, user->handler_data);
903 release_ipmi_user(user, index);
905 /* User went away, give up. */
906 ipmi_free_recv_msg(msg);
914 static void deliver_local_response(struct ipmi_smi *intf,
915 struct ipmi_recv_msg *msg)
917 if (deliver_response(intf, msg))
918 ipmi_inc_stat(intf, unhandled_local_responses);
920 ipmi_inc_stat(intf, handled_local_responses);
923 static void deliver_err_response(struct ipmi_smi *intf,
924 struct ipmi_recv_msg *msg, int err)
926 msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
927 msg->msg_data[0] = err;
928 msg->msg.netfn |= 1; /* Convert to a response. */
929 msg->msg.data_len = 1;
930 msg->msg.data = msg->msg_data;
931 deliver_local_response(intf, msg);
934 static void smi_add_watch(struct ipmi_smi *intf, unsigned int flags)
936 unsigned long iflags;
938 if (!intf->handlers->set_need_watch)
941 spin_lock_irqsave(&intf->watch_lock, iflags);
942 if (flags & IPMI_WATCH_MASK_CHECK_MESSAGES)
943 intf->response_waiters++;
945 if (flags & IPMI_WATCH_MASK_CHECK_WATCHDOG)
946 intf->watchdog_waiters++;
948 if (flags & IPMI_WATCH_MASK_CHECK_COMMANDS)
949 intf->command_waiters++;
951 if ((intf->last_watch_mask & flags) != flags) {
952 intf->last_watch_mask |= flags;
953 intf->handlers->set_need_watch(intf->send_info,
954 intf->last_watch_mask);
956 spin_unlock_irqrestore(&intf->watch_lock, iflags);
959 static void smi_remove_watch(struct ipmi_smi *intf, unsigned int flags)
961 unsigned long iflags;
963 if (!intf->handlers->set_need_watch)
966 spin_lock_irqsave(&intf->watch_lock, iflags);
967 if (flags & IPMI_WATCH_MASK_CHECK_MESSAGES)
968 intf->response_waiters--;
970 if (flags & IPMI_WATCH_MASK_CHECK_WATCHDOG)
971 intf->watchdog_waiters--;
973 if (flags & IPMI_WATCH_MASK_CHECK_COMMANDS)
974 intf->command_waiters--;
977 if (intf->response_waiters)
978 flags |= IPMI_WATCH_MASK_CHECK_MESSAGES;
979 if (intf->watchdog_waiters)
980 flags |= IPMI_WATCH_MASK_CHECK_WATCHDOG;
981 if (intf->command_waiters)
982 flags |= IPMI_WATCH_MASK_CHECK_COMMANDS;
984 if (intf->last_watch_mask != flags) {
985 intf->last_watch_mask = flags;
986 intf->handlers->set_need_watch(intf->send_info,
987 intf->last_watch_mask);
989 spin_unlock_irqrestore(&intf->watch_lock, iflags);
993 * Find the next sequence number not being used and add the given
994 * message with the given timeout to the sequence table. This must be
995 * called with the interface's seq_lock held.
997 static int intf_next_seq(struct ipmi_smi *intf,
998 struct ipmi_recv_msg *recv_msg,
999 unsigned long timeout,
1009 timeout = default_retry_ms;
1011 retries = default_max_retries;
1013 for (i = intf->curr_seq; (i+1)%IPMI_IPMB_NUM_SEQ != intf->curr_seq;
1014 i = (i+1)%IPMI_IPMB_NUM_SEQ) {
1015 if (!intf->seq_table[i].inuse)
1019 if (!intf->seq_table[i].inuse) {
1020 intf->seq_table[i].recv_msg = recv_msg;
1023 * Start with the maximum timeout, when the send response
1024 * comes in we will start the real timer.
1026 intf->seq_table[i].timeout = MAX_MSG_TIMEOUT;
1027 intf->seq_table[i].orig_timeout = timeout;
1028 intf->seq_table[i].retries_left = retries;
1029 intf->seq_table[i].broadcast = broadcast;
1030 intf->seq_table[i].inuse = 1;
1031 intf->seq_table[i].seqid = NEXT_SEQID(intf->seq_table[i].seqid);
1033 *seqid = intf->seq_table[i].seqid;
1034 intf->curr_seq = (i+1)%IPMI_IPMB_NUM_SEQ;
1035 smi_add_watch(intf, IPMI_WATCH_MASK_CHECK_MESSAGES);
1045 * Return the receive message for the given sequence number and
1046 * release the sequence number so it can be reused. Some other data
1047 * is passed in to be sure the message matches up correctly (to help
1048 * guard against message coming in after their timeout and the
1049 * sequence number being reused).
1051 static int intf_find_seq(struct ipmi_smi *intf,
1055 unsigned char netfn,
1056 struct ipmi_addr *addr,
1057 struct ipmi_recv_msg **recv_msg)
1060 unsigned long flags;
1062 if (seq >= IPMI_IPMB_NUM_SEQ)
1065 spin_lock_irqsave(&intf->seq_lock, flags);
1066 if (intf->seq_table[seq].inuse) {
1067 struct ipmi_recv_msg *msg = intf->seq_table[seq].recv_msg;
1069 if ((msg->addr.channel == channel) && (msg->msg.cmd == cmd)
1070 && (msg->msg.netfn == netfn)
1071 && (ipmi_addr_equal(addr, &msg->addr))) {
1073 intf->seq_table[seq].inuse = 0;
1074 smi_remove_watch(intf, IPMI_WATCH_MASK_CHECK_MESSAGES);
1078 spin_unlock_irqrestore(&intf->seq_lock, flags);
1084 /* Start the timer for a specific sequence table entry. */
1085 static int intf_start_seq_timer(struct ipmi_smi *intf,
1089 unsigned long flags;
1091 unsigned long seqid;
1094 GET_SEQ_FROM_MSGID(msgid, seq, seqid);
1096 spin_lock_irqsave(&intf->seq_lock, flags);
1098 * We do this verification because the user can be deleted
1099 * while a message is outstanding.
1101 if ((intf->seq_table[seq].inuse)
1102 && (intf->seq_table[seq].seqid == seqid)) {
1103 struct seq_table *ent = &intf->seq_table[seq];
1104 ent->timeout = ent->orig_timeout;
1107 spin_unlock_irqrestore(&intf->seq_lock, flags);
1112 /* Got an error for the send message for a specific sequence number. */
1113 static int intf_err_seq(struct ipmi_smi *intf,
1118 unsigned long flags;
1120 unsigned long seqid;
1121 struct ipmi_recv_msg *msg = NULL;
1124 GET_SEQ_FROM_MSGID(msgid, seq, seqid);
1126 spin_lock_irqsave(&intf->seq_lock, flags);
1128 * We do this verification because the user can be deleted
1129 * while a message is outstanding.
1131 if ((intf->seq_table[seq].inuse)
1132 && (intf->seq_table[seq].seqid == seqid)) {
1133 struct seq_table *ent = &intf->seq_table[seq];
1136 smi_remove_watch(intf, IPMI_WATCH_MASK_CHECK_MESSAGES);
1137 msg = ent->recv_msg;
1140 spin_unlock_irqrestore(&intf->seq_lock, flags);
1143 deliver_err_response(intf, msg, err);
1148 static void free_user_work(struct work_struct *work)
1150 struct ipmi_user *user = container_of(work, struct ipmi_user,
1153 cleanup_srcu_struct(&user->release_barrier);
1157 int ipmi_create_user(unsigned int if_num,
1158 const struct ipmi_user_hndl *handler,
1160 struct ipmi_user **user)
1162 unsigned long flags;
1163 struct ipmi_user *new_user;
1165 struct ipmi_smi *intf;
1168 * There is no module usecount here, because it's not
1169 * required. Since this can only be used by and called from
1170 * other modules, they will implicitly use this module, and
1171 * thus this can't be removed unless the other modules are
1175 if (handler == NULL)
1179 * Make sure the driver is actually initialized, this handles
1180 * problems with initialization order.
1182 rv = ipmi_init_msghandler();
1186 new_user = kmalloc(sizeof(*new_user), GFP_KERNEL);
1190 index = srcu_read_lock(&ipmi_interfaces_srcu);
1191 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
1192 if (intf->intf_num == if_num)
1195 /* Not found, return an error */
1200 INIT_WORK(&new_user->remove_work, free_user_work);
1202 rv = init_srcu_struct(&new_user->release_barrier);
1206 if (!try_module_get(intf->owner)) {
1211 /* Note that each existing user holds a refcount to the interface. */
1212 kref_get(&intf->refcount);
1214 kref_init(&new_user->refcount);
1215 new_user->handler = handler;
1216 new_user->handler_data = handler_data;
1217 new_user->intf = intf;
1218 new_user->gets_events = false;
1220 rcu_assign_pointer(new_user->self, new_user);
1221 spin_lock_irqsave(&intf->seq_lock, flags);
1222 list_add_rcu(&new_user->link, &intf->users);
1223 spin_unlock_irqrestore(&intf->seq_lock, flags);
1224 if (handler->ipmi_watchdog_pretimeout)
1225 /* User wants pretimeouts, so make sure to watch for them. */
1226 smi_add_watch(intf, IPMI_WATCH_MASK_CHECK_WATCHDOG);
1227 srcu_read_unlock(&ipmi_interfaces_srcu, index);
1232 srcu_read_unlock(&ipmi_interfaces_srcu, index);
1236 EXPORT_SYMBOL(ipmi_create_user);
1238 int ipmi_get_smi_info(int if_num, struct ipmi_smi_info *data)
1241 struct ipmi_smi *intf;
1243 index = srcu_read_lock(&ipmi_interfaces_srcu);
1244 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
1245 if (intf->intf_num == if_num)
1248 srcu_read_unlock(&ipmi_interfaces_srcu, index);
1250 /* Not found, return an error */
1254 if (!intf->handlers->get_smi_info)
1257 rv = intf->handlers->get_smi_info(intf->send_info, data);
1258 srcu_read_unlock(&ipmi_interfaces_srcu, index);
1262 EXPORT_SYMBOL(ipmi_get_smi_info);
1264 static void free_user(struct kref *ref)
1266 struct ipmi_user *user = container_of(ref, struct ipmi_user, refcount);
1268 /* SRCU cleanup must happen in task context. */
1269 schedule_work(&user->remove_work);
1272 static void _ipmi_destroy_user(struct ipmi_user *user)
1274 struct ipmi_smi *intf = user->intf;
1276 unsigned long flags;
1277 struct cmd_rcvr *rcvr;
1278 struct cmd_rcvr *rcvrs = NULL;
1280 if (!acquire_ipmi_user(user, &i)) {
1282 * The user has already been cleaned up, just make sure
1283 * nothing is using it and return.
1285 synchronize_srcu(&user->release_barrier);
1289 rcu_assign_pointer(user->self, NULL);
1290 release_ipmi_user(user, i);
1292 synchronize_srcu(&user->release_barrier);
1294 if (user->handler->shutdown)
1295 user->handler->shutdown(user->handler_data);
1297 if (user->handler->ipmi_watchdog_pretimeout)
1298 smi_remove_watch(intf, IPMI_WATCH_MASK_CHECK_WATCHDOG);
1300 if (user->gets_events)
1301 atomic_dec(&intf->event_waiters);
1303 /* Remove the user from the interface's sequence table. */
1304 spin_lock_irqsave(&intf->seq_lock, flags);
1305 list_del_rcu(&user->link);
1307 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
1308 if (intf->seq_table[i].inuse
1309 && (intf->seq_table[i].recv_msg->user == user)) {
1310 intf->seq_table[i].inuse = 0;
1311 smi_remove_watch(intf, IPMI_WATCH_MASK_CHECK_MESSAGES);
1312 ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
1315 spin_unlock_irqrestore(&intf->seq_lock, flags);
1318 * Remove the user from the command receiver's table. First
1319 * we build a list of everything (not using the standard link,
1320 * since other things may be using it till we do
1321 * synchronize_srcu()) then free everything in that list.
1323 mutex_lock(&intf->cmd_rcvrs_mutex);
1324 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
1325 if (rcvr->user == user) {
1326 list_del_rcu(&rcvr->link);
1331 mutex_unlock(&intf->cmd_rcvrs_mutex);
1339 kref_put(&intf->refcount, intf_free);
1340 module_put(intf->owner);
1343 int ipmi_destroy_user(struct ipmi_user *user)
1345 _ipmi_destroy_user(user);
1347 kref_put(&user->refcount, free_user);
1351 EXPORT_SYMBOL(ipmi_destroy_user);
1353 int ipmi_get_version(struct ipmi_user *user,
1354 unsigned char *major,
1355 unsigned char *minor)
1357 struct ipmi_device_id id;
1360 user = acquire_ipmi_user(user, &index);
1364 rv = bmc_get_device_id(user->intf, NULL, &id, NULL, NULL);
1366 *major = ipmi_version_major(&id);
1367 *minor = ipmi_version_minor(&id);
1369 release_ipmi_user(user, index);
1373 EXPORT_SYMBOL(ipmi_get_version);
1375 int ipmi_set_my_address(struct ipmi_user *user,
1376 unsigned int channel,
1377 unsigned char address)
1381 user = acquire_ipmi_user(user, &index);
1385 if (channel >= IPMI_MAX_CHANNELS) {
1388 channel = array_index_nospec(channel, IPMI_MAX_CHANNELS);
1389 user->intf->addrinfo[channel].address = address;
1391 release_ipmi_user(user, index);
1395 EXPORT_SYMBOL(ipmi_set_my_address);
1397 int ipmi_get_my_address(struct ipmi_user *user,
1398 unsigned int channel,
1399 unsigned char *address)
1403 user = acquire_ipmi_user(user, &index);
1407 if (channel >= IPMI_MAX_CHANNELS) {
1410 channel = array_index_nospec(channel, IPMI_MAX_CHANNELS);
1411 *address = user->intf->addrinfo[channel].address;
1413 release_ipmi_user(user, index);
1417 EXPORT_SYMBOL(ipmi_get_my_address);
1419 int ipmi_set_my_LUN(struct ipmi_user *user,
1420 unsigned int channel,
1425 user = acquire_ipmi_user(user, &index);
1429 if (channel >= IPMI_MAX_CHANNELS) {
1432 channel = array_index_nospec(channel, IPMI_MAX_CHANNELS);
1433 user->intf->addrinfo[channel].lun = LUN & 0x3;
1435 release_ipmi_user(user, index);
1439 EXPORT_SYMBOL(ipmi_set_my_LUN);
1441 int ipmi_get_my_LUN(struct ipmi_user *user,
1442 unsigned int channel,
1443 unsigned char *address)
1447 user = acquire_ipmi_user(user, &index);
1451 if (channel >= IPMI_MAX_CHANNELS) {
1454 channel = array_index_nospec(channel, IPMI_MAX_CHANNELS);
1455 *address = user->intf->addrinfo[channel].lun;
1457 release_ipmi_user(user, index);
1461 EXPORT_SYMBOL(ipmi_get_my_LUN);
1463 int ipmi_get_maintenance_mode(struct ipmi_user *user)
1466 unsigned long flags;
1468 user = acquire_ipmi_user(user, &index);
1472 spin_lock_irqsave(&user->intf->maintenance_mode_lock, flags);
1473 mode = user->intf->maintenance_mode;
1474 spin_unlock_irqrestore(&user->intf->maintenance_mode_lock, flags);
1475 release_ipmi_user(user, index);
1479 EXPORT_SYMBOL(ipmi_get_maintenance_mode);
1481 static void maintenance_mode_update(struct ipmi_smi *intf)
1483 if (intf->handlers->set_maintenance_mode)
1484 intf->handlers->set_maintenance_mode(
1485 intf->send_info, intf->maintenance_mode_enable);
1488 int ipmi_set_maintenance_mode(struct ipmi_user *user, int mode)
1491 unsigned long flags;
1492 struct ipmi_smi *intf = user->intf;
1494 user = acquire_ipmi_user(user, &index);
1498 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
1499 if (intf->maintenance_mode != mode) {
1501 case IPMI_MAINTENANCE_MODE_AUTO:
1502 intf->maintenance_mode_enable
1503 = (intf->auto_maintenance_timeout > 0);
1506 case IPMI_MAINTENANCE_MODE_OFF:
1507 intf->maintenance_mode_enable = false;
1510 case IPMI_MAINTENANCE_MODE_ON:
1511 intf->maintenance_mode_enable = true;
1518 intf->maintenance_mode = mode;
1520 maintenance_mode_update(intf);
1523 spin_unlock_irqrestore(&intf->maintenance_mode_lock, flags);
1524 release_ipmi_user(user, index);
1528 EXPORT_SYMBOL(ipmi_set_maintenance_mode);
1530 int ipmi_set_gets_events(struct ipmi_user *user, bool val)
1532 unsigned long flags;
1533 struct ipmi_smi *intf = user->intf;
1534 struct ipmi_recv_msg *msg, *msg2;
1535 struct list_head msgs;
1538 user = acquire_ipmi_user(user, &index);
1542 INIT_LIST_HEAD(&msgs);
1544 spin_lock_irqsave(&intf->events_lock, flags);
1545 if (user->gets_events == val)
1548 user->gets_events = val;
1551 if (atomic_inc_return(&intf->event_waiters) == 1)
1554 atomic_dec(&intf->event_waiters);
1557 if (intf->delivering_events)
1559 * Another thread is delivering events for this, so
1560 * let it handle any new events.
1564 /* Deliver any queued events. */
1565 while (user->gets_events && !list_empty(&intf->waiting_events)) {
1566 list_for_each_entry_safe(msg, msg2, &intf->waiting_events, link)
1567 list_move_tail(&msg->link, &msgs);
1568 intf->waiting_events_count = 0;
1569 if (intf->event_msg_printed) {
1570 dev_warn(intf->si_dev, "Event queue no longer full\n");
1571 intf->event_msg_printed = 0;
1574 intf->delivering_events = 1;
1575 spin_unlock_irqrestore(&intf->events_lock, flags);
1577 list_for_each_entry_safe(msg, msg2, &msgs, link) {
1579 kref_get(&user->refcount);
1580 deliver_local_response(intf, msg);
1583 spin_lock_irqsave(&intf->events_lock, flags);
1584 intf->delivering_events = 0;
1588 spin_unlock_irqrestore(&intf->events_lock, flags);
1589 release_ipmi_user(user, index);
1593 EXPORT_SYMBOL(ipmi_set_gets_events);
1595 static struct cmd_rcvr *find_cmd_rcvr(struct ipmi_smi *intf,
1596 unsigned char netfn,
1600 struct cmd_rcvr *rcvr;
1602 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
1603 if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)
1604 && (rcvr->chans & (1 << chan)))
1610 static int is_cmd_rcvr_exclusive(struct ipmi_smi *intf,
1611 unsigned char netfn,
1615 struct cmd_rcvr *rcvr;
1617 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
1618 if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)
1619 && (rcvr->chans & chans))
1625 int ipmi_register_for_cmd(struct ipmi_user *user,
1626 unsigned char netfn,
1630 struct ipmi_smi *intf = user->intf;
1631 struct cmd_rcvr *rcvr;
1634 user = acquire_ipmi_user(user, &index);
1638 rcvr = kmalloc(sizeof(*rcvr), GFP_KERNEL);
1644 rcvr->netfn = netfn;
1645 rcvr->chans = chans;
1648 mutex_lock(&intf->cmd_rcvrs_mutex);
1649 /* Make sure the command/netfn is not already registered. */
1650 if (!is_cmd_rcvr_exclusive(intf, netfn, cmd, chans)) {
1655 smi_add_watch(intf, IPMI_WATCH_MASK_CHECK_COMMANDS);
1657 list_add_rcu(&rcvr->link, &intf->cmd_rcvrs);
1660 mutex_unlock(&intf->cmd_rcvrs_mutex);
1664 release_ipmi_user(user, index);
1668 EXPORT_SYMBOL(ipmi_register_for_cmd);
1670 int ipmi_unregister_for_cmd(struct ipmi_user *user,
1671 unsigned char netfn,
1675 struct ipmi_smi *intf = user->intf;
1676 struct cmd_rcvr *rcvr;
1677 struct cmd_rcvr *rcvrs = NULL;
1678 int i, rv = -ENOENT, index;
1680 user = acquire_ipmi_user(user, &index);
1684 mutex_lock(&intf->cmd_rcvrs_mutex);
1685 for (i = 0; i < IPMI_NUM_CHANNELS; i++) {
1686 if (((1 << i) & chans) == 0)
1688 rcvr = find_cmd_rcvr(intf, netfn, cmd, i);
1691 if (rcvr->user == user) {
1693 rcvr->chans &= ~chans;
1694 if (rcvr->chans == 0) {
1695 list_del_rcu(&rcvr->link);
1701 mutex_unlock(&intf->cmd_rcvrs_mutex);
1703 release_ipmi_user(user, index);
1705 smi_remove_watch(intf, IPMI_WATCH_MASK_CHECK_COMMANDS);
1713 EXPORT_SYMBOL(ipmi_unregister_for_cmd);
1715 static unsigned char
1716 ipmb_checksum(unsigned char *data, int size)
1718 unsigned char csum = 0;
1720 for (; size > 0; size--, data++)
1726 static inline void format_ipmb_msg(struct ipmi_smi_msg *smi_msg,
1727 struct kernel_ipmi_msg *msg,
1728 struct ipmi_ipmb_addr *ipmb_addr,
1730 unsigned char ipmb_seq,
1732 unsigned char source_address,
1733 unsigned char source_lun)
1737 /* Format the IPMB header data. */
1738 smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
1739 smi_msg->data[1] = IPMI_SEND_MSG_CMD;
1740 smi_msg->data[2] = ipmb_addr->channel;
1742 smi_msg->data[3] = 0;
1743 smi_msg->data[i+3] = ipmb_addr->slave_addr;
1744 smi_msg->data[i+4] = (msg->netfn << 2) | (ipmb_addr->lun & 0x3);
1745 smi_msg->data[i+5] = ipmb_checksum(&smi_msg->data[i + 3], 2);
1746 smi_msg->data[i+6] = source_address;
1747 smi_msg->data[i+7] = (ipmb_seq << 2) | source_lun;
1748 smi_msg->data[i+8] = msg->cmd;
1750 /* Now tack on the data to the message. */
1751 if (msg->data_len > 0)
1752 memcpy(&smi_msg->data[i + 9], msg->data, msg->data_len);
1753 smi_msg->data_size = msg->data_len + 9;
1755 /* Now calculate the checksum and tack it on. */
1756 smi_msg->data[i+smi_msg->data_size]
1757 = ipmb_checksum(&smi_msg->data[i + 6], smi_msg->data_size - 6);
1760 * Add on the checksum size and the offset from the
1763 smi_msg->data_size += 1 + i;
1765 smi_msg->msgid = msgid;
1768 static inline void format_lan_msg(struct ipmi_smi_msg *smi_msg,
1769 struct kernel_ipmi_msg *msg,
1770 struct ipmi_lan_addr *lan_addr,
1772 unsigned char ipmb_seq,
1773 unsigned char source_lun)
1775 /* Format the IPMB header data. */
1776 smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
1777 smi_msg->data[1] = IPMI_SEND_MSG_CMD;
1778 smi_msg->data[2] = lan_addr->channel;
1779 smi_msg->data[3] = lan_addr->session_handle;
1780 smi_msg->data[4] = lan_addr->remote_SWID;
1781 smi_msg->data[5] = (msg->netfn << 2) | (lan_addr->lun & 0x3);
1782 smi_msg->data[6] = ipmb_checksum(&smi_msg->data[4], 2);
1783 smi_msg->data[7] = lan_addr->local_SWID;
1784 smi_msg->data[8] = (ipmb_seq << 2) | source_lun;
1785 smi_msg->data[9] = msg->cmd;
1787 /* Now tack on the data to the message. */
1788 if (msg->data_len > 0)
1789 memcpy(&smi_msg->data[10], msg->data, msg->data_len);
1790 smi_msg->data_size = msg->data_len + 10;
1792 /* Now calculate the checksum and tack it on. */
1793 smi_msg->data[smi_msg->data_size]
1794 = ipmb_checksum(&smi_msg->data[7], smi_msg->data_size - 7);
1797 * Add on the checksum size and the offset from the
1800 smi_msg->data_size += 1;
1802 smi_msg->msgid = msgid;
1805 static struct ipmi_smi_msg *smi_add_send_msg(struct ipmi_smi *intf,
1806 struct ipmi_smi_msg *smi_msg,
1809 if (intf->curr_msg) {
1811 list_add_tail(&smi_msg->link, &intf->hp_xmit_msgs);
1813 list_add_tail(&smi_msg->link, &intf->xmit_msgs);
1816 intf->curr_msg = smi_msg;
1822 static void smi_send(struct ipmi_smi *intf,
1823 const struct ipmi_smi_handlers *handlers,
1824 struct ipmi_smi_msg *smi_msg, int priority)
1826 int run_to_completion = intf->run_to_completion;
1827 unsigned long flags = 0;
1829 if (!run_to_completion)
1830 spin_lock_irqsave(&intf->xmit_msgs_lock, flags);
1831 smi_msg = smi_add_send_msg(intf, smi_msg, priority);
1833 if (!run_to_completion)
1834 spin_unlock_irqrestore(&intf->xmit_msgs_lock, flags);
1837 handlers->sender(intf->send_info, smi_msg);
1840 static bool is_maintenance_mode_cmd(struct kernel_ipmi_msg *msg)
1842 return (((msg->netfn == IPMI_NETFN_APP_REQUEST)
1843 && ((msg->cmd == IPMI_COLD_RESET_CMD)
1844 || (msg->cmd == IPMI_WARM_RESET_CMD)))
1845 || (msg->netfn == IPMI_NETFN_FIRMWARE_REQUEST));
1848 static int i_ipmi_req_sysintf(struct ipmi_smi *intf,
1849 struct ipmi_addr *addr,
1851 struct kernel_ipmi_msg *msg,
1852 struct ipmi_smi_msg *smi_msg,
1853 struct ipmi_recv_msg *recv_msg,
1855 unsigned int retry_time_ms)
1857 struct ipmi_system_interface_addr *smi_addr;
1860 /* Responses are not allowed to the SMI. */
1863 smi_addr = (struct ipmi_system_interface_addr *) addr;
1864 if (smi_addr->lun > 3) {
1865 ipmi_inc_stat(intf, sent_invalid_commands);
1869 memcpy(&recv_msg->addr, smi_addr, sizeof(*smi_addr));
1871 if ((msg->netfn == IPMI_NETFN_APP_REQUEST)
1872 && ((msg->cmd == IPMI_SEND_MSG_CMD)
1873 || (msg->cmd == IPMI_GET_MSG_CMD)
1874 || (msg->cmd == IPMI_READ_EVENT_MSG_BUFFER_CMD))) {
1876 * We don't let the user do these, since we manage
1877 * the sequence numbers.
1879 ipmi_inc_stat(intf, sent_invalid_commands);
1883 if (is_maintenance_mode_cmd(msg)) {
1884 unsigned long flags;
1886 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
1887 intf->auto_maintenance_timeout
1888 = maintenance_mode_timeout_ms;
1889 if (!intf->maintenance_mode
1890 && !intf->maintenance_mode_enable) {
1891 intf->maintenance_mode_enable = true;
1892 maintenance_mode_update(intf);
1894 spin_unlock_irqrestore(&intf->maintenance_mode_lock,
1898 if (msg->data_len + 2 > IPMI_MAX_MSG_LENGTH) {
1899 ipmi_inc_stat(intf, sent_invalid_commands);
1903 smi_msg->data[0] = (msg->netfn << 2) | (smi_addr->lun & 0x3);
1904 smi_msg->data[1] = msg->cmd;
1905 smi_msg->msgid = msgid;
1906 smi_msg->user_data = recv_msg;
1907 if (msg->data_len > 0)
1908 memcpy(&smi_msg->data[2], msg->data, msg->data_len);
1909 smi_msg->data_size = msg->data_len + 2;
1910 ipmi_inc_stat(intf, sent_local_commands);
1915 static int i_ipmi_req_ipmb(struct ipmi_smi *intf,
1916 struct ipmi_addr *addr,
1918 struct kernel_ipmi_msg *msg,
1919 struct ipmi_smi_msg *smi_msg,
1920 struct ipmi_recv_msg *recv_msg,
1921 unsigned char source_address,
1922 unsigned char source_lun,
1924 unsigned int retry_time_ms)
1926 struct ipmi_ipmb_addr *ipmb_addr;
1927 unsigned char ipmb_seq;
1930 struct ipmi_channel *chans;
1933 if (addr->channel >= IPMI_MAX_CHANNELS) {
1934 ipmi_inc_stat(intf, sent_invalid_commands);
1938 chans = READ_ONCE(intf->channel_list)->c;
1940 if (chans[addr->channel].medium != IPMI_CHANNEL_MEDIUM_IPMB) {
1941 ipmi_inc_stat(intf, sent_invalid_commands);
1945 if (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE) {
1947 * Broadcasts add a zero at the beginning of the
1948 * message, but otherwise is the same as an IPMB
1951 addr->addr_type = IPMI_IPMB_ADDR_TYPE;
1953 retries = 0; /* Don't retry broadcasts. */
1957 * 9 for the header and 1 for the checksum, plus
1958 * possibly one for the broadcast.
1960 if ((msg->data_len + 10 + broadcast) > IPMI_MAX_MSG_LENGTH) {
1961 ipmi_inc_stat(intf, sent_invalid_commands);
1965 ipmb_addr = (struct ipmi_ipmb_addr *) addr;
1966 if (ipmb_addr->lun > 3) {
1967 ipmi_inc_stat(intf, sent_invalid_commands);
1971 memcpy(&recv_msg->addr, ipmb_addr, sizeof(*ipmb_addr));
1973 if (recv_msg->msg.netfn & 0x1) {
1975 * It's a response, so use the user's sequence
1978 ipmi_inc_stat(intf, sent_ipmb_responses);
1979 format_ipmb_msg(smi_msg, msg, ipmb_addr, msgid,
1981 source_address, source_lun);
1984 * Save the receive message so we can use it
1985 * to deliver the response.
1987 smi_msg->user_data = recv_msg;
1989 /* It's a command, so get a sequence for it. */
1990 unsigned long flags;
1992 spin_lock_irqsave(&intf->seq_lock, flags);
1994 if (is_maintenance_mode_cmd(msg))
1995 intf->ipmb_maintenance_mode_timeout =
1996 maintenance_mode_timeout_ms;
1998 if (intf->ipmb_maintenance_mode_timeout && retry_time_ms == 0)
1999 /* Different default in maintenance mode */
2000 retry_time_ms = default_maintenance_retry_ms;
2003 * Create a sequence number with a 1 second
2004 * timeout and 4 retries.
2006 rv = intf_next_seq(intf,
2015 * We have used up all the sequence numbers,
2016 * probably, so abort.
2020 ipmi_inc_stat(intf, sent_ipmb_commands);
2023 * Store the sequence number in the message,
2024 * so that when the send message response
2025 * comes back we can start the timer.
2027 format_ipmb_msg(smi_msg, msg, ipmb_addr,
2028 STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
2029 ipmb_seq, broadcast,
2030 source_address, source_lun);
2033 * Copy the message into the recv message data, so we
2034 * can retransmit it later if necessary.
2036 memcpy(recv_msg->msg_data, smi_msg->data,
2037 smi_msg->data_size);
2038 recv_msg->msg.data = recv_msg->msg_data;
2039 recv_msg->msg.data_len = smi_msg->data_size;
2042 * We don't unlock until here, because we need
2043 * to copy the completed message into the
2044 * recv_msg before we release the lock.
2045 * Otherwise, race conditions may bite us. I
2046 * know that's pretty paranoid, but I prefer
2050 spin_unlock_irqrestore(&intf->seq_lock, flags);
2056 static int i_ipmi_req_lan(struct ipmi_smi *intf,
2057 struct ipmi_addr *addr,
2059 struct kernel_ipmi_msg *msg,
2060 struct ipmi_smi_msg *smi_msg,
2061 struct ipmi_recv_msg *recv_msg,
2062 unsigned char source_lun,
2064 unsigned int retry_time_ms)
2066 struct ipmi_lan_addr *lan_addr;
2067 unsigned char ipmb_seq;
2069 struct ipmi_channel *chans;
2072 if (addr->channel >= IPMI_MAX_CHANNELS) {
2073 ipmi_inc_stat(intf, sent_invalid_commands);
2077 chans = READ_ONCE(intf->channel_list)->c;
2079 if ((chans[addr->channel].medium
2080 != IPMI_CHANNEL_MEDIUM_8023LAN)
2081 && (chans[addr->channel].medium
2082 != IPMI_CHANNEL_MEDIUM_ASYNC)) {
2083 ipmi_inc_stat(intf, sent_invalid_commands);
2087 /* 11 for the header and 1 for the checksum. */
2088 if ((msg->data_len + 12) > IPMI_MAX_MSG_LENGTH) {
2089 ipmi_inc_stat(intf, sent_invalid_commands);
2093 lan_addr = (struct ipmi_lan_addr *) addr;
2094 if (lan_addr->lun > 3) {
2095 ipmi_inc_stat(intf, sent_invalid_commands);
2099 memcpy(&recv_msg->addr, lan_addr, sizeof(*lan_addr));
2101 if (recv_msg->msg.netfn & 0x1) {
2103 * It's a response, so use the user's sequence
2106 ipmi_inc_stat(intf, sent_lan_responses);
2107 format_lan_msg(smi_msg, msg, lan_addr, msgid,
2111 * Save the receive message so we can use it
2112 * to deliver the response.
2114 smi_msg->user_data = recv_msg;
2116 /* It's a command, so get a sequence for it. */
2117 unsigned long flags;
2119 spin_lock_irqsave(&intf->seq_lock, flags);
2122 * Create a sequence number with a 1 second
2123 * timeout and 4 retries.
2125 rv = intf_next_seq(intf,
2134 * We have used up all the sequence numbers,
2135 * probably, so abort.
2139 ipmi_inc_stat(intf, sent_lan_commands);
2142 * Store the sequence number in the message,
2143 * so that when the send message response
2144 * comes back we can start the timer.
2146 format_lan_msg(smi_msg, msg, lan_addr,
2147 STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
2148 ipmb_seq, source_lun);
2151 * Copy the message into the recv message data, so we
2152 * can retransmit it later if necessary.
2154 memcpy(recv_msg->msg_data, smi_msg->data,
2155 smi_msg->data_size);
2156 recv_msg->msg.data = recv_msg->msg_data;
2157 recv_msg->msg.data_len = smi_msg->data_size;
2160 * We don't unlock until here, because we need
2161 * to copy the completed message into the
2162 * recv_msg before we release the lock.
2163 * Otherwise, race conditions may bite us. I
2164 * know that's pretty paranoid, but I prefer
2168 spin_unlock_irqrestore(&intf->seq_lock, flags);
2175 * Separate from ipmi_request so that the user does not have to be
2176 * supplied in certain circumstances (mainly at panic time). If
2177 * messages are supplied, they will be freed, even if an error
2180 static int i_ipmi_request(struct ipmi_user *user,
2181 struct ipmi_smi *intf,
2182 struct ipmi_addr *addr,
2184 struct kernel_ipmi_msg *msg,
2185 void *user_msg_data,
2187 struct ipmi_recv_msg *supplied_recv,
2189 unsigned char source_address,
2190 unsigned char source_lun,
2192 unsigned int retry_time_ms)
2194 struct ipmi_smi_msg *smi_msg;
2195 struct ipmi_recv_msg *recv_msg;
2199 recv_msg = supplied_recv;
2201 recv_msg = ipmi_alloc_recv_msg();
2202 if (recv_msg == NULL) {
2207 recv_msg->user_msg_data = user_msg_data;
2210 smi_msg = (struct ipmi_smi_msg *) supplied_smi;
2212 smi_msg = ipmi_alloc_smi_msg();
2213 if (smi_msg == NULL) {
2215 ipmi_free_recv_msg(recv_msg);
2222 if (intf->in_shutdown) {
2227 recv_msg->user = user;
2229 /* The put happens when the message is freed. */
2230 kref_get(&user->refcount);
2231 recv_msg->msgid = msgid;
2233 * Store the message to send in the receive message so timeout
2234 * responses can get the proper response data.
2236 recv_msg->msg = *msg;
2238 if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
2239 rv = i_ipmi_req_sysintf(intf, addr, msgid, msg, smi_msg,
2240 recv_msg, retries, retry_time_ms);
2241 } else if (is_ipmb_addr(addr) || is_ipmb_bcast_addr(addr)) {
2242 rv = i_ipmi_req_ipmb(intf, addr, msgid, msg, smi_msg, recv_msg,
2243 source_address, source_lun,
2244 retries, retry_time_ms);
2245 } else if (is_lan_addr(addr)) {
2246 rv = i_ipmi_req_lan(intf, addr, msgid, msg, smi_msg, recv_msg,
2247 source_lun, retries, retry_time_ms);
2249 /* Unknown address type. */
2250 ipmi_inc_stat(intf, sent_invalid_commands);
2256 ipmi_free_smi_msg(smi_msg);
2257 ipmi_free_recv_msg(recv_msg);
2259 pr_debug("Send: %*ph\n", smi_msg->data_size, smi_msg->data);
2261 smi_send(intf, intf->handlers, smi_msg, priority);
2269 static int check_addr(struct ipmi_smi *intf,
2270 struct ipmi_addr *addr,
2271 unsigned char *saddr,
2274 if (addr->channel >= IPMI_MAX_CHANNELS)
2276 addr->channel = array_index_nospec(addr->channel, IPMI_MAX_CHANNELS);
2277 *lun = intf->addrinfo[addr->channel].lun;
2278 *saddr = intf->addrinfo[addr->channel].address;
2282 int ipmi_request_settime(struct ipmi_user *user,
2283 struct ipmi_addr *addr,
2285 struct kernel_ipmi_msg *msg,
2286 void *user_msg_data,
2289 unsigned int retry_time_ms)
2291 unsigned char saddr = 0, lun = 0;
2297 user = acquire_ipmi_user(user, &index);
2301 rv = check_addr(user->intf, addr, &saddr, &lun);
2303 rv = i_ipmi_request(user,
2316 release_ipmi_user(user, index);
2319 EXPORT_SYMBOL(ipmi_request_settime);
2321 int ipmi_request_supply_msgs(struct ipmi_user *user,
2322 struct ipmi_addr *addr,
2324 struct kernel_ipmi_msg *msg,
2325 void *user_msg_data,
2327 struct ipmi_recv_msg *supplied_recv,
2330 unsigned char saddr = 0, lun = 0;
2336 user = acquire_ipmi_user(user, &index);
2340 rv = check_addr(user->intf, addr, &saddr, &lun);
2342 rv = i_ipmi_request(user,
2355 release_ipmi_user(user, index);
2358 EXPORT_SYMBOL(ipmi_request_supply_msgs);
2360 static void bmc_device_id_handler(struct ipmi_smi *intf,
2361 struct ipmi_recv_msg *msg)
2365 if ((msg->addr.addr_type != IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
2366 || (msg->msg.netfn != IPMI_NETFN_APP_RESPONSE)
2367 || (msg->msg.cmd != IPMI_GET_DEVICE_ID_CMD)) {
2368 dev_warn(intf->si_dev,
2369 "invalid device_id msg: addr_type=%d netfn=%x cmd=%x\n",
2370 msg->addr.addr_type, msg->msg.netfn, msg->msg.cmd);
2374 rv = ipmi_demangle_device_id(msg->msg.netfn, msg->msg.cmd,
2375 msg->msg.data, msg->msg.data_len, &intf->bmc->fetch_id);
2377 dev_warn(intf->si_dev, "device id demangle failed: %d\n", rv);
2378 intf->bmc->dyn_id_set = 0;
2381 * Make sure the id data is available before setting
2385 intf->bmc->dyn_id_set = 1;
2388 wake_up(&intf->waitq);
2392 send_get_device_id_cmd(struct ipmi_smi *intf)
2394 struct ipmi_system_interface_addr si;
2395 struct kernel_ipmi_msg msg;
2397 si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2398 si.channel = IPMI_BMC_CHANNEL;
2401 msg.netfn = IPMI_NETFN_APP_REQUEST;
2402 msg.cmd = IPMI_GET_DEVICE_ID_CMD;
2406 return i_ipmi_request(NULL,
2408 (struct ipmi_addr *) &si,
2415 intf->addrinfo[0].address,
2416 intf->addrinfo[0].lun,
2420 static int __get_device_id(struct ipmi_smi *intf, struct bmc_device *bmc)
2424 bmc->dyn_id_set = 2;
2426 intf->null_user_handler = bmc_device_id_handler;
2428 rv = send_get_device_id_cmd(intf);
2432 wait_event(intf->waitq, bmc->dyn_id_set != 2);
2434 if (!bmc->dyn_id_set)
2435 rv = -EIO; /* Something went wrong in the fetch. */
2437 /* dyn_id_set makes the id data available. */
2440 intf->null_user_handler = NULL;
2446 * Fetch the device id for the bmc/interface. You must pass in either
2447 * bmc or intf, this code will get the other one. If the data has
2448 * been recently fetched, this will just use the cached data. Otherwise
2449 * it will run a new fetch.
2451 * Except for the first time this is called (in ipmi_add_smi()),
2452 * this will always return good data;
2454 static int __bmc_get_device_id(struct ipmi_smi *intf, struct bmc_device *bmc,
2455 struct ipmi_device_id *id,
2456 bool *guid_set, guid_t *guid, int intf_num)
2459 int prev_dyn_id_set, prev_guid_set;
2460 bool intf_set = intf != NULL;
2463 mutex_lock(&bmc->dyn_mutex);
2465 if (list_empty(&bmc->intfs)) {
2466 mutex_unlock(&bmc->dyn_mutex);
2469 intf = list_first_entry(&bmc->intfs, struct ipmi_smi,
2471 kref_get(&intf->refcount);
2472 mutex_unlock(&bmc->dyn_mutex);
2473 mutex_lock(&intf->bmc_reg_mutex);
2474 mutex_lock(&bmc->dyn_mutex);
2475 if (intf != list_first_entry(&bmc->intfs, struct ipmi_smi,
2477 mutex_unlock(&intf->bmc_reg_mutex);
2478 kref_put(&intf->refcount, intf_free);
2479 goto retry_bmc_lock;
2482 mutex_lock(&intf->bmc_reg_mutex);
2484 mutex_lock(&bmc->dyn_mutex);
2485 kref_get(&intf->refcount);
2488 /* If we have a valid and current ID, just return that. */
2489 if (intf->in_bmc_register ||
2490 (bmc->dyn_id_set && time_is_after_jiffies(bmc->dyn_id_expiry)))
2491 goto out_noprocessing;
2493 prev_guid_set = bmc->dyn_guid_set;
2496 prev_dyn_id_set = bmc->dyn_id_set;
2497 rv = __get_device_id(intf, bmc);
2502 * The guid, device id, manufacturer id, and product id should
2503 * not change on a BMC. If it does we have to do some dancing.
2505 if (!intf->bmc_registered
2506 || (!prev_guid_set && bmc->dyn_guid_set)
2507 || (!prev_dyn_id_set && bmc->dyn_id_set)
2508 || (prev_guid_set && bmc->dyn_guid_set
2509 && !guid_equal(&bmc->guid, &bmc->fetch_guid))
2510 || bmc->id.device_id != bmc->fetch_id.device_id
2511 || bmc->id.manufacturer_id != bmc->fetch_id.manufacturer_id
2512 || bmc->id.product_id != bmc->fetch_id.product_id) {
2513 struct ipmi_device_id id = bmc->fetch_id;
2514 int guid_set = bmc->dyn_guid_set;
2517 guid = bmc->fetch_guid;
2518 mutex_unlock(&bmc->dyn_mutex);
2520 __ipmi_bmc_unregister(intf);
2521 /* Fill in the temporary BMC for good measure. */
2523 intf->bmc->dyn_guid_set = guid_set;
2524 intf->bmc->guid = guid;
2525 if (__ipmi_bmc_register(intf, &id, guid_set, &guid, intf_num))
2526 need_waiter(intf); /* Retry later on an error. */
2528 __scan_channels(intf, &id);
2533 * We weren't given the interface on the
2534 * command line, so restart the operation on
2535 * the next interface for the BMC.
2537 mutex_unlock(&intf->bmc_reg_mutex);
2538 mutex_lock(&bmc->dyn_mutex);
2539 goto retry_bmc_lock;
2542 /* We have a new BMC, set it up. */
2544 mutex_lock(&bmc->dyn_mutex);
2545 goto out_noprocessing;
2546 } else if (memcmp(&bmc->fetch_id, &bmc->id, sizeof(bmc->id)))
2547 /* Version info changes, scan the channels again. */
2548 __scan_channels(intf, &bmc->fetch_id);
2550 bmc->dyn_id_expiry = jiffies + IPMI_DYN_DEV_ID_EXPIRY;
2553 if (rv && prev_dyn_id_set) {
2554 rv = 0; /* Ignore failures if we have previous data. */
2555 bmc->dyn_id_set = prev_dyn_id_set;
2558 bmc->id = bmc->fetch_id;
2559 if (bmc->dyn_guid_set)
2560 bmc->guid = bmc->fetch_guid;
2561 else if (prev_guid_set)
2563 * The guid used to be valid and it failed to fetch,
2564 * just use the cached value.
2566 bmc->dyn_guid_set = prev_guid_set;
2574 *guid_set = bmc->dyn_guid_set;
2576 if (guid && bmc->dyn_guid_set)
2580 mutex_unlock(&bmc->dyn_mutex);
2581 mutex_unlock(&intf->bmc_reg_mutex);
2583 kref_put(&intf->refcount, intf_free);
2587 static int bmc_get_device_id(struct ipmi_smi *intf, struct bmc_device *bmc,
2588 struct ipmi_device_id *id,
2589 bool *guid_set, guid_t *guid)
2591 return __bmc_get_device_id(intf, bmc, id, guid_set, guid, -1);
2594 static ssize_t device_id_show(struct device *dev,
2595 struct device_attribute *attr,
2598 struct bmc_device *bmc = to_bmc_device(dev);
2599 struct ipmi_device_id id;
2602 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2606 return snprintf(buf, 10, "%u\n", id.device_id);
2608 static DEVICE_ATTR_RO(device_id);
2610 static ssize_t provides_device_sdrs_show(struct device *dev,
2611 struct device_attribute *attr,
2614 struct bmc_device *bmc = to_bmc_device(dev);
2615 struct ipmi_device_id id;
2618 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2622 return snprintf(buf, 10, "%u\n", (id.device_revision & 0x80) >> 7);
2624 static DEVICE_ATTR_RO(provides_device_sdrs);
2626 static ssize_t revision_show(struct device *dev, struct device_attribute *attr,
2629 struct bmc_device *bmc = to_bmc_device(dev);
2630 struct ipmi_device_id id;
2633 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2637 return snprintf(buf, 20, "%u\n", id.device_revision & 0x0F);
2639 static DEVICE_ATTR_RO(revision);
2641 static ssize_t firmware_revision_show(struct device *dev,
2642 struct device_attribute *attr,
2645 struct bmc_device *bmc = to_bmc_device(dev);
2646 struct ipmi_device_id id;
2649 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2653 return snprintf(buf, 20, "%u.%x\n", id.firmware_revision_1,
2654 id.firmware_revision_2);
2656 static DEVICE_ATTR_RO(firmware_revision);
2658 static ssize_t ipmi_version_show(struct device *dev,
2659 struct device_attribute *attr,
2662 struct bmc_device *bmc = to_bmc_device(dev);
2663 struct ipmi_device_id id;
2666 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2670 return snprintf(buf, 20, "%u.%u\n",
2671 ipmi_version_major(&id),
2672 ipmi_version_minor(&id));
2674 static DEVICE_ATTR_RO(ipmi_version);
2676 static ssize_t add_dev_support_show(struct device *dev,
2677 struct device_attribute *attr,
2680 struct bmc_device *bmc = to_bmc_device(dev);
2681 struct ipmi_device_id id;
2684 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2688 return snprintf(buf, 10, "0x%02x\n", id.additional_device_support);
2690 static DEVICE_ATTR(additional_device_support, S_IRUGO, add_dev_support_show,
2693 static ssize_t manufacturer_id_show(struct device *dev,
2694 struct device_attribute *attr,
2697 struct bmc_device *bmc = to_bmc_device(dev);
2698 struct ipmi_device_id id;
2701 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2705 return snprintf(buf, 20, "0x%6.6x\n", id.manufacturer_id);
2707 static DEVICE_ATTR_RO(manufacturer_id);
2709 static ssize_t product_id_show(struct device *dev,
2710 struct device_attribute *attr,
2713 struct bmc_device *bmc = to_bmc_device(dev);
2714 struct ipmi_device_id id;
2717 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2721 return snprintf(buf, 10, "0x%4.4x\n", id.product_id);
2723 static DEVICE_ATTR_RO(product_id);
2725 static ssize_t aux_firmware_rev_show(struct device *dev,
2726 struct device_attribute *attr,
2729 struct bmc_device *bmc = to_bmc_device(dev);
2730 struct ipmi_device_id id;
2733 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2737 return snprintf(buf, 21, "0x%02x 0x%02x 0x%02x 0x%02x\n",
2738 id.aux_firmware_revision[3],
2739 id.aux_firmware_revision[2],
2740 id.aux_firmware_revision[1],
2741 id.aux_firmware_revision[0]);
2743 static DEVICE_ATTR(aux_firmware_revision, S_IRUGO, aux_firmware_rev_show, NULL);
2745 static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
2748 struct bmc_device *bmc = to_bmc_device(dev);
2753 rv = bmc_get_device_id(NULL, bmc, NULL, &guid_set, &guid);
2759 return snprintf(buf, UUID_STRING_LEN + 1 + 1, "%pUl\n", &guid);
2761 static DEVICE_ATTR_RO(guid);
2763 static struct attribute *bmc_dev_attrs[] = {
2764 &dev_attr_device_id.attr,
2765 &dev_attr_provides_device_sdrs.attr,
2766 &dev_attr_revision.attr,
2767 &dev_attr_firmware_revision.attr,
2768 &dev_attr_ipmi_version.attr,
2769 &dev_attr_additional_device_support.attr,
2770 &dev_attr_manufacturer_id.attr,
2771 &dev_attr_product_id.attr,
2772 &dev_attr_aux_firmware_revision.attr,
2773 &dev_attr_guid.attr,
2777 static umode_t bmc_dev_attr_is_visible(struct kobject *kobj,
2778 struct attribute *attr, int idx)
2780 struct device *dev = kobj_to_dev(kobj);
2781 struct bmc_device *bmc = to_bmc_device(dev);
2782 umode_t mode = attr->mode;
2785 if (attr == &dev_attr_aux_firmware_revision.attr) {
2786 struct ipmi_device_id id;
2788 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2789 return (!rv && id.aux_firmware_revision_set) ? mode : 0;
2791 if (attr == &dev_attr_guid.attr) {
2794 rv = bmc_get_device_id(NULL, bmc, NULL, &guid_set, NULL);
2795 return (!rv && guid_set) ? mode : 0;
2800 static const struct attribute_group bmc_dev_attr_group = {
2801 .attrs = bmc_dev_attrs,
2802 .is_visible = bmc_dev_attr_is_visible,
2805 static const struct attribute_group *bmc_dev_attr_groups[] = {
2806 &bmc_dev_attr_group,
2810 static const struct device_type bmc_device_type = {
2811 .groups = bmc_dev_attr_groups,
2814 static int __find_bmc_guid(struct device *dev, const void *data)
2816 const guid_t *guid = data;
2817 struct bmc_device *bmc;
2820 if (dev->type != &bmc_device_type)
2823 bmc = to_bmc_device(dev);
2824 rv = bmc->dyn_guid_set && guid_equal(&bmc->guid, guid);
2826 rv = kref_get_unless_zero(&bmc->usecount);
2831 * Returns with the bmc's usecount incremented, if it is non-NULL.
2833 static struct bmc_device *ipmi_find_bmc_guid(struct device_driver *drv,
2837 struct bmc_device *bmc = NULL;
2839 dev = driver_find_device(drv, NULL, guid, __find_bmc_guid);
2841 bmc = to_bmc_device(dev);
2847 struct prod_dev_id {
2848 unsigned int product_id;
2849 unsigned char device_id;
2852 static int __find_bmc_prod_dev_id(struct device *dev, const void *data)
2854 const struct prod_dev_id *cid = data;
2855 struct bmc_device *bmc;
2858 if (dev->type != &bmc_device_type)
2861 bmc = to_bmc_device(dev);
2862 rv = (bmc->id.product_id == cid->product_id
2863 && bmc->id.device_id == cid->device_id);
2865 rv = kref_get_unless_zero(&bmc->usecount);
2870 * Returns with the bmc's usecount incremented, if it is non-NULL.
2872 static struct bmc_device *ipmi_find_bmc_prod_dev_id(
2873 struct device_driver *drv,
2874 unsigned int product_id, unsigned char device_id)
2876 struct prod_dev_id id = {
2877 .product_id = product_id,
2878 .device_id = device_id,
2881 struct bmc_device *bmc = NULL;
2883 dev = driver_find_device(drv, NULL, &id, __find_bmc_prod_dev_id);
2885 bmc = to_bmc_device(dev);
2891 static DEFINE_IDA(ipmi_bmc_ida);
2894 release_bmc_device(struct device *dev)
2896 kfree(to_bmc_device(dev));
2899 static void cleanup_bmc_work(struct work_struct *work)
2901 struct bmc_device *bmc = container_of(work, struct bmc_device,
2903 int id = bmc->pdev.id; /* Unregister overwrites id */
2905 platform_device_unregister(&bmc->pdev);
2906 ida_simple_remove(&ipmi_bmc_ida, id);
2910 cleanup_bmc_device(struct kref *ref)
2912 struct bmc_device *bmc = container_of(ref, struct bmc_device, usecount);
2915 * Remove the platform device in a work queue to avoid issues
2916 * with removing the device attributes while reading a device
2919 schedule_work(&bmc->remove_work);
2923 * Must be called with intf->bmc_reg_mutex held.
2925 static void __ipmi_bmc_unregister(struct ipmi_smi *intf)
2927 struct bmc_device *bmc = intf->bmc;
2929 if (!intf->bmc_registered)
2932 sysfs_remove_link(&intf->si_dev->kobj, "bmc");
2933 sysfs_remove_link(&bmc->pdev.dev.kobj, intf->my_dev_name);
2934 kfree(intf->my_dev_name);
2935 intf->my_dev_name = NULL;
2937 mutex_lock(&bmc->dyn_mutex);
2938 list_del(&intf->bmc_link);
2939 mutex_unlock(&bmc->dyn_mutex);
2940 intf->bmc = &intf->tmp_bmc;
2941 kref_put(&bmc->usecount, cleanup_bmc_device);
2942 intf->bmc_registered = false;
2945 static void ipmi_bmc_unregister(struct ipmi_smi *intf)
2947 mutex_lock(&intf->bmc_reg_mutex);
2948 __ipmi_bmc_unregister(intf);
2949 mutex_unlock(&intf->bmc_reg_mutex);
2953 * Must be called with intf->bmc_reg_mutex held.
2955 static int __ipmi_bmc_register(struct ipmi_smi *intf,
2956 struct ipmi_device_id *id,
2957 bool guid_set, guid_t *guid, int intf_num)
2960 struct bmc_device *bmc;
2961 struct bmc_device *old_bmc;
2964 * platform_device_register() can cause bmc_reg_mutex to
2965 * be claimed because of the is_visible functions of
2966 * the attributes. Eliminate possible recursion and
2969 intf->in_bmc_register = true;
2970 mutex_unlock(&intf->bmc_reg_mutex);
2973 * Try to find if there is an bmc_device struct
2974 * representing the interfaced BMC already
2976 mutex_lock(&ipmidriver_mutex);
2978 old_bmc = ipmi_find_bmc_guid(&ipmidriver.driver, guid);
2980 old_bmc = ipmi_find_bmc_prod_dev_id(&ipmidriver.driver,
2985 * If there is already an bmc_device, free the new one,
2986 * otherwise register the new BMC device
2991 * Note: old_bmc already has usecount incremented by
2992 * the BMC find functions.
2994 intf->bmc = old_bmc;
2995 mutex_lock(&bmc->dyn_mutex);
2996 list_add_tail(&intf->bmc_link, &bmc->intfs);
2997 mutex_unlock(&bmc->dyn_mutex);
2999 dev_info(intf->si_dev,
3000 "interfacing existing BMC (man_id: 0x%6.6x, prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
3001 bmc->id.manufacturer_id,
3005 bmc = kzalloc(sizeof(*bmc), GFP_KERNEL);
3010 INIT_LIST_HEAD(&bmc->intfs);
3011 mutex_init(&bmc->dyn_mutex);
3012 INIT_WORK(&bmc->remove_work, cleanup_bmc_work);
3015 bmc->dyn_id_set = 1;
3016 bmc->dyn_guid_set = guid_set;
3018 bmc->dyn_id_expiry = jiffies + IPMI_DYN_DEV_ID_EXPIRY;
3020 bmc->pdev.name = "ipmi_bmc";
3022 rv = ida_simple_get(&ipmi_bmc_ida, 0, 0, GFP_KERNEL);
3028 bmc->pdev.dev.driver = &ipmidriver.driver;
3030 bmc->pdev.dev.release = release_bmc_device;
3031 bmc->pdev.dev.type = &bmc_device_type;
3032 kref_init(&bmc->usecount);
3035 mutex_lock(&bmc->dyn_mutex);
3036 list_add_tail(&intf->bmc_link, &bmc->intfs);
3037 mutex_unlock(&bmc->dyn_mutex);
3039 rv = platform_device_register(&bmc->pdev);
3041 dev_err(intf->si_dev,
3042 "Unable to register bmc device: %d\n",
3047 dev_info(intf->si_dev,
3048 "Found new BMC (man_id: 0x%6.6x, prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
3049 bmc->id.manufacturer_id,
3055 * create symlink from system interface device to bmc device
3058 rv = sysfs_create_link(&intf->si_dev->kobj, &bmc->pdev.dev.kobj, "bmc");
3060 dev_err(intf->si_dev, "Unable to create bmc symlink: %d\n", rv);
3065 intf_num = intf->intf_num;
3066 intf->my_dev_name = kasprintf(GFP_KERNEL, "ipmi%d", intf_num);
3067 if (!intf->my_dev_name) {
3069 dev_err(intf->si_dev, "Unable to allocate link from BMC: %d\n",
3074 rv = sysfs_create_link(&bmc->pdev.dev.kobj, &intf->si_dev->kobj,
3077 kfree(intf->my_dev_name);
3078 intf->my_dev_name = NULL;
3079 dev_err(intf->si_dev, "Unable to create symlink to bmc: %d\n",
3081 goto out_free_my_dev_name;
3084 intf->bmc_registered = true;
3087 mutex_unlock(&ipmidriver_mutex);
3088 mutex_lock(&intf->bmc_reg_mutex);
3089 intf->in_bmc_register = false;
3093 out_free_my_dev_name:
3094 kfree(intf->my_dev_name);
3095 intf->my_dev_name = NULL;
3098 sysfs_remove_link(&intf->si_dev->kobj, "bmc");
3101 mutex_lock(&bmc->dyn_mutex);
3102 list_del(&intf->bmc_link);
3103 mutex_unlock(&bmc->dyn_mutex);
3104 intf->bmc = &intf->tmp_bmc;
3105 kref_put(&bmc->usecount, cleanup_bmc_device);
3109 mutex_lock(&bmc->dyn_mutex);
3110 list_del(&intf->bmc_link);
3111 mutex_unlock(&bmc->dyn_mutex);
3112 intf->bmc = &intf->tmp_bmc;
3113 put_device(&bmc->pdev.dev);
3118 send_guid_cmd(struct ipmi_smi *intf, int chan)
3120 struct kernel_ipmi_msg msg;
3121 struct ipmi_system_interface_addr si;
3123 si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3124 si.channel = IPMI_BMC_CHANNEL;
3127 msg.netfn = IPMI_NETFN_APP_REQUEST;
3128 msg.cmd = IPMI_GET_DEVICE_GUID_CMD;
3131 return i_ipmi_request(NULL,
3133 (struct ipmi_addr *) &si,
3140 intf->addrinfo[0].address,
3141 intf->addrinfo[0].lun,
3145 static void guid_handler(struct ipmi_smi *intf, struct ipmi_recv_msg *msg)
3147 struct bmc_device *bmc = intf->bmc;
3149 if ((msg->addr.addr_type != IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
3150 || (msg->msg.netfn != IPMI_NETFN_APP_RESPONSE)
3151 || (msg->msg.cmd != IPMI_GET_DEVICE_GUID_CMD))
3155 if (msg->msg.data[0] != 0) {
3156 /* Error from getting the GUID, the BMC doesn't have one. */
3157 bmc->dyn_guid_set = 0;
3161 if (msg->msg.data_len < UUID_SIZE + 1) {
3162 bmc->dyn_guid_set = 0;
3163 dev_warn(intf->si_dev,
3164 "The GUID response from the BMC was too short, it was %d but should have been %d. Assuming GUID is not available.\n",
3165 msg->msg.data_len, UUID_SIZE + 1);
3169 guid_copy(&bmc->fetch_guid, (guid_t *)(msg->msg.data + 1));
3171 * Make sure the guid data is available before setting
3175 bmc->dyn_guid_set = 1;
3177 wake_up(&intf->waitq);
3180 static void __get_guid(struct ipmi_smi *intf)
3183 struct bmc_device *bmc = intf->bmc;
3185 bmc->dyn_guid_set = 2;
3186 intf->null_user_handler = guid_handler;
3187 rv = send_guid_cmd(intf, 0);
3189 /* Send failed, no GUID available. */
3190 bmc->dyn_guid_set = 0;
3192 wait_event(intf->waitq, bmc->dyn_guid_set != 2);
3194 /* dyn_guid_set makes the guid data available. */
3197 intf->null_user_handler = NULL;
3201 send_channel_info_cmd(struct ipmi_smi *intf, int chan)
3203 struct kernel_ipmi_msg msg;
3204 unsigned char data[1];
3205 struct ipmi_system_interface_addr si;
3207 si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3208 si.channel = IPMI_BMC_CHANNEL;
3211 msg.netfn = IPMI_NETFN_APP_REQUEST;
3212 msg.cmd = IPMI_GET_CHANNEL_INFO_CMD;
3216 return i_ipmi_request(NULL,
3218 (struct ipmi_addr *) &si,
3225 intf->addrinfo[0].address,
3226 intf->addrinfo[0].lun,
3231 channel_handler(struct ipmi_smi *intf, struct ipmi_recv_msg *msg)
3235 unsigned int set = intf->curr_working_cset;
3236 struct ipmi_channel *chans;
3238 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
3239 && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
3240 && (msg->msg.cmd == IPMI_GET_CHANNEL_INFO_CMD)) {
3241 /* It's the one we want */
3242 if (msg->msg.data[0] != 0) {
3243 /* Got an error from the channel, just go on. */
3245 if (msg->msg.data[0] == IPMI_INVALID_COMMAND_ERR) {
3247 * If the MC does not support this
3248 * command, that is legal. We just
3249 * assume it has one IPMB at channel
3252 intf->wchannels[set].c[0].medium
3253 = IPMI_CHANNEL_MEDIUM_IPMB;
3254 intf->wchannels[set].c[0].protocol
3255 = IPMI_CHANNEL_PROTOCOL_IPMB;
3257 intf->channel_list = intf->wchannels + set;
3258 intf->channels_ready = true;
3259 wake_up(&intf->waitq);
3264 if (msg->msg.data_len < 4) {
3265 /* Message not big enough, just go on. */
3268 ch = intf->curr_channel;
3269 chans = intf->wchannels[set].c;
3270 chans[ch].medium = msg->msg.data[2] & 0x7f;
3271 chans[ch].protocol = msg->msg.data[3] & 0x1f;
3274 intf->curr_channel++;
3275 if (intf->curr_channel >= IPMI_MAX_CHANNELS) {
3276 intf->channel_list = intf->wchannels + set;
3277 intf->channels_ready = true;
3278 wake_up(&intf->waitq);
3280 intf->channel_list = intf->wchannels + set;
3281 intf->channels_ready = true;
3282 rv = send_channel_info_cmd(intf, intf->curr_channel);
3286 /* Got an error somehow, just give up. */
3287 dev_warn(intf->si_dev,
3288 "Error sending channel information for channel %d: %d\n",
3289 intf->curr_channel, rv);
3291 intf->channel_list = intf->wchannels + set;
3292 intf->channels_ready = true;
3293 wake_up(&intf->waitq);
3301 * Must be holding intf->bmc_reg_mutex to call this.
3303 static int __scan_channels(struct ipmi_smi *intf, struct ipmi_device_id *id)
3307 if (ipmi_version_major(id) > 1
3308 || (ipmi_version_major(id) == 1
3309 && ipmi_version_minor(id) >= 5)) {
3313 * Start scanning the channels to see what is
3316 set = !intf->curr_working_cset;
3317 intf->curr_working_cset = set;
3318 memset(&intf->wchannels[set], 0,
3319 sizeof(struct ipmi_channel_set));
3321 intf->null_user_handler = channel_handler;
3322 intf->curr_channel = 0;
3323 rv = send_channel_info_cmd(intf, 0);
3325 dev_warn(intf->si_dev,
3326 "Error sending channel information for channel 0, %d\n",
3331 /* Wait for the channel info to be read. */
3332 wait_event(intf->waitq, intf->channels_ready);
3333 intf->null_user_handler = NULL;
3335 unsigned int set = intf->curr_working_cset;
3337 /* Assume a single IPMB channel at zero. */
3338 intf->wchannels[set].c[0].medium = IPMI_CHANNEL_MEDIUM_IPMB;
3339 intf->wchannels[set].c[0].protocol = IPMI_CHANNEL_PROTOCOL_IPMB;
3340 intf->channel_list = intf->wchannels + set;
3341 intf->channels_ready = true;
3347 static void ipmi_poll(struct ipmi_smi *intf)
3349 if (intf->handlers->poll)
3350 intf->handlers->poll(intf->send_info);
3351 /* In case something came in */
3352 handle_new_recv_msgs(intf);
3355 void ipmi_poll_interface(struct ipmi_user *user)
3357 ipmi_poll(user->intf);
3359 EXPORT_SYMBOL(ipmi_poll_interface);
3361 static void redo_bmc_reg(struct work_struct *work)
3363 struct ipmi_smi *intf = container_of(work, struct ipmi_smi,
3366 if (!intf->in_shutdown)
3367 bmc_get_device_id(intf, NULL, NULL, NULL, NULL);
3369 kref_put(&intf->refcount, intf_free);
3372 int ipmi_add_smi(struct module *owner,
3373 const struct ipmi_smi_handlers *handlers,
3375 struct device *si_dev,
3376 unsigned char slave_addr)
3380 struct ipmi_smi *intf, *tintf;
3381 struct list_head *link;
3382 struct ipmi_device_id id;
3385 * Make sure the driver is actually initialized, this handles
3386 * problems with initialization order.
3388 rv = ipmi_init_msghandler();
3392 intf = kzalloc(sizeof(*intf), GFP_KERNEL);
3396 rv = init_srcu_struct(&intf->users_srcu);
3402 intf->owner = owner;
3403 intf->bmc = &intf->tmp_bmc;
3404 INIT_LIST_HEAD(&intf->bmc->intfs);
3405 mutex_init(&intf->bmc->dyn_mutex);
3406 INIT_LIST_HEAD(&intf->bmc_link);
3407 mutex_init(&intf->bmc_reg_mutex);
3408 intf->intf_num = -1; /* Mark it invalid for now. */
3409 kref_init(&intf->refcount);
3410 INIT_WORK(&intf->bmc_reg_work, redo_bmc_reg);
3411 intf->si_dev = si_dev;
3412 for (j = 0; j < IPMI_MAX_CHANNELS; j++) {
3413 intf->addrinfo[j].address = IPMI_BMC_SLAVE_ADDR;
3414 intf->addrinfo[j].lun = 2;
3416 if (slave_addr != 0)
3417 intf->addrinfo[0].address = slave_addr;
3418 INIT_LIST_HEAD(&intf->users);
3419 intf->handlers = handlers;
3420 intf->send_info = send_info;
3421 spin_lock_init(&intf->seq_lock);
3422 for (j = 0; j < IPMI_IPMB_NUM_SEQ; j++) {
3423 intf->seq_table[j].inuse = 0;
3424 intf->seq_table[j].seqid = 0;
3427 spin_lock_init(&intf->waiting_rcv_msgs_lock);
3428 INIT_LIST_HEAD(&intf->waiting_rcv_msgs);
3429 tasklet_init(&intf->recv_tasklet,
3431 (unsigned long) intf);
3432 atomic_set(&intf->watchdog_pretimeouts_to_deliver, 0);
3433 spin_lock_init(&intf->xmit_msgs_lock);
3434 INIT_LIST_HEAD(&intf->xmit_msgs);
3435 INIT_LIST_HEAD(&intf->hp_xmit_msgs);
3436 spin_lock_init(&intf->events_lock);
3437 spin_lock_init(&intf->watch_lock);
3438 atomic_set(&intf->event_waiters, 0);
3439 intf->ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
3440 INIT_LIST_HEAD(&intf->waiting_events);
3441 intf->waiting_events_count = 0;
3442 mutex_init(&intf->cmd_rcvrs_mutex);
3443 spin_lock_init(&intf->maintenance_mode_lock);
3444 INIT_LIST_HEAD(&intf->cmd_rcvrs);
3445 init_waitqueue_head(&intf->waitq);
3446 for (i = 0; i < IPMI_NUM_STATS; i++)
3447 atomic_set(&intf->stats[i], 0);
3449 mutex_lock(&ipmi_interfaces_mutex);
3450 /* Look for a hole in the numbers. */
3452 link = &ipmi_interfaces;
3453 list_for_each_entry_rcu(tintf, &ipmi_interfaces, link) {
3454 if (tintf->intf_num != i) {
3455 link = &tintf->link;
3460 /* Add the new interface in numeric order. */
3462 list_add_rcu(&intf->link, &ipmi_interfaces);
3464 list_add_tail_rcu(&intf->link, link);
3466 rv = handlers->start_processing(send_info, intf);
3470 rv = __bmc_get_device_id(intf, NULL, &id, NULL, NULL, i);
3472 dev_err(si_dev, "Unable to get the device id: %d\n", rv);
3473 goto out_err_started;
3476 mutex_lock(&intf->bmc_reg_mutex);
3477 rv = __scan_channels(intf, &id);
3478 mutex_unlock(&intf->bmc_reg_mutex);
3480 goto out_err_bmc_reg;
3483 * Keep memory order straight for RCU readers. Make
3484 * sure everything else is committed to memory before
3485 * setting intf_num to mark the interface valid.
3489 mutex_unlock(&ipmi_interfaces_mutex);
3491 /* After this point the interface is legal to use. */
3492 call_smi_watchers(i, intf->si_dev);
3497 ipmi_bmc_unregister(intf);
3499 if (intf->handlers->shutdown)
3500 intf->handlers->shutdown(intf->send_info);
3502 list_del_rcu(&intf->link);
3503 mutex_unlock(&ipmi_interfaces_mutex);
3504 synchronize_srcu(&ipmi_interfaces_srcu);
3505 cleanup_srcu_struct(&intf->users_srcu);
3506 kref_put(&intf->refcount, intf_free);
3510 EXPORT_SYMBOL(ipmi_add_smi);
3512 static void deliver_smi_err_response(struct ipmi_smi *intf,
3513 struct ipmi_smi_msg *msg,
3516 msg->rsp[0] = msg->data[0] | 4;
3517 msg->rsp[1] = msg->data[1];
3520 /* It's an error, so it will never requeue, no need to check return. */
3521 handle_one_recv_msg(intf, msg);
3524 static void cleanup_smi_msgs(struct ipmi_smi *intf)
3527 struct seq_table *ent;
3528 struct ipmi_smi_msg *msg;
3529 struct list_head *entry;
3530 struct list_head tmplist;
3532 /* Clear out our transmit queues and hold the messages. */
3533 INIT_LIST_HEAD(&tmplist);
3534 list_splice_tail(&intf->hp_xmit_msgs, &tmplist);
3535 list_splice_tail(&intf->xmit_msgs, &tmplist);
3537 /* Current message first, to preserve order */
3538 while (intf->curr_msg && !list_empty(&intf->waiting_rcv_msgs)) {
3539 /* Wait for the message to clear out. */
3540 schedule_timeout(1);
3543 /* No need for locks, the interface is down. */
3546 * Return errors for all pending messages in queue and in the
3547 * tables waiting for remote responses.
3549 while (!list_empty(&tmplist)) {
3550 entry = tmplist.next;
3552 msg = list_entry(entry, struct ipmi_smi_msg, link);
3553 deliver_smi_err_response(intf, msg, IPMI_ERR_UNSPECIFIED);
3556 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
3557 ent = &intf->seq_table[i];
3560 deliver_err_response(intf, ent->recv_msg, IPMI_ERR_UNSPECIFIED);
3564 void ipmi_unregister_smi(struct ipmi_smi *intf)
3566 struct ipmi_smi_watcher *w;
3567 int intf_num = intf->intf_num, index;
3569 mutex_lock(&ipmi_interfaces_mutex);
3570 intf->intf_num = -1;
3571 intf->in_shutdown = true;
3572 list_del_rcu(&intf->link);
3573 mutex_unlock(&ipmi_interfaces_mutex);
3574 synchronize_srcu(&ipmi_interfaces_srcu);
3576 /* At this point no users can be added to the interface. */
3579 * Call all the watcher interfaces to tell them that
3580 * an interface is going away.
3582 mutex_lock(&smi_watchers_mutex);
3583 list_for_each_entry(w, &smi_watchers, link)
3584 w->smi_gone(intf_num);
3585 mutex_unlock(&smi_watchers_mutex);
3587 index = srcu_read_lock(&intf->users_srcu);
3588 while (!list_empty(&intf->users)) {
3589 struct ipmi_user *user =
3590 container_of(list_next_rcu(&intf->users),
3591 struct ipmi_user, link);
3593 _ipmi_destroy_user(user);
3595 srcu_read_unlock(&intf->users_srcu, index);
3597 if (intf->handlers->shutdown)
3598 intf->handlers->shutdown(intf->send_info);
3600 cleanup_smi_msgs(intf);
3602 ipmi_bmc_unregister(intf);
3604 cleanup_srcu_struct(&intf->users_srcu);
3605 kref_put(&intf->refcount, intf_free);
3607 EXPORT_SYMBOL(ipmi_unregister_smi);
3609 static int handle_ipmb_get_msg_rsp(struct ipmi_smi *intf,
3610 struct ipmi_smi_msg *msg)
3612 struct ipmi_ipmb_addr ipmb_addr;
3613 struct ipmi_recv_msg *recv_msg;
3616 * This is 11, not 10, because the response must contain a
3619 if (msg->rsp_size < 11) {
3620 /* Message not big enough, just ignore it. */
3621 ipmi_inc_stat(intf, invalid_ipmb_responses);
3625 if (msg->rsp[2] != 0) {
3626 /* An error getting the response, just ignore it. */
3630 ipmb_addr.addr_type = IPMI_IPMB_ADDR_TYPE;
3631 ipmb_addr.slave_addr = msg->rsp[6];
3632 ipmb_addr.channel = msg->rsp[3] & 0x0f;
3633 ipmb_addr.lun = msg->rsp[7] & 3;
3636 * It's a response from a remote entity. Look up the sequence
3637 * number and handle the response.
3639 if (intf_find_seq(intf,
3643 (msg->rsp[4] >> 2) & (~1),
3644 (struct ipmi_addr *) &ipmb_addr,
3647 * We were unable to find the sequence number,
3648 * so just nuke the message.
3650 ipmi_inc_stat(intf, unhandled_ipmb_responses);
3654 memcpy(recv_msg->msg_data, &msg->rsp[9], msg->rsp_size - 9);
3656 * The other fields matched, so no need to set them, except
3657 * for netfn, which needs to be the response that was
3658 * returned, not the request value.
3660 recv_msg->msg.netfn = msg->rsp[4] >> 2;
3661 recv_msg->msg.data = recv_msg->msg_data;
3662 recv_msg->msg.data_len = msg->rsp_size - 10;
3663 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
3664 if (deliver_response(intf, recv_msg))
3665 ipmi_inc_stat(intf, unhandled_ipmb_responses);
3667 ipmi_inc_stat(intf, handled_ipmb_responses);
3672 static int handle_ipmb_get_msg_cmd(struct ipmi_smi *intf,
3673 struct ipmi_smi_msg *msg)
3675 struct cmd_rcvr *rcvr;
3677 unsigned char netfn;
3680 struct ipmi_user *user = NULL;
3681 struct ipmi_ipmb_addr *ipmb_addr;
3682 struct ipmi_recv_msg *recv_msg;
3684 if (msg->rsp_size < 10) {
3685 /* Message not big enough, just ignore it. */
3686 ipmi_inc_stat(intf, invalid_commands);
3690 if (msg->rsp[2] != 0) {
3691 /* An error getting the response, just ignore it. */
3695 netfn = msg->rsp[4] >> 2;
3697 chan = msg->rsp[3] & 0xf;
3700 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
3703 kref_get(&user->refcount);
3709 /* We didn't find a user, deliver an error response. */
3710 ipmi_inc_stat(intf, unhandled_commands);
3712 msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
3713 msg->data[1] = IPMI_SEND_MSG_CMD;
3714 msg->data[2] = msg->rsp[3];
3715 msg->data[3] = msg->rsp[6];
3716 msg->data[4] = ((netfn + 1) << 2) | (msg->rsp[7] & 0x3);
3717 msg->data[5] = ipmb_checksum(&msg->data[3], 2);
3718 msg->data[6] = intf->addrinfo[msg->rsp[3] & 0xf].address;
3720 msg->data[7] = (msg->rsp[7] & 0xfc) | (msg->rsp[4] & 0x3);
3721 msg->data[8] = msg->rsp[8]; /* cmd */
3722 msg->data[9] = IPMI_INVALID_CMD_COMPLETION_CODE;
3723 msg->data[10] = ipmb_checksum(&msg->data[6], 4);
3724 msg->data_size = 11;
3726 pr_debug("Invalid command: %*ph\n", msg->data_size, msg->data);
3729 if (!intf->in_shutdown) {
3730 smi_send(intf, intf->handlers, msg, 0);
3732 * We used the message, so return the value
3733 * that causes it to not be freed or
3740 recv_msg = ipmi_alloc_recv_msg();
3743 * We couldn't allocate memory for the
3744 * message, so requeue it for handling
3748 kref_put(&user->refcount, free_user);
3750 /* Extract the source address from the data. */
3751 ipmb_addr = (struct ipmi_ipmb_addr *) &recv_msg->addr;
3752 ipmb_addr->addr_type = IPMI_IPMB_ADDR_TYPE;
3753 ipmb_addr->slave_addr = msg->rsp[6];
3754 ipmb_addr->lun = msg->rsp[7] & 3;
3755 ipmb_addr->channel = msg->rsp[3] & 0xf;
3758 * Extract the rest of the message information
3759 * from the IPMB header.
3761 recv_msg->user = user;
3762 recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
3763 recv_msg->msgid = msg->rsp[7] >> 2;
3764 recv_msg->msg.netfn = msg->rsp[4] >> 2;
3765 recv_msg->msg.cmd = msg->rsp[8];
3766 recv_msg->msg.data = recv_msg->msg_data;
3769 * We chop off 10, not 9 bytes because the checksum
3770 * at the end also needs to be removed.
3772 recv_msg->msg.data_len = msg->rsp_size - 10;
3773 memcpy(recv_msg->msg_data, &msg->rsp[9],
3774 msg->rsp_size - 10);
3775 if (deliver_response(intf, recv_msg))
3776 ipmi_inc_stat(intf, unhandled_commands);
3778 ipmi_inc_stat(intf, handled_commands);
3785 static int handle_lan_get_msg_rsp(struct ipmi_smi *intf,
3786 struct ipmi_smi_msg *msg)
3788 struct ipmi_lan_addr lan_addr;
3789 struct ipmi_recv_msg *recv_msg;
3793 * This is 13, not 12, because the response must contain a
3796 if (msg->rsp_size < 13) {
3797 /* Message not big enough, just ignore it. */
3798 ipmi_inc_stat(intf, invalid_lan_responses);
3802 if (msg->rsp[2] != 0) {
3803 /* An error getting the response, just ignore it. */
3807 lan_addr.addr_type = IPMI_LAN_ADDR_TYPE;
3808 lan_addr.session_handle = msg->rsp[4];
3809 lan_addr.remote_SWID = msg->rsp[8];
3810 lan_addr.local_SWID = msg->rsp[5];
3811 lan_addr.channel = msg->rsp[3] & 0x0f;
3812 lan_addr.privilege = msg->rsp[3] >> 4;
3813 lan_addr.lun = msg->rsp[9] & 3;
3816 * It's a response from a remote entity. Look up the sequence
3817 * number and handle the response.
3819 if (intf_find_seq(intf,
3823 (msg->rsp[6] >> 2) & (~1),
3824 (struct ipmi_addr *) &lan_addr,
3827 * We were unable to find the sequence number,
3828 * so just nuke the message.
3830 ipmi_inc_stat(intf, unhandled_lan_responses);
3834 memcpy(recv_msg->msg_data, &msg->rsp[11], msg->rsp_size - 11);
3836 * The other fields matched, so no need to set them, except
3837 * for netfn, which needs to be the response that was
3838 * returned, not the request value.
3840 recv_msg->msg.netfn = msg->rsp[6] >> 2;
3841 recv_msg->msg.data = recv_msg->msg_data;
3842 recv_msg->msg.data_len = msg->rsp_size - 12;
3843 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
3844 if (deliver_response(intf, recv_msg))
3845 ipmi_inc_stat(intf, unhandled_lan_responses);
3847 ipmi_inc_stat(intf, handled_lan_responses);
3852 static int handle_lan_get_msg_cmd(struct ipmi_smi *intf,
3853 struct ipmi_smi_msg *msg)
3855 struct cmd_rcvr *rcvr;
3857 unsigned char netfn;
3860 struct ipmi_user *user = NULL;
3861 struct ipmi_lan_addr *lan_addr;
3862 struct ipmi_recv_msg *recv_msg;
3864 if (msg->rsp_size < 12) {
3865 /* Message not big enough, just ignore it. */
3866 ipmi_inc_stat(intf, invalid_commands);
3870 if (msg->rsp[2] != 0) {
3871 /* An error getting the response, just ignore it. */
3875 netfn = msg->rsp[6] >> 2;
3877 chan = msg->rsp[3] & 0xf;
3880 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
3883 kref_get(&user->refcount);
3889 /* We didn't find a user, just give up. */
3890 ipmi_inc_stat(intf, unhandled_commands);
3893 * Don't do anything with these messages, just allow
3898 recv_msg = ipmi_alloc_recv_msg();
3901 * We couldn't allocate memory for the
3902 * message, so requeue it for handling later.
3905 kref_put(&user->refcount, free_user);
3907 /* Extract the source address from the data. */
3908 lan_addr = (struct ipmi_lan_addr *) &recv_msg->addr;
3909 lan_addr->addr_type = IPMI_LAN_ADDR_TYPE;
3910 lan_addr->session_handle = msg->rsp[4];
3911 lan_addr->remote_SWID = msg->rsp[8];
3912 lan_addr->local_SWID = msg->rsp[5];
3913 lan_addr->lun = msg->rsp[9] & 3;
3914 lan_addr->channel = msg->rsp[3] & 0xf;
3915 lan_addr->privilege = msg->rsp[3] >> 4;
3918 * Extract the rest of the message information
3919 * from the IPMB header.
3921 recv_msg->user = user;
3922 recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
3923 recv_msg->msgid = msg->rsp[9] >> 2;
3924 recv_msg->msg.netfn = msg->rsp[6] >> 2;
3925 recv_msg->msg.cmd = msg->rsp[10];
3926 recv_msg->msg.data = recv_msg->msg_data;
3929 * We chop off 12, not 11 bytes because the checksum
3930 * at the end also needs to be removed.
3932 recv_msg->msg.data_len = msg->rsp_size - 12;
3933 memcpy(recv_msg->msg_data, &msg->rsp[11],
3934 msg->rsp_size - 12);
3935 if (deliver_response(intf, recv_msg))
3936 ipmi_inc_stat(intf, unhandled_commands);
3938 ipmi_inc_stat(intf, handled_commands);
3946 * This routine will handle "Get Message" command responses with
3947 * channels that use an OEM Medium. The message format belongs to
3948 * the OEM. See IPMI 2.0 specification, Chapter 6 and
3949 * Chapter 22, sections 22.6 and 22.24 for more details.
3951 static int handle_oem_get_msg_cmd(struct ipmi_smi *intf,
3952 struct ipmi_smi_msg *msg)
3954 struct cmd_rcvr *rcvr;
3956 unsigned char netfn;
3959 struct ipmi_user *user = NULL;
3960 struct ipmi_system_interface_addr *smi_addr;
3961 struct ipmi_recv_msg *recv_msg;
3964 * We expect the OEM SW to perform error checking
3965 * so we just do some basic sanity checks
3967 if (msg->rsp_size < 4) {
3968 /* Message not big enough, just ignore it. */
3969 ipmi_inc_stat(intf, invalid_commands);
3973 if (msg->rsp[2] != 0) {
3974 /* An error getting the response, just ignore it. */
3979 * This is an OEM Message so the OEM needs to know how
3980 * handle the message. We do no interpretation.
3982 netfn = msg->rsp[0] >> 2;
3984 chan = msg->rsp[3] & 0xf;
3987 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
3990 kref_get(&user->refcount);
3996 /* We didn't find a user, just give up. */
3997 ipmi_inc_stat(intf, unhandled_commands);
4000 * Don't do anything with these messages, just allow
4006 recv_msg = ipmi_alloc_recv_msg();
4009 * We couldn't allocate memory for the
4010 * message, so requeue it for handling
4014 kref_put(&user->refcount, free_user);
4017 * OEM Messages are expected to be delivered via
4018 * the system interface to SMS software. We might
4019 * need to visit this again depending on OEM
4022 smi_addr = ((struct ipmi_system_interface_addr *)
4024 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
4025 smi_addr->channel = IPMI_BMC_CHANNEL;
4026 smi_addr->lun = msg->rsp[0] & 3;
4028 recv_msg->user = user;
4029 recv_msg->user_msg_data = NULL;
4030 recv_msg->recv_type = IPMI_OEM_RECV_TYPE;
4031 recv_msg->msg.netfn = msg->rsp[0] >> 2;
4032 recv_msg->msg.cmd = msg->rsp[1];
4033 recv_msg->msg.data = recv_msg->msg_data;
4036 * The message starts at byte 4 which follows the
4037 * the Channel Byte in the "GET MESSAGE" command
4039 recv_msg->msg.data_len = msg->rsp_size - 4;
4040 memcpy(recv_msg->msg_data, &msg->rsp[4],
4042 if (deliver_response(intf, recv_msg))
4043 ipmi_inc_stat(intf, unhandled_commands);
4045 ipmi_inc_stat(intf, handled_commands);
4052 static void copy_event_into_recv_msg(struct ipmi_recv_msg *recv_msg,
4053 struct ipmi_smi_msg *msg)
4055 struct ipmi_system_interface_addr *smi_addr;
4057 recv_msg->msgid = 0;
4058 smi_addr = (struct ipmi_system_interface_addr *) &recv_msg->addr;
4059 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
4060 smi_addr->channel = IPMI_BMC_CHANNEL;
4061 smi_addr->lun = msg->rsp[0] & 3;
4062 recv_msg->recv_type = IPMI_ASYNC_EVENT_RECV_TYPE;
4063 recv_msg->msg.netfn = msg->rsp[0] >> 2;
4064 recv_msg->msg.cmd = msg->rsp[1];
4065 memcpy(recv_msg->msg_data, &msg->rsp[3], msg->rsp_size - 3);
4066 recv_msg->msg.data = recv_msg->msg_data;
4067 recv_msg->msg.data_len = msg->rsp_size - 3;
4070 static int handle_read_event_rsp(struct ipmi_smi *intf,
4071 struct ipmi_smi_msg *msg)
4073 struct ipmi_recv_msg *recv_msg, *recv_msg2;
4074 struct list_head msgs;
4075 struct ipmi_user *user;
4076 int rv = 0, deliver_count = 0, index;
4077 unsigned long flags;
4079 if (msg->rsp_size < 19) {
4080 /* Message is too small to be an IPMB event. */
4081 ipmi_inc_stat(intf, invalid_events);
4085 if (msg->rsp[2] != 0) {
4086 /* An error getting the event, just ignore it. */
4090 INIT_LIST_HEAD(&msgs);
4092 spin_lock_irqsave(&intf->events_lock, flags);
4094 ipmi_inc_stat(intf, events);
4097 * Allocate and fill in one message for every user that is
4100 index = srcu_read_lock(&intf->users_srcu);
4101 list_for_each_entry_rcu(user, &intf->users, link) {
4102 if (!user->gets_events)
4105 recv_msg = ipmi_alloc_recv_msg();
4108 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs,
4110 list_del(&recv_msg->link);
4111 ipmi_free_recv_msg(recv_msg);
4114 * We couldn't allocate memory for the
4115 * message, so requeue it for handling
4124 copy_event_into_recv_msg(recv_msg, msg);
4125 recv_msg->user = user;
4126 kref_get(&user->refcount);
4127 list_add_tail(&recv_msg->link, &msgs);
4129 srcu_read_unlock(&intf->users_srcu, index);
4131 if (deliver_count) {
4132 /* Now deliver all the messages. */
4133 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs, link) {
4134 list_del(&recv_msg->link);
4135 deliver_local_response(intf, recv_msg);
4137 } else if (intf->waiting_events_count < MAX_EVENTS_IN_QUEUE) {
4139 * No one to receive the message, put it in queue if there's
4140 * not already too many things in the queue.
4142 recv_msg = ipmi_alloc_recv_msg();
4145 * We couldn't allocate memory for the
4146 * message, so requeue it for handling
4153 copy_event_into_recv_msg(recv_msg, msg);
4154 list_add_tail(&recv_msg->link, &intf->waiting_events);
4155 intf->waiting_events_count++;
4156 } else if (!intf->event_msg_printed) {
4158 * There's too many things in the queue, discard this
4161 dev_warn(intf->si_dev,
4162 "Event queue full, discarding incoming events\n");
4163 intf->event_msg_printed = 1;
4167 spin_unlock_irqrestore(&intf->events_lock, flags);
4172 static int handle_bmc_rsp(struct ipmi_smi *intf,
4173 struct ipmi_smi_msg *msg)
4175 struct ipmi_recv_msg *recv_msg;
4176 struct ipmi_system_interface_addr *smi_addr;
4178 recv_msg = (struct ipmi_recv_msg *) msg->user_data;
4179 if (recv_msg == NULL) {
4180 dev_warn(intf->si_dev,
4181 "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");
4185 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
4186 recv_msg->msgid = msg->msgid;
4187 smi_addr = ((struct ipmi_system_interface_addr *)
4189 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
4190 smi_addr->channel = IPMI_BMC_CHANNEL;
4191 smi_addr->lun = msg->rsp[0] & 3;
4192 recv_msg->msg.netfn = msg->rsp[0] >> 2;
4193 recv_msg->msg.cmd = msg->rsp[1];
4194 memcpy(recv_msg->msg_data, &msg->rsp[2], msg->rsp_size - 2);
4195 recv_msg->msg.data = recv_msg->msg_data;
4196 recv_msg->msg.data_len = msg->rsp_size - 2;
4197 deliver_local_response(intf, recv_msg);
4203 * Handle a received message. Return 1 if the message should be requeued,
4204 * 0 if the message should be freed, or -1 if the message should not
4205 * be freed or requeued.
4207 static int handle_one_recv_msg(struct ipmi_smi *intf,
4208 struct ipmi_smi_msg *msg)
4213 pr_debug("Recv: %*ph\n", msg->rsp_size, msg->rsp);
4215 if ((msg->data_size >= 2)
4216 && (msg->data[0] == (IPMI_NETFN_APP_REQUEST << 2))
4217 && (msg->data[1] == IPMI_SEND_MSG_CMD)
4218 && (msg->user_data == NULL)) {
4220 if (intf->in_shutdown)
4224 * This is the local response to a command send, start
4225 * the timer for these. The user_data will not be
4226 * NULL if this is a response send, and we will let
4227 * response sends just go through.
4231 * Check for errors, if we get certain errors (ones
4232 * that mean basically we can try again later), we
4233 * ignore them and start the timer. Otherwise we
4234 * report the error immediately.
4236 if ((msg->rsp_size >= 3) && (msg->rsp[2] != 0)
4237 && (msg->rsp[2] != IPMI_NODE_BUSY_ERR)
4238 && (msg->rsp[2] != IPMI_LOST_ARBITRATION_ERR)
4239 && (msg->rsp[2] != IPMI_BUS_ERR)
4240 && (msg->rsp[2] != IPMI_NAK_ON_WRITE_ERR)) {
4241 int ch = msg->rsp[3] & 0xf;
4242 struct ipmi_channel *chans;
4244 /* Got an error sending the message, handle it. */
4246 chans = READ_ONCE(intf->channel_list)->c;
4247 if ((chans[ch].medium == IPMI_CHANNEL_MEDIUM_8023LAN)
4248 || (chans[ch].medium == IPMI_CHANNEL_MEDIUM_ASYNC))
4249 ipmi_inc_stat(intf, sent_lan_command_errs);
4251 ipmi_inc_stat(intf, sent_ipmb_command_errs);
4252 intf_err_seq(intf, msg->msgid, msg->rsp[2]);
4254 /* The message was sent, start the timer. */
4255 intf_start_seq_timer(intf, msg->msgid);
4260 } else if (msg->rsp_size < 2) {
4261 /* Message is too small to be correct. */
4262 dev_warn(intf->si_dev,
4263 "BMC returned too small a message for netfn %x cmd %x, got %d bytes\n",
4264 (msg->data[0] >> 2) | 1, msg->data[1], msg->rsp_size);
4266 /* Generate an error response for the message. */
4267 msg->rsp[0] = msg->data[0] | (1 << 2);
4268 msg->rsp[1] = msg->data[1];
4269 msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
4271 } else if (((msg->rsp[0] >> 2) != ((msg->data[0] >> 2) | 1))
4272 || (msg->rsp[1] != msg->data[1])) {
4274 * The NetFN and Command in the response is not even
4275 * marginally correct.
4277 dev_warn(intf->si_dev,
4278 "BMC returned incorrect response, expected netfn %x cmd %x, got netfn %x cmd %x\n",
4279 (msg->data[0] >> 2) | 1, msg->data[1],
4280 msg->rsp[0] >> 2, msg->rsp[1]);
4282 /* Generate an error response for the message. */
4283 msg->rsp[0] = msg->data[0] | (1 << 2);
4284 msg->rsp[1] = msg->data[1];
4285 msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
4289 if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
4290 && (msg->rsp[1] == IPMI_SEND_MSG_CMD)
4291 && (msg->user_data != NULL)) {
4293 * It's a response to a response we sent. For this we
4294 * deliver a send message response to the user.
4296 struct ipmi_recv_msg *recv_msg = msg->user_data;
4299 if (msg->rsp_size < 2)
4300 /* Message is too small to be correct. */
4303 chan = msg->data[2] & 0x0f;
4304 if (chan >= IPMI_MAX_CHANNELS)
4305 /* Invalid channel number */
4311 recv_msg->recv_type = IPMI_RESPONSE_RESPONSE_TYPE;
4312 recv_msg->msg.data = recv_msg->msg_data;
4313 recv_msg->msg.data_len = 1;
4314 recv_msg->msg_data[0] = msg->rsp[2];
4315 deliver_local_response(intf, recv_msg);
4316 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
4317 && (msg->rsp[1] == IPMI_GET_MSG_CMD)) {
4318 struct ipmi_channel *chans;
4320 /* It's from the receive queue. */
4321 chan = msg->rsp[3] & 0xf;
4322 if (chan >= IPMI_MAX_CHANNELS) {
4323 /* Invalid channel number */
4329 * We need to make sure the channels have been initialized.
4330 * The channel_handler routine will set the "curr_channel"
4331 * equal to or greater than IPMI_MAX_CHANNELS when all the
4332 * channels for this interface have been initialized.
4334 if (!intf->channels_ready) {
4335 requeue = 0; /* Throw the message away */
4339 chans = READ_ONCE(intf->channel_list)->c;
4341 switch (chans[chan].medium) {
4342 case IPMI_CHANNEL_MEDIUM_IPMB:
4343 if (msg->rsp[4] & 0x04) {
4345 * It's a response, so find the
4346 * requesting message and send it up.
4348 requeue = handle_ipmb_get_msg_rsp(intf, msg);
4351 * It's a command to the SMS from some other
4352 * entity. Handle that.
4354 requeue = handle_ipmb_get_msg_cmd(intf, msg);
4358 case IPMI_CHANNEL_MEDIUM_8023LAN:
4359 case IPMI_CHANNEL_MEDIUM_ASYNC:
4360 if (msg->rsp[6] & 0x04) {
4362 * It's a response, so find the
4363 * requesting message and send it up.
4365 requeue = handle_lan_get_msg_rsp(intf, msg);
4368 * It's a command to the SMS from some other
4369 * entity. Handle that.
4371 requeue = handle_lan_get_msg_cmd(intf, msg);
4376 /* Check for OEM Channels. Clients had better
4377 register for these commands. */
4378 if ((chans[chan].medium >= IPMI_CHANNEL_MEDIUM_OEM_MIN)
4379 && (chans[chan].medium
4380 <= IPMI_CHANNEL_MEDIUM_OEM_MAX)) {
4381 requeue = handle_oem_get_msg_cmd(intf, msg);
4384 * We don't handle the channel type, so just
4391 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
4392 && (msg->rsp[1] == IPMI_READ_EVENT_MSG_BUFFER_CMD)) {
4393 /* It's an asynchronous event. */
4394 requeue = handle_read_event_rsp(intf, msg);
4396 /* It's a response from the local BMC. */
4397 requeue = handle_bmc_rsp(intf, msg);
4405 * If there are messages in the queue or pretimeouts, handle them.
4407 static void handle_new_recv_msgs(struct ipmi_smi *intf)
4409 struct ipmi_smi_msg *smi_msg;
4410 unsigned long flags = 0;
4412 int run_to_completion = intf->run_to_completion;
4414 /* See if any waiting messages need to be processed. */
4415 if (!run_to_completion)
4416 spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags);
4417 while (!list_empty(&intf->waiting_rcv_msgs)) {
4418 smi_msg = list_entry(intf->waiting_rcv_msgs.next,
4419 struct ipmi_smi_msg, link);
4420 list_del(&smi_msg->link);
4421 if (!run_to_completion)
4422 spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock,
4424 rv = handle_one_recv_msg(intf, smi_msg);
4425 if (!run_to_completion)
4426 spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags);
4429 * To preserve message order, quit if we
4430 * can't handle a message. Add the message
4431 * back at the head, this is safe because this
4432 * tasklet is the only thing that pulls the
4435 list_add(&smi_msg->link, &intf->waiting_rcv_msgs);
4439 /* Message handled */
4440 ipmi_free_smi_msg(smi_msg);
4441 /* If rv < 0, fatal error, del but don't free. */
4444 if (!run_to_completion)
4445 spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock, flags);
4448 * If the pretimout count is non-zero, decrement one from it and
4449 * deliver pretimeouts to all the users.
4451 if (atomic_add_unless(&intf->watchdog_pretimeouts_to_deliver, -1, 0)) {
4452 struct ipmi_user *user;
4455 index = srcu_read_lock(&intf->users_srcu);
4456 list_for_each_entry_rcu(user, &intf->users, link) {
4457 if (user->handler->ipmi_watchdog_pretimeout)
4458 user->handler->ipmi_watchdog_pretimeout(
4459 user->handler_data);
4461 srcu_read_unlock(&intf->users_srcu, index);
4465 static void smi_recv_tasklet(unsigned long val)
4467 unsigned long flags = 0; /* keep us warning-free. */
4468 struct ipmi_smi *intf = (struct ipmi_smi *) val;
4469 int run_to_completion = intf->run_to_completion;
4470 struct ipmi_smi_msg *newmsg = NULL;
4473 * Start the next message if available.
4475 * Do this here, not in the actual receiver, because we may deadlock
4476 * because the lower layer is allowed to hold locks while calling
4482 if (!run_to_completion)
4483 spin_lock_irqsave(&intf->xmit_msgs_lock, flags);
4484 if (intf->curr_msg == NULL && !intf->in_shutdown) {
4485 struct list_head *entry = NULL;
4487 /* Pick the high priority queue first. */
4488 if (!list_empty(&intf->hp_xmit_msgs))
4489 entry = intf->hp_xmit_msgs.next;
4490 else if (!list_empty(&intf->xmit_msgs))
4491 entry = intf->xmit_msgs.next;
4495 newmsg = list_entry(entry, struct ipmi_smi_msg, link);
4496 intf->curr_msg = newmsg;
4500 if (!run_to_completion)
4501 spin_unlock_irqrestore(&intf->xmit_msgs_lock, flags);
4503 intf->handlers->sender(intf->send_info, newmsg);
4507 handle_new_recv_msgs(intf);
4510 /* Handle a new message from the lower layer. */
4511 void ipmi_smi_msg_received(struct ipmi_smi *intf,
4512 struct ipmi_smi_msg *msg)
4514 unsigned long flags = 0; /* keep us warning-free. */
4515 int run_to_completion = intf->run_to_completion;
4518 * To preserve message order, we keep a queue and deliver from
4521 if (!run_to_completion)
4522 spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags);
4523 list_add_tail(&msg->link, &intf->waiting_rcv_msgs);
4524 if (!run_to_completion)
4525 spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock,
4528 if (!run_to_completion)
4529 spin_lock_irqsave(&intf->xmit_msgs_lock, flags);
4531 * We can get an asynchronous event or receive message in addition
4532 * to commands we send.
4534 if (msg == intf->curr_msg)
4535 intf->curr_msg = NULL;
4536 if (!run_to_completion)
4537 spin_unlock_irqrestore(&intf->xmit_msgs_lock, flags);
4539 if (run_to_completion)
4540 smi_recv_tasklet((unsigned long) intf);
4542 tasklet_schedule(&intf->recv_tasklet);
4544 EXPORT_SYMBOL(ipmi_smi_msg_received);
4546 void ipmi_smi_watchdog_pretimeout(struct ipmi_smi *intf)
4548 if (intf->in_shutdown)
4551 atomic_set(&intf->watchdog_pretimeouts_to_deliver, 1);
4552 tasklet_schedule(&intf->recv_tasklet);
4554 EXPORT_SYMBOL(ipmi_smi_watchdog_pretimeout);
4556 static struct ipmi_smi_msg *
4557 smi_from_recv_msg(struct ipmi_smi *intf, struct ipmi_recv_msg *recv_msg,
4558 unsigned char seq, long seqid)
4560 struct ipmi_smi_msg *smi_msg = ipmi_alloc_smi_msg();
4563 * If we can't allocate the message, then just return, we
4564 * get 4 retries, so this should be ok.
4568 memcpy(smi_msg->data, recv_msg->msg.data, recv_msg->msg.data_len);
4569 smi_msg->data_size = recv_msg->msg.data_len;
4570 smi_msg->msgid = STORE_SEQ_IN_MSGID(seq, seqid);
4572 pr_debug("Resend: %*ph\n", smi_msg->data_size, smi_msg->data);
4577 static void check_msg_timeout(struct ipmi_smi *intf, struct seq_table *ent,
4578 struct list_head *timeouts,
4579 unsigned long timeout_period,
4580 int slot, unsigned long *flags,
4583 struct ipmi_recv_msg *msg;
4585 if (intf->in_shutdown)
4591 if (timeout_period < ent->timeout) {
4592 ent->timeout -= timeout_period;
4597 if (ent->retries_left == 0) {
4598 /* The message has used all its retries. */
4600 smi_remove_watch(intf, IPMI_WATCH_MASK_CHECK_MESSAGES);
4601 msg = ent->recv_msg;
4602 list_add_tail(&msg->link, timeouts);
4604 ipmi_inc_stat(intf, timed_out_ipmb_broadcasts);
4605 else if (is_lan_addr(&ent->recv_msg->addr))
4606 ipmi_inc_stat(intf, timed_out_lan_commands);
4608 ipmi_inc_stat(intf, timed_out_ipmb_commands);
4610 struct ipmi_smi_msg *smi_msg;
4611 /* More retries, send again. */
4616 * Start with the max timer, set to normal timer after
4617 * the message is sent.
4619 ent->timeout = MAX_MSG_TIMEOUT;
4620 ent->retries_left--;
4621 smi_msg = smi_from_recv_msg(intf, ent->recv_msg, slot,
4624 if (is_lan_addr(&ent->recv_msg->addr))
4626 dropped_rexmit_lan_commands);
4629 dropped_rexmit_ipmb_commands);
4633 spin_unlock_irqrestore(&intf->seq_lock, *flags);
4636 * Send the new message. We send with a zero
4637 * priority. It timed out, I doubt time is that
4638 * critical now, and high priority messages are really
4639 * only for messages to the local MC, which don't get
4642 if (intf->handlers) {
4643 if (is_lan_addr(&ent->recv_msg->addr))
4645 retransmitted_lan_commands);
4648 retransmitted_ipmb_commands);
4650 smi_send(intf, intf->handlers, smi_msg, 0);
4652 ipmi_free_smi_msg(smi_msg);
4654 spin_lock_irqsave(&intf->seq_lock, *flags);
4658 static bool ipmi_timeout_handler(struct ipmi_smi *intf,
4659 unsigned long timeout_period)
4661 struct list_head timeouts;
4662 struct ipmi_recv_msg *msg, *msg2;
4663 unsigned long flags;
4665 bool need_timer = false;
4667 if (!intf->bmc_registered) {
4668 kref_get(&intf->refcount);
4669 if (!schedule_work(&intf->bmc_reg_work)) {
4670 kref_put(&intf->refcount, intf_free);
4676 * Go through the seq table and find any messages that
4677 * have timed out, putting them in the timeouts
4680 INIT_LIST_HEAD(&timeouts);
4681 spin_lock_irqsave(&intf->seq_lock, flags);
4682 if (intf->ipmb_maintenance_mode_timeout) {
4683 if (intf->ipmb_maintenance_mode_timeout <= timeout_period)
4684 intf->ipmb_maintenance_mode_timeout = 0;
4686 intf->ipmb_maintenance_mode_timeout -= timeout_period;
4688 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++)
4689 check_msg_timeout(intf, &intf->seq_table[i],
4690 &timeouts, timeout_period, i,
4691 &flags, &need_timer);
4692 spin_unlock_irqrestore(&intf->seq_lock, flags);
4694 list_for_each_entry_safe(msg, msg2, &timeouts, link)
4695 deliver_err_response(intf, msg, IPMI_TIMEOUT_COMPLETION_CODE);
4698 * Maintenance mode handling. Check the timeout
4699 * optimistically before we claim the lock. It may
4700 * mean a timeout gets missed occasionally, but that
4701 * only means the timeout gets extended by one period
4702 * in that case. No big deal, and it avoids the lock
4705 if (intf->auto_maintenance_timeout > 0) {
4706 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
4707 if (intf->auto_maintenance_timeout > 0) {
4708 intf->auto_maintenance_timeout
4710 if (!intf->maintenance_mode
4711 && (intf->auto_maintenance_timeout <= 0)) {
4712 intf->maintenance_mode_enable = false;
4713 maintenance_mode_update(intf);
4716 spin_unlock_irqrestore(&intf->maintenance_mode_lock,
4720 tasklet_schedule(&intf->recv_tasklet);
4725 static void ipmi_request_event(struct ipmi_smi *intf)
4727 /* No event requests when in maintenance mode. */
4728 if (intf->maintenance_mode_enable)
4731 if (!intf->in_shutdown)
4732 intf->handlers->request_events(intf->send_info);
4735 static struct timer_list ipmi_timer;
4737 static atomic_t stop_operation;
4739 static void ipmi_timeout(struct timer_list *unused)
4741 struct ipmi_smi *intf;
4742 bool need_timer = false;
4745 if (atomic_read(&stop_operation))
4748 index = srcu_read_lock(&ipmi_interfaces_srcu);
4749 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
4750 if (atomic_read(&intf->event_waiters)) {
4751 intf->ticks_to_req_ev--;
4752 if (intf->ticks_to_req_ev == 0) {
4753 ipmi_request_event(intf);
4754 intf->ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
4759 need_timer |= ipmi_timeout_handler(intf, IPMI_TIMEOUT_TIME);
4761 srcu_read_unlock(&ipmi_interfaces_srcu, index);
4764 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
4767 static void need_waiter(struct ipmi_smi *intf)
4769 /* Racy, but worst case we start the timer twice. */
4770 if (!timer_pending(&ipmi_timer))
4771 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
4774 static atomic_t smi_msg_inuse_count = ATOMIC_INIT(0);
4775 static atomic_t recv_msg_inuse_count = ATOMIC_INIT(0);
4777 static void free_smi_msg(struct ipmi_smi_msg *msg)
4779 atomic_dec(&smi_msg_inuse_count);
4783 struct ipmi_smi_msg *ipmi_alloc_smi_msg(void)
4785 struct ipmi_smi_msg *rv;
4786 rv = kmalloc(sizeof(struct ipmi_smi_msg), GFP_ATOMIC);
4788 rv->done = free_smi_msg;
4789 rv->user_data = NULL;
4790 atomic_inc(&smi_msg_inuse_count);
4794 EXPORT_SYMBOL(ipmi_alloc_smi_msg);
4796 static void free_recv_msg(struct ipmi_recv_msg *msg)
4798 atomic_dec(&recv_msg_inuse_count);
4802 static struct ipmi_recv_msg *ipmi_alloc_recv_msg(void)
4804 struct ipmi_recv_msg *rv;
4806 rv = kmalloc(sizeof(struct ipmi_recv_msg), GFP_ATOMIC);
4809 rv->done = free_recv_msg;
4810 atomic_inc(&recv_msg_inuse_count);
4815 void ipmi_free_recv_msg(struct ipmi_recv_msg *msg)
4818 kref_put(&msg->user->refcount, free_user);
4821 EXPORT_SYMBOL(ipmi_free_recv_msg);
4823 static atomic_t panic_done_count = ATOMIC_INIT(0);
4825 static void dummy_smi_done_handler(struct ipmi_smi_msg *msg)
4827 atomic_dec(&panic_done_count);
4830 static void dummy_recv_done_handler(struct ipmi_recv_msg *msg)
4832 atomic_dec(&panic_done_count);
4836 * Inside a panic, send a message and wait for a response.
4838 static void ipmi_panic_request_and_wait(struct ipmi_smi *intf,
4839 struct ipmi_addr *addr,
4840 struct kernel_ipmi_msg *msg)
4842 struct ipmi_smi_msg smi_msg;
4843 struct ipmi_recv_msg recv_msg;
4846 smi_msg.done = dummy_smi_done_handler;
4847 recv_msg.done = dummy_recv_done_handler;
4848 atomic_add(2, &panic_done_count);
4849 rv = i_ipmi_request(NULL,
4858 intf->addrinfo[0].address,
4859 intf->addrinfo[0].lun,
4860 0, 1); /* Don't retry, and don't wait. */
4862 atomic_sub(2, &panic_done_count);
4863 else if (intf->handlers->flush_messages)
4864 intf->handlers->flush_messages(intf->send_info);
4866 while (atomic_read(&panic_done_count) != 0)
4870 static void event_receiver_fetcher(struct ipmi_smi *intf,
4871 struct ipmi_recv_msg *msg)
4873 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
4874 && (msg->msg.netfn == IPMI_NETFN_SENSOR_EVENT_RESPONSE)
4875 && (msg->msg.cmd == IPMI_GET_EVENT_RECEIVER_CMD)
4876 && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) {
4877 /* A get event receiver command, save it. */
4878 intf->event_receiver = msg->msg.data[1];
4879 intf->event_receiver_lun = msg->msg.data[2] & 0x3;
4883 static void device_id_fetcher(struct ipmi_smi *intf, struct ipmi_recv_msg *msg)
4885 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
4886 && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
4887 && (msg->msg.cmd == IPMI_GET_DEVICE_ID_CMD)
4888 && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) {
4890 * A get device id command, save if we are an event
4891 * receiver or generator.
4893 intf->local_sel_device = (msg->msg.data[6] >> 2) & 1;
4894 intf->local_event_generator = (msg->msg.data[6] >> 5) & 1;
4898 static void send_panic_events(struct ipmi_smi *intf, char *str)
4900 struct kernel_ipmi_msg msg;
4901 unsigned char data[16];
4902 struct ipmi_system_interface_addr *si;
4903 struct ipmi_addr addr;
4905 struct ipmi_ipmb_addr *ipmb;
4908 if (ipmi_send_panic_event == IPMI_SEND_PANIC_EVENT_NONE)
4911 si = (struct ipmi_system_interface_addr *) &addr;
4912 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
4913 si->channel = IPMI_BMC_CHANNEL;
4916 /* Fill in an event telling that we have failed. */
4917 msg.netfn = 0x04; /* Sensor or Event. */
4918 msg.cmd = 2; /* Platform event command. */
4921 data[0] = 0x41; /* Kernel generator ID, IPMI table 5-4 */
4922 data[1] = 0x03; /* This is for IPMI 1.0. */
4923 data[2] = 0x20; /* OS Critical Stop, IPMI table 36-3 */
4924 data[4] = 0x6f; /* Sensor specific, IPMI table 36-1 */
4925 data[5] = 0xa1; /* Runtime stop OEM bytes 2 & 3. */
4928 * Put a few breadcrumbs in. Hopefully later we can add more things
4929 * to make the panic events more useful.
4937 /* Send the event announcing the panic. */
4938 ipmi_panic_request_and_wait(intf, &addr, &msg);
4941 * On every interface, dump a bunch of OEM event holding the
4944 if (ipmi_send_panic_event != IPMI_SEND_PANIC_EVENT_STRING || !str)
4948 * intf_num is used as an marker to tell if the
4949 * interface is valid. Thus we need a read barrier to
4950 * make sure data fetched before checking intf_num
4956 * First job here is to figure out where to send the
4957 * OEM events. There's no way in IPMI to send OEM
4958 * events using an event send command, so we have to
4959 * find the SEL to put them in and stick them in
4963 /* Get capabilities from the get device id. */
4964 intf->local_sel_device = 0;
4965 intf->local_event_generator = 0;
4966 intf->event_receiver = 0;
4968 /* Request the device info from the local MC. */
4969 msg.netfn = IPMI_NETFN_APP_REQUEST;
4970 msg.cmd = IPMI_GET_DEVICE_ID_CMD;
4973 intf->null_user_handler = device_id_fetcher;
4974 ipmi_panic_request_and_wait(intf, &addr, &msg);
4976 if (intf->local_event_generator) {
4977 /* Request the event receiver from the local MC. */
4978 msg.netfn = IPMI_NETFN_SENSOR_EVENT_REQUEST;
4979 msg.cmd = IPMI_GET_EVENT_RECEIVER_CMD;
4982 intf->null_user_handler = event_receiver_fetcher;
4983 ipmi_panic_request_and_wait(intf, &addr, &msg);
4985 intf->null_user_handler = NULL;
4988 * Validate the event receiver. The low bit must not
4989 * be 1 (it must be a valid IPMB address), it cannot
4990 * be zero, and it must not be my address.
4992 if (((intf->event_receiver & 1) == 0)
4993 && (intf->event_receiver != 0)
4994 && (intf->event_receiver != intf->addrinfo[0].address)) {
4996 * The event receiver is valid, send an IPMB
4999 ipmb = (struct ipmi_ipmb_addr *) &addr;
5000 ipmb->addr_type = IPMI_IPMB_ADDR_TYPE;
5001 ipmb->channel = 0; /* FIXME - is this right? */
5002 ipmb->lun = intf->event_receiver_lun;
5003 ipmb->slave_addr = intf->event_receiver;
5004 } else if (intf->local_sel_device) {
5006 * The event receiver was not valid (or was
5007 * me), but I am an SEL device, just dump it
5010 si = (struct ipmi_system_interface_addr *) &addr;
5011 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
5012 si->channel = IPMI_BMC_CHANNEL;
5015 return; /* No where to send the event. */
5017 msg.netfn = IPMI_NETFN_STORAGE_REQUEST; /* Storage. */
5018 msg.cmd = IPMI_ADD_SEL_ENTRY_CMD;
5024 int size = strlen(p);
5030 data[2] = 0xf0; /* OEM event without timestamp. */
5031 data[3] = intf->addrinfo[0].address;
5032 data[4] = j++; /* sequence # */
5034 * Always give 11 bytes, so strncpy will fill
5035 * it with zeroes for me.
5037 strncpy(data+5, p, 11);
5040 ipmi_panic_request_and_wait(intf, &addr, &msg);
5044 static int has_panicked;
5046 static int panic_event(struct notifier_block *this,
5047 unsigned long event,
5050 struct ipmi_smi *intf;
5051 struct ipmi_user *user;
5057 /* For every registered interface, set it to run to completion. */
5058 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
5059 if (!intf->handlers || intf->intf_num == -1)
5060 /* Interface is not ready. */
5063 if (!intf->handlers->poll)
5067 * If we were interrupted while locking xmit_msgs_lock or
5068 * waiting_rcv_msgs_lock, the corresponding list may be
5069 * corrupted. In this case, drop items on the list for
5072 if (!spin_trylock(&intf->xmit_msgs_lock)) {
5073 INIT_LIST_HEAD(&intf->xmit_msgs);
5074 INIT_LIST_HEAD(&intf->hp_xmit_msgs);
5076 spin_unlock(&intf->xmit_msgs_lock);
5078 if (!spin_trylock(&intf->waiting_rcv_msgs_lock))
5079 INIT_LIST_HEAD(&intf->waiting_rcv_msgs);
5081 spin_unlock(&intf->waiting_rcv_msgs_lock);
5083 intf->run_to_completion = 1;
5084 if (intf->handlers->set_run_to_completion)
5085 intf->handlers->set_run_to_completion(intf->send_info,
5088 list_for_each_entry_rcu(user, &intf->users, link) {
5089 if (user->handler->ipmi_panic_handler)
5090 user->handler->ipmi_panic_handler(
5091 user->handler_data);
5094 send_panic_events(intf, ptr);
5100 /* Must be called with ipmi_interfaces_mutex held. */
5101 static int ipmi_register_driver(void)
5108 rv = driver_register(&ipmidriver.driver);
5110 pr_err("Could not register IPMI driver\n");
5112 drvregistered = true;
5116 static struct notifier_block panic_block = {
5117 .notifier_call = panic_event,
5119 .priority = 200 /* priority: INT_MAX >= x >= 0 */
5122 static int ipmi_init_msghandler(void)
5126 mutex_lock(&ipmi_interfaces_mutex);
5127 rv = ipmi_register_driver();
5133 init_srcu_struct(&ipmi_interfaces_srcu);
5135 timer_setup(&ipmi_timer, ipmi_timeout, 0);
5136 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
5138 atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
5143 mutex_unlock(&ipmi_interfaces_mutex);
5147 static int __init ipmi_init_msghandler_mod(void)
5151 pr_info("version " IPMI_DRIVER_VERSION "\n");
5153 mutex_lock(&ipmi_interfaces_mutex);
5154 rv = ipmi_register_driver();
5155 mutex_unlock(&ipmi_interfaces_mutex);
5160 static void __exit cleanup_ipmi(void)
5165 atomic_notifier_chain_unregister(&panic_notifier_list,
5169 * This can't be called if any interfaces exist, so no worry
5170 * about shutting down the interfaces.
5174 * Tell the timer to stop, then wait for it to stop. This
5175 * avoids problems with race conditions removing the timer
5178 atomic_set(&stop_operation, 1);
5179 del_timer_sync(&ipmi_timer);
5181 initialized = false;
5183 /* Check for buffer leaks. */
5184 count = atomic_read(&smi_msg_inuse_count);
5186 pr_warn("SMI message count %d at exit\n", count);
5187 count = atomic_read(&recv_msg_inuse_count);
5189 pr_warn("recv message count %d at exit\n", count);
5191 cleanup_srcu_struct(&ipmi_interfaces_srcu);
5194 driver_unregister(&ipmidriver.driver);
5196 module_exit(cleanup_ipmi);
5198 module_init(ipmi_init_msghandler_mod);
5199 MODULE_LICENSE("GPL");
5201 MODULE_DESCRIPTION("Incoming and outgoing message routing for an IPMI"
5203 MODULE_VERSION(IPMI_DRIVER_VERSION);
5204 MODULE_SOFTDEP("post: ipmi_devintf");