ixgbe_sysfs.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 1999 - 2018 Intel Corporation. */
  3. #include "ixgbe.h"
  4. #include "ixgbe_common.h"
  5. #include "ixgbe_type.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. /* hwmon callback functions */
  14. static ssize_t ixgbe_hwmon_show_location(struct device *dev,
  15. struct device_attribute *attr,
  16. char *buf)
  17. {
  18. struct hwmon_attr *ixgbe_attr = container_of(attr, struct hwmon_attr,
  19. dev_attr);
  20. return sprintf(buf, "loc%u\n",
  21. ixgbe_attr->sensor->location);
  22. }
  23. static ssize_t ixgbe_hwmon_show_temp(struct device *dev,
  24. struct device_attribute *attr,
  25. char *buf)
  26. {
  27. struct hwmon_attr *ixgbe_attr = container_of(attr, struct hwmon_attr,
  28. dev_attr);
  29. unsigned int value;
  30. /* reset the temp field */
  31. ixgbe_attr->hw->mac.ops.get_thermal_sensor_data(ixgbe_attr->hw);
  32. value = ixgbe_attr->sensor->temp;
  33. /* display millidegree */
  34. value *= 1000;
  35. return sprintf(buf, "%u\n", value);
  36. }
  37. static ssize_t ixgbe_hwmon_show_cautionthresh(struct device *dev,
  38. struct device_attribute *attr,
  39. char *buf)
  40. {
  41. struct hwmon_attr *ixgbe_attr = container_of(attr, struct hwmon_attr,
  42. dev_attr);
  43. unsigned int value = ixgbe_attr->sensor->caution_thresh;
  44. /* display millidegree */
  45. value *= 1000;
  46. return sprintf(buf, "%u\n", value);
  47. }
  48. static ssize_t ixgbe_hwmon_show_maxopthresh(struct device *dev,
  49. struct device_attribute *attr,
  50. char *buf)
  51. {
  52. struct hwmon_attr *ixgbe_attr = container_of(attr, struct hwmon_attr,
  53. dev_attr);
  54. unsigned int value = ixgbe_attr->sensor->max_op_thresh;
  55. /* display millidegree */
  56. value *= 1000;
  57. return sprintf(buf, "%u\n", value);
  58. }
  59. /**
  60. * ixgbe_add_hwmon_attr - Create hwmon attr table for a hwmon sysfs file.
  61. * @adapter: pointer to the adapter structure
  62. * @offset: offset in the eeprom sensor data table
  63. * @type: type of sensor data to display
  64. *
  65. * For each file we want in hwmon's sysfs interface we need a device_attribute
  66. * This is included in our hwmon_attr struct that contains the references to
  67. * the data structures we need to get the data to display.
  68. */
  69. static int ixgbe_add_hwmon_attr(struct ixgbe_adapter *adapter,
  70. unsigned int offset, int type) {
  71. int rc;
  72. unsigned int n_attr;
  73. struct hwmon_attr *ixgbe_attr;
  74. n_attr = adapter->ixgbe_hwmon_buff->n_hwmon;
  75. ixgbe_attr = &adapter->ixgbe_hwmon_buff->hwmon_list[n_attr];
  76. switch (type) {
  77. case IXGBE_HWMON_TYPE_LOC:
  78. ixgbe_attr->dev_attr.show = ixgbe_hwmon_show_location;
  79. snprintf(ixgbe_attr->name, sizeof(ixgbe_attr->name),
  80. "temp%u_label", offset + 1);
  81. break;
  82. case IXGBE_HWMON_TYPE_TEMP:
  83. ixgbe_attr->dev_attr.show = ixgbe_hwmon_show_temp;
  84. snprintf(ixgbe_attr->name, sizeof(ixgbe_attr->name),
  85. "temp%u_input", offset + 1);
  86. break;
  87. case IXGBE_HWMON_TYPE_CAUTION:
  88. ixgbe_attr->dev_attr.show = ixgbe_hwmon_show_cautionthresh;
  89. snprintf(ixgbe_attr->name, sizeof(ixgbe_attr->name),
  90. "temp%u_max", offset + 1);
  91. break;
  92. case IXGBE_HWMON_TYPE_MAX:
  93. ixgbe_attr->dev_attr.show = ixgbe_hwmon_show_maxopthresh;
  94. snprintf(ixgbe_attr->name, sizeof(ixgbe_attr->name),
  95. "temp%u_crit", offset + 1);
  96. break;
  97. default:
  98. rc = -EPERM;
  99. return rc;
  100. }
  101. /* These always the same regardless of type */
  102. ixgbe_attr->sensor =
  103. &adapter->hw.mac.thermal_sensor_data.sensor[offset];
  104. ixgbe_attr->hw = &adapter->hw;
  105. ixgbe_attr->dev_attr.store = NULL;
  106. ixgbe_attr->dev_attr.attr.mode = 0444;
  107. ixgbe_attr->dev_attr.attr.name = ixgbe_attr->name;
  108. sysfs_attr_init(&ixgbe_attr->dev_attr.attr);
  109. adapter->ixgbe_hwmon_buff->attrs[n_attr] = &ixgbe_attr->dev_attr.attr;
  110. ++adapter->ixgbe_hwmon_buff->n_hwmon;
  111. return 0;
  112. }
  113. static void ixgbe_sysfs_del_adapter(struct ixgbe_adapter *adapter)
  114. {
  115. }
  116. /* called from ixgbe_main.c */
  117. void ixgbe_sysfs_exit(struct ixgbe_adapter *adapter)
  118. {
  119. ixgbe_sysfs_del_adapter(adapter);
  120. }
  121. /* called from ixgbe_main.c */
  122. int ixgbe_sysfs_init(struct ixgbe_adapter *adapter)
  123. {
  124. struct hwmon_buff *ixgbe_hwmon;
  125. struct device *hwmon_dev;
  126. unsigned int i;
  127. int rc = 0;
  128. /* If this method isn't defined we don't support thermals */
  129. if (adapter->hw.mac.ops.init_thermal_sensor_thresh == NULL) {
  130. goto exit;
  131. }
  132. /* Don't create thermal hwmon interface if no sensors present */
  133. if (adapter->hw.mac.ops.init_thermal_sensor_thresh(&adapter->hw))
  134. goto exit;
  135. ixgbe_hwmon = devm_kzalloc(&adapter->pdev->dev, sizeof(*ixgbe_hwmon),
  136. GFP_KERNEL);
  137. if (ixgbe_hwmon == NULL) {
  138. rc = -ENOMEM;
  139. goto exit;
  140. }
  141. adapter->ixgbe_hwmon_buff = ixgbe_hwmon;
  142. for (i = 0; i < IXGBE_MAX_SENSORS; i++) {
  143. /*
  144. * Only create hwmon sysfs entries for sensors that have
  145. * meaningful data for.
  146. */
  147. if (adapter->hw.mac.thermal_sensor_data.sensor[i].location == 0)
  148. continue;
  149. /* Bail if any hwmon attr struct fails to initialize */
  150. rc = ixgbe_add_hwmon_attr(adapter, i, IXGBE_HWMON_TYPE_CAUTION);
  151. if (rc)
  152. goto exit;
  153. rc = ixgbe_add_hwmon_attr(adapter, i, IXGBE_HWMON_TYPE_LOC);
  154. if (rc)
  155. goto exit;
  156. rc = ixgbe_add_hwmon_attr(adapter, i, IXGBE_HWMON_TYPE_TEMP);
  157. if (rc)
  158. goto exit;
  159. rc = ixgbe_add_hwmon_attr(adapter, i, IXGBE_HWMON_TYPE_MAX);
  160. if (rc)
  161. goto exit;
  162. }
  163. ixgbe_hwmon->groups[0] = &ixgbe_hwmon->group;
  164. ixgbe_hwmon->group.attrs = ixgbe_hwmon->attrs;
  165. hwmon_dev = devm_hwmon_device_register_with_groups(&adapter->pdev->dev,
  166. "ixgbe",
  167. ixgbe_hwmon,
  168. ixgbe_hwmon->groups);
  169. if (IS_ERR(hwmon_dev))
  170. rc = PTR_ERR(hwmon_dev);
  171. exit:
  172. return rc;
  173. }