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