xref: /freebsd-14-stable/sys/dev/ipmi/ipmi_opal.c (revision 1e84a6ba21fddc77c33ad059e476a302f40cd544)
1 /*-
2  * Copyright (C) 2018 Justin Hibbits
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
17  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
18  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
19  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
20  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
21  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24 
25 #include <sys/cdefs.h>
26 #include <sys/param.h>
27 #include <sys/kernel.h>
28 #include <sys/systm.h>
29 #include <sys/lock.h>
30 #include <sys/module.h>
31 #include <sys/mutex.h>
32 #include <sys/bus.h>
33 #include <sys/kthread.h>
34 #include <sys/proc.h>
35 #include <sys/selinfo.h>
36 #include <sys/sysctl.h>
37 
38 #include <vm/vm.h>
39 #include <vm/pmap.h>
40 
41 #include <machine/bus.h>
42 
43 #include <dev/ofw/openfirm.h>
44 #include <dev/ofw/ofw_bus.h>
45 #include <dev/ofw/ofw_bus_subr.h>
46 
47 #include <sys/ipmi.h>
48 #include <dev/ipmi/ipmivars.h>
49 
50 #include <powerpc/powernv/opal.h>
51 
52 /*
53  * OPAL_IPMI_DEBUG
54  *
55  * 0 - disabled
56  * 1 - enable error messages (EPRINTF)
57  * 2 - enable error and debug messages (DPRINTF)
58  */
59 #define	OPAL_IPMI_DEBUG		0
60 #if OPAL_IPMI_DEBUG >= 2
61 /* debug printf */
62 #define	DPRINTF(fmt, ...)	printf("ipmi: " fmt "\n", ## __VA_ARGS__)
63 #else
64 #define	DPRINTF(fmt, ...)	((void)0)
65 #endif
66 #if OPAL_IPMI_DEBUG >= 1
67 /* error printf: to print messages only when something fails */
68 #define	EPRINTF(fmt, ...)	printf("ipmi: " fmt "\n", ## __VA_ARGS__)
69 #else
70 #define	EPRINTF(fmt, ...)	((void)0)
71 #endif
72 
73 struct opal_ipmi_softc {
74 	struct ipmi_softc ipmi;
75 	uint64_t sc_interface;
76 	int sc_timedout;
77 	struct opal_ipmi_msg *sc_msg; /* Protected by IPMI lock */
78 };
79 
80 static MALLOC_DEFINE(M_IPMI, "ipmi", "OPAL IPMI");
81 
82 static int
opal_ipmi_recv(struct opal_ipmi_softc * sc,uint64_t * msg_len,int timo)83 opal_ipmi_recv(struct opal_ipmi_softc *sc, uint64_t *msg_len, int timo)
84 {
85 	int err;
86 
87 	if (timo == 0)
88 		timo = MAX_TIMEOUT;
89 	timo *= 10; /* Timeout is in milliseconds, we delay in 100us */
90 
91 	for (;;) {
92 		*msg_len = sizeof(struct opal_ipmi_msg) + IPMI_MAX_RX;
93 		/* Crank the OPAL state machine while we poll for a reply. */
94 		opal_call(OPAL_POLL_EVENTS, NULL);
95 		err = opal_call(OPAL_IPMI_RECV, sc->sc_interface,
96 		    vtophys(sc->sc_msg), vtophys(msg_len));
97 		*msg_len = be64toh(*msg_len);
98 		if (err != OPAL_EMPTY)
99 			break;
100 
101 		DELAY(100);
102 		if (timo-- <= 0) {
103 			sc->sc_timedout = 1;
104 			break;
105 		}
106 	}
107 
108 	if (err != OPAL_SUCCESS)
109 		EPRINTF("RECV: error: %d", err);
110 
111 	switch (err) {
112 	case OPAL_SUCCESS:
113 		DPRINTF("RECV: rv=%02x len=%ld",
114 		    sc->sc_msg->data[0], *msg_len);
115 		return (0);
116 	case OPAL_RESOURCE:
117 		return (ENOMEM);
118 	case OPAL_EMPTY:
119 		return (EAGAIN);
120 	default:
121 		return (EIO);
122 	}
123 }
124 
125 static void
opal_ipmi_discard_msgs(struct opal_ipmi_softc * sc)126 opal_ipmi_discard_msgs(struct opal_ipmi_softc *sc)
127 {
128 	uint64_t msg_len;
129 	int err, i = 0;
130 
131 	/* OPAL_IPMI_RECV fails when msg version is not set. */
132 	sc->sc_msg->version = OPAL_IPMI_MSG_FORMAT_VERSION_1;
133 
134 	/* Wait up to 100ms for the 1st timedout message. */
135 	err = opal_ipmi_recv(sc, &msg_len, 100);
136 	while (err == 0) {
137 		i++;
138 		/* Wait only 10ms for the remaining messages. */
139 		err = opal_ipmi_recv(sc, &msg_len, 10);
140 	}
141 	if (i > 0)
142 		EPRINTF("Discarded %d message(s)", i);
143 	sc->sc_timedout = 0;
144 }
145 
146 static int
opal_ipmi_polled_request(struct opal_ipmi_softc * sc,struct ipmi_request * req,int timo)147 opal_ipmi_polled_request(struct opal_ipmi_softc *sc, struct ipmi_request *req,
148     int timo)
149 {
150 	uint64_t msg_len;
151 	int err;
152 
153 	/*
154 	 * Discard timed out messages before sending a new one, to avoid
155 	 * them being confused with the reply of the new message.
156 	 */
157 	if (sc->sc_timedout)
158 		opal_ipmi_discard_msgs(sc);
159 
160 	/* Construct and send the message. */
161 	sc->sc_msg->version = OPAL_IPMI_MSG_FORMAT_VERSION_1;
162 	sc->sc_msg->netfn = req->ir_addr;
163 	sc->sc_msg->cmd = req->ir_command;
164 
165 	if (req->ir_requestlen > IPMI_MAX_RX) {
166 		err = ENOMEM;
167 		goto out;
168 	}
169 	memcpy(sc->sc_msg->data, req->ir_request, req->ir_requestlen);
170 
171 	msg_len = sizeof(*sc->sc_msg) + req->ir_requestlen;
172 	err = opal_call(OPAL_IPMI_SEND, sc->sc_interface, vtophys(sc->sc_msg),
173 	    msg_len);
174 
175 	DPRINTF("SEND: cmd=%02x netfn=%02x len=%ld -> %d",
176 	    sc->sc_msg->cmd, sc->sc_msg->netfn, msg_len, err);
177 
178 	if (err != OPAL_SUCCESS)
179 		EPRINTF("SEND: error: %d", err);
180 
181 	switch (err) {
182 	case OPAL_SUCCESS:
183 		break;
184 	case OPAL_PARAMETER:
185 	case OPAL_UNSUPPORTED:
186 		err = EINVAL;
187 		goto out;
188 	case OPAL_RESOURCE:
189 		err = ENOMEM;
190 		goto out;
191 	case OPAL_HARDWARE:
192 	default:
193 		err = EIO;
194 		goto out;
195 	}
196 
197 	if ((err = opal_ipmi_recv(sc, &msg_len, timo)) == 0) {
198 		/* Subtract one extra for the completion code. */
199 		req->ir_replylen = msg_len - sizeof(struct opal_ipmi_msg) - 1;
200 		req->ir_replylen = min(req->ir_replylen, req->ir_replybuflen);
201 		memcpy(req->ir_reply, &sc->sc_msg->data[1], req->ir_replylen);
202 		req->ir_compcode = sc->sc_msg->data[0];
203 	}
204 
205 out:
206 	return (err);
207 }
208 
209 static int
opal_ipmi_probe(device_t dev)210 opal_ipmi_probe(device_t dev)
211 {
212 	if (!ofw_bus_is_compatible(dev, "ibm,opal-ipmi"))
213 		return (ENXIO);
214 
215 	device_set_desc(dev, "OPAL IPMI System Interface");
216 
217 	return (BUS_PROBE_DEFAULT);
218 }
219 
220 static void
opal_ipmi_loop(void * arg)221 opal_ipmi_loop(void *arg)
222 {
223 	struct opal_ipmi_softc *sc = arg;
224 	struct ipmi_request *req;
225 	int i, err;
226 
227 	IPMI_LOCK(&sc->ipmi);
228 	while ((req = ipmi_dequeue_request(&sc->ipmi)) != NULL) {
229 		IPMI_UNLOCK(&sc->ipmi);
230 		err = EIO;
231 		for (i = 0; i < 3 && err != 0; i++) {
232 			IPMI_IO_LOCK(&sc->ipmi);
233 			err = opal_ipmi_polled_request(sc, req, MAX_TIMEOUT);
234 			IPMI_IO_UNLOCK(&sc->ipmi);
235 		}
236 		req->ir_error = err == 0 ? 0 : EIO;
237 		IPMI_LOCK(&sc->ipmi);
238 		ipmi_complete_request(&sc->ipmi, req);
239 	}
240 	IPMI_UNLOCK(&sc->ipmi);
241 	kproc_exit(0);
242 }
243 
244 static int
opal_ipmi_startup(struct ipmi_softc * sc)245 opal_ipmi_startup(struct ipmi_softc *sc)
246 {
247 
248 	return (kproc_create(opal_ipmi_loop, sc, &sc->ipmi_kthread, 0, 0,
249 	    "%s: opal", device_get_nameunit(sc->ipmi_dev)));
250 }
251 
252 static int
opal_ipmi_driver_request(struct ipmi_softc * isc,struct ipmi_request * req,int timo)253 opal_ipmi_driver_request(struct ipmi_softc *isc, struct ipmi_request *req,
254     int timo)
255 {
256 	struct opal_ipmi_softc *sc = (struct opal_ipmi_softc *)isc;
257 	int i, err;
258 
259 	for (i = 0; i < 3; i++) {
260 		IPMI_LOCK(&sc->ipmi);
261 		err = opal_ipmi_polled_request(sc, req, timo);
262 		IPMI_UNLOCK(&sc->ipmi);
263 		if (err == 0)
264 			break;
265 	}
266 
267 	req->ir_error = err;
268 
269 	return (err);
270 }
271 
272 static int
opal_ipmi_attach(device_t dev)273 opal_ipmi_attach(device_t dev)
274 {
275 	struct opal_ipmi_softc *sc;
276 	pcell_t ifid;
277 
278 	sc = device_get_softc(dev);
279 
280 	if (OF_getencprop(ofw_bus_get_node(dev), "ibm,ipmi-interface-id",
281 	    &ifid, sizeof(ifid)) < 0) {
282 		device_printf(dev, "Missing interface id\n");
283 		return (ENXIO);
284 	}
285 	sc->sc_interface = ifid;
286 	sc->ipmi.ipmi_startup = opal_ipmi_startup;
287 	sc->ipmi.ipmi_driver_request = opal_ipmi_driver_request;
288 	sc->ipmi.ipmi_enqueue_request = ipmi_polled_enqueue_request;
289 	sc->ipmi.ipmi_driver_requests_polled = 1;
290 	sc->ipmi.ipmi_dev = dev;
291 
292 	sc->sc_msg = malloc(sizeof(struct opal_ipmi_msg) + IPMI_MAX_RX, M_IPMI,
293 	    M_WAITOK | M_ZERO);
294 
295 	/* Discard old messages that may have remained in receive queue. */
296 	opal_ipmi_discard_msgs(sc);
297 
298 	return (ipmi_attach(dev));
299 }
300 
301 static int
opal_ipmi_detach(device_t dev)302 opal_ipmi_detach(device_t dev)
303 {
304 	struct opal_ipmi_softc *sc;
305 	int err;
306 
307 	sc = device_get_softc(dev);
308 	err = ipmi_detach(dev);
309 	if (err == 0)
310 		free(sc->sc_msg, M_IPMI);
311 
312 	return (err);
313 }
314 
315 static device_method_t	opal_ipmi_methods[] = {
316 	/* Device interface */
317 	DEVMETHOD(device_probe,		opal_ipmi_probe),
318 	DEVMETHOD(device_attach,	opal_ipmi_attach),
319 	DEVMETHOD(device_detach,	opal_ipmi_detach),
320 	DEVMETHOD_END
321 };
322 
323 static driver_t opal_ipmi_driver = {
324 	"ipmi",
325 	opal_ipmi_methods,
326 	sizeof(struct opal_ipmi_softc)
327 };
328 
329 DRIVER_MODULE(opal_ipmi, opal, opal_ipmi_driver, NULL, NULL);
330