1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * ipmi_ssif.c
4  *
5  * The interface to the IPMI driver for SMBus access to a SMBus
6  * compliant device.  Called SSIF by the IPMI spec.
7  *
8  * Author: Intel Corporation
9  *         Todd Davis <todd.c.davis@intel.com>
10  *
11  * Rewritten by Corey Minyard <minyard@acm.org> to support the
12  * non-blocking I2C interface, add support for multi-part
13  * transactions, add PEC support, and general clenaup.
14  *
15  * Copyright 2003 Intel Corporation
16  * Copyright 2005 MontaVista Software
17  */
18 
19 /*
20  * This file holds the "policy" for the interface to the SSIF state
21  * machine.  It does the configuration, handles timers and interrupts,
22  * and drives the real SSIF state machine.
23  */
24 
25 #define pr_fmt(fmt) "ipmi_ssif: " fmt
26 #define dev_fmt(fmt) "ipmi_ssif: " fmt
27 
28 #if defined(MODVERSIONS)
29 #include <linux/modversions.h>
30 #endif
31 
32 #include <linux/module.h>
33 #include <linux/moduleparam.h>
34 #include <linux/sched.h>
35 #include <linux/seq_file.h>
36 #include <linux/timer.h>
37 #include <linux/delay.h>
38 #include <linux/errno.h>
39 #include <linux/spinlock.h>
40 #include <linux/slab.h>
41 #include <linux/list.h>
42 #include <linux/i2c.h>
43 #include <linux/ipmi_smi.h>
44 #include <linux/init.h>
45 #include <linux/dmi.h>
46 #include <linux/kthread.h>
47 #include <linux/acpi.h>
48 #include <linux/ctype.h>
49 #include <linux/time64.h>
50 #include "ipmi_dmi.h"
51 
52 #define DEVICE_NAME "ipmi_ssif"
53 
54 #define IPMI_GET_SYSTEM_INTERFACE_CAPABILITIES_CMD	0x57
55 
56 #define	SSIF_IPMI_REQUEST			2
57 #define	SSIF_IPMI_MULTI_PART_REQUEST_START	6
58 #define	SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE	7
59 #define	SSIF_IPMI_MULTI_PART_REQUEST_END	8
60 #define	SSIF_IPMI_RESPONSE			3
61 #define	SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE	9
62 
63 /* ssif_debug is a bit-field
64  *	SSIF_DEBUG_MSG -	commands and their responses
65  *	SSIF_DEBUG_STATES -	message states
66  *	SSIF_DEBUG_TIMING -	 Measure times between events in the driver
67  */
68 #define SSIF_DEBUG_TIMING	4
69 #define SSIF_DEBUG_STATE	2
70 #define SSIF_DEBUG_MSG		1
71 #define SSIF_NODEBUG		0
72 #define SSIF_DEFAULT_DEBUG	(SSIF_NODEBUG)
73 
74 /*
75  * Timer values
76  */
77 #define SSIF_MSG_USEC		60000	/* 60ms between message tries (T3). */
78 #define SSIF_REQ_RETRY_USEC	60000	/* 60ms between send retries (T6). */
79 #define SSIF_MSG_PART_USEC	5000	/* 5ms for a message part */
80 
81 /* How many times to we retry sending/receiving the message. */
82 #define	SSIF_SEND_RETRIES	5
83 #define	SSIF_RECV_RETRIES	250
84 
85 #define SSIF_MSG_MSEC		(SSIF_MSG_USEC / 1000)
86 #define SSIF_REQ_RETRY_MSEC	(SSIF_REQ_RETRY_USEC / 1000)
87 #define SSIF_MSG_JIFFIES	((SSIF_MSG_USEC * 1000) / TICK_NSEC)
88 #define SSIF_REQ_RETRY_JIFFIES	((SSIF_REQ_RETRY_USEC * 1000) / TICK_NSEC)
89 #define SSIF_MSG_PART_JIFFIES	((SSIF_MSG_PART_USEC * 1000) / TICK_NSEC)
90 
91 /*
92  * Timeout for the watch, only used for get flag timer.
93  */
94 #define SSIF_WATCH_MSG_TIMEOUT		msecs_to_jiffies(10)
95 #define SSIF_WATCH_WATCHDOG_TIMEOUT	msecs_to_jiffies(250)
96 
97 enum ssif_intf_state {
98 	SSIF_IDLE,
99 	SSIF_GETTING_FLAGS,
100 	SSIF_GETTING_EVENTS,
101 	SSIF_CLEARING_FLAGS,
102 	SSIF_GETTING_MESSAGES,
103 	/* FIXME - add watchdog stuff. */
104 };
105 
106 #define IS_SSIF_IDLE(ssif) ((ssif)->ssif_state == SSIF_IDLE \
107 			    && (ssif)->curr_msg == NULL)
108 
109 /*
110  * Indexes into stats[] in ssif_info below.
111  */
112 enum ssif_stat_indexes {
113 	/* Number of total messages sent. */
114 	SSIF_STAT_sent_messages = 0,
115 
116 	/*
117 	 * Number of message parts sent.  Messages may be broken into
118 	 * parts if they are long.
119 	 */
120 	SSIF_STAT_sent_messages_parts,
121 
122 	/*
123 	 * Number of time a message was retried.
124 	 */
125 	SSIF_STAT_send_retries,
126 
127 	/*
128 	 * Number of times the send of a message failed.
129 	 */
130 	SSIF_STAT_send_errors,
131 
132 	/*
133 	 * Number of message responses received.
134 	 */
135 	SSIF_STAT_received_messages,
136 
137 	/*
138 	 * Number of message fragments received.
139 	 */
140 	SSIF_STAT_received_message_parts,
141 
142 	/*
143 	 * Number of times the receive of a message was retried.
144 	 */
145 	SSIF_STAT_receive_retries,
146 
147 	/*
148 	 * Number of errors receiving messages.
149 	 */
150 	SSIF_STAT_receive_errors,
151 
152 	/*
153 	 * Number of times a flag fetch was requested.
154 	 */
155 	SSIF_STAT_flag_fetches,
156 
157 	/*
158 	 * Number of times the hardware didn't follow the state machine.
159 	 */
160 	SSIF_STAT_hosed,
161 
162 	/*
163 	 * Number of received events.
164 	 */
165 	SSIF_STAT_events,
166 
167 	/* Number of asyncronous messages received. */
168 	SSIF_STAT_incoming_messages,
169 
170 	/* Number of watchdog pretimeouts. */
171 	SSIF_STAT_watchdog_pretimeouts,
172 
173 	/* Number of alers received. */
174 	SSIF_STAT_alerts,
175 
176 	/* Always add statistics before this value, it must be last. */
177 	SSIF_NUM_STATS
178 };
179 
180 struct ssif_addr_info {
181 	struct i2c_board_info binfo;
182 	char *adapter_name;
183 	int debug;
184 	int slave_addr;
185 	enum ipmi_addr_src addr_src;
186 	union ipmi_smi_info_union addr_info;
187 	struct device *dev;
188 	struct i2c_client *client;
189 
190 	struct mutex clients_mutex;
191 	struct list_head clients;
192 
193 	struct list_head link;
194 };
195 
196 struct ssif_info;
197 
198 typedef void (*ssif_i2c_done)(struct ssif_info *ssif_info, int result,
199 			     unsigned char *data, unsigned int len);
200 
201 struct ssif_info {
202 	struct ipmi_smi     *intf;
203 	spinlock_t	    lock;
204 	struct ipmi_smi_msg *waiting_msg;
205 	struct ipmi_smi_msg *curr_msg;
206 	enum ssif_intf_state ssif_state;
207 	unsigned long       ssif_debug;
208 
209 	struct ipmi_smi_handlers handlers;
210 
211 	enum ipmi_addr_src addr_source; /* ACPI, PCI, SMBIOS, hardcode, etc. */
212 	union ipmi_smi_info_union addr_info;
213 
214 	/*
215 	 * Flags from the last GET_MSG_FLAGS command, used when an ATTN
216 	 * is set to hold the flags until we are done handling everything
217 	 * from the flags.
218 	 */
219 #define RECEIVE_MSG_AVAIL	0x01
220 #define EVENT_MSG_BUFFER_FULL	0x02
221 #define WDT_PRE_TIMEOUT_INT	0x08
222 	unsigned char       msg_flags;
223 
224 	u8		    global_enables;
225 	bool		    has_event_buffer;
226 	bool		    supports_alert;
227 
228 	/*
229 	 * Used to tell what we should do with alerts.  If we are
230 	 * waiting on a response, read the data immediately.
231 	 */
232 	bool		    got_alert;
233 	bool		    waiting_alert;
234 
235 	/* Used to inform the timeout that it should do a resend. */
236 	bool		    do_resend;
237 
238 	/*
239 	 * If set to true, this will request events the next time the
240 	 * state machine is idle.
241 	 */
242 	bool                req_events;
243 
244 	/*
245 	 * If set to true, this will request flags the next time the
246 	 * state machine is idle.
247 	 */
248 	bool                req_flags;
249 
250 	/*
251 	 * Used to perform timer operations when run-to-completion
252 	 * mode is on.  This is a countdown timer.
253 	 */
254 	int                 rtc_us_timer;
255 
256 	/* Used for sending/receiving data.  +1 for the length. */
257 	unsigned char data[IPMI_MAX_MSG_LENGTH + 1];
258 	unsigned int  data_len;
259 
260 	/* Temp receive buffer, gets copied into data. */
261 	unsigned char recv[I2C_SMBUS_BLOCK_MAX];
262 
263 	struct i2c_client *client;
264 	ssif_i2c_done done_handler;
265 
266 	/* Thread interface handling */
267 	struct task_struct *thread;
268 	struct completion wake_thread;
269 	bool stopping;
270 	int i2c_read_write;
271 	int i2c_command;
272 	unsigned char *i2c_data;
273 	unsigned int i2c_size;
274 
275 	struct timer_list retry_timer;
276 	int retries_left;
277 
278 	long watch_timeout;		/* Timeout for flags check, 0 if off. */
279 	struct timer_list watch_timer;	/* Flag fetch timer. */
280 
281 	/* Info from SSIF cmd */
282 	unsigned char max_xmit_msg_size;
283 	unsigned char max_recv_msg_size;
284 	bool cmd8_works; /* See test_multipart_messages() for details. */
285 	unsigned int  multi_support;
286 	int           supports_pec;
287 
288 #define SSIF_NO_MULTI		0
289 #define SSIF_MULTI_2_PART	1
290 #define SSIF_MULTI_n_PART	2
291 	unsigned char *multi_data;
292 	unsigned int  multi_len;
293 	unsigned int  multi_pos;
294 
295 	atomic_t stats[SSIF_NUM_STATS];
296 };
297 
298 #define ssif_inc_stat(ssif, stat) \
299 	atomic_inc(&(ssif)->stats[SSIF_STAT_ ## stat])
300 #define ssif_get_stat(ssif, stat) \
301 	((unsigned int) atomic_read(&(ssif)->stats[SSIF_STAT_ ## stat]))
302 
303 static bool initialized;
304 static bool platform_registered;
305 
306 static void return_hosed_msg(struct ssif_info *ssif_info,
307 			     struct ipmi_smi_msg *msg);
308 static void start_next_msg(struct ssif_info *ssif_info, unsigned long *flags);
309 static int start_send(struct ssif_info *ssif_info,
310 		      unsigned char   *data,
311 		      unsigned int    len);
312 
313 static unsigned long *ipmi_ssif_lock_cond(struct ssif_info *ssif_info,
314 					  unsigned long *flags)
315 	__acquires(&ssif_info->lock)
316 {
317 	spin_lock_irqsave(&ssif_info->lock, *flags);
318 	return flags;
319 }
320 
321 static void ipmi_ssif_unlock_cond(struct ssif_info *ssif_info,
322 				  unsigned long *flags)
323 	__releases(&ssif_info->lock)
324 {
325 	spin_unlock_irqrestore(&ssif_info->lock, *flags);
326 }
327 
deliver_recv_msg(struct ssif_info *ssif_info, struct ipmi_smi_msg *msg)328 static void deliver_recv_msg(struct ssif_info *ssif_info,
329 			     struct ipmi_smi_msg *msg)
330 {
331 	if (msg->rsp_size < 0) {
332 		return_hosed_msg(ssif_info, msg);
333 		dev_err(&ssif_info->client->dev,
334 			"%s: Malformed message: rsp_size = %d\n",
335 		       __func__, msg->rsp_size);
336 	} else {
337 		ipmi_smi_msg_received(ssif_info->intf, msg);
338 	}
339 }
340 
return_hosed_msg(struct ssif_info *ssif_info, struct ipmi_smi_msg *msg)341 static void return_hosed_msg(struct ssif_info *ssif_info,
342 			     struct ipmi_smi_msg *msg)
343 {
344 	ssif_inc_stat(ssif_info, hosed);
345 
346 	/* Make it a response */
347 	msg->rsp[0] = msg->data[0] | 4;
348 	msg->rsp[1] = msg->data[1];
349 	msg->rsp[2] = 0xFF; /* Unknown error. */
350 	msg->rsp_size = 3;
351 
352 	deliver_recv_msg(ssif_info, msg);
353 }
354 
355 /*
356  * Must be called with the message lock held.  This will release the
357  * message lock.  Note that the caller will check IS_SSIF_IDLE and
358  * start a new operation, so there is no need to check for new
359  * messages to start in here.
360  */
start_clear_flags(struct ssif_info *ssif_info, unsigned long *flags)361 static void start_clear_flags(struct ssif_info *ssif_info, unsigned long *flags)
362 {
363 	unsigned char msg[3];
364 
365 	ssif_info->msg_flags &= ~WDT_PRE_TIMEOUT_INT;
366 	ssif_info->ssif_state = SSIF_CLEARING_FLAGS;
367 	ipmi_ssif_unlock_cond(ssif_info, flags);
368 
369 	/* Make sure the watchdog pre-timeout flag is not set at startup. */
370 	msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
371 	msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
372 	msg[2] = WDT_PRE_TIMEOUT_INT;
373 
374 	if (start_send(ssif_info, msg, 3) != 0) {
375 		/* Error, just go to normal state. */
376 		ssif_info->ssif_state = SSIF_IDLE;
377 	}
378 }
379 
start_flag_fetch(struct ssif_info *ssif_info, unsigned long *flags)380 static void start_flag_fetch(struct ssif_info *ssif_info, unsigned long *flags)
381 {
382 	unsigned char mb[2];
383 
384 	ssif_info->req_flags = false;
385 	ssif_info->ssif_state = SSIF_GETTING_FLAGS;
386 	ipmi_ssif_unlock_cond(ssif_info, flags);
387 
388 	mb[0] = (IPMI_NETFN_APP_REQUEST << 2);
389 	mb[1] = IPMI_GET_MSG_FLAGS_CMD;
390 	if (start_send(ssif_info, mb, 2) != 0)
391 		ssif_info->ssif_state = SSIF_IDLE;
392 }
393 
check_start_send(struct ssif_info *ssif_info, unsigned long *flags, struct ipmi_smi_msg *msg)394 static void check_start_send(struct ssif_info *ssif_info, unsigned long *flags,
395 			     struct ipmi_smi_msg *msg)
396 {
397 	if (start_send(ssif_info, msg->data, msg->data_size) != 0) {
398 		unsigned long oflags;
399 
400 		flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
401 		ssif_info->curr_msg = NULL;
402 		ssif_info->ssif_state = SSIF_IDLE;
403 		ipmi_ssif_unlock_cond(ssif_info, flags);
404 		ipmi_free_smi_msg(msg);
405 	}
406 }
407 
start_event_fetch(struct ssif_info *ssif_info, unsigned long *flags)408 static void start_event_fetch(struct ssif_info *ssif_info, unsigned long *flags)
409 {
410 	struct ipmi_smi_msg *msg;
411 
412 	ssif_info->req_events = false;
413 
414 	msg = ipmi_alloc_smi_msg();
415 	if (!msg) {
416 		ssif_info->ssif_state = SSIF_IDLE;
417 		ipmi_ssif_unlock_cond(ssif_info, flags);
418 		return;
419 	}
420 
421 	ssif_info->curr_msg = msg;
422 	ssif_info->ssif_state = SSIF_GETTING_EVENTS;
423 	ipmi_ssif_unlock_cond(ssif_info, flags);
424 
425 	msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
426 	msg->data[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD;
427 	msg->data_size = 2;
428 
429 	check_start_send(ssif_info, flags, msg);
430 }
431 
start_recv_msg_fetch(struct ssif_info *ssif_info, unsigned long *flags)432 static void start_recv_msg_fetch(struct ssif_info *ssif_info,
433 				 unsigned long *flags)
434 {
435 	struct ipmi_smi_msg *msg;
436 
437 	msg = ipmi_alloc_smi_msg();
438 	if (!msg) {
439 		ssif_info->ssif_state = SSIF_IDLE;
440 		ipmi_ssif_unlock_cond(ssif_info, flags);
441 		return;
442 	}
443 
444 	ssif_info->curr_msg = msg;
445 	ssif_info->ssif_state = SSIF_GETTING_MESSAGES;
446 	ipmi_ssif_unlock_cond(ssif_info, flags);
447 
448 	msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
449 	msg->data[1] = IPMI_GET_MSG_CMD;
450 	msg->data_size = 2;
451 
452 	check_start_send(ssif_info, flags, msg);
453 }
454 
455 /*
456  * Must be called with the message lock held.  This will release the
457  * message lock.  Note that the caller will check IS_SSIF_IDLE and
458  * start a new operation, so there is no need to check for new
459  * messages to start in here.
460  */
handle_flags(struct ssif_info *ssif_info, unsigned long *flags)461 static void handle_flags(struct ssif_info *ssif_info, unsigned long *flags)
462 {
463 	if (ssif_info->msg_flags & WDT_PRE_TIMEOUT_INT) {
464 		/* Watchdog pre-timeout */
465 		ssif_inc_stat(ssif_info, watchdog_pretimeouts);
466 		start_clear_flags(ssif_info, flags);
467 		ipmi_smi_watchdog_pretimeout(ssif_info->intf);
468 	} else if (ssif_info->msg_flags & RECEIVE_MSG_AVAIL)
469 		/* Messages available. */
470 		start_recv_msg_fetch(ssif_info, flags);
471 	else if (ssif_info->msg_flags & EVENT_MSG_BUFFER_FULL)
472 		/* Events available. */
473 		start_event_fetch(ssif_info, flags);
474 	else {
475 		ssif_info->ssif_state = SSIF_IDLE;
476 		ipmi_ssif_unlock_cond(ssif_info, flags);
477 	}
478 }
479 
ipmi_ssif_thread(void *data)480 static int ipmi_ssif_thread(void *data)
481 {
482 	struct ssif_info *ssif_info = data;
483 
484 	while (!kthread_should_stop()) {
485 		int result;
486 
487 		/* Wait for something to do */
488 		result = wait_for_completion_interruptible(
489 						&ssif_info->wake_thread);
490 		if (ssif_info->stopping)
491 			break;
492 		if (result == -ERESTARTSYS)
493 			continue;
494 		init_completion(&ssif_info->wake_thread);
495 
496 		if (ssif_info->i2c_read_write == I2C_SMBUS_WRITE) {
497 			result = i2c_smbus_write_block_data(
498 				ssif_info->client, ssif_info->i2c_command,
499 				ssif_info->i2c_data[0],
500 				ssif_info->i2c_data + 1);
501 			ssif_info->done_handler(ssif_info, result, NULL, 0);
502 		} else {
503 			result = i2c_smbus_read_block_data(
504 				ssif_info->client, ssif_info->i2c_command,
505 				ssif_info->i2c_data);
506 			if (result < 0)
507 				ssif_info->done_handler(ssif_info, result,
508 							NULL, 0);
509 			else
510 				ssif_info->done_handler(ssif_info, 0,
511 							ssif_info->i2c_data,
512 							result);
513 		}
514 	}
515 
516 	return 0;
517 }
518 
ssif_i2c_send(struct ssif_info *ssif_info, ssif_i2c_done handler, int read_write, int command, unsigned char *data, unsigned int size)519 static void ssif_i2c_send(struct ssif_info *ssif_info,
520 			ssif_i2c_done handler,
521 			int read_write, int command,
522 			unsigned char *data, unsigned int size)
523 {
524 	ssif_info->done_handler = handler;
525 
526 	ssif_info->i2c_read_write = read_write;
527 	ssif_info->i2c_command = command;
528 	ssif_info->i2c_data = data;
529 	ssif_info->i2c_size = size;
530 	complete(&ssif_info->wake_thread);
531 }
532 
533 
534 static void msg_done_handler(struct ssif_info *ssif_info, int result,
535 			     unsigned char *data, unsigned int len);
536 
start_get(struct ssif_info *ssif_info)537 static void start_get(struct ssif_info *ssif_info)
538 {
539 	ssif_info->rtc_us_timer = 0;
540 	ssif_info->multi_pos = 0;
541 
542 	ssif_i2c_send(ssif_info, msg_done_handler, I2C_SMBUS_READ,
543 		  SSIF_IPMI_RESPONSE,
544 		  ssif_info->recv, I2C_SMBUS_BLOCK_DATA);
545 }
546 
547 static void start_resend(struct ssif_info *ssif_info);
548 
retry_timeout(struct timer_list *t)549 static void retry_timeout(struct timer_list *t)
550 {
551 	struct ssif_info *ssif_info = from_timer(ssif_info, t, retry_timer);
552 	unsigned long oflags, *flags;
553 	bool waiting, resend;
554 
555 	if (ssif_info->stopping)
556 		return;
557 
558 	flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
559 	resend = ssif_info->do_resend;
560 	ssif_info->do_resend = false;
561 	waiting = ssif_info->waiting_alert;
562 	ssif_info->waiting_alert = false;
563 	ipmi_ssif_unlock_cond(ssif_info, flags);
564 
565 	if (waiting)
566 		start_get(ssif_info);
567 	if (resend) {
568 		start_resend(ssif_info);
569 		ssif_inc_stat(ssif_info, send_retries);
570 	}
571 }
572 
watch_timeout(struct timer_list *t)573 static void watch_timeout(struct timer_list *t)
574 {
575 	struct ssif_info *ssif_info = from_timer(ssif_info, t, watch_timer);
576 	unsigned long oflags, *flags;
577 
578 	if (ssif_info->stopping)
579 		return;
580 
581 	flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
582 	if (ssif_info->watch_timeout) {
583 		mod_timer(&ssif_info->watch_timer,
584 			  jiffies + ssif_info->watch_timeout);
585 		if (IS_SSIF_IDLE(ssif_info)) {
586 			start_flag_fetch(ssif_info, flags); /* Releases lock */
587 			return;
588 		}
589 		ssif_info->req_flags = true;
590 	}
591 	ipmi_ssif_unlock_cond(ssif_info, flags);
592 }
593 
ssif_alert(struct i2c_client *client, enum i2c_alert_protocol type, unsigned int data)594 static void ssif_alert(struct i2c_client *client, enum i2c_alert_protocol type,
595 		       unsigned int data)
596 {
597 	struct ssif_info *ssif_info = i2c_get_clientdata(client);
598 	unsigned long oflags, *flags;
599 	bool do_get = false;
600 
601 	if (type != I2C_PROTOCOL_SMBUS_ALERT)
602 		return;
603 
604 	ssif_inc_stat(ssif_info, alerts);
605 
606 	flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
607 	if (ssif_info->waiting_alert) {
608 		ssif_info->waiting_alert = false;
609 		del_timer(&ssif_info->retry_timer);
610 		do_get = true;
611 	} else if (ssif_info->curr_msg) {
612 		ssif_info->got_alert = true;
613 	}
614 	ipmi_ssif_unlock_cond(ssif_info, flags);
615 	if (do_get)
616 		start_get(ssif_info);
617 }
618 
msg_done_handler(struct ssif_info *ssif_info, int result, unsigned char *data, unsigned int len)619 static void msg_done_handler(struct ssif_info *ssif_info, int result,
620 			     unsigned char *data, unsigned int len)
621 {
622 	struct ipmi_smi_msg *msg;
623 	unsigned long oflags, *flags;
624 
625 	/*
626 	 * We are single-threaded here, so no need for a lock until we
627 	 * start messing with driver states or the queues.
628 	 */
629 
630 	if (result < 0) {
631 		ssif_info->retries_left--;
632 		if (ssif_info->retries_left > 0) {
633 			ssif_inc_stat(ssif_info, receive_retries);
634 
635 			flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
636 			ssif_info->waiting_alert = true;
637 			ssif_info->rtc_us_timer = SSIF_MSG_USEC;
638 			if (!ssif_info->stopping)
639 				mod_timer(&ssif_info->retry_timer,
640 					  jiffies + SSIF_MSG_JIFFIES);
641 			ipmi_ssif_unlock_cond(ssif_info, flags);
642 			return;
643 		}
644 
645 		ssif_inc_stat(ssif_info, receive_errors);
646 
647 		if  (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
648 			dev_dbg(&ssif_info->client->dev,
649 				"%s: Error %d\n", __func__, result);
650 		len = 0;
651 		goto continue_op;
652 	}
653 
654 	if ((len > 1) && (ssif_info->multi_pos == 0)
655 				&& (data[0] == 0x00) && (data[1] == 0x01)) {
656 		/* Start of multi-part read.  Start the next transaction. */
657 		int i;
658 
659 		ssif_inc_stat(ssif_info, received_message_parts);
660 
661 		/* Remove the multi-part read marker. */
662 		len -= 2;
663 		data += 2;
664 		for (i = 0; i < len; i++)
665 			ssif_info->data[i] = data[i];
666 		ssif_info->multi_len = len;
667 		ssif_info->multi_pos = 1;
668 
669 		ssif_i2c_send(ssif_info, msg_done_handler, I2C_SMBUS_READ,
670 			 SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE,
671 			 ssif_info->recv, I2C_SMBUS_BLOCK_DATA);
672 		return;
673 	} else if (ssif_info->multi_pos) {
674 		/* Middle of multi-part read.  Start the next transaction. */
675 		int i;
676 		unsigned char blocknum;
677 
678 		if (len == 0) {
679 			result = -EIO;
680 			if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
681 				dev_dbg(&ssif_info->client->dev,
682 					"Middle message with no data\n");
683 
684 			goto continue_op;
685 		}
686 
687 		blocknum = data[0];
688 		len--;
689 		data++;
690 
691 		if (blocknum != 0xff && len != 31) {
692 		    /* All blocks but the last must have 31 data bytes. */
693 			result = -EIO;
694 			if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
695 				dev_dbg(&ssif_info->client->dev,
696 					"Received middle message <31\n");
697 
698 			goto continue_op;
699 		}
700 
701 		if (ssif_info->multi_len + len > IPMI_MAX_MSG_LENGTH) {
702 			/* Received message too big, abort the operation. */
703 			result = -E2BIG;
704 			if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
705 				dev_dbg(&ssif_info->client->dev,
706 					"Received message too big\n");
707 
708 			goto continue_op;
709 		}
710 
711 		for (i = 0; i < len; i++)
712 			ssif_info->data[i + ssif_info->multi_len] = data[i];
713 		ssif_info->multi_len += len;
714 		if (blocknum == 0xff) {
715 			/* End of read */
716 			len = ssif_info->multi_len;
717 			data = ssif_info->data;
718 		} else if (blocknum + 1 != ssif_info->multi_pos) {
719 			/*
720 			 * Out of sequence block, just abort.  Block
721 			 * numbers start at zero for the second block,
722 			 * but multi_pos starts at one, so the +1.
723 			 */
724 			if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
725 				dev_dbg(&ssif_info->client->dev,
726 					"Received message out of sequence, expected %u, got %u\n",
727 					ssif_info->multi_pos - 1, blocknum);
728 			result = -EIO;
729 		} else {
730 			ssif_inc_stat(ssif_info, received_message_parts);
731 
732 			ssif_info->multi_pos++;
733 
734 			ssif_i2c_send(ssif_info, msg_done_handler,
735 				  I2C_SMBUS_READ,
736 				  SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE,
737 				  ssif_info->recv,
738 				  I2C_SMBUS_BLOCK_DATA);
739 			return;
740 		}
741 	}
742 
743  continue_op:
744 	if (result < 0) {
745 		ssif_inc_stat(ssif_info, receive_errors);
746 	} else {
747 		ssif_inc_stat(ssif_info, received_messages);
748 		ssif_inc_stat(ssif_info, received_message_parts);
749 	}
750 
751 	if (ssif_info->ssif_debug & SSIF_DEBUG_STATE)
752 		dev_dbg(&ssif_info->client->dev,
753 			"DONE 1: state = %d, result=%d\n",
754 			ssif_info->ssif_state, result);
755 
756 	flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
757 	msg = ssif_info->curr_msg;
758 	if (msg) {
759 		if (data) {
760 			if (len > IPMI_MAX_MSG_LENGTH)
761 				len = IPMI_MAX_MSG_LENGTH;
762 			memcpy(msg->rsp, data, len);
763 		} else {
764 			len = 0;
765 		}
766 		msg->rsp_size = len;
767 		ssif_info->curr_msg = NULL;
768 	}
769 
770 	switch (ssif_info->ssif_state) {
771 	case SSIF_IDLE:
772 		ipmi_ssif_unlock_cond(ssif_info, flags);
773 		if (!msg)
774 			break;
775 
776 		if (result < 0)
777 			return_hosed_msg(ssif_info, msg);
778 		else
779 			deliver_recv_msg(ssif_info, msg);
780 		break;
781 
782 	case SSIF_GETTING_FLAGS:
783 		/* We got the flags from the SSIF, now handle them. */
784 		if ((result < 0) || (len < 4) || (data[2] != 0)) {
785 			/*
786 			 * Error fetching flags, or invalid length,
787 			 * just give up for now.
788 			 */
789 			ssif_info->ssif_state = SSIF_IDLE;
790 			ipmi_ssif_unlock_cond(ssif_info, flags);
791 			dev_warn(&ssif_info->client->dev,
792 				 "Error getting flags: %d %d, %x\n",
793 				 result, len, (len >= 3) ? data[2] : 0);
794 		} else if (data[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
795 			   || data[1] != IPMI_GET_MSG_FLAGS_CMD) {
796 			/*
797 			 * Recv error response, give up.
798 			 */
799 			ssif_info->ssif_state = SSIF_IDLE;
800 			ipmi_ssif_unlock_cond(ssif_info, flags);
801 			dev_warn(&ssif_info->client->dev,
802 				 "Invalid response getting flags: %x %x\n",
803 				 data[0], data[1]);
804 		} else {
805 			ssif_inc_stat(ssif_info, flag_fetches);
806 			ssif_info->msg_flags = data[3];
807 			handle_flags(ssif_info, flags);
808 		}
809 		break;
810 
811 	case SSIF_CLEARING_FLAGS:
812 		/* We cleared the flags. */
813 		if ((result < 0) || (len < 3) || (data[2] != 0)) {
814 			/* Error clearing flags */
815 			dev_warn(&ssif_info->client->dev,
816 				 "Error clearing flags: %d %d, %x\n",
817 				 result, len, (len >= 3) ? data[2] : 0);
818 		} else if (data[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
819 			   || data[1] != IPMI_CLEAR_MSG_FLAGS_CMD) {
820 			dev_warn(&ssif_info->client->dev,
821 				 "Invalid response clearing flags: %x %x\n",
822 				 data[0], data[1]);
823 		}
824 		ssif_info->ssif_state = SSIF_IDLE;
825 		ipmi_ssif_unlock_cond(ssif_info, flags);
826 		break;
827 
828 	case SSIF_GETTING_EVENTS:
829 		if (!msg) {
830 			/* Should never happen, but just in case. */
831 			dev_warn(&ssif_info->client->dev,
832 				 "No message set while getting events\n");
833 			ipmi_ssif_unlock_cond(ssif_info, flags);
834 			break;
835 		}
836 
837 		if ((result < 0) || (len < 3) || (msg->rsp[2] != 0)) {
838 			/* Error getting event, probably done. */
839 			msg->done(msg);
840 
841 			/* Take off the event flag. */
842 			ssif_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
843 			handle_flags(ssif_info, flags);
844 		} else if (msg->rsp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
845 			   || msg->rsp[1] != IPMI_READ_EVENT_MSG_BUFFER_CMD) {
846 			dev_warn(&ssif_info->client->dev,
847 				 "Invalid response getting events: %x %x\n",
848 				 msg->rsp[0], msg->rsp[1]);
849 			msg->done(msg);
850 			/* Take off the event flag. */
851 			ssif_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
852 			handle_flags(ssif_info, flags);
853 		} else {
854 			handle_flags(ssif_info, flags);
855 			ssif_inc_stat(ssif_info, events);
856 			deliver_recv_msg(ssif_info, msg);
857 		}
858 		break;
859 
860 	case SSIF_GETTING_MESSAGES:
861 		if (!msg) {
862 			/* Should never happen, but just in case. */
863 			dev_warn(&ssif_info->client->dev,
864 				 "No message set while getting messages\n");
865 			ipmi_ssif_unlock_cond(ssif_info, flags);
866 			break;
867 		}
868 
869 		if ((result < 0) || (len < 3) || (msg->rsp[2] != 0)) {
870 			/* Error getting event, probably done. */
871 			msg->done(msg);
872 
873 			/* Take off the msg flag. */
874 			ssif_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
875 			handle_flags(ssif_info, flags);
876 		} else if (msg->rsp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
877 			   || msg->rsp[1] != IPMI_GET_MSG_CMD) {
878 			dev_warn(&ssif_info->client->dev,
879 				 "Invalid response clearing flags: %x %x\n",
880 				 msg->rsp[0], msg->rsp[1]);
881 			msg->done(msg);
882 
883 			/* Take off the msg flag. */
884 			ssif_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
885 			handle_flags(ssif_info, flags);
886 		} else {
887 			ssif_inc_stat(ssif_info, incoming_messages);
888 			handle_flags(ssif_info, flags);
889 			deliver_recv_msg(ssif_info, msg);
890 		}
891 		break;
892 
893 	default:
894 		/* Should never happen, but just in case. */
895 		dev_warn(&ssif_info->client->dev,
896 			 "Invalid state in message done handling: %d\n",
897 			 ssif_info->ssif_state);
898 		ipmi_ssif_unlock_cond(ssif_info, flags);
899 	}
900 
901 	flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
902 	if (IS_SSIF_IDLE(ssif_info) && !ssif_info->stopping) {
903 		if (ssif_info->req_events)
904 			start_event_fetch(ssif_info, flags);
905 		else if (ssif_info->req_flags)
906 			start_flag_fetch(ssif_info, flags);
907 		else
908 			start_next_msg(ssif_info, flags);
909 	} else
910 		ipmi_ssif_unlock_cond(ssif_info, flags);
911 
912 	if (ssif_info->ssif_debug & SSIF_DEBUG_STATE)
913 		dev_dbg(&ssif_info->client->dev,
914 			"DONE 2: state = %d.\n", ssif_info->ssif_state);
915 }
916 
msg_written_handler(struct ssif_info *ssif_info, int result, unsigned char *data, unsigned int len)917 static void msg_written_handler(struct ssif_info *ssif_info, int result,
918 				unsigned char *data, unsigned int len)
919 {
920 	/* We are single-threaded here, so no need for a lock. */
921 	if (result < 0) {
922 		ssif_info->retries_left--;
923 		if (ssif_info->retries_left > 0) {
924 			/*
925 			 * Wait the retry timeout time per the spec,
926 			 * then redo the send.
927 			 */
928 			ssif_info->do_resend = true;
929 			mod_timer(&ssif_info->retry_timer,
930 				  jiffies + SSIF_REQ_RETRY_JIFFIES);
931 			return;
932 		}
933 
934 		ssif_inc_stat(ssif_info, send_errors);
935 
936 		if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
937 			dev_dbg(&ssif_info->client->dev,
938 				"%s: Out of retries\n", __func__);
939 
940 		msg_done_handler(ssif_info, -EIO, NULL, 0);
941 		return;
942 	}
943 
944 	if (ssif_info->multi_data) {
945 		/*
946 		 * In the middle of a multi-data write.  See the comment
947 		 * in the SSIF_MULTI_n_PART case in the probe function
948 		 * for details on the intricacies of this.
949 		 */
950 		int left, to_write;
951 		unsigned char *data_to_send;
952 		unsigned char cmd;
953 
954 		ssif_inc_stat(ssif_info, sent_messages_parts);
955 
956 		left = ssif_info->multi_len - ssif_info->multi_pos;
957 		to_write = left;
958 		if (to_write > 32)
959 			to_write = 32;
960 		/* Length byte. */
961 		ssif_info->multi_data[ssif_info->multi_pos] = to_write;
962 		data_to_send = ssif_info->multi_data + ssif_info->multi_pos;
963 		ssif_info->multi_pos += to_write;
964 		cmd = SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE;
965 		if (ssif_info->cmd8_works) {
966 			if (left == to_write) {
967 				cmd = SSIF_IPMI_MULTI_PART_REQUEST_END;
968 				ssif_info->multi_data = NULL;
969 			}
970 		} else if (to_write < 32) {
971 			ssif_info->multi_data = NULL;
972 		}
973 
974 		ssif_i2c_send(ssif_info, msg_written_handler,
975 			  I2C_SMBUS_WRITE, cmd,
976 			  data_to_send, I2C_SMBUS_BLOCK_DATA);
977 	} else {
978 		/* Ready to request the result. */
979 		unsigned long oflags, *flags;
980 
981 		ssif_inc_stat(ssif_info, sent_messages);
982 		ssif_inc_stat(ssif_info, sent_messages_parts);
983 
984 		flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
985 		if (ssif_info->got_alert) {
986 			/* The result is already ready, just start it. */
987 			ssif_info->got_alert = false;
988 			ipmi_ssif_unlock_cond(ssif_info, flags);
989 			start_get(ssif_info);
990 		} else {
991 			/* Wait a jiffie then request the next message */
992 			ssif_info->waiting_alert = true;
993 			ssif_info->retries_left = SSIF_RECV_RETRIES;
994 			ssif_info->rtc_us_timer = SSIF_MSG_PART_USEC;
995 			if (!ssif_info->stopping)
996 				mod_timer(&ssif_info->retry_timer,
997 					  jiffies + SSIF_MSG_PART_JIFFIES);
998 			ipmi_ssif_unlock_cond(ssif_info, flags);
999 		}
1000 	}
1001 }
1002 
start_resend(struct ssif_info *ssif_info)1003 static void start_resend(struct ssif_info *ssif_info)
1004 {
1005 	int command;
1006 
1007 	ssif_info->got_alert = false;
1008 
1009 	if (ssif_info->data_len > 32) {
1010 		command = SSIF_IPMI_MULTI_PART_REQUEST_START;
1011 		ssif_info->multi_data = ssif_info->data;
1012 		ssif_info->multi_len = ssif_info->data_len;
1013 		/*
1014 		 * Subtle thing, this is 32, not 33, because we will
1015 		 * overwrite the thing at position 32 (which was just
1016 		 * transmitted) with the new length.
1017 		 */
1018 		ssif_info->multi_pos = 32;
1019 		ssif_info->data[0] = 32;
1020 	} else {
1021 		ssif_info->multi_data = NULL;
1022 		command = SSIF_IPMI_REQUEST;
1023 		ssif_info->data[0] = ssif_info->data_len;
1024 	}
1025 
1026 	ssif_i2c_send(ssif_info, msg_written_handler, I2C_SMBUS_WRITE,
1027 		   command, ssif_info->data, I2C_SMBUS_BLOCK_DATA);
1028 }
1029 
start_send(struct ssif_info *ssif_info, unsigned char *data, unsigned int len)1030 static int start_send(struct ssif_info *ssif_info,
1031 		      unsigned char   *data,
1032 		      unsigned int    len)
1033 {
1034 	if (len > IPMI_MAX_MSG_LENGTH)
1035 		return -E2BIG;
1036 	if (len > ssif_info->max_xmit_msg_size)
1037 		return -E2BIG;
1038 
1039 	ssif_info->retries_left = SSIF_SEND_RETRIES;
1040 	memcpy(ssif_info->data + 1, data, len);
1041 	ssif_info->data_len = len;
1042 	start_resend(ssif_info);
1043 	return 0;
1044 }
1045 
1046 /* Must be called with the message lock held. */
start_next_msg(struct ssif_info *ssif_info, unsigned long *flags)1047 static void start_next_msg(struct ssif_info *ssif_info, unsigned long *flags)
1048 {
1049 	struct ipmi_smi_msg *msg;
1050 	unsigned long oflags;
1051 
1052  restart:
1053 	if (!IS_SSIF_IDLE(ssif_info)) {
1054 		ipmi_ssif_unlock_cond(ssif_info, flags);
1055 		return;
1056 	}
1057 
1058 	if (!ssif_info->waiting_msg) {
1059 		ssif_info->curr_msg = NULL;
1060 		ipmi_ssif_unlock_cond(ssif_info, flags);
1061 	} else {
1062 		int rv;
1063 
1064 		ssif_info->curr_msg = ssif_info->waiting_msg;
1065 		ssif_info->waiting_msg = NULL;
1066 		ipmi_ssif_unlock_cond(ssif_info, flags);
1067 		rv = start_send(ssif_info,
1068 				ssif_info->curr_msg->data,
1069 				ssif_info->curr_msg->data_size);
1070 		if (rv) {
1071 			msg = ssif_info->curr_msg;
1072 			ssif_info->curr_msg = NULL;
1073 			return_hosed_msg(ssif_info, msg);
1074 			flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1075 			goto restart;
1076 		}
1077 	}
1078 }
1079 
sender(void *send_info, struct ipmi_smi_msg *msg)1080 static void sender(void                *send_info,
1081 		   struct ipmi_smi_msg *msg)
1082 {
1083 	struct ssif_info *ssif_info = (struct ssif_info *) send_info;
1084 	unsigned long oflags, *flags;
1085 
1086 	BUG_ON(ssif_info->waiting_msg);
1087 	ssif_info->waiting_msg = msg;
1088 
1089 	flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1090 	start_next_msg(ssif_info, flags);
1091 
1092 	if (ssif_info->ssif_debug & SSIF_DEBUG_TIMING) {
1093 		struct timespec64 t;
1094 
1095 		ktime_get_real_ts64(&t);
1096 		dev_dbg(&ssif_info->client->dev,
1097 			"**Enqueue %02x %02x: %lld.%6.6ld\n",
1098 			msg->data[0], msg->data[1],
1099 			(long long)t.tv_sec, (long)t.tv_nsec / NSEC_PER_USEC);
1100 	}
1101 }
1102 
get_smi_info(void *send_info, struct ipmi_smi_info *data)1103 static int get_smi_info(void *send_info, struct ipmi_smi_info *data)
1104 {
1105 	struct ssif_info *ssif_info = send_info;
1106 
1107 	data->addr_src = ssif_info->addr_source;
1108 	data->dev = &ssif_info->client->dev;
1109 	data->addr_info = ssif_info->addr_info;
1110 	get_device(data->dev);
1111 
1112 	return 0;
1113 }
1114 
1115 /*
1116  * Upper layer wants us to request events.
1117  */
request_events(void *send_info)1118 static void request_events(void *send_info)
1119 {
1120 	struct ssif_info *ssif_info = (struct ssif_info *) send_info;
1121 	unsigned long oflags, *flags;
1122 
1123 	if (!ssif_info->has_event_buffer)
1124 		return;
1125 
1126 	flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1127 	ssif_info->req_events = true;
1128 	ipmi_ssif_unlock_cond(ssif_info, flags);
1129 }
1130 
1131 /*
1132  * Upper layer is changing the flag saying whether we need to request
1133  * flags periodically or not.
1134  */
ssif_set_need_watch(void *send_info, unsigned int watch_mask)1135 static void ssif_set_need_watch(void *send_info, unsigned int watch_mask)
1136 {
1137 	struct ssif_info *ssif_info = (struct ssif_info *) send_info;
1138 	unsigned long oflags, *flags;
1139 	long timeout = 0;
1140 
1141 	if (watch_mask & IPMI_WATCH_MASK_CHECK_MESSAGES)
1142 		timeout = SSIF_WATCH_MSG_TIMEOUT;
1143 	else if (watch_mask)
1144 		timeout = SSIF_WATCH_WATCHDOG_TIMEOUT;
1145 
1146 	flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1147 	if (timeout != ssif_info->watch_timeout) {
1148 		ssif_info->watch_timeout = timeout;
1149 		if (ssif_info->watch_timeout)
1150 			mod_timer(&ssif_info->watch_timer,
1151 				  jiffies + ssif_info->watch_timeout);
1152 	}
1153 	ipmi_ssif_unlock_cond(ssif_info, flags);
1154 }
1155 
ssif_start_processing(void *send_info, struct ipmi_smi *intf)1156 static int ssif_start_processing(void            *send_info,
1157 				 struct ipmi_smi *intf)
1158 {
1159 	struct ssif_info *ssif_info = send_info;
1160 
1161 	ssif_info->intf = intf;
1162 
1163 	return 0;
1164 }
1165 
1166 #define MAX_SSIF_BMCS 4
1167 
1168 static unsigned short addr[MAX_SSIF_BMCS];
1169 static int num_addrs;
1170 module_param_array(addr, ushort, &num_addrs, 0);
1171 MODULE_PARM_DESC(addr, "The addresses to scan for IPMI BMCs on the SSIFs.");
1172 
1173 static char *adapter_name[MAX_SSIF_BMCS];
1174 static int num_adapter_names;
1175 module_param_array(adapter_name, charp, &num_adapter_names, 0);
1176 MODULE_PARM_DESC(adapter_name, "The string name of the I2C device that has the BMC.  By default all devices are scanned.");
1177 
1178 static int slave_addrs[MAX_SSIF_BMCS];
1179 static int num_slave_addrs;
1180 module_param_array(slave_addrs, int, &num_slave_addrs, 0);
1181 MODULE_PARM_DESC(slave_addrs,
1182 		 "The default IPMB slave address for the controller.");
1183 
1184 static bool alerts_broken;
1185 module_param(alerts_broken, bool, 0);
1186 MODULE_PARM_DESC(alerts_broken, "Don't enable alerts for the controller.");
1187 
1188 /*
1189  * Bit 0 enables message debugging, bit 1 enables state debugging, and
1190  * bit 2 enables timing debugging.  This is an array indexed by
1191  * interface number"
1192  */
1193 static int dbg[MAX_SSIF_BMCS];
1194 static int num_dbg;
1195 module_param_array(dbg, int, &num_dbg, 0);
1196 MODULE_PARM_DESC(dbg, "Turn on debugging.");
1197 
1198 static bool ssif_dbg_probe;
1199 module_param_named(dbg_probe, ssif_dbg_probe, bool, 0);
1200 MODULE_PARM_DESC(dbg_probe, "Enable debugging of probing of adapters.");
1201 
1202 static bool ssif_tryacpi = true;
1203 module_param_named(tryacpi, ssif_tryacpi, bool, 0);
1204 MODULE_PARM_DESC(tryacpi, "Setting this to zero will disable the default scan of the interfaces identified via ACPI");
1205 
1206 static bool ssif_trydmi = true;
1207 module_param_named(trydmi, ssif_trydmi, bool, 0);
1208 MODULE_PARM_DESC(trydmi, "Setting this to zero will disable the default scan of the interfaces identified via DMI (SMBIOS)");
1209 
1210 static DEFINE_MUTEX(ssif_infos_mutex);
1211 static LIST_HEAD(ssif_infos);
1212 
1213 #define IPMI_SSIF_ATTR(name) \
1214 static ssize_t ipmi_##name##_show(struct device *dev,			\
1215 				  struct device_attribute *attr,	\
1216 				  char *buf)				\
1217 {									\
1218 	struct ssif_info *ssif_info = dev_get_drvdata(dev);		\
1219 									\
1220 	return snprintf(buf, 10, "%u\n", ssif_get_stat(ssif_info, name));\
1221 }									\
1222 static DEVICE_ATTR(name, S_IRUGO, ipmi_##name##_show, NULL)
1223 
ipmi_type_show(struct device *dev, struct device_attribute *attr, char *buf)1224 static ssize_t ipmi_type_show(struct device *dev,
1225 			      struct device_attribute *attr,
1226 			      char *buf)
1227 {
1228 	return snprintf(buf, 10, "ssif\n");
1229 }
1230 static DEVICE_ATTR(type, S_IRUGO, ipmi_type_show, NULL);
1231 
1232 IPMI_SSIF_ATTR(sent_messages);
1233 IPMI_SSIF_ATTR(sent_messages_parts);
1234 IPMI_SSIF_ATTR(send_retries);
1235 IPMI_SSIF_ATTR(send_errors);
1236 IPMI_SSIF_ATTR(received_messages);
1237 IPMI_SSIF_ATTR(received_message_parts);
1238 IPMI_SSIF_ATTR(receive_retries);
1239 IPMI_SSIF_ATTR(receive_errors);
1240 IPMI_SSIF_ATTR(flag_fetches);
1241 IPMI_SSIF_ATTR(hosed);
1242 IPMI_SSIF_ATTR(events);
1243 IPMI_SSIF_ATTR(watchdog_pretimeouts);
1244 IPMI_SSIF_ATTR(alerts);
1245 
1246 static struct attribute *ipmi_ssif_dev_attrs[] = {
1247 	&dev_attr_type.attr,
1248 	&dev_attr_sent_messages.attr,
1249 	&dev_attr_sent_messages_parts.attr,
1250 	&dev_attr_send_retries.attr,
1251 	&dev_attr_send_errors.attr,
1252 	&dev_attr_received_messages.attr,
1253 	&dev_attr_received_message_parts.attr,
1254 	&dev_attr_receive_retries.attr,
1255 	&dev_attr_receive_errors.attr,
1256 	&dev_attr_flag_fetches.attr,
1257 	&dev_attr_hosed.attr,
1258 	&dev_attr_events.attr,
1259 	&dev_attr_watchdog_pretimeouts.attr,
1260 	&dev_attr_alerts.attr,
1261 	NULL
1262 };
1263 
1264 static const struct attribute_group ipmi_ssif_dev_attr_group = {
1265 	.attrs		= ipmi_ssif_dev_attrs,
1266 };
1267 
shutdown_ssif(void *send_info)1268 static void shutdown_ssif(void *send_info)
1269 {
1270 	struct ssif_info *ssif_info = send_info;
1271 
1272 	device_remove_group(&ssif_info->client->dev, &ipmi_ssif_dev_attr_group);
1273 	dev_set_drvdata(&ssif_info->client->dev, NULL);
1274 
1275 	/* make sure the driver is not looking for flags any more. */
1276 	while (ssif_info->ssif_state != SSIF_IDLE)
1277 		schedule_timeout(1);
1278 
1279 	ssif_info->stopping = true;
1280 	del_timer_sync(&ssif_info->watch_timer);
1281 	del_timer_sync(&ssif_info->retry_timer);
1282 	if (ssif_info->thread) {
1283 		complete(&ssif_info->wake_thread);
1284 		kthread_stop(ssif_info->thread);
1285 	}
1286 }
1287 
ssif_remove(struct i2c_client *client)1288 static int ssif_remove(struct i2c_client *client)
1289 {
1290 	struct ssif_info *ssif_info = i2c_get_clientdata(client);
1291 	struct ssif_addr_info *addr_info;
1292 
1293 	if (!ssif_info)
1294 		return 0;
1295 
1296 	/*
1297 	 * After this point, we won't deliver anything asychronously
1298 	 * to the message handler.  We can unregister ourself.
1299 	 */
1300 	ipmi_unregister_smi(ssif_info->intf);
1301 
1302 	list_for_each_entry(addr_info, &ssif_infos, link) {
1303 		if (addr_info->client == client) {
1304 			addr_info->client = NULL;
1305 			break;
1306 		}
1307 	}
1308 
1309 	kfree(ssif_info);
1310 
1311 	return 0;
1312 }
1313 
read_response(struct i2c_client *client, unsigned char *resp)1314 static int read_response(struct i2c_client *client, unsigned char *resp)
1315 {
1316 	int ret = -ENODEV, retry_cnt = SSIF_RECV_RETRIES;
1317 
1318 	while (retry_cnt > 0) {
1319 		ret = i2c_smbus_read_block_data(client, SSIF_IPMI_RESPONSE,
1320 						resp);
1321 		if (ret > 0)
1322 			break;
1323 		msleep(SSIF_MSG_MSEC);
1324 		retry_cnt--;
1325 		if (retry_cnt <= 0)
1326 			break;
1327 	}
1328 
1329 	return ret;
1330 }
1331 
do_cmd(struct i2c_client *client, int len, unsigned char *msg, int *resp_len, unsigned char *resp)1332 static int do_cmd(struct i2c_client *client, int len, unsigned char *msg,
1333 		  int *resp_len, unsigned char *resp)
1334 {
1335 	int retry_cnt;
1336 	int ret;
1337 
1338 	retry_cnt = SSIF_SEND_RETRIES;
1339  retry1:
1340 	ret = i2c_smbus_write_block_data(client, SSIF_IPMI_REQUEST, len, msg);
1341 	if (ret) {
1342 		retry_cnt--;
1343 		if (retry_cnt > 0) {
1344 			msleep(SSIF_REQ_RETRY_MSEC);
1345 			goto retry1;
1346 		}
1347 		return -ENODEV;
1348 	}
1349 
1350 	ret = read_response(client, resp);
1351 	if (ret > 0) {
1352 		/* Validate that the response is correct. */
1353 		if (ret < 3 ||
1354 		    (resp[0] != (msg[0] | (1 << 2))) ||
1355 		    (resp[1] != msg[1]))
1356 			ret = -EINVAL;
1357 		else if (ret > IPMI_MAX_MSG_LENGTH) {
1358 			ret = -E2BIG;
1359 		} else {
1360 			*resp_len = ret;
1361 			ret = 0;
1362 		}
1363 	}
1364 
1365 	return ret;
1366 }
1367 
ssif_detect(struct i2c_client *client, struct i2c_board_info *info)1368 static int ssif_detect(struct i2c_client *client, struct i2c_board_info *info)
1369 {
1370 	unsigned char *resp;
1371 	unsigned char msg[3];
1372 	int           rv;
1373 	int           len;
1374 
1375 	resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1376 	if (!resp)
1377 		return -ENOMEM;
1378 
1379 	/* Do a Get Device ID command, since it is required. */
1380 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1381 	msg[1] = IPMI_GET_DEVICE_ID_CMD;
1382 	rv = do_cmd(client, 2, msg, &len, resp);
1383 	if (rv)
1384 		rv = -ENODEV;
1385 	else
1386 		strlcpy(info->type, DEVICE_NAME, I2C_NAME_SIZE);
1387 	kfree(resp);
1388 	return rv;
1389 }
1390 
strcmp_nospace(char *s1, char *s2)1391 static int strcmp_nospace(char *s1, char *s2)
1392 {
1393 	while (*s1 && *s2) {
1394 		while (isspace(*s1))
1395 			s1++;
1396 		while (isspace(*s2))
1397 			s2++;
1398 		if (*s1 > *s2)
1399 			return 1;
1400 		if (*s1 < *s2)
1401 			return -1;
1402 		s1++;
1403 		s2++;
1404 	}
1405 	return 0;
1406 }
1407 
ssif_info_find(unsigned short addr, char *adapter_name, bool match_null_name)1408 static struct ssif_addr_info *ssif_info_find(unsigned short addr,
1409 					     char *adapter_name,
1410 					     bool match_null_name)
1411 {
1412 	struct ssif_addr_info *info, *found = NULL;
1413 
1414 restart:
1415 	list_for_each_entry(info, &ssif_infos, link) {
1416 		if (info->binfo.addr == addr) {
1417 			if (info->addr_src == SI_SMBIOS && !info->adapter_name)
1418 				info->adapter_name = kstrdup(adapter_name,
1419 							     GFP_KERNEL);
1420 
1421 			if (info->adapter_name || adapter_name) {
1422 				if (!info->adapter_name != !adapter_name) {
1423 					/* One is NULL and one is not */
1424 					continue;
1425 				}
1426 				if (adapter_name &&
1427 				    strcmp_nospace(info->adapter_name,
1428 						   adapter_name))
1429 					/* Names do not match */
1430 					continue;
1431 			}
1432 			found = info;
1433 			break;
1434 		}
1435 	}
1436 
1437 	if (!found && match_null_name) {
1438 		/* Try to get an exact match first, then try with a NULL name */
1439 		adapter_name = NULL;
1440 		match_null_name = false;
1441 		goto restart;
1442 	}
1443 
1444 	return found;
1445 }
1446 
check_acpi(struct ssif_info *ssif_info, struct device *dev)1447 static bool check_acpi(struct ssif_info *ssif_info, struct device *dev)
1448 {
1449 #ifdef CONFIG_ACPI
1450 	acpi_handle acpi_handle;
1451 
1452 	acpi_handle = ACPI_HANDLE(dev);
1453 	if (acpi_handle) {
1454 		ssif_info->addr_source = SI_ACPI;
1455 		ssif_info->addr_info.acpi_info.acpi_handle = acpi_handle;
1456 		request_module("acpi_ipmi");
1457 		return true;
1458 	}
1459 #endif
1460 	return false;
1461 }
1462 
find_slave_address(struct i2c_client *client, int slave_addr)1463 static int find_slave_address(struct i2c_client *client, int slave_addr)
1464 {
1465 #ifdef CONFIG_IPMI_DMI_DECODE
1466 	if (!slave_addr)
1467 		slave_addr = ipmi_dmi_get_slave_addr(
1468 			SI_TYPE_INVALID,
1469 			i2c_adapter_id(client->adapter),
1470 			client->addr);
1471 #endif
1472 
1473 	return slave_addr;
1474 }
1475 
start_multipart_test(struct i2c_client *client, unsigned char *msg, bool do_middle)1476 static int start_multipart_test(struct i2c_client *client,
1477 				unsigned char *msg, bool do_middle)
1478 {
1479 	int retry_cnt = SSIF_SEND_RETRIES, ret;
1480 
1481 retry_write:
1482 	ret = i2c_smbus_write_block_data(client,
1483 					 SSIF_IPMI_MULTI_PART_REQUEST_START,
1484 					 32, msg);
1485 	if (ret) {
1486 		retry_cnt--;
1487 		if (retry_cnt > 0) {
1488 			msleep(SSIF_REQ_RETRY_MSEC);
1489 			goto retry_write;
1490 		}
1491 		dev_err(&client->dev, "Could not write multi-part start, though the BMC said it could handle it.  Just limit sends to one part.\n");
1492 		return ret;
1493 	}
1494 
1495 	if (!do_middle)
1496 		return 0;
1497 
1498 	ret = i2c_smbus_write_block_data(client,
1499 					 SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE,
1500 					 32, msg + 32);
1501 	if (ret) {
1502 		dev_err(&client->dev, "Could not write multi-part middle, though the BMC said it could handle it.  Just limit sends to one part.\n");
1503 		return ret;
1504 	}
1505 
1506 	return 0;
1507 }
1508 
test_multipart_messages(struct i2c_client *client, struct ssif_info *ssif_info, unsigned char *resp)1509 static void test_multipart_messages(struct i2c_client *client,
1510 				    struct ssif_info *ssif_info,
1511 				    unsigned char *resp)
1512 {
1513 	unsigned char msg[65];
1514 	int ret;
1515 	bool do_middle;
1516 
1517 	if (ssif_info->max_xmit_msg_size <= 32)
1518 		return;
1519 
1520 	do_middle = ssif_info->max_xmit_msg_size > 63;
1521 
1522 	memset(msg, 0, sizeof(msg));
1523 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1524 	msg[1] = IPMI_GET_DEVICE_ID_CMD;
1525 
1526 	/*
1527 	 * The specification is all messed up dealing with sending
1528 	 * multi-part messages.  Per what the specification says, it
1529 	 * is impossible to send a message that is a multiple of 32
1530 	 * bytes, except for 32 itself.  It talks about a "start"
1531 	 * transaction (cmd=6) that must be 32 bytes, "middle"
1532 	 * transaction (cmd=7) that must be 32 bytes, and an "end"
1533 	 * transaction.  The "end" transaction is shown as cmd=7 in
1534 	 * the text, but if that's the case there is no way to
1535 	 * differentiate between a middle and end part except the
1536 	 * length being less than 32.  But there is a table at the far
1537 	 * end of the section (that I had never noticed until someone
1538 	 * pointed it out to me) that mentions it as cmd=8.
1539 	 *
1540 	 * After some thought, I think the example is wrong and the
1541 	 * end transaction should be cmd=8.  But some systems don't
1542 	 * implement cmd=8, they use a zero-length end transaction,
1543 	 * even though that violates the SMBus specification.
1544 	 *
1545 	 * So, to work around this, this code tests if cmd=8 works.
1546 	 * If it does, then we use that.  If not, it tests zero-
1547 	 * byte end transactions.  If that works, good.  If not,
1548 	 * we only allow 63-byte transactions max.
1549 	 */
1550 
1551 	ret = start_multipart_test(client, msg, do_middle);
1552 	if (ret)
1553 		goto out_no_multi_part;
1554 
1555 	ret = i2c_smbus_write_block_data(client,
1556 					 SSIF_IPMI_MULTI_PART_REQUEST_END,
1557 					 1, msg + 64);
1558 
1559 	if (!ret)
1560 		ret = read_response(client, resp);
1561 
1562 	if (ret > 0) {
1563 		/* End transactions work, we are good. */
1564 		ssif_info->cmd8_works = true;
1565 		return;
1566 	}
1567 
1568 	ret = start_multipart_test(client, msg, do_middle);
1569 	if (ret) {
1570 		dev_err(&client->dev, "Second multipart test failed.\n");
1571 		goto out_no_multi_part;
1572 	}
1573 
1574 	ret = i2c_smbus_write_block_data(client,
1575 					 SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE,
1576 					 0, msg + 64);
1577 	if (!ret)
1578 		ret = read_response(client, resp);
1579 	if (ret > 0)
1580 		/* Zero-size end parts work, use those. */
1581 		return;
1582 
1583 	/* Limit to 63 bytes and use a short middle command to mark the end. */
1584 	if (ssif_info->max_xmit_msg_size > 63)
1585 		ssif_info->max_xmit_msg_size = 63;
1586 	return;
1587 
1588 out_no_multi_part:
1589 	ssif_info->max_xmit_msg_size = 32;
1590 	return;
1591 }
1592 
1593 /*
1594  * Global enables we care about.
1595  */
1596 #define GLOBAL_ENABLES_MASK (IPMI_BMC_EVT_MSG_BUFF | IPMI_BMC_RCV_MSG_INTR | \
1597 			     IPMI_BMC_EVT_MSG_INTR)
1598 
ssif_remove_dup(struct i2c_client *client)1599 static void ssif_remove_dup(struct i2c_client *client)
1600 {
1601 	struct ssif_info *ssif_info = i2c_get_clientdata(client);
1602 
1603 	ipmi_unregister_smi(ssif_info->intf);
1604 	kfree(ssif_info);
1605 }
1606 
ssif_add_infos(struct i2c_client *client)1607 static int ssif_add_infos(struct i2c_client *client)
1608 {
1609 	struct ssif_addr_info *info;
1610 
1611 	info = kzalloc(sizeof(*info), GFP_KERNEL);
1612 	if (!info)
1613 		return -ENOMEM;
1614 	info->addr_src = SI_ACPI;
1615 	info->client = client;
1616 	info->adapter_name = kstrdup(client->adapter->name, GFP_KERNEL);
1617 	if (!info->adapter_name) {
1618 		kfree(info);
1619 		return -ENOMEM;
1620 	}
1621 
1622 	info->binfo.addr = client->addr;
1623 	list_add_tail(&info->link, &ssif_infos);
1624 	return 0;
1625 }
1626 
1627 /*
1628  * Prefer ACPI over SMBIOS, if both are available.
1629  * So if we get an ACPI interface and have already registered a SMBIOS
1630  * interface at the same address, remove the SMBIOS and add the ACPI one.
1631  */
ssif_check_and_remove(struct i2c_client *client, struct ssif_info *ssif_info)1632 static int ssif_check_and_remove(struct i2c_client *client,
1633 			      struct ssif_info *ssif_info)
1634 {
1635 	struct ssif_addr_info *info;
1636 
1637 	list_for_each_entry(info, &ssif_infos, link) {
1638 		if (!info->client)
1639 			return 0;
1640 		if (!strcmp(info->adapter_name, client->adapter->name) &&
1641 		    info->binfo.addr == client->addr) {
1642 			if (info->addr_src == SI_ACPI)
1643 				return -EEXIST;
1644 
1645 			if (ssif_info->addr_source == SI_ACPI &&
1646 			    info->addr_src == SI_SMBIOS) {
1647 				dev_info(&client->dev,
1648 					 "Removing %s-specified SSIF interface in favor of ACPI\n",
1649 					 ipmi_addr_src_to_str(info->addr_src));
1650 				ssif_remove_dup(info->client);
1651 				return 0;
1652 			}
1653 		}
1654 	}
1655 	return 0;
1656 }
1657 
ssif_probe(struct i2c_client *client, const struct i2c_device_id *id)1658 static int ssif_probe(struct i2c_client *client, const struct i2c_device_id *id)
1659 {
1660 	unsigned char     msg[3];
1661 	unsigned char     *resp;
1662 	struct ssif_info   *ssif_info;
1663 	int               rv = 0;
1664 	int               len;
1665 	int               i;
1666 	u8		  slave_addr = 0;
1667 	struct ssif_addr_info *addr_info = NULL;
1668 
1669 	mutex_lock(&ssif_infos_mutex);
1670 	resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1671 	if (!resp) {
1672 		mutex_unlock(&ssif_infos_mutex);
1673 		return -ENOMEM;
1674 	}
1675 
1676 	ssif_info = kzalloc(sizeof(*ssif_info), GFP_KERNEL);
1677 	if (!ssif_info) {
1678 		kfree(resp);
1679 		mutex_unlock(&ssif_infos_mutex);
1680 		return -ENOMEM;
1681 	}
1682 
1683 	if (!check_acpi(ssif_info, &client->dev)) {
1684 		addr_info = ssif_info_find(client->addr, client->adapter->name,
1685 					   true);
1686 		if (!addr_info) {
1687 			/* Must have come in through sysfs. */
1688 			ssif_info->addr_source = SI_HOTMOD;
1689 		} else {
1690 			ssif_info->addr_source = addr_info->addr_src;
1691 			ssif_info->ssif_debug = addr_info->debug;
1692 			ssif_info->addr_info = addr_info->addr_info;
1693 			addr_info->client = client;
1694 			slave_addr = addr_info->slave_addr;
1695 		}
1696 	}
1697 
1698 	ssif_info->client = client;
1699 	i2c_set_clientdata(client, ssif_info);
1700 
1701 	rv = ssif_check_and_remove(client, ssif_info);
1702 	/* If rv is 0 and addr source is not SI_ACPI, continue probing */
1703 	if (!rv && ssif_info->addr_source == SI_ACPI) {
1704 		rv = ssif_add_infos(client);
1705 		if (rv) {
1706 			dev_err(&client->dev, "Out of memory!, exiting ..\n");
1707 			goto out;
1708 		}
1709 	} else if (rv) {
1710 		dev_err(&client->dev, "Not probing, Interface already present\n");
1711 		goto out;
1712 	}
1713 
1714 	slave_addr = find_slave_address(client, slave_addr);
1715 
1716 	dev_info(&client->dev,
1717 		 "Trying %s-specified SSIF interface at i2c address 0x%x, adapter %s, slave address 0x%x\n",
1718 		ipmi_addr_src_to_str(ssif_info->addr_source),
1719 		client->addr, client->adapter->name, slave_addr);
1720 
1721 	/* Now check for system interface capabilities */
1722 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1723 	msg[1] = IPMI_GET_SYSTEM_INTERFACE_CAPABILITIES_CMD;
1724 	msg[2] = 0; /* SSIF */
1725 	rv = do_cmd(client, 3, msg, &len, resp);
1726 	if (!rv && (len >= 3) && (resp[2] == 0)) {
1727 		if (len < 7) {
1728 			if (ssif_dbg_probe)
1729 				dev_dbg(&ssif_info->client->dev,
1730 					"SSIF info too short: %d\n", len);
1731 			goto no_support;
1732 		}
1733 
1734 		/* Got a good SSIF response, handle it. */
1735 		ssif_info->max_xmit_msg_size = resp[5];
1736 		ssif_info->max_recv_msg_size = resp[6];
1737 		ssif_info->multi_support = (resp[4] >> 6) & 0x3;
1738 		ssif_info->supports_pec = (resp[4] >> 3) & 0x1;
1739 
1740 		/* Sanitize the data */
1741 		switch (ssif_info->multi_support) {
1742 		case SSIF_NO_MULTI:
1743 			if (ssif_info->max_xmit_msg_size > 32)
1744 				ssif_info->max_xmit_msg_size = 32;
1745 			if (ssif_info->max_recv_msg_size > 32)
1746 				ssif_info->max_recv_msg_size = 32;
1747 			break;
1748 
1749 		case SSIF_MULTI_2_PART:
1750 			if (ssif_info->max_xmit_msg_size > 63)
1751 				ssif_info->max_xmit_msg_size = 63;
1752 			if (ssif_info->max_recv_msg_size > 62)
1753 				ssif_info->max_recv_msg_size = 62;
1754 			break;
1755 
1756 		case SSIF_MULTI_n_PART:
1757 			/* We take whatever size given, but do some testing. */
1758 			break;
1759 
1760 		default:
1761 			/* Data is not sane, just give up. */
1762 			goto no_support;
1763 		}
1764 	} else {
1765  no_support:
1766 		/* Assume no multi-part or PEC support */
1767 		dev_info(&ssif_info->client->dev,
1768 			 "Error fetching SSIF: %d %d %2.2x, your system probably doesn't support this command so using defaults\n",
1769 			rv, len, resp[2]);
1770 
1771 		ssif_info->max_xmit_msg_size = 32;
1772 		ssif_info->max_recv_msg_size = 32;
1773 		ssif_info->multi_support = SSIF_NO_MULTI;
1774 		ssif_info->supports_pec = 0;
1775 	}
1776 
1777 	test_multipart_messages(client, ssif_info, resp);
1778 
1779 	/* Make sure the NMI timeout is cleared. */
1780 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1781 	msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
1782 	msg[2] = WDT_PRE_TIMEOUT_INT;
1783 	rv = do_cmd(client, 3, msg, &len, resp);
1784 	if (rv || (len < 3) || (resp[2] != 0))
1785 		dev_warn(&ssif_info->client->dev,
1786 			 "Unable to clear message flags: %d %d %2.2x\n",
1787 			 rv, len, resp[2]);
1788 
1789 	/* Attempt to enable the event buffer. */
1790 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1791 	msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
1792 	rv = do_cmd(client, 2, msg, &len, resp);
1793 	if (rv || (len < 4) || (resp[2] != 0)) {
1794 		dev_warn(&ssif_info->client->dev,
1795 			 "Error getting global enables: %d %d %2.2x\n",
1796 			 rv, len, resp[2]);
1797 		rv = 0; /* Not fatal */
1798 		goto found;
1799 	}
1800 
1801 	ssif_info->global_enables = resp[3];
1802 
1803 	if (resp[3] & IPMI_BMC_EVT_MSG_BUFF) {
1804 		ssif_info->has_event_buffer = true;
1805 		/* buffer is already enabled, nothing to do. */
1806 		goto found;
1807 	}
1808 
1809 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1810 	msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
1811 	msg[2] = ssif_info->global_enables | IPMI_BMC_EVT_MSG_BUFF;
1812 	rv = do_cmd(client, 3, msg, &len, resp);
1813 	if (rv || (len < 2)) {
1814 		dev_warn(&ssif_info->client->dev,
1815 			 "Error setting global enables: %d %d %2.2x\n",
1816 			 rv, len, resp[2]);
1817 		rv = 0; /* Not fatal */
1818 		goto found;
1819 	}
1820 
1821 	if (resp[2] == 0) {
1822 		/* A successful return means the event buffer is supported. */
1823 		ssif_info->has_event_buffer = true;
1824 		ssif_info->global_enables |= IPMI_BMC_EVT_MSG_BUFF;
1825 	}
1826 
1827 	/* Some systems don't behave well if you enable alerts. */
1828 	if (alerts_broken)
1829 		goto found;
1830 
1831 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1832 	msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
1833 	msg[2] = ssif_info->global_enables | IPMI_BMC_RCV_MSG_INTR;
1834 	rv = do_cmd(client, 3, msg, &len, resp);
1835 	if (rv || (len < 2)) {
1836 		dev_warn(&ssif_info->client->dev,
1837 			 "Error setting global enables: %d %d %2.2x\n",
1838 			 rv, len, resp[2]);
1839 		rv = 0; /* Not fatal */
1840 		goto found;
1841 	}
1842 
1843 	if (resp[2] == 0) {
1844 		/* A successful return means the alert is supported. */
1845 		ssif_info->supports_alert = true;
1846 		ssif_info->global_enables |= IPMI_BMC_RCV_MSG_INTR;
1847 	}
1848 
1849  found:
1850 	if (ssif_dbg_probe) {
1851 		dev_dbg(&ssif_info->client->dev,
1852 		       "%s: i2c_probe found device at i2c address %x\n",
1853 		       __func__, client->addr);
1854 	}
1855 
1856 	spin_lock_init(&ssif_info->lock);
1857 	ssif_info->ssif_state = SSIF_IDLE;
1858 	timer_setup(&ssif_info->retry_timer, retry_timeout, 0);
1859 	timer_setup(&ssif_info->watch_timer, watch_timeout, 0);
1860 
1861 	for (i = 0; i < SSIF_NUM_STATS; i++)
1862 		atomic_set(&ssif_info->stats[i], 0);
1863 
1864 	if (ssif_info->supports_pec)
1865 		ssif_info->client->flags |= I2C_CLIENT_PEC;
1866 
1867 	ssif_info->handlers.owner = THIS_MODULE;
1868 	ssif_info->handlers.start_processing = ssif_start_processing;
1869 	ssif_info->handlers.shutdown = shutdown_ssif;
1870 	ssif_info->handlers.get_smi_info = get_smi_info;
1871 	ssif_info->handlers.sender = sender;
1872 	ssif_info->handlers.request_events = request_events;
1873 	ssif_info->handlers.set_need_watch = ssif_set_need_watch;
1874 
1875 	{
1876 		unsigned int thread_num;
1877 
1878 		thread_num = ((i2c_adapter_id(ssif_info->client->adapter)
1879 			       << 8) |
1880 			      ssif_info->client->addr);
1881 		init_completion(&ssif_info->wake_thread);
1882 		ssif_info->thread = kthread_run(ipmi_ssif_thread, ssif_info,
1883 					       "kssif%4.4x", thread_num);
1884 		if (IS_ERR(ssif_info->thread)) {
1885 			rv = PTR_ERR(ssif_info->thread);
1886 			dev_notice(&ssif_info->client->dev,
1887 				   "Could not start kernel thread: error %d\n",
1888 				   rv);
1889 			goto out;
1890 		}
1891 	}
1892 
1893 	dev_set_drvdata(&ssif_info->client->dev, ssif_info);
1894 	rv = device_add_group(&ssif_info->client->dev,
1895 			      &ipmi_ssif_dev_attr_group);
1896 	if (rv) {
1897 		dev_err(&ssif_info->client->dev,
1898 			"Unable to add device attributes: error %d\n",
1899 			rv);
1900 		goto out;
1901 	}
1902 
1903 	rv = ipmi_register_smi(&ssif_info->handlers,
1904 			       ssif_info,
1905 			       &ssif_info->client->dev,
1906 			       slave_addr);
1907 	if (rv) {
1908 		dev_err(&ssif_info->client->dev,
1909 			"Unable to register device: error %d\n", rv);
1910 		goto out_remove_attr;
1911 	}
1912 
1913  out:
1914 	if (rv) {
1915 		if (addr_info)
1916 			addr_info->client = NULL;
1917 
1918 		dev_err(&ssif_info->client->dev,
1919 			"Unable to start IPMI SSIF: %d\n", rv);
1920 		i2c_set_clientdata(client, NULL);
1921 		kfree(ssif_info);
1922 	}
1923 	kfree(resp);
1924 	mutex_unlock(&ssif_infos_mutex);
1925 	return rv;
1926 
1927 out_remove_attr:
1928 	device_remove_group(&ssif_info->client->dev, &ipmi_ssif_dev_attr_group);
1929 	dev_set_drvdata(&ssif_info->client->dev, NULL);
1930 	goto out;
1931 }
1932 
new_ssif_client(int addr, char *adapter_name, int debug, int slave_addr, enum ipmi_addr_src addr_src, struct device *dev)1933 static int new_ssif_client(int addr, char *adapter_name,
1934 			   int debug, int slave_addr,
1935 			   enum ipmi_addr_src addr_src,
1936 			   struct device *dev)
1937 {
1938 	struct ssif_addr_info *addr_info;
1939 	int rv = 0;
1940 
1941 	mutex_lock(&ssif_infos_mutex);
1942 	if (ssif_info_find(addr, adapter_name, false)) {
1943 		rv = -EEXIST;
1944 		goto out_unlock;
1945 	}
1946 
1947 	addr_info = kzalloc(sizeof(*addr_info), GFP_KERNEL);
1948 	if (!addr_info) {
1949 		rv = -ENOMEM;
1950 		goto out_unlock;
1951 	}
1952 
1953 	if (adapter_name) {
1954 		addr_info->adapter_name = kstrdup(adapter_name, GFP_KERNEL);
1955 		if (!addr_info->adapter_name) {
1956 			kfree(addr_info);
1957 			rv = -ENOMEM;
1958 			goto out_unlock;
1959 		}
1960 	}
1961 
1962 	strncpy(addr_info->binfo.type, DEVICE_NAME,
1963 		sizeof(addr_info->binfo.type));
1964 	addr_info->binfo.addr = addr;
1965 	addr_info->binfo.platform_data = addr_info;
1966 	addr_info->debug = debug;
1967 	addr_info->slave_addr = slave_addr;
1968 	addr_info->addr_src = addr_src;
1969 	addr_info->dev = dev;
1970 
1971 	if (dev)
1972 		dev_set_drvdata(dev, addr_info);
1973 
1974 	list_add_tail(&addr_info->link, &ssif_infos);
1975 
1976 	/* Address list will get it */
1977 
1978 out_unlock:
1979 	mutex_unlock(&ssif_infos_mutex);
1980 	return rv;
1981 }
1982 
free_ssif_clients(void)1983 static void free_ssif_clients(void)
1984 {
1985 	struct ssif_addr_info *info, *tmp;
1986 
1987 	mutex_lock(&ssif_infos_mutex);
1988 	list_for_each_entry_safe(info, tmp, &ssif_infos, link) {
1989 		list_del(&info->link);
1990 		kfree(info->adapter_name);
1991 		kfree(info);
1992 	}
1993 	mutex_unlock(&ssif_infos_mutex);
1994 }
1995 
ssif_address_list(void)1996 static unsigned short *ssif_address_list(void)
1997 {
1998 	struct ssif_addr_info *info;
1999 	unsigned int count = 0, i = 0;
2000 	unsigned short *address_list;
2001 
2002 	list_for_each_entry(info, &ssif_infos, link)
2003 		count++;
2004 
2005 	address_list = kcalloc(count + 1, sizeof(*address_list),
2006 			       GFP_KERNEL);
2007 	if (!address_list)
2008 		return NULL;
2009 
2010 	list_for_each_entry(info, &ssif_infos, link) {
2011 		unsigned short addr = info->binfo.addr;
2012 		int j;
2013 
2014 		for (j = 0; j < i; j++) {
2015 			if (address_list[j] == addr)
2016 				/* Found a dup. */
2017 				break;
2018 		}
2019 		if (j == i) /* Didn't find it in the list. */
2020 			address_list[i++] = addr;
2021 	}
2022 	address_list[i] = I2C_CLIENT_END;
2023 
2024 	return address_list;
2025 }
2026 
2027 #ifdef CONFIG_ACPI
2028 static const struct acpi_device_id ssif_acpi_match[] = {
2029 	{ "IPI0001", 0 },
2030 	{ },
2031 };
2032 MODULE_DEVICE_TABLE(acpi, ssif_acpi_match);
2033 #endif
2034 
2035 #ifdef CONFIG_DMI
dmi_ipmi_probe(struct platform_device *pdev)2036 static int dmi_ipmi_probe(struct platform_device *pdev)
2037 {
2038 	u8 slave_addr = 0;
2039 	u16 i2c_addr;
2040 	int rv;
2041 
2042 	if (!ssif_trydmi)
2043 		return -ENODEV;
2044 
2045 	rv = device_property_read_u16(&pdev->dev, "i2c-addr", &i2c_addr);
2046 	if (rv) {
2047 		dev_warn(&pdev->dev, "No i2c-addr property\n");
2048 		return -ENODEV;
2049 	}
2050 
2051 	rv = device_property_read_u8(&pdev->dev, "slave-addr", &slave_addr);
2052 	if (rv)
2053 		slave_addr = 0x20;
2054 
2055 	return new_ssif_client(i2c_addr, NULL, 0,
2056 			       slave_addr, SI_SMBIOS, &pdev->dev);
2057 }
2058 #else
dmi_ipmi_probe(struct platform_device *pdev)2059 static int dmi_ipmi_probe(struct platform_device *pdev)
2060 {
2061 	return -ENODEV;
2062 }
2063 #endif
2064 
2065 static const struct i2c_device_id ssif_id[] = {
2066 	{ DEVICE_NAME, 0 },
2067 	{ }
2068 };
2069 MODULE_DEVICE_TABLE(i2c, ssif_id);
2070 
2071 static struct i2c_driver ssif_i2c_driver = {
2072 	.class		= I2C_CLASS_HWMON,
2073 	.driver		= {
2074 		.name			= DEVICE_NAME
2075 	},
2076 	.probe		= ssif_probe,
2077 	.remove		= ssif_remove,
2078 	.alert		= ssif_alert,
2079 	.id_table	= ssif_id,
2080 	.detect		= ssif_detect
2081 };
2082 
ssif_platform_probe(struct platform_device *dev)2083 static int ssif_platform_probe(struct platform_device *dev)
2084 {
2085 	return dmi_ipmi_probe(dev);
2086 }
2087 
ssif_platform_remove(struct platform_device *dev)2088 static int ssif_platform_remove(struct platform_device *dev)
2089 {
2090 	struct ssif_addr_info *addr_info = dev_get_drvdata(&dev->dev);
2091 
2092 	if (!addr_info)
2093 		return 0;
2094 
2095 	mutex_lock(&ssif_infos_mutex);
2096 	list_del(&addr_info->link);
2097 	kfree(addr_info);
2098 	mutex_unlock(&ssif_infos_mutex);
2099 	return 0;
2100 }
2101 
2102 static const struct platform_device_id ssif_plat_ids[] = {
2103     { "dmi-ipmi-ssif", 0 },
2104     { }
2105 };
2106 
2107 static struct platform_driver ipmi_driver = {
2108 	.driver = {
2109 		.name = DEVICE_NAME,
2110 	},
2111 	.probe		= ssif_platform_probe,
2112 	.remove		= ssif_platform_remove,
2113 	.id_table       = ssif_plat_ids
2114 };
2115 
init_ipmi_ssif(void)2116 static int init_ipmi_ssif(void)
2117 {
2118 	int i;
2119 	int rv;
2120 
2121 	if (initialized)
2122 		return 0;
2123 
2124 	pr_info("IPMI SSIF Interface driver\n");
2125 
2126 	/* build list for i2c from addr list */
2127 	for (i = 0; i < num_addrs; i++) {
2128 		rv = new_ssif_client(addr[i], adapter_name[i],
2129 				     dbg[i], slave_addrs[i],
2130 				     SI_HARDCODED, NULL);
2131 		if (rv)
2132 			pr_err("Couldn't add hardcoded device at addr 0x%x\n",
2133 			       addr[i]);
2134 	}
2135 
2136 	if (ssif_tryacpi)
2137 		ssif_i2c_driver.driver.acpi_match_table	=
2138 			ACPI_PTR(ssif_acpi_match);
2139 
2140 	if (ssif_trydmi) {
2141 		rv = platform_driver_register(&ipmi_driver);
2142 		if (rv)
2143 			pr_err("Unable to register driver: %d\n", rv);
2144 		else
2145 			platform_registered = true;
2146 	}
2147 
2148 	ssif_i2c_driver.address_list = ssif_address_list();
2149 
2150 	rv = i2c_add_driver(&ssif_i2c_driver);
2151 	if (!rv)
2152 		initialized = true;
2153 
2154 	return rv;
2155 }
2156 module_init(init_ipmi_ssif);
2157 
cleanup_ipmi_ssif(void)2158 static void cleanup_ipmi_ssif(void)
2159 {
2160 	if (!initialized)
2161 		return;
2162 
2163 	initialized = false;
2164 
2165 	i2c_del_driver(&ssif_i2c_driver);
2166 
2167 	kfree(ssif_i2c_driver.address_list);
2168 
2169 	if (ssif_trydmi && platform_registered)
2170 		platform_driver_unregister(&ipmi_driver);
2171 
2172 	free_ssif_clients();
2173 }
2174 module_exit(cleanup_ipmi_ssif);
2175 
2176 MODULE_ALIAS("platform:dmi-ipmi-ssif");
2177 MODULE_AUTHOR("Todd C Davis <todd.c.davis@intel.com>, Corey Minyard <minyard@acm.org>");
2178 MODULE_DESCRIPTION("IPMI driver for management controllers on a SMBus");
2179 MODULE_LICENSE("GPL");
2180