1ae06c70bSJeff Kirsher // SPDX-License-Identifier: GPL-2.0 251dce24bSJeff Kirsher /* Copyright(c) 2007 - 2018 Intel Corporation. */ 3e428893bSCarolyn Wyborny 4e428893bSCarolyn Wyborny #include "igb.h" 5e428893bSCarolyn Wyborny #include "e1000_82575.h" 6e428893bSCarolyn Wyborny #include "e1000_hw.h" 7e428893bSCarolyn Wyborny 8e428893bSCarolyn Wyborny #include <linux/module.h> 9e428893bSCarolyn Wyborny #include <linux/types.h> 10e428893bSCarolyn Wyborny #include <linux/sysfs.h> 11e428893bSCarolyn Wyborny #include <linux/kobject.h> 12e428893bSCarolyn Wyborny #include <linux/device.h> 13e428893bSCarolyn Wyborny #include <linux/netdevice.h> 14e428893bSCarolyn Wyborny #include <linux/hwmon.h> 15e428893bSCarolyn Wyborny #include <linux/pci.h> 16e428893bSCarolyn Wyborny 17e428893bSCarolyn Wyborny #ifdef CONFIG_IGB_HWMON 1805ec29e8SStephen Hemminger static struct i2c_board_info i350_sensor_info = { 19603e86faSCarolyn Wyborny I2C_BOARD_INFO("i350bb", (0Xf8 >> 1)), 20603e86faSCarolyn Wyborny }; 21603e86faSCarolyn Wyborny 22e428893bSCarolyn Wyborny /* hwmon callback functions */ 23e428893bSCarolyn Wyborny static ssize_t igb_hwmon_show_location(struct device *dev, 24e428893bSCarolyn Wyborny struct device_attribute *attr, 25e428893bSCarolyn Wyborny char *buf) 26e428893bSCarolyn Wyborny { 27e428893bSCarolyn Wyborny struct hwmon_attr *igb_attr = container_of(attr, struct hwmon_attr, 28e428893bSCarolyn Wyborny dev_attr); 29e428893bSCarolyn Wyborny return sprintf(buf, "loc%u\n", 30e428893bSCarolyn Wyborny igb_attr->sensor->location); 31e428893bSCarolyn Wyborny } 32e428893bSCarolyn Wyborny 33e428893bSCarolyn Wyborny static ssize_t igb_hwmon_show_temp(struct device *dev, 34e428893bSCarolyn Wyborny struct device_attribute *attr, 35e428893bSCarolyn Wyborny char *buf) 36e428893bSCarolyn Wyborny { 37e428893bSCarolyn Wyborny struct hwmon_attr *igb_attr = container_of(attr, struct hwmon_attr, 38e428893bSCarolyn Wyborny dev_attr); 39e428893bSCarolyn Wyborny unsigned int value; 40e428893bSCarolyn Wyborny 41e428893bSCarolyn Wyborny /* reset the temp field */ 42e428893bSCarolyn Wyborny igb_attr->hw->mac.ops.get_thermal_sensor_data(igb_attr->hw); 43e428893bSCarolyn Wyborny 44e428893bSCarolyn Wyborny value = igb_attr->sensor->temp; 45e428893bSCarolyn Wyborny 46e428893bSCarolyn Wyborny /* display millidegree */ 47e428893bSCarolyn Wyborny value *= 1000; 48e428893bSCarolyn Wyborny 49e428893bSCarolyn Wyborny return sprintf(buf, "%u\n", value); 50e428893bSCarolyn Wyborny } 51e428893bSCarolyn Wyborny 52e428893bSCarolyn Wyborny static ssize_t igb_hwmon_show_cautionthresh(struct device *dev, 53e428893bSCarolyn Wyborny struct device_attribute *attr, 54e428893bSCarolyn Wyborny char *buf) 55e428893bSCarolyn Wyborny { 56e428893bSCarolyn Wyborny struct hwmon_attr *igb_attr = container_of(attr, struct hwmon_attr, 57e428893bSCarolyn Wyborny dev_attr); 58e428893bSCarolyn Wyborny unsigned int value = igb_attr->sensor->caution_thresh; 59e428893bSCarolyn Wyborny 60e428893bSCarolyn Wyborny /* display millidegree */ 61e428893bSCarolyn Wyborny value *= 1000; 62e428893bSCarolyn Wyborny 63e428893bSCarolyn Wyborny return sprintf(buf, "%u\n", value); 64e428893bSCarolyn Wyborny } 65e428893bSCarolyn Wyborny 66e428893bSCarolyn Wyborny static ssize_t igb_hwmon_show_maxopthresh(struct device *dev, 67e428893bSCarolyn Wyborny struct device_attribute *attr, 68e428893bSCarolyn Wyborny char *buf) 69e428893bSCarolyn Wyborny { 70e428893bSCarolyn Wyborny struct hwmon_attr *igb_attr = container_of(attr, struct hwmon_attr, 71e428893bSCarolyn Wyborny dev_attr); 72e428893bSCarolyn Wyborny unsigned int value = igb_attr->sensor->max_op_thresh; 73e428893bSCarolyn Wyborny 74e428893bSCarolyn Wyborny /* display millidegree */ 75e428893bSCarolyn Wyborny value *= 1000; 76e428893bSCarolyn Wyborny 77e428893bSCarolyn Wyborny return sprintf(buf, "%u\n", value); 78e428893bSCarolyn Wyborny } 79e428893bSCarolyn Wyborny 80e428893bSCarolyn Wyborny /* igb_add_hwmon_attr - Create hwmon attr table for a hwmon sysfs file. 81e428893bSCarolyn Wyborny * @ adapter: pointer to the adapter structure 82e428893bSCarolyn Wyborny * @ offset: offset in the eeprom sensor data table 83e428893bSCarolyn Wyborny * @ type: type of sensor data to display 84e428893bSCarolyn Wyborny * 85e428893bSCarolyn Wyborny * For each file we want in hwmon's sysfs interface we need a device_attribute 86e428893bSCarolyn Wyborny * This is included in our hwmon_attr struct that contains the references to 87e428893bSCarolyn Wyborny * the data structures we need to get the data to display. 88e428893bSCarolyn Wyborny */ 89e428893bSCarolyn Wyborny static int igb_add_hwmon_attr(struct igb_adapter *adapter, 90b980ac18SJeff Kirsher unsigned int offset, int type) 91b980ac18SJeff Kirsher { 92e428893bSCarolyn Wyborny int rc; 93e428893bSCarolyn Wyborny unsigned int n_attr; 94e428893bSCarolyn Wyborny struct hwmon_attr *igb_attr; 95e428893bSCarolyn Wyborny 96e3670b81SGuenter Roeck n_attr = adapter->igb_hwmon_buff->n_hwmon; 97e3670b81SGuenter Roeck igb_attr = &adapter->igb_hwmon_buff->hwmon_list[n_attr]; 98e428893bSCarolyn Wyborny 99e428893bSCarolyn Wyborny switch (type) { 100e428893bSCarolyn Wyborny case IGB_HWMON_TYPE_LOC: 101e428893bSCarolyn Wyborny igb_attr->dev_attr.show = igb_hwmon_show_location; 102e428893bSCarolyn Wyborny snprintf(igb_attr->name, sizeof(igb_attr->name), 103e6e25bbaSGuenter Roeck "temp%u_label", offset + 1); 104e428893bSCarolyn Wyborny break; 105e428893bSCarolyn Wyborny case IGB_HWMON_TYPE_TEMP: 106e428893bSCarolyn Wyborny igb_attr->dev_attr.show = igb_hwmon_show_temp; 107e428893bSCarolyn Wyborny snprintf(igb_attr->name, sizeof(igb_attr->name), 108e6e25bbaSGuenter Roeck "temp%u_input", offset + 1); 109e428893bSCarolyn Wyborny break; 110e428893bSCarolyn Wyborny case IGB_HWMON_TYPE_CAUTION: 111e428893bSCarolyn Wyborny igb_attr->dev_attr.show = igb_hwmon_show_cautionthresh; 112e428893bSCarolyn Wyborny snprintf(igb_attr->name, sizeof(igb_attr->name), 113e6e25bbaSGuenter Roeck "temp%u_max", offset + 1); 114e428893bSCarolyn Wyborny break; 115e428893bSCarolyn Wyborny case IGB_HWMON_TYPE_MAX: 116e428893bSCarolyn Wyborny igb_attr->dev_attr.show = igb_hwmon_show_maxopthresh; 117e428893bSCarolyn Wyborny snprintf(igb_attr->name, sizeof(igb_attr->name), 118e6e25bbaSGuenter Roeck "temp%u_crit", offset + 1); 119e428893bSCarolyn Wyborny break; 120e428893bSCarolyn Wyborny default: 121e428893bSCarolyn Wyborny rc = -EPERM; 122e428893bSCarolyn Wyborny return rc; 123e428893bSCarolyn Wyborny } 124e428893bSCarolyn Wyborny 125e428893bSCarolyn Wyborny /* These always the same regardless of type */ 126e428893bSCarolyn Wyborny igb_attr->sensor = 127e428893bSCarolyn Wyborny &adapter->hw.mac.thermal_sensor_data.sensor[offset]; 128e428893bSCarolyn Wyborny igb_attr->hw = &adapter->hw; 129e428893bSCarolyn Wyborny igb_attr->dev_attr.store = NULL; 130d3757ba4SJoe Perches igb_attr->dev_attr.attr.mode = 0444; 131e428893bSCarolyn Wyborny igb_attr->dev_attr.attr.name = igb_attr->name; 132e428893bSCarolyn Wyborny sysfs_attr_init(&igb_attr->dev_attr.attr); 133e428893bSCarolyn Wyborny 134e3670b81SGuenter Roeck adapter->igb_hwmon_buff->attrs[n_attr] = &igb_attr->dev_attr.attr; 135e3670b81SGuenter Roeck 136e3670b81SGuenter Roeck ++adapter->igb_hwmon_buff->n_hwmon; 137e3670b81SGuenter Roeck 138e3670b81SGuenter Roeck return 0; 139e428893bSCarolyn Wyborny } 140e428893bSCarolyn Wyborny 141e428893bSCarolyn Wyborny static void igb_sysfs_del_adapter(struct igb_adapter *adapter) 142e428893bSCarolyn Wyborny { 143e428893bSCarolyn Wyborny } 144e428893bSCarolyn Wyborny 145e428893bSCarolyn Wyborny /* called from igb_main.c */ 146e428893bSCarolyn Wyborny void igb_sysfs_exit(struct igb_adapter *adapter) 147e428893bSCarolyn Wyborny { 148e428893bSCarolyn Wyborny igb_sysfs_del_adapter(adapter); 149e428893bSCarolyn Wyborny } 150e428893bSCarolyn Wyborny 151e428893bSCarolyn Wyborny /* called from igb_main.c */ 152e428893bSCarolyn Wyborny int igb_sysfs_init(struct igb_adapter *adapter) 153e428893bSCarolyn Wyborny { 154e3670b81SGuenter Roeck struct hwmon_buff *igb_hwmon; 155e3670b81SGuenter Roeck struct i2c_client *client; 156e3670b81SGuenter Roeck struct device *hwmon_dev; 157e428893bSCarolyn Wyborny unsigned int i; 158e428893bSCarolyn Wyborny int rc = 0; 159e428893bSCarolyn Wyborny 160e428893bSCarolyn Wyborny /* If this method isn't defined we don't support thermals */ 161e428893bSCarolyn Wyborny if (adapter->hw.mac.ops.init_thermal_sensor_thresh == NULL) 162e428893bSCarolyn Wyborny goto exit; 163e428893bSCarolyn Wyborny 164e428893bSCarolyn Wyborny /* Don't create thermal hwmon interface if no sensors present */ 165e428893bSCarolyn Wyborny rc = (adapter->hw.mac.ops.init_thermal_sensor_thresh(&adapter->hw)); 166e428893bSCarolyn Wyborny if (rc) 167e428893bSCarolyn Wyborny goto exit; 168e428893bSCarolyn Wyborny 169e3670b81SGuenter Roeck igb_hwmon = devm_kzalloc(&adapter->pdev->dev, sizeof(*igb_hwmon), 170e3670b81SGuenter Roeck GFP_KERNEL); 171e3670b81SGuenter Roeck if (!igb_hwmon) { 172e3670b81SGuenter Roeck rc = -ENOMEM; 173603e86faSCarolyn Wyborny goto exit; 174603e86faSCarolyn Wyborny } 175e3670b81SGuenter Roeck adapter->igb_hwmon_buff = igb_hwmon; 176e428893bSCarolyn Wyborny 177e428893bSCarolyn Wyborny for (i = 0; i < E1000_MAX_SENSORS; i++) { 178e428893bSCarolyn Wyborny 179e428893bSCarolyn Wyborny /* Only create hwmon sysfs entries for sensors that have 180e428893bSCarolyn Wyborny * meaningful data. 181e428893bSCarolyn Wyborny */ 182e428893bSCarolyn Wyborny if (adapter->hw.mac.thermal_sensor_data.sensor[i].location == 0) 183e428893bSCarolyn Wyborny continue; 184e428893bSCarolyn Wyborny 185e428893bSCarolyn Wyborny /* Bail if any hwmon attr struct fails to initialize */ 186e428893bSCarolyn Wyborny rc = igb_add_hwmon_attr(adapter, i, IGB_HWMON_TYPE_CAUTION); 187e428893bSCarolyn Wyborny if (rc) 188e3670b81SGuenter Roeck goto exit; 189e3670b81SGuenter Roeck rc = igb_add_hwmon_attr(adapter, i, IGB_HWMON_TYPE_LOC); 190e3670b81SGuenter Roeck if (rc) 191e3670b81SGuenter Roeck goto exit; 192e3670b81SGuenter Roeck rc = igb_add_hwmon_attr(adapter, i, IGB_HWMON_TYPE_TEMP); 193e3670b81SGuenter Roeck if (rc) 194e3670b81SGuenter Roeck goto exit; 195e3670b81SGuenter Roeck rc = igb_add_hwmon_attr(adapter, i, IGB_HWMON_TYPE_MAX); 196e3670b81SGuenter Roeck if (rc) 197e3670b81SGuenter Roeck goto exit; 198e3670b81SGuenter Roeck } 199e3670b81SGuenter Roeck 200e3670b81SGuenter Roeck /* init i2c_client */ 201*07eaf53aSWolfram Sang client = i2c_new_client_device(&adapter->i2c_adap, &i350_sensor_info); 202*07eaf53aSWolfram Sang if (IS_ERR(client)) { 203e3670b81SGuenter Roeck dev_info(&adapter->pdev->dev, 204e3670b81SGuenter Roeck "Failed to create new i2c device.\n"); 205*07eaf53aSWolfram Sang rc = PTR_ERR(client); 206e3670b81SGuenter Roeck goto exit; 207e3670b81SGuenter Roeck } 208e3670b81SGuenter Roeck adapter->i2c_client = client; 209e3670b81SGuenter Roeck 210e3670b81SGuenter Roeck igb_hwmon->groups[0] = &igb_hwmon->group; 211e3670b81SGuenter Roeck igb_hwmon->group.attrs = igb_hwmon->attrs; 212e3670b81SGuenter Roeck 213e3670b81SGuenter Roeck hwmon_dev = devm_hwmon_device_register_with_groups(&adapter->pdev->dev, 214e3670b81SGuenter Roeck client->name, 215e3670b81SGuenter Roeck igb_hwmon, 216e3670b81SGuenter Roeck igb_hwmon->groups); 217e3670b81SGuenter Roeck if (IS_ERR(hwmon_dev)) { 218e3670b81SGuenter Roeck rc = PTR_ERR(hwmon_dev); 219e428893bSCarolyn Wyborny goto err; 220e428893bSCarolyn Wyborny } 221e428893bSCarolyn Wyborny 222e428893bSCarolyn Wyborny goto exit; 223e428893bSCarolyn Wyborny 224e428893bSCarolyn Wyborny err: 225e428893bSCarolyn Wyborny igb_sysfs_del_adapter(adapter); 226e428893bSCarolyn Wyborny exit: 227e428893bSCarolyn Wyborny return rc; 228e428893bSCarolyn Wyborny } 229e428893bSCarolyn Wyborny #endif 230