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