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