scsi_pt.c revision 287289
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 287289 2015-08-29 11:21:20Z 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	if (periph == NULL)
17739213Sgibbs		return (ENXIO);
17839213Sgibbs
17939213Sgibbs	softc = (struct pt_softc *)periph->softc;
18039213Sgibbs
181168752Sscottl	cam_periph_lock(periph);
18239213Sgibbs
18339213Sgibbs	softc->flags &= ~PT_FLAG_OPEN;
184236138Sken	cam_periph_release_locked(periph);
18539213Sgibbs	cam_periph_unlock(periph);
18639213Sgibbs	return (0);
18739213Sgibbs}
18839213Sgibbs
18939213Sgibbs/*
19039213Sgibbs * Actually translate the requested transfer into one the physical driver
19139213Sgibbs * can understand.  The transfer is described by a buf and will include
19239213Sgibbs * only one physical transfer.
19339213Sgibbs */
19439213Sgibbsstatic void
19559249Sphkptstrategy(struct bio *bp)
19639213Sgibbs{
19739213Sgibbs	struct cam_periph *periph;
19839213Sgibbs	struct pt_softc *softc;
19939213Sgibbs
200101940Snjl	periph = (struct cam_periph *)bp->bio_dev->si_drv1;
20176362Sphk	bp->bio_resid = bp->bio_bcount;
20239213Sgibbs	if (periph == NULL) {
20376362Sphk		biofinish(bp, NULL, ENXIO);
20476362Sphk		return;
20539213Sgibbs	}
206168752Sscottl	cam_periph_lock(periph);
20739213Sgibbs	softc = (struct pt_softc *)periph->softc;
20839213Sgibbs
20939213Sgibbs	/*
21039213Sgibbs	 * If the device has been made invalid, error out
21139213Sgibbs	 */
21239213Sgibbs	if ((softc->flags & PT_FLAG_DEVICE_INVALID)) {
213168752Sscottl		cam_periph_unlock(periph);
21476362Sphk		biofinish(bp, NULL, ENXIO);
21576362Sphk		return;
21639213Sgibbs	}
21739213Sgibbs
21839213Sgibbs	/*
21939213Sgibbs	 * Place it in the queue of disk activities for this disk
22039213Sgibbs	 */
22159249Sphk	bioq_insert_tail(&softc->bio_queue, bp);
22239213Sgibbs
22339213Sgibbs	/*
22439213Sgibbs	 * Schedule ourselves for performing the work.
22539213Sgibbs	 */
226198382Smav	xpt_schedule(periph, CAM_PRIORITY_NORMAL);
227168752Sscottl	cam_periph_unlock(periph);
22839213Sgibbs
22939213Sgibbs	return;
23039213Sgibbs}
23139213Sgibbs
23239213Sgibbsstatic void
23339213Sgibbsptinit(void)
23439213Sgibbs{
23539213Sgibbs	cam_status status;
23639213Sgibbs
23739213Sgibbs	/*
23839213Sgibbs	 * Install a global async callback.  This callback will
23939213Sgibbs	 * receive async callbacks like "new device found".
24039213Sgibbs	 */
241169605Sscottl	status = xpt_register_async(AC_FOUND_DEVICE, ptasync, NULL, NULL);
24239213Sgibbs
24339213Sgibbs	if (status != CAM_REQ_CMP) {
24439213Sgibbs		printf("pt: Failed to attach master async callback "
24539213Sgibbs		       "due to status 0x%x!\n", status);
24639213Sgibbs	}
24739213Sgibbs}
24839213Sgibbs
24939213Sgibbsstatic cam_status
25039213Sgibbsptctor(struct cam_periph *periph, void *arg)
25139213Sgibbs{
25239213Sgibbs	struct pt_softc *softc;
25339213Sgibbs	struct ccb_getdev *cgd;
254220644Smav	struct ccb_pathinq cpi;
25539213Sgibbs
25639213Sgibbs	cgd = (struct ccb_getdev *)arg;
25739213Sgibbs	if (cgd == NULL) {
25839213Sgibbs		printf("ptregister: no getdev CCB, can't register device\n");
25939213Sgibbs		return(CAM_REQ_CMP_ERR);
26039213Sgibbs	}
26139213Sgibbs
26239213Sgibbs	softc = (struct pt_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT);
26339213Sgibbs
26439213Sgibbs	if (softc == NULL) {
26539213Sgibbs		printf("daregister: Unable to probe new device. "
26639213Sgibbs		       "Unable to allocate softc\n");
26739213Sgibbs		return(CAM_REQ_CMP_ERR);
26839213Sgibbs	}
26939213Sgibbs
27039213Sgibbs	bzero(softc, sizeof(*softc));
27139213Sgibbs	LIST_INIT(&softc->pending_ccbs);
27239213Sgibbs	softc->state = PT_STATE_NORMAL;
27359249Sphk	bioq_init(&softc->bio_queue);
27439213Sgibbs
27550073Sken	softc->io_timeout = SCSI_PT_DEFAULT_TIMEOUT * 1000;
27650073Sken
27739213Sgibbs	periph->softc = softc;
278220644Smav
279220644Smav	bzero(&cpi, sizeof(cpi));
280220644Smav	xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
281220644Smav	cpi.ccb_h.func_code = XPT_PATH_INQ;
282220644Smav	xpt_action((union ccb *)&cpi);
283220644Smav
284169605Sscottl	cam_periph_unlock(periph);
285112006Sphk	softc->device_stats = devstat_new_entry("pt",
28639213Sgibbs			  periph->unit_number, 0,
28739213Sgibbs			  DEVSTAT_NO_BLOCKSIZE,
288220644Smav			  SID_TYPE(&cgd->inq_data) |
289220644Smav			  XPORT_DEVSTAT_TYPE(cpi.transport),
29043819Sken			  DEVSTAT_PRIORITY_OTHER);
29139213Sgibbs
29253257Sken	softc->dev = make_dev(&pt_cdevsw, periph->unit_number, UID_ROOT,
29353257Sken			      GID_OPERATOR, 0600, "%s%d", periph->periph_name,
29453257Sken			      periph->unit_number);
295168872Sscottl	cam_periph_lock(periph);
296101940Snjl	softc->dev->si_drv1 = periph;
297101940Snjl
29839213Sgibbs	/*
29939213Sgibbs	 * Add async callbacks for bus reset and
30039213Sgibbs	 * bus device reset calls.  I don't bother
30139213Sgibbs	 * checking if this fails as, in most cases,
30239213Sgibbs	 * the system will function just fine without
30339213Sgibbs	 * them and the only alternative would be to
30439213Sgibbs	 * not attach the device on failure.
30539213Sgibbs	 */
306169605Sscottl	xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE,
307169605Sscottl			   ptasync, periph, periph->path);
30839213Sgibbs
30939213Sgibbs	/* Tell the user we've attached to the device */
31039213Sgibbs	xpt_announce_periph(periph, NULL);
31139213Sgibbs
31239213Sgibbs	return(CAM_REQ_CMP);
31339213Sgibbs}
31439213Sgibbs
31539213Sgibbsstatic void
31640603Skenptoninvalidate(struct cam_periph *periph)
31740603Sken{
31840603Sken	struct pt_softc *softc;
31940603Sken
32040603Sken	softc = (struct pt_softc *)periph->softc;
32140603Sken
32240603Sken	/*
32340603Sken	 * De-register any async callbacks.
32440603Sken	 */
325169605Sscottl	xpt_register_async(0, ptasync, periph, periph->path);
32640603Sken
32740603Sken	softc->flags |= PT_FLAG_DEVICE_INVALID;
32840603Sken
32940603Sken	/*
33040603Sken	 * Return all queued I/O with ENXIO.
33140603Sken	 * XXX Handle any transactions queued to the card
33240603Sken	 *     with XPT_ABORT_CCB.
33340603Sken	 */
334112946Sphk	bioq_flush(&softc->bio_queue, NULL, ENXIO);
33540603Sken}
33640603Sken
33740603Skenstatic void
33839213Sgibbsptdtor(struct cam_periph *periph)
33939213Sgibbs{
34040603Sken	struct pt_softc *softc;
34140603Sken
34240603Sken	softc = (struct pt_softc *)periph->softc;
34340603Sken
344112006Sphk	devstat_remove_entry(softc->device_stats);
345187028Strasz	cam_periph_unlock(periph);
34653257Sken	destroy_dev(softc->dev);
347187028Strasz	cam_periph_lock(periph);
34840603Sken	free(softc, M_DEVBUF);
34939213Sgibbs}
35039213Sgibbs
35139213Sgibbsstatic void
35239213Sgibbsptasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
35339213Sgibbs{
35439213Sgibbs	struct cam_periph *periph;
35539213Sgibbs
35639213Sgibbs	periph = (struct cam_periph *)callback_arg;
35739213Sgibbs	switch (code) {
35839213Sgibbs	case AC_FOUND_DEVICE:
35939213Sgibbs	{
36039213Sgibbs		struct ccb_getdev *cgd;
36139213Sgibbs		cam_status status;
36239213Sgibbs
36339213Sgibbs		cgd = (struct ccb_getdev *)arg;
36479177Smjacob		if (cgd == NULL)
36579177Smjacob			break;
36639213Sgibbs
367195534Sscottl		if (cgd->protocol != PROTO_SCSI)
368195534Sscottl			break;
369287289Smav		if (SID_QUAL(&cgd->inq_data) != SID_QUAL_LU_CONNECTED)
370287289Smav			break;
37156148Smjacob		if (SID_TYPE(&cgd->inq_data) != T_PROCESSOR)
37239213Sgibbs			break;
37339213Sgibbs
37439213Sgibbs		/*
37539213Sgibbs		 * Allocate a peripheral instance for
37639213Sgibbs		 * this device and start the probe
37739213Sgibbs		 * process.
37839213Sgibbs		 */
37940603Sken		status = cam_periph_alloc(ptctor, ptoninvalidate, ptdtor,
38040603Sken					  ptstart, "pt", CAM_PERIPH_BIO,
381256843Smav					  path, ptasync,
38240603Sken					  AC_FOUND_DEVICE, cgd);
38339213Sgibbs
38439213Sgibbs		if (status != CAM_REQ_CMP
38539213Sgibbs		 && status != CAM_REQ_INPROG)
38639213Sgibbs			printf("ptasync: Unable to attach to new device "
38739213Sgibbs				"due to status 0x%x\n", status);
38839213Sgibbs		break;
38939213Sgibbs	}
39039213Sgibbs	case AC_SENT_BDR:
39139213Sgibbs	case AC_BUS_RESET:
39239213Sgibbs	{
39339213Sgibbs		struct pt_softc *softc;
39439213Sgibbs		struct ccb_hdr *ccbh;
39539213Sgibbs
39639213Sgibbs		softc = (struct pt_softc *)periph->softc;
39739213Sgibbs		/*
39839213Sgibbs		 * Don't fail on the expected unit attention
39939213Sgibbs		 * that will occur.
40039213Sgibbs		 */
40139213Sgibbs		softc->flags |= PT_FLAG_RETRY_UA;
40271999Sphk		LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
40339213Sgibbs			ccbh->ccb_state |= PT_CCB_RETRY_UA;
40439213Sgibbs	}
405115562Sphk	/* FALLTHROUGH */
40639213Sgibbs	default:
40747413Sgibbs		cam_periph_async(periph, code, path, arg);
40839213Sgibbs		break;
40939213Sgibbs	}
41039213Sgibbs}
41139213Sgibbs
41239213Sgibbsstatic void
41339213Sgibbsptstart(struct cam_periph *periph, union ccb *start_ccb)
41439213Sgibbs{
41539213Sgibbs	struct pt_softc *softc;
41659249Sphk	struct bio *bp;
41739213Sgibbs
41839213Sgibbs	softc = (struct pt_softc *)periph->softc;
41939213Sgibbs
420236602Smav	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("ptstart\n"));
421236602Smav
42239213Sgibbs	/*
42339213Sgibbs	 * See if there is a buf with work for us to do..
42439213Sgibbs	 */
42559249Sphk	bp = bioq_first(&softc->bio_queue);
426256843Smav	if (bp == NULL) {
42739213Sgibbs		xpt_release_ccb(start_ccb);
42839213Sgibbs	} else {
42959249Sphk		bioq_remove(&softc->bio_queue, bp);
43039213Sgibbs
431112260Sphk		devstat_start_transaction_bio(softc->device_stats, bp);
43239213Sgibbs
43339213Sgibbs		scsi_send_receive(&start_ccb->csio,
43439213Sgibbs				  /*retries*/4,
43539213Sgibbs				  ptdone,
43639213Sgibbs				  MSG_SIMPLE_Q_TAG,
43759249Sphk				  bp->bio_cmd == BIO_READ,
43839213Sgibbs				  /*byte2*/0,
43959249Sphk				  bp->bio_bcount,
44059249Sphk				  bp->bio_data,
44139213Sgibbs				  /*sense_len*/SSD_FULL_SIZE,
44250073Sken				  /*timeout*/softc->io_timeout);
44339213Sgibbs
44476192Sken		start_ccb->ccb_h.ccb_state = PT_CCB_BUFFER_IO_UA;
44539213Sgibbs
44639213Sgibbs		/*
447250460Seadler		 * Block out any asynchronous callbacks
44839213Sgibbs		 * while we touch the pending ccb list.
44939213Sgibbs		 */
45039213Sgibbs		LIST_INSERT_HEAD(&softc->pending_ccbs, &start_ccb->ccb_h,
45139213Sgibbs				 periph_links.le);
45239213Sgibbs
45339213Sgibbs		start_ccb->ccb_h.ccb_bp = bp;
45459249Sphk		bp = bioq_first(&softc->bio_queue);
45539213Sgibbs
45639213Sgibbs		xpt_action(start_ccb);
45739213Sgibbs
45839213Sgibbs		if (bp != NULL) {
45939213Sgibbs			/* Have more work to do, so ensure we stay scheduled */
460198382Smav			xpt_schedule(periph, CAM_PRIORITY_NORMAL);
46139213Sgibbs		}
46239213Sgibbs	}
46339213Sgibbs}
46439213Sgibbs
46539213Sgibbsstatic void
46639213Sgibbsptdone(struct cam_periph *periph, union ccb *done_ccb)
46739213Sgibbs{
46839213Sgibbs	struct pt_softc *softc;
46939213Sgibbs	struct ccb_scsiio *csio;
47039213Sgibbs
47139213Sgibbs	softc = (struct pt_softc *)periph->softc;
472236602Smav
473236602Smav	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("ptdone\n"));
474236602Smav
47539213Sgibbs	csio = &done_ccb->csio;
47639213Sgibbs	switch (csio->ccb_h.ccb_state) {
47739213Sgibbs	case PT_CCB_BUFFER_IO:
47839213Sgibbs	case PT_CCB_BUFFER_IO_UA:
47939213Sgibbs	{
48059249Sphk		struct bio *bp;
48139213Sgibbs
48259249Sphk		bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
48339213Sgibbs		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
48439213Sgibbs			int error;
48539213Sgibbs			int sf;
48639213Sgibbs
48739213Sgibbs			if ((csio->ccb_h.ccb_state & PT_CCB_RETRY_UA) != 0)
48839213Sgibbs				sf = SF_RETRY_UA;
48939213Sgibbs			else
49039213Sgibbs				sf = 0;
49139213Sgibbs
49274840Sken			error = pterror(done_ccb, CAM_RETRY_SELTO, sf);
49374840Sken			if (error == ERESTART) {
49439213Sgibbs				/*
49539213Sgibbs				 * A retry was scheuled, so
49639213Sgibbs				 * just return.
49739213Sgibbs				 */
49839213Sgibbs				return;
49939213Sgibbs			}
50039213Sgibbs			if (error != 0) {
50139213Sgibbs				if (error == ENXIO) {
50239213Sgibbs					/*
50339213Sgibbs					 * Catastrophic error.  Mark our device
50439213Sgibbs					 * as invalid.
50539213Sgibbs					 */
506164906Smjacob					xpt_print(periph->path,
507164906Smjacob					    "Invalidating device\n");
50839213Sgibbs					softc->flags |= PT_FLAG_DEVICE_INVALID;
50939213Sgibbs				}
51039213Sgibbs
51139213Sgibbs				/*
51239213Sgibbs				 * return all queued I/O with EIO, so that
51339213Sgibbs				 * the client can retry these I/Os in the
51439213Sgibbs				 * proper order should it attempt to recover.
51539213Sgibbs				 */
516112946Sphk				bioq_flush(&softc->bio_queue, NULL, EIO);
51759249Sphk				bp->bio_error = error;
51859249Sphk				bp->bio_resid = bp->bio_bcount;
51959249Sphk				bp->bio_flags |= BIO_ERROR;
52039213Sgibbs			} else {
52159249Sphk				bp->bio_resid = csio->resid;
52259249Sphk				bp->bio_error = 0;
52359249Sphk				if (bp->bio_resid != 0) {
52439213Sgibbs					/* Short transfer ??? */
52559249Sphk					bp->bio_flags |= BIO_ERROR;
52639213Sgibbs				}
52739213Sgibbs			}
52839213Sgibbs			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
52939213Sgibbs				cam_release_devq(done_ccb->ccb_h.path,
53039213Sgibbs						 /*relsim_flags*/0,
53139213Sgibbs						 /*reduction*/0,
53239213Sgibbs						 /*timeout*/0,
53339213Sgibbs						 /*getcount_only*/0);
53439213Sgibbs		} else {
53559249Sphk			bp->bio_resid = csio->resid;
53659249Sphk			if (bp->bio_resid != 0)
53759249Sphk				bp->bio_flags |= BIO_ERROR;
53839213Sgibbs		}
53939213Sgibbs
54039213Sgibbs		/*
541250460Seadler		 * Block out any asynchronous callbacks
54239213Sgibbs		 * while we touch the pending ccb list.
54339213Sgibbs		 */
54439213Sgibbs		LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
54539213Sgibbs
546112006Sphk		biofinish(bp, softc->device_stats, 0);
54739213Sgibbs		break;
54839213Sgibbs	}
54939213Sgibbs	}
55039213Sgibbs	xpt_release_ccb(done_ccb);
55139213Sgibbs}
55239213Sgibbs
55339213Sgibbsstatic int
55439213Sgibbspterror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
55539213Sgibbs{
55639213Sgibbs	struct pt_softc	  *softc;
55739213Sgibbs	struct cam_periph *periph;
55839213Sgibbs
55939213Sgibbs	periph = xpt_path_periph(ccb->ccb_h.path);
56039213Sgibbs	softc = (struct pt_softc *)periph->softc;
56139213Sgibbs
56239213Sgibbs	return(cam_periph_error(ccb, cam_flags, sense_flags,
56339213Sgibbs				&softc->saved_ccb));
56439213Sgibbs}
56539213Sgibbs
56650073Skenstatic int
567130585Sphkptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
56850073Sken{
56950073Sken	struct cam_periph *periph;
57050073Sken	struct pt_softc *softc;
571168752Sscottl	int error = 0;
57250073Sken
573101940Snjl	periph = (struct cam_periph *)dev->si_drv1;
57450073Sken	if (periph == NULL)
57550073Sken		return(ENXIO);
57650073Sken
57750073Sken	softc = (struct pt_softc *)periph->softc;
57850073Sken
579168752Sscottl	cam_periph_lock(periph);
58050073Sken
58150073Sken	switch(cmd) {
58250073Sken	case PTIOCGETTIMEOUT:
58350073Sken		if (softc->io_timeout >= 1000)
58450073Sken			*(int *)addr = softc->io_timeout / 1000;
58550073Sken		else
58650073Sken			*(int *)addr = 0;
58750073Sken		break;
58850073Sken	case PTIOCSETTIMEOUT:
58950073Sken		if (*(int *)addr < 1) {
59050073Sken			error = EINVAL;
59150073Sken			break;
59250073Sken		}
59350073Sken
59450073Sken		softc->io_timeout = *(int *)addr * 1000;
59550073Sken
59650073Sken		break;
59750073Sken	default:
59850073Sken		error = cam_periph_ioctl(periph, cmd, addr, pterror);
59950073Sken		break;
60050073Sken	}
60150073Sken
60250073Sken	cam_periph_unlock(periph);
60350073Sken
60450073Sken	return(error);
60550073Sken}
60650073Sken
60739213Sgibbsvoid
60839213Sgibbsscsi_send_receive(struct ccb_scsiio *csio, u_int32_t retries,
60939213Sgibbs		  void (*cbfcnp)(struct cam_periph *, union ccb *),
61039213Sgibbs		  u_int tag_action, int readop, u_int byte2,
61139213Sgibbs		  u_int32_t xfer_len, u_int8_t *data_ptr, u_int8_t sense_len,
61239213Sgibbs		  u_int32_t timeout)
61339213Sgibbs{
61439213Sgibbs	struct scsi_send_receive *scsi_cmd;
61539213Sgibbs
61639213Sgibbs	scsi_cmd = (struct scsi_send_receive *)&csio->cdb_io.cdb_bytes;
61739213Sgibbs	scsi_cmd->opcode = readop ? RECEIVE : SEND;
61839213Sgibbs	scsi_cmd->byte2 = byte2;
61939213Sgibbs	scsi_ulto3b(xfer_len, scsi_cmd->xfer_len);
62039213Sgibbs	scsi_cmd->control = 0;
62139213Sgibbs
62239213Sgibbs	cam_fill_csio(csio,
62339213Sgibbs		      retries,
62439213Sgibbs		      cbfcnp,
62539213Sgibbs		      /*flags*/readop ? CAM_DIR_IN : CAM_DIR_OUT,
62639213Sgibbs		      tag_action,
62739213Sgibbs		      data_ptr,
62839213Sgibbs		      xfer_len,
62939213Sgibbs		      sense_len,
63039213Sgibbs		      sizeof(*scsi_cmd),
63139213Sgibbs		      timeout);
63239213Sgibbs}
633