scsi_pt.c revision 236138
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 236138 2012-05-27 06:11:09Z ken $"); 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) { 151236138Sken cam_periph_release_locked(periph); 152168752Sscottl cam_periph_unlock(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; 185236138Sken cam_periph_release_locked(periph); 18639213Sgibbs cam_periph_unlock(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 */ 227198382Smav xpt_schedule(periph, CAM_PRIORITY_NORMAL); 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; 255220644Smav struct ccb_pathinq cpi; 25639213Sgibbs 25739213Sgibbs cgd = (struct ccb_getdev *)arg; 25839213Sgibbs if (periph == NULL) { 25939213Sgibbs printf("ptregister: periph was NULL!!\n"); 26039213Sgibbs return(CAM_REQ_CMP_ERR); 26139213Sgibbs } 26239213Sgibbs 26339213Sgibbs if (cgd == NULL) { 26439213Sgibbs printf("ptregister: no getdev CCB, can't register device\n"); 26539213Sgibbs return(CAM_REQ_CMP_ERR); 26639213Sgibbs } 26739213Sgibbs 26839213Sgibbs softc = (struct pt_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT); 26939213Sgibbs 27039213Sgibbs if (softc == NULL) { 27139213Sgibbs printf("daregister: Unable to probe new device. " 27239213Sgibbs "Unable to allocate softc\n"); 27339213Sgibbs return(CAM_REQ_CMP_ERR); 27439213Sgibbs } 27539213Sgibbs 27639213Sgibbs bzero(softc, sizeof(*softc)); 27739213Sgibbs LIST_INIT(&softc->pending_ccbs); 27839213Sgibbs softc->state = PT_STATE_NORMAL; 27959249Sphk bioq_init(&softc->bio_queue); 28039213Sgibbs 28150073Sken softc->io_timeout = SCSI_PT_DEFAULT_TIMEOUT * 1000; 28250073Sken 28339213Sgibbs periph->softc = softc; 284220644Smav 285220644Smav bzero(&cpi, sizeof(cpi)); 286220644Smav xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 287220644Smav cpi.ccb_h.func_code = XPT_PATH_INQ; 288220644Smav xpt_action((union ccb *)&cpi); 289220644Smav 290169605Sscottl cam_periph_unlock(periph); 291112006Sphk softc->device_stats = devstat_new_entry("pt", 29239213Sgibbs periph->unit_number, 0, 29339213Sgibbs DEVSTAT_NO_BLOCKSIZE, 294220644Smav SID_TYPE(&cgd->inq_data) | 295220644Smav XPORT_DEVSTAT_TYPE(cpi.transport), 29643819Sken DEVSTAT_PRIORITY_OTHER); 29739213Sgibbs 29853257Sken softc->dev = make_dev(&pt_cdevsw, periph->unit_number, UID_ROOT, 29953257Sken GID_OPERATOR, 0600, "%s%d", periph->periph_name, 30053257Sken periph->unit_number); 301168872Sscottl cam_periph_lock(periph); 302101940Snjl softc->dev->si_drv1 = periph; 303101940Snjl 30439213Sgibbs /* 30539213Sgibbs * Add async callbacks for bus reset and 30639213Sgibbs * bus device reset calls. I don't bother 30739213Sgibbs * checking if this fails as, in most cases, 30839213Sgibbs * the system will function just fine without 30939213Sgibbs * them and the only alternative would be to 31039213Sgibbs * not attach the device on failure. 31139213Sgibbs */ 312169605Sscottl xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE, 313169605Sscottl ptasync, periph, periph->path); 31439213Sgibbs 31539213Sgibbs /* Tell the user we've attached to the device */ 31639213Sgibbs xpt_announce_periph(periph, NULL); 31739213Sgibbs 31839213Sgibbs return(CAM_REQ_CMP); 31939213Sgibbs} 32039213Sgibbs 32139213Sgibbsstatic void 32240603Skenptoninvalidate(struct cam_periph *periph) 32340603Sken{ 32440603Sken struct pt_softc *softc; 32540603Sken 32640603Sken softc = (struct pt_softc *)periph->softc; 32740603Sken 32840603Sken /* 32940603Sken * De-register any async callbacks. 33040603Sken */ 331169605Sscottl xpt_register_async(0, ptasync, periph, periph->path); 33240603Sken 33340603Sken softc->flags |= PT_FLAG_DEVICE_INVALID; 33440603Sken 33540603Sken /* 33640603Sken * Return all queued I/O with ENXIO. 33740603Sken * XXX Handle any transactions queued to the card 33840603Sken * with XPT_ABORT_CCB. 33940603Sken */ 340112946Sphk bioq_flush(&softc->bio_queue, NULL, ENXIO); 34140603Sken 342164906Smjacob xpt_print(periph->path, "lost device\n"); 34340603Sken} 34440603Sken 34540603Skenstatic void 34639213Sgibbsptdtor(struct cam_periph *periph) 34739213Sgibbs{ 34840603Sken struct pt_softc *softc; 34940603Sken 35040603Sken softc = (struct pt_softc *)periph->softc; 35140603Sken 352187028Strasz xpt_print(periph->path, "removing device entry\n"); 353112006Sphk devstat_remove_entry(softc->device_stats); 354187028Strasz cam_periph_unlock(periph); 35553257Sken destroy_dev(softc->dev); 356187028Strasz cam_periph_lock(periph); 35740603Sken free(softc, M_DEVBUF); 35839213Sgibbs} 35939213Sgibbs 36039213Sgibbsstatic void 36139213Sgibbsptasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg) 36239213Sgibbs{ 36339213Sgibbs struct cam_periph *periph; 36439213Sgibbs 36539213Sgibbs periph = (struct cam_periph *)callback_arg; 36639213Sgibbs switch (code) { 36739213Sgibbs case AC_FOUND_DEVICE: 36839213Sgibbs { 36939213Sgibbs struct ccb_getdev *cgd; 37039213Sgibbs cam_status status; 37139213Sgibbs 37239213Sgibbs cgd = (struct ccb_getdev *)arg; 37379177Smjacob if (cgd == NULL) 37479177Smjacob break; 37539213Sgibbs 376195534Sscottl if (cgd->protocol != PROTO_SCSI) 377195534Sscottl break; 378195534Sscottl 37956148Smjacob if (SID_TYPE(&cgd->inq_data) != T_PROCESSOR) 38039213Sgibbs break; 38139213Sgibbs 38239213Sgibbs /* 38339213Sgibbs * Allocate a peripheral instance for 38439213Sgibbs * this device and start the probe 38539213Sgibbs * process. 38639213Sgibbs */ 38740603Sken status = cam_periph_alloc(ptctor, ptoninvalidate, ptdtor, 38840603Sken ptstart, "pt", CAM_PERIPH_BIO, 38940603Sken cgd->ccb_h.path, ptasync, 39040603Sken AC_FOUND_DEVICE, cgd); 39139213Sgibbs 39239213Sgibbs if (status != CAM_REQ_CMP 39339213Sgibbs && status != CAM_REQ_INPROG) 39439213Sgibbs printf("ptasync: Unable to attach to new device " 39539213Sgibbs "due to status 0x%x\n", status); 39639213Sgibbs break; 39739213Sgibbs } 39839213Sgibbs case AC_SENT_BDR: 39939213Sgibbs case AC_BUS_RESET: 40039213Sgibbs { 40139213Sgibbs struct pt_softc *softc; 40239213Sgibbs struct ccb_hdr *ccbh; 40339213Sgibbs 40439213Sgibbs softc = (struct pt_softc *)periph->softc; 40539213Sgibbs /* 40639213Sgibbs * Don't fail on the expected unit attention 40739213Sgibbs * that will occur. 40839213Sgibbs */ 40939213Sgibbs softc->flags |= PT_FLAG_RETRY_UA; 41071999Sphk LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le) 41139213Sgibbs ccbh->ccb_state |= PT_CCB_RETRY_UA; 41239213Sgibbs } 413115562Sphk /* FALLTHROUGH */ 41439213Sgibbs default: 41547413Sgibbs cam_periph_async(periph, code, path, arg); 41639213Sgibbs break; 41739213Sgibbs } 41839213Sgibbs} 41939213Sgibbs 42039213Sgibbsstatic void 42139213Sgibbsptstart(struct cam_periph *periph, union ccb *start_ccb) 42239213Sgibbs{ 42339213Sgibbs struct pt_softc *softc; 42459249Sphk struct bio *bp; 42539213Sgibbs 42639213Sgibbs softc = (struct pt_softc *)periph->softc; 42739213Sgibbs 42839213Sgibbs /* 42939213Sgibbs * See if there is a buf with work for us to do.. 43039213Sgibbs */ 43159249Sphk bp = bioq_first(&softc->bio_queue); 43239213Sgibbs if (periph->immediate_priority <= periph->pinfo.priority) { 43339213Sgibbs CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE, 43439213Sgibbs ("queuing for immediate ccb\n")); 43539213Sgibbs start_ccb->ccb_h.ccb_state = PT_CCB_WAITING; 43639213Sgibbs SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h, 43739213Sgibbs periph_links.sle); 43839213Sgibbs periph->immediate_priority = CAM_PRIORITY_NONE; 43939213Sgibbs wakeup(&periph->ccb_list); 44039213Sgibbs } else if (bp == NULL) { 44139213Sgibbs xpt_release_ccb(start_ccb); 44239213Sgibbs } else { 44359249Sphk bioq_remove(&softc->bio_queue, bp); 44439213Sgibbs 445112260Sphk devstat_start_transaction_bio(softc->device_stats, bp); 44639213Sgibbs 44739213Sgibbs scsi_send_receive(&start_ccb->csio, 44839213Sgibbs /*retries*/4, 44939213Sgibbs ptdone, 45039213Sgibbs MSG_SIMPLE_Q_TAG, 45159249Sphk bp->bio_cmd == BIO_READ, 45239213Sgibbs /*byte2*/0, 45359249Sphk bp->bio_bcount, 45459249Sphk bp->bio_data, 45539213Sgibbs /*sense_len*/SSD_FULL_SIZE, 45650073Sken /*timeout*/softc->io_timeout); 45739213Sgibbs 45876192Sken start_ccb->ccb_h.ccb_state = PT_CCB_BUFFER_IO_UA; 45939213Sgibbs 46039213Sgibbs /* 46139213Sgibbs * Block out any asyncronous callbacks 46239213Sgibbs * while we touch the pending ccb list. 46339213Sgibbs */ 46439213Sgibbs LIST_INSERT_HEAD(&softc->pending_ccbs, &start_ccb->ccb_h, 46539213Sgibbs periph_links.le); 46639213Sgibbs 46739213Sgibbs start_ccb->ccb_h.ccb_bp = bp; 46859249Sphk bp = bioq_first(&softc->bio_queue); 46939213Sgibbs 47039213Sgibbs xpt_action(start_ccb); 47139213Sgibbs 47239213Sgibbs if (bp != NULL) { 47339213Sgibbs /* Have more work to do, so ensure we stay scheduled */ 474198382Smav xpt_schedule(periph, CAM_PRIORITY_NORMAL); 47539213Sgibbs } 47639213Sgibbs } 47739213Sgibbs} 47839213Sgibbs 47939213Sgibbsstatic void 48039213Sgibbsptdone(struct cam_periph *periph, union ccb *done_ccb) 48139213Sgibbs{ 48239213Sgibbs struct pt_softc *softc; 48339213Sgibbs struct ccb_scsiio *csio; 48439213Sgibbs 48539213Sgibbs softc = (struct pt_softc *)periph->softc; 48639213Sgibbs csio = &done_ccb->csio; 48739213Sgibbs switch (csio->ccb_h.ccb_state) { 48839213Sgibbs case PT_CCB_BUFFER_IO: 48939213Sgibbs case PT_CCB_BUFFER_IO_UA: 49039213Sgibbs { 49159249Sphk struct bio *bp; 49239213Sgibbs 49359249Sphk bp = (struct bio *)done_ccb->ccb_h.ccb_bp; 49439213Sgibbs if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 49539213Sgibbs int error; 49639213Sgibbs int sf; 49739213Sgibbs 49839213Sgibbs if ((csio->ccb_h.ccb_state & PT_CCB_RETRY_UA) != 0) 49939213Sgibbs sf = SF_RETRY_UA; 50039213Sgibbs else 50139213Sgibbs sf = 0; 50239213Sgibbs 50374840Sken error = pterror(done_ccb, CAM_RETRY_SELTO, sf); 50474840Sken if (error == ERESTART) { 50539213Sgibbs /* 50639213Sgibbs * A retry was scheuled, so 50739213Sgibbs * just return. 50839213Sgibbs */ 50939213Sgibbs return; 51039213Sgibbs } 51139213Sgibbs if (error != 0) { 51239213Sgibbs if (error == ENXIO) { 51339213Sgibbs /* 51439213Sgibbs * Catastrophic error. Mark our device 51539213Sgibbs * as invalid. 51639213Sgibbs */ 517164906Smjacob xpt_print(periph->path, 518164906Smjacob "Invalidating device\n"); 51939213Sgibbs softc->flags |= PT_FLAG_DEVICE_INVALID; 52039213Sgibbs } 52139213Sgibbs 52239213Sgibbs /* 52339213Sgibbs * return all queued I/O with EIO, so that 52439213Sgibbs * the client can retry these I/Os in the 52539213Sgibbs * proper order should it attempt to recover. 52639213Sgibbs */ 527112946Sphk bioq_flush(&softc->bio_queue, NULL, EIO); 52859249Sphk bp->bio_error = error; 52959249Sphk bp->bio_resid = bp->bio_bcount; 53059249Sphk bp->bio_flags |= BIO_ERROR; 53139213Sgibbs } else { 53259249Sphk bp->bio_resid = csio->resid; 53359249Sphk bp->bio_error = 0; 53459249Sphk if (bp->bio_resid != 0) { 53539213Sgibbs /* Short transfer ??? */ 53659249Sphk bp->bio_flags |= BIO_ERROR; 53739213Sgibbs } 53839213Sgibbs } 53939213Sgibbs if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 54039213Sgibbs cam_release_devq(done_ccb->ccb_h.path, 54139213Sgibbs /*relsim_flags*/0, 54239213Sgibbs /*reduction*/0, 54339213Sgibbs /*timeout*/0, 54439213Sgibbs /*getcount_only*/0); 54539213Sgibbs } else { 54659249Sphk bp->bio_resid = csio->resid; 54759249Sphk if (bp->bio_resid != 0) 54859249Sphk bp->bio_flags |= BIO_ERROR; 54939213Sgibbs } 55039213Sgibbs 55139213Sgibbs /* 55239213Sgibbs * Block out any asyncronous callbacks 55339213Sgibbs * while we touch the pending ccb list. 55439213Sgibbs */ 55539213Sgibbs LIST_REMOVE(&done_ccb->ccb_h, periph_links.le); 55639213Sgibbs 557112006Sphk biofinish(bp, softc->device_stats, 0); 55839213Sgibbs break; 55939213Sgibbs } 56039213Sgibbs case PT_CCB_WAITING: 56139213Sgibbs /* Caller will release the CCB */ 56239213Sgibbs wakeup(&done_ccb->ccb_h.cbfcnp); 56339213Sgibbs return; 56439213Sgibbs } 56539213Sgibbs xpt_release_ccb(done_ccb); 56639213Sgibbs} 56739213Sgibbs 56839213Sgibbsstatic int 56939213Sgibbspterror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 57039213Sgibbs{ 57139213Sgibbs struct pt_softc *softc; 57239213Sgibbs struct cam_periph *periph; 57339213Sgibbs 57439213Sgibbs periph = xpt_path_periph(ccb->ccb_h.path); 57539213Sgibbs softc = (struct pt_softc *)periph->softc; 57639213Sgibbs 57739213Sgibbs return(cam_periph_error(ccb, cam_flags, sense_flags, 57839213Sgibbs &softc->saved_ccb)); 57939213Sgibbs} 58039213Sgibbs 58150073Skenstatic int 582130585Sphkptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td) 58350073Sken{ 58450073Sken struct cam_periph *periph; 58550073Sken struct pt_softc *softc; 586168752Sscottl int error = 0; 58750073Sken 588101940Snjl periph = (struct cam_periph *)dev->si_drv1; 58950073Sken if (periph == NULL) 59050073Sken return(ENXIO); 59150073Sken 59250073Sken softc = (struct pt_softc *)periph->softc; 59350073Sken 594168752Sscottl cam_periph_lock(periph); 59550073Sken 59650073Sken switch(cmd) { 59750073Sken case PTIOCGETTIMEOUT: 59850073Sken if (softc->io_timeout >= 1000) 59950073Sken *(int *)addr = softc->io_timeout / 1000; 60050073Sken else 60150073Sken *(int *)addr = 0; 60250073Sken break; 60350073Sken case PTIOCSETTIMEOUT: 60450073Sken if (*(int *)addr < 1) { 60550073Sken error = EINVAL; 60650073Sken break; 60750073Sken } 60850073Sken 60950073Sken softc->io_timeout = *(int *)addr * 1000; 61050073Sken 61150073Sken break; 61250073Sken default: 61350073Sken error = cam_periph_ioctl(periph, cmd, addr, pterror); 61450073Sken break; 61550073Sken } 61650073Sken 61750073Sken cam_periph_unlock(periph); 61850073Sken 61950073Sken return(error); 62050073Sken} 62150073Sken 62239213Sgibbsvoid 62339213Sgibbsscsi_send_receive(struct ccb_scsiio *csio, u_int32_t retries, 62439213Sgibbs void (*cbfcnp)(struct cam_periph *, union ccb *), 62539213Sgibbs u_int tag_action, int readop, u_int byte2, 62639213Sgibbs u_int32_t xfer_len, u_int8_t *data_ptr, u_int8_t sense_len, 62739213Sgibbs u_int32_t timeout) 62839213Sgibbs{ 62939213Sgibbs struct scsi_send_receive *scsi_cmd; 63039213Sgibbs 63139213Sgibbs scsi_cmd = (struct scsi_send_receive *)&csio->cdb_io.cdb_bytes; 63239213Sgibbs scsi_cmd->opcode = readop ? RECEIVE : SEND; 63339213Sgibbs scsi_cmd->byte2 = byte2; 63439213Sgibbs scsi_ulto3b(xfer_len, scsi_cmd->xfer_len); 63539213Sgibbs scsi_cmd->control = 0; 63639213Sgibbs 63739213Sgibbs cam_fill_csio(csio, 63839213Sgibbs retries, 63939213Sgibbs cbfcnp, 64039213Sgibbs /*flags*/readop ? CAM_DIR_IN : CAM_DIR_OUT, 64139213Sgibbs tag_action, 64239213Sgibbs data_ptr, 64339213Sgibbs xfer_len, 64439213Sgibbs sense_len, 64539213Sgibbs sizeof(*scsi_cmd), 64639213Sgibbs timeout); 64739213Sgibbs} 648