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