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