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