ipmi.c revision 1.5 1 1.5 nonaka /* $NetBSD: ipmi.c,v 1.5 2020/08/17 08:34:36 nonaka Exp $ */
2 1.1 mlelstv
3 1.1 mlelstv /*
4 1.4 mlelstv * Copyright (c) 2019 Michael van Elst
5 1.4 mlelstv *
6 1.4 mlelstv * Redistribution and use in source and binary forms, with or without
7 1.4 mlelstv * modification, are permitted provided that the following conditions
8 1.4 mlelstv * are met:
9 1.4 mlelstv * 1. Redistributions of source code must retain the above copyright
10 1.4 mlelstv * notice, this list of conditions and the following disclaimer.
11 1.4 mlelstv * 2. Redistributions in binary form must reproduce the above copyright
12 1.4 mlelstv * notice, this list of conditions and the following disclaimer in the
13 1.4 mlelstv * documentation and/or other materials provided with the distribution.
14 1.4 mlelstv *
15 1.4 mlelstv * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.4 mlelstv * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 1.4 mlelstv * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 1.4 mlelstv * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 1.4 mlelstv * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 1.4 mlelstv * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 1.4 mlelstv * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 1.4 mlelstv * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 1.4 mlelstv * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 1.4 mlelstv * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 1.4 mlelstv *
26 1.4 mlelstv */
27 1.4 mlelstv /*
28 1.1 mlelstv * Copyright (c) 2006 Manuel Bouyer.
29 1.1 mlelstv *
30 1.1 mlelstv * Redistribution and use in source and binary forms, with or without
31 1.1 mlelstv * modification, are permitted provided that the following conditions
32 1.1 mlelstv * are met:
33 1.1 mlelstv * 1. Redistributions of source code must retain the above copyright
34 1.1 mlelstv * notice, this list of conditions and the following disclaimer.
35 1.1 mlelstv * 2. Redistributions in binary form must reproduce the above copyright
36 1.1 mlelstv * notice, this list of conditions and the following disclaimer in the
37 1.1 mlelstv * documentation and/or other materials provided with the distribution.
38 1.1 mlelstv *
39 1.1 mlelstv * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
40 1.1 mlelstv * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41 1.1 mlelstv * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
42 1.1 mlelstv * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
43 1.1 mlelstv * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44 1.1 mlelstv * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
45 1.1 mlelstv * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
46 1.1 mlelstv * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
47 1.1 mlelstv * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
48 1.1 mlelstv * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
49 1.1 mlelstv *
50 1.1 mlelstv */
51 1.1 mlelstv
52 1.1 mlelstv /*
53 1.1 mlelstv * Copyright (c) 2005 Jordan Hargrave
54 1.1 mlelstv * All rights reserved.
55 1.1 mlelstv *
56 1.1 mlelstv * Redistribution and use in source and binary forms, with or without
57 1.1 mlelstv * modification, are permitted provided that the following conditions
58 1.1 mlelstv * are met:
59 1.1 mlelstv * 1. Redistributions of source code must retain the above copyright
60 1.1 mlelstv * notice, this list of conditions and the following disclaimer.
61 1.1 mlelstv * 2. Redistributions in binary form must reproduce the above copyright
62 1.1 mlelstv * notice, this list of conditions and the following disclaimer in the
63 1.1 mlelstv * documentation and/or other materials provided with the distribution.
64 1.1 mlelstv *
65 1.1 mlelstv * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
66 1.1 mlelstv * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
67 1.1 mlelstv * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
68 1.1 mlelstv * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
69 1.1 mlelstv * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
70 1.1 mlelstv * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
71 1.1 mlelstv * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
72 1.1 mlelstv * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
73 1.1 mlelstv * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
74 1.1 mlelstv * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
75 1.1 mlelstv * SUCH DAMAGE.
76 1.1 mlelstv */
77 1.1 mlelstv
78 1.1 mlelstv #include <sys/cdefs.h>
79 1.5 nonaka __KERNEL_RCSID(0, "$NetBSD: ipmi.c,v 1.5 2020/08/17 08:34:36 nonaka Exp $");
80 1.1 mlelstv
81 1.1 mlelstv #include <sys/types.h>
82 1.1 mlelstv #include <sys/param.h>
83 1.1 mlelstv #include <sys/systm.h>
84 1.1 mlelstv #include <sys/kernel.h>
85 1.1 mlelstv #include <sys/device.h>
86 1.1 mlelstv #include <sys/extent.h>
87 1.1 mlelstv #include <sys/callout.h>
88 1.1 mlelstv #include <sys/envsys.h>
89 1.1 mlelstv #include <sys/malloc.h>
90 1.1 mlelstv #include <sys/kthread.h>
91 1.1 mlelstv #include <sys/bus.h>
92 1.1 mlelstv #include <sys/intr.h>
93 1.4 mlelstv #include <sys/ioctl.h>
94 1.4 mlelstv #include <sys/poll.h>
95 1.4 mlelstv #include <sys/conf.h>
96 1.1 mlelstv
97 1.1 mlelstv #include <dev/isa/isareg.h>
98 1.1 mlelstv #include <dev/isa/isavar.h>
99 1.1 mlelstv
100 1.4 mlelstv #include <sys/ipmi.h>
101 1.1 mlelstv #include <dev/ipmivar.h>
102 1.1 mlelstv
103 1.1 mlelstv #include <uvm/uvm_extern.h>
104 1.1 mlelstv
105 1.4 mlelstv #include "ioconf.h"
106 1.4 mlelstv
107 1.4 mlelstv static dev_type_open(ipmi_open);
108 1.4 mlelstv static dev_type_close(ipmi_close);
109 1.4 mlelstv static dev_type_ioctl(ipmi_ioctl);
110 1.4 mlelstv static dev_type_poll(ipmi_poll);
111 1.4 mlelstv
112 1.4 mlelstv const struct cdevsw ipmi_cdevsw = {
113 1.4 mlelstv .d_open = ipmi_open,
114 1.4 mlelstv .d_close = ipmi_close,
115 1.4 mlelstv .d_read = noread,
116 1.4 mlelstv .d_write = nowrite,
117 1.4 mlelstv .d_ioctl = ipmi_ioctl,
118 1.4 mlelstv .d_stop = nostop,
119 1.4 mlelstv .d_tty = notty,
120 1.4 mlelstv .d_poll = ipmi_poll,
121 1.4 mlelstv .d_mmap = nommap,
122 1.4 mlelstv .d_kqfilter = nokqfilter,
123 1.4 mlelstv .d_discard = nodiscard,
124 1.4 mlelstv .d_flag = D_OTHER
125 1.4 mlelstv };
126 1.4 mlelstv
127 1.4 mlelstv #define IPMIUNIT(n) (minor(n))
128 1.4 mlelstv
129 1.1 mlelstv struct ipmi_sensor {
130 1.1 mlelstv uint8_t *i_sdr;
131 1.1 mlelstv int i_num;
132 1.1 mlelstv int i_stype;
133 1.1 mlelstv int i_etype;
134 1.1 mlelstv char i_envdesc[64];
135 1.1 mlelstv int i_envtype; /* envsys compatible type */
136 1.1 mlelstv int i_envnum; /* envsys index */
137 1.1 mlelstv sysmon_envsys_lim_t i_limits, i_deflims;
138 1.1 mlelstv uint32_t i_props, i_defprops;
139 1.1 mlelstv SLIST_ENTRY(ipmi_sensor) i_list;
140 1.1 mlelstv int32_t i_prevval; /* feed rnd source on change */
141 1.1 mlelstv };
142 1.1 mlelstv
143 1.1 mlelstv #if 0
144 1.1 mlelstv static int ipmi_nintr;
145 1.1 mlelstv #endif
146 1.1 mlelstv static int ipmi_dbg = 0;
147 1.1 mlelstv static int ipmi_enabled = 0;
148 1.1 mlelstv
149 1.1 mlelstv #define SENSOR_REFRESH_RATE (hz / 2)
150 1.1 mlelstv
151 1.1 mlelstv #define IPMI_BTMSG_LEN 0
152 1.1 mlelstv #define IPMI_BTMSG_NFLN 1
153 1.1 mlelstv #define IPMI_BTMSG_SEQ 2
154 1.1 mlelstv #define IPMI_BTMSG_CMD 3
155 1.1 mlelstv #define IPMI_BTMSG_CCODE 4
156 1.1 mlelstv #define IPMI_BTMSG_DATASND 4
157 1.1 mlelstv #define IPMI_BTMSG_DATARCV 5
158 1.1 mlelstv
159 1.1 mlelstv #define IPMI_MSG_NFLN 0
160 1.1 mlelstv #define IPMI_MSG_CMD 1
161 1.1 mlelstv #define IPMI_MSG_CCODE 2
162 1.1 mlelstv #define IPMI_MSG_DATASND 2
163 1.1 mlelstv #define IPMI_MSG_DATARCV 3
164 1.1 mlelstv
165 1.1 mlelstv #define IPMI_SENSOR_TYPE_TEMP 0x0101
166 1.1 mlelstv #define IPMI_SENSOR_TYPE_VOLT 0x0102
167 1.1 mlelstv #define IPMI_SENSOR_TYPE_FAN 0x0104
168 1.1 mlelstv #define IPMI_SENSOR_TYPE_INTRUSION 0x6F05
169 1.1 mlelstv #define IPMI_SENSOR_TYPE_PWRSUPPLY 0x6F08
170 1.1 mlelstv
171 1.1 mlelstv #define IPMI_NAME_UNICODE 0x00
172 1.1 mlelstv #define IPMI_NAME_BCDPLUS 0x01
173 1.1 mlelstv #define IPMI_NAME_ASCII6BIT 0x02
174 1.1 mlelstv #define IPMI_NAME_ASCII8BIT 0x03
175 1.1 mlelstv
176 1.1 mlelstv #define IPMI_ENTITY_PWRSUPPLY 0x0A
177 1.1 mlelstv
178 1.1 mlelstv #define IPMI_SENSOR_SCANNING_ENABLED (1L << 6)
179 1.1 mlelstv #define IPMI_SENSOR_UNAVAILABLE (1L << 5)
180 1.1 mlelstv #define IPMI_INVALID_SENSOR_P(x) \
181 1.1 mlelstv (((x) & (IPMI_SENSOR_SCANNING_ENABLED|IPMI_SENSOR_UNAVAILABLE)) \
182 1.1 mlelstv != IPMI_SENSOR_SCANNING_ENABLED)
183 1.1 mlelstv
184 1.1 mlelstv #define IPMI_SDR_TYPEFULL 1
185 1.1 mlelstv #define IPMI_SDR_TYPECOMPACT 2
186 1.1 mlelstv
187 1.1 mlelstv #define byteof(x) ((x) >> 3)
188 1.1 mlelstv #define bitof(x) (1L << ((x) & 0x7))
189 1.1 mlelstv #define TB(b,m) (data[2+byteof(b)] & bitof(b))
190 1.1 mlelstv
191 1.1 mlelstv #define dbg_printf(lvl, fmt...) \
192 1.1 mlelstv if (ipmi_dbg >= lvl) \
193 1.1 mlelstv printf(fmt);
194 1.1 mlelstv #define dbg_dump(lvl, msg, len, buf) \
195 1.1 mlelstv if (len && ipmi_dbg >= lvl) \
196 1.1 mlelstv dumpb(msg, len, (const uint8_t *)(buf));
197 1.1 mlelstv
198 1.1 mlelstv static long signextend(unsigned long, int);
199 1.1 mlelstv
200 1.1 mlelstv SLIST_HEAD(ipmi_sensors_head, ipmi_sensor);
201 1.1 mlelstv static struct ipmi_sensors_head ipmi_sensor_list =
202 1.1 mlelstv SLIST_HEAD_INITIALIZER(&ipmi_sensor_list);
203 1.1 mlelstv
204 1.1 mlelstv static void dumpb(const char *, int, const uint8_t *);
205 1.1 mlelstv
206 1.1 mlelstv static int read_sensor(struct ipmi_softc *, struct ipmi_sensor *);
207 1.1 mlelstv static int add_sdr_sensor(struct ipmi_softc *, uint8_t *);
208 1.1 mlelstv static int get_sdr_partial(struct ipmi_softc *, uint16_t, uint16_t,
209 1.1 mlelstv uint8_t, uint8_t, void *, uint16_t *);
210 1.1 mlelstv static int get_sdr(struct ipmi_softc *, uint16_t, uint16_t *);
211 1.1 mlelstv
212 1.1 mlelstv static char *ipmi_buf_acquire(struct ipmi_softc *, size_t);
213 1.1 mlelstv static void ipmi_buf_release(struct ipmi_softc *, char *);
214 1.1 mlelstv static int ipmi_sendcmd(struct ipmi_softc *, int, int, int, int, int, const void*);
215 1.1 mlelstv static int ipmi_recvcmd(struct ipmi_softc *, int, int *, void *);
216 1.1 mlelstv static void ipmi_delay(struct ipmi_softc *, int);
217 1.1 mlelstv
218 1.3 mlelstv static int ipmi_get_device_id(struct ipmi_softc *, struct ipmi_device_id *);
219 1.1 mlelstv static int ipmi_watchdog_setmode(struct sysmon_wdog *);
220 1.1 mlelstv static int ipmi_watchdog_tickle(struct sysmon_wdog *);
221 1.1 mlelstv static void ipmi_dotickle(struct ipmi_softc *);
222 1.1 mlelstv
223 1.1 mlelstv #if 0
224 1.1 mlelstv static int ipmi_intr(void *);
225 1.1 mlelstv #endif
226 1.1 mlelstv
227 1.1 mlelstv static int ipmi_match(device_t, cfdata_t, void *);
228 1.1 mlelstv static void ipmi_attach(device_t, device_t, void *);
229 1.1 mlelstv static int ipmi_detach(device_t, int);
230 1.1 mlelstv
231 1.1 mlelstv static long ipmi_convert(uint8_t, struct sdrtype1 *, long);
232 1.1 mlelstv static void ipmi_sensor_name(char *, int, uint8_t, uint8_t *);
233 1.1 mlelstv
234 1.1 mlelstv /* BMC Helper Functions */
235 1.1 mlelstv static uint8_t bmc_read(struct ipmi_softc *, int);
236 1.1 mlelstv static void bmc_write(struct ipmi_softc *, int, uint8_t);
237 1.1 mlelstv static int bmc_io_wait(struct ipmi_softc *, int, uint8_t, uint8_t, const char *);
238 1.1 mlelstv static int bmc_io_wait_spin(struct ipmi_softc *, int, uint8_t, uint8_t);
239 1.1 mlelstv static int bmc_io_wait_sleep(struct ipmi_softc *, int, uint8_t, uint8_t);
240 1.1 mlelstv
241 1.1 mlelstv static void *bt_buildmsg(struct ipmi_softc *, int, int, int, const void *, int *);
242 1.1 mlelstv static void *cmn_buildmsg(struct ipmi_softc *, int, int, int, const void *, int *);
243 1.1 mlelstv
244 1.1 mlelstv static int getbits(uint8_t *, int, int);
245 1.1 mlelstv static int ipmi_sensor_type(int, int, int);
246 1.1 mlelstv
247 1.1 mlelstv static void ipmi_refresh_sensors(struct ipmi_softc *);
248 1.1 mlelstv static int ipmi_map_regs(struct ipmi_softc *, struct ipmi_attach_args *);
249 1.1 mlelstv static void ipmi_unmap_regs(struct ipmi_softc *);
250 1.1 mlelstv
251 1.1 mlelstv static int32_t ipmi_convert_sensor(uint8_t *, struct ipmi_sensor *);
252 1.1 mlelstv static void ipmi_set_limits(struct sysmon_envsys *, envsys_data_t *,
253 1.1 mlelstv sysmon_envsys_lim_t *, uint32_t *);
254 1.1 mlelstv static void ipmi_get_limits(struct sysmon_envsys *, envsys_data_t *,
255 1.1 mlelstv sysmon_envsys_lim_t *, uint32_t *);
256 1.1 mlelstv static void ipmi_get_sensor_limits(struct ipmi_softc *, struct ipmi_sensor *,
257 1.1 mlelstv sysmon_envsys_lim_t *, uint32_t *);
258 1.1 mlelstv static int ipmi_sensor_status(struct ipmi_softc *, struct ipmi_sensor *,
259 1.1 mlelstv envsys_data_t *, uint8_t *);
260 1.1 mlelstv
261 1.1 mlelstv static int add_child_sensors(struct ipmi_softc *, uint8_t *, int, int, int,
262 1.1 mlelstv int, int, int, const char *);
263 1.1 mlelstv
264 1.1 mlelstv static bool ipmi_suspend(device_t, const pmf_qual_t *);
265 1.1 mlelstv
266 1.1 mlelstv static int kcs_probe(struct ipmi_softc *);
267 1.1 mlelstv static int kcs_reset(struct ipmi_softc *);
268 1.1 mlelstv static int kcs_sendmsg(struct ipmi_softc *, int, const uint8_t *);
269 1.1 mlelstv static int kcs_recvmsg(struct ipmi_softc *, int, int *len, uint8_t *);
270 1.1 mlelstv
271 1.1 mlelstv static int bt_probe(struct ipmi_softc *);
272 1.1 mlelstv static int bt_reset(struct ipmi_softc *);
273 1.1 mlelstv static int bt_sendmsg(struct ipmi_softc *, int, const uint8_t *);
274 1.1 mlelstv static int bt_recvmsg(struct ipmi_softc *, int, int *, uint8_t *);
275 1.1 mlelstv
276 1.1 mlelstv static int smic_probe(struct ipmi_softc *);
277 1.1 mlelstv static int smic_reset(struct ipmi_softc *);
278 1.1 mlelstv static int smic_sendmsg(struct ipmi_softc *, int, const uint8_t *);
279 1.1 mlelstv static int smic_recvmsg(struct ipmi_softc *, int, int *, uint8_t *);
280 1.1 mlelstv
281 1.1 mlelstv static struct ipmi_if kcs_if = {
282 1.1 mlelstv "KCS",
283 1.1 mlelstv IPMI_IF_KCS_NREGS,
284 1.1 mlelstv cmn_buildmsg,
285 1.1 mlelstv kcs_sendmsg,
286 1.1 mlelstv kcs_recvmsg,
287 1.1 mlelstv kcs_reset,
288 1.1 mlelstv kcs_probe,
289 1.1 mlelstv };
290 1.1 mlelstv
291 1.1 mlelstv static struct ipmi_if smic_if = {
292 1.1 mlelstv "SMIC",
293 1.1 mlelstv IPMI_IF_SMIC_NREGS,
294 1.1 mlelstv cmn_buildmsg,
295 1.1 mlelstv smic_sendmsg,
296 1.1 mlelstv smic_recvmsg,
297 1.1 mlelstv smic_reset,
298 1.1 mlelstv smic_probe,
299 1.1 mlelstv };
300 1.1 mlelstv
301 1.1 mlelstv static struct ipmi_if bt_if = {
302 1.1 mlelstv "BT",
303 1.1 mlelstv IPMI_IF_BT_NREGS,
304 1.1 mlelstv bt_buildmsg,
305 1.1 mlelstv bt_sendmsg,
306 1.1 mlelstv bt_recvmsg,
307 1.1 mlelstv bt_reset,
308 1.1 mlelstv bt_probe,
309 1.1 mlelstv };
310 1.1 mlelstv
311 1.1 mlelstv static struct ipmi_if *ipmi_get_if(int);
312 1.1 mlelstv
313 1.1 mlelstv static struct ipmi_if *
314 1.1 mlelstv ipmi_get_if(int iftype)
315 1.1 mlelstv {
316 1.1 mlelstv switch (iftype) {
317 1.1 mlelstv case IPMI_IF_KCS:
318 1.1 mlelstv return &kcs_if;
319 1.1 mlelstv case IPMI_IF_SMIC:
320 1.1 mlelstv return &smic_if;
321 1.1 mlelstv case IPMI_IF_BT:
322 1.1 mlelstv return &bt_if;
323 1.1 mlelstv default:
324 1.1 mlelstv return NULL;
325 1.1 mlelstv }
326 1.1 mlelstv }
327 1.1 mlelstv
328 1.1 mlelstv /*
329 1.1 mlelstv * BMC Helper Functions
330 1.1 mlelstv */
331 1.1 mlelstv static uint8_t
332 1.1 mlelstv bmc_read(struct ipmi_softc *sc, int offset)
333 1.1 mlelstv {
334 1.1 mlelstv return bus_space_read_1(sc->sc_iot, sc->sc_ioh,
335 1.1 mlelstv offset * sc->sc_if_iospacing);
336 1.1 mlelstv }
337 1.1 mlelstv
338 1.1 mlelstv static void
339 1.1 mlelstv bmc_write(struct ipmi_softc *sc, int offset, uint8_t val)
340 1.1 mlelstv {
341 1.1 mlelstv bus_space_write_1(sc->sc_iot, sc->sc_ioh,
342 1.1 mlelstv offset * sc->sc_if_iospacing, val);
343 1.1 mlelstv }
344 1.1 mlelstv
345 1.1 mlelstv static int
346 1.1 mlelstv bmc_io_wait_sleep(struct ipmi_softc *sc, int offset, uint8_t mask,
347 1.1 mlelstv uint8_t value)
348 1.1 mlelstv {
349 1.1 mlelstv int retries;
350 1.1 mlelstv uint8_t v;
351 1.1 mlelstv
352 1.1 mlelstv KASSERT(mutex_owned(&sc->sc_cmd_mtx));
353 1.1 mlelstv
354 1.1 mlelstv for (retries = 0; retries < sc->sc_max_retries; retries++) {
355 1.1 mlelstv v = bmc_read(sc, offset);
356 1.1 mlelstv if ((v & mask) == value)
357 1.1 mlelstv return v;
358 1.1 mlelstv mutex_enter(&sc->sc_sleep_mtx);
359 1.1 mlelstv cv_timedwait(&sc->sc_cmd_sleep, &sc->sc_sleep_mtx, 1);
360 1.1 mlelstv mutex_exit(&sc->sc_sleep_mtx);
361 1.1 mlelstv }
362 1.1 mlelstv return -1;
363 1.1 mlelstv }
364 1.1 mlelstv
365 1.1 mlelstv static int
366 1.1 mlelstv bmc_io_wait(struct ipmi_softc *sc, int offset, uint8_t mask, uint8_t value,
367 1.1 mlelstv const char *lbl)
368 1.1 mlelstv {
369 1.1 mlelstv int v;
370 1.1 mlelstv
371 1.1 mlelstv v = bmc_io_wait_spin(sc, offset, mask, value);
372 1.1 mlelstv if (cold || v != -1)
373 1.1 mlelstv return v;
374 1.1 mlelstv
375 1.1 mlelstv return bmc_io_wait_sleep(sc, offset, mask, value);
376 1.1 mlelstv }
377 1.1 mlelstv
378 1.1 mlelstv static int
379 1.1 mlelstv bmc_io_wait_spin(struct ipmi_softc *sc, int offset, uint8_t mask,
380 1.1 mlelstv uint8_t value)
381 1.1 mlelstv {
382 1.1 mlelstv uint8_t v;
383 1.1 mlelstv int count = cold ? 15000 : 500;
384 1.1 mlelstv /* ~us */
385 1.1 mlelstv
386 1.1 mlelstv while (count--) {
387 1.1 mlelstv v = bmc_read(sc, offset);
388 1.1 mlelstv if ((v & mask) == value)
389 1.1 mlelstv return v;
390 1.1 mlelstv
391 1.1 mlelstv delay(1);
392 1.1 mlelstv }
393 1.1 mlelstv
394 1.1 mlelstv return -1;
395 1.1 mlelstv
396 1.1 mlelstv }
397 1.1 mlelstv
398 1.1 mlelstv #define NETFN_LUN(nf,ln) (((nf) << 2) | ((ln) & 0x3))
399 1.4 mlelstv #define GET_NETFN(m) (((m) >> 2)
400 1.4 mlelstv #define GET_LUN(m) ((m) & 0x03)
401 1.1 mlelstv
402 1.1 mlelstv /*
403 1.1 mlelstv * BT interface
404 1.1 mlelstv */
405 1.1 mlelstv #define _BT_CTRL_REG 0
406 1.1 mlelstv #define BT_CLR_WR_PTR (1L << 0)
407 1.1 mlelstv #define BT_CLR_RD_PTR (1L << 1)
408 1.1 mlelstv #define BT_HOST2BMC_ATN (1L << 2)
409 1.1 mlelstv #define BT_BMC2HOST_ATN (1L << 3)
410 1.1 mlelstv #define BT_EVT_ATN (1L << 4)
411 1.1 mlelstv #define BT_HOST_BUSY (1L << 6)
412 1.1 mlelstv #define BT_BMC_BUSY (1L << 7)
413 1.1 mlelstv
414 1.1 mlelstv #define BT_READY (BT_HOST_BUSY|BT_HOST2BMC_ATN|BT_BMC2HOST_ATN)
415 1.1 mlelstv
416 1.1 mlelstv #define _BT_DATAIN_REG 1
417 1.1 mlelstv #define _BT_DATAOUT_REG 1
418 1.1 mlelstv
419 1.1 mlelstv #define _BT_INTMASK_REG 2
420 1.1 mlelstv #define BT_IM_HIRQ_PEND (1L << 1)
421 1.1 mlelstv #define BT_IM_SCI_EN (1L << 2)
422 1.1 mlelstv #define BT_IM_SMI_EN (1L << 3)
423 1.1 mlelstv #define BT_IM_NMI2SMI (1L << 4)
424 1.1 mlelstv
425 1.1 mlelstv static int bt_read(struct ipmi_softc *, int);
426 1.1 mlelstv static int bt_write(struct ipmi_softc *, int, uint8_t);
427 1.1 mlelstv
428 1.1 mlelstv static int
429 1.1 mlelstv bt_read(struct ipmi_softc *sc, int reg)
430 1.1 mlelstv {
431 1.1 mlelstv return bmc_read(sc, reg);
432 1.1 mlelstv }
433 1.1 mlelstv
434 1.1 mlelstv static int
435 1.1 mlelstv bt_write(struct ipmi_softc *sc, int reg, uint8_t data)
436 1.1 mlelstv {
437 1.1 mlelstv if (bmc_io_wait(sc, _BT_CTRL_REG, BT_BMC_BUSY, 0, __func__) < 0)
438 1.1 mlelstv return -1;
439 1.1 mlelstv
440 1.1 mlelstv bmc_write(sc, reg, data);
441 1.1 mlelstv return 0;
442 1.1 mlelstv }
443 1.1 mlelstv
444 1.1 mlelstv static int
445 1.1 mlelstv bt_sendmsg(struct ipmi_softc *sc, int len, const uint8_t *data)
446 1.1 mlelstv {
447 1.1 mlelstv int i;
448 1.1 mlelstv
449 1.1 mlelstv bt_write(sc, _BT_CTRL_REG, BT_CLR_WR_PTR);
450 1.1 mlelstv for (i = 0; i < len; i++)
451 1.1 mlelstv bt_write(sc, _BT_DATAOUT_REG, data[i]);
452 1.1 mlelstv
453 1.1 mlelstv bt_write(sc, _BT_CTRL_REG, BT_HOST2BMC_ATN);
454 1.1 mlelstv if (bmc_io_wait(sc, _BT_CTRL_REG, BT_HOST2BMC_ATN | BT_BMC_BUSY, 0,
455 1.1 mlelstv __func__) < 0)
456 1.1 mlelstv return -1;
457 1.1 mlelstv
458 1.1 mlelstv return 0;
459 1.1 mlelstv }
460 1.1 mlelstv
461 1.1 mlelstv static int
462 1.1 mlelstv bt_recvmsg(struct ipmi_softc *sc, int maxlen, int *rxlen, uint8_t *data)
463 1.1 mlelstv {
464 1.1 mlelstv uint8_t len, v, i;
465 1.1 mlelstv
466 1.1 mlelstv if (bmc_io_wait(sc, _BT_CTRL_REG, BT_BMC2HOST_ATN, BT_BMC2HOST_ATN,
467 1.1 mlelstv __func__) < 0)
468 1.1 mlelstv return -1;
469 1.1 mlelstv
470 1.1 mlelstv bt_write(sc, _BT_CTRL_REG, BT_HOST_BUSY);
471 1.1 mlelstv bt_write(sc, _BT_CTRL_REG, BT_BMC2HOST_ATN);
472 1.1 mlelstv bt_write(sc, _BT_CTRL_REG, BT_CLR_RD_PTR);
473 1.1 mlelstv len = bt_read(sc, _BT_DATAIN_REG);
474 1.1 mlelstv for (i = IPMI_BTMSG_NFLN; i <= len; i++) {
475 1.1 mlelstv v = bt_read(sc, _BT_DATAIN_REG);
476 1.1 mlelstv if (i != IPMI_BTMSG_SEQ)
477 1.1 mlelstv *(data++) = v;
478 1.1 mlelstv }
479 1.1 mlelstv bt_write(sc, _BT_CTRL_REG, BT_HOST_BUSY);
480 1.1 mlelstv *rxlen = len - 1;
481 1.1 mlelstv
482 1.1 mlelstv return 0;
483 1.1 mlelstv }
484 1.1 mlelstv
485 1.1 mlelstv static int
486 1.1 mlelstv bt_reset(struct ipmi_softc *sc)
487 1.1 mlelstv {
488 1.1 mlelstv return -1;
489 1.1 mlelstv }
490 1.1 mlelstv
491 1.1 mlelstv static int
492 1.1 mlelstv bt_probe(struct ipmi_softc *sc)
493 1.1 mlelstv {
494 1.1 mlelstv uint8_t rv;
495 1.1 mlelstv
496 1.1 mlelstv rv = bmc_read(sc, _BT_CTRL_REG);
497 1.1 mlelstv rv &= BT_HOST_BUSY;
498 1.1 mlelstv rv |= BT_CLR_WR_PTR|BT_CLR_RD_PTR|BT_BMC2HOST_ATN|BT_HOST2BMC_ATN;
499 1.1 mlelstv bmc_write(sc, _BT_CTRL_REG, rv);
500 1.1 mlelstv
501 1.1 mlelstv rv = bmc_read(sc, _BT_INTMASK_REG);
502 1.1 mlelstv rv &= BT_IM_SCI_EN|BT_IM_SMI_EN|BT_IM_NMI2SMI;
503 1.1 mlelstv rv |= BT_IM_HIRQ_PEND;
504 1.1 mlelstv bmc_write(sc, _BT_INTMASK_REG, rv);
505 1.1 mlelstv
506 1.1 mlelstv #if 0
507 1.1 mlelstv printf("%s: %2x\n", __func__, v);
508 1.1 mlelstv printf(" WR : %2x\n", v & BT_CLR_WR_PTR);
509 1.1 mlelstv printf(" RD : %2x\n", v & BT_CLR_RD_PTR);
510 1.1 mlelstv printf(" H2B : %2x\n", v & BT_HOST2BMC_ATN);
511 1.1 mlelstv printf(" B2H : %2x\n", v & BT_BMC2HOST_ATN);
512 1.1 mlelstv printf(" EVT : %2x\n", v & BT_EVT_ATN);
513 1.1 mlelstv printf(" HBSY : %2x\n", v & BT_HOST_BUSY);
514 1.1 mlelstv printf(" BBSY : %2x\n", v & BT_BMC_BUSY);
515 1.1 mlelstv #endif
516 1.1 mlelstv return 0;
517 1.1 mlelstv }
518 1.1 mlelstv
519 1.1 mlelstv /*
520 1.1 mlelstv * SMIC interface
521 1.1 mlelstv */
522 1.1 mlelstv #define _SMIC_DATAIN_REG 0
523 1.1 mlelstv #define _SMIC_DATAOUT_REG 0
524 1.1 mlelstv
525 1.1 mlelstv #define _SMIC_CTRL_REG 1
526 1.1 mlelstv #define SMS_CC_GET_STATUS 0x40
527 1.1 mlelstv #define SMS_CC_START_TRANSFER 0x41
528 1.1 mlelstv #define SMS_CC_NEXT_TRANSFER 0x42
529 1.1 mlelstv #define SMS_CC_END_TRANSFER 0x43
530 1.1 mlelstv #define SMS_CC_START_RECEIVE 0x44
531 1.1 mlelstv #define SMS_CC_NEXT_RECEIVE 0x45
532 1.1 mlelstv #define SMS_CC_END_RECEIVE 0x46
533 1.1 mlelstv #define SMS_CC_TRANSFER_ABORT 0x47
534 1.1 mlelstv
535 1.1 mlelstv #define SMS_SC_READY 0xc0
536 1.1 mlelstv #define SMS_SC_WRITE_START 0xc1
537 1.1 mlelstv #define SMS_SC_WRITE_NEXT 0xc2
538 1.1 mlelstv #define SMS_SC_WRITE_END 0xc3
539 1.1 mlelstv #define SMS_SC_READ_START 0xc4
540 1.1 mlelstv #define SMS_SC_READ_NEXT 0xc5
541 1.1 mlelstv #define SMS_SC_READ_END 0xc6
542 1.1 mlelstv
543 1.1 mlelstv #define _SMIC_FLAG_REG 2
544 1.1 mlelstv #define SMIC_BUSY (1L << 0)
545 1.1 mlelstv #define SMIC_SMS_ATN (1L << 2)
546 1.1 mlelstv #define SMIC_EVT_ATN (1L << 3)
547 1.1 mlelstv #define SMIC_SMI (1L << 4)
548 1.1 mlelstv #define SMIC_TX_DATA_RDY (1L << 6)
549 1.1 mlelstv #define SMIC_RX_DATA_RDY (1L << 7)
550 1.1 mlelstv
551 1.1 mlelstv static int smic_wait(struct ipmi_softc *, uint8_t, uint8_t, const char *);
552 1.1 mlelstv static int smic_write_cmd_data(struct ipmi_softc *, uint8_t, const uint8_t *);
553 1.1 mlelstv static int smic_read_data(struct ipmi_softc *, uint8_t *);
554 1.1 mlelstv
555 1.1 mlelstv static int
556 1.1 mlelstv smic_wait(struct ipmi_softc *sc, uint8_t mask, uint8_t val, const char *lbl)
557 1.1 mlelstv {
558 1.1 mlelstv int v;
559 1.1 mlelstv
560 1.1 mlelstv /* Wait for expected flag bits */
561 1.1 mlelstv v = bmc_io_wait(sc, _SMIC_FLAG_REG, mask, val, __func__);
562 1.1 mlelstv if (v < 0)
563 1.1 mlelstv return -1;
564 1.1 mlelstv
565 1.1 mlelstv /* Return current status */
566 1.1 mlelstv v = bmc_read(sc, _SMIC_CTRL_REG);
567 1.1 mlelstv dbg_printf(99, "%s(%s) = %#.2x\n", __func__, lbl, v);
568 1.1 mlelstv return v;
569 1.1 mlelstv }
570 1.1 mlelstv
571 1.1 mlelstv static int
572 1.1 mlelstv smic_write_cmd_data(struct ipmi_softc *sc, uint8_t cmd, const uint8_t *data)
573 1.1 mlelstv {
574 1.1 mlelstv int sts, v;
575 1.1 mlelstv
576 1.1 mlelstv dbg_printf(50, "%s: %#.2x %#.2x\n", __func__, cmd, data ? *data : -1);
577 1.1 mlelstv sts = smic_wait(sc, SMIC_TX_DATA_RDY | SMIC_BUSY, SMIC_TX_DATA_RDY,
578 1.1 mlelstv "smic_write_cmd_data ready");
579 1.1 mlelstv if (sts < 0)
580 1.1 mlelstv return sts;
581 1.1 mlelstv
582 1.1 mlelstv bmc_write(sc, _SMIC_CTRL_REG, cmd);
583 1.1 mlelstv if (data)
584 1.1 mlelstv bmc_write(sc, _SMIC_DATAOUT_REG, *data);
585 1.1 mlelstv
586 1.1 mlelstv /* Toggle BUSY bit, then wait for busy bit to clear */
587 1.1 mlelstv v = bmc_read(sc, _SMIC_FLAG_REG);
588 1.1 mlelstv bmc_write(sc, _SMIC_FLAG_REG, v | SMIC_BUSY);
589 1.1 mlelstv
590 1.1 mlelstv return smic_wait(sc, SMIC_BUSY, 0, __func__);
591 1.1 mlelstv }
592 1.1 mlelstv
593 1.1 mlelstv static int
594 1.1 mlelstv smic_read_data(struct ipmi_softc *sc, uint8_t *data)
595 1.1 mlelstv {
596 1.1 mlelstv int sts;
597 1.1 mlelstv
598 1.1 mlelstv sts = smic_wait(sc, SMIC_RX_DATA_RDY | SMIC_BUSY, SMIC_RX_DATA_RDY,
599 1.1 mlelstv __func__);
600 1.1 mlelstv if (sts >= 0) {
601 1.1 mlelstv *data = bmc_read(sc, _SMIC_DATAIN_REG);
602 1.1 mlelstv dbg_printf(50, "%s: %#.2x\n", __func__, *data);
603 1.1 mlelstv }
604 1.1 mlelstv return sts;
605 1.1 mlelstv }
606 1.1 mlelstv
607 1.1 mlelstv #define ErrStat(a, ...) if (a) printf(__VA_ARGS__);
608 1.1 mlelstv
609 1.1 mlelstv static int
610 1.1 mlelstv smic_sendmsg(struct ipmi_softc *sc, int len, const uint8_t *data)
611 1.1 mlelstv {
612 1.1 mlelstv int sts, idx;
613 1.1 mlelstv
614 1.1 mlelstv sts = smic_write_cmd_data(sc, SMS_CC_START_TRANSFER, &data[0]);
615 1.1 mlelstv ErrStat(sts != SMS_SC_WRITE_START, "%s: wstart", __func__);
616 1.1 mlelstv for (idx = 1; idx < len - 1; idx++) {
617 1.1 mlelstv sts = smic_write_cmd_data(sc, SMS_CC_NEXT_TRANSFER,
618 1.1 mlelstv &data[idx]);
619 1.1 mlelstv ErrStat(sts != SMS_SC_WRITE_NEXT, "%s: write", __func__);
620 1.1 mlelstv }
621 1.1 mlelstv sts = smic_write_cmd_data(sc, SMS_CC_END_TRANSFER, &data[idx]);
622 1.1 mlelstv if (sts != SMS_SC_WRITE_END) {
623 1.1 mlelstv dbg_printf(50, "%s: %d/%d = %#.2x\n", __func__, idx, len, sts);
624 1.1 mlelstv return -1;
625 1.1 mlelstv }
626 1.1 mlelstv
627 1.1 mlelstv return 0;
628 1.1 mlelstv }
629 1.1 mlelstv
630 1.1 mlelstv static int
631 1.1 mlelstv smic_recvmsg(struct ipmi_softc *sc, int maxlen, int *len, uint8_t *data)
632 1.1 mlelstv {
633 1.1 mlelstv int sts, idx;
634 1.1 mlelstv
635 1.1 mlelstv *len = 0;
636 1.1 mlelstv sts = smic_wait(sc, SMIC_RX_DATA_RDY, SMIC_RX_DATA_RDY, __func__);
637 1.1 mlelstv if (sts < 0)
638 1.1 mlelstv return -1;
639 1.1 mlelstv
640 1.1 mlelstv sts = smic_write_cmd_data(sc, SMS_CC_START_RECEIVE, NULL);
641 1.1 mlelstv ErrStat(sts != SMS_SC_READ_START, "%s: rstart", __func__);
642 1.1 mlelstv for (idx = 0;; ) {
643 1.1 mlelstv sts = smic_read_data(sc, &data[idx++]);
644 1.1 mlelstv if (sts != SMS_SC_READ_START && sts != SMS_SC_READ_NEXT)
645 1.1 mlelstv break;
646 1.1 mlelstv smic_write_cmd_data(sc, SMS_CC_NEXT_RECEIVE, NULL);
647 1.1 mlelstv }
648 1.1 mlelstv ErrStat(sts != SMS_SC_READ_END, "%s: rend", __func__);
649 1.1 mlelstv
650 1.1 mlelstv *len = idx;
651 1.1 mlelstv
652 1.1 mlelstv sts = smic_write_cmd_data(sc, SMS_CC_END_RECEIVE, NULL);
653 1.1 mlelstv if (sts != SMS_SC_READY) {
654 1.1 mlelstv dbg_printf(50, "%s: %d/%d = %#.2x\n",
655 1.1 mlelstv __func__, idx, maxlen, sts);
656 1.1 mlelstv return -1;
657 1.1 mlelstv }
658 1.1 mlelstv
659 1.1 mlelstv return 0;
660 1.1 mlelstv }
661 1.1 mlelstv
662 1.1 mlelstv static int
663 1.1 mlelstv smic_reset(struct ipmi_softc *sc)
664 1.1 mlelstv {
665 1.1 mlelstv return -1;
666 1.1 mlelstv }
667 1.1 mlelstv
668 1.1 mlelstv static int
669 1.1 mlelstv smic_probe(struct ipmi_softc *sc)
670 1.1 mlelstv {
671 1.1 mlelstv /* Flag register should not be 0xFF on a good system */
672 1.1 mlelstv if (bmc_read(sc, _SMIC_FLAG_REG) == 0xFF)
673 1.1 mlelstv return -1;
674 1.1 mlelstv
675 1.1 mlelstv return 0;
676 1.1 mlelstv }
677 1.1 mlelstv
678 1.1 mlelstv /*
679 1.1 mlelstv * KCS interface
680 1.1 mlelstv */
681 1.1 mlelstv #define _KCS_DATAIN_REGISTER 0
682 1.1 mlelstv #define _KCS_DATAOUT_REGISTER 0
683 1.1 mlelstv #define KCS_READ_NEXT 0x68
684 1.1 mlelstv
685 1.1 mlelstv #define _KCS_COMMAND_REGISTER 1
686 1.1 mlelstv #define KCS_GET_STATUS 0x60
687 1.1 mlelstv #define KCS_WRITE_START 0x61
688 1.1 mlelstv #define KCS_WRITE_END 0x62
689 1.1 mlelstv
690 1.1 mlelstv #define _KCS_STATUS_REGISTER 1
691 1.1 mlelstv #define KCS_OBF (1L << 0)
692 1.1 mlelstv #define KCS_IBF (1L << 1)
693 1.1 mlelstv #define KCS_SMS_ATN (1L << 2)
694 1.1 mlelstv #define KCS_CD (1L << 3)
695 1.1 mlelstv #define KCS_OEM1 (1L << 4)
696 1.1 mlelstv #define KCS_OEM2 (1L << 5)
697 1.1 mlelstv #define KCS_STATE_MASK 0xc0
698 1.1 mlelstv #define KCS_IDLE_STATE 0x00
699 1.1 mlelstv #define KCS_READ_STATE 0x40
700 1.1 mlelstv #define KCS_WRITE_STATE 0x80
701 1.1 mlelstv #define KCS_ERROR_STATE 0xC0
702 1.1 mlelstv
703 1.1 mlelstv static int kcs_wait(struct ipmi_softc *, uint8_t, uint8_t, const char *);
704 1.1 mlelstv static int kcs_write_cmd(struct ipmi_softc *, uint8_t);
705 1.1 mlelstv static int kcs_write_data(struct ipmi_softc *, uint8_t);
706 1.1 mlelstv static int kcs_read_data(struct ipmi_softc *, uint8_t *);
707 1.1 mlelstv
708 1.1 mlelstv static int
709 1.1 mlelstv kcs_wait(struct ipmi_softc *sc, uint8_t mask, uint8_t value, const char *lbl)
710 1.1 mlelstv {
711 1.1 mlelstv int v;
712 1.1 mlelstv
713 1.1 mlelstv v = bmc_io_wait(sc, _KCS_STATUS_REGISTER, mask, value, lbl);
714 1.1 mlelstv if (v < 0)
715 1.1 mlelstv return v;
716 1.1 mlelstv
717 1.1 mlelstv /* Check if output buffer full, read dummy byte */
718 1.1 mlelstv if ((v & (KCS_OBF | KCS_STATE_MASK)) == (KCS_OBF | KCS_WRITE_STATE))
719 1.1 mlelstv bmc_read(sc, _KCS_DATAIN_REGISTER);
720 1.1 mlelstv
721 1.1 mlelstv /* Check for error state */
722 1.1 mlelstv if ((v & KCS_STATE_MASK) == KCS_ERROR_STATE) {
723 1.1 mlelstv bmc_write(sc, _KCS_COMMAND_REGISTER, KCS_GET_STATUS);
724 1.1 mlelstv while (bmc_read(sc, _KCS_STATUS_REGISTER) & KCS_IBF)
725 1.1 mlelstv ;
726 1.1 mlelstv aprint_error_dev(sc->sc_dev, "error code: %#x\n",
727 1.1 mlelstv bmc_read(sc, _KCS_DATAIN_REGISTER));
728 1.1 mlelstv }
729 1.1 mlelstv
730 1.1 mlelstv return v & KCS_STATE_MASK;
731 1.1 mlelstv }
732 1.1 mlelstv
733 1.1 mlelstv static int
734 1.1 mlelstv kcs_write_cmd(struct ipmi_softc *sc, uint8_t cmd)
735 1.1 mlelstv {
736 1.1 mlelstv /* ASSERT: IBF and OBF are clear */
737 1.1 mlelstv dbg_printf(50, "%s: %#.2x\n", __func__, cmd);
738 1.1 mlelstv bmc_write(sc, _KCS_COMMAND_REGISTER, cmd);
739 1.1 mlelstv
740 1.1 mlelstv return kcs_wait(sc, KCS_IBF, 0, "write_cmd");
741 1.1 mlelstv }
742 1.1 mlelstv
743 1.1 mlelstv static int
744 1.1 mlelstv kcs_write_data(struct ipmi_softc *sc, uint8_t data)
745 1.1 mlelstv {
746 1.1 mlelstv /* ASSERT: IBF and OBF are clear */
747 1.1 mlelstv dbg_printf(50, "%s: %#.2x\n", __func__, data);
748 1.1 mlelstv bmc_write(sc, _KCS_DATAOUT_REGISTER, data);
749 1.1 mlelstv
750 1.1 mlelstv return kcs_wait(sc, KCS_IBF, 0, "write_data");
751 1.1 mlelstv }
752 1.1 mlelstv
753 1.1 mlelstv static int
754 1.1 mlelstv kcs_read_data(struct ipmi_softc *sc, uint8_t * data)
755 1.1 mlelstv {
756 1.1 mlelstv int sts;
757 1.1 mlelstv
758 1.1 mlelstv sts = kcs_wait(sc, KCS_IBF | KCS_OBF, KCS_OBF, __func__);
759 1.1 mlelstv if (sts != KCS_READ_STATE)
760 1.1 mlelstv return sts;
761 1.1 mlelstv
762 1.1 mlelstv /* ASSERT: OBF is set read data, request next byte */
763 1.1 mlelstv *data = bmc_read(sc, _KCS_DATAIN_REGISTER);
764 1.1 mlelstv bmc_write(sc, _KCS_DATAOUT_REGISTER, KCS_READ_NEXT);
765 1.1 mlelstv
766 1.1 mlelstv dbg_printf(50, "%s: %#.2x\n", __func__, *data);
767 1.1 mlelstv
768 1.1 mlelstv return sts;
769 1.1 mlelstv }
770 1.1 mlelstv
771 1.1 mlelstv /* Exported KCS functions */
772 1.1 mlelstv static int
773 1.1 mlelstv kcs_sendmsg(struct ipmi_softc *sc, int len, const uint8_t * data)
774 1.1 mlelstv {
775 1.1 mlelstv int idx, sts;
776 1.1 mlelstv
777 1.1 mlelstv /* ASSERT: IBF is clear */
778 1.1 mlelstv dbg_dump(50, __func__, len, data);
779 1.1 mlelstv sts = kcs_write_cmd(sc, KCS_WRITE_START);
780 1.1 mlelstv for (idx = 0; idx < len; idx++) {
781 1.1 mlelstv if (idx == len - 1)
782 1.1 mlelstv sts = kcs_write_cmd(sc, KCS_WRITE_END);
783 1.1 mlelstv
784 1.1 mlelstv if (sts != KCS_WRITE_STATE)
785 1.1 mlelstv break;
786 1.1 mlelstv
787 1.1 mlelstv sts = kcs_write_data(sc, data[idx]);
788 1.1 mlelstv }
789 1.1 mlelstv if (sts != KCS_READ_STATE) {
790 1.1 mlelstv dbg_printf(1, "%s: %d/%d <%#.2x>\n", __func__, idx, len, sts);
791 1.1 mlelstv dbg_dump(1, __func__, len, data);
792 1.1 mlelstv return -1;
793 1.1 mlelstv }
794 1.1 mlelstv
795 1.1 mlelstv return 0;
796 1.1 mlelstv }
797 1.1 mlelstv
798 1.1 mlelstv static int
799 1.1 mlelstv kcs_recvmsg(struct ipmi_softc *sc, int maxlen, int *rxlen, uint8_t * data)
800 1.1 mlelstv {
801 1.1 mlelstv int idx, sts;
802 1.1 mlelstv
803 1.1 mlelstv for (idx = 0; idx < maxlen; idx++) {
804 1.1 mlelstv sts = kcs_read_data(sc, &data[idx]);
805 1.1 mlelstv if (sts != KCS_READ_STATE)
806 1.1 mlelstv break;
807 1.1 mlelstv }
808 1.1 mlelstv sts = kcs_wait(sc, KCS_IBF, 0, __func__);
809 1.1 mlelstv *rxlen = idx;
810 1.1 mlelstv if (sts != KCS_IDLE_STATE) {
811 1.1 mlelstv dbg_printf(1, "%s: %d/%d <%#.2x>\n",
812 1.1 mlelstv __func__, idx, maxlen, sts);
813 1.1 mlelstv return -1;
814 1.1 mlelstv }
815 1.1 mlelstv
816 1.1 mlelstv dbg_dump(50, __func__, idx, data);
817 1.1 mlelstv
818 1.1 mlelstv return 0;
819 1.1 mlelstv }
820 1.1 mlelstv
821 1.1 mlelstv static int
822 1.1 mlelstv kcs_reset(struct ipmi_softc *sc)
823 1.1 mlelstv {
824 1.1 mlelstv return -1;
825 1.1 mlelstv }
826 1.1 mlelstv
827 1.1 mlelstv static int
828 1.1 mlelstv kcs_probe(struct ipmi_softc *sc)
829 1.1 mlelstv {
830 1.1 mlelstv uint8_t v;
831 1.1 mlelstv
832 1.1 mlelstv v = bmc_read(sc, _KCS_STATUS_REGISTER);
833 1.1 mlelstv #if 0
834 1.1 mlelstv printf("%s: %2x\n", __func__, v);
835 1.1 mlelstv printf(" STS: %2x\n", v & KCS_STATE_MASK);
836 1.1 mlelstv printf(" ATN: %2x\n", v & KCS_SMS_ATN);
837 1.1 mlelstv printf(" C/D: %2x\n", v & KCS_CD);
838 1.1 mlelstv printf(" IBF: %2x\n", v & KCS_IBF);
839 1.1 mlelstv printf(" OBF: %2x\n", v & KCS_OBF);
840 1.1 mlelstv #else
841 1.1 mlelstv __USE(v);
842 1.1 mlelstv #endif
843 1.1 mlelstv return 0;
844 1.1 mlelstv }
845 1.1 mlelstv
846 1.1 mlelstv /*
847 1.1 mlelstv * IPMI code
848 1.1 mlelstv */
849 1.1 mlelstv #define READ_SMS_BUFFER 0x37
850 1.1 mlelstv #define WRITE_I2C 0x50
851 1.1 mlelstv
852 1.1 mlelstv #define GET_MESSAGE_CMD 0x33
853 1.1 mlelstv #define SEND_MESSAGE_CMD 0x34
854 1.1 mlelstv
855 1.1 mlelstv #define IPMB_CHANNEL_NUMBER 0
856 1.1 mlelstv
857 1.1 mlelstv #define PUBLIC_BUS 0
858 1.1 mlelstv
859 1.1 mlelstv #define MIN_I2C_PACKET_SIZE 3
860 1.1 mlelstv #define MIN_IMB_PACKET_SIZE 7 /* one byte for cksum */
861 1.1 mlelstv
862 1.1 mlelstv #define MIN_BTBMC_REQ_SIZE 4
863 1.1 mlelstv #define MIN_BTBMC_RSP_SIZE 5
864 1.1 mlelstv #define MIN_BMC_REQ_SIZE 2
865 1.1 mlelstv #define MIN_BMC_RSP_SIZE 3
866 1.1 mlelstv
867 1.1 mlelstv #define BMC_SA 0x20 /* BMC/ESM3 */
868 1.1 mlelstv #define FPC_SA 0x22 /* front panel */
869 1.1 mlelstv #define BP_SA 0xC0 /* Primary Backplane */
870 1.1 mlelstv #define BP2_SA 0xC2 /* Secondary Backplane */
871 1.1 mlelstv #define PBP_SA 0xC4 /* Peripheral Backplane */
872 1.1 mlelstv #define DRAC_SA 0x28 /* DRAC-III */
873 1.1 mlelstv #define DRAC3_SA 0x30 /* DRAC-III */
874 1.1 mlelstv #define BMC_LUN 0
875 1.1 mlelstv #define SMS_LUN 2
876 1.1 mlelstv
877 1.1 mlelstv struct ipmi_request {
878 1.1 mlelstv uint8_t rsSa;
879 1.1 mlelstv uint8_t rsLun;
880 1.1 mlelstv uint8_t netFn;
881 1.1 mlelstv uint8_t cmd;
882 1.1 mlelstv uint8_t data_len;
883 1.1 mlelstv uint8_t *data;
884 1.1 mlelstv };
885 1.1 mlelstv
886 1.1 mlelstv struct ipmi_response {
887 1.1 mlelstv uint8_t cCode;
888 1.1 mlelstv uint8_t data_len;
889 1.1 mlelstv uint8_t *data;
890 1.1 mlelstv };
891 1.1 mlelstv
892 1.1 mlelstv struct ipmi_bmc_request {
893 1.1 mlelstv uint8_t bmc_nfLn;
894 1.1 mlelstv uint8_t bmc_cmd;
895 1.1 mlelstv uint8_t bmc_data_len;
896 1.1 mlelstv uint8_t bmc_data[1];
897 1.1 mlelstv };
898 1.1 mlelstv
899 1.1 mlelstv struct ipmi_bmc_response {
900 1.1 mlelstv uint8_t bmc_nfLn;
901 1.1 mlelstv uint8_t bmc_cmd;
902 1.1 mlelstv uint8_t bmc_cCode;
903 1.1 mlelstv uint8_t bmc_data_len;
904 1.1 mlelstv uint8_t bmc_data[1];
905 1.1 mlelstv };
906 1.1 mlelstv
907 1.1 mlelstv
908 1.1 mlelstv CFATTACH_DECL2_NEW(ipmi, sizeof(struct ipmi_softc),
909 1.1 mlelstv ipmi_match, ipmi_attach, ipmi_detach, NULL, NULL, NULL);
910 1.1 mlelstv
911 1.1 mlelstv static void
912 1.1 mlelstv dumpb(const char *lbl, int len, const uint8_t *data)
913 1.1 mlelstv {
914 1.1 mlelstv int idx;
915 1.1 mlelstv
916 1.1 mlelstv printf("%s: ", lbl);
917 1.1 mlelstv for (idx = 0; idx < len; idx++)
918 1.1 mlelstv printf("%.2x ", data[idx]);
919 1.1 mlelstv
920 1.1 mlelstv printf("\n");
921 1.1 mlelstv }
922 1.1 mlelstv
923 1.1 mlelstv /*
924 1.1 mlelstv * bt_buildmsg builds an IPMI message from a nfLun, cmd, and data
925 1.1 mlelstv * This is used by BT protocol
926 1.1 mlelstv *
927 1.1 mlelstv * Returns a buffer to an allocated message, txlen contains length
928 1.1 mlelstv * of allocated message
929 1.1 mlelstv */
930 1.1 mlelstv static void *
931 1.1 mlelstv bt_buildmsg(struct ipmi_softc *sc, int nfLun, int cmd, int len,
932 1.1 mlelstv const void *data, int *txlen)
933 1.1 mlelstv {
934 1.1 mlelstv uint8_t *buf;
935 1.1 mlelstv
936 1.1 mlelstv /* Block transfer needs 4 extra bytes: length/netfn/seq/cmd + data */
937 1.1 mlelstv *txlen = len + 4;
938 1.1 mlelstv buf = ipmi_buf_acquire(sc, *txlen);
939 1.1 mlelstv if (buf == NULL)
940 1.1 mlelstv return NULL;
941 1.1 mlelstv
942 1.1 mlelstv buf[IPMI_BTMSG_LEN] = len + 3;
943 1.1 mlelstv buf[IPMI_BTMSG_NFLN] = nfLun;
944 1.1 mlelstv buf[IPMI_BTMSG_SEQ] = sc->sc_btseq++;
945 1.1 mlelstv buf[IPMI_BTMSG_CMD] = cmd;
946 1.1 mlelstv if (len && data)
947 1.1 mlelstv memcpy(buf + IPMI_BTMSG_DATASND, data, len);
948 1.1 mlelstv
949 1.1 mlelstv return buf;
950 1.1 mlelstv }
951 1.1 mlelstv
952 1.1 mlelstv /*
953 1.1 mlelstv * cmn_buildmsg builds an IPMI message from a nfLun, cmd, and data
954 1.1 mlelstv * This is used by both SMIC and KCS protocols
955 1.1 mlelstv *
956 1.1 mlelstv * Returns a buffer to an allocated message, txlen contains length
957 1.1 mlelstv * of allocated message
958 1.1 mlelstv */
959 1.1 mlelstv static void *
960 1.1 mlelstv cmn_buildmsg(struct ipmi_softc *sc, int nfLun, int cmd, int len,
961 1.1 mlelstv const void *data, int *txlen)
962 1.1 mlelstv {
963 1.1 mlelstv uint8_t *buf;
964 1.1 mlelstv
965 1.1 mlelstv /* Common needs two extra bytes: nfLun/cmd + data */
966 1.1 mlelstv *txlen = len + 2;
967 1.1 mlelstv buf = ipmi_buf_acquire(sc, *txlen);
968 1.1 mlelstv if (buf == NULL)
969 1.1 mlelstv return NULL;
970 1.1 mlelstv
971 1.1 mlelstv buf[IPMI_MSG_NFLN] = nfLun;
972 1.1 mlelstv buf[IPMI_MSG_CMD] = cmd;
973 1.1 mlelstv if (len && data)
974 1.1 mlelstv memcpy(buf + IPMI_MSG_DATASND, data, len);
975 1.1 mlelstv
976 1.1 mlelstv return buf;
977 1.1 mlelstv }
978 1.1 mlelstv
979 1.1 mlelstv /*
980 1.1 mlelstv * ipmi_sendcmd: caller must hold sc_cmd_mtx.
981 1.1 mlelstv *
982 1.1 mlelstv * Send an IPMI command
983 1.1 mlelstv */
984 1.1 mlelstv static int
985 1.1 mlelstv ipmi_sendcmd(struct ipmi_softc *sc, int rssa, int rslun, int netfn, int cmd,
986 1.1 mlelstv int txlen, const void *data)
987 1.1 mlelstv {
988 1.1 mlelstv uint8_t *buf;
989 1.1 mlelstv int rc = -1;
990 1.1 mlelstv
991 1.1 mlelstv dbg_printf(50, "%s: rssa=%#.2x nfln=%#.2x cmd=%#.2x len=%#.2x\n",
992 1.1 mlelstv __func__, rssa, NETFN_LUN(netfn, rslun), cmd, txlen);
993 1.1 mlelstv dbg_dump(10, __func__, txlen, data);
994 1.1 mlelstv if (rssa != BMC_SA) {
995 1.1 mlelstv #if 0
996 1.1 mlelstv buf = sc->sc_if->buildmsg(sc, NETFN_LUN(APP_NETFN, BMC_LUN),
997 1.1 mlelstv APP_SEND_MESSAGE, 7 + txlen, NULL, &txlen);
998 1.1 mlelstv pI2C->bus = (sc->if_ver == 0x09) ?
999 1.1 mlelstv PUBLIC_BUS :
1000 1.1 mlelstv IPMB_CHANNEL_NUMBER;
1001 1.1 mlelstv
1002 1.1 mlelstv imbreq->rsSa = rssa;
1003 1.1 mlelstv imbreq->nfLn = NETFN_LUN(netfn, rslun);
1004 1.1 mlelstv imbreq->cSum1 = -(imbreq->rsSa + imbreq->nfLn);
1005 1.1 mlelstv imbreq->rqSa = BMC_SA;
1006 1.1 mlelstv imbreq->seqLn = NETFN_LUN(sc->imb_seq++, SMS_LUN);
1007 1.1 mlelstv imbreq->cmd = cmd;
1008 1.1 mlelstv if (txlen)
1009 1.1 mlelstv memcpy(imbreq->data, data, txlen);
1010 1.1 mlelstv /* Set message checksum */
1011 1.1 mlelstv imbreq->data[txlen] = cksum8(&imbreq->rqSa, txlen + 3);
1012 1.1 mlelstv #endif
1013 1.1 mlelstv goto done;
1014 1.1 mlelstv } else
1015 1.1 mlelstv buf = sc->sc_if->buildmsg(sc, NETFN_LUN(netfn, rslun), cmd,
1016 1.1 mlelstv txlen, data, &txlen);
1017 1.1 mlelstv
1018 1.1 mlelstv if (buf == NULL) {
1019 1.1 mlelstv aprint_error_dev(sc->sc_dev, "sendcmd buffer busy\n");
1020 1.1 mlelstv goto done;
1021 1.1 mlelstv }
1022 1.1 mlelstv rc = sc->sc_if->sendmsg(sc, txlen, buf);
1023 1.1 mlelstv ipmi_buf_release(sc, buf);
1024 1.1 mlelstv
1025 1.1 mlelstv ipmi_delay(sc, 50); /* give bmc chance to digest command */
1026 1.1 mlelstv
1027 1.1 mlelstv done:
1028 1.1 mlelstv return rc;
1029 1.1 mlelstv }
1030 1.1 mlelstv
1031 1.1 mlelstv static void
1032 1.1 mlelstv ipmi_buf_release(struct ipmi_softc *sc, char *buf)
1033 1.1 mlelstv {
1034 1.1 mlelstv KASSERT(sc->sc_buf_rsvd);
1035 1.1 mlelstv KASSERT(sc->sc_buf == buf);
1036 1.1 mlelstv sc->sc_buf_rsvd = false;
1037 1.1 mlelstv }
1038 1.1 mlelstv
1039 1.1 mlelstv static char *
1040 1.1 mlelstv ipmi_buf_acquire(struct ipmi_softc *sc, size_t len)
1041 1.1 mlelstv {
1042 1.1 mlelstv KASSERT(len <= sizeof(sc->sc_buf));
1043 1.1 mlelstv
1044 1.1 mlelstv if (sc->sc_buf_rsvd || len > sizeof(sc->sc_buf))
1045 1.1 mlelstv return NULL;
1046 1.1 mlelstv sc->sc_buf_rsvd = true;
1047 1.1 mlelstv return sc->sc_buf;
1048 1.1 mlelstv }
1049 1.1 mlelstv
1050 1.1 mlelstv /*
1051 1.1 mlelstv * ipmi_recvcmd: caller must hold sc_cmd_mtx.
1052 1.1 mlelstv */
1053 1.1 mlelstv static int
1054 1.1 mlelstv ipmi_recvcmd(struct ipmi_softc *sc, int maxlen, int *rxlen, void *data)
1055 1.1 mlelstv {
1056 1.1 mlelstv uint8_t *buf, rc = 0;
1057 1.1 mlelstv int rawlen;
1058 1.1 mlelstv
1059 1.1 mlelstv /* Need three extra bytes: netfn/cmd/ccode + data */
1060 1.1 mlelstv buf = ipmi_buf_acquire(sc, maxlen + 3);
1061 1.1 mlelstv if (buf == NULL) {
1062 1.1 mlelstv aprint_error_dev(sc->sc_dev, "%s: malloc fails\n", __func__);
1063 1.1 mlelstv return -1;
1064 1.1 mlelstv }
1065 1.1 mlelstv /* Receive message from interface, copy out result data */
1066 1.1 mlelstv if (sc->sc_if->recvmsg(sc, maxlen + 3, &rawlen, buf)) {
1067 1.1 mlelstv ipmi_buf_release(sc, buf);
1068 1.1 mlelstv return -1;
1069 1.1 mlelstv }
1070 1.1 mlelstv
1071 1.4 mlelstv *rxlen = rawlen >= IPMI_MSG_DATARCV ? rawlen - IPMI_MSG_DATARCV : 0;
1072 1.1 mlelstv if (*rxlen > 0 && data)
1073 1.1 mlelstv memcpy(data, buf + IPMI_MSG_DATARCV, *rxlen);
1074 1.1 mlelstv
1075 1.1 mlelstv if ((rc = buf[IPMI_MSG_CCODE]) != 0)
1076 1.1 mlelstv dbg_printf(1, "%s: nfln=%#.2x cmd=%#.2x err=%#.2x\n", __func__,
1077 1.1 mlelstv buf[IPMI_MSG_NFLN], buf[IPMI_MSG_CMD], buf[IPMI_MSG_CCODE]);
1078 1.1 mlelstv
1079 1.1 mlelstv dbg_printf(50, "%s: nfln=%#.2x cmd=%#.2x err=%#.2x len=%#.2x\n",
1080 1.1 mlelstv __func__, buf[IPMI_MSG_NFLN], buf[IPMI_MSG_CMD],
1081 1.1 mlelstv buf[IPMI_MSG_CCODE], *rxlen);
1082 1.1 mlelstv dbg_dump(10, __func__, *rxlen, data);
1083 1.1 mlelstv
1084 1.1 mlelstv ipmi_buf_release(sc, buf);
1085 1.1 mlelstv
1086 1.1 mlelstv return rc;
1087 1.1 mlelstv }
1088 1.1 mlelstv
1089 1.1 mlelstv /*
1090 1.1 mlelstv * ipmi_delay: caller must hold sc_cmd_mtx.
1091 1.1 mlelstv */
1092 1.1 mlelstv static void
1093 1.1 mlelstv ipmi_delay(struct ipmi_softc *sc, int ms)
1094 1.1 mlelstv {
1095 1.1 mlelstv if (cold) {
1096 1.1 mlelstv delay(ms * 1000);
1097 1.1 mlelstv return;
1098 1.1 mlelstv }
1099 1.1 mlelstv mutex_enter(&sc->sc_sleep_mtx);
1100 1.1 mlelstv cv_timedwait(&sc->sc_cmd_sleep, &sc->sc_sleep_mtx, mstohz(ms));
1101 1.1 mlelstv mutex_exit(&sc->sc_sleep_mtx);
1102 1.1 mlelstv }
1103 1.1 mlelstv
1104 1.1 mlelstv /* Read a partial SDR entry */
1105 1.1 mlelstv static int
1106 1.1 mlelstv get_sdr_partial(struct ipmi_softc *sc, uint16_t recordId, uint16_t reserveId,
1107 1.1 mlelstv uint8_t offset, uint8_t length, void *buffer, uint16_t *nxtRecordId)
1108 1.1 mlelstv {
1109 1.1 mlelstv uint8_t cmd[256 + 8];
1110 1.1 mlelstv int len;
1111 1.1 mlelstv
1112 1.1 mlelstv ((uint16_t *) cmd)[0] = reserveId;
1113 1.1 mlelstv ((uint16_t *) cmd)[1] = recordId;
1114 1.1 mlelstv cmd[4] = offset;
1115 1.1 mlelstv cmd[5] = length;
1116 1.1 mlelstv mutex_enter(&sc->sc_cmd_mtx);
1117 1.1 mlelstv if (ipmi_sendcmd(sc, BMC_SA, 0, STORAGE_NETFN, STORAGE_GET_SDR, 6,
1118 1.1 mlelstv cmd)) {
1119 1.1 mlelstv mutex_exit(&sc->sc_cmd_mtx);
1120 1.1 mlelstv aprint_error_dev(sc->sc_dev, "%s: sendcmd fails\n", __func__);
1121 1.1 mlelstv return -1;
1122 1.1 mlelstv }
1123 1.1 mlelstv if (ipmi_recvcmd(sc, 8 + length, &len, cmd)) {
1124 1.1 mlelstv mutex_exit(&sc->sc_cmd_mtx);
1125 1.1 mlelstv aprint_error_dev(sc->sc_dev, "%s: recvcmd fails\n", __func__);
1126 1.1 mlelstv return -1;
1127 1.1 mlelstv }
1128 1.1 mlelstv mutex_exit(&sc->sc_cmd_mtx);
1129 1.1 mlelstv if (nxtRecordId)
1130 1.1 mlelstv *nxtRecordId = *(uint16_t *) cmd;
1131 1.1 mlelstv memcpy(buffer, cmd + 2, len - 2);
1132 1.1 mlelstv
1133 1.1 mlelstv return 0;
1134 1.1 mlelstv }
1135 1.1 mlelstv
1136 1.1 mlelstv static int maxsdrlen = 0x10;
1137 1.1 mlelstv
1138 1.1 mlelstv /* Read an entire SDR; pass to add sensor */
1139 1.1 mlelstv static int
1140 1.1 mlelstv get_sdr(struct ipmi_softc *sc, uint16_t recid, uint16_t *nxtrec)
1141 1.1 mlelstv {
1142 1.1 mlelstv uint16_t resid = 0;
1143 1.1 mlelstv int len, sdrlen, offset;
1144 1.1 mlelstv uint8_t *psdr;
1145 1.1 mlelstv struct sdrhdr shdr;
1146 1.1 mlelstv
1147 1.1 mlelstv mutex_enter(&sc->sc_cmd_mtx);
1148 1.1 mlelstv /* Reserve SDR */
1149 1.1 mlelstv if (ipmi_sendcmd(sc, BMC_SA, 0, STORAGE_NETFN, STORAGE_RESERVE_SDR,
1150 1.1 mlelstv 0, NULL)) {
1151 1.1 mlelstv mutex_exit(&sc->sc_cmd_mtx);
1152 1.1 mlelstv aprint_error_dev(sc->sc_dev, "reserve send fails\n");
1153 1.1 mlelstv return -1;
1154 1.1 mlelstv }
1155 1.1 mlelstv if (ipmi_recvcmd(sc, sizeof(resid), &len, &resid)) {
1156 1.1 mlelstv mutex_exit(&sc->sc_cmd_mtx);
1157 1.1 mlelstv aprint_error_dev(sc->sc_dev, "reserve recv fails\n");
1158 1.1 mlelstv return -1;
1159 1.1 mlelstv }
1160 1.1 mlelstv mutex_exit(&sc->sc_cmd_mtx);
1161 1.1 mlelstv /* Get SDR Header */
1162 1.1 mlelstv if (get_sdr_partial(sc, recid, resid, 0, sizeof shdr, &shdr, nxtrec)) {
1163 1.1 mlelstv aprint_error_dev(sc->sc_dev, "get header fails\n");
1164 1.1 mlelstv return -1;
1165 1.1 mlelstv }
1166 1.1 mlelstv /* Allocate space for entire SDR Length of SDR in header does not
1167 1.1 mlelstv * include header length */
1168 1.1 mlelstv sdrlen = sizeof(shdr) + shdr.record_length;
1169 1.1 mlelstv psdr = malloc(sdrlen, M_DEVBUF, M_WAITOK);
1170 1.1 mlelstv if (psdr == NULL)
1171 1.1 mlelstv return -1;
1172 1.1 mlelstv
1173 1.1 mlelstv memcpy(psdr, &shdr, sizeof(shdr));
1174 1.1 mlelstv
1175 1.1 mlelstv /* Read SDR Data maxsdrlen bytes at a time */
1176 1.1 mlelstv for (offset = sizeof(shdr); offset < sdrlen; offset += maxsdrlen) {
1177 1.1 mlelstv len = sdrlen - offset;
1178 1.1 mlelstv if (len > maxsdrlen)
1179 1.1 mlelstv len = maxsdrlen;
1180 1.1 mlelstv
1181 1.1 mlelstv if (get_sdr_partial(sc, recid, resid, offset, len,
1182 1.1 mlelstv psdr + offset, NULL)) {
1183 1.1 mlelstv aprint_error_dev(sc->sc_dev,
1184 1.1 mlelstv "get chunk : %d,%d fails\n", offset, len);
1185 1.1 mlelstv free(psdr, M_DEVBUF);
1186 1.1 mlelstv return -1;
1187 1.1 mlelstv }
1188 1.1 mlelstv }
1189 1.1 mlelstv
1190 1.1 mlelstv /* Add SDR to sensor list, if not wanted, free buffer */
1191 1.1 mlelstv if (add_sdr_sensor(sc, psdr) == 0)
1192 1.1 mlelstv free(psdr, M_DEVBUF);
1193 1.1 mlelstv
1194 1.1 mlelstv return 0;
1195 1.1 mlelstv }
1196 1.1 mlelstv
1197 1.1 mlelstv static int
1198 1.1 mlelstv getbits(uint8_t *bytes, int bitpos, int bitlen)
1199 1.1 mlelstv {
1200 1.1 mlelstv int v;
1201 1.1 mlelstv int mask;
1202 1.1 mlelstv
1203 1.1 mlelstv bitpos += bitlen - 1;
1204 1.1 mlelstv for (v = 0; bitlen--;) {
1205 1.1 mlelstv v <<= 1;
1206 1.1 mlelstv mask = 1L << (bitpos & 7);
1207 1.1 mlelstv if (bytes[bitpos >> 3] & mask)
1208 1.1 mlelstv v |= 1;
1209 1.1 mlelstv bitpos--;
1210 1.1 mlelstv }
1211 1.1 mlelstv
1212 1.1 mlelstv return v;
1213 1.1 mlelstv }
1214 1.1 mlelstv
1215 1.1 mlelstv /* Decode IPMI sensor name */
1216 1.1 mlelstv static void
1217 1.1 mlelstv ipmi_sensor_name(char *name, int len, uint8_t typelen, uint8_t *bits)
1218 1.1 mlelstv {
1219 1.1 mlelstv int i, slen;
1220 1.1 mlelstv char bcdplus[] = "0123456789 -.:,_";
1221 1.1 mlelstv
1222 1.1 mlelstv slen = typelen & 0x1F;
1223 1.1 mlelstv switch (typelen >> 6) {
1224 1.1 mlelstv case IPMI_NAME_UNICODE:
1225 1.1 mlelstv //unicode
1226 1.1 mlelstv break;
1227 1.1 mlelstv
1228 1.1 mlelstv case IPMI_NAME_BCDPLUS:
1229 1.1 mlelstv /* Characters are encoded in 4-bit BCDPLUS */
1230 1.1 mlelstv if (len < slen * 2 + 1)
1231 1.1 mlelstv slen = (len >> 1) - 1;
1232 1.1 mlelstv for (i = 0; i < slen; i++) {
1233 1.1 mlelstv *(name++) = bcdplus[bits[i] >> 4];
1234 1.1 mlelstv *(name++) = bcdplus[bits[i] & 0xF];
1235 1.1 mlelstv }
1236 1.1 mlelstv break;
1237 1.1 mlelstv
1238 1.1 mlelstv case IPMI_NAME_ASCII6BIT:
1239 1.1 mlelstv /* Characters are encoded in 6-bit ASCII
1240 1.1 mlelstv * 0x00 - 0x3F maps to 0x20 - 0x5F */
1241 1.1 mlelstv /* XXX: need to calculate max len: slen = 3/4 * len */
1242 1.1 mlelstv if (len < slen + 1)
1243 1.1 mlelstv slen = len - 1;
1244 1.1 mlelstv for (i = 0; i < slen * 8; i += 6)
1245 1.1 mlelstv *(name++) = getbits(bits, i, 6) + ' ';
1246 1.1 mlelstv break;
1247 1.1 mlelstv
1248 1.1 mlelstv case IPMI_NAME_ASCII8BIT:
1249 1.1 mlelstv /* Characters are 8-bit ascii */
1250 1.1 mlelstv if (len < slen + 1)
1251 1.1 mlelstv slen = len - 1;
1252 1.1 mlelstv while (slen--)
1253 1.1 mlelstv *(name++) = *(bits++);
1254 1.1 mlelstv break;
1255 1.1 mlelstv }
1256 1.1 mlelstv *name = 0;
1257 1.1 mlelstv }
1258 1.1 mlelstv
1259 1.1 mlelstv /* Sign extend a n-bit value */
1260 1.1 mlelstv static long
1261 1.1 mlelstv signextend(unsigned long val, int bits)
1262 1.1 mlelstv {
1263 1.1 mlelstv long msk = (1L << (bits-1))-1;
1264 1.1 mlelstv
1265 1.1 mlelstv return -(val & ~msk) | val;
1266 1.1 mlelstv }
1267 1.1 mlelstv
1268 1.1 mlelstv
1269 1.1 mlelstv /* fixpoint arithmetic */
1270 1.1 mlelstv #define FIX2INT(x) ((int64_t)((x) >> 32))
1271 1.1 mlelstv #define INT2FIX(x) ((int64_t)((uint64_t)(x) << 32))
1272 1.1 mlelstv
1273 1.1 mlelstv #define FIX2 0x0000000200000000ll /* 2.0 */
1274 1.1 mlelstv #define FIX3 0x0000000300000000ll /* 3.0 */
1275 1.1 mlelstv #define FIXE 0x00000002b7e15163ll /* 2.71828182845904523536 */
1276 1.1 mlelstv #define FIX10 0x0000000a00000000ll /* 10.0 */
1277 1.1 mlelstv #define FIXMONE 0xffffffff00000000ll /* -1.0 */
1278 1.1 mlelstv #define FIXHALF 0x0000000080000000ll /* 0.5 */
1279 1.1 mlelstv #define FIXTHIRD 0x0000000055555555ll /* 0.33333333333333333333 */
1280 1.1 mlelstv
1281 1.1 mlelstv #define FIX1LOG2 0x0000000171547653ll /* 1.0/log(2) */
1282 1.1 mlelstv #define FIX1LOGE 0x0000000100000000ll /* 1.0/log(2.71828182845904523536) */
1283 1.1 mlelstv #define FIX1LOG10 0x000000006F2DEC55ll /* 1.0/log(10) */
1284 1.1 mlelstv
1285 1.1 mlelstv #define FIX1E 0x000000005E2D58D9ll /* 1.0/2.71828182845904523536 */
1286 1.1 mlelstv
1287 1.1 mlelstv static int64_t fixlog_a[] = {
1288 1.1 mlelstv 0x0000000100000000ll /* 1.0/1.0 */,
1289 1.1 mlelstv 0xffffffff80000000ll /* -1.0/2.0 */,
1290 1.1 mlelstv 0x0000000055555555ll /* 1.0/3.0 */,
1291 1.1 mlelstv 0xffffffffc0000000ll /* -1.0/4.0 */,
1292 1.1 mlelstv 0x0000000033333333ll /* 1.0/5.0 */,
1293 1.1 mlelstv 0x000000002aaaaaabll /* -1.0/6.0 */,
1294 1.1 mlelstv 0x0000000024924925ll /* 1.0/7.0 */,
1295 1.1 mlelstv 0x0000000020000000ll /* -1.0/8.0 */,
1296 1.1 mlelstv 0x000000001c71c71cll /* 1.0/9.0 */
1297 1.1 mlelstv };
1298 1.1 mlelstv
1299 1.1 mlelstv static int64_t fixexp_a[] = {
1300 1.1 mlelstv 0x0000000100000000ll /* 1.0/1.0 */,
1301 1.1 mlelstv 0x0000000100000000ll /* 1.0/1.0 */,
1302 1.1 mlelstv 0x0000000080000000ll /* 1.0/2.0 */,
1303 1.1 mlelstv 0x000000002aaaaaabll /* 1.0/6.0 */,
1304 1.1 mlelstv 0x000000000aaaaaabll /* 1.0/24.0 */,
1305 1.1 mlelstv 0x0000000002222222ll /* 1.0/120.0 */,
1306 1.1 mlelstv 0x00000000005b05b0ll /* 1.0/720.0 */,
1307 1.1 mlelstv 0x00000000000d00d0ll /* 1.0/5040.0 */,
1308 1.1 mlelstv 0x000000000001a01all /* 1.0/40320.0 */
1309 1.1 mlelstv };
1310 1.1 mlelstv
1311 1.1 mlelstv static int64_t
1312 1.1 mlelstv fixmul(int64_t x, int64_t y)
1313 1.1 mlelstv {
1314 1.1 mlelstv int64_t z;
1315 1.1 mlelstv int64_t a,b,c,d;
1316 1.1 mlelstv int neg;
1317 1.1 mlelstv
1318 1.1 mlelstv neg = 0;
1319 1.1 mlelstv if (x < 0) {
1320 1.1 mlelstv x = -x;
1321 1.1 mlelstv neg = !neg;
1322 1.1 mlelstv }
1323 1.1 mlelstv if (y < 0) {
1324 1.1 mlelstv y = -y;
1325 1.1 mlelstv neg = !neg;
1326 1.1 mlelstv }
1327 1.1 mlelstv
1328 1.1 mlelstv a = FIX2INT(x);
1329 1.1 mlelstv b = x - INT2FIX(a);
1330 1.1 mlelstv c = FIX2INT(y);
1331 1.1 mlelstv d = y - INT2FIX(c);
1332 1.1 mlelstv
1333 1.1 mlelstv z = INT2FIX(a*c) + a * d + b * c + (b/2 * d/2 >> 30);
1334 1.1 mlelstv
1335 1.1 mlelstv return neg ? -z : z;
1336 1.1 mlelstv }
1337 1.1 mlelstv
1338 1.1 mlelstv static int64_t
1339 1.1 mlelstv poly(int64_t x0, int64_t x, int64_t a[], int n)
1340 1.1 mlelstv {
1341 1.1 mlelstv int64_t z;
1342 1.1 mlelstv int i;
1343 1.1 mlelstv
1344 1.1 mlelstv z = fixmul(x0, a[0]);
1345 1.1 mlelstv for (i=1; i<n; ++i) {
1346 1.1 mlelstv x0 = fixmul(x0, x);
1347 1.1 mlelstv z = fixmul(x0, a[i]) + z;
1348 1.1 mlelstv }
1349 1.1 mlelstv return z;
1350 1.1 mlelstv }
1351 1.1 mlelstv
1352 1.1 mlelstv static int64_t
1353 1.1 mlelstv logx(int64_t x, int64_t y)
1354 1.1 mlelstv {
1355 1.1 mlelstv int64_t z;
1356 1.1 mlelstv
1357 1.1 mlelstv if (x <= INT2FIX(0)) {
1358 1.1 mlelstv z = INT2FIX(-99999);
1359 1.1 mlelstv goto done;
1360 1.1 mlelstv }
1361 1.1 mlelstv
1362 1.1 mlelstv z = INT2FIX(0);
1363 1.1 mlelstv while (x >= FIXE) {
1364 1.1 mlelstv x = fixmul(x, FIX1E);
1365 1.1 mlelstv z += INT2FIX(1);
1366 1.1 mlelstv }
1367 1.1 mlelstv while (x < INT2FIX(1)) {
1368 1.1 mlelstv x = fixmul(x, FIXE);
1369 1.1 mlelstv z -= INT2FIX(1);
1370 1.1 mlelstv }
1371 1.1 mlelstv
1372 1.1 mlelstv x -= INT2FIX(1);
1373 1.1 mlelstv z += poly(x, x, fixlog_a, sizeof(fixlog_a)/sizeof(fixlog_a[0]));
1374 1.1 mlelstv z = fixmul(z, y);
1375 1.1 mlelstv
1376 1.1 mlelstv done:
1377 1.1 mlelstv return z;
1378 1.1 mlelstv }
1379 1.1 mlelstv
1380 1.1 mlelstv static int64_t
1381 1.1 mlelstv powx(int64_t x, int64_t y)
1382 1.1 mlelstv {
1383 1.1 mlelstv int64_t k;
1384 1.1 mlelstv
1385 1.1 mlelstv if (x == INT2FIX(0))
1386 1.1 mlelstv goto done;
1387 1.1 mlelstv
1388 1.1 mlelstv x = logx(x,y);
1389 1.1 mlelstv
1390 1.1 mlelstv if (x < INT2FIX(0)) {
1391 1.1 mlelstv x = INT2FIX(0) - x;
1392 1.1 mlelstv k = -FIX2INT(x);
1393 1.1 mlelstv x = INT2FIX(-k) - x;
1394 1.1 mlelstv } else {
1395 1.1 mlelstv k = FIX2INT(x);
1396 1.1 mlelstv x = x - INT2FIX(k);
1397 1.1 mlelstv }
1398 1.1 mlelstv
1399 1.1 mlelstv x = poly(INT2FIX(1), x, fixexp_a, sizeof(fixexp_a)/sizeof(fixexp_a[0]));
1400 1.1 mlelstv
1401 1.1 mlelstv while (k < 0) {
1402 1.1 mlelstv x = fixmul(x, FIX1E);
1403 1.1 mlelstv ++k;
1404 1.1 mlelstv }
1405 1.1 mlelstv while (k > 0) {
1406 1.1 mlelstv x = fixmul(x, FIXE);
1407 1.1 mlelstv --k;
1408 1.1 mlelstv }
1409 1.1 mlelstv
1410 1.1 mlelstv done:
1411 1.1 mlelstv return x;
1412 1.1 mlelstv }
1413 1.1 mlelstv
1414 1.1 mlelstv /* Convert IPMI reading from sensor factors */
1415 1.1 mlelstv static long
1416 1.1 mlelstv ipmi_convert(uint8_t v, struct sdrtype1 *s1, long adj)
1417 1.1 mlelstv {
1418 1.1 mlelstv int64_t M, B;
1419 1.1 mlelstv char K1, K2;
1420 1.1 mlelstv int64_t val, v1, v2, vs;
1421 1.1 mlelstv int sign = (s1->units1 >> 6) & 0x3;
1422 1.1 mlelstv
1423 1.1 mlelstv vs = (sign == 0x1 || sign == 0x2) ? (int8_t)v : v;
1424 1.1 mlelstv if ((vs < 0) && (sign == 0x1))
1425 1.1 mlelstv vs++;
1426 1.1 mlelstv
1427 1.1 mlelstv /* Calculate linear reading variables */
1428 1.1 mlelstv M = signextend((((short)(s1->m_tolerance & 0xC0)) << 2) + s1->m, 10);
1429 1.1 mlelstv B = signextend((((short)(s1->b_accuracy & 0xC0)) << 2) + s1->b, 10);
1430 1.1 mlelstv K1 = signextend(s1->rbexp & 0xF, 4);
1431 1.1 mlelstv K2 = signextend(s1->rbexp >> 4, 4);
1432 1.1 mlelstv
1433 1.1 mlelstv /* Calculate sensor reading:
1434 1.1 mlelstv * y = L((M * v + (B * 10^K1)) * 10^(K2+adj)
1435 1.1 mlelstv *
1436 1.1 mlelstv * This commutes out to:
1437 1.1 mlelstv * y = L(M*v * 10^(K2+adj) + B * 10^(K1+K2+adj)); */
1438 1.1 mlelstv v1 = powx(FIX10, INT2FIX(K2 + adj));
1439 1.1 mlelstv v2 = powx(FIX10, INT2FIX(K1 + K2 + adj));
1440 1.1 mlelstv val = M * vs * v1 + B * v2;
1441 1.1 mlelstv
1442 1.1 mlelstv /* Linearization function: y = f(x) 0 : y = x 1 : y = ln(x) 2 : y =
1443 1.1 mlelstv * log10(x) 3 : y = log2(x) 4 : y = e^x 5 : y = 10^x 6 : y = 2^x 7 : y
1444 1.1 mlelstv * = 1/x 8 : y = x^2 9 : y = x^3 10 : y = square root(x) 11 : y = cube
1445 1.1 mlelstv * root(x) */
1446 1.1 mlelstv switch (s1->linear & 0x7f) {
1447 1.1 mlelstv case 0: break;
1448 1.1 mlelstv case 1: val = logx(val,FIX1LOGE); break;
1449 1.1 mlelstv case 2: val = logx(val,FIX1LOG10); break;
1450 1.1 mlelstv case 3: val = logx(val,FIX1LOG2); break;
1451 1.1 mlelstv case 4: val = powx(FIXE,val); break;
1452 1.1 mlelstv case 5: val = powx(FIX10,val); break;
1453 1.1 mlelstv case 6: val = powx(FIX2,val); break;
1454 1.1 mlelstv case 7: val = powx(val,FIXMONE); break;
1455 1.1 mlelstv case 8: val = powx(val,FIX2); break;
1456 1.1 mlelstv case 9: val = powx(val,FIX3); break;
1457 1.1 mlelstv case 10: val = powx(val,FIXHALF); break;
1458 1.1 mlelstv case 11: val = powx(val,FIXTHIRD); break;
1459 1.1 mlelstv }
1460 1.1 mlelstv
1461 1.1 mlelstv return FIX2INT(val);
1462 1.1 mlelstv }
1463 1.1 mlelstv
1464 1.1 mlelstv static int32_t
1465 1.1 mlelstv ipmi_convert_sensor(uint8_t *reading, struct ipmi_sensor *psensor)
1466 1.1 mlelstv {
1467 1.1 mlelstv struct sdrtype1 *s1 = (struct sdrtype1 *)psensor->i_sdr;
1468 1.1 mlelstv int32_t val;
1469 1.1 mlelstv
1470 1.1 mlelstv switch (psensor->i_envtype) {
1471 1.1 mlelstv case ENVSYS_STEMP:
1472 1.1 mlelstv val = ipmi_convert(reading[0], s1, 6) + 273150000;
1473 1.1 mlelstv break;
1474 1.1 mlelstv
1475 1.1 mlelstv case ENVSYS_SVOLTS_DC:
1476 1.1 mlelstv val = ipmi_convert(reading[0], s1, 6);
1477 1.1 mlelstv break;
1478 1.1 mlelstv
1479 1.1 mlelstv case ENVSYS_SFANRPM:
1480 1.1 mlelstv val = ipmi_convert(reading[0], s1, 0);
1481 1.1 mlelstv if (((s1->units1>>3)&0x7) == 0x3)
1482 1.1 mlelstv val *= 60; /* RPS -> RPM */
1483 1.1 mlelstv break;
1484 1.1 mlelstv default:
1485 1.1 mlelstv val = 0;
1486 1.1 mlelstv break;
1487 1.1 mlelstv }
1488 1.1 mlelstv return val;
1489 1.1 mlelstv }
1490 1.1 mlelstv
1491 1.1 mlelstv static void
1492 1.1 mlelstv ipmi_set_limits(struct sysmon_envsys *sme, envsys_data_t *edata,
1493 1.1 mlelstv sysmon_envsys_lim_t *limits, uint32_t *props)
1494 1.1 mlelstv {
1495 1.1 mlelstv struct ipmi_sensor *ipmi_s;
1496 1.1 mlelstv
1497 1.1 mlelstv /* Find the ipmi_sensor corresponding to this edata */
1498 1.1 mlelstv SLIST_FOREACH(ipmi_s, &ipmi_sensor_list, i_list) {
1499 1.1 mlelstv if (ipmi_s->i_envnum == edata->sensor) {
1500 1.1 mlelstv if (limits == NULL) {
1501 1.1 mlelstv limits = &ipmi_s->i_deflims;
1502 1.1 mlelstv props = &ipmi_s->i_defprops;
1503 1.1 mlelstv }
1504 1.1 mlelstv *props |= PROP_DRIVER_LIMITS;
1505 1.1 mlelstv ipmi_s->i_limits = *limits;
1506 1.1 mlelstv ipmi_s->i_props = *props;
1507 1.1 mlelstv return;
1508 1.1 mlelstv }
1509 1.1 mlelstv }
1510 1.1 mlelstv return;
1511 1.1 mlelstv }
1512 1.1 mlelstv
1513 1.1 mlelstv static void
1514 1.1 mlelstv ipmi_get_limits(struct sysmon_envsys *sme, envsys_data_t *edata,
1515 1.1 mlelstv sysmon_envsys_lim_t *limits, uint32_t *props)
1516 1.1 mlelstv {
1517 1.1 mlelstv struct ipmi_sensor *ipmi_s;
1518 1.1 mlelstv struct ipmi_softc *sc = sme->sme_cookie;
1519 1.1 mlelstv
1520 1.1 mlelstv /* Find the ipmi_sensor corresponding to this edata */
1521 1.1 mlelstv SLIST_FOREACH(ipmi_s, &ipmi_sensor_list, i_list) {
1522 1.1 mlelstv if (ipmi_s->i_envnum == edata->sensor) {
1523 1.1 mlelstv ipmi_get_sensor_limits(sc, ipmi_s, limits, props);
1524 1.1 mlelstv ipmi_s->i_limits = *limits;
1525 1.1 mlelstv ipmi_s->i_props = *props;
1526 1.1 mlelstv if (ipmi_s->i_defprops == 0) {
1527 1.1 mlelstv ipmi_s->i_defprops = *props;
1528 1.1 mlelstv ipmi_s->i_deflims = *limits;
1529 1.1 mlelstv }
1530 1.1 mlelstv return;
1531 1.1 mlelstv }
1532 1.1 mlelstv }
1533 1.1 mlelstv return;
1534 1.1 mlelstv }
1535 1.1 mlelstv
1536 1.1 mlelstv static void
1537 1.1 mlelstv ipmi_get_sensor_limits(struct ipmi_softc *sc, struct ipmi_sensor *psensor,
1538 1.1 mlelstv sysmon_envsys_lim_t *limits, uint32_t *props)
1539 1.1 mlelstv {
1540 1.1 mlelstv struct sdrtype1 *s1 = (struct sdrtype1 *)psensor->i_sdr;
1541 1.1 mlelstv bool failure;
1542 1.1 mlelstv int rxlen;
1543 1.1 mlelstv uint8_t data[32];
1544 1.1 mlelstv uint32_t prop_critmax, prop_warnmax, prop_critmin, prop_warnmin;
1545 1.1 mlelstv int32_t *pcritmax, *pwarnmax, *pcritmin, *pwarnmin;
1546 1.1 mlelstv
1547 1.1 mlelstv *props &= ~(PROP_CRITMIN | PROP_CRITMAX | PROP_WARNMIN | PROP_WARNMAX);
1548 1.1 mlelstv data[0] = psensor->i_num;
1549 1.1 mlelstv mutex_enter(&sc->sc_cmd_mtx);
1550 1.1 mlelstv failure =
1551 1.1 mlelstv ipmi_sendcmd(sc, s1->owner_id, s1->owner_lun,
1552 1.1 mlelstv SE_NETFN, SE_GET_SENSOR_THRESHOLD, 1, data) ||
1553 1.1 mlelstv ipmi_recvcmd(sc, sizeof(data), &rxlen, data);
1554 1.1 mlelstv mutex_exit(&sc->sc_cmd_mtx);
1555 1.1 mlelstv if (failure)
1556 1.1 mlelstv return;
1557 1.1 mlelstv
1558 1.1 mlelstv dbg_printf(25, "%s: %#.2x %#.2x %#.2x %#.2x %#.2x %#.2x %#.2x\n",
1559 1.1 mlelstv __func__, data[0], data[1], data[2], data[3], data[4], data[5],
1560 1.1 mlelstv data[6]);
1561 1.1 mlelstv
1562 1.1 mlelstv switch (s1->linear & 0x7f) {
1563 1.1 mlelstv case 7: /* 1/x sensor, exchange upper and lower limits */
1564 1.1 mlelstv prop_critmax = PROP_CRITMIN;
1565 1.1 mlelstv prop_warnmax = PROP_WARNMIN;
1566 1.1 mlelstv prop_critmin = PROP_CRITMAX;
1567 1.1 mlelstv prop_warnmin = PROP_WARNMAX;
1568 1.1 mlelstv pcritmax = &limits->sel_critmin;
1569 1.1 mlelstv pwarnmax = &limits->sel_warnmin;
1570 1.1 mlelstv pcritmin = &limits->sel_critmax;
1571 1.1 mlelstv pwarnmin = &limits->sel_warnmax;
1572 1.1 mlelstv break;
1573 1.1 mlelstv default:
1574 1.1 mlelstv prop_critmax = PROP_CRITMAX;
1575 1.1 mlelstv prop_warnmax = PROP_WARNMAX;
1576 1.1 mlelstv prop_critmin = PROP_CRITMIN;
1577 1.1 mlelstv prop_warnmin = PROP_WARNMIN;
1578 1.1 mlelstv pcritmax = &limits->sel_critmax;
1579 1.1 mlelstv pwarnmax = &limits->sel_warnmax;
1580 1.1 mlelstv pcritmin = &limits->sel_critmin;
1581 1.1 mlelstv pwarnmin = &limits->sel_warnmin;
1582 1.1 mlelstv break;
1583 1.1 mlelstv }
1584 1.1 mlelstv
1585 1.1 mlelstv if (data[0] & 0x20 && data[6] != 0xff) {
1586 1.1 mlelstv *pcritmax = ipmi_convert_sensor(&data[6], psensor);
1587 1.1 mlelstv *props |= prop_critmax;
1588 1.1 mlelstv }
1589 1.1 mlelstv if (data[0] & 0x10 && data[5] != 0xff) {
1590 1.1 mlelstv *pcritmax = ipmi_convert_sensor(&data[5], psensor);
1591 1.1 mlelstv *props |= prop_critmax;
1592 1.1 mlelstv }
1593 1.1 mlelstv if (data[0] & 0x08 && data[4] != 0xff) {
1594 1.1 mlelstv *pwarnmax = ipmi_convert_sensor(&data[4], psensor);
1595 1.1 mlelstv *props |= prop_warnmax;
1596 1.1 mlelstv }
1597 1.1 mlelstv if (data[0] & 0x04 && data[3] != 0x00) {
1598 1.1 mlelstv *pcritmin = ipmi_convert_sensor(&data[3], psensor);
1599 1.1 mlelstv *props |= prop_critmin;
1600 1.1 mlelstv }
1601 1.1 mlelstv if (data[0] & 0x02 && data[2] != 0x00) {
1602 1.1 mlelstv *pcritmin = ipmi_convert_sensor(&data[2], psensor);
1603 1.1 mlelstv *props |= prop_critmin;
1604 1.1 mlelstv }
1605 1.1 mlelstv if (data[0] & 0x01 && data[1] != 0x00) {
1606 1.1 mlelstv *pwarnmin = ipmi_convert_sensor(&data[1], psensor);
1607 1.1 mlelstv *props |= prop_warnmin;
1608 1.1 mlelstv }
1609 1.1 mlelstv return;
1610 1.1 mlelstv }
1611 1.1 mlelstv
1612 1.1 mlelstv static int
1613 1.1 mlelstv ipmi_sensor_status(struct ipmi_softc *sc, struct ipmi_sensor *psensor,
1614 1.1 mlelstv envsys_data_t *edata, uint8_t *reading)
1615 1.1 mlelstv {
1616 1.1 mlelstv int etype;
1617 1.1 mlelstv
1618 1.1 mlelstv /* Get reading of sensor */
1619 1.1 mlelstv edata->value_cur = ipmi_convert_sensor(reading, psensor);
1620 1.1 mlelstv
1621 1.1 mlelstv /* Return Sensor Status */
1622 1.1 mlelstv etype = (psensor->i_etype << 8) + psensor->i_stype;
1623 1.1 mlelstv switch (etype) {
1624 1.1 mlelstv case IPMI_SENSOR_TYPE_TEMP:
1625 1.1 mlelstv case IPMI_SENSOR_TYPE_VOLT:
1626 1.1 mlelstv case IPMI_SENSOR_TYPE_FAN:
1627 1.1 mlelstv if (psensor->i_props & PROP_CRITMAX &&
1628 1.1 mlelstv edata->value_cur > psensor->i_limits.sel_critmax)
1629 1.1 mlelstv return ENVSYS_SCRITOVER;
1630 1.1 mlelstv
1631 1.1 mlelstv if (psensor->i_props & PROP_WARNMAX &&
1632 1.1 mlelstv edata->value_cur > psensor->i_limits.sel_warnmax)
1633 1.1 mlelstv return ENVSYS_SWARNOVER;
1634 1.1 mlelstv
1635 1.5 nonaka if (psensor->i_props & PROP_CRITMIN &&
1636 1.5 nonaka edata->value_cur < psensor->i_limits.sel_critmin)
1637 1.5 nonaka return ENVSYS_SCRITUNDER;
1638 1.5 nonaka
1639 1.1 mlelstv if (psensor->i_props & PROP_WARNMIN &&
1640 1.1 mlelstv edata->value_cur < psensor->i_limits.sel_warnmin)
1641 1.1 mlelstv return ENVSYS_SWARNUNDER;
1642 1.1 mlelstv
1643 1.1 mlelstv break;
1644 1.1 mlelstv
1645 1.1 mlelstv case IPMI_SENSOR_TYPE_INTRUSION:
1646 1.1 mlelstv edata->value_cur = (reading[2] & 1) ? 0 : 1;
1647 1.1 mlelstv if (reading[2] & 0x1)
1648 1.1 mlelstv return ENVSYS_SCRITICAL;
1649 1.1 mlelstv break;
1650 1.1 mlelstv
1651 1.1 mlelstv case IPMI_SENSOR_TYPE_PWRSUPPLY:
1652 1.1 mlelstv /* Reading: 1 = present+powered, 0 = otherwise */
1653 1.1 mlelstv edata->value_cur = (reading[2] & 1) ? 0 : 1;
1654 1.1 mlelstv if (reading[2] & 0x10) {
1655 1.1 mlelstv /* XXX: Need envsys type for Power Supply types
1656 1.1 mlelstv * ok: power supply installed && powered
1657 1.1 mlelstv * warn: power supply installed && !powered
1658 1.1 mlelstv * crit: power supply !installed
1659 1.1 mlelstv */
1660 1.1 mlelstv return ENVSYS_SCRITICAL;
1661 1.1 mlelstv }
1662 1.1 mlelstv if (reading[2] & 0x08) {
1663 1.1 mlelstv /* Power supply AC lost */
1664 1.1 mlelstv return ENVSYS_SWARNOVER;
1665 1.1 mlelstv }
1666 1.1 mlelstv break;
1667 1.1 mlelstv }
1668 1.1 mlelstv
1669 1.1 mlelstv return ENVSYS_SVALID;
1670 1.1 mlelstv }
1671 1.1 mlelstv
1672 1.1 mlelstv static int
1673 1.1 mlelstv read_sensor(struct ipmi_softc *sc, struct ipmi_sensor *psensor)
1674 1.1 mlelstv {
1675 1.1 mlelstv struct sdrtype1 *s1 = (struct sdrtype1 *) psensor->i_sdr;
1676 1.1 mlelstv uint8_t data[8];
1677 1.1 mlelstv int rxlen;
1678 1.1 mlelstv envsys_data_t *edata = &sc->sc_sensor[psensor->i_envnum];
1679 1.1 mlelstv
1680 1.1 mlelstv memset(data, 0, sizeof(data));
1681 1.1 mlelstv data[0] = psensor->i_num;
1682 1.1 mlelstv
1683 1.1 mlelstv mutex_enter(&sc->sc_cmd_mtx);
1684 1.1 mlelstv if (ipmi_sendcmd(sc, s1->owner_id, s1->owner_lun, SE_NETFN,
1685 1.1 mlelstv SE_GET_SENSOR_READING, 1, data))
1686 1.1 mlelstv goto err;
1687 1.1 mlelstv
1688 1.1 mlelstv if (ipmi_recvcmd(sc, sizeof(data), &rxlen, data))
1689 1.1 mlelstv goto err;
1690 1.1 mlelstv mutex_exit(&sc->sc_cmd_mtx);
1691 1.1 mlelstv
1692 1.1 mlelstv dbg_printf(10, "m=%u, m_tolerance=%u, b=%u, b_accuracy=%u, "
1693 1.1 mlelstv "rbexp=%u, linear=%d\n", s1->m, s1->m_tolerance, s1->b,
1694 1.1 mlelstv s1->b_accuracy, s1->rbexp, s1->linear);
1695 1.1 mlelstv dbg_printf(10, "values=%#.2x %#.2x %#.2x %#.2x %s\n",
1696 1.1 mlelstv data[0],data[1],data[2],data[3], edata->desc);
1697 1.1 mlelstv if (IPMI_INVALID_SENSOR_P(data[1])) {
1698 1.1 mlelstv /* Check if sensor is valid */
1699 1.1 mlelstv edata->state = ENVSYS_SINVALID;
1700 1.1 mlelstv } else {
1701 1.1 mlelstv edata->state = ipmi_sensor_status(sc, psensor, edata, data);
1702 1.1 mlelstv }
1703 1.1 mlelstv return 0;
1704 1.1 mlelstv err:
1705 1.1 mlelstv mutex_exit(&sc->sc_cmd_mtx);
1706 1.1 mlelstv return -1;
1707 1.1 mlelstv }
1708 1.1 mlelstv
1709 1.1 mlelstv static int
1710 1.1 mlelstv ipmi_sensor_type(int type, int ext_type, int entity)
1711 1.1 mlelstv {
1712 1.1 mlelstv switch (ext_type << 8L | type) {
1713 1.1 mlelstv case IPMI_SENSOR_TYPE_TEMP:
1714 1.1 mlelstv return ENVSYS_STEMP;
1715 1.1 mlelstv
1716 1.1 mlelstv case IPMI_SENSOR_TYPE_VOLT:
1717 1.1 mlelstv return ENVSYS_SVOLTS_DC;
1718 1.1 mlelstv
1719 1.1 mlelstv case IPMI_SENSOR_TYPE_FAN:
1720 1.1 mlelstv return ENVSYS_SFANRPM;
1721 1.1 mlelstv
1722 1.1 mlelstv case IPMI_SENSOR_TYPE_PWRSUPPLY:
1723 1.1 mlelstv if (entity == IPMI_ENTITY_PWRSUPPLY)
1724 1.1 mlelstv return ENVSYS_INDICATOR;
1725 1.1 mlelstv break;
1726 1.1 mlelstv
1727 1.1 mlelstv case IPMI_SENSOR_TYPE_INTRUSION:
1728 1.1 mlelstv return ENVSYS_INDICATOR;
1729 1.1 mlelstv }
1730 1.1 mlelstv
1731 1.1 mlelstv return -1;
1732 1.1 mlelstv }
1733 1.1 mlelstv
1734 1.1 mlelstv /* Add Sensor to BSD Sysctl interface */
1735 1.1 mlelstv static int
1736 1.1 mlelstv add_sdr_sensor(struct ipmi_softc *sc, uint8_t *psdr)
1737 1.1 mlelstv {
1738 1.1 mlelstv int rc;
1739 1.1 mlelstv struct sdrtype1 *s1 = (struct sdrtype1 *)psdr;
1740 1.1 mlelstv struct sdrtype2 *s2 = (struct sdrtype2 *)psdr;
1741 1.1 mlelstv char name[64];
1742 1.1 mlelstv
1743 1.1 mlelstv switch (s1->sdrhdr.record_type) {
1744 1.1 mlelstv case IPMI_SDR_TYPEFULL:
1745 1.1 mlelstv ipmi_sensor_name(name, sizeof(name), s1->typelen, s1->name);
1746 1.1 mlelstv rc = add_child_sensors(sc, psdr, 1, s1->sensor_num,
1747 1.1 mlelstv s1->sensor_type, s1->event_code, 0, s1->entity_id, name);
1748 1.1 mlelstv break;
1749 1.1 mlelstv
1750 1.1 mlelstv case IPMI_SDR_TYPECOMPACT:
1751 1.1 mlelstv ipmi_sensor_name(name, sizeof(name), s2->typelen, s2->name);
1752 1.1 mlelstv rc = add_child_sensors(sc, psdr, s2->share1 & 0xF,
1753 1.1 mlelstv s2->sensor_num, s2->sensor_type, s2->event_code,
1754 1.1 mlelstv s2->share2 & 0x7F, s2->entity_id, name);
1755 1.1 mlelstv break;
1756 1.1 mlelstv
1757 1.1 mlelstv default:
1758 1.1 mlelstv return 0;
1759 1.1 mlelstv }
1760 1.1 mlelstv
1761 1.1 mlelstv return rc;
1762 1.1 mlelstv }
1763 1.1 mlelstv
1764 1.1 mlelstv static int
1765 1.1 mlelstv ipmi_is_dupname(char *name)
1766 1.1 mlelstv {
1767 1.1 mlelstv struct ipmi_sensor *ipmi_s;
1768 1.1 mlelstv
1769 1.1 mlelstv SLIST_FOREACH(ipmi_s, &ipmi_sensor_list, i_list) {
1770 1.1 mlelstv if (strcmp(ipmi_s->i_envdesc, name) == 0) {
1771 1.1 mlelstv return 1;
1772 1.1 mlelstv }
1773 1.1 mlelstv }
1774 1.1 mlelstv return 0;
1775 1.1 mlelstv }
1776 1.1 mlelstv
1777 1.1 mlelstv static int
1778 1.1 mlelstv add_child_sensors(struct ipmi_softc *sc, uint8_t *psdr, int count,
1779 1.1 mlelstv int sensor_num, int sensor_type, int ext_type, int sensor_base,
1780 1.1 mlelstv int entity, const char *name)
1781 1.1 mlelstv {
1782 1.1 mlelstv int typ, idx, dupcnt, c;
1783 1.1 mlelstv char *e;
1784 1.1 mlelstv struct ipmi_sensor *psensor;
1785 1.1 mlelstv struct sdrtype1 *s1 = (struct sdrtype1 *)psdr;
1786 1.1 mlelstv
1787 1.1 mlelstv typ = ipmi_sensor_type(sensor_type, ext_type, entity);
1788 1.1 mlelstv if (typ == -1) {
1789 1.1 mlelstv dbg_printf(5, "Unknown sensor type:%#.2x et:%#.2x sn:%#.2x "
1790 1.1 mlelstv "name:%s\n", sensor_type, ext_type, sensor_num, name);
1791 1.1 mlelstv return 0;
1792 1.1 mlelstv }
1793 1.1 mlelstv dupcnt = 0;
1794 1.1 mlelstv sc->sc_nsensors += count;
1795 1.1 mlelstv for (idx = 0; idx < count; idx++) {
1796 1.1 mlelstv psensor = malloc(sizeof(struct ipmi_sensor), M_DEVBUF,
1797 1.1 mlelstv M_WAITOK);
1798 1.1 mlelstv if (psensor == NULL)
1799 1.1 mlelstv break;
1800 1.1 mlelstv
1801 1.1 mlelstv memset(psensor, 0, sizeof(struct ipmi_sensor));
1802 1.1 mlelstv
1803 1.1 mlelstv /* Initialize BSD Sensor info */
1804 1.1 mlelstv psensor->i_sdr = psdr;
1805 1.1 mlelstv psensor->i_num = sensor_num + idx;
1806 1.1 mlelstv psensor->i_stype = sensor_type;
1807 1.1 mlelstv psensor->i_etype = ext_type;
1808 1.1 mlelstv psensor->i_envtype = typ;
1809 1.1 mlelstv if (count > 1)
1810 1.1 mlelstv snprintf(psensor->i_envdesc,
1811 1.1 mlelstv sizeof(psensor->i_envdesc),
1812 1.1 mlelstv "%s - %d", name, sensor_base + idx);
1813 1.1 mlelstv else
1814 1.1 mlelstv strlcpy(psensor->i_envdesc, name,
1815 1.1 mlelstv sizeof(psensor->i_envdesc));
1816 1.1 mlelstv
1817 1.1 mlelstv /*
1818 1.1 mlelstv * Check for duplicates. If there are duplicates,
1819 1.1 mlelstv * make sure there is space in the name (if not,
1820 1.1 mlelstv * truncate to make space) for a count (1-99) to
1821 1.1 mlelstv * add to make the name unique. If we run the
1822 1.1 mlelstv * counter out, just accept the duplicate (@name99)
1823 1.1 mlelstv * for now.
1824 1.1 mlelstv */
1825 1.1 mlelstv if (ipmi_is_dupname(psensor->i_envdesc)) {
1826 1.1 mlelstv if (strlen(psensor->i_envdesc) >=
1827 1.1 mlelstv sizeof(psensor->i_envdesc) - 3) {
1828 1.1 mlelstv e = psensor->i_envdesc +
1829 1.1 mlelstv sizeof(psensor->i_envdesc) - 3;
1830 1.1 mlelstv } else {
1831 1.1 mlelstv e = psensor->i_envdesc +
1832 1.1 mlelstv strlen(psensor->i_envdesc);
1833 1.1 mlelstv }
1834 1.1 mlelstv c = psensor->i_envdesc +
1835 1.1 mlelstv sizeof(psensor->i_envdesc) - e;
1836 1.1 mlelstv do {
1837 1.1 mlelstv dupcnt++;
1838 1.1 mlelstv snprintf(e, c, "%d", dupcnt);
1839 1.1 mlelstv } while (dupcnt < 100 &&
1840 1.1 mlelstv ipmi_is_dupname(psensor->i_envdesc));
1841 1.1 mlelstv }
1842 1.1 mlelstv
1843 1.1 mlelstv dbg_printf(5, "%s: %#.4x %#.2x:%d ent:%#.2x:%#.2x %s\n",
1844 1.1 mlelstv __func__,
1845 1.1 mlelstv s1->sdrhdr.record_id, s1->sensor_type,
1846 1.1 mlelstv typ, s1->entity_id, s1->entity_instance,
1847 1.1 mlelstv psensor->i_envdesc);
1848 1.1 mlelstv SLIST_INSERT_HEAD(&ipmi_sensor_list, psensor, i_list);
1849 1.1 mlelstv }
1850 1.1 mlelstv
1851 1.1 mlelstv return 1;
1852 1.1 mlelstv }
1853 1.1 mlelstv
1854 1.1 mlelstv #if 0
1855 1.1 mlelstv /* Interrupt handler */
1856 1.1 mlelstv static int
1857 1.1 mlelstv ipmi_intr(void *arg)
1858 1.1 mlelstv {
1859 1.1 mlelstv struct ipmi_softc *sc = (struct ipmi_softc *)arg;
1860 1.1 mlelstv int v;
1861 1.1 mlelstv
1862 1.1 mlelstv v = bmc_read(sc, _KCS_STATUS_REGISTER);
1863 1.1 mlelstv if (v & KCS_OBF)
1864 1.1 mlelstv ++ipmi_nintr;
1865 1.1 mlelstv
1866 1.1 mlelstv return 0;
1867 1.1 mlelstv }
1868 1.1 mlelstv #endif
1869 1.1 mlelstv
1870 1.1 mlelstv /* Handle IPMI Timer - reread sensor values */
1871 1.1 mlelstv static void
1872 1.1 mlelstv ipmi_refresh_sensors(struct ipmi_softc *sc)
1873 1.1 mlelstv {
1874 1.1 mlelstv
1875 1.1 mlelstv if (SLIST_EMPTY(&ipmi_sensor_list))
1876 1.1 mlelstv return;
1877 1.1 mlelstv
1878 1.1 mlelstv sc->current_sensor = SLIST_NEXT(sc->current_sensor, i_list);
1879 1.1 mlelstv if (sc->current_sensor == NULL)
1880 1.1 mlelstv sc->current_sensor = SLIST_FIRST(&ipmi_sensor_list);
1881 1.1 mlelstv
1882 1.1 mlelstv if (read_sensor(sc, sc->current_sensor)) {
1883 1.1 mlelstv dbg_printf(1, "%s: error reading\n", __func__);
1884 1.1 mlelstv }
1885 1.1 mlelstv }
1886 1.1 mlelstv
1887 1.1 mlelstv static int
1888 1.1 mlelstv ipmi_map_regs(struct ipmi_softc *sc, struct ipmi_attach_args *ia)
1889 1.1 mlelstv {
1890 1.1 mlelstv int error;
1891 1.1 mlelstv
1892 1.1 mlelstv sc->sc_if = ipmi_get_if(ia->iaa_if_type);
1893 1.1 mlelstv if (sc->sc_if == NULL)
1894 1.1 mlelstv return -1;
1895 1.1 mlelstv
1896 1.1 mlelstv if (ia->iaa_if_iotype == 'i')
1897 1.1 mlelstv sc->sc_iot = ia->iaa_iot;
1898 1.1 mlelstv else
1899 1.1 mlelstv sc->sc_iot = ia->iaa_memt;
1900 1.1 mlelstv
1901 1.1 mlelstv sc->sc_if_rev = ia->iaa_if_rev;
1902 1.1 mlelstv sc->sc_if_iospacing = ia->iaa_if_iospacing;
1903 1.1 mlelstv if ((error = bus_space_map(sc->sc_iot, ia->iaa_if_iobase,
1904 1.1 mlelstv sc->sc_if->nregs * sc->sc_if_iospacing, 0, &sc->sc_ioh)) != 0) {
1905 1.1 mlelstv const char *xname = sc->sc_dev ? device_xname(sc->sc_dev) :
1906 1.1 mlelstv "ipmi0";
1907 1.1 mlelstv aprint_error("%s: %s:bus_space_map(..., %" PRIx64 ", %x"
1908 1.1 mlelstv ", 0, %p) type %c failed %d\n",
1909 1.2 mlelstv xname, __func__, (uint64_t)ia->iaa_if_iobase,
1910 1.1 mlelstv sc->sc_if->nregs * sc->sc_if_iospacing, &sc->sc_ioh,
1911 1.1 mlelstv ia->iaa_if_iotype, error);
1912 1.1 mlelstv return -1;
1913 1.1 mlelstv }
1914 1.1 mlelstv #if 0
1915 1.1 mlelstv if (iaa->if_if_irq != -1)
1916 1.1 mlelstv sc->ih = isa_intr_establish(-1, iaa->if_if_irq,
1917 1.1 mlelstv iaa->if_irqlvl, IPL_BIO, ipmi_intr, sc,
1918 1.1 mlelstv device_xname(sc->sc_dev);
1919 1.1 mlelstv #endif
1920 1.1 mlelstv return 0;
1921 1.1 mlelstv }
1922 1.1 mlelstv
1923 1.1 mlelstv static void
1924 1.1 mlelstv ipmi_unmap_regs(struct ipmi_softc *sc)
1925 1.1 mlelstv {
1926 1.1 mlelstv bus_space_unmap(sc->sc_iot, sc->sc_ioh,
1927 1.1 mlelstv sc->sc_if->nregs * sc->sc_if_iospacing);
1928 1.1 mlelstv }
1929 1.1 mlelstv
1930 1.1 mlelstv static int
1931 1.1 mlelstv ipmi_match(device_t parent, cfdata_t cf, void *aux)
1932 1.1 mlelstv {
1933 1.1 mlelstv struct ipmi_softc sc;
1934 1.1 mlelstv struct ipmi_attach_args *ia = aux;
1935 1.1 mlelstv int rv = 0;
1936 1.1 mlelstv
1937 1.1 mlelstv memset(&sc, 0, sizeof(sc));
1938 1.1 mlelstv
1939 1.1 mlelstv /* Map registers */
1940 1.1 mlelstv if (ipmi_map_regs(&sc, ia) != 0)
1941 1.1 mlelstv return 0;
1942 1.1 mlelstv
1943 1.1 mlelstv sc.sc_if->probe(&sc);
1944 1.1 mlelstv
1945 1.1 mlelstv mutex_init(&sc.sc_cmd_mtx, MUTEX_DEFAULT, IPL_SOFTCLOCK);
1946 1.1 mlelstv cv_init(&sc.sc_cmd_sleep, "ipmimtch");
1947 1.1 mlelstv
1948 1.3 mlelstv if (ipmi_get_device_id(&sc, NULL) == 0)
1949 1.3 mlelstv rv = 1;
1950 1.3 mlelstv
1951 1.1 mlelstv cv_destroy(&sc.sc_cmd_sleep);
1952 1.1 mlelstv mutex_destroy(&sc.sc_cmd_mtx);
1953 1.1 mlelstv ipmi_unmap_regs(&sc);
1954 1.1 mlelstv
1955 1.1 mlelstv return rv;
1956 1.1 mlelstv }
1957 1.1 mlelstv
1958 1.1 mlelstv static void
1959 1.1 mlelstv ipmi_thread(void *cookie)
1960 1.1 mlelstv {
1961 1.1 mlelstv device_t self = cookie;
1962 1.1 mlelstv struct ipmi_softc *sc = device_private(self);
1963 1.1 mlelstv struct ipmi_attach_args *ia = &sc->sc_ia;
1964 1.1 mlelstv uint16_t rec;
1965 1.1 mlelstv struct ipmi_sensor *ipmi_s;
1966 1.3 mlelstv struct ipmi_device_id id;
1967 1.1 mlelstv int i;
1968 1.1 mlelstv
1969 1.1 mlelstv sc->sc_thread_running = true;
1970 1.1 mlelstv
1971 1.1 mlelstv /* setup ticker */
1972 1.1 mlelstv sc->sc_max_retries = hz * 90; /* 90 seconds max */
1973 1.1 mlelstv
1974 1.1 mlelstv /* Map registers */
1975 1.1 mlelstv ipmi_map_regs(sc, ia);
1976 1.1 mlelstv
1977 1.3 mlelstv memset(&id, 0, sizeof(id));
1978 1.3 mlelstv if (ipmi_get_device_id(sc, &id))
1979 1.3 mlelstv aprint_error_dev(self, "Failed to re-query device ID\n");
1980 1.3 mlelstv
1981 1.1 mlelstv /* Scan SDRs, add sensors to list */
1982 1.1 mlelstv for (rec = 0; rec != 0xFFFF;)
1983 1.1 mlelstv if (get_sdr(sc, rec, &rec))
1984 1.1 mlelstv break;
1985 1.1 mlelstv
1986 1.1 mlelstv /* allocate and fill sensor arrays */
1987 1.1 mlelstv sc->sc_sensor =
1988 1.1 mlelstv malloc(sizeof(envsys_data_t) * sc->sc_nsensors,
1989 1.1 mlelstv M_DEVBUF, M_WAITOK | M_ZERO);
1990 1.1 mlelstv if (sc->sc_sensor == NULL) {
1991 1.1 mlelstv aprint_error_dev(self, "can't allocate envsys_data_t\n");
1992 1.1 mlelstv kthread_exit(0);
1993 1.1 mlelstv }
1994 1.1 mlelstv
1995 1.1 mlelstv sc->sc_envsys = sysmon_envsys_create();
1996 1.1 mlelstv sc->sc_envsys->sme_cookie = sc;
1997 1.1 mlelstv sc->sc_envsys->sme_get_limits = ipmi_get_limits;
1998 1.1 mlelstv sc->sc_envsys->sme_set_limits = ipmi_set_limits;
1999 1.1 mlelstv
2000 1.1 mlelstv i = 0;
2001 1.1 mlelstv SLIST_FOREACH(ipmi_s, &ipmi_sensor_list, i_list) {
2002 1.1 mlelstv ipmi_s->i_props = 0;
2003 1.1 mlelstv ipmi_s->i_envnum = -1;
2004 1.1 mlelstv sc->sc_sensor[i].units = ipmi_s->i_envtype;
2005 1.1 mlelstv sc->sc_sensor[i].state = ENVSYS_SINVALID;
2006 1.1 mlelstv sc->sc_sensor[i].flags |= ENVSYS_FHAS_ENTROPY;
2007 1.1 mlelstv /*
2008 1.1 mlelstv * Monitor threshold limits in the sensors.
2009 1.1 mlelstv */
2010 1.1 mlelstv switch (sc->sc_sensor[i].units) {
2011 1.1 mlelstv case ENVSYS_STEMP:
2012 1.1 mlelstv case ENVSYS_SVOLTS_DC:
2013 1.1 mlelstv case ENVSYS_SFANRPM:
2014 1.1 mlelstv sc->sc_sensor[i].flags |= ENVSYS_FMONLIMITS;
2015 1.1 mlelstv break;
2016 1.1 mlelstv default:
2017 1.1 mlelstv sc->sc_sensor[i].flags |= ENVSYS_FMONCRITICAL;
2018 1.1 mlelstv }
2019 1.1 mlelstv (void)strlcpy(sc->sc_sensor[i].desc, ipmi_s->i_envdesc,
2020 1.1 mlelstv sizeof(sc->sc_sensor[i].desc));
2021 1.1 mlelstv ++i;
2022 1.1 mlelstv
2023 1.1 mlelstv if (sysmon_envsys_sensor_attach(sc->sc_envsys,
2024 1.1 mlelstv &sc->sc_sensor[i-1]))
2025 1.1 mlelstv continue;
2026 1.1 mlelstv
2027 1.1 mlelstv /* get reference number from envsys */
2028 1.1 mlelstv ipmi_s->i_envnum = sc->sc_sensor[i-1].sensor;
2029 1.1 mlelstv }
2030 1.1 mlelstv
2031 1.1 mlelstv sc->sc_envsys->sme_name = device_xname(sc->sc_dev);
2032 1.1 mlelstv sc->sc_envsys->sme_flags = SME_DISABLE_REFRESH;
2033 1.1 mlelstv
2034 1.1 mlelstv if (sysmon_envsys_register(sc->sc_envsys)) {
2035 1.1 mlelstv aprint_error_dev(self, "unable to register with sysmon\n");
2036 1.1 mlelstv sysmon_envsys_destroy(sc->sc_envsys);
2037 1.1 mlelstv }
2038 1.1 mlelstv
2039 1.1 mlelstv /* initialize sensor list for thread */
2040 1.1 mlelstv if (!SLIST_EMPTY(&ipmi_sensor_list))
2041 1.1 mlelstv sc->current_sensor = SLIST_FIRST(&ipmi_sensor_list);
2042 1.1 mlelstv
2043 1.1 mlelstv aprint_verbose_dev(self, "version %d.%d interface %s %sbase "
2044 1.1 mlelstv "0x%" PRIx64 "/%#x spacing %d\n",
2045 1.1 mlelstv ia->iaa_if_rev >> 4, ia->iaa_if_rev & 0xF, sc->sc_if->name,
2046 1.2 mlelstv ia->iaa_if_iotype == 'i' ? "io" : "mem",
2047 1.2 mlelstv (uint64_t)ia->iaa_if_iobase,
2048 1.1 mlelstv ia->iaa_if_iospacing * sc->sc_if->nregs, ia->iaa_if_iospacing);
2049 1.1 mlelstv if (ia->iaa_if_irq != -1)
2050 1.1 mlelstv aprint_verbose_dev(self, " irq %d\n", ia->iaa_if_irq);
2051 1.1 mlelstv
2052 1.3 mlelstv if (id.deviceid != 0) {
2053 1.3 mlelstv aprint_normal_dev(self, "ID %u.%u IPMI %x.%x%s%s\n",
2054 1.3 mlelstv id.deviceid, (id.revision & 0xf),
2055 1.3 mlelstv (id.version & 0xf), (id.version >> 4) & 0xf,
2056 1.3 mlelstv (id.fwrev1 & 0x80) ? " Initializing" : " Available",
2057 1.3 mlelstv (id.revision & 0x80) ? " +SDRs" : "");
2058 1.3 mlelstv if (id.additional != 0)
2059 1.3 mlelstv aprint_verbose_dev(self, "Additional%s%s%s%s%s%s%s%s\n",
2060 1.3 mlelstv (id.additional & 0x80) ? " Chassis" : "",
2061 1.3 mlelstv (id.additional & 0x40) ? " Bridge" : "",
2062 1.3 mlelstv (id.additional & 0x20) ? " IPMBGen" : "",
2063 1.3 mlelstv (id.additional & 0x10) ? " IPMBRcv" : "",
2064 1.3 mlelstv (id.additional & 0x08) ? " FRU" : "",
2065 1.3 mlelstv (id.additional & 0x04) ? " SEL" : "",
2066 1.3 mlelstv (id.additional & 0x02) ? " SDR" : "",
2067 1.3 mlelstv (id.additional & 0x01) ? " Sensor" : "");
2068 1.3 mlelstv aprint_verbose_dev(self, "Manufacturer %05x Product %04x\n",
2069 1.3 mlelstv (id.manufacturer[2] & 0xf) << 16
2070 1.3 mlelstv | id.manufacturer[1] << 8
2071 1.3 mlelstv | id.manufacturer[0],
2072 1.3 mlelstv id.product[1] << 8
2073 1.3 mlelstv | id.manufacturer[0]);
2074 1.3 mlelstv aprint_verbose_dev(self, "Firmware %u.%x\n",
2075 1.3 mlelstv (id.fwrev1 & 0x7f), id.fwrev2);
2076 1.3 mlelstv }
2077 1.3 mlelstv
2078 1.1 mlelstv /* setup flag to exclude iic */
2079 1.1 mlelstv ipmi_enabled = 1;
2080 1.1 mlelstv
2081 1.1 mlelstv /* Setup Watchdog timer */
2082 1.1 mlelstv sc->sc_wdog.smw_name = device_xname(sc->sc_dev);
2083 1.1 mlelstv sc->sc_wdog.smw_cookie = sc;
2084 1.1 mlelstv sc->sc_wdog.smw_setmode = ipmi_watchdog_setmode;
2085 1.1 mlelstv sc->sc_wdog.smw_tickle = ipmi_watchdog_tickle;
2086 1.1 mlelstv sysmon_wdog_register(&sc->sc_wdog);
2087 1.1 mlelstv
2088 1.1 mlelstv /* Set up a power handler so we can possibly sleep */
2089 1.1 mlelstv if (!pmf_device_register(self, ipmi_suspend, NULL))
2090 1.1 mlelstv aprint_error_dev(self, "couldn't establish a power handler\n");
2091 1.1 mlelstv
2092 1.1 mlelstv mutex_enter(&sc->sc_poll_mtx);
2093 1.1 mlelstv while (sc->sc_thread_running) {
2094 1.4 mlelstv while (sc->sc_mode == IPMI_MODE_COMMAND)
2095 1.4 mlelstv cv_wait(&sc->sc_mode_cv, &sc->sc_poll_mtx);
2096 1.4 mlelstv sc->sc_mode = IPMI_MODE_ENVSYS;
2097 1.4 mlelstv
2098 1.1 mlelstv if (sc->sc_tickle_due) {
2099 1.1 mlelstv ipmi_dotickle(sc);
2100 1.1 mlelstv sc->sc_tickle_due = false;
2101 1.1 mlelstv }
2102 1.4 mlelstv ipmi_refresh_sensors(sc);
2103 1.4 mlelstv
2104 1.4 mlelstv sc->sc_mode = IPMI_MODE_IDLE;
2105 1.4 mlelstv cv_broadcast(&sc->sc_mode_cv);
2106 1.4 mlelstv cv_timedwait(&sc->sc_poll_cv, &sc->sc_poll_mtx,
2107 1.4 mlelstv SENSOR_REFRESH_RATE);
2108 1.1 mlelstv }
2109 1.1 mlelstv mutex_exit(&sc->sc_poll_mtx);
2110 1.1 mlelstv self->dv_flags &= ~DVF_ATTACH_INPROGRESS;
2111 1.1 mlelstv kthread_exit(0);
2112 1.1 mlelstv }
2113 1.1 mlelstv
2114 1.1 mlelstv static void
2115 1.1 mlelstv ipmi_attach(device_t parent, device_t self, void *aux)
2116 1.1 mlelstv {
2117 1.1 mlelstv struct ipmi_softc *sc = device_private(self);
2118 1.1 mlelstv
2119 1.1 mlelstv sc->sc_ia = *(struct ipmi_attach_args *)aux;
2120 1.1 mlelstv sc->sc_dev = self;
2121 1.1 mlelstv aprint_naive("\n");
2122 1.1 mlelstv aprint_normal("\n");
2123 1.1 mlelstv
2124 1.1 mlelstv /* lock around read_sensor so that no one messes with the bmc regs */
2125 1.1 mlelstv mutex_init(&sc->sc_cmd_mtx, MUTEX_DEFAULT, IPL_SOFTCLOCK);
2126 1.1 mlelstv mutex_init(&sc->sc_sleep_mtx, MUTEX_DEFAULT, IPL_SOFTCLOCK);
2127 1.1 mlelstv cv_init(&sc->sc_cmd_sleep, "ipmicmd");
2128 1.1 mlelstv
2129 1.1 mlelstv mutex_init(&sc->sc_poll_mtx, MUTEX_DEFAULT, IPL_SOFTCLOCK);
2130 1.1 mlelstv cv_init(&sc->sc_poll_cv, "ipmipoll");
2131 1.4 mlelstv cv_init(&sc->sc_mode_cv, "ipmimode");
2132 1.1 mlelstv
2133 1.1 mlelstv if (kthread_create(PRI_NONE, 0, NULL, ipmi_thread, self,
2134 1.1 mlelstv &sc->sc_kthread, "%s", device_xname(self)) != 0) {
2135 1.1 mlelstv aprint_error_dev(self, "unable to create thread, disabled\n");
2136 1.1 mlelstv } else
2137 1.1 mlelstv self->dv_flags |= DVF_ATTACH_INPROGRESS;
2138 1.1 mlelstv }
2139 1.1 mlelstv
2140 1.1 mlelstv static int
2141 1.1 mlelstv ipmi_detach(device_t self, int flags)
2142 1.1 mlelstv {
2143 1.1 mlelstv struct ipmi_sensor *i;
2144 1.1 mlelstv int rc;
2145 1.1 mlelstv struct ipmi_softc *sc = device_private(self);
2146 1.1 mlelstv
2147 1.1 mlelstv mutex_enter(&sc->sc_poll_mtx);
2148 1.1 mlelstv sc->sc_thread_running = false;
2149 1.1 mlelstv cv_signal(&sc->sc_poll_cv);
2150 1.1 mlelstv mutex_exit(&sc->sc_poll_mtx);
2151 1.1 mlelstv
2152 1.1 mlelstv if ((rc = sysmon_wdog_unregister(&sc->sc_wdog)) != 0) {
2153 1.1 mlelstv if (rc == ERESTART)
2154 1.1 mlelstv rc = EINTR;
2155 1.1 mlelstv return rc;
2156 1.1 mlelstv }
2157 1.1 mlelstv
2158 1.1 mlelstv /* cancel any pending countdown */
2159 1.1 mlelstv sc->sc_wdog.smw_mode &= ~WDOG_MODE_MASK;
2160 1.1 mlelstv sc->sc_wdog.smw_mode |= WDOG_MODE_DISARMED;
2161 1.1 mlelstv sc->sc_wdog.smw_period = WDOG_PERIOD_DEFAULT;
2162 1.1 mlelstv
2163 1.1 mlelstv if ((rc = ipmi_watchdog_setmode(&sc->sc_wdog)) != 0)
2164 1.1 mlelstv return rc;
2165 1.1 mlelstv
2166 1.1 mlelstv ipmi_enabled = 0;
2167 1.1 mlelstv
2168 1.1 mlelstv if (sc->sc_envsys != NULL) {
2169 1.1 mlelstv /* _unregister also destroys */
2170 1.1 mlelstv sysmon_envsys_unregister(sc->sc_envsys);
2171 1.1 mlelstv sc->sc_envsys = NULL;
2172 1.1 mlelstv }
2173 1.1 mlelstv
2174 1.1 mlelstv while ((i = SLIST_FIRST(&ipmi_sensor_list)) != NULL) {
2175 1.1 mlelstv SLIST_REMOVE_HEAD(&ipmi_sensor_list, i_list);
2176 1.1 mlelstv free(i, M_DEVBUF);
2177 1.1 mlelstv }
2178 1.1 mlelstv
2179 1.1 mlelstv if (sc->sc_sensor != NULL) {
2180 1.1 mlelstv free(sc->sc_sensor, M_DEVBUF);
2181 1.1 mlelstv sc->sc_sensor = NULL;
2182 1.1 mlelstv }
2183 1.1 mlelstv
2184 1.1 mlelstv ipmi_unmap_regs(sc);
2185 1.1 mlelstv
2186 1.4 mlelstv cv_destroy(&sc->sc_mode_cv);
2187 1.1 mlelstv cv_destroy(&sc->sc_poll_cv);
2188 1.1 mlelstv mutex_destroy(&sc->sc_poll_mtx);
2189 1.1 mlelstv cv_destroy(&sc->sc_cmd_sleep);
2190 1.1 mlelstv mutex_destroy(&sc->sc_sleep_mtx);
2191 1.1 mlelstv mutex_destroy(&sc->sc_cmd_mtx);
2192 1.1 mlelstv
2193 1.1 mlelstv return 0;
2194 1.1 mlelstv }
2195 1.1 mlelstv
2196 1.1 mlelstv static int
2197 1.3 mlelstv ipmi_get_device_id(struct ipmi_softc *sc, struct ipmi_device_id *res)
2198 1.3 mlelstv {
2199 1.3 mlelstv uint8_t buf[32];
2200 1.3 mlelstv int len;
2201 1.3 mlelstv int rc;
2202 1.3 mlelstv
2203 1.3 mlelstv mutex_enter(&sc->sc_cmd_mtx);
2204 1.3 mlelstv /* Identify BMC device early to detect lying bios */
2205 1.3 mlelstv rc = ipmi_sendcmd(sc, BMC_SA, 0, APP_NETFN, APP_GET_DEVICE_ID, 0, NULL);
2206 1.3 mlelstv if (rc) {
2207 1.3 mlelstv dbg_printf(1, ": unable to send get device id "
2208 1.3 mlelstv "command\n");
2209 1.3 mlelstv goto done;
2210 1.3 mlelstv }
2211 1.3 mlelstv rc = ipmi_recvcmd(sc, sizeof(buf), &len, buf);
2212 1.3 mlelstv if (rc) {
2213 1.3 mlelstv dbg_printf(1, ": unable to retrieve device id\n");
2214 1.3 mlelstv }
2215 1.3 mlelstv done:
2216 1.3 mlelstv mutex_exit(&sc->sc_cmd_mtx);
2217 1.3 mlelstv
2218 1.3 mlelstv if (rc == 0 && res != NULL)
2219 1.3 mlelstv memcpy(res, buf, MIN(sizeof(*res), len));
2220 1.3 mlelstv
2221 1.3 mlelstv return rc;
2222 1.3 mlelstv }
2223 1.3 mlelstv
2224 1.3 mlelstv static int
2225 1.1 mlelstv ipmi_watchdog_setmode(struct sysmon_wdog *smwdog)
2226 1.1 mlelstv {
2227 1.1 mlelstv struct ipmi_softc *sc = smwdog->smw_cookie;
2228 1.1 mlelstv struct ipmi_get_watchdog gwdog;
2229 1.1 mlelstv struct ipmi_set_watchdog swdog;
2230 1.1 mlelstv int rc, len;
2231 1.1 mlelstv
2232 1.1 mlelstv if (smwdog->smw_period < 10)
2233 1.1 mlelstv return EINVAL;
2234 1.1 mlelstv if (smwdog->smw_period == WDOG_PERIOD_DEFAULT)
2235 1.1 mlelstv sc->sc_wdog.smw_period = 10;
2236 1.1 mlelstv else
2237 1.1 mlelstv sc->sc_wdog.smw_period = smwdog->smw_period;
2238 1.1 mlelstv
2239 1.1 mlelstv mutex_enter(&sc->sc_cmd_mtx);
2240 1.1 mlelstv /* see if we can properly task to the watchdog */
2241 1.1 mlelstv rc = ipmi_sendcmd(sc, BMC_SA, BMC_LUN, APP_NETFN,
2242 1.1 mlelstv APP_GET_WATCHDOG_TIMER, 0, NULL);
2243 1.1 mlelstv rc = ipmi_recvcmd(sc, sizeof(gwdog), &len, &gwdog);
2244 1.1 mlelstv mutex_exit(&sc->sc_cmd_mtx);
2245 1.1 mlelstv if (rc) {
2246 1.1 mlelstv aprint_error_dev(sc->sc_dev,
2247 1.1 mlelstv "APP_GET_WATCHDOG_TIMER returned %#x\n", rc);
2248 1.1 mlelstv return EIO;
2249 1.1 mlelstv }
2250 1.1 mlelstv
2251 1.1 mlelstv memset(&swdog, 0, sizeof(swdog));
2252 1.1 mlelstv /* Period is 10ths/sec */
2253 1.1 mlelstv swdog.wdog_timeout = htole16(sc->sc_wdog.smw_period * 10);
2254 1.1 mlelstv if ((smwdog->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED)
2255 1.1 mlelstv swdog.wdog_action = IPMI_WDOG_ACT_DISABLED;
2256 1.1 mlelstv else
2257 1.1 mlelstv swdog.wdog_action = IPMI_WDOG_ACT_RESET;
2258 1.1 mlelstv swdog.wdog_use = IPMI_WDOG_USE_USE_OS;
2259 1.1 mlelstv
2260 1.1 mlelstv mutex_enter(&sc->sc_cmd_mtx);
2261 1.1 mlelstv if ((rc = ipmi_sendcmd(sc, BMC_SA, BMC_LUN, APP_NETFN,
2262 1.1 mlelstv APP_SET_WATCHDOG_TIMER, sizeof(swdog), &swdog)) == 0)
2263 1.1 mlelstv rc = ipmi_recvcmd(sc, 0, &len, NULL);
2264 1.1 mlelstv mutex_exit(&sc->sc_cmd_mtx);
2265 1.1 mlelstv if (rc) {
2266 1.1 mlelstv aprint_error_dev(sc->sc_dev,
2267 1.1 mlelstv "APP_SET_WATCHDOG_TIMER returned %#x\n", rc);
2268 1.1 mlelstv return EIO;
2269 1.1 mlelstv }
2270 1.1 mlelstv
2271 1.1 mlelstv return 0;
2272 1.1 mlelstv }
2273 1.1 mlelstv
2274 1.1 mlelstv static int
2275 1.1 mlelstv ipmi_watchdog_tickle(struct sysmon_wdog *smwdog)
2276 1.1 mlelstv {
2277 1.1 mlelstv struct ipmi_softc *sc = smwdog->smw_cookie;
2278 1.1 mlelstv
2279 1.1 mlelstv mutex_enter(&sc->sc_poll_mtx);
2280 1.1 mlelstv sc->sc_tickle_due = true;
2281 1.1 mlelstv cv_signal(&sc->sc_poll_cv);
2282 1.1 mlelstv mutex_exit(&sc->sc_poll_mtx);
2283 1.1 mlelstv return 0;
2284 1.1 mlelstv }
2285 1.1 mlelstv
2286 1.1 mlelstv static void
2287 1.1 mlelstv ipmi_dotickle(struct ipmi_softc *sc)
2288 1.1 mlelstv {
2289 1.1 mlelstv int rc, len;
2290 1.1 mlelstv
2291 1.1 mlelstv mutex_enter(&sc->sc_cmd_mtx);
2292 1.1 mlelstv /* tickle the watchdog */
2293 1.1 mlelstv if ((rc = ipmi_sendcmd(sc, BMC_SA, BMC_LUN, APP_NETFN,
2294 1.1 mlelstv APP_RESET_WATCHDOG, 0, NULL)) == 0)
2295 1.1 mlelstv rc = ipmi_recvcmd(sc, 0, &len, NULL);
2296 1.1 mlelstv mutex_exit(&sc->sc_cmd_mtx);
2297 1.1 mlelstv if (rc != 0) {
2298 1.1 mlelstv aprint_error_dev(sc->sc_dev, "watchdog tickle returned %#x\n",
2299 1.1 mlelstv rc);
2300 1.1 mlelstv }
2301 1.1 mlelstv }
2302 1.1 mlelstv
2303 1.1 mlelstv static bool
2304 1.1 mlelstv ipmi_suspend(device_t dev, const pmf_qual_t *qual)
2305 1.1 mlelstv {
2306 1.1 mlelstv struct ipmi_softc *sc = device_private(dev);
2307 1.1 mlelstv
2308 1.1 mlelstv /* Don't allow suspend if watchdog is armed */
2309 1.1 mlelstv if ((sc->sc_wdog.smw_mode & WDOG_MODE_MASK) != WDOG_MODE_DISARMED)
2310 1.1 mlelstv return false;
2311 1.1 mlelstv return true;
2312 1.1 mlelstv }
2313 1.4 mlelstv
2314 1.4 mlelstv static int
2315 1.4 mlelstv ipmi_open(dev_t dev, int flag, int fmt, lwp_t *l)
2316 1.4 mlelstv {
2317 1.4 mlelstv return 0;
2318 1.4 mlelstv }
2319 1.4 mlelstv
2320 1.4 mlelstv static int
2321 1.4 mlelstv ipmi_close(dev_t dev, int flag, int fmt, lwp_t *l)
2322 1.4 mlelstv {
2323 1.4 mlelstv struct ipmi_softc *sc;
2324 1.4 mlelstv int unit;
2325 1.4 mlelstv
2326 1.4 mlelstv unit = IPMIUNIT(dev);
2327 1.4 mlelstv if ((sc = device_lookup_private(&ipmi_cd, unit)) == NULL)
2328 1.4 mlelstv return (ENXIO);
2329 1.4 mlelstv
2330 1.4 mlelstv mutex_enter(&sc->sc_poll_mtx);
2331 1.4 mlelstv if (sc->sc_mode == IPMI_MODE_COMMAND) {
2332 1.4 mlelstv sc->sc_mode = IPMI_MODE_IDLE;
2333 1.4 mlelstv cv_broadcast(&sc->sc_mode_cv);
2334 1.4 mlelstv }
2335 1.4 mlelstv mutex_exit(&sc->sc_poll_mtx);
2336 1.4 mlelstv return 0;
2337 1.4 mlelstv }
2338 1.4 mlelstv
2339 1.4 mlelstv static int
2340 1.4 mlelstv ipmi_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
2341 1.4 mlelstv {
2342 1.4 mlelstv struct ipmi_softc *sc;
2343 1.4 mlelstv int unit, error = 0, len;
2344 1.4 mlelstv struct ipmi_req *req;
2345 1.4 mlelstv struct ipmi_recv *recv;
2346 1.4 mlelstv struct ipmi_addr addr;
2347 1.4 mlelstv unsigned char ccode, *buf = NULL;
2348 1.4 mlelstv
2349 1.4 mlelstv unit = IPMIUNIT(dev);
2350 1.4 mlelstv if ((sc = device_lookup_private(&ipmi_cd, unit)) == NULL)
2351 1.4 mlelstv return (ENXIO);
2352 1.4 mlelstv
2353 1.4 mlelstv switch (cmd) {
2354 1.4 mlelstv case IPMICTL_SEND_COMMAND:
2355 1.4 mlelstv mutex_enter(&sc->sc_poll_mtx);
2356 1.4 mlelstv while (sc->sc_mode == IPMI_MODE_ENVSYS) {
2357 1.4 mlelstv error = cv_wait_sig(&sc->sc_mode_cv, &sc->sc_poll_mtx);
2358 1.4 mlelstv if (error == EINTR) {
2359 1.4 mlelstv mutex_exit(&sc->sc_poll_mtx);
2360 1.4 mlelstv return error;
2361 1.4 mlelstv }
2362 1.4 mlelstv }
2363 1.4 mlelstv sc->sc_mode = IPMI_MODE_COMMAND;
2364 1.4 mlelstv mutex_exit(&sc->sc_poll_mtx);
2365 1.4 mlelstv break;
2366 1.4 mlelstv }
2367 1.4 mlelstv
2368 1.4 mlelstv mutex_enter(&sc->sc_cmd_mtx);
2369 1.4 mlelstv
2370 1.4 mlelstv switch (cmd) {
2371 1.4 mlelstv case IPMICTL_SEND_COMMAND:
2372 1.4 mlelstv req = data;
2373 1.4 mlelstv buf = malloc(IPMI_MAX_RX, M_DEVBUF, M_WAITOK);
2374 1.4 mlelstv
2375 1.4 mlelstv len = req->msg.data_len;
2376 1.4 mlelstv if (len < 0 || len > IPMI_MAX_RX) {
2377 1.4 mlelstv error = EINVAL;
2378 1.4 mlelstv break;
2379 1.4 mlelstv }
2380 1.4 mlelstv
2381 1.4 mlelstv /* clear pending result */
2382 1.4 mlelstv if (sc->sc_sent)
2383 1.4 mlelstv (void)ipmi_recvcmd(sc, IPMI_MAX_RX, &len, buf);
2384 1.4 mlelstv
2385 1.4 mlelstv /* XXX */
2386 1.4 mlelstv error = copyin(req->addr, &addr, sizeof(addr));
2387 1.4 mlelstv if (error)
2388 1.4 mlelstv break;
2389 1.4 mlelstv
2390 1.4 mlelstv error = copyin(req->msg.data, buf, len);
2391 1.4 mlelstv if (error)
2392 1.4 mlelstv break;
2393 1.4 mlelstv
2394 1.4 mlelstv /* save for receive */
2395 1.4 mlelstv sc->sc_msgid = req->msgid;
2396 1.4 mlelstv sc->sc_netfn = req->msg.netfn;
2397 1.4 mlelstv sc->sc_cmd = req->msg.cmd;
2398 1.4 mlelstv
2399 1.4 mlelstv if (ipmi_sendcmd(sc, BMC_SA, 0, req->msg.netfn,
2400 1.4 mlelstv req->msg.cmd, len, buf)) {
2401 1.4 mlelstv error = EIO;
2402 1.4 mlelstv break;
2403 1.4 mlelstv }
2404 1.4 mlelstv sc->sc_sent = true;
2405 1.4 mlelstv break;
2406 1.4 mlelstv case IPMICTL_RECEIVE_MSG_TRUNC:
2407 1.4 mlelstv case IPMICTL_RECEIVE_MSG:
2408 1.4 mlelstv recv = data;
2409 1.4 mlelstv buf = malloc(IPMI_MAX_RX, M_DEVBUF, M_WAITOK);
2410 1.4 mlelstv
2411 1.4 mlelstv if (recv->msg.data_len < 1) {
2412 1.4 mlelstv error = EINVAL;
2413 1.4 mlelstv break;
2414 1.4 mlelstv }
2415 1.4 mlelstv
2416 1.4 mlelstv /* XXX */
2417 1.4 mlelstv error = copyin(recv->addr, &addr, sizeof(addr));
2418 1.4 mlelstv if (error)
2419 1.4 mlelstv break;
2420 1.4 mlelstv
2421 1.4 mlelstv
2422 1.4 mlelstv if (!sc->sc_sent) {
2423 1.4 mlelstv error = EIO;
2424 1.4 mlelstv break;
2425 1.4 mlelstv }
2426 1.4 mlelstv
2427 1.4 mlelstv len = 0;
2428 1.4 mlelstv error = ipmi_recvcmd(sc, IPMI_MAX_RX, &len, buf);
2429 1.4 mlelstv if (error < 0) {
2430 1.4 mlelstv error = EIO;
2431 1.4 mlelstv break;
2432 1.4 mlelstv }
2433 1.4 mlelstv ccode = (unsigned char)error;
2434 1.4 mlelstv sc->sc_sent = false;
2435 1.4 mlelstv
2436 1.4 mlelstv if (len > recv->msg.data_len - 1) {
2437 1.4 mlelstv if (cmd == IPMICTL_RECEIVE_MSG) {
2438 1.4 mlelstv error = EMSGSIZE;
2439 1.4 mlelstv break;
2440 1.4 mlelstv }
2441 1.4 mlelstv len = recv->msg.data_len - 1;
2442 1.4 mlelstv }
2443 1.4 mlelstv
2444 1.4 mlelstv addr.channel = IPMI_BMC_CHANNEL;
2445 1.4 mlelstv
2446 1.4 mlelstv recv->recv_type = IPMI_RESPONSE_RECV_TYPE;
2447 1.4 mlelstv recv->msgid = sc->sc_msgid;
2448 1.4 mlelstv recv->msg.netfn = sc->sc_netfn;
2449 1.4 mlelstv recv->msg.cmd = sc->sc_cmd;
2450 1.4 mlelstv recv->msg.data_len = len+1;
2451 1.4 mlelstv
2452 1.4 mlelstv error = copyout(&addr, recv->addr, sizeof(addr));
2453 1.4 mlelstv if (error == 0)
2454 1.4 mlelstv error = copyout(&ccode, recv->msg.data, 1);
2455 1.4 mlelstv if (error == 0)
2456 1.4 mlelstv error = copyout(buf, recv->msg.data+1, len);
2457 1.4 mlelstv break;
2458 1.4 mlelstv case IPMICTL_SET_MY_ADDRESS_CMD:
2459 1.4 mlelstv sc->sc_address = *(int *)data;
2460 1.4 mlelstv break;
2461 1.4 mlelstv case IPMICTL_GET_MY_ADDRESS_CMD:
2462 1.4 mlelstv *(int *)data = sc->sc_address;
2463 1.4 mlelstv break;
2464 1.4 mlelstv case IPMICTL_SET_MY_LUN_CMD:
2465 1.4 mlelstv sc->sc_lun = *(int *)data & 0x3;
2466 1.4 mlelstv break;
2467 1.4 mlelstv case IPMICTL_GET_MY_LUN_CMD:
2468 1.4 mlelstv *(int *)data = sc->sc_lun;
2469 1.4 mlelstv break;
2470 1.4 mlelstv case IPMICTL_SET_GETS_EVENTS_CMD:
2471 1.4 mlelstv break;
2472 1.4 mlelstv case IPMICTL_REGISTER_FOR_CMD:
2473 1.4 mlelstv case IPMICTL_UNREGISTER_FOR_CMD:
2474 1.4 mlelstv error = EOPNOTSUPP;
2475 1.4 mlelstv break;
2476 1.4 mlelstv default:
2477 1.4 mlelstv error = ENODEV;
2478 1.4 mlelstv break;
2479 1.4 mlelstv }
2480 1.4 mlelstv
2481 1.4 mlelstv if (buf)
2482 1.4 mlelstv free(buf, M_DEVBUF);
2483 1.4 mlelstv
2484 1.4 mlelstv mutex_exit(&sc->sc_cmd_mtx);
2485 1.4 mlelstv
2486 1.4 mlelstv switch (cmd) {
2487 1.4 mlelstv case IPMICTL_RECEIVE_MSG:
2488 1.4 mlelstv case IPMICTL_RECEIVE_MSG_TRUNC:
2489 1.4 mlelstv mutex_enter(&sc->sc_poll_mtx);
2490 1.4 mlelstv sc->sc_mode = IPMI_MODE_IDLE;
2491 1.4 mlelstv cv_broadcast(&sc->sc_mode_cv);
2492 1.4 mlelstv mutex_exit(&sc->sc_poll_mtx);
2493 1.4 mlelstv break;
2494 1.4 mlelstv }
2495 1.4 mlelstv
2496 1.4 mlelstv return error;
2497 1.4 mlelstv }
2498 1.4 mlelstv
2499 1.4 mlelstv static int
2500 1.4 mlelstv ipmi_poll(dev_t dev, int events, lwp_t *l)
2501 1.4 mlelstv {
2502 1.4 mlelstv struct ipmi_softc *sc;
2503 1.4 mlelstv int unit, revents = 0;
2504 1.4 mlelstv
2505 1.4 mlelstv unit = IPMIUNIT(dev);
2506 1.4 mlelstv if ((sc = device_lookup_private(&ipmi_cd, unit)) == NULL)
2507 1.4 mlelstv return (ENXIO);
2508 1.4 mlelstv
2509 1.4 mlelstv mutex_enter(&sc->sc_cmd_mtx);
2510 1.4 mlelstv if (events & (POLLIN | POLLRDNORM)) {
2511 1.4 mlelstv if (sc->sc_sent)
2512 1.4 mlelstv revents |= events & (POLLIN | POLLRDNORM);
2513 1.4 mlelstv }
2514 1.4 mlelstv mutex_exit(&sc->sc_cmd_mtx);
2515 1.4 mlelstv
2516 1.4 mlelstv return revents;
2517 1.4 mlelstv }
2518