igb_hwmon.c 5.8 KB

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