2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2007 Scott Long
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
30 * scsi_sg peripheral driver. This driver is meant to implement the Linux
31 * SG passthrough interface for SCSI.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/types.h>
42 #include <sys/malloc.h>
43 #include <sys/fcntl.h>
44 #include <sys/ioccom.h>
46 #include <sys/errno.h>
47 #include <sys/devicestat.h>
52 #include <cam/cam_ccb.h>
53 #include <cam/cam_periph.h>
54 #include <cam/cam_queue.h>
55 #include <cam/cam_xpt_periph.h>
56 #include <cam/cam_debug.h>
57 #include <cam/cam_sim.h>
59 #include <cam/scsi/scsi_all.h>
60 #include <cam/scsi/scsi_message.h>
61 #include <cam/scsi/scsi_sg.h>
64 SG_FLAG_LOCKED = 0x01,
65 SG_FLAG_INVALID = 0x02
82 #define ccb_type ppriv_field0
83 #define ccb_rdwr ppriv_ptr1
86 TAILQ_ENTRY(sg_rdwr) rdwr_link;
94 struct sg_io_hdr io_hdr;
103 struct devstat *device_stats;
104 TAILQ_HEAD(, sg_rdwr) rdwr_done;
111 static d_open_t sgopen;
112 static d_close_t sgclose;
113 static d_ioctl_t sgioctl;
114 static d_write_t sgwrite;
115 static d_read_t sgread;
117 static periph_init_t sginit;
118 static periph_ctor_t sgregister;
119 static periph_oninv_t sgoninvalidate;
120 static periph_dtor_t sgcleanup;
121 static void sgasync(void *callback_arg, uint32_t code,
122 struct cam_path *path, void *arg);
123 static void sgdone(struct cam_periph *periph, union ccb *done_ccb);
124 static int sgsendccb(struct cam_periph *periph, union ccb *ccb);
125 static int sgsendrdwr(struct cam_periph *periph, union ccb *ccb);
126 static int sgerror(union ccb *ccb, uint32_t cam_flags,
127 uint32_t sense_flags);
128 static void sg_scsiio_status(struct ccb_scsiio *csio,
129 u_short *hoststat, u_short *drvstat);
131 static int scsi_group_len(u_char cmd);
133 static struct periph_driver sgdriver =
136 TAILQ_HEAD_INITIALIZER(sgdriver.units), /* gen */ 0
138 PERIPHDRIVER_DECLARE(sg, sgdriver);
140 static struct cdevsw sg_cdevsw = {
141 .d_version = D_VERSION,
142 .d_flags = D_TRACKCLOSE,
151 static int sg_version = 30125;
159 * Install a global async callback. This callback will receive aync
160 * callbacks like "new device found".
162 status = xpt_register_async(AC_FOUND_DEVICE, sgasync, NULL, NULL);
164 if (status != CAM_REQ_CMP) {
165 printf("sg: Failed to attach master async callbac "
166 "due to status 0x%x!\n", status);
171 sgdevgonecb(void *arg)
173 struct cam_periph *periph;
174 struct sg_softc *softc;
178 periph = (struct cam_periph *)arg;
179 mtx = cam_periph_mtx(periph);
182 softc = (struct sg_softc *)periph->softc;
183 KASSERT(softc->open_count >= 0, ("Negative open count %d",
187 * When we get this callback, we will get no more close calls from
188 * devfs. So if we have any dangling opens, we need to release the
189 * reference held for that particular context.
191 for (i = 0; i < softc->open_count; i++)
192 cam_periph_release_locked(periph);
194 softc->open_count = 0;
197 * Release the reference held for the device node, it is gone now.
199 cam_periph_release_locked(periph);
202 * We reference the lock directly here, instead of using
203 * cam_periph_unlock(). The reason is that the final call to
204 * cam_periph_release_locked() above could result in the periph
205 * getting freed. If that is the case, dereferencing the periph
206 * with a cam_periph_unlock() call would cause a page fault.
212 sgoninvalidate(struct cam_periph *periph)
214 struct sg_softc *softc;
216 softc = (struct sg_softc *)periph->softc;
219 * Deregister any async callbacks.
221 xpt_register_async(0, sgasync, periph, periph->path);
223 softc->flags |= SG_FLAG_INVALID;
226 * Tell devfs this device has gone away, and ask for a callback
227 * when it has cleaned up its state.
229 destroy_dev_sched_cb(softc->dev, sgdevgonecb, periph);
232 * XXX Return all queued I/O with ENXIO.
233 * XXX Handle any transactions queued to the card
234 * with XPT_ABORT_CCB.
240 sgcleanup(struct cam_periph *periph)
242 struct sg_softc *softc;
244 softc = (struct sg_softc *)periph->softc;
246 devstat_remove_entry(softc->device_stats);
248 free(softc, M_DEVBUF);
252 sgasync(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
254 struct cam_periph *periph;
256 periph = (struct cam_periph *)callback_arg;
259 case AC_FOUND_DEVICE:
261 struct ccb_getdev *cgd;
264 cgd = (struct ccb_getdev *)arg;
268 if (cgd->protocol != PROTO_SCSI)
272 * Allocate a peripheral instance for this device and
273 * start the probe process.
275 status = cam_periph_alloc(sgregister, sgoninvalidate,
276 sgcleanup, NULL, "sg",
277 CAM_PERIPH_BIO, path,
278 sgasync, AC_FOUND_DEVICE, cgd);
279 if ((status != CAM_REQ_CMP) && (status != CAM_REQ_INPROG)) {
280 const struct cam_status_entry *entry;
282 entry = cam_fetch_status_entry(status);
283 printf("sgasync: Unable to attach new device "
284 "due to status %#x: %s\n", status, entry ?
285 entry->status_text : "Unknown");
290 cam_periph_async(periph, code, path, arg);
296 sgregister(struct cam_periph *periph, void *arg)
298 struct sg_softc *softc;
299 struct ccb_getdev *cgd;
300 struct ccb_pathinq cpi;
301 struct make_dev_args args;
304 cgd = (struct ccb_getdev *)arg;
306 printf("sgregister: no getdev CCB, can't register device\n");
307 return (CAM_REQ_CMP_ERR);
310 softc = malloc(sizeof(*softc), M_DEVBUF, M_ZERO | M_NOWAIT);
312 printf("sgregister: Unable to allocate softc\n");
313 return (CAM_REQ_CMP_ERR);
316 softc->state = SG_STATE_NORMAL;
317 softc->pd_type = SID_TYPE(&cgd->inq_data);
318 softc->sg_timeout = SG_DEFAULT_TIMEOUT / SG_DEFAULT_HZ * hz;
319 softc->sg_user_timeout = SG_DEFAULT_TIMEOUT;
320 TAILQ_INIT(&softc->rdwr_done);
321 periph->softc = softc;
323 xpt_path_inq(&cpi, periph->path);
326 softc->maxio = DFLTPHYS; /* traditional default */
327 else if (cpi.maxio > maxphys)
328 softc->maxio = maxphys; /* for safety */
330 softc->maxio = cpi.maxio; /* real value */
333 * We pass in 0 for all blocksize, since we don't know what the
334 * blocksize of the device is, if it even has a blocksize.
336 cam_periph_unlock(periph);
337 no_tags = (cgd->inq_data.flags & SID_CmdQue) == 0;
338 softc->device_stats = devstat_new_entry("sg",
339 periph->unit_number, 0,
341 | (no_tags ? DEVSTAT_NO_ORDERED_TAGS : 0),
343 XPORT_DEVSTAT_TYPE(cpi.transport) |
345 DEVSTAT_PRIORITY_PASS);
348 * Acquire a reference to the periph before we create the devfs
349 * instance for it. We'll release this reference once the devfs
350 * instance has been freed.
352 if (cam_periph_acquire(periph) != 0) {
353 xpt_print(periph->path, "%s: lost periph during "
354 "registration!\n", __func__);
355 cam_periph_lock(periph);
356 return (CAM_REQ_CMP_ERR);
359 /* Register the device */
360 make_dev_args_init(&args);
361 args.mda_devsw = &sg_cdevsw;
362 args.mda_unit = periph->unit_number;
363 args.mda_uid = UID_ROOT;
364 args.mda_gid = GID_OPERATOR;
365 args.mda_mode = 0600;
366 args.mda_si_drv1 = periph;
367 error = make_dev_s(&args, &softc->dev, "%s%d",
368 periph->periph_name, periph->unit_number);
370 cam_periph_lock(periph);
371 cam_periph_release_locked(periph);
372 return (CAM_REQ_CMP_ERR);
374 if (periph->unit_number < 26) {
375 (void)make_dev_alias(softc->dev, "sg%c",
376 periph->unit_number + 'a');
378 (void)make_dev_alias(softc->dev, "sg%c%c",
379 ((periph->unit_number / 26) - 1) + 'a',
380 (periph->unit_number % 26) + 'a');
382 cam_periph_lock(periph);
385 * Add as async callback so that we get
386 * notified if this device goes away.
388 xpt_register_async(AC_LOST_DEVICE, sgasync, periph, periph->path);
391 xpt_announce_periph(periph, NULL);
393 return (CAM_REQ_CMP);
397 sgdone(struct cam_periph *periph, union ccb *done_ccb)
399 struct sg_softc *softc;
400 struct ccb_scsiio *csio;
402 softc = (struct sg_softc *)periph->softc;
403 csio = &done_ccb->csio;
404 switch (csio->ccb_h.ccb_type) {
407 struct sg_rdwr *rdwr;
409 devstat_end_transaction(softc->device_stats,
411 csio->tag_action & 0xf,
412 ((csio->ccb_h.flags & CAM_DIR_MASK) ==
413 CAM_DIR_NONE) ? DEVSTAT_NO_DATA :
414 (csio->ccb_h.flags & CAM_DIR_OUT) ?
415 DEVSTAT_WRITE : DEVSTAT_READ,
418 rdwr = done_ccb->ccb_h.ccb_rdwr;
419 rdwr->state = SG_RDWR_DONE;
424 panic("unknown sg CCB type");
429 sgopen(struct cdev *dev, int flags, int fmt, struct thread *td)
431 struct cam_periph *periph;
432 struct sg_softc *softc;
435 periph = (struct cam_periph *)dev->si_drv1;
436 if (cam_periph_acquire(periph) != 0)
440 * Don't allow access when we're running at a high securelevel.
442 error = securelevel_gt(td->td_ucred, 1);
444 cam_periph_release(periph);
448 cam_periph_lock(periph);
450 softc = (struct sg_softc *)periph->softc;
451 if (softc->flags & SG_FLAG_INVALID) {
452 cam_periph_release_locked(periph);
453 cam_periph_unlock(periph);
459 cam_periph_unlock(periph);
465 sgclose(struct cdev *dev, int flag, int fmt, struct thread *td)
467 struct cam_periph *periph;
468 struct sg_softc *softc;
471 periph = (struct cam_periph *)dev->si_drv1;
472 mtx = cam_periph_mtx(periph);
475 softc = periph->softc;
478 cam_periph_release_locked(periph);
481 * We reference the lock directly here, instead of using
482 * cam_periph_unlock(). The reason is that the call to
483 * cam_periph_release_locked() above could result in the periph
484 * getting freed. If that is the case, dereferencing the periph
485 * with a cam_periph_unlock() call would cause a page fault.
487 * cam_periph_release() avoids this problem using the same method,
488 * but we're manually acquiring and dropping the lock here to
489 * protect the open count and avoid another lock acquisition and
498 sgioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *td)
501 struct ccb_scsiio *csio;
502 struct cam_periph *periph;
503 struct sg_softc *softc;
504 struct sg_io_hdr *req;
507 periph = (struct cam_periph *)dev->si_drv1;
508 cam_periph_lock(periph);
510 softc = (struct sg_softc *)periph->softc;
514 case SG_GET_VERSION_NUM:
516 int *version = (int *)arg;
518 *version = sg_version;
523 u_int user_timeout = *(u_int *)arg;
525 softc->sg_user_timeout = user_timeout;
526 softc->sg_timeout = user_timeout / SG_DEFAULT_HZ * hz;
531 * The value is returned directly to the syscall.
533 td->td_retval[0] = softc->sg_user_timeout;
537 req = (struct sg_io_hdr *)arg;
539 if (req->cmd_len > IOCDBLEN) {
544 if (req->iovec_count != 0) {
549 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
552 error = copyin(req->cmdp, &csio->cdb_io.cdb_bytes,
555 xpt_release_ccb(ccb);
559 switch(req->dxfer_direction) {
560 case SG_DXFER_TO_DEV:
563 case SG_DXFER_FROM_DEV:
566 case SG_DXFER_TO_FROM_DEV:
586 error = sgsendccb(periph, ccb);
588 req->host_status = DID_ERROR;
589 req->driver_status = DRIVER_INVALID;
590 xpt_release_ccb(ccb);
594 req->status = csio->scsi_status;
595 req->masked_status = (csio->scsi_status >> 1) & 0x7f;
596 sg_scsiio_status(csio, &req->host_status, &req->driver_status);
597 req->resid = csio->resid;
598 req->duration = csio->ccb_h.timeout;
601 if ((csio->ccb_h.status & CAM_AUTOSNS_VALID)
602 && (req->sbp != NULL)) {
603 req->sb_len_wr = req->mx_sb_len - csio->sense_resid;
604 error = copyout(&csio->sense_data, req->sbp,
608 xpt_release_ccb(ccb);
611 case SG_GET_RESERVED_SIZE:
613 int *size = (int *)arg;
620 struct sg_scsi_id *id = (struct sg_scsi_id *)arg;
622 id->host_no = cam_sim_path(xpt_path_sim(periph->path));
623 id->channel = xpt_path_path_id(periph->path);
624 id->scsi_id = xpt_path_target_id(periph->path);
625 id->lun = xpt_path_lun_id(periph->path);
626 id->scsi_type = softc->pd_type;
627 id->h_cmd_per_lun = 1;
628 id->d_queue_depth = 1;
634 case SG_GET_SG_TABLESIZE:
636 int *size = (int *)arg;
641 case SG_EMULATED_HOST:
642 case SG_SET_TRANSFORM:
643 case SG_GET_TRANSFORM:
644 case SG_GET_NUM_WAITING:
646 case SG_GET_REQUEST_TABLE:
647 case SG_SET_KEEP_ORPHAN:
648 case SG_GET_KEEP_ORPHAN:
649 case SG_GET_ACCESS_COUNT:
650 case SG_SET_FORCE_LOW_DMA:
652 case SG_SET_FORCE_PACK_ID:
654 case SG_SET_RESERVED_SIZE:
655 case SG_GET_COMMAND_Q:
656 case SG_SET_COMMAND_Q:
658 case SG_NEXT_CMD_LEN:
661 printf("sgioctl: rejecting cmd 0x%lx\n", cmd);
667 cam_periph_unlock(periph);
672 sgwrite(struct cdev *dev, struct uio *uio, int ioflag)
675 struct cam_periph *periph;
676 struct ccb_scsiio *csio;
678 struct sg_header *hdr;
679 struct sg_rdwr *rdwr;
682 int error = 0, cdb_len, buf_len, dir;
684 periph = dev->si_drv1;
685 rdwr = malloc(sizeof(*rdwr), M_DEVBUF, M_WAITOK | M_ZERO);
686 hdr = &rdwr->hdr.hdr;
688 /* Copy in the header block and sanity check it */
689 if (uio->uio_resid < sizeof(*hdr)) {
693 error = uiomove(hdr, sizeof(*hdr), uio);
697 /* XXX: We don't support SG 3.x read/write API. */
698 if (hdr->reply_len < 0) {
703 ccb = xpt_alloc_ccb();
711 * Copy in the CDB block. The designers of the interface didn't
712 * bother to provide a size for this in the header, so we have to
713 * figure it out ourselves.
715 if (uio->uio_resid < 1)
717 error = uiomove(&cdb_cmd, 1, uio);
720 if (hdr->twelve_byte)
723 cdb_len = scsi_group_len(cdb_cmd);
725 * We've already read the first byte of the CDB and advanced the uio
726 * pointer. Just read the rest.
728 csio->cdb_io.cdb_bytes[0] = cdb_cmd;
729 error = uiomove(&csio->cdb_io.cdb_bytes[1], cdb_len - 1, uio);
734 * Now set up the data block. Again, the designers didn't bother
735 * to make this reliable.
737 buf_len = uio->uio_resid;
739 buf = malloc(buf_len, M_DEVBUF, M_WAITOK | M_ZERO);
740 error = uiomove(buf, buf_len, uio);
744 } else if (hdr->reply_len != 0) {
745 buf = malloc(hdr->reply_len, M_DEVBUF, M_WAITOK | M_ZERO);
746 buf_len = hdr->reply_len;
754 cam_periph_lock(periph);
756 xpt_setup_ccb(&ccb->ccb_h, periph->path, CAM_PRIORITY_NORMAL);
769 * Send off the command and hope that it works. This path does not
770 * go through sgstart because the I/O is supposed to be asynchronous.
773 rdwr->buf_len = buf_len;
774 rdwr->tag = hdr->pack_id;
776 rdwr->state = SG_RDWR_INPROG;
777 ccb->ccb_h.ccb_rdwr = rdwr;
778 ccb->ccb_h.ccb_type = SG_CCB_RDWR_IO;
779 TAILQ_INSERT_TAIL(&sc->rdwr_done, rdwr, rdwr_link);
780 error = sgsendrdwr(periph, ccb);
781 cam_periph_unlock(periph);
789 free(rdwr, M_DEVBUF);
794 sgread(struct cdev *dev, struct uio *uio, int ioflag)
796 struct ccb_scsiio *csio;
797 struct cam_periph *periph;
799 struct sg_header *hdr;
800 struct sg_rdwr *rdwr;
801 u_short hstat, dstat;
802 int error, pack_len, reply_len, pack_id;
804 periph = dev->si_drv1;
806 /* XXX The pack len field needs to be updated and written out instead
807 * of discarded. Not sure how to do that.
809 uio->uio_rw = UIO_WRITE;
810 if ((error = uiomove(&pack_len, 4, uio)) != 0)
812 if ((error = uiomove(&reply_len, 4, uio)) != 0)
814 if ((error = uiomove(&pack_id, 4, uio)) != 0)
816 uio->uio_rw = UIO_READ;
818 cam_periph_lock(periph);
821 TAILQ_FOREACH(rdwr, &sc->rdwr_done, rdwr_link) {
822 if (rdwr->tag == pack_id)
825 if ((rdwr == NULL) || (rdwr->state != SG_RDWR_DONE)) {
826 if (cam_periph_sleep(periph, rdwr, PCATCH, "sgread", 0) == ERESTART)
830 TAILQ_REMOVE(&sc->rdwr_done, rdwr, rdwr_link);
831 cam_periph_unlock(periph);
833 hdr = &rdwr->hdr.hdr;
834 csio = &rdwr->ccb->csio;
835 sg_scsiio_status(csio, &hstat, &dstat);
836 hdr->host_status = hstat;
837 hdr->driver_status = dstat;
838 hdr->target_status = csio->scsi_status >> 1;
842 case DID_PASSTHROUGH:
862 if (dstat == DRIVER_SENSE) {
863 bcopy(&csio->sense_data, hdr->sense_buffer,
864 min(csio->sense_len, SG_MAX_SENSE));
866 scsi_sense_print(csio);
870 error = uiomove(&hdr->result, sizeof(*hdr) -
871 offsetof(struct sg_header, result), uio);
872 if ((error == 0) && (hdr->result == 0))
873 error = uiomove(rdwr->buf, rdwr->buf_len, uio);
875 cam_periph_lock(periph);
876 xpt_free_ccb(rdwr->ccb);
877 cam_periph_unlock(periph);
878 free(rdwr->buf, M_DEVBUF);
879 free(rdwr, M_DEVBUF);
884 sgsendccb(struct cam_periph *periph, union ccb *ccb)
886 struct sg_softc *softc;
887 struct cam_periph_map_info mapinfo;
890 softc = periph->softc;
891 bzero(&mapinfo, sizeof(mapinfo));
894 * cam_periph_mapmem calls into proc and vm functions that can
895 * sleep as well as trigger I/O, so we can't hold the lock.
896 * Dropping it here is reasonably safe.
897 * The only CCB opcode that is possible here is XPT_SCSI_IO, no
898 * need for additional checks.
900 cam_periph_unlock(periph);
901 error = cam_periph_mapmem(ccb, &mapinfo, softc->maxio);
902 cam_periph_lock(periph);
906 error = cam_periph_runccb(ccb,
910 softc->device_stats);
912 cam_periph_unlock(periph);
913 cam_periph_unmapmem(ccb, &mapinfo);
914 cam_periph_lock(periph);
920 sgsendrdwr(struct cam_periph *periph, union ccb *ccb)
922 struct sg_softc *softc;
924 softc = periph->softc;
925 devstat_start_transaction(softc->device_stats, NULL);
931 sgerror(union ccb *ccb, uint32_t cam_flags, uint32_t sense_flags)
934 return (cam_periph_error(ccb, cam_flags, sense_flags));
938 sg_scsiio_status(struct ccb_scsiio *csio, u_short *hoststat, u_short *drvstat)
942 status = csio->ccb_h.status;
944 switch (status & CAM_STATUS_MASK) {
949 case CAM_REQ_CMP_ERR:
950 *hoststat = DID_ERROR;
953 case CAM_REQ_ABORTED:
954 *hoststat = DID_ABORT;
957 case CAM_REQ_INVALID:
958 *hoststat = DID_ERROR;
959 *drvstat = DRIVER_INVALID;
961 case CAM_DEV_NOT_THERE:
962 *hoststat = DID_BAD_TARGET;
965 case CAM_SEL_TIMEOUT:
966 *hoststat = DID_NO_CONNECT;
969 case CAM_CMD_TIMEOUT:
970 *hoststat = DID_TIME_OUT;
973 case CAM_SCSI_STATUS_ERROR:
974 *hoststat = DID_ERROR;
977 case CAM_SCSI_BUS_RESET:
978 *hoststat = DID_RESET;
981 case CAM_UNCOR_PARITY:
982 *hoststat = DID_PARITY;
986 *hoststat = DID_BUS_BUSY;
990 *hoststat = DID_ERROR;
991 *drvstat = DRIVER_ERROR;
994 if (status & CAM_AUTOSNS_VALID)
995 *drvstat = DRIVER_SENSE;
999 scsi_group_len(u_char cmd)
1001 int len[] = {6, 10, 10, 12, 12, 12, 10, 10};
1004 group = (cmd >> 5) & 0x7;
1005 return (len[group]);