The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/cam/scsi/scsi_ses.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*
    2  * Copyright (c) 2000 Matthew Jacob
    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
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions, and the following disclaimer,
   10  *    without modification, immediately at the beginning of the file.
   11  * 2. The name of the author may not be used to endorse or promote products
   12  *    derived from this software without specific prior written permission.
   13  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
   18  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24  * SUCH DAMAGE.
   25  */
   26 
   27 #include <sys/cdefs.h>
   28 __FBSDID("$FreeBSD: releng/5.2/sys/cam/scsi/scsi_ses.c 116162 2003-06-10 18:14:05Z obrien $");
   29 
   30 #include <sys/param.h>
   31 #include <sys/queue.h>
   32 #include <sys/systm.h>
   33 #include <sys/kernel.h>
   34 #include <sys/types.h>
   35 #include <sys/malloc.h>
   36 #include <sys/fcntl.h>
   37 #include <sys/conf.h>
   38 #include <sys/errno.h>
   39 #include <machine/stdarg.h>
   40 
   41 #include <cam/cam.h>
   42 #include <cam/cam_ccb.h>
   43 #include <cam/cam_periph.h>
   44 #include <cam/cam_xpt_periph.h>
   45 #include <cam/cam_debug.h>
   46 
   47 #include <cam/scsi/scsi_all.h>
   48 #include <cam/scsi/scsi_message.h>
   49 #include <sys/ioccom.h>
   50 #include <cam/scsi/scsi_ses.h>
   51 
   52 #include <opt_ses.h>
   53 
   54 /*
   55  * Platform Independent Driver Internal Definitions for SES devices.
   56  */
   57 typedef enum {
   58         SES_NONE,
   59         SES_SES_SCSI2,
   60         SES_SES,
   61         SES_SES_PASSTHROUGH,
   62         SES_SEN,
   63         SES_SAFT
   64 } enctyp;
   65 
   66 struct ses_softc;
   67 typedef struct ses_softc ses_softc_t;
   68 typedef struct {
   69         int (*softc_init)(ses_softc_t *, int);
   70         int (*init_enc)(ses_softc_t *);
   71         int (*get_encstat)(ses_softc_t *, int);
   72         int (*set_encstat)(ses_softc_t *, ses_encstat, int);
   73         int (*get_objstat)(ses_softc_t *, ses_objstat *, int);
   74         int (*set_objstat)(ses_softc_t *, ses_objstat *, int);
   75 } encvec;
   76 
   77 #define ENCI_SVALID     0x80
   78 
   79 typedef struct {
   80         uint32_t
   81                 enctype : 8,            /* enclosure type */
   82                 subenclosure : 8,       /* subenclosure id */
   83                 svalid  : 1,            /* enclosure information valid */
   84                 priv    : 15;           /* private data, per object */
   85         uint8_t encstat[4];     /* state && stats */
   86 } encobj;
   87 
   88 #define SEN_ID          "UNISYS           SUN_SEN"
   89 #define SEN_ID_LEN      24
   90 
   91 
   92 static enctyp ses_type(void *, int);
   93 
   94 
   95 /* Forward reference to Enclosure Functions */
   96 static int ses_softc_init(ses_softc_t *, int);
   97 static int ses_init_enc(ses_softc_t *);
   98 static int ses_get_encstat(ses_softc_t *, int);
   99 static int ses_set_encstat(ses_softc_t *, uint8_t, int);
  100 static int ses_get_objstat(ses_softc_t *, ses_objstat *, int);
  101 static int ses_set_objstat(ses_softc_t *, ses_objstat *, int);
  102 
  103 static int safte_softc_init(ses_softc_t *, int);
  104 static int safte_init_enc(ses_softc_t *);
  105 static int safte_get_encstat(ses_softc_t *, int);
  106 static int safte_set_encstat(ses_softc_t *, uint8_t, int);
  107 static int safte_get_objstat(ses_softc_t *, ses_objstat *, int);
  108 static int safte_set_objstat(ses_softc_t *, ses_objstat *, int);
  109 
  110 /*
  111  * Platform implementation defines/functions for SES internal kernel stuff
  112  */
  113 
  114 #define STRNCMP                 strncmp
  115 #define PRINTF                  printf
  116 #define SES_LOG                 ses_log
  117 #ifdef  DEBUG
  118 #define SES_DLOG                ses_log
  119 #else
  120 #define SES_DLOG                if (0) ses_log
  121 #endif
  122 #define SES_VLOG                if (bootverbose) ses_log
  123 #define SES_MALLOC(amt)         malloc(amt, M_DEVBUF, M_NOWAIT)
  124 #define SES_FREE(ptr, amt)      free(ptr, M_DEVBUF)
  125 #define MEMZERO                 bzero
  126 #define MEMCPY(dest, src, amt)  bcopy(src, dest, amt)
  127 
  128 static int ses_runcmd(struct ses_softc *, char *, int, char *, int *);
  129 static void ses_log(struct ses_softc *, const char *, ...);
  130 
  131 /*
  132  * Gerenal FreeBSD kernel stuff.
  133  */
  134 
  135 
  136 #define ccb_state       ppriv_field0
  137 #define ccb_bp          ppriv_ptr1
  138 
  139 struct ses_softc {
  140         enctyp          ses_type;       /* type of enclosure */
  141         encvec          ses_vec;        /* vector to handlers */
  142         void *          ses_private;    /* per-type private data */
  143         encobj *        ses_objmap;     /* objects */
  144         u_int32_t       ses_nobjects;   /* number of objects */
  145         ses_encstat     ses_encstat;    /* overall status */
  146         u_int8_t        ses_flags;
  147         union ccb       ses_saved_ccb;
  148         dev_t           ses_dev;
  149         struct cam_periph *periph;
  150 };
  151 #define SES_FLAG_INVALID        0x01
  152 #define SES_FLAG_OPEN           0x02
  153 #define SES_FLAG_INITIALIZED    0x04
  154 
  155 #define SESUNIT(x)       (minor((x)))
  156 #define SES_CDEV_MAJOR  110
  157 
  158 static  d_open_t        sesopen;
  159 static  d_close_t       sesclose;
  160 static  d_ioctl_t       sesioctl;
  161 static  periph_init_t   sesinit;
  162 static  periph_ctor_t   sesregister;
  163 static  periph_oninv_t  sesoninvalidate;
  164 static  periph_dtor_t   sescleanup;
  165 static  periph_start_t  sesstart;
  166 
  167 static void sesasync(void *, u_int32_t, struct cam_path *, void *);
  168 static void sesdone(struct cam_periph *, union ccb *);
  169 static int seserror(union ccb *, u_int32_t, u_int32_t);
  170 
  171 static struct periph_driver sesdriver = {
  172         sesinit, "ses",
  173         TAILQ_HEAD_INITIALIZER(sesdriver.units), /* generation */ 0
  174 };
  175 
  176 PERIPHDRIVER_DECLARE(ses, sesdriver);
  177 
  178 static struct cdevsw ses_cdevsw = 
  179 {
  180         .d_open =       sesopen,
  181         .d_close =      sesclose,
  182         .d_ioctl =      sesioctl,
  183         .d_name =       "ses",
  184         .d_maj =        SES_CDEV_MAJOR,
  185 };
  186 
  187 static void
  188 sesinit(void)
  189 {
  190         cam_status status;
  191         struct cam_path *path;
  192 
  193         /*
  194          * Install a global async callback.  This callback will
  195          * receive async callbacks like "new device found".
  196          */
  197         status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
  198             CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
  199 
  200         if (status == CAM_REQ_CMP) {
  201                 struct ccb_setasync csa;
  202 
  203                 xpt_setup_ccb(&csa.ccb_h, path, 5);
  204                 csa.ccb_h.func_code = XPT_SASYNC_CB;
  205                 csa.event_enable = AC_FOUND_DEVICE;
  206                 csa.callback = sesasync;
  207                 csa.callback_arg = NULL;
  208                 xpt_action((union ccb *)&csa);
  209                 status = csa.ccb_h.status;
  210                 xpt_free_path(path);
  211         }
  212 
  213         if (status != CAM_REQ_CMP) {
  214                 printf("ses: Failed to attach master async callback "
  215                        "due to status 0x%x!\n", status);
  216         }
  217 }
  218 
  219 static void
  220 sesoninvalidate(struct cam_periph *periph)
  221 {
  222         struct ses_softc *softc;
  223         struct ccb_setasync csa;
  224 
  225         softc = (struct ses_softc *)periph->softc;
  226 
  227         /*
  228          * Unregister any async callbacks.
  229          */
  230         xpt_setup_ccb(&csa.ccb_h, periph->path, 5);
  231         csa.ccb_h.func_code = XPT_SASYNC_CB;
  232         csa.event_enable = 0;
  233         csa.callback = sesasync;
  234         csa.callback_arg = periph;
  235         xpt_action((union ccb *)&csa);
  236 
  237         softc->ses_flags |= SES_FLAG_INVALID;
  238 
  239         xpt_print_path(periph->path);
  240         printf("lost device\n");
  241 }
  242 
  243 static void
  244 sescleanup(struct cam_periph *periph)
  245 {
  246         struct ses_softc *softc;
  247 
  248         softc = (struct ses_softc *)periph->softc;
  249 
  250         destroy_dev(softc->ses_dev);
  251 
  252         xpt_print_path(periph->path);
  253         printf("removing device entry\n");
  254         free(softc, M_DEVBUF);
  255 }
  256 
  257 static void
  258 sesasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
  259 {
  260         struct cam_periph *periph;
  261 
  262         periph = (struct cam_periph *)callback_arg;
  263 
  264         switch(code) {
  265         case AC_FOUND_DEVICE:
  266         {
  267                 cam_status status;
  268                 struct ccb_getdev *cgd;
  269                 int inq_len;
  270 
  271                 cgd = (struct ccb_getdev *)arg;
  272                 if (arg == NULL) {
  273                         break;
  274                 }
  275 
  276                 inq_len = cgd->inq_data.additional_length + 4;
  277 
  278                 /*
  279                  * PROBLEM: WE NEED TO LOOK AT BYTES 48-53 TO SEE IF THIS IS
  280                  * PROBLEM: IS A SAF-TE DEVICE.
  281                  */
  282                 switch (ses_type(&cgd->inq_data, inq_len)) {
  283                 case SES_SES:
  284                 case SES_SES_SCSI2:
  285                 case SES_SES_PASSTHROUGH:
  286                 case SES_SEN:
  287                 case SES_SAFT:
  288                         break;
  289                 default:
  290                         return;
  291                 }
  292 
  293                 status = cam_periph_alloc(sesregister, sesoninvalidate,
  294                     sescleanup, sesstart, "ses", CAM_PERIPH_BIO,
  295                     cgd->ccb_h.path, sesasync, AC_FOUND_DEVICE, cgd);
  296 
  297                 if (status != CAM_REQ_CMP && status != CAM_REQ_INPROG) {
  298                         printf("sesasync: Unable to probe new device due to "
  299                             "status 0x%x\n", status);
  300                 }
  301                 break;
  302         }
  303         default:
  304                 cam_periph_async(periph, code, path, arg);
  305                 break;
  306         }
  307 }
  308 
  309 static cam_status
  310 sesregister(struct cam_periph *periph, void *arg)
  311 {
  312         struct ses_softc *softc;
  313         struct ccb_setasync csa;
  314         struct ccb_getdev *cgd;
  315         char *tname;
  316 
  317         cgd = (struct ccb_getdev *)arg;
  318         if (periph == NULL) {
  319                 printf("sesregister: periph was NULL!!\n");
  320                 return (CAM_REQ_CMP_ERR);
  321         }
  322 
  323         if (cgd == NULL) {
  324                 printf("sesregister: no getdev CCB, can't register device\n");
  325                 return (CAM_REQ_CMP_ERR);
  326         }
  327 
  328         softc = malloc(sizeof (struct ses_softc), M_DEVBUF, M_NOWAIT);
  329         if (softc == NULL) {
  330                 printf("sesregister: Unable to probe new device. "
  331                        "Unable to allocate softc\n");                           
  332                 return (CAM_REQ_CMP_ERR);
  333         }
  334         bzero(softc, sizeof (struct ses_softc));
  335         periph->softc = softc;
  336         softc->periph = periph;
  337 
  338         softc->ses_type = ses_type(&cgd->inq_data, sizeof (cgd->inq_data));
  339 
  340         switch (softc->ses_type) {
  341         case SES_SES:
  342         case SES_SES_SCSI2:
  343         case SES_SES_PASSTHROUGH:
  344                 softc->ses_vec.softc_init = ses_softc_init;
  345                 softc->ses_vec.init_enc = ses_init_enc;
  346                 softc->ses_vec.get_encstat = ses_get_encstat;
  347                 softc->ses_vec.set_encstat = ses_set_encstat;
  348                 softc->ses_vec.get_objstat = ses_get_objstat;
  349                 softc->ses_vec.set_objstat = ses_set_objstat;
  350                 break;
  351         case SES_SAFT:
  352                 softc->ses_vec.softc_init = safte_softc_init;
  353                 softc->ses_vec.init_enc = safte_init_enc;
  354                 softc->ses_vec.get_encstat = safte_get_encstat;
  355                 softc->ses_vec.set_encstat = safte_set_encstat;
  356                 softc->ses_vec.get_objstat = safte_get_objstat;
  357                 softc->ses_vec.set_objstat = safte_set_objstat;
  358                 break;
  359         case SES_SEN:
  360                 break;
  361         case SES_NONE:
  362         default:
  363                 free(softc, M_DEVBUF);
  364                 return (CAM_REQ_CMP_ERR);
  365         }
  366 
  367         softc->ses_dev = make_dev(&ses_cdevsw, periph->unit_number,
  368             UID_ROOT, GID_OPERATOR, 0600, "%s%d",
  369             periph->periph_name, periph->unit_number);
  370         softc->ses_dev->si_drv1 = periph;
  371 
  372         /*
  373          * Add an async callback so that we get
  374          * notified if this device goes away.
  375          */
  376         xpt_setup_ccb(&csa.ccb_h, periph->path, 5);
  377         csa.ccb_h.func_code = XPT_SASYNC_CB;
  378         csa.event_enable = AC_LOST_DEVICE;
  379         csa.callback = sesasync;
  380         csa.callback_arg = periph;
  381         xpt_action((union ccb *)&csa);
  382 
  383         switch (softc->ses_type) {
  384         default:
  385         case SES_NONE:
  386                 tname = "No SES device";
  387                 break;
  388         case SES_SES_SCSI2:
  389                 tname = "SCSI-2 SES Device";
  390                 break;
  391         case SES_SES:
  392                 tname = "SCSI-3 SES Device";
  393                 break;
  394         case SES_SES_PASSTHROUGH:
  395                 tname = "SES Passthrough Device";
  396                 break;
  397         case SES_SEN:
  398                 tname = "UNISYS SEN Device (NOT HANDLED YET)";
  399                 break;
  400         case SES_SAFT:
  401                 tname = "SAF-TE Compliant Device";
  402                 break;
  403         }
  404         xpt_announce_periph(periph, tname);
  405         return (CAM_REQ_CMP);
  406 }
  407 
  408 static int
  409 sesopen(dev_t dev, int flags, int fmt, struct thread *td)
  410 {
  411         struct cam_periph *periph;
  412         struct ses_softc *softc;
  413         int error, s;
  414 
  415         s = splsoftcam();
  416         periph = (struct cam_periph *)dev->si_drv1;
  417         if (periph == NULL) {
  418                 splx(s);
  419                 return (ENXIO);
  420         }
  421         if ((error = cam_periph_lock(periph, PRIBIO | PCATCH)) != 0) {
  422                 splx(s);
  423                 return (error);
  424         }
  425         splx(s);
  426 
  427         if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
  428                 cam_periph_unlock(periph);
  429                 return (ENXIO);
  430         }
  431 
  432         softc = (struct ses_softc *)periph->softc;
  433 
  434         if (softc->ses_flags & SES_FLAG_INVALID) {
  435                 error = ENXIO;
  436                 goto out;
  437         }
  438         if (softc->ses_flags & SES_FLAG_OPEN) {
  439                 error = EBUSY;
  440                 goto out;
  441         }
  442         if (softc->ses_vec.softc_init == NULL) {
  443                 error = ENXIO;
  444                 goto out;
  445         }
  446 
  447         softc->ses_flags |= SES_FLAG_OPEN;
  448         if ((softc->ses_flags & SES_FLAG_INITIALIZED) == 0) {
  449                 error = (*softc->ses_vec.softc_init)(softc, 1);
  450                 if (error)
  451                         softc->ses_flags &= ~SES_FLAG_OPEN;
  452                 else
  453                         softc->ses_flags |= SES_FLAG_INITIALIZED;
  454         }
  455 
  456 out:
  457         if (error) {
  458                 cam_periph_release(periph);
  459         }
  460         cam_periph_unlock(periph);
  461         return (error);
  462 }
  463 
  464 static int
  465 sesclose(dev_t dev, int flag, int fmt, struct thread *td)
  466 {
  467         struct cam_periph *periph;
  468         struct ses_softc *softc;
  469         int error;
  470 
  471         error = 0;
  472 
  473         periph = (struct cam_periph *)dev->si_drv1;
  474         if (periph == NULL)
  475                 return (ENXIO);
  476 
  477         softc = (struct ses_softc *)periph->softc;
  478 
  479         if ((error = cam_periph_lock(periph, PRIBIO)) != 0)
  480                 return (error);
  481 
  482         softc->ses_flags &= ~SES_FLAG_OPEN;
  483 
  484         cam_periph_unlock(periph);
  485         cam_periph_release(periph);
  486 
  487         return (0);
  488 }
  489 
  490 static void
  491 sesstart(struct cam_periph *p, union ccb *sccb)
  492 {
  493         int s = splbio();
  494         if (p->immediate_priority <= p->pinfo.priority) {
  495                 SLIST_INSERT_HEAD(&p->ccb_list, &sccb->ccb_h, periph_links.sle);
  496                 p->immediate_priority = CAM_PRIORITY_NONE;
  497                 wakeup(&p->ccb_list);
  498         }
  499         splx(s);
  500 }
  501 
  502 static void
  503 sesdone(struct cam_periph *periph, union ccb *dccb)
  504 {
  505         wakeup(&dccb->ccb_h.cbfcnp);
  506 }
  507 
  508 static int
  509 seserror(union ccb *ccb, u_int32_t cflags, u_int32_t sflags)
  510 {
  511         struct ses_softc *softc;
  512         struct cam_periph *periph;
  513 
  514         periph = xpt_path_periph(ccb->ccb_h.path);
  515         softc = (struct ses_softc *)periph->softc;
  516 
  517         return (cam_periph_error(ccb, cflags, sflags, &softc->ses_saved_ccb));
  518 }
  519 
  520 static int
  521 sesioctl(dev_t dev, u_long cmd, caddr_t arg_addr, int flag, struct thread *td)
  522 {
  523         struct cam_periph *periph;
  524         ses_encstat tmp;
  525         ses_objstat objs;
  526         ses_object obj, *uobj;
  527         struct ses_softc *ssc;
  528         void *addr;
  529         int error, i;
  530 
  531 
  532         if (arg_addr)
  533                 addr = *((caddr_t *) arg_addr);
  534         else
  535                 addr = NULL;
  536 
  537         periph = (struct cam_periph *)dev->si_drv1;
  538         if (periph == NULL)
  539                 return (ENXIO);
  540 
  541         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering sesioctl\n"));
  542 
  543         ssc = (struct ses_softc *)periph->softc;
  544 
  545         /*
  546          * Now check to see whether we're initialized or not.
  547          */
  548         if ((ssc->ses_flags & SES_FLAG_INITIALIZED) == 0) {
  549                 return (ENXIO);
  550         }
  551 
  552         error = 0;
  553 
  554         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
  555             ("trying to do ioctl %#lx\n", cmd));
  556 
  557         /*
  558          * If this command can change the device's state,
  559          * we must have the device open for writing.
  560          */
  561         switch (cmd) {
  562         case SESIOC_GETNOBJ:
  563         case SESIOC_GETOBJMAP:
  564         case SESIOC_GETENCSTAT:
  565         case SESIOC_GETOBJSTAT:
  566                 break;
  567         default:
  568                 if ((flag & FWRITE) == 0) {
  569                         return (EBADF);
  570                 }
  571         }
  572 
  573         switch (cmd) {
  574         case SESIOC_GETNOBJ:
  575                 error = copyout(&ssc->ses_nobjects, addr,
  576                     sizeof (ssc->ses_nobjects));
  577                 break;
  578                 
  579         case SESIOC_GETOBJMAP:
  580                 for (uobj = addr, i = 0; i != ssc->ses_nobjects; i++, uobj++) {
  581                         obj.obj_id = i;
  582                         obj.subencid = ssc->ses_objmap[i].subenclosure;
  583                         obj.object_type = ssc->ses_objmap[i].enctype;
  584                         error = copyout(&obj, uobj, sizeof (ses_object));
  585                         if (error) {
  586                                 break;
  587                         }
  588                 }
  589                 break;
  590 
  591         case SESIOC_GETENCSTAT:
  592                 error = (*ssc->ses_vec.get_encstat)(ssc, 1);
  593                 if (error)
  594                         break;
  595                 tmp = ssc->ses_encstat & ~ENCI_SVALID;
  596                 error = copyout(&tmp, addr, sizeof (ses_encstat));
  597                 ssc->ses_encstat = tmp;
  598                 break;
  599 
  600         case SESIOC_SETENCSTAT:
  601                 error = copyin(addr, &tmp, sizeof (ses_encstat));
  602                 if (error)
  603                         break;
  604                 error = (*ssc->ses_vec.set_encstat)(ssc, tmp, 1);
  605                 break;
  606 
  607         case SESIOC_GETOBJSTAT:
  608                 error = copyin(addr, &objs, sizeof (ses_objstat));
  609                 if (error)
  610                         break;
  611                 if (objs.obj_id >= ssc->ses_nobjects) {
  612                         error = EINVAL;
  613                         break;
  614                 }
  615                 error = (*ssc->ses_vec.get_objstat)(ssc, &objs, 1);
  616                 if (error)
  617                         break;
  618                 error = copyout(&objs, addr, sizeof (ses_objstat));
  619                 /*
  620                  * Always (for now) invalidate entry.
  621                  */
  622                 ssc->ses_objmap[objs.obj_id].svalid = 0;
  623                 break;
  624 
  625         case SESIOC_SETOBJSTAT:
  626                 error = copyin(addr, &objs, sizeof (ses_objstat));
  627                 if (error)
  628                         break;
  629 
  630                 if (objs.obj_id >= ssc->ses_nobjects) {
  631                         error = EINVAL;
  632                         break;
  633                 }
  634                 error = (*ssc->ses_vec.set_objstat)(ssc, &objs, 1);
  635 
  636                 /*
  637                  * Always (for now) invalidate entry.
  638                  */
  639                 ssc->ses_objmap[objs.obj_id].svalid = 0;
  640                 break;
  641 
  642         case SESIOC_INIT:
  643 
  644                 error = (*ssc->ses_vec.init_enc)(ssc);
  645                 break;
  646 
  647         default:
  648                 error = cam_periph_ioctl(periph, cmd, arg_addr, seserror);
  649                 break;
  650         }
  651         return (error);
  652 }
  653 
  654 #define SES_CFLAGS      CAM_RETRY_SELTO
  655 #define SES_FLAGS       SF_NO_PRINT | SF_RETRY_UA
  656 static int
  657 ses_runcmd(struct ses_softc *ssc, char *cdb, int cdbl, char *dptr, int *dlenp)
  658 {
  659         int error, dlen;
  660         ccb_flags ddf;
  661         union ccb *ccb;
  662 
  663         if (dptr) {
  664                 if ((dlen = *dlenp) < 0) {
  665                         dlen = -dlen;
  666                         ddf = CAM_DIR_OUT;
  667                 } else {
  668                         ddf = CAM_DIR_IN;
  669                 }
  670         } else {
  671                 dlen = 0;
  672                 ddf = CAM_DIR_NONE;
  673         }
  674 
  675         if (cdbl > IOCDBLEN) {
  676                 cdbl = IOCDBLEN;
  677         }
  678 
  679         ccb = cam_periph_getccb(ssc->periph, 1);
  680         cam_fill_csio(&ccb->csio, 0, sesdone, ddf, MSG_SIMPLE_Q_TAG, dptr,
  681             dlen, sizeof (struct scsi_sense_data), cdbl, 60 * 1000);
  682         bcopy(cdb, ccb->csio.cdb_io.cdb_bytes, cdbl);
  683 
  684         error = cam_periph_runccb(ccb, seserror, SES_CFLAGS, SES_FLAGS, NULL);
  685         if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
  686                 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
  687         if (error) {
  688                 if (dptr) {
  689                         *dlenp = dlen;
  690                 }
  691         } else {
  692                 if (dptr) {
  693                         *dlenp = ccb->csio.resid;
  694                 }
  695         }
  696         xpt_release_ccb(ccb);
  697         return (error);
  698 }
  699 
  700 static void
  701 ses_log(struct ses_softc *ssc, const char *fmt, ...)
  702 {
  703         va_list ap;
  704 
  705         printf("%s%d: ", ssc->periph->periph_name, ssc->periph->unit_number);
  706         va_start(ap, fmt);
  707         vprintf(fmt, ap);
  708         va_end(ap);
  709 }
  710 
  711 /*
  712  * The code after this point runs on many platforms,
  713  * so forgive the slightly awkward and nonconforming
  714  * appearance.
  715  */
  716 
  717 /*
  718  * Is this a device that supports enclosure services?
  719  *
  720  * It's a a pretty simple ruleset- if it is device type 0x0D (13), it's
  721  * an SES device. If it happens to be an old UNISYS SEN device, we can
  722  * handle that too.
  723  */
  724 
  725 #define SAFTE_START     44
  726 #define SAFTE_END       50
  727 #define SAFTE_LEN       SAFTE_END-SAFTE_START
  728 
  729 static enctyp
  730 ses_type(void *buf, int buflen)
  731 {
  732         unsigned char *iqd = buf;
  733 
  734         if (buflen < 8+SEN_ID_LEN)
  735                 return (SES_NONE);
  736 
  737         if ((iqd[0] & 0x1f) == T_ENCLOSURE) {
  738                 if (STRNCMP(&iqd[8], SEN_ID, SEN_ID_LEN) == 0) {
  739                         return (SES_SEN);
  740                 } else if ((iqd[2] & 0x7) > 2) {
  741                         return (SES_SES);
  742                 } else {
  743                         return (SES_SES_SCSI2);
  744                 }
  745                 return (SES_NONE);
  746         }
  747 
  748 #ifdef  SES_ENABLE_PASSTHROUGH
  749         if ((iqd[6] & 0x40) && (iqd[2] & 0x7) >= 2) {
  750                 /*
  751                  * PassThrough Device.
  752                  */
  753                 return (SES_SES_PASSTHROUGH);
  754         }
  755 #endif
  756 
  757         /*
  758          * The comparison is short for a reason-
  759          * some vendors were chopping it short.
  760          */
  761 
  762         if (buflen < SAFTE_END - 2) {
  763                 return (SES_NONE);
  764         }
  765 
  766         if (STRNCMP((char *)&iqd[SAFTE_START], "SAF-TE", SAFTE_LEN - 2) == 0) {
  767                 return (SES_SAFT);
  768         }
  769         return (SES_NONE);
  770 }
  771 
  772 /*
  773  * SES Native Type Device Support
  774  */
  775 
  776 /*
  777  * SES Diagnostic Page Codes
  778  */
  779 
  780 typedef enum {
  781         SesConfigPage = 0x1,
  782         SesControlPage,
  783 #define SesStatusPage SesControlPage
  784         SesHelpTxt,
  785         SesStringOut,
  786 #define SesStringIn     SesStringOut
  787         SesThresholdOut,
  788 #define SesThresholdIn SesThresholdOut
  789         SesArrayControl,
  790 #define SesArrayStatus  SesArrayControl
  791         SesElementDescriptor,
  792         SesShortStatus
  793 } SesDiagPageCodes;
  794 
  795 /*
  796  * minimal amounts
  797  */
  798 
  799 /*
  800  * Minimum amount of data, starting from byte 0, to have
  801  * the config header.
  802  */
  803 #define SES_CFGHDR_MINLEN       12
  804 
  805 /*
  806  * Minimum amount of data, starting from byte 0, to have
  807  * the config header and one enclosure header.
  808  */
  809 #define SES_ENCHDR_MINLEN       48
  810 
  811 /*
  812  * Take this value, subtract it from VEnclen and you know
  813  * the length of the vendor unique bytes.
  814  */
  815 #define SES_ENCHDR_VMIN         36
  816 
  817 /*
  818  * SES Data Structures
  819  */
  820 
  821 typedef struct {
  822         uint32_t GenCode;       /* Generation Code */
  823         uint8_t Nsubenc;        /* Number of Subenclosures */
  824 } SesCfgHdr;
  825 
  826 typedef struct {
  827         uint8_t Subencid;       /* SubEnclosure Identifier */
  828         uint8_t Ntypes;         /* # of supported types */
  829         uint8_t VEnclen;        /* Enclosure Descriptor Length */
  830 } SesEncHdr;
  831 
  832 typedef struct {
  833         uint8_t encWWN[8];      /* XXX- Not Right Yet */
  834         uint8_t encVid[8];
  835         uint8_t encPid[16];
  836         uint8_t encRev[4];
  837         uint8_t encVen[1];
  838 } SesEncDesc;
  839 
  840 typedef struct {
  841         uint8_t enc_type;               /* type of element */
  842         uint8_t enc_maxelt;             /* maximum supported */
  843         uint8_t enc_subenc;             /* in SubEnc # N */
  844         uint8_t enc_tlen;               /* Type Descriptor Text Length */
  845 } SesThdr;
  846 
  847 typedef struct {
  848         uint8_t comstatus;
  849         uint8_t comstat[3];
  850 } SesComStat;
  851 
  852 struct typidx {
  853         int ses_tidx;
  854         int ses_oidx;
  855 };
  856 
  857 struct sscfg {
  858         uint8_t ses_ntypes;     /* total number of types supported */
  859 
  860         /*
  861          * We need to keep a type index as well as an
  862          * object index for each object in an enclosure.
  863          */
  864         struct typidx *ses_typidx;
  865 
  866         /*
  867          * We also need to keep track of the number of elements
  868          * per type of element. This is needed later so that we
  869          * can find precisely in the returned status data the
  870          * status for the Nth element of the Kth type.
  871          */
  872         uint8_t *       ses_eltmap;
  873 };
  874 
  875 
  876 /*
  877  * (de)canonicalization defines
  878  */
  879 #define sbyte(x, byte)          ((((uint32_t)(x)) >> (byte * 8)) & 0xff)
  880 #define sbit(x, bit)            (((uint32_t)(x)) << bit)
  881 #define sset8(outp, idx, sval)  (((uint8_t *)(outp))[idx++]) = sbyte(sval, 0)
  882 
  883 #define sset16(outp, idx, sval) \
  884         (((uint8_t *)(outp))[idx++]) = sbyte(sval, 1), \
  885         (((uint8_t *)(outp))[idx++]) = sbyte(sval, 0)
  886 
  887 
  888 #define sset24(outp, idx, sval) \
  889         (((uint8_t *)(outp))[idx++]) = sbyte(sval, 2), \
  890         (((uint8_t *)(outp))[idx++]) = sbyte(sval, 1), \
  891         (((uint8_t *)(outp))[idx++]) = sbyte(sval, 0)
  892 
  893 
  894 #define sset32(outp, idx, sval) \
  895         (((uint8_t *)(outp))[idx++]) = sbyte(sval, 3), \
  896         (((uint8_t *)(outp))[idx++]) = sbyte(sval, 2), \
  897         (((uint8_t *)(outp))[idx++]) = sbyte(sval, 1), \
  898         (((uint8_t *)(outp))[idx++]) = sbyte(sval, 0)
  899 
  900 #define gbyte(x, byte)  ((((uint32_t)(x)) & 0xff) << (byte * 8))
  901 #define gbit(lv, in, idx, shft, mask)   lv = ((in[idx] >> shft) & mask)
  902 #define sget8(inp, idx, lval)   lval = (((uint8_t *)(inp))[idx++])
  903 #define gget8(inp, idx, lval)   lval = (((uint8_t *)(inp))[idx])
  904 
  905 #define sget16(inp, idx, lval)  \
  906         lval = gbyte((((uint8_t *)(inp))[idx]), 1) | \
  907                 (((uint8_t *)(inp))[idx+1]), idx += 2
  908 
  909 #define gget16(inp, idx, lval)  \
  910         lval = gbyte((((uint8_t *)(inp))[idx]), 1) | \
  911                 (((uint8_t *)(inp))[idx+1])
  912 
  913 #define sget24(inp, idx, lval)  \
  914         lval = gbyte((((uint8_t *)(inp))[idx]), 2) | \
  915                 gbyte((((uint8_t *)(inp))[idx+1]), 1) | \
  916                         (((uint8_t *)(inp))[idx+2]), idx += 3
  917 
  918 #define gget24(inp, idx, lval)  \
  919         lval = gbyte((((uint8_t *)(inp))[idx]), 2) | \
  920                 gbyte((((uint8_t *)(inp))[idx+1]), 1) | \
  921                         (((uint8_t *)(inp))[idx+2])
  922 
  923 #define sget32(inp, idx, lval)  \
  924         lval = gbyte((((uint8_t *)(inp))[idx]), 3) | \
  925                 gbyte((((uint8_t *)(inp))[idx+1]), 2) | \
  926                 gbyte((((uint8_t *)(inp))[idx+2]), 1) | \
  927                         (((uint8_t *)(inp))[idx+3]), idx += 4
  928 
  929 #define gget32(inp, idx, lval)  \
  930         lval = gbyte((((uint8_t *)(inp))[idx]), 3) | \
  931                 gbyte((((uint8_t *)(inp))[idx+1]), 2) | \
  932                 gbyte((((uint8_t *)(inp))[idx+2]), 1) | \
  933                         (((uint8_t *)(inp))[idx+3])
  934 
  935 #define SCSZ    0x2000
  936 #define CFLEN   (256 + SES_ENCHDR_MINLEN)
  937 
  938 /*
  939  * Routines specific && private to SES only
  940  */
  941 
  942 static int ses_getconfig(ses_softc_t *);
  943 static int ses_getputstat(ses_softc_t *, int, SesComStat *, int, int);
  944 static int ses_cfghdr(uint8_t *, int, SesCfgHdr *);
  945 static int ses_enchdr(uint8_t *, int, uint8_t, SesEncHdr *);
  946 static int ses_encdesc(uint8_t *, int, uint8_t, SesEncDesc *);
  947 static int ses_getthdr(uint8_t *, int,  int, SesThdr *);
  948 static int ses_decode(char *, int, uint8_t *, int, int, SesComStat *);
  949 static int ses_encode(char *, int, uint8_t *, int, int, SesComStat *);
  950 
  951 static int
  952 ses_softc_init(ses_softc_t *ssc, int doinit)
  953 {
  954         if (doinit == 0) {
  955                 struct sscfg *cc;
  956                 if (ssc->ses_nobjects) {
  957                         SES_FREE(ssc->ses_objmap,
  958                             ssc->ses_nobjects * sizeof (encobj));
  959                         ssc->ses_objmap = NULL;
  960                 }
  961                 if ((cc = ssc->ses_private) != NULL) {
  962                         if (cc->ses_eltmap && cc->ses_ntypes) {
  963                                 SES_FREE(cc->ses_eltmap, cc->ses_ntypes);
  964                                 cc->ses_eltmap = NULL;
  965                                 cc->ses_ntypes = 0;
  966                         }
  967                         if (cc->ses_typidx && ssc->ses_nobjects) {
  968                                 SES_FREE(cc->ses_typidx,
  969                                     ssc->ses_nobjects * sizeof (struct typidx));
  970                                 cc->ses_typidx = NULL;
  971                         }
  972                         SES_FREE(cc, sizeof (struct sscfg));
  973                         ssc->ses_private = NULL;
  974                 }
  975                 ssc->ses_nobjects = 0;
  976                 return (0);
  977         }
  978         if (ssc->ses_private == NULL) {
  979                 ssc->ses_private = SES_MALLOC(sizeof (struct sscfg));
  980         }
  981         if (ssc->ses_private == NULL) {
  982                 return (ENOMEM);
  983         }
  984         ssc->ses_nobjects = 0;
  985         ssc->ses_encstat = 0;
  986         return (ses_getconfig(ssc));
  987 }
  988 
  989 static int
  990 ses_init_enc(ses_softc_t *ssc)
  991 {
  992         return (0);
  993 }
  994 
  995 static int
  996 ses_get_encstat(ses_softc_t *ssc, int slpflag)
  997 {
  998         SesComStat ComStat;
  999         int status;
 1000 
 1001         if ((status = ses_getputstat(ssc, -1, &ComStat, slpflag, 1)) != 0) {
 1002                 return (status);
 1003         }
 1004         ssc->ses_encstat = ComStat.comstatus | ENCI_SVALID;
 1005         return (0);
 1006 }
 1007 
 1008 static int
 1009 ses_set_encstat(ses_softc_t *ssc, uint8_t encstat, int slpflag)
 1010 {
 1011         SesComStat ComStat;
 1012         int status;
 1013 
 1014         ComStat.comstatus = encstat & 0xf;
 1015         if ((status = ses_getputstat(ssc, -1, &ComStat, slpflag, 0)) != 0) {
 1016                 return (status);
 1017         }
 1018         ssc->ses_encstat = encstat & 0xf;       /* note no SVALID set */
 1019         return (0);
 1020 }
 1021 
 1022 static int
 1023 ses_get_objstat(ses_softc_t *ssc, ses_objstat *obp, int slpflag)
 1024 {
 1025         int i = (int)obp->obj_id;
 1026 
 1027         if (ssc->ses_objmap[i].svalid == 0) {
 1028                 SesComStat ComStat;
 1029                 int err = ses_getputstat(ssc, i, &ComStat, slpflag, 1);
 1030                 if (err)
 1031                         return (err);
 1032                 ssc->ses_objmap[i].encstat[0] = ComStat.comstatus;
 1033                 ssc->ses_objmap[i].encstat[1] = ComStat.comstat[0];
 1034                 ssc->ses_objmap[i].encstat[2] = ComStat.comstat[1];
 1035                 ssc->ses_objmap[i].encstat[3] = ComStat.comstat[2];
 1036                 ssc->ses_objmap[i].svalid = 1;
 1037         }
 1038         obp->cstat[0] = ssc->ses_objmap[i].encstat[0];
 1039         obp->cstat[1] = ssc->ses_objmap[i].encstat[1];
 1040         obp->cstat[2] = ssc->ses_objmap[i].encstat[2];
 1041         obp->cstat[3] = ssc->ses_objmap[i].encstat[3];
 1042         return (0);
 1043 }
 1044 
 1045 static int
 1046 ses_set_objstat(ses_softc_t *ssc, ses_objstat *obp, int slpflag)
 1047 {
 1048         SesComStat ComStat;
 1049         int err;
 1050         /*
 1051          * If this is clear, we don't do diddly.
 1052          */
 1053         if ((obp->cstat[0] & SESCTL_CSEL) == 0) {
 1054                 return (0);
 1055         }
 1056         ComStat.comstatus = obp->cstat[0];
 1057         ComStat.comstat[0] = obp->cstat[1];
 1058         ComStat.comstat[1] = obp->cstat[2];
 1059         ComStat.comstat[2] = obp->cstat[3];
 1060         err = ses_getputstat(ssc, (int)obp->obj_id, &ComStat, slpflag, 0);
 1061         ssc->ses_objmap[(int)obp->obj_id].svalid = 0;
 1062         return (err);
 1063 }
 1064 
 1065 static int
 1066 ses_getconfig(ses_softc_t *ssc)
 1067 {
 1068         struct sscfg *cc;
 1069         SesCfgHdr cf;
 1070         SesEncHdr hd;
 1071         SesEncDesc *cdp;
 1072         SesThdr thdr;
 1073         int err, amt, i, nobj, ntype, maxima;
 1074         char storage[CFLEN], *sdata;
 1075         static char cdb[6] = {
 1076             RECEIVE_DIAGNOSTIC, 0x1, SesConfigPage, SCSZ >> 8, SCSZ & 0xff, 0
 1077         };
 1078 
 1079         cc = ssc->ses_private;
 1080         if (cc == NULL) {
 1081                 return (ENXIO);
 1082         }
 1083 
 1084         sdata = SES_MALLOC(SCSZ);
 1085         if (sdata == NULL)
 1086                 return (ENOMEM);
 1087 
 1088         amt = SCSZ;
 1089         err = ses_runcmd(ssc, cdb, 6, sdata, &amt);
 1090         if (err) {
 1091                 SES_FREE(sdata, SCSZ);
 1092                 return (err);
 1093         }
 1094         amt = SCSZ - amt;
 1095 
 1096         if (ses_cfghdr((uint8_t *) sdata, amt, &cf)) {
 1097                 SES_LOG(ssc, "Unable to parse SES Config Header\n");
 1098                 SES_FREE(sdata, SCSZ);
 1099                 return (EIO);
 1100         }
 1101         if (amt < SES_ENCHDR_MINLEN) {
 1102                 SES_LOG(ssc, "runt enclosure length (%d)\n", amt);
 1103                 SES_FREE(sdata, SCSZ);
 1104                 return (EIO);
 1105         }
 1106 
 1107         SES_VLOG(ssc, "GenCode %x %d Subenclosures\n", cf.GenCode, cf.Nsubenc);
 1108 
 1109         /*
 1110          * Now waltz through all the subenclosures toting up the
 1111          * number of types available in each. For this, we only
 1112          * really need the enclosure header. However, we get the
 1113          * enclosure descriptor for debug purposes, as well
 1114          * as self-consistency checking purposes.
 1115          */
 1116 
 1117         maxima = cf.Nsubenc + 1;
 1118         cdp = (SesEncDesc *) storage;
 1119         for (ntype = i = 0; i < maxima; i++) {
 1120                 MEMZERO((caddr_t)cdp, sizeof (*cdp));
 1121                 if (ses_enchdr((uint8_t *) sdata, amt, i, &hd)) {
 1122                         SES_LOG(ssc, "Cannot Extract Enclosure Header %d\n", i);
 1123                         SES_FREE(sdata, SCSZ);
 1124                         return (EIO);
 1125                 }
 1126                 SES_VLOG(ssc, " SubEnclosure ID %d, %d Types With this ID, En"
 1127                     "closure Length %d\n", hd.Subencid, hd.Ntypes, hd.VEnclen);
 1128 
 1129                 if (ses_encdesc((uint8_t *)sdata, amt, i, cdp)) {
 1130                         SES_LOG(ssc, "Can't get Enclosure Descriptor %d\n", i);
 1131                         SES_FREE(sdata, SCSZ);
 1132                         return (EIO);
 1133                 }
 1134                 SES_VLOG(ssc, " WWN: %02x%02x%02x%02x%02x%02x%02x%02x\n",
 1135                     cdp->encWWN[0], cdp->encWWN[1], cdp->encWWN[2],
 1136                     cdp->encWWN[3], cdp->encWWN[4], cdp->encWWN[5],
 1137                     cdp->encWWN[6], cdp->encWWN[7]);
 1138                 ntype += hd.Ntypes;
 1139         }
 1140 
 1141         /*
 1142          * Now waltz through all the types that are available, getting
 1143          * the type header so we can start adding up the number of
 1144          * objects available.
 1145          */
 1146         for (nobj = i = 0; i < ntype; i++) {
 1147                 if (ses_getthdr((uint8_t *)sdata, amt, i, &thdr)) {
 1148                         SES_LOG(ssc, "Can't get Enclosure Type Header %d\n", i);
 1149                         SES_FREE(sdata, SCSZ);
 1150                         return (EIO);
 1151                 }
 1152                 SES_LOG(ssc, " Type Desc[%d]: Type 0x%x, MaxElt %d, In Subenc "
 1153                     "%d, Text Length %d\n", i, thdr.enc_type, thdr.enc_maxelt,
 1154                     thdr.enc_subenc, thdr.enc_tlen);
 1155                 nobj += thdr.enc_maxelt;
 1156         }
 1157 
 1158 
 1159         /*
 1160          * Now allocate the object array and type map.
 1161          */
 1162 
 1163         ssc->ses_objmap = SES_MALLOC(nobj * sizeof (encobj));
 1164         cc->ses_typidx = SES_MALLOC(nobj * sizeof (struct typidx));
 1165         cc->ses_eltmap = SES_MALLOC(ntype);
 1166 
 1167         if (ssc->ses_objmap == NULL || cc->ses_typidx == NULL ||
 1168             cc->ses_eltmap == NULL) {
 1169                 if (ssc->ses_objmap) {
 1170                         SES_FREE(ssc->ses_objmap, (nobj * sizeof (encobj)));
 1171                         ssc->ses_objmap = NULL;
 1172                 }
 1173                 if (cc->ses_typidx) {
 1174                         SES_FREE(cc->ses_typidx,
 1175                             (nobj * sizeof (struct typidx)));
 1176                         cc->ses_typidx = NULL;
 1177                 }
 1178                 if (cc->ses_eltmap) {
 1179                         SES_FREE(cc->ses_eltmap, ntype);
 1180                         cc->ses_eltmap = NULL;
 1181                 }
 1182                 SES_FREE(sdata, SCSZ);
 1183                 return (ENOMEM);
 1184         }
 1185         MEMZERO(ssc->ses_objmap, nobj * sizeof (encobj));
 1186         MEMZERO(cc->ses_typidx, nobj * sizeof (struct typidx));
 1187         MEMZERO(cc->ses_eltmap, ntype);
 1188         cc->ses_ntypes = (uint8_t) ntype;
 1189         ssc->ses_nobjects = nobj;
 1190 
 1191         /*
 1192          * Now waltz through the # of types again to fill in the types
 1193          * (and subenclosure ids) of the allocated objects.
 1194          */
 1195         nobj = 0;
 1196         for (i = 0; i < ntype; i++) {
 1197                 int j;
 1198                 if (ses_getthdr((uint8_t *)sdata, amt, i, &thdr)) {
 1199                         continue;
 1200                 }
 1201                 cc->ses_eltmap[i] = thdr.enc_maxelt;
 1202                 for (j = 0; j < thdr.enc_maxelt; j++) {
 1203                         cc->ses_typidx[nobj].ses_tidx = i;
 1204                         cc->ses_typidx[nobj].ses_oidx = j;
 1205                         ssc->ses_objmap[nobj].subenclosure = thdr.enc_subenc;
 1206                         ssc->ses_objmap[nobj++].enctype = thdr.enc_type;
 1207                 }
 1208         }
 1209         SES_FREE(sdata, SCSZ);
 1210         return (0);
 1211 }
 1212 
 1213 static int
 1214 ses_getputstat(ses_softc_t *ssc, int objid, SesComStat *sp, int slp, int in)
 1215 {
 1216         struct sscfg *cc;
 1217         int err, amt, bufsiz, tidx, oidx;
 1218         char cdb[6], *sdata;
 1219 
 1220         cc = ssc->ses_private;
 1221         if (cc == NULL) {
 1222                 return (ENXIO);
 1223         }
 1224 
 1225         /*
 1226          * If we're just getting overall enclosure status,
 1227          * we only need 2 bytes of data storage.
 1228          *
 1229          * If we're getting anything else, we know how much
 1230          * storage we need by noting that starting at offset
 1231          * 8 in returned data, all object status bytes are 4
 1232          * bytes long, and are stored in chunks of types(M)
 1233          * and nth+1 instances of type M.
 1234          */
 1235         if (objid == -1) {
 1236                 bufsiz = 2;
 1237         } else {
 1238                 bufsiz = (ssc->ses_nobjects * 4) + (cc->ses_ntypes * 4) + 8;
 1239         }
 1240         sdata = SES_MALLOC(bufsiz);
 1241         if (sdata == NULL)
 1242                 return (ENOMEM);
 1243 
 1244         cdb[0] = RECEIVE_DIAGNOSTIC;
 1245         cdb[1] = 1;
 1246         cdb[2] = SesStatusPage;
 1247         cdb[3] = bufsiz >> 8;
 1248         cdb[4] = bufsiz & 0xff;
 1249         cdb[5] = 0;
 1250         amt = bufsiz;
 1251         err = ses_runcmd(ssc, cdb, 6, sdata, &amt);
 1252         if (err) {
 1253                 SES_FREE(sdata, bufsiz);
 1254                 return (err);
 1255         }
 1256         amt = bufsiz - amt;
 1257 
 1258         if (objid == -1) {
 1259                 tidx = -1;
 1260                 oidx = -1;
 1261         } else {
 1262                 tidx = cc->ses_typidx[objid].ses_tidx;
 1263                 oidx = cc->ses_typidx[objid].ses_oidx;
 1264         }
 1265         if (in) {
 1266                 if (ses_decode(sdata, amt, cc->ses_eltmap, tidx, oidx, sp)) {
 1267                         err = ENODEV;
 1268                 }
 1269         } else {
 1270                 if (ses_encode(sdata, amt, cc->ses_eltmap, tidx, oidx, sp)) {
 1271                         err = ENODEV;
 1272                 } else {
 1273                         cdb[0] = SEND_DIAGNOSTIC;
 1274                         cdb[1] = 0x10;
 1275                         cdb[2] = 0;
 1276                         cdb[3] = bufsiz >> 8;
 1277                         cdb[4] = bufsiz & 0xff;
 1278                         cdb[5] = 0;
 1279                         amt = -bufsiz;
 1280                         err = ses_runcmd(ssc, cdb, 6, sdata, &amt);   
 1281                 }
 1282         }
 1283         SES_FREE(sdata, bufsiz);
 1284         return (0);
 1285 }
 1286 
 1287 
 1288 /*
 1289  * Routines to parse returned SES data structures.
 1290  * Architecture and compiler independent.
 1291  */
 1292 
 1293 static int
 1294 ses_cfghdr(uint8_t *buffer, int buflen, SesCfgHdr *cfp)
 1295 {
 1296         if (buflen < SES_CFGHDR_MINLEN) {
 1297                 return (-1);
 1298         }
 1299         gget8(buffer, 1, cfp->Nsubenc);
 1300         gget32(buffer, 4, cfp->GenCode);
 1301         return (0);
 1302 }
 1303 
 1304 static int
 1305 ses_enchdr(uint8_t *buffer, int amt, uint8_t SubEncId, SesEncHdr *chp)
 1306 {
 1307         int s, off = 8;
 1308         for (s = 0; s < SubEncId; s++) {
 1309                 if (off + 3 > amt)
 1310                         return (-1);
 1311                 off += buffer[off+3] + 4;
 1312         }
 1313         if (off + 3 > amt) {
 1314                 return (-1);
 1315         }
 1316         gget8(buffer, off+1, chp->Subencid);
 1317         gget8(buffer, off+2, chp->Ntypes);
 1318         gget8(buffer, off+3, chp->VEnclen);
 1319         return (0);
 1320 }
 1321 
 1322 static int
 1323 ses_encdesc(uint8_t *buffer, int amt, uint8_t SubEncId, SesEncDesc *cdp)
 1324 {
 1325         int s, e, enclen, off = 8;
 1326         for (s = 0; s < SubEncId; s++) {
 1327                 if (off + 3 > amt)
 1328                         return (-1);
 1329                 off += buffer[off+3] + 4;
 1330         }
 1331         if (off + 3 > amt) {
 1332                 return (-1);
 1333         }
 1334         gget8(buffer, off+3, enclen);
 1335         off += 4;
 1336         if (off  >= amt)
 1337                 return (-1);
 1338 
 1339         e = off + enclen;
 1340         if (e > amt) {
 1341                 e = amt;
 1342         }
 1343         MEMCPY(cdp, &buffer[off], e - off);
 1344         return (0);
 1345 }
 1346 
 1347 static int
 1348 ses_getthdr(uint8_t *buffer, int amt, int nth, SesThdr *thp)
 1349 {
 1350         int s, off = 8;
 1351 
 1352         if (amt < SES_CFGHDR_MINLEN) {
 1353                 return (-1);
 1354         }
 1355         for (s = 0; s < buffer[1]; s++) {
 1356                 if (off + 3 > amt)
 1357                         return (-1);
 1358                 off += buffer[off+3] + 4;
 1359         }
 1360         if (off + 3 > amt) {
 1361                 return (-1);
 1362         }
 1363         off += buffer[off+3] + 4 + (nth * 4);
 1364         if (amt < (off + 4))
 1365                 return (-1);
 1366 
 1367         gget8(buffer, off++, thp->enc_type);
 1368         gget8(buffer, off++, thp->enc_maxelt);
 1369         gget8(buffer, off++, thp->enc_subenc);
 1370         gget8(buffer, off, thp->enc_tlen);
 1371         return (0);
 1372 }
 1373 
 1374 /*
 1375  * This function needs a little explanation.
 1376  *
 1377  * The arguments are:
 1378  *
 1379  *
 1380  *      char *b, int amt
 1381  *
 1382  *              These describes the raw input SES status data and length.
 1383  *
 1384  *      uint8_t *ep
 1385  *
 1386  *              This is a map of the number of types for each element type
 1387  *              in the enclosure.
 1388  *
 1389  *      int elt
 1390  *
 1391  *              This is the element type being sought. If elt is -1,
 1392  *              then overall enclosure status is being sought.
 1393  *
 1394  *      int elm
 1395  *
 1396  *              This is the ordinal Mth element of type elt being sought.
 1397  *
 1398  *      SesComStat *sp
 1399  *
 1400  *              This is the output area to store the status for
 1401  *              the Mth element of type Elt.
 1402  */
 1403 
 1404 static int
 1405 ses_decode(char *b, int amt, uint8_t *ep, int elt, int elm, SesComStat *sp)
 1406 {
 1407         int idx, i;
 1408 
 1409         /*
 1410          * If it's overall enclosure status being sought, get that.
 1411          * We need at least 2 bytes of status data to get that.
 1412          */
 1413         if (elt == -1) {
 1414                 if (amt < 2)
 1415                         return (-1);
 1416                 gget8(b, 1, sp->comstatus);
 1417                 sp->comstat[0] = 0;
 1418                 sp->comstat[1] = 0;
 1419                 sp->comstat[2] = 0;
 1420                 return (0);
 1421         }
 1422 
 1423         /*
 1424          * Check to make sure that the Mth element is legal for type Elt.
 1425          */
 1426 
 1427         if (elm >= ep[elt])
 1428                 return (-1);
 1429 
 1430         /*
 1431          * Starting at offset 8, start skipping over the storage
 1432          * for the element types we're not interested in.
 1433          */
 1434         for (idx = 8, i = 0; i < elt; i++) {
 1435                 idx += ((ep[i] + 1) * 4);
 1436         }
 1437 
 1438         /*
 1439          * Skip over Overall status for this element type.
 1440          */
 1441         idx += 4;
 1442 
 1443         /*
 1444          * And skip to the index for the Mth element that we're going for.
 1445          */
 1446         idx += (4 * elm);
 1447 
 1448         /*
 1449          * Make sure we haven't overflowed the buffer.
 1450          */
 1451         if (idx+4 > amt)
 1452                 return (-1);
 1453 
 1454         /*
 1455          * Retrieve the status.
 1456          */
 1457         gget8(b, idx++, sp->comstatus);
 1458         gget8(b, idx++, sp->comstat[0]);
 1459         gget8(b, idx++, sp->comstat[1]);
 1460         gget8(b, idx++, sp->comstat[2]);
 1461 #if     0
 1462         PRINTF("Get Elt 0x%x Elm 0x%x (idx %d)\n", elt, elm, idx-4);
 1463 #endif
 1464         return (0);
 1465 }
 1466 
 1467 /*
 1468  * This is the mirror function to ses_decode, but we set the 'select'
 1469  * bit for the object which we're interested in. All other objects,
 1470  * after a status fetch, should have that bit off. Hmm. It'd be easy
 1471  * enough to ensure this, so we will.
 1472  */
 1473 
 1474 static int
 1475 ses_encode(char *b, int amt, uint8_t *ep, int elt, int elm, SesComStat *sp)
 1476 {
 1477         int idx, i;
 1478 
 1479         /*
 1480          * If it's overall enclosure status being sought, get that.
 1481          * We need at least 2 bytes of status data to get that.
 1482          */
 1483         if (elt == -1) {
 1484                 if (amt < 2)
 1485                         return (-1);
 1486                 i = 0;
 1487                 sset8(b, i, 0);
 1488                 sset8(b, i, sp->comstatus & 0xf);
 1489 #if     0
 1490                 PRINTF("set EncStat %x\n", sp->comstatus);
 1491 #endif
 1492                 return (0);
 1493         }
 1494 
 1495         /*
 1496          * Check to make sure that the Mth element is legal for type Elt.
 1497          */
 1498 
 1499         if (elm >= ep[elt])
 1500                 return (-1);
 1501 
 1502         /*
 1503          * Starting at offset 8, start skipping over the storage
 1504          * for the element types we're not interested in.
 1505          */
 1506         for (idx = 8, i = 0; i < elt; i++) {
 1507                 idx += ((ep[i] + 1) * 4);
 1508         }
 1509 
 1510         /*
 1511          * Skip over Overall status for this element type.
 1512          */
 1513         idx += 4;
 1514 
 1515         /*
 1516          * And skip to the index for the Mth element that we're going for.
 1517          */
 1518         idx += (4 * elm);
 1519 
 1520         /*
 1521          * Make sure we haven't overflowed the buffer.
 1522          */
 1523         if (idx+4 > amt)
 1524                 return (-1);
 1525 
 1526         /*
 1527          * Set the status.
 1528          */
 1529         sset8(b, idx, sp->comstatus);
 1530         sset8(b, idx, sp->comstat[0]);
 1531         sset8(b, idx, sp->comstat[1]);
 1532         sset8(b, idx, sp->comstat[2]);
 1533         idx -= 4;
 1534 
 1535 #if     0
 1536         PRINTF("Set Elt 0x%x Elm 0x%x (idx %d) with %x %x %x %x\n",
 1537             elt, elm, idx, sp->comstatus, sp->comstat[0],
 1538             sp->comstat[1], sp->comstat[2]);
 1539 #endif
 1540 
 1541         /*
 1542          * Now make sure all other 'Select' bits are off.
 1543          */
 1544         for (i = 8; i < amt; i += 4) {
 1545                 if (i != idx)
 1546                         b[i] &= ~0x80;
 1547         }
 1548         /*
 1549          * And make sure the INVOP bit is clear.
 1550          */
 1551         b[2] &= ~0x10;
 1552 
 1553         return (0);
 1554 }
 1555 
 1556 /*
 1557  * SAF-TE Type Device Emulation
 1558  */
 1559 
 1560 static int safte_getconfig(ses_softc_t *);
 1561 static int safte_rdstat(ses_softc_t *, int);;
 1562 static int set_objstat_sel(ses_softc_t *, ses_objstat *, int);
 1563 static int wrbuf16(ses_softc_t *, uint8_t, uint8_t, uint8_t, uint8_t, int);
 1564 static void wrslot_stat(ses_softc_t *, int);
 1565 static int perf_slotop(ses_softc_t *, uint8_t, uint8_t, int);
 1566 
 1567 #define ALL_ENC_STAT (SES_ENCSTAT_CRITICAL | SES_ENCSTAT_UNRECOV | \
 1568         SES_ENCSTAT_NONCRITICAL | SES_ENCSTAT_INFO)
 1569 /*
 1570  * SAF-TE specific defines- Mandatory ones only...
 1571  */
 1572 
 1573 /*
 1574  * READ BUFFER ('get' commands) IDs- placed in offset 2 of cdb
 1575  */
 1576 #define SAFTE_RD_RDCFG  0x00    /* read enclosure configuration */
 1577 #define SAFTE_RD_RDESTS 0x01    /* read enclosure status */
 1578 #define SAFTE_RD_RDDSTS 0x04    /* read drive slot status */
 1579 
 1580 /*
 1581  * WRITE BUFFER ('set' commands) IDs- placed in offset 0 of databuf
 1582  */
 1583 #define SAFTE_WT_DSTAT  0x10    /* write device slot status */
 1584 #define SAFTE_WT_SLTOP  0x12    /* perform slot operation */
 1585 #define SAFTE_WT_FANSPD 0x13    /* set fan speed */
 1586 #define SAFTE_WT_ACTPWS 0x14    /* turn on/off power supply */
 1587 #define SAFTE_WT_GLOBAL 0x15    /* send global command */
 1588 
 1589 
 1590 #define SAFT_SCRATCH    64
 1591 #define NPSEUDO_THERM   16
 1592 #define NPSEUDO_ALARM   1
 1593 struct scfg {
 1594         /*
 1595          * Cached Configuration
 1596          */
 1597         uint8_t Nfans;          /* Number of Fans */
 1598         uint8_t Npwr;           /* Number of Power Supplies */
 1599         uint8_t Nslots;         /* Number of Device Slots */
 1600         uint8_t DoorLock;       /* Door Lock Installed */
 1601         uint8_t Ntherm;         /* Number of Temperature Sensors */
 1602         uint8_t Nspkrs;         /* Number of Speakers */
 1603         uint8_t Nalarm;         /* Number of Alarms (at least one) */
 1604         /*
 1605          * Cached Flag Bytes for Global Status
 1606          */
 1607         uint8_t flag1;
 1608         uint8_t flag2;
 1609         /*
 1610          * What object index ID is where various slots start.
 1611          */
 1612         uint8_t pwroff;
 1613         uint8_t slotoff;
 1614 #define SAFT_ALARM_OFFSET(cc)   (cc)->slotoff - 1
 1615 };
 1616 
 1617 #define SAFT_FLG1_ALARM         0x1
 1618 #define SAFT_FLG1_GLOBFAIL      0x2
 1619 #define SAFT_FLG1_GLOBWARN      0x4
 1620 #define SAFT_FLG1_ENCPWROFF     0x8
 1621 #define SAFT_FLG1_ENCFANFAIL    0x10
 1622 #define SAFT_FLG1_ENCPWRFAIL    0x20
 1623 #define SAFT_FLG1_ENCDRVFAIL    0x40
 1624 #define SAFT_FLG1_ENCDRVWARN    0x80
 1625 
 1626 #define SAFT_FLG2_LOCKDOOR      0x4
 1627 #define SAFT_PRIVATE            sizeof (struct scfg)
 1628 
 1629 static char *safte_2little = "Too Little Data Returned (%d) at line %d\n";
 1630 #define SAFT_BAIL(r, x, k, l)   \
 1631         if ((r) >= (x)) { \
 1632                 SES_LOG(ssc, safte_2little, x, __LINE__);\
 1633                 SES_FREE((k), (l)); \
 1634                 return (EIO); \
 1635         }
 1636 
 1637 
 1638 static int
 1639 safte_softc_init(ses_softc_t *ssc, int doinit)
 1640 {
 1641         int err, i, r;
 1642         struct scfg *cc;
 1643 
 1644         if (doinit == 0) {
 1645                 if (ssc->ses_nobjects) {
 1646                         if (ssc->ses_objmap) {
 1647                                 SES_FREE(ssc->ses_objmap,
 1648                                     ssc->ses_nobjects * sizeof (encobj));
 1649                                 ssc->ses_objmap = NULL;
 1650                         }
 1651                         ssc->ses_nobjects = 0;
 1652                 }
 1653                 if (ssc->ses_private) {
 1654                         SES_FREE(ssc->ses_private, SAFT_PRIVATE);
 1655                         ssc->ses_private = NULL;
 1656                 }
 1657                 return (0);
 1658         }
 1659 
 1660         if (ssc->ses_private == NULL) {
 1661                 ssc->ses_private = SES_MALLOC(SAFT_PRIVATE);
 1662                 if (ssc->ses_private == NULL) {
 1663                         return (ENOMEM);
 1664                 }
 1665                 MEMZERO(ssc->ses_private, SAFT_PRIVATE);
 1666         }
 1667 
 1668         ssc->ses_nobjects = 0;
 1669         ssc->ses_encstat = 0;
 1670 
 1671         if ((err = safte_getconfig(ssc)) != 0) {
 1672                 return (err);
 1673         }
 1674 
 1675         /*
 1676          * The number of objects here, as well as that reported by the
 1677          * READ_BUFFER/GET_CONFIG call, are the over-temperature flags (15)
 1678          * that get reported during READ_BUFFER/READ_ENC_STATUS.
 1679          */
 1680         cc = ssc->ses_private;
 1681         ssc->ses_nobjects = cc->Nfans + cc->Npwr + cc->Nslots + cc->DoorLock +
 1682             cc->Ntherm + cc->Nspkrs + NPSEUDO_THERM + NPSEUDO_ALARM;
 1683         ssc->ses_objmap = (encobj *)
 1684             SES_MALLOC(ssc->ses_nobjects * sizeof (encobj));
 1685         if (ssc->ses_objmap == NULL) {
 1686                 return (ENOMEM);
 1687         }
 1688         MEMZERO(ssc->ses_objmap, ssc->ses_nobjects * sizeof (encobj));
 1689 
 1690         r = 0;
 1691         /*
 1692          * Note that this is all arranged for the convenience
 1693          * in later fetches of status.
 1694          */
 1695         for (i = 0; i < cc->Nfans; i++)
 1696                 ssc->ses_objmap[r++].enctype = SESTYP_FAN;
 1697         cc->pwroff = (uint8_t) r;
 1698         for (i = 0; i < cc->Npwr; i++)
 1699                 ssc->ses_objmap[r++].enctype = SESTYP_POWER;
 1700         for (i = 0; i < cc->DoorLock; i++)
 1701                 ssc->ses_objmap[r++].enctype = SESTYP_DOORLOCK;
 1702         for (i = 0; i < cc->Nspkrs; i++)
 1703                 ssc->ses_objmap[r++].enctype = SESTYP_ALARM;
 1704         for (i = 0; i < cc->Ntherm; i++)
 1705                 ssc->ses_objmap[r++].enctype = SESTYP_THERM;
 1706         for (i = 0; i < NPSEUDO_THERM; i++)
 1707                 ssc->ses_objmap[r++].enctype = SESTYP_THERM;
 1708         ssc->ses_objmap[r++].enctype = SESTYP_ALARM;
 1709         cc->slotoff = (uint8_t) r;
 1710         for (i = 0; i < cc->Nslots; i++)
 1711                 ssc->ses_objmap[r++].enctype = SESTYP_DEVICE;
 1712         return (0);
 1713 }
 1714 
 1715 static int
 1716 safte_init_enc(ses_softc_t *ssc)
 1717 {
 1718         int err;
 1719         static char cdb0[6] = { SEND_DIAGNOSTIC };
 1720 
 1721         err = ses_runcmd(ssc, cdb0, 6, NULL, 0);
 1722         if (err) {
 1723                 return (err);
 1724         }
 1725         DELAY(5000);
 1726         err = wrbuf16(ssc, SAFTE_WT_GLOBAL, 0, 0, 0, 1);
 1727         return (err);
 1728 }
 1729 
 1730 static int
 1731 safte_get_encstat(ses_softc_t *ssc, int slpflg)
 1732 {
 1733         return (safte_rdstat(ssc, slpflg));
 1734 }
 1735 
 1736 static int
 1737 safte_set_encstat(ses_softc_t *ssc, uint8_t encstat, int slpflg)
 1738 {
 1739         struct scfg *cc = ssc->ses_private;
 1740         if (cc == NULL)
 1741                 return (0);
 1742         /*
 1743          * Since SAF-TE devices aren't necessarily sticky in terms
 1744          * of state, make our soft copy of enclosure status 'sticky'-
 1745          * that is, things set in enclosure status stay set (as implied
 1746          * by conditions set in reading object status) until cleared.
 1747          */
 1748         ssc->ses_encstat &= ~ALL_ENC_STAT;
 1749         ssc->ses_encstat |= (encstat & ALL_ENC_STAT);
 1750         ssc->ses_encstat |= ENCI_SVALID;
 1751         cc->flag1 &= ~(SAFT_FLG1_ALARM|SAFT_FLG1_GLOBFAIL|SAFT_FLG1_GLOBWARN);
 1752         if ((encstat & (SES_ENCSTAT_CRITICAL|SES_ENCSTAT_UNRECOV)) != 0) {
 1753                 cc->flag1 |= SAFT_FLG1_ALARM|SAFT_FLG1_GLOBFAIL;
 1754         } else if ((encstat & SES_ENCSTAT_NONCRITICAL) != 0) {
 1755                 cc->flag1 |= SAFT_FLG1_GLOBWARN;
 1756         }
 1757         return (wrbuf16(ssc, SAFTE_WT_GLOBAL, cc->flag1, cc->flag2, 0, slpflg));
 1758 }
 1759 
 1760 static int
 1761 safte_get_objstat(ses_softc_t *ssc, ses_objstat *obp, int slpflg)
 1762 {
 1763         int i = (int)obp->obj_id;
 1764 
 1765         if ((ssc->ses_encstat & ENCI_SVALID) == 0 ||
 1766             (ssc->ses_objmap[i].svalid) == 0) {
 1767                 int err = safte_rdstat(ssc, slpflg);
 1768                 if (err)
 1769                         return (err);
 1770         }
 1771         obp->cstat[0] = ssc->ses_objmap[i].encstat[0];
 1772         obp->cstat[1] = ssc->ses_objmap[i].encstat[1];
 1773         obp->cstat[2] = ssc->ses_objmap[i].encstat[2];
 1774         obp->cstat[3] = ssc->ses_objmap[i].encstat[3];
 1775         return (0);
 1776 }
 1777 
 1778 
 1779 static int
 1780 safte_set_objstat(ses_softc_t *ssc, ses_objstat *obp, int slp)
 1781 {
 1782         int idx, err;
 1783         encobj *ep;
 1784         struct scfg *cc;
 1785 
 1786 
 1787         SES_DLOG(ssc, "safte_set_objstat(%d): %x %x %x %x\n",
 1788             (int)obp->obj_id, obp->cstat[0], obp->cstat[1], obp->cstat[2],
 1789             obp->cstat[3]);
 1790 
 1791         /*
 1792          * If this is clear, we don't do diddly.
 1793          */
 1794         if ((obp->cstat[0] & SESCTL_CSEL) == 0) {
 1795                 return (0);
 1796         }
 1797 
 1798         err = 0;
 1799         /*
 1800          * Check to see if the common bits are set and do them first.
 1801          */
 1802         if (obp->cstat[0] & ~SESCTL_CSEL) {
 1803                 err = set_objstat_sel(ssc, obp, slp);
 1804                 if (err)
 1805                         return (err);
 1806         }
 1807 
 1808         cc = ssc->ses_private;
 1809         if (cc == NULL)
 1810                 return (0);
 1811 
 1812         idx = (int)obp->obj_id;
 1813         ep = &ssc->ses_objmap[idx];
 1814 
 1815         switch (ep->enctype) {
 1816         case SESTYP_DEVICE:
 1817         {
 1818                 uint8_t slotop = 0;
 1819                 /*
 1820                  * XXX: I should probably cache the previous state
 1821                  * XXX: of SESCTL_DEVOFF so that when it goes from
 1822                  * XXX: true to false I can then set PREPARE FOR OPERATION
 1823                  * XXX: flag in PERFORM SLOT OPERATION write buffer command.
 1824                  */
 1825                 if (obp->cstat[2] & (SESCTL_RQSINS|SESCTL_RQSRMV)) {
 1826                         slotop |= 0x2;
 1827                 }
 1828                 if (obp->cstat[2] & SESCTL_RQSID) {
 1829                         slotop |= 0x4;
 1830                 }
 1831                 err = perf_slotop(ssc, (uint8_t) idx - (uint8_t) cc->slotoff,
 1832                     slotop, slp);
 1833                 if (err)
 1834                         return (err);
 1835                 if (obp->cstat[3] & SESCTL_RQSFLT) {
 1836                         ep->priv |= 0x2;
 1837                 } else {
 1838                         ep->priv &= ~0x2;
 1839                 }
 1840                 if (ep->priv & 0xc6) {
 1841                         ep->priv &= ~0x1;
 1842                 } else {
 1843                         ep->priv |= 0x1;        /* no errors */
 1844                 }
 1845                 wrslot_stat(ssc, slp);
 1846                 break;
 1847         }
 1848         case SESTYP_POWER:
 1849                 if (obp->cstat[3] & SESCTL_RQSTFAIL) {
 1850                         cc->flag1 |= SAFT_FLG1_ENCPWRFAIL;
 1851                 } else {
 1852                         cc->flag1 &= ~SAFT_FLG1_ENCPWRFAIL;
 1853                 }
 1854                 err = wrbuf16(ssc, SAFTE_WT_GLOBAL, cc->flag1,
 1855                     cc->flag2, 0, slp);
 1856                 if (err)
 1857                         return (err);
 1858                 if (obp->cstat[3] & SESCTL_RQSTON) {
 1859                         (void) wrbuf16(ssc, SAFTE_WT_ACTPWS,
 1860                                 idx - cc->pwroff, 0, 0, slp);
 1861                 } else {
 1862                         (void) wrbuf16(ssc, SAFTE_WT_ACTPWS,
 1863                                 idx - cc->pwroff, 0, 1, slp);
 1864                 }
 1865                 break;
 1866         case SESTYP_FAN:
 1867                 if (obp->cstat[3] & SESCTL_RQSTFAIL) {
 1868                         cc->flag1 |= SAFT_FLG1_ENCFANFAIL;
 1869                 } else {
 1870                         cc->flag1 &= ~SAFT_FLG1_ENCFANFAIL;
 1871                 }
 1872                 err = wrbuf16(ssc, SAFTE_WT_GLOBAL, cc->flag1,
 1873                     cc->flag2, 0, slp);
 1874                 if (err)
 1875                         return (err);
 1876                 if (obp->cstat[3] & SESCTL_RQSTON) {
 1877                         uint8_t fsp;
 1878                         if ((obp->cstat[3] & 0x7) == 7) {
 1879                                 fsp = 4;
 1880                         } else if ((obp->cstat[3] & 0x7) == 6) {
 1881                                 fsp = 3;
 1882                         } else if ((obp->cstat[3] & 0x7) == 4) {
 1883                                 fsp = 2;
 1884                         } else {
 1885                                 fsp = 1;
 1886                         }
 1887                         (void) wrbuf16(ssc, SAFTE_WT_FANSPD, idx, fsp, 0, slp);
 1888                 } else {
 1889                         (void) wrbuf16(ssc, SAFTE_WT_FANSPD, idx, 0, 0, slp);
 1890                 }
 1891                 break;
 1892         case SESTYP_DOORLOCK:
 1893                 if (obp->cstat[3] & 0x1) {
 1894                         cc->flag2 &= ~SAFT_FLG2_LOCKDOOR;
 1895                 } else {
 1896                         cc->flag2 |= SAFT_FLG2_LOCKDOOR;
 1897                 }
 1898                 (void) wrbuf16(ssc, SAFTE_WT_GLOBAL, cc->flag1,
 1899                     cc->flag2, 0, slp);
 1900                 break;
 1901         case SESTYP_ALARM:
 1902                 /*
 1903                  * On all nonzero but the 'muted' bit, we turn on the alarm,
 1904                  */
 1905                 obp->cstat[3] &= ~0xa;
 1906                 if (obp->cstat[3] & 0x40) {
 1907                         cc->flag2 &= ~SAFT_FLG1_ALARM;
 1908                 } else if (obp->cstat[3] != 0) {
 1909                         cc->flag2 |= SAFT_FLG1_ALARM;
 1910                 } else {
 1911                         cc->flag2 &= ~SAFT_FLG1_ALARM;
 1912                 }
 1913                 ep->priv = obp->cstat[3];
 1914                 (void) wrbuf16(ssc, SAFTE_WT_GLOBAL, cc->flag1,
 1915                         cc->flag2, 0, slp);
 1916                 break;
 1917         default:
 1918                 break;
 1919         }
 1920         ep->svalid = 0;
 1921         return (0);
 1922 }
 1923 
 1924 static int
 1925 safte_getconfig(ses_softc_t *ssc)
 1926 {
 1927         struct scfg *cfg;
 1928         int err, amt;
 1929         char *sdata;
 1930         static char cdb[10] =
 1931             { READ_BUFFER, 1, SAFTE_RD_RDCFG, 0, 0, 0, 0, 0, SAFT_SCRATCH, 0 };
 1932 
 1933         cfg = ssc->ses_private;
 1934         if (cfg == NULL)
 1935                 return (ENXIO);
 1936 
 1937         sdata = SES_MALLOC(SAFT_SCRATCH);
 1938         if (sdata == NULL)
 1939                 return (ENOMEM);
 1940 
 1941         amt = SAFT_SCRATCH;
 1942         err = ses_runcmd(ssc, cdb, 10, sdata, &amt);
 1943         if (err) {
 1944                 SES_FREE(sdata, SAFT_SCRATCH);
 1945                 return (err);
 1946         }
 1947         amt = SAFT_SCRATCH - amt;
 1948         if (amt < 6) {
 1949                 SES_LOG(ssc, "too little data (%d) for configuration\n", amt);
 1950                 SES_FREE(sdata, SAFT_SCRATCH);
 1951                 return (EIO);
 1952         }
 1953         SES_VLOG(ssc, "Nfans %d Npwr %d Nslots %d Lck %d Ntherm %d Nspkrs %d\n",
 1954             sdata[0], sdata[1], sdata[2], sdata[3], sdata[4], sdata[5]);
 1955         cfg->Nfans = sdata[0];
 1956         cfg->Npwr = sdata[1];
 1957         cfg->Nslots = sdata[2];
 1958         cfg->DoorLock = sdata[3];
 1959         cfg->Ntherm = sdata[4];
 1960         cfg->Nspkrs = sdata[5];
 1961         cfg->Nalarm = NPSEUDO_ALARM;
 1962         SES_FREE(sdata, SAFT_SCRATCH);
 1963         return (0);
 1964 }
 1965 
 1966 static int
 1967 safte_rdstat(ses_softc_t *ssc, int slpflg)
 1968 {
 1969         int err, oid, r, i, hiwater, nitems, amt;
 1970         uint16_t tempflags;
 1971         size_t buflen;
 1972         uint8_t status, oencstat;
 1973         char *sdata, cdb[10];
 1974         struct scfg *cc = ssc->ses_private;
 1975 
 1976 
 1977         /*
 1978          * The number of objects overstates things a bit,
 1979          * both for the bogus 'thermometer' entries and
 1980          * the drive status (which isn't read at the same
 1981          * time as the enclosure status), but that's okay.
 1982          */
 1983         buflen = 4 * cc->Nslots;
 1984         if (ssc->ses_nobjects > buflen)
 1985                 buflen = ssc->ses_nobjects;
 1986         sdata = SES_MALLOC(buflen);
 1987         if (sdata == NULL)
 1988                 return (ENOMEM);
 1989 
 1990         cdb[0] = READ_BUFFER;
 1991         cdb[1] = 1;
 1992         cdb[2] = SAFTE_RD_RDESTS;
 1993         cdb[3] = 0;
 1994         cdb[4] = 0;
 1995         cdb[5] = 0;
 1996         cdb[6] = 0;
 1997         cdb[7] = (buflen >> 8) & 0xff;
 1998         cdb[8] = buflen & 0xff;
 1999         cdb[9] = 0;
 2000         amt = buflen;
 2001         err = ses_runcmd(ssc, cdb, 10, sdata, &amt);
 2002         if (err) {
 2003                 SES_FREE(sdata, buflen);
 2004                 return (err);
 2005         }
 2006         hiwater = buflen - amt;
 2007 
 2008 
 2009         /*
 2010          * invalidate all status bits.
 2011          */
 2012         for (i = 0; i < ssc->ses_nobjects; i++)
 2013                 ssc->ses_objmap[i].svalid = 0;
 2014         oencstat = ssc->ses_encstat & ALL_ENC_STAT;
 2015         ssc->ses_encstat = 0;
 2016 
 2017 
 2018         /*
 2019          * Now parse returned buffer.
 2020          * If we didn't get enough data back,
 2021          * that's considered a fatal error.
 2022          */
 2023         oid = r = 0;
 2024 
 2025         for (nitems = i = 0; i < cc->Nfans; i++) {
 2026                 SAFT_BAIL(r, hiwater, sdata, buflen);
 2027                 /*
 2028                  * 0 = Fan Operational
 2029                  * 1 = Fan is malfunctioning
 2030                  * 2 = Fan is not present
 2031                  * 0x80 = Unknown or Not Reportable Status
 2032                  */
 2033                 ssc->ses_objmap[oid].encstat[1] = 0;    /* resvd */
 2034                 ssc->ses_objmap[oid].encstat[2] = 0;    /* resvd */
 2035                 switch ((int)(uint8_t)sdata[r]) {
 2036                 case 0:
 2037                         nitems++;
 2038                         ssc->ses_objmap[oid].encstat[0] = SES_OBJSTAT_OK;
 2039                         /*
 2040                          * We could get fancier and cache
 2041                          * fan speeds that we have set, but
 2042                          * that isn't done now.
 2043                          */
 2044                         ssc->ses_objmap[oid].encstat[3] = 7;
 2045                         break;
 2046 
 2047                 case 1:
 2048                         ssc->ses_objmap[oid].encstat[0] = SES_OBJSTAT_CRIT;
 2049                         /*
 2050                          * FAIL and FAN STOPPED synthesized
 2051                          */
 2052                         ssc->ses_objmap[oid].encstat[3] = 0x40;
 2053                         /*
 2054                          * Enclosure marked with CRITICAL error
 2055                          * if only one fan or no thermometers,
 2056                          * else the NONCRITICAL error is set.
 2057                          */
 2058                         if (cc->Nfans == 1 || cc->Ntherm == 0)
 2059                                 ssc->ses_encstat |= SES_ENCSTAT_CRITICAL;
 2060                         else
 2061                                 ssc->ses_encstat |= SES_ENCSTAT_NONCRITICAL;
 2062                         break;
 2063                 case 2:
 2064                         ssc->ses_objmap[oid].encstat[0] =
 2065                             SES_OBJSTAT_NOTINSTALLED;
 2066                         ssc->ses_objmap[oid].encstat[3] = 0;
 2067                         /*
 2068                          * Enclosure marked with CRITICAL error
 2069                          * if only one fan or no thermometers,
 2070                          * else the NONCRITICAL error is set.
 2071                          */
 2072                         if (cc->Nfans == 1)
 2073                                 ssc->ses_encstat |= SES_ENCSTAT_CRITICAL;
 2074                         else
 2075                                 ssc->ses_encstat |= SES_ENCSTAT_NONCRITICAL;
 2076                         break;
 2077                 case 0x80:
 2078                         ssc->ses_objmap[oid].encstat[0] = SES_OBJSTAT_UNKNOWN;
 2079                         ssc->ses_objmap[oid].encstat[3] = 0;
 2080                         ssc->ses_encstat |= SES_ENCSTAT_INFO;
 2081                         break;
 2082                 default:
 2083                         ssc->ses_objmap[oid].encstat[0] =
 2084                             SES_OBJSTAT_UNSUPPORTED;
 2085                         SES_LOG(ssc, "Unknown fan%d status 0x%x\n", i,
 2086                             sdata[r] & 0xff);
 2087                         break;
 2088                 }
 2089                 ssc->ses_objmap[oid++].svalid = 1;
 2090                 r++;
 2091         }
 2092 
 2093         /*
 2094          * No matter how you cut it, no cooling elements when there
 2095          * should be some there is critical.
 2096          */
 2097         if (cc->Nfans && nitems == 0) {
 2098                 ssc->ses_encstat |= SES_ENCSTAT_CRITICAL;
 2099         }
 2100 
 2101 
 2102         for (i = 0; i < cc->Npwr; i++) {
 2103                 SAFT_BAIL(r, hiwater, sdata, buflen);
 2104                 ssc->ses_objmap[oid].encstat[0] = SES_OBJSTAT_UNKNOWN;
 2105                 ssc->ses_objmap[oid].encstat[1] = 0;    /* resvd */
 2106                 ssc->ses_objmap[oid].encstat[2] = 0;    /* resvd */
 2107                 ssc->ses_objmap[oid].encstat[3] = 0x20; /* requested on */
 2108                 switch ((uint8_t)sdata[r]) {
 2109                 case 0x00:      /* pws operational and on */
 2110                         ssc->ses_objmap[oid].encstat[0] = SES_OBJSTAT_OK;
 2111                         break;
 2112                 case 0x01:      /* pws operational and off */
 2113                         ssc->ses_objmap[oid].encstat[0] = SES_OBJSTAT_OK;
 2114                         ssc->ses_objmap[oid].encstat[3] = 0x10;
 2115                         ssc->ses_encstat |= SES_ENCSTAT_INFO;
 2116                         break;
 2117                 case 0x10:      /* pws is malfunctioning and commanded on */
 2118                         ssc->ses_objmap[oid].encstat[0] = SES_OBJSTAT_CRIT;
 2119                         ssc->ses_objmap[oid].encstat[3] = 0x61;
 2120                         ssc->ses_encstat |= SES_ENCSTAT_NONCRITICAL;
 2121                         break;
 2122 
 2123                 case 0x11:      /* pws is malfunctioning and commanded off */
 2124                         ssc->ses_objmap[oid].encstat[0] = SES_OBJSTAT_NONCRIT;
 2125                         ssc->ses_objmap[oid].encstat[3] = 0x51;
 2126                         ssc->ses_encstat |= SES_ENCSTAT_NONCRITICAL;
 2127                         break;
 2128                 case 0x20:      /* pws is not present */
 2129                         ssc->ses_objmap[oid].encstat[0] =
 2130                             SES_OBJSTAT_NOTINSTALLED;
 2131                         ssc->ses_objmap[oid].encstat[3] = 0;
 2132                         ssc->ses_encstat |= SES_ENCSTAT_INFO;
 2133                         break;
 2134                 case 0x21:      /* pws is present */
 2135                         /*
 2136                          * This is for enclosures that cannot tell whether the
 2137                          * device is on or malfunctioning, but know that it is
 2138                          * present. Just fall through.
 2139                          */
 2140                         /* FALLTHROUGH */
 2141                 case 0x80:      /* Unknown or Not Reportable Status */
 2142                         ssc->ses_objmap[oid].encstat[0] = SES_OBJSTAT_UNKNOWN;
 2143                         ssc->ses_objmap[oid].encstat[3] = 0;
 2144                         ssc->ses_encstat |= SES_ENCSTAT_INFO;
 2145                         break;
 2146                 default:
 2147                         SES_LOG(ssc, "unknown power supply %d status (0x%x)\n",
 2148                             i, sdata[r] & 0xff);
 2149                         break;
 2150                 }
 2151                 ssc->ses_objmap[oid++].svalid = 1;
 2152                 r++;
 2153         }
 2154 
 2155         /*
 2156          * Skip over Slot SCSI IDs
 2157          */
 2158         r += cc->Nslots;
 2159 
 2160         /*
 2161          * We always have doorlock status, no matter what,
 2162          * but we only save the status if we have one.
 2163          */
 2164         SAFT_BAIL(r, hiwater, sdata, buflen);
 2165         if (cc->DoorLock) {
 2166                 /*
 2167                  * 0 = Door Locked
 2168                  * 1 = Door Unlocked, or no Lock Installed
 2169                  * 0x80 = Unknown or Not Reportable Status
 2170                  */
 2171                 ssc->ses_objmap[oid].encstat[1] = 0;
 2172                 ssc->ses_objmap[oid].encstat[2] = 0;
 2173                 switch ((uint8_t)sdata[r]) {
 2174                 case 0:
 2175                         ssc->ses_objmap[oid].encstat[0] = SES_OBJSTAT_OK;
 2176                         ssc->ses_objmap[oid].encstat[3] = 0;
 2177                         break;
 2178                 case 1:
 2179                         ssc->ses_objmap[oid].encstat[0] = SES_OBJSTAT_OK;
 2180                         ssc->ses_objmap[oid].encstat[3] = 1;
 2181                         break;
 2182                 case 0x80:
 2183                         ssc->ses_objmap[oid].encstat[0] = SES_OBJSTAT_UNKNOWN;
 2184                         ssc->ses_objmap[oid].encstat[3] = 0;
 2185                         ssc->ses_encstat |= SES_ENCSTAT_INFO;
 2186                         break;
 2187                 default:
 2188                         ssc->ses_objmap[oid].encstat[0] =
 2189                             SES_OBJSTAT_UNSUPPORTED;
 2190                         SES_LOG(ssc, "unknown lock status 0x%x\n",
 2191                             sdata[r] & 0xff);
 2192                         break;
 2193                 }
 2194                 ssc->ses_objmap[oid++].svalid = 1;
 2195         }
 2196         r++;
 2197 
 2198         /*
 2199          * We always have speaker status, no matter what,
 2200          * but we only save the status if we have one.
 2201          */
 2202         SAFT_BAIL(r, hiwater, sdata, buflen);
 2203         if (cc->Nspkrs) {
 2204                 ssc->ses_objmap[oid].encstat[1] = 0;
 2205                 ssc->ses_objmap[oid].encstat[2] = 0;
 2206                 if (sdata[r] == 1) {
 2207                         /*
 2208                          * We need to cache tone urgency indicators.
 2209                          * Someday.
 2210                          */
 2211                         ssc->ses_objmap[oid].encstat[0] = SES_OBJSTAT_NONCRIT;
 2212                         ssc->ses_objmap[oid].encstat[3] = 0x8;
 2213                         ssc->ses_encstat |= SES_ENCSTAT_NONCRITICAL;
 2214                 } else if (sdata[r] == 0) {
 2215                         ssc->ses_objmap[oid].encstat[0] = SES_OBJSTAT_OK;
 2216                         ssc->ses_objmap[oid].encstat[3] = 0;
 2217                 } else {
 2218                         ssc->ses_objmap[oid].encstat[0] =
 2219                             SES_OBJSTAT_UNSUPPORTED;
 2220                         ssc->ses_objmap[oid].encstat[3] = 0;
 2221                         SES_LOG(ssc, "unknown spkr status 0x%x\n",
 2222                             sdata[r] & 0xff);
 2223                 }
 2224                 ssc->ses_objmap[oid++].svalid = 1;
 2225         }
 2226         r++;
 2227 
 2228         for (i = 0; i < cc->Ntherm; i++) {
 2229                 SAFT_BAIL(r, hiwater, sdata, buflen);
 2230                 /*
 2231                  * Status is a range from -10 to 245 deg Celsius,
 2232                  * which we need to normalize to -20 to -245 according
 2233                  * to the latest SCSI spec, which makes little
 2234                  * sense since this would overflow an 8bit value.
 2235                  * Well, still, the base normalization is -20,
 2236                  * not -10, so we have to adjust.
 2237                  *
 2238                  * So what's over and under temperature?
 2239                  * Hmm- we'll state that 'normal' operating
 2240                  * is 10 to 40 deg Celsius.
 2241                  */
 2242 
 2243                 /*
 2244                  * Actually.... All of the units that people out in the world
 2245                  * seem to have do not come even close to setting a value that
 2246                  * complies with this spec.
 2247                  *
 2248                  * The closest explanation I could find was in an
 2249                  * LSI-Logic manual, which seemed to indicate that
 2250                  * this value would be set by whatever the I2C code
 2251                  * would interpolate from the output of an LM75
 2252                  * temperature sensor.
 2253                  *
 2254                  * This means that it is impossible to use the actual
 2255                  * numeric value to predict anything. But we don't want
 2256                  * to lose the value. So, we'll propagate the *uncorrected*
 2257                  * value and set SES_OBJSTAT_NOTAVAIL. We'll depend on the
 2258                  * temperature flags for warnings.
 2259                  */
 2260                 ssc->ses_objmap[oid].encstat[0] = SES_OBJSTAT_NOTAVAIL;
 2261                 ssc->ses_objmap[oid].encstat[1] = 0;
 2262                 ssc->ses_objmap[oid].encstat[2] = sdata[r];
 2263                 ssc->ses_objmap[oid].encstat[3] = 0;;
 2264                 ssc->ses_objmap[oid++].svalid = 1;
 2265                 r++;
 2266         }
 2267 
 2268         /*
 2269          * Now, for "pseudo" thermometers, we have two bytes
 2270          * of information in enclosure status- 16 bits. Actually,
 2271          * the MSB is a single TEMP ALERT flag indicating whether
 2272          * any other bits are set, but, thanks to fuzzy thinking,
 2273          * in the SAF-TE spec, this can also be set even if no
 2274          * other bits are set, thus making this really another
 2275          * binary temperature sensor.
 2276          */
 2277 
 2278         SAFT_BAIL(r, hiwater, sdata, buflen);
 2279         tempflags = sdata[r++];
 2280         SAFT_BAIL(r, hiwater, sdata, buflen);
 2281         tempflags |= (tempflags << 8) | sdata[r++];
 2282 
 2283         for (i = 0; i < NPSEUDO_THERM; i++) {
 2284                 ssc->ses_objmap[oid].encstat[1] = 0;
 2285                 if (tempflags & (1 << (NPSEUDO_THERM - i - 1))) {
 2286                         ssc->ses_objmap[oid].encstat[0] = SES_OBJSTAT_CRIT;
 2287                         ssc->ses_objmap[4].encstat[2] = 0xff;
 2288                         /*
 2289                          * Set 'over temperature' failure.
 2290                          */
 2291                         ssc->ses_objmap[oid].encstat[3] = 8;
 2292                         ssc->ses_encstat |= SES_ENCSTAT_CRITICAL;
 2293                 } else {
 2294                         /*
 2295                          * We used to say 'not available' and synthesize a
 2296                          * nominal 30 deg (C)- that was wrong. Actually,
 2297                          * Just say 'OK', and use the reserved value of
 2298                          * zero.
 2299                          */
 2300                         ssc->ses_objmap[oid].encstat[0] = SES_OBJSTAT_OK;
 2301                         ssc->ses_objmap[oid].encstat[2] = 0;
 2302                         ssc->ses_objmap[oid].encstat[3] = 0;
 2303                 }
 2304                 ssc->ses_objmap[oid++].svalid = 1;
 2305         }
 2306 
 2307         /*
 2308          * Get alarm status.
 2309          */
 2310         ssc->ses_objmap[oid].encstat[0] = SES_OBJSTAT_OK;
 2311         ssc->ses_objmap[oid].encstat[3] = ssc->ses_objmap[oid].priv;
 2312         ssc->ses_objmap[oid++].svalid = 1;
 2313 
 2314         /*
 2315          * Now get drive slot status
 2316          */
 2317         cdb[2] = SAFTE_RD_RDDSTS;
 2318         amt = buflen;
 2319         err = ses_runcmd(ssc, cdb, 10, sdata, &amt);
 2320         if (err) {
 2321                 SES_FREE(sdata, buflen);
 2322                 return (err);
 2323         }
 2324         hiwater = buflen - amt;
 2325         for (r = i = 0; i < cc->Nslots; i++, r += 4) {
 2326                 SAFT_BAIL(r+3, hiwater, sdata, buflen);
 2327                 ssc->ses_objmap[oid].encstat[0] = SES_OBJSTAT_UNSUPPORTED;
 2328                 ssc->ses_objmap[oid].encstat[1] = (uint8_t) i;
 2329                 ssc->ses_objmap[oid].encstat[2] = 0;
 2330                 ssc->ses_objmap[oid].encstat[3] = 0;
 2331                 status = sdata[r+3];
 2332                 if ((status & 0x1) == 0) {      /* no device */
 2333                         ssc->ses_objmap[oid].encstat[0] =
 2334                             SES_OBJSTAT_NOTINSTALLED;
 2335                 } else {
 2336                         ssc->ses_objmap[oid].encstat[0] = SES_OBJSTAT_OK;
 2337                 }
 2338                 if (status & 0x2) {
 2339                         ssc->ses_objmap[oid].encstat[2] = 0x8;
 2340                 }
 2341                 if ((status & 0x4) == 0) {
 2342                         ssc->ses_objmap[oid].encstat[3] = 0x10;
 2343                 }
 2344                 ssc->ses_objmap[oid++].svalid = 1;
 2345         }
 2346         /* see comment below about sticky enclosure status */
 2347         ssc->ses_encstat |= ENCI_SVALID | oencstat;
 2348         SES_FREE(sdata, buflen);
 2349         return (0);
 2350 }
 2351 
 2352 static int
 2353 set_objstat_sel(ses_softc_t *ssc, ses_objstat *obp, int slp)
 2354 {
 2355         int idx;
 2356         encobj *ep;
 2357         struct scfg *cc = ssc->ses_private;
 2358 
 2359         if (cc == NULL)
 2360                 return (0);
 2361 
 2362         idx = (int)obp->obj_id;
 2363         ep = &ssc->ses_objmap[idx];
 2364 
 2365         switch (ep->enctype) {
 2366         case SESTYP_DEVICE:
 2367                 if (obp->cstat[0] & SESCTL_PRDFAIL) {
 2368                         ep->priv |= 0x40;
 2369                 }
 2370                 /* SESCTL_RSTSWAP has no correspondence in SAF-TE */
 2371                 if (obp->cstat[0] & SESCTL_DISABLE) {
 2372                         ep->priv |= 0x80;
 2373                         /*
 2374                          * Hmm. Try to set the 'No Drive' flag.
 2375                          * Maybe that will count as a 'disable'.
 2376                          */
 2377                 }
 2378                 if (ep->priv & 0xc6) {
 2379                         ep->priv &= ~0x1;
 2380                 } else {
 2381                         ep->priv |= 0x1;        /* no errors */
 2382                 }
 2383                 wrslot_stat(ssc, slp);
 2384                 break;
 2385         case SESTYP_POWER:
 2386                 /*
 2387                  * Okay- the only one that makes sense here is to
 2388                  * do the 'disable' for a power supply.
 2389                  */
 2390                 if (obp->cstat[0] & SESCTL_DISABLE) {
 2391                         (void) wrbuf16(ssc, SAFTE_WT_ACTPWS,
 2392                                 idx - cc->pwroff, 0, 0, slp);
 2393                 }
 2394                 break;
 2395         case SESTYP_FAN:
 2396                 /*
 2397                  * Okay- the only one that makes sense here is to
 2398                  * set fan speed to zero on disable.
 2399                  */
 2400                 if (obp->cstat[0] & SESCTL_DISABLE) {
 2401                         /* remember- fans are the first items, so idx works */
 2402                         (void) wrbuf16(ssc, SAFTE_WT_FANSPD, idx, 0, 0, slp);
 2403                 }
 2404                 break;
 2405         case SESTYP_DOORLOCK:
 2406                 /*
 2407                  * Well, we can 'disable' the lock.
 2408                  */
 2409                 if (obp->cstat[0] & SESCTL_DISABLE) {
 2410                         cc->flag2 &= ~SAFT_FLG2_LOCKDOOR;
 2411                         (void) wrbuf16(ssc, SAFTE_WT_GLOBAL, cc->flag1,
 2412                                 cc->flag2, 0, slp);
 2413                 }
 2414                 break;
 2415         case SESTYP_ALARM:
 2416                 /*
 2417                  * Well, we can 'disable' the alarm.
 2418                  */
 2419                 if (obp->cstat[0] & SESCTL_DISABLE) {
 2420                         cc->flag2 &= ~SAFT_FLG1_ALARM;
 2421                         ep->priv |= 0x40;       /* Muted */
 2422                         (void) wrbuf16(ssc, SAFTE_WT_GLOBAL, cc->flag1,
 2423                                 cc->flag2, 0, slp);
 2424                 }
 2425                 break;
 2426         default:
 2427                 break;
 2428         }
 2429         ep->svalid = 0;
 2430         return (0);
 2431 }
 2432 
 2433 /*
 2434  * This function handles all of the 16 byte WRITE BUFFER commands.
 2435  */
 2436 static int
 2437 wrbuf16(ses_softc_t *ssc, uint8_t op, uint8_t b1, uint8_t b2,
 2438     uint8_t b3, int slp)
 2439 {
 2440         int err, amt;
 2441         char *sdata;
 2442         struct scfg *cc = ssc->ses_private;
 2443         static char cdb[10] = { WRITE_BUFFER, 1, 0, 0, 0, 0, 0, 0, 16, 0 };
 2444 
 2445         if (cc == NULL)
 2446                 return (0);
 2447 
 2448         sdata = SES_MALLOC(16);
 2449         if (sdata == NULL)
 2450                 return (ENOMEM);
 2451 
 2452         SES_DLOG(ssc, "saf_wrbuf16 %x %x %x %x\n", op, b1, b2, b3);
 2453 
 2454         sdata[0] = op;
 2455         sdata[1] = b1;
 2456         sdata[2] = b2;
 2457         sdata[3] = b3;
 2458         MEMZERO(&sdata[4], 12);
 2459         amt = -16;
 2460         err = ses_runcmd(ssc, cdb, 10, sdata, &amt);
 2461         SES_FREE(sdata, 16);
 2462         return (err);
 2463 }
 2464 
 2465 /*
 2466  * This function updates the status byte for the device slot described.
 2467  *
 2468  * Since this is an optional SAF-TE command, there's no point in
 2469  * returning an error.
 2470  */
 2471 static void
 2472 wrslot_stat(ses_softc_t *ssc, int slp)
 2473 {
 2474         int i, amt;
 2475         encobj *ep;
 2476         char cdb[10], *sdata;
 2477         struct scfg *cc = ssc->ses_private;
 2478 
 2479         if (cc == NULL)
 2480                 return;
 2481 
 2482         SES_DLOG(ssc, "saf_wrslot\n");
 2483         cdb[0] = WRITE_BUFFER;
 2484         cdb[1] = 1;
 2485         cdb[2] = 0;
 2486         cdb[3] = 0;
 2487         cdb[4] = 0;
 2488         cdb[5] = 0;
 2489         cdb[6] = 0;
 2490         cdb[7] = 0;
 2491         cdb[8] = cc->Nslots * 3 + 1;
 2492         cdb[9] = 0;
 2493 
 2494         sdata = SES_MALLOC(cc->Nslots * 3 + 1);
 2495         if (sdata == NULL)
 2496                 return;
 2497         MEMZERO(sdata, cc->Nslots * 3 + 1);
 2498 
 2499         sdata[0] = SAFTE_WT_DSTAT;
 2500         for (i = 0; i < cc->Nslots; i++) {
 2501                 ep = &ssc->ses_objmap[cc->slotoff + i];
 2502                 SES_DLOG(ssc, "saf_wrslot %d <- %x\n", i, ep->priv & 0xff);
 2503                 sdata[1 + (3 * i)] = ep->priv & 0xff;
 2504         }
 2505         amt = -(cc->Nslots * 3 + 1);
 2506         (void) ses_runcmd(ssc, cdb, 10, sdata, &amt);
 2507         SES_FREE(sdata, cc->Nslots * 3 + 1);
 2508 }
 2509 
 2510 /*
 2511  * This function issues the "PERFORM SLOT OPERATION" command.
 2512  */
 2513 static int
 2514 perf_slotop(ses_softc_t *ssc, uint8_t slot, uint8_t opflag, int slp)
 2515 {
 2516         int err, amt;
 2517         char *sdata;
 2518         struct scfg *cc = ssc->ses_private;
 2519         static char cdb[10] =
 2520             { WRITE_BUFFER, 1, 0, 0, 0, 0, 0, 0, SAFT_SCRATCH, 0 };
 2521 
 2522         if (cc == NULL)
 2523                 return (0);
 2524 
 2525         sdata = SES_MALLOC(SAFT_SCRATCH);
 2526         if (sdata == NULL)
 2527                 return (ENOMEM);
 2528         MEMZERO(sdata, SAFT_SCRATCH);
 2529 
 2530         sdata[0] = SAFTE_WT_SLTOP;
 2531         sdata[1] = slot;
 2532         sdata[2] = opflag;
 2533         SES_DLOG(ssc, "saf_slotop slot %d op %x\n", slot, opflag);
 2534         amt = -SAFT_SCRATCH;
 2535         err = ses_runcmd(ssc, cdb, 10, sdata, &amt);
 2536         SES_FREE(sdata, SAFT_SCRATCH);
 2537         return (err);
 2538 }

Cache object: ceade37b379a8f5bf5cd0c1ff606a433


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.