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