aboutsummaryrefslogtreecommitdiff
path: root/sys/dev/ipmi/ipmi_bt.c
blob: 800893977f1581e9f00d306cf83bd86994048678 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*-
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Copyright (c) 2023 Yandex LLC
 * Copyright (c) 2023 Andrey V. Elsukov <ae@FreeBSD.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/condvar.h>
#include <sys/eventhandler.h>
#include <sys/kernel.h>
#include <sys/kthread.h>
#include <sys/module.h>
#include <sys/rman.h>
#include <sys/selinfo.h>
#include <machine/bus.h>

#include <sys/ipmi.h>
#include <dev/ipmi/ipmivars.h>

/*
 * BT interface
 */

#define	DMSG0(sc, fmt, ...)	do {				\
	device_printf((sc)->ipmi_dev, "BT: %s: " fmt "\n",	\
	    __func__, ## __VA_ARGS__);				\
} while (0)

#define	DMSGV(...)		if (bootverbose) {		\
	DMSG0(__VA_ARGS__);					\
}

#ifdef IPMI_BT_DEBUG
#define	DMSG(...)		DMSG0(__VA_ARGS__)
#else
#define	DMSG(...)
#endif

#define	BT_IO_BASE		0xe4

#define	BT_CTRL_REG		0
#define	  BT_C_CLR_WR_PTR	(1L << 0)
#define	  BT_C_CLR_RD_PTR	(1L << 1)
#define	  BT_C_H2B_ATN		(1L << 2)
#define	  BT_C_B2H_ATN		(1L << 3)
#define	  BT_C_SMS_ATN		(1L << 4)
#define	  BT_C_OEM0		(1L << 5)
#define	  BT_C_H_BUSY		(1L << 6)
#define	  BT_C_B_BUSY		(1L << 7)

#define	BT_CTRL_BITS		"\20\01CLR_WR_PTR\02CLR_RD_PTR\03H2B_ATN\04B2H_ATN"\
				"\05SMS_ATN\06OEM0\07H_BUSY\010B_BUSY"

#define	BT_DATA_REG		1
#define	 BTMSG_REQLEN		3
#define	 BTMSG_REPLEN		4

#define	BT_INTMASK_REG		2
#define	 BT_IM_B2H_IRQ_EN	(1L << 0)
#define	 BT_IM_B2H_IRQ		(1L << 1)
#define	 BT_IM_BMC_HWRST	(1L << 7)

static int bt_polled_request(struct ipmi_softc *, struct ipmi_request *);
static int bt_driver_request(struct ipmi_softc *, struct ipmi_request *, int);
static int bt_wait(struct ipmi_softc *, uint8_t, uint8_t);
static int bt_reset(struct ipmi_softc *);

static void bt_loop(void *);
static int bt_startup(struct ipmi_softc *);

#define	BT_DELAY_MIN	1
#define	BT_DELAY_MAX	256

static int
bt_wait(struct ipmi_softc *sc, uint8_t mask, uint8_t wanted)
{
	volatile uint8_t value;
	int delay = BT_DELAY_MIN;
	int count = 20000; /* about 5 seconds */

	while (count--) {
		value = INB(sc, BT_CTRL_REG);
		if ((value & mask) == wanted)
			return (value);
		/*
		 * The wait delay is increased exponentially to avoid putting
		 * significant load on I/O bus.
		 */
		DELAY(delay);
		if (delay < BT_DELAY_MAX)
			delay <<= 1;
	}
	DMSGV(sc, "failed: m=%b w=%b v=0x%02x\n",
	    mask, BT_CTRL_BITS, wanted, BT_CTRL_BITS, value);
	return (-1);

}

static int
bt_reset(struct ipmi_softc *sc)
{
	uint8_t v;

	v = INB(sc, BT_CTRL_REG);
	DMSG(sc, "ctrl: %b", v, BT_CTRL_BITS);
	v &= BT_C_H_BUSY; /* clear H_BUSY iff it set */
	v |= BT_C_CLR_WR_PTR | BT_C_CLR_RD_PTR | BT_C_B2H_ATN | BT_C_H2B_ATN;

	bt_wait(sc, BT_C_B_BUSY, 0);
	OUTB(sc, BT_CTRL_REG, v);

	v = BT_IM_B2H_IRQ | BT_IM_BMC_HWRST;
	OUTB(sc, BT_INTMASK_REG, v);

	return (0);
}

/*
 * Send a request message and collect the reply. Returns 1 if we
 * succeed.
 */
static int
bt_polled_request(struct ipmi_softc *sc, struct ipmi_request *req)
{
	uint8_t addr, cmd, seq, v;
	int i;

	IPMI_IO_LOCK(sc);

	/*
	 * Send the request:
	 *
	 * Byte 1 | Byte 2    | Byte 3 | Byte 4 | Byte 5:N
	 * -------+-----------+--------+--------+---------
	 * Length | NetFn/LUN | Seq    | Cmd    | Data
	 */

	if (bt_wait(sc, BT_C_B_BUSY | BT_C_H2B_ATN, 0) < 0) {
		DMSG(sc, "failed to start write transfer");
		goto fail;
	}
	DMSG(sc, "request: length=%d, addr=0x%02x, seq=%u, cmd=0x%02x",
	    (int)req->ir_requestlen, req->ir_addr, sc->ipmi_bt_seq, req->ir_command);
	OUTB(sc, BT_CTRL_REG, BT_C_CLR_WR_PTR);
	OUTB(sc, BT_DATA_REG, req->ir_requestlen + BTMSG_REQLEN);
	OUTB(sc, BT_DATA_REG, req->ir_addr);
	OUTB(sc, BT_DATA_REG, sc->ipmi_bt_seq);
	OUTB(sc, BT_DATA_REG, req->ir_command);
	for (i = 0; i < req->ir_requestlen; i++)
		OUTB(sc, BT_DATA_REG, req->ir_request[i]);
	OUTB(sc, BT_CTRL_REG, BT_C_H2B_ATN);

	if (bt_wait(sc, BT_C_B_BUSY | BT_C_H2B_ATN, 0) < 0) {
		DMSG(sc, "failed to finish write transfer");
		goto fail;
	}

	/*
	 * Read the reply:
	 *
	 * Byte 1 | Byte 2    | Byte 3 | Byte 4 | Byte 5          | Byte 6:N
	 * -------+-----------+--------+--------+-----------------+---------
	 * Length | NetFn/LUN | Seq    | Cmd    | Completion code | Data
	 */
	if (bt_wait(sc, BT_C_B2H_ATN, BT_C_B2H_ATN) < 0) {
		DMSG(sc, "got no reply from BMC");
		goto fail;
	}
	OUTB(sc, BT_CTRL_REG, BT_C_H_BUSY);
	OUTB(sc, BT_CTRL_REG, BT_C_B2H_ATN);
	OUTB(sc, BT_CTRL_REG, BT_C_CLR_RD_PTR);

	i = INB(sc, BT_DATA_REG);
	if (i < BTMSG_REPLEN) {
		DMSG(sc, "wrong data length: %d", i);
		goto fail;
	}
	req->ir_replylen = i - BTMSG_REPLEN;
	DMSG(sc, "data length: %d, frame length: %d", req->ir_replylen, i);

	addr = INB(sc, BT_DATA_REG);
	if (addr != IPMI_REPLY_ADDR(req->ir_addr)) {
		DMSGV(sc, "address doesn't match: addr=0x%02x vs. 0x%02x",
		    req->ir_addr, addr);
	}

	seq = INB(sc, BT_DATA_REG);
	if (seq != sc->ipmi_bt_seq) {
		DMSGV(sc, "seq number doesn't match: seq=0x%02x vs. 0x%02x",
		    sc->ipmi_bt_seq, seq);
	}

	cmd = INB(sc, BT_DATA_REG);
	if (cmd != req->ir_command) {
		DMSGV(sc, "command doesn't match: cmd=0x%02x vs. 0x%02x",
		    req->ir_command, cmd);
	}

	req->ir_compcode = INB(sc, BT_DATA_REG);
	for (i = 0; i < req->ir_replylen; i++) {
		v = INB(sc, BT_DATA_REG);
		if (i < req->ir_replybuflen)
			req->ir_reply[i] = v;
	}

	OUTB(sc, BT_CTRL_REG, BT_C_H_BUSY);
	IPMI_IO_UNLOCK(sc);
	DMSG(sc, "reply: length=%d, addr=0x%02x, seq=%u, cmd=0x%02x, code=0x%02x",
	    (int)req->ir_replylen, addr, seq, req->ir_command, req->ir_compcode);
	return (1);
fail:
	bt_reset(sc);
	IPMI_IO_UNLOCK(sc);
	return (0);
}

static void
bt_loop(void *arg)
{
	struct ipmi_softc *sc = arg;
	struct ipmi_request *req;

	IPMI_LOCK(sc);
	while ((req = ipmi_dequeue_request(sc)) != NULL) {
		IPMI_UNLOCK(sc);
		(void)bt_driver_request(sc, req, 0);
		IPMI_LOCK(sc);
		sc->ipmi_bt_seq++;
		ipmi_complete_request(sc, req);
	}
	IPMI_UNLOCK(sc);
	kproc_exit(0);
}

static int
bt_startup(struct ipmi_softc *sc)
{

	return (kproc_create(bt_loop, sc, &sc->ipmi_kthread, 0, 0, "%s: bt",
	    device_get_nameunit(sc->ipmi_dev)));
}

static int
bt_driver_request(struct ipmi_softc *sc, struct ipmi_request *req, int timo __unused)
{
	int i, ok;

	ok = 0;
	for (i = 0; i < 3 && !ok; i++)
		ok = bt_polled_request(sc, req);
	if (ok)
		req->ir_error = 0;
	else
		req->ir_error = EIO;
	return (req->ir_error);
}

int
ipmi_bt_attach(struct ipmi_softc *sc)
{
	/* Setup function pointers. */
	sc->ipmi_startup = bt_startup;
	sc->ipmi_enqueue_request = ipmi_polled_enqueue_request;
	sc->ipmi_driver_request = bt_driver_request;
	sc->ipmi_driver_requests_polled = 1;
	sc->ipmi_bt_seq = 1;

	return (bt_reset(sc));
}