2 * Copyright (c) 1997, 1998 Justin T. Gibbs.
3 * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer,
11 * without modification, immediately at the beginning of the file.
12 * 2. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * $FreeBSD: src/sys/cam/scsi/scsi_pass.c,v 1.19 2000/01/17 06:27:37 mjacob Exp $
28 * $DragonFly: src/sys/bus/cam/scsi/scsi_pass.c,v 1.19 2006/12/22 23:12:16 swildner Exp $
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/types.h>
36 #include <sys/malloc.h>
37 #include <sys/fcntl.h>
42 #include <sys/errno.h>
43 #include <sys/devicestat.h>
46 #include <sys/thread2.h>
49 #include "../cam_ccb.h"
50 #include "../cam_extend.h"
51 #include "../cam_periph.h"
52 #include "../cam_xpt_periph.h"
53 #include "../cam_debug.h"
56 #include "scsi_message.h"
58 #include "scsi_pass.h"
61 PASS_FLAG_OPEN = 0x01,
62 PASS_FLAG_LOCKED = 0x02,
63 PASS_FLAG_INVALID = 0x04
75 #define ccb_type ppriv_field0
76 #define ccb_bio ppriv_ptr1
82 struct bio_queue_head bio_queue;
84 struct devstat device_stats;
87 #define PASS_CDEV_MAJOR 31
89 static d_open_t passopen;
90 static d_close_t passclose;
91 static d_ioctl_t passioctl;
92 static d_strategy_t passstrategy;
94 static periph_init_t passinit;
95 static periph_ctor_t passregister;
96 static periph_oninv_t passoninvalidate;
97 static periph_dtor_t passcleanup;
98 static periph_start_t passstart;
99 static void passasync(void *callback_arg, u_int32_t code,
100 struct cam_path *path, void *arg);
101 static void passdone(struct cam_periph *periph,
102 union ccb *done_ccb);
103 static int passerror(union ccb *ccb, u_int32_t cam_flags,
104 u_int32_t sense_flags);
105 static int passsendccb(struct cam_periph *periph, union ccb *ccb,
108 static struct periph_driver passdriver =
111 TAILQ_HEAD_INITIALIZER(passdriver.units), /* generation */ 0
114 DATA_SET(periphdriver_set, passdriver);
116 static struct dev_ops pass_ops = {
117 { "pass", PASS_CDEV_MAJOR, 0 },
119 .d_close = passclose,
121 .d_write = physwrite,
122 .d_ioctl = passioctl,
123 .d_strategy = passstrategy,
126 static struct extend_array *passperiphs;
132 struct cam_path *path;
135 * Create our extend array for storing the devices we attach to.
137 passperiphs = cam_extend_new();
138 if (passperiphs == NULL) {
139 kprintf("passm: Failed to alloc extend array!\n");
144 * Install a global async callback. This callback will
145 * receive async callbacks like "new device found".
147 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
148 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
150 if (status == CAM_REQ_CMP) {
151 struct ccb_setasync csa;
153 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
154 csa.ccb_h.func_code = XPT_SASYNC_CB;
155 csa.event_enable = AC_FOUND_DEVICE;
156 csa.callback = passasync;
157 csa.callback_arg = NULL;
158 xpt_action((union ccb *)&csa);
159 status = csa.ccb_h.status;
163 if (status != CAM_REQ_CMP) {
164 kprintf("pass: Failed to attach master async callback "
165 "due to status 0x%x!\n", status);
171 passoninvalidate(struct cam_periph *periph)
173 struct pass_softc *softc;
176 struct ccb_setasync csa;
178 softc = (struct pass_softc *)periph->softc;
181 * De-register any async callbacks.
183 xpt_setup_ccb(&csa.ccb_h, periph->path,
185 csa.ccb_h.func_code = XPT_SASYNC_CB;
186 csa.event_enable = 0;
187 csa.callback = passasync;
188 csa.callback_arg = periph;
189 xpt_action((union ccb *)&csa);
191 softc->flags |= PASS_FLAG_INVALID;
194 * We need to be in a critical section here to keep the buffer
195 * queue from being modified while we traverse it.
200 * Return all queued I/O with ENXIO.
201 * XXX Handle any transactions queued to the card
202 * with XPT_ABORT_CCB.
204 while ((q_bio = bioq_first(&softc->bio_queue)) != NULL){
205 bioq_remove(&softc->bio_queue, q_bio);
206 q_bp = q_bio->bio_buf;
207 q_bp->b_resid = q_bp->b_bcount;
208 q_bp->b_error = ENXIO;
209 q_bp->b_flags |= B_ERROR;
215 xpt_print_path(periph->path);
216 kprintf("lost device\n");
222 passcleanup(struct cam_periph *periph)
224 struct pass_softc *softc;
226 softc = (struct pass_softc *)periph->softc;
228 devstat_remove_entry(&softc->device_stats);
230 cam_extend_release(passperiphs, periph->unit_number);
233 xpt_print_path(periph->path);
234 kprintf("removing device entry\n");
236 dev_ops_remove(&pass_ops, -1, periph->unit_number);
237 kfree(softc, M_DEVBUF);
241 passasync(void *callback_arg, u_int32_t code,
242 struct cam_path *path, void *arg)
244 struct cam_periph *periph;
246 periph = (struct cam_periph *)callback_arg;
249 case AC_FOUND_DEVICE:
251 struct ccb_getdev *cgd;
254 cgd = (struct ccb_getdev *)arg;
257 * Allocate a peripheral instance for
258 * this device and start the probe
261 status = cam_periph_alloc(passregister, passoninvalidate,
262 passcleanup, passstart, "pass",
263 CAM_PERIPH_BIO, cgd->ccb_h.path,
264 passasync, AC_FOUND_DEVICE, cgd);
266 if (status != CAM_REQ_CMP
267 && status != CAM_REQ_INPROG)
268 kprintf("passasync: Unable to attach new device "
269 "due to status 0x%x\n", status);
274 cam_periph_async(periph, code, path, arg);
280 passregister(struct cam_periph *periph, void *arg)
282 struct pass_softc *softc;
283 struct ccb_setasync csa;
284 struct ccb_getdev *cgd;
286 cgd = (struct ccb_getdev *)arg;
287 if (periph == NULL) {
288 kprintf("passregister: periph was NULL!!\n");
289 return(CAM_REQ_CMP_ERR);
293 kprintf("passregister: no getdev CCB, can't register device\n");
294 return(CAM_REQ_CMP_ERR);
297 softc = kmalloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO);
298 softc->state = PASS_STATE_NORMAL;
299 softc->pd_type = SID_TYPE(&cgd->inq_data);
300 bioq_init(&softc->bio_queue);
302 periph->softc = softc;
304 cam_extend_set(passperiphs, periph->unit_number, periph);
306 * We pass in 0 for a blocksize, since we don't
307 * know what the blocksize of this device is, if
308 * it even has a blocksize.
310 devstat_add_entry(&softc->device_stats, "pass", periph->unit_number,
311 0, DEVSTAT_NO_BLOCKSIZE | DEVSTAT_NO_ORDERED_TAGS,
313 DEVSTAT_TYPE_IF_SCSI |
315 DEVSTAT_PRIORITY_PASS);
317 /* Register the device */
318 dev_ops_add(&pass_ops, -1, periph->unit_number);
319 make_dev(&pass_ops, periph->unit_number, UID_ROOT,
320 GID_OPERATOR, 0600, "%s%d", periph->periph_name,
321 periph->unit_number);
324 * Add an async callback so that we get
325 * notified if this device goes away.
327 xpt_setup_ccb(&csa.ccb_h, periph->path, /* priority */ 5);
328 csa.ccb_h.func_code = XPT_SASYNC_CB;
329 csa.event_enable = AC_LOST_DEVICE;
330 csa.callback = passasync;
331 csa.callback_arg = periph;
332 xpt_action((union ccb *)&csa);
335 xpt_announce_periph(periph, NULL);
341 passopen(struct dev_open_args *ap)
343 cdev_t dev = ap->a_head.a_dev;
344 struct cam_periph *periph;
345 struct pass_softc *softc;
348 error = 0; /* default to no error */
350 /* unit = dkunit(dev); */
351 /* XXX KDM fix this */
352 unit = minor(dev) & 0xff;
354 periph = cam_extend_get(passperiphs, unit);
359 softc = (struct pass_softc *)periph->softc;
362 if (softc->flags & PASS_FLAG_INVALID) {
368 * Don't allow access when we're running at a high securelvel.
370 if (securelevel > 1) {
376 * Only allow read-write access.
378 if (((ap->a_oflags & FWRITE) == 0) || ((ap->a_oflags & FREAD) == 0)) {
384 * We don't allow nonblocking access.
386 if ((ap->a_oflags & O_NONBLOCK) != 0) {
387 xpt_print_path(periph->path);
388 kprintf("can't do nonblocking accesss\n");
393 if ((error = cam_periph_lock(periph, PCATCH)) != 0) {
400 if ((softc->flags & PASS_FLAG_OPEN) == 0) {
401 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
403 softc->flags |= PASS_FLAG_OPEN;
406 cam_periph_unlock(periph);
412 passclose(struct dev_close_args *ap)
414 cdev_t dev = ap->a_head.a_dev;
415 struct cam_periph *periph;
416 struct pass_softc *softc;
419 /* unit = dkunit(dev); */
420 /* XXX KDM fix this */
421 unit = minor(dev) & 0xff;
423 periph = cam_extend_get(passperiphs, unit);
427 softc = (struct pass_softc *)periph->softc;
429 if ((error = cam_periph_lock(periph, 0)) != 0)
432 softc->flags &= ~PASS_FLAG_OPEN;
434 cam_periph_unlock(periph);
435 cam_periph_release(periph);
441 * Actually translate the requested transfer into one the physical driver
442 * can understand. The transfer is described by a buf and will include
443 * only one physical transfer.
446 passstrategy(struct dev_strategy_args *ap)
448 cdev_t dev = ap->a_head.a_dev;
449 struct bio *bio = ap->a_bio;
450 struct buf *bp = bio->bio_buf;
451 struct cam_periph *periph;
452 struct pass_softc *softc;
456 * The read/write interface for the passthrough driver doesn't
457 * really work right now. So, we just pass back EINVAL to tell the
460 bp->b_error = EINVAL;
463 /* unit = dkunit(dev); */
464 /* XXX KDM fix this */
465 unit = minor(dev) & 0xff;
467 periph = cam_extend_get(passperiphs, unit);
468 if (periph == NULL) {
472 softc = (struct pass_softc *)periph->softc;
475 * Odd number of bytes or negative offset
478 if (bio->bio_offset < 0) {
479 bp->b_error = EINVAL;
484 * Mask interrupts so that the pack cannot be invalidated until
485 * after we are in the queue. Otherwise, we might not properly
486 * clean up one of the buffers.
489 bioq_insert_tail(&softc->bio_queue, bio);
493 * Schedule ourselves for performing the work.
495 xpt_schedule(periph, /* XXX priority */1);
499 bp->b_flags |= B_ERROR;
502 * Correctly set the buf to indicate a completed xfer
504 bp->b_resid = bp->b_bcount;
510 passstart(struct cam_periph *periph, union ccb *start_ccb)
512 struct pass_softc *softc;
514 softc = (struct pass_softc *)periph->softc;
516 switch (softc->state) {
517 case PASS_STATE_NORMAL:
523 bio = bioq_first(&softc->bio_queue);
524 if (periph->immediate_priority <= periph->pinfo.priority) {
525 start_ccb->ccb_h.ccb_type = PASS_CCB_WAITING;
526 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
528 periph->immediate_priority = CAM_PRIORITY_NONE;
530 wakeup(&periph->ccb_list);
531 } else if (bio == NULL) {
533 xpt_release_ccb(start_ccb);
536 bioq_remove(&softc->bio_queue, bio);
539 devstat_start_transaction(&softc->device_stats);
543 * Interpret the contents of the bp as a CCB
544 * and pass it to a routine shared by our ioctl
546 * For now, just biodone it with EIO so we don't
550 bp->b_flags |= B_ERROR;
551 bp->b_resid = bp->b_bcount;
553 bio = bioq_first(&softc->bio_queue);
556 xpt_action(start_ccb);
560 /* Have more work to do, so ensure we stay scheduled */
561 xpt_schedule(periph, /* XXX priority */1);
568 passdone(struct cam_periph *periph, union ccb *done_ccb)
570 struct pass_softc *softc;
571 struct ccb_scsiio *csio;
573 softc = (struct pass_softc *)periph->softc;
574 csio = &done_ccb->csio;
575 switch (csio->ccb_h.ccb_type) {
576 case PASS_CCB_BUFFER_IO:
581 u_int8_t scsi_status;
582 devstat_trans_flags ds_flags;
584 status = done_ccb->ccb_h.status;
585 scsi_status = done_ccb->csio.scsi_status;
586 bio = (struct bio *)done_ccb->ccb_h.ccb_bio;
589 /* XXX handle errors */
590 if (!(((status & CAM_STATUS_MASK) == CAM_REQ_CMP)
591 && (scsi_status == SCSI_STATUS_OK))) {
594 if ((error = passerror(done_ccb, 0, 0)) == ERESTART) {
596 * A retry was scheuled, so
603 * XXX unfreeze the queue after we complete
607 bp->b_flags |= B_ERROR;
610 if ((done_ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
611 ds_flags = DEVSTAT_READ;
612 else if ((done_ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
613 ds_flags = DEVSTAT_WRITE;
615 ds_flags = DEVSTAT_NO_DATA;
617 devstat_end_transaction_buf(&softc->device_stats, bp);
621 case PASS_CCB_WAITING:
623 /* Caller will release the CCB */
624 wakeup(&done_ccb->ccb_h.cbfcnp);
628 xpt_release_ccb(done_ccb);
632 passioctl(struct dev_ioctl_args *ap)
634 cdev_t dev = ap->a_head.a_dev;
635 caddr_t addr = ap->a_data;
636 struct cam_periph *periph;
637 struct pass_softc *softc;
642 /* unit = dkunit(dev); */
643 /* XXX KDM fix this */
644 unit = minor(dev) & 0xff;
646 periph = cam_extend_get(passperiphs, unit);
651 softc = (struct pass_softc *)periph->softc;
663 inccb = (union ccb *)addr;
666 * Some CCB types, like scan bus and scan lun can only go
667 * through the transport layer device.
669 if (inccb->ccb_h.func_code & XPT_FC_XPT_ONLY) {
670 xpt_print_path(periph->path);
671 kprintf("CCB function code %#x is restricted to the "
672 "XPT device\n", inccb->ccb_h.func_code);
678 * Non-immediate CCBs need a CCB from the per-device pool
679 * of CCBs, which is scheduled by the transport layer.
680 * Immediate CCBs and user-supplied CCBs should just be
683 if ((inccb->ccb_h.func_code & XPT_FC_QUEUED)
684 && ((inccb->ccb_h.func_code & XPT_FC_USER_CCB) == 0)) {
685 ccb = cam_periph_getccb(periph,
686 inccb->ccb_h.pinfo.priority);
689 ccb = xpt_alloc_ccb();
692 xpt_setup_ccb(&ccb->ccb_h, periph->path,
693 inccb->ccb_h.pinfo.priority);
698 xpt_print_path(periph->path);
699 kprintf("unable to allocate CCB\n");
704 error = passsendccb(periph, ccb, inccb);
709 xpt_release_ccb(ccb);
714 error = cam_periph_ioctl(periph, ap->a_cmd, addr, passerror);
722 * Generally, "ccb" should be the CCB supplied by the kernel. "inccb"
723 * should be the CCB that is copied in from the user.
726 passsendccb(struct cam_periph *periph, union ccb *ccb, union ccb *inccb)
728 struct pass_softc *softc;
729 struct cam_periph_map_info mapinfo;
730 int error, need_unmap;
732 softc = (struct pass_softc *)periph->softc;
737 * There are some fields in the CCB header that need to be
738 * preserved, the rest we get from the user.
740 xpt_merge_ccb(ccb, inccb);
743 * There's no way for the user to have a completion
744 * function, so we put our own completion function in here.
746 ccb->ccb_h.cbfcnp = passdone;
749 * We only attempt to map the user memory into kernel space
750 * if they haven't passed in a physical memory pointer,
751 * and if there is actually an I/O operation to perform.
752 * Right now cam_periph_mapmem() only supports SCSI and device
753 * match CCBs. For the SCSI CCBs, we only pass the CCB in if
754 * there's actually data to map. cam_periph_mapmem() will do the
755 * right thing, even if there isn't data to map, but since CCBs
756 * without data are a reasonably common occurance (e.g. test unit
757 * ready), it will save a few cycles if we check for it here.
759 if (((ccb->ccb_h.flags & CAM_DATA_PHYS) == 0)
760 && (((ccb->ccb_h.func_code == XPT_SCSI_IO)
761 && ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE))
762 || (ccb->ccb_h.func_code == XPT_DEV_MATCH))) {
764 bzero(&mapinfo, sizeof(mapinfo));
766 error = cam_periph_mapmem(ccb, &mapinfo);
769 * cam_periph_mapmem returned an error, we can't continue.
770 * Return the error to the user.
776 * We successfully mapped the memory in, so we need to
777 * unmap it when the transaction is done.
783 * If the user wants us to perform any error recovery, then honor
784 * that request. Otherwise, it's up to the user to perform any
787 error = cam_periph_runccb(ccb,
788 (ccb->ccb_h.flags & CAM_PASS_ERR_RECOVER) ?
791 /* sense_flags */SF_RETRY_UA | SF_RETRY_SELTO,
792 &softc->device_stats);
795 cam_periph_unmapmem(ccb, &mapinfo);
797 ccb->ccb_h.cbfcnp = NULL;
798 ccb->ccb_h.periph_priv = inccb->ccb_h.periph_priv;
799 bcopy(ccb, inccb, sizeof(union ccb));
805 passerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
807 struct cam_periph *periph;
808 struct pass_softc *softc;
810 periph = xpt_path_periph(ccb->ccb_h.path);
811 softc = (struct pass_softc *)periph->softc;
813 return(cam_periph_error(ccb, cam_flags, sense_flags,