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