igb_hwmon.c 6.7 KB

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