1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*/
/*
* Copyright 2020 Oxide Computer Company
*/
#include <mlxcx.h>
#include <sys/sensors.h>
/*
* The PRM indicates that the temperature is measured in 1/8th degrees.
*/
#define MLXCX_TEMP_GRAN 8
/*
* Read a single temperature sensor entry. The ksensor framework guarantees that
* it will only call this once for a given sensor at any time, though multiple
* sensors can be in parallel.
*/
static int
mlxcx_temperature_read(void *arg, sensor_ioctl_scalar_t *scalar)
{
boolean_t ok;
uint16_t tmp;
mlxcx_register_data_t data;
mlxcx_temp_sensor_t *sensor = arg;
mlxcx_t *mlxp = sensor->mlts_mlx;
bzero(&data, sizeof (data));
data.mlrd_mtmp.mlrd_mtmp_sensor_index = to_be16(sensor->mlts_index);
ok = mlxcx_cmd_access_register(mlxp, MLXCX_CMD_ACCESS_REGISTER_READ,
MLXCX_REG_MTMP, &data);
if (!ok) {
return (EIO);
}
tmp = from_be16(data.mlrd_mtmp.mlrd_mtmp_temperature);
sensor->mlts_value = (int16_t)tmp;
tmp = from_be16(data.mlrd_mtmp.mlrd_mtmp_max_temperature);
sensor->mlts_max_value = (int16_t)tmp;
bcopy(data.mlrd_mtmp.mlrd_mtmp_name, sensor->mlts_name,
sizeof (sensor->mlts_name));
scalar->sis_unit = SENSOR_UNIT_CELSIUS;
scalar->sis_gran = MLXCX_TEMP_GRAN;
scalar->sis_prec = 0;
scalar->sis_value = (int64_t)sensor->mlts_value;
return (0);
}
static const ksensor_ops_t mlxcx_temp_ops = {
.kso_kind = ksensor_kind_temperature,
.kso_scalar = mlxcx_temperature_read
};
void
mlxcx_teardown_sensors(mlxcx_t *mlxp)
{
if (mlxp->mlx_temp_nsensors == 0)
return;
(void) ksensor_remove(mlxp->mlx_dip, KSENSOR_ALL_IDS);
kmem_free(mlxp->mlx_temp_sensors, sizeof (mlxcx_temp_sensor_t) *
mlxp->mlx_temp_nsensors);
}
boolean_t
mlxcx_setup_sensors(mlxcx_t *mlxp)
{
mlxcx_register_data_t data;
boolean_t ok;
mlxp->mlx_temp_nsensors = 0;
bzero(&data, sizeof (data));
ok = mlxcx_cmd_access_register(mlxp, MLXCX_CMD_ACCESS_REGISTER_READ,
MLXCX_REG_MTCAP, &data);
if (!ok) {
return (B_FALSE);
}
if (data.mlrd_mtcap.mlrd_mtcap_sensor_count == 0) {
return (B_TRUE);
}
mlxp->mlx_temp_nsensors = data.mlrd_mtcap.mlrd_mtcap_sensor_count;
mlxp->mlx_temp_sensors = kmem_zalloc(sizeof (mlxcx_temp_sensor_t) *
mlxp->mlx_temp_nsensors, KM_SLEEP);
for (uint8_t i = 0; i < mlxp->mlx_temp_nsensors; i++) {
char buf[32];
int ret;
if (snprintf(buf, sizeof (buf), "temp%u", i) >= sizeof (buf)) {
mlxcx_warn(mlxp, "sensor name %u would overflow "
"internal buffer");
goto err;
}
mlxp->mlx_temp_sensors[i].mlts_mlx = mlxp;
mlxp->mlx_temp_sensors[i].mlts_index = i;
ret = ksensor_create_scalar_pcidev(mlxp->mlx_dip,
SENSOR_KIND_TEMPERATURE, &mlxcx_temp_ops,
&mlxp->mlx_temp_sensors[i], buf,
&mlxp->mlx_temp_sensors[i].mlts_ksensor);
if (ret != 0) {
mlxcx_warn(mlxp, "failed to create temp sensor %s: %d",
buf, ret);
goto err;
}
}
return (B_TRUE);
err:
mlxcx_teardown_sensors(mlxp);
return (B_FALSE);
}
|