xref: /freebsd/sys/cam/scsi/scsi_pt.c (revision 2ad872c5794e4c26fdf6ed219ad3f09ca0d5304a)
1 /*
2  * Implementation of SCSI Processor Target Peripheral driver for CAM.
3  *
4  * Copyright (c) 1998 Justin T. Gibbs.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions, and the following disclaimer,
12  *    without modification, immediately at the beginning of the file.
13  * 2. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  *      $Id: scsi_pt.c,v 1.3 1998/10/22 22:16:56 ken Exp $
29  */
30 
31 #include <sys/param.h>
32 #include <sys/queue.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/types.h>
36 #include <sys/buf.h>
37 #include <sys/devicestat.h>
38 #include <sys/malloc.h>
39 #include <sys/conf.h>
40 
41 #include <cam/cam.h>
42 #include <cam/cam_ccb.h>
43 #include <cam/cam_extend.h>
44 #include <cam/cam_periph.h>
45 #include <cam/cam_xpt_periph.h>
46 #include <cam/cam_debug.h>
47 
48 #include <cam/scsi/scsi_all.h>
49 #include <cam/scsi/scsi_message.h>
50 #include <cam/scsi/scsi_pt.h>
51 
52 typedef enum {
53 	PT_STATE_PROBE,
54 	PT_STATE_NORMAL
55 } pt_state;
56 
57 typedef enum {
58 	PT_FLAG_NONE		= 0x00,
59 	PT_FLAG_OPEN		= 0x01,
60 	PT_FLAG_DEVICE_INVALID	= 0x02,
61 	PT_FLAG_RETRY_UA	= 0x04
62 } pt_flags;
63 
64 typedef enum {
65 	PT_CCB_BUFFER_IO	= 0x01,
66 	PT_CCB_WAITING		= 0x02,
67 	PT_CCB_RETRY_UA		= 0x04,
68 	PT_CCB_BUFFER_IO_UA	= PT_CCB_BUFFER_IO|PT_CCB_RETRY_UA
69 } pt_ccb_state;
70 
71 /* Offsets into our private area for storing information */
72 #define ccb_state	ppriv_field0
73 #define ccb_bp		ppriv_ptr1
74 
75 struct pt_softc {
76 	struct	 buf_queue_head buf_queue;
77 	struct	 devstat device_stats;
78 	LIST_HEAD(, ccb_hdr) pending_ccbs;
79 	pt_state state;
80 	pt_flags flags;
81 	union	 ccb saved_ccb;
82 };
83 
84 static	d_open_t	ptopen;
85 static	d_read_t	ptread;
86 static	d_write_t	ptwrite;
87 static	d_close_t	ptclose;
88 static	d_strategy_t	ptstrategy;
89 static	periph_init_t	ptinit;
90 static	void		ptasync(void *callback_arg, u_int32_t code,
91 				struct cam_path *path, void *arg);
92 static	periph_ctor_t	ptctor;
93 static	periph_oninv_t	ptoninvalidate;
94 static	periph_dtor_t	ptdtor;
95 static	periph_start_t	ptstart;
96 static	void		ptdone(struct cam_periph *periph,
97 			       union ccb *done_ccb);
98 static  int		pterror(union ccb *ccb, u_int32_t cam_flags,
99 				u_int32_t sense_flags);
100 
101 void	scsi_send_receive(struct ccb_scsiio *csio, u_int32_t retries,
102 			  void (*cbfcnp)(struct cam_periph *, union ccb *),
103 			  u_int tag_action, int readop, u_int byte2,
104 			  u_int32_t xfer_len, u_int8_t *data_ptr,
105 			  u_int8_t sense_len, u_int32_t timeout);
106 
107 static struct periph_driver ptdriver =
108 {
109 	ptinit, "pt",
110 	TAILQ_HEAD_INITIALIZER(ptdriver.units), /* generation */ 0
111 };
112 
113 DATA_SET(periphdriver_set, ptdriver);
114 
115 #define PT_CDEV_MAJOR 61
116 
117 static struct cdevsw pt_cdevsw =
118 {
119 	/*d_open*/	ptopen,
120 	/*d_close*/	ptclose,
121 	/*d_read*/	ptread,
122 	/*d_write*/	ptwrite,
123 	/*d_ioctl*/	noioctl,
124 	/*d_stop*/	nostop,
125 	/*d_reset*/	noreset,
126 	/*d_devtotty*/	nodevtotty,
127 	/*d_poll*/	seltrue,
128 	/*d_mmap*/	nommap,
129 	/*d_strategy*/	ptstrategy,
130 	/*d_name*/	"pt",
131 	/*d_spare*/	NULL,
132 	/*d_maj*/	-1,
133 	/*d_dump*/	nodump,
134 	/*d_psize*/	nopsize,
135 	/*d_flags*/	0,
136 	/*d_maxio*/	0,
137 	/*b_maj*/	-1
138 };
139 
140 static struct extend_array *ptperiphs;
141 
142 static int
143 ptopen(dev_t dev, int flags, int fmt, struct proc *p)
144 {
145 	struct cam_periph *periph;
146 	struct pt_softc *softc;
147 	int unit;
148 	int error;
149 	int s;
150 
151 	unit = minor(dev);
152 	periph = cam_extend_get(ptperiphs, unit);
153 	if (periph == NULL)
154 		return (ENXIO);
155 
156 	softc = (struct pt_softc *)periph->softc;
157 
158 	s = splsoftcam();
159 	if (softc->flags & PT_FLAG_DEVICE_INVALID) {
160 		splx(s);
161 		return(ENXIO);
162 	}
163 
164 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
165 	    ("ptopen: dev=0x%x (unit %d)\n", dev, unit));
166 
167 	if ((error = cam_periph_lock(periph, PRIBIO|PCATCH)) != 0) {
168 		splx(s);
169 		return (error); /* error code from tsleep */
170 	}
171 
172 	splx(s);
173 
174 	if ((softc->flags & PT_FLAG_OPEN) == 0) {
175 		if (cam_periph_acquire(periph) != CAM_REQ_CMP)
176 			error = ENXIO;
177 		else
178 			softc->flags |= PT_FLAG_OPEN;
179 	} else
180 		error = EBUSY;
181 
182 	cam_periph_unlock(periph);
183 	return (error);
184 }
185 
186 static int
187 ptclose(dev_t dev, int flag, int fmt, struct proc *p)
188 {
189 	struct	cam_periph *periph;
190 	struct	pt_softc *softc;
191 	int	unit;
192 	int	error;
193 
194 	unit = minor(dev);
195 	periph = cam_extend_get(ptperiphs, unit);
196 	if (periph == NULL)
197 		return (ENXIO);
198 
199 	softc = (struct pt_softc *)periph->softc;
200 
201 	if ((error = cam_periph_lock(periph, PRIBIO)) != 0)
202 		return (error); /* error code from tsleep */
203 
204 	softc->flags &= ~PT_FLAG_OPEN;
205 	cam_periph_unlock(periph);
206 	cam_periph_release(periph);
207 	return (0);
208 }
209 
210 static int
211 ptread(dev_t dev, struct uio *uio, int ioflag)
212 {
213 	return(physio(ptstrategy, NULL, dev, 1, minphys, uio));
214 }
215 
216 static int
217 ptwrite(dev_t dev, struct uio *uio, int ioflag)
218 {
219 	return(physio(ptstrategy, NULL, dev, 0, minphys, uio));
220 }
221 
222 /*
223  * Actually translate the requested transfer into one the physical driver
224  * can understand.  The transfer is described by a buf and will include
225  * only one physical transfer.
226  */
227 static void
228 ptstrategy(struct buf *bp)
229 {
230 	struct cam_periph *periph;
231 	struct pt_softc *softc;
232 	u_int  unit;
233 	int    s;
234 
235 	unit = minor(bp->b_dev);
236 	periph = cam_extend_get(ptperiphs, unit);
237 	if (periph == NULL) {
238 		bp->b_error = ENXIO;
239 		goto bad;
240 	}
241 	softc = (struct pt_softc *)periph->softc;
242 
243 	/*
244 	 * Mask interrupts so that the pack cannot be invalidated until
245 	 * after we are in the queue.  Otherwise, we might not properly
246 	 * clean up one of the buffers.
247 	 */
248 	s = splbio();
249 
250 	/*
251 	 * If the device has been made invalid, error out
252 	 */
253 	if ((softc->flags & PT_FLAG_DEVICE_INVALID)) {
254 		splx(s);
255 		bp->b_error = ENXIO;
256 		goto bad;
257 	}
258 
259 	/*
260 	 * Place it in the queue of disk activities for this disk
261 	 */
262 	bufq_insert_tail(&softc->buf_queue, bp);
263 
264 	splx(s);
265 
266 	/*
267 	 * Schedule ourselves for performing the work.
268 	 */
269 	xpt_schedule(periph, /* XXX priority */1);
270 
271 	return;
272 bad:
273 	bp->b_flags |= B_ERROR;
274 
275 	/*
276 	 * Correctly set the buf to indicate a completed xfer
277 	 */
278 	bp->b_resid = bp->b_bcount;
279 	biodone(bp);
280 }
281 
282 static void
283 ptinit(void)
284 {
285 	cam_status status;
286 	struct cam_path *path;
287 
288 	/*
289 	 * Create our extend array for storing the devices we attach to.
290 	 */
291 	ptperiphs = cam_extend_new();
292 	if (ptperiphs == NULL) {
293 		printf("pt: Failed to alloc extend array!\n");
294 		return;
295 	}
296 
297 	/*
298 	 * Install a global async callback.  This callback will
299 	 * receive async callbacks like "new device found".
300 	 */
301 	status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
302 				 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
303 
304 	if (status == CAM_REQ_CMP) {
305 		struct ccb_setasync csa;
306 
307                 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
308                 csa.ccb_h.func_code = XPT_SASYNC_CB;
309                 csa.event_enable = AC_FOUND_DEVICE;
310                 csa.callback = ptasync;
311                 csa.callback_arg = NULL;
312                 xpt_action((union ccb *)&csa);
313 		status = csa.ccb_h.status;
314                 xpt_free_path(path);
315         }
316 
317 	if (status != CAM_REQ_CMP) {
318 		printf("pt: Failed to attach master async callback "
319 		       "due to status 0x%x!\n", status);
320 	} else {
321 		/* If we were successfull, register our devsw */
322 		dev_t dev;
323 
324 		dev = makedev(PT_CDEV_MAJOR, 0);
325 		cdevsw_add(&dev,&pt_cdevsw, NULL);
326 	}
327 }
328 
329 static cam_status
330 ptctor(struct cam_periph *periph, void *arg)
331 {
332 	struct pt_softc *softc;
333 	struct ccb_setasync csa;
334 	struct ccb_getdev *cgd;
335 
336 	cgd = (struct ccb_getdev *)arg;
337 	if (periph == NULL) {
338 		printf("ptregister: periph was NULL!!\n");
339 		return(CAM_REQ_CMP_ERR);
340 	}
341 
342 	if (cgd == NULL) {
343 		printf("ptregister: no getdev CCB, can't register device\n");
344 		return(CAM_REQ_CMP_ERR);
345 	}
346 
347 	softc = (struct pt_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT);
348 
349 	if (softc == NULL) {
350 		printf("daregister: Unable to probe new device. "
351 		       "Unable to allocate softc\n");
352 		return(CAM_REQ_CMP_ERR);
353 	}
354 
355 	bzero(softc, sizeof(*softc));
356 	LIST_INIT(&softc->pending_ccbs);
357 	softc->state = PT_STATE_NORMAL;
358 	bufq_init(&softc->buf_queue);
359 
360 	periph->softc = softc;
361 
362 	cam_extend_set(ptperiphs, periph->unit_number, periph);
363 
364 	/*
365 	 * The DA driver supports a blocksize, but
366 	 * we don't know the blocksize until we do
367 	 * a read capacity.  So, set a flag to
368 	 * indicate that the blocksize is
369 	 * unavailable right now.  We'll clear the
370 	 * flag as soon as we've done a read capacity.
371 	 */
372 	devstat_add_entry(&softc->device_stats, "pt",
373 			  periph->unit_number, 0,
374 			  DEVSTAT_NO_BLOCKSIZE,
375 			  cgd->pd_type | DEVSTAT_TYPE_IF_SCSI);
376 
377 	/*
378 	 * Add async callbacks for bus reset and
379 	 * bus device reset calls.  I don't bother
380 	 * checking if this fails as, in most cases,
381 	 * the system will function just fine without
382 	 * them and the only alternative would be to
383 	 * not attach the device on failure.
384 	 */
385 	xpt_setup_ccb(&csa.ccb_h, periph->path, /*priority*/5);
386 	csa.ccb_h.func_code = XPT_SASYNC_CB;
387 	csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE;
388 	csa.callback = ptasync;
389 	csa.callback_arg = periph;
390 	xpt_action((union ccb *)&csa);
391 
392 	/* Tell the user we've attached to the device */
393 	xpt_announce_periph(periph, NULL);
394 
395 	return(CAM_REQ_CMP);
396 }
397 
398 static void
399 ptoninvalidate(struct cam_periph *periph)
400 {
401 	int s;
402 	struct pt_softc *softc;
403 	struct buf *q_bp;
404 	struct ccb_setasync csa;
405 
406 	softc = (struct pt_softc *)periph->softc;
407 
408 	/*
409 	 * De-register any async callbacks.
410 	 */
411 	xpt_setup_ccb(&csa.ccb_h, periph->path,
412 		      /* priority */ 5);
413 	csa.ccb_h.func_code = XPT_SASYNC_CB;
414 	csa.event_enable = 0;
415 	csa.callback = ptasync;
416 	csa.callback_arg = periph;
417 	xpt_action((union ccb *)&csa);
418 
419 	softc->flags |= PT_FLAG_DEVICE_INVALID;
420 
421 	/*
422 	 * Although the oninvalidate() routines are always called at
423 	 * splsoftcam, we need to be at splbio() here to keep the buffer
424 	 * queue from being modified while we traverse it.
425 	 */
426 	s = splbio();
427 
428 	/*
429 	 * Return all queued I/O with ENXIO.
430 	 * XXX Handle any transactions queued to the card
431 	 *     with XPT_ABORT_CCB.
432 	 */
433 	while ((q_bp = bufq_first(&softc->buf_queue)) != NULL){
434 		bufq_remove(&softc->buf_queue, q_bp);
435 		q_bp->b_resid = q_bp->b_bcount;
436 		q_bp->b_error = ENXIO;
437 		q_bp->b_flags |= B_ERROR;
438 		biodone(q_bp);
439 	}
440 
441 	splx(s);
442 
443 	xpt_print_path(periph->path);
444 	printf("lost device\n");
445 }
446 
447 static void
448 ptdtor(struct cam_periph *periph)
449 {
450 	struct pt_softc *softc;
451 
452 	softc = (struct pt_softc *)periph->softc;
453 
454 	devstat_remove_entry(&softc->device_stats);
455 
456 	cam_extend_release(ptperiphs, periph->unit_number);
457 	xpt_print_path(periph->path);
458 	printf("removing device entry\n");
459 	free(softc, M_DEVBUF);
460 }
461 
462 static void
463 ptasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
464 {
465 	struct cam_periph *periph;
466 
467 	periph = (struct cam_periph *)callback_arg;
468 	switch (code) {
469 	case AC_FOUND_DEVICE:
470 	{
471 		struct ccb_getdev *cgd;
472 		cam_status status;
473 
474 		cgd = (struct ccb_getdev *)arg;
475 
476 		if (cgd->pd_type != T_PROCESSOR)
477 			break;
478 
479 		/*
480 		 * Allocate a peripheral instance for
481 		 * this device and start the probe
482 		 * process.
483 		 */
484 		status = cam_periph_alloc(ptctor, ptoninvalidate, ptdtor,
485 					  ptstart, "pt", CAM_PERIPH_BIO,
486 					  cgd->ccb_h.path, ptasync,
487 					  AC_FOUND_DEVICE, cgd);
488 
489 		if (status != CAM_REQ_CMP
490 		 && status != CAM_REQ_INPROG)
491 			printf("ptasync: Unable to attach to new device "
492 				"due to status 0x%x\n", status);
493 		break;
494 	}
495 	case AC_LOST_DEVICE:
496 	{
497 		cam_periph_invalidate(periph);
498 		break;
499 	}
500 	case AC_SENT_BDR:
501 	case AC_BUS_RESET:
502 	{
503 		struct pt_softc *softc;
504 		struct ccb_hdr *ccbh;
505 		int s;
506 
507 		softc = (struct pt_softc *)periph->softc;
508 		s = splsoftcam();
509 		/*
510 		 * Don't fail on the expected unit attention
511 		 * that will occur.
512 		 */
513 		softc->flags |= PT_FLAG_RETRY_UA;
514 		for (ccbh = LIST_FIRST(&softc->pending_ccbs);
515 		     ccbh != NULL; ccbh = LIST_NEXT(ccbh, periph_links.le))
516 			ccbh->ccb_state |= PT_CCB_RETRY_UA;
517 		splx(s);
518 		break;
519 	}
520 	case AC_TRANSFER_NEG:
521 	case AC_SCSI_AEN:
522 	case AC_UNSOL_RESEL:
523 	default:
524 		break;
525 	}
526 }
527 
528 static void
529 ptstart(struct cam_periph *periph, union ccb *start_ccb)
530 {
531 	struct pt_softc *softc;
532 	struct buf *bp;
533 	int s;
534 
535 	softc = (struct pt_softc *)periph->softc;
536 
537 	/*
538 	 * See if there is a buf with work for us to do..
539 	 */
540 	s = splbio();
541 	bp = bufq_first(&softc->buf_queue);
542 	if (periph->immediate_priority <= periph->pinfo.priority) {
543 		CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
544 				("queuing for immediate ccb\n"));
545 		start_ccb->ccb_h.ccb_state = PT_CCB_WAITING;
546 		SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
547 				  periph_links.sle);
548 		periph->immediate_priority = CAM_PRIORITY_NONE;
549 		splx(s);
550 		wakeup(&periph->ccb_list);
551 	} else if (bp == NULL) {
552 		splx(s);
553 		xpt_release_ccb(start_ccb);
554 	} else {
555 		int oldspl;
556 
557 		bufq_remove(&softc->buf_queue, bp);
558 
559 		devstat_start_transaction(&softc->device_stats);
560 
561 		scsi_send_receive(&start_ccb->csio,
562 				  /*retries*/4,
563 				  ptdone,
564 				  MSG_SIMPLE_Q_TAG,
565 				  bp->b_flags & B_READ,
566 				  /*byte2*/0,
567 				  bp->b_bcount,
568 				  bp->b_data,
569 				  /*sense_len*/SSD_FULL_SIZE,
570 				  /*timeout*/10000);
571 
572 		start_ccb->ccb_h.ccb_state = PT_CCB_BUFFER_IO;
573 
574 		/*
575 		 * Block out any asyncronous callbacks
576 		 * while we touch the pending ccb list.
577 		 */
578 		oldspl = splcam();
579 		LIST_INSERT_HEAD(&softc->pending_ccbs, &start_ccb->ccb_h,
580 				 periph_links.le);
581 		splx(oldspl);
582 
583 		start_ccb->ccb_h.ccb_bp = bp;
584 		bp = bufq_first(&softc->buf_queue);
585 		splx(s);
586 
587 		xpt_action(start_ccb);
588 
589 		if (bp != NULL) {
590 			/* Have more work to do, so ensure we stay scheduled */
591 			xpt_schedule(periph, /* XXX priority */1);
592 		}
593 	}
594 }
595 
596 static void
597 ptdone(struct cam_periph *periph, union ccb *done_ccb)
598 {
599 	struct pt_softc *softc;
600 	struct ccb_scsiio *csio;
601 
602 	softc = (struct pt_softc *)periph->softc;
603 	csio = &done_ccb->csio;
604 	switch (csio->ccb_h.ccb_state) {
605 	case PT_CCB_BUFFER_IO:
606 	case PT_CCB_BUFFER_IO_UA:
607 	{
608 		struct buf *bp;
609 		int    oldspl;
610 
611 		bp = (struct buf *)done_ccb->ccb_h.ccb_bp;
612 		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
613 			int error;
614 			int s;
615 			int sf;
616 
617 			if ((csio->ccb_h.ccb_state & PT_CCB_RETRY_UA) != 0)
618 				sf = SF_RETRY_UA;
619 			else
620 				sf = 0;
621 
622 			if ((error = pterror(done_ccb, 0, sf)) == ERESTART) {
623 				/*
624 				 * A retry was scheuled, so
625 				 * just return.
626 				 */
627 				return;
628 			}
629 			if (error != 0) {
630 				struct buf *q_bp;
631 
632 				s = splbio();
633 
634 				if (error == ENXIO) {
635 					/*
636 					 * Catastrophic error.  Mark our device
637 					 * as invalid.
638 					 */
639 					xpt_print_path(periph->path);
640 					printf("Invalidating device\n");
641 					softc->flags |= PT_FLAG_DEVICE_INVALID;
642 				}
643 
644 				/*
645 				 * return all queued I/O with EIO, so that
646 				 * the client can retry these I/Os in the
647 				 * proper order should it attempt to recover.
648 				 */
649 				while ((q_bp = bufq_first(&softc->buf_queue))
650 					!= NULL) {
651 					bufq_remove(&softc->buf_queue, q_bp);
652 					q_bp->b_resid = q_bp->b_bcount;
653 					q_bp->b_error = EIO;
654 					q_bp->b_flags |= B_ERROR;
655 					biodone(q_bp);
656 				}
657 				splx(s);
658 				bp->b_error = error;
659 				bp->b_resid = bp->b_bcount;
660 				bp->b_flags |= B_ERROR;
661 			} else {
662 				bp->b_resid = csio->resid;
663 				bp->b_error = 0;
664 				if (bp->b_resid != 0) {
665 					/* Short transfer ??? */
666 					bp->b_flags |= B_ERROR;
667 				}
668 			}
669 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
670 				cam_release_devq(done_ccb->ccb_h.path,
671 						 /*relsim_flags*/0,
672 						 /*reduction*/0,
673 						 /*timeout*/0,
674 						 /*getcount_only*/0);
675 		} else {
676 			bp->b_resid = csio->resid;
677 			if (bp->b_resid != 0)
678 				bp->b_flags |= B_ERROR;
679 		}
680 
681 		/*
682 		 * Block out any asyncronous callbacks
683 		 * while we touch the pending ccb list.
684 		 */
685 		oldspl = splcam();
686 		LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
687 		splx(oldspl);
688 
689 		devstat_end_transaction(&softc->device_stats,
690 					bp->b_bcount - bp->b_resid,
691 					done_ccb->csio.tag_action & 0xf,
692 					(bp->b_flags & B_READ) ? DEVSTAT_READ
693 							       : DEVSTAT_WRITE);
694 
695 		biodone(bp);
696 		break;
697 	}
698 	case PT_CCB_WAITING:
699 		/* Caller will release the CCB */
700 		wakeup(&done_ccb->ccb_h.cbfcnp);
701 		return;
702 	}
703 	xpt_release_ccb(done_ccb);
704 }
705 
706 static int
707 pterror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
708 {
709 	struct pt_softc	  *softc;
710 	struct cam_periph *periph;
711 
712 	periph = xpt_path_periph(ccb->ccb_h.path);
713 	softc = (struct pt_softc *)periph->softc;
714 
715 	return(cam_periph_error(ccb, cam_flags, sense_flags,
716 				&softc->saved_ccb));
717 }
718 
719 void
720 scsi_send_receive(struct ccb_scsiio *csio, u_int32_t retries,
721 		  void (*cbfcnp)(struct cam_periph *, union ccb *),
722 		  u_int tag_action, int readop, u_int byte2,
723 		  u_int32_t xfer_len, u_int8_t *data_ptr, u_int8_t sense_len,
724 		  u_int32_t timeout)
725 {
726 	struct scsi_send_receive *scsi_cmd;
727 
728 	scsi_cmd = (struct scsi_send_receive *)&csio->cdb_io.cdb_bytes;
729 	scsi_cmd->opcode = readop ? RECEIVE : SEND;
730 	scsi_cmd->byte2 = byte2;
731 	scsi_ulto3b(xfer_len, scsi_cmd->xfer_len);
732 	scsi_cmd->control = 0;
733 
734 	cam_fill_csio(csio,
735 		      retries,
736 		      cbfcnp,
737 		      /*flags*/readop ? CAM_DIR_IN : CAM_DIR_OUT,
738 		      tag_action,
739 		      data_ptr,
740 		      xfer_len,
741 		      sense_len,
742 		      sizeof(*scsi_cmd),
743 		      timeout);
744 }
745