Skip to content

Commit 52deba0

Browse files
mitakeithbusch
authored andcommitted
nvme: hwmon: provide temperature min and max values for each sensor
According to the NVMe specification, the over temperature threshold and under temperature threshold features shall be implemented for Composite Temperature if a non-zero WCTEMP field value is reported in the Identify Controller data structure. The features are also implemented for all implemented temperature sensors (i.e., all Temperature Sensor fields that report a non-zero value). This provides the over temperature threshold and under temperature threshold for each sensor as temperature min and max values of hwmon sysfs attributes. The WCTEMP is already provided as a temperature max value for Composite Temperature, but this change isn't incompatible. Because the default value of the over temperature threshold for Composite Temperature is the WCTEMP. Now the alarm attribute for Composite Temperature indicates one of the temperature is outside of a temperature threshold. Because there is only a single bit in Critical Warning field that indicates a temperature is outside of a threshold. Example output from the "sensors" command: nvme-pci-0100 Adapter: PCI adapter Composite: +33.9°C (low = -273.1°C, high = +69.8°C) (crit = +79.8°C) Sensor 1: +34.9°C (low = -273.1°C, high = +65261.8°C) Sensor 2: +31.9°C (low = -273.1°C, high = +65261.8°C) Sensor 5: +47.9°C (low = -273.1°C, high = +65261.8°C) This also adds helper macros for kelvin from/to milli Celsius conversion, and replaces the repeated code in hwmon.c. Cc: Keith Busch <kbusch@kernel.org> Cc: Jens Axboe <axboe@fb.com> Cc: Christoph Hellwig <hch@lst.de> Cc: Sagi Grimberg <sagi@grimberg.me> Cc: Jean Delvare <jdelvare@suse.com> Reviewed-by: Guenter Roeck <linux@roeck-us.net> Tested-by: Guenter Roeck <linux@roeck-us.net> Signed-off-by: Akinobu Mita <akinobu.mita@gmail.com> Signed-off-by: Keith Busch <kbusch@kernel.org>
1 parent 3aeb6a2 commit 52deba0

File tree

2 files changed

+96
-16
lines changed

2 files changed

+96
-16
lines changed

drivers/nvme/host/hwmon.c

+90-16
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,57 @@
99

1010
#include "nvme.h"
1111

12+
/* These macros should be moved to linux/temperature.h */
13+
#define MILLICELSIUS_TO_KELVIN(t) DIV_ROUND_CLOSEST((t) + 273150, 1000)
14+
#define KELVIN_TO_MILLICELSIUS(t) ((t) * 1000L - 273150)
15+
1216
struct nvme_hwmon_data {
1317
struct nvme_ctrl *ctrl;
1418
struct nvme_smart_log log;
1519
struct mutex read_lock;
1620
};
1721

22+
static int nvme_get_temp_thresh(struct nvme_ctrl *ctrl, int sensor, bool under,
23+
long *temp)
24+
{
25+
unsigned int threshold = sensor << NVME_TEMP_THRESH_SELECT_SHIFT;
26+
u32 status;
27+
int ret;
28+
29+
if (under)
30+
threshold |= NVME_TEMP_THRESH_TYPE_UNDER;
31+
32+
ret = nvme_get_features(ctrl, NVME_FEAT_TEMP_THRESH, threshold, NULL, 0,
33+
&status);
34+
if (ret > 0)
35+
return -EIO;
36+
if (ret < 0)
37+
return ret;
38+
*temp = KELVIN_TO_MILLICELSIUS(status & NVME_TEMP_THRESH_MASK);
39+
40+
return 0;
41+
}
42+
43+
static int nvme_set_temp_thresh(struct nvme_ctrl *ctrl, int sensor, bool under,
44+
long temp)
45+
{
46+
unsigned int threshold = sensor << NVME_TEMP_THRESH_SELECT_SHIFT;
47+
int ret;
48+
49+
temp = MILLICELSIUS_TO_KELVIN(temp);
50+
threshold |= clamp_val(temp, 0, NVME_TEMP_THRESH_MASK);
51+
52+
if (under)
53+
threshold |= NVME_TEMP_THRESH_TYPE_UNDER;
54+
55+
ret = nvme_set_features(ctrl, NVME_FEAT_TEMP_THRESH, threshold, NULL, 0,
56+
NULL);
57+
if (ret > 0)
58+
return -EIO;
59+
60+
return ret;
61+
}
62+
1863
static int nvme_hwmon_get_smart_log(struct nvme_hwmon_data *data)
1964
{
2065
int ret;
@@ -39,10 +84,11 @@ static int nvme_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
3984
*/
4085
switch (attr) {
4186
case hwmon_temp_max:
42-
*val = (data->ctrl->wctemp - 273) * 1000;
43-
return 0;
87+
return nvme_get_temp_thresh(data->ctrl, channel, false, val);
88+
case hwmon_temp_min:
89+
return nvme_get_temp_thresh(data->ctrl, channel, true, val);
4490
case hwmon_temp_crit:
45-
*val = (data->ctrl->cctemp - 273) * 1000;
91+
*val = KELVIN_TO_MILLICELSIUS(data->ctrl->cctemp);
4692
return 0;
4793
default:
4894
break;
@@ -59,7 +105,7 @@ static int nvme_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
59105
temp = get_unaligned_le16(log->temperature);
60106
else
61107
temp = le16_to_cpu(log->temp_sensor[channel - 1]);
62-
*val = (temp - 273) * 1000;
108+
*val = KELVIN_TO_MILLICELSIUS(temp);
63109
break;
64110
case hwmon_temp_alarm:
65111
*val = !!(log->critical_warning & NVME_SMART_CRIT_TEMPERATURE);
@@ -73,6 +119,23 @@ static int nvme_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
73119
return err;
74120
}
75121

122+
static int nvme_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
123+
u32 attr, int channel, long val)
124+
{
125+
struct nvme_hwmon_data *data = dev_get_drvdata(dev);
126+
127+
switch (attr) {
128+
case hwmon_temp_max:
129+
return nvme_set_temp_thresh(data->ctrl, channel, false, val);
130+
case hwmon_temp_min:
131+
return nvme_set_temp_thresh(data->ctrl, channel, true, val);
132+
default:
133+
break;
134+
}
135+
136+
return -EOPNOTSUPP;
137+
}
138+
76139
static const char * const nvme_hwmon_sensor_names[] = {
77140
"Composite",
78141
"Sensor 1",
@@ -105,8 +168,10 @@ static umode_t nvme_hwmon_is_visible(const void *_data,
105168
return 0444;
106169
break;
107170
case hwmon_temp_max:
108-
if (!channel && data->ctrl->wctemp)
109-
return 0444;
171+
case hwmon_temp_min:
172+
if ((!channel && data->ctrl->wctemp) ||
173+
(channel && data->log.temp_sensor[channel - 1]))
174+
return 0644;
110175
break;
111176
case hwmon_temp_alarm:
112177
if (!channel)
@@ -126,23 +191,32 @@ static umode_t nvme_hwmon_is_visible(const void *_data,
126191
static const struct hwmon_channel_info *nvme_hwmon_info[] = {
127192
HWMON_CHANNEL_INFO(chip, HWMON_C_REGISTER_TZ),
128193
HWMON_CHANNEL_INFO(temp,
129-
HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT |
130-
HWMON_T_LABEL | HWMON_T_ALARM,
131-
HWMON_T_INPUT | HWMON_T_LABEL,
132-
HWMON_T_INPUT | HWMON_T_LABEL,
133-
HWMON_T_INPUT | HWMON_T_LABEL,
134-
HWMON_T_INPUT | HWMON_T_LABEL,
135-
HWMON_T_INPUT | HWMON_T_LABEL,
136-
HWMON_T_INPUT | HWMON_T_LABEL,
137-
HWMON_T_INPUT | HWMON_T_LABEL,
138-
HWMON_T_INPUT | HWMON_T_LABEL),
194+
HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MIN |
195+
HWMON_T_CRIT | HWMON_T_LABEL | HWMON_T_ALARM,
196+
HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MIN |
197+
HWMON_T_LABEL,
198+
HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MIN |
199+
HWMON_T_LABEL,
200+
HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MIN |
201+
HWMON_T_LABEL,
202+
HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MIN |
203+
HWMON_T_LABEL,
204+
HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MIN |
205+
HWMON_T_LABEL,
206+
HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MIN |
207+
HWMON_T_LABEL,
208+
HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MIN |
209+
HWMON_T_LABEL,
210+
HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MIN |
211+
HWMON_T_LABEL),
139212
NULL
140213
};
141214

142215
static const struct hwmon_ops nvme_hwmon_ops = {
143216
.is_visible = nvme_hwmon_is_visible,
144217
.read = nvme_hwmon_read,
145218
.read_string = nvme_hwmon_read_string,
219+
.write = nvme_hwmon_write,
146220
};
147221

148222
static const struct hwmon_chip_info nvme_hwmon_chip_info = {

include/linux/nvme.h

+6
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,12 @@ struct nvme_write_zeroes_cmd {
804804

805805
/* Features */
806806

807+
enum {
808+
NVME_TEMP_THRESH_MASK = 0xffff,
809+
NVME_TEMP_THRESH_SELECT_SHIFT = 16,
810+
NVME_TEMP_THRESH_TYPE_UNDER = 0x100000,
811+
};
812+
807813
struct nvme_feat_auto_pst {
808814
__le64 entries[32];
809815
};

0 commit comments

Comments
 (0)