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.13 2005/06/02 20:40:31 dillon 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_bp ppriv_ptr1
82 struct buf_queue_head buf_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 cdevsw pass_cdevsw = {
118 /* maj */ PASS_CDEV_MAJOR,
124 /* close */ passclose,
126 /* write */ physwrite,
127 /* ioctl */ passioctl,
130 /* strategy */ passstrategy,
135 static struct extend_array *passperiphs;
141 struct cam_path *path;
144 * Create our extend array for storing the devices we attach to.
146 passperiphs = cam_extend_new();
147 if (passperiphs == NULL) {
148 printf("passm: Failed to alloc extend array!\n");
153 * Install a global async callback. This callback will
154 * receive async callbacks like "new device found".
156 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
157 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
159 if (status == CAM_REQ_CMP) {
160 struct ccb_setasync csa;
162 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
163 csa.ccb_h.func_code = XPT_SASYNC_CB;
164 csa.event_enable = AC_FOUND_DEVICE;
165 csa.callback = passasync;
166 csa.callback_arg = NULL;
167 xpt_action((union ccb *)&csa);
168 status = csa.ccb_h.status;
172 if (status != CAM_REQ_CMP) {
173 printf("pass: Failed to attach master async callback "
174 "due to status 0x%x!\n", status);
180 passoninvalidate(struct cam_periph *periph)
182 struct pass_softc *softc;
184 struct ccb_setasync csa;
186 softc = (struct pass_softc *)periph->softc;
189 * De-register any async callbacks.
191 xpt_setup_ccb(&csa.ccb_h, periph->path,
193 csa.ccb_h.func_code = XPT_SASYNC_CB;
194 csa.event_enable = 0;
195 csa.callback = passasync;
196 csa.callback_arg = periph;
197 xpt_action((union ccb *)&csa);
199 softc->flags |= PASS_FLAG_INVALID;
202 * We need to be in a critical section here to keep the buffer
203 * queue from being modified while we traverse it.
208 * Return all queued I/O with ENXIO.
209 * XXX Handle any transactions queued to the card
210 * with XPT_ABORT_CCB.
212 while ((q_bp = bufq_first(&softc->buf_queue)) != NULL){
213 bufq_remove(&softc->buf_queue, q_bp);
214 q_bp->b_resid = q_bp->b_bcount;
215 q_bp->b_error = ENXIO;
216 q_bp->b_flags |= B_ERROR;
222 xpt_print_path(periph->path);
223 printf("lost device\n");
229 passcleanup(struct cam_periph *periph)
231 struct pass_softc *softc;
233 softc = (struct pass_softc *)periph->softc;
235 devstat_remove_entry(&softc->device_stats);
237 cam_extend_release(passperiphs, periph->unit_number);
240 xpt_print_path(periph->path);
241 printf("removing device entry\n");
243 cdevsw_remove(&pass_cdevsw, -1, periph->unit_number);
244 free(softc, M_DEVBUF);
248 passasync(void *callback_arg, u_int32_t code,
249 struct cam_path *path, void *arg)
251 struct cam_periph *periph;
253 periph = (struct cam_periph *)callback_arg;
256 case AC_FOUND_DEVICE:
258 struct ccb_getdev *cgd;
261 cgd = (struct ccb_getdev *)arg;
264 * Allocate a peripheral instance for
265 * this device and start the probe
268 status = cam_periph_alloc(passregister, passoninvalidate,
269 passcleanup, passstart, "pass",
270 CAM_PERIPH_BIO, cgd->ccb_h.path,
271 passasync, AC_FOUND_DEVICE, cgd);
273 if (status != CAM_REQ_CMP
274 && status != CAM_REQ_INPROG)
275 printf("passasync: Unable to attach new device "
276 "due to status 0x%x\n", status);
281 cam_periph_async(periph, code, path, arg);
287 passregister(struct cam_periph *periph, void *arg)
289 struct pass_softc *softc;
290 struct ccb_setasync csa;
291 struct ccb_getdev *cgd;
293 cgd = (struct ccb_getdev *)arg;
294 if (periph == NULL) {
295 printf("passregister: periph was NULL!!\n");
296 return(CAM_REQ_CMP_ERR);
300 printf("passregister: no getdev CCB, can't register device\n");
301 return(CAM_REQ_CMP_ERR);
304 softc = malloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO);
305 softc->state = PASS_STATE_NORMAL;
306 softc->pd_type = SID_TYPE(&cgd->inq_data);
307 bufq_init(&softc->buf_queue);
309 periph->softc = softc;
311 cam_extend_set(passperiphs, periph->unit_number, periph);
313 * We pass in 0 for a blocksize, since we don't
314 * know what the blocksize of this device is, if
315 * it even has a blocksize.
317 devstat_add_entry(&softc->device_stats, "pass", periph->unit_number,
318 0, DEVSTAT_NO_BLOCKSIZE | DEVSTAT_NO_ORDERED_TAGS,
320 DEVSTAT_TYPE_IF_SCSI |
322 DEVSTAT_PRIORITY_PASS);
324 /* Register the device */
325 cdevsw_add(&pass_cdevsw, -1, periph->unit_number);
326 make_dev(&pass_cdevsw, periph->unit_number, UID_ROOT,
327 GID_OPERATOR, 0600, "%s%d", periph->periph_name,
328 periph->unit_number);
331 * Add an async callback so that we get
332 * notified if this device goes away.
334 xpt_setup_ccb(&csa.ccb_h, periph->path, /* priority */ 5);
335 csa.ccb_h.func_code = XPT_SASYNC_CB;
336 csa.event_enable = AC_LOST_DEVICE;
337 csa.callback = passasync;
338 csa.callback_arg = periph;
339 xpt_action((union ccb *)&csa);
342 xpt_announce_periph(periph, NULL);
348 passopen(dev_t dev, int flags, int fmt, struct thread *td)
350 struct cam_periph *periph;
351 struct pass_softc *softc;
354 error = 0; /* default to no error */
356 /* unit = dkunit(dev); */
357 /* XXX KDM fix this */
358 unit = minor(dev) & 0xff;
360 periph = cam_extend_get(passperiphs, unit);
365 softc = (struct pass_softc *)periph->softc;
368 if (softc->flags & PASS_FLAG_INVALID) {
374 * Don't allow access when we're running at a high securelvel.
376 if (securelevel > 1) {
382 * Only allow read-write access.
384 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0)) {
390 * We don't allow nonblocking access.
392 if ((flags & O_NONBLOCK) != 0) {
393 xpt_print_path(periph->path);
394 printf("can't do nonblocking accesss\n");
399 if ((error = cam_periph_lock(periph, PCATCH)) != 0) {
406 if ((softc->flags & PASS_FLAG_OPEN) == 0) {
407 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
409 softc->flags |= PASS_FLAG_OPEN;
412 cam_periph_unlock(periph);
418 passclose(dev_t dev, int flag, int fmt, struct thread *td)
420 struct cam_periph *periph;
421 struct pass_softc *softc;
424 /* unit = dkunit(dev); */
425 /* XXX KDM fix this */
426 unit = minor(dev) & 0xff;
428 periph = cam_extend_get(passperiphs, unit);
432 softc = (struct pass_softc *)periph->softc;
434 if ((error = cam_periph_lock(periph, 0)) != 0)
437 softc->flags &= ~PASS_FLAG_OPEN;
439 cam_periph_unlock(periph);
440 cam_periph_release(periph);
446 * Actually translate the requested transfer into one the physical driver
447 * can understand. The transfer is described by a buf and will include
448 * only one physical transfer.
451 passstrategy(struct buf *bp)
453 struct cam_periph *periph;
454 struct pass_softc *softc;
458 * The read/write interface for the passthrough driver doesn't
459 * really work right now. So, we just pass back EINVAL to tell the
462 bp->b_error = EINVAL;
465 /* unit = dkunit(bp->b_dev); */
466 /* XXX KDM fix this */
467 unit = minor(bp->b_dev) & 0xff;
469 periph = cam_extend_get(passperiphs, unit);
470 if (periph == NULL) {
474 softc = (struct pass_softc *)periph->softc;
477 * Odd number of bytes or negative offset
480 if (bp->b_blkno < 0) {
481 bp->b_error = EINVAL;
486 * Mask interrupts so that the pack cannot be invalidated until
487 * after we are in the queue. Otherwise, we might not properly
488 * clean up one of the buffers.
491 bufq_insert_tail(&softc->buf_queue, bp);
495 * Schedule ourselves for performing the work.
497 xpt_schedule(periph, /* XXX priority */1);
501 bp->b_flags |= B_ERROR;
504 * Correctly set the buf to indicate a completed xfer
506 bp->b_resid = bp->b_bcount;
512 passstart(struct cam_periph *periph, union ccb *start_ccb)
514 struct pass_softc *softc;
516 softc = (struct pass_softc *)periph->softc;
518 switch (softc->state) {
519 case PASS_STATE_NORMAL:
524 bp = bufq_first(&softc->buf_queue);
525 if (periph->immediate_priority <= periph->pinfo.priority) {
526 start_ccb->ccb_h.ccb_type = PASS_CCB_WAITING;
527 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
529 periph->immediate_priority = CAM_PRIORITY_NONE;
531 wakeup(&periph->ccb_list);
532 } else if (bp == NULL) {
534 xpt_release_ccb(start_ccb);
537 bufq_remove(&softc->buf_queue, bp);
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 bp = bufq_first(&softc->buf_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:
580 u_int8_t scsi_status;
581 devstat_trans_flags ds_flags;
583 status = done_ccb->ccb_h.status;
584 scsi_status = done_ccb->csio.scsi_status;
585 bp = (struct buf *)done_ccb->ccb_h.ccb_bp;
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(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
631 struct cam_periph *periph;
632 struct pass_softc *softc;
637 /* unit = dkunit(dev); */
638 /* XXX KDM fix this */
639 unit = minor(dev) & 0xff;
641 periph = cam_extend_get(passperiphs, unit);
646 softc = (struct pass_softc *)periph->softc;
658 inccb = (union ccb *)addr;
661 * Some CCB types, like scan bus and scan lun can only go
662 * through the transport layer device.
664 if (inccb->ccb_h.func_code & XPT_FC_XPT_ONLY) {
665 xpt_print_path(periph->path);
666 printf("CCB function code %#x is restricted to the "
667 "XPT device\n", inccb->ccb_h.func_code);
673 * Non-immediate CCBs need a CCB from the per-device pool
674 * of CCBs, which is scheduled by the transport layer.
675 * Immediate CCBs and user-supplied CCBs should just be
678 if ((inccb->ccb_h.func_code & XPT_FC_QUEUED)
679 && ((inccb->ccb_h.func_code & XPT_FC_USER_CCB) == 0)) {
680 ccb = cam_periph_getccb(periph,
681 inccb->ccb_h.pinfo.priority);
684 ccb = xpt_alloc_ccb();
687 xpt_setup_ccb(&ccb->ccb_h, periph->path,
688 inccb->ccb_h.pinfo.priority);
693 xpt_print_path(periph->path);
694 printf("unable to allocate CCB\n");
699 error = passsendccb(periph, ccb, inccb);
704 xpt_release_ccb(ccb);
709 error = cam_periph_ioctl(periph, cmd, addr, passerror);
717 * Generally, "ccb" should be the CCB supplied by the kernel. "inccb"
718 * should be the CCB that is copied in from the user.
721 passsendccb(struct cam_periph *periph, union ccb *ccb, union ccb *inccb)
723 struct pass_softc *softc;
724 struct cam_periph_map_info mapinfo;
725 int error, need_unmap;
727 softc = (struct pass_softc *)periph->softc;
732 * There are some fields in the CCB header that need to be
733 * preserved, the rest we get from the user.
735 xpt_merge_ccb(ccb, inccb);
738 * There's no way for the user to have a completion
739 * function, so we put our own completion function in here.
741 ccb->ccb_h.cbfcnp = passdone;
744 * We only attempt to map the user memory into kernel space
745 * if they haven't passed in a physical memory pointer,
746 * and if there is actually an I/O operation to perform.
747 * Right now cam_periph_mapmem() only supports SCSI and device
748 * match CCBs. For the SCSI CCBs, we only pass the CCB in if
749 * there's actually data to map. cam_periph_mapmem() will do the
750 * right thing, even if there isn't data to map, but since CCBs
751 * without data are a reasonably common occurance (e.g. test unit
752 * ready), it will save a few cycles if we check for it here.
754 if (((ccb->ccb_h.flags & CAM_DATA_PHYS) == 0)
755 && (((ccb->ccb_h.func_code == XPT_SCSI_IO)
756 && ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE))
757 || (ccb->ccb_h.func_code == XPT_DEV_MATCH))) {
759 bzero(&mapinfo, sizeof(mapinfo));
761 error = cam_periph_mapmem(ccb, &mapinfo);
764 * cam_periph_mapmem returned an error, we can't continue.
765 * Return the error to the user.
771 * We successfully mapped the memory in, so we need to
772 * unmap it when the transaction is done.
778 * If the user wants us to perform any error recovery, then honor
779 * that request. Otherwise, it's up to the user to perform any
782 error = cam_periph_runccb(ccb,
783 (ccb->ccb_h.flags & CAM_PASS_ERR_RECOVER) ?
786 /* sense_flags */SF_RETRY_UA | SF_RETRY_SELTO,
787 &softc->device_stats);
790 cam_periph_unmapmem(ccb, &mapinfo);
792 ccb->ccb_h.cbfcnp = NULL;
793 ccb->ccb_h.periph_priv = inccb->ccb_h.periph_priv;
794 bcopy(ccb, inccb, sizeof(union ccb));
800 passerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
802 struct cam_periph *periph;
803 struct pass_softc *softc;
805 periph = xpt_path_periph(ccb->ccb_h.path);
806 softc = (struct pass_softc *)periph->softc;
808 return(cam_periph_error(ccb, cam_flags, sense_flags,