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