ipmi_kcs.c revision 248705
1226031Sstas/*-
2226031Sstas * Copyright (c) 2006 IronPort Systems Inc. <ambrisko@ironport.com>
3226031Sstas * All rights reserved.
4226031Sstas *
5226031Sstas * Redistribution and use in source and binary forms, with or without
6226031Sstas * modification, are permitted provided that the following conditions
7226031Sstas * are met:
8226031Sstas * 1. Redistributions of source code must retain the above copyright
9226031Sstas *    notice, this list of conditions and the following disclaimer.
10226031Sstas * 2. Redistributions in binary form must reproduce the above copyright
11226031Sstas *    notice, this list of conditions and the following disclaimer in the
12226031Sstas *    documentation and/or other materials provided with the distribution.
13226031Sstas *
14226031Sstas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15226031Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16226031Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17226031Sstas * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18226031Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19226031Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20226031Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21226031Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22226031Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23226031Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24226031Sstas * SUCH DAMAGE.
25226031Sstas */
26226031Sstas
27226031Sstas#include <sys/cdefs.h>
28226031Sstas__FBSDID("$FreeBSD: head/sys/dev/ipmi/ipmi_kcs.c 248705 2013-03-25 14:30:34Z melifaro $");
29226031Sstas
30226031Sstas#include <sys/param.h>
31226031Sstas#include <sys/systm.h>
32226031Sstas#include <sys/bus.h>
33226031Sstas#include <sys/condvar.h>
34226031Sstas#include <sys/eventhandler.h>
35226031Sstas#include <sys/kernel.h>
36226031Sstas#include <sys/kthread.h>
37226031Sstas#include <sys/rman.h>
38226031Sstas#include <sys/selinfo.h>
39226031Sstas#include <machine/bus.h>
40226031Sstas
41226031Sstas#ifdef LOCAL_MODULE
42226031Sstas#include <ipmi.h>
43226031Sstas#include <ipmivars.h>
44226031Sstas#else
45226031Sstas#include <sys/ipmi.h>
46226031Sstas#include <dev/ipmi/ipmivars.h>
47226031Sstas#endif
48226031Sstas
49226031Sstasstatic void	kcs_clear_obf(struct ipmi_softc *, int);
50226031Sstasstatic void	kcs_error(struct ipmi_softc *);
51226031Sstasstatic int	kcs_wait_for_ibf(struct ipmi_softc *, int);
52226031Sstasstatic int	kcs_wait_for_obf(struct ipmi_softc *, int);
53226031Sstas
54226031Sstasstatic int
55226031Sstaskcs_wait_for_ibf(struct ipmi_softc *sc, int state)
56226031Sstas{
57226031Sstas	int status, start = ticks;
58226031Sstas
59226031Sstas	status = INB(sc, KCS_CTL_STS);
60226031Sstas	if (state == 0) {
61226031Sstas		/* WAIT FOR IBF = 0 */
62226031Sstas		while (ticks - start < MAX_TIMEOUT && status & KCS_STATUS_IBF) {
63226031Sstas			DELAY(100);
64226031Sstas			status = INB(sc, KCS_CTL_STS);
65226031Sstas		}
66226031Sstas	} else {
67226031Sstas		/* WAIT FOR IBF = 1 */
68226031Sstas		while (ticks - start < MAX_TIMEOUT &&
69226031Sstas		    !(status & KCS_STATUS_IBF)) {
70226031Sstas			DELAY(100);
71226031Sstas			status = INB(sc, KCS_CTL_STS);
72226031Sstas		}
73226031Sstas	}
74226031Sstas	return (status);
75226031Sstas}
76226031Sstas
77226031Sstasstatic int
78226031Sstaskcs_wait_for_obf(struct ipmi_softc *sc, int state)
79226031Sstas{
80226031Sstas	int status, start = ticks;
81226031Sstas
82226031Sstas	status = INB(sc, KCS_CTL_STS);
83226031Sstas	if (state == 0) {
84226031Sstas		/* WAIT FOR OBF = 0 */
85226031Sstas		while (ticks - start < MAX_TIMEOUT && status & KCS_STATUS_OBF) {
86226031Sstas			DELAY(100);
87226031Sstas			status = INB(sc, KCS_CTL_STS);
88226031Sstas		}
89226031Sstas	} else {
90226031Sstas		/* WAIT FOR OBF = 1 */
91226031Sstas		while (ticks - start < MAX_TIMEOUT &&
92226031Sstas		    !(status & KCS_STATUS_OBF)) {
93226031Sstas			DELAY(100);
94226031Sstas			status = INB(sc, KCS_CTL_STS);
95226031Sstas		}
96226031Sstas	}
97226031Sstas	return (status);
98226031Sstas}
99226031Sstas
100226031Sstasstatic void
101226031Sstaskcs_clear_obf(struct ipmi_softc *sc, int status)
102226031Sstas{
103226031Sstas	int data;
104226031Sstas
105226031Sstas	/* Clear OBF */
106226031Sstas	if (status & KCS_STATUS_OBF) {
107226031Sstas		data = INB(sc, KCS_DATA);
108226031Sstas	}
109226031Sstas}
110226031Sstas
111226031Sstasstatic void
112226031Sstaskcs_error(struct ipmi_softc *sc)
113234027Sstas{
114226031Sstas	int retry, status;
115226031Sstas	u_char data;
116234027Sstas
117226031Sstas	for (retry = 0; retry < 2; retry++) {
118226031Sstas
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 */
179static int
180kcs_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
188		/* Clear OBF */
189		kcs_clear_obf(sc, status);
190
191		/* Write start to command */
192		OUTB(sc, KCS_CTL_STS, KCS_CONTROL_WRITE_START);
193
194		/* Wait for IBF = 0 */
195		status = kcs_wait_for_ibf(sc, 0);
196		if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_WRITE)
197			break;
198		DELAY(1000000);
199	}
200
201	if (KCS_STATUS_STATE(status) != KCS_STATUS_STATE_WRITE)
202		/* error state */
203		return (0);
204
205	/* Clear OBF */
206	kcs_clear_obf(sc, status);
207
208	return (1);
209}
210
211/*
212 * Write a byte of the request message, excluding the last byte of the
213 * message which requires special handling.
214 */
215static int
216kcs_write_byte(struct ipmi_softc *sc, u_char data)
217{
218	int status;
219
220	/* Data to Data */
221	OUTB(sc, KCS_DATA, data);
222
223	/* Wait for IBF = 0 */
224	status = kcs_wait_for_ibf(sc, 0);
225
226	if (KCS_STATUS_STATE(status) != KCS_STATUS_STATE_WRITE)
227		return (0);
228
229	/* Clear OBF */
230	kcs_clear_obf(sc, status);
231	return (1);
232}
233
234/*
235 * Write the last byte of a request message.
236 */
237static int
238kcs_write_last_byte(struct ipmi_softc *sc, u_char data)
239{
240	int status;
241
242	/* Write end to command */
243	OUTB(sc, KCS_CTL_STS, KCS_CONTROL_WRITE_END);
244
245	/* Wait for IBF = 0 */
246	status = kcs_wait_for_ibf(sc, 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 */
263static int
264kcs_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
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
292		/* Read Dummy */
293		dummy = INB(sc, KCS_DATA);
294		return (2);
295	}
296
297	/* Error State */
298	return (0);
299}
300
301/*
302 * Send a request message and collect the reply.  Returns true if we
303 * succeed.
304 */
305static int
306kcs_polled_request(struct ipmi_softc *sc, struct ipmi_request *req)
307{
308	u_char *cp, data;
309	int i, state;
310
311	/* Send the request. */
312	if (!kcs_start_write(sc)) {
313		device_printf(sc->ipmi_dev, "KCS: Failed to start write\n");
314		goto fail;
315	}
316#ifdef KCS_DEBUG
317	device_printf(sc->ipmi_dev, "KCS: WRITE_START... ok\n");
318#endif
319
320	if (!kcs_write_byte(sc, req->ir_addr)) {
321		device_printf(sc->ipmi_dev, "KCS: Failed to write address\n");
322		goto fail;
323	}
324#ifdef KCS_DEBUG
325	device_printf(sc->ipmi_dev, "KCS: Wrote address: %02x\n", req->ir_addr);
326#endif
327
328	if (req->ir_requestlen == 0) {
329		if (!kcs_write_last_byte(sc, req->ir_command)) {
330			device_printf(sc->ipmi_dev,
331			    "KCS: Failed to write command\n");
332			goto fail;
333		}
334#ifdef KCS_DEBUG
335		device_printf(sc->ipmi_dev, "KCS: Wrote command: %02x\n",
336		    req->ir_command);
337#endif
338	} else {
339		if (!kcs_write_byte(sc, req->ir_command)) {
340			device_printf(sc->ipmi_dev,
341			    "KCS: Failed to write command\n");
342			goto fail;
343		}
344#ifdef KCS_DEBUG
345		device_printf(sc->ipmi_dev, "KCS: Wrote command: %02x\n",
346		    req->ir_command);
347#endif
348
349		cp = req->ir_request;
350		for (i = 0; i < req->ir_requestlen - 1; i++) {
351			if (!kcs_write_byte(sc, *cp++)) {
352				device_printf(sc->ipmi_dev,
353				    "KCS: Failed to write data byte %d\n",
354				    i + 1);
355				goto fail;
356			}
357#ifdef KCS_DEBUG
358			device_printf(sc->ipmi_dev, "KCS: Wrote data: %02x\n",
359			    cp[-1]);
360#endif
361		}
362
363		if (!kcs_write_last_byte(sc, *cp)) {
364			device_printf(sc->ipmi_dev,
365			    "KCS: Failed to write last dta byte\n");
366			goto fail;
367		}
368#ifdef KCS_DEBUG
369		device_printf(sc->ipmi_dev, "KCS: Wrote last data: %02x\n",
370		    *cp);
371#endif
372	}
373
374	/* Read the reply.  First, read the NetFn/LUN. */
375	if (kcs_read_byte(sc, &data) != 1) {
376		device_printf(sc->ipmi_dev, "KCS: Failed to read address\n");
377		goto fail;
378	}
379#ifdef KCS_DEBUG
380	device_printf(sc->ipmi_dev, "KCS: Read address: %02x\n", data);
381#endif
382	if (data != IPMI_REPLY_ADDR(req->ir_addr)) {
383		device_printf(sc->ipmi_dev, "KCS: Reply address mismatch\n");
384		goto fail;
385	}
386
387	/* Next we read the command. */
388	if (kcs_read_byte(sc, &data) != 1) {
389		device_printf(sc->ipmi_dev, "KCS: Failed to read command\n");
390		goto fail;
391	}
392#ifdef KCS_DEBUG
393	device_printf(sc->ipmi_dev, "KCS: Read command: %02x\n", data);
394#endif
395	if (data != req->ir_command) {
396		device_printf(sc->ipmi_dev, "KCS: Command mismatch\n");
397		goto fail;
398	}
399
400	/* Next we read the completion code. */
401	if (kcs_read_byte(sc, &req->ir_compcode) != 1) {
402		device_printf(sc->ipmi_dev,
403		    "KCS: Failed to read completion code\n");
404		goto fail;
405	}
406#ifdef KCS_DEBUG
407	device_printf(sc->ipmi_dev, "KCS: Read completion code: %02x\n",
408	    req->ir_compcode);
409#endif
410
411	/* Finally, read the reply from the BMC. */
412	i = 0;
413	for (;;) {
414		state = kcs_read_byte(sc, &data);
415		if (state == 0) {
416			device_printf(sc->ipmi_dev,
417			    "KCS: Read failed on byte %d\n", i + 1);
418			goto fail;
419		}
420		if (state == 2)
421			break;
422		if (i < req->ir_replybuflen) {
423			req->ir_reply[i] = data;
424#ifdef KCS_DEBUG
425			device_printf(sc->ipmi_dev, "KCS: Read data %02x\n",
426			    data);
427		} else {
428			device_printf(sc->ipmi_dev,
429			    "KCS: Read short %02x byte %d\n", data, i + 1);
430#endif
431		}
432		i++;
433	}
434	req->ir_replylen = i;
435#ifdef KCS_DEBUG
436	device_printf(sc->ipmi_dev, "KCS: READ finished (%d bytes)\n", i);
437	if (req->ir_replybuflen < i)
438#else
439	if (req->ir_replybuflen < i && req->ir_replybuflen != 0)
440#endif
441		device_printf(sc->ipmi_dev,
442		    "KCS: Read short: %zd buffer, %d actual\n",
443		    req->ir_replybuflen, i);
444	return (1);
445fail:
446	kcs_error(sc);
447	return (0);
448}
449
450static void
451kcs_loop(void *arg)
452{
453	struct ipmi_softc *sc = arg;
454	struct ipmi_request *req;
455	int i, ok;
456
457	IPMI_LOCK(sc);
458	while ((req = ipmi_dequeue_request(sc)) != NULL) {
459		IPMI_UNLOCK(sc);
460		ok = 0;
461		for (i = 0; i < 3 && !ok; i++)
462			ok = kcs_polled_request(sc, req);
463		if (ok)
464			req->ir_error = 0;
465		else
466			req->ir_error = EIO;
467		IPMI_LOCK(sc);
468		ipmi_complete_request(sc, req);
469	}
470	IPMI_UNLOCK(sc);
471	kproc_exit(0);
472}
473
474static int
475kcs_startup(struct ipmi_softc *sc)
476{
477
478	return (kproc_create(kcs_loop, sc, &sc->ipmi_kthread, 0, 0, "%s: kcs",
479	    device_get_nameunit(sc->ipmi_dev)));
480}
481
482int
483ipmi_kcs_attach(struct ipmi_softc *sc)
484{
485	int status;
486
487	/* Setup function pointers. */
488	sc->ipmi_startup = kcs_startup;
489	sc->ipmi_enqueue_request = ipmi_polled_enqueue_request;
490
491	/* See if we can talk to the controller. */
492	status = INB(sc, KCS_CTL_STS);
493	if (status == 0xff) {
494		device_printf(sc->ipmi_dev, "couldn't find it\n");
495		return (ENXIO);
496	}
497
498#ifdef KCS_DEBUG
499	device_printf(sc->ipmi_dev, "KCS: initial state: %02x\n", status);
500#endif
501	if (status & KCS_STATUS_OBF ||
502	    KCS_STATUS_STATE(status) != KCS_STATUS_STATE_IDLE)
503		kcs_error(sc);
504
505	return (0);
506}
507
508/*
509 * Determine the alignment automatically for a PCI attachment.  In this case,
510 * any unused bytes will return 0x00 when read.  We make use of the C/D bit
511 * in the CTL_STS register to try to start a GET_STATUS transaction.  When
512 * we write the command, that bit should be set, so we should get a non-zero
513 * value back when we read CTL_STS if the offset we are testing is the CTL_STS
514 * register.
515 */
516int
517ipmi_kcs_probe_align(struct ipmi_softc *sc)
518{
519	int data, status;
520
521	sc->ipmi_io_spacing = 1;
522retry:
523#ifdef KCS_DEBUG
524	device_printf(sc->ipmi_dev, "Trying KCS align %d... ", sc->ipmi_io_spacing);
525#endif
526
527	/* Wait for IBF = 0 */
528	status = INB(sc, KCS_CTL_STS);
529	while (status & KCS_STATUS_IBF) {
530		DELAY(100);
531		status = INB(sc, KCS_CTL_STS);
532	}
533
534	OUTB(sc, KCS_CTL_STS, KCS_CONTROL_GET_STATUS_ABORT);
535
536	/* Wait for IBF = 0 */
537	status = INB(sc, KCS_CTL_STS);
538	while (status & KCS_STATUS_IBF) {
539		DELAY(100);
540		status = INB(sc, KCS_CTL_STS);
541	}
542
543	/* If we got 0x00 back, then this must not be the CTL_STS register. */
544	if (status == 0) {
545#ifdef KCS_DEBUG
546		printf("failed\n");
547#endif
548		sc->ipmi_io_spacing <<= 1;
549		if (sc->ipmi_io_spacing > 4)
550			return (0);
551		goto retry;
552	}
553#ifdef KCS_DEBUG
554	printf("ok\n");
555#endif
556
557	/* Finish out the transaction. */
558
559	/* Clear OBF */
560	if (status & KCS_STATUS_OBF)
561		data = INB(sc, KCS_DATA);
562
563	/* 0x00 to DATA_IN */
564	OUTB(sc, KCS_DATA, 0);
565
566	/* Wait for IBF = 0 */
567	status = INB(sc, KCS_CTL_STS);
568	while (status & KCS_STATUS_IBF) {
569		DELAY(100);
570		status = INB(sc, KCS_CTL_STS);
571	}
572
573	if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_READ) {
574		/* Wait for IBF = 1 */
575		while (!(status & KCS_STATUS_OBF)) {
576			DELAY(100);
577			status = INB(sc, KCS_CTL_STS);
578		}
579
580		/* Read error status. */
581		data = INB(sc, KCS_DATA);
582
583		/* Write dummy READ to DATA_IN. */
584		OUTB(sc, KCS_DATA, KCS_DATA_IN_READ);
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
594	if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_IDLE) {
595		/* Wait for IBF = 1 */
596		while (!(status & KCS_STATUS_OBF)) {
597			DELAY(100);
598			status = INB(sc, KCS_CTL_STS);
599		}
600
601		/* Clear OBF */
602		if (status & KCS_STATUS_OBF)
603			data = INB(sc, KCS_DATA);
604	} else
605		device_printf(sc->ipmi_dev, "KCS probe: end state %x\n",
606		    KCS_STATUS_STATE(status));
607
608	return (1);
609}
610