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