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