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