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