]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/cam/scsi/scsi_enc.c
Rename IPPROTO 33 from SEP to DCCP
[FreeBSD/FreeBSD.git] / sys / cam / scsi / scsi_enc.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33
34 #include <sys/conf.h>
35 #include <sys/errno.h>
36 #include <sys/fcntl.h>
37 #include <sys/kernel.h>
38 #include <sys/kthread.h>
39 #include <sys/lock.h>
40 #include <sys/malloc.h>
41 #include <sys/mutex.h>
42 #include <sys/proc.h>
43 #include <sys/queue.h>
44 #include <sys/sbuf.h>
45 #include <sys/sx.h>
46 #include <sys/sysent.h>
47 #include <sys/systm.h>
48 #include <sys/sysctl.h>
49 #include <sys/types.h>
50
51 #include <machine/stdarg.h>
52
53 #include <cam/cam.h>
54 #include <cam/cam_ccb.h>
55 #include <cam/cam_debug.h>
56 #include <cam/cam_periph.h>
57 #include <cam/cam_xpt_periph.h>
58
59 #include <cam/scsi/scsi_all.h>
60 #include <cam/scsi/scsi_message.h>
61 #include <cam/scsi/scsi_enc.h>
62 #include <cam/scsi/scsi_enc_internal.h>
63
64 #include "opt_ses.h"
65
66 MALLOC_DEFINE(M_SCSIENC, "SCSI ENC", "SCSI ENC buffers");
67
68 /* Enclosure type independent driver */
69
70 static  d_open_t        enc_open;
71 static  d_close_t       enc_close;
72 static  d_ioctl_t       enc_ioctl;
73 static  periph_init_t   enc_init;
74 static  periph_ctor_t   enc_ctor;
75 static  periph_oninv_t  enc_oninvalidate;
76 static  periph_dtor_t   enc_dtor;
77
78 static void enc_async(void *, uint32_t, struct cam_path *, void *);
79 static enctyp enc_type(struct ccb_getdev *);
80
81 SYSCTL_NODE(_kern_cam, OID_AUTO, enc, CTLFLAG_RD, 0,
82             "CAM Enclosure Services driver");
83
84 #if defined(DEBUG) || defined(ENC_DEBUG)
85 int enc_verbose = 1;
86 #else
87 int enc_verbose = 0;
88 #endif
89 SYSCTL_INT(_kern_cam_enc, OID_AUTO, verbose, CTLFLAG_RWTUN,
90            &enc_verbose, 0, "Enable verbose logging");
91
92 const char *elm_type_names[] = ELM_TYPE_NAMES;
93 CTASSERT(nitems(elm_type_names) - 1 == ELMTYP_LAST);
94
95 static struct periph_driver encdriver = {
96         enc_init, "ses",
97         TAILQ_HEAD_INITIALIZER(encdriver.units), /* generation */ 0
98 };
99
100 PERIPHDRIVER_DECLARE(enc, encdriver);
101
102 static struct cdevsw enc_cdevsw = {
103         .d_version =    D_VERSION,
104         .d_open =       enc_open,
105         .d_close =      enc_close,
106         .d_ioctl =      enc_ioctl,
107         .d_name =       "ses",
108         .d_flags =      D_TRACKCLOSE,
109 };
110
111 static void
112 enc_init(void)
113 {
114         cam_status status;
115
116         /*
117          * Install a global async callback.  This callback will
118          * receive async callbacks like "new device found".
119          */
120         status = xpt_register_async(AC_FOUND_DEVICE, enc_async, NULL, NULL);
121
122         if (status != CAM_REQ_CMP) {
123                 printf("enc: Failed to attach master async callback "
124                        "due to status 0x%x!\n", status);
125         }
126 }
127
128 static void
129 enc_devgonecb(void *arg)
130 {
131         struct cam_periph *periph;
132         struct enc_softc  *enc;
133         struct mtx *mtx;
134         int i;
135
136         periph = (struct cam_periph *)arg;
137         mtx = cam_periph_mtx(periph);
138         mtx_lock(mtx);
139         enc = (struct enc_softc *)periph->softc;
140
141         /*
142          * When we get this callback, we will get no more close calls from
143          * devfs.  So if we have any dangling opens, we need to release the
144          * reference held for that particular context.
145          */
146         for (i = 0; i < enc->open_count; i++)
147                 cam_periph_release_locked(periph);
148
149         enc->open_count = 0;
150
151         /*
152          * Release the reference held for the device node, it is gone now.
153          */
154         cam_periph_release_locked(periph);
155
156         /*
157          * We reference the lock directly here, instead of using
158          * cam_periph_unlock().  The reason is that the final call to
159          * cam_periph_release_locked() above could result in the periph
160          * getting freed.  If that is the case, dereferencing the periph
161          * with a cam_periph_unlock() call would cause a page fault.
162          */
163         mtx_unlock(mtx);
164 }
165
166 static void
167 enc_oninvalidate(struct cam_periph *periph)
168 {
169         struct enc_softc *enc;
170
171         enc = periph->softc;
172
173         enc->enc_flags |= ENC_FLAG_INVALID;
174
175         /* If the sub-driver has an invalidate routine, call it */
176         if (enc->enc_vec.softc_invalidate != NULL)
177                 enc->enc_vec.softc_invalidate(enc);
178
179         /*
180          * Unregister any async callbacks.
181          */
182         xpt_register_async(0, enc_async, periph, periph->path);
183
184         /*
185          * Shutdown our daemon.
186          */
187         enc->enc_flags |= ENC_FLAG_SHUTDOWN;
188         if (enc->enc_daemon != NULL) {
189                 /* Signal the ses daemon to terminate. */
190                 wakeup(enc->enc_daemon);
191         }
192         callout_drain(&enc->status_updater);
193
194         destroy_dev_sched_cb(enc->enc_dev, enc_devgonecb, periph);
195 }
196
197 static void
198 enc_dtor(struct cam_periph *periph)
199 {
200         struct enc_softc *enc;
201
202         enc = periph->softc;
203
204         /* If the sub-driver has a cleanup routine, call it */
205         if (enc->enc_vec.softc_cleanup != NULL)
206                 enc->enc_vec.softc_cleanup(enc);
207
208         if (enc->enc_boot_hold_ch.ich_func != NULL) {
209                 config_intrhook_disestablish(&enc->enc_boot_hold_ch);
210                 enc->enc_boot_hold_ch.ich_func = NULL;
211         }
212
213         ENC_FREE(enc);
214 }
215
216 static void
217 enc_async(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
218 {
219         struct cam_periph *periph;
220
221         periph = (struct cam_periph *)callback_arg;
222
223         switch(code) {
224         case AC_FOUND_DEVICE:
225         {
226                 struct ccb_getdev *cgd;
227                 cam_status status;
228                 path_id_t path_id;
229
230                 cgd = (struct ccb_getdev *)arg;
231                 if (arg == NULL) {
232                         break;
233                 }
234
235                 if (enc_type(cgd) == ENC_NONE) {
236                         /*
237                          * Schedule announcement of the ENC bindings for
238                          * this device if it is managed by a SEP.
239                          */
240                         path_id = xpt_path_path_id(path);
241                         xpt_lock_buses();
242                         TAILQ_FOREACH(periph, &encdriver.units, unit_links) {
243                                 struct enc_softc *softc;
244
245                                 softc = (struct enc_softc *)periph->softc;
246
247                                 /* Check this SEP is ready. */
248                                 if (softc == NULL || (softc->enc_flags &
249                                      ENC_FLAG_INITIALIZED) == 0 ||
250                                     softc->enc_vec.device_found == NULL)
251                                         continue;
252
253                                 /* Check this SEP may manage this device. */
254                                 if (xpt_path_path_id(periph->path) != path_id &&
255                                     (softc->enc_type != ENC_SEMB_SES ||
256                                      cgd->protocol != PROTO_ATA))
257                                         continue;
258
259                                 softc->enc_vec.device_found(softc);
260                         }
261                         xpt_unlock_buses();
262                         return;
263                 }
264
265                 status = cam_periph_alloc(enc_ctor, enc_oninvalidate,
266                     enc_dtor, NULL, "ses", CAM_PERIPH_BIO,
267                     path, enc_async, AC_FOUND_DEVICE, cgd);
268
269                 if (status != CAM_REQ_CMP && status != CAM_REQ_INPROG) {
270                         printf("enc_async: Unable to probe new device due to "
271                             "status 0x%x\n", status);
272                 }
273                 break;
274         }
275         default:
276                 cam_periph_async(periph, code, path, arg);
277                 break;
278         }
279 }
280
281 static int
282 enc_open(struct cdev *dev, int flags, int fmt, struct thread *td)
283 {
284         struct cam_periph *periph;
285         struct enc_softc *softc;
286         int error = 0;
287
288         periph = (struct cam_periph *)dev->si_drv1;
289         if (cam_periph_acquire(periph) != 0)
290                 return (ENXIO);
291
292         cam_periph_lock(periph);
293
294         softc = (struct enc_softc *)periph->softc;
295
296         if ((softc->enc_flags & ENC_FLAG_INITIALIZED) == 0) {
297                 error = ENXIO;
298                 goto out;
299         }
300         if (softc->enc_flags & ENC_FLAG_INVALID) {
301                 error = ENXIO;
302                 goto out;
303         }
304 out:
305         if (error != 0)
306                 cam_periph_release_locked(periph);
307         else
308                 softc->open_count++;
309
310         cam_periph_unlock(periph);
311
312         return (error);
313 }
314
315 static int
316 enc_close(struct cdev *dev, int flag, int fmt, struct thread *td)
317 {
318         struct cam_periph *periph;
319         struct enc_softc  *enc;
320         struct mtx *mtx;
321
322         periph = (struct cam_periph *)dev->si_drv1;
323         mtx = cam_periph_mtx(periph);
324         mtx_lock(mtx);
325
326         enc = periph->softc;
327         enc->open_count--;
328
329         cam_periph_release_locked(periph);
330
331         /*
332          * We reference the lock directly here, instead of using
333          * cam_periph_unlock().  The reason is that the call to
334          * cam_periph_release_locked() above could result in the periph
335          * getting freed.  If that is the case, dereferencing the periph
336          * with a cam_periph_unlock() call would cause a page fault.
337          *
338          * cam_periph_release() avoids this problem using the same method,
339          * but we're manually acquiring and dropping the lock here to
340          * protect the open count and avoid another lock acquisition and
341          * release.
342          */
343         mtx_unlock(mtx);
344
345         return (0);
346 }
347
348 int
349 enc_error(union ccb *ccb, uint32_t cflags, uint32_t sflags)
350 {
351         struct enc_softc *softc;
352         struct cam_periph *periph;
353
354         periph = xpt_path_periph(ccb->ccb_h.path);
355         softc = (struct enc_softc *)periph->softc;
356
357         return (cam_periph_error(ccb, cflags, sflags));
358 }
359
360 static int
361 enc_ioctl(struct cdev *dev, u_long cmd, caddr_t arg_addr, int flag,
362          struct thread *td)
363 {
364         struct cam_periph *periph;
365         encioc_enc_status_t tmp;
366         encioc_string_t sstr;
367         encioc_elm_status_t elms;
368         encioc_elm_desc_t elmd;
369         encioc_elm_devnames_t elmdn;
370         encioc_element_t *uelm;
371         enc_softc_t *enc;
372         enc_cache_t *cache;
373         void *addr;
374         int error, i;
375
376 #ifdef  COMPAT_FREEBSD32
377         if (SV_PROC_FLAG(td->td_proc, SV_ILP32))
378                 return (ENOTTY);
379 #endif
380
381         if (arg_addr)
382                 addr = *((caddr_t *) arg_addr);
383         else
384                 addr = NULL;
385
386         periph = (struct cam_periph *)dev->si_drv1;
387         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering encioctl\n"));
388
389         cam_periph_lock(periph);
390         enc = (struct enc_softc *)periph->softc;
391         cache = &enc->enc_cache;
392
393         /*
394          * Now check to see whether we're initialized or not.
395          * This actually should never fail as we're not supposed
396          * to get past enc_open w/o successfully initializing
397          * things.
398          */
399         if ((enc->enc_flags & ENC_FLAG_INITIALIZED) == 0) {
400                 cam_periph_unlock(periph);
401                 return (ENXIO);
402         }
403         cam_periph_unlock(periph);
404
405         error = 0;
406
407         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
408             ("trying to do ioctl %#lx\n", cmd));
409
410         /*
411          * If this command can change the device's state,
412          * we must have the device open for writing.
413          *
414          * For commands that get information about the
415          * device- we don't need to lock the peripheral
416          * if we aren't running a command.  The periph
417          * also can't go away while a user process has
418          * it open.
419          */
420         switch (cmd) {
421         case ENCIOC_GETNELM:
422         case ENCIOC_GETELMMAP:
423         case ENCIOC_GETENCSTAT:
424         case ENCIOC_GETELMSTAT:
425         case ENCIOC_GETELMDESC:
426         case ENCIOC_GETELMDEVNAMES:
427         case ENCIOC_GETENCNAME:
428         case ENCIOC_GETENCID:
429                 break;
430         default:
431                 if ((flag & FWRITE) == 0) {
432                         return (EBADF);
433                 }
434         }
435  
436         /*
437          * XXX The values read here are only valid for the current
438          *     configuration generation.  We need these ioctls
439          *     to also pass in/out a generation number.
440          */
441         sx_slock(&enc->enc_cache_lock);
442         switch (cmd) {
443         case ENCIOC_GETNELM:
444                 error = copyout(&cache->nelms, addr, sizeof (cache->nelms));
445                 break;
446                 
447         case ENCIOC_GETELMMAP:
448                 for (uelm = addr, i = 0; i != cache->nelms; i++) {
449                         encioc_element_t kelm;
450                         kelm.elm_idx = i;
451                         kelm.elm_subenc_id = cache->elm_map[i].subenclosure;
452                         kelm.elm_type = cache->elm_map[i].elm_type;
453                         error = copyout(&kelm, &uelm[i], sizeof(kelm));
454                         if (error)
455                                 break;
456                 }
457                 break;
458
459         case ENCIOC_GETENCSTAT:
460                 cam_periph_lock(periph);
461                 error = enc->enc_vec.get_enc_status(enc, 1);
462                 if (error) {
463                         cam_periph_unlock(periph);
464                         break;
465                 }
466                 tmp = cache->enc_status;
467                 cam_periph_unlock(periph);
468                 error = copyout(&tmp, addr, sizeof(tmp));
469                 cache->enc_status = tmp;
470                 break;
471
472         case ENCIOC_SETENCSTAT:
473                 error = copyin(addr, &tmp, sizeof(tmp));
474                 if (error)
475                         break;
476                 cam_periph_lock(periph);
477                 error = enc->enc_vec.set_enc_status(enc, tmp, 1);
478                 cam_periph_unlock(periph);
479                 break;
480
481         case ENCIOC_GETSTRING:
482         case ENCIOC_SETSTRING:
483         case ENCIOC_GETENCNAME:
484         case ENCIOC_GETENCID:
485                 if (enc->enc_vec.handle_string == NULL) {
486                         error = EINVAL;
487                         break;
488                 }
489                 error = copyin(addr, &sstr, sizeof(sstr));
490                 if (error)
491                         break;
492                 cam_periph_lock(periph);
493                 error = enc->enc_vec.handle_string(enc, &sstr, cmd);
494                 cam_periph_unlock(periph);
495                 break;
496
497         case ENCIOC_GETELMSTAT:
498                 error = copyin(addr, &elms, sizeof(elms));
499                 if (error)
500                         break;
501                 if (elms.elm_idx >= cache->nelms) {
502                         error = EINVAL;
503                         break;
504                 }
505                 cam_periph_lock(periph);
506                 error = enc->enc_vec.get_elm_status(enc, &elms, 1);
507                 cam_periph_unlock(periph);
508                 if (error)
509                         break;
510                 error = copyout(&elms, addr, sizeof(elms));
511                 break;
512
513         case ENCIOC_GETELMDESC:
514                 error = copyin(addr, &elmd, sizeof(elmd));
515                 if (error)
516                         break;
517                 if (elmd.elm_idx >= cache->nelms) {
518                         error = EINVAL;
519                         break;
520                 }
521                 if (enc->enc_vec.get_elm_desc != NULL) {
522                         error = enc->enc_vec.get_elm_desc(enc, &elmd);
523                         if (error)
524                                 break;
525                 } else
526                         elmd.elm_desc_len = 0;
527                 error = copyout(&elmd, addr, sizeof(elmd));
528                 break;
529
530         case ENCIOC_GETELMDEVNAMES:
531                 if (enc->enc_vec.get_elm_devnames == NULL) {
532                         error = EINVAL;
533                         break;
534                 }
535                 error = copyin(addr, &elmdn, sizeof(elmdn));
536                 if (error)
537                         break;
538                 if (elmdn.elm_idx >= cache->nelms) {
539                         error = EINVAL;
540                         break;
541                 }
542                 cam_periph_lock(periph);
543                 error = (*enc->enc_vec.get_elm_devnames)(enc, &elmdn);
544                 cam_periph_unlock(periph);
545                 if (error)
546                         break;
547                 error = copyout(&elmdn, addr, sizeof(elmdn));
548                 break;
549
550         case ENCIOC_SETELMSTAT:
551                 error = copyin(addr, &elms, sizeof(elms));
552                 if (error)
553                         break;
554
555                 if (elms.elm_idx >= cache->nelms) {
556                         error = EINVAL;
557                         break;
558                 }
559                 cam_periph_lock(periph);
560                 error = enc->enc_vec.set_elm_status(enc, &elms, 1);
561                 cam_periph_unlock(periph);
562
563                 break;
564
565         case ENCIOC_INIT:
566
567                 cam_periph_lock(periph);
568                 error = enc->enc_vec.init_enc(enc);
569                 cam_periph_unlock(periph);
570                 break;
571
572         default:
573                 cam_periph_lock(periph);
574                 error = cam_periph_ioctl(periph, cmd, arg_addr, enc_error);
575                 cam_periph_unlock(periph);
576                 break;
577         }
578         sx_sunlock(&enc->enc_cache_lock);
579         return (error);
580 }
581
582 int
583 enc_runcmd(struct enc_softc *enc, char *cdb, int cdbl, char *dptr, int *dlenp)
584 {
585         int error, dlen, tdlen;
586         ccb_flags ddf;
587         union ccb *ccb;
588
589         CAM_DEBUG(enc->periph->path, CAM_DEBUG_TRACE,
590             ("entering enc_runcmd\n"));
591         if (dptr) {
592                 if ((dlen = *dlenp) < 0) {
593                         dlen = -dlen;
594                         ddf = CAM_DIR_OUT;
595                 } else {
596                         ddf = CAM_DIR_IN;
597                 }
598         } else {
599                 dlen = 0;
600                 ddf = CAM_DIR_NONE;
601         }
602
603         if (cdbl > IOCDBLEN) {
604                 cdbl = IOCDBLEN;
605         }
606
607         ccb = cam_periph_getccb(enc->periph, CAM_PRIORITY_NORMAL);
608         if (enc->enc_type == ENC_SEMB_SES || enc->enc_type == ENC_SEMB_SAFT) {
609                 tdlen = min(dlen, 1020);
610                 tdlen = (tdlen + 3) & ~3;
611                 cam_fill_ataio(&ccb->ataio, 0, NULL, ddf, 0, dptr, tdlen,
612                     30 * 1000);
613                 if (cdb[0] == RECEIVE_DIAGNOSTIC)
614                         ata_28bit_cmd(&ccb->ataio,
615                             ATA_SEP_ATTN, cdb[2], 0x02, tdlen / 4);
616                 else if (cdb[0] == SEND_DIAGNOSTIC)
617                         ata_28bit_cmd(&ccb->ataio,
618                             ATA_SEP_ATTN, dlen > 0 ? dptr[0] : 0,
619                             0x82, tdlen / 4);
620                 else if (cdb[0] == READ_BUFFER)
621                         ata_28bit_cmd(&ccb->ataio,
622                             ATA_SEP_ATTN, cdb[2], 0x00, tdlen / 4);
623                 else
624                         ata_28bit_cmd(&ccb->ataio,
625                             ATA_SEP_ATTN, dlen > 0 ? dptr[0] : 0,
626                             0x80, tdlen / 4);
627         } else {
628                 tdlen = dlen;
629                 cam_fill_csio(&ccb->csio, 0, NULL, ddf, MSG_SIMPLE_Q_TAG,
630                     dptr, dlen, sizeof (struct scsi_sense_data), cdbl,
631                     60 * 1000);
632                 bcopy(cdb, ccb->csio.cdb_io.cdb_bytes, cdbl);
633         }
634
635         error = cam_periph_runccb(ccb, enc_error, ENC_CFLAGS, ENC_FLAGS, NULL);
636         if (error) {
637                 if (dptr) {
638                         *dlenp = dlen;
639                 }
640         } else {
641                 if (dptr) {
642                         if (ccb->ccb_h.func_code == XPT_ATA_IO)
643                                 *dlenp = ccb->ataio.resid;
644                         else
645                                 *dlenp = ccb->csio.resid;
646                         *dlenp += tdlen - dlen;
647                 }
648         }
649         xpt_release_ccb(ccb);
650         CAM_DEBUG(enc->periph->path, CAM_DEBUG_SUBTRACE,
651             ("exiting enc_runcmd: *dlenp = %d\n", *dlenp));
652         return (error);
653 }
654
655 void
656 enc_log(struct enc_softc *enc, const char *fmt, ...)
657 {
658         va_list ap;
659
660         printf("%s%d: ", enc->periph->periph_name, enc->periph->unit_number);
661         va_start(ap, fmt);
662         vprintf(fmt, ap);
663         va_end(ap);
664 }
665
666 /*
667  * The code after this point runs on many platforms,
668  * so forgive the slightly awkward and nonconforming
669  * appearance.
670  */
671
672 /*
673  * Is this a device that supports enclosure services?
674  *
675  * It's a pretty simple ruleset- if it is device type
676  * 0x0D (13), it's an ENCLOSURE device.
677  */
678
679 #define SAFTE_START     44
680 #define SAFTE_END       50
681 #define SAFTE_LEN       SAFTE_END-SAFTE_START
682
683 static enctyp
684 enc_type(struct ccb_getdev *cgd)
685 {
686         int buflen;
687         unsigned char *iqd;
688
689         if (cgd->protocol == PROTO_SEMB) {
690                 iqd = (unsigned char *)&cgd->ident_data;
691                 if (STRNCMP(iqd + 43, "S-E-S", 5) == 0)
692                         return (ENC_SEMB_SES);
693                 else if (STRNCMP(iqd + 43, "SAF-TE", 6) == 0)
694                         return (ENC_SEMB_SAFT);
695                 return (ENC_NONE);
696
697         } else if (cgd->protocol != PROTO_SCSI)
698                 return (ENC_NONE);
699
700         iqd = (unsigned char *)&cgd->inq_data;
701         buflen = min(sizeof(cgd->inq_data),
702             SID_ADDITIONAL_LENGTH(&cgd->inq_data));
703
704         if ((iqd[0] & 0x1f) == T_ENCLOSURE)
705                 return (ENC_SES);
706
707 #ifdef  SES_ENABLE_PASSTHROUGH
708         if ((iqd[6] & 0x40) && (iqd[2] & 0x7) >= 2) {
709                 /*
710                  * PassThrough Device.
711                  */
712                 return (ENC_SES_PASSTHROUGH);
713         }
714 #endif
715
716         /*
717          * The comparison is short for a reason-
718          * some vendors were chopping it short.
719          */
720
721         if (buflen < SAFTE_END - 2) {
722                 return (ENC_NONE);
723         }
724
725         if (STRNCMP((char *)&iqd[SAFTE_START], "SAF-TE", SAFTE_LEN - 2) == 0) {
726                 return (ENC_SAFT);
727         }
728         return (ENC_NONE);
729 }
730
731 /*================== Enclosure Monitoring/Processing Daemon ==================*/
732 /**
733  * \brief Queue an update request for a given action, if needed.
734  *
735  * \param enc           SES softc to queue the request for.
736  * \param action        Action requested.
737  */
738 void
739 enc_update_request(enc_softc_t *enc, uint32_t action)
740 {
741         if ((enc->pending_actions & (0x1 << action)) == 0) {
742                 enc->pending_actions |= (0x1 << action);
743                 ENC_DLOG(enc, "%s: queing requested action %d\n",
744                     __func__, action);
745                 if (enc->current_action == ENC_UPDATE_NONE)
746                         wakeup(enc->enc_daemon);
747         } else {
748                 ENC_DLOG(enc, "%s: ignoring requested action %d - "
749                     "Already queued\n", __func__, action);
750         }
751 }
752
753 /**
754  * \brief Invoke the handler of the highest priority pending
755  *        state in the SES state machine.
756  *
757  * \param enc  The SES instance invoking the state machine.
758  */
759 static void
760 enc_fsm_step(enc_softc_t *enc)
761 {
762         union ccb            *ccb;
763         uint8_t              *buf;
764         struct enc_fsm_state *cur_state;
765         int                   error;
766         uint32_t              xfer_len;
767         
768         ENC_DLOG(enc, "%s enter %p\n", __func__, enc);
769
770         enc->current_action   = ffs(enc->pending_actions) - 1;
771         enc->pending_actions &= ~(0x1 << enc->current_action);
772
773         cur_state = &enc->enc_fsm_states[enc->current_action];
774
775         buf = NULL;
776         if (cur_state->buf_size != 0) {
777                 cam_periph_unlock(enc->periph);
778                 buf = malloc(cur_state->buf_size, M_SCSIENC, M_WAITOK|M_ZERO);
779                 cam_periph_lock(enc->periph);
780         }
781
782         error = 0;
783         ccb   = NULL;
784         if (cur_state->fill != NULL) {
785                 ccb = cam_periph_getccb(enc->periph, CAM_PRIORITY_NORMAL);
786
787                 error = cur_state->fill(enc, cur_state, ccb, buf);
788                 if (error != 0)
789                         goto done;
790
791                 error = cam_periph_runccb(ccb, cur_state->error,
792                                           ENC_CFLAGS,
793                                           ENC_FLAGS|SF_QUIET_IR, NULL);
794         }
795
796         if (ccb != NULL) {
797                 if (ccb->ccb_h.func_code == XPT_ATA_IO)
798                         xfer_len = ccb->ataio.dxfer_len - ccb->ataio.resid;
799                 else
800                         xfer_len = ccb->csio.dxfer_len - ccb->csio.resid;
801         } else
802                 xfer_len = 0;
803
804         cam_periph_unlock(enc->periph);
805         cur_state->done(enc, cur_state, ccb, &buf, error, xfer_len);
806         cam_periph_lock(enc->periph);
807
808 done:
809         ENC_DLOG(enc, "%s exit - result %d\n", __func__, error);
810         ENC_FREE_AND_NULL(buf);
811         if (ccb != NULL)
812                 xpt_release_ccb(ccb);
813 }
814
815 /**
816  * \invariant Called with cam_periph mutex held.
817  */
818 static void
819 enc_status_updater(void *arg)
820 {
821         enc_softc_t *enc;
822
823         enc = arg;
824         if (enc->enc_vec.poll_status != NULL)
825                 enc->enc_vec.poll_status(enc);
826 }
827
828 static void
829 enc_daemon(void *arg)
830 {
831         enc_softc_t *enc;
832
833         enc = arg;
834
835         cam_periph_lock(enc->periph);
836         while ((enc->enc_flags & ENC_FLAG_SHUTDOWN) == 0) {
837                 if (enc->pending_actions == 0) {
838                         struct intr_config_hook *hook;
839
840                         /*
841                          * Reset callout and msleep, or
842                          * issue timed task completion
843                          * status command.
844                          */
845                         enc->current_action = ENC_UPDATE_NONE;
846
847                         /*
848                          * We've been through our state machine at least
849                          * once.  Allow the transition to userland.
850                          */
851                         hook = &enc->enc_boot_hold_ch;
852                         if (hook->ich_func != NULL) {
853                                 config_intrhook_disestablish(hook);
854                                 hook->ich_func = NULL;
855                         }
856
857                         callout_reset(&enc->status_updater, 60*hz,
858                                       enc_status_updater, enc);
859
860                         cam_periph_sleep(enc->periph, enc->enc_daemon,
861                                          PUSER, "idle", 0);
862                 } else {
863                         enc_fsm_step(enc);
864                 }
865         }
866         enc->enc_daemon = NULL;
867         cam_periph_unlock(enc->periph);
868         cam_periph_release(enc->periph);
869         kproc_exit(0);
870 }
871
872 static int
873 enc_kproc_init(enc_softc_t *enc)
874 {
875         int result;
876
877         callout_init_mtx(&enc->status_updater, cam_periph_mtx(enc->periph), 0);
878
879         if (cam_periph_acquire(enc->periph) != 0)
880                 return (ENXIO);
881
882         result = kproc_create(enc_daemon, enc, &enc->enc_daemon, /*flags*/0,
883                               /*stackpgs*/0, "enc_daemon%d",
884                               enc->periph->unit_number);
885         if (result == 0) {
886                 /* Do an initial load of all page data. */
887                 cam_periph_lock(enc->periph);
888                 enc->enc_vec.poll_status(enc);
889                 cam_periph_unlock(enc->periph);
890         } else
891                 cam_periph_release(enc->periph);
892         return (result);
893 }
894  
895 /**
896  * \brief Interrupt configuration hook callback associated with
897  *        enc_boot_hold_ch.
898  *
899  * Since interrupts are always functional at the time of enclosure
900  * configuration, there is nothing to be done when the callback occurs.
901  * This hook is only registered to hold up boot processing while initial
902  * eclosure processing occurs.
903  * 
904  * \param arg  The enclosure softc, but currently unused in this callback.
905  */
906 static void
907 enc_nop_confighook_cb(void *arg __unused)
908 {
909 }
910
911 static cam_status
912 enc_ctor(struct cam_periph *periph, void *arg)
913 {
914         cam_status status = CAM_REQ_CMP_ERR;
915         int err;
916         enc_softc_t *enc;
917         struct ccb_getdev *cgd;
918         char *tname;
919         struct make_dev_args args;
920         struct sbuf sb;
921
922         cgd = (struct ccb_getdev *)arg;
923         if (cgd == NULL) {
924                 printf("enc_ctor: no getdev CCB, can't register device\n");
925                 goto out;
926         }
927
928         enc = ENC_MALLOCZ(sizeof(*enc));
929         if (enc == NULL) {
930                 printf("enc_ctor: Unable to probe new device. "
931                        "Unable to allocate enc\n");                             
932                 goto out;
933         }
934         enc->periph = periph;
935         enc->current_action = ENC_UPDATE_INVALID;
936
937         enc->enc_type = enc_type(cgd);
938         sx_init(&enc->enc_cache_lock, "enccache");
939
940         switch (enc->enc_type) {
941         case ENC_SES:
942         case ENC_SES_PASSTHROUGH:
943         case ENC_SEMB_SES:
944                 err = ses_softc_init(enc);
945                 break;
946         case ENC_SAFT:
947         case ENC_SEMB_SAFT:
948                 err = safte_softc_init(enc);
949                 break;
950         case ENC_NONE:
951         default:
952                 ENC_FREE(enc);
953                 return (CAM_REQ_CMP_ERR);
954         }
955
956         if (err) {
957                 xpt_print(periph->path, "error %d initializing\n", err);
958                 goto out;
959         }
960
961         /*
962          * Hold off userland until we have made at least one pass
963          * through our state machine so that physical path data is
964          * present.
965          */
966         if (enc->enc_vec.poll_status != NULL) {
967                 enc->enc_boot_hold_ch.ich_func = enc_nop_confighook_cb;
968                 enc->enc_boot_hold_ch.ich_arg = enc;
969                 config_intrhook_establish(&enc->enc_boot_hold_ch);
970         }
971
972         /*
973          * The softc field is set only once the enc is fully initialized
974          * so that we can rely on this field to detect partially
975          * initialized periph objects in the AC_FOUND_DEVICE handler.
976          */
977         periph->softc = enc;
978
979         cam_periph_unlock(periph);
980         if (enc->enc_vec.poll_status != NULL) {
981                 err = enc_kproc_init(enc);
982                 if (err) {
983                         xpt_print(periph->path,
984                                   "error %d starting enc_daemon\n", err);
985                         goto out;
986                 }
987         }
988
989         /*
990          * Acquire a reference to the periph before we create the devfs
991          * instance for it.  We'll release this reference once the devfs
992          * instance has been freed.
993          */
994         if (cam_periph_acquire(periph) != 0) {
995                 xpt_print(periph->path, "%s: lost periph during "
996                           "registration!\n", __func__);
997                 cam_periph_lock(periph);
998
999                 return (CAM_REQ_CMP_ERR);
1000         }
1001
1002         make_dev_args_init(&args);
1003         args.mda_devsw = &enc_cdevsw;
1004         args.mda_unit = periph->unit_number;
1005         args.mda_uid = UID_ROOT;
1006         args.mda_gid = GID_OPERATOR;
1007         args.mda_mode = 0600;
1008         args.mda_si_drv1 = periph;
1009         err = make_dev_s(&args, &enc->enc_dev, "%s%d", periph->periph_name,
1010             periph->unit_number);
1011         cam_periph_lock(periph);
1012         if (err != 0) {
1013                 cam_periph_release_locked(periph);
1014                 return (CAM_REQ_CMP_ERR);
1015         }
1016
1017         enc->enc_flags |= ENC_FLAG_INITIALIZED;
1018
1019         /*
1020          * Add an async callback so that we get notified if this
1021          * device goes away.
1022          */
1023         xpt_register_async(AC_LOST_DEVICE, enc_async, periph, periph->path);
1024
1025         switch (enc->enc_type) {
1026         default:
1027         case ENC_NONE:
1028                 tname = "No ENC device";
1029                 break;
1030         case ENC_SES:
1031                 tname = "SES Device";
1032                 break;
1033         case ENC_SES_PASSTHROUGH:
1034                 tname = "SES Passthrough Device";
1035                 break;
1036         case ENC_SAFT:
1037                 tname = "SAF-TE Device";
1038                 break;
1039         case ENC_SEMB_SES:
1040                 tname = "SEMB SES Device";
1041                 break;
1042         case ENC_SEMB_SAFT:
1043                 tname = "SEMB SAF-TE Device";
1044                 break;
1045         }
1046
1047         sbuf_new(&sb, enc->announce_buf, ENC_ANNOUNCE_SZ, SBUF_FIXEDLEN);
1048         xpt_announce_periph_sbuf(periph, &sb, tname);
1049         sbuf_finish(&sb);
1050         sbuf_putbuf(&sb);
1051
1052         status = CAM_REQ_CMP;
1053
1054 out:
1055         if (status != CAM_REQ_CMP)
1056                 enc_dtor(periph);
1057         return (status);
1058 }
1059