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