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