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.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/types.h>
36 #include <sys/malloc.h>
37 #include <sys/kernel.h>
40 #include <sys/mutex.h>
43 #include <sys/devicestat.h>
47 #include <vm/vm_extern.h>
50 #include <cam/cam_ccb.h>
51 #include <cam/cam_queue.h>
52 #include <cam/cam_xpt_periph.h>
53 #include <cam/cam_periph.h>
54 #include <cam/cam_debug.h>
55 #include <cam/cam_sim.h>
57 #include <cam/scsi/scsi_all.h>
58 #include <cam/scsi/scsi_message.h>
59 #include <cam/scsi/scsi_pass.h>
61 static u_int camperiphnextunit(struct periph_driver *p_drv,
62 u_int newunit, int wired,
63 path_id_t pathid, target_id_t target,
65 static u_int camperiphunit(struct periph_driver *p_drv,
66 path_id_t pathid, target_id_t target,
68 static void camperiphdone(struct cam_periph *periph,
70 static void camperiphfree(struct cam_periph *periph);
71 static int camperiphscsistatuserror(union ccb *ccb,
73 u_int32_t sense_flags,
75 u_int32_t *relsim_flags,
77 const char **action_string);
78 static int camperiphscsisenseerror(union ccb *ccb,
80 u_int32_t sense_flags,
82 u_int32_t *relsim_flags,
84 const char **action_string);
86 static int nperiph_drivers;
87 static int initialized = 0;
88 struct periph_driver **periph_drivers;
90 MALLOC_DEFINE(M_CAMPERIPH, "CAM periph", "CAM peripheral buffers");
92 static int periph_selto_delay = 1000;
93 TUNABLE_INT("kern.cam.periph_selto_delay", &periph_selto_delay);
94 static int periph_noresrc_delay = 500;
95 TUNABLE_INT("kern.cam.periph_noresrc_delay", &periph_noresrc_delay);
96 static int periph_busy_delay = 500;
97 TUNABLE_INT("kern.cam.periph_busy_delay", &periph_busy_delay);
101 periphdriver_register(void *data)
103 struct periph_driver *drv = (struct periph_driver *)data;
104 struct periph_driver **newdrivers, **old;
107 ndrivers = nperiph_drivers + 2;
108 newdrivers = malloc(sizeof(*newdrivers) * ndrivers, M_CAMPERIPH,
111 bcopy(periph_drivers, newdrivers,
112 sizeof(*newdrivers) * nperiph_drivers);
113 newdrivers[nperiph_drivers] = drv;
114 newdrivers[nperiph_drivers + 1] = NULL;
115 old = periph_drivers;
116 periph_drivers = newdrivers;
118 free(old, M_CAMPERIPH);
120 /* If driver marked as early or it is late now, initialize it. */
121 if (((drv->flags & CAM_PERIPH_DRV_EARLY) != 0 && initialized > 0) ||
127 periphdriver_init(int level)
131 initialized = max(initialized, level);
132 for (i = 0; periph_drivers[i] != NULL; i++) {
133 early = (periph_drivers[i]->flags & CAM_PERIPH_DRV_EARLY) ? 1 : 2;
134 if (early == initialized)
135 (*periph_drivers[i]->init)();
140 cam_periph_alloc(periph_ctor_t *periph_ctor,
141 periph_oninv_t *periph_oninvalidate,
142 periph_dtor_t *periph_dtor, periph_start_t *periph_start,
143 char *name, cam_periph_type type, struct cam_path *path,
144 ac_callback_t *ac_callback, ac_code code, void *arg)
146 struct periph_driver **p_drv;
148 struct cam_periph *periph;
149 struct cam_periph *cur_periph;
151 target_id_t target_id;
158 * Handle Hot-Plug scenarios. If there is already a peripheral
159 * of our type assigned to this path, we are likely waiting for
160 * final close on an old, invalidated, peripheral. If this is
161 * the case, queue up a deferred call to the peripheral's async
162 * handler. If it looks like a mistaken re-allocation, complain.
164 if ((periph = cam_periph_find(path, name)) != NULL) {
166 if ((periph->flags & CAM_PERIPH_INVALID) != 0
167 && (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) == 0) {
168 periph->flags |= CAM_PERIPH_NEW_DEV_FOUND;
169 periph->deferred_callback = ac_callback;
170 periph->deferred_ac = code;
171 return (CAM_REQ_INPROG);
173 printf("cam_periph_alloc: attempt to re-allocate "
174 "valid device %s%d rejected\n",
175 periph->periph_name, periph->unit_number);
177 return (CAM_REQ_INVALID);
180 periph = (struct cam_periph *)malloc(sizeof(*periph), M_CAMPERIPH,
184 return (CAM_RESRC_UNAVAIL);
189 sim = xpt_path_sim(path);
190 path_id = xpt_path_path_id(path);
191 target_id = xpt_path_target_id(path);
192 lun_id = xpt_path_lun_id(path);
193 bzero(periph, sizeof(*periph));
194 cam_init_pinfo(&periph->pinfo);
195 periph->periph_start = periph_start;
196 periph->periph_dtor = periph_dtor;
197 periph->periph_oninval = periph_oninvalidate;
199 periph->periph_name = name;
200 periph->immediate_priority = CAM_PRIORITY_NONE;
201 periph->refcount = 0;
203 SLIST_INIT(&periph->ccb_list);
204 status = xpt_create_path(&path, periph, path_id, target_id, lun_id);
205 if (status != CAM_REQ_CMP)
210 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
211 if (strcmp((*p_drv)->driver_name, name) == 0)
214 if (*p_drv == NULL) {
215 printf("cam_periph_alloc: invalid periph name '%s'\n", name);
216 xpt_free_path(periph->path);
217 free(periph, M_CAMPERIPH);
219 return (CAM_REQ_INVALID);
221 periph->unit_number = camperiphunit(*p_drv, path_id, target_id, lun_id);
222 cur_periph = TAILQ_FIRST(&(*p_drv)->units);
223 while (cur_periph != NULL
224 && cur_periph->unit_number < periph->unit_number)
225 cur_periph = TAILQ_NEXT(cur_periph, unit_links);
226 if (cur_periph != NULL) {
227 KASSERT(cur_periph->unit_number != periph->unit_number, ("duplicate units on periph list"));
228 TAILQ_INSERT_BEFORE(cur_periph, periph, unit_links);
230 TAILQ_INSERT_TAIL(&(*p_drv)->units, periph, unit_links);
231 (*p_drv)->generation++;
237 status = xpt_add_periph(periph);
238 if (status != CAM_REQ_CMP)
243 status = periph_ctor(periph, arg);
245 if (status == CAM_REQ_CMP)
249 switch (init_level) {
251 /* Initialized successfully */
254 xpt_remove_periph(periph);
258 TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
260 xpt_free_path(periph->path);
263 free(periph, M_CAMPERIPH);
266 /* No cleanup to perform. */
269 panic("cam_periph_alloc: Unkown init level");
275 * Find a peripheral structure with the specified path, target, lun,
276 * and (optionally) type. If the name is NULL, this function will return
277 * the first peripheral driver that matches the specified path.
280 cam_periph_find(struct cam_path *path, char *name)
282 struct periph_driver **p_drv;
283 struct cam_periph *periph;
286 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
288 if (name != NULL && (strcmp((*p_drv)->driver_name, name) != 0))
291 TAILQ_FOREACH(periph, &(*p_drv)->units, unit_links) {
292 if (xpt_path_comp(periph->path, path) == 0) {
294 mtx_assert(periph->sim->mtx, MA_OWNED);
308 * Find a peripheral structure with the specified path, target, lun,
309 * and (optionally) type. If the name is NULL, this function will return
310 * the first peripheral driver that matches the specified path.
313 cam_periph_list(struct cam_path *path, struct sbuf *sb)
315 struct periph_driver **p_drv;
316 struct cam_periph *periph;
321 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
323 TAILQ_FOREACH(periph, &(*p_drv)->units, unit_links) {
324 if (xpt_path_comp(periph->path, path) != 0)
327 if (sbuf_len(sb) != 0)
330 sbuf_printf(sb, "%s%d", periph->periph_name,
331 periph->unit_number);
340 cam_periph_acquire(struct cam_periph *periph)
344 return(CAM_REQ_CMP_ERR);
354 cam_periph_release_locked(struct cam_periph *periph)
361 if (periph->refcount != 0) {
364 xpt_print(periph->path, "%s: release %p when refcount is zero\n ", __func__, periph);
366 if (periph->refcount == 0
367 && (periph->flags & CAM_PERIPH_INVALID)) {
368 camperiphfree(periph);
374 cam_periph_release(struct cam_periph *periph)
382 mtx_assert(sim->mtx, MA_NOTOWNED);
384 cam_periph_release_locked(periph);
385 mtx_unlock(sim->mtx);
389 cam_periph_hold(struct cam_periph *periph, int priority)
394 * Increment the reference count on the peripheral
395 * while we wait for our lock attempt to succeed
396 * to ensure the peripheral doesn't disappear out
397 * from user us while we sleep.
400 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
403 mtx_assert(periph->sim->mtx, MA_OWNED);
404 while ((periph->flags & CAM_PERIPH_LOCKED) != 0) {
405 periph->flags |= CAM_PERIPH_LOCK_WANTED;
406 if ((error = mtx_sleep(periph, periph->sim->mtx, priority,
407 "caplck", 0)) != 0) {
408 cam_periph_release_locked(periph);
413 periph->flags |= CAM_PERIPH_LOCKED;
418 cam_periph_unhold(struct cam_periph *periph)
421 mtx_assert(periph->sim->mtx, MA_OWNED);
423 periph->flags &= ~CAM_PERIPH_LOCKED;
424 if ((periph->flags & CAM_PERIPH_LOCK_WANTED) != 0) {
425 periph->flags &= ~CAM_PERIPH_LOCK_WANTED;
429 cam_periph_release_locked(periph);
433 * Look for the next unit number that is not currently in use for this
434 * peripheral type starting at "newunit". Also exclude unit numbers that
435 * are reserved by for future "hardwiring" unless we already know that this
436 * is a potential wired device. Only assume that the device is "wired" the
437 * first time through the loop since after that we'll be looking at unit
438 * numbers that did not match a wiring entry.
441 camperiphnextunit(struct periph_driver *p_drv, u_int newunit, int wired,
442 path_id_t pathid, target_id_t target, lun_id_t lun)
444 struct cam_periph *periph;
446 int i, val, dunit, r;
447 const char *dname, *strval;
449 periph_name = p_drv->driver_name;
452 for (periph = TAILQ_FIRST(&p_drv->units);
453 periph != NULL && periph->unit_number != newunit;
454 periph = TAILQ_NEXT(periph, unit_links))
457 if (periph != NULL && periph->unit_number == newunit) {
459 xpt_print(periph->path, "Duplicate Wired "
461 xpt_print(periph->path, "Second device (%s "
462 "device at scbus%d target %d lun %d) will "
463 "not be wired\n", periph_name, pathid,
473 * Don't match entries like "da 4" as a wired down
474 * device, but do match entries like "da 4 target 5"
475 * or even "da 4 scbus 1".
480 r = resource_find_dev(&i, dname, &dunit, NULL, NULL);
483 /* if no "target" and no specific scbus, skip */
484 if (resource_int_value(dname, dunit, "target", &val) &&
485 (resource_string_value(dname, dunit, "at",&strval)||
486 strcmp(strval, "scbus") == 0))
488 if (newunit == dunit)
498 camperiphunit(struct periph_driver *p_drv, path_id_t pathid,
499 target_id_t target, lun_id_t lun)
502 int wired, i, val, dunit;
503 const char *dname, *strval;
504 char pathbuf[32], *periph_name;
506 periph_name = p_drv->driver_name;
507 snprintf(pathbuf, sizeof(pathbuf), "scbus%d", pathid);
511 for (wired = 0; resource_find_dev(&i, dname, &dunit, NULL, NULL) == 0;
513 if (resource_string_value(dname, dunit, "at", &strval) == 0) {
514 if (strcmp(strval, pathbuf) != 0)
518 if (resource_int_value(dname, dunit, "target", &val) == 0) {
523 if (resource_int_value(dname, dunit, "lun", &val) == 0) {
535 * Either start from 0 looking for the next unit or from
536 * the unit number given in the resource config. This way,
537 * if we have wildcard matches, we don't return the same
540 unit = camperiphnextunit(p_drv, unit, wired, pathid, target, lun);
546 cam_periph_invalidate(struct cam_periph *periph)
550 * We only call this routine the first time a peripheral is
553 if (((periph->flags & CAM_PERIPH_INVALID) == 0)
554 && (periph->periph_oninval != NULL))
555 periph->periph_oninval(periph);
557 periph->flags |= CAM_PERIPH_INVALID;
558 periph->flags &= ~CAM_PERIPH_NEW_DEV_FOUND;
561 if (periph->refcount == 0)
562 camperiphfree(periph);
567 camperiphfree(struct cam_periph *periph)
569 struct periph_driver **p_drv;
571 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
572 if (strcmp((*p_drv)->driver_name, periph->periph_name) == 0)
575 if (*p_drv == NULL) {
576 printf("camperiphfree: attempt to free non-existant periph\n");
580 TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
581 (*p_drv)->generation++;
584 if (periph->periph_dtor != NULL)
585 periph->periph_dtor(periph);
586 xpt_remove_periph(periph);
588 if (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) {
592 switch (periph->deferred_ac) {
593 case AC_FOUND_DEVICE:
594 ccb.ccb_h.func_code = XPT_GDEV_TYPE;
595 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
599 case AC_PATH_REGISTERED:
600 ccb.ccb_h.func_code = XPT_PATH_INQ;
601 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
609 periph->deferred_callback(NULL, periph->deferred_ac,
612 xpt_free_path(periph->path);
613 free(periph, M_CAMPERIPH);
618 * Map user virtual pointers into kernel virtual address space, so we can
619 * access the memory. This won't work on physical pointers, for now it's
620 * up to the caller to check for that. (XXX KDM -- should we do that here
621 * instead?) This also only works for up to MAXPHYS memory. Since we use
622 * buffers to map stuff in and out, we're limited to the buffer size.
625 cam_periph_mapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
628 int flags[CAM_PERIPH_MAXMAPS];
629 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
630 u_int32_t lengths[CAM_PERIPH_MAXMAPS];
631 u_int32_t dirs[CAM_PERIPH_MAXMAPS];
632 /* Some controllers may not be able to handle more data. */
633 size_t maxmap = DFLTPHYS;
635 switch(ccb->ccb_h.func_code) {
637 if (ccb->cdm.match_buf_len == 0) {
638 printf("cam_periph_mapmem: invalid match buffer "
642 if (ccb->cdm.pattern_buf_len > 0) {
643 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
644 lengths[0] = ccb->cdm.pattern_buf_len;
645 dirs[0] = CAM_DIR_OUT;
646 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
647 lengths[1] = ccb->cdm.match_buf_len;
648 dirs[1] = CAM_DIR_IN;
651 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
652 lengths[0] = ccb->cdm.match_buf_len;
653 dirs[0] = CAM_DIR_IN;
657 * This request will not go to the hardware, no reason
658 * to be so strict. vmapbuf() is able to map up to MAXPHYS.
663 case XPT_CONT_TARGET_IO:
664 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
667 data_ptrs[0] = &ccb->csio.data_ptr;
668 lengths[0] = ccb->csio.dxfer_len;
669 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
673 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
676 data_ptrs[0] = &ccb->ataio.data_ptr;
677 lengths[0] = ccb->ataio.dxfer_len;
678 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
682 data_ptrs[0] = &ccb->smpio.smp_request;
683 lengths[0] = ccb->smpio.smp_request_len;
684 dirs[0] = CAM_DIR_OUT;
685 data_ptrs[1] = &ccb->smpio.smp_response;
686 lengths[1] = ccb->smpio.smp_response_len;
687 dirs[1] = CAM_DIR_IN;
690 case XPT_DEV_ADVINFO:
691 if (ccb->cdai.bufsiz == 0)
694 data_ptrs[0] = (uint8_t **)&ccb->cdai.buf;
695 lengths[0] = ccb->cdai.bufsiz;
696 dirs[0] = CAM_DIR_IN;
700 * This request will not go to the hardware, no reason
701 * to be so strict. vmapbuf() is able to map up to MAXPHYS.
707 break; /* NOTREACHED */
711 * Check the transfer length and permissions first, so we don't
712 * have to unmap any previously mapped buffers.
714 for (i = 0; i < numbufs; i++) {
719 * The userland data pointer passed in may not be page
720 * aligned. vmapbuf() truncates the address to a page
721 * boundary, so if the address isn't page aligned, we'll
722 * need enough space for the given transfer length, plus
723 * whatever extra space is necessary to make it to the page
727 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)) > maxmap){
728 printf("cam_periph_mapmem: attempt to map %lu bytes, "
729 "which is greater than %lu\n",
731 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)),
736 if (dirs[i] & CAM_DIR_OUT) {
737 flags[i] = BIO_WRITE;
740 if (dirs[i] & CAM_DIR_IN) {
746 /* this keeps the current process from getting swapped */
748 * XXX KDM should I use P_NOSWAP instead?
752 for (i = 0; i < numbufs; i++) {
756 mapinfo->bp[i] = getpbuf(NULL);
758 /* save the buffer's data address */
759 mapinfo->bp[i]->b_saveaddr = mapinfo->bp[i]->b_data;
761 /* put our pointer in the data slot */
762 mapinfo->bp[i]->b_data = *data_ptrs[i];
764 /* set the transfer length, we know it's < MAXPHYS */
765 mapinfo->bp[i]->b_bufsize = lengths[i];
767 /* set the direction */
768 mapinfo->bp[i]->b_iocmd = flags[i];
771 * Map the buffer into kernel memory.
773 * Note that useracc() alone is not a sufficient test.
774 * vmapbuf() can still fail due to a smaller file mapped
775 * into a larger area of VM, or if userland races against
776 * vmapbuf() after the useracc() check.
778 if (vmapbuf(mapinfo->bp[i]) < 0) {
779 for (j = 0; j < i; ++j) {
780 *data_ptrs[j] = mapinfo->bp[j]->b_saveaddr;
781 vunmapbuf(mapinfo->bp[j]);
782 relpbuf(mapinfo->bp[j], NULL);
784 relpbuf(mapinfo->bp[i], NULL);
789 /* set our pointer to the new mapped area */
790 *data_ptrs[i] = mapinfo->bp[i]->b_data;
792 mapinfo->num_bufs_used++;
796 * Now that we've gotten this far, change ownership to the kernel
797 * of the buffers so that we don't run afoul of returning to user
798 * space with locks (on the buffer) held.
800 for (i = 0; i < numbufs; i++) {
801 BUF_KERNPROC(mapinfo->bp[i]);
809 * Unmap memory segments mapped into kernel virtual address space by
810 * cam_periph_mapmem().
813 cam_periph_unmapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
816 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
818 if (mapinfo->num_bufs_used <= 0) {
819 /* allow ourselves to be swapped once again */
824 switch (ccb->ccb_h.func_code) {
826 numbufs = min(mapinfo->num_bufs_used, 2);
829 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
831 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
832 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
836 case XPT_CONT_TARGET_IO:
837 data_ptrs[0] = &ccb->csio.data_ptr;
838 numbufs = min(mapinfo->num_bufs_used, 1);
841 data_ptrs[0] = &ccb->ataio.data_ptr;
842 numbufs = min(mapinfo->num_bufs_used, 1);
845 numbufs = min(mapinfo->num_bufs_used, 2);
846 data_ptrs[0] = &ccb->smpio.smp_request;
847 data_ptrs[1] = &ccb->smpio.smp_response;
849 case XPT_DEV_ADVINFO:
850 numbufs = min(mapinfo->num_bufs_used, 1);
851 data_ptrs[0] = (uint8_t **)&ccb->cdai.buf;
854 /* allow ourselves to be swapped once again */
857 break; /* NOTREACHED */
860 for (i = 0; i < numbufs; i++) {
861 /* Set the user's pointer back to the original value */
862 *data_ptrs[i] = mapinfo->bp[i]->b_saveaddr;
864 /* unmap the buffer */
865 vunmapbuf(mapinfo->bp[i]);
867 /* release the buffer */
868 relpbuf(mapinfo->bp[i], NULL);
871 /* allow ourselves to be swapped once again */
876 cam_periph_getccb(struct cam_periph *periph, u_int32_t priority)
878 struct ccb_hdr *ccb_h;
880 mtx_assert(periph->sim->mtx, MA_OWNED);
881 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdgetccb\n"));
883 while (SLIST_FIRST(&periph->ccb_list) == NULL) {
884 if (periph->immediate_priority > priority)
885 periph->immediate_priority = priority;
886 xpt_schedule(periph, priority);
887 if ((SLIST_FIRST(&periph->ccb_list) != NULL)
888 && (SLIST_FIRST(&periph->ccb_list)->pinfo.priority == priority))
890 mtx_assert(periph->sim->mtx, MA_OWNED);
891 mtx_sleep(&periph->ccb_list, periph->sim->mtx, PRIBIO, "cgticb",
895 ccb_h = SLIST_FIRST(&periph->ccb_list);
896 SLIST_REMOVE_HEAD(&periph->ccb_list, periph_links.sle);
897 return ((union ccb *)ccb_h);
901 cam_periph_ccbwait(union ccb *ccb)
905 sim = xpt_path_sim(ccb->ccb_h.path);
906 if ((ccb->ccb_h.pinfo.index != CAM_UNQUEUED_INDEX)
907 || ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG))
908 mtx_sleep(&ccb->ccb_h.cbfcnp, sim->mtx, PRIBIO, "cbwait", 0);
912 cam_periph_ioctl(struct cam_periph *periph, u_long cmd, caddr_t addr,
913 int (*error_routine)(union ccb *ccb,
915 u_int32_t sense_flags))
925 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
926 xpt_setup_ccb(&ccb->ccb_h,
928 CAM_PRIORITY_NORMAL);
929 ccb->ccb_h.func_code = XPT_GDEVLIST;
932 * Basically, the point of this is that we go through
933 * getting the list of devices, until we find a passthrough
934 * device. In the current version of the CAM code, the
935 * only way to determine what type of device we're dealing
936 * with is by its name.
940 ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS;
941 while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) {
943 /* we want the next device in the list */
945 if (strncmp(ccb->cgdl.periph_name,
951 if ((ccb->cgdl.status == CAM_GDEVLIST_LAST_DEVICE) &&
953 ccb->cgdl.periph_name[0] = '\0';
954 ccb->cgdl.unit_number = 0;
959 /* copy the result back out */
960 bcopy(ccb, addr, sizeof(union ccb));
962 /* and release the ccb */
963 xpt_release_ccb(ccb);
974 cam_periph_runccb(union ccb *ccb,
975 int (*error_routine)(union ccb *ccb,
977 u_int32_t sense_flags),
978 cam_flags camflags, u_int32_t sense_flags,
985 sim = xpt_path_sim(ccb->ccb_h.path);
986 mtx_assert(sim->mtx, MA_OWNED);
989 * If the user has supplied a stats structure, and if we understand
990 * this particular type of ccb, record the transaction start.
992 if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO ||
993 ccb->ccb_h.func_code == XPT_ATA_IO))
994 devstat_start_transaction(ds, NULL);
999 cam_periph_ccbwait(ccb);
1000 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
1002 else if (error_routine != NULL)
1003 error = (*error_routine)(ccb, camflags, sense_flags);
1007 } while (error == ERESTART);
1009 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1010 cam_release_devq(ccb->ccb_h.path,
1011 /* relsim_flags */0,
1014 /* getcount_only */ FALSE);
1015 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1019 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1020 devstat_end_transaction(ds,
1021 ccb->csio.dxfer_len,
1022 ccb->csio.tag_action & 0x3,
1023 ((ccb->ccb_h.flags & CAM_DIR_MASK) ==
1024 CAM_DIR_NONE) ? DEVSTAT_NO_DATA :
1025 (ccb->ccb_h.flags & CAM_DIR_OUT) ?
1027 DEVSTAT_READ, NULL, NULL);
1028 } else if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1029 devstat_end_transaction(ds,
1030 ccb->ataio.dxfer_len,
1031 ccb->ataio.tag_action & 0x3,
1032 ((ccb->ccb_h.flags & CAM_DIR_MASK) ==
1033 CAM_DIR_NONE) ? DEVSTAT_NO_DATA :
1034 (ccb->ccb_h.flags & CAM_DIR_OUT) ?
1036 DEVSTAT_READ, NULL, NULL);
1044 cam_freeze_devq(struct cam_path *path)
1047 cam_freeze_devq_arg(path, 0, 0);
1051 cam_freeze_devq_arg(struct cam_path *path, uint32_t flags, uint32_t arg)
1053 struct ccb_relsim crs;
1055 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NONE);
1056 crs.ccb_h.func_code = XPT_FREEZE_QUEUE;
1057 crs.release_flags = flags;
1059 crs.release_timeout = arg;
1060 xpt_action((union ccb *)&crs);
1064 cam_release_devq(struct cam_path *path, u_int32_t relsim_flags,
1065 u_int32_t openings, u_int32_t arg,
1068 struct ccb_relsim crs;
1070 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
1071 crs.ccb_h.func_code = XPT_REL_SIMQ;
1072 crs.ccb_h.flags = getcount_only ? CAM_DEV_QFREEZE : 0;
1073 crs.release_flags = relsim_flags;
1074 crs.openings = openings;
1075 crs.release_timeout = arg;
1076 xpt_action((union ccb *)&crs);
1077 return (crs.qfrozen_cnt);
1080 #define saved_ccb_ptr ppriv_ptr0
1081 #define recovery_depth ppriv_field1
1083 camperiphsensedone(struct cam_periph *periph, union ccb *done_ccb)
1085 union ccb *saved_ccb = (union ccb *)done_ccb->ccb_h.saved_ccb_ptr;
1089 int depth = done_ccb->ccb_h.recovery_depth;
1091 status = done_ccb->ccb_h.status;
1092 if (status & CAM_DEV_QFRZN) {
1095 * Clear freeze flag now for case of retry,
1096 * freeze will be dropped later.
1098 done_ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1100 status &= CAM_STATUS_MASK;
1105 * If we manually retrieved sense into a CCB and got
1106 * something other than "NO SENSE" send the updated CCB
1107 * back to the client via xpt_done() to be processed via
1108 * the error recovery code again.
1110 sense_key = saved_ccb->csio.sense_data.flags;
1111 sense_key &= SSD_KEY;
1112 if (sense_key != SSD_KEY_NO_SENSE) {
1113 saved_ccb->ccb_h.status |=
1116 saved_ccb->ccb_h.status &=
1118 saved_ccb->ccb_h.status |=
1121 saved_ccb->csio.sense_resid = done_ccb->csio.resid;
1122 bcopy(saved_ccb, done_ccb, sizeof(union ccb));
1123 xpt_free_ccb(saved_ccb);
1127 bcopy(saved_ccb, done_ccb, sizeof(union ccb));
1128 xpt_free_ccb(saved_ccb);
1129 done_ccb->ccb_h.status &= ~CAM_STATUS_MASK;
1130 done_ccb->ccb_h.status |= CAM_AUTOSENSE_FAIL;
1133 periph->flags &= ~CAM_PERIPH_SENSE_INPROG;
1135 * If it is the end of recovery, drop freeze, taken due to
1136 * CAM_DEV_QFREEZE flag, set on recovery request.
1139 cam_release_devq(done_ccb->ccb_h.path,
1143 /*getcount_only*/0);
1146 * Copy frozen flag from recovery request if it is set there
1150 done_ccb->ccb_h.status |= CAM_DEV_QFRZN;
1151 (*done_ccb->ccb_h.cbfcnp)(periph, done_ccb);
1155 camperiphdone(struct cam_periph *periph, union ccb *done_ccb)
1157 union ccb *saved_ccb, *save_ccb;
1160 struct scsi_start_stop_unit *scsi_cmd;
1161 u_int32_t relsim_flags, timeout;
1163 status = done_ccb->ccb_h.status;
1164 if (status & CAM_DEV_QFRZN) {
1167 * Clear freeze flag now for case of retry,
1168 * freeze will be dropped later.
1170 done_ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1175 saved_ccb = (union ccb *)done_ccb->ccb_h.saved_ccb_ptr;
1177 switch (status & CAM_STATUS_MASK) {
1181 * If we have successfully taken a device from the not
1182 * ready to ready state, re-scan the device and re-get
1183 * the inquiry information. Many devices (mostly disks)
1184 * don't properly report their inquiry information unless
1187 scsi_cmd = (struct scsi_start_stop_unit *)
1188 &done_ccb->csio.cdb_io.cdb_bytes;
1190 if (scsi_cmd->opcode == START_STOP_UNIT)
1191 xpt_async(AC_INQ_CHANGED,
1192 done_ccb->ccb_h.path, NULL);
1195 case CAM_SCSI_STATUS_ERROR:
1196 scsi_cmd = (struct scsi_start_stop_unit *)
1197 &done_ccb->csio.cdb_io.cdb_bytes;
1198 if (status & CAM_AUTOSNS_VALID) {
1199 struct ccb_getdev cgd;
1200 struct scsi_sense_data *sense;
1201 int error_code, sense_key, asc, ascq;
1202 scsi_sense_action err_action;
1204 sense = &done_ccb->csio.sense_data;
1205 scsi_extract_sense(sense, &error_code,
1206 &sense_key, &asc, &ascq);
1208 * Grab the inquiry data for this device.
1210 xpt_setup_ccb(&cgd.ccb_h, done_ccb->ccb_h.path,
1211 CAM_PRIORITY_NORMAL);
1212 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1213 xpt_action((union ccb *)&cgd);
1214 err_action = scsi_error_action(&done_ccb->csio,
1217 * If the error is "invalid field in CDB",
1218 * and the load/eject flag is set, turn the
1219 * flag off and try again. This is just in
1220 * case the drive in question barfs on the
1221 * load eject flag. The CAM code should set
1222 * the load/eject flag by default for
1226 * Should we check to see what the specific
1227 * scsi status is?? Or does it not matter
1228 * since we already know that there was an
1229 * error, and we know what the specific
1230 * error code was, and we know what the
1233 if ((scsi_cmd->opcode == START_STOP_UNIT) &&
1234 ((scsi_cmd->how & SSS_LOEJ) != 0) &&
1235 (asc == 0x24) && (ascq == 0x00) &&
1236 (done_ccb->ccb_h.retry_count > 0)) {
1238 scsi_cmd->how &= ~SSS_LOEJ;
1239 xpt_action(done_ccb);
1240 } else if ((done_ccb->ccb_h.retry_count > 1)
1241 && ((err_action & SS_MASK) != SS_FAIL)) {
1244 * In this case, the error recovery
1245 * command failed, but we've got
1246 * some retries left on it. Give
1247 * it another try unless this is an
1248 * unretryable error.
1250 /* set the timeout to .5 sec */
1252 RELSIM_RELEASE_AFTER_TIMEOUT;
1254 xpt_action(done_ccb);
1258 * Perform the final retry with the original
1259 * CCB so that final error processing is
1260 * performed by the owner of the CCB.
1265 save_ccb = xpt_alloc_ccb_nowait();
1266 if (save_ccb == NULL)
1268 bcopy(done_ccb, save_ccb, sizeof(*save_ccb));
1269 periph->flags |= CAM_PERIPH_SENSE_INPROG;
1271 * Send a Request Sense to the device. We
1272 * assume that we are in a contingent allegiance
1273 * condition so we do not tag this request.
1275 scsi_request_sense(&done_ccb->csio, /*retries*/1,
1277 &save_ccb->csio.sense_data,
1278 save_ccb->csio.sense_len,
1279 CAM_TAG_ACTION_NONE,
1280 /*sense_len*/SSD_FULL_SIZE,
1282 done_ccb->ccb_h.pinfo.priority--;
1283 done_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1284 done_ccb->ccb_h.saved_ccb_ptr = save_ccb;
1285 done_ccb->ccb_h.recovery_depth++;
1286 xpt_action(done_ccb);
1291 bcopy(saved_ccb, done_ccb, sizeof(*done_ccb));
1292 xpt_free_ccb(saved_ccb);
1293 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1294 xpt_action(done_ccb);
1298 /* decrement the retry count */
1300 * XXX This isn't appropriate in all cases. Restructure,
1301 * so that the retry count is only decremented on an
1302 * actual retry. Remeber that the orignal ccb had its
1303 * retry count dropped before entering recovery, so
1304 * doing it again is a bug.
1306 if (done_ccb->ccb_h.retry_count > 0)
1307 done_ccb->ccb_h.retry_count--;
1309 * Drop freeze taken due to CAM_DEV_QFREEZE flag set on recovery
1312 cam_release_devq(done_ccb->ccb_h.path,
1313 /*relsim_flags*/relsim_flags,
1316 /*getcount_only*/0);
1317 /* Drop freeze taken, if this recovery request got error. */
1319 cam_release_devq(done_ccb->ccb_h.path,
1323 /*getcount_only*/0);
1328 * Generic Async Event handler. Peripheral drivers usually
1329 * filter out the events that require personal attention,
1330 * and leave the rest to this function.
1333 cam_periph_async(struct cam_periph *periph, u_int32_t code,
1334 struct cam_path *path, void *arg)
1337 case AC_LOST_DEVICE:
1338 cam_periph_invalidate(periph);
1346 cam_periph_bus_settle(struct cam_periph *periph, u_int bus_settle)
1348 struct ccb_getdevstats cgds;
1350 xpt_setup_ccb(&cgds.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1351 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1352 xpt_action((union ccb *)&cgds);
1353 cam_periph_freeze_after_event(periph, &cgds.last_reset, bus_settle);
1357 cam_periph_freeze_after_event(struct cam_periph *periph,
1358 struct timeval* event_time, u_int duration_ms)
1360 struct timeval delta;
1361 struct timeval duration_tv;
1364 timevalsub(&delta, event_time);
1365 duration_tv.tv_sec = duration_ms / 1000;
1366 duration_tv.tv_usec = (duration_ms % 1000) * 1000;
1367 if (timevalcmp(&delta, &duration_tv, <)) {
1368 timevalsub(&duration_tv, &delta);
1370 duration_ms = duration_tv.tv_sec * 1000;
1371 duration_ms += duration_tv.tv_usec / 1000;
1372 cam_freeze_devq(periph->path);
1373 cam_release_devq(periph->path,
1374 RELSIM_RELEASE_AFTER_TIMEOUT,
1376 /*timeout*/duration_ms,
1377 /*getcount_only*/0);
1383 camperiphscsistatuserror(union ccb *ccb, cam_flags camflags,
1384 u_int32_t sense_flags,
1385 int *openings, u_int32_t *relsim_flags,
1386 u_int32_t *timeout, const char **action_string)
1390 switch (ccb->csio.scsi_status) {
1391 case SCSI_STATUS_OK:
1392 case SCSI_STATUS_COND_MET:
1393 case SCSI_STATUS_INTERMED:
1394 case SCSI_STATUS_INTERMED_COND_MET:
1397 case SCSI_STATUS_CMD_TERMINATED:
1398 case SCSI_STATUS_CHECK_COND:
1400 xpt_print(ccb->ccb_h.path, "SCSI status error\n");
1401 error = camperiphscsisenseerror(ccb,
1409 case SCSI_STATUS_QUEUE_FULL:
1412 struct ccb_getdevstats cgds;
1415 * First off, find out what the current
1416 * transaction counts are.
1418 xpt_setup_ccb(&cgds.ccb_h,
1420 CAM_PRIORITY_NORMAL);
1421 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1422 xpt_action((union ccb *)&cgds);
1425 * If we were the only transaction active, treat
1426 * the QUEUE FULL as if it were a BUSY condition.
1428 if (cgds.dev_active != 0) {
1432 * Reduce the number of openings to
1433 * be 1 less than the amount it took
1434 * to get a queue full bounded by the
1435 * minimum allowed tag count for this
1438 total_openings = cgds.dev_active + cgds.dev_openings;
1439 *openings = cgds.dev_active;
1440 if (*openings < cgds.mintags)
1441 *openings = cgds.mintags;
1442 if (*openings < total_openings)
1443 *relsim_flags = RELSIM_ADJUST_OPENINGS;
1446 * Some devices report queue full for
1447 * temporary resource shortages. For
1448 * this reason, we allow a minimum
1449 * tag count to be entered via a
1450 * quirk entry to prevent the queue
1451 * count on these devices from falling
1452 * to a pessimisticly low value. We
1453 * still wait for the next successful
1454 * completion, however, before queueing
1455 * more transactions to the device.
1457 *relsim_flags = RELSIM_RELEASE_AFTER_CMDCMPLT;
1462 xpt_print(ccb->ccb_h.path, "Queue full\n");
1468 case SCSI_STATUS_BUSY:
1470 * Restart the queue after either another
1471 * command completes or a 1 second timeout.
1474 xpt_print(ccb->ccb_h.path, "Device busy\n");
1476 if (ccb->ccb_h.retry_count > 0) {
1477 ccb->ccb_h.retry_count--;
1479 *relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT
1480 | RELSIM_RELEASE_AFTER_CMDCMPLT;
1486 case SCSI_STATUS_RESERV_CONFLICT:
1487 xpt_print(ccb->ccb_h.path, "Reservation conflict\n");
1491 xpt_print(ccb->ccb_h.path, "SCSI status 0x%x\n",
1492 ccb->csio.scsi_status);
1500 camperiphscsisenseerror(union ccb *ccb, cam_flags camflags,
1501 u_int32_t sense_flags,
1502 int *openings, u_int32_t *relsim_flags,
1503 u_int32_t *timeout, const char **action_string)
1505 struct cam_periph *periph;
1506 union ccb *orig_ccb = ccb;
1509 periph = xpt_path_periph(ccb->ccb_h.path);
1511 (CAM_PERIPH_RECOVERY_INPROG | CAM_PERIPH_SENSE_INPROG)) {
1513 * If error recovery is already in progress, don't attempt
1514 * to process this error, but requeue it unconditionally
1515 * and attempt to process it once error recovery has
1516 * completed. This failed command is probably related to
1517 * the error that caused the currently active error recovery
1518 * action so our current recovery efforts should also
1519 * address this command. Be aware that the error recovery
1520 * code assumes that only one recovery action is in progress
1521 * on a particular peripheral instance at any given time
1522 * (e.g. only one saved CCB for error recovery) so it is
1523 * imperitive that we don't violate this assumption.
1527 scsi_sense_action err_action;
1528 struct ccb_getdev cgd;
1531 * Grab the inquiry data for this device.
1533 xpt_setup_ccb(&cgd.ccb_h, ccb->ccb_h.path, CAM_PRIORITY_NORMAL);
1534 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1535 xpt_action((union ccb *)&cgd);
1537 if ((ccb->ccb_h.status & CAM_AUTOSNS_VALID) != 0)
1538 err_action = scsi_error_action(&ccb->csio,
1541 else if ((ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)
1542 err_action = SS_REQSENSE;
1544 err_action = SS_RETRY|SSQ_DECREMENT_COUNT|EIO;
1546 error = err_action & SS_ERRMASK;
1549 * If the recovery action will consume a retry,
1550 * make sure we actually have retries available.
1552 if ((err_action & SSQ_DECREMENT_COUNT) != 0) {
1553 if (ccb->ccb_h.retry_count > 0 &&
1554 (periph->flags & CAM_PERIPH_INVALID) == 0)
1555 ccb->ccb_h.retry_count--;
1557 *action_string = "Retries exhausted";
1558 goto sense_error_done;
1562 if ((err_action & SS_MASK) >= SS_START) {
1564 * Do common portions of commands that
1565 * use recovery CCBs.
1567 orig_ccb = xpt_alloc_ccb_nowait();
1568 if (orig_ccb == NULL) {
1569 *action_string = "Can't allocate recovery CCB";
1570 goto sense_error_done;
1573 * Clear freeze flag for original request here, as
1574 * this freeze will be dropped as part of ERESTART.
1576 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1577 bcopy(ccb, orig_ccb, sizeof(*orig_ccb));
1580 switch (err_action & SS_MASK) {
1582 *action_string = "No recovery action needed";
1586 *action_string = "Retrying command (per sense data)";
1590 *action_string = "Unretryable error";
1595 if (SID_TYPE(&cgd.inq_data) == T_SEQUENTIAL) {
1596 xpt_free_ccb(orig_ccb);
1597 ccb->ccb_h.status |= CAM_DEV_QFRZN;
1598 *action_string = "Will not autostart a "
1599 "sequential access device";
1600 err_action = SS_FAIL;
1606 * Send a start unit command to the device, and
1607 * then retry the command.
1609 *action_string = "Attempting to start unit";
1610 periph->flags |= CAM_PERIPH_RECOVERY_INPROG;
1613 * Check for removable media and set
1614 * load/eject flag appropriately.
1616 if (SID_IS_REMOVABLE(&cgd.inq_data))
1621 scsi_start_stop(&ccb->csio,
1635 * Send a Test Unit Ready to the device.
1636 * If the 'many' flag is set, we send 120
1637 * test unit ready commands, one every half
1638 * second. Otherwise, we just send one TUR.
1639 * We only want to do this if the retry
1640 * count has not been exhausted.
1644 if ((err_action & SSQ_MANY) != 0) {
1645 *action_string = "Polling device for readiness";
1648 *action_string = "Testing device for readiness";
1651 periph->flags |= CAM_PERIPH_RECOVERY_INPROG;
1652 scsi_test_unit_ready(&ccb->csio,
1660 * Accomplish our 500ms delay by deferring
1661 * the release of our device queue appropriately.
1663 *relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1669 *action_string = "Requesting SCSI sense data";
1670 periph->flags |= CAM_PERIPH_SENSE_INPROG;
1672 * Send a Request Sense to the device. We
1673 * assume that we are in a contingent allegiance
1674 * condition so we do not tag this request.
1676 scsi_request_sense(&ccb->csio, /*retries*/1,
1678 &orig_ccb->csio.sense_data,
1679 orig_ccb->csio.sense_len,
1680 CAM_TAG_ACTION_NONE,
1681 /*sense_len*/SSD_FULL_SIZE,
1686 panic("Unhandled error action %x", err_action);
1689 if ((err_action & SS_MASK) >= SS_START) {
1691 * Drop the priority, so that the recovery
1692 * CCB is the first to execute. Freeze the queue
1693 * after this command is sent so that we can
1694 * restore the old csio and have it queued in
1695 * the proper order before we release normal
1696 * transactions to the device.
1698 ccb->ccb_h.pinfo.priority--;
1699 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1700 ccb->ccb_h.saved_ccb_ptr = orig_ccb;
1701 ccb->ccb_h.recovery_depth = 0;
1706 if ((err_action & SSQ_PRINT_SENSE) != 0
1707 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID) != 0)
1708 cam_error_print(orig_ccb, CAM_ESF_ALL, CAM_EPF_ALL);
1714 * Generic error handler. Peripheral drivers usually filter
1715 * out the errors that they handle in a unique mannor, then
1716 * call this function.
1719 cam_periph_error(union ccb *ccb, cam_flags camflags,
1720 u_int32_t sense_flags, union ccb *save_ccb)
1722 struct cam_periph *periph;
1723 const char *action_string;
1726 int error, printed = 0;
1728 u_int32_t relsim_flags;
1729 u_int32_t timeout = 0;
1731 periph = xpt_path_periph(ccb->ccb_h.path);
1732 action_string = NULL;
1733 status = ccb->ccb_h.status;
1734 frozen = (status & CAM_DEV_QFRZN) != 0;
1735 status &= CAM_STATUS_MASK;
1736 openings = relsim_flags = 0;
1742 case CAM_SCSI_STATUS_ERROR:
1743 error = camperiphscsistatuserror(ccb,
1751 case CAM_AUTOSENSE_FAIL:
1752 xpt_print(ccb->ccb_h.path, "AutoSense failed\n");
1753 error = EIO; /* we have to kill the command */
1755 case CAM_ATA_STATUS_ERROR:
1756 if (bootverbose && printed == 0) {
1757 xpt_print(ccb->ccb_h.path, "ATA status error\n");
1758 cam_error_print(ccb, CAM_ESF_ALL, CAM_EPF_ALL);
1762 case CAM_REQ_CMP_ERR:
1763 if (bootverbose && printed == 0) {
1764 xpt_print(ccb->ccb_h.path,
1765 "Request completed with CAM_REQ_CMP_ERR\n");
1769 case CAM_CMD_TIMEOUT:
1770 if (bootverbose && printed == 0) {
1771 xpt_print(ccb->ccb_h.path, "Command timed out\n");
1775 case CAM_UNEXP_BUSFREE:
1776 if (bootverbose && printed == 0) {
1777 xpt_print(ccb->ccb_h.path, "Unexpected Bus Free\n");
1781 case CAM_UNCOR_PARITY:
1782 if (bootverbose && printed == 0) {
1783 xpt_print(ccb->ccb_h.path,
1784 "Uncorrected parity error\n");
1788 case CAM_DATA_RUN_ERR:
1789 if (bootverbose && printed == 0) {
1790 xpt_print(ccb->ccb_h.path, "Data overrun\n");
1793 /* decrement the number of retries */
1794 if (ccb->ccb_h.retry_count > 0 &&
1795 (periph->flags & CAM_PERIPH_INVALID) == 0) {
1796 ccb->ccb_h.retry_count--;
1799 action_string = "Retries exhausted";
1805 case CAM_MSG_REJECT_REC:
1806 /* XXX Don't know that these are correct */
1809 case CAM_SEL_TIMEOUT:
1811 struct cam_path *newpath;
1813 if ((camflags & CAM_RETRY_SELTO) != 0) {
1814 if (ccb->ccb_h.retry_count > 0 &&
1815 (periph->flags & CAM_PERIPH_INVALID) == 0) {
1817 ccb->ccb_h.retry_count--;
1819 if (bootverbose && printed == 0) {
1820 xpt_print(ccb->ccb_h.path,
1821 "Selection timeout\n");
1826 * Wait a bit to give the device
1827 * time to recover before we try again.
1829 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1830 timeout = periph_selto_delay;
1833 action_string = "Retries exhausted";
1836 /* Should we do more if we can't create the path?? */
1837 if (xpt_create_path(&newpath, periph,
1838 xpt_path_path_id(ccb->ccb_h.path),
1839 xpt_path_target_id(ccb->ccb_h.path),
1840 CAM_LUN_WILDCARD) != CAM_REQ_CMP)
1844 * Let peripheral drivers know that this device has gone
1847 xpt_async(AC_LOST_DEVICE, newpath, NULL);
1848 xpt_free_path(newpath);
1851 case CAM_REQ_INVALID:
1852 case CAM_PATH_INVALID:
1853 case CAM_DEV_NOT_THERE:
1855 case CAM_PROVIDE_FAIL:
1856 case CAM_REQ_TOO_BIG:
1857 case CAM_LUN_INVALID:
1858 case CAM_TID_INVALID:
1861 case CAM_SCSI_BUS_RESET:
1864 * Commands that repeatedly timeout and cause these
1865 * kinds of error recovery actions, should return
1866 * CAM_CMD_TIMEOUT, which allows us to safely assume
1867 * that this command was an innocent bystander to
1868 * these events and should be unconditionally
1871 if (bootverbose && printed == 0) {
1872 xpt_print_path(ccb->ccb_h.path);
1873 if (status == CAM_BDR_SENT)
1874 printf("Bus Device Reset sent\n");
1876 printf("Bus Reset issued\n");
1880 case CAM_REQUEUE_REQ:
1881 /* Unconditional requeue */
1882 if (bootverbose && printed == 0) {
1883 xpt_print(ccb->ccb_h.path, "Request requeued\n");
1886 if ((periph->flags & CAM_PERIPH_INVALID) == 0)
1889 action_string = "Retries exhausted";
1893 case CAM_RESRC_UNAVAIL:
1894 /* Wait a bit for the resource shortage to abate. */
1895 timeout = periph_noresrc_delay;
1899 /* Wait a bit for the busy condition to abate. */
1900 timeout = periph_busy_delay;
1902 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1905 /* decrement the number of retries */
1906 if (ccb->ccb_h.retry_count > 0 &&
1907 (periph->flags & CAM_PERIPH_INVALID) == 0) {
1908 ccb->ccb_h.retry_count--;
1910 if (bootverbose && printed == 0) {
1911 xpt_print(ccb->ccb_h.path, "CAM status 0x%x\n",
1917 action_string = "Retries exhausted";
1923 * If we have and error and are booting verbosely, whine
1924 * *unless* this was a non-retryable selection timeout.
1926 if (error != 0 && bootverbose &&
1927 !(status == CAM_SEL_TIMEOUT && (camflags & CAM_RETRY_SELTO) == 0)) {
1928 if (error != ERESTART) {
1929 if (action_string == NULL)
1930 action_string = "Unretryable error";
1931 xpt_print(ccb->ccb_h.path, "Error %d, %s\n",
1932 error, action_string);
1933 } else if (action_string != NULL)
1934 xpt_print(ccb->ccb_h.path, "%s\n", action_string);
1936 xpt_print(ccb->ccb_h.path, "Retrying command\n");
1939 /* Attempt a retry */
1940 if (error == ERESTART || error == 0) {
1942 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1943 if (error == ERESTART)
1946 cam_release_devq(ccb->ccb_h.path,
1950 /*getcount_only*/0);