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.12 2005/06/02 20:40:31 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>
43 #include <sys/thread2.h>
46 #include "../cam_ccb.h"
47 #include "../cam_extend.h"
48 #include "../cam_periph.h"
49 #include "../cam_xpt_periph.h"
50 #include "../cam_debug.h"
53 #include "scsi_message.h"
66 PT_FLAG_DEVICE_INVALID = 0x02,
67 PT_FLAG_RETRY_UA = 0x04
71 PT_CCB_BUFFER_IO = 0x01,
72 PT_CCB_WAITING = 0x02,
73 PT_CCB_RETRY_UA = 0x04,
74 PT_CCB_BUFFER_IO_UA = PT_CCB_BUFFER_IO|PT_CCB_RETRY_UA
77 /* Offsets into our private area for storing information */
78 #define ccb_state ppriv_field0
79 #define ccb_bp ppriv_ptr1
82 struct buf_queue_head buf_queue;
83 struct devstat device_stats;
84 LIST_HEAD(, ccb_hdr) pending_ccbs;
92 static d_open_t ptopen;
93 static d_close_t ptclose;
94 static d_strategy_t ptstrategy;
95 static periph_init_t ptinit;
96 static void ptasync(void *callback_arg, u_int32_t code,
97 struct cam_path *path, void *arg);
98 static periph_ctor_t ptctor;
99 static periph_oninv_t ptoninvalidate;
100 static periph_dtor_t ptdtor;
101 static periph_start_t ptstart;
102 static void ptdone(struct cam_periph *periph,
103 union ccb *done_ccb);
104 static d_ioctl_t ptioctl;
105 static int pterror(union ccb *ccb, u_int32_t cam_flags,
106 u_int32_t sense_flags);
108 void scsi_send_receive(struct ccb_scsiio *csio, u_int32_t retries,
109 void (*cbfcnp)(struct cam_periph *, union ccb *),
110 u_int tag_action, int readop, u_int byte2,
111 u_int32_t xfer_len, u_int8_t *data_ptr,
112 u_int8_t sense_len, u_int32_t timeout);
114 static struct periph_driver ptdriver =
117 TAILQ_HEAD_INITIALIZER(ptdriver.units), /* generation */ 0
120 DATA_SET(periphdriver_set, ptdriver);
122 #define PT_CDEV_MAJOR 61
124 static struct cdevsw pt_cdevsw = {
126 /* maj */ PT_CDEV_MAJOR,
134 /* write */ physwrite,
138 /* strategy */ ptstrategy,
143 static struct extend_array *ptperiphs;
145 #ifndef SCSI_PT_DEFAULT_TIMEOUT
146 #define SCSI_PT_DEFAULT_TIMEOUT 60
150 ptopen(dev_t dev, int flags, int fmt, struct thread *td)
152 struct cam_periph *periph;
153 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;
228 unit = minor(bp->b_dev);
229 periph = cam_extend_get(ptperiphs, unit);
230 if (periph == NULL) {
234 softc = (struct pt_softc *)periph->softc;
237 * Mask interrupts so that the pack cannot be invalidated until
238 * after we are in the queue. Otherwise, we might not properly
239 * clean up one of the buffers.
244 * If the device has been made invalid, error out
246 if ((softc->flags & PT_FLAG_DEVICE_INVALID)) {
253 * Place it in the queue of disk activities for this disk
255 bufq_insert_tail(&softc->buf_queue, bp);
260 * Schedule ourselves for performing the work.
262 xpt_schedule(periph, /* XXX priority */1);
266 bp->b_flags |= B_ERROR;
269 * Correctly set the buf to indicate a completed xfer
271 bp->b_resid = bp->b_bcount;
279 struct cam_path *path;
282 * Create our extend array for storing the devices we attach to.
284 ptperiphs = cam_extend_new();
285 if (ptperiphs == NULL) {
286 printf("pt: Failed to alloc extend array!\n");
291 * Install a global async callback. This callback will
292 * receive async callbacks like "new device found".
294 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
295 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
297 if (status == CAM_REQ_CMP) {
298 struct ccb_setasync csa;
300 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
301 csa.ccb_h.func_code = XPT_SASYNC_CB;
302 csa.event_enable = AC_FOUND_DEVICE;
303 csa.callback = ptasync;
304 csa.callback_arg = NULL;
305 xpt_action((union ccb *)&csa);
306 status = csa.ccb_h.status;
310 if (status != CAM_REQ_CMP) {
311 printf("pt: Failed to attach master async callback "
312 "due to status 0x%x!\n", status);
317 ptctor(struct cam_periph *periph, void *arg)
319 struct pt_softc *softc;
320 struct ccb_setasync csa;
321 struct ccb_getdev *cgd;
323 cgd = (struct ccb_getdev *)arg;
324 if (periph == NULL) {
325 printf("ptregister: periph was NULL!!\n");
326 return(CAM_REQ_CMP_ERR);
330 printf("ptregister: no getdev CCB, can't register device\n");
331 return(CAM_REQ_CMP_ERR);
334 softc = malloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO);
335 LIST_INIT(&softc->pending_ccbs);
336 softc->state = PT_STATE_NORMAL;
337 bufq_init(&softc->buf_queue);
339 softc->io_timeout = SCSI_PT_DEFAULT_TIMEOUT * 1000;
341 periph->softc = softc;
343 cam_extend_set(ptperiphs, periph->unit_number, periph);
345 devstat_add_entry(&softc->device_stats, "pt",
346 periph->unit_number, 0,
347 DEVSTAT_NO_BLOCKSIZE,
348 SID_TYPE(&cgd->inq_data) | DEVSTAT_TYPE_IF_SCSI,
349 DEVSTAT_PRIORITY_OTHER);
351 cdevsw_add(&pt_cdevsw, -1, periph->unit_number);
352 make_dev(&pt_cdevsw, periph->unit_number, UID_ROOT,
353 GID_OPERATOR, 0600, "%s%d", periph->periph_name,
354 periph->unit_number);
356 * Add async callbacks for bus reset and
357 * bus device reset calls. I don't bother
358 * checking if this fails as, in most cases,
359 * the system will function just fine without
360 * them and the only alternative would be to
361 * not attach the device on failure.
363 xpt_setup_ccb(&csa.ccb_h, periph->path, /*priority*/5);
364 csa.ccb_h.func_code = XPT_SASYNC_CB;
365 csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE;
366 csa.callback = ptasync;
367 csa.callback_arg = periph;
368 xpt_action((union ccb *)&csa);
370 /* Tell the user we've attached to the device */
371 xpt_announce_periph(periph, NULL);
377 ptoninvalidate(struct cam_periph *periph)
379 struct pt_softc *softc;
381 struct ccb_setasync csa;
383 softc = (struct pt_softc *)periph->softc;
386 * De-register any async callbacks.
388 xpt_setup_ccb(&csa.ccb_h, periph->path,
390 csa.ccb_h.func_code = XPT_SASYNC_CB;
391 csa.event_enable = 0;
392 csa.callback = ptasync;
393 csa.callback_arg = periph;
394 xpt_action((union ccb *)&csa);
396 softc->flags |= PT_FLAG_DEVICE_INVALID;
399 * We need to be in a critical section here to keep the buffer
400 * queue from being modified while we traverse it.
405 * Return all queued I/O with ENXIO.
406 * XXX Handle any transactions queued to the card
407 * with XPT_ABORT_CCB.
409 while ((q_bp = bufq_first(&softc->buf_queue)) != NULL){
410 bufq_remove(&softc->buf_queue, q_bp);
411 q_bp->b_resid = q_bp->b_bcount;
412 q_bp->b_error = ENXIO;
413 q_bp->b_flags |= B_ERROR;
419 xpt_print_path(periph->path);
420 printf("lost device\n");
424 ptdtor(struct cam_periph *periph)
426 struct pt_softc *softc;
428 softc = (struct pt_softc *)periph->softc;
430 devstat_remove_entry(&softc->device_stats);
432 cam_extend_release(ptperiphs, periph->unit_number);
433 xpt_print_path(periph->path);
434 printf("removing device entry\n");
435 cdevsw_remove(&pt_cdevsw, -1, periph->unit_number);
436 free(softc, M_DEVBUF);
440 ptasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
442 struct cam_periph *periph;
444 periph = (struct cam_periph *)callback_arg;
446 case AC_FOUND_DEVICE:
448 struct ccb_getdev *cgd;
451 cgd = (struct ccb_getdev *)arg;
453 if (SID_TYPE(&cgd->inq_data) != T_PROCESSOR)
457 * Allocate a peripheral instance for
458 * this device and start the probe
461 status = cam_periph_alloc(ptctor, ptoninvalidate, ptdtor,
462 ptstart, "pt", CAM_PERIPH_BIO,
463 cgd->ccb_h.path, ptasync,
464 AC_FOUND_DEVICE, cgd);
466 if (status != CAM_REQ_CMP
467 && status != CAM_REQ_INPROG)
468 printf("ptasync: Unable to attach to new device "
469 "due to status 0x%x\n", status);
475 struct pt_softc *softc;
476 struct ccb_hdr *ccbh;
478 softc = (struct pt_softc *)periph->softc;
481 * Don't fail on the expected unit attention
484 softc->flags |= PT_FLAG_RETRY_UA;
485 for (ccbh = LIST_FIRST(&softc->pending_ccbs);
486 ccbh != NULL; ccbh = LIST_NEXT(ccbh, periph_links.le))
487 ccbh->ccb_state |= PT_CCB_RETRY_UA;
492 cam_periph_async(periph, code, path, arg);
498 ptstart(struct cam_periph *periph, union ccb *start_ccb)
500 struct pt_softc *softc;
503 softc = (struct pt_softc *)periph->softc;
506 * See if there is a buf with work for us to do..
509 bp = bufq_first(&softc->buf_queue);
510 if (periph->immediate_priority <= periph->pinfo.priority) {
511 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
512 ("queuing for immediate ccb\n"));
513 start_ccb->ccb_h.ccb_state = PT_CCB_WAITING;
514 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
516 periph->immediate_priority = CAM_PRIORITY_NONE;
518 wakeup(&periph->ccb_list);
519 } else if (bp == NULL) {
521 xpt_release_ccb(start_ccb);
523 bufq_remove(&softc->buf_queue, bp);
525 devstat_start_transaction(&softc->device_stats);
527 scsi_send_receive(&start_ccb->csio,
531 bp->b_flags & B_READ,
535 /*sense_len*/SSD_FULL_SIZE,
536 /*timeout*/softc->io_timeout);
538 start_ccb->ccb_h.ccb_state = PT_CCB_BUFFER_IO;
541 * Block out any asyncronous callbacks
542 * while we touch the pending ccb list.
544 LIST_INSERT_HEAD(&softc->pending_ccbs, &start_ccb->ccb_h,
547 start_ccb->ccb_h.ccb_bp = bp;
548 bp = bufq_first(&softc->buf_queue);
551 xpt_action(start_ccb);
554 /* Have more work to do, so ensure we stay scheduled */
555 xpt_schedule(periph, /* XXX priority */1);
561 ptdone(struct cam_periph *periph, union ccb *done_ccb)
563 struct pt_softc *softc;
564 struct ccb_scsiio *csio;
566 softc = (struct pt_softc *)periph->softc;
567 csio = &done_ccb->csio;
568 switch (csio->ccb_h.ccb_state) {
569 case PT_CCB_BUFFER_IO:
570 case PT_CCB_BUFFER_IO_UA:
574 bp = (struct buf *)done_ccb->ccb_h.ccb_bp;
575 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
579 if ((csio->ccb_h.ccb_state & PT_CCB_RETRY_UA) != 0)
584 sf |= SF_RETRY_SELTO;
586 if ((error = pterror(done_ccb, 0, sf)) == ERESTART) {
588 * A retry was scheuled, so
598 if (error == ENXIO) {
600 * Catastrophic error. Mark our device
603 xpt_print_path(periph->path);
604 printf("Invalidating device\n");
605 softc->flags |= PT_FLAG_DEVICE_INVALID;
609 * return all queued I/O with EIO, so that
610 * the client can retry these I/Os in the
611 * proper order should it attempt to recover.
613 while ((q_bp = bufq_first(&softc->buf_queue))
615 bufq_remove(&softc->buf_queue, q_bp);
616 q_bp->b_resid = q_bp->b_bcount;
618 q_bp->b_flags |= B_ERROR;
623 bp->b_resid = bp->b_bcount;
624 bp->b_flags |= B_ERROR;
626 bp->b_resid = csio->resid;
628 if (bp->b_resid != 0) {
629 /* Short transfer ??? */
630 bp->b_flags |= B_ERROR;
633 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
634 cam_release_devq(done_ccb->ccb_h.path,
640 bp->b_resid = csio->resid;
641 if (bp->b_resid != 0)
642 bp->b_flags |= B_ERROR;
646 * Block out any asyncronous callbacks
647 * while we touch the pending ccb list.
650 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
653 devstat_end_transaction_buf(&softc->device_stats, bp);
658 /* Caller will release the CCB */
659 wakeup(&done_ccb->ccb_h.cbfcnp);
662 xpt_release_ccb(done_ccb);
666 pterror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
668 struct pt_softc *softc;
669 struct cam_periph *periph;
671 periph = xpt_path_periph(ccb->ccb_h.path);
672 softc = (struct pt_softc *)periph->softc;
674 return(cam_periph_error(ccb, cam_flags, sense_flags,
679 ptioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
681 struct cam_periph *periph;
682 struct pt_softc *softc;
687 periph = cam_extend_get(ptperiphs, unit);
692 softc = (struct pt_softc *)periph->softc;
694 if ((error = cam_periph_lock(periph, PCATCH)) != 0) {
695 return (error); /* error code from tsleep */
699 case PTIOCGETTIMEOUT:
700 if (softc->io_timeout >= 1000)
701 *(int *)addr = softc->io_timeout / 1000;
705 case PTIOCSETTIMEOUT:
707 if (*(int *)addr < 1) {
713 softc->io_timeout = *(int *)addr * 1000;
719 error = cam_periph_ioctl(periph, cmd, addr, pterror);
723 cam_periph_unlock(periph);
729 scsi_send_receive(struct ccb_scsiio *csio, u_int32_t retries,
730 void (*cbfcnp)(struct cam_periph *, union ccb *),
731 u_int tag_action, int readop, u_int byte2,
732 u_int32_t xfer_len, u_int8_t *data_ptr, u_int8_t sense_len,
735 struct scsi_send_receive *scsi_cmd;
737 scsi_cmd = (struct scsi_send_receive *)&csio->cdb_io.cdb_bytes;
738 scsi_cmd->opcode = readop ? RECEIVE : SEND;
739 scsi_cmd->byte2 = byte2;
740 scsi_ulto3b(xfer_len, scsi_cmd->xfer_len);
741 scsi_cmd->control = 0;
746 /*flags*/readop ? CAM_DIR_IN : CAM_DIR_OUT,