/*-
* Copyright (c) 2011 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Juergen Hannken-Illjes.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
if (!pmf_device_register(self, NULL, NULL))
aprint_error_dev(self, "couldn't establish power handler\n");
if (ibmhawk_request(sc, IHR_NAME, &resp)) {
aprint_error_dev(self, "communication failed\n");
return;
}
aprint_normal(": IBM Hawk \"%.16s\"\n", resp.ihr_name);
if (ibmhawk_request(sc, IHR_EQUIP, &resp)) {
aprint_error_dev(sc->sc_dev, "equip query failed\n");
return;
}
sc->sc_numcpus = uimin(resp.ihr_numcpus, IBMHAWK_MAX_CPU);
sc->sc_numfans = uimin(resp.ihr_numfans, IBMHAWK_MAX_FAN);
#if IBMHAWK_DEBUG > 0
aprint_normal_dev(sc->sc_dev, "monitoring %d/%d cpu(s) %d/%d fan(s)\n",
sc->sc_numcpus, resp.ihr_numcpus, sc->sc_numfans, resp.ihr_numfans);
#endif
/* Request and set sensor thresholds. */
if (ibmhawk_request(sc, IHR_TEMP_THR, &resp)) {
aprint_error_dev(sc->sc_dev, "temp threshold query failed\n");
return;
}
for (i = 0; i < sc->sc_numcpus; i++)
sc->sc_sensordata[IBMHAWK_T_CPU+i].ihs_warnmax =
resp.ihr_t_warn_thr;
if (ibmhawk_request(sc, IHR_VOLT_THR, &resp)) {
aprint_error_dev(sc->sc_dev, "volt threshold query failed\n");
return;
}
for (i = 0; i < IBMHAWK_MAX_VOLTAGE; i++) {
sc->sc_sensordata[IBMHAWK_V_VOLTAGE+i].ihs_warnmax =
bswap16(resp.ihr_v_voltage_thr[i*2]);
sc->sc_sensordata[IBMHAWK_V_VOLTAGE+i].ihs_warnmin =
bswap16(resp.ihr_v_voltage_thr[i*2+1]);
}
if ((sc->sc_sme = sysmon_envsys_create()) == NULL) {
aprint_error_dev(sc->sc_dev, "sysmon_envsys_create failed\n");
return;
}
ibmhawk_refreshall(sc, true);
sc->sc_sme->sme_name = device_xname(sc->sc_dev);
sc->sc_sme->sme_cookie = sc;
sc->sc_sme->sme_refresh = ibmhawk_refresh;
sc->sc_sme->sme_get_limits = ibmhawk_get_limits;
if (sysmon_envsys_register(sc->sc_sme)) {
aprint_error_dev(sc->sc_dev, "sysmon_envsys_register failed\n");
sysmon_envsys_destroy(sc->sc_sme);
sc->sc_sme = NULL;
return;
}
}
static int
ibmhawk_detach(device_t self, int flags)
{
struct ibmhawk_softc *sc = device_private(self);
if (sc->sc_sme)
sysmon_envsys_destroy(sc->sc_sme);
return 0;
}
/*
* Compute the message checksum.
*/
static uint8_t
ibmhawk_cksum(uint8_t *buf)
{
int len = *buf++;
int s = 0;
while (--len > 0)
s += *buf++;
return -s;
}
/*
* Request information from the management processor.
* The response will be zeroed on error.
* Request and response have the form <n> <data 0:n-2> <checksum>.
*/
static int
ibmhawk_request(struct ibmhawk_softc *sc, uint8_t request,
ibmhawk_response_t *response)
{
int i, error, retries;
uint8_t buf[sizeof(ibmhawk_response_t)+3], dummy;
error = EIO; /* Fail until we have a valid response. */
retries = 0;
if (iic_acquire_bus(sc->sc_tag, 0))
return error;
again:
memset(response, 0, sizeof(*response));
/* Build and send the request. */
buf[0] = 2;
buf[1] = request;
buf[2] = ibmhawk_cksum(buf);
#if IBMHAWK_DEBUG > 1
printf("[");
for (i = 0; i < 3; i++)
printf(" %02x", buf[i]);
printf(" ]");
#endif
for (i = 0; i < 3; i++)
if (iic_smbus_send_byte(sc->sc_tag, sc->sc_addr, buf[i], 0))
goto bad;
/* Receive and check the response. */
#if IBMHAWK_DEBUG > 1
printf(" => [");
#endif
if (iic_smbus_receive_byte(sc->sc_tag, sc->sc_addr, &buf[0], 0))
goto bad;
if (buf[0] == 0 || buf[0] == 255)
goto bad;
for (i = 1; i < buf[0]+1; i++)
if (iic_smbus_receive_byte(sc->sc_tag, sc->sc_addr,
(i < sizeof buf ? &buf[i] : &dummy), 0))
goto bad;
if (buf[0] >= sizeof(buf) || buf[1] != request ||
ibmhawk_cksum(buf) != buf[buf[0]])
goto bad;
if (buf[0] > 2)
memcpy(response, buf+2, buf[0]-2);
error = 0;
bad:
#if IBMHAWK_DEBUG > 1
for (i = 0; i < uimin(buf[0]+1, sizeof buf); i++)
printf(" %02x", buf[i]);
printf(" ] => %d\n", error);
#endif
if (error != 0 && retries++ < 3)
goto again;