Jeff Kirsher | ae06c70 | 2018-03-22 10:08:48 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Carolyn Wyborny | e52c0f9 | 2014-04-11 01:46:06 +0000 | [diff] [blame] | 2 | /* Intel(R) Gigabit Ethernet Linux driver |
| 3 | * Copyright(c) 2007-2014 Intel Corporation. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms and conditions of the GNU General Public License, |
| 7 | * version 2, as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along with |
| 15 | * this program; if not, see <http://www.gnu.org/licenses/>. |
| 16 | * |
| 17 | * The full GNU General Public License is included in this distribution in |
| 18 | * the file called "COPYING". |
| 19 | * |
| 20 | * Contact Information: |
| 21 | * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net> |
| 22 | * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497 |
| 23 | */ |
Carolyn Wyborny | e428893 | 2012-12-07 03:01:42 +0000 | [diff] [blame] | 24 | |
| 25 | #include "igb.h" |
| 26 | #include "e1000_82575.h" |
| 27 | #include "e1000_hw.h" |
| 28 | |
| 29 | #include <linux/module.h> |
| 30 | #include <linux/types.h> |
| 31 | #include <linux/sysfs.h> |
| 32 | #include <linux/kobject.h> |
| 33 | #include <linux/device.h> |
| 34 | #include <linux/netdevice.h> |
| 35 | #include <linux/hwmon.h> |
| 36 | #include <linux/pci.h> |
| 37 | |
| 38 | #ifdef CONFIG_IGB_HWMON |
Stephen Hemminger | 05ec29e | 2013-03-20 09:06:29 +0000 | [diff] [blame] | 39 | static struct i2c_board_info i350_sensor_info = { |
Carolyn Wyborny | 603e86f | 2013-02-20 07:40:55 +0000 | [diff] [blame] | 40 | I2C_BOARD_INFO("i350bb", (0Xf8 >> 1)), |
| 41 | }; |
| 42 | |
Carolyn Wyborny | e428893 | 2012-12-07 03:01:42 +0000 | [diff] [blame] | 43 | /* hwmon callback functions */ |
| 44 | static ssize_t igb_hwmon_show_location(struct device *dev, |
Jeff Kirsher | b980ac1 | 2013-02-23 07:29:56 +0000 | [diff] [blame] | 45 | struct device_attribute *attr, |
| 46 | char *buf) |
Carolyn Wyborny | e428893 | 2012-12-07 03:01:42 +0000 | [diff] [blame] | 47 | { |
| 48 | struct hwmon_attr *igb_attr = container_of(attr, struct hwmon_attr, |
Jeff Kirsher | b980ac1 | 2013-02-23 07:29:56 +0000 | [diff] [blame] | 49 | dev_attr); |
Carolyn Wyborny | e428893 | 2012-12-07 03:01:42 +0000 | [diff] [blame] | 50 | return sprintf(buf, "loc%u\n", |
| 51 | igb_attr->sensor->location); |
| 52 | } |
| 53 | |
| 54 | static ssize_t igb_hwmon_show_temp(struct device *dev, |
Jeff Kirsher | b980ac1 | 2013-02-23 07:29:56 +0000 | [diff] [blame] | 55 | struct device_attribute *attr, |
| 56 | char *buf) |
Carolyn Wyborny | e428893 | 2012-12-07 03:01:42 +0000 | [diff] [blame] | 57 | { |
| 58 | struct hwmon_attr *igb_attr = container_of(attr, struct hwmon_attr, |
Jeff Kirsher | b980ac1 | 2013-02-23 07:29:56 +0000 | [diff] [blame] | 59 | dev_attr); |
Carolyn Wyborny | e428893 | 2012-12-07 03:01:42 +0000 | [diff] [blame] | 60 | unsigned int value; |
| 61 | |
| 62 | /* reset the temp field */ |
| 63 | igb_attr->hw->mac.ops.get_thermal_sensor_data(igb_attr->hw); |
| 64 | |
| 65 | value = igb_attr->sensor->temp; |
| 66 | |
| 67 | /* display millidegree */ |
| 68 | value *= 1000; |
| 69 | |
| 70 | return sprintf(buf, "%u\n", value); |
| 71 | } |
| 72 | |
| 73 | static ssize_t igb_hwmon_show_cautionthresh(struct device *dev, |
Jeff Kirsher | b980ac1 | 2013-02-23 07:29:56 +0000 | [diff] [blame] | 74 | struct device_attribute *attr, |
| 75 | char *buf) |
Carolyn Wyborny | e428893 | 2012-12-07 03:01:42 +0000 | [diff] [blame] | 76 | { |
| 77 | struct hwmon_attr *igb_attr = container_of(attr, struct hwmon_attr, |
Jeff Kirsher | b980ac1 | 2013-02-23 07:29:56 +0000 | [diff] [blame] | 78 | dev_attr); |
Carolyn Wyborny | e428893 | 2012-12-07 03:01:42 +0000 | [diff] [blame] | 79 | unsigned int value = igb_attr->sensor->caution_thresh; |
| 80 | |
| 81 | /* display millidegree */ |
| 82 | value *= 1000; |
| 83 | |
| 84 | return sprintf(buf, "%u\n", value); |
| 85 | } |
| 86 | |
| 87 | static ssize_t igb_hwmon_show_maxopthresh(struct device *dev, |
Jeff Kirsher | b980ac1 | 2013-02-23 07:29:56 +0000 | [diff] [blame] | 88 | struct device_attribute *attr, |
| 89 | char *buf) |
Carolyn Wyborny | e428893 | 2012-12-07 03:01:42 +0000 | [diff] [blame] | 90 | { |
| 91 | struct hwmon_attr *igb_attr = container_of(attr, struct hwmon_attr, |
Jeff Kirsher | b980ac1 | 2013-02-23 07:29:56 +0000 | [diff] [blame] | 92 | dev_attr); |
Carolyn Wyborny | e428893 | 2012-12-07 03:01:42 +0000 | [diff] [blame] | 93 | unsigned int value = igb_attr->sensor->max_op_thresh; |
| 94 | |
| 95 | /* display millidegree */ |
| 96 | value *= 1000; |
| 97 | |
| 98 | return sprintf(buf, "%u\n", value); |
| 99 | } |
| 100 | |
| 101 | /* igb_add_hwmon_attr - Create hwmon attr table for a hwmon sysfs file. |
| 102 | * @ adapter: pointer to the adapter structure |
| 103 | * @ offset: offset in the eeprom sensor data table |
| 104 | * @ type: type of sensor data to display |
| 105 | * |
| 106 | * For each file we want in hwmon's sysfs interface we need a device_attribute |
| 107 | * This is included in our hwmon_attr struct that contains the references to |
| 108 | * the data structures we need to get the data to display. |
| 109 | */ |
| 110 | static int igb_add_hwmon_attr(struct igb_adapter *adapter, |
Jeff Kirsher | b980ac1 | 2013-02-23 07:29:56 +0000 | [diff] [blame] | 111 | unsigned int offset, int type) |
| 112 | { |
Carolyn Wyborny | e428893 | 2012-12-07 03:01:42 +0000 | [diff] [blame] | 113 | int rc; |
| 114 | unsigned int n_attr; |
| 115 | struct hwmon_attr *igb_attr; |
| 116 | |
Guenter Roeck | e3670b8 | 2013-11-26 07:15:23 +0000 | [diff] [blame] | 117 | n_attr = adapter->igb_hwmon_buff->n_hwmon; |
| 118 | igb_attr = &adapter->igb_hwmon_buff->hwmon_list[n_attr]; |
Carolyn Wyborny | e428893 | 2012-12-07 03:01:42 +0000 | [diff] [blame] | 119 | |
| 120 | switch (type) { |
| 121 | case IGB_HWMON_TYPE_LOC: |
| 122 | igb_attr->dev_attr.show = igb_hwmon_show_location; |
| 123 | snprintf(igb_attr->name, sizeof(igb_attr->name), |
Guenter Roeck | e6e25bb | 2013-11-26 07:15:34 +0000 | [diff] [blame] | 124 | "temp%u_label", offset + 1); |
Carolyn Wyborny | e428893 | 2012-12-07 03:01:42 +0000 | [diff] [blame] | 125 | break; |
| 126 | case IGB_HWMON_TYPE_TEMP: |
| 127 | igb_attr->dev_attr.show = igb_hwmon_show_temp; |
| 128 | snprintf(igb_attr->name, sizeof(igb_attr->name), |
Guenter Roeck | e6e25bb | 2013-11-26 07:15:34 +0000 | [diff] [blame] | 129 | "temp%u_input", offset + 1); |
Carolyn Wyborny | e428893 | 2012-12-07 03:01:42 +0000 | [diff] [blame] | 130 | break; |
| 131 | case IGB_HWMON_TYPE_CAUTION: |
| 132 | igb_attr->dev_attr.show = igb_hwmon_show_cautionthresh; |
| 133 | snprintf(igb_attr->name, sizeof(igb_attr->name), |
Guenter Roeck | e6e25bb | 2013-11-26 07:15:34 +0000 | [diff] [blame] | 134 | "temp%u_max", offset + 1); |
Carolyn Wyborny | e428893 | 2012-12-07 03:01:42 +0000 | [diff] [blame] | 135 | break; |
| 136 | case IGB_HWMON_TYPE_MAX: |
| 137 | igb_attr->dev_attr.show = igb_hwmon_show_maxopthresh; |
| 138 | snprintf(igb_attr->name, sizeof(igb_attr->name), |
Guenter Roeck | e6e25bb | 2013-11-26 07:15:34 +0000 | [diff] [blame] | 139 | "temp%u_crit", offset + 1); |
Carolyn Wyborny | e428893 | 2012-12-07 03:01:42 +0000 | [diff] [blame] | 140 | break; |
| 141 | default: |
| 142 | rc = -EPERM; |
| 143 | return rc; |
| 144 | } |
| 145 | |
| 146 | /* These always the same regardless of type */ |
| 147 | igb_attr->sensor = |
| 148 | &adapter->hw.mac.thermal_sensor_data.sensor[offset]; |
| 149 | igb_attr->hw = &adapter->hw; |
| 150 | igb_attr->dev_attr.store = NULL; |
Joe Perches | d3757ba | 2018-03-23 16:34:44 -0700 | [diff] [blame] | 151 | igb_attr->dev_attr.attr.mode = 0444; |
Carolyn Wyborny | e428893 | 2012-12-07 03:01:42 +0000 | [diff] [blame] | 152 | igb_attr->dev_attr.attr.name = igb_attr->name; |
| 153 | sysfs_attr_init(&igb_attr->dev_attr.attr); |
Carolyn Wyborny | e428893 | 2012-12-07 03:01:42 +0000 | [diff] [blame] | 154 | |
Guenter Roeck | e3670b8 | 2013-11-26 07:15:23 +0000 | [diff] [blame] | 155 | adapter->igb_hwmon_buff->attrs[n_attr] = &igb_attr->dev_attr.attr; |
| 156 | |
| 157 | ++adapter->igb_hwmon_buff->n_hwmon; |
| 158 | |
| 159 | return 0; |
Carolyn Wyborny | e428893 | 2012-12-07 03:01:42 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | static void igb_sysfs_del_adapter(struct igb_adapter *adapter) |
| 163 | { |
Carolyn Wyborny | e428893 | 2012-12-07 03:01:42 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /* called from igb_main.c */ |
| 167 | void igb_sysfs_exit(struct igb_adapter *adapter) |
| 168 | { |
| 169 | igb_sysfs_del_adapter(adapter); |
| 170 | } |
| 171 | |
| 172 | /* called from igb_main.c */ |
| 173 | int igb_sysfs_init(struct igb_adapter *adapter) |
| 174 | { |
Guenter Roeck | e3670b8 | 2013-11-26 07:15:23 +0000 | [diff] [blame] | 175 | struct hwmon_buff *igb_hwmon; |
| 176 | struct i2c_client *client; |
| 177 | struct device *hwmon_dev; |
Carolyn Wyborny | e428893 | 2012-12-07 03:01:42 +0000 | [diff] [blame] | 178 | unsigned int i; |
Carolyn Wyborny | e428893 | 2012-12-07 03:01:42 +0000 | [diff] [blame] | 179 | int rc = 0; |
| 180 | |
| 181 | /* If this method isn't defined we don't support thermals */ |
| 182 | if (adapter->hw.mac.ops.init_thermal_sensor_thresh == NULL) |
| 183 | goto exit; |
| 184 | |
| 185 | /* Don't create thermal hwmon interface if no sensors present */ |
| 186 | rc = (adapter->hw.mac.ops.init_thermal_sensor_thresh(&adapter->hw)); |
Guenter Roeck | e3670b8 | 2013-11-26 07:15:23 +0000 | [diff] [blame] | 187 | if (rc) |
| 188 | goto exit; |
Carolyn Wyborny | e428893 | 2012-12-07 03:01:42 +0000 | [diff] [blame] | 189 | |
Guenter Roeck | e3670b8 | 2013-11-26 07:15:23 +0000 | [diff] [blame] | 190 | igb_hwmon = devm_kzalloc(&adapter->pdev->dev, sizeof(*igb_hwmon), |
| 191 | GFP_KERNEL); |
| 192 | if (!igb_hwmon) { |
| 193 | rc = -ENOMEM; |
Carolyn Wyborny | 603e86f | 2013-02-20 07:40:55 +0000 | [diff] [blame] | 194 | goto exit; |
| 195 | } |
Guenter Roeck | e3670b8 | 2013-11-26 07:15:23 +0000 | [diff] [blame] | 196 | adapter->igb_hwmon_buff = igb_hwmon; |
Carolyn Wyborny | e428893 | 2012-12-07 03:01:42 +0000 | [diff] [blame] | 197 | |
| 198 | for (i = 0; i < E1000_MAX_SENSORS; i++) { |
| 199 | |
| 200 | /* Only create hwmon sysfs entries for sensors that have |
| 201 | * meaningful data. |
| 202 | */ |
| 203 | if (adapter->hw.mac.thermal_sensor_data.sensor[i].location == 0) |
| 204 | continue; |
| 205 | |
| 206 | /* Bail if any hwmon attr struct fails to initialize */ |
| 207 | rc = igb_add_hwmon_attr(adapter, i, IGB_HWMON_TYPE_CAUTION); |
Carolyn Wyborny | e428893 | 2012-12-07 03:01:42 +0000 | [diff] [blame] | 208 | if (rc) |
Guenter Roeck | e3670b8 | 2013-11-26 07:15:23 +0000 | [diff] [blame] | 209 | goto exit; |
| 210 | rc = igb_add_hwmon_attr(adapter, i, IGB_HWMON_TYPE_LOC); |
| 211 | if (rc) |
| 212 | goto exit; |
| 213 | rc = igb_add_hwmon_attr(adapter, i, IGB_HWMON_TYPE_TEMP); |
| 214 | if (rc) |
| 215 | goto exit; |
| 216 | rc = igb_add_hwmon_attr(adapter, i, IGB_HWMON_TYPE_MAX); |
| 217 | if (rc) |
| 218 | goto exit; |
| 219 | } |
| 220 | |
| 221 | /* init i2c_client */ |
| 222 | client = i2c_new_device(&adapter->i2c_adap, &i350_sensor_info); |
| 223 | if (client == NULL) { |
| 224 | dev_info(&adapter->pdev->dev, |
| 225 | "Failed to create new i2c device.\n"); |
| 226 | rc = -ENODEV; |
| 227 | goto exit; |
| 228 | } |
| 229 | adapter->i2c_client = client; |
| 230 | |
| 231 | igb_hwmon->groups[0] = &igb_hwmon->group; |
| 232 | igb_hwmon->group.attrs = igb_hwmon->attrs; |
| 233 | |
| 234 | hwmon_dev = devm_hwmon_device_register_with_groups(&adapter->pdev->dev, |
| 235 | client->name, |
| 236 | igb_hwmon, |
| 237 | igb_hwmon->groups); |
| 238 | if (IS_ERR(hwmon_dev)) { |
| 239 | rc = PTR_ERR(hwmon_dev); |
| 240 | goto err; |
Carolyn Wyborny | e428893 | 2012-12-07 03:01:42 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | goto exit; |
| 244 | |
| 245 | err: |
| 246 | igb_sysfs_del_adapter(adapter); |
| 247 | exit: |
| 248 | return rc; |
| 249 | } |
| 250 | #endif |