FreeBSD kernel CAM code
scsi_enc.c
Go to the documentation of this file.
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>
61#include <cam/scsi/scsi_enc.h>
63
64#include "opt_ses.h"
65
66MALLOC_DEFINE(M_SCSIENC, "SCSI ENC", "SCSI ENC buffers");
67
68/* Enclosure type independent driver */
69
70static d_open_t enc_open;
71static d_close_t enc_close;
72static d_ioctl_t enc_ioctl;
77
78static void enc_async(void *, uint32_t, struct cam_path *, void *);
79static enctyp enc_type(struct ccb_getdev *);
80
81SYSCTL_NODE(_kern_cam, OID_AUTO, enc, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
82 "CAM Enclosure Services driver");
83
84#if defined(DEBUG) || defined(ENC_DEBUG)
85int enc_verbose = 1;
86#else
88#endif
89SYSCTL_INT(_kern_cam_enc, OID_AUTO, verbose, CTLFLAG_RWTUN,
90 &enc_verbose, 0, "Enable verbose logging");
91
94
95static struct periph_driver encdriver = {
96 enc_init, "ses",
97 TAILQ_HEAD_INITIALIZER(encdriver.units), /* generation */ 0
98};
99
101
102static 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
111static void
112enc_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
128static void
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++)
148
149 enc->open_count = 0;
150
151 /*
152 * Release the reference held for the device node, it is gone now.
153 */
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
166static void
168{
169 struct enc_softc *enc;
170
171 enc = periph->softc;
172
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 */
183
184 /*
185 * Shutdown our daemon.
186 */
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
197static void
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 root_mount_rel(&enc->enc_rootmount);
209
210 ENC_FREE(enc);
211}
212
213static void
214enc_async(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
215{
216 struct cam_periph *periph;
217
218 periph = (struct cam_periph *)callback_arg;
219
220 switch(code) {
221 case AC_FOUND_DEVICE:
222 {
223 struct ccb_getdev *cgd;
224 cam_status status;
225 path_id_t path_id;
226
227 cgd = (struct ccb_getdev *)arg;
228 if (arg == NULL) {
229 break;
230 }
231
232 if (enc_type(cgd) == ENC_NONE) {
233 /*
234 * Schedule announcement of the ENC bindings for
235 * this device if it is managed by a SEP.
236 */
237 path_id = xpt_path_path_id(path);
239 TAILQ_FOREACH(periph, &encdriver.units, unit_links) {
240 struct enc_softc *softc;
241
242 softc = (struct enc_softc *)periph->softc;
243
244 /* Check this SEP is ready. */
245 if (softc == NULL || (softc->enc_flags &
246 ENC_FLAG_INITIALIZED) == 0 ||
247 softc->enc_vec.device_found == NULL)
248 continue;
249
250 /* Check this SEP may manage this device. */
251 if (xpt_path_path_id(periph->path) != path_id &&
252 (softc->enc_type != ENC_SEMB_SES ||
253 cgd->protocol != PROTO_ATA))
254 continue;
255
256 softc->enc_vec.device_found(softc);
257 }
259 return;
260 }
261
263 enc_dtor, NULL, "ses", CAM_PERIPH_BIO,
264 path, enc_async, AC_FOUND_DEVICE, cgd);
265
266 if (status != CAM_REQ_CMP && status != CAM_REQ_INPROG) {
267 printf("enc_async: Unable to probe new device due to "
268 "status 0x%x\n", status);
269 }
270 break;
271 }
272 default:
273 cam_periph_async(periph, code, path, arg);
274 break;
275 }
276}
277
278static int
279enc_open(struct cdev *dev, int flags, int fmt, struct thread *td)
280{
281 struct cam_periph *periph;
282 struct enc_softc *softc;
283 int error = 0;
284
285 periph = (struct cam_periph *)dev->si_drv1;
286 if (cam_periph_acquire(periph) != 0)
287 return (ENXIO);
288
289 cam_periph_lock(periph);
290
291 softc = (struct enc_softc *)periph->softc;
292
293 if ((softc->enc_flags & ENC_FLAG_INITIALIZED) == 0) {
294 error = ENXIO;
295 goto out;
296 }
297 if (softc->enc_flags & ENC_FLAG_INVALID) {
298 error = ENXIO;
299 goto out;
300 }
301out:
302 if (error != 0)
304 else
305 softc->open_count++;
306
308
309 return (error);
310}
311
312static int
313enc_close(struct cdev *dev, int flag, int fmt, struct thread *td)
314{
315 struct cam_periph *periph;
316 struct enc_softc *enc;
317 struct mtx *mtx;
318
319 periph = (struct cam_periph *)dev->si_drv1;
320 mtx = cam_periph_mtx(periph);
321 mtx_lock(mtx);
322
323 enc = periph->softc;
324 enc->open_count--;
325
327
328 /*
329 * We reference the lock directly here, instead of using
330 * cam_periph_unlock(). The reason is that the call to
331 * cam_periph_release_locked() above could result in the periph
332 * getting freed. If that is the case, dereferencing the periph
333 * with a cam_periph_unlock() call would cause a page fault.
334 *
335 * cam_periph_release() avoids this problem using the same method,
336 * but we're manually acquiring and dropping the lock here to
337 * protect the open count and avoid another lock acquisition and
338 * release.
339 */
340 mtx_unlock(mtx);
341
342 return (0);
343}
344
345int
346enc_error(union ccb *ccb, uint32_t cflags, uint32_t sflags)
347{
348
349 return (cam_periph_error(ccb, cflags, sflags));
350}
351
352static int
353enc_ioctl(struct cdev *dev, u_long cmd, caddr_t arg_addr, int flag,
354 struct thread *td)
355{
356 struct cam_periph *periph;
357 enc_softc_t *enc;
358 enc_cache_t *cache;
359 void *addr;
360 int error, i;
361
362#ifdef COMPAT_FREEBSD32
363 if (SV_PROC_FLAG(td->td_proc, SV_ILP32))
364 return (ENOTTY);
365#endif
366
367 if (arg_addr)
368 addr = *((caddr_t *) arg_addr);
369 else
370 addr = NULL;
371
372 periph = (struct cam_periph *)dev->si_drv1;
373 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering encioctl\n"));
374
375 cam_periph_lock(periph);
376 enc = (struct enc_softc *)periph->softc;
377 cache = &enc->enc_cache;
378
379 /*
380 * Now check to see whether we're initialized or not.
381 * This actually should never fail as we're not supposed
382 * to get past enc_open w/o successfully initializing
383 * things.
384 */
385 if ((enc->enc_flags & ENC_FLAG_INITIALIZED) == 0) {
387 return (ENXIO);
388 }
390
391 error = 0;
392
394 ("trying to do ioctl %#lx\n", cmd));
395
396 /*
397 * If this command can change the device's state,
398 * we must have the device open for writing.
399 *
400 * For commands that get information about the
401 * device- we don't need to lock the peripheral
402 * if we aren't running a command. The periph
403 * also can't go away while a user process has
404 * it open.
405 */
406 switch (cmd) {
407 case ENCIOC_GETNELM:
408 case ENCIOC_GETELMMAP:
414 case ENCIOC_GETENCID:
415 break;
416 default:
417 if ((flag & FWRITE) == 0) {
418 return (EBADF);
419 }
420 }
421
422 /*
423 * XXX The values read here are only valid for the current
424 * configuration generation. We need these ioctls
425 * to also pass in/out a generation number.
426 */
427 sx_slock(&enc->enc_cache_lock);
428 switch (cmd) {
429 case ENCIOC_GETNELM:
430 error = copyout(&cache->nelms, addr, sizeof (cache->nelms));
431 break;
432
433 case ENCIOC_GETELMMAP: {
434 encioc_element_t *uelm;
435
436 for (uelm = addr, i = 0; i != cache->nelms; i++) {
437 encioc_element_t kelm;
438 kelm.elm_idx = i;
439 kelm.elm_subenc_id = cache->elm_map[i].subenclosure;
440 kelm.elm_type = cache->elm_map[i].elm_type;
441 error = copyout(&kelm, &uelm[i], sizeof(kelm));
442 if (error)
443 break;
444 }
445 break;
446 }
447 case ENCIOC_GETENCSTAT: {
448 error = copyout(&cache->enc_status, addr,
449 sizeof(cache->enc_status));
450 break;
451 }
452 case ENCIOC_SETENCSTAT: {
454
455 error = copyin(addr, &tmp, sizeof(tmp));
456 if (error)
457 break;
459 error = enc->enc_vec.set_enc_status(enc, tmp, 1);
461 break;
462 }
463 case ENCIOC_GETSTRING:
464 case ENCIOC_SETSTRING:
466 case ENCIOC_GETENCID: {
467 encioc_string_t sstr;
468
469 if (enc->enc_vec.handle_string == NULL) {
470 error = EINVAL;
471 break;
472 }
473 error = copyin(addr, &sstr, sizeof(sstr));
474 if (error)
475 break;
477 error = enc->enc_vec.handle_string(enc, &sstr, cmd);
479 if (error == 0 || error == ENOMEM)
480 (void)copyout(&sstr.bufsiz,
481 &((encioc_string_t *)addr)->bufsiz,
482 sizeof(sstr.bufsiz));
483 break;
484 }
485 case ENCIOC_GETELMSTAT: {
487
488 error = copyin(addr, &elms, sizeof(elms));
489 if (error)
490 break;
491 if (elms.elm_idx >= cache->nelms) {
492 error = EINVAL;
493 break;
494 }
496 error = enc->enc_vec.get_elm_status(enc, &elms, 1);
498 if (error)
499 break;
500 error = copyout(&elms, addr, sizeof(elms));
501 break;
502 }
503 case ENCIOC_GETELMDESC: {
505
506 error = copyin(addr, &elmd, sizeof(elmd));
507 if (error)
508 break;
509 if (elmd.elm_idx >= cache->nelms) {
510 error = EINVAL;
511 break;
512 }
513 if (enc->enc_vec.get_elm_desc != NULL) {
514 error = enc->enc_vec.get_elm_desc(enc, &elmd);
515 if (error)
516 break;
517 } else
518 elmd.elm_desc_len = 0;
519 error = copyout(&elmd, addr, sizeof(elmd));
520 break;
521 }
524
525 if (enc->enc_vec.get_elm_devnames == NULL) {
526 error = EINVAL;
527 break;
528 }
529 error = copyin(addr, &elmdn, sizeof(elmdn));
530 if (error)
531 break;
532 if (elmdn.elm_idx >= cache->nelms) {
533 error = EINVAL;
534 break;
535 }
537 error = (*enc->enc_vec.get_elm_devnames)(enc, &elmdn);
539 if (error)
540 break;
541 error = copyout(&elmdn, addr, sizeof(elmdn));
542 break;
543 }
544 case ENCIOC_SETELMSTAT: {
546
547 error = copyin(addr, &elms, sizeof(elms));
548 if (error)
549 break;
550
551 if (elms.elm_idx >= cache->nelms) {
552 error = EINVAL;
553 break;
554 }
556 error = enc->enc_vec.set_elm_status(enc, &elms, 1);
558
559 break;
560 }
561 case ENCIOC_INIT:
562
564 error = enc->enc_vec.init_enc(enc);
566 break;
567
568 default:
570 error = cam_periph_ioctl(periph, cmd, arg_addr, enc_error);
572 break;
573 }
574 sx_sunlock(&enc->enc_cache_lock);
575 return (error);
576}
577
578int
579enc_runcmd(struct enc_softc *enc, char *cdb, int cdbl, char *dptr, int *dlenp)
580{
581 int error, dlen, tdlen;
582 ccb_flags ddf;
583 union ccb *ccb;
584
586 ("entering enc_runcmd\n"));
587 if (dptr) {
588 if ((dlen = *dlenp) < 0) {
589 dlen = -dlen;
590 ddf = CAM_DIR_OUT;
591 } else {
592 ddf = CAM_DIR_IN;
593 }
594 } else {
595 dlen = 0;
596 ddf = CAM_DIR_NONE;
597 }
598
599 if (cdbl > IOCDBLEN) {
600 cdbl = IOCDBLEN;
601 }
602
604 if (enc->enc_type == ENC_SEMB_SES || enc->enc_type == ENC_SEMB_SAFT) {
605 tdlen = min(dlen, 1020);
606 tdlen = (tdlen + 3) & ~3;
607 cam_fill_ataio(&ccb->ataio, 0, NULL, ddf, 0, dptr, tdlen,
608 30 * 1000);
609 if (cdb[0] == RECEIVE_DIAGNOSTIC)
611 ATA_SEP_ATTN, cdb[2], 0x02, tdlen / 4);
612 else if (cdb[0] == SEND_DIAGNOSTIC)
614 ATA_SEP_ATTN, dlen > 0 ? dptr[0] : 0,
615 0x82, tdlen / 4);
616 else if (cdb[0] == READ_BUFFER)
618 ATA_SEP_ATTN, cdb[2], 0x00, tdlen / 4);
619 else
621 ATA_SEP_ATTN, dlen > 0 ? dptr[0] : 0,
622 0x80, tdlen / 4);
623 } else {
624 tdlen = dlen;
625 cam_fill_csio(&ccb->csio, 0, NULL, ddf, MSG_SIMPLE_Q_TAG,
626 dptr, dlen, sizeof (struct scsi_sense_data), cdbl,
627 60 * 1000);
628 bcopy(cdb, ccb->csio.cdb_io.cdb_bytes, cdbl);
629 }
630
632 if (error) {
633 if (dptr) {
634 *dlenp = dlen;
635 }
636 } else {
637 if (dptr) {
639 *dlenp = ccb->ataio.resid;
640 else
641 *dlenp = ccb->csio.resid;
642 *dlenp += tdlen - dlen;
643 }
644 }
647 ("exiting enc_runcmd: *dlenp = %d\n", *dlenp));
648 return (error);
649}
650
651void
652enc_log(struct enc_softc *enc, const char *fmt, ...)
653{
654 va_list ap;
655
656 printf("%s%d: ", enc->periph->periph_name, enc->periph->unit_number);
657 va_start(ap, fmt);
658 vprintf(fmt, ap);
659 va_end(ap);
660}
661
662/*
663 * The code after this point runs on many platforms,
664 * so forgive the slightly awkward and nonconforming
665 * appearance.
666 */
667
668/*
669 * Is this a device that supports enclosure services?
670 *
671 * It's a pretty simple ruleset- if it is device type
672 * 0x0D (13), it's an ENCLOSURE device.
673 */
674
675#define SAFTE_START 44
676#define SAFTE_END 50
677#define SAFTE_LEN SAFTE_END-SAFTE_START
678
679static enctyp
681{
682 int buflen;
683 unsigned char *iqd;
684
685 if (cgd->protocol == PROTO_SEMB) {
686 iqd = (unsigned char *)&cgd->ident_data;
687 if (STRNCMP(iqd + 43, "S-E-S", 5) == 0)
688 return (ENC_SEMB_SES);
689 else if (STRNCMP(iqd + 43, "SAF-TE", 6) == 0)
690 return (ENC_SEMB_SAFT);
691 return (ENC_NONE);
692
693 } else if (cgd->protocol != PROTO_SCSI)
694 return (ENC_NONE);
695
696 iqd = (unsigned char *)&cgd->inq_data;
697 buflen = min(sizeof(cgd->inq_data),
699
700 if ((iqd[0] & 0x1f) == T_ENCLOSURE)
701 return (ENC_SES);
702
703#ifdef SES_ENABLE_PASSTHROUGH
704 if ((iqd[6] & 0x40) && (iqd[2] & 0x7) >= 2) {
705 /*
706 * PassThrough Device.
707 */
708 return (ENC_SES_PASSTHROUGH);
709 }
710#endif
711
712 /*
713 * The comparison is short for a reason-
714 * some vendors were chopping it short.
715 */
716
717 if (buflen < SAFTE_END - 2) {
718 return (ENC_NONE);
719 }
720
721 if (STRNCMP((char *)&iqd[SAFTE_START], "SAF-TE", SAFTE_LEN - 2) == 0) {
722 return (ENC_SAFT);
723 }
724 return (ENC_NONE);
725}
726
727/*================== Enclosure Monitoring/Processing Daemon ==================*/
734void
735enc_update_request(enc_softc_t *enc, uint32_t action)
736{
737 if ((enc->pending_actions & (0x1 << action)) == 0) {
738 enc->pending_actions |= (0x1 << action);
739 ENC_DLOG(enc, "%s: queing requested action %d\n",
740 __func__, action);
742 wakeup(enc->enc_daemon);
743 } else {
744 ENC_DLOG(enc, "%s: ignoring requested action %d - "
745 "Already queued\n", __func__, action);
746 }
747}
748
755static void
757{
758 union ccb *ccb;
759 uint8_t *buf;
760 struct enc_fsm_state *cur_state;
761 int error;
762 uint32_t xfer_len;
763
764 ENC_DLOG(enc, "%s enter %p\n", __func__, enc);
765
766 enc->current_action = ffs(enc->pending_actions) - 1;
767 enc->pending_actions &= ~(0x1 << enc->current_action);
768
769 cur_state = &enc->enc_fsm_states[enc->current_action];
770
771 buf = NULL;
772 if (cur_state->buf_size != 0) {
774 buf = malloc(cur_state->buf_size, M_SCSIENC, M_WAITOK|M_ZERO);
776 }
777
778 error = 0;
779 ccb = NULL;
780 if (cur_state->fill != NULL) {
782
783 error = cur_state->fill(enc, cur_state, ccb, buf);
784 if (error != 0)
785 goto done;
786
787 error = cam_periph_runccb(ccb, cur_state->error,
789 ENC_FLAGS|SF_QUIET_IR, NULL);
790 }
791
792 if (ccb != NULL) {
794 xfer_len = ccb->ataio.dxfer_len - ccb->ataio.resid;
795 else
796 xfer_len = ccb->csio.dxfer_len - ccb->csio.resid;
797 } else
798 xfer_len = 0;
799
801 cur_state->done(enc, cur_state, ccb, &buf, error, xfer_len);
803
804done:
805 ENC_DLOG(enc, "%s exit - result %d\n", __func__, error);
807 if (ccb != NULL)
809}
810
814static void
816{
817 enc_softc_t *enc;
818
819 enc = arg;
820 if (enc->enc_vec.poll_status != NULL)
821 enc->enc_vec.poll_status(enc);
822}
823
824static void
825enc_daemon(void *arg)
826{
827 enc_softc_t *enc;
828
829 enc = arg;
830
832 while ((enc->enc_flags & ENC_FLAG_SHUTDOWN) == 0) {
833 if (enc->pending_actions == 0) {
834 /*
835 * Reset callout and msleep, or
836 * issue timed task completion
837 * status command.
838 */
840
841 /*
842 * We've been through our state machine at least
843 * once. Allow the transition to userland.
844 */
845 root_mount_rel(&enc->enc_rootmount);
846
847 callout_reset_sbt(&enc->status_updater, 60 * SBT_1S, 0,
848 enc_status_updater, enc, C_PREL(1));
849
851 PUSER, "idle", 0);
852 } else {
853 enc_fsm_step(enc);
854 }
855 }
856 enc->enc_daemon = NULL;
859 kproc_exit(0);
860}
861
862static int
864{
865 int result;
866
867 callout_init_mtx(&enc->status_updater, cam_periph_mtx(enc->periph), 0);
868
869 if (cam_periph_acquire(enc->periph) != 0)
870 return (ENXIO);
871
872 result = kproc_create(enc_daemon, enc, &enc->enc_daemon, /*flags*/0,
873 /*stackpgs*/0, "enc_daemon%d",
874 enc->periph->unit_number);
875 if (result == 0) {
876 /* Do an initial load of all page data. */
878 enc->enc_vec.poll_status(enc);
880 } else
882 return (result);
883}
884
885static cam_status
886enc_ctor(struct cam_periph *periph, void *arg)
887{
889 int err;
890 enc_softc_t *enc;
891 struct ccb_getdev *cgd;
892 char *tname;
893 struct make_dev_args args;
894 struct sbuf sb;
895
896 cgd = (struct ccb_getdev *)arg;
897 if (cgd == NULL) {
898 printf("enc_ctor: no getdev CCB, can't register device\n");
899 goto out;
900 }
901
902 enc = ENC_MALLOCZ(sizeof(*enc));
903 if (enc == NULL) {
904 printf("enc_ctor: Unable to probe new device. "
905 "Unable to allocate enc\n");
906 goto out;
907 }
908 enc->periph = periph;
910
911 enc->enc_type = enc_type(cgd);
912 sx_init(&enc->enc_cache_lock, "enccache");
913
914 switch (enc->enc_type) {
915 case ENC_SES:
917 case ENC_SEMB_SES:
918 err = ses_softc_init(enc);
919 break;
920 case ENC_SAFT:
921 case ENC_SEMB_SAFT:
922 err = safte_softc_init(enc);
923 break;
924 case ENC_NONE:
925 default:
926 ENC_FREE(enc);
927 return (CAM_REQ_CMP_ERR);
928 }
929
930 if (err) {
931 xpt_print(periph->path, "error %d initializing\n", err);
932 goto out;
933 }
934
935 /*
936 * Hold off userland until we have made at least one pass
937 * through our state machine so that physical path data is
938 * present.
939 */
940 if (enc->enc_vec.poll_status != NULL) {
941 root_mount_hold_token(periph->periph_name, &enc->enc_rootmount);
942 }
943
944 /*
945 * The softc field is set only once the enc is fully initialized
946 * so that we can rely on this field to detect partially
947 * initialized periph objects in the AC_FOUND_DEVICE handler.
948 */
949 periph->softc = enc;
950
951 cam_periph_unlock(periph);
952 if (enc->enc_vec.poll_status != NULL) {
953 err = enc_kproc_init(enc);
954 if (err) {
955 xpt_print(periph->path,
956 "error %d starting enc_daemon\n", err);
957 goto out;
958 }
959 }
960
961 /*
962 * Acquire a reference to the periph before we create the devfs
963 * instance for it. We'll release this reference once the devfs
964 * instance has been freed.
965 */
966 if (cam_periph_acquire(periph) != 0) {
967 xpt_print(periph->path, "%s: lost periph during "
968 "registration!\n", __func__);
969 cam_periph_lock(periph);
970
971 return (CAM_REQ_CMP_ERR);
972 }
973
974 make_dev_args_init(&args);
975 args.mda_devsw = &enc_cdevsw;
976 args.mda_unit = periph->unit_number;
977 args.mda_uid = UID_ROOT;
978 args.mda_gid = GID_OPERATOR;
979 args.mda_mode = 0600;
980 args.mda_si_drv1 = periph;
981 err = make_dev_s(&args, &enc->enc_dev, "%s%d", periph->periph_name,
982 periph->unit_number);
983 cam_periph_lock(periph);
984 if (err != 0) {
986 return (CAM_REQ_CMP_ERR);
987 }
988
990
991 /*
992 * Add an async callback so that we get notified if this
993 * device goes away.
994 */
996
997 switch (enc->enc_type) {
998 default:
999 case ENC_NONE:
1000 tname = "No ENC device";
1001 break;
1002 case ENC_SES:
1003 tname = "SES Device";
1004 break;
1006 tname = "SES Passthrough Device";
1007 break;
1008 case ENC_SAFT:
1009 tname = "SAF-TE Device";
1010 break;
1011 case ENC_SEMB_SES:
1012 tname = "SEMB SES Device";
1013 break;
1014 case ENC_SEMB_SAFT:
1015 tname = "SEMB SAF-TE Device";
1016 break;
1017 }
1018
1019 sbuf_new(&sb, enc->announce_buf, ENC_ANNOUNCE_SZ, SBUF_FIXEDLEN);
1020 xpt_announce_periph_sbuf(periph, &sb, tname);
1021 sbuf_finish(&sb);
1022 sbuf_putbuf(&sb);
1023
1024 status = CAM_REQ_CMP;
1025
1026out:
1027 if (status != CAM_REQ_CMP)
1028 enc_dtor(periph);
1029 return (status);
1030}
void ata_28bit_cmd(struct ccb_ataio *ataio, uint8_t cmd, uint8_t features, uint32_t lba, uint8_t sector_count)
Definition: ata_all.c:621
@ SF_QUIET_IR
Definition: cam.h:124
#define CAM_PRIORITY_NORMAL
Definition: cam.h:92
u_int path_id_t
Definition: cam.h:42
cam_status
Definition: cam.h:132
@ CAM_REQ_INPROG
Definition: cam.h:134
@ CAM_REQ_CMP
Definition: cam.h:137
@ CAM_REQ_CMP_ERR
Definition: cam.h:146
@ AC_FOUND_DEVICE
Definition: cam_ccb.h:874
@ AC_LOST_DEVICE
Definition: cam_ccb.h:873
static __inline void cam_fill_ataio(struct ccb_ataio *ataio, u_int32_t retries, void(*cbfcnp)(struct cam_periph *, union ccb *), u_int32_t flags, u_int tag_action __unused, u_int8_t *data_ptr, u_int32_t dxfer_len, u_int32_t timeout)
Definition: cam_ccb.h:1436
@ PROTO_SEMB
Definition: cam_ccb.h:283
@ PROTO_ATA
Definition: cam_ccb.h:280
@ PROTO_SCSI
Definition: cam_ccb.h:279
ccb_flags
Definition: cam_ccb.h:68
@ CAM_DIR_IN
Definition: cam_ccb.h:79
@ CAM_DIR_NONE
Definition: cam_ccb.h:81
@ CAM_DIR_OUT
Definition: cam_ccb.h:80
static __BEGIN_DECLS __inline void cam_fill_csio(struct ccb_scsiio *csio, u_int32_t retries, void(*cbfcnp)(struct cam_periph *, union ccb *), u_int32_t flags, u_int8_t tag_action, u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len, u_int8_t cdb_len, u_int32_t timeout)
Definition: cam_ccb.h:1389
@ XPT_ATA_IO
Definition: cam_ccb.h:199
#define IOCDBLEN
Definition: cam_ccb.h:50
@ CAM_DEBUG_TRACE
Definition: cam_debug.h:41
@ CAM_DEBUG_SUBTRACE
Definition: cam_debug.h:42
#define CAM_DEBUG(path, flag, printfargs)
Definition: cam_debug.h:93
void cam_periph_release_locked(struct cam_periph *periph)
Definition: cam_periph.c:453
int cam_periph_acquire(struct cam_periph *periph)
Definition: cam_periph.c:413
void cam_periph_async(struct cam_periph *periph, u_int32_t code, struct cam_path *path, void *arg)
Definition: cam_periph.c:1471
void cam_periph_release(struct cam_periph *periph)
Definition: cam_periph.c:465
int cam_periph_runccb(union ccb *ccb, int(*error_routine)(union ccb *ccb, cam_flags camflags, u_int32_t sense_flags), cam_flags camflags, u_int32_t sense_flags, struct devstat *ds)
Definition: cam_periph.c:1207
int cam_periph_error(union ccb *ccb, cam_flags camflags, u_int32_t sense_flags)
Definition: cam_periph.c:1864
int cam_periph_ioctl(struct cam_periph *periph, u_long cmd, caddr_t addr, int(*error_routine)(union ccb *ccb, cam_flags camflags, u_int32_t sense_flags))
Definition: cam_periph.c:1109
cam_status cam_periph_alloc(periph_ctor_t *periph_ctor, periph_oninv_t *periph_oninvalidate, periph_dtor_t *periph_dtor, periph_start_t *periph_start, char *name, cam_periph_type type, struct cam_path *path, ac_callback_t *ac_callback, ac_code code, void *arg)
Definition: cam_periph.c:197
cam_status periph_ctor_t(struct cam_periph *periph, void *arg)
Definition: cam_periph.h:115
void periph_oninv_t(struct cam_periph *periph)
Definition: cam_periph.h:117
#define cam_periph_lock(periph)
Definition: cam_periph.h:224
union ccb * cam_periph_getccb(struct cam_periph *periph, u_int32_t priority)
Definition: cam_xpt.c:4695
#define cam_periph_unlock(periph)
Definition: cam_periph.h:227
static __inline struct mtx * cam_periph_mtx(struct cam_periph *periph)
Definition: cam_periph.h:213
void periph_dtor_t(struct cam_periph *periph)
Definition: cam_periph.h:118
#define cam_periph_sleep(periph, chan, priority, wmesg, timo)
Definition: cam_periph.h:233
@ CAM_PERIPH_BIO
Definition: cam_periph.h:104
void() periph_init_t(void)
Definition: cam_periph.h:85
void xpt_unlock_buses(void)
Definition: cam_xpt.c:5322
void xpt_print(struct cam_path *path, const char *fmt,...)
Definition: cam_xpt.c:3814
path_id_t xpt_path_path_id(struct cam_path *path)
Definition: cam_xpt.c:3880
void xpt_lock_buses(void)
Definition: cam_xpt.c:5316
cam_status xpt_register_async(int event, ac_callback_t *cbfunc, void *cbarg, struct cam_path *path)
Definition: cam_xpt.c:5213
void xpt_release_ccb(union ccb *free_ccb)
Definition: cam_xpt.c:3924
void xpt_announce_periph_sbuf(struct cam_periph *periph, struct sbuf *sb, char *announce_string)
Definition: cam_xpt.c:1093
union ccb * ccb
Definition: mmc_sim_if.m:53
#define SEND_DIAGNOSTIC
Definition: scsi_all.h:2093
#define SID_ADDITIONAL_LENGTH(iqd)
Definition: scsi_all.h:2237
#define T_ENCLOSURE
Definition: scsi_all.h:2183
#define RECEIVE_DIAGNOSTIC
Definition: scsi_all.h:2092
#define READ_BUFFER
Definition: scsi_all.h:2104
int enc_runcmd(struct enc_softc *enc, char *cdb, int cdbl, char *dptr, int *dlenp)
Definition: scsi_enc.c:579
static int enc_kproc_init(enc_softc_t *enc)
Definition: scsi_enc.c:863
#define SAFTE_LEN
Definition: scsi_enc.c:677
static d_ioctl_t enc_ioctl
Definition: scsi_enc.c:72
static void enc_async(void *, uint32_t, struct cam_path *, void *)
Definition: scsi_enc.c:214
static periph_oninv_t enc_oninvalidate
Definition: scsi_enc.c:75
static void enc_daemon(void *arg)
Definition: scsi_enc.c:825
static enctyp enc_type(struct ccb_getdev *)
Definition: scsi_enc.c:680
static void enc_status_updater(void *arg)
Definition: scsi_enc.c:815
int enc_verbose
Definition: scsi_enc.c:87
static void enc_fsm_step(enc_softc_t *enc)
Invoke the handler of the highest priority pending state in the SES state machine.
Definition: scsi_enc.c:756
static void enc_devgonecb(void *arg)
Definition: scsi_enc.c:129
static d_open_t enc_open
Definition: scsi_enc.c:70
SYSCTL_NODE(_kern_cam, OID_AUTO, enc, CTLFLAG_RD|CTLFLAG_MPSAFE, 0, "CAM Enclosure Services driver")
__FBSDID("$FreeBSD$")
static d_close_t enc_close
Definition: scsi_enc.c:71
static struct cdevsw enc_cdevsw
Definition: scsi_enc.c:102
const char * elm_type_names[]
Definition: scsi_enc.c:92
CTASSERT(nitems(elm_type_names) - 1==ELMTYP_LAST)
void enc_update_request(enc_softc_t *enc, uint32_t action)
Queue an update request for a given action, if needed.
Definition: scsi_enc.c:735
PERIPHDRIVER_DECLARE(enc, encdriver)
#define SAFTE_START
Definition: scsi_enc.c:675
void enc_log(struct enc_softc *enc, const char *fmt,...)
Definition: scsi_enc.c:652
SYSCTL_INT(_kern_cam_enc, OID_AUTO, verbose, CTLFLAG_RWTUN, &enc_verbose, 0, "Enable verbose logging")
int enc_error(union ccb *ccb, uint32_t cflags, uint32_t sflags)
Definition: scsi_enc.c:346
#define SAFTE_END
Definition: scsi_enc.c:676
static struct periph_driver encdriver
Definition: scsi_enc.c:95
MALLOC_DEFINE(M_SCSIENC, "SCSI ENC", "SCSI ENC buffers")
static periph_init_t enc_init
Definition: scsi_enc.c:73
static periph_ctor_t enc_ctor
Definition: scsi_enc.c:74
static periph_dtor_t enc_dtor
Definition: scsi_enc.c:76
#define ENCIOC_SETENCSTAT
Definition: scsi_enc.h:42
#define ELM_TYPE_NAMES
Definition: scsi_enc.h:128
#define ENCIOC_GETELMDESC
Definition: scsi_enc.h:47
#define ENCIOC_GETNELM
Definition: scsi_enc.h:39
unsigned char encioc_enc_status_t
Definition: scsi_enc.h:175
#define ENCIOC_SETSTRING
Definition: scsi_enc.h:50
#define ENCIOC_GETENCID
Definition: scsi_enc.h:52
#define ENCIOC_GETELMMAP
Definition: scsi_enc.h:40
#define ENCIOC_GETSTRING
Definition: scsi_enc.h:49
@ ELMTYP_LAST
Definition: scsi_enc.h:125
#define ENCIOC_GETENCNAME
Definition: scsi_enc.h:51
#define ENCIOC_GETENCSTAT
Definition: scsi_enc.h:41
#define ENCIOC_INIT
Definition: scsi_enc.h:46
#define ENCIOC_GETELMSTAT
Definition: scsi_enc.h:43
#define ENCIOC_SETELMSTAT
Definition: scsi_enc.h:44
#define ENCIOC_GETELMDEVNAMES
Definition: scsi_enc.h:48
#define ENC_FLAGS
#define ENC_DLOG
#define ENC_ANNOUNCE_SZ
#define ENC_CFLAGS
#define ENC_FLAG_SHUTDOWN
enc_softc_init_t ses_softc_init
#define ENC_FLAG_INITIALIZED
#define STRNCMP
enc_softc_init_t safte_softc_init
@ ENC_SES
@ ENC_SAFT
@ ENC_SEMB_SAFT
@ ENC_SES_PASSTHROUGH
@ ENC_NONE
@ ENC_SEMB_SES
#define ENC_UPDATE_INVALID
#define ENC_FREE(ptr)
#define ENC_FREE_AND_NULL(ptr)
#define ENC_MALLOCZ(amt)
#define ENC_FLAG_INVALID
#define ENC_UPDATE_NONE
#define MSG_SIMPLE_Q_TAG
Definition: scsi_message.h:35
char * periph_name
Definition: cam_periph.h:123
u_int32_t unit_number
Definition: cam_periph.h:127
void * softc
Definition: cam_periph.h:125
struct cam_path * path
Definition: cam_periph.h:124
u_int32_t dxfer_len
Definition: cam_ccb.h:797
u_int32_t resid
Definition: cam_ccb.h:798
cam_proto protocol
Definition: cam_ccb.h:380
struct ata_params ident_data
Definition: cam_ccb.h:382
struct scsi_inquiry_data inq_data
Definition: cam_ccb.h:381
xpt_opcode func_code
Definition: cam_ccb.h:362
cdb_t cdb_io
Definition: cam_ccb.h:763
u_int32_t dxfer_len
Definition: cam_ccb.h:754
u_int32_t resid
Definition: cam_ccb.h:762
enc_element_t * elm_map
encioc_enc_status_t enc_status
uint8_t subenclosure
fsm_fill_handler_t * fill
fsm_error_handler_t * error
fsm_done_handler_t * done
struct root_hold_token enc_rootmount
struct proc * enc_daemon
struct callout status_updater
uint32_t current_action
struct cdev * enc_dev
struct enc_vec enc_vec
struct cam_periph * periph
char announce_buf[ENC_ANNOUNCE_SZ]
uint32_t pending_actions
struct sx enc_cache_lock
struct enc_fsm_state * enc_fsm_states
uint8_t enc_flags
enc_cache_t enc_cache
enc_softc_invalidate_t * softc_invalidate
enc_handle_string_t * handle_string
enc_init_enc_t * init_enc
enc_poll_status_t * poll_status
enc_get_elm_devnames_t * get_elm_devnames
enc_get_elm_desc_t * get_elm_desc
enc_get_elm_status_t * get_elm_status
enc_softc_cleanup_t * softc_cleanup
enc_set_elm_status_t * set_elm_status
enc_set_enc_status_t * set_enc_status
enc_device_found_t * device_found
unsigned int elm_subenc_id
Definition: scsi_enc.h:166
elm_type_t elm_type
Definition: scsi_enc.h:169
unsigned int elm_idx
Definition: scsi_enc.h:163
uint16_t elm_desc_len
Definition: scsi_enc.h:203
unsigned int elm_idx
Definition: scsi_enc.h:202
unsigned int elm_idx
Definition: scsi_enc.h:212
unsigned int elm_idx
Definition: scsi_enc.h:181
size_t bufsiz
Definition: scsi_enc.h:190
Definition: cam_ccb.h:1345
struct ccb_hdr ccb_h
Definition: cam_ccb.h:1346
struct ccb_scsiio csio
Definition: cam_ccb.h:1347
struct ccb_getdev cgd
Definition: cam_ccb.h:1348
struct ccb_ataio ataio
Definition: cam_ccb.h:1376
u_int8_t cdb_bytes[IOCDBLEN]
Definition: cam_ccb.h:742