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