Guenter Roeck | 400b6a7 | 2019-11-06 06:35:18 -0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * NVM Express hardware monitoring support |
| 4 | * Copyright (c) 2019, Guenter Roeck |
| 5 | */ |
| 6 | |
| 7 | #include <linux/hwmon.h> |
Akinobu Mita | 7724cd2 | 2020-01-30 22:15:53 -0800 | [diff] [blame] | 8 | #include <linux/units.h> |
Guenter Roeck | 400b6a7 | 2019-11-06 06:35:18 -0800 | [diff] [blame] | 9 | #include <asm/unaligned.h> |
| 10 | |
| 11 | #include "nvme.h" |
| 12 | |
| 13 | struct nvme_hwmon_data { |
| 14 | struct nvme_ctrl *ctrl; |
| 15 | struct nvme_smart_log log; |
| 16 | struct mutex read_lock; |
| 17 | }; |
| 18 | |
Akinobu Mita | 52deba0 | 2019-11-15 00:40:00 +0900 | [diff] [blame] | 19 | static int nvme_get_temp_thresh(struct nvme_ctrl *ctrl, int sensor, bool under, |
| 20 | long *temp) |
| 21 | { |
| 22 | unsigned int threshold = sensor << NVME_TEMP_THRESH_SELECT_SHIFT; |
| 23 | u32 status; |
| 24 | int ret; |
| 25 | |
| 26 | if (under) |
| 27 | threshold |= NVME_TEMP_THRESH_TYPE_UNDER; |
| 28 | |
| 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; |
Akinobu Mita | 7724cd2 | 2020-01-30 22:15:53 -0800 | [diff] [blame] | 35 | *temp = kelvin_to_millicelsius(status & NVME_TEMP_THRESH_MASK); |
Akinobu Mita | 52deba0 | 2019-11-15 00:40:00 +0900 | [diff] [blame] | 36 | |
| 37 | return 0; |
| 38 | } |
| 39 | |
| 40 | static int nvme_set_temp_thresh(struct nvme_ctrl *ctrl, int sensor, bool under, |
| 41 | long temp) |
| 42 | { |
| 43 | unsigned int threshold = sensor << NVME_TEMP_THRESH_SELECT_SHIFT; |
| 44 | int ret; |
| 45 | |
Akinobu Mita | 7724cd2 | 2020-01-30 22:15:53 -0800 | [diff] [blame] | 46 | temp = millicelsius_to_kelvin(temp); |
Akinobu Mita | 52deba0 | 2019-11-15 00:40:00 +0900 | [diff] [blame] | 47 | threshold |= clamp_val(temp, 0, NVME_TEMP_THRESH_MASK); |
| 48 | |
| 49 | if (under) |
| 50 | threshold |= NVME_TEMP_THRESH_TYPE_UNDER; |
| 51 | |
| 52 | ret = nvme_set_features(ctrl, NVME_FEAT_TEMP_THRESH, threshold, NULL, 0, |
| 53 | NULL); |
| 54 | if (ret > 0) |
| 55 | return -EIO; |
| 56 | |
| 57 | return ret; |
| 58 | } |
| 59 | |
Guenter Roeck | 400b6a7 | 2019-11-06 06:35:18 -0800 | [diff] [blame] | 60 | static int nvme_hwmon_get_smart_log(struct nvme_hwmon_data *data) |
| 61 | { |
Keith Busch | 59e330f | 2020-09-17 08:50:25 -0700 | [diff] [blame] | 62 | return nvme_get_log(data->ctrl, NVME_NSID_ALL, NVME_LOG_SMART, 0, |
Keith Busch | be93e87 | 2020-06-29 12:06:40 -0700 | [diff] [blame] | 63 | NVME_CSI_NVM, &data->log, sizeof(data->log), 0); |
Guenter Roeck | 400b6a7 | 2019-11-06 06:35:18 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | static int nvme_hwmon_read(struct device *dev, enum hwmon_sensor_types type, |
| 67 | u32 attr, int channel, long *val) |
| 68 | { |
| 69 | struct nvme_hwmon_data *data = dev_get_drvdata(dev); |
| 70 | struct nvme_smart_log *log = &data->log; |
| 71 | int temp; |
| 72 | int err; |
| 73 | |
| 74 | /* |
| 75 | * First handle attributes which don't require us to read |
| 76 | * the smart log. |
| 77 | */ |
| 78 | switch (attr) { |
| 79 | case hwmon_temp_max: |
Akinobu Mita | 52deba0 | 2019-11-15 00:40:00 +0900 | [diff] [blame] | 80 | return nvme_get_temp_thresh(data->ctrl, channel, false, val); |
| 81 | case hwmon_temp_min: |
| 82 | return nvme_get_temp_thresh(data->ctrl, channel, true, val); |
Guenter Roeck | 400b6a7 | 2019-11-06 06:35:18 -0800 | [diff] [blame] | 83 | case hwmon_temp_crit: |
Akinobu Mita | 7724cd2 | 2020-01-30 22:15:53 -0800 | [diff] [blame] | 84 | *val = kelvin_to_millicelsius(data->ctrl->cctemp); |
Guenter Roeck | 400b6a7 | 2019-11-06 06:35:18 -0800 | [diff] [blame] | 85 | return 0; |
| 86 | default: |
| 87 | break; |
| 88 | } |
| 89 | |
| 90 | mutex_lock(&data->read_lock); |
| 91 | err = nvme_hwmon_get_smart_log(data); |
| 92 | if (err) |
| 93 | goto unlock; |
| 94 | |
| 95 | switch (attr) { |
| 96 | case hwmon_temp_input: |
| 97 | if (!channel) |
| 98 | temp = get_unaligned_le16(log->temperature); |
| 99 | else |
| 100 | temp = le16_to_cpu(log->temp_sensor[channel - 1]); |
Akinobu Mita | 7724cd2 | 2020-01-30 22:15:53 -0800 | [diff] [blame] | 101 | *val = kelvin_to_millicelsius(temp); |
Guenter Roeck | 400b6a7 | 2019-11-06 06:35:18 -0800 | [diff] [blame] | 102 | break; |
| 103 | case hwmon_temp_alarm: |
| 104 | *val = !!(log->critical_warning & NVME_SMART_CRIT_TEMPERATURE); |
| 105 | break; |
| 106 | default: |
| 107 | err = -EOPNOTSUPP; |
| 108 | break; |
| 109 | } |
| 110 | unlock: |
| 111 | mutex_unlock(&data->read_lock); |
| 112 | return err; |
| 113 | } |
| 114 | |
Akinobu Mita | 52deba0 | 2019-11-15 00:40:00 +0900 | [diff] [blame] | 115 | static int nvme_hwmon_write(struct device *dev, enum hwmon_sensor_types type, |
| 116 | u32 attr, int channel, long val) |
| 117 | { |
| 118 | struct nvme_hwmon_data *data = dev_get_drvdata(dev); |
| 119 | |
| 120 | switch (attr) { |
| 121 | case hwmon_temp_max: |
| 122 | return nvme_set_temp_thresh(data->ctrl, channel, false, val); |
| 123 | case hwmon_temp_min: |
| 124 | return nvme_set_temp_thresh(data->ctrl, channel, true, val); |
| 125 | default: |
| 126 | break; |
| 127 | } |
| 128 | |
| 129 | return -EOPNOTSUPP; |
| 130 | } |
| 131 | |
Guenter Roeck | 400b6a7 | 2019-11-06 06:35:18 -0800 | [diff] [blame] | 132 | static const char * const nvme_hwmon_sensor_names[] = { |
| 133 | "Composite", |
| 134 | "Sensor 1", |
| 135 | "Sensor 2", |
| 136 | "Sensor 3", |
| 137 | "Sensor 4", |
| 138 | "Sensor 5", |
| 139 | "Sensor 6", |
| 140 | "Sensor 7", |
| 141 | "Sensor 8", |
| 142 | }; |
| 143 | |
| 144 | static int nvme_hwmon_read_string(struct device *dev, |
| 145 | enum hwmon_sensor_types type, u32 attr, |
| 146 | int channel, const char **str) |
| 147 | { |
| 148 | *str = nvme_hwmon_sensor_names[channel]; |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | static umode_t nvme_hwmon_is_visible(const void *_data, |
| 153 | enum hwmon_sensor_types type, |
| 154 | u32 attr, int channel) |
| 155 | { |
| 156 | const struct nvme_hwmon_data *data = _data; |
| 157 | |
| 158 | switch (attr) { |
| 159 | case hwmon_temp_crit: |
| 160 | if (!channel && data->ctrl->cctemp) |
| 161 | return 0444; |
| 162 | break; |
| 163 | case hwmon_temp_max: |
Akinobu Mita | 52deba0 | 2019-11-15 00:40:00 +0900 | [diff] [blame] | 164 | case hwmon_temp_min: |
| 165 | if ((!channel && data->ctrl->wctemp) || |
Akinobu Mita | 6c6aa2f | 2019-11-15 00:40:01 +0900 | [diff] [blame] | 166 | (channel && data->log.temp_sensor[channel - 1])) { |
| 167 | if (data->ctrl->quirks & |
| 168 | NVME_QUIRK_NO_TEMP_THRESH_CHANGE) |
| 169 | return 0444; |
Akinobu Mita | 52deba0 | 2019-11-15 00:40:00 +0900 | [diff] [blame] | 170 | return 0644; |
Akinobu Mita | 6c6aa2f | 2019-11-15 00:40:01 +0900 | [diff] [blame] | 171 | } |
Guenter Roeck | 400b6a7 | 2019-11-06 06:35:18 -0800 | [diff] [blame] | 172 | break; |
| 173 | case hwmon_temp_alarm: |
| 174 | if (!channel) |
| 175 | return 0444; |
| 176 | break; |
| 177 | case hwmon_temp_input: |
| 178 | case hwmon_temp_label: |
| 179 | if (!channel || data->log.temp_sensor[channel - 1]) |
| 180 | return 0444; |
| 181 | break; |
| 182 | default: |
| 183 | break; |
| 184 | } |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | static const struct hwmon_channel_info *nvme_hwmon_info[] = { |
| 189 | HWMON_CHANNEL_INFO(chip, HWMON_C_REGISTER_TZ), |
| 190 | HWMON_CHANNEL_INFO(temp, |
Akinobu Mita | 52deba0 | 2019-11-15 00:40:00 +0900 | [diff] [blame] | 191 | HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MIN | |
| 192 | HWMON_T_CRIT | HWMON_T_LABEL | HWMON_T_ALARM, |
| 193 | HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MIN | |
| 194 | HWMON_T_LABEL, |
| 195 | HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MIN | |
| 196 | HWMON_T_LABEL, |
| 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), |
Guenter Roeck | 400b6a7 | 2019-11-06 06:35:18 -0800 | [diff] [blame] | 209 | NULL |
| 210 | }; |
| 211 | |
| 212 | static const struct hwmon_ops nvme_hwmon_ops = { |
| 213 | .is_visible = nvme_hwmon_is_visible, |
| 214 | .read = nvme_hwmon_read, |
| 215 | .read_string = nvme_hwmon_read_string, |
Akinobu Mita | 52deba0 | 2019-11-15 00:40:00 +0900 | [diff] [blame] | 216 | .write = nvme_hwmon_write, |
Guenter Roeck | 400b6a7 | 2019-11-06 06:35:18 -0800 | [diff] [blame] | 217 | }; |
| 218 | |
| 219 | static const struct hwmon_chip_info nvme_hwmon_chip_info = { |
| 220 | .ops = &nvme_hwmon_ops, |
| 221 | .info = nvme_hwmon_info, |
| 222 | }; |
| 223 | |
Keith Busch | 59e330f | 2020-09-17 08:50:25 -0700 | [diff] [blame] | 224 | int nvme_hwmon_init(struct nvme_ctrl *ctrl) |
Guenter Roeck | 400b6a7 | 2019-11-06 06:35:18 -0800 | [diff] [blame] | 225 | { |
Hannes Reinecke | ed7770f | 2021-01-19 07:43:18 +0100 | [diff] [blame] | 226 | struct device *dev = ctrl->device; |
Guenter Roeck | 400b6a7 | 2019-11-06 06:35:18 -0800 | [diff] [blame] | 227 | struct nvme_hwmon_data *data; |
| 228 | struct device *hwmon; |
| 229 | int err; |
| 230 | |
Hannes Reinecke | ed7770f | 2021-01-19 07:43:18 +0100 | [diff] [blame] | 231 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
Guenter Roeck | 400b6a7 | 2019-11-06 06:35:18 -0800 | [diff] [blame] | 232 | if (!data) |
Keith Busch | 59e330f | 2020-09-17 08:50:25 -0700 | [diff] [blame] | 233 | return 0; |
Guenter Roeck | 400b6a7 | 2019-11-06 06:35:18 -0800 | [diff] [blame] | 234 | |
| 235 | data->ctrl = ctrl; |
| 236 | mutex_init(&data->read_lock); |
| 237 | |
| 238 | err = nvme_hwmon_get_smart_log(data); |
| 239 | if (err) { |
Hannes Reinecke | ed7770f | 2021-01-19 07:43:18 +0100 | [diff] [blame] | 240 | dev_warn(dev, "Failed to read smart log (error %d)\n", err); |
| 241 | kfree(data); |
Keith Busch | 59e330f | 2020-09-17 08:50:25 -0700 | [diff] [blame] | 242 | return err; |
Guenter Roeck | 400b6a7 | 2019-11-06 06:35:18 -0800 | [diff] [blame] | 243 | } |
| 244 | |
Hannes Reinecke | ed7770f | 2021-01-19 07:43:18 +0100 | [diff] [blame] | 245 | hwmon = hwmon_device_register_with_info(dev, "nvme", |
| 246 | data, &nvme_hwmon_chip_info, |
| 247 | NULL); |
Guenter Roeck | 400b6a7 | 2019-11-06 06:35:18 -0800 | [diff] [blame] | 248 | if (IS_ERR(hwmon)) { |
| 249 | dev_warn(dev, "Failed to instantiate hwmon device\n"); |
Hannes Reinecke | ed7770f | 2021-01-19 07:43:18 +0100 | [diff] [blame] | 250 | kfree(data); |
Daniel Wagner | 78570f8 | 2021-02-12 10:30:15 +0100 | [diff] [blame] | 251 | return PTR_ERR(hwmon); |
Guenter Roeck | 400b6a7 | 2019-11-06 06:35:18 -0800 | [diff] [blame] | 252 | } |
Hannes Reinecke | ed7770f | 2021-01-19 07:43:18 +0100 | [diff] [blame] | 253 | ctrl->hwmon_device = hwmon; |
Keith Busch | 59e330f | 2020-09-17 08:50:25 -0700 | [diff] [blame] | 254 | return 0; |
Guenter Roeck | 400b6a7 | 2019-11-06 06:35:18 -0800 | [diff] [blame] | 255 | } |
Hannes Reinecke | ed7770f | 2021-01-19 07:43:18 +0100 | [diff] [blame] | 256 | |
| 257 | void nvme_hwmon_exit(struct nvme_ctrl *ctrl) |
| 258 | { |
| 259 | if (ctrl->hwmon_device) { |
| 260 | struct nvme_hwmon_data *data = |
| 261 | dev_get_drvdata(ctrl->hwmon_device); |
| 262 | |
| 263 | hwmon_device_unregister(ctrl->hwmon_device); |
| 264 | ctrl->hwmon_device = NULL; |
| 265 | kfree(data); |
| 266 | } |
| 267 | } |