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.6 2003/07/21 05:50:24 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/cam_ccb.h>
49 #include <cam/cam_extend.h>
50 #include <cam/cam_periph.h>
51 #include <cam/cam_xpt_periph.h>
52 #include <cam/cam_debug.h>
54 #include <cam/scsi/scsi_all.h>
55 #include <cam/scsi/scsi_message.h>
56 #include <cam/scsi/scsi_da.h>
57 #include <cam/scsi/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;
88 #define MIN(x,y) ((x<y) ? x : y)
91 #define PASS_CDEV_MAJOR 31
93 static d_open_t passopen;
94 static d_close_t passclose;
95 static d_ioctl_t passioctl;
96 static d_strategy_t passstrategy;
98 static periph_init_t passinit;
99 static periph_ctor_t passregister;
100 static periph_oninv_t passoninvalidate;
101 static periph_dtor_t passcleanup;
102 static periph_start_t passstart;
103 static void passasync(void *callback_arg, u_int32_t code,
104 struct cam_path *path, void *arg);
105 static void passdone(struct cam_periph *periph,
106 union ccb *done_ccb);
107 static int passerror(union ccb *ccb, u_int32_t cam_flags,
108 u_int32_t sense_flags);
109 static int passsendccb(struct cam_periph *periph, union ccb *ccb,
112 static struct periph_driver passdriver =
115 TAILQ_HEAD_INITIALIZER(passdriver.units), /* generation */ 0
118 DATA_SET(periphdriver_set, passdriver);
120 static struct cdevsw pass_cdevsw = {
122 /* maj */ PASS_CDEV_MAJOR,
128 /* close */ passclose,
130 /* write */ physwrite,
131 /* ioctl */ passioctl,
134 /* strategy */ passstrategy,
139 static struct extend_array *passperiphs;
145 struct cam_path *path;
148 * Create our extend array for storing the devices we attach to.
150 passperiphs = cam_extend_new();
151 if (passperiphs == NULL) {
152 printf("passm: Failed to alloc extend array!\n");
157 * Install a global async callback. This callback will
158 * receive async callbacks like "new device found".
160 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
161 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
163 if (status == CAM_REQ_CMP) {
164 struct ccb_setasync csa;
166 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
167 csa.ccb_h.func_code = XPT_SASYNC_CB;
168 csa.event_enable = AC_FOUND_DEVICE;
169 csa.callback = passasync;
170 csa.callback_arg = NULL;
171 xpt_action((union ccb *)&csa);
172 status = csa.ccb_h.status;
176 if (status != CAM_REQ_CMP) {
177 printf("pass: Failed to attach master async callback "
178 "due to status 0x%x!\n", status);
184 passoninvalidate(struct cam_periph *periph)
187 struct pass_softc *softc;
189 struct ccb_setasync csa;
191 softc = (struct pass_softc *)periph->softc;
194 * De-register any async callbacks.
196 xpt_setup_ccb(&csa.ccb_h, periph->path,
198 csa.ccb_h.func_code = XPT_SASYNC_CB;
199 csa.event_enable = 0;
200 csa.callback = passasync;
201 csa.callback_arg = periph;
202 xpt_action((union ccb *)&csa);
204 softc->flags |= PASS_FLAG_INVALID;
207 * Although the oninvalidate() routines are always called at
208 * splsoftcam, we need to be at splbio() here to keep the buffer
209 * queue from being modified while we traverse it.
214 * Return all queued I/O with ENXIO.
215 * XXX Handle any transactions queued to the card
216 * with XPT_ABORT_CCB.
218 while ((q_bp = bufq_first(&softc->buf_queue)) != NULL){
219 bufq_remove(&softc->buf_queue, q_bp);
220 q_bp->b_resid = q_bp->b_bcount;
221 q_bp->b_error = ENXIO;
222 q_bp->b_flags |= B_ERROR;
228 xpt_print_path(periph->path);
229 printf("lost device\n");
235 passcleanup(struct cam_periph *periph)
237 struct pass_softc *softc;
239 softc = (struct pass_softc *)periph->softc;
241 devstat_remove_entry(&softc->device_stats);
243 destroy_dev(softc->dev);
245 cam_extend_release(passperiphs, periph->unit_number);
248 xpt_print_path(periph->path);
249 printf("removing device entry\n");
251 free(softc, M_DEVBUF);
255 passasync(void *callback_arg, u_int32_t code,
256 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;
271 * Allocate a peripheral instance for
272 * this device and start the probe
275 status = cam_periph_alloc(passregister, passoninvalidate,
276 passcleanup, passstart, "pass",
277 CAM_PERIPH_BIO, cgd->ccb_h.path,
278 passasync, AC_FOUND_DEVICE, cgd);
280 if (status != CAM_REQ_CMP
281 && status != CAM_REQ_INPROG)
282 printf("passasync: Unable to attach new device "
283 "due to status 0x%x\n", status);
288 cam_periph_async(periph, code, path, arg);
294 passregister(struct cam_periph *periph, void *arg)
296 struct pass_softc *softc;
297 struct ccb_setasync csa;
298 struct ccb_getdev *cgd;
300 cgd = (struct ccb_getdev *)arg;
301 if (periph == NULL) {
302 printf("passregister: periph was NULL!!\n");
303 return(CAM_REQ_CMP_ERR);
307 printf("passregister: no getdev CCB, can't register device\n");
308 return(CAM_REQ_CMP_ERR);
311 softc = (struct pass_softc *)malloc(sizeof(*softc),
315 printf("passregister: Unable to probe new device. "
316 "Unable to allocate softc\n");
317 return(CAM_REQ_CMP_ERR);
320 bzero(softc, sizeof(*softc));
321 softc->state = PASS_STATE_NORMAL;
322 softc->pd_type = SID_TYPE(&cgd->inq_data);
323 bufq_init(&softc->buf_queue);
325 periph->softc = softc;
327 cam_extend_set(passperiphs, periph->unit_number, periph);
329 * We pass in 0 for a blocksize, since we don't
330 * know what the blocksize of this device is, if
331 * it even has a blocksize.
333 devstat_add_entry(&softc->device_stats, "pass", periph->unit_number,
334 0, DEVSTAT_NO_BLOCKSIZE | DEVSTAT_NO_ORDERED_TAGS,
336 DEVSTAT_TYPE_IF_SCSI |
338 DEVSTAT_PRIORITY_PASS);
340 /* Register the device */
341 softc->dev = make_dev(&pass_cdevsw, periph->unit_number, UID_ROOT,
342 GID_OPERATOR, 0600, "%s%d", periph->periph_name,
343 periph->unit_number);
346 * Add an async callback so that we get
347 * notified if this device goes away.
349 xpt_setup_ccb(&csa.ccb_h, periph->path, /* priority */ 5);
350 csa.ccb_h.func_code = XPT_SASYNC_CB;
351 csa.event_enable = AC_LOST_DEVICE;
352 csa.callback = passasync;
353 csa.callback_arg = periph;
354 xpt_action((union ccb *)&csa);
357 xpt_announce_periph(periph, NULL);
363 passopen(dev_t dev, int flags, int fmt, struct thread *td)
365 struct cam_periph *periph;
366 struct pass_softc *softc;
370 error = 0; /* default to no error */
372 /* unit = dkunit(dev); */
373 /* XXX KDM fix this */
374 unit = minor(dev) & 0xff;
376 periph = cam_extend_get(passperiphs, unit);
381 softc = (struct pass_softc *)periph->softc;
384 if (softc->flags & PASS_FLAG_INVALID) {
390 * Don't allow access when we're running at a high securelvel.
392 if (securelevel > 1) {
398 * Only allow read-write access.
400 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0)) {
406 * We don't allow nonblocking access.
408 if ((flags & O_NONBLOCK) != 0) {
409 xpt_print_path(periph->path);
410 printf("can't do nonblocking accesss\n");
415 if ((error = cam_periph_lock(periph, PCATCH)) != 0) {
422 if ((softc->flags & PASS_FLAG_OPEN) == 0) {
423 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
425 softc->flags |= PASS_FLAG_OPEN;
428 cam_periph_unlock(periph);
434 passclose(dev_t dev, int flag, int fmt, struct thread *td)
436 struct cam_periph *periph;
437 struct pass_softc *softc;
440 /* unit = dkunit(dev); */
441 /* XXX KDM fix this */
442 unit = minor(dev) & 0xff;
444 periph = cam_extend_get(passperiphs, unit);
448 softc = (struct pass_softc *)periph->softc;
450 if ((error = cam_periph_lock(periph, 0)) != 0)
453 softc->flags &= ~PASS_FLAG_OPEN;
455 cam_periph_unlock(periph);
456 cam_periph_release(periph);
462 * Actually translate the requested transfer into one the physical driver
463 * can understand. The transfer is described by a buf and will include
464 * only one physical transfer.
467 passstrategy(struct buf *bp)
469 struct cam_periph *periph;
470 struct pass_softc *softc;
475 * The read/write interface for the passthrough driver doesn't
476 * really work right now. So, we just pass back EINVAL to tell the
479 bp->b_error = EINVAL;
482 /* unit = dkunit(bp->b_dev); */
483 /* XXX KDM fix this */
484 unit = minor(bp->b_dev) & 0xff;
486 periph = cam_extend_get(passperiphs, unit);
487 if (periph == NULL) {
491 softc = (struct pass_softc *)periph->softc;
494 * Odd number of bytes or negative offset
497 if (bp->b_blkno < 0) {
498 bp->b_error = EINVAL;
503 * Mask interrupts so that the pack cannot be invalidated until
504 * after we are in the queue. Otherwise, we might not properly
505 * clean up one of the buffers.
509 bufq_insert_tail(&softc->buf_queue, bp);
514 * Schedule ourselves for performing the work.
516 xpt_schedule(periph, /* XXX priority */1);
520 bp->b_flags |= B_ERROR;
523 * Correctly set the buf to indicate a completed xfer
525 bp->b_resid = bp->b_bcount;
531 passstart(struct cam_periph *periph, union ccb *start_ccb)
533 struct pass_softc *softc;
536 softc = (struct pass_softc *)periph->softc;
538 switch (softc->state) {
539 case PASS_STATE_NORMAL:
544 bp = bufq_first(&softc->buf_queue);
545 if (periph->immediate_priority <= periph->pinfo.priority) {
546 start_ccb->ccb_h.ccb_type = PASS_CCB_WAITING;
547 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
549 periph->immediate_priority = CAM_PRIORITY_NONE;
551 wakeup(&periph->ccb_list);
552 } else if (bp == NULL) {
554 xpt_release_ccb(start_ccb);
557 bufq_remove(&softc->buf_queue, bp);
559 devstat_start_transaction(&softc->device_stats);
563 * Interpret the contents of the bp as a CCB
564 * and pass it to a routine shared by our ioctl
566 * For now, just biodone it with EIO so we don't
570 bp->b_flags |= B_ERROR;
571 bp->b_resid = bp->b_bcount;
573 bp = bufq_first(&softc->buf_queue);
576 xpt_action(start_ccb);
580 /* Have more work to do, so ensure we stay scheduled */
581 xpt_schedule(periph, /* XXX priority */1);
588 passdone(struct cam_periph *periph, union ccb *done_ccb)
590 struct pass_softc *softc;
591 struct ccb_scsiio *csio;
593 softc = (struct pass_softc *)periph->softc;
594 csio = &done_ccb->csio;
595 switch (csio->ccb_h.ccb_type) {
596 case PASS_CCB_BUFFER_IO:
600 u_int8_t scsi_status;
601 devstat_trans_flags ds_flags;
603 status = done_ccb->ccb_h.status;
604 scsi_status = done_ccb->csio.scsi_status;
605 bp = (struct buf *)done_ccb->ccb_h.ccb_bp;
606 /* XXX handle errors */
607 if (!(((status & CAM_STATUS_MASK) == CAM_REQ_CMP)
608 && (scsi_status == SCSI_STATUS_OK))) {
611 if ((error = passerror(done_ccb, 0, 0)) == ERESTART) {
613 * A retry was scheuled, so
620 * XXX unfreeze the queue after we complete
624 bp->b_flags |= B_ERROR;
627 if ((done_ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
628 ds_flags = DEVSTAT_READ;
629 else if ((done_ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
630 ds_flags = DEVSTAT_WRITE;
632 ds_flags = DEVSTAT_NO_DATA;
634 devstat_end_transaction_buf(&softc->device_stats, bp);
638 case PASS_CCB_WAITING:
640 /* Caller will release the CCB */
641 wakeup(&done_ccb->ccb_h.cbfcnp);
645 xpt_release_ccb(done_ccb);
649 passioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
651 struct cam_periph *periph;
652 struct pass_softc *softc;
657 /* unit = dkunit(dev); */
658 /* XXX KDM fix this */
659 unit = minor(dev) & 0xff;
661 periph = cam_extend_get(passperiphs, unit);
666 softc = (struct pass_softc *)periph->softc;
678 inccb = (union ccb *)addr;
681 * Some CCB types, like scan bus and scan lun can only go
682 * through the transport layer device.
684 if (inccb->ccb_h.func_code & XPT_FC_XPT_ONLY) {
685 xpt_print_path(periph->path);
686 printf("CCB function code %#x is restricted to the "
687 "XPT device\n", inccb->ccb_h.func_code);
693 * Non-immediate CCBs need a CCB from the per-device pool
694 * of CCBs, which is scheduled by the transport layer.
695 * Immediate CCBs and user-supplied CCBs should just be
698 if ((inccb->ccb_h.func_code & XPT_FC_QUEUED)
699 && ((inccb->ccb_h.func_code & XPT_FC_USER_CCB) == 0)) {
700 ccb = cam_periph_getccb(periph,
701 inccb->ccb_h.pinfo.priority);
704 ccb = xpt_alloc_ccb();
707 xpt_setup_ccb(&ccb->ccb_h, periph->path,
708 inccb->ccb_h.pinfo.priority);
713 xpt_print_path(periph->path);
714 printf("unable to allocate CCB\n");
719 error = passsendccb(periph, ccb, inccb);
724 xpt_release_ccb(ccb);
729 error = cam_periph_ioctl(periph, cmd, addr, passerror);
737 * Generally, "ccb" should be the CCB supplied by the kernel. "inccb"
738 * should be the CCB that is copied in from the user.
741 passsendccb(struct cam_periph *periph, union ccb *ccb, union ccb *inccb)
743 struct pass_softc *softc;
744 struct cam_periph_map_info mapinfo;
745 int error, need_unmap;
747 softc = (struct pass_softc *)periph->softc;
752 * There are some fields in the CCB header that need to be
753 * preserved, the rest we get from the user.
755 xpt_merge_ccb(ccb, inccb);
758 * There's no way for the user to have a completion
759 * function, so we put our own completion function in here.
761 ccb->ccb_h.cbfcnp = passdone;
764 * We only attempt to map the user memory into kernel space
765 * if they haven't passed in a physical memory pointer,
766 * and if there is actually an I/O operation to perform.
767 * Right now cam_periph_mapmem() only supports SCSI and device
768 * match CCBs. For the SCSI CCBs, we only pass the CCB in if
769 * there's actually data to map. cam_periph_mapmem() will do the
770 * right thing, even if there isn't data to map, but since CCBs
771 * without data are a reasonably common occurance (e.g. test unit
772 * ready), it will save a few cycles if we check for it here.
774 if (((ccb->ccb_h.flags & CAM_DATA_PHYS) == 0)
775 && (((ccb->ccb_h.func_code == XPT_SCSI_IO)
776 && ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE))
777 || (ccb->ccb_h.func_code == XPT_DEV_MATCH))) {
779 bzero(&mapinfo, sizeof(mapinfo));
781 error = cam_periph_mapmem(ccb, &mapinfo);
784 * cam_periph_mapmem returned an error, we can't continue.
785 * Return the error to the user.
791 * We successfully mapped the memory in, so we need to
792 * unmap it when the transaction is done.
798 * If the user wants us to perform any error recovery, then honor
799 * that request. Otherwise, it's up to the user to perform any
802 error = cam_periph_runccb(ccb,
803 (ccb->ccb_h.flags & CAM_PASS_ERR_RECOVER) ?
806 /* sense_flags */SF_RETRY_UA | SF_RETRY_SELTO,
807 &softc->device_stats);
810 cam_periph_unmapmem(ccb, &mapinfo);
812 ccb->ccb_h.cbfcnp = NULL;
813 ccb->ccb_h.periph_priv = inccb->ccb_h.periph_priv;
814 bcopy(ccb, inccb, sizeof(union ccb));
820 passerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
822 struct cam_periph *periph;
823 struct pass_softc *softc;
825 periph = xpt_path_periph(ccb->ccb_h.path);
826 softc = (struct pass_softc *)periph->softc;
828 return(cam_periph_error(ccb, cam_flags, sense_flags,