1 // SPDX-License-Identifier: GPL-2.0
3 * NVM Express hardware monitoring support
4 * Copyright (c) 2019, Guenter Roeck
7 #include <linux/hwmon.h>
8 #include <asm/unaligned.h>
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)
16 struct nvme_hwmon_data {
17 struct nvme_ctrl *ctrl;
18 struct nvme_smart_log log;
19 struct mutex read_lock;
22 static int nvme_get_temp_thresh(struct nvme_ctrl *ctrl, int sensor, bool under,
25 unsigned int threshold = sensor << NVME_TEMP_THRESH_SELECT_SHIFT;
30 threshold |= NVME_TEMP_THRESH_TYPE_UNDER;
32 ret = nvme_get_features(ctrl, NVME_FEAT_TEMP_THRESH, threshold, NULL, 0,
38 *temp = KELVIN_TO_MILLICELSIUS(status & NVME_TEMP_THRESH_MASK);
43 static int nvme_set_temp_thresh(struct nvme_ctrl *ctrl, int sensor, bool under,
46 unsigned int threshold = sensor << NVME_TEMP_THRESH_SELECT_SHIFT;
49 temp = MILLICELSIUS_TO_KELVIN(temp);
50 threshold |= clamp_val(temp, 0, NVME_TEMP_THRESH_MASK);
53 threshold |= NVME_TEMP_THRESH_TYPE_UNDER;
55 ret = nvme_set_features(ctrl, NVME_FEAT_TEMP_THRESH, threshold, NULL, 0,
63 static int nvme_hwmon_get_smart_log(struct nvme_hwmon_data *data)
67 ret = nvme_get_log(data->ctrl, NVME_NSID_ALL, NVME_LOG_SMART, 0,
68 &data->log, sizeof(data->log), 0);
70 return ret <= 0 ? ret : -EIO;
73 static int nvme_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
74 u32 attr, int channel, long *val)
76 struct nvme_hwmon_data *data = dev_get_drvdata(dev);
77 struct nvme_smart_log *log = &data->log;
82 * First handle attributes which don't require us to read
87 return nvme_get_temp_thresh(data->ctrl, channel, false, val);
89 return nvme_get_temp_thresh(data->ctrl, channel, true, val);
91 *val = KELVIN_TO_MILLICELSIUS(data->ctrl->cctemp);
97 mutex_lock(&data->read_lock);
98 err = nvme_hwmon_get_smart_log(data);
103 case hwmon_temp_input:
105 temp = get_unaligned_le16(log->temperature);
107 temp = le16_to_cpu(log->temp_sensor[channel - 1]);
108 *val = KELVIN_TO_MILLICELSIUS(temp);
110 case hwmon_temp_alarm:
111 *val = !!(log->critical_warning & NVME_SMART_CRIT_TEMPERATURE);
118 mutex_unlock(&data->read_lock);
122 static int nvme_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
123 u32 attr, int channel, long val)
125 struct nvme_hwmon_data *data = dev_get_drvdata(dev);
129 return nvme_set_temp_thresh(data->ctrl, channel, false, val);
131 return nvme_set_temp_thresh(data->ctrl, channel, true, val);
139 static const char * const nvme_hwmon_sensor_names[] = {
151 static int nvme_hwmon_read_string(struct device *dev,
152 enum hwmon_sensor_types type, u32 attr,
153 int channel, const char **str)
155 *str = nvme_hwmon_sensor_names[channel];
159 static umode_t nvme_hwmon_is_visible(const void *_data,
160 enum hwmon_sensor_types type,
161 u32 attr, int channel)
163 const struct nvme_hwmon_data *data = _data;
166 case hwmon_temp_crit:
167 if (!channel && data->ctrl->cctemp)
172 if ((!channel && data->ctrl->wctemp) ||
173 (channel && data->log.temp_sensor[channel - 1])) {
174 if (data->ctrl->quirks &
175 NVME_QUIRK_NO_TEMP_THRESH_CHANGE)
180 case hwmon_temp_alarm:
184 case hwmon_temp_input:
185 case hwmon_temp_label:
186 if (!channel || data->log.temp_sensor[channel - 1])
195 static const struct hwmon_channel_info *nvme_hwmon_info[] = {
196 HWMON_CHANNEL_INFO(chip, HWMON_C_REGISTER_TZ),
197 HWMON_CHANNEL_INFO(temp,
198 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MIN |
199 HWMON_T_CRIT | HWMON_T_LABEL | HWMON_T_ALARM,
200 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MIN |
202 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MIN |
204 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MIN |
206 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MIN |
208 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MIN |
210 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MIN |
212 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MIN |
214 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MIN |
219 static const struct hwmon_ops nvme_hwmon_ops = {
220 .is_visible = nvme_hwmon_is_visible,
221 .read = nvme_hwmon_read,
222 .read_string = nvme_hwmon_read_string,
223 .write = nvme_hwmon_write,
226 static const struct hwmon_chip_info nvme_hwmon_chip_info = {
227 .ops = &nvme_hwmon_ops,
228 .info = nvme_hwmon_info,
231 void nvme_hwmon_init(struct nvme_ctrl *ctrl)
233 struct device *dev = ctrl->dev;
234 struct nvme_hwmon_data *data;
235 struct device *hwmon;
238 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
243 mutex_init(&data->read_lock);
245 err = nvme_hwmon_get_smart_log(data);
247 dev_warn(dev, "Failed to read smart log (error %d)\n", err);
248 devm_kfree(dev, data);
252 hwmon = devm_hwmon_device_register_with_info(dev, "nvme", data,
253 &nvme_hwmon_chip_info,
256 dev_warn(dev, "Failed to instantiate hwmon device\n");
257 devm_kfree(dev, data);