19113a79cSeschrock /*
29113a79cSeschrock  * CDDL HEADER START
39113a79cSeschrock  *
49113a79cSeschrock  * The contents of this file are subject to the terms of the
59113a79cSeschrock  * Common Development and Distribution License (the "License").
69113a79cSeschrock  * You may not use this file except in compliance with the License.
79113a79cSeschrock  *
89113a79cSeschrock  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
99113a79cSeschrock  * or http://www.opensolaris.org/os/licensing.
109113a79cSeschrock  * See the License for the specific language governing permissions
119113a79cSeschrock  * and limitations under the License.
129113a79cSeschrock  *
139113a79cSeschrock  * When distributing Covered Code, include this CDDL HEADER in each
149113a79cSeschrock  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
159113a79cSeschrock  * If applicable, add the following below this CDDL HEADER, with the
169113a79cSeschrock  * fields enclosed by brackets "[]" replaced with your own identifying
179113a79cSeschrock  * information: Portions Copyright [yyyy] [name of copyright owner]
189113a79cSeschrock  *
199113a79cSeschrock  * CDDL HEADER END
209113a79cSeschrock  */
219113a79cSeschrock /*
22918a0d8aSrobj  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
239113a79cSeschrock  * Use is subject to license terms.
249113a79cSeschrock  */
25*208e1562SRob Johnston /*
26*208e1562SRob Johnston  * Copyright (c) 2018, Joyent, Inc.
27*208e1562SRob Johnston  */
289113a79cSeschrock 
299113a79cSeschrock #include <libipmi.h>
309113a79cSeschrock #include <string.h>
319113a79cSeschrock 
329113a79cSeschrock #include "ipmi_impl.h"
339113a79cSeschrock 
349113a79cSeschrock ipmi_sensor_reading_t *
ipmi_get_sensor_reading(ipmi_handle_t * ihp,uint8_t id)359113a79cSeschrock ipmi_get_sensor_reading(ipmi_handle_t *ihp, uint8_t id)
369113a79cSeschrock {
379113a79cSeschrock 	ipmi_cmd_t cmd, *resp;
389113a79cSeschrock 	ipmi_sensor_reading_t *srp;
399113a79cSeschrock 
409113a79cSeschrock 	cmd.ic_netfn = IPMI_NETFN_SE;
419113a79cSeschrock 	cmd.ic_cmd = IPMI_CMD_GET_SENSOR_READING;
429113a79cSeschrock 	cmd.ic_lun = 0;
439113a79cSeschrock 	cmd.ic_data = &id;
449113a79cSeschrock 	cmd.ic_dlen = sizeof (id);
459113a79cSeschrock 
469113a79cSeschrock 	if ((resp = ipmi_send(ihp, &cmd)) == NULL)
479113a79cSeschrock 		return (NULL);
489113a79cSeschrock 
499113a79cSeschrock 	/*
509113a79cSeschrock 	 * The upper half of the state field is optional, so if it's not
519113a79cSeschrock 	 * present, then set it to zero.  We also need to convert to the
529113a79cSeschrock 	 * native endianness.
539113a79cSeschrock 	 */
549113a79cSeschrock 	if (resp->ic_dlen < sizeof (ipmi_sensor_reading_t) - sizeof (uint8_t)) {
559113a79cSeschrock 		(void) ipmi_set_error(ihp, EIPMI_BAD_RESPONSE_LENGTH, NULL);
569113a79cSeschrock 		return (NULL);
579113a79cSeschrock 	}
589113a79cSeschrock 	srp = resp->ic_data;
599113a79cSeschrock 
609113a79cSeschrock 	if (resp->ic_dlen < sizeof (ipmi_sensor_reading_t))
619113a79cSeschrock 		(void) memset((char *)srp + resp->ic_dlen, '\0',
629113a79cSeschrock 		    sizeof (ipmi_sensor_reading_t) - resp->ic_dlen);
639113a79cSeschrock 
64918a0d8aSrobj 	srp->isr_state = LE_IN16(&srp->isr_state);
659113a79cSeschrock 	return (srp);
669113a79cSeschrock }
679113a79cSeschrock 
689113a79cSeschrock int
ipmi_set_sensor_reading(ipmi_handle_t * ihp,ipmi_set_sensor_reading_t * req)699113a79cSeschrock ipmi_set_sensor_reading(ipmi_handle_t *ihp, ipmi_set_sensor_reading_t *req)
709113a79cSeschrock {
719113a79cSeschrock 	ipmi_set_sensor_reading_t realreq;
729113a79cSeschrock 	ipmi_cmd_t cmd, *resp;
73e1a24155Srobj 	uint16_t tmp;
749113a79cSeschrock 
759113a79cSeschrock 	/*
769113a79cSeschrock 	 * Convert states to little endian.
779113a79cSeschrock 	 */
789113a79cSeschrock 	(void) memcpy(&realreq, req, sizeof (realreq));
799113a79cSeschrock 
80e1a24155Srobj 	tmp = LE_IN16(&realreq.iss_assert_state);
81e1a24155Srobj 	(void) memcpy(&realreq.iss_assert_state, &tmp, sizeof (tmp));
82e1a24155Srobj 	tmp = LE_IN16(&realreq.iss_deassert_state);
83e1a24155Srobj 	(void) memcpy(&realreq.iss_deassert_state, &tmp, sizeof (tmp));
849113a79cSeschrock 
859113a79cSeschrock 	cmd.ic_netfn = IPMI_NETFN_SE;
869113a79cSeschrock 	cmd.ic_cmd = IPMI_CMD_SET_SENSOR_READING;
879113a79cSeschrock 	cmd.ic_lun = 0;
889113a79cSeschrock 	cmd.ic_data = &realreq;
899113a79cSeschrock 	cmd.ic_dlen = sizeof (realreq);
909113a79cSeschrock 
919113a79cSeschrock 	if ((resp = ipmi_send(ihp, &cmd)) == NULL)
929113a79cSeschrock 		return (-1);
939113a79cSeschrock 
949113a79cSeschrock 	if (resp->ic_dlen != 0)
959113a79cSeschrock 		return (ipmi_set_error(ihp, EIPMI_BAD_RESPONSE_LENGTH, NULL));
969113a79cSeschrock 
979113a79cSeschrock 	return (0);
989113a79cSeschrock }
99*208e1562SRob Johnston 
100*208e1562SRob Johnston int
ipmi_get_sensor_thresholds(ipmi_handle_t * ihp,ipmi_sensor_thresholds_t * thresh,uint8_t id)101*208e1562SRob Johnston ipmi_get_sensor_thresholds(ipmi_handle_t *ihp, ipmi_sensor_thresholds_t *thresh,
102*208e1562SRob Johnston     uint8_t id)
103*208e1562SRob Johnston {
104*208e1562SRob Johnston 	ipmi_cmd_t cmd, *resp;
105*208e1562SRob Johnston 
106*208e1562SRob Johnston 	cmd.ic_netfn = IPMI_NETFN_SE;
107*208e1562SRob Johnston 	cmd.ic_cmd = IPMI_CMD_GET_SENSOR_THRESHOLDS;
108*208e1562SRob Johnston 	cmd.ic_lun = 0;
109*208e1562SRob Johnston 	cmd.ic_data = &id;
110*208e1562SRob Johnston 	cmd.ic_dlen = sizeof (id);
111*208e1562SRob Johnston 
112*208e1562SRob Johnston 	if ((resp = ipmi_send(ihp, &cmd)) == NULL)
113*208e1562SRob Johnston 		return (-1);
114*208e1562SRob Johnston 
115*208e1562SRob Johnston 	if (resp->ic_dlen < sizeof (ipmi_sensor_thresholds_t)) {
116*208e1562SRob Johnston 		return (ipmi_set_error(ihp, EIPMI_BAD_RESPONSE_LENGTH, NULL));
117*208e1562SRob Johnston 	}
118*208e1562SRob Johnston 
119*208e1562SRob Johnston 	(void) memcpy(thresh, resp->ic_data, sizeof (ipmi_sensor_thresholds_t));
120*208e1562SRob Johnston 
121*208e1562SRob Johnston 	return (0);
122*208e1562SRob Johnston }
123