1 /*-
2  * Copyright (c) 2009-2016 Solarflare Communications Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  *    this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  *    this list of conditions and the following disclaimer in the documentation
12  *    and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
24  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * The views and conclusions contained in the software and documentation are
27  * those of the authors and should not be interpreted as representing official
28  * policies, either expressed or implied, of the FreeBSD Project.
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD: stable/10/sys/dev/sfxge/common/mcdi_mon.c 342516 2018-12-26 10:25:01Z arybchik $");
33 
34 #include "efx.h"
35 #include "efx_impl.h"
36 #include "mcdi_mon.h"
37 
38 #if EFSYS_OPT_MON_MCDI
39 
40 #if EFSYS_OPT_MON_STATS
41 
42 #define	MCDI_MON_NEXT_PAGE  ((uint16_t)0xfffe)
43 #define	MCDI_MON_INVALID_SENSOR ((uint16_t)0xfffd)
44 #define	MCDI_MON_PAGE_SIZE 0x20
45 
46 /* Bitmasks of valid port(s) for each sensor */
47 #define	MCDI_MON_PORT_NONE	(0x00)
48 #define	MCDI_MON_PORT_P1	(0x01)
49 #define	MCDI_MON_PORT_P2	(0x02)
50 #define	MCDI_MON_PORT_P3	(0x04)
51 #define	MCDI_MON_PORT_P4	(0x08)
52 #define	MCDI_MON_PORT_Px	(0xFFFF)
53 
54 /* Get port mask from one-based MCDI port number */
55 #define	MCDI_MON_PORT_MASK(_emip) (1U << ((_emip)->emi_port - 1))
56 
57 /* Entry for MCDI sensor in sensor map */
58 #define	STAT(portmask, stat)	\
59 	{ (MCDI_MON_PORT_##portmask), (EFX_MON_STAT_##stat) }
60 
61 /* Entry for sensor next page flag in sensor map */
62 #define	STAT_NEXT_PAGE()	\
63 	{ MCDI_MON_PORT_NONE, MCDI_MON_NEXT_PAGE }
64 
65 /* Placeholder for gaps in the array */
66 #define	STAT_NO_SENSOR()	\
67 	{ MCDI_MON_PORT_NONE, MCDI_MON_INVALID_SENSOR }
68 
69 /* Map from MC sensors to monitor statistics */
70 static const struct mcdi_sensor_map_s {
71 	uint16_t	msm_port_mask;
72 	uint16_t	msm_stat;
73 } mcdi_sensor_map[] = {
74 	/* Sensor page 0		MC_CMD_SENSOR_xxx */
75 	STAT(Px, INT_TEMP),		/* 0x00 CONTROLLER_TEMP */
76 	STAT(Px, EXT_TEMP),		/* 0x01 PHY_COMMON_TEMP */
77 	STAT(Px, INT_COOLING),		/* 0x02 CONTROLLER_COOLING */
78 	STAT(P1, EXT_TEMP),		/* 0x03 PHY0_TEMP */
79 	STAT(P1, EXT_COOLING),		/* 0x04 PHY0_COOLING */
80 	STAT(P2, EXT_TEMP),		/* 0x05 PHY1_TEMP */
81 	STAT(P2, EXT_COOLING),		/* 0x06 PHY1_COOLING */
82 	STAT(Px, 1V),			/* 0x07 IN_1V0 */
83 	STAT(Px, 1_2V),			/* 0x08 IN_1V2 */
84 	STAT(Px, 1_8V),			/* 0x09 IN_1V8 */
85 	STAT(Px, 2_5V),			/* 0x0a IN_2V5 */
86 	STAT(Px, 3_3V),			/* 0x0b IN_3V3 */
87 	STAT(Px, 12V),			/* 0x0c IN_12V0 */
88 	STAT(Px, 1_2VA),		/* 0x0d IN_1V2A */
89 	STAT(Px, VREF),			/* 0x0e IN_VREF */
90 	STAT(Px, VAOE),			/* 0x0f OUT_VAOE */
91 	STAT(Px, AOE_TEMP),		/* 0x10 AOE_TEMP */
92 	STAT(Px, PSU_AOE_TEMP),		/* 0x11 PSU_AOE_TEMP */
93 	STAT(Px, PSU_TEMP),		/* 0x12 PSU_TEMP */
94 	STAT(Px, FAN0),			/* 0x13 FAN_0 */
95 	STAT(Px, FAN1),			/* 0x14 FAN_1 */
96 	STAT(Px, FAN2),			/* 0x15 FAN_2 */
97 	STAT(Px, FAN3),			/* 0x16 FAN_3 */
98 	STAT(Px, FAN4),			/* 0x17 FAN_4 */
99 	STAT(Px, VAOE_IN),		/* 0x18 IN_VAOE */
100 	STAT(Px, IAOE),			/* 0x19 OUT_IAOE */
101 	STAT(Px, IAOE_IN),		/* 0x1a IN_IAOE */
102 	STAT(Px, NIC_POWER),		/* 0x1b NIC_POWER */
103 	STAT(Px, 0_9V),			/* 0x1c IN_0V9 */
104 	STAT(Px, I0_9V),		/* 0x1d IN_I0V9 */
105 	STAT(Px, I1_2V),		/* 0x1e IN_I1V2 */
106 	STAT_NEXT_PAGE(),		/* 0x1f Next page flag (not a sensor) */
107 
108 	/* Sensor page 1		MC_CMD_SENSOR_xxx */
109 	STAT(Px, 0_9V_ADC),		/* 0x20 IN_0V9_ADC */
110 	STAT(Px, INT_TEMP2),		/* 0x21 CONTROLLER_2_TEMP */
111 	STAT(Px, VREG_TEMP),		/* 0x22 VREG_INTERNAL_TEMP */
112 	STAT(Px, VREG_0_9V_TEMP),	/* 0x23 VREG_0V9_TEMP */
113 	STAT(Px, VREG_1_2V_TEMP),	/* 0x24 VREG_1V2_TEMP */
114 	STAT(Px, INT_VPTAT),		/* 0x25 CTRLR. VPTAT */
115 	STAT(Px, INT_ADC_TEMP),		/* 0x26 CTRLR. INTERNAL_TEMP */
116 	STAT(Px, EXT_VPTAT),		/* 0x27 CTRLR. VPTAT_EXTADC */
117 	STAT(Px, EXT_ADC_TEMP),		/* 0x28 CTRLR. INTERNAL_TEMP_EXTADC */
118 	STAT(Px, AMBIENT_TEMP),		/* 0x29 AMBIENT_TEMP */
119 	STAT(Px, AIRFLOW),		/* 0x2a AIRFLOW */
120 	STAT(Px, VDD08D_VSS08D_CSR),	/* 0x2b VDD08D_VSS08D_CSR */
121 	STAT(Px, VDD08D_VSS08D_CSR_EXTADC), /* 0x2c VDD08D_VSS08D_CSR_EXTADC */
122 	STAT(Px, HOTPOINT_TEMP),	/* 0x2d HOTPOINT_TEMP */
123 	STAT(P1, PHY_POWER_SWITCH_PORT0),   /* 0x2e PHY_POWER_SWITCH_PORT0 */
124 	STAT(P2, PHY_POWER_SWITCH_PORT1),   /* 0x2f PHY_POWER_SWITCH_PORT1 */
125 	STAT(Px, MUM_VCC),		/* 0x30 MUM_VCC */
126 	STAT(Px, 0V9_A),		/* 0x31 0V9_A */
127 	STAT(Px, I0V9_A),		/* 0x32 I0V9_A */
128 	STAT(Px, 0V9_A_TEMP),		/* 0x33 0V9_A_TEMP */
129 	STAT(Px, 0V9_B),		/* 0x34 0V9_B */
130 	STAT(Px, I0V9_B),		/* 0x35 I0V9_B */
131 	STAT(Px, 0V9_B_TEMP),		/* 0x36 0V9_B_TEMP */
132 	STAT(Px, CCOM_AVREG_1V2_SUPPLY),  /* 0x37 CCOM_AVREG_1V2_SUPPLY */
133 	STAT(Px, CCOM_AVREG_1V2_SUPPLY_EXT_ADC),
134 					/* 0x38 CCOM_AVREG_1V2_SUPPLY_EXT_ADC */
135 	STAT(Px, CCOM_AVREG_1V8_SUPPLY),  /* 0x39 CCOM_AVREG_1V8_SUPPLY */
136 	STAT(Px, CCOM_AVREG_1V8_SUPPLY_EXT_ADC),
137 					/* 0x3a CCOM_AVREG_1V8_SUPPLY_EXT_ADC */
138 	STAT_NO_SENSOR(),		/* 0x3b (no sensor) */
139 	STAT_NO_SENSOR(),		/* 0x3c (no sensor) */
140 	STAT_NO_SENSOR(),		/* 0x3d (no sensor) */
141 	STAT_NO_SENSOR(),		/* 0x3e (no sensor) */
142 	STAT_NEXT_PAGE(),		/* 0x3f Next page flag (not a sensor) */
143 
144 	/* Sensor page 2		MC_CMD_SENSOR_xxx */
145 	STAT(Px, CONTROLLER_MASTER_VPTAT),	   /* 0x40 MASTER_VPTAT */
146 	STAT(Px, CONTROLLER_MASTER_INTERNAL_TEMP), /* 0x41 MASTER_INT_TEMP */
147 	STAT(Px, CONTROLLER_MASTER_VPTAT_EXT_ADC), /* 0x42 MAST_VPTAT_EXT_ADC */
148 	STAT(Px, CONTROLLER_MASTER_INTERNAL_TEMP_EXT_ADC),
149 					/* 0x43 MASTER_INTERNAL_TEMP_EXT_ADC */
150 	STAT(Px, CONTROLLER_SLAVE_VPTAT),	  /* 0x44 SLAVE_VPTAT */
151 	STAT(Px, CONTROLLER_SLAVE_INTERNAL_TEMP), /* 0x45 SLAVE_INTERNAL_TEMP */
152 	STAT(Px, CONTROLLER_SLAVE_VPTAT_EXT_ADC), /* 0x46 SLAVE_VPTAT_EXT_ADC */
153 	STAT(Px, CONTROLLER_SLAVE_INTERNAL_TEMP_EXT_ADC),
154 					/* 0x47 SLAVE_INTERNAL_TEMP_EXT_ADC */
155 	STAT_NO_SENSOR(),		/* 0x48 (no sensor) */
156 	STAT(Px, SODIMM_VOUT),		/* 0x49 SODIMM_VOUT */
157 	STAT(Px, SODIMM_0_TEMP),	/* 0x4a SODIMM_0_TEMP */
158 	STAT(Px, SODIMM_1_TEMP),	/* 0x4b SODIMM_1_TEMP */
159 	STAT(Px, PHY0_VCC),		/* 0x4c PHY0_VCC */
160 	STAT(Px, PHY1_VCC),		/* 0x4d PHY1_VCC */
161 	STAT(Px, CONTROLLER_TDIODE_TEMP), /* 0x4e CONTROLLER_TDIODE_TEMP */
162 	STAT(Px, BOARD_FRONT_TEMP),	/* 0x4f BOARD_FRONT_TEMP */
163 	STAT(Px, BOARD_BACK_TEMP),	/* 0x50 BOARD_BACK_TEMP */
164 };
165 
166 #define	MCDI_STATIC_SENSOR_ASSERT(_field)				\
167 	EFX_STATIC_ASSERT(MC_CMD_SENSOR_STATE_ ## _field		\
168 			    == EFX_MON_STAT_STATE_ ## _field)
169 
170 static						void
mcdi_mon_decode_stats(__in efx_nic_t * enp,__in_bcount (sensor_mask_size)uint32_t * sensor_mask,__in size_t sensor_mask_size,__in_opt efsys_mem_t * esmp,__out_bcount_opt (sensor_mask_size)uint32_t * stat_maskp,__inout_ecount_opt (EFX_MON_NSTATS)efx_mon_stat_value_t * stat)171 mcdi_mon_decode_stats(
172 	__in					efx_nic_t *enp,
173 	__in_bcount(sensor_mask_size)		uint32_t *sensor_mask,
174 	__in					size_t sensor_mask_size,
175 	__in_opt				efsys_mem_t *esmp,
176 	__out_bcount_opt(sensor_mask_size)	uint32_t *stat_maskp,
177 	__inout_ecount_opt(EFX_MON_NSTATS)	efx_mon_stat_value_t *stat)
178 {
179 	efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
180 	uint16_t port_mask;
181 	uint16_t sensor;
182 	size_t sensor_max;
183 	uint32_t stat_mask[(EFX_ARRAY_SIZE(mcdi_sensor_map) + 31) / 32];
184 	uint32_t idx = 0;
185 	uint32_t page = 0;
186 
187 	/* Assert the MC_CMD_SENSOR and EFX_MON_STATE namespaces agree */
188 	MCDI_STATIC_SENSOR_ASSERT(OK);
189 	MCDI_STATIC_SENSOR_ASSERT(WARNING);
190 	MCDI_STATIC_SENSOR_ASSERT(FATAL);
191 	MCDI_STATIC_SENSOR_ASSERT(BROKEN);
192 	MCDI_STATIC_SENSOR_ASSERT(NO_READING);
193 
194 	EFX_STATIC_ASSERT(sizeof (stat_mask[0]) * 8 ==
195 	    EFX_MON_MASK_ELEMENT_SIZE);
196 	sensor_max =
197 	    MIN((8 * sensor_mask_size), EFX_ARRAY_SIZE(mcdi_sensor_map));
198 
199 	EFSYS_ASSERT(emip->emi_port > 0); /* MCDI port number is one-based */
200 	port_mask = MCDI_MON_PORT_MASK(emip);
201 
202 	memset(stat_mask, 0, sizeof (stat_mask));
203 
204 	/*
205 	 * The MCDI sensor readings in the DMA buffer are a packed array of
206 	 * MC_CMD_SENSOR_VALUE_ENTRY structures, which only includes entries for
207 	 * supported sensors (bit set in sensor_mask). The sensor_mask and
208 	 * sensor readings do not include entries for the per-page NEXT_PAGE
209 	 * flag.
210 	 *
211 	 * sensor_mask may legitimately contain MCDI sensors that the driver
212 	 * does not understand.
213 	 */
214 	for (sensor = 0; sensor < sensor_max; ++sensor) {
215 		efx_mon_stat_t id = mcdi_sensor_map[sensor].msm_stat;
216 
217 		if ((sensor % MCDI_MON_PAGE_SIZE) == MC_CMD_SENSOR_PAGE0_NEXT) {
218 			EFSYS_ASSERT3U(id, ==, MCDI_MON_NEXT_PAGE);
219 			page++;
220 			continue;
221 		}
222 		if (~(sensor_mask[page]) & (1U << sensor))
223 			continue;
224 		idx++;
225 
226 		if ((port_mask & mcdi_sensor_map[sensor].msm_port_mask) == 0)
227 			continue;
228 		EFSYS_ASSERT(id < EFX_MON_NSTATS);
229 
230 		/*
231 		 * stat_mask is a bitmask indexed by EFX_MON_* monitor statistic
232 		 * identifiers from efx_mon_stat_t (without NEXT_PAGE bits).
233 		 *
234 		 * If there is an entry in the MCDI sensor to monitor statistic
235 		 * map then the sensor reading is used for the value of the
236 		 * monitor statistic.
237 		 */
238 		stat_mask[id / EFX_MON_MASK_ELEMENT_SIZE] |=
239 		    (1U << (id % EFX_MON_MASK_ELEMENT_SIZE));
240 
241 		if (stat != NULL && esmp != NULL && !EFSYS_MEM_IS_NULL(esmp)) {
242 			efx_dword_t dword;
243 
244 			/* Get MCDI sensor reading from DMA buffer */
245 			EFSYS_MEM_READD(esmp, 4 * (idx - 1), &dword);
246 
247 			/* Update EFX monitor stat from MCDI sensor reading */
248 			stat[id].emsv_value = (uint16_t)EFX_DWORD_FIELD(dword,
249 			    MC_CMD_SENSOR_VALUE_ENTRY_TYPEDEF_VALUE);
250 
251 			stat[id].emsv_state = (uint16_t)EFX_DWORD_FIELD(dword,
252 			    MC_CMD_SENSOR_VALUE_ENTRY_TYPEDEF_STATE);
253 		}
254 	}
255 
256 	if (stat_maskp != NULL) {
257 		memcpy(stat_maskp, stat_mask, sizeof (stat_mask));
258 	}
259 }
260 
261 	__checkReturn			efx_rc_t
mcdi_mon_ev(__in efx_nic_t * enp,__in efx_qword_t * eqp,__out efx_mon_stat_t * idp,__out efx_mon_stat_value_t * valuep)262 mcdi_mon_ev(
263 	__in				efx_nic_t *enp,
264 	__in				efx_qword_t *eqp,
265 	__out				efx_mon_stat_t *idp,
266 	__out				efx_mon_stat_value_t *valuep)
267 {
268 	efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
269 	uint16_t port_mask;
270 	uint16_t sensor;
271 	uint16_t state;
272 	uint16_t value;
273 	efx_mon_stat_t id;
274 	efx_rc_t rc;
275 
276 	EFSYS_ASSERT(emip->emi_port > 0); /* MCDI port number is one-based */
277 	port_mask = MCDI_MON_PORT_MASK(emip);
278 
279 	sensor = (uint16_t)MCDI_EV_FIELD(eqp, SENSOREVT_MONITOR);
280 	state = (uint16_t)MCDI_EV_FIELD(eqp, SENSOREVT_STATE);
281 	value = (uint16_t)MCDI_EV_FIELD(eqp, SENSOREVT_VALUE);
282 
283 	/* Hardware must support this MCDI sensor */
284 	EFSYS_ASSERT3U(sensor, <,
285 	    (8 * enp->en_nic_cfg.enc_mcdi_sensor_mask_size));
286 	EFSYS_ASSERT((sensor % MCDI_MON_PAGE_SIZE) != MC_CMD_SENSOR_PAGE0_NEXT);
287 	EFSYS_ASSERT(enp->en_nic_cfg.enc_mcdi_sensor_maskp != NULL);
288 	EFSYS_ASSERT(
289 	    (enp->en_nic_cfg.enc_mcdi_sensor_maskp[sensor/MCDI_MON_PAGE_SIZE] &
290 	    (1U << (sensor % MCDI_MON_PAGE_SIZE))) != 0);
291 
292 	/* But we don't have to understand it */
293 	if (sensor >= EFX_ARRAY_SIZE(mcdi_sensor_map)) {
294 		rc = ENOTSUP;
295 		goto fail1;
296 	}
297 	id = mcdi_sensor_map[sensor].msm_stat;
298 	if ((port_mask & mcdi_sensor_map[sensor].msm_port_mask) == 0)
299 		return (ENODEV);
300 	EFSYS_ASSERT(id < EFX_MON_NSTATS);
301 
302 	*idp = id;
303 	valuep->emsv_value = value;
304 	valuep->emsv_state = state;
305 
306 	return (0);
307 
308 fail1:
309 	EFSYS_PROBE1(fail1, efx_rc_t, rc);
310 
311 	return (rc);
312 }
313 
314 
315 static	__checkReturn	efx_rc_t
efx_mcdi_read_sensors(__in efx_nic_t * enp,__in efsys_mem_t * esmp,__in uint32_t size)316 efx_mcdi_read_sensors(
317 	__in		efx_nic_t *enp,
318 	__in		efsys_mem_t *esmp,
319 	__in		uint32_t size)
320 {
321 	efx_mcdi_req_t req;
322 	EFX_MCDI_DECLARE_BUF(payload, MC_CMD_READ_SENSORS_EXT_IN_LEN,
323 		MC_CMD_READ_SENSORS_EXT_OUT_LEN);
324 	uint32_t addr_lo, addr_hi;
325 	efx_rc_t rc;
326 
327 	if (EFSYS_MEM_SIZE(esmp) < size) {
328 		rc = EINVAL;
329 		goto fail1;
330 	}
331 
332 	req.emr_cmd = MC_CMD_READ_SENSORS;
333 	req.emr_in_buf = payload;
334 	req.emr_in_length = MC_CMD_READ_SENSORS_EXT_IN_LEN;
335 	req.emr_out_buf = payload;
336 	req.emr_out_length = MC_CMD_READ_SENSORS_EXT_OUT_LEN;
337 
338 	addr_lo = (uint32_t)(EFSYS_MEM_ADDR(esmp) & 0xffffffff);
339 	addr_hi = (uint32_t)(EFSYS_MEM_ADDR(esmp) >> 32);
340 
341 	MCDI_IN_SET_DWORD(req, READ_SENSORS_EXT_IN_DMA_ADDR_LO, addr_lo);
342 	MCDI_IN_SET_DWORD(req, READ_SENSORS_EXT_IN_DMA_ADDR_HI, addr_hi);
343 	MCDI_IN_SET_DWORD(req, READ_SENSORS_EXT_IN_LENGTH, size);
344 
345 	efx_mcdi_execute(enp, &req);
346 
347 	return (req.emr_rc);
348 
349 fail1:
350 	EFSYS_PROBE1(fail1, efx_rc_t, rc);
351 
352 	return (rc);
353 }
354 
355 static	__checkReturn	efx_rc_t
efx_mcdi_sensor_info_npages(__in efx_nic_t * enp,__out uint32_t * npagesp)356 efx_mcdi_sensor_info_npages(
357 	__in		efx_nic_t *enp,
358 	__out		uint32_t *npagesp)
359 {
360 	efx_mcdi_req_t req;
361 	EFX_MCDI_DECLARE_BUF(payload, MC_CMD_SENSOR_INFO_EXT_IN_LEN,
362 		MC_CMD_SENSOR_INFO_OUT_LENMAX);
363 	int page;
364 	efx_rc_t rc;
365 
366 	EFSYS_ASSERT(npagesp != NULL);
367 
368 	page = 0;
369 	do {
370 		(void) memset(payload, 0, sizeof (payload));
371 		req.emr_cmd = MC_CMD_SENSOR_INFO;
372 		req.emr_in_buf = payload;
373 		req.emr_in_length = MC_CMD_SENSOR_INFO_EXT_IN_LEN;
374 		req.emr_out_buf = payload;
375 		req.emr_out_length = MC_CMD_SENSOR_INFO_OUT_LENMAX;
376 
377 		MCDI_IN_SET_DWORD(req, SENSOR_INFO_EXT_IN_PAGE, page++);
378 
379 		efx_mcdi_execute_quiet(enp, &req);
380 
381 		if (req.emr_rc != 0) {
382 			rc = req.emr_rc;
383 			goto fail1;
384 		}
385 	} while (MCDI_OUT_DWORD(req, SENSOR_INFO_OUT_MASK) &
386 	    (1U << MC_CMD_SENSOR_PAGE0_NEXT));
387 
388 	*npagesp = page;
389 
390 	return (0);
391 
392 fail1:
393 	EFSYS_PROBE1(fail1, efx_rc_t, rc);
394 
395 	return (rc);
396 }
397 
398 static	__checkReturn		efx_rc_t
efx_mcdi_sensor_info(__in efx_nic_t * enp,__out_ecount (npages)uint32_t * sensor_maskp,__in size_t npages)399 efx_mcdi_sensor_info(
400 	__in			efx_nic_t *enp,
401 	__out_ecount(npages)	uint32_t *sensor_maskp,
402 	__in			size_t npages)
403 {
404 	efx_mcdi_req_t req;
405 	EFX_MCDI_DECLARE_BUF(payload, MC_CMD_SENSOR_INFO_EXT_IN_LEN,
406 		MC_CMD_SENSOR_INFO_OUT_LENMAX);
407 	uint32_t page;
408 	efx_rc_t rc;
409 
410 	EFSYS_ASSERT(sensor_maskp != NULL);
411 
412 	if (npages < 1) {
413 		rc = EINVAL;
414 		goto fail1;
415 	}
416 
417 	for (page = 0; page < npages; page++) {
418 		uint32_t mask;
419 
420 		(void) memset(payload, 0, sizeof (payload));
421 		req.emr_cmd = MC_CMD_SENSOR_INFO;
422 		req.emr_in_buf = payload;
423 		req.emr_in_length = MC_CMD_SENSOR_INFO_EXT_IN_LEN;
424 		req.emr_out_buf = payload;
425 		req.emr_out_length = MC_CMD_SENSOR_INFO_OUT_LENMAX;
426 
427 		MCDI_IN_SET_DWORD(req, SENSOR_INFO_EXT_IN_PAGE, page);
428 
429 		efx_mcdi_execute(enp, &req);
430 
431 		if (req.emr_rc != 0) {
432 			rc = req.emr_rc;
433 			goto fail2;
434 		}
435 
436 		mask = MCDI_OUT_DWORD(req, SENSOR_INFO_OUT_MASK);
437 
438 		if ((page != (npages - 1)) &&
439 		    ((mask & (1U << MC_CMD_SENSOR_PAGE0_NEXT)) == 0)) {
440 			rc = EINVAL;
441 			goto fail3;
442 		}
443 		sensor_maskp[page] = mask;
444 	}
445 
446 	if (sensor_maskp[npages - 1] & (1U << MC_CMD_SENSOR_PAGE0_NEXT)) {
447 		rc = EINVAL;
448 		goto fail4;
449 	}
450 
451 	return (0);
452 
453 fail4:
454 	EFSYS_PROBE(fail4);
455 fail3:
456 	EFSYS_PROBE(fail3);
457 fail2:
458 	EFSYS_PROBE(fail2);
459 fail1:
460 	EFSYS_PROBE1(fail1, efx_rc_t, rc);
461 
462 	return (rc);
463 }
464 
465 	__checkReturn			efx_rc_t
mcdi_mon_stats_update(__in efx_nic_t * enp,__in efsys_mem_t * esmp,__inout_ecount (EFX_MON_NSTATS)efx_mon_stat_value_t * values)466 mcdi_mon_stats_update(
467 	__in				efx_nic_t *enp,
468 	__in				efsys_mem_t *esmp,
469 	__inout_ecount(EFX_MON_NSTATS)	efx_mon_stat_value_t *values)
470 {
471 	efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
472 	uint32_t size = encp->enc_mon_stat_dma_buf_size;
473 	efx_rc_t rc;
474 
475 	if ((rc = efx_mcdi_read_sensors(enp, esmp, size)) != 0)
476 		goto fail1;
477 
478 	EFSYS_DMA_SYNC_FOR_KERNEL(esmp, 0, size);
479 
480 	mcdi_mon_decode_stats(enp,
481 	    encp->enc_mcdi_sensor_maskp,
482 	    encp->enc_mcdi_sensor_mask_size,
483 	    esmp, NULL, values);
484 
485 	return (0);
486 
487 fail1:
488 	EFSYS_PROBE1(fail1, efx_rc_t, rc);
489 
490 	return (rc);
491 }
492 
493 	__checkReturn	efx_rc_t
mcdi_mon_cfg_build(__in efx_nic_t * enp)494 mcdi_mon_cfg_build(
495 	__in		efx_nic_t *enp)
496 {
497 	efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
498 	uint32_t npages;
499 	efx_rc_t rc;
500 
501 	switch (enp->en_family) {
502 #if EFSYS_OPT_SIENA
503 	case EFX_FAMILY_SIENA:
504 		encp->enc_mon_type = EFX_MON_SFC90X0;
505 		break;
506 #endif
507 #if EFSYS_OPT_HUNTINGTON
508 	case EFX_FAMILY_HUNTINGTON:
509 		encp->enc_mon_type = EFX_MON_SFC91X0;
510 		break;
511 #endif
512 #if EFSYS_OPT_MEDFORD
513 	case EFX_FAMILY_MEDFORD:
514 		encp->enc_mon_type = EFX_MON_SFC92X0;
515 		break;
516 #endif
517 	default:
518 		rc = EINVAL;
519 		goto fail1;
520 	}
521 
522 	/* Get mc sensor mask size */
523 	npages = 0;
524 	if ((rc = efx_mcdi_sensor_info_npages(enp, &npages)) != 0)
525 		goto fail2;
526 
527 	encp->enc_mon_stat_dma_buf_size	= npages * EFX_MON_STATS_PAGE_SIZE;
528 	encp->enc_mcdi_sensor_mask_size = npages * sizeof (uint32_t);
529 
530 	/* Allocate mc sensor mask */
531 	EFSYS_KMEM_ALLOC(enp->en_esip,
532 	    encp->enc_mcdi_sensor_mask_size,
533 	    encp->enc_mcdi_sensor_maskp);
534 
535 	if (encp->enc_mcdi_sensor_maskp == NULL) {
536 		rc = ENOMEM;
537 		goto fail3;
538 	}
539 
540 	/* Read mc sensor mask */
541 	if ((rc = efx_mcdi_sensor_info(enp,
542 		    encp->enc_mcdi_sensor_maskp,
543 		    npages)) != 0)
544 		goto fail4;
545 
546 	/* Build monitor statistics mask */
547 	mcdi_mon_decode_stats(enp,
548 	    encp->enc_mcdi_sensor_maskp,
549 	    encp->enc_mcdi_sensor_mask_size,
550 	    NULL, encp->enc_mon_stat_mask, NULL);
551 
552 	return (0);
553 
554 fail4:
555 	EFSYS_PROBE(fail4);
556 	EFSYS_KMEM_FREE(enp->en_esip,
557 	    encp->enc_mcdi_sensor_mask_size,
558 	    encp->enc_mcdi_sensor_maskp);
559 
560 fail3:
561 	EFSYS_PROBE(fail3);
562 
563 fail2:
564 	EFSYS_PROBE(fail2);
565 
566 fail1:
567 	EFSYS_PROBE1(fail1, efx_rc_t, rc);
568 
569 	return (rc);
570 }
571 
572 			void
mcdi_mon_cfg_free(__in efx_nic_t * enp)573 mcdi_mon_cfg_free(
574 	__in		efx_nic_t *enp)
575 {
576 	efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
577 
578 	if (encp->enc_mcdi_sensor_maskp != NULL) {
579 		EFSYS_KMEM_FREE(enp->en_esip,
580 		    encp->enc_mcdi_sensor_mask_size,
581 		    encp->enc_mcdi_sensor_maskp);
582 	}
583 }
584 
585 
586 #endif	/* EFSYS_OPT_MON_STATS */
587 
588 #endif	/* EFSYS_OPT_MON_MCDI */
589