diff options
author | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 18:20:36 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 18:20:36 -0400 |
commit | 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch) | |
tree | 0bba044c4ce775e45a88a51686b5d9f90697ea9d /drivers/char/ipmi/ipmi_kcs_sm.c |
Linux-2.6.12-rc2
Initial git repository build. I'm not bothering with the full history,
even though we have it. We can create a separate "historical" git
archive of that later if we want to, and in the meantime it's about
3.2GB when imported into git - space that would just make the early
git days unnecessarily complicated, when we don't have a lot of good
infrastructure for it.
Let it rip!
Diffstat (limited to 'drivers/char/ipmi/ipmi_kcs_sm.c')
-rw-r--r-- | drivers/char/ipmi/ipmi_kcs_sm.c | 500 |
1 files changed, 500 insertions, 0 deletions
diff --git a/drivers/char/ipmi/ipmi_kcs_sm.c b/drivers/char/ipmi/ipmi_kcs_sm.c new file mode 100644 index 00000000000..48cce24329b --- /dev/null +++ b/drivers/char/ipmi/ipmi_kcs_sm.c | |||
@@ -0,0 +1,500 @@ | |||
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 | #define IPMI_KCS_VERSION "v33" | ||
46 | |||
47 | /* Set this if you want a printout of why the state machine was hosed | ||
48 | when it gets hosed. */ | ||
49 | #define DEBUG_HOSED_REASON | ||
50 | |||
51 | /* Print the state machine state on entry every time. */ | ||
52 | #undef DEBUG_STATE | ||
53 | |||
54 | /* The states the KCS driver may be in. */ | ||
55 | enum kcs_states { | ||
56 | KCS_IDLE, /* The KCS interface is currently | ||
57 | doing nothing. */ | ||
58 | KCS_START_OP, /* We are starting an operation. The | ||
59 | data is in the output buffer, but | ||
60 | nothing has been done to the | ||
61 | interface yet. This was added to | ||
62 | the state machine in the spec to | ||
63 | wait for the initial IBF. */ | ||
64 | KCS_WAIT_WRITE_START, /* We have written a write cmd to the | ||
65 | interface. */ | ||
66 | KCS_WAIT_WRITE, /* We are writing bytes to the | ||
67 | interface. */ | ||
68 | KCS_WAIT_WRITE_END, /* We have written the write end cmd | ||
69 | to the interface, and still need to | ||
70 | write the last byte. */ | ||
71 | KCS_WAIT_READ, /* We are waiting to read data from | ||
72 | the interface. */ | ||
73 | KCS_ERROR0, /* State to transition to the error | ||
74 | handler, this was added to the | ||
75 | state machine in the spec to be | ||
76 | sure IBF was there. */ | ||
77 | KCS_ERROR1, /* First stage error handler, wait for | ||
78 | the interface to respond. */ | ||
79 | KCS_ERROR2, /* The abort cmd has been written, | ||
80 | wait for the interface to | ||
81 | respond. */ | ||
82 | KCS_ERROR3, /* We wrote some data to the | ||
83 | interface, wait for it to switch to | ||
84 | read mode. */ | ||
85 | KCS_HOSED /* The hardware failed to follow the | ||
86 | state machine. */ | ||
87 | }; | ||
88 | |||
89 | #define MAX_KCS_READ_SIZE 80 | ||
90 | #define MAX_KCS_WRITE_SIZE 80 | ||
91 | |||
92 | /* Timeouts in microseconds. */ | ||
93 | #define IBF_RETRY_TIMEOUT 1000000 | ||
94 | #define OBF_RETRY_TIMEOUT 1000000 | ||
95 | #define MAX_ERROR_RETRIES 10 | ||
96 | |||
97 | struct si_sm_data | ||
98 | { | ||
99 | enum kcs_states state; | ||
100 | struct si_sm_io *io; | ||
101 | unsigned char write_data[MAX_KCS_WRITE_SIZE]; | ||
102 | int write_pos; | ||
103 | int write_count; | ||
104 | int orig_write_count; | ||
105 | unsigned char read_data[MAX_KCS_READ_SIZE]; | ||
106 | int read_pos; | ||
107 | int truncated; | ||
108 | |||
109 | unsigned int error_retries; | ||
110 | long ibf_timeout; | ||
111 | long obf_timeout; | ||
112 | }; | ||
113 | |||
114 | static unsigned int init_kcs_data(struct si_sm_data *kcs, | ||
115 | struct si_sm_io *io) | ||
116 | { | ||
117 | kcs->state = KCS_IDLE; | ||
118 | kcs->io = io; | ||
119 | kcs->write_pos = 0; | ||
120 | kcs->write_count = 0; | ||
121 | kcs->orig_write_count = 0; | ||
122 | kcs->read_pos = 0; | ||
123 | kcs->error_retries = 0; | ||
124 | kcs->truncated = 0; | ||
125 | kcs->ibf_timeout = IBF_RETRY_TIMEOUT; | ||
126 | kcs->obf_timeout = OBF_RETRY_TIMEOUT; | ||
127 | |||
128 | /* Reserve 2 I/O bytes. */ | ||
129 | return 2; | ||
130 | } | ||
131 | |||
132 | static inline unsigned char read_status(struct si_sm_data *kcs) | ||
133 | { | ||
134 | return kcs->io->inputb(kcs->io, 1); | ||
135 | } | ||
136 | |||
137 | static inline unsigned char read_data(struct si_sm_data *kcs) | ||
138 | { | ||
139 | return kcs->io->inputb(kcs->io, 0); | ||
140 | } | ||
141 | |||
142 | static inline void write_cmd(struct si_sm_data *kcs, unsigned char data) | ||
143 | { | ||
144 | kcs->io->outputb(kcs->io, 1, data); | ||
145 | } | ||
146 | |||
147 | static inline void write_data(struct si_sm_data *kcs, unsigned char data) | ||
148 | { | ||
149 | kcs->io->outputb(kcs->io, 0, data); | ||
150 | } | ||
151 | |||
152 | /* Control codes. */ | ||
153 | #define KCS_GET_STATUS_ABORT 0x60 | ||
154 | #define KCS_WRITE_START 0x61 | ||
155 | #define KCS_WRITE_END 0x62 | ||
156 | #define KCS_READ_BYTE 0x68 | ||
157 | |||
158 | /* Status bits. */ | ||
159 | #define GET_STATUS_STATE(status) (((status) >> 6) & 0x03) | ||
160 | #define KCS_IDLE_STATE 0 | ||
161 | #define KCS_READ_STATE 1 | ||
162 | #define KCS_WRITE_STATE 2 | ||
163 | #define KCS_ERROR_STATE 3 | ||
164 | #define GET_STATUS_ATN(status) ((status) & 0x04) | ||
165 | #define GET_STATUS_IBF(status) ((status) & 0x02) | ||
166 | #define GET_STATUS_OBF(status) ((status) & 0x01) | ||
167 | |||
168 | |||
169 | static inline void write_next_byte(struct si_sm_data *kcs) | ||
170 | { | ||
171 | write_data(kcs, kcs->write_data[kcs->write_pos]); | ||
172 | (kcs->write_pos)++; | ||
173 | (kcs->write_count)--; | ||
174 | } | ||
175 | |||
176 | static inline void start_error_recovery(struct si_sm_data *kcs, char *reason) | ||
177 | { | ||
178 | (kcs->error_retries)++; | ||
179 | if (kcs->error_retries > MAX_ERROR_RETRIES) { | ||
180 | #ifdef DEBUG_HOSED_REASON | ||
181 | printk("ipmi_kcs_sm: kcs hosed: %s\n", reason); | ||
182 | #endif | ||
183 | kcs->state = KCS_HOSED; | ||
184 | } else { | ||
185 | kcs->state = KCS_ERROR0; | ||
186 | } | ||
187 | } | ||
188 | |||
189 | static inline void read_next_byte(struct si_sm_data *kcs) | ||
190 | { | ||
191 | if (kcs->read_pos >= MAX_KCS_READ_SIZE) { | ||
192 | /* Throw the data away and mark it truncated. */ | ||
193 | read_data(kcs); | ||
194 | kcs->truncated = 1; | ||
195 | } else { | ||
196 | kcs->read_data[kcs->read_pos] = read_data(kcs); | ||
197 | (kcs->read_pos)++; | ||
198 | } | ||
199 | write_data(kcs, KCS_READ_BYTE); | ||
200 | } | ||
201 | |||
202 | static inline int check_ibf(struct si_sm_data *kcs, unsigned char status, | ||
203 | long time) | ||
204 | { | ||
205 | if (GET_STATUS_IBF(status)) { | ||
206 | kcs->ibf_timeout -= time; | ||
207 | if (kcs->ibf_timeout < 0) { | ||
208 | start_error_recovery(kcs, "IBF not ready in time"); | ||
209 | kcs->ibf_timeout = IBF_RETRY_TIMEOUT; | ||
210 | return 1; | ||
211 | } | ||
212 | return 0; | ||
213 | } | ||
214 | kcs->ibf_timeout = IBF_RETRY_TIMEOUT; | ||
215 | return 1; | ||
216 | } | ||
217 | |||
218 | static inline int check_obf(struct si_sm_data *kcs, unsigned char status, | ||
219 | long time) | ||
220 | { | ||
221 | if (! GET_STATUS_OBF(status)) { | ||
222 | kcs->obf_timeout -= time; | ||
223 | if (kcs->obf_timeout < 0) { | ||
224 | start_error_recovery(kcs, "OBF not ready in time"); | ||
225 | return 1; | ||
226 | } | ||
227 | return 0; | ||
228 | } | ||
229 | kcs->obf_timeout = OBF_RETRY_TIMEOUT; | ||
230 | return 1; | ||
231 | } | ||
232 | |||
233 | static void clear_obf(struct si_sm_data *kcs, unsigned char status) | ||
234 | { | ||
235 | if (GET_STATUS_OBF(status)) | ||
236 | read_data(kcs); | ||
237 | } | ||
238 | |||
239 | static void restart_kcs_transaction(struct si_sm_data *kcs) | ||
240 | { | ||
241 | kcs->write_count = kcs->orig_write_count; | ||
242 | kcs->write_pos = 0; | ||
243 | kcs->read_pos = 0; | ||
244 | kcs->state = KCS_WAIT_WRITE_START; | ||
245 | kcs->ibf_timeout = IBF_RETRY_TIMEOUT; | ||
246 | kcs->obf_timeout = OBF_RETRY_TIMEOUT; | ||
247 | write_cmd(kcs, KCS_WRITE_START); | ||
248 | } | ||
249 | |||
250 | static int start_kcs_transaction(struct si_sm_data *kcs, unsigned char *data, | ||
251 | unsigned int size) | ||
252 | { | ||
253 | if ((size < 2) || (size > MAX_KCS_WRITE_SIZE)) { | ||
254 | return -1; | ||
255 | } | ||
256 | |||
257 | if ((kcs->state != KCS_IDLE) && (kcs->state != KCS_HOSED)) { | ||
258 | return -2; | ||
259 | } | ||
260 | |||
261 | kcs->error_retries = 0; | ||
262 | memcpy(kcs->write_data, data, size); | ||
263 | kcs->write_count = size; | ||
264 | kcs->orig_write_count = size; | ||
265 | kcs->write_pos = 0; | ||
266 | kcs->read_pos = 0; | ||
267 | kcs->state = KCS_START_OP; | ||
268 | kcs->ibf_timeout = IBF_RETRY_TIMEOUT; | ||
269 | kcs->obf_timeout = OBF_RETRY_TIMEOUT; | ||
270 | return 0; | ||
271 | } | ||
272 | |||
273 | static int get_kcs_result(struct si_sm_data *kcs, unsigned char *data, | ||
274 | unsigned int length) | ||
275 | { | ||
276 | if (length < kcs->read_pos) { | ||
277 | kcs->read_pos = length; | ||
278 | kcs->truncated = 1; | ||
279 | } | ||
280 | |||
281 | memcpy(data, kcs->read_data, kcs->read_pos); | ||
282 | |||
283 | if ((length >= 3) && (kcs->read_pos < 3)) { | ||
284 | /* Guarantee that we return at least 3 bytes, with an | ||
285 | error in the third byte if it is too short. */ | ||
286 | data[2] = IPMI_ERR_UNSPECIFIED; | ||
287 | kcs->read_pos = 3; | ||
288 | } | ||
289 | if (kcs->truncated) { | ||
290 | /* Report a truncated error. We might overwrite | ||
291 | another error, but that's too bad, the user needs | ||
292 | to know it was truncated. */ | ||
293 | data[2] = IPMI_ERR_MSG_TRUNCATED; | ||
294 | kcs->truncated = 0; | ||
295 | } | ||
296 | |||
297 | return kcs->read_pos; | ||
298 | } | ||
299 | |||
300 | /* This implements the state machine defined in the IPMI manual, see | ||
301 | that for details on how this works. Divide that flowchart into | ||
302 | sections delimited by "Wait for IBF" and this will become clear. */ | ||
303 | static enum si_sm_result kcs_event(struct si_sm_data *kcs, long time) | ||
304 | { | ||
305 | unsigned char status; | ||
306 | unsigned char state; | ||
307 | |||
308 | status = read_status(kcs); | ||
309 | |||
310 | #ifdef DEBUG_STATE | ||
311 | printk(" State = %d, %x\n", kcs->state, status); | ||
312 | #endif | ||
313 | /* All states wait for ibf, so just do it here. */ | ||
314 | if (!check_ibf(kcs, status, time)) | ||
315 | return SI_SM_CALL_WITH_DELAY; | ||
316 | |||
317 | /* Just about everything looks at the KCS state, so grab that, too. */ | ||
318 | state = GET_STATUS_STATE(status); | ||
319 | |||
320 | switch (kcs->state) { | ||
321 | case KCS_IDLE: | ||
322 | /* If there's and interrupt source, turn it off. */ | ||
323 | clear_obf(kcs, status); | ||
324 | |||
325 | if (GET_STATUS_ATN(status)) | ||
326 | return SI_SM_ATTN; | ||
327 | else | ||
328 | return SI_SM_IDLE; | ||
329 | |||
330 | case KCS_START_OP: | ||
331 | if (state != KCS_IDLE) { | ||
332 | start_error_recovery(kcs, | ||
333 | "State machine not idle at start"); | ||
334 | break; | ||
335 | } | ||
336 | |||
337 | clear_obf(kcs, status); | ||
338 | write_cmd(kcs, KCS_WRITE_START); | ||
339 | kcs->state = KCS_WAIT_WRITE_START; | ||
340 | break; | ||
341 | |||
342 | case KCS_WAIT_WRITE_START: | ||
343 | if (state != KCS_WRITE_STATE) { | ||
344 | start_error_recovery( | ||
345 | kcs, | ||
346 | "Not in write state at write start"); | ||
347 | break; | ||
348 | } | ||
349 | read_data(kcs); | ||
350 | if (kcs->write_count == 1) { | ||
351 | write_cmd(kcs, KCS_WRITE_END); | ||
352 | kcs->state = KCS_WAIT_WRITE_END; | ||
353 | } else { | ||
354 | write_next_byte(kcs); | ||
355 | kcs->state = KCS_WAIT_WRITE; | ||
356 | } | ||
357 | break; | ||
358 | |||
359 | case KCS_WAIT_WRITE: | ||
360 | if (state != KCS_WRITE_STATE) { | ||
361 | start_error_recovery(kcs, | ||
362 | "Not in write state for write"); | ||
363 | break; | ||
364 | } | ||
365 | clear_obf(kcs, status); | ||
366 | if (kcs->write_count == 1) { | ||
367 | write_cmd(kcs, KCS_WRITE_END); | ||
368 | kcs->state = KCS_WAIT_WRITE_END; | ||
369 | } else { | ||
370 | write_next_byte(kcs); | ||
371 | } | ||
372 | break; | ||
373 | |||
374 | case KCS_WAIT_WRITE_END: | ||
375 | if (state != KCS_WRITE_STATE) { | ||
376 | start_error_recovery(kcs, | ||
377 | "Not in write state for write end"); | ||
378 | break; | ||
379 | } | ||
380 | clear_obf(kcs, status); | ||
381 | write_next_byte(kcs); | ||
382 | kcs->state = KCS_WAIT_READ; | ||
383 | break; | ||
384 | |||
385 | case KCS_WAIT_READ: | ||
386 | if ((state != KCS_READ_STATE) && (state != KCS_IDLE_STATE)) { | ||
387 | start_error_recovery( | ||
388 | kcs, | ||
389 | "Not in read or idle in read state"); | ||
390 | break; | ||
391 | } | ||
392 | |||
393 | if (state == KCS_READ_STATE) { | ||
394 | if (! check_obf(kcs, status, time)) | ||
395 | return SI_SM_CALL_WITH_DELAY; | ||
396 | read_next_byte(kcs); | ||
397 | } else { | ||
398 | /* We don't implement this exactly like the state | ||
399 | machine in the spec. Some broken hardware | ||
400 | does not write the final dummy byte to the | ||
401 | read register. Thus obf will never go high | ||
402 | here. We just go straight to idle, and we | ||
403 | handle clearing out obf in idle state if it | ||
404 | happens to come in. */ | ||
405 | clear_obf(kcs, status); | ||
406 | kcs->orig_write_count = 0; | ||
407 | kcs->state = KCS_IDLE; | ||
408 | return SI_SM_TRANSACTION_COMPLETE; | ||
409 | } | ||
410 | break; | ||
411 | |||
412 | case KCS_ERROR0: | ||
413 | clear_obf(kcs, status); | ||
414 | write_cmd(kcs, KCS_GET_STATUS_ABORT); | ||
415 | kcs->state = KCS_ERROR1; | ||
416 | break; | ||
417 | |||
418 | case KCS_ERROR1: | ||
419 | clear_obf(kcs, status); | ||
420 | write_data(kcs, 0); | ||
421 | kcs->state = KCS_ERROR2; | ||
422 | break; | ||
423 | |||
424 | case KCS_ERROR2: | ||
425 | if (state != KCS_READ_STATE) { | ||
426 | start_error_recovery(kcs, | ||
427 | "Not in read state for error2"); | ||
428 | break; | ||
429 | } | ||
430 | if (! check_obf(kcs, status, time)) | ||
431 | return SI_SM_CALL_WITH_DELAY; | ||
432 | |||
433 | clear_obf(kcs, status); | ||
434 | write_data(kcs, KCS_READ_BYTE); | ||
435 | kcs->state = KCS_ERROR3; | ||
436 | break; | ||
437 | |||
438 | case KCS_ERROR3: | ||
439 | if (state != KCS_IDLE_STATE) { | ||
440 | start_error_recovery(kcs, | ||
441 | "Not in idle state for error3"); | ||
442 | break; | ||
443 | } | ||
444 | |||
445 | if (! check_obf(kcs, status, time)) | ||
446 | return SI_SM_CALL_WITH_DELAY; | ||
447 | |||
448 | clear_obf(kcs, status); | ||
449 | if (kcs->orig_write_count) { | ||
450 | restart_kcs_transaction(kcs); | ||
451 | } else { | ||
452 | kcs->state = KCS_IDLE; | ||
453 | return SI_SM_TRANSACTION_COMPLETE; | ||
454 | } | ||
455 | break; | ||
456 | |||
457 | case KCS_HOSED: | ||
458 | break; | ||
459 | } | ||
460 | |||
461 | if (kcs->state == KCS_HOSED) { | ||
462 | init_kcs_data(kcs, kcs->io); | ||
463 | return SI_SM_HOSED; | ||
464 | } | ||
465 | |||
466 | return SI_SM_CALL_WITHOUT_DELAY; | ||
467 | } | ||
468 | |||
469 | static int kcs_size(void) | ||
470 | { | ||
471 | return sizeof(struct si_sm_data); | ||
472 | } | ||
473 | |||
474 | static int kcs_detect(struct si_sm_data *kcs) | ||
475 | { | ||
476 | /* It's impossible for the KCS status register to be all 1's, | ||
477 | (assuming a properly functioning, self-initialized BMC) | ||
478 | but that's what you get from reading a bogus address, so we | ||
479 | test that first. */ | ||
480 | if (read_status(kcs) == 0xff) | ||
481 | return 1; | ||
482 | |||
483 | return 0; | ||
484 | } | ||
485 | |||
486 | static void kcs_cleanup(struct si_sm_data *kcs) | ||
487 | { | ||
488 | } | ||
489 | |||
490 | struct si_sm_handlers kcs_smi_handlers = | ||
491 | { | ||
492 | .version = IPMI_KCS_VERSION, | ||
493 | .init_data = init_kcs_data, | ||
494 | .start_transaction = start_kcs_transaction, | ||
495 | .get_result = get_kcs_result, | ||
496 | .event = kcs_event, | ||
497 | .detect = kcs_detect, | ||
498 | .cleanup = kcs_cleanup, | ||
499 | .size = kcs_size, | ||
500 | }; | ||