Skip to contentSkip to conversation

nvme: add thermal zone devices

PATCH Superseded Akinobu Mita 3 participants 9 messages Jun 13, 2019 – Jun 26, 2019 NVMe

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover nvme: add thermal zone devices

The NVMe controller reports up to nine temperature values in the SMART / Health log page (the composite temperature and temperature sensor 1 through temperature sensor 8).
The temperature threshold feature (Feature Identifier 04h) configures the asynchronous event request command to complete when the temperature is crossed its corresponding temperature threshold.

This provides these temperatures and thresholds via thermal zone devices.

The main purpose of this is to turn on a fan when overheated without polling the device for the smart log that could prevent the lower power state transitions.

In addition to the standard thermal zone device, this also adds support for registering the DT thermal zone device. (This is currently untested, but I plan to get a SBC with PCIe to test with DT)

* v4
- add thermal.c to hold thermal zone related code
- add 'use_thermal_zone' module parameter
- add CONFIG_THERMAL_WRITABLE_TRIPS dependency
- add comment about the return value of nvme_thermal_zones_register()
- support DT thermal zone device.
- use bitmap to iterate over implemented sensors

* v3
- Change the type name of thermal zone devices from 'nvme_temp<sensor>' to 'nvme<instance>_temp<sensor>'
- Pass a NULL to the status argument of nvme_set_feature()
- Change the name of symbolic link from 'nvme_temp<sensor>' to 'temp<sensor>'
- Don't make it fatal error if the device provides a response
- Don't register thermal zone for composite temperature if smart log reports zero value
- Move the thermal zones registration and unregistration into the core module.

* v2
- s/correspoinding/corresponding/ typo in commit log
- Borrowed nvme_get_features() from Keith's patch
- Temperature threshold notification is splitted into another patch
- Change the data type of 'sensor' to unsigned
- Add BUILD_BUG_ON for the array size of tzdev member in nvme_ctrl
- Add WARN_ON_ONCE for paranoid checks
- Fix off-by-one error in nvme_get_temp
- Validate 'sensor' where the value is actually used
- Define and utilize two enums related to the temperature threshold feature
- Remove hysteresis value for this trip point and don't utilize the under temperature threshold
- Print error message for thermal_zone_device_register() failure
- Add function comments for nvme_thermal_zones_{,un}register
- Suppress non-fatal errors from nvme_thermal_zones_register()
- Add comment about implemented temperature sensors
- Instead of creating a new 'thermal_work', append async smart event's action to the existing async_event_work
- Add comment for tzdev member in nvme_ctrl
- Call nvme_thermal_zones_unregister() earlier than the last reference release

Akinobu Mita (2):
nvme: add thermal zone devices
nvme: notify thermal framework when temperature threshold events occur

Keith Busch (1):
nvme: Export get and set features

 drivers/nvme/host/Kconfig   |   1 +
 drivers/nvme/host/Makefile  |   1 +
 drivers/nvme/host/core.c    |  43 +++++-
 drivers/nvme/host/nvme.h    |  46 +++++++
 drivers/nvme/host/thermal.c | 323 ++++++++++++++++++++++++++++++++++++++++++++
 include/linux/nvme.h        |  12 ++
 6 files changed, 423 insertions(+), 3 deletions(-)
 create mode 100644 drivers/nvme/host/thermal.c
Cc Zhang Rui <rui.zhang at intel.com>
Cc Eduardo Valentin <edubezval at gmail.com>
Cc Daniel Lezcano <daniel.lezcano at linaro.org>
Cc Keith Busch <kbusch at kernel.org>
Cc Jens Axboe <axboe at fb.com>
Cc Christoph Hellwig <hch at lst.de>
Cc Sagi Grimberg <sagi at grimberg.me>
Cc Minwoo Im <minwoo.im.dev at gmail.com>
Cc Kenneth Heitke <kenneth.heitke at intel.com>
Cc Chaitanya Kulkarni <Chaitanya.Kulkarni at wdc.com>
Signature · 1 line
2.7.4
#1 lore raw

PATCH v4 1/3 nvme: Export get and set features

From: Keith Busch <[email protected]>

Future use intends to make use of both, so export these functions. And since their implementation is identical except for the opcode, provide a new function that implement both.

[akinobu.mita at gmail.com>: fix line over 80 characters]
Signed-off-by Keith Busch <keith.busch at intel.com>
Signed-off-by Akinobu Mita <akinobu.mita at gmail.com>
Reviewed-by Chaitanya Kulkarni <chaitanya.kulkarni at wdc.com>
Reviewed-by Minwoo Im <minwoo.im.dev at gmail.com>
---
 drivers/nvme/host/core.c | 24 +++++++++++++++++++++---
 drivers/nvme/host/nvme.h |  6 ++++++
 2 files changed, 27 insertions(+), 3 deletions(-)
drivers/nvme/host/core.c +21 -3
index 120fb59..7e3c786 100644
@@ -1113,15 +1113,15 @@ static struct nvme_id_ns *nvme_identify_ns(struct nvme_ctrl *ctrl,
 	return id;
 }
 
-static int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11,
-		      void *buffer, size_t buflen, u32 *result)
+static int nvme_features(struct nvme_ctrl *dev, u8 op, unsigned int fid,
+		unsigned int dword11, void *buffer, size_t buflen, u32 *result)
 {
 	struct nvme_command c;
 	union nvme_result res;
 	int ret;
 
 	memset(&c, 0, sizeof(c));
-	c.features.opcode = nvme_admin_set_features;
+	c.features.opcode = op;
 	c.features.fid = cpu_to_le32(fid);
 	c.features.dword11 = cpu_to_le32(dword11);
 
@@ -1132,6 +1132,24 @@ static int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword
 	return ret;
 }
 
+int nvme_set_features(struct nvme_ctrl *dev, unsigned int fid,
+		      unsigned int dword11, void *buffer, size_t buflen,
+		      u32 *result)
+{
+	return nvme_features(dev, nvme_admin_set_features, fid, dword11, buffer,
+			     buflen, result);
+}
+EXPORT_SYMBOL_GPL(nvme_set_features);
+
+int nvme_get_features(struct nvme_ctrl *dev, unsigned int fid,
+		      unsigned int dword11, void *buffer, size_t buflen,
+		      u32 *result)
+{
+	return nvme_features(dev, nvme_admin_get_features, fid, dword11, buffer,
+			     buflen, result);
+}
+EXPORT_SYMBOL_GPL(nvme_get_features);
+
 int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count)
 {
 	u32 q_count = (*count - 1) | ((*count - 1) << 16);
drivers/nvme/host/nvme.h +6 -0
index 55553d2..038b893 100644
@@ -459,6 +459,12 @@ int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
 		union nvme_result *result, void *buffer, unsigned bufflen,
 		unsigned timeout, int qid, int at_head,
 		blk_mq_req_flags_t flags, bool poll);
+int nvme_set_features(struct nvme_ctrl *dev, unsigned int fid,
+		      unsigned int dword11, void *buffer, size_t buflen,
+		      u32 *result);
+int nvme_get_features(struct nvme_ctrl *dev, unsigned int fid,
+		      unsigned int dword11, void *buffer, size_t buflen,
+		      u32 *result);
 int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count);
 void nvme_stop_keep_alive(struct nvme_ctrl *ctrl);
 int nvme_reset_ctrl(struct nvme_ctrl *ctrl);
Signature · 1 line
2.7.4
#2 lore raw

PATCH v4 2/3 nvme: add thermal zone devices

In reply to Akinobu Mita

The NVMe controller reports up to nine temperature values in the SMART / Health log page (the composite temperature and temperature sensor 1 through temperature sensor 8).

This provides these temperatures via thermal zone devices.

Once the controller is identified, the thermal zone devices are created for all implemented temperature sensors including the composite temperature.

/sys/class/thermal/thermal_zone[0-*]:

    |---type: 'nvme<instance>-temp<sensor>'
    |---temp: Temperature
    |---trip_point_0_temp: Over temperature threshold

The thermal_zone[0-*] contains a 'device' symlink to the corresponding nvme device.

On the other hand, the following symlinks to the thermal zone devices are created in the nvme device sysfs directory.

- temp0: Composite temperature
- temp1: Temperature sensor 1
...
- temp8: Temperature sensor 8

In addition to the standard thermal zone device, this also adds support for registering the DT thermal zone device.

Cc Zhang Rui <rui.zhang at intel.com>
Cc Eduardo Valentin <edubezval at gmail.com>
Cc Daniel Lezcano <daniel.lezcano at linaro.org>
Cc Keith Busch <kbusch at kernel.org>
Cc Jens Axboe <axboe at fb.com>
Cc Christoph Hellwig <hch at lst.de>
Cc Sagi Grimberg <sagi at grimberg.me>
Cc Minwoo Im <minwoo.im.dev at gmail.com>
Cc Kenneth Heitke <kenneth.heitke at intel.com>
Cc Chaitanya Kulkarni <Chaitanya.Kulkarni at wdc.com>
Signed-off-by Akinobu Mita <akinobu.mita at gmail.com>

---
* v4
- add thermal.c to hold thermal zone related code
- add 'use_thermal_zone' module parameter
- add CONFIG_THERMAL_WRITABLE_TRIPS dependency
- add comment about the return value of nvme_thermal_zones_register()
- support DT thermal zone device.
- use bitmap to iterate over implemented sensors

 drivers/nvme/host/Kconfig   |   1 +
 drivers/nvme/host/Makefile  |   1 +
 drivers/nvme/host/core.c    |   5 +
 drivers/nvme/host/nvme.h    |  35 +++++
 drivers/nvme/host/thermal.c | 311 ++++++++++++++++++++++++++++++++++++++++++++
 include/linux/nvme.h        |   5 +
 6 files changed, 358 insertions(+)
 create mode 100644 drivers/nvme/host/thermal.c
drivers/nvme/host/Kconfig +1 -0
index ec43ac9..90429f8 100644
@@ -1,6 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0-only
 config NVME_CORE
 	tristate
+	select THERMAL_WRITABLE_TRIPS if THERMAL
 
 config BLK_DEV_NVME
 	tristate "NVM Express block device"
drivers/nvme/host/Makefile +1 -0
index 8a4b671..6018668 100644
@@ -14,6 +14,7 @@ nvme-core-$(CONFIG_TRACING)		+= trace.o
 nvme-core-$(CONFIG_NVME_MULTIPATH)	+= multipath.o
 nvme-core-$(CONFIG_NVM)			+= lightnvm.o
 nvme-core-$(CONFIG_FAULT_INJECTION_DEBUG_FS)	+= fault_inject.o
+nvme-core-$(CONFIG_THERMAL)		+= thermal.o
 
 nvme-y					+= pci.o
 
drivers/nvme/host/core.c +5 -0
index 7e3c786..4be339b 100644
@@ -2696,6 +2696,10 @@ int nvme_init_identify(struct nvme_ctrl *ctrl)
 	if (ret < 0)
 		return ret;
 
+	ret = nvme_thermal_zones_register(ctrl);
+	if (ret)
+		return ret;
+
 	ctrl->identified = true;
 
 	return 0;
@@ -3699,6 +3703,7 @@ void nvme_stop_ctrl(struct nvme_ctrl *ctrl)
 {
 	nvme_mpath_stop(ctrl);
 	nvme_stop_keep_alive(ctrl);
+	nvme_thermal_zones_unregister(ctrl);
 	flush_work(&ctrl->async_event_work);
 	cancel_work_sync(&ctrl->fw_act_work);
 }
drivers/nvme/host/nvme.h +35 -0
index 038b893..a9c63ea 100644
@@ -15,6 +15,7 @@
 #include <linux/sed-opal.h>
 #include <linux/fault-inject.h>
 #include <linux/rcupdate.h>
+#include <linux/thermal.h>
 
 extern unsigned int nvme_io_timeout;
 #define NVME_IO_TIMEOUT	(nvme_io_timeout * HZ)
@@ -146,6 +147,13 @@ enum nvme_ctrl_state {
 	NVME_CTRL_DEAD,
 };
 
+struct nvme_tz {
+	struct thermal_zone_params params;
+	struct thermal_zone_device *dev;
+	struct thermal_zone_device *of_dev;
+	unsigned int sensor;
+};
+
 struct nvme_ctrl {
 	bool comp_seen;
 	enum nvme_ctrl_state state;
@@ -247,6 +255,15 @@ struct nvme_ctrl {
 
 	struct page *discard_page;
 	unsigned long discard_page_busy;
+
+#ifdef CONFIG_THERMAL
+	/*
+	 * tz[0]: composite temperature
+	 * tz[1-8]: temperature sensor 1 through 8
+	 */
+	struct nvme_tz tz[9];
+	DECLARE_BITMAP(tz_enabled, 9);
+#endif
 };
 
 enum nvme_iopolicy {
@@ -584,4 +601,22 @@ static inline struct nvme_ns *nvme_get_ns_from_dev(struct device *dev)
 	return dev_to_disk(dev)->private_data;
 }
 
+#ifdef CONFIG_THERMAL
+
+int nvme_thermal_zones_register(struct nvme_ctrl *ctrl);
+void nvme_thermal_zones_unregister(struct nvme_ctrl *ctrl);
+
+#else
+
+static inline int nvme_thermal_zones_register(struct nvme_ctrl *ctrl)
+{
+	return 0;
+}
+
+static inline void nvme_thermal_zones_unregister(struct nvme_ctrl *ctrl)
+{
+}
+
+#endif /* CONFIG_THERMAL */
+
 #endif /* _NVME_H */
drivers/nvme/host/thermal.c new file +311 -0

311 lines not on this page · raw message

include/linux/nvme.h +5 -0
index 8028ada..f29728b 100644
@@ -749,6 +749,11 @@ struct nvme_write_zeroes_cmd {
 
 /* Features */
 
+enum {
+	NVME_TEMP_THRESH_MASK		= 0xffff,
+	NVME_TEMP_THRESH_SELECT_SHIFT	= 16,
+};
+
 struct nvme_feat_auto_pst {
 	__le64 entries[32];
 };
Signature · 1 line
2.7.4
#3 lore raw

PATCH v4 2/3 nvme: add thermal zone devices

Quoted 26 lines · Akinobu Mita

The NVMe controller reports up to nine temperature values in the SMART /
Health log page (the composite temperature and temperature sensor 1 through
temperature sensor 8).

This provides these temperatures via thermal zone devices.

Once the controller is identified, the thermal zone devices are created for
all implemented temperature sensors including the composite temperature.

/sys/class/thermal/thermal_zone[0-*]:

????|---type: 'nvme<instance>-temp<sensor>'
????|---temp: Temperature
????|---trip_point_0_temp: Over temperature threshold

The thermal_zone[0-*] contains a 'device' symlink to the corresponding nvme
device.

On the other hand, the following symlinks to the thermal zone devices are
created in the nvme device sysfs directory.

Quoted 9 lines

- temp0: Composite temperature
- temp1: Temperature sensor 1
...
- temp8: Temperature sensor 8

In addition to the standard thermal zone device, this also adds support for
registering the DT thermal zone device.

I don't see standard thermal zone device and DT thermal zone device are registered at the same time very often, especially if they represent the same sensor.

Eduardo, any comments?

thanks,
rui

Quoted 515 lines
Cc Zhang Rui <rui.zhang at intel.com>
Cc Eduardo Valentin <edubezval at gmail.com>
Cc Daniel Lezcano <daniel.lezcano at linaro.org>
Cc Keith Busch <kbusch at kernel.org>
Cc Jens Axboe <axboe at fb.com>
Cc Christoph Hellwig <hch at lst.de>
Cc Sagi Grimberg <sagi at grimberg.me>
Cc Minwoo Im <minwoo.im.dev at gmail.com>
Cc Kenneth Heitke <kenneth.heitke at intel.com>
Cc Chaitanya Kulkarni <Chaitanya.Kulkarni at wdc.com>
Signed-off-by Akinobu Mita <akinobu.mita at gmail.com>

---
* v4
- add thermal.c to hold thermal zone related code
- add 'use_thermal_zone' module parameter
- add CONFIG_THERMAL_WRITABLE_TRIPS dependency
- add comment about the return value of nvme_thermal_zones_register()
- support DT thermal zone device.
- use bitmap to iterate over implemented sensors

?drivers/nvme/host/Kconfig???|???1 +
?drivers/nvme/host/Makefile??|???1 +
?drivers/nvme/host/core.c????|???5 +
?drivers/nvme/host/nvme.h????|??35 +++++
?drivers/nvme/host/thermal.c | 311

++++++++++++++++++++++++++++++++++++++++++++
?include/linux/nvme.h????????|???5 +
?6 files changed, 358 insertions(+) ?create mode 100644 drivers/nvme/host/thermal.c

drivers/nvme/host/Kconfig +0 -0
index ec43ac9..90429f8 100644
@@ -1,6 +1,7 @@

?# SPDX-License-Identifier: GPL-2.0-only ?config NVME_CORE
? tristate

drivers/nvme/host/Kconfig +1 -0
+	select THERMAL_WRITABLE_TRIPS if THERMAL

?
?config BLK_DEV_NVME
? tristate "NVM Express block device"

drivers/nvme/host/Makefile +0 -0
index 8a4b671..6018668 100644
@@ -14,6 +14,7 @@ nvme-core-$(CONFIG_TRACING)		+=

trace.o
?nvme-core-$(CONFIG_NVME_MULTIPATH) += multipath.o
?nvme-core-$(CONFIG_NVM) += lightnvm.o
?nvme-core-$(CONFIG_FAULT_INJECTION_DEBUG_FS) +=
fault_inject.o
+nvme-core-$(CONFIG_THERMAL) += thermal.o
?
?nvme-y += pci.o
?

drivers/nvme/host/core.c +0 -0
index 7e3c786..4be339b 100644
@@ -2696,6 +2696,10 @@ int nvme_init_identify(struct nvme_ctrl *ctrl)
 ?	if (ret < 0)
 ?		return ret;

?

drivers/nvme/host/core.c +4 -0
+	ret = nvme_thermal_zones_register(ctrl);
+	if (ret)
+		return ret;
+

? ctrl->identified = true;
?
? return 0;

drivers/nvme/host/core.c +1 -0
@@ -3699,6 +3703,7 @@ void nvme_stop_ctrl(struct nvme_ctrl *ctrl)
 ?{
 ?	nvme_mpath_stop(ctrl);
 ?	nvme_stop_keep_alive(ctrl);
+	nvme_thermal_zones_unregister(ctrl);
 ?	flush_work(&ctrl->async_event_work);
 ?	cancel_work_sync(&ctrl->fw_act_work);
 ?}
drivers/nvme/host/nvme.h +0 -0
index 038b893..a9c63ea 100644
@@ -15,6 +15,7 @@

?#include <linux/sed-opal.h>
?#include <linux/fault-inject.h> ?#include <linux/rcupdate.h>
+#include <linux/thermal.h>
?
?extern unsigned int nvme_io_timeout;
?#define NVME_IO_TIMEOUT (nvme_io_timeout * HZ)

drivers/nvme/host/nvme.h +0 -0
@@ -146,6 +147,13 @@ enum nvme_ctrl_state {
 ?	NVME_CTRL_DEAD,
 ?};

?

drivers/nvme/host/nvme.h +7 -0
+struct nvme_tz {
+	struct thermal_zone_params params;
+	struct thermal_zone_device *dev;
+	struct thermal_zone_device *of_dev;
+	unsigned int sensor;
+};
+

?struct nvme_ctrl {
? bool comp_seen;
? enum nvme_ctrl_state state;

drivers/nvme/host/nvme.h +0 -0
@@ -247,6 +255,15 @@ struct nvme_ctrl {

?
? struct page *discard_page;
? unsigned long discard_page_busy;

drivers/nvme/host/nvme.h +9 -0
+
+#ifdef CONFIG_THERMAL
+	/*
+	?* tz[0]: composite temperature
+	?* tz[1-8]: temperature sensor 1 through 8
+	?*/
+	struct nvme_tz tz[9];
+	DECLARE_BITMAP(tz_enabled, 9);
+#endif

?};
?
?enum nvme_iopolicy {

drivers/nvme/host/nvme.h +0 -0
@@ -584,4 +601,22 @@ static inline struct nvme_ns
 *nvme_get_ns_from_dev(struct device *dev)
 ?	return dev_to_disk(dev)->private_data;
 ?}

?

drivers/nvme/host/nvme.h +8 -0
+#ifdef CONFIG_THERMAL
+
+int nvme_thermal_zones_register(struct nvme_ctrl *ctrl);
+void nvme_thermal_zones_unregister(struct nvme_ctrl *ctrl);
+
+#else
+
+static inline int nvme_thermal_zones_register(struct nvme_ctrl

*ctrl)

drivers/nvme/host/nvme.h +5 -0
+{
+	return 0;
+}
+
+static inline void nvme_thermal_zones_unregister(struct nvme_ctrl

*ctrl)

drivers/nvme/host/nvme.h +5 -0
+{
+}
+
+#endif /* CONFIG_THERMAL */
+

?#endif /* _NVME_H */

drivers/nvme/host/thermal.c new file +13 -0
new file mode 100644
index 0000000..18d0e4c
@@ -0,0 +1,311 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/moduleparam.h>
+#include <asm/unaligned.h>
+
+#include "nvme.h"
+
+static bool use_thermal_zone = true;
+module_param(use_thermal_zone, bool, 0444);
+MODULE_PARM_DESC(use_thermal_zone,
+	"Export nvme temperature via generic thermal interface");
+
+static int nvme_get_temp(struct nvme_ctrl *ctrl, unsigned int

sensor, int *temp)

drivers/nvme/host/thermal.c new file +5 -0
+{
+	struct nvme_smart_log *log;
+	int ret;
+
+	BUILD_BUG_ON(ARRAY_SIZE(log->temp_sensor) + 1 !=

ARRAY_SIZE(ctrl->tz));

drivers/nvme/host/thermal.c new file +9 -0
+
+	if (WARN_ON_ONCE(sensor > ARRAY_SIZE(log->temp_sensor)))
+		return -EINVAL;
+
+	log = kzalloc(sizeof(*log), GFP_KERNEL);
+	if (!log)
+		return -ENOMEM;
+
+	ret = nvme_get_log(ctrl, NVME_NSID_ALL, NVME_LOG_SMART, 0,

log,

drivers/nvme/host/thermal.c new file +18 -0
+			???sizeof(*log), 0);
+	if (ret) {
+		ret = ret > 0 ? -EINVAL : ret;
+		goto free_log;
+	}
+
+	if (sensor)
+		*temp = le16_to_cpu(log->temp_sensor[sensor - 1]);
+	else
+		*temp = get_unaligned_le16(log->temperature);
+
+free_log:
+	kfree(log);
+
+	return ret;
+}
+
+#define KELVIN_TO_MILLICELSIUS(t) DECI_KELVIN_TO_MILLICELSIUS((t) *

10)
+#define MILLICELSIUS_TO_KELVIN(t) ((MILLICELSIUS_TO_DECI_KELVIN(t) +
5) / 10)

drivers/nvme/host/thermal.c new file +6 -0
+
+static int nvme_tz_of_get_temp(void *data, int *temp)
+{
+	struct nvme_tz *tz = data;
+	unsigned int sensor = tz->sensor;
+	struct nvme_ctrl *ctrl = container_of(tz, struct nvme_ctrl,

tz[sensor]);

drivers/nvme/host/thermal.c new file +10 -0
+	int ret;
+
+	ret = nvme_get_temp(ctrl, sensor, temp);
+	if (!ret)
+		*temp = KELVIN_TO_MILLICELSIUS(*temp);
+
+	return ret;
+}
+
+static int nvme_tz_get_temp(struct thermal_zone_device *tzdev, int

*temp)

drivers/nvme/host/thermal.c new file +6 -0
+{
+	return nvme_tz_of_get_temp(tzdev->devdata, temp);
+}
+
+static int nvme_tz_get_trip_type(struct thermal_zone_device *tzdev,
+				?int trip, enum thermal_trip_type

*type)

drivers/nvme/host/thermal.c new file +10 -0
+{
+	*type = THERMAL_TRIP_ACTIVE;
+
+	return 0;
+}
+
+static int nvme_get_over_temp_thresh(struct nvme_ctrl *ctrl,
+				?????unsigned int sensor, int *temp)
+{
+	unsigned int threshold = sensor <<

NVME_TEMP_THRESH_SELECT_SHIFT;

drivers/nvme/host/thermal.c new file +7 -0
+	int status;
+	int ret;
+
+	if (WARN_ON_ONCE(sensor >= ARRAY_SIZE(ctrl->tz)))
+		return -EINVAL;
+
+	ret = nvme_get_features(ctrl, NVME_FEAT_TEMP_THRESH,

threshold, NULL, 0,

drivers/nvme/host/thermal.c new file +11 -0
+				&status);
+	if (!ret)
+		*temp = status & NVME_TEMP_THRESH_MASK;
+
+	return ret > 0 ? -EINVAL : ret;
+}
+
+static int nvme_set_over_temp_thresh(struct nvme_ctrl *ctrl,
+				?????unsigned int sensor, int temp)
+{
+	unsigned int threshold = sensor <<

NVME_TEMP_THRESH_SELECT_SHIFT;

drivers/nvme/host/thermal.c new file +11 -0
+	int ret;
+
+	if (WARN_ON_ONCE(sensor >= ARRAY_SIZE(ctrl->tz)))
+		return -EINVAL;
+
+	if (temp > NVME_TEMP_THRESH_MASK)
+		return -EINVAL;
+
+	threshold |= temp & NVME_TEMP_THRESH_MASK;
+
+	ret = nvme_set_features(ctrl, NVME_FEAT_TEMP_THRESH,

threshold, NULL, 0,

drivers/nvme/host/thermal.c new file +11 -0
+				NULL);
+
+	return ret > 0 ? -EINVAL : ret;
+}
+
+static int nvme_tz_get_trip_temp(struct thermal_zone_device *tzdev,
+				?int trip, int *temp)
+{
+	struct nvme_tz *tz = tzdev->devdata;
+	unsigned int sensor = tz->sensor;
+	struct nvme_ctrl *ctrl = container_of(tz, struct nvme_ctrl,

tz[sensor]);

drivers/nvme/host/thermal.c new file +14 -0
+	int ret;
+
+	ret = nvme_get_over_temp_thresh(ctrl, sensor, temp);
+	if (!ret)
+		*temp = KELVIN_TO_MILLICELSIUS(*temp);
+
+	return ret;
+}
+
+static int nvme_tz_of_set_trip_temp(void *data, int trip, int temp)
+{
+	struct nvme_tz *tz = data;
+	unsigned int sensor = tz->sensor;
+	struct nvme_ctrl *ctrl = container_of(tz, struct nvme_ctrl,

tz[sensor]);

drivers/nvme/host/thermal.c new file +40 -0
+
+	temp = MILLICELSIUS_TO_KELVIN(temp);
+
+	return nvme_set_over_temp_thresh(ctrl, sensor, temp);
+}
+
+static int nvme_tz_set_trip_temp(struct thermal_zone_device *tzdev,
+				?int trip, int temp)
+{
+	return nvme_tz_of_set_trip_temp(tzdev->devdata, trip, temp);
+}
+
+static struct thermal_zone_device_ops nvme_tz_ops = {
+	.get_temp = nvme_tz_get_temp,
+	.get_trip_type = nvme_tz_get_trip_type,
+	.get_trip_temp = nvme_tz_get_trip_temp,
+	.set_trip_temp = nvme_tz_set_trip_temp,
+};
+
+static struct thermal_zone_of_device_ops nvme_tz_of_ops = {
+	.get_temp = nvme_tz_of_get_temp,
+	.set_trip_temp = nvme_tz_of_set_trip_temp,
+};
+
+static const struct thermal_zone_params nvme_tz_params = {
+	.governor_name = "user_space",
+	.no_hwmon = true,
+};
+
+static int nvme_thermal_zone_register(struct nvme_ctrl *ctrl,
+				??????unsigned int sensor)
+{
+	struct thermal_zone_device *tzdev;
+	struct nvme_tz *tz = &ctrl->tz[sensor];
+	char name[THERMAL_NAME_LENGTH];
+	int ret;
+
+	tz->params = nvme_tz_params;
+	tz->sensor = sensor;
+	snprintf(name, sizeof(name), "nvme%d_temp%u", ctrl-
Quoted 1 line

instance, sensor);

drivers/nvme/host/thermal.c new file +2 -0
+
+	tzdev = thermal_zone_device_register(name, 1, 1, tz,

&nvme_tz_ops,

drivers/nvme/host/thermal.c new file +4 -0
+					?????&tz->params, 0, 0);
+	if (IS_ERR(tzdev)) {
+		dev_err(ctrl->device,
+			"Failed to register thermal zone device:

%ld\n",

drivers/nvme/host/thermal.c new file +8 -0
+			PTR_ERR(tzdev));
+		return PTR_ERR(tzdev);
+	}
+
+	tz->dev = tzdev;
+
+	snprintf(name, sizeof(name), "temp%d", sensor);
+	ret = sysfs_create_link(&ctrl->ctrl_device.kobj, &tz->dev-
Quoted 1 line

device.kobj,

drivers/nvme/host/thermal.c new file +10 -0
+				name);
+	if (ret)
+		goto device_unregister;
+
+	ret = sysfs_create_link(&tz->dev->device.kobj,
+				&ctrl->ctrl_device.kobj, "device");
+	if (ret)
+		goto remove_sensor_link;
+
+	tzdev = thermal_zone_of_sensor_register(ctrl->dev, sensor,

tz,

drivers/nvme/host/thermal.c new file +7 -0
+						&nvme_tz_of_ops);
+	if (!IS_ERR(tzdev)) {
+		tz->of_dev = tzdev;
+	} else if (PTR_ERR(tzdev) != -ENODEV) {
+		ret = PTR_ERR(tzdev);
+		dev_err(ctrl->device,
+			"Failed to register thermal zone of sensor:

%d\n", ret);

drivers/nvme/host/thermal.c new file +18 -0
+		goto remove_device_link;
+	}
+
+	return 0;
+
+remove_device_link:
+	sysfs_remove_link(&tz->dev->device.kobj, "device");
+remove_sensor_link:
+	sysfs_remove_link(&ctrl->ctrl_device.kobj, name);
+device_unregister:
+	thermal_zone_device_unregister(tz->dev);
+	tz->dev = NULL;
+
+	return ret;
+}
+
+/**
+ * nvme_thermal_zones_register() - register nvme thermal zone

devices
+ * @ctrl: controller instance
+ *
+ * This function creates up to nine thermal zone devices for all implemented
+ * temperature sensors including the composite temperature.
+ * Each thermal zone device provides a single trip point temperature that is
+ * associated with an over temperature threshold.
+ * A negative value is returned when the device doesn't respond to the host
+ * and won't be able to continue initialization.
+ */

drivers/nvme/host/thermal.c new file +15 -0
+int nvme_thermal_zones_register(struct nvme_ctrl *ctrl)
+{
+	struct nvme_smart_log *log;
+	int ret = 0;
+	int err;
+	int i;
+
+	if (!use_thermal_zone)
+		return 0;
+
+	log = kzalloc(sizeof(*log), GFP_KERNEL);
+	if (!log)
+		return 0;
+
+	err = nvme_get_log(ctrl, NVME_NSID_ALL, NVME_LOG_SMART, 0,

log,

drivers/nvme/host/thermal.c new file +3 -0
+			???sizeof(*log), 0);
+	if (err) {
+		dev_err(ctrl->device, "Failed to get SMART log:

%d\n", err);

drivers/nvme/host/thermal.c new file +1 -0
+		/* If the device provided a response, then it's non-

fatal */

drivers/nvme/host/thermal.c new file +5 -0
+		ret = (err > 0) ? 0 : err;
+		goto out;
+	}
+
+	for_each_clear_bit(i, ctrl->tz_enabled, ARRAY_SIZE(ctrl-
Quoted 1 line

tz)) {

drivers/nvme/host/thermal.c new file +9 -0
+		int temp;
+
+		if (i)
+			temp = le16_to_cpu(log->temp_sensor[i - 1]);
+		else
+			temp = get_unaligned_le16(log->temperature);
+
+		/*
+		?* All implemented temperature sensors report a non-

zero value

drivers/nvme/host/thermal.c new file +1 -0
+		?* in temperature sensor fields in the smart log

page.

drivers/nvme/host/thermal.c new file +21 -0
+		?*/
+		if (!temp)
+			continue;
+
+		err = nvme_thermal_zone_register(ctrl, i);
+		if (err)
+			goto out;
+
+		__set_bit(i, ctrl->tz_enabled);
+	}
+out:
+	if (err)
+		nvme_thermal_zones_unregister(ctrl);
+
+	kfree(log);
+
+	return ret;
+}
+
+/**
+ * nvme_thermal_zones_unregister() - unregister nvme thermal zone

devices
+ * @ctrl: controller instance
+ *
+ * This function removes the registered thermal zone devices and symlinks.
+ */

drivers/nvme/host/thermal.c new file +5 -0
+void nvme_thermal_zones_unregister(struct nvme_ctrl *ctrl)
+{
+	int i;
+
+	for_each_set_bit(i, ctrl->tz_enabled, ARRAY_SIZE(ctrl->tz))

{

drivers/nvme/host/thermal.c new file +4 -0
+		struct nvme_tz *tz = &ctrl->tz[i];
+		char name[20];
+
+		thermal_zone_of_sensor_unregister(ctrl->dev, tz-
Quoted 1 line

of_dev);

drivers/nvme/host/thermal.c new file +13 -0
+		tz->of_dev = NULL;
+
+		sysfs_remove_link(&tz->dev->device.kobj, "device");
+
+		snprintf(name, sizeof(name), "temp%d", i);
+		sysfs_remove_link(&ctrl->ctrl_device.kobj, name);
+
+		thermal_zone_device_unregister(tz->dev);
+		tz->dev = NULL;
+
+		__clear_bit(i, ctrl->tz_enabled);
+	}
+}
include/linux/nvme.h +0 -0
index 8028ada..f29728b 100644
@@ -749,6 +749,11 @@ struct nvme_write_zeroes_cmd {

?
?/* Features */
?

include/linux/nvme.h +5 -0
+enum {
+	NVME_TEMP_THRESH_MASK		= 0xffff,
+	NVME_TEMP_THRESH_SELECT_SHIFT	= 16,
+};
+

?struct nvme_feat_auto_pst {
? __le64 entries[32];
?};

#4 lore raw

PATCH v4 2/3 nvme: add thermal zone devices

2019?6?26?(?) 0:03 Zhang Rui <rui.zhang at intel.com>:

Quoted 33 lines · Akinobu Mita
Quoted 31 lines

The NVMe controller reports up to nine temperature values in the SMART /
Health log page (the composite temperature and temperature sensor 1 through
temperature sensor 8).

This provides these temperatures via thermal zone devices.

Once the controller is identified, the thermal zone devices are created for
all implemented temperature sensors including the composite temperature.

/sys/class/thermal/thermal_zone[0-*]:

    |---type: 'nvme<instance>-temp<sensor>'
    |---temp: Temperature
    |---trip_point_0_temp: Over temperature threshold

The thermal_zone[0-*] contains a 'device' symlink to the corresponding nvme
device.

On the other hand, the following symlinks to the thermal zone devices are
created in the nvme device sysfs directory.

- temp0: Composite temperature
- temp1: Temperature sensor 1
...
- temp8: Temperature sensor 8

Quoted 7 lines
Quoted 4 lines

In addition to the standard thermal zone device, this also adds support for
registering the DT thermal zone device.

I don't see standard thermal zone device and DT thermal zone device are registered at the same time very often, especially if they represent the same sensor.

Good point.

It is pointless to register both standard and DT thermal zone devices for the same sensor. We should register either one. (i.e. firstly try to register DT thermal zone. If no thermal zones found for the sensor in device tree, then try to register standard one)

#5 lore raw

PATCH v4 3/3 nvme: notify thermal framework when temperature threshold events occur

In reply to Akinobu Mita

The NVMe controller supports the temperature threshold feature (Feature Identifier 04h) that enables to configure the asynchronous event request command to complete when the temperature is crossed its corresponding temperature threshold.

This enables the reporting of asynchronous events from the controller when the temperature reached or exceeded a temperature threshold. In the case of the temperature threshold conditions, this notifies the thermal framework.

The main purpose of this is to turn on a fan when overheated without polling the device for the smart log that could prevent the lower power state transitions.

Cc Zhang Rui <rui.zhang at intel.com>
Cc Eduardo Valentin <edubezval at gmail.com>
Cc Daniel Lezcano <daniel.lezcano at linaro.org>
Cc Keith Busch <kbusch at kernel.org>
Cc Jens Axboe <axboe at fb.com>
Cc Christoph Hellwig <hch at lst.de>
Cc Sagi Grimberg <sagi at grimberg.me>
Cc Minwoo Im <minwoo.im.dev at gmail.com>
Cc Kenneth Heitke <kenneth.heitke at intel.com>
Cc Chaitanya Kulkarni <Chaitanya.Kulkarni at wdc.com>
Signed-off-by Akinobu Mita <akinobu.mita at gmail.com>

---
* v4
- support DT thermal zone device.
- use bitmap to iterate over implemented sensors

 drivers/nvme/host/core.c    | 14 ++++++++++++++
 drivers/nvme/host/nvme.h    |  5 +++++
 drivers/nvme/host/thermal.c | 12 ++++++++++++
 include/linux/nvme.h        |  7 +++++++
 4 files changed, 38 insertions(+)
drivers/nvme/host/core.c +14 -0
index 4be339b..44dadbb 100644
@@ -1186,6 +1186,9 @@ static void nvme_enable_aen(struct nvme_ctrl *ctrl)
 	u32 result, supported_aens = ctrl->oaes & NVME_AEN_SUPPORTED;
 	int status;
 
+	if (IS_ENABLED(CONFIG_THERMAL))
+		supported_aens |= NVME_SMART_CRIT_TEMPERATURE;
+
 	if (!supported_aens)
 		return;
 
@@ -3561,6 +3564,16 @@ void nvme_remove_namespaces(struct nvme_ctrl *ctrl)
 }
 EXPORT_SYMBOL_GPL(nvme_remove_namespaces);
 
+static void nvme_handle_aen_smart(struct nvme_ctrl *ctrl, u32 result)
+{
+	u32 aer_type = result & NVME_AER_TYPE_MASK;
+	u32 aer_info = (result >> NVME_AER_INFO_SHIFT) & NVME_AER_INFO_MASK;
+
+	if (aer_type == NVME_AER_SMART &&
+	    aer_info == NVME_AER_SMART_TEMP_THRESH)
+		nvme_thermal_notify_framework(ctrl);
+}
+
 static void nvme_aen_uevent(struct nvme_ctrl *ctrl)
 {
 	char *envp[2] = { NULL, NULL };
@@ -3582,6 +3595,7 @@ static void nvme_async_event_work(struct work_struct *work)
 	struct nvme_ctrl *ctrl =
 		container_of(work, struct nvme_ctrl, async_event_work);
 
+	nvme_handle_aen_smart(ctrl, ctrl->aen_result);
 	nvme_aen_uevent(ctrl);
 	ctrl->ops->submit_async_event(ctrl);
 }
drivers/nvme/host/nvme.h +5 -0
index a9c63ea..40325b6 100644
@@ -605,6 +605,7 @@ static inline struct nvme_ns *nvme_get_ns_from_dev(struct device *dev)
 
 int nvme_thermal_zones_register(struct nvme_ctrl *ctrl);
 void nvme_thermal_zones_unregister(struct nvme_ctrl *ctrl);
+void nvme_thermal_notify_framework(struct nvme_ctrl *ctrl);
 
 #else
 
@@ -617,6 +618,10 @@ static inline void nvme_thermal_zones_unregister(struct nvme_ctrl *ctrl)
 {
 }
 
+static inline void nvme_thermal_notify_framework(struct nvme_ctrl *ctrl)
+{
+}
+
 #endif /* CONFIG_THERMAL */
 
 #endif /* _NVME_H */
drivers/nvme/host/thermal.c +12 -0
index 18d0e4c..b5e835d 100644
@@ -309,3 +309,15 @@ void nvme_thermal_zones_unregister(struct nvme_ctrl *ctrl)
 		__clear_bit(i, ctrl->tz_enabled);
 	}
 }
+
+void nvme_thermal_notify_framework(struct nvme_ctrl *ctrl)
+{
+	int i;
+
+	for_each_set_bit(i, ctrl->tz_enabled, ARRAY_SIZE(ctrl->tz)) {
+		if (ctrl->tz[i].dev)
+			thermal_notify_framework(ctrl->tz[i].dev, 0);
+		if (ctrl->tz[i].of_dev)
+			thermal_notify_framework(ctrl->tz[i].of_dev, 0);
+	}
+}
include/linux/nvme.h +7 -0
index f29728b..069b962 100644
@@ -476,6 +476,7 @@ enum {
 };
 
 enum {
+	NVME_AER_TYPE_MASK		= 0x7,
 	NVME_AER_ERROR			= 0,
 	NVME_AER_SMART			= 1,
 	NVME_AER_NOTICE			= 2,
@@ -484,6 +485,12 @@ enum {
 };
 
 enum {
+	NVME_AER_INFO_SHIFT		= 8,
+	NVME_AER_INFO_MASK		= 0xff,
+	NVME_AER_SMART_TEMP_THRESH	= 0x01,
+};
+
+enum {
 	NVME_AER_NOTICE_NS_CHANGED	= 0x00,
 	NVME_AER_NOTICE_FW_ACT_STARTING = 0x01,
 	NVME_AER_NOTICE_ANA		= 0x03,
Signature · 1 line
2.7.4
#6 lore raw

PATCH v4 3/3 nvme: notify thermal framework when temperature threshold events occur

Quoted 132 lines · Akinobu Mita

The NVMe controller supports the temperature threshold feature (Feature
Identifier 04h) that enables to configure the asynchronous event request
command to complete when the temperature is crossed its corresponding temperature threshold.

This enables the reporting of asynchronous events from the controller when
the temperature reached or exceeded a temperature threshold. In the case of the temperature threshold conditions, this notifies the
thermal framework.

The main purpose of this is to turn on a fan when overheated without polling the device for the smart log that could prevent the lower power
state transitions.

Cc Zhang Rui <rui.zhang at intel.com>
Cc Eduardo Valentin <edubezval at gmail.com>
Cc Daniel Lezcano <daniel.lezcano at linaro.org>
Cc Keith Busch <kbusch at kernel.org>
Cc Jens Axboe <axboe at fb.com>
Cc Christoph Hellwig <hch at lst.de>
Cc Sagi Grimberg <sagi at grimberg.me>
Cc Minwoo Im <minwoo.im.dev at gmail.com>
Cc Kenneth Heitke <kenneth.heitke at intel.com>
Cc Chaitanya Kulkarni <Chaitanya.Kulkarni at wdc.com>
Signed-off-by Akinobu Mita <akinobu.mita at gmail.com>

---
* v4
- support DT thermal zone device.
- use bitmap to iterate over implemented sensors

?drivers/nvme/host/core.c????| 14 ++++++++++++++
?drivers/nvme/host/nvme.h????|??5 +++++
?drivers/nvme/host/thermal.c | 12 ++++++++++++
?include/linux/nvme.h????????|??7 +++++++

?4 files changed, 38 insertions(+)

drivers/nvme/host/core.c +0 -0
index 4be339b..44dadbb 100644
@@ -1186,6 +1186,9 @@ static void nvme_enable_aen(struct nvme_ctrl
 *ctrl)

? u32 result, supported_aens = ctrl->oaes &
NVME_AEN_SUPPORTED;
? int status;
?

drivers/nvme/host/core.c +3 -0
+	if (IS_ENABLED(CONFIG_THERMAL))
+		supported_aens |= NVME_SMART_CRIT_TEMPERATURE;
+

? if (!supported_aens)
? return;
?

drivers/nvme/host/core.c +0 -0
@@ -3561,6 +3564,16 @@ void nvme_remove_namespaces(struct nvme_ctrl
 *ctrl)
 ?}
 ?EXPORT_SYMBOL_GPL(nvme_remove_namespaces);

?
+static void nvme_handle_aen_smart(struct nvme_ctrl *ctrl, u32 result)

drivers/nvme/host/core.c +3 -0
+{
+	u32 aer_type = result & NVME_AER_TYPE_MASK;
+	u32 aer_info = (result >> NVME_AER_INFO_SHIFT) &

NVME_AER_INFO_MASK;

drivers/nvme/host/core.c +6 -0
+
+	if (aer_type == NVME_AER_SMART &&
+	????aer_info == NVME_AER_SMART_TEMP_THRESH)
+		nvme_thermal_notify_framework(ctrl);
+}
+

?static void nvme_aen_uevent(struct nvme_ctrl *ctrl) ?{
? char *envp[2] = { NULL, NULL };

drivers/nvme/host/core.c +0 -0
@@ -3582,6 +3595,7 @@ static void nvme_async_event_work(struct
 work_struct *work)

? struct nvme_ctrl *ctrl =
? container_of(work, struct nvme_ctrl,
async_event_work);
?

drivers/nvme/host/core.c +1 -0
+	nvme_handle_aen_smart(ctrl, ctrl->aen_result);

? nvme_aen_uevent(ctrl);
? ctrl->ops->submit_async_event(ctrl);
?}

drivers/nvme/host/nvme.h +0 -0
index a9c63ea..40325b6 100644
@@ -605,6 +605,7 @@ static inline struct nvme_ns
 *nvme_get_ns_from_dev(struct device *dev)

?
?int nvme_thermal_zones_register(struct nvme_ctrl *ctrl); ?void nvme_thermal_zones_unregister(struct nvme_ctrl *ctrl); +void nvme_thermal_notify_framework(struct nvme_ctrl *ctrl); ?
?#else
?

drivers/nvme/host/nvme.h +0 -0
@@ -617,6 +618,10 @@ static inline void
 nvme_thermal_zones_unregister(struct nvme_ctrl *ctrl)
 ?{
 ?}

?
+static inline void nvme_thermal_notify_framework(struct nvme_ctrl *ctrl)

drivers/nvme/host/nvme.h +3 -0
+{
+}
+

?#endif /* CONFIG_THERMAL */
?
?#endif /* _NVME_H */

drivers/nvme/host/thermal.c +0 -0
index 18d0e4c..b5e835d 100644
@@ -309,3 +309,15 @@ void nvme_thermal_zones_unregister(struct
 nvme_ctrl *ctrl)
 ?		__clear_bit(i, ctrl->tz_enabled);
 ?	}

?}

drivers/nvme/host/thermal.c +6 -0
+
+void nvme_thermal_notify_framework(struct nvme_ctrl *ctrl)
+{
+	int i;
+
+	for_each_set_bit(i, ctrl->tz_enabled, ARRAY_SIZE(ctrl->tz))

{

drivers/nvme/host/thermal.c +2 -0
+		if (ctrl->tz[i].dev)
+			thermal_notify_framework(ctrl->tz[i].dev,
Quoted 1 line

0);

I'd prefer to use thermal_zone_device_update() here to notify the thermal framework.
thermal_notify_framework() will be removed sometime later.

thanks,
rui

Quoted 30 lines
drivers/nvme/host/thermal.c +2 -0
+		if (ctrl->tz[i].of_dev)
+			thermal_notify_framework(ctrl->tz[i].of_dev, 

0);

drivers/nvme/host/thermal.c +2 -0
+	}
+}
include/linux/nvme.h +0 -0
index f29728b..069b962 100644
@@ -476,6 +476,7 @@ enum {
 ?};

?
?enum {

include/linux/nvme.h +1 -0
+	NVME_AER_TYPE_MASK		= 0x7,

? NVME_AER_ERROR = 0,
? NVME_AER_SMART = 1,
? NVME_AER_NOTICE = 2,

include/linux/nvme.h +0 -0
@@ -484,6 +485,12 @@ enum {
 ?};

?
?enum {

include/linux/nvme.h +6 -0
+	NVME_AER_INFO_SHIFT		= 8,
+	NVME_AER_INFO_MASK		= 0xff,
+	NVME_AER_SMART_TEMP_THRESH	= 0x01,
+};
+
+enum {

? NVME_AER_NOTICE_NS_CHANGED = 0x00,
? NVME_AER_NOTICE_FW_ACT_STARTING = 0x01,
? NVME_AER_NOTICE_ANA = 0x03,

#7 lore raw

PATCH v4 3/3 nvme: notify thermal framework when temperature threshold events occur

2019?6?25?(?) 23:44 Zhang Rui <rui.zhang at intel.com>:

Quoted 131 lines · Akinobu Mita
Quoted 129 lines

The NVMe controller supports the temperature threshold feature (Feature
Identifier 04h) that enables to configure the asynchronous event request
command to complete when the temperature is crossed its corresponding temperature threshold.

This enables the reporting of asynchronous events from the controller when
the temperature reached or exceeded a temperature threshold. In the case of the temperature threshold conditions, this notifies the
thermal framework.

The main purpose of this is to turn on a fan when overheated without polling the device for the smart log that could prevent the lower power
state transitions.

Cc Zhang Rui <rui.zhang at intel.com>
Cc Eduardo Valentin <edubezval at gmail.com>
Cc Daniel Lezcano <daniel.lezcano at linaro.org>
Cc Keith Busch <kbusch at kernel.org>
Cc Jens Axboe <axboe at fb.com>
Cc Christoph Hellwig <hch at lst.de>
Cc Sagi Grimberg <sagi at grimberg.me>
Cc Minwoo Im <minwoo.im.dev at gmail.com>
Cc Kenneth Heitke <kenneth.heitke at intel.com>
Cc Chaitanya Kulkarni <Chaitanya.Kulkarni at wdc.com>
Signed-off-by Akinobu Mita <akinobu.mita at gmail.com>

---
* v4
- support DT thermal zone device.
- use bitmap to iterate over implemented sensors

 drivers/nvme/host/core.c    | 14 ++++++++++++++
 drivers/nvme/host/nvme.h    |  5 +++++
 drivers/nvme/host/thermal.c | 12 ++++++++++++
 include/linux/nvme.h        |  7 +++++++
 4 files changed, 38 insertions(+)
drivers/nvme/host/core.c +3 -0
index 4be339b..44dadbb 100644
@@ -1186,6 +1186,9 @@ static void nvme_enable_aen(struct nvme_ctrl
 *ctrl)
      u32 result, supported_aens = ctrl->oaes &
 NVME_AEN_SUPPORTED;
      int status;
 
+     if (IS_ENABLED(CONFIG_THERMAL))
+             supported_aens |= NVME_SMART_CRIT_TEMPERATURE;
+
      if (!supported_aens)
              return;
drivers/nvme/host/core.c +10 -0
@@ -3561,6 +3564,16 @@ void nvme_remove_namespaces(struct nvme_ctrl
 *ctrl)
 }
 EXPORT_SYMBOL_GPL(nvme_remove_namespaces);
 
+static void nvme_handle_aen_smart(struct nvme_ctrl *ctrl, u32
 result)
+{
+     u32 aer_type = result & NVME_AER_TYPE_MASK;
+     u32 aer_info = (result >> NVME_AER_INFO_SHIFT) &
 NVME_AER_INFO_MASK;
+
+     if (aer_type == NVME_AER_SMART &&
+         aer_info == NVME_AER_SMART_TEMP_THRESH)
+             nvme_thermal_notify_framework(ctrl);
+}
+

static void nvme_aen_uevent(struct nvme_ctrl *ctrl)

 {
      char *envp[2] = { NULL, NULL };
drivers/nvme/host/core.c +1 -0
@@ -3582,6 +3595,7 @@ static void nvme_async_event_work(struct
 work_struct *work)
      struct nvme_ctrl *ctrl =
              container_of(work, struct nvme_ctrl,
 async_event_work);
 
+     nvme_handle_aen_smart(ctrl, ctrl->aen_result);
      nvme_aen_uevent(ctrl);
      ctrl->ops->submit_async_event(ctrl);

}

drivers/nvme/host/nvme.h +5 -0
index a9c63ea..40325b6 100644
@@ -605,6 +605,7 @@ static inline struct nvme_ns
 *nvme_get_ns_from_dev(struct device *dev)
 
 int nvme_thermal_zones_register(struct nvme_ctrl *ctrl);
 void nvme_thermal_zones_unregister(struct nvme_ctrl *ctrl);
+void nvme_thermal_notify_framework(struct nvme_ctrl *ctrl);
 
 #else
@@ -617,6 +618,10 @@ static inline void
 nvme_thermal_zones_unregister(struct nvme_ctrl *ctrl)
 {
 }
 
+static inline void nvme_thermal_notify_framework(struct nvme_ctrl
 *ctrl)
+{
+}
+
 #endif /* CONFIG_THERMAL */

#endif /* _NVME_H */

drivers/nvme/host/thermal.c +6 -0
index 18d0e4c..b5e835d 100644
@@ -309,3 +309,15 @@ void nvme_thermal_zones_unregister(struct
 nvme_ctrl *ctrl)
              __clear_bit(i, ctrl->tz_enabled);
      }
 }
+
+void nvme_thermal_notify_framework(struct nvme_ctrl *ctrl)
+{
+     int i;
+
+     for_each_set_bit(i, ctrl->tz_enabled, ARRAY_SIZE(ctrl->tz))
Quoted 8 lines
Quoted 4 lines

{

drivers/nvme/host/thermal.c +2 -0
+             if (ctrl->tz[i].dev)
+                     thermal_notify_framework(ctrl->tz[i].dev,

0);

I'd prefer to use thermal_zone_device_update() here to notify the thermal framework.
thermal_notify_framework() will be removed sometime later.

You are right.

I got my ROCKPro64 board and just started testing with pwm-fan cooling device. And then I also noticed that using thermal_notify_framework() is insufficient when polling-delay is zero (i.e. interrupt driven thermal zone).

#8 lore raw

v4 cover nvme: add thermal zone devices

In reply to Akinobu Mita

So the code looks perfectly fine to me. But I'm still not convinced we have an important enough use case, and I'm worried about the AEN uevent implication.

Keith, Sagi, Jens, any opinion?