1 // SPDX-License-Identifier: GPL-2.0+
5 * Incoming and outgoing message routing for an IPMI interface.
7 * Author: MontaVista Software, Inc.
8 * Corey Minyard <minyard@mvista.com>
11 * Copyright 2002 MontaVista Software Inc.
14 #define pr_fmt(fmt) "%s" fmt, "IPMI message handler: "
15 #define dev_fmt pr_fmt
17 #include <linux/module.h>
18 #include <linux/errno.h>
19 #include <linux/poll.h>
20 #include <linux/sched.h>
21 #include <linux/seq_file.h>
22 #include <linux/spinlock.h>
23 #include <linux/mutex.h>
24 #include <linux/slab.h>
25 #include <linux/ipmi.h>
26 #include <linux/ipmi_smi.h>
27 #include <linux/notifier.h>
28 #include <linux/init.h>
29 #include <linux/proc_fs.h>
30 #include <linux/rcupdate.h>
31 #include <linux/interrupt.h>
32 #include <linux/moduleparam.h>
33 #include <linux/workqueue.h>
34 #include <linux/uuid.h>
35 #include <linux/nospec.h>
37 #define IPMI_DRIVER_VERSION "39.2"
39 static struct ipmi_recv_msg *ipmi_alloc_recv_msg(void);
40 static int ipmi_init_msghandler(void);
41 static void smi_recv_tasklet(unsigned long);
42 static void handle_new_recv_msgs(struct ipmi_smi *intf);
43 static void need_waiter(struct ipmi_smi *intf);
44 static int handle_one_recv_msg(struct ipmi_smi *intf,
45 struct ipmi_smi_msg *msg);
48 static void ipmi_debug_msg(const char *title, unsigned char *data,
54 pos = snprintf(buf, sizeof(buf), "%s: ", title);
55 for (i = 0; i < len; i++)
56 pos += snprintf(buf + pos, sizeof(buf) - pos,
58 pr_debug("%s\n", buf);
61 static void ipmi_debug_msg(const char *title, unsigned char *data,
66 static bool initialized;
67 static bool drvregistered;
69 enum ipmi_panic_event_op {
70 IPMI_SEND_PANIC_EVENT_NONE,
71 IPMI_SEND_PANIC_EVENT,
72 IPMI_SEND_PANIC_EVENT_STRING
74 #ifdef CONFIG_IPMI_PANIC_STRING
75 #define IPMI_PANIC_DEFAULT IPMI_SEND_PANIC_EVENT_STRING
76 #elif defined(CONFIG_IPMI_PANIC_EVENT)
77 #define IPMI_PANIC_DEFAULT IPMI_SEND_PANIC_EVENT
79 #define IPMI_PANIC_DEFAULT IPMI_SEND_PANIC_EVENT_NONE
81 static enum ipmi_panic_event_op ipmi_send_panic_event = IPMI_PANIC_DEFAULT;
83 static int panic_op_write_handler(const char *val,
84 const struct kernel_param *kp)
89 strncpy(valcp, val, 15);
94 if (strcmp(s, "none") == 0)
95 ipmi_send_panic_event = IPMI_SEND_PANIC_EVENT_NONE;
96 else if (strcmp(s, "event") == 0)
97 ipmi_send_panic_event = IPMI_SEND_PANIC_EVENT;
98 else if (strcmp(s, "string") == 0)
99 ipmi_send_panic_event = IPMI_SEND_PANIC_EVENT_STRING;
106 static int panic_op_read_handler(char *buffer, const struct kernel_param *kp)
108 switch (ipmi_send_panic_event) {
109 case IPMI_SEND_PANIC_EVENT_NONE:
110 strcpy(buffer, "none");
113 case IPMI_SEND_PANIC_EVENT:
114 strcpy(buffer, "event");
117 case IPMI_SEND_PANIC_EVENT_STRING:
118 strcpy(buffer, "string");
122 strcpy(buffer, "???");
126 return strlen(buffer);
129 static const struct kernel_param_ops panic_op_ops = {
130 .set = panic_op_write_handler,
131 .get = panic_op_read_handler
133 module_param_cb(panic_op, &panic_op_ops, NULL, 0600);
134 MODULE_PARM_DESC(panic_op, "Sets if the IPMI driver will attempt to store panic information in the event log in the event of a panic. Set to 'none' for no, 'event' for a single event, or 'string' for a generic event and the panic string in IPMI OEM events.");
137 #define MAX_EVENTS_IN_QUEUE 25
139 /* Remain in auto-maintenance mode for this amount of time (in ms). */
140 static unsigned long maintenance_mode_timeout_ms = 30000;
141 module_param(maintenance_mode_timeout_ms, ulong, 0644);
142 MODULE_PARM_DESC(maintenance_mode_timeout_ms,
143 "The time (milliseconds) after the last maintenance message that the connection stays in maintenance mode.");
146 * Don't let a message sit in a queue forever, always time it with at lest
147 * the max message timer. This is in milliseconds.
149 #define MAX_MSG_TIMEOUT 60000
152 * Timeout times below are in milliseconds, and are done off a 1
153 * second timer. So setting the value to 1000 would mean anything
154 * between 0 and 1000ms. So really the only reasonable minimum
155 * setting it 2000ms, which is between 1 and 2 seconds.
158 /* The default timeout for message retries. */
159 static unsigned long default_retry_ms = 2000;
160 module_param(default_retry_ms, ulong, 0644);
161 MODULE_PARM_DESC(default_retry_ms,
162 "The time (milliseconds) between retry sends");
164 /* The default timeout for maintenance mode message retries. */
165 static unsigned long default_maintenance_retry_ms = 3000;
166 module_param(default_maintenance_retry_ms, ulong, 0644);
167 MODULE_PARM_DESC(default_maintenance_retry_ms,
168 "The time (milliseconds) between retry sends in maintenance mode");
170 /* The default maximum number of retries */
171 static unsigned int default_max_retries = 4;
172 module_param(default_max_retries, uint, 0644);
173 MODULE_PARM_DESC(default_max_retries,
174 "The time (milliseconds) between retry sends in maintenance mode");
176 /* Call every ~1000 ms. */
177 #define IPMI_TIMEOUT_TIME 1000
179 /* How many jiffies does it take to get to the timeout time. */
180 #define IPMI_TIMEOUT_JIFFIES ((IPMI_TIMEOUT_TIME * HZ) / 1000)
183 * Request events from the queue every second (this is the number of
184 * IPMI_TIMEOUT_TIMES between event requests). Hopefully, in the
185 * future, IPMI will add a way to know immediately if an event is in
186 * the queue and this silliness can go away.
188 #define IPMI_REQUEST_EV_TIME (1000 / (IPMI_TIMEOUT_TIME))
190 /* How long should we cache dynamic device IDs? */
191 #define IPMI_DYN_DEV_ID_EXPIRY (10 * HZ)
194 * The main "user" data structure.
197 struct list_head link;
200 * Set to NULL when the user is destroyed, a pointer to myself
201 * so srcu_dereference can be used on it.
203 struct ipmi_user *self;
204 struct srcu_struct release_barrier;
206 struct kref refcount;
208 /* The upper layer that handles receive messages. */
209 const struct ipmi_user_hndl *handler;
212 /* The interface this user is bound to. */
213 struct ipmi_smi *intf;
215 /* Does this interface receive IPMI events? */
219 static struct ipmi_user *acquire_ipmi_user(struct ipmi_user *user, int *index)
220 __acquires(user->release_barrier)
222 struct ipmi_user *ruser;
224 *index = srcu_read_lock(&user->release_barrier);
225 ruser = srcu_dereference(user->self, &user->release_barrier);
227 srcu_read_unlock(&user->release_barrier, *index);
231 static void release_ipmi_user(struct ipmi_user *user, int index)
233 srcu_read_unlock(&user->release_barrier, index);
237 struct list_head link;
239 struct ipmi_user *user;
245 * This is used to form a linked lised during mass deletion.
246 * Since this is in an RCU list, we cannot use the link above
247 * or change any data until the RCU period completes. So we
248 * use this next variable during mass deletion so we can have
249 * a list and don't have to wait and restart the search on
250 * every individual deletion of a command.
252 struct cmd_rcvr *next;
256 unsigned int inuse : 1;
257 unsigned int broadcast : 1;
259 unsigned long timeout;
260 unsigned long orig_timeout;
261 unsigned int retries_left;
264 * To verify on an incoming send message response that this is
265 * the message that the response is for, we keep a sequence id
266 * and increment it every time we send a message.
271 * This is held so we can properly respond to the message on a
272 * timeout, and it is used to hold the temporary data for
273 * retransmission, too.
275 struct ipmi_recv_msg *recv_msg;
279 * Store the information in a msgid (long) to allow us to find a
280 * sequence table entry from the msgid.
282 #define STORE_SEQ_IN_MSGID(seq, seqid) \
283 ((((seq) & 0x3f) << 26) | ((seqid) & 0x3ffffff))
285 #define GET_SEQ_FROM_MSGID(msgid, seq, seqid) \
287 seq = (((msgid) >> 26) & 0x3f); \
288 seqid = ((msgid) & 0x3ffffff); \
291 #define NEXT_SEQID(seqid) (((seqid) + 1) & 0x3ffffff)
293 #define IPMI_MAX_CHANNELS 16
294 struct ipmi_channel {
295 unsigned char medium;
296 unsigned char protocol;
299 struct ipmi_channel_set {
300 struct ipmi_channel c[IPMI_MAX_CHANNELS];
303 struct ipmi_my_addrinfo {
305 * My slave address. This is initialized to IPMI_BMC_SLAVE_ADDR,
306 * but may be changed by the user.
308 unsigned char address;
311 * My LUN. This should generally stay the SMS LUN, but just in
318 * Note that the product id, manufacturer id, guid, and device id are
319 * immutable in this structure, so dyn_mutex is not required for
320 * accessing those. If those change on a BMC, a new BMC is allocated.
323 struct platform_device pdev;
324 struct list_head intfs; /* Interfaces on this BMC. */
325 struct ipmi_device_id id;
326 struct ipmi_device_id fetch_id;
328 unsigned long dyn_id_expiry;
329 struct mutex dyn_mutex; /* Protects id, intfs, & dyn* */
333 struct kref usecount;
334 struct work_struct remove_work;
336 #define to_bmc_device(x) container_of((x), struct bmc_device, pdev.dev)
338 static int bmc_get_device_id(struct ipmi_smi *intf, struct bmc_device *bmc,
339 struct ipmi_device_id *id,
340 bool *guid_set, guid_t *guid);
343 * Various statistics for IPMI, these index stats[] in the ipmi_smi
346 enum ipmi_stat_indexes {
347 /* Commands we got from the user that were invalid. */
348 IPMI_STAT_sent_invalid_commands = 0,
350 /* Commands we sent to the MC. */
351 IPMI_STAT_sent_local_commands,
353 /* Responses from the MC that were delivered to a user. */
354 IPMI_STAT_handled_local_responses,
356 /* Responses from the MC that were not delivered to a user. */
357 IPMI_STAT_unhandled_local_responses,
359 /* Commands we sent out to the IPMB bus. */
360 IPMI_STAT_sent_ipmb_commands,
362 /* Commands sent on the IPMB that had errors on the SEND CMD */
363 IPMI_STAT_sent_ipmb_command_errs,
365 /* Each retransmit increments this count. */
366 IPMI_STAT_retransmitted_ipmb_commands,
369 * When a message times out (runs out of retransmits) this is
372 IPMI_STAT_timed_out_ipmb_commands,
375 * This is like above, but for broadcasts. Broadcasts are
376 * *not* included in the above count (they are expected to
379 IPMI_STAT_timed_out_ipmb_broadcasts,
381 /* Responses I have sent to the IPMB bus. */
382 IPMI_STAT_sent_ipmb_responses,
384 /* The response was delivered to the user. */
385 IPMI_STAT_handled_ipmb_responses,
387 /* The response had invalid data in it. */
388 IPMI_STAT_invalid_ipmb_responses,
390 /* The response didn't have anyone waiting for it. */
391 IPMI_STAT_unhandled_ipmb_responses,
393 /* Commands we sent out to the IPMB bus. */
394 IPMI_STAT_sent_lan_commands,
396 /* Commands sent on the IPMB that had errors on the SEND CMD */
397 IPMI_STAT_sent_lan_command_errs,
399 /* Each retransmit increments this count. */
400 IPMI_STAT_retransmitted_lan_commands,
403 * When a message times out (runs out of retransmits) this is
406 IPMI_STAT_timed_out_lan_commands,
408 /* Responses I have sent to the IPMB bus. */
409 IPMI_STAT_sent_lan_responses,
411 /* The response was delivered to the user. */
412 IPMI_STAT_handled_lan_responses,
414 /* The response had invalid data in it. */
415 IPMI_STAT_invalid_lan_responses,
417 /* The response didn't have anyone waiting for it. */
418 IPMI_STAT_unhandled_lan_responses,
420 /* The command was delivered to the user. */
421 IPMI_STAT_handled_commands,
423 /* The command had invalid data in it. */
424 IPMI_STAT_invalid_commands,
426 /* The command didn't have anyone waiting for it. */
427 IPMI_STAT_unhandled_commands,
429 /* Invalid data in an event. */
430 IPMI_STAT_invalid_events,
432 /* Events that were received with the proper format. */
435 /* Retransmissions on IPMB that failed. */
436 IPMI_STAT_dropped_rexmit_ipmb_commands,
438 /* Retransmissions on LAN that failed. */
439 IPMI_STAT_dropped_rexmit_lan_commands,
441 /* This *must* remain last, add new values above this. */
446 #define IPMI_IPMB_NUM_SEQ 64
448 /* What interface number are we? */
451 struct kref refcount;
453 /* Set when the interface is being unregistered. */
456 /* Used for a list of interfaces. */
457 struct list_head link;
460 * The list of upper layers that are using me. seq_lock write
461 * protects this. Read protection is with srcu.
463 struct list_head users;
464 struct srcu_struct users_srcu;
466 /* Used for wake ups at startup. */
467 wait_queue_head_t waitq;
470 * Prevents the interface from being unregistered when the
471 * interface is used by being looked up through the BMC
474 struct mutex bmc_reg_mutex;
476 struct bmc_device tmp_bmc;
477 struct bmc_device *bmc;
479 struct list_head bmc_link;
481 bool in_bmc_register; /* Handle recursive situations. Yuck. */
482 struct work_struct bmc_reg_work;
484 const struct ipmi_smi_handlers *handlers;
487 /* Driver-model device for the system interface. */
488 struct device *si_dev;
491 * A table of sequence numbers for this interface. We use the
492 * sequence numbers for IPMB messages that go out of the
493 * interface to match them up with their responses. A routine
494 * is called periodically to time the items in this list.
497 struct seq_table seq_table[IPMI_IPMB_NUM_SEQ];
501 * Messages queued for delivery. If delivery fails (out of memory
502 * for instance), They will stay in here to be processed later in a
503 * periodic timer interrupt. The tasklet is for handling received
504 * messages directly from the handler.
506 spinlock_t waiting_rcv_msgs_lock;
507 struct list_head waiting_rcv_msgs;
508 atomic_t watchdog_pretimeouts_to_deliver;
509 struct tasklet_struct recv_tasklet;
511 spinlock_t xmit_msgs_lock;
512 struct list_head xmit_msgs;
513 struct ipmi_smi_msg *curr_msg;
514 struct list_head hp_xmit_msgs;
517 * The list of command receivers that are registered for commands
520 struct mutex cmd_rcvrs_mutex;
521 struct list_head cmd_rcvrs;
524 * Events that were queues because no one was there to receive
527 spinlock_t events_lock; /* For dealing with event stuff. */
528 struct list_head waiting_events;
529 unsigned int waiting_events_count; /* How many events in queue? */
530 char delivering_events;
531 char event_msg_printed;
532 atomic_t event_waiters;
533 unsigned int ticks_to_req_ev;
534 int last_needs_timer;
537 * The event receiver for my BMC, only really used at panic
538 * shutdown as a place to store this.
540 unsigned char event_receiver;
541 unsigned char event_receiver_lun;
542 unsigned char local_sel_device;
543 unsigned char local_event_generator;
545 /* For handling of maintenance mode. */
546 int maintenance_mode;
547 bool maintenance_mode_enable;
548 int auto_maintenance_timeout;
549 spinlock_t maintenance_mode_lock; /* Used in a timer... */
552 * If we are doing maintenance on something on IPMB, extend
553 * the timeout time to avoid timeouts writing firmware and
556 int ipmb_maintenance_mode_timeout;
559 * A cheap hack, if this is non-null and a message to an
560 * interface comes in with a NULL user, call this routine with
561 * it. Note that the message will still be freed by the
562 * caller. This only works on the system interface.
564 * Protected by bmc_reg_mutex.
566 void (*null_user_handler)(struct ipmi_smi *intf,
567 struct ipmi_recv_msg *msg);
570 * When we are scanning the channels for an SMI, this will
571 * tell which channel we are scanning.
575 /* Channel information */
576 struct ipmi_channel_set *channel_list;
577 unsigned int curr_working_cset; /* First index into the following. */
578 struct ipmi_channel_set wchannels[2];
579 struct ipmi_my_addrinfo addrinfo[IPMI_MAX_CHANNELS];
582 atomic_t stats[IPMI_NUM_STATS];
585 * run_to_completion duplicate of smb_info, smi_info
586 * and ipmi_serial_info structures. Used to decrease numbers of
587 * parameters passed by "low" level IPMI code.
589 int run_to_completion;
591 #define to_si_intf_from_dev(device) container_of(device, struct ipmi_smi, dev)
593 static void __get_guid(struct ipmi_smi *intf);
594 static void __ipmi_bmc_unregister(struct ipmi_smi *intf);
595 static int __ipmi_bmc_register(struct ipmi_smi *intf,
596 struct ipmi_device_id *id,
597 bool guid_set, guid_t *guid, int intf_num);
598 static int __scan_channels(struct ipmi_smi *intf, struct ipmi_device_id *id);
602 * The driver model view of the IPMI messaging driver.
604 static struct platform_driver ipmidriver = {
607 .bus = &platform_bus_type
611 * This mutex keeps us from adding the same BMC twice.
613 static DEFINE_MUTEX(ipmidriver_mutex);
615 static LIST_HEAD(ipmi_interfaces);
616 static DEFINE_MUTEX(ipmi_interfaces_mutex);
617 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 void free_recv_msg_list(struct list_head *q)
660 struct ipmi_recv_msg *msg, *msg2;
662 list_for_each_entry_safe(msg, msg2, q, link) {
663 list_del(&msg->link);
664 ipmi_free_recv_msg(msg);
668 static void free_smi_msg_list(struct list_head *q)
670 struct ipmi_smi_msg *msg, *msg2;
672 list_for_each_entry_safe(msg, msg2, q, link) {
673 list_del(&msg->link);
674 ipmi_free_smi_msg(msg);
678 static void clean_up_interface_data(struct ipmi_smi *intf)
681 struct cmd_rcvr *rcvr, *rcvr2;
682 struct list_head list;
684 tasklet_kill(&intf->recv_tasklet);
686 free_smi_msg_list(&intf->waiting_rcv_msgs);
687 free_recv_msg_list(&intf->waiting_events);
690 * Wholesale remove all the entries from the list in the
691 * interface and wait for RCU to know that none are in use.
693 mutex_lock(&intf->cmd_rcvrs_mutex);
694 INIT_LIST_HEAD(&list);
695 list_splice_init_rcu(&intf->cmd_rcvrs, &list, synchronize_rcu);
696 mutex_unlock(&intf->cmd_rcvrs_mutex);
698 list_for_each_entry_safe(rcvr, rcvr2, &list, link)
701 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
702 if ((intf->seq_table[i].inuse)
703 && (intf->seq_table[i].recv_msg))
704 ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
708 static void intf_free(struct kref *ref)
710 struct ipmi_smi *intf = container_of(ref, struct ipmi_smi, refcount);
712 clean_up_interface_data(intf);
716 struct watcher_entry {
718 struct ipmi_smi *intf;
719 struct list_head link;
722 int ipmi_smi_watcher_register(struct ipmi_smi_watcher *watcher)
724 struct ipmi_smi *intf;
728 * Make sure the driver is actually initialized, this handles
729 * problems with initialization order.
731 rv = ipmi_init_msghandler();
735 mutex_lock(&smi_watchers_mutex);
737 list_add(&watcher->link, &smi_watchers);
739 index = srcu_read_lock(&ipmi_interfaces_srcu);
740 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
741 int intf_num = READ_ONCE(intf->intf_num);
745 watcher->new_smi(intf_num, intf->si_dev);
747 srcu_read_unlock(&ipmi_interfaces_srcu, index);
749 mutex_unlock(&smi_watchers_mutex);
753 EXPORT_SYMBOL(ipmi_smi_watcher_register);
755 int ipmi_smi_watcher_unregister(struct ipmi_smi_watcher *watcher)
757 mutex_lock(&smi_watchers_mutex);
758 list_del(&watcher->link);
759 mutex_unlock(&smi_watchers_mutex);
762 EXPORT_SYMBOL(ipmi_smi_watcher_unregister);
765 * Must be called with smi_watchers_mutex held.
768 call_smi_watchers(int i, struct device *dev)
770 struct ipmi_smi_watcher *w;
772 mutex_lock(&smi_watchers_mutex);
773 list_for_each_entry(w, &smi_watchers, link) {
774 if (try_module_get(w->owner)) {
776 module_put(w->owner);
779 mutex_unlock(&smi_watchers_mutex);
783 ipmi_addr_equal(struct ipmi_addr *addr1, struct ipmi_addr *addr2)
785 if (addr1->addr_type != addr2->addr_type)
788 if (addr1->channel != addr2->channel)
791 if (addr1->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
792 struct ipmi_system_interface_addr *smi_addr1
793 = (struct ipmi_system_interface_addr *) addr1;
794 struct ipmi_system_interface_addr *smi_addr2
795 = (struct ipmi_system_interface_addr *) addr2;
796 return (smi_addr1->lun == smi_addr2->lun);
799 if (is_ipmb_addr(addr1) || is_ipmb_bcast_addr(addr1)) {
800 struct ipmi_ipmb_addr *ipmb_addr1
801 = (struct ipmi_ipmb_addr *) addr1;
802 struct ipmi_ipmb_addr *ipmb_addr2
803 = (struct ipmi_ipmb_addr *) addr2;
805 return ((ipmb_addr1->slave_addr == ipmb_addr2->slave_addr)
806 && (ipmb_addr1->lun == ipmb_addr2->lun));
809 if (is_lan_addr(addr1)) {
810 struct ipmi_lan_addr *lan_addr1
811 = (struct ipmi_lan_addr *) addr1;
812 struct ipmi_lan_addr *lan_addr2
813 = (struct ipmi_lan_addr *) addr2;
815 return ((lan_addr1->remote_SWID == lan_addr2->remote_SWID)
816 && (lan_addr1->local_SWID == lan_addr2->local_SWID)
817 && (lan_addr1->session_handle
818 == lan_addr2->session_handle)
819 && (lan_addr1->lun == lan_addr2->lun));
825 int ipmi_validate_addr(struct ipmi_addr *addr, int len)
827 if (len < sizeof(struct ipmi_system_interface_addr))
830 if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
831 if (addr->channel != IPMI_BMC_CHANNEL)
836 if ((addr->channel == IPMI_BMC_CHANNEL)
837 || (addr->channel >= IPMI_MAX_CHANNELS)
838 || (addr->channel < 0))
841 if (is_ipmb_addr(addr) || is_ipmb_bcast_addr(addr)) {
842 if (len < sizeof(struct ipmi_ipmb_addr))
847 if (is_lan_addr(addr)) {
848 if (len < sizeof(struct ipmi_lan_addr))
855 EXPORT_SYMBOL(ipmi_validate_addr);
857 unsigned int ipmi_addr_length(int addr_type)
859 if (addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
860 return sizeof(struct ipmi_system_interface_addr);
862 if ((addr_type == IPMI_IPMB_ADDR_TYPE)
863 || (addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE))
864 return sizeof(struct ipmi_ipmb_addr);
866 if (addr_type == IPMI_LAN_ADDR_TYPE)
867 return sizeof(struct ipmi_lan_addr);
871 EXPORT_SYMBOL(ipmi_addr_length);
873 static int deliver_response(struct ipmi_smi *intf, struct ipmi_recv_msg *msg)
878 /* Special handling for NULL users. */
879 if (intf->null_user_handler) {
880 intf->null_user_handler(intf, msg);
882 /* No handler, so give up. */
885 ipmi_free_recv_msg(msg);
886 } else if (!oops_in_progress) {
888 * If we are running in the panic context, calling the
889 * receive handler doesn't much meaning and has a deadlock
890 * risk. At this moment, simply skip it in that case.
893 struct ipmi_user *user = acquire_ipmi_user(msg->user, &index);
896 user->handler->ipmi_recv_hndl(msg, user->handler_data);
897 release_ipmi_user(user, index);
899 /* User went away, give up. */
900 ipmi_free_recv_msg(msg);
908 static void deliver_local_response(struct ipmi_smi *intf,
909 struct ipmi_recv_msg *msg)
911 if (deliver_response(intf, msg))
912 ipmi_inc_stat(intf, unhandled_local_responses);
914 ipmi_inc_stat(intf, handled_local_responses);
917 static void deliver_err_response(struct ipmi_smi *intf,
918 struct ipmi_recv_msg *msg, int err)
920 msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
921 msg->msg_data[0] = err;
922 msg->msg.netfn |= 1; /* Convert to a response. */
923 msg->msg.data_len = 1;
924 msg->msg.data = msg->msg_data;
925 deliver_local_response(intf, msg);
929 * Find the next sequence number not being used and add the given
930 * message with the given timeout to the sequence table. This must be
931 * called with the interface's seq_lock held.
933 static int intf_next_seq(struct ipmi_smi *intf,
934 struct ipmi_recv_msg *recv_msg,
935 unsigned long timeout,
945 timeout = default_retry_ms;
947 retries = default_max_retries;
949 for (i = intf->curr_seq; (i+1)%IPMI_IPMB_NUM_SEQ != intf->curr_seq;
950 i = (i+1)%IPMI_IPMB_NUM_SEQ) {
951 if (!intf->seq_table[i].inuse)
955 if (!intf->seq_table[i].inuse) {
956 intf->seq_table[i].recv_msg = recv_msg;
959 * Start with the maximum timeout, when the send response
960 * comes in we will start the real timer.
962 intf->seq_table[i].timeout = MAX_MSG_TIMEOUT;
963 intf->seq_table[i].orig_timeout = timeout;
964 intf->seq_table[i].retries_left = retries;
965 intf->seq_table[i].broadcast = broadcast;
966 intf->seq_table[i].inuse = 1;
967 intf->seq_table[i].seqid = NEXT_SEQID(intf->seq_table[i].seqid);
969 *seqid = intf->seq_table[i].seqid;
970 intf->curr_seq = (i+1)%IPMI_IPMB_NUM_SEQ;
980 * Return the receive message for the given sequence number and
981 * release the sequence number so it can be reused. Some other data
982 * is passed in to be sure the message matches up correctly (to help
983 * guard against message coming in after their timeout and the
984 * sequence number being reused).
986 static int intf_find_seq(struct ipmi_smi *intf,
991 struct ipmi_addr *addr,
992 struct ipmi_recv_msg **recv_msg)
997 if (seq >= IPMI_IPMB_NUM_SEQ)
1000 spin_lock_irqsave(&intf->seq_lock, flags);
1001 if (intf->seq_table[seq].inuse) {
1002 struct ipmi_recv_msg *msg = intf->seq_table[seq].recv_msg;
1004 if ((msg->addr.channel == channel) && (msg->msg.cmd == cmd)
1005 && (msg->msg.netfn == netfn)
1006 && (ipmi_addr_equal(addr, &msg->addr))) {
1008 intf->seq_table[seq].inuse = 0;
1012 spin_unlock_irqrestore(&intf->seq_lock, flags);
1018 /* Start the timer for a specific sequence table entry. */
1019 static int intf_start_seq_timer(struct ipmi_smi *intf,
1023 unsigned long flags;
1025 unsigned long seqid;
1028 GET_SEQ_FROM_MSGID(msgid, seq, seqid);
1030 spin_lock_irqsave(&intf->seq_lock, flags);
1032 * We do this verification because the user can be deleted
1033 * while a message is outstanding.
1035 if ((intf->seq_table[seq].inuse)
1036 && (intf->seq_table[seq].seqid == seqid)) {
1037 struct seq_table *ent = &intf->seq_table[seq];
1038 ent->timeout = ent->orig_timeout;
1041 spin_unlock_irqrestore(&intf->seq_lock, flags);
1046 /* Got an error for the send message for a specific sequence number. */
1047 static int intf_err_seq(struct ipmi_smi *intf,
1052 unsigned long flags;
1054 unsigned long seqid;
1055 struct ipmi_recv_msg *msg = NULL;
1058 GET_SEQ_FROM_MSGID(msgid, seq, seqid);
1060 spin_lock_irqsave(&intf->seq_lock, flags);
1062 * We do this verification because the user can be deleted
1063 * while a message is outstanding.
1065 if ((intf->seq_table[seq].inuse)
1066 && (intf->seq_table[seq].seqid == seqid)) {
1067 struct seq_table *ent = &intf->seq_table[seq];
1070 msg = ent->recv_msg;
1073 spin_unlock_irqrestore(&intf->seq_lock, flags);
1076 deliver_err_response(intf, msg, err);
1082 int ipmi_create_user(unsigned int if_num,
1083 const struct ipmi_user_hndl *handler,
1085 struct ipmi_user **user)
1087 unsigned long flags;
1088 struct ipmi_user *new_user;
1090 struct ipmi_smi *intf;
1093 * There is no module usecount here, because it's not
1094 * required. Since this can only be used by and called from
1095 * other modules, they will implicitly use this module, and
1096 * thus this can't be removed unless the other modules are
1100 if (handler == NULL)
1104 * Make sure the driver is actually initialized, this handles
1105 * problems with initialization order.
1107 rv = ipmi_init_msghandler();
1111 new_user = kmalloc(sizeof(*new_user), GFP_KERNEL);
1115 index = srcu_read_lock(&ipmi_interfaces_srcu);
1116 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
1117 if (intf->intf_num == if_num)
1120 /* Not found, return an error */
1125 rv = init_srcu_struct(&new_user->release_barrier);
1129 /* Note that each existing user holds a refcount to the interface. */
1130 kref_get(&intf->refcount);
1132 kref_init(&new_user->refcount);
1133 new_user->handler = handler;
1134 new_user->handler_data = handler_data;
1135 new_user->intf = intf;
1136 new_user->gets_events = false;
1138 rcu_assign_pointer(new_user->self, new_user);
1139 spin_lock_irqsave(&intf->seq_lock, flags);
1140 list_add_rcu(&new_user->link, &intf->users);
1141 spin_unlock_irqrestore(&intf->seq_lock, flags);
1142 if (handler->ipmi_watchdog_pretimeout) {
1143 /* User wants pretimeouts, so make sure to watch for them. */
1144 if (atomic_inc_return(&intf->event_waiters) == 1)
1147 srcu_read_unlock(&ipmi_interfaces_srcu, index);
1152 srcu_read_unlock(&ipmi_interfaces_srcu, index);
1156 EXPORT_SYMBOL(ipmi_create_user);
1158 int ipmi_get_smi_info(int if_num, struct ipmi_smi_info *data)
1161 struct ipmi_smi *intf;
1163 index = srcu_read_lock(&ipmi_interfaces_srcu);
1164 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
1165 if (intf->intf_num == if_num)
1168 srcu_read_unlock(&ipmi_interfaces_srcu, index);
1170 /* Not found, return an error */
1174 if (!intf->handlers->get_smi_info)
1177 rv = intf->handlers->get_smi_info(intf->send_info, data);
1178 srcu_read_unlock(&ipmi_interfaces_srcu, index);
1182 EXPORT_SYMBOL(ipmi_get_smi_info);
1184 static void free_user(struct kref *ref)
1186 struct ipmi_user *user = container_of(ref, struct ipmi_user, refcount);
1187 cleanup_srcu_struct(&user->release_barrier);
1191 static void _ipmi_destroy_user(struct ipmi_user *user)
1193 struct ipmi_smi *intf = user->intf;
1195 unsigned long flags;
1196 struct cmd_rcvr *rcvr;
1197 struct cmd_rcvr *rcvrs = NULL;
1199 if (!acquire_ipmi_user(user, &i)) {
1201 * The user has already been cleaned up, just make sure
1202 * nothing is using it and return.
1204 synchronize_srcu(&user->release_barrier);
1208 rcu_assign_pointer(user->self, NULL);
1209 release_ipmi_user(user, i);
1211 synchronize_srcu(&user->release_barrier);
1213 if (user->handler->shutdown)
1214 user->handler->shutdown(user->handler_data);
1216 if (user->handler->ipmi_watchdog_pretimeout)
1217 atomic_dec(&intf->event_waiters);
1219 if (user->gets_events)
1220 atomic_dec(&intf->event_waiters);
1222 /* Remove the user from the interface's sequence table. */
1223 spin_lock_irqsave(&intf->seq_lock, flags);
1224 list_del_rcu(&user->link);
1226 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
1227 if (intf->seq_table[i].inuse
1228 && (intf->seq_table[i].recv_msg->user == user)) {
1229 intf->seq_table[i].inuse = 0;
1230 ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
1233 spin_unlock_irqrestore(&intf->seq_lock, flags);
1236 * Remove the user from the command receiver's table. First
1237 * we build a list of everything (not using the standard link,
1238 * since other things may be using it till we do
1239 * synchronize_srcu()) then free everything in that list.
1241 mutex_lock(&intf->cmd_rcvrs_mutex);
1242 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
1243 if (rcvr->user == user) {
1244 list_del_rcu(&rcvr->link);
1249 mutex_unlock(&intf->cmd_rcvrs_mutex);
1257 kref_put(&intf->refcount, intf_free);
1260 int ipmi_destroy_user(struct ipmi_user *user)
1262 _ipmi_destroy_user(user);
1264 kref_put(&user->refcount, free_user);
1268 EXPORT_SYMBOL(ipmi_destroy_user);
1270 int ipmi_get_version(struct ipmi_user *user,
1271 unsigned char *major,
1272 unsigned char *minor)
1274 struct ipmi_device_id id;
1277 user = acquire_ipmi_user(user, &index);
1281 rv = bmc_get_device_id(user->intf, NULL, &id, NULL, NULL);
1283 *major = ipmi_version_major(&id);
1284 *minor = ipmi_version_minor(&id);
1286 release_ipmi_user(user, index);
1290 EXPORT_SYMBOL(ipmi_get_version);
1292 int ipmi_set_my_address(struct ipmi_user *user,
1293 unsigned int channel,
1294 unsigned char address)
1298 user = acquire_ipmi_user(user, &index);
1302 if (channel >= IPMI_MAX_CHANNELS) {
1305 channel = array_index_nospec(channel, IPMI_MAX_CHANNELS);
1306 user->intf->addrinfo[channel].address = address;
1308 release_ipmi_user(user, index);
1312 EXPORT_SYMBOL(ipmi_set_my_address);
1314 int ipmi_get_my_address(struct ipmi_user *user,
1315 unsigned int channel,
1316 unsigned char *address)
1320 user = acquire_ipmi_user(user, &index);
1324 if (channel >= IPMI_MAX_CHANNELS) {
1327 channel = array_index_nospec(channel, IPMI_MAX_CHANNELS);
1328 *address = user->intf->addrinfo[channel].address;
1330 release_ipmi_user(user, index);
1334 EXPORT_SYMBOL(ipmi_get_my_address);
1336 int ipmi_set_my_LUN(struct ipmi_user *user,
1337 unsigned int channel,
1342 user = acquire_ipmi_user(user, &index);
1346 if (channel >= IPMI_MAX_CHANNELS) {
1349 channel = array_index_nospec(channel, IPMI_MAX_CHANNELS);
1350 user->intf->addrinfo[channel].lun = LUN & 0x3;
1352 release_ipmi_user(user, index);
1356 EXPORT_SYMBOL(ipmi_set_my_LUN);
1358 int ipmi_get_my_LUN(struct ipmi_user *user,
1359 unsigned int channel,
1360 unsigned char *address)
1364 user = acquire_ipmi_user(user, &index);
1368 if (channel >= IPMI_MAX_CHANNELS) {
1371 channel = array_index_nospec(channel, IPMI_MAX_CHANNELS);
1372 *address = user->intf->addrinfo[channel].lun;
1374 release_ipmi_user(user, index);
1378 EXPORT_SYMBOL(ipmi_get_my_LUN);
1380 int ipmi_get_maintenance_mode(struct ipmi_user *user)
1383 unsigned long flags;
1385 user = acquire_ipmi_user(user, &index);
1389 spin_lock_irqsave(&user->intf->maintenance_mode_lock, flags);
1390 mode = user->intf->maintenance_mode;
1391 spin_unlock_irqrestore(&user->intf->maintenance_mode_lock, flags);
1392 release_ipmi_user(user, index);
1396 EXPORT_SYMBOL(ipmi_get_maintenance_mode);
1398 static void maintenance_mode_update(struct ipmi_smi *intf)
1400 if (intf->handlers->set_maintenance_mode)
1401 intf->handlers->set_maintenance_mode(
1402 intf->send_info, intf->maintenance_mode_enable);
1405 int ipmi_set_maintenance_mode(struct ipmi_user *user, int mode)
1408 unsigned long flags;
1409 struct ipmi_smi *intf = user->intf;
1411 user = acquire_ipmi_user(user, &index);
1415 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
1416 if (intf->maintenance_mode != mode) {
1418 case IPMI_MAINTENANCE_MODE_AUTO:
1419 intf->maintenance_mode_enable
1420 = (intf->auto_maintenance_timeout > 0);
1423 case IPMI_MAINTENANCE_MODE_OFF:
1424 intf->maintenance_mode_enable = false;
1427 case IPMI_MAINTENANCE_MODE_ON:
1428 intf->maintenance_mode_enable = true;
1435 intf->maintenance_mode = mode;
1437 maintenance_mode_update(intf);
1440 spin_unlock_irqrestore(&intf->maintenance_mode_lock, flags);
1441 release_ipmi_user(user, index);
1445 EXPORT_SYMBOL(ipmi_set_maintenance_mode);
1447 int ipmi_set_gets_events(struct ipmi_user *user, bool val)
1449 unsigned long flags;
1450 struct ipmi_smi *intf = user->intf;
1451 struct ipmi_recv_msg *msg, *msg2;
1452 struct list_head msgs;
1455 user = acquire_ipmi_user(user, &index);
1459 INIT_LIST_HEAD(&msgs);
1461 spin_lock_irqsave(&intf->events_lock, flags);
1462 if (user->gets_events == val)
1465 user->gets_events = val;
1468 if (atomic_inc_return(&intf->event_waiters) == 1)
1471 atomic_dec(&intf->event_waiters);
1474 if (intf->delivering_events)
1476 * Another thread is delivering events for this, so
1477 * let it handle any new events.
1481 /* Deliver any queued events. */
1482 while (user->gets_events && !list_empty(&intf->waiting_events)) {
1483 list_for_each_entry_safe(msg, msg2, &intf->waiting_events, link)
1484 list_move_tail(&msg->link, &msgs);
1485 intf->waiting_events_count = 0;
1486 if (intf->event_msg_printed) {
1487 dev_warn(intf->si_dev, "Event queue no longer full\n");
1488 intf->event_msg_printed = 0;
1491 intf->delivering_events = 1;
1492 spin_unlock_irqrestore(&intf->events_lock, flags);
1494 list_for_each_entry_safe(msg, msg2, &msgs, link) {
1496 kref_get(&user->refcount);
1497 deliver_local_response(intf, msg);
1500 spin_lock_irqsave(&intf->events_lock, flags);
1501 intf->delivering_events = 0;
1505 spin_unlock_irqrestore(&intf->events_lock, flags);
1506 release_ipmi_user(user, index);
1510 EXPORT_SYMBOL(ipmi_set_gets_events);
1512 static struct cmd_rcvr *find_cmd_rcvr(struct ipmi_smi *intf,
1513 unsigned char netfn,
1517 struct cmd_rcvr *rcvr;
1519 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
1520 if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)
1521 && (rcvr->chans & (1 << chan)))
1527 static int is_cmd_rcvr_exclusive(struct ipmi_smi *intf,
1528 unsigned char netfn,
1532 struct cmd_rcvr *rcvr;
1534 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
1535 if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)
1536 && (rcvr->chans & chans))
1542 int ipmi_register_for_cmd(struct ipmi_user *user,
1543 unsigned char netfn,
1547 struct ipmi_smi *intf = user->intf;
1548 struct cmd_rcvr *rcvr;
1551 user = acquire_ipmi_user(user, &index);
1555 rcvr = kmalloc(sizeof(*rcvr), GFP_KERNEL);
1561 rcvr->netfn = netfn;
1562 rcvr->chans = chans;
1565 mutex_lock(&intf->cmd_rcvrs_mutex);
1566 /* Make sure the command/netfn is not already registered. */
1567 if (!is_cmd_rcvr_exclusive(intf, netfn, cmd, chans)) {
1572 if (atomic_inc_return(&intf->event_waiters) == 1)
1575 list_add_rcu(&rcvr->link, &intf->cmd_rcvrs);
1578 mutex_unlock(&intf->cmd_rcvrs_mutex);
1582 release_ipmi_user(user, index);
1586 EXPORT_SYMBOL(ipmi_register_for_cmd);
1588 int ipmi_unregister_for_cmd(struct ipmi_user *user,
1589 unsigned char netfn,
1593 struct ipmi_smi *intf = user->intf;
1594 struct cmd_rcvr *rcvr;
1595 struct cmd_rcvr *rcvrs = NULL;
1596 int i, rv = -ENOENT, index;
1598 user = acquire_ipmi_user(user, &index);
1602 mutex_lock(&intf->cmd_rcvrs_mutex);
1603 for (i = 0; i < IPMI_NUM_CHANNELS; i++) {
1604 if (((1 << i) & chans) == 0)
1606 rcvr = find_cmd_rcvr(intf, netfn, cmd, i);
1609 if (rcvr->user == user) {
1611 rcvr->chans &= ~chans;
1612 if (rcvr->chans == 0) {
1613 list_del_rcu(&rcvr->link);
1619 mutex_unlock(&intf->cmd_rcvrs_mutex);
1621 release_ipmi_user(user, index);
1623 atomic_dec(&intf->event_waiters);
1631 EXPORT_SYMBOL(ipmi_unregister_for_cmd);
1633 static unsigned char
1634 ipmb_checksum(unsigned char *data, int size)
1636 unsigned char csum = 0;
1638 for (; size > 0; size--, data++)
1644 static inline void format_ipmb_msg(struct ipmi_smi_msg *smi_msg,
1645 struct kernel_ipmi_msg *msg,
1646 struct ipmi_ipmb_addr *ipmb_addr,
1648 unsigned char ipmb_seq,
1650 unsigned char source_address,
1651 unsigned char source_lun)
1655 /* Format the IPMB header data. */
1656 smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
1657 smi_msg->data[1] = IPMI_SEND_MSG_CMD;
1658 smi_msg->data[2] = ipmb_addr->channel;
1660 smi_msg->data[3] = 0;
1661 smi_msg->data[i+3] = ipmb_addr->slave_addr;
1662 smi_msg->data[i+4] = (msg->netfn << 2) | (ipmb_addr->lun & 0x3);
1663 smi_msg->data[i+5] = ipmb_checksum(&smi_msg->data[i + 3], 2);
1664 smi_msg->data[i+6] = source_address;
1665 smi_msg->data[i+7] = (ipmb_seq << 2) | source_lun;
1666 smi_msg->data[i+8] = msg->cmd;
1668 /* Now tack on the data to the message. */
1669 if (msg->data_len > 0)
1670 memcpy(&smi_msg->data[i + 9], msg->data, msg->data_len);
1671 smi_msg->data_size = msg->data_len + 9;
1673 /* Now calculate the checksum and tack it on. */
1674 smi_msg->data[i+smi_msg->data_size]
1675 = ipmb_checksum(&smi_msg->data[i + 6], smi_msg->data_size - 6);
1678 * Add on the checksum size and the offset from the
1681 smi_msg->data_size += 1 + i;
1683 smi_msg->msgid = msgid;
1686 static inline void format_lan_msg(struct ipmi_smi_msg *smi_msg,
1687 struct kernel_ipmi_msg *msg,
1688 struct ipmi_lan_addr *lan_addr,
1690 unsigned char ipmb_seq,
1691 unsigned char source_lun)
1693 /* Format the IPMB header data. */
1694 smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
1695 smi_msg->data[1] = IPMI_SEND_MSG_CMD;
1696 smi_msg->data[2] = lan_addr->channel;
1697 smi_msg->data[3] = lan_addr->session_handle;
1698 smi_msg->data[4] = lan_addr->remote_SWID;
1699 smi_msg->data[5] = (msg->netfn << 2) | (lan_addr->lun & 0x3);
1700 smi_msg->data[6] = ipmb_checksum(&smi_msg->data[4], 2);
1701 smi_msg->data[7] = lan_addr->local_SWID;
1702 smi_msg->data[8] = (ipmb_seq << 2) | source_lun;
1703 smi_msg->data[9] = msg->cmd;
1705 /* Now tack on the data to the message. */
1706 if (msg->data_len > 0)
1707 memcpy(&smi_msg->data[10], msg->data, msg->data_len);
1708 smi_msg->data_size = msg->data_len + 10;
1710 /* Now calculate the checksum and tack it on. */
1711 smi_msg->data[smi_msg->data_size]
1712 = ipmb_checksum(&smi_msg->data[7], smi_msg->data_size - 7);
1715 * Add on the checksum size and the offset from the
1718 smi_msg->data_size += 1;
1720 smi_msg->msgid = msgid;
1723 static struct ipmi_smi_msg *smi_add_send_msg(struct ipmi_smi *intf,
1724 struct ipmi_smi_msg *smi_msg,
1727 if (intf->curr_msg) {
1729 list_add_tail(&smi_msg->link, &intf->hp_xmit_msgs);
1731 list_add_tail(&smi_msg->link, &intf->xmit_msgs);
1734 intf->curr_msg = smi_msg;
1741 static void smi_send(struct ipmi_smi *intf,
1742 const struct ipmi_smi_handlers *handlers,
1743 struct ipmi_smi_msg *smi_msg, int priority)
1745 int run_to_completion = intf->run_to_completion;
1747 if (run_to_completion) {
1748 smi_msg = smi_add_send_msg(intf, smi_msg, priority);
1750 unsigned long flags;
1752 spin_lock_irqsave(&intf->xmit_msgs_lock, flags);
1753 smi_msg = smi_add_send_msg(intf, smi_msg, priority);
1754 spin_unlock_irqrestore(&intf->xmit_msgs_lock, flags);
1758 handlers->sender(intf->send_info, smi_msg);
1761 static bool is_maintenance_mode_cmd(struct kernel_ipmi_msg *msg)
1763 return (((msg->netfn == IPMI_NETFN_APP_REQUEST)
1764 && ((msg->cmd == IPMI_COLD_RESET_CMD)
1765 || (msg->cmd == IPMI_WARM_RESET_CMD)))
1766 || (msg->netfn == IPMI_NETFN_FIRMWARE_REQUEST));
1769 static int i_ipmi_req_sysintf(struct ipmi_smi *intf,
1770 struct ipmi_addr *addr,
1772 struct kernel_ipmi_msg *msg,
1773 struct ipmi_smi_msg *smi_msg,
1774 struct ipmi_recv_msg *recv_msg,
1776 unsigned int retry_time_ms)
1778 struct ipmi_system_interface_addr *smi_addr;
1781 /* Responses are not allowed to the SMI. */
1784 smi_addr = (struct ipmi_system_interface_addr *) addr;
1785 if (smi_addr->lun > 3) {
1786 ipmi_inc_stat(intf, sent_invalid_commands);
1790 memcpy(&recv_msg->addr, smi_addr, sizeof(*smi_addr));
1792 if ((msg->netfn == IPMI_NETFN_APP_REQUEST)
1793 && ((msg->cmd == IPMI_SEND_MSG_CMD)
1794 || (msg->cmd == IPMI_GET_MSG_CMD)
1795 || (msg->cmd == IPMI_READ_EVENT_MSG_BUFFER_CMD))) {
1797 * We don't let the user do these, since we manage
1798 * the sequence numbers.
1800 ipmi_inc_stat(intf, sent_invalid_commands);
1804 if (is_maintenance_mode_cmd(msg)) {
1805 unsigned long flags;
1807 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
1808 intf->auto_maintenance_timeout
1809 = maintenance_mode_timeout_ms;
1810 if (!intf->maintenance_mode
1811 && !intf->maintenance_mode_enable) {
1812 intf->maintenance_mode_enable = true;
1813 maintenance_mode_update(intf);
1815 spin_unlock_irqrestore(&intf->maintenance_mode_lock,
1819 if (msg->data_len + 2 > IPMI_MAX_MSG_LENGTH) {
1820 ipmi_inc_stat(intf, sent_invalid_commands);
1824 smi_msg->data[0] = (msg->netfn << 2) | (smi_addr->lun & 0x3);
1825 smi_msg->data[1] = msg->cmd;
1826 smi_msg->msgid = msgid;
1827 smi_msg->user_data = recv_msg;
1828 if (msg->data_len > 0)
1829 memcpy(&smi_msg->data[2], msg->data, msg->data_len);
1830 smi_msg->data_size = msg->data_len + 2;
1831 ipmi_inc_stat(intf, sent_local_commands);
1836 static int i_ipmi_req_ipmb(struct ipmi_smi *intf,
1837 struct ipmi_addr *addr,
1839 struct kernel_ipmi_msg *msg,
1840 struct ipmi_smi_msg *smi_msg,
1841 struct ipmi_recv_msg *recv_msg,
1842 unsigned char source_address,
1843 unsigned char source_lun,
1845 unsigned int retry_time_ms)
1847 struct ipmi_ipmb_addr *ipmb_addr;
1848 unsigned char ipmb_seq;
1851 struct ipmi_channel *chans;
1854 if (addr->channel >= IPMI_MAX_CHANNELS) {
1855 ipmi_inc_stat(intf, sent_invalid_commands);
1859 chans = READ_ONCE(intf->channel_list)->c;
1861 if (chans[addr->channel].medium != IPMI_CHANNEL_MEDIUM_IPMB) {
1862 ipmi_inc_stat(intf, sent_invalid_commands);
1866 if (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE) {
1868 * Broadcasts add a zero at the beginning of the
1869 * message, but otherwise is the same as an IPMB
1872 addr->addr_type = IPMI_IPMB_ADDR_TYPE;
1874 retries = 0; /* Don't retry broadcasts. */
1878 * 9 for the header and 1 for the checksum, plus
1879 * possibly one for the broadcast.
1881 if ((msg->data_len + 10 + broadcast) > IPMI_MAX_MSG_LENGTH) {
1882 ipmi_inc_stat(intf, sent_invalid_commands);
1886 ipmb_addr = (struct ipmi_ipmb_addr *) addr;
1887 if (ipmb_addr->lun > 3) {
1888 ipmi_inc_stat(intf, sent_invalid_commands);
1892 memcpy(&recv_msg->addr, ipmb_addr, sizeof(*ipmb_addr));
1894 if (recv_msg->msg.netfn & 0x1) {
1896 * It's a response, so use the user's sequence
1899 ipmi_inc_stat(intf, sent_ipmb_responses);
1900 format_ipmb_msg(smi_msg, msg, ipmb_addr, msgid,
1902 source_address, source_lun);
1905 * Save the receive message so we can use it
1906 * to deliver the response.
1908 smi_msg->user_data = recv_msg;
1910 /* It's a command, so get a sequence for it. */
1911 unsigned long flags;
1913 spin_lock_irqsave(&intf->seq_lock, flags);
1915 if (is_maintenance_mode_cmd(msg))
1916 intf->ipmb_maintenance_mode_timeout =
1917 maintenance_mode_timeout_ms;
1919 if (intf->ipmb_maintenance_mode_timeout && retry_time_ms == 0)
1920 /* Different default in maintenance mode */
1921 retry_time_ms = default_maintenance_retry_ms;
1924 * Create a sequence number with a 1 second
1925 * timeout and 4 retries.
1927 rv = intf_next_seq(intf,
1936 * We have used up all the sequence numbers,
1937 * probably, so abort.
1941 ipmi_inc_stat(intf, sent_ipmb_commands);
1944 * Store the sequence number in the message,
1945 * so that when the send message response
1946 * comes back we can start the timer.
1948 format_ipmb_msg(smi_msg, msg, ipmb_addr,
1949 STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
1950 ipmb_seq, broadcast,
1951 source_address, source_lun);
1954 * Copy the message into the recv message data, so we
1955 * can retransmit it later if necessary.
1957 memcpy(recv_msg->msg_data, smi_msg->data,
1958 smi_msg->data_size);
1959 recv_msg->msg.data = recv_msg->msg_data;
1960 recv_msg->msg.data_len = smi_msg->data_size;
1963 * We don't unlock until here, because we need
1964 * to copy the completed message into the
1965 * recv_msg before we release the lock.
1966 * Otherwise, race conditions may bite us. I
1967 * know that's pretty paranoid, but I prefer
1971 spin_unlock_irqrestore(&intf->seq_lock, flags);
1977 static int i_ipmi_req_lan(struct ipmi_smi *intf,
1978 struct ipmi_addr *addr,
1980 struct kernel_ipmi_msg *msg,
1981 struct ipmi_smi_msg *smi_msg,
1982 struct ipmi_recv_msg *recv_msg,
1983 unsigned char source_lun,
1985 unsigned int retry_time_ms)
1987 struct ipmi_lan_addr *lan_addr;
1988 unsigned char ipmb_seq;
1990 struct ipmi_channel *chans;
1993 if (addr->channel >= IPMI_MAX_CHANNELS) {
1994 ipmi_inc_stat(intf, sent_invalid_commands);
1998 chans = READ_ONCE(intf->channel_list)->c;
2000 if ((chans[addr->channel].medium
2001 != IPMI_CHANNEL_MEDIUM_8023LAN)
2002 && (chans[addr->channel].medium
2003 != IPMI_CHANNEL_MEDIUM_ASYNC)) {
2004 ipmi_inc_stat(intf, sent_invalid_commands);
2008 /* 11 for the header and 1 for the checksum. */
2009 if ((msg->data_len + 12) > IPMI_MAX_MSG_LENGTH) {
2010 ipmi_inc_stat(intf, sent_invalid_commands);
2014 lan_addr = (struct ipmi_lan_addr *) addr;
2015 if (lan_addr->lun > 3) {
2016 ipmi_inc_stat(intf, sent_invalid_commands);
2020 memcpy(&recv_msg->addr, lan_addr, sizeof(*lan_addr));
2022 if (recv_msg->msg.netfn & 0x1) {
2024 * It's a response, so use the user's sequence
2027 ipmi_inc_stat(intf, sent_lan_responses);
2028 format_lan_msg(smi_msg, msg, lan_addr, msgid,
2032 * Save the receive message so we can use it
2033 * to deliver the response.
2035 smi_msg->user_data = recv_msg;
2037 /* It's a command, so get a sequence for it. */
2038 unsigned long flags;
2040 spin_lock_irqsave(&intf->seq_lock, flags);
2043 * Create a sequence number with a 1 second
2044 * timeout and 4 retries.
2046 rv = intf_next_seq(intf,
2055 * We have used up all the sequence numbers,
2056 * probably, so abort.
2060 ipmi_inc_stat(intf, sent_lan_commands);
2063 * Store the sequence number in the message,
2064 * so that when the send message response
2065 * comes back we can start the timer.
2067 format_lan_msg(smi_msg, msg, lan_addr,
2068 STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
2069 ipmb_seq, source_lun);
2072 * Copy the message into the recv message data, so we
2073 * can retransmit it later if necessary.
2075 memcpy(recv_msg->msg_data, smi_msg->data,
2076 smi_msg->data_size);
2077 recv_msg->msg.data = recv_msg->msg_data;
2078 recv_msg->msg.data_len = smi_msg->data_size;
2081 * We don't unlock until here, because we need
2082 * to copy the completed message into the
2083 * recv_msg before we release the lock.
2084 * Otherwise, race conditions may bite us. I
2085 * know that's pretty paranoid, but I prefer
2089 spin_unlock_irqrestore(&intf->seq_lock, flags);
2096 * Separate from ipmi_request so that the user does not have to be
2097 * supplied in certain circumstances (mainly at panic time). If
2098 * messages are supplied, they will be freed, even if an error
2101 static int i_ipmi_request(struct ipmi_user *user,
2102 struct ipmi_smi *intf,
2103 struct ipmi_addr *addr,
2105 struct kernel_ipmi_msg *msg,
2106 void *user_msg_data,
2108 struct ipmi_recv_msg *supplied_recv,
2110 unsigned char source_address,
2111 unsigned char source_lun,
2113 unsigned int retry_time_ms)
2115 struct ipmi_smi_msg *smi_msg;
2116 struct ipmi_recv_msg *recv_msg;
2120 recv_msg = supplied_recv;
2122 recv_msg = ipmi_alloc_recv_msg();
2123 if (recv_msg == NULL) {
2128 recv_msg->user_msg_data = user_msg_data;
2131 smi_msg = (struct ipmi_smi_msg *) supplied_smi;
2133 smi_msg = ipmi_alloc_smi_msg();
2134 if (smi_msg == NULL) {
2135 ipmi_free_recv_msg(recv_msg);
2142 if (intf->in_shutdown) {
2147 recv_msg->user = user;
2149 /* The put happens when the message is freed. */
2150 kref_get(&user->refcount);
2151 recv_msg->msgid = msgid;
2153 * Store the message to send in the receive message so timeout
2154 * responses can get the proper response data.
2156 recv_msg->msg = *msg;
2158 if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
2159 rv = i_ipmi_req_sysintf(intf, addr, msgid, msg, smi_msg,
2160 recv_msg, retries, retry_time_ms);
2161 } else if (is_ipmb_addr(addr) || is_ipmb_bcast_addr(addr)) {
2162 rv = i_ipmi_req_ipmb(intf, addr, msgid, msg, smi_msg, recv_msg,
2163 source_address, source_lun,
2164 retries, retry_time_ms);
2165 } else if (is_lan_addr(addr)) {
2166 rv = i_ipmi_req_lan(intf, addr, msgid, msg, smi_msg, recv_msg,
2167 source_lun, retries, retry_time_ms);
2169 /* Unknown address type. */
2170 ipmi_inc_stat(intf, sent_invalid_commands);
2176 ipmi_free_smi_msg(smi_msg);
2177 ipmi_free_recv_msg(recv_msg);
2179 ipmi_debug_msg("Send", smi_msg->data, smi_msg->data_size);
2181 smi_send(intf, intf->handlers, smi_msg, priority);
2189 static int check_addr(struct ipmi_smi *intf,
2190 struct ipmi_addr *addr,
2191 unsigned char *saddr,
2194 if (addr->channel >= IPMI_MAX_CHANNELS)
2196 addr->channel = array_index_nospec(addr->channel, IPMI_MAX_CHANNELS);
2197 *lun = intf->addrinfo[addr->channel].lun;
2198 *saddr = intf->addrinfo[addr->channel].address;
2202 int ipmi_request_settime(struct ipmi_user *user,
2203 struct ipmi_addr *addr,
2205 struct kernel_ipmi_msg *msg,
2206 void *user_msg_data,
2209 unsigned int retry_time_ms)
2211 unsigned char saddr = 0, lun = 0;
2217 user = acquire_ipmi_user(user, &index);
2221 rv = check_addr(user->intf, addr, &saddr, &lun);
2223 rv = i_ipmi_request(user,
2236 release_ipmi_user(user, index);
2239 EXPORT_SYMBOL(ipmi_request_settime);
2241 int ipmi_request_supply_msgs(struct ipmi_user *user,
2242 struct ipmi_addr *addr,
2244 struct kernel_ipmi_msg *msg,
2245 void *user_msg_data,
2247 struct ipmi_recv_msg *supplied_recv,
2250 unsigned char saddr = 0, lun = 0;
2256 user = acquire_ipmi_user(user, &index);
2260 rv = check_addr(user->intf, addr, &saddr, &lun);
2262 rv = i_ipmi_request(user,
2275 release_ipmi_user(user, index);
2278 EXPORT_SYMBOL(ipmi_request_supply_msgs);
2280 static void bmc_device_id_handler(struct ipmi_smi *intf,
2281 struct ipmi_recv_msg *msg)
2285 if ((msg->addr.addr_type != IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
2286 || (msg->msg.netfn != IPMI_NETFN_APP_RESPONSE)
2287 || (msg->msg.cmd != IPMI_GET_DEVICE_ID_CMD)) {
2288 dev_warn(intf->si_dev,
2289 "invalid device_id msg: addr_type=%d netfn=%x cmd=%x\n",
2290 msg->addr.addr_type, msg->msg.netfn, msg->msg.cmd);
2294 rv = ipmi_demangle_device_id(msg->msg.netfn, msg->msg.cmd,
2295 msg->msg.data, msg->msg.data_len, &intf->bmc->fetch_id);
2297 dev_warn(intf->si_dev, "device id demangle failed: %d\n", rv);
2298 intf->bmc->dyn_id_set = 0;
2301 * Make sure the id data is available before setting
2305 intf->bmc->dyn_id_set = 1;
2308 wake_up(&intf->waitq);
2312 send_get_device_id_cmd(struct ipmi_smi *intf)
2314 struct ipmi_system_interface_addr si;
2315 struct kernel_ipmi_msg msg;
2317 si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2318 si.channel = IPMI_BMC_CHANNEL;
2321 msg.netfn = IPMI_NETFN_APP_REQUEST;
2322 msg.cmd = IPMI_GET_DEVICE_ID_CMD;
2326 return i_ipmi_request(NULL,
2328 (struct ipmi_addr *) &si,
2335 intf->addrinfo[0].address,
2336 intf->addrinfo[0].lun,
2340 static int __get_device_id(struct ipmi_smi *intf, struct bmc_device *bmc)
2344 bmc->dyn_id_set = 2;
2346 intf->null_user_handler = bmc_device_id_handler;
2348 rv = send_get_device_id_cmd(intf);
2352 wait_event(intf->waitq, bmc->dyn_id_set != 2);
2354 if (!bmc->dyn_id_set)
2355 rv = -EIO; /* Something went wrong in the fetch. */
2357 /* dyn_id_set makes the id data available. */
2360 intf->null_user_handler = NULL;
2366 * Fetch the device id for the bmc/interface. You must pass in either
2367 * bmc or intf, this code will get the other one. If the data has
2368 * been recently fetched, this will just use the cached data. Otherwise
2369 * it will run a new fetch.
2371 * Except for the first time this is called (in ipmi_register_smi()),
2372 * this will always return good data;
2374 static int __bmc_get_device_id(struct ipmi_smi *intf, struct bmc_device *bmc,
2375 struct ipmi_device_id *id,
2376 bool *guid_set, guid_t *guid, int intf_num)
2379 int prev_dyn_id_set, prev_guid_set;
2380 bool intf_set = intf != NULL;
2383 mutex_lock(&bmc->dyn_mutex);
2385 if (list_empty(&bmc->intfs)) {
2386 mutex_unlock(&bmc->dyn_mutex);
2389 intf = list_first_entry(&bmc->intfs, struct ipmi_smi,
2391 kref_get(&intf->refcount);
2392 mutex_unlock(&bmc->dyn_mutex);
2393 mutex_lock(&intf->bmc_reg_mutex);
2394 mutex_lock(&bmc->dyn_mutex);
2395 if (intf != list_first_entry(&bmc->intfs, struct ipmi_smi,
2397 mutex_unlock(&intf->bmc_reg_mutex);
2398 kref_put(&intf->refcount, intf_free);
2399 goto retry_bmc_lock;
2402 mutex_lock(&intf->bmc_reg_mutex);
2404 mutex_lock(&bmc->dyn_mutex);
2405 kref_get(&intf->refcount);
2408 /* If we have a valid and current ID, just return that. */
2409 if (intf->in_bmc_register ||
2410 (bmc->dyn_id_set && time_is_after_jiffies(bmc->dyn_id_expiry)))
2411 goto out_noprocessing;
2413 prev_guid_set = bmc->dyn_guid_set;
2416 prev_dyn_id_set = bmc->dyn_id_set;
2417 rv = __get_device_id(intf, bmc);
2422 * The guid, device id, manufacturer id, and product id should
2423 * not change on a BMC. If it does we have to do some dancing.
2425 if (!intf->bmc_registered
2426 || (!prev_guid_set && bmc->dyn_guid_set)
2427 || (!prev_dyn_id_set && bmc->dyn_id_set)
2428 || (prev_guid_set && bmc->dyn_guid_set
2429 && !guid_equal(&bmc->guid, &bmc->fetch_guid))
2430 || bmc->id.device_id != bmc->fetch_id.device_id
2431 || bmc->id.manufacturer_id != bmc->fetch_id.manufacturer_id
2432 || bmc->id.product_id != bmc->fetch_id.product_id) {
2433 struct ipmi_device_id id = bmc->fetch_id;
2434 int guid_set = bmc->dyn_guid_set;
2437 guid = bmc->fetch_guid;
2438 mutex_unlock(&bmc->dyn_mutex);
2440 __ipmi_bmc_unregister(intf);
2441 /* Fill in the temporary BMC for good measure. */
2443 intf->bmc->dyn_guid_set = guid_set;
2444 intf->bmc->guid = guid;
2445 if (__ipmi_bmc_register(intf, &id, guid_set, &guid, intf_num))
2446 need_waiter(intf); /* Retry later on an error. */
2448 __scan_channels(intf, &id);
2453 * We weren't given the interface on the
2454 * command line, so restart the operation on
2455 * the next interface for the BMC.
2457 mutex_unlock(&intf->bmc_reg_mutex);
2458 mutex_lock(&bmc->dyn_mutex);
2459 goto retry_bmc_lock;
2462 /* We have a new BMC, set it up. */
2464 mutex_lock(&bmc->dyn_mutex);
2465 goto out_noprocessing;
2466 } else if (memcmp(&bmc->fetch_id, &bmc->id, sizeof(bmc->id)))
2467 /* Version info changes, scan the channels again. */
2468 __scan_channels(intf, &bmc->fetch_id);
2470 bmc->dyn_id_expiry = jiffies + IPMI_DYN_DEV_ID_EXPIRY;
2473 if (rv && prev_dyn_id_set) {
2474 rv = 0; /* Ignore failures if we have previous data. */
2475 bmc->dyn_id_set = prev_dyn_id_set;
2478 bmc->id = bmc->fetch_id;
2479 if (bmc->dyn_guid_set)
2480 bmc->guid = bmc->fetch_guid;
2481 else if (prev_guid_set)
2483 * The guid used to be valid and it failed to fetch,
2484 * just use the cached value.
2486 bmc->dyn_guid_set = prev_guid_set;
2494 *guid_set = bmc->dyn_guid_set;
2496 if (guid && bmc->dyn_guid_set)
2500 mutex_unlock(&bmc->dyn_mutex);
2501 mutex_unlock(&intf->bmc_reg_mutex);
2503 kref_put(&intf->refcount, intf_free);
2507 static int bmc_get_device_id(struct ipmi_smi *intf, struct bmc_device *bmc,
2508 struct ipmi_device_id *id,
2509 bool *guid_set, guid_t *guid)
2511 return __bmc_get_device_id(intf, bmc, id, guid_set, guid, -1);
2514 static ssize_t device_id_show(struct device *dev,
2515 struct device_attribute *attr,
2518 struct bmc_device *bmc = to_bmc_device(dev);
2519 struct ipmi_device_id id;
2522 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2526 return snprintf(buf, 10, "%u\n", id.device_id);
2528 static DEVICE_ATTR_RO(device_id);
2530 static ssize_t provides_device_sdrs_show(struct device *dev,
2531 struct device_attribute *attr,
2534 struct bmc_device *bmc = to_bmc_device(dev);
2535 struct ipmi_device_id id;
2538 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2542 return snprintf(buf, 10, "%u\n", (id.device_revision & 0x80) >> 7);
2544 static DEVICE_ATTR_RO(provides_device_sdrs);
2546 static ssize_t revision_show(struct device *dev, struct device_attribute *attr,
2549 struct bmc_device *bmc = to_bmc_device(dev);
2550 struct ipmi_device_id id;
2553 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2557 return snprintf(buf, 20, "%u\n", id.device_revision & 0x0F);
2559 static DEVICE_ATTR_RO(revision);
2561 static ssize_t firmware_revision_show(struct device *dev,
2562 struct device_attribute *attr,
2565 struct bmc_device *bmc = to_bmc_device(dev);
2566 struct ipmi_device_id id;
2569 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2573 return snprintf(buf, 20, "%u.%x\n", id.firmware_revision_1,
2574 id.firmware_revision_2);
2576 static DEVICE_ATTR_RO(firmware_revision);
2578 static ssize_t ipmi_version_show(struct device *dev,
2579 struct device_attribute *attr,
2582 struct bmc_device *bmc = to_bmc_device(dev);
2583 struct ipmi_device_id id;
2586 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2590 return snprintf(buf, 20, "%u.%u\n",
2591 ipmi_version_major(&id),
2592 ipmi_version_minor(&id));
2594 static DEVICE_ATTR_RO(ipmi_version);
2596 static ssize_t add_dev_support_show(struct device *dev,
2597 struct device_attribute *attr,
2600 struct bmc_device *bmc = to_bmc_device(dev);
2601 struct ipmi_device_id id;
2604 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2608 return snprintf(buf, 10, "0x%02x\n", id.additional_device_support);
2610 static DEVICE_ATTR(additional_device_support, S_IRUGO, add_dev_support_show,
2613 static ssize_t manufacturer_id_show(struct device *dev,
2614 struct device_attribute *attr,
2617 struct bmc_device *bmc = to_bmc_device(dev);
2618 struct ipmi_device_id id;
2621 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2625 return snprintf(buf, 20, "0x%6.6x\n", id.manufacturer_id);
2627 static DEVICE_ATTR_RO(manufacturer_id);
2629 static ssize_t product_id_show(struct device *dev,
2630 struct device_attribute *attr,
2633 struct bmc_device *bmc = to_bmc_device(dev);
2634 struct ipmi_device_id id;
2637 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2641 return snprintf(buf, 10, "0x%4.4x\n", id.product_id);
2643 static DEVICE_ATTR_RO(product_id);
2645 static ssize_t aux_firmware_rev_show(struct device *dev,
2646 struct device_attribute *attr,
2649 struct bmc_device *bmc = to_bmc_device(dev);
2650 struct ipmi_device_id id;
2653 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2657 return snprintf(buf, 21, "0x%02x 0x%02x 0x%02x 0x%02x\n",
2658 id.aux_firmware_revision[3],
2659 id.aux_firmware_revision[2],
2660 id.aux_firmware_revision[1],
2661 id.aux_firmware_revision[0]);
2663 static DEVICE_ATTR(aux_firmware_revision, S_IRUGO, aux_firmware_rev_show, NULL);
2665 static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
2668 struct bmc_device *bmc = to_bmc_device(dev);
2673 rv = bmc_get_device_id(NULL, bmc, NULL, &guid_set, &guid);
2679 return snprintf(buf, 38, "%pUl\n", guid.b);
2681 static DEVICE_ATTR_RO(guid);
2683 static struct attribute *bmc_dev_attrs[] = {
2684 &dev_attr_device_id.attr,
2685 &dev_attr_provides_device_sdrs.attr,
2686 &dev_attr_revision.attr,
2687 &dev_attr_firmware_revision.attr,
2688 &dev_attr_ipmi_version.attr,
2689 &dev_attr_additional_device_support.attr,
2690 &dev_attr_manufacturer_id.attr,
2691 &dev_attr_product_id.attr,
2692 &dev_attr_aux_firmware_revision.attr,
2693 &dev_attr_guid.attr,
2697 static umode_t bmc_dev_attr_is_visible(struct kobject *kobj,
2698 struct attribute *attr, int idx)
2700 struct device *dev = kobj_to_dev(kobj);
2701 struct bmc_device *bmc = to_bmc_device(dev);
2702 umode_t mode = attr->mode;
2705 if (attr == &dev_attr_aux_firmware_revision.attr) {
2706 struct ipmi_device_id id;
2708 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2709 return (!rv && id.aux_firmware_revision_set) ? mode : 0;
2711 if (attr == &dev_attr_guid.attr) {
2714 rv = bmc_get_device_id(NULL, bmc, NULL, &guid_set, NULL);
2715 return (!rv && guid_set) ? mode : 0;
2720 static const struct attribute_group bmc_dev_attr_group = {
2721 .attrs = bmc_dev_attrs,
2722 .is_visible = bmc_dev_attr_is_visible,
2725 static const struct attribute_group *bmc_dev_attr_groups[] = {
2726 &bmc_dev_attr_group,
2730 static const struct device_type bmc_device_type = {
2731 .groups = bmc_dev_attr_groups,
2734 static int __find_bmc_guid(struct device *dev, void *data)
2736 guid_t *guid = data;
2737 struct bmc_device *bmc;
2740 if (dev->type != &bmc_device_type)
2743 bmc = to_bmc_device(dev);
2744 rv = bmc->dyn_guid_set && guid_equal(&bmc->guid, guid);
2746 rv = kref_get_unless_zero(&bmc->usecount);
2751 * Returns with the bmc's usecount incremented, if it is non-NULL.
2753 static struct bmc_device *ipmi_find_bmc_guid(struct device_driver *drv,
2757 struct bmc_device *bmc = NULL;
2759 dev = driver_find_device(drv, NULL, guid, __find_bmc_guid);
2761 bmc = to_bmc_device(dev);
2767 struct prod_dev_id {
2768 unsigned int product_id;
2769 unsigned char device_id;
2772 static int __find_bmc_prod_dev_id(struct device *dev, void *data)
2774 struct prod_dev_id *cid = data;
2775 struct bmc_device *bmc;
2778 if (dev->type != &bmc_device_type)
2781 bmc = to_bmc_device(dev);
2782 rv = (bmc->id.product_id == cid->product_id
2783 && bmc->id.device_id == cid->device_id);
2785 rv = kref_get_unless_zero(&bmc->usecount);
2790 * Returns with the bmc's usecount incremented, if it is non-NULL.
2792 static struct bmc_device *ipmi_find_bmc_prod_dev_id(
2793 struct device_driver *drv,
2794 unsigned int product_id, unsigned char device_id)
2796 struct prod_dev_id id = {
2797 .product_id = product_id,
2798 .device_id = device_id,
2801 struct bmc_device *bmc = NULL;
2803 dev = driver_find_device(drv, NULL, &id, __find_bmc_prod_dev_id);
2805 bmc = to_bmc_device(dev);
2811 static DEFINE_IDA(ipmi_bmc_ida);
2814 release_bmc_device(struct device *dev)
2816 kfree(to_bmc_device(dev));
2819 static void cleanup_bmc_work(struct work_struct *work)
2821 struct bmc_device *bmc = container_of(work, struct bmc_device,
2823 int id = bmc->pdev.id; /* Unregister overwrites id */
2825 platform_device_unregister(&bmc->pdev);
2826 ida_simple_remove(&ipmi_bmc_ida, id);
2830 cleanup_bmc_device(struct kref *ref)
2832 struct bmc_device *bmc = container_of(ref, struct bmc_device, usecount);
2835 * Remove the platform device in a work queue to avoid issues
2836 * with removing the device attributes while reading a device
2839 schedule_work(&bmc->remove_work);
2843 * Must be called with intf->bmc_reg_mutex held.
2845 static void __ipmi_bmc_unregister(struct ipmi_smi *intf)
2847 struct bmc_device *bmc = intf->bmc;
2849 if (!intf->bmc_registered)
2852 sysfs_remove_link(&intf->si_dev->kobj, "bmc");
2853 sysfs_remove_link(&bmc->pdev.dev.kobj, intf->my_dev_name);
2854 kfree(intf->my_dev_name);
2855 intf->my_dev_name = NULL;
2857 mutex_lock(&bmc->dyn_mutex);
2858 list_del(&intf->bmc_link);
2859 mutex_unlock(&bmc->dyn_mutex);
2860 intf->bmc = &intf->tmp_bmc;
2861 kref_put(&bmc->usecount, cleanup_bmc_device);
2862 intf->bmc_registered = false;
2865 static void ipmi_bmc_unregister(struct ipmi_smi *intf)
2867 mutex_lock(&intf->bmc_reg_mutex);
2868 __ipmi_bmc_unregister(intf);
2869 mutex_unlock(&intf->bmc_reg_mutex);
2873 * Must be called with intf->bmc_reg_mutex held.
2875 static int __ipmi_bmc_register(struct ipmi_smi *intf,
2876 struct ipmi_device_id *id,
2877 bool guid_set, guid_t *guid, int intf_num)
2880 struct bmc_device *bmc;
2881 struct bmc_device *old_bmc;
2884 * platform_device_register() can cause bmc_reg_mutex to
2885 * be claimed because of the is_visible functions of
2886 * the attributes. Eliminate possible recursion and
2889 intf->in_bmc_register = true;
2890 mutex_unlock(&intf->bmc_reg_mutex);
2893 * Try to find if there is an bmc_device struct
2894 * representing the interfaced BMC already
2896 mutex_lock(&ipmidriver_mutex);
2898 old_bmc = ipmi_find_bmc_guid(&ipmidriver.driver, guid);
2900 old_bmc = ipmi_find_bmc_prod_dev_id(&ipmidriver.driver,
2905 * If there is already an bmc_device, free the new one,
2906 * otherwise register the new BMC device
2911 * Note: old_bmc already has usecount incremented by
2912 * the BMC find functions.
2914 intf->bmc = old_bmc;
2915 mutex_lock(&bmc->dyn_mutex);
2916 list_add_tail(&intf->bmc_link, &bmc->intfs);
2917 mutex_unlock(&bmc->dyn_mutex);
2919 dev_info(intf->si_dev,
2920 "interfacing existing BMC (man_id: 0x%6.6x, prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
2921 bmc->id.manufacturer_id,
2925 bmc = kzalloc(sizeof(*bmc), GFP_KERNEL);
2930 INIT_LIST_HEAD(&bmc->intfs);
2931 mutex_init(&bmc->dyn_mutex);
2932 INIT_WORK(&bmc->remove_work, cleanup_bmc_work);
2935 bmc->dyn_id_set = 1;
2936 bmc->dyn_guid_set = guid_set;
2938 bmc->dyn_id_expiry = jiffies + IPMI_DYN_DEV_ID_EXPIRY;
2940 bmc->pdev.name = "ipmi_bmc";
2942 rv = ida_simple_get(&ipmi_bmc_ida, 0, 0, GFP_KERNEL);
2945 bmc->pdev.dev.driver = &ipmidriver.driver;
2947 bmc->pdev.dev.release = release_bmc_device;
2948 bmc->pdev.dev.type = &bmc_device_type;
2949 kref_init(&bmc->usecount);
2952 mutex_lock(&bmc->dyn_mutex);
2953 list_add_tail(&intf->bmc_link, &bmc->intfs);
2954 mutex_unlock(&bmc->dyn_mutex);
2956 rv = platform_device_register(&bmc->pdev);
2958 dev_err(intf->si_dev,
2959 "Unable to register bmc device: %d\n",
2964 dev_info(intf->si_dev,
2965 "Found new BMC (man_id: 0x%6.6x, prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
2966 bmc->id.manufacturer_id,
2972 * create symlink from system interface device to bmc device
2975 rv = sysfs_create_link(&intf->si_dev->kobj, &bmc->pdev.dev.kobj, "bmc");
2977 dev_err(intf->si_dev, "Unable to create bmc symlink: %d\n", rv);
2982 intf_num = intf->intf_num;
2983 intf->my_dev_name = kasprintf(GFP_KERNEL, "ipmi%d", intf_num);
2984 if (!intf->my_dev_name) {
2986 dev_err(intf->si_dev, "Unable to allocate link from BMC: %d\n",
2991 rv = sysfs_create_link(&bmc->pdev.dev.kobj, &intf->si_dev->kobj,
2994 kfree(intf->my_dev_name);
2995 intf->my_dev_name = NULL;
2996 dev_err(intf->si_dev, "Unable to create symlink to bmc: %d\n",
2998 goto out_free_my_dev_name;
3001 intf->bmc_registered = true;
3004 mutex_unlock(&ipmidriver_mutex);
3005 mutex_lock(&intf->bmc_reg_mutex);
3006 intf->in_bmc_register = false;
3010 out_free_my_dev_name:
3011 kfree(intf->my_dev_name);
3012 intf->my_dev_name = NULL;
3015 sysfs_remove_link(&intf->si_dev->kobj, "bmc");
3018 mutex_lock(&bmc->dyn_mutex);
3019 list_del(&intf->bmc_link);
3020 mutex_unlock(&bmc->dyn_mutex);
3021 intf->bmc = &intf->tmp_bmc;
3022 kref_put(&bmc->usecount, cleanup_bmc_device);
3026 mutex_lock(&bmc->dyn_mutex);
3027 list_del(&intf->bmc_link);
3028 mutex_unlock(&bmc->dyn_mutex);
3029 intf->bmc = &intf->tmp_bmc;
3030 put_device(&bmc->pdev.dev);
3035 send_guid_cmd(struct ipmi_smi *intf, int chan)
3037 struct kernel_ipmi_msg msg;
3038 struct ipmi_system_interface_addr si;
3040 si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3041 si.channel = IPMI_BMC_CHANNEL;
3044 msg.netfn = IPMI_NETFN_APP_REQUEST;
3045 msg.cmd = IPMI_GET_DEVICE_GUID_CMD;
3048 return i_ipmi_request(NULL,
3050 (struct ipmi_addr *) &si,
3057 intf->addrinfo[0].address,
3058 intf->addrinfo[0].lun,
3062 static void guid_handler(struct ipmi_smi *intf, struct ipmi_recv_msg *msg)
3064 struct bmc_device *bmc = intf->bmc;
3066 if ((msg->addr.addr_type != IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
3067 || (msg->msg.netfn != IPMI_NETFN_APP_RESPONSE)
3068 || (msg->msg.cmd != IPMI_GET_DEVICE_GUID_CMD))
3072 if (msg->msg.data[0] != 0) {
3073 /* Error from getting the GUID, the BMC doesn't have one. */
3074 bmc->dyn_guid_set = 0;
3078 if (msg->msg.data_len < 17) {
3079 bmc->dyn_guid_set = 0;
3080 dev_warn(intf->si_dev,
3081 "The GUID response from the BMC was too short, it was %d but should have been 17. Assuming GUID is not available.\n",
3086 memcpy(bmc->fetch_guid.b, msg->msg.data + 1, 16);
3088 * Make sure the guid data is available before setting
3092 bmc->dyn_guid_set = 1;
3094 wake_up(&intf->waitq);
3097 static void __get_guid(struct ipmi_smi *intf)
3100 struct bmc_device *bmc = intf->bmc;
3102 bmc->dyn_guid_set = 2;
3103 intf->null_user_handler = guid_handler;
3104 rv = send_guid_cmd(intf, 0);
3106 /* Send failed, no GUID available. */
3107 bmc->dyn_guid_set = 0;
3109 wait_event(intf->waitq, bmc->dyn_guid_set != 2);
3111 /* dyn_guid_set makes the guid data available. */
3114 intf->null_user_handler = NULL;
3118 send_channel_info_cmd(struct ipmi_smi *intf, int chan)
3120 struct kernel_ipmi_msg msg;
3121 unsigned char data[1];
3122 struct ipmi_system_interface_addr si;
3124 si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3125 si.channel = IPMI_BMC_CHANNEL;
3128 msg.netfn = IPMI_NETFN_APP_REQUEST;
3129 msg.cmd = IPMI_GET_CHANNEL_INFO_CMD;
3133 return i_ipmi_request(NULL,
3135 (struct ipmi_addr *) &si,
3142 intf->addrinfo[0].address,
3143 intf->addrinfo[0].lun,
3148 channel_handler(struct ipmi_smi *intf, struct ipmi_recv_msg *msg)
3152 unsigned int set = intf->curr_working_cset;
3153 struct ipmi_channel *chans;
3155 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
3156 && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
3157 && (msg->msg.cmd == IPMI_GET_CHANNEL_INFO_CMD)) {
3158 /* It's the one we want */
3159 if (msg->msg.data[0] != 0) {
3160 /* Got an error from the channel, just go on. */
3162 if (msg->msg.data[0] == IPMI_INVALID_COMMAND_ERR) {
3164 * If the MC does not support this
3165 * command, that is legal. We just
3166 * assume it has one IPMB at channel
3169 intf->wchannels[set].c[0].medium
3170 = IPMI_CHANNEL_MEDIUM_IPMB;
3171 intf->wchannels[set].c[0].protocol
3172 = IPMI_CHANNEL_PROTOCOL_IPMB;
3174 intf->channel_list = intf->wchannels + set;
3175 intf->channels_ready = true;
3176 wake_up(&intf->waitq);
3181 if (msg->msg.data_len < 4) {
3182 /* Message not big enough, just go on. */
3185 ch = intf->curr_channel;
3186 chans = intf->wchannels[set].c;
3187 chans[ch].medium = msg->msg.data[2] & 0x7f;
3188 chans[ch].protocol = msg->msg.data[3] & 0x1f;
3191 intf->curr_channel++;
3192 if (intf->curr_channel >= IPMI_MAX_CHANNELS) {
3193 intf->channel_list = intf->wchannels + set;
3194 intf->channels_ready = true;
3195 wake_up(&intf->waitq);
3197 intf->channel_list = intf->wchannels + set;
3198 intf->channels_ready = true;
3199 rv = send_channel_info_cmd(intf, intf->curr_channel);
3203 /* Got an error somehow, just give up. */
3204 dev_warn(intf->si_dev,
3205 "Error sending channel information for channel %d: %d\n",
3206 intf->curr_channel, rv);
3208 intf->channel_list = intf->wchannels + set;
3209 intf->channels_ready = true;
3210 wake_up(&intf->waitq);
3218 * Must be holding intf->bmc_reg_mutex to call this.
3220 static int __scan_channels(struct ipmi_smi *intf, struct ipmi_device_id *id)
3224 if (ipmi_version_major(id) > 1
3225 || (ipmi_version_major(id) == 1
3226 && ipmi_version_minor(id) >= 5)) {
3230 * Start scanning the channels to see what is
3233 set = !intf->curr_working_cset;
3234 intf->curr_working_cset = set;
3235 memset(&intf->wchannels[set], 0,
3236 sizeof(struct ipmi_channel_set));
3238 intf->null_user_handler = channel_handler;
3239 intf->curr_channel = 0;
3240 rv = send_channel_info_cmd(intf, 0);
3242 dev_warn(intf->si_dev,
3243 "Error sending channel information for channel 0, %d\n",
3248 /* Wait for the channel info to be read. */
3249 wait_event(intf->waitq, intf->channels_ready);
3250 intf->null_user_handler = NULL;
3252 unsigned int set = intf->curr_working_cset;
3254 /* Assume a single IPMB channel at zero. */
3255 intf->wchannels[set].c[0].medium = IPMI_CHANNEL_MEDIUM_IPMB;
3256 intf->wchannels[set].c[0].protocol = IPMI_CHANNEL_PROTOCOL_IPMB;
3257 intf->channel_list = intf->wchannels + set;
3258 intf->channels_ready = true;
3264 static void ipmi_poll(struct ipmi_smi *intf)
3266 if (intf->handlers->poll)
3267 intf->handlers->poll(intf->send_info);
3268 /* In case something came in */
3269 handle_new_recv_msgs(intf);
3272 void ipmi_poll_interface(struct ipmi_user *user)
3274 ipmi_poll(user->intf);
3276 EXPORT_SYMBOL(ipmi_poll_interface);
3278 static void redo_bmc_reg(struct work_struct *work)
3280 struct ipmi_smi *intf = container_of(work, struct ipmi_smi,
3283 if (!intf->in_shutdown)
3284 bmc_get_device_id(intf, NULL, NULL, NULL, NULL);
3286 kref_put(&intf->refcount, intf_free);
3289 int ipmi_register_smi(const struct ipmi_smi_handlers *handlers,
3291 struct device *si_dev,
3292 unsigned char slave_addr)
3296 struct ipmi_smi *intf, *tintf;
3297 struct list_head *link;
3298 struct ipmi_device_id id;
3301 * Make sure the driver is actually initialized, this handles
3302 * problems with initialization order.
3304 rv = ipmi_init_msghandler();
3308 intf = kzalloc(sizeof(*intf), GFP_KERNEL);
3312 rv = init_srcu_struct(&intf->users_srcu);
3319 intf->bmc = &intf->tmp_bmc;
3320 INIT_LIST_HEAD(&intf->bmc->intfs);
3321 mutex_init(&intf->bmc->dyn_mutex);
3322 INIT_LIST_HEAD(&intf->bmc_link);
3323 mutex_init(&intf->bmc_reg_mutex);
3324 intf->intf_num = -1; /* Mark it invalid for now. */
3325 kref_init(&intf->refcount);
3326 INIT_WORK(&intf->bmc_reg_work, redo_bmc_reg);
3327 intf->si_dev = si_dev;
3328 for (j = 0; j < IPMI_MAX_CHANNELS; j++) {
3329 intf->addrinfo[j].address = IPMI_BMC_SLAVE_ADDR;
3330 intf->addrinfo[j].lun = 2;
3332 if (slave_addr != 0)
3333 intf->addrinfo[0].address = slave_addr;
3334 INIT_LIST_HEAD(&intf->users);
3335 intf->handlers = handlers;
3336 intf->send_info = send_info;
3337 spin_lock_init(&intf->seq_lock);
3338 for (j = 0; j < IPMI_IPMB_NUM_SEQ; j++) {
3339 intf->seq_table[j].inuse = 0;
3340 intf->seq_table[j].seqid = 0;
3343 spin_lock_init(&intf->waiting_rcv_msgs_lock);
3344 INIT_LIST_HEAD(&intf->waiting_rcv_msgs);
3345 tasklet_init(&intf->recv_tasklet,
3347 (unsigned long) intf);
3348 atomic_set(&intf->watchdog_pretimeouts_to_deliver, 0);
3349 spin_lock_init(&intf->xmit_msgs_lock);
3350 INIT_LIST_HEAD(&intf->xmit_msgs);
3351 INIT_LIST_HEAD(&intf->hp_xmit_msgs);
3352 spin_lock_init(&intf->events_lock);
3353 atomic_set(&intf->event_waiters, 0);
3354 intf->ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
3355 INIT_LIST_HEAD(&intf->waiting_events);
3356 intf->waiting_events_count = 0;
3357 mutex_init(&intf->cmd_rcvrs_mutex);
3358 spin_lock_init(&intf->maintenance_mode_lock);
3359 INIT_LIST_HEAD(&intf->cmd_rcvrs);
3360 init_waitqueue_head(&intf->waitq);
3361 for (i = 0; i < IPMI_NUM_STATS; i++)
3362 atomic_set(&intf->stats[i], 0);
3364 mutex_lock(&ipmi_interfaces_mutex);
3365 /* Look for a hole in the numbers. */
3367 link = &ipmi_interfaces;
3368 list_for_each_entry_rcu(tintf, &ipmi_interfaces, link) {
3369 if (tintf->intf_num != i) {
3370 link = &tintf->link;
3375 /* Add the new interface in numeric order. */
3377 list_add_rcu(&intf->link, &ipmi_interfaces);
3379 list_add_tail_rcu(&intf->link, link);
3381 rv = handlers->start_processing(send_info, intf);
3385 rv = __bmc_get_device_id(intf, NULL, &id, NULL, NULL, i);
3387 dev_err(si_dev, "Unable to get the device id: %d\n", rv);
3388 goto out_err_started;
3391 mutex_lock(&intf->bmc_reg_mutex);
3392 rv = __scan_channels(intf, &id);
3393 mutex_unlock(&intf->bmc_reg_mutex);
3395 goto out_err_bmc_reg;
3398 * Keep memory order straight for RCU readers. Make
3399 * sure everything else is committed to memory before
3400 * setting intf_num to mark the interface valid.
3404 mutex_unlock(&ipmi_interfaces_mutex);
3406 /* After this point the interface is legal to use. */
3407 call_smi_watchers(i, intf->si_dev);
3412 ipmi_bmc_unregister(intf);
3414 if (intf->handlers->shutdown)
3415 intf->handlers->shutdown(intf->send_info);
3417 list_del_rcu(&intf->link);
3418 mutex_unlock(&ipmi_interfaces_mutex);
3419 synchronize_srcu(&ipmi_interfaces_srcu);
3420 cleanup_srcu_struct(&intf->users_srcu);
3421 kref_put(&intf->refcount, intf_free);
3425 EXPORT_SYMBOL(ipmi_register_smi);
3427 static void deliver_smi_err_response(struct ipmi_smi *intf,
3428 struct ipmi_smi_msg *msg,
3431 msg->rsp[0] = msg->data[0] | 4;
3432 msg->rsp[1] = msg->data[1];
3435 /* It's an error, so it will never requeue, no need to check return. */
3436 handle_one_recv_msg(intf, msg);
3439 static void cleanup_smi_msgs(struct ipmi_smi *intf)
3442 struct seq_table *ent;
3443 struct ipmi_smi_msg *msg;
3444 struct list_head *entry;
3445 struct list_head tmplist;
3447 /* Clear out our transmit queues and hold the messages. */
3448 INIT_LIST_HEAD(&tmplist);
3449 list_splice_tail(&intf->hp_xmit_msgs, &tmplist);
3450 list_splice_tail(&intf->xmit_msgs, &tmplist);
3452 /* Current message first, to preserve order */
3453 while (intf->curr_msg && !list_empty(&intf->waiting_rcv_msgs)) {
3454 /* Wait for the message to clear out. */
3455 schedule_timeout(1);
3458 /* No need for locks, the interface is down. */
3461 * Return errors for all pending messages in queue and in the
3462 * tables waiting for remote responses.
3464 while (!list_empty(&tmplist)) {
3465 entry = tmplist.next;
3467 msg = list_entry(entry, struct ipmi_smi_msg, link);
3468 deliver_smi_err_response(intf, msg, IPMI_ERR_UNSPECIFIED);
3471 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
3472 ent = &intf->seq_table[i];
3475 deliver_err_response(intf, ent->recv_msg, IPMI_ERR_UNSPECIFIED);
3479 void ipmi_unregister_smi(struct ipmi_smi *intf)
3481 struct ipmi_smi_watcher *w;
3482 int intf_num = intf->intf_num, index;
3484 mutex_lock(&ipmi_interfaces_mutex);
3485 intf->intf_num = -1;
3486 intf->in_shutdown = true;
3487 list_del_rcu(&intf->link);
3488 mutex_unlock(&ipmi_interfaces_mutex);
3489 synchronize_srcu(&ipmi_interfaces_srcu);
3491 /* At this point no users can be added to the interface. */
3494 * Call all the watcher interfaces to tell them that
3495 * an interface is going away.
3497 mutex_lock(&smi_watchers_mutex);
3498 list_for_each_entry(w, &smi_watchers, link)
3499 w->smi_gone(intf_num);
3500 mutex_unlock(&smi_watchers_mutex);
3502 index = srcu_read_lock(&intf->users_srcu);
3503 while (!list_empty(&intf->users)) {
3504 struct ipmi_user *user =
3505 container_of(list_next_rcu(&intf->users),
3506 struct ipmi_user, link);
3508 _ipmi_destroy_user(user);
3510 srcu_read_unlock(&intf->users_srcu, index);
3512 if (intf->handlers->shutdown)
3513 intf->handlers->shutdown(intf->send_info);
3515 cleanup_smi_msgs(intf);
3517 ipmi_bmc_unregister(intf);
3519 cleanup_srcu_struct(&intf->users_srcu);
3520 kref_put(&intf->refcount, intf_free);
3522 EXPORT_SYMBOL(ipmi_unregister_smi);
3524 static int handle_ipmb_get_msg_rsp(struct ipmi_smi *intf,
3525 struct ipmi_smi_msg *msg)
3527 struct ipmi_ipmb_addr ipmb_addr;
3528 struct ipmi_recv_msg *recv_msg;
3531 * This is 11, not 10, because the response must contain a
3534 if (msg->rsp_size < 11) {
3535 /* Message not big enough, just ignore it. */
3536 ipmi_inc_stat(intf, invalid_ipmb_responses);
3540 if (msg->rsp[2] != 0) {
3541 /* An error getting the response, just ignore it. */
3545 ipmb_addr.addr_type = IPMI_IPMB_ADDR_TYPE;
3546 ipmb_addr.slave_addr = msg->rsp[6];
3547 ipmb_addr.channel = msg->rsp[3] & 0x0f;
3548 ipmb_addr.lun = msg->rsp[7] & 3;
3551 * It's a response from a remote entity. Look up the sequence
3552 * number and handle the response.
3554 if (intf_find_seq(intf,
3558 (msg->rsp[4] >> 2) & (~1),
3559 (struct ipmi_addr *) &ipmb_addr,
3562 * We were unable to find the sequence number,
3563 * so just nuke the message.
3565 ipmi_inc_stat(intf, unhandled_ipmb_responses);
3569 memcpy(recv_msg->msg_data, &msg->rsp[9], msg->rsp_size - 9);
3571 * The other fields matched, so no need to set them, except
3572 * for netfn, which needs to be the response that was
3573 * returned, not the request value.
3575 recv_msg->msg.netfn = msg->rsp[4] >> 2;
3576 recv_msg->msg.data = recv_msg->msg_data;
3577 recv_msg->msg.data_len = msg->rsp_size - 10;
3578 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
3579 if (deliver_response(intf, recv_msg))
3580 ipmi_inc_stat(intf, unhandled_ipmb_responses);
3582 ipmi_inc_stat(intf, handled_ipmb_responses);
3587 static int handle_ipmb_get_msg_cmd(struct ipmi_smi *intf,
3588 struct ipmi_smi_msg *msg)
3590 struct cmd_rcvr *rcvr;
3592 unsigned char netfn;
3595 struct ipmi_user *user = NULL;
3596 struct ipmi_ipmb_addr *ipmb_addr;
3597 struct ipmi_recv_msg *recv_msg;
3599 if (msg->rsp_size < 10) {
3600 /* Message not big enough, just ignore it. */
3601 ipmi_inc_stat(intf, invalid_commands);
3605 if (msg->rsp[2] != 0) {
3606 /* An error getting the response, just ignore it. */
3610 netfn = msg->rsp[4] >> 2;
3612 chan = msg->rsp[3] & 0xf;
3615 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
3618 kref_get(&user->refcount);
3624 /* We didn't find a user, deliver an error response. */
3625 ipmi_inc_stat(intf, unhandled_commands);
3627 msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
3628 msg->data[1] = IPMI_SEND_MSG_CMD;
3629 msg->data[2] = msg->rsp[3];
3630 msg->data[3] = msg->rsp[6];
3631 msg->data[4] = ((netfn + 1) << 2) | (msg->rsp[7] & 0x3);
3632 msg->data[5] = ipmb_checksum(&msg->data[3], 2);
3633 msg->data[6] = intf->addrinfo[msg->rsp[3] & 0xf].address;
3635 msg->data[7] = (msg->rsp[7] & 0xfc) | (msg->rsp[4] & 0x3);
3636 msg->data[8] = msg->rsp[8]; /* cmd */
3637 msg->data[9] = IPMI_INVALID_CMD_COMPLETION_CODE;
3638 msg->data[10] = ipmb_checksum(&msg->data[6], 4);
3639 msg->data_size = 11;
3641 ipmi_debug_msg("Invalid command:", msg->data, msg->data_size);
3644 if (!intf->in_shutdown) {
3645 smi_send(intf, intf->handlers, msg, 0);
3647 * We used the message, so return the value
3648 * that causes it to not be freed or
3655 recv_msg = ipmi_alloc_recv_msg();
3658 * We couldn't allocate memory for the
3659 * message, so requeue it for handling
3663 kref_put(&user->refcount, free_user);
3665 /* Extract the source address from the data. */
3666 ipmb_addr = (struct ipmi_ipmb_addr *) &recv_msg->addr;
3667 ipmb_addr->addr_type = IPMI_IPMB_ADDR_TYPE;
3668 ipmb_addr->slave_addr = msg->rsp[6];
3669 ipmb_addr->lun = msg->rsp[7] & 3;
3670 ipmb_addr->channel = msg->rsp[3] & 0xf;
3673 * Extract the rest of the message information
3674 * from the IPMB header.
3676 recv_msg->user = user;
3677 recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
3678 recv_msg->msgid = msg->rsp[7] >> 2;
3679 recv_msg->msg.netfn = msg->rsp[4] >> 2;
3680 recv_msg->msg.cmd = msg->rsp[8];
3681 recv_msg->msg.data = recv_msg->msg_data;
3684 * We chop off 10, not 9 bytes because the checksum
3685 * at the end also needs to be removed.
3687 recv_msg->msg.data_len = msg->rsp_size - 10;
3688 memcpy(recv_msg->msg_data, &msg->rsp[9],
3689 msg->rsp_size - 10);
3690 if (deliver_response(intf, recv_msg))
3691 ipmi_inc_stat(intf, unhandled_commands);
3693 ipmi_inc_stat(intf, handled_commands);
3700 static int handle_lan_get_msg_rsp(struct ipmi_smi *intf,
3701 struct ipmi_smi_msg *msg)
3703 struct ipmi_lan_addr lan_addr;
3704 struct ipmi_recv_msg *recv_msg;
3708 * This is 13, not 12, because the response must contain a
3711 if (msg->rsp_size < 13) {
3712 /* Message not big enough, just ignore it. */
3713 ipmi_inc_stat(intf, invalid_lan_responses);
3717 if (msg->rsp[2] != 0) {
3718 /* An error getting the response, just ignore it. */
3722 lan_addr.addr_type = IPMI_LAN_ADDR_TYPE;
3723 lan_addr.session_handle = msg->rsp[4];
3724 lan_addr.remote_SWID = msg->rsp[8];
3725 lan_addr.local_SWID = msg->rsp[5];
3726 lan_addr.channel = msg->rsp[3] & 0x0f;
3727 lan_addr.privilege = msg->rsp[3] >> 4;
3728 lan_addr.lun = msg->rsp[9] & 3;
3731 * It's a response from a remote entity. Look up the sequence
3732 * number and handle the response.
3734 if (intf_find_seq(intf,
3738 (msg->rsp[6] >> 2) & (~1),
3739 (struct ipmi_addr *) &lan_addr,
3742 * We were unable to find the sequence number,
3743 * so just nuke the message.
3745 ipmi_inc_stat(intf, unhandled_lan_responses);
3749 memcpy(recv_msg->msg_data, &msg->rsp[11], msg->rsp_size - 11);
3751 * The other fields matched, so no need to set them, except
3752 * for netfn, which needs to be the response that was
3753 * returned, not the request value.
3755 recv_msg->msg.netfn = msg->rsp[6] >> 2;
3756 recv_msg->msg.data = recv_msg->msg_data;
3757 recv_msg->msg.data_len = msg->rsp_size - 12;
3758 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
3759 if (deliver_response(intf, recv_msg))
3760 ipmi_inc_stat(intf, unhandled_lan_responses);
3762 ipmi_inc_stat(intf, handled_lan_responses);
3767 static int handle_lan_get_msg_cmd(struct ipmi_smi *intf,
3768 struct ipmi_smi_msg *msg)
3770 struct cmd_rcvr *rcvr;
3772 unsigned char netfn;
3775 struct ipmi_user *user = NULL;
3776 struct ipmi_lan_addr *lan_addr;
3777 struct ipmi_recv_msg *recv_msg;
3779 if (msg->rsp_size < 12) {
3780 /* Message not big enough, just ignore it. */
3781 ipmi_inc_stat(intf, invalid_commands);
3785 if (msg->rsp[2] != 0) {
3786 /* An error getting the response, just ignore it. */
3790 netfn = msg->rsp[6] >> 2;
3792 chan = msg->rsp[3] & 0xf;
3795 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
3798 kref_get(&user->refcount);
3804 /* We didn't find a user, just give up. */
3805 ipmi_inc_stat(intf, unhandled_commands);
3808 * Don't do anything with these messages, just allow
3813 recv_msg = ipmi_alloc_recv_msg();
3816 * We couldn't allocate memory for the
3817 * message, so requeue it for handling later.
3820 kref_put(&user->refcount, free_user);
3822 /* Extract the source address from the data. */
3823 lan_addr = (struct ipmi_lan_addr *) &recv_msg->addr;
3824 lan_addr->addr_type = IPMI_LAN_ADDR_TYPE;
3825 lan_addr->session_handle = msg->rsp[4];
3826 lan_addr->remote_SWID = msg->rsp[8];
3827 lan_addr->local_SWID = msg->rsp[5];
3828 lan_addr->lun = msg->rsp[9] & 3;
3829 lan_addr->channel = msg->rsp[3] & 0xf;
3830 lan_addr->privilege = msg->rsp[3] >> 4;
3833 * Extract the rest of the message information
3834 * from the IPMB header.
3836 recv_msg->user = user;
3837 recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
3838 recv_msg->msgid = msg->rsp[9] >> 2;
3839 recv_msg->msg.netfn = msg->rsp[6] >> 2;
3840 recv_msg->msg.cmd = msg->rsp[10];
3841 recv_msg->msg.data = recv_msg->msg_data;
3844 * We chop off 12, not 11 bytes because the checksum
3845 * at the end also needs to be removed.
3847 recv_msg->msg.data_len = msg->rsp_size - 12;
3848 memcpy(recv_msg->msg_data, &msg->rsp[11],
3849 msg->rsp_size - 12);
3850 if (deliver_response(intf, recv_msg))
3851 ipmi_inc_stat(intf, unhandled_commands);
3853 ipmi_inc_stat(intf, handled_commands);
3861 * This routine will handle "Get Message" command responses with
3862 * channels that use an OEM Medium. The message format belongs to
3863 * the OEM. See IPMI 2.0 specification, Chapter 6 and
3864 * Chapter 22, sections 22.6 and 22.24 for more details.
3866 static int handle_oem_get_msg_cmd(struct ipmi_smi *intf,
3867 struct ipmi_smi_msg *msg)
3869 struct cmd_rcvr *rcvr;
3871 unsigned char netfn;
3874 struct ipmi_user *user = NULL;
3875 struct ipmi_system_interface_addr *smi_addr;
3876 struct ipmi_recv_msg *recv_msg;
3879 * We expect the OEM SW to perform error checking
3880 * so we just do some basic sanity checks
3882 if (msg->rsp_size < 4) {
3883 /* Message not big enough, just ignore it. */
3884 ipmi_inc_stat(intf, invalid_commands);
3888 if (msg->rsp[2] != 0) {
3889 /* An error getting the response, just ignore it. */
3894 * This is an OEM Message so the OEM needs to know how
3895 * handle the message. We do no interpretation.
3897 netfn = msg->rsp[0] >> 2;
3899 chan = msg->rsp[3] & 0xf;
3902 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
3905 kref_get(&user->refcount);
3911 /* We didn't find a user, just give up. */
3912 ipmi_inc_stat(intf, unhandled_commands);
3915 * Don't do anything with these messages, just allow
3921 recv_msg = ipmi_alloc_recv_msg();
3924 * We couldn't allocate memory for the
3925 * message, so requeue it for handling
3929 kref_put(&user->refcount, free_user);
3932 * OEM Messages are expected to be delivered via
3933 * the system interface to SMS software. We might
3934 * need to visit this again depending on OEM
3937 smi_addr = ((struct ipmi_system_interface_addr *)
3939 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3940 smi_addr->channel = IPMI_BMC_CHANNEL;
3941 smi_addr->lun = msg->rsp[0] & 3;
3943 recv_msg->user = user;
3944 recv_msg->user_msg_data = NULL;
3945 recv_msg->recv_type = IPMI_OEM_RECV_TYPE;
3946 recv_msg->msg.netfn = msg->rsp[0] >> 2;
3947 recv_msg->msg.cmd = msg->rsp[1];
3948 recv_msg->msg.data = recv_msg->msg_data;
3951 * The message starts at byte 4 which follows the
3952 * the Channel Byte in the "GET MESSAGE" command
3954 recv_msg->msg.data_len = msg->rsp_size - 4;
3955 memcpy(recv_msg->msg_data, &msg->rsp[4],
3957 if (deliver_response(intf, recv_msg))
3958 ipmi_inc_stat(intf, unhandled_commands);
3960 ipmi_inc_stat(intf, handled_commands);
3967 static void copy_event_into_recv_msg(struct ipmi_recv_msg *recv_msg,
3968 struct ipmi_smi_msg *msg)
3970 struct ipmi_system_interface_addr *smi_addr;
3972 recv_msg->msgid = 0;
3973 smi_addr = (struct ipmi_system_interface_addr *) &recv_msg->addr;
3974 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3975 smi_addr->channel = IPMI_BMC_CHANNEL;
3976 smi_addr->lun = msg->rsp[0] & 3;
3977 recv_msg->recv_type = IPMI_ASYNC_EVENT_RECV_TYPE;
3978 recv_msg->msg.netfn = msg->rsp[0] >> 2;
3979 recv_msg->msg.cmd = msg->rsp[1];
3980 memcpy(recv_msg->msg_data, &msg->rsp[3], msg->rsp_size - 3);
3981 recv_msg->msg.data = recv_msg->msg_data;
3982 recv_msg->msg.data_len = msg->rsp_size - 3;
3985 static int handle_read_event_rsp(struct ipmi_smi *intf,
3986 struct ipmi_smi_msg *msg)
3988 struct ipmi_recv_msg *recv_msg, *recv_msg2;
3989 struct list_head msgs;
3990 struct ipmi_user *user;
3991 int rv = 0, deliver_count = 0, index;
3992 unsigned long flags;
3994 if (msg->rsp_size < 19) {
3995 /* Message is too small to be an IPMB event. */
3996 ipmi_inc_stat(intf, invalid_events);
4000 if (msg->rsp[2] != 0) {
4001 /* An error getting the event, just ignore it. */
4005 INIT_LIST_HEAD(&msgs);
4007 spin_lock_irqsave(&intf->events_lock, flags);
4009 ipmi_inc_stat(intf, events);
4012 * Allocate and fill in one message for every user that is
4015 index = srcu_read_lock(&intf->users_srcu);
4016 list_for_each_entry_rcu(user, &intf->users, link) {
4017 if (!user->gets_events)
4020 recv_msg = ipmi_alloc_recv_msg();
4023 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs,
4025 list_del(&recv_msg->link);
4026 ipmi_free_recv_msg(recv_msg);
4029 * We couldn't allocate memory for the
4030 * message, so requeue it for handling
4039 copy_event_into_recv_msg(recv_msg, msg);
4040 recv_msg->user = user;
4041 kref_get(&user->refcount);
4042 list_add_tail(&recv_msg->link, &msgs);
4044 srcu_read_unlock(&intf->users_srcu, index);
4046 if (deliver_count) {
4047 /* Now deliver all the messages. */
4048 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs, link) {
4049 list_del(&recv_msg->link);
4050 deliver_local_response(intf, recv_msg);
4052 } else if (intf->waiting_events_count < MAX_EVENTS_IN_QUEUE) {
4054 * No one to receive the message, put it in queue if there's
4055 * not already too many things in the queue.
4057 recv_msg = ipmi_alloc_recv_msg();
4060 * We couldn't allocate memory for the
4061 * message, so requeue it for handling
4068 copy_event_into_recv_msg(recv_msg, msg);
4069 list_add_tail(&recv_msg->link, &intf->waiting_events);
4070 intf->waiting_events_count++;
4071 } else if (!intf->event_msg_printed) {
4073 * There's too many things in the queue, discard this
4076 dev_warn(intf->si_dev,
4077 "Event queue full, discarding incoming events\n");
4078 intf->event_msg_printed = 1;
4082 spin_unlock_irqrestore(&intf->events_lock, flags);
4087 static int handle_bmc_rsp(struct ipmi_smi *intf,
4088 struct ipmi_smi_msg *msg)
4090 struct ipmi_recv_msg *recv_msg;
4091 struct ipmi_system_interface_addr *smi_addr;
4093 recv_msg = (struct ipmi_recv_msg *) msg->user_data;
4094 if (recv_msg == NULL) {
4095 dev_warn(intf->si_dev,
4096 "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");
4100 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
4101 recv_msg->msgid = msg->msgid;
4102 smi_addr = ((struct ipmi_system_interface_addr *)
4104 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
4105 smi_addr->channel = IPMI_BMC_CHANNEL;
4106 smi_addr->lun = msg->rsp[0] & 3;
4107 recv_msg->msg.netfn = msg->rsp[0] >> 2;
4108 recv_msg->msg.cmd = msg->rsp[1];
4109 memcpy(recv_msg->msg_data, &msg->rsp[2], msg->rsp_size - 2);
4110 recv_msg->msg.data = recv_msg->msg_data;
4111 recv_msg->msg.data_len = msg->rsp_size - 2;
4112 deliver_local_response(intf, recv_msg);
4118 * Handle a received message. Return 1 if the message should be requeued,
4119 * 0 if the message should be freed, or -1 if the message should not
4120 * be freed or requeued.
4122 static int handle_one_recv_msg(struct ipmi_smi *intf,
4123 struct ipmi_smi_msg *msg)
4128 ipmi_debug_msg("Recv:", msg->rsp, msg->rsp_size);
4129 if (msg->rsp_size < 2) {
4130 /* Message is too small to be correct. */
4131 dev_warn(intf->si_dev,
4132 "BMC returned too small a message for netfn %x cmd %x, got %d bytes\n",
4133 (msg->data[0] >> 2) | 1, msg->data[1], msg->rsp_size);
4135 /* Generate an error response for the message. */
4136 msg->rsp[0] = msg->data[0] | (1 << 2);
4137 msg->rsp[1] = msg->data[1];
4138 msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
4140 } else if (((msg->rsp[0] >> 2) != ((msg->data[0] >> 2) | 1))
4141 || (msg->rsp[1] != msg->data[1])) {
4143 * The NetFN and Command in the response is not even
4144 * marginally correct.
4146 dev_warn(intf->si_dev,
4147 "BMC returned incorrect response, expected netfn %x cmd %x, got netfn %x cmd %x\n",
4148 (msg->data[0] >> 2) | 1, msg->data[1],
4149 msg->rsp[0] >> 2, msg->rsp[1]);
4151 /* Generate an error response for the message. */
4152 msg->rsp[0] = msg->data[0] | (1 << 2);
4153 msg->rsp[1] = msg->data[1];
4154 msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
4158 if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
4159 && (msg->rsp[1] == IPMI_SEND_MSG_CMD)
4160 && (msg->user_data != NULL)) {
4162 * It's a response to a response we sent. For this we
4163 * deliver a send message response to the user.
4165 struct ipmi_recv_msg *recv_msg = msg->user_data;
4168 if (msg->rsp_size < 2)
4169 /* Message is too small to be correct. */
4172 chan = msg->data[2] & 0x0f;
4173 if (chan >= IPMI_MAX_CHANNELS)
4174 /* Invalid channel number */
4180 recv_msg->recv_type = IPMI_RESPONSE_RESPONSE_TYPE;
4181 recv_msg->msg.data = recv_msg->msg_data;
4182 recv_msg->msg.data_len = 1;
4183 recv_msg->msg_data[0] = msg->rsp[2];
4184 deliver_local_response(intf, recv_msg);
4185 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
4186 && (msg->rsp[1] == IPMI_GET_MSG_CMD)) {
4187 struct ipmi_channel *chans;
4189 /* It's from the receive queue. */
4190 chan = msg->rsp[3] & 0xf;
4191 if (chan >= IPMI_MAX_CHANNELS) {
4192 /* Invalid channel number */
4198 * We need to make sure the channels have been initialized.
4199 * The channel_handler routine will set the "curr_channel"
4200 * equal to or greater than IPMI_MAX_CHANNELS when all the
4201 * channels for this interface have been initialized.
4203 if (!intf->channels_ready) {
4204 requeue = 0; /* Throw the message away */
4208 chans = READ_ONCE(intf->channel_list)->c;
4210 switch (chans[chan].medium) {
4211 case IPMI_CHANNEL_MEDIUM_IPMB:
4212 if (msg->rsp[4] & 0x04) {
4214 * It's a response, so find the
4215 * requesting message and send it up.
4217 requeue = handle_ipmb_get_msg_rsp(intf, msg);
4220 * It's a command to the SMS from some other
4221 * entity. Handle that.
4223 requeue = handle_ipmb_get_msg_cmd(intf, msg);
4227 case IPMI_CHANNEL_MEDIUM_8023LAN:
4228 case IPMI_CHANNEL_MEDIUM_ASYNC:
4229 if (msg->rsp[6] & 0x04) {
4231 * It's a response, so find the
4232 * requesting message and send it up.
4234 requeue = handle_lan_get_msg_rsp(intf, msg);
4237 * It's a command to the SMS from some other
4238 * entity. Handle that.
4240 requeue = handle_lan_get_msg_cmd(intf, msg);
4245 /* Check for OEM Channels. Clients had better
4246 register for these commands. */
4247 if ((chans[chan].medium >= IPMI_CHANNEL_MEDIUM_OEM_MIN)
4248 && (chans[chan].medium
4249 <= IPMI_CHANNEL_MEDIUM_OEM_MAX)) {
4250 requeue = handle_oem_get_msg_cmd(intf, msg);
4253 * We don't handle the channel type, so just
4260 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
4261 && (msg->rsp[1] == IPMI_READ_EVENT_MSG_BUFFER_CMD)) {
4262 /* It's an asynchronous event. */
4263 requeue = handle_read_event_rsp(intf, msg);
4265 /* It's a response from the local BMC. */
4266 requeue = handle_bmc_rsp(intf, msg);
4274 * If there are messages in the queue or pretimeouts, handle them.
4276 static void handle_new_recv_msgs(struct ipmi_smi *intf)
4278 struct ipmi_smi_msg *smi_msg;
4279 unsigned long flags = 0;
4281 int run_to_completion = intf->run_to_completion;
4283 /* See if any waiting messages need to be processed. */
4284 if (!run_to_completion)
4285 spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags);
4286 while (!list_empty(&intf->waiting_rcv_msgs)) {
4287 smi_msg = list_entry(intf->waiting_rcv_msgs.next,
4288 struct ipmi_smi_msg, link);
4289 list_del(&smi_msg->link);
4290 if (!run_to_completion)
4291 spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock,
4293 rv = handle_one_recv_msg(intf, smi_msg);
4294 if (!run_to_completion)
4295 spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags);
4298 * To preserve message order, quit if we
4299 * can't handle a message. Add the message
4300 * back at the head, this is safe because this
4301 * tasklet is the only thing that pulls the
4304 list_add(&smi_msg->link, &intf->waiting_rcv_msgs);
4308 /* Message handled */
4309 ipmi_free_smi_msg(smi_msg);
4310 /* If rv < 0, fatal error, del but don't free. */
4313 if (!run_to_completion)
4314 spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock, flags);
4317 * If the pretimout count is non-zero, decrement one from it and
4318 * deliver pretimeouts to all the users.
4320 if (atomic_add_unless(&intf->watchdog_pretimeouts_to_deliver, -1, 0)) {
4321 struct ipmi_user *user;
4324 index = srcu_read_lock(&intf->users_srcu);
4325 list_for_each_entry_rcu(user, &intf->users, link) {
4326 if (user->handler->ipmi_watchdog_pretimeout)
4327 user->handler->ipmi_watchdog_pretimeout(
4328 user->handler_data);
4330 srcu_read_unlock(&intf->users_srcu, index);
4334 static void smi_recv_tasklet(unsigned long val)
4336 unsigned long flags = 0; /* keep us warning-free. */
4337 struct ipmi_smi *intf = (struct ipmi_smi *) val;
4338 int run_to_completion = intf->run_to_completion;
4339 struct ipmi_smi_msg *newmsg = NULL;
4342 * Start the next message if available.
4344 * Do this here, not in the actual receiver, because we may deadlock
4345 * because the lower layer is allowed to hold locks while calling
4351 if (!run_to_completion)
4352 spin_lock_irqsave(&intf->xmit_msgs_lock, flags);
4353 if (intf->curr_msg == NULL && !intf->in_shutdown) {
4354 struct list_head *entry = NULL;
4356 /* Pick the high priority queue first. */
4357 if (!list_empty(&intf->hp_xmit_msgs))
4358 entry = intf->hp_xmit_msgs.next;
4359 else if (!list_empty(&intf->xmit_msgs))
4360 entry = intf->xmit_msgs.next;
4364 newmsg = list_entry(entry, struct ipmi_smi_msg, link);
4365 intf->curr_msg = newmsg;
4368 if (!run_to_completion)
4369 spin_unlock_irqrestore(&intf->xmit_msgs_lock, flags);
4371 intf->handlers->sender(intf->send_info, newmsg);
4375 handle_new_recv_msgs(intf);
4378 /* Handle a new message from the lower layer. */
4379 void ipmi_smi_msg_received(struct ipmi_smi *intf,
4380 struct ipmi_smi_msg *msg)
4382 unsigned long flags = 0; /* keep us warning-free. */
4383 int run_to_completion = intf->run_to_completion;
4385 if ((msg->data_size >= 2)
4386 && (msg->data[0] == (IPMI_NETFN_APP_REQUEST << 2))
4387 && (msg->data[1] == IPMI_SEND_MSG_CMD)
4388 && (msg->user_data == NULL)) {
4390 if (intf->in_shutdown)
4394 * This is the local response to a command send, start
4395 * the timer for these. The user_data will not be
4396 * NULL if this is a response send, and we will let
4397 * response sends just go through.
4401 * Check for errors, if we get certain errors (ones
4402 * that mean basically we can try again later), we
4403 * ignore them and start the timer. Otherwise we
4404 * report the error immediately.
4406 if ((msg->rsp_size >= 3) && (msg->rsp[2] != 0)
4407 && (msg->rsp[2] != IPMI_NODE_BUSY_ERR)
4408 && (msg->rsp[2] != IPMI_LOST_ARBITRATION_ERR)
4409 && (msg->rsp[2] != IPMI_BUS_ERR)
4410 && (msg->rsp[2] != IPMI_NAK_ON_WRITE_ERR)) {
4411 int ch = msg->rsp[3] & 0xf;
4412 struct ipmi_channel *chans;
4414 /* Got an error sending the message, handle it. */
4416 chans = READ_ONCE(intf->channel_list)->c;
4417 if ((chans[ch].medium == IPMI_CHANNEL_MEDIUM_8023LAN)
4418 || (chans[ch].medium == IPMI_CHANNEL_MEDIUM_ASYNC))
4419 ipmi_inc_stat(intf, sent_lan_command_errs);
4421 ipmi_inc_stat(intf, sent_ipmb_command_errs);
4422 intf_err_seq(intf, msg->msgid, msg->rsp[2]);
4424 /* The message was sent, start the timer. */
4425 intf_start_seq_timer(intf, msg->msgid);
4428 ipmi_free_smi_msg(msg);
4431 * To preserve message order, we keep a queue and deliver from
4434 if (!run_to_completion)
4435 spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags);
4436 list_add_tail(&msg->link, &intf->waiting_rcv_msgs);
4437 if (!run_to_completion)
4438 spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock,
4442 if (!run_to_completion)
4443 spin_lock_irqsave(&intf->xmit_msgs_lock, flags);
4445 * We can get an asynchronous event or receive message in addition
4446 * to commands we send.
4448 if (msg == intf->curr_msg)
4449 intf->curr_msg = NULL;
4450 if (!run_to_completion)
4451 spin_unlock_irqrestore(&intf->xmit_msgs_lock, flags);
4453 if (run_to_completion)
4454 smi_recv_tasklet((unsigned long) intf);
4456 tasklet_schedule(&intf->recv_tasklet);
4458 EXPORT_SYMBOL(ipmi_smi_msg_received);
4460 void ipmi_smi_watchdog_pretimeout(struct ipmi_smi *intf)
4462 if (intf->in_shutdown)
4465 atomic_set(&intf->watchdog_pretimeouts_to_deliver, 1);
4466 tasklet_schedule(&intf->recv_tasklet);
4468 EXPORT_SYMBOL(ipmi_smi_watchdog_pretimeout);
4470 static struct ipmi_smi_msg *
4471 smi_from_recv_msg(struct ipmi_smi *intf, struct ipmi_recv_msg *recv_msg,
4472 unsigned char seq, long seqid)
4474 struct ipmi_smi_msg *smi_msg = ipmi_alloc_smi_msg();
4477 * If we can't allocate the message, then just return, we
4478 * get 4 retries, so this should be ok.
4482 memcpy(smi_msg->data, recv_msg->msg.data, recv_msg->msg.data_len);
4483 smi_msg->data_size = recv_msg->msg.data_len;
4484 smi_msg->msgid = STORE_SEQ_IN_MSGID(seq, seqid);
4486 ipmi_debug_msg("Resend: ", smi_msg->data, smi_msg->data_size);
4491 static void check_msg_timeout(struct ipmi_smi *intf, struct seq_table *ent,
4492 struct list_head *timeouts,
4493 unsigned long timeout_period,
4494 int slot, unsigned long *flags,
4495 unsigned int *waiting_msgs)
4497 struct ipmi_recv_msg *msg;
4499 if (intf->in_shutdown)
4505 if (timeout_period < ent->timeout) {
4506 ent->timeout -= timeout_period;
4511 if (ent->retries_left == 0) {
4512 /* The message has used all its retries. */
4514 msg = ent->recv_msg;
4515 list_add_tail(&msg->link, timeouts);
4517 ipmi_inc_stat(intf, timed_out_ipmb_broadcasts);
4518 else if (is_lan_addr(&ent->recv_msg->addr))
4519 ipmi_inc_stat(intf, timed_out_lan_commands);
4521 ipmi_inc_stat(intf, timed_out_ipmb_commands);
4523 struct ipmi_smi_msg *smi_msg;
4524 /* More retries, send again. */
4529 * Start with the max timer, set to normal timer after
4530 * the message is sent.
4532 ent->timeout = MAX_MSG_TIMEOUT;
4533 ent->retries_left--;
4534 smi_msg = smi_from_recv_msg(intf, ent->recv_msg, slot,
4537 if (is_lan_addr(&ent->recv_msg->addr))
4539 dropped_rexmit_lan_commands);
4542 dropped_rexmit_ipmb_commands);
4546 spin_unlock_irqrestore(&intf->seq_lock, *flags);
4549 * Send the new message. We send with a zero
4550 * priority. It timed out, I doubt time is that
4551 * critical now, and high priority messages are really
4552 * only for messages to the local MC, which don't get
4555 if (intf->handlers) {
4556 if (is_lan_addr(&ent->recv_msg->addr))
4558 retransmitted_lan_commands);
4561 retransmitted_ipmb_commands);
4563 smi_send(intf, intf->handlers, smi_msg, 0);
4565 ipmi_free_smi_msg(smi_msg);
4567 spin_lock_irqsave(&intf->seq_lock, *flags);
4571 static unsigned int ipmi_timeout_handler(struct ipmi_smi *intf,
4572 unsigned long timeout_period)
4574 struct list_head timeouts;
4575 struct ipmi_recv_msg *msg, *msg2;
4576 unsigned long flags;
4578 unsigned int waiting_msgs = 0;
4580 if (!intf->bmc_registered) {
4581 kref_get(&intf->refcount);
4582 if (!schedule_work(&intf->bmc_reg_work)) {
4583 kref_put(&intf->refcount, intf_free);
4589 * Go through the seq table and find any messages that
4590 * have timed out, putting them in the timeouts
4593 INIT_LIST_HEAD(&timeouts);
4594 spin_lock_irqsave(&intf->seq_lock, flags);
4595 if (intf->ipmb_maintenance_mode_timeout) {
4596 if (intf->ipmb_maintenance_mode_timeout <= timeout_period)
4597 intf->ipmb_maintenance_mode_timeout = 0;
4599 intf->ipmb_maintenance_mode_timeout -= timeout_period;
4601 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++)
4602 check_msg_timeout(intf, &intf->seq_table[i],
4603 &timeouts, timeout_period, i,
4604 &flags, &waiting_msgs);
4605 spin_unlock_irqrestore(&intf->seq_lock, flags);
4607 list_for_each_entry_safe(msg, msg2, &timeouts, link)
4608 deliver_err_response(intf, msg, IPMI_TIMEOUT_COMPLETION_CODE);
4611 * Maintenance mode handling. Check the timeout
4612 * optimistically before we claim the lock. It may
4613 * mean a timeout gets missed occasionally, but that
4614 * only means the timeout gets extended by one period
4615 * in that case. No big deal, and it avoids the lock
4618 if (intf->auto_maintenance_timeout > 0) {
4619 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
4620 if (intf->auto_maintenance_timeout > 0) {
4621 intf->auto_maintenance_timeout
4623 if (!intf->maintenance_mode
4624 && (intf->auto_maintenance_timeout <= 0)) {
4625 intf->maintenance_mode_enable = false;
4626 maintenance_mode_update(intf);
4629 spin_unlock_irqrestore(&intf->maintenance_mode_lock,
4633 tasklet_schedule(&intf->recv_tasklet);
4635 return waiting_msgs;
4638 static void ipmi_request_event(struct ipmi_smi *intf)
4640 /* No event requests when in maintenance mode. */
4641 if (intf->maintenance_mode_enable)
4644 if (!intf->in_shutdown)
4645 intf->handlers->request_events(intf->send_info);
4648 static struct timer_list ipmi_timer;
4650 static atomic_t stop_operation;
4652 static void ipmi_timeout(struct timer_list *unused)
4654 struct ipmi_smi *intf;
4657 if (atomic_read(&stop_operation))
4660 index = srcu_read_lock(&ipmi_interfaces_srcu);
4661 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
4664 if (atomic_read(&intf->event_waiters)) {
4665 intf->ticks_to_req_ev--;
4666 if (intf->ticks_to_req_ev == 0) {
4667 ipmi_request_event(intf);
4668 intf->ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
4673 lnt += ipmi_timeout_handler(intf, IPMI_TIMEOUT_TIME);
4676 if (lnt != intf->last_needs_timer &&
4677 intf->handlers->set_need_watch)
4678 intf->handlers->set_need_watch(intf->send_info, lnt);
4679 intf->last_needs_timer = lnt;
4683 srcu_read_unlock(&ipmi_interfaces_srcu, index);
4686 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
4689 static void need_waiter(struct ipmi_smi *intf)
4691 /* Racy, but worst case we start the timer twice. */
4692 if (!timer_pending(&ipmi_timer))
4693 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
4696 static atomic_t smi_msg_inuse_count = ATOMIC_INIT(0);
4697 static atomic_t recv_msg_inuse_count = ATOMIC_INIT(0);
4699 static void free_smi_msg(struct ipmi_smi_msg *msg)
4701 atomic_dec(&smi_msg_inuse_count);
4705 struct ipmi_smi_msg *ipmi_alloc_smi_msg(void)
4707 struct ipmi_smi_msg *rv;
4708 rv = kmalloc(sizeof(struct ipmi_smi_msg), GFP_ATOMIC);
4710 rv->done = free_smi_msg;
4711 rv->user_data = NULL;
4712 atomic_inc(&smi_msg_inuse_count);
4716 EXPORT_SYMBOL(ipmi_alloc_smi_msg);
4718 static void free_recv_msg(struct ipmi_recv_msg *msg)
4720 atomic_dec(&recv_msg_inuse_count);
4724 static struct ipmi_recv_msg *ipmi_alloc_recv_msg(void)
4726 struct ipmi_recv_msg *rv;
4728 rv = kmalloc(sizeof(struct ipmi_recv_msg), GFP_ATOMIC);
4731 rv->done = free_recv_msg;
4732 atomic_inc(&recv_msg_inuse_count);
4737 void ipmi_free_recv_msg(struct ipmi_recv_msg *msg)
4740 kref_put(&msg->user->refcount, free_user);
4743 EXPORT_SYMBOL(ipmi_free_recv_msg);
4745 static atomic_t panic_done_count = ATOMIC_INIT(0);
4747 static void dummy_smi_done_handler(struct ipmi_smi_msg *msg)
4749 atomic_dec(&panic_done_count);
4752 static void dummy_recv_done_handler(struct ipmi_recv_msg *msg)
4754 atomic_dec(&panic_done_count);
4758 * Inside a panic, send a message and wait for a response.
4760 static void ipmi_panic_request_and_wait(struct ipmi_smi *intf,
4761 struct ipmi_addr *addr,
4762 struct kernel_ipmi_msg *msg)
4764 struct ipmi_smi_msg smi_msg;
4765 struct ipmi_recv_msg recv_msg;
4768 smi_msg.done = dummy_smi_done_handler;
4769 recv_msg.done = dummy_recv_done_handler;
4770 atomic_add(2, &panic_done_count);
4771 rv = i_ipmi_request(NULL,
4780 intf->addrinfo[0].address,
4781 intf->addrinfo[0].lun,
4782 0, 1); /* Don't retry, and don't wait. */
4784 atomic_sub(2, &panic_done_count);
4785 else if (intf->handlers->flush_messages)
4786 intf->handlers->flush_messages(intf->send_info);
4788 while (atomic_read(&panic_done_count) != 0)
4792 static void event_receiver_fetcher(struct ipmi_smi *intf,
4793 struct ipmi_recv_msg *msg)
4795 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
4796 && (msg->msg.netfn == IPMI_NETFN_SENSOR_EVENT_RESPONSE)
4797 && (msg->msg.cmd == IPMI_GET_EVENT_RECEIVER_CMD)
4798 && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) {
4799 /* A get event receiver command, save it. */
4800 intf->event_receiver = msg->msg.data[1];
4801 intf->event_receiver_lun = msg->msg.data[2] & 0x3;
4805 static void device_id_fetcher(struct ipmi_smi *intf, struct ipmi_recv_msg *msg)
4807 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
4808 && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
4809 && (msg->msg.cmd == IPMI_GET_DEVICE_ID_CMD)
4810 && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) {
4812 * A get device id command, save if we are an event
4813 * receiver or generator.
4815 intf->local_sel_device = (msg->msg.data[6] >> 2) & 1;
4816 intf->local_event_generator = (msg->msg.data[6] >> 5) & 1;
4820 static void send_panic_events(struct ipmi_smi *intf, char *str)
4822 struct kernel_ipmi_msg msg;
4823 unsigned char data[16];
4824 struct ipmi_system_interface_addr *si;
4825 struct ipmi_addr addr;
4827 struct ipmi_ipmb_addr *ipmb;
4830 if (ipmi_send_panic_event == IPMI_SEND_PANIC_EVENT_NONE)
4833 si = (struct ipmi_system_interface_addr *) &addr;
4834 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
4835 si->channel = IPMI_BMC_CHANNEL;
4838 /* Fill in an event telling that we have failed. */
4839 msg.netfn = 0x04; /* Sensor or Event. */
4840 msg.cmd = 2; /* Platform event command. */
4843 data[0] = 0x41; /* Kernel generator ID, IPMI table 5-4 */
4844 data[1] = 0x03; /* This is for IPMI 1.0. */
4845 data[2] = 0x20; /* OS Critical Stop, IPMI table 36-3 */
4846 data[4] = 0x6f; /* Sensor specific, IPMI table 36-1 */
4847 data[5] = 0xa1; /* Runtime stop OEM bytes 2 & 3. */
4850 * Put a few breadcrumbs in. Hopefully later we can add more things
4851 * to make the panic events more useful.
4859 /* Send the event announcing the panic. */
4860 ipmi_panic_request_and_wait(intf, &addr, &msg);
4863 * On every interface, dump a bunch of OEM event holding the
4866 if (ipmi_send_panic_event != IPMI_SEND_PANIC_EVENT_STRING || !str)
4870 * intf_num is used as an marker to tell if the
4871 * interface is valid. Thus we need a read barrier to
4872 * make sure data fetched before checking intf_num
4878 * First job here is to figure out where to send the
4879 * OEM events. There's no way in IPMI to send OEM
4880 * events using an event send command, so we have to
4881 * find the SEL to put them in and stick them in
4885 /* Get capabilities from the get device id. */
4886 intf->local_sel_device = 0;
4887 intf->local_event_generator = 0;
4888 intf->event_receiver = 0;
4890 /* Request the device info from the local MC. */
4891 msg.netfn = IPMI_NETFN_APP_REQUEST;
4892 msg.cmd = IPMI_GET_DEVICE_ID_CMD;
4895 intf->null_user_handler = device_id_fetcher;
4896 ipmi_panic_request_and_wait(intf, &addr, &msg);
4898 if (intf->local_event_generator) {
4899 /* Request the event receiver from the local MC. */
4900 msg.netfn = IPMI_NETFN_SENSOR_EVENT_REQUEST;
4901 msg.cmd = IPMI_GET_EVENT_RECEIVER_CMD;
4904 intf->null_user_handler = event_receiver_fetcher;
4905 ipmi_panic_request_and_wait(intf, &addr, &msg);
4907 intf->null_user_handler = NULL;
4910 * Validate the event receiver. The low bit must not
4911 * be 1 (it must be a valid IPMB address), it cannot
4912 * be zero, and it must not be my address.
4914 if (((intf->event_receiver & 1) == 0)
4915 && (intf->event_receiver != 0)
4916 && (intf->event_receiver != intf->addrinfo[0].address)) {
4918 * The event receiver is valid, send an IPMB
4921 ipmb = (struct ipmi_ipmb_addr *) &addr;
4922 ipmb->addr_type = IPMI_IPMB_ADDR_TYPE;
4923 ipmb->channel = 0; /* FIXME - is this right? */
4924 ipmb->lun = intf->event_receiver_lun;
4925 ipmb->slave_addr = intf->event_receiver;
4926 } else if (intf->local_sel_device) {
4928 * The event receiver was not valid (or was
4929 * me), but I am an SEL device, just dump it
4932 si = (struct ipmi_system_interface_addr *) &addr;
4933 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
4934 si->channel = IPMI_BMC_CHANNEL;
4937 return; /* No where to send the event. */
4939 msg.netfn = IPMI_NETFN_STORAGE_REQUEST; /* Storage. */
4940 msg.cmd = IPMI_ADD_SEL_ENTRY_CMD;
4946 int size = strlen(p);
4952 data[2] = 0xf0; /* OEM event without timestamp. */
4953 data[3] = intf->addrinfo[0].address;
4954 data[4] = j++; /* sequence # */
4956 * Always give 11 bytes, so strncpy will fill
4957 * it with zeroes for me.
4959 strncpy(data+5, p, 11);
4962 ipmi_panic_request_and_wait(intf, &addr, &msg);
4966 static int has_panicked;
4968 static int panic_event(struct notifier_block *this,
4969 unsigned long event,
4972 struct ipmi_smi *intf;
4973 struct ipmi_user *user;
4979 /* For every registered interface, set it to run to completion. */
4980 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
4981 if (!intf->handlers || intf->intf_num == -1)
4982 /* Interface is not ready. */
4985 if (!intf->handlers->poll)
4989 * If we were interrupted while locking xmit_msgs_lock or
4990 * waiting_rcv_msgs_lock, the corresponding list may be
4991 * corrupted. In this case, drop items on the list for
4994 if (!spin_trylock(&intf->xmit_msgs_lock)) {
4995 INIT_LIST_HEAD(&intf->xmit_msgs);
4996 INIT_LIST_HEAD(&intf->hp_xmit_msgs);
4998 spin_unlock(&intf->xmit_msgs_lock);
5000 if (!spin_trylock(&intf->waiting_rcv_msgs_lock))
5001 INIT_LIST_HEAD(&intf->waiting_rcv_msgs);
5003 spin_unlock(&intf->waiting_rcv_msgs_lock);
5005 intf->run_to_completion = 1;
5006 if (intf->handlers->set_run_to_completion)
5007 intf->handlers->set_run_to_completion(intf->send_info,
5010 list_for_each_entry_rcu(user, &intf->users, link) {
5011 if (user->handler->ipmi_panic_handler)
5012 user->handler->ipmi_panic_handler(
5013 user->handler_data);
5016 send_panic_events(intf, ptr);
5022 /* Must be called with ipmi_interfaces_mutex held. */
5023 static int ipmi_register_driver(void)
5030 rv = driver_register(&ipmidriver.driver);
5032 pr_err("Could not register IPMI driver\n");
5034 drvregistered = true;
5038 static struct notifier_block panic_block = {
5039 .notifier_call = panic_event,
5041 .priority = 200 /* priority: INT_MAX >= x >= 0 */
5044 static int ipmi_init_msghandler(void)
5048 mutex_lock(&ipmi_interfaces_mutex);
5049 rv = ipmi_register_driver();
5055 init_srcu_struct(&ipmi_interfaces_srcu);
5057 timer_setup(&ipmi_timer, ipmi_timeout, 0);
5058 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
5060 atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
5065 mutex_unlock(&ipmi_interfaces_mutex);
5069 static int __init ipmi_init_msghandler_mod(void)
5073 pr_info("version " IPMI_DRIVER_VERSION "\n");
5075 mutex_lock(&ipmi_interfaces_mutex);
5076 rv = ipmi_register_driver();
5077 mutex_unlock(&ipmi_interfaces_mutex);
5082 static void __exit cleanup_ipmi(void)
5087 atomic_notifier_chain_unregister(&panic_notifier_list,
5091 * This can't be called if any interfaces exist, so no worry
5092 * about shutting down the interfaces.
5096 * Tell the timer to stop, then wait for it to stop. This
5097 * avoids problems with race conditions removing the timer
5100 atomic_inc(&stop_operation);
5101 del_timer_sync(&ipmi_timer);
5103 initialized = false;
5105 /* Check for buffer leaks. */
5106 count = atomic_read(&smi_msg_inuse_count);
5108 pr_warn("SMI message count %d at exit\n", count);
5109 count = atomic_read(&recv_msg_inuse_count);
5111 pr_warn("recv message count %d at exit\n", count);
5113 cleanup_srcu_struct(&ipmi_interfaces_srcu);
5116 driver_unregister(&ipmidriver.driver);
5118 module_exit(cleanup_ipmi);
5120 module_init(ipmi_init_msghandler_mod);
5121 MODULE_LICENSE("GPL");
5122 MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
5123 MODULE_DESCRIPTION("Incoming and outgoing message routing for an IPMI"
5125 MODULE_VERSION(IPMI_DRIVER_VERSION);
5126 MODULE_SOFTDEP("post: ipmi_devintf");