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;
112 static d_open_t sgopen;
113 static d_close_t sgclose;
114 static d_ioctl_t sgioctl;
115 static d_write_t sgwrite;
116 static d_read_t sgread;
118 static periph_init_t sginit;
119 static periph_ctor_t sgregister;
120 static periph_oninv_t sgoninvalidate;
121 static periph_dtor_t sgcleanup;
122 static void sgasync(void *callback_arg, uint32_t code,
123 struct cam_path *path, void *arg);
124 static void sgdone(struct cam_periph *periph, union ccb *done_ccb);
125 static int sgsendccb(struct cam_periph *periph, union ccb *ccb);
126 static int sgsendrdwr(struct cam_periph *periph, union ccb *ccb);
127 static int sgerror(union ccb *ccb, uint32_t cam_flags,
128 uint32_t sense_flags);
129 static void sg_scsiio_status(struct ccb_scsiio *csio,
130 u_short *hoststat, u_short *drvstat);
132 static int scsi_group_len(u_char cmd);
134 static struct periph_driver sgdriver =
137 TAILQ_HEAD_INITIALIZER(sgdriver.units), /* gen */ 0
139 PERIPHDRIVER_DECLARE(sg, sgdriver);
141 static struct cdevsw sg_cdevsw = {
142 .d_version = D_VERSION,
143 .d_flags = D_TRACKCLOSE,
152 static int sg_version = 30125;
160 * Install a global async callback. This callback will receive aync
161 * callbacks like "new device found".
163 status = xpt_register_async(AC_FOUND_DEVICE, sgasync, NULL, NULL);
165 if (status != CAM_REQ_CMP) {
166 printf("sg: Failed to attach master async callbac "
167 "due to status 0x%x!\n", status);
172 sgdevgonecb(void *arg)
174 struct cam_periph *periph;
175 struct sg_softc *softc;
179 periph = (struct cam_periph *)arg;
180 mtx = cam_periph_mtx(periph);
183 softc = (struct sg_softc *)periph->softc;
184 KASSERT(softc->open_count >= 0, ("Negative open count %d",
188 * When we get this callback, we will get no more close calls from
189 * devfs. So if we have any dangling opens, we need to release the
190 * reference held for that particular context.
192 for (i = 0; i < softc->open_count; i++)
193 cam_periph_release_locked(periph);
195 softc->open_count = 0;
198 * Release the reference held for the device node, it is gone now.
200 cam_periph_release_locked(periph);
203 * We reference the lock directly here, instead of using
204 * cam_periph_unlock(). The reason is that the final call to
205 * cam_periph_release_locked() above could result in the periph
206 * getting freed. If that is the case, dereferencing the periph
207 * with a cam_periph_unlock() call would cause a page fault.
213 sgoninvalidate(struct cam_periph *periph)
215 struct sg_softc *softc;
217 softc = (struct sg_softc *)periph->softc;
220 * Deregister any async callbacks.
222 xpt_register_async(0, sgasync, periph, periph->path);
224 softc->flags |= SG_FLAG_INVALID;
227 * Tell devfs this device has gone away, and ask for a callback
228 * when it has cleaned up its state.
230 destroy_dev_sched_cb(softc->dev, sgdevgonecb, periph);
233 * XXX Return all queued I/O with ENXIO.
234 * XXX Handle any transactions queued to the card
235 * with XPT_ABORT_CCB.
241 sgcleanup(struct cam_periph *periph)
243 struct sg_softc *softc;
245 softc = (struct sg_softc *)periph->softc;
247 devstat_remove_entry(softc->device_stats);
249 free(softc, M_DEVBUF);
253 sgasync(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
255 struct cam_periph *periph;
257 periph = (struct cam_periph *)callback_arg;
260 case AC_FOUND_DEVICE:
262 struct ccb_getdev *cgd;
265 cgd = (struct ccb_getdev *)arg;
269 if (cgd->protocol != PROTO_SCSI)
273 * Allocate a peripheral instance for this device and
274 * start the probe process.
276 status = cam_periph_alloc(sgregister, sgoninvalidate,
277 sgcleanup, NULL, "sg",
278 CAM_PERIPH_BIO, path,
279 sgasync, AC_FOUND_DEVICE, cgd);
280 if ((status != CAM_REQ_CMP) && (status != CAM_REQ_INPROG)) {
281 const struct cam_status_entry *entry;
283 entry = cam_fetch_status_entry(status);
284 printf("sgasync: Unable to attach new device "
285 "due to status %#x: %s\n", status, entry ?
286 entry->status_text : "Unknown");
291 cam_periph_async(periph, code, path, arg);
297 sgregister(struct cam_periph *periph, void *arg)
299 struct sg_softc *softc;
300 struct ccb_getdev *cgd;
301 struct ccb_pathinq cpi;
302 struct make_dev_args args;
305 cgd = (struct ccb_getdev *)arg;
307 printf("sgregister: no getdev CCB, can't register device\n");
308 return (CAM_REQ_CMP_ERR);
311 softc = malloc(sizeof(*softc), M_DEVBUF, M_ZERO | M_NOWAIT);
313 printf("sgregister: Unable to allocate softc\n");
314 return (CAM_REQ_CMP_ERR);
317 softc->state = SG_STATE_NORMAL;
318 softc->pd_type = SID_TYPE(&cgd->inq_data);
319 softc->sg_timeout = SG_DEFAULT_TIMEOUT / SG_DEFAULT_HZ * hz;
320 softc->sg_user_timeout = SG_DEFAULT_TIMEOUT;
321 TAILQ_INIT(&softc->rdwr_done);
322 periph->softc = softc;
324 xpt_path_inq(&cpi, periph->path);
327 softc->maxio = DFLTPHYS; /* traditional default */
328 else if (cpi.maxio > maxphys)
329 softc->maxio = maxphys; /* for safety */
331 softc->maxio = cpi.maxio; /* real value */
334 * We pass in 0 for all blocksize, since we don't know what the
335 * blocksize of the device is, if it even has a blocksize.
337 cam_periph_unlock(periph);
338 no_tags = (cgd->inq_data.flags & SID_CmdQue) == 0;
339 softc->device_stats = devstat_new_entry("sg",
340 periph->unit_number, 0,
342 | (no_tags ? DEVSTAT_NO_ORDERED_TAGS : 0),
344 XPORT_DEVSTAT_TYPE(cpi.transport) |
346 DEVSTAT_PRIORITY_PASS);
349 * Acquire a reference to the periph before we create the devfs
350 * instance for it. We'll release this reference once the devfs
351 * instance has been freed.
353 if (cam_periph_acquire(periph) != 0) {
354 xpt_print(periph->path, "%s: lost periph during "
355 "registration!\n", __func__);
356 cam_periph_lock(periph);
357 return (CAM_REQ_CMP_ERR);
360 /* Register the device */
361 make_dev_args_init(&args);
362 args.mda_devsw = &sg_cdevsw;
363 args.mda_unit = periph->unit_number;
364 args.mda_uid = UID_ROOT;
365 args.mda_gid = GID_OPERATOR;
366 args.mda_mode = 0600;
367 args.mda_si_drv1 = periph;
368 error = make_dev_s(&args, &softc->dev, "%s%d",
369 periph->periph_name, periph->unit_number);
371 cam_periph_lock(periph);
372 cam_periph_release_locked(periph);
373 return (CAM_REQ_CMP_ERR);
375 if (periph->unit_number < 26) {
376 (void)make_dev_alias(softc->dev, "sg%c",
377 periph->unit_number + 'a');
379 (void)make_dev_alias(softc->dev, "sg%c%c",
380 ((periph->unit_number / 26) - 1) + 'a',
381 (periph->unit_number % 26) + 'a');
383 cam_periph_lock(periph);
386 * Add as async callback so that we get
387 * notified if this device goes away.
389 xpt_register_async(AC_LOST_DEVICE, sgasync, periph, periph->path);
392 xpt_announce_periph(periph, NULL);
394 return (CAM_REQ_CMP);
398 sgdone(struct cam_periph *periph, union ccb *done_ccb)
400 struct sg_softc *softc;
401 struct ccb_scsiio *csio;
403 softc = (struct sg_softc *)periph->softc;
404 csio = &done_ccb->csio;
405 switch (csio->ccb_h.ccb_type) {
408 struct sg_rdwr *rdwr;
411 devstat_end_transaction(softc->device_stats,
413 csio->tag_action & 0xf,
414 ((csio->ccb_h.flags & CAM_DIR_MASK) ==
415 CAM_DIR_NONE) ? DEVSTAT_NO_DATA :
416 (csio->ccb_h.flags & CAM_DIR_OUT) ?
417 DEVSTAT_WRITE : DEVSTAT_READ,
420 rdwr = done_ccb->ccb_h.ccb_rdwr;
422 rdwr->state = SG_RDWR_DONE;
427 panic("unknown sg CCB type");
432 sgopen(struct cdev *dev, int flags, int fmt, struct thread *td)
434 struct cam_periph *periph;
435 struct sg_softc *softc;
438 periph = (struct cam_periph *)dev->si_drv1;
439 if (cam_periph_acquire(periph) != 0)
443 * Don't allow access when we're running at a high securelevel.
445 error = securelevel_gt(td->td_ucred, 1);
447 cam_periph_release(periph);
451 cam_periph_lock(periph);
453 softc = (struct sg_softc *)periph->softc;
454 if (softc->flags & SG_FLAG_INVALID) {
455 cam_periph_release_locked(periph);
456 cam_periph_unlock(periph);
462 cam_periph_unlock(periph);
468 sgclose(struct cdev *dev, int flag, int fmt, struct thread *td)
470 struct cam_periph *periph;
471 struct sg_softc *softc;
474 periph = (struct cam_periph *)dev->si_drv1;
475 mtx = cam_periph_mtx(periph);
478 softc = periph->softc;
481 cam_periph_release_locked(periph);
484 * We reference the lock directly here, instead of using
485 * cam_periph_unlock(). The reason is that the call to
486 * cam_periph_release_locked() above could result in the periph
487 * getting freed. If that is the case, dereferencing the periph
488 * with a cam_periph_unlock() call would cause a page fault.
490 * cam_periph_release() avoids this problem using the same method,
491 * but we're manually acquiring and dropping the lock here to
492 * protect the open count and avoid another lock acquisition and
501 sgioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *td)
504 struct ccb_scsiio *csio;
505 struct cam_periph *periph;
506 struct sg_softc *softc;
507 struct sg_io_hdr *req;
510 periph = (struct cam_periph *)dev->si_drv1;
511 cam_periph_lock(periph);
513 softc = (struct sg_softc *)periph->softc;
517 case SG_GET_VERSION_NUM:
519 int *version = (int *)arg;
521 *version = sg_version;
526 u_int user_timeout = *(u_int *)arg;
528 softc->sg_user_timeout = user_timeout;
529 softc->sg_timeout = user_timeout / SG_DEFAULT_HZ * hz;
534 * The value is returned directly to the syscall.
536 td->td_retval[0] = softc->sg_user_timeout;
540 req = (struct sg_io_hdr *)arg;
542 if (req->cmd_len > IOCDBLEN) {
547 if (req->iovec_count != 0) {
552 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
555 error = copyin(req->cmdp, &csio->cdb_io.cdb_bytes,
558 xpt_release_ccb(ccb);
562 switch(req->dxfer_direction) {
563 case SG_DXFER_TO_DEV:
566 case SG_DXFER_FROM_DEV:
569 case SG_DXFER_TO_FROM_DEV:
589 error = sgsendccb(periph, ccb);
591 req->host_status = DID_ERROR;
592 req->driver_status = DRIVER_INVALID;
593 xpt_release_ccb(ccb);
597 req->status = csio->scsi_status;
598 req->masked_status = (csio->scsi_status >> 1) & 0x7f;
599 sg_scsiio_status(csio, &req->host_status, &req->driver_status);
600 req->resid = csio->resid;
601 req->duration = csio->ccb_h.timeout;
604 if ((csio->ccb_h.status & CAM_AUTOSNS_VALID)
605 && (req->sbp != NULL)) {
606 req->sb_len_wr = req->mx_sb_len - csio->sense_resid;
607 error = copyout(&csio->sense_data, req->sbp,
611 xpt_release_ccb(ccb);
614 case SG_GET_RESERVED_SIZE:
616 int *size = (int *)arg;
623 struct sg_scsi_id *id = (struct sg_scsi_id *)arg;
625 id->host_no = cam_sim_path(xpt_path_sim(periph->path));
626 id->channel = xpt_path_path_id(periph->path);
627 id->scsi_id = xpt_path_target_id(periph->path);
628 id->lun = xpt_path_lun_id(periph->path);
629 id->scsi_type = softc->pd_type;
630 id->h_cmd_per_lun = 1;
631 id->d_queue_depth = 1;
637 case SG_GET_SG_TABLESIZE:
639 int *size = (int *)arg;
644 case SG_EMULATED_HOST:
645 case SG_SET_TRANSFORM:
646 case SG_GET_TRANSFORM:
647 case SG_GET_NUM_WAITING:
649 case SG_GET_REQUEST_TABLE:
650 case SG_SET_KEEP_ORPHAN:
651 case SG_GET_KEEP_ORPHAN:
652 case SG_GET_ACCESS_COUNT:
653 case SG_SET_FORCE_LOW_DMA:
655 case SG_SET_FORCE_PACK_ID:
657 case SG_SET_RESERVED_SIZE:
658 case SG_GET_COMMAND_Q:
659 case SG_SET_COMMAND_Q:
661 case SG_NEXT_CMD_LEN:
664 printf("sgioctl: rejecting cmd 0x%lx\n", cmd);
670 cam_periph_unlock(periph);
675 sgwrite(struct cdev *dev, struct uio *uio, int ioflag)
678 struct cam_periph *periph;
679 struct ccb_scsiio *csio;
681 struct sg_header *hdr;
682 struct sg_rdwr *rdwr;
685 int error = 0, cdb_len, buf_len, dir;
687 periph = dev->si_drv1;
688 rdwr = malloc(sizeof(*rdwr), M_DEVBUF, M_WAITOK | M_ZERO);
689 hdr = &rdwr->hdr.hdr;
691 /* Copy in the header block and sanity check it */
692 if (uio->uio_resid < sizeof(*hdr)) {
696 error = uiomove(hdr, sizeof(*hdr), uio);
700 /* XXX: We don't support SG 3.x read/write API. */
701 if (hdr->reply_len < 0) {
706 ccb = xpt_alloc_ccb();
714 * Copy in the CDB block. The designers of the interface didn't
715 * bother to provide a size for this in the header, so we have to
716 * figure it out ourselves.
718 if (uio->uio_resid < 1)
720 error = uiomove(&cdb_cmd, 1, uio);
723 if (hdr->twelve_byte)
726 cdb_len = scsi_group_len(cdb_cmd);
728 * We've already read the first byte of the CDB and advanced the uio
729 * pointer. Just read the rest.
731 csio->cdb_io.cdb_bytes[0] = cdb_cmd;
732 error = uiomove(&csio->cdb_io.cdb_bytes[1], cdb_len - 1, uio);
737 * Now set up the data block. Again, the designers didn't bother
738 * to make this reliable.
740 buf_len = uio->uio_resid;
742 buf = malloc(buf_len, M_DEVBUF, M_WAITOK | M_ZERO);
743 error = uiomove(buf, buf_len, uio);
747 } else if (hdr->reply_len != 0) {
748 buf = malloc(hdr->reply_len, M_DEVBUF, M_WAITOK | M_ZERO);
749 buf_len = hdr->reply_len;
757 cam_periph_lock(periph);
759 xpt_setup_ccb(&ccb->ccb_h, periph->path, CAM_PRIORITY_NORMAL);
772 * Send off the command and hope that it works. This path does not
773 * go through sgstart because the I/O is supposed to be asynchronous.
776 rdwr->buf_len = buf_len;
777 rdwr->tag = hdr->pack_id;
779 rdwr->state = SG_RDWR_INPROG;
780 ccb->ccb_h.ccb_rdwr = rdwr;
781 ccb->ccb_h.ccb_type = SG_CCB_RDWR_IO;
782 TAILQ_INSERT_TAIL(&sc->rdwr_done, rdwr, rdwr_link);
783 error = sgsendrdwr(periph, ccb);
784 cam_periph_unlock(periph);
792 free(rdwr, M_DEVBUF);
797 sgread(struct cdev *dev, struct uio *uio, int ioflag)
799 struct ccb_scsiio *csio;
800 struct cam_periph *periph;
802 struct sg_header *hdr;
803 struct sg_rdwr *rdwr;
804 u_short hstat, dstat;
805 int error, pack_len, reply_len, pack_id;
807 periph = dev->si_drv1;
809 /* XXX The pack len field needs to be updated and written out instead
810 * of discarded. Not sure how to do that.
812 uio->uio_rw = UIO_WRITE;
813 if ((error = uiomove(&pack_len, 4, uio)) != 0)
815 if ((error = uiomove(&reply_len, 4, uio)) != 0)
817 if ((error = uiomove(&pack_id, 4, uio)) != 0)
819 uio->uio_rw = UIO_READ;
821 cam_periph_lock(periph);
824 TAILQ_FOREACH(rdwr, &sc->rdwr_done, rdwr_link) {
825 if (rdwr->tag == pack_id)
828 if ((rdwr == NULL) || (rdwr->state != SG_RDWR_DONE)) {
829 if (cam_periph_sleep(periph, rdwr, PCATCH, "sgread", 0) == ERESTART)
833 TAILQ_REMOVE(&sc->rdwr_done, rdwr, rdwr_link);
834 cam_periph_unlock(periph);
836 hdr = &rdwr->hdr.hdr;
837 csio = &rdwr->ccb->csio;
838 sg_scsiio_status(csio, &hstat, &dstat);
839 hdr->host_status = hstat;
840 hdr->driver_status = dstat;
841 hdr->target_status = csio->scsi_status >> 1;
845 case DID_PASSTHROUGH:
865 if (dstat == DRIVER_SENSE) {
866 bcopy(&csio->sense_data, hdr->sense_buffer,
867 min(csio->sense_len, SG_MAX_SENSE));
869 scsi_sense_print(csio);
873 error = uiomove(&hdr->result, sizeof(*hdr) -
874 offsetof(struct sg_header, result), uio);
875 if ((error == 0) && (hdr->result == 0))
876 error = uiomove(rdwr->buf, rdwr->buf_len, uio);
878 cam_periph_lock(periph);
879 xpt_free_ccb(rdwr->ccb);
880 cam_periph_unlock(periph);
881 free(rdwr->buf, M_DEVBUF);
882 free(rdwr, M_DEVBUF);
887 sgsendccb(struct cam_periph *periph, union ccb *ccb)
889 struct sg_softc *softc;
890 struct cam_periph_map_info mapinfo;
893 softc = periph->softc;
894 bzero(&mapinfo, sizeof(mapinfo));
897 * cam_periph_mapmem calls into proc and vm functions that can
898 * sleep as well as trigger I/O, so we can't hold the lock.
899 * Dropping it here is reasonably safe.
900 * The only CCB opcode that is possible here is XPT_SCSI_IO, no
901 * need for additional checks.
903 cam_periph_unlock(periph);
904 error = cam_periph_mapmem(ccb, &mapinfo, softc->maxio);
905 cam_periph_lock(periph);
909 error = cam_periph_runccb(ccb,
913 softc->device_stats);
915 cam_periph_unlock(periph);
916 cam_periph_unmapmem(ccb, &mapinfo);
917 cam_periph_lock(periph);
923 sgsendrdwr(struct cam_periph *periph, union ccb *ccb)
925 struct sg_softc *softc;
927 softc = periph->softc;
928 devstat_start_transaction(softc->device_stats, NULL);
934 sgerror(union ccb *ccb, uint32_t cam_flags, uint32_t sense_flags)
936 struct cam_periph *periph;
937 struct sg_softc *softc;
939 periph = xpt_path_periph(ccb->ccb_h.path);
940 softc = (struct sg_softc *)periph->softc;
942 return (cam_periph_error(ccb, cam_flags, sense_flags));
946 sg_scsiio_status(struct ccb_scsiio *csio, u_short *hoststat, u_short *drvstat)
950 status = csio->ccb_h.status;
952 switch (status & CAM_STATUS_MASK) {
957 case CAM_REQ_CMP_ERR:
958 *hoststat = DID_ERROR;
961 case CAM_REQ_ABORTED:
962 *hoststat = DID_ABORT;
965 case CAM_REQ_INVALID:
966 *hoststat = DID_ERROR;
967 *drvstat = DRIVER_INVALID;
969 case CAM_DEV_NOT_THERE:
970 *hoststat = DID_BAD_TARGET;
973 case CAM_SEL_TIMEOUT:
974 *hoststat = DID_NO_CONNECT;
977 case CAM_CMD_TIMEOUT:
978 *hoststat = DID_TIME_OUT;
981 case CAM_SCSI_STATUS_ERROR:
982 *hoststat = DID_ERROR;
985 case CAM_SCSI_BUS_RESET:
986 *hoststat = DID_RESET;
989 case CAM_UNCOR_PARITY:
990 *hoststat = DID_PARITY;
994 *hoststat = DID_BUS_BUSY;
998 *hoststat = DID_ERROR;
999 *drvstat = DRIVER_ERROR;
1002 if (status & CAM_AUTOSNS_VALID)
1003 *drvstat = DRIVER_SENSE;
1007 scsi_group_len(u_char cmd)
1009 int len[] = {6, 10, 10, 12, 12, 12, 10, 10};
1012 group = (cmd >> 5) & 0x7;
1013 return (len[group]);