mcdi_mon.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /****************************************************************************
  2. * Driver for Solarflare network controllers and boards
  3. * Copyright 2011-2013 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, /* voltage */
  22. EFX_HWMON_CURR, /* current */
  23. EFX_HWMON_POWER, /* power */
  24. };
  25. static const struct {
  26. const char *label;
  27. enum efx_hwmon_type hwmon_type;
  28. int port;
  29. } efx_mcdi_sensor_type[] = {
  30. #define SENSOR(name, label, hwmon_type, port) \
  31. [MC_CMD_SENSOR_##name] = { label, EFX_HWMON_ ## hwmon_type, port }
  32. SENSOR(CONTROLLER_TEMP, "Controller ext. temp.", TEMP, -1),
  33. SENSOR(PHY_COMMON_TEMP, "PHY temp.", TEMP, -1),
  34. SENSOR(CONTROLLER_COOLING, "Controller cooling", COOL, -1),
  35. SENSOR(PHY0_TEMP, "PHY temp.", TEMP, 0),
  36. SENSOR(PHY0_COOLING, "PHY cooling", COOL, 0),
  37. SENSOR(PHY1_TEMP, "PHY temp.", TEMP, 1),
  38. SENSOR(PHY1_COOLING, "PHY cooling", COOL, 1),
  39. SENSOR(IN_1V0, "1.0V supply", IN, -1),
  40. SENSOR(IN_1V2, "1.2V supply", IN, -1),
  41. SENSOR(IN_1V8, "1.8V supply", IN, -1),
  42. SENSOR(IN_2V5, "2.5V supply", IN, -1),
  43. SENSOR(IN_3V3, "3.3V supply", IN, -1),
  44. SENSOR(IN_12V0, "12.0V supply", IN, -1),
  45. SENSOR(IN_1V2A, "1.2V analogue supply", IN, -1),
  46. SENSOR(IN_VREF, "ref. voltage", IN, -1),
  47. SENSOR(OUT_VAOE, "AOE power supply", IN, -1),
  48. SENSOR(AOE_TEMP, "AOE temp.", TEMP, -1),
  49. SENSOR(PSU_AOE_TEMP, "AOE PSU temp.", TEMP, -1),
  50. SENSOR(PSU_TEMP, "Controller PSU temp.", TEMP, -1),
  51. SENSOR(FAN_0, NULL, COOL, -1),
  52. SENSOR(FAN_1, NULL, COOL, -1),
  53. SENSOR(FAN_2, NULL, COOL, -1),
  54. SENSOR(FAN_3, NULL, COOL, -1),
  55. SENSOR(FAN_4, NULL, COOL, -1),
  56. SENSOR(IN_VAOE, "AOE input supply", IN, -1),
  57. SENSOR(OUT_IAOE, "AOE output current", CURR, -1),
  58. SENSOR(IN_IAOE, "AOE input current", CURR, -1),
  59. SENSOR(NIC_POWER, "Board power use", POWER, -1),
  60. SENSOR(IN_0V9, "0.9V supply", IN, -1),
  61. SENSOR(IN_I0V9, "0.9V input current", CURR, -1),
  62. SENSOR(IN_I1V2, "1.2V input current", CURR, -1),
  63. SENSOR(IN_0V9_ADC, "0.9V supply (at ADC)", IN, -1),
  64. SENSOR(CONTROLLER_2_TEMP, "Controller ext. temp. 2", TEMP, -1),
  65. SENSOR(VREG_INTERNAL_TEMP, "Voltage regulator temp.", TEMP, -1),
  66. SENSOR(VREG_0V9_TEMP, "0.9V regulator temp.", TEMP, -1),
  67. SENSOR(VREG_1V2_TEMP, "1.2V regulator temp.", TEMP, -1),
  68. SENSOR(CONTROLLER_VPTAT, "Controller int. temp. raw", IN, -1),
  69. SENSOR(CONTROLLER_INTERNAL_TEMP, "Controller int. temp.", TEMP, -1),
  70. SENSOR(CONTROLLER_VPTAT_EXTADC,
  71. "Controller int. temp. raw (at ADC)", IN, -1),
  72. SENSOR(CONTROLLER_INTERNAL_TEMP_EXTADC,
  73. "Controller int. temp. (via ADC)", TEMP, -1),
  74. SENSOR(AMBIENT_TEMP, "Ambient temp.", TEMP, -1),
  75. SENSOR(AIRFLOW, "Air flow raw", IN, -1),
  76. #undef SENSOR
  77. };
  78. static const char *const sensor_status_names[] = {
  79. [MC_CMD_SENSOR_STATE_OK] = "OK",
  80. [MC_CMD_SENSOR_STATE_WARNING] = "Warning",
  81. [MC_CMD_SENSOR_STATE_FATAL] = "Fatal",
  82. [MC_CMD_SENSOR_STATE_BROKEN] = "Device failure",
  83. [MC_CMD_SENSOR_STATE_NO_READING] = "No reading",
  84. };
  85. void efx_mcdi_sensor_event(struct efx_nic *efx, efx_qword_t *ev)
  86. {
  87. unsigned int type, state, value;
  88. const char *name = NULL, *state_txt;
  89. type = EFX_QWORD_FIELD(*ev, MCDI_EVENT_SENSOREVT_MONITOR);
  90. state = EFX_QWORD_FIELD(*ev, MCDI_EVENT_SENSOREVT_STATE);
  91. value = EFX_QWORD_FIELD(*ev, MCDI_EVENT_SENSOREVT_VALUE);
  92. /* Deal gracefully with the board having more drivers than we
  93. * know about, but do not expect new sensor states. */
  94. if (type < ARRAY_SIZE(efx_mcdi_sensor_type))
  95. name = efx_mcdi_sensor_type[type].label;
  96. if (!name)
  97. name = "No sensor name available";
  98. EFX_BUG_ON_PARANOID(state >= ARRAY_SIZE(sensor_status_names));
  99. state_txt = sensor_status_names[state];
  100. netif_err(efx, hw, efx->net_dev,
  101. "Sensor %d (%s) reports condition '%s' for raw value %d\n",
  102. type, name, state_txt, value);
  103. }
  104. #ifdef CONFIG_SFC_MCDI_MON
  105. struct efx_mcdi_mon_attribute {
  106. struct device_attribute dev_attr;
  107. unsigned int index;
  108. unsigned int type;
  109. enum efx_hwmon_type hwmon_type;
  110. unsigned int limit_value;
  111. char name[12];
  112. };
  113. static int efx_mcdi_mon_update(struct efx_nic *efx)
  114. {
  115. struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx);
  116. MCDI_DECLARE_BUF(inbuf, MC_CMD_READ_SENSORS_EXT_IN_LEN);
  117. int rc;
  118. MCDI_SET_QWORD(inbuf, READ_SENSORS_EXT_IN_DMA_ADDR,
  119. hwmon->dma_buf.dma_addr);
  120. MCDI_SET_DWORD(inbuf, READ_SENSORS_EXT_IN_LENGTH, hwmon->dma_buf.len);
  121. rc = efx_mcdi_rpc(efx, MC_CMD_READ_SENSORS,
  122. inbuf, sizeof(inbuf), NULL, 0, NULL);
  123. if (rc == 0)
  124. hwmon->last_update = jiffies;
  125. return rc;
  126. }
  127. static int efx_mcdi_mon_get_entry(struct device *dev, unsigned int index,
  128. efx_dword_t *entry)
  129. {
  130. struct efx_nic *efx = dev_get_drvdata(dev->parent);
  131. struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx);
  132. int rc;
  133. BUILD_BUG_ON(MC_CMD_READ_SENSORS_OUT_LEN != 0);
  134. mutex_lock(&hwmon->update_lock);
  135. /* Use cached value if last update was < 1 s ago */
  136. if (time_before(jiffies, hwmon->last_update + HZ))
  137. rc = 0;
  138. else
  139. rc = efx_mcdi_mon_update(efx);
  140. /* Copy out the requested entry */
  141. *entry = ((efx_dword_t *)hwmon->dma_buf.addr)[index];
  142. mutex_unlock(&hwmon->update_lock);
  143. return rc;
  144. }
  145. static ssize_t efx_mcdi_mon_show_value(struct device *dev,
  146. struct device_attribute *attr,
  147. char *buf)
  148. {
  149. struct efx_mcdi_mon_attribute *mon_attr =
  150. container_of(attr, struct efx_mcdi_mon_attribute, dev_attr);
  151. efx_dword_t entry;
  152. unsigned int value, state;
  153. int rc;
  154. rc = efx_mcdi_mon_get_entry(dev, mon_attr->index, &entry);
  155. if (rc)
  156. return rc;
  157. state = EFX_DWORD_FIELD(entry, MC_CMD_SENSOR_VALUE_ENTRY_TYPEDEF_STATE);
  158. if (state == MC_CMD_SENSOR_STATE_NO_READING)
  159. return -EBUSY;
  160. value = EFX_DWORD_FIELD(entry, MC_CMD_SENSOR_VALUE_ENTRY_TYPEDEF_VALUE);
  161. switch (mon_attr->hwmon_type) {
  162. case EFX_HWMON_TEMP:
  163. /* Convert temperature from degrees to milli-degrees Celsius */
  164. value *= 1000;
  165. break;
  166. case EFX_HWMON_POWER:
  167. /* Convert power from watts to microwatts */
  168. value *= 1000000;
  169. break;
  170. default:
  171. /* No conversion needed */
  172. break;
  173. }
  174. return sprintf(buf, "%u\n", value);
  175. }
  176. static ssize_t efx_mcdi_mon_show_limit(struct device *dev,
  177. struct device_attribute *attr,
  178. char *buf)
  179. {
  180. struct efx_mcdi_mon_attribute *mon_attr =
  181. container_of(attr, struct efx_mcdi_mon_attribute, dev_attr);
  182. unsigned int value;
  183. value = mon_attr->limit_value;
  184. switch (mon_attr->hwmon_type) {
  185. case EFX_HWMON_TEMP:
  186. /* Convert temperature from degrees to milli-degrees Celsius */
  187. value *= 1000;
  188. break;
  189. case EFX_HWMON_POWER:
  190. /* Convert power from watts to microwatts */
  191. value *= 1000000;
  192. break;
  193. default:
  194. /* No conversion needed */
  195. break;
  196. }
  197. return sprintf(buf, "%u\n", value);
  198. }
  199. static ssize_t efx_mcdi_mon_show_alarm(struct device *dev,
  200. struct device_attribute *attr,
  201. char *buf)
  202. {
  203. struct efx_mcdi_mon_attribute *mon_attr =
  204. container_of(attr, struct efx_mcdi_mon_attribute, dev_attr);
  205. efx_dword_t entry;
  206. int state;
  207. int rc;
  208. rc = efx_mcdi_mon_get_entry(dev, mon_attr->index, &entry);
  209. if (rc)
  210. return rc;
  211. state = EFX_DWORD_FIELD(entry, MC_CMD_SENSOR_VALUE_ENTRY_TYPEDEF_STATE);
  212. return sprintf(buf, "%d\n", state != MC_CMD_SENSOR_STATE_OK);
  213. }
  214. static ssize_t efx_mcdi_mon_show_label(struct device *dev,
  215. struct device_attribute *attr,
  216. char *buf)
  217. {
  218. struct efx_mcdi_mon_attribute *mon_attr =
  219. container_of(attr, struct efx_mcdi_mon_attribute, dev_attr);
  220. return sprintf(buf, "%s\n",
  221. efx_mcdi_sensor_type[mon_attr->type].label);
  222. }
  223. static void
  224. efx_mcdi_mon_add_attr(struct efx_nic *efx, const char *name,
  225. ssize_t (*reader)(struct device *,
  226. struct device_attribute *, char *),
  227. unsigned int index, unsigned int type,
  228. unsigned int limit_value)
  229. {
  230. struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx);
  231. struct efx_mcdi_mon_attribute *attr = &hwmon->attrs[hwmon->n_attrs];
  232. strlcpy(attr->name, name, sizeof(attr->name));
  233. attr->index = index;
  234. attr->type = type;
  235. if (type < ARRAY_SIZE(efx_mcdi_sensor_type))
  236. attr->hwmon_type = efx_mcdi_sensor_type[type].hwmon_type;
  237. else
  238. attr->hwmon_type = EFX_HWMON_UNKNOWN;
  239. attr->limit_value = limit_value;
  240. sysfs_attr_init(&attr->dev_attr.attr);
  241. attr->dev_attr.attr.name = attr->name;
  242. attr->dev_attr.attr.mode = S_IRUGO;
  243. attr->dev_attr.show = reader;
  244. hwmon->group.attrs[hwmon->n_attrs++] = &attr->dev_attr.attr;
  245. }
  246. int efx_mcdi_mon_probe(struct efx_nic *efx)
  247. {
  248. unsigned int n_temp = 0, n_cool = 0, n_in = 0, n_curr = 0, n_power = 0;
  249. struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx);
  250. MCDI_DECLARE_BUF(inbuf, MC_CMD_SENSOR_INFO_EXT_IN_LEN);
  251. MCDI_DECLARE_BUF(outbuf, MC_CMD_SENSOR_INFO_OUT_LENMAX);
  252. unsigned int n_pages, n_sensors, n_attrs, page;
  253. size_t outlen;
  254. char name[12];
  255. u32 mask;
  256. int rc, i, j, type;
  257. /* Find out how many sensors are present */
  258. n_sensors = 0;
  259. page = 0;
  260. do {
  261. MCDI_SET_DWORD(inbuf, SENSOR_INFO_EXT_IN_PAGE, page);
  262. rc = efx_mcdi_rpc(efx, MC_CMD_SENSOR_INFO, inbuf, sizeof(inbuf),
  263. outbuf, sizeof(outbuf), &outlen);
  264. if (rc)
  265. return rc;
  266. if (outlen < MC_CMD_SENSOR_INFO_OUT_LENMIN)
  267. return -EIO;
  268. mask = MCDI_DWORD(outbuf, SENSOR_INFO_OUT_MASK);
  269. n_sensors += hweight32(mask & ~(1 << MC_CMD_SENSOR_PAGE0_NEXT));
  270. ++page;
  271. } while (mask & (1 << MC_CMD_SENSOR_PAGE0_NEXT));
  272. n_pages = page;
  273. /* Don't create a device if there are none */
  274. if (n_sensors == 0)
  275. return 0;
  276. rc = efx_nic_alloc_buffer(
  277. efx, &hwmon->dma_buf,
  278. n_sensors * MC_CMD_SENSOR_VALUE_ENTRY_TYPEDEF_LEN,
  279. GFP_KERNEL);
  280. if (rc)
  281. return rc;
  282. mutex_init(&hwmon->update_lock);
  283. efx_mcdi_mon_update(efx);
  284. /* Allocate space for the maximum possible number of
  285. * attributes for this set of sensors:
  286. * value, min, max, crit, alarm and label for each sensor.
  287. */
  288. n_attrs = 6 * n_sensors;
  289. hwmon->attrs = kcalloc(n_attrs, sizeof(*hwmon->attrs), GFP_KERNEL);
  290. if (!hwmon->attrs) {
  291. rc = -ENOMEM;
  292. goto fail;
  293. }
  294. hwmon->group.attrs = kcalloc(n_attrs + 1, sizeof(struct attribute *),
  295. GFP_KERNEL);
  296. if (!hwmon->group.attrs) {
  297. rc = -ENOMEM;
  298. goto fail;
  299. }
  300. for (i = 0, j = -1, type = -1; ; i++) {
  301. enum efx_hwmon_type hwmon_type;
  302. const char *hwmon_prefix;
  303. unsigned hwmon_index;
  304. u16 min1, max1, min2, max2;
  305. /* Find next sensor type or exit if there is none */
  306. do {
  307. type++;
  308. if ((type % 32) == 0) {
  309. page = type / 32;
  310. j = -1;
  311. if (page == n_pages)
  312. goto hwmon_register;
  313. MCDI_SET_DWORD(inbuf, SENSOR_INFO_EXT_IN_PAGE,
  314. page);
  315. rc = efx_mcdi_rpc(efx, MC_CMD_SENSOR_INFO,
  316. inbuf, sizeof(inbuf),
  317. outbuf, sizeof(outbuf),
  318. &outlen);
  319. if (rc)
  320. goto fail;
  321. if (outlen < MC_CMD_SENSOR_INFO_OUT_LENMIN) {
  322. rc = -EIO;
  323. goto fail;
  324. }
  325. mask = (MCDI_DWORD(outbuf,
  326. SENSOR_INFO_OUT_MASK) &
  327. ~(1 << MC_CMD_SENSOR_PAGE0_NEXT));
  328. /* Check again for short response */
  329. if (outlen <
  330. MC_CMD_SENSOR_INFO_OUT_LEN(hweight32(mask))) {
  331. rc = -EIO;
  332. goto fail;
  333. }
  334. }
  335. } while (!(mask & (1 << type % 32)));
  336. j++;
  337. if (type < ARRAY_SIZE(efx_mcdi_sensor_type)) {
  338. hwmon_type = efx_mcdi_sensor_type[type].hwmon_type;
  339. /* Skip sensors specific to a different port */
  340. if (hwmon_type != EFX_HWMON_UNKNOWN &&
  341. efx_mcdi_sensor_type[type].port >= 0 &&
  342. efx_mcdi_sensor_type[type].port !=
  343. efx_port_num(efx))
  344. continue;
  345. } else {
  346. hwmon_type = EFX_HWMON_UNKNOWN;
  347. }
  348. switch (hwmon_type) {
  349. case EFX_HWMON_TEMP:
  350. hwmon_prefix = "temp";
  351. hwmon_index = ++n_temp; /* 1-based */
  352. break;
  353. case EFX_HWMON_COOL:
  354. /* This is likely to be a heatsink, but there
  355. * is no convention for representing cooling
  356. * devices other than fans.
  357. */
  358. hwmon_prefix = "fan";
  359. hwmon_index = ++n_cool; /* 1-based */
  360. break;
  361. default:
  362. hwmon_prefix = "in";
  363. hwmon_index = n_in++; /* 0-based */
  364. break;
  365. case EFX_HWMON_CURR:
  366. hwmon_prefix = "curr";
  367. hwmon_index = ++n_curr; /* 1-based */
  368. break;
  369. case EFX_HWMON_POWER:
  370. hwmon_prefix = "power";
  371. hwmon_index = ++n_power; /* 1-based */
  372. break;
  373. }
  374. min1 = MCDI_ARRAY_FIELD(outbuf, SENSOR_ENTRY,
  375. SENSOR_INFO_ENTRY, j, MIN1);
  376. max1 = MCDI_ARRAY_FIELD(outbuf, SENSOR_ENTRY,
  377. SENSOR_INFO_ENTRY, j, MAX1);
  378. min2 = MCDI_ARRAY_FIELD(outbuf, SENSOR_ENTRY,
  379. SENSOR_INFO_ENTRY, j, MIN2);
  380. max2 = MCDI_ARRAY_FIELD(outbuf, SENSOR_ENTRY,
  381. SENSOR_INFO_ENTRY, j, MAX2);
  382. if (min1 != max1) {
  383. snprintf(name, sizeof(name), "%s%u_input",
  384. hwmon_prefix, hwmon_index);
  385. efx_mcdi_mon_add_attr(
  386. efx, name, efx_mcdi_mon_show_value, i, type, 0);
  387. if (hwmon_type != EFX_HWMON_POWER) {
  388. snprintf(name, sizeof(name), "%s%u_min",
  389. hwmon_prefix, hwmon_index);
  390. efx_mcdi_mon_add_attr(
  391. efx, name, efx_mcdi_mon_show_limit,
  392. i, type, min1);
  393. }
  394. snprintf(name, sizeof(name), "%s%u_max",
  395. hwmon_prefix, hwmon_index);
  396. efx_mcdi_mon_add_attr(
  397. efx, name, efx_mcdi_mon_show_limit,
  398. i, type, max1);
  399. if (min2 != max2) {
  400. /* Assume max2 is critical value.
  401. * But we have no good way to expose min2.
  402. */
  403. snprintf(name, sizeof(name), "%s%u_crit",
  404. hwmon_prefix, hwmon_index);
  405. efx_mcdi_mon_add_attr(
  406. efx, name, efx_mcdi_mon_show_limit,
  407. i, type, max2);
  408. }
  409. }
  410. snprintf(name, sizeof(name), "%s%u_alarm",
  411. hwmon_prefix, hwmon_index);
  412. efx_mcdi_mon_add_attr(
  413. efx, name, efx_mcdi_mon_show_alarm, i, type, 0);
  414. if (type < ARRAY_SIZE(efx_mcdi_sensor_type) &&
  415. efx_mcdi_sensor_type[type].label) {
  416. snprintf(name, sizeof(name), "%s%u_label",
  417. hwmon_prefix, hwmon_index);
  418. efx_mcdi_mon_add_attr(
  419. efx, name, efx_mcdi_mon_show_label, i, type, 0);
  420. }
  421. }
  422. hwmon_register:
  423. hwmon->groups[0] = &hwmon->group;
  424. hwmon->device = hwmon_device_register_with_groups(&efx->pci_dev->dev,
  425. KBUILD_MODNAME, NULL,
  426. hwmon->groups);
  427. if (IS_ERR(hwmon->device)) {
  428. rc = PTR_ERR(hwmon->device);
  429. goto fail;
  430. }
  431. return 0;
  432. fail:
  433. efx_mcdi_mon_remove(efx);
  434. return rc;
  435. }
  436. void efx_mcdi_mon_remove(struct efx_nic *efx)
  437. {
  438. struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx);
  439. if (hwmon->device)
  440. hwmon_device_unregister(hwmon->device);
  441. kfree(hwmon->attrs);
  442. kfree(hwmon->group.attrs);
  443. efx_nic_free_buffer(efx, &hwmon->dma_buf);
  444. }
  445. #endif /* CONFIG_SFC_MCDI_MON */