2 * Copyright (c) 2007 Scott Long
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions, and the following disclaimer,
10 * without modification, immediately at the beginning of the file.
11 * 2. The name of the author may not be used to endorse or promote products
12 * derived from this software without specific prior written permission.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
18 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * scsi_sg peripheral driver. This driver is meant to implement the Linux
29 * SG passthrough interface for SCSI.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/types.h>
40 #include <sys/malloc.h>
41 #include <sys/fcntl.h>
42 #include <sys/ioccom.h>
44 #include <sys/errno.h>
45 #include <sys/devicestat.h>
50 #include <cam/cam_ccb.h>
51 #include <cam/cam_periph.h>
52 #include <cam/cam_queue.h>
53 #include <cam/cam_xpt_periph.h>
54 #include <cam/cam_debug.h>
55 #include <cam/cam_sim.h>
57 #include <cam/scsi/scsi_all.h>
58 #include <cam/scsi/scsi_message.h>
59 #include <cam/scsi/scsi_sg.h>
61 #include <compat/linux/linux_ioctl.h>
64 SG_FLAG_LOCKED = 0x01,
65 SG_FLAG_INVALID = 0x02
83 #define ccb_type ppriv_field0
84 #define ccb_rdwr ppriv_ptr1
87 TAILQ_ENTRY(sg_rdwr) rdwr_link;
95 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 periph_start_t sgstart;
123 static void sgasync(void *callback_arg, uint32_t code,
124 struct cam_path *path, void *arg);
125 static void sgdone(struct cam_periph *periph, union ccb *done_ccb);
126 static int sgsendccb(struct cam_periph *periph, union ccb *ccb);
127 static int sgsendrdwr(struct cam_periph *periph, union ccb *ccb);
128 static int sgerror(union ccb *ccb, uint32_t cam_flags,
129 uint32_t sense_flags);
130 static void sg_scsiio_status(struct ccb_scsiio *csio,
131 u_short *hoststat, u_short *drvstat);
133 static int scsi_group_len(u_char cmd);
135 static struct periph_driver sgdriver =
138 TAILQ_HEAD_INITIALIZER(sgdriver.units), /* gen */ 0
140 PERIPHDRIVER_DECLARE(sg, sgdriver);
142 static struct cdevsw sg_cdevsw = {
143 .d_version = D_VERSION,
144 .d_flags = D_NEEDGIANT | D_TRACKCLOSE,
153 static int sg_version = 30125;
161 * Install a global async callback. This callback will receive aync
162 * callbacks like "new device found".
164 status = xpt_register_async(AC_FOUND_DEVICE, sgasync, NULL, NULL);
166 if (status != CAM_REQ_CMP) {
167 printf("sg: Failed to attach master async callbac "
168 "due to status 0x%x!\n", status);
173 sgdevgonecb(void *arg)
176 struct cam_periph *periph;
177 struct sg_softc *softc;
180 periph = (struct cam_periph *)arg;
182 softc = (struct sg_softc *)periph->softc;
184 KASSERT(softc->open_count >= 0, ("Negative open count %d",
190 * When we get this callback, we will get no more close calls from
191 * devfs. So if we have any dangling opens, we need to release the
192 * reference held for that particular context.
194 for (i = 0; i < softc->open_count; i++)
195 cam_periph_release_locked(periph);
197 softc->open_count = 0;
200 * Release the reference held for the device node, it is gone now.
202 cam_periph_release_locked(periph);
205 * We reference the SIM lock directly here, instead of using
206 * cam_periph_unlock(). The reason is that the final call to
207 * cam_periph_release_locked() above could result in the periph
208 * getting freed. If that is the case, dereferencing the periph
209 * with a cam_periph_unlock() call would cause a page fault.
211 mtx_unlock(sim->mtx);
216 sgoninvalidate(struct cam_periph *periph)
218 struct sg_softc *softc;
220 softc = (struct sg_softc *)periph->softc;
223 * Deregister any async callbacks.
225 xpt_register_async(0, sgasync, periph, periph->path);
227 softc->flags |= SG_FLAG_INVALID;
230 * Tell devfs this device has gone away, and ask for a callback
231 * when it has cleaned up its state.
233 destroy_dev_sched_cb(softc->dev, sgdevgonecb, periph);
236 * XXX Return all queued I/O with ENXIO.
237 * XXX Handle any transactions queued to the card
238 * with XPT_ABORT_CCB.
242 xpt_print(periph->path, "lost device\n");
247 sgcleanup(struct cam_periph *periph)
249 struct sg_softc *softc;
251 softc = (struct sg_softc *)periph->softc;
253 xpt_print(periph->path, "removing device entry\n");
255 devstat_remove_entry(softc->device_stats);
257 free(softc, M_DEVBUF);
261 sgasync(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
263 struct cam_periph *periph;
265 periph = (struct cam_periph *)callback_arg;
268 case AC_FOUND_DEVICE:
270 struct ccb_getdev *cgd;
273 cgd = (struct ccb_getdev *)arg;
277 if (cgd->protocol != PROTO_SCSI)
281 * Allocate a peripheral instance for this device and
282 * start the probe process.
284 status = cam_periph_alloc(sgregister, sgoninvalidate,
285 sgcleanup, sgstart, "sg",
286 CAM_PERIPH_BIO, cgd->ccb_h.path,
287 sgasync, AC_FOUND_DEVICE, cgd);
288 if ((status != CAM_REQ_CMP) && (status != CAM_REQ_INPROG)) {
289 const struct cam_status_entry *entry;
291 entry = cam_fetch_status_entry(status);
292 printf("sgasync: Unable to attach new device "
293 "due to status %#x: %s\n", status, entry ?
294 entry->status_text : "Unknown");
299 cam_periph_async(periph, code, path, arg);
305 sgregister(struct cam_periph *periph, void *arg)
307 struct sg_softc *softc;
308 struct ccb_getdev *cgd;
309 struct ccb_pathinq cpi;
312 cgd = (struct ccb_getdev *)arg;
314 printf("sgregister: no getdev CCB, can't register device\n");
315 return (CAM_REQ_CMP_ERR);
318 softc = malloc(sizeof(*softc), M_DEVBUF, M_ZERO | M_NOWAIT);
320 printf("sgregister: Unable to allocate softc\n");
321 return (CAM_REQ_CMP_ERR);
324 softc->state = SG_STATE_NORMAL;
325 softc->pd_type = SID_TYPE(&cgd->inq_data);
326 softc->sg_timeout = SG_DEFAULT_TIMEOUT / SG_DEFAULT_HZ * hz;
327 softc->sg_user_timeout = SG_DEFAULT_TIMEOUT;
328 TAILQ_INIT(&softc->rdwr_done);
329 periph->softc = softc;
331 bzero(&cpi, sizeof(cpi));
332 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
333 cpi.ccb_h.func_code = XPT_PATH_INQ;
334 xpt_action((union ccb *)&cpi);
337 * We pass in 0 for all blocksize, since we don't know what the
338 * blocksize of the device is, if it even has a blocksize.
340 cam_periph_unlock(periph);
341 no_tags = (cgd->inq_data.flags & SID_CmdQue) == 0;
342 softc->device_stats = devstat_new_entry("sg",
343 periph->unit_number, 0,
345 | (no_tags ? DEVSTAT_NO_ORDERED_TAGS : 0),
347 XPORT_DEVSTAT_TYPE(cpi.transport) |
349 DEVSTAT_PRIORITY_PASS);
352 * Acquire a reference to the periph before we create the devfs
353 * instance for it. We'll release this reference once the devfs
354 * instance has been freed.
356 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
357 xpt_print(periph->path, "%s: lost periph during "
358 "registration!\n", __func__);
359 cam_periph_lock(periph);
360 return (CAM_REQ_CMP_ERR);
363 /* Register the device */
364 softc->dev = make_dev(&sg_cdevsw, periph->unit_number,
365 UID_ROOT, GID_OPERATOR, 0600, "%s%d",
366 periph->periph_name, periph->unit_number);
367 if (periph->unit_number < 26) {
368 (void)make_dev_alias(softc->dev, "sg%c",
369 periph->unit_number + 'a');
371 (void)make_dev_alias(softc->dev, "sg%c%c",
372 ((periph->unit_number / 26) - 1) + 'a',
373 (periph->unit_number % 26) + 'a');
375 cam_periph_lock(periph);
376 softc->dev->si_drv1 = periph;
379 * Add as async callback so that we get
380 * notified if this device goes away.
382 xpt_register_async(AC_LOST_DEVICE, sgasync, periph, periph->path);
385 xpt_announce_periph(periph, NULL);
387 return (CAM_REQ_CMP);
391 sgstart(struct cam_periph *periph, union ccb *start_ccb)
393 struct sg_softc *softc;
395 softc = (struct sg_softc *)periph->softc;
397 switch (softc->state) {
398 case SG_STATE_NORMAL:
399 start_ccb->ccb_h.ccb_type = SG_CCB_WAITING;
400 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
402 periph->immediate_priority = CAM_PRIORITY_NONE;
403 wakeup(&periph->ccb_list);
409 sgdone(struct cam_periph *periph, union ccb *done_ccb)
411 struct sg_softc *softc;
412 struct ccb_scsiio *csio;
414 softc = (struct sg_softc *)periph->softc;
415 csio = &done_ccb->csio;
416 switch (csio->ccb_h.ccb_type) {
418 /* Caller will release the CCB */
419 wakeup(&done_ccb->ccb_h.cbfcnp);
423 struct sg_rdwr *rdwr;
426 devstat_end_transaction(softc->device_stats,
428 csio->tag_action & 0xf,
429 ((csio->ccb_h.flags & CAM_DIR_MASK) ==
430 CAM_DIR_NONE) ? DEVSTAT_NO_DATA :
431 (csio->ccb_h.flags & CAM_DIR_OUT) ?
432 DEVSTAT_WRITE : DEVSTAT_READ,
435 rdwr = done_ccb->ccb_h.ccb_rdwr;
437 rdwr->state = SG_RDWR_DONE;
442 panic("unknown sg CCB type");
447 sgopen(struct cdev *dev, int flags, int fmt, struct thread *td)
449 struct cam_periph *periph;
450 struct sg_softc *softc;
453 periph = (struct cam_periph *)dev->si_drv1;
457 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
461 * Don't allow access when we're running at a high securelevel.
463 error = securelevel_gt(td->td_ucred, 1);
465 cam_periph_release(periph);
469 cam_periph_lock(periph);
471 softc = (struct sg_softc *)periph->softc;
472 if (softc->flags & SG_FLAG_INVALID) {
473 cam_periph_release_locked(periph);
474 cam_periph_unlock(periph);
480 cam_periph_unlock(periph);
486 sgclose(struct cdev *dev, int flag, int fmt, struct thread *td)
489 struct cam_periph *periph;
490 struct sg_softc *softc;
492 periph = (struct cam_periph *)dev->si_drv1;
497 softc = periph->softc;
503 cam_periph_release_locked(periph);
506 * We reference the SIM lock directly here, instead of using
507 * cam_periph_unlock(). The reason is that the call to
508 * cam_periph_release_locked() above could result in the periph
509 * getting freed. If that is the case, dereferencing the periph
510 * with a cam_periph_unlock() call would cause a page fault.
512 * cam_periph_release() avoids this problem using the same method,
513 * but we're manually acquiring and dropping the lock here to
514 * protect the open count and avoid another lock acquisition and
517 mtx_unlock(sim->mtx);
523 sgioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *td)
526 struct ccb_scsiio *csio;
527 struct cam_periph *periph;
528 struct sg_softc *softc;
529 struct sg_io_hdr req;
532 periph = (struct cam_periph *)dev->si_drv1;
536 cam_periph_lock(periph);
538 softc = (struct sg_softc *)periph->softc;
542 case LINUX_SCSI_GET_BUS_NUMBER: {
545 busno = xpt_path_path_id(periph->path);
546 error = copyout(&busno, arg, sizeof(busno));
549 case LINUX_SCSI_GET_IDLUN: {
550 struct scsi_idlun idlun;
553 idlun.dev_id = xpt_path_target_id(periph->path);
554 sim = xpt_path_sim(periph->path);
555 idlun.host_unique_id = sim->unit_number;
556 error = copyout(&idlun, arg, sizeof(idlun));
559 case SG_GET_VERSION_NUM:
560 case LINUX_SG_GET_VERSION_NUM:
561 error = copyout(&sg_version, arg, sizeof(sg_version));
564 case LINUX_SG_SET_TIMEOUT: {
567 error = copyin(arg, &user_timeout, sizeof(u_int));
569 softc->sg_user_timeout = user_timeout;
570 softc->sg_timeout = user_timeout / SG_DEFAULT_HZ * hz;
575 case LINUX_SG_GET_TIMEOUT:
577 * The value is returned directly to the syscall.
579 td->td_retval[0] = softc->sg_user_timeout;
584 error = copyin(arg, &req, sizeof(req));
588 if (req.cmd_len > IOCDBLEN) {
593 if (req.iovec_count != 0) {
598 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
601 error = copyin(req.cmdp, &csio->cdb_io.cdb_bytes,
604 xpt_release_ccb(ccb);
608 switch(req.dxfer_direction) {
609 case SG_DXFER_TO_DEV:
612 case SG_DXFER_FROM_DEV:
615 case SG_DXFER_TO_FROM_DEV:
616 dir = CAM_DIR_IN | CAM_DIR_OUT;
635 error = sgsendccb(periph, ccb);
637 req.host_status = DID_ERROR;
638 req.driver_status = DRIVER_INVALID;
639 xpt_release_ccb(ccb);
643 req.status = csio->scsi_status;
644 req.masked_status = (csio->scsi_status >> 1) & 0x7f;
645 sg_scsiio_status(csio, &req.host_status, &req.driver_status);
646 req.resid = csio->resid;
647 req.duration = csio->ccb_h.timeout;
650 error = copyout(&req, arg, sizeof(req));
651 if ((error == 0) && (csio->ccb_h.status & CAM_AUTOSNS_VALID)
652 && (req.sbp != NULL)) {
653 req.sb_len_wr = req.mx_sb_len - csio->sense_resid;
654 error = copyout(&csio->sense_data, req.sbp,
658 xpt_release_ccb(ccb);
661 case SG_GET_RESERVED_SIZE:
662 case LINUX_SG_GET_RESERVED_SIZE: {
665 error = copyout(&size, arg, sizeof(size));
670 case LINUX_SG_GET_SCSI_ID:
672 struct sg_scsi_id id;
674 id.host_no = cam_sim_path(xpt_path_sim(periph->path));
675 id.channel = xpt_path_path_id(periph->path);
676 id.scsi_id = xpt_path_target_id(periph->path);
677 id.lun = xpt_path_lun_id(periph->path);
678 id.scsi_type = softc->pd_type;
679 id.h_cmd_per_lun = 1;
680 id.d_queue_depth = 1;
684 error = copyout(&id, arg, sizeof(id));
688 case SG_EMULATED_HOST:
689 case SG_SET_TRANSFORM:
690 case SG_GET_TRANSFORM:
691 case SG_GET_NUM_WAITING:
693 case SG_GET_REQUEST_TABLE:
694 case SG_SET_KEEP_ORPHAN:
695 case SG_GET_KEEP_ORPHAN:
696 case SG_GET_ACCESS_COUNT:
697 case SG_SET_FORCE_LOW_DMA:
699 case SG_GET_SG_TABLESIZE:
700 case SG_SET_FORCE_PACK_ID:
702 case SG_SET_RESERVED_SIZE:
703 case SG_GET_COMMAND_Q:
704 case SG_SET_COMMAND_Q:
706 case SG_NEXT_CMD_LEN:
707 case LINUX_SG_EMULATED_HOST:
708 case LINUX_SG_SET_TRANSFORM:
709 case LINUX_SG_GET_TRANSFORM:
710 case LINUX_SG_GET_NUM_WAITING:
711 case LINUX_SG_SCSI_RESET:
712 case LINUX_SG_GET_REQUEST_TABLE:
713 case LINUX_SG_SET_KEEP_ORPHAN:
714 case LINUX_SG_GET_KEEP_ORPHAN:
715 case LINUX_SG_GET_ACCESS_COUNT:
716 case LINUX_SG_SET_FORCE_LOW_DMA:
717 case LINUX_SG_GET_LOW_DMA:
718 case LINUX_SG_GET_SG_TABLESIZE:
719 case LINUX_SG_SET_FORCE_PACK_ID:
720 case LINUX_SG_GET_PACK_ID:
721 case LINUX_SG_SET_RESERVED_SIZE:
722 case LINUX_SG_GET_COMMAND_Q:
723 case LINUX_SG_SET_COMMAND_Q:
724 case LINUX_SG_SET_DEBUG:
725 case LINUX_SG_NEXT_CMD_LEN:
728 printf("sgioctl: rejecting cmd 0x%lx\n", cmd);
734 cam_periph_unlock(periph);
739 sgwrite(struct cdev *dev, struct uio *uio, int ioflag)
742 struct cam_periph *periph;
743 struct ccb_scsiio *csio;
745 struct sg_header *hdr;
746 struct sg_rdwr *rdwr;
749 int error = 0, cdb_len, buf_len, dir;
751 periph = dev->si_drv1;
752 rdwr = malloc(sizeof(*rdwr), M_DEVBUF, M_WAITOK | M_ZERO);
753 hdr = &rdwr->hdr.hdr;
755 /* Copy in the header block and sanity check it */
756 if (uio->uio_resid < sizeof(*hdr)) {
760 error = uiomove(hdr, sizeof(*hdr), uio);
764 ccb = xpt_alloc_ccb();
772 * Copy in the CDB block. The designers of the interface didn't
773 * bother to provide a size for this in the header, so we have to
774 * figure it out ourselves.
776 if (uio->uio_resid < 1)
778 error = uiomove(&cdb_cmd, 1, uio);
781 if (hdr->twelve_byte)
784 cdb_len = scsi_group_len(cdb_cmd);
786 * We've already read the first byte of the CDB and advanced the uio
787 * pointer. Just read the rest.
789 csio->cdb_io.cdb_bytes[0] = cdb_cmd;
790 error = uiomove(&csio->cdb_io.cdb_bytes[1], cdb_len - 1, uio);
795 * Now set up the data block. Again, the designers didn't bother
796 * to make this reliable.
798 buf_len = uio->uio_resid;
800 buf = malloc(buf_len, M_DEVBUF, M_WAITOK | M_ZERO);
801 error = uiomove(buf, buf_len, uio);
805 } else if (hdr->reply_len != 0) {
806 buf = malloc(hdr->reply_len, M_DEVBUF, M_WAITOK | M_ZERO);
807 buf_len = hdr->reply_len;
815 cam_periph_lock(periph);
817 xpt_setup_ccb(&ccb->ccb_h, periph->path, CAM_PRIORITY_NORMAL);
830 * Send off the command and hope that it works. This path does not
831 * go through sgstart because the I/O is supposed to be asynchronous.
834 rdwr->buf_len = buf_len;
835 rdwr->tag = hdr->pack_id;
837 rdwr->state = SG_RDWR_INPROG;
838 ccb->ccb_h.ccb_rdwr = rdwr;
839 ccb->ccb_h.ccb_type = SG_CCB_RDWR_IO;
840 TAILQ_INSERT_TAIL(&sc->rdwr_done, rdwr, rdwr_link);
841 error = sgsendrdwr(periph, ccb);
842 cam_periph_unlock(periph);
850 free(rdwr, M_DEVBUF);
855 sgread(struct cdev *dev, struct uio *uio, int ioflag)
857 struct ccb_scsiio *csio;
858 struct cam_periph *periph;
860 struct sg_header *hdr;
861 struct sg_rdwr *rdwr;
862 u_short hstat, dstat;
863 int error, pack_len, reply_len, pack_id;
865 periph = dev->si_drv1;
867 /* XXX The pack len field needs to be updated and written out instead
868 * of discarded. Not sure how to do that.
870 uio->uio_rw = UIO_WRITE;
871 if ((error = uiomove(&pack_len, 4, uio)) != 0)
873 if ((error = uiomove(&reply_len, 4, uio)) != 0)
875 if ((error = uiomove(&pack_id, 4, uio)) != 0)
877 uio->uio_rw = UIO_READ;
879 cam_periph_lock(periph);
882 TAILQ_FOREACH(rdwr, &sc->rdwr_done, rdwr_link) {
883 if (rdwr->tag == pack_id)
886 if ((rdwr == NULL) || (rdwr->state != SG_RDWR_DONE)) {
887 if (msleep(rdwr, periph->sim->mtx, PCATCH, "sgread", 0) == ERESTART)
891 TAILQ_REMOVE(&sc->rdwr_done, rdwr, rdwr_link);
892 cam_periph_unlock(periph);
894 hdr = &rdwr->hdr.hdr;
895 csio = &rdwr->ccb->csio;
896 sg_scsiio_status(csio, &hstat, &dstat);
897 hdr->host_status = hstat;
898 hdr->driver_status = dstat;
899 hdr->target_status = csio->scsi_status >> 1;
903 case DID_PASSTHROUGH:
923 if (dstat == DRIVER_SENSE) {
924 bcopy(&csio->sense_data, hdr->sense_buffer,
925 min(csio->sense_len, SG_MAX_SENSE));
927 scsi_sense_print(csio);
931 error = uiomove(&hdr->result, sizeof(*hdr) -
932 offsetof(struct sg_header, result), uio);
933 if ((error == 0) && (hdr->result == 0))
934 error = uiomove(rdwr->buf, rdwr->buf_len, uio);
936 cam_periph_lock(periph);
937 xpt_free_ccb(rdwr->ccb);
938 cam_periph_unlock(periph);
939 free(rdwr->buf, M_DEVBUF);
940 free(rdwr, M_DEVBUF);
945 sgsendccb(struct cam_periph *periph, union ccb *ccb)
947 struct sg_softc *softc;
948 struct cam_periph_map_info mapinfo;
951 softc = periph->softc;
952 bzero(&mapinfo, sizeof(mapinfo));
955 * cam_periph_mapmem calls into proc and vm functions that can
956 * sleep as well as trigger I/O, so we can't hold the lock.
957 * Dropping it here is reasonably safe.
958 * The only CCB opcode that is possible here is XPT_SCSI_IO, no
959 * need for additional checks.
961 cam_periph_unlock(periph);
962 error = cam_periph_mapmem(ccb, &mapinfo);
963 cam_periph_lock(periph);
967 error = cam_periph_runccb(ccb,
971 softc->device_stats);
973 cam_periph_unmapmem(ccb, &mapinfo);
979 sgsendrdwr(struct cam_periph *periph, union ccb *ccb)
981 struct sg_softc *softc;
983 softc = periph->softc;
984 devstat_start_transaction(softc->device_stats, NULL);
990 sgerror(union ccb *ccb, uint32_t cam_flags, uint32_t sense_flags)
992 struct cam_periph *periph;
993 struct sg_softc *softc;
995 periph = xpt_path_periph(ccb->ccb_h.path);
996 softc = (struct sg_softc *)periph->softc;
998 return (cam_periph_error(ccb, cam_flags, sense_flags,
1003 sg_scsiio_status(struct ccb_scsiio *csio, u_short *hoststat, u_short *drvstat)
1007 status = csio->ccb_h.status;
1009 switch (status & CAM_STATUS_MASK) {
1014 case CAM_REQ_CMP_ERR:
1015 *hoststat = DID_ERROR;
1018 case CAM_REQ_ABORTED:
1019 *hoststat = DID_ABORT;
1022 case CAM_REQ_INVALID:
1023 *hoststat = DID_ERROR;
1024 *drvstat = DRIVER_INVALID;
1026 case CAM_DEV_NOT_THERE:
1027 *hoststat = DID_BAD_TARGET;
1030 case CAM_SEL_TIMEOUT:
1031 *hoststat = DID_NO_CONNECT;
1034 case CAM_CMD_TIMEOUT:
1035 *hoststat = DID_TIME_OUT;
1038 case CAM_SCSI_STATUS_ERROR:
1039 *hoststat = DID_ERROR;
1042 case CAM_SCSI_BUS_RESET:
1043 *hoststat = DID_RESET;
1046 case CAM_UNCOR_PARITY:
1047 *hoststat = DID_PARITY;
1051 *hoststat = DID_BUS_BUSY;
1055 *hoststat = DID_ERROR;
1056 *drvstat = DRIVER_ERROR;
1059 if (status & CAM_AUTOSNS_VALID)
1060 *drvstat = DRIVER_SENSE;
1064 scsi_group_len(u_char cmd)
1066 int len[] = {6, 10, 10, 12, 12, 12, 10, 10};
1069 group = (cmd >> 5) & 0x7;
1070 return (len[group]);