mcdi_mon.c 15 KB

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