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