xref: /openbmc/linux/drivers/char/ipmi/ipmi_ssif.c (revision 2427f03f)
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 void 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 }
526 
527 
528 static void msg_done_handler(struct ssif_info *ssif_info, int result,
529 			     unsigned char *data, unsigned int len);
530 
531 static void start_get(struct ssif_info *ssif_info)
532 {
533 	ssif_info->rtc_us_timer = 0;
534 	ssif_info->multi_pos = 0;
535 
536 	ssif_i2c_send(ssif_info, msg_done_handler, I2C_SMBUS_READ,
537 		  SSIF_IPMI_RESPONSE,
538 		  ssif_info->recv, I2C_SMBUS_BLOCK_DATA);
539 }
540 
541 static void retry_timeout(struct timer_list *t)
542 {
543 	struct ssif_info *ssif_info = from_timer(ssif_info, t, retry_timer);
544 	unsigned long oflags, *flags;
545 	bool waiting;
546 
547 	if (ssif_info->stopping)
548 		return;
549 
550 	flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
551 	waiting = ssif_info->waiting_alert;
552 	ssif_info->waiting_alert = false;
553 	ipmi_ssif_unlock_cond(ssif_info, flags);
554 
555 	if (waiting)
556 		start_get(ssif_info);
557 }
558 
559 static void watch_timeout(struct timer_list *t)
560 {
561 	struct ssif_info *ssif_info = from_timer(ssif_info, t, watch_timer);
562 	unsigned long oflags, *flags;
563 
564 	if (ssif_info->stopping)
565 		return;
566 
567 	flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
568 	if (ssif_info->watch_timeout) {
569 		mod_timer(&ssif_info->watch_timer,
570 			  jiffies + ssif_info->watch_timeout);
571 		if (SSIF_IDLE(ssif_info)) {
572 			start_flag_fetch(ssif_info, flags); /* Releases lock */
573 			return;
574 		}
575 		ssif_info->req_flags = true;
576 	}
577 	ipmi_ssif_unlock_cond(ssif_info, flags);
578 }
579 
580 static void ssif_alert(struct i2c_client *client, enum i2c_alert_protocol type,
581 		       unsigned int data)
582 {
583 	struct ssif_info *ssif_info = i2c_get_clientdata(client);
584 	unsigned long oflags, *flags;
585 	bool do_get = false;
586 
587 	if (type != I2C_PROTOCOL_SMBUS_ALERT)
588 		return;
589 
590 	ssif_inc_stat(ssif_info, alerts);
591 
592 	flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
593 	if (ssif_info->waiting_alert) {
594 		ssif_info->waiting_alert = false;
595 		del_timer(&ssif_info->retry_timer);
596 		do_get = true;
597 	} else if (ssif_info->curr_msg) {
598 		ssif_info->got_alert = true;
599 	}
600 	ipmi_ssif_unlock_cond(ssif_info, flags);
601 	if (do_get)
602 		start_get(ssif_info);
603 }
604 
605 static int start_resend(struct ssif_info *ssif_info);
606 
607 static void msg_done_handler(struct ssif_info *ssif_info, int result,
608 			     unsigned char *data, unsigned int len)
609 {
610 	struct ipmi_smi_msg *msg;
611 	unsigned long oflags, *flags;
612 
613 	/*
614 	 * We are single-threaded here, so no need for a lock until we
615 	 * start messing with driver states or the queues.
616 	 */
617 
618 	if (result < 0) {
619 		ssif_info->retries_left--;
620 		if (ssif_info->retries_left > 0) {
621 			ssif_inc_stat(ssif_info, receive_retries);
622 
623 			flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
624 			ssif_info->waiting_alert = true;
625 			ssif_info->rtc_us_timer = SSIF_MSG_USEC;
626 			if (!ssif_info->stopping)
627 				mod_timer(&ssif_info->retry_timer,
628 					  jiffies + SSIF_MSG_JIFFIES);
629 			ipmi_ssif_unlock_cond(ssif_info, flags);
630 			return;
631 		}
632 
633 		ssif_inc_stat(ssif_info, receive_errors);
634 
635 		if  (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
636 			dev_dbg(&ssif_info->client->dev,
637 				"%s: Error %d\n", __func__, result);
638 		len = 0;
639 		goto continue_op;
640 	}
641 
642 	if ((len > 1) && (ssif_info->multi_pos == 0)
643 				&& (data[0] == 0x00) && (data[1] == 0x01)) {
644 		/* Start of multi-part read.  Start the next transaction. */
645 		int i;
646 
647 		ssif_inc_stat(ssif_info, received_message_parts);
648 
649 		/* Remove the multi-part read marker. */
650 		len -= 2;
651 		data += 2;
652 		for (i = 0; i < len; i++)
653 			ssif_info->data[i] = data[i];
654 		ssif_info->multi_len = len;
655 		ssif_info->multi_pos = 1;
656 
657 		ssif_i2c_send(ssif_info, msg_done_handler, I2C_SMBUS_READ,
658 			 SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE,
659 			 ssif_info->recv, I2C_SMBUS_BLOCK_DATA);
660 		return;
661 	} else if (ssif_info->multi_pos) {
662 		/* Middle of multi-part read.  Start the next transaction. */
663 		int i;
664 		unsigned char blocknum;
665 
666 		if (len == 0) {
667 			result = -EIO;
668 			if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
669 				dev_dbg(&ssif_info->client->dev,
670 					"Middle message with no data\n");
671 
672 			goto continue_op;
673 		}
674 
675 		blocknum = data[0];
676 		len--;
677 		data++;
678 
679 		if (blocknum != 0xff && len != 31) {
680 		    /* All blocks but the last must have 31 data bytes. */
681 			result = -EIO;
682 			if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
683 				dev_dbg(&ssif_info->client->dev,
684 					"Received middle message <31\n");
685 
686 			goto continue_op;
687 		}
688 
689 		if (ssif_info->multi_len + len > IPMI_MAX_MSG_LENGTH) {
690 			/* Received message too big, abort the operation. */
691 			result = -E2BIG;
692 			if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
693 				dev_dbg(&ssif_info->client->dev,
694 					"Received message too big\n");
695 
696 			goto continue_op;
697 		}
698 
699 		for (i = 0; i < len; i++)
700 			ssif_info->data[i + ssif_info->multi_len] = data[i];
701 		ssif_info->multi_len += len;
702 		if (blocknum == 0xff) {
703 			/* End of read */
704 			len = ssif_info->multi_len;
705 			data = ssif_info->data;
706 		} else if (blocknum + 1 != ssif_info->multi_pos) {
707 			/*
708 			 * Out of sequence block, just abort.  Block
709 			 * numbers start at zero for the second block,
710 			 * but multi_pos starts at one, so the +1.
711 			 */
712 			if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
713 				dev_dbg(&ssif_info->client->dev,
714 					"Received message out of sequence, expected %u, got %u\n",
715 					ssif_info->multi_pos - 1, blocknum);
716 			result = -EIO;
717 		} else {
718 			ssif_inc_stat(ssif_info, received_message_parts);
719 
720 			ssif_info->multi_pos++;
721 
722 			ssif_i2c_send(ssif_info, msg_done_handler,
723 				  I2C_SMBUS_READ,
724 				  SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE,
725 				  ssif_info->recv,
726 				  I2C_SMBUS_BLOCK_DATA);
727 			return;
728 		}
729 	}
730 
731  continue_op:
732 	if (result < 0) {
733 		ssif_inc_stat(ssif_info, receive_errors);
734 	} else {
735 		ssif_inc_stat(ssif_info, received_messages);
736 		ssif_inc_stat(ssif_info, received_message_parts);
737 	}
738 
739 	if (ssif_info->ssif_debug & SSIF_DEBUG_STATE)
740 		dev_dbg(&ssif_info->client->dev,
741 			"DONE 1: state = %d, result=%d\n",
742 			ssif_info->ssif_state, result);
743 
744 	flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
745 	msg = ssif_info->curr_msg;
746 	if (msg) {
747 		if (data) {
748 			if (len > IPMI_MAX_MSG_LENGTH)
749 				len = IPMI_MAX_MSG_LENGTH;
750 			memcpy(msg->rsp, data, len);
751 		} else {
752 			len = 0;
753 		}
754 		msg->rsp_size = len;
755 		ssif_info->curr_msg = NULL;
756 	}
757 
758 	switch (ssif_info->ssif_state) {
759 	case SSIF_NORMAL:
760 		ipmi_ssif_unlock_cond(ssif_info, flags);
761 		if (!msg)
762 			break;
763 
764 		if (result < 0)
765 			return_hosed_msg(ssif_info, msg);
766 		else
767 			deliver_recv_msg(ssif_info, msg);
768 		break;
769 
770 	case SSIF_GETTING_FLAGS:
771 		/* We got the flags from the SSIF, now handle them. */
772 		if ((result < 0) || (len < 4) || (data[2] != 0)) {
773 			/*
774 			 * Error fetching flags, or invalid length,
775 			 * just give up for now.
776 			 */
777 			ssif_info->ssif_state = SSIF_NORMAL;
778 			ipmi_ssif_unlock_cond(ssif_info, flags);
779 			dev_warn(&ssif_info->client->dev,
780 				 "Error getting flags: %d %d, %x\n",
781 				 result, len, (len >= 3) ? data[2] : 0);
782 		} else if (data[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
783 			   || data[1] != IPMI_GET_MSG_FLAGS_CMD) {
784 			/*
785 			 * Don't abort here, maybe it was a queued
786 			 * response to a previous command.
787 			 */
788 			ipmi_ssif_unlock_cond(ssif_info, flags);
789 			dev_warn(&ssif_info->client->dev,
790 				 "Invalid response getting flags: %x %x\n",
791 				 data[0], data[1]);
792 		} else {
793 			ssif_inc_stat(ssif_info, flag_fetches);
794 			ssif_info->msg_flags = data[3];
795 			handle_flags(ssif_info, flags);
796 		}
797 		break;
798 
799 	case SSIF_CLEARING_FLAGS:
800 		/* We cleared the flags. */
801 		if ((result < 0) || (len < 3) || (data[2] != 0)) {
802 			/* Error clearing flags */
803 			dev_warn(&ssif_info->client->dev,
804 				 "Error clearing flags: %d %d, %x\n",
805 				 result, len, (len >= 3) ? data[2] : 0);
806 		} else if (data[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
807 			   || data[1] != IPMI_CLEAR_MSG_FLAGS_CMD) {
808 			dev_warn(&ssif_info->client->dev,
809 				 "Invalid response clearing flags: %x %x\n",
810 				 data[0], data[1]);
811 		}
812 		ssif_info->ssif_state = SSIF_NORMAL;
813 		ipmi_ssif_unlock_cond(ssif_info, flags);
814 		break;
815 
816 	case SSIF_GETTING_EVENTS:
817 		if ((result < 0) || (len < 3) || (msg->rsp[2] != 0)) {
818 			/* Error getting event, probably done. */
819 			msg->done(msg);
820 
821 			/* Take off the event flag. */
822 			ssif_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
823 			handle_flags(ssif_info, flags);
824 		} else if (msg->rsp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
825 			   || msg->rsp[1] != IPMI_READ_EVENT_MSG_BUFFER_CMD) {
826 			dev_warn(&ssif_info->client->dev,
827 				 "Invalid response getting events: %x %x\n",
828 				 msg->rsp[0], msg->rsp[1]);
829 			msg->done(msg);
830 			/* Take off the event flag. */
831 			ssif_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
832 			handle_flags(ssif_info, flags);
833 		} else {
834 			handle_flags(ssif_info, flags);
835 			ssif_inc_stat(ssif_info, events);
836 			deliver_recv_msg(ssif_info, msg);
837 		}
838 		break;
839 
840 	case SSIF_GETTING_MESSAGES:
841 		if ((result < 0) || (len < 3) || (msg->rsp[2] != 0)) {
842 			/* Error getting event, probably done. */
843 			msg->done(msg);
844 
845 			/* Take off the msg flag. */
846 			ssif_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
847 			handle_flags(ssif_info, flags);
848 		} else if (msg->rsp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
849 			   || msg->rsp[1] != IPMI_GET_MSG_CMD) {
850 			dev_warn(&ssif_info->client->dev,
851 				 "Invalid response clearing flags: %x %x\n",
852 				 msg->rsp[0], msg->rsp[1]);
853 			msg->done(msg);
854 
855 			/* Take off the msg flag. */
856 			ssif_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
857 			handle_flags(ssif_info, flags);
858 		} else {
859 			ssif_inc_stat(ssif_info, incoming_messages);
860 			handle_flags(ssif_info, flags);
861 			deliver_recv_msg(ssif_info, msg);
862 		}
863 		break;
864 	}
865 
866 	flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
867 	if (SSIF_IDLE(ssif_info) && !ssif_info->stopping) {
868 		if (ssif_info->req_events)
869 			start_event_fetch(ssif_info, flags);
870 		else if (ssif_info->req_flags)
871 			start_flag_fetch(ssif_info, flags);
872 		else
873 			start_next_msg(ssif_info, flags);
874 	} else
875 		ipmi_ssif_unlock_cond(ssif_info, flags);
876 
877 	if (ssif_info->ssif_debug & SSIF_DEBUG_STATE)
878 		dev_dbg(&ssif_info->client->dev,
879 			"DONE 2: state = %d.\n", ssif_info->ssif_state);
880 }
881 
882 static void msg_written_handler(struct ssif_info *ssif_info, int result,
883 				unsigned char *data, unsigned int len)
884 {
885 	/* We are single-threaded here, so no need for a lock. */
886 	if (result < 0) {
887 		ssif_info->retries_left--;
888 		if (ssif_info->retries_left > 0) {
889 			if (!start_resend(ssif_info)) {
890 				ssif_inc_stat(ssif_info, send_retries);
891 				return;
892 			}
893 			/* request failed, just return the error. */
894 			ssif_inc_stat(ssif_info, send_errors);
895 
896 			if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
897 				dev_dbg(&ssif_info->client->dev,
898 					"%s: Out of retries\n", __func__);
899 			msg_done_handler(ssif_info, -EIO, NULL, 0);
900 			return;
901 		}
902 
903 		ssif_inc_stat(ssif_info, send_errors);
904 
905 		/*
906 		 * Got an error on transmit, let the done routine
907 		 * handle it.
908 		 */
909 		if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
910 			dev_dbg(&ssif_info->client->dev,
911 				"%s: Error  %d\n", __func__, result);
912 
913 		msg_done_handler(ssif_info, result, NULL, 0);
914 		return;
915 	}
916 
917 	if (ssif_info->multi_data) {
918 		/*
919 		 * In the middle of a multi-data write.  See the comment
920 		 * in the SSIF_MULTI_n_PART case in the probe function
921 		 * for details on the intricacies of this.
922 		 */
923 		int left, to_write;
924 		unsigned char *data_to_send;
925 		unsigned char cmd;
926 
927 		ssif_inc_stat(ssif_info, sent_messages_parts);
928 
929 		left = ssif_info->multi_len - ssif_info->multi_pos;
930 		to_write = left;
931 		if (to_write > 32)
932 			to_write = 32;
933 		/* Length byte. */
934 		ssif_info->multi_data[ssif_info->multi_pos] = to_write;
935 		data_to_send = ssif_info->multi_data + ssif_info->multi_pos;
936 		ssif_info->multi_pos += to_write;
937 		cmd = SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE;
938 		if (ssif_info->cmd8_works) {
939 			if (left == to_write) {
940 				cmd = SSIF_IPMI_MULTI_PART_REQUEST_END;
941 				ssif_info->multi_data = NULL;
942 			}
943 		} else if (to_write < 32) {
944 			ssif_info->multi_data = NULL;
945 		}
946 
947 		ssif_i2c_send(ssif_info, msg_written_handler,
948 			  I2C_SMBUS_WRITE, cmd,
949 			  data_to_send, I2C_SMBUS_BLOCK_DATA);
950 	} else {
951 		/* Ready to request the result. */
952 		unsigned long oflags, *flags;
953 
954 		ssif_inc_stat(ssif_info, sent_messages);
955 		ssif_inc_stat(ssif_info, sent_messages_parts);
956 
957 		flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
958 		if (ssif_info->got_alert) {
959 			/* The result is already ready, just start it. */
960 			ssif_info->got_alert = false;
961 			ipmi_ssif_unlock_cond(ssif_info, flags);
962 			start_get(ssif_info);
963 		} else {
964 			/* Wait a jiffie then request the next message */
965 			ssif_info->waiting_alert = true;
966 			ssif_info->retries_left = SSIF_RECV_RETRIES;
967 			ssif_info->rtc_us_timer = SSIF_MSG_PART_USEC;
968 			if (!ssif_info->stopping)
969 				mod_timer(&ssif_info->retry_timer,
970 					  jiffies + SSIF_MSG_PART_JIFFIES);
971 			ipmi_ssif_unlock_cond(ssif_info, flags);
972 		}
973 	}
974 }
975 
976 static int start_resend(struct ssif_info *ssif_info)
977 {
978 	int command;
979 
980 	ssif_info->got_alert = false;
981 
982 	if (ssif_info->data_len > 32) {
983 		command = SSIF_IPMI_MULTI_PART_REQUEST_START;
984 		ssif_info->multi_data = ssif_info->data;
985 		ssif_info->multi_len = ssif_info->data_len;
986 		/*
987 		 * Subtle thing, this is 32, not 33, because we will
988 		 * overwrite the thing at position 32 (which was just
989 		 * transmitted) with the new length.
990 		 */
991 		ssif_info->multi_pos = 32;
992 		ssif_info->data[0] = 32;
993 	} else {
994 		ssif_info->multi_data = NULL;
995 		command = SSIF_IPMI_REQUEST;
996 		ssif_info->data[0] = ssif_info->data_len;
997 	}
998 
999 	ssif_i2c_send(ssif_info, msg_written_handler, I2C_SMBUS_WRITE,
1000 		   command, ssif_info->data, I2C_SMBUS_BLOCK_DATA);
1001 	return 0;
1002 }
1003 
1004 static int start_send(struct ssif_info *ssif_info,
1005 		      unsigned char   *data,
1006 		      unsigned int    len)
1007 {
1008 	if (len > IPMI_MAX_MSG_LENGTH)
1009 		return -E2BIG;
1010 	if (len > ssif_info->max_xmit_msg_size)
1011 		return -E2BIG;
1012 
1013 	ssif_info->retries_left = SSIF_SEND_RETRIES;
1014 	memcpy(ssif_info->data + 1, data, len);
1015 	ssif_info->data_len = len;
1016 	return start_resend(ssif_info);
1017 }
1018 
1019 /* Must be called with the message lock held. */
1020 static void start_next_msg(struct ssif_info *ssif_info, unsigned long *flags)
1021 {
1022 	struct ipmi_smi_msg *msg;
1023 	unsigned long oflags;
1024 
1025  restart:
1026 	if (!SSIF_IDLE(ssif_info)) {
1027 		ipmi_ssif_unlock_cond(ssif_info, flags);
1028 		return;
1029 	}
1030 
1031 	if (!ssif_info->waiting_msg) {
1032 		ssif_info->curr_msg = NULL;
1033 		ipmi_ssif_unlock_cond(ssif_info, flags);
1034 	} else {
1035 		int rv;
1036 
1037 		ssif_info->curr_msg = ssif_info->waiting_msg;
1038 		ssif_info->waiting_msg = NULL;
1039 		ipmi_ssif_unlock_cond(ssif_info, flags);
1040 		rv = start_send(ssif_info,
1041 				ssif_info->curr_msg->data,
1042 				ssif_info->curr_msg->data_size);
1043 		if (rv) {
1044 			msg = ssif_info->curr_msg;
1045 			ssif_info->curr_msg = NULL;
1046 			return_hosed_msg(ssif_info, msg);
1047 			flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1048 			goto restart;
1049 		}
1050 	}
1051 }
1052 
1053 static void sender(void                *send_info,
1054 		   struct ipmi_smi_msg *msg)
1055 {
1056 	struct ssif_info *ssif_info = (struct ssif_info *) send_info;
1057 	unsigned long oflags, *flags;
1058 
1059 	BUG_ON(ssif_info->waiting_msg);
1060 	ssif_info->waiting_msg = msg;
1061 
1062 	flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1063 	start_next_msg(ssif_info, flags);
1064 
1065 	if (ssif_info->ssif_debug & SSIF_DEBUG_TIMING) {
1066 		struct timespec64 t;
1067 
1068 		ktime_get_real_ts64(&t);
1069 		dev_dbg(&ssif_info->client->dev,
1070 			"**Enqueue %02x %02x: %lld.%6.6ld\n",
1071 			msg->data[0], msg->data[1],
1072 			(long long)t.tv_sec, (long)t.tv_nsec / NSEC_PER_USEC);
1073 	}
1074 }
1075 
1076 static int get_smi_info(void *send_info, struct ipmi_smi_info *data)
1077 {
1078 	struct ssif_info *ssif_info = send_info;
1079 
1080 	data->addr_src = ssif_info->addr_source;
1081 	data->dev = &ssif_info->client->dev;
1082 	data->addr_info = ssif_info->addr_info;
1083 	get_device(data->dev);
1084 
1085 	return 0;
1086 }
1087 
1088 /*
1089  * Upper layer wants us to request events.
1090  */
1091 static void request_events(void *send_info)
1092 {
1093 	struct ssif_info *ssif_info = (struct ssif_info *) send_info;
1094 	unsigned long oflags, *flags;
1095 
1096 	if (!ssif_info->has_event_buffer)
1097 		return;
1098 
1099 	flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1100 	ssif_info->req_events = true;
1101 	ipmi_ssif_unlock_cond(ssif_info, flags);
1102 }
1103 
1104 /*
1105  * Upper layer is changing the flag saying whether we need to request
1106  * flags periodically or not.
1107  */
1108 static void ssif_set_need_watch(void *send_info, unsigned int watch_mask)
1109 {
1110 	struct ssif_info *ssif_info = (struct ssif_info *) send_info;
1111 	unsigned long oflags, *flags;
1112 	long timeout = 0;
1113 
1114 	if (watch_mask & IPMI_WATCH_MASK_CHECK_MESSAGES)
1115 		timeout = SSIF_WATCH_MSG_TIMEOUT;
1116 	else if (watch_mask)
1117 		timeout = SSIF_WATCH_WATCHDOG_TIMEOUT;
1118 
1119 	flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1120 	if (timeout != ssif_info->watch_timeout) {
1121 		ssif_info->watch_timeout = timeout;
1122 		if (ssif_info->watch_timeout)
1123 			mod_timer(&ssif_info->watch_timer,
1124 				  jiffies + ssif_info->watch_timeout);
1125 	}
1126 	ipmi_ssif_unlock_cond(ssif_info, flags);
1127 }
1128 
1129 static int ssif_start_processing(void            *send_info,
1130 				 struct ipmi_smi *intf)
1131 {
1132 	struct ssif_info *ssif_info = send_info;
1133 
1134 	ssif_info->intf = intf;
1135 
1136 	return 0;
1137 }
1138 
1139 #define MAX_SSIF_BMCS 4
1140 
1141 static unsigned short addr[MAX_SSIF_BMCS];
1142 static int num_addrs;
1143 module_param_array(addr, ushort, &num_addrs, 0);
1144 MODULE_PARM_DESC(addr, "The addresses to scan for IPMI BMCs on the SSIFs.");
1145 
1146 static char *adapter_name[MAX_SSIF_BMCS];
1147 static int num_adapter_names;
1148 module_param_array(adapter_name, charp, &num_adapter_names, 0);
1149 MODULE_PARM_DESC(adapter_name, "The string name of the I2C device that has the BMC.  By default all devices are scanned.");
1150 
1151 static int slave_addrs[MAX_SSIF_BMCS];
1152 static int num_slave_addrs;
1153 module_param_array(slave_addrs, int, &num_slave_addrs, 0);
1154 MODULE_PARM_DESC(slave_addrs,
1155 		 "The default IPMB slave address for the controller.");
1156 
1157 static bool alerts_broken;
1158 module_param(alerts_broken, bool, 0);
1159 MODULE_PARM_DESC(alerts_broken, "Don't enable alerts for the controller.");
1160 
1161 /*
1162  * Bit 0 enables message debugging, bit 1 enables state debugging, and
1163  * bit 2 enables timing debugging.  This is an array indexed by
1164  * interface number"
1165  */
1166 static int dbg[MAX_SSIF_BMCS];
1167 static int num_dbg;
1168 module_param_array(dbg, int, &num_dbg, 0);
1169 MODULE_PARM_DESC(dbg, "Turn on debugging.");
1170 
1171 static bool ssif_dbg_probe;
1172 module_param_named(dbg_probe, ssif_dbg_probe, bool, 0);
1173 MODULE_PARM_DESC(dbg_probe, "Enable debugging of probing of adapters.");
1174 
1175 static bool ssif_tryacpi = true;
1176 module_param_named(tryacpi, ssif_tryacpi, bool, 0);
1177 MODULE_PARM_DESC(tryacpi, "Setting this to zero will disable the default scan of the interfaces identified via ACPI");
1178 
1179 static bool ssif_trydmi = true;
1180 module_param_named(trydmi, ssif_trydmi, bool, 0);
1181 MODULE_PARM_DESC(trydmi, "Setting this to zero will disable the default scan of the interfaces identified via DMI (SMBIOS)");
1182 
1183 static DEFINE_MUTEX(ssif_infos_mutex);
1184 static LIST_HEAD(ssif_infos);
1185 
1186 #define IPMI_SSIF_ATTR(name) \
1187 static ssize_t ipmi_##name##_show(struct device *dev,			\
1188 				  struct device_attribute *attr,	\
1189 				  char *buf)				\
1190 {									\
1191 	struct ssif_info *ssif_info = dev_get_drvdata(dev);		\
1192 									\
1193 	return sysfs_emit(buf, "%u\n", ssif_get_stat(ssif_info, name));\
1194 }									\
1195 static DEVICE_ATTR(name, S_IRUGO, ipmi_##name##_show, NULL)
1196 
1197 static ssize_t ipmi_type_show(struct device *dev,
1198 			      struct device_attribute *attr,
1199 			      char *buf)
1200 {
1201 	return sysfs_emit(buf, "ssif\n");
1202 }
1203 static DEVICE_ATTR(type, S_IRUGO, ipmi_type_show, NULL);
1204 
1205 IPMI_SSIF_ATTR(sent_messages);
1206 IPMI_SSIF_ATTR(sent_messages_parts);
1207 IPMI_SSIF_ATTR(send_retries);
1208 IPMI_SSIF_ATTR(send_errors);
1209 IPMI_SSIF_ATTR(received_messages);
1210 IPMI_SSIF_ATTR(received_message_parts);
1211 IPMI_SSIF_ATTR(receive_retries);
1212 IPMI_SSIF_ATTR(receive_errors);
1213 IPMI_SSIF_ATTR(flag_fetches);
1214 IPMI_SSIF_ATTR(hosed);
1215 IPMI_SSIF_ATTR(events);
1216 IPMI_SSIF_ATTR(watchdog_pretimeouts);
1217 IPMI_SSIF_ATTR(alerts);
1218 
1219 static struct attribute *ipmi_ssif_dev_attrs[] = {
1220 	&dev_attr_type.attr,
1221 	&dev_attr_sent_messages.attr,
1222 	&dev_attr_sent_messages_parts.attr,
1223 	&dev_attr_send_retries.attr,
1224 	&dev_attr_send_errors.attr,
1225 	&dev_attr_received_messages.attr,
1226 	&dev_attr_received_message_parts.attr,
1227 	&dev_attr_receive_retries.attr,
1228 	&dev_attr_receive_errors.attr,
1229 	&dev_attr_flag_fetches.attr,
1230 	&dev_attr_hosed.attr,
1231 	&dev_attr_events.attr,
1232 	&dev_attr_watchdog_pretimeouts.attr,
1233 	&dev_attr_alerts.attr,
1234 	NULL
1235 };
1236 
1237 static const struct attribute_group ipmi_ssif_dev_attr_group = {
1238 	.attrs		= ipmi_ssif_dev_attrs,
1239 };
1240 
1241 static void shutdown_ssif(void *send_info)
1242 {
1243 	struct ssif_info *ssif_info = send_info;
1244 
1245 	device_remove_group(&ssif_info->client->dev, &ipmi_ssif_dev_attr_group);
1246 	dev_set_drvdata(&ssif_info->client->dev, NULL);
1247 
1248 	/* make sure the driver is not looking for flags any more. */
1249 	while (ssif_info->ssif_state != SSIF_NORMAL)
1250 		schedule_timeout(1);
1251 
1252 	ssif_info->stopping = true;
1253 	del_timer_sync(&ssif_info->watch_timer);
1254 	del_timer_sync(&ssif_info->retry_timer);
1255 	if (ssif_info->thread) {
1256 		complete(&ssif_info->wake_thread);
1257 		kthread_stop(ssif_info->thread);
1258 	}
1259 }
1260 
1261 static int ssif_remove(struct i2c_client *client)
1262 {
1263 	struct ssif_info *ssif_info = i2c_get_clientdata(client);
1264 	struct ssif_addr_info *addr_info;
1265 
1266 	if (!ssif_info)
1267 		return 0;
1268 
1269 	/*
1270 	 * After this point, we won't deliver anything asychronously
1271 	 * to the message handler.  We can unregister ourself.
1272 	 */
1273 	ipmi_unregister_smi(ssif_info->intf);
1274 
1275 	list_for_each_entry(addr_info, &ssif_infos, link) {
1276 		if (addr_info->client == client) {
1277 			addr_info->client = NULL;
1278 			break;
1279 		}
1280 	}
1281 
1282 	kfree(ssif_info);
1283 
1284 	return 0;
1285 }
1286 
1287 static int read_response(struct i2c_client *client, unsigned char *resp)
1288 {
1289 	int ret = -ENODEV, retry_cnt = SSIF_RECV_RETRIES;
1290 
1291 	while (retry_cnt > 0) {
1292 		ret = i2c_smbus_read_block_data(client, SSIF_IPMI_RESPONSE,
1293 						resp);
1294 		if (ret > 0)
1295 			break;
1296 		msleep(SSIF_MSG_MSEC);
1297 		retry_cnt--;
1298 		if (retry_cnt <= 0)
1299 			break;
1300 	}
1301 
1302 	return ret;
1303 }
1304 
1305 static int do_cmd(struct i2c_client *client, int len, unsigned char *msg,
1306 		  int *resp_len, unsigned char *resp)
1307 {
1308 	int retry_cnt;
1309 	int ret;
1310 
1311 	retry_cnt = SSIF_SEND_RETRIES;
1312  retry1:
1313 	ret = i2c_smbus_write_block_data(client, SSIF_IPMI_REQUEST, len, msg);
1314 	if (ret) {
1315 		retry_cnt--;
1316 		if (retry_cnt > 0)
1317 			goto retry1;
1318 		return -ENODEV;
1319 	}
1320 
1321 	ret = read_response(client, resp);
1322 	if (ret > 0) {
1323 		/* Validate that the response is correct. */
1324 		if (ret < 3 ||
1325 		    (resp[0] != (msg[0] | (1 << 2))) ||
1326 		    (resp[1] != msg[1]))
1327 			ret = -EINVAL;
1328 		else if (ret > IPMI_MAX_MSG_LENGTH) {
1329 			ret = -E2BIG;
1330 		} else {
1331 			*resp_len = ret;
1332 			ret = 0;
1333 		}
1334 	}
1335 
1336 	return ret;
1337 }
1338 
1339 static int ssif_detect(struct i2c_client *client, struct i2c_board_info *info)
1340 {
1341 	unsigned char *resp;
1342 	unsigned char msg[3];
1343 	int           rv;
1344 	int           len;
1345 
1346 	resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1347 	if (!resp)
1348 		return -ENOMEM;
1349 
1350 	/* Do a Get Device ID command, since it is required. */
1351 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1352 	msg[1] = IPMI_GET_DEVICE_ID_CMD;
1353 	rv = do_cmd(client, 2, msg, &len, resp);
1354 	if (rv)
1355 		rv = -ENODEV;
1356 	else
1357 		strlcpy(info->type, DEVICE_NAME, I2C_NAME_SIZE);
1358 	kfree(resp);
1359 	return rv;
1360 }
1361 
1362 static int strcmp_nospace(char *s1, char *s2)
1363 {
1364 	while (*s1 && *s2) {
1365 		while (isspace(*s1))
1366 			s1++;
1367 		while (isspace(*s2))
1368 			s2++;
1369 		if (*s1 > *s2)
1370 			return 1;
1371 		if (*s1 < *s2)
1372 			return -1;
1373 		s1++;
1374 		s2++;
1375 	}
1376 	return 0;
1377 }
1378 
1379 static struct ssif_addr_info *ssif_info_find(unsigned short addr,
1380 					     char *adapter_name,
1381 					     bool match_null_name)
1382 {
1383 	struct ssif_addr_info *info, *found = NULL;
1384 
1385 restart:
1386 	list_for_each_entry(info, &ssif_infos, link) {
1387 		if (info->binfo.addr == addr) {
1388 			if (info->addr_src == SI_SMBIOS)
1389 				info->adapter_name = kstrdup(adapter_name,
1390 							     GFP_KERNEL);
1391 
1392 			if (info->adapter_name || adapter_name) {
1393 				if (!info->adapter_name != !adapter_name) {
1394 					/* One is NULL and one is not */
1395 					continue;
1396 				}
1397 				if (adapter_name &&
1398 				    strcmp_nospace(info->adapter_name,
1399 						   adapter_name))
1400 					/* Names do not match */
1401 					continue;
1402 			}
1403 			found = info;
1404 			break;
1405 		}
1406 	}
1407 
1408 	if (!found && match_null_name) {
1409 		/* Try to get an exact match first, then try with a NULL name */
1410 		adapter_name = NULL;
1411 		match_null_name = false;
1412 		goto restart;
1413 	}
1414 
1415 	return found;
1416 }
1417 
1418 static bool check_acpi(struct ssif_info *ssif_info, struct device *dev)
1419 {
1420 #ifdef CONFIG_ACPI
1421 	acpi_handle acpi_handle;
1422 
1423 	acpi_handle = ACPI_HANDLE(dev);
1424 	if (acpi_handle) {
1425 		ssif_info->addr_source = SI_ACPI;
1426 		ssif_info->addr_info.acpi_info.acpi_handle = acpi_handle;
1427 		request_module("acpi_ipmi");
1428 		return true;
1429 	}
1430 #endif
1431 	return false;
1432 }
1433 
1434 static int find_slave_address(struct i2c_client *client, int slave_addr)
1435 {
1436 #ifdef CONFIG_IPMI_DMI_DECODE
1437 	if (!slave_addr)
1438 		slave_addr = ipmi_dmi_get_slave_addr(
1439 			SI_TYPE_INVALID,
1440 			i2c_adapter_id(client->adapter),
1441 			client->addr);
1442 #endif
1443 
1444 	return slave_addr;
1445 }
1446 
1447 static int start_multipart_test(struct i2c_client *client,
1448 				unsigned char *msg, bool do_middle)
1449 {
1450 	int retry_cnt = SSIF_SEND_RETRIES, ret;
1451 
1452 retry_write:
1453 	ret = i2c_smbus_write_block_data(client,
1454 					 SSIF_IPMI_MULTI_PART_REQUEST_START,
1455 					 32, msg);
1456 	if (ret) {
1457 		retry_cnt--;
1458 		if (retry_cnt > 0)
1459 			goto retry_write;
1460 		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");
1461 		return ret;
1462 	}
1463 
1464 	if (!do_middle)
1465 		return 0;
1466 
1467 	ret = i2c_smbus_write_block_data(client,
1468 					 SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE,
1469 					 32, msg + 32);
1470 	if (ret) {
1471 		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");
1472 		return ret;
1473 	}
1474 
1475 	return 0;
1476 }
1477 
1478 static void test_multipart_messages(struct i2c_client *client,
1479 				    struct ssif_info *ssif_info,
1480 				    unsigned char *resp)
1481 {
1482 	unsigned char msg[65];
1483 	int ret;
1484 	bool do_middle;
1485 
1486 	if (ssif_info->max_xmit_msg_size <= 32)
1487 		return;
1488 
1489 	do_middle = ssif_info->max_xmit_msg_size > 63;
1490 
1491 	memset(msg, 0, sizeof(msg));
1492 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1493 	msg[1] = IPMI_GET_DEVICE_ID_CMD;
1494 
1495 	/*
1496 	 * The specification is all messed up dealing with sending
1497 	 * multi-part messages.  Per what the specification says, it
1498 	 * is impossible to send a message that is a multiple of 32
1499 	 * bytes, except for 32 itself.  It talks about a "start"
1500 	 * transaction (cmd=6) that must be 32 bytes, "middle"
1501 	 * transaction (cmd=7) that must be 32 bytes, and an "end"
1502 	 * transaction.  The "end" transaction is shown as cmd=7 in
1503 	 * the text, but if that's the case there is no way to
1504 	 * differentiate between a middle and end part except the
1505 	 * length being less than 32.  But there is a table at the far
1506 	 * end of the section (that I had never noticed until someone
1507 	 * pointed it out to me) that mentions it as cmd=8.
1508 	 *
1509 	 * After some thought, I think the example is wrong and the
1510 	 * end transaction should be cmd=8.  But some systems don't
1511 	 * implement cmd=8, they use a zero-length end transaction,
1512 	 * even though that violates the SMBus specification.
1513 	 *
1514 	 * So, to work around this, this code tests if cmd=8 works.
1515 	 * If it does, then we use that.  If not, it tests zero-
1516 	 * byte end transactions.  If that works, good.  If not,
1517 	 * we only allow 63-byte transactions max.
1518 	 */
1519 
1520 	ret = start_multipart_test(client, msg, do_middle);
1521 	if (ret)
1522 		goto out_no_multi_part;
1523 
1524 	ret = i2c_smbus_write_block_data(client,
1525 					 SSIF_IPMI_MULTI_PART_REQUEST_END,
1526 					 1, msg + 64);
1527 
1528 	if (!ret)
1529 		ret = read_response(client, resp);
1530 
1531 	if (ret > 0) {
1532 		/* End transactions work, we are good. */
1533 		ssif_info->cmd8_works = true;
1534 		return;
1535 	}
1536 
1537 	ret = start_multipart_test(client, msg, do_middle);
1538 	if (ret) {
1539 		dev_err(&client->dev, "Second multipart test failed.\n");
1540 		goto out_no_multi_part;
1541 	}
1542 
1543 	ret = i2c_smbus_write_block_data(client,
1544 					 SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE,
1545 					 0, msg + 64);
1546 	if (!ret)
1547 		ret = read_response(client, resp);
1548 	if (ret > 0)
1549 		/* Zero-size end parts work, use those. */
1550 		return;
1551 
1552 	/* Limit to 63 bytes and use a short middle command to mark the end. */
1553 	if (ssif_info->max_xmit_msg_size > 63)
1554 		ssif_info->max_xmit_msg_size = 63;
1555 	return;
1556 
1557 out_no_multi_part:
1558 	ssif_info->max_xmit_msg_size = 32;
1559 	return;
1560 }
1561 
1562 /*
1563  * Global enables we care about.
1564  */
1565 #define GLOBAL_ENABLES_MASK (IPMI_BMC_EVT_MSG_BUFF | IPMI_BMC_RCV_MSG_INTR | \
1566 			     IPMI_BMC_EVT_MSG_INTR)
1567 
1568 static void ssif_remove_dup(struct i2c_client *client)
1569 {
1570 	struct ssif_info *ssif_info = i2c_get_clientdata(client);
1571 
1572 	ipmi_unregister_smi(ssif_info->intf);
1573 	kfree(ssif_info);
1574 }
1575 
1576 static int ssif_add_infos(struct i2c_client *client)
1577 {
1578 	struct ssif_addr_info *info;
1579 
1580 	info = kzalloc(sizeof(*info), GFP_KERNEL);
1581 	if (!info)
1582 		return -ENOMEM;
1583 	info->addr_src = SI_ACPI;
1584 	info->client = client;
1585 	info->adapter_name = kstrdup(client->adapter->name, GFP_KERNEL);
1586 	info->binfo.addr = client->addr;
1587 	list_add_tail(&info->link, &ssif_infos);
1588 	return 0;
1589 }
1590 
1591 /*
1592  * Prefer ACPI over SMBIOS, if both are available.
1593  * So if we get an ACPI interface and have already registered a SMBIOS
1594  * interface at the same address, remove the SMBIOS and add the ACPI one.
1595  */
1596 static int ssif_check_and_remove(struct i2c_client *client,
1597 			      struct ssif_info *ssif_info)
1598 {
1599 	struct ssif_addr_info *info;
1600 
1601 	list_for_each_entry(info, &ssif_infos, link) {
1602 		if (!info->client)
1603 			return 0;
1604 		if (!strcmp(info->adapter_name, client->adapter->name) &&
1605 		    info->binfo.addr == client->addr) {
1606 			if (info->addr_src == SI_ACPI)
1607 				return -EEXIST;
1608 
1609 			if (ssif_info->addr_source == SI_ACPI &&
1610 			    info->addr_src == SI_SMBIOS) {
1611 				dev_info(&client->dev,
1612 					 "Removing %s-specified SSIF interface in favor of ACPI\n",
1613 					 ipmi_addr_src_to_str(info->addr_src));
1614 				ssif_remove_dup(info->client);
1615 				return 0;
1616 			}
1617 		}
1618 	}
1619 	return 0;
1620 }
1621 
1622 static int ssif_probe(struct i2c_client *client, const struct i2c_device_id *id)
1623 {
1624 	unsigned char     msg[3];
1625 	unsigned char     *resp;
1626 	struct ssif_info   *ssif_info;
1627 	int               rv = 0;
1628 	int               len;
1629 	int               i;
1630 	u8		  slave_addr = 0;
1631 	struct ssif_addr_info *addr_info = NULL;
1632 
1633 	mutex_lock(&ssif_infos_mutex);
1634 	resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1635 	if (!resp) {
1636 		mutex_unlock(&ssif_infos_mutex);
1637 		return -ENOMEM;
1638 	}
1639 
1640 	ssif_info = kzalloc(sizeof(*ssif_info), GFP_KERNEL);
1641 	if (!ssif_info) {
1642 		kfree(resp);
1643 		mutex_unlock(&ssif_infos_mutex);
1644 		return -ENOMEM;
1645 	}
1646 
1647 	if (!check_acpi(ssif_info, &client->dev)) {
1648 		addr_info = ssif_info_find(client->addr, client->adapter->name,
1649 					   true);
1650 		if (!addr_info) {
1651 			/* Must have come in through sysfs. */
1652 			ssif_info->addr_source = SI_HOTMOD;
1653 		} else {
1654 			ssif_info->addr_source = addr_info->addr_src;
1655 			ssif_info->ssif_debug = addr_info->debug;
1656 			ssif_info->addr_info = addr_info->addr_info;
1657 			addr_info->client = client;
1658 			slave_addr = addr_info->slave_addr;
1659 		}
1660 	}
1661 
1662 	ssif_info->client = client;
1663 	i2c_set_clientdata(client, ssif_info);
1664 
1665 	rv = ssif_check_and_remove(client, ssif_info);
1666 	/* If rv is 0 and addr source is not SI_ACPI, continue probing */
1667 	if (!rv && ssif_info->addr_source == SI_ACPI) {
1668 		rv = ssif_add_infos(client);
1669 		if (rv) {
1670 			dev_err(&client->dev, "Out of memory!, exiting ..\n");
1671 			goto out;
1672 		}
1673 	} else if (rv) {
1674 		dev_err(&client->dev, "Not probing, Interface already present\n");
1675 		goto out;
1676 	}
1677 
1678 	slave_addr = find_slave_address(client, slave_addr);
1679 
1680 	dev_info(&client->dev,
1681 		 "Trying %s-specified SSIF interface at i2c address 0x%x, adapter %s, slave address 0x%x\n",
1682 		ipmi_addr_src_to_str(ssif_info->addr_source),
1683 		client->addr, client->adapter->name, slave_addr);
1684 
1685 	/* Now check for system interface capabilities */
1686 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1687 	msg[1] = IPMI_GET_SYSTEM_INTERFACE_CAPABILITIES_CMD;
1688 	msg[2] = 0; /* SSIF */
1689 	rv = do_cmd(client, 3, msg, &len, resp);
1690 	if (!rv && (len >= 3) && (resp[2] == 0)) {
1691 		if (len < 7) {
1692 			if (ssif_dbg_probe)
1693 				dev_dbg(&ssif_info->client->dev,
1694 					"SSIF info too short: %d\n", len);
1695 			goto no_support;
1696 		}
1697 
1698 		/* Got a good SSIF response, handle it. */
1699 		ssif_info->max_xmit_msg_size = resp[5];
1700 		ssif_info->max_recv_msg_size = resp[6];
1701 		ssif_info->multi_support = (resp[4] >> 6) & 0x3;
1702 		ssif_info->supports_pec = (resp[4] >> 3) & 0x1;
1703 
1704 		/* Sanitize the data */
1705 		switch (ssif_info->multi_support) {
1706 		case SSIF_NO_MULTI:
1707 			if (ssif_info->max_xmit_msg_size > 32)
1708 				ssif_info->max_xmit_msg_size = 32;
1709 			if (ssif_info->max_recv_msg_size > 32)
1710 				ssif_info->max_recv_msg_size = 32;
1711 			break;
1712 
1713 		case SSIF_MULTI_2_PART:
1714 			if (ssif_info->max_xmit_msg_size > 63)
1715 				ssif_info->max_xmit_msg_size = 63;
1716 			if (ssif_info->max_recv_msg_size > 62)
1717 				ssif_info->max_recv_msg_size = 62;
1718 			break;
1719 
1720 		case SSIF_MULTI_n_PART:
1721 			/* We take whatever size given, but do some testing. */
1722 			break;
1723 
1724 		default:
1725 			/* Data is not sane, just give up. */
1726 			goto no_support;
1727 		}
1728 	} else {
1729  no_support:
1730 		/* Assume no multi-part or PEC support */
1731 		dev_info(&ssif_info->client->dev,
1732 			 "Error fetching SSIF: %d %d %2.2x, your system probably doesn't support this command so using defaults\n",
1733 			rv, len, resp[2]);
1734 
1735 		ssif_info->max_xmit_msg_size = 32;
1736 		ssif_info->max_recv_msg_size = 32;
1737 		ssif_info->multi_support = SSIF_NO_MULTI;
1738 		ssif_info->supports_pec = 0;
1739 	}
1740 
1741 	test_multipart_messages(client, ssif_info, resp);
1742 
1743 	/* Make sure the NMI timeout is cleared. */
1744 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1745 	msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
1746 	msg[2] = WDT_PRE_TIMEOUT_INT;
1747 	rv = do_cmd(client, 3, msg, &len, resp);
1748 	if (rv || (len < 3) || (resp[2] != 0))
1749 		dev_warn(&ssif_info->client->dev,
1750 			 "Unable to clear message flags: %d %d %2.2x\n",
1751 			 rv, len, resp[2]);
1752 
1753 	/* Attempt to enable the event buffer. */
1754 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1755 	msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
1756 	rv = do_cmd(client, 2, msg, &len, resp);
1757 	if (rv || (len < 4) || (resp[2] != 0)) {
1758 		dev_warn(&ssif_info->client->dev,
1759 			 "Error getting global enables: %d %d %2.2x\n",
1760 			 rv, len, resp[2]);
1761 		rv = 0; /* Not fatal */
1762 		goto found;
1763 	}
1764 
1765 	ssif_info->global_enables = resp[3];
1766 
1767 	if (resp[3] & IPMI_BMC_EVT_MSG_BUFF) {
1768 		ssif_info->has_event_buffer = true;
1769 		/* buffer is already enabled, nothing to do. */
1770 		goto found;
1771 	}
1772 
1773 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1774 	msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
1775 	msg[2] = ssif_info->global_enables | IPMI_BMC_EVT_MSG_BUFF;
1776 	rv = do_cmd(client, 3, msg, &len, resp);
1777 	if (rv || (len < 2)) {
1778 		dev_warn(&ssif_info->client->dev,
1779 			 "Error setting global enables: %d %d %2.2x\n",
1780 			 rv, len, resp[2]);
1781 		rv = 0; /* Not fatal */
1782 		goto found;
1783 	}
1784 
1785 	if (resp[2] == 0) {
1786 		/* A successful return means the event buffer is supported. */
1787 		ssif_info->has_event_buffer = true;
1788 		ssif_info->global_enables |= IPMI_BMC_EVT_MSG_BUFF;
1789 	}
1790 
1791 	/* Some systems don't behave well if you enable alerts. */
1792 	if (alerts_broken)
1793 		goto found;
1794 
1795 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1796 	msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
1797 	msg[2] = ssif_info->global_enables | IPMI_BMC_RCV_MSG_INTR;
1798 	rv = do_cmd(client, 3, msg, &len, resp);
1799 	if (rv || (len < 2)) {
1800 		dev_warn(&ssif_info->client->dev,
1801 			 "Error setting global enables: %d %d %2.2x\n",
1802 			 rv, len, resp[2]);
1803 		rv = 0; /* Not fatal */
1804 		goto found;
1805 	}
1806 
1807 	if (resp[2] == 0) {
1808 		/* A successful return means the alert is supported. */
1809 		ssif_info->supports_alert = true;
1810 		ssif_info->global_enables |= IPMI_BMC_RCV_MSG_INTR;
1811 	}
1812 
1813  found:
1814 	if (ssif_dbg_probe) {
1815 		dev_dbg(&ssif_info->client->dev,
1816 		       "%s: i2c_probe found device at i2c address %x\n",
1817 		       __func__, client->addr);
1818 	}
1819 
1820 	spin_lock_init(&ssif_info->lock);
1821 	ssif_info->ssif_state = SSIF_NORMAL;
1822 	timer_setup(&ssif_info->retry_timer, retry_timeout, 0);
1823 	timer_setup(&ssif_info->watch_timer, watch_timeout, 0);
1824 
1825 	for (i = 0; i < SSIF_NUM_STATS; i++)
1826 		atomic_set(&ssif_info->stats[i], 0);
1827 
1828 	if (ssif_info->supports_pec)
1829 		ssif_info->client->flags |= I2C_CLIENT_PEC;
1830 
1831 	ssif_info->handlers.owner = THIS_MODULE;
1832 	ssif_info->handlers.start_processing = ssif_start_processing;
1833 	ssif_info->handlers.shutdown = shutdown_ssif;
1834 	ssif_info->handlers.get_smi_info = get_smi_info;
1835 	ssif_info->handlers.sender = sender;
1836 	ssif_info->handlers.request_events = request_events;
1837 	ssif_info->handlers.set_need_watch = ssif_set_need_watch;
1838 
1839 	{
1840 		unsigned int thread_num;
1841 
1842 		thread_num = ((i2c_adapter_id(ssif_info->client->adapter)
1843 			       << 8) |
1844 			      ssif_info->client->addr);
1845 		init_completion(&ssif_info->wake_thread);
1846 		ssif_info->thread = kthread_run(ipmi_ssif_thread, ssif_info,
1847 					       "kssif%4.4x", thread_num);
1848 		if (IS_ERR(ssif_info->thread)) {
1849 			rv = PTR_ERR(ssif_info->thread);
1850 			dev_notice(&ssif_info->client->dev,
1851 				   "Could not start kernel thread: error %d\n",
1852 				   rv);
1853 			goto out;
1854 		}
1855 	}
1856 
1857 	dev_set_drvdata(&ssif_info->client->dev, ssif_info);
1858 	rv = device_add_group(&ssif_info->client->dev,
1859 			      &ipmi_ssif_dev_attr_group);
1860 	if (rv) {
1861 		dev_err(&ssif_info->client->dev,
1862 			"Unable to add device attributes: error %d\n",
1863 			rv);
1864 		goto out;
1865 	}
1866 
1867 	rv = ipmi_register_smi(&ssif_info->handlers,
1868 			       ssif_info,
1869 			       &ssif_info->client->dev,
1870 			       slave_addr);
1871 	if (rv) {
1872 		dev_err(&ssif_info->client->dev,
1873 			"Unable to register device: error %d\n", rv);
1874 		goto out_remove_attr;
1875 	}
1876 
1877  out:
1878 	if (rv) {
1879 		if (addr_info)
1880 			addr_info->client = NULL;
1881 
1882 		dev_err(&ssif_info->client->dev,
1883 			"Unable to start IPMI SSIF: %d\n", rv);
1884 		i2c_set_clientdata(client, NULL);
1885 		kfree(ssif_info);
1886 	}
1887 	kfree(resp);
1888 	mutex_unlock(&ssif_infos_mutex);
1889 	return rv;
1890 
1891 out_remove_attr:
1892 	device_remove_group(&ssif_info->client->dev, &ipmi_ssif_dev_attr_group);
1893 	dev_set_drvdata(&ssif_info->client->dev, NULL);
1894 	goto out;
1895 }
1896 
1897 static int new_ssif_client(int addr, char *adapter_name,
1898 			   int debug, int slave_addr,
1899 			   enum ipmi_addr_src addr_src,
1900 			   struct device *dev)
1901 {
1902 	struct ssif_addr_info *addr_info;
1903 	int rv = 0;
1904 
1905 	mutex_lock(&ssif_infos_mutex);
1906 	if (ssif_info_find(addr, adapter_name, false)) {
1907 		rv = -EEXIST;
1908 		goto out_unlock;
1909 	}
1910 
1911 	addr_info = kzalloc(sizeof(*addr_info), GFP_KERNEL);
1912 	if (!addr_info) {
1913 		rv = -ENOMEM;
1914 		goto out_unlock;
1915 	}
1916 
1917 	if (adapter_name) {
1918 		addr_info->adapter_name = kstrdup(adapter_name, GFP_KERNEL);
1919 		if (!addr_info->adapter_name) {
1920 			kfree(addr_info);
1921 			rv = -ENOMEM;
1922 			goto out_unlock;
1923 		}
1924 	}
1925 
1926 	strncpy(addr_info->binfo.type, DEVICE_NAME,
1927 		sizeof(addr_info->binfo.type));
1928 	addr_info->binfo.addr = addr;
1929 	addr_info->binfo.platform_data = addr_info;
1930 	addr_info->debug = debug;
1931 	addr_info->slave_addr = slave_addr;
1932 	addr_info->addr_src = addr_src;
1933 	addr_info->dev = dev;
1934 
1935 	if (dev)
1936 		dev_set_drvdata(dev, addr_info);
1937 
1938 	list_add_tail(&addr_info->link, &ssif_infos);
1939 
1940 	/* Address list will get it */
1941 
1942 out_unlock:
1943 	mutex_unlock(&ssif_infos_mutex);
1944 	return rv;
1945 }
1946 
1947 static void free_ssif_clients(void)
1948 {
1949 	struct ssif_addr_info *info, *tmp;
1950 
1951 	mutex_lock(&ssif_infos_mutex);
1952 	list_for_each_entry_safe(info, tmp, &ssif_infos, link) {
1953 		list_del(&info->link);
1954 		kfree(info->adapter_name);
1955 		kfree(info);
1956 	}
1957 	mutex_unlock(&ssif_infos_mutex);
1958 }
1959 
1960 static unsigned short *ssif_address_list(void)
1961 {
1962 	struct ssif_addr_info *info;
1963 	unsigned int count = 0, i = 0;
1964 	unsigned short *address_list;
1965 
1966 	list_for_each_entry(info, &ssif_infos, link)
1967 		count++;
1968 
1969 	address_list = kcalloc(count + 1, sizeof(*address_list),
1970 			       GFP_KERNEL);
1971 	if (!address_list)
1972 		return NULL;
1973 
1974 	list_for_each_entry(info, &ssif_infos, link) {
1975 		unsigned short addr = info->binfo.addr;
1976 		int j;
1977 
1978 		for (j = 0; j < i; j++) {
1979 			if (address_list[j] == addr)
1980 				/* Found a dup. */
1981 				break;
1982 		}
1983 		if (j == i) /* Didn't find it in the list. */
1984 			address_list[i++] = addr;
1985 	}
1986 	address_list[i] = I2C_CLIENT_END;
1987 
1988 	return address_list;
1989 }
1990 
1991 #ifdef CONFIG_ACPI
1992 static const struct acpi_device_id ssif_acpi_match[] = {
1993 	{ "IPI0001", 0 },
1994 	{ },
1995 };
1996 MODULE_DEVICE_TABLE(acpi, ssif_acpi_match);
1997 #endif
1998 
1999 #ifdef CONFIG_DMI
2000 static int dmi_ipmi_probe(struct platform_device *pdev)
2001 {
2002 	u8 slave_addr = 0;
2003 	u16 i2c_addr;
2004 	int rv;
2005 
2006 	if (!ssif_trydmi)
2007 		return -ENODEV;
2008 
2009 	rv = device_property_read_u16(&pdev->dev, "i2c-addr", &i2c_addr);
2010 	if (rv) {
2011 		dev_warn(&pdev->dev, "No i2c-addr property\n");
2012 		return -ENODEV;
2013 	}
2014 
2015 	rv = device_property_read_u8(&pdev->dev, "slave-addr", &slave_addr);
2016 	if (rv)
2017 		slave_addr = 0x20;
2018 
2019 	return new_ssif_client(i2c_addr, NULL, 0,
2020 			       slave_addr, SI_SMBIOS, &pdev->dev);
2021 }
2022 #else
2023 static int dmi_ipmi_probe(struct platform_device *pdev)
2024 {
2025 	return -ENODEV;
2026 }
2027 #endif
2028 
2029 static const struct i2c_device_id ssif_id[] = {
2030 	{ DEVICE_NAME, 0 },
2031 	{ }
2032 };
2033 MODULE_DEVICE_TABLE(i2c, ssif_id);
2034 
2035 static struct i2c_driver ssif_i2c_driver = {
2036 	.class		= I2C_CLASS_HWMON,
2037 	.driver		= {
2038 		.name			= DEVICE_NAME
2039 	},
2040 	.probe		= ssif_probe,
2041 	.remove		= ssif_remove,
2042 	.alert		= ssif_alert,
2043 	.id_table	= ssif_id,
2044 	.detect		= ssif_detect
2045 };
2046 
2047 static int ssif_platform_probe(struct platform_device *dev)
2048 {
2049 	return dmi_ipmi_probe(dev);
2050 }
2051 
2052 static int ssif_platform_remove(struct platform_device *dev)
2053 {
2054 	struct ssif_addr_info *addr_info = dev_get_drvdata(&dev->dev);
2055 
2056 	if (!addr_info)
2057 		return 0;
2058 
2059 	mutex_lock(&ssif_infos_mutex);
2060 	list_del(&addr_info->link);
2061 	kfree(addr_info);
2062 	mutex_unlock(&ssif_infos_mutex);
2063 	return 0;
2064 }
2065 
2066 static const struct platform_device_id ssif_plat_ids[] = {
2067     { "dmi-ipmi-ssif", 0 },
2068     { }
2069 };
2070 
2071 static struct platform_driver ipmi_driver = {
2072 	.driver = {
2073 		.name = DEVICE_NAME,
2074 	},
2075 	.probe		= ssif_platform_probe,
2076 	.remove		= ssif_platform_remove,
2077 	.id_table       = ssif_plat_ids
2078 };
2079 
2080 static int init_ipmi_ssif(void)
2081 {
2082 	int i;
2083 	int rv;
2084 
2085 	if (initialized)
2086 		return 0;
2087 
2088 	pr_info("IPMI SSIF Interface driver\n");
2089 
2090 	/* build list for i2c from addr list */
2091 	for (i = 0; i < num_addrs; i++) {
2092 		rv = new_ssif_client(addr[i], adapter_name[i],
2093 				     dbg[i], slave_addrs[i],
2094 				     SI_HARDCODED, NULL);
2095 		if (rv)
2096 			pr_err("Couldn't add hardcoded device at addr 0x%x\n",
2097 			       addr[i]);
2098 	}
2099 
2100 	if (ssif_tryacpi)
2101 		ssif_i2c_driver.driver.acpi_match_table	=
2102 			ACPI_PTR(ssif_acpi_match);
2103 
2104 	if (ssif_trydmi) {
2105 		rv = platform_driver_register(&ipmi_driver);
2106 		if (rv)
2107 			pr_err("Unable to register driver: %d\n", rv);
2108 		else
2109 			platform_registered = true;
2110 	}
2111 
2112 	ssif_i2c_driver.address_list = ssif_address_list();
2113 
2114 	rv = i2c_add_driver(&ssif_i2c_driver);
2115 	if (!rv)
2116 		initialized = true;
2117 
2118 	return rv;
2119 }
2120 module_init(init_ipmi_ssif);
2121 
2122 static void cleanup_ipmi_ssif(void)
2123 {
2124 	if (!initialized)
2125 		return;
2126 
2127 	initialized = false;
2128 
2129 	i2c_del_driver(&ssif_i2c_driver);
2130 
2131 	kfree(ssif_i2c_driver.address_list);
2132 
2133 	if (ssif_trydmi && platform_registered)
2134 		platform_driver_unregister(&ipmi_driver);
2135 
2136 	free_ssif_clients();
2137 }
2138 module_exit(cleanup_ipmi_ssif);
2139 
2140 MODULE_ALIAS("platform:dmi-ipmi-ssif");
2141 MODULE_AUTHOR("Todd C Davis <todd.c.davis@intel.com>, Corey Minyard <minyard@acm.org>");
2142 MODULE_DESCRIPTION("IPMI driver for management controllers on a SMBus");
2143 MODULE_LICENSE("GPL");
2144