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