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