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