ixgbe_sysfs.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  18. Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  19. *******************************************************************************/
  20. #include "ixgbe.h"
  21. #include "ixgbe_common.h"
  22. #include "ixgbe_type.h"
  23. #include <linux/module.h>
  24. #include <linux/types.h>
  25. #include <linux/sysfs.h>
  26. #include <linux/kobject.h>
  27. #include <linux/device.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/hwmon.h>
  30. /* hwmon callback functions */
  31. static ssize_t ixgbe_hwmon_show_location(struct device *dev,
  32. struct device_attribute *attr,
  33. char *buf)
  34. {
  35. struct hwmon_attr *ixgbe_attr = container_of(attr, struct hwmon_attr,
  36. dev_attr);
  37. return sprintf(buf, "loc%u\n",
  38. ixgbe_attr->sensor->location);
  39. }
  40. static ssize_t ixgbe_hwmon_show_temp(struct device *dev,
  41. struct device_attribute *attr,
  42. char *buf)
  43. {
  44. struct hwmon_attr *ixgbe_attr = container_of(attr, struct hwmon_attr,
  45. dev_attr);
  46. unsigned int value;
  47. /* reset the temp field */
  48. ixgbe_attr->hw->mac.ops.get_thermal_sensor_data(ixgbe_attr->hw);
  49. value = ixgbe_attr->sensor->temp;
  50. /* display millidegree */
  51. value *= 1000;
  52. return sprintf(buf, "%u\n", value);
  53. }
  54. static ssize_t ixgbe_hwmon_show_cautionthresh(struct device *dev,
  55. struct device_attribute *attr,
  56. char *buf)
  57. {
  58. struct hwmon_attr *ixgbe_attr = container_of(attr, struct hwmon_attr,
  59. dev_attr);
  60. unsigned int value = ixgbe_attr->sensor->caution_thresh;
  61. /* display millidegree */
  62. value *= 1000;
  63. return sprintf(buf, "%u\n", value);
  64. }
  65. static ssize_t ixgbe_hwmon_show_maxopthresh(struct device *dev,
  66. struct device_attribute *attr,
  67. char *buf)
  68. {
  69. struct hwmon_attr *ixgbe_attr = container_of(attr, struct hwmon_attr,
  70. dev_attr);
  71. unsigned int value = ixgbe_attr->sensor->max_op_thresh;
  72. /* display millidegree */
  73. value *= 1000;
  74. return sprintf(buf, "%u\n", value);
  75. }
  76. /**
  77. * ixgbe_add_hwmon_attr - Create hwmon attr table for a hwmon sysfs file.
  78. * @adapter: pointer to the adapter structure
  79. * @offset: offset in the eeprom sensor data table
  80. * @type: type of sensor data to display
  81. *
  82. * For each file we want in hwmon's sysfs interface we need a device_attribute
  83. * This is included in our hwmon_attr struct that contains the references to
  84. * the data structures we need to get the data to display.
  85. */
  86. static int ixgbe_add_hwmon_attr(struct ixgbe_adapter *adapter,
  87. unsigned int offset, int type) {
  88. int rc;
  89. unsigned int n_attr;
  90. struct hwmon_attr *ixgbe_attr;
  91. n_attr = adapter->ixgbe_hwmon_buff.n_hwmon;
  92. ixgbe_attr = &adapter->ixgbe_hwmon_buff.hwmon_list[n_attr];
  93. switch (type) {
  94. case IXGBE_HWMON_TYPE_LOC:
  95. ixgbe_attr->dev_attr.show = ixgbe_hwmon_show_location;
  96. snprintf(ixgbe_attr->name, sizeof(ixgbe_attr->name),
  97. "temp%u_label", offset);
  98. break;
  99. case IXGBE_HWMON_TYPE_TEMP:
  100. ixgbe_attr->dev_attr.show = ixgbe_hwmon_show_temp;
  101. snprintf(ixgbe_attr->name, sizeof(ixgbe_attr->name),
  102. "temp%u_input", offset);
  103. break;
  104. case IXGBE_HWMON_TYPE_CAUTION:
  105. ixgbe_attr->dev_attr.show = ixgbe_hwmon_show_cautionthresh;
  106. snprintf(ixgbe_attr->name, sizeof(ixgbe_attr->name),
  107. "temp%u_max", offset);
  108. break;
  109. case IXGBE_HWMON_TYPE_MAX:
  110. ixgbe_attr->dev_attr.show = ixgbe_hwmon_show_maxopthresh;
  111. snprintf(ixgbe_attr->name, sizeof(ixgbe_attr->name),
  112. "temp%u_crit", offset);
  113. break;
  114. default:
  115. rc = -EPERM;
  116. return rc;
  117. }
  118. /* These always the same regardless of type */
  119. ixgbe_attr->sensor =
  120. &adapter->hw.mac.thermal_sensor_data.sensor[offset];
  121. ixgbe_attr->hw = &adapter->hw;
  122. ixgbe_attr->dev_attr.store = NULL;
  123. ixgbe_attr->dev_attr.attr.mode = S_IRUGO;
  124. ixgbe_attr->dev_attr.attr.name = ixgbe_attr->name;
  125. rc = device_create_file(&adapter->pdev->dev,
  126. &ixgbe_attr->dev_attr);
  127. if (rc == 0)
  128. ++adapter->ixgbe_hwmon_buff.n_hwmon;
  129. return rc;
  130. }
  131. static void ixgbe_sysfs_del_adapter(struct ixgbe_adapter *adapter)
  132. {
  133. int i;
  134. if (adapter == NULL)
  135. return;
  136. for (i = 0; i < adapter->ixgbe_hwmon_buff.n_hwmon; i++) {
  137. device_remove_file(&adapter->pdev->dev,
  138. &adapter->ixgbe_hwmon_buff.hwmon_list[i].dev_attr);
  139. }
  140. kfree(adapter->ixgbe_hwmon_buff.hwmon_list);
  141. if (adapter->ixgbe_hwmon_buff.device)
  142. hwmon_device_unregister(adapter->ixgbe_hwmon_buff.device);
  143. }
  144. /* called from ixgbe_main.c */
  145. void ixgbe_sysfs_exit(struct ixgbe_adapter *adapter)
  146. {
  147. ixgbe_sysfs_del_adapter(adapter);
  148. }
  149. /* called from ixgbe_main.c */
  150. int ixgbe_sysfs_init(struct ixgbe_adapter *adapter)
  151. {
  152. struct hwmon_buff *ixgbe_hwmon = &adapter->ixgbe_hwmon_buff;
  153. unsigned int i;
  154. int n_attrs;
  155. int rc = 0;
  156. /* If this method isn't defined we don't support thermals */
  157. if (adapter->hw.mac.ops.init_thermal_sensor_thresh == NULL) {
  158. goto exit;
  159. }
  160. /* Don't create thermal hwmon interface if no sensors present */
  161. if (adapter->hw.mac.ops.init_thermal_sensor_thresh(&adapter->hw))
  162. goto exit;
  163. /*
  164. * Allocation space for max attributs
  165. * max num sensors * values (loc, temp, max, caution)
  166. */
  167. n_attrs = IXGBE_MAX_SENSORS * 4;
  168. ixgbe_hwmon->hwmon_list = kcalloc(n_attrs, sizeof(struct hwmon_attr),
  169. GFP_KERNEL);
  170. if (!ixgbe_hwmon->hwmon_list) {
  171. rc = -ENOMEM;
  172. goto err;
  173. }
  174. ixgbe_hwmon->device = hwmon_device_register(&adapter->pdev->dev);
  175. if (IS_ERR(ixgbe_hwmon->device)) {
  176. rc = PTR_ERR(ixgbe_hwmon->device);
  177. goto err;
  178. }
  179. for (i = 0; i < IXGBE_MAX_SENSORS; i++) {
  180. /*
  181. * Only create hwmon sysfs entries for sensors that have
  182. * meaningful data for.
  183. */
  184. if (adapter->hw.mac.thermal_sensor_data.sensor[i].location == 0)
  185. continue;
  186. /* Bail if any hwmon attr struct fails to initialize */
  187. rc = ixgbe_add_hwmon_attr(adapter, i, IXGBE_HWMON_TYPE_CAUTION);
  188. rc |= ixgbe_add_hwmon_attr(adapter, i, IXGBE_HWMON_TYPE_LOC);
  189. rc |= ixgbe_add_hwmon_attr(adapter, i, IXGBE_HWMON_TYPE_TEMP);
  190. rc |= ixgbe_add_hwmon_attr(adapter, i, IXGBE_HWMON_TYPE_MAX);
  191. if (rc)
  192. goto err;
  193. }
  194. goto exit;
  195. err:
  196. ixgbe_sysfs_del_adapter(adapter);
  197. exit:
  198. return rc;
  199. }