summaryrefslogtreecommitdiff
path: root/sys/dev/i2c/ipmi_i2c.c
blob: 41f86dbdf0e1d3cfa2774dd31bcc36b12e41a69d (plain)
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
/*	$OpenBSD: ipmi_i2c.c,v 1.3 2020/01/11 20:41:34 kettenis Exp $	*/
/*
 * Copyright (c) 2019 Mark Kettenis <kettenis@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>

#include <machine/bus.h>

#include <dev/i2c/i2cvar.h>
#include <dev/ipmivar.h>

#define BMC_SA			0x20	/* BMC/ESM3 */
#define BMC_LUN			0

struct ipmi_i2c_softc {
	struct ipmi_softc sc;
	i2c_tag_t	sc_tag;
	i2c_addr_t	sc_addr;
	uint8_t		sc_rev;
};

void	cmn_buildmsg(struct ipmi_cmd *);
int	ssif_sendmsg(struct ipmi_cmd *);
int	ssif_recvmsg(struct ipmi_cmd *);
int	ssif_reset(struct ipmi_softc *);
int	ssif_probe(struct ipmi_softc *);

struct ipmi_if ssif_if = {
	"SSIF",
	0,
	cmn_buildmsg,
	ssif_sendmsg,
	ssif_recvmsg,
	ssif_reset,
	ssif_probe,
	IPMI_MSG_DATASND,
	IPMI_MSG_DATARCV
};

extern void ipmi_attach(struct device *, struct device *, void *);

int	ipmi_i2c_match(struct device *, void *, void *);
void	ipmi_i2c_attach(struct device *, struct device *, void *);

struct cfattach ipmi_i2c_ca = {
	sizeof(struct ipmi_i2c_softc), ipmi_i2c_match, ipmi_i2c_attach
};

int	ipmi_i2c_get_interface_caps(struct ipmi_i2c_softc *);
int	ipmi_i2c_get_device_id(struct ipmi_i2c_softc *);

int
ipmi_i2c_match(struct device *parent, void *match, void *aux)
{
	struct i2c_attach_args *ia = aux;

	return (strcmp(ia->ia_name, "IPI0001") == 0 ||
	    strcmp(ia->ia_name, "APMC0D8A") == 0);
}

void
ipmi_i2c_attach(struct device *parent, struct device *self, void *aux)
{
	struct ipmi_i2c_softc *sc = (struct ipmi_i2c_softc *)self;
	struct i2c_attach_args *ia = aux;
	struct ipmi_attach_args iaa;

	sc->sc_tag = ia->ia_tag;
	sc->sc_addr = ia->ia_addr;
	sc->sc.sc_if = &ssif_if;

	if (ipmi_i2c_get_interface_caps(sc)) {
		printf(": can't get system interface capabilities\n");
		return;
	}

	if (ipmi_i2c_get_device_id(sc)) {
		printf(": can't get system interface capabilities\n");
		return;
	}

	memset(&iaa, 0, sizeof(iaa));
	iaa.iaa_if_type = IPMI_IF_SSIF;
	iaa.iaa_if_rev = (sc->sc_rev >> 4 | sc->sc_rev << 4);
	iaa.iaa_if_irq = -1;
	ipmi_attach_common(&sc->sc, &iaa);
}

int
ipmi_i2c_get_interface_caps(struct ipmi_i2c_softc *sc)
{
	struct ipmi_cmd c;
	uint8_t data[5];

	data[0] = 0;		/* SSIF */

	c.c_sc = &sc->sc;
	c.c_rssa = BMC_SA;
	c.c_rslun = BMC_LUN;
	c.c_netfn = APP_NETFN;
	c.c_cmd = APP_GET_SYSTEM_INTERFACE_CAPS;
	c.c_txlen = 1;
	c.c_rxlen = 0;
	c.c_maxrxlen = sizeof(data);
	c.c_data = data;
	if (ipmi_sendcmd(&c) || ipmi_recvcmd(&c))
		return EIO;

	/* Check SSIF version number. */
	if ((data[1] & 0x7) != 0)
		return EINVAL;
	/* Check input and output message sizes. */
	if (data[2] < 32 || data[3] < 32)
		return EINVAL;

	return 0;
}

int
ipmi_i2c_get_device_id(struct ipmi_i2c_softc *sc)
{
	struct ipmi_cmd c;
	uint8_t data[16];

	c.c_sc = &sc->sc;
	c.c_rssa = BMC_SA;
	c.c_rslun = BMC_LUN;
	c.c_netfn = APP_NETFN;
	c.c_cmd = APP_GET_DEVICE_ID;
	c.c_txlen = 0;
	c.c_rxlen = 0;
	c.c_maxrxlen = sizeof(data);
	c.c_data = data;
	if (ipmi_sendcmd(&c) || ipmi_recvcmd(&c))
		return EIO;

	sc->sc_rev = data[4];
	return 0;
}

int
ssif_sendmsg(struct ipmi_cmd *c)
{
	struct ipmi_i2c_softc *sc = (struct ipmi_i2c_softc *)c->c_sc;
	uint8_t *buf = sc->sc.sc_buf;
	uint8_t cmd[2];
	int error, retry;

	/* We only support single-part writes. */
	if (c->c_txlen > 32)
		return -1;

	iic_acquire_bus(sc->sc_tag, 0);

	cmd[0] = 2;
	cmd[1] = c->c_txlen;
	for (retry = 0; retry < 5; retry++) {
		error = iic_exec(sc->sc_tag, I2C_OP_WRITE_BLOCK,
		    sc->sc_addr, cmd, sizeof(cmd), buf, c->c_txlen, 0);
		if (!error)
			break;
	}

	iic_release_bus(sc->sc_tag, 0);

	return (error ? -1 : 0);
}

int
ssif_recvmsg(struct ipmi_cmd *c)
{
	struct ipmi_i2c_softc *sc = (struct ipmi_i2c_softc *)c->c_sc;
	uint8_t buf[33];
	uint8_t cmd[1];
	uint8_t len;
	int error, retry;
	int blkno = 0;

	iic_acquire_bus(sc->sc_tag, 0);

	cmd[0] = 3;
	for (retry = 0; retry < 250; retry++) {
		memset(buf, 0, sizeof(buf));
		error = iic_exec(sc->sc_tag, I2C_OP_READ_BLOCK,
		    sc->sc_addr, cmd, sizeof(cmd), buf, sizeof(buf), 0);
		if (error)
			continue;

		if (buf[0] < 1 || buf[0] > 32) {
			error = EIO;
			goto release;
		}

		if (buf[0] == 32 && buf[1] == 0x00 && buf[2] == 0x01) {
			/* Multi-part read start. */
			c->c_rxlen = MIN(30, c->c_maxrxlen);
			memcpy(sc->sc.sc_buf, &buf[3], c->c_rxlen);
			break;
		} else {
			/* Single-part read. */
			c->c_rxlen = MIN(buf[0], c->c_maxrxlen);
			memcpy(sc->sc.sc_buf, &buf[1], c->c_rxlen);
			goto release;
		}
	}
	if (retry == 250)
		goto release;

	cmd[0] = 9;
	while (buf[1] != 0xff && c->c_rxlen < c->c_maxrxlen) {
		memset(buf, 0, sizeof(buf));
		error = iic_exec(sc->sc_tag, I2C_OP_READ_BLOCK,
		    sc->sc_addr, cmd, sizeof(cmd), buf, sizeof(buf), 0);
		if (error)
			goto release;

		if (buf[0] < 1 || buf[0] > 32) {
			error = EIO;
			goto release;
		}

		if (buf[0] == 32 && buf[1] == blkno) {
			/* Multi-part read middle. */
			len = MIN(31, c->c_maxrxlen - c->c_rxlen);
			memcpy(&sc->sc.sc_buf[c->c_rxlen], &buf[2], len);
		} else if (buf[1] == 0xff) {
			/* Multi-part read end. */
			len = MIN(buf[0] - 1, c->c_maxrxlen - c->c_rxlen);
			memcpy(&sc->sc.sc_buf[c->c_rxlen], &buf[2], len);
		} else {
			error = EIO;
			goto release;
		}
		c->c_rxlen += len;
		blkno++;
	}

release:
	iic_release_bus(sc->sc_tag, 0);

	return (error ? -1 : 0);
}

int
ssif_reset(struct ipmi_softc *sc)
{
	return -1;
}

int
ssif_probe(struct ipmi_softc *sc)
{
	return 0;
}