mcdi_mon.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /****************************************************************************
  2. * Driver for Solarflare Solarstorm network controllers and boards
  3. * Copyright 2011 Solarflare Communications Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published
  7. * by the Free Software Foundation, incorporated herein by reference.
  8. */
  9. #include <linux/bitops.h>
  10. #include <linux/slab.h>
  11. #include <linux/hwmon.h>
  12. #include <linux/stat.h>
  13. #include "net_driver.h"
  14. #include "mcdi.h"
  15. #include "mcdi_pcol.h"
  16. #include "nic.h"
  17. enum efx_hwmon_type {
  18. EFX_HWMON_UNKNOWN,
  19. EFX_HWMON_TEMP, /* temperature */
  20. EFX_HWMON_COOL, /* cooling device, probably a heatsink */
  21. EFX_HWMON_IN /* input voltage */
  22. };
  23. static const struct {
  24. const char *label;
  25. enum efx_hwmon_type hwmon_type;
  26. int port;
  27. } efx_mcdi_sensor_type[MC_CMD_SENSOR_ENTRY_MAXNUM] = {
  28. #define SENSOR(name, label, hwmon_type, port) \
  29. [MC_CMD_SENSOR_##name] = { label, hwmon_type, port }
  30. SENSOR(CONTROLLER_TEMP, "Controller temp.", EFX_HWMON_TEMP, -1),
  31. SENSOR(PHY_COMMON_TEMP, "PHY temp.", EFX_HWMON_TEMP, -1),
  32. SENSOR(CONTROLLER_COOLING, "Controller cooling", EFX_HWMON_COOL, -1),
  33. SENSOR(PHY0_TEMP, "PHY temp.", EFX_HWMON_TEMP, 0),
  34. SENSOR(PHY0_COOLING, "PHY cooling", EFX_HWMON_COOL, 0),
  35. SENSOR(PHY1_TEMP, "PHY temp.", EFX_HWMON_TEMP, 1),
  36. SENSOR(PHY1_COOLING, "PHY cooling", EFX_HWMON_COOL, 1),
  37. SENSOR(IN_1V0, "1.0V supply", EFX_HWMON_IN, -1),
  38. SENSOR(IN_1V2, "1.2V supply", EFX_HWMON_IN, -1),
  39. SENSOR(IN_1V8, "1.8V supply", EFX_HWMON_IN, -1),
  40. SENSOR(IN_2V5, "2.5V supply", EFX_HWMON_IN, -1),
  41. SENSOR(IN_3V3, "3.3V supply", EFX_HWMON_IN, -1),
  42. SENSOR(IN_12V0, "12.0V supply", EFX_HWMON_IN, -1),
  43. SENSOR(IN_1V2A, "1.2V analogue supply", EFX_HWMON_IN, -1),
  44. SENSOR(IN_VREF, "ref. voltage", EFX_HWMON_IN, -1),
  45. #undef SENSOR
  46. };
  47. static const char *const sensor_status_names[] = {
  48. [MC_CMD_SENSOR_STATE_OK] = "OK",
  49. [MC_CMD_SENSOR_STATE_WARNING] = "Warning",
  50. [MC_CMD_SENSOR_STATE_FATAL] = "Fatal",
  51. [MC_CMD_SENSOR_STATE_BROKEN] = "Device failure",
  52. };
  53. void efx_mcdi_sensor_event(struct efx_nic *efx, efx_qword_t *ev)
  54. {
  55. unsigned int type, state, value;
  56. const char *name = NULL, *state_txt;
  57. type = EFX_QWORD_FIELD(*ev, MCDI_EVENT_SENSOREVT_MONITOR);
  58. state = EFX_QWORD_FIELD(*ev, MCDI_EVENT_SENSOREVT_STATE);
  59. value = EFX_QWORD_FIELD(*ev, MCDI_EVENT_SENSOREVT_VALUE);
  60. /* Deal gracefully with the board having more drivers than we
  61. * know about, but do not expect new sensor states. */
  62. if (type < ARRAY_SIZE(efx_mcdi_sensor_type))
  63. name = efx_mcdi_sensor_type[type].label;
  64. if (!name)
  65. name = "No sensor name available";
  66. EFX_BUG_ON_PARANOID(state >= ARRAY_SIZE(sensor_status_names));
  67. state_txt = sensor_status_names[state];
  68. netif_err(efx, hw, efx->net_dev,
  69. "Sensor %d (%s) reports condition '%s' for raw value %d\n",
  70. type, name, state_txt, value);
  71. }
  72. #ifdef CONFIG_SFC_MCDI_MON
  73. struct efx_mcdi_mon_attribute {
  74. struct device_attribute dev_attr;
  75. unsigned int index;
  76. unsigned int type;
  77. unsigned int limit_value;
  78. char name[12];
  79. };
  80. static int efx_mcdi_mon_update(struct efx_nic *efx)
  81. {
  82. struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx);
  83. MCDI_DECLARE_BUF(inbuf, MC_CMD_READ_SENSORS_IN_LEN);
  84. int rc;
  85. MCDI_SET_QWORD(inbuf, READ_SENSORS_IN_DMA_ADDR,
  86. hwmon->dma_buf.dma_addr);
  87. rc = efx_mcdi_rpc(efx, MC_CMD_READ_SENSORS,
  88. inbuf, sizeof(inbuf), NULL, 0, NULL);
  89. if (rc == 0)
  90. hwmon->last_update = jiffies;
  91. return rc;
  92. }
  93. static ssize_t efx_mcdi_mon_show_name(struct device *dev,
  94. struct device_attribute *attr,
  95. char *buf)
  96. {
  97. return sprintf(buf, "%s\n", KBUILD_MODNAME);
  98. }
  99. static int efx_mcdi_mon_get_entry(struct device *dev, unsigned int index,
  100. efx_dword_t *entry)
  101. {
  102. struct efx_nic *efx = dev_get_drvdata(dev);
  103. struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx);
  104. int rc;
  105. BUILD_BUG_ON(MC_CMD_READ_SENSORS_OUT_LEN != 0);
  106. mutex_lock(&hwmon->update_lock);
  107. /* Use cached value if last update was < 1 s ago */
  108. if (time_before(jiffies, hwmon->last_update + HZ))
  109. rc = 0;
  110. else
  111. rc = efx_mcdi_mon_update(efx);
  112. /* Copy out the requested entry */
  113. *entry = ((efx_dword_t *)hwmon->dma_buf.addr)[index];
  114. mutex_unlock(&hwmon->update_lock);
  115. return rc;
  116. }
  117. static ssize_t efx_mcdi_mon_show_value(struct device *dev,
  118. struct device_attribute *attr,
  119. char *buf)
  120. {
  121. struct efx_mcdi_mon_attribute *mon_attr =
  122. container_of(attr, struct efx_mcdi_mon_attribute, dev_attr);
  123. efx_dword_t entry;
  124. unsigned int value;
  125. int rc;
  126. rc = efx_mcdi_mon_get_entry(dev, mon_attr->index, &entry);
  127. if (rc)
  128. return rc;
  129. value = EFX_DWORD_FIELD(entry, MC_CMD_SENSOR_VALUE_ENTRY_TYPEDEF_VALUE);
  130. /* Convert temperature from degrees to milli-degrees Celsius */
  131. if (efx_mcdi_sensor_type[mon_attr->type].hwmon_type == EFX_HWMON_TEMP)
  132. value *= 1000;
  133. return sprintf(buf, "%u\n", value);
  134. }
  135. static ssize_t efx_mcdi_mon_show_limit(struct device *dev,
  136. struct device_attribute *attr,
  137. char *buf)
  138. {
  139. struct efx_mcdi_mon_attribute *mon_attr =
  140. container_of(attr, struct efx_mcdi_mon_attribute, dev_attr);
  141. unsigned int value;
  142. value = mon_attr->limit_value;
  143. /* Convert temperature from degrees to milli-degrees Celsius */
  144. if (efx_mcdi_sensor_type[mon_attr->type].hwmon_type == EFX_HWMON_TEMP)
  145. value *= 1000;
  146. return sprintf(buf, "%u\n", value);
  147. }
  148. static ssize_t efx_mcdi_mon_show_alarm(struct device *dev,
  149. struct device_attribute *attr,
  150. char *buf)
  151. {
  152. struct efx_mcdi_mon_attribute *mon_attr =
  153. container_of(attr, struct efx_mcdi_mon_attribute, dev_attr);
  154. efx_dword_t entry;
  155. int state;
  156. int rc;
  157. rc = efx_mcdi_mon_get_entry(dev, mon_attr->index, &entry);
  158. if (rc)
  159. return rc;
  160. state = EFX_DWORD_FIELD(entry, MC_CMD_SENSOR_VALUE_ENTRY_TYPEDEF_STATE);
  161. return sprintf(buf, "%d\n", state != MC_CMD_SENSOR_STATE_OK);
  162. }
  163. static ssize_t efx_mcdi_mon_show_label(struct device *dev,
  164. struct device_attribute *attr,
  165. char *buf)
  166. {
  167. struct efx_mcdi_mon_attribute *mon_attr =
  168. container_of(attr, struct efx_mcdi_mon_attribute, dev_attr);
  169. return sprintf(buf, "%s\n",
  170. efx_mcdi_sensor_type[mon_attr->type].label);
  171. }
  172. static int
  173. efx_mcdi_mon_add_attr(struct efx_nic *efx, const char *name,
  174. ssize_t (*reader)(struct device *,
  175. struct device_attribute *, char *),
  176. unsigned int index, unsigned int type,
  177. unsigned int limit_value)
  178. {
  179. struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx);
  180. struct efx_mcdi_mon_attribute *attr = &hwmon->attrs[hwmon->n_attrs];
  181. int rc;
  182. strlcpy(attr->name, name, sizeof(attr->name));
  183. attr->index = index;
  184. attr->type = type;
  185. attr->limit_value = limit_value;
  186. sysfs_attr_init(&attr->dev_attr.attr);
  187. attr->dev_attr.attr.name = attr->name;
  188. attr->dev_attr.attr.mode = S_IRUGO;
  189. attr->dev_attr.show = reader;
  190. rc = device_create_file(&efx->pci_dev->dev, &attr->dev_attr);
  191. if (rc == 0)
  192. ++hwmon->n_attrs;
  193. return rc;
  194. }
  195. int efx_mcdi_mon_probe(struct efx_nic *efx)
  196. {
  197. struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx);
  198. unsigned int n_attrs, n_temp = 0, n_cool = 0, n_in = 0;
  199. MCDI_DECLARE_BUF(outbuf, MC_CMD_SENSOR_INFO_OUT_LENMAX);
  200. size_t outlen;
  201. char name[12];
  202. u32 mask;
  203. int rc, i, type;
  204. BUILD_BUG_ON(MC_CMD_SENSOR_INFO_IN_LEN != 0);
  205. rc = efx_mcdi_rpc(efx, MC_CMD_SENSOR_INFO, NULL, 0,
  206. outbuf, sizeof(outbuf), &outlen);
  207. if (rc)
  208. return rc;
  209. if (outlen < MC_CMD_SENSOR_INFO_OUT_LENMIN)
  210. return -EIO;
  211. /* Find out which sensors are present. Don't create a device
  212. * if there are none.
  213. */
  214. mask = MCDI_DWORD(outbuf, SENSOR_INFO_OUT_MASK);
  215. if (mask == 0)
  216. return 0;
  217. /* Check again for short response */
  218. if (outlen < MC_CMD_SENSOR_INFO_OUT_LEN(hweight32(mask)))
  219. return -EIO;
  220. rc = efx_nic_alloc_buffer(efx, &hwmon->dma_buf,
  221. 4 * MC_CMD_SENSOR_ENTRY_MAXNUM, GFP_KERNEL);
  222. if (rc)
  223. return rc;
  224. mutex_init(&hwmon->update_lock);
  225. efx_mcdi_mon_update(efx);
  226. /* Allocate space for the maximum possible number of
  227. * attributes for this set of sensors: name of the driver plus
  228. * value, min, max, crit, alarm and label for each sensor.
  229. */
  230. n_attrs = 1 + 6 * hweight32(mask);
  231. hwmon->attrs = kcalloc(n_attrs, sizeof(*hwmon->attrs), GFP_KERNEL);
  232. if (!hwmon->attrs) {
  233. rc = -ENOMEM;
  234. goto fail;
  235. }
  236. hwmon->device = hwmon_device_register(&efx->pci_dev->dev);
  237. if (IS_ERR(hwmon->device)) {
  238. rc = PTR_ERR(hwmon->device);
  239. goto fail;
  240. }
  241. rc = efx_mcdi_mon_add_attr(efx, "name", efx_mcdi_mon_show_name, 0, 0, 0);
  242. if (rc)
  243. goto fail;
  244. for (i = 0, type = -1; ; i++) {
  245. const char *hwmon_prefix;
  246. unsigned hwmon_index;
  247. u16 min1, max1, min2, max2;
  248. /* Find next sensor type or exit if there is none */
  249. type++;
  250. while (!(mask & (1 << type))) {
  251. type++;
  252. if (type == 32)
  253. return 0;
  254. }
  255. /* Skip sensors specific to a different port */
  256. if (efx_mcdi_sensor_type[type].hwmon_type != EFX_HWMON_UNKNOWN &&
  257. efx_mcdi_sensor_type[type].port >= 0 &&
  258. efx_mcdi_sensor_type[type].port != efx_port_num(efx))
  259. continue;
  260. switch (efx_mcdi_sensor_type[type].hwmon_type) {
  261. case EFX_HWMON_TEMP:
  262. hwmon_prefix = "temp";
  263. hwmon_index = ++n_temp; /* 1-based */
  264. break;
  265. case EFX_HWMON_COOL:
  266. /* This is likely to be a heatsink, but there
  267. * is no convention for representing cooling
  268. * devices other than fans.
  269. */
  270. hwmon_prefix = "fan";
  271. hwmon_index = ++n_cool; /* 1-based */
  272. break;
  273. default:
  274. hwmon_prefix = "in";
  275. hwmon_index = n_in++; /* 0-based */
  276. break;
  277. }
  278. min1 = MCDI_ARRAY_FIELD(outbuf, SENSOR_ENTRY,
  279. SENSOR_INFO_ENTRY, i, MIN1);
  280. max1 = MCDI_ARRAY_FIELD(outbuf, SENSOR_ENTRY,
  281. SENSOR_INFO_ENTRY, i, MAX1);
  282. min2 = MCDI_ARRAY_FIELD(outbuf, SENSOR_ENTRY,
  283. SENSOR_INFO_ENTRY, i, MIN2);
  284. max2 = MCDI_ARRAY_FIELD(outbuf, SENSOR_ENTRY,
  285. SENSOR_INFO_ENTRY, i, MAX2);
  286. if (min1 != max1) {
  287. snprintf(name, sizeof(name), "%s%u_input",
  288. hwmon_prefix, hwmon_index);
  289. rc = efx_mcdi_mon_add_attr(
  290. efx, name, efx_mcdi_mon_show_value, i, type, 0);
  291. if (rc)
  292. goto fail;
  293. snprintf(name, sizeof(name), "%s%u_min",
  294. hwmon_prefix, hwmon_index);
  295. rc = efx_mcdi_mon_add_attr(
  296. efx, name, efx_mcdi_mon_show_limit,
  297. i, type, min1);
  298. if (rc)
  299. goto fail;
  300. snprintf(name, sizeof(name), "%s%u_max",
  301. hwmon_prefix, hwmon_index);
  302. rc = efx_mcdi_mon_add_attr(
  303. efx, name, efx_mcdi_mon_show_limit,
  304. i, type, max1);
  305. if (rc)
  306. goto fail;
  307. if (min2 != max2) {
  308. /* Assume max2 is critical value.
  309. * But we have no good way to expose min2.
  310. */
  311. snprintf(name, sizeof(name), "%s%u_crit",
  312. hwmon_prefix, hwmon_index);
  313. rc = efx_mcdi_mon_add_attr(
  314. efx, name, efx_mcdi_mon_show_limit,
  315. i, type, max2);
  316. if (rc)
  317. goto fail;
  318. }
  319. }
  320. snprintf(name, sizeof(name), "%s%u_alarm",
  321. hwmon_prefix, hwmon_index);
  322. rc = efx_mcdi_mon_add_attr(
  323. efx, name, efx_mcdi_mon_show_alarm, i, type, 0);
  324. if (rc)
  325. goto fail;
  326. if (efx_mcdi_sensor_type[type].label) {
  327. snprintf(name, sizeof(name), "%s%u_label",
  328. hwmon_prefix, hwmon_index);
  329. rc = efx_mcdi_mon_add_attr(
  330. efx, name, efx_mcdi_mon_show_label, i, type, 0);
  331. if (rc)
  332. goto fail;
  333. }
  334. }
  335. fail:
  336. efx_mcdi_mon_remove(efx);
  337. return rc;
  338. }
  339. void efx_mcdi_mon_remove(struct efx_nic *efx)
  340. {
  341. struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx);
  342. unsigned int i;
  343. for (i = 0; i < hwmon->n_attrs; i++)
  344. device_remove_file(&efx->pci_dev->dev,
  345. &hwmon->attrs[i].dev_attr);
  346. kfree(hwmon->attrs);
  347. if (hwmon->device)
  348. hwmon_device_unregister(hwmon->device);
  349. efx_nic_free_buffer(efx, &hwmon->dma_buf);
  350. }
  351. #endif /* CONFIG_SFC_MCDI_MON */