xref: /freebsd/sys/dev/ipmi/ipmi_kcs.c (revision 1d386b48)
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/conf.h>
34 #include <sys/condvar.h>
35 #include <sys/eventhandler.h>
36 #include <sys/kernel.h>
37 #include <sys/kthread.h>
38 #include <sys/rman.h>
39 #include <sys/selinfo.h>
40 #include <machine/bus.h>
41 
42 #ifdef LOCAL_MODULE
43 #include <ipmi.h>
44 #include <ipmivars.h>
45 #else
46 #include <sys/ipmi.h>
47 #include <dev/ipmi/ipmivars.h>
48 #endif
49 
50 #define	POLLING_DELAY_MIN 4	/* Waits are 2-3 usecs on typical systems */
51 #define	POLLING_DELAY_MAX 256
52 
53 static void	kcs_clear_obf(struct ipmi_softc *, int);
54 static void	kcs_error(struct ipmi_softc *);
55 static int	kcs_wait_for_ibf(struct ipmi_softc *, bool);
56 static int	kcs_wait_for_obf(struct ipmi_softc *, bool);
57 
58 static int
59 kcs_wait(struct ipmi_softc *sc, int value, int mask)
60 {
61 	int status, start = ticks;
62 	int delay_usec = POLLING_DELAY_MIN;
63 
64 	status = INB(sc, KCS_CTL_STS);
65 	while (ticks - start < MAX_TIMEOUT && (status & mask) != value) {
66 		/*
67 		 * The wait delay is increased exponentially to avoid putting
68 		 * significant load on I/O bus.
69 		 */
70 		DELAY(delay_usec);
71 		status = INB(sc, KCS_CTL_STS);
72 		if (delay_usec < POLLING_DELAY_MAX)
73 			delay_usec *= 2;
74 	}
75 	return (status);
76 }
77 
78 static int
79 kcs_wait_for_ibf(struct ipmi_softc *sc, bool level)
80 {
81 
82 	return (kcs_wait(sc, level ? KCS_STATUS_IBF : 0, KCS_STATUS_IBF));
83 }
84 
85 static int
86 kcs_wait_for_obf(struct ipmi_softc *sc, bool level)
87 {
88 
89 	return (kcs_wait(sc, level ? KCS_STATUS_OBF : 0, KCS_STATUS_OBF));
90 }
91 
92 static void
93 kcs_clear_obf(struct ipmi_softc *sc, int status)
94 {
95 
96 	/* Clear OBF */
97 	if (status & KCS_STATUS_OBF) {
98 		INB(sc, KCS_DATA);
99 	}
100 }
101 
102 static void
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
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
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
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
264 kcs_read_byte(struct ipmi_softc *sc, u_char *data)
265 {
266 	int status;
267 
268 	/* Wait for IBF = 0 */
269 	status = kcs_wait_for_ibf(sc, 0);
270 
271 	/* Read State */
272 	if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_READ) {
273 
274 		/* Wait for OBF = 1 */
275 		status = kcs_wait_for_obf(sc, 1);
276 		if ((status & KCS_STATUS_OBF) == 0)
277 			return (0);
278 
279 		/* Read Data_out */
280 		*data = INB(sc, KCS_DATA);
281 
282 		/* Write READ into Data_in */
283 		OUTB(sc, KCS_DATA, KCS_DATA_IN_READ);
284 		return (1);
285 	}
286 
287 	/* Idle State */
288 	if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_IDLE) {
289 
290 		/* Wait for OBF = 1*/
291 		status = kcs_wait_for_obf(sc, 1);
292 		if ((status & KCS_STATUS_OBF) == 0)
293 			return (0);
294 
295 		/* Read Dummy */
296 		INB(sc, KCS_DATA);
297 		return (2);
298 	}
299 
300 	/* Error State */
301 	return (0);
302 }
303 
304 /*
305  * Send a request message and collect the reply.  Returns true if we
306  * succeed.
307  */
308 static int
309 kcs_polled_request(struct ipmi_softc *sc, struct ipmi_request *req)
310 {
311 	u_char *cp, data;
312 	int i, state;
313 
314 	IPMI_IO_LOCK(sc);
315 
316 	/* Send the request. */
317 	if (!kcs_start_write(sc)) {
318 		device_printf(sc->ipmi_dev, "KCS: Failed to start write\n");
319 		goto fail;
320 	}
321 #ifdef KCS_DEBUG
322 	device_printf(sc->ipmi_dev, "KCS: WRITE_START... ok\n");
323 #endif
324 
325 	if (!kcs_write_byte(sc, req->ir_addr)) {
326 		device_printf(sc->ipmi_dev, "KCS: Failed to write address\n");
327 		goto fail;
328 	}
329 #ifdef KCS_DEBUG
330 	device_printf(sc->ipmi_dev, "KCS: Wrote address: %02x\n", req->ir_addr);
331 #endif
332 
333 	if (req->ir_requestlen == 0) {
334 		if (!kcs_write_last_byte(sc, req->ir_command)) {
335 			device_printf(sc->ipmi_dev,
336 			    "KCS: Failed to write command\n");
337 			goto fail;
338 		}
339 #ifdef KCS_DEBUG
340 		device_printf(sc->ipmi_dev, "KCS: Wrote command: %02x\n",
341 		    req->ir_command);
342 #endif
343 	} else {
344 		if (!kcs_write_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 
354 		cp = req->ir_request;
355 		for (i = 0; i < req->ir_requestlen - 1; i++) {
356 			if (!kcs_write_byte(sc, *cp++)) {
357 				device_printf(sc->ipmi_dev,
358 				    "KCS: Failed to write data byte %d\n",
359 				    i + 1);
360 				goto fail;
361 			}
362 #ifdef KCS_DEBUG
363 			device_printf(sc->ipmi_dev, "KCS: Wrote data: %02x\n",
364 			    cp[-1]);
365 #endif
366 		}
367 
368 		if (!kcs_write_last_byte(sc, *cp)) {
369 			device_printf(sc->ipmi_dev,
370 			    "KCS: Failed to write last dta byte\n");
371 			goto fail;
372 		}
373 #ifdef KCS_DEBUG
374 		device_printf(sc->ipmi_dev, "KCS: Wrote last data: %02x\n",
375 		    *cp);
376 #endif
377 	}
378 
379 	/* Read the reply.  First, read the NetFn/LUN. */
380 	if (kcs_read_byte(sc, &data) != 1) {
381 		device_printf(sc->ipmi_dev, "KCS: Failed to read address\n");
382 		goto fail;
383 	}
384 #ifdef KCS_DEBUG
385 	device_printf(sc->ipmi_dev, "KCS: Read address: %02x\n", data);
386 #endif
387 	if (data != IPMI_REPLY_ADDR(req->ir_addr)) {
388 		device_printf(sc->ipmi_dev, "KCS: Reply address mismatch\n");
389 		goto fail;
390 	}
391 
392 	/* Next we read the command. */
393 	if (kcs_read_byte(sc, &data) != 1) {
394 		device_printf(sc->ipmi_dev, "KCS: Failed to read command\n");
395 		goto fail;
396 	}
397 #ifdef KCS_DEBUG
398 	device_printf(sc->ipmi_dev, "KCS: Read command: %02x\n", data);
399 #endif
400 	if (data != req->ir_command) {
401 		device_printf(sc->ipmi_dev, "KCS: Command mismatch\n");
402 		goto fail;
403 	}
404 
405 	/* Next we read the completion code. */
406 	if (kcs_read_byte(sc, &req->ir_compcode) != 1) {
407 		if (bootverbose) {
408 			device_printf(sc->ipmi_dev,
409 			    "KCS: Failed to read completion code\n");
410 		}
411 		goto fail;
412 	}
413 #ifdef KCS_DEBUG
414 	device_printf(sc->ipmi_dev, "KCS: Read completion code: %02x\n",
415 	    req->ir_compcode);
416 #endif
417 
418 	/* Finally, read the reply from the BMC. */
419 	i = 0;
420 	for (;;) {
421 		state = kcs_read_byte(sc, &data);
422 		if (state == 0) {
423 			device_printf(sc->ipmi_dev,
424 			    "KCS: Read failed on byte %d\n", i + 1);
425 			goto fail;
426 		}
427 		if (state == 2)
428 			break;
429 		if (i < req->ir_replybuflen) {
430 			req->ir_reply[i] = data;
431 #ifdef KCS_DEBUG
432 			device_printf(sc->ipmi_dev, "KCS: Read data %02x\n",
433 			    data);
434 		} else {
435 			device_printf(sc->ipmi_dev,
436 			    "KCS: Read short %02x byte %d\n", data, i + 1);
437 #endif
438 		}
439 		i++;
440 	}
441 	IPMI_IO_UNLOCK(sc);
442 	req->ir_replylen = i;
443 #ifdef KCS_DEBUG
444 	device_printf(sc->ipmi_dev, "KCS: READ finished (%d bytes)\n", i);
445 	if (req->ir_replybuflen < i)
446 #else
447 	if (req->ir_replybuflen < i && req->ir_replybuflen != 0)
448 #endif
449 		device_printf(sc->ipmi_dev,
450 		    "KCS: Read short: %zd buffer, %d actual\n",
451 		    req->ir_replybuflen, i);
452 	return (1);
453 fail:
454 	kcs_error(sc);
455 	IPMI_IO_UNLOCK(sc);
456 	return (0);
457 }
458 
459 static void
460 kcs_loop(void *arg)
461 {
462 	struct ipmi_softc *sc = arg;
463 	struct ipmi_request *req;
464 	int i, ok;
465 
466 	IPMI_LOCK(sc);
467 	while ((req = ipmi_dequeue_request(sc)) != NULL) {
468 		IPMI_UNLOCK(sc);
469 		ok = 0;
470 		for (i = 0; i < 3 && !ok; i++)
471 			ok = kcs_polled_request(sc, req);
472 		if (ok)
473 			req->ir_error = 0;
474 		else
475 			req->ir_error = EIO;
476 		IPMI_LOCK(sc);
477 		ipmi_complete_request(sc, req);
478 	}
479 	IPMI_UNLOCK(sc);
480 	kproc_exit(0);
481 }
482 
483 static int
484 kcs_startup(struct ipmi_softc *sc)
485 {
486 
487 	return (kproc_create(kcs_loop, sc, &sc->ipmi_kthread, 0, 0, "%s: kcs",
488 	    device_get_nameunit(sc->ipmi_dev)));
489 }
490 
491 static int
492 kcs_driver_request_queue(struct ipmi_softc *sc, struct ipmi_request *req, int timo)
493 {
494 	int error;
495 
496 	IPMI_LOCK(sc);
497 	ipmi_polled_enqueue_request_highpri(sc, req);
498 	error = msleep(req, &sc->ipmi_requests_lock, 0, "ipmireq", timo);
499 	if (error == 0)
500 		error = req->ir_error;
501 	IPMI_UNLOCK(sc);
502 	return (error);
503 }
504 
505 static int
506 kcs_driver_request_poll(struct ipmi_softc *sc, struct ipmi_request *req)
507 {
508 	int i, ok;
509 
510 	ok = 0;
511 	for (i = 0; i < 3 && !ok; i++)
512 		ok = kcs_polled_request(sc, req);
513 	if (ok)
514 		req->ir_error = 0;
515 	else
516 		req->ir_error = EIO;
517 	return (req->ir_error);
518 }
519 
520 static int
521 kcs_driver_request(struct ipmi_softc *sc, struct ipmi_request *req, int timo)
522 {
523 
524 	if (KERNEL_PANICKED() || dumping)
525 		return (kcs_driver_request_poll(sc, req));
526 	else
527 		return (kcs_driver_request_queue(sc, req, timo));
528 }
529 
530 
531 int
532 ipmi_kcs_attach(struct ipmi_softc *sc)
533 {
534 	int status;
535 
536 	/* Setup function pointers. */
537 	sc->ipmi_startup = kcs_startup;
538 	sc->ipmi_enqueue_request = ipmi_polled_enqueue_request;
539 	sc->ipmi_driver_request = kcs_driver_request;
540 	sc->ipmi_driver_requests_polled = 1;
541 
542 	/* See if we can talk to the controller. */
543 	status = INB(sc, KCS_CTL_STS);
544 	if (status == 0xff) {
545 		device_printf(sc->ipmi_dev, "couldn't find it\n");
546 		return (ENXIO);
547 	}
548 
549 #ifdef KCS_DEBUG
550 	device_printf(sc->ipmi_dev, "KCS: initial state: %02x\n", status);
551 #endif
552 	if (status & KCS_STATUS_OBF ||
553 	    KCS_STATUS_STATE(status) != KCS_STATUS_STATE_IDLE)
554 		kcs_error(sc);
555 
556 	return (0);
557 }
558 
559 /*
560  * Determine the alignment automatically for a PCI attachment.  In this case,
561  * any unused bytes will return 0x00 when read.  We make use of the C/D bit
562  * in the CTL_STS register to try to start a GET_STATUS transaction.  When
563  * we write the command, that bit should be set, so we should get a non-zero
564  * value back when we read CTL_STS if the offset we are testing is the CTL_STS
565  * register.
566  */
567 int
568 ipmi_kcs_probe_align(struct ipmi_softc *sc)
569 {
570 	int status;
571 
572 	sc->ipmi_io_spacing = 1;
573 retry:
574 #ifdef KCS_DEBUG
575 	device_printf(sc->ipmi_dev, "Trying KCS align %d... ", sc->ipmi_io_spacing);
576 #endif
577 
578 	/* Wait for IBF = 0 */
579 	status = INB(sc, KCS_CTL_STS);
580 	while (status & KCS_STATUS_IBF) {
581 		DELAY(100);
582 		status = INB(sc, KCS_CTL_STS);
583 	}
584 
585 	OUTB(sc, KCS_CTL_STS, KCS_CONTROL_GET_STATUS_ABORT);
586 
587 	/* Wait for IBF = 0 */
588 	status = INB(sc, KCS_CTL_STS);
589 	while (status & KCS_STATUS_IBF) {
590 		DELAY(100);
591 		status = INB(sc, KCS_CTL_STS);
592 	}
593 
594 	/* If we got 0x00 back, then this must not be the CTL_STS register. */
595 	if (status == 0) {
596 #ifdef KCS_DEBUG
597 		printf("failed\n");
598 #endif
599 		sc->ipmi_io_spacing <<= 1;
600 		if (sc->ipmi_io_spacing > 4)
601 			return (0);
602 		goto retry;
603 	}
604 #ifdef KCS_DEBUG
605 	printf("ok\n");
606 #endif
607 
608 	/* Finish out the transaction. */
609 
610 	/* Clear OBF */
611 	if (status & KCS_STATUS_OBF)
612 		INB(sc, KCS_DATA);
613 
614 	/* 0x00 to DATA_IN */
615 	OUTB(sc, KCS_DATA, 0);
616 
617 	/* Wait for IBF = 0 */
618 	status = INB(sc, KCS_CTL_STS);
619 	while (status & KCS_STATUS_IBF) {
620 		DELAY(100);
621 		status = INB(sc, KCS_CTL_STS);
622 	}
623 
624 	if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_READ) {
625 		/* Wait for IBF = 1 */
626 		while (!(status & KCS_STATUS_OBF)) {
627 			DELAY(100);
628 			status = INB(sc, KCS_CTL_STS);
629 		}
630 
631 		/* Read error status. */
632 		INB(sc, KCS_DATA);
633 
634 		/* Write dummy READ to DATA_IN. */
635 		OUTB(sc, KCS_DATA, KCS_DATA_IN_READ);
636 
637 		/* Wait for IBF = 0 */
638 		status = INB(sc, KCS_CTL_STS);
639 		while (status & KCS_STATUS_IBF) {
640 			DELAY(100);
641 			status = INB(sc, KCS_CTL_STS);
642 		}
643 	}
644 
645 	if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_IDLE) {
646 		/* Wait for IBF = 1 */
647 		while (!(status & KCS_STATUS_OBF)) {
648 			DELAY(100);
649 			status = INB(sc, KCS_CTL_STS);
650 		}
651 
652 		/* Clear OBF */
653 		if (status & KCS_STATUS_OBF)
654 			INB(sc, KCS_DATA);
655 	} else
656 		device_printf(sc->ipmi_dev, "KCS probe: end state %x\n",
657 		    KCS_STATUS_STATE(status));
658 
659 	return (1);
660 }
661