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 $
29 * $DragonFly: src/sys/bus/cam/scsi/scsi_pt.c,v 1.11 2004/05/19 22:52:38 dillon Exp $
32 #include <sys/param.h>
33 #include <sys/queue.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/types.h>
38 #include <sys/devicestat.h>
39 #include <sys/malloc.h>
45 #include "../cam_ccb.h"
46 #include "../cam_extend.h"
47 #include "../cam_periph.h"
48 #include "../cam_xpt_periph.h"
49 #include "../cam_debug.h"
52 #include "scsi_message.h"
65 PT_FLAG_DEVICE_INVALID = 0x02,
66 PT_FLAG_RETRY_UA = 0x04
70 PT_CCB_BUFFER_IO = 0x01,
71 PT_CCB_WAITING = 0x02,
72 PT_CCB_RETRY_UA = 0x04,
73 PT_CCB_BUFFER_IO_UA = PT_CCB_BUFFER_IO|PT_CCB_RETRY_UA
76 /* Offsets into our private area for storing information */
77 #define ccb_state ppriv_field0
78 #define ccb_bp ppriv_ptr1
81 struct buf_queue_head buf_queue;
82 struct devstat device_stats;
83 LIST_HEAD(, ccb_hdr) pending_ccbs;
91 static d_open_t ptopen;
92 static d_close_t ptclose;
93 static d_strategy_t ptstrategy;
94 static periph_init_t ptinit;
95 static void ptasync(void *callback_arg, u_int32_t code,
96 struct cam_path *path, void *arg);
97 static periph_ctor_t ptctor;
98 static periph_oninv_t ptoninvalidate;
99 static periph_dtor_t ptdtor;
100 static periph_start_t ptstart;
101 static void ptdone(struct cam_periph *periph,
102 union ccb *done_ccb);
103 static d_ioctl_t ptioctl;
104 static int pterror(union ccb *ccb, u_int32_t cam_flags,
105 u_int32_t sense_flags);
107 void scsi_send_receive(struct ccb_scsiio *csio, u_int32_t retries,
108 void (*cbfcnp)(struct cam_periph *, union ccb *),
109 u_int tag_action, int readop, u_int byte2,
110 u_int32_t xfer_len, u_int8_t *data_ptr,
111 u_int8_t sense_len, u_int32_t timeout);
113 static struct periph_driver ptdriver =
116 TAILQ_HEAD_INITIALIZER(ptdriver.units), /* generation */ 0
119 DATA_SET(periphdriver_set, ptdriver);
121 #define PT_CDEV_MAJOR 61
123 static struct cdevsw pt_cdevsw = {
125 /* maj */ PT_CDEV_MAJOR,
133 /* write */ physwrite,
137 /* strategy */ ptstrategy,
142 static struct extend_array *ptperiphs;
144 #ifndef SCSI_PT_DEFAULT_TIMEOUT
145 #define SCSI_PT_DEFAULT_TIMEOUT 60
149 ptopen(dev_t dev, int flags, int fmt, struct thread *td)
151 struct cam_periph *periph;
152 struct pt_softc *softc;
158 periph = cam_extend_get(ptperiphs, unit);
162 softc = (struct pt_softc *)periph->softc;
165 if (softc->flags & PT_FLAG_DEVICE_INVALID) {
170 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
171 ("ptopen: dev=%s (unit %d)\n", devtoname(dev), unit));
173 if ((error = cam_periph_lock(periph, PCATCH)) != 0) {
175 return (error); /* error code from tsleep */
180 if ((softc->flags & PT_FLAG_OPEN) == 0) {
181 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
184 softc->flags |= PT_FLAG_OPEN;
188 cam_periph_unlock(periph);
193 ptclose(dev_t dev, int flag, int fmt, struct thread *td)
195 struct cam_periph *periph;
196 struct pt_softc *softc;
201 periph = cam_extend_get(ptperiphs, unit);
205 softc = (struct pt_softc *)periph->softc;
207 if ((error = cam_periph_lock(periph, 0)) != 0)
208 return (error); /* error code from tsleep */
210 softc->flags &= ~PT_FLAG_OPEN;
211 cam_periph_unlock(periph);
212 cam_periph_release(periph);
217 * Actually translate the requested transfer into one the physical driver
218 * can understand. The transfer is described by a buf and will include
219 * only one physical transfer.
222 ptstrategy(struct buf *bp)
224 struct cam_periph *periph;
225 struct pt_softc *softc;
229 unit = minor(bp->b_dev);
230 periph = cam_extend_get(ptperiphs, unit);
231 if (periph == NULL) {
235 softc = (struct pt_softc *)periph->softc;
238 * Mask interrupts so that the pack cannot be invalidated until
239 * after we are in the queue. Otherwise, we might not properly
240 * clean up one of the buffers.
245 * If the device has been made invalid, error out
247 if ((softc->flags & PT_FLAG_DEVICE_INVALID)) {
254 * Place it in the queue of disk activities for this disk
256 bufq_insert_tail(&softc->buf_queue, bp);
261 * Schedule ourselves for performing the work.
263 xpt_schedule(periph, /* XXX priority */1);
267 bp->b_flags |= B_ERROR;
270 * Correctly set the buf to indicate a completed xfer
272 bp->b_resid = bp->b_bcount;
280 struct cam_path *path;
283 * Create our extend array for storing the devices we attach to.
285 ptperiphs = cam_extend_new();
286 if (ptperiphs == NULL) {
287 printf("pt: Failed to alloc extend array!\n");
292 * Install a global async callback. This callback will
293 * receive async callbacks like "new device found".
295 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
296 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
298 if (status == CAM_REQ_CMP) {
299 struct ccb_setasync csa;
301 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
302 csa.ccb_h.func_code = XPT_SASYNC_CB;
303 csa.event_enable = AC_FOUND_DEVICE;
304 csa.callback = ptasync;
305 csa.callback_arg = NULL;
306 xpt_action((union ccb *)&csa);
307 status = csa.ccb_h.status;
311 if (status != CAM_REQ_CMP) {
312 printf("pt: Failed to attach master async callback "
313 "due to status 0x%x!\n", status);
318 ptctor(struct cam_periph *periph, void *arg)
320 struct pt_softc *softc;
321 struct ccb_setasync csa;
322 struct ccb_getdev *cgd;
324 cgd = (struct ccb_getdev *)arg;
325 if (periph == NULL) {
326 printf("ptregister: periph was NULL!!\n");
327 return(CAM_REQ_CMP_ERR);
331 printf("ptregister: no getdev CCB, can't register device\n");
332 return(CAM_REQ_CMP_ERR);
335 softc = malloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO);
336 LIST_INIT(&softc->pending_ccbs);
337 softc->state = PT_STATE_NORMAL;
338 bufq_init(&softc->buf_queue);
340 softc->io_timeout = SCSI_PT_DEFAULT_TIMEOUT * 1000;
342 periph->softc = softc;
344 cam_extend_set(ptperiphs, periph->unit_number, periph);
346 devstat_add_entry(&softc->device_stats, "pt",
347 periph->unit_number, 0,
348 DEVSTAT_NO_BLOCKSIZE,
349 SID_TYPE(&cgd->inq_data) | DEVSTAT_TYPE_IF_SCSI,
350 DEVSTAT_PRIORITY_OTHER);
352 cdevsw_add(&pt_cdevsw, -1, periph->unit_number);
353 make_dev(&pt_cdevsw, periph->unit_number, UID_ROOT,
354 GID_OPERATOR, 0600, "%s%d", periph->periph_name,
355 periph->unit_number);
357 * Add async callbacks for bus reset and
358 * bus device reset calls. I don't bother
359 * checking if this fails as, in most cases,
360 * the system will function just fine without
361 * them and the only alternative would be to
362 * not attach the device on failure.
364 xpt_setup_ccb(&csa.ccb_h, periph->path, /*priority*/5);
365 csa.ccb_h.func_code = XPT_SASYNC_CB;
366 csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE;
367 csa.callback = ptasync;
368 csa.callback_arg = periph;
369 xpt_action((union ccb *)&csa);
371 /* Tell the user we've attached to the device */
372 xpt_announce_periph(periph, NULL);
378 ptoninvalidate(struct cam_periph *periph)
381 struct pt_softc *softc;
383 struct ccb_setasync csa;
385 softc = (struct pt_softc *)periph->softc;
388 * De-register any async callbacks.
390 xpt_setup_ccb(&csa.ccb_h, periph->path,
392 csa.ccb_h.func_code = XPT_SASYNC_CB;
393 csa.event_enable = 0;
394 csa.callback = ptasync;
395 csa.callback_arg = periph;
396 xpt_action((union ccb *)&csa);
398 softc->flags |= PT_FLAG_DEVICE_INVALID;
401 * Although the oninvalidate() routines are always called at
402 * splsoftcam, we need to be at splbio() here to keep the buffer
403 * queue from being modified while we traverse it.
408 * Return all queued I/O with ENXIO.
409 * XXX Handle any transactions queued to the card
410 * with XPT_ABORT_CCB.
412 while ((q_bp = bufq_first(&softc->buf_queue)) != NULL){
413 bufq_remove(&softc->buf_queue, q_bp);
414 q_bp->b_resid = q_bp->b_bcount;
415 q_bp->b_error = ENXIO;
416 q_bp->b_flags |= B_ERROR;
422 xpt_print_path(periph->path);
423 printf("lost device\n");
427 ptdtor(struct cam_periph *periph)
429 struct pt_softc *softc;
431 softc = (struct pt_softc *)periph->softc;
433 devstat_remove_entry(&softc->device_stats);
435 cam_extend_release(ptperiphs, periph->unit_number);
436 xpt_print_path(periph->path);
437 printf("removing device entry\n");
438 cdevsw_remove(&pt_cdevsw, -1, periph->unit_number);
439 free(softc, M_DEVBUF);
443 ptasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
445 struct cam_periph *periph;
447 periph = (struct cam_periph *)callback_arg;
449 case AC_FOUND_DEVICE:
451 struct ccb_getdev *cgd;
454 cgd = (struct ccb_getdev *)arg;
456 if (SID_TYPE(&cgd->inq_data) != T_PROCESSOR)
460 * Allocate a peripheral instance for
461 * this device and start the probe
464 status = cam_periph_alloc(ptctor, ptoninvalidate, ptdtor,
465 ptstart, "pt", CAM_PERIPH_BIO,
466 cgd->ccb_h.path, ptasync,
467 AC_FOUND_DEVICE, cgd);
469 if (status != CAM_REQ_CMP
470 && status != CAM_REQ_INPROG)
471 printf("ptasync: Unable to attach to new device "
472 "due to status 0x%x\n", status);
478 struct pt_softc *softc;
479 struct ccb_hdr *ccbh;
482 softc = (struct pt_softc *)periph->softc;
485 * Don't fail on the expected unit attention
488 softc->flags |= PT_FLAG_RETRY_UA;
489 for (ccbh = LIST_FIRST(&softc->pending_ccbs);
490 ccbh != NULL; ccbh = LIST_NEXT(ccbh, periph_links.le))
491 ccbh->ccb_state |= PT_CCB_RETRY_UA;
496 cam_periph_async(periph, code, path, arg);
502 ptstart(struct cam_periph *periph, union ccb *start_ccb)
504 struct pt_softc *softc;
508 softc = (struct pt_softc *)periph->softc;
511 * See if there is a buf with work for us to do..
514 bp = bufq_first(&softc->buf_queue);
515 if (periph->immediate_priority <= periph->pinfo.priority) {
516 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
517 ("queuing for immediate ccb\n"));
518 start_ccb->ccb_h.ccb_state = PT_CCB_WAITING;
519 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
521 periph->immediate_priority = CAM_PRIORITY_NONE;
523 wakeup(&periph->ccb_list);
524 } else if (bp == NULL) {
526 xpt_release_ccb(start_ccb);
530 bufq_remove(&softc->buf_queue, bp);
532 devstat_start_transaction(&softc->device_stats);
534 scsi_send_receive(&start_ccb->csio,
538 bp->b_flags & B_READ,
542 /*sense_len*/SSD_FULL_SIZE,
543 /*timeout*/softc->io_timeout);
545 start_ccb->ccb_h.ccb_state = PT_CCB_BUFFER_IO;
548 * Block out any asyncronous callbacks
549 * while we touch the pending ccb list.
552 LIST_INSERT_HEAD(&softc->pending_ccbs, &start_ccb->ccb_h,
556 start_ccb->ccb_h.ccb_bp = bp;
557 bp = bufq_first(&softc->buf_queue);
560 xpt_action(start_ccb);
563 /* Have more work to do, so ensure we stay scheduled */
564 xpt_schedule(periph, /* XXX priority */1);
570 ptdone(struct cam_periph *periph, union ccb *done_ccb)
572 struct pt_softc *softc;
573 struct ccb_scsiio *csio;
575 softc = (struct pt_softc *)periph->softc;
576 csio = &done_ccb->csio;
577 switch (csio->ccb_h.ccb_state) {
578 case PT_CCB_BUFFER_IO:
579 case PT_CCB_BUFFER_IO_UA:
584 bp = (struct buf *)done_ccb->ccb_h.ccb_bp;
585 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
590 if ((csio->ccb_h.ccb_state & PT_CCB_RETRY_UA) != 0)
595 sf |= SF_RETRY_SELTO;
597 if ((error = pterror(done_ccb, 0, sf)) == ERESTART) {
599 * A retry was scheuled, so
609 if (error == ENXIO) {
611 * Catastrophic error. Mark our device
614 xpt_print_path(periph->path);
615 printf("Invalidating device\n");
616 softc->flags |= PT_FLAG_DEVICE_INVALID;
620 * return all queued I/O with EIO, so that
621 * the client can retry these I/Os in the
622 * proper order should it attempt to recover.
624 while ((q_bp = bufq_first(&softc->buf_queue))
626 bufq_remove(&softc->buf_queue, q_bp);
627 q_bp->b_resid = q_bp->b_bcount;
629 q_bp->b_flags |= B_ERROR;
634 bp->b_resid = bp->b_bcount;
635 bp->b_flags |= B_ERROR;
637 bp->b_resid = csio->resid;
639 if (bp->b_resid != 0) {
640 /* Short transfer ??? */
641 bp->b_flags |= B_ERROR;
644 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
645 cam_release_devq(done_ccb->ccb_h.path,
651 bp->b_resid = csio->resid;
652 if (bp->b_resid != 0)
653 bp->b_flags |= B_ERROR;
657 * Block out any asyncronous callbacks
658 * while we touch the pending ccb list.
661 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
664 devstat_end_transaction_buf(&softc->device_stats, bp);
669 /* Caller will release the CCB */
670 wakeup(&done_ccb->ccb_h.cbfcnp);
673 xpt_release_ccb(done_ccb);
677 pterror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
679 struct pt_softc *softc;
680 struct cam_periph *periph;
682 periph = xpt_path_periph(ccb->ccb_h.path);
683 softc = (struct pt_softc *)periph->softc;
685 return(cam_periph_error(ccb, cam_flags, sense_flags,
690 ptioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
692 struct cam_periph *periph;
693 struct pt_softc *softc;
698 periph = cam_extend_get(ptperiphs, unit);
703 softc = (struct pt_softc *)periph->softc;
705 if ((error = cam_periph_lock(periph, PCATCH)) != 0) {
706 return (error); /* error code from tsleep */
710 case PTIOCGETTIMEOUT:
711 if (softc->io_timeout >= 1000)
712 *(int *)addr = softc->io_timeout / 1000;
716 case PTIOCSETTIMEOUT:
720 if (*(int *)addr < 1) {
726 softc->io_timeout = *(int *)addr * 1000;
732 error = cam_periph_ioctl(periph, cmd, addr, pterror);
736 cam_periph_unlock(periph);
742 scsi_send_receive(struct ccb_scsiio *csio, u_int32_t retries,
743 void (*cbfcnp)(struct cam_periph *, union ccb *),
744 u_int tag_action, int readop, u_int byte2,
745 u_int32_t xfer_len, u_int8_t *data_ptr, u_int8_t sense_len,
748 struct scsi_send_receive *scsi_cmd;
750 scsi_cmd = (struct scsi_send_receive *)&csio->cdb_io.cdb_bytes;
751 scsi_cmd->opcode = readop ? RECEIVE : SEND;
752 scsi_cmd->byte2 = byte2;
753 scsi_ulto3b(xfer_len, scsi_cmd->xfer_len);
754 scsi_cmd->control = 0;
759 /*flags*/readop ? CAM_DIR_IN : CAM_DIR_OUT,