1 // SPDX-License-Identifier: GPL-2.0+
5 * Incoming and outgoing message routing for an IPMI interface.
7 * Author: MontaVista Software, Inc.
8 * Corey Minyard <minyard@mvista.com>
11 * Copyright 2002 MontaVista Software Inc.
14 #define pr_fmt(fmt) "%s" fmt, "IPMI message handler: "
15 #define dev_fmt pr_fmt
17 #include <linux/module.h>
18 #include <linux/errno.h>
19 #include <linux/panic_notifier.h>
20 #include <linux/poll.h>
21 #include <linux/sched.h>
22 #include <linux/seq_file.h>
23 #include <linux/spinlock.h>
24 #include <linux/mutex.h>
25 #include <linux/slab.h>
26 #include <linux/ipmi.h>
27 #include <linux/ipmi_smi.h>
28 #include <linux/notifier.h>
29 #include <linux/init.h>
30 #include <linux/proc_fs.h>
31 #include <linux/rcupdate.h>
32 #include <linux/interrupt.h>
33 #include <linux/moduleparam.h>
34 #include <linux/workqueue.h>
35 #include <linux/uuid.h>
36 #include <linux/nospec.h>
37 #include <linux/vmalloc.h>
38 #include <linux/delay.h>
40 #define IPMI_DRIVER_VERSION "39.2"
42 static struct ipmi_recv_msg *ipmi_alloc_recv_msg(void);
43 static int ipmi_init_msghandler(void);
44 static void smi_recv_tasklet(struct tasklet_struct *t);
45 static void handle_new_recv_msgs(struct ipmi_smi *intf);
46 static void need_waiter(struct ipmi_smi *intf);
47 static int handle_one_recv_msg(struct ipmi_smi *intf,
48 struct ipmi_smi_msg *msg);
50 static bool initialized;
51 static bool drvregistered;
53 /* Numbers in this enumerator should be mapped to ipmi_panic_event_str */
54 enum ipmi_panic_event_op {
55 IPMI_SEND_PANIC_EVENT_NONE,
56 IPMI_SEND_PANIC_EVENT,
57 IPMI_SEND_PANIC_EVENT_STRING,
58 IPMI_SEND_PANIC_EVENT_MAX
61 /* Indices in this array should be mapped to enum ipmi_panic_event_op */
62 static const char *const ipmi_panic_event_str[] = { "none", "event", "string", NULL };
64 #ifdef CONFIG_IPMI_PANIC_STRING
65 #define IPMI_PANIC_DEFAULT IPMI_SEND_PANIC_EVENT_STRING
66 #elif defined(CONFIG_IPMI_PANIC_EVENT)
67 #define IPMI_PANIC_DEFAULT IPMI_SEND_PANIC_EVENT
69 #define IPMI_PANIC_DEFAULT IPMI_SEND_PANIC_EVENT_NONE
72 static enum ipmi_panic_event_op ipmi_send_panic_event = IPMI_PANIC_DEFAULT;
74 static int panic_op_write_handler(const char *val,
75 const struct kernel_param *kp)
80 strscpy(valcp, val, sizeof(valcp));
81 e = match_string(ipmi_panic_event_str, -1, strstrip(valcp));
85 ipmi_send_panic_event = e;
89 static int panic_op_read_handler(char *buffer, const struct kernel_param *kp)
91 const char *event_str;
93 if (ipmi_send_panic_event >= IPMI_SEND_PANIC_EVENT_MAX)
96 event_str = ipmi_panic_event_str[ipmi_send_panic_event];
98 return sprintf(buffer, "%s\n", event_str);
101 static const struct kernel_param_ops panic_op_ops = {
102 .set = panic_op_write_handler,
103 .get = panic_op_read_handler
105 module_param_cb(panic_op, &panic_op_ops, NULL, 0600);
106 MODULE_PARM_DESC(panic_op, "Sets if the IPMI driver will attempt to store panic information in the event log in the event of a panic. Set to 'none' for no, 'event' for a single event, or 'string' for a generic event and the panic string in IPMI OEM events.");
109 #define MAX_EVENTS_IN_QUEUE 25
111 /* Remain in auto-maintenance mode for this amount of time (in ms). */
112 static unsigned long maintenance_mode_timeout_ms = 30000;
113 module_param(maintenance_mode_timeout_ms, ulong, 0644);
114 MODULE_PARM_DESC(maintenance_mode_timeout_ms,
115 "The time (milliseconds) after the last maintenance message that the connection stays in maintenance mode.");
118 * Don't let a message sit in a queue forever, always time it with at lest
119 * the max message timer. This is in milliseconds.
121 #define MAX_MSG_TIMEOUT 60000
124 * Timeout times below are in milliseconds, and are done off a 1
125 * second timer. So setting the value to 1000 would mean anything
126 * between 0 and 1000ms. So really the only reasonable minimum
127 * setting it 2000ms, which is between 1 and 2 seconds.
130 /* The default timeout for message retries. */
131 static unsigned long default_retry_ms = 2000;
132 module_param(default_retry_ms, ulong, 0644);
133 MODULE_PARM_DESC(default_retry_ms,
134 "The time (milliseconds) between retry sends");
136 /* The default timeout for maintenance mode message retries. */
137 static unsigned long default_maintenance_retry_ms = 3000;
138 module_param(default_maintenance_retry_ms, ulong, 0644);
139 MODULE_PARM_DESC(default_maintenance_retry_ms,
140 "The time (milliseconds) between retry sends in maintenance mode");
142 /* The default maximum number of retries */
143 static unsigned int default_max_retries = 4;
144 module_param(default_max_retries, uint, 0644);
145 MODULE_PARM_DESC(default_max_retries,
146 "The time (milliseconds) between retry sends in maintenance mode");
148 /* Call every ~1000 ms. */
149 #define IPMI_TIMEOUT_TIME 1000
151 /* How many jiffies does it take to get to the timeout time. */
152 #define IPMI_TIMEOUT_JIFFIES ((IPMI_TIMEOUT_TIME * HZ) / 1000)
155 * Request events from the queue every second (this is the number of
156 * IPMI_TIMEOUT_TIMES between event requests). Hopefully, in the
157 * future, IPMI will add a way to know immediately if an event is in
158 * the queue and this silliness can go away.
160 #define IPMI_REQUEST_EV_TIME (1000 / (IPMI_TIMEOUT_TIME))
162 /* How long should we cache dynamic device IDs? */
163 #define IPMI_DYN_DEV_ID_EXPIRY (10 * HZ)
166 * The main "user" data structure.
169 struct list_head link;
172 * Set to NULL when the user is destroyed, a pointer to myself
173 * so srcu_dereference can be used on it.
175 struct ipmi_user *self;
176 struct srcu_struct release_barrier;
178 struct kref refcount;
180 /* The upper layer that handles receive messages. */
181 const struct ipmi_user_hndl *handler;
184 /* The interface this user is bound to. */
185 struct ipmi_smi *intf;
187 /* Does this interface receive IPMI events? */
190 /* Free must run in process context for RCU cleanup. */
191 struct work_struct remove_work;
194 static struct workqueue_struct *remove_work_wq;
196 static struct ipmi_user *acquire_ipmi_user(struct ipmi_user *user, int *index)
197 __acquires(user->release_barrier)
199 struct ipmi_user *ruser;
201 *index = srcu_read_lock(&user->release_barrier);
202 ruser = srcu_dereference(user->self, &user->release_barrier);
204 srcu_read_unlock(&user->release_barrier, *index);
208 static void release_ipmi_user(struct ipmi_user *user, int index)
210 srcu_read_unlock(&user->release_barrier, index);
214 struct list_head link;
216 struct ipmi_user *user;
222 * This is used to form a linked lised during mass deletion.
223 * Since this is in an RCU list, we cannot use the link above
224 * or change any data until the RCU period completes. So we
225 * use this next variable during mass deletion so we can have
226 * a list and don't have to wait and restart the search on
227 * every individual deletion of a command.
229 struct cmd_rcvr *next;
233 unsigned int inuse : 1;
234 unsigned int broadcast : 1;
236 unsigned long timeout;
237 unsigned long orig_timeout;
238 unsigned int retries_left;
241 * To verify on an incoming send message response that this is
242 * the message that the response is for, we keep a sequence id
243 * and increment it every time we send a message.
248 * This is held so we can properly respond to the message on a
249 * timeout, and it is used to hold the temporary data for
250 * retransmission, too.
252 struct ipmi_recv_msg *recv_msg;
256 * Store the information in a msgid (long) to allow us to find a
257 * sequence table entry from the msgid.
259 #define STORE_SEQ_IN_MSGID(seq, seqid) \
260 ((((seq) & 0x3f) << 26) | ((seqid) & 0x3ffffff))
262 #define GET_SEQ_FROM_MSGID(msgid, seq, seqid) \
264 seq = (((msgid) >> 26) & 0x3f); \
265 seqid = ((msgid) & 0x3ffffff); \
268 #define NEXT_SEQID(seqid) (((seqid) + 1) & 0x3ffffff)
270 #define IPMI_MAX_CHANNELS 16
271 struct ipmi_channel {
272 unsigned char medium;
273 unsigned char protocol;
276 struct ipmi_channel_set {
277 struct ipmi_channel c[IPMI_MAX_CHANNELS];
280 struct ipmi_my_addrinfo {
282 * My slave address. This is initialized to IPMI_BMC_SLAVE_ADDR,
283 * but may be changed by the user.
285 unsigned char address;
288 * My LUN. This should generally stay the SMS LUN, but just in
295 * Note that the product id, manufacturer id, guid, and device id are
296 * immutable in this structure, so dyn_mutex is not required for
297 * accessing those. If those change on a BMC, a new BMC is allocated.
300 struct platform_device pdev;
301 struct list_head intfs; /* Interfaces on this BMC. */
302 struct ipmi_device_id id;
303 struct ipmi_device_id fetch_id;
305 unsigned long dyn_id_expiry;
306 struct mutex dyn_mutex; /* Protects id, intfs, & dyn* */
310 struct kref usecount;
311 struct work_struct remove_work;
312 unsigned char cc; /* completion code */
314 #define to_bmc_device(x) container_of((x), struct bmc_device, pdev.dev)
316 static int bmc_get_device_id(struct ipmi_smi *intf, struct bmc_device *bmc,
317 struct ipmi_device_id *id,
318 bool *guid_set, guid_t *guid);
321 * Various statistics for IPMI, these index stats[] in the ipmi_smi
324 enum ipmi_stat_indexes {
325 /* Commands we got from the user that were invalid. */
326 IPMI_STAT_sent_invalid_commands = 0,
328 /* Commands we sent to the MC. */
329 IPMI_STAT_sent_local_commands,
331 /* Responses from the MC that were delivered to a user. */
332 IPMI_STAT_handled_local_responses,
334 /* Responses from the MC that were not delivered to a user. */
335 IPMI_STAT_unhandled_local_responses,
337 /* Commands we sent out to the IPMB bus. */
338 IPMI_STAT_sent_ipmb_commands,
340 /* Commands sent on the IPMB that had errors on the SEND CMD */
341 IPMI_STAT_sent_ipmb_command_errs,
343 /* Each retransmit increments this count. */
344 IPMI_STAT_retransmitted_ipmb_commands,
347 * When a message times out (runs out of retransmits) this is
350 IPMI_STAT_timed_out_ipmb_commands,
353 * This is like above, but for broadcasts. Broadcasts are
354 * *not* included in the above count (they are expected to
357 IPMI_STAT_timed_out_ipmb_broadcasts,
359 /* Responses I have sent to the IPMB bus. */
360 IPMI_STAT_sent_ipmb_responses,
362 /* The response was delivered to the user. */
363 IPMI_STAT_handled_ipmb_responses,
365 /* The response had invalid data in it. */
366 IPMI_STAT_invalid_ipmb_responses,
368 /* The response didn't have anyone waiting for it. */
369 IPMI_STAT_unhandled_ipmb_responses,
371 /* Commands we sent out to the IPMB bus. */
372 IPMI_STAT_sent_lan_commands,
374 /* Commands sent on the IPMB that had errors on the SEND CMD */
375 IPMI_STAT_sent_lan_command_errs,
377 /* Each retransmit increments this count. */
378 IPMI_STAT_retransmitted_lan_commands,
381 * When a message times out (runs out of retransmits) this is
384 IPMI_STAT_timed_out_lan_commands,
386 /* Responses I have sent to the IPMB bus. */
387 IPMI_STAT_sent_lan_responses,
389 /* The response was delivered to the user. */
390 IPMI_STAT_handled_lan_responses,
392 /* The response had invalid data in it. */
393 IPMI_STAT_invalid_lan_responses,
395 /* The response didn't have anyone waiting for it. */
396 IPMI_STAT_unhandled_lan_responses,
398 /* The command was delivered to the user. */
399 IPMI_STAT_handled_commands,
401 /* The command had invalid data in it. */
402 IPMI_STAT_invalid_commands,
404 /* The command didn't have anyone waiting for it. */
405 IPMI_STAT_unhandled_commands,
407 /* Invalid data in an event. */
408 IPMI_STAT_invalid_events,
410 /* Events that were received with the proper format. */
413 /* Retransmissions on IPMB that failed. */
414 IPMI_STAT_dropped_rexmit_ipmb_commands,
416 /* Retransmissions on LAN that failed. */
417 IPMI_STAT_dropped_rexmit_lan_commands,
419 /* This *must* remain last, add new values above this. */
424 #define IPMI_IPMB_NUM_SEQ 64
426 struct module *owner;
428 /* What interface number are we? */
431 struct kref refcount;
433 /* Set when the interface is being unregistered. */
436 /* Used for a list of interfaces. */
437 struct list_head link;
440 * The list of upper layers that are using me. seq_lock write
441 * protects this. Read protection is with srcu.
443 struct list_head users;
444 struct srcu_struct users_srcu;
446 /* Used for wake ups at startup. */
447 wait_queue_head_t waitq;
450 * Prevents the interface from being unregistered when the
451 * interface is used by being looked up through the BMC
454 struct mutex bmc_reg_mutex;
456 struct bmc_device tmp_bmc;
457 struct bmc_device *bmc;
459 struct list_head bmc_link;
461 bool in_bmc_register; /* Handle recursive situations. Yuck. */
462 struct work_struct bmc_reg_work;
464 const struct ipmi_smi_handlers *handlers;
467 /* Driver-model device for the system interface. */
468 struct device *si_dev;
471 * A table of sequence numbers for this interface. We use the
472 * sequence numbers for IPMB messages that go out of the
473 * interface to match them up with their responses. A routine
474 * is called periodically to time the items in this list.
477 struct seq_table seq_table[IPMI_IPMB_NUM_SEQ];
481 * Messages queued for delivery. If delivery fails (out of memory
482 * for instance), They will stay in here to be processed later in a
483 * periodic timer interrupt. The tasklet is for handling received
484 * messages directly from the handler.
486 spinlock_t waiting_rcv_msgs_lock;
487 struct list_head waiting_rcv_msgs;
488 atomic_t watchdog_pretimeouts_to_deliver;
489 struct tasklet_struct recv_tasklet;
491 spinlock_t xmit_msgs_lock;
492 struct list_head xmit_msgs;
493 struct ipmi_smi_msg *curr_msg;
494 struct list_head hp_xmit_msgs;
497 * The list of command receivers that are registered for commands
500 struct mutex cmd_rcvrs_mutex;
501 struct list_head cmd_rcvrs;
504 * Events that were queues because no one was there to receive
507 spinlock_t events_lock; /* For dealing with event stuff. */
508 struct list_head waiting_events;
509 unsigned int waiting_events_count; /* How many events in queue? */
510 char delivering_events;
511 char event_msg_printed;
513 /* How many users are waiting for events? */
514 atomic_t event_waiters;
515 unsigned int ticks_to_req_ev;
517 spinlock_t watch_lock; /* For dealing with watch stuff below. */
519 /* How many users are waiting for commands? */
520 unsigned int command_waiters;
522 /* How many users are waiting for watchdogs? */
523 unsigned int watchdog_waiters;
525 /* How many users are waiting for message responses? */
526 unsigned int response_waiters;
529 * Tells what the lower layer has last been asked to watch for,
530 * messages and/or watchdogs. Protected by watch_lock.
532 unsigned int last_watch_mask;
535 * The event receiver for my BMC, only really used at panic
536 * shutdown as a place to store this.
538 unsigned char event_receiver;
539 unsigned char event_receiver_lun;
540 unsigned char local_sel_device;
541 unsigned char local_event_generator;
543 /* For handling of maintenance mode. */
544 int maintenance_mode;
545 bool maintenance_mode_enable;
546 int auto_maintenance_timeout;
547 spinlock_t maintenance_mode_lock; /* Used in a timer... */
550 * If we are doing maintenance on something on IPMB, extend
551 * the timeout time to avoid timeouts writing firmware and
554 int ipmb_maintenance_mode_timeout;
557 * A cheap hack, if this is non-null and a message to an
558 * interface comes in with a NULL user, call this routine with
559 * it. Note that the message will still be freed by the
560 * caller. This only works on the system interface.
562 * Protected by bmc_reg_mutex.
564 void (*null_user_handler)(struct ipmi_smi *intf,
565 struct ipmi_recv_msg *msg);
568 * When we are scanning the channels for an SMI, this will
569 * tell which channel we are scanning.
573 /* Channel information */
574 struct ipmi_channel_set *channel_list;
575 unsigned int curr_working_cset; /* First index into the following. */
576 struct ipmi_channel_set wchannels[2];
577 struct ipmi_my_addrinfo addrinfo[IPMI_MAX_CHANNELS];
580 atomic_t stats[IPMI_NUM_STATS];
583 * run_to_completion duplicate of smb_info, smi_info
584 * and ipmi_serial_info structures. Used to decrease numbers of
585 * parameters passed by "low" level IPMI code.
587 int run_to_completion;
589 #define to_si_intf_from_dev(device) container_of(device, struct ipmi_smi, dev)
591 static void __get_guid(struct ipmi_smi *intf);
592 static void __ipmi_bmc_unregister(struct ipmi_smi *intf);
593 static int __ipmi_bmc_register(struct ipmi_smi *intf,
594 struct ipmi_device_id *id,
595 bool guid_set, guid_t *guid, int intf_num);
596 static int __scan_channels(struct ipmi_smi *intf, struct ipmi_device_id *id);
600 * The driver model view of the IPMI messaging driver.
602 static struct platform_driver ipmidriver = {
605 .bus = &platform_bus_type
609 * This mutex keeps us from adding the same BMC twice.
611 static DEFINE_MUTEX(ipmidriver_mutex);
613 static LIST_HEAD(ipmi_interfaces);
614 static DEFINE_MUTEX(ipmi_interfaces_mutex);
615 #define ipmi_interfaces_mutex_held() \
616 lockdep_is_held(&ipmi_interfaces_mutex)
617 static struct srcu_struct ipmi_interfaces_srcu;
620 * List of watchers that want to know when smi's are added and deleted.
622 static LIST_HEAD(smi_watchers);
623 static DEFINE_MUTEX(smi_watchers_mutex);
625 #define ipmi_inc_stat(intf, stat) \
626 atomic_inc(&(intf)->stats[IPMI_STAT_ ## stat])
627 #define ipmi_get_stat(intf, stat) \
628 ((unsigned int) atomic_read(&(intf)->stats[IPMI_STAT_ ## stat]))
630 static const char * const addr_src_to_str[] = {
631 "invalid", "hotmod", "hardcoded", "SPMI", "ACPI", "SMBIOS", "PCI",
632 "device-tree", "platform"
635 const char *ipmi_addr_src_to_str(enum ipmi_addr_src src)
638 src = 0; /* Invalid */
639 return addr_src_to_str[src];
641 EXPORT_SYMBOL(ipmi_addr_src_to_str);
643 static int is_lan_addr(struct ipmi_addr *addr)
645 return addr->addr_type == IPMI_LAN_ADDR_TYPE;
648 static int is_ipmb_addr(struct ipmi_addr *addr)
650 return addr->addr_type == IPMI_IPMB_ADDR_TYPE;
653 static int is_ipmb_bcast_addr(struct ipmi_addr *addr)
655 return addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE;
658 static int is_ipmb_direct_addr(struct ipmi_addr *addr)
660 return addr->addr_type == IPMI_IPMB_DIRECT_ADDR_TYPE;
663 static void free_recv_msg_list(struct list_head *q)
665 struct ipmi_recv_msg *msg, *msg2;
667 list_for_each_entry_safe(msg, msg2, q, link) {
668 list_del(&msg->link);
669 ipmi_free_recv_msg(msg);
673 static void free_smi_msg_list(struct list_head *q)
675 struct ipmi_smi_msg *msg, *msg2;
677 list_for_each_entry_safe(msg, msg2, q, link) {
678 list_del(&msg->link);
679 ipmi_free_smi_msg(msg);
683 static void clean_up_interface_data(struct ipmi_smi *intf)
686 struct cmd_rcvr *rcvr, *rcvr2;
687 struct list_head list;
689 tasklet_kill(&intf->recv_tasklet);
691 free_smi_msg_list(&intf->waiting_rcv_msgs);
692 free_recv_msg_list(&intf->waiting_events);
695 * Wholesale remove all the entries from the list in the
696 * interface and wait for RCU to know that none are in use.
698 mutex_lock(&intf->cmd_rcvrs_mutex);
699 INIT_LIST_HEAD(&list);
700 list_splice_init_rcu(&intf->cmd_rcvrs, &list, synchronize_rcu);
701 mutex_unlock(&intf->cmd_rcvrs_mutex);
703 list_for_each_entry_safe(rcvr, rcvr2, &list, link)
706 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
707 if ((intf->seq_table[i].inuse)
708 && (intf->seq_table[i].recv_msg))
709 ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
713 static void intf_free(struct kref *ref)
715 struct ipmi_smi *intf = container_of(ref, struct ipmi_smi, refcount);
717 clean_up_interface_data(intf);
721 struct watcher_entry {
723 struct ipmi_smi *intf;
724 struct list_head link;
727 int ipmi_smi_watcher_register(struct ipmi_smi_watcher *watcher)
729 struct ipmi_smi *intf;
733 * Make sure the driver is actually initialized, this handles
734 * problems with initialization order.
736 rv = ipmi_init_msghandler();
740 mutex_lock(&smi_watchers_mutex);
742 list_add(&watcher->link, &smi_watchers);
744 index = srcu_read_lock(&ipmi_interfaces_srcu);
745 list_for_each_entry_rcu(intf, &ipmi_interfaces, link,
746 lockdep_is_held(&smi_watchers_mutex)) {
747 int intf_num = READ_ONCE(intf->intf_num);
751 watcher->new_smi(intf_num, intf->si_dev);
753 srcu_read_unlock(&ipmi_interfaces_srcu, index);
755 mutex_unlock(&smi_watchers_mutex);
759 EXPORT_SYMBOL(ipmi_smi_watcher_register);
761 int ipmi_smi_watcher_unregister(struct ipmi_smi_watcher *watcher)
763 mutex_lock(&smi_watchers_mutex);
764 list_del(&watcher->link);
765 mutex_unlock(&smi_watchers_mutex);
768 EXPORT_SYMBOL(ipmi_smi_watcher_unregister);
771 * Must be called with smi_watchers_mutex held.
774 call_smi_watchers(int i, struct device *dev)
776 struct ipmi_smi_watcher *w;
778 mutex_lock(&smi_watchers_mutex);
779 list_for_each_entry(w, &smi_watchers, link) {
780 if (try_module_get(w->owner)) {
782 module_put(w->owner);
785 mutex_unlock(&smi_watchers_mutex);
789 ipmi_addr_equal(struct ipmi_addr *addr1, struct ipmi_addr *addr2)
791 if (addr1->addr_type != addr2->addr_type)
794 if (addr1->channel != addr2->channel)
797 if (addr1->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
798 struct ipmi_system_interface_addr *smi_addr1
799 = (struct ipmi_system_interface_addr *) addr1;
800 struct ipmi_system_interface_addr *smi_addr2
801 = (struct ipmi_system_interface_addr *) addr2;
802 return (smi_addr1->lun == smi_addr2->lun);
805 if (is_ipmb_addr(addr1) || is_ipmb_bcast_addr(addr1)) {
806 struct ipmi_ipmb_addr *ipmb_addr1
807 = (struct ipmi_ipmb_addr *) addr1;
808 struct ipmi_ipmb_addr *ipmb_addr2
809 = (struct ipmi_ipmb_addr *) addr2;
811 return ((ipmb_addr1->slave_addr == ipmb_addr2->slave_addr)
812 && (ipmb_addr1->lun == ipmb_addr2->lun));
815 if (is_ipmb_direct_addr(addr1)) {
816 struct ipmi_ipmb_direct_addr *daddr1
817 = (struct ipmi_ipmb_direct_addr *) addr1;
818 struct ipmi_ipmb_direct_addr *daddr2
819 = (struct ipmi_ipmb_direct_addr *) addr2;
821 return daddr1->slave_addr == daddr2->slave_addr &&
822 daddr1->rq_lun == daddr2->rq_lun &&
823 daddr1->rs_lun == daddr2->rs_lun;
826 if (is_lan_addr(addr1)) {
827 struct ipmi_lan_addr *lan_addr1
828 = (struct ipmi_lan_addr *) addr1;
829 struct ipmi_lan_addr *lan_addr2
830 = (struct ipmi_lan_addr *) addr2;
832 return ((lan_addr1->remote_SWID == lan_addr2->remote_SWID)
833 && (lan_addr1->local_SWID == lan_addr2->local_SWID)
834 && (lan_addr1->session_handle
835 == lan_addr2->session_handle)
836 && (lan_addr1->lun == lan_addr2->lun));
842 int ipmi_validate_addr(struct ipmi_addr *addr, int len)
844 if (len < sizeof(struct ipmi_system_interface_addr))
847 if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
848 if (addr->channel != IPMI_BMC_CHANNEL)
853 if ((addr->channel == IPMI_BMC_CHANNEL)
854 || (addr->channel >= IPMI_MAX_CHANNELS)
855 || (addr->channel < 0))
858 if (is_ipmb_addr(addr) || is_ipmb_bcast_addr(addr)) {
859 if (len < sizeof(struct ipmi_ipmb_addr))
864 if (is_ipmb_direct_addr(addr)) {
865 struct ipmi_ipmb_direct_addr *daddr = (void *) addr;
867 if (addr->channel != 0)
869 if (len < sizeof(struct ipmi_ipmb_direct_addr))
872 if (daddr->slave_addr & 0x01)
874 if (daddr->rq_lun >= 4)
876 if (daddr->rs_lun >= 4)
881 if (is_lan_addr(addr)) {
882 if (len < sizeof(struct ipmi_lan_addr))
889 EXPORT_SYMBOL(ipmi_validate_addr);
891 unsigned int ipmi_addr_length(int addr_type)
893 if (addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
894 return sizeof(struct ipmi_system_interface_addr);
896 if ((addr_type == IPMI_IPMB_ADDR_TYPE)
897 || (addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE))
898 return sizeof(struct ipmi_ipmb_addr);
900 if (addr_type == IPMI_IPMB_DIRECT_ADDR_TYPE)
901 return sizeof(struct ipmi_ipmb_direct_addr);
903 if (addr_type == IPMI_LAN_ADDR_TYPE)
904 return sizeof(struct ipmi_lan_addr);
908 EXPORT_SYMBOL(ipmi_addr_length);
910 static int deliver_response(struct ipmi_smi *intf, struct ipmi_recv_msg *msg)
915 /* Special handling for NULL users. */
916 if (intf->null_user_handler) {
917 intf->null_user_handler(intf, msg);
919 /* No handler, so give up. */
922 ipmi_free_recv_msg(msg);
923 } else if (oops_in_progress) {
925 * If we are running in the panic context, calling the
926 * receive handler doesn't much meaning and has a deadlock
927 * risk. At this moment, simply skip it in that case.
929 ipmi_free_recv_msg(msg);
932 struct ipmi_user *user = acquire_ipmi_user(msg->user, &index);
935 user->handler->ipmi_recv_hndl(msg, user->handler_data);
936 release_ipmi_user(user, index);
938 /* User went away, give up. */
939 ipmi_free_recv_msg(msg);
947 static void deliver_local_response(struct ipmi_smi *intf,
948 struct ipmi_recv_msg *msg)
950 if (deliver_response(intf, msg))
951 ipmi_inc_stat(intf, unhandled_local_responses);
953 ipmi_inc_stat(intf, handled_local_responses);
956 static void deliver_err_response(struct ipmi_smi *intf,
957 struct ipmi_recv_msg *msg, int err)
959 msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
960 msg->msg_data[0] = err;
961 msg->msg.netfn |= 1; /* Convert to a response. */
962 msg->msg.data_len = 1;
963 msg->msg.data = msg->msg_data;
964 deliver_local_response(intf, msg);
967 static void smi_add_watch(struct ipmi_smi *intf, unsigned int flags)
969 unsigned long iflags;
971 if (!intf->handlers->set_need_watch)
974 spin_lock_irqsave(&intf->watch_lock, iflags);
975 if (flags & IPMI_WATCH_MASK_CHECK_MESSAGES)
976 intf->response_waiters++;
978 if (flags & IPMI_WATCH_MASK_CHECK_WATCHDOG)
979 intf->watchdog_waiters++;
981 if (flags & IPMI_WATCH_MASK_CHECK_COMMANDS)
982 intf->command_waiters++;
984 if ((intf->last_watch_mask & flags) != 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);
992 static void smi_remove_watch(struct ipmi_smi *intf, unsigned int flags)
994 unsigned long iflags;
996 if (!intf->handlers->set_need_watch)
999 spin_lock_irqsave(&intf->watch_lock, iflags);
1000 if (flags & IPMI_WATCH_MASK_CHECK_MESSAGES)
1001 intf->response_waiters--;
1003 if (flags & IPMI_WATCH_MASK_CHECK_WATCHDOG)
1004 intf->watchdog_waiters--;
1006 if (flags & IPMI_WATCH_MASK_CHECK_COMMANDS)
1007 intf->command_waiters--;
1010 if (intf->response_waiters)
1011 flags |= IPMI_WATCH_MASK_CHECK_MESSAGES;
1012 if (intf->watchdog_waiters)
1013 flags |= IPMI_WATCH_MASK_CHECK_WATCHDOG;
1014 if (intf->command_waiters)
1015 flags |= IPMI_WATCH_MASK_CHECK_COMMANDS;
1017 if (intf->last_watch_mask != flags) {
1018 intf->last_watch_mask = flags;
1019 intf->handlers->set_need_watch(intf->send_info,
1020 intf->last_watch_mask);
1022 spin_unlock_irqrestore(&intf->watch_lock, iflags);
1026 * Find the next sequence number not being used and add the given
1027 * message with the given timeout to the sequence table. This must be
1028 * called with the interface's seq_lock held.
1030 static int intf_next_seq(struct ipmi_smi *intf,
1031 struct ipmi_recv_msg *recv_msg,
1032 unsigned long timeout,
1042 timeout = default_retry_ms;
1044 retries = default_max_retries;
1046 for (i = intf->curr_seq; (i+1)%IPMI_IPMB_NUM_SEQ != intf->curr_seq;
1047 i = (i+1)%IPMI_IPMB_NUM_SEQ) {
1048 if (!intf->seq_table[i].inuse)
1052 if (!intf->seq_table[i].inuse) {
1053 intf->seq_table[i].recv_msg = recv_msg;
1056 * Start with the maximum timeout, when the send response
1057 * comes in we will start the real timer.
1059 intf->seq_table[i].timeout = MAX_MSG_TIMEOUT;
1060 intf->seq_table[i].orig_timeout = timeout;
1061 intf->seq_table[i].retries_left = retries;
1062 intf->seq_table[i].broadcast = broadcast;
1063 intf->seq_table[i].inuse = 1;
1064 intf->seq_table[i].seqid = NEXT_SEQID(intf->seq_table[i].seqid);
1066 *seqid = intf->seq_table[i].seqid;
1067 intf->curr_seq = (i+1)%IPMI_IPMB_NUM_SEQ;
1068 smi_add_watch(intf, IPMI_WATCH_MASK_CHECK_MESSAGES);
1078 * Return the receive message for the given sequence number and
1079 * release the sequence number so it can be reused. Some other data
1080 * is passed in to be sure the message matches up correctly (to help
1081 * guard against message coming in after their timeout and the
1082 * sequence number being reused).
1084 static int intf_find_seq(struct ipmi_smi *intf,
1088 unsigned char netfn,
1089 struct ipmi_addr *addr,
1090 struct ipmi_recv_msg **recv_msg)
1093 unsigned long flags;
1095 if (seq >= IPMI_IPMB_NUM_SEQ)
1098 spin_lock_irqsave(&intf->seq_lock, flags);
1099 if (intf->seq_table[seq].inuse) {
1100 struct ipmi_recv_msg *msg = intf->seq_table[seq].recv_msg;
1102 if ((msg->addr.channel == channel) && (msg->msg.cmd == cmd)
1103 && (msg->msg.netfn == netfn)
1104 && (ipmi_addr_equal(addr, &msg->addr))) {
1106 intf->seq_table[seq].inuse = 0;
1107 smi_remove_watch(intf, IPMI_WATCH_MASK_CHECK_MESSAGES);
1111 spin_unlock_irqrestore(&intf->seq_lock, flags);
1117 /* Start the timer for a specific sequence table entry. */
1118 static int intf_start_seq_timer(struct ipmi_smi *intf,
1122 unsigned long flags;
1124 unsigned long seqid;
1127 GET_SEQ_FROM_MSGID(msgid, seq, seqid);
1129 spin_lock_irqsave(&intf->seq_lock, flags);
1131 * We do this verification because the user can be deleted
1132 * while a message is outstanding.
1134 if ((intf->seq_table[seq].inuse)
1135 && (intf->seq_table[seq].seqid == seqid)) {
1136 struct seq_table *ent = &intf->seq_table[seq];
1137 ent->timeout = ent->orig_timeout;
1140 spin_unlock_irqrestore(&intf->seq_lock, flags);
1145 /* Got an error for the send message for a specific sequence number. */
1146 static int intf_err_seq(struct ipmi_smi *intf,
1151 unsigned long flags;
1153 unsigned long seqid;
1154 struct ipmi_recv_msg *msg = NULL;
1157 GET_SEQ_FROM_MSGID(msgid, seq, seqid);
1159 spin_lock_irqsave(&intf->seq_lock, flags);
1161 * We do this verification because the user can be deleted
1162 * while a message is outstanding.
1164 if ((intf->seq_table[seq].inuse)
1165 && (intf->seq_table[seq].seqid == seqid)) {
1166 struct seq_table *ent = &intf->seq_table[seq];
1169 smi_remove_watch(intf, IPMI_WATCH_MASK_CHECK_MESSAGES);
1170 msg = ent->recv_msg;
1173 spin_unlock_irqrestore(&intf->seq_lock, flags);
1176 deliver_err_response(intf, msg, err);
1181 static void free_user_work(struct work_struct *work)
1183 struct ipmi_user *user = container_of(work, struct ipmi_user,
1186 cleanup_srcu_struct(&user->release_barrier);
1190 int ipmi_create_user(unsigned int if_num,
1191 const struct ipmi_user_hndl *handler,
1193 struct ipmi_user **user)
1195 unsigned long flags;
1196 struct ipmi_user *new_user;
1198 struct ipmi_smi *intf;
1201 * There is no module usecount here, because it's not
1202 * required. Since this can only be used by and called from
1203 * other modules, they will implicitly use this module, and
1204 * thus this can't be removed unless the other modules are
1208 if (handler == NULL)
1212 * Make sure the driver is actually initialized, this handles
1213 * problems with initialization order.
1215 rv = ipmi_init_msghandler();
1219 new_user = vzalloc(sizeof(*new_user));
1223 index = srcu_read_lock(&ipmi_interfaces_srcu);
1224 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
1225 if (intf->intf_num == if_num)
1228 /* Not found, return an error */
1233 INIT_WORK(&new_user->remove_work, free_user_work);
1235 rv = init_srcu_struct(&new_user->release_barrier);
1239 if (!try_module_get(intf->owner)) {
1244 /* Note that each existing user holds a refcount to the interface. */
1245 kref_get(&intf->refcount);
1247 kref_init(&new_user->refcount);
1248 new_user->handler = handler;
1249 new_user->handler_data = handler_data;
1250 new_user->intf = intf;
1251 new_user->gets_events = false;
1253 rcu_assign_pointer(new_user->self, new_user);
1254 spin_lock_irqsave(&intf->seq_lock, flags);
1255 list_add_rcu(&new_user->link, &intf->users);
1256 spin_unlock_irqrestore(&intf->seq_lock, flags);
1257 if (handler->ipmi_watchdog_pretimeout)
1258 /* User wants pretimeouts, so make sure to watch for them. */
1259 smi_add_watch(intf, IPMI_WATCH_MASK_CHECK_WATCHDOG);
1260 srcu_read_unlock(&ipmi_interfaces_srcu, index);
1265 srcu_read_unlock(&ipmi_interfaces_srcu, index);
1269 EXPORT_SYMBOL(ipmi_create_user);
1271 int ipmi_get_smi_info(int if_num, struct ipmi_smi_info *data)
1274 struct ipmi_smi *intf;
1276 index = srcu_read_lock(&ipmi_interfaces_srcu);
1277 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
1278 if (intf->intf_num == if_num)
1281 srcu_read_unlock(&ipmi_interfaces_srcu, index);
1283 /* Not found, return an error */
1287 if (!intf->handlers->get_smi_info)
1290 rv = intf->handlers->get_smi_info(intf->send_info, data);
1291 srcu_read_unlock(&ipmi_interfaces_srcu, index);
1295 EXPORT_SYMBOL(ipmi_get_smi_info);
1297 static void free_user(struct kref *ref)
1299 struct ipmi_user *user = container_of(ref, struct ipmi_user, refcount);
1301 /* SRCU cleanup must happen in task context. */
1302 queue_work(remove_work_wq, &user->remove_work);
1305 static void _ipmi_destroy_user(struct ipmi_user *user)
1307 struct ipmi_smi *intf = user->intf;
1309 unsigned long flags;
1310 struct cmd_rcvr *rcvr;
1311 struct cmd_rcvr *rcvrs = NULL;
1313 if (!acquire_ipmi_user(user, &i)) {
1315 * The user has already been cleaned up, just make sure
1316 * nothing is using it and return.
1318 synchronize_srcu(&user->release_barrier);
1322 rcu_assign_pointer(user->self, NULL);
1323 release_ipmi_user(user, i);
1325 synchronize_srcu(&user->release_barrier);
1327 if (user->handler->shutdown)
1328 user->handler->shutdown(user->handler_data);
1330 if (user->handler->ipmi_watchdog_pretimeout)
1331 smi_remove_watch(intf, IPMI_WATCH_MASK_CHECK_WATCHDOG);
1333 if (user->gets_events)
1334 atomic_dec(&intf->event_waiters);
1336 /* Remove the user from the interface's sequence table. */
1337 spin_lock_irqsave(&intf->seq_lock, flags);
1338 list_del_rcu(&user->link);
1340 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
1341 if (intf->seq_table[i].inuse
1342 && (intf->seq_table[i].recv_msg->user == user)) {
1343 intf->seq_table[i].inuse = 0;
1344 smi_remove_watch(intf, IPMI_WATCH_MASK_CHECK_MESSAGES);
1345 ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
1348 spin_unlock_irqrestore(&intf->seq_lock, flags);
1351 * Remove the user from the command receiver's table. First
1352 * we build a list of everything (not using the standard link,
1353 * since other things may be using it till we do
1354 * synchronize_srcu()) then free everything in that list.
1356 mutex_lock(&intf->cmd_rcvrs_mutex);
1357 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link,
1358 lockdep_is_held(&intf->cmd_rcvrs_mutex)) {
1359 if (rcvr->user == user) {
1360 list_del_rcu(&rcvr->link);
1365 mutex_unlock(&intf->cmd_rcvrs_mutex);
1373 kref_put(&intf->refcount, intf_free);
1374 module_put(intf->owner);
1377 int ipmi_destroy_user(struct ipmi_user *user)
1379 _ipmi_destroy_user(user);
1381 kref_put(&user->refcount, free_user);
1385 EXPORT_SYMBOL(ipmi_destroy_user);
1387 int ipmi_get_version(struct ipmi_user *user,
1388 unsigned char *major,
1389 unsigned char *minor)
1391 struct ipmi_device_id id;
1394 user = acquire_ipmi_user(user, &index);
1398 rv = bmc_get_device_id(user->intf, NULL, &id, NULL, NULL);
1400 *major = ipmi_version_major(&id);
1401 *minor = ipmi_version_minor(&id);
1403 release_ipmi_user(user, index);
1407 EXPORT_SYMBOL(ipmi_get_version);
1409 int ipmi_set_my_address(struct ipmi_user *user,
1410 unsigned int channel,
1411 unsigned char address)
1415 user = acquire_ipmi_user(user, &index);
1419 if (channel >= IPMI_MAX_CHANNELS) {
1422 channel = array_index_nospec(channel, IPMI_MAX_CHANNELS);
1423 user->intf->addrinfo[channel].address = address;
1425 release_ipmi_user(user, index);
1429 EXPORT_SYMBOL(ipmi_set_my_address);
1431 int ipmi_get_my_address(struct ipmi_user *user,
1432 unsigned int channel,
1433 unsigned char *address)
1437 user = acquire_ipmi_user(user, &index);
1441 if (channel >= IPMI_MAX_CHANNELS) {
1444 channel = array_index_nospec(channel, IPMI_MAX_CHANNELS);
1445 *address = user->intf->addrinfo[channel].address;
1447 release_ipmi_user(user, index);
1451 EXPORT_SYMBOL(ipmi_get_my_address);
1453 int ipmi_set_my_LUN(struct ipmi_user *user,
1454 unsigned int channel,
1459 user = acquire_ipmi_user(user, &index);
1463 if (channel >= IPMI_MAX_CHANNELS) {
1466 channel = array_index_nospec(channel, IPMI_MAX_CHANNELS);
1467 user->intf->addrinfo[channel].lun = LUN & 0x3;
1469 release_ipmi_user(user, index);
1473 EXPORT_SYMBOL(ipmi_set_my_LUN);
1475 int ipmi_get_my_LUN(struct ipmi_user *user,
1476 unsigned int channel,
1477 unsigned char *address)
1481 user = acquire_ipmi_user(user, &index);
1485 if (channel >= IPMI_MAX_CHANNELS) {
1488 channel = array_index_nospec(channel, IPMI_MAX_CHANNELS);
1489 *address = user->intf->addrinfo[channel].lun;
1491 release_ipmi_user(user, index);
1495 EXPORT_SYMBOL(ipmi_get_my_LUN);
1497 int ipmi_get_maintenance_mode(struct ipmi_user *user)
1500 unsigned long flags;
1502 user = acquire_ipmi_user(user, &index);
1506 spin_lock_irqsave(&user->intf->maintenance_mode_lock, flags);
1507 mode = user->intf->maintenance_mode;
1508 spin_unlock_irqrestore(&user->intf->maintenance_mode_lock, flags);
1509 release_ipmi_user(user, index);
1513 EXPORT_SYMBOL(ipmi_get_maintenance_mode);
1515 static void maintenance_mode_update(struct ipmi_smi *intf)
1517 if (intf->handlers->set_maintenance_mode)
1518 intf->handlers->set_maintenance_mode(
1519 intf->send_info, intf->maintenance_mode_enable);
1522 int ipmi_set_maintenance_mode(struct ipmi_user *user, int mode)
1525 unsigned long flags;
1526 struct ipmi_smi *intf = user->intf;
1528 user = acquire_ipmi_user(user, &index);
1532 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
1533 if (intf->maintenance_mode != mode) {
1535 case IPMI_MAINTENANCE_MODE_AUTO:
1536 intf->maintenance_mode_enable
1537 = (intf->auto_maintenance_timeout > 0);
1540 case IPMI_MAINTENANCE_MODE_OFF:
1541 intf->maintenance_mode_enable = false;
1544 case IPMI_MAINTENANCE_MODE_ON:
1545 intf->maintenance_mode_enable = true;
1552 intf->maintenance_mode = mode;
1554 maintenance_mode_update(intf);
1557 spin_unlock_irqrestore(&intf->maintenance_mode_lock, flags);
1558 release_ipmi_user(user, index);
1562 EXPORT_SYMBOL(ipmi_set_maintenance_mode);
1564 int ipmi_set_gets_events(struct ipmi_user *user, bool val)
1566 unsigned long flags;
1567 struct ipmi_smi *intf = user->intf;
1568 struct ipmi_recv_msg *msg, *msg2;
1569 struct list_head msgs;
1572 user = acquire_ipmi_user(user, &index);
1576 INIT_LIST_HEAD(&msgs);
1578 spin_lock_irqsave(&intf->events_lock, flags);
1579 if (user->gets_events == val)
1582 user->gets_events = val;
1585 if (atomic_inc_return(&intf->event_waiters) == 1)
1588 atomic_dec(&intf->event_waiters);
1591 if (intf->delivering_events)
1593 * Another thread is delivering events for this, so
1594 * let it handle any new events.
1598 /* Deliver any queued events. */
1599 while (user->gets_events && !list_empty(&intf->waiting_events)) {
1600 list_for_each_entry_safe(msg, msg2, &intf->waiting_events, link)
1601 list_move_tail(&msg->link, &msgs);
1602 intf->waiting_events_count = 0;
1603 if (intf->event_msg_printed) {
1604 dev_warn(intf->si_dev, "Event queue no longer full\n");
1605 intf->event_msg_printed = 0;
1608 intf->delivering_events = 1;
1609 spin_unlock_irqrestore(&intf->events_lock, flags);
1611 list_for_each_entry_safe(msg, msg2, &msgs, link) {
1613 kref_get(&user->refcount);
1614 deliver_local_response(intf, msg);
1617 spin_lock_irqsave(&intf->events_lock, flags);
1618 intf->delivering_events = 0;
1622 spin_unlock_irqrestore(&intf->events_lock, flags);
1623 release_ipmi_user(user, index);
1627 EXPORT_SYMBOL(ipmi_set_gets_events);
1629 static struct cmd_rcvr *find_cmd_rcvr(struct ipmi_smi *intf,
1630 unsigned char netfn,
1634 struct cmd_rcvr *rcvr;
1636 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link,
1637 lockdep_is_held(&intf->cmd_rcvrs_mutex)) {
1638 if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)
1639 && (rcvr->chans & (1 << chan)))
1645 static int is_cmd_rcvr_exclusive(struct ipmi_smi *intf,
1646 unsigned char netfn,
1650 struct cmd_rcvr *rcvr;
1652 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link,
1653 lockdep_is_held(&intf->cmd_rcvrs_mutex)) {
1654 if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)
1655 && (rcvr->chans & chans))
1661 int ipmi_register_for_cmd(struct ipmi_user *user,
1662 unsigned char netfn,
1666 struct ipmi_smi *intf = user->intf;
1667 struct cmd_rcvr *rcvr;
1670 user = acquire_ipmi_user(user, &index);
1674 rcvr = kmalloc(sizeof(*rcvr), GFP_KERNEL);
1680 rcvr->netfn = netfn;
1681 rcvr->chans = chans;
1684 mutex_lock(&intf->cmd_rcvrs_mutex);
1685 /* Make sure the command/netfn is not already registered. */
1686 if (!is_cmd_rcvr_exclusive(intf, netfn, cmd, chans)) {
1691 smi_add_watch(intf, IPMI_WATCH_MASK_CHECK_COMMANDS);
1693 list_add_rcu(&rcvr->link, &intf->cmd_rcvrs);
1696 mutex_unlock(&intf->cmd_rcvrs_mutex);
1700 release_ipmi_user(user, index);
1704 EXPORT_SYMBOL(ipmi_register_for_cmd);
1706 int ipmi_unregister_for_cmd(struct ipmi_user *user,
1707 unsigned char netfn,
1711 struct ipmi_smi *intf = user->intf;
1712 struct cmd_rcvr *rcvr;
1713 struct cmd_rcvr *rcvrs = NULL;
1714 int i, rv = -ENOENT, index;
1716 user = acquire_ipmi_user(user, &index);
1720 mutex_lock(&intf->cmd_rcvrs_mutex);
1721 for (i = 0; i < IPMI_NUM_CHANNELS; i++) {
1722 if (((1 << i) & chans) == 0)
1724 rcvr = find_cmd_rcvr(intf, netfn, cmd, i);
1727 if (rcvr->user == user) {
1729 rcvr->chans &= ~chans;
1730 if (rcvr->chans == 0) {
1731 list_del_rcu(&rcvr->link);
1737 mutex_unlock(&intf->cmd_rcvrs_mutex);
1739 release_ipmi_user(user, index);
1741 smi_remove_watch(intf, IPMI_WATCH_MASK_CHECK_COMMANDS);
1749 EXPORT_SYMBOL(ipmi_unregister_for_cmd);
1752 ipmb_checksum(unsigned char *data, int size)
1754 unsigned char csum = 0;
1756 for (; size > 0; size--, data++)
1761 EXPORT_SYMBOL(ipmb_checksum);
1763 static inline void format_ipmb_msg(struct ipmi_smi_msg *smi_msg,
1764 struct kernel_ipmi_msg *msg,
1765 struct ipmi_ipmb_addr *ipmb_addr,
1767 unsigned char ipmb_seq,
1769 unsigned char source_address,
1770 unsigned char source_lun)
1774 /* Format the IPMB header data. */
1775 smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
1776 smi_msg->data[1] = IPMI_SEND_MSG_CMD;
1777 smi_msg->data[2] = ipmb_addr->channel;
1779 smi_msg->data[3] = 0;
1780 smi_msg->data[i+3] = ipmb_addr->slave_addr;
1781 smi_msg->data[i+4] = (msg->netfn << 2) | (ipmb_addr->lun & 0x3);
1782 smi_msg->data[i+5] = ipmb_checksum(&smi_msg->data[i + 3], 2);
1783 smi_msg->data[i+6] = source_address;
1784 smi_msg->data[i+7] = (ipmb_seq << 2) | source_lun;
1785 smi_msg->data[i+8] = msg->cmd;
1787 /* Now tack on the data to the message. */
1788 if (msg->data_len > 0)
1789 memcpy(&smi_msg->data[i + 9], msg->data, msg->data_len);
1790 smi_msg->data_size = msg->data_len + 9;
1792 /* Now calculate the checksum and tack it on. */
1793 smi_msg->data[i+smi_msg->data_size]
1794 = ipmb_checksum(&smi_msg->data[i + 6], smi_msg->data_size - 6);
1797 * Add on the checksum size and the offset from the
1800 smi_msg->data_size += 1 + i;
1802 smi_msg->msgid = msgid;
1805 static inline void format_lan_msg(struct ipmi_smi_msg *smi_msg,
1806 struct kernel_ipmi_msg *msg,
1807 struct ipmi_lan_addr *lan_addr,
1809 unsigned char ipmb_seq,
1810 unsigned char source_lun)
1812 /* Format the IPMB header data. */
1813 smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
1814 smi_msg->data[1] = IPMI_SEND_MSG_CMD;
1815 smi_msg->data[2] = lan_addr->channel;
1816 smi_msg->data[3] = lan_addr->session_handle;
1817 smi_msg->data[4] = lan_addr->remote_SWID;
1818 smi_msg->data[5] = (msg->netfn << 2) | (lan_addr->lun & 0x3);
1819 smi_msg->data[6] = ipmb_checksum(&smi_msg->data[4], 2);
1820 smi_msg->data[7] = lan_addr->local_SWID;
1821 smi_msg->data[8] = (ipmb_seq << 2) | source_lun;
1822 smi_msg->data[9] = msg->cmd;
1824 /* Now tack on the data to the message. */
1825 if (msg->data_len > 0)
1826 memcpy(&smi_msg->data[10], msg->data, msg->data_len);
1827 smi_msg->data_size = msg->data_len + 10;
1829 /* Now calculate the checksum and tack it on. */
1830 smi_msg->data[smi_msg->data_size]
1831 = ipmb_checksum(&smi_msg->data[7], smi_msg->data_size - 7);
1834 * Add on the checksum size and the offset from the
1837 smi_msg->data_size += 1;
1839 smi_msg->msgid = msgid;
1842 static struct ipmi_smi_msg *smi_add_send_msg(struct ipmi_smi *intf,
1843 struct ipmi_smi_msg *smi_msg,
1846 if (intf->curr_msg) {
1848 list_add_tail(&smi_msg->link, &intf->hp_xmit_msgs);
1850 list_add_tail(&smi_msg->link, &intf->xmit_msgs);
1853 intf->curr_msg = smi_msg;
1859 static void smi_send(struct ipmi_smi *intf,
1860 const struct ipmi_smi_handlers *handlers,
1861 struct ipmi_smi_msg *smi_msg, int priority)
1863 int run_to_completion = intf->run_to_completion;
1864 unsigned long flags = 0;
1866 if (!run_to_completion)
1867 spin_lock_irqsave(&intf->xmit_msgs_lock, flags);
1868 smi_msg = smi_add_send_msg(intf, smi_msg, priority);
1870 if (!run_to_completion)
1871 spin_unlock_irqrestore(&intf->xmit_msgs_lock, flags);
1874 handlers->sender(intf->send_info, smi_msg);
1877 static bool is_maintenance_mode_cmd(struct kernel_ipmi_msg *msg)
1879 return (((msg->netfn == IPMI_NETFN_APP_REQUEST)
1880 && ((msg->cmd == IPMI_COLD_RESET_CMD)
1881 || (msg->cmd == IPMI_WARM_RESET_CMD)))
1882 || (msg->netfn == IPMI_NETFN_FIRMWARE_REQUEST));
1885 static int i_ipmi_req_sysintf(struct ipmi_smi *intf,
1886 struct ipmi_addr *addr,
1888 struct kernel_ipmi_msg *msg,
1889 struct ipmi_smi_msg *smi_msg,
1890 struct ipmi_recv_msg *recv_msg,
1892 unsigned int retry_time_ms)
1894 struct ipmi_system_interface_addr *smi_addr;
1897 /* Responses are not allowed to the SMI. */
1900 smi_addr = (struct ipmi_system_interface_addr *) addr;
1901 if (smi_addr->lun > 3) {
1902 ipmi_inc_stat(intf, sent_invalid_commands);
1906 memcpy(&recv_msg->addr, smi_addr, sizeof(*smi_addr));
1908 if ((msg->netfn == IPMI_NETFN_APP_REQUEST)
1909 && ((msg->cmd == IPMI_SEND_MSG_CMD)
1910 || (msg->cmd == IPMI_GET_MSG_CMD)
1911 || (msg->cmd == IPMI_READ_EVENT_MSG_BUFFER_CMD))) {
1913 * We don't let the user do these, since we manage
1914 * the sequence numbers.
1916 ipmi_inc_stat(intf, sent_invalid_commands);
1920 if (is_maintenance_mode_cmd(msg)) {
1921 unsigned long flags;
1923 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
1924 intf->auto_maintenance_timeout
1925 = maintenance_mode_timeout_ms;
1926 if (!intf->maintenance_mode
1927 && !intf->maintenance_mode_enable) {
1928 intf->maintenance_mode_enable = true;
1929 maintenance_mode_update(intf);
1931 spin_unlock_irqrestore(&intf->maintenance_mode_lock,
1935 if (msg->data_len + 2 > IPMI_MAX_MSG_LENGTH) {
1936 ipmi_inc_stat(intf, sent_invalid_commands);
1940 smi_msg->data[0] = (msg->netfn << 2) | (smi_addr->lun & 0x3);
1941 smi_msg->data[1] = msg->cmd;
1942 smi_msg->msgid = msgid;
1943 smi_msg->user_data = recv_msg;
1944 if (msg->data_len > 0)
1945 memcpy(&smi_msg->data[2], msg->data, msg->data_len);
1946 smi_msg->data_size = msg->data_len + 2;
1947 ipmi_inc_stat(intf, sent_local_commands);
1952 static int i_ipmi_req_ipmb(struct ipmi_smi *intf,
1953 struct ipmi_addr *addr,
1955 struct kernel_ipmi_msg *msg,
1956 struct ipmi_smi_msg *smi_msg,
1957 struct ipmi_recv_msg *recv_msg,
1958 unsigned char source_address,
1959 unsigned char source_lun,
1961 unsigned int retry_time_ms)
1963 struct ipmi_ipmb_addr *ipmb_addr;
1964 unsigned char ipmb_seq;
1967 struct ipmi_channel *chans;
1970 if (addr->channel >= IPMI_MAX_CHANNELS) {
1971 ipmi_inc_stat(intf, sent_invalid_commands);
1975 chans = READ_ONCE(intf->channel_list)->c;
1977 if (chans[addr->channel].medium != IPMI_CHANNEL_MEDIUM_IPMB) {
1978 ipmi_inc_stat(intf, sent_invalid_commands);
1982 if (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE) {
1984 * Broadcasts add a zero at the beginning of the
1985 * message, but otherwise is the same as an IPMB
1988 addr->addr_type = IPMI_IPMB_ADDR_TYPE;
1990 retries = 0; /* Don't retry broadcasts. */
1994 * 9 for the header and 1 for the checksum, plus
1995 * possibly one for the broadcast.
1997 if ((msg->data_len + 10 + broadcast) > IPMI_MAX_MSG_LENGTH) {
1998 ipmi_inc_stat(intf, sent_invalid_commands);
2002 ipmb_addr = (struct ipmi_ipmb_addr *) addr;
2003 if (ipmb_addr->lun > 3) {
2004 ipmi_inc_stat(intf, sent_invalid_commands);
2008 memcpy(&recv_msg->addr, ipmb_addr, sizeof(*ipmb_addr));
2010 if (recv_msg->msg.netfn & 0x1) {
2012 * It's a response, so use the user's sequence
2015 ipmi_inc_stat(intf, sent_ipmb_responses);
2016 format_ipmb_msg(smi_msg, msg, ipmb_addr, msgid,
2018 source_address, source_lun);
2021 * Save the receive message so we can use it
2022 * to deliver the response.
2024 smi_msg->user_data = recv_msg;
2026 /* It's a command, so get a sequence for it. */
2027 unsigned long flags;
2029 spin_lock_irqsave(&intf->seq_lock, flags);
2031 if (is_maintenance_mode_cmd(msg))
2032 intf->ipmb_maintenance_mode_timeout =
2033 maintenance_mode_timeout_ms;
2035 if (intf->ipmb_maintenance_mode_timeout && retry_time_ms == 0)
2036 /* Different default in maintenance mode */
2037 retry_time_ms = default_maintenance_retry_ms;
2040 * Create a sequence number with a 1 second
2041 * timeout and 4 retries.
2043 rv = intf_next_seq(intf,
2052 * We have used up all the sequence numbers,
2053 * probably, so abort.
2057 ipmi_inc_stat(intf, sent_ipmb_commands);
2060 * Store the sequence number in the message,
2061 * so that when the send message response
2062 * comes back we can start the timer.
2064 format_ipmb_msg(smi_msg, msg, ipmb_addr,
2065 STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
2066 ipmb_seq, broadcast,
2067 source_address, source_lun);
2070 * Copy the message into the recv message data, so we
2071 * can retransmit it later if necessary.
2073 memcpy(recv_msg->msg_data, smi_msg->data,
2074 smi_msg->data_size);
2075 recv_msg->msg.data = recv_msg->msg_data;
2076 recv_msg->msg.data_len = smi_msg->data_size;
2079 * We don't unlock until here, because we need
2080 * to copy the completed message into the
2081 * recv_msg before we release the lock.
2082 * Otherwise, race conditions may bite us. I
2083 * know that's pretty paranoid, but I prefer
2087 spin_unlock_irqrestore(&intf->seq_lock, flags);
2093 static int i_ipmi_req_ipmb_direct(struct ipmi_smi *intf,
2094 struct ipmi_addr *addr,
2096 struct kernel_ipmi_msg *msg,
2097 struct ipmi_smi_msg *smi_msg,
2098 struct ipmi_recv_msg *recv_msg,
2099 unsigned char source_lun)
2101 struct ipmi_ipmb_direct_addr *daddr;
2102 bool is_cmd = !(recv_msg->msg.netfn & 0x1);
2104 if (!(intf->handlers->flags & IPMI_SMI_CAN_HANDLE_IPMB_DIRECT))
2105 return -EAFNOSUPPORT;
2107 /* Responses must have a completion code. */
2108 if (!is_cmd && msg->data_len < 1) {
2109 ipmi_inc_stat(intf, sent_invalid_commands);
2113 if ((msg->data_len + 4) > IPMI_MAX_MSG_LENGTH) {
2114 ipmi_inc_stat(intf, sent_invalid_commands);
2118 daddr = (struct ipmi_ipmb_direct_addr *) addr;
2119 if (daddr->rq_lun > 3 || daddr->rs_lun > 3) {
2120 ipmi_inc_stat(intf, sent_invalid_commands);
2124 smi_msg->type = IPMI_SMI_MSG_TYPE_IPMB_DIRECT;
2125 smi_msg->msgid = msgid;
2128 smi_msg->data[0] = msg->netfn << 2 | daddr->rs_lun;
2129 smi_msg->data[2] = recv_msg->msgid << 2 | daddr->rq_lun;
2131 smi_msg->data[0] = msg->netfn << 2 | daddr->rq_lun;
2132 smi_msg->data[2] = recv_msg->msgid << 2 | daddr->rs_lun;
2134 smi_msg->data[1] = daddr->slave_addr;
2135 smi_msg->data[3] = msg->cmd;
2137 memcpy(smi_msg->data + 4, msg->data, msg->data_len);
2138 smi_msg->data_size = msg->data_len + 4;
2140 smi_msg->user_data = recv_msg;
2145 static int i_ipmi_req_lan(struct ipmi_smi *intf,
2146 struct ipmi_addr *addr,
2148 struct kernel_ipmi_msg *msg,
2149 struct ipmi_smi_msg *smi_msg,
2150 struct ipmi_recv_msg *recv_msg,
2151 unsigned char source_lun,
2153 unsigned int retry_time_ms)
2155 struct ipmi_lan_addr *lan_addr;
2156 unsigned char ipmb_seq;
2158 struct ipmi_channel *chans;
2161 if (addr->channel >= IPMI_MAX_CHANNELS) {
2162 ipmi_inc_stat(intf, sent_invalid_commands);
2166 chans = READ_ONCE(intf->channel_list)->c;
2168 if ((chans[addr->channel].medium
2169 != IPMI_CHANNEL_MEDIUM_8023LAN)
2170 && (chans[addr->channel].medium
2171 != IPMI_CHANNEL_MEDIUM_ASYNC)) {
2172 ipmi_inc_stat(intf, sent_invalid_commands);
2176 /* 11 for the header and 1 for the checksum. */
2177 if ((msg->data_len + 12) > IPMI_MAX_MSG_LENGTH) {
2178 ipmi_inc_stat(intf, sent_invalid_commands);
2182 lan_addr = (struct ipmi_lan_addr *) addr;
2183 if (lan_addr->lun > 3) {
2184 ipmi_inc_stat(intf, sent_invalid_commands);
2188 memcpy(&recv_msg->addr, lan_addr, sizeof(*lan_addr));
2190 if (recv_msg->msg.netfn & 0x1) {
2192 * It's a response, so use the user's sequence
2195 ipmi_inc_stat(intf, sent_lan_responses);
2196 format_lan_msg(smi_msg, msg, lan_addr, msgid,
2200 * Save the receive message so we can use it
2201 * to deliver the response.
2203 smi_msg->user_data = recv_msg;
2205 /* It's a command, so get a sequence for it. */
2206 unsigned long flags;
2208 spin_lock_irqsave(&intf->seq_lock, flags);
2211 * Create a sequence number with a 1 second
2212 * timeout and 4 retries.
2214 rv = intf_next_seq(intf,
2223 * We have used up all the sequence numbers,
2224 * probably, so abort.
2228 ipmi_inc_stat(intf, sent_lan_commands);
2231 * Store the sequence number in the message,
2232 * so that when the send message response
2233 * comes back we can start the timer.
2235 format_lan_msg(smi_msg, msg, lan_addr,
2236 STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
2237 ipmb_seq, source_lun);
2240 * Copy the message into the recv message data, so we
2241 * can retransmit it later if necessary.
2243 memcpy(recv_msg->msg_data, smi_msg->data,
2244 smi_msg->data_size);
2245 recv_msg->msg.data = recv_msg->msg_data;
2246 recv_msg->msg.data_len = smi_msg->data_size;
2249 * We don't unlock until here, because we need
2250 * to copy the completed message into the
2251 * recv_msg before we release the lock.
2252 * Otherwise, race conditions may bite us. I
2253 * know that's pretty paranoid, but I prefer
2257 spin_unlock_irqrestore(&intf->seq_lock, flags);
2264 * Separate from ipmi_request so that the user does not have to be
2265 * supplied in certain circumstances (mainly at panic time). If
2266 * messages are supplied, they will be freed, even if an error
2269 static int i_ipmi_request(struct ipmi_user *user,
2270 struct ipmi_smi *intf,
2271 struct ipmi_addr *addr,
2273 struct kernel_ipmi_msg *msg,
2274 void *user_msg_data,
2276 struct ipmi_recv_msg *supplied_recv,
2278 unsigned char source_address,
2279 unsigned char source_lun,
2281 unsigned int retry_time_ms)
2283 struct ipmi_smi_msg *smi_msg;
2284 struct ipmi_recv_msg *recv_msg;
2288 recv_msg = supplied_recv;
2290 recv_msg = ipmi_alloc_recv_msg();
2291 if (recv_msg == NULL) {
2296 recv_msg->user_msg_data = user_msg_data;
2299 smi_msg = (struct ipmi_smi_msg *) supplied_smi;
2301 smi_msg = ipmi_alloc_smi_msg();
2302 if (smi_msg == NULL) {
2304 ipmi_free_recv_msg(recv_msg);
2311 if (intf->in_shutdown) {
2316 recv_msg->user = user;
2318 /* The put happens when the message is freed. */
2319 kref_get(&user->refcount);
2320 recv_msg->msgid = msgid;
2322 * Store the message to send in the receive message so timeout
2323 * responses can get the proper response data.
2325 recv_msg->msg = *msg;
2327 if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
2328 rv = i_ipmi_req_sysintf(intf, addr, msgid, msg, smi_msg,
2329 recv_msg, retries, retry_time_ms);
2330 } else if (is_ipmb_addr(addr) || is_ipmb_bcast_addr(addr)) {
2331 rv = i_ipmi_req_ipmb(intf, addr, msgid, msg, smi_msg, recv_msg,
2332 source_address, source_lun,
2333 retries, retry_time_ms);
2334 } else if (is_ipmb_direct_addr(addr)) {
2335 rv = i_ipmi_req_ipmb_direct(intf, addr, msgid, msg, smi_msg,
2336 recv_msg, source_lun);
2337 } else if (is_lan_addr(addr)) {
2338 rv = i_ipmi_req_lan(intf, addr, msgid, msg, smi_msg, recv_msg,
2339 source_lun, retries, retry_time_ms);
2341 /* Unknown address type. */
2342 ipmi_inc_stat(intf, sent_invalid_commands);
2348 ipmi_free_smi_msg(smi_msg);
2349 ipmi_free_recv_msg(recv_msg);
2351 pr_debug("Send: %*ph\n", smi_msg->data_size, smi_msg->data);
2353 smi_send(intf, intf->handlers, smi_msg, priority);
2361 static int check_addr(struct ipmi_smi *intf,
2362 struct ipmi_addr *addr,
2363 unsigned char *saddr,
2366 if (addr->channel >= IPMI_MAX_CHANNELS)
2368 addr->channel = array_index_nospec(addr->channel, IPMI_MAX_CHANNELS);
2369 *lun = intf->addrinfo[addr->channel].lun;
2370 *saddr = intf->addrinfo[addr->channel].address;
2374 int ipmi_request_settime(struct ipmi_user *user,
2375 struct ipmi_addr *addr,
2377 struct kernel_ipmi_msg *msg,
2378 void *user_msg_data,
2381 unsigned int retry_time_ms)
2383 unsigned char saddr = 0, lun = 0;
2389 user = acquire_ipmi_user(user, &index);
2393 rv = check_addr(user->intf, addr, &saddr, &lun);
2395 rv = i_ipmi_request(user,
2408 release_ipmi_user(user, index);
2411 EXPORT_SYMBOL(ipmi_request_settime);
2413 int ipmi_request_supply_msgs(struct ipmi_user *user,
2414 struct ipmi_addr *addr,
2416 struct kernel_ipmi_msg *msg,
2417 void *user_msg_data,
2419 struct ipmi_recv_msg *supplied_recv,
2422 unsigned char saddr = 0, lun = 0;
2428 user = acquire_ipmi_user(user, &index);
2432 rv = check_addr(user->intf, addr, &saddr, &lun);
2434 rv = i_ipmi_request(user,
2447 release_ipmi_user(user, index);
2450 EXPORT_SYMBOL(ipmi_request_supply_msgs);
2452 static void bmc_device_id_handler(struct ipmi_smi *intf,
2453 struct ipmi_recv_msg *msg)
2457 if ((msg->addr.addr_type != IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
2458 || (msg->msg.netfn != IPMI_NETFN_APP_RESPONSE)
2459 || (msg->msg.cmd != IPMI_GET_DEVICE_ID_CMD)) {
2460 dev_warn(intf->si_dev,
2461 "invalid device_id msg: addr_type=%d netfn=%x cmd=%x\n",
2462 msg->addr.addr_type, msg->msg.netfn, msg->msg.cmd);
2466 if (msg->msg.data[0]) {
2467 dev_warn(intf->si_dev, "device id fetch failed: 0x%2.2x\n",
2469 intf->bmc->dyn_id_set = 0;
2473 rv = ipmi_demangle_device_id(msg->msg.netfn, msg->msg.cmd,
2474 msg->msg.data, msg->msg.data_len, &intf->bmc->fetch_id);
2476 dev_warn(intf->si_dev, "device id demangle failed: %d\n", rv);
2477 /* record completion code when error */
2478 intf->bmc->cc = msg->msg.data[0];
2479 intf->bmc->dyn_id_set = 0;
2482 * Make sure the id data is available before setting
2486 intf->bmc->dyn_id_set = 1;
2489 wake_up(&intf->waitq);
2493 send_get_device_id_cmd(struct ipmi_smi *intf)
2495 struct ipmi_system_interface_addr si;
2496 struct kernel_ipmi_msg msg;
2498 si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2499 si.channel = IPMI_BMC_CHANNEL;
2502 msg.netfn = IPMI_NETFN_APP_REQUEST;
2503 msg.cmd = IPMI_GET_DEVICE_ID_CMD;
2507 return i_ipmi_request(NULL,
2509 (struct ipmi_addr *) &si,
2516 intf->addrinfo[0].address,
2517 intf->addrinfo[0].lun,
2521 static int __get_device_id(struct ipmi_smi *intf, struct bmc_device *bmc)
2524 unsigned int retry_count = 0;
2526 intf->null_user_handler = bmc_device_id_handler;
2530 bmc->dyn_id_set = 2;
2532 rv = send_get_device_id_cmd(intf);
2534 goto out_reset_handler;
2536 wait_event(intf->waitq, bmc->dyn_id_set != 2);
2538 if (!bmc->dyn_id_set) {
2539 if (bmc->cc != IPMI_CC_NO_ERROR &&
2540 ++retry_count <= GET_DEVICE_ID_MAX_RETRY) {
2542 dev_warn(intf->si_dev,
2543 "BMC returned 0x%2.2x, retry get bmc device id\n",
2548 rv = -EIO; /* Something went wrong in the fetch. */
2551 /* dyn_id_set makes the id data available. */
2555 intf->null_user_handler = NULL;
2561 * Fetch the device id for the bmc/interface. You must pass in either
2562 * bmc or intf, this code will get the other one. If the data has
2563 * been recently fetched, this will just use the cached data. Otherwise
2564 * it will run a new fetch.
2566 * Except for the first time this is called (in ipmi_add_smi()),
2567 * this will always return good data;
2569 static int __bmc_get_device_id(struct ipmi_smi *intf, struct bmc_device *bmc,
2570 struct ipmi_device_id *id,
2571 bool *guid_set, guid_t *guid, int intf_num)
2574 int prev_dyn_id_set, prev_guid_set;
2575 bool intf_set = intf != NULL;
2578 mutex_lock(&bmc->dyn_mutex);
2580 if (list_empty(&bmc->intfs)) {
2581 mutex_unlock(&bmc->dyn_mutex);
2584 intf = list_first_entry(&bmc->intfs, struct ipmi_smi,
2586 kref_get(&intf->refcount);
2587 mutex_unlock(&bmc->dyn_mutex);
2588 mutex_lock(&intf->bmc_reg_mutex);
2589 mutex_lock(&bmc->dyn_mutex);
2590 if (intf != list_first_entry(&bmc->intfs, struct ipmi_smi,
2592 mutex_unlock(&intf->bmc_reg_mutex);
2593 kref_put(&intf->refcount, intf_free);
2594 goto retry_bmc_lock;
2597 mutex_lock(&intf->bmc_reg_mutex);
2599 mutex_lock(&bmc->dyn_mutex);
2600 kref_get(&intf->refcount);
2603 /* If we have a valid and current ID, just return that. */
2604 if (intf->in_bmc_register ||
2605 (bmc->dyn_id_set && time_is_after_jiffies(bmc->dyn_id_expiry)))
2606 goto out_noprocessing;
2608 prev_guid_set = bmc->dyn_guid_set;
2611 prev_dyn_id_set = bmc->dyn_id_set;
2612 rv = __get_device_id(intf, bmc);
2617 * The guid, device id, manufacturer id, and product id should
2618 * not change on a BMC. If it does we have to do some dancing.
2620 if (!intf->bmc_registered
2621 || (!prev_guid_set && bmc->dyn_guid_set)
2622 || (!prev_dyn_id_set && bmc->dyn_id_set)
2623 || (prev_guid_set && bmc->dyn_guid_set
2624 && !guid_equal(&bmc->guid, &bmc->fetch_guid))
2625 || bmc->id.device_id != bmc->fetch_id.device_id
2626 || bmc->id.manufacturer_id != bmc->fetch_id.manufacturer_id
2627 || bmc->id.product_id != bmc->fetch_id.product_id) {
2628 struct ipmi_device_id id = bmc->fetch_id;
2629 int guid_set = bmc->dyn_guid_set;
2632 guid = bmc->fetch_guid;
2633 mutex_unlock(&bmc->dyn_mutex);
2635 __ipmi_bmc_unregister(intf);
2636 /* Fill in the temporary BMC for good measure. */
2638 intf->bmc->dyn_guid_set = guid_set;
2639 intf->bmc->guid = guid;
2640 if (__ipmi_bmc_register(intf, &id, guid_set, &guid, intf_num))
2641 need_waiter(intf); /* Retry later on an error. */
2643 __scan_channels(intf, &id);
2648 * We weren't given the interface on the
2649 * command line, so restart the operation on
2650 * the next interface for the BMC.
2652 mutex_unlock(&intf->bmc_reg_mutex);
2653 mutex_lock(&bmc->dyn_mutex);
2654 goto retry_bmc_lock;
2657 /* We have a new BMC, set it up. */
2659 mutex_lock(&bmc->dyn_mutex);
2660 goto out_noprocessing;
2661 } else if (memcmp(&bmc->fetch_id, &bmc->id, sizeof(bmc->id)))
2662 /* Version info changes, scan the channels again. */
2663 __scan_channels(intf, &bmc->fetch_id);
2665 bmc->dyn_id_expiry = jiffies + IPMI_DYN_DEV_ID_EXPIRY;
2668 if (rv && prev_dyn_id_set) {
2669 rv = 0; /* Ignore failures if we have previous data. */
2670 bmc->dyn_id_set = prev_dyn_id_set;
2673 bmc->id = bmc->fetch_id;
2674 if (bmc->dyn_guid_set)
2675 bmc->guid = bmc->fetch_guid;
2676 else if (prev_guid_set)
2678 * The guid used to be valid and it failed to fetch,
2679 * just use the cached value.
2681 bmc->dyn_guid_set = prev_guid_set;
2689 *guid_set = bmc->dyn_guid_set;
2691 if (guid && bmc->dyn_guid_set)
2695 mutex_unlock(&bmc->dyn_mutex);
2696 mutex_unlock(&intf->bmc_reg_mutex);
2698 kref_put(&intf->refcount, intf_free);
2702 static int bmc_get_device_id(struct ipmi_smi *intf, struct bmc_device *bmc,
2703 struct ipmi_device_id *id,
2704 bool *guid_set, guid_t *guid)
2706 return __bmc_get_device_id(intf, bmc, id, guid_set, guid, -1);
2709 static ssize_t device_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 sysfs_emit(buf, "%u\n", id.device_id);
2723 static DEVICE_ATTR_RO(device_id);
2725 static ssize_t provides_device_sdrs_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 sysfs_emit(buf, "%u\n", (id.device_revision & 0x80) >> 7);
2739 static DEVICE_ATTR_RO(provides_device_sdrs);
2741 static ssize_t revision_show(struct device *dev, struct device_attribute *attr,
2744 struct bmc_device *bmc = to_bmc_device(dev);
2745 struct ipmi_device_id id;
2748 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2752 return sysfs_emit(buf, "%u\n", id.device_revision & 0x0F);
2754 static DEVICE_ATTR_RO(revision);
2756 static ssize_t firmware_revision_show(struct device *dev,
2757 struct device_attribute *attr,
2760 struct bmc_device *bmc = to_bmc_device(dev);
2761 struct ipmi_device_id id;
2764 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2768 return sysfs_emit(buf, "%u.%x\n", id.firmware_revision_1,
2769 id.firmware_revision_2);
2771 static DEVICE_ATTR_RO(firmware_revision);
2773 static ssize_t ipmi_version_show(struct device *dev,
2774 struct device_attribute *attr,
2777 struct bmc_device *bmc = to_bmc_device(dev);
2778 struct ipmi_device_id id;
2781 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2785 return sysfs_emit(buf, "%u.%u\n",
2786 ipmi_version_major(&id),
2787 ipmi_version_minor(&id));
2789 static DEVICE_ATTR_RO(ipmi_version);
2791 static ssize_t add_dev_support_show(struct device *dev,
2792 struct device_attribute *attr,
2795 struct bmc_device *bmc = to_bmc_device(dev);
2796 struct ipmi_device_id id;
2799 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2803 return sysfs_emit(buf, "0x%02x\n", id.additional_device_support);
2805 static DEVICE_ATTR(additional_device_support, S_IRUGO, add_dev_support_show,
2808 static ssize_t manufacturer_id_show(struct device *dev,
2809 struct device_attribute *attr,
2812 struct bmc_device *bmc = to_bmc_device(dev);
2813 struct ipmi_device_id id;
2816 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2820 return sysfs_emit(buf, "0x%6.6x\n", id.manufacturer_id);
2822 static DEVICE_ATTR_RO(manufacturer_id);
2824 static ssize_t product_id_show(struct device *dev,
2825 struct device_attribute *attr,
2828 struct bmc_device *bmc = to_bmc_device(dev);
2829 struct ipmi_device_id id;
2832 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2836 return sysfs_emit(buf, "0x%4.4x\n", id.product_id);
2838 static DEVICE_ATTR_RO(product_id);
2840 static ssize_t aux_firmware_rev_show(struct device *dev,
2841 struct device_attribute *attr,
2844 struct bmc_device *bmc = to_bmc_device(dev);
2845 struct ipmi_device_id id;
2848 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2852 return sysfs_emit(buf, "0x%02x 0x%02x 0x%02x 0x%02x\n",
2853 id.aux_firmware_revision[3],
2854 id.aux_firmware_revision[2],
2855 id.aux_firmware_revision[1],
2856 id.aux_firmware_revision[0]);
2858 static DEVICE_ATTR(aux_firmware_revision, S_IRUGO, aux_firmware_rev_show, NULL);
2860 static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
2863 struct bmc_device *bmc = to_bmc_device(dev);
2868 rv = bmc_get_device_id(NULL, bmc, NULL, &guid_set, &guid);
2874 return sysfs_emit(buf, "%pUl\n", &guid);
2876 static DEVICE_ATTR_RO(guid);
2878 static struct attribute *bmc_dev_attrs[] = {
2879 &dev_attr_device_id.attr,
2880 &dev_attr_provides_device_sdrs.attr,
2881 &dev_attr_revision.attr,
2882 &dev_attr_firmware_revision.attr,
2883 &dev_attr_ipmi_version.attr,
2884 &dev_attr_additional_device_support.attr,
2885 &dev_attr_manufacturer_id.attr,
2886 &dev_attr_product_id.attr,
2887 &dev_attr_aux_firmware_revision.attr,
2888 &dev_attr_guid.attr,
2892 static umode_t bmc_dev_attr_is_visible(struct kobject *kobj,
2893 struct attribute *attr, int idx)
2895 struct device *dev = kobj_to_dev(kobj);
2896 struct bmc_device *bmc = to_bmc_device(dev);
2897 umode_t mode = attr->mode;
2900 if (attr == &dev_attr_aux_firmware_revision.attr) {
2901 struct ipmi_device_id id;
2903 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2904 return (!rv && id.aux_firmware_revision_set) ? mode : 0;
2906 if (attr == &dev_attr_guid.attr) {
2909 rv = bmc_get_device_id(NULL, bmc, NULL, &guid_set, NULL);
2910 return (!rv && guid_set) ? mode : 0;
2915 static const struct attribute_group bmc_dev_attr_group = {
2916 .attrs = bmc_dev_attrs,
2917 .is_visible = bmc_dev_attr_is_visible,
2920 static const struct attribute_group *bmc_dev_attr_groups[] = {
2921 &bmc_dev_attr_group,
2925 static const struct device_type bmc_device_type = {
2926 .groups = bmc_dev_attr_groups,
2929 static int __find_bmc_guid(struct device *dev, const void *data)
2931 const guid_t *guid = data;
2932 struct bmc_device *bmc;
2935 if (dev->type != &bmc_device_type)
2938 bmc = to_bmc_device(dev);
2939 rv = bmc->dyn_guid_set && guid_equal(&bmc->guid, guid);
2941 rv = kref_get_unless_zero(&bmc->usecount);
2946 * Returns with the bmc's usecount incremented, if it is non-NULL.
2948 static struct bmc_device *ipmi_find_bmc_guid(struct device_driver *drv,
2952 struct bmc_device *bmc = NULL;
2954 dev = driver_find_device(drv, NULL, guid, __find_bmc_guid);
2956 bmc = to_bmc_device(dev);
2962 struct prod_dev_id {
2963 unsigned int product_id;
2964 unsigned char device_id;
2967 static int __find_bmc_prod_dev_id(struct device *dev, const void *data)
2969 const struct prod_dev_id *cid = data;
2970 struct bmc_device *bmc;
2973 if (dev->type != &bmc_device_type)
2976 bmc = to_bmc_device(dev);
2977 rv = (bmc->id.product_id == cid->product_id
2978 && bmc->id.device_id == cid->device_id);
2980 rv = kref_get_unless_zero(&bmc->usecount);
2985 * Returns with the bmc's usecount incremented, if it is non-NULL.
2987 static struct bmc_device *ipmi_find_bmc_prod_dev_id(
2988 struct device_driver *drv,
2989 unsigned int product_id, unsigned char device_id)
2991 struct prod_dev_id id = {
2992 .product_id = product_id,
2993 .device_id = device_id,
2996 struct bmc_device *bmc = NULL;
2998 dev = driver_find_device(drv, NULL, &id, __find_bmc_prod_dev_id);
3000 bmc = to_bmc_device(dev);
3006 static DEFINE_IDA(ipmi_bmc_ida);
3009 release_bmc_device(struct device *dev)
3011 kfree(to_bmc_device(dev));
3014 static void cleanup_bmc_work(struct work_struct *work)
3016 struct bmc_device *bmc = container_of(work, struct bmc_device,
3018 int id = bmc->pdev.id; /* Unregister overwrites id */
3020 platform_device_unregister(&bmc->pdev);
3021 ida_simple_remove(&ipmi_bmc_ida, id);
3025 cleanup_bmc_device(struct kref *ref)
3027 struct bmc_device *bmc = container_of(ref, struct bmc_device, usecount);
3030 * Remove the platform device in a work queue to avoid issues
3031 * with removing the device attributes while reading a device
3034 queue_work(remove_work_wq, &bmc->remove_work);
3038 * Must be called with intf->bmc_reg_mutex held.
3040 static void __ipmi_bmc_unregister(struct ipmi_smi *intf)
3042 struct bmc_device *bmc = intf->bmc;
3044 if (!intf->bmc_registered)
3047 sysfs_remove_link(&intf->si_dev->kobj, "bmc");
3048 sysfs_remove_link(&bmc->pdev.dev.kobj, intf->my_dev_name);
3049 kfree(intf->my_dev_name);
3050 intf->my_dev_name = NULL;
3052 mutex_lock(&bmc->dyn_mutex);
3053 list_del(&intf->bmc_link);
3054 mutex_unlock(&bmc->dyn_mutex);
3055 intf->bmc = &intf->tmp_bmc;
3056 kref_put(&bmc->usecount, cleanup_bmc_device);
3057 intf->bmc_registered = false;
3060 static void ipmi_bmc_unregister(struct ipmi_smi *intf)
3062 mutex_lock(&intf->bmc_reg_mutex);
3063 __ipmi_bmc_unregister(intf);
3064 mutex_unlock(&intf->bmc_reg_mutex);
3068 * Must be called with intf->bmc_reg_mutex held.
3070 static int __ipmi_bmc_register(struct ipmi_smi *intf,
3071 struct ipmi_device_id *id,
3072 bool guid_set, guid_t *guid, int intf_num)
3075 struct bmc_device *bmc;
3076 struct bmc_device *old_bmc;
3079 * platform_device_register() can cause bmc_reg_mutex to
3080 * be claimed because of the is_visible functions of
3081 * the attributes. Eliminate possible recursion and
3084 intf->in_bmc_register = true;
3085 mutex_unlock(&intf->bmc_reg_mutex);
3088 * Try to find if there is an bmc_device struct
3089 * representing the interfaced BMC already
3091 mutex_lock(&ipmidriver_mutex);
3093 old_bmc = ipmi_find_bmc_guid(&ipmidriver.driver, guid);
3095 old_bmc = ipmi_find_bmc_prod_dev_id(&ipmidriver.driver,
3100 * If there is already an bmc_device, free the new one,
3101 * otherwise register the new BMC device
3106 * Note: old_bmc already has usecount incremented by
3107 * the BMC find functions.
3109 intf->bmc = old_bmc;
3110 mutex_lock(&bmc->dyn_mutex);
3111 list_add_tail(&intf->bmc_link, &bmc->intfs);
3112 mutex_unlock(&bmc->dyn_mutex);
3114 dev_info(intf->si_dev,
3115 "interfacing existing BMC (man_id: 0x%6.6x, prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
3116 bmc->id.manufacturer_id,
3120 bmc = kzalloc(sizeof(*bmc), GFP_KERNEL);
3125 INIT_LIST_HEAD(&bmc->intfs);
3126 mutex_init(&bmc->dyn_mutex);
3127 INIT_WORK(&bmc->remove_work, cleanup_bmc_work);
3130 bmc->dyn_id_set = 1;
3131 bmc->dyn_guid_set = guid_set;
3133 bmc->dyn_id_expiry = jiffies + IPMI_DYN_DEV_ID_EXPIRY;
3135 bmc->pdev.name = "ipmi_bmc";
3137 rv = ida_simple_get(&ipmi_bmc_ida, 0, 0, GFP_KERNEL);
3143 bmc->pdev.dev.driver = &ipmidriver.driver;
3145 bmc->pdev.dev.release = release_bmc_device;
3146 bmc->pdev.dev.type = &bmc_device_type;
3147 kref_init(&bmc->usecount);
3150 mutex_lock(&bmc->dyn_mutex);
3151 list_add_tail(&intf->bmc_link, &bmc->intfs);
3152 mutex_unlock(&bmc->dyn_mutex);
3154 rv = platform_device_register(&bmc->pdev);
3156 dev_err(intf->si_dev,
3157 "Unable to register bmc device: %d\n",
3162 dev_info(intf->si_dev,
3163 "Found new BMC (man_id: 0x%6.6x, prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
3164 bmc->id.manufacturer_id,
3170 * create symlink from system interface device to bmc device
3173 rv = sysfs_create_link(&intf->si_dev->kobj, &bmc->pdev.dev.kobj, "bmc");
3175 dev_err(intf->si_dev, "Unable to create bmc symlink: %d\n", rv);
3180 intf_num = intf->intf_num;
3181 intf->my_dev_name = kasprintf(GFP_KERNEL, "ipmi%d", intf_num);
3182 if (!intf->my_dev_name) {
3184 dev_err(intf->si_dev, "Unable to allocate link from BMC: %d\n",
3189 rv = sysfs_create_link(&bmc->pdev.dev.kobj, &intf->si_dev->kobj,
3192 dev_err(intf->si_dev, "Unable to create symlink to bmc: %d\n",
3194 goto out_free_my_dev_name;
3197 intf->bmc_registered = true;
3200 mutex_unlock(&ipmidriver_mutex);
3201 mutex_lock(&intf->bmc_reg_mutex);
3202 intf->in_bmc_register = false;
3206 out_free_my_dev_name:
3207 kfree(intf->my_dev_name);
3208 intf->my_dev_name = NULL;
3211 sysfs_remove_link(&intf->si_dev->kobj, "bmc");
3214 mutex_lock(&bmc->dyn_mutex);
3215 list_del(&intf->bmc_link);
3216 mutex_unlock(&bmc->dyn_mutex);
3217 intf->bmc = &intf->tmp_bmc;
3218 kref_put(&bmc->usecount, cleanup_bmc_device);
3222 mutex_lock(&bmc->dyn_mutex);
3223 list_del(&intf->bmc_link);
3224 mutex_unlock(&bmc->dyn_mutex);
3225 intf->bmc = &intf->tmp_bmc;
3226 put_device(&bmc->pdev.dev);
3231 send_guid_cmd(struct ipmi_smi *intf, int chan)
3233 struct kernel_ipmi_msg msg;
3234 struct ipmi_system_interface_addr si;
3236 si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3237 si.channel = IPMI_BMC_CHANNEL;
3240 msg.netfn = IPMI_NETFN_APP_REQUEST;
3241 msg.cmd = IPMI_GET_DEVICE_GUID_CMD;
3244 return i_ipmi_request(NULL,
3246 (struct ipmi_addr *) &si,
3253 intf->addrinfo[0].address,
3254 intf->addrinfo[0].lun,
3258 static void guid_handler(struct ipmi_smi *intf, struct ipmi_recv_msg *msg)
3260 struct bmc_device *bmc = intf->bmc;
3262 if ((msg->addr.addr_type != IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
3263 || (msg->msg.netfn != IPMI_NETFN_APP_RESPONSE)
3264 || (msg->msg.cmd != IPMI_GET_DEVICE_GUID_CMD))
3268 if (msg->msg.data[0] != 0) {
3269 /* Error from getting the GUID, the BMC doesn't have one. */
3270 bmc->dyn_guid_set = 0;
3274 if (msg->msg.data_len < UUID_SIZE + 1) {
3275 bmc->dyn_guid_set = 0;
3276 dev_warn(intf->si_dev,
3277 "The GUID response from the BMC was too short, it was %d but should have been %d. Assuming GUID is not available.\n",
3278 msg->msg.data_len, UUID_SIZE + 1);
3282 import_guid(&bmc->fetch_guid, msg->msg.data + 1);
3284 * Make sure the guid data is available before setting
3288 bmc->dyn_guid_set = 1;
3290 wake_up(&intf->waitq);
3293 static void __get_guid(struct ipmi_smi *intf)
3296 struct bmc_device *bmc = intf->bmc;
3298 bmc->dyn_guid_set = 2;
3299 intf->null_user_handler = guid_handler;
3300 rv = send_guid_cmd(intf, 0);
3302 /* Send failed, no GUID available. */
3303 bmc->dyn_guid_set = 0;
3305 wait_event(intf->waitq, bmc->dyn_guid_set != 2);
3307 /* dyn_guid_set makes the guid data available. */
3310 intf->null_user_handler = NULL;
3314 send_channel_info_cmd(struct ipmi_smi *intf, int chan)
3316 struct kernel_ipmi_msg msg;
3317 unsigned char data[1];
3318 struct ipmi_system_interface_addr si;
3320 si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3321 si.channel = IPMI_BMC_CHANNEL;
3324 msg.netfn = IPMI_NETFN_APP_REQUEST;
3325 msg.cmd = IPMI_GET_CHANNEL_INFO_CMD;
3329 return i_ipmi_request(NULL,
3331 (struct ipmi_addr *) &si,
3338 intf->addrinfo[0].address,
3339 intf->addrinfo[0].lun,
3344 channel_handler(struct ipmi_smi *intf, struct ipmi_recv_msg *msg)
3348 unsigned int set = intf->curr_working_cset;
3349 struct ipmi_channel *chans;
3351 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
3352 && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
3353 && (msg->msg.cmd == IPMI_GET_CHANNEL_INFO_CMD)) {
3354 /* It's the one we want */
3355 if (msg->msg.data[0] != 0) {
3356 /* Got an error from the channel, just go on. */
3357 if (msg->msg.data[0] == IPMI_INVALID_COMMAND_ERR) {
3359 * If the MC does not support this
3360 * command, that is legal. We just
3361 * assume it has one IPMB at channel
3364 intf->wchannels[set].c[0].medium
3365 = IPMI_CHANNEL_MEDIUM_IPMB;
3366 intf->wchannels[set].c[0].protocol
3367 = IPMI_CHANNEL_PROTOCOL_IPMB;
3369 intf->channel_list = intf->wchannels + set;
3370 intf->channels_ready = true;
3371 wake_up(&intf->waitq);
3376 if (msg->msg.data_len < 4) {
3377 /* Message not big enough, just go on. */
3380 ch = intf->curr_channel;
3381 chans = intf->wchannels[set].c;
3382 chans[ch].medium = msg->msg.data[2] & 0x7f;
3383 chans[ch].protocol = msg->msg.data[3] & 0x1f;
3386 intf->curr_channel++;
3387 if (intf->curr_channel >= IPMI_MAX_CHANNELS) {
3388 intf->channel_list = intf->wchannels + set;
3389 intf->channels_ready = true;
3390 wake_up(&intf->waitq);
3392 intf->channel_list = intf->wchannels + set;
3393 intf->channels_ready = true;
3394 rv = send_channel_info_cmd(intf, intf->curr_channel);
3398 /* Got an error somehow, just give up. */
3399 dev_warn(intf->si_dev,
3400 "Error sending channel information for channel %d: %d\n",
3401 intf->curr_channel, rv);
3403 intf->channel_list = intf->wchannels + set;
3404 intf->channels_ready = true;
3405 wake_up(&intf->waitq);
3413 * Must be holding intf->bmc_reg_mutex to call this.
3415 static int __scan_channels(struct ipmi_smi *intf, struct ipmi_device_id *id)
3419 if (ipmi_version_major(id) > 1
3420 || (ipmi_version_major(id) == 1
3421 && ipmi_version_minor(id) >= 5)) {
3425 * Start scanning the channels to see what is
3428 set = !intf->curr_working_cset;
3429 intf->curr_working_cset = set;
3430 memset(&intf->wchannels[set], 0,
3431 sizeof(struct ipmi_channel_set));
3433 intf->null_user_handler = channel_handler;
3434 intf->curr_channel = 0;
3435 rv = send_channel_info_cmd(intf, 0);
3437 dev_warn(intf->si_dev,
3438 "Error sending channel information for channel 0, %d\n",
3440 intf->null_user_handler = NULL;
3444 /* Wait for the channel info to be read. */
3445 wait_event(intf->waitq, intf->channels_ready);
3446 intf->null_user_handler = NULL;
3448 unsigned int set = intf->curr_working_cset;
3450 /* Assume a single IPMB channel at zero. */
3451 intf->wchannels[set].c[0].medium = IPMI_CHANNEL_MEDIUM_IPMB;
3452 intf->wchannels[set].c[0].protocol = IPMI_CHANNEL_PROTOCOL_IPMB;
3453 intf->channel_list = intf->wchannels + set;
3454 intf->channels_ready = true;
3460 static void ipmi_poll(struct ipmi_smi *intf)
3462 if (intf->handlers->poll)
3463 intf->handlers->poll(intf->send_info);
3464 /* In case something came in */
3465 handle_new_recv_msgs(intf);
3468 void ipmi_poll_interface(struct ipmi_user *user)
3470 ipmi_poll(user->intf);
3472 EXPORT_SYMBOL(ipmi_poll_interface);
3474 static void redo_bmc_reg(struct work_struct *work)
3476 struct ipmi_smi *intf = container_of(work, struct ipmi_smi,
3479 if (!intf->in_shutdown)
3480 bmc_get_device_id(intf, NULL, NULL, NULL, NULL);
3482 kref_put(&intf->refcount, intf_free);
3485 int ipmi_add_smi(struct module *owner,
3486 const struct ipmi_smi_handlers *handlers,
3488 struct device *si_dev,
3489 unsigned char slave_addr)
3493 struct ipmi_smi *intf, *tintf;
3494 struct list_head *link;
3495 struct ipmi_device_id id;
3498 * Make sure the driver is actually initialized, this handles
3499 * problems with initialization order.
3501 rv = ipmi_init_msghandler();
3505 intf = kzalloc(sizeof(*intf), GFP_KERNEL);
3509 rv = init_srcu_struct(&intf->users_srcu);
3515 intf->owner = owner;
3516 intf->bmc = &intf->tmp_bmc;
3517 INIT_LIST_HEAD(&intf->bmc->intfs);
3518 mutex_init(&intf->bmc->dyn_mutex);
3519 INIT_LIST_HEAD(&intf->bmc_link);
3520 mutex_init(&intf->bmc_reg_mutex);
3521 intf->intf_num = -1; /* Mark it invalid for now. */
3522 kref_init(&intf->refcount);
3523 INIT_WORK(&intf->bmc_reg_work, redo_bmc_reg);
3524 intf->si_dev = si_dev;
3525 for (j = 0; j < IPMI_MAX_CHANNELS; j++) {
3526 intf->addrinfo[j].address = IPMI_BMC_SLAVE_ADDR;
3527 intf->addrinfo[j].lun = 2;
3529 if (slave_addr != 0)
3530 intf->addrinfo[0].address = slave_addr;
3531 INIT_LIST_HEAD(&intf->users);
3532 intf->handlers = handlers;
3533 intf->send_info = send_info;
3534 spin_lock_init(&intf->seq_lock);
3535 for (j = 0; j < IPMI_IPMB_NUM_SEQ; j++) {
3536 intf->seq_table[j].inuse = 0;
3537 intf->seq_table[j].seqid = 0;
3540 spin_lock_init(&intf->waiting_rcv_msgs_lock);
3541 INIT_LIST_HEAD(&intf->waiting_rcv_msgs);
3542 tasklet_setup(&intf->recv_tasklet,
3544 atomic_set(&intf->watchdog_pretimeouts_to_deliver, 0);
3545 spin_lock_init(&intf->xmit_msgs_lock);
3546 INIT_LIST_HEAD(&intf->xmit_msgs);
3547 INIT_LIST_HEAD(&intf->hp_xmit_msgs);
3548 spin_lock_init(&intf->events_lock);
3549 spin_lock_init(&intf->watch_lock);
3550 atomic_set(&intf->event_waiters, 0);
3551 intf->ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
3552 INIT_LIST_HEAD(&intf->waiting_events);
3553 intf->waiting_events_count = 0;
3554 mutex_init(&intf->cmd_rcvrs_mutex);
3555 spin_lock_init(&intf->maintenance_mode_lock);
3556 INIT_LIST_HEAD(&intf->cmd_rcvrs);
3557 init_waitqueue_head(&intf->waitq);
3558 for (i = 0; i < IPMI_NUM_STATS; i++)
3559 atomic_set(&intf->stats[i], 0);
3561 mutex_lock(&ipmi_interfaces_mutex);
3562 /* Look for a hole in the numbers. */
3564 link = &ipmi_interfaces;
3565 list_for_each_entry_rcu(tintf, &ipmi_interfaces, link,
3566 ipmi_interfaces_mutex_held()) {
3567 if (tintf->intf_num != i) {
3568 link = &tintf->link;
3573 /* Add the new interface in numeric order. */
3575 list_add_rcu(&intf->link, &ipmi_interfaces);
3577 list_add_tail_rcu(&intf->link, link);
3579 rv = handlers->start_processing(send_info, intf);
3583 rv = __bmc_get_device_id(intf, NULL, &id, NULL, NULL, i);
3585 dev_err(si_dev, "Unable to get the device id: %d\n", rv);
3586 goto out_err_started;
3589 mutex_lock(&intf->bmc_reg_mutex);
3590 rv = __scan_channels(intf, &id);
3591 mutex_unlock(&intf->bmc_reg_mutex);
3593 goto out_err_bmc_reg;
3596 * Keep memory order straight for RCU readers. Make
3597 * sure everything else is committed to memory before
3598 * setting intf_num to mark the interface valid.
3602 mutex_unlock(&ipmi_interfaces_mutex);
3604 /* After this point the interface is legal to use. */
3605 call_smi_watchers(i, intf->si_dev);
3610 ipmi_bmc_unregister(intf);
3612 if (intf->handlers->shutdown)
3613 intf->handlers->shutdown(intf->send_info);
3615 list_del_rcu(&intf->link);
3616 mutex_unlock(&ipmi_interfaces_mutex);
3617 synchronize_srcu(&ipmi_interfaces_srcu);
3618 cleanup_srcu_struct(&intf->users_srcu);
3619 kref_put(&intf->refcount, intf_free);
3623 EXPORT_SYMBOL(ipmi_add_smi);
3625 static void deliver_smi_err_response(struct ipmi_smi *intf,
3626 struct ipmi_smi_msg *msg,
3629 msg->rsp[0] = msg->data[0] | 4;
3630 msg->rsp[1] = msg->data[1];
3633 /* It's an error, so it will never requeue, no need to check return. */
3634 handle_one_recv_msg(intf, msg);
3637 static void cleanup_smi_msgs(struct ipmi_smi *intf)
3640 struct seq_table *ent;
3641 struct ipmi_smi_msg *msg;
3642 struct list_head *entry;
3643 struct list_head tmplist;
3645 /* Clear out our transmit queues and hold the messages. */
3646 INIT_LIST_HEAD(&tmplist);
3647 list_splice_tail(&intf->hp_xmit_msgs, &tmplist);
3648 list_splice_tail(&intf->xmit_msgs, &tmplist);
3650 /* Current message first, to preserve order */
3651 while (intf->curr_msg && !list_empty(&intf->waiting_rcv_msgs)) {
3652 /* Wait for the message to clear out. */
3653 schedule_timeout(1);
3656 /* No need for locks, the interface is down. */
3659 * Return errors for all pending messages in queue and in the
3660 * tables waiting for remote responses.
3662 while (!list_empty(&tmplist)) {
3663 entry = tmplist.next;
3665 msg = list_entry(entry, struct ipmi_smi_msg, link);
3666 deliver_smi_err_response(intf, msg, IPMI_ERR_UNSPECIFIED);
3669 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
3670 ent = &intf->seq_table[i];
3673 deliver_err_response(intf, ent->recv_msg, IPMI_ERR_UNSPECIFIED);
3677 void ipmi_unregister_smi(struct ipmi_smi *intf)
3679 struct ipmi_smi_watcher *w;
3680 int intf_num = intf->intf_num, index;
3682 mutex_lock(&ipmi_interfaces_mutex);
3683 intf->intf_num = -1;
3684 intf->in_shutdown = true;
3685 list_del_rcu(&intf->link);
3686 mutex_unlock(&ipmi_interfaces_mutex);
3687 synchronize_srcu(&ipmi_interfaces_srcu);
3689 /* At this point no users can be added to the interface. */
3692 * Call all the watcher interfaces to tell them that
3693 * an interface is going away.
3695 mutex_lock(&smi_watchers_mutex);
3696 list_for_each_entry(w, &smi_watchers, link)
3697 w->smi_gone(intf_num);
3698 mutex_unlock(&smi_watchers_mutex);
3700 index = srcu_read_lock(&intf->users_srcu);
3701 while (!list_empty(&intf->users)) {
3702 struct ipmi_user *user =
3703 container_of(list_next_rcu(&intf->users),
3704 struct ipmi_user, link);
3706 _ipmi_destroy_user(user);
3708 srcu_read_unlock(&intf->users_srcu, index);
3710 if (intf->handlers->shutdown)
3711 intf->handlers->shutdown(intf->send_info);
3713 cleanup_smi_msgs(intf);
3715 ipmi_bmc_unregister(intf);
3717 cleanup_srcu_struct(&intf->users_srcu);
3718 kref_put(&intf->refcount, intf_free);
3720 EXPORT_SYMBOL(ipmi_unregister_smi);
3722 static int handle_ipmb_get_msg_rsp(struct ipmi_smi *intf,
3723 struct ipmi_smi_msg *msg)
3725 struct ipmi_ipmb_addr ipmb_addr;
3726 struct ipmi_recv_msg *recv_msg;
3729 * This is 11, not 10, because the response must contain a
3732 if (msg->rsp_size < 11) {
3733 /* Message not big enough, just ignore it. */
3734 ipmi_inc_stat(intf, invalid_ipmb_responses);
3738 if (msg->rsp[2] != 0) {
3739 /* An error getting the response, just ignore it. */
3743 ipmb_addr.addr_type = IPMI_IPMB_ADDR_TYPE;
3744 ipmb_addr.slave_addr = msg->rsp[6];
3745 ipmb_addr.channel = msg->rsp[3] & 0x0f;
3746 ipmb_addr.lun = msg->rsp[7] & 3;
3749 * It's a response from a remote entity. Look up the sequence
3750 * number and handle the response.
3752 if (intf_find_seq(intf,
3756 (msg->rsp[4] >> 2) & (~1),
3757 (struct ipmi_addr *) &ipmb_addr,
3760 * We were unable to find the sequence number,
3761 * so just nuke the message.
3763 ipmi_inc_stat(intf, unhandled_ipmb_responses);
3767 memcpy(recv_msg->msg_data, &msg->rsp[9], msg->rsp_size - 9);
3769 * The other fields matched, so no need to set them, except
3770 * for netfn, which needs to be the response that was
3771 * returned, not the request value.
3773 recv_msg->msg.netfn = msg->rsp[4] >> 2;
3774 recv_msg->msg.data = recv_msg->msg_data;
3775 recv_msg->msg.data_len = msg->rsp_size - 10;
3776 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
3777 if (deliver_response(intf, recv_msg))
3778 ipmi_inc_stat(intf, unhandled_ipmb_responses);
3780 ipmi_inc_stat(intf, handled_ipmb_responses);
3785 static int handle_ipmb_get_msg_cmd(struct ipmi_smi *intf,
3786 struct ipmi_smi_msg *msg)
3788 struct cmd_rcvr *rcvr;
3790 unsigned char netfn;
3793 struct ipmi_user *user = NULL;
3794 struct ipmi_ipmb_addr *ipmb_addr;
3795 struct ipmi_recv_msg *recv_msg;
3797 if (msg->rsp_size < 10) {
3798 /* Message not big enough, just ignore it. */
3799 ipmi_inc_stat(intf, invalid_commands);
3803 if (msg->rsp[2] != 0) {
3804 /* An error getting the response, just ignore it. */
3808 netfn = msg->rsp[4] >> 2;
3810 chan = msg->rsp[3] & 0xf;
3813 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
3816 kref_get(&user->refcount);
3822 /* We didn't find a user, deliver an error response. */
3823 ipmi_inc_stat(intf, unhandled_commands);
3825 msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
3826 msg->data[1] = IPMI_SEND_MSG_CMD;
3827 msg->data[2] = msg->rsp[3];
3828 msg->data[3] = msg->rsp[6];
3829 msg->data[4] = ((netfn + 1) << 2) | (msg->rsp[7] & 0x3);
3830 msg->data[5] = ipmb_checksum(&msg->data[3], 2);
3831 msg->data[6] = intf->addrinfo[msg->rsp[3] & 0xf].address;
3833 msg->data[7] = (msg->rsp[7] & 0xfc) | (msg->rsp[4] & 0x3);
3834 msg->data[8] = msg->rsp[8]; /* cmd */
3835 msg->data[9] = IPMI_INVALID_CMD_COMPLETION_CODE;
3836 msg->data[10] = ipmb_checksum(&msg->data[6], 4);
3837 msg->data_size = 11;
3839 pr_debug("Invalid command: %*ph\n", msg->data_size, msg->data);
3842 if (!intf->in_shutdown) {
3843 smi_send(intf, intf->handlers, msg, 0);
3845 * We used the message, so return the value
3846 * that causes it to not be freed or
3853 recv_msg = ipmi_alloc_recv_msg();
3856 * We couldn't allocate memory for the
3857 * message, so requeue it for handling
3861 kref_put(&user->refcount, free_user);
3863 /* Extract the source address from the data. */
3864 ipmb_addr = (struct ipmi_ipmb_addr *) &recv_msg->addr;
3865 ipmb_addr->addr_type = IPMI_IPMB_ADDR_TYPE;
3866 ipmb_addr->slave_addr = msg->rsp[6];
3867 ipmb_addr->lun = msg->rsp[7] & 3;
3868 ipmb_addr->channel = msg->rsp[3] & 0xf;
3871 * Extract the rest of the message information
3872 * from the IPMB header.
3874 recv_msg->user = user;
3875 recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
3876 recv_msg->msgid = msg->rsp[7] >> 2;
3877 recv_msg->msg.netfn = msg->rsp[4] >> 2;
3878 recv_msg->msg.cmd = msg->rsp[8];
3879 recv_msg->msg.data = recv_msg->msg_data;
3882 * We chop off 10, not 9 bytes because the checksum
3883 * at the end also needs to be removed.
3885 recv_msg->msg.data_len = msg->rsp_size - 10;
3886 memcpy(recv_msg->msg_data, &msg->rsp[9],
3887 msg->rsp_size - 10);
3888 if (deliver_response(intf, recv_msg))
3889 ipmi_inc_stat(intf, unhandled_commands);
3891 ipmi_inc_stat(intf, handled_commands);
3898 static int handle_ipmb_direct_rcv_cmd(struct ipmi_smi *intf,
3899 struct ipmi_smi_msg *msg)
3901 struct cmd_rcvr *rcvr;
3903 struct ipmi_user *user = NULL;
3904 struct ipmi_ipmb_direct_addr *daddr;
3905 struct ipmi_recv_msg *recv_msg;
3906 unsigned char netfn = msg->rsp[0] >> 2;
3907 unsigned char cmd = msg->rsp[3];
3910 /* We always use channel 0 for direct messages. */
3911 rcvr = find_cmd_rcvr(intf, netfn, cmd, 0);
3914 kref_get(&user->refcount);
3920 /* We didn't find a user, deliver an error response. */
3921 ipmi_inc_stat(intf, unhandled_commands);
3923 msg->data[0] = (netfn + 1) << 2;
3924 msg->data[0] |= msg->rsp[2] & 0x3; /* rqLUN */
3925 msg->data[1] = msg->rsp[1]; /* Addr */
3926 msg->data[2] = msg->rsp[2] & ~0x3; /* rqSeq */
3927 msg->data[2] |= msg->rsp[0] & 0x3; /* rsLUN */
3929 msg->data[4] = IPMI_INVALID_CMD_COMPLETION_CODE;
3933 if (!intf->in_shutdown) {
3934 smi_send(intf, intf->handlers, msg, 0);
3936 * We used the message, so return the value
3937 * that causes it to not be freed or
3944 recv_msg = ipmi_alloc_recv_msg();
3947 * We couldn't allocate memory for the
3948 * message, so requeue it for handling
3952 kref_put(&user->refcount, free_user);
3954 /* Extract the source address from the data. */
3955 daddr = (struct ipmi_ipmb_direct_addr *)&recv_msg->addr;
3956 daddr->addr_type = IPMI_IPMB_DIRECT_ADDR_TYPE;
3958 daddr->slave_addr = msg->rsp[1];
3959 daddr->rs_lun = msg->rsp[0] & 3;
3960 daddr->rq_lun = msg->rsp[2] & 3;
3963 * Extract the rest of the message information
3964 * from the IPMB header.
3966 recv_msg->user = user;
3967 recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
3968 recv_msg->msgid = (msg->rsp[2] >> 2);
3969 recv_msg->msg.netfn = msg->rsp[0] >> 2;
3970 recv_msg->msg.cmd = msg->rsp[3];
3971 recv_msg->msg.data = recv_msg->msg_data;
3973 recv_msg->msg.data_len = msg->rsp_size - 4;
3974 memcpy(recv_msg->msg_data, msg->rsp + 4,
3976 if (deliver_response(intf, recv_msg))
3977 ipmi_inc_stat(intf, unhandled_commands);
3979 ipmi_inc_stat(intf, handled_commands);
3986 static int handle_ipmb_direct_rcv_rsp(struct ipmi_smi *intf,
3987 struct ipmi_smi_msg *msg)
3989 struct ipmi_recv_msg *recv_msg;
3990 struct ipmi_ipmb_direct_addr *daddr;
3992 recv_msg = (struct ipmi_recv_msg *) msg->user_data;
3993 if (recv_msg == NULL) {
3994 dev_warn(intf->si_dev,
3995 "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");
3999 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
4000 recv_msg->msgid = msg->msgid;
4001 daddr = (struct ipmi_ipmb_direct_addr *) &recv_msg->addr;
4002 daddr->addr_type = IPMI_IPMB_DIRECT_ADDR_TYPE;
4004 daddr->slave_addr = msg->rsp[1];
4005 daddr->rq_lun = msg->rsp[0] & 3;
4006 daddr->rs_lun = msg->rsp[2] & 3;
4007 recv_msg->msg.netfn = msg->rsp[0] >> 2;
4008 recv_msg->msg.cmd = msg->rsp[3];
4009 memcpy(recv_msg->msg_data, &msg->rsp[4], msg->rsp_size - 4);
4010 recv_msg->msg.data = recv_msg->msg_data;
4011 recv_msg->msg.data_len = msg->rsp_size - 4;
4012 deliver_local_response(intf, recv_msg);
4017 static int handle_lan_get_msg_rsp(struct ipmi_smi *intf,
4018 struct ipmi_smi_msg *msg)
4020 struct ipmi_lan_addr lan_addr;
4021 struct ipmi_recv_msg *recv_msg;
4025 * This is 13, not 12, because the response must contain a
4028 if (msg->rsp_size < 13) {
4029 /* Message not big enough, just ignore it. */
4030 ipmi_inc_stat(intf, invalid_lan_responses);
4034 if (msg->rsp[2] != 0) {
4035 /* An error getting the response, just ignore it. */
4039 lan_addr.addr_type = IPMI_LAN_ADDR_TYPE;
4040 lan_addr.session_handle = msg->rsp[4];
4041 lan_addr.remote_SWID = msg->rsp[8];
4042 lan_addr.local_SWID = msg->rsp[5];
4043 lan_addr.channel = msg->rsp[3] & 0x0f;
4044 lan_addr.privilege = msg->rsp[3] >> 4;
4045 lan_addr.lun = msg->rsp[9] & 3;
4048 * It's a response from a remote entity. Look up the sequence
4049 * number and handle the response.
4051 if (intf_find_seq(intf,
4055 (msg->rsp[6] >> 2) & (~1),
4056 (struct ipmi_addr *) &lan_addr,
4059 * We were unable to find the sequence number,
4060 * so just nuke the message.
4062 ipmi_inc_stat(intf, unhandled_lan_responses);
4066 memcpy(recv_msg->msg_data, &msg->rsp[11], msg->rsp_size - 11);
4068 * The other fields matched, so no need to set them, except
4069 * for netfn, which needs to be the response that was
4070 * returned, not the request value.
4072 recv_msg->msg.netfn = msg->rsp[6] >> 2;
4073 recv_msg->msg.data = recv_msg->msg_data;
4074 recv_msg->msg.data_len = msg->rsp_size - 12;
4075 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
4076 if (deliver_response(intf, recv_msg))
4077 ipmi_inc_stat(intf, unhandled_lan_responses);
4079 ipmi_inc_stat(intf, handled_lan_responses);
4084 static int handle_lan_get_msg_cmd(struct ipmi_smi *intf,
4085 struct ipmi_smi_msg *msg)
4087 struct cmd_rcvr *rcvr;
4089 unsigned char netfn;
4092 struct ipmi_user *user = NULL;
4093 struct ipmi_lan_addr *lan_addr;
4094 struct ipmi_recv_msg *recv_msg;
4096 if (msg->rsp_size < 12) {
4097 /* Message not big enough, just ignore it. */
4098 ipmi_inc_stat(intf, invalid_commands);
4102 if (msg->rsp[2] != 0) {
4103 /* An error getting the response, just ignore it. */
4107 netfn = msg->rsp[6] >> 2;
4109 chan = msg->rsp[3] & 0xf;
4112 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
4115 kref_get(&user->refcount);
4121 /* We didn't find a user, just give up. */
4122 ipmi_inc_stat(intf, unhandled_commands);
4125 * Don't do anything with these messages, just allow
4130 recv_msg = ipmi_alloc_recv_msg();
4133 * We couldn't allocate memory for the
4134 * message, so requeue it for handling later.
4137 kref_put(&user->refcount, free_user);
4139 /* Extract the source address from the data. */
4140 lan_addr = (struct ipmi_lan_addr *) &recv_msg->addr;
4141 lan_addr->addr_type = IPMI_LAN_ADDR_TYPE;
4142 lan_addr->session_handle = msg->rsp[4];
4143 lan_addr->remote_SWID = msg->rsp[8];
4144 lan_addr->local_SWID = msg->rsp[5];
4145 lan_addr->lun = msg->rsp[9] & 3;
4146 lan_addr->channel = msg->rsp[3] & 0xf;
4147 lan_addr->privilege = msg->rsp[3] >> 4;
4150 * Extract the rest of the message information
4151 * from the IPMB header.
4153 recv_msg->user = user;
4154 recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
4155 recv_msg->msgid = msg->rsp[9] >> 2;
4156 recv_msg->msg.netfn = msg->rsp[6] >> 2;
4157 recv_msg->msg.cmd = msg->rsp[10];
4158 recv_msg->msg.data = recv_msg->msg_data;
4161 * We chop off 12, not 11 bytes because the checksum
4162 * at the end also needs to be removed.
4164 recv_msg->msg.data_len = msg->rsp_size - 12;
4165 memcpy(recv_msg->msg_data, &msg->rsp[11],
4166 msg->rsp_size - 12);
4167 if (deliver_response(intf, recv_msg))
4168 ipmi_inc_stat(intf, unhandled_commands);
4170 ipmi_inc_stat(intf, handled_commands);
4178 * This routine will handle "Get Message" command responses with
4179 * channels that use an OEM Medium. The message format belongs to
4180 * the OEM. See IPMI 2.0 specification, Chapter 6 and
4181 * Chapter 22, sections 22.6 and 22.24 for more details.
4183 static int handle_oem_get_msg_cmd(struct ipmi_smi *intf,
4184 struct ipmi_smi_msg *msg)
4186 struct cmd_rcvr *rcvr;
4188 unsigned char netfn;
4191 struct ipmi_user *user = NULL;
4192 struct ipmi_system_interface_addr *smi_addr;
4193 struct ipmi_recv_msg *recv_msg;
4196 * We expect the OEM SW to perform error checking
4197 * so we just do some basic sanity checks
4199 if (msg->rsp_size < 4) {
4200 /* Message not big enough, just ignore it. */
4201 ipmi_inc_stat(intf, invalid_commands);
4205 if (msg->rsp[2] != 0) {
4206 /* An error getting the response, just ignore it. */
4211 * This is an OEM Message so the OEM needs to know how
4212 * handle the message. We do no interpretation.
4214 netfn = msg->rsp[0] >> 2;
4216 chan = msg->rsp[3] & 0xf;
4219 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
4222 kref_get(&user->refcount);
4228 /* We didn't find a user, just give up. */
4229 ipmi_inc_stat(intf, unhandled_commands);
4232 * Don't do anything with these messages, just allow
4238 recv_msg = ipmi_alloc_recv_msg();
4241 * We couldn't allocate memory for the
4242 * message, so requeue it for handling
4246 kref_put(&user->refcount, free_user);
4249 * OEM Messages are expected to be delivered via
4250 * the system interface to SMS software. We might
4251 * need to visit this again depending on OEM
4254 smi_addr = ((struct ipmi_system_interface_addr *)
4256 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
4257 smi_addr->channel = IPMI_BMC_CHANNEL;
4258 smi_addr->lun = msg->rsp[0] & 3;
4260 recv_msg->user = user;
4261 recv_msg->user_msg_data = NULL;
4262 recv_msg->recv_type = IPMI_OEM_RECV_TYPE;
4263 recv_msg->msg.netfn = msg->rsp[0] >> 2;
4264 recv_msg->msg.cmd = msg->rsp[1];
4265 recv_msg->msg.data = recv_msg->msg_data;
4268 * The message starts at byte 4 which follows the
4269 * the Channel Byte in the "GET MESSAGE" command
4271 recv_msg->msg.data_len = msg->rsp_size - 4;
4272 memcpy(recv_msg->msg_data, &msg->rsp[4],
4274 if (deliver_response(intf, recv_msg))
4275 ipmi_inc_stat(intf, unhandled_commands);
4277 ipmi_inc_stat(intf, handled_commands);
4284 static void copy_event_into_recv_msg(struct ipmi_recv_msg *recv_msg,
4285 struct ipmi_smi_msg *msg)
4287 struct ipmi_system_interface_addr *smi_addr;
4289 recv_msg->msgid = 0;
4290 smi_addr = (struct ipmi_system_interface_addr *) &recv_msg->addr;
4291 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
4292 smi_addr->channel = IPMI_BMC_CHANNEL;
4293 smi_addr->lun = msg->rsp[0] & 3;
4294 recv_msg->recv_type = IPMI_ASYNC_EVENT_RECV_TYPE;
4295 recv_msg->msg.netfn = msg->rsp[0] >> 2;
4296 recv_msg->msg.cmd = msg->rsp[1];
4297 memcpy(recv_msg->msg_data, &msg->rsp[3], msg->rsp_size - 3);
4298 recv_msg->msg.data = recv_msg->msg_data;
4299 recv_msg->msg.data_len = msg->rsp_size - 3;
4302 static int handle_read_event_rsp(struct ipmi_smi *intf,
4303 struct ipmi_smi_msg *msg)
4305 struct ipmi_recv_msg *recv_msg, *recv_msg2;
4306 struct list_head msgs;
4307 struct ipmi_user *user;
4308 int rv = 0, deliver_count = 0, index;
4309 unsigned long flags;
4311 if (msg->rsp_size < 19) {
4312 /* Message is too small to be an IPMB event. */
4313 ipmi_inc_stat(intf, invalid_events);
4317 if (msg->rsp[2] != 0) {
4318 /* An error getting the event, just ignore it. */
4322 INIT_LIST_HEAD(&msgs);
4324 spin_lock_irqsave(&intf->events_lock, flags);
4326 ipmi_inc_stat(intf, events);
4329 * Allocate and fill in one message for every user that is
4332 index = srcu_read_lock(&intf->users_srcu);
4333 list_for_each_entry_rcu(user, &intf->users, link) {
4334 if (!user->gets_events)
4337 recv_msg = ipmi_alloc_recv_msg();
4340 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs,
4342 list_del(&recv_msg->link);
4343 ipmi_free_recv_msg(recv_msg);
4346 * We couldn't allocate memory for the
4347 * message, so requeue it for handling
4356 copy_event_into_recv_msg(recv_msg, msg);
4357 recv_msg->user = user;
4358 kref_get(&user->refcount);
4359 list_add_tail(&recv_msg->link, &msgs);
4361 srcu_read_unlock(&intf->users_srcu, index);
4363 if (deliver_count) {
4364 /* Now deliver all the messages. */
4365 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs, link) {
4366 list_del(&recv_msg->link);
4367 deliver_local_response(intf, recv_msg);
4369 } else if (intf->waiting_events_count < MAX_EVENTS_IN_QUEUE) {
4371 * No one to receive the message, put it in queue if there's
4372 * not already too many things in the queue.
4374 recv_msg = ipmi_alloc_recv_msg();
4377 * We couldn't allocate memory for the
4378 * message, so requeue it for handling
4385 copy_event_into_recv_msg(recv_msg, msg);
4386 list_add_tail(&recv_msg->link, &intf->waiting_events);
4387 intf->waiting_events_count++;
4388 } else if (!intf->event_msg_printed) {
4390 * There's too many things in the queue, discard this
4393 dev_warn(intf->si_dev,
4394 "Event queue full, discarding incoming events\n");
4395 intf->event_msg_printed = 1;
4399 spin_unlock_irqrestore(&intf->events_lock, flags);
4404 static int handle_bmc_rsp(struct ipmi_smi *intf,
4405 struct ipmi_smi_msg *msg)
4407 struct ipmi_recv_msg *recv_msg;
4408 struct ipmi_system_interface_addr *smi_addr;
4410 recv_msg = (struct ipmi_recv_msg *) msg->user_data;
4411 if (recv_msg == NULL) {
4412 dev_warn(intf->si_dev,
4413 "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");
4417 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
4418 recv_msg->msgid = msg->msgid;
4419 smi_addr = ((struct ipmi_system_interface_addr *)
4421 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
4422 smi_addr->channel = IPMI_BMC_CHANNEL;
4423 smi_addr->lun = msg->rsp[0] & 3;
4424 recv_msg->msg.netfn = msg->rsp[0] >> 2;
4425 recv_msg->msg.cmd = msg->rsp[1];
4426 memcpy(recv_msg->msg_data, &msg->rsp[2], msg->rsp_size - 2);
4427 recv_msg->msg.data = recv_msg->msg_data;
4428 recv_msg->msg.data_len = msg->rsp_size - 2;
4429 deliver_local_response(intf, recv_msg);
4435 * Handle a received message. Return 1 if the message should be requeued,
4436 * 0 if the message should be freed, or -1 if the message should not
4437 * be freed or requeued.
4439 static int handle_one_recv_msg(struct ipmi_smi *intf,
4440 struct ipmi_smi_msg *msg)
4445 bool is_cmd = !((msg->rsp[0] >> 2) & 1);
4447 pr_debug("Recv: %*ph\n", msg->rsp_size, msg->rsp);
4449 if (msg->rsp_size < 2) {
4450 /* Message is too small to be correct. */
4451 dev_warn(intf->si_dev,
4452 "BMC returned too small a message for netfn %x cmd %x, got %d bytes\n",
4453 (msg->data[0] >> 2) | 1, msg->data[1], msg->rsp_size);
4456 /* Generate an error response for the message. */
4457 msg->rsp[0] = msg->data[0] | (1 << 2);
4458 msg->rsp[1] = msg->data[1];
4459 msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
4461 } else if (msg->type == IPMI_SMI_MSG_TYPE_IPMB_DIRECT) {
4462 /* commands must have at least 4 bytes, responses 5. */
4463 if (is_cmd && (msg->rsp_size < 4)) {
4464 ipmi_inc_stat(intf, invalid_commands);
4467 if (!is_cmd && (msg->rsp_size < 5)) {
4468 ipmi_inc_stat(intf, invalid_ipmb_responses);
4469 /* Construct a valid error response. */
4470 msg->rsp[0] = msg->data[0] & 0xfc; /* NetFN */
4471 msg->rsp[0] |= (1 << 2); /* Make it a response */
4472 msg->rsp[0] |= msg->data[2] & 3; /* rqLUN */
4473 msg->rsp[1] = msg->data[1]; /* Addr */
4474 msg->rsp[2] = msg->data[2] & 0xfc; /* rqSeq */
4475 msg->rsp[2] |= msg->data[0] & 0x3; /* rsLUN */
4476 msg->rsp[3] = msg->data[3]; /* Cmd */
4477 msg->rsp[4] = IPMI_ERR_UNSPECIFIED;
4480 } else if ((msg->data_size >= 2)
4481 && (msg->data[0] == (IPMI_NETFN_APP_REQUEST << 2))
4482 && (msg->data[1] == IPMI_SEND_MSG_CMD)
4483 && (msg->user_data == NULL)) {
4485 if (intf->in_shutdown)
4489 * This is the local response to a command send, start
4490 * the timer for these. The user_data will not be
4491 * NULL if this is a response send, and we will let
4492 * response sends just go through.
4496 * Check for errors, if we get certain errors (ones
4497 * that mean basically we can try again later), we
4498 * ignore them and start the timer. Otherwise we
4499 * report the error immediately.
4501 if ((msg->rsp_size >= 3) && (msg->rsp[2] != 0)
4502 && (msg->rsp[2] != IPMI_NODE_BUSY_ERR)
4503 && (msg->rsp[2] != IPMI_LOST_ARBITRATION_ERR)
4504 && (msg->rsp[2] != IPMI_BUS_ERR)
4505 && (msg->rsp[2] != IPMI_NAK_ON_WRITE_ERR)) {
4506 int ch = msg->rsp[3] & 0xf;
4507 struct ipmi_channel *chans;
4509 /* Got an error sending the message, handle it. */
4511 chans = READ_ONCE(intf->channel_list)->c;
4512 if ((chans[ch].medium == IPMI_CHANNEL_MEDIUM_8023LAN)
4513 || (chans[ch].medium == IPMI_CHANNEL_MEDIUM_ASYNC))
4514 ipmi_inc_stat(intf, sent_lan_command_errs);
4516 ipmi_inc_stat(intf, sent_ipmb_command_errs);
4517 intf_err_seq(intf, msg->msgid, msg->rsp[2]);
4519 /* The message was sent, start the timer. */
4520 intf_start_seq_timer(intf, msg->msgid);
4521 } else if (((msg->rsp[0] >> 2) != ((msg->data[0] >> 2) | 1))
4522 || (msg->rsp[1] != msg->data[1])) {
4524 * The NetFN and Command in the response is not even
4525 * marginally correct.
4527 dev_warn(intf->si_dev,
4528 "BMC returned incorrect response, expected netfn %x cmd %x, got netfn %x cmd %x\n",
4529 (msg->data[0] >> 2) | 1, msg->data[1],
4530 msg->rsp[0] >> 2, msg->rsp[1]);
4532 goto return_unspecified;
4535 if (msg->type == IPMI_SMI_MSG_TYPE_IPMB_DIRECT) {
4536 if ((msg->data[0] >> 2) & 1) {
4537 /* It's a response to a sent response. */
4540 goto process_response_response;
4543 requeue = handle_ipmb_direct_rcv_cmd(intf, msg);
4545 requeue = handle_ipmb_direct_rcv_rsp(intf, msg);
4546 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
4547 && (msg->rsp[1] == IPMI_SEND_MSG_CMD)
4548 && (msg->user_data != NULL)) {
4550 * It's a response to a response we sent. For this we
4551 * deliver a send message response to the user.
4553 struct ipmi_recv_msg *recv_msg;
4555 chan = msg->data[2] & 0x0f;
4556 if (chan >= IPMI_MAX_CHANNELS)
4557 /* Invalid channel number */
4561 process_response_response:
4562 recv_msg = msg->user_data;
4568 recv_msg->recv_type = IPMI_RESPONSE_RESPONSE_TYPE;
4569 recv_msg->msg.data = recv_msg->msg_data;
4570 recv_msg->msg_data[0] = cc;
4571 recv_msg->msg.data_len = 1;
4572 deliver_local_response(intf, recv_msg);
4573 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
4574 && (msg->rsp[1] == IPMI_GET_MSG_CMD)) {
4575 struct ipmi_channel *chans;
4577 /* It's from the receive queue. */
4578 chan = msg->rsp[3] & 0xf;
4579 if (chan >= IPMI_MAX_CHANNELS) {
4580 /* Invalid channel number */
4586 * We need to make sure the channels have been initialized.
4587 * The channel_handler routine will set the "curr_channel"
4588 * equal to or greater than IPMI_MAX_CHANNELS when all the
4589 * channels for this interface have been initialized.
4591 if (!intf->channels_ready) {
4592 requeue = 0; /* Throw the message away */
4596 chans = READ_ONCE(intf->channel_list)->c;
4598 switch (chans[chan].medium) {
4599 case IPMI_CHANNEL_MEDIUM_IPMB:
4600 if (msg->rsp[4] & 0x04) {
4602 * It's a response, so find the
4603 * requesting message and send it up.
4605 requeue = handle_ipmb_get_msg_rsp(intf, msg);
4608 * It's a command to the SMS from some other
4609 * entity. Handle that.
4611 requeue = handle_ipmb_get_msg_cmd(intf, msg);
4615 case IPMI_CHANNEL_MEDIUM_8023LAN:
4616 case IPMI_CHANNEL_MEDIUM_ASYNC:
4617 if (msg->rsp[6] & 0x04) {
4619 * It's a response, so find the
4620 * requesting message and send it up.
4622 requeue = handle_lan_get_msg_rsp(intf, msg);
4625 * It's a command to the SMS from some other
4626 * entity. Handle that.
4628 requeue = handle_lan_get_msg_cmd(intf, msg);
4633 /* Check for OEM Channels. Clients had better
4634 register for these commands. */
4635 if ((chans[chan].medium >= IPMI_CHANNEL_MEDIUM_OEM_MIN)
4636 && (chans[chan].medium
4637 <= IPMI_CHANNEL_MEDIUM_OEM_MAX)) {
4638 requeue = handle_oem_get_msg_cmd(intf, msg);
4641 * We don't handle the channel type, so just
4648 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
4649 && (msg->rsp[1] == IPMI_READ_EVENT_MSG_BUFFER_CMD)) {
4650 /* It's an asynchronous event. */
4651 requeue = handle_read_event_rsp(intf, msg);
4653 /* It's a response from the local BMC. */
4654 requeue = handle_bmc_rsp(intf, msg);
4662 * If there are messages in the queue or pretimeouts, handle them.
4664 static void handle_new_recv_msgs(struct ipmi_smi *intf)
4666 struct ipmi_smi_msg *smi_msg;
4667 unsigned long flags = 0;
4669 int run_to_completion = intf->run_to_completion;
4671 /* See if any waiting messages need to be processed. */
4672 if (!run_to_completion)
4673 spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags);
4674 while (!list_empty(&intf->waiting_rcv_msgs)) {
4675 smi_msg = list_entry(intf->waiting_rcv_msgs.next,
4676 struct ipmi_smi_msg, link);
4677 list_del(&smi_msg->link);
4678 if (!run_to_completion)
4679 spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock,
4681 rv = handle_one_recv_msg(intf, smi_msg);
4682 if (!run_to_completion)
4683 spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags);
4686 * To preserve message order, quit if we
4687 * can't handle a message. Add the message
4688 * back at the head, this is safe because this
4689 * tasklet is the only thing that pulls the
4692 list_add(&smi_msg->link, &intf->waiting_rcv_msgs);
4696 /* Message handled */
4697 ipmi_free_smi_msg(smi_msg);
4698 /* If rv < 0, fatal error, del but don't free. */
4701 if (!run_to_completion)
4702 spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock, flags);
4705 * If the pretimout count is non-zero, decrement one from it and
4706 * deliver pretimeouts to all the users.
4708 if (atomic_add_unless(&intf->watchdog_pretimeouts_to_deliver, -1, 0)) {
4709 struct ipmi_user *user;
4712 index = srcu_read_lock(&intf->users_srcu);
4713 list_for_each_entry_rcu(user, &intf->users, link) {
4714 if (user->handler->ipmi_watchdog_pretimeout)
4715 user->handler->ipmi_watchdog_pretimeout(
4716 user->handler_data);
4718 srcu_read_unlock(&intf->users_srcu, index);
4722 static void smi_recv_tasklet(struct tasklet_struct *t)
4724 unsigned long flags = 0; /* keep us warning-free. */
4725 struct ipmi_smi *intf = from_tasklet(intf, t, recv_tasklet);
4726 int run_to_completion = intf->run_to_completion;
4727 struct ipmi_smi_msg *newmsg = NULL;
4730 * Start the next message if available.
4732 * Do this here, not in the actual receiver, because we may deadlock
4733 * because the lower layer is allowed to hold locks while calling
4739 if (!run_to_completion)
4740 spin_lock_irqsave(&intf->xmit_msgs_lock, flags);
4741 if (intf->curr_msg == NULL && !intf->in_shutdown) {
4742 struct list_head *entry = NULL;
4744 /* Pick the high priority queue first. */
4745 if (!list_empty(&intf->hp_xmit_msgs))
4746 entry = intf->hp_xmit_msgs.next;
4747 else if (!list_empty(&intf->xmit_msgs))
4748 entry = intf->xmit_msgs.next;
4752 newmsg = list_entry(entry, struct ipmi_smi_msg, link);
4753 intf->curr_msg = newmsg;
4757 if (!run_to_completion)
4758 spin_unlock_irqrestore(&intf->xmit_msgs_lock, flags);
4760 intf->handlers->sender(intf->send_info, newmsg);
4764 handle_new_recv_msgs(intf);
4767 /* Handle a new message from the lower layer. */
4768 void ipmi_smi_msg_received(struct ipmi_smi *intf,
4769 struct ipmi_smi_msg *msg)
4771 unsigned long flags = 0; /* keep us warning-free. */
4772 int run_to_completion = intf->run_to_completion;
4775 * To preserve message order, we keep a queue and deliver from
4778 if (!run_to_completion)
4779 spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags);
4780 list_add_tail(&msg->link, &intf->waiting_rcv_msgs);
4781 if (!run_to_completion)
4782 spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock,
4785 if (!run_to_completion)
4786 spin_lock_irqsave(&intf->xmit_msgs_lock, flags);
4788 * We can get an asynchronous event or receive message in addition
4789 * to commands we send.
4791 if (msg == intf->curr_msg)
4792 intf->curr_msg = NULL;
4793 if (!run_to_completion)
4794 spin_unlock_irqrestore(&intf->xmit_msgs_lock, flags);
4796 if (run_to_completion)
4797 smi_recv_tasklet(&intf->recv_tasklet);
4799 tasklet_schedule(&intf->recv_tasklet);
4801 EXPORT_SYMBOL(ipmi_smi_msg_received);
4803 void ipmi_smi_watchdog_pretimeout(struct ipmi_smi *intf)
4805 if (intf->in_shutdown)
4808 atomic_set(&intf->watchdog_pretimeouts_to_deliver, 1);
4809 tasklet_schedule(&intf->recv_tasklet);
4811 EXPORT_SYMBOL(ipmi_smi_watchdog_pretimeout);
4813 static struct ipmi_smi_msg *
4814 smi_from_recv_msg(struct ipmi_smi *intf, struct ipmi_recv_msg *recv_msg,
4815 unsigned char seq, long seqid)
4817 struct ipmi_smi_msg *smi_msg = ipmi_alloc_smi_msg();
4820 * If we can't allocate the message, then just return, we
4821 * get 4 retries, so this should be ok.
4825 memcpy(smi_msg->data, recv_msg->msg.data, recv_msg->msg.data_len);
4826 smi_msg->data_size = recv_msg->msg.data_len;
4827 smi_msg->msgid = STORE_SEQ_IN_MSGID(seq, seqid);
4829 pr_debug("Resend: %*ph\n", smi_msg->data_size, smi_msg->data);
4834 static void check_msg_timeout(struct ipmi_smi *intf, struct seq_table *ent,
4835 struct list_head *timeouts,
4836 unsigned long timeout_period,
4837 int slot, unsigned long *flags,
4840 struct ipmi_recv_msg *msg;
4842 if (intf->in_shutdown)
4848 if (timeout_period < ent->timeout) {
4849 ent->timeout -= timeout_period;
4854 if (ent->retries_left == 0) {
4855 /* The message has used all its retries. */
4857 smi_remove_watch(intf, IPMI_WATCH_MASK_CHECK_MESSAGES);
4858 msg = ent->recv_msg;
4859 list_add_tail(&msg->link, timeouts);
4861 ipmi_inc_stat(intf, timed_out_ipmb_broadcasts);
4862 else if (is_lan_addr(&ent->recv_msg->addr))
4863 ipmi_inc_stat(intf, timed_out_lan_commands);
4865 ipmi_inc_stat(intf, timed_out_ipmb_commands);
4867 struct ipmi_smi_msg *smi_msg;
4868 /* More retries, send again. */
4873 * Start with the max timer, set to normal timer after
4874 * the message is sent.
4876 ent->timeout = MAX_MSG_TIMEOUT;
4877 ent->retries_left--;
4878 smi_msg = smi_from_recv_msg(intf, ent->recv_msg, slot,
4881 if (is_lan_addr(&ent->recv_msg->addr))
4883 dropped_rexmit_lan_commands);
4886 dropped_rexmit_ipmb_commands);
4890 spin_unlock_irqrestore(&intf->seq_lock, *flags);
4893 * Send the new message. We send with a zero
4894 * priority. It timed out, I doubt time is that
4895 * critical now, and high priority messages are really
4896 * only for messages to the local MC, which don't get
4899 if (intf->handlers) {
4900 if (is_lan_addr(&ent->recv_msg->addr))
4902 retransmitted_lan_commands);
4905 retransmitted_ipmb_commands);
4907 smi_send(intf, intf->handlers, smi_msg, 0);
4909 ipmi_free_smi_msg(smi_msg);
4911 spin_lock_irqsave(&intf->seq_lock, *flags);
4915 static bool ipmi_timeout_handler(struct ipmi_smi *intf,
4916 unsigned long timeout_period)
4918 struct list_head timeouts;
4919 struct ipmi_recv_msg *msg, *msg2;
4920 unsigned long flags;
4922 bool need_timer = false;
4924 if (!intf->bmc_registered) {
4925 kref_get(&intf->refcount);
4926 if (!schedule_work(&intf->bmc_reg_work)) {
4927 kref_put(&intf->refcount, intf_free);
4933 * Go through the seq table and find any messages that
4934 * have timed out, putting them in the timeouts
4937 INIT_LIST_HEAD(&timeouts);
4938 spin_lock_irqsave(&intf->seq_lock, flags);
4939 if (intf->ipmb_maintenance_mode_timeout) {
4940 if (intf->ipmb_maintenance_mode_timeout <= timeout_period)
4941 intf->ipmb_maintenance_mode_timeout = 0;
4943 intf->ipmb_maintenance_mode_timeout -= timeout_period;
4945 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++)
4946 check_msg_timeout(intf, &intf->seq_table[i],
4947 &timeouts, timeout_period, i,
4948 &flags, &need_timer);
4949 spin_unlock_irqrestore(&intf->seq_lock, flags);
4951 list_for_each_entry_safe(msg, msg2, &timeouts, link)
4952 deliver_err_response(intf, msg, IPMI_TIMEOUT_COMPLETION_CODE);
4955 * Maintenance mode handling. Check the timeout
4956 * optimistically before we claim the lock. It may
4957 * mean a timeout gets missed occasionally, but that
4958 * only means the timeout gets extended by one period
4959 * in that case. No big deal, and it avoids the lock
4962 if (intf->auto_maintenance_timeout > 0) {
4963 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
4964 if (intf->auto_maintenance_timeout > 0) {
4965 intf->auto_maintenance_timeout
4967 if (!intf->maintenance_mode
4968 && (intf->auto_maintenance_timeout <= 0)) {
4969 intf->maintenance_mode_enable = false;
4970 maintenance_mode_update(intf);
4973 spin_unlock_irqrestore(&intf->maintenance_mode_lock,
4977 tasklet_schedule(&intf->recv_tasklet);
4982 static void ipmi_request_event(struct ipmi_smi *intf)
4984 /* No event requests when in maintenance mode. */
4985 if (intf->maintenance_mode_enable)
4988 if (!intf->in_shutdown)
4989 intf->handlers->request_events(intf->send_info);
4992 static struct timer_list ipmi_timer;
4994 static atomic_t stop_operation;
4996 static void ipmi_timeout(struct timer_list *unused)
4998 struct ipmi_smi *intf;
4999 bool need_timer = false;
5002 if (atomic_read(&stop_operation))
5005 index = srcu_read_lock(&ipmi_interfaces_srcu);
5006 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
5007 if (atomic_read(&intf->event_waiters)) {
5008 intf->ticks_to_req_ev--;
5009 if (intf->ticks_to_req_ev == 0) {
5010 ipmi_request_event(intf);
5011 intf->ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
5016 need_timer |= ipmi_timeout_handler(intf, IPMI_TIMEOUT_TIME);
5018 srcu_read_unlock(&ipmi_interfaces_srcu, index);
5021 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
5024 static void need_waiter(struct ipmi_smi *intf)
5026 /* Racy, but worst case we start the timer twice. */
5027 if (!timer_pending(&ipmi_timer))
5028 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
5031 static atomic_t smi_msg_inuse_count = ATOMIC_INIT(0);
5032 static atomic_t recv_msg_inuse_count = ATOMIC_INIT(0);
5034 static void free_smi_msg(struct ipmi_smi_msg *msg)
5036 atomic_dec(&smi_msg_inuse_count);
5037 /* Try to keep as much stuff out of the panic path as possible. */
5038 if (!oops_in_progress)
5042 struct ipmi_smi_msg *ipmi_alloc_smi_msg(void)
5044 struct ipmi_smi_msg *rv;
5045 rv = kmalloc(sizeof(struct ipmi_smi_msg), GFP_ATOMIC);
5047 rv->done = free_smi_msg;
5048 rv->user_data = NULL;
5049 rv->type = IPMI_SMI_MSG_TYPE_NORMAL;
5050 atomic_inc(&smi_msg_inuse_count);
5054 EXPORT_SYMBOL(ipmi_alloc_smi_msg);
5056 static void free_recv_msg(struct ipmi_recv_msg *msg)
5058 atomic_dec(&recv_msg_inuse_count);
5059 /* Try to keep as much stuff out of the panic path as possible. */
5060 if (!oops_in_progress)
5064 static struct ipmi_recv_msg *ipmi_alloc_recv_msg(void)
5066 struct ipmi_recv_msg *rv;
5068 rv = kmalloc(sizeof(struct ipmi_recv_msg), GFP_ATOMIC);
5071 rv->done = free_recv_msg;
5072 atomic_inc(&recv_msg_inuse_count);
5077 void ipmi_free_recv_msg(struct ipmi_recv_msg *msg)
5079 if (msg->user && !oops_in_progress)
5080 kref_put(&msg->user->refcount, free_user);
5083 EXPORT_SYMBOL(ipmi_free_recv_msg);
5085 static atomic_t panic_done_count = ATOMIC_INIT(0);
5087 static void dummy_smi_done_handler(struct ipmi_smi_msg *msg)
5089 atomic_dec(&panic_done_count);
5092 static void dummy_recv_done_handler(struct ipmi_recv_msg *msg)
5094 atomic_dec(&panic_done_count);
5098 * Inside a panic, send a message and wait for a response.
5100 static void ipmi_panic_request_and_wait(struct ipmi_smi *intf,
5101 struct ipmi_addr *addr,
5102 struct kernel_ipmi_msg *msg)
5104 struct ipmi_smi_msg smi_msg;
5105 struct ipmi_recv_msg recv_msg;
5108 smi_msg.done = dummy_smi_done_handler;
5109 recv_msg.done = dummy_recv_done_handler;
5110 atomic_add(2, &panic_done_count);
5111 rv = i_ipmi_request(NULL,
5120 intf->addrinfo[0].address,
5121 intf->addrinfo[0].lun,
5122 0, 1); /* Don't retry, and don't wait. */
5124 atomic_sub(2, &panic_done_count);
5125 else if (intf->handlers->flush_messages)
5126 intf->handlers->flush_messages(intf->send_info);
5128 while (atomic_read(&panic_done_count) != 0)
5132 static void event_receiver_fetcher(struct ipmi_smi *intf,
5133 struct ipmi_recv_msg *msg)
5135 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
5136 && (msg->msg.netfn == IPMI_NETFN_SENSOR_EVENT_RESPONSE)
5137 && (msg->msg.cmd == IPMI_GET_EVENT_RECEIVER_CMD)
5138 && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) {
5139 /* A get event receiver command, save it. */
5140 intf->event_receiver = msg->msg.data[1];
5141 intf->event_receiver_lun = msg->msg.data[2] & 0x3;
5145 static void device_id_fetcher(struct ipmi_smi *intf, struct ipmi_recv_msg *msg)
5147 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
5148 && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
5149 && (msg->msg.cmd == IPMI_GET_DEVICE_ID_CMD)
5150 && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) {
5152 * A get device id command, save if we are an event
5153 * receiver or generator.
5155 intf->local_sel_device = (msg->msg.data[6] >> 2) & 1;
5156 intf->local_event_generator = (msg->msg.data[6] >> 5) & 1;
5160 static void send_panic_events(struct ipmi_smi *intf, char *str)
5162 struct kernel_ipmi_msg msg;
5163 unsigned char data[16];
5164 struct ipmi_system_interface_addr *si;
5165 struct ipmi_addr addr;
5167 struct ipmi_ipmb_addr *ipmb;
5170 if (ipmi_send_panic_event == IPMI_SEND_PANIC_EVENT_NONE)
5173 si = (struct ipmi_system_interface_addr *) &addr;
5174 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
5175 si->channel = IPMI_BMC_CHANNEL;
5178 /* Fill in an event telling that we have failed. */
5179 msg.netfn = 0x04; /* Sensor or Event. */
5180 msg.cmd = 2; /* Platform event command. */
5183 data[0] = 0x41; /* Kernel generator ID, IPMI table 5-4 */
5184 data[1] = 0x03; /* This is for IPMI 1.0. */
5185 data[2] = 0x20; /* OS Critical Stop, IPMI table 36-3 */
5186 data[4] = 0x6f; /* Sensor specific, IPMI table 36-1 */
5187 data[5] = 0xa1; /* Runtime stop OEM bytes 2 & 3. */
5190 * Put a few breadcrumbs in. Hopefully later we can add more things
5191 * to make the panic events more useful.
5199 /* Send the event announcing the panic. */
5200 ipmi_panic_request_and_wait(intf, &addr, &msg);
5203 * On every interface, dump a bunch of OEM event holding the
5206 if (ipmi_send_panic_event != IPMI_SEND_PANIC_EVENT_STRING || !str)
5210 * intf_num is used as an marker to tell if the
5211 * interface is valid. Thus we need a read barrier to
5212 * make sure data fetched before checking intf_num
5218 * First job here is to figure out where to send the
5219 * OEM events. There's no way in IPMI to send OEM
5220 * events using an event send command, so we have to
5221 * find the SEL to put them in and stick them in
5225 /* Get capabilities from the get device id. */
5226 intf->local_sel_device = 0;
5227 intf->local_event_generator = 0;
5228 intf->event_receiver = 0;
5230 /* Request the device info from the local MC. */
5231 msg.netfn = IPMI_NETFN_APP_REQUEST;
5232 msg.cmd = IPMI_GET_DEVICE_ID_CMD;
5235 intf->null_user_handler = device_id_fetcher;
5236 ipmi_panic_request_and_wait(intf, &addr, &msg);
5238 if (intf->local_event_generator) {
5239 /* Request the event receiver from the local MC. */
5240 msg.netfn = IPMI_NETFN_SENSOR_EVENT_REQUEST;
5241 msg.cmd = IPMI_GET_EVENT_RECEIVER_CMD;
5244 intf->null_user_handler = event_receiver_fetcher;
5245 ipmi_panic_request_and_wait(intf, &addr, &msg);
5247 intf->null_user_handler = NULL;
5250 * Validate the event receiver. The low bit must not
5251 * be 1 (it must be a valid IPMB address), it cannot
5252 * be zero, and it must not be my address.
5254 if (((intf->event_receiver & 1) == 0)
5255 && (intf->event_receiver != 0)
5256 && (intf->event_receiver != intf->addrinfo[0].address)) {
5258 * The event receiver is valid, send an IPMB
5261 ipmb = (struct ipmi_ipmb_addr *) &addr;
5262 ipmb->addr_type = IPMI_IPMB_ADDR_TYPE;
5263 ipmb->channel = 0; /* FIXME - is this right? */
5264 ipmb->lun = intf->event_receiver_lun;
5265 ipmb->slave_addr = intf->event_receiver;
5266 } else if (intf->local_sel_device) {
5268 * The event receiver was not valid (or was
5269 * me), but I am an SEL device, just dump it
5272 si = (struct ipmi_system_interface_addr *) &addr;
5273 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
5274 si->channel = IPMI_BMC_CHANNEL;
5277 return; /* No where to send the event. */
5279 msg.netfn = IPMI_NETFN_STORAGE_REQUEST; /* Storage. */
5280 msg.cmd = IPMI_ADD_SEL_ENTRY_CMD;
5286 int size = strlen(p);
5292 data[2] = 0xf0; /* OEM event without timestamp. */
5293 data[3] = intf->addrinfo[0].address;
5294 data[4] = j++; /* sequence # */
5296 * Always give 11 bytes, so strncpy will fill
5297 * it with zeroes for me.
5299 strncpy(data+5, p, 11);
5302 ipmi_panic_request_and_wait(intf, &addr, &msg);
5306 static int has_panicked;
5308 static int panic_event(struct notifier_block *this,
5309 unsigned long event,
5312 struct ipmi_smi *intf;
5313 struct ipmi_user *user;
5319 /* For every registered interface, set it to run to completion. */
5320 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
5321 if (!intf->handlers || intf->intf_num == -1)
5322 /* Interface is not ready. */
5325 if (!intf->handlers->poll)
5329 * If we were interrupted while locking xmit_msgs_lock or
5330 * waiting_rcv_msgs_lock, the corresponding list may be
5331 * corrupted. In this case, drop items on the list for
5334 if (!spin_trylock(&intf->xmit_msgs_lock)) {
5335 INIT_LIST_HEAD(&intf->xmit_msgs);
5336 INIT_LIST_HEAD(&intf->hp_xmit_msgs);
5338 spin_unlock(&intf->xmit_msgs_lock);
5340 if (!spin_trylock(&intf->waiting_rcv_msgs_lock))
5341 INIT_LIST_HEAD(&intf->waiting_rcv_msgs);
5343 spin_unlock(&intf->waiting_rcv_msgs_lock);
5345 intf->run_to_completion = 1;
5346 if (intf->handlers->set_run_to_completion)
5347 intf->handlers->set_run_to_completion(intf->send_info,
5350 list_for_each_entry_rcu(user, &intf->users, link) {
5351 if (user->handler->ipmi_panic_handler)
5352 user->handler->ipmi_panic_handler(
5353 user->handler_data);
5356 send_panic_events(intf, ptr);
5362 /* Must be called with ipmi_interfaces_mutex held. */
5363 static int ipmi_register_driver(void)
5370 rv = driver_register(&ipmidriver.driver);
5372 pr_err("Could not register IPMI driver\n");
5374 drvregistered = true;
5378 static struct notifier_block panic_block = {
5379 .notifier_call = panic_event,
5381 .priority = 200 /* priority: INT_MAX >= x >= 0 */
5384 static int ipmi_init_msghandler(void)
5388 mutex_lock(&ipmi_interfaces_mutex);
5389 rv = ipmi_register_driver();
5395 rv = init_srcu_struct(&ipmi_interfaces_srcu);
5399 remove_work_wq = create_singlethread_workqueue("ipmi-msghandler-remove-wq");
5400 if (!remove_work_wq) {
5401 pr_err("unable to create ipmi-msghandler-remove-wq workqueue");
5406 timer_setup(&ipmi_timer, ipmi_timeout, 0);
5407 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
5409 atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
5415 cleanup_srcu_struct(&ipmi_interfaces_srcu);
5417 mutex_unlock(&ipmi_interfaces_mutex);
5421 static int __init ipmi_init_msghandler_mod(void)
5425 pr_info("version " IPMI_DRIVER_VERSION "\n");
5427 mutex_lock(&ipmi_interfaces_mutex);
5428 rv = ipmi_register_driver();
5429 mutex_unlock(&ipmi_interfaces_mutex);
5434 static void __exit cleanup_ipmi(void)
5439 destroy_workqueue(remove_work_wq);
5441 atomic_notifier_chain_unregister(&panic_notifier_list,
5445 * This can't be called if any interfaces exist, so no worry
5446 * about shutting down the interfaces.
5450 * Tell the timer to stop, then wait for it to stop. This
5451 * avoids problems with race conditions removing the timer
5454 atomic_set(&stop_operation, 1);
5455 del_timer_sync(&ipmi_timer);
5457 initialized = false;
5459 /* Check for buffer leaks. */
5460 count = atomic_read(&smi_msg_inuse_count);
5462 pr_warn("SMI message count %d at exit\n", count);
5463 count = atomic_read(&recv_msg_inuse_count);
5465 pr_warn("recv message count %d at exit\n", count);
5467 cleanup_srcu_struct(&ipmi_interfaces_srcu);
5470 driver_unregister(&ipmidriver.driver);
5472 module_exit(cleanup_ipmi);
5474 module_init(ipmi_init_msghandler_mod);
5475 MODULE_LICENSE("GPL");
5476 MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
5477 MODULE_DESCRIPTION("Incoming and outgoing message routing for an IPMI interface.");
5478 MODULE_VERSION(IPMI_DRIVER_VERSION);
5479 MODULE_SOFTDEP("post: ipmi_devintf");