net/sfc/base: fix warnings from VS2015 C compiler (C4189)
[dpdk.git] / drivers / net / sfc / base / mcdi_mon.c
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 "efx.h"
32 #include "efx_impl.h"
33
34 #if EFSYS_OPT_MON_MCDI
35
36 #if EFSYS_OPT_MON_STATS
37
38 #define MCDI_MON_NEXT_PAGE  ((uint16_t)0xfffe)
39 #define MCDI_MON_INVALID_SENSOR ((uint16_t)0xfffd)
40 #define MCDI_MON_PAGE_SIZE 0x20
41
42 /* Bitmasks of valid port(s) for each sensor */
43 #define MCDI_MON_PORT_NONE      (0x00)
44 #define MCDI_MON_PORT_P1        (0x01)
45 #define MCDI_MON_PORT_P2        (0x02)
46 #define MCDI_MON_PORT_P3        (0x04)
47 #define MCDI_MON_PORT_P4        (0x08)
48 #define MCDI_MON_PORT_Px        (0xFFFF)
49
50 /* Get port mask from one-based MCDI port number */
51 #define MCDI_MON_PORT_MASK(_emip) (1U << ((_emip)->emi_port - 1))
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         STAT(Px, BOARD_FRONT_TEMP),     /* 0x4f BOARD_FRONT_TEMP */
159         STAT(Px, BOARD_BACK_TEMP),      /* 0x50 BOARD_BACK_TEMP */
160         STAT(Px, I1V8),                 /* 0x51 IN_I1V8 */
161         STAT(Px, I2V5),                 /* 0x52 IN_I2V5 */
162 };
163
164 #define MCDI_STATIC_SENSOR_ASSERT(_field)                               \
165         EFX_STATIC_ASSERT(MC_CMD_SENSOR_STATE_ ## _field                \
166                             == EFX_MON_STAT_STATE_ ## _field)
167
168 static                                          void
169 mcdi_mon_decode_stats(
170         __in                                    efx_nic_t *enp,
171         __in_bcount(sensor_mask_size)           uint32_t *sensor_mask,
172         __in                                    size_t sensor_mask_size,
173         __in_opt                                efsys_mem_t *esmp,
174         __out_bcount_opt(sensor_mask_size)      uint32_t *stat_maskp,
175         __inout_ecount_opt(EFX_MON_NSTATS)      efx_mon_stat_value_t *stat)
176 {
177         efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
178         uint16_t port_mask;
179         uint16_t sensor;
180         size_t sensor_max;
181         uint32_t stat_mask[(EFX_ARRAY_SIZE(mcdi_sensor_map) + 31) / 32];
182         uint32_t idx = 0;
183         uint32_t page = 0;
184
185         /* Assert the MC_CMD_SENSOR and EFX_MON_STATE namespaces agree */
186         MCDI_STATIC_SENSOR_ASSERT(OK);
187         MCDI_STATIC_SENSOR_ASSERT(WARNING);
188         MCDI_STATIC_SENSOR_ASSERT(FATAL);
189         MCDI_STATIC_SENSOR_ASSERT(BROKEN);
190         MCDI_STATIC_SENSOR_ASSERT(NO_READING);
191
192         EFX_STATIC_ASSERT(sizeof (stat_mask[0]) * 8 ==
193             EFX_MON_MASK_ELEMENT_SIZE);
194         sensor_max =
195             MIN((8 * sensor_mask_size), EFX_ARRAY_SIZE(mcdi_sensor_map));
196
197         EFSYS_ASSERT(emip->emi_port > 0); /* MCDI port number is one-based */
198         port_mask = MCDI_MON_PORT_MASK(emip);
199
200         memset(stat_mask, 0, sizeof (stat_mask));
201
202         /*
203          * The MCDI sensor readings in the DMA buffer are a packed array of
204          * MC_CMD_SENSOR_VALUE_ENTRY structures, which only includes entries for
205          * supported sensors (bit set in sensor_mask). The sensor_mask and
206          * sensor readings do not include entries for the per-page NEXT_PAGE
207          * flag.
208          *
209          * sensor_mask may legitimately contain MCDI sensors that the driver
210          * does not understand.
211          */
212         for (sensor = 0; sensor < sensor_max; ++sensor) {
213                 efx_mon_stat_t id = mcdi_sensor_map[sensor].msm_stat;
214
215                 if ((sensor % MCDI_MON_PAGE_SIZE) == MC_CMD_SENSOR_PAGE0_NEXT) {
216                         EFSYS_ASSERT3U(id, ==, MCDI_MON_NEXT_PAGE);
217                         page++;
218                         continue;
219                 }
220                 if (~(sensor_mask[page]) & (1U << sensor))
221                         continue;
222                 idx++;
223
224                 if ((port_mask & mcdi_sensor_map[sensor].msm_port_mask) == 0)
225                         continue;
226                 EFSYS_ASSERT(id < EFX_MON_NSTATS);
227
228                 /*
229                  * stat_mask is a bitmask indexed by EFX_MON_* monitor statistic
230                  * identifiers from efx_mon_stat_t (without NEXT_PAGE bits).
231                  *
232                  * If there is an entry in the MCDI sensor to monitor statistic
233                  * map then the sensor reading is used for the value of the
234                  * monitor statistic.
235                  */
236                 stat_mask[id / EFX_MON_MASK_ELEMENT_SIZE] |=
237                     (1U << (id % EFX_MON_MASK_ELEMENT_SIZE));
238
239                 if (stat != NULL && esmp != NULL && !EFSYS_MEM_IS_NULL(esmp)) {
240                         efx_dword_t dword;
241
242                         /* Get MCDI sensor reading from DMA buffer */
243                         EFSYS_MEM_READD(esmp, 4 * (idx - 1), &dword);
244
245                         /* Update EFX monitor stat from MCDI sensor reading */
246                         stat[id].emsv_value = (uint16_t)EFX_DWORD_FIELD(dword,
247                             MC_CMD_SENSOR_VALUE_ENTRY_TYPEDEF_VALUE);
248
249                         stat[id].emsv_state = (uint16_t)EFX_DWORD_FIELD(dword,
250                             MC_CMD_SENSOR_VALUE_ENTRY_TYPEDEF_STATE);
251                 }
252         }
253
254         if (stat_maskp != NULL) {
255                 memcpy(stat_maskp, stat_mask, sizeof (stat_mask));
256         }
257 }
258
259         __checkReturn                   efx_rc_t
260 mcdi_mon_ev(
261         __in                            efx_nic_t *enp,
262         __in                            efx_qword_t *eqp,
263         __out                           efx_mon_stat_t *idp,
264         __out                           efx_mon_stat_value_t *valuep)
265 {
266         efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
267         uint16_t port_mask;
268         uint16_t sensor;
269         uint16_t state;
270         uint16_t value;
271         efx_mon_stat_t id;
272         efx_rc_t rc;
273
274         EFSYS_ASSERT(emip->emi_port > 0); /* MCDI port number is one-based */
275         port_mask = MCDI_MON_PORT_MASK(emip);
276
277         sensor = (uint16_t)MCDI_EV_FIELD(eqp, SENSOREVT_MONITOR);
278         state = (uint16_t)MCDI_EV_FIELD(eqp, SENSOREVT_STATE);
279         value = (uint16_t)MCDI_EV_FIELD(eqp, SENSOREVT_VALUE);
280
281         /* Hardware must support this MCDI sensor */
282         EFSYS_ASSERT3U(sensor, <,
283             (8 * enp->en_nic_cfg.enc_mcdi_sensor_mask_size));
284         EFSYS_ASSERT((sensor % MCDI_MON_PAGE_SIZE) != MC_CMD_SENSOR_PAGE0_NEXT);
285         EFSYS_ASSERT(enp->en_nic_cfg.enc_mcdi_sensor_maskp != NULL);
286         EFSYS_ASSERT(
287             (enp->en_nic_cfg.enc_mcdi_sensor_maskp[sensor/MCDI_MON_PAGE_SIZE] &
288             (1U << (sensor % MCDI_MON_PAGE_SIZE))) != 0);
289
290         /* But we don't have to understand it */
291         if (sensor >= EFX_ARRAY_SIZE(mcdi_sensor_map)) {
292                 rc = ENOTSUP;
293                 goto fail1;
294         }
295         id = mcdi_sensor_map[sensor].msm_stat;
296         if ((port_mask & mcdi_sensor_map[sensor].msm_port_mask) == 0)
297                 return (ENODEV);
298         EFSYS_ASSERT(id < EFX_MON_NSTATS);
299
300         *idp = id;
301         valuep->emsv_value = value;
302         valuep->emsv_state = state;
303
304         return (0);
305
306 fail1:
307         EFSYS_PROBE1(fail1, efx_rc_t, rc);
308
309         return (rc);
310 }
311
312
313 static  __checkReturn   efx_rc_t
314 efx_mcdi_read_sensors(
315         __in            efx_nic_t *enp,
316         __in            efsys_mem_t *esmp,
317         __in            uint32_t size)
318 {
319         efx_mcdi_req_t req;
320         uint8_t payload[MAX(MC_CMD_READ_SENSORS_EXT_IN_LEN,
321                             MC_CMD_READ_SENSORS_EXT_OUT_LEN)];
322         uint32_t addr_lo, addr_hi;
323
324         req.emr_cmd = MC_CMD_READ_SENSORS;
325         req.emr_in_buf = payload;
326         req.emr_in_length = MC_CMD_READ_SENSORS_EXT_IN_LEN;
327         req.emr_out_buf = payload;
328         req.emr_out_length = MC_CMD_READ_SENSORS_EXT_OUT_LEN;
329
330         addr_lo = (uint32_t)(EFSYS_MEM_ADDR(esmp) & 0xffffffff);
331         addr_hi = (uint32_t)(EFSYS_MEM_ADDR(esmp) >> 32);
332
333         MCDI_IN_SET_DWORD(req, READ_SENSORS_EXT_IN_DMA_ADDR_LO, addr_lo);
334         MCDI_IN_SET_DWORD(req, READ_SENSORS_EXT_IN_DMA_ADDR_HI, addr_hi);
335         MCDI_IN_SET_DWORD(req, READ_SENSORS_EXT_IN_LENGTH, size);
336
337         efx_mcdi_execute(enp, &req);
338
339         return (req.emr_rc);
340 }
341
342 static  __checkReturn   efx_rc_t
343 efx_mcdi_sensor_info_npages(
344         __in            efx_nic_t *enp,
345         __out           uint32_t *npagesp)
346 {
347         efx_mcdi_req_t req;
348         uint8_t payload[MAX(MC_CMD_SENSOR_INFO_EXT_IN_LEN,
349                             MC_CMD_SENSOR_INFO_OUT_LENMAX)];
350         int page;
351         efx_rc_t rc;
352
353         EFSYS_ASSERT(npagesp != NULL);
354
355         page = 0;
356         do {
357                 (void) memset(payload, 0, sizeof (payload));
358                 req.emr_cmd = MC_CMD_SENSOR_INFO;
359                 req.emr_in_buf = payload;
360                 req.emr_in_length = MC_CMD_SENSOR_INFO_EXT_IN_LEN;
361                 req.emr_out_buf = payload;
362                 req.emr_out_length = MC_CMD_SENSOR_INFO_OUT_LENMAX;
363
364                 MCDI_IN_SET_DWORD(req, SENSOR_INFO_EXT_IN_PAGE, page++);
365
366                 efx_mcdi_execute_quiet(enp, &req);
367
368                 if (req.emr_rc != 0) {
369                         rc = req.emr_rc;
370                         goto fail1;
371                 }
372         } while (MCDI_OUT_DWORD(req, SENSOR_INFO_OUT_MASK) &
373             (1U << MC_CMD_SENSOR_PAGE0_NEXT));
374
375         *npagesp = page;
376
377         return (0);
378
379 fail1:
380         EFSYS_PROBE1(fail1, efx_rc_t, rc);
381
382         return (rc);
383 }
384
385 static  __checkReturn           efx_rc_t
386 efx_mcdi_sensor_info(
387         __in                    efx_nic_t *enp,
388         __out_ecount(npages)    uint32_t *sensor_maskp,
389         __in                    size_t npages)
390 {
391         efx_mcdi_req_t req;
392         uint8_t payload[MAX(MC_CMD_SENSOR_INFO_EXT_IN_LEN,
393                             MC_CMD_SENSOR_INFO_OUT_LENMAX)];
394         uint32_t page;
395         efx_rc_t rc;
396
397         EFSYS_ASSERT(sensor_maskp != NULL);
398
399         for (page = 0; page < npages; page++) {
400                 uint32_t mask;
401
402                 (void) memset(payload, 0, sizeof (payload));
403                 req.emr_cmd = MC_CMD_SENSOR_INFO;
404                 req.emr_in_buf = payload;
405                 req.emr_in_length = MC_CMD_SENSOR_INFO_EXT_IN_LEN;
406                 req.emr_out_buf = payload;
407                 req.emr_out_length = MC_CMD_SENSOR_INFO_OUT_LENMAX;
408
409                 MCDI_IN_SET_DWORD(req, SENSOR_INFO_EXT_IN_PAGE, page);
410
411                 efx_mcdi_execute(enp, &req);
412
413                 if (req.emr_rc != 0) {
414                         rc = req.emr_rc;
415                         goto fail1;
416                 }
417
418                 mask = MCDI_OUT_DWORD(req, SENSOR_INFO_OUT_MASK);
419
420                 if ((page != (npages - 1)) &&
421                     ((mask & (1U << MC_CMD_SENSOR_PAGE0_NEXT)) == 0)) {
422                         rc = EINVAL;
423                         goto fail2;
424                 }
425                 sensor_maskp[page] = mask;
426         }
427
428         if (sensor_maskp[npages - 1] & (1U << MC_CMD_SENSOR_PAGE0_NEXT)) {
429                 rc = EINVAL;
430                 goto fail3;
431         }
432
433         return (0);
434
435 fail3:
436         EFSYS_PROBE(fail3);
437 fail2:
438         EFSYS_PROBE(fail2);
439 fail1:
440         EFSYS_PROBE1(fail1, efx_rc_t, rc);
441
442         return (rc);
443 }
444
445         __checkReturn                   efx_rc_t
446 mcdi_mon_stats_update(
447         __in                            efx_nic_t *enp,
448         __in                            efsys_mem_t *esmp,
449         __inout_ecount(EFX_MON_NSTATS)  efx_mon_stat_value_t *values)
450 {
451         efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
452         uint32_t size = encp->enc_mon_stat_dma_buf_size;
453         efx_rc_t rc;
454
455         if ((rc = efx_mcdi_read_sensors(enp, esmp, size)) != 0)
456                 goto fail1;
457
458         EFSYS_DMA_SYNC_FOR_KERNEL(esmp, 0, size);
459
460         mcdi_mon_decode_stats(enp,
461             encp->enc_mcdi_sensor_maskp,
462             encp->enc_mcdi_sensor_mask_size,
463             esmp, NULL, values);
464
465         return (0);
466
467 fail1:
468         EFSYS_PROBE1(fail1, efx_rc_t, rc);
469
470         return (rc);
471 }
472
473         __checkReturn   efx_rc_t
474 mcdi_mon_cfg_build(
475         __in            efx_nic_t *enp)
476 {
477         efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
478         uint32_t npages;
479         efx_rc_t rc;
480
481         switch (enp->en_family) {
482 #if EFSYS_OPT_SIENA
483         case EFX_FAMILY_SIENA:
484                 encp->enc_mon_type = EFX_MON_SFC90X0;
485                 break;
486 #endif
487 #if EFSYS_OPT_HUNTINGTON
488         case EFX_FAMILY_HUNTINGTON:
489                 encp->enc_mon_type = EFX_MON_SFC91X0;
490                 break;
491 #endif
492 #if EFSYS_OPT_MEDFORD
493         case EFX_FAMILY_MEDFORD:
494                 encp->enc_mon_type = EFX_MON_SFC92X0;
495                 break;
496 #endif
497         default:
498                 rc = EINVAL;
499                 goto fail1;
500         }
501
502         /* Get mc sensor mask size */
503         npages = 0;
504         if ((rc = efx_mcdi_sensor_info_npages(enp, &npages)) != 0)
505                 goto fail2;
506
507         encp->enc_mon_stat_dma_buf_size = npages * EFX_MON_STATS_PAGE_SIZE;
508         encp->enc_mcdi_sensor_mask_size = npages * sizeof (uint32_t);
509
510         /* Allocate mc sensor mask */
511         EFSYS_KMEM_ALLOC(enp->en_esip,
512             encp->enc_mcdi_sensor_mask_size,
513             encp->enc_mcdi_sensor_maskp);
514
515         if (encp->enc_mcdi_sensor_maskp == NULL) {
516                 rc = ENOMEM;
517                 goto fail3;
518         }
519
520         /* Read mc sensor mask */
521         if ((rc = efx_mcdi_sensor_info(enp,
522                     encp->enc_mcdi_sensor_maskp,
523                     npages)) != 0)
524                 goto fail4;
525
526         /* Build monitor statistics mask */
527         mcdi_mon_decode_stats(enp,
528             encp->enc_mcdi_sensor_maskp,
529             encp->enc_mcdi_sensor_mask_size,
530             NULL, encp->enc_mon_stat_mask, NULL);
531
532         return (0);
533
534 fail4:
535         EFSYS_PROBE(fail4);
536         EFSYS_KMEM_FREE(enp->en_esip,
537             encp->enc_mcdi_sensor_mask_size,
538             encp->enc_mcdi_sensor_maskp);
539
540 fail3:
541         EFSYS_PROBE(fail3);
542
543 fail2:
544         EFSYS_PROBE(fail2);
545
546 fail1:
547         EFSYS_PROBE1(fail1, efx_rc_t, rc);
548
549         return (rc);
550 }
551
552                         void
553 mcdi_mon_cfg_free(
554         __in            efx_nic_t *enp)
555 {
556         efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
557
558         if (encp->enc_mcdi_sensor_maskp != NULL) {
559                 EFSYS_KMEM_FREE(enp->en_esip,
560                     encp->enc_mcdi_sensor_mask_size,
561                     encp->enc_mcdi_sensor_maskp);
562         }
563 }
564
565
566 #endif  /* EFSYS_OPT_MON_STATS */
567
568 #endif  /* EFSYS_OPT_MON_MCDI */