xref: /openbmc/linux/drivers/char/ipmi/ipmi_msghandler.c (revision 55b7acbd15b15e75c6df468c72177a6b32e648cf)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * ipmi_msghandler.c
4  *
5  * Incoming and outgoing message routing for an IPMI interface.
6  *
7  * Author: MontaVista Software, Inc.
8  *         Corey Minyard <minyard@mvista.com>
9  *         source@mvista.com
10  *
11  * Copyright 2002 MontaVista Software Inc.
12  */
13 
14 #define pr_fmt(fmt) "IPMI message handler: " fmt
15 #define dev_fmt(fmt) pr_fmt(fmt)
16 
17 #include <linux/module.h>
18 #include <linux/errno.h>
19 #include <linux/panic_notifier.h>
20 #include <linux/poll.h>
21 #include <linux/sched.h>
22 #include <linux/seq_file.h>
23 #include <linux/spinlock.h>
24 #include <linux/mutex.h>
25 #include <linux/slab.h>
26 #include <linux/ipmi.h>
27 #include <linux/ipmi_smi.h>
28 #include <linux/notifier.h>
29 #include <linux/init.h>
30 #include <linux/proc_fs.h>
31 #include <linux/rcupdate.h>
32 #include <linux/interrupt.h>
33 #include <linux/moduleparam.h>
34 #include <linux/workqueue.h>
35 #include <linux/uuid.h>
36 #include <linux/nospec.h>
37 #include <linux/vmalloc.h>
38 #include <linux/delay.h>
39 
40 #define IPMI_DRIVER_VERSION "39.2"
41 
42 static struct ipmi_recv_msg *ipmi_alloc_recv_msg(void);
43 static int ipmi_init_msghandler(void);
44 static void smi_recv_tasklet(struct tasklet_struct *t);
45 static void handle_new_recv_msgs(struct ipmi_smi *intf);
46 static void need_waiter(struct ipmi_smi *intf);
47 static int handle_one_recv_msg(struct ipmi_smi *intf,
48 			       struct ipmi_smi_msg *msg);
49 
50 static bool initialized;
51 static bool drvregistered;
52 
53 /* Numbers in this enumerator should be mapped to ipmi_panic_event_str */
54 enum ipmi_panic_event_op {
55 	IPMI_SEND_PANIC_EVENT_NONE,
56 	IPMI_SEND_PANIC_EVENT,
57 	IPMI_SEND_PANIC_EVENT_STRING,
58 	IPMI_SEND_PANIC_EVENT_MAX
59 };
60 
61 /* Indices in this array should be mapped to enum ipmi_panic_event_op */
62 static const char *const ipmi_panic_event_str[] = { "none", "event", "string", NULL };
63 
64 #ifdef CONFIG_IPMI_PANIC_STRING
65 #define IPMI_PANIC_DEFAULT IPMI_SEND_PANIC_EVENT_STRING
66 #elif defined(CONFIG_IPMI_PANIC_EVENT)
67 #define IPMI_PANIC_DEFAULT IPMI_SEND_PANIC_EVENT
68 #else
69 #define IPMI_PANIC_DEFAULT IPMI_SEND_PANIC_EVENT_NONE
70 #endif
71 
72 static enum ipmi_panic_event_op ipmi_send_panic_event = IPMI_PANIC_DEFAULT;
73 
panic_op_write_handler(const char * val,const struct kernel_param * kp)74 static int panic_op_write_handler(const char *val,
75 				  const struct kernel_param *kp)
76 {
77 	char valcp[16];
78 	int e;
79 
80 	strscpy(valcp, val, sizeof(valcp));
81 	e = match_string(ipmi_panic_event_str, -1, strstrip(valcp));
82 	if (e < 0)
83 		return e;
84 
85 	ipmi_send_panic_event = e;
86 	return 0;
87 }
88 
panic_op_read_handler(char * buffer,const struct kernel_param * kp)89 static int panic_op_read_handler(char *buffer, const struct kernel_param *kp)
90 {
91 	const char *event_str;
92 
93 	if (ipmi_send_panic_event >= IPMI_SEND_PANIC_EVENT_MAX)
94 		event_str = "???";
95 	else
96 		event_str = ipmi_panic_event_str[ipmi_send_panic_event];
97 
98 	return sprintf(buffer, "%s\n", event_str);
99 }
100 
101 static const struct kernel_param_ops panic_op_ops = {
102 	.set = panic_op_write_handler,
103 	.get = panic_op_read_handler
104 };
105 module_param_cb(panic_op, &panic_op_ops, NULL, 0600);
106 MODULE_PARM_DESC(panic_op, "Sets if the IPMI driver will attempt to store panic information in the event log in the event of a panic.  Set to 'none' for no, 'event' for a single event, or 'string' for a generic event and the panic string in IPMI OEM events.");
107 
108 
109 #define MAX_EVENTS_IN_QUEUE	25
110 
111 /* Remain in auto-maintenance mode for this amount of time (in ms). */
112 static unsigned long maintenance_mode_timeout_ms = 30000;
113 module_param(maintenance_mode_timeout_ms, ulong, 0644);
114 MODULE_PARM_DESC(maintenance_mode_timeout_ms,
115 		 "The time (milliseconds) after the last maintenance message that the connection stays in maintenance mode.");
116 
117 /*
118  * Don't let a message sit in a queue forever, always time it with at lest
119  * the max message timer.  This is in milliseconds.
120  */
121 #define MAX_MSG_TIMEOUT		60000
122 
123 /*
124  * Timeout times below are in milliseconds, and are done off a 1
125  * second timer.  So setting the value to 1000 would mean anything
126  * between 0 and 1000ms.  So really the only reasonable minimum
127  * setting it 2000ms, which is between 1 and 2 seconds.
128  */
129 
130 /* The default timeout for message retries. */
131 static unsigned long default_retry_ms = 2000;
132 module_param(default_retry_ms, ulong, 0644);
133 MODULE_PARM_DESC(default_retry_ms,
134 		 "The time (milliseconds) between retry sends");
135 
136 /* The default timeout for maintenance mode message retries. */
137 static unsigned long default_maintenance_retry_ms = 3000;
138 module_param(default_maintenance_retry_ms, ulong, 0644);
139 MODULE_PARM_DESC(default_maintenance_retry_ms,
140 		 "The time (milliseconds) between retry sends in maintenance mode");
141 
142 /* The default maximum number of retries */
143 static unsigned int default_max_retries = 4;
144 module_param(default_max_retries, uint, 0644);
145 MODULE_PARM_DESC(default_max_retries,
146 		 "The time (milliseconds) between retry sends in maintenance mode");
147 
148 /* The default maximum number of users that may register. */
149 static unsigned int max_users = 30;
150 module_param(max_users, uint, 0644);
151 MODULE_PARM_DESC(max_users,
152 		 "The most users that may use the IPMI stack at one time.");
153 
154 /* The default maximum number of message a user may have outstanding. */
155 static unsigned int max_msgs_per_user = 100;
156 module_param(max_msgs_per_user, uint, 0644);
157 MODULE_PARM_DESC(max_msgs_per_user,
158 		 "The most message a user may have outstanding.");
159 
160 /* Call every ~1000 ms. */
161 #define IPMI_TIMEOUT_TIME	1000
162 
163 /* How many jiffies does it take to get to the timeout time. */
164 #define IPMI_TIMEOUT_JIFFIES	((IPMI_TIMEOUT_TIME * HZ) / 1000)
165 
166 /*
167  * Request events from the queue every second (this is the number of
168  * IPMI_TIMEOUT_TIMES between event requests).  Hopefully, in the
169  * future, IPMI will add a way to know immediately if an event is in
170  * the queue and this silliness can go away.
171  */
172 #define IPMI_REQUEST_EV_TIME	(1000 / (IPMI_TIMEOUT_TIME))
173 
174 /* How long should we cache dynamic device IDs? */
175 #define IPMI_DYN_DEV_ID_EXPIRY	(10 * HZ)
176 
177 /*
178  * The main "user" data structure.
179  */
180 struct ipmi_user {
181 	struct list_head link;
182 
183 	/*
184 	 * Set to NULL when the user is destroyed, a pointer to myself
185 	 * so srcu_dereference can be used on it.
186 	 */
187 	struct ipmi_user *self;
188 	struct srcu_struct release_barrier;
189 
190 	struct kref refcount;
191 
192 	/* The upper layer that handles receive messages. */
193 	const struct ipmi_user_hndl *handler;
194 	void             *handler_data;
195 
196 	/* The interface this user is bound to. */
197 	struct ipmi_smi *intf;
198 
199 	/* Does this interface receive IPMI events? */
200 	bool gets_events;
201 
202 	atomic_t nr_msgs;
203 
204 	/* Free must run in process context for RCU cleanup. */
205 	struct work_struct remove_work;
206 };
207 
208 static struct workqueue_struct *remove_work_wq;
209 
acquire_ipmi_user(struct ipmi_user * user,int * index)210 static struct ipmi_user *acquire_ipmi_user(struct ipmi_user *user, int *index)
211 	__acquires(user->release_barrier)
212 {
213 	struct ipmi_user *ruser;
214 
215 	*index = srcu_read_lock(&user->release_barrier);
216 	ruser = srcu_dereference(user->self, &user->release_barrier);
217 	if (!ruser)
218 		srcu_read_unlock(&user->release_barrier, *index);
219 	return ruser;
220 }
221 
release_ipmi_user(struct ipmi_user * user,int index)222 static void release_ipmi_user(struct ipmi_user *user, int index)
223 {
224 	srcu_read_unlock(&user->release_barrier, index);
225 }
226 
227 struct cmd_rcvr {
228 	struct list_head link;
229 
230 	struct ipmi_user *user;
231 	unsigned char netfn;
232 	unsigned char cmd;
233 	unsigned int  chans;
234 
235 	/*
236 	 * This is used to form a linked lised during mass deletion.
237 	 * Since this is in an RCU list, we cannot use the link above
238 	 * or change any data until the RCU period completes.  So we
239 	 * use this next variable during mass deletion so we can have
240 	 * a list and don't have to wait and restart the search on
241 	 * every individual deletion of a command.
242 	 */
243 	struct cmd_rcvr *next;
244 };
245 
246 struct seq_table {
247 	unsigned int         inuse : 1;
248 	unsigned int         broadcast : 1;
249 
250 	unsigned long        timeout;
251 	unsigned long        orig_timeout;
252 	unsigned int         retries_left;
253 
254 	/*
255 	 * To verify on an incoming send message response that this is
256 	 * the message that the response is for, we keep a sequence id
257 	 * and increment it every time we send a message.
258 	 */
259 	long                 seqid;
260 
261 	/*
262 	 * This is held so we can properly respond to the message on a
263 	 * timeout, and it is used to hold the temporary data for
264 	 * retransmission, too.
265 	 */
266 	struct ipmi_recv_msg *recv_msg;
267 };
268 
269 /*
270  * Store the information in a msgid (long) to allow us to find a
271  * sequence table entry from the msgid.
272  */
273 #define STORE_SEQ_IN_MSGID(seq, seqid) \
274 	((((seq) & 0x3f) << 26) | ((seqid) & 0x3ffffff))
275 
276 #define GET_SEQ_FROM_MSGID(msgid, seq, seqid) \
277 	do {								\
278 		seq = (((msgid) >> 26) & 0x3f);				\
279 		seqid = ((msgid) & 0x3ffffff);				\
280 	} while (0)
281 
282 #define NEXT_SEQID(seqid) (((seqid) + 1) & 0x3ffffff)
283 
284 #define IPMI_MAX_CHANNELS       16
285 struct ipmi_channel {
286 	unsigned char medium;
287 	unsigned char protocol;
288 };
289 
290 struct ipmi_channel_set {
291 	struct ipmi_channel c[IPMI_MAX_CHANNELS];
292 };
293 
294 struct ipmi_my_addrinfo {
295 	/*
296 	 * My slave address.  This is initialized to IPMI_BMC_SLAVE_ADDR,
297 	 * but may be changed by the user.
298 	 */
299 	unsigned char address;
300 
301 	/*
302 	 * My LUN.  This should generally stay the SMS LUN, but just in
303 	 * case...
304 	 */
305 	unsigned char lun;
306 };
307 
308 /*
309  * Note that the product id, manufacturer id, guid, and device id are
310  * immutable in this structure, so dyn_mutex is not required for
311  * accessing those.  If those change on a BMC, a new BMC is allocated.
312  */
313 struct bmc_device {
314 	struct platform_device pdev;
315 	struct list_head       intfs; /* Interfaces on this BMC. */
316 	struct ipmi_device_id  id;
317 	struct ipmi_device_id  fetch_id;
318 	int                    dyn_id_set;
319 	unsigned long          dyn_id_expiry;
320 	struct mutex           dyn_mutex; /* Protects id, intfs, & dyn* */
321 	guid_t                 guid;
322 	guid_t                 fetch_guid;
323 	int                    dyn_guid_set;
324 	struct kref	       usecount;
325 	struct work_struct     remove_work;
326 	unsigned char	       cc; /* completion code */
327 };
328 #define to_bmc_device(x) container_of((x), struct bmc_device, pdev.dev)
329 
330 static int bmc_get_device_id(struct ipmi_smi *intf, struct bmc_device *bmc,
331 			     struct ipmi_device_id *id,
332 			     bool *guid_set, guid_t *guid);
333 
334 /*
335  * Various statistics for IPMI, these index stats[] in the ipmi_smi
336  * structure.
337  */
338 enum ipmi_stat_indexes {
339 	/* Commands we got from the user that were invalid. */
340 	IPMI_STAT_sent_invalid_commands = 0,
341 
342 	/* Commands we sent to the MC. */
343 	IPMI_STAT_sent_local_commands,
344 
345 	/* Responses from the MC that were delivered to a user. */
346 	IPMI_STAT_handled_local_responses,
347 
348 	/* Responses from the MC that were not delivered to a user. */
349 	IPMI_STAT_unhandled_local_responses,
350 
351 	/* Commands we sent out to the IPMB bus. */
352 	IPMI_STAT_sent_ipmb_commands,
353 
354 	/* Commands sent on the IPMB that had errors on the SEND CMD */
355 	IPMI_STAT_sent_ipmb_command_errs,
356 
357 	/* Each retransmit increments this count. */
358 	IPMI_STAT_retransmitted_ipmb_commands,
359 
360 	/*
361 	 * When a message times out (runs out of retransmits) this is
362 	 * incremented.
363 	 */
364 	IPMI_STAT_timed_out_ipmb_commands,
365 
366 	/*
367 	 * This is like above, but for broadcasts.  Broadcasts are
368 	 * *not* included in the above count (they are expected to
369 	 * time out).
370 	 */
371 	IPMI_STAT_timed_out_ipmb_broadcasts,
372 
373 	/* Responses I have sent to the IPMB bus. */
374 	IPMI_STAT_sent_ipmb_responses,
375 
376 	/* The response was delivered to the user. */
377 	IPMI_STAT_handled_ipmb_responses,
378 
379 	/* The response had invalid data in it. */
380 	IPMI_STAT_invalid_ipmb_responses,
381 
382 	/* The response didn't have anyone waiting for it. */
383 	IPMI_STAT_unhandled_ipmb_responses,
384 
385 	/* Commands we sent out to the IPMB bus. */
386 	IPMI_STAT_sent_lan_commands,
387 
388 	/* Commands sent on the IPMB that had errors on the SEND CMD */
389 	IPMI_STAT_sent_lan_command_errs,
390 
391 	/* Each retransmit increments this count. */
392 	IPMI_STAT_retransmitted_lan_commands,
393 
394 	/*
395 	 * When a message times out (runs out of retransmits) this is
396 	 * incremented.
397 	 */
398 	IPMI_STAT_timed_out_lan_commands,
399 
400 	/* Responses I have sent to the IPMB bus. */
401 	IPMI_STAT_sent_lan_responses,
402 
403 	/* The response was delivered to the user. */
404 	IPMI_STAT_handled_lan_responses,
405 
406 	/* The response had invalid data in it. */
407 	IPMI_STAT_invalid_lan_responses,
408 
409 	/* The response didn't have anyone waiting for it. */
410 	IPMI_STAT_unhandled_lan_responses,
411 
412 	/* The command was delivered to the user. */
413 	IPMI_STAT_handled_commands,
414 
415 	/* The command had invalid data in it. */
416 	IPMI_STAT_invalid_commands,
417 
418 	/* The command didn't have anyone waiting for it. */
419 	IPMI_STAT_unhandled_commands,
420 
421 	/* Invalid data in an event. */
422 	IPMI_STAT_invalid_events,
423 
424 	/* Events that were received with the proper format. */
425 	IPMI_STAT_events,
426 
427 	/* Retransmissions on IPMB that failed. */
428 	IPMI_STAT_dropped_rexmit_ipmb_commands,
429 
430 	/* Retransmissions on LAN that failed. */
431 	IPMI_STAT_dropped_rexmit_lan_commands,
432 
433 	/* This *must* remain last, add new values above this. */
434 	IPMI_NUM_STATS
435 };
436 
437 
438 #define IPMI_IPMB_NUM_SEQ	64
439 struct ipmi_smi {
440 	struct module *owner;
441 
442 	/* What interface number are we? */
443 	int intf_num;
444 
445 	struct kref refcount;
446 
447 	/* Set when the interface is being unregistered. */
448 	bool in_shutdown;
449 
450 	/* Used for a list of interfaces. */
451 	struct list_head link;
452 
453 	/*
454 	 * The list of upper layers that are using me.  seq_lock write
455 	 * protects this.  Read protection is with srcu.
456 	 */
457 	struct list_head users;
458 	struct srcu_struct users_srcu;
459 	atomic_t nr_users;
460 	struct device_attribute nr_users_devattr;
461 	struct device_attribute nr_msgs_devattr;
462 
463 
464 	/* Used for wake ups at startup. */
465 	wait_queue_head_t waitq;
466 
467 	/*
468 	 * Prevents the interface from being unregistered when the
469 	 * interface is used by being looked up through the BMC
470 	 * structure.
471 	 */
472 	struct mutex bmc_reg_mutex;
473 
474 	struct bmc_device tmp_bmc;
475 	struct bmc_device *bmc;
476 	bool bmc_registered;
477 	struct list_head bmc_link;
478 	char *my_dev_name;
479 	bool in_bmc_register;  /* Handle recursive situations.  Yuck. */
480 	struct work_struct bmc_reg_work;
481 
482 	const struct ipmi_smi_handlers *handlers;
483 	void                     *send_info;
484 
485 	/* Driver-model device for the system interface. */
486 	struct device          *si_dev;
487 
488 	/*
489 	 * A table of sequence numbers for this interface.  We use the
490 	 * sequence numbers for IPMB messages that go out of the
491 	 * interface to match them up with their responses.  A routine
492 	 * is called periodically to time the items in this list.
493 	 */
494 	spinlock_t       seq_lock;
495 	struct seq_table seq_table[IPMI_IPMB_NUM_SEQ];
496 	int curr_seq;
497 
498 	/*
499 	 * Messages queued for delivery.  If delivery fails (out of memory
500 	 * for instance), They will stay in here to be processed later in a
501 	 * periodic timer interrupt.  The tasklet is for handling received
502 	 * messages directly from the handler.
503 	 */
504 	spinlock_t       waiting_rcv_msgs_lock;
505 	struct list_head waiting_rcv_msgs;
506 	atomic_t	 watchdog_pretimeouts_to_deliver;
507 	struct tasklet_struct recv_tasklet;
508 
509 	spinlock_t             xmit_msgs_lock;
510 	struct list_head       xmit_msgs;
511 	struct ipmi_smi_msg    *curr_msg;
512 	struct list_head       hp_xmit_msgs;
513 
514 	/*
515 	 * The list of command receivers that are registered for commands
516 	 * on this interface.
517 	 */
518 	struct mutex     cmd_rcvrs_mutex;
519 	struct list_head cmd_rcvrs;
520 
521 	/*
522 	 * Events that were queues because no one was there to receive
523 	 * them.
524 	 */
525 	spinlock_t       events_lock; /* For dealing with event stuff. */
526 	struct list_head waiting_events;
527 	unsigned int     waiting_events_count; /* How many events in queue? */
528 	char             delivering_events;
529 	char             event_msg_printed;
530 
531 	/* How many users are waiting for events? */
532 	atomic_t         event_waiters;
533 	unsigned int     ticks_to_req_ev;
534 
535 	spinlock_t       watch_lock; /* For dealing with watch stuff below. */
536 
537 	/* How many users are waiting for commands? */
538 	unsigned int     command_waiters;
539 
540 	/* How many users are waiting for watchdogs? */
541 	unsigned int     watchdog_waiters;
542 
543 	/* How many users are waiting for message responses? */
544 	unsigned int     response_waiters;
545 
546 	/*
547 	 * Tells what the lower layer has last been asked to watch for,
548 	 * messages and/or watchdogs.  Protected by watch_lock.
549 	 */
550 	unsigned int     last_watch_mask;
551 
552 	/*
553 	 * The event receiver for my BMC, only really used at panic
554 	 * shutdown as a place to store this.
555 	 */
556 	unsigned char event_receiver;
557 	unsigned char event_receiver_lun;
558 	unsigned char local_sel_device;
559 	unsigned char local_event_generator;
560 
561 	/* For handling of maintenance mode. */
562 	int maintenance_mode;
563 	bool maintenance_mode_enable;
564 	int auto_maintenance_timeout;
565 	spinlock_t maintenance_mode_lock; /* Used in a timer... */
566 
567 	/*
568 	 * If we are doing maintenance on something on IPMB, extend
569 	 * the timeout time to avoid timeouts writing firmware and
570 	 * such.
571 	 */
572 	int ipmb_maintenance_mode_timeout;
573 
574 	/*
575 	 * A cheap hack, if this is non-null and a message to an
576 	 * interface comes in with a NULL user, call this routine with
577 	 * it.  Note that the message will still be freed by the
578 	 * caller.  This only works on the system interface.
579 	 *
580 	 * Protected by bmc_reg_mutex.
581 	 */
582 	void (*null_user_handler)(struct ipmi_smi *intf,
583 				  struct ipmi_recv_msg *msg);
584 
585 	/*
586 	 * When we are scanning the channels for an SMI, this will
587 	 * tell which channel we are scanning.
588 	 */
589 	int curr_channel;
590 
591 	/* Channel information */
592 	struct ipmi_channel_set *channel_list;
593 	unsigned int curr_working_cset; /* First index into the following. */
594 	struct ipmi_channel_set wchannels[2];
595 	struct ipmi_my_addrinfo addrinfo[IPMI_MAX_CHANNELS];
596 	bool channels_ready;
597 
598 	atomic_t stats[IPMI_NUM_STATS];
599 
600 	/*
601 	 * run_to_completion duplicate of smb_info, smi_info
602 	 * and ipmi_serial_info structures. Used to decrease numbers of
603 	 * parameters passed by "low" level IPMI code.
604 	 */
605 	int run_to_completion;
606 };
607 #define to_si_intf_from_dev(device) container_of(device, struct ipmi_smi, dev)
608 
609 static void __get_guid(struct ipmi_smi *intf);
610 static void __ipmi_bmc_unregister(struct ipmi_smi *intf);
611 static int __ipmi_bmc_register(struct ipmi_smi *intf,
612 			       struct ipmi_device_id *id,
613 			       bool guid_set, guid_t *guid, int intf_num);
614 static int __scan_channels(struct ipmi_smi *intf, struct ipmi_device_id *id);
615 
616 
617 /*
618  * The driver model view of the IPMI messaging driver.
619  */
620 static struct platform_driver ipmidriver = {
621 	.driver = {
622 		.name = "ipmi",
623 		.bus = &platform_bus_type
624 	}
625 };
626 /*
627  * This mutex keeps us from adding the same BMC twice.
628  */
629 static DEFINE_MUTEX(ipmidriver_mutex);
630 
631 static LIST_HEAD(ipmi_interfaces);
632 static DEFINE_MUTEX(ipmi_interfaces_mutex);
633 #define ipmi_interfaces_mutex_held() \
634 	lockdep_is_held(&ipmi_interfaces_mutex)
635 static struct srcu_struct ipmi_interfaces_srcu;
636 
637 /*
638  * List of watchers that want to know when smi's are added and deleted.
639  */
640 static LIST_HEAD(smi_watchers);
641 static DEFINE_MUTEX(smi_watchers_mutex);
642 
643 #define ipmi_inc_stat(intf, stat) \
644 	atomic_inc(&(intf)->stats[IPMI_STAT_ ## stat])
645 #define ipmi_get_stat(intf, stat) \
646 	((unsigned int) atomic_read(&(intf)->stats[IPMI_STAT_ ## stat]))
647 
648 static const char * const addr_src_to_str[] = {
649 	"invalid", "hotmod", "hardcoded", "SPMI", "ACPI", "SMBIOS", "PCI",
650 	"device-tree", "platform"
651 };
652 
ipmi_addr_src_to_str(enum ipmi_addr_src src)653 const char *ipmi_addr_src_to_str(enum ipmi_addr_src src)
654 {
655 	if (src >= SI_LAST)
656 		src = 0; /* Invalid */
657 	return addr_src_to_str[src];
658 }
659 EXPORT_SYMBOL(ipmi_addr_src_to_str);
660 
is_lan_addr(struct ipmi_addr * addr)661 static int is_lan_addr(struct ipmi_addr *addr)
662 {
663 	return addr->addr_type == IPMI_LAN_ADDR_TYPE;
664 }
665 
is_ipmb_addr(struct ipmi_addr * addr)666 static int is_ipmb_addr(struct ipmi_addr *addr)
667 {
668 	return addr->addr_type == IPMI_IPMB_ADDR_TYPE;
669 }
670 
is_ipmb_bcast_addr(struct ipmi_addr * addr)671 static int is_ipmb_bcast_addr(struct ipmi_addr *addr)
672 {
673 	return addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE;
674 }
675 
is_ipmb_direct_addr(struct ipmi_addr * addr)676 static int is_ipmb_direct_addr(struct ipmi_addr *addr)
677 {
678 	return addr->addr_type == IPMI_IPMB_DIRECT_ADDR_TYPE;
679 }
680 
free_recv_msg_list(struct list_head * q)681 static void free_recv_msg_list(struct list_head *q)
682 {
683 	struct ipmi_recv_msg *msg, *msg2;
684 
685 	list_for_each_entry_safe(msg, msg2, q, link) {
686 		list_del(&msg->link);
687 		ipmi_free_recv_msg(msg);
688 	}
689 }
690 
free_smi_msg_list(struct list_head * q)691 static void free_smi_msg_list(struct list_head *q)
692 {
693 	struct ipmi_smi_msg *msg, *msg2;
694 
695 	list_for_each_entry_safe(msg, msg2, q, link) {
696 		list_del(&msg->link);
697 		ipmi_free_smi_msg(msg);
698 	}
699 }
700 
clean_up_interface_data(struct ipmi_smi * intf)701 static void clean_up_interface_data(struct ipmi_smi *intf)
702 {
703 	int              i;
704 	struct cmd_rcvr  *rcvr, *rcvr2;
705 	struct list_head list;
706 
707 	tasklet_kill(&intf->recv_tasklet);
708 
709 	free_smi_msg_list(&intf->waiting_rcv_msgs);
710 	free_recv_msg_list(&intf->waiting_events);
711 
712 	/*
713 	 * Wholesale remove all the entries from the list in the
714 	 * interface and wait for RCU to know that none are in use.
715 	 */
716 	mutex_lock(&intf->cmd_rcvrs_mutex);
717 	INIT_LIST_HEAD(&list);
718 	list_splice_init_rcu(&intf->cmd_rcvrs, &list, synchronize_rcu);
719 	mutex_unlock(&intf->cmd_rcvrs_mutex);
720 
721 	list_for_each_entry_safe(rcvr, rcvr2, &list, link)
722 		kfree(rcvr);
723 
724 	for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
725 		if ((intf->seq_table[i].inuse)
726 					&& (intf->seq_table[i].recv_msg))
727 			ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
728 	}
729 }
730 
intf_free(struct kref * ref)731 static void intf_free(struct kref *ref)
732 {
733 	struct ipmi_smi *intf = container_of(ref, struct ipmi_smi, refcount);
734 
735 	clean_up_interface_data(intf);
736 	kfree(intf);
737 }
738 
ipmi_smi_watcher_register(struct ipmi_smi_watcher * watcher)739 int ipmi_smi_watcher_register(struct ipmi_smi_watcher *watcher)
740 {
741 	struct ipmi_smi *intf;
742 	int index, rv;
743 
744 	/*
745 	 * Make sure the driver is actually initialized, this handles
746 	 * problems with initialization order.
747 	 */
748 	rv = ipmi_init_msghandler();
749 	if (rv)
750 		return rv;
751 
752 	mutex_lock(&smi_watchers_mutex);
753 
754 	list_add(&watcher->link, &smi_watchers);
755 
756 	index = srcu_read_lock(&ipmi_interfaces_srcu);
757 	list_for_each_entry_rcu(intf, &ipmi_interfaces, link,
758 			lockdep_is_held(&smi_watchers_mutex)) {
759 		int intf_num = READ_ONCE(intf->intf_num);
760 
761 		if (intf_num == -1)
762 			continue;
763 		watcher->new_smi(intf_num, intf->si_dev);
764 	}
765 	srcu_read_unlock(&ipmi_interfaces_srcu, index);
766 
767 	mutex_unlock(&smi_watchers_mutex);
768 
769 	return 0;
770 }
771 EXPORT_SYMBOL(ipmi_smi_watcher_register);
772 
ipmi_smi_watcher_unregister(struct ipmi_smi_watcher * watcher)773 int ipmi_smi_watcher_unregister(struct ipmi_smi_watcher *watcher)
774 {
775 	mutex_lock(&smi_watchers_mutex);
776 	list_del(&watcher->link);
777 	mutex_unlock(&smi_watchers_mutex);
778 	return 0;
779 }
780 EXPORT_SYMBOL(ipmi_smi_watcher_unregister);
781 
782 /*
783  * Must be called with smi_watchers_mutex held.
784  */
785 static void
call_smi_watchers(int i,struct device * dev)786 call_smi_watchers(int i, struct device *dev)
787 {
788 	struct ipmi_smi_watcher *w;
789 
790 	mutex_lock(&smi_watchers_mutex);
791 	list_for_each_entry(w, &smi_watchers, link) {
792 		if (try_module_get(w->owner)) {
793 			w->new_smi(i, dev);
794 			module_put(w->owner);
795 		}
796 	}
797 	mutex_unlock(&smi_watchers_mutex);
798 }
799 
800 static int
ipmi_addr_equal(struct ipmi_addr * addr1,struct ipmi_addr * addr2)801 ipmi_addr_equal(struct ipmi_addr *addr1, struct ipmi_addr *addr2)
802 {
803 	if (addr1->addr_type != addr2->addr_type)
804 		return 0;
805 
806 	if (addr1->channel != addr2->channel)
807 		return 0;
808 
809 	if (addr1->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
810 		struct ipmi_system_interface_addr *smi_addr1
811 		    = (struct ipmi_system_interface_addr *) addr1;
812 		struct ipmi_system_interface_addr *smi_addr2
813 		    = (struct ipmi_system_interface_addr *) addr2;
814 		return (smi_addr1->lun == smi_addr2->lun);
815 	}
816 
817 	if (is_ipmb_addr(addr1) || is_ipmb_bcast_addr(addr1)) {
818 		struct ipmi_ipmb_addr *ipmb_addr1
819 		    = (struct ipmi_ipmb_addr *) addr1;
820 		struct ipmi_ipmb_addr *ipmb_addr2
821 		    = (struct ipmi_ipmb_addr *) addr2;
822 
823 		return ((ipmb_addr1->slave_addr == ipmb_addr2->slave_addr)
824 			&& (ipmb_addr1->lun == ipmb_addr2->lun));
825 	}
826 
827 	if (is_ipmb_direct_addr(addr1)) {
828 		struct ipmi_ipmb_direct_addr *daddr1
829 			= (struct ipmi_ipmb_direct_addr *) addr1;
830 		struct ipmi_ipmb_direct_addr *daddr2
831 			= (struct ipmi_ipmb_direct_addr *) addr2;
832 
833 		return daddr1->slave_addr == daddr2->slave_addr &&
834 			daddr1->rq_lun == daddr2->rq_lun &&
835 			daddr1->rs_lun == daddr2->rs_lun;
836 	}
837 
838 	if (is_lan_addr(addr1)) {
839 		struct ipmi_lan_addr *lan_addr1
840 			= (struct ipmi_lan_addr *) addr1;
841 		struct ipmi_lan_addr *lan_addr2
842 		    = (struct ipmi_lan_addr *) addr2;
843 
844 		return ((lan_addr1->remote_SWID == lan_addr2->remote_SWID)
845 			&& (lan_addr1->local_SWID == lan_addr2->local_SWID)
846 			&& (lan_addr1->session_handle
847 			    == lan_addr2->session_handle)
848 			&& (lan_addr1->lun == lan_addr2->lun));
849 	}
850 
851 	return 1;
852 }
853 
ipmi_validate_addr(struct ipmi_addr * addr,int len)854 int ipmi_validate_addr(struct ipmi_addr *addr, int len)
855 {
856 	if (len < sizeof(struct ipmi_system_interface_addr))
857 		return -EINVAL;
858 
859 	if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
860 		if (addr->channel != IPMI_BMC_CHANNEL)
861 			return -EINVAL;
862 		return 0;
863 	}
864 
865 	if ((addr->channel == IPMI_BMC_CHANNEL)
866 	    || (addr->channel >= IPMI_MAX_CHANNELS)
867 	    || (addr->channel < 0))
868 		return -EINVAL;
869 
870 	if (is_ipmb_addr(addr) || is_ipmb_bcast_addr(addr)) {
871 		if (len < sizeof(struct ipmi_ipmb_addr))
872 			return -EINVAL;
873 		return 0;
874 	}
875 
876 	if (is_ipmb_direct_addr(addr)) {
877 		struct ipmi_ipmb_direct_addr *daddr = (void *) addr;
878 
879 		if (addr->channel != 0)
880 			return -EINVAL;
881 		if (len < sizeof(struct ipmi_ipmb_direct_addr))
882 			return -EINVAL;
883 
884 		if (daddr->slave_addr & 0x01)
885 			return -EINVAL;
886 		if (daddr->rq_lun >= 4)
887 			return -EINVAL;
888 		if (daddr->rs_lun >= 4)
889 			return -EINVAL;
890 		return 0;
891 	}
892 
893 	if (is_lan_addr(addr)) {
894 		if (len < sizeof(struct ipmi_lan_addr))
895 			return -EINVAL;
896 		return 0;
897 	}
898 
899 	return -EINVAL;
900 }
901 EXPORT_SYMBOL(ipmi_validate_addr);
902 
ipmi_addr_length(int addr_type)903 unsigned int ipmi_addr_length(int addr_type)
904 {
905 	if (addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
906 		return sizeof(struct ipmi_system_interface_addr);
907 
908 	if ((addr_type == IPMI_IPMB_ADDR_TYPE)
909 			|| (addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE))
910 		return sizeof(struct ipmi_ipmb_addr);
911 
912 	if (addr_type == IPMI_IPMB_DIRECT_ADDR_TYPE)
913 		return sizeof(struct ipmi_ipmb_direct_addr);
914 
915 	if (addr_type == IPMI_LAN_ADDR_TYPE)
916 		return sizeof(struct ipmi_lan_addr);
917 
918 	return 0;
919 }
920 EXPORT_SYMBOL(ipmi_addr_length);
921 
deliver_response(struct ipmi_smi * intf,struct ipmi_recv_msg * msg)922 static int deliver_response(struct ipmi_smi *intf, struct ipmi_recv_msg *msg)
923 {
924 	int rv = 0;
925 
926 	if (!msg->user) {
927 		/* Special handling for NULL users. */
928 		if (intf->null_user_handler) {
929 			intf->null_user_handler(intf, msg);
930 		} else {
931 			/* No handler, so give up. */
932 			rv = -EINVAL;
933 		}
934 		ipmi_free_recv_msg(msg);
935 	} else if (oops_in_progress) {
936 		/*
937 		 * If we are running in the panic context, calling the
938 		 * receive handler doesn't much meaning and has a deadlock
939 		 * risk.  At this moment, simply skip it in that case.
940 		 */
941 		ipmi_free_recv_msg(msg);
942 		atomic_dec(&msg->user->nr_msgs);
943 	} else {
944 		int index;
945 		struct ipmi_user *user = acquire_ipmi_user(msg->user, &index);
946 
947 		if (user) {
948 			atomic_dec(&user->nr_msgs);
949 			user->handler->ipmi_recv_hndl(msg, user->handler_data);
950 			release_ipmi_user(user, index);
951 		} else {
952 			/* User went away, give up. */
953 			ipmi_free_recv_msg(msg);
954 			rv = -EINVAL;
955 		}
956 	}
957 
958 	return rv;
959 }
960 
deliver_local_response(struct ipmi_smi * intf,struct ipmi_recv_msg * msg)961 static void deliver_local_response(struct ipmi_smi *intf,
962 				   struct ipmi_recv_msg *msg)
963 {
964 	if (deliver_response(intf, msg))
965 		ipmi_inc_stat(intf, unhandled_local_responses);
966 	else
967 		ipmi_inc_stat(intf, handled_local_responses);
968 }
969 
deliver_err_response(struct ipmi_smi * intf,struct ipmi_recv_msg * msg,int err)970 static void deliver_err_response(struct ipmi_smi *intf,
971 				 struct ipmi_recv_msg *msg, int err)
972 {
973 	msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
974 	msg->msg_data[0] = err;
975 	msg->msg.netfn |= 1; /* Convert to a response. */
976 	msg->msg.data_len = 1;
977 	msg->msg.data = msg->msg_data;
978 	deliver_local_response(intf, msg);
979 }
980 
smi_add_watch(struct ipmi_smi * intf,unsigned int flags)981 static void smi_add_watch(struct ipmi_smi *intf, unsigned int flags)
982 {
983 	unsigned long iflags;
984 
985 	if (!intf->handlers->set_need_watch)
986 		return;
987 
988 	spin_lock_irqsave(&intf->watch_lock, iflags);
989 	if (flags & IPMI_WATCH_MASK_CHECK_MESSAGES)
990 		intf->response_waiters++;
991 
992 	if (flags & IPMI_WATCH_MASK_CHECK_WATCHDOG)
993 		intf->watchdog_waiters++;
994 
995 	if (flags & IPMI_WATCH_MASK_CHECK_COMMANDS)
996 		intf->command_waiters++;
997 
998 	if ((intf->last_watch_mask & flags) != flags) {
999 		intf->last_watch_mask |= flags;
1000 		intf->handlers->set_need_watch(intf->send_info,
1001 					       intf->last_watch_mask);
1002 	}
1003 	spin_unlock_irqrestore(&intf->watch_lock, iflags);
1004 }
1005 
smi_remove_watch(struct ipmi_smi * intf,unsigned int flags)1006 static void smi_remove_watch(struct ipmi_smi *intf, unsigned int flags)
1007 {
1008 	unsigned long iflags;
1009 
1010 	if (!intf->handlers->set_need_watch)
1011 		return;
1012 
1013 	spin_lock_irqsave(&intf->watch_lock, iflags);
1014 	if (flags & IPMI_WATCH_MASK_CHECK_MESSAGES)
1015 		intf->response_waiters--;
1016 
1017 	if (flags & IPMI_WATCH_MASK_CHECK_WATCHDOG)
1018 		intf->watchdog_waiters--;
1019 
1020 	if (flags & IPMI_WATCH_MASK_CHECK_COMMANDS)
1021 		intf->command_waiters--;
1022 
1023 	flags = 0;
1024 	if (intf->response_waiters)
1025 		flags |= IPMI_WATCH_MASK_CHECK_MESSAGES;
1026 	if (intf->watchdog_waiters)
1027 		flags |= IPMI_WATCH_MASK_CHECK_WATCHDOG;
1028 	if (intf->command_waiters)
1029 		flags |= IPMI_WATCH_MASK_CHECK_COMMANDS;
1030 
1031 	if (intf->last_watch_mask != flags) {
1032 		intf->last_watch_mask = flags;
1033 		intf->handlers->set_need_watch(intf->send_info,
1034 					       intf->last_watch_mask);
1035 	}
1036 	spin_unlock_irqrestore(&intf->watch_lock, iflags);
1037 }
1038 
1039 /*
1040  * Find the next sequence number not being used and add the given
1041  * message with the given timeout to the sequence table.  This must be
1042  * called with the interface's seq_lock held.
1043  */
intf_next_seq(struct ipmi_smi * intf,struct ipmi_recv_msg * recv_msg,unsigned long timeout,int retries,int broadcast,unsigned char * seq,long * seqid)1044 static int intf_next_seq(struct ipmi_smi      *intf,
1045 			 struct ipmi_recv_msg *recv_msg,
1046 			 unsigned long        timeout,
1047 			 int                  retries,
1048 			 int                  broadcast,
1049 			 unsigned char        *seq,
1050 			 long                 *seqid)
1051 {
1052 	int          rv = 0;
1053 	unsigned int i;
1054 
1055 	if (timeout == 0)
1056 		timeout = default_retry_ms;
1057 	if (retries < 0)
1058 		retries = default_max_retries;
1059 
1060 	for (i = intf->curr_seq; (i+1)%IPMI_IPMB_NUM_SEQ != intf->curr_seq;
1061 					i = (i+1)%IPMI_IPMB_NUM_SEQ) {
1062 		if (!intf->seq_table[i].inuse)
1063 			break;
1064 	}
1065 
1066 	if (!intf->seq_table[i].inuse) {
1067 		intf->seq_table[i].recv_msg = recv_msg;
1068 
1069 		/*
1070 		 * Start with the maximum timeout, when the send response
1071 		 * comes in we will start the real timer.
1072 		 */
1073 		intf->seq_table[i].timeout = MAX_MSG_TIMEOUT;
1074 		intf->seq_table[i].orig_timeout = timeout;
1075 		intf->seq_table[i].retries_left = retries;
1076 		intf->seq_table[i].broadcast = broadcast;
1077 		intf->seq_table[i].inuse = 1;
1078 		intf->seq_table[i].seqid = NEXT_SEQID(intf->seq_table[i].seqid);
1079 		*seq = i;
1080 		*seqid = intf->seq_table[i].seqid;
1081 		intf->curr_seq = (i+1)%IPMI_IPMB_NUM_SEQ;
1082 		smi_add_watch(intf, IPMI_WATCH_MASK_CHECK_MESSAGES);
1083 		need_waiter(intf);
1084 	} else {
1085 		rv = -EAGAIN;
1086 	}
1087 
1088 	return rv;
1089 }
1090 
1091 /*
1092  * Return the receive message for the given sequence number and
1093  * release the sequence number so it can be reused.  Some other data
1094  * is passed in to be sure the message matches up correctly (to help
1095  * guard against message coming in after their timeout and the
1096  * sequence number being reused).
1097  */
intf_find_seq(struct ipmi_smi * intf,unsigned char seq,short channel,unsigned char cmd,unsigned char netfn,struct ipmi_addr * addr,struct ipmi_recv_msg ** recv_msg)1098 static int intf_find_seq(struct ipmi_smi      *intf,
1099 			 unsigned char        seq,
1100 			 short                channel,
1101 			 unsigned char        cmd,
1102 			 unsigned char        netfn,
1103 			 struct ipmi_addr     *addr,
1104 			 struct ipmi_recv_msg **recv_msg)
1105 {
1106 	int           rv = -ENODEV;
1107 	unsigned long flags;
1108 
1109 	if (seq >= IPMI_IPMB_NUM_SEQ)
1110 		return -EINVAL;
1111 
1112 	spin_lock_irqsave(&intf->seq_lock, flags);
1113 	if (intf->seq_table[seq].inuse) {
1114 		struct ipmi_recv_msg *msg = intf->seq_table[seq].recv_msg;
1115 
1116 		if ((msg->addr.channel == channel) && (msg->msg.cmd == cmd)
1117 				&& (msg->msg.netfn == netfn)
1118 				&& (ipmi_addr_equal(addr, &msg->addr))) {
1119 			*recv_msg = msg;
1120 			intf->seq_table[seq].inuse = 0;
1121 			smi_remove_watch(intf, IPMI_WATCH_MASK_CHECK_MESSAGES);
1122 			rv = 0;
1123 		}
1124 	}
1125 	spin_unlock_irqrestore(&intf->seq_lock, flags);
1126 
1127 	return rv;
1128 }
1129 
1130 
1131 /* Start the timer for a specific sequence table entry. */
intf_start_seq_timer(struct ipmi_smi * intf,long msgid)1132 static int intf_start_seq_timer(struct ipmi_smi *intf,
1133 				long       msgid)
1134 {
1135 	int           rv = -ENODEV;
1136 	unsigned long flags;
1137 	unsigned char seq;
1138 	unsigned long seqid;
1139 
1140 
1141 	GET_SEQ_FROM_MSGID(msgid, seq, seqid);
1142 
1143 	spin_lock_irqsave(&intf->seq_lock, flags);
1144 	/*
1145 	 * We do this verification because the user can be deleted
1146 	 * while a message is outstanding.
1147 	 */
1148 	if ((intf->seq_table[seq].inuse)
1149 				&& (intf->seq_table[seq].seqid == seqid)) {
1150 		struct seq_table *ent = &intf->seq_table[seq];
1151 		ent->timeout = ent->orig_timeout;
1152 		rv = 0;
1153 	}
1154 	spin_unlock_irqrestore(&intf->seq_lock, flags);
1155 
1156 	return rv;
1157 }
1158 
1159 /* Got an error for the send message for a specific sequence number. */
intf_err_seq(struct ipmi_smi * intf,long msgid,unsigned int err)1160 static int intf_err_seq(struct ipmi_smi *intf,
1161 			long         msgid,
1162 			unsigned int err)
1163 {
1164 	int                  rv = -ENODEV;
1165 	unsigned long        flags;
1166 	unsigned char        seq;
1167 	unsigned long        seqid;
1168 	struct ipmi_recv_msg *msg = NULL;
1169 
1170 
1171 	GET_SEQ_FROM_MSGID(msgid, seq, seqid);
1172 
1173 	spin_lock_irqsave(&intf->seq_lock, flags);
1174 	/*
1175 	 * We do this verification because the user can be deleted
1176 	 * while a message is outstanding.
1177 	 */
1178 	if ((intf->seq_table[seq].inuse)
1179 				&& (intf->seq_table[seq].seqid == seqid)) {
1180 		struct seq_table *ent = &intf->seq_table[seq];
1181 
1182 		ent->inuse = 0;
1183 		smi_remove_watch(intf, IPMI_WATCH_MASK_CHECK_MESSAGES);
1184 		msg = ent->recv_msg;
1185 		rv = 0;
1186 	}
1187 	spin_unlock_irqrestore(&intf->seq_lock, flags);
1188 
1189 	if (msg)
1190 		deliver_err_response(intf, msg, err);
1191 
1192 	return rv;
1193 }
1194 
free_user_work(struct work_struct * work)1195 static void free_user_work(struct work_struct *work)
1196 {
1197 	struct ipmi_user *user = container_of(work, struct ipmi_user,
1198 					      remove_work);
1199 
1200 	cleanup_srcu_struct(&user->release_barrier);
1201 	vfree(user);
1202 }
1203 
ipmi_create_user(unsigned int if_num,const struct ipmi_user_hndl * handler,void * handler_data,struct ipmi_user ** user)1204 int ipmi_create_user(unsigned int          if_num,
1205 		     const struct ipmi_user_hndl *handler,
1206 		     void                  *handler_data,
1207 		     struct ipmi_user      **user)
1208 {
1209 	unsigned long flags;
1210 	struct ipmi_user *new_user;
1211 	int           rv, index;
1212 	struct ipmi_smi *intf;
1213 
1214 	/*
1215 	 * There is no module usecount here, because it's not
1216 	 * required.  Since this can only be used by and called from
1217 	 * other modules, they will implicitly use this module, and
1218 	 * thus this can't be removed unless the other modules are
1219 	 * removed.
1220 	 */
1221 
1222 	if (handler == NULL)
1223 		return -EINVAL;
1224 
1225 	/*
1226 	 * Make sure the driver is actually initialized, this handles
1227 	 * problems with initialization order.
1228 	 */
1229 	rv = ipmi_init_msghandler();
1230 	if (rv)
1231 		return rv;
1232 
1233 	new_user = vzalloc(sizeof(*new_user));
1234 	if (!new_user)
1235 		return -ENOMEM;
1236 
1237 	index = srcu_read_lock(&ipmi_interfaces_srcu);
1238 	list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
1239 		if (intf->intf_num == if_num)
1240 			goto found;
1241 	}
1242 	/* Not found, return an error */
1243 	rv = -EINVAL;
1244 	goto out_unlock;
1245 
1246  found:
1247 	if (atomic_add_return(1, &intf->nr_users) > max_users) {
1248 		rv = -EBUSY;
1249 		goto out_kfree;
1250 	}
1251 
1252 	INIT_WORK(&new_user->remove_work, free_user_work);
1253 
1254 	rv = init_srcu_struct(&new_user->release_barrier);
1255 	if (rv)
1256 		goto out_kfree;
1257 
1258 	if (!try_module_get(intf->owner)) {
1259 		rv = -ENODEV;
1260 		goto out_kfree;
1261 	}
1262 
1263 	/* Note that each existing user holds a refcount to the interface. */
1264 	kref_get(&intf->refcount);
1265 
1266 	atomic_set(&new_user->nr_msgs, 0);
1267 	kref_init(&new_user->refcount);
1268 	new_user->handler = handler;
1269 	new_user->handler_data = handler_data;
1270 	new_user->intf = intf;
1271 	new_user->gets_events = false;
1272 
1273 	rcu_assign_pointer(new_user->self, new_user);
1274 	spin_lock_irqsave(&intf->seq_lock, flags);
1275 	list_add_rcu(&new_user->link, &intf->users);
1276 	spin_unlock_irqrestore(&intf->seq_lock, flags);
1277 	if (handler->ipmi_watchdog_pretimeout)
1278 		/* User wants pretimeouts, so make sure to watch for them. */
1279 		smi_add_watch(intf, IPMI_WATCH_MASK_CHECK_WATCHDOG);
1280 	srcu_read_unlock(&ipmi_interfaces_srcu, index);
1281 	*user = new_user;
1282 	return 0;
1283 
1284 out_kfree:
1285 	atomic_dec(&intf->nr_users);
1286 out_unlock:
1287 	srcu_read_unlock(&ipmi_interfaces_srcu, index);
1288 	vfree(new_user);
1289 	return rv;
1290 }
1291 EXPORT_SYMBOL(ipmi_create_user);
1292 
ipmi_get_smi_info(int if_num,struct ipmi_smi_info * data)1293 int ipmi_get_smi_info(int if_num, struct ipmi_smi_info *data)
1294 {
1295 	int rv, index;
1296 	struct ipmi_smi *intf;
1297 
1298 	index = srcu_read_lock(&ipmi_interfaces_srcu);
1299 	list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
1300 		if (intf->intf_num == if_num)
1301 			goto found;
1302 	}
1303 	srcu_read_unlock(&ipmi_interfaces_srcu, index);
1304 
1305 	/* Not found, return an error */
1306 	return -EINVAL;
1307 
1308 found:
1309 	if (!intf->handlers->get_smi_info)
1310 		rv = -ENOTTY;
1311 	else
1312 		rv = intf->handlers->get_smi_info(intf->send_info, data);
1313 	srcu_read_unlock(&ipmi_interfaces_srcu, index);
1314 
1315 	return rv;
1316 }
1317 EXPORT_SYMBOL(ipmi_get_smi_info);
1318 
free_user(struct kref * ref)1319 static void free_user(struct kref *ref)
1320 {
1321 	struct ipmi_user *user = container_of(ref, struct ipmi_user, refcount);
1322 
1323 	/* SRCU cleanup must happen in task context. */
1324 	queue_work(remove_work_wq, &user->remove_work);
1325 }
1326 
_ipmi_destroy_user(struct ipmi_user * user)1327 static void _ipmi_destroy_user(struct ipmi_user *user)
1328 {
1329 	struct ipmi_smi  *intf = user->intf;
1330 	int              i;
1331 	unsigned long    flags;
1332 	struct cmd_rcvr  *rcvr;
1333 	struct cmd_rcvr  *rcvrs = NULL;
1334 	struct module    *owner;
1335 
1336 	if (!acquire_ipmi_user(user, &i)) {
1337 		/*
1338 		 * The user has already been cleaned up, just make sure
1339 		 * nothing is using it and return.
1340 		 */
1341 		synchronize_srcu(&user->release_barrier);
1342 		return;
1343 	}
1344 
1345 	rcu_assign_pointer(user->self, NULL);
1346 	release_ipmi_user(user, i);
1347 
1348 	synchronize_srcu(&user->release_barrier);
1349 
1350 	if (user->handler->shutdown)
1351 		user->handler->shutdown(user->handler_data);
1352 
1353 	if (user->handler->ipmi_watchdog_pretimeout)
1354 		smi_remove_watch(intf, IPMI_WATCH_MASK_CHECK_WATCHDOG);
1355 
1356 	if (user->gets_events)
1357 		atomic_dec(&intf->event_waiters);
1358 
1359 	/* Remove the user from the interface's sequence table. */
1360 	spin_lock_irqsave(&intf->seq_lock, flags);
1361 	list_del_rcu(&user->link);
1362 	atomic_dec(&intf->nr_users);
1363 
1364 	for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
1365 		if (intf->seq_table[i].inuse
1366 		    && (intf->seq_table[i].recv_msg->user == user)) {
1367 			intf->seq_table[i].inuse = 0;
1368 			smi_remove_watch(intf, IPMI_WATCH_MASK_CHECK_MESSAGES);
1369 			ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
1370 		}
1371 	}
1372 	spin_unlock_irqrestore(&intf->seq_lock, flags);
1373 
1374 	/*
1375 	 * Remove the user from the command receiver's table.  First
1376 	 * we build a list of everything (not using the standard link,
1377 	 * since other things may be using it till we do
1378 	 * synchronize_srcu()) then free everything in that list.
1379 	 */
1380 	mutex_lock(&intf->cmd_rcvrs_mutex);
1381 	list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link,
1382 				lockdep_is_held(&intf->cmd_rcvrs_mutex)) {
1383 		if (rcvr->user == user) {
1384 			list_del_rcu(&rcvr->link);
1385 			rcvr->next = rcvrs;
1386 			rcvrs = rcvr;
1387 		}
1388 	}
1389 	mutex_unlock(&intf->cmd_rcvrs_mutex);
1390 	synchronize_rcu();
1391 	while (rcvrs) {
1392 		rcvr = rcvrs;
1393 		rcvrs = rcvr->next;
1394 		kfree(rcvr);
1395 	}
1396 
1397 	owner = intf->owner;
1398 	kref_put(&intf->refcount, intf_free);
1399 	module_put(owner);
1400 }
1401 
ipmi_destroy_user(struct ipmi_user * user)1402 int ipmi_destroy_user(struct ipmi_user *user)
1403 {
1404 	_ipmi_destroy_user(user);
1405 
1406 	kref_put(&user->refcount, free_user);
1407 
1408 	return 0;
1409 }
1410 EXPORT_SYMBOL(ipmi_destroy_user);
1411 
ipmi_get_version(struct ipmi_user * user,unsigned char * major,unsigned char * minor)1412 int ipmi_get_version(struct ipmi_user *user,
1413 		     unsigned char *major,
1414 		     unsigned char *minor)
1415 {
1416 	struct ipmi_device_id id;
1417 	int rv, index;
1418 
1419 	user = acquire_ipmi_user(user, &index);
1420 	if (!user)
1421 		return -ENODEV;
1422 
1423 	rv = bmc_get_device_id(user->intf, NULL, &id, NULL, NULL);
1424 	if (!rv) {
1425 		*major = ipmi_version_major(&id);
1426 		*minor = ipmi_version_minor(&id);
1427 	}
1428 	release_ipmi_user(user, index);
1429 
1430 	return rv;
1431 }
1432 EXPORT_SYMBOL(ipmi_get_version);
1433 
ipmi_set_my_address(struct ipmi_user * user,unsigned int channel,unsigned char address)1434 int ipmi_set_my_address(struct ipmi_user *user,
1435 			unsigned int  channel,
1436 			unsigned char address)
1437 {
1438 	int index, rv = 0;
1439 
1440 	user = acquire_ipmi_user(user, &index);
1441 	if (!user)
1442 		return -ENODEV;
1443 
1444 	if (channel >= IPMI_MAX_CHANNELS) {
1445 		rv = -EINVAL;
1446 	} else {
1447 		channel = array_index_nospec(channel, IPMI_MAX_CHANNELS);
1448 		user->intf->addrinfo[channel].address = address;
1449 	}
1450 	release_ipmi_user(user, index);
1451 
1452 	return rv;
1453 }
1454 EXPORT_SYMBOL(ipmi_set_my_address);
1455 
ipmi_get_my_address(struct ipmi_user * user,unsigned int channel,unsigned char * address)1456 int ipmi_get_my_address(struct ipmi_user *user,
1457 			unsigned int  channel,
1458 			unsigned char *address)
1459 {
1460 	int index, rv = 0;
1461 
1462 	user = acquire_ipmi_user(user, &index);
1463 	if (!user)
1464 		return -ENODEV;
1465 
1466 	if (channel >= IPMI_MAX_CHANNELS) {
1467 		rv = -EINVAL;
1468 	} else {
1469 		channel = array_index_nospec(channel, IPMI_MAX_CHANNELS);
1470 		*address = user->intf->addrinfo[channel].address;
1471 	}
1472 	release_ipmi_user(user, index);
1473 
1474 	return rv;
1475 }
1476 EXPORT_SYMBOL(ipmi_get_my_address);
1477 
ipmi_set_my_LUN(struct ipmi_user * user,unsigned int channel,unsigned char LUN)1478 int ipmi_set_my_LUN(struct ipmi_user *user,
1479 		    unsigned int  channel,
1480 		    unsigned char LUN)
1481 {
1482 	int index, rv = 0;
1483 
1484 	user = acquire_ipmi_user(user, &index);
1485 	if (!user)
1486 		return -ENODEV;
1487 
1488 	if (channel >= IPMI_MAX_CHANNELS) {
1489 		rv = -EINVAL;
1490 	} else {
1491 		channel = array_index_nospec(channel, IPMI_MAX_CHANNELS);
1492 		user->intf->addrinfo[channel].lun = LUN & 0x3;
1493 	}
1494 	release_ipmi_user(user, index);
1495 
1496 	return rv;
1497 }
1498 EXPORT_SYMBOL(ipmi_set_my_LUN);
1499 
ipmi_get_my_LUN(struct ipmi_user * user,unsigned int channel,unsigned char * address)1500 int ipmi_get_my_LUN(struct ipmi_user *user,
1501 		    unsigned int  channel,
1502 		    unsigned char *address)
1503 {
1504 	int index, rv = 0;
1505 
1506 	user = acquire_ipmi_user(user, &index);
1507 	if (!user)
1508 		return -ENODEV;
1509 
1510 	if (channel >= IPMI_MAX_CHANNELS) {
1511 		rv = -EINVAL;
1512 	} else {
1513 		channel = array_index_nospec(channel, IPMI_MAX_CHANNELS);
1514 		*address = user->intf->addrinfo[channel].lun;
1515 	}
1516 	release_ipmi_user(user, index);
1517 
1518 	return rv;
1519 }
1520 EXPORT_SYMBOL(ipmi_get_my_LUN);
1521 
ipmi_get_maintenance_mode(struct ipmi_user * user)1522 int ipmi_get_maintenance_mode(struct ipmi_user *user)
1523 {
1524 	int mode, index;
1525 	unsigned long flags;
1526 
1527 	user = acquire_ipmi_user(user, &index);
1528 	if (!user)
1529 		return -ENODEV;
1530 
1531 	spin_lock_irqsave(&user->intf->maintenance_mode_lock, flags);
1532 	mode = user->intf->maintenance_mode;
1533 	spin_unlock_irqrestore(&user->intf->maintenance_mode_lock, flags);
1534 	release_ipmi_user(user, index);
1535 
1536 	return mode;
1537 }
1538 EXPORT_SYMBOL(ipmi_get_maintenance_mode);
1539 
maintenance_mode_update(struct ipmi_smi * intf)1540 static void maintenance_mode_update(struct ipmi_smi *intf)
1541 {
1542 	if (intf->handlers->set_maintenance_mode)
1543 		intf->handlers->set_maintenance_mode(
1544 			intf->send_info, intf->maintenance_mode_enable);
1545 }
1546 
ipmi_set_maintenance_mode(struct ipmi_user * user,int mode)1547 int ipmi_set_maintenance_mode(struct ipmi_user *user, int mode)
1548 {
1549 	int rv = 0, index;
1550 	unsigned long flags;
1551 	struct ipmi_smi *intf = user->intf;
1552 
1553 	user = acquire_ipmi_user(user, &index);
1554 	if (!user)
1555 		return -ENODEV;
1556 
1557 	spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
1558 	if (intf->maintenance_mode != mode) {
1559 		switch (mode) {
1560 		case IPMI_MAINTENANCE_MODE_AUTO:
1561 			intf->maintenance_mode_enable
1562 				= (intf->auto_maintenance_timeout > 0);
1563 			break;
1564 
1565 		case IPMI_MAINTENANCE_MODE_OFF:
1566 			intf->maintenance_mode_enable = false;
1567 			break;
1568 
1569 		case IPMI_MAINTENANCE_MODE_ON:
1570 			intf->maintenance_mode_enable = true;
1571 			break;
1572 
1573 		default:
1574 			rv = -EINVAL;
1575 			goto out_unlock;
1576 		}
1577 		intf->maintenance_mode = mode;
1578 
1579 		maintenance_mode_update(intf);
1580 	}
1581  out_unlock:
1582 	spin_unlock_irqrestore(&intf->maintenance_mode_lock, flags);
1583 	release_ipmi_user(user, index);
1584 
1585 	return rv;
1586 }
1587 EXPORT_SYMBOL(ipmi_set_maintenance_mode);
1588 
ipmi_set_gets_events(struct ipmi_user * user,bool val)1589 int ipmi_set_gets_events(struct ipmi_user *user, bool val)
1590 {
1591 	unsigned long        flags;
1592 	struct ipmi_smi      *intf = user->intf;
1593 	struct ipmi_recv_msg *msg, *msg2;
1594 	struct list_head     msgs;
1595 	int index;
1596 
1597 	user = acquire_ipmi_user(user, &index);
1598 	if (!user)
1599 		return -ENODEV;
1600 
1601 	INIT_LIST_HEAD(&msgs);
1602 
1603 	spin_lock_irqsave(&intf->events_lock, flags);
1604 	if (user->gets_events == val)
1605 		goto out;
1606 
1607 	user->gets_events = val;
1608 
1609 	if (val) {
1610 		if (atomic_inc_return(&intf->event_waiters) == 1)
1611 			need_waiter(intf);
1612 	} else {
1613 		atomic_dec(&intf->event_waiters);
1614 	}
1615 
1616 	if (intf->delivering_events)
1617 		/*
1618 		 * Another thread is delivering events for this, so
1619 		 * let it handle any new events.
1620 		 */
1621 		goto out;
1622 
1623 	/* Deliver any queued events. */
1624 	while (user->gets_events && !list_empty(&intf->waiting_events)) {
1625 		list_for_each_entry_safe(msg, msg2, &intf->waiting_events, link)
1626 			list_move_tail(&msg->link, &msgs);
1627 		intf->waiting_events_count = 0;
1628 		if (intf->event_msg_printed) {
1629 			dev_warn(intf->si_dev, "Event queue no longer full\n");
1630 			intf->event_msg_printed = 0;
1631 		}
1632 
1633 		intf->delivering_events = 1;
1634 		spin_unlock_irqrestore(&intf->events_lock, flags);
1635 
1636 		list_for_each_entry_safe(msg, msg2, &msgs, link) {
1637 			msg->user = user;
1638 			kref_get(&user->refcount);
1639 			deliver_local_response(intf, msg);
1640 		}
1641 
1642 		spin_lock_irqsave(&intf->events_lock, flags);
1643 		intf->delivering_events = 0;
1644 	}
1645 
1646  out:
1647 	spin_unlock_irqrestore(&intf->events_lock, flags);
1648 	release_ipmi_user(user, index);
1649 
1650 	return 0;
1651 }
1652 EXPORT_SYMBOL(ipmi_set_gets_events);
1653 
find_cmd_rcvr(struct ipmi_smi * intf,unsigned char netfn,unsigned char cmd,unsigned char chan)1654 static struct cmd_rcvr *find_cmd_rcvr(struct ipmi_smi *intf,
1655 				      unsigned char netfn,
1656 				      unsigned char cmd,
1657 				      unsigned char chan)
1658 {
1659 	struct cmd_rcvr *rcvr;
1660 
1661 	list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link,
1662 				lockdep_is_held(&intf->cmd_rcvrs_mutex)) {
1663 		if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)
1664 					&& (rcvr->chans & (1 << chan)))
1665 			return rcvr;
1666 	}
1667 	return NULL;
1668 }
1669 
is_cmd_rcvr_exclusive(struct ipmi_smi * intf,unsigned char netfn,unsigned char cmd,unsigned int chans)1670 static int is_cmd_rcvr_exclusive(struct ipmi_smi *intf,
1671 				 unsigned char netfn,
1672 				 unsigned char cmd,
1673 				 unsigned int  chans)
1674 {
1675 	struct cmd_rcvr *rcvr;
1676 
1677 	list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link,
1678 				lockdep_is_held(&intf->cmd_rcvrs_mutex)) {
1679 		if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)
1680 					&& (rcvr->chans & chans))
1681 			return 0;
1682 	}
1683 	return 1;
1684 }
1685 
ipmi_register_for_cmd(struct ipmi_user * user,unsigned char netfn,unsigned char cmd,unsigned int chans)1686 int ipmi_register_for_cmd(struct ipmi_user *user,
1687 			  unsigned char netfn,
1688 			  unsigned char cmd,
1689 			  unsigned int  chans)
1690 {
1691 	struct ipmi_smi *intf = user->intf;
1692 	struct cmd_rcvr *rcvr;
1693 	int rv = 0, index;
1694 
1695 	user = acquire_ipmi_user(user, &index);
1696 	if (!user)
1697 		return -ENODEV;
1698 
1699 	rcvr = kmalloc(sizeof(*rcvr), GFP_KERNEL);
1700 	if (!rcvr) {
1701 		rv = -ENOMEM;
1702 		goto out_release;
1703 	}
1704 	rcvr->cmd = cmd;
1705 	rcvr->netfn = netfn;
1706 	rcvr->chans = chans;
1707 	rcvr->user = user;
1708 
1709 	mutex_lock(&intf->cmd_rcvrs_mutex);
1710 	/* Make sure the command/netfn is not already registered. */
1711 	if (!is_cmd_rcvr_exclusive(intf, netfn, cmd, chans)) {
1712 		rv = -EBUSY;
1713 		goto out_unlock;
1714 	}
1715 
1716 	smi_add_watch(intf, IPMI_WATCH_MASK_CHECK_COMMANDS);
1717 
1718 	list_add_rcu(&rcvr->link, &intf->cmd_rcvrs);
1719 
1720 out_unlock:
1721 	mutex_unlock(&intf->cmd_rcvrs_mutex);
1722 	if (rv)
1723 		kfree(rcvr);
1724 out_release:
1725 	release_ipmi_user(user, index);
1726 
1727 	return rv;
1728 }
1729 EXPORT_SYMBOL(ipmi_register_for_cmd);
1730 
ipmi_unregister_for_cmd(struct ipmi_user * user,unsigned char netfn,unsigned char cmd,unsigned int chans)1731 int ipmi_unregister_for_cmd(struct ipmi_user *user,
1732 			    unsigned char netfn,
1733 			    unsigned char cmd,
1734 			    unsigned int  chans)
1735 {
1736 	struct ipmi_smi *intf = user->intf;
1737 	struct cmd_rcvr *rcvr;
1738 	struct cmd_rcvr *rcvrs = NULL;
1739 	int i, rv = -ENOENT, index;
1740 
1741 	user = acquire_ipmi_user(user, &index);
1742 	if (!user)
1743 		return -ENODEV;
1744 
1745 	mutex_lock(&intf->cmd_rcvrs_mutex);
1746 	for (i = 0; i < IPMI_NUM_CHANNELS; i++) {
1747 		if (((1 << i) & chans) == 0)
1748 			continue;
1749 		rcvr = find_cmd_rcvr(intf, netfn, cmd, i);
1750 		if (rcvr == NULL)
1751 			continue;
1752 		if (rcvr->user == user) {
1753 			rv = 0;
1754 			rcvr->chans &= ~chans;
1755 			if (rcvr->chans == 0) {
1756 				list_del_rcu(&rcvr->link);
1757 				rcvr->next = rcvrs;
1758 				rcvrs = rcvr;
1759 			}
1760 		}
1761 	}
1762 	mutex_unlock(&intf->cmd_rcvrs_mutex);
1763 	synchronize_rcu();
1764 	release_ipmi_user(user, index);
1765 	while (rcvrs) {
1766 		smi_remove_watch(intf, IPMI_WATCH_MASK_CHECK_COMMANDS);
1767 		rcvr = rcvrs;
1768 		rcvrs = rcvr->next;
1769 		kfree(rcvr);
1770 	}
1771 
1772 	return rv;
1773 }
1774 EXPORT_SYMBOL(ipmi_unregister_for_cmd);
1775 
1776 unsigned char
ipmb_checksum(unsigned char * data,int size)1777 ipmb_checksum(unsigned char *data, int size)
1778 {
1779 	unsigned char csum = 0;
1780 
1781 	for (; size > 0; size--, data++)
1782 		csum += *data;
1783 
1784 	return -csum;
1785 }
1786 EXPORT_SYMBOL(ipmb_checksum);
1787 
format_ipmb_msg(struct ipmi_smi_msg * smi_msg,struct kernel_ipmi_msg * msg,struct ipmi_ipmb_addr * ipmb_addr,long msgid,unsigned char ipmb_seq,int broadcast,unsigned char source_address,unsigned char source_lun)1788 static inline void format_ipmb_msg(struct ipmi_smi_msg   *smi_msg,
1789 				   struct kernel_ipmi_msg *msg,
1790 				   struct ipmi_ipmb_addr *ipmb_addr,
1791 				   long                  msgid,
1792 				   unsigned char         ipmb_seq,
1793 				   int                   broadcast,
1794 				   unsigned char         source_address,
1795 				   unsigned char         source_lun)
1796 {
1797 	int i = broadcast;
1798 
1799 	/* Format the IPMB header data. */
1800 	smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
1801 	smi_msg->data[1] = IPMI_SEND_MSG_CMD;
1802 	smi_msg->data[2] = ipmb_addr->channel;
1803 	if (broadcast)
1804 		smi_msg->data[3] = 0;
1805 	smi_msg->data[i+3] = ipmb_addr->slave_addr;
1806 	smi_msg->data[i+4] = (msg->netfn << 2) | (ipmb_addr->lun & 0x3);
1807 	smi_msg->data[i+5] = ipmb_checksum(&smi_msg->data[i + 3], 2);
1808 	smi_msg->data[i+6] = source_address;
1809 	smi_msg->data[i+7] = (ipmb_seq << 2) | source_lun;
1810 	smi_msg->data[i+8] = msg->cmd;
1811 
1812 	/* Now tack on the data to the message. */
1813 	if (msg->data_len > 0)
1814 		memcpy(&smi_msg->data[i + 9], msg->data, msg->data_len);
1815 	smi_msg->data_size = msg->data_len + 9;
1816 
1817 	/* Now calculate the checksum and tack it on. */
1818 	smi_msg->data[i+smi_msg->data_size]
1819 		= ipmb_checksum(&smi_msg->data[i + 6], smi_msg->data_size - 6);
1820 
1821 	/*
1822 	 * Add on the checksum size and the offset from the
1823 	 * broadcast.
1824 	 */
1825 	smi_msg->data_size += 1 + i;
1826 
1827 	smi_msg->msgid = msgid;
1828 }
1829 
format_lan_msg(struct ipmi_smi_msg * smi_msg,struct kernel_ipmi_msg * msg,struct ipmi_lan_addr * lan_addr,long msgid,unsigned char ipmb_seq,unsigned char source_lun)1830 static inline void format_lan_msg(struct ipmi_smi_msg   *smi_msg,
1831 				  struct kernel_ipmi_msg *msg,
1832 				  struct ipmi_lan_addr  *lan_addr,
1833 				  long                  msgid,
1834 				  unsigned char         ipmb_seq,
1835 				  unsigned char         source_lun)
1836 {
1837 	/* Format the IPMB header data. */
1838 	smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
1839 	smi_msg->data[1] = IPMI_SEND_MSG_CMD;
1840 	smi_msg->data[2] = lan_addr->channel;
1841 	smi_msg->data[3] = lan_addr->session_handle;
1842 	smi_msg->data[4] = lan_addr->remote_SWID;
1843 	smi_msg->data[5] = (msg->netfn << 2) | (lan_addr->lun & 0x3);
1844 	smi_msg->data[6] = ipmb_checksum(&smi_msg->data[4], 2);
1845 	smi_msg->data[7] = lan_addr->local_SWID;
1846 	smi_msg->data[8] = (ipmb_seq << 2) | source_lun;
1847 	smi_msg->data[9] = msg->cmd;
1848 
1849 	/* Now tack on the data to the message. */
1850 	if (msg->data_len > 0)
1851 		memcpy(&smi_msg->data[10], msg->data, msg->data_len);
1852 	smi_msg->data_size = msg->data_len + 10;
1853 
1854 	/* Now calculate the checksum and tack it on. */
1855 	smi_msg->data[smi_msg->data_size]
1856 		= ipmb_checksum(&smi_msg->data[7], smi_msg->data_size - 7);
1857 
1858 	/*
1859 	 * Add on the checksum size and the offset from the
1860 	 * broadcast.
1861 	 */
1862 	smi_msg->data_size += 1;
1863 
1864 	smi_msg->msgid = msgid;
1865 }
1866 
smi_add_send_msg(struct ipmi_smi * intf,struct ipmi_smi_msg * smi_msg,int priority)1867 static struct ipmi_smi_msg *smi_add_send_msg(struct ipmi_smi *intf,
1868 					     struct ipmi_smi_msg *smi_msg,
1869 					     int priority)
1870 {
1871 	if (intf->curr_msg) {
1872 		if (priority > 0)
1873 			list_add_tail(&smi_msg->link, &intf->hp_xmit_msgs);
1874 		else
1875 			list_add_tail(&smi_msg->link, &intf->xmit_msgs);
1876 		smi_msg = NULL;
1877 	} else {
1878 		intf->curr_msg = smi_msg;
1879 	}
1880 
1881 	return smi_msg;
1882 }
1883 
smi_send(struct ipmi_smi * intf,const struct ipmi_smi_handlers * handlers,struct ipmi_smi_msg * smi_msg,int priority)1884 static void smi_send(struct ipmi_smi *intf,
1885 		     const struct ipmi_smi_handlers *handlers,
1886 		     struct ipmi_smi_msg *smi_msg, int priority)
1887 {
1888 	int run_to_completion = intf->run_to_completion;
1889 	unsigned long flags = 0;
1890 
1891 	if (!run_to_completion)
1892 		spin_lock_irqsave(&intf->xmit_msgs_lock, flags);
1893 	smi_msg = smi_add_send_msg(intf, smi_msg, priority);
1894 
1895 	if (!run_to_completion)
1896 		spin_unlock_irqrestore(&intf->xmit_msgs_lock, flags);
1897 
1898 	if (smi_msg)
1899 		handlers->sender(intf->send_info, smi_msg);
1900 }
1901 
is_maintenance_mode_cmd(struct kernel_ipmi_msg * msg)1902 static bool is_maintenance_mode_cmd(struct kernel_ipmi_msg *msg)
1903 {
1904 	return (((msg->netfn == IPMI_NETFN_APP_REQUEST)
1905 		 && ((msg->cmd == IPMI_COLD_RESET_CMD)
1906 		     || (msg->cmd == IPMI_WARM_RESET_CMD)))
1907 		|| (msg->netfn == IPMI_NETFN_FIRMWARE_REQUEST));
1908 }
1909 
i_ipmi_req_sysintf(struct ipmi_smi * intf,struct ipmi_addr * addr,long msgid,struct kernel_ipmi_msg * msg,struct ipmi_smi_msg * smi_msg,struct ipmi_recv_msg * recv_msg,int retries,unsigned int retry_time_ms)1910 static int i_ipmi_req_sysintf(struct ipmi_smi        *intf,
1911 			      struct ipmi_addr       *addr,
1912 			      long                   msgid,
1913 			      struct kernel_ipmi_msg *msg,
1914 			      struct ipmi_smi_msg    *smi_msg,
1915 			      struct ipmi_recv_msg   *recv_msg,
1916 			      int                    retries,
1917 			      unsigned int           retry_time_ms)
1918 {
1919 	struct ipmi_system_interface_addr *smi_addr;
1920 
1921 	if (msg->netfn & 1)
1922 		/* Responses are not allowed to the SMI. */
1923 		return -EINVAL;
1924 
1925 	smi_addr = (struct ipmi_system_interface_addr *) addr;
1926 	if (smi_addr->lun > 3) {
1927 		ipmi_inc_stat(intf, sent_invalid_commands);
1928 		return -EINVAL;
1929 	}
1930 
1931 	memcpy(&recv_msg->addr, smi_addr, sizeof(*smi_addr));
1932 
1933 	if ((msg->netfn == IPMI_NETFN_APP_REQUEST)
1934 	    && ((msg->cmd == IPMI_SEND_MSG_CMD)
1935 		|| (msg->cmd == IPMI_GET_MSG_CMD)
1936 		|| (msg->cmd == IPMI_READ_EVENT_MSG_BUFFER_CMD))) {
1937 		/*
1938 		 * We don't let the user do these, since we manage
1939 		 * the sequence numbers.
1940 		 */
1941 		ipmi_inc_stat(intf, sent_invalid_commands);
1942 		return -EINVAL;
1943 	}
1944 
1945 	if (is_maintenance_mode_cmd(msg)) {
1946 		unsigned long flags;
1947 
1948 		spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
1949 		intf->auto_maintenance_timeout
1950 			= maintenance_mode_timeout_ms;
1951 		if (!intf->maintenance_mode
1952 		    && !intf->maintenance_mode_enable) {
1953 			intf->maintenance_mode_enable = true;
1954 			maintenance_mode_update(intf);
1955 		}
1956 		spin_unlock_irqrestore(&intf->maintenance_mode_lock,
1957 				       flags);
1958 	}
1959 
1960 	if (msg->data_len + 2 > IPMI_MAX_MSG_LENGTH) {
1961 		ipmi_inc_stat(intf, sent_invalid_commands);
1962 		return -EMSGSIZE;
1963 	}
1964 
1965 	smi_msg->data[0] = (msg->netfn << 2) | (smi_addr->lun & 0x3);
1966 	smi_msg->data[1] = msg->cmd;
1967 	smi_msg->msgid = msgid;
1968 	smi_msg->user_data = recv_msg;
1969 	if (msg->data_len > 0)
1970 		memcpy(&smi_msg->data[2], msg->data, msg->data_len);
1971 	smi_msg->data_size = msg->data_len + 2;
1972 	ipmi_inc_stat(intf, sent_local_commands);
1973 
1974 	return 0;
1975 }
1976 
i_ipmi_req_ipmb(struct ipmi_smi * intf,struct ipmi_addr * addr,long msgid,struct kernel_ipmi_msg * msg,struct ipmi_smi_msg * smi_msg,struct ipmi_recv_msg * recv_msg,unsigned char source_address,unsigned char source_lun,int retries,unsigned int retry_time_ms)1977 static int i_ipmi_req_ipmb(struct ipmi_smi        *intf,
1978 			   struct ipmi_addr       *addr,
1979 			   long                   msgid,
1980 			   struct kernel_ipmi_msg *msg,
1981 			   struct ipmi_smi_msg    *smi_msg,
1982 			   struct ipmi_recv_msg   *recv_msg,
1983 			   unsigned char          source_address,
1984 			   unsigned char          source_lun,
1985 			   int                    retries,
1986 			   unsigned int           retry_time_ms)
1987 {
1988 	struct ipmi_ipmb_addr *ipmb_addr;
1989 	unsigned char ipmb_seq;
1990 	long seqid;
1991 	int broadcast = 0;
1992 	struct ipmi_channel *chans;
1993 	int rv = 0;
1994 
1995 	if (addr->channel >= IPMI_MAX_CHANNELS) {
1996 		ipmi_inc_stat(intf, sent_invalid_commands);
1997 		return -EINVAL;
1998 	}
1999 
2000 	chans = READ_ONCE(intf->channel_list)->c;
2001 
2002 	if (chans[addr->channel].medium != IPMI_CHANNEL_MEDIUM_IPMB) {
2003 		ipmi_inc_stat(intf, sent_invalid_commands);
2004 		return -EINVAL;
2005 	}
2006 
2007 	if (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE) {
2008 		/*
2009 		 * Broadcasts add a zero at the beginning of the
2010 		 * message, but otherwise is the same as an IPMB
2011 		 * address.
2012 		 */
2013 		addr->addr_type = IPMI_IPMB_ADDR_TYPE;
2014 		broadcast = 1;
2015 		retries = 0; /* Don't retry broadcasts. */
2016 	}
2017 
2018 	/*
2019 	 * 9 for the header and 1 for the checksum, plus
2020 	 * possibly one for the broadcast.
2021 	 */
2022 	if ((msg->data_len + 10 + broadcast) > IPMI_MAX_MSG_LENGTH) {
2023 		ipmi_inc_stat(intf, sent_invalid_commands);
2024 		return -EMSGSIZE;
2025 	}
2026 
2027 	ipmb_addr = (struct ipmi_ipmb_addr *) addr;
2028 	if (ipmb_addr->lun > 3) {
2029 		ipmi_inc_stat(intf, sent_invalid_commands);
2030 		return -EINVAL;
2031 	}
2032 
2033 	memcpy(&recv_msg->addr, ipmb_addr, sizeof(*ipmb_addr));
2034 
2035 	if (recv_msg->msg.netfn & 0x1) {
2036 		/*
2037 		 * It's a response, so use the user's sequence
2038 		 * from msgid.
2039 		 */
2040 		ipmi_inc_stat(intf, sent_ipmb_responses);
2041 		format_ipmb_msg(smi_msg, msg, ipmb_addr, msgid,
2042 				msgid, broadcast,
2043 				source_address, source_lun);
2044 
2045 		/*
2046 		 * Save the receive message so we can use it
2047 		 * to deliver the response.
2048 		 */
2049 		smi_msg->user_data = recv_msg;
2050 	} else {
2051 		/* It's a command, so get a sequence for it. */
2052 		unsigned long flags;
2053 
2054 		spin_lock_irqsave(&intf->seq_lock, flags);
2055 
2056 		if (is_maintenance_mode_cmd(msg))
2057 			intf->ipmb_maintenance_mode_timeout =
2058 				maintenance_mode_timeout_ms;
2059 
2060 		if (intf->ipmb_maintenance_mode_timeout && retry_time_ms == 0)
2061 			/* Different default in maintenance mode */
2062 			retry_time_ms = default_maintenance_retry_ms;
2063 
2064 		/*
2065 		 * Create a sequence number with a 1 second
2066 		 * timeout and 4 retries.
2067 		 */
2068 		rv = intf_next_seq(intf,
2069 				   recv_msg,
2070 				   retry_time_ms,
2071 				   retries,
2072 				   broadcast,
2073 				   &ipmb_seq,
2074 				   &seqid);
2075 		if (rv)
2076 			/*
2077 			 * We have used up all the sequence numbers,
2078 			 * probably, so abort.
2079 			 */
2080 			goto out_err;
2081 
2082 		ipmi_inc_stat(intf, sent_ipmb_commands);
2083 
2084 		/*
2085 		 * Store the sequence number in the message,
2086 		 * so that when the send message response
2087 		 * comes back we can start the timer.
2088 		 */
2089 		format_ipmb_msg(smi_msg, msg, ipmb_addr,
2090 				STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
2091 				ipmb_seq, broadcast,
2092 				source_address, source_lun);
2093 
2094 		/*
2095 		 * Copy the message into the recv message data, so we
2096 		 * can retransmit it later if necessary.
2097 		 */
2098 		memcpy(recv_msg->msg_data, smi_msg->data,
2099 		       smi_msg->data_size);
2100 		recv_msg->msg.data = recv_msg->msg_data;
2101 		recv_msg->msg.data_len = smi_msg->data_size;
2102 
2103 		/*
2104 		 * We don't unlock until here, because we need
2105 		 * to copy the completed message into the
2106 		 * recv_msg before we release the lock.
2107 		 * Otherwise, race conditions may bite us.  I
2108 		 * know that's pretty paranoid, but I prefer
2109 		 * to be correct.
2110 		 */
2111 out_err:
2112 		spin_unlock_irqrestore(&intf->seq_lock, flags);
2113 	}
2114 
2115 	return rv;
2116 }
2117 
i_ipmi_req_ipmb_direct(struct ipmi_smi * intf,struct ipmi_addr * addr,long msgid,struct kernel_ipmi_msg * msg,struct ipmi_smi_msg * smi_msg,struct ipmi_recv_msg * recv_msg,unsigned char source_lun)2118 static int i_ipmi_req_ipmb_direct(struct ipmi_smi        *intf,
2119 				  struct ipmi_addr       *addr,
2120 				  long			 msgid,
2121 				  struct kernel_ipmi_msg *msg,
2122 				  struct ipmi_smi_msg    *smi_msg,
2123 				  struct ipmi_recv_msg   *recv_msg,
2124 				  unsigned char          source_lun)
2125 {
2126 	struct ipmi_ipmb_direct_addr *daddr;
2127 	bool is_cmd = !(recv_msg->msg.netfn & 0x1);
2128 
2129 	if (!(intf->handlers->flags & IPMI_SMI_CAN_HANDLE_IPMB_DIRECT))
2130 		return -EAFNOSUPPORT;
2131 
2132 	/* Responses must have a completion code. */
2133 	if (!is_cmd && msg->data_len < 1) {
2134 		ipmi_inc_stat(intf, sent_invalid_commands);
2135 		return -EINVAL;
2136 	}
2137 
2138 	if ((msg->data_len + 4) > IPMI_MAX_MSG_LENGTH) {
2139 		ipmi_inc_stat(intf, sent_invalid_commands);
2140 		return -EMSGSIZE;
2141 	}
2142 
2143 	daddr = (struct ipmi_ipmb_direct_addr *) addr;
2144 	if (daddr->rq_lun > 3 || daddr->rs_lun > 3) {
2145 		ipmi_inc_stat(intf, sent_invalid_commands);
2146 		return -EINVAL;
2147 	}
2148 
2149 	smi_msg->type = IPMI_SMI_MSG_TYPE_IPMB_DIRECT;
2150 	smi_msg->msgid = msgid;
2151 
2152 	if (is_cmd) {
2153 		smi_msg->data[0] = msg->netfn << 2 | daddr->rs_lun;
2154 		smi_msg->data[2] = recv_msg->msgid << 2 | daddr->rq_lun;
2155 	} else {
2156 		smi_msg->data[0] = msg->netfn << 2 | daddr->rq_lun;
2157 		smi_msg->data[2] = recv_msg->msgid << 2 | daddr->rs_lun;
2158 	}
2159 	smi_msg->data[1] = daddr->slave_addr;
2160 	smi_msg->data[3] = msg->cmd;
2161 
2162 	memcpy(smi_msg->data + 4, msg->data, msg->data_len);
2163 	smi_msg->data_size = msg->data_len + 4;
2164 
2165 	smi_msg->user_data = recv_msg;
2166 
2167 	return 0;
2168 }
2169 
i_ipmi_req_lan(struct ipmi_smi * intf,struct ipmi_addr * addr,long msgid,struct kernel_ipmi_msg * msg,struct ipmi_smi_msg * smi_msg,struct ipmi_recv_msg * recv_msg,unsigned char source_lun,int retries,unsigned int retry_time_ms)2170 static int i_ipmi_req_lan(struct ipmi_smi        *intf,
2171 			  struct ipmi_addr       *addr,
2172 			  long                   msgid,
2173 			  struct kernel_ipmi_msg *msg,
2174 			  struct ipmi_smi_msg    *smi_msg,
2175 			  struct ipmi_recv_msg   *recv_msg,
2176 			  unsigned char          source_lun,
2177 			  int                    retries,
2178 			  unsigned int           retry_time_ms)
2179 {
2180 	struct ipmi_lan_addr  *lan_addr;
2181 	unsigned char ipmb_seq;
2182 	long seqid;
2183 	struct ipmi_channel *chans;
2184 	int rv = 0;
2185 
2186 	if (addr->channel >= IPMI_MAX_CHANNELS) {
2187 		ipmi_inc_stat(intf, sent_invalid_commands);
2188 		return -EINVAL;
2189 	}
2190 
2191 	chans = READ_ONCE(intf->channel_list)->c;
2192 
2193 	if ((chans[addr->channel].medium
2194 				!= IPMI_CHANNEL_MEDIUM_8023LAN)
2195 			&& (chans[addr->channel].medium
2196 			    != IPMI_CHANNEL_MEDIUM_ASYNC)) {
2197 		ipmi_inc_stat(intf, sent_invalid_commands);
2198 		return -EINVAL;
2199 	}
2200 
2201 	/* 11 for the header and 1 for the checksum. */
2202 	if ((msg->data_len + 12) > IPMI_MAX_MSG_LENGTH) {
2203 		ipmi_inc_stat(intf, sent_invalid_commands);
2204 		return -EMSGSIZE;
2205 	}
2206 
2207 	lan_addr = (struct ipmi_lan_addr *) addr;
2208 	if (lan_addr->lun > 3) {
2209 		ipmi_inc_stat(intf, sent_invalid_commands);
2210 		return -EINVAL;
2211 	}
2212 
2213 	memcpy(&recv_msg->addr, lan_addr, sizeof(*lan_addr));
2214 
2215 	if (recv_msg->msg.netfn & 0x1) {
2216 		/*
2217 		 * It's a response, so use the user's sequence
2218 		 * from msgid.
2219 		 */
2220 		ipmi_inc_stat(intf, sent_lan_responses);
2221 		format_lan_msg(smi_msg, msg, lan_addr, msgid,
2222 			       msgid, source_lun);
2223 
2224 		/*
2225 		 * Save the receive message so we can use it
2226 		 * to deliver the response.
2227 		 */
2228 		smi_msg->user_data = recv_msg;
2229 	} else {
2230 		/* It's a command, so get a sequence for it. */
2231 		unsigned long flags;
2232 
2233 		spin_lock_irqsave(&intf->seq_lock, flags);
2234 
2235 		/*
2236 		 * Create a sequence number with a 1 second
2237 		 * timeout and 4 retries.
2238 		 */
2239 		rv = intf_next_seq(intf,
2240 				   recv_msg,
2241 				   retry_time_ms,
2242 				   retries,
2243 				   0,
2244 				   &ipmb_seq,
2245 				   &seqid);
2246 		if (rv)
2247 			/*
2248 			 * We have used up all the sequence numbers,
2249 			 * probably, so abort.
2250 			 */
2251 			goto out_err;
2252 
2253 		ipmi_inc_stat(intf, sent_lan_commands);
2254 
2255 		/*
2256 		 * Store the sequence number in the message,
2257 		 * so that when the send message response
2258 		 * comes back we can start the timer.
2259 		 */
2260 		format_lan_msg(smi_msg, msg, lan_addr,
2261 			       STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
2262 			       ipmb_seq, source_lun);
2263 
2264 		/*
2265 		 * Copy the message into the recv message data, so we
2266 		 * can retransmit it later if necessary.
2267 		 */
2268 		memcpy(recv_msg->msg_data, smi_msg->data,
2269 		       smi_msg->data_size);
2270 		recv_msg->msg.data = recv_msg->msg_data;
2271 		recv_msg->msg.data_len = smi_msg->data_size;
2272 
2273 		/*
2274 		 * We don't unlock until here, because we need
2275 		 * to copy the completed message into the
2276 		 * recv_msg before we release the lock.
2277 		 * Otherwise, race conditions may bite us.  I
2278 		 * know that's pretty paranoid, but I prefer
2279 		 * to be correct.
2280 		 */
2281 out_err:
2282 		spin_unlock_irqrestore(&intf->seq_lock, flags);
2283 	}
2284 
2285 	return rv;
2286 }
2287 
2288 /*
2289  * Separate from ipmi_request so that the user does not have to be
2290  * supplied in certain circumstances (mainly at panic time).  If
2291  * messages are supplied, they will be freed, even if an error
2292  * occurs.
2293  */
i_ipmi_request(struct ipmi_user * user,struct ipmi_smi * intf,struct ipmi_addr * addr,long msgid,struct kernel_ipmi_msg * msg,void * user_msg_data,void * supplied_smi,struct ipmi_recv_msg * supplied_recv,int priority,unsigned char source_address,unsigned char source_lun,int retries,unsigned int retry_time_ms)2294 static int i_ipmi_request(struct ipmi_user     *user,
2295 			  struct ipmi_smi      *intf,
2296 			  struct ipmi_addr     *addr,
2297 			  long                 msgid,
2298 			  struct kernel_ipmi_msg *msg,
2299 			  void                 *user_msg_data,
2300 			  void                 *supplied_smi,
2301 			  struct ipmi_recv_msg *supplied_recv,
2302 			  int                  priority,
2303 			  unsigned char        source_address,
2304 			  unsigned char        source_lun,
2305 			  int                  retries,
2306 			  unsigned int         retry_time_ms)
2307 {
2308 	struct ipmi_smi_msg *smi_msg;
2309 	struct ipmi_recv_msg *recv_msg;
2310 	int rv = 0;
2311 
2312 	if (user) {
2313 		if (atomic_add_return(1, &user->nr_msgs) > max_msgs_per_user) {
2314 			/* Decrement will happen at the end of the routine. */
2315 			rv = -EBUSY;
2316 			goto out;
2317 		}
2318 	}
2319 
2320 	if (supplied_recv)
2321 		recv_msg = supplied_recv;
2322 	else {
2323 		recv_msg = ipmi_alloc_recv_msg();
2324 		if (recv_msg == NULL) {
2325 			rv = -ENOMEM;
2326 			goto out;
2327 		}
2328 	}
2329 	recv_msg->user_msg_data = user_msg_data;
2330 
2331 	if (supplied_smi)
2332 		smi_msg = supplied_smi;
2333 	else {
2334 		smi_msg = ipmi_alloc_smi_msg();
2335 		if (smi_msg == NULL) {
2336 			if (!supplied_recv)
2337 				ipmi_free_recv_msg(recv_msg);
2338 			rv = -ENOMEM;
2339 			goto out;
2340 		}
2341 	}
2342 
2343 	rcu_read_lock();
2344 	if (intf->in_shutdown) {
2345 		rv = -ENODEV;
2346 		goto out_err;
2347 	}
2348 
2349 	recv_msg->user = user;
2350 	if (user)
2351 		/* The put happens when the message is freed. */
2352 		kref_get(&user->refcount);
2353 	recv_msg->msgid = msgid;
2354 	/*
2355 	 * Store the message to send in the receive message so timeout
2356 	 * responses can get the proper response data.
2357 	 */
2358 	recv_msg->msg = *msg;
2359 
2360 	if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
2361 		rv = i_ipmi_req_sysintf(intf, addr, msgid, msg, smi_msg,
2362 					recv_msg, retries, retry_time_ms);
2363 	} else if (is_ipmb_addr(addr) || is_ipmb_bcast_addr(addr)) {
2364 		rv = i_ipmi_req_ipmb(intf, addr, msgid, msg, smi_msg, recv_msg,
2365 				     source_address, source_lun,
2366 				     retries, retry_time_ms);
2367 	} else if (is_ipmb_direct_addr(addr)) {
2368 		rv = i_ipmi_req_ipmb_direct(intf, addr, msgid, msg, smi_msg,
2369 					    recv_msg, source_lun);
2370 	} else if (is_lan_addr(addr)) {
2371 		rv = i_ipmi_req_lan(intf, addr, msgid, msg, smi_msg, recv_msg,
2372 				    source_lun, retries, retry_time_ms);
2373 	} else {
2374 	    /* Unknown address type. */
2375 		ipmi_inc_stat(intf, sent_invalid_commands);
2376 		rv = -EINVAL;
2377 	}
2378 
2379 	if (rv) {
2380 out_err:
2381 		ipmi_free_smi_msg(smi_msg);
2382 		ipmi_free_recv_msg(recv_msg);
2383 	} else {
2384 		dev_dbg(intf->si_dev, "Send: %*ph\n",
2385 			smi_msg->data_size, smi_msg->data);
2386 
2387 		smi_send(intf, intf->handlers, smi_msg, priority);
2388 	}
2389 	rcu_read_unlock();
2390 
2391 out:
2392 	if (rv && user)
2393 		atomic_dec(&user->nr_msgs);
2394 	return rv;
2395 }
2396 
check_addr(struct ipmi_smi * intf,struct ipmi_addr * addr,unsigned char * saddr,unsigned char * lun)2397 static int check_addr(struct ipmi_smi  *intf,
2398 		      struct ipmi_addr *addr,
2399 		      unsigned char    *saddr,
2400 		      unsigned char    *lun)
2401 {
2402 	if (addr->channel >= IPMI_MAX_CHANNELS)
2403 		return -EINVAL;
2404 	addr->channel = array_index_nospec(addr->channel, IPMI_MAX_CHANNELS);
2405 	*lun = intf->addrinfo[addr->channel].lun;
2406 	*saddr = intf->addrinfo[addr->channel].address;
2407 	return 0;
2408 }
2409 
ipmi_request_settime(struct ipmi_user * user,struct ipmi_addr * addr,long msgid,struct kernel_ipmi_msg * msg,void * user_msg_data,int priority,int retries,unsigned int retry_time_ms)2410 int ipmi_request_settime(struct ipmi_user *user,
2411 			 struct ipmi_addr *addr,
2412 			 long             msgid,
2413 			 struct kernel_ipmi_msg  *msg,
2414 			 void             *user_msg_data,
2415 			 int              priority,
2416 			 int              retries,
2417 			 unsigned int     retry_time_ms)
2418 {
2419 	unsigned char saddr = 0, lun = 0;
2420 	int rv, index;
2421 
2422 	if (!user)
2423 		return -EINVAL;
2424 
2425 	user = acquire_ipmi_user(user, &index);
2426 	if (!user)
2427 		return -ENODEV;
2428 
2429 	rv = check_addr(user->intf, addr, &saddr, &lun);
2430 	if (!rv)
2431 		rv = i_ipmi_request(user,
2432 				    user->intf,
2433 				    addr,
2434 				    msgid,
2435 				    msg,
2436 				    user_msg_data,
2437 				    NULL, NULL,
2438 				    priority,
2439 				    saddr,
2440 				    lun,
2441 				    retries,
2442 				    retry_time_ms);
2443 
2444 	release_ipmi_user(user, index);
2445 	return rv;
2446 }
2447 EXPORT_SYMBOL(ipmi_request_settime);
2448 
ipmi_request_supply_msgs(struct ipmi_user * user,struct ipmi_addr * addr,long msgid,struct kernel_ipmi_msg * msg,void * user_msg_data,void * supplied_smi,struct ipmi_recv_msg * supplied_recv,int priority)2449 int ipmi_request_supply_msgs(struct ipmi_user     *user,
2450 			     struct ipmi_addr     *addr,
2451 			     long                 msgid,
2452 			     struct kernel_ipmi_msg *msg,
2453 			     void                 *user_msg_data,
2454 			     void                 *supplied_smi,
2455 			     struct ipmi_recv_msg *supplied_recv,
2456 			     int                  priority)
2457 {
2458 	unsigned char saddr = 0, lun = 0;
2459 	int rv, index;
2460 
2461 	if (!user)
2462 		return -EINVAL;
2463 
2464 	user = acquire_ipmi_user(user, &index);
2465 	if (!user)
2466 		return -ENODEV;
2467 
2468 	rv = check_addr(user->intf, addr, &saddr, &lun);
2469 	if (!rv)
2470 		rv = i_ipmi_request(user,
2471 				    user->intf,
2472 				    addr,
2473 				    msgid,
2474 				    msg,
2475 				    user_msg_data,
2476 				    supplied_smi,
2477 				    supplied_recv,
2478 				    priority,
2479 				    saddr,
2480 				    lun,
2481 				    -1, 0);
2482 
2483 	release_ipmi_user(user, index);
2484 	return rv;
2485 }
2486 EXPORT_SYMBOL(ipmi_request_supply_msgs);
2487 
bmc_device_id_handler(struct ipmi_smi * intf,struct ipmi_recv_msg * msg)2488 static void bmc_device_id_handler(struct ipmi_smi *intf,
2489 				  struct ipmi_recv_msg *msg)
2490 {
2491 	int rv;
2492 
2493 	if ((msg->addr.addr_type != IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
2494 			|| (msg->msg.netfn != IPMI_NETFN_APP_RESPONSE)
2495 			|| (msg->msg.cmd != IPMI_GET_DEVICE_ID_CMD)) {
2496 		dev_warn(intf->si_dev,
2497 			 "invalid device_id msg: addr_type=%d netfn=%x cmd=%x\n",
2498 			 msg->addr.addr_type, msg->msg.netfn, msg->msg.cmd);
2499 		return;
2500 	}
2501 
2502 	if (msg->msg.data[0]) {
2503 		dev_warn(intf->si_dev, "device id fetch failed: 0x%2.2x\n",
2504 			 msg->msg.data[0]);
2505 		intf->bmc->dyn_id_set = 0;
2506 		goto out;
2507 	}
2508 
2509 	rv = ipmi_demangle_device_id(msg->msg.netfn, msg->msg.cmd,
2510 			msg->msg.data, msg->msg.data_len, &intf->bmc->fetch_id);
2511 	if (rv) {
2512 		dev_warn(intf->si_dev, "device id demangle failed: %d\n", rv);
2513 		/* record completion code when error */
2514 		intf->bmc->cc = msg->msg.data[0];
2515 		intf->bmc->dyn_id_set = 0;
2516 	} else {
2517 		/*
2518 		 * Make sure the id data is available before setting
2519 		 * dyn_id_set.
2520 		 */
2521 		smp_wmb();
2522 		intf->bmc->dyn_id_set = 1;
2523 	}
2524 out:
2525 	wake_up(&intf->waitq);
2526 }
2527 
2528 static int
send_get_device_id_cmd(struct ipmi_smi * intf)2529 send_get_device_id_cmd(struct ipmi_smi *intf)
2530 {
2531 	struct ipmi_system_interface_addr si;
2532 	struct kernel_ipmi_msg msg;
2533 
2534 	si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2535 	si.channel = IPMI_BMC_CHANNEL;
2536 	si.lun = 0;
2537 
2538 	msg.netfn = IPMI_NETFN_APP_REQUEST;
2539 	msg.cmd = IPMI_GET_DEVICE_ID_CMD;
2540 	msg.data = NULL;
2541 	msg.data_len = 0;
2542 
2543 	return i_ipmi_request(NULL,
2544 			      intf,
2545 			      (struct ipmi_addr *) &si,
2546 			      0,
2547 			      &msg,
2548 			      intf,
2549 			      NULL,
2550 			      NULL,
2551 			      0,
2552 			      intf->addrinfo[0].address,
2553 			      intf->addrinfo[0].lun,
2554 			      -1, 0);
2555 }
2556 
__get_device_id(struct ipmi_smi * intf,struct bmc_device * bmc)2557 static int __get_device_id(struct ipmi_smi *intf, struct bmc_device *bmc)
2558 {
2559 	int rv;
2560 	unsigned int retry_count = 0;
2561 
2562 	intf->null_user_handler = bmc_device_id_handler;
2563 
2564 retry:
2565 	bmc->cc = 0;
2566 	bmc->dyn_id_set = 2;
2567 
2568 	rv = send_get_device_id_cmd(intf);
2569 	if (rv)
2570 		goto out_reset_handler;
2571 
2572 	wait_event(intf->waitq, bmc->dyn_id_set != 2);
2573 
2574 	if (!bmc->dyn_id_set) {
2575 		if (bmc->cc != IPMI_CC_NO_ERROR &&
2576 		    ++retry_count <= GET_DEVICE_ID_MAX_RETRY) {
2577 			msleep(500);
2578 			dev_warn(intf->si_dev,
2579 			    "BMC returned 0x%2.2x, retry get bmc device id\n",
2580 			    bmc->cc);
2581 			goto retry;
2582 		}
2583 
2584 		rv = -EIO; /* Something went wrong in the fetch. */
2585 	}
2586 
2587 	/* dyn_id_set makes the id data available. */
2588 	smp_rmb();
2589 
2590 out_reset_handler:
2591 	intf->null_user_handler = NULL;
2592 
2593 	return rv;
2594 }
2595 
2596 /*
2597  * Fetch the device id for the bmc/interface.  You must pass in either
2598  * bmc or intf, this code will get the other one.  If the data has
2599  * been recently fetched, this will just use the cached data.  Otherwise
2600  * it will run a new fetch.
2601  *
2602  * Except for the first time this is called (in ipmi_add_smi()),
2603  * this will always return good data;
2604  */
__bmc_get_device_id(struct ipmi_smi * intf,struct bmc_device * bmc,struct ipmi_device_id * id,bool * guid_set,guid_t * guid,int intf_num)2605 static int __bmc_get_device_id(struct ipmi_smi *intf, struct bmc_device *bmc,
2606 			       struct ipmi_device_id *id,
2607 			       bool *guid_set, guid_t *guid, int intf_num)
2608 {
2609 	int rv = 0;
2610 	int prev_dyn_id_set, prev_guid_set;
2611 	bool intf_set = intf != NULL;
2612 
2613 	if (!intf) {
2614 		mutex_lock(&bmc->dyn_mutex);
2615 retry_bmc_lock:
2616 		if (list_empty(&bmc->intfs)) {
2617 			mutex_unlock(&bmc->dyn_mutex);
2618 			return -ENOENT;
2619 		}
2620 		intf = list_first_entry(&bmc->intfs, struct ipmi_smi,
2621 					bmc_link);
2622 		kref_get(&intf->refcount);
2623 		mutex_unlock(&bmc->dyn_mutex);
2624 		mutex_lock(&intf->bmc_reg_mutex);
2625 		mutex_lock(&bmc->dyn_mutex);
2626 		if (intf != list_first_entry(&bmc->intfs, struct ipmi_smi,
2627 					     bmc_link)) {
2628 			mutex_unlock(&intf->bmc_reg_mutex);
2629 			kref_put(&intf->refcount, intf_free);
2630 			goto retry_bmc_lock;
2631 		}
2632 	} else {
2633 		mutex_lock(&intf->bmc_reg_mutex);
2634 		bmc = intf->bmc;
2635 		mutex_lock(&bmc->dyn_mutex);
2636 		kref_get(&intf->refcount);
2637 	}
2638 
2639 	/* If we have a valid and current ID, just return that. */
2640 	if (intf->in_bmc_register ||
2641 	    (bmc->dyn_id_set && time_is_after_jiffies(bmc->dyn_id_expiry)))
2642 		goto out_noprocessing;
2643 
2644 	prev_guid_set = bmc->dyn_guid_set;
2645 	__get_guid(intf);
2646 
2647 	prev_dyn_id_set = bmc->dyn_id_set;
2648 	rv = __get_device_id(intf, bmc);
2649 	if (rv)
2650 		goto out;
2651 
2652 	/*
2653 	 * The guid, device id, manufacturer id, and product id should
2654 	 * not change on a BMC.  If it does we have to do some dancing.
2655 	 */
2656 	if (!intf->bmc_registered
2657 	    || (!prev_guid_set && bmc->dyn_guid_set)
2658 	    || (!prev_dyn_id_set && bmc->dyn_id_set)
2659 	    || (prev_guid_set && bmc->dyn_guid_set
2660 		&& !guid_equal(&bmc->guid, &bmc->fetch_guid))
2661 	    || bmc->id.device_id != bmc->fetch_id.device_id
2662 	    || bmc->id.manufacturer_id != bmc->fetch_id.manufacturer_id
2663 	    || bmc->id.product_id != bmc->fetch_id.product_id) {
2664 		struct ipmi_device_id id = bmc->fetch_id;
2665 		int guid_set = bmc->dyn_guid_set;
2666 		guid_t guid;
2667 
2668 		guid = bmc->fetch_guid;
2669 		mutex_unlock(&bmc->dyn_mutex);
2670 
2671 		__ipmi_bmc_unregister(intf);
2672 		/* Fill in the temporary BMC for good measure. */
2673 		intf->bmc->id = id;
2674 		intf->bmc->dyn_guid_set = guid_set;
2675 		intf->bmc->guid = guid;
2676 		if (__ipmi_bmc_register(intf, &id, guid_set, &guid, intf_num))
2677 			need_waiter(intf); /* Retry later on an error. */
2678 		else
2679 			__scan_channels(intf, &id);
2680 
2681 
2682 		if (!intf_set) {
2683 			/*
2684 			 * We weren't given the interface on the
2685 			 * command line, so restart the operation on
2686 			 * the next interface for the BMC.
2687 			 */
2688 			mutex_unlock(&intf->bmc_reg_mutex);
2689 			mutex_lock(&bmc->dyn_mutex);
2690 			goto retry_bmc_lock;
2691 		}
2692 
2693 		/* We have a new BMC, set it up. */
2694 		bmc = intf->bmc;
2695 		mutex_lock(&bmc->dyn_mutex);
2696 		goto out_noprocessing;
2697 	} else if (memcmp(&bmc->fetch_id, &bmc->id, sizeof(bmc->id)))
2698 		/* Version info changes, scan the channels again. */
2699 		__scan_channels(intf, &bmc->fetch_id);
2700 
2701 	bmc->dyn_id_expiry = jiffies + IPMI_DYN_DEV_ID_EXPIRY;
2702 
2703 out:
2704 	if (rv && prev_dyn_id_set) {
2705 		rv = 0; /* Ignore failures if we have previous data. */
2706 		bmc->dyn_id_set = prev_dyn_id_set;
2707 	}
2708 	if (!rv) {
2709 		bmc->id = bmc->fetch_id;
2710 		if (bmc->dyn_guid_set)
2711 			bmc->guid = bmc->fetch_guid;
2712 		else if (prev_guid_set)
2713 			/*
2714 			 * The guid used to be valid and it failed to fetch,
2715 			 * just use the cached value.
2716 			 */
2717 			bmc->dyn_guid_set = prev_guid_set;
2718 	}
2719 out_noprocessing:
2720 	if (!rv) {
2721 		if (id)
2722 			*id = bmc->id;
2723 
2724 		if (guid_set)
2725 			*guid_set = bmc->dyn_guid_set;
2726 
2727 		if (guid && bmc->dyn_guid_set)
2728 			*guid =  bmc->guid;
2729 	}
2730 
2731 	mutex_unlock(&bmc->dyn_mutex);
2732 	mutex_unlock(&intf->bmc_reg_mutex);
2733 
2734 	kref_put(&intf->refcount, intf_free);
2735 	return rv;
2736 }
2737 
bmc_get_device_id(struct ipmi_smi * intf,struct bmc_device * bmc,struct ipmi_device_id * id,bool * guid_set,guid_t * guid)2738 static int bmc_get_device_id(struct ipmi_smi *intf, struct bmc_device *bmc,
2739 			     struct ipmi_device_id *id,
2740 			     bool *guid_set, guid_t *guid)
2741 {
2742 	return __bmc_get_device_id(intf, bmc, id, guid_set, guid, -1);
2743 }
2744 
device_id_show(struct device * dev,struct device_attribute * attr,char * buf)2745 static ssize_t device_id_show(struct device *dev,
2746 			      struct device_attribute *attr,
2747 			      char *buf)
2748 {
2749 	struct bmc_device *bmc = to_bmc_device(dev);
2750 	struct ipmi_device_id id;
2751 	int rv;
2752 
2753 	rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2754 	if (rv)
2755 		return rv;
2756 
2757 	return sysfs_emit(buf, "%u\n", id.device_id);
2758 }
2759 static DEVICE_ATTR_RO(device_id);
2760 
provides_device_sdrs_show(struct device * dev,struct device_attribute * attr,char * buf)2761 static ssize_t provides_device_sdrs_show(struct device *dev,
2762 					 struct device_attribute *attr,
2763 					 char *buf)
2764 {
2765 	struct bmc_device *bmc = to_bmc_device(dev);
2766 	struct ipmi_device_id id;
2767 	int rv;
2768 
2769 	rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2770 	if (rv)
2771 		return rv;
2772 
2773 	return sysfs_emit(buf, "%u\n", (id.device_revision & 0x80) >> 7);
2774 }
2775 static DEVICE_ATTR_RO(provides_device_sdrs);
2776 
revision_show(struct device * dev,struct device_attribute * attr,char * buf)2777 static ssize_t revision_show(struct device *dev, struct device_attribute *attr,
2778 			     char *buf)
2779 {
2780 	struct bmc_device *bmc = to_bmc_device(dev);
2781 	struct ipmi_device_id id;
2782 	int rv;
2783 
2784 	rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2785 	if (rv)
2786 		return rv;
2787 
2788 	return sysfs_emit(buf, "%u\n", id.device_revision & 0x0F);
2789 }
2790 static DEVICE_ATTR_RO(revision);
2791 
firmware_revision_show(struct device * dev,struct device_attribute * attr,char * buf)2792 static ssize_t firmware_revision_show(struct device *dev,
2793 				      struct device_attribute *attr,
2794 				      char *buf)
2795 {
2796 	struct bmc_device *bmc = to_bmc_device(dev);
2797 	struct ipmi_device_id id;
2798 	int rv;
2799 
2800 	rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2801 	if (rv)
2802 		return rv;
2803 
2804 	return sysfs_emit(buf, "%u.%x\n", id.firmware_revision_1,
2805 			id.firmware_revision_2);
2806 }
2807 static DEVICE_ATTR_RO(firmware_revision);
2808 
ipmi_version_show(struct device * dev,struct device_attribute * attr,char * buf)2809 static ssize_t ipmi_version_show(struct device *dev,
2810 				 struct device_attribute *attr,
2811 				 char *buf)
2812 {
2813 	struct bmc_device *bmc = to_bmc_device(dev);
2814 	struct ipmi_device_id id;
2815 	int rv;
2816 
2817 	rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2818 	if (rv)
2819 		return rv;
2820 
2821 	return sysfs_emit(buf, "%u.%u\n",
2822 			ipmi_version_major(&id),
2823 			ipmi_version_minor(&id));
2824 }
2825 static DEVICE_ATTR_RO(ipmi_version);
2826 
add_dev_support_show(struct device * dev,struct device_attribute * attr,char * buf)2827 static ssize_t add_dev_support_show(struct device *dev,
2828 				    struct device_attribute *attr,
2829 				    char *buf)
2830 {
2831 	struct bmc_device *bmc = to_bmc_device(dev);
2832 	struct ipmi_device_id id;
2833 	int rv;
2834 
2835 	rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2836 	if (rv)
2837 		return rv;
2838 
2839 	return sysfs_emit(buf, "0x%02x\n", id.additional_device_support);
2840 }
2841 static DEVICE_ATTR(additional_device_support, S_IRUGO, add_dev_support_show,
2842 		   NULL);
2843 
manufacturer_id_show(struct device * dev,struct device_attribute * attr,char * buf)2844 static ssize_t manufacturer_id_show(struct device *dev,
2845 				    struct device_attribute *attr,
2846 				    char *buf)
2847 {
2848 	struct bmc_device *bmc = to_bmc_device(dev);
2849 	struct ipmi_device_id id;
2850 	int rv;
2851 
2852 	rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2853 	if (rv)
2854 		return rv;
2855 
2856 	return sysfs_emit(buf, "0x%6.6x\n", id.manufacturer_id);
2857 }
2858 static DEVICE_ATTR_RO(manufacturer_id);
2859 
product_id_show(struct device * dev,struct device_attribute * attr,char * buf)2860 static ssize_t product_id_show(struct device *dev,
2861 			       struct device_attribute *attr,
2862 			       char *buf)
2863 {
2864 	struct bmc_device *bmc = to_bmc_device(dev);
2865 	struct ipmi_device_id id;
2866 	int rv;
2867 
2868 	rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2869 	if (rv)
2870 		return rv;
2871 
2872 	return sysfs_emit(buf, "0x%4.4x\n", id.product_id);
2873 }
2874 static DEVICE_ATTR_RO(product_id);
2875 
aux_firmware_rev_show(struct device * dev,struct device_attribute * attr,char * buf)2876 static ssize_t aux_firmware_rev_show(struct device *dev,
2877 				     struct device_attribute *attr,
2878 				     char *buf)
2879 {
2880 	struct bmc_device *bmc = to_bmc_device(dev);
2881 	struct ipmi_device_id id;
2882 	int rv;
2883 
2884 	rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2885 	if (rv)
2886 		return rv;
2887 
2888 	return sysfs_emit(buf, "0x%02x 0x%02x 0x%02x 0x%02x\n",
2889 			id.aux_firmware_revision[3],
2890 			id.aux_firmware_revision[2],
2891 			id.aux_firmware_revision[1],
2892 			id.aux_firmware_revision[0]);
2893 }
2894 static DEVICE_ATTR(aux_firmware_revision, S_IRUGO, aux_firmware_rev_show, NULL);
2895 
guid_show(struct device * dev,struct device_attribute * attr,char * buf)2896 static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
2897 			 char *buf)
2898 {
2899 	struct bmc_device *bmc = to_bmc_device(dev);
2900 	bool guid_set;
2901 	guid_t guid;
2902 	int rv;
2903 
2904 	rv = bmc_get_device_id(NULL, bmc, NULL, &guid_set, &guid);
2905 	if (rv)
2906 		return rv;
2907 	if (!guid_set)
2908 		return -ENOENT;
2909 
2910 	return sysfs_emit(buf, "%pUl\n", &guid);
2911 }
2912 static DEVICE_ATTR_RO(guid);
2913 
2914 static struct attribute *bmc_dev_attrs[] = {
2915 	&dev_attr_device_id.attr,
2916 	&dev_attr_provides_device_sdrs.attr,
2917 	&dev_attr_revision.attr,
2918 	&dev_attr_firmware_revision.attr,
2919 	&dev_attr_ipmi_version.attr,
2920 	&dev_attr_additional_device_support.attr,
2921 	&dev_attr_manufacturer_id.attr,
2922 	&dev_attr_product_id.attr,
2923 	&dev_attr_aux_firmware_revision.attr,
2924 	&dev_attr_guid.attr,
2925 	NULL
2926 };
2927 
bmc_dev_attr_is_visible(struct kobject * kobj,struct attribute * attr,int idx)2928 static umode_t bmc_dev_attr_is_visible(struct kobject *kobj,
2929 				       struct attribute *attr, int idx)
2930 {
2931 	struct device *dev = kobj_to_dev(kobj);
2932 	struct bmc_device *bmc = to_bmc_device(dev);
2933 	umode_t mode = attr->mode;
2934 	int rv;
2935 
2936 	if (attr == &dev_attr_aux_firmware_revision.attr) {
2937 		struct ipmi_device_id id;
2938 
2939 		rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2940 		return (!rv && id.aux_firmware_revision_set) ? mode : 0;
2941 	}
2942 	if (attr == &dev_attr_guid.attr) {
2943 		bool guid_set;
2944 
2945 		rv = bmc_get_device_id(NULL, bmc, NULL, &guid_set, NULL);
2946 		return (!rv && guid_set) ? mode : 0;
2947 	}
2948 	return mode;
2949 }
2950 
2951 static const struct attribute_group bmc_dev_attr_group = {
2952 	.attrs		= bmc_dev_attrs,
2953 	.is_visible	= bmc_dev_attr_is_visible,
2954 };
2955 
2956 static const struct attribute_group *bmc_dev_attr_groups[] = {
2957 	&bmc_dev_attr_group,
2958 	NULL
2959 };
2960 
2961 static const struct device_type bmc_device_type = {
2962 	.groups		= bmc_dev_attr_groups,
2963 };
2964 
__find_bmc_guid(struct device * dev,const void * data)2965 static int __find_bmc_guid(struct device *dev, const void *data)
2966 {
2967 	const guid_t *guid = data;
2968 	struct bmc_device *bmc;
2969 	int rv;
2970 
2971 	if (dev->type != &bmc_device_type)
2972 		return 0;
2973 
2974 	bmc = to_bmc_device(dev);
2975 	rv = bmc->dyn_guid_set && guid_equal(&bmc->guid, guid);
2976 	if (rv)
2977 		rv = kref_get_unless_zero(&bmc->usecount);
2978 	return rv;
2979 }
2980 
2981 /*
2982  * Returns with the bmc's usecount incremented, if it is non-NULL.
2983  */
ipmi_find_bmc_guid(struct device_driver * drv,guid_t * guid)2984 static struct bmc_device *ipmi_find_bmc_guid(struct device_driver *drv,
2985 					     guid_t *guid)
2986 {
2987 	struct device *dev;
2988 	struct bmc_device *bmc = NULL;
2989 
2990 	dev = driver_find_device(drv, NULL, guid, __find_bmc_guid);
2991 	if (dev) {
2992 		bmc = to_bmc_device(dev);
2993 		put_device(dev);
2994 	}
2995 	return bmc;
2996 }
2997 
2998 struct prod_dev_id {
2999 	unsigned int  product_id;
3000 	unsigned char device_id;
3001 };
3002 
__find_bmc_prod_dev_id(struct device * dev,const void * data)3003 static int __find_bmc_prod_dev_id(struct device *dev, const void *data)
3004 {
3005 	const struct prod_dev_id *cid = data;
3006 	struct bmc_device *bmc;
3007 	int rv;
3008 
3009 	if (dev->type != &bmc_device_type)
3010 		return 0;
3011 
3012 	bmc = to_bmc_device(dev);
3013 	rv = (bmc->id.product_id == cid->product_id
3014 	      && bmc->id.device_id == cid->device_id);
3015 	if (rv)
3016 		rv = kref_get_unless_zero(&bmc->usecount);
3017 	return rv;
3018 }
3019 
3020 /*
3021  * Returns with the bmc's usecount incremented, if it is non-NULL.
3022  */
ipmi_find_bmc_prod_dev_id(struct device_driver * drv,unsigned int product_id,unsigned char device_id)3023 static struct bmc_device *ipmi_find_bmc_prod_dev_id(
3024 	struct device_driver *drv,
3025 	unsigned int product_id, unsigned char device_id)
3026 {
3027 	struct prod_dev_id id = {
3028 		.product_id = product_id,
3029 		.device_id = device_id,
3030 	};
3031 	struct device *dev;
3032 	struct bmc_device *bmc = NULL;
3033 
3034 	dev = driver_find_device(drv, NULL, &id, __find_bmc_prod_dev_id);
3035 	if (dev) {
3036 		bmc = to_bmc_device(dev);
3037 		put_device(dev);
3038 	}
3039 	return bmc;
3040 }
3041 
3042 static DEFINE_IDA(ipmi_bmc_ida);
3043 
3044 static void
release_bmc_device(struct device * dev)3045 release_bmc_device(struct device *dev)
3046 {
3047 	kfree(to_bmc_device(dev));
3048 }
3049 
cleanup_bmc_work(struct work_struct * work)3050 static void cleanup_bmc_work(struct work_struct *work)
3051 {
3052 	struct bmc_device *bmc = container_of(work, struct bmc_device,
3053 					      remove_work);
3054 	int id = bmc->pdev.id; /* Unregister overwrites id */
3055 
3056 	platform_device_unregister(&bmc->pdev);
3057 	ida_simple_remove(&ipmi_bmc_ida, id);
3058 }
3059 
3060 static void
cleanup_bmc_device(struct kref * ref)3061 cleanup_bmc_device(struct kref *ref)
3062 {
3063 	struct bmc_device *bmc = container_of(ref, struct bmc_device, usecount);
3064 
3065 	/*
3066 	 * Remove the platform device in a work queue to avoid issues
3067 	 * with removing the device attributes while reading a device
3068 	 * attribute.
3069 	 */
3070 	queue_work(remove_work_wq, &bmc->remove_work);
3071 }
3072 
3073 /*
3074  * Must be called with intf->bmc_reg_mutex held.
3075  */
__ipmi_bmc_unregister(struct ipmi_smi * intf)3076 static void __ipmi_bmc_unregister(struct ipmi_smi *intf)
3077 {
3078 	struct bmc_device *bmc = intf->bmc;
3079 
3080 	if (!intf->bmc_registered)
3081 		return;
3082 
3083 	sysfs_remove_link(&intf->si_dev->kobj, "bmc");
3084 	sysfs_remove_link(&bmc->pdev.dev.kobj, intf->my_dev_name);
3085 	kfree(intf->my_dev_name);
3086 	intf->my_dev_name = NULL;
3087 
3088 	mutex_lock(&bmc->dyn_mutex);
3089 	list_del(&intf->bmc_link);
3090 	mutex_unlock(&bmc->dyn_mutex);
3091 	intf->bmc = &intf->tmp_bmc;
3092 	kref_put(&bmc->usecount, cleanup_bmc_device);
3093 	intf->bmc_registered = false;
3094 }
3095 
ipmi_bmc_unregister(struct ipmi_smi * intf)3096 static void ipmi_bmc_unregister(struct ipmi_smi *intf)
3097 {
3098 	mutex_lock(&intf->bmc_reg_mutex);
3099 	__ipmi_bmc_unregister(intf);
3100 	mutex_unlock(&intf->bmc_reg_mutex);
3101 }
3102 
3103 /*
3104  * Must be called with intf->bmc_reg_mutex held.
3105  */
__ipmi_bmc_register(struct ipmi_smi * intf,struct ipmi_device_id * id,bool guid_set,guid_t * guid,int intf_num)3106 static int __ipmi_bmc_register(struct ipmi_smi *intf,
3107 			       struct ipmi_device_id *id,
3108 			       bool guid_set, guid_t *guid, int intf_num)
3109 {
3110 	int               rv;
3111 	struct bmc_device *bmc;
3112 	struct bmc_device *old_bmc;
3113 
3114 	/*
3115 	 * platform_device_register() can cause bmc_reg_mutex to
3116 	 * be claimed because of the is_visible functions of
3117 	 * the attributes.  Eliminate possible recursion and
3118 	 * release the lock.
3119 	 */
3120 	intf->in_bmc_register = true;
3121 	mutex_unlock(&intf->bmc_reg_mutex);
3122 
3123 	/*
3124 	 * Try to find if there is an bmc_device struct
3125 	 * representing the interfaced BMC already
3126 	 */
3127 	mutex_lock(&ipmidriver_mutex);
3128 	if (guid_set)
3129 		old_bmc = ipmi_find_bmc_guid(&ipmidriver.driver, guid);
3130 	else
3131 		old_bmc = ipmi_find_bmc_prod_dev_id(&ipmidriver.driver,
3132 						    id->product_id,
3133 						    id->device_id);
3134 
3135 	/*
3136 	 * If there is already an bmc_device, free the new one,
3137 	 * otherwise register the new BMC device
3138 	 */
3139 	if (old_bmc) {
3140 		bmc = old_bmc;
3141 		/*
3142 		 * Note: old_bmc already has usecount incremented by
3143 		 * the BMC find functions.
3144 		 */
3145 		intf->bmc = old_bmc;
3146 		mutex_lock(&bmc->dyn_mutex);
3147 		list_add_tail(&intf->bmc_link, &bmc->intfs);
3148 		mutex_unlock(&bmc->dyn_mutex);
3149 
3150 		dev_info(intf->si_dev,
3151 			 "interfacing existing BMC (man_id: 0x%6.6x, prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
3152 			 bmc->id.manufacturer_id,
3153 			 bmc->id.product_id,
3154 			 bmc->id.device_id);
3155 	} else {
3156 		bmc = kzalloc(sizeof(*bmc), GFP_KERNEL);
3157 		if (!bmc) {
3158 			rv = -ENOMEM;
3159 			goto out;
3160 		}
3161 		INIT_LIST_HEAD(&bmc->intfs);
3162 		mutex_init(&bmc->dyn_mutex);
3163 		INIT_WORK(&bmc->remove_work, cleanup_bmc_work);
3164 
3165 		bmc->id = *id;
3166 		bmc->dyn_id_set = 1;
3167 		bmc->dyn_guid_set = guid_set;
3168 		bmc->guid = *guid;
3169 		bmc->dyn_id_expiry = jiffies + IPMI_DYN_DEV_ID_EXPIRY;
3170 
3171 		bmc->pdev.name = "ipmi_bmc";
3172 
3173 		rv = ida_simple_get(&ipmi_bmc_ida, 0, 0, GFP_KERNEL);
3174 		if (rv < 0) {
3175 			kfree(bmc);
3176 			goto out;
3177 		}
3178 
3179 		bmc->pdev.dev.driver = &ipmidriver.driver;
3180 		bmc->pdev.id = rv;
3181 		bmc->pdev.dev.release = release_bmc_device;
3182 		bmc->pdev.dev.type = &bmc_device_type;
3183 		kref_init(&bmc->usecount);
3184 
3185 		intf->bmc = bmc;
3186 		mutex_lock(&bmc->dyn_mutex);
3187 		list_add_tail(&intf->bmc_link, &bmc->intfs);
3188 		mutex_unlock(&bmc->dyn_mutex);
3189 
3190 		rv = platform_device_register(&bmc->pdev);
3191 		if (rv) {
3192 			dev_err(intf->si_dev,
3193 				"Unable to register bmc device: %d\n",
3194 				rv);
3195 			goto out_list_del;
3196 		}
3197 
3198 		dev_info(intf->si_dev,
3199 			 "Found new BMC (man_id: 0x%6.6x, prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
3200 			 bmc->id.manufacturer_id,
3201 			 bmc->id.product_id,
3202 			 bmc->id.device_id);
3203 	}
3204 
3205 	/*
3206 	 * create symlink from system interface device to bmc device
3207 	 * and back.
3208 	 */
3209 	rv = sysfs_create_link(&intf->si_dev->kobj, &bmc->pdev.dev.kobj, "bmc");
3210 	if (rv) {
3211 		dev_err(intf->si_dev, "Unable to create bmc symlink: %d\n", rv);
3212 		goto out_put_bmc;
3213 	}
3214 
3215 	if (intf_num == -1)
3216 		intf_num = intf->intf_num;
3217 	intf->my_dev_name = kasprintf(GFP_KERNEL, "ipmi%d", intf_num);
3218 	if (!intf->my_dev_name) {
3219 		rv = -ENOMEM;
3220 		dev_err(intf->si_dev, "Unable to allocate link from BMC: %d\n",
3221 			rv);
3222 		goto out_unlink1;
3223 	}
3224 
3225 	rv = sysfs_create_link(&bmc->pdev.dev.kobj, &intf->si_dev->kobj,
3226 			       intf->my_dev_name);
3227 	if (rv) {
3228 		dev_err(intf->si_dev, "Unable to create symlink to bmc: %d\n",
3229 			rv);
3230 		goto out_free_my_dev_name;
3231 	}
3232 
3233 	intf->bmc_registered = true;
3234 
3235 out:
3236 	mutex_unlock(&ipmidriver_mutex);
3237 	mutex_lock(&intf->bmc_reg_mutex);
3238 	intf->in_bmc_register = false;
3239 	return rv;
3240 
3241 
3242 out_free_my_dev_name:
3243 	kfree(intf->my_dev_name);
3244 	intf->my_dev_name = NULL;
3245 
3246 out_unlink1:
3247 	sysfs_remove_link(&intf->si_dev->kobj, "bmc");
3248 
3249 out_put_bmc:
3250 	mutex_lock(&bmc->dyn_mutex);
3251 	list_del(&intf->bmc_link);
3252 	mutex_unlock(&bmc->dyn_mutex);
3253 	intf->bmc = &intf->tmp_bmc;
3254 	kref_put(&bmc->usecount, cleanup_bmc_device);
3255 	goto out;
3256 
3257 out_list_del:
3258 	mutex_lock(&bmc->dyn_mutex);
3259 	list_del(&intf->bmc_link);
3260 	mutex_unlock(&bmc->dyn_mutex);
3261 	intf->bmc = &intf->tmp_bmc;
3262 	put_device(&bmc->pdev.dev);
3263 	goto out;
3264 }
3265 
3266 static int
send_guid_cmd(struct ipmi_smi * intf,int chan)3267 send_guid_cmd(struct ipmi_smi *intf, int chan)
3268 {
3269 	struct kernel_ipmi_msg            msg;
3270 	struct ipmi_system_interface_addr si;
3271 
3272 	si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3273 	si.channel = IPMI_BMC_CHANNEL;
3274 	si.lun = 0;
3275 
3276 	msg.netfn = IPMI_NETFN_APP_REQUEST;
3277 	msg.cmd = IPMI_GET_DEVICE_GUID_CMD;
3278 	msg.data = NULL;
3279 	msg.data_len = 0;
3280 	return i_ipmi_request(NULL,
3281 			      intf,
3282 			      (struct ipmi_addr *) &si,
3283 			      0,
3284 			      &msg,
3285 			      intf,
3286 			      NULL,
3287 			      NULL,
3288 			      0,
3289 			      intf->addrinfo[0].address,
3290 			      intf->addrinfo[0].lun,
3291 			      -1, 0);
3292 }
3293 
guid_handler(struct ipmi_smi * intf,struct ipmi_recv_msg * msg)3294 static void guid_handler(struct ipmi_smi *intf, struct ipmi_recv_msg *msg)
3295 {
3296 	struct bmc_device *bmc = intf->bmc;
3297 
3298 	if ((msg->addr.addr_type != IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
3299 	    || (msg->msg.netfn != IPMI_NETFN_APP_RESPONSE)
3300 	    || (msg->msg.cmd != IPMI_GET_DEVICE_GUID_CMD))
3301 		/* Not for me */
3302 		return;
3303 
3304 	if (msg->msg.data[0] != 0) {
3305 		/* Error from getting the GUID, the BMC doesn't have one. */
3306 		bmc->dyn_guid_set = 0;
3307 		goto out;
3308 	}
3309 
3310 	if (msg->msg.data_len < UUID_SIZE + 1) {
3311 		bmc->dyn_guid_set = 0;
3312 		dev_warn(intf->si_dev,
3313 			 "The GUID response from the BMC was too short, it was %d but should have been %d.  Assuming GUID is not available.\n",
3314 			 msg->msg.data_len, UUID_SIZE + 1);
3315 		goto out;
3316 	}
3317 
3318 	import_guid(&bmc->fetch_guid, msg->msg.data + 1);
3319 	/*
3320 	 * Make sure the guid data is available before setting
3321 	 * dyn_guid_set.
3322 	 */
3323 	smp_wmb();
3324 	bmc->dyn_guid_set = 1;
3325  out:
3326 	wake_up(&intf->waitq);
3327 }
3328 
__get_guid(struct ipmi_smi * intf)3329 static void __get_guid(struct ipmi_smi *intf)
3330 {
3331 	int rv;
3332 	struct bmc_device *bmc = intf->bmc;
3333 
3334 	bmc->dyn_guid_set = 2;
3335 	intf->null_user_handler = guid_handler;
3336 	rv = send_guid_cmd(intf, 0);
3337 	if (rv)
3338 		/* Send failed, no GUID available. */
3339 		bmc->dyn_guid_set = 0;
3340 	else
3341 		wait_event(intf->waitq, bmc->dyn_guid_set != 2);
3342 
3343 	/* dyn_guid_set makes the guid data available. */
3344 	smp_rmb();
3345 
3346 	intf->null_user_handler = NULL;
3347 }
3348 
3349 static int
send_channel_info_cmd(struct ipmi_smi * intf,int chan)3350 send_channel_info_cmd(struct ipmi_smi *intf, int chan)
3351 {
3352 	struct kernel_ipmi_msg            msg;
3353 	unsigned char                     data[1];
3354 	struct ipmi_system_interface_addr si;
3355 
3356 	si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3357 	si.channel = IPMI_BMC_CHANNEL;
3358 	si.lun = 0;
3359 
3360 	msg.netfn = IPMI_NETFN_APP_REQUEST;
3361 	msg.cmd = IPMI_GET_CHANNEL_INFO_CMD;
3362 	msg.data = data;
3363 	msg.data_len = 1;
3364 	data[0] = chan;
3365 	return i_ipmi_request(NULL,
3366 			      intf,
3367 			      (struct ipmi_addr *) &si,
3368 			      0,
3369 			      &msg,
3370 			      intf,
3371 			      NULL,
3372 			      NULL,
3373 			      0,
3374 			      intf->addrinfo[0].address,
3375 			      intf->addrinfo[0].lun,
3376 			      -1, 0);
3377 }
3378 
3379 static void
channel_handler(struct ipmi_smi * intf,struct ipmi_recv_msg * msg)3380 channel_handler(struct ipmi_smi *intf, struct ipmi_recv_msg *msg)
3381 {
3382 	int rv = 0;
3383 	int ch;
3384 	unsigned int set = intf->curr_working_cset;
3385 	struct ipmi_channel *chans;
3386 
3387 	if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
3388 	    && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
3389 	    && (msg->msg.cmd == IPMI_GET_CHANNEL_INFO_CMD)) {
3390 		/* It's the one we want */
3391 		if (msg->msg.data[0] != 0) {
3392 			/* Got an error from the channel, just go on. */
3393 			if (msg->msg.data[0] == IPMI_INVALID_COMMAND_ERR) {
3394 				/*
3395 				 * If the MC does not support this
3396 				 * command, that is legal.  We just
3397 				 * assume it has one IPMB at channel
3398 				 * zero.
3399 				 */
3400 				intf->wchannels[set].c[0].medium
3401 					= IPMI_CHANNEL_MEDIUM_IPMB;
3402 				intf->wchannels[set].c[0].protocol
3403 					= IPMI_CHANNEL_PROTOCOL_IPMB;
3404 
3405 				intf->channel_list = intf->wchannels + set;
3406 				intf->channels_ready = true;
3407 				wake_up(&intf->waitq);
3408 				goto out;
3409 			}
3410 			goto next_channel;
3411 		}
3412 		if (msg->msg.data_len < 4) {
3413 			/* Message not big enough, just go on. */
3414 			goto next_channel;
3415 		}
3416 		ch = intf->curr_channel;
3417 		chans = intf->wchannels[set].c;
3418 		chans[ch].medium = msg->msg.data[2] & 0x7f;
3419 		chans[ch].protocol = msg->msg.data[3] & 0x1f;
3420 
3421  next_channel:
3422 		intf->curr_channel++;
3423 		if (intf->curr_channel >= IPMI_MAX_CHANNELS) {
3424 			intf->channel_list = intf->wchannels + set;
3425 			intf->channels_ready = true;
3426 			wake_up(&intf->waitq);
3427 		} else {
3428 			intf->channel_list = intf->wchannels + set;
3429 			intf->channels_ready = true;
3430 			rv = send_channel_info_cmd(intf, intf->curr_channel);
3431 		}
3432 
3433 		if (rv) {
3434 			/* Got an error somehow, just give up. */
3435 			dev_warn(intf->si_dev,
3436 				 "Error sending channel information for channel %d: %d\n",
3437 				 intf->curr_channel, rv);
3438 
3439 			intf->channel_list = intf->wchannels + set;
3440 			intf->channels_ready = true;
3441 			wake_up(&intf->waitq);
3442 		}
3443 	}
3444  out:
3445 	return;
3446 }
3447 
3448 /*
3449  * Must be holding intf->bmc_reg_mutex to call this.
3450  */
__scan_channels(struct ipmi_smi * intf,struct ipmi_device_id * id)3451 static int __scan_channels(struct ipmi_smi *intf, struct ipmi_device_id *id)
3452 {
3453 	int rv;
3454 
3455 	if (ipmi_version_major(id) > 1
3456 			|| (ipmi_version_major(id) == 1
3457 			    && ipmi_version_minor(id) >= 5)) {
3458 		unsigned int set;
3459 
3460 		/*
3461 		 * Start scanning the channels to see what is
3462 		 * available.
3463 		 */
3464 		set = !intf->curr_working_cset;
3465 		intf->curr_working_cset = set;
3466 		memset(&intf->wchannels[set], 0,
3467 		       sizeof(struct ipmi_channel_set));
3468 
3469 		intf->null_user_handler = channel_handler;
3470 		intf->curr_channel = 0;
3471 		rv = send_channel_info_cmd(intf, 0);
3472 		if (rv) {
3473 			dev_warn(intf->si_dev,
3474 				 "Error sending channel information for channel 0, %d\n",
3475 				 rv);
3476 			intf->null_user_handler = NULL;
3477 			return -EIO;
3478 		}
3479 
3480 		/* Wait for the channel info to be read. */
3481 		wait_event(intf->waitq, intf->channels_ready);
3482 		intf->null_user_handler = NULL;
3483 	} else {
3484 		unsigned int set = intf->curr_working_cset;
3485 
3486 		/* Assume a single IPMB channel at zero. */
3487 		intf->wchannels[set].c[0].medium = IPMI_CHANNEL_MEDIUM_IPMB;
3488 		intf->wchannels[set].c[0].protocol = IPMI_CHANNEL_PROTOCOL_IPMB;
3489 		intf->channel_list = intf->wchannels + set;
3490 		intf->channels_ready = true;
3491 	}
3492 
3493 	return 0;
3494 }
3495 
ipmi_poll(struct ipmi_smi * intf)3496 static void ipmi_poll(struct ipmi_smi *intf)
3497 {
3498 	if (intf->handlers->poll)
3499 		intf->handlers->poll(intf->send_info);
3500 	/* In case something came in */
3501 	handle_new_recv_msgs(intf);
3502 }
3503 
ipmi_poll_interface(struct ipmi_user * user)3504 void ipmi_poll_interface(struct ipmi_user *user)
3505 {
3506 	ipmi_poll(user->intf);
3507 }
3508 EXPORT_SYMBOL(ipmi_poll_interface);
3509 
nr_users_show(struct device * dev,struct device_attribute * attr,char * buf)3510 static ssize_t nr_users_show(struct device *dev,
3511 			     struct device_attribute *attr,
3512 			     char *buf)
3513 {
3514 	struct ipmi_smi *intf = container_of(attr,
3515 			 struct ipmi_smi, nr_users_devattr);
3516 
3517 	return sysfs_emit(buf, "%d\n", atomic_read(&intf->nr_users));
3518 }
3519 static DEVICE_ATTR_RO(nr_users);
3520 
nr_msgs_show(struct device * dev,struct device_attribute * attr,char * buf)3521 static ssize_t nr_msgs_show(struct device *dev,
3522 			    struct device_attribute *attr,
3523 			    char *buf)
3524 {
3525 	struct ipmi_smi *intf = container_of(attr,
3526 			 struct ipmi_smi, nr_msgs_devattr);
3527 	struct ipmi_user *user;
3528 	int index;
3529 	unsigned int count = 0;
3530 
3531 	index = srcu_read_lock(&intf->users_srcu);
3532 	list_for_each_entry_rcu(user, &intf->users, link)
3533 		count += atomic_read(&user->nr_msgs);
3534 	srcu_read_unlock(&intf->users_srcu, index);
3535 
3536 	return sysfs_emit(buf, "%u\n", count);
3537 }
3538 static DEVICE_ATTR_RO(nr_msgs);
3539 
redo_bmc_reg(struct work_struct * work)3540 static void redo_bmc_reg(struct work_struct *work)
3541 {
3542 	struct ipmi_smi *intf = container_of(work, struct ipmi_smi,
3543 					     bmc_reg_work);
3544 
3545 	if (!intf->in_shutdown)
3546 		bmc_get_device_id(intf, NULL, NULL, NULL, NULL);
3547 
3548 	kref_put(&intf->refcount, intf_free);
3549 }
3550 
ipmi_add_smi(struct module * owner,const struct ipmi_smi_handlers * handlers,void * send_info,struct device * si_dev,unsigned char slave_addr)3551 int ipmi_add_smi(struct module         *owner,
3552 		 const struct ipmi_smi_handlers *handlers,
3553 		 void		       *send_info,
3554 		 struct device         *si_dev,
3555 		 unsigned char         slave_addr)
3556 {
3557 	int              i, j;
3558 	int              rv;
3559 	struct ipmi_smi *intf, *tintf;
3560 	struct list_head *link;
3561 	struct ipmi_device_id id;
3562 
3563 	/*
3564 	 * Make sure the driver is actually initialized, this handles
3565 	 * problems with initialization order.
3566 	 */
3567 	rv = ipmi_init_msghandler();
3568 	if (rv)
3569 		return rv;
3570 
3571 	intf = kzalloc(sizeof(*intf), GFP_KERNEL);
3572 	if (!intf)
3573 		return -ENOMEM;
3574 
3575 	rv = init_srcu_struct(&intf->users_srcu);
3576 	if (rv) {
3577 		kfree(intf);
3578 		return rv;
3579 	}
3580 
3581 	intf->owner = owner;
3582 	intf->bmc = &intf->tmp_bmc;
3583 	INIT_LIST_HEAD(&intf->bmc->intfs);
3584 	mutex_init(&intf->bmc->dyn_mutex);
3585 	INIT_LIST_HEAD(&intf->bmc_link);
3586 	mutex_init(&intf->bmc_reg_mutex);
3587 	intf->intf_num = -1; /* Mark it invalid for now. */
3588 	kref_init(&intf->refcount);
3589 	INIT_WORK(&intf->bmc_reg_work, redo_bmc_reg);
3590 	intf->si_dev = si_dev;
3591 	for (j = 0; j < IPMI_MAX_CHANNELS; j++) {
3592 		intf->addrinfo[j].address = IPMI_BMC_SLAVE_ADDR;
3593 		intf->addrinfo[j].lun = 2;
3594 	}
3595 	if (slave_addr != 0)
3596 		intf->addrinfo[0].address = slave_addr;
3597 	INIT_LIST_HEAD(&intf->users);
3598 	atomic_set(&intf->nr_users, 0);
3599 	intf->handlers = handlers;
3600 	intf->send_info = send_info;
3601 	spin_lock_init(&intf->seq_lock);
3602 	for (j = 0; j < IPMI_IPMB_NUM_SEQ; j++) {
3603 		intf->seq_table[j].inuse = 0;
3604 		intf->seq_table[j].seqid = 0;
3605 	}
3606 	intf->curr_seq = 0;
3607 	spin_lock_init(&intf->waiting_rcv_msgs_lock);
3608 	INIT_LIST_HEAD(&intf->waiting_rcv_msgs);
3609 	tasklet_setup(&intf->recv_tasklet,
3610 		     smi_recv_tasklet);
3611 	atomic_set(&intf->watchdog_pretimeouts_to_deliver, 0);
3612 	spin_lock_init(&intf->xmit_msgs_lock);
3613 	INIT_LIST_HEAD(&intf->xmit_msgs);
3614 	INIT_LIST_HEAD(&intf->hp_xmit_msgs);
3615 	spin_lock_init(&intf->events_lock);
3616 	spin_lock_init(&intf->watch_lock);
3617 	atomic_set(&intf->event_waiters, 0);
3618 	intf->ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
3619 	INIT_LIST_HEAD(&intf->waiting_events);
3620 	intf->waiting_events_count = 0;
3621 	mutex_init(&intf->cmd_rcvrs_mutex);
3622 	spin_lock_init(&intf->maintenance_mode_lock);
3623 	INIT_LIST_HEAD(&intf->cmd_rcvrs);
3624 	init_waitqueue_head(&intf->waitq);
3625 	for (i = 0; i < IPMI_NUM_STATS; i++)
3626 		atomic_set(&intf->stats[i], 0);
3627 
3628 	mutex_lock(&ipmi_interfaces_mutex);
3629 	/* Look for a hole in the numbers. */
3630 	i = 0;
3631 	link = &ipmi_interfaces;
3632 	list_for_each_entry_rcu(tintf, &ipmi_interfaces, link,
3633 				ipmi_interfaces_mutex_held()) {
3634 		if (tintf->intf_num != i) {
3635 			link = &tintf->link;
3636 			break;
3637 		}
3638 		i++;
3639 	}
3640 	/* Add the new interface in numeric order. */
3641 	if (i == 0)
3642 		list_add_rcu(&intf->link, &ipmi_interfaces);
3643 	else
3644 		list_add_tail_rcu(&intf->link, link);
3645 
3646 	rv = handlers->start_processing(send_info, intf);
3647 	if (rv)
3648 		goto out_err;
3649 
3650 	rv = __bmc_get_device_id(intf, NULL, &id, NULL, NULL, i);
3651 	if (rv) {
3652 		dev_err(si_dev, "Unable to get the device id: %d\n", rv);
3653 		goto out_err_started;
3654 	}
3655 
3656 	mutex_lock(&intf->bmc_reg_mutex);
3657 	rv = __scan_channels(intf, &id);
3658 	mutex_unlock(&intf->bmc_reg_mutex);
3659 	if (rv)
3660 		goto out_err_bmc_reg;
3661 
3662 	intf->nr_users_devattr = dev_attr_nr_users;
3663 	sysfs_attr_init(&intf->nr_users_devattr.attr);
3664 	rv = device_create_file(intf->si_dev, &intf->nr_users_devattr);
3665 	if (rv)
3666 		goto out_err_bmc_reg;
3667 
3668 	intf->nr_msgs_devattr = dev_attr_nr_msgs;
3669 	sysfs_attr_init(&intf->nr_msgs_devattr.attr);
3670 	rv = device_create_file(intf->si_dev, &intf->nr_msgs_devattr);
3671 	if (rv) {
3672 		device_remove_file(intf->si_dev, &intf->nr_users_devattr);
3673 		goto out_err_bmc_reg;
3674 	}
3675 
3676 	/*
3677 	 * Keep memory order straight for RCU readers.  Make
3678 	 * sure everything else is committed to memory before
3679 	 * setting intf_num to mark the interface valid.
3680 	 */
3681 	smp_wmb();
3682 	intf->intf_num = i;
3683 	mutex_unlock(&ipmi_interfaces_mutex);
3684 
3685 	/* After this point the interface is legal to use. */
3686 	call_smi_watchers(i, intf->si_dev);
3687 
3688 	return 0;
3689 
3690  out_err_bmc_reg:
3691 	ipmi_bmc_unregister(intf);
3692  out_err_started:
3693 	if (intf->handlers->shutdown)
3694 		intf->handlers->shutdown(intf->send_info);
3695  out_err:
3696 	list_del_rcu(&intf->link);
3697 	mutex_unlock(&ipmi_interfaces_mutex);
3698 	synchronize_srcu(&ipmi_interfaces_srcu);
3699 	cleanup_srcu_struct(&intf->users_srcu);
3700 	kref_put(&intf->refcount, intf_free);
3701 
3702 	return rv;
3703 }
3704 EXPORT_SYMBOL(ipmi_add_smi);
3705 
deliver_smi_err_response(struct ipmi_smi * intf,struct ipmi_smi_msg * msg,unsigned char err)3706 static void deliver_smi_err_response(struct ipmi_smi *intf,
3707 				     struct ipmi_smi_msg *msg,
3708 				     unsigned char err)
3709 {
3710 	int rv;
3711 	msg->rsp[0] = msg->data[0] | 4;
3712 	msg->rsp[1] = msg->data[1];
3713 	msg->rsp[2] = err;
3714 	msg->rsp_size = 3;
3715 
3716 	/* This will never requeue, but it may ask us to free the message. */
3717 	rv = handle_one_recv_msg(intf, msg);
3718 	if (rv == 0)
3719 		ipmi_free_smi_msg(msg);
3720 }
3721 
cleanup_smi_msgs(struct ipmi_smi * intf)3722 static void cleanup_smi_msgs(struct ipmi_smi *intf)
3723 {
3724 	int              i;
3725 	struct seq_table *ent;
3726 	struct ipmi_smi_msg *msg;
3727 	struct list_head *entry;
3728 	struct list_head tmplist;
3729 
3730 	/* Clear out our transmit queues and hold the messages. */
3731 	INIT_LIST_HEAD(&tmplist);
3732 	list_splice_tail(&intf->hp_xmit_msgs, &tmplist);
3733 	list_splice_tail(&intf->xmit_msgs, &tmplist);
3734 
3735 	/* Current message first, to preserve order */
3736 	while (intf->curr_msg && !list_empty(&intf->waiting_rcv_msgs)) {
3737 		/* Wait for the message to clear out. */
3738 		schedule_timeout(1);
3739 	}
3740 
3741 	/* No need for locks, the interface is down. */
3742 
3743 	/*
3744 	 * Return errors for all pending messages in queue and in the
3745 	 * tables waiting for remote responses.
3746 	 */
3747 	while (!list_empty(&tmplist)) {
3748 		entry = tmplist.next;
3749 		list_del(entry);
3750 		msg = list_entry(entry, struct ipmi_smi_msg, link);
3751 		deliver_smi_err_response(intf, msg, IPMI_ERR_UNSPECIFIED);
3752 	}
3753 
3754 	for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
3755 		ent = &intf->seq_table[i];
3756 		if (!ent->inuse)
3757 			continue;
3758 		deliver_err_response(intf, ent->recv_msg, IPMI_ERR_UNSPECIFIED);
3759 	}
3760 }
3761 
ipmi_unregister_smi(struct ipmi_smi * intf)3762 void ipmi_unregister_smi(struct ipmi_smi *intf)
3763 {
3764 	struct ipmi_smi_watcher *w;
3765 	int intf_num, index;
3766 
3767 	if (!intf)
3768 		return;
3769 	intf_num = intf->intf_num;
3770 	mutex_lock(&ipmi_interfaces_mutex);
3771 	intf->intf_num = -1;
3772 	intf->in_shutdown = true;
3773 	list_del_rcu(&intf->link);
3774 	mutex_unlock(&ipmi_interfaces_mutex);
3775 	synchronize_srcu(&ipmi_interfaces_srcu);
3776 
3777 	/* At this point no users can be added to the interface. */
3778 
3779 	device_remove_file(intf->si_dev, &intf->nr_msgs_devattr);
3780 	device_remove_file(intf->si_dev, &intf->nr_users_devattr);
3781 
3782 	/*
3783 	 * Call all the watcher interfaces to tell them that
3784 	 * an interface is going away.
3785 	 */
3786 	mutex_lock(&smi_watchers_mutex);
3787 	list_for_each_entry(w, &smi_watchers, link)
3788 		w->smi_gone(intf_num);
3789 	mutex_unlock(&smi_watchers_mutex);
3790 
3791 	index = srcu_read_lock(&intf->users_srcu);
3792 	while (!list_empty(&intf->users)) {
3793 		struct ipmi_user *user =
3794 			container_of(list_next_rcu(&intf->users),
3795 				     struct ipmi_user, link);
3796 
3797 		_ipmi_destroy_user(user);
3798 	}
3799 	srcu_read_unlock(&intf->users_srcu, index);
3800 
3801 	if (intf->handlers->shutdown)
3802 		intf->handlers->shutdown(intf->send_info);
3803 
3804 	cleanup_smi_msgs(intf);
3805 
3806 	ipmi_bmc_unregister(intf);
3807 
3808 	cleanup_srcu_struct(&intf->users_srcu);
3809 	kref_put(&intf->refcount, intf_free);
3810 }
3811 EXPORT_SYMBOL(ipmi_unregister_smi);
3812 
handle_ipmb_get_msg_rsp(struct ipmi_smi * intf,struct ipmi_smi_msg * msg)3813 static int handle_ipmb_get_msg_rsp(struct ipmi_smi *intf,
3814 				   struct ipmi_smi_msg *msg)
3815 {
3816 	struct ipmi_ipmb_addr ipmb_addr;
3817 	struct ipmi_recv_msg  *recv_msg;
3818 
3819 	/*
3820 	 * This is 11, not 10, because the response must contain a
3821 	 * completion code.
3822 	 */
3823 	if (msg->rsp_size < 11) {
3824 		/* Message not big enough, just ignore it. */
3825 		ipmi_inc_stat(intf, invalid_ipmb_responses);
3826 		return 0;
3827 	}
3828 
3829 	if (msg->rsp[2] != 0) {
3830 		/* An error getting the response, just ignore it. */
3831 		return 0;
3832 	}
3833 
3834 	ipmb_addr.addr_type = IPMI_IPMB_ADDR_TYPE;
3835 	ipmb_addr.slave_addr = msg->rsp[6];
3836 	ipmb_addr.channel = msg->rsp[3] & 0x0f;
3837 	ipmb_addr.lun = msg->rsp[7] & 3;
3838 
3839 	/*
3840 	 * It's a response from a remote entity.  Look up the sequence
3841 	 * number and handle the response.
3842 	 */
3843 	if (intf_find_seq(intf,
3844 			  msg->rsp[7] >> 2,
3845 			  msg->rsp[3] & 0x0f,
3846 			  msg->rsp[8],
3847 			  (msg->rsp[4] >> 2) & (~1),
3848 			  (struct ipmi_addr *) &ipmb_addr,
3849 			  &recv_msg)) {
3850 		/*
3851 		 * We were unable to find the sequence number,
3852 		 * so just nuke the message.
3853 		 */
3854 		ipmi_inc_stat(intf, unhandled_ipmb_responses);
3855 		return 0;
3856 	}
3857 
3858 	memcpy(recv_msg->msg_data, &msg->rsp[9], msg->rsp_size - 9);
3859 	/*
3860 	 * The other fields matched, so no need to set them, except
3861 	 * for netfn, which needs to be the response that was
3862 	 * returned, not the request value.
3863 	 */
3864 	recv_msg->msg.netfn = msg->rsp[4] >> 2;
3865 	recv_msg->msg.data = recv_msg->msg_data;
3866 	recv_msg->msg.data_len = msg->rsp_size - 10;
3867 	recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
3868 	if (deliver_response(intf, recv_msg))
3869 		ipmi_inc_stat(intf, unhandled_ipmb_responses);
3870 	else
3871 		ipmi_inc_stat(intf, handled_ipmb_responses);
3872 
3873 	return 0;
3874 }
3875 
handle_ipmb_get_msg_cmd(struct ipmi_smi * intf,struct ipmi_smi_msg * msg)3876 static int handle_ipmb_get_msg_cmd(struct ipmi_smi *intf,
3877 				   struct ipmi_smi_msg *msg)
3878 {
3879 	struct cmd_rcvr          *rcvr;
3880 	int                      rv = 0;
3881 	unsigned char            netfn;
3882 	unsigned char            cmd;
3883 	unsigned char            chan;
3884 	struct ipmi_user         *user = NULL;
3885 	struct ipmi_ipmb_addr    *ipmb_addr;
3886 	struct ipmi_recv_msg     *recv_msg;
3887 
3888 	if (msg->rsp_size < 10) {
3889 		/* Message not big enough, just ignore it. */
3890 		ipmi_inc_stat(intf, invalid_commands);
3891 		return 0;
3892 	}
3893 
3894 	if (msg->rsp[2] != 0) {
3895 		/* An error getting the response, just ignore it. */
3896 		return 0;
3897 	}
3898 
3899 	netfn = msg->rsp[4] >> 2;
3900 	cmd = msg->rsp[8];
3901 	chan = msg->rsp[3] & 0xf;
3902 
3903 	rcu_read_lock();
3904 	rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
3905 	if (rcvr) {
3906 		user = rcvr->user;
3907 		kref_get(&user->refcount);
3908 	} else
3909 		user = NULL;
3910 	rcu_read_unlock();
3911 
3912 	if (user == NULL) {
3913 		/* We didn't find a user, deliver an error response. */
3914 		ipmi_inc_stat(intf, unhandled_commands);
3915 
3916 		msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
3917 		msg->data[1] = IPMI_SEND_MSG_CMD;
3918 		msg->data[2] = msg->rsp[3];
3919 		msg->data[3] = msg->rsp[6];
3920 		msg->data[4] = ((netfn + 1) << 2) | (msg->rsp[7] & 0x3);
3921 		msg->data[5] = ipmb_checksum(&msg->data[3], 2);
3922 		msg->data[6] = intf->addrinfo[msg->rsp[3] & 0xf].address;
3923 		/* rqseq/lun */
3924 		msg->data[7] = (msg->rsp[7] & 0xfc) | (msg->rsp[4] & 0x3);
3925 		msg->data[8] = msg->rsp[8]; /* cmd */
3926 		msg->data[9] = IPMI_INVALID_CMD_COMPLETION_CODE;
3927 		msg->data[10] = ipmb_checksum(&msg->data[6], 4);
3928 		msg->data_size = 11;
3929 
3930 		dev_dbg(intf->si_dev, "Invalid command: %*ph\n",
3931 			msg->data_size, msg->data);
3932 
3933 		rcu_read_lock();
3934 		if (!intf->in_shutdown) {
3935 			smi_send(intf, intf->handlers, msg, 0);
3936 			/*
3937 			 * We used the message, so return the value
3938 			 * that causes it to not be freed or
3939 			 * queued.
3940 			 */
3941 			rv = -1;
3942 		}
3943 		rcu_read_unlock();
3944 	} else {
3945 		recv_msg = ipmi_alloc_recv_msg();
3946 		if (!recv_msg) {
3947 			/*
3948 			 * We couldn't allocate memory for the
3949 			 * message, so requeue it for handling
3950 			 * later.
3951 			 */
3952 			rv = 1;
3953 			kref_put(&user->refcount, free_user);
3954 		} else {
3955 			/* Extract the source address from the data. */
3956 			ipmb_addr = (struct ipmi_ipmb_addr *) &recv_msg->addr;
3957 			ipmb_addr->addr_type = IPMI_IPMB_ADDR_TYPE;
3958 			ipmb_addr->slave_addr = msg->rsp[6];
3959 			ipmb_addr->lun = msg->rsp[7] & 3;
3960 			ipmb_addr->channel = msg->rsp[3] & 0xf;
3961 
3962 			/*
3963 			 * Extract the rest of the message information
3964 			 * from the IPMB header.
3965 			 */
3966 			recv_msg->user = user;
3967 			recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
3968 			recv_msg->msgid = msg->rsp[7] >> 2;
3969 			recv_msg->msg.netfn = msg->rsp[4] >> 2;
3970 			recv_msg->msg.cmd = msg->rsp[8];
3971 			recv_msg->msg.data = recv_msg->msg_data;
3972 
3973 			/*
3974 			 * We chop off 10, not 9 bytes because the checksum
3975 			 * at the end also needs to be removed.
3976 			 */
3977 			recv_msg->msg.data_len = msg->rsp_size - 10;
3978 			memcpy(recv_msg->msg_data, &msg->rsp[9],
3979 			       msg->rsp_size - 10);
3980 			if (deliver_response(intf, recv_msg))
3981 				ipmi_inc_stat(intf, unhandled_commands);
3982 			else
3983 				ipmi_inc_stat(intf, handled_commands);
3984 		}
3985 	}
3986 
3987 	return rv;
3988 }
3989 
handle_ipmb_direct_rcv_cmd(struct ipmi_smi * intf,struct ipmi_smi_msg * msg)3990 static int handle_ipmb_direct_rcv_cmd(struct ipmi_smi *intf,
3991 				      struct ipmi_smi_msg *msg)
3992 {
3993 	struct cmd_rcvr          *rcvr;
3994 	int                      rv = 0;
3995 	struct ipmi_user         *user = NULL;
3996 	struct ipmi_ipmb_direct_addr *daddr;
3997 	struct ipmi_recv_msg     *recv_msg;
3998 	unsigned char netfn = msg->rsp[0] >> 2;
3999 	unsigned char cmd = msg->rsp[3];
4000 
4001 	rcu_read_lock();
4002 	/* We always use channel 0 for direct messages. */
4003 	rcvr = find_cmd_rcvr(intf, netfn, cmd, 0);
4004 	if (rcvr) {
4005 		user = rcvr->user;
4006 		kref_get(&user->refcount);
4007 	} else
4008 		user = NULL;
4009 	rcu_read_unlock();
4010 
4011 	if (user == NULL) {
4012 		/* We didn't find a user, deliver an error response. */
4013 		ipmi_inc_stat(intf, unhandled_commands);
4014 
4015 		msg->data[0] = (netfn + 1) << 2;
4016 		msg->data[0] |= msg->rsp[2] & 0x3; /* rqLUN */
4017 		msg->data[1] = msg->rsp[1]; /* Addr */
4018 		msg->data[2] = msg->rsp[2] & ~0x3; /* rqSeq */
4019 		msg->data[2] |= msg->rsp[0] & 0x3; /* rsLUN */
4020 		msg->data[3] = cmd;
4021 		msg->data[4] = IPMI_INVALID_CMD_COMPLETION_CODE;
4022 		msg->data_size = 5;
4023 
4024 		rcu_read_lock();
4025 		if (!intf->in_shutdown) {
4026 			smi_send(intf, intf->handlers, msg, 0);
4027 			/*
4028 			 * We used the message, so return the value
4029 			 * that causes it to not be freed or
4030 			 * queued.
4031 			 */
4032 			rv = -1;
4033 		}
4034 		rcu_read_unlock();
4035 	} else {
4036 		recv_msg = ipmi_alloc_recv_msg();
4037 		if (!recv_msg) {
4038 			/*
4039 			 * We couldn't allocate memory for the
4040 			 * message, so requeue it for handling
4041 			 * later.
4042 			 */
4043 			rv = 1;
4044 			kref_put(&user->refcount, free_user);
4045 		} else {
4046 			/* Extract the source address from the data. */
4047 			daddr = (struct ipmi_ipmb_direct_addr *)&recv_msg->addr;
4048 			daddr->addr_type = IPMI_IPMB_DIRECT_ADDR_TYPE;
4049 			daddr->channel = 0;
4050 			daddr->slave_addr = msg->rsp[1];
4051 			daddr->rs_lun = msg->rsp[0] & 3;
4052 			daddr->rq_lun = msg->rsp[2] & 3;
4053 
4054 			/*
4055 			 * Extract the rest of the message information
4056 			 * from the IPMB header.
4057 			 */
4058 			recv_msg->user = user;
4059 			recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
4060 			recv_msg->msgid = (msg->rsp[2] >> 2);
4061 			recv_msg->msg.netfn = msg->rsp[0] >> 2;
4062 			recv_msg->msg.cmd = msg->rsp[3];
4063 			recv_msg->msg.data = recv_msg->msg_data;
4064 
4065 			recv_msg->msg.data_len = msg->rsp_size - 4;
4066 			memcpy(recv_msg->msg_data, msg->rsp + 4,
4067 			       msg->rsp_size - 4);
4068 			if (deliver_response(intf, recv_msg))
4069 				ipmi_inc_stat(intf, unhandled_commands);
4070 			else
4071 				ipmi_inc_stat(intf, handled_commands);
4072 		}
4073 	}
4074 
4075 	return rv;
4076 }
4077 
handle_ipmb_direct_rcv_rsp(struct ipmi_smi * intf,struct ipmi_smi_msg * msg)4078 static int handle_ipmb_direct_rcv_rsp(struct ipmi_smi *intf,
4079 				      struct ipmi_smi_msg *msg)
4080 {
4081 	struct ipmi_recv_msg *recv_msg;
4082 	struct ipmi_ipmb_direct_addr *daddr;
4083 
4084 	recv_msg = msg->user_data;
4085 	if (recv_msg == NULL) {
4086 		dev_warn(intf->si_dev,
4087 			 "IPMI direct message received with no owner. This could be because of a malformed message, or because of a hardware error.  Contact your hardware vendor for assistance.\n");
4088 		return 0;
4089 	}
4090 
4091 	recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
4092 	recv_msg->msgid = msg->msgid;
4093 	daddr = (struct ipmi_ipmb_direct_addr *) &recv_msg->addr;
4094 	daddr->addr_type = IPMI_IPMB_DIRECT_ADDR_TYPE;
4095 	daddr->channel = 0;
4096 	daddr->slave_addr = msg->rsp[1];
4097 	daddr->rq_lun = msg->rsp[0] & 3;
4098 	daddr->rs_lun = msg->rsp[2] & 3;
4099 	recv_msg->msg.netfn = msg->rsp[0] >> 2;
4100 	recv_msg->msg.cmd = msg->rsp[3];
4101 	memcpy(recv_msg->msg_data, &msg->rsp[4], msg->rsp_size - 4);
4102 	recv_msg->msg.data = recv_msg->msg_data;
4103 	recv_msg->msg.data_len = msg->rsp_size - 4;
4104 	deliver_local_response(intf, recv_msg);
4105 
4106 	return 0;
4107 }
4108 
handle_lan_get_msg_rsp(struct ipmi_smi * intf,struct ipmi_smi_msg * msg)4109 static int handle_lan_get_msg_rsp(struct ipmi_smi *intf,
4110 				  struct ipmi_smi_msg *msg)
4111 {
4112 	struct ipmi_lan_addr  lan_addr;
4113 	struct ipmi_recv_msg  *recv_msg;
4114 
4115 
4116 	/*
4117 	 * This is 13, not 12, because the response must contain a
4118 	 * completion code.
4119 	 */
4120 	if (msg->rsp_size < 13) {
4121 		/* Message not big enough, just ignore it. */
4122 		ipmi_inc_stat(intf, invalid_lan_responses);
4123 		return 0;
4124 	}
4125 
4126 	if (msg->rsp[2] != 0) {
4127 		/* An error getting the response, just ignore it. */
4128 		return 0;
4129 	}
4130 
4131 	lan_addr.addr_type = IPMI_LAN_ADDR_TYPE;
4132 	lan_addr.session_handle = msg->rsp[4];
4133 	lan_addr.remote_SWID = msg->rsp[8];
4134 	lan_addr.local_SWID = msg->rsp[5];
4135 	lan_addr.channel = msg->rsp[3] & 0x0f;
4136 	lan_addr.privilege = msg->rsp[3] >> 4;
4137 	lan_addr.lun = msg->rsp[9] & 3;
4138 
4139 	/*
4140 	 * It's a response from a remote entity.  Look up the sequence
4141 	 * number and handle the response.
4142 	 */
4143 	if (intf_find_seq(intf,
4144 			  msg->rsp[9] >> 2,
4145 			  msg->rsp[3] & 0x0f,
4146 			  msg->rsp[10],
4147 			  (msg->rsp[6] >> 2) & (~1),
4148 			  (struct ipmi_addr *) &lan_addr,
4149 			  &recv_msg)) {
4150 		/*
4151 		 * We were unable to find the sequence number,
4152 		 * so just nuke the message.
4153 		 */
4154 		ipmi_inc_stat(intf, unhandled_lan_responses);
4155 		return 0;
4156 	}
4157 
4158 	memcpy(recv_msg->msg_data, &msg->rsp[11], msg->rsp_size - 11);
4159 	/*
4160 	 * The other fields matched, so no need to set them, except
4161 	 * for netfn, which needs to be the response that was
4162 	 * returned, not the request value.
4163 	 */
4164 	recv_msg->msg.netfn = msg->rsp[6] >> 2;
4165 	recv_msg->msg.data = recv_msg->msg_data;
4166 	recv_msg->msg.data_len = msg->rsp_size - 12;
4167 	recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
4168 	if (deliver_response(intf, recv_msg))
4169 		ipmi_inc_stat(intf, unhandled_lan_responses);
4170 	else
4171 		ipmi_inc_stat(intf, handled_lan_responses);
4172 
4173 	return 0;
4174 }
4175 
handle_lan_get_msg_cmd(struct ipmi_smi * intf,struct ipmi_smi_msg * msg)4176 static int handle_lan_get_msg_cmd(struct ipmi_smi *intf,
4177 				  struct ipmi_smi_msg *msg)
4178 {
4179 	struct cmd_rcvr          *rcvr;
4180 	int                      rv = 0;
4181 	unsigned char            netfn;
4182 	unsigned char            cmd;
4183 	unsigned char            chan;
4184 	struct ipmi_user         *user = NULL;
4185 	struct ipmi_lan_addr     *lan_addr;
4186 	struct ipmi_recv_msg     *recv_msg;
4187 
4188 	if (msg->rsp_size < 12) {
4189 		/* Message not big enough, just ignore it. */
4190 		ipmi_inc_stat(intf, invalid_commands);
4191 		return 0;
4192 	}
4193 
4194 	if (msg->rsp[2] != 0) {
4195 		/* An error getting the response, just ignore it. */
4196 		return 0;
4197 	}
4198 
4199 	netfn = msg->rsp[6] >> 2;
4200 	cmd = msg->rsp[10];
4201 	chan = msg->rsp[3] & 0xf;
4202 
4203 	rcu_read_lock();
4204 	rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
4205 	if (rcvr) {
4206 		user = rcvr->user;
4207 		kref_get(&user->refcount);
4208 	} else
4209 		user = NULL;
4210 	rcu_read_unlock();
4211 
4212 	if (user == NULL) {
4213 		/* We didn't find a user, just give up. */
4214 		ipmi_inc_stat(intf, unhandled_commands);
4215 
4216 		/*
4217 		 * Don't do anything with these messages, just allow
4218 		 * them to be freed.
4219 		 */
4220 		rv = 0;
4221 	} else {
4222 		recv_msg = ipmi_alloc_recv_msg();
4223 		if (!recv_msg) {
4224 			/*
4225 			 * We couldn't allocate memory for the
4226 			 * message, so requeue it for handling later.
4227 			 */
4228 			rv = 1;
4229 			kref_put(&user->refcount, free_user);
4230 		} else {
4231 			/* Extract the source address from the data. */
4232 			lan_addr = (struct ipmi_lan_addr *) &recv_msg->addr;
4233 			lan_addr->addr_type = IPMI_LAN_ADDR_TYPE;
4234 			lan_addr->session_handle = msg->rsp[4];
4235 			lan_addr->remote_SWID = msg->rsp[8];
4236 			lan_addr->local_SWID = msg->rsp[5];
4237 			lan_addr->lun = msg->rsp[9] & 3;
4238 			lan_addr->channel = msg->rsp[3] & 0xf;
4239 			lan_addr->privilege = msg->rsp[3] >> 4;
4240 
4241 			/*
4242 			 * Extract the rest of the message information
4243 			 * from the IPMB header.
4244 			 */
4245 			recv_msg->user = user;
4246 			recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
4247 			recv_msg->msgid = msg->rsp[9] >> 2;
4248 			recv_msg->msg.netfn = msg->rsp[6] >> 2;
4249 			recv_msg->msg.cmd = msg->rsp[10];
4250 			recv_msg->msg.data = recv_msg->msg_data;
4251 
4252 			/*
4253 			 * We chop off 12, not 11 bytes because the checksum
4254 			 * at the end also needs to be removed.
4255 			 */
4256 			recv_msg->msg.data_len = msg->rsp_size - 12;
4257 			memcpy(recv_msg->msg_data, &msg->rsp[11],
4258 			       msg->rsp_size - 12);
4259 			if (deliver_response(intf, recv_msg))
4260 				ipmi_inc_stat(intf, unhandled_commands);
4261 			else
4262 				ipmi_inc_stat(intf, handled_commands);
4263 		}
4264 	}
4265 
4266 	return rv;
4267 }
4268 
4269 /*
4270  * This routine will handle "Get Message" command responses with
4271  * channels that use an OEM Medium. The message format belongs to
4272  * the OEM.  See IPMI 2.0 specification, Chapter 6 and
4273  * Chapter 22, sections 22.6 and 22.24 for more details.
4274  */
handle_oem_get_msg_cmd(struct ipmi_smi * intf,struct ipmi_smi_msg * msg)4275 static int handle_oem_get_msg_cmd(struct ipmi_smi *intf,
4276 				  struct ipmi_smi_msg *msg)
4277 {
4278 	struct cmd_rcvr       *rcvr;
4279 	int                   rv = 0;
4280 	unsigned char         netfn;
4281 	unsigned char         cmd;
4282 	unsigned char         chan;
4283 	struct ipmi_user *user = NULL;
4284 	struct ipmi_system_interface_addr *smi_addr;
4285 	struct ipmi_recv_msg  *recv_msg;
4286 
4287 	/*
4288 	 * We expect the OEM SW to perform error checking
4289 	 * so we just do some basic sanity checks
4290 	 */
4291 	if (msg->rsp_size < 4) {
4292 		/* Message not big enough, just ignore it. */
4293 		ipmi_inc_stat(intf, invalid_commands);
4294 		return 0;
4295 	}
4296 
4297 	if (msg->rsp[2] != 0) {
4298 		/* An error getting the response, just ignore it. */
4299 		return 0;
4300 	}
4301 
4302 	/*
4303 	 * This is an OEM Message so the OEM needs to know how
4304 	 * handle the message. We do no interpretation.
4305 	 */
4306 	netfn = msg->rsp[0] >> 2;
4307 	cmd = msg->rsp[1];
4308 	chan = msg->rsp[3] & 0xf;
4309 
4310 	rcu_read_lock();
4311 	rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
4312 	if (rcvr) {
4313 		user = rcvr->user;
4314 		kref_get(&user->refcount);
4315 	} else
4316 		user = NULL;
4317 	rcu_read_unlock();
4318 
4319 	if (user == NULL) {
4320 		/* We didn't find a user, just give up. */
4321 		ipmi_inc_stat(intf, unhandled_commands);
4322 
4323 		/*
4324 		 * Don't do anything with these messages, just allow
4325 		 * them to be freed.
4326 		 */
4327 
4328 		rv = 0;
4329 	} else {
4330 		recv_msg = ipmi_alloc_recv_msg();
4331 		if (!recv_msg) {
4332 			/*
4333 			 * We couldn't allocate memory for the
4334 			 * message, so requeue it for handling
4335 			 * later.
4336 			 */
4337 			rv = 1;
4338 			kref_put(&user->refcount, free_user);
4339 		} else {
4340 			/*
4341 			 * OEM Messages are expected to be delivered via
4342 			 * the system interface to SMS software.  We might
4343 			 * need to visit this again depending on OEM
4344 			 * requirements
4345 			 */
4346 			smi_addr = ((struct ipmi_system_interface_addr *)
4347 				    &recv_msg->addr);
4348 			smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
4349 			smi_addr->channel = IPMI_BMC_CHANNEL;
4350 			smi_addr->lun = msg->rsp[0] & 3;
4351 
4352 			recv_msg->user = user;
4353 			recv_msg->user_msg_data = NULL;
4354 			recv_msg->recv_type = IPMI_OEM_RECV_TYPE;
4355 			recv_msg->msg.netfn = msg->rsp[0] >> 2;
4356 			recv_msg->msg.cmd = msg->rsp[1];
4357 			recv_msg->msg.data = recv_msg->msg_data;
4358 
4359 			/*
4360 			 * The message starts at byte 4 which follows the
4361 			 * Channel Byte in the "GET MESSAGE" command
4362 			 */
4363 			recv_msg->msg.data_len = msg->rsp_size - 4;
4364 			memcpy(recv_msg->msg_data, &msg->rsp[4],
4365 			       msg->rsp_size - 4);
4366 			if (deliver_response(intf, recv_msg))
4367 				ipmi_inc_stat(intf, unhandled_commands);
4368 			else
4369 				ipmi_inc_stat(intf, handled_commands);
4370 		}
4371 	}
4372 
4373 	return rv;
4374 }
4375 
copy_event_into_recv_msg(struct ipmi_recv_msg * recv_msg,struct ipmi_smi_msg * msg)4376 static void copy_event_into_recv_msg(struct ipmi_recv_msg *recv_msg,
4377 				     struct ipmi_smi_msg  *msg)
4378 {
4379 	struct ipmi_system_interface_addr *smi_addr;
4380 
4381 	recv_msg->msgid = 0;
4382 	smi_addr = (struct ipmi_system_interface_addr *) &recv_msg->addr;
4383 	smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
4384 	smi_addr->channel = IPMI_BMC_CHANNEL;
4385 	smi_addr->lun = msg->rsp[0] & 3;
4386 	recv_msg->recv_type = IPMI_ASYNC_EVENT_RECV_TYPE;
4387 	recv_msg->msg.netfn = msg->rsp[0] >> 2;
4388 	recv_msg->msg.cmd = msg->rsp[1];
4389 	memcpy(recv_msg->msg_data, &msg->rsp[3], msg->rsp_size - 3);
4390 	recv_msg->msg.data = recv_msg->msg_data;
4391 	recv_msg->msg.data_len = msg->rsp_size - 3;
4392 }
4393 
handle_read_event_rsp(struct ipmi_smi * intf,struct ipmi_smi_msg * msg)4394 static int handle_read_event_rsp(struct ipmi_smi *intf,
4395 				 struct ipmi_smi_msg *msg)
4396 {
4397 	struct ipmi_recv_msg *recv_msg, *recv_msg2;
4398 	struct list_head     msgs;
4399 	struct ipmi_user     *user;
4400 	int rv = 0, deliver_count = 0, index;
4401 	unsigned long        flags;
4402 
4403 	if (msg->rsp_size < 19) {
4404 		/* Message is too small to be an IPMB event. */
4405 		ipmi_inc_stat(intf, invalid_events);
4406 		return 0;
4407 	}
4408 
4409 	if (msg->rsp[2] != 0) {
4410 		/* An error getting the event, just ignore it. */
4411 		return 0;
4412 	}
4413 
4414 	INIT_LIST_HEAD(&msgs);
4415 
4416 	spin_lock_irqsave(&intf->events_lock, flags);
4417 
4418 	ipmi_inc_stat(intf, events);
4419 
4420 	/*
4421 	 * Allocate and fill in one message for every user that is
4422 	 * getting events.
4423 	 */
4424 	index = srcu_read_lock(&intf->users_srcu);
4425 	list_for_each_entry_rcu(user, &intf->users, link) {
4426 		if (!user->gets_events)
4427 			continue;
4428 
4429 		recv_msg = ipmi_alloc_recv_msg();
4430 		if (!recv_msg) {
4431 			rcu_read_unlock();
4432 			list_for_each_entry_safe(recv_msg, recv_msg2, &msgs,
4433 						 link) {
4434 				list_del(&recv_msg->link);
4435 				ipmi_free_recv_msg(recv_msg);
4436 			}
4437 			/*
4438 			 * We couldn't allocate memory for the
4439 			 * message, so requeue it for handling
4440 			 * later.
4441 			 */
4442 			rv = 1;
4443 			goto out;
4444 		}
4445 
4446 		deliver_count++;
4447 
4448 		copy_event_into_recv_msg(recv_msg, msg);
4449 		recv_msg->user = user;
4450 		kref_get(&user->refcount);
4451 		list_add_tail(&recv_msg->link, &msgs);
4452 	}
4453 	srcu_read_unlock(&intf->users_srcu, index);
4454 
4455 	if (deliver_count) {
4456 		/* Now deliver all the messages. */
4457 		list_for_each_entry_safe(recv_msg, recv_msg2, &msgs, link) {
4458 			list_del(&recv_msg->link);
4459 			deliver_local_response(intf, recv_msg);
4460 		}
4461 	} else if (intf->waiting_events_count < MAX_EVENTS_IN_QUEUE) {
4462 		/*
4463 		 * No one to receive the message, put it in queue if there's
4464 		 * not already too many things in the queue.
4465 		 */
4466 		recv_msg = ipmi_alloc_recv_msg();
4467 		if (!recv_msg) {
4468 			/*
4469 			 * We couldn't allocate memory for the
4470 			 * message, so requeue it for handling
4471 			 * later.
4472 			 */
4473 			rv = 1;
4474 			goto out;
4475 		}
4476 
4477 		copy_event_into_recv_msg(recv_msg, msg);
4478 		list_add_tail(&recv_msg->link, &intf->waiting_events);
4479 		intf->waiting_events_count++;
4480 	} else if (!intf->event_msg_printed) {
4481 		/*
4482 		 * There's too many things in the queue, discard this
4483 		 * message.
4484 		 */
4485 		dev_warn(intf->si_dev,
4486 			 "Event queue full, discarding incoming events\n");
4487 		intf->event_msg_printed = 1;
4488 	}
4489 
4490  out:
4491 	spin_unlock_irqrestore(&intf->events_lock, flags);
4492 
4493 	return rv;
4494 }
4495 
handle_bmc_rsp(struct ipmi_smi * intf,struct ipmi_smi_msg * msg)4496 static int handle_bmc_rsp(struct ipmi_smi *intf,
4497 			  struct ipmi_smi_msg *msg)
4498 {
4499 	struct ipmi_recv_msg *recv_msg;
4500 	struct ipmi_system_interface_addr *smi_addr;
4501 
4502 	recv_msg = msg->user_data;
4503 	if (recv_msg == NULL) {
4504 		dev_warn(intf->si_dev,
4505 			 "IPMI SMI message received with no owner. This could be because of a malformed message, or because of a hardware error.  Contact your hardware vendor for assistance.\n");
4506 		return 0;
4507 	}
4508 
4509 	recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
4510 	recv_msg->msgid = msg->msgid;
4511 	smi_addr = ((struct ipmi_system_interface_addr *)
4512 		    &recv_msg->addr);
4513 	smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
4514 	smi_addr->channel = IPMI_BMC_CHANNEL;
4515 	smi_addr->lun = msg->rsp[0] & 3;
4516 	recv_msg->msg.netfn = msg->rsp[0] >> 2;
4517 	recv_msg->msg.cmd = msg->rsp[1];
4518 	memcpy(recv_msg->msg_data, &msg->rsp[2], msg->rsp_size - 2);
4519 	recv_msg->msg.data = recv_msg->msg_data;
4520 	recv_msg->msg.data_len = msg->rsp_size - 2;
4521 	deliver_local_response(intf, recv_msg);
4522 
4523 	return 0;
4524 }
4525 
4526 /*
4527  * Handle a received message.  Return 1 if the message should be requeued,
4528  * 0 if the message should be freed, or -1 if the message should not
4529  * be freed or requeued.
4530  */
handle_one_recv_msg(struct ipmi_smi * intf,struct ipmi_smi_msg * msg)4531 static int handle_one_recv_msg(struct ipmi_smi *intf,
4532 			       struct ipmi_smi_msg *msg)
4533 {
4534 	int requeue = 0;
4535 	int chan;
4536 	unsigned char cc;
4537 	bool is_cmd = !((msg->rsp[0] >> 2) & 1);
4538 
4539 	dev_dbg(intf->si_dev, "Recv: %*ph\n", msg->rsp_size, msg->rsp);
4540 
4541 	if (msg->rsp_size < 2) {
4542 		/* Message is too small to be correct. */
4543 		dev_warn(intf->si_dev,
4544 			 "BMC returned too small a message for netfn %x cmd %x, got %d bytes\n",
4545 			 (msg->data[0] >> 2) | 1, msg->data[1], msg->rsp_size);
4546 
4547 return_unspecified:
4548 		/* Generate an error response for the message. */
4549 		msg->rsp[0] = msg->data[0] | (1 << 2);
4550 		msg->rsp[1] = msg->data[1];
4551 		msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
4552 		msg->rsp_size = 3;
4553 	} else if (msg->type == IPMI_SMI_MSG_TYPE_IPMB_DIRECT) {
4554 		/* commands must have at least 4 bytes, responses 5. */
4555 		if (is_cmd && (msg->rsp_size < 4)) {
4556 			ipmi_inc_stat(intf, invalid_commands);
4557 			goto out;
4558 		}
4559 		if (!is_cmd && (msg->rsp_size < 5)) {
4560 			ipmi_inc_stat(intf, invalid_ipmb_responses);
4561 			/* Construct a valid error response. */
4562 			msg->rsp[0] = msg->data[0] & 0xfc; /* NetFN */
4563 			msg->rsp[0] |= (1 << 2); /* Make it a response */
4564 			msg->rsp[0] |= msg->data[2] & 3; /* rqLUN */
4565 			msg->rsp[1] = msg->data[1]; /* Addr */
4566 			msg->rsp[2] = msg->data[2] & 0xfc; /* rqSeq */
4567 			msg->rsp[2] |= msg->data[0] & 0x3; /* rsLUN */
4568 			msg->rsp[3] = msg->data[3]; /* Cmd */
4569 			msg->rsp[4] = IPMI_ERR_UNSPECIFIED;
4570 			msg->rsp_size = 5;
4571 		}
4572 	} else if ((msg->data_size >= 2)
4573 	    && (msg->data[0] == (IPMI_NETFN_APP_REQUEST << 2))
4574 	    && (msg->data[1] == IPMI_SEND_MSG_CMD)
4575 	    && (msg->user_data == NULL)) {
4576 
4577 		if (intf->in_shutdown)
4578 			goto out;
4579 
4580 		/*
4581 		 * This is the local response to a command send, start
4582 		 * the timer for these.  The user_data will not be
4583 		 * NULL if this is a response send, and we will let
4584 		 * response sends just go through.
4585 		 */
4586 
4587 		/*
4588 		 * Check for errors, if we get certain errors (ones
4589 		 * that mean basically we can try again later), we
4590 		 * ignore them and start the timer.  Otherwise we
4591 		 * report the error immediately.
4592 		 */
4593 		if ((msg->rsp_size >= 3) && (msg->rsp[2] != 0)
4594 		    && (msg->rsp[2] != IPMI_NODE_BUSY_ERR)
4595 		    && (msg->rsp[2] != IPMI_LOST_ARBITRATION_ERR)
4596 		    && (msg->rsp[2] != IPMI_BUS_ERR)
4597 		    && (msg->rsp[2] != IPMI_NAK_ON_WRITE_ERR)) {
4598 			int ch = msg->rsp[3] & 0xf;
4599 			struct ipmi_channel *chans;
4600 
4601 			/* Got an error sending the message, handle it. */
4602 
4603 			chans = READ_ONCE(intf->channel_list)->c;
4604 			if ((chans[ch].medium == IPMI_CHANNEL_MEDIUM_8023LAN)
4605 			    || (chans[ch].medium == IPMI_CHANNEL_MEDIUM_ASYNC))
4606 				ipmi_inc_stat(intf, sent_lan_command_errs);
4607 			else
4608 				ipmi_inc_stat(intf, sent_ipmb_command_errs);
4609 			intf_err_seq(intf, msg->msgid, msg->rsp[2]);
4610 		} else
4611 			/* The message was sent, start the timer. */
4612 			intf_start_seq_timer(intf, msg->msgid);
4613 		requeue = 0;
4614 		goto out;
4615 	} else if (((msg->rsp[0] >> 2) != ((msg->data[0] >> 2) | 1))
4616 		   || (msg->rsp[1] != msg->data[1])) {
4617 		/*
4618 		 * The NetFN and Command in the response is not even
4619 		 * marginally correct.
4620 		 */
4621 		dev_warn(intf->si_dev,
4622 			 "BMC returned incorrect response, expected netfn %x cmd %x, got netfn %x cmd %x\n",
4623 			 (msg->data[0] >> 2) | 1, msg->data[1],
4624 			 msg->rsp[0] >> 2, msg->rsp[1]);
4625 
4626 		goto return_unspecified;
4627 	}
4628 
4629 	if (msg->type == IPMI_SMI_MSG_TYPE_IPMB_DIRECT) {
4630 		if ((msg->data[0] >> 2) & 1) {
4631 			/* It's a response to a sent response. */
4632 			chan = 0;
4633 			cc = msg->rsp[4];
4634 			goto process_response_response;
4635 		}
4636 		if (is_cmd)
4637 			requeue = handle_ipmb_direct_rcv_cmd(intf, msg);
4638 		else
4639 			requeue = handle_ipmb_direct_rcv_rsp(intf, msg);
4640 	} else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
4641 		   && (msg->rsp[1] == IPMI_SEND_MSG_CMD)
4642 		   && (msg->user_data != NULL)) {
4643 		/*
4644 		 * It's a response to a response we sent.  For this we
4645 		 * deliver a send message response to the user.
4646 		 */
4647 		struct ipmi_recv_msg *recv_msg;
4648 
4649 		chan = msg->data[2] & 0x0f;
4650 		if (chan >= IPMI_MAX_CHANNELS)
4651 			/* Invalid channel number */
4652 			goto out;
4653 		cc = msg->rsp[2];
4654 
4655 process_response_response:
4656 		recv_msg = msg->user_data;
4657 
4658 		requeue = 0;
4659 		if (!recv_msg)
4660 			goto out;
4661 
4662 		recv_msg->recv_type = IPMI_RESPONSE_RESPONSE_TYPE;
4663 		recv_msg->msg.data = recv_msg->msg_data;
4664 		recv_msg->msg_data[0] = cc;
4665 		recv_msg->msg.data_len = 1;
4666 		deliver_local_response(intf, recv_msg);
4667 	} else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
4668 		   && (msg->rsp[1] == IPMI_GET_MSG_CMD)) {
4669 		struct ipmi_channel   *chans;
4670 
4671 		/* It's from the receive queue. */
4672 		chan = msg->rsp[3] & 0xf;
4673 		if (chan >= IPMI_MAX_CHANNELS) {
4674 			/* Invalid channel number */
4675 			requeue = 0;
4676 			goto out;
4677 		}
4678 
4679 		/*
4680 		 * We need to make sure the channels have been initialized.
4681 		 * The channel_handler routine will set the "curr_channel"
4682 		 * equal to or greater than IPMI_MAX_CHANNELS when all the
4683 		 * channels for this interface have been initialized.
4684 		 */
4685 		if (!intf->channels_ready) {
4686 			requeue = 0; /* Throw the message away */
4687 			goto out;
4688 		}
4689 
4690 		chans = READ_ONCE(intf->channel_list)->c;
4691 
4692 		switch (chans[chan].medium) {
4693 		case IPMI_CHANNEL_MEDIUM_IPMB:
4694 			if (msg->rsp[4] & 0x04) {
4695 				/*
4696 				 * It's a response, so find the
4697 				 * requesting message and send it up.
4698 				 */
4699 				requeue = handle_ipmb_get_msg_rsp(intf, msg);
4700 			} else {
4701 				/*
4702 				 * It's a command to the SMS from some other
4703 				 * entity.  Handle that.
4704 				 */
4705 				requeue = handle_ipmb_get_msg_cmd(intf, msg);
4706 			}
4707 			break;
4708 
4709 		case IPMI_CHANNEL_MEDIUM_8023LAN:
4710 		case IPMI_CHANNEL_MEDIUM_ASYNC:
4711 			if (msg->rsp[6] & 0x04) {
4712 				/*
4713 				 * It's a response, so find the
4714 				 * requesting message and send it up.
4715 				 */
4716 				requeue = handle_lan_get_msg_rsp(intf, msg);
4717 			} else {
4718 				/*
4719 				 * It's a command to the SMS from some other
4720 				 * entity.  Handle that.
4721 				 */
4722 				requeue = handle_lan_get_msg_cmd(intf, msg);
4723 			}
4724 			break;
4725 
4726 		default:
4727 			/* Check for OEM Channels.  Clients had better
4728 			   register for these commands. */
4729 			if ((chans[chan].medium >= IPMI_CHANNEL_MEDIUM_OEM_MIN)
4730 			    && (chans[chan].medium
4731 				<= IPMI_CHANNEL_MEDIUM_OEM_MAX)) {
4732 				requeue = handle_oem_get_msg_cmd(intf, msg);
4733 			} else {
4734 				/*
4735 				 * We don't handle the channel type, so just
4736 				 * free the message.
4737 				 */
4738 				requeue = 0;
4739 			}
4740 		}
4741 
4742 	} else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
4743 		   && (msg->rsp[1] == IPMI_READ_EVENT_MSG_BUFFER_CMD)) {
4744 		/* It's an asynchronous event. */
4745 		requeue = handle_read_event_rsp(intf, msg);
4746 	} else {
4747 		/* It's a response from the local BMC. */
4748 		requeue = handle_bmc_rsp(intf, msg);
4749 	}
4750 
4751  out:
4752 	return requeue;
4753 }
4754 
4755 /*
4756  * If there are messages in the queue or pretimeouts, handle them.
4757  */
handle_new_recv_msgs(struct ipmi_smi * intf)4758 static void handle_new_recv_msgs(struct ipmi_smi *intf)
4759 {
4760 	struct ipmi_smi_msg  *smi_msg;
4761 	unsigned long        flags = 0;
4762 	int                  rv;
4763 	int                  run_to_completion = intf->run_to_completion;
4764 
4765 	/* See if any waiting messages need to be processed. */
4766 	if (!run_to_completion)
4767 		spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags);
4768 	while (!list_empty(&intf->waiting_rcv_msgs)) {
4769 		smi_msg = list_entry(intf->waiting_rcv_msgs.next,
4770 				     struct ipmi_smi_msg, link);
4771 		list_del(&smi_msg->link);
4772 		if (!run_to_completion)
4773 			spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock,
4774 					       flags);
4775 		rv = handle_one_recv_msg(intf, smi_msg);
4776 		if (!run_to_completion)
4777 			spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags);
4778 		if (rv > 0) {
4779 			/*
4780 			 * To preserve message order, quit if we
4781 			 * can't handle a message.  Add the message
4782 			 * back at the head, this is safe because this
4783 			 * tasklet is the only thing that pulls the
4784 			 * messages.
4785 			 */
4786 			list_add(&smi_msg->link, &intf->waiting_rcv_msgs);
4787 			break;
4788 		} else {
4789 			if (rv == 0)
4790 				/* Message handled */
4791 				ipmi_free_smi_msg(smi_msg);
4792 			/* If rv < 0, fatal error, del but don't free. */
4793 		}
4794 	}
4795 	if (!run_to_completion)
4796 		spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock, flags);
4797 
4798 	/*
4799 	 * If the pretimout count is non-zero, decrement one from it and
4800 	 * deliver pretimeouts to all the users.
4801 	 */
4802 	if (atomic_add_unless(&intf->watchdog_pretimeouts_to_deliver, -1, 0)) {
4803 		struct ipmi_user *user;
4804 		int index;
4805 
4806 		index = srcu_read_lock(&intf->users_srcu);
4807 		list_for_each_entry_rcu(user, &intf->users, link) {
4808 			if (user->handler->ipmi_watchdog_pretimeout)
4809 				user->handler->ipmi_watchdog_pretimeout(
4810 					user->handler_data);
4811 		}
4812 		srcu_read_unlock(&intf->users_srcu, index);
4813 	}
4814 }
4815 
smi_recv_tasklet(struct tasklet_struct * t)4816 static void smi_recv_tasklet(struct tasklet_struct *t)
4817 {
4818 	unsigned long flags = 0; /* keep us warning-free. */
4819 	struct ipmi_smi *intf = from_tasklet(intf, t, recv_tasklet);
4820 	int run_to_completion = intf->run_to_completion;
4821 	struct ipmi_smi_msg *newmsg = NULL;
4822 
4823 	/*
4824 	 * Start the next message if available.
4825 	 *
4826 	 * Do this here, not in the actual receiver, because we may deadlock
4827 	 * because the lower layer is allowed to hold locks while calling
4828 	 * message delivery.
4829 	 */
4830 
4831 	rcu_read_lock();
4832 
4833 	if (!run_to_completion)
4834 		spin_lock_irqsave(&intf->xmit_msgs_lock, flags);
4835 	if (intf->curr_msg == NULL && !intf->in_shutdown) {
4836 		struct list_head *entry = NULL;
4837 
4838 		/* Pick the high priority queue first. */
4839 		if (!list_empty(&intf->hp_xmit_msgs))
4840 			entry = intf->hp_xmit_msgs.next;
4841 		else if (!list_empty(&intf->xmit_msgs))
4842 			entry = intf->xmit_msgs.next;
4843 
4844 		if (entry) {
4845 			list_del(entry);
4846 			newmsg = list_entry(entry, struct ipmi_smi_msg, link);
4847 			intf->curr_msg = newmsg;
4848 		}
4849 	}
4850 
4851 	if (!run_to_completion)
4852 		spin_unlock_irqrestore(&intf->xmit_msgs_lock, flags);
4853 	if (newmsg)
4854 		intf->handlers->sender(intf->send_info, newmsg);
4855 
4856 	rcu_read_unlock();
4857 
4858 	handle_new_recv_msgs(intf);
4859 }
4860 
4861 /* Handle a new message from the lower layer. */
ipmi_smi_msg_received(struct ipmi_smi * intf,struct ipmi_smi_msg * msg)4862 void ipmi_smi_msg_received(struct ipmi_smi *intf,
4863 			   struct ipmi_smi_msg *msg)
4864 {
4865 	unsigned long flags = 0; /* keep us warning-free. */
4866 	int run_to_completion = intf->run_to_completion;
4867 
4868 	/*
4869 	 * To preserve message order, we keep a queue and deliver from
4870 	 * a tasklet.
4871 	 */
4872 	if (!run_to_completion)
4873 		spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags);
4874 	list_add_tail(&msg->link, &intf->waiting_rcv_msgs);
4875 	if (!run_to_completion)
4876 		spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock,
4877 				       flags);
4878 
4879 	if (!run_to_completion)
4880 		spin_lock_irqsave(&intf->xmit_msgs_lock, flags);
4881 	/*
4882 	 * We can get an asynchronous event or receive message in addition
4883 	 * to commands we send.
4884 	 */
4885 	if (msg == intf->curr_msg)
4886 		intf->curr_msg = NULL;
4887 	if (!run_to_completion)
4888 		spin_unlock_irqrestore(&intf->xmit_msgs_lock, flags);
4889 
4890 	if (run_to_completion)
4891 		smi_recv_tasklet(&intf->recv_tasklet);
4892 	else
4893 		tasklet_schedule(&intf->recv_tasklet);
4894 }
4895 EXPORT_SYMBOL(ipmi_smi_msg_received);
4896 
ipmi_smi_watchdog_pretimeout(struct ipmi_smi * intf)4897 void ipmi_smi_watchdog_pretimeout(struct ipmi_smi *intf)
4898 {
4899 	if (intf->in_shutdown)
4900 		return;
4901 
4902 	atomic_set(&intf->watchdog_pretimeouts_to_deliver, 1);
4903 	tasklet_schedule(&intf->recv_tasklet);
4904 }
4905 EXPORT_SYMBOL(ipmi_smi_watchdog_pretimeout);
4906 
4907 static struct ipmi_smi_msg *
smi_from_recv_msg(struct ipmi_smi * intf,struct ipmi_recv_msg * recv_msg,unsigned char seq,long seqid)4908 smi_from_recv_msg(struct ipmi_smi *intf, struct ipmi_recv_msg *recv_msg,
4909 		  unsigned char seq, long seqid)
4910 {
4911 	struct ipmi_smi_msg *smi_msg = ipmi_alloc_smi_msg();
4912 	if (!smi_msg)
4913 		/*
4914 		 * If we can't allocate the message, then just return, we
4915 		 * get 4 retries, so this should be ok.
4916 		 */
4917 		return NULL;
4918 
4919 	memcpy(smi_msg->data, recv_msg->msg.data, recv_msg->msg.data_len);
4920 	smi_msg->data_size = recv_msg->msg.data_len;
4921 	smi_msg->msgid = STORE_SEQ_IN_MSGID(seq, seqid);
4922 
4923 	dev_dbg(intf->si_dev, "Resend: %*ph\n",
4924 		smi_msg->data_size, smi_msg->data);
4925 
4926 	return smi_msg;
4927 }
4928 
check_msg_timeout(struct ipmi_smi * intf,struct seq_table * ent,struct list_head * timeouts,unsigned long timeout_period,int slot,unsigned long * flags,bool * need_timer)4929 static void check_msg_timeout(struct ipmi_smi *intf, struct seq_table *ent,
4930 			      struct list_head *timeouts,
4931 			      unsigned long timeout_period,
4932 			      int slot, unsigned long *flags,
4933 			      bool *need_timer)
4934 {
4935 	struct ipmi_recv_msg *msg;
4936 
4937 	if (intf->in_shutdown)
4938 		return;
4939 
4940 	if (!ent->inuse)
4941 		return;
4942 
4943 	if (timeout_period < ent->timeout) {
4944 		ent->timeout -= timeout_period;
4945 		*need_timer = true;
4946 		return;
4947 	}
4948 
4949 	if (ent->retries_left == 0) {
4950 		/* The message has used all its retries. */
4951 		ent->inuse = 0;
4952 		smi_remove_watch(intf, IPMI_WATCH_MASK_CHECK_MESSAGES);
4953 		msg = ent->recv_msg;
4954 		list_add_tail(&msg->link, timeouts);
4955 		if (ent->broadcast)
4956 			ipmi_inc_stat(intf, timed_out_ipmb_broadcasts);
4957 		else if (is_lan_addr(&ent->recv_msg->addr))
4958 			ipmi_inc_stat(intf, timed_out_lan_commands);
4959 		else
4960 			ipmi_inc_stat(intf, timed_out_ipmb_commands);
4961 	} else {
4962 		struct ipmi_smi_msg *smi_msg;
4963 		/* More retries, send again. */
4964 
4965 		*need_timer = true;
4966 
4967 		/*
4968 		 * Start with the max timer, set to normal timer after
4969 		 * the message is sent.
4970 		 */
4971 		ent->timeout = MAX_MSG_TIMEOUT;
4972 		ent->retries_left--;
4973 		smi_msg = smi_from_recv_msg(intf, ent->recv_msg, slot,
4974 					    ent->seqid);
4975 		if (!smi_msg) {
4976 			if (is_lan_addr(&ent->recv_msg->addr))
4977 				ipmi_inc_stat(intf,
4978 					      dropped_rexmit_lan_commands);
4979 			else
4980 				ipmi_inc_stat(intf,
4981 					      dropped_rexmit_ipmb_commands);
4982 			return;
4983 		}
4984 
4985 		spin_unlock_irqrestore(&intf->seq_lock, *flags);
4986 
4987 		/*
4988 		 * Send the new message.  We send with a zero
4989 		 * priority.  It timed out, I doubt time is that
4990 		 * critical now, and high priority messages are really
4991 		 * only for messages to the local MC, which don't get
4992 		 * resent.
4993 		 */
4994 		if (intf->handlers) {
4995 			if (is_lan_addr(&ent->recv_msg->addr))
4996 				ipmi_inc_stat(intf,
4997 					      retransmitted_lan_commands);
4998 			else
4999 				ipmi_inc_stat(intf,
5000 					      retransmitted_ipmb_commands);
5001 
5002 			smi_send(intf, intf->handlers, smi_msg, 0);
5003 		} else
5004 			ipmi_free_smi_msg(smi_msg);
5005 
5006 		spin_lock_irqsave(&intf->seq_lock, *flags);
5007 	}
5008 }
5009 
ipmi_timeout_handler(struct ipmi_smi * intf,unsigned long timeout_period)5010 static bool ipmi_timeout_handler(struct ipmi_smi *intf,
5011 				 unsigned long timeout_period)
5012 {
5013 	struct list_head     timeouts;
5014 	struct ipmi_recv_msg *msg, *msg2;
5015 	unsigned long        flags;
5016 	int                  i;
5017 	bool                 need_timer = false;
5018 
5019 	if (!intf->bmc_registered) {
5020 		kref_get(&intf->refcount);
5021 		if (!schedule_work(&intf->bmc_reg_work)) {
5022 			kref_put(&intf->refcount, intf_free);
5023 			need_timer = true;
5024 		}
5025 	}
5026 
5027 	/*
5028 	 * Go through the seq table and find any messages that
5029 	 * have timed out, putting them in the timeouts
5030 	 * list.
5031 	 */
5032 	INIT_LIST_HEAD(&timeouts);
5033 	spin_lock_irqsave(&intf->seq_lock, flags);
5034 	if (intf->ipmb_maintenance_mode_timeout) {
5035 		if (intf->ipmb_maintenance_mode_timeout <= timeout_period)
5036 			intf->ipmb_maintenance_mode_timeout = 0;
5037 		else
5038 			intf->ipmb_maintenance_mode_timeout -= timeout_period;
5039 	}
5040 	for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++)
5041 		check_msg_timeout(intf, &intf->seq_table[i],
5042 				  &timeouts, timeout_period, i,
5043 				  &flags, &need_timer);
5044 	spin_unlock_irqrestore(&intf->seq_lock, flags);
5045 
5046 	list_for_each_entry_safe(msg, msg2, &timeouts, link)
5047 		deliver_err_response(intf, msg, IPMI_TIMEOUT_COMPLETION_CODE);
5048 
5049 	/*
5050 	 * Maintenance mode handling.  Check the timeout
5051 	 * optimistically before we claim the lock.  It may
5052 	 * mean a timeout gets missed occasionally, but that
5053 	 * only means the timeout gets extended by one period
5054 	 * in that case.  No big deal, and it avoids the lock
5055 	 * most of the time.
5056 	 */
5057 	if (intf->auto_maintenance_timeout > 0) {
5058 		spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
5059 		if (intf->auto_maintenance_timeout > 0) {
5060 			intf->auto_maintenance_timeout
5061 				-= timeout_period;
5062 			if (!intf->maintenance_mode
5063 			    && (intf->auto_maintenance_timeout <= 0)) {
5064 				intf->maintenance_mode_enable = false;
5065 				maintenance_mode_update(intf);
5066 			}
5067 		}
5068 		spin_unlock_irqrestore(&intf->maintenance_mode_lock,
5069 				       flags);
5070 	}
5071 
5072 	tasklet_schedule(&intf->recv_tasklet);
5073 
5074 	return need_timer;
5075 }
5076 
ipmi_request_event(struct ipmi_smi * intf)5077 static void ipmi_request_event(struct ipmi_smi *intf)
5078 {
5079 	/* No event requests when in maintenance mode. */
5080 	if (intf->maintenance_mode_enable)
5081 		return;
5082 
5083 	if (!intf->in_shutdown)
5084 		intf->handlers->request_events(intf->send_info);
5085 }
5086 
5087 static struct timer_list ipmi_timer;
5088 
5089 static atomic_t stop_operation;
5090 
ipmi_timeout(struct timer_list * unused)5091 static void ipmi_timeout(struct timer_list *unused)
5092 {
5093 	struct ipmi_smi *intf;
5094 	bool need_timer = false;
5095 	int index;
5096 
5097 	if (atomic_read(&stop_operation))
5098 		return;
5099 
5100 	index = srcu_read_lock(&ipmi_interfaces_srcu);
5101 	list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
5102 		if (atomic_read(&intf->event_waiters)) {
5103 			intf->ticks_to_req_ev--;
5104 			if (intf->ticks_to_req_ev == 0) {
5105 				ipmi_request_event(intf);
5106 				intf->ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
5107 			}
5108 			need_timer = true;
5109 		}
5110 
5111 		need_timer |= ipmi_timeout_handler(intf, IPMI_TIMEOUT_TIME);
5112 	}
5113 	srcu_read_unlock(&ipmi_interfaces_srcu, index);
5114 
5115 	if (need_timer)
5116 		mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
5117 }
5118 
need_waiter(struct ipmi_smi * intf)5119 static void need_waiter(struct ipmi_smi *intf)
5120 {
5121 	/* Racy, but worst case we start the timer twice. */
5122 	if (!timer_pending(&ipmi_timer))
5123 		mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
5124 }
5125 
5126 static atomic_t smi_msg_inuse_count = ATOMIC_INIT(0);
5127 static atomic_t recv_msg_inuse_count = ATOMIC_INIT(0);
5128 
free_smi_msg(struct ipmi_smi_msg * msg)5129 static void free_smi_msg(struct ipmi_smi_msg *msg)
5130 {
5131 	atomic_dec(&smi_msg_inuse_count);
5132 	/* Try to keep as much stuff out of the panic path as possible. */
5133 	if (!oops_in_progress)
5134 		kfree(msg);
5135 }
5136 
ipmi_alloc_smi_msg(void)5137 struct ipmi_smi_msg *ipmi_alloc_smi_msg(void)
5138 {
5139 	struct ipmi_smi_msg *rv;
5140 	rv = kmalloc(sizeof(struct ipmi_smi_msg), GFP_ATOMIC);
5141 	if (rv) {
5142 		rv->done = free_smi_msg;
5143 		rv->user_data = NULL;
5144 		rv->type = IPMI_SMI_MSG_TYPE_NORMAL;
5145 		atomic_inc(&smi_msg_inuse_count);
5146 	}
5147 	return rv;
5148 }
5149 EXPORT_SYMBOL(ipmi_alloc_smi_msg);
5150 
free_recv_msg(struct ipmi_recv_msg * msg)5151 static void free_recv_msg(struct ipmi_recv_msg *msg)
5152 {
5153 	atomic_dec(&recv_msg_inuse_count);
5154 	/* Try to keep as much stuff out of the panic path as possible. */
5155 	if (!oops_in_progress)
5156 		kfree(msg);
5157 }
5158 
ipmi_alloc_recv_msg(void)5159 static struct ipmi_recv_msg *ipmi_alloc_recv_msg(void)
5160 {
5161 	struct ipmi_recv_msg *rv;
5162 
5163 	rv = kmalloc(sizeof(struct ipmi_recv_msg), GFP_ATOMIC);
5164 	if (rv) {
5165 		rv->user = NULL;
5166 		rv->done = free_recv_msg;
5167 		atomic_inc(&recv_msg_inuse_count);
5168 	}
5169 	return rv;
5170 }
5171 
ipmi_free_recv_msg(struct ipmi_recv_msg * msg)5172 void ipmi_free_recv_msg(struct ipmi_recv_msg *msg)
5173 {
5174 	if (msg->user && !oops_in_progress)
5175 		kref_put(&msg->user->refcount, free_user);
5176 	msg->done(msg);
5177 }
5178 EXPORT_SYMBOL(ipmi_free_recv_msg);
5179 
5180 static atomic_t panic_done_count = ATOMIC_INIT(0);
5181 
dummy_smi_done_handler(struct ipmi_smi_msg * msg)5182 static void dummy_smi_done_handler(struct ipmi_smi_msg *msg)
5183 {
5184 	atomic_dec(&panic_done_count);
5185 }
5186 
dummy_recv_done_handler(struct ipmi_recv_msg * msg)5187 static void dummy_recv_done_handler(struct ipmi_recv_msg *msg)
5188 {
5189 	atomic_dec(&panic_done_count);
5190 }
5191 
5192 /*
5193  * Inside a panic, send a message and wait for a response.
5194  */
ipmi_panic_request_and_wait(struct ipmi_smi * intf,struct ipmi_addr * addr,struct kernel_ipmi_msg * msg)5195 static void ipmi_panic_request_and_wait(struct ipmi_smi *intf,
5196 					struct ipmi_addr *addr,
5197 					struct kernel_ipmi_msg *msg)
5198 {
5199 	struct ipmi_smi_msg  smi_msg;
5200 	struct ipmi_recv_msg recv_msg;
5201 	int rv;
5202 
5203 	smi_msg.done = dummy_smi_done_handler;
5204 	recv_msg.done = dummy_recv_done_handler;
5205 	atomic_add(2, &panic_done_count);
5206 	rv = i_ipmi_request(NULL,
5207 			    intf,
5208 			    addr,
5209 			    0,
5210 			    msg,
5211 			    intf,
5212 			    &smi_msg,
5213 			    &recv_msg,
5214 			    0,
5215 			    intf->addrinfo[0].address,
5216 			    intf->addrinfo[0].lun,
5217 			    0, 1); /* Don't retry, and don't wait. */
5218 	if (rv)
5219 		atomic_sub(2, &panic_done_count);
5220 	else if (intf->handlers->flush_messages)
5221 		intf->handlers->flush_messages(intf->send_info);
5222 
5223 	while (atomic_read(&panic_done_count) != 0)
5224 		ipmi_poll(intf);
5225 }
5226 
event_receiver_fetcher(struct ipmi_smi * intf,struct ipmi_recv_msg * msg)5227 static void event_receiver_fetcher(struct ipmi_smi *intf,
5228 				   struct ipmi_recv_msg *msg)
5229 {
5230 	if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
5231 	    && (msg->msg.netfn == IPMI_NETFN_SENSOR_EVENT_RESPONSE)
5232 	    && (msg->msg.cmd == IPMI_GET_EVENT_RECEIVER_CMD)
5233 	    && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) {
5234 		/* A get event receiver command, save it. */
5235 		intf->event_receiver = msg->msg.data[1];
5236 		intf->event_receiver_lun = msg->msg.data[2] & 0x3;
5237 	}
5238 }
5239 
device_id_fetcher(struct ipmi_smi * intf,struct ipmi_recv_msg * msg)5240 static void device_id_fetcher(struct ipmi_smi *intf, struct ipmi_recv_msg *msg)
5241 {
5242 	if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
5243 	    && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
5244 	    && (msg->msg.cmd == IPMI_GET_DEVICE_ID_CMD)
5245 	    && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) {
5246 		/*
5247 		 * A get device id command, save if we are an event
5248 		 * receiver or generator.
5249 		 */
5250 		intf->local_sel_device = (msg->msg.data[6] >> 2) & 1;
5251 		intf->local_event_generator = (msg->msg.data[6] >> 5) & 1;
5252 	}
5253 }
5254 
send_panic_events(struct ipmi_smi * intf,char * str)5255 static void send_panic_events(struct ipmi_smi *intf, char *str)
5256 {
5257 	struct kernel_ipmi_msg msg;
5258 	unsigned char data[16];
5259 	struct ipmi_system_interface_addr *si;
5260 	struct ipmi_addr addr;
5261 	char *p = str;
5262 	struct ipmi_ipmb_addr *ipmb;
5263 	int j;
5264 
5265 	if (ipmi_send_panic_event == IPMI_SEND_PANIC_EVENT_NONE)
5266 		return;
5267 
5268 	si = (struct ipmi_system_interface_addr *) &addr;
5269 	si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
5270 	si->channel = IPMI_BMC_CHANNEL;
5271 	si->lun = 0;
5272 
5273 	/* Fill in an event telling that we have failed. */
5274 	msg.netfn = 0x04; /* Sensor or Event. */
5275 	msg.cmd = 2; /* Platform event command. */
5276 	msg.data = data;
5277 	msg.data_len = 8;
5278 	data[0] = 0x41; /* Kernel generator ID, IPMI table 5-4 */
5279 	data[1] = 0x03; /* This is for IPMI 1.0. */
5280 	data[2] = 0x20; /* OS Critical Stop, IPMI table 36-3 */
5281 	data[4] = 0x6f; /* Sensor specific, IPMI table 36-1 */
5282 	data[5] = 0xa1; /* Runtime stop OEM bytes 2 & 3. */
5283 
5284 	/*
5285 	 * Put a few breadcrumbs in.  Hopefully later we can add more things
5286 	 * to make the panic events more useful.
5287 	 */
5288 	if (str) {
5289 		data[3] = str[0];
5290 		data[6] = str[1];
5291 		data[7] = str[2];
5292 	}
5293 
5294 	/* Send the event announcing the panic. */
5295 	ipmi_panic_request_and_wait(intf, &addr, &msg);
5296 
5297 	/*
5298 	 * On every interface, dump a bunch of OEM event holding the
5299 	 * string.
5300 	 */
5301 	if (ipmi_send_panic_event != IPMI_SEND_PANIC_EVENT_STRING || !str)
5302 		return;
5303 
5304 	/*
5305 	 * intf_num is used as an marker to tell if the
5306 	 * interface is valid.  Thus we need a read barrier to
5307 	 * make sure data fetched before checking intf_num
5308 	 * won't be used.
5309 	 */
5310 	smp_rmb();
5311 
5312 	/*
5313 	 * First job here is to figure out where to send the
5314 	 * OEM events.  There's no way in IPMI to send OEM
5315 	 * events using an event send command, so we have to
5316 	 * find the SEL to put them in and stick them in
5317 	 * there.
5318 	 */
5319 
5320 	/* Get capabilities from the get device id. */
5321 	intf->local_sel_device = 0;
5322 	intf->local_event_generator = 0;
5323 	intf->event_receiver = 0;
5324 
5325 	/* Request the device info from the local MC. */
5326 	msg.netfn = IPMI_NETFN_APP_REQUEST;
5327 	msg.cmd = IPMI_GET_DEVICE_ID_CMD;
5328 	msg.data = NULL;
5329 	msg.data_len = 0;
5330 	intf->null_user_handler = device_id_fetcher;
5331 	ipmi_panic_request_and_wait(intf, &addr, &msg);
5332 
5333 	if (intf->local_event_generator) {
5334 		/* Request the event receiver from the local MC. */
5335 		msg.netfn = IPMI_NETFN_SENSOR_EVENT_REQUEST;
5336 		msg.cmd = IPMI_GET_EVENT_RECEIVER_CMD;
5337 		msg.data = NULL;
5338 		msg.data_len = 0;
5339 		intf->null_user_handler = event_receiver_fetcher;
5340 		ipmi_panic_request_and_wait(intf, &addr, &msg);
5341 	}
5342 	intf->null_user_handler = NULL;
5343 
5344 	/*
5345 	 * Validate the event receiver.  The low bit must not
5346 	 * be 1 (it must be a valid IPMB address), it cannot
5347 	 * be zero, and it must not be my address.
5348 	 */
5349 	if (((intf->event_receiver & 1) == 0)
5350 	    && (intf->event_receiver != 0)
5351 	    && (intf->event_receiver != intf->addrinfo[0].address)) {
5352 		/*
5353 		 * The event receiver is valid, send an IPMB
5354 		 * message.
5355 		 */
5356 		ipmb = (struct ipmi_ipmb_addr *) &addr;
5357 		ipmb->addr_type = IPMI_IPMB_ADDR_TYPE;
5358 		ipmb->channel = 0; /* FIXME - is this right? */
5359 		ipmb->lun = intf->event_receiver_lun;
5360 		ipmb->slave_addr = intf->event_receiver;
5361 	} else if (intf->local_sel_device) {
5362 		/*
5363 		 * The event receiver was not valid (or was
5364 		 * me), but I am an SEL device, just dump it
5365 		 * in my SEL.
5366 		 */
5367 		si = (struct ipmi_system_interface_addr *) &addr;
5368 		si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
5369 		si->channel = IPMI_BMC_CHANNEL;
5370 		si->lun = 0;
5371 	} else
5372 		return; /* No where to send the event. */
5373 
5374 	msg.netfn = IPMI_NETFN_STORAGE_REQUEST; /* Storage. */
5375 	msg.cmd = IPMI_ADD_SEL_ENTRY_CMD;
5376 	msg.data = data;
5377 	msg.data_len = 16;
5378 
5379 	j = 0;
5380 	while (*p) {
5381 		int size = strlen(p);
5382 
5383 		if (size > 11)
5384 			size = 11;
5385 		data[0] = 0;
5386 		data[1] = 0;
5387 		data[2] = 0xf0; /* OEM event without timestamp. */
5388 		data[3] = intf->addrinfo[0].address;
5389 		data[4] = j++; /* sequence # */
5390 		/*
5391 		 * Always give 11 bytes, so strncpy will fill
5392 		 * it with zeroes for me.
5393 		 */
5394 		strncpy(data+5, p, 11);
5395 		p += size;
5396 
5397 		ipmi_panic_request_and_wait(intf, &addr, &msg);
5398 	}
5399 }
5400 
5401 static int has_panicked;
5402 
panic_event(struct notifier_block * this,unsigned long event,void * ptr)5403 static int panic_event(struct notifier_block *this,
5404 		       unsigned long         event,
5405 		       void                  *ptr)
5406 {
5407 	struct ipmi_smi *intf;
5408 	struct ipmi_user *user;
5409 
5410 	if (has_panicked)
5411 		return NOTIFY_DONE;
5412 	has_panicked = 1;
5413 
5414 	/* For every registered interface, set it to run to completion. */
5415 	list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
5416 		if (!intf->handlers || intf->intf_num == -1)
5417 			/* Interface is not ready. */
5418 			continue;
5419 
5420 		if (!intf->handlers->poll)
5421 			continue;
5422 
5423 		/*
5424 		 * If we were interrupted while locking xmit_msgs_lock or
5425 		 * waiting_rcv_msgs_lock, the corresponding list may be
5426 		 * corrupted.  In this case, drop items on the list for
5427 		 * the safety.
5428 		 */
5429 		if (!spin_trylock(&intf->xmit_msgs_lock)) {
5430 			INIT_LIST_HEAD(&intf->xmit_msgs);
5431 			INIT_LIST_HEAD(&intf->hp_xmit_msgs);
5432 		} else
5433 			spin_unlock(&intf->xmit_msgs_lock);
5434 
5435 		if (!spin_trylock(&intf->waiting_rcv_msgs_lock))
5436 			INIT_LIST_HEAD(&intf->waiting_rcv_msgs);
5437 		else
5438 			spin_unlock(&intf->waiting_rcv_msgs_lock);
5439 
5440 		intf->run_to_completion = 1;
5441 		if (intf->handlers->set_run_to_completion)
5442 			intf->handlers->set_run_to_completion(intf->send_info,
5443 							      1);
5444 
5445 		list_for_each_entry_rcu(user, &intf->users, link) {
5446 			if (user->handler->ipmi_panic_handler)
5447 				user->handler->ipmi_panic_handler(
5448 					user->handler_data);
5449 		}
5450 
5451 		send_panic_events(intf, ptr);
5452 	}
5453 
5454 	return NOTIFY_DONE;
5455 }
5456 
5457 /* Must be called with ipmi_interfaces_mutex held. */
ipmi_register_driver(void)5458 static int ipmi_register_driver(void)
5459 {
5460 	int rv;
5461 
5462 	if (drvregistered)
5463 		return 0;
5464 
5465 	rv = driver_register(&ipmidriver.driver);
5466 	if (rv)
5467 		pr_err("Could not register IPMI driver\n");
5468 	else
5469 		drvregistered = true;
5470 	return rv;
5471 }
5472 
5473 static struct notifier_block panic_block = {
5474 	.notifier_call	= panic_event,
5475 	.next		= NULL,
5476 	.priority	= 200	/* priority: INT_MAX >= x >= 0 */
5477 };
5478 
ipmi_init_msghandler(void)5479 static int ipmi_init_msghandler(void)
5480 {
5481 	int rv;
5482 
5483 	mutex_lock(&ipmi_interfaces_mutex);
5484 	rv = ipmi_register_driver();
5485 	if (rv)
5486 		goto out;
5487 	if (initialized)
5488 		goto out;
5489 
5490 	rv = init_srcu_struct(&ipmi_interfaces_srcu);
5491 	if (rv)
5492 		goto out;
5493 
5494 	remove_work_wq = create_singlethread_workqueue("ipmi-msghandler-remove-wq");
5495 	if (!remove_work_wq) {
5496 		pr_err("unable to create ipmi-msghandler-remove-wq workqueue");
5497 		rv = -ENOMEM;
5498 		goto out_wq;
5499 	}
5500 
5501 	timer_setup(&ipmi_timer, ipmi_timeout, 0);
5502 	mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
5503 
5504 	atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
5505 
5506 	initialized = true;
5507 
5508 out_wq:
5509 	if (rv)
5510 		cleanup_srcu_struct(&ipmi_interfaces_srcu);
5511 out:
5512 	mutex_unlock(&ipmi_interfaces_mutex);
5513 	return rv;
5514 }
5515 
ipmi_init_msghandler_mod(void)5516 static int __init ipmi_init_msghandler_mod(void)
5517 {
5518 	int rv;
5519 
5520 	pr_info("version " IPMI_DRIVER_VERSION "\n");
5521 
5522 	mutex_lock(&ipmi_interfaces_mutex);
5523 	rv = ipmi_register_driver();
5524 	mutex_unlock(&ipmi_interfaces_mutex);
5525 
5526 	return rv;
5527 }
5528 
cleanup_ipmi(void)5529 static void __exit cleanup_ipmi(void)
5530 {
5531 	int count;
5532 
5533 	if (initialized) {
5534 		destroy_workqueue(remove_work_wq);
5535 
5536 		atomic_notifier_chain_unregister(&panic_notifier_list,
5537 						 &panic_block);
5538 
5539 		/*
5540 		 * This can't be called if any interfaces exist, so no worry
5541 		 * about shutting down the interfaces.
5542 		 */
5543 
5544 		/*
5545 		 * Tell the timer to stop, then wait for it to stop.  This
5546 		 * avoids problems with race conditions removing the timer
5547 		 * here.
5548 		 */
5549 		atomic_set(&stop_operation, 1);
5550 		del_timer_sync(&ipmi_timer);
5551 
5552 		initialized = false;
5553 
5554 		/* Check for buffer leaks. */
5555 		count = atomic_read(&smi_msg_inuse_count);
5556 		if (count != 0)
5557 			pr_warn("SMI message count %d at exit\n", count);
5558 		count = atomic_read(&recv_msg_inuse_count);
5559 		if (count != 0)
5560 			pr_warn("recv message count %d at exit\n", count);
5561 
5562 		cleanup_srcu_struct(&ipmi_interfaces_srcu);
5563 	}
5564 	if (drvregistered)
5565 		driver_unregister(&ipmidriver.driver);
5566 }
5567 module_exit(cleanup_ipmi);
5568 
5569 module_init(ipmi_init_msghandler_mod);
5570 MODULE_LICENSE("GPL");
5571 MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
5572 MODULE_DESCRIPTION("Incoming and outgoing message routing for an IPMI interface.");
5573 MODULE_VERSION(IPMI_DRIVER_VERSION);
5574 MODULE_SOFTDEP("post: ipmi_devintf");
5575