blob: 99c80f8c0a0e4da8a178f1aa4f24b41e77feccd3 [file] [log] [blame]
Ben Hutchings55c5e0f82012-01-06 20:25:39 +00001/****************************************************************************
Ben Hutchingsf7a6d2c2013-08-29 23:32:48 +01002 * Driver for Solarflare network controllers and boards
3 * Copyright 2011-2013 Solarflare Communications Inc.
Ben Hutchings55c5e0f82012-01-06 20:25:39 +00004 *
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
10#include <linux/bitops.h>
11#include <linux/slab.h>
12#include <linux/hwmon.h>
13#include <linux/stat.h>
14
15#include "net_driver.h"
16#include "mcdi.h"
17#include "mcdi_pcol.h"
18#include "nic.h"
19
20enum efx_hwmon_type {
21 EFX_HWMON_UNKNOWN,
22 EFX_HWMON_TEMP, /* temperature */
23 EFX_HWMON_COOL, /* cooling device, probably a heatsink */
Ben Hutchings38589cd2012-11-28 23:11:18 +000024 EFX_HWMON_IN, /* voltage */
25 EFX_HWMON_CURR, /* current */
26 EFX_HWMON_POWER, /* power */
Edward Cree2b216ce2013-09-30 10:52:49 +010027 EFX_HWMON_TYPES_COUNT
28};
29
30static const char *const efx_hwmon_unit[EFX_HWMON_TYPES_COUNT] = {
31 [EFX_HWMON_TEMP] = " degC",
32 [EFX_HWMON_COOL] = " rpm", /* though nonsense for a heatsink */
33 [EFX_HWMON_IN] = " mV",
34 [EFX_HWMON_CURR] = " mA",
35 [EFX_HWMON_POWER] = " W",
Ben Hutchings55c5e0f82012-01-06 20:25:39 +000036};
37
38static const struct {
39 const char *label;
40 enum efx_hwmon_type hwmon_type;
41 int port;
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +010042} efx_mcdi_sensor_type[] = {
Ben Hutchings38589cd2012-11-28 23:11:18 +000043#define SENSOR(name, label, hwmon_type, port) \
44 [MC_CMD_SENSOR_##name] = { label, EFX_HWMON_ ## hwmon_type, port }
45 SENSOR(CONTROLLER_TEMP, "Controller ext. temp.", TEMP, -1),
46 SENSOR(PHY_COMMON_TEMP, "PHY temp.", TEMP, -1),
47 SENSOR(CONTROLLER_COOLING, "Controller cooling", COOL, -1),
48 SENSOR(PHY0_TEMP, "PHY temp.", TEMP, 0),
49 SENSOR(PHY0_COOLING, "PHY cooling", COOL, 0),
50 SENSOR(PHY1_TEMP, "PHY temp.", TEMP, 1),
51 SENSOR(PHY1_COOLING, "PHY cooling", COOL, 1),
52 SENSOR(IN_1V0, "1.0V supply", IN, -1),
53 SENSOR(IN_1V2, "1.2V supply", IN, -1),
54 SENSOR(IN_1V8, "1.8V supply", IN, -1),
55 SENSOR(IN_2V5, "2.5V supply", IN, -1),
56 SENSOR(IN_3V3, "3.3V supply", IN, -1),
57 SENSOR(IN_12V0, "12.0V supply", IN, -1),
58 SENSOR(IN_1V2A, "1.2V analogue supply", IN, -1),
59 SENSOR(IN_VREF, "ref. voltage", IN, -1),
60 SENSOR(OUT_VAOE, "AOE power supply", IN, -1),
61 SENSOR(AOE_TEMP, "AOE temp.", TEMP, -1),
62 SENSOR(PSU_AOE_TEMP, "AOE PSU temp.", TEMP, -1),
63 SENSOR(PSU_TEMP, "Controller PSU temp.", TEMP, -1),
64 SENSOR(FAN_0, NULL, COOL, -1),
65 SENSOR(FAN_1, NULL, COOL, -1),
66 SENSOR(FAN_2, NULL, COOL, -1),
67 SENSOR(FAN_3, NULL, COOL, -1),
68 SENSOR(FAN_4, NULL, COOL, -1),
69 SENSOR(IN_VAOE, "AOE input supply", IN, -1),
70 SENSOR(OUT_IAOE, "AOE output current", CURR, -1),
71 SENSOR(IN_IAOE, "AOE input current", CURR, -1),
72 SENSOR(NIC_POWER, "Board power use", POWER, -1),
73 SENSOR(IN_0V9, "0.9V supply", IN, -1),
74 SENSOR(IN_I0V9, "0.9V input current", CURR, -1),
75 SENSOR(IN_I1V2, "1.2V input current", CURR, -1),
76 SENSOR(IN_0V9_ADC, "0.9V supply (at ADC)", IN, -1),
77 SENSOR(CONTROLLER_2_TEMP, "Controller ext. temp. 2", TEMP, -1),
78 SENSOR(VREG_INTERNAL_TEMP, "Voltage regulator temp.", TEMP, -1),
79 SENSOR(VREG_0V9_TEMP, "0.9V regulator temp.", TEMP, -1),
80 SENSOR(VREG_1V2_TEMP, "1.2V regulator temp.", TEMP, -1),
81 SENSOR(CONTROLLER_VPTAT, "Controller int. temp. raw", IN, -1),
82 SENSOR(CONTROLLER_INTERNAL_TEMP, "Controller int. temp.", TEMP, -1),
83 SENSOR(CONTROLLER_VPTAT_EXTADC,
84 "Controller int. temp. raw (at ADC)", IN, -1),
85 SENSOR(CONTROLLER_INTERNAL_TEMP_EXTADC,
86 "Controller int. temp. (via ADC)", TEMP, -1),
87 SENSOR(AMBIENT_TEMP, "Ambient temp.", TEMP, -1),
88 SENSOR(AIRFLOW, "Air flow raw", IN, -1),
Ben Hutchings55c5e0f82012-01-06 20:25:39 +000089#undef SENSOR
90};
91
92static const char *const sensor_status_names[] = {
93 [MC_CMD_SENSOR_STATE_OK] = "OK",
94 [MC_CMD_SENSOR_STATE_WARNING] = "Warning",
95 [MC_CMD_SENSOR_STATE_FATAL] = "Fatal",
96 [MC_CMD_SENSOR_STATE_BROKEN] = "Device failure",
Alexandre Rames8c4e7202013-07-03 09:47:34 +010097 [MC_CMD_SENSOR_STATE_NO_READING] = "No reading",
Ben Hutchings55c5e0f82012-01-06 20:25:39 +000098};
99
100void efx_mcdi_sensor_event(struct efx_nic *efx, efx_qword_t *ev)
101{
102 unsigned int type, state, value;
Edward Cree2b216ce2013-09-30 10:52:49 +0100103 enum efx_hwmon_type hwmon_type = EFX_HWMON_UNKNOWN;
104 const char *name = NULL, *state_txt, *unit;
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000105
106 type = EFX_QWORD_FIELD(*ev, MCDI_EVENT_SENSOREVT_MONITOR);
107 state = EFX_QWORD_FIELD(*ev, MCDI_EVENT_SENSOREVT_STATE);
108 value = EFX_QWORD_FIELD(*ev, MCDI_EVENT_SENSOREVT_VALUE);
109
110 /* Deal gracefully with the board having more drivers than we
111 * know about, but do not expect new sensor states. */
Edward Cree2b216ce2013-09-30 10:52:49 +0100112 if (type < ARRAY_SIZE(efx_mcdi_sensor_type)) {
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000113 name = efx_mcdi_sensor_type[type].label;
Edward Cree2b216ce2013-09-30 10:52:49 +0100114 hwmon_type = efx_mcdi_sensor_type[type].hwmon_type;
115 }
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000116 if (!name)
117 name = "No sensor name available";
118 EFX_BUG_ON_PARANOID(state >= ARRAY_SIZE(sensor_status_names));
119 state_txt = sensor_status_names[state];
Edward Cree2b216ce2013-09-30 10:52:49 +0100120 EFX_BUG_ON_PARANOID(hwmon_type >= EFX_HWMON_TYPES_COUNT);
121 unit = efx_hwmon_unit[hwmon_type];
122 if (!unit)
123 unit = "";
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000124
125 netif_err(efx, hw, efx->net_dev,
Edward Cree2b216ce2013-09-30 10:52:49 +0100126 "Sensor %d (%s) reports condition '%s' for value %d%s\n",
127 type, name, state_txt, value, unit);
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000128}
129
130#ifdef CONFIG_SFC_MCDI_MON
131
132struct efx_mcdi_mon_attribute {
133 struct device_attribute dev_attr;
134 unsigned int index;
135 unsigned int type;
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +0100136 enum efx_hwmon_type hwmon_type;
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000137 unsigned int limit_value;
138 char name[12];
139};
140
141static int efx_mcdi_mon_update(struct efx_nic *efx)
142{
143 struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx);
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +0100144 MCDI_DECLARE_BUF(inbuf, MC_CMD_READ_SENSORS_EXT_IN_LEN);
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000145 int rc;
146
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +0100147 MCDI_SET_QWORD(inbuf, READ_SENSORS_EXT_IN_DMA_ADDR,
Ben Hutchings338f74d2012-10-10 23:20:17 +0100148 hwmon->dma_buf.dma_addr);
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +0100149 MCDI_SET_DWORD(inbuf, READ_SENSORS_EXT_IN_LENGTH, hwmon->dma_buf.len);
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000150
151 rc = efx_mcdi_rpc(efx, MC_CMD_READ_SENSORS,
152 inbuf, sizeof(inbuf), NULL, 0, NULL);
153 if (rc == 0)
154 hwmon->last_update = jiffies;
155 return rc;
156}
157
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000158static int efx_mcdi_mon_get_entry(struct device *dev, unsigned int index,
159 efx_dword_t *entry)
160{
Guenter Roeck85493e62013-11-27 18:54:31 -0800161 struct efx_nic *efx = dev_get_drvdata(dev->parent);
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000162 struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx);
163 int rc;
164
165 BUILD_BUG_ON(MC_CMD_READ_SENSORS_OUT_LEN != 0);
166
167 mutex_lock(&hwmon->update_lock);
168
169 /* Use cached value if last update was < 1 s ago */
170 if (time_before(jiffies, hwmon->last_update + HZ))
171 rc = 0;
172 else
173 rc = efx_mcdi_mon_update(efx);
174
175 /* Copy out the requested entry */
176 *entry = ((efx_dword_t *)hwmon->dma_buf.addr)[index];
177
178 mutex_unlock(&hwmon->update_lock);
179
180 return rc;
181}
182
183static ssize_t efx_mcdi_mon_show_value(struct device *dev,
184 struct device_attribute *attr,
185 char *buf)
186{
187 struct efx_mcdi_mon_attribute *mon_attr =
188 container_of(attr, struct efx_mcdi_mon_attribute, dev_attr);
189 efx_dword_t entry;
Alexandre Rames8c4e7202013-07-03 09:47:34 +0100190 unsigned int value, state;
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000191 int rc;
192
193 rc = efx_mcdi_mon_get_entry(dev, mon_attr->index, &entry);
194 if (rc)
195 return rc;
196
Alexandre Rames8c4e7202013-07-03 09:47:34 +0100197 state = EFX_DWORD_FIELD(entry, MC_CMD_SENSOR_VALUE_ENTRY_TYPEDEF_STATE);
198 if (state == MC_CMD_SENSOR_STATE_NO_READING)
199 return -EBUSY;
200
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000201 value = EFX_DWORD_FIELD(entry, MC_CMD_SENSOR_VALUE_ENTRY_TYPEDEF_VALUE);
202
Ben Hutchings38589cd2012-11-28 23:11:18 +0000203 switch (mon_attr->hwmon_type) {
204 case EFX_HWMON_TEMP:
205 /* Convert temperature from degrees to milli-degrees Celsius */
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000206 value *= 1000;
Ben Hutchings38589cd2012-11-28 23:11:18 +0000207 break;
208 case EFX_HWMON_POWER:
209 /* Convert power from watts to microwatts */
210 value *= 1000000;
211 break;
212 default:
213 /* No conversion needed */
214 break;
215 }
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000216
217 return sprintf(buf, "%u\n", value);
218}
219
220static ssize_t efx_mcdi_mon_show_limit(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 unsigned int value;
227
228 value = mon_attr->limit_value;
229
Ben Hutchings38589cd2012-11-28 23:11:18 +0000230 switch (mon_attr->hwmon_type) {
231 case EFX_HWMON_TEMP:
232 /* Convert temperature from degrees to milli-degrees Celsius */
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000233 value *= 1000;
Ben Hutchings38589cd2012-11-28 23:11:18 +0000234 break;
235 case EFX_HWMON_POWER:
236 /* Convert power from watts to microwatts */
237 value *= 1000000;
238 break;
239 default:
240 /* No conversion needed */
241 break;
242 }
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000243
244 return sprintf(buf, "%u\n", value);
245}
246
247static ssize_t efx_mcdi_mon_show_alarm(struct device *dev,
248 struct device_attribute *attr,
249 char *buf)
250{
251 struct efx_mcdi_mon_attribute *mon_attr =
252 container_of(attr, struct efx_mcdi_mon_attribute, dev_attr);
253 efx_dword_t entry;
254 int state;
255 int rc;
256
257 rc = efx_mcdi_mon_get_entry(dev, mon_attr->index, &entry);
258 if (rc)
259 return rc;
260
261 state = EFX_DWORD_FIELD(entry, MC_CMD_SENSOR_VALUE_ENTRY_TYPEDEF_STATE);
262 return sprintf(buf, "%d\n", state != MC_CMD_SENSOR_STATE_OK);
263}
264
265static ssize_t efx_mcdi_mon_show_label(struct device *dev,
266 struct device_attribute *attr,
267 char *buf)
268{
269 struct efx_mcdi_mon_attribute *mon_attr =
270 container_of(attr, struct efx_mcdi_mon_attribute, dev_attr);
271 return sprintf(buf, "%s\n",
272 efx_mcdi_sensor_type[mon_attr->type].label);
273}
274
Guenter Roeck85493e62013-11-27 18:54:31 -0800275static void
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000276efx_mcdi_mon_add_attr(struct efx_nic *efx, const char *name,
277 ssize_t (*reader)(struct device *,
278 struct device_attribute *, char *),
279 unsigned int index, unsigned int type,
280 unsigned int limit_value)
281{
282 struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx);
283 struct efx_mcdi_mon_attribute *attr = &hwmon->attrs[hwmon->n_attrs];
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000284
285 strlcpy(attr->name, name, sizeof(attr->name));
286 attr->index = index;
287 attr->type = type;
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +0100288 if (type < ARRAY_SIZE(efx_mcdi_sensor_type))
289 attr->hwmon_type = efx_mcdi_sensor_type[type].hwmon_type;
290 else
291 attr->hwmon_type = EFX_HWMON_UNKNOWN;
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000292 attr->limit_value = limit_value;
Michal Schmidta9ec6bd2012-07-19 07:04:45 +0000293 sysfs_attr_init(&attr->dev_attr.attr);
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000294 attr->dev_attr.attr.name = attr->name;
295 attr->dev_attr.attr.mode = S_IRUGO;
296 attr->dev_attr.show = reader;
Guenter Roeck85493e62013-11-27 18:54:31 -0800297 hwmon->group.attrs[hwmon->n_attrs++] = &attr->dev_attr.attr;
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000298}
299
300int efx_mcdi_mon_probe(struct efx_nic *efx)
301{
Ben Hutchings38589cd2012-11-28 23:11:18 +0000302 unsigned int n_temp = 0, n_cool = 0, n_in = 0, n_curr = 0, n_power = 0;
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000303 struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx);
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +0100304 MCDI_DECLARE_BUF(inbuf, MC_CMD_SENSOR_INFO_EXT_IN_LEN);
Ben Hutchings59cfc472012-09-14 17:30:10 +0100305 MCDI_DECLARE_BUF(outbuf, MC_CMD_SENSOR_INFO_OUT_LENMAX);
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +0100306 unsigned int n_pages, n_sensors, n_attrs, page;
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000307 size_t outlen;
308 char name[12];
309 u32 mask;
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +0100310 int rc, i, j, type;
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000311
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +0100312 /* Find out how many sensors are present */
313 n_sensors = 0;
314 page = 0;
315 do {
316 MCDI_SET_DWORD(inbuf, SENSOR_INFO_EXT_IN_PAGE, page);
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000317
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +0100318 rc = efx_mcdi_rpc(efx, MC_CMD_SENSOR_INFO, inbuf, sizeof(inbuf),
319 outbuf, sizeof(outbuf), &outlen);
320 if (rc)
321 return rc;
322 if (outlen < MC_CMD_SENSOR_INFO_OUT_LENMIN)
323 return -EIO;
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000324
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +0100325 mask = MCDI_DWORD(outbuf, SENSOR_INFO_OUT_MASK);
326 n_sensors += hweight32(mask & ~(1 << MC_CMD_SENSOR_PAGE0_NEXT));
327 ++page;
328 } while (mask & (1 << MC_CMD_SENSOR_PAGE0_NEXT));
329 n_pages = page;
330
331 /* Don't create a device if there are none */
332 if (n_sensors == 0)
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000333 return 0;
334
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +0100335 rc = efx_nic_alloc_buffer(
336 efx, &hwmon->dma_buf,
337 n_sensors * MC_CMD_SENSOR_VALUE_ENTRY_TYPEDEF_LEN,
338 GFP_KERNEL);
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000339 if (rc)
340 return rc;
341
342 mutex_init(&hwmon->update_lock);
343 efx_mcdi_mon_update(efx);
344
345 /* Allocate space for the maximum possible number of
Guenter Roeck85493e62013-11-27 18:54:31 -0800346 * attributes for this set of sensors:
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000347 * value, min, max, crit, alarm and label for each sensor.
348 */
Guenter Roeck85493e62013-11-27 18:54:31 -0800349 n_attrs = 6 * n_sensors;
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000350 hwmon->attrs = kcalloc(n_attrs, sizeof(*hwmon->attrs), GFP_KERNEL);
351 if (!hwmon->attrs) {
352 rc = -ENOMEM;
353 goto fail;
354 }
Guenter Roeck85493e62013-11-27 18:54:31 -0800355 hwmon->group.attrs = kcalloc(n_attrs + 1, sizeof(struct attribute *),
356 GFP_KERNEL);
357 if (!hwmon->group.attrs) {
358 rc = -ENOMEM;
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000359 goto fail;
360 }
361
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +0100362 for (i = 0, j = -1, type = -1; ; i++) {
363 enum efx_hwmon_type hwmon_type;
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000364 const char *hwmon_prefix;
365 unsigned hwmon_index;
366 u16 min1, max1, min2, max2;
367
368 /* Find next sensor type or exit if there is none */
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +0100369 do {
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000370 type++;
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +0100371
372 if ((type % 32) == 0) {
373 page = type / 32;
374 j = -1;
375 if (page == n_pages)
Guenter Roeck85493e62013-11-27 18:54:31 -0800376 goto hwmon_register;
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +0100377
378 MCDI_SET_DWORD(inbuf, SENSOR_INFO_EXT_IN_PAGE,
379 page);
380 rc = efx_mcdi_rpc(efx, MC_CMD_SENSOR_INFO,
381 inbuf, sizeof(inbuf),
382 outbuf, sizeof(outbuf),
383 &outlen);
384 if (rc)
385 goto fail;
386 if (outlen < MC_CMD_SENSOR_INFO_OUT_LENMIN) {
387 rc = -EIO;
388 goto fail;
389 }
390
391 mask = (MCDI_DWORD(outbuf,
392 SENSOR_INFO_OUT_MASK) &
393 ~(1 << MC_CMD_SENSOR_PAGE0_NEXT));
394
395 /* Check again for short response */
396 if (outlen <
397 MC_CMD_SENSOR_INFO_OUT_LEN(hweight32(mask))) {
398 rc = -EIO;
399 goto fail;
400 }
401 }
402 } while (!(mask & (1 << type % 32)));
403 j++;
404
405 if (type < ARRAY_SIZE(efx_mcdi_sensor_type)) {
406 hwmon_type = efx_mcdi_sensor_type[type].hwmon_type;
407
408 /* Skip sensors specific to a different port */
409 if (hwmon_type != EFX_HWMON_UNKNOWN &&
410 efx_mcdi_sensor_type[type].port >= 0 &&
411 efx_mcdi_sensor_type[type].port !=
412 efx_port_num(efx))
413 continue;
414 } else {
415 hwmon_type = EFX_HWMON_UNKNOWN;
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000416 }
417
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +0100418 switch (hwmon_type) {
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000419 case EFX_HWMON_TEMP:
420 hwmon_prefix = "temp";
421 hwmon_index = ++n_temp; /* 1-based */
422 break;
423 case EFX_HWMON_COOL:
424 /* This is likely to be a heatsink, but there
425 * is no convention for representing cooling
426 * devices other than fans.
427 */
428 hwmon_prefix = "fan";
429 hwmon_index = ++n_cool; /* 1-based */
430 break;
431 default:
432 hwmon_prefix = "in";
433 hwmon_index = n_in++; /* 0-based */
434 break;
Ben Hutchings38589cd2012-11-28 23:11:18 +0000435 case EFX_HWMON_CURR:
436 hwmon_prefix = "curr";
437 hwmon_index = ++n_curr; /* 1-based */
438 break;
439 case EFX_HWMON_POWER:
440 hwmon_prefix = "power";
441 hwmon_index = ++n_power; /* 1-based */
442 break;
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000443 }
444
445 min1 = MCDI_ARRAY_FIELD(outbuf, SENSOR_ENTRY,
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +0100446 SENSOR_INFO_ENTRY, j, MIN1);
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000447 max1 = MCDI_ARRAY_FIELD(outbuf, SENSOR_ENTRY,
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +0100448 SENSOR_INFO_ENTRY, j, MAX1);
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000449 min2 = MCDI_ARRAY_FIELD(outbuf, SENSOR_ENTRY,
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +0100450 SENSOR_INFO_ENTRY, j, MIN2);
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000451 max2 = MCDI_ARRAY_FIELD(outbuf, SENSOR_ENTRY,
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +0100452 SENSOR_INFO_ENTRY, j, MAX2);
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000453
454 if (min1 != max1) {
455 snprintf(name, sizeof(name), "%s%u_input",
456 hwmon_prefix, hwmon_index);
Guenter Roeck85493e62013-11-27 18:54:31 -0800457 efx_mcdi_mon_add_attr(
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000458 efx, name, efx_mcdi_mon_show_value, i, type, 0);
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000459
Ben Hutchings38589cd2012-11-28 23:11:18 +0000460 if (hwmon_type != EFX_HWMON_POWER) {
461 snprintf(name, sizeof(name), "%s%u_min",
462 hwmon_prefix, hwmon_index);
Guenter Roeck85493e62013-11-27 18:54:31 -0800463 efx_mcdi_mon_add_attr(
Ben Hutchings38589cd2012-11-28 23:11:18 +0000464 efx, name, efx_mcdi_mon_show_limit,
465 i, type, min1);
Ben Hutchings38589cd2012-11-28 23:11:18 +0000466 }
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000467
468 snprintf(name, sizeof(name), "%s%u_max",
469 hwmon_prefix, hwmon_index);
Guenter Roeck85493e62013-11-27 18:54:31 -0800470 efx_mcdi_mon_add_attr(
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000471 efx, name, efx_mcdi_mon_show_limit,
472 i, type, max1);
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000473
474 if (min2 != max2) {
475 /* Assume max2 is critical value.
476 * But we have no good way to expose min2.
477 */
478 snprintf(name, sizeof(name), "%s%u_crit",
479 hwmon_prefix, hwmon_index);
Guenter Roeck85493e62013-11-27 18:54:31 -0800480 efx_mcdi_mon_add_attr(
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000481 efx, name, efx_mcdi_mon_show_limit,
482 i, type, max2);
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000483 }
484 }
485
486 snprintf(name, sizeof(name), "%s%u_alarm",
487 hwmon_prefix, hwmon_index);
Guenter Roeck85493e62013-11-27 18:54:31 -0800488 efx_mcdi_mon_add_attr(
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000489 efx, name, efx_mcdi_mon_show_alarm, i, type, 0);
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000490
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +0100491 if (type < ARRAY_SIZE(efx_mcdi_sensor_type) &&
492 efx_mcdi_sensor_type[type].label) {
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000493 snprintf(name, sizeof(name), "%s%u_label",
494 hwmon_prefix, hwmon_index);
Guenter Roeck85493e62013-11-27 18:54:31 -0800495 efx_mcdi_mon_add_attr(
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000496 efx, name, efx_mcdi_mon_show_label, i, type, 0);
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000497 }
498 }
499
Guenter Roeck85493e62013-11-27 18:54:31 -0800500hwmon_register:
501 hwmon->groups[0] = &hwmon->group;
502 hwmon->device = hwmon_device_register_with_groups(&efx->pci_dev->dev,
503 KBUILD_MODNAME, NULL,
504 hwmon->groups);
505 if (IS_ERR(hwmon->device)) {
506 rc = PTR_ERR(hwmon->device);
507 goto fail;
508 }
509
510 return 0;
511
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000512fail:
513 efx_mcdi_mon_remove(efx);
514 return rc;
515}
516
517void efx_mcdi_mon_remove(struct efx_nic *efx)
518{
Ben Hutchingse847b532012-09-13 01:11:24 +0100519 struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx);
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000520
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000521 if (hwmon->device)
522 hwmon_device_unregister(hwmon->device);
Guenter Roeck85493e62013-11-27 18:54:31 -0800523 kfree(hwmon->attrs);
524 kfree(hwmon->group.attrs);
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000525 efx_nic_free_buffer(efx, &hwmon->dma_buf);
526}
527
528#endif /* CONFIG_SFC_MCDI_MON */