xref: /dragonfly/sys/dev/misc/ipmi/ipmi_kcs.c (revision 53a374c18f69009d375e6a59247b55bfe03f59e2)
1 /*-
2  * Copyright (c) 2006 IronPort Systems Inc. <ambrisko@ironport.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: head/sys/dev/ipmi/ipmi_kcs.c 248705 2013-03-25 14:30:34Z melifaro $
27  */
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/bus.h>
32 #include <sys/condvar.h>
33 #include <sys/eventhandler.h>
34 #include <sys/kernel.h>
35 #include <sys/kthread.h>
36 #include <sys/rman.h>
37 #include <sys/conf.h>
38 
39 #ifdef LOCAL_MODULE
40 #include <ipmi.h>
41 #include <ipmivars.h>
42 #else
43 #include <sys/ipmi.h>
44 #include <dev/misc/ipmi/ipmivars.h>
45 #endif
46 
47 static void         kcs_clear_obf(struct ipmi_softc *, int);
48 static void         kcs_error(struct ipmi_softc *);
49 static int          kcs_wait_for_ibf(struct ipmi_softc *, int);
50 static int          kcs_wait_for_obf(struct ipmi_softc *, int);
51 
52 static int
kcs_wait_for_ibf(struct ipmi_softc * sc,int state)53 kcs_wait_for_ibf(struct ipmi_softc *sc, int state)
54 {
55           int status, start = ticks;
56 
57           status = INB(sc, KCS_CTL_STS);
58           if (state == 0) {
59                     /* WAIT FOR IBF = 0 */
60                     while (ticks - start < MAX_TIMEOUT && status & KCS_STATUS_IBF) {
61                               DELAY(100);
62                               status = INB(sc, KCS_CTL_STS);
63                     }
64           } else {
65                     /* WAIT FOR IBF = 1 */
66                     while (ticks - start < MAX_TIMEOUT &&
67                         !(status & KCS_STATUS_IBF)) {
68                               DELAY(100);
69                               status = INB(sc, KCS_CTL_STS);
70                     }
71           }
72           return (status);
73 }
74 
75 static int
kcs_wait_for_obf(struct ipmi_softc * sc,int state)76 kcs_wait_for_obf(struct ipmi_softc *sc, int state)
77 {
78           int status, start = ticks;
79 
80           status = INB(sc, KCS_CTL_STS);
81           if (state == 0) {
82                     /* WAIT FOR OBF = 0 */
83                     while (ticks - start < MAX_TIMEOUT && status & KCS_STATUS_OBF) {
84                               DELAY(100);
85                               status = INB(sc, KCS_CTL_STS);
86                     }
87           } else {
88                     /* WAIT FOR OBF = 1 */
89                     while (ticks - start < MAX_TIMEOUT &&
90                         !(status & KCS_STATUS_OBF)) {
91                               DELAY(100);
92                               status = INB(sc, KCS_CTL_STS);
93                     }
94           }
95           return (status);
96 }
97 
98 static void
kcs_clear_obf(struct ipmi_softc * sc,int status)99 kcs_clear_obf(struct ipmi_softc *sc, int status)
100 {
101           int data;
102 
103           /* Clear OBF */
104           if (status & KCS_STATUS_OBF) {
105                     data = INB(sc, KCS_DATA);
106           }
107 }
108 
109 static void
kcs_error(struct ipmi_softc * sc)110 kcs_error(struct ipmi_softc *sc)
111 {
112           int retry, status;
113           u_char data;
114 
115           for (retry = 0; retry < 2; retry++) {
116 
117                     /* Wait for IBF = 0 */
118                     status = kcs_wait_for_ibf(sc, 0);
119 
120                     /* ABORT */
121                     OUTB(sc, KCS_CTL_STS, KCS_CONTROL_GET_STATUS_ABORT);
122 
123                     /* Wait for IBF = 0 */
124                     status = kcs_wait_for_ibf(sc, 0);
125 
126                     /* Clear OBF */
127                     kcs_clear_obf(sc, status);
128 
129                     if (status & KCS_STATUS_OBF) {
130                               data = INB(sc, KCS_DATA);
131                               if (data != 0)
132                                         device_printf(sc->ipmi_dev,
133                                             "KCS Error Data %02x\n", data);
134                     }
135 
136                     /* 0x00 to DATA_IN */
137                     OUTB(sc, KCS_DATA, 0x00);
138 
139                     /* Wait for IBF = 0 */
140                     status = kcs_wait_for_ibf(sc, 0);
141 
142                     if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_READ) {
143 
144                               /* Wait for OBF = 1 */
145                               status = kcs_wait_for_obf(sc, 1);
146 
147                               /* Read error status */
148                               data = INB(sc, KCS_DATA);
149                               if (data != 0)
150                                         device_printf(sc->ipmi_dev, "KCS error: %02x\n",
151                                             data);
152 
153                               /* Write READ into Data_in */
154                               OUTB(sc, KCS_DATA, KCS_DATA_IN_READ);
155 
156                               /* Wait for IBF = 0 */
157                               status = kcs_wait_for_ibf(sc, 0);
158                     }
159 
160                     /* IDLE STATE */
161                     if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_IDLE) {
162                               /* Wait for OBF = 1 */
163                               status = kcs_wait_for_obf(sc, 1);
164 
165                               /* Clear OBF */
166                               kcs_clear_obf(sc, status);
167                               return;
168                     }
169           }
170           device_printf(sc->ipmi_dev, "KCS: Error retry exhausted\n");
171 }
172 
173 /*
174  * Start to write a request.  Waits for IBF to clear and then sends the
175  * WR_START command.
176  */
177 static int
kcs_start_write(struct ipmi_softc * sc)178 kcs_start_write(struct ipmi_softc *sc)
179 {
180           int retry, status;
181 
182           for (retry = 0; retry < 10; retry++) {
183                     /* Wait for IBF = 0 */
184                     status = kcs_wait_for_ibf(sc, 0);
185 
186                     /* Clear OBF */
187                     kcs_clear_obf(sc, status);
188 
189                     /* Write start to command */
190                     OUTB(sc, KCS_CTL_STS, KCS_CONTROL_WRITE_START);
191 
192                     /* Wait for IBF = 0 */
193                     status = kcs_wait_for_ibf(sc, 0);
194                     if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_WRITE)
195                               break;
196                     DELAY(1000000);
197           }
198 
199           if (KCS_STATUS_STATE(status) != KCS_STATUS_STATE_WRITE)
200                     /* error state */
201                     return (0);
202 
203           /* Clear OBF */
204           kcs_clear_obf(sc, status);
205 
206           return (1);
207 }
208 
209 /*
210  * Write a byte of the request message, excluding the last byte of the
211  * message which requires special handling.
212  */
213 static int
kcs_write_byte(struct ipmi_softc * sc,u_char data)214 kcs_write_byte(struct ipmi_softc *sc, u_char data)
215 {
216           int status;
217 
218           /* Data to Data */
219           OUTB(sc, KCS_DATA, data);
220 
221           /* Wait for IBF = 0 */
222           status = kcs_wait_for_ibf(sc, 0);
223 
224           if (KCS_STATUS_STATE(status) != KCS_STATUS_STATE_WRITE)
225                     return (0);
226 
227           /* Clear OBF */
228           kcs_clear_obf(sc, status);
229           return (1);
230 }
231 
232 /*
233  * Write the last byte of a request message.
234  */
235 static int
kcs_write_last_byte(struct ipmi_softc * sc,u_char data)236 kcs_write_last_byte(struct ipmi_softc *sc, u_char data)
237 {
238           int status;
239 
240           /* Write end to command */
241           OUTB(sc, KCS_CTL_STS, KCS_CONTROL_WRITE_END);
242 
243           /* Wait for IBF = 0 */
244           status = kcs_wait_for_ibf(sc, 0);
245 
246           if (KCS_STATUS_STATE(status) != KCS_STATUS_STATE_WRITE)
247                     /* error state */
248                     return (0);
249 
250           /* Clear OBF */
251           kcs_clear_obf(sc, status);
252 
253           /* Send data byte to DATA. */
254           OUTB(sc, KCS_DATA, data);
255           return (1);
256 }
257 
258 /*
259  * Read one byte of the reply message.
260  */
261 static int
kcs_read_byte(struct ipmi_softc * sc,u_char * data)262 kcs_read_byte(struct ipmi_softc *sc, u_char *data)
263 {
264           int status;
265           u_char dummy;
266 
267           /* Wait for IBF = 0 */
268           status = kcs_wait_for_ibf(sc, 0);
269 
270           /* Read State */
271           if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_READ) {
272 
273                     /* Wait for OBF = 1 */
274                     status = kcs_wait_for_obf(sc, 1);
275 
276                     /* Read Data_out */
277                     *data = INB(sc, KCS_DATA);
278 
279                     /* Write READ into Data_in */
280                     OUTB(sc, KCS_DATA, KCS_DATA_IN_READ);
281                     return (1);
282           }
283 
284           /* Idle State */
285           if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_IDLE) {
286 
287                     /* Wait for OBF = 1*/
288                     status = kcs_wait_for_obf(sc, 1);
289 
290                     /* Read Dummy */
291                     dummy = INB(sc, KCS_DATA);
292                     return (2);
293           }
294 
295           /* Error State */
296           return (0);
297 }
298 
299 /*
300  * Send a request message and collect the reply.  Returns true if we
301  * succeed.
302  */
303 static int
kcs_polled_request(struct ipmi_softc * sc,struct ipmi_request * req)304 kcs_polled_request(struct ipmi_softc *sc, struct ipmi_request *req)
305 {
306           u_char *cp, data;
307           int i, state;
308 
309           /* Send the request. */
310           if (!kcs_start_write(sc)) {
311                     device_printf(sc->ipmi_dev, "KCS: Failed to start write\n");
312                     goto fail;
313           }
314 #ifdef KCS_DEBUG
315           device_printf(sc->ipmi_dev, "KCS: WRITE_START... ok\n");
316 #endif
317 
318           if (!kcs_write_byte(sc, req->ir_addr)) {
319                     device_printf(sc->ipmi_dev, "KCS: Failed to write address\n");
320                     goto fail;
321           }
322 #ifdef KCS_DEBUG
323           device_printf(sc->ipmi_dev, "KCS: Wrote address: %02x\n", req->ir_addr);
324 #endif
325 
326           if (req->ir_requestlen == 0) {
327                     if (!kcs_write_last_byte(sc, req->ir_command)) {
328                               device_printf(sc->ipmi_dev,
329                                   "KCS: Failed to write command\n");
330                               goto fail;
331                     }
332 #ifdef KCS_DEBUG
333                     device_printf(sc->ipmi_dev, "KCS: Wrote command: %02x\n",
334                         req->ir_command);
335 #endif
336           } else {
337                     if (!kcs_write_byte(sc, req->ir_command)) {
338                               device_printf(sc->ipmi_dev,
339                                   "KCS: Failed to write command\n");
340                               goto fail;
341                     }
342 #ifdef KCS_DEBUG
343                     device_printf(sc->ipmi_dev, "KCS: Wrote command: %02x\n",
344                         req->ir_command);
345 #endif
346 
347                     cp = req->ir_request;
348                     for (i = 0; i < req->ir_requestlen - 1; i++) {
349                               if (!kcs_write_byte(sc, *cp++)) {
350                                         device_printf(sc->ipmi_dev,
351                                             "KCS: Failed to write data byte %d\n",
352                                             i + 1);
353                                         goto fail;
354                               }
355 #ifdef KCS_DEBUG
356                               device_printf(sc->ipmi_dev, "KCS: Wrote data: %02x\n",
357                                   cp[-1]);
358 #endif
359                     }
360 
361                     if (!kcs_write_last_byte(sc, *cp)) {
362                               device_printf(sc->ipmi_dev,
363                                   "KCS: Failed to write last dta byte\n");
364                               goto fail;
365                     }
366 #ifdef KCS_DEBUG
367                     device_printf(sc->ipmi_dev, "KCS: Wrote last data: %02x\n",
368                         *cp);
369 #endif
370           }
371 
372           /* Read the reply.  First, read the NetFn/LUN. */
373           if (kcs_read_byte(sc, &data) != 1) {
374                     device_printf(sc->ipmi_dev, "KCS: Failed to read address\n");
375                     goto fail;
376           }
377 #ifdef KCS_DEBUG
378           device_printf(sc->ipmi_dev, "KCS: Read address: %02x\n", data);
379 #endif
380           if (data != IPMI_REPLY_ADDR(req->ir_addr)) {
381                     device_printf(sc->ipmi_dev, "KCS: Reply address mismatch\n");
382                     goto fail;
383           }
384 
385           /* Next we read the command. */
386           if (kcs_read_byte(sc, &data) != 1) {
387                     device_printf(sc->ipmi_dev, "KCS: Failed to read command\n");
388                     goto fail;
389           }
390 #ifdef KCS_DEBUG
391           device_printf(sc->ipmi_dev, "KCS: Read command: %02x\n", data);
392 #endif
393           if (data != req->ir_command) {
394                     device_printf(sc->ipmi_dev, "KCS: Command mismatch\n");
395                     goto fail;
396           }
397 
398           /* Next we read the completion code. */
399           if (kcs_read_byte(sc, &req->ir_compcode) != 1) {
400                     device_printf(sc->ipmi_dev,
401                         "KCS: Failed to read completion code\n");
402                     goto fail;
403           }
404 #ifdef KCS_DEBUG
405           device_printf(sc->ipmi_dev, "KCS: Read completion code: %02x\n",
406               req->ir_compcode);
407 #endif
408 
409           /* Finally, read the reply from the BMC. */
410           i = 0;
411           for (;;) {
412                     state = kcs_read_byte(sc, &data);
413                     if (state == 0) {
414                               device_printf(sc->ipmi_dev,
415                                   "KCS: Read failed on byte %d\n", i + 1);
416                               goto fail;
417                     }
418                     if (state == 2)
419                               break;
420                     if (i < req->ir_replybuflen) {
421                               req->ir_reply[i] = data;
422 #ifdef KCS_DEBUG
423                               device_printf(sc->ipmi_dev, "KCS: Read data %02x\n",
424                                   data);
425                     } else {
426                               device_printf(sc->ipmi_dev,
427                                   "KCS: Read short %02x byte %d\n", data, i + 1);
428 #endif
429                     }
430                     i++;
431           }
432           req->ir_replylen = i;
433 #ifdef KCS_DEBUG
434           device_printf(sc->ipmi_dev, "KCS: READ finished (%d bytes)\n", i);
435           if (req->ir_replybuflen < i)
436 #else
437           if (req->ir_replybuflen < i && req->ir_replybuflen != 0)
438 #endif
439                     device_printf(sc->ipmi_dev,
440                         "KCS: Read short: %zd buffer, %d actual\n",
441                         req->ir_replybuflen, i);
442           return (1);
443 fail:
444           kcs_error(sc);
445           return (0);
446 }
447 
448 static void
kcs_loop(void * arg)449 kcs_loop(void *arg)
450 {
451           struct ipmi_softc *sc = arg;
452           struct ipmi_request *req;
453           int i, ok;
454 
455           IPMI_LOCK(sc);
456           while ((req = ipmi_dequeue_request(sc)) != NULL) {
457                     IPMI_UNLOCK(sc);
458                     ok = 0;
459                     for (i = 0; i < 3 && !ok; i++)
460                               ok = kcs_polled_request(sc, req);
461                     if (ok)
462                               req->ir_error = 0;
463                     else
464                               req->ir_error = EIO;
465                     IPMI_LOCK(sc);
466                     ipmi_complete_request(sc, req);
467           }
468           IPMI_UNLOCK(sc);
469           kthread_exit();
470 }
471 
472 static int
kcs_startup(struct ipmi_softc * sc)473 kcs_startup(struct ipmi_softc *sc)
474 {
475 
476           return (kthread_create(kcs_loop, sc, &sc->ipmi_kthread, "%s: kcs",
477               device_get_nameunit(sc->ipmi_dev)));
478 }
479 
480 int
ipmi_kcs_attach(struct ipmi_softc * sc)481 ipmi_kcs_attach(struct ipmi_softc *sc)
482 {
483           int status;
484 
485           /* Setup function pointers. */
486           sc->ipmi_startup = kcs_startup;
487           sc->ipmi_enqueue_request = ipmi_polled_enqueue_request;
488 
489           /* See if we can talk to the controller. */
490           status = INB(sc, KCS_CTL_STS);
491           if (status == 0xff) {
492                     device_printf(sc->ipmi_dev, "couldn't find it\n");
493                     return (ENXIO);
494           }
495 
496 #ifdef KCS_DEBUG
497           device_printf(sc->ipmi_dev, "KCS: initial state: %02x\n", status);
498 #endif
499           if (status & KCS_STATUS_OBF ||
500               KCS_STATUS_STATE(status) != KCS_STATUS_STATE_IDLE)
501                     kcs_error(sc);
502 
503           return (0);
504 }
505 
506 /*
507  * Determine the alignment automatically for a PCI attachment.  In this case,
508  * any unused bytes will return 0x00 when read.  We make use of the C/D bit
509  * in the CTL_STS register to try to start a GET_STATUS transaction.  When
510  * we write the command, that bit should be set, so we should get a non-zero
511  * value back when we read CTL_STS if the offset we are testing is the CTL_STS
512  * register.
513  */
514 int
ipmi_kcs_probe_align(struct ipmi_softc * sc)515 ipmi_kcs_probe_align(struct ipmi_softc *sc)
516 {
517           int data, status;
518 
519           sc->ipmi_io_spacing = 1;
520 retry:
521 #ifdef KCS_DEBUG
522           device_printf(sc->ipmi_dev, "Trying KCS align %d... ", sc->ipmi_io_spacing);
523 #endif
524 
525           /* Wait for IBF = 0 */
526           status = INB(sc, KCS_CTL_STS);
527           while (status & KCS_STATUS_IBF) {
528                     DELAY(100);
529                     status = INB(sc, KCS_CTL_STS);
530           }
531 
532           OUTB(sc, KCS_CTL_STS, KCS_CONTROL_GET_STATUS_ABORT);
533 
534           /* Wait for IBF = 0 */
535           status = INB(sc, KCS_CTL_STS);
536           while (status & KCS_STATUS_IBF) {
537                     DELAY(100);
538                     status = INB(sc, KCS_CTL_STS);
539           }
540 
541           /* If we got 0x00 back, then this must not be the CTL_STS register. */
542           if (status == 0) {
543 #ifdef KCS_DEBUG
544                     kprintf("failed\n");
545 #endif
546                     sc->ipmi_io_spacing <<= 1;
547                     if (sc->ipmi_io_spacing > 4)
548                               return (0);
549                     goto retry;
550           }
551 #ifdef KCS_DEBUG
552           kprintf("ok\n");
553 #endif
554 
555           /* Finish out the transaction. */
556 
557           /* Clear OBF */
558           if (status & KCS_STATUS_OBF)
559                     data = INB(sc, KCS_DATA);
560 
561           /* 0x00 to DATA_IN */
562           OUTB(sc, KCS_DATA, 0);
563 
564           /* Wait for IBF = 0 */
565           status = INB(sc, KCS_CTL_STS);
566           while (status & KCS_STATUS_IBF) {
567                     DELAY(100);
568                     status = INB(sc, KCS_CTL_STS);
569           }
570 
571           if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_READ) {
572                     /* Wait for IBF = 1 */
573                     while (!(status & KCS_STATUS_OBF)) {
574                               DELAY(100);
575                               status = INB(sc, KCS_CTL_STS);
576                     }
577 
578                     /* Read error status. */
579                     data = INB(sc, KCS_DATA);
580 
581                     /* Write dummy READ to DATA_IN. */
582                     OUTB(sc, KCS_DATA, KCS_DATA_IN_READ);
583 
584                     /* Wait for IBF = 0 */
585                     status = INB(sc, KCS_CTL_STS);
586                     while (status & KCS_STATUS_IBF) {
587                               DELAY(100);
588                               status = INB(sc, KCS_CTL_STS);
589                     }
590           }
591 
592           if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_IDLE) {
593                     /* Wait for IBF = 1 */
594                     while (!(status & KCS_STATUS_OBF)) {
595                               DELAY(100);
596                               status = INB(sc, KCS_CTL_STS);
597                     }
598 
599                     /* Clear OBF */
600                     if (status & KCS_STATUS_OBF)
601                               data = INB(sc, KCS_DATA);
602           } else
603                     device_printf(sc->ipmi_dev, "KCS probe: end state %x\n",
604                         KCS_STATUS_STATE(status));
605 
606           return (1);
607 }
608