xref: /freebsd/sys/cam/scsi/scsi_pt.c (revision 1b6c76a2fe091c74f08427e6c870851025a9cf67)
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$
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 
55 typedef enum {
56 	PT_STATE_PROBE,
57 	PT_STATE_NORMAL
58 } pt_state;
59 
60 typedef 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 
67 typedef 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 
78 struct 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 
89 static	d_open_t	ptopen;
90 static	d_close_t	ptclose;
91 static	d_strategy_t	ptstrategy;
92 static	periph_init_t	ptinit;
93 static	void		ptasync(void *callback_arg, u_int32_t code,
94 				struct cam_path *path, void *arg);
95 static	periph_ctor_t	ptctor;
96 static	periph_oninv_t	ptoninvalidate;
97 static	periph_dtor_t	ptdtor;
98 static	periph_start_t	ptstart;
99 static	void		ptdone(struct cam_periph *periph,
100 			       union ccb *done_ccb);
101 static	d_ioctl_t	ptioctl;
102 static  int		pterror(union ccb *ccb, u_int32_t cam_flags,
103 				u_int32_t sense_flags);
104 
105 void	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 
111 static struct periph_driver ptdriver =
112 {
113 	ptinit, "pt",
114 	TAILQ_HEAD_INITIALIZER(ptdriver.units), /* generation */ 0
115 };
116 
117 PERIPHDRIVER_DECLARE(pt, ptdriver);
118 
119 #define PT_CDEV_MAJOR 61
120 
121 static 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 
137 static struct extend_array *ptperiphs;
138 
139 #ifndef SCSI_PT_DEFAULT_TIMEOUT
140 #define SCSI_PT_DEFAULT_TIMEOUT		60
141 #endif
142 
143 static int
144 ptopen(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 
187 static int
188 ptclose(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  */
216 static void
217 ptstrategy(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 
264 static void
265 ptinit(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 
305 static cam_status
306 ptctor(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 
372 static void
373 ptoninvalidate(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 
419 static void
420 ptdtor(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 
436 static void
437 ptasync(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 
450 		if (SID_TYPE(&cgd->inq_data) != T_PROCESSOR)
451 			break;
452 
453 		/*
454 		 * Allocate a peripheral instance for
455 		 * this device and start the probe
456 		 * process.
457 		 */
458 		status = cam_periph_alloc(ptctor, ptoninvalidate, ptdtor,
459 					  ptstart, "pt", CAM_PERIPH_BIO,
460 					  cgd->ccb_h.path, ptasync,
461 					  AC_FOUND_DEVICE, cgd);
462 
463 		if (status != CAM_REQ_CMP
464 		 && status != CAM_REQ_INPROG)
465 			printf("ptasync: Unable to attach to new device "
466 				"due to status 0x%x\n", status);
467 		break;
468 	}
469 	case AC_SENT_BDR:
470 	case AC_BUS_RESET:
471 	{
472 		struct pt_softc *softc;
473 		struct ccb_hdr *ccbh;
474 		int s;
475 
476 		softc = (struct pt_softc *)periph->softc;
477 		s = splsoftcam();
478 		/*
479 		 * Don't fail on the expected unit attention
480 		 * that will occur.
481 		 */
482 		softc->flags |= PT_FLAG_RETRY_UA;
483 		LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
484 			ccbh->ccb_state |= PT_CCB_RETRY_UA;
485 		splx(s);
486 		/* FALLTHROUGH */
487 	}
488 	default:
489 		cam_periph_async(periph, code, path, arg);
490 		break;
491 	}
492 }
493 
494 static void
495 ptstart(struct cam_periph *periph, union ccb *start_ccb)
496 {
497 	struct pt_softc *softc;
498 	struct bio *bp;
499 	int s;
500 
501 	softc = (struct pt_softc *)periph->softc;
502 
503 	/*
504 	 * See if there is a buf with work for us to do..
505 	 */
506 	s = splbio();
507 	bp = bioq_first(&softc->bio_queue);
508 	if (periph->immediate_priority <= periph->pinfo.priority) {
509 		CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
510 				("queuing for immediate ccb\n"));
511 		start_ccb->ccb_h.ccb_state = PT_CCB_WAITING;
512 		SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
513 				  periph_links.sle);
514 		periph->immediate_priority = CAM_PRIORITY_NONE;
515 		splx(s);
516 		wakeup(&periph->ccb_list);
517 	} else if (bp == NULL) {
518 		splx(s);
519 		xpt_release_ccb(start_ccb);
520 	} else {
521 		int oldspl;
522 
523 		bioq_remove(&softc->bio_queue, bp);
524 
525 		devstat_start_transaction(&softc->device_stats);
526 
527 		scsi_send_receive(&start_ccb->csio,
528 				  /*retries*/4,
529 				  ptdone,
530 				  MSG_SIMPLE_Q_TAG,
531 				  bp->bio_cmd == BIO_READ,
532 				  /*byte2*/0,
533 				  bp->bio_bcount,
534 				  bp->bio_data,
535 				  /*sense_len*/SSD_FULL_SIZE,
536 				  /*timeout*/softc->io_timeout);
537 
538 		start_ccb->ccb_h.ccb_state = PT_CCB_BUFFER_IO_UA;
539 
540 		/*
541 		 * Block out any asyncronous callbacks
542 		 * while we touch the pending ccb list.
543 		 */
544 		oldspl = splcam();
545 		LIST_INSERT_HEAD(&softc->pending_ccbs, &start_ccb->ccb_h,
546 				 periph_links.le);
547 		splx(oldspl);
548 
549 		start_ccb->ccb_h.ccb_bp = bp;
550 		bp = bioq_first(&softc->bio_queue);
551 		splx(s);
552 
553 		xpt_action(start_ccb);
554 
555 		if (bp != NULL) {
556 			/* Have more work to do, so ensure we stay scheduled */
557 			xpt_schedule(periph, /* XXX priority */1);
558 		}
559 	}
560 }
561 
562 static void
563 ptdone(struct cam_periph *periph, union ccb *done_ccb)
564 {
565 	struct pt_softc *softc;
566 	struct ccb_scsiio *csio;
567 
568 	softc = (struct pt_softc *)periph->softc;
569 	csio = &done_ccb->csio;
570 	switch (csio->ccb_h.ccb_state) {
571 	case PT_CCB_BUFFER_IO:
572 	case PT_CCB_BUFFER_IO_UA:
573 	{
574 		struct bio *bp;
575 		int    oldspl;
576 
577 		bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
578 		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
579 			int error;
580 			int s;
581 			int sf;
582 
583 			if ((csio->ccb_h.ccb_state & PT_CCB_RETRY_UA) != 0)
584 				sf = SF_RETRY_UA;
585 			else
586 				sf = 0;
587 
588 			error = pterror(done_ccb, CAM_RETRY_SELTO, sf);
589 			if (error == ERESTART) {
590 				/*
591 				 * A retry was scheuled, so
592 				 * just return.
593 				 */
594 				return;
595 			}
596 			if (error != 0) {
597 				struct bio *q_bp;
598 
599 				s = splbio();
600 
601 				if (error == ENXIO) {
602 					/*
603 					 * Catastrophic error.  Mark our device
604 					 * as invalid.
605 					 */
606 					xpt_print_path(periph->path);
607 					printf("Invalidating device\n");
608 					softc->flags |= PT_FLAG_DEVICE_INVALID;
609 				}
610 
611 				/*
612 				 * return all queued I/O with EIO, so that
613 				 * the client can retry these I/Os in the
614 				 * proper order should it attempt to recover.
615 				 */
616 				while ((q_bp = bioq_first(&softc->bio_queue))
617 					!= NULL) {
618 					bioq_remove(&softc->bio_queue, q_bp);
619 					q_bp->bio_resid = q_bp->bio_bcount;
620 					biofinish(q_bp, NULL, EIO);
621 				}
622 				splx(s);
623 				bp->bio_error = error;
624 				bp->bio_resid = bp->bio_bcount;
625 				bp->bio_flags |= BIO_ERROR;
626 			} else {
627 				bp->bio_resid = csio->resid;
628 				bp->bio_error = 0;
629 				if (bp->bio_resid != 0) {
630 					/* Short transfer ??? */
631 					bp->bio_flags |= BIO_ERROR;
632 				}
633 			}
634 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
635 				cam_release_devq(done_ccb->ccb_h.path,
636 						 /*relsim_flags*/0,
637 						 /*reduction*/0,
638 						 /*timeout*/0,
639 						 /*getcount_only*/0);
640 		} else {
641 			bp->bio_resid = csio->resid;
642 			if (bp->bio_resid != 0)
643 				bp->bio_flags |= BIO_ERROR;
644 		}
645 
646 		/*
647 		 * Block out any asyncronous callbacks
648 		 * while we touch the pending ccb list.
649 		 */
650 		oldspl = splcam();
651 		LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
652 		splx(oldspl);
653 
654 		biofinish(bp, &softc->device_stats, 0);
655 		break;
656 	}
657 	case PT_CCB_WAITING:
658 		/* Caller will release the CCB */
659 		wakeup(&done_ccb->ccb_h.cbfcnp);
660 		return;
661 	}
662 	xpt_release_ccb(done_ccb);
663 }
664 
665 static int
666 pterror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
667 {
668 	struct pt_softc	  *softc;
669 	struct cam_periph *periph;
670 
671 	periph = xpt_path_periph(ccb->ccb_h.path);
672 	softc = (struct pt_softc *)periph->softc;
673 
674 	return(cam_periph_error(ccb, cam_flags, sense_flags,
675 				&softc->saved_ccb));
676 }
677 
678 static int
679 ptioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
680 {
681 	struct cam_periph *periph;
682 	struct pt_softc *softc;
683 	int unit;
684 	int error;
685 
686 	unit = minor(dev);
687 	periph = cam_extend_get(ptperiphs, unit);
688 
689 	if (periph == NULL)
690 		return(ENXIO);
691 
692 	softc = (struct pt_softc *)periph->softc;
693 
694 	if ((error = cam_periph_lock(periph, PRIBIO|PCATCH)) != 0) {
695 		return (error); /* error code from tsleep */
696 	}
697 
698 	switch(cmd) {
699 	case PTIOCGETTIMEOUT:
700 		if (softc->io_timeout >= 1000)
701 			*(int *)addr = softc->io_timeout / 1000;
702 		else
703 			*(int *)addr = 0;
704 		break;
705 	case PTIOCSETTIMEOUT:
706 	{
707 		int s;
708 
709 		if (*(int *)addr < 1) {
710 			error = EINVAL;
711 			break;
712 		}
713 
714 		s = splsoftcam();
715 		softc->io_timeout = *(int *)addr * 1000;
716 		splx(s);
717 
718 		break;
719 	}
720 	default:
721 		error = cam_periph_ioctl(periph, cmd, addr, pterror);
722 		break;
723 	}
724 
725 	cam_periph_unlock(periph);
726 
727 	return(error);
728 }
729 
730 void
731 scsi_send_receive(struct ccb_scsiio *csio, u_int32_t retries,
732 		  void (*cbfcnp)(struct cam_periph *, union ccb *),
733 		  u_int tag_action, int readop, u_int byte2,
734 		  u_int32_t xfer_len, u_int8_t *data_ptr, u_int8_t sense_len,
735 		  u_int32_t timeout)
736 {
737 	struct scsi_send_receive *scsi_cmd;
738 
739 	scsi_cmd = (struct scsi_send_receive *)&csio->cdb_io.cdb_bytes;
740 	scsi_cmd->opcode = readop ? RECEIVE : SEND;
741 	scsi_cmd->byte2 = byte2;
742 	scsi_ulto3b(xfer_len, scsi_cmd->xfer_len);
743 	scsi_cmd->control = 0;
744 
745 	cam_fill_csio(csio,
746 		      retries,
747 		      cbfcnp,
748 		      /*flags*/readop ? CAM_DIR_IN : CAM_DIR_OUT,
749 		      tag_action,
750 		      data_ptr,
751 		      xfer_len,
752 		      sense_len,
753 		      sizeof(*scsi_cmd),
754 		      timeout);
755 }
756