1 /*
   2  * Copyright 2007-2013 Solarflare Communications Inc.  All rights reserved.
   3  *
   4  * Redistribution and use in source and binary forms, with or without
   5  * modification, are permitted provided that the following conditions
   6  * are met:
   7  * 1. Redistributions of source code must retain the above copyright
   8  *    notice, this list of conditions and the following disclaimer.
   9  * 2. Redistributions in binary form must reproduce the above copyright
  10  *    notice, this list of conditions and the following disclaimer in the
  11  *    documentation and/or other materials provided with the distribution.
  12  *
  13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS AND
  14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  23  * SUCH DAMAGE.
  24  */
  25 
  26 #include "efsys.h"
  27 #include "efx.h"
  28 #include "efx_types.h"
  29 #include "efx_regs.h"
  30 #include "efx_impl.h"
  31 
  32 #if EFSYS_OPT_MON_NULL
  33 #include "nullmon.h"
  34 #endif
  35 
  36 #if EFSYS_OPT_MON_LM87
  37 #include "lm87.h"
  38 #endif
  39 
  40 #if EFSYS_OPT_MON_MAX6647
  41 #include "max6647.h"
  42 #endif
  43 
  44 #if EFSYS_OPT_NAMES
  45 
  46 static const char       __cs * __cs __efx_mon_name[] = {
  47         "",
  48         "nullmon",
  49         "lm87",
  50         "max6647",
  51         "sfx90x0"
  52 };
  53 
  54                 const char __cs *
  55 efx_mon_name(
  56         __in    efx_nic_t *enp)
  57 {
  58         efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
  59 
  60         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
  61 
  62         EFSYS_ASSERT(encp->enc_mon_type != EFX_MON_INVALID);
  63         EFSYS_ASSERT3U(encp->enc_mon_type, <, EFX_MON_NTYPES);
  64         return (__efx_mon_name[encp->enc_mon_type]);
  65 }
  66 
  67 #endif  /* EFSYS_OPT_NAMES */
  68 
  69 #if EFSYS_OPT_MON_NULL
  70 static efx_mon_ops_t    __cs __efx_mon_null_ops = {
  71         nullmon_reset,                  /* emo_reset */
  72         nullmon_reconfigure,            /* emo_reconfigure */
  73 #if EFSYS_OPT_MON_STATS
  74         nullmon_stats_update            /* emo_stat_update */
  75 #endif  /* EFSYS_OPT_MON_STATS */
  76 };
  77 #endif
  78 
  79 #if EFSYS_OPT_MON_LM87
  80 static efx_mon_ops_t    __cs __efx_mon_lm87_ops = {
  81         lm87_reset,                     /* emo_reset */
  82         lm87_reconfigure,               /* emo_reconfigure */
  83 #if EFSYS_OPT_MON_STATS
  84         lm87_stats_update               /* emo_stat_update */
  85 #endif  /* EFSYS_OPT_MON_STATS */
  86 };
  87 #endif
  88 
  89 #if EFSYS_OPT_MON_MAX6647
  90 static efx_mon_ops_t    __cs __efx_mon_max6647_ops = {
  91         max6647_reset,                  /* emo_reset */
  92         max6647_reconfigure,            /* emo_reconfigure */
  93 #if EFSYS_OPT_MON_STATS
  94         max6647_stats_update            /* emo_stat_update */
  95 #endif  /* EFSYS_OPT_MON_STATS */
  96 };
  97 #endif
  98 
  99 #if EFSYS_OPT_MON_SIENA
 100 static efx_mon_ops_t    __cs __efx_mon_siena_ops = {
 101         siena_mon_reset,                /* emo_reset */
 102         siena_mon_reconfigure,          /* emo_reconfigure */
 103 #if EFSYS_OPT_MON_STATS
 104         siena_mon_stats_update          /* emo_stat_update */
 105 #endif  /* EFSYS_OPT_MON_STATS */
 106 };
 107 #endif
 108 
 109 
 110 static efx_mon_ops_t    __cs * __cs __efx_mon_ops[] = {
 111         NULL,
 112 #if EFSYS_OPT_MON_NULL
 113         &__efx_mon_null_ops,
 114 #else
 115         NULL,
 116 #endif
 117 #if EFSYS_OPT_MON_LM87
 118         &__efx_mon_lm87_ops,
 119 #else
 120         NULL,
 121 #endif
 122 #if EFSYS_OPT_MON_MAX6647
 123         &__efx_mon_max6647_ops,
 124 #else
 125         NULL,
 126 #endif
 127 #if EFSYS_OPT_MON_SIENA
 128         &__efx_mon_siena_ops
 129 #else
 130         NULL
 131 #endif
 132 };
 133 
 134         __checkReturn   int
 135 efx_mon_init(
 136         __in            efx_nic_t *enp)
 137 {
 138         efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
 139         efx_mon_t *emp = &(enp->en_mon);
 140         efx_mon_ops_t *emop;
 141         int rc;
 142 
 143         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
 144         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PROBE);
 145 
 146         if (enp->en_mod_flags & EFX_MOD_MON) {
 147                 rc = EINVAL;
 148                 goto fail1;
 149         }
 150 
 151         enp->en_mod_flags |= EFX_MOD_MON;
 152 
 153         emp->em_type = encp->enc_mon_type;
 154 
 155         EFSYS_ASSERT(encp->enc_mon_type != EFX_MON_INVALID);
 156         EFSYS_ASSERT3U(emp->em_type, <, EFX_MON_NTYPES);
 157         if ((emop = (efx_mon_ops_t *)__efx_mon_ops[emp->em_type]) == NULL) {
 158                 rc = ENOTSUP;
 159                 goto fail2;
 160         }
 161 
 162         if ((rc = emop->emo_reset(enp)) != 0)
 163                 goto fail3;
 164 
 165         if ((rc = emop->emo_reconfigure(enp)) != 0)
 166                 goto fail4;
 167 
 168         emp->em_emop = emop;
 169         return (0);
 170 
 171 fail4:
 172         EFSYS_PROBE(fail5);
 173 
 174         (void) emop->emo_reset(enp);
 175 
 176 fail3:
 177         EFSYS_PROBE(fail4);
 178 fail2:
 179         EFSYS_PROBE(fail3);
 180 
 181         emp->em_type = EFX_MON_INVALID;
 182 
 183         enp->en_mod_flags &= ~EFX_MOD_MON;
 184 
 185 fail1:
 186         EFSYS_PROBE1(fail1, int, rc);
 187 
 188         return (rc);
 189 }
 190 
 191 #if EFSYS_OPT_MON_STATS
 192 
 193 #if EFSYS_OPT_NAMES
 194 
 195 /* START MKCONFIG GENERATED MonitorStatNamesBlock cc98f339ca74c6b8 */
 196 static const char       __cs * __cs __mon_stat_name[] = {
 197         "value_2_5v",
 198         "value_vccp1",
 199         "value_vcc",
 200         "value_5v",
 201         "value_12v",
 202         "value_vccp2",
 203         "value_ext_temp",
 204         "value_int_temp",
 205         "value_ain1",
 206         "value_ain2",
 207         "controller_cooling",
 208         "ext_cooling",
 209         "1v",
 210         "1_2v",
 211         "1_8v",
 212         "3_3v",
 213         "1_2va",
 214         "vref",
 215 };
 216 
 217 /* END MKCONFIG GENERATED MonitorStatNamesBlock */
 218 
 219 extern                                  const char __cs *
 220 efx_mon_stat_name(
 221         __in                            efx_nic_t *enp,
 222         __in                            efx_mon_stat_t id)
 223 {
 224         _NOTE(ARGUNUSED(enp))
 225         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
 226 
 227         EFSYS_ASSERT3U(id, <, EFX_MON_NSTATS);
 228         return (__mon_stat_name[id]);
 229 }
 230 
 231 #endif  /* EFSYS_OPT_NAMES */
 232 
 233         __checkReturn                   int
 234 efx_mon_stats_update(
 235         __in                            efx_nic_t *enp,
 236         __in                            efsys_mem_t *esmp,
 237         __out_ecount(EFX_MON_NSTATS)    efx_mon_stat_value_t *values)
 238 {
 239         efx_mon_t *emp = &(enp->en_mon);
 240         efx_mon_ops_t *emop = emp->em_emop;
 241 
 242         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
 243         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_MON);
 244 
 245         return (emop->emo_stats_update(enp, esmp, values));
 246 }
 247 
 248 #endif  /* EFSYS_OPT_MON_STATS */
 249 
 250                 void
 251 efx_mon_fini(
 252         __in    efx_nic_t *enp)
 253 {
 254         efx_mon_t *emp = &(enp->en_mon);
 255         efx_mon_ops_t *emop = emp->em_emop;
 256         int rc;
 257 
 258         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
 259         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PROBE);
 260         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_MON);
 261 
 262         emp->em_emop = NULL;
 263 
 264         rc = emop->emo_reset(enp);
 265         if (rc != 0)
 266                 EFSYS_PROBE1(fail1, int, rc);
 267 
 268         emp->em_type = EFX_MON_INVALID;
 269 
 270         enp->en_mod_flags &= ~EFX_MOD_MON;
 271 }