mcdi_mon.c 12 KB

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