|
| 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> |
| 8 | +#include <asm/unaligned.h> |
| 9 | + |
| 10 | +#include "nvme.h" |
| 11 | + |
| 12 | +struct nvme_hwmon_data { |
| 13 | + struct nvme_ctrl *ctrl; |
| 14 | + struct nvme_smart_log log; |
| 15 | + struct mutex read_lock; |
| 16 | +}; |
| 17 | + |
| 18 | +static int nvme_hwmon_get_smart_log(struct nvme_hwmon_data *data) |
| 19 | +{ |
| 20 | + int ret; |
| 21 | + |
| 22 | + ret = nvme_get_log(data->ctrl, NVME_NSID_ALL, NVME_LOG_SMART, 0, |
| 23 | + &data->log, sizeof(data->log), 0); |
| 24 | + |
| 25 | + return ret <= 0 ? ret : -EIO; |
| 26 | +} |
| 27 | + |
| 28 | +static int nvme_hwmon_read(struct device *dev, enum hwmon_sensor_types type, |
| 29 | + u32 attr, int channel, long *val) |
| 30 | +{ |
| 31 | + struct nvme_hwmon_data *data = dev_get_drvdata(dev); |
| 32 | + struct nvme_smart_log *log = &data->log; |
| 33 | + int temp; |
| 34 | + int err; |
| 35 | + |
| 36 | + /* |
| 37 | + * First handle attributes which don't require us to read |
| 38 | + * the smart log. |
| 39 | + */ |
| 40 | + switch (attr) { |
| 41 | + case hwmon_temp_max: |
| 42 | + *val = (data->ctrl->wctemp - 273) * 1000; |
| 43 | + return 0; |
| 44 | + case hwmon_temp_crit: |
| 45 | + *val = (data->ctrl->cctemp - 273) * 1000; |
| 46 | + return 0; |
| 47 | + default: |
| 48 | + break; |
| 49 | + } |
| 50 | + |
| 51 | + mutex_lock(&data->read_lock); |
| 52 | + err = nvme_hwmon_get_smart_log(data); |
| 53 | + if (err) |
| 54 | + goto unlock; |
| 55 | + |
| 56 | + switch (attr) { |
| 57 | + case hwmon_temp_input: |
| 58 | + if (!channel) |
| 59 | + temp = get_unaligned_le16(log->temperature); |
| 60 | + else |
| 61 | + temp = le16_to_cpu(log->temp_sensor[channel - 1]); |
| 62 | + *val = (temp - 273) * 1000; |
| 63 | + break; |
| 64 | + case hwmon_temp_alarm: |
| 65 | + *val = !!(log->critical_warning & NVME_SMART_CRIT_TEMPERATURE); |
| 66 | + break; |
| 67 | + default: |
| 68 | + err = -EOPNOTSUPP; |
| 69 | + break; |
| 70 | + } |
| 71 | +unlock: |
| 72 | + mutex_unlock(&data->read_lock); |
| 73 | + return err; |
| 74 | +} |
| 75 | + |
| 76 | +static const char * const nvme_hwmon_sensor_names[] = { |
| 77 | + "Composite", |
| 78 | + "Sensor 1", |
| 79 | + "Sensor 2", |
| 80 | + "Sensor 3", |
| 81 | + "Sensor 4", |
| 82 | + "Sensor 5", |
| 83 | + "Sensor 6", |
| 84 | + "Sensor 7", |
| 85 | + "Sensor 8", |
| 86 | +}; |
| 87 | + |
| 88 | +static int nvme_hwmon_read_string(struct device *dev, |
| 89 | + enum hwmon_sensor_types type, u32 attr, |
| 90 | + int channel, const char **str) |
| 91 | +{ |
| 92 | + *str = nvme_hwmon_sensor_names[channel]; |
| 93 | + return 0; |
| 94 | +} |
| 95 | + |
| 96 | +static umode_t nvme_hwmon_is_visible(const void *_data, |
| 97 | + enum hwmon_sensor_types type, |
| 98 | + u32 attr, int channel) |
| 99 | +{ |
| 100 | + const struct nvme_hwmon_data *data = _data; |
| 101 | + |
| 102 | + switch (attr) { |
| 103 | + case hwmon_temp_crit: |
| 104 | + if (!channel && data->ctrl->cctemp) |
| 105 | + return 0444; |
| 106 | + break; |
| 107 | + case hwmon_temp_max: |
| 108 | + if (!channel && data->ctrl->wctemp) |
| 109 | + return 0444; |
| 110 | + break; |
| 111 | + case hwmon_temp_alarm: |
| 112 | + if (!channel) |
| 113 | + return 0444; |
| 114 | + break; |
| 115 | + case hwmon_temp_input: |
| 116 | + case hwmon_temp_label: |
| 117 | + if (!channel || data->log.temp_sensor[channel - 1]) |
| 118 | + return 0444; |
| 119 | + break; |
| 120 | + default: |
| 121 | + break; |
| 122 | + } |
| 123 | + return 0; |
| 124 | +} |
| 125 | + |
| 126 | +static const struct hwmon_channel_info *nvme_hwmon_info[] = { |
| 127 | + HWMON_CHANNEL_INFO(chip, HWMON_C_REGISTER_TZ), |
| 128 | + 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), |
| 139 | + NULL |
| 140 | +}; |
| 141 | + |
| 142 | +static const struct hwmon_ops nvme_hwmon_ops = { |
| 143 | + .is_visible = nvme_hwmon_is_visible, |
| 144 | + .read = nvme_hwmon_read, |
| 145 | + .read_string = nvme_hwmon_read_string, |
| 146 | +}; |
| 147 | + |
| 148 | +static const struct hwmon_chip_info nvme_hwmon_chip_info = { |
| 149 | + .ops = &nvme_hwmon_ops, |
| 150 | + .info = nvme_hwmon_info, |
| 151 | +}; |
| 152 | + |
| 153 | +void nvme_hwmon_init(struct nvme_ctrl *ctrl) |
| 154 | +{ |
| 155 | + struct device *dev = ctrl->dev; |
| 156 | + struct nvme_hwmon_data *data; |
| 157 | + struct device *hwmon; |
| 158 | + int err; |
| 159 | + |
| 160 | + data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); |
| 161 | + if (!data) |
| 162 | + return; |
| 163 | + |
| 164 | + data->ctrl = ctrl; |
| 165 | + mutex_init(&data->read_lock); |
| 166 | + |
| 167 | + err = nvme_hwmon_get_smart_log(data); |
| 168 | + if (err) { |
| 169 | + dev_warn(dev, "Failed to read smart log (error %d)\n", err); |
| 170 | + devm_kfree(dev, data); |
| 171 | + return; |
| 172 | + } |
| 173 | + |
| 174 | + hwmon = devm_hwmon_device_register_with_info(dev, "nvme", data, |
| 175 | + &nvme_hwmon_chip_info, |
| 176 | + NULL); |
| 177 | + if (IS_ERR(hwmon)) { |
| 178 | + dev_warn(dev, "Failed to instantiate hwmon device\n"); |
| 179 | + devm_kfree(dev, data); |
| 180 | + } |
| 181 | +} |
0 commit comments