1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 1999 - 2018 Intel Corporation. */
5 #include "ixgbe_common.h"
6 #include "ixgbe_type.h"
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>
16 /* hwmon callback functions */
17 static ssize_t
ixgbe_hwmon_show_location(struct device
*dev
,
18 struct device_attribute
*attr
,
21 struct hwmon_attr
*ixgbe_attr
= container_of(attr
, struct hwmon_attr
,
23 return sprintf(buf
, "loc%u\n",
24 ixgbe_attr
->sensor
->location
);
27 static ssize_t
ixgbe_hwmon_show_temp(struct device
*dev
,
28 struct device_attribute
*attr
,
31 struct hwmon_attr
*ixgbe_attr
= container_of(attr
, struct hwmon_attr
,
35 /* reset the temp field */
36 ixgbe_attr
->hw
->mac
.ops
.get_thermal_sensor_data(ixgbe_attr
->hw
);
38 value
= ixgbe_attr
->sensor
->temp
;
40 /* display millidegree */
43 return sprintf(buf
, "%u\n", value
);
46 static ssize_t
ixgbe_hwmon_show_cautionthresh(struct device
*dev
,
47 struct device_attribute
*attr
,
50 struct hwmon_attr
*ixgbe_attr
= container_of(attr
, struct hwmon_attr
,
52 unsigned int value
= ixgbe_attr
->sensor
->caution_thresh
;
54 /* display millidegree */
57 return sprintf(buf
, "%u\n", value
);
60 static ssize_t
ixgbe_hwmon_show_maxopthresh(struct device
*dev
,
61 struct device_attribute
*attr
,
64 struct hwmon_attr
*ixgbe_attr
= container_of(attr
, struct hwmon_attr
,
66 unsigned int value
= ixgbe_attr
->sensor
->max_op_thresh
;
68 /* display millidegree */
71 return sprintf(buf
, "%u\n", value
);
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
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.
84 static int ixgbe_add_hwmon_attr(struct ixgbe_adapter
*adapter
,
85 unsigned int offset
, int type
) {
88 struct hwmon_attr
*ixgbe_attr
;
90 n_attr
= adapter
->ixgbe_hwmon_buff
->n_hwmon
;
91 ixgbe_attr
= &adapter
->ixgbe_hwmon_buff
->hwmon_list
[n_attr
];
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);
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);
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);
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);
119 /* These always the same regardless of type */
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
);
128 adapter
->ixgbe_hwmon_buff
->attrs
[n_attr
] = &ixgbe_attr
->dev_attr
.attr
;
130 ++adapter
->ixgbe_hwmon_buff
->n_hwmon
;
135 static void ixgbe_sysfs_del_adapter(struct ixgbe_adapter
*adapter
)
139 /* called from ixgbe_main.c */
140 void ixgbe_sysfs_exit(struct ixgbe_adapter
*adapter
)
142 ixgbe_sysfs_del_adapter(adapter
);
145 /* called from ixgbe_main.c */
146 int ixgbe_sysfs_init(struct ixgbe_adapter
*adapter
)
148 struct hwmon_buff
*ixgbe_hwmon
;
149 struct device
*hwmon_dev
;
153 /* If this method isn't defined we don't support thermals */
154 if (adapter
->hw
.mac
.ops
.init_thermal_sensor_thresh
== NULL
) {
158 /* Don't create thermal hwmon interface if no sensors present */
159 if (adapter
->hw
.mac
.ops
.init_thermal_sensor_thresh(&adapter
->hw
))
162 ixgbe_hwmon
= devm_kzalloc(&adapter
->pdev
->dev
, sizeof(*ixgbe_hwmon
),
164 if (ixgbe_hwmon
== NULL
) {
168 adapter
->ixgbe_hwmon_buff
= ixgbe_hwmon
;
170 for (i
= 0; i
< IXGBE_MAX_SENSORS
; i
++) {
172 * Only create hwmon sysfs entries for sensors that have
173 * meaningful data for.
175 if (adapter
->hw
.mac
.thermal_sensor_data
.sensor
[i
].location
== 0)
178 /* Bail if any hwmon attr struct fails to initialize */
179 rc
= ixgbe_add_hwmon_attr(adapter
, i
, IXGBE_HWMON_TYPE_CAUTION
);
182 rc
= ixgbe_add_hwmon_attr(adapter
, i
, IXGBE_HWMON_TYPE_LOC
);
185 rc
= ixgbe_add_hwmon_attr(adapter
, i
, IXGBE_HWMON_TYPE_TEMP
);
188 rc
= ixgbe_add_hwmon_attr(adapter
, i
, IXGBE_HWMON_TYPE_MAX
);
193 ixgbe_hwmon
->groups
[0] = &ixgbe_hwmon
->group
;
194 ixgbe_hwmon
->group
.attrs
= ixgbe_hwmon
->attrs
;
196 hwmon_dev
= devm_hwmon_device_register_with_groups(&adapter
->pdev
->dev
,
199 ixgbe_hwmon
->groups
);
200 if (IS_ERR(hwmon_dev
))
201 rc
= PTR_ERR(hwmon_dev
);