162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/* Copyright(c) 1999 - 2018 Intel Corporation. */
362306a36Sopenharmony_ci
462306a36Sopenharmony_ci#include "ixgbe.h"
562306a36Sopenharmony_ci#include "ixgbe_common.h"
662306a36Sopenharmony_ci#include "ixgbe_type.h"
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include <linux/module.h>
962306a36Sopenharmony_ci#include <linux/types.h>
1062306a36Sopenharmony_ci#include <linux/sysfs.h>
1162306a36Sopenharmony_ci#include <linux/kobject.h>
1262306a36Sopenharmony_ci#include <linux/device.h>
1362306a36Sopenharmony_ci#include <linux/netdevice.h>
1462306a36Sopenharmony_ci#include <linux/hwmon.h>
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci/* hwmon callback functions */
1762306a36Sopenharmony_cistatic ssize_t ixgbe_hwmon_show_location(struct device *dev,
1862306a36Sopenharmony_ci					 struct device_attribute *attr,
1962306a36Sopenharmony_ci					 char *buf)
2062306a36Sopenharmony_ci{
2162306a36Sopenharmony_ci	struct hwmon_attr *ixgbe_attr = container_of(attr, struct hwmon_attr,
2262306a36Sopenharmony_ci						     dev_attr);
2362306a36Sopenharmony_ci	return sprintf(buf, "loc%u\n",
2462306a36Sopenharmony_ci		       ixgbe_attr->sensor->location);
2562306a36Sopenharmony_ci}
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_cistatic ssize_t ixgbe_hwmon_show_temp(struct device *dev,
2862306a36Sopenharmony_ci				     struct device_attribute *attr,
2962306a36Sopenharmony_ci				     char *buf)
3062306a36Sopenharmony_ci{
3162306a36Sopenharmony_ci	struct hwmon_attr *ixgbe_attr = container_of(attr, struct hwmon_attr,
3262306a36Sopenharmony_ci						     dev_attr);
3362306a36Sopenharmony_ci	unsigned int value;
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci	/* reset the temp field */
3662306a36Sopenharmony_ci	ixgbe_attr->hw->mac.ops.get_thermal_sensor_data(ixgbe_attr->hw);
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci	value = ixgbe_attr->sensor->temp;
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci	/* display millidegree */
4162306a36Sopenharmony_ci	value *= 1000;
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci	return sprintf(buf, "%u\n", value);
4462306a36Sopenharmony_ci}
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_cistatic ssize_t ixgbe_hwmon_show_cautionthresh(struct device *dev,
4762306a36Sopenharmony_ci				     struct device_attribute *attr,
4862306a36Sopenharmony_ci				     char *buf)
4962306a36Sopenharmony_ci{
5062306a36Sopenharmony_ci	struct hwmon_attr *ixgbe_attr = container_of(attr, struct hwmon_attr,
5162306a36Sopenharmony_ci						     dev_attr);
5262306a36Sopenharmony_ci	unsigned int value = ixgbe_attr->sensor->caution_thresh;
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci	/* display millidegree */
5562306a36Sopenharmony_ci	value *= 1000;
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci	return sprintf(buf, "%u\n", value);
5862306a36Sopenharmony_ci}
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_cistatic ssize_t ixgbe_hwmon_show_maxopthresh(struct device *dev,
6162306a36Sopenharmony_ci				     struct device_attribute *attr,
6262306a36Sopenharmony_ci				     char *buf)
6362306a36Sopenharmony_ci{
6462306a36Sopenharmony_ci	struct hwmon_attr *ixgbe_attr = container_of(attr, struct hwmon_attr,
6562306a36Sopenharmony_ci						     dev_attr);
6662306a36Sopenharmony_ci	unsigned int value = ixgbe_attr->sensor->max_op_thresh;
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci	/* display millidegree */
6962306a36Sopenharmony_ci	value *= 1000;
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci	return sprintf(buf, "%u\n", value);
7262306a36Sopenharmony_ci}
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci/**
7562306a36Sopenharmony_ci * ixgbe_add_hwmon_attr - Create hwmon attr table for a hwmon sysfs file.
7662306a36Sopenharmony_ci * @adapter: pointer to the adapter structure
7762306a36Sopenharmony_ci * @offset: offset in the eeprom sensor data table
7862306a36Sopenharmony_ci * @type: type of sensor data to display
7962306a36Sopenharmony_ci *
8062306a36Sopenharmony_ci * For each file we want in hwmon's sysfs interface we need a device_attribute
8162306a36Sopenharmony_ci * This is included in our hwmon_attr struct that contains the references to
8262306a36Sopenharmony_ci * the data structures we need to get the data to display.
8362306a36Sopenharmony_ci */
8462306a36Sopenharmony_cistatic int ixgbe_add_hwmon_attr(struct ixgbe_adapter *adapter,
8562306a36Sopenharmony_ci				unsigned int offset, int type) {
8662306a36Sopenharmony_ci	int rc;
8762306a36Sopenharmony_ci	unsigned int n_attr;
8862306a36Sopenharmony_ci	struct hwmon_attr *ixgbe_attr;
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci	n_attr = adapter->ixgbe_hwmon_buff->n_hwmon;
9162306a36Sopenharmony_ci	ixgbe_attr = &adapter->ixgbe_hwmon_buff->hwmon_list[n_attr];
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci	switch (type) {
9462306a36Sopenharmony_ci	case IXGBE_HWMON_TYPE_LOC:
9562306a36Sopenharmony_ci		ixgbe_attr->dev_attr.show = ixgbe_hwmon_show_location;
9662306a36Sopenharmony_ci		snprintf(ixgbe_attr->name, sizeof(ixgbe_attr->name),
9762306a36Sopenharmony_ci			 "temp%u_label", offset + 1);
9862306a36Sopenharmony_ci		break;
9962306a36Sopenharmony_ci	case IXGBE_HWMON_TYPE_TEMP:
10062306a36Sopenharmony_ci		ixgbe_attr->dev_attr.show = ixgbe_hwmon_show_temp;
10162306a36Sopenharmony_ci		snprintf(ixgbe_attr->name, sizeof(ixgbe_attr->name),
10262306a36Sopenharmony_ci			 "temp%u_input", offset + 1);
10362306a36Sopenharmony_ci		break;
10462306a36Sopenharmony_ci	case IXGBE_HWMON_TYPE_CAUTION:
10562306a36Sopenharmony_ci		ixgbe_attr->dev_attr.show = ixgbe_hwmon_show_cautionthresh;
10662306a36Sopenharmony_ci		snprintf(ixgbe_attr->name, sizeof(ixgbe_attr->name),
10762306a36Sopenharmony_ci			 "temp%u_max", offset + 1);
10862306a36Sopenharmony_ci		break;
10962306a36Sopenharmony_ci	case IXGBE_HWMON_TYPE_MAX:
11062306a36Sopenharmony_ci		ixgbe_attr->dev_attr.show = ixgbe_hwmon_show_maxopthresh;
11162306a36Sopenharmony_ci		snprintf(ixgbe_attr->name, sizeof(ixgbe_attr->name),
11262306a36Sopenharmony_ci			 "temp%u_crit", offset + 1);
11362306a36Sopenharmony_ci		break;
11462306a36Sopenharmony_ci	default:
11562306a36Sopenharmony_ci		rc = -EPERM;
11662306a36Sopenharmony_ci		return rc;
11762306a36Sopenharmony_ci	}
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci	/* These always the same regardless of type */
12062306a36Sopenharmony_ci	ixgbe_attr->sensor =
12162306a36Sopenharmony_ci		&adapter->hw.mac.thermal_sensor_data.sensor[offset];
12262306a36Sopenharmony_ci	ixgbe_attr->hw = &adapter->hw;
12362306a36Sopenharmony_ci	ixgbe_attr->dev_attr.store = NULL;
12462306a36Sopenharmony_ci	ixgbe_attr->dev_attr.attr.mode = 0444;
12562306a36Sopenharmony_ci	ixgbe_attr->dev_attr.attr.name = ixgbe_attr->name;
12662306a36Sopenharmony_ci	sysfs_attr_init(&ixgbe_attr->dev_attr.attr);
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ci	adapter->ixgbe_hwmon_buff->attrs[n_attr] = &ixgbe_attr->dev_attr.attr;
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ci	++adapter->ixgbe_hwmon_buff->n_hwmon;
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ci	return 0;
13362306a36Sopenharmony_ci}
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_cistatic void ixgbe_sysfs_del_adapter(struct ixgbe_adapter *adapter)
13662306a36Sopenharmony_ci{
13762306a36Sopenharmony_ci}
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci/* called from ixgbe_main.c */
14062306a36Sopenharmony_civoid ixgbe_sysfs_exit(struct ixgbe_adapter *adapter)
14162306a36Sopenharmony_ci{
14262306a36Sopenharmony_ci	ixgbe_sysfs_del_adapter(adapter);
14362306a36Sopenharmony_ci}
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_ci/* called from ixgbe_main.c */
14662306a36Sopenharmony_ciint ixgbe_sysfs_init(struct ixgbe_adapter *adapter)
14762306a36Sopenharmony_ci{
14862306a36Sopenharmony_ci	struct hwmon_buff *ixgbe_hwmon;
14962306a36Sopenharmony_ci	struct device *hwmon_dev;
15062306a36Sopenharmony_ci	unsigned int i;
15162306a36Sopenharmony_ci	int rc = 0;
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ci	/* If this method isn't defined we don't support thermals */
15462306a36Sopenharmony_ci	if (adapter->hw.mac.ops.init_thermal_sensor_thresh == NULL) {
15562306a36Sopenharmony_ci		goto exit;
15662306a36Sopenharmony_ci	}
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ci	/* Don't create thermal hwmon interface if no sensors present */
15962306a36Sopenharmony_ci	if (adapter->hw.mac.ops.init_thermal_sensor_thresh(&adapter->hw))
16062306a36Sopenharmony_ci		goto exit;
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ci	ixgbe_hwmon = devm_kzalloc(&adapter->pdev->dev, sizeof(*ixgbe_hwmon),
16362306a36Sopenharmony_ci				   GFP_KERNEL);
16462306a36Sopenharmony_ci	if (ixgbe_hwmon == NULL) {
16562306a36Sopenharmony_ci		rc = -ENOMEM;
16662306a36Sopenharmony_ci		goto exit;
16762306a36Sopenharmony_ci	}
16862306a36Sopenharmony_ci	adapter->ixgbe_hwmon_buff = ixgbe_hwmon;
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_ci	for (i = 0; i < IXGBE_MAX_SENSORS; i++) {
17162306a36Sopenharmony_ci		/*
17262306a36Sopenharmony_ci		 * Only create hwmon sysfs entries for sensors that have
17362306a36Sopenharmony_ci		 * meaningful data for.
17462306a36Sopenharmony_ci		 */
17562306a36Sopenharmony_ci		if (adapter->hw.mac.thermal_sensor_data.sensor[i].location == 0)
17662306a36Sopenharmony_ci			continue;
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_ci		/* Bail if any hwmon attr struct fails to initialize */
17962306a36Sopenharmony_ci		rc = ixgbe_add_hwmon_attr(adapter, i, IXGBE_HWMON_TYPE_CAUTION);
18062306a36Sopenharmony_ci		if (rc)
18162306a36Sopenharmony_ci			goto exit;
18262306a36Sopenharmony_ci		rc = ixgbe_add_hwmon_attr(adapter, i, IXGBE_HWMON_TYPE_LOC);
18362306a36Sopenharmony_ci		if (rc)
18462306a36Sopenharmony_ci			goto exit;
18562306a36Sopenharmony_ci		rc = ixgbe_add_hwmon_attr(adapter, i, IXGBE_HWMON_TYPE_TEMP);
18662306a36Sopenharmony_ci		if (rc)
18762306a36Sopenharmony_ci			goto exit;
18862306a36Sopenharmony_ci		rc = ixgbe_add_hwmon_attr(adapter, i, IXGBE_HWMON_TYPE_MAX);
18962306a36Sopenharmony_ci		if (rc)
19062306a36Sopenharmony_ci			goto exit;
19162306a36Sopenharmony_ci	}
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_ci	ixgbe_hwmon->groups[0] = &ixgbe_hwmon->group;
19462306a36Sopenharmony_ci	ixgbe_hwmon->group.attrs = ixgbe_hwmon->attrs;
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_ci	hwmon_dev = devm_hwmon_device_register_with_groups(&adapter->pdev->dev,
19762306a36Sopenharmony_ci							   "ixgbe",
19862306a36Sopenharmony_ci							   ixgbe_hwmon,
19962306a36Sopenharmony_ci							   ixgbe_hwmon->groups);
20062306a36Sopenharmony_ci	if (IS_ERR(hwmon_dev))
20162306a36Sopenharmony_ci		rc = PTR_ERR(hwmon_dev);
20262306a36Sopenharmony_ciexit:
20362306a36Sopenharmony_ci	return rc;
20462306a36Sopenharmony_ci}
20562306a36Sopenharmony_ci
206