mcdi_mon.c 12 KB

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