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