If we have an error and are booting verbosely, don't be complaining
[dragonfly.git] / sys / bus / cam / cam_periph.c
bloba718087b2e5294d5cb3abbfe7bd9719386297f5b
1 /*
2 * Common functions for CAM "type" (peripheral) drivers.
4 * Copyright (c) 1997, 1998 Justin T. Gibbs.
5 * Copyright (c) 1997, 1998, 1999, 2000 Kenneth D. Merry.
6 * All rights reserved.
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.
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.
29 * $FreeBSD: src/sys/cam/cam_periph.c,v 1.24.2.3 2003/01/25 19:04:40 dillon Exp $
30 * $DragonFly: src/sys/bus/cam/cam_periph.c,v 1.26 2007/11/18 18:49:53 pavalos Exp $
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/types.h>
36 #include <sys/malloc.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>
44 #include <sys/thread2.h>
46 #include "cam.h"
47 #include "cam_ccb.h"
48 #include "cam_xpt_periph.h"
49 #include "cam_periph.h"
50 #include "cam_debug.h"
52 #include <bus/cam/scsi/scsi_all.h>
53 #include <bus/cam/scsi/scsi_message.h>
54 #include <bus/cam/scsi/scsi_pass.h>
56 static u_int camperiphnextunit(struct periph_driver *p_drv,
57 u_int newunit, int wired,
58 path_id_t pathid, target_id_t target,
59 lun_id_t lun);
60 static u_int camperiphunit(struct periph_driver *p_drv,
61 path_id_t pathid, target_id_t target,
62 lun_id_t lun);
63 static void camperiphdone(struct cam_periph *periph,
64 union ccb *done_ccb);
65 static void camperiphfree(struct cam_periph *periph);
66 static int camperiphscsistatuserror(union ccb *ccb,
67 cam_flags camflags,
68 u_int32_t sense_flags,
69 union ccb *save_ccb,
70 int *openings,
71 u_int32_t *relsim_flags,
72 u_int32_t *timeout);
73 static int camperiphscsisenseerror(union ccb *ccb,
74 cam_flags camflags,
75 u_int32_t sense_flags,
76 union ccb *save_ccb,
77 int *openings,
78 u_int32_t *relsim_flags,
79 u_int32_t *timeout);
81 static int nperiph_drivers;
82 struct periph_driver **periph_drivers;
84 void
85 periphdriver_register(void *data)
87 struct periph_driver **newdrivers, **old;
88 int ndrivers;
90 ndrivers = nperiph_drivers + 2;
91 newdrivers = kmalloc(sizeof(*newdrivers) * ndrivers, M_TEMP, M_WAITOK);
92 if (periph_drivers)
93 bcopy(periph_drivers, newdrivers,
94 sizeof(*newdrivers) * nperiph_drivers);
95 newdrivers[nperiph_drivers] = (struct periph_driver *)data;
96 newdrivers[nperiph_drivers + 1] = NULL;
97 old = periph_drivers;
98 periph_drivers = newdrivers;
99 if (old)
100 kfree(old, M_TEMP);
101 nperiph_drivers++;
104 cam_status
105 cam_periph_alloc(periph_ctor_t *periph_ctor,
106 periph_oninv_t *periph_oninvalidate,
107 periph_dtor_t *periph_dtor, periph_start_t *periph_start,
108 char *name, cam_periph_type type, struct cam_path *path,
109 ac_callback_t *ac_callback, ac_code code, void *arg)
111 struct periph_driver **p_drv;
112 struct cam_periph *periph;
113 struct cam_periph *cur_periph;
114 path_id_t path_id;
115 target_id_t target_id;
116 lun_id_t lun_id;
117 cam_status status;
118 u_int init_level;
120 init_level = 0;
122 * Handle Hot-Plug scenarios. If there is already a peripheral
123 * of our type assigned to this path, we are likely waiting for
124 * final close on an old, invalidated, peripheral. If this is
125 * the case, queue up a deferred call to the peripheral's async
126 * handler. If it looks like a mistaken re-alloation, complain.
128 if ((periph = cam_periph_find(path, name)) != NULL) {
130 if ((periph->flags & CAM_PERIPH_INVALID) != 0
131 && (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) == 0) {
132 periph->flags |= CAM_PERIPH_NEW_DEV_FOUND;
133 periph->deferred_callback = ac_callback;
134 periph->deferred_ac = code;
135 return (CAM_REQ_INPROG);
136 } else {
137 kprintf("cam_periph_alloc: attempt to re-allocate "
138 "valid device %s%d rejected\n",
139 periph->periph_name, periph->unit_number);
141 return (CAM_REQ_INVALID);
144 periph = kmalloc(sizeof(*periph), M_DEVBUF, M_INTWAIT | M_ZERO);
146 init_level++;
148 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
149 if (strcmp((*p_drv)->driver_name, name) == 0)
150 break;
153 path_id = xpt_path_path_id(path);
154 target_id = xpt_path_target_id(path);
155 lun_id = xpt_path_lun_id(path);
156 cam_init_pinfo(&periph->pinfo);
157 periph->periph_start = periph_start;
158 periph->periph_dtor = periph_dtor;
159 periph->periph_oninval = periph_oninvalidate;
160 periph->type = type;
161 periph->periph_name = name;
162 periph->unit_number = camperiphunit(*p_drv, path_id, target_id, lun_id);
163 periph->immediate_priority = CAM_PRIORITY_NONE;
164 periph->refcount = 0;
165 SLIST_INIT(&periph->ccb_list);
166 status = xpt_create_path(&path, periph, path_id, target_id, lun_id);
167 if (status != CAM_REQ_CMP)
168 goto failure;
170 periph->path = path;
171 init_level++;
173 status = xpt_add_periph(periph);
175 if (status != CAM_REQ_CMP)
176 goto failure;
178 crit_enter();
179 cur_periph = TAILQ_FIRST(&(*p_drv)->units);
180 while (cur_periph != NULL
181 && cur_periph->unit_number < periph->unit_number)
182 cur_periph = TAILQ_NEXT(cur_periph, unit_links);
184 if (cur_periph != NULL)
185 TAILQ_INSERT_BEFORE(cur_periph, periph, unit_links);
186 else {
187 TAILQ_INSERT_TAIL(&(*p_drv)->units, periph, unit_links);
188 (*p_drv)->generation++;
191 crit_exit();
193 init_level++;
195 status = periph_ctor(periph, arg);
197 if (status == CAM_REQ_CMP)
198 init_level++;
200 failure:
201 switch (init_level) {
202 case 4:
203 /* Initialized successfully */
204 break;
205 case 3:
206 crit_enter();
207 TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
208 crit_exit();
209 xpt_remove_periph(periph);
210 case 2:
211 xpt_free_path(periph->path);
212 case 1:
213 kfree(periph, M_DEVBUF);
214 case 0:
215 /* No cleanup to perform. */
216 break;
217 default:
218 panic("cam_periph_alloc: Unknown init level");
220 return(status);
224 * Find a peripheral structure with the specified path, target, lun,
225 * and (optionally) type. If the name is NULL, this function will return
226 * the first peripheral driver that matches the specified path.
228 struct cam_periph *
229 cam_periph_find(struct cam_path *path, char *name)
231 struct periph_driver **p_drv;
232 struct cam_periph *periph;
234 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
235 if (name != NULL && (strcmp((*p_drv)->driver_name, name) != 0))
236 continue;
238 crit_enter();
239 TAILQ_FOREACH(periph, &(*p_drv)->units, unit_links) {
240 if (xpt_path_comp(periph->path, path) == 0) {
241 crit_exit();
242 return(periph);
245 crit_exit();
246 if (name != NULL)
247 return(NULL);
249 return(NULL);
252 cam_status
253 cam_periph_acquire(struct cam_periph *periph)
255 if (periph == NULL)
256 return(CAM_REQ_CMP_ERR);
258 crit_enter();
259 periph->refcount++;
260 crit_exit();
262 return(CAM_REQ_CMP);
265 void
266 cam_periph_release(struct cam_periph *periph)
268 if (periph == NULL)
269 return;
271 crit_enter();
272 if ((--periph->refcount == 0)
273 && (periph->flags & CAM_PERIPH_INVALID)) {
274 camperiphfree(periph);
276 crit_exit();
280 * Look for the next unit number that is not currently in use for this
281 * peripheral type starting at "newunit". Also exclude unit numbers that
282 * are reserved by for future "hardwiring" unless we already know that this
283 * is a potential wired device. Only assume that the device is "wired" the
284 * first time through the loop since after that we'll be looking at unit
285 * numbers that did not match a wiring entry.
287 static u_int
288 camperiphnextunit(struct periph_driver *p_drv, u_int newunit, int wired,
289 path_id_t pathid, target_id_t target, lun_id_t lun)
291 struct cam_periph *periph;
292 char *periph_name, *strval;
293 int i, val, dunit;
294 const char *dname;
296 crit_enter();
297 periph_name = p_drv->driver_name;
298 for (;;newunit++) {
300 for (periph = TAILQ_FIRST(&p_drv->units);
301 periph != NULL && periph->unit_number != newunit;
302 periph = TAILQ_NEXT(periph, unit_links))
305 if (periph != NULL && periph->unit_number == newunit) {
306 if (wired != 0) {
307 xpt_print_path(periph->path);
308 kprintf("Duplicate Wired Device entry!\n");
309 xpt_print_path(periph->path);
310 kprintf("Second device (%s device at scbus%d "
311 "target %d lun %d) will not be wired\n",
312 periph_name, pathid, target, lun);
313 wired = 0;
315 continue;
317 if (wired)
318 break;
321 * Don't match entries like "da 4" as a wired down
322 * device, but do match entries like "da 4 target 5"
323 * or even "da 4 scbus 1".
325 i = -1;
326 while ((i = resource_locate(i, periph_name)) != -1) {
327 dname = resource_query_name(i);
328 dunit = resource_query_unit(i);
329 /* if no "target" and no specific scbus, skip */
330 if (resource_int_value(dname, dunit, "target", &val) &&
331 (resource_string_value(dname, dunit, "at",&strval)||
332 strcmp(strval, "scbus") == 0))
333 continue;
334 if (newunit == dunit)
335 break;
337 if (i == -1)
338 break;
340 crit_exit();
341 return (newunit);
344 static u_int
345 camperiphunit(struct periph_driver *p_drv, path_id_t pathid,
346 target_id_t target, lun_id_t lun)
348 u_int unit;
349 int hit, i, val, dunit;
350 const char *dname;
351 char pathbuf[32], *strval, *periph_name;
353 unit = 0;
355 periph_name = p_drv->driver_name;
356 ksnprintf(pathbuf, sizeof(pathbuf), "scbus%d", pathid);
357 i = -1;
358 for (hit = 0; (i = resource_locate(i, periph_name)) != -1; hit = 0) {
359 dname = resource_query_name(i);
360 dunit = resource_query_unit(i);
361 if (resource_string_value(dname, dunit, "at", &strval) == 0) {
362 if (strcmp(strval, pathbuf) != 0)
363 continue;
364 hit++;
366 if (resource_int_value(dname, dunit, "target", &val) == 0) {
367 if (val != target)
368 continue;
369 hit++;
371 if (resource_int_value(dname, dunit, "lun", &val) == 0) {
372 if (val != lun)
373 continue;
374 hit++;
376 if (hit != 0) {
377 unit = dunit;
378 break;
383 * Either start from 0 looking for the next unit or from
384 * the unit number given in the resource config. This way,
385 * if we have wildcard matches, we don't return the same
386 * unit number twice.
388 unit = camperiphnextunit(p_drv, unit, /*wired*/hit, pathid,
389 target, lun);
391 return (unit);
394 void
395 cam_periph_invalidate(struct cam_periph *periph)
398 * We only call this routine the first time a peripheral is
399 * invalidated. The oninvalidate() routine is always called in
400 * a critical section.
402 crit_enter();
403 if (((periph->flags & CAM_PERIPH_INVALID) == 0)
404 && (periph->periph_oninval != NULL))
405 periph->periph_oninval(periph);
407 periph->flags |= CAM_PERIPH_INVALID;
408 periph->flags &= ~CAM_PERIPH_NEW_DEV_FOUND;
410 if (periph->refcount == 0)
411 camperiphfree(periph);
412 else if (periph->refcount < 0)
413 kprintf("cam_invalidate_periph: refcount < 0!!\n");
414 crit_exit();
417 static void
418 camperiphfree(struct cam_periph *periph)
420 struct periph_driver **p_drv;
422 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
423 if (strcmp((*p_drv)->driver_name, periph->periph_name) == 0)
424 break;
427 if (*p_drv == NULL) {
428 kprintf("camperiphfree: attempt to free "
429 "non-existent periph: %s\n", periph->periph_name);
430 return;
433 if (periph->periph_dtor != NULL)
434 periph->periph_dtor(periph);
436 crit_enter();
437 TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
438 (*p_drv)->generation++;
439 crit_exit();
441 xpt_remove_periph(periph);
443 if (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) {
444 union ccb ccb;
445 void *arg;
447 switch (periph->deferred_ac) {
448 case AC_FOUND_DEVICE:
449 ccb.ccb_h.func_code = XPT_GDEV_TYPE;
450 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/ 1);
451 xpt_action(&ccb);
452 arg = &ccb;
453 break;
454 case AC_PATH_REGISTERED:
455 ccb.ccb_h.func_code = XPT_PATH_INQ;
456 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/ 1);
457 xpt_action(&ccb);
458 arg = &ccb;
459 break;
460 default:
461 arg = NULL;
462 break;
464 periph->deferred_callback(NULL, periph->deferred_ac,
465 periph->path, arg);
467 xpt_free_path(periph->path);
468 kfree(periph, M_DEVBUF);
472 * Wait interruptibly for an exclusive lock.
475 cam_periph_lock(struct cam_periph *periph, int flags)
477 int error;
480 * Increment the reference count on the peripheral
481 * while we wait for our lock attempt to succeed
482 * to ensure the peripheral doesn't dissappear
483 * out from under us while we sleep.
485 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
486 return(ENXIO);
488 while ((periph->flags & CAM_PERIPH_LOCKED) != 0) {
489 periph->flags |= CAM_PERIPH_LOCK_WANTED;
490 if ((error = tsleep(periph, flags, "caplck", 0)) != 0) {
491 cam_periph_release(periph);
492 return error;
496 periph->flags |= CAM_PERIPH_LOCKED;
497 return 0;
501 * Unlock and wake up any waiters.
503 void
504 cam_periph_unlock(struct cam_periph *periph)
506 periph->flags &= ~CAM_PERIPH_LOCKED;
507 if ((periph->flags & CAM_PERIPH_LOCK_WANTED) != 0) {
508 periph->flags &= ~CAM_PERIPH_LOCK_WANTED;
509 wakeup(periph);
512 cam_periph_release(periph);
516 * Map user virtual pointers into kernel virtual address space, so we can
517 * access the memory. This won't work on physical pointers, for now it's
518 * up to the caller to check for that. (XXX KDM -- should we do that here
519 * instead?) This also only works for up to MAXPHYS memory. Since we use
520 * buffers to map stuff in and out, we're limited to the buffer size.
523 cam_periph_mapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
525 int numbufs, i, j;
526 buf_cmd_t cmd[CAM_PERIPH_MAXMAPS];
527 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
528 u_int32_t lengths[CAM_PERIPH_MAXMAPS];
529 u_int32_t dirs[CAM_PERIPH_MAXMAPS];
531 switch(ccb->ccb_h.func_code) {
532 case XPT_DEV_MATCH:
533 if (ccb->cdm.match_buf_len == 0) {
534 kprintf("cam_periph_mapmem: invalid match buffer "
535 "length 0\n");
536 return(EINVAL);
538 if (ccb->cdm.pattern_buf_len > 0) {
539 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
540 lengths[0] = ccb->cdm.pattern_buf_len;
541 dirs[0] = CAM_DIR_OUT;
542 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
543 lengths[1] = ccb->cdm.match_buf_len;
544 dirs[1] = CAM_DIR_IN;
545 numbufs = 2;
546 } else {
547 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
548 lengths[0] = ccb->cdm.match_buf_len;
549 dirs[0] = CAM_DIR_IN;
550 numbufs = 1;
552 break;
553 case XPT_SCSI_IO:
554 case XPT_CONT_TARGET_IO:
555 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
556 return(0);
558 data_ptrs[0] = &ccb->csio.data_ptr;
559 lengths[0] = ccb->csio.dxfer_len;
560 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
561 numbufs = 1;
562 break;
563 default:
564 return(EINVAL);
565 break; /* NOTREACHED */
569 * Check the transfer length and permissions first, so we don't
570 * have to unmap any previously mapped buffers.
572 for (i = 0; i < numbufs; i++) {
574 * Its kinda bogus, we need a R+W command. For now the
575 * buffer needs some sort of command. Use BUF_CMD_WRITE
576 * to indicate a write and BUF_CMD_READ to indicate R+W.
578 cmd[i] = BUF_CMD_WRITE;
581 * The userland data pointer passed in may not be page
582 * aligned. vmapbuf() truncates the address to a page
583 * boundary, so if the address isn't page aligned, we'll
584 * need enough space for the given transfer length, plus
585 * whatever extra space is necessary to make it to the page
586 * boundary.
588 if ((lengths[i] +
589 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)) > DFLTPHYS){
590 kprintf("cam_periph_mapmem: attempt to map %lu bytes, "
591 "which is greater than DFLTPHYS(%d)\n",
592 (long)(lengths[i] +
593 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)),
594 DFLTPHYS);
595 return(E2BIG);
598 if (dirs[i] & CAM_DIR_OUT) {
599 if (!useracc(*data_ptrs[i], lengths[i],
600 VM_PROT_READ)) {
601 kprintf("cam_periph_mapmem: error, "
602 "address %p, length %lu isn't "
603 "user accessible for READ\n",
604 (void *)*data_ptrs[i],
605 (u_long)lengths[i]);
606 return(EACCES);
610 if (dirs[i] & CAM_DIR_IN) {
611 cmd[i] = BUF_CMD_READ;
612 if (!useracc(*data_ptrs[i], lengths[i],
613 VM_PROT_WRITE)) {
614 kprintf("cam_periph_mapmem: error, "
615 "address %p, length %lu isn't "
616 "user accessible for WRITE\n",
617 (void *)*data_ptrs[i],
618 (u_long)lengths[i]);
620 return(EACCES);
626 for (i = 0; i < numbufs; i++) {
628 * Get the buffer.
630 mapinfo->bp[i] = getpbuf(NULL);
632 /* save the original user pointer */
633 mapinfo->saved_ptrs[i] = *data_ptrs[i];
635 /* set the flags */
636 mapinfo->bp[i]->b_cmd = cmd[i];
638 /* map the user buffer into kernel memory */
639 if (vmapbuf(mapinfo->bp[i], *data_ptrs[i], lengths[i]) < 0) {
640 kprintf("cam_periph_mapmem: error, "
641 "address %p, length %lu isn't "
642 "user accessible any more\n",
643 (void *)*data_ptrs[i],
644 (u_long)lengths[i]);
645 for (j = 0; j < i; ++j) {
646 *data_ptrs[j] = mapinfo->saved_ptrs[j];
647 vunmapbuf(mapinfo->bp[j]);
648 relpbuf(mapinfo->bp[j], NULL);
650 mapinfo->num_bufs_used -= i;
651 return(EACCES);
654 /* set our pointer to the new mapped area */
655 *data_ptrs[i] = mapinfo->bp[i]->b_data;
657 mapinfo->num_bufs_used++;
660 return(0);
664 * Unmap memory segments mapped into kernel virtual address space by
665 * cam_periph_mapmem().
667 void
668 cam_periph_unmapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
670 int numbufs, i;
671 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
673 if (mapinfo->num_bufs_used <= 0) {
674 /* allow ourselves to be swapped once again */
675 return;
678 switch (ccb->ccb_h.func_code) {
679 case XPT_DEV_MATCH:
680 numbufs = min(mapinfo->num_bufs_used, 2);
682 if (numbufs == 1) {
683 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
684 } else {
685 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
686 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
688 break;
689 case XPT_SCSI_IO:
690 case XPT_CONT_TARGET_IO:
691 data_ptrs[0] = &ccb->csio.data_ptr;
692 numbufs = min(mapinfo->num_bufs_used, 1);
693 break;
694 default:
695 /* allow ourselves to be swapped once again */
696 return;
697 break; /* NOTREACHED */
700 for (i = 0; i < numbufs; i++) {
701 /* Set the user's pointer back to the original value */
702 *data_ptrs[i] = mapinfo->saved_ptrs[i];
704 /* unmap the buffer */
705 vunmapbuf(mapinfo->bp[i]);
707 /* release the buffer */
708 relpbuf(mapinfo->bp[i], NULL);
711 /* allow ourselves to be swapped once again */
714 union ccb *
715 cam_periph_getccb(struct cam_periph *periph, u_int32_t priority)
717 struct ccb_hdr *ccb_h;
719 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdgetccb\n"));
721 crit_enter();
723 while (SLIST_FIRST(&periph->ccb_list) == NULL) {
724 if (periph->immediate_priority > priority)
725 periph->immediate_priority = priority;
726 xpt_schedule(periph, priority);
727 if ((SLIST_FIRST(&periph->ccb_list) != NULL)
728 && (SLIST_FIRST(&periph->ccb_list)->pinfo.priority == priority))
729 break;
730 tsleep(&periph->ccb_list, 0, "cgticb", 0);
733 ccb_h = SLIST_FIRST(&periph->ccb_list);
734 SLIST_REMOVE_HEAD(&periph->ccb_list, periph_links.sle);
735 crit_exit();
736 return ((union ccb *)ccb_h);
739 void
740 cam_periph_ccbwait(union ccb *ccb)
742 crit_enter();
743 if ((ccb->ccb_h.pinfo.index != CAM_UNQUEUED_INDEX)
744 || ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG))
745 tsleep(&ccb->ccb_h.cbfcnp, 0, "cbwait", 0);
746 crit_exit();
750 cam_periph_ioctl(struct cam_periph *periph, int cmd, caddr_t addr,
751 int (*error_routine)(union ccb *ccb,
752 cam_flags camflags,
753 u_int32_t sense_flags))
755 union ccb *ccb;
756 int error;
757 int found;
759 error = found = 0;
761 switch(cmd){
762 case CAMGETPASSTHRU:
763 ccb = cam_periph_getccb(periph, /* priority */ 1);
764 xpt_setup_ccb(&ccb->ccb_h,
765 ccb->ccb_h.path,
766 /*priority*/1);
767 ccb->ccb_h.func_code = XPT_GDEVLIST;
770 * Basically, the point of this is that we go through
771 * getting the list of devices, until we find a passthrough
772 * device. In the current version of the CAM code, the
773 * only way to determine what type of device we're dealing
774 * with is by its name.
776 while (found == 0) {
777 ccb->cgdl.index = 0;
778 ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS;
779 while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) {
781 /* we want the next device in the list */
782 xpt_action(ccb);
783 if (strncmp(ccb->cgdl.periph_name,
784 "pass", 4) == 0){
785 found = 1;
786 break;
789 if ((ccb->cgdl.status == CAM_GDEVLIST_LAST_DEVICE) &&
790 (found == 0)) {
791 ccb->cgdl.periph_name[0] = '\0';
792 ccb->cgdl.unit_number = 0;
793 break;
797 /* copy the result back out */
798 bcopy(ccb, addr, sizeof(union ccb));
800 /* and release the ccb */
801 xpt_release_ccb(ccb);
803 break;
804 default:
805 error = ENOTTY;
806 break;
808 return(error);
812 cam_periph_runccb(union ccb *ccb,
813 int (*error_routine)(union ccb *ccb,
814 cam_flags camflags,
815 u_int32_t sense_flags),
816 cam_flags camflags, u_int32_t sense_flags,
817 struct devstat *ds)
819 int error;
821 error = 0;
824 * If the user has supplied a stats structure, and if we understand
825 * this particular type of ccb, record the transaction start.
827 if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO))
828 devstat_start_transaction(ds);
830 xpt_action(ccb);
832 do {
833 cam_periph_ccbwait(ccb);
834 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
835 error = 0;
836 else if (error_routine != NULL)
837 error = (*error_routine)(ccb, camflags, sense_flags);
838 else
839 error = 0;
841 } while (error == ERESTART);
843 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
844 cam_release_devq(ccb->ccb_h.path,
845 /* relsim_flags */0,
846 /* openings */0,
847 /* timeout */0,
848 /* getcount_only */ FALSE);
850 if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO))
851 devstat_end_transaction(ds,
852 ccb->csio.dxfer_len,
853 ccb->csio.tag_action & 0xf,
854 ((ccb->ccb_h.flags & CAM_DIR_MASK) ==
855 CAM_DIR_NONE) ? DEVSTAT_NO_DATA :
856 (ccb->ccb_h.flags & CAM_DIR_OUT) ?
857 DEVSTAT_WRITE :
858 DEVSTAT_READ);
860 return(error);
863 void
864 cam_freeze_devq(struct cam_path *path)
866 struct ccb_hdr ccb_h;
868 xpt_setup_ccb(&ccb_h, path, /*priority*/1);
869 ccb_h.func_code = XPT_NOOP;
870 ccb_h.flags = CAM_DEV_QFREEZE;
871 xpt_action((union ccb *)&ccb_h);
874 u_int32_t
875 cam_release_devq(struct cam_path *path, u_int32_t relsim_flags,
876 u_int32_t openings, u_int32_t timeout,
877 int getcount_only)
879 struct ccb_relsim crs;
881 xpt_setup_ccb(&crs.ccb_h, path,
882 /*priority*/1);
883 crs.ccb_h.func_code = XPT_REL_SIMQ;
884 crs.ccb_h.flags = getcount_only ? CAM_DEV_QFREEZE : 0;
885 crs.release_flags = relsim_flags;
886 crs.openings = openings;
887 crs.release_timeout = timeout;
888 xpt_action((union ccb *)&crs);
889 return (crs.qfrozen_cnt);
892 #define saved_ccb_ptr ppriv_ptr0
893 static void
894 camperiphdone(struct cam_periph *periph, union ccb *done_ccb)
896 union ccb *saved_ccb;
897 cam_status status;
898 int frozen;
899 int sense;
900 struct scsi_start_stop_unit *scsi_cmd;
901 u_int32_t relsim_flags, timeout;
902 u_int32_t qfrozen_cnt;
903 int xpt_done_ccb;
905 xpt_done_ccb = FALSE;
906 status = done_ccb->ccb_h.status;
907 frozen = (status & CAM_DEV_QFRZN) != 0;
908 sense = (status & CAM_AUTOSNS_VALID) != 0;
909 status &= CAM_STATUS_MASK;
911 timeout = 0;
912 relsim_flags = 0;
913 saved_ccb = (union ccb *)done_ccb->ccb_h.saved_ccb_ptr;
916 * Unfreeze the queue once if it is already frozen..
918 if (frozen != 0) {
919 qfrozen_cnt = cam_release_devq(done_ccb->ccb_h.path,
920 /*relsim_flags*/0,
921 /*openings*/0,
922 /*timeout*/0,
923 /*getcount_only*/0);
926 switch (status) {
927 case CAM_REQ_CMP:
930 * If we have successfully taken a device from the not
931 * ready to ready state, re-scan the device and re-get
932 * the inquiry information. Many devices (mostly disks)
933 * don't properly report their inquiry information unless
934 * they are spun up.
936 * If we manually retrieved sense into a CCB and got
937 * something other than "NO SENSE" send the updated CCB
938 * back to the client via xpt_done() to be processed via
939 * the error recovery code again.
941 if (done_ccb->ccb_h.func_code == XPT_SCSI_IO) {
942 scsi_cmd = (struct scsi_start_stop_unit *)
943 &done_ccb->csio.cdb_io.cdb_bytes;
945 if (scsi_cmd->opcode == START_STOP_UNIT)
946 xpt_async(AC_INQ_CHANGED,
947 done_ccb->ccb_h.path, NULL);
948 if (scsi_cmd->opcode == REQUEST_SENSE) {
949 u_int sense_key;
951 sense_key = saved_ccb->csio.sense_data.flags;
952 sense_key &= SSD_KEY;
953 if (sense_key != SSD_KEY_NO_SENSE) {
954 saved_ccb->ccb_h.flags |=
955 CAM_AUTOSNS_VALID;
956 xpt_print_path(saved_ccb->ccb_h.path);
957 kprintf("Recovered Sense\n");
958 #if 0
959 scsi_sense_print(&saved_ccb->csio);
960 #endif
961 cam_error_print(saved_ccb, CAM_ESF_ALL,
962 CAM_EPF_ALL);
963 xpt_done_ccb = TRUE;
967 bcopy(done_ccb->ccb_h.saved_ccb_ptr, done_ccb,
968 sizeof(union ccb));
970 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
972 if (xpt_done_ccb == FALSE)
973 xpt_action(done_ccb);
975 break;
977 case CAM_SCSI_STATUS_ERROR:
978 scsi_cmd = (struct scsi_start_stop_unit *)
979 &done_ccb->csio.cdb_io.cdb_bytes;
980 if (sense != 0) {
981 struct scsi_sense_data *sense;
982 int error_code, sense_key, asc, ascq;
984 sense = &done_ccb->csio.sense_data;
985 scsi_extract_sense(sense, &error_code,
986 &sense_key, &asc, &ascq);
989 * If the error is "invalid field in CDB",
990 * and the load/eject flag is set, turn the
991 * flag off and try again. This is just in
992 * case the drive in question barfs on the
993 * load eject flag. The CAM code should set
994 * the load/eject flag by default for
995 * removable media.
998 /* XXX KDM
999 * Should we check to see what the specific
1000 * scsi status is?? Or does it not matter
1001 * since we already know that there was an
1002 * error, and we know what the specific
1003 * error code was, and we know what the
1004 * opcode is..
1006 if ((scsi_cmd->opcode == START_STOP_UNIT) &&
1007 ((scsi_cmd->how & SSS_LOEJ) != 0) &&
1008 (asc == 0x24) && (ascq == 0x00) &&
1009 (done_ccb->ccb_h.retry_count > 0)) {
1011 scsi_cmd->how &= ~SSS_LOEJ;
1013 xpt_action(done_ccb);
1015 } else if (done_ccb->ccb_h.retry_count > 1) {
1017 * In this case, the error recovery
1018 * command failed, but we've got
1019 * some retries left on it. Give
1020 * it another try.
1023 /* set the timeout to .5 sec */
1024 relsim_flags =
1025 RELSIM_RELEASE_AFTER_TIMEOUT;
1026 timeout = 500;
1028 xpt_action(done_ccb);
1030 break;
1032 } else {
1034 * Perform the final retry with the original
1035 * CCB so that final error processing is
1036 * performed by the owner of the CCB.
1038 bcopy(done_ccb->ccb_h.saved_ccb_ptr,
1039 done_ccb, sizeof(union ccb));
1041 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1043 xpt_action(done_ccb);
1045 } else {
1047 * Eh?? The command failed, but we don't
1048 * have any sense. What's up with that?
1049 * Fire the CCB again to return it to the
1050 * caller.
1052 bcopy(done_ccb->ccb_h.saved_ccb_ptr,
1053 done_ccb, sizeof(union ccb));
1055 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1057 xpt_action(done_ccb);
1060 break;
1061 default:
1062 bcopy(done_ccb->ccb_h.saved_ccb_ptr, done_ccb,
1063 sizeof(union ccb));
1065 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1067 xpt_action(done_ccb);
1069 break;
1072 /* decrement the retry count */
1074 * XXX This isn't appropriate in all cases. Restructure,
1075 * so that the retry count is only decremented on an
1076 * actual retry. Remeber that the orignal ccb had its
1077 * retry count dropped before entering recovery, so
1078 * doing it again is a bug.
1080 if (done_ccb->ccb_h.retry_count > 0)
1081 done_ccb->ccb_h.retry_count--;
1083 qfrozen_cnt = cam_release_devq(done_ccb->ccb_h.path,
1084 /*relsim_flags*/relsim_flags,
1085 /*openings*/0,
1086 /*timeout*/timeout,
1087 /*getcount_only*/0);
1088 if (xpt_done_ccb == TRUE)
1089 (*done_ccb->ccb_h.cbfcnp)(periph, done_ccb);
1093 * Generic Async Event handler. Peripheral drivers usually
1094 * filter out the events that require personal attention,
1095 * and leave the rest to this function.
1097 void
1098 cam_periph_async(struct cam_periph *periph, u_int32_t code,
1099 struct cam_path *path, void *arg)
1101 switch (code) {
1102 case AC_LOST_DEVICE:
1103 cam_periph_invalidate(periph);
1104 break;
1105 case AC_SENT_BDR:
1106 case AC_BUS_RESET:
1108 cam_periph_bus_settle(periph, SCSI_DELAY);
1109 break;
1111 default:
1112 break;
1116 void
1117 cam_periph_bus_settle(struct cam_periph *periph, u_int bus_settle)
1119 struct ccb_getdevstats cgds;
1121 xpt_setup_ccb(&cgds.ccb_h, periph->path, /*priority*/1);
1122 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1123 xpt_action((union ccb *)&cgds);
1124 cam_periph_freeze_after_event(periph, &cgds.last_reset, bus_settle);
1127 void
1128 cam_periph_freeze_after_event(struct cam_periph *periph,
1129 struct timeval* event_time, u_int duration_ms)
1131 struct timeval delta;
1132 struct timeval duration_tv;
1134 microuptime(&delta);
1135 timevalsub(&delta, event_time);
1136 duration_tv.tv_sec = duration_ms / 1000;
1137 duration_tv.tv_usec = (duration_ms % 1000) * 1000;
1138 if (timevalcmp(&delta, &duration_tv, <)) {
1139 timevalsub(&duration_tv, &delta);
1141 duration_ms = duration_tv.tv_sec * 1000;
1142 duration_ms += duration_tv.tv_usec / 1000;
1143 cam_freeze_devq(periph->path);
1144 cam_release_devq(periph->path,
1145 RELSIM_RELEASE_AFTER_TIMEOUT,
1146 /*reduction*/0,
1147 /*timeout*/duration_ms,
1148 /*getcount_only*/0);
1153 static int
1154 camperiphscsistatuserror(union ccb *ccb, cam_flags camflags,
1155 u_int32_t sense_flags, union ccb *save_ccb,
1156 int *openings, u_int32_t *relsim_flags,
1157 u_int32_t *timeout)
1159 int error;
1161 switch (ccb->csio.scsi_status) {
1162 case SCSI_STATUS_OK:
1163 case SCSI_STATUS_COND_MET:
1164 case SCSI_STATUS_INTERMED:
1165 case SCSI_STATUS_INTERMED_COND_MET:
1166 error = 0;
1167 break;
1168 case SCSI_STATUS_CMD_TERMINATED:
1169 case SCSI_STATUS_CHECK_COND:
1170 error = camperiphscsisenseerror(ccb,
1171 camflags,
1172 sense_flags,
1173 save_ccb,
1174 openings,
1175 relsim_flags,
1176 timeout);
1177 break;
1178 case SCSI_STATUS_QUEUE_FULL:
1180 /* no decrement */
1181 struct ccb_getdevstats cgds;
1184 * First off, find out what the current
1185 * transaction counts are.
1187 xpt_setup_ccb(&cgds.ccb_h,
1188 ccb->ccb_h.path,
1189 /*priority*/1);
1190 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1191 xpt_action((union ccb *)&cgds);
1194 * If we were the only transaction active, treat
1195 * the QUEUE FULL as if it were a BUSY condition.
1197 if (cgds.dev_active != 0) {
1198 int total_openings;
1201 * Reduce the number of openings to
1202 * be 1 less than the amount it took
1203 * to get a queue full bounded by the
1204 * minimum allowed tag count for this
1205 * device.
1207 total_openings = cgds.dev_active + cgds.dev_openings;
1208 *openings = cgds.dev_active;
1209 if (*openings < cgds.mintags)
1210 *openings = cgds.mintags;
1211 if (*openings < total_openings)
1212 *relsim_flags = RELSIM_ADJUST_OPENINGS;
1213 else {
1215 * Some devices report queue full for
1216 * temporary resource shortages. For
1217 * this reason, we allow a minimum
1218 * tag count to be entered via a
1219 * quirk entry to prevent the queue
1220 * count on these devices from falling
1221 * to a pessimisticly low value. We
1222 * still wait for the next successful
1223 * completion, however, before queueing
1224 * more transactions to the device.
1226 *relsim_flags = RELSIM_RELEASE_AFTER_CMDCMPLT;
1228 *timeout = 0;
1229 error = ERESTART;
1230 break;
1232 /* FALLTHROUGH */
1234 case SCSI_STATUS_BUSY:
1236 * Restart the queue after either another
1237 * command completes or a 1 second timeout.
1239 if (ccb->ccb_h.retry_count > 0) {
1240 ccb->ccb_h.retry_count--;
1241 error = ERESTART;
1242 *relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT
1243 | RELSIM_RELEASE_AFTER_CMDCMPLT;
1244 *timeout = 1000;
1245 } else {
1246 error = EIO;
1248 break;
1249 case SCSI_STATUS_RESERV_CONFLICT:
1250 error = EIO;
1251 break;
1252 default:
1253 error = EIO;
1254 break;
1256 return (error);
1259 static int
1260 camperiphscsisenseerror(union ccb *ccb, cam_flags camflags,
1261 u_int32_t sense_flags, union ccb *save_ccb,
1262 int *openings, u_int32_t *relsim_flags,
1263 u_int32_t *timeout)
1265 struct cam_periph *periph;
1266 int error;
1268 periph = xpt_path_periph(ccb->ccb_h.path);
1269 if (periph->flags & CAM_PERIPH_RECOVERY_INPROG) {
1272 * If error recovery is already in progress, don't attempt
1273 * to process this error, but requeue it unconditionally
1274 * and attempt to process it once error recovery has
1275 * completed. This failed command is probably related to
1276 * the error that caused the currently active error recovery
1277 * action so our current recovery efforts should also
1278 * address this command. Be aware that the error recovery
1279 * code assumes that only one recovery action is in progress
1280 * on a particular peripheral instance at any given time
1281 * (e.g. only one saved CCB for error recovery) so it is
1282 * imperitive that we don't violate this assumption.
1284 error = ERESTART;
1285 } else {
1286 scsi_sense_action err_action;
1287 struct ccb_getdev cgd;
1288 const char *action_string;
1289 union ccb* print_ccb;
1291 /* A description of the error recovery action performed */
1292 action_string = NULL;
1295 * The location of the orignal ccb
1296 * for sense printing purposes.
1298 print_ccb = ccb;
1301 * Grab the inquiry data for this device.
1303 xpt_setup_ccb(&cgd.ccb_h, ccb->ccb_h.path, /*priority*/ 1);
1304 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1305 xpt_action((union ccb *)&cgd);
1307 if ((ccb->ccb_h.status & CAM_AUTOSNS_VALID) != 0)
1308 err_action = scsi_error_action(&ccb->csio,
1309 &cgd.inq_data,
1310 sense_flags);
1311 else if ((ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)
1312 err_action = SS_REQSENSE;
1313 else
1314 err_action = SS_RETRY|SSQ_DECREMENT_COUNT|EIO;
1316 error = err_action & SS_ERRMASK;
1319 * If the recovery action will consume a retry,
1320 * make sure we actually have retries available.
1322 if ((err_action & SSQ_DECREMENT_COUNT) != 0) {
1323 if (ccb->ccb_h.retry_count > 0)
1324 ccb->ccb_h.retry_count--;
1325 else {
1326 action_string = "Retries Exhausted";
1327 goto sense_error_done;
1331 if ((err_action & SS_MASK) >= SS_START) {
1333 * Do common portions of commands that
1334 * use recovery CCBs.
1336 if (save_ccb == NULL) {
1337 action_string = "No recovery CCB supplied";
1338 goto sense_error_done;
1340 bcopy(ccb, save_ccb, sizeof(*save_ccb));
1341 print_ccb = save_ccb;
1342 periph->flags |= CAM_PERIPH_RECOVERY_INPROG;
1345 switch (err_action & SS_MASK) {
1346 case SS_NOP:
1347 case SS_RETRY:
1348 action_string = "Retrying Command";
1349 error = ERESTART;
1350 break;
1351 case SS_FAIL:
1352 action_string = "Unretryable error";
1353 break;
1354 case SS_START:
1356 int le;
1359 * Send a start unit command to the device, and
1360 * then retry the command.
1362 action_string = "Attempting to Start Unit";
1365 * Check for removable media and set
1366 * load/eject flag appropriately.
1368 if (SID_IS_REMOVABLE(&cgd.inq_data))
1369 le = TRUE;
1370 else
1371 le = FALSE;
1373 scsi_start_stop(&ccb->csio,
1374 /*retries*/1,
1375 camperiphdone,
1376 MSG_SIMPLE_Q_TAG,
1377 /*start*/TRUE,
1378 /*load/eject*/le,
1379 /*immediate*/FALSE,
1380 SSD_FULL_SIZE,
1381 /*timeout*/50000);
1382 break;
1384 case SS_TUR:
1387 * Send a Test Unit Ready to the device.
1388 * If the 'many' flag is set, we send 120
1389 * test unit ready commands, one every half
1390 * second. Otherwise, we just send one TUR.
1391 * We only want to do this if the retry
1392 * count has not been exhausted.
1394 int retries;
1396 if ((err_action & SSQ_MANY) != 0) {
1397 action_string = "Polling device for readiness";
1398 retries = 120;
1399 } else {
1400 action_string = "Testing device for readiness";
1401 retries = 1;
1403 scsi_test_unit_ready(&ccb->csio,
1404 retries,
1405 camperiphdone,
1406 MSG_SIMPLE_Q_TAG,
1407 SSD_FULL_SIZE,
1408 /*timeout*/5000);
1411 * Accomplish our 500ms delay by deferring
1412 * the release of our device queue appropriately.
1414 *relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1415 *timeout = 500;
1416 break;
1418 case SS_REQSENSE:
1421 * Send a Request Sense to the device. We
1422 * assume that we are in a contingent allegiance
1423 * condition so we do not tag this request.
1425 scsi_request_sense(&ccb->csio, /*retries*/1,
1426 camperiphdone,
1427 &save_ccb->csio.sense_data,
1428 sizeof(save_ccb->csio.sense_data),
1429 CAM_TAG_ACTION_NONE,
1430 /*sense_len*/SSD_FULL_SIZE,
1431 /*timeout*/5000);
1432 break;
1434 default:
1435 panic("Unhandled error action %x\n", err_action);
1438 if ((err_action & SS_MASK) >= SS_START) {
1440 * Drop the priority to 0 so that the recovery
1441 * CCB is the first to execute. Freeze the queue
1442 * after this command is sent so that we can
1443 * restore the old csio and have it queued in
1444 * the proper order before we release normal
1445 * transactions to the device.
1447 ccb->ccb_h.pinfo.priority = 0;
1448 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1449 ccb->ccb_h.saved_ccb_ptr = save_ccb;
1450 error = ERESTART;
1453 sense_error_done:
1454 if ((err_action & SSQ_PRINT_SENSE) != 0
1455 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID) != 0) {
1456 #if 0
1457 scsi_sense_print(&print_ccb->csio);
1458 #endif
1459 cam_error_print(print_ccb, CAM_ESF_ALL, CAM_EPF_ALL);
1460 xpt_print_path(ccb->ccb_h.path);
1461 kprintf("%s\n", action_string);
1464 return (error);
1468 * Generic error handler. Peripheral drivers usually filter
1469 * out the errors that they handle in a unique mannor, then
1470 * call this function.
1473 cam_periph_error(union ccb *ccb, cam_flags camflags,
1474 u_int32_t sense_flags, union ccb *save_ccb)
1476 const char *action_string;
1477 cam_status status;
1478 int frozen;
1479 int error;
1480 int openings;
1481 u_int32_t relsim_flags;
1482 u_int32_t timeout;
1484 action_string = NULL;
1485 status = ccb->ccb_h.status;
1486 frozen = (status & CAM_DEV_QFRZN) != 0;
1487 status &= CAM_STATUS_MASK;
1488 relsim_flags = 0;
1490 switch (status) {
1491 case CAM_REQ_CMP:
1492 error = 0;
1493 break;
1494 case CAM_SCSI_STATUS_ERROR:
1495 error = camperiphscsistatuserror(ccb,
1496 camflags,
1497 sense_flags,
1498 save_ccb,
1499 &openings,
1500 &relsim_flags,
1501 &timeout);
1502 break;
1503 case CAM_AUTOSENSE_FAIL:
1504 xpt_print_path(ccb->ccb_h.path);
1505 kprintf("AutoSense Failed\n");
1506 case CAM_REQ_CMP_ERR:
1507 case CAM_CMD_TIMEOUT:
1508 case CAM_UNEXP_BUSFREE:
1509 case CAM_UNCOR_PARITY:
1510 case CAM_DATA_RUN_ERR:
1511 /* decrement the number of retries */
1512 if (ccb->ccb_h.retry_count > 0) {
1513 ccb->ccb_h.retry_count--;
1514 error = ERESTART;
1515 } else {
1516 action_string = "Retries Exausted";
1517 error = EIO;
1519 break;
1520 case CAM_UA_ABORT:
1521 case CAM_UA_TERMIO:
1522 case CAM_MSG_REJECT_REC:
1523 /* XXX Don't know that these are correct */
1524 error = EIO;
1525 break;
1526 case CAM_SEL_TIMEOUT:
1528 struct cam_path *newpath;
1530 if ((camflags & CAM_RETRY_SELTO) != 0) {
1531 if (ccb->ccb_h.retry_count > 0) {
1533 ccb->ccb_h.retry_count--;
1534 error = ERESTART;
1537 * Wait a second to give the device
1538 * time to recover before we try again.
1540 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1541 timeout = 1000;
1542 break;
1545 error = ENXIO;
1546 /* Should we do more if we can't create the path?? */
1547 if (xpt_create_path(&newpath, xpt_path_periph(ccb->ccb_h.path),
1548 xpt_path_path_id(ccb->ccb_h.path),
1549 xpt_path_target_id(ccb->ccb_h.path),
1550 CAM_LUN_WILDCARD) != CAM_REQ_CMP)
1551 break;
1554 * Let peripheral drivers know that this device has gone
1555 * away.
1557 xpt_async(AC_LOST_DEVICE, newpath, NULL);
1558 xpt_free_path(newpath);
1559 break;
1561 case CAM_REQ_INVALID:
1562 case CAM_PATH_INVALID:
1563 case CAM_DEV_NOT_THERE:
1564 case CAM_NO_HBA:
1565 case CAM_PROVIDE_FAIL:
1566 case CAM_REQ_TOO_BIG:
1567 error = EINVAL;
1568 break;
1569 case CAM_SCSI_BUS_RESET:
1570 case CAM_BDR_SENT:
1572 * Commands that repeatedly timeout and cause these
1573 * kinds of error recovery actions, should return
1574 * CAM_CMD_TIMEOUT, which allows us to safely assume
1575 * that this command was an innocent bystander to
1576 * these events and should be unconditionally
1577 * retried.
1579 /* FALLTHROUGH */
1580 case CAM_REQUEUE_REQ:
1581 /* Unconditional requeue */
1582 error = ERESTART;
1583 break;
1584 case CAM_RESRC_UNAVAIL:
1585 case CAM_BUSY:
1586 /* timeout??? */
1587 default:
1588 /* decrement the number of retries */
1589 if (ccb->ccb_h.retry_count > 0) {
1590 ccb->ccb_h.retry_count--;
1591 error = ERESTART;
1592 } else {
1593 error = EIO;
1594 action_string = "Retries Exhausted";
1596 break;
1599 /* Attempt a retry */
1600 if (error == ERESTART || error == 0) {
1601 if (frozen != 0)
1602 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1604 if (error == ERESTART) {
1605 action_string = "Retrying Command";
1606 xpt_action(ccb);
1609 if (frozen != 0)
1610 cam_release_devq(ccb->ccb_h.path,
1611 relsim_flags,
1612 openings,
1613 timeout,
1614 /*getcount_only*/0);
1618 * If we have an error and are booting verbosely, whine
1619 * *unless* this was a non-retryable selection timeout.
1621 if (error != 0 && bootverbose &&
1622 !(status == CAM_SEL_TIMEOUT && (camflags & CAM_RETRY_SELTO) == 0)) {
1625 if (action_string == NULL)
1626 action_string = "Unretryable Error";
1627 if (error != ERESTART) {
1628 xpt_print_path(ccb->ccb_h.path);
1629 kprintf("error %d\n", error);
1631 xpt_print_path(ccb->ccb_h.path);
1632 kprintf("%s\n", action_string);
1635 return (error);