scsi_pt.c revision 195534
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: head/sys/cam/scsi/scsi_pt.c 195534 2009-07-10 08:18:08Z scottl $"); 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_WAITING = 0x02, 7039213Sgibbs PT_CCB_RETRY_UA = 0x04, 7139213Sgibbs PT_CCB_BUFFER_IO_UA = PT_CCB_BUFFER_IO|PT_CCB_RETRY_UA 7239213Sgibbs} pt_ccb_state; 7339213Sgibbs 7439213Sgibbs/* Offsets into our private area for storing information */ 7539213Sgibbs#define ccb_state ppriv_field0 7639213Sgibbs#define ccb_bp ppriv_ptr1 7739213Sgibbs 7839213Sgibbsstruct pt_softc { 7959249Sphk struct bio_queue_head bio_queue; 80112006Sphk struct devstat *device_stats; 8160938Sjake LIST_HEAD(, ccb_hdr) pending_ccbs; 8239213Sgibbs pt_state state; 8339213Sgibbs pt_flags flags; 8439213Sgibbs union ccb saved_ccb; 8550073Sken int io_timeout; 86130585Sphk struct cdev *dev; 8739213Sgibbs}; 8839213Sgibbs 8939213Sgibbsstatic d_open_t ptopen; 9039213Sgibbsstatic d_close_t ptclose; 9139213Sgibbsstatic d_strategy_t ptstrategy; 9239213Sgibbsstatic periph_init_t ptinit; 9339213Sgibbsstatic void ptasync(void *callback_arg, u_int32_t code, 9439213Sgibbs struct cam_path *path, void *arg); 9539213Sgibbsstatic periph_ctor_t ptctor; 9640603Skenstatic periph_oninv_t ptoninvalidate; 9739213Sgibbsstatic periph_dtor_t ptdtor; 9839213Sgibbsstatic periph_start_t ptstart; 9939213Sgibbsstatic void ptdone(struct cam_periph *periph, 10039213Sgibbs union ccb *done_ccb); 10150073Skenstatic d_ioctl_t ptioctl; 10239213Sgibbsstatic int pterror(union ccb *ccb, u_int32_t cam_flags, 10339213Sgibbs u_int32_t sense_flags); 10439213Sgibbs 10539213Sgibbsvoid scsi_send_receive(struct ccb_scsiio *csio, u_int32_t retries, 10639213Sgibbs void (*cbfcnp)(struct cam_periph *, union ccb *), 10739213Sgibbs u_int tag_action, int readop, u_int byte2, 10839213Sgibbs u_int32_t xfer_len, u_int8_t *data_ptr, 10939213Sgibbs u_int8_t sense_len, u_int32_t timeout); 11039213Sgibbs 11139213Sgibbsstatic struct periph_driver ptdriver = 11239213Sgibbs{ 11339213Sgibbs ptinit, "pt", 11439213Sgibbs TAILQ_HEAD_INITIALIZER(ptdriver.units), /* generation */ 0 11539213Sgibbs}; 11639213Sgibbs 11772119SpeterPERIPHDRIVER_DECLARE(pt, ptdriver); 11839213Sgibbs 11939213Sgibbs 12047625Sphkstatic struct cdevsw pt_cdevsw = { 121126080Sphk .d_version = D_VERSION, 122168752Sscottl .d_flags = 0, 123111815Sphk .d_open = ptopen, 124111815Sphk .d_close = ptclose, 125111815Sphk .d_read = physread, 126111815Sphk .d_write = physwrite, 127111815Sphk .d_ioctl = ptioctl, 128111815Sphk .d_strategy = ptstrategy, 129111815Sphk .d_name = "pt", 13039213Sgibbs}; 13139213Sgibbs 13250073Sken#ifndef SCSI_PT_DEFAULT_TIMEOUT 13350073Sken#define SCSI_PT_DEFAULT_TIMEOUT 60 13450073Sken#endif 13550073Sken 13639213Sgibbsstatic int 137130585Sphkptopen(struct cdev *dev, int flags, int fmt, struct thread *td) 13839213Sgibbs{ 13939213Sgibbs struct cam_periph *periph; 14039213Sgibbs struct pt_softc *softc; 141168752Sscottl int error = 0; 14239213Sgibbs 143101940Snjl periph = (struct cam_periph *)dev->si_drv1; 144168752Sscottl if (cam_periph_acquire(periph) != CAM_REQ_CMP) 14539213Sgibbs return (ENXIO); 14639213Sgibbs 14739213Sgibbs softc = (struct pt_softc *)periph->softc; 14839213Sgibbs 149168752Sscottl cam_periph_lock(periph); 15040603Sken if (softc->flags & PT_FLAG_DEVICE_INVALID) { 151168752Sscottl cam_periph_unlock(periph); 152168752Sscottl cam_periph_release(periph); 15340603Sken return(ENXIO); 15440603Sken } 15540603Sken 156168752Sscottl if ((softc->flags & PT_FLAG_OPEN) == 0) 157168752Sscottl softc->flags |= PT_FLAG_OPEN; 158168752Sscottl else { 159168752Sscottl error = EBUSY; 160168752Sscottl cam_periph_release(periph); 16141297Sken } 16239213Sgibbs 163168752Sscottl CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 164168752Sscottl ("ptopen: dev=%s\n", devtoname(dev))); 16541297Sken 16639213Sgibbs cam_periph_unlock(periph); 16739213Sgibbs return (error); 16839213Sgibbs} 16939213Sgibbs 17039213Sgibbsstatic int 171130585Sphkptclose(struct cdev *dev, int flag, int fmt, struct thread *td) 17239213Sgibbs{ 17339213Sgibbs struct cam_periph *periph; 17439213Sgibbs struct pt_softc *softc; 17539213Sgibbs 176101940Snjl periph = (struct cam_periph *)dev->si_drv1; 17739213Sgibbs if (periph == NULL) 17839213Sgibbs return (ENXIO); 17939213Sgibbs 18039213Sgibbs softc = (struct pt_softc *)periph->softc; 18139213Sgibbs 182168752Sscottl cam_periph_lock(periph); 18339213Sgibbs 18439213Sgibbs softc->flags &= ~PT_FLAG_OPEN; 18539213Sgibbs cam_periph_unlock(periph); 18639213Sgibbs cam_periph_release(periph); 18739213Sgibbs return (0); 18839213Sgibbs} 18939213Sgibbs 19039213Sgibbs/* 19139213Sgibbs * Actually translate the requested transfer into one the physical driver 19239213Sgibbs * can understand. The transfer is described by a buf and will include 19339213Sgibbs * only one physical transfer. 19439213Sgibbs */ 19539213Sgibbsstatic void 19659249Sphkptstrategy(struct bio *bp) 19739213Sgibbs{ 19839213Sgibbs struct cam_periph *periph; 19939213Sgibbs struct pt_softc *softc; 20039213Sgibbs 201101940Snjl periph = (struct cam_periph *)bp->bio_dev->si_drv1; 20276362Sphk bp->bio_resid = bp->bio_bcount; 20339213Sgibbs if (periph == NULL) { 20476362Sphk biofinish(bp, NULL, ENXIO); 20576362Sphk return; 20639213Sgibbs } 207168752Sscottl cam_periph_lock(periph); 20839213Sgibbs softc = (struct pt_softc *)periph->softc; 20939213Sgibbs 21039213Sgibbs /* 21139213Sgibbs * If the device has been made invalid, error out 21239213Sgibbs */ 21339213Sgibbs if ((softc->flags & PT_FLAG_DEVICE_INVALID)) { 214168752Sscottl cam_periph_unlock(periph); 21576362Sphk biofinish(bp, NULL, ENXIO); 21676362Sphk return; 21739213Sgibbs } 21839213Sgibbs 21939213Sgibbs /* 22039213Sgibbs * Place it in the queue of disk activities for this disk 22139213Sgibbs */ 22259249Sphk bioq_insert_tail(&softc->bio_queue, bp); 22339213Sgibbs 22439213Sgibbs /* 22539213Sgibbs * Schedule ourselves for performing the work. 22639213Sgibbs */ 22739213Sgibbs xpt_schedule(periph, /* XXX priority */1); 228168752Sscottl cam_periph_unlock(periph); 22939213Sgibbs 23039213Sgibbs return; 23139213Sgibbs} 23239213Sgibbs 23339213Sgibbsstatic void 23439213Sgibbsptinit(void) 23539213Sgibbs{ 23639213Sgibbs cam_status status; 23739213Sgibbs 23839213Sgibbs /* 23939213Sgibbs * Install a global async callback. This callback will 24039213Sgibbs * receive async callbacks like "new device found". 24139213Sgibbs */ 242169605Sscottl status = xpt_register_async(AC_FOUND_DEVICE, ptasync, NULL, NULL); 24339213Sgibbs 24439213Sgibbs if (status != CAM_REQ_CMP) { 24539213Sgibbs printf("pt: Failed to attach master async callback " 24639213Sgibbs "due to status 0x%x!\n", status); 24739213Sgibbs } 24839213Sgibbs} 24939213Sgibbs 25039213Sgibbsstatic cam_status 25139213Sgibbsptctor(struct cam_periph *periph, void *arg) 25239213Sgibbs{ 25339213Sgibbs struct pt_softc *softc; 25439213Sgibbs struct ccb_getdev *cgd; 25539213Sgibbs 25639213Sgibbs cgd = (struct ccb_getdev *)arg; 25739213Sgibbs if (periph == NULL) { 25839213Sgibbs printf("ptregister: periph was NULL!!\n"); 25939213Sgibbs return(CAM_REQ_CMP_ERR); 26039213Sgibbs } 26139213Sgibbs 26239213Sgibbs if (cgd == NULL) { 26339213Sgibbs printf("ptregister: no getdev CCB, can't register device\n"); 26439213Sgibbs return(CAM_REQ_CMP_ERR); 26539213Sgibbs } 26639213Sgibbs 26739213Sgibbs softc = (struct pt_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT); 26839213Sgibbs 26939213Sgibbs if (softc == NULL) { 27039213Sgibbs printf("daregister: Unable to probe new device. " 27139213Sgibbs "Unable to allocate softc\n"); 27239213Sgibbs return(CAM_REQ_CMP_ERR); 27339213Sgibbs } 27439213Sgibbs 27539213Sgibbs bzero(softc, sizeof(*softc)); 27639213Sgibbs LIST_INIT(&softc->pending_ccbs); 27739213Sgibbs softc->state = PT_STATE_NORMAL; 27859249Sphk bioq_init(&softc->bio_queue); 27939213Sgibbs 28050073Sken softc->io_timeout = SCSI_PT_DEFAULT_TIMEOUT * 1000; 28150073Sken 28239213Sgibbs periph->softc = softc; 28339213Sgibbs 284169605Sscottl cam_periph_unlock(periph); 285112006Sphk softc->device_stats = devstat_new_entry("pt", 28639213Sgibbs periph->unit_number, 0, 28739213Sgibbs DEVSTAT_NO_BLOCKSIZE, 28856148Smjacob SID_TYPE(&cgd->inq_data) | DEVSTAT_TYPE_IF_SCSI, 28943819Sken DEVSTAT_PRIORITY_OTHER); 29039213Sgibbs 29153257Sken softc->dev = make_dev(&pt_cdevsw, periph->unit_number, UID_ROOT, 29253257Sken GID_OPERATOR, 0600, "%s%d", periph->periph_name, 29353257Sken periph->unit_number); 294168872Sscottl cam_periph_lock(periph); 295101940Snjl softc->dev->si_drv1 = periph; 296101940Snjl 29739213Sgibbs /* 29839213Sgibbs * Add async callbacks for bus reset and 29939213Sgibbs * bus device reset calls. I don't bother 30039213Sgibbs * checking if this fails as, in most cases, 30139213Sgibbs * the system will function just fine without 30239213Sgibbs * them and the only alternative would be to 30339213Sgibbs * not attach the device on failure. 30439213Sgibbs */ 305169605Sscottl xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE, 306169605Sscottl ptasync, periph, periph->path); 30739213Sgibbs 30839213Sgibbs /* Tell the user we've attached to the device */ 30939213Sgibbs xpt_announce_periph(periph, NULL); 31039213Sgibbs 31139213Sgibbs return(CAM_REQ_CMP); 31239213Sgibbs} 31339213Sgibbs 31439213Sgibbsstatic void 31540603Skenptoninvalidate(struct cam_periph *periph) 31640603Sken{ 31740603Sken struct pt_softc *softc; 31840603Sken 31940603Sken softc = (struct pt_softc *)periph->softc; 32040603Sken 32140603Sken /* 32240603Sken * De-register any async callbacks. 32340603Sken */ 324169605Sscottl xpt_register_async(0, ptasync, periph, periph->path); 32540603Sken 32640603Sken softc->flags |= PT_FLAG_DEVICE_INVALID; 32740603Sken 32840603Sken /* 32940603Sken * Return all queued I/O with ENXIO. 33040603Sken * XXX Handle any transactions queued to the card 33140603Sken * with XPT_ABORT_CCB. 33240603Sken */ 333112946Sphk bioq_flush(&softc->bio_queue, NULL, ENXIO); 33440603Sken 335164906Smjacob xpt_print(periph->path, "lost device\n"); 33640603Sken} 33740603Sken 33840603Skenstatic void 33939213Sgibbsptdtor(struct cam_periph *periph) 34039213Sgibbs{ 34140603Sken struct pt_softc *softc; 34240603Sken 34340603Sken softc = (struct pt_softc *)periph->softc; 34440603Sken 345187028Strasz xpt_print(periph->path, "removing device entry\n"); 346112006Sphk devstat_remove_entry(softc->device_stats); 347187028Strasz cam_periph_unlock(periph); 34853257Sken destroy_dev(softc->dev); 349187028Strasz cam_periph_lock(periph); 35040603Sken free(softc, M_DEVBUF); 35139213Sgibbs} 35239213Sgibbs 35339213Sgibbsstatic void 35439213Sgibbsptasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg) 35539213Sgibbs{ 35639213Sgibbs struct cam_periph *periph; 35739213Sgibbs 35839213Sgibbs periph = (struct cam_periph *)callback_arg; 35939213Sgibbs switch (code) { 36039213Sgibbs case AC_FOUND_DEVICE: 36139213Sgibbs { 36239213Sgibbs struct ccb_getdev *cgd; 36339213Sgibbs cam_status status; 36439213Sgibbs 36539213Sgibbs cgd = (struct ccb_getdev *)arg; 36679177Smjacob if (cgd == NULL) 36779177Smjacob break; 36839213Sgibbs 369195534Sscottl if (cgd->protocol != PROTO_SCSI) 370195534Sscottl break; 371195534Sscottl 37256148Smjacob if (SID_TYPE(&cgd->inq_data) != T_PROCESSOR) 37339213Sgibbs break; 37439213Sgibbs 37539213Sgibbs /* 37639213Sgibbs * Allocate a peripheral instance for 37739213Sgibbs * this device and start the probe 37839213Sgibbs * process. 37939213Sgibbs */ 38040603Sken status = cam_periph_alloc(ptctor, ptoninvalidate, ptdtor, 38140603Sken ptstart, "pt", CAM_PERIPH_BIO, 38240603Sken cgd->ccb_h.path, ptasync, 38340603Sken AC_FOUND_DEVICE, cgd); 38439213Sgibbs 38539213Sgibbs if (status != CAM_REQ_CMP 38639213Sgibbs && status != CAM_REQ_INPROG) 38739213Sgibbs printf("ptasync: Unable to attach to new device " 38839213Sgibbs "due to status 0x%x\n", status); 38939213Sgibbs break; 39039213Sgibbs } 39139213Sgibbs case AC_SENT_BDR: 39239213Sgibbs case AC_BUS_RESET: 39339213Sgibbs { 39439213Sgibbs struct pt_softc *softc; 39539213Sgibbs struct ccb_hdr *ccbh; 39639213Sgibbs 39739213Sgibbs softc = (struct pt_softc *)periph->softc; 39839213Sgibbs /* 39939213Sgibbs * Don't fail on the expected unit attention 40039213Sgibbs * that will occur. 40139213Sgibbs */ 40239213Sgibbs softc->flags |= PT_FLAG_RETRY_UA; 40371999Sphk LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le) 40439213Sgibbs ccbh->ccb_state |= PT_CCB_RETRY_UA; 40539213Sgibbs } 406115562Sphk /* FALLTHROUGH */ 40739213Sgibbs default: 40847413Sgibbs cam_periph_async(periph, code, path, arg); 40939213Sgibbs break; 41039213Sgibbs } 41139213Sgibbs} 41239213Sgibbs 41339213Sgibbsstatic void 41439213Sgibbsptstart(struct cam_periph *periph, union ccb *start_ccb) 41539213Sgibbs{ 41639213Sgibbs struct pt_softc *softc; 41759249Sphk struct bio *bp; 41839213Sgibbs 41939213Sgibbs softc = (struct pt_softc *)periph->softc; 42039213Sgibbs 42139213Sgibbs /* 42239213Sgibbs * See if there is a buf with work for us to do.. 42339213Sgibbs */ 42459249Sphk bp = bioq_first(&softc->bio_queue); 42539213Sgibbs if (periph->immediate_priority <= periph->pinfo.priority) { 42639213Sgibbs CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE, 42739213Sgibbs ("queuing for immediate ccb\n")); 42839213Sgibbs start_ccb->ccb_h.ccb_state = PT_CCB_WAITING; 42939213Sgibbs SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h, 43039213Sgibbs periph_links.sle); 43139213Sgibbs periph->immediate_priority = CAM_PRIORITY_NONE; 43239213Sgibbs wakeup(&periph->ccb_list); 43339213Sgibbs } else 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 /* 45439213Sgibbs * Block out any asyncronous 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 */ 46739213Sgibbs xpt_schedule(periph, /* XXX priority */1); 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; 47939213Sgibbs csio = &done_ccb->csio; 48039213Sgibbs switch (csio->ccb_h.ccb_state) { 48139213Sgibbs case PT_CCB_BUFFER_IO: 48239213Sgibbs case PT_CCB_BUFFER_IO_UA: 48339213Sgibbs { 48459249Sphk struct bio *bp; 48539213Sgibbs 48659249Sphk bp = (struct bio *)done_ccb->ccb_h.ccb_bp; 48739213Sgibbs if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 48839213Sgibbs int error; 48939213Sgibbs int sf; 49039213Sgibbs 49139213Sgibbs if ((csio->ccb_h.ccb_state & PT_CCB_RETRY_UA) != 0) 49239213Sgibbs sf = SF_RETRY_UA; 49339213Sgibbs else 49439213Sgibbs sf = 0; 49539213Sgibbs 49674840Sken error = pterror(done_ccb, CAM_RETRY_SELTO, sf); 49774840Sken if (error == ERESTART) { 49839213Sgibbs /* 49939213Sgibbs * A retry was scheuled, so 50039213Sgibbs * just return. 50139213Sgibbs */ 50239213Sgibbs return; 50339213Sgibbs } 50439213Sgibbs if (error != 0) { 50539213Sgibbs if (error == ENXIO) { 50639213Sgibbs /* 50739213Sgibbs * Catastrophic error. Mark our device 50839213Sgibbs * as invalid. 50939213Sgibbs */ 510164906Smjacob xpt_print(periph->path, 511164906Smjacob "Invalidating device\n"); 51239213Sgibbs softc->flags |= PT_FLAG_DEVICE_INVALID; 51339213Sgibbs } 51439213Sgibbs 51539213Sgibbs /* 51639213Sgibbs * return all queued I/O with EIO, so that 51739213Sgibbs * the client can retry these I/Os in the 51839213Sgibbs * proper order should it attempt to recover. 51939213Sgibbs */ 520112946Sphk bioq_flush(&softc->bio_queue, NULL, EIO); 52159249Sphk bp->bio_error = error; 52259249Sphk bp->bio_resid = bp->bio_bcount; 52359249Sphk bp->bio_flags |= BIO_ERROR; 52439213Sgibbs } else { 52559249Sphk bp->bio_resid = csio->resid; 52659249Sphk bp->bio_error = 0; 52759249Sphk if (bp->bio_resid != 0) { 52839213Sgibbs /* Short transfer ??? */ 52959249Sphk bp->bio_flags |= BIO_ERROR; 53039213Sgibbs } 53139213Sgibbs } 53239213Sgibbs if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 53339213Sgibbs cam_release_devq(done_ccb->ccb_h.path, 53439213Sgibbs /*relsim_flags*/0, 53539213Sgibbs /*reduction*/0, 53639213Sgibbs /*timeout*/0, 53739213Sgibbs /*getcount_only*/0); 53839213Sgibbs } else { 53959249Sphk bp->bio_resid = csio->resid; 54059249Sphk if (bp->bio_resid != 0) 54159249Sphk bp->bio_flags |= BIO_ERROR; 54239213Sgibbs } 54339213Sgibbs 54439213Sgibbs /* 54539213Sgibbs * Block out any asyncronous callbacks 54639213Sgibbs * while we touch the pending ccb list. 54739213Sgibbs */ 54839213Sgibbs LIST_REMOVE(&done_ccb->ccb_h, periph_links.le); 54939213Sgibbs 550112006Sphk biofinish(bp, softc->device_stats, 0); 55139213Sgibbs break; 55239213Sgibbs } 55339213Sgibbs case PT_CCB_WAITING: 55439213Sgibbs /* Caller will release the CCB */ 55539213Sgibbs wakeup(&done_ccb->ccb_h.cbfcnp); 55639213Sgibbs return; 55739213Sgibbs } 55839213Sgibbs xpt_release_ccb(done_ccb); 55939213Sgibbs} 56039213Sgibbs 56139213Sgibbsstatic int 56239213Sgibbspterror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 56339213Sgibbs{ 56439213Sgibbs struct pt_softc *softc; 56539213Sgibbs struct cam_periph *periph; 56639213Sgibbs 56739213Sgibbs periph = xpt_path_periph(ccb->ccb_h.path); 56839213Sgibbs softc = (struct pt_softc *)periph->softc; 56939213Sgibbs 57039213Sgibbs return(cam_periph_error(ccb, cam_flags, sense_flags, 57139213Sgibbs &softc->saved_ccb)); 57239213Sgibbs} 57339213Sgibbs 57450073Skenstatic int 575130585Sphkptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td) 57650073Sken{ 57750073Sken struct cam_periph *periph; 57850073Sken struct pt_softc *softc; 579168752Sscottl int error = 0; 58050073Sken 581101940Snjl periph = (struct cam_periph *)dev->si_drv1; 58250073Sken if (periph == NULL) 58350073Sken return(ENXIO); 58450073Sken 58550073Sken softc = (struct pt_softc *)periph->softc; 58650073Sken 587168752Sscottl cam_periph_lock(periph); 58850073Sken 58950073Sken switch(cmd) { 59050073Sken case PTIOCGETTIMEOUT: 59150073Sken if (softc->io_timeout >= 1000) 59250073Sken *(int *)addr = softc->io_timeout / 1000; 59350073Sken else 59450073Sken *(int *)addr = 0; 59550073Sken break; 59650073Sken case PTIOCSETTIMEOUT: 59750073Sken if (*(int *)addr < 1) { 59850073Sken error = EINVAL; 59950073Sken break; 60050073Sken } 60150073Sken 60250073Sken softc->io_timeout = *(int *)addr * 1000; 60350073Sken 60450073Sken break; 60550073Sken default: 60650073Sken error = cam_periph_ioctl(periph, cmd, addr, pterror); 60750073Sken break; 60850073Sken } 60950073Sken 61050073Sken cam_periph_unlock(periph); 61150073Sken 61250073Sken return(error); 61350073Sken} 61450073Sken 61539213Sgibbsvoid 61639213Sgibbsscsi_send_receive(struct ccb_scsiio *csio, u_int32_t retries, 61739213Sgibbs void (*cbfcnp)(struct cam_periph *, union ccb *), 61839213Sgibbs u_int tag_action, int readop, u_int byte2, 61939213Sgibbs u_int32_t xfer_len, u_int8_t *data_ptr, u_int8_t sense_len, 62039213Sgibbs u_int32_t timeout) 62139213Sgibbs{ 62239213Sgibbs struct scsi_send_receive *scsi_cmd; 62339213Sgibbs 62439213Sgibbs scsi_cmd = (struct scsi_send_receive *)&csio->cdb_io.cdb_bytes; 62539213Sgibbs scsi_cmd->opcode = readop ? RECEIVE : SEND; 62639213Sgibbs scsi_cmd->byte2 = byte2; 62739213Sgibbs scsi_ulto3b(xfer_len, scsi_cmd->xfer_len); 62839213Sgibbs scsi_cmd->control = 0; 62939213Sgibbs 63039213Sgibbs cam_fill_csio(csio, 63139213Sgibbs retries, 63239213Sgibbs cbfcnp, 63339213Sgibbs /*flags*/readop ? CAM_DIR_IN : CAM_DIR_OUT, 63439213Sgibbs tag_action, 63539213Sgibbs data_ptr, 63639213Sgibbs xfer_len, 63739213Sgibbs sense_len, 63839213Sgibbs sizeof(*scsi_cmd), 63939213Sgibbs timeout); 64039213Sgibbs} 641