igb_hwmon.c 6.6 KB

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