2 * Copyright (C) 2018 Justin Hibbits
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
25 #include <sys/cdefs.h>
26 __FBSDID("$FreeBSD$");
28 #include <sys/param.h>
29 #include <sys/kernel.h>
30 #include <sys/systm.h>
32 #include <sys/module.h>
33 #include <sys/mutex.h>
35 #include <sys/kthread.h>
37 #include <sys/selinfo.h>
38 #include <sys/sysctl.h>
43 #include <machine/bus.h>
45 #include <dev/ofw/openfirm.h>
46 #include <dev/ofw/ofw_bus.h>
47 #include <dev/ofw/ofw_bus_subr.h>
50 #include <dev/ipmi/ipmivars.h>
52 #include <powerpc/powernv/opal.h>
54 struct opal_ipmi_softc {
55 struct ipmi_softc ipmi;
56 uint64_t sc_interface;
57 struct opal_ipmi_msg *sc_msg; /* Protected by IPMI lock */
60 static MALLOC_DEFINE(M_IPMI, "ipmi", "OPAL IPMI");
63 opal_ipmi_polled_request(struct opal_ipmi_softc *sc, struct ipmi_request *req,
69 /* Construct and send the message. */
70 sc->sc_msg->version = OPAL_IPMI_MSG_FORMAT_VERSION_1;
71 sc->sc_msg->netfn = req->ir_addr;
72 sc->sc_msg->cmd = req->ir_command;
74 if (req->ir_requestlen > IPMI_MAX_RX) {
78 memcpy(sc->sc_msg->data, req->ir_request, req->ir_requestlen);
80 msg_len = sizeof(*sc->sc_msg) + req->ir_requestlen;
81 err = opal_call(OPAL_IPMI_SEND, sc->sc_interface, vtophys(sc->sc_msg),
92 case OPAL_UNSUPPORTED:
100 timo *= 10; /* Timeout is in milliseconds, we delay in 100us */
102 msg_len = sizeof(struct opal_ipmi_msg) + IPMI_MAX_RX;
103 /* Crank the OPAL state machine while we poll for a reply. */
104 opal_call(OPAL_POLL_EVENTS, NULL);
105 err = opal_call(OPAL_IPMI_RECV, sc->sc_interface,
106 vtophys(sc->sc_msg), vtophys(&msg_len));
107 if (err != OPAL_EMPTY)
110 } while (err == OPAL_EMPTY && timo-- != 0);
114 /* Subtract one extra for the completion code. */
115 req->ir_replylen = msg_len - sizeof(struct opal_ipmi_msg) - 1;
116 req->ir_replylen = min(req->ir_replylen, req->ir_replybuflen);
117 memcpy(req->ir_reply, &sc->sc_msg->data[1], req->ir_replylen);
118 req->ir_compcode = sc->sc_msg->data[0];
138 opal_ipmi_probe(device_t dev)
140 if (!ofw_bus_is_compatible(dev, "ibm,opal-ipmi"))
143 device_set_desc(dev, "OPAL IPMI System Interface");
145 return (BUS_PROBE_DEFAULT);
149 opal_ipmi_loop(void *arg)
151 struct opal_ipmi_softc *sc = arg;
152 struct ipmi_request *req;
155 IPMI_LOCK(&sc->ipmi);
156 while ((req = ipmi_dequeue_request(&sc->ipmi)) != NULL) {
157 IPMI_UNLOCK(&sc->ipmi);
159 for (i = 0; i < 3 && !ok; i++) {
160 IPMI_IO_LOCK(&sc->ipmi);
161 ok = opal_ipmi_polled_request(sc, req, MAX_TIMEOUT);
162 IPMI_IO_UNLOCK(&sc->ipmi);
168 IPMI_LOCK(&sc->ipmi);
169 ipmi_complete_request(&sc->ipmi, req);
171 IPMI_UNLOCK(&sc->ipmi);
176 opal_ipmi_startup(struct ipmi_softc *sc)
179 return (kproc_create(opal_ipmi_loop, sc, &sc->ipmi_kthread, 0, 0,
180 "%s: opal", device_get_nameunit(sc->ipmi_dev)));
184 opal_ipmi_driver_request(struct ipmi_softc *isc, struct ipmi_request *req,
187 struct opal_ipmi_softc *sc = (struct opal_ipmi_softc *)isc;
190 for (i = 0; i < 3; i++) {
191 IPMI_LOCK(&sc->ipmi);
192 err = opal_ipmi_polled_request(sc, req, timo);
193 IPMI_UNLOCK(&sc->ipmi);
204 opal_ipmi_attach(device_t dev)
206 struct opal_ipmi_softc *sc;
208 sc = device_get_softc(dev);
210 if (OF_getencprop(ofw_bus_get_node(dev), "ibm,ipmi-interface-id",
211 (pcell_t*)&sc->sc_interface, sizeof(sc->sc_interface)) < 0) {
212 device_printf(dev, "Missing interface id\n");
215 sc->ipmi.ipmi_startup = opal_ipmi_startup;
216 sc->ipmi.ipmi_driver_request = opal_ipmi_driver_request;
217 sc->ipmi.ipmi_enqueue_request = ipmi_polled_enqueue_request;
218 sc->ipmi.ipmi_driver_requests_polled = 1;
219 sc->ipmi.ipmi_dev = dev;
221 sc->sc_msg = malloc(sizeof(struct opal_ipmi_msg) + IPMI_MAX_RX, M_IPMI,
224 return (ipmi_attach(dev));
228 opal_ipmi_detach(device_t dev)
230 struct opal_ipmi_softc *sc;
233 sc = device_get_softc(dev);
234 err = ipmi_detach(dev);
236 free(sc->sc_msg, M_IPMI);
241 static device_method_t opal_ipmi_methods[] = {
242 /* Device interface */
243 DEVMETHOD(device_probe, opal_ipmi_probe),
244 DEVMETHOD(device_attach, opal_ipmi_attach),
245 DEVMETHOD(device_detach, opal_ipmi_detach),
249 static driver_t opal_ipmi_driver = {
252 sizeof(struct opal_ipmi_softc)
255 DRIVER_MODULE(opal_ipmi, opal, opal_ipmi_driver, ipmi_devclass, NULL, NULL);