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 #include <linux/module.h>
15 #include <linux/errno.h>
16 #include <linux/poll.h>
17 #include <linux/sched.h>
18 #include <linux/seq_file.h>
19 #include <linux/spinlock.h>
20 #include <linux/mutex.h>
21 #include <linux/slab.h>
22 #include <linux/ipmi.h>
23 #include <linux/ipmi_smi.h>
24 #include <linux/notifier.h>
25 #include <linux/init.h>
26 #include <linux/proc_fs.h>
27 #include <linux/rcupdate.h>
28 #include <linux/interrupt.h>
29 #include <linux/moduleparam.h>
30 #include <linux/workqueue.h>
31 #include <linux/uuid.h>
33 #define PFX "IPMI message handler: "
35 #define IPMI_DRIVER_VERSION "39.2"
37 static struct ipmi_recv_msg
*ipmi_alloc_recv_msg(void);
38 static int ipmi_init_msghandler(void);
39 static void smi_recv_tasklet(unsigned long);
40 static void handle_new_recv_msgs(struct ipmi_smi
*intf
);
41 static void need_waiter(struct ipmi_smi
*intf
);
42 static int handle_one_recv_msg(struct ipmi_smi
*intf
,
43 struct ipmi_smi_msg
*msg
);
46 static void ipmi_debug_msg(const char *title
, unsigned char *data
,
52 pos
= snprintf(buf
, sizeof(buf
), "%s: ", title
);
53 for (i
= 0; i
< len
; i
++)
54 pos
+= snprintf(buf
+ pos
, sizeof(buf
) - pos
,
56 pr_debug("%s\n", buf
);
59 static void ipmi_debug_msg(const char *title
, unsigned char *data
,
64 static int initialized
;
66 enum ipmi_panic_event_op
{
67 IPMI_SEND_PANIC_EVENT_NONE
,
68 IPMI_SEND_PANIC_EVENT
,
69 IPMI_SEND_PANIC_EVENT_STRING
71 #ifdef CONFIG_IPMI_PANIC_STRING
72 #define IPMI_PANIC_DEFAULT IPMI_SEND_PANIC_EVENT_STRING
73 #elif defined(CONFIG_IPMI_PANIC_EVENT)
74 #define IPMI_PANIC_DEFAULT IPMI_SEND_PANIC_EVENT
76 #define IPMI_PANIC_DEFAULT IPMI_SEND_PANIC_EVENT_NONE
78 static enum ipmi_panic_event_op ipmi_send_panic_event
= IPMI_PANIC_DEFAULT
;
80 static int panic_op_write_handler(const char *val
,
81 const struct kernel_param
*kp
)
86 strncpy(valcp
, val
, 15);
91 if (strcmp(s
, "none") == 0)
92 ipmi_send_panic_event
= IPMI_SEND_PANIC_EVENT_NONE
;
93 else if (strcmp(s
, "event") == 0)
94 ipmi_send_panic_event
= IPMI_SEND_PANIC_EVENT
;
95 else if (strcmp(s
, "string") == 0)
96 ipmi_send_panic_event
= IPMI_SEND_PANIC_EVENT_STRING
;
103 static int panic_op_read_handler(char *buffer
, const struct kernel_param
*kp
)
105 switch (ipmi_send_panic_event
) {
106 case IPMI_SEND_PANIC_EVENT_NONE
:
107 strcpy(buffer
, "none");
110 case IPMI_SEND_PANIC_EVENT
:
111 strcpy(buffer
, "event");
114 case IPMI_SEND_PANIC_EVENT_STRING
:
115 strcpy(buffer
, "string");
119 strcpy(buffer
, "???");
123 return strlen(buffer
);
126 static const struct kernel_param_ops panic_op_ops
= {
127 .set
= panic_op_write_handler
,
128 .get
= panic_op_read_handler
130 module_param_cb(panic_op
, &panic_op_ops
, NULL
, 0600);
131 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.");
134 #define MAX_EVENTS_IN_QUEUE 25
136 /* Remain in auto-maintenance mode for this amount of time (in ms). */
137 static unsigned long maintenance_mode_timeout_ms
= 30000;
138 module_param(maintenance_mode_timeout_ms
, ulong
, 0644);
139 MODULE_PARM_DESC(maintenance_mode_timeout_ms
,
140 "The time (milliseconds) after the last maintenance message that the connection stays in maintenance mode.");
143 * Don't let a message sit in a queue forever, always time it with at lest
144 * the max message timer. This is in milliseconds.
146 #define MAX_MSG_TIMEOUT 60000
149 * Timeout times below are in milliseconds, and are done off a 1
150 * second timer. So setting the value to 1000 would mean anything
151 * between 0 and 1000ms. So really the only reasonable minimum
152 * setting it 2000ms, which is between 1 and 2 seconds.
155 /* The default timeout for message retries. */
156 static unsigned long default_retry_ms
= 2000;
157 module_param(default_retry_ms
, ulong
, 0644);
158 MODULE_PARM_DESC(default_retry_ms
,
159 "The time (milliseconds) between retry sends");
161 /* The default timeout for maintenance mode message retries. */
162 static unsigned long default_maintenance_retry_ms
= 3000;
163 module_param(default_maintenance_retry_ms
, ulong
, 0644);
164 MODULE_PARM_DESC(default_maintenance_retry_ms
,
165 "The time (milliseconds) between retry sends in maintenance mode");
167 /* The default maximum number of retries */
168 static unsigned int default_max_retries
= 4;
169 module_param(default_max_retries
, uint
, 0644);
170 MODULE_PARM_DESC(default_max_retries
,
171 "The time (milliseconds) between retry sends in maintenance mode");
173 /* Call every ~1000 ms. */
174 #define IPMI_TIMEOUT_TIME 1000
176 /* How many jiffies does it take to get to the timeout time. */
177 #define IPMI_TIMEOUT_JIFFIES ((IPMI_TIMEOUT_TIME * HZ) / 1000)
180 * Request events from the queue every second (this is the number of
181 * IPMI_TIMEOUT_TIMES between event requests). Hopefully, in the
182 * future, IPMI will add a way to know immediately if an event is in
183 * the queue and this silliness can go away.
185 #define IPMI_REQUEST_EV_TIME (1000 / (IPMI_TIMEOUT_TIME))
187 /* How long should we cache dynamic device IDs? */
188 #define IPMI_DYN_DEV_ID_EXPIRY (10 * HZ)
191 * The main "user" data structure.
194 struct list_head link
;
197 * Set to NULL when the user is destroyed, a pointer to myself
198 * so srcu_dereference can be used on it.
200 struct ipmi_user
*self
;
201 struct srcu_struct release_barrier
;
203 struct kref refcount
;
205 /* The upper layer that handles receive messages. */
206 const struct ipmi_user_hndl
*handler
;
209 /* The interface this user is bound to. */
210 struct ipmi_smi
*intf
;
212 /* Does this interface receive IPMI events? */
216 static struct ipmi_user
*acquire_ipmi_user(struct ipmi_user
*user
, int *index
)
217 __acquires(user
->release_barrier
)
219 struct ipmi_user
*ruser
;
221 *index
= srcu_read_lock(&user
->release_barrier
);
222 ruser
= srcu_dereference(user
->self
, &user
->release_barrier
);
224 srcu_read_unlock(&user
->release_barrier
, *index
);
228 static void release_ipmi_user(struct ipmi_user
*user
, int index
)
230 srcu_read_unlock(&user
->release_barrier
, index
);
234 struct list_head link
;
236 struct ipmi_user
*user
;
242 * This is used to form a linked lised during mass deletion.
243 * Since this is in an RCU list, we cannot use the link above
244 * or change any data until the RCU period completes. So we
245 * use this next variable during mass deletion so we can have
246 * a list and don't have to wait and restart the search on
247 * every individual deletion of a command.
249 struct cmd_rcvr
*next
;
253 unsigned int inuse
: 1;
254 unsigned int broadcast
: 1;
256 unsigned long timeout
;
257 unsigned long orig_timeout
;
258 unsigned int retries_left
;
261 * To verify on an incoming send message response that this is
262 * the message that the response is for, we keep a sequence id
263 * and increment it every time we send a message.
268 * This is held so we can properly respond to the message on a
269 * timeout, and it is used to hold the temporary data for
270 * retransmission, too.
272 struct ipmi_recv_msg
*recv_msg
;
276 * Store the information in a msgid (long) to allow us to find a
277 * sequence table entry from the msgid.
279 #define STORE_SEQ_IN_MSGID(seq, seqid) \
280 ((((seq) & 0x3f) << 26) | ((seqid) & 0x3ffffff))
282 #define GET_SEQ_FROM_MSGID(msgid, seq, seqid) \
284 seq = (((msgid) >> 26) & 0x3f); \
285 seqid = ((msgid) & 0x3ffffff); \
288 #define NEXT_SEQID(seqid) (((seqid) + 1) & 0x3ffffff)
290 #define IPMI_MAX_CHANNELS 16
291 struct ipmi_channel
{
292 unsigned char medium
;
293 unsigned char protocol
;
296 struct ipmi_channel_set
{
297 struct ipmi_channel c
[IPMI_MAX_CHANNELS
];
300 struct ipmi_my_addrinfo
{
302 * My slave address. This is initialized to IPMI_BMC_SLAVE_ADDR,
303 * but may be changed by the user.
305 unsigned char address
;
308 * My LUN. This should generally stay the SMS LUN, but just in
315 * Note that the product id, manufacturer id, guid, and device id are
316 * immutable in this structure, so dyn_mutex is not required for
317 * accessing those. If those change on a BMC, a new BMC is allocated.
320 struct platform_device pdev
;
321 struct list_head intfs
; /* Interfaces on this BMC. */
322 struct ipmi_device_id id
;
323 struct ipmi_device_id fetch_id
;
325 unsigned long dyn_id_expiry
;
326 struct mutex dyn_mutex
; /* Protects id, intfs, & dyn* */
330 struct kref usecount
;
331 struct work_struct remove_work
;
333 #define to_bmc_device(x) container_of((x), struct bmc_device, pdev.dev)
335 static int bmc_get_device_id(struct ipmi_smi
*intf
, struct bmc_device
*bmc
,
336 struct ipmi_device_id
*id
,
337 bool *guid_set
, guid_t
*guid
);
340 * Various statistics for IPMI, these index stats[] in the ipmi_smi
343 enum ipmi_stat_indexes
{
344 /* Commands we got from the user that were invalid. */
345 IPMI_STAT_sent_invalid_commands
= 0,
347 /* Commands we sent to the MC. */
348 IPMI_STAT_sent_local_commands
,
350 /* Responses from the MC that were delivered to a user. */
351 IPMI_STAT_handled_local_responses
,
353 /* Responses from the MC that were not delivered to a user. */
354 IPMI_STAT_unhandled_local_responses
,
356 /* Commands we sent out to the IPMB bus. */
357 IPMI_STAT_sent_ipmb_commands
,
359 /* Commands sent on the IPMB that had errors on the SEND CMD */
360 IPMI_STAT_sent_ipmb_command_errs
,
362 /* Each retransmit increments this count. */
363 IPMI_STAT_retransmitted_ipmb_commands
,
366 * When a message times out (runs out of retransmits) this is
369 IPMI_STAT_timed_out_ipmb_commands
,
372 * This is like above, but for broadcasts. Broadcasts are
373 * *not* included in the above count (they are expected to
376 IPMI_STAT_timed_out_ipmb_broadcasts
,
378 /* Responses I have sent to the IPMB bus. */
379 IPMI_STAT_sent_ipmb_responses
,
381 /* The response was delivered to the user. */
382 IPMI_STAT_handled_ipmb_responses
,
384 /* The response had invalid data in it. */
385 IPMI_STAT_invalid_ipmb_responses
,
387 /* The response didn't have anyone waiting for it. */
388 IPMI_STAT_unhandled_ipmb_responses
,
390 /* Commands we sent out to the IPMB bus. */
391 IPMI_STAT_sent_lan_commands
,
393 /* Commands sent on the IPMB that had errors on the SEND CMD */
394 IPMI_STAT_sent_lan_command_errs
,
396 /* Each retransmit increments this count. */
397 IPMI_STAT_retransmitted_lan_commands
,
400 * When a message times out (runs out of retransmits) this is
403 IPMI_STAT_timed_out_lan_commands
,
405 /* Responses I have sent to the IPMB bus. */
406 IPMI_STAT_sent_lan_responses
,
408 /* The response was delivered to the user. */
409 IPMI_STAT_handled_lan_responses
,
411 /* The response had invalid data in it. */
412 IPMI_STAT_invalid_lan_responses
,
414 /* The response didn't have anyone waiting for it. */
415 IPMI_STAT_unhandled_lan_responses
,
417 /* The command was delivered to the user. */
418 IPMI_STAT_handled_commands
,
420 /* The command had invalid data in it. */
421 IPMI_STAT_invalid_commands
,
423 /* The command didn't have anyone waiting for it. */
424 IPMI_STAT_unhandled_commands
,
426 /* Invalid data in an event. */
427 IPMI_STAT_invalid_events
,
429 /* Events that were received with the proper format. */
432 /* Retransmissions on IPMB that failed. */
433 IPMI_STAT_dropped_rexmit_ipmb_commands
,
435 /* Retransmissions on LAN that failed. */
436 IPMI_STAT_dropped_rexmit_lan_commands
,
438 /* This *must* remain last, add new values above this. */
443 #define IPMI_IPMB_NUM_SEQ 64
445 /* What interface number are we? */
448 struct kref refcount
;
450 /* Set when the interface is being unregistered. */
453 /* Used for a list of interfaces. */
454 struct list_head link
;
457 * The list of upper layers that are using me. seq_lock write
458 * protects this. Read protection is with srcu.
460 struct list_head users
;
461 struct srcu_struct users_srcu
;
463 /* Used for wake ups at startup. */
464 wait_queue_head_t waitq
;
467 * Prevents the interface from being unregistered when the
468 * interface is used by being looked up through the BMC
471 struct mutex bmc_reg_mutex
;
473 struct bmc_device tmp_bmc
;
474 struct bmc_device
*bmc
;
476 struct list_head bmc_link
;
478 bool in_bmc_register
; /* Handle recursive situations. Yuck. */
479 struct work_struct bmc_reg_work
;
481 const struct ipmi_smi_handlers
*handlers
;
484 /* Driver-model device for the system interface. */
485 struct device
*si_dev
;
488 * A table of sequence numbers for this interface. We use the
489 * sequence numbers for IPMB messages that go out of the
490 * interface to match them up with their responses. A routine
491 * is called periodically to time the items in this list.
494 struct seq_table seq_table
[IPMI_IPMB_NUM_SEQ
];
498 * Messages queued for delivery. If delivery fails (out of memory
499 * for instance), They will stay in here to be processed later in a
500 * periodic timer interrupt. The tasklet is for handling received
501 * messages directly from the handler.
503 spinlock_t waiting_rcv_msgs_lock
;
504 struct list_head waiting_rcv_msgs
;
505 atomic_t watchdog_pretimeouts_to_deliver
;
506 struct tasklet_struct recv_tasklet
;
508 spinlock_t xmit_msgs_lock
;
509 struct list_head xmit_msgs
;
510 struct ipmi_smi_msg
*curr_msg
;
511 struct list_head hp_xmit_msgs
;
514 * The list of command receivers that are registered for commands
517 struct mutex cmd_rcvrs_mutex
;
518 struct list_head cmd_rcvrs
;
521 * Events that were queues because no one was there to receive
524 spinlock_t events_lock
; /* For dealing with event stuff. */
525 struct list_head waiting_events
;
526 unsigned int waiting_events_count
; /* How many events in queue? */
527 char delivering_events
;
528 char event_msg_printed
;
529 atomic_t event_waiters
;
530 unsigned int ticks_to_req_ev
;
531 int last_needs_timer
;
534 * The event receiver for my BMC, only really used at panic
535 * shutdown as a place to store this.
537 unsigned char event_receiver
;
538 unsigned char event_receiver_lun
;
539 unsigned char local_sel_device
;
540 unsigned char local_event_generator
;
542 /* For handling of maintenance mode. */
543 int maintenance_mode
;
544 bool maintenance_mode_enable
;
545 int auto_maintenance_timeout
;
546 spinlock_t maintenance_mode_lock
; /* Used in a timer... */
549 * If we are doing maintenance on something on IPMB, extend
550 * the timeout time to avoid timeouts writing firmware and
553 int ipmb_maintenance_mode_timeout
;
556 * A cheap hack, if this is non-null and a message to an
557 * interface comes in with a NULL user, call this routine with
558 * it. Note that the message will still be freed by the
559 * caller. This only works on the system interface.
561 * Protected by bmc_reg_mutex.
563 void (*null_user_handler
)(struct ipmi_smi
*intf
,
564 struct ipmi_recv_msg
*msg
);
567 * When we are scanning the channels for an SMI, this will
568 * tell which channel we are scanning.
572 /* Channel information */
573 struct ipmi_channel_set
*channel_list
;
574 unsigned int curr_working_cset
; /* First index into the following. */
575 struct ipmi_channel_set wchannels
[2];
576 struct ipmi_my_addrinfo addrinfo
[IPMI_MAX_CHANNELS
];
579 atomic_t stats
[IPMI_NUM_STATS
];
582 * run_to_completion duplicate of smb_info, smi_info
583 * and ipmi_serial_info structures. Used to decrease numbers of
584 * parameters passed by "low" level IPMI code.
586 int run_to_completion
;
588 #define to_si_intf_from_dev(device) container_of(device, struct ipmi_smi, dev)
590 static void __get_guid(struct ipmi_smi
*intf
);
591 static void __ipmi_bmc_unregister(struct ipmi_smi
*intf
);
592 static int __ipmi_bmc_register(struct ipmi_smi
*intf
,
593 struct ipmi_device_id
*id
,
594 bool guid_set
, guid_t
*guid
, int intf_num
);
595 static int __scan_channels(struct ipmi_smi
*intf
, struct ipmi_device_id
*id
);
599 * The driver model view of the IPMI messaging driver.
601 static struct platform_driver ipmidriver
= {
604 .bus
= &platform_bus_type
608 * This mutex keeps us from adding the same BMC twice.
610 static DEFINE_MUTEX(ipmidriver_mutex
);
612 static LIST_HEAD(ipmi_interfaces
);
613 static DEFINE_MUTEX(ipmi_interfaces_mutex
);
614 DEFINE_STATIC_SRCU(ipmi_interfaces_srcu
);
617 * List of watchers that want to know when smi's are added and deleted.
619 static LIST_HEAD(smi_watchers
);
620 static DEFINE_MUTEX(smi_watchers_mutex
);
622 #define ipmi_inc_stat(intf, stat) \
623 atomic_inc(&(intf)->stats[IPMI_STAT_ ## stat])
624 #define ipmi_get_stat(intf, stat) \
625 ((unsigned int) atomic_read(&(intf)->stats[IPMI_STAT_ ## stat]))
627 static const char * const addr_src_to_str
[] = {
628 "invalid", "hotmod", "hardcoded", "SPMI", "ACPI", "SMBIOS", "PCI",
629 "device-tree", "platform"
632 const char *ipmi_addr_src_to_str(enum ipmi_addr_src src
)
635 src
= 0; /* Invalid */
636 return addr_src_to_str
[src
];
638 EXPORT_SYMBOL(ipmi_addr_src_to_str
);
640 static int is_lan_addr(struct ipmi_addr
*addr
)
642 return addr
->addr_type
== IPMI_LAN_ADDR_TYPE
;
645 static int is_ipmb_addr(struct ipmi_addr
*addr
)
647 return addr
->addr_type
== IPMI_IPMB_ADDR_TYPE
;
650 static int is_ipmb_bcast_addr(struct ipmi_addr
*addr
)
652 return addr
->addr_type
== IPMI_IPMB_BROADCAST_ADDR_TYPE
;
655 static void free_recv_msg_list(struct list_head
*q
)
657 struct ipmi_recv_msg
*msg
, *msg2
;
659 list_for_each_entry_safe(msg
, msg2
, q
, link
) {
660 list_del(&msg
->link
);
661 ipmi_free_recv_msg(msg
);
665 static void free_smi_msg_list(struct list_head
*q
)
667 struct ipmi_smi_msg
*msg
, *msg2
;
669 list_for_each_entry_safe(msg
, msg2
, q
, link
) {
670 list_del(&msg
->link
);
671 ipmi_free_smi_msg(msg
);
675 static void clean_up_interface_data(struct ipmi_smi
*intf
)
678 struct cmd_rcvr
*rcvr
, *rcvr2
;
679 struct list_head list
;
681 tasklet_kill(&intf
->recv_tasklet
);
683 free_smi_msg_list(&intf
->waiting_rcv_msgs
);
684 free_recv_msg_list(&intf
->waiting_events
);
687 * Wholesale remove all the entries from the list in the
688 * interface and wait for RCU to know that none are in use.
690 mutex_lock(&intf
->cmd_rcvrs_mutex
);
691 INIT_LIST_HEAD(&list
);
692 list_splice_init_rcu(&intf
->cmd_rcvrs
, &list
, synchronize_rcu
);
693 mutex_unlock(&intf
->cmd_rcvrs_mutex
);
695 list_for_each_entry_safe(rcvr
, rcvr2
, &list
, link
)
698 for (i
= 0; i
< IPMI_IPMB_NUM_SEQ
; i
++) {
699 if ((intf
->seq_table
[i
].inuse
)
700 && (intf
->seq_table
[i
].recv_msg
))
701 ipmi_free_recv_msg(intf
->seq_table
[i
].recv_msg
);
705 static void intf_free(struct kref
*ref
)
707 struct ipmi_smi
*intf
= container_of(ref
, struct ipmi_smi
, refcount
);
709 clean_up_interface_data(intf
);
713 struct watcher_entry
{
715 struct ipmi_smi
*intf
;
716 struct list_head link
;
719 int ipmi_smi_watcher_register(struct ipmi_smi_watcher
*watcher
)
721 struct ipmi_smi
*intf
;
724 mutex_lock(&smi_watchers_mutex
);
726 list_add(&watcher
->link
, &smi_watchers
);
728 index
= srcu_read_lock(&ipmi_interfaces_srcu
);
729 list_for_each_entry_rcu(intf
, &ipmi_interfaces
, link
) {
730 int intf_num
= READ_ONCE(intf
->intf_num
);
734 watcher
->new_smi(intf_num
, intf
->si_dev
);
736 srcu_read_unlock(&ipmi_interfaces_srcu
, index
);
738 mutex_unlock(&smi_watchers_mutex
);
742 EXPORT_SYMBOL(ipmi_smi_watcher_register
);
744 int ipmi_smi_watcher_unregister(struct ipmi_smi_watcher
*watcher
)
746 mutex_lock(&smi_watchers_mutex
);
747 list_del(&watcher
->link
);
748 mutex_unlock(&smi_watchers_mutex
);
751 EXPORT_SYMBOL(ipmi_smi_watcher_unregister
);
754 * Must be called with smi_watchers_mutex held.
757 call_smi_watchers(int i
, struct device
*dev
)
759 struct ipmi_smi_watcher
*w
;
761 mutex_lock(&smi_watchers_mutex
);
762 list_for_each_entry(w
, &smi_watchers
, link
) {
763 if (try_module_get(w
->owner
)) {
765 module_put(w
->owner
);
768 mutex_unlock(&smi_watchers_mutex
);
772 ipmi_addr_equal(struct ipmi_addr
*addr1
, struct ipmi_addr
*addr2
)
774 if (addr1
->addr_type
!= addr2
->addr_type
)
777 if (addr1
->channel
!= addr2
->channel
)
780 if (addr1
->addr_type
== IPMI_SYSTEM_INTERFACE_ADDR_TYPE
) {
781 struct ipmi_system_interface_addr
*smi_addr1
782 = (struct ipmi_system_interface_addr
*) addr1
;
783 struct ipmi_system_interface_addr
*smi_addr2
784 = (struct ipmi_system_interface_addr
*) addr2
;
785 return (smi_addr1
->lun
== smi_addr2
->lun
);
788 if (is_ipmb_addr(addr1
) || is_ipmb_bcast_addr(addr1
)) {
789 struct ipmi_ipmb_addr
*ipmb_addr1
790 = (struct ipmi_ipmb_addr
*) addr1
;
791 struct ipmi_ipmb_addr
*ipmb_addr2
792 = (struct ipmi_ipmb_addr
*) addr2
;
794 return ((ipmb_addr1
->slave_addr
== ipmb_addr2
->slave_addr
)
795 && (ipmb_addr1
->lun
== ipmb_addr2
->lun
));
798 if (is_lan_addr(addr1
)) {
799 struct ipmi_lan_addr
*lan_addr1
800 = (struct ipmi_lan_addr
*) addr1
;
801 struct ipmi_lan_addr
*lan_addr2
802 = (struct ipmi_lan_addr
*) addr2
;
804 return ((lan_addr1
->remote_SWID
== lan_addr2
->remote_SWID
)
805 && (lan_addr1
->local_SWID
== lan_addr2
->local_SWID
)
806 && (lan_addr1
->session_handle
807 == lan_addr2
->session_handle
)
808 && (lan_addr1
->lun
== lan_addr2
->lun
));
814 int ipmi_validate_addr(struct ipmi_addr
*addr
, int len
)
816 if (len
< sizeof(struct ipmi_system_interface_addr
))
819 if (addr
->addr_type
== IPMI_SYSTEM_INTERFACE_ADDR_TYPE
) {
820 if (addr
->channel
!= IPMI_BMC_CHANNEL
)
825 if ((addr
->channel
== IPMI_BMC_CHANNEL
)
826 || (addr
->channel
>= IPMI_MAX_CHANNELS
)
827 || (addr
->channel
< 0))
830 if (is_ipmb_addr(addr
) || is_ipmb_bcast_addr(addr
)) {
831 if (len
< sizeof(struct ipmi_ipmb_addr
))
836 if (is_lan_addr(addr
)) {
837 if (len
< sizeof(struct ipmi_lan_addr
))
844 EXPORT_SYMBOL(ipmi_validate_addr
);
846 unsigned int ipmi_addr_length(int addr_type
)
848 if (addr_type
== IPMI_SYSTEM_INTERFACE_ADDR_TYPE
)
849 return sizeof(struct ipmi_system_interface_addr
);
851 if ((addr_type
== IPMI_IPMB_ADDR_TYPE
)
852 || (addr_type
== IPMI_IPMB_BROADCAST_ADDR_TYPE
))
853 return sizeof(struct ipmi_ipmb_addr
);
855 if (addr_type
== IPMI_LAN_ADDR_TYPE
)
856 return sizeof(struct ipmi_lan_addr
);
860 EXPORT_SYMBOL(ipmi_addr_length
);
862 static int deliver_response(struct ipmi_smi
*intf
, struct ipmi_recv_msg
*msg
)
867 /* Special handling for NULL users. */
868 if (intf
->null_user_handler
) {
869 intf
->null_user_handler(intf
, msg
);
871 /* No handler, so give up. */
874 ipmi_free_recv_msg(msg
);
875 } else if (!oops_in_progress
) {
877 * If we are running in the panic context, calling the
878 * receive handler doesn't much meaning and has a deadlock
879 * risk. At this moment, simply skip it in that case.
882 struct ipmi_user
*user
= acquire_ipmi_user(msg
->user
, &index
);
885 user
->handler
->ipmi_recv_hndl(msg
, user
->handler_data
);
886 release_ipmi_user(msg
->user
, index
);
888 /* User went away, give up. */
889 ipmi_free_recv_msg(msg
);
897 static void deliver_local_response(struct ipmi_smi
*intf
,
898 struct ipmi_recv_msg
*msg
)
900 if (deliver_response(intf
, msg
))
901 ipmi_inc_stat(intf
, unhandled_local_responses
);
903 ipmi_inc_stat(intf
, handled_local_responses
);
906 static void deliver_err_response(struct ipmi_smi
*intf
,
907 struct ipmi_recv_msg
*msg
, int err
)
909 msg
->recv_type
= IPMI_RESPONSE_RECV_TYPE
;
910 msg
->msg_data
[0] = err
;
911 msg
->msg
.netfn
|= 1; /* Convert to a response. */
912 msg
->msg
.data_len
= 1;
913 msg
->msg
.data
= msg
->msg_data
;
914 deliver_local_response(intf
, msg
);
918 * Find the next sequence number not being used and add the given
919 * message with the given timeout to the sequence table. This must be
920 * called with the interface's seq_lock held.
922 static int intf_next_seq(struct ipmi_smi
*intf
,
923 struct ipmi_recv_msg
*recv_msg
,
924 unsigned long timeout
,
934 timeout
= default_retry_ms
;
936 retries
= default_max_retries
;
938 for (i
= intf
->curr_seq
; (i
+1)%IPMI_IPMB_NUM_SEQ
!= intf
->curr_seq
;
939 i
= (i
+1)%IPMI_IPMB_NUM_SEQ
) {
940 if (!intf
->seq_table
[i
].inuse
)
944 if (!intf
->seq_table
[i
].inuse
) {
945 intf
->seq_table
[i
].recv_msg
= recv_msg
;
948 * Start with the maximum timeout, when the send response
949 * comes in we will start the real timer.
951 intf
->seq_table
[i
].timeout
= MAX_MSG_TIMEOUT
;
952 intf
->seq_table
[i
].orig_timeout
= timeout
;
953 intf
->seq_table
[i
].retries_left
= retries
;
954 intf
->seq_table
[i
].broadcast
= broadcast
;
955 intf
->seq_table
[i
].inuse
= 1;
956 intf
->seq_table
[i
].seqid
= NEXT_SEQID(intf
->seq_table
[i
].seqid
);
958 *seqid
= intf
->seq_table
[i
].seqid
;
959 intf
->curr_seq
= (i
+1)%IPMI_IPMB_NUM_SEQ
;
969 * Return the receive message for the given sequence number and
970 * release the sequence number so it can be reused. Some other data
971 * is passed in to be sure the message matches up correctly (to help
972 * guard against message coming in after their timeout and the
973 * sequence number being reused).
975 static int intf_find_seq(struct ipmi_smi
*intf
,
980 struct ipmi_addr
*addr
,
981 struct ipmi_recv_msg
**recv_msg
)
986 if (seq
>= IPMI_IPMB_NUM_SEQ
)
989 spin_lock_irqsave(&intf
->seq_lock
, flags
);
990 if (intf
->seq_table
[seq
].inuse
) {
991 struct ipmi_recv_msg
*msg
= intf
->seq_table
[seq
].recv_msg
;
993 if ((msg
->addr
.channel
== channel
) && (msg
->msg
.cmd
== cmd
)
994 && (msg
->msg
.netfn
== netfn
)
995 && (ipmi_addr_equal(addr
, &msg
->addr
))) {
997 intf
->seq_table
[seq
].inuse
= 0;
1001 spin_unlock_irqrestore(&intf
->seq_lock
, flags
);
1007 /* Start the timer for a specific sequence table entry. */
1008 static int intf_start_seq_timer(struct ipmi_smi
*intf
,
1012 unsigned long flags
;
1014 unsigned long seqid
;
1017 GET_SEQ_FROM_MSGID(msgid
, seq
, seqid
);
1019 spin_lock_irqsave(&intf
->seq_lock
, flags
);
1021 * We do this verification because the user can be deleted
1022 * while a message is outstanding.
1024 if ((intf
->seq_table
[seq
].inuse
)
1025 && (intf
->seq_table
[seq
].seqid
== seqid
)) {
1026 struct seq_table
*ent
= &intf
->seq_table
[seq
];
1027 ent
->timeout
= ent
->orig_timeout
;
1030 spin_unlock_irqrestore(&intf
->seq_lock
, flags
);
1035 /* Got an error for the send message for a specific sequence number. */
1036 static int intf_err_seq(struct ipmi_smi
*intf
,
1041 unsigned long flags
;
1043 unsigned long seqid
;
1044 struct ipmi_recv_msg
*msg
= NULL
;
1047 GET_SEQ_FROM_MSGID(msgid
, seq
, seqid
);
1049 spin_lock_irqsave(&intf
->seq_lock
, flags
);
1051 * We do this verification because the user can be deleted
1052 * while a message is outstanding.
1054 if ((intf
->seq_table
[seq
].inuse
)
1055 && (intf
->seq_table
[seq
].seqid
== seqid
)) {
1056 struct seq_table
*ent
= &intf
->seq_table
[seq
];
1059 msg
= ent
->recv_msg
;
1062 spin_unlock_irqrestore(&intf
->seq_lock
, flags
);
1065 deliver_err_response(intf
, msg
, err
);
1071 int ipmi_create_user(unsigned int if_num
,
1072 const struct ipmi_user_hndl
*handler
,
1074 struct ipmi_user
**user
)
1076 unsigned long flags
;
1077 struct ipmi_user
*new_user
;
1079 struct ipmi_smi
*intf
;
1082 * There is no module usecount here, because it's not
1083 * required. Since this can only be used by and called from
1084 * other modules, they will implicitly use this module, and
1085 * thus this can't be removed unless the other modules are
1089 if (handler
== NULL
)
1093 * Make sure the driver is actually initialized, this handles
1094 * problems with initialization order.
1097 rv
= ipmi_init_msghandler();
1102 * The init code doesn't return an error if it was turned
1103 * off, but it won't initialize. Check that.
1109 new_user
= kmalloc(sizeof(*new_user
), GFP_KERNEL
);
1113 index
= srcu_read_lock(&ipmi_interfaces_srcu
);
1114 list_for_each_entry_rcu(intf
, &ipmi_interfaces
, link
) {
1115 if (intf
->intf_num
== if_num
)
1118 /* Not found, return an error */
1123 rv
= init_srcu_struct(&new_user
->release_barrier
);
1127 /* Note that each existing user holds a refcount to the interface. */
1128 kref_get(&intf
->refcount
);
1130 kref_init(&new_user
->refcount
);
1131 new_user
->handler
= handler
;
1132 new_user
->handler_data
= handler_data
;
1133 new_user
->intf
= intf
;
1134 new_user
->gets_events
= false;
1136 rcu_assign_pointer(new_user
->self
, new_user
);
1137 spin_lock_irqsave(&intf
->seq_lock
, flags
);
1138 list_add_rcu(&new_user
->link
, &intf
->users
);
1139 spin_unlock_irqrestore(&intf
->seq_lock
, flags
);
1140 if (handler
->ipmi_watchdog_pretimeout
) {
1141 /* User wants pretimeouts, so make sure to watch for them. */
1142 if (atomic_inc_return(&intf
->event_waiters
) == 1)
1145 srcu_read_unlock(&ipmi_interfaces_srcu
, index
);
1150 srcu_read_unlock(&ipmi_interfaces_srcu
, index
);
1154 EXPORT_SYMBOL(ipmi_create_user
);
1156 int ipmi_get_smi_info(int if_num
, struct ipmi_smi_info
*data
)
1159 struct ipmi_smi
*intf
;
1161 index
= srcu_read_lock(&ipmi_interfaces_srcu
);
1162 list_for_each_entry_rcu(intf
, &ipmi_interfaces
, link
) {
1163 if (intf
->intf_num
== if_num
)
1166 srcu_read_unlock(&ipmi_interfaces_srcu
, index
);
1168 /* Not found, return an error */
1172 if (!intf
->handlers
->get_smi_info
)
1175 rv
= intf
->handlers
->get_smi_info(intf
->send_info
, data
);
1176 srcu_read_unlock(&ipmi_interfaces_srcu
, index
);
1180 EXPORT_SYMBOL(ipmi_get_smi_info
);
1182 static void free_user(struct kref
*ref
)
1184 struct ipmi_user
*user
= container_of(ref
, struct ipmi_user
, refcount
);
1188 static void _ipmi_destroy_user(struct ipmi_user
*user
)
1190 struct ipmi_smi
*intf
= user
->intf
;
1192 unsigned long flags
;
1193 struct cmd_rcvr
*rcvr
;
1194 struct cmd_rcvr
*rcvrs
= NULL
;
1196 if (!acquire_ipmi_user(user
, &i
)) {
1198 * The user has already been cleaned up, just make sure
1199 * nothing is using it and return.
1201 synchronize_srcu(&user
->release_barrier
);
1205 rcu_assign_pointer(user
->self
, NULL
);
1206 release_ipmi_user(user
, i
);
1208 synchronize_srcu(&user
->release_barrier
);
1210 if (user
->handler
->shutdown
)
1211 user
->handler
->shutdown(user
->handler_data
);
1213 if (user
->handler
->ipmi_watchdog_pretimeout
)
1214 atomic_dec(&intf
->event_waiters
);
1216 if (user
->gets_events
)
1217 atomic_dec(&intf
->event_waiters
);
1219 /* Remove the user from the interface's sequence table. */
1220 spin_lock_irqsave(&intf
->seq_lock
, flags
);
1221 list_del_rcu(&user
->link
);
1223 for (i
= 0; i
< IPMI_IPMB_NUM_SEQ
; i
++) {
1224 if (intf
->seq_table
[i
].inuse
1225 && (intf
->seq_table
[i
].recv_msg
->user
== user
)) {
1226 intf
->seq_table
[i
].inuse
= 0;
1227 ipmi_free_recv_msg(intf
->seq_table
[i
].recv_msg
);
1230 spin_unlock_irqrestore(&intf
->seq_lock
, flags
);
1233 * Remove the user from the command receiver's table. First
1234 * we build a list of everything (not using the standard link,
1235 * since other things may be using it till we do
1236 * synchronize_srcu()) then free everything in that list.
1238 mutex_lock(&intf
->cmd_rcvrs_mutex
);
1239 list_for_each_entry_rcu(rcvr
, &intf
->cmd_rcvrs
, link
) {
1240 if (rcvr
->user
== user
) {
1241 list_del_rcu(&rcvr
->link
);
1246 mutex_unlock(&intf
->cmd_rcvrs_mutex
);
1254 kref_put(&intf
->refcount
, intf_free
);
1257 int ipmi_destroy_user(struct ipmi_user
*user
)
1259 _ipmi_destroy_user(user
);
1261 cleanup_srcu_struct(&user
->release_barrier
);
1262 kref_put(&user
->refcount
, free_user
);
1266 EXPORT_SYMBOL(ipmi_destroy_user
);
1268 int ipmi_get_version(struct ipmi_user
*user
,
1269 unsigned char *major
,
1270 unsigned char *minor
)
1272 struct ipmi_device_id id
;
1275 user
= acquire_ipmi_user(user
, &index
);
1279 rv
= bmc_get_device_id(user
->intf
, NULL
, &id
, NULL
, NULL
);
1281 *major
= ipmi_version_major(&id
);
1282 *minor
= ipmi_version_minor(&id
);
1284 release_ipmi_user(user
, index
);
1288 EXPORT_SYMBOL(ipmi_get_version
);
1290 int ipmi_set_my_address(struct ipmi_user
*user
,
1291 unsigned int channel
,
1292 unsigned char address
)
1296 user
= acquire_ipmi_user(user
, &index
);
1300 if (channel
>= IPMI_MAX_CHANNELS
)
1303 user
->intf
->addrinfo
[channel
].address
= address
;
1304 release_ipmi_user(user
, index
);
1308 EXPORT_SYMBOL(ipmi_set_my_address
);
1310 int ipmi_get_my_address(struct ipmi_user
*user
,
1311 unsigned int channel
,
1312 unsigned char *address
)
1316 user
= acquire_ipmi_user(user
, &index
);
1320 if (channel
>= IPMI_MAX_CHANNELS
)
1323 *address
= user
->intf
->addrinfo
[channel
].address
;
1324 release_ipmi_user(user
, index
);
1328 EXPORT_SYMBOL(ipmi_get_my_address
);
1330 int ipmi_set_my_LUN(struct ipmi_user
*user
,
1331 unsigned int channel
,
1336 user
= acquire_ipmi_user(user
, &index
);
1340 if (channel
>= IPMI_MAX_CHANNELS
)
1343 user
->intf
->addrinfo
[channel
].lun
= LUN
& 0x3;
1344 release_ipmi_user(user
, index
);
1348 EXPORT_SYMBOL(ipmi_set_my_LUN
);
1350 int ipmi_get_my_LUN(struct ipmi_user
*user
,
1351 unsigned int channel
,
1352 unsigned char *address
)
1356 user
= acquire_ipmi_user(user
, &index
);
1360 if (channel
>= IPMI_MAX_CHANNELS
)
1363 *address
= user
->intf
->addrinfo
[channel
].lun
;
1364 release_ipmi_user(user
, index
);
1368 EXPORT_SYMBOL(ipmi_get_my_LUN
);
1370 int ipmi_get_maintenance_mode(struct ipmi_user
*user
)
1373 unsigned long flags
;
1375 user
= acquire_ipmi_user(user
, &index
);
1379 spin_lock_irqsave(&user
->intf
->maintenance_mode_lock
, flags
);
1380 mode
= user
->intf
->maintenance_mode
;
1381 spin_unlock_irqrestore(&user
->intf
->maintenance_mode_lock
, flags
);
1382 release_ipmi_user(user
, index
);
1386 EXPORT_SYMBOL(ipmi_get_maintenance_mode
);
1388 static void maintenance_mode_update(struct ipmi_smi
*intf
)
1390 if (intf
->handlers
->set_maintenance_mode
)
1391 intf
->handlers
->set_maintenance_mode(
1392 intf
->send_info
, intf
->maintenance_mode_enable
);
1395 int ipmi_set_maintenance_mode(struct ipmi_user
*user
, int mode
)
1398 unsigned long flags
;
1399 struct ipmi_smi
*intf
= user
->intf
;
1401 user
= acquire_ipmi_user(user
, &index
);
1405 spin_lock_irqsave(&intf
->maintenance_mode_lock
, flags
);
1406 if (intf
->maintenance_mode
!= mode
) {
1408 case IPMI_MAINTENANCE_MODE_AUTO
:
1409 intf
->maintenance_mode_enable
1410 = (intf
->auto_maintenance_timeout
> 0);
1413 case IPMI_MAINTENANCE_MODE_OFF
:
1414 intf
->maintenance_mode_enable
= false;
1417 case IPMI_MAINTENANCE_MODE_ON
:
1418 intf
->maintenance_mode_enable
= true;
1425 intf
->maintenance_mode
= mode
;
1427 maintenance_mode_update(intf
);
1430 spin_unlock_irqrestore(&intf
->maintenance_mode_lock
, flags
);
1431 release_ipmi_user(user
, index
);
1435 EXPORT_SYMBOL(ipmi_set_maintenance_mode
);
1437 int ipmi_set_gets_events(struct ipmi_user
*user
, bool val
)
1439 unsigned long flags
;
1440 struct ipmi_smi
*intf
= user
->intf
;
1441 struct ipmi_recv_msg
*msg
, *msg2
;
1442 struct list_head msgs
;
1445 user
= acquire_ipmi_user(user
, &index
);
1449 INIT_LIST_HEAD(&msgs
);
1451 spin_lock_irqsave(&intf
->events_lock
, flags
);
1452 if (user
->gets_events
== val
)
1455 user
->gets_events
= val
;
1458 if (atomic_inc_return(&intf
->event_waiters
) == 1)
1461 atomic_dec(&intf
->event_waiters
);
1464 if (intf
->delivering_events
)
1466 * Another thread is delivering events for this, so
1467 * let it handle any new events.
1471 /* Deliver any queued events. */
1472 while (user
->gets_events
&& !list_empty(&intf
->waiting_events
)) {
1473 list_for_each_entry_safe(msg
, msg2
, &intf
->waiting_events
, link
)
1474 list_move_tail(&msg
->link
, &msgs
);
1475 intf
->waiting_events_count
= 0;
1476 if (intf
->event_msg_printed
) {
1477 dev_warn(intf
->si_dev
,
1478 PFX
"Event queue no longer full\n");
1479 intf
->event_msg_printed
= 0;
1482 intf
->delivering_events
= 1;
1483 spin_unlock_irqrestore(&intf
->events_lock
, flags
);
1485 list_for_each_entry_safe(msg
, msg2
, &msgs
, link
) {
1487 kref_get(&user
->refcount
);
1488 deliver_local_response(intf
, msg
);
1491 spin_lock_irqsave(&intf
->events_lock
, flags
);
1492 intf
->delivering_events
= 0;
1496 spin_unlock_irqrestore(&intf
->events_lock
, flags
);
1497 release_ipmi_user(user
, index
);
1501 EXPORT_SYMBOL(ipmi_set_gets_events
);
1503 static struct cmd_rcvr
*find_cmd_rcvr(struct ipmi_smi
*intf
,
1504 unsigned char netfn
,
1508 struct cmd_rcvr
*rcvr
;
1510 list_for_each_entry_rcu(rcvr
, &intf
->cmd_rcvrs
, link
) {
1511 if ((rcvr
->netfn
== netfn
) && (rcvr
->cmd
== cmd
)
1512 && (rcvr
->chans
& (1 << chan
)))
1518 static int is_cmd_rcvr_exclusive(struct ipmi_smi
*intf
,
1519 unsigned char netfn
,
1523 struct cmd_rcvr
*rcvr
;
1525 list_for_each_entry_rcu(rcvr
, &intf
->cmd_rcvrs
, link
) {
1526 if ((rcvr
->netfn
== netfn
) && (rcvr
->cmd
== cmd
)
1527 && (rcvr
->chans
& chans
))
1533 int ipmi_register_for_cmd(struct ipmi_user
*user
,
1534 unsigned char netfn
,
1538 struct ipmi_smi
*intf
= user
->intf
;
1539 struct cmd_rcvr
*rcvr
;
1542 user
= acquire_ipmi_user(user
, &index
);
1546 rcvr
= kmalloc(sizeof(*rcvr
), GFP_KERNEL
);
1552 rcvr
->netfn
= netfn
;
1553 rcvr
->chans
= chans
;
1556 mutex_lock(&intf
->cmd_rcvrs_mutex
);
1557 /* Make sure the command/netfn is not already registered. */
1558 if (!is_cmd_rcvr_exclusive(intf
, netfn
, cmd
, chans
)) {
1563 if (atomic_inc_return(&intf
->event_waiters
) == 1)
1566 list_add_rcu(&rcvr
->link
, &intf
->cmd_rcvrs
);
1569 mutex_unlock(&intf
->cmd_rcvrs_mutex
);
1573 release_ipmi_user(user
, index
);
1577 EXPORT_SYMBOL(ipmi_register_for_cmd
);
1579 int ipmi_unregister_for_cmd(struct ipmi_user
*user
,
1580 unsigned char netfn
,
1584 struct ipmi_smi
*intf
= user
->intf
;
1585 struct cmd_rcvr
*rcvr
;
1586 struct cmd_rcvr
*rcvrs
= NULL
;
1587 int i
, rv
= -ENOENT
, index
;
1589 user
= acquire_ipmi_user(user
, &index
);
1593 mutex_lock(&intf
->cmd_rcvrs_mutex
);
1594 for (i
= 0; i
< IPMI_NUM_CHANNELS
; i
++) {
1595 if (((1 << i
) & chans
) == 0)
1597 rcvr
= find_cmd_rcvr(intf
, netfn
, cmd
, i
);
1600 if (rcvr
->user
== user
) {
1602 rcvr
->chans
&= ~chans
;
1603 if (rcvr
->chans
== 0) {
1604 list_del_rcu(&rcvr
->link
);
1610 mutex_unlock(&intf
->cmd_rcvrs_mutex
);
1612 release_ipmi_user(user
, index
);
1614 atomic_dec(&intf
->event_waiters
);
1622 EXPORT_SYMBOL(ipmi_unregister_for_cmd
);
1624 static unsigned char
1625 ipmb_checksum(unsigned char *data
, int size
)
1627 unsigned char csum
= 0;
1629 for (; size
> 0; size
--, data
++)
1635 static inline void format_ipmb_msg(struct ipmi_smi_msg
*smi_msg
,
1636 struct kernel_ipmi_msg
*msg
,
1637 struct ipmi_ipmb_addr
*ipmb_addr
,
1639 unsigned char ipmb_seq
,
1641 unsigned char source_address
,
1642 unsigned char source_lun
)
1646 /* Format the IPMB header data. */
1647 smi_msg
->data
[0] = (IPMI_NETFN_APP_REQUEST
<< 2);
1648 smi_msg
->data
[1] = IPMI_SEND_MSG_CMD
;
1649 smi_msg
->data
[2] = ipmb_addr
->channel
;
1651 smi_msg
->data
[3] = 0;
1652 smi_msg
->data
[i
+3] = ipmb_addr
->slave_addr
;
1653 smi_msg
->data
[i
+4] = (msg
->netfn
<< 2) | (ipmb_addr
->lun
& 0x3);
1654 smi_msg
->data
[i
+5] = ipmb_checksum(&smi_msg
->data
[i
+ 3], 2);
1655 smi_msg
->data
[i
+6] = source_address
;
1656 smi_msg
->data
[i
+7] = (ipmb_seq
<< 2) | source_lun
;
1657 smi_msg
->data
[i
+8] = msg
->cmd
;
1659 /* Now tack on the data to the message. */
1660 if (msg
->data_len
> 0)
1661 memcpy(&smi_msg
->data
[i
+ 9], msg
->data
, msg
->data_len
);
1662 smi_msg
->data_size
= msg
->data_len
+ 9;
1664 /* Now calculate the checksum and tack it on. */
1665 smi_msg
->data
[i
+smi_msg
->data_size
]
1666 = ipmb_checksum(&smi_msg
->data
[i
+ 6], smi_msg
->data_size
- 6);
1669 * Add on the checksum size and the offset from the
1672 smi_msg
->data_size
+= 1 + i
;
1674 smi_msg
->msgid
= msgid
;
1677 static inline void format_lan_msg(struct ipmi_smi_msg
*smi_msg
,
1678 struct kernel_ipmi_msg
*msg
,
1679 struct ipmi_lan_addr
*lan_addr
,
1681 unsigned char ipmb_seq
,
1682 unsigned char source_lun
)
1684 /* Format the IPMB header data. */
1685 smi_msg
->data
[0] = (IPMI_NETFN_APP_REQUEST
<< 2);
1686 smi_msg
->data
[1] = IPMI_SEND_MSG_CMD
;
1687 smi_msg
->data
[2] = lan_addr
->channel
;
1688 smi_msg
->data
[3] = lan_addr
->session_handle
;
1689 smi_msg
->data
[4] = lan_addr
->remote_SWID
;
1690 smi_msg
->data
[5] = (msg
->netfn
<< 2) | (lan_addr
->lun
& 0x3);
1691 smi_msg
->data
[6] = ipmb_checksum(&smi_msg
->data
[4], 2);
1692 smi_msg
->data
[7] = lan_addr
->local_SWID
;
1693 smi_msg
->data
[8] = (ipmb_seq
<< 2) | source_lun
;
1694 smi_msg
->data
[9] = msg
->cmd
;
1696 /* Now tack on the data to the message. */
1697 if (msg
->data_len
> 0)
1698 memcpy(&smi_msg
->data
[10], msg
->data
, msg
->data_len
);
1699 smi_msg
->data_size
= msg
->data_len
+ 10;
1701 /* Now calculate the checksum and tack it on. */
1702 smi_msg
->data
[smi_msg
->data_size
]
1703 = ipmb_checksum(&smi_msg
->data
[7], smi_msg
->data_size
- 7);
1706 * Add on the checksum size and the offset from the
1709 smi_msg
->data_size
+= 1;
1711 smi_msg
->msgid
= msgid
;
1714 static struct ipmi_smi_msg
*smi_add_send_msg(struct ipmi_smi
*intf
,
1715 struct ipmi_smi_msg
*smi_msg
,
1718 if (intf
->curr_msg
) {
1720 list_add_tail(&smi_msg
->link
, &intf
->hp_xmit_msgs
);
1722 list_add_tail(&smi_msg
->link
, &intf
->xmit_msgs
);
1725 intf
->curr_msg
= smi_msg
;
1732 static void smi_send(struct ipmi_smi
*intf
,
1733 const struct ipmi_smi_handlers
*handlers
,
1734 struct ipmi_smi_msg
*smi_msg
, int priority
)
1736 int run_to_completion
= intf
->run_to_completion
;
1738 if (run_to_completion
) {
1739 smi_msg
= smi_add_send_msg(intf
, smi_msg
, priority
);
1741 unsigned long flags
;
1743 spin_lock_irqsave(&intf
->xmit_msgs_lock
, flags
);
1744 smi_msg
= smi_add_send_msg(intf
, smi_msg
, priority
);
1745 spin_unlock_irqrestore(&intf
->xmit_msgs_lock
, flags
);
1749 handlers
->sender(intf
->send_info
, smi_msg
);
1752 static bool is_maintenance_mode_cmd(struct kernel_ipmi_msg
*msg
)
1754 return (((msg
->netfn
== IPMI_NETFN_APP_REQUEST
)
1755 && ((msg
->cmd
== IPMI_COLD_RESET_CMD
)
1756 || (msg
->cmd
== IPMI_WARM_RESET_CMD
)))
1757 || (msg
->netfn
== IPMI_NETFN_FIRMWARE_REQUEST
));
1760 static int i_ipmi_req_sysintf(struct ipmi_smi
*intf
,
1761 struct ipmi_addr
*addr
,
1763 struct kernel_ipmi_msg
*msg
,
1764 struct ipmi_smi_msg
*smi_msg
,
1765 struct ipmi_recv_msg
*recv_msg
,
1767 unsigned int retry_time_ms
)
1769 struct ipmi_system_interface_addr
*smi_addr
;
1772 /* Responses are not allowed to the SMI. */
1775 smi_addr
= (struct ipmi_system_interface_addr
*) addr
;
1776 if (smi_addr
->lun
> 3) {
1777 ipmi_inc_stat(intf
, sent_invalid_commands
);
1781 memcpy(&recv_msg
->addr
, smi_addr
, sizeof(*smi_addr
));
1783 if ((msg
->netfn
== IPMI_NETFN_APP_REQUEST
)
1784 && ((msg
->cmd
== IPMI_SEND_MSG_CMD
)
1785 || (msg
->cmd
== IPMI_GET_MSG_CMD
)
1786 || (msg
->cmd
== IPMI_READ_EVENT_MSG_BUFFER_CMD
))) {
1788 * We don't let the user do these, since we manage
1789 * the sequence numbers.
1791 ipmi_inc_stat(intf
, sent_invalid_commands
);
1795 if (is_maintenance_mode_cmd(msg
)) {
1796 unsigned long flags
;
1798 spin_lock_irqsave(&intf
->maintenance_mode_lock
, flags
);
1799 intf
->auto_maintenance_timeout
1800 = maintenance_mode_timeout_ms
;
1801 if (!intf
->maintenance_mode
1802 && !intf
->maintenance_mode_enable
) {
1803 intf
->maintenance_mode_enable
= true;
1804 maintenance_mode_update(intf
);
1806 spin_unlock_irqrestore(&intf
->maintenance_mode_lock
,
1810 if (msg
->data_len
+ 2 > IPMI_MAX_MSG_LENGTH
) {
1811 ipmi_inc_stat(intf
, sent_invalid_commands
);
1815 smi_msg
->data
[0] = (msg
->netfn
<< 2) | (smi_addr
->lun
& 0x3);
1816 smi_msg
->data
[1] = msg
->cmd
;
1817 smi_msg
->msgid
= msgid
;
1818 smi_msg
->user_data
= recv_msg
;
1819 if (msg
->data_len
> 0)
1820 memcpy(&smi_msg
->data
[2], msg
->data
, msg
->data_len
);
1821 smi_msg
->data_size
= msg
->data_len
+ 2;
1822 ipmi_inc_stat(intf
, sent_local_commands
);
1827 static int i_ipmi_req_ipmb(struct ipmi_smi
*intf
,
1828 struct ipmi_addr
*addr
,
1830 struct kernel_ipmi_msg
*msg
,
1831 struct ipmi_smi_msg
*smi_msg
,
1832 struct ipmi_recv_msg
*recv_msg
,
1833 unsigned char source_address
,
1834 unsigned char source_lun
,
1836 unsigned int retry_time_ms
)
1838 struct ipmi_ipmb_addr
*ipmb_addr
;
1839 unsigned char ipmb_seq
;
1842 struct ipmi_channel
*chans
;
1845 if (addr
->channel
>= IPMI_MAX_CHANNELS
) {
1846 ipmi_inc_stat(intf
, sent_invalid_commands
);
1850 chans
= READ_ONCE(intf
->channel_list
)->c
;
1852 if (chans
[addr
->channel
].medium
!= IPMI_CHANNEL_MEDIUM_IPMB
) {
1853 ipmi_inc_stat(intf
, sent_invalid_commands
);
1857 if (addr
->addr_type
== IPMI_IPMB_BROADCAST_ADDR_TYPE
) {
1859 * Broadcasts add a zero at the beginning of the
1860 * message, but otherwise is the same as an IPMB
1863 addr
->addr_type
= IPMI_IPMB_ADDR_TYPE
;
1865 retries
= 0; /* Don't retry broadcasts. */
1869 * 9 for the header and 1 for the checksum, plus
1870 * possibly one for the broadcast.
1872 if ((msg
->data_len
+ 10 + broadcast
) > IPMI_MAX_MSG_LENGTH
) {
1873 ipmi_inc_stat(intf
, sent_invalid_commands
);
1877 ipmb_addr
= (struct ipmi_ipmb_addr
*) addr
;
1878 if (ipmb_addr
->lun
> 3) {
1879 ipmi_inc_stat(intf
, sent_invalid_commands
);
1883 memcpy(&recv_msg
->addr
, ipmb_addr
, sizeof(*ipmb_addr
));
1885 if (recv_msg
->msg
.netfn
& 0x1) {
1887 * It's a response, so use the user's sequence
1890 ipmi_inc_stat(intf
, sent_ipmb_responses
);
1891 format_ipmb_msg(smi_msg
, msg
, ipmb_addr
, msgid
,
1893 source_address
, source_lun
);
1896 * Save the receive message so we can use it
1897 * to deliver the response.
1899 smi_msg
->user_data
= recv_msg
;
1901 /* It's a command, so get a sequence for it. */
1902 unsigned long flags
;
1904 spin_lock_irqsave(&intf
->seq_lock
, flags
);
1906 if (is_maintenance_mode_cmd(msg
))
1907 intf
->ipmb_maintenance_mode_timeout
=
1908 maintenance_mode_timeout_ms
;
1910 if (intf
->ipmb_maintenance_mode_timeout
&& retry_time_ms
== 0)
1911 /* Different default in maintenance mode */
1912 retry_time_ms
= default_maintenance_retry_ms
;
1915 * Create a sequence number with a 1 second
1916 * timeout and 4 retries.
1918 rv
= intf_next_seq(intf
,
1927 * We have used up all the sequence numbers,
1928 * probably, so abort.
1932 ipmi_inc_stat(intf
, sent_ipmb_commands
);
1935 * Store the sequence number in the message,
1936 * so that when the send message response
1937 * comes back we can start the timer.
1939 format_ipmb_msg(smi_msg
, msg
, ipmb_addr
,
1940 STORE_SEQ_IN_MSGID(ipmb_seq
, seqid
),
1941 ipmb_seq
, broadcast
,
1942 source_address
, source_lun
);
1945 * Copy the message into the recv message data, so we
1946 * can retransmit it later if necessary.
1948 memcpy(recv_msg
->msg_data
, smi_msg
->data
,
1949 smi_msg
->data_size
);
1950 recv_msg
->msg
.data
= recv_msg
->msg_data
;
1951 recv_msg
->msg
.data_len
= smi_msg
->data_size
;
1954 * We don't unlock until here, because we need
1955 * to copy the completed message into the
1956 * recv_msg before we release the lock.
1957 * Otherwise, race conditions may bite us. I
1958 * know that's pretty paranoid, but I prefer
1962 spin_unlock_irqrestore(&intf
->seq_lock
, flags
);
1968 static int i_ipmi_req_lan(struct ipmi_smi
*intf
,
1969 struct ipmi_addr
*addr
,
1971 struct kernel_ipmi_msg
*msg
,
1972 struct ipmi_smi_msg
*smi_msg
,
1973 struct ipmi_recv_msg
*recv_msg
,
1974 unsigned char source_lun
,
1976 unsigned int retry_time_ms
)
1978 struct ipmi_lan_addr
*lan_addr
;
1979 unsigned char ipmb_seq
;
1981 struct ipmi_channel
*chans
;
1984 if (addr
->channel
>= IPMI_MAX_CHANNELS
) {
1985 ipmi_inc_stat(intf
, sent_invalid_commands
);
1989 chans
= READ_ONCE(intf
->channel_list
)->c
;
1991 if ((chans
[addr
->channel
].medium
1992 != IPMI_CHANNEL_MEDIUM_8023LAN
)
1993 && (chans
[addr
->channel
].medium
1994 != IPMI_CHANNEL_MEDIUM_ASYNC
)) {
1995 ipmi_inc_stat(intf
, sent_invalid_commands
);
1999 /* 11 for the header and 1 for the checksum. */
2000 if ((msg
->data_len
+ 12) > IPMI_MAX_MSG_LENGTH
) {
2001 ipmi_inc_stat(intf
, sent_invalid_commands
);
2005 lan_addr
= (struct ipmi_lan_addr
*) addr
;
2006 if (lan_addr
->lun
> 3) {
2007 ipmi_inc_stat(intf
, sent_invalid_commands
);
2011 memcpy(&recv_msg
->addr
, lan_addr
, sizeof(*lan_addr
));
2013 if (recv_msg
->msg
.netfn
& 0x1) {
2015 * It's a response, so use the user's sequence
2018 ipmi_inc_stat(intf
, sent_lan_responses
);
2019 format_lan_msg(smi_msg
, msg
, lan_addr
, msgid
,
2023 * Save the receive message so we can use it
2024 * to deliver the response.
2026 smi_msg
->user_data
= recv_msg
;
2028 /* It's a command, so get a sequence for it. */
2029 unsigned long flags
;
2031 spin_lock_irqsave(&intf
->seq_lock
, flags
);
2034 * Create a sequence number with a 1 second
2035 * timeout and 4 retries.
2037 rv
= intf_next_seq(intf
,
2046 * We have used up all the sequence numbers,
2047 * probably, so abort.
2051 ipmi_inc_stat(intf
, sent_lan_commands
);
2054 * Store the sequence number in the message,
2055 * so that when the send message response
2056 * comes back we can start the timer.
2058 format_lan_msg(smi_msg
, msg
, lan_addr
,
2059 STORE_SEQ_IN_MSGID(ipmb_seq
, seqid
),
2060 ipmb_seq
, source_lun
);
2063 * Copy the message into the recv message data, so we
2064 * can retransmit it later if necessary.
2066 memcpy(recv_msg
->msg_data
, smi_msg
->data
,
2067 smi_msg
->data_size
);
2068 recv_msg
->msg
.data
= recv_msg
->msg_data
;
2069 recv_msg
->msg
.data_len
= smi_msg
->data_size
;
2072 * We don't unlock until here, because we need
2073 * to copy the completed message into the
2074 * recv_msg before we release the lock.
2075 * Otherwise, race conditions may bite us. I
2076 * know that's pretty paranoid, but I prefer
2080 spin_unlock_irqrestore(&intf
->seq_lock
, flags
);
2087 * Separate from ipmi_request so that the user does not have to be
2088 * supplied in certain circumstances (mainly at panic time). If
2089 * messages are supplied, they will be freed, even if an error
2092 static int i_ipmi_request(struct ipmi_user
*user
,
2093 struct ipmi_smi
*intf
,
2094 struct ipmi_addr
*addr
,
2096 struct kernel_ipmi_msg
*msg
,
2097 void *user_msg_data
,
2099 struct ipmi_recv_msg
*supplied_recv
,
2101 unsigned char source_address
,
2102 unsigned char source_lun
,
2104 unsigned int retry_time_ms
)
2106 struct ipmi_smi_msg
*smi_msg
;
2107 struct ipmi_recv_msg
*recv_msg
;
2111 recv_msg
= supplied_recv
;
2113 recv_msg
= ipmi_alloc_recv_msg();
2114 if (recv_msg
== NULL
) {
2119 recv_msg
->user_msg_data
= user_msg_data
;
2122 smi_msg
= (struct ipmi_smi_msg
*) supplied_smi
;
2124 smi_msg
= ipmi_alloc_smi_msg();
2125 if (smi_msg
== NULL
) {
2126 ipmi_free_recv_msg(recv_msg
);
2133 if (intf
->in_shutdown
) {
2138 recv_msg
->user
= user
;
2140 /* The put happens when the message is freed. */
2141 kref_get(&user
->refcount
);
2142 recv_msg
->msgid
= msgid
;
2144 * Store the message to send in the receive message so timeout
2145 * responses can get the proper response data.
2147 recv_msg
->msg
= *msg
;
2149 if (addr
->addr_type
== IPMI_SYSTEM_INTERFACE_ADDR_TYPE
) {
2150 rv
= i_ipmi_req_sysintf(intf
, addr
, msgid
, msg
, smi_msg
,
2151 recv_msg
, retries
, retry_time_ms
);
2152 } else if (is_ipmb_addr(addr
) || is_ipmb_bcast_addr(addr
)) {
2153 rv
= i_ipmi_req_ipmb(intf
, addr
, msgid
, msg
, smi_msg
, recv_msg
,
2154 source_address
, source_lun
,
2155 retries
, retry_time_ms
);
2156 } else if (is_lan_addr(addr
)) {
2157 rv
= i_ipmi_req_lan(intf
, addr
, msgid
, msg
, smi_msg
, recv_msg
,
2158 source_lun
, retries
, retry_time_ms
);
2160 /* Unknown address type. */
2161 ipmi_inc_stat(intf
, sent_invalid_commands
);
2167 ipmi_free_smi_msg(smi_msg
);
2168 ipmi_free_recv_msg(recv_msg
);
2170 ipmi_debug_msg("Send", smi_msg
->data
, smi_msg
->data_size
);
2172 smi_send(intf
, intf
->handlers
, smi_msg
, priority
);
2180 static int check_addr(struct ipmi_smi
*intf
,
2181 struct ipmi_addr
*addr
,
2182 unsigned char *saddr
,
2185 if (addr
->channel
>= IPMI_MAX_CHANNELS
)
2187 *lun
= intf
->addrinfo
[addr
->channel
].lun
;
2188 *saddr
= intf
->addrinfo
[addr
->channel
].address
;
2192 int ipmi_request_settime(struct ipmi_user
*user
,
2193 struct ipmi_addr
*addr
,
2195 struct kernel_ipmi_msg
*msg
,
2196 void *user_msg_data
,
2199 unsigned int retry_time_ms
)
2201 unsigned char saddr
= 0, lun
= 0;
2207 user
= acquire_ipmi_user(user
, &index
);
2211 rv
= check_addr(user
->intf
, addr
, &saddr
, &lun
);
2213 rv
= i_ipmi_request(user
,
2226 release_ipmi_user(user
, index
);
2229 EXPORT_SYMBOL(ipmi_request_settime
);
2231 int ipmi_request_supply_msgs(struct ipmi_user
*user
,
2232 struct ipmi_addr
*addr
,
2234 struct kernel_ipmi_msg
*msg
,
2235 void *user_msg_data
,
2237 struct ipmi_recv_msg
*supplied_recv
,
2240 unsigned char saddr
= 0, lun
= 0;
2246 user
= acquire_ipmi_user(user
, &index
);
2250 rv
= check_addr(user
->intf
, addr
, &saddr
, &lun
);
2252 rv
= i_ipmi_request(user
,
2265 release_ipmi_user(user
, index
);
2268 EXPORT_SYMBOL(ipmi_request_supply_msgs
);
2270 static void bmc_device_id_handler(struct ipmi_smi
*intf
,
2271 struct ipmi_recv_msg
*msg
)
2275 if ((msg
->addr
.addr_type
!= IPMI_SYSTEM_INTERFACE_ADDR_TYPE
)
2276 || (msg
->msg
.netfn
!= IPMI_NETFN_APP_RESPONSE
)
2277 || (msg
->msg
.cmd
!= IPMI_GET_DEVICE_ID_CMD
)) {
2278 dev_warn(intf
->si_dev
,
2279 PFX
"invalid device_id msg: addr_type=%d netfn=%x cmd=%x\n",
2280 msg
->addr
.addr_type
, msg
->msg
.netfn
, msg
->msg
.cmd
);
2284 rv
= ipmi_demangle_device_id(msg
->msg
.netfn
, msg
->msg
.cmd
,
2285 msg
->msg
.data
, msg
->msg
.data_len
, &intf
->bmc
->fetch_id
);
2287 dev_warn(intf
->si_dev
,
2288 PFX
"device id demangle failed: %d\n", rv
);
2289 intf
->bmc
->dyn_id_set
= 0;
2292 * Make sure the id data is available before setting
2296 intf
->bmc
->dyn_id_set
= 1;
2299 wake_up(&intf
->waitq
);
2303 send_get_device_id_cmd(struct ipmi_smi
*intf
)
2305 struct ipmi_system_interface_addr si
;
2306 struct kernel_ipmi_msg msg
;
2308 si
.addr_type
= IPMI_SYSTEM_INTERFACE_ADDR_TYPE
;
2309 si
.channel
= IPMI_BMC_CHANNEL
;
2312 msg
.netfn
= IPMI_NETFN_APP_REQUEST
;
2313 msg
.cmd
= IPMI_GET_DEVICE_ID_CMD
;
2317 return i_ipmi_request(NULL
,
2319 (struct ipmi_addr
*) &si
,
2326 intf
->addrinfo
[0].address
,
2327 intf
->addrinfo
[0].lun
,
2331 static int __get_device_id(struct ipmi_smi
*intf
, struct bmc_device
*bmc
)
2335 bmc
->dyn_id_set
= 2;
2337 intf
->null_user_handler
= bmc_device_id_handler
;
2339 rv
= send_get_device_id_cmd(intf
);
2343 wait_event(intf
->waitq
, bmc
->dyn_id_set
!= 2);
2345 if (!bmc
->dyn_id_set
)
2346 rv
= -EIO
; /* Something went wrong in the fetch. */
2348 /* dyn_id_set makes the id data available. */
2351 intf
->null_user_handler
= NULL
;
2357 * Fetch the device id for the bmc/interface. You must pass in either
2358 * bmc or intf, this code will get the other one. If the data has
2359 * been recently fetched, this will just use the cached data. Otherwise
2360 * it will run a new fetch.
2362 * Except for the first time this is called (in ipmi_register_smi()),
2363 * this will always return good data;
2365 static int __bmc_get_device_id(struct ipmi_smi
*intf
, struct bmc_device
*bmc
,
2366 struct ipmi_device_id
*id
,
2367 bool *guid_set
, guid_t
*guid
, int intf_num
)
2370 int prev_dyn_id_set
, prev_guid_set
;
2371 bool intf_set
= intf
!= NULL
;
2374 mutex_lock(&bmc
->dyn_mutex
);
2376 if (list_empty(&bmc
->intfs
)) {
2377 mutex_unlock(&bmc
->dyn_mutex
);
2380 intf
= list_first_entry(&bmc
->intfs
, struct ipmi_smi
,
2382 kref_get(&intf
->refcount
);
2383 mutex_unlock(&bmc
->dyn_mutex
);
2384 mutex_lock(&intf
->bmc_reg_mutex
);
2385 mutex_lock(&bmc
->dyn_mutex
);
2386 if (intf
!= list_first_entry(&bmc
->intfs
, struct ipmi_smi
,
2388 mutex_unlock(&intf
->bmc_reg_mutex
);
2389 kref_put(&intf
->refcount
, intf_free
);
2390 goto retry_bmc_lock
;
2393 mutex_lock(&intf
->bmc_reg_mutex
);
2395 mutex_lock(&bmc
->dyn_mutex
);
2396 kref_get(&intf
->refcount
);
2399 /* If we have a valid and current ID, just return that. */
2400 if (intf
->in_bmc_register
||
2401 (bmc
->dyn_id_set
&& time_is_after_jiffies(bmc
->dyn_id_expiry
)))
2402 goto out_noprocessing
;
2404 prev_guid_set
= bmc
->dyn_guid_set
;
2407 prev_dyn_id_set
= bmc
->dyn_id_set
;
2408 rv
= __get_device_id(intf
, bmc
);
2413 * The guid, device id, manufacturer id, and product id should
2414 * not change on a BMC. If it does we have to do some dancing.
2416 if (!intf
->bmc_registered
2417 || (!prev_guid_set
&& bmc
->dyn_guid_set
)
2418 || (!prev_dyn_id_set
&& bmc
->dyn_id_set
)
2419 || (prev_guid_set
&& bmc
->dyn_guid_set
2420 && !guid_equal(&bmc
->guid
, &bmc
->fetch_guid
))
2421 || bmc
->id
.device_id
!= bmc
->fetch_id
.device_id
2422 || bmc
->id
.manufacturer_id
!= bmc
->fetch_id
.manufacturer_id
2423 || bmc
->id
.product_id
!= bmc
->fetch_id
.product_id
) {
2424 struct ipmi_device_id id
= bmc
->fetch_id
;
2425 int guid_set
= bmc
->dyn_guid_set
;
2428 guid
= bmc
->fetch_guid
;
2429 mutex_unlock(&bmc
->dyn_mutex
);
2431 __ipmi_bmc_unregister(intf
);
2432 /* Fill in the temporary BMC for good measure. */
2434 intf
->bmc
->dyn_guid_set
= guid_set
;
2435 intf
->bmc
->guid
= guid
;
2436 if (__ipmi_bmc_register(intf
, &id
, guid_set
, &guid
, intf_num
))
2437 need_waiter(intf
); /* Retry later on an error. */
2439 __scan_channels(intf
, &id
);
2444 * We weren't given the interface on the
2445 * command line, so restart the operation on
2446 * the next interface for the BMC.
2448 mutex_unlock(&intf
->bmc_reg_mutex
);
2449 mutex_lock(&bmc
->dyn_mutex
);
2450 goto retry_bmc_lock
;
2453 /* We have a new BMC, set it up. */
2455 mutex_lock(&bmc
->dyn_mutex
);
2456 goto out_noprocessing
;
2457 } else if (memcmp(&bmc
->fetch_id
, &bmc
->id
, sizeof(bmc
->id
)))
2458 /* Version info changes, scan the channels again. */
2459 __scan_channels(intf
, &bmc
->fetch_id
);
2461 bmc
->dyn_id_expiry
= jiffies
+ IPMI_DYN_DEV_ID_EXPIRY
;
2464 if (rv
&& prev_dyn_id_set
) {
2465 rv
= 0; /* Ignore failures if we have previous data. */
2466 bmc
->dyn_id_set
= prev_dyn_id_set
;
2469 bmc
->id
= bmc
->fetch_id
;
2470 if (bmc
->dyn_guid_set
)
2471 bmc
->guid
= bmc
->fetch_guid
;
2472 else if (prev_guid_set
)
2474 * The guid used to be valid and it failed to fetch,
2475 * just use the cached value.
2477 bmc
->dyn_guid_set
= prev_guid_set
;
2485 *guid_set
= bmc
->dyn_guid_set
;
2487 if (guid
&& bmc
->dyn_guid_set
)
2491 mutex_unlock(&bmc
->dyn_mutex
);
2492 mutex_unlock(&intf
->bmc_reg_mutex
);
2494 kref_put(&intf
->refcount
, intf_free
);
2498 static int bmc_get_device_id(struct ipmi_smi
*intf
, struct bmc_device
*bmc
,
2499 struct ipmi_device_id
*id
,
2500 bool *guid_set
, guid_t
*guid
)
2502 return __bmc_get_device_id(intf
, bmc
, id
, guid_set
, guid
, -1);
2505 static ssize_t
device_id_show(struct device
*dev
,
2506 struct device_attribute
*attr
,
2509 struct bmc_device
*bmc
= to_bmc_device(dev
);
2510 struct ipmi_device_id id
;
2513 rv
= bmc_get_device_id(NULL
, bmc
, &id
, NULL
, NULL
);
2517 return snprintf(buf
, 10, "%u\n", id
.device_id
);
2519 static DEVICE_ATTR_RO(device_id
);
2521 static ssize_t
provides_device_sdrs_show(struct device
*dev
,
2522 struct device_attribute
*attr
,
2525 struct bmc_device
*bmc
= to_bmc_device(dev
);
2526 struct ipmi_device_id id
;
2529 rv
= bmc_get_device_id(NULL
, bmc
, &id
, NULL
, NULL
);
2533 return snprintf(buf
, 10, "%u\n", (id
.device_revision
& 0x80) >> 7);
2535 static DEVICE_ATTR_RO(provides_device_sdrs
);
2537 static ssize_t
revision_show(struct device
*dev
, struct device_attribute
*attr
,
2540 struct bmc_device
*bmc
= to_bmc_device(dev
);
2541 struct ipmi_device_id id
;
2544 rv
= bmc_get_device_id(NULL
, bmc
, &id
, NULL
, NULL
);
2548 return snprintf(buf
, 20, "%u\n", id
.device_revision
& 0x0F);
2550 static DEVICE_ATTR_RO(revision
);
2552 static ssize_t
firmware_revision_show(struct device
*dev
,
2553 struct device_attribute
*attr
,
2556 struct bmc_device
*bmc
= to_bmc_device(dev
);
2557 struct ipmi_device_id id
;
2560 rv
= bmc_get_device_id(NULL
, bmc
, &id
, NULL
, NULL
);
2564 return snprintf(buf
, 20, "%u.%x\n", id
.firmware_revision_1
,
2565 id
.firmware_revision_2
);
2567 static DEVICE_ATTR_RO(firmware_revision
);
2569 static ssize_t
ipmi_version_show(struct device
*dev
,
2570 struct device_attribute
*attr
,
2573 struct bmc_device
*bmc
= to_bmc_device(dev
);
2574 struct ipmi_device_id id
;
2577 rv
= bmc_get_device_id(NULL
, bmc
, &id
, NULL
, NULL
);
2581 return snprintf(buf
, 20, "%u.%u\n",
2582 ipmi_version_major(&id
),
2583 ipmi_version_minor(&id
));
2585 static DEVICE_ATTR_RO(ipmi_version
);
2587 static ssize_t
add_dev_support_show(struct device
*dev
,
2588 struct device_attribute
*attr
,
2591 struct bmc_device
*bmc
= to_bmc_device(dev
);
2592 struct ipmi_device_id id
;
2595 rv
= bmc_get_device_id(NULL
, bmc
, &id
, NULL
, NULL
);
2599 return snprintf(buf
, 10, "0x%02x\n", id
.additional_device_support
);
2601 static DEVICE_ATTR(additional_device_support
, S_IRUGO
, add_dev_support_show
,
2604 static ssize_t
manufacturer_id_show(struct device
*dev
,
2605 struct device_attribute
*attr
,
2608 struct bmc_device
*bmc
= to_bmc_device(dev
);
2609 struct ipmi_device_id id
;
2612 rv
= bmc_get_device_id(NULL
, bmc
, &id
, NULL
, NULL
);
2616 return snprintf(buf
, 20, "0x%6.6x\n", id
.manufacturer_id
);
2618 static DEVICE_ATTR_RO(manufacturer_id
);
2620 static ssize_t
product_id_show(struct device
*dev
,
2621 struct device_attribute
*attr
,
2624 struct bmc_device
*bmc
= to_bmc_device(dev
);
2625 struct ipmi_device_id id
;
2628 rv
= bmc_get_device_id(NULL
, bmc
, &id
, NULL
, NULL
);
2632 return snprintf(buf
, 10, "0x%4.4x\n", id
.product_id
);
2634 static DEVICE_ATTR_RO(product_id
);
2636 static ssize_t
aux_firmware_rev_show(struct device
*dev
,
2637 struct device_attribute
*attr
,
2640 struct bmc_device
*bmc
= to_bmc_device(dev
);
2641 struct ipmi_device_id id
;
2644 rv
= bmc_get_device_id(NULL
, bmc
, &id
, NULL
, NULL
);
2648 return snprintf(buf
, 21, "0x%02x 0x%02x 0x%02x 0x%02x\n",
2649 id
.aux_firmware_revision
[3],
2650 id
.aux_firmware_revision
[2],
2651 id
.aux_firmware_revision
[1],
2652 id
.aux_firmware_revision
[0]);
2654 static DEVICE_ATTR(aux_firmware_revision
, S_IRUGO
, aux_firmware_rev_show
, NULL
);
2656 static ssize_t
guid_show(struct device
*dev
, struct device_attribute
*attr
,
2659 struct bmc_device
*bmc
= to_bmc_device(dev
);
2664 rv
= bmc_get_device_id(NULL
, bmc
, NULL
, &guid_set
, &guid
);
2670 return snprintf(buf
, 38, "%pUl\n", guid
.b
);
2672 static DEVICE_ATTR_RO(guid
);
2674 static struct attribute
*bmc_dev_attrs
[] = {
2675 &dev_attr_device_id
.attr
,
2676 &dev_attr_provides_device_sdrs
.attr
,
2677 &dev_attr_revision
.attr
,
2678 &dev_attr_firmware_revision
.attr
,
2679 &dev_attr_ipmi_version
.attr
,
2680 &dev_attr_additional_device_support
.attr
,
2681 &dev_attr_manufacturer_id
.attr
,
2682 &dev_attr_product_id
.attr
,
2683 &dev_attr_aux_firmware_revision
.attr
,
2684 &dev_attr_guid
.attr
,
2688 static umode_t
bmc_dev_attr_is_visible(struct kobject
*kobj
,
2689 struct attribute
*attr
, int idx
)
2691 struct device
*dev
= kobj_to_dev(kobj
);
2692 struct bmc_device
*bmc
= to_bmc_device(dev
);
2693 umode_t mode
= attr
->mode
;
2696 if (attr
== &dev_attr_aux_firmware_revision
.attr
) {
2697 struct ipmi_device_id id
;
2699 rv
= bmc_get_device_id(NULL
, bmc
, &id
, NULL
, NULL
);
2700 return (!rv
&& id
.aux_firmware_revision_set
) ? mode
: 0;
2702 if (attr
== &dev_attr_guid
.attr
) {
2705 rv
= bmc_get_device_id(NULL
, bmc
, NULL
, &guid_set
, NULL
);
2706 return (!rv
&& guid_set
) ? mode
: 0;
2711 static const struct attribute_group bmc_dev_attr_group
= {
2712 .attrs
= bmc_dev_attrs
,
2713 .is_visible
= bmc_dev_attr_is_visible
,
2716 static const struct attribute_group
*bmc_dev_attr_groups
[] = {
2717 &bmc_dev_attr_group
,
2721 static const struct device_type bmc_device_type
= {
2722 .groups
= bmc_dev_attr_groups
,
2725 static int __find_bmc_guid(struct device
*dev
, void *data
)
2727 guid_t
*guid
= data
;
2728 struct bmc_device
*bmc
;
2731 if (dev
->type
!= &bmc_device_type
)
2734 bmc
= to_bmc_device(dev
);
2735 rv
= bmc
->dyn_guid_set
&& guid_equal(&bmc
->guid
, guid
);
2737 rv
= kref_get_unless_zero(&bmc
->usecount
);
2742 * Returns with the bmc's usecount incremented, if it is non-NULL.
2744 static struct bmc_device
*ipmi_find_bmc_guid(struct device_driver
*drv
,
2748 struct bmc_device
*bmc
= NULL
;
2750 dev
= driver_find_device(drv
, NULL
, guid
, __find_bmc_guid
);
2752 bmc
= to_bmc_device(dev
);
2758 struct prod_dev_id
{
2759 unsigned int product_id
;
2760 unsigned char device_id
;
2763 static int __find_bmc_prod_dev_id(struct device
*dev
, void *data
)
2765 struct prod_dev_id
*cid
= data
;
2766 struct bmc_device
*bmc
;
2769 if (dev
->type
!= &bmc_device_type
)
2772 bmc
= to_bmc_device(dev
);
2773 rv
= (bmc
->id
.product_id
== cid
->product_id
2774 && bmc
->id
.device_id
== cid
->device_id
);
2776 rv
= kref_get_unless_zero(&bmc
->usecount
);
2781 * Returns with the bmc's usecount incremented, if it is non-NULL.
2783 static struct bmc_device
*ipmi_find_bmc_prod_dev_id(
2784 struct device_driver
*drv
,
2785 unsigned int product_id
, unsigned char device_id
)
2787 struct prod_dev_id id
= {
2788 .product_id
= product_id
,
2789 .device_id
= device_id
,
2792 struct bmc_device
*bmc
= NULL
;
2794 dev
= driver_find_device(drv
, NULL
, &id
, __find_bmc_prod_dev_id
);
2796 bmc
= to_bmc_device(dev
);
2802 static DEFINE_IDA(ipmi_bmc_ida
);
2805 release_bmc_device(struct device
*dev
)
2807 kfree(to_bmc_device(dev
));
2810 static void cleanup_bmc_work(struct work_struct
*work
)
2812 struct bmc_device
*bmc
= container_of(work
, struct bmc_device
,
2814 int id
= bmc
->pdev
.id
; /* Unregister overwrites id */
2816 platform_device_unregister(&bmc
->pdev
);
2817 ida_simple_remove(&ipmi_bmc_ida
, id
);
2821 cleanup_bmc_device(struct kref
*ref
)
2823 struct bmc_device
*bmc
= container_of(ref
, struct bmc_device
, usecount
);
2826 * Remove the platform device in a work queue to avoid issues
2827 * with removing the device attributes while reading a device
2830 schedule_work(&bmc
->remove_work
);
2834 * Must be called with intf->bmc_reg_mutex held.
2836 static void __ipmi_bmc_unregister(struct ipmi_smi
*intf
)
2838 struct bmc_device
*bmc
= intf
->bmc
;
2840 if (!intf
->bmc_registered
)
2843 sysfs_remove_link(&intf
->si_dev
->kobj
, "bmc");
2844 sysfs_remove_link(&bmc
->pdev
.dev
.kobj
, intf
->my_dev_name
);
2845 kfree(intf
->my_dev_name
);
2846 intf
->my_dev_name
= NULL
;
2848 mutex_lock(&bmc
->dyn_mutex
);
2849 list_del(&intf
->bmc_link
);
2850 mutex_unlock(&bmc
->dyn_mutex
);
2851 intf
->bmc
= &intf
->tmp_bmc
;
2852 kref_put(&bmc
->usecount
, cleanup_bmc_device
);
2853 intf
->bmc_registered
= false;
2856 static void ipmi_bmc_unregister(struct ipmi_smi
*intf
)
2858 mutex_lock(&intf
->bmc_reg_mutex
);
2859 __ipmi_bmc_unregister(intf
);
2860 mutex_unlock(&intf
->bmc_reg_mutex
);
2864 * Must be called with intf->bmc_reg_mutex held.
2866 static int __ipmi_bmc_register(struct ipmi_smi
*intf
,
2867 struct ipmi_device_id
*id
,
2868 bool guid_set
, guid_t
*guid
, int intf_num
)
2871 struct bmc_device
*bmc
;
2872 struct bmc_device
*old_bmc
;
2875 * platform_device_register() can cause bmc_reg_mutex to
2876 * be claimed because of the is_visible functions of
2877 * the attributes. Eliminate possible recursion and
2880 intf
->in_bmc_register
= true;
2881 mutex_unlock(&intf
->bmc_reg_mutex
);
2884 * Try to find if there is an bmc_device struct
2885 * representing the interfaced BMC already
2887 mutex_lock(&ipmidriver_mutex
);
2889 old_bmc
= ipmi_find_bmc_guid(&ipmidriver
.driver
, guid
);
2891 old_bmc
= ipmi_find_bmc_prod_dev_id(&ipmidriver
.driver
,
2896 * If there is already an bmc_device, free the new one,
2897 * otherwise register the new BMC device
2902 * Note: old_bmc already has usecount incremented by
2903 * the BMC find functions.
2905 intf
->bmc
= old_bmc
;
2906 mutex_lock(&bmc
->dyn_mutex
);
2907 list_add_tail(&intf
->bmc_link
, &bmc
->intfs
);
2908 mutex_unlock(&bmc
->dyn_mutex
);
2910 dev_info(intf
->si_dev
,
2911 "ipmi: interfacing existing BMC (man_id: 0x%6.6x,"
2912 " prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
2913 bmc
->id
.manufacturer_id
,
2917 bmc
= kzalloc(sizeof(*bmc
), GFP_KERNEL
);
2922 INIT_LIST_HEAD(&bmc
->intfs
);
2923 mutex_init(&bmc
->dyn_mutex
);
2924 INIT_WORK(&bmc
->remove_work
, cleanup_bmc_work
);
2927 bmc
->dyn_id_set
= 1;
2928 bmc
->dyn_guid_set
= guid_set
;
2930 bmc
->dyn_id_expiry
= jiffies
+ IPMI_DYN_DEV_ID_EXPIRY
;
2932 bmc
->pdev
.name
= "ipmi_bmc";
2934 rv
= ida_simple_get(&ipmi_bmc_ida
, 0, 0, GFP_KERNEL
);
2937 bmc
->pdev
.dev
.driver
= &ipmidriver
.driver
;
2939 bmc
->pdev
.dev
.release
= release_bmc_device
;
2940 bmc
->pdev
.dev
.type
= &bmc_device_type
;
2941 kref_init(&bmc
->usecount
);
2944 mutex_lock(&bmc
->dyn_mutex
);
2945 list_add_tail(&intf
->bmc_link
, &bmc
->intfs
);
2946 mutex_unlock(&bmc
->dyn_mutex
);
2948 rv
= platform_device_register(&bmc
->pdev
);
2950 dev_err(intf
->si_dev
,
2951 PFX
" Unable to register bmc device: %d\n",
2956 dev_info(intf
->si_dev
,
2957 "Found new BMC (man_id: 0x%6.6x, prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
2958 bmc
->id
.manufacturer_id
,
2964 * create symlink from system interface device to bmc device
2967 rv
= sysfs_create_link(&intf
->si_dev
->kobj
, &bmc
->pdev
.dev
.kobj
, "bmc");
2969 dev_err(intf
->si_dev
,
2970 PFX
"Unable to create bmc symlink: %d\n", rv
);
2975 intf_num
= intf
->intf_num
;
2976 intf
->my_dev_name
= kasprintf(GFP_KERNEL
, "ipmi%d", intf_num
);
2977 if (!intf
->my_dev_name
) {
2979 dev_err(intf
->si_dev
,
2980 PFX
"Unable to allocate link from BMC: %d\n", rv
);
2984 rv
= sysfs_create_link(&bmc
->pdev
.dev
.kobj
, &intf
->si_dev
->kobj
,
2987 kfree(intf
->my_dev_name
);
2988 intf
->my_dev_name
= NULL
;
2989 dev_err(intf
->si_dev
,
2990 PFX
"Unable to create symlink to bmc: %d\n", rv
);
2991 goto out_free_my_dev_name
;
2994 intf
->bmc_registered
= true;
2997 mutex_unlock(&ipmidriver_mutex
);
2998 mutex_lock(&intf
->bmc_reg_mutex
);
2999 intf
->in_bmc_register
= false;
3003 out_free_my_dev_name
:
3004 kfree(intf
->my_dev_name
);
3005 intf
->my_dev_name
= NULL
;
3008 sysfs_remove_link(&intf
->si_dev
->kobj
, "bmc");
3011 mutex_lock(&bmc
->dyn_mutex
);
3012 list_del(&intf
->bmc_link
);
3013 mutex_unlock(&bmc
->dyn_mutex
);
3014 intf
->bmc
= &intf
->tmp_bmc
;
3015 kref_put(&bmc
->usecount
, cleanup_bmc_device
);
3019 mutex_lock(&bmc
->dyn_mutex
);
3020 list_del(&intf
->bmc_link
);
3021 mutex_unlock(&bmc
->dyn_mutex
);
3022 intf
->bmc
= &intf
->tmp_bmc
;
3023 put_device(&bmc
->pdev
.dev
);
3028 send_guid_cmd(struct ipmi_smi
*intf
, int chan
)
3030 struct kernel_ipmi_msg msg
;
3031 struct ipmi_system_interface_addr si
;
3033 si
.addr_type
= IPMI_SYSTEM_INTERFACE_ADDR_TYPE
;
3034 si
.channel
= IPMI_BMC_CHANNEL
;
3037 msg
.netfn
= IPMI_NETFN_APP_REQUEST
;
3038 msg
.cmd
= IPMI_GET_DEVICE_GUID_CMD
;
3041 return i_ipmi_request(NULL
,
3043 (struct ipmi_addr
*) &si
,
3050 intf
->addrinfo
[0].address
,
3051 intf
->addrinfo
[0].lun
,
3055 static void guid_handler(struct ipmi_smi
*intf
, struct ipmi_recv_msg
*msg
)
3057 struct bmc_device
*bmc
= intf
->bmc
;
3059 if ((msg
->addr
.addr_type
!= IPMI_SYSTEM_INTERFACE_ADDR_TYPE
)
3060 || (msg
->msg
.netfn
!= IPMI_NETFN_APP_RESPONSE
)
3061 || (msg
->msg
.cmd
!= IPMI_GET_DEVICE_GUID_CMD
))
3065 if (msg
->msg
.data
[0] != 0) {
3066 /* Error from getting the GUID, the BMC doesn't have one. */
3067 bmc
->dyn_guid_set
= 0;
3071 if (msg
->msg
.data_len
< 17) {
3072 bmc
->dyn_guid_set
= 0;
3073 dev_warn(intf
->si_dev
,
3074 PFX
"The GUID response from the BMC was too short, it was %d but should have been 17. Assuming GUID is not available.\n",
3079 memcpy(bmc
->fetch_guid
.b
, msg
->msg
.data
+ 1, 16);
3081 * Make sure the guid data is available before setting
3085 bmc
->dyn_guid_set
= 1;
3087 wake_up(&intf
->waitq
);
3090 static void __get_guid(struct ipmi_smi
*intf
)
3093 struct bmc_device
*bmc
= intf
->bmc
;
3095 bmc
->dyn_guid_set
= 2;
3096 intf
->null_user_handler
= guid_handler
;
3097 rv
= send_guid_cmd(intf
, 0);
3099 /* Send failed, no GUID available. */
3100 bmc
->dyn_guid_set
= 0;
3102 wait_event(intf
->waitq
, bmc
->dyn_guid_set
!= 2);
3104 /* dyn_guid_set makes the guid data available. */
3107 intf
->null_user_handler
= NULL
;
3111 send_channel_info_cmd(struct ipmi_smi
*intf
, int chan
)
3113 struct kernel_ipmi_msg msg
;
3114 unsigned char data
[1];
3115 struct ipmi_system_interface_addr si
;
3117 si
.addr_type
= IPMI_SYSTEM_INTERFACE_ADDR_TYPE
;
3118 si
.channel
= IPMI_BMC_CHANNEL
;
3121 msg
.netfn
= IPMI_NETFN_APP_REQUEST
;
3122 msg
.cmd
= IPMI_GET_CHANNEL_INFO_CMD
;
3126 return i_ipmi_request(NULL
,
3128 (struct ipmi_addr
*) &si
,
3135 intf
->addrinfo
[0].address
,
3136 intf
->addrinfo
[0].lun
,
3141 channel_handler(struct ipmi_smi
*intf
, struct ipmi_recv_msg
*msg
)
3145 unsigned int set
= intf
->curr_working_cset
;
3146 struct ipmi_channel
*chans
;
3148 if ((msg
->addr
.addr_type
== IPMI_SYSTEM_INTERFACE_ADDR_TYPE
)
3149 && (msg
->msg
.netfn
== IPMI_NETFN_APP_RESPONSE
)
3150 && (msg
->msg
.cmd
== IPMI_GET_CHANNEL_INFO_CMD
)) {
3151 /* It's the one we want */
3152 if (msg
->msg
.data
[0] != 0) {
3153 /* Got an error from the channel, just go on. */
3155 if (msg
->msg
.data
[0] == IPMI_INVALID_COMMAND_ERR
) {
3157 * If the MC does not support this
3158 * command, that is legal. We just
3159 * assume it has one IPMB at channel
3162 intf
->wchannels
[set
].c
[0].medium
3163 = IPMI_CHANNEL_MEDIUM_IPMB
;
3164 intf
->wchannels
[set
].c
[0].protocol
3165 = IPMI_CHANNEL_PROTOCOL_IPMB
;
3167 intf
->channel_list
= intf
->wchannels
+ set
;
3168 intf
->channels_ready
= true;
3169 wake_up(&intf
->waitq
);
3174 if (msg
->msg
.data_len
< 4) {
3175 /* Message not big enough, just go on. */
3178 ch
= intf
->curr_channel
;
3179 chans
= intf
->wchannels
[set
].c
;
3180 chans
[ch
].medium
= msg
->msg
.data
[2] & 0x7f;
3181 chans
[ch
].protocol
= msg
->msg
.data
[3] & 0x1f;
3184 intf
->curr_channel
++;
3185 if (intf
->curr_channel
>= IPMI_MAX_CHANNELS
) {
3186 intf
->channel_list
= intf
->wchannels
+ set
;
3187 intf
->channels_ready
= true;
3188 wake_up(&intf
->waitq
);
3190 intf
->channel_list
= intf
->wchannels
+ set
;
3191 intf
->channels_ready
= true;
3192 rv
= send_channel_info_cmd(intf
, intf
->curr_channel
);
3196 /* Got an error somehow, just give up. */
3197 dev_warn(intf
->si_dev
,
3198 PFX
"Error sending channel information for channel %d: %d\n",
3199 intf
->curr_channel
, rv
);
3201 intf
->channel_list
= intf
->wchannels
+ set
;
3202 intf
->channels_ready
= true;
3203 wake_up(&intf
->waitq
);
3211 * Must be holding intf->bmc_reg_mutex to call this.
3213 static int __scan_channels(struct ipmi_smi
*intf
, struct ipmi_device_id
*id
)
3217 if (ipmi_version_major(id
) > 1
3218 || (ipmi_version_major(id
) == 1
3219 && ipmi_version_minor(id
) >= 5)) {
3223 * Start scanning the channels to see what is
3226 set
= !intf
->curr_working_cset
;
3227 intf
->curr_working_cset
= set
;
3228 memset(&intf
->wchannels
[set
], 0,
3229 sizeof(struct ipmi_channel_set
));
3231 intf
->null_user_handler
= channel_handler
;
3232 intf
->curr_channel
= 0;
3233 rv
= send_channel_info_cmd(intf
, 0);
3235 dev_warn(intf
->si_dev
,
3236 "Error sending channel information for channel 0, %d\n",
3241 /* Wait for the channel info to be read. */
3242 wait_event(intf
->waitq
, intf
->channels_ready
);
3243 intf
->null_user_handler
= NULL
;
3245 unsigned int set
= intf
->curr_working_cset
;
3247 /* Assume a single IPMB channel at zero. */
3248 intf
->wchannels
[set
].c
[0].medium
= IPMI_CHANNEL_MEDIUM_IPMB
;
3249 intf
->wchannels
[set
].c
[0].protocol
= IPMI_CHANNEL_PROTOCOL_IPMB
;
3250 intf
->channel_list
= intf
->wchannels
+ set
;
3251 intf
->channels_ready
= true;
3257 static void ipmi_poll(struct ipmi_smi
*intf
)
3259 if (intf
->handlers
->poll
)
3260 intf
->handlers
->poll(intf
->send_info
);
3261 /* In case something came in */
3262 handle_new_recv_msgs(intf
);
3265 void ipmi_poll_interface(struct ipmi_user
*user
)
3267 ipmi_poll(user
->intf
);
3269 EXPORT_SYMBOL(ipmi_poll_interface
);
3271 static void redo_bmc_reg(struct work_struct
*work
)
3273 struct ipmi_smi
*intf
= container_of(work
, struct ipmi_smi
,
3276 if (!intf
->in_shutdown
)
3277 bmc_get_device_id(intf
, NULL
, NULL
, NULL
, NULL
);
3279 kref_put(&intf
->refcount
, intf_free
);
3282 int ipmi_register_smi(const struct ipmi_smi_handlers
*handlers
,
3284 struct device
*si_dev
,
3285 unsigned char slave_addr
)
3289 struct ipmi_smi
*intf
, *tintf
;
3290 struct list_head
*link
;
3291 struct ipmi_device_id id
;
3294 * Make sure the driver is actually initialized, this handles
3295 * problems with initialization order.
3298 rv
= ipmi_init_msghandler();
3302 * The init code doesn't return an error if it was turned
3303 * off, but it won't initialize. Check that.
3309 intf
= kzalloc(sizeof(*intf
), GFP_KERNEL
);
3313 rv
= init_srcu_struct(&intf
->users_srcu
);
3320 intf
->bmc
= &intf
->tmp_bmc
;
3321 INIT_LIST_HEAD(&intf
->bmc
->intfs
);
3322 mutex_init(&intf
->bmc
->dyn_mutex
);
3323 INIT_LIST_HEAD(&intf
->bmc_link
);
3324 mutex_init(&intf
->bmc_reg_mutex
);
3325 intf
->intf_num
= -1; /* Mark it invalid for now. */
3326 kref_init(&intf
->refcount
);
3327 INIT_WORK(&intf
->bmc_reg_work
, redo_bmc_reg
);
3328 intf
->si_dev
= si_dev
;
3329 for (j
= 0; j
< IPMI_MAX_CHANNELS
; j
++) {
3330 intf
->addrinfo
[j
].address
= IPMI_BMC_SLAVE_ADDR
;
3331 intf
->addrinfo
[j
].lun
= 2;
3333 if (slave_addr
!= 0)
3334 intf
->addrinfo
[0].address
= slave_addr
;
3335 INIT_LIST_HEAD(&intf
->users
);
3336 intf
->handlers
= handlers
;
3337 intf
->send_info
= send_info
;
3338 spin_lock_init(&intf
->seq_lock
);
3339 for (j
= 0; j
< IPMI_IPMB_NUM_SEQ
; j
++) {
3340 intf
->seq_table
[j
].inuse
= 0;
3341 intf
->seq_table
[j
].seqid
= 0;
3344 spin_lock_init(&intf
->waiting_rcv_msgs_lock
);
3345 INIT_LIST_HEAD(&intf
->waiting_rcv_msgs
);
3346 tasklet_init(&intf
->recv_tasklet
,
3348 (unsigned long) intf
);
3349 atomic_set(&intf
->watchdog_pretimeouts_to_deliver
, 0);
3350 spin_lock_init(&intf
->xmit_msgs_lock
);
3351 INIT_LIST_HEAD(&intf
->xmit_msgs
);
3352 INIT_LIST_HEAD(&intf
->hp_xmit_msgs
);
3353 spin_lock_init(&intf
->events_lock
);
3354 atomic_set(&intf
->event_waiters
, 0);
3355 intf
->ticks_to_req_ev
= IPMI_REQUEST_EV_TIME
;
3356 INIT_LIST_HEAD(&intf
->waiting_events
);
3357 intf
->waiting_events_count
= 0;
3358 mutex_init(&intf
->cmd_rcvrs_mutex
);
3359 spin_lock_init(&intf
->maintenance_mode_lock
);
3360 INIT_LIST_HEAD(&intf
->cmd_rcvrs
);
3361 init_waitqueue_head(&intf
->waitq
);
3362 for (i
= 0; i
< IPMI_NUM_STATS
; i
++)
3363 atomic_set(&intf
->stats
[i
], 0);
3365 mutex_lock(&ipmi_interfaces_mutex
);
3366 /* Look for a hole in the numbers. */
3368 link
= &ipmi_interfaces
;
3369 list_for_each_entry_rcu(tintf
, &ipmi_interfaces
, link
) {
3370 if (tintf
->intf_num
!= i
) {
3371 link
= &tintf
->link
;
3376 /* Add the new interface in numeric order. */
3378 list_add_rcu(&intf
->link
, &ipmi_interfaces
);
3380 list_add_tail_rcu(&intf
->link
, link
);
3382 rv
= handlers
->start_processing(send_info
, intf
);
3386 rv
= __bmc_get_device_id(intf
, NULL
, &id
, NULL
, NULL
, i
);
3388 dev_err(si_dev
, "Unable to get the device id: %d\n", rv
);
3389 goto out_err_started
;
3392 mutex_lock(&intf
->bmc_reg_mutex
);
3393 rv
= __scan_channels(intf
, &id
);
3394 mutex_unlock(&intf
->bmc_reg_mutex
);
3396 goto out_err_bmc_reg
;
3399 * Keep memory order straight for RCU readers. Make
3400 * sure everything else is committed to memory before
3401 * setting intf_num to mark the interface valid.
3405 mutex_unlock(&ipmi_interfaces_mutex
);
3407 /* After this point the interface is legal to use. */
3408 call_smi_watchers(i
, intf
->si_dev
);
3413 ipmi_bmc_unregister(intf
);
3415 if (intf
->handlers
->shutdown
)
3416 intf
->handlers
->shutdown(intf
->send_info
);
3418 list_del_rcu(&intf
->link
);
3419 mutex_unlock(&ipmi_interfaces_mutex
);
3420 synchronize_srcu(&ipmi_interfaces_srcu
);
3421 cleanup_srcu_struct(&intf
->users_srcu
);
3422 kref_put(&intf
->refcount
, intf_free
);
3426 EXPORT_SYMBOL(ipmi_register_smi
);
3428 static void deliver_smi_err_response(struct ipmi_smi
*intf
,
3429 struct ipmi_smi_msg
*msg
,
3432 msg
->rsp
[0] = msg
->data
[0] | 4;
3433 msg
->rsp
[1] = msg
->data
[1];
3436 /* It's an error, so it will never requeue, no need to check return. */
3437 handle_one_recv_msg(intf
, msg
);
3440 static void cleanup_smi_msgs(struct ipmi_smi
*intf
)
3443 struct seq_table
*ent
;
3444 struct ipmi_smi_msg
*msg
;
3445 struct list_head
*entry
;
3446 struct list_head tmplist
;
3448 /* Clear out our transmit queues and hold the messages. */
3449 INIT_LIST_HEAD(&tmplist
);
3450 list_splice_tail(&intf
->hp_xmit_msgs
, &tmplist
);
3451 list_splice_tail(&intf
->xmit_msgs
, &tmplist
);
3453 /* Current message first, to preserve order */
3454 while (intf
->curr_msg
&& !list_empty(&intf
->waiting_rcv_msgs
)) {
3455 /* Wait for the message to clear out. */
3456 schedule_timeout(1);
3459 /* No need for locks, the interface is down. */
3462 * Return errors for all pending messages in queue and in the
3463 * tables waiting for remote responses.
3465 while (!list_empty(&tmplist
)) {
3466 entry
= tmplist
.next
;
3468 msg
= list_entry(entry
, struct ipmi_smi_msg
, link
);
3469 deliver_smi_err_response(intf
, msg
, IPMI_ERR_UNSPECIFIED
);
3472 for (i
= 0; i
< IPMI_IPMB_NUM_SEQ
; i
++) {
3473 ent
= &intf
->seq_table
[i
];
3476 deliver_err_response(intf
, ent
->recv_msg
, IPMI_ERR_UNSPECIFIED
);
3480 void ipmi_unregister_smi(struct ipmi_smi
*intf
)
3482 struct ipmi_smi_watcher
*w
;
3483 int intf_num
= intf
->intf_num
, index
;
3485 mutex_lock(&ipmi_interfaces_mutex
);
3486 intf
->intf_num
= -1;
3487 intf
->in_shutdown
= true;
3488 list_del_rcu(&intf
->link
);
3489 mutex_unlock(&ipmi_interfaces_mutex
);
3490 synchronize_srcu(&ipmi_interfaces_srcu
);
3492 /* At this point no users can be added to the interface. */
3495 * Call all the watcher interfaces to tell them that
3496 * an interface is going away.
3498 mutex_lock(&smi_watchers_mutex
);
3499 list_for_each_entry(w
, &smi_watchers
, link
)
3500 w
->smi_gone(intf_num
);
3501 mutex_unlock(&smi_watchers_mutex
);
3503 index
= srcu_read_lock(&intf
->users_srcu
);
3504 while (!list_empty(&intf
->users
)) {
3505 struct ipmi_user
*user
=
3506 container_of(list_next_rcu(&intf
->users
),
3507 struct ipmi_user
, link
);
3509 _ipmi_destroy_user(user
);
3511 srcu_read_unlock(&intf
->users_srcu
, index
);
3513 if (intf
->handlers
->shutdown
)
3514 intf
->handlers
->shutdown(intf
->send_info
);
3516 cleanup_smi_msgs(intf
);
3518 ipmi_bmc_unregister(intf
);
3520 cleanup_srcu_struct(&intf
->users_srcu
);
3521 kref_put(&intf
->refcount
, intf_free
);
3523 EXPORT_SYMBOL(ipmi_unregister_smi
);
3525 static int handle_ipmb_get_msg_rsp(struct ipmi_smi
*intf
,
3526 struct ipmi_smi_msg
*msg
)
3528 struct ipmi_ipmb_addr ipmb_addr
;
3529 struct ipmi_recv_msg
*recv_msg
;
3532 * This is 11, not 10, because the response must contain a
3535 if (msg
->rsp_size
< 11) {
3536 /* Message not big enough, just ignore it. */
3537 ipmi_inc_stat(intf
, invalid_ipmb_responses
);
3541 if (msg
->rsp
[2] != 0) {
3542 /* An error getting the response, just ignore it. */
3546 ipmb_addr
.addr_type
= IPMI_IPMB_ADDR_TYPE
;
3547 ipmb_addr
.slave_addr
= msg
->rsp
[6];
3548 ipmb_addr
.channel
= msg
->rsp
[3] & 0x0f;
3549 ipmb_addr
.lun
= msg
->rsp
[7] & 3;
3552 * It's a response from a remote entity. Look up the sequence
3553 * number and handle the response.
3555 if (intf_find_seq(intf
,
3559 (msg
->rsp
[4] >> 2) & (~1),
3560 (struct ipmi_addr
*) &ipmb_addr
,
3563 * We were unable to find the sequence number,
3564 * so just nuke the message.
3566 ipmi_inc_stat(intf
, unhandled_ipmb_responses
);
3570 memcpy(recv_msg
->msg_data
, &msg
->rsp
[9], msg
->rsp_size
- 9);
3572 * The other fields matched, so no need to set them, except
3573 * for netfn, which needs to be the response that was
3574 * returned, not the request value.
3576 recv_msg
->msg
.netfn
= msg
->rsp
[4] >> 2;
3577 recv_msg
->msg
.data
= recv_msg
->msg_data
;
3578 recv_msg
->msg
.data_len
= msg
->rsp_size
- 10;
3579 recv_msg
->recv_type
= IPMI_RESPONSE_RECV_TYPE
;
3580 if (deliver_response(intf
, recv_msg
))
3581 ipmi_inc_stat(intf
, unhandled_ipmb_responses
);
3583 ipmi_inc_stat(intf
, handled_ipmb_responses
);
3588 static int handle_ipmb_get_msg_cmd(struct ipmi_smi
*intf
,
3589 struct ipmi_smi_msg
*msg
)
3591 struct cmd_rcvr
*rcvr
;
3593 unsigned char netfn
;
3596 struct ipmi_user
*user
= NULL
;
3597 struct ipmi_ipmb_addr
*ipmb_addr
;
3598 struct ipmi_recv_msg
*recv_msg
;
3600 if (msg
->rsp_size
< 10) {
3601 /* Message not big enough, just ignore it. */
3602 ipmi_inc_stat(intf
, invalid_commands
);
3606 if (msg
->rsp
[2] != 0) {
3607 /* An error getting the response, just ignore it. */
3611 netfn
= msg
->rsp
[4] >> 2;
3613 chan
= msg
->rsp
[3] & 0xf;
3616 rcvr
= find_cmd_rcvr(intf
, netfn
, cmd
, chan
);
3619 kref_get(&user
->refcount
);
3625 /* We didn't find a user, deliver an error response. */
3626 ipmi_inc_stat(intf
, unhandled_commands
);
3628 msg
->data
[0] = (IPMI_NETFN_APP_REQUEST
<< 2);
3629 msg
->data
[1] = IPMI_SEND_MSG_CMD
;
3630 msg
->data
[2] = msg
->rsp
[3];
3631 msg
->data
[3] = msg
->rsp
[6];
3632 msg
->data
[4] = ((netfn
+ 1) << 2) | (msg
->rsp
[7] & 0x3);
3633 msg
->data
[5] = ipmb_checksum(&msg
->data
[3], 2);
3634 msg
->data
[6] = intf
->addrinfo
[msg
->rsp
[3] & 0xf].address
;
3636 msg
->data
[7] = (msg
->rsp
[7] & 0xfc) | (msg
->rsp
[4] & 0x3);
3637 msg
->data
[8] = msg
->rsp
[8]; /* cmd */
3638 msg
->data
[9] = IPMI_INVALID_CMD_COMPLETION_CODE
;
3639 msg
->data
[10] = ipmb_checksum(&msg
->data
[6], 4);
3640 msg
->data_size
= 11;
3642 ipmi_debug_msg("Invalid command:", msg
->data
, msg
->data_size
);
3645 if (!intf
->in_shutdown
) {
3646 smi_send(intf
, intf
->handlers
, msg
, 0);
3648 * We used the message, so return the value
3649 * that causes it to not be freed or
3656 recv_msg
= ipmi_alloc_recv_msg();
3659 * We couldn't allocate memory for the
3660 * message, so requeue it for handling
3664 kref_put(&user
->refcount
, free_user
);
3666 /* Extract the source address from the data. */
3667 ipmb_addr
= (struct ipmi_ipmb_addr
*) &recv_msg
->addr
;
3668 ipmb_addr
->addr_type
= IPMI_IPMB_ADDR_TYPE
;
3669 ipmb_addr
->slave_addr
= msg
->rsp
[6];
3670 ipmb_addr
->lun
= msg
->rsp
[7] & 3;
3671 ipmb_addr
->channel
= msg
->rsp
[3] & 0xf;
3674 * Extract the rest of the message information
3675 * from the IPMB header.
3677 recv_msg
->user
= user
;
3678 recv_msg
->recv_type
= IPMI_CMD_RECV_TYPE
;
3679 recv_msg
->msgid
= msg
->rsp
[7] >> 2;
3680 recv_msg
->msg
.netfn
= msg
->rsp
[4] >> 2;
3681 recv_msg
->msg
.cmd
= msg
->rsp
[8];
3682 recv_msg
->msg
.data
= recv_msg
->msg_data
;
3685 * We chop off 10, not 9 bytes because the checksum
3686 * at the end also needs to be removed.
3688 recv_msg
->msg
.data_len
= msg
->rsp_size
- 10;
3689 memcpy(recv_msg
->msg_data
, &msg
->rsp
[9],
3690 msg
->rsp_size
- 10);
3691 if (deliver_response(intf
, recv_msg
))
3692 ipmi_inc_stat(intf
, unhandled_commands
);
3694 ipmi_inc_stat(intf
, handled_commands
);
3701 static int handle_lan_get_msg_rsp(struct ipmi_smi
*intf
,
3702 struct ipmi_smi_msg
*msg
)
3704 struct ipmi_lan_addr lan_addr
;
3705 struct ipmi_recv_msg
*recv_msg
;
3709 * This is 13, not 12, because the response must contain a
3712 if (msg
->rsp_size
< 13) {
3713 /* Message not big enough, just ignore it. */
3714 ipmi_inc_stat(intf
, invalid_lan_responses
);
3718 if (msg
->rsp
[2] != 0) {
3719 /* An error getting the response, just ignore it. */
3723 lan_addr
.addr_type
= IPMI_LAN_ADDR_TYPE
;
3724 lan_addr
.session_handle
= msg
->rsp
[4];
3725 lan_addr
.remote_SWID
= msg
->rsp
[8];
3726 lan_addr
.local_SWID
= msg
->rsp
[5];
3727 lan_addr
.channel
= msg
->rsp
[3] & 0x0f;
3728 lan_addr
.privilege
= msg
->rsp
[3] >> 4;
3729 lan_addr
.lun
= msg
->rsp
[9] & 3;
3732 * It's a response from a remote entity. Look up the sequence
3733 * number and handle the response.
3735 if (intf_find_seq(intf
,
3739 (msg
->rsp
[6] >> 2) & (~1),
3740 (struct ipmi_addr
*) &lan_addr
,
3743 * We were unable to find the sequence number,
3744 * so just nuke the message.
3746 ipmi_inc_stat(intf
, unhandled_lan_responses
);
3750 memcpy(recv_msg
->msg_data
, &msg
->rsp
[11], msg
->rsp_size
- 11);
3752 * The other fields matched, so no need to set them, except
3753 * for netfn, which needs to be the response that was
3754 * returned, not the request value.
3756 recv_msg
->msg
.netfn
= msg
->rsp
[6] >> 2;
3757 recv_msg
->msg
.data
= recv_msg
->msg_data
;
3758 recv_msg
->msg
.data_len
= msg
->rsp_size
- 12;
3759 recv_msg
->recv_type
= IPMI_RESPONSE_RECV_TYPE
;
3760 if (deliver_response(intf
, recv_msg
))
3761 ipmi_inc_stat(intf
, unhandled_lan_responses
);
3763 ipmi_inc_stat(intf
, handled_lan_responses
);
3768 static int handle_lan_get_msg_cmd(struct ipmi_smi
*intf
,
3769 struct ipmi_smi_msg
*msg
)
3771 struct cmd_rcvr
*rcvr
;
3773 unsigned char netfn
;
3776 struct ipmi_user
*user
= NULL
;
3777 struct ipmi_lan_addr
*lan_addr
;
3778 struct ipmi_recv_msg
*recv_msg
;
3780 if (msg
->rsp_size
< 12) {
3781 /* Message not big enough, just ignore it. */
3782 ipmi_inc_stat(intf
, invalid_commands
);
3786 if (msg
->rsp
[2] != 0) {
3787 /* An error getting the response, just ignore it. */
3791 netfn
= msg
->rsp
[6] >> 2;
3793 chan
= msg
->rsp
[3] & 0xf;
3796 rcvr
= find_cmd_rcvr(intf
, netfn
, cmd
, chan
);
3799 kref_get(&user
->refcount
);
3805 /* We didn't find a user, just give up. */
3806 ipmi_inc_stat(intf
, unhandled_commands
);
3809 * Don't do anything with these messages, just allow
3814 recv_msg
= ipmi_alloc_recv_msg();
3817 * We couldn't allocate memory for the
3818 * message, so requeue it for handling later.
3821 kref_put(&user
->refcount
, free_user
);
3823 /* Extract the source address from the data. */
3824 lan_addr
= (struct ipmi_lan_addr
*) &recv_msg
->addr
;
3825 lan_addr
->addr_type
= IPMI_LAN_ADDR_TYPE
;
3826 lan_addr
->session_handle
= msg
->rsp
[4];
3827 lan_addr
->remote_SWID
= msg
->rsp
[8];
3828 lan_addr
->local_SWID
= msg
->rsp
[5];
3829 lan_addr
->lun
= msg
->rsp
[9] & 3;
3830 lan_addr
->channel
= msg
->rsp
[3] & 0xf;
3831 lan_addr
->privilege
= msg
->rsp
[3] >> 4;
3834 * Extract the rest of the message information
3835 * from the IPMB header.
3837 recv_msg
->user
= user
;
3838 recv_msg
->recv_type
= IPMI_CMD_RECV_TYPE
;
3839 recv_msg
->msgid
= msg
->rsp
[9] >> 2;
3840 recv_msg
->msg
.netfn
= msg
->rsp
[6] >> 2;
3841 recv_msg
->msg
.cmd
= msg
->rsp
[10];
3842 recv_msg
->msg
.data
= recv_msg
->msg_data
;
3845 * We chop off 12, not 11 bytes because the checksum
3846 * at the end also needs to be removed.
3848 recv_msg
->msg
.data_len
= msg
->rsp_size
- 12;
3849 memcpy(recv_msg
->msg_data
, &msg
->rsp
[11],
3850 msg
->rsp_size
- 12);
3851 if (deliver_response(intf
, recv_msg
))
3852 ipmi_inc_stat(intf
, unhandled_commands
);
3854 ipmi_inc_stat(intf
, handled_commands
);
3862 * This routine will handle "Get Message" command responses with
3863 * channels that use an OEM Medium. The message format belongs to
3864 * the OEM. See IPMI 2.0 specification, Chapter 6 and
3865 * Chapter 22, sections 22.6 and 22.24 for more details.
3867 static int handle_oem_get_msg_cmd(struct ipmi_smi
*intf
,
3868 struct ipmi_smi_msg
*msg
)
3870 struct cmd_rcvr
*rcvr
;
3872 unsigned char netfn
;
3875 struct ipmi_user
*user
= NULL
;
3876 struct ipmi_system_interface_addr
*smi_addr
;
3877 struct ipmi_recv_msg
*recv_msg
;
3880 * We expect the OEM SW to perform error checking
3881 * so we just do some basic sanity checks
3883 if (msg
->rsp_size
< 4) {
3884 /* Message not big enough, just ignore it. */
3885 ipmi_inc_stat(intf
, invalid_commands
);
3889 if (msg
->rsp
[2] != 0) {
3890 /* An error getting the response, just ignore it. */
3895 * This is an OEM Message so the OEM needs to know how
3896 * handle the message. We do no interpretation.
3898 netfn
= msg
->rsp
[0] >> 2;
3900 chan
= msg
->rsp
[3] & 0xf;
3903 rcvr
= find_cmd_rcvr(intf
, netfn
, cmd
, chan
);
3906 kref_get(&user
->refcount
);
3912 /* We didn't find a user, just give up. */
3913 ipmi_inc_stat(intf
, unhandled_commands
);
3916 * Don't do anything with these messages, just allow
3922 recv_msg
= ipmi_alloc_recv_msg();
3925 * We couldn't allocate memory for the
3926 * message, so requeue it for handling
3930 kref_put(&user
->refcount
, free_user
);
3933 * OEM Messages are expected to be delivered via
3934 * the system interface to SMS software. We might
3935 * need to visit this again depending on OEM
3938 smi_addr
= ((struct ipmi_system_interface_addr
*)
3940 smi_addr
->addr_type
= IPMI_SYSTEM_INTERFACE_ADDR_TYPE
;
3941 smi_addr
->channel
= IPMI_BMC_CHANNEL
;
3942 smi_addr
->lun
= msg
->rsp
[0] & 3;
3944 recv_msg
->user
= user
;
3945 recv_msg
->user_msg_data
= NULL
;
3946 recv_msg
->recv_type
= IPMI_OEM_RECV_TYPE
;
3947 recv_msg
->msg
.netfn
= msg
->rsp
[0] >> 2;
3948 recv_msg
->msg
.cmd
= msg
->rsp
[1];
3949 recv_msg
->msg
.data
= recv_msg
->msg_data
;
3952 * The message starts at byte 4 which follows the
3953 * the Channel Byte in the "GET MESSAGE" command
3955 recv_msg
->msg
.data_len
= msg
->rsp_size
- 4;
3956 memcpy(recv_msg
->msg_data
, &msg
->rsp
[4],
3958 if (deliver_response(intf
, recv_msg
))
3959 ipmi_inc_stat(intf
, unhandled_commands
);
3961 ipmi_inc_stat(intf
, handled_commands
);
3968 static void copy_event_into_recv_msg(struct ipmi_recv_msg
*recv_msg
,
3969 struct ipmi_smi_msg
*msg
)
3971 struct ipmi_system_interface_addr
*smi_addr
;
3973 recv_msg
->msgid
= 0;
3974 smi_addr
= (struct ipmi_system_interface_addr
*) &recv_msg
->addr
;
3975 smi_addr
->addr_type
= IPMI_SYSTEM_INTERFACE_ADDR_TYPE
;
3976 smi_addr
->channel
= IPMI_BMC_CHANNEL
;
3977 smi_addr
->lun
= msg
->rsp
[0] & 3;
3978 recv_msg
->recv_type
= IPMI_ASYNC_EVENT_RECV_TYPE
;
3979 recv_msg
->msg
.netfn
= msg
->rsp
[0] >> 2;
3980 recv_msg
->msg
.cmd
= msg
->rsp
[1];
3981 memcpy(recv_msg
->msg_data
, &msg
->rsp
[3], msg
->rsp_size
- 3);
3982 recv_msg
->msg
.data
= recv_msg
->msg_data
;
3983 recv_msg
->msg
.data_len
= msg
->rsp_size
- 3;
3986 static int handle_read_event_rsp(struct ipmi_smi
*intf
,
3987 struct ipmi_smi_msg
*msg
)
3989 struct ipmi_recv_msg
*recv_msg
, *recv_msg2
;
3990 struct list_head msgs
;
3991 struct ipmi_user
*user
;
3992 int rv
= 0, deliver_count
= 0, index
;
3993 unsigned long flags
;
3995 if (msg
->rsp_size
< 19) {
3996 /* Message is too small to be an IPMB event. */
3997 ipmi_inc_stat(intf
, invalid_events
);
4001 if (msg
->rsp
[2] != 0) {
4002 /* An error getting the event, just ignore it. */
4006 INIT_LIST_HEAD(&msgs
);
4008 spin_lock_irqsave(&intf
->events_lock
, flags
);
4010 ipmi_inc_stat(intf
, events
);
4013 * Allocate and fill in one message for every user that is
4016 index
= srcu_read_lock(&intf
->users_srcu
);
4017 list_for_each_entry_rcu(user
, &intf
->users
, link
) {
4018 if (!user
->gets_events
)
4021 recv_msg
= ipmi_alloc_recv_msg();
4024 list_for_each_entry_safe(recv_msg
, recv_msg2
, &msgs
,
4026 list_del(&recv_msg
->link
);
4027 ipmi_free_recv_msg(recv_msg
);
4030 * We couldn't allocate memory for the
4031 * message, so requeue it for handling
4040 copy_event_into_recv_msg(recv_msg
, msg
);
4041 recv_msg
->user
= user
;
4042 kref_get(&user
->refcount
);
4043 list_add_tail(&recv_msg
->link
, &msgs
);
4045 srcu_read_unlock(&intf
->users_srcu
, index
);
4047 if (deliver_count
) {
4048 /* Now deliver all the messages. */
4049 list_for_each_entry_safe(recv_msg
, recv_msg2
, &msgs
, link
) {
4050 list_del(&recv_msg
->link
);
4051 deliver_local_response(intf
, recv_msg
);
4053 } else if (intf
->waiting_events_count
< MAX_EVENTS_IN_QUEUE
) {
4055 * No one to receive the message, put it in queue if there's
4056 * not already too many things in the queue.
4058 recv_msg
= ipmi_alloc_recv_msg();
4061 * We couldn't allocate memory for the
4062 * message, so requeue it for handling
4069 copy_event_into_recv_msg(recv_msg
, msg
);
4070 list_add_tail(&recv_msg
->link
, &intf
->waiting_events
);
4071 intf
->waiting_events_count
++;
4072 } else if (!intf
->event_msg_printed
) {
4074 * There's too many things in the queue, discard this
4077 dev_warn(intf
->si_dev
,
4078 PFX
"Event queue full, discarding incoming events\n");
4079 intf
->event_msg_printed
= 1;
4083 spin_unlock_irqrestore(&intf
->events_lock
, flags
);
4088 static int handle_bmc_rsp(struct ipmi_smi
*intf
,
4089 struct ipmi_smi_msg
*msg
)
4091 struct ipmi_recv_msg
*recv_msg
;
4092 struct ipmi_system_interface_addr
*smi_addr
;
4094 recv_msg
= (struct ipmi_recv_msg
*) msg
->user_data
;
4095 if (recv_msg
== NULL
) {
4096 dev_warn(intf
->si_dev
,
4097 "IPMI message received with no owner. This could be because of a malformed message, or because of a hardware error. Contact your hardware vender for assistance\n");
4101 recv_msg
->recv_type
= IPMI_RESPONSE_RECV_TYPE
;
4102 recv_msg
->msgid
= msg
->msgid
;
4103 smi_addr
= ((struct ipmi_system_interface_addr
*)
4105 smi_addr
->addr_type
= IPMI_SYSTEM_INTERFACE_ADDR_TYPE
;
4106 smi_addr
->channel
= IPMI_BMC_CHANNEL
;
4107 smi_addr
->lun
= msg
->rsp
[0] & 3;
4108 recv_msg
->msg
.netfn
= msg
->rsp
[0] >> 2;
4109 recv_msg
->msg
.cmd
= msg
->rsp
[1];
4110 memcpy(recv_msg
->msg_data
, &msg
->rsp
[2], msg
->rsp_size
- 2);
4111 recv_msg
->msg
.data
= recv_msg
->msg_data
;
4112 recv_msg
->msg
.data_len
= msg
->rsp_size
- 2;
4113 deliver_local_response(intf
, recv_msg
);
4119 * Handle a received message. Return 1 if the message should be requeued,
4120 * 0 if the message should be freed, or -1 if the message should not
4121 * be freed or requeued.
4123 static int handle_one_recv_msg(struct ipmi_smi
*intf
,
4124 struct ipmi_smi_msg
*msg
)
4129 ipmi_debug_msg("Recv:", msg
->rsp
, msg
->rsp_size
);
4130 if (msg
->rsp_size
< 2) {
4131 /* Message is too small to be correct. */
4132 dev_warn(intf
->si_dev
,
4133 PFX
"BMC returned to small a message for netfn %x cmd %x, got %d bytes\n",
4134 (msg
->data
[0] >> 2) | 1, msg
->data
[1], msg
->rsp_size
);
4136 /* Generate an error response for the message. */
4137 msg
->rsp
[0] = msg
->data
[0] | (1 << 2);
4138 msg
->rsp
[1] = msg
->data
[1];
4139 msg
->rsp
[2] = IPMI_ERR_UNSPECIFIED
;
4141 } else if (((msg
->rsp
[0] >> 2) != ((msg
->data
[0] >> 2) | 1))
4142 || (msg
->rsp
[1] != msg
->data
[1])) {
4144 * The NetFN and Command in the response is not even
4145 * marginally correct.
4147 dev_warn(intf
->si_dev
,
4148 PFX
"BMC returned incorrect response, expected netfn %x cmd %x, got netfn %x cmd %x\n",
4149 (msg
->data
[0] >> 2) | 1, msg
->data
[1],
4150 msg
->rsp
[0] >> 2, msg
->rsp
[1]);
4152 /* Generate an error response for the message. */
4153 msg
->rsp
[0] = msg
->data
[0] | (1 << 2);
4154 msg
->rsp
[1] = msg
->data
[1];
4155 msg
->rsp
[2] = IPMI_ERR_UNSPECIFIED
;
4159 if ((msg
->rsp
[0] == ((IPMI_NETFN_APP_REQUEST
|1) << 2))
4160 && (msg
->rsp
[1] == IPMI_SEND_MSG_CMD
)
4161 && (msg
->user_data
!= NULL
)) {
4163 * It's a response to a response we sent. For this we
4164 * deliver a send message response to the user.
4166 struct ipmi_recv_msg
*recv_msg
= msg
->user_data
;
4169 if (msg
->rsp_size
< 2)
4170 /* Message is too small to be correct. */
4173 chan
= msg
->data
[2] & 0x0f;
4174 if (chan
>= IPMI_MAX_CHANNELS
)
4175 /* Invalid channel number */
4181 recv_msg
->recv_type
= IPMI_RESPONSE_RESPONSE_TYPE
;
4182 recv_msg
->msg
.data
= recv_msg
->msg_data
;
4183 recv_msg
->msg
.data_len
= 1;
4184 recv_msg
->msg_data
[0] = msg
->rsp
[2];
4185 deliver_local_response(intf
, recv_msg
);
4186 } else if ((msg
->rsp
[0] == ((IPMI_NETFN_APP_REQUEST
|1) << 2))
4187 && (msg
->rsp
[1] == IPMI_GET_MSG_CMD
)) {
4188 struct ipmi_channel
*chans
;
4190 /* It's from the receive queue. */
4191 chan
= msg
->rsp
[3] & 0xf;
4192 if (chan
>= IPMI_MAX_CHANNELS
) {
4193 /* Invalid channel number */
4199 * We need to make sure the channels have been initialized.
4200 * The channel_handler routine will set the "curr_channel"
4201 * equal to or greater than IPMI_MAX_CHANNELS when all the
4202 * channels for this interface have been initialized.
4204 if (!intf
->channels_ready
) {
4205 requeue
= 0; /* Throw the message away */
4209 chans
= READ_ONCE(intf
->channel_list
)->c
;
4211 switch (chans
[chan
].medium
) {
4212 case IPMI_CHANNEL_MEDIUM_IPMB
:
4213 if (msg
->rsp
[4] & 0x04) {
4215 * It's a response, so find the
4216 * requesting message and send it up.
4218 requeue
= handle_ipmb_get_msg_rsp(intf
, msg
);
4221 * It's a command to the SMS from some other
4222 * entity. Handle that.
4224 requeue
= handle_ipmb_get_msg_cmd(intf
, msg
);
4228 case IPMI_CHANNEL_MEDIUM_8023LAN
:
4229 case IPMI_CHANNEL_MEDIUM_ASYNC
:
4230 if (msg
->rsp
[6] & 0x04) {
4232 * It's a response, so find the
4233 * requesting message and send it up.
4235 requeue
= handle_lan_get_msg_rsp(intf
, msg
);
4238 * It's a command to the SMS from some other
4239 * entity. Handle that.
4241 requeue
= handle_lan_get_msg_cmd(intf
, msg
);
4246 /* Check for OEM Channels. Clients had better
4247 register for these commands. */
4248 if ((chans
[chan
].medium
>= IPMI_CHANNEL_MEDIUM_OEM_MIN
)
4249 && (chans
[chan
].medium
4250 <= IPMI_CHANNEL_MEDIUM_OEM_MAX
)) {
4251 requeue
= handle_oem_get_msg_cmd(intf
, msg
);
4254 * We don't handle the channel type, so just
4261 } else if ((msg
->rsp
[0] == ((IPMI_NETFN_APP_REQUEST
|1) << 2))
4262 && (msg
->rsp
[1] == IPMI_READ_EVENT_MSG_BUFFER_CMD
)) {
4263 /* It's an asynchronous event. */
4264 requeue
= handle_read_event_rsp(intf
, msg
);
4266 /* It's a response from the local BMC. */
4267 requeue
= handle_bmc_rsp(intf
, msg
);
4275 * If there are messages in the queue or pretimeouts, handle them.
4277 static void handle_new_recv_msgs(struct ipmi_smi
*intf
)
4279 struct ipmi_smi_msg
*smi_msg
;
4280 unsigned long flags
= 0;
4282 int run_to_completion
= intf
->run_to_completion
;
4284 /* See if any waiting messages need to be processed. */
4285 if (!run_to_completion
)
4286 spin_lock_irqsave(&intf
->waiting_rcv_msgs_lock
, flags
);
4287 while (!list_empty(&intf
->waiting_rcv_msgs
)) {
4288 smi_msg
= list_entry(intf
->waiting_rcv_msgs
.next
,
4289 struct ipmi_smi_msg
, link
);
4290 list_del(&smi_msg
->link
);
4291 if (!run_to_completion
)
4292 spin_unlock_irqrestore(&intf
->waiting_rcv_msgs_lock
,
4294 rv
= handle_one_recv_msg(intf
, smi_msg
);
4295 if (!run_to_completion
)
4296 spin_lock_irqsave(&intf
->waiting_rcv_msgs_lock
, flags
);
4299 * To preserve message order, quit if we
4300 * can't handle a message. Add the message
4301 * back at the head, this is safe because this
4302 * tasklet is the only thing that pulls the
4305 list_add(&smi_msg
->link
, &intf
->waiting_rcv_msgs
);
4309 /* Message handled */
4310 ipmi_free_smi_msg(smi_msg
);
4311 /* If rv < 0, fatal error, del but don't free. */
4314 if (!run_to_completion
)
4315 spin_unlock_irqrestore(&intf
->waiting_rcv_msgs_lock
, flags
);
4318 * If the pretimout count is non-zero, decrement one from it and
4319 * deliver pretimeouts to all the users.
4321 if (atomic_add_unless(&intf
->watchdog_pretimeouts_to_deliver
, -1, 0)) {
4322 struct ipmi_user
*user
;
4325 index
= srcu_read_lock(&intf
->users_srcu
);
4326 list_for_each_entry_rcu(user
, &intf
->users
, link
) {
4327 if (user
->handler
->ipmi_watchdog_pretimeout
)
4328 user
->handler
->ipmi_watchdog_pretimeout(
4329 user
->handler_data
);
4331 srcu_read_unlock(&intf
->users_srcu
, index
);
4335 static void smi_recv_tasklet(unsigned long val
)
4337 unsigned long flags
= 0; /* keep us warning-free. */
4338 struct ipmi_smi
*intf
= (struct ipmi_smi
*) val
;
4339 int run_to_completion
= intf
->run_to_completion
;
4340 struct ipmi_smi_msg
*newmsg
= NULL
;
4343 * Start the next message if available.
4345 * Do this here, not in the actual receiver, because we may deadlock
4346 * because the lower layer is allowed to hold locks while calling
4352 if (!run_to_completion
)
4353 spin_lock_irqsave(&intf
->xmit_msgs_lock
, flags
);
4354 if (intf
->curr_msg
== NULL
&& !intf
->in_shutdown
) {
4355 struct list_head
*entry
= NULL
;
4357 /* Pick the high priority queue first. */
4358 if (!list_empty(&intf
->hp_xmit_msgs
))
4359 entry
= intf
->hp_xmit_msgs
.next
;
4360 else if (!list_empty(&intf
->xmit_msgs
))
4361 entry
= intf
->xmit_msgs
.next
;
4365 newmsg
= list_entry(entry
, struct ipmi_smi_msg
, link
);
4366 intf
->curr_msg
= newmsg
;
4369 if (!run_to_completion
)
4370 spin_unlock_irqrestore(&intf
->xmit_msgs_lock
, flags
);
4372 intf
->handlers
->sender(intf
->send_info
, newmsg
);
4376 handle_new_recv_msgs(intf
);
4379 /* Handle a new message from the lower layer. */
4380 void ipmi_smi_msg_received(struct ipmi_smi
*intf
,
4381 struct ipmi_smi_msg
*msg
)
4383 unsigned long flags
= 0; /* keep us warning-free. */
4384 int run_to_completion
= intf
->run_to_completion
;
4386 if ((msg
->data_size
>= 2)
4387 && (msg
->data
[0] == (IPMI_NETFN_APP_REQUEST
<< 2))
4388 && (msg
->data
[1] == IPMI_SEND_MSG_CMD
)
4389 && (msg
->user_data
== NULL
)) {
4391 if (intf
->in_shutdown
)
4395 * This is the local response to a command send, start
4396 * the timer for these. The user_data will not be
4397 * NULL if this is a response send, and we will let
4398 * response sends just go through.
4402 * Check for errors, if we get certain errors (ones
4403 * that mean basically we can try again later), we
4404 * ignore them and start the timer. Otherwise we
4405 * report the error immediately.
4407 if ((msg
->rsp_size
>= 3) && (msg
->rsp
[2] != 0)
4408 && (msg
->rsp
[2] != IPMI_NODE_BUSY_ERR
)
4409 && (msg
->rsp
[2] != IPMI_LOST_ARBITRATION_ERR
)
4410 && (msg
->rsp
[2] != IPMI_BUS_ERR
)
4411 && (msg
->rsp
[2] != IPMI_NAK_ON_WRITE_ERR
)) {
4412 int ch
= msg
->rsp
[3] & 0xf;
4413 struct ipmi_channel
*chans
;
4415 /* Got an error sending the message, handle it. */
4417 chans
= READ_ONCE(intf
->channel_list
)->c
;
4418 if ((chans
[ch
].medium
== IPMI_CHANNEL_MEDIUM_8023LAN
)
4419 || (chans
[ch
].medium
== IPMI_CHANNEL_MEDIUM_ASYNC
))
4420 ipmi_inc_stat(intf
, sent_lan_command_errs
);
4422 ipmi_inc_stat(intf
, sent_ipmb_command_errs
);
4423 intf_err_seq(intf
, msg
->msgid
, msg
->rsp
[2]);
4425 /* The message was sent, start the timer. */
4426 intf_start_seq_timer(intf
, msg
->msgid
);
4429 ipmi_free_smi_msg(msg
);
4432 * To preserve message order, we keep a queue and deliver from
4435 if (!run_to_completion
)
4436 spin_lock_irqsave(&intf
->waiting_rcv_msgs_lock
, flags
);
4437 list_add_tail(&msg
->link
, &intf
->waiting_rcv_msgs
);
4438 if (!run_to_completion
)
4439 spin_unlock_irqrestore(&intf
->waiting_rcv_msgs_lock
,
4443 if (!run_to_completion
)
4444 spin_lock_irqsave(&intf
->xmit_msgs_lock
, flags
);
4446 * We can get an asynchronous event or receive message in addition
4447 * to commands we send.
4449 if (msg
== intf
->curr_msg
)
4450 intf
->curr_msg
= NULL
;
4451 if (!run_to_completion
)
4452 spin_unlock_irqrestore(&intf
->xmit_msgs_lock
, flags
);
4454 if (run_to_completion
)
4455 smi_recv_tasklet((unsigned long) intf
);
4457 tasklet_schedule(&intf
->recv_tasklet
);
4459 EXPORT_SYMBOL(ipmi_smi_msg_received
);
4461 void ipmi_smi_watchdog_pretimeout(struct ipmi_smi
*intf
)
4463 if (intf
->in_shutdown
)
4466 atomic_set(&intf
->watchdog_pretimeouts_to_deliver
, 1);
4467 tasklet_schedule(&intf
->recv_tasklet
);
4469 EXPORT_SYMBOL(ipmi_smi_watchdog_pretimeout
);
4471 static struct ipmi_smi_msg
*
4472 smi_from_recv_msg(struct ipmi_smi
*intf
, struct ipmi_recv_msg
*recv_msg
,
4473 unsigned char seq
, long seqid
)
4475 struct ipmi_smi_msg
*smi_msg
= ipmi_alloc_smi_msg();
4478 * If we can't allocate the message, then just return, we
4479 * get 4 retries, so this should be ok.
4483 memcpy(smi_msg
->data
, recv_msg
->msg
.data
, recv_msg
->msg
.data_len
);
4484 smi_msg
->data_size
= recv_msg
->msg
.data_len
;
4485 smi_msg
->msgid
= STORE_SEQ_IN_MSGID(seq
, seqid
);
4487 ipmi_debug_msg("Resend: ", smi_msg
->data
, smi_msg
->data_size
);
4492 static void check_msg_timeout(struct ipmi_smi
*intf
, struct seq_table
*ent
,
4493 struct list_head
*timeouts
,
4494 unsigned long timeout_period
,
4495 int slot
, unsigned long *flags
,
4496 unsigned int *waiting_msgs
)
4498 struct ipmi_recv_msg
*msg
;
4500 if (intf
->in_shutdown
)
4506 if (timeout_period
< ent
->timeout
) {
4507 ent
->timeout
-= timeout_period
;
4512 if (ent
->retries_left
== 0) {
4513 /* The message has used all its retries. */
4515 msg
= ent
->recv_msg
;
4516 list_add_tail(&msg
->link
, timeouts
);
4518 ipmi_inc_stat(intf
, timed_out_ipmb_broadcasts
);
4519 else if (is_lan_addr(&ent
->recv_msg
->addr
))
4520 ipmi_inc_stat(intf
, timed_out_lan_commands
);
4522 ipmi_inc_stat(intf
, timed_out_ipmb_commands
);
4524 struct ipmi_smi_msg
*smi_msg
;
4525 /* More retries, send again. */
4530 * Start with the max timer, set to normal timer after
4531 * the message is sent.
4533 ent
->timeout
= MAX_MSG_TIMEOUT
;
4534 ent
->retries_left
--;
4535 smi_msg
= smi_from_recv_msg(intf
, ent
->recv_msg
, slot
,
4538 if (is_lan_addr(&ent
->recv_msg
->addr
))
4540 dropped_rexmit_lan_commands
);
4543 dropped_rexmit_ipmb_commands
);
4547 spin_unlock_irqrestore(&intf
->seq_lock
, *flags
);
4550 * Send the new message. We send with a zero
4551 * priority. It timed out, I doubt time is that
4552 * critical now, and high priority messages are really
4553 * only for messages to the local MC, which don't get
4556 if (intf
->handlers
) {
4557 if (is_lan_addr(&ent
->recv_msg
->addr
))
4559 retransmitted_lan_commands
);
4562 retransmitted_ipmb_commands
);
4564 smi_send(intf
, intf
->handlers
, smi_msg
, 0);
4566 ipmi_free_smi_msg(smi_msg
);
4568 spin_lock_irqsave(&intf
->seq_lock
, *flags
);
4572 static unsigned int ipmi_timeout_handler(struct ipmi_smi
*intf
,
4573 unsigned long timeout_period
)
4575 struct list_head timeouts
;
4576 struct ipmi_recv_msg
*msg
, *msg2
;
4577 unsigned long flags
;
4579 unsigned int waiting_msgs
= 0;
4581 if (!intf
->bmc_registered
) {
4582 kref_get(&intf
->refcount
);
4583 if (!schedule_work(&intf
->bmc_reg_work
)) {
4584 kref_put(&intf
->refcount
, intf_free
);
4590 * Go through the seq table and find any messages that
4591 * have timed out, putting them in the timeouts
4594 INIT_LIST_HEAD(&timeouts
);
4595 spin_lock_irqsave(&intf
->seq_lock
, flags
);
4596 if (intf
->ipmb_maintenance_mode_timeout
) {
4597 if (intf
->ipmb_maintenance_mode_timeout
<= timeout_period
)
4598 intf
->ipmb_maintenance_mode_timeout
= 0;
4600 intf
->ipmb_maintenance_mode_timeout
-= timeout_period
;
4602 for (i
= 0; i
< IPMI_IPMB_NUM_SEQ
; i
++)
4603 check_msg_timeout(intf
, &intf
->seq_table
[i
],
4604 &timeouts
, timeout_period
, i
,
4605 &flags
, &waiting_msgs
);
4606 spin_unlock_irqrestore(&intf
->seq_lock
, flags
);
4608 list_for_each_entry_safe(msg
, msg2
, &timeouts
, link
)
4609 deliver_err_response(intf
, msg
, IPMI_TIMEOUT_COMPLETION_CODE
);
4612 * Maintenance mode handling. Check the timeout
4613 * optimistically before we claim the lock. It may
4614 * mean a timeout gets missed occasionally, but that
4615 * only means the timeout gets extended by one period
4616 * in that case. No big deal, and it avoids the lock
4619 if (intf
->auto_maintenance_timeout
> 0) {
4620 spin_lock_irqsave(&intf
->maintenance_mode_lock
, flags
);
4621 if (intf
->auto_maintenance_timeout
> 0) {
4622 intf
->auto_maintenance_timeout
4624 if (!intf
->maintenance_mode
4625 && (intf
->auto_maintenance_timeout
<= 0)) {
4626 intf
->maintenance_mode_enable
= false;
4627 maintenance_mode_update(intf
);
4630 spin_unlock_irqrestore(&intf
->maintenance_mode_lock
,
4634 tasklet_schedule(&intf
->recv_tasklet
);
4636 return waiting_msgs
;
4639 static void ipmi_request_event(struct ipmi_smi
*intf
)
4641 /* No event requests when in maintenance mode. */
4642 if (intf
->maintenance_mode_enable
)
4645 if (!intf
->in_shutdown
)
4646 intf
->handlers
->request_events(intf
->send_info
);
4649 static struct timer_list ipmi_timer
;
4651 static atomic_t stop_operation
;
4653 static void ipmi_timeout(struct timer_list
*unused
)
4655 struct ipmi_smi
*intf
;
4658 if (atomic_read(&stop_operation
))
4661 index
= srcu_read_lock(&ipmi_interfaces_srcu
);
4662 list_for_each_entry_rcu(intf
, &ipmi_interfaces
, link
) {
4665 if (atomic_read(&intf
->event_waiters
)) {
4666 intf
->ticks_to_req_ev
--;
4667 if (intf
->ticks_to_req_ev
== 0) {
4668 ipmi_request_event(intf
);
4669 intf
->ticks_to_req_ev
= IPMI_REQUEST_EV_TIME
;
4674 lnt
+= ipmi_timeout_handler(intf
, IPMI_TIMEOUT_TIME
);
4677 if (lnt
!= intf
->last_needs_timer
&&
4678 intf
->handlers
->set_need_watch
)
4679 intf
->handlers
->set_need_watch(intf
->send_info
, lnt
);
4680 intf
->last_needs_timer
= lnt
;
4684 srcu_read_unlock(&ipmi_interfaces_srcu
, index
);
4687 mod_timer(&ipmi_timer
, jiffies
+ IPMI_TIMEOUT_JIFFIES
);
4690 static void need_waiter(struct ipmi_smi
*intf
)
4692 /* Racy, but worst case we start the timer twice. */
4693 if (!timer_pending(&ipmi_timer
))
4694 mod_timer(&ipmi_timer
, jiffies
+ IPMI_TIMEOUT_JIFFIES
);
4697 static atomic_t smi_msg_inuse_count
= ATOMIC_INIT(0);
4698 static atomic_t recv_msg_inuse_count
= ATOMIC_INIT(0);
4700 static void free_smi_msg(struct ipmi_smi_msg
*msg
)
4702 atomic_dec(&smi_msg_inuse_count
);
4706 struct ipmi_smi_msg
*ipmi_alloc_smi_msg(void)
4708 struct ipmi_smi_msg
*rv
;
4709 rv
= kmalloc(sizeof(struct ipmi_smi_msg
), GFP_ATOMIC
);
4711 rv
->done
= free_smi_msg
;
4712 rv
->user_data
= NULL
;
4713 atomic_inc(&smi_msg_inuse_count
);
4717 EXPORT_SYMBOL(ipmi_alloc_smi_msg
);
4719 static void free_recv_msg(struct ipmi_recv_msg
*msg
)
4721 atomic_dec(&recv_msg_inuse_count
);
4725 static struct ipmi_recv_msg
*ipmi_alloc_recv_msg(void)
4727 struct ipmi_recv_msg
*rv
;
4729 rv
= kmalloc(sizeof(struct ipmi_recv_msg
), GFP_ATOMIC
);
4732 rv
->done
= free_recv_msg
;
4733 atomic_inc(&recv_msg_inuse_count
);
4738 void ipmi_free_recv_msg(struct ipmi_recv_msg
*msg
)
4741 kref_put(&msg
->user
->refcount
, free_user
);
4744 EXPORT_SYMBOL(ipmi_free_recv_msg
);
4746 static atomic_t panic_done_count
= ATOMIC_INIT(0);
4748 static void dummy_smi_done_handler(struct ipmi_smi_msg
*msg
)
4750 atomic_dec(&panic_done_count
);
4753 static void dummy_recv_done_handler(struct ipmi_recv_msg
*msg
)
4755 atomic_dec(&panic_done_count
);
4759 * Inside a panic, send a message and wait for a response.
4761 static void ipmi_panic_request_and_wait(struct ipmi_smi
*intf
,
4762 struct ipmi_addr
*addr
,
4763 struct kernel_ipmi_msg
*msg
)
4765 struct ipmi_smi_msg smi_msg
;
4766 struct ipmi_recv_msg recv_msg
;
4769 smi_msg
.done
= dummy_smi_done_handler
;
4770 recv_msg
.done
= dummy_recv_done_handler
;
4771 atomic_add(2, &panic_done_count
);
4772 rv
= i_ipmi_request(NULL
,
4781 intf
->addrinfo
[0].address
,
4782 intf
->addrinfo
[0].lun
,
4783 0, 1); /* Don't retry, and don't wait. */
4785 atomic_sub(2, &panic_done_count
);
4786 else if (intf
->handlers
->flush_messages
)
4787 intf
->handlers
->flush_messages(intf
->send_info
);
4789 while (atomic_read(&panic_done_count
) != 0)
4793 static void event_receiver_fetcher(struct ipmi_smi
*intf
,
4794 struct ipmi_recv_msg
*msg
)
4796 if ((msg
->addr
.addr_type
== IPMI_SYSTEM_INTERFACE_ADDR_TYPE
)
4797 && (msg
->msg
.netfn
== IPMI_NETFN_SENSOR_EVENT_RESPONSE
)
4798 && (msg
->msg
.cmd
== IPMI_GET_EVENT_RECEIVER_CMD
)
4799 && (msg
->msg
.data
[0] == IPMI_CC_NO_ERROR
)) {
4800 /* A get event receiver command, save it. */
4801 intf
->event_receiver
= msg
->msg
.data
[1];
4802 intf
->event_receiver_lun
= msg
->msg
.data
[2] & 0x3;
4806 static void device_id_fetcher(struct ipmi_smi
*intf
, struct ipmi_recv_msg
*msg
)
4808 if ((msg
->addr
.addr_type
== IPMI_SYSTEM_INTERFACE_ADDR_TYPE
)
4809 && (msg
->msg
.netfn
== IPMI_NETFN_APP_RESPONSE
)
4810 && (msg
->msg
.cmd
== IPMI_GET_DEVICE_ID_CMD
)
4811 && (msg
->msg
.data
[0] == IPMI_CC_NO_ERROR
)) {
4813 * A get device id command, save if we are an event
4814 * receiver or generator.
4816 intf
->local_sel_device
= (msg
->msg
.data
[6] >> 2) & 1;
4817 intf
->local_event_generator
= (msg
->msg
.data
[6] >> 5) & 1;
4821 static void send_panic_events(struct ipmi_smi
*intf
, char *str
)
4823 struct kernel_ipmi_msg msg
;
4824 unsigned char data
[16];
4825 struct ipmi_system_interface_addr
*si
;
4826 struct ipmi_addr addr
;
4828 struct ipmi_ipmb_addr
*ipmb
;
4831 if (ipmi_send_panic_event
== IPMI_SEND_PANIC_EVENT_NONE
)
4834 si
= (struct ipmi_system_interface_addr
*) &addr
;
4835 si
->addr_type
= IPMI_SYSTEM_INTERFACE_ADDR_TYPE
;
4836 si
->channel
= IPMI_BMC_CHANNEL
;
4839 /* Fill in an event telling that we have failed. */
4840 msg
.netfn
= 0x04; /* Sensor or Event. */
4841 msg
.cmd
= 2; /* Platform event command. */
4844 data
[0] = 0x41; /* Kernel generator ID, IPMI table 5-4 */
4845 data
[1] = 0x03; /* This is for IPMI 1.0. */
4846 data
[2] = 0x20; /* OS Critical Stop, IPMI table 36-3 */
4847 data
[4] = 0x6f; /* Sensor specific, IPMI table 36-1 */
4848 data
[5] = 0xa1; /* Runtime stop OEM bytes 2 & 3. */
4851 * Put a few breadcrumbs in. Hopefully later we can add more things
4852 * to make the panic events more useful.
4860 /* Send the event announcing the panic. */
4861 ipmi_panic_request_and_wait(intf
, &addr
, &msg
);
4864 * On every interface, dump a bunch of OEM event holding the
4867 if (ipmi_send_panic_event
!= IPMI_SEND_PANIC_EVENT_STRING
|| !str
)
4871 * intf_num is used as an marker to tell if the
4872 * interface is valid. Thus we need a read barrier to
4873 * make sure data fetched before checking intf_num
4879 * First job here is to figure out where to send the
4880 * OEM events. There's no way in IPMI to send OEM
4881 * events using an event send command, so we have to
4882 * find the SEL to put them in and stick them in
4886 /* Get capabilities from the get device id. */
4887 intf
->local_sel_device
= 0;
4888 intf
->local_event_generator
= 0;
4889 intf
->event_receiver
= 0;
4891 /* Request the device info from the local MC. */
4892 msg
.netfn
= IPMI_NETFN_APP_REQUEST
;
4893 msg
.cmd
= IPMI_GET_DEVICE_ID_CMD
;
4896 intf
->null_user_handler
= device_id_fetcher
;
4897 ipmi_panic_request_and_wait(intf
, &addr
, &msg
);
4899 if (intf
->local_event_generator
) {
4900 /* Request the event receiver from the local MC. */
4901 msg
.netfn
= IPMI_NETFN_SENSOR_EVENT_REQUEST
;
4902 msg
.cmd
= IPMI_GET_EVENT_RECEIVER_CMD
;
4905 intf
->null_user_handler
= event_receiver_fetcher
;
4906 ipmi_panic_request_and_wait(intf
, &addr
, &msg
);
4908 intf
->null_user_handler
= NULL
;
4911 * Validate the event receiver. The low bit must not
4912 * be 1 (it must be a valid IPMB address), it cannot
4913 * be zero, and it must not be my address.
4915 if (((intf
->event_receiver
& 1) == 0)
4916 && (intf
->event_receiver
!= 0)
4917 && (intf
->event_receiver
!= intf
->addrinfo
[0].address
)) {
4919 * The event receiver is valid, send an IPMB
4922 ipmb
= (struct ipmi_ipmb_addr
*) &addr
;
4923 ipmb
->addr_type
= IPMI_IPMB_ADDR_TYPE
;
4924 ipmb
->channel
= 0; /* FIXME - is this right? */
4925 ipmb
->lun
= intf
->event_receiver_lun
;
4926 ipmb
->slave_addr
= intf
->event_receiver
;
4927 } else if (intf
->local_sel_device
) {
4929 * The event receiver was not valid (or was
4930 * me), but I am an SEL device, just dump it
4933 si
= (struct ipmi_system_interface_addr
*) &addr
;
4934 si
->addr_type
= IPMI_SYSTEM_INTERFACE_ADDR_TYPE
;
4935 si
->channel
= IPMI_BMC_CHANNEL
;
4938 return; /* No where to send the event. */
4940 msg
.netfn
= IPMI_NETFN_STORAGE_REQUEST
; /* Storage. */
4941 msg
.cmd
= IPMI_ADD_SEL_ENTRY_CMD
;
4947 int size
= strlen(p
);
4953 data
[2] = 0xf0; /* OEM event without timestamp. */
4954 data
[3] = intf
->addrinfo
[0].address
;
4955 data
[4] = j
++; /* sequence # */
4957 * Always give 11 bytes, so strncpy will fill
4958 * it with zeroes for me.
4960 strncpy(data
+5, p
, 11);
4963 ipmi_panic_request_and_wait(intf
, &addr
, &msg
);
4967 static int has_panicked
;
4969 static int panic_event(struct notifier_block
*this,
4970 unsigned long event
,
4973 struct ipmi_smi
*intf
;
4974 struct ipmi_user
*user
;
4980 /* For every registered interface, set it to run to completion. */
4981 list_for_each_entry_rcu(intf
, &ipmi_interfaces
, link
) {
4982 if (!intf
->handlers
|| intf
->intf_num
== -1)
4983 /* Interface is not ready. */
4986 if (!intf
->handlers
->poll
)
4990 * If we were interrupted while locking xmit_msgs_lock or
4991 * waiting_rcv_msgs_lock, the corresponding list may be
4992 * corrupted. In this case, drop items on the list for
4995 if (!spin_trylock(&intf
->xmit_msgs_lock
)) {
4996 INIT_LIST_HEAD(&intf
->xmit_msgs
);
4997 INIT_LIST_HEAD(&intf
->hp_xmit_msgs
);
4999 spin_unlock(&intf
->xmit_msgs_lock
);
5001 if (!spin_trylock(&intf
->waiting_rcv_msgs_lock
))
5002 INIT_LIST_HEAD(&intf
->waiting_rcv_msgs
);
5004 spin_unlock(&intf
->waiting_rcv_msgs_lock
);
5006 intf
->run_to_completion
= 1;
5007 if (intf
->handlers
->set_run_to_completion
)
5008 intf
->handlers
->set_run_to_completion(intf
->send_info
,
5011 list_for_each_entry_rcu(user
, &intf
->users
, link
) {
5012 if (user
->handler
->ipmi_panic_handler
)
5013 user
->handler
->ipmi_panic_handler(
5014 user
->handler_data
);
5017 send_panic_events(intf
, ptr
);
5023 static struct notifier_block panic_block
= {
5024 .notifier_call
= panic_event
,
5026 .priority
= 200 /* priority: INT_MAX >= x >= 0 */
5029 static int ipmi_init_msghandler(void)
5036 rv
= driver_register(&ipmidriver
.driver
);
5038 pr_err(PFX
"Could not register IPMI driver\n");
5042 pr_info("ipmi message handler version " IPMI_DRIVER_VERSION
"\n");
5044 timer_setup(&ipmi_timer
, ipmi_timeout
, 0);
5045 mod_timer(&ipmi_timer
, jiffies
+ IPMI_TIMEOUT_JIFFIES
);
5047 atomic_notifier_chain_register(&panic_notifier_list
, &panic_block
);
5054 static int __init
ipmi_init_msghandler_mod(void)
5056 ipmi_init_msghandler();
5060 static void __exit
cleanup_ipmi(void)
5067 atomic_notifier_chain_unregister(&panic_notifier_list
, &panic_block
);
5070 * This can't be called if any interfaces exist, so no worry
5071 * about shutting down the interfaces.
5075 * Tell the timer to stop, then wait for it to stop. This
5076 * avoids problems with race conditions removing the timer
5079 atomic_inc(&stop_operation
);
5080 del_timer_sync(&ipmi_timer
);
5082 driver_unregister(&ipmidriver
.driver
);
5086 /* Check for buffer leaks. */
5087 count
= atomic_read(&smi_msg_inuse_count
);
5089 pr_warn(PFX
"SMI message count %d at exit\n", count
);
5090 count
= atomic_read(&recv_msg_inuse_count
);
5092 pr_warn(PFX
"recv message count %d at exit\n", count
);
5094 module_exit(cleanup_ipmi
);
5096 module_init(ipmi_init_msghandler_mod
);
5097 MODULE_LICENSE("GPL");
5098 MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
5099 MODULE_DESCRIPTION("Incoming and outgoing message routing for an IPMI"
5101 MODULE_VERSION(IPMI_DRIVER_VERSION
);
5102 MODULE_SOFTDEP("post: ipmi_devintf");