scsi_pt.c revision 79177
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 * $FreeBSD: head/sys/cam/scsi/scsi_pt.c 79177 2001-07-04 05:22:42Z mjacob $
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/bio.h>
37#include <sys/devicestat.h>
38#include <sys/malloc.h>
39#include <sys/conf.h>
40#include <sys/ptio.h>
41
42#include <cam/cam.h>
43#include <cam/cam_ccb.h>
44#include <cam/cam_extend.h>
45#include <cam/cam_periph.h>
46#include <cam/cam_xpt_periph.h>
47#include <cam/cam_debug.h>
48
49#include <cam/scsi/scsi_all.h>
50#include <cam/scsi/scsi_message.h>
51#include <cam/scsi/scsi_pt.h>
52
53#include "opt_pt.h"
54
55typedef enum {
56	PT_STATE_PROBE,
57	PT_STATE_NORMAL
58} pt_state;
59
60typedef enum {
61	PT_FLAG_NONE		= 0x00,
62	PT_FLAG_OPEN		= 0x01,
63	PT_FLAG_DEVICE_INVALID	= 0x02,
64	PT_FLAG_RETRY_UA	= 0x04
65} pt_flags;
66
67typedef enum {
68	PT_CCB_BUFFER_IO	= 0x01,
69	PT_CCB_WAITING		= 0x02,
70	PT_CCB_RETRY_UA		= 0x04,
71	PT_CCB_BUFFER_IO_UA	= PT_CCB_BUFFER_IO|PT_CCB_RETRY_UA
72} pt_ccb_state;
73
74/* Offsets into our private area for storing information */
75#define ccb_state	ppriv_field0
76#define ccb_bp		ppriv_ptr1
77
78struct pt_softc {
79	struct	 bio_queue_head bio_queue;
80	struct	 devstat device_stats;
81	LIST_HEAD(, ccb_hdr) pending_ccbs;
82	pt_state state;
83	pt_flags flags;
84	union	 ccb saved_ccb;
85	int	 io_timeout;
86	dev_t	 dev;
87};
88
89static	d_open_t	ptopen;
90static	d_close_t	ptclose;
91static	d_strategy_t	ptstrategy;
92static	periph_init_t	ptinit;
93static	void		ptasync(void *callback_arg, u_int32_t code,
94				struct cam_path *path, void *arg);
95static	periph_ctor_t	ptctor;
96static	periph_oninv_t	ptoninvalidate;
97static	periph_dtor_t	ptdtor;
98static	periph_start_t	ptstart;
99static	void		ptdone(struct cam_periph *periph,
100			       union ccb *done_ccb);
101static	d_ioctl_t	ptioctl;
102static  int		pterror(union ccb *ccb, u_int32_t cam_flags,
103				u_int32_t sense_flags);
104
105void	scsi_send_receive(struct ccb_scsiio *csio, u_int32_t retries,
106			  void (*cbfcnp)(struct cam_periph *, union ccb *),
107			  u_int tag_action, int readop, u_int byte2,
108			  u_int32_t xfer_len, u_int8_t *data_ptr,
109			  u_int8_t sense_len, u_int32_t timeout);
110
111static struct periph_driver ptdriver =
112{
113	ptinit, "pt",
114	TAILQ_HEAD_INITIALIZER(ptdriver.units), /* generation */ 0
115};
116
117PERIPHDRIVER_DECLARE(pt, ptdriver);
118
119#define PT_CDEV_MAJOR 61
120
121static struct cdevsw pt_cdevsw = {
122	/* open */	ptopen,
123	/* close */	ptclose,
124	/* read */	physread,
125	/* write */	physwrite,
126	/* ioctl */	ptioctl,
127	/* poll */	nopoll,
128	/* mmap */	nommap,
129	/* strategy */	ptstrategy,
130	/* name */	"pt",
131	/* maj */	PT_CDEV_MAJOR,
132	/* dump */	nodump,
133	/* psize */	nopsize,
134	/* flags */	0,
135};
136
137static struct extend_array *ptperiphs;
138
139#ifndef SCSI_PT_DEFAULT_TIMEOUT
140#define SCSI_PT_DEFAULT_TIMEOUT		60
141#endif
142
143static int
144ptopen(dev_t dev, int flags, int fmt, struct proc *p)
145{
146	struct cam_periph *periph;
147	struct pt_softc *softc;
148	int unit;
149	int error;
150	int s;
151
152	unit = minor(dev);
153	periph = cam_extend_get(ptperiphs, unit);
154	if (periph == NULL)
155		return (ENXIO);
156
157	softc = (struct pt_softc *)periph->softc;
158
159	s = splsoftcam();
160	if (softc->flags & PT_FLAG_DEVICE_INVALID) {
161		splx(s);
162		return(ENXIO);
163	}
164
165	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
166	    ("ptopen: dev=%s (unit %d)\n", devtoname(dev), unit));
167
168	if ((error = cam_periph_lock(periph, PRIBIO|PCATCH)) != 0) {
169		splx(s);
170		return (error); /* error code from tsleep */
171	}
172
173	splx(s);
174
175	if ((softc->flags & PT_FLAG_OPEN) == 0) {
176		if (cam_periph_acquire(periph) != CAM_REQ_CMP)
177			error = ENXIO;
178		else
179			softc->flags |= PT_FLAG_OPEN;
180	} else
181		error = EBUSY;
182
183	cam_periph_unlock(periph);
184	return (error);
185}
186
187static int
188ptclose(dev_t dev, int flag, int fmt, struct proc *p)
189{
190	struct	cam_periph *periph;
191	struct	pt_softc *softc;
192	int	unit;
193	int	error;
194
195	unit = minor(dev);
196	periph = cam_extend_get(ptperiphs, unit);
197	if (periph == NULL)
198		return (ENXIO);
199
200	softc = (struct pt_softc *)periph->softc;
201
202	if ((error = cam_periph_lock(periph, PRIBIO)) != 0)
203		return (error); /* error code from tsleep */
204
205	softc->flags &= ~PT_FLAG_OPEN;
206	cam_periph_unlock(periph);
207	cam_periph_release(periph);
208	return (0);
209}
210
211/*
212 * Actually translate the requested transfer into one the physical driver
213 * can understand.  The transfer is described by a buf and will include
214 * only one physical transfer.
215 */
216static void
217ptstrategy(struct bio *bp)
218{
219	struct cam_periph *periph;
220	struct pt_softc *softc;
221	u_int  unit;
222	int    s;
223
224	unit = minor(bp->bio_dev);
225	periph = cam_extend_get(ptperiphs, unit);
226	bp->bio_resid = bp->bio_bcount;
227	if (periph == NULL) {
228		biofinish(bp, NULL, ENXIO);
229		return;
230	}
231	softc = (struct pt_softc *)periph->softc;
232
233	/*
234	 * Mask interrupts so that the pack cannot be invalidated until
235	 * after we are in the queue.  Otherwise, we might not properly
236	 * clean up one of the buffers.
237	 */
238	s = splbio();
239
240	/*
241	 * If the device has been made invalid, error out
242	 */
243	if ((softc->flags & PT_FLAG_DEVICE_INVALID)) {
244		splx(s);
245		biofinish(bp, NULL, ENXIO);
246		return;
247	}
248
249	/*
250	 * Place it in the queue of disk activities for this disk
251	 */
252	bioq_insert_tail(&softc->bio_queue, bp);
253
254	splx(s);
255
256	/*
257	 * Schedule ourselves for performing the work.
258	 */
259	xpt_schedule(periph, /* XXX priority */1);
260
261	return;
262}
263
264static void
265ptinit(void)
266{
267	cam_status status;
268	struct cam_path *path;
269
270	/*
271	 * Create our extend array for storing the devices we attach to.
272	 */
273	ptperiphs = cam_extend_new();
274	if (ptperiphs == NULL) {
275		printf("pt: Failed to alloc extend array!\n");
276		return;
277	}
278
279	/*
280	 * Install a global async callback.  This callback will
281	 * receive async callbacks like "new device found".
282	 */
283	status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
284				 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
285
286	if (status == CAM_REQ_CMP) {
287		struct ccb_setasync csa;
288
289                xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
290                csa.ccb_h.func_code = XPT_SASYNC_CB;
291                csa.event_enable = AC_FOUND_DEVICE;
292                csa.callback = ptasync;
293                csa.callback_arg = NULL;
294                xpt_action((union ccb *)&csa);
295		status = csa.ccb_h.status;
296                xpt_free_path(path);
297        }
298
299	if (status != CAM_REQ_CMP) {
300		printf("pt: Failed to attach master async callback "
301		       "due to status 0x%x!\n", status);
302	}
303}
304
305static cam_status
306ptctor(struct cam_periph *periph, void *arg)
307{
308	struct pt_softc *softc;
309	struct ccb_setasync csa;
310	struct ccb_getdev *cgd;
311
312	cgd = (struct ccb_getdev *)arg;
313	if (periph == NULL) {
314		printf("ptregister: periph was NULL!!\n");
315		return(CAM_REQ_CMP_ERR);
316	}
317
318	if (cgd == NULL) {
319		printf("ptregister: no getdev CCB, can't register device\n");
320		return(CAM_REQ_CMP_ERR);
321	}
322
323	softc = (struct pt_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT);
324
325	if (softc == NULL) {
326		printf("daregister: Unable to probe new device. "
327		       "Unable to allocate softc\n");
328		return(CAM_REQ_CMP_ERR);
329	}
330
331	bzero(softc, sizeof(*softc));
332	LIST_INIT(&softc->pending_ccbs);
333	softc->state = PT_STATE_NORMAL;
334	bioq_init(&softc->bio_queue);
335
336	softc->io_timeout = SCSI_PT_DEFAULT_TIMEOUT * 1000;
337
338	periph->softc = softc;
339
340	cam_extend_set(ptperiphs, periph->unit_number, periph);
341
342	devstat_add_entry(&softc->device_stats, "pt",
343			  periph->unit_number, 0,
344			  DEVSTAT_NO_BLOCKSIZE,
345			  SID_TYPE(&cgd->inq_data) | DEVSTAT_TYPE_IF_SCSI,
346			  DEVSTAT_PRIORITY_OTHER);
347
348	softc->dev = make_dev(&pt_cdevsw, periph->unit_number, UID_ROOT,
349			      GID_OPERATOR, 0600, "%s%d", periph->periph_name,
350			      periph->unit_number);
351	/*
352	 * Add async callbacks for bus reset and
353	 * bus device reset calls.  I don't bother
354	 * checking if this fails as, in most cases,
355	 * the system will function just fine without
356	 * them and the only alternative would be to
357	 * not attach the device on failure.
358	 */
359	xpt_setup_ccb(&csa.ccb_h, periph->path, /*priority*/5);
360	csa.ccb_h.func_code = XPT_SASYNC_CB;
361	csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE;
362	csa.callback = ptasync;
363	csa.callback_arg = periph;
364	xpt_action((union ccb *)&csa);
365
366	/* Tell the user we've attached to the device */
367	xpt_announce_periph(periph, NULL);
368
369	return(CAM_REQ_CMP);
370}
371
372static void
373ptoninvalidate(struct cam_periph *periph)
374{
375	int s;
376	struct pt_softc *softc;
377	struct bio *q_bp;
378	struct ccb_setasync csa;
379
380	softc = (struct pt_softc *)periph->softc;
381
382	/*
383	 * De-register any async callbacks.
384	 */
385	xpt_setup_ccb(&csa.ccb_h, periph->path,
386		      /* priority */ 5);
387	csa.ccb_h.func_code = XPT_SASYNC_CB;
388	csa.event_enable = 0;
389	csa.callback = ptasync;
390	csa.callback_arg = periph;
391	xpt_action((union ccb *)&csa);
392
393	softc->flags |= PT_FLAG_DEVICE_INVALID;
394
395	/*
396	 * Although the oninvalidate() routines are always called at
397	 * splsoftcam, we need to be at splbio() here to keep the buffer
398	 * queue from being modified while we traverse it.
399	 */
400	s = splbio();
401
402	/*
403	 * Return all queued I/O with ENXIO.
404	 * XXX Handle any transactions queued to the card
405	 *     with XPT_ABORT_CCB.
406	 */
407	while ((q_bp = bioq_first(&softc->bio_queue)) != NULL){
408		bioq_remove(&softc->bio_queue, q_bp);
409		q_bp->bio_resid = q_bp->bio_bcount;
410		biofinish(q_bp, NULL, ENXIO);
411	}
412
413	splx(s);
414
415	xpt_print_path(periph->path);
416	printf("lost device\n");
417}
418
419static void
420ptdtor(struct cam_periph *periph)
421{
422	struct pt_softc *softc;
423
424	softc = (struct pt_softc *)periph->softc;
425
426	devstat_remove_entry(&softc->device_stats);
427
428	destroy_dev(softc->dev);
429
430	cam_extend_release(ptperiphs, periph->unit_number);
431	xpt_print_path(periph->path);
432	printf("removing device entry\n");
433	free(softc, M_DEVBUF);
434}
435
436static void
437ptasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
438{
439	struct cam_periph *periph;
440
441	periph = (struct cam_periph *)callback_arg;
442	switch (code) {
443	case AC_FOUND_DEVICE:
444	{
445		struct ccb_getdev *cgd;
446		cam_status status;
447
448		cgd = (struct ccb_getdev *)arg;
449		if (cgd == NULL)
450			break;
451
452		if (SID_TYPE(&cgd->inq_data) != T_PROCESSOR)
453			break;
454
455		/*
456		 * Allocate a peripheral instance for
457		 * this device and start the probe
458		 * process.
459		 */
460		status = cam_periph_alloc(ptctor, ptoninvalidate, ptdtor,
461					  ptstart, "pt", CAM_PERIPH_BIO,
462					  cgd->ccb_h.path, ptasync,
463					  AC_FOUND_DEVICE, cgd);
464
465		if (status != CAM_REQ_CMP
466		 && status != CAM_REQ_INPROG)
467			printf("ptasync: Unable to attach to new device "
468				"due to status 0x%x\n", status);
469		break;
470	}
471	case AC_SENT_BDR:
472	case AC_BUS_RESET:
473	{
474		struct pt_softc *softc;
475		struct ccb_hdr *ccbh;
476		int s;
477
478		softc = (struct pt_softc *)periph->softc;
479		s = splsoftcam();
480		/*
481		 * Don't fail on the expected unit attention
482		 * that will occur.
483		 */
484		softc->flags |= PT_FLAG_RETRY_UA;
485		LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
486			ccbh->ccb_state |= PT_CCB_RETRY_UA;
487		splx(s);
488		/* FALLTHROUGH */
489	}
490	default:
491		cam_periph_async(periph, code, path, arg);
492		break;
493	}
494}
495
496static void
497ptstart(struct cam_periph *periph, union ccb *start_ccb)
498{
499	struct pt_softc *softc;
500	struct bio *bp;
501	int s;
502
503	softc = (struct pt_softc *)periph->softc;
504
505	/*
506	 * See if there is a buf with work for us to do..
507	 */
508	s = splbio();
509	bp = bioq_first(&softc->bio_queue);
510	if (periph->immediate_priority <= periph->pinfo.priority) {
511		CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
512				("queuing for immediate ccb\n"));
513		start_ccb->ccb_h.ccb_state = PT_CCB_WAITING;
514		SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
515				  periph_links.sle);
516		periph->immediate_priority = CAM_PRIORITY_NONE;
517		splx(s);
518		wakeup(&periph->ccb_list);
519	} else if (bp == NULL) {
520		splx(s);
521		xpt_release_ccb(start_ccb);
522	} else {
523		int oldspl;
524
525		bioq_remove(&softc->bio_queue, bp);
526
527		devstat_start_transaction(&softc->device_stats);
528
529		scsi_send_receive(&start_ccb->csio,
530				  /*retries*/4,
531				  ptdone,
532				  MSG_SIMPLE_Q_TAG,
533				  bp->bio_cmd == BIO_READ,
534				  /*byte2*/0,
535				  bp->bio_bcount,
536				  bp->bio_data,
537				  /*sense_len*/SSD_FULL_SIZE,
538				  /*timeout*/softc->io_timeout);
539
540		start_ccb->ccb_h.ccb_state = PT_CCB_BUFFER_IO_UA;
541
542		/*
543		 * Block out any asyncronous callbacks
544		 * while we touch the pending ccb list.
545		 */
546		oldspl = splcam();
547		LIST_INSERT_HEAD(&softc->pending_ccbs, &start_ccb->ccb_h,
548				 periph_links.le);
549		splx(oldspl);
550
551		start_ccb->ccb_h.ccb_bp = bp;
552		bp = bioq_first(&softc->bio_queue);
553		splx(s);
554
555		xpt_action(start_ccb);
556
557		if (bp != NULL) {
558			/* Have more work to do, so ensure we stay scheduled */
559			xpt_schedule(periph, /* XXX priority */1);
560		}
561	}
562}
563
564static void
565ptdone(struct cam_periph *periph, union ccb *done_ccb)
566{
567	struct pt_softc *softc;
568	struct ccb_scsiio *csio;
569
570	softc = (struct pt_softc *)periph->softc;
571	csio = &done_ccb->csio;
572	switch (csio->ccb_h.ccb_state) {
573	case PT_CCB_BUFFER_IO:
574	case PT_CCB_BUFFER_IO_UA:
575	{
576		struct bio *bp;
577		int    oldspl;
578
579		bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
580		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
581			int error;
582			int s;
583			int sf;
584
585			if ((csio->ccb_h.ccb_state & PT_CCB_RETRY_UA) != 0)
586				sf = SF_RETRY_UA;
587			else
588				sf = 0;
589
590			error = pterror(done_ccb, CAM_RETRY_SELTO, sf);
591			if (error == ERESTART) {
592				/*
593				 * A retry was scheuled, so
594				 * just return.
595				 */
596				return;
597			}
598			if (error != 0) {
599				struct bio *q_bp;
600
601				s = splbio();
602
603				if (error == ENXIO) {
604					/*
605					 * Catastrophic error.  Mark our device
606					 * as invalid.
607					 */
608					xpt_print_path(periph->path);
609					printf("Invalidating device\n");
610					softc->flags |= PT_FLAG_DEVICE_INVALID;
611				}
612
613				/*
614				 * return all queued I/O with EIO, so that
615				 * the client can retry these I/Os in the
616				 * proper order should it attempt to recover.
617				 */
618				while ((q_bp = bioq_first(&softc->bio_queue))
619					!= NULL) {
620					bioq_remove(&softc->bio_queue, q_bp);
621					q_bp->bio_resid = q_bp->bio_bcount;
622					biofinish(q_bp, NULL, EIO);
623				}
624				splx(s);
625				bp->bio_error = error;
626				bp->bio_resid = bp->bio_bcount;
627				bp->bio_flags |= BIO_ERROR;
628			} else {
629				bp->bio_resid = csio->resid;
630				bp->bio_error = 0;
631				if (bp->bio_resid != 0) {
632					/* Short transfer ??? */
633					bp->bio_flags |= BIO_ERROR;
634				}
635			}
636			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
637				cam_release_devq(done_ccb->ccb_h.path,
638						 /*relsim_flags*/0,
639						 /*reduction*/0,
640						 /*timeout*/0,
641						 /*getcount_only*/0);
642		} else {
643			bp->bio_resid = csio->resid;
644			if (bp->bio_resid != 0)
645				bp->bio_flags |= BIO_ERROR;
646		}
647
648		/*
649		 * Block out any asyncronous callbacks
650		 * while we touch the pending ccb list.
651		 */
652		oldspl = splcam();
653		LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
654		splx(oldspl);
655
656		biofinish(bp, &softc->device_stats, 0);
657		break;
658	}
659	case PT_CCB_WAITING:
660		/* Caller will release the CCB */
661		wakeup(&done_ccb->ccb_h.cbfcnp);
662		return;
663	}
664	xpt_release_ccb(done_ccb);
665}
666
667static int
668pterror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
669{
670	struct pt_softc	  *softc;
671	struct cam_periph *periph;
672
673	periph = xpt_path_periph(ccb->ccb_h.path);
674	softc = (struct pt_softc *)periph->softc;
675
676	return(cam_periph_error(ccb, cam_flags, sense_flags,
677				&softc->saved_ccb));
678}
679
680static int
681ptioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
682{
683	struct cam_periph *periph;
684	struct pt_softc *softc;
685	int unit;
686	int error;
687
688	unit = minor(dev);
689	periph = cam_extend_get(ptperiphs, unit);
690
691	if (periph == NULL)
692		return(ENXIO);
693
694	softc = (struct pt_softc *)periph->softc;
695
696	if ((error = cam_periph_lock(periph, PRIBIO|PCATCH)) != 0) {
697		return (error); /* error code from tsleep */
698	}
699
700	switch(cmd) {
701	case PTIOCGETTIMEOUT:
702		if (softc->io_timeout >= 1000)
703			*(int *)addr = softc->io_timeout / 1000;
704		else
705			*(int *)addr = 0;
706		break;
707	case PTIOCSETTIMEOUT:
708	{
709		int s;
710
711		if (*(int *)addr < 1) {
712			error = EINVAL;
713			break;
714		}
715
716		s = splsoftcam();
717		softc->io_timeout = *(int *)addr * 1000;
718		splx(s);
719
720		break;
721	}
722	default:
723		error = cam_periph_ioctl(periph, cmd, addr, pterror);
724		break;
725	}
726
727	cam_periph_unlock(periph);
728
729	return(error);
730}
731
732void
733scsi_send_receive(struct ccb_scsiio *csio, u_int32_t retries,
734		  void (*cbfcnp)(struct cam_periph *, union ccb *),
735		  u_int tag_action, int readop, u_int byte2,
736		  u_int32_t xfer_len, u_int8_t *data_ptr, u_int8_t sense_len,
737		  u_int32_t timeout)
738{
739	struct scsi_send_receive *scsi_cmd;
740
741	scsi_cmd = (struct scsi_send_receive *)&csio->cdb_io.cdb_bytes;
742	scsi_cmd->opcode = readop ? RECEIVE : SEND;
743	scsi_cmd->byte2 = byte2;
744	scsi_ulto3b(xfer_len, scsi_cmd->xfer_len);
745	scsi_cmd->control = 0;
746
747	cam_fill_csio(csio,
748		      retries,
749		      cbfcnp,
750		      /*flags*/readop ? CAM_DIR_IN : CAM_DIR_OUT,
751		      tag_action,
752		      data_ptr,
753		      xfer_len,
754		      sense_len,
755		      sizeof(*scsi_cmd),
756		      timeout);
757}
758