mcdi_mon.c 13 KB

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