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.
244 sgcleanup(struct cam_periph *periph)
246 struct sg_softc *softc;
248 softc = (struct sg_softc *)periph->softc;
250 devstat_remove_entry(softc->device_stats);
252 free(softc, M_DEVBUF);
256 sgasync(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
258 struct cam_periph *periph;
260 periph = (struct cam_periph *)callback_arg;
263 case AC_FOUND_DEVICE:
265 struct ccb_getdev *cgd;
268 cgd = (struct ccb_getdev *)arg;
272 if (cgd->protocol != PROTO_SCSI)
276 * Allocate a peripheral instance for this device and
277 * start the probe process.
279 status = cam_periph_alloc(sgregister, sgoninvalidate,
280 sgcleanup, sgstart, "sg",
281 CAM_PERIPH_BIO, cgd->ccb_h.path,
282 sgasync, AC_FOUND_DEVICE, cgd);
283 if ((status != CAM_REQ_CMP) && (status != CAM_REQ_INPROG)) {
284 const struct cam_status_entry *entry;
286 entry = cam_fetch_status_entry(status);
287 printf("sgasync: Unable to attach new device "
288 "due to status %#x: %s\n", status, entry ?
289 entry->status_text : "Unknown");
294 cam_periph_async(periph, code, path, arg);
300 sgregister(struct cam_periph *periph, void *arg)
302 struct sg_softc *softc;
303 struct ccb_getdev *cgd;
304 struct ccb_pathinq cpi;
307 cgd = (struct ccb_getdev *)arg;
309 printf("sgregister: no getdev CCB, can't register device\n");
310 return (CAM_REQ_CMP_ERR);
313 softc = malloc(sizeof(*softc), M_DEVBUF, M_ZERO | M_NOWAIT);
315 printf("sgregister: Unable to allocate softc\n");
316 return (CAM_REQ_CMP_ERR);
319 softc->state = SG_STATE_NORMAL;
320 softc->pd_type = SID_TYPE(&cgd->inq_data);
321 softc->sg_timeout = SG_DEFAULT_TIMEOUT / SG_DEFAULT_HZ * hz;
322 softc->sg_user_timeout = SG_DEFAULT_TIMEOUT;
323 TAILQ_INIT(&softc->rdwr_done);
324 periph->softc = softc;
326 bzero(&cpi, sizeof(cpi));
327 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
328 cpi.ccb_h.func_code = XPT_PATH_INQ;
329 xpt_action((union ccb *)&cpi);
332 * We pass in 0 for all blocksize, since we don't know what the
333 * blocksize of the device is, if it even has a blocksize.
335 cam_periph_unlock(periph);
336 no_tags = (cgd->inq_data.flags & SID_CmdQue) == 0;
337 softc->device_stats = devstat_new_entry("sg",
338 periph->unit_number, 0,
340 | (no_tags ? DEVSTAT_NO_ORDERED_TAGS : 0),
342 XPORT_DEVSTAT_TYPE(cpi.transport) |
344 DEVSTAT_PRIORITY_PASS);
347 * Acquire a reference to the periph before we create the devfs
348 * instance for it. We'll release this reference once the devfs
349 * instance has been freed.
351 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
352 xpt_print(periph->path, "%s: lost periph during "
353 "registration!\n", __func__);
354 cam_periph_lock(periph);
355 return (CAM_REQ_CMP_ERR);
358 /* Register the device */
359 softc->dev = make_dev(&sg_cdevsw, periph->unit_number,
360 UID_ROOT, GID_OPERATOR, 0600, "%s%d",
361 periph->periph_name, periph->unit_number);
362 if (periph->unit_number < 26) {
363 (void)make_dev_alias(softc->dev, "sg%c",
364 periph->unit_number + 'a');
366 (void)make_dev_alias(softc->dev, "sg%c%c",
367 ((periph->unit_number / 26) - 1) + 'a',
368 (periph->unit_number % 26) + 'a');
370 cam_periph_lock(periph);
371 softc->dev->si_drv1 = periph;
374 * Add as async callback so that we get
375 * notified if this device goes away.
377 xpt_register_async(AC_LOST_DEVICE, sgasync, periph, periph->path);
380 xpt_announce_periph(periph, NULL);
382 return (CAM_REQ_CMP);
386 sgstart(struct cam_periph *periph, union ccb *start_ccb)
388 struct sg_softc *softc;
390 softc = (struct sg_softc *)periph->softc;
392 switch (softc->state) {
393 case SG_STATE_NORMAL:
394 start_ccb->ccb_h.ccb_type = SG_CCB_WAITING;
395 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
397 periph->immediate_priority = CAM_PRIORITY_NONE;
398 wakeup(&periph->ccb_list);
404 sgdone(struct cam_periph *periph, union ccb *done_ccb)
406 struct sg_softc *softc;
407 struct ccb_scsiio *csio;
409 softc = (struct sg_softc *)periph->softc;
410 csio = &done_ccb->csio;
411 switch (csio->ccb_h.ccb_type) {
413 /* Caller will release the CCB */
414 wakeup(&done_ccb->ccb_h.cbfcnp);
418 struct sg_rdwr *rdwr;
421 devstat_end_transaction(softc->device_stats,
423 csio->tag_action & 0xf,
424 ((csio->ccb_h.flags & CAM_DIR_MASK) ==
425 CAM_DIR_NONE) ? DEVSTAT_NO_DATA :
426 (csio->ccb_h.flags & CAM_DIR_OUT) ?
427 DEVSTAT_WRITE : DEVSTAT_READ,
430 rdwr = done_ccb->ccb_h.ccb_rdwr;
432 rdwr->state = SG_RDWR_DONE;
437 panic("unknown sg CCB type");
442 sgopen(struct cdev *dev, int flags, int fmt, struct thread *td)
444 struct cam_periph *periph;
445 struct sg_softc *softc;
448 periph = (struct cam_periph *)dev->si_drv1;
452 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
456 * Don't allow access when we're running at a high securelevel.
458 error = securelevel_gt(td->td_ucred, 1);
460 cam_periph_release(periph);
464 cam_periph_lock(periph);
466 softc = (struct sg_softc *)periph->softc;
467 if (softc->flags & SG_FLAG_INVALID) {
468 cam_periph_release_locked(periph);
469 cam_periph_unlock(periph);
475 cam_periph_unlock(periph);
481 sgclose(struct cdev *dev, int flag, int fmt, struct thread *td)
484 struct cam_periph *periph;
485 struct sg_softc *softc;
487 periph = (struct cam_periph *)dev->si_drv1;
492 softc = periph->softc;
498 cam_periph_release_locked(periph);
501 * We reference the SIM lock directly here, instead of using
502 * cam_periph_unlock(). The reason is that the call to
503 * cam_periph_release_locked() above could result in the periph
504 * getting freed. If that is the case, dereferencing the periph
505 * with a cam_periph_unlock() call would cause a page fault.
507 * cam_periph_release() avoids this problem using the same method,
508 * but we're manually acquiring and dropping the lock here to
509 * protect the open count and avoid another lock acquisition and
512 mtx_unlock(sim->mtx);
518 sgioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *td)
521 struct ccb_scsiio *csio;
522 struct cam_periph *periph;
523 struct sg_softc *softc;
524 struct sg_io_hdr req;
527 periph = (struct cam_periph *)dev->si_drv1;
531 cam_periph_lock(periph);
533 softc = (struct sg_softc *)periph->softc;
537 case LINUX_SCSI_GET_BUS_NUMBER: {
540 busno = xpt_path_path_id(periph->path);
541 error = copyout(&busno, arg, sizeof(busno));
544 case LINUX_SCSI_GET_IDLUN: {
545 struct scsi_idlun idlun;
548 idlun.dev_id = xpt_path_target_id(periph->path);
549 sim = xpt_path_sim(periph->path);
550 idlun.host_unique_id = sim->unit_number;
551 error = copyout(&idlun, arg, sizeof(idlun));
554 case SG_GET_VERSION_NUM:
555 case LINUX_SG_GET_VERSION_NUM:
556 error = copyout(&sg_version, arg, sizeof(sg_version));
559 case LINUX_SG_SET_TIMEOUT: {
562 error = copyin(arg, &user_timeout, sizeof(u_int));
564 softc->sg_user_timeout = user_timeout;
565 softc->sg_timeout = user_timeout / SG_DEFAULT_HZ * hz;
570 case LINUX_SG_GET_TIMEOUT:
572 * The value is returned directly to the syscall.
574 td->td_retval[0] = softc->sg_user_timeout;
579 error = copyin(arg, &req, sizeof(req));
583 if (req.cmd_len > IOCDBLEN) {
588 if (req.iovec_count != 0) {
593 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
596 error = copyin(req.cmdp, &csio->cdb_io.cdb_bytes,
599 xpt_release_ccb(ccb);
603 switch(req.dxfer_direction) {
604 case SG_DXFER_TO_DEV:
607 case SG_DXFER_FROM_DEV:
610 case SG_DXFER_TO_FROM_DEV:
611 dir = CAM_DIR_IN | CAM_DIR_OUT;
630 error = sgsendccb(periph, ccb);
632 req.host_status = DID_ERROR;
633 req.driver_status = DRIVER_INVALID;
634 xpt_release_ccb(ccb);
638 req.status = csio->scsi_status;
639 req.masked_status = (csio->scsi_status >> 1) & 0x7f;
640 sg_scsiio_status(csio, &req.host_status, &req.driver_status);
641 req.resid = csio->resid;
642 req.duration = csio->ccb_h.timeout;
645 error = copyout(&req, arg, sizeof(req));
646 if ((error == 0) && (csio->ccb_h.status & CAM_AUTOSNS_VALID)
647 && (req.sbp != NULL)) {
648 req.sb_len_wr = req.mx_sb_len - csio->sense_resid;
649 error = copyout(&csio->sense_data, req.sbp,
653 xpt_release_ccb(ccb);
656 case SG_GET_RESERVED_SIZE:
657 case LINUX_SG_GET_RESERVED_SIZE: {
660 error = copyout(&size, arg, sizeof(size));
665 case LINUX_SG_GET_SCSI_ID:
667 struct sg_scsi_id id;
669 id.host_no = cam_sim_path(xpt_path_sim(periph->path));
670 id.channel = xpt_path_path_id(periph->path);
671 id.scsi_id = xpt_path_target_id(periph->path);
672 id.lun = xpt_path_lun_id(periph->path);
673 id.scsi_type = softc->pd_type;
674 id.h_cmd_per_lun = 1;
675 id.d_queue_depth = 1;
679 error = copyout(&id, arg, sizeof(id));
683 case SG_EMULATED_HOST:
684 case SG_SET_TRANSFORM:
685 case SG_GET_TRANSFORM:
686 case SG_GET_NUM_WAITING:
688 case SG_GET_REQUEST_TABLE:
689 case SG_SET_KEEP_ORPHAN:
690 case SG_GET_KEEP_ORPHAN:
691 case SG_GET_ACCESS_COUNT:
692 case SG_SET_FORCE_LOW_DMA:
694 case SG_GET_SG_TABLESIZE:
695 case SG_SET_FORCE_PACK_ID:
697 case SG_SET_RESERVED_SIZE:
698 case SG_GET_COMMAND_Q:
699 case SG_SET_COMMAND_Q:
701 case SG_NEXT_CMD_LEN:
702 case LINUX_SG_EMULATED_HOST:
703 case LINUX_SG_SET_TRANSFORM:
704 case LINUX_SG_GET_TRANSFORM:
705 case LINUX_SG_GET_NUM_WAITING:
706 case LINUX_SG_SCSI_RESET:
707 case LINUX_SG_GET_REQUEST_TABLE:
708 case LINUX_SG_SET_KEEP_ORPHAN:
709 case LINUX_SG_GET_KEEP_ORPHAN:
710 case LINUX_SG_GET_ACCESS_COUNT:
711 case LINUX_SG_SET_FORCE_LOW_DMA:
712 case LINUX_SG_GET_LOW_DMA:
713 case LINUX_SG_GET_SG_TABLESIZE:
714 case LINUX_SG_SET_FORCE_PACK_ID:
715 case LINUX_SG_GET_PACK_ID:
716 case LINUX_SG_SET_RESERVED_SIZE:
717 case LINUX_SG_GET_COMMAND_Q:
718 case LINUX_SG_SET_COMMAND_Q:
719 case LINUX_SG_SET_DEBUG:
720 case LINUX_SG_NEXT_CMD_LEN:
723 printf("sgioctl: rejecting cmd 0x%lx\n", cmd);
729 cam_periph_unlock(periph);
734 sgwrite(struct cdev *dev, struct uio *uio, int ioflag)
737 struct cam_periph *periph;
738 struct ccb_scsiio *csio;
740 struct sg_header *hdr;
741 struct sg_rdwr *rdwr;
744 int error = 0, cdb_len, buf_len, dir;
746 periph = dev->si_drv1;
747 rdwr = malloc(sizeof(*rdwr), M_DEVBUF, M_WAITOK | M_ZERO);
748 hdr = &rdwr->hdr.hdr;
750 /* Copy in the header block and sanity check it */
751 if (uio->uio_resid < sizeof(*hdr)) {
755 error = uiomove(hdr, sizeof(*hdr), uio);
759 ccb = xpt_alloc_ccb();
767 * Copy in the CDB block. The designers of the interface didn't
768 * bother to provide a size for this in the header, so we have to
769 * figure it out ourselves.
771 if (uio->uio_resid < 1)
773 error = uiomove(&cdb_cmd, 1, uio);
776 if (hdr->twelve_byte)
779 cdb_len = scsi_group_len(cdb_cmd);
781 * We've already read the first byte of the CDB and advanced the uio
782 * pointer. Just read the rest.
784 csio->cdb_io.cdb_bytes[0] = cdb_cmd;
785 error = uiomove(&csio->cdb_io.cdb_bytes[1], cdb_len - 1, uio);
790 * Now set up the data block. Again, the designers didn't bother
791 * to make this reliable.
793 buf_len = uio->uio_resid;
795 buf = malloc(buf_len, M_DEVBUF, M_WAITOK | M_ZERO);
796 error = uiomove(buf, buf_len, uio);
800 } else if (hdr->reply_len != 0) {
801 buf = malloc(hdr->reply_len, M_DEVBUF, M_WAITOK | M_ZERO);
802 buf_len = hdr->reply_len;
810 cam_periph_lock(periph);
812 xpt_setup_ccb(&ccb->ccb_h, periph->path, CAM_PRIORITY_NORMAL);
825 * Send off the command and hope that it works. This path does not
826 * go through sgstart because the I/O is supposed to be asynchronous.
829 rdwr->buf_len = buf_len;
830 rdwr->tag = hdr->pack_id;
832 rdwr->state = SG_RDWR_INPROG;
833 ccb->ccb_h.ccb_rdwr = rdwr;
834 ccb->ccb_h.ccb_type = SG_CCB_RDWR_IO;
835 TAILQ_INSERT_TAIL(&sc->rdwr_done, rdwr, rdwr_link);
836 error = sgsendrdwr(periph, ccb);
837 cam_periph_unlock(periph);
845 free(rdwr, M_DEVBUF);
850 sgread(struct cdev *dev, struct uio *uio, int ioflag)
852 struct ccb_scsiio *csio;
853 struct cam_periph *periph;
855 struct sg_header *hdr;
856 struct sg_rdwr *rdwr;
857 u_short hstat, dstat;
858 int error, pack_len, reply_len, pack_id;
860 periph = dev->si_drv1;
862 /* XXX The pack len field needs to be updated and written out instead
863 * of discarded. Not sure how to do that.
865 uio->uio_rw = UIO_WRITE;
866 if ((error = uiomove(&pack_len, 4, uio)) != 0)
868 if ((error = uiomove(&reply_len, 4, uio)) != 0)
870 if ((error = uiomove(&pack_id, 4, uio)) != 0)
872 uio->uio_rw = UIO_READ;
874 cam_periph_lock(periph);
877 TAILQ_FOREACH(rdwr, &sc->rdwr_done, rdwr_link) {
878 if (rdwr->tag == pack_id)
881 if ((rdwr == NULL) || (rdwr->state != SG_RDWR_DONE)) {
882 if (msleep(rdwr, periph->sim->mtx, PCATCH, "sgread", 0) == ERESTART)
886 TAILQ_REMOVE(&sc->rdwr_done, rdwr, rdwr_link);
887 cam_periph_unlock(periph);
889 hdr = &rdwr->hdr.hdr;
890 csio = &rdwr->ccb->csio;
891 sg_scsiio_status(csio, &hstat, &dstat);
892 hdr->host_status = hstat;
893 hdr->driver_status = dstat;
894 hdr->target_status = csio->scsi_status >> 1;
898 case DID_PASSTHROUGH:
918 if (dstat == DRIVER_SENSE) {
919 bcopy(&csio->sense_data, hdr->sense_buffer,
920 min(csio->sense_len, SG_MAX_SENSE));
922 scsi_sense_print(csio);
926 error = uiomove(&hdr->result, sizeof(*hdr) -
927 offsetof(struct sg_header, result), uio);
928 if ((error == 0) && (hdr->result == 0))
929 error = uiomove(rdwr->buf, rdwr->buf_len, uio);
931 cam_periph_lock(periph);
932 xpt_free_ccb(rdwr->ccb);
933 cam_periph_unlock(periph);
934 free(rdwr->buf, M_DEVBUF);
935 free(rdwr, M_DEVBUF);
940 sgsendccb(struct cam_periph *periph, union ccb *ccb)
942 struct sg_softc *softc;
943 struct cam_periph_map_info mapinfo;
946 softc = periph->softc;
947 bzero(&mapinfo, sizeof(mapinfo));
950 * cam_periph_mapmem calls into proc and vm functions that can
951 * sleep as well as trigger I/O, so we can't hold the lock.
952 * Dropping it here is reasonably safe.
953 * The only CCB opcode that is possible here is XPT_SCSI_IO, no
954 * need for additional checks.
956 cam_periph_unlock(periph);
957 error = cam_periph_mapmem(ccb, &mapinfo);
958 cam_periph_lock(periph);
962 error = cam_periph_runccb(ccb,
966 softc->device_stats);
968 cam_periph_unmapmem(ccb, &mapinfo);
974 sgsendrdwr(struct cam_periph *periph, union ccb *ccb)
976 struct sg_softc *softc;
978 softc = periph->softc;
979 devstat_start_transaction(softc->device_stats, NULL);
985 sgerror(union ccb *ccb, uint32_t cam_flags, uint32_t sense_flags)
987 struct cam_periph *periph;
988 struct sg_softc *softc;
990 periph = xpt_path_periph(ccb->ccb_h.path);
991 softc = (struct sg_softc *)periph->softc;
993 return (cam_periph_error(ccb, cam_flags, sense_flags,
998 sg_scsiio_status(struct ccb_scsiio *csio, u_short *hoststat, u_short *drvstat)
1002 status = csio->ccb_h.status;
1004 switch (status & CAM_STATUS_MASK) {
1009 case CAM_REQ_CMP_ERR:
1010 *hoststat = DID_ERROR;
1013 case CAM_REQ_ABORTED:
1014 *hoststat = DID_ABORT;
1017 case CAM_REQ_INVALID:
1018 *hoststat = DID_ERROR;
1019 *drvstat = DRIVER_INVALID;
1021 case CAM_DEV_NOT_THERE:
1022 *hoststat = DID_BAD_TARGET;
1025 case CAM_SEL_TIMEOUT:
1026 *hoststat = DID_NO_CONNECT;
1029 case CAM_CMD_TIMEOUT:
1030 *hoststat = DID_TIME_OUT;
1033 case CAM_SCSI_STATUS_ERROR:
1034 *hoststat = DID_ERROR;
1037 case CAM_SCSI_BUS_RESET:
1038 *hoststat = DID_RESET;
1041 case CAM_UNCOR_PARITY:
1042 *hoststat = DID_PARITY;
1046 *hoststat = DID_BUS_BUSY;
1050 *hoststat = DID_ERROR;
1051 *drvstat = DRIVER_ERROR;
1054 if (status & CAM_AUTOSNS_VALID)
1055 *drvstat = DRIVER_SENSE;
1059 scsi_group_len(u_char cmd)
1061 int len[] = {6, 10, 10, 12, 12, 12, 10, 10};
1064 group = (cmd >> 5) & 0x7;
1065 return (len[group]);