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