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