Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0
  2/* Copyright(c) 1999 - 2018 Intel Corporation. */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  3
  4#include "ixgbe.h"
  5#include "ixgbe_common.h"
  6#include "ixgbe_type.h"
  7
  8#include <linux/module.h>
  9#include <linux/types.h>
 10#include <linux/sysfs.h>
 11#include <linux/kobject.h>
 12#include <linux/device.h>
 13#include <linux/netdevice.h>
 14#include <linux/hwmon.h>
 15
 
 16/* hwmon callback functions */
 17static ssize_t ixgbe_hwmon_show_location(struct device *dev,
 18					 struct device_attribute *attr,
 19					 char *buf)
 20{
 21	struct hwmon_attr *ixgbe_attr = container_of(attr, struct hwmon_attr,
 22						     dev_attr);
 23	return sprintf(buf, "loc%u\n",
 24		       ixgbe_attr->sensor->location);
 25}
 26
 27static ssize_t ixgbe_hwmon_show_temp(struct device *dev,
 28				     struct device_attribute *attr,
 29				     char *buf)
 30{
 31	struct hwmon_attr *ixgbe_attr = container_of(attr, struct hwmon_attr,
 32						     dev_attr);
 33	unsigned int value;
 34
 35	/* reset the temp field */
 36	ixgbe_attr->hw->mac.ops.get_thermal_sensor_data(ixgbe_attr->hw);
 37
 38	value = ixgbe_attr->sensor->temp;
 39
 40	/* display millidegree */
 41	value *= 1000;
 42
 43	return sprintf(buf, "%u\n", value);
 44}
 45
 46static ssize_t ixgbe_hwmon_show_cautionthresh(struct device *dev,
 47				     struct device_attribute *attr,
 48				     char *buf)
 49{
 50	struct hwmon_attr *ixgbe_attr = container_of(attr, struct hwmon_attr,
 51						     dev_attr);
 52	unsigned int value = ixgbe_attr->sensor->caution_thresh;
 53
 54	/* display millidegree */
 55	value *= 1000;
 56
 57	return sprintf(buf, "%u\n", value);
 58}
 59
 60static ssize_t ixgbe_hwmon_show_maxopthresh(struct device *dev,
 61				     struct device_attribute *attr,
 62				     char *buf)
 63{
 64	struct hwmon_attr *ixgbe_attr = container_of(attr, struct hwmon_attr,
 65						     dev_attr);
 66	unsigned int value = ixgbe_attr->sensor->max_op_thresh;
 67
 68	/* display millidegree */
 69	value *= 1000;
 70
 71	return sprintf(buf, "%u\n", value);
 72}
 73
 74/**
 75 * ixgbe_add_hwmon_attr - Create hwmon attr table for a hwmon sysfs file.
 76 * @adapter: pointer to the adapter structure
 77 * @offset: offset in the eeprom sensor data table
 78 * @type: type of sensor data to display
 79 *
 80 * For each file we want in hwmon's sysfs interface we need a device_attribute
 81 * This is included in our hwmon_attr struct that contains the references to
 82 * the data structures we need to get the data to display.
 83 */
 84static int ixgbe_add_hwmon_attr(struct ixgbe_adapter *adapter,
 85				unsigned int offset, int type) {
 86	int rc;
 87	unsigned int n_attr;
 88	struct hwmon_attr *ixgbe_attr;
 89
 90	n_attr = adapter->ixgbe_hwmon_buff->n_hwmon;
 91	ixgbe_attr = &adapter->ixgbe_hwmon_buff->hwmon_list[n_attr];
 92
 93	switch (type) {
 94	case IXGBE_HWMON_TYPE_LOC:
 95		ixgbe_attr->dev_attr.show = ixgbe_hwmon_show_location;
 96		snprintf(ixgbe_attr->name, sizeof(ixgbe_attr->name),
 97			 "temp%u_label", offset + 1);
 98		break;
 99	case IXGBE_HWMON_TYPE_TEMP:
100		ixgbe_attr->dev_attr.show = ixgbe_hwmon_show_temp;
101		snprintf(ixgbe_attr->name, sizeof(ixgbe_attr->name),
102			 "temp%u_input", offset + 1);
103		break;
104	case IXGBE_HWMON_TYPE_CAUTION:
105		ixgbe_attr->dev_attr.show = ixgbe_hwmon_show_cautionthresh;
106		snprintf(ixgbe_attr->name, sizeof(ixgbe_attr->name),
107			 "temp%u_max", offset + 1);
108		break;
109	case IXGBE_HWMON_TYPE_MAX:
110		ixgbe_attr->dev_attr.show = ixgbe_hwmon_show_maxopthresh;
111		snprintf(ixgbe_attr->name, sizeof(ixgbe_attr->name),
112			 "temp%u_crit", offset + 1);
113		break;
114	default:
115		rc = -EPERM;
116		return rc;
117	}
118
119	/* These always the same regardless of type */
120	ixgbe_attr->sensor =
121		&adapter->hw.mac.thermal_sensor_data.sensor[offset];
122	ixgbe_attr->hw = &adapter->hw;
123	ixgbe_attr->dev_attr.store = NULL;
124	ixgbe_attr->dev_attr.attr.mode = 0444;
125	ixgbe_attr->dev_attr.attr.name = ixgbe_attr->name;
126	sysfs_attr_init(&ixgbe_attr->dev_attr.attr);
127
128	adapter->ixgbe_hwmon_buff->attrs[n_attr] = &ixgbe_attr->dev_attr.attr;
 
129
130	++adapter->ixgbe_hwmon_buff->n_hwmon;
 
131
132	return 0;
133}
134
135static void ixgbe_sysfs_del_adapter(struct ixgbe_adapter *adapter)
136{
 
 
 
 
 
 
 
 
 
 
 
 
 
 
137}
138
139/* called from ixgbe_main.c */
140void ixgbe_sysfs_exit(struct ixgbe_adapter *adapter)
141{
142	ixgbe_sysfs_del_adapter(adapter);
143}
144
145/* called from ixgbe_main.c */
146int ixgbe_sysfs_init(struct ixgbe_adapter *adapter)
147{
148	struct hwmon_buff *ixgbe_hwmon;
149	struct device *hwmon_dev;
150	unsigned int i;
 
151	int rc = 0;
152
153	/* If this method isn't defined we don't support thermals */
154	if (adapter->hw.mac.ops.init_thermal_sensor_thresh == NULL) {
155		goto exit;
156	}
157
158	/* Don't create thermal hwmon interface if no sensors present */
159	if (adapter->hw.mac.ops.init_thermal_sensor_thresh(&adapter->hw))
160		goto exit;
161
162	ixgbe_hwmon = devm_kzalloc(&adapter->pdev->dev, sizeof(*ixgbe_hwmon),
163				   GFP_KERNEL);
164	if (ixgbe_hwmon == NULL) {
 
 
 
 
 
165		rc = -ENOMEM;
166		goto exit;
 
 
 
 
 
 
167	}
168	adapter->ixgbe_hwmon_buff = ixgbe_hwmon;
169
170	for (i = 0; i < IXGBE_MAX_SENSORS; i++) {
171		/*
172		 * Only create hwmon sysfs entries for sensors that have
173		 * meaningful data for.
174		 */
175		if (adapter->hw.mac.thermal_sensor_data.sensor[i].location == 0)
176			continue;
177
178		/* Bail if any hwmon attr struct fails to initialize */
179		rc = ixgbe_add_hwmon_attr(adapter, i, IXGBE_HWMON_TYPE_CAUTION);
 
 
 
180		if (rc)
181			goto exit;
182		rc = ixgbe_add_hwmon_attr(adapter, i, IXGBE_HWMON_TYPE_LOC);
183		if (rc)
184			goto exit;
185		rc = ixgbe_add_hwmon_attr(adapter, i, IXGBE_HWMON_TYPE_TEMP);
186		if (rc)
187			goto exit;
188		rc = ixgbe_add_hwmon_attr(adapter, i, IXGBE_HWMON_TYPE_MAX);
189		if (rc)
190			goto exit;
191	}
192
193	ixgbe_hwmon->groups[0] = &ixgbe_hwmon->group;
194	ixgbe_hwmon->group.attrs = ixgbe_hwmon->attrs;
195
196	hwmon_dev = devm_hwmon_device_register_with_groups(&adapter->pdev->dev,
197							   "ixgbe",
198							   ixgbe_hwmon,
199							   ixgbe_hwmon->groups);
200	if (IS_ERR(hwmon_dev))
201		rc = PTR_ERR(hwmon_dev);
202exit:
203	return rc;
204}
 
205
v3.5.6
  1/*******************************************************************************
  2
  3  Intel 10 Gigabit PCI Express Linux driver
  4  Copyright(c) 1999 - 2012 Intel Corporation.
  5
  6  This program is free software; you can redistribute it and/or modify it
  7  under the terms and conditions of the GNU General Public License,
  8  version 2, as published by the Free Software Foundation.
  9
 10  This program is distributed in the hope it will be useful, but WITHOUT
 11  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 13  more details.
 14
 15  You should have received a copy of the GNU General Public License along with
 16  this program; if not, write to the Free Software Foundation, Inc.,
 17  51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 18
 19  The full GNU General Public License is included in this distribution in
 20  the file called "COPYING".
 21
 22  Contact Information:
 23  e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
 24  Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
 25
 26*******************************************************************************/
 27
 28#include "ixgbe.h"
 29#include "ixgbe_common.h"
 30#include "ixgbe_type.h"
 31
 32#include <linux/module.h>
 33#include <linux/types.h>
 34#include <linux/sysfs.h>
 35#include <linux/kobject.h>
 36#include <linux/device.h>
 37#include <linux/netdevice.h>
 38#include <linux/hwmon.h>
 39
 40#ifdef CONFIG_IXGBE_HWMON
 41/* hwmon callback functions */
 42static ssize_t ixgbe_hwmon_show_location(struct device *dev,
 43					 struct device_attribute *attr,
 44					 char *buf)
 45{
 46	struct hwmon_attr *ixgbe_attr = container_of(attr, struct hwmon_attr,
 47						     dev_attr);
 48	return sprintf(buf, "loc%u\n",
 49		       ixgbe_attr->sensor->location);
 50}
 51
 52static ssize_t ixgbe_hwmon_show_temp(struct device *dev,
 53				     struct device_attribute *attr,
 54				     char *buf)
 55{
 56	struct hwmon_attr *ixgbe_attr = container_of(attr, struct hwmon_attr,
 57						     dev_attr);
 58	unsigned int value;
 59
 60	/* reset the temp field */
 61	ixgbe_attr->hw->mac.ops.get_thermal_sensor_data(ixgbe_attr->hw);
 62
 63	value = ixgbe_attr->sensor->temp;
 64
 65	/* display millidegree */
 66	value *= 1000;
 67
 68	return sprintf(buf, "%u\n", value);
 69}
 70
 71static ssize_t ixgbe_hwmon_show_cautionthresh(struct device *dev,
 72				     struct device_attribute *attr,
 73				     char *buf)
 74{
 75	struct hwmon_attr *ixgbe_attr = container_of(attr, struct hwmon_attr,
 76						     dev_attr);
 77	unsigned int value = ixgbe_attr->sensor->caution_thresh;
 78
 79	/* display millidegree */
 80	value *= 1000;
 81
 82	return sprintf(buf, "%u\n", value);
 83}
 84
 85static ssize_t ixgbe_hwmon_show_maxopthresh(struct device *dev,
 86				     struct device_attribute *attr,
 87				     char *buf)
 88{
 89	struct hwmon_attr *ixgbe_attr = container_of(attr, struct hwmon_attr,
 90						     dev_attr);
 91	unsigned int value = ixgbe_attr->sensor->max_op_thresh;
 92
 93	/* display millidegree */
 94	value *= 1000;
 95
 96	return sprintf(buf, "%u\n", value);
 97}
 98
 99/*
100 * ixgbe_add_hwmon_attr - Create hwmon attr table for a hwmon sysfs file.
101 * @ adapter: pointer to the adapter structure
102 * @ offset: offset in the eeprom sensor data table
103 * @ type: type of sensor data to display
104 *
105 * For each file we want in hwmon's sysfs interface we need a device_attribute
106 * This is included in our hwmon_attr struct that contains the references to
107 * the data structures we need to get the data to display.
108 */
109static int ixgbe_add_hwmon_attr(struct ixgbe_adapter *adapter,
110				unsigned int offset, int type) {
111	int rc;
112	unsigned int n_attr;
113	struct hwmon_attr *ixgbe_attr;
114
115	n_attr = adapter->ixgbe_hwmon_buff.n_hwmon;
116	ixgbe_attr = &adapter->ixgbe_hwmon_buff.hwmon_list[n_attr];
117
118	switch (type) {
119	case IXGBE_HWMON_TYPE_LOC:
120		ixgbe_attr->dev_attr.show = ixgbe_hwmon_show_location;
121		snprintf(ixgbe_attr->name, sizeof(ixgbe_attr->name),
122			 "temp%u_label", offset);
123		break;
124	case IXGBE_HWMON_TYPE_TEMP:
125		ixgbe_attr->dev_attr.show = ixgbe_hwmon_show_temp;
126		snprintf(ixgbe_attr->name, sizeof(ixgbe_attr->name),
127			 "temp%u_input", offset);
128		break;
129	case IXGBE_HWMON_TYPE_CAUTION:
130		ixgbe_attr->dev_attr.show = ixgbe_hwmon_show_cautionthresh;
131		snprintf(ixgbe_attr->name, sizeof(ixgbe_attr->name),
132			 "temp%u_max", offset);
133		break;
134	case IXGBE_HWMON_TYPE_MAX:
135		ixgbe_attr->dev_attr.show = ixgbe_hwmon_show_maxopthresh;
136		snprintf(ixgbe_attr->name, sizeof(ixgbe_attr->name),
137			 "temp%u_crit", offset);
138		break;
139	default:
140		rc = -EPERM;
141		return rc;
142	}
143
144	/* These always the same regardless of type */
145	ixgbe_attr->sensor =
146		&adapter->hw.mac.thermal_sensor_data.sensor[offset];
147	ixgbe_attr->hw = &adapter->hw;
148	ixgbe_attr->dev_attr.store = NULL;
149	ixgbe_attr->dev_attr.attr.mode = S_IRUGO;
150	ixgbe_attr->dev_attr.attr.name = ixgbe_attr->name;
 
151
152	rc = device_create_file(&adapter->pdev->dev,
153				&ixgbe_attr->dev_attr);
154
155	if (rc == 0)
156		++adapter->ixgbe_hwmon_buff.n_hwmon;
157
158	return rc;
159}
160
161static void ixgbe_sysfs_del_adapter(struct ixgbe_adapter *adapter)
162{
163	int i;
164
165	if (adapter == NULL)
166		return;
167
168	for (i = 0; i < adapter->ixgbe_hwmon_buff.n_hwmon; i++) {
169		device_remove_file(&adapter->pdev->dev,
170			   &adapter->ixgbe_hwmon_buff.hwmon_list[i].dev_attr);
171	}
172
173	kfree(adapter->ixgbe_hwmon_buff.hwmon_list);
174
175	if (adapter->ixgbe_hwmon_buff.device)
176		hwmon_device_unregister(adapter->ixgbe_hwmon_buff.device);
177}
178
179/* called from ixgbe_main.c */
180void ixgbe_sysfs_exit(struct ixgbe_adapter *adapter)
181{
182	ixgbe_sysfs_del_adapter(adapter);
183}
184
185/* called from ixgbe_main.c */
186int ixgbe_sysfs_init(struct ixgbe_adapter *adapter)
187{
188	struct hwmon_buff *ixgbe_hwmon = &adapter->ixgbe_hwmon_buff;
 
189	unsigned int i;
190	int n_attrs;
191	int rc = 0;
192
193	/* If this method isn't defined we don't support thermals */
194	if (adapter->hw.mac.ops.init_thermal_sensor_thresh == NULL) {
195		goto exit;
196	}
197
198	/* Don't create thermal hwmon interface if no sensors present */
199	if (adapter->hw.mac.ops.init_thermal_sensor_thresh(&adapter->hw))
200		goto exit;
201
202	/*
203	 * Allocation space for max attributs
204	 * max num sensors * values (loc, temp, max, caution)
205	 */
206	n_attrs = IXGBE_MAX_SENSORS * 4;
207	ixgbe_hwmon->hwmon_list = kcalloc(n_attrs, sizeof(struct hwmon_attr),
208					  GFP_KERNEL);
209	if (!ixgbe_hwmon->hwmon_list) {
210		rc = -ENOMEM;
211		goto err;
212	}
213
214	ixgbe_hwmon->device = hwmon_device_register(&adapter->pdev->dev);
215	if (IS_ERR(ixgbe_hwmon->device)) {
216		rc = PTR_ERR(ixgbe_hwmon->device);
217		goto err;
218	}
 
219
220	for (i = 0; i < IXGBE_MAX_SENSORS; i++) {
221		/*
222		 * Only create hwmon sysfs entries for sensors that have
223		 * meaningful data for.
224		 */
225		if (adapter->hw.mac.thermal_sensor_data.sensor[i].location == 0)
226			continue;
227
228		/* Bail if any hwmon attr struct fails to initialize */
229		rc = ixgbe_add_hwmon_attr(adapter, i, IXGBE_HWMON_TYPE_CAUTION);
230		rc |= ixgbe_add_hwmon_attr(adapter, i, IXGBE_HWMON_TYPE_LOC);
231		rc |= ixgbe_add_hwmon_attr(adapter, i, IXGBE_HWMON_TYPE_TEMP);
232		rc |= ixgbe_add_hwmon_attr(adapter, i, IXGBE_HWMON_TYPE_MAX);
233		if (rc)
234			goto err;
 
 
 
 
 
 
 
 
 
235	}
236
237	goto exit;
 
238
239err:
240	ixgbe_sysfs_del_adapter(adapter);
 
 
 
 
241exit:
242	return rc;
243}
244#endif /* CONFIG_IXGBE_HWMON */
245