Skip to content

Commit 400b6a7

Browse files
groeckkeithbusch
authored andcommitted
nvme: Add hardware monitoring support
nvme devices report temperature information in the controller information (for limits) and in the smart log. Currently, the only means to retrieve this information is the nvme command line interface, which requires super-user privileges. At the same time, it would be desirable to be able to use NVMe temperature information for thermal control. This patch adds support to read NVMe temperatures from the kernel using the hwmon API and adds temperature zones for NVMe drives. The thermal subsystem can use this information to set thermal policies, and userspace can access it using libsensors and/or the "sensors" command. Example output from the "sensors" command: nvme0-pci-0100 Adapter: PCI adapter Composite: +39.0°C (high = +85.0°C, crit = +85.0°C) Sensor 1: +39.0°C Sensor 2: +41.0°C Reviewed-by: Christoph Hellwig <[email protected]> Signed-off-by: Guenter Roeck <[email protected]> Signed-off-by: Keith Busch <[email protected]>
1 parent 64fab72 commit 400b6a7

File tree

5 files changed

+206
-0
lines changed

5 files changed

+206
-0
lines changed

drivers/nvme/host/Kconfig

+10
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ config NVME_MULTIPATH
2323
/dev/nvmeXnY device will show up for each NVMe namespaces,
2424
even if it is accessible through multiple controllers.
2525

26+
config NVME_HWMON
27+
bool "NVMe hardware monitoring"
28+
depends on (NVME_CORE=y && HWMON=y) || (NVME_CORE=m && HWMON)
29+
help
30+
This provides support for NVMe hardware monitoring. If enabled,
31+
a hardware monitoring device will be created for each NVMe drive
32+
in the system.
33+
34+
If unsure, say N.
35+
2636
config NVME_FABRICS
2737
tristate
2838

drivers/nvme/host/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ nvme-core-$(CONFIG_TRACING) += trace.o
1414
nvme-core-$(CONFIG_NVME_MULTIPATH) += multipath.o
1515
nvme-core-$(CONFIG_NVM) += lightnvm.o
1616
nvme-core-$(CONFIG_FAULT_INJECTION_DEBUG_FS) += fault_inject.o
17+
nvme-core-$(CONFIG_NVME_HWMON) += hwmon.o
1718

1819
nvme-y += pci.o
1920

drivers/nvme/host/core.c

+6
Original file line numberDiff line numberDiff line change
@@ -2760,6 +2760,9 @@ int nvme_init_identify(struct nvme_ctrl *ctrl)
27602760
ctrl->oncs = le16_to_cpu(id->oncs);
27612761
ctrl->mtfa = le16_to_cpu(id->mtfa);
27622762
ctrl->oaes = le32_to_cpu(id->oaes);
2763+
ctrl->wctemp = le16_to_cpu(id->wctemp);
2764+
ctrl->cctemp = le16_to_cpu(id->cctemp);
2765+
27632766
atomic_set(&ctrl->abort_limit, id->acl + 1);
27642767
ctrl->vwc = id->vwc;
27652768
if (id->mdts)
@@ -2859,6 +2862,9 @@ int nvme_init_identify(struct nvme_ctrl *ctrl)
28592862
if (ret < 0)
28602863
return ret;
28612864

2865+
if (!ctrl->identified)
2866+
nvme_hwmon_init(ctrl);
2867+
28622868
ctrl->identified = true;
28632869

28642870
return 0;

drivers/nvme/host/hwmon.c

+181
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
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+
}

drivers/nvme/host/nvme.h

+8
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,8 @@ struct nvme_ctrl {
230230
u16 kas;
231231
u8 npss;
232232
u8 apsta;
233+
u16 wctemp;
234+
u16 cctemp;
233235
u32 oaes;
234236
u32 aen_result;
235237
u32 ctratt;
@@ -665,4 +667,10 @@ static inline struct nvme_ns *nvme_get_ns_from_dev(struct device *dev)
665667
return dev_to_disk(dev)->private_data;
666668
}
667669

670+
#ifdef CONFIG_NVME_HWMON
671+
void nvme_hwmon_init(struct nvme_ctrl *ctrl);
672+
#else
673+
static inline void nvme_hwmon_init(struct nvme_ctrl *ctrl) { }
674+
#endif
675+
668676
#endif /* _NVME_H */

0 commit comments

Comments
 (0)