@@ -0,0 +1,291 @@
+// 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)
+{
+ struct nvme_smart_log *log;
+ int ret;
+
+ BUILD_BUG_ON(ARRAY_SIZE(log->temp_sensor) + 1 != ARRAY_SIZE(ctrl->tz));
+
+ 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,
+ 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) (((t) * 1000) - 273150)
+#define MILLICELSIUS_TO_KELVIN(t) (((t) + 273150) / 1000)
+#define MILLICELSIUS_TO_KELVIN_ROUNDUP(t) MILLICELSIUS_TO_KELVIN((t) + 999)
+
+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]);
+ 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)
+{
+ 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)
+{
+ *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;
+ 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,
+ &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;
+ 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,
+ 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]);
+ 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]);
+
+ temp = MILLICELSIUS_TO_KELVIN_ROUNDUP(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 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->sensor = sensor;
+ tz->params = nvme_tz_params;
+ snprintf(name, sizeof(name), "nvme%d_temp%u", ctrl->instance, sensor);
+
+ tzdev = thermal_zone_device_register(name, 1, 1, tz, &nvme_tz_ops,
+ &tz->params, 0, 0);
+ if (IS_ERR(tzdev)) {
+ dev_err(ctrl->device,
+ "Failed to register thermal zone device: %ld\n",
+ PTR_ERR(tzdev));
+ return PTR_ERR(tzdev);
+ }
+
+ snprintf(name, sizeof(name), "temp%d", sensor);
+ ret = sysfs_create_link(&ctrl->ctrl_device.kobj, &tzdev->device.kobj,
+ name);
+ if (ret)
+ goto device_unregister;
+
+ ret = sysfs_create_link(&tzdev->device.kobj,
+ &ctrl->ctrl_device.kobj, "device");
+ if (ret)
+ goto remove_sensor_link;
+
+ tz->dev = tzdev;
+
+ return 0;
+
+remove_sensor_link:
+ sysfs_remove_link(&ctrl->ctrl_device.kobj, name);
+device_unregister:
+ thermal_zone_device_unregister(tzdev);
+
+ 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.
+ */
+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,
+ sizeof(*log), 0);
+ if (err) {
+ dev_err(ctrl->device, "Failed to get SMART log: %d\n", err);
+ /* If the device provided a response, then it's non-fatal */
+ ret = (err > 0) ? 0 : err;
+ goto out;
+ }
+
+ for_each_clear_bit(i, ctrl->tz_enabled, ARRAY_SIZE(ctrl->tz)) {
+ 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
+ * in temperature sensor fields in the smart log page.
+ */
+ 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.
+ */
+void nvme_thermal_zones_unregister(struct nvme_ctrl *ctrl)
+{
+ int i;
+
+ for_each_set_bit(i, ctrl->tz_enabled, ARRAY_SIZE(ctrl->tz)) {
+ struct nvme_tz *tz = &ctrl->tz[i];
+
+ if (tz->dev) {
+ char name[20];
+
+ 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);
+ }
+}