1 /*
   2  * This file and its contents are supplied under the terms of the
   3  * Common Development and Distribution License ("CDDL"), version 1.0.
   4  * You may only use this file in accordance with the terms of version
   5  * 1.0 of the CDDL.
   6  *
   7  * A full copy of the text of the CDDL should have accompanied this
   8  * source.  A copy of the CDDL is also available via the Internet at
   9  * http://www.illumos.org/license/CDDL.
  10  */
  11 
  12 /*
  13  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
  14  */
  15 
  16 #include <unistd.h>
  17 #include <stdio.h>
  18 #include <stdlib.h>
  19 #include <fcntl.h>
  20 #include <string.h>
  21 #include <strings.h>
  22 #include <limits.h>
  23 #include <alloca.h>
  24 #include <errno.h>
  25 #include <libnvpair.h>
  26 #include <sys/types.h>
  27 #include <sys/stat.h>
  28 #include <sys/param.h>
  29 #include <sys/fm/protocol.h>
  30 #include <fm/libtopo.h>
  31 #include <fm/topo_mod.h>
  32 
  33 #include "sys/scsi/adapters/mpt_sas/mptsas_ioctl.h"
  34 
  35 #define TOPO_METH_MPTSAS_LED_MODE_VERSION       0
  36 
  37 static int fac_prov_mptsas_enum(topo_mod_t *, tnode_t *, const char *,
  38     topo_instance_t, topo_instance_t, void *, void *);
  39 
  40 /*
  41  * mpt_sas facility provider methods
  42  */
  43 static int mptsas_led_mode(topo_mod_t *, tnode_t *, topo_version_t,
  44     nvlist_t *, nvlist_t **);
  45 
  46 const topo_modops_t mptsas_ops = { fac_prov_mptsas_enum, NULL };
  47 
  48 const topo_modinfo_t mptsas_info =
  49         { "mpt_sas facility provider", FM_FMRI_SCHEME_HC, TOPO_VERSION,
  50         &mptsas_ops };
  51 
  52 static const topo_method_t mptsas_fac_methods[] = {
  53         { "mptsas_led_mode", TOPO_PROP_METH_DESC,
  54             TOPO_METH_MPTSAS_LED_MODE_VERSION,
  55             TOPO_STABILITY_INTERNAL, mptsas_led_mode },
  56         { NULL }
  57 };
  58 
  59 /*ARGSUSED*/
  60 int
  61 _topo_init(topo_mod_t *mod, topo_version_t version)
  62 {
  63         if (getenv("TOPOFACMPTSASDEBUG") != NULL)
  64                 topo_mod_setdebug(mod);
  65 
  66         return (topo_mod_register(mod, &mptsas_info, TOPO_VERSION));
  67 }
  68 
  69 void
  70 _topo_fini(topo_mod_t *mod)
  71 {
  72         topo_mod_unregister(mod);
  73 }
  74 
  75 /*
  76  * Get or set LED state for a particular target attached to an mpt_sas
  77  * instance at (Enclosure Number, Slot Number).
  78  *
  79  * Returns:
  80  *   -2  /devices node (*devctl) does not exist
  81  *   -1  All other failures
  82  *    0  Success
  83  */
  84 static int
  85 do_led_control(topo_mod_t *mod, char *devctl, uint16_t enclosure,
  86     uint16_t slot, uint8_t led, uint32_t *ledmode, boolean_t set)
  87 {
  88         int fd;
  89         mptsas_led_control_t lc;
  90 
  91         bzero(&lc, sizeof (lc));
  92 
  93         lc.Command = set ? MPTSAS_LEDCTL_FLAG_SET : MPTSAS_LEDCTL_FLAG_GET;
  94         lc.Enclosure = enclosure;
  95         lc.Slot = slot;
  96         lc.Led = led;
  97         lc.LedStatus = *ledmode;
  98 
  99         if ((fd = open(devctl, (set ? O_RDWR : O_RDONLY))) == -1) {
 100                 int rc = (errno == ENOENT ? -2 : -1);
 101                 topo_mod_dprintf(mod, "devctl open failed: %s",
 102                     strerror(errno));
 103                 return (rc);
 104         }
 105 
 106         if (ioctl(fd, MPTIOCTL_LED_CONTROL, &lc) == -1) {
 107                 if (errno == ENOENT) {
 108                         /*
 109                          * If there is not presently a target attached for
 110                          * a particular enclosure/slot pair then the driver
 111                          * does not track LED status for this bay.  Assume
 112                          * all LEDs are off.
 113                          */
 114                         lc.LedStatus = 0;
 115                 } else {
 116                         topo_mod_dprintf(mod, "led control ioctl failed: %s",
 117                             strerror(errno));
 118                         (void) close(fd);
 119                         return (-1);
 120                 }
 121         }
 122 
 123         *ledmode = lc.LedStatus ? TOPO_LED_STATE_ON : TOPO_LED_STATE_OFF;
 124 
 125         (void) close(fd);
 126         return (0);
 127 }
 128 
 129 static int
 130 mptsas_led_mode(topo_mod_t *mod, tnode_t *node, topo_version_t vers,
 131     nvlist_t *in, nvlist_t **nvout)
 132 {
 133         int err, ret = 0;
 134         tnode_t *pnode = topo_node_parent(node);
 135         uint32_t type, ledmode = 0;
 136         nvlist_t *pargs, *nvl;
 137         char *driver = NULL, *devctl = NULL;
 138         uint32_t enclosure, slot;
 139         uint8_t mptsas_led;
 140         boolean_t set;
 141         char *elem, *lastp;
 142 
 143         if (vers > TOPO_METH_MPTSAS_LED_MODE_VERSION)
 144                 return (topo_mod_seterrno(mod, ETOPO_METHOD_VERNEW));
 145 
 146         if (topo_prop_get_string(pnode, TOPO_PGROUP_BINDING,
 147             TOPO_BINDING_DRIVER, &driver, &err) != 0 ||
 148             strcmp("mpt_sas", driver) != 0) {
 149                 topo_mod_dprintf(mod, "%s: Facility driver was not mpt_sas",
 150                     __func__);
 151                 ret = topo_mod_seterrno(mod, EMOD_NVL_INVAL);
 152                 goto out;
 153         }
 154         if (topo_prop_get_uint32(node, TOPO_PGROUP_FACILITY, TOPO_FACILITY_TYPE,
 155             &type, &err) != 0) {
 156                 topo_mod_dprintf(mod, "%s: Failed to lookup %s property "
 157                     "(%s)", __func__, TOPO_FACILITY_TYPE, topo_strerror(err));
 158                 return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
 159         }
 160         switch (type) {
 161         case (TOPO_LED_TYPE_SERVICE):
 162                 mptsas_led = MPTSAS_LEDCTL_LED_FAIL;
 163                 break;
 164         case (TOPO_LED_TYPE_LOCATE):
 165                 mptsas_led = MPTSAS_LEDCTL_LED_IDENT;
 166                 break;
 167         case (TOPO_LED_TYPE_OK2RM):
 168                 mptsas_led = MPTSAS_LEDCTL_LED_OK2RM;
 169                 break;
 170         default:
 171                 topo_mod_dprintf(mod, "%s: Invalid LED type: 0x%x\n", __func__,
 172                     type);
 173                 return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
 174         }
 175         if (topo_prop_get_string(pnode, TOPO_PGROUP_BINDING,
 176             TOPO_BINDING_DEVCTL, &devctl, &err) != 0 ||
 177             topo_prop_get_uint32(pnode, TOPO_PGROUP_BINDING,
 178             TOPO_BINDING_ENCLOSURE, &enclosure, &err) != 0 ||
 179             topo_prop_get_uint32(pnode, TOPO_PGROUP_BINDING,
 180             TOPO_BINDING_SLOT, &slot, &err) != 0) {
 181                 topo_mod_dprintf(mod, "%s: Facility was missing mpt_sas binding"
 182                     " properties\n", __func__);
 183                 ret = topo_mod_seterrno(mod, EMOD_NVL_INVAL);
 184                 goto out;
 185         }
 186 
 187         if ((nvlist_lookup_nvlist(in, TOPO_PROP_PARGS, &pargs) == 0) &&
 188             nvlist_exists(pargs, TOPO_PROP_VAL_VAL)) {
 189                 /*
 190                  * Set the LED mode
 191                  */
 192                 set = B_TRUE;
 193                 if ((ret = nvlist_lookup_uint32(pargs, TOPO_PROP_VAL_VAL,
 194                     &ledmode)) != 0) {
 195                         topo_mod_dprintf(mod, "%s: Failed to lookup %s nvpair "
 196                             "(%s)\n", __func__, TOPO_PROP_VAL_VAL,
 197                             strerror(ret));
 198                         ret = topo_mod_seterrno(mod, EMOD_NVL_INVAL);
 199                         goto out;
 200                 }
 201                 topo_mod_dprintf(mod, "%s: Setting LED mode to %s\n", __func__,
 202                     ledmode ? "ON" : "OFF");
 203         } else {
 204                 /*
 205                  * Get the LED mode
 206                  */
 207                 set = B_FALSE;
 208                 topo_mod_dprintf(mod, "%s: Getting LED mode\n", __func__);
 209         }
 210 
 211         /*
 212          * devctl is a (potentially) pipe-separated list of different device
 213          * paths to try.
 214          */
 215         if ((elem = topo_mod_strsplit(mod, devctl, "|", &lastp)) == NULL) {
 216                 topo_mod_dprintf(mod, "%s: could not parse devctl list",
 217                     __func__);
 218                 ret = topo_mod_seterrno(mod, EMOD_UNKNOWN);
 219                 goto out;
 220         }
 221         do {
 222                 topo_mod_dprintf(mod, "%s: trying mpt_sas instance at %s\n",
 223                     __func__, elem);
 224 
 225                 ret = do_led_control(mod, elem, enclosure, slot,
 226                     mptsas_led, &ledmode, set);
 227 
 228                 topo_mod_strfree(mod, elem);
 229 
 230                 /*
 231                  * Only try further devctl paths from the list if this one
 232                  * was not found:
 233                  */
 234                 if (ret != -2) {
 235                         break;
 236                 } else {
 237                         topo_mod_dprintf(mod, "%s: instance not found\n",
 238                             __func__);
 239                 }
 240 
 241         } while ((elem = topo_mod_strsplit(mod, NULL, "|", &lastp)) != NULL);
 242 
 243         if (ret != 0) {
 244                 topo_mod_dprintf(mod, "%s: do_led_control failed", __func__);
 245                 ret = topo_mod_seterrno(mod, EMOD_UNKNOWN);
 246                 goto out;
 247         }
 248 
 249         if (topo_mod_nvalloc(mod, &nvl, NV_UNIQUE_NAME) != 0 ||
 250             nvlist_add_string(nvl, TOPO_PROP_VAL_NAME, TOPO_LED_MODE) != 0 ||
 251             nvlist_add_uint32(nvl, TOPO_PROP_VAL_TYPE, TOPO_TYPE_UINT32) != 0 ||
 252             nvlist_add_uint32(nvl, TOPO_PROP_VAL_VAL, ledmode) != 0) {
 253                 topo_mod_dprintf(mod, "%s: Failed to allocate 'out' nvlist\n",
 254                     __func__);
 255                 nvlist_free(nvl);
 256                 ret = topo_mod_seterrno(mod, EMOD_NOMEM);
 257                 goto out;
 258         }
 259         *nvout = nvl;
 260 
 261 out:
 262         if (driver != NULL)
 263                 topo_mod_strfree(mod, driver);
 264         if (devctl != NULL)
 265                 topo_mod_strfree(mod, devctl);
 266         return (ret);
 267 }
 268 
 269 /*ARGSUSED*/
 270 static int
 271 fac_prov_mptsas_enum(topo_mod_t *mod, tnode_t *rnode, const char *name,
 272     topo_instance_t min, topo_instance_t max, void *arg, void *unused)
 273 {
 274         if (topo_node_flags(rnode) == TOPO_NODE_FACILITY) {
 275                 if (topo_method_register(mod, rnode, mptsas_fac_methods) != 0) {
 276                         topo_mod_dprintf(mod, "%s: topo_method_register() "
 277                             "failed: %s", __func__, topo_mod_errmsg(mod));
 278                         return (-1);
 279                 }
 280                 return (0);
 281         }
 282 
 283         topo_mod_dprintf(mod, "%s: unexpected node flags %x", __func__,
 284             topo_node_flags(rnode));
 285         return (-1);
 286 }