2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2000 Matthew Jacob
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
35 #include <sys/errno.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/mutex.h>
39 #include <sys/queue.h>
41 #include <sys/systm.h>
42 #include <sys/sysctl.h>
43 #include <sys/types.h>
46 #include <cam/cam_ccb.h>
47 #include <cam/cam_periph.h>
49 #include <cam/scsi/scsi_enc.h>
50 #include <cam/scsi/scsi_enc_internal.h>
51 #include <cam/scsi/scsi_message.h>
54 * SAF-TE Type Device Emulation
57 static int safte_set_enc_status(enc_softc_t *enc, uint8_t encstat, int slpflag);
59 #define ALL_ENC_STAT (SES_ENCSTAT_CRITICAL | SES_ENCSTAT_UNRECOV | \
60 SES_ENCSTAT_NONCRITICAL | SES_ENCSTAT_INFO)
62 * SAF-TE specific defines- Mandatory ones only...
66 * READ BUFFER ('get' commands) IDs- placed in offset 2 of cdb
68 #define SAFTE_RD_RDCFG 0x00 /* read enclosure configuration */
69 #define SAFTE_RD_RDESTS 0x01 /* read enclosure status */
70 #define SAFTE_RD_RDDSTS 0x04 /* read drive slot status */
71 #define SAFTE_RD_RDGFLG 0x05 /* read global flags */
74 * WRITE BUFFER ('set' commands) IDs- placed in offset 0 of databuf
76 #define SAFTE_WT_DSTAT 0x10 /* write device slot status */
77 #define SAFTE_WT_SLTOP 0x12 /* perform slot operation */
78 #define SAFTE_WT_FANSPD 0x13 /* set fan speed */
79 #define SAFTE_WT_ACTPWS 0x14 /* turn on/off power supply */
80 #define SAFTE_WT_GLOBAL 0x15 /* send global command */
82 #define SAFT_SCRATCH 64
87 SAFTE_UPDATE_READCONFIG,
88 SAFTE_UPDATE_READGFLAGS,
89 SAFTE_UPDATE_READENCSTATUS,
90 SAFTE_UPDATE_READSLOTSTATUS,
91 SAFTE_PROCESS_CONTROL_REQS,
92 SAFTE_NUM_UPDATE_STATES
93 } safte_update_action;
95 static fsm_fill_handler_t safte_fill_read_buf_io;
96 static fsm_fill_handler_t safte_fill_control_request;
97 static fsm_done_handler_t safte_process_config;
98 static fsm_done_handler_t safte_process_gflags;
99 static fsm_done_handler_t safte_process_status;
100 static fsm_done_handler_t safte_process_slotstatus;
101 static fsm_done_handler_t safte_process_control_request;
103 static struct enc_fsm_state enc_fsm_states[SAFTE_NUM_UPDATE_STATES] =
105 { "SAFTE_UPDATE_NONE", 0, 0, 0, NULL, NULL, NULL },
107 "SAFTE_UPDATE_READCONFIG",
111 safte_fill_read_buf_io,
112 safte_process_config,
116 "SAFTE_UPDATE_READGFLAGS",
120 safte_fill_read_buf_io,
121 safte_process_gflags,
125 "SAFTE_UPDATE_READENCSTATUS",
129 safte_fill_read_buf_io,
130 safte_process_status,
134 "SAFTE_UPDATE_READSLOTSTATUS",
138 safte_fill_read_buf_io,
139 safte_process_slotstatus,
143 "SAFTE_PROCESS_CONTROL_REQS",
147 safte_fill_control_request,
148 safte_process_control_request,
153 typedef struct safte_control_request {
157 TAILQ_ENTRY(safte_control_request) links;
158 } safte_control_request_t;
159 TAILQ_HEAD(safte_control_reqlist, safte_control_request);
160 typedef struct safte_control_reqlist safte_control_reqlist_t;
162 SES_SETSTATUS_ENC_IDX = -1
166 safte_terminate_control_requests(safte_control_reqlist_t *reqlist, int result)
168 safte_control_request_t *req;
170 while ((req = TAILQ_FIRST(reqlist)) != NULL) {
171 TAILQ_REMOVE(reqlist, req, links);
172 req->result = result;
179 * Cached Configuration
181 uint8_t Nfans; /* Number of Fans */
182 uint8_t Npwr; /* Number of Power Supplies */
183 uint8_t Nslots; /* Number of Device Slots */
184 uint8_t DoorLock; /* Door Lock Installed */
185 uint8_t Ntherm; /* Number of Temperature Sensors */
186 uint8_t Nspkrs; /* Number of Speakers */
187 uint8_t Ntstats; /* Number of Thermostats */
189 * Cached Flag Bytes for Global Status
194 * What object index ID is where various slots start.
198 #define SAFT_ALARM_OFFSET(cc) (cc)->slotoff - 1
200 encioc_enc_status_t adm_status;
201 encioc_enc_status_t enc_status;
202 encioc_enc_status_t slot_status;
204 safte_control_reqlist_t requests;
205 safte_control_request_t *current_request;
206 int current_request_stage;
207 int current_request_stages;
210 #define SAFT_FLG1_ALARM 0x1
211 #define SAFT_FLG1_GLOBFAIL 0x2
212 #define SAFT_FLG1_GLOBWARN 0x4
213 #define SAFT_FLG1_ENCPWROFF 0x8
214 #define SAFT_FLG1_ENCFANFAIL 0x10
215 #define SAFT_FLG1_ENCPWRFAIL 0x20
216 #define SAFT_FLG1_ENCDRVFAIL 0x40
217 #define SAFT_FLG1_ENCDRVWARN 0x80
219 #define SAFT_FLG2_LOCKDOOR 0x4
220 #define SAFT_PRIVATE sizeof (struct scfg)
222 static char *safte_2little = "Too Little Data Returned (%d) at line %d\n";
223 #define SAFT_BAIL(r, x) \
225 ENC_VLOG(enc, safte_2little, x, __LINE__);\
229 int emulate_array_devices = 1;
230 SYSCTL_DECL(_kern_cam_enc);
231 SYSCTL_INT(_kern_cam_enc, OID_AUTO, emulate_array_devices, CTLFLAG_RWTUN,
232 &emulate_array_devices, 0, "Emulate Array Devices for SAF-TE");
235 safte_fill_read_buf_io(enc_softc_t *enc, struct enc_fsm_state *state,
236 union ccb *ccb, uint8_t *buf)
239 if (state->page_code != SAFTE_RD_RDCFG &&
240 enc->enc_cache.nelms == 0) {
241 enc_update_request(enc, SAFTE_UPDATE_READCONFIG);
245 if (enc->enc_type == ENC_SEMB_SAFT) {
246 semb_read_buffer(&ccb->ataio, /*retries*/5,
247 NULL, MSG_SIMPLE_Q_TAG,
248 state->page_code, buf, state->buf_size,
251 scsi_read_buffer(&ccb->csio, /*retries*/5,
252 NULL, MSG_SIMPLE_Q_TAG, 1,
253 state->page_code, 0, buf, state->buf_size,
254 SSD_FULL_SIZE, state->timeout);
260 safte_process_config(enc_softc_t *enc, struct enc_fsm_state *state,
261 union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
264 uint8_t *buf = *bufp;
267 cfg = enc->enc_private;
273 ENC_VLOG(enc, "too little data (%d) for configuration\n",
279 cfg->Nslots = buf[2];
280 cfg->DoorLock = buf[3];
281 cfg->Ntherm = buf[4];
282 cfg->Nspkrs = buf[5];
284 cfg->Ntstats = buf[6] & 0x0f;
287 ENC_VLOG(enc, "Nfans %d Npwr %d Nslots %d Lck %d Ntherm %d Nspkrs %d "
289 cfg->Nfans, cfg->Npwr, cfg->Nslots, cfg->DoorLock, cfg->Ntherm,
290 cfg->Nspkrs, cfg->Ntstats);
292 enc->enc_cache.nelms = cfg->Nfans + cfg->Npwr + cfg->Nslots +
293 cfg->DoorLock + cfg->Ntherm + cfg->Nspkrs + cfg->Ntstats + 1;
294 ENC_FREE_AND_NULL(enc->enc_cache.elm_map);
295 enc->enc_cache.elm_map =
296 malloc(enc->enc_cache.nelms * sizeof(enc_element_t),
297 M_SCSIENC, M_WAITOK|M_ZERO);
301 * Note that this is all arranged for the convenience
302 * in later fetches of status.
304 for (i = 0; i < cfg->Nfans; i++)
305 enc->enc_cache.elm_map[r++].enctype = ELMTYP_FAN;
306 cfg->pwroff = (uint8_t) r;
307 for (i = 0; i < cfg->Npwr; i++)
308 enc->enc_cache.elm_map[r++].enctype = ELMTYP_POWER;
309 for (i = 0; i < cfg->DoorLock; i++)
310 enc->enc_cache.elm_map[r++].enctype = ELMTYP_DOORLOCK;
312 enc->enc_cache.elm_map[r++].enctype = ELMTYP_ALARM;
313 for (i = 0; i < cfg->Ntherm; i++)
314 enc->enc_cache.elm_map[r++].enctype = ELMTYP_THERM;
315 for (i = 0; i <= cfg->Ntstats; i++)
316 enc->enc_cache.elm_map[r++].enctype = ELMTYP_THERM;
317 cfg->slotoff = (uint8_t) r;
318 for (i = 0; i < cfg->Nslots; i++)
319 enc->enc_cache.elm_map[r++].enctype =
320 emulate_array_devices ? ELMTYP_ARRAY_DEV :
323 enc_update_request(enc, SAFTE_UPDATE_READGFLAGS);
324 enc_update_request(enc, SAFTE_UPDATE_READENCSTATUS);
325 enc_update_request(enc, SAFTE_UPDATE_READSLOTSTATUS);
331 safte_process_gflags(enc_softc_t *enc, struct enc_fsm_state *state,
332 union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
335 uint8_t *buf = *bufp;
337 cfg = enc->enc_private;
342 SAFT_BAIL(3, xfer_len);
347 if (cfg->flag1 & SAFT_FLG1_GLOBFAIL)
348 cfg->adm_status |= SES_ENCSTAT_CRITICAL;
349 else if (cfg->flag1 & SAFT_FLG1_GLOBWARN)
350 cfg->adm_status |= SES_ENCSTAT_NONCRITICAL;
356 safte_process_status(enc_softc_t *enc, struct enc_fsm_state *state,
357 union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
360 uint8_t *buf = *bufp;
361 int oid, r, i, nitems;
363 enc_cache_t *cache = &enc->enc_cache;
365 cfg = enc->enc_private;
374 for (nitems = i = 0; i < cfg->Nfans; i++) {
375 SAFT_BAIL(r, xfer_len);
377 * 0 = Fan Operational
378 * 1 = Fan is malfunctioning
379 * 2 = Fan is not present
380 * 0x80 = Unknown or Not Reportable Status
382 cache->elm_map[oid].encstat[1] = 0; /* resvd */
383 cache->elm_map[oid].encstat[2] = 0; /* resvd */
384 if (cfg->flag1 & SAFT_FLG1_ENCFANFAIL)
385 cache->elm_map[oid].encstat[3] |= 0x40;
387 cache->elm_map[oid].encstat[3] &= ~0x40;
388 switch ((int)buf[r]) {
391 cache->elm_map[oid].encstat[0] = SES_OBJSTAT_OK;
392 if ((cache->elm_map[oid].encstat[3] & 0x37) == 0)
393 cache->elm_map[oid].encstat[3] |= 0x27;
397 cache->elm_map[oid].encstat[0] =
400 * FAIL and FAN STOPPED synthesized
402 cache->elm_map[oid].encstat[3] |= 0x10;
403 cache->elm_map[oid].encstat[3] &= ~0x07;
405 * Enclosure marked with CRITICAL error
406 * if only one fan or no thermometers,
407 * else the NONCRITICAL error is set.
409 if (cfg->Nfans == 1 || (cfg->Ntherm + cfg->Ntstats) == 0)
410 cfg->enc_status |= SES_ENCSTAT_CRITICAL;
412 cfg->enc_status |= SES_ENCSTAT_NONCRITICAL;
415 cache->elm_map[oid].encstat[0] =
416 SES_OBJSTAT_NOTINSTALLED;
417 cache->elm_map[oid].encstat[3] |= 0x10;
418 cache->elm_map[oid].encstat[3] &= ~0x07;
420 * Enclosure marked with CRITICAL error
421 * if only one fan or no thermometers,
422 * else the NONCRITICAL error is set.
425 cfg->enc_status |= SES_ENCSTAT_CRITICAL;
427 cfg->enc_status |= SES_ENCSTAT_NONCRITICAL;
430 cache->elm_map[oid].encstat[0] = SES_OBJSTAT_UNKNOWN;
431 cache->elm_map[oid].encstat[3] = 0;
432 cfg->enc_status |= SES_ENCSTAT_INFO;
435 cache->elm_map[oid].encstat[0] = SES_OBJSTAT_UNSUPPORTED;
436 ENC_VLOG(enc, "Unknown fan%d status 0x%x\n", i,
440 cache->elm_map[oid++].svalid = 1;
445 * No matter how you cut it, no cooling elements when there
446 * should be some there is critical.
448 if (cfg->Nfans && nitems == 0)
449 cfg->enc_status |= SES_ENCSTAT_CRITICAL;
451 for (i = 0; i < cfg->Npwr; i++) {
452 SAFT_BAIL(r, xfer_len);
453 cache->elm_map[oid].encstat[0] = SES_OBJSTAT_UNKNOWN;
454 cache->elm_map[oid].encstat[1] = 0; /* resvd */
455 cache->elm_map[oid].encstat[2] = 0; /* resvd */
456 cache->elm_map[oid].encstat[3] = 0x20; /* requested on */
458 case 0x00: /* pws operational and on */
459 cache->elm_map[oid].encstat[0] = SES_OBJSTAT_OK;
461 case 0x01: /* pws operational and off */
462 cache->elm_map[oid].encstat[0] = SES_OBJSTAT_OK;
463 cache->elm_map[oid].encstat[3] = 0x10;
464 cfg->enc_status |= SES_ENCSTAT_INFO;
466 case 0x10: /* pws is malfunctioning and commanded on */
467 cache->elm_map[oid].encstat[0] = SES_OBJSTAT_CRIT;
468 cache->elm_map[oid].encstat[3] = 0x61;
469 cfg->enc_status |= SES_ENCSTAT_NONCRITICAL;
472 case 0x11: /* pws is malfunctioning and commanded off */
473 cache->elm_map[oid].encstat[0] = SES_OBJSTAT_NONCRIT;
474 cache->elm_map[oid].encstat[3] = 0x51;
475 cfg->enc_status |= SES_ENCSTAT_NONCRITICAL;
477 case 0x20: /* pws is not present */
478 cache->elm_map[oid].encstat[0] =
479 SES_OBJSTAT_NOTINSTALLED;
480 cache->elm_map[oid].encstat[3] = 0;
481 cfg->enc_status |= SES_ENCSTAT_INFO;
483 case 0x21: /* pws is present */
485 * This is for enclosures that cannot tell whether the
486 * device is on or malfunctioning, but know that it is
487 * present. Just fall through.
490 case 0x80: /* Unknown or Not Reportable Status */
491 cache->elm_map[oid].encstat[0] = SES_OBJSTAT_UNKNOWN;
492 cache->elm_map[oid].encstat[3] = 0;
493 cfg->enc_status |= SES_ENCSTAT_INFO;
496 ENC_VLOG(enc, "unknown power supply %d status (0x%x)\n",
500 enc->enc_cache.elm_map[oid++].svalid = 1;
507 for (i = 0; i < cfg->Nslots; i++) {
508 SAFT_BAIL(r, xfer_len);
509 if (cache->elm_map[cfg->slotoff + i].enctype == ELMTYP_DEVICE)
510 cache->elm_map[cfg->slotoff + i].encstat[1] = buf[r];
515 * We always have doorlock status, no matter what,
516 * but we only save the status if we have one.
518 SAFT_BAIL(r, xfer_len);
522 * 1 = Door Unlocked, or no Lock Installed
523 * 0x80 = Unknown or Not Reportable Status
525 cache->elm_map[oid].encstat[1] = 0;
526 cache->elm_map[oid].encstat[2] = 0;
529 cache->elm_map[oid].encstat[0] = SES_OBJSTAT_OK;
530 cache->elm_map[oid].encstat[3] = 0;
533 cache->elm_map[oid].encstat[0] = SES_OBJSTAT_OK;
534 cache->elm_map[oid].encstat[3] = 1;
537 cache->elm_map[oid].encstat[0] = SES_OBJSTAT_UNKNOWN;
538 cache->elm_map[oid].encstat[3] = 0;
539 cfg->enc_status |= SES_ENCSTAT_INFO;
542 cache->elm_map[oid].encstat[0] =
543 SES_OBJSTAT_UNSUPPORTED;
544 ENC_VLOG(enc, "unknown lock status 0x%x\n",
548 cache->elm_map[oid++].svalid = 1;
553 * We always have speaker status, no matter what,
554 * but we only save the status if we have one.
556 SAFT_BAIL(r, xfer_len);
558 cache->elm_map[oid].encstat[0] = SES_OBJSTAT_OK;
559 cache->elm_map[oid].encstat[1] = 0;
560 cache->elm_map[oid].encstat[2] = 0;
562 cache->elm_map[oid].encstat[0] |= SESCTL_DISABLE;
563 cache->elm_map[oid].encstat[3] |= 0x40;
565 cache->elm_map[oid++].svalid = 1;
570 * Now, for "pseudo" thermometers, we have two bytes
571 * of information in enclosure status- 16 bits. Actually,
572 * the MSB is a single TEMP ALERT flag indicating whether
573 * any other bits are set, but, thanks to fuzzy thinking,
574 * in the SAF-TE spec, this can also be set even if no
575 * other bits are set, thus making this really another
576 * binary temperature sensor.
579 SAFT_BAIL(r + cfg->Ntherm, xfer_len);
580 tempflags = buf[r + cfg->Ntherm];
581 SAFT_BAIL(r + cfg->Ntherm + 1, xfer_len);
582 tempflags |= (tempflags << 8) | buf[r + cfg->Ntherm + 1];
584 for (i = 0; i < cfg->Ntherm; i++) {
585 SAFT_BAIL(r, xfer_len);
587 * Status is a range from -10 to 245 deg Celsius,
588 * which we need to normalize to -20 to -245 according
589 * to the latest SCSI spec, which makes little
590 * sense since this would overflow an 8bit value.
591 * Well, still, the base normalization is -20,
592 * not -10, so we have to adjust.
594 * So what's over and under temperature?
595 * Hmm- we'll state that 'normal' operating
596 * is 10 to 40 deg Celsius.
600 * Actually.... All of the units that people out in the world
601 * seem to have do not come even close to setting a value that
602 * complies with this spec.
604 * The closest explanation I could find was in an
605 * LSI-Logic manual, which seemed to indicate that
606 * this value would be set by whatever the I2C code
607 * would interpolate from the output of an LM75
608 * temperature sensor.
610 * This means that it is impossible to use the actual
611 * numeric value to predict anything. But we don't want
612 * to lose the value. So, we'll propagate the *uncorrected*
613 * value and set SES_OBJSTAT_NOTAVAIL. We'll depend on the
614 * temperature flags for warnings.
616 if (tempflags & (1 << i)) {
617 cache->elm_map[oid].encstat[0] = SES_OBJSTAT_CRIT;
618 cfg->enc_status |= SES_ENCSTAT_CRITICAL;
620 cache->elm_map[oid].encstat[0] = SES_OBJSTAT_OK;
621 cache->elm_map[oid].encstat[1] = 0;
622 cache->elm_map[oid].encstat[2] = buf[r];
623 cache->elm_map[oid].encstat[3] = 0;
624 cache->elm_map[oid++].svalid = 1;
628 for (i = 0; i <= cfg->Ntstats; i++) {
629 cache->elm_map[oid].encstat[1] = 0;
630 if (tempflags & (1 <<
631 ((i == cfg->Ntstats) ? 15 : (cfg->Ntherm + i)))) {
632 cache->elm_map[oid].encstat[0] = SES_OBJSTAT_CRIT;
633 cache->elm_map[4].encstat[2] = 0xff;
635 * Set 'over temperature' failure.
637 cache->elm_map[oid].encstat[3] = 8;
638 cfg->enc_status |= SES_ENCSTAT_CRITICAL;
641 * We used to say 'not available' and synthesize a
642 * nominal 30 deg (C)- that was wrong. Actually,
643 * Just say 'OK', and use the reserved value of
646 if ((cfg->Ntherm + cfg->Ntstats) == 0)
647 cache->elm_map[oid].encstat[0] =
648 SES_OBJSTAT_NOTAVAIL;
650 cache->elm_map[oid].encstat[0] =
652 cache->elm_map[oid].encstat[2] = 0;
653 cache->elm_map[oid].encstat[3] = 0;
655 cache->elm_map[oid++].svalid = 1;
660 cfg->enc_status | cfg->slot_status | cfg->adm_status;
665 safte_process_slotstatus(enc_softc_t *enc, struct enc_fsm_state *state,
666 union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
669 uint8_t *buf = *bufp;
670 enc_cache_t *cache = &enc->enc_cache;
673 cfg = enc->enc_private;
678 cfg->slot_status = 0;
680 for (r = i = 0; i < cfg->Nslots; i++, r += 4) {
681 SAFT_BAIL(r+3, xfer_len);
682 if (cache->elm_map[oid].enctype == ELMTYP_ARRAY_DEV)
683 cache->elm_map[oid].encstat[1] = 0;
684 cache->elm_map[oid].encstat[2] &= SESCTL_RQSID;
685 cache->elm_map[oid].encstat[3] = 0;
686 if ((buf[r+3] & 0x01) == 0) { /* no device */
687 cache->elm_map[oid].encstat[0] = SES_OBJSTAT_NOTINSTALLED;
688 } else if (buf[r+0] & 0x02) {
689 cache->elm_map[oid].encstat[0] = SES_OBJSTAT_CRIT;
690 cfg->slot_status |= SES_ENCSTAT_CRITICAL;
691 } else if (buf[r+0] & 0x40) {
692 cache->elm_map[oid].encstat[0] = SES_OBJSTAT_NONCRIT;
693 cfg->slot_status |= SES_ENCSTAT_NONCRITICAL;
695 cache->elm_map[oid].encstat[0] = SES_OBJSTAT_OK;
697 if (buf[r+3] & 0x2) {
699 cache->elm_map[oid].encstat[2] |= SESCTL_RQSRMV;
701 cache->elm_map[oid].encstat[2] |= SESCTL_RQSINS;
703 if ((buf[r+3] & 0x04) == 0)
704 cache->elm_map[oid].encstat[3] |= SESCTL_DEVOFF;
706 cache->elm_map[oid].encstat[3] |= SESCTL_RQSFLT;
708 cache->elm_map[oid].encstat[0] |= SESCTL_PRDFAIL;
709 if (cache->elm_map[oid].enctype == ELMTYP_ARRAY_DEV) {
711 cache->elm_map[oid].encstat[1] |= 0x80;
713 cache->elm_map[oid].encstat[1] |= 0x02;
715 cache->elm_map[oid].encstat[1] |= 0x04;
717 cache->elm_map[oid].encstat[1] |= 0x08;
719 cache->elm_map[oid].encstat[1] |= 0x10;
721 cache->elm_map[oid].encstat[1] |= 0x20;
723 cache->elm_map[oid].encstat[1] |= 0x01;
725 cache->elm_map[oid++].svalid = 1;
729 cfg->enc_status | cfg->slot_status | cfg->adm_status;
734 safte_fill_control_request(enc_softc_t *enc, struct enc_fsm_state *state,
735 union ccb *ccb, uint8_t *buf)
738 enc_element_t *ep, *ep1;
739 safte_control_request_t *req;
740 int i, idx, xfer_len;
742 cfg = enc->enc_private;
746 if (enc->enc_cache.nelms == 0) {
747 enc_update_request(enc, SAFTE_UPDATE_READCONFIG);
751 if (cfg->current_request == NULL) {
752 cfg->current_request = TAILQ_FIRST(&cfg->requests);
753 TAILQ_REMOVE(&cfg->requests, cfg->current_request, links);
754 cfg->current_request_stage = 0;
755 cfg->current_request_stages = 1;
757 req = cfg->current_request;
759 idx = (int)req->elm_idx;
760 if (req->elm_idx == SES_SETSTATUS_ENC_IDX) {
761 cfg->adm_status = req->elm_stat[0] & ALL_ENC_STAT;
762 cfg->flag1 &= ~(SAFT_FLG1_GLOBFAIL|SAFT_FLG1_GLOBWARN);
763 if (req->elm_stat[0] & (SES_ENCSTAT_CRITICAL|SES_ENCSTAT_UNRECOV))
764 cfg->flag1 |= SAFT_FLG1_GLOBFAIL;
765 else if (req->elm_stat[0] & SES_ENCSTAT_NONCRITICAL)
766 cfg->flag1 |= SAFT_FLG1_GLOBWARN;
767 buf[0] = SAFTE_WT_GLOBAL;
773 ep = &enc->enc_cache.elm_map[idx];
775 switch (ep->enctype) {
777 case ELMTYP_ARRAY_DEV:
778 switch (cfg->current_request_stage) {
781 if (req->elm_stat[0] & SESCTL_PRDFAIL)
783 if (req->elm_stat[3] & SESCTL_RQSFLT)
785 if (ep->enctype == ELMTYP_ARRAY_DEV) {
786 if (req->elm_stat[1] & 0x01)
788 if (req->elm_stat[1] & 0x02)
790 if (req->elm_stat[1] & 0x04)
792 if (req->elm_stat[1] & 0x08)
794 if (req->elm_stat[1] & 0x10)
796 if (req->elm_stat[1] & 0x20)
798 if (req->elm_stat[1] & 0x80)
802 ep->priv |= 0x01; /* no errors */
804 buf[0] = SAFTE_WT_DSTAT;
805 for (i = 0; i < cfg->Nslots; i++) {
806 ep1 = &enc->enc_cache.elm_map[cfg->slotoff + i];
807 buf[1 + (3 * i)] = ep1->priv;
808 buf[2 + (3 * i)] = ep1->priv >> 8;
810 xfer_len = cfg->Nslots * 3 + 1;
811 #define DEVON(x) (!(((x)[2] & SESCTL_RQSINS) | \
812 ((x)[2] & SESCTL_RQSRMV) | \
813 ((x)[3] & SESCTL_DEVOFF)))
814 if (DEVON(req->elm_stat) != DEVON(ep->encstat))
815 cfg->current_request_stages++;
816 #define IDON(x) (!!((x)[2] & SESCTL_RQSID))
817 if (IDON(req->elm_stat) != IDON(ep->encstat))
818 cfg->current_request_stages++;
822 buf[0] = SAFTE_WT_SLTOP;
823 buf[1] = idx - cfg->slotoff;
824 if (cfg->current_request_stage == 1 &&
825 DEVON(req->elm_stat) != DEVON(ep->encstat)) {
826 if (DEVON(req->elm_stat))
831 if (IDON(req->elm_stat))
835 ep->encstat[2] &= ~SESCTL_RQSID;
836 ep->encstat[2] |= req->elm_stat[2] &
846 cfg->current_request_stages = 2;
847 switch (cfg->current_request_stage) {
849 if (req->elm_stat[3] & SESCTL_RQSTFAIL) {
850 cfg->flag1 |= SAFT_FLG1_ENCPWRFAIL;
852 cfg->flag1 &= ~SAFT_FLG1_ENCPWRFAIL;
854 buf[0] = SAFTE_WT_GLOBAL;
861 buf[0] = SAFTE_WT_ACTPWS;
862 buf[1] = idx - cfg->pwroff;
863 if (req->elm_stat[3] & SESCTL_RQSTON)
874 if ((req->elm_stat[3] & 0x7) != 0)
875 cfg->current_request_stages = 2;
876 switch (cfg->current_request_stage) {
878 if (req->elm_stat[3] & SESCTL_RQSTFAIL)
879 cfg->flag1 |= SAFT_FLG1_ENCFANFAIL;
881 cfg->flag1 &= ~SAFT_FLG1_ENCFANFAIL;
882 buf[0] = SAFTE_WT_GLOBAL;
889 buf[0] = SAFTE_WT_FANSPD;
891 if (req->elm_stat[3] & SESCTL_RQSTON) {
892 if ((req->elm_stat[3] & 0x7) == 7)
894 else if ((req->elm_stat[3] & 0x7) >= 5)
896 else if ((req->elm_stat[3] & 0x7) >= 3)
904 ep->encstat[3] = req->elm_stat[3] & 0x67;
909 case ELMTYP_DOORLOCK:
910 if (req->elm_stat[3] & 0x1)
911 cfg->flag2 &= ~SAFT_FLG2_LOCKDOOR;
913 cfg->flag2 |= SAFT_FLG2_LOCKDOOR;
914 buf[0] = SAFTE_WT_GLOBAL;
921 if ((req->elm_stat[0] & SESCTL_DISABLE) ||
922 (req->elm_stat[3] & 0x40)) {
923 cfg->flag2 &= ~SAFT_FLG1_ALARM;
924 } else if ((req->elm_stat[3] & 0x0f) != 0) {
925 cfg->flag2 |= SAFT_FLG1_ALARM;
927 cfg->flag2 &= ~SAFT_FLG1_ALARM;
929 buf[0] = SAFTE_WT_GLOBAL;
934 ep->encstat[3] = req->elm_stat[3];
941 if (enc->enc_type == ENC_SEMB_SAFT) {
942 semb_write_buffer(&ccb->ataio, /*retries*/5,
943 NULL, MSG_SIMPLE_Q_TAG,
944 buf, xfer_len, state->timeout);
946 scsi_write_buffer(&ccb->csio, /*retries*/5,
947 NULL, MSG_SIMPLE_Q_TAG, 1,
949 SSD_FULL_SIZE, state->timeout);
955 safte_process_control_request(enc_softc_t *enc, struct enc_fsm_state *state,
956 union ccb *ccb, uint8_t **bufp, int error, int xfer_len)
959 safte_control_request_t *req;
962 cfg = enc->enc_private;
966 req = cfg->current_request;
967 if (req->result == 0)
969 if (++cfg->current_request_stage >= cfg->current_request_stages) {
971 if (idx == SES_SETSTATUS_ENC_IDX)
974 type = enc->enc_cache.elm_map[idx].enctype;
975 if (type == ELMTYP_DEVICE || type == ELMTYP_ARRAY_DEV)
976 enc_update_request(enc, SAFTE_UPDATE_READSLOTSTATUS);
978 enc_update_request(enc, SAFTE_UPDATE_READENCSTATUS);
979 cfg->current_request = NULL;
982 enc_update_request(enc, SAFTE_PROCESS_CONTROL_REQS);
988 safte_softc_invalidate(enc_softc_t *enc)
992 cfg = enc->enc_private;
993 safte_terminate_control_requests(&cfg->requests, ENXIO);
997 safte_softc_cleanup(enc_softc_t *enc)
1000 ENC_FREE_AND_NULL(enc->enc_cache.elm_map);
1001 ENC_FREE_AND_NULL(enc->enc_private);
1002 enc->enc_cache.nelms = 0;
1006 safte_init_enc(enc_softc_t *enc)
1010 static char cdb0[6] = { SEND_DIAGNOSTIC };
1012 cfg = enc->enc_private;
1016 err = enc_runcmd(enc, cdb0, 6, NULL, 0);
1023 err = safte_set_enc_status(enc, 0, 1);
1028 safte_get_enc_status(enc_softc_t *enc, int slpflg)
1035 safte_set_enc_status(enc_softc_t *enc, uint8_t encstat, int slpflag)
1038 safte_control_request_t req;
1040 cfg = enc->enc_private;
1044 req.elm_idx = SES_SETSTATUS_ENC_IDX;
1045 req.elm_stat[0] = encstat & 0xf;
1048 TAILQ_INSERT_TAIL(&cfg->requests, &req, links);
1049 enc_update_request(enc, SAFTE_PROCESS_CONTROL_REQS);
1050 cam_periph_sleep(enc->periph, &req, PUSER, "encstat", 0);
1052 return (req.result);
1056 safte_get_elm_status(enc_softc_t *enc, encioc_elm_status_t *elms, int slpflg)
1058 int i = (int)elms->elm_idx;
1060 elms->cstat[0] = enc->enc_cache.elm_map[i].encstat[0];
1061 elms->cstat[1] = enc->enc_cache.elm_map[i].encstat[1];
1062 elms->cstat[2] = enc->enc_cache.elm_map[i].encstat[2];
1063 elms->cstat[3] = enc->enc_cache.elm_map[i].encstat[3];
1068 safte_set_elm_status(enc_softc_t *enc, encioc_elm_status_t *elms, int slpflag)
1071 safte_control_request_t req;
1073 cfg = enc->enc_private;
1077 /* If this is clear, we don't do diddly. */
1078 if ((elms->cstat[0] & SESCTL_CSEL) == 0)
1081 req.elm_idx = elms->elm_idx;
1082 memcpy(&req.elm_stat, elms->cstat, sizeof(req.elm_stat));
1085 TAILQ_INSERT_TAIL(&cfg->requests, &req, links);
1086 enc_update_request(enc, SAFTE_PROCESS_CONTROL_REQS);
1087 cam_periph_sleep(enc->periph, &req, PUSER, "encstat", 0);
1089 return (req.result);
1093 safte_poll_status(enc_softc_t *enc)
1096 enc_update_request(enc, SAFTE_UPDATE_READENCSTATUS);
1097 enc_update_request(enc, SAFTE_UPDATE_READSLOTSTATUS);
1100 static struct enc_vec safte_enc_vec =
1102 .softc_invalidate = safte_softc_invalidate,
1103 .softc_cleanup = safte_softc_cleanup,
1104 .init_enc = safte_init_enc,
1105 .get_enc_status = safte_get_enc_status,
1106 .set_enc_status = safte_set_enc_status,
1107 .get_elm_status = safte_get_elm_status,
1108 .set_elm_status = safte_set_elm_status,
1109 .poll_status = safte_poll_status
1113 safte_softc_init(enc_softc_t *enc)
1117 enc->enc_vec = safte_enc_vec;
1118 enc->enc_fsm_states = enc_fsm_states;
1120 if (enc->enc_private == NULL) {
1121 enc->enc_private = ENC_MALLOCZ(SAFT_PRIVATE);
1122 if (enc->enc_private == NULL)
1125 cfg = enc->enc_private;
1127 enc->enc_cache.nelms = 0;
1128 enc->enc_cache.enc_status = 0;
1130 TAILQ_INIT(&cfg->requests);