xref: /linux/drivers/char/ipmi/ipmi_kcs_sm.c (revision 2624f124b3b5d550ab2fbef7ee3bc0e1fed09722)
1 /*
2  * ipmi_kcs_sm.c
3  *
4  * State machine for handling IPMI KCS interfaces.
5  *
6  * Author: MontaVista Software, Inc.
7  *         Corey Minyard <minyard@mvista.com>
8  *         source@mvista.com
9  *
10  * Copyright 2002 MontaVista Software Inc.
11  *
12  *  This program is free software; you can redistribute it and/or modify it
13  *  under the terms of the GNU General Public License as published by the
14  *  Free Software Foundation; either version 2 of the License, or (at your
15  *  option) any later version.
16  *
17  *
18  *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
19  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
24  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
26  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
27  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  *  You should have received a copy of the GNU General Public License along
30  *  with this program; if not, write to the Free Software Foundation, Inc.,
31  *  675 Mass Ave, Cambridge, MA 02139, USA.
32  */
33 
34 /*
35  * This state machine is taken from the state machine in the IPMI spec,
36  * pretty much verbatim.  If you have questions about the states, see
37  * that document.
38  */
39 
40 #include <linux/kernel.h> /* For printk. */
41 #include <linux/string.h>
42 #include <linux/ipmi_msgdefs.h>		/* for completion codes */
43 #include "ipmi_si_sm.h"
44 
45 /* Set this if you want a printout of why the state machine was hosed
46    when it gets hosed. */
47 #define DEBUG_HOSED_REASON
48 
49 /* Print the state machine state on entry every time. */
50 #undef DEBUG_STATE
51 
52 /* The states the KCS driver may be in. */
53 enum kcs_states {
54 	KCS_IDLE,		/* The KCS interface is currently
55                                    doing nothing. */
56 	KCS_START_OP,		/* We are starting an operation.  The
57 				   data is in the output buffer, but
58 				   nothing has been done to the
59 				   interface yet.  This was added to
60 				   the state machine in the spec to
61 				   wait for the initial IBF. */
62 	KCS_WAIT_WRITE_START,	/* We have written a write cmd to the
63 				   interface. */
64 	KCS_WAIT_WRITE,		/* We are writing bytes to the
65                                    interface. */
66 	KCS_WAIT_WRITE_END,	/* We have written the write end cmd
67                                    to the interface, and still need to
68                                    write the last byte. */
69 	KCS_WAIT_READ,		/* We are waiting to read data from
70 				   the interface. */
71 	KCS_ERROR0,		/* State to transition to the error
72 				   handler, this was added to the
73 				   state machine in the spec to be
74 				   sure IBF was there. */
75 	KCS_ERROR1,		/* First stage error handler, wait for
76                                    the interface to respond. */
77 	KCS_ERROR2,		/* The abort cmd has been written,
78 				   wait for the interface to
79 				   respond. */
80 	KCS_ERROR3,		/* We wrote some data to the
81 				   interface, wait for it to switch to
82 				   read mode. */
83 	KCS_HOSED		/* The hardware failed to follow the
84 				   state machine. */
85 };
86 
87 #define MAX_KCS_READ_SIZE 80
88 #define MAX_KCS_WRITE_SIZE 80
89 
90 /* Timeouts in microseconds. */
91 #define IBF_RETRY_TIMEOUT 1000000
92 #define OBF_RETRY_TIMEOUT 1000000
93 #define MAX_ERROR_RETRIES 10
94 
95 struct si_sm_data
96 {
97 	enum kcs_states  state;
98 	struct si_sm_io *io;
99 	unsigned char    write_data[MAX_KCS_WRITE_SIZE];
100 	int              write_pos;
101 	int              write_count;
102 	int              orig_write_count;
103 	unsigned char    read_data[MAX_KCS_READ_SIZE];
104 	int              read_pos;
105 	int	         truncated;
106 
107 	unsigned int  error_retries;
108 	long          ibf_timeout;
109 	long          obf_timeout;
110 };
111 
112 static unsigned int init_kcs_data(struct si_sm_data *kcs,
113 				  struct si_sm_io *io)
114 {
115 	kcs->state = KCS_IDLE;
116 	kcs->io = io;
117 	kcs->write_pos = 0;
118 	kcs->write_count = 0;
119 	kcs->orig_write_count = 0;
120 	kcs->read_pos = 0;
121 	kcs->error_retries = 0;
122 	kcs->truncated = 0;
123 	kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
124 	kcs->obf_timeout = OBF_RETRY_TIMEOUT;
125 
126 	/* Reserve 2 I/O bytes. */
127 	return 2;
128 }
129 
130 static inline unsigned char read_status(struct si_sm_data *kcs)
131 {
132 	return kcs->io->inputb(kcs->io, 1);
133 }
134 
135 static inline unsigned char read_data(struct si_sm_data *kcs)
136 {
137 	return kcs->io->inputb(kcs->io, 0);
138 }
139 
140 static inline void write_cmd(struct si_sm_data *kcs, unsigned char data)
141 {
142 	kcs->io->outputb(kcs->io, 1, data);
143 }
144 
145 static inline void write_data(struct si_sm_data *kcs, unsigned char data)
146 {
147 	kcs->io->outputb(kcs->io, 0, data);
148 }
149 
150 /* Control codes. */
151 #define KCS_GET_STATUS_ABORT	0x60
152 #define KCS_WRITE_START		0x61
153 #define KCS_WRITE_END		0x62
154 #define KCS_READ_BYTE		0x68
155 
156 /* Status bits. */
157 #define GET_STATUS_STATE(status) (((status) >> 6) & 0x03)
158 #define KCS_IDLE_STATE	0
159 #define KCS_READ_STATE	1
160 #define KCS_WRITE_STATE	2
161 #define KCS_ERROR_STATE	3
162 #define GET_STATUS_ATN(status) ((status) & 0x04)
163 #define GET_STATUS_IBF(status) ((status) & 0x02)
164 #define GET_STATUS_OBF(status) ((status) & 0x01)
165 
166 
167 static inline void write_next_byte(struct si_sm_data *kcs)
168 {
169 	write_data(kcs, kcs->write_data[kcs->write_pos]);
170 	(kcs->write_pos)++;
171 	(kcs->write_count)--;
172 }
173 
174 static inline void start_error_recovery(struct si_sm_data *kcs, char *reason)
175 {
176 	(kcs->error_retries)++;
177 	if (kcs->error_retries > MAX_ERROR_RETRIES) {
178 #ifdef DEBUG_HOSED_REASON
179 		printk("ipmi_kcs_sm: kcs hosed: %s\n", reason);
180 #endif
181 		kcs->state = KCS_HOSED;
182 	} else {
183 		kcs->state = KCS_ERROR0;
184 	}
185 }
186 
187 static inline void read_next_byte(struct si_sm_data *kcs)
188 {
189 	if (kcs->read_pos >= MAX_KCS_READ_SIZE) {
190 		/* Throw the data away and mark it truncated. */
191 		read_data(kcs);
192 		kcs->truncated = 1;
193 	} else {
194 		kcs->read_data[kcs->read_pos] = read_data(kcs);
195 		(kcs->read_pos)++;
196 	}
197 	write_data(kcs, KCS_READ_BYTE);
198 }
199 
200 static inline int check_ibf(struct si_sm_data *kcs, unsigned char status,
201 			    long time)
202 {
203 	if (GET_STATUS_IBF(status)) {
204 		kcs->ibf_timeout -= time;
205 		if (kcs->ibf_timeout < 0) {
206 			start_error_recovery(kcs, "IBF not ready in time");
207 			kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
208 			return 1;
209 		}
210 		return 0;
211 	}
212 	kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
213 	return 1;
214 }
215 
216 static inline int check_obf(struct si_sm_data *kcs, unsigned char status,
217 			    long time)
218 {
219 	if (! GET_STATUS_OBF(status)) {
220 		kcs->obf_timeout -= time;
221 		if (kcs->obf_timeout < 0) {
222 		    start_error_recovery(kcs, "OBF not ready in time");
223 		    return 1;
224 		}
225 		return 0;
226 	}
227 	kcs->obf_timeout = OBF_RETRY_TIMEOUT;
228 	return 1;
229 }
230 
231 static void clear_obf(struct si_sm_data *kcs, unsigned char status)
232 {
233 	if (GET_STATUS_OBF(status))
234 		read_data(kcs);
235 }
236 
237 static void restart_kcs_transaction(struct si_sm_data *kcs)
238 {
239 	kcs->write_count = kcs->orig_write_count;
240 	kcs->write_pos = 0;
241 	kcs->read_pos = 0;
242 	kcs->state = KCS_WAIT_WRITE_START;
243 	kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
244 	kcs->obf_timeout = OBF_RETRY_TIMEOUT;
245 	write_cmd(kcs, KCS_WRITE_START);
246 }
247 
248 static int start_kcs_transaction(struct si_sm_data *kcs, unsigned char *data,
249 				 unsigned int size)
250 {
251 	if ((size < 2) || (size > MAX_KCS_WRITE_SIZE)) {
252 		return -1;
253 	}
254 
255 	if ((kcs->state != KCS_IDLE) && (kcs->state != KCS_HOSED)) {
256 		return -2;
257 	}
258 
259 	kcs->error_retries = 0;
260 	memcpy(kcs->write_data, data, size);
261 	kcs->write_count = size;
262 	kcs->orig_write_count = size;
263 	kcs->write_pos = 0;
264 	kcs->read_pos = 0;
265 	kcs->state = KCS_START_OP;
266 	kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
267 	kcs->obf_timeout = OBF_RETRY_TIMEOUT;
268 	return 0;
269 }
270 
271 static int get_kcs_result(struct si_sm_data *kcs, unsigned char *data,
272 			  unsigned int length)
273 {
274 	if (length < kcs->read_pos) {
275 		kcs->read_pos = length;
276 		kcs->truncated = 1;
277 	}
278 
279 	memcpy(data, kcs->read_data, kcs->read_pos);
280 
281 	if ((length >= 3) && (kcs->read_pos < 3)) {
282 		/* Guarantee that we return at least 3 bytes, with an
283 		   error in the third byte if it is too short. */
284 		data[2] = IPMI_ERR_UNSPECIFIED;
285 		kcs->read_pos = 3;
286 	}
287 	if (kcs->truncated) {
288 		/* Report a truncated error.  We might overwrite
289 		   another error, but that's too bad, the user needs
290 		   to know it was truncated. */
291 		data[2] = IPMI_ERR_MSG_TRUNCATED;
292 		kcs->truncated = 0;
293 	}
294 
295 	return kcs->read_pos;
296 }
297 
298 /* This implements the state machine defined in the IPMI manual, see
299    that for details on how this works.  Divide that flowchart into
300    sections delimited by "Wait for IBF" and this will become clear. */
301 static enum si_sm_result kcs_event(struct si_sm_data *kcs, long time)
302 {
303 	unsigned char status;
304 	unsigned char state;
305 
306 	status = read_status(kcs);
307 
308 #ifdef DEBUG_STATE
309 	printk("  State = %d, %x\n", kcs->state, status);
310 #endif
311 	/* All states wait for ibf, so just do it here. */
312 	if (!check_ibf(kcs, status, time))
313 		return SI_SM_CALL_WITH_DELAY;
314 
315 	/* Just about everything looks at the KCS state, so grab that, too. */
316 	state = GET_STATUS_STATE(status);
317 
318 	switch (kcs->state) {
319 	case KCS_IDLE:
320 		/* If there's and interrupt source, turn it off. */
321 		clear_obf(kcs, status);
322 
323 		if (GET_STATUS_ATN(status))
324 			return SI_SM_ATTN;
325 		else
326 			return SI_SM_IDLE;
327 
328 	case KCS_START_OP:
329 		if (state != KCS_IDLE) {
330 			start_error_recovery(kcs,
331 					     "State machine not idle at start");
332 			break;
333 		}
334 
335 		clear_obf(kcs, status);
336 		write_cmd(kcs, KCS_WRITE_START);
337 		kcs->state = KCS_WAIT_WRITE_START;
338 		break;
339 
340 	case KCS_WAIT_WRITE_START:
341 		if (state != KCS_WRITE_STATE) {
342 			start_error_recovery(
343 				kcs,
344 				"Not in write state at write start");
345 			break;
346 		}
347 		read_data(kcs);
348 		if (kcs->write_count == 1) {
349 			write_cmd(kcs, KCS_WRITE_END);
350 			kcs->state = KCS_WAIT_WRITE_END;
351 		} else {
352 			write_next_byte(kcs);
353 			kcs->state = KCS_WAIT_WRITE;
354 		}
355 		break;
356 
357 	case KCS_WAIT_WRITE:
358 		if (state != KCS_WRITE_STATE) {
359 			start_error_recovery(kcs,
360 					     "Not in write state for write");
361 			break;
362 		}
363 		clear_obf(kcs, status);
364 		if (kcs->write_count == 1) {
365 			write_cmd(kcs, KCS_WRITE_END);
366 			kcs->state = KCS_WAIT_WRITE_END;
367 		} else {
368 			write_next_byte(kcs);
369 		}
370 		break;
371 
372 	case KCS_WAIT_WRITE_END:
373 		if (state != KCS_WRITE_STATE) {
374 			start_error_recovery(kcs,
375 					     "Not in write state for write end");
376 			break;
377 		}
378 		clear_obf(kcs, status);
379 		write_next_byte(kcs);
380 		kcs->state = KCS_WAIT_READ;
381 		break;
382 
383 	case KCS_WAIT_READ:
384 		if ((state != KCS_READ_STATE) && (state != KCS_IDLE_STATE)) {
385 			start_error_recovery(
386 				kcs,
387 				"Not in read or idle in read state");
388 			break;
389 		}
390 
391 		if (state == KCS_READ_STATE) {
392 			if (! check_obf(kcs, status, time))
393 				return SI_SM_CALL_WITH_DELAY;
394 			read_next_byte(kcs);
395 		} else {
396 			/* We don't implement this exactly like the state
397 			   machine in the spec.  Some broken hardware
398 			   does not write the final dummy byte to the
399 			   read register.  Thus obf will never go high
400 			   here.  We just go straight to idle, and we
401 			   handle clearing out obf in idle state if it
402 			   happens to come in. */
403 			clear_obf(kcs, status);
404 			kcs->orig_write_count = 0;
405 			kcs->state = KCS_IDLE;
406 			return SI_SM_TRANSACTION_COMPLETE;
407 		}
408 		break;
409 
410 	case KCS_ERROR0:
411 		clear_obf(kcs, status);
412 		write_cmd(kcs, KCS_GET_STATUS_ABORT);
413 		kcs->state = KCS_ERROR1;
414 		break;
415 
416 	case KCS_ERROR1:
417 		clear_obf(kcs, status);
418 		write_data(kcs, 0);
419 		kcs->state = KCS_ERROR2;
420 		break;
421 
422 	case KCS_ERROR2:
423 		if (state != KCS_READ_STATE) {
424 			start_error_recovery(kcs,
425 					     "Not in read state for error2");
426 			break;
427 		}
428 		if (! check_obf(kcs, status, time))
429 			return SI_SM_CALL_WITH_DELAY;
430 
431 		clear_obf(kcs, status);
432 		write_data(kcs, KCS_READ_BYTE);
433 		kcs->state = KCS_ERROR3;
434 		break;
435 
436 	case KCS_ERROR3:
437 		if (state != KCS_IDLE_STATE) {
438 			start_error_recovery(kcs,
439 					     "Not in idle state for error3");
440 			break;
441 		}
442 
443 		if (! check_obf(kcs, status, time))
444 			return SI_SM_CALL_WITH_DELAY;
445 
446 		clear_obf(kcs, status);
447 		if (kcs->orig_write_count) {
448 			restart_kcs_transaction(kcs);
449 		} else {
450 			kcs->state = KCS_IDLE;
451 			return SI_SM_TRANSACTION_COMPLETE;
452 		}
453 		break;
454 
455 	case KCS_HOSED:
456 		break;
457 	}
458 
459 	if (kcs->state == KCS_HOSED) {
460 		init_kcs_data(kcs, kcs->io);
461 		return SI_SM_HOSED;
462 	}
463 
464 	return SI_SM_CALL_WITHOUT_DELAY;
465 }
466 
467 static int kcs_size(void)
468 {
469 	return sizeof(struct si_sm_data);
470 }
471 
472 static int kcs_detect(struct si_sm_data *kcs)
473 {
474 	/* It's impossible for the KCS status register to be all 1's,
475 	   (assuming a properly functioning, self-initialized BMC)
476 	   but that's what you get from reading a bogus address, so we
477 	   test that first. */
478 	if (read_status(kcs) == 0xff)
479 		return 1;
480 
481 	return 0;
482 }
483 
484 static void kcs_cleanup(struct si_sm_data *kcs)
485 {
486 }
487 
488 struct si_sm_handlers kcs_smi_handlers =
489 {
490 	.init_data         = init_kcs_data,
491 	.start_transaction = start_kcs_transaction,
492 	.get_result        = get_kcs_result,
493 	.event             = kcs_event,
494 	.detect            = kcs_detect,
495 	.cleanup           = kcs_cleanup,
496 	.size              = kcs_size,
497 };
498