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 int initialized;
68 enum ipmi_panic_event_op {
69 IPMI_SEND_PANIC_EVENT_NONE,
70 IPMI_SEND_PANIC_EVENT,
71 IPMI_SEND_PANIC_EVENT_STRING
73 #ifdef CONFIG_IPMI_PANIC_STRING
74 #define IPMI_PANIC_DEFAULT IPMI_SEND_PANIC_EVENT_STRING
75 #elif defined(CONFIG_IPMI_PANIC_EVENT)
76 #define IPMI_PANIC_DEFAULT IPMI_SEND_PANIC_EVENT
78 #define IPMI_PANIC_DEFAULT IPMI_SEND_PANIC_EVENT_NONE
80 static enum ipmi_panic_event_op ipmi_send_panic_event = IPMI_PANIC_DEFAULT;
82 static int panic_op_write_handler(const char *val,
83 const struct kernel_param *kp)
88 strncpy(valcp, val, 15);
93 if (strcmp(s, "none") == 0)
94 ipmi_send_panic_event = IPMI_SEND_PANIC_EVENT_NONE;
95 else if (strcmp(s, "event") == 0)
96 ipmi_send_panic_event = IPMI_SEND_PANIC_EVENT;
97 else if (strcmp(s, "string") == 0)
98 ipmi_send_panic_event = IPMI_SEND_PANIC_EVENT_STRING;
105 static int panic_op_read_handler(char *buffer, const struct kernel_param *kp)
107 switch (ipmi_send_panic_event) {
108 case IPMI_SEND_PANIC_EVENT_NONE:
109 strcpy(buffer, "none");
112 case IPMI_SEND_PANIC_EVENT:
113 strcpy(buffer, "event");
116 case IPMI_SEND_PANIC_EVENT_STRING:
117 strcpy(buffer, "string");
121 strcpy(buffer, "???");
125 return strlen(buffer);
128 static const struct kernel_param_ops panic_op_ops = {
129 .set = panic_op_write_handler,
130 .get = panic_op_read_handler
132 module_param_cb(panic_op, &panic_op_ops, NULL, 0600);
133 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.");
136 #define MAX_EVENTS_IN_QUEUE 25
138 /* Remain in auto-maintenance mode for this amount of time (in ms). */
139 static unsigned long maintenance_mode_timeout_ms = 30000;
140 module_param(maintenance_mode_timeout_ms, ulong, 0644);
141 MODULE_PARM_DESC(maintenance_mode_timeout_ms,
142 "The time (milliseconds) after the last maintenance message that the connection stays in maintenance mode.");
145 * Don't let a message sit in a queue forever, always time it with at lest
146 * the max message timer. This is in milliseconds.
148 #define MAX_MSG_TIMEOUT 60000
151 * Timeout times below are in milliseconds, and are done off a 1
152 * second timer. So setting the value to 1000 would mean anything
153 * between 0 and 1000ms. So really the only reasonable minimum
154 * setting it 2000ms, which is between 1 and 2 seconds.
157 /* The default timeout for message retries. */
158 static unsigned long default_retry_ms = 2000;
159 module_param(default_retry_ms, ulong, 0644);
160 MODULE_PARM_DESC(default_retry_ms,
161 "The time (milliseconds) between retry sends");
163 /* The default timeout for maintenance mode message retries. */
164 static unsigned long default_maintenance_retry_ms = 3000;
165 module_param(default_maintenance_retry_ms, ulong, 0644);
166 MODULE_PARM_DESC(default_maintenance_retry_ms,
167 "The time (milliseconds) between retry sends in maintenance mode");
169 /* The default maximum number of retries */
170 static unsigned int default_max_retries = 4;
171 module_param(default_max_retries, uint, 0644);
172 MODULE_PARM_DESC(default_max_retries,
173 "The time (milliseconds) between retry sends in maintenance mode");
175 /* Call every ~1000 ms. */
176 #define IPMI_TIMEOUT_TIME 1000
178 /* How many jiffies does it take to get to the timeout time. */
179 #define IPMI_TIMEOUT_JIFFIES ((IPMI_TIMEOUT_TIME * HZ) / 1000)
182 * Request events from the queue every second (this is the number of
183 * IPMI_TIMEOUT_TIMES between event requests). Hopefully, in the
184 * future, IPMI will add a way to know immediately if an event is in
185 * the queue and this silliness can go away.
187 #define IPMI_REQUEST_EV_TIME (1000 / (IPMI_TIMEOUT_TIME))
189 /* How long should we cache dynamic device IDs? */
190 #define IPMI_DYN_DEV_ID_EXPIRY (10 * HZ)
193 * The main "user" data structure.
196 struct list_head link;
199 * Set to NULL when the user is destroyed, a pointer to myself
200 * so srcu_dereference can be used on it.
202 struct ipmi_user *self;
203 struct srcu_struct release_barrier;
205 struct kref refcount;
207 /* The upper layer that handles receive messages. */
208 const struct ipmi_user_hndl *handler;
211 /* The interface this user is bound to. */
212 struct ipmi_smi *intf;
214 /* Does this interface receive IPMI events? */
218 static struct ipmi_user *acquire_ipmi_user(struct ipmi_user *user, int *index)
219 __acquires(user->release_barrier)
221 struct ipmi_user *ruser;
223 *index = srcu_read_lock(&user->release_barrier);
224 ruser = srcu_dereference(user->self, &user->release_barrier);
226 srcu_read_unlock(&user->release_barrier, *index);
230 static void release_ipmi_user(struct ipmi_user *user, int index)
232 srcu_read_unlock(&user->release_barrier, index);
236 struct list_head link;
238 struct ipmi_user *user;
244 * This is used to form a linked lised during mass deletion.
245 * Since this is in an RCU list, we cannot use the link above
246 * or change any data until the RCU period completes. So we
247 * use this next variable during mass deletion so we can have
248 * a list and don't have to wait and restart the search on
249 * every individual deletion of a command.
251 struct cmd_rcvr *next;
255 unsigned int inuse : 1;
256 unsigned int broadcast : 1;
258 unsigned long timeout;
259 unsigned long orig_timeout;
260 unsigned int retries_left;
263 * To verify on an incoming send message response that this is
264 * the message that the response is for, we keep a sequence id
265 * and increment it every time we send a message.
270 * This is held so we can properly respond to the message on a
271 * timeout, and it is used to hold the temporary data for
272 * retransmission, too.
274 struct ipmi_recv_msg *recv_msg;
278 * Store the information in a msgid (long) to allow us to find a
279 * sequence table entry from the msgid.
281 #define STORE_SEQ_IN_MSGID(seq, seqid) \
282 ((((seq) & 0x3f) << 26) | ((seqid) & 0x3ffffff))
284 #define GET_SEQ_FROM_MSGID(msgid, seq, seqid) \
286 seq = (((msgid) >> 26) & 0x3f); \
287 seqid = ((msgid) & 0x3ffffff); \
290 #define NEXT_SEQID(seqid) (((seqid) + 1) & 0x3ffffff)
292 #define IPMI_MAX_CHANNELS 16
293 struct ipmi_channel {
294 unsigned char medium;
295 unsigned char protocol;
298 struct ipmi_channel_set {
299 struct ipmi_channel c[IPMI_MAX_CHANNELS];
302 struct ipmi_my_addrinfo {
304 * My slave address. This is initialized to IPMI_BMC_SLAVE_ADDR,
305 * but may be changed by the user.
307 unsigned char address;
310 * My LUN. This should generally stay the SMS LUN, but just in
317 * Note that the product id, manufacturer id, guid, and device id are
318 * immutable in this structure, so dyn_mutex is not required for
319 * accessing those. If those change on a BMC, a new BMC is allocated.
322 struct platform_device pdev;
323 struct list_head intfs; /* Interfaces on this BMC. */
324 struct ipmi_device_id id;
325 struct ipmi_device_id fetch_id;
327 unsigned long dyn_id_expiry;
328 struct mutex dyn_mutex; /* Protects id, intfs, & dyn* */
332 struct kref usecount;
333 struct work_struct remove_work;
335 #define to_bmc_device(x) container_of((x), struct bmc_device, pdev.dev)
337 static int bmc_get_device_id(struct ipmi_smi *intf, struct bmc_device *bmc,
338 struct ipmi_device_id *id,
339 bool *guid_set, guid_t *guid);
342 * Various statistics for IPMI, these index stats[] in the ipmi_smi
345 enum ipmi_stat_indexes {
346 /* Commands we got from the user that were invalid. */
347 IPMI_STAT_sent_invalid_commands = 0,
349 /* Commands we sent to the MC. */
350 IPMI_STAT_sent_local_commands,
352 /* Responses from the MC that were delivered to a user. */
353 IPMI_STAT_handled_local_responses,
355 /* Responses from the MC that were not delivered to a user. */
356 IPMI_STAT_unhandled_local_responses,
358 /* Commands we sent out to the IPMB bus. */
359 IPMI_STAT_sent_ipmb_commands,
361 /* Commands sent on the IPMB that had errors on the SEND CMD */
362 IPMI_STAT_sent_ipmb_command_errs,
364 /* Each retransmit increments this count. */
365 IPMI_STAT_retransmitted_ipmb_commands,
368 * When a message times out (runs out of retransmits) this is
371 IPMI_STAT_timed_out_ipmb_commands,
374 * This is like above, but for broadcasts. Broadcasts are
375 * *not* included in the above count (they are expected to
378 IPMI_STAT_timed_out_ipmb_broadcasts,
380 /* Responses I have sent to the IPMB bus. */
381 IPMI_STAT_sent_ipmb_responses,
383 /* The response was delivered to the user. */
384 IPMI_STAT_handled_ipmb_responses,
386 /* The response had invalid data in it. */
387 IPMI_STAT_invalid_ipmb_responses,
389 /* The response didn't have anyone waiting for it. */
390 IPMI_STAT_unhandled_ipmb_responses,
392 /* Commands we sent out to the IPMB bus. */
393 IPMI_STAT_sent_lan_commands,
395 /* Commands sent on the IPMB that had errors on the SEND CMD */
396 IPMI_STAT_sent_lan_command_errs,
398 /* Each retransmit increments this count. */
399 IPMI_STAT_retransmitted_lan_commands,
402 * When a message times out (runs out of retransmits) this is
405 IPMI_STAT_timed_out_lan_commands,
407 /* Responses I have sent to the IPMB bus. */
408 IPMI_STAT_sent_lan_responses,
410 /* The response was delivered to the user. */
411 IPMI_STAT_handled_lan_responses,
413 /* The response had invalid data in it. */
414 IPMI_STAT_invalid_lan_responses,
416 /* The response didn't have anyone waiting for it. */
417 IPMI_STAT_unhandled_lan_responses,
419 /* The command was delivered to the user. */
420 IPMI_STAT_handled_commands,
422 /* The command had invalid data in it. */
423 IPMI_STAT_invalid_commands,
425 /* The command didn't have anyone waiting for it. */
426 IPMI_STAT_unhandled_commands,
428 /* Invalid data in an event. */
429 IPMI_STAT_invalid_events,
431 /* Events that were received with the proper format. */
434 /* Retransmissions on IPMB that failed. */
435 IPMI_STAT_dropped_rexmit_ipmb_commands,
437 /* Retransmissions on LAN that failed. */
438 IPMI_STAT_dropped_rexmit_lan_commands,
440 /* This *must* remain last, add new values above this. */
445 #define IPMI_IPMB_NUM_SEQ 64
447 /* What interface number are we? */
450 struct kref refcount;
452 /* Set when the interface is being unregistered. */
455 /* Used for a list of interfaces. */
456 struct list_head link;
459 * The list of upper layers that are using me. seq_lock write
460 * protects this. Read protection is with srcu.
462 struct list_head users;
463 struct srcu_struct users_srcu;
465 /* Used for wake ups at startup. */
466 wait_queue_head_t waitq;
469 * Prevents the interface from being unregistered when the
470 * interface is used by being looked up through the BMC
473 struct mutex bmc_reg_mutex;
475 struct bmc_device tmp_bmc;
476 struct bmc_device *bmc;
478 struct list_head bmc_link;
480 bool in_bmc_register; /* Handle recursive situations. Yuck. */
481 struct work_struct bmc_reg_work;
483 const struct ipmi_smi_handlers *handlers;
486 /* Driver-model device for the system interface. */
487 struct device *si_dev;
490 * A table of sequence numbers for this interface. We use the
491 * sequence numbers for IPMB messages that go out of the
492 * interface to match them up with their responses. A routine
493 * is called periodically to time the items in this list.
496 struct seq_table seq_table[IPMI_IPMB_NUM_SEQ];
500 * Messages queued for delivery. If delivery fails (out of memory
501 * for instance), They will stay in here to be processed later in a
502 * periodic timer interrupt. The tasklet is for handling received
503 * messages directly from the handler.
505 spinlock_t waiting_rcv_msgs_lock;
506 struct list_head waiting_rcv_msgs;
507 atomic_t watchdog_pretimeouts_to_deliver;
508 struct tasklet_struct recv_tasklet;
510 spinlock_t xmit_msgs_lock;
511 struct list_head xmit_msgs;
512 struct ipmi_smi_msg *curr_msg;
513 struct list_head hp_xmit_msgs;
516 * The list of command receivers that are registered for commands
519 struct mutex cmd_rcvrs_mutex;
520 struct list_head cmd_rcvrs;
523 * Events that were queues because no one was there to receive
526 spinlock_t events_lock; /* For dealing with event stuff. */
527 struct list_head waiting_events;
528 unsigned int waiting_events_count; /* How many events in queue? */
529 char delivering_events;
530 char event_msg_printed;
531 atomic_t event_waiters;
532 unsigned int ticks_to_req_ev;
533 int last_needs_timer;
536 * The event receiver for my BMC, only really used at panic
537 * shutdown as a place to store this.
539 unsigned char event_receiver;
540 unsigned char event_receiver_lun;
541 unsigned char local_sel_device;
542 unsigned char local_event_generator;
544 /* For handling of maintenance mode. */
545 int maintenance_mode;
546 bool maintenance_mode_enable;
547 int auto_maintenance_timeout;
548 spinlock_t maintenance_mode_lock; /* Used in a timer... */
551 * If we are doing maintenance on something on IPMB, extend
552 * the timeout time to avoid timeouts writing firmware and
555 int ipmb_maintenance_mode_timeout;
558 * A cheap hack, if this is non-null and a message to an
559 * interface comes in with a NULL user, call this routine with
560 * it. Note that the message will still be freed by the
561 * caller. This only works on the system interface.
563 * Protected by bmc_reg_mutex.
565 void (*null_user_handler)(struct ipmi_smi *intf,
566 struct ipmi_recv_msg *msg);
569 * When we are scanning the channels for an SMI, this will
570 * tell which channel we are scanning.
574 /* Channel information */
575 struct ipmi_channel_set *channel_list;
576 unsigned int curr_working_cset; /* First index into the following. */
577 struct ipmi_channel_set wchannels[2];
578 struct ipmi_my_addrinfo addrinfo[IPMI_MAX_CHANNELS];
581 atomic_t stats[IPMI_NUM_STATS];
584 * run_to_completion duplicate of smb_info, smi_info
585 * and ipmi_serial_info structures. Used to decrease numbers of
586 * parameters passed by "low" level IPMI code.
588 int run_to_completion;
590 #define to_si_intf_from_dev(device) container_of(device, struct ipmi_smi, dev)
592 static void __get_guid(struct ipmi_smi *intf);
593 static void __ipmi_bmc_unregister(struct ipmi_smi *intf);
594 static int __ipmi_bmc_register(struct ipmi_smi *intf,
595 struct ipmi_device_id *id,
596 bool guid_set, guid_t *guid, int intf_num);
597 static int __scan_channels(struct ipmi_smi *intf, struct ipmi_device_id *id);
601 * The driver model view of the IPMI messaging driver.
603 static struct platform_driver ipmidriver = {
606 .bus = &platform_bus_type
610 * This mutex keeps us from adding the same BMC twice.
612 static DEFINE_MUTEX(ipmidriver_mutex);
614 static LIST_HEAD(ipmi_interfaces);
615 static DEFINE_MUTEX(ipmi_interfaces_mutex);
616 DEFINE_STATIC_SRCU(ipmi_interfaces_srcu);
619 * List of watchers that want to know when smi's are added and deleted.
621 static LIST_HEAD(smi_watchers);
622 static DEFINE_MUTEX(smi_watchers_mutex);
624 #define ipmi_inc_stat(intf, stat) \
625 atomic_inc(&(intf)->stats[IPMI_STAT_ ## stat])
626 #define ipmi_get_stat(intf, stat) \
627 ((unsigned int) atomic_read(&(intf)->stats[IPMI_STAT_ ## stat]))
629 static const char * const addr_src_to_str[] = {
630 "invalid", "hotmod", "hardcoded", "SPMI", "ACPI", "SMBIOS", "PCI",
631 "device-tree", "platform"
634 const char *ipmi_addr_src_to_str(enum ipmi_addr_src src)
637 src = 0; /* Invalid */
638 return addr_src_to_str[src];
640 EXPORT_SYMBOL(ipmi_addr_src_to_str);
642 static int is_lan_addr(struct ipmi_addr *addr)
644 return addr->addr_type == IPMI_LAN_ADDR_TYPE;
647 static int is_ipmb_addr(struct ipmi_addr *addr)
649 return addr->addr_type == IPMI_IPMB_ADDR_TYPE;
652 static int is_ipmb_bcast_addr(struct ipmi_addr *addr)
654 return addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE;
657 static void free_recv_msg_list(struct list_head *q)
659 struct ipmi_recv_msg *msg, *msg2;
661 list_for_each_entry_safe(msg, msg2, q, link) {
662 list_del(&msg->link);
663 ipmi_free_recv_msg(msg);
667 static void free_smi_msg_list(struct list_head *q)
669 struct ipmi_smi_msg *msg, *msg2;
671 list_for_each_entry_safe(msg, msg2, q, link) {
672 list_del(&msg->link);
673 ipmi_free_smi_msg(msg);
677 static void clean_up_interface_data(struct ipmi_smi *intf)
680 struct cmd_rcvr *rcvr, *rcvr2;
681 struct list_head list;
683 tasklet_kill(&intf->recv_tasklet);
685 free_smi_msg_list(&intf->waiting_rcv_msgs);
686 free_recv_msg_list(&intf->waiting_events);
689 * Wholesale remove all the entries from the list in the
690 * interface and wait for RCU to know that none are in use.
692 mutex_lock(&intf->cmd_rcvrs_mutex);
693 INIT_LIST_HEAD(&list);
694 list_splice_init_rcu(&intf->cmd_rcvrs, &list, synchronize_rcu);
695 mutex_unlock(&intf->cmd_rcvrs_mutex);
697 list_for_each_entry_safe(rcvr, rcvr2, &list, link)
700 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
701 if ((intf->seq_table[i].inuse)
702 && (intf->seq_table[i].recv_msg))
703 ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
707 static void intf_free(struct kref *ref)
709 struct ipmi_smi *intf = container_of(ref, struct ipmi_smi, refcount);
711 clean_up_interface_data(intf);
715 struct watcher_entry {
717 struct ipmi_smi *intf;
718 struct list_head link;
721 int ipmi_smi_watcher_register(struct ipmi_smi_watcher *watcher)
723 struct ipmi_smi *intf;
726 mutex_lock(&smi_watchers_mutex);
728 list_add(&watcher->link, &smi_watchers);
730 index = srcu_read_lock(&ipmi_interfaces_srcu);
731 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
732 int intf_num = READ_ONCE(intf->intf_num);
736 watcher->new_smi(intf_num, intf->si_dev);
738 srcu_read_unlock(&ipmi_interfaces_srcu, index);
740 mutex_unlock(&smi_watchers_mutex);
744 EXPORT_SYMBOL(ipmi_smi_watcher_register);
746 int ipmi_smi_watcher_unregister(struct ipmi_smi_watcher *watcher)
748 mutex_lock(&smi_watchers_mutex);
749 list_del(&watcher->link);
750 mutex_unlock(&smi_watchers_mutex);
753 EXPORT_SYMBOL(ipmi_smi_watcher_unregister);
756 * Must be called with smi_watchers_mutex held.
759 call_smi_watchers(int i, struct device *dev)
761 struct ipmi_smi_watcher *w;
763 mutex_lock(&smi_watchers_mutex);
764 list_for_each_entry(w, &smi_watchers, link) {
765 if (try_module_get(w->owner)) {
767 module_put(w->owner);
770 mutex_unlock(&smi_watchers_mutex);
774 ipmi_addr_equal(struct ipmi_addr *addr1, struct ipmi_addr *addr2)
776 if (addr1->addr_type != addr2->addr_type)
779 if (addr1->channel != addr2->channel)
782 if (addr1->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
783 struct ipmi_system_interface_addr *smi_addr1
784 = (struct ipmi_system_interface_addr *) addr1;
785 struct ipmi_system_interface_addr *smi_addr2
786 = (struct ipmi_system_interface_addr *) addr2;
787 return (smi_addr1->lun == smi_addr2->lun);
790 if (is_ipmb_addr(addr1) || is_ipmb_bcast_addr(addr1)) {
791 struct ipmi_ipmb_addr *ipmb_addr1
792 = (struct ipmi_ipmb_addr *) addr1;
793 struct ipmi_ipmb_addr *ipmb_addr2
794 = (struct ipmi_ipmb_addr *) addr2;
796 return ((ipmb_addr1->slave_addr == ipmb_addr2->slave_addr)
797 && (ipmb_addr1->lun == ipmb_addr2->lun));
800 if (is_lan_addr(addr1)) {
801 struct ipmi_lan_addr *lan_addr1
802 = (struct ipmi_lan_addr *) addr1;
803 struct ipmi_lan_addr *lan_addr2
804 = (struct ipmi_lan_addr *) addr2;
806 return ((lan_addr1->remote_SWID == lan_addr2->remote_SWID)
807 && (lan_addr1->local_SWID == lan_addr2->local_SWID)
808 && (lan_addr1->session_handle
809 == lan_addr2->session_handle)
810 && (lan_addr1->lun == lan_addr2->lun));
816 int ipmi_validate_addr(struct ipmi_addr *addr, int len)
818 if (len < sizeof(struct ipmi_system_interface_addr))
821 if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
822 if (addr->channel != IPMI_BMC_CHANNEL)
827 if ((addr->channel == IPMI_BMC_CHANNEL)
828 || (addr->channel >= IPMI_MAX_CHANNELS)
829 || (addr->channel < 0))
832 if (is_ipmb_addr(addr) || is_ipmb_bcast_addr(addr)) {
833 if (len < sizeof(struct ipmi_ipmb_addr))
838 if (is_lan_addr(addr)) {
839 if (len < sizeof(struct ipmi_lan_addr))
846 EXPORT_SYMBOL(ipmi_validate_addr);
848 unsigned int ipmi_addr_length(int addr_type)
850 if (addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
851 return sizeof(struct ipmi_system_interface_addr);
853 if ((addr_type == IPMI_IPMB_ADDR_TYPE)
854 || (addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE))
855 return sizeof(struct ipmi_ipmb_addr);
857 if (addr_type == IPMI_LAN_ADDR_TYPE)
858 return sizeof(struct ipmi_lan_addr);
862 EXPORT_SYMBOL(ipmi_addr_length);
864 static int deliver_response(struct ipmi_smi *intf, struct ipmi_recv_msg *msg)
869 /* Special handling for NULL users. */
870 if (intf->null_user_handler) {
871 intf->null_user_handler(intf, msg);
873 /* No handler, so give up. */
876 ipmi_free_recv_msg(msg);
877 } else if (!oops_in_progress) {
879 * If we are running in the panic context, calling the
880 * receive handler doesn't much meaning and has a deadlock
881 * risk. At this moment, simply skip it in that case.
884 struct ipmi_user *user = acquire_ipmi_user(msg->user, &index);
887 user->handler->ipmi_recv_hndl(msg, user->handler_data);
888 release_ipmi_user(user, index);
890 /* User went away, give up. */
891 ipmi_free_recv_msg(msg);
899 static void deliver_local_response(struct ipmi_smi *intf,
900 struct ipmi_recv_msg *msg)
902 if (deliver_response(intf, msg))
903 ipmi_inc_stat(intf, unhandled_local_responses);
905 ipmi_inc_stat(intf, handled_local_responses);
908 static void deliver_err_response(struct ipmi_smi *intf,
909 struct ipmi_recv_msg *msg, int err)
911 msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
912 msg->msg_data[0] = err;
913 msg->msg.netfn |= 1; /* Convert to a response. */
914 msg->msg.data_len = 1;
915 msg->msg.data = msg->msg_data;
916 deliver_local_response(intf, msg);
920 * Find the next sequence number not being used and add the given
921 * message with the given timeout to the sequence table. This must be
922 * called with the interface's seq_lock held.
924 static int intf_next_seq(struct ipmi_smi *intf,
925 struct ipmi_recv_msg *recv_msg,
926 unsigned long timeout,
936 timeout = default_retry_ms;
938 retries = default_max_retries;
940 for (i = intf->curr_seq; (i+1)%IPMI_IPMB_NUM_SEQ != intf->curr_seq;
941 i = (i+1)%IPMI_IPMB_NUM_SEQ) {
942 if (!intf->seq_table[i].inuse)
946 if (!intf->seq_table[i].inuse) {
947 intf->seq_table[i].recv_msg = recv_msg;
950 * Start with the maximum timeout, when the send response
951 * comes in we will start the real timer.
953 intf->seq_table[i].timeout = MAX_MSG_TIMEOUT;
954 intf->seq_table[i].orig_timeout = timeout;
955 intf->seq_table[i].retries_left = retries;
956 intf->seq_table[i].broadcast = broadcast;
957 intf->seq_table[i].inuse = 1;
958 intf->seq_table[i].seqid = NEXT_SEQID(intf->seq_table[i].seqid);
960 *seqid = intf->seq_table[i].seqid;
961 intf->curr_seq = (i+1)%IPMI_IPMB_NUM_SEQ;
971 * Return the receive message for the given sequence number and
972 * release the sequence number so it can be reused. Some other data
973 * is passed in to be sure the message matches up correctly (to help
974 * guard against message coming in after their timeout and the
975 * sequence number being reused).
977 static int intf_find_seq(struct ipmi_smi *intf,
982 struct ipmi_addr *addr,
983 struct ipmi_recv_msg **recv_msg)
988 if (seq >= IPMI_IPMB_NUM_SEQ)
991 spin_lock_irqsave(&intf->seq_lock, flags);
992 if (intf->seq_table[seq].inuse) {
993 struct ipmi_recv_msg *msg = intf->seq_table[seq].recv_msg;
995 if ((msg->addr.channel == channel) && (msg->msg.cmd == cmd)
996 && (msg->msg.netfn == netfn)
997 && (ipmi_addr_equal(addr, &msg->addr))) {
999 intf->seq_table[seq].inuse = 0;
1003 spin_unlock_irqrestore(&intf->seq_lock, flags);
1009 /* Start the timer for a specific sequence table entry. */
1010 static int intf_start_seq_timer(struct ipmi_smi *intf,
1014 unsigned long flags;
1016 unsigned long seqid;
1019 GET_SEQ_FROM_MSGID(msgid, seq, seqid);
1021 spin_lock_irqsave(&intf->seq_lock, flags);
1023 * We do this verification because the user can be deleted
1024 * while a message is outstanding.
1026 if ((intf->seq_table[seq].inuse)
1027 && (intf->seq_table[seq].seqid == seqid)) {
1028 struct seq_table *ent = &intf->seq_table[seq];
1029 ent->timeout = ent->orig_timeout;
1032 spin_unlock_irqrestore(&intf->seq_lock, flags);
1037 /* Got an error for the send message for a specific sequence number. */
1038 static int intf_err_seq(struct ipmi_smi *intf,
1043 unsigned long flags;
1045 unsigned long seqid;
1046 struct ipmi_recv_msg *msg = NULL;
1049 GET_SEQ_FROM_MSGID(msgid, seq, seqid);
1051 spin_lock_irqsave(&intf->seq_lock, flags);
1053 * We do this verification because the user can be deleted
1054 * while a message is outstanding.
1056 if ((intf->seq_table[seq].inuse)
1057 && (intf->seq_table[seq].seqid == seqid)) {
1058 struct seq_table *ent = &intf->seq_table[seq];
1061 msg = ent->recv_msg;
1064 spin_unlock_irqrestore(&intf->seq_lock, flags);
1067 deliver_err_response(intf, msg, err);
1073 int ipmi_create_user(unsigned int if_num,
1074 const struct ipmi_user_hndl *handler,
1076 struct ipmi_user **user)
1078 unsigned long flags;
1079 struct ipmi_user *new_user;
1081 struct ipmi_smi *intf;
1084 * There is no module usecount here, because it's not
1085 * required. Since this can only be used by and called from
1086 * other modules, they will implicitly use this module, and
1087 * thus this can't be removed unless the other modules are
1091 if (handler == NULL)
1095 * Make sure the driver is actually initialized, this handles
1096 * problems with initialization order.
1099 rv = ipmi_init_msghandler();
1104 * The init code doesn't return an error if it was turned
1105 * off, but it won't initialize. Check that.
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.
3305 rv = ipmi_init_msghandler();
3309 * The init code doesn't return an error if it was turned
3310 * off, but it won't initialize. Check that.
3316 intf = kzalloc(sizeof(*intf), GFP_KERNEL);
3320 rv = init_srcu_struct(&intf->users_srcu);
3327 intf->bmc = &intf->tmp_bmc;
3328 INIT_LIST_HEAD(&intf->bmc->intfs);
3329 mutex_init(&intf->bmc->dyn_mutex);
3330 INIT_LIST_HEAD(&intf->bmc_link);
3331 mutex_init(&intf->bmc_reg_mutex);
3332 intf->intf_num = -1; /* Mark it invalid for now. */
3333 kref_init(&intf->refcount);
3334 INIT_WORK(&intf->bmc_reg_work, redo_bmc_reg);
3335 intf->si_dev = si_dev;
3336 for (j = 0; j < IPMI_MAX_CHANNELS; j++) {
3337 intf->addrinfo[j].address = IPMI_BMC_SLAVE_ADDR;
3338 intf->addrinfo[j].lun = 2;
3340 if (slave_addr != 0)
3341 intf->addrinfo[0].address = slave_addr;
3342 INIT_LIST_HEAD(&intf->users);
3343 intf->handlers = handlers;
3344 intf->send_info = send_info;
3345 spin_lock_init(&intf->seq_lock);
3346 for (j = 0; j < IPMI_IPMB_NUM_SEQ; j++) {
3347 intf->seq_table[j].inuse = 0;
3348 intf->seq_table[j].seqid = 0;
3351 spin_lock_init(&intf->waiting_rcv_msgs_lock);
3352 INIT_LIST_HEAD(&intf->waiting_rcv_msgs);
3353 tasklet_init(&intf->recv_tasklet,
3355 (unsigned long) intf);
3356 atomic_set(&intf->watchdog_pretimeouts_to_deliver, 0);
3357 spin_lock_init(&intf->xmit_msgs_lock);
3358 INIT_LIST_HEAD(&intf->xmit_msgs);
3359 INIT_LIST_HEAD(&intf->hp_xmit_msgs);
3360 spin_lock_init(&intf->events_lock);
3361 atomic_set(&intf->event_waiters, 0);
3362 intf->ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
3363 INIT_LIST_HEAD(&intf->waiting_events);
3364 intf->waiting_events_count = 0;
3365 mutex_init(&intf->cmd_rcvrs_mutex);
3366 spin_lock_init(&intf->maintenance_mode_lock);
3367 INIT_LIST_HEAD(&intf->cmd_rcvrs);
3368 init_waitqueue_head(&intf->waitq);
3369 for (i = 0; i < IPMI_NUM_STATS; i++)
3370 atomic_set(&intf->stats[i], 0);
3372 mutex_lock(&ipmi_interfaces_mutex);
3373 /* Look for a hole in the numbers. */
3375 link = &ipmi_interfaces;
3376 list_for_each_entry_rcu(tintf, &ipmi_interfaces, link) {
3377 if (tintf->intf_num != i) {
3378 link = &tintf->link;
3383 /* Add the new interface in numeric order. */
3385 list_add_rcu(&intf->link, &ipmi_interfaces);
3387 list_add_tail_rcu(&intf->link, link);
3389 rv = handlers->start_processing(send_info, intf);
3393 rv = __bmc_get_device_id(intf, NULL, &id, NULL, NULL, i);
3395 dev_err(si_dev, "Unable to get the device id: %d\n", rv);
3396 goto out_err_started;
3399 mutex_lock(&intf->bmc_reg_mutex);
3400 rv = __scan_channels(intf, &id);
3401 mutex_unlock(&intf->bmc_reg_mutex);
3403 goto out_err_bmc_reg;
3406 * Keep memory order straight for RCU readers. Make
3407 * sure everything else is committed to memory before
3408 * setting intf_num to mark the interface valid.
3412 mutex_unlock(&ipmi_interfaces_mutex);
3414 /* After this point the interface is legal to use. */
3415 call_smi_watchers(i, intf->si_dev);
3420 ipmi_bmc_unregister(intf);
3422 if (intf->handlers->shutdown)
3423 intf->handlers->shutdown(intf->send_info);
3425 list_del_rcu(&intf->link);
3426 mutex_unlock(&ipmi_interfaces_mutex);
3427 synchronize_srcu(&ipmi_interfaces_srcu);
3428 cleanup_srcu_struct(&intf->users_srcu);
3429 kref_put(&intf->refcount, intf_free);
3433 EXPORT_SYMBOL(ipmi_register_smi);
3435 static void deliver_smi_err_response(struct ipmi_smi *intf,
3436 struct ipmi_smi_msg *msg,
3439 msg->rsp[0] = msg->data[0] | 4;
3440 msg->rsp[1] = msg->data[1];
3443 /* It's an error, so it will never requeue, no need to check return. */
3444 handle_one_recv_msg(intf, msg);
3447 static void cleanup_smi_msgs(struct ipmi_smi *intf)
3450 struct seq_table *ent;
3451 struct ipmi_smi_msg *msg;
3452 struct list_head *entry;
3453 struct list_head tmplist;
3455 /* Clear out our transmit queues and hold the messages. */
3456 INIT_LIST_HEAD(&tmplist);
3457 list_splice_tail(&intf->hp_xmit_msgs, &tmplist);
3458 list_splice_tail(&intf->xmit_msgs, &tmplist);
3460 /* Current message first, to preserve order */
3461 while (intf->curr_msg && !list_empty(&intf->waiting_rcv_msgs)) {
3462 /* Wait for the message to clear out. */
3463 schedule_timeout(1);
3466 /* No need for locks, the interface is down. */
3469 * Return errors for all pending messages in queue and in the
3470 * tables waiting for remote responses.
3472 while (!list_empty(&tmplist)) {
3473 entry = tmplist.next;
3475 msg = list_entry(entry, struct ipmi_smi_msg, link);
3476 deliver_smi_err_response(intf, msg, IPMI_ERR_UNSPECIFIED);
3479 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
3480 ent = &intf->seq_table[i];
3483 deliver_err_response(intf, ent->recv_msg, IPMI_ERR_UNSPECIFIED);
3487 void ipmi_unregister_smi(struct ipmi_smi *intf)
3489 struct ipmi_smi_watcher *w;
3490 int intf_num = intf->intf_num, index;
3492 mutex_lock(&ipmi_interfaces_mutex);
3493 intf->intf_num = -1;
3494 intf->in_shutdown = true;
3495 list_del_rcu(&intf->link);
3496 mutex_unlock(&ipmi_interfaces_mutex);
3497 synchronize_srcu(&ipmi_interfaces_srcu);
3499 /* At this point no users can be added to the interface. */
3502 * Call all the watcher interfaces to tell them that
3503 * an interface is going away.
3505 mutex_lock(&smi_watchers_mutex);
3506 list_for_each_entry(w, &smi_watchers, link)
3507 w->smi_gone(intf_num);
3508 mutex_unlock(&smi_watchers_mutex);
3510 index = srcu_read_lock(&intf->users_srcu);
3511 while (!list_empty(&intf->users)) {
3512 struct ipmi_user *user =
3513 container_of(list_next_rcu(&intf->users),
3514 struct ipmi_user, link);
3516 _ipmi_destroy_user(user);
3518 srcu_read_unlock(&intf->users_srcu, index);
3520 if (intf->handlers->shutdown)
3521 intf->handlers->shutdown(intf->send_info);
3523 cleanup_smi_msgs(intf);
3525 ipmi_bmc_unregister(intf);
3527 cleanup_srcu_struct(&intf->users_srcu);
3528 kref_put(&intf->refcount, intf_free);
3530 EXPORT_SYMBOL(ipmi_unregister_smi);
3532 static int handle_ipmb_get_msg_rsp(struct ipmi_smi *intf,
3533 struct ipmi_smi_msg *msg)
3535 struct ipmi_ipmb_addr ipmb_addr;
3536 struct ipmi_recv_msg *recv_msg;
3539 * This is 11, not 10, because the response must contain a
3542 if (msg->rsp_size < 11) {
3543 /* Message not big enough, just ignore it. */
3544 ipmi_inc_stat(intf, invalid_ipmb_responses);
3548 if (msg->rsp[2] != 0) {
3549 /* An error getting the response, just ignore it. */
3553 ipmb_addr.addr_type = IPMI_IPMB_ADDR_TYPE;
3554 ipmb_addr.slave_addr = msg->rsp[6];
3555 ipmb_addr.channel = msg->rsp[3] & 0x0f;
3556 ipmb_addr.lun = msg->rsp[7] & 3;
3559 * It's a response from a remote entity. Look up the sequence
3560 * number and handle the response.
3562 if (intf_find_seq(intf,
3566 (msg->rsp[4] >> 2) & (~1),
3567 (struct ipmi_addr *) &ipmb_addr,
3570 * We were unable to find the sequence number,
3571 * so just nuke the message.
3573 ipmi_inc_stat(intf, unhandled_ipmb_responses);
3577 memcpy(recv_msg->msg_data, &msg->rsp[9], msg->rsp_size - 9);
3579 * The other fields matched, so no need to set them, except
3580 * for netfn, which needs to be the response that was
3581 * returned, not the request value.
3583 recv_msg->msg.netfn = msg->rsp[4] >> 2;
3584 recv_msg->msg.data = recv_msg->msg_data;
3585 recv_msg->msg.data_len = msg->rsp_size - 10;
3586 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
3587 if (deliver_response(intf, recv_msg))
3588 ipmi_inc_stat(intf, unhandled_ipmb_responses);
3590 ipmi_inc_stat(intf, handled_ipmb_responses);
3595 static int handle_ipmb_get_msg_cmd(struct ipmi_smi *intf,
3596 struct ipmi_smi_msg *msg)
3598 struct cmd_rcvr *rcvr;
3600 unsigned char netfn;
3603 struct ipmi_user *user = NULL;
3604 struct ipmi_ipmb_addr *ipmb_addr;
3605 struct ipmi_recv_msg *recv_msg;
3607 if (msg->rsp_size < 10) {
3608 /* Message not big enough, just ignore it. */
3609 ipmi_inc_stat(intf, invalid_commands);
3613 if (msg->rsp[2] != 0) {
3614 /* An error getting the response, just ignore it. */
3618 netfn = msg->rsp[4] >> 2;
3620 chan = msg->rsp[3] & 0xf;
3623 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
3626 kref_get(&user->refcount);
3632 /* We didn't find a user, deliver an error response. */
3633 ipmi_inc_stat(intf, unhandled_commands);
3635 msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
3636 msg->data[1] = IPMI_SEND_MSG_CMD;
3637 msg->data[2] = msg->rsp[3];
3638 msg->data[3] = msg->rsp[6];
3639 msg->data[4] = ((netfn + 1) << 2) | (msg->rsp[7] & 0x3);
3640 msg->data[5] = ipmb_checksum(&msg->data[3], 2);
3641 msg->data[6] = intf->addrinfo[msg->rsp[3] & 0xf].address;
3643 msg->data[7] = (msg->rsp[7] & 0xfc) | (msg->rsp[4] & 0x3);
3644 msg->data[8] = msg->rsp[8]; /* cmd */
3645 msg->data[9] = IPMI_INVALID_CMD_COMPLETION_CODE;
3646 msg->data[10] = ipmb_checksum(&msg->data[6], 4);
3647 msg->data_size = 11;
3649 ipmi_debug_msg("Invalid command:", msg->data, msg->data_size);
3652 if (!intf->in_shutdown) {
3653 smi_send(intf, intf->handlers, msg, 0);
3655 * We used the message, so return the value
3656 * that causes it to not be freed or
3663 recv_msg = ipmi_alloc_recv_msg();
3666 * We couldn't allocate memory for the
3667 * message, so requeue it for handling
3671 kref_put(&user->refcount, free_user);
3673 /* Extract the source address from the data. */
3674 ipmb_addr = (struct ipmi_ipmb_addr *) &recv_msg->addr;
3675 ipmb_addr->addr_type = IPMI_IPMB_ADDR_TYPE;
3676 ipmb_addr->slave_addr = msg->rsp[6];
3677 ipmb_addr->lun = msg->rsp[7] & 3;
3678 ipmb_addr->channel = msg->rsp[3] & 0xf;
3681 * Extract the rest of the message information
3682 * from the IPMB header.
3684 recv_msg->user = user;
3685 recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
3686 recv_msg->msgid = msg->rsp[7] >> 2;
3687 recv_msg->msg.netfn = msg->rsp[4] >> 2;
3688 recv_msg->msg.cmd = msg->rsp[8];
3689 recv_msg->msg.data = recv_msg->msg_data;
3692 * We chop off 10, not 9 bytes because the checksum
3693 * at the end also needs to be removed.
3695 recv_msg->msg.data_len = msg->rsp_size - 10;
3696 memcpy(recv_msg->msg_data, &msg->rsp[9],
3697 msg->rsp_size - 10);
3698 if (deliver_response(intf, recv_msg))
3699 ipmi_inc_stat(intf, unhandled_commands);
3701 ipmi_inc_stat(intf, handled_commands);
3708 static int handle_lan_get_msg_rsp(struct ipmi_smi *intf,
3709 struct ipmi_smi_msg *msg)
3711 struct ipmi_lan_addr lan_addr;
3712 struct ipmi_recv_msg *recv_msg;
3716 * This is 13, not 12, because the response must contain a
3719 if (msg->rsp_size < 13) {
3720 /* Message not big enough, just ignore it. */
3721 ipmi_inc_stat(intf, invalid_lan_responses);
3725 if (msg->rsp[2] != 0) {
3726 /* An error getting the response, just ignore it. */
3730 lan_addr.addr_type = IPMI_LAN_ADDR_TYPE;
3731 lan_addr.session_handle = msg->rsp[4];
3732 lan_addr.remote_SWID = msg->rsp[8];
3733 lan_addr.local_SWID = msg->rsp[5];
3734 lan_addr.channel = msg->rsp[3] & 0x0f;
3735 lan_addr.privilege = msg->rsp[3] >> 4;
3736 lan_addr.lun = msg->rsp[9] & 3;
3739 * It's a response from a remote entity. Look up the sequence
3740 * number and handle the response.
3742 if (intf_find_seq(intf,
3746 (msg->rsp[6] >> 2) & (~1),
3747 (struct ipmi_addr *) &lan_addr,
3750 * We were unable to find the sequence number,
3751 * so just nuke the message.
3753 ipmi_inc_stat(intf, unhandled_lan_responses);
3757 memcpy(recv_msg->msg_data, &msg->rsp[11], msg->rsp_size - 11);
3759 * The other fields matched, so no need to set them, except
3760 * for netfn, which needs to be the response that was
3761 * returned, not the request value.
3763 recv_msg->msg.netfn = msg->rsp[6] >> 2;
3764 recv_msg->msg.data = recv_msg->msg_data;
3765 recv_msg->msg.data_len = msg->rsp_size - 12;
3766 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
3767 if (deliver_response(intf, recv_msg))
3768 ipmi_inc_stat(intf, unhandled_lan_responses);
3770 ipmi_inc_stat(intf, handled_lan_responses);
3775 static int handle_lan_get_msg_cmd(struct ipmi_smi *intf,
3776 struct ipmi_smi_msg *msg)
3778 struct cmd_rcvr *rcvr;
3780 unsigned char netfn;
3783 struct ipmi_user *user = NULL;
3784 struct ipmi_lan_addr *lan_addr;
3785 struct ipmi_recv_msg *recv_msg;
3787 if (msg->rsp_size < 12) {
3788 /* Message not big enough, just ignore it. */
3789 ipmi_inc_stat(intf, invalid_commands);
3793 if (msg->rsp[2] != 0) {
3794 /* An error getting the response, just ignore it. */
3798 netfn = msg->rsp[6] >> 2;
3800 chan = msg->rsp[3] & 0xf;
3803 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
3806 kref_get(&user->refcount);
3812 /* We didn't find a user, just give up. */
3813 ipmi_inc_stat(intf, unhandled_commands);
3816 * Don't do anything with these messages, just allow
3821 recv_msg = ipmi_alloc_recv_msg();
3824 * We couldn't allocate memory for the
3825 * message, so requeue it for handling later.
3828 kref_put(&user->refcount, free_user);
3830 /* Extract the source address from the data. */
3831 lan_addr = (struct ipmi_lan_addr *) &recv_msg->addr;
3832 lan_addr->addr_type = IPMI_LAN_ADDR_TYPE;
3833 lan_addr->session_handle = msg->rsp[4];
3834 lan_addr->remote_SWID = msg->rsp[8];
3835 lan_addr->local_SWID = msg->rsp[5];
3836 lan_addr->lun = msg->rsp[9] & 3;
3837 lan_addr->channel = msg->rsp[3] & 0xf;
3838 lan_addr->privilege = msg->rsp[3] >> 4;
3841 * Extract the rest of the message information
3842 * from the IPMB header.
3844 recv_msg->user = user;
3845 recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
3846 recv_msg->msgid = msg->rsp[9] >> 2;
3847 recv_msg->msg.netfn = msg->rsp[6] >> 2;
3848 recv_msg->msg.cmd = msg->rsp[10];
3849 recv_msg->msg.data = recv_msg->msg_data;
3852 * We chop off 12, not 11 bytes because the checksum
3853 * at the end also needs to be removed.
3855 recv_msg->msg.data_len = msg->rsp_size - 12;
3856 memcpy(recv_msg->msg_data, &msg->rsp[11],
3857 msg->rsp_size - 12);
3858 if (deliver_response(intf, recv_msg))
3859 ipmi_inc_stat(intf, unhandled_commands);
3861 ipmi_inc_stat(intf, handled_commands);
3869 * This routine will handle "Get Message" command responses with
3870 * channels that use an OEM Medium. The message format belongs to
3871 * the OEM. See IPMI 2.0 specification, Chapter 6 and
3872 * Chapter 22, sections 22.6 and 22.24 for more details.
3874 static int handle_oem_get_msg_cmd(struct ipmi_smi *intf,
3875 struct ipmi_smi_msg *msg)
3877 struct cmd_rcvr *rcvr;
3879 unsigned char netfn;
3882 struct ipmi_user *user = NULL;
3883 struct ipmi_system_interface_addr *smi_addr;
3884 struct ipmi_recv_msg *recv_msg;
3887 * We expect the OEM SW to perform error checking
3888 * so we just do some basic sanity checks
3890 if (msg->rsp_size < 4) {
3891 /* Message not big enough, just ignore it. */
3892 ipmi_inc_stat(intf, invalid_commands);
3896 if (msg->rsp[2] != 0) {
3897 /* An error getting the response, just ignore it. */
3902 * This is an OEM Message so the OEM needs to know how
3903 * handle the message. We do no interpretation.
3905 netfn = msg->rsp[0] >> 2;
3907 chan = msg->rsp[3] & 0xf;
3910 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
3913 kref_get(&user->refcount);
3919 /* We didn't find a user, just give up. */
3920 ipmi_inc_stat(intf, unhandled_commands);
3923 * Don't do anything with these messages, just allow
3929 recv_msg = ipmi_alloc_recv_msg();
3932 * We couldn't allocate memory for the
3933 * message, so requeue it for handling
3937 kref_put(&user->refcount, free_user);
3940 * OEM Messages are expected to be delivered via
3941 * the system interface to SMS software. We might
3942 * need to visit this again depending on OEM
3945 smi_addr = ((struct ipmi_system_interface_addr *)
3947 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3948 smi_addr->channel = IPMI_BMC_CHANNEL;
3949 smi_addr->lun = msg->rsp[0] & 3;
3951 recv_msg->user = user;
3952 recv_msg->user_msg_data = NULL;
3953 recv_msg->recv_type = IPMI_OEM_RECV_TYPE;
3954 recv_msg->msg.netfn = msg->rsp[0] >> 2;
3955 recv_msg->msg.cmd = msg->rsp[1];
3956 recv_msg->msg.data = recv_msg->msg_data;
3959 * The message starts at byte 4 which follows the
3960 * the Channel Byte in the "GET MESSAGE" command
3962 recv_msg->msg.data_len = msg->rsp_size - 4;
3963 memcpy(recv_msg->msg_data, &msg->rsp[4],
3965 if (deliver_response(intf, recv_msg))
3966 ipmi_inc_stat(intf, unhandled_commands);
3968 ipmi_inc_stat(intf, handled_commands);
3975 static void copy_event_into_recv_msg(struct ipmi_recv_msg *recv_msg,
3976 struct ipmi_smi_msg *msg)
3978 struct ipmi_system_interface_addr *smi_addr;
3980 recv_msg->msgid = 0;
3981 smi_addr = (struct ipmi_system_interface_addr *) &recv_msg->addr;
3982 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3983 smi_addr->channel = IPMI_BMC_CHANNEL;
3984 smi_addr->lun = msg->rsp[0] & 3;
3985 recv_msg->recv_type = IPMI_ASYNC_EVENT_RECV_TYPE;
3986 recv_msg->msg.netfn = msg->rsp[0] >> 2;
3987 recv_msg->msg.cmd = msg->rsp[1];
3988 memcpy(recv_msg->msg_data, &msg->rsp[3], msg->rsp_size - 3);
3989 recv_msg->msg.data = recv_msg->msg_data;
3990 recv_msg->msg.data_len = msg->rsp_size - 3;
3993 static int handle_read_event_rsp(struct ipmi_smi *intf,
3994 struct ipmi_smi_msg *msg)
3996 struct ipmi_recv_msg *recv_msg, *recv_msg2;
3997 struct list_head msgs;
3998 struct ipmi_user *user;
3999 int rv = 0, deliver_count = 0, index;
4000 unsigned long flags;
4002 if (msg->rsp_size < 19) {
4003 /* Message is too small to be an IPMB event. */
4004 ipmi_inc_stat(intf, invalid_events);
4008 if (msg->rsp[2] != 0) {
4009 /* An error getting the event, just ignore it. */
4013 INIT_LIST_HEAD(&msgs);
4015 spin_lock_irqsave(&intf->events_lock, flags);
4017 ipmi_inc_stat(intf, events);
4020 * Allocate and fill in one message for every user that is
4023 index = srcu_read_lock(&intf->users_srcu);
4024 list_for_each_entry_rcu(user, &intf->users, link) {
4025 if (!user->gets_events)
4028 recv_msg = ipmi_alloc_recv_msg();
4031 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs,
4033 list_del(&recv_msg->link);
4034 ipmi_free_recv_msg(recv_msg);
4037 * We couldn't allocate memory for the
4038 * message, so requeue it for handling
4047 copy_event_into_recv_msg(recv_msg, msg);
4048 recv_msg->user = user;
4049 kref_get(&user->refcount);
4050 list_add_tail(&recv_msg->link, &msgs);
4052 srcu_read_unlock(&intf->users_srcu, index);
4054 if (deliver_count) {
4055 /* Now deliver all the messages. */
4056 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs, link) {
4057 list_del(&recv_msg->link);
4058 deliver_local_response(intf, recv_msg);
4060 } else if (intf->waiting_events_count < MAX_EVENTS_IN_QUEUE) {
4062 * No one to receive the message, put it in queue if there's
4063 * not already too many things in the queue.
4065 recv_msg = ipmi_alloc_recv_msg();
4068 * We couldn't allocate memory for the
4069 * message, so requeue it for handling
4076 copy_event_into_recv_msg(recv_msg, msg);
4077 list_add_tail(&recv_msg->link, &intf->waiting_events);
4078 intf->waiting_events_count++;
4079 } else if (!intf->event_msg_printed) {
4081 * There's too many things in the queue, discard this
4084 dev_warn(intf->si_dev,
4085 "Event queue full, discarding incoming events\n");
4086 intf->event_msg_printed = 1;
4090 spin_unlock_irqrestore(&intf->events_lock, flags);
4095 static int handle_bmc_rsp(struct ipmi_smi *intf,
4096 struct ipmi_smi_msg *msg)
4098 struct ipmi_recv_msg *recv_msg;
4099 struct ipmi_system_interface_addr *smi_addr;
4101 recv_msg = (struct ipmi_recv_msg *) msg->user_data;
4102 if (recv_msg == NULL) {
4103 dev_warn(intf->si_dev,
4104 "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");
4108 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
4109 recv_msg->msgid = msg->msgid;
4110 smi_addr = ((struct ipmi_system_interface_addr *)
4112 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
4113 smi_addr->channel = IPMI_BMC_CHANNEL;
4114 smi_addr->lun = msg->rsp[0] & 3;
4115 recv_msg->msg.netfn = msg->rsp[0] >> 2;
4116 recv_msg->msg.cmd = msg->rsp[1];
4117 memcpy(recv_msg->msg_data, &msg->rsp[2], msg->rsp_size - 2);
4118 recv_msg->msg.data = recv_msg->msg_data;
4119 recv_msg->msg.data_len = msg->rsp_size - 2;
4120 deliver_local_response(intf, recv_msg);
4126 * Handle a received message. Return 1 if the message should be requeued,
4127 * 0 if the message should be freed, or -1 if the message should not
4128 * be freed or requeued.
4130 static int handle_one_recv_msg(struct ipmi_smi *intf,
4131 struct ipmi_smi_msg *msg)
4136 ipmi_debug_msg("Recv:", msg->rsp, msg->rsp_size);
4137 if (msg->rsp_size < 2) {
4138 /* Message is too small to be correct. */
4139 dev_warn(intf->si_dev,
4140 "BMC returned too small a message for netfn %x cmd %x, got %d bytes\n",
4141 (msg->data[0] >> 2) | 1, msg->data[1], msg->rsp_size);
4143 /* Generate an error response for the message. */
4144 msg->rsp[0] = msg->data[0] | (1 << 2);
4145 msg->rsp[1] = msg->data[1];
4146 msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
4148 } else if (((msg->rsp[0] >> 2) != ((msg->data[0] >> 2) | 1))
4149 || (msg->rsp[1] != msg->data[1])) {
4151 * The NetFN and Command in the response is not even
4152 * marginally correct.
4154 dev_warn(intf->si_dev,
4155 "BMC returned incorrect response, expected netfn %x cmd %x, got netfn %x cmd %x\n",
4156 (msg->data[0] >> 2) | 1, msg->data[1],
4157 msg->rsp[0] >> 2, msg->rsp[1]);
4159 /* Generate an error response for the message. */
4160 msg->rsp[0] = msg->data[0] | (1 << 2);
4161 msg->rsp[1] = msg->data[1];
4162 msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
4166 if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
4167 && (msg->rsp[1] == IPMI_SEND_MSG_CMD)
4168 && (msg->user_data != NULL)) {
4170 * It's a response to a response we sent. For this we
4171 * deliver a send message response to the user.
4173 struct ipmi_recv_msg *recv_msg = msg->user_data;
4176 if (msg->rsp_size < 2)
4177 /* Message is too small to be correct. */
4180 chan = msg->data[2] & 0x0f;
4181 if (chan >= IPMI_MAX_CHANNELS)
4182 /* Invalid channel number */
4188 recv_msg->recv_type = IPMI_RESPONSE_RESPONSE_TYPE;
4189 recv_msg->msg.data = recv_msg->msg_data;
4190 recv_msg->msg.data_len = 1;
4191 recv_msg->msg_data[0] = msg->rsp[2];
4192 deliver_local_response(intf, recv_msg);
4193 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
4194 && (msg->rsp[1] == IPMI_GET_MSG_CMD)) {
4195 struct ipmi_channel *chans;
4197 /* It's from the receive queue. */
4198 chan = msg->rsp[3] & 0xf;
4199 if (chan >= IPMI_MAX_CHANNELS) {
4200 /* Invalid channel number */
4206 * We need to make sure the channels have been initialized.
4207 * The channel_handler routine will set the "curr_channel"
4208 * equal to or greater than IPMI_MAX_CHANNELS when all the
4209 * channels for this interface have been initialized.
4211 if (!intf->channels_ready) {
4212 requeue = 0; /* Throw the message away */
4216 chans = READ_ONCE(intf->channel_list)->c;
4218 switch (chans[chan].medium) {
4219 case IPMI_CHANNEL_MEDIUM_IPMB:
4220 if (msg->rsp[4] & 0x04) {
4222 * It's a response, so find the
4223 * requesting message and send it up.
4225 requeue = handle_ipmb_get_msg_rsp(intf, msg);
4228 * It's a command to the SMS from some other
4229 * entity. Handle that.
4231 requeue = handle_ipmb_get_msg_cmd(intf, msg);
4235 case IPMI_CHANNEL_MEDIUM_8023LAN:
4236 case IPMI_CHANNEL_MEDIUM_ASYNC:
4237 if (msg->rsp[6] & 0x04) {
4239 * It's a response, so find the
4240 * requesting message and send it up.
4242 requeue = handle_lan_get_msg_rsp(intf, msg);
4245 * It's a command to the SMS from some other
4246 * entity. Handle that.
4248 requeue = handle_lan_get_msg_cmd(intf, msg);
4253 /* Check for OEM Channels. Clients had better
4254 register for these commands. */
4255 if ((chans[chan].medium >= IPMI_CHANNEL_MEDIUM_OEM_MIN)
4256 && (chans[chan].medium
4257 <= IPMI_CHANNEL_MEDIUM_OEM_MAX)) {
4258 requeue = handle_oem_get_msg_cmd(intf, msg);
4261 * We don't handle the channel type, so just
4268 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
4269 && (msg->rsp[1] == IPMI_READ_EVENT_MSG_BUFFER_CMD)) {
4270 /* It's an asynchronous event. */
4271 requeue = handle_read_event_rsp(intf, msg);
4273 /* It's a response from the local BMC. */
4274 requeue = handle_bmc_rsp(intf, msg);
4282 * If there are messages in the queue or pretimeouts, handle them.
4284 static void handle_new_recv_msgs(struct ipmi_smi *intf)
4286 struct ipmi_smi_msg *smi_msg;
4287 unsigned long flags = 0;
4289 int run_to_completion = intf->run_to_completion;
4291 /* See if any waiting messages need to be processed. */
4292 if (!run_to_completion)
4293 spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags);
4294 while (!list_empty(&intf->waiting_rcv_msgs)) {
4295 smi_msg = list_entry(intf->waiting_rcv_msgs.next,
4296 struct ipmi_smi_msg, link);
4297 list_del(&smi_msg->link);
4298 if (!run_to_completion)
4299 spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock,
4301 rv = handle_one_recv_msg(intf, smi_msg);
4302 if (!run_to_completion)
4303 spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags);
4306 * To preserve message order, quit if we
4307 * can't handle a message. Add the message
4308 * back at the head, this is safe because this
4309 * tasklet is the only thing that pulls the
4312 list_add(&smi_msg->link, &intf->waiting_rcv_msgs);
4316 /* Message handled */
4317 ipmi_free_smi_msg(smi_msg);
4318 /* If rv < 0, fatal error, del but don't free. */
4321 if (!run_to_completion)
4322 spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock, flags);
4325 * If the pretimout count is non-zero, decrement one from it and
4326 * deliver pretimeouts to all the users.
4328 if (atomic_add_unless(&intf->watchdog_pretimeouts_to_deliver, -1, 0)) {
4329 struct ipmi_user *user;
4332 index = srcu_read_lock(&intf->users_srcu);
4333 list_for_each_entry_rcu(user, &intf->users, link) {
4334 if (user->handler->ipmi_watchdog_pretimeout)
4335 user->handler->ipmi_watchdog_pretimeout(
4336 user->handler_data);
4338 srcu_read_unlock(&intf->users_srcu, index);
4342 static void smi_recv_tasklet(unsigned long val)
4344 unsigned long flags = 0; /* keep us warning-free. */
4345 struct ipmi_smi *intf = (struct ipmi_smi *) val;
4346 int run_to_completion = intf->run_to_completion;
4347 struct ipmi_smi_msg *newmsg = NULL;
4350 * Start the next message if available.
4352 * Do this here, not in the actual receiver, because we may deadlock
4353 * because the lower layer is allowed to hold locks while calling
4359 if (!run_to_completion)
4360 spin_lock_irqsave(&intf->xmit_msgs_lock, flags);
4361 if (intf->curr_msg == NULL && !intf->in_shutdown) {
4362 struct list_head *entry = NULL;
4364 /* Pick the high priority queue first. */
4365 if (!list_empty(&intf->hp_xmit_msgs))
4366 entry = intf->hp_xmit_msgs.next;
4367 else if (!list_empty(&intf->xmit_msgs))
4368 entry = intf->xmit_msgs.next;
4372 newmsg = list_entry(entry, struct ipmi_smi_msg, link);
4373 intf->curr_msg = newmsg;
4376 if (!run_to_completion)
4377 spin_unlock_irqrestore(&intf->xmit_msgs_lock, flags);
4379 intf->handlers->sender(intf->send_info, newmsg);
4383 handle_new_recv_msgs(intf);
4386 /* Handle a new message from the lower layer. */
4387 void ipmi_smi_msg_received(struct ipmi_smi *intf,
4388 struct ipmi_smi_msg *msg)
4390 unsigned long flags = 0; /* keep us warning-free. */
4391 int run_to_completion = intf->run_to_completion;
4393 if ((msg->data_size >= 2)
4394 && (msg->data[0] == (IPMI_NETFN_APP_REQUEST << 2))
4395 && (msg->data[1] == IPMI_SEND_MSG_CMD)
4396 && (msg->user_data == NULL)) {
4398 if (intf->in_shutdown)
4402 * This is the local response to a command send, start
4403 * the timer for these. The user_data will not be
4404 * NULL if this is a response send, and we will let
4405 * response sends just go through.
4409 * Check for errors, if we get certain errors (ones
4410 * that mean basically we can try again later), we
4411 * ignore them and start the timer. Otherwise we
4412 * report the error immediately.
4414 if ((msg->rsp_size >= 3) && (msg->rsp[2] != 0)
4415 && (msg->rsp[2] != IPMI_NODE_BUSY_ERR)
4416 && (msg->rsp[2] != IPMI_LOST_ARBITRATION_ERR)
4417 && (msg->rsp[2] != IPMI_BUS_ERR)
4418 && (msg->rsp[2] != IPMI_NAK_ON_WRITE_ERR)) {
4419 int ch = msg->rsp[3] & 0xf;
4420 struct ipmi_channel *chans;
4422 /* Got an error sending the message, handle it. */
4424 chans = READ_ONCE(intf->channel_list)->c;
4425 if ((chans[ch].medium == IPMI_CHANNEL_MEDIUM_8023LAN)
4426 || (chans[ch].medium == IPMI_CHANNEL_MEDIUM_ASYNC))
4427 ipmi_inc_stat(intf, sent_lan_command_errs);
4429 ipmi_inc_stat(intf, sent_ipmb_command_errs);
4430 intf_err_seq(intf, msg->msgid, msg->rsp[2]);
4432 /* The message was sent, start the timer. */
4433 intf_start_seq_timer(intf, msg->msgid);
4436 ipmi_free_smi_msg(msg);
4439 * To preserve message order, we keep a queue and deliver from
4442 if (!run_to_completion)
4443 spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags);
4444 list_add_tail(&msg->link, &intf->waiting_rcv_msgs);
4445 if (!run_to_completion)
4446 spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock,
4450 if (!run_to_completion)
4451 spin_lock_irqsave(&intf->xmit_msgs_lock, flags);
4453 * We can get an asynchronous event or receive message in addition
4454 * to commands we send.
4456 if (msg == intf->curr_msg)
4457 intf->curr_msg = NULL;
4458 if (!run_to_completion)
4459 spin_unlock_irqrestore(&intf->xmit_msgs_lock, flags);
4461 if (run_to_completion)
4462 smi_recv_tasklet((unsigned long) intf);
4464 tasklet_schedule(&intf->recv_tasklet);
4466 EXPORT_SYMBOL(ipmi_smi_msg_received);
4468 void ipmi_smi_watchdog_pretimeout(struct ipmi_smi *intf)
4470 if (intf->in_shutdown)
4473 atomic_set(&intf->watchdog_pretimeouts_to_deliver, 1);
4474 tasklet_schedule(&intf->recv_tasklet);
4476 EXPORT_SYMBOL(ipmi_smi_watchdog_pretimeout);
4478 static struct ipmi_smi_msg *
4479 smi_from_recv_msg(struct ipmi_smi *intf, struct ipmi_recv_msg *recv_msg,
4480 unsigned char seq, long seqid)
4482 struct ipmi_smi_msg *smi_msg = ipmi_alloc_smi_msg();
4485 * If we can't allocate the message, then just return, we
4486 * get 4 retries, so this should be ok.
4490 memcpy(smi_msg->data, recv_msg->msg.data, recv_msg->msg.data_len);
4491 smi_msg->data_size = recv_msg->msg.data_len;
4492 smi_msg->msgid = STORE_SEQ_IN_MSGID(seq, seqid);
4494 ipmi_debug_msg("Resend: ", smi_msg->data, smi_msg->data_size);
4499 static void check_msg_timeout(struct ipmi_smi *intf, struct seq_table *ent,
4500 struct list_head *timeouts,
4501 unsigned long timeout_period,
4502 int slot, unsigned long *flags,
4503 unsigned int *waiting_msgs)
4505 struct ipmi_recv_msg *msg;
4507 if (intf->in_shutdown)
4513 if (timeout_period < ent->timeout) {
4514 ent->timeout -= timeout_period;
4519 if (ent->retries_left == 0) {
4520 /* The message has used all its retries. */
4522 msg = ent->recv_msg;
4523 list_add_tail(&msg->link, timeouts);
4525 ipmi_inc_stat(intf, timed_out_ipmb_broadcasts);
4526 else if (is_lan_addr(&ent->recv_msg->addr))
4527 ipmi_inc_stat(intf, timed_out_lan_commands);
4529 ipmi_inc_stat(intf, timed_out_ipmb_commands);
4531 struct ipmi_smi_msg *smi_msg;
4532 /* More retries, send again. */
4537 * Start with the max timer, set to normal timer after
4538 * the message is sent.
4540 ent->timeout = MAX_MSG_TIMEOUT;
4541 ent->retries_left--;
4542 smi_msg = smi_from_recv_msg(intf, ent->recv_msg, slot,
4545 if (is_lan_addr(&ent->recv_msg->addr))
4547 dropped_rexmit_lan_commands);
4550 dropped_rexmit_ipmb_commands);
4554 spin_unlock_irqrestore(&intf->seq_lock, *flags);
4557 * Send the new message. We send with a zero
4558 * priority. It timed out, I doubt time is that
4559 * critical now, and high priority messages are really
4560 * only for messages to the local MC, which don't get
4563 if (intf->handlers) {
4564 if (is_lan_addr(&ent->recv_msg->addr))
4566 retransmitted_lan_commands);
4569 retransmitted_ipmb_commands);
4571 smi_send(intf, intf->handlers, smi_msg, 0);
4573 ipmi_free_smi_msg(smi_msg);
4575 spin_lock_irqsave(&intf->seq_lock, *flags);
4579 static unsigned int ipmi_timeout_handler(struct ipmi_smi *intf,
4580 unsigned long timeout_period)
4582 struct list_head timeouts;
4583 struct ipmi_recv_msg *msg, *msg2;
4584 unsigned long flags;
4586 unsigned int waiting_msgs = 0;
4588 if (!intf->bmc_registered) {
4589 kref_get(&intf->refcount);
4590 if (!schedule_work(&intf->bmc_reg_work)) {
4591 kref_put(&intf->refcount, intf_free);
4597 * Go through the seq table and find any messages that
4598 * have timed out, putting them in the timeouts
4601 INIT_LIST_HEAD(&timeouts);
4602 spin_lock_irqsave(&intf->seq_lock, flags);
4603 if (intf->ipmb_maintenance_mode_timeout) {
4604 if (intf->ipmb_maintenance_mode_timeout <= timeout_period)
4605 intf->ipmb_maintenance_mode_timeout = 0;
4607 intf->ipmb_maintenance_mode_timeout -= timeout_period;
4609 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++)
4610 check_msg_timeout(intf, &intf->seq_table[i],
4611 &timeouts, timeout_period, i,
4612 &flags, &waiting_msgs);
4613 spin_unlock_irqrestore(&intf->seq_lock, flags);
4615 list_for_each_entry_safe(msg, msg2, &timeouts, link)
4616 deliver_err_response(intf, msg, IPMI_TIMEOUT_COMPLETION_CODE);
4619 * Maintenance mode handling. Check the timeout
4620 * optimistically before we claim the lock. It may
4621 * mean a timeout gets missed occasionally, but that
4622 * only means the timeout gets extended by one period
4623 * in that case. No big deal, and it avoids the lock
4626 if (intf->auto_maintenance_timeout > 0) {
4627 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
4628 if (intf->auto_maintenance_timeout > 0) {
4629 intf->auto_maintenance_timeout
4631 if (!intf->maintenance_mode
4632 && (intf->auto_maintenance_timeout <= 0)) {
4633 intf->maintenance_mode_enable = false;
4634 maintenance_mode_update(intf);
4637 spin_unlock_irqrestore(&intf->maintenance_mode_lock,
4641 tasklet_schedule(&intf->recv_tasklet);
4643 return waiting_msgs;
4646 static void ipmi_request_event(struct ipmi_smi *intf)
4648 /* No event requests when in maintenance mode. */
4649 if (intf->maintenance_mode_enable)
4652 if (!intf->in_shutdown)
4653 intf->handlers->request_events(intf->send_info);
4656 static struct timer_list ipmi_timer;
4658 static atomic_t stop_operation;
4660 static void ipmi_timeout(struct timer_list *unused)
4662 struct ipmi_smi *intf;
4665 if (atomic_read(&stop_operation))
4668 index = srcu_read_lock(&ipmi_interfaces_srcu);
4669 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
4672 if (atomic_read(&intf->event_waiters)) {
4673 intf->ticks_to_req_ev--;
4674 if (intf->ticks_to_req_ev == 0) {
4675 ipmi_request_event(intf);
4676 intf->ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
4681 lnt += ipmi_timeout_handler(intf, IPMI_TIMEOUT_TIME);
4684 if (lnt != intf->last_needs_timer &&
4685 intf->handlers->set_need_watch)
4686 intf->handlers->set_need_watch(intf->send_info, lnt);
4687 intf->last_needs_timer = lnt;
4691 srcu_read_unlock(&ipmi_interfaces_srcu, index);
4694 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
4697 static void need_waiter(struct ipmi_smi *intf)
4699 /* Racy, but worst case we start the timer twice. */
4700 if (!timer_pending(&ipmi_timer))
4701 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
4704 static atomic_t smi_msg_inuse_count = ATOMIC_INIT(0);
4705 static atomic_t recv_msg_inuse_count = ATOMIC_INIT(0);
4707 static void free_smi_msg(struct ipmi_smi_msg *msg)
4709 atomic_dec(&smi_msg_inuse_count);
4713 struct ipmi_smi_msg *ipmi_alloc_smi_msg(void)
4715 struct ipmi_smi_msg *rv;
4716 rv = kmalloc(sizeof(struct ipmi_smi_msg), GFP_ATOMIC);
4718 rv->done = free_smi_msg;
4719 rv->user_data = NULL;
4720 atomic_inc(&smi_msg_inuse_count);
4724 EXPORT_SYMBOL(ipmi_alloc_smi_msg);
4726 static void free_recv_msg(struct ipmi_recv_msg *msg)
4728 atomic_dec(&recv_msg_inuse_count);
4732 static struct ipmi_recv_msg *ipmi_alloc_recv_msg(void)
4734 struct ipmi_recv_msg *rv;
4736 rv = kmalloc(sizeof(struct ipmi_recv_msg), GFP_ATOMIC);
4739 rv->done = free_recv_msg;
4740 atomic_inc(&recv_msg_inuse_count);
4745 void ipmi_free_recv_msg(struct ipmi_recv_msg *msg)
4748 kref_put(&msg->user->refcount, free_user);
4751 EXPORT_SYMBOL(ipmi_free_recv_msg);
4753 static atomic_t panic_done_count = ATOMIC_INIT(0);
4755 static void dummy_smi_done_handler(struct ipmi_smi_msg *msg)
4757 atomic_dec(&panic_done_count);
4760 static void dummy_recv_done_handler(struct ipmi_recv_msg *msg)
4762 atomic_dec(&panic_done_count);
4766 * Inside a panic, send a message and wait for a response.
4768 static void ipmi_panic_request_and_wait(struct ipmi_smi *intf,
4769 struct ipmi_addr *addr,
4770 struct kernel_ipmi_msg *msg)
4772 struct ipmi_smi_msg smi_msg;
4773 struct ipmi_recv_msg recv_msg;
4776 smi_msg.done = dummy_smi_done_handler;
4777 recv_msg.done = dummy_recv_done_handler;
4778 atomic_add(2, &panic_done_count);
4779 rv = i_ipmi_request(NULL,
4788 intf->addrinfo[0].address,
4789 intf->addrinfo[0].lun,
4790 0, 1); /* Don't retry, and don't wait. */
4792 atomic_sub(2, &panic_done_count);
4793 else if (intf->handlers->flush_messages)
4794 intf->handlers->flush_messages(intf->send_info);
4796 while (atomic_read(&panic_done_count) != 0)
4800 static void event_receiver_fetcher(struct ipmi_smi *intf,
4801 struct ipmi_recv_msg *msg)
4803 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
4804 && (msg->msg.netfn == IPMI_NETFN_SENSOR_EVENT_RESPONSE)
4805 && (msg->msg.cmd == IPMI_GET_EVENT_RECEIVER_CMD)
4806 && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) {
4807 /* A get event receiver command, save it. */
4808 intf->event_receiver = msg->msg.data[1];
4809 intf->event_receiver_lun = msg->msg.data[2] & 0x3;
4813 static void device_id_fetcher(struct ipmi_smi *intf, struct ipmi_recv_msg *msg)
4815 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
4816 && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
4817 && (msg->msg.cmd == IPMI_GET_DEVICE_ID_CMD)
4818 && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) {
4820 * A get device id command, save if we are an event
4821 * receiver or generator.
4823 intf->local_sel_device = (msg->msg.data[6] >> 2) & 1;
4824 intf->local_event_generator = (msg->msg.data[6] >> 5) & 1;
4828 static void send_panic_events(struct ipmi_smi *intf, char *str)
4830 struct kernel_ipmi_msg msg;
4831 unsigned char data[16];
4832 struct ipmi_system_interface_addr *si;
4833 struct ipmi_addr addr;
4835 struct ipmi_ipmb_addr *ipmb;
4838 if (ipmi_send_panic_event == IPMI_SEND_PANIC_EVENT_NONE)
4841 si = (struct ipmi_system_interface_addr *) &addr;
4842 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
4843 si->channel = IPMI_BMC_CHANNEL;
4846 /* Fill in an event telling that we have failed. */
4847 msg.netfn = 0x04; /* Sensor or Event. */
4848 msg.cmd = 2; /* Platform event command. */
4851 data[0] = 0x41; /* Kernel generator ID, IPMI table 5-4 */
4852 data[1] = 0x03; /* This is for IPMI 1.0. */
4853 data[2] = 0x20; /* OS Critical Stop, IPMI table 36-3 */
4854 data[4] = 0x6f; /* Sensor specific, IPMI table 36-1 */
4855 data[5] = 0xa1; /* Runtime stop OEM bytes 2 & 3. */
4858 * Put a few breadcrumbs in. Hopefully later we can add more things
4859 * to make the panic events more useful.
4867 /* Send the event announcing the panic. */
4868 ipmi_panic_request_and_wait(intf, &addr, &msg);
4871 * On every interface, dump a bunch of OEM event holding the
4874 if (ipmi_send_panic_event != IPMI_SEND_PANIC_EVENT_STRING || !str)
4878 * intf_num is used as an marker to tell if the
4879 * interface is valid. Thus we need a read barrier to
4880 * make sure data fetched before checking intf_num
4886 * First job here is to figure out where to send the
4887 * OEM events. There's no way in IPMI to send OEM
4888 * events using an event send command, so we have to
4889 * find the SEL to put them in and stick them in
4893 /* Get capabilities from the get device id. */
4894 intf->local_sel_device = 0;
4895 intf->local_event_generator = 0;
4896 intf->event_receiver = 0;
4898 /* Request the device info from the local MC. */
4899 msg.netfn = IPMI_NETFN_APP_REQUEST;
4900 msg.cmd = IPMI_GET_DEVICE_ID_CMD;
4903 intf->null_user_handler = device_id_fetcher;
4904 ipmi_panic_request_and_wait(intf, &addr, &msg);
4906 if (intf->local_event_generator) {
4907 /* Request the event receiver from the local MC. */
4908 msg.netfn = IPMI_NETFN_SENSOR_EVENT_REQUEST;
4909 msg.cmd = IPMI_GET_EVENT_RECEIVER_CMD;
4912 intf->null_user_handler = event_receiver_fetcher;
4913 ipmi_panic_request_and_wait(intf, &addr, &msg);
4915 intf->null_user_handler = NULL;
4918 * Validate the event receiver. The low bit must not
4919 * be 1 (it must be a valid IPMB address), it cannot
4920 * be zero, and it must not be my address.
4922 if (((intf->event_receiver & 1) == 0)
4923 && (intf->event_receiver != 0)
4924 && (intf->event_receiver != intf->addrinfo[0].address)) {
4926 * The event receiver is valid, send an IPMB
4929 ipmb = (struct ipmi_ipmb_addr *) &addr;
4930 ipmb->addr_type = IPMI_IPMB_ADDR_TYPE;
4931 ipmb->channel = 0; /* FIXME - is this right? */
4932 ipmb->lun = intf->event_receiver_lun;
4933 ipmb->slave_addr = intf->event_receiver;
4934 } else if (intf->local_sel_device) {
4936 * The event receiver was not valid (or was
4937 * me), but I am an SEL device, just dump it
4940 si = (struct ipmi_system_interface_addr *) &addr;
4941 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
4942 si->channel = IPMI_BMC_CHANNEL;
4945 return; /* No where to send the event. */
4947 msg.netfn = IPMI_NETFN_STORAGE_REQUEST; /* Storage. */
4948 msg.cmd = IPMI_ADD_SEL_ENTRY_CMD;
4954 int size = strlen(p);
4960 data[2] = 0xf0; /* OEM event without timestamp. */
4961 data[3] = intf->addrinfo[0].address;
4962 data[4] = j++; /* sequence # */
4964 * Always give 11 bytes, so strncpy will fill
4965 * it with zeroes for me.
4967 strncpy(data+5, p, 11);
4970 ipmi_panic_request_and_wait(intf, &addr, &msg);
4974 static int has_panicked;
4976 static int panic_event(struct notifier_block *this,
4977 unsigned long event,
4980 struct ipmi_smi *intf;
4981 struct ipmi_user *user;
4987 /* For every registered interface, set it to run to completion. */
4988 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
4989 if (!intf->handlers || intf->intf_num == -1)
4990 /* Interface is not ready. */
4993 if (!intf->handlers->poll)
4997 * If we were interrupted while locking xmit_msgs_lock or
4998 * waiting_rcv_msgs_lock, the corresponding list may be
4999 * corrupted. In this case, drop items on the list for
5002 if (!spin_trylock(&intf->xmit_msgs_lock)) {
5003 INIT_LIST_HEAD(&intf->xmit_msgs);
5004 INIT_LIST_HEAD(&intf->hp_xmit_msgs);
5006 spin_unlock(&intf->xmit_msgs_lock);
5008 if (!spin_trylock(&intf->waiting_rcv_msgs_lock))
5009 INIT_LIST_HEAD(&intf->waiting_rcv_msgs);
5011 spin_unlock(&intf->waiting_rcv_msgs_lock);
5013 intf->run_to_completion = 1;
5014 if (intf->handlers->set_run_to_completion)
5015 intf->handlers->set_run_to_completion(intf->send_info,
5018 list_for_each_entry_rcu(user, &intf->users, link) {
5019 if (user->handler->ipmi_panic_handler)
5020 user->handler->ipmi_panic_handler(
5021 user->handler_data);
5024 send_panic_events(intf, ptr);
5030 static struct notifier_block panic_block = {
5031 .notifier_call = panic_event,
5033 .priority = 200 /* priority: INT_MAX >= x >= 0 */
5036 static int ipmi_init_msghandler(void)
5043 rv = driver_register(&ipmidriver.driver);
5045 pr_err("Could not register IPMI driver\n");
5049 pr_info("version " IPMI_DRIVER_VERSION "\n");
5051 timer_setup(&ipmi_timer, ipmi_timeout, 0);
5052 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
5054 atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
5061 static int __init ipmi_init_msghandler_mod(void)
5063 ipmi_init_msghandler();
5067 static void __exit cleanup_ipmi(void)
5074 atomic_notifier_chain_unregister(&panic_notifier_list, &panic_block);
5077 * This can't be called if any interfaces exist, so no worry
5078 * about shutting down the interfaces.
5082 * Tell the timer to stop, then wait for it to stop. This
5083 * avoids problems with race conditions removing the timer
5086 atomic_inc(&stop_operation);
5087 del_timer_sync(&ipmi_timer);
5089 driver_unregister(&ipmidriver.driver);
5093 /* Check for buffer leaks. */
5094 count = atomic_read(&smi_msg_inuse_count);
5096 pr_warn("SMI message count %d at exit\n", count);
5097 count = atomic_read(&recv_msg_inuse_count);
5099 pr_warn("recv message count %d at exit\n", count);
5101 module_exit(cleanup_ipmi);
5103 module_init(ipmi_init_msghandler_mod);
5104 MODULE_LICENSE("GPL");
5105 MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
5106 MODULE_DESCRIPTION("Incoming and outgoing message routing for an IPMI"
5108 MODULE_VERSION(IPMI_DRIVER_VERSION);
5109 MODULE_SOFTDEP("post: ipmi_devintf");