1139743Simp/*- 239213Sgibbs * Implementation of SCSI Processor Target Peripheral driver for CAM. 339213Sgibbs * 439213Sgibbs * Copyright (c) 1998 Justin T. Gibbs. 539213Sgibbs * All rights reserved. 639213Sgibbs * 739213Sgibbs * Redistribution and use in source and binary forms, with or without 839213Sgibbs * modification, are permitted provided that the following conditions 939213Sgibbs * are met: 1039213Sgibbs * 1. Redistributions of source code must retain the above copyright 1139213Sgibbs * notice, this list of conditions, and the following disclaimer, 1239213Sgibbs * without modification, immediately at the beginning of the file. 1339213Sgibbs * 2. The name of the author may not be used to endorse or promote products 1439213Sgibbs * derived from this software without specific prior written permission. 1539213Sgibbs * 1639213Sgibbs * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1739213Sgibbs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1839213Sgibbs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1939213Sgibbs * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 2039213Sgibbs * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2139213Sgibbs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2239213Sgibbs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2339213Sgibbs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2439213Sgibbs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2539213Sgibbs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2639213Sgibbs * SUCH DAMAGE. 2739213Sgibbs */ 2839213Sgibbs 29116162Sobrien#include <sys/cdefs.h> 30116162Sobrien__FBSDID("$FreeBSD: stable/11/sys/cam/scsi/scsi_pt.c 350804 2019-08-08 22:16:19Z mav $"); 31116162Sobrien 3239213Sgibbs#include <sys/param.h> 3339213Sgibbs#include <sys/queue.h> 3439213Sgibbs#include <sys/systm.h> 3539213Sgibbs#include <sys/kernel.h> 3639213Sgibbs#include <sys/types.h> 3760041Sphk#include <sys/bio.h> 3839213Sgibbs#include <sys/devicestat.h> 3939213Sgibbs#include <sys/malloc.h> 4039213Sgibbs#include <sys/conf.h> 4150073Sken#include <sys/ptio.h> 4239213Sgibbs 4339213Sgibbs#include <cam/cam.h> 4439213Sgibbs#include <cam/cam_ccb.h> 4539213Sgibbs#include <cam/cam_periph.h> 4639213Sgibbs#include <cam/cam_xpt_periph.h> 4739213Sgibbs#include <cam/cam_debug.h> 4839213Sgibbs 4939213Sgibbs#include <cam/scsi/scsi_all.h> 5039213Sgibbs#include <cam/scsi/scsi_message.h> 5139213Sgibbs#include <cam/scsi/scsi_pt.h> 5239213Sgibbs 5350073Sken#include "opt_pt.h" 5450073Sken 5539213Sgibbstypedef enum { 5639213Sgibbs PT_STATE_PROBE, 5739213Sgibbs PT_STATE_NORMAL 5839213Sgibbs} pt_state; 5939213Sgibbs 6039213Sgibbstypedef enum { 6139213Sgibbs PT_FLAG_NONE = 0x00, 6239213Sgibbs PT_FLAG_OPEN = 0x01, 6339213Sgibbs PT_FLAG_DEVICE_INVALID = 0x02, 6439213Sgibbs PT_FLAG_RETRY_UA = 0x04 6539213Sgibbs} pt_flags; 6639213Sgibbs 6739213Sgibbstypedef enum { 6839213Sgibbs PT_CCB_BUFFER_IO = 0x01, 6939213Sgibbs PT_CCB_RETRY_UA = 0x04, 7039213Sgibbs PT_CCB_BUFFER_IO_UA = PT_CCB_BUFFER_IO|PT_CCB_RETRY_UA 7139213Sgibbs} pt_ccb_state; 7239213Sgibbs 7339213Sgibbs/* Offsets into our private area for storing information */ 7439213Sgibbs#define ccb_state ppriv_field0 7539213Sgibbs#define ccb_bp ppriv_ptr1 7639213Sgibbs 7739213Sgibbsstruct pt_softc { 7859249Sphk struct bio_queue_head bio_queue; 79112006Sphk struct devstat *device_stats; 8060938Sjake LIST_HEAD(, ccb_hdr) pending_ccbs; 8139213Sgibbs pt_state state; 8239213Sgibbs pt_flags flags; 8339213Sgibbs union ccb saved_ccb; 8450073Sken int io_timeout; 85130585Sphk struct cdev *dev; 8639213Sgibbs}; 8739213Sgibbs 8839213Sgibbsstatic d_open_t ptopen; 8939213Sgibbsstatic d_close_t ptclose; 9039213Sgibbsstatic d_strategy_t ptstrategy; 9139213Sgibbsstatic periph_init_t ptinit; 9239213Sgibbsstatic void ptasync(void *callback_arg, u_int32_t code, 9339213Sgibbs struct cam_path *path, void *arg); 9439213Sgibbsstatic periph_ctor_t ptctor; 9540603Skenstatic periph_oninv_t ptoninvalidate; 9639213Sgibbsstatic periph_dtor_t ptdtor; 9739213Sgibbsstatic periph_start_t ptstart; 9839213Sgibbsstatic void ptdone(struct cam_periph *periph, 9939213Sgibbs union ccb *done_ccb); 10050073Skenstatic d_ioctl_t ptioctl; 10139213Sgibbsstatic int pterror(union ccb *ccb, u_int32_t cam_flags, 10239213Sgibbs u_int32_t sense_flags); 10339213Sgibbs 10439213Sgibbsvoid scsi_send_receive(struct ccb_scsiio *csio, u_int32_t retries, 10539213Sgibbs void (*cbfcnp)(struct cam_periph *, union ccb *), 10639213Sgibbs u_int tag_action, int readop, u_int byte2, 10739213Sgibbs u_int32_t xfer_len, u_int8_t *data_ptr, 10839213Sgibbs u_int8_t sense_len, u_int32_t timeout); 10939213Sgibbs 11039213Sgibbsstatic struct periph_driver ptdriver = 11139213Sgibbs{ 11239213Sgibbs ptinit, "pt", 11339213Sgibbs TAILQ_HEAD_INITIALIZER(ptdriver.units), /* generation */ 0 11439213Sgibbs}; 11539213Sgibbs 11672119SpeterPERIPHDRIVER_DECLARE(pt, ptdriver); 11739213Sgibbs 11839213Sgibbs 11947625Sphkstatic struct cdevsw pt_cdevsw = { 120126080Sphk .d_version = D_VERSION, 121168752Sscottl .d_flags = 0, 122111815Sphk .d_open = ptopen, 123111815Sphk .d_close = ptclose, 124111815Sphk .d_read = physread, 125111815Sphk .d_write = physwrite, 126111815Sphk .d_ioctl = ptioctl, 127111815Sphk .d_strategy = ptstrategy, 128111815Sphk .d_name = "pt", 12939213Sgibbs}; 13039213Sgibbs 13150073Sken#ifndef SCSI_PT_DEFAULT_TIMEOUT 13250073Sken#define SCSI_PT_DEFAULT_TIMEOUT 60 13350073Sken#endif 13450073Sken 13539213Sgibbsstatic int 136130585Sphkptopen(struct cdev *dev, int flags, int fmt, struct thread *td) 13739213Sgibbs{ 13839213Sgibbs struct cam_periph *periph; 13939213Sgibbs struct pt_softc *softc; 140168752Sscottl int error = 0; 14139213Sgibbs 142101940Snjl periph = (struct cam_periph *)dev->si_drv1; 143168752Sscottl if (cam_periph_acquire(periph) != CAM_REQ_CMP) 14439213Sgibbs return (ENXIO); 14539213Sgibbs 14639213Sgibbs softc = (struct pt_softc *)periph->softc; 14739213Sgibbs 148168752Sscottl cam_periph_lock(periph); 14940603Sken if (softc->flags & PT_FLAG_DEVICE_INVALID) { 150236138Sken cam_periph_release_locked(periph); 151168752Sscottl cam_periph_unlock(periph); 15240603Sken return(ENXIO); 15340603Sken } 15440603Sken 155168752Sscottl if ((softc->flags & PT_FLAG_OPEN) == 0) 156168752Sscottl softc->flags |= PT_FLAG_OPEN; 157168752Sscottl else { 158168752Sscottl error = EBUSY; 159168752Sscottl cam_periph_release(periph); 16041297Sken } 16139213Sgibbs 162168752Sscottl CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 163168752Sscottl ("ptopen: dev=%s\n", devtoname(dev))); 16441297Sken 16539213Sgibbs cam_periph_unlock(periph); 16639213Sgibbs return (error); 16739213Sgibbs} 16839213Sgibbs 16939213Sgibbsstatic int 170130585Sphkptclose(struct cdev *dev, int flag, int fmt, struct thread *td) 17139213Sgibbs{ 17239213Sgibbs struct cam_periph *periph; 17339213Sgibbs struct pt_softc *softc; 17439213Sgibbs 175101940Snjl periph = (struct cam_periph *)dev->si_drv1; 17639213Sgibbs softc = (struct pt_softc *)periph->softc; 17739213Sgibbs 178168752Sscottl cam_periph_lock(periph); 17939213Sgibbs 18039213Sgibbs softc->flags &= ~PT_FLAG_OPEN; 181236138Sken cam_periph_release_locked(periph); 18239213Sgibbs cam_periph_unlock(periph); 18339213Sgibbs return (0); 18439213Sgibbs} 18539213Sgibbs 18639213Sgibbs/* 18739213Sgibbs * Actually translate the requested transfer into one the physical driver 18839213Sgibbs * can understand. The transfer is described by a buf and will include 18939213Sgibbs * only one physical transfer. 19039213Sgibbs */ 19139213Sgibbsstatic void 19259249Sphkptstrategy(struct bio *bp) 19339213Sgibbs{ 19439213Sgibbs struct cam_periph *periph; 19539213Sgibbs struct pt_softc *softc; 19639213Sgibbs 197101940Snjl periph = (struct cam_periph *)bp->bio_dev->si_drv1; 19876362Sphk bp->bio_resid = bp->bio_bcount; 19939213Sgibbs if (periph == NULL) { 20076362Sphk biofinish(bp, NULL, ENXIO); 20176362Sphk return; 20239213Sgibbs } 203168752Sscottl cam_periph_lock(periph); 20439213Sgibbs softc = (struct pt_softc *)periph->softc; 20539213Sgibbs 20639213Sgibbs /* 20739213Sgibbs * If the device has been made invalid, error out 20839213Sgibbs */ 20939213Sgibbs if ((softc->flags & PT_FLAG_DEVICE_INVALID)) { 210168752Sscottl cam_periph_unlock(periph); 21176362Sphk biofinish(bp, NULL, ENXIO); 21276362Sphk return; 21339213Sgibbs } 21439213Sgibbs 21539213Sgibbs /* 21639213Sgibbs * Place it in the queue of disk activities for this disk 21739213Sgibbs */ 21859249Sphk bioq_insert_tail(&softc->bio_queue, bp); 21939213Sgibbs 22039213Sgibbs /* 22139213Sgibbs * Schedule ourselves for performing the work. 22239213Sgibbs */ 223198382Smav xpt_schedule(periph, CAM_PRIORITY_NORMAL); 224168752Sscottl cam_periph_unlock(periph); 22539213Sgibbs 22639213Sgibbs return; 22739213Sgibbs} 22839213Sgibbs 22939213Sgibbsstatic void 23039213Sgibbsptinit(void) 23139213Sgibbs{ 23239213Sgibbs cam_status status; 23339213Sgibbs 23439213Sgibbs /* 23539213Sgibbs * Install a global async callback. This callback will 23639213Sgibbs * receive async callbacks like "new device found". 23739213Sgibbs */ 238169605Sscottl status = xpt_register_async(AC_FOUND_DEVICE, ptasync, NULL, NULL); 23939213Sgibbs 24039213Sgibbs if (status != CAM_REQ_CMP) { 24139213Sgibbs printf("pt: Failed to attach master async callback " 24239213Sgibbs "due to status 0x%x!\n", status); 24339213Sgibbs } 24439213Sgibbs} 24539213Sgibbs 24639213Sgibbsstatic cam_status 24739213Sgibbsptctor(struct cam_periph *periph, void *arg) 24839213Sgibbs{ 24939213Sgibbs struct pt_softc *softc; 25039213Sgibbs struct ccb_getdev *cgd; 251220644Smav struct ccb_pathinq cpi; 252293350Skib struct make_dev_args args; 253293350Skib int error; 25439213Sgibbs 25539213Sgibbs cgd = (struct ccb_getdev *)arg; 25639213Sgibbs if (cgd == NULL) { 25739213Sgibbs printf("ptregister: no getdev CCB, can't register device\n"); 25839213Sgibbs return(CAM_REQ_CMP_ERR); 25939213Sgibbs } 26039213Sgibbs 26139213Sgibbs softc = (struct pt_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT); 26239213Sgibbs 26339213Sgibbs if (softc == NULL) { 26439213Sgibbs printf("daregister: Unable to probe new device. " 26539213Sgibbs "Unable to allocate softc\n"); 26639213Sgibbs return(CAM_REQ_CMP_ERR); 26739213Sgibbs } 26839213Sgibbs 26939213Sgibbs bzero(softc, sizeof(*softc)); 27039213Sgibbs LIST_INIT(&softc->pending_ccbs); 27139213Sgibbs softc->state = PT_STATE_NORMAL; 27259249Sphk bioq_init(&softc->bio_queue); 27339213Sgibbs 27450073Sken softc->io_timeout = SCSI_PT_DEFAULT_TIMEOUT * 1000; 27550073Sken 27639213Sgibbs periph->softc = softc; 277220644Smav 278350804Smav xpt_path_inq(&cpi, periph->path); 279220644Smav 280169605Sscottl cam_periph_unlock(periph); 281293350Skib 282293350Skib make_dev_args_init(&args); 283293350Skib args.mda_devsw = &pt_cdevsw; 284293350Skib args.mda_unit = periph->unit_number; 285293350Skib args.mda_uid = UID_ROOT; 286293350Skib args.mda_gid = GID_OPERATOR; 287293350Skib args.mda_mode = 0600; 288293350Skib args.mda_si_drv1 = periph; 289293350Skib error = make_dev_s(&args, &softc->dev, "%s%d", periph->periph_name, 290293350Skib periph->unit_number); 291293350Skib if (error != 0) { 292293350Skib cam_periph_lock(periph); 293293350Skib return (CAM_REQ_CMP_ERR); 294293350Skib } 295293350Skib 296112006Sphk softc->device_stats = devstat_new_entry("pt", 29739213Sgibbs periph->unit_number, 0, 29839213Sgibbs DEVSTAT_NO_BLOCKSIZE, 299220644Smav SID_TYPE(&cgd->inq_data) | 300220644Smav XPORT_DEVSTAT_TYPE(cpi.transport), 30143819Sken DEVSTAT_PRIORITY_OTHER); 30239213Sgibbs 303168872Sscottl cam_periph_lock(periph); 304101940Snjl 30539213Sgibbs /* 30639213Sgibbs * Add async callbacks for bus reset and 30739213Sgibbs * bus device reset calls. I don't bother 30839213Sgibbs * checking if this fails as, in most cases, 30939213Sgibbs * the system will function just fine without 31039213Sgibbs * them and the only alternative would be to 31139213Sgibbs * not attach the device on failure. 31239213Sgibbs */ 313169605Sscottl xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE, 314169605Sscottl ptasync, periph, periph->path); 31539213Sgibbs 31639213Sgibbs /* Tell the user we've attached to the device */ 31739213Sgibbs xpt_announce_periph(periph, NULL); 31839213Sgibbs 31939213Sgibbs return(CAM_REQ_CMP); 32039213Sgibbs} 32139213Sgibbs 32239213Sgibbsstatic void 32340603Skenptoninvalidate(struct cam_periph *periph) 32440603Sken{ 32540603Sken struct pt_softc *softc; 32640603Sken 32740603Sken softc = (struct pt_softc *)periph->softc; 32840603Sken 32940603Sken /* 33040603Sken * De-register any async callbacks. 33140603Sken */ 332169605Sscottl xpt_register_async(0, ptasync, periph, periph->path); 33340603Sken 33440603Sken softc->flags |= PT_FLAG_DEVICE_INVALID; 33540603Sken 33640603Sken /* 33740603Sken * Return all queued I/O with ENXIO. 33840603Sken * XXX Handle any transactions queued to the card 33940603Sken * with XPT_ABORT_CCB. 34040603Sken */ 341112946Sphk bioq_flush(&softc->bio_queue, NULL, ENXIO); 34240603Sken} 34340603Sken 34440603Skenstatic void 34539213Sgibbsptdtor(struct cam_periph *periph) 34639213Sgibbs{ 34740603Sken struct pt_softc *softc; 34840603Sken 34940603Sken softc = (struct pt_softc *)periph->softc; 35040603Sken 351112006Sphk devstat_remove_entry(softc->device_stats); 352187028Strasz cam_periph_unlock(periph); 35353257Sken destroy_dev(softc->dev); 354187028Strasz cam_periph_lock(periph); 35540603Sken free(softc, M_DEVBUF); 35639213Sgibbs} 35739213Sgibbs 35839213Sgibbsstatic void 35939213Sgibbsptasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg) 36039213Sgibbs{ 36139213Sgibbs struct cam_periph *periph; 36239213Sgibbs 36339213Sgibbs periph = (struct cam_periph *)callback_arg; 36439213Sgibbs switch (code) { 36539213Sgibbs case AC_FOUND_DEVICE: 36639213Sgibbs { 36739213Sgibbs struct ccb_getdev *cgd; 36839213Sgibbs cam_status status; 36939213Sgibbs 37039213Sgibbs cgd = (struct ccb_getdev *)arg; 37179177Smjacob if (cgd == NULL) 37279177Smjacob break; 37339213Sgibbs 374195534Sscottl if (cgd->protocol != PROTO_SCSI) 375195534Sscottl break; 376287289Smav if (SID_QUAL(&cgd->inq_data) != SID_QUAL_LU_CONNECTED) 377287289Smav break; 37856148Smjacob if (SID_TYPE(&cgd->inq_data) != T_PROCESSOR) 37939213Sgibbs break; 38039213Sgibbs 38139213Sgibbs /* 38239213Sgibbs * Allocate a peripheral instance for 38339213Sgibbs * this device and start the probe 38439213Sgibbs * process. 38539213Sgibbs */ 38640603Sken status = cam_periph_alloc(ptctor, ptoninvalidate, ptdtor, 38740603Sken ptstart, "pt", CAM_PERIPH_BIO, 388256843Smav path, ptasync, 38940603Sken AC_FOUND_DEVICE, cgd); 39039213Sgibbs 39139213Sgibbs if (status != CAM_REQ_CMP 39239213Sgibbs && status != CAM_REQ_INPROG) 39339213Sgibbs printf("ptasync: Unable to attach to new device " 39439213Sgibbs "due to status 0x%x\n", status); 39539213Sgibbs break; 39639213Sgibbs } 39739213Sgibbs case AC_SENT_BDR: 39839213Sgibbs case AC_BUS_RESET: 39939213Sgibbs { 40039213Sgibbs struct pt_softc *softc; 40139213Sgibbs struct ccb_hdr *ccbh; 40239213Sgibbs 40339213Sgibbs softc = (struct pt_softc *)periph->softc; 40439213Sgibbs /* 40539213Sgibbs * Don't fail on the expected unit attention 40639213Sgibbs * that will occur. 40739213Sgibbs */ 40839213Sgibbs softc->flags |= PT_FLAG_RETRY_UA; 40971999Sphk LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le) 41039213Sgibbs ccbh->ccb_state |= PT_CCB_RETRY_UA; 41139213Sgibbs } 412115562Sphk /* FALLTHROUGH */ 41339213Sgibbs default: 41447413Sgibbs cam_periph_async(periph, code, path, arg); 41539213Sgibbs break; 41639213Sgibbs } 41739213Sgibbs} 41839213Sgibbs 41939213Sgibbsstatic void 42039213Sgibbsptstart(struct cam_periph *periph, union ccb *start_ccb) 42139213Sgibbs{ 42239213Sgibbs struct pt_softc *softc; 42359249Sphk struct bio *bp; 42439213Sgibbs 42539213Sgibbs softc = (struct pt_softc *)periph->softc; 42639213Sgibbs 427236602Smav CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("ptstart\n")); 428236602Smav 42939213Sgibbs /* 43039213Sgibbs * See if there is a buf with work for us to do.. 43139213Sgibbs */ 43259249Sphk bp = bioq_first(&softc->bio_queue); 433256843Smav if (bp == NULL) { 43439213Sgibbs xpt_release_ccb(start_ccb); 43539213Sgibbs } else { 43659249Sphk bioq_remove(&softc->bio_queue, bp); 43739213Sgibbs 438112260Sphk devstat_start_transaction_bio(softc->device_stats, bp); 43939213Sgibbs 44039213Sgibbs scsi_send_receive(&start_ccb->csio, 44139213Sgibbs /*retries*/4, 44239213Sgibbs ptdone, 44339213Sgibbs MSG_SIMPLE_Q_TAG, 44459249Sphk bp->bio_cmd == BIO_READ, 44539213Sgibbs /*byte2*/0, 44659249Sphk bp->bio_bcount, 44759249Sphk bp->bio_data, 44839213Sgibbs /*sense_len*/SSD_FULL_SIZE, 44950073Sken /*timeout*/softc->io_timeout); 45039213Sgibbs 45176192Sken start_ccb->ccb_h.ccb_state = PT_CCB_BUFFER_IO_UA; 45239213Sgibbs 45339213Sgibbs /* 454250460Seadler * Block out any asynchronous callbacks 45539213Sgibbs * while we touch the pending ccb list. 45639213Sgibbs */ 45739213Sgibbs LIST_INSERT_HEAD(&softc->pending_ccbs, &start_ccb->ccb_h, 45839213Sgibbs periph_links.le); 45939213Sgibbs 46039213Sgibbs start_ccb->ccb_h.ccb_bp = bp; 46159249Sphk bp = bioq_first(&softc->bio_queue); 46239213Sgibbs 46339213Sgibbs xpt_action(start_ccb); 46439213Sgibbs 46539213Sgibbs if (bp != NULL) { 46639213Sgibbs /* Have more work to do, so ensure we stay scheduled */ 467198382Smav xpt_schedule(periph, CAM_PRIORITY_NORMAL); 46839213Sgibbs } 46939213Sgibbs } 47039213Sgibbs} 47139213Sgibbs 47239213Sgibbsstatic void 47339213Sgibbsptdone(struct cam_periph *periph, union ccb *done_ccb) 47439213Sgibbs{ 47539213Sgibbs struct pt_softc *softc; 47639213Sgibbs struct ccb_scsiio *csio; 47739213Sgibbs 47839213Sgibbs softc = (struct pt_softc *)periph->softc; 479236602Smav 480236602Smav CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("ptdone\n")); 481236602Smav 48239213Sgibbs csio = &done_ccb->csio; 48339213Sgibbs switch (csio->ccb_h.ccb_state) { 48439213Sgibbs case PT_CCB_BUFFER_IO: 48539213Sgibbs case PT_CCB_BUFFER_IO_UA: 48639213Sgibbs { 48759249Sphk struct bio *bp; 48839213Sgibbs 48959249Sphk bp = (struct bio *)done_ccb->ccb_h.ccb_bp; 49039213Sgibbs if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 49139213Sgibbs int error; 49239213Sgibbs int sf; 49339213Sgibbs 49439213Sgibbs if ((csio->ccb_h.ccb_state & PT_CCB_RETRY_UA) != 0) 49539213Sgibbs sf = SF_RETRY_UA; 49639213Sgibbs else 49739213Sgibbs sf = 0; 49839213Sgibbs 49974840Sken error = pterror(done_ccb, CAM_RETRY_SELTO, sf); 50074840Sken if (error == ERESTART) { 50139213Sgibbs /* 50239213Sgibbs * A retry was scheuled, so 50339213Sgibbs * just return. 50439213Sgibbs */ 50539213Sgibbs return; 50639213Sgibbs } 50739213Sgibbs if (error != 0) { 50839213Sgibbs if (error == ENXIO) { 50939213Sgibbs /* 51039213Sgibbs * Catastrophic error. Mark our device 51139213Sgibbs * as invalid. 51239213Sgibbs */ 513164906Smjacob xpt_print(periph->path, 514164906Smjacob "Invalidating device\n"); 51539213Sgibbs softc->flags |= PT_FLAG_DEVICE_INVALID; 51639213Sgibbs } 51739213Sgibbs 51839213Sgibbs /* 51939213Sgibbs * return all queued I/O with EIO, so that 52039213Sgibbs * the client can retry these I/Os in the 52139213Sgibbs * proper order should it attempt to recover. 52239213Sgibbs */ 523112946Sphk bioq_flush(&softc->bio_queue, NULL, EIO); 52459249Sphk bp->bio_error = error; 52559249Sphk bp->bio_resid = bp->bio_bcount; 52659249Sphk bp->bio_flags |= BIO_ERROR; 52739213Sgibbs } else { 52859249Sphk bp->bio_resid = csio->resid; 52959249Sphk bp->bio_error = 0; 53059249Sphk if (bp->bio_resid != 0) { 53139213Sgibbs /* Short transfer ??? */ 53259249Sphk bp->bio_flags |= BIO_ERROR; 53339213Sgibbs } 53439213Sgibbs } 53539213Sgibbs if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 53639213Sgibbs cam_release_devq(done_ccb->ccb_h.path, 53739213Sgibbs /*relsim_flags*/0, 53839213Sgibbs /*reduction*/0, 53939213Sgibbs /*timeout*/0, 54039213Sgibbs /*getcount_only*/0); 54139213Sgibbs } else { 54259249Sphk bp->bio_resid = csio->resid; 54359249Sphk if (bp->bio_resid != 0) 54459249Sphk bp->bio_flags |= BIO_ERROR; 54539213Sgibbs } 54639213Sgibbs 54739213Sgibbs /* 548250460Seadler * Block out any asynchronous callbacks 54939213Sgibbs * while we touch the pending ccb list. 55039213Sgibbs */ 55139213Sgibbs LIST_REMOVE(&done_ccb->ccb_h, periph_links.le); 55239213Sgibbs 553112006Sphk biofinish(bp, softc->device_stats, 0); 55439213Sgibbs break; 55539213Sgibbs } 55639213Sgibbs } 55739213Sgibbs xpt_release_ccb(done_ccb); 55839213Sgibbs} 55939213Sgibbs 56039213Sgibbsstatic int 56139213Sgibbspterror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 56239213Sgibbs{ 56339213Sgibbs struct pt_softc *softc; 56439213Sgibbs struct cam_periph *periph; 56539213Sgibbs 56639213Sgibbs periph = xpt_path_periph(ccb->ccb_h.path); 56739213Sgibbs softc = (struct pt_softc *)periph->softc; 56839213Sgibbs 56939213Sgibbs return(cam_periph_error(ccb, cam_flags, sense_flags, 57039213Sgibbs &softc->saved_ccb)); 57139213Sgibbs} 57239213Sgibbs 57350073Skenstatic int 574130585Sphkptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td) 57550073Sken{ 57650073Sken struct cam_periph *periph; 57750073Sken struct pt_softc *softc; 578168752Sscottl int error = 0; 57950073Sken 580101940Snjl periph = (struct cam_periph *)dev->si_drv1; 58150073Sken softc = (struct pt_softc *)periph->softc; 58250073Sken 583168752Sscottl cam_periph_lock(periph); 58450073Sken 58550073Sken switch(cmd) { 58650073Sken case PTIOCGETTIMEOUT: 58750073Sken if (softc->io_timeout >= 1000) 58850073Sken *(int *)addr = softc->io_timeout / 1000; 58950073Sken else 59050073Sken *(int *)addr = 0; 59150073Sken break; 59250073Sken case PTIOCSETTIMEOUT: 59350073Sken if (*(int *)addr < 1) { 59450073Sken error = EINVAL; 59550073Sken break; 59650073Sken } 59750073Sken 59850073Sken softc->io_timeout = *(int *)addr * 1000; 59950073Sken 60050073Sken break; 60150073Sken default: 60250073Sken error = cam_periph_ioctl(periph, cmd, addr, pterror); 60350073Sken break; 60450073Sken } 60550073Sken 60650073Sken cam_periph_unlock(periph); 60750073Sken 60850073Sken return(error); 60950073Sken} 61050073Sken 61139213Sgibbsvoid 61239213Sgibbsscsi_send_receive(struct ccb_scsiio *csio, u_int32_t retries, 61339213Sgibbs void (*cbfcnp)(struct cam_periph *, union ccb *), 61439213Sgibbs u_int tag_action, int readop, u_int byte2, 61539213Sgibbs u_int32_t xfer_len, u_int8_t *data_ptr, u_int8_t sense_len, 61639213Sgibbs u_int32_t timeout) 61739213Sgibbs{ 61839213Sgibbs struct scsi_send_receive *scsi_cmd; 61939213Sgibbs 62039213Sgibbs scsi_cmd = (struct scsi_send_receive *)&csio->cdb_io.cdb_bytes; 62139213Sgibbs scsi_cmd->opcode = readop ? RECEIVE : SEND; 62239213Sgibbs scsi_cmd->byte2 = byte2; 62339213Sgibbs scsi_ulto3b(xfer_len, scsi_cmd->xfer_len); 62439213Sgibbs scsi_cmd->control = 0; 62539213Sgibbs 62639213Sgibbs cam_fill_csio(csio, 62739213Sgibbs retries, 62839213Sgibbs cbfcnp, 62939213Sgibbs /*flags*/readop ? CAM_DIR_IN : CAM_DIR_OUT, 63039213Sgibbs tag_action, 63139213Sgibbs data_ptr, 63239213Sgibbs xfer_len, 63339213Sgibbs sense_len, 63439213Sgibbs sizeof(*scsi_cmd), 63539213Sgibbs timeout); 63639213Sgibbs} 637