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.12 2004/05/19 22:52:38 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>
48 #include "../cam_ccb.h"
49 #include "../cam_extend.h"
50 #include "../cam_periph.h"
51 #include "../cam_xpt_periph.h"
52 #include "../cam_debug.h"
55 #include "scsi_message.h"
57 #include "scsi_pass.h"
60 PASS_FLAG_OPEN = 0x01,
61 PASS_FLAG_LOCKED = 0x02,
62 PASS_FLAG_INVALID = 0x04
74 #define ccb_type ppriv_field0
75 #define ccb_bp ppriv_ptr1
81 struct buf_queue_head buf_queue;
83 struct devstat device_stats;
86 #define PASS_CDEV_MAJOR 31
88 static d_open_t passopen;
89 static d_close_t passclose;
90 static d_ioctl_t passioctl;
91 static d_strategy_t passstrategy;
93 static periph_init_t passinit;
94 static periph_ctor_t passregister;
95 static periph_oninv_t passoninvalidate;
96 static periph_dtor_t passcleanup;
97 static periph_start_t passstart;
98 static void passasync(void *callback_arg, u_int32_t code,
99 struct cam_path *path, void *arg);
100 static void passdone(struct cam_periph *periph,
101 union ccb *done_ccb);
102 static int passerror(union ccb *ccb, u_int32_t cam_flags,
103 u_int32_t sense_flags);
104 static int passsendccb(struct cam_periph *periph, union ccb *ccb,
107 static struct periph_driver passdriver =
110 TAILQ_HEAD_INITIALIZER(passdriver.units), /* generation */ 0
113 DATA_SET(periphdriver_set, passdriver);
115 static struct cdevsw pass_cdevsw = {
117 /* maj */ PASS_CDEV_MAJOR,
123 /* close */ passclose,
125 /* write */ physwrite,
126 /* ioctl */ passioctl,
129 /* strategy */ passstrategy,
134 static struct extend_array *passperiphs;
140 struct cam_path *path;
143 * Create our extend array for storing the devices we attach to.
145 passperiphs = cam_extend_new();
146 if (passperiphs == NULL) {
147 printf("passm: Failed to alloc extend array!\n");
152 * Install a global async callback. This callback will
153 * receive async callbacks like "new device found".
155 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
156 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
158 if (status == CAM_REQ_CMP) {
159 struct ccb_setasync csa;
161 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
162 csa.ccb_h.func_code = XPT_SASYNC_CB;
163 csa.event_enable = AC_FOUND_DEVICE;
164 csa.callback = passasync;
165 csa.callback_arg = NULL;
166 xpt_action((union ccb *)&csa);
167 status = csa.ccb_h.status;
171 if (status != CAM_REQ_CMP) {
172 printf("pass: Failed to attach master async callback "
173 "due to status 0x%x!\n", status);
179 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 * Although the oninvalidate() routines are always called at
203 * splsoftcam, we need to be at splbio() here to keep the buffer
204 * queue from being modified while we traverse it.
209 * Return all queued I/O with ENXIO.
210 * XXX Handle any transactions queued to the card
211 * with XPT_ABORT_CCB.
213 while ((q_bp = bufq_first(&softc->buf_queue)) != NULL){
214 bufq_remove(&softc->buf_queue, q_bp);
215 q_bp->b_resid = q_bp->b_bcount;
216 q_bp->b_error = ENXIO;
217 q_bp->b_flags |= B_ERROR;
223 xpt_print_path(periph->path);
224 printf("lost device\n");
230 passcleanup(struct cam_periph *periph)
232 struct pass_softc *softc;
234 softc = (struct pass_softc *)periph->softc;
236 devstat_remove_entry(&softc->device_stats);
238 cam_extend_release(passperiphs, periph->unit_number);
241 xpt_print_path(periph->path);
242 printf("removing device entry\n");
244 cdevsw_remove(&pass_cdevsw, -1, periph->unit_number);
245 free(softc, M_DEVBUF);
249 passasync(void *callback_arg, u_int32_t code,
250 struct cam_path *path, void *arg)
252 struct cam_periph *periph;
254 periph = (struct cam_periph *)callback_arg;
257 case AC_FOUND_DEVICE:
259 struct ccb_getdev *cgd;
262 cgd = (struct ccb_getdev *)arg;
265 * Allocate a peripheral instance for
266 * this device and start the probe
269 status = cam_periph_alloc(passregister, passoninvalidate,
270 passcleanup, passstart, "pass",
271 CAM_PERIPH_BIO, cgd->ccb_h.path,
272 passasync, AC_FOUND_DEVICE, cgd);
274 if (status != CAM_REQ_CMP
275 && status != CAM_REQ_INPROG)
276 printf("passasync: Unable to attach new device "
277 "due to status 0x%x\n", status);
282 cam_periph_async(periph, code, path, arg);
288 passregister(struct cam_periph *periph, void *arg)
290 struct pass_softc *softc;
291 struct ccb_setasync csa;
292 struct ccb_getdev *cgd;
294 cgd = (struct ccb_getdev *)arg;
295 if (periph == NULL) {
296 printf("passregister: periph was NULL!!\n");
297 return(CAM_REQ_CMP_ERR);
301 printf("passregister: no getdev CCB, can't register device\n");
302 return(CAM_REQ_CMP_ERR);
305 softc = malloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO);
306 softc->state = PASS_STATE_NORMAL;
307 softc->pd_type = SID_TYPE(&cgd->inq_data);
308 bufq_init(&softc->buf_queue);
310 periph->softc = softc;
312 cam_extend_set(passperiphs, periph->unit_number, periph);
314 * We pass in 0 for a blocksize, since we don't
315 * know what the blocksize of this device is, if
316 * it even has a blocksize.
318 devstat_add_entry(&softc->device_stats, "pass", periph->unit_number,
319 0, DEVSTAT_NO_BLOCKSIZE | DEVSTAT_NO_ORDERED_TAGS,
321 DEVSTAT_TYPE_IF_SCSI |
323 DEVSTAT_PRIORITY_PASS);
325 /* Register the device */
326 cdevsw_add(&pass_cdevsw, -1, periph->unit_number);
327 make_dev(&pass_cdevsw, periph->unit_number, UID_ROOT,
328 GID_OPERATOR, 0600, "%s%d", periph->periph_name,
329 periph->unit_number);
332 * Add an async callback so that we get
333 * notified if this device goes away.
335 xpt_setup_ccb(&csa.ccb_h, periph->path, /* priority */ 5);
336 csa.ccb_h.func_code = XPT_SASYNC_CB;
337 csa.event_enable = AC_LOST_DEVICE;
338 csa.callback = passasync;
339 csa.callback_arg = periph;
340 xpt_action((union ccb *)&csa);
343 xpt_announce_periph(periph, NULL);
349 passopen(dev_t dev, int flags, int fmt, struct thread *td)
351 struct cam_periph *periph;
352 struct pass_softc *softc;
356 error = 0; /* default to no error */
358 /* unit = dkunit(dev); */
359 /* XXX KDM fix this */
360 unit = minor(dev) & 0xff;
362 periph = cam_extend_get(passperiphs, unit);
367 softc = (struct pass_softc *)periph->softc;
370 if (softc->flags & PASS_FLAG_INVALID) {
376 * Don't allow access when we're running at a high securelvel.
378 if (securelevel > 1) {
384 * Only allow read-write access.
386 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0)) {
392 * We don't allow nonblocking access.
394 if ((flags & O_NONBLOCK) != 0) {
395 xpt_print_path(periph->path);
396 printf("can't do nonblocking accesss\n");
401 if ((error = cam_periph_lock(periph, PCATCH)) != 0) {
408 if ((softc->flags & PASS_FLAG_OPEN) == 0) {
409 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
411 softc->flags |= PASS_FLAG_OPEN;
414 cam_periph_unlock(periph);
420 passclose(dev_t dev, int flag, int fmt, struct thread *td)
422 struct cam_periph *periph;
423 struct pass_softc *softc;
426 /* unit = dkunit(dev); */
427 /* XXX KDM fix this */
428 unit = minor(dev) & 0xff;
430 periph = cam_extend_get(passperiphs, unit);
434 softc = (struct pass_softc *)periph->softc;
436 if ((error = cam_periph_lock(periph, 0)) != 0)
439 softc->flags &= ~PASS_FLAG_OPEN;
441 cam_periph_unlock(periph);
442 cam_periph_release(periph);
448 * Actually translate the requested transfer into one the physical driver
449 * can understand. The transfer is described by a buf and will include
450 * only one physical transfer.
453 passstrategy(struct buf *bp)
455 struct cam_periph *periph;
456 struct pass_softc *softc;
461 * The read/write interface for the passthrough driver doesn't
462 * really work right now. So, we just pass back EINVAL to tell the
465 bp->b_error = EINVAL;
468 /* unit = dkunit(bp->b_dev); */
469 /* XXX KDM fix this */
470 unit = minor(bp->b_dev) & 0xff;
472 periph = cam_extend_get(passperiphs, unit);
473 if (periph == NULL) {
477 softc = (struct pass_softc *)periph->softc;
480 * Odd number of bytes or negative offset
483 if (bp->b_blkno < 0) {
484 bp->b_error = EINVAL;
489 * Mask interrupts so that the pack cannot be invalidated until
490 * after we are in the queue. Otherwise, we might not properly
491 * clean up one of the buffers.
495 bufq_insert_tail(&softc->buf_queue, bp);
500 * Schedule ourselves for performing the work.
502 xpt_schedule(periph, /* XXX priority */1);
506 bp->b_flags |= B_ERROR;
509 * Correctly set the buf to indicate a completed xfer
511 bp->b_resid = bp->b_bcount;
517 passstart(struct cam_periph *periph, union ccb *start_ccb)
519 struct pass_softc *softc;
522 softc = (struct pass_softc *)periph->softc;
524 switch (softc->state) {
525 case PASS_STATE_NORMAL:
530 bp = bufq_first(&softc->buf_queue);
531 if (periph->immediate_priority <= periph->pinfo.priority) {
532 start_ccb->ccb_h.ccb_type = PASS_CCB_WAITING;
533 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
535 periph->immediate_priority = CAM_PRIORITY_NONE;
537 wakeup(&periph->ccb_list);
538 } else if (bp == NULL) {
540 xpt_release_ccb(start_ccb);
543 bufq_remove(&softc->buf_queue, bp);
545 devstat_start_transaction(&softc->device_stats);
549 * Interpret the contents of the bp as a CCB
550 * and pass it to a routine shared by our ioctl
552 * For now, just biodone it with EIO so we don't
556 bp->b_flags |= B_ERROR;
557 bp->b_resid = bp->b_bcount;
559 bp = bufq_first(&softc->buf_queue);
562 xpt_action(start_ccb);
566 /* Have more work to do, so ensure we stay scheduled */
567 xpt_schedule(periph, /* XXX priority */1);
574 passdone(struct cam_periph *periph, union ccb *done_ccb)
576 struct pass_softc *softc;
577 struct ccb_scsiio *csio;
579 softc = (struct pass_softc *)periph->softc;
580 csio = &done_ccb->csio;
581 switch (csio->ccb_h.ccb_type) {
582 case PASS_CCB_BUFFER_IO:
586 u_int8_t scsi_status;
587 devstat_trans_flags ds_flags;
589 status = done_ccb->ccb_h.status;
590 scsi_status = done_ccb->csio.scsi_status;
591 bp = (struct buf *)done_ccb->ccb_h.ccb_bp;
592 /* XXX handle errors */
593 if (!(((status & CAM_STATUS_MASK) == CAM_REQ_CMP)
594 && (scsi_status == SCSI_STATUS_OK))) {
597 if ((error = passerror(done_ccb, 0, 0)) == ERESTART) {
599 * A retry was scheuled, so
606 * XXX unfreeze the queue after we complete
610 bp->b_flags |= B_ERROR;
613 if ((done_ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
614 ds_flags = DEVSTAT_READ;
615 else if ((done_ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
616 ds_flags = DEVSTAT_WRITE;
618 ds_flags = DEVSTAT_NO_DATA;
620 devstat_end_transaction_buf(&softc->device_stats, bp);
624 case PASS_CCB_WAITING:
626 /* Caller will release the CCB */
627 wakeup(&done_ccb->ccb_h.cbfcnp);
631 xpt_release_ccb(done_ccb);
635 passioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
637 struct cam_periph *periph;
638 struct pass_softc *softc;
643 /* unit = dkunit(dev); */
644 /* XXX KDM fix this */
645 unit = minor(dev) & 0xff;
647 periph = cam_extend_get(passperiphs, unit);
652 softc = (struct pass_softc *)periph->softc;
664 inccb = (union ccb *)addr;
667 * Some CCB types, like scan bus and scan lun can only go
668 * through the transport layer device.
670 if (inccb->ccb_h.func_code & XPT_FC_XPT_ONLY) {
671 xpt_print_path(periph->path);
672 printf("CCB function code %#x is restricted to the "
673 "XPT device\n", inccb->ccb_h.func_code);
679 * Non-immediate CCBs need a CCB from the per-device pool
680 * of CCBs, which is scheduled by the transport layer.
681 * Immediate CCBs and user-supplied CCBs should just be
684 if ((inccb->ccb_h.func_code & XPT_FC_QUEUED)
685 && ((inccb->ccb_h.func_code & XPT_FC_USER_CCB) == 0)) {
686 ccb = cam_periph_getccb(periph,
687 inccb->ccb_h.pinfo.priority);
690 ccb = xpt_alloc_ccb();
693 xpt_setup_ccb(&ccb->ccb_h, periph->path,
694 inccb->ccb_h.pinfo.priority);
699 xpt_print_path(periph->path);
700 printf("unable to allocate CCB\n");
705 error = passsendccb(periph, ccb, inccb);
710 xpt_release_ccb(ccb);
715 error = cam_periph_ioctl(periph, cmd, addr, passerror);
723 * Generally, "ccb" should be the CCB supplied by the kernel. "inccb"
724 * should be the CCB that is copied in from the user.
727 passsendccb(struct cam_periph *periph, union ccb *ccb, union ccb *inccb)
729 struct pass_softc *softc;
730 struct cam_periph_map_info mapinfo;
731 int error, need_unmap;
733 softc = (struct pass_softc *)periph->softc;
738 * There are some fields in the CCB header that need to be
739 * preserved, the rest we get from the user.
741 xpt_merge_ccb(ccb, inccb);
744 * There's no way for the user to have a completion
745 * function, so we put our own completion function in here.
747 ccb->ccb_h.cbfcnp = passdone;
750 * We only attempt to map the user memory into kernel space
751 * if they haven't passed in a physical memory pointer,
752 * and if there is actually an I/O operation to perform.
753 * Right now cam_periph_mapmem() only supports SCSI and device
754 * match CCBs. For the SCSI CCBs, we only pass the CCB in if
755 * there's actually data to map. cam_periph_mapmem() will do the
756 * right thing, even if there isn't data to map, but since CCBs
757 * without data are a reasonably common occurance (e.g. test unit
758 * ready), it will save a few cycles if we check for it here.
760 if (((ccb->ccb_h.flags & CAM_DATA_PHYS) == 0)
761 && (((ccb->ccb_h.func_code == XPT_SCSI_IO)
762 && ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE))
763 || (ccb->ccb_h.func_code == XPT_DEV_MATCH))) {
765 bzero(&mapinfo, sizeof(mapinfo));
767 error = cam_periph_mapmem(ccb, &mapinfo);
770 * cam_periph_mapmem returned an error, we can't continue.
771 * Return the error to the user.
777 * We successfully mapped the memory in, so we need to
778 * unmap it when the transaction is done.
784 * If the user wants us to perform any error recovery, then honor
785 * that request. Otherwise, it's up to the user to perform any
788 error = cam_periph_runccb(ccb,
789 (ccb->ccb_h.flags & CAM_PASS_ERR_RECOVER) ?
792 /* sense_flags */SF_RETRY_UA | SF_RETRY_SELTO,
793 &softc->device_stats);
796 cam_periph_unmapmem(ccb, &mapinfo);
798 ccb->ccb_h.cbfcnp = NULL;
799 ccb->ccb_h.periph_priv = inccb->ccb_h.periph_priv;
800 bcopy(ccb, inccb, sizeof(union ccb));
806 passerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
808 struct cam_periph *periph;
809 struct pass_softc *softc;
811 periph = xpt_path_periph(ccb->ccb_h.path);
812 softc = (struct pass_softc *)periph->softc;
814 return(cam_periph_error(ccb, cam_flags, sense_flags,