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>
38 #include <sys/linker_set.h>
41 #include <sys/mutex.h>
44 #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 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
190 if (strcmp((*p_drv)->driver_name, name) == 0)
194 if (*p_drv == NULL) {
195 printf("cam_periph_alloc: invalid periph name '%s'\n", name);
196 free(periph, M_CAMPERIPH);
197 return (CAM_REQ_INVALID);
200 sim = xpt_path_sim(path);
201 path_id = xpt_path_path_id(path);
202 target_id = xpt_path_target_id(path);
203 lun_id = xpt_path_lun_id(path);
204 bzero(periph, sizeof(*periph));
205 cam_init_pinfo(&periph->pinfo);
206 periph->periph_start = periph_start;
207 periph->periph_dtor = periph_dtor;
208 periph->periph_oninval = periph_oninvalidate;
210 periph->periph_name = name;
211 periph->unit_number = camperiphunit(*p_drv, path_id, target_id, lun_id);
212 periph->immediate_priority = CAM_PRIORITY_NONE;
213 periph->refcount = 0;
215 SLIST_INIT(&periph->ccb_list);
216 status = xpt_create_path(&path, periph, path_id, target_id, lun_id);
217 if (status != CAM_REQ_CMP)
223 status = xpt_add_periph(periph);
225 if (status != CAM_REQ_CMP)
228 cur_periph = TAILQ_FIRST(&(*p_drv)->units);
229 while (cur_periph != NULL
230 && cur_periph->unit_number < periph->unit_number)
231 cur_periph = TAILQ_NEXT(cur_periph, unit_links);
233 if (cur_periph != NULL)
234 TAILQ_INSERT_BEFORE(cur_periph, periph, unit_links);
236 TAILQ_INSERT_TAIL(&(*p_drv)->units, periph, unit_links);
237 (*p_drv)->generation++;
242 status = periph_ctor(periph, arg);
244 if (status == CAM_REQ_CMP)
248 switch (init_level) {
250 /* Initialized successfully */
253 TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
254 xpt_remove_periph(periph);
257 xpt_free_path(periph->path);
260 free(periph, M_CAMPERIPH);
263 /* No cleanup to perform. */
266 panic("cam_periph_alloc: Unkown init level");
272 * Find a peripheral structure with the specified path, target, lun,
273 * and (optionally) type. If the name is NULL, this function will return
274 * the first peripheral driver that matches the specified path.
277 cam_periph_find(struct cam_path *path, char *name)
279 struct periph_driver **p_drv;
280 struct cam_periph *periph;
283 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
285 if (name != NULL && (strcmp((*p_drv)->driver_name, name) != 0))
288 TAILQ_FOREACH(periph, &(*p_drv)->units, unit_links) {
289 if (xpt_path_comp(periph->path, path) == 0) {
304 cam_periph_acquire(struct cam_periph *periph)
308 return(CAM_REQ_CMP_ERR);
318 cam_periph_release_locked(struct cam_periph *periph)
325 if ((--periph->refcount == 0)
326 && (periph->flags & CAM_PERIPH_INVALID)) {
327 camperiphfree(periph);
333 cam_periph_release(struct cam_periph *periph)
341 mtx_assert(sim->mtx, MA_NOTOWNED);
343 cam_periph_release_locked(periph);
344 mtx_unlock(sim->mtx);
348 cam_periph_hold(struct cam_periph *periph, int priority)
353 * Increment the reference count on the peripheral
354 * while we wait for our lock attempt to succeed
355 * to ensure the peripheral doesn't disappear out
356 * from user us while we sleep.
359 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
362 mtx_assert(periph->sim->mtx, MA_OWNED);
363 while ((periph->flags & CAM_PERIPH_LOCKED) != 0) {
364 periph->flags |= CAM_PERIPH_LOCK_WANTED;
365 if ((error = mtx_sleep(periph, periph->sim->mtx, priority,
366 "caplck", 0)) != 0) {
367 cam_periph_release_locked(periph);
372 periph->flags |= CAM_PERIPH_LOCKED;
377 cam_periph_unhold(struct cam_periph *periph)
380 mtx_assert(periph->sim->mtx, MA_OWNED);
382 periph->flags &= ~CAM_PERIPH_LOCKED;
383 if ((periph->flags & CAM_PERIPH_LOCK_WANTED) != 0) {
384 periph->flags &= ~CAM_PERIPH_LOCK_WANTED;
388 cam_periph_release_locked(periph);
392 * Look for the next unit number that is not currently in use for this
393 * peripheral type starting at "newunit". Also exclude unit numbers that
394 * are reserved by for future "hardwiring" unless we already know that this
395 * is a potential wired device. Only assume that the device is "wired" the
396 * first time through the loop since after that we'll be looking at unit
397 * numbers that did not match a wiring entry.
400 camperiphnextunit(struct periph_driver *p_drv, u_int newunit, int wired,
401 path_id_t pathid, target_id_t target, lun_id_t lun)
403 struct cam_periph *periph;
405 int i, val, dunit, r;
406 const char *dname, *strval;
408 periph_name = p_drv->driver_name;
411 for (periph = TAILQ_FIRST(&p_drv->units);
412 periph != NULL && periph->unit_number != newunit;
413 periph = TAILQ_NEXT(periph, unit_links))
416 if (periph != NULL && periph->unit_number == newunit) {
418 xpt_print(periph->path, "Duplicate Wired "
420 xpt_print(periph->path, "Second device (%s "
421 "device at scbus%d target %d lun %d) will "
422 "not be wired\n", periph_name, pathid,
432 * Don't match entries like "da 4" as a wired down
433 * device, but do match entries like "da 4 target 5"
434 * or even "da 4 scbus 1".
439 r = resource_find_dev(&i, dname, &dunit, NULL, NULL);
442 /* if no "target" and no specific scbus, skip */
443 if (resource_int_value(dname, dunit, "target", &val) &&
444 (resource_string_value(dname, dunit, "at",&strval)||
445 strcmp(strval, "scbus") == 0))
447 if (newunit == dunit)
457 camperiphunit(struct periph_driver *p_drv, path_id_t pathid,
458 target_id_t target, lun_id_t lun)
461 int wired, i, val, dunit;
462 const char *dname, *strval;
463 char pathbuf[32], *periph_name;
465 periph_name = p_drv->driver_name;
466 snprintf(pathbuf, sizeof(pathbuf), "scbus%d", pathid);
470 for (wired = 0; resource_find_dev(&i, dname, &dunit, NULL, NULL) == 0;
472 if (resource_string_value(dname, dunit, "at", &strval) == 0) {
473 if (strcmp(strval, pathbuf) != 0)
477 if (resource_int_value(dname, dunit, "target", &val) == 0) {
482 if (resource_int_value(dname, dunit, "lun", &val) == 0) {
494 * Either start from 0 looking for the next unit or from
495 * the unit number given in the resource config. This way,
496 * if we have wildcard matches, we don't return the same
499 unit = camperiphnextunit(p_drv, unit, wired, pathid, target, lun);
505 cam_periph_invalidate(struct cam_periph *periph)
509 * We only call this routine the first time a peripheral is
512 if (((periph->flags & CAM_PERIPH_INVALID) == 0)
513 && (periph->periph_oninval != NULL))
514 periph->periph_oninval(periph);
516 periph->flags |= CAM_PERIPH_INVALID;
517 periph->flags &= ~CAM_PERIPH_NEW_DEV_FOUND;
520 if (periph->refcount == 0)
521 camperiphfree(periph);
522 else if (periph->refcount < 0)
523 printf("cam_invalidate_periph: refcount < 0!!\n");
528 camperiphfree(struct cam_periph *periph)
530 struct periph_driver **p_drv;
532 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
533 if (strcmp((*p_drv)->driver_name, periph->periph_name) == 0)
536 if (*p_drv == NULL) {
537 printf("camperiphfree: attempt to free non-existant periph\n");
541 TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
542 (*p_drv)->generation++;
545 if (periph->periph_dtor != NULL)
546 periph->periph_dtor(periph);
547 xpt_remove_periph(periph);
549 if (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) {
553 switch (periph->deferred_ac) {
554 case AC_FOUND_DEVICE:
555 ccb.ccb_h.func_code = XPT_GDEV_TYPE;
556 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
560 case AC_PATH_REGISTERED:
561 ccb.ccb_h.func_code = XPT_PATH_INQ;
562 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
570 periph->deferred_callback(NULL, periph->deferred_ac,
573 xpt_free_path(periph->path);
574 free(periph, M_CAMPERIPH);
579 * Map user virtual pointers into kernel virtual address space, so we can
580 * access the memory. This won't work on physical pointers, for now it's
581 * up to the caller to check for that. (XXX KDM -- should we do that here
582 * instead?) This also only works for up to MAXPHYS memory. Since we use
583 * buffers to map stuff in and out, we're limited to the buffer size.
586 cam_periph_mapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
589 int flags[CAM_PERIPH_MAXMAPS];
590 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
591 u_int32_t lengths[CAM_PERIPH_MAXMAPS];
592 u_int32_t dirs[CAM_PERIPH_MAXMAPS];
593 /* Some controllers may not be able to handle more data. */
594 size_t maxmap = DFLTPHYS;
596 switch(ccb->ccb_h.func_code) {
598 if (ccb->cdm.match_buf_len == 0) {
599 printf("cam_periph_mapmem: invalid match buffer "
603 if (ccb->cdm.pattern_buf_len > 0) {
604 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
605 lengths[0] = ccb->cdm.pattern_buf_len;
606 dirs[0] = CAM_DIR_OUT;
607 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
608 lengths[1] = ccb->cdm.match_buf_len;
609 dirs[1] = CAM_DIR_IN;
612 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
613 lengths[0] = ccb->cdm.match_buf_len;
614 dirs[0] = CAM_DIR_IN;
618 * This request will not go to the hardware, no reason
619 * to be so strict. vmapbuf() is able to map up to MAXPHYS.
624 case XPT_CONT_TARGET_IO:
625 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
628 data_ptrs[0] = &ccb->csio.data_ptr;
629 lengths[0] = ccb->csio.dxfer_len;
630 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
634 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
637 data_ptrs[0] = &ccb->ataio.data_ptr;
638 lengths[0] = ccb->ataio.dxfer_len;
639 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
644 break; /* NOTREACHED */
648 * Check the transfer length and permissions first, so we don't
649 * have to unmap any previously mapped buffers.
651 for (i = 0; i < numbufs; i++) {
656 * The userland data pointer passed in may not be page
657 * aligned. vmapbuf() truncates the address to a page
658 * boundary, so if the address isn't page aligned, we'll
659 * need enough space for the given transfer length, plus
660 * whatever extra space is necessary to make it to the page
664 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)) > maxmap){
665 printf("cam_periph_mapmem: attempt to map %lu bytes, "
666 "which is greater than %lu\n",
668 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)),
673 if (dirs[i] & CAM_DIR_OUT) {
674 flags[i] = BIO_WRITE;
677 if (dirs[i] & CAM_DIR_IN) {
683 /* this keeps the current process from getting swapped */
685 * XXX KDM should I use P_NOSWAP instead?
689 for (i = 0; i < numbufs; i++) {
693 mapinfo->bp[i] = getpbuf(NULL);
695 /* save the buffer's data address */
696 mapinfo->bp[i]->b_saveaddr = mapinfo->bp[i]->b_data;
698 /* put our pointer in the data slot */
699 mapinfo->bp[i]->b_data = *data_ptrs[i];
701 /* set the transfer length, we know it's < MAXPHYS */
702 mapinfo->bp[i]->b_bufsize = lengths[i];
704 /* set the direction */
705 mapinfo->bp[i]->b_iocmd = flags[i];
708 * Map the buffer into kernel memory.
710 * Note that useracc() alone is not a sufficient test.
711 * vmapbuf() can still fail due to a smaller file mapped
712 * into a larger area of VM, or if userland races against
713 * vmapbuf() after the useracc() check.
715 if (vmapbuf(mapinfo->bp[i]) < 0) {
716 for (j = 0; j < i; ++j) {
717 *data_ptrs[j] = mapinfo->bp[j]->b_saveaddr;
718 vunmapbuf(mapinfo->bp[j]);
719 relpbuf(mapinfo->bp[j], NULL);
721 relpbuf(mapinfo->bp[i], NULL);
726 /* set our pointer to the new mapped area */
727 *data_ptrs[i] = mapinfo->bp[i]->b_data;
729 mapinfo->num_bufs_used++;
733 * Now that we've gotten this far, change ownership to the kernel
734 * of the buffers so that we don't run afoul of returning to user
735 * space with locks (on the buffer) held.
737 for (i = 0; i < numbufs; i++) {
738 BUF_KERNPROC(mapinfo->bp[i]);
746 * Unmap memory segments mapped into kernel virtual address space by
747 * cam_periph_mapmem().
750 cam_periph_unmapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
753 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
755 if (mapinfo->num_bufs_used <= 0) {
756 /* allow ourselves to be swapped once again */
761 switch (ccb->ccb_h.func_code) {
763 numbufs = min(mapinfo->num_bufs_used, 2);
766 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
768 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
769 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
773 case XPT_CONT_TARGET_IO:
774 data_ptrs[0] = &ccb->csio.data_ptr;
775 numbufs = min(mapinfo->num_bufs_used, 1);
778 data_ptrs[0] = &ccb->ataio.data_ptr;
779 numbufs = min(mapinfo->num_bufs_used, 1);
782 /* allow ourselves to be swapped once again */
785 break; /* NOTREACHED */
788 for (i = 0; i < numbufs; i++) {
789 /* Set the user's pointer back to the original value */
790 *data_ptrs[i] = mapinfo->bp[i]->b_saveaddr;
792 /* unmap the buffer */
793 vunmapbuf(mapinfo->bp[i]);
795 /* release the buffer */
796 relpbuf(mapinfo->bp[i], NULL);
799 /* allow ourselves to be swapped once again */
804 cam_periph_getccb(struct cam_periph *periph, u_int32_t priority)
806 struct ccb_hdr *ccb_h;
808 mtx_assert(periph->sim->mtx, MA_OWNED);
809 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdgetccb\n"));
811 while (SLIST_FIRST(&periph->ccb_list) == NULL) {
812 if (periph->immediate_priority > priority)
813 periph->immediate_priority = priority;
814 xpt_schedule(periph, priority);
815 if ((SLIST_FIRST(&periph->ccb_list) != NULL)
816 && (SLIST_FIRST(&periph->ccb_list)->pinfo.priority == priority))
818 mtx_assert(periph->sim->mtx, MA_OWNED);
819 mtx_sleep(&periph->ccb_list, periph->sim->mtx, PRIBIO, "cgticb",
823 ccb_h = SLIST_FIRST(&periph->ccb_list);
824 SLIST_REMOVE_HEAD(&periph->ccb_list, periph_links.sle);
825 return ((union ccb *)ccb_h);
829 cam_periph_ccbwait(union ccb *ccb)
833 sim = xpt_path_sim(ccb->ccb_h.path);
834 if ((ccb->ccb_h.pinfo.index != CAM_UNQUEUED_INDEX)
835 || ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG))
836 mtx_sleep(&ccb->ccb_h.cbfcnp, sim->mtx, PRIBIO, "cbwait", 0);
840 cam_periph_ioctl(struct cam_periph *periph, u_long cmd, caddr_t addr,
841 int (*error_routine)(union ccb *ccb,
843 u_int32_t sense_flags))
853 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
854 xpt_setup_ccb(&ccb->ccb_h,
856 CAM_PRIORITY_NORMAL);
857 ccb->ccb_h.func_code = XPT_GDEVLIST;
860 * Basically, the point of this is that we go through
861 * getting the list of devices, until we find a passthrough
862 * device. In the current version of the CAM code, the
863 * only way to determine what type of device we're dealing
864 * with is by its name.
868 ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS;
869 while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) {
871 /* we want the next device in the list */
873 if (strncmp(ccb->cgdl.periph_name,
879 if ((ccb->cgdl.status == CAM_GDEVLIST_LAST_DEVICE) &&
881 ccb->cgdl.periph_name[0] = '\0';
882 ccb->cgdl.unit_number = 0;
887 /* copy the result back out */
888 bcopy(ccb, addr, sizeof(union ccb));
890 /* and release the ccb */
891 xpt_release_ccb(ccb);
902 cam_periph_runccb(union ccb *ccb,
903 int (*error_routine)(union ccb *ccb,
905 u_int32_t sense_flags),
906 cam_flags camflags, u_int32_t sense_flags,
913 sim = xpt_path_sim(ccb->ccb_h.path);
914 mtx_assert(sim->mtx, MA_OWNED);
917 * If the user has supplied a stats structure, and if we understand
918 * this particular type of ccb, record the transaction start.
920 if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO ||
921 ccb->ccb_h.func_code == XPT_ATA_IO))
922 devstat_start_transaction(ds, NULL);
927 cam_periph_ccbwait(ccb);
928 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
930 else if (error_routine != NULL)
931 error = (*error_routine)(ccb, camflags, sense_flags);
935 } while (error == ERESTART);
937 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
938 cam_release_devq(ccb->ccb_h.path,
942 /* getcount_only */ FALSE);
943 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
947 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
948 devstat_end_transaction(ds,
950 ccb->csio.tag_action & 0x3,
951 ((ccb->ccb_h.flags & CAM_DIR_MASK) ==
952 CAM_DIR_NONE) ? DEVSTAT_NO_DATA :
953 (ccb->ccb_h.flags & CAM_DIR_OUT) ?
955 DEVSTAT_READ, NULL, NULL);
956 } else if (ccb->ccb_h.func_code == XPT_ATA_IO) {
957 devstat_end_transaction(ds,
958 ccb->ataio.dxfer_len,
959 ccb->ataio.tag_action & 0x3,
960 ((ccb->ccb_h.flags & CAM_DIR_MASK) ==
961 CAM_DIR_NONE) ? DEVSTAT_NO_DATA :
962 (ccb->ccb_h.flags & CAM_DIR_OUT) ?
964 DEVSTAT_READ, NULL, NULL);
972 cam_freeze_devq(struct cam_path *path)
975 cam_freeze_devq_arg(path, 0, 0);
979 cam_freeze_devq_arg(struct cam_path *path, uint32_t flags, uint32_t arg)
981 struct ccb_relsim crs;
983 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NONE);
984 crs.ccb_h.func_code = XPT_FREEZE_QUEUE;
985 crs.release_flags = flags;
987 crs.release_timeout = arg;
988 xpt_action((union ccb *)&crs);
992 cam_release_devq(struct cam_path *path, u_int32_t relsim_flags,
993 u_int32_t openings, u_int32_t arg,
996 struct ccb_relsim crs;
998 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
999 crs.ccb_h.func_code = XPT_REL_SIMQ;
1000 crs.ccb_h.flags = getcount_only ? CAM_DEV_QFREEZE : 0;
1001 crs.release_flags = relsim_flags;
1002 crs.openings = openings;
1003 crs.release_timeout = arg;
1004 xpt_action((union ccb *)&crs);
1005 return (crs.qfrozen_cnt);
1008 #define saved_ccb_ptr ppriv_ptr0
1009 #define recovery_depth ppriv_field1
1011 camperiphsensedone(struct cam_periph *periph, union ccb *done_ccb)
1013 union ccb *saved_ccb = (union ccb *)done_ccb->ccb_h.saved_ccb_ptr;
1017 int depth = done_ccb->ccb_h.recovery_depth;
1019 status = done_ccb->ccb_h.status;
1020 if (status & CAM_DEV_QFRZN) {
1023 * Clear freeze flag now for case of retry,
1024 * freeze will be dropped later.
1026 done_ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1028 status &= CAM_STATUS_MASK;
1033 * If we manually retrieved sense into a CCB and got
1034 * something other than "NO SENSE" send the updated CCB
1035 * back to the client via xpt_done() to be processed via
1036 * the error recovery code again.
1038 sense_key = saved_ccb->csio.sense_data.flags;
1039 sense_key &= SSD_KEY;
1040 if (sense_key != SSD_KEY_NO_SENSE) {
1041 saved_ccb->ccb_h.status |=
1044 saved_ccb->ccb_h.status &=
1046 saved_ccb->ccb_h.status |=
1049 bcopy(saved_ccb, done_ccb, sizeof(union ccb));
1050 xpt_free_ccb(saved_ccb);
1054 bcopy(saved_ccb, done_ccb, sizeof(union ccb));
1055 xpt_free_ccb(saved_ccb);
1056 done_ccb->ccb_h.status &= ~CAM_STATUS_MASK;
1057 done_ccb->ccb_h.status |= CAM_AUTOSENSE_FAIL;
1060 periph->flags &= ~CAM_PERIPH_SENSE_INPROG;
1062 * If it is the end of recovery, drop freeze, taken due to
1063 * CAM_DEV_QFREEZE flag, set on recovery request.
1066 cam_release_devq(done_ccb->ccb_h.path,
1070 /*getcount_only*/0);
1073 * Copy frozen flag from recovery request if it is set there
1077 done_ccb->ccb_h.status |= CAM_DEV_QFRZN;
1078 (*done_ccb->ccb_h.cbfcnp)(periph, done_ccb);
1082 camperiphdone(struct cam_periph *periph, union ccb *done_ccb)
1084 union ccb *saved_ccb, *save_ccb;
1087 struct scsi_start_stop_unit *scsi_cmd;
1088 u_int32_t relsim_flags, timeout;
1090 status = done_ccb->ccb_h.status;
1091 if (status & CAM_DEV_QFRZN) {
1094 * Clear freeze flag now for case of retry,
1095 * freeze will be dropped later.
1097 done_ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1102 saved_ccb = (union ccb *)done_ccb->ccb_h.saved_ccb_ptr;
1104 switch (status & CAM_STATUS_MASK) {
1108 * If we have successfully taken a device from the not
1109 * ready to ready state, re-scan the device and re-get
1110 * the inquiry information. Many devices (mostly disks)
1111 * don't properly report their inquiry information unless
1114 scsi_cmd = (struct scsi_start_stop_unit *)
1115 &done_ccb->csio.cdb_io.cdb_bytes;
1117 if (scsi_cmd->opcode == START_STOP_UNIT)
1118 xpt_async(AC_INQ_CHANGED,
1119 done_ccb->ccb_h.path, NULL);
1122 case CAM_SCSI_STATUS_ERROR:
1123 scsi_cmd = (struct scsi_start_stop_unit *)
1124 &done_ccb->csio.cdb_io.cdb_bytes;
1125 if (status & CAM_AUTOSNS_VALID) {
1126 struct ccb_getdev cgd;
1127 struct scsi_sense_data *sense;
1128 int error_code, sense_key, asc, ascq;
1129 scsi_sense_action err_action;
1131 sense = &done_ccb->csio.sense_data;
1132 scsi_extract_sense(sense, &error_code,
1133 &sense_key, &asc, &ascq);
1135 * Grab the inquiry data for this device.
1137 xpt_setup_ccb(&cgd.ccb_h, done_ccb->ccb_h.path,
1138 CAM_PRIORITY_NORMAL);
1139 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1140 xpt_action((union ccb *)&cgd);
1141 err_action = scsi_error_action(&done_ccb->csio,
1144 * If the error is "invalid field in CDB",
1145 * and the load/eject flag is set, turn the
1146 * flag off and try again. This is just in
1147 * case the drive in question barfs on the
1148 * load eject flag. The CAM code should set
1149 * the load/eject flag by default for
1153 * Should we check to see what the specific
1154 * scsi status is?? Or does it not matter
1155 * since we already know that there was an
1156 * error, and we know what the specific
1157 * error code was, and we know what the
1160 if ((scsi_cmd->opcode == START_STOP_UNIT) &&
1161 ((scsi_cmd->how & SSS_LOEJ) != 0) &&
1162 (asc == 0x24) && (ascq == 0x00) &&
1163 (done_ccb->ccb_h.retry_count > 0)) {
1165 scsi_cmd->how &= ~SSS_LOEJ;
1166 xpt_action(done_ccb);
1167 } else if ((done_ccb->ccb_h.retry_count > 1)
1168 && ((err_action & SS_MASK) != SS_FAIL)) {
1171 * In this case, the error recovery
1172 * command failed, but we've got
1173 * some retries left on it. Give
1174 * it another try unless this is an
1175 * unretryable error.
1177 /* set the timeout to .5 sec */
1179 RELSIM_RELEASE_AFTER_TIMEOUT;
1181 xpt_action(done_ccb);
1185 * Perform the final retry with the original
1186 * CCB so that final error processing is
1187 * performed by the owner of the CCB.
1192 save_ccb = xpt_alloc_ccb_nowait();
1193 if (save_ccb == NULL)
1195 bcopy(done_ccb, save_ccb, sizeof(*save_ccb));
1196 periph->flags |= CAM_PERIPH_SENSE_INPROG;
1198 * Send a Request Sense to the device. We
1199 * assume that we are in a contingent allegiance
1200 * condition so we do not tag this request.
1202 scsi_request_sense(&done_ccb->csio, /*retries*/1,
1204 &save_ccb->csio.sense_data,
1205 sizeof(save_ccb->csio.sense_data),
1206 CAM_TAG_ACTION_NONE,
1207 /*sense_len*/SSD_FULL_SIZE,
1209 done_ccb->ccb_h.pinfo.priority--;
1210 done_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1211 done_ccb->ccb_h.saved_ccb_ptr = save_ccb;
1212 done_ccb->ccb_h.recovery_depth++;
1213 xpt_action(done_ccb);
1218 bcopy(saved_ccb, done_ccb, sizeof(*done_ccb));
1219 xpt_free_ccb(saved_ccb);
1220 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1221 xpt_action(done_ccb);
1225 /* decrement the retry count */
1227 * XXX This isn't appropriate in all cases. Restructure,
1228 * so that the retry count is only decremented on an
1229 * actual retry. Remeber that the orignal ccb had its
1230 * retry count dropped before entering recovery, so
1231 * doing it again is a bug.
1233 if (done_ccb->ccb_h.retry_count > 0)
1234 done_ccb->ccb_h.retry_count--;
1236 * Drop freeze taken due to CAM_DEV_QFREEZE flag set on recovery
1239 cam_release_devq(done_ccb->ccb_h.path,
1240 /*relsim_flags*/relsim_flags,
1243 /*getcount_only*/0);
1244 /* Drop freeze taken, if this recovery request got error. */
1246 cam_release_devq(done_ccb->ccb_h.path,
1250 /*getcount_only*/0);
1255 * Generic Async Event handler. Peripheral drivers usually
1256 * filter out the events that require personal attention,
1257 * and leave the rest to this function.
1260 cam_periph_async(struct cam_periph *periph, u_int32_t code,
1261 struct cam_path *path, void *arg)
1264 case AC_LOST_DEVICE:
1265 cam_periph_invalidate(periph);
1273 cam_periph_bus_settle(struct cam_periph *periph, u_int bus_settle)
1275 struct ccb_getdevstats cgds;
1277 xpt_setup_ccb(&cgds.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1278 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1279 xpt_action((union ccb *)&cgds);
1280 cam_periph_freeze_after_event(periph, &cgds.last_reset, bus_settle);
1284 cam_periph_freeze_after_event(struct cam_periph *periph,
1285 struct timeval* event_time, u_int duration_ms)
1287 struct timeval delta;
1288 struct timeval duration_tv;
1291 timevalsub(&delta, event_time);
1292 duration_tv.tv_sec = duration_ms / 1000;
1293 duration_tv.tv_usec = (duration_ms % 1000) * 1000;
1294 if (timevalcmp(&delta, &duration_tv, <)) {
1295 timevalsub(&duration_tv, &delta);
1297 duration_ms = duration_tv.tv_sec * 1000;
1298 duration_ms += duration_tv.tv_usec / 1000;
1299 cam_freeze_devq(periph->path);
1300 cam_release_devq(periph->path,
1301 RELSIM_RELEASE_AFTER_TIMEOUT,
1303 /*timeout*/duration_ms,
1304 /*getcount_only*/0);
1310 camperiphscsistatuserror(union ccb *ccb, cam_flags camflags,
1311 u_int32_t sense_flags,
1312 int *openings, u_int32_t *relsim_flags,
1313 u_int32_t *timeout, const char **action_string)
1317 switch (ccb->csio.scsi_status) {
1318 case SCSI_STATUS_OK:
1319 case SCSI_STATUS_COND_MET:
1320 case SCSI_STATUS_INTERMED:
1321 case SCSI_STATUS_INTERMED_COND_MET:
1324 case SCSI_STATUS_CMD_TERMINATED:
1325 case SCSI_STATUS_CHECK_COND:
1327 xpt_print(ccb->ccb_h.path, "SCSI status error\n");
1328 error = camperiphscsisenseerror(ccb,
1336 case SCSI_STATUS_QUEUE_FULL:
1339 struct ccb_getdevstats cgds;
1342 * First off, find out what the current
1343 * transaction counts are.
1345 xpt_setup_ccb(&cgds.ccb_h,
1347 CAM_PRIORITY_NORMAL);
1348 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1349 xpt_action((union ccb *)&cgds);
1352 * If we were the only transaction active, treat
1353 * the QUEUE FULL as if it were a BUSY condition.
1355 if (cgds.dev_active != 0) {
1359 * Reduce the number of openings to
1360 * be 1 less than the amount it took
1361 * to get a queue full bounded by the
1362 * minimum allowed tag count for this
1365 total_openings = cgds.dev_active + cgds.dev_openings;
1366 *openings = cgds.dev_active;
1367 if (*openings < cgds.mintags)
1368 *openings = cgds.mintags;
1369 if (*openings < total_openings)
1370 *relsim_flags = RELSIM_ADJUST_OPENINGS;
1373 * Some devices report queue full for
1374 * temporary resource shortages. For
1375 * this reason, we allow a minimum
1376 * tag count to be entered via a
1377 * quirk entry to prevent the queue
1378 * count on these devices from falling
1379 * to a pessimisticly low value. We
1380 * still wait for the next successful
1381 * completion, however, before queueing
1382 * more transactions to the device.
1384 *relsim_flags = RELSIM_RELEASE_AFTER_CMDCMPLT;
1389 xpt_print(ccb->ccb_h.path, "Queue full\n");
1395 case SCSI_STATUS_BUSY:
1397 * Restart the queue after either another
1398 * command completes or a 1 second timeout.
1401 xpt_print(ccb->ccb_h.path, "Device busy\n");
1403 if (ccb->ccb_h.retry_count > 0) {
1404 ccb->ccb_h.retry_count--;
1406 *relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT
1407 | RELSIM_RELEASE_AFTER_CMDCMPLT;
1413 case SCSI_STATUS_RESERV_CONFLICT:
1414 xpt_print(ccb->ccb_h.path, "Reservation conflict\n");
1418 xpt_print(ccb->ccb_h.path, "SCSI status 0x%x\n",
1419 ccb->csio.scsi_status);
1427 camperiphscsisenseerror(union ccb *ccb, cam_flags camflags,
1428 u_int32_t sense_flags,
1429 int *openings, u_int32_t *relsim_flags,
1430 u_int32_t *timeout, const char **action_string)
1432 struct cam_periph *periph;
1433 union ccb *orig_ccb = ccb;
1436 periph = xpt_path_periph(ccb->ccb_h.path);
1438 (CAM_PERIPH_RECOVERY_INPROG | CAM_PERIPH_SENSE_INPROG)) {
1440 * If error recovery is already in progress, don't attempt
1441 * to process this error, but requeue it unconditionally
1442 * and attempt to process it once error recovery has
1443 * completed. This failed command is probably related to
1444 * the error that caused the currently active error recovery
1445 * action so our current recovery efforts should also
1446 * address this command. Be aware that the error recovery
1447 * code assumes that only one recovery action is in progress
1448 * on a particular peripheral instance at any given time
1449 * (e.g. only one saved CCB for error recovery) so it is
1450 * imperitive that we don't violate this assumption.
1454 scsi_sense_action err_action;
1455 struct ccb_getdev cgd;
1458 * Grab the inquiry data for this device.
1460 xpt_setup_ccb(&cgd.ccb_h, ccb->ccb_h.path, CAM_PRIORITY_NORMAL);
1461 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1462 xpt_action((union ccb *)&cgd);
1464 if ((ccb->ccb_h.status & CAM_AUTOSNS_VALID) != 0)
1465 err_action = scsi_error_action(&ccb->csio,
1468 else if ((ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)
1469 err_action = SS_REQSENSE;
1471 err_action = SS_RETRY|SSQ_DECREMENT_COUNT|EIO;
1473 error = err_action & SS_ERRMASK;
1476 * If the recovery action will consume a retry,
1477 * make sure we actually have retries available.
1479 if ((err_action & SSQ_DECREMENT_COUNT) != 0) {
1480 if (ccb->ccb_h.retry_count > 0)
1481 ccb->ccb_h.retry_count--;
1483 *action_string = "Retries exhausted";
1484 goto sense_error_done;
1488 if ((err_action & SS_MASK) >= SS_START) {
1490 * Do common portions of commands that
1491 * use recovery CCBs.
1493 orig_ccb = xpt_alloc_ccb_nowait();
1494 if (orig_ccb == NULL) {
1495 *action_string = "Can't allocate recovery CCB";
1496 goto sense_error_done;
1499 * Clear freeze flag for original request here, as
1500 * this freeze will be dropped as part of ERESTART.
1502 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1503 bcopy(ccb, orig_ccb, sizeof(*orig_ccb));
1506 switch (err_action & SS_MASK) {
1508 *action_string = "No recovery action needed";
1512 *action_string = "Retrying command (per sense data)";
1516 *action_string = "Unretryable error";
1523 * Send a start unit command to the device, and
1524 * then retry the command.
1526 *action_string = "Attempting to start unit";
1527 periph->flags |= CAM_PERIPH_RECOVERY_INPROG;
1530 * Check for removable media and set
1531 * load/eject flag appropriately.
1533 if (SID_IS_REMOVABLE(&cgd.inq_data))
1538 scsi_start_stop(&ccb->csio,
1552 * Send a Test Unit Ready to the device.
1553 * If the 'many' flag is set, we send 120
1554 * test unit ready commands, one every half
1555 * second. Otherwise, we just send one TUR.
1556 * We only want to do this if the retry
1557 * count has not been exhausted.
1561 if ((err_action & SSQ_MANY) != 0) {
1562 *action_string = "Polling device for readiness";
1565 *action_string = "Testing device for readiness";
1568 periph->flags |= CAM_PERIPH_RECOVERY_INPROG;
1569 scsi_test_unit_ready(&ccb->csio,
1577 * Accomplish our 500ms delay by deferring
1578 * the release of our device queue appropriately.
1580 *relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1586 *action_string = "Requesting SCSI sense data";
1587 periph->flags |= CAM_PERIPH_SENSE_INPROG;
1589 * Send a Request Sense to the device. We
1590 * assume that we are in a contingent allegiance
1591 * condition so we do not tag this request.
1593 scsi_request_sense(&ccb->csio, /*retries*/1,
1595 &orig_ccb->csio.sense_data,
1596 sizeof(orig_ccb->csio.sense_data),
1597 CAM_TAG_ACTION_NONE,
1598 /*sense_len*/SSD_FULL_SIZE,
1603 panic("Unhandled error action %x", err_action);
1606 if ((err_action & SS_MASK) >= SS_START) {
1608 * Drop the priority, so that the recovery
1609 * CCB is the first to execute. Freeze the queue
1610 * after this command is sent so that we can
1611 * restore the old csio and have it queued in
1612 * the proper order before we release normal
1613 * transactions to the device.
1615 ccb->ccb_h.pinfo.priority--;
1616 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1617 ccb->ccb_h.saved_ccb_ptr = orig_ccb;
1618 ccb->ccb_h.recovery_depth = 0;
1623 if ((err_action & SSQ_PRINT_SENSE) != 0
1624 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID) != 0)
1625 cam_error_print(orig_ccb, CAM_ESF_ALL, CAM_EPF_ALL);
1631 * Generic error handler. Peripheral drivers usually filter
1632 * out the errors that they handle in a unique mannor, then
1633 * call this function.
1636 cam_periph_error(union ccb *ccb, cam_flags camflags,
1637 u_int32_t sense_flags, union ccb *save_ccb)
1639 const char *action_string;
1642 int error, printed = 0;
1644 u_int32_t relsim_flags;
1645 u_int32_t timeout = 0;
1647 action_string = NULL;
1648 status = ccb->ccb_h.status;
1649 frozen = (status & CAM_DEV_QFRZN) != 0;
1650 status &= CAM_STATUS_MASK;
1651 openings = relsim_flags = 0;
1657 case CAM_SCSI_STATUS_ERROR:
1658 error = camperiphscsistatuserror(ccb,
1666 case CAM_AUTOSENSE_FAIL:
1667 xpt_print(ccb->ccb_h.path, "AutoSense failed\n");
1668 error = EIO; /* we have to kill the command */
1670 case CAM_ATA_STATUS_ERROR:
1671 if (bootverbose && printed == 0) {
1672 xpt_print(ccb->ccb_h.path, "ATA status error\n");
1673 cam_error_print(ccb, CAM_ESF_ALL, CAM_EPF_ALL);
1677 case CAM_REQ_CMP_ERR:
1678 if (bootverbose && printed == 0) {
1679 xpt_print(ccb->ccb_h.path,
1680 "Request completed with CAM_REQ_CMP_ERR\n");
1684 case CAM_CMD_TIMEOUT:
1685 if (bootverbose && printed == 0) {
1686 xpt_print(ccb->ccb_h.path, "Command timed out\n");
1690 case CAM_UNEXP_BUSFREE:
1691 if (bootverbose && printed == 0) {
1692 xpt_print(ccb->ccb_h.path, "Unexpected Bus Free\n");
1696 case CAM_UNCOR_PARITY:
1697 if (bootverbose && printed == 0) {
1698 xpt_print(ccb->ccb_h.path,
1699 "Uncorrected parity error\n");
1703 case CAM_DATA_RUN_ERR:
1704 if (bootverbose && printed == 0) {
1705 xpt_print(ccb->ccb_h.path, "Data overrun\n");
1708 error = EIO; /* we have to kill the command */
1709 /* decrement the number of retries */
1710 if (ccb->ccb_h.retry_count > 0) {
1711 ccb->ccb_h.retry_count--;
1714 action_string = "Retries exhausted";
1720 case CAM_MSG_REJECT_REC:
1721 /* XXX Don't know that these are correct */
1724 case CAM_SEL_TIMEOUT:
1726 struct cam_path *newpath;
1728 if ((camflags & CAM_RETRY_SELTO) != 0) {
1729 if (ccb->ccb_h.retry_count > 0) {
1731 ccb->ccb_h.retry_count--;
1733 if (bootverbose && printed == 0) {
1734 xpt_print(ccb->ccb_h.path,
1735 "Selection timeout\n");
1740 * Wait a bit to give the device
1741 * time to recover before we try again.
1743 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1744 timeout = periph_selto_delay;
1749 /* Should we do more if we can't create the path?? */
1750 if (xpt_create_path(&newpath, xpt_path_periph(ccb->ccb_h.path),
1751 xpt_path_path_id(ccb->ccb_h.path),
1752 xpt_path_target_id(ccb->ccb_h.path),
1753 CAM_LUN_WILDCARD) != CAM_REQ_CMP)
1757 * Let peripheral drivers know that this device has gone
1760 xpt_async(AC_LOST_DEVICE, newpath, NULL);
1761 xpt_free_path(newpath);
1764 case CAM_REQ_INVALID:
1765 case CAM_PATH_INVALID:
1766 case CAM_DEV_NOT_THERE:
1768 case CAM_PROVIDE_FAIL:
1769 case CAM_REQ_TOO_BIG:
1770 case CAM_LUN_INVALID:
1771 case CAM_TID_INVALID:
1774 case CAM_SCSI_BUS_RESET:
1777 * Commands that repeatedly timeout and cause these
1778 * kinds of error recovery actions, should return
1779 * CAM_CMD_TIMEOUT, which allows us to safely assume
1780 * that this command was an innocent bystander to
1781 * these events and should be unconditionally
1784 if (bootverbose && printed == 0) {
1785 xpt_print_path(ccb->ccb_h.path);
1786 if (status == CAM_BDR_SENT)
1787 printf("Bus Device Reset sent\n");
1789 printf("Bus Reset issued\n");
1793 case CAM_REQUEUE_REQ:
1794 /* Unconditional requeue */
1796 if (bootverbose && printed == 0) {
1797 xpt_print(ccb->ccb_h.path, "Request requeued\n");
1801 case CAM_RESRC_UNAVAIL:
1802 /* Wait a bit for the resource shortage to abate. */
1803 timeout = periph_noresrc_delay;
1807 /* Wait a bit for the busy condition to abate. */
1808 timeout = periph_busy_delay;
1810 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1813 /* decrement the number of retries */
1814 if (ccb->ccb_h.retry_count > 0) {
1815 ccb->ccb_h.retry_count--;
1817 if (bootverbose && printed == 0) {
1818 xpt_print(ccb->ccb_h.path, "CAM status 0x%x\n",
1824 action_string = "Retries exhausted";
1830 * If we have and error and are booting verbosely, whine
1831 * *unless* this was a non-retryable selection timeout.
1833 if (error != 0 && bootverbose &&
1834 !(status == CAM_SEL_TIMEOUT && (camflags & CAM_RETRY_SELTO) == 0)) {
1835 if (error != ERESTART) {
1836 if (action_string == NULL)
1837 action_string = "Unretryable error";
1838 xpt_print(ccb->ccb_h.path, "Error %d, %s\n",
1839 error, action_string);
1840 } else if (action_string != NULL)
1841 xpt_print(ccb->ccb_h.path, "%s\n", action_string);
1843 xpt_print(ccb->ccb_h.path, "Retrying command\n");
1846 /* Attempt a retry */
1847 if (error == ERESTART || error == 0) {
1849 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1850 if (error == ERESTART)
1853 cam_release_devq(ccb->ccb_h.path,
1857 /*getcount_only*/0);