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