treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / nvme / host / hwmon.c
blob2e6477ed420f6e6e0035159c5337438e9c78deda
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * NVM Express hardware monitoring support
4 * Copyright (c) 2019, Guenter Roeck
5 */
7 #include <linux/hwmon.h>
8 #include <linux/units.h>
9 #include <asm/unaligned.h>
11 #include "nvme.h"
13 struct nvme_hwmon_data {
14 struct nvme_ctrl *ctrl;
15 struct nvme_smart_log log;
16 struct mutex read_lock;
19 static int nvme_get_temp_thresh(struct nvme_ctrl *ctrl, int sensor, bool under,
20 long *temp)
22 unsigned int threshold = sensor << NVME_TEMP_THRESH_SELECT_SHIFT;
23 u32 status;
24 int ret;
26 if (under)
27 threshold |= NVME_TEMP_THRESH_TYPE_UNDER;
29 ret = nvme_get_features(ctrl, NVME_FEAT_TEMP_THRESH, threshold, NULL, 0,
30 &status);
31 if (ret > 0)
32 return -EIO;
33 if (ret < 0)
34 return ret;
35 *temp = kelvin_to_millicelsius(status & NVME_TEMP_THRESH_MASK);
37 return 0;
40 static int nvme_set_temp_thresh(struct nvme_ctrl *ctrl, int sensor, bool under,
41 long temp)
43 unsigned int threshold = sensor << NVME_TEMP_THRESH_SELECT_SHIFT;
44 int ret;
46 temp = millicelsius_to_kelvin(temp);
47 threshold |= clamp_val(temp, 0, NVME_TEMP_THRESH_MASK);
49 if (under)
50 threshold |= NVME_TEMP_THRESH_TYPE_UNDER;
52 ret = nvme_set_features(ctrl, NVME_FEAT_TEMP_THRESH, threshold, NULL, 0,
53 NULL);
54 if (ret > 0)
55 return -EIO;
57 return ret;
60 static int nvme_hwmon_get_smart_log(struct nvme_hwmon_data *data)
62 int ret;
64 ret = nvme_get_log(data->ctrl, NVME_NSID_ALL, NVME_LOG_SMART, 0,
65 &data->log, sizeof(data->log), 0);
67 return ret <= 0 ? ret : -EIO;
70 static int nvme_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
71 u32 attr, int channel, long *val)
73 struct nvme_hwmon_data *data = dev_get_drvdata(dev);
74 struct nvme_smart_log *log = &data->log;
75 int temp;
76 int err;
79 * First handle attributes which don't require us to read
80 * the smart log.
82 switch (attr) {
83 case hwmon_temp_max:
84 return nvme_get_temp_thresh(data->ctrl, channel, false, val);
85 case hwmon_temp_min:
86 return nvme_get_temp_thresh(data->ctrl, channel, true, val);
87 case hwmon_temp_crit:
88 *val = kelvin_to_millicelsius(data->ctrl->cctemp);
89 return 0;
90 default:
91 break;
94 mutex_lock(&data->read_lock);
95 err = nvme_hwmon_get_smart_log(data);
96 if (err)
97 goto unlock;
99 switch (attr) {
100 case hwmon_temp_input:
101 if (!channel)
102 temp = get_unaligned_le16(log->temperature);
103 else
104 temp = le16_to_cpu(log->temp_sensor[channel - 1]);
105 *val = kelvin_to_millicelsius(temp);
106 break;
107 case hwmon_temp_alarm:
108 *val = !!(log->critical_warning & NVME_SMART_CRIT_TEMPERATURE);
109 break;
110 default:
111 err = -EOPNOTSUPP;
112 break;
114 unlock:
115 mutex_unlock(&data->read_lock);
116 return err;
119 static int nvme_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
120 u32 attr, int channel, long val)
122 struct nvme_hwmon_data *data = dev_get_drvdata(dev);
124 switch (attr) {
125 case hwmon_temp_max:
126 return nvme_set_temp_thresh(data->ctrl, channel, false, val);
127 case hwmon_temp_min:
128 return nvme_set_temp_thresh(data->ctrl, channel, true, val);
129 default:
130 break;
133 return -EOPNOTSUPP;
136 static const char * const nvme_hwmon_sensor_names[] = {
137 "Composite",
138 "Sensor 1",
139 "Sensor 2",
140 "Sensor 3",
141 "Sensor 4",
142 "Sensor 5",
143 "Sensor 6",
144 "Sensor 7",
145 "Sensor 8",
148 static int nvme_hwmon_read_string(struct device *dev,
149 enum hwmon_sensor_types type, u32 attr,
150 int channel, const char **str)
152 *str = nvme_hwmon_sensor_names[channel];
153 return 0;
156 static umode_t nvme_hwmon_is_visible(const void *_data,
157 enum hwmon_sensor_types type,
158 u32 attr, int channel)
160 const struct nvme_hwmon_data *data = _data;
162 switch (attr) {
163 case hwmon_temp_crit:
164 if (!channel && data->ctrl->cctemp)
165 return 0444;
166 break;
167 case hwmon_temp_max:
168 case hwmon_temp_min:
169 if ((!channel && data->ctrl->wctemp) ||
170 (channel && data->log.temp_sensor[channel - 1])) {
171 if (data->ctrl->quirks &
172 NVME_QUIRK_NO_TEMP_THRESH_CHANGE)
173 return 0444;
174 return 0644;
176 break;
177 case hwmon_temp_alarm:
178 if (!channel)
179 return 0444;
180 break;
181 case hwmon_temp_input:
182 case hwmon_temp_label:
183 if (!channel || data->log.temp_sensor[channel - 1])
184 return 0444;
185 break;
186 default:
187 break;
189 return 0;
192 static const struct hwmon_channel_info *nvme_hwmon_info[] = {
193 HWMON_CHANNEL_INFO(chip, HWMON_C_REGISTER_TZ),
194 HWMON_CHANNEL_INFO(temp,
195 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MIN |
196 HWMON_T_CRIT | HWMON_T_LABEL | HWMON_T_ALARM,
197 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MIN |
198 HWMON_T_LABEL,
199 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MIN |
200 HWMON_T_LABEL,
201 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MIN |
202 HWMON_T_LABEL,
203 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MIN |
204 HWMON_T_LABEL,
205 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MIN |
206 HWMON_T_LABEL,
207 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MIN |
208 HWMON_T_LABEL,
209 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MIN |
210 HWMON_T_LABEL,
211 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MIN |
212 HWMON_T_LABEL),
213 NULL
216 static const struct hwmon_ops nvme_hwmon_ops = {
217 .is_visible = nvme_hwmon_is_visible,
218 .read = nvme_hwmon_read,
219 .read_string = nvme_hwmon_read_string,
220 .write = nvme_hwmon_write,
223 static const struct hwmon_chip_info nvme_hwmon_chip_info = {
224 .ops = &nvme_hwmon_ops,
225 .info = nvme_hwmon_info,
228 void nvme_hwmon_init(struct nvme_ctrl *ctrl)
230 struct device *dev = ctrl->dev;
231 struct nvme_hwmon_data *data;
232 struct device *hwmon;
233 int err;
235 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
236 if (!data)
237 return;
239 data->ctrl = ctrl;
240 mutex_init(&data->read_lock);
242 err = nvme_hwmon_get_smart_log(data);
243 if (err) {
244 dev_warn(dev, "Failed to read smart log (error %d)\n", err);
245 devm_kfree(dev, data);
246 return;
249 hwmon = devm_hwmon_device_register_with_info(dev, "nvme", data,
250 &nvme_hwmon_chip_info,
251 NULL);
252 if (IS_ERR(hwmon)) {
253 dev_warn(dev, "Failed to instantiate hwmon device\n");
254 devm_kfree(dev, data);