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.21 2007/11/12 23:48:52 pavalos 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>
40 #include <sys/errno.h>
41 #include <sys/devicestat.h>
43 #include <sys/thread2.h>
46 #include "../cam_ccb.h"
47 #include "../cam_extend.h"
48 #include "../cam_periph.h"
49 #include "../cam_xpt_periph.h"
50 #include "../cam_debug.h"
53 #include "scsi_message.h"
55 #include "scsi_pass.h"
58 PASS_FLAG_OPEN = 0x01,
59 PASS_FLAG_LOCKED = 0x02,
60 PASS_FLAG_INVALID = 0x04
72 #define ccb_type ppriv_field0
73 #define ccb_bio ppriv_ptr1
79 struct bio_queue_head bio_queue;
81 struct devstat device_stats;
84 #define PASS_CDEV_MAJOR 31
86 static d_open_t passopen;
87 static d_close_t passclose;
88 static d_ioctl_t passioctl;
89 static d_strategy_t passstrategy;
91 static periph_init_t passinit;
92 static periph_ctor_t passregister;
93 static periph_oninv_t passoninvalidate;
94 static periph_dtor_t passcleanup;
95 static periph_start_t passstart;
96 static void passasync(void *callback_arg, u_int32_t code,
97 struct cam_path *path, void *arg);
98 static void passdone(struct cam_periph *periph,
100 static int passerror(union ccb *ccb, u_int32_t cam_flags,
101 u_int32_t sense_flags);
102 static int passsendccb(struct cam_periph *periph, union ccb *ccb,
105 static struct periph_driver passdriver =
108 TAILQ_HEAD_INITIALIZER(passdriver.units), /* generation */ 0
111 DATA_SET(periphdriver_set, passdriver);
113 static struct dev_ops pass_ops = {
114 { "pass", PASS_CDEV_MAJOR, 0 },
116 .d_close = passclose,
118 .d_write = physwrite,
119 .d_ioctl = passioctl,
120 .d_strategy = passstrategy,
123 static struct extend_array *passperiphs;
129 struct cam_path *path;
132 * Create our extend array for storing the devices we attach to.
134 passperiphs = cam_extend_new();
135 if (passperiphs == NULL) {
136 kprintf("passm: Failed to alloc extend array!\n");
141 * Install a global async callback. This callback will
142 * receive async callbacks like "new device found".
144 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
145 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
147 if (status == CAM_REQ_CMP) {
148 struct ccb_setasync csa;
150 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
151 csa.ccb_h.func_code = XPT_SASYNC_CB;
152 csa.event_enable = AC_FOUND_DEVICE;
153 csa.callback = passasync;
154 csa.callback_arg = NULL;
155 xpt_action((union ccb *)&csa);
156 status = csa.ccb_h.status;
160 if (status != CAM_REQ_CMP) {
161 kprintf("pass: Failed to attach master async callback "
162 "due to status 0x%x!\n", status);
168 passoninvalidate(struct cam_periph *periph)
170 struct pass_softc *softc;
173 struct ccb_setasync csa;
175 softc = (struct pass_softc *)periph->softc;
178 * De-register any async callbacks.
180 xpt_setup_ccb(&csa.ccb_h, periph->path,
182 csa.ccb_h.func_code = XPT_SASYNC_CB;
183 csa.event_enable = 0;
184 csa.callback = passasync;
185 csa.callback_arg = periph;
186 xpt_action((union ccb *)&csa);
188 softc->flags |= PASS_FLAG_INVALID;
191 * We need to be in a critical section here to keep the buffer
192 * queue from being modified while we traverse it.
197 * Return all queued I/O with ENXIO.
198 * XXX Handle any transactions queued to the card
199 * with XPT_ABORT_CCB.
201 while ((q_bio = bioq_first(&softc->bio_queue)) != NULL){
202 bioq_remove(&softc->bio_queue, q_bio);
203 q_bp = q_bio->bio_buf;
204 q_bp->b_resid = q_bp->b_bcount;
205 q_bp->b_error = ENXIO;
206 q_bp->b_flags |= B_ERROR;
212 xpt_print_path(periph->path);
213 kprintf("lost device\n");
219 passcleanup(struct cam_periph *periph)
221 struct pass_softc *softc;
223 softc = (struct pass_softc *)periph->softc;
225 devstat_remove_entry(&softc->device_stats);
227 cam_extend_release(passperiphs, periph->unit_number);
230 xpt_print_path(periph->path);
231 kprintf("removing device entry\n");
233 dev_ops_remove(&pass_ops, -1, periph->unit_number);
234 kfree(softc, M_DEVBUF);
238 passasync(void *callback_arg, u_int32_t code,
239 struct cam_path *path, void *arg)
241 struct cam_periph *periph;
243 periph = (struct cam_periph *)callback_arg;
246 case AC_FOUND_DEVICE:
248 struct ccb_getdev *cgd;
251 cgd = (struct ccb_getdev *)arg;
254 * Allocate a peripheral instance for
255 * this device and start the probe
258 status = cam_periph_alloc(passregister, passoninvalidate,
259 passcleanup, passstart, "pass",
260 CAM_PERIPH_BIO, cgd->ccb_h.path,
261 passasync, AC_FOUND_DEVICE, cgd);
263 if (status != CAM_REQ_CMP
264 && status != CAM_REQ_INPROG)
265 kprintf("passasync: Unable to attach new device "
266 "due to status 0x%x\n", status);
271 cam_periph_async(periph, code, path, arg);
277 passregister(struct cam_periph *periph, void *arg)
279 struct pass_softc *softc;
280 struct ccb_setasync csa;
281 struct ccb_getdev *cgd;
283 cgd = (struct ccb_getdev *)arg;
284 if (periph == NULL) {
285 kprintf("passregister: periph was NULL!!\n");
286 return(CAM_REQ_CMP_ERR);
290 kprintf("passregister: no getdev CCB, can't register device\n");
291 return(CAM_REQ_CMP_ERR);
294 softc = kmalloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO);
295 softc->state = PASS_STATE_NORMAL;
296 softc->pd_type = SID_TYPE(&cgd->inq_data);
297 bioq_init(&softc->bio_queue);
299 periph->softc = softc;
301 cam_extend_set(passperiphs, periph->unit_number, periph);
303 * We pass in 0 for a blocksize, since we don't
304 * know what the blocksize of this device is, if
305 * it even has a blocksize.
307 devstat_add_entry(&softc->device_stats, "pass", periph->unit_number,
308 0, DEVSTAT_NO_BLOCKSIZE | DEVSTAT_NO_ORDERED_TAGS,
310 DEVSTAT_TYPE_IF_SCSI |
312 DEVSTAT_PRIORITY_PASS);
314 /* Register the device */
315 dev_ops_add(&pass_ops, -1, periph->unit_number);
316 make_dev(&pass_ops, periph->unit_number, UID_ROOT,
317 GID_OPERATOR, 0600, "%s%d", periph->periph_name,
318 periph->unit_number);
321 * Add an async callback so that we get
322 * notified if this device goes away.
324 xpt_setup_ccb(&csa.ccb_h, periph->path, /* priority */ 5);
325 csa.ccb_h.func_code = XPT_SASYNC_CB;
326 csa.event_enable = AC_LOST_DEVICE;
327 csa.callback = passasync;
328 csa.callback_arg = periph;
329 xpt_action((union ccb *)&csa);
332 xpt_announce_periph(periph, NULL);
338 passopen(struct dev_open_args *ap)
340 cdev_t dev = ap->a_head.a_dev;
341 struct cam_periph *periph;
342 struct pass_softc *softc;
345 error = 0; /* default to no error */
347 /* unit = dkunit(dev); */
348 /* XXX KDM fix this */
349 unit = minor(dev) & 0xff;
351 periph = cam_extend_get(passperiphs, unit);
356 softc = (struct pass_softc *)periph->softc;
359 if (softc->flags & PASS_FLAG_INVALID) {
365 * Don't allow access when we're running at a high securelvel.
367 if (securelevel > 1) {
373 * Only allow read-write access.
375 if (((ap->a_oflags & FWRITE) == 0) || ((ap->a_oflags & FREAD) == 0)) {
381 * We don't allow nonblocking access.
383 if ((ap->a_oflags & O_NONBLOCK) != 0) {
384 xpt_print_path(periph->path);
385 kprintf("can't do nonblocking accesss\n");
390 if ((error = cam_periph_lock(periph, PCATCH)) != 0) {
397 if ((softc->flags & PASS_FLAG_OPEN) == 0) {
398 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
400 softc->flags |= PASS_FLAG_OPEN;
403 cam_periph_unlock(periph);
409 passclose(struct dev_close_args *ap)
411 cdev_t dev = ap->a_head.a_dev;
412 struct cam_periph *periph;
413 struct pass_softc *softc;
416 /* unit = dkunit(dev); */
417 /* XXX KDM fix this */
418 unit = minor(dev) & 0xff;
420 periph = cam_extend_get(passperiphs, unit);
424 softc = (struct pass_softc *)periph->softc;
426 if ((error = cam_periph_lock(periph, 0)) != 0)
429 softc->flags &= ~PASS_FLAG_OPEN;
431 cam_periph_unlock(periph);
432 cam_periph_release(periph);
438 * Actually translate the requested transfer into one the physical driver
439 * can understand. The transfer is described by a buf and will include
440 * only one physical transfer.
443 passstrategy(struct dev_strategy_args *ap)
445 cdev_t dev = ap->a_head.a_dev;
446 struct bio *bio = ap->a_bio;
447 struct buf *bp = bio->bio_buf;
448 struct cam_periph *periph;
449 struct pass_softc *softc;
453 * The read/write interface for the passthrough driver doesn't
454 * really work right now. So, we just pass back EINVAL to tell the
457 bp->b_error = EINVAL;
460 /* unit = dkunit(dev); */
461 /* XXX KDM fix this */
462 unit = minor(dev) & 0xff;
464 periph = cam_extend_get(passperiphs, unit);
465 if (periph == NULL) {
469 softc = (struct pass_softc *)periph->softc;
472 * Odd number of bytes or negative offset
475 if (bio->bio_offset < 0) {
476 bp->b_error = EINVAL;
481 * Mask interrupts so that the pack cannot be invalidated until
482 * after we are in the queue. Otherwise, we might not properly
483 * clean up one of the buffers.
486 bioq_insert_tail(&softc->bio_queue, bio);
490 * Schedule ourselves for performing the work.
492 xpt_schedule(periph, /* XXX priority */1);
496 bp->b_flags |= B_ERROR;
499 * Correctly set the buf to indicate a completed xfer
501 bp->b_resid = bp->b_bcount;
507 passstart(struct cam_periph *periph, union ccb *start_ccb)
509 struct pass_softc *softc;
511 softc = (struct pass_softc *)periph->softc;
513 switch (softc->state) {
514 case PASS_STATE_NORMAL:
520 bio = bioq_first(&softc->bio_queue);
521 if (periph->immediate_priority <= periph->pinfo.priority) {
522 start_ccb->ccb_h.ccb_type = PASS_CCB_WAITING;
523 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
525 periph->immediate_priority = CAM_PRIORITY_NONE;
527 wakeup(&periph->ccb_list);
528 } else if (bio == NULL) {
530 xpt_release_ccb(start_ccb);
533 bioq_remove(&softc->bio_queue, bio);
536 devstat_start_transaction(&softc->device_stats);
540 * Interpret the contents of the bp as a CCB
541 * and pass it to a routine shared by our ioctl
543 * For now, just biodone it with EIO so we don't
547 bp->b_flags |= B_ERROR;
548 bp->b_resid = bp->b_bcount;
550 bio = bioq_first(&softc->bio_queue);
553 xpt_action(start_ccb);
557 /* Have more work to do, so ensure we stay scheduled */
558 xpt_schedule(periph, /* XXX priority */1);
565 passdone(struct cam_periph *periph, union ccb *done_ccb)
567 struct pass_softc *softc;
568 struct ccb_scsiio *csio;
570 softc = (struct pass_softc *)periph->softc;
571 csio = &done_ccb->csio;
572 switch (csio->ccb_h.ccb_type) {
573 case PASS_CCB_BUFFER_IO:
578 u_int8_t scsi_status;
579 devstat_trans_flags ds_flags;
581 status = done_ccb->ccb_h.status;
582 scsi_status = done_ccb->csio.scsi_status;
583 bio = (struct bio *)done_ccb->ccb_h.ccb_bio;
586 /* XXX handle errors */
587 if (!(((status & CAM_STATUS_MASK) == CAM_REQ_CMP)
588 && (scsi_status == SCSI_STATUS_OK))) {
591 if ((error = passerror(done_ccb, 0, 0)) == ERESTART) {
593 * A retry was scheuled, so
600 * XXX unfreeze the queue after we complete
604 bp->b_flags |= B_ERROR;
607 if ((done_ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
608 ds_flags = DEVSTAT_READ;
609 else if ((done_ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
610 ds_flags = DEVSTAT_WRITE;
612 ds_flags = DEVSTAT_NO_DATA;
614 devstat_end_transaction_buf(&softc->device_stats, bp);
618 case PASS_CCB_WAITING:
620 /* Caller will release the CCB */
621 wakeup(&done_ccb->ccb_h.cbfcnp);
625 xpt_release_ccb(done_ccb);
629 passioctl(struct dev_ioctl_args *ap)
631 cdev_t dev = ap->a_head.a_dev;
632 caddr_t addr = ap->a_data;
633 struct cam_periph *periph;
634 struct pass_softc *softc;
639 /* unit = dkunit(dev); */
640 /* XXX KDM fix this */
641 unit = minor(dev) & 0xff;
643 periph = cam_extend_get(passperiphs, unit);
648 softc = (struct pass_softc *)periph->softc;
660 inccb = (union ccb *)addr;
663 * Some CCB types, like scan bus and scan lun can only go
664 * through the transport layer device.
666 if (inccb->ccb_h.func_code & XPT_FC_XPT_ONLY) {
667 xpt_print_path(periph->path);
668 kprintf("CCB function code %#x is restricted to the "
669 "XPT device\n", inccb->ccb_h.func_code);
675 * Non-immediate CCBs need a CCB from the per-device pool
676 * of CCBs, which is scheduled by the transport layer.
677 * Immediate CCBs and user-supplied CCBs should just be
680 if ((inccb->ccb_h.func_code & XPT_FC_QUEUED)
681 && ((inccb->ccb_h.func_code & XPT_FC_USER_CCB) == 0)) {
682 ccb = cam_periph_getccb(periph,
683 inccb->ccb_h.pinfo.priority);
686 ccb = xpt_alloc_ccb();
689 xpt_setup_ccb(&ccb->ccb_h, periph->path,
690 inccb->ccb_h.pinfo.priority);
695 xpt_print_path(periph->path);
696 kprintf("unable to allocate CCB\n");
701 error = passsendccb(periph, ccb, inccb);
706 xpt_release_ccb(ccb);
711 error = cam_periph_ioctl(periph, ap->a_cmd, addr, passerror);
719 * Generally, "ccb" should be the CCB supplied by the kernel. "inccb"
720 * should be the CCB that is copied in from the user.
723 passsendccb(struct cam_periph *periph, union ccb *ccb, union ccb *inccb)
725 struct pass_softc *softc;
726 struct cam_periph_map_info mapinfo;
727 int error, need_unmap;
729 softc = (struct pass_softc *)periph->softc;
734 * There are some fields in the CCB header that need to be
735 * preserved, the rest we get from the user.
737 xpt_merge_ccb(ccb, inccb);
740 * There's no way for the user to have a completion
741 * function, so we put our own completion function in here.
743 ccb->ccb_h.cbfcnp = passdone;
746 * We only attempt to map the user memory into kernel space
747 * if they haven't passed in a physical memory pointer,
748 * and if there is actually an I/O operation to perform.
749 * Right now cam_periph_mapmem() only supports SCSI and device
750 * match CCBs. For the SCSI CCBs, we only pass the CCB in if
751 * there's actually data to map. cam_periph_mapmem() will do the
752 * right thing, even if there isn't data to map, but since CCBs
753 * without data are a reasonably common occurance (e.g. test unit
754 * ready), it will save a few cycles if we check for it here.
756 if (((ccb->ccb_h.flags & CAM_DATA_PHYS) == 0)
757 && (((ccb->ccb_h.func_code == XPT_SCSI_IO)
758 && ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE))
759 || (ccb->ccb_h.func_code == XPT_DEV_MATCH))) {
761 bzero(&mapinfo, sizeof(mapinfo));
763 error = cam_periph_mapmem(ccb, &mapinfo);
766 * cam_periph_mapmem returned an error, we can't continue.
767 * Return the error to the user.
773 * We successfully mapped the memory in, so we need to
774 * unmap it when the transaction is done.
780 * If the user wants us to perform any error recovery, then honor
781 * that request. Otherwise, it's up to the user to perform any
784 error = cam_periph_runccb(ccb,
785 (ccb->ccb_h.flags & CAM_PASS_ERR_RECOVER) ?
788 /* sense_flags */SF_RETRY_UA | SF_RETRY_SELTO,
789 &softc->device_stats);
792 cam_periph_unmapmem(ccb, &mapinfo);
794 ccb->ccb_h.cbfcnp = NULL;
795 ccb->ccb_h.periph_priv = inccb->ccb_h.periph_priv;
796 bcopy(ccb, inccb, sizeof(union ccb));
802 passerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
804 struct cam_periph *periph;
805 struct pass_softc *softc;
807 periph = xpt_path_periph(ccb->ccb_h.path);
808 softc = (struct pass_softc *)periph->softc;
810 return(cam_periph_error(ccb, cam_flags, sense_flags,