2 * Implementation of SCSI Processor Target Peripheral driver for CAM.
4 * Copyright (c) 1998 Justin T. Gibbs.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, and the following disclaimer,
12 * without modification, immediately at the beginning of the file.
13 * 2. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * $FreeBSD: src/sys/cam/scsi/scsi_pt.c,v 1.17 2000/01/17 06:27:37 mjacob Exp $
31 #include <sys/param.h>
32 #include <sys/queue.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/types.h>
37 #include <sys/devicestat.h>
38 #include <sys/malloc.h>
43 #include <cam/cam_ccb.h>
44 #include <cam/cam_extend.h>
45 #include <cam/cam_periph.h>
46 #include <cam/cam_xpt_periph.h>
47 #include <cam/cam_debug.h>
49 #include <cam/scsi/scsi_all.h>
50 #include <cam/scsi/scsi_message.h>
51 #include <cam/scsi/scsi_pt.h>
63 PT_FLAG_DEVICE_INVALID = 0x02,
64 PT_FLAG_RETRY_UA = 0x04
68 PT_CCB_BUFFER_IO = 0x01,
69 PT_CCB_WAITING = 0x02,
70 PT_CCB_RETRY_UA = 0x04,
71 PT_CCB_BUFFER_IO_UA = PT_CCB_BUFFER_IO|PT_CCB_RETRY_UA
74 /* Offsets into our private area for storing information */
75 #define ccb_state ppriv_field0
76 #define ccb_bp ppriv_ptr1
79 struct buf_queue_head buf_queue;
80 struct devstat device_stats;
81 LIST_HEAD(, ccb_hdr) pending_ccbs;
89 static d_open_t ptopen;
90 static d_close_t ptclose;
91 static d_strategy_t ptstrategy;
92 static periph_init_t ptinit;
93 static void ptasync(void *callback_arg, u_int32_t code,
94 struct cam_path *path, void *arg);
95 static periph_ctor_t ptctor;
96 static periph_oninv_t ptoninvalidate;
97 static periph_dtor_t ptdtor;
98 static periph_start_t ptstart;
99 static void ptdone(struct cam_periph *periph,
100 union ccb *done_ccb);
101 static d_ioctl_t ptioctl;
102 static int pterror(union ccb *ccb, u_int32_t cam_flags,
103 u_int32_t sense_flags);
105 void scsi_send_receive(struct ccb_scsiio *csio, u_int32_t retries,
106 void (*cbfcnp)(struct cam_periph *, union ccb *),
107 u_int tag_action, int readop, u_int byte2,
108 u_int32_t xfer_len, u_int8_t *data_ptr,
109 u_int8_t sense_len, u_int32_t timeout);
111 static struct periph_driver ptdriver =
114 TAILQ_HEAD_INITIALIZER(ptdriver.units), /* generation */ 0
117 DATA_SET(periphdriver_set, ptdriver);
119 #define PT_CDEV_MAJOR 61
121 static struct cdevsw pt_cdevsw = {
125 /* write */ physwrite,
129 /* strategy */ ptstrategy,
131 /* maj */ PT_CDEV_MAJOR,
138 static struct extend_array *ptperiphs;
140 #ifndef SCSI_PT_DEFAULT_TIMEOUT
141 #define SCSI_PT_DEFAULT_TIMEOUT 60
145 ptopen(dev_t dev, int flags, int fmt, struct proc *p)
147 struct cam_periph *periph;
148 struct pt_softc *softc;
154 periph = cam_extend_get(ptperiphs, unit);
158 softc = (struct pt_softc *)periph->softc;
161 if (softc->flags & PT_FLAG_DEVICE_INVALID) {
166 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
167 ("ptopen: dev=%s (unit %d)\n", devtoname(dev), unit));
169 if ((error = cam_periph_lock(periph, PRIBIO|PCATCH)) != 0) {
171 return (error); /* error code from tsleep */
176 if ((softc->flags & PT_FLAG_OPEN) == 0) {
177 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
180 softc->flags |= PT_FLAG_OPEN;
184 cam_periph_unlock(periph);
189 ptclose(dev_t dev, int flag, int fmt, struct proc *p)
191 struct cam_periph *periph;
192 struct pt_softc *softc;
197 periph = cam_extend_get(ptperiphs, unit);
201 softc = (struct pt_softc *)periph->softc;
203 if ((error = cam_periph_lock(periph, PRIBIO)) != 0)
204 return (error); /* error code from tsleep */
206 softc->flags &= ~PT_FLAG_OPEN;
207 cam_periph_unlock(periph);
208 cam_periph_release(periph);
213 * Actually translate the requested transfer into one the physical driver
214 * can understand. The transfer is described by a buf and will include
215 * only one physical transfer.
218 ptstrategy(struct buf *bp)
220 struct cam_periph *periph;
221 struct pt_softc *softc;
225 unit = minor(bp->b_dev);
226 periph = cam_extend_get(ptperiphs, unit);
227 if (periph == NULL) {
231 softc = (struct pt_softc *)periph->softc;
234 * Mask interrupts so that the pack cannot be invalidated until
235 * after we are in the queue. Otherwise, we might not properly
236 * clean up one of the buffers.
241 * If the device has been made invalid, error out
243 if ((softc->flags & PT_FLAG_DEVICE_INVALID)) {
250 * Place it in the queue of disk activities for this disk
252 bufq_insert_tail(&softc->buf_queue, bp);
257 * Schedule ourselves for performing the work.
259 xpt_schedule(periph, /* XXX priority */1);
263 bp->b_flags |= B_ERROR;
266 * Correctly set the buf to indicate a completed xfer
268 bp->b_resid = bp->b_bcount;
276 struct cam_path *path;
279 * Create our extend array for storing the devices we attach to.
281 ptperiphs = cam_extend_new();
282 if (ptperiphs == NULL) {
283 printf("pt: Failed to alloc extend array!\n");
288 * Install a global async callback. This callback will
289 * receive async callbacks like "new device found".
291 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
292 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
294 if (status == CAM_REQ_CMP) {
295 struct ccb_setasync csa;
297 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
298 csa.ccb_h.func_code = XPT_SASYNC_CB;
299 csa.event_enable = AC_FOUND_DEVICE;
300 csa.callback = ptasync;
301 csa.callback_arg = NULL;
302 xpt_action((union ccb *)&csa);
303 status = csa.ccb_h.status;
307 if (status != CAM_REQ_CMP) {
308 printf("pt: Failed to attach master async callback "
309 "due to status 0x%x!\n", status);
314 ptctor(struct cam_periph *periph, void *arg)
316 struct pt_softc *softc;
317 struct ccb_setasync csa;
318 struct ccb_getdev *cgd;
320 cgd = (struct ccb_getdev *)arg;
321 if (periph == NULL) {
322 printf("ptregister: periph was NULL!!\n");
323 return(CAM_REQ_CMP_ERR);
327 printf("ptregister: no getdev CCB, can't register device\n");
328 return(CAM_REQ_CMP_ERR);
331 softc = (struct pt_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT);
334 printf("daregister: Unable to probe new device. "
335 "Unable to allocate softc\n");
336 return(CAM_REQ_CMP_ERR);
339 bzero(softc, sizeof(*softc));
340 LIST_INIT(&softc->pending_ccbs);
341 softc->state = PT_STATE_NORMAL;
342 bufq_init(&softc->buf_queue);
344 softc->io_timeout = SCSI_PT_DEFAULT_TIMEOUT * 1000;
346 periph->softc = softc;
348 cam_extend_set(ptperiphs, periph->unit_number, periph);
350 devstat_add_entry(&softc->device_stats, "pt",
351 periph->unit_number, 0,
352 DEVSTAT_NO_BLOCKSIZE,
353 SID_TYPE(&cgd->inq_data) | DEVSTAT_TYPE_IF_SCSI,
354 DEVSTAT_PRIORITY_OTHER);
356 softc->dev = make_dev(&pt_cdevsw, periph->unit_number, UID_ROOT,
357 GID_OPERATOR, 0600, "%s%d", periph->periph_name,
358 periph->unit_number);
360 * Add async callbacks for bus reset and
361 * bus device reset calls. I don't bother
362 * checking if this fails as, in most cases,
363 * the system will function just fine without
364 * them and the only alternative would be to
365 * not attach the device on failure.
367 xpt_setup_ccb(&csa.ccb_h, periph->path, /*priority*/5);
368 csa.ccb_h.func_code = XPT_SASYNC_CB;
369 csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE;
370 csa.callback = ptasync;
371 csa.callback_arg = periph;
372 xpt_action((union ccb *)&csa);
374 /* Tell the user we've attached to the device */
375 xpt_announce_periph(periph, NULL);
381 ptoninvalidate(struct cam_periph *periph)
384 struct pt_softc *softc;
386 struct ccb_setasync csa;
388 softc = (struct pt_softc *)periph->softc;
391 * De-register any async callbacks.
393 xpt_setup_ccb(&csa.ccb_h, periph->path,
395 csa.ccb_h.func_code = XPT_SASYNC_CB;
396 csa.event_enable = 0;
397 csa.callback = ptasync;
398 csa.callback_arg = periph;
399 xpt_action((union ccb *)&csa);
401 softc->flags |= PT_FLAG_DEVICE_INVALID;
404 * Although the oninvalidate() routines are always called at
405 * splsoftcam, we need to be at splbio() here to keep the buffer
406 * queue from being modified while we traverse it.
411 * Return all queued I/O with ENXIO.
412 * XXX Handle any transactions queued to the card
413 * with XPT_ABORT_CCB.
415 while ((q_bp = bufq_first(&softc->buf_queue)) != NULL){
416 bufq_remove(&softc->buf_queue, q_bp);
417 q_bp->b_resid = q_bp->b_bcount;
418 q_bp->b_error = ENXIO;
419 q_bp->b_flags |= B_ERROR;
425 xpt_print_path(periph->path);
426 printf("lost device\n");
430 ptdtor(struct cam_periph *periph)
432 struct pt_softc *softc;
434 softc = (struct pt_softc *)periph->softc;
436 devstat_remove_entry(&softc->device_stats);
438 destroy_dev(softc->dev);
440 cam_extend_release(ptperiphs, periph->unit_number);
441 xpt_print_path(periph->path);
442 printf("removing device entry\n");
443 free(softc, M_DEVBUF);
447 ptasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
449 struct cam_periph *periph;
451 periph = (struct cam_periph *)callback_arg;
453 case AC_FOUND_DEVICE:
455 struct ccb_getdev *cgd;
458 cgd = (struct ccb_getdev *)arg;
460 if (SID_TYPE(&cgd->inq_data) != T_PROCESSOR)
464 * Allocate a peripheral instance for
465 * this device and start the probe
468 status = cam_periph_alloc(ptctor, ptoninvalidate, ptdtor,
469 ptstart, "pt", CAM_PERIPH_BIO,
470 cgd->ccb_h.path, ptasync,
471 AC_FOUND_DEVICE, cgd);
473 if (status != CAM_REQ_CMP
474 && status != CAM_REQ_INPROG)
475 printf("ptasync: Unable to attach to new device "
476 "due to status 0x%x\n", status);
482 struct pt_softc *softc;
483 struct ccb_hdr *ccbh;
486 softc = (struct pt_softc *)periph->softc;
489 * Don't fail on the expected unit attention
492 softc->flags |= PT_FLAG_RETRY_UA;
493 for (ccbh = LIST_FIRST(&softc->pending_ccbs);
494 ccbh != NULL; ccbh = LIST_NEXT(ccbh, periph_links.le))
495 ccbh->ccb_state |= PT_CCB_RETRY_UA;
500 cam_periph_async(periph, code, path, arg);
506 ptstart(struct cam_periph *periph, union ccb *start_ccb)
508 struct pt_softc *softc;
512 softc = (struct pt_softc *)periph->softc;
515 * See if there is a buf with work for us to do..
518 bp = bufq_first(&softc->buf_queue);
519 if (periph->immediate_priority <= periph->pinfo.priority) {
520 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
521 ("queuing for immediate ccb\n"));
522 start_ccb->ccb_h.ccb_state = PT_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 (bp == NULL) {
530 xpt_release_ccb(start_ccb);
534 bufq_remove(&softc->buf_queue, bp);
536 devstat_start_transaction(&softc->device_stats);
538 scsi_send_receive(&start_ccb->csio,
542 bp->b_flags & B_READ,
546 /*sense_len*/SSD_FULL_SIZE,
547 /*timeout*/softc->io_timeout);
549 start_ccb->ccb_h.ccb_state = PT_CCB_BUFFER_IO;
552 * Block out any asyncronous callbacks
553 * while we touch the pending ccb list.
556 LIST_INSERT_HEAD(&softc->pending_ccbs, &start_ccb->ccb_h,
560 start_ccb->ccb_h.ccb_bp = bp;
561 bp = bufq_first(&softc->buf_queue);
564 xpt_action(start_ccb);
567 /* Have more work to do, so ensure we stay scheduled */
568 xpt_schedule(periph, /* XXX priority */1);
574 ptdone(struct cam_periph *periph, union ccb *done_ccb)
576 struct pt_softc *softc;
577 struct ccb_scsiio *csio;
579 softc = (struct pt_softc *)periph->softc;
580 csio = &done_ccb->csio;
581 switch (csio->ccb_h.ccb_state) {
582 case PT_CCB_BUFFER_IO:
583 case PT_CCB_BUFFER_IO_UA:
588 bp = (struct buf *)done_ccb->ccb_h.ccb_bp;
589 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
594 if ((csio->ccb_h.ccb_state & PT_CCB_RETRY_UA) != 0)
599 sf |= SF_RETRY_SELTO;
601 if ((error = pterror(done_ccb, 0, sf)) == ERESTART) {
603 * A retry was scheuled, so
613 if (error == ENXIO) {
615 * Catastrophic error. Mark our device
618 xpt_print_path(periph->path);
619 printf("Invalidating device\n");
620 softc->flags |= PT_FLAG_DEVICE_INVALID;
624 * return all queued I/O with EIO, so that
625 * the client can retry these I/Os in the
626 * proper order should it attempt to recover.
628 while ((q_bp = bufq_first(&softc->buf_queue))
630 bufq_remove(&softc->buf_queue, q_bp);
631 q_bp->b_resid = q_bp->b_bcount;
633 q_bp->b_flags |= B_ERROR;
638 bp->b_resid = bp->b_bcount;
639 bp->b_flags |= B_ERROR;
641 bp->b_resid = csio->resid;
643 if (bp->b_resid != 0) {
644 /* Short transfer ??? */
645 bp->b_flags |= B_ERROR;
648 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
649 cam_release_devq(done_ccb->ccb_h.path,
655 bp->b_resid = csio->resid;
656 if (bp->b_resid != 0)
657 bp->b_flags |= B_ERROR;
661 * Block out any asyncronous callbacks
662 * while we touch the pending ccb list.
665 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
668 devstat_end_transaction_buf(&softc->device_stats, bp);
673 /* Caller will release the CCB */
674 wakeup(&done_ccb->ccb_h.cbfcnp);
677 xpt_release_ccb(done_ccb);
681 pterror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
683 struct pt_softc *softc;
684 struct cam_periph *periph;
686 periph = xpt_path_periph(ccb->ccb_h.path);
687 softc = (struct pt_softc *)periph->softc;
689 return(cam_periph_error(ccb, cam_flags, sense_flags,
694 ptioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
696 struct cam_periph *periph;
697 struct pt_softc *softc;
702 periph = cam_extend_get(ptperiphs, unit);
707 softc = (struct pt_softc *)periph->softc;
709 if ((error = cam_periph_lock(periph, PRIBIO|PCATCH)) != 0) {
710 return (error); /* error code from tsleep */
714 case PTIOCGETTIMEOUT:
715 if (softc->io_timeout >= 1000)
716 *(int *)addr = softc->io_timeout / 1000;
720 case PTIOCSETTIMEOUT:
724 if (*(int *)addr < 1) {
730 softc->io_timeout = *(int *)addr * 1000;
736 error = cam_periph_ioctl(periph, cmd, addr, pterror);
740 cam_periph_unlock(periph);
746 scsi_send_receive(struct ccb_scsiio *csio, u_int32_t retries,
747 void (*cbfcnp)(struct cam_periph *, union ccb *),
748 u_int tag_action, int readop, u_int byte2,
749 u_int32_t xfer_len, u_int8_t *data_ptr, u_int8_t sense_len,
752 struct scsi_send_receive *scsi_cmd;
754 scsi_cmd = (struct scsi_send_receive *)&csio->cdb_io.cdb_bytes;
755 scsi_cmd->opcode = readop ? RECEIVE : SEND;
756 scsi_cmd->byte2 = byte2;
757 scsi_ulto3b(xfer_len, scsi_cmd->xfer_len);
758 scsi_cmd->control = 0;
763 /*flags*/readop ? CAM_DIR_IN : CAM_DIR_OUT,