xref: /linux/drivers/char/ipmi/ipmi_ssif.c (revision 4949009eb8d40a441dcddcd96e101e77d31cf1b2)
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 #include <linux/version.h>
35 #if defined(MODVERSIONS)
36 #include <linux/modversions.h>
37 #endif
38 
39 #include <linux/module.h>
40 #include <linux/moduleparam.h>
41 #include <linux/sched.h>
42 #include <linux/seq_file.h>
43 #include <linux/timer.h>
44 #include <linux/delay.h>
45 #include <linux/errno.h>
46 #include <linux/spinlock.h>
47 #include <linux/slab.h>
48 #include <linux/list.h>
49 #include <linux/i2c.h>
50 #include <linux/ipmi_smi.h>
51 #include <linux/init.h>
52 #include <linux/dmi.h>
53 #include <linux/kthread.h>
54 #include <linux/acpi.h>
55 #include <linux/ctype.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 	/* Always add statistics before this value, it must be last. */
170 	SSIF_NUM_STATS
171 };
172 
173 struct ssif_addr_info {
174 	unsigned short addr;
175 	struct i2c_board_info binfo;
176 	char *adapter_name;
177 	int debug;
178 	int slave_addr;
179 	enum ipmi_addr_src addr_src;
180 	union ipmi_smi_info_union addr_info;
181 
182 	struct mutex clients_mutex;
183 	struct list_head clients;
184 
185 	struct list_head link;
186 };
187 
188 struct ssif_info;
189 
190 typedef void (*ssif_i2c_done)(struct ssif_info *ssif_info, int result,
191 			     unsigned char *data, unsigned int len);
192 
193 struct ssif_info {
194 	ipmi_smi_t          intf;
195 	int                 intf_num;
196 	spinlock_t	    lock;
197 	struct ipmi_smi_msg *waiting_msg;
198 	struct ipmi_smi_msg *curr_msg;
199 	enum ssif_intf_state ssif_state;
200 	unsigned long       ssif_debug;
201 
202 	struct ipmi_smi_handlers handlers;
203 
204 	enum ipmi_addr_src addr_source; /* ACPI, PCI, SMBIOS, hardcode, etc. */
205 	union ipmi_smi_info_union addr_info;
206 
207 	/*
208 	 * Flags from the last GET_MSG_FLAGS command, used when an ATTN
209 	 * is set to hold the flags until we are done handling everything
210 	 * from the flags.
211 	 */
212 #define RECEIVE_MSG_AVAIL	0x01
213 #define EVENT_MSG_BUFFER_FULL	0x02
214 #define WDT_PRE_TIMEOUT_INT	0x08
215 	unsigned char       msg_flags;
216 
217 	bool		    has_event_buffer;
218 
219 	/*
220 	 * If set to true, this will request events the next time the
221 	 * state machine is idle.
222 	 */
223 	bool                req_events;
224 
225 	/*
226 	 * If set to true, this will request flags the next time the
227 	 * state machine is idle.
228 	 */
229 	bool                req_flags;
230 
231 	/*
232 	 * Used to perform timer operations when run-to-completion
233 	 * mode is on.  This is a countdown timer.
234 	 */
235 	int                 rtc_us_timer;
236 
237 	/* Used for sending/receiving data.  +1 for the length. */
238 	unsigned char data[IPMI_MAX_MSG_LENGTH + 1];
239 	unsigned int  data_len;
240 
241 	/* Temp receive buffer, gets copied into data. */
242 	unsigned char recv[I2C_SMBUS_BLOCK_MAX];
243 
244 	struct i2c_client *client;
245 	ssif_i2c_done done_handler;
246 
247 	/* Thread interface handling */
248 	struct task_struct *thread;
249 	struct completion wake_thread;
250 	bool stopping;
251 	int i2c_read_write;
252 	int i2c_command;
253 	unsigned char *i2c_data;
254 	unsigned int i2c_size;
255 
256 	/* From the device id response. */
257 	struct ipmi_device_id device_id;
258 
259 	struct timer_list retry_timer;
260 	int retries_left;
261 
262 	/* Info from SSIF cmd */
263 	unsigned char max_xmit_msg_size;
264 	unsigned char max_recv_msg_size;
265 	unsigned int  multi_support;
266 	int           supports_pec;
267 
268 #define SSIF_NO_MULTI		0
269 #define SSIF_MULTI_2_PART	1
270 #define SSIF_MULTI_n_PART	2
271 	unsigned char *multi_data;
272 	unsigned int  multi_len;
273 	unsigned int  multi_pos;
274 
275 	atomic_t stats[SSIF_NUM_STATS];
276 };
277 
278 #define ssif_inc_stat(ssif, stat) \
279 	atomic_inc(&(ssif)->stats[SSIF_STAT_ ## stat])
280 #define ssif_get_stat(ssif, stat) \
281 	((unsigned int) atomic_read(&(ssif)->stats[SSIF_STAT_ ## stat]))
282 
283 static bool initialized;
284 
285 static atomic_t next_intf = ATOMIC_INIT(0);
286 
287 static void return_hosed_msg(struct ssif_info *ssif_info,
288 			     struct ipmi_smi_msg *msg);
289 static void start_next_msg(struct ssif_info *ssif_info, unsigned long *flags);
290 static int start_send(struct ssif_info *ssif_info,
291 		      unsigned char   *data,
292 		      unsigned int    len);
293 
294 static unsigned long *ipmi_ssif_lock_cond(struct ssif_info *ssif_info,
295 					  unsigned long *flags)
296 {
297 	spin_lock_irqsave(&ssif_info->lock, *flags);
298 	return flags;
299 }
300 
301 static void ipmi_ssif_unlock_cond(struct ssif_info *ssif_info,
302 				  unsigned long *flags)
303 {
304 	spin_unlock_irqrestore(&ssif_info->lock, *flags);
305 }
306 
307 static void deliver_recv_msg(struct ssif_info *ssif_info,
308 			     struct ipmi_smi_msg *msg)
309 {
310 	ipmi_smi_t    intf = ssif_info->intf;
311 
312 	if (!intf) {
313 		ipmi_free_smi_msg(msg);
314 	} else if (msg->rsp_size < 0) {
315 		return_hosed_msg(ssif_info, msg);
316 		pr_err(PFX
317 		       "Malformed message in deliver_recv_msg: rsp_size = %d\n",
318 		       msg->rsp_size);
319 	} else {
320 		ipmi_smi_msg_received(intf, msg);
321 	}
322 }
323 
324 static void return_hosed_msg(struct ssif_info *ssif_info,
325 			     struct ipmi_smi_msg *msg)
326 {
327 	ssif_inc_stat(ssif_info, hosed);
328 
329 	/* Make it a response */
330 	msg->rsp[0] = msg->data[0] | 4;
331 	msg->rsp[1] = msg->data[1];
332 	msg->rsp[2] = 0xFF; /* Unknown error. */
333 	msg->rsp_size = 3;
334 
335 	deliver_recv_msg(ssif_info, msg);
336 }
337 
338 /*
339  * Must be called with the message lock held.  This will release the
340  * message lock.  Note that the caller will check SSIF_IDLE and start a
341  * new operation, so there is no need to check for new messages to
342  * start in here.
343  */
344 static void start_clear_flags(struct ssif_info *ssif_info, unsigned long *flags)
345 {
346 	unsigned char msg[3];
347 
348 	ssif_info->msg_flags &= ~WDT_PRE_TIMEOUT_INT;
349 	ssif_info->ssif_state = SSIF_CLEARING_FLAGS;
350 	ipmi_ssif_unlock_cond(ssif_info, flags);
351 
352 	/* Make sure the watchdog pre-timeout flag is not set at startup. */
353 	msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
354 	msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
355 	msg[2] = WDT_PRE_TIMEOUT_INT;
356 
357 	if (start_send(ssif_info, msg, 3) != 0) {
358 		/* Error, just go to normal state. */
359 		ssif_info->ssif_state = SSIF_NORMAL;
360 	}
361 }
362 
363 static void start_flag_fetch(struct ssif_info *ssif_info, unsigned long *flags)
364 {
365 	unsigned char mb[2];
366 
367 	ssif_info->req_flags = false;
368 	ssif_info->ssif_state = SSIF_GETTING_FLAGS;
369 	ipmi_ssif_unlock_cond(ssif_info, flags);
370 
371 	mb[0] = (IPMI_NETFN_APP_REQUEST << 2);
372 	mb[1] = IPMI_GET_MSG_FLAGS_CMD;
373 	if (start_send(ssif_info, mb, 2) != 0)
374 		ssif_info->ssif_state = SSIF_NORMAL;
375 }
376 
377 static void check_start_send(struct ssif_info *ssif_info, unsigned long *flags,
378 			     struct ipmi_smi_msg *msg)
379 {
380 	if (start_send(ssif_info, msg->data, msg->data_size) != 0) {
381 		unsigned long oflags;
382 
383 		flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
384 		ssif_info->curr_msg = NULL;
385 		ssif_info->ssif_state = SSIF_NORMAL;
386 		ipmi_ssif_unlock_cond(ssif_info, flags);
387 		ipmi_free_smi_msg(msg);
388 	}
389 }
390 
391 static void start_event_fetch(struct ssif_info *ssif_info, unsigned long *flags)
392 {
393 	struct ipmi_smi_msg *msg;
394 
395 	ssif_info->req_events = false;
396 
397 	msg = ipmi_alloc_smi_msg();
398 	if (!msg) {
399 		ssif_info->ssif_state = SSIF_NORMAL;
400 		return;
401 	}
402 
403 	ssif_info->curr_msg = msg;
404 	ssif_info->ssif_state = SSIF_GETTING_EVENTS;
405 	ipmi_ssif_unlock_cond(ssif_info, flags);
406 
407 	msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
408 	msg->data[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD;
409 	msg->data_size = 2;
410 
411 	check_start_send(ssif_info, flags, msg);
412 }
413 
414 static void start_recv_msg_fetch(struct ssif_info *ssif_info,
415 				 unsigned long *flags)
416 {
417 	struct ipmi_smi_msg *msg;
418 
419 	msg = ipmi_alloc_smi_msg();
420 	if (!msg) {
421 		ssif_info->ssif_state = SSIF_NORMAL;
422 		return;
423 	}
424 
425 	ssif_info->curr_msg = msg;
426 	ssif_info->ssif_state = SSIF_GETTING_MESSAGES;
427 	ipmi_ssif_unlock_cond(ssif_info, flags);
428 
429 	msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
430 	msg->data[1] = IPMI_GET_MSG_CMD;
431 	msg->data_size = 2;
432 
433 	check_start_send(ssif_info, flags, msg);
434 }
435 
436 /*
437  * Must be called with the message lock held.  This will release the
438  * message lock.  Note that the caller will check SSIF_IDLE and start a
439  * new operation, so there is no need to check for new messages to
440  * start in here.
441  */
442 static void handle_flags(struct ssif_info *ssif_info, unsigned long *flags)
443 {
444 	if (ssif_info->msg_flags & WDT_PRE_TIMEOUT_INT) {
445 		ipmi_smi_t intf = ssif_info->intf;
446 		/* Watchdog pre-timeout */
447 		ssif_inc_stat(ssif_info, watchdog_pretimeouts);
448 		start_clear_flags(ssif_info, flags);
449 		if (intf)
450 			ipmi_smi_watchdog_pretimeout(intf);
451 	} else if (ssif_info->msg_flags & RECEIVE_MSG_AVAIL)
452 		/* Messages available. */
453 		start_recv_msg_fetch(ssif_info, flags);
454 	else if (ssif_info->msg_flags & EVENT_MSG_BUFFER_FULL)
455 		/* Events available. */
456 		start_event_fetch(ssif_info, flags);
457 	else {
458 		ssif_info->ssif_state = SSIF_NORMAL;
459 		ipmi_ssif_unlock_cond(ssif_info, flags);
460 	}
461 }
462 
463 static int ipmi_ssif_thread(void *data)
464 {
465 	struct ssif_info *ssif_info = data;
466 
467 	while (!kthread_should_stop()) {
468 		int result;
469 
470 		/* Wait for something to do */
471 		wait_for_completion(&ssif_info->wake_thread);
472 		init_completion(&ssif_info->wake_thread);
473 
474 		if (ssif_info->stopping)
475 			break;
476 
477 		if (ssif_info->i2c_read_write == I2C_SMBUS_WRITE) {
478 			result = i2c_smbus_write_block_data(
479 				ssif_info->client, SSIF_IPMI_REQUEST,
480 				ssif_info->i2c_data[0],
481 				ssif_info->i2c_data + 1);
482 			ssif_info->done_handler(ssif_info, result, NULL, 0);
483 		} else {
484 			result = i2c_smbus_read_block_data(
485 				ssif_info->client, SSIF_IPMI_RESPONSE,
486 				ssif_info->i2c_data);
487 			if (result < 0)
488 				ssif_info->done_handler(ssif_info, result,
489 							NULL, 0);
490 			else
491 				ssif_info->done_handler(ssif_info, 0,
492 							ssif_info->i2c_data,
493 							result);
494 		}
495 	}
496 
497 	return 0;
498 }
499 
500 static int ssif_i2c_send(struct ssif_info *ssif_info,
501 			ssif_i2c_done handler,
502 			int read_write, int command,
503 			unsigned char *data, unsigned int size)
504 {
505 	ssif_info->done_handler = handler;
506 
507 	ssif_info->i2c_read_write = read_write;
508 	ssif_info->i2c_command = command;
509 	ssif_info->i2c_data = data;
510 	ssif_info->i2c_size = size;
511 	complete(&ssif_info->wake_thread);
512 	return 0;
513 }
514 
515 
516 static void msg_done_handler(struct ssif_info *ssif_info, int result,
517 			     unsigned char *data, unsigned int len);
518 
519 static void retry_timeout(unsigned long data)
520 {
521 	struct ssif_info *ssif_info = (void *) data;
522 	int rv;
523 
524 	if (ssif_info->stopping)
525 		return;
526 
527 	ssif_info->rtc_us_timer = 0;
528 
529 	rv = ssif_i2c_send(ssif_info, msg_done_handler, I2C_SMBUS_READ,
530 			  SSIF_IPMI_RESPONSE,
531 			  ssif_info->recv, I2C_SMBUS_BLOCK_DATA);
532 	if (rv < 0) {
533 		/* request failed, just return the error. */
534 		if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
535 			pr_info("Error from i2c_non_blocking_op(5)\n");
536 
537 		msg_done_handler(ssif_info, -EIO, NULL, 0);
538 	}
539 }
540 
541 static int start_resend(struct ssif_info *ssif_info);
542 
543 static void msg_done_handler(struct ssif_info *ssif_info, int result,
544 			     unsigned char *data, unsigned int len)
545 {
546 	struct ipmi_smi_msg *msg;
547 	unsigned long oflags, *flags;
548 	int rv;
549 
550 	/*
551 	 * We are single-threaded here, so no need for a lock until we
552 	 * start messing with driver states or the queues.
553 	 */
554 
555 	if (result < 0) {
556 		ssif_info->retries_left--;
557 		if (ssif_info->retries_left > 0) {
558 			ssif_inc_stat(ssif_info, receive_retries);
559 
560 			mod_timer(&ssif_info->retry_timer,
561 				  jiffies + SSIF_MSG_JIFFIES);
562 			ssif_info->rtc_us_timer = SSIF_MSG_USEC;
563 			return;
564 		}
565 
566 		ssif_inc_stat(ssif_info, receive_errors);
567 
568 		if  (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
569 			pr_info("Error in msg_done_handler: %d\n", result);
570 		len = 0;
571 		goto continue_op;
572 	}
573 
574 	if ((len > 1) && (ssif_info->multi_pos == 0)
575 				&& (data[0] == 0x00) && (data[1] == 0x01)) {
576 		/* Start of multi-part read.  Start the next transaction. */
577 		int i;
578 
579 		ssif_inc_stat(ssif_info, received_message_parts);
580 
581 		/* Remove the multi-part read marker. */
582 		for (i = 0; i < (len-2); i++)
583 			ssif_info->data[i] = data[i+2];
584 		len -= 2;
585 		ssif_info->multi_len = len;
586 		ssif_info->multi_pos = 1;
587 
588 		rv = ssif_i2c_send(ssif_info, msg_done_handler, I2C_SMBUS_READ,
589 				  SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE,
590 				  ssif_info->recv, I2C_SMBUS_BLOCK_DATA);
591 		if (rv < 0) {
592 			if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
593 				pr_info("Error from i2c_non_blocking_op(1)\n");
594 
595 			result = -EIO;
596 		} else
597 			return;
598 	} else if (ssif_info->multi_pos) {
599 		/* Middle of multi-part read.  Start the next transaction. */
600 		int i;
601 		unsigned char blocknum;
602 
603 		if (len == 0) {
604 			result = -EIO;
605 			if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
606 				pr_info(PFX "Middle message with no data\n");
607 
608 			goto continue_op;
609 		}
610 
611 		blocknum = data[ssif_info->multi_len];
612 
613 		if (ssif_info->multi_len+len-1 > IPMI_MAX_MSG_LENGTH) {
614 			/* Received message too big, abort the operation. */
615 			result = -E2BIG;
616 			if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
617 				pr_info("Received message too big\n");
618 
619 			goto continue_op;
620 		}
621 
622 		/* Remove the blocknum from the data. */
623 		for (i = 0; i < (len-1); i++)
624 			ssif_info->data[i+ssif_info->multi_len] = data[i+1];
625 		len--;
626 		ssif_info->multi_len += len;
627 		if (blocknum == 0xff) {
628 			/* End of read */
629 			len = ssif_info->multi_len;
630 			data = ssif_info->data;
631 		} else if ((blocknum+1) != ssif_info->multi_pos) {
632 			/*
633 			 * Out of sequence block, just abort.  Block
634 			 * numbers start at zero for the second block,
635 			 * but multi_pos starts at one, so the +1.
636 			 */
637 			result = -EIO;
638 		} else {
639 			ssif_inc_stat(ssif_info, received_message_parts);
640 
641 			ssif_info->multi_pos++;
642 
643 			rv = ssif_i2c_send(ssif_info, msg_done_handler,
644 					   I2C_SMBUS_READ,
645 					   SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE,
646 					   ssif_info->recv,
647 					   I2C_SMBUS_BLOCK_DATA);
648 			if (rv < 0) {
649 				if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
650 					pr_info(PFX
651 						"Error from i2c_non_blocking_op(2)\n");
652 
653 				result = -EIO;
654 			} else
655 				return;
656 		}
657 	}
658 
659 	if (result < 0) {
660 		ssif_inc_stat(ssif_info, receive_errors);
661 	} else {
662 		ssif_inc_stat(ssif_info, received_messages);
663 		ssif_inc_stat(ssif_info, received_message_parts);
664 	}
665 
666 
667  continue_op:
668 	if (ssif_info->ssif_debug & SSIF_DEBUG_STATE)
669 		pr_info(PFX "DONE 1: state = %d, result=%d.\n",
670 			ssif_info->ssif_state, result);
671 
672 	flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
673 	msg = ssif_info->curr_msg;
674 	if (msg) {
675 		msg->rsp_size = len;
676 		if (msg->rsp_size > IPMI_MAX_MSG_LENGTH)
677 			msg->rsp_size = IPMI_MAX_MSG_LENGTH;
678 		memcpy(msg->rsp, data, msg->rsp_size);
679 		ssif_info->curr_msg = NULL;
680 	}
681 
682 	switch (ssif_info->ssif_state) {
683 	case SSIF_NORMAL:
684 		ipmi_ssif_unlock_cond(ssif_info, flags);
685 		if (!msg)
686 			break;
687 
688 		if (result < 0)
689 			return_hosed_msg(ssif_info, msg);
690 		else
691 			deliver_recv_msg(ssif_info, msg);
692 		break;
693 
694 	case SSIF_GETTING_FLAGS:
695 		/* We got the flags from the SSIF, now handle them. */
696 		if ((result < 0) || (len < 4) || (data[2] != 0)) {
697 			/*
698 			 * Error fetching flags, or invalid length,
699 			 * just give up for now.
700 			 */
701 			ssif_info->ssif_state = SSIF_NORMAL;
702 			ipmi_ssif_unlock_cond(ssif_info, flags);
703 			pr_warn(PFX "Error getting flags: %d %d, %x\n",
704 			       result, len, data[2]);
705 		} else if (data[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
706 			   || data[1] != IPMI_GET_MSG_FLAGS_CMD) {
707 			pr_warn(PFX "Invalid response getting flags: %x %x\n",
708 				data[0], data[1]);
709 		} else {
710 			ssif_inc_stat(ssif_info, flag_fetches);
711 			ssif_info->msg_flags = data[3];
712 			handle_flags(ssif_info, flags);
713 		}
714 		break;
715 
716 	case SSIF_CLEARING_FLAGS:
717 		/* We cleared the flags. */
718 		if ((result < 0) || (len < 3) || (data[2] != 0)) {
719 			/* Error clearing flags */
720 			pr_warn(PFX "Error clearing flags: %d %d, %x\n",
721 			       result, len, data[2]);
722 		} else if (data[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
723 			   || data[1] != IPMI_CLEAR_MSG_FLAGS_CMD) {
724 			pr_warn(PFX "Invalid response clearing flags: %x %x\n",
725 				data[0], data[1]);
726 		}
727 		ssif_info->ssif_state = SSIF_NORMAL;
728 		ipmi_ssif_unlock_cond(ssif_info, flags);
729 		break;
730 
731 	case SSIF_GETTING_EVENTS:
732 		if ((result < 0) || (len < 3) || (msg->rsp[2] != 0)) {
733 			/* Error getting event, probably done. */
734 			msg->done(msg);
735 
736 			/* Take off the event flag. */
737 			ssif_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
738 			handle_flags(ssif_info, flags);
739 		} else if (msg->rsp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
740 			   || msg->rsp[1] != IPMI_READ_EVENT_MSG_BUFFER_CMD) {
741 			pr_warn(PFX "Invalid response getting events: %x %x\n",
742 				msg->rsp[0], msg->rsp[1]);
743 			msg->done(msg);
744 			/* Take off the event flag. */
745 			ssif_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
746 			handle_flags(ssif_info, flags);
747 		} else {
748 			handle_flags(ssif_info, flags);
749 			ssif_inc_stat(ssif_info, events);
750 			deliver_recv_msg(ssif_info, msg);
751 		}
752 		break;
753 
754 	case SSIF_GETTING_MESSAGES:
755 		if ((result < 0) || (len < 3) || (msg->rsp[2] != 0)) {
756 			/* Error getting event, probably done. */
757 			msg->done(msg);
758 
759 			/* Take off the msg flag. */
760 			ssif_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
761 			handle_flags(ssif_info, flags);
762 		} else if (msg->rsp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
763 			   || msg->rsp[1] != IPMI_GET_MSG_CMD) {
764 			pr_warn(PFX "Invalid response clearing flags: %x %x\n",
765 				msg->rsp[0], msg->rsp[1]);
766 			msg->done(msg);
767 
768 			/* Take off the msg flag. */
769 			ssif_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
770 			handle_flags(ssif_info, flags);
771 		} else {
772 			ssif_inc_stat(ssif_info, incoming_messages);
773 			handle_flags(ssif_info, flags);
774 			deliver_recv_msg(ssif_info, msg);
775 		}
776 		break;
777 	}
778 
779 	flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
780 	if (SSIF_IDLE(ssif_info) && !ssif_info->stopping) {
781 		if (ssif_info->req_events)
782 			start_event_fetch(ssif_info, flags);
783 		else if (ssif_info->req_flags)
784 			start_flag_fetch(ssif_info, flags);
785 		else
786 			start_next_msg(ssif_info, flags);
787 	} else
788 		ipmi_ssif_unlock_cond(ssif_info, flags);
789 
790 	if (ssif_info->ssif_debug & SSIF_DEBUG_STATE)
791 		pr_info(PFX "DONE 2: state = %d.\n", ssif_info->ssif_state);
792 }
793 
794 static void msg_written_handler(struct ssif_info *ssif_info, int result,
795 				unsigned char *data, unsigned int len)
796 {
797 	int rv;
798 
799 	/* We are single-threaded here, so no need for a lock. */
800 	if (result < 0) {
801 		ssif_info->retries_left--;
802 		if (ssif_info->retries_left > 0) {
803 			if (!start_resend(ssif_info)) {
804 				ssif_inc_stat(ssif_info, send_retries);
805 				return;
806 			}
807 			/* request failed, just return the error. */
808 			ssif_inc_stat(ssif_info, send_errors);
809 
810 			if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
811 				pr_info(PFX
812 					"Out of retries in msg_written_handler\n");
813 			msg_done_handler(ssif_info, -EIO, NULL, 0);
814 			return;
815 		}
816 
817 		ssif_inc_stat(ssif_info, send_errors);
818 
819 		/*
820 		 * Got an error on transmit, let the done routine
821 		 * handle it.
822 		 */
823 		if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
824 			pr_info("Error in msg_written_handler: %d\n", result);
825 
826 		msg_done_handler(ssif_info, result, NULL, 0);
827 		return;
828 	}
829 
830 	if (ssif_info->multi_data) {
831 		/* In the middle of a multi-data write. */
832 		int left;
833 
834 		ssif_inc_stat(ssif_info, sent_messages_parts);
835 
836 		left = ssif_info->multi_len - ssif_info->multi_pos;
837 		if (left > 32)
838 			left = 32;
839 		/* Length byte. */
840 		ssif_info->multi_data[ssif_info->multi_pos] = left;
841 		ssif_info->multi_pos += left;
842 		if (left < 32)
843 			/*
844 			 * Write is finished.  Note that we must end
845 			 * with a write of less than 32 bytes to
846 			 * complete the transaction, even if it is
847 			 * zero bytes.
848 			 */
849 			ssif_info->multi_data = NULL;
850 
851 		rv = ssif_i2c_send(ssif_info, msg_written_handler,
852 				  I2C_SMBUS_WRITE,
853 				  SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE,
854 				  ssif_info->multi_data + ssif_info->multi_pos,
855 				  I2C_SMBUS_BLOCK_DATA);
856 		if (rv < 0) {
857 			/* request failed, just return the error. */
858 			ssif_inc_stat(ssif_info, send_errors);
859 
860 			if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
861 				pr_info("Error from i2c_non_blocking_op(3)\n");
862 			msg_done_handler(ssif_info, -EIO, NULL, 0);
863 		}
864 	} else {
865 		ssif_inc_stat(ssif_info, sent_messages);
866 		ssif_inc_stat(ssif_info, sent_messages_parts);
867 
868 		/* Wait a jiffie then request the next message */
869 		ssif_info->retries_left = SSIF_RECV_RETRIES;
870 		ssif_info->rtc_us_timer = SSIF_MSG_PART_USEC;
871 		mod_timer(&ssif_info->retry_timer,
872 			  jiffies + SSIF_MSG_PART_JIFFIES);
873 		return;
874 	}
875 }
876 
877 static int start_resend(struct ssif_info *ssif_info)
878 {
879 	int rv;
880 	int command;
881 
882 	if (ssif_info->data_len > 32) {
883 		command = SSIF_IPMI_MULTI_PART_REQUEST_START;
884 		ssif_info->multi_data = ssif_info->data;
885 		ssif_info->multi_len = ssif_info->data_len;
886 		/*
887 		 * Subtle thing, this is 32, not 33, because we will
888 		 * overwrite the thing at position 32 (which was just
889 		 * transmitted) with the new length.
890 		 */
891 		ssif_info->multi_pos = 32;
892 		ssif_info->data[0] = 32;
893 	} else {
894 		ssif_info->multi_data = NULL;
895 		command = SSIF_IPMI_REQUEST;
896 		ssif_info->data[0] = ssif_info->data_len;
897 	}
898 
899 	rv = ssif_i2c_send(ssif_info, msg_written_handler, I2C_SMBUS_WRITE,
900 			  command, ssif_info->data, I2C_SMBUS_BLOCK_DATA);
901 	if (rv && (ssif_info->ssif_debug & SSIF_DEBUG_MSG))
902 		pr_info("Error from i2c_non_blocking_op(4)\n");
903 	return rv;
904 }
905 
906 static int start_send(struct ssif_info *ssif_info,
907 		      unsigned char   *data,
908 		      unsigned int    len)
909 {
910 	if (len > IPMI_MAX_MSG_LENGTH)
911 		return -E2BIG;
912 	if (len > ssif_info->max_xmit_msg_size)
913 		return -E2BIG;
914 
915 	ssif_info->retries_left = SSIF_SEND_RETRIES;
916 	memcpy(ssif_info->data+1, data, len);
917 	ssif_info->data_len = len;
918 	return start_resend(ssif_info);
919 }
920 
921 /* Must be called with the message lock held. */
922 static void start_next_msg(struct ssif_info *ssif_info, unsigned long *flags)
923 {
924 	struct ipmi_smi_msg *msg;
925 	unsigned long oflags;
926 
927  restart:
928 	if (!SSIF_IDLE(ssif_info)) {
929 		ipmi_ssif_unlock_cond(ssif_info, flags);
930 		return;
931 	}
932 
933 	if (!ssif_info->waiting_msg) {
934 		ssif_info->curr_msg = NULL;
935 		ipmi_ssif_unlock_cond(ssif_info, flags);
936 	} else {
937 		int rv;
938 
939 		ssif_info->curr_msg = ssif_info->waiting_msg;
940 		ssif_info->waiting_msg = NULL;
941 		ipmi_ssif_unlock_cond(ssif_info, flags);
942 		rv = start_send(ssif_info,
943 				ssif_info->curr_msg->data,
944 				ssif_info->curr_msg->data_size);
945 		if (rv) {
946 			msg = ssif_info->curr_msg;
947 			ssif_info->curr_msg = NULL;
948 			return_hosed_msg(ssif_info, msg);
949 			flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
950 			goto restart;
951 		}
952 	}
953 }
954 
955 static void sender(void                *send_info,
956 		   struct ipmi_smi_msg *msg)
957 {
958 	struct ssif_info *ssif_info = (struct ssif_info *) send_info;
959 	unsigned long oflags, *flags;
960 
961 	BUG_ON(ssif_info->waiting_msg);
962 	ssif_info->waiting_msg = msg;
963 
964 	flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
965 	start_next_msg(ssif_info, flags);
966 
967 	if (ssif_info->ssif_debug & SSIF_DEBUG_TIMING) {
968 		struct timeval t;
969 
970 		do_gettimeofday(&t);
971 		pr_info("**Enqueue %02x %02x: %ld.%6.6ld\n",
972 		       msg->data[0], msg->data[1],
973 		       (long) t.tv_sec, (long) t.tv_usec);
974 	}
975 }
976 
977 static int get_smi_info(void *send_info, struct ipmi_smi_info *data)
978 {
979 	struct ssif_info *ssif_info = send_info;
980 
981 	data->addr_src = ssif_info->addr_source;
982 	data->dev = &ssif_info->client->dev;
983 	data->addr_info = ssif_info->addr_info;
984 	get_device(data->dev);
985 
986 	return 0;
987 }
988 
989 /*
990  * Instead of having our own timer to periodically check the message
991  * flags, we let the message handler drive us.
992  */
993 static void request_events(void *send_info)
994 {
995 	struct ssif_info *ssif_info = (struct ssif_info *) send_info;
996 	unsigned long oflags, *flags;
997 
998 	if (!ssif_info->has_event_buffer)
999 		return;
1000 
1001 	flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1002 	/*
1003 	 * Request flags first, not events, because the lower layer
1004 	 * doesn't have a way to send an attention.  But make sure
1005 	 * event checking still happens.
1006 	 */
1007 	ssif_info->req_events = true;
1008 	if (SSIF_IDLE(ssif_info))
1009 		start_flag_fetch(ssif_info, flags);
1010 	else {
1011 		ssif_info->req_flags = true;
1012 		ipmi_ssif_unlock_cond(ssif_info, flags);
1013 	}
1014 }
1015 
1016 static int inc_usecount(void *send_info)
1017 {
1018 	struct ssif_info *ssif_info = send_info;
1019 
1020 	if (!i2c_get_adapter(ssif_info->client->adapter->nr))
1021 		return -ENODEV;
1022 
1023 	i2c_use_client(ssif_info->client);
1024 	return 0;
1025 }
1026 
1027 static void dec_usecount(void *send_info)
1028 {
1029 	struct ssif_info *ssif_info = send_info;
1030 
1031 	i2c_release_client(ssif_info->client);
1032 	i2c_put_adapter(ssif_info->client->adapter);
1033 }
1034 
1035 static int ssif_start_processing(void *send_info,
1036 				 ipmi_smi_t intf)
1037 {
1038 	struct ssif_info *ssif_info = send_info;
1039 
1040 	ssif_info->intf = intf;
1041 
1042 	return 0;
1043 }
1044 
1045 #define MAX_SSIF_BMCS 4
1046 
1047 static unsigned short addr[MAX_SSIF_BMCS];
1048 static int num_addrs;
1049 module_param_array(addr, ushort, &num_addrs, 0);
1050 MODULE_PARM_DESC(addr, "The addresses to scan for IPMI BMCs on the SSIFs.");
1051 
1052 static char *adapter_name[MAX_SSIF_BMCS];
1053 static int num_adapter_names;
1054 module_param_array(adapter_name, charp, &num_adapter_names, 0);
1055 MODULE_PARM_DESC(adapter_name, "The string name of the I2C device that has the BMC.  By default all devices are scanned.");
1056 
1057 static int slave_addrs[MAX_SSIF_BMCS];
1058 static int num_slave_addrs;
1059 module_param_array(slave_addrs, int, &num_slave_addrs, 0);
1060 MODULE_PARM_DESC(slave_addrs,
1061 		 "The default IPMB slave address for the controller.");
1062 
1063 /*
1064  * Bit 0 enables message debugging, bit 1 enables state debugging, and
1065  * bit 2 enables timing debugging.  This is an array indexed by
1066  * interface number"
1067  */
1068 static int dbg[MAX_SSIF_BMCS];
1069 static int num_dbg;
1070 module_param_array(dbg, int, &num_dbg, 0);
1071 MODULE_PARM_DESC(dbg, "Turn on debugging.");
1072 
1073 static bool ssif_dbg_probe;
1074 module_param_named(dbg_probe, ssif_dbg_probe, bool, 0);
1075 MODULE_PARM_DESC(dbg_probe, "Enable debugging of probing of adapters.");
1076 
1077 static int use_thread;
1078 module_param(use_thread, int, 0);
1079 MODULE_PARM_DESC(use_thread, "Use the thread interface.");
1080 
1081 static bool ssif_tryacpi = 1;
1082 module_param_named(tryacpi, ssif_tryacpi, bool, 0);
1083 MODULE_PARM_DESC(tryacpi, "Setting this to zero will disable the default scan of the interfaces identified via ACPI");
1084 
1085 static bool ssif_trydmi = 1;
1086 module_param_named(trydmi, ssif_trydmi, bool, 0);
1087 MODULE_PARM_DESC(trydmi, "Setting this to zero will disable the default scan of the interfaces identified via DMI (SMBIOS)");
1088 
1089 static DEFINE_MUTEX(ssif_infos_mutex);
1090 static LIST_HEAD(ssif_infos);
1091 
1092 static int ssif_remove(struct i2c_client *client)
1093 {
1094 	struct ssif_info *ssif_info = i2c_get_clientdata(client);
1095 	int rv;
1096 
1097 	if (!ssif_info)
1098 		return 0;
1099 
1100 	i2c_set_clientdata(client, NULL);
1101 
1102 	/*
1103 	 * After this point, we won't deliver anything asychronously
1104 	 * to the message handler.  We can unregister ourself.
1105 	 */
1106 	rv = ipmi_unregister_smi(ssif_info->intf);
1107 	if (rv) {
1108 		pr_err(PFX "Unable to unregister device: errno=%d\n", rv);
1109 		return rv;
1110 	}
1111 	ssif_info->intf = NULL;
1112 
1113 	/* make sure the driver is not looking for flags any more. */
1114 	while (ssif_info->ssif_state != SSIF_NORMAL)
1115 		schedule_timeout(1);
1116 
1117 	ssif_info->stopping = true;
1118 	del_timer_sync(&ssif_info->retry_timer);
1119 	if (ssif_info->thread) {
1120 		complete(&ssif_info->wake_thread);
1121 		kthread_stop(ssif_info->thread);
1122 	}
1123 
1124 	/*
1125 	 * No message can be outstanding now, we have removed the
1126 	 * upper layer and it permitted us to do so.
1127 	 */
1128 	kfree(ssif_info);
1129 	return 0;
1130 }
1131 
1132 static int do_cmd(struct i2c_client *client, int len, unsigned char *msg,
1133 		  int *resp_len, unsigned char *resp)
1134 {
1135 	int retry_cnt;
1136 	int ret;
1137 
1138 	retry_cnt = SSIF_SEND_RETRIES;
1139  retry1:
1140 	ret = i2c_smbus_write_block_data(client, SSIF_IPMI_REQUEST, len, msg);
1141 	if (ret) {
1142 		retry_cnt--;
1143 		if (retry_cnt > 0)
1144 			goto retry1;
1145 		return -ENODEV;
1146 	}
1147 
1148 	ret = -ENODEV;
1149 	retry_cnt = SSIF_RECV_RETRIES;
1150 	while (retry_cnt > 0) {
1151 		ret = i2c_smbus_read_block_data(client, SSIF_IPMI_RESPONSE,
1152 						resp);
1153 		if (ret > 0)
1154 			break;
1155 		msleep(SSIF_MSG_MSEC);
1156 		retry_cnt--;
1157 		if (retry_cnt <= 0)
1158 			break;
1159 	}
1160 
1161 	if (ret > 0) {
1162 		/* Validate that the response is correct. */
1163 		if (ret < 3 ||
1164 		    (resp[0] != (msg[0] | (1 << 2))) ||
1165 		    (resp[1] != msg[1]))
1166 			ret = -EINVAL;
1167 		else {
1168 			*resp_len = ret;
1169 			ret = 0;
1170 		}
1171 	}
1172 
1173 	return ret;
1174 }
1175 
1176 static int ssif_detect(struct i2c_client *client, struct i2c_board_info *info)
1177 {
1178 	unsigned char *resp;
1179 	unsigned char msg[3];
1180 	int           rv;
1181 	int           len;
1182 
1183 	resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1184 	if (!resp)
1185 		return -ENOMEM;
1186 
1187 	/* Do a Get Device ID command, since it is required. */
1188 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1189 	msg[1] = IPMI_GET_DEVICE_ID_CMD;
1190 	rv = do_cmd(client, 2, msg, &len, resp);
1191 	if (rv)
1192 		rv = -ENODEV;
1193 	else
1194 		strlcpy(info->type, DEVICE_NAME, I2C_NAME_SIZE);
1195 	kfree(resp);
1196 	return rv;
1197 }
1198 
1199 static int smi_type_proc_show(struct seq_file *m, void *v)
1200 {
1201 	return seq_puts(m, "ssif\n");
1202 }
1203 
1204 static int smi_type_proc_open(struct inode *inode, struct file *file)
1205 {
1206 	return single_open(file, smi_type_proc_show, inode->i_private);
1207 }
1208 
1209 static const struct file_operations smi_type_proc_ops = {
1210 	.open		= smi_type_proc_open,
1211 	.read		= seq_read,
1212 	.llseek		= seq_lseek,
1213 	.release	= single_release,
1214 };
1215 
1216 static int smi_stats_proc_show(struct seq_file *m, void *v)
1217 {
1218 	struct ssif_info *ssif_info = m->private;
1219 
1220 	seq_printf(m, "sent_messages:          %u\n",
1221 		   ssif_get_stat(ssif_info, sent_messages));
1222 	seq_printf(m, "sent_messages_parts:    %u\n",
1223 		   ssif_get_stat(ssif_info, sent_messages_parts));
1224 	seq_printf(m, "send_retries:           %u\n",
1225 		   ssif_get_stat(ssif_info, send_retries));
1226 	seq_printf(m, "send_errors:            %u\n",
1227 		   ssif_get_stat(ssif_info, send_errors));
1228 	seq_printf(m, "received_messages:      %u\n",
1229 		   ssif_get_stat(ssif_info, received_messages));
1230 	seq_printf(m, "received_message_parts: %u\n",
1231 		   ssif_get_stat(ssif_info, received_message_parts));
1232 	seq_printf(m, "receive_retries:        %u\n",
1233 		   ssif_get_stat(ssif_info, receive_retries));
1234 	seq_printf(m, "receive_errors:         %u\n",
1235 		   ssif_get_stat(ssif_info, receive_errors));
1236 	seq_printf(m, "flag_fetches:           %u\n",
1237 		   ssif_get_stat(ssif_info, flag_fetches));
1238 	seq_printf(m, "hosed:                  %u\n",
1239 		   ssif_get_stat(ssif_info, hosed));
1240 	seq_printf(m, "events:                 %u\n",
1241 		   ssif_get_stat(ssif_info, events));
1242 	seq_printf(m, "watchdog_pretimeouts:   %u\n",
1243 		   ssif_get_stat(ssif_info, watchdog_pretimeouts));
1244 	return 0;
1245 }
1246 
1247 static int smi_stats_proc_open(struct inode *inode, struct file *file)
1248 {
1249 	return single_open(file, smi_stats_proc_show, PDE_DATA(inode));
1250 }
1251 
1252 static const struct file_operations smi_stats_proc_ops = {
1253 	.open		= smi_stats_proc_open,
1254 	.read		= seq_read,
1255 	.llseek		= seq_lseek,
1256 	.release	= single_release,
1257 };
1258 
1259 static struct ssif_addr_info *ssif_info_find(unsigned short addr,
1260 					     char *adapter_name,
1261 					     bool match_null_name)
1262 {
1263 	struct ssif_addr_info *info, *found = NULL;
1264 
1265 restart:
1266 	list_for_each_entry(info, &ssif_infos, link) {
1267 		if (info->binfo.addr == addr) {
1268 			if (info->adapter_name || adapter_name) {
1269 				if (!info->adapter_name != !adapter_name) {
1270 					/* One is NULL and one is not */
1271 					continue;
1272 				}
1273 				if (strcmp(info->adapter_name, adapter_name))
1274 					/* Names to not match */
1275 					continue;
1276 			}
1277 			found = info;
1278 			break;
1279 		}
1280 	}
1281 
1282 	if (!found && match_null_name) {
1283 		/* Try to get an exact match first, then try with a NULL name */
1284 		adapter_name = NULL;
1285 		match_null_name = false;
1286 		goto restart;
1287 	}
1288 
1289 	return found;
1290 }
1291 
1292 static bool check_acpi(struct ssif_info *ssif_info, struct device *dev)
1293 {
1294 #ifdef CONFIG_ACPI
1295 	acpi_handle acpi_handle;
1296 
1297 	acpi_handle = ACPI_HANDLE(dev);
1298 	if (acpi_handle) {
1299 		ssif_info->addr_source = SI_ACPI;
1300 		ssif_info->addr_info.acpi_info.acpi_handle = acpi_handle;
1301 		return true;
1302 	}
1303 #endif
1304 	return false;
1305 }
1306 
1307 static int ssif_probe(struct i2c_client *client, const struct i2c_device_id *id)
1308 {
1309 	unsigned char     msg[3];
1310 	unsigned char     *resp;
1311 	struct ssif_info   *ssif_info;
1312 	int               rv = 0;
1313 	int               len;
1314 	int               i;
1315 	u8		  slave_addr = 0;
1316 	struct ssif_addr_info *addr_info = NULL;
1317 
1318 
1319 	resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1320 	if (!resp)
1321 		return -ENOMEM;
1322 
1323 	ssif_info = kzalloc(sizeof(*ssif_info), GFP_KERNEL);
1324 	if (!ssif_info) {
1325 		kfree(resp);
1326 		return -ENOMEM;
1327 	}
1328 
1329 	if (!check_acpi(ssif_info, &client->dev)) {
1330 		addr_info = ssif_info_find(client->addr, client->adapter->name,
1331 					   true);
1332 		if (!addr_info) {
1333 			/* Must have come in through sysfs. */
1334 			ssif_info->addr_source = SI_HOTMOD;
1335 		} else {
1336 			ssif_info->addr_source = addr_info->addr_src;
1337 			ssif_info->ssif_debug = addr_info->debug;
1338 			ssif_info->addr_info = addr_info->addr_info;
1339 			slave_addr = addr_info->slave_addr;
1340 		}
1341 	}
1342 
1343 	pr_info(PFX "Trying %s-specified SSIF interface at i2c address 0x%x, adapter %s, slave address 0x%x\n",
1344 	       ipmi_addr_src_to_str(ssif_info->addr_source),
1345 	       client->addr, client->adapter->name, slave_addr);
1346 
1347 	/*
1348 	 * Do a Get Device ID command, since it comes back with some
1349 	 * useful info.
1350 	 */
1351 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1352 	msg[1] = IPMI_GET_DEVICE_ID_CMD;
1353 	rv = do_cmd(client, 2, msg, &len, resp);
1354 	if (rv)
1355 		goto out;
1356 
1357 	rv = ipmi_demangle_device_id(resp, len, &ssif_info->device_id);
1358 	if (rv)
1359 		goto out;
1360 
1361 	ssif_info->client = client;
1362 	i2c_set_clientdata(client, ssif_info);
1363 
1364 	/* Now check for system interface capabilities */
1365 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1366 	msg[1] = IPMI_GET_SYSTEM_INTERFACE_CAPABILITIES_CMD;
1367 	msg[2] = 0; /* SSIF */
1368 	rv = do_cmd(client, 3, msg, &len, resp);
1369 	if (!rv && (len >= 3) && (resp[2] == 0)) {
1370 		if (len < 7) {
1371 			if (ssif_dbg_probe)
1372 				pr_info(PFX "SSIF info too short: %d\n", len);
1373 			goto no_support;
1374 		}
1375 
1376 		/* Got a good SSIF response, handle it. */
1377 		ssif_info->max_xmit_msg_size = resp[5];
1378 		ssif_info->max_recv_msg_size = resp[6];
1379 		ssif_info->multi_support = (resp[4] >> 6) & 0x3;
1380 		ssif_info->supports_pec = (resp[4] >> 3) & 0x1;
1381 
1382 		/* Sanitize the data */
1383 		switch (ssif_info->multi_support) {
1384 		case SSIF_NO_MULTI:
1385 			if (ssif_info->max_xmit_msg_size > 32)
1386 				ssif_info->max_xmit_msg_size = 32;
1387 			if (ssif_info->max_recv_msg_size > 32)
1388 				ssif_info->max_recv_msg_size = 32;
1389 			break;
1390 
1391 		case SSIF_MULTI_2_PART:
1392 			if (ssif_info->max_xmit_msg_size > 64)
1393 				ssif_info->max_xmit_msg_size = 64;
1394 			if (ssif_info->max_recv_msg_size > 62)
1395 				ssif_info->max_recv_msg_size = 62;
1396 			break;
1397 
1398 		case SSIF_MULTI_n_PART:
1399 			break;
1400 
1401 		default:
1402 			/* Data is not sane, just give up. */
1403 			goto no_support;
1404 		}
1405 	} else {
1406  no_support:
1407 		/* Assume no multi-part or PEC support */
1408 		pr_info(PFX "Error fetching SSIF: %d %d %2.2x, your system probably doesn't support this command so  using defaults\n",
1409 		       rv, len, resp[2]);
1410 
1411 		ssif_info->max_xmit_msg_size = 32;
1412 		ssif_info->max_recv_msg_size = 32;
1413 		ssif_info->multi_support = SSIF_NO_MULTI;
1414 		ssif_info->supports_pec = 0;
1415 	}
1416 
1417 	/* Make sure the NMI timeout is cleared. */
1418 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1419 	msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
1420 	msg[2] = WDT_PRE_TIMEOUT_INT;
1421 	rv = do_cmd(client, 3, msg, &len, resp);
1422 	if (rv || (len < 3) || (resp[2] != 0))
1423 		pr_warn(PFX "Unable to clear message flags: %d %d %2.2x\n",
1424 			rv, len, resp[2]);
1425 
1426 	/* Attempt to enable the event buffer. */
1427 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1428 	msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
1429 	rv = do_cmd(client, 2, msg, &len, resp);
1430 	if (rv || (len < 4) || (resp[2] != 0)) {
1431 		pr_warn(PFX "Error getting global enables: %d %d %2.2x\n",
1432 			rv, len, resp[2]);
1433 		rv = 0; /* Not fatal */
1434 		goto found;
1435 	}
1436 
1437 	if (resp[3] & IPMI_BMC_EVT_MSG_BUFF) {
1438 		ssif_info->has_event_buffer = true;
1439 		/* buffer is already enabled, nothing to do. */
1440 		goto found;
1441 	}
1442 
1443 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1444 	msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
1445 	msg[2] = resp[3] | IPMI_BMC_EVT_MSG_BUFF;
1446 	rv = do_cmd(client, 3, msg, &len, resp);
1447 	if (rv || (len < 2)) {
1448 		pr_warn(PFX "Error getting global enables: %d %d %2.2x\n",
1449 			rv, len, resp[2]);
1450 		rv = 0; /* Not fatal */
1451 		goto found;
1452 	}
1453 
1454 	if (resp[2] == 0)
1455 		/* A successful return means the event buffer is supported. */
1456 		ssif_info->has_event_buffer = true;
1457 
1458  found:
1459 	ssif_info->intf_num = atomic_inc_return(&next_intf);
1460 
1461 	if (ssif_dbg_probe) {
1462 		pr_info("ssif_probe: i2c_probe found device at i2c address %x\n",
1463 			client->addr);
1464 	}
1465 
1466 	spin_lock_init(&ssif_info->lock);
1467 	ssif_info->ssif_state = SSIF_NORMAL;
1468 	init_timer(&ssif_info->retry_timer);
1469 	ssif_info->retry_timer.data = (unsigned long) ssif_info;
1470 	ssif_info->retry_timer.function = retry_timeout;
1471 
1472 	for (i = 0; i < SSIF_NUM_STATS; i++)
1473 		atomic_set(&ssif_info->stats[i], 0);
1474 
1475 	if (ssif_info->supports_pec)
1476 		ssif_info->client->flags |= I2C_CLIENT_PEC;
1477 
1478 	ssif_info->handlers.owner = THIS_MODULE;
1479 	ssif_info->handlers.start_processing = ssif_start_processing;
1480 	ssif_info->handlers.get_smi_info = get_smi_info;
1481 	ssif_info->handlers.sender = sender;
1482 	ssif_info->handlers.request_events = request_events;
1483 	ssif_info->handlers.inc_usecount = inc_usecount;
1484 	ssif_info->handlers.dec_usecount = dec_usecount;
1485 
1486 	{
1487 		unsigned int thread_num;
1488 
1489 		thread_num = ((ssif_info->client->adapter->nr << 8) |
1490 			      ssif_info->client->addr);
1491 		init_completion(&ssif_info->wake_thread);
1492 		ssif_info->thread = kthread_run(ipmi_ssif_thread, ssif_info,
1493 					       "kssif%4.4x", thread_num);
1494 		if (IS_ERR(ssif_info->thread)) {
1495 			rv = PTR_ERR(ssif_info->thread);
1496 			dev_notice(&ssif_info->client->dev,
1497 				   "Could not start kernel thread: error %d\n",
1498 				   rv);
1499 			goto out;
1500 		}
1501 	}
1502 
1503 	rv = ipmi_register_smi(&ssif_info->handlers,
1504 			       ssif_info,
1505 			       &ssif_info->device_id,
1506 			       &ssif_info->client->dev,
1507 			       slave_addr);
1508 	 if (rv) {
1509 		pr_err(PFX "Unable to register device: error %d\n", rv);
1510 		goto out;
1511 	}
1512 
1513 	rv = ipmi_smi_add_proc_entry(ssif_info->intf, "type",
1514 				     &smi_type_proc_ops,
1515 				     ssif_info);
1516 	if (rv) {
1517 		pr_err(PFX "Unable to create proc entry: %d\n", rv);
1518 		goto out_err_unreg;
1519 	}
1520 
1521 	rv = ipmi_smi_add_proc_entry(ssif_info->intf, "ssif_stats",
1522 				     &smi_stats_proc_ops,
1523 				     ssif_info);
1524 	if (rv) {
1525 		pr_err(PFX "Unable to create proc entry: %d\n", rv);
1526 		goto out_err_unreg;
1527 	}
1528 
1529  out:
1530 	if (rv)
1531 		kfree(ssif_info);
1532 	kfree(resp);
1533 	return rv;
1534 
1535  out_err_unreg:
1536 	ipmi_unregister_smi(ssif_info->intf);
1537 	goto out;
1538 }
1539 
1540 static int ssif_adapter_handler(struct device *adev, void *opaque)
1541 {
1542 	struct ssif_addr_info *addr_info = opaque;
1543 
1544 	if (adev->type != &i2c_adapter_type)
1545 		return 0;
1546 
1547 	i2c_new_device(to_i2c_adapter(adev), &addr_info->binfo);
1548 
1549 	if (!addr_info->adapter_name)
1550 		return 1; /* Only try the first I2C adapter by default. */
1551 	return 0;
1552 }
1553 
1554 static int new_ssif_client(int addr, char *adapter_name,
1555 			   int debug, int slave_addr,
1556 			   enum ipmi_addr_src addr_src)
1557 {
1558 	struct ssif_addr_info *addr_info;
1559 	int rv = 0;
1560 
1561 	mutex_lock(&ssif_infos_mutex);
1562 	if (ssif_info_find(addr, adapter_name, false)) {
1563 		rv = -EEXIST;
1564 		goto out_unlock;
1565 	}
1566 
1567 	addr_info = kzalloc(sizeof(*addr_info), GFP_KERNEL);
1568 	if (!addr_info) {
1569 		rv = -ENOMEM;
1570 		goto out_unlock;
1571 	}
1572 
1573 	if (adapter_name) {
1574 		addr_info->adapter_name = kstrdup(adapter_name, GFP_KERNEL);
1575 		if (!addr_info->adapter_name) {
1576 			kfree(addr_info);
1577 			rv = -ENOMEM;
1578 			goto out_unlock;
1579 		}
1580 	}
1581 
1582 	strncpy(addr_info->binfo.type, DEVICE_NAME,
1583 		sizeof(addr_info->binfo.type));
1584 	addr_info->binfo.addr = addr;
1585 	addr_info->binfo.platform_data = addr_info;
1586 	addr_info->debug = debug;
1587 	addr_info->slave_addr = slave_addr;
1588 	addr_info->addr_src = addr_src;
1589 
1590 	list_add_tail(&addr_info->link, &ssif_infos);
1591 
1592 	if (initialized)
1593 		i2c_for_each_dev(addr_info, ssif_adapter_handler);
1594 	/* Otherwise address list will get it */
1595 
1596 out_unlock:
1597 	mutex_unlock(&ssif_infos_mutex);
1598 	return rv;
1599 }
1600 
1601 static void free_ssif_clients(void)
1602 {
1603 	struct ssif_addr_info *info, *tmp;
1604 
1605 	mutex_lock(&ssif_infos_mutex);
1606 	list_for_each_entry_safe(info, tmp, &ssif_infos, link) {
1607 		list_del(&info->link);
1608 		kfree(info->adapter_name);
1609 		kfree(info);
1610 	}
1611 	mutex_unlock(&ssif_infos_mutex);
1612 }
1613 
1614 static unsigned short *ssif_address_list(void)
1615 {
1616 	struct ssif_addr_info *info;
1617 	unsigned int count = 0, i;
1618 	unsigned short *address_list;
1619 
1620 	list_for_each_entry(info, &ssif_infos, link)
1621 		count++;
1622 
1623 	address_list = kzalloc(sizeof(*address_list) * (count + 1), GFP_KERNEL);
1624 	if (!address_list)
1625 		return NULL;
1626 
1627 	i = 0;
1628 	list_for_each_entry(info, &ssif_infos, link) {
1629 		unsigned short addr = info->binfo.addr;
1630 		int j;
1631 
1632 		for (j = 0; j < i; j++) {
1633 			if (address_list[j] == addr)
1634 				goto skip_addr;
1635 		}
1636 		address_list[i] = addr;
1637 skip_addr:
1638 		i++;
1639 	}
1640 	address_list[i] = I2C_CLIENT_END;
1641 
1642 	return address_list;
1643 }
1644 
1645 #ifdef CONFIG_ACPI
1646 static struct acpi_device_id ssif_acpi_match[] = {
1647 	{ "IPI0001", 0 },
1648 	{ },
1649 };
1650 MODULE_DEVICE_TABLE(acpi, ssif_acpi_match);
1651 
1652 /*
1653  * Once we get an ACPI failure, we don't try any more, because we go
1654  * through the tables sequentially.  Once we don't find a table, there
1655  * are no more.
1656  */
1657 static int acpi_failure;
1658 
1659 /*
1660  * Defined in the IPMI 2.0 spec.
1661  */
1662 struct SPMITable {
1663 	s8	Signature[4];
1664 	u32	Length;
1665 	u8	Revision;
1666 	u8	Checksum;
1667 	s8	OEMID[6];
1668 	s8	OEMTableID[8];
1669 	s8	OEMRevision[4];
1670 	s8	CreatorID[4];
1671 	s8	CreatorRevision[4];
1672 	u8	InterfaceType;
1673 	u8	IPMIlegacy;
1674 	s16	SpecificationRevision;
1675 
1676 	/*
1677 	 * Bit 0 - SCI interrupt supported
1678 	 * Bit 1 - I/O APIC/SAPIC
1679 	 */
1680 	u8	InterruptType;
1681 
1682 	/*
1683 	 * If bit 0 of InterruptType is set, then this is the SCI
1684 	 * interrupt in the GPEx_STS register.
1685 	 */
1686 	u8	GPE;
1687 
1688 	s16	Reserved;
1689 
1690 	/*
1691 	 * If bit 1 of InterruptType is set, then this is the I/O
1692 	 * APIC/SAPIC interrupt.
1693 	 */
1694 	u32	GlobalSystemInterrupt;
1695 
1696 	/* The actual register address. */
1697 	struct acpi_generic_address addr;
1698 
1699 	u8	UID[4];
1700 
1701 	s8      spmi_id[1]; /* A '\0' terminated array starts here. */
1702 };
1703 
1704 static int try_init_spmi(struct SPMITable *spmi)
1705 {
1706 	unsigned short myaddr;
1707 
1708 	if (num_addrs >= MAX_SSIF_BMCS)
1709 		return -1;
1710 
1711 	if (spmi->IPMIlegacy != 1) {
1712 		pr_warn("IPMI: Bad SPMI legacy: %d\n", spmi->IPMIlegacy);
1713 		return -ENODEV;
1714 	}
1715 
1716 	if (spmi->InterfaceType != 4)
1717 		return -ENODEV;
1718 
1719 	if (spmi->addr.space_id != ACPI_ADR_SPACE_SMBUS) {
1720 		pr_warn(PFX "Invalid ACPI SSIF I/O Address type: %d\n",
1721 			spmi->addr.space_id);
1722 		return -EIO;
1723 	}
1724 
1725 	myaddr = spmi->addr.address >> 1;
1726 
1727 	return new_ssif_client(myaddr, NULL, 0, 0, SI_SPMI);
1728 }
1729 
1730 static void spmi_find_bmc(void)
1731 {
1732 	acpi_status      status;
1733 	struct SPMITable *spmi;
1734 	int              i;
1735 
1736 	if (acpi_disabled)
1737 		return;
1738 
1739 	if (acpi_failure)
1740 		return;
1741 
1742 	for (i = 0; ; i++) {
1743 		status = acpi_get_table(ACPI_SIG_SPMI, i+1,
1744 					(struct acpi_table_header **)&spmi);
1745 		if (status != AE_OK)
1746 			return;
1747 
1748 		try_init_spmi(spmi);
1749 	}
1750 }
1751 #else
1752 static void spmi_find_bmc(void) { }
1753 #endif
1754 
1755 #ifdef CONFIG_DMI
1756 static int decode_dmi(const struct dmi_device *dmi_dev)
1757 {
1758 	struct dmi_header *dm = dmi_dev->device_data;
1759 	u8             *data = (u8 *) dm;
1760 	u8             len = dm->length;
1761 	unsigned short myaddr;
1762 	int            slave_addr;
1763 
1764 	if (num_addrs >= MAX_SSIF_BMCS)
1765 		return -1;
1766 
1767 	if (len < 9)
1768 		return -1;
1769 
1770 	if (data[0x04] != 4) /* Not SSIF */
1771 		return -1;
1772 
1773 	if ((data[8] >> 1) == 0) {
1774 		/*
1775 		 * Some broken systems put the I2C address in
1776 		 * the slave address field.  We try to
1777 		 * accommodate them here.
1778 		 */
1779 		myaddr = data[6] >> 1;
1780 		slave_addr = 0;
1781 	} else {
1782 		myaddr = data[8] >> 1;
1783 		slave_addr = data[6];
1784 	}
1785 
1786 	return new_ssif_client(myaddr, NULL, 0, 0, SI_SMBIOS);
1787 }
1788 
1789 static void dmi_iterator(void)
1790 {
1791 	const struct dmi_device *dev = NULL;
1792 
1793 	while ((dev = dmi_find_device(DMI_DEV_TYPE_IPMI, NULL, dev)))
1794 		decode_dmi(dev);
1795 }
1796 #else
1797 static void dmi_iterator(void) { }
1798 #endif
1799 
1800 static const struct i2c_device_id ssif_id[] = {
1801 	{ DEVICE_NAME, 0 },
1802 	{ }
1803 };
1804 MODULE_DEVICE_TABLE(i2c, ssif_id);
1805 
1806 static struct i2c_driver ssif_i2c_driver = {
1807 	.class		= I2C_CLASS_HWMON,
1808 	.driver		= {
1809 		.owner			= THIS_MODULE,
1810 		.name			= DEVICE_NAME
1811 	},
1812 	.probe		= ssif_probe,
1813 	.remove		= ssif_remove,
1814 	.id_table	= ssif_id,
1815 	.detect		= ssif_detect
1816 };
1817 
1818 static int init_ipmi_ssif(void)
1819 {
1820 	int i;
1821 	int rv;
1822 
1823 	if (initialized)
1824 		return 0;
1825 
1826 	pr_info("IPMI SSIF Interface driver\n");
1827 
1828 	/* build list for i2c from addr list */
1829 	for (i = 0; i < num_addrs; i++) {
1830 		rv = new_ssif_client(addr[i], adapter_name[i],
1831 				     dbg[i], slave_addrs[i],
1832 				     SI_HARDCODED);
1833 		if (!rv)
1834 			pr_err(PFX
1835 			       "Couldn't add hardcoded device at addr 0x%x\n",
1836 			       addr[i]);
1837 	}
1838 
1839 	if (ssif_tryacpi)
1840 		ssif_i2c_driver.driver.acpi_match_table	=
1841 			ACPI_PTR(ssif_acpi_match);
1842 	if (ssif_trydmi)
1843 		dmi_iterator();
1844 	if (ssif_tryacpi)
1845 		spmi_find_bmc();
1846 
1847 	ssif_i2c_driver.address_list = ssif_address_list();
1848 
1849 	rv = i2c_add_driver(&ssif_i2c_driver);
1850 	if (!rv)
1851 		initialized = true;
1852 
1853 	return rv;
1854 }
1855 module_init(init_ipmi_ssif);
1856 
1857 static void cleanup_ipmi_ssif(void)
1858 {
1859 	if (!initialized)
1860 		return;
1861 
1862 	initialized = false;
1863 
1864 	i2c_del_driver(&ssif_i2c_driver);
1865 
1866 	free_ssif_clients();
1867 }
1868 module_exit(cleanup_ipmi_ssif);
1869 
1870 MODULE_AUTHOR("Todd C Davis <todd.c.davis@intel.com>, Corey Minyard <minyard@acm.org>");
1871 MODULE_DESCRIPTION("IPMI driver for management controllers on a SMBus");
1872 MODULE_LICENSE("GPL");
1873