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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
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>
44 #include <cam/cam_ccb.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 bio_queue_head bio_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 PERIPHDRIVER_DECLARE(pt, ptdriver);
120 static struct cdevsw pt_cdevsw = {
121 .d_version = D_VERSION,
122 .d_flags = D_NEEDGIANT,
126 .d_write = physwrite,
128 .d_strategy = ptstrategy,
132 #ifndef SCSI_PT_DEFAULT_TIMEOUT
133 #define SCSI_PT_DEFAULT_TIMEOUT 60
137 ptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
139 struct cam_periph *periph;
140 struct pt_softc *softc;
146 periph = (struct cam_periph *)dev->si_drv1;
150 softc = (struct pt_softc *)periph->softc;
153 if (softc->flags & PT_FLAG_DEVICE_INVALID) {
158 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
159 ("ptopen: dev=%s (unit %d)\n", devtoname(dev), unit));
161 if ((error = cam_periph_lock(periph, PRIBIO|PCATCH)) != 0) {
163 return (error); /* error code from tsleep */
168 if ((softc->flags & PT_FLAG_OPEN) == 0) {
169 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
172 softc->flags |= PT_FLAG_OPEN;
176 cam_periph_unlock(periph);
181 ptclose(struct cdev *dev, int flag, int fmt, struct thread *td)
183 struct cam_periph *periph;
184 struct pt_softc *softc;
187 periph = (struct cam_periph *)dev->si_drv1;
191 softc = (struct pt_softc *)periph->softc;
193 if ((error = cam_periph_lock(periph, PRIBIO)) != 0)
194 return (error); /* error code from tsleep */
196 softc->flags &= ~PT_FLAG_OPEN;
197 cam_periph_unlock(periph);
198 cam_periph_release(periph);
203 * Actually translate the requested transfer into one the physical driver
204 * can understand. The transfer is described by a buf and will include
205 * only one physical transfer.
208 ptstrategy(struct bio *bp)
210 struct cam_periph *periph;
211 struct pt_softc *softc;
214 periph = (struct cam_periph *)bp->bio_dev->si_drv1;
215 bp->bio_resid = bp->bio_bcount;
216 if (periph == NULL) {
217 biofinish(bp, NULL, ENXIO);
220 softc = (struct pt_softc *)periph->softc;
223 * Mask interrupts so that the pack cannot be invalidated until
224 * after we are in the queue. Otherwise, we might not properly
225 * clean up one of the buffers.
230 * If the device has been made invalid, error out
232 if ((softc->flags & PT_FLAG_DEVICE_INVALID)) {
234 biofinish(bp, NULL, ENXIO);
239 * Place it in the queue of disk activities for this disk
241 bioq_insert_tail(&softc->bio_queue, bp);
246 * Schedule ourselves for performing the work.
248 xpt_schedule(periph, /* XXX priority */1);
257 struct cam_path *path;
260 * Install a global async callback. This callback will
261 * receive async callbacks like "new device found".
263 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
264 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
266 if (status == CAM_REQ_CMP) {
267 struct ccb_setasync csa;
269 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
270 csa.ccb_h.func_code = XPT_SASYNC_CB;
271 csa.event_enable = AC_FOUND_DEVICE;
272 csa.callback = ptasync;
273 csa.callback_arg = NULL;
274 xpt_action((union ccb *)&csa);
275 status = csa.ccb_h.status;
279 if (status != CAM_REQ_CMP) {
280 printf("pt: Failed to attach master async callback "
281 "due to status 0x%x!\n", status);
286 ptctor(struct cam_periph *periph, void *arg)
288 struct pt_softc *softc;
289 struct ccb_setasync csa;
290 struct ccb_getdev *cgd;
292 cgd = (struct ccb_getdev *)arg;
293 if (periph == NULL) {
294 printf("ptregister: periph was NULL!!\n");
295 return(CAM_REQ_CMP_ERR);
299 printf("ptregister: no getdev CCB, can't register device\n");
300 return(CAM_REQ_CMP_ERR);
303 softc = (struct pt_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT);
306 printf("daregister: Unable to probe new device. "
307 "Unable to allocate softc\n");
308 return(CAM_REQ_CMP_ERR);
311 bzero(softc, sizeof(*softc));
312 LIST_INIT(&softc->pending_ccbs);
313 softc->state = PT_STATE_NORMAL;
314 bioq_init(&softc->bio_queue);
316 softc->io_timeout = SCSI_PT_DEFAULT_TIMEOUT * 1000;
318 periph->softc = softc;
320 softc->device_stats = devstat_new_entry("pt",
321 periph->unit_number, 0,
322 DEVSTAT_NO_BLOCKSIZE,
323 SID_TYPE(&cgd->inq_data) | DEVSTAT_TYPE_IF_SCSI,
324 DEVSTAT_PRIORITY_OTHER);
326 softc->dev = make_dev(&pt_cdevsw, periph->unit_number, UID_ROOT,
327 GID_OPERATOR, 0600, "%s%d", periph->periph_name,
328 periph->unit_number);
329 softc->dev->si_drv1 = periph;
332 * Add async callbacks for bus reset and
333 * bus device reset calls. I don't bother
334 * checking if this fails as, in most cases,
335 * the system will function just fine without
336 * them and the only alternative would be to
337 * not attach the device on failure.
339 xpt_setup_ccb(&csa.ccb_h, periph->path, /*priority*/5);
340 csa.ccb_h.func_code = XPT_SASYNC_CB;
341 csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE;
342 csa.callback = ptasync;
343 csa.callback_arg = periph;
344 xpt_action((union ccb *)&csa);
346 /* Tell the user we've attached to the device */
347 xpt_announce_periph(periph, NULL);
353 ptoninvalidate(struct cam_periph *periph)
356 struct pt_softc *softc;
357 struct ccb_setasync csa;
359 softc = (struct pt_softc *)periph->softc;
362 * De-register any async callbacks.
364 xpt_setup_ccb(&csa.ccb_h, periph->path,
366 csa.ccb_h.func_code = XPT_SASYNC_CB;
367 csa.event_enable = 0;
368 csa.callback = ptasync;
369 csa.callback_arg = periph;
370 xpt_action((union ccb *)&csa);
372 softc->flags |= PT_FLAG_DEVICE_INVALID;
375 * Although the oninvalidate() routines are always called at
376 * splsoftcam, we need to be at splbio() here to keep the buffer
377 * queue from being modified while we traverse it.
382 * Return all queued I/O with ENXIO.
383 * XXX Handle any transactions queued to the card
384 * with XPT_ABORT_CCB.
386 bioq_flush(&softc->bio_queue, NULL, ENXIO);
390 xpt_print_path(periph->path);
391 printf("lost device\n");
395 ptdtor(struct cam_periph *periph)
397 struct pt_softc *softc;
399 softc = (struct pt_softc *)periph->softc;
401 devstat_remove_entry(softc->device_stats);
403 destroy_dev(softc->dev);
405 xpt_print_path(periph->path);
406 printf("removing device entry\n");
407 free(softc, M_DEVBUF);
411 ptasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
413 struct cam_periph *periph;
415 periph = (struct cam_periph *)callback_arg;
417 case AC_FOUND_DEVICE:
419 struct ccb_getdev *cgd;
422 cgd = (struct ccb_getdev *)arg;
426 if (SID_TYPE(&cgd->inq_data) != T_PROCESSOR)
430 * Allocate a peripheral instance for
431 * this device and start the probe
434 status = cam_periph_alloc(ptctor, ptoninvalidate, ptdtor,
435 ptstart, "pt", CAM_PERIPH_BIO,
436 cgd->ccb_h.path, ptasync,
437 AC_FOUND_DEVICE, cgd);
439 if (status != CAM_REQ_CMP
440 && status != CAM_REQ_INPROG)
441 printf("ptasync: Unable to attach to new device "
442 "due to status 0x%x\n", status);
448 struct pt_softc *softc;
449 struct ccb_hdr *ccbh;
452 softc = (struct pt_softc *)periph->softc;
455 * Don't fail on the expected unit attention
458 softc->flags |= PT_FLAG_RETRY_UA;
459 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
460 ccbh->ccb_state |= PT_CCB_RETRY_UA;
465 cam_periph_async(periph, code, path, arg);
471 ptstart(struct cam_periph *periph, union ccb *start_ccb)
473 struct pt_softc *softc;
477 softc = (struct pt_softc *)periph->softc;
480 * See if there is a buf with work for us to do..
483 bp = bioq_first(&softc->bio_queue);
484 if (periph->immediate_priority <= periph->pinfo.priority) {
485 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
486 ("queuing for immediate ccb\n"));
487 start_ccb->ccb_h.ccb_state = PT_CCB_WAITING;
488 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
490 periph->immediate_priority = CAM_PRIORITY_NONE;
492 wakeup(&periph->ccb_list);
493 } else if (bp == NULL) {
495 xpt_release_ccb(start_ccb);
499 bioq_remove(&softc->bio_queue, bp);
501 devstat_start_transaction_bio(softc->device_stats, bp);
503 scsi_send_receive(&start_ccb->csio,
507 bp->bio_cmd == BIO_READ,
511 /*sense_len*/SSD_FULL_SIZE,
512 /*timeout*/softc->io_timeout);
514 start_ccb->ccb_h.ccb_state = PT_CCB_BUFFER_IO_UA;
517 * Block out any asyncronous callbacks
518 * while we touch the pending ccb list.
521 LIST_INSERT_HEAD(&softc->pending_ccbs, &start_ccb->ccb_h,
525 start_ccb->ccb_h.ccb_bp = bp;
526 bp = bioq_first(&softc->bio_queue);
529 xpt_action(start_ccb);
532 /* Have more work to do, so ensure we stay scheduled */
533 xpt_schedule(periph, /* XXX priority */1);
539 ptdone(struct cam_periph *periph, union ccb *done_ccb)
541 struct pt_softc *softc;
542 struct ccb_scsiio *csio;
544 softc = (struct pt_softc *)periph->softc;
545 csio = &done_ccb->csio;
546 switch (csio->ccb_h.ccb_state) {
547 case PT_CCB_BUFFER_IO:
548 case PT_CCB_BUFFER_IO_UA:
553 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
554 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
559 if ((csio->ccb_h.ccb_state & PT_CCB_RETRY_UA) != 0)
564 error = pterror(done_ccb, CAM_RETRY_SELTO, sf);
565 if (error == ERESTART) {
567 * A retry was scheuled, so
575 if (error == ENXIO) {
577 * Catastrophic error. Mark our device
580 xpt_print_path(periph->path);
581 printf("Invalidating device\n");
582 softc->flags |= PT_FLAG_DEVICE_INVALID;
586 * return all queued I/O with EIO, so that
587 * the client can retry these I/Os in the
588 * proper order should it attempt to recover.
590 bioq_flush(&softc->bio_queue, NULL, EIO);
592 bp->bio_error = error;
593 bp->bio_resid = bp->bio_bcount;
594 bp->bio_flags |= BIO_ERROR;
596 bp->bio_resid = csio->resid;
598 if (bp->bio_resid != 0) {
599 /* Short transfer ??? */
600 bp->bio_flags |= BIO_ERROR;
603 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
604 cam_release_devq(done_ccb->ccb_h.path,
610 bp->bio_resid = csio->resid;
611 if (bp->bio_resid != 0)
612 bp->bio_flags |= BIO_ERROR;
616 * Block out any asyncronous callbacks
617 * while we touch the pending ccb list.
620 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
623 biofinish(bp, softc->device_stats, 0);
627 /* Caller will release the CCB */
628 wakeup(&done_ccb->ccb_h.cbfcnp);
631 xpt_release_ccb(done_ccb);
635 pterror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
637 struct pt_softc *softc;
638 struct cam_periph *periph;
640 periph = xpt_path_periph(ccb->ccb_h.path);
641 softc = (struct pt_softc *)periph->softc;
643 return(cam_periph_error(ccb, cam_flags, sense_flags,
648 ptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
650 struct cam_periph *periph;
651 struct pt_softc *softc;
654 periph = (struct cam_periph *)dev->si_drv1;
658 softc = (struct pt_softc *)periph->softc;
660 if ((error = cam_periph_lock(periph, PRIBIO|PCATCH)) != 0) {
661 return (error); /* error code from tsleep */
665 case PTIOCGETTIMEOUT:
666 if (softc->io_timeout >= 1000)
667 *(int *)addr = softc->io_timeout / 1000;
671 case PTIOCSETTIMEOUT:
675 if (*(int *)addr < 1) {
681 softc->io_timeout = *(int *)addr * 1000;
687 error = cam_periph_ioctl(periph, cmd, addr, pterror);
691 cam_periph_unlock(periph);
697 scsi_send_receive(struct ccb_scsiio *csio, u_int32_t retries,
698 void (*cbfcnp)(struct cam_periph *, union ccb *),
699 u_int tag_action, int readop, u_int byte2,
700 u_int32_t xfer_len, u_int8_t *data_ptr, u_int8_t sense_len,
703 struct scsi_send_receive *scsi_cmd;
705 scsi_cmd = (struct scsi_send_receive *)&csio->cdb_io.cdb_bytes;
706 scsi_cmd->opcode = readop ? RECEIVE : SEND;
707 scsi_cmd->byte2 = byte2;
708 scsi_ulto3b(xfer_len, scsi_cmd->xfer_len);
709 scsi_cmd->control = 0;
714 /*flags*/readop ? CAM_DIR_IN : CAM_DIR_OUT,