ixgbe_sysfs.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*******************************************************************************
  2. Intel 10 Gigabit PCI Express Linux driver
  3. Copyright(c) 1999 - 2013 Intel Corporation.
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms and conditions of the GNU General Public License,
  6. version 2, as published by the Free Software Foundation.
  7. This program is distributed in the hope it will be useful, but WITHOUT
  8. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  9. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  10. more details.
  11. You should have received a copy of the GNU General Public License along with
  12. this program; if not, write to the Free Software Foundation, Inc.,
  13. 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  14. The full GNU General Public License is included in this distribution in
  15. the file called "COPYING".
  16. Contact Information:
  17. Linux NICS <linux.nics@intel.com>
  18. e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  19. Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  20. *******************************************************************************/
  21. #include "ixgbe.h"
  22. #include "ixgbe_common.h"
  23. #include "ixgbe_type.h"
  24. #include <linux/module.h>
  25. #include <linux/types.h>
  26. #include <linux/sysfs.h>
  27. #include <linux/kobject.h>
  28. #include <linux/device.h>
  29. #include <linux/netdevice.h>
  30. #include <linux/hwmon.h>
  31. /* hwmon callback functions */
  32. static ssize_t ixgbe_hwmon_show_location(struct device *dev,
  33. struct device_attribute *attr,
  34. char *buf)
  35. {
  36. struct hwmon_attr *ixgbe_attr = container_of(attr, struct hwmon_attr,
  37. dev_attr);
  38. return sprintf(buf, "loc%u\n",
  39. ixgbe_attr->sensor->location);
  40. }
  41. static ssize_t ixgbe_hwmon_show_temp(struct device *dev,
  42. struct device_attribute *attr,
  43. char *buf)
  44. {
  45. struct hwmon_attr *ixgbe_attr = container_of(attr, struct hwmon_attr,
  46. dev_attr);
  47. unsigned int value;
  48. /* reset the temp field */
  49. ixgbe_attr->hw->mac.ops.get_thermal_sensor_data(ixgbe_attr->hw);
  50. value = ixgbe_attr->sensor->temp;
  51. /* display millidegree */
  52. value *= 1000;
  53. return sprintf(buf, "%u\n", value);
  54. }
  55. static ssize_t ixgbe_hwmon_show_cautionthresh(struct device *dev,
  56. struct device_attribute *attr,
  57. char *buf)
  58. {
  59. struct hwmon_attr *ixgbe_attr = container_of(attr, struct hwmon_attr,
  60. dev_attr);
  61. unsigned int value = ixgbe_attr->sensor->caution_thresh;
  62. /* display millidegree */
  63. value *= 1000;
  64. return sprintf(buf, "%u\n", value);
  65. }
  66. static ssize_t ixgbe_hwmon_show_maxopthresh(struct device *dev,
  67. struct device_attribute *attr,
  68. char *buf)
  69. {
  70. struct hwmon_attr *ixgbe_attr = container_of(attr, struct hwmon_attr,
  71. dev_attr);
  72. unsigned int value = ixgbe_attr->sensor->max_op_thresh;
  73. /* display millidegree */
  74. value *= 1000;
  75. return sprintf(buf, "%u\n", value);
  76. }
  77. /**
  78. * ixgbe_add_hwmon_attr - Create hwmon attr table for a hwmon sysfs file.
  79. * @adapter: pointer to the adapter structure
  80. * @offset: offset in the eeprom sensor data table
  81. * @type: type of sensor data to display
  82. *
  83. * For each file we want in hwmon's sysfs interface we need a device_attribute
  84. * This is included in our hwmon_attr struct that contains the references to
  85. * the data structures we need to get the data to display.
  86. */
  87. static int ixgbe_add_hwmon_attr(struct ixgbe_adapter *adapter,
  88. unsigned int offset, int type) {
  89. int rc;
  90. unsigned int n_attr;
  91. struct hwmon_attr *ixgbe_attr;
  92. n_attr = adapter->ixgbe_hwmon_buff->n_hwmon;
  93. ixgbe_attr = &adapter->ixgbe_hwmon_buff->hwmon_list[n_attr];
  94. switch (type) {
  95. case IXGBE_HWMON_TYPE_LOC:
  96. ixgbe_attr->dev_attr.show = ixgbe_hwmon_show_location;
  97. snprintf(ixgbe_attr->name, sizeof(ixgbe_attr->name),
  98. "temp%u_label", offset + 1);
  99. break;
  100. case IXGBE_HWMON_TYPE_TEMP:
  101. ixgbe_attr->dev_attr.show = ixgbe_hwmon_show_temp;
  102. snprintf(ixgbe_attr->name, sizeof(ixgbe_attr->name),
  103. "temp%u_input", offset + 1);
  104. break;
  105. case IXGBE_HWMON_TYPE_CAUTION:
  106. ixgbe_attr->dev_attr.show = ixgbe_hwmon_show_cautionthresh;
  107. snprintf(ixgbe_attr->name, sizeof(ixgbe_attr->name),
  108. "temp%u_max", offset + 1);
  109. break;
  110. case IXGBE_HWMON_TYPE_MAX:
  111. ixgbe_attr->dev_attr.show = ixgbe_hwmon_show_maxopthresh;
  112. snprintf(ixgbe_attr->name, sizeof(ixgbe_attr->name),
  113. "temp%u_crit", offset + 1);
  114. break;
  115. default:
  116. rc = -EPERM;
  117. return rc;
  118. }
  119. /* These always the same regardless of type */
  120. ixgbe_attr->sensor =
  121. &adapter->hw.mac.thermal_sensor_data.sensor[offset];
  122. ixgbe_attr->hw = &adapter->hw;
  123. ixgbe_attr->dev_attr.store = NULL;
  124. ixgbe_attr->dev_attr.attr.mode = S_IRUGO;
  125. ixgbe_attr->dev_attr.attr.name = ixgbe_attr->name;
  126. sysfs_attr_init(&ixgbe_attr->dev_attr.attr);
  127. adapter->ixgbe_hwmon_buff->attrs[n_attr] = &ixgbe_attr->dev_attr.attr;
  128. ++adapter->ixgbe_hwmon_buff->n_hwmon;
  129. return 0;
  130. }
  131. static void ixgbe_sysfs_del_adapter(struct ixgbe_adapter *adapter)
  132. {
  133. }
  134. /* called from ixgbe_main.c */
  135. void ixgbe_sysfs_exit(struct ixgbe_adapter *adapter)
  136. {
  137. ixgbe_sysfs_del_adapter(adapter);
  138. }
  139. /* called from ixgbe_main.c */
  140. int ixgbe_sysfs_init(struct ixgbe_adapter *adapter)
  141. {
  142. struct hwmon_buff *ixgbe_hwmon;
  143. struct device *hwmon_dev;
  144. unsigned int i;
  145. int rc = 0;
  146. /* If this method isn't defined we don't support thermals */
  147. if (adapter->hw.mac.ops.init_thermal_sensor_thresh == NULL) {
  148. goto exit;
  149. }
  150. /* Don't create thermal hwmon interface if no sensors present */
  151. if (adapter->hw.mac.ops.init_thermal_sensor_thresh(&adapter->hw))
  152. goto exit;
  153. ixgbe_hwmon = devm_kzalloc(&adapter->pdev->dev, sizeof(*ixgbe_hwmon),
  154. GFP_KERNEL);
  155. if (ixgbe_hwmon == NULL) {
  156. rc = -ENOMEM;
  157. goto exit;
  158. }
  159. adapter->ixgbe_hwmon_buff = ixgbe_hwmon;
  160. for (i = 0; i < IXGBE_MAX_SENSORS; i++) {
  161. /*
  162. * Only create hwmon sysfs entries for sensors that have
  163. * meaningful data for.
  164. */
  165. if (adapter->hw.mac.thermal_sensor_data.sensor[i].location == 0)
  166. continue;
  167. /* Bail if any hwmon attr struct fails to initialize */
  168. rc = ixgbe_add_hwmon_attr(adapter, i, IXGBE_HWMON_TYPE_CAUTION);
  169. if (rc)
  170. goto exit;
  171. rc = ixgbe_add_hwmon_attr(adapter, i, IXGBE_HWMON_TYPE_LOC);
  172. if (rc)
  173. goto exit;
  174. rc = ixgbe_add_hwmon_attr(adapter, i, IXGBE_HWMON_TYPE_TEMP);
  175. if (rc)
  176. goto exit;
  177. rc = ixgbe_add_hwmon_attr(adapter, i, IXGBE_HWMON_TYPE_MAX);
  178. if (rc)
  179. goto exit;
  180. }
  181. ixgbe_hwmon->groups[0] = &ixgbe_hwmon->group;
  182. ixgbe_hwmon->group.attrs = ixgbe_hwmon->attrs;
  183. hwmon_dev = devm_hwmon_device_register_with_groups(&adapter->pdev->dev,
  184. "ixgbe",
  185. ixgbe_hwmon,
  186. ixgbe_hwmon->groups);
  187. if (IS_ERR(hwmon_dev))
  188. rc = PTR_ERR(hwmon_dev);
  189. exit:
  190. return rc;
  191. }