xref: /freebsd/sys/cam/cam_periph.c (revision 23f282aa31e9b6fceacd449020e936e98d6f2298)
1 /*
2  * Common functions for CAM "type" (peripheral) drivers.
3  *
4  * Copyright (c) 1997, 1998 Justin T. Gibbs.
5  * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions, and the following disclaimer,
13  *    without modification, immediately at the beginning of the file.
14  * 2. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/types.h>
35 #include <sys/malloc.h>
36 #include <sys/linker_set.h>
37 #include <sys/buf.h>
38 #include <sys/proc.h>
39 #include <sys/devicestat.h>
40 #include <sys/bus.h>
41 #include <vm/vm.h>
42 #include <vm/vm_extern.h>
43 
44 #include <cam/cam.h>
45 #include <cam/cam_ccb.h>
46 #include <cam/cam_xpt_periph.h>
47 #include <cam/cam_periph.h>
48 #include <cam/cam_debug.h>
49 
50 #include <cam/scsi/scsi_all.h>
51 #include <cam/scsi/scsi_message.h>
52 #include <cam/scsi/scsi_da.h>
53 #include <cam/scsi/scsi_pass.h>
54 
55 static	u_int		camperiphnextunit(struct periph_driver *p_drv,
56 					  u_int newunit, int wired);
57 static	u_int		camperiphunit(struct periph_driver *p_drv,
58 				      path_id_t path_id_t,
59 				      target_id_t target, lun_id_t lun);
60 static	void		camperiphdone(struct cam_periph *periph,
61 					union ccb *done_ccb);
62 static  void		camperiphfree(struct cam_periph *periph);
63 
64 cam_status
65 cam_periph_alloc(periph_ctor_t *periph_ctor,
66 		 periph_oninv_t *periph_oninvalidate,
67 		 periph_dtor_t *periph_dtor, periph_start_t *periph_start,
68 		 char *name, cam_periph_type type, struct cam_path *path,
69 		 ac_callback_t *ac_callback, ac_code code, void *arg)
70 {
71 	struct		periph_driver **p_drv;
72 	struct		cam_periph *periph;
73 	struct		cam_periph *cur_periph;
74 	path_id_t	path_id;
75 	target_id_t	target_id;
76 	lun_id_t	lun_id;
77 	cam_status	status;
78 	u_int		init_level;
79 	int s;
80 
81 	init_level = 0;
82 	/*
83 	 * Handle Hot-Plug scenarios.  If there is already a peripheral
84 	 * of our type assigned to this path, we are likely waiting for
85 	 * final close on an old, invalidated, peripheral.  If this is
86 	 * the case, queue up a deferred call to the peripheral's async
87 	 * handler.  If it looks like a mistaken re-alloation, complain.
88 	 */
89 	if ((periph = cam_periph_find(path, name)) != NULL) {
90 
91 		if ((periph->flags & CAM_PERIPH_INVALID) != 0
92 		 && (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) == 0) {
93 			periph->flags |= CAM_PERIPH_NEW_DEV_FOUND;
94 			periph->deferred_callback = ac_callback;
95 			periph->deferred_ac = code;
96 			return (CAM_REQ_INPROG);
97 		} else {
98 			printf("cam_periph_alloc: attempt to re-allocate "
99 			       "valid device %s%d rejected\n",
100 			       periph->periph_name, periph->unit_number);
101 		}
102 		return (CAM_REQ_INVALID);
103 	}
104 
105 	periph = (struct cam_periph *)malloc(sizeof(*periph), M_DEVBUF,
106 					     M_NOWAIT);
107 
108 	if (periph == NULL)
109 		return (CAM_RESRC_UNAVAIL);
110 
111 	init_level++;
112 
113 	for (p_drv = (struct periph_driver **)periphdriver_set.ls_items;
114 	     *p_drv != NULL; p_drv++) {
115 		if (strcmp((*p_drv)->driver_name, name) == 0)
116 			break;
117 	}
118 
119 	path_id = xpt_path_path_id(path);
120 	target_id = xpt_path_target_id(path);
121 	lun_id = xpt_path_lun_id(path);
122 	bzero(periph, sizeof(*periph));
123 	cam_init_pinfo(&periph->pinfo);
124 	periph->periph_start = periph_start;
125 	periph->periph_dtor = periph_dtor;
126 	periph->periph_oninval = periph_oninvalidate;
127 	periph->type = type;
128 	periph->periph_name = name;
129 	periph->unit_number = camperiphunit(*p_drv, path_id, target_id, lun_id);
130 	periph->immediate_priority = CAM_PRIORITY_NONE;
131 	periph->refcount = 0;
132 	SLIST_INIT(&periph->ccb_list);
133 	status = xpt_create_path(&path, periph, path_id, target_id, lun_id);
134 	if (status != CAM_REQ_CMP)
135 		goto failure;
136 
137 	periph->path = path;
138 	init_level++;
139 
140 	status = xpt_add_periph(periph);
141 
142 	if (status != CAM_REQ_CMP)
143 		goto failure;
144 
145 	s = splsoftcam();
146 	cur_periph = TAILQ_FIRST(&(*p_drv)->units);
147 	while (cur_periph != NULL
148 	    && cur_periph->unit_number < periph->unit_number)
149 		cur_periph = TAILQ_NEXT(cur_periph, unit_links);
150 
151 	if (cur_periph != NULL)
152 		TAILQ_INSERT_BEFORE(cur_periph, periph, unit_links);
153 	else {
154 		TAILQ_INSERT_TAIL(&(*p_drv)->units, periph, unit_links);
155 		(*p_drv)->generation++;
156 	}
157 
158 	splx(s);
159 
160 	init_level++;
161 
162 	status = periph_ctor(periph, arg);
163 
164 	if (status == CAM_REQ_CMP)
165 		init_level++;
166 
167 failure:
168 	switch (init_level) {
169 	case 4:
170 		/* Initialized successfully */
171 		break;
172 	case 3:
173 		s = splsoftcam();
174 		TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
175 		splx(s);
176 		xpt_remove_periph(periph);
177 	case 2:
178 		xpt_free_path(periph->path);
179 	case 1:
180 		free(periph, M_DEVBUF);
181 	case 0:
182 		/* No cleanup to perform. */
183 		break;
184 	default:
185 		panic("cam_periph_alloc: Unkown init level");
186 	}
187 	return(status);
188 }
189 
190 /*
191  * Find a peripheral structure with the specified path, target, lun,
192  * and (optionally) type.  If the name is NULL, this function will return
193  * the first peripheral driver that matches the specified path.
194  */
195 struct cam_periph *
196 cam_periph_find(struct cam_path *path, char *name)
197 {
198 	struct periph_driver **p_drv;
199 	struct cam_periph *periph;
200 	int s;
201 
202 	for (p_drv = (struct periph_driver **)periphdriver_set.ls_items;
203 	     *p_drv != NULL; p_drv++) {
204 
205 		if (name != NULL && (strcmp((*p_drv)->driver_name, name) != 0))
206 			continue;
207 
208 		s = splsoftcam();
209 		for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
210 		     periph = TAILQ_NEXT(periph, unit_links)) {
211 			if (xpt_path_comp(periph->path, path) == 0) {
212 				splx(s);
213 				return(periph);
214 			}
215 		}
216 		splx(s);
217 		if (name != NULL)
218 			return(NULL);
219 	}
220 	return(NULL);
221 }
222 
223 cam_status
224 cam_periph_acquire(struct cam_periph *periph)
225 {
226 	int s;
227 
228 	if (periph == NULL)
229 		return(CAM_REQ_CMP_ERR);
230 
231 	s = splsoftcam();
232 	periph->refcount++;
233 	splx(s);
234 
235 	return(CAM_REQ_CMP);
236 }
237 
238 void
239 cam_periph_release(struct cam_periph *periph)
240 {
241 	int s;
242 
243 	if (periph == NULL)
244 		return;
245 
246 	s = splsoftcam();
247 	if ((--periph->refcount == 0)
248 	 && (periph->flags & CAM_PERIPH_INVALID)) {
249 		camperiphfree(periph);
250 	}
251 	splx(s);
252 
253 }
254 
255 /*
256  * Look for the next unit number that is not currently in use for this
257  * peripheral type starting at "newunit".  Also exclude unit numbers that
258  * are reserved by for future "hardwiring" unless we already know that this
259  * is a potential wired device.  Only assume that the device is "wired" the
260  * first time through the loop since after that we'll be looking at unit
261  * numbers that did not match a wiring entry.
262  */
263 static u_int
264 camperiphnextunit(struct periph_driver *p_drv, u_int newunit, int wired)
265 {
266 	struct	cam_periph *periph;
267 	char	*periph_name, *strval;
268 	int	s;
269 	int	i, val, dunit;
270 	const char *dname;
271 
272 	s = splsoftcam();
273 	periph_name = p_drv->driver_name;
274 	for (;;newunit++) {
275 
276 		for (periph = TAILQ_FIRST(&p_drv->units);
277 		     periph != NULL && periph->unit_number != newunit;
278 		     periph = TAILQ_NEXT(periph, unit_links))
279 			;
280 
281 		if (periph != NULL && periph->unit_number == newunit) {
282 			if (wired != 0) {
283 				xpt_print_path(periph->path);
284 				printf("Duplicate Wired Device entry!\n");
285 				xpt_print_path(periph->path);
286 				printf("Second device will not be wired\n");
287 				wired = 0;
288 			}
289 			continue;
290 		}
291 		if (wired)
292 			break;
293 
294 		/*
295 		 * Don't match entries like "da 4" as a wired down
296 		 * device, but do match entries like "da 4 target 5"
297 		 * or even "da 4 scbus 1".
298 		 */
299 		i = -1;
300 		while ((i = resource_locate(i, periph_name)) != -1) {
301 			dname = resource_query_name(i);
302 			dunit = resource_query_unit(i);
303 			/* if no "target" and no specific scbus, skip */
304 			if (resource_int_value(dname, dunit, "target", &val) &&
305 			    (resource_string_value(dname, dunit, "at",&strval)||
306 			     strcmp(strval, "scbus") == 0))
307 				continue;
308 			if (newunit == dunit)
309 				break;
310 		}
311 		if (i == -1)
312 			break;
313 	}
314 	splx(s);
315 	return (newunit);
316 }
317 
318 static u_int
319 camperiphunit(struct periph_driver *p_drv, path_id_t pathid,
320 	      target_id_t target, lun_id_t lun)
321 {
322 	u_int	unit;
323 	int	hit, i, val, dunit;
324 	const char *dname;
325 	char	pathbuf[32], *strval, *periph_name;
326 
327 	unit = 0;
328 	hit = 0;
329 
330 	periph_name = p_drv->driver_name;
331 	snprintf(pathbuf, sizeof(pathbuf), "scbus%d", pathid);
332 	i = -1;
333 	while ((i = resource_locate(i, periph_name)) != -1) {
334 		dname = resource_query_name(i);
335 		dunit = resource_query_unit(i);
336 		if (resource_string_value(dname, dunit, "at", &strval) == 0) {
337 			if (strcmp(strval, pathbuf) != 0)
338 				continue;
339 			hit++;
340 		}
341 		if (resource_int_value(dname, dunit, "target", &val) == 0) {
342 			if (val != target)
343 				continue;
344 			hit++;
345 		}
346 		if (resource_int_value(dname, dunit, "lun", &val) == 0) {
347 			if (val != lun)
348 				continue;
349 			hit++;
350 		}
351 		if (hit != 0) {
352 			unit = dunit;
353 			break;
354 		}
355 	}
356 
357 	/*
358 	 * Either start from 0 looking for the next unit or from
359 	 * the unit number given in the resource config.  This way,
360 	 * if we have wildcard matches, we don't return the same
361 	 * unit number twice.
362 	 */
363 	unit = camperiphnextunit(p_drv, unit, /*wired*/hit);
364 
365 	return (unit);
366 }
367 
368 void
369 cam_periph_invalidate(struct cam_periph *periph)
370 {
371 	int s;
372 
373 	s = splsoftcam();
374 	/*
375 	 * We only call this routine the first time a peripheral is
376 	 * invalidated.  The oninvalidate() routine is always called at
377 	 * splsoftcam().
378 	 */
379 	if (((periph->flags & CAM_PERIPH_INVALID) == 0)
380 	 && (periph->periph_oninval != NULL))
381 		periph->periph_oninval(periph);
382 
383 	periph->flags |= CAM_PERIPH_INVALID;
384 	periph->flags &= ~CAM_PERIPH_NEW_DEV_FOUND;
385 
386 	if (periph->refcount == 0)
387 		camperiphfree(periph);
388 	else if (periph->refcount < 0)
389 		printf("cam_invalidate_periph: refcount < 0!!\n");
390 	splx(s);
391 }
392 
393 static void
394 camperiphfree(struct cam_periph *periph)
395 {
396 	int s;
397 	struct periph_driver **p_drv;
398 
399 	for (p_drv = (struct periph_driver **)periphdriver_set.ls_items;
400 	     *p_drv != NULL; p_drv++) {
401 		if (strcmp((*p_drv)->driver_name, periph->periph_name) == 0)
402 			break;
403 	}
404 
405 	if (periph->periph_dtor != NULL)
406 		periph->periph_dtor(periph);
407 
408 	s = splsoftcam();
409 	TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
410 	(*p_drv)->generation++;
411 	splx(s);
412 
413 	xpt_remove_periph(periph);
414 
415 	if (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) {
416 		union ccb ccb;
417 		void *arg;
418 
419 		switch (periph->deferred_ac) {
420 		case AC_FOUND_DEVICE:
421 			ccb.ccb_h.func_code = XPT_GDEV_TYPE;
422 			xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/ 1);
423 			xpt_action(&ccb);
424 			arg = &ccb;
425 			break;
426 		case AC_PATH_REGISTERED:
427 			ccb.ccb_h.func_code = XPT_PATH_INQ;
428 			xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/ 1);
429 			xpt_action(&ccb);
430 			arg = &ccb;
431 			break;
432 		default:
433 			arg = NULL;
434 			break;
435 		}
436 		periph->deferred_callback(NULL, periph->deferred_ac,
437 					  periph->path, arg);
438 	}
439 	xpt_free_path(periph->path);
440 	free(periph, M_DEVBUF);
441 }
442 
443 /*
444  * Wait interruptibly for an exclusive lock.
445  */
446 int
447 cam_periph_lock(struct cam_periph *periph, int priority)
448 {
449 	int error;
450 
451 	while ((periph->flags & CAM_PERIPH_LOCKED) != 0) {
452 		periph->flags |= CAM_PERIPH_LOCK_WANTED;
453 		if ((error = tsleep(periph, priority, "caplck", 0)) != 0)
454 			return error;
455 	}
456 
457 	if (cam_periph_acquire(periph) != CAM_REQ_CMP)
458 		return(ENXIO);
459 
460 	periph->flags |= CAM_PERIPH_LOCKED;
461 	return 0;
462 }
463 
464 /*
465  * Unlock and wake up any waiters.
466  */
467 void
468 cam_periph_unlock(struct cam_periph *periph)
469 {
470 	periph->flags &= ~CAM_PERIPH_LOCKED;
471 	if ((periph->flags & CAM_PERIPH_LOCK_WANTED) != 0) {
472 		periph->flags &= ~CAM_PERIPH_LOCK_WANTED;
473 		wakeup(periph);
474 	}
475 
476 	cam_periph_release(periph);
477 }
478 
479 /*
480  * Map user virtual pointers into kernel virtual address space, so we can
481  * access the memory.  This won't work on physical pointers, for now it's
482  * up to the caller to check for that.  (XXX KDM -- should we do that here
483  * instead?)  This also only works for up to MAXPHYS memory.  Since we use
484  * buffers to map stuff in and out, we're limited to the buffer size.
485  */
486 int
487 cam_periph_mapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
488 {
489 	int numbufs, i;
490 	int flags[CAM_PERIPH_MAXMAPS];
491 	u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
492 	u_int32_t lengths[CAM_PERIPH_MAXMAPS];
493 	u_int32_t dirs[CAM_PERIPH_MAXMAPS];
494 
495 	switch(ccb->ccb_h.func_code) {
496 	case XPT_DEV_MATCH:
497 		if (ccb->cdm.match_buf_len == 0) {
498 			printf("cam_periph_mapmem: invalid match buffer "
499 			       "length 0\n");
500 			return(EINVAL);
501 		}
502 		if (ccb->cdm.pattern_buf_len > 0) {
503 			data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
504 			lengths[0] = ccb->cdm.pattern_buf_len;
505 			dirs[0] = CAM_DIR_OUT;
506 			data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
507 			lengths[1] = ccb->cdm.match_buf_len;
508 			dirs[1] = CAM_DIR_IN;
509 			numbufs = 2;
510 		} else {
511 			data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
512 			lengths[0] = ccb->cdm.match_buf_len;
513 			dirs[0] = CAM_DIR_IN;
514 			numbufs = 1;
515 		}
516 		break;
517 	case XPT_SCSI_IO:
518 	case XPT_CONT_TARGET_IO:
519 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
520 			return(0);
521 
522 		data_ptrs[0] = &ccb->csio.data_ptr;
523 		lengths[0] = ccb->csio.dxfer_len;
524 		dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
525 		numbufs = 1;
526 		break;
527 	default:
528 		return(EINVAL);
529 		break; /* NOTREACHED */
530 	}
531 
532 	/*
533 	 * Check the transfer length and permissions first, so we don't
534 	 * have to unmap any previously mapped buffers.
535 	 */
536 	for (i = 0; i < numbufs; i++) {
537 
538 		flags[i] = 0;
539 
540 		/*
541 		 * The userland data pointer passed in may not be page
542 		 * aligned.  vmapbuf() truncates the address to a page
543 		 * boundary, so if the address isn't page aligned, we'll
544 		 * need enough space for the given transfer length, plus
545 		 * whatever extra space is necessary to make it to the page
546 		 * boundary.
547 		 */
548 		if ((lengths[i] +
549 		    (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)) > DFLTPHYS){
550 			printf("cam_periph_mapmem: attempt to map %lu bytes, "
551 			       "which is greater than DFLTPHYS(%d)\n",
552 			       (long)(lengths[i] +
553 			       (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)),
554 			       DFLTPHYS);
555 			return(E2BIG);
556 		}
557 
558 		if (dirs[i] & CAM_DIR_OUT) {
559 			flags[i] = BIO_WRITE;
560 			if (!useracc(*data_ptrs[i], lengths[i],
561 				     VM_PROT_READ)) {
562 				printf("cam_periph_mapmem: error, "
563 					"address %p, length %lu isn't "
564 					"user accessible for READ\n",
565 					(void *)*data_ptrs[i],
566 					(u_long)lengths[i]);
567 				return(EACCES);
568 			}
569 		}
570 
571 		if (dirs[i] & CAM_DIR_IN) {
572 			flags[i] = BIO_READ;
573 			if (!useracc(*data_ptrs[i], lengths[i],
574 				     VM_PROT_WRITE)) {
575 				printf("cam_periph_mapmem: error, "
576 					"address %p, length %lu isn't "
577 					"user accessible for WRITE\n",
578 					(void *)*data_ptrs[i],
579 					(u_long)lengths[i]);
580 
581 				return(EACCES);
582 			}
583 		}
584 
585 	}
586 
587 	/* this keeps the current process from getting swapped */
588 	/*
589 	 * XXX KDM should I use P_NOSWAP instead?
590 	 */
591 	PHOLD(curproc);
592 
593 	for (i = 0; i < numbufs; i++) {
594 		/*
595 		 * Get the buffer.
596 		 */
597 		mapinfo->bp[i] = getpbuf(NULL);
598 
599 		/* save the buffer's data address */
600 		mapinfo->bp[i]->b_saveaddr = mapinfo->bp[i]->b_data;
601 
602 		/* put our pointer in the data slot */
603 		mapinfo->bp[i]->b_data = *data_ptrs[i];
604 
605 		/* set the transfer length, we know it's < DFLTPHYS */
606 		mapinfo->bp[i]->b_bufsize = lengths[i];
607 
608 		/* set the flags */
609 		mapinfo->bp[i]->b_flags = B_PHYS;
610 
611 		/* set the direction */
612 		mapinfo->bp[i]->b_iocmd = flags[i];
613 
614 		/* map the buffer into kernel memory */
615 		vmapbuf(mapinfo->bp[i]);
616 
617 		/* set our pointer to the new mapped area */
618 		*data_ptrs[i] = mapinfo->bp[i]->b_data;
619 
620 		mapinfo->num_bufs_used++;
621 	}
622 
623 	return(0);
624 }
625 
626 /*
627  * Unmap memory segments mapped into kernel virtual address space by
628  * cam_periph_mapmem().
629  */
630 void
631 cam_periph_unmapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
632 {
633 	int numbufs, i;
634 	u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
635 
636 	if (mapinfo->num_bufs_used <= 0) {
637 		/* allow ourselves to be swapped once again */
638 		PRELE(curproc);
639 		return;
640 	}
641 
642 	switch (ccb->ccb_h.func_code) {
643 	case XPT_DEV_MATCH:
644 		numbufs = min(mapinfo->num_bufs_used, 2);
645 
646 		if (numbufs == 1) {
647 			data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
648 		} else {
649 			data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
650 			data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
651 		}
652 		break;
653 	case XPT_SCSI_IO:
654 	case XPT_CONT_TARGET_IO:
655 		data_ptrs[0] = &ccb->csio.data_ptr;
656 		numbufs = min(mapinfo->num_bufs_used, 1);
657 		break;
658 	default:
659 		/* allow ourselves to be swapped once again */
660 		PRELE(curproc);
661 		return;
662 		break; /* NOTREACHED */
663 	}
664 
665 	for (i = 0; i < numbufs; i++) {
666 		/* Set the user's pointer back to the original value */
667 		*data_ptrs[i] = mapinfo->bp[i]->b_saveaddr;
668 
669 		/* unmap the buffer */
670 		vunmapbuf(mapinfo->bp[i]);
671 
672 		/* clear the flags we set above */
673 		mapinfo->bp[i]->b_flags &= ~B_PHYS;
674 
675 		/* release the buffer */
676 		relpbuf(mapinfo->bp[i], NULL);
677 	}
678 
679 	/* allow ourselves to be swapped once again */
680 	PRELE(curproc);
681 }
682 
683 union ccb *
684 cam_periph_getccb(struct cam_periph *periph, u_int32_t priority)
685 {
686 	struct ccb_hdr *ccb_h;
687 	int s;
688 
689 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdgetccb\n"));
690 
691 	s = splsoftcam();
692 
693 	while (periph->ccb_list.slh_first == NULL) {
694 		if (periph->immediate_priority > priority)
695 			periph->immediate_priority = priority;
696 		xpt_schedule(periph, priority);
697 		if ((periph->ccb_list.slh_first != NULL)
698 		 && (periph->ccb_list.slh_first->pinfo.priority == priority))
699 			break;
700 		tsleep(&periph->ccb_list, PRIBIO, "cgticb", 0);
701 	}
702 
703 	ccb_h = periph->ccb_list.slh_first;
704 	SLIST_REMOVE_HEAD(&periph->ccb_list, periph_links.sle);
705 	splx(s);
706 	return ((union ccb *)ccb_h);
707 }
708 
709 void
710 cam_periph_ccbwait(union ccb *ccb)
711 {
712 	int s;
713 
714 	s = splsoftcam();
715 	if ((ccb->ccb_h.pinfo.index != CAM_UNQUEUED_INDEX)
716 	 || ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG))
717 		tsleep(&ccb->ccb_h.cbfcnp, PRIBIO, "cbwait", 0);
718 
719 	splx(s);
720 }
721 
722 int
723 cam_periph_ioctl(struct cam_periph *periph, int cmd, caddr_t addr,
724 		 int (*error_routine)(union ccb *ccb,
725 				      cam_flags camflags,
726 				      u_int32_t sense_flags))
727 {
728 	union ccb 	     *ccb;
729 	int 		     error;
730 	int		     found;
731 
732 	error = found = 0;
733 
734 	switch(cmd){
735 	case CAMGETPASSTHRU:
736 		ccb = cam_periph_getccb(periph, /* priority */ 1);
737 		xpt_setup_ccb(&ccb->ccb_h,
738 			      ccb->ccb_h.path,
739 			      /*priority*/1);
740 		ccb->ccb_h.func_code = XPT_GDEVLIST;
741 
742 		/*
743 		 * Basically, the point of this is that we go through
744 		 * getting the list of devices, until we find a passthrough
745 		 * device.  In the current version of the CAM code, the
746 		 * only way to determine what type of device we're dealing
747 		 * with is by its name.
748 		 */
749 		while (found == 0) {
750 			ccb->cgdl.index = 0;
751 			ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS;
752 			while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) {
753 
754 				/* we want the next device in the list */
755 				xpt_action(ccb);
756 				if (strncmp(ccb->cgdl.periph_name,
757 				    "pass", 4) == 0){
758 					found = 1;
759 					break;
760 				}
761 			}
762 			if ((ccb->cgdl.status == CAM_GDEVLIST_LAST_DEVICE) &&
763 			    (found == 0)) {
764 				ccb->cgdl.periph_name[0] = '\0';
765 				ccb->cgdl.unit_number = 0;
766 				break;
767 			}
768 		}
769 
770 		/* copy the result back out */
771 		bcopy(ccb, addr, sizeof(union ccb));
772 
773 		/* and release the ccb */
774 		xpt_release_ccb(ccb);
775 
776 		break;
777 	default:
778 		error = ENOTTY;
779 		break;
780 	}
781 	return(error);
782 }
783 
784 int
785 cam_periph_runccb(union ccb *ccb,
786 		  int (*error_routine)(union ccb *ccb,
787 				       cam_flags camflags,
788 				       u_int32_t sense_flags),
789 		  cam_flags camflags, u_int32_t sense_flags,
790 		  struct devstat *ds)
791 {
792 	int error;
793 
794 	error = 0;
795 
796 	/*
797 	 * If the user has supplied a stats structure, and if we understand
798 	 * this particular type of ccb, record the transaction start.
799 	 */
800 	if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO))
801 		devstat_start_transaction(ds);
802 
803 	xpt_action(ccb);
804 
805 	do {
806 		cam_periph_ccbwait(ccb);
807 		if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
808 			error = 0;
809 		else if (error_routine != NULL)
810 			error = (*error_routine)(ccb, camflags, sense_flags);
811 		else
812 			error = 0;
813 
814 	} while (error == ERESTART);
815 
816 	if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
817 		cam_release_devq(ccb->ccb_h.path,
818 				 /* relsim_flags */0,
819 				 /* openings */0,
820 				 /* timeout */0,
821 				 /* getcount_only */ FALSE);
822 
823 	if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO))
824 		devstat_end_transaction(ds,
825 					ccb->csio.dxfer_len,
826 					ccb->csio.tag_action & 0xf,
827 					((ccb->ccb_h.flags & CAM_DIR_MASK) ==
828 					CAM_DIR_NONE) ?  DEVSTAT_NO_DATA :
829 					(ccb->ccb_h.flags & CAM_DIR_OUT) ?
830 					DEVSTAT_WRITE :
831 					DEVSTAT_READ);
832 
833 	return(error);
834 }
835 
836 void
837 cam_freeze_devq(struct cam_path *path)
838 {
839 	struct ccb_hdr ccb_h;
840 
841 	xpt_setup_ccb(&ccb_h, path, /*priority*/1);
842 	ccb_h.func_code = XPT_NOOP;
843 	ccb_h.flags = CAM_DEV_QFREEZE;
844 	xpt_action((union ccb *)&ccb_h);
845 }
846 
847 u_int32_t
848 cam_release_devq(struct cam_path *path, u_int32_t relsim_flags,
849 		 u_int32_t openings, u_int32_t timeout,
850 		 int getcount_only)
851 {
852 	struct ccb_relsim crs;
853 
854 	xpt_setup_ccb(&crs.ccb_h, path,
855 		      /*priority*/1);
856 	crs.ccb_h.func_code = XPT_REL_SIMQ;
857 	crs.ccb_h.flags = getcount_only ? CAM_DEV_QFREEZE : 0;
858 	crs.release_flags = relsim_flags;
859 	crs.openings = openings;
860 	crs.release_timeout = timeout;
861 	xpt_action((union ccb *)&crs);
862 	return (crs.qfrozen_cnt);
863 }
864 
865 #define saved_ccb_ptr ppriv_ptr0
866 static void
867 camperiphdone(struct cam_periph *periph, union ccb *done_ccb)
868 {
869 	cam_status	status;
870 	int		frozen;
871 	int		sense;
872 	struct scsi_start_stop_unit *scsi_cmd;
873 	u_int32_t	relsim_flags, timeout;
874 	u_int32_t	qfrozen_cnt;
875 
876 	status = done_ccb->ccb_h.status;
877 	frozen = (status & CAM_DEV_QFRZN) != 0;
878 	sense  = (status & CAM_AUTOSNS_VALID) != 0;
879 	status &= CAM_STATUS_MASK;
880 
881 	timeout = 0;
882 	relsim_flags = 0;
883 
884 	/*
885 	 * Unfreeze the queue once if it is already frozen..
886 	 */
887 	if (frozen != 0) {
888 		qfrozen_cnt = cam_release_devq(done_ccb->ccb_h.path,
889 					      /*relsim_flags*/0,
890 					      /*openings*/0,
891 					      /*timeout*/0,
892 					      /*getcount_only*/0);
893 	}
894 
895 	switch (status) {
896 
897 	case CAM_REQ_CMP:
898 
899 		/*
900 		 * If we have successfully taken a device from the not
901 		 * ready to ready state, re-scan the device and re-get the
902 		 * inquiry information.  Many devices (mostly disks) don't
903 		 * properly report their inquiry information unless they
904 		 * are spun up.
905 		 */
906 		if (done_ccb->ccb_h.func_code == XPT_SCSI_IO) {
907 			scsi_cmd = (struct scsi_start_stop_unit *)
908 					&done_ccb->csio.cdb_io.cdb_bytes;
909 
910 		 	if (scsi_cmd->opcode == START_STOP_UNIT)
911 				xpt_async(AC_INQ_CHANGED,
912 					  done_ccb->ccb_h.path, NULL);
913 		}
914 		bcopy(done_ccb->ccb_h.saved_ccb_ptr, done_ccb,
915 		      sizeof(union ccb));
916 
917 		periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
918 
919 		xpt_action(done_ccb);
920 
921 		break;
922 	case CAM_SCSI_STATUS_ERROR:
923 		scsi_cmd = (struct scsi_start_stop_unit *)
924 				&done_ccb->csio.cdb_io.cdb_bytes;
925 		if (sense != 0) {
926 			struct scsi_sense_data *sense;
927 			int    error_code, sense_key, asc, ascq;
928 
929 			sense = &done_ccb->csio.sense_data;
930 			scsi_extract_sense(sense, &error_code,
931 					   &sense_key, &asc, &ascq);
932 
933 			/*
934 	 		 * If the error is "invalid field in CDB",
935 			 * and the load/eject flag is set, turn the
936 			 * flag off and try again.  This is just in
937 			 * case the drive in question barfs on the
938 			 * load eject flag.  The CAM code should set
939 			 * the load/eject flag by default for
940 			 * removable media.
941 			 */
942 
943 			/* XXX KDM
944 			 * Should we check to see what the specific
945 			 * scsi status is??  Or does it not matter
946 			 * since we already know that there was an
947 			 * error, and we know what the specific
948 			 * error code was, and we know what the
949 			 * opcode is..
950 			 */
951 			if ((scsi_cmd->opcode == START_STOP_UNIT) &&
952 			    ((scsi_cmd->how & SSS_LOEJ) != 0) &&
953 			     (asc == 0x24) && (ascq == 0x00) &&
954 			     (done_ccb->ccb_h.retry_count > 0)) {
955 
956 				scsi_cmd->how &= ~SSS_LOEJ;
957 
958 				xpt_action(done_ccb);
959 
960 			} else if (done_ccb->ccb_h.retry_count > 0) {
961 				/*
962 				 * In this case, the error recovery
963 				 * command failed, but we've got
964 				 * some retries left on it.  Give
965 				 * it another try.
966 				 */
967 
968 				/* set the timeout to .5 sec */
969 				relsim_flags =
970 					RELSIM_RELEASE_AFTER_TIMEOUT;
971 				timeout = 500;
972 
973 				xpt_action(done_ccb);
974 
975 				break;
976 
977 			} else {
978 				/*
979 				 * Copy the original CCB back and
980 				 * send it back to the caller.
981 				 */
982 				bcopy(done_ccb->ccb_h.saved_ccb_ptr,
983 				      done_ccb, sizeof(union ccb));
984 
985 				periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
986 
987 				xpt_action(done_ccb);
988 			}
989 		} else {
990 			/*
991 			 * Eh??  The command failed, but we don't
992 			 * have any sense.  What's up with that?
993 			 * Fire the CCB again to return it to the
994 			 * caller.
995 			 */
996 			bcopy(done_ccb->ccb_h.saved_ccb_ptr,
997 			      done_ccb, sizeof(union ccb));
998 
999 			periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1000 
1001 			xpt_action(done_ccb);
1002 
1003 		}
1004 		break;
1005 	default:
1006 		bcopy(done_ccb->ccb_h.saved_ccb_ptr, done_ccb,
1007 		      sizeof(union ccb));
1008 
1009 		periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1010 
1011 		xpt_action(done_ccb);
1012 
1013 		break;
1014 	}
1015 
1016 	/* decrement the retry count */
1017 	if (done_ccb->ccb_h.retry_count > 0)
1018 		done_ccb->ccb_h.retry_count--;
1019 
1020 	qfrozen_cnt = cam_release_devq(done_ccb->ccb_h.path,
1021 				      /*relsim_flags*/relsim_flags,
1022 				      /*openings*/0,
1023 				      /*timeout*/timeout,
1024 				      /*getcount_only*/0);
1025 }
1026 
1027 /*
1028  * Generic Async Event handler.  Peripheral drivers usually
1029  * filter out the events that require personal attention,
1030  * and leave the rest to this function.
1031  */
1032 void
1033 cam_periph_async(struct cam_periph *periph, u_int32_t code,
1034 		 struct cam_path *path, void *arg)
1035 {
1036 	switch (code) {
1037 	case AC_LOST_DEVICE:
1038 		cam_periph_invalidate(periph);
1039 		break;
1040 	case AC_SENT_BDR:
1041 	case AC_BUS_RESET:
1042 	{
1043 		cam_periph_bus_settle(periph, SCSI_DELAY);
1044 		break;
1045 	}
1046 	default:
1047 		break;
1048 	}
1049 }
1050 
1051 void
1052 cam_periph_bus_settle(struct cam_periph *periph, u_int bus_settle)
1053 {
1054 	struct ccb_getdevstats cgds;
1055 
1056 	xpt_setup_ccb(&cgds.ccb_h, periph->path, /*priority*/1);
1057 	cgds.ccb_h.func_code = XPT_GDEV_STATS;
1058 	xpt_action((union ccb *)&cgds);
1059 	cam_periph_freeze_after_event(periph, &cgds.last_reset, bus_settle);
1060 }
1061 
1062 void
1063 cam_periph_freeze_after_event(struct cam_periph *periph,
1064 			      struct timeval* event_time, u_int duration_ms)
1065 {
1066 	struct timeval delta;
1067 	struct timeval duration_tv;
1068 	int s;
1069 
1070 	s = splclock();
1071 	microtime(&delta);
1072 	splx(s);
1073 	timevalsub(&delta, event_time);
1074 	duration_tv.tv_sec = duration_ms / 1000;
1075 	duration_tv.tv_usec = (duration_ms % 1000) * 1000;
1076 	if (timevalcmp(&delta, &duration_tv, <)) {
1077 		timevalsub(&duration_tv, &delta);
1078 
1079 		duration_ms = duration_tv.tv_sec * 1000;
1080 		duration_ms += duration_tv.tv_usec / 1000;
1081 		cam_freeze_devq(periph->path);
1082 		cam_release_devq(periph->path,
1083 				RELSIM_RELEASE_AFTER_TIMEOUT,
1084 				/*reduction*/0,
1085 				/*timeout*/duration_ms,
1086 				/*getcount_only*/0);
1087 	}
1088 
1089 }
1090 
1091 /*
1092  * Generic error handler.  Peripheral drivers usually filter
1093  * out the errors that they handle in a unique mannor, then
1094  * call this function.
1095  */
1096 int
1097 cam_periph_error(union ccb *ccb, cam_flags camflags,
1098 		 u_int32_t sense_flags, union ccb *save_ccb)
1099 {
1100 	cam_status status;
1101 	int	   frozen;
1102 	int	   sense;
1103 	int	   error;
1104 	int        openings;
1105 	int	   retry;
1106 	u_int32_t  relsim_flags;
1107 	u_int32_t  timeout;
1108 
1109 	status = ccb->ccb_h.status;
1110 	frozen = (status & CAM_DEV_QFRZN) != 0;
1111 	sense  = (status & CAM_AUTOSNS_VALID) != 0;
1112 	status &= CAM_STATUS_MASK;
1113 	relsim_flags = 0;
1114 
1115 	switch (status) {
1116 	case CAM_REQ_CMP:
1117 		/* decrement the number of retries */
1118 		retry = ccb->ccb_h.retry_count > 0;
1119 		if (retry)
1120 			ccb->ccb_h.retry_count--;
1121 		error = 0;
1122 		break;
1123 	case CAM_AUTOSENSE_FAIL:
1124 	case CAM_SCSI_STATUS_ERROR:
1125 
1126 		switch (ccb->csio.scsi_status) {
1127 		case SCSI_STATUS_OK:
1128 		case SCSI_STATUS_COND_MET:
1129 		case SCSI_STATUS_INTERMED:
1130 		case SCSI_STATUS_INTERMED_COND_MET:
1131 			error = 0;
1132 			break;
1133 		case SCSI_STATUS_CMD_TERMINATED:
1134 		case SCSI_STATUS_CHECK_COND:
1135 			if (sense != 0) {
1136 				struct scsi_sense_data *sense;
1137 				int    error_code, sense_key, asc, ascq;
1138 				struct cam_periph *periph;
1139 				scsi_sense_action err_action;
1140 				struct ccb_getdev cgd;
1141 
1142 				sense = &ccb->csio.sense_data;
1143 				scsi_extract_sense(sense, &error_code,
1144 						   &sense_key, &asc, &ascq);
1145 				periph = xpt_path_periph(ccb->ccb_h.path);
1146 
1147 				/*
1148 				 * Grab the inquiry data for this device.
1149 				 */
1150 				xpt_setup_ccb(&cgd.ccb_h, ccb->ccb_h.path,
1151 					      /*priority*/ 1);
1152 				cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1153 				xpt_action((union ccb *)&cgd);
1154 
1155 				err_action = scsi_error_action(asc, ascq,
1156 							       &cgd.inq_data);
1157 
1158 				/*
1159 				 * Send a Test Unit Ready to the device.
1160 				 * If the 'many' flag is set, we send 120
1161 				 * test unit ready commands, one every half
1162 				 * second.  Otherwise, we just send one TUR.
1163 				 * We only want to do this if the retry
1164 				 * count has not been exhausted.
1165 				 */
1166 				if (((err_action & SS_MASK) == SS_TUR)
1167 				 && save_ccb != NULL
1168 				 && ccb->ccb_h.retry_count > 0) {
1169 
1170 					/*
1171 					 * Since error recovery is already
1172 					 * in progress, don't attempt to
1173 					 * process this error.  It is probably
1174 					 * related to the error that caused
1175 					 * the currently active error recovery
1176 					 * action.  Also, we only have
1177 					 * space for one saved CCB, so if we
1178 					 * had two concurrent error recovery
1179 					 * actions, we would end up
1180 					 * over-writing one error recovery
1181 					 * CCB with another one.
1182 					 */
1183 					if (periph->flags &
1184 					    CAM_PERIPH_RECOVERY_INPROG) {
1185 						error = ERESTART;
1186 						break;
1187 					}
1188 
1189 					periph->flags |=
1190 						CAM_PERIPH_RECOVERY_INPROG;
1191 
1192 					/* decrement the number of retries */
1193 					if ((err_action &
1194 					     SSQ_DECREMENT_COUNT) != 0) {
1195 						retry = 1;
1196 						ccb->ccb_h.retry_count--;
1197 					}
1198 
1199 					bcopy(ccb, save_ccb, sizeof(*save_ccb));
1200 
1201 					/*
1202 					 * We retry this one every half
1203 					 * second for a minute.  If the
1204 					 * device hasn't become ready in a
1205 					 * minute's time, it's unlikely to
1206 					 * ever become ready.  If the table
1207 					 * doesn't specify SSQ_MANY, we can
1208 					 * only try this once.  Oh well.
1209 					 */
1210 					if ((err_action & SSQ_MANY) != 0)
1211 						scsi_test_unit_ready(&ccb->csio,
1212 							       /*retries*/120,
1213 							       camperiphdone,
1214 						 	       MSG_SIMPLE_Q_TAG,
1215 							       SSD_FULL_SIZE,
1216 							       /*timeout*/5000);
1217 					else
1218 						scsi_test_unit_ready(&ccb->csio,
1219 							       /*retries*/1,
1220 							       camperiphdone,
1221 						 	       MSG_SIMPLE_Q_TAG,
1222 							       SSD_FULL_SIZE,
1223 							       /*timeout*/5000);
1224 
1225 					/* release the queue after .5 sec.  */
1226 					relsim_flags =
1227 						RELSIM_RELEASE_AFTER_TIMEOUT;
1228 					timeout = 500;
1229 					/*
1230 					 * Drop the priority to 0 so that
1231 					 * we are the first to execute.  Also
1232 					 * freeze the queue after this command
1233 					 * is sent so that we can restore the
1234 					 * old csio and have it queued in the
1235 					 * proper order before we let normal
1236 					 * transactions go to the drive.
1237 					 */
1238 					ccb->ccb_h.pinfo.priority = 0;
1239 					ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1240 
1241 					/*
1242 					 * Save a pointer to the original
1243 					 * CCB in the new CCB.
1244 					 */
1245 					ccb->ccb_h.saved_ccb_ptr = save_ccb;
1246 
1247 					error = ERESTART;
1248 				}
1249 				/*
1250 				 * Send a start unit command to the device,
1251 				 * and then retry the command.  We only
1252 				 * want to do this if the retry count has
1253 				 * not been exhausted.  If the user
1254 				 * specified 0 retries, then we follow
1255 				 * their request and do not retry.
1256 				 */
1257 				else if (((err_action & SS_MASK) == SS_START)
1258 				      && save_ccb != NULL
1259 				      && ccb->ccb_h.retry_count > 0) {
1260 					int le;
1261 
1262 					/*
1263 					 * Only one error recovery action
1264 					 * at a time.  See above.
1265 					 */
1266 					if (periph->flags &
1267 					    CAM_PERIPH_RECOVERY_INPROG) {
1268 						error = ERESTART;
1269 						break;
1270 					}
1271 
1272 					periph->flags |=
1273 						CAM_PERIPH_RECOVERY_INPROG;
1274 
1275 					/* decrement the number of retries */
1276 					retry = 1;
1277 					ccb->ccb_h.retry_count--;
1278 
1279 					/*
1280 					 * Check for removable media and
1281 					 * set load/eject flag
1282 					 * appropriately.
1283 					 */
1284 					if (SID_IS_REMOVABLE(&cgd.inq_data))
1285 						le = TRUE;
1286 					else
1287 						le = FALSE;
1288 
1289 					/*
1290 					 * Attempt to start the drive up.
1291 					 *
1292 					 * Save the current ccb so it can
1293 					 * be restored and retried once the
1294 					 * drive is started up.
1295 					 */
1296 					bcopy(ccb, save_ccb, sizeof(*save_ccb));
1297 
1298 					scsi_start_stop(&ccb->csio,
1299 							/*retries*/1,
1300 							camperiphdone,
1301 							MSG_SIMPLE_Q_TAG,
1302 							/*start*/TRUE,
1303 							/*load/eject*/le,
1304 							/*immediate*/FALSE,
1305 							SSD_FULL_SIZE,
1306 							/*timeout*/50000);
1307 					/*
1308 					 * Drop the priority to 0 so that
1309 					 * we are the first to execute.  Also
1310 					 * freeze the queue after this command
1311 					 * is sent so that we can restore the
1312 					 * old csio and have it queued in the
1313 					 * proper order before we let normal
1314 					 * transactions go to the drive.
1315 					 */
1316 					ccb->ccb_h.pinfo.priority = 0;
1317 					ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1318 
1319 					/*
1320 					 * Save a pointer to the original
1321 					 * CCB in the new CCB.
1322 					 */
1323 					ccb->ccb_h.saved_ccb_ptr = save_ccb;
1324 
1325 					error = ERESTART;
1326 				} else if ((sense_flags & SF_RETRY_UA) != 0) {
1327 					/*
1328 					 * XXX KDM this is a *horrible*
1329 					 * hack.
1330 					 */
1331 					error = scsi_interpret_sense(ccb,
1332 								  sense_flags,
1333 								  &relsim_flags,
1334 								  &openings,
1335 								  &timeout,
1336 								  err_action);
1337 				}
1338 
1339 				/*
1340 				 * Theoretically, this code should send a
1341 				 * test unit ready to the given device, and
1342 				 * if it returns and error, send a start
1343 				 * unit command.  Since we don't yet have
1344 				 * the capability to do two-command error
1345 				 * recovery, just send a start unit.
1346 				 * XXX KDM fix this!
1347 				 */
1348 				else if (((err_action & SS_MASK) == SS_TURSTART)
1349 				      && save_ccb != NULL
1350 				      && ccb->ccb_h.retry_count > 0) {
1351 					int le;
1352 
1353 					/*
1354 					 * Only one error recovery action
1355 					 * at a time.  See above.
1356 					 */
1357 					if (periph->flags &
1358 					    CAM_PERIPH_RECOVERY_INPROG) {
1359 						error = ERESTART;
1360 						break;
1361 					}
1362 
1363 					periph->flags |=
1364 						CAM_PERIPH_RECOVERY_INPROG;
1365 
1366 					/* decrement the number of retries */
1367 					retry = 1;
1368 					ccb->ccb_h.retry_count--;
1369 
1370 					/*
1371 					 * Check for removable media and
1372 					 * set load/eject flag
1373 					 * appropriately.
1374 					 */
1375 					if (SID_IS_REMOVABLE(&cgd.inq_data))
1376 						le = TRUE;
1377 					else
1378 						le = FALSE;
1379 
1380 					/*
1381 					 * Attempt to start the drive up.
1382 					 *
1383 					 * Save the current ccb so it can
1384 					 * be restored and retried once the
1385 					 * drive is started up.
1386 					 */
1387 					bcopy(ccb, save_ccb, sizeof(*save_ccb));
1388 
1389 					scsi_start_stop(&ccb->csio,
1390 							/*retries*/1,
1391 							camperiphdone,
1392 							MSG_SIMPLE_Q_TAG,
1393 							/*start*/TRUE,
1394 							/*load/eject*/le,
1395 							/*immediate*/FALSE,
1396 							SSD_FULL_SIZE,
1397 							/*timeout*/50000);
1398 
1399 					/* release the queue after .5 sec.  */
1400 					relsim_flags =
1401 						RELSIM_RELEASE_AFTER_TIMEOUT;
1402 					timeout = 500;
1403 					/*
1404 					 * Drop the priority to 0 so that
1405 					 * we are the first to execute.  Also
1406 					 * freeze the queue after this command
1407 					 * is sent so that we can restore the
1408 					 * old csio and have it queued in the
1409 					 * proper order before we let normal
1410 					 * transactions go to the drive.
1411 					 */
1412 					ccb->ccb_h.pinfo.priority = 0;
1413 					ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1414 
1415 					/*
1416 					 * Save a pointer to the original
1417 					 * CCB in the new CCB.
1418 					 */
1419 					ccb->ccb_h.saved_ccb_ptr = save_ccb;
1420 
1421 					error = ERESTART;
1422 				} else {
1423 					error = scsi_interpret_sense(ccb,
1424 								  sense_flags,
1425 								  &relsim_flags,
1426 								  &openings,
1427 								  &timeout,
1428 								  err_action);
1429 				}
1430 			} else if (ccb->csio.scsi_status ==
1431 				   SCSI_STATUS_CHECK_COND
1432 				&& status != CAM_AUTOSENSE_FAIL) {
1433 				/* no point in decrementing the retry count */
1434 				panic("cam_periph_error: scsi status of "
1435 				      "CHECK COND returned but no sense "
1436 				      "information is availible.  "
1437 				      "Controller should have returned "
1438 				      "CAM_AUTOSENSE_FAILED");
1439 				/* NOTREACHED */
1440 				error = EIO;
1441 			} else if (ccb->ccb_h.retry_count == 0) {
1442 				/*
1443 				 * XXX KDM shouldn't there be a better
1444 				 * argument to return??
1445 				 */
1446 				error = EIO;
1447 			} else {
1448 				/* decrement the number of retries */
1449 				retry = ccb->ccb_h.retry_count > 0;
1450 				if (retry)
1451 					ccb->ccb_h.retry_count--;
1452 				/*
1453 				 * If it was aborted with no
1454 				 * clue as to the reason, just
1455 				 * retry it again.
1456 				 */
1457 				error = ERESTART;
1458 			}
1459 			break;
1460 		case SCSI_STATUS_QUEUE_FULL:
1461 		{
1462 			/* no decrement */
1463 			struct ccb_getdevstats cgds;
1464 
1465 			/*
1466 			 * First off, find out what the current
1467 			 * transaction counts are.
1468 			 */
1469 			xpt_setup_ccb(&cgds.ccb_h,
1470 				      ccb->ccb_h.path,
1471 				      /*priority*/1);
1472 			cgds.ccb_h.func_code = XPT_GDEV_STATS;
1473 			xpt_action((union ccb *)&cgds);
1474 
1475 			/*
1476 			 * If we were the only transaction active, treat
1477 			 * the QUEUE FULL as if it were a BUSY condition.
1478 			 */
1479 			if (cgds.dev_active != 0) {
1480 				int total_openings;
1481 
1482 				/*
1483 			 	 * Reduce the number of openings to
1484 				 * be 1 less than the amount it took
1485 				 * to get a queue full bounded by the
1486 				 * minimum allowed tag count for this
1487 				 * device.
1488 			 	 */
1489 				total_openings =
1490 				    cgds.dev_active+cgds.dev_openings;
1491 				openings = cgds.dev_active;
1492 				if (openings < cgds.mintags)
1493 					openings = cgds.mintags;
1494 				if (openings < total_openings)
1495 					relsim_flags = RELSIM_ADJUST_OPENINGS;
1496 				else {
1497 					/*
1498 					 * Some devices report queue full for
1499 					 * temporary resource shortages.  For
1500 					 * this reason, we allow a minimum
1501 					 * tag count to be entered via a
1502 					 * quirk entry to prevent the queue
1503 					 * count on these devices from falling
1504 					 * to a pessimisticly low value.  We
1505 					 * still wait for the next successful
1506 					 * completion, however, before queueing
1507 					 * more transactions to the device.
1508 					 */
1509 					relsim_flags =
1510 					    RELSIM_RELEASE_AFTER_CMDCMPLT;
1511 				}
1512 				timeout = 0;
1513 				error = ERESTART;
1514 				break;
1515 			}
1516 			/* FALLTHROUGH */
1517 		}
1518 		case SCSI_STATUS_BUSY:
1519 			/*
1520 			 * Restart the queue after either another
1521 			 * command completes or a 1 second timeout.
1522 			 * If we have any retries left, that is.
1523 			 */
1524 			retry = ccb->ccb_h.retry_count > 0;
1525 			if (retry) {
1526 				ccb->ccb_h.retry_count--;
1527 				error = ERESTART;
1528 				relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT
1529 					     | RELSIM_RELEASE_AFTER_CMDCMPLT;
1530 				timeout = 1000;
1531 			} else {
1532 				error = EIO;
1533 			}
1534 			break;
1535 		case SCSI_STATUS_RESERV_CONFLICT:
1536 			error = EIO;
1537 			break;
1538 		default:
1539 			error = EIO;
1540 			break;
1541 		}
1542 		break;
1543 	case CAM_REQ_CMP_ERR:
1544 	case CAM_CMD_TIMEOUT:
1545 	case CAM_UNEXP_BUSFREE:
1546 	case CAM_UNCOR_PARITY:
1547 	case CAM_DATA_RUN_ERR:
1548 		/* decrement the number of retries */
1549 		retry = ccb->ccb_h.retry_count > 0;
1550 		if (retry) {
1551 			ccb->ccb_h.retry_count--;
1552 			error = ERESTART;
1553 		} else {
1554 			error = EIO;
1555 		}
1556 		break;
1557 	case CAM_UA_ABORT:
1558 	case CAM_UA_TERMIO:
1559 	case CAM_MSG_REJECT_REC:
1560 		/* XXX Don't know that these are correct */
1561 		error = EIO;
1562 		break;
1563 	case CAM_SEL_TIMEOUT:
1564 	{
1565 		/*
1566 		 * XXX
1567 		 * A single selection timeout should not be enough
1568 		 * to invalidate a device.  We should retry for multiple
1569 		 * seconds assuming this isn't a probe.  We'll probably
1570 		 * need a special flag for that.
1571 		 */
1572 #if 0
1573 		struct cam_path *newpath;
1574 
1575 		/* Should we do more if we can't create the path?? */
1576 		if (xpt_create_path(&newpath, xpt_path_periph(ccb->ccb_h.path),
1577 				    xpt_path_path_id(ccb->ccb_h.path),
1578 				    xpt_path_target_id(ccb->ccb_h.path),
1579 				    CAM_LUN_WILDCARD) != CAM_REQ_CMP)
1580 			break;
1581 		/*
1582 		 * Let peripheral drivers know that this device has gone
1583 		 * away.
1584 		 */
1585 		xpt_async(AC_LOST_DEVICE, newpath, NULL);
1586 		xpt_free_path(newpath);
1587 #endif
1588 		if ((sense_flags & SF_RETRY_SELTO) != 0) {
1589 			retry = ccb->ccb_h.retry_count > 0;
1590 			if (retry) {
1591 				ccb->ccb_h.retry_count--;
1592 				error = ERESTART;
1593 				/*
1594 				 * Wait half a second to give the device
1595 				 * time to recover before we try again.
1596 				 */
1597 				relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1598 				timeout = 500;
1599 			} else {
1600 				error = ENXIO;
1601 			}
1602 		} else {
1603 			error = ENXIO;
1604 		}
1605 		break;
1606 	}
1607 	case CAM_REQ_INVALID:
1608 	case CAM_PATH_INVALID:
1609 	case CAM_DEV_NOT_THERE:
1610 	case CAM_NO_HBA:
1611 	case CAM_PROVIDE_FAIL:
1612 	case CAM_REQ_TOO_BIG:
1613 		error = EINVAL;
1614 		break;
1615 	case CAM_SCSI_BUS_RESET:
1616 	case CAM_BDR_SENT:
1617 	case CAM_REQUEUE_REQ:
1618 		/* Unconditional requeue, dammit */
1619 		error = ERESTART;
1620 		break;
1621 	case CAM_RESRC_UNAVAIL:
1622 	case CAM_BUSY:
1623 		/* timeout??? */
1624 	default:
1625 		/* decrement the number of retries */
1626 		retry = ccb->ccb_h.retry_count > 0;
1627 		if (retry) {
1628 			ccb->ccb_h.retry_count--;
1629 			error = ERESTART;
1630 		} else {
1631 			/* Check the sense codes */
1632 			error = EIO;
1633 		}
1634 		break;
1635 	}
1636 
1637 	/* Attempt a retry */
1638 	if (error == ERESTART || error == 0) {
1639 		if (frozen != 0)
1640 			ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1641 
1642 		if (error == ERESTART)
1643 			xpt_action(ccb);
1644 
1645 		if (frozen != 0) {
1646 			cam_release_devq(ccb->ccb_h.path,
1647 					 relsim_flags,
1648 					 openings,
1649 					 timeout,
1650 					 /*getcount_only*/0);
1651 		}
1652 	}
1653 
1654 
1655 	return (error);
1656 }
1657