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>
46 #include <vm/vm_extern.h>
49 #include <cam/cam_ccb.h>
50 #include <cam/cam_queue.h>
51 #include <cam/cam_xpt_periph.h>
52 #include <cam/cam_periph.h>
53 #include <cam/cam_debug.h>
54 #include <cam/cam_sim.h>
56 #include <cam/scsi/scsi_all.h>
57 #include <cam/scsi/scsi_message.h>
58 #include <cam/scsi/scsi_pass.h>
60 static u_int camperiphnextunit(struct periph_driver *p_drv,
61 u_int newunit, int wired,
62 path_id_t pathid, target_id_t target,
64 static u_int camperiphunit(struct periph_driver *p_drv,
65 path_id_t pathid, target_id_t target,
67 static void camperiphdone(struct cam_periph *periph,
69 static void camperiphfree(struct cam_periph *periph);
70 static int camperiphscsistatuserror(union ccb *ccb,
72 u_int32_t sense_flags,
74 u_int32_t *relsim_flags,
76 const char **action_string);
77 static int camperiphscsisenseerror(union ccb *ccb,
79 u_int32_t sense_flags,
81 u_int32_t *relsim_flags,
83 const char **action_string);
85 static int nperiph_drivers;
86 static int initialized = 0;
87 struct periph_driver **periph_drivers;
89 MALLOC_DEFINE(M_CAMPERIPH, "CAM periph", "CAM peripheral buffers");
91 static int periph_selto_delay = 1000;
92 TUNABLE_INT("kern.cam.periph_selto_delay", &periph_selto_delay);
93 static int periph_noresrc_delay = 500;
94 TUNABLE_INT("kern.cam.periph_noresrc_delay", &periph_noresrc_delay);
95 static int periph_busy_delay = 500;
96 TUNABLE_INT("kern.cam.periph_busy_delay", &periph_busy_delay);
100 periphdriver_register(void *data)
102 struct periph_driver *drv = (struct periph_driver *)data;
103 struct periph_driver **newdrivers, **old;
106 ndrivers = nperiph_drivers + 2;
107 newdrivers = malloc(sizeof(*newdrivers) * ndrivers, M_CAMPERIPH,
110 bcopy(periph_drivers, newdrivers,
111 sizeof(*newdrivers) * nperiph_drivers);
112 newdrivers[nperiph_drivers] = drv;
113 newdrivers[nperiph_drivers + 1] = NULL;
114 old = periph_drivers;
115 periph_drivers = newdrivers;
117 free(old, M_CAMPERIPH);
119 /* If driver marked as early or it is late now, initialize it. */
120 if (((drv->flags & CAM_PERIPH_DRV_EARLY) != 0 && initialized > 0) ||
126 periphdriver_init(int level)
130 initialized = max(initialized, level);
131 for (i = 0; periph_drivers[i] != NULL; i++) {
132 early = (periph_drivers[i]->flags & CAM_PERIPH_DRV_EARLY) ? 1 : 2;
133 if (early == initialized)
134 (*periph_drivers[i]->init)();
139 cam_periph_alloc(periph_ctor_t *periph_ctor,
140 periph_oninv_t *periph_oninvalidate,
141 periph_dtor_t *periph_dtor, periph_start_t *periph_start,
142 char *name, cam_periph_type type, struct cam_path *path,
143 ac_callback_t *ac_callback, ac_code code, void *arg)
145 struct periph_driver **p_drv;
147 struct cam_periph *periph;
148 struct cam_periph *cur_periph;
150 target_id_t target_id;
157 * Handle Hot-Plug scenarios. If there is already a peripheral
158 * of our type assigned to this path, we are likely waiting for
159 * final close on an old, invalidated, peripheral. If this is
160 * the case, queue up a deferred call to the peripheral's async
161 * handler. If it looks like a mistaken re-allocation, complain.
163 if ((periph = cam_periph_find(path, name)) != NULL) {
165 if ((periph->flags & CAM_PERIPH_INVALID) != 0
166 && (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) == 0) {
167 periph->flags |= CAM_PERIPH_NEW_DEV_FOUND;
168 periph->deferred_callback = ac_callback;
169 periph->deferred_ac = code;
170 return (CAM_REQ_INPROG);
172 printf("cam_periph_alloc: attempt to re-allocate "
173 "valid device %s%d rejected flags %#x "
174 "refcount %d\n", periph->periph_name,
175 periph->unit_number, periph->flags,
178 return (CAM_REQ_INVALID);
181 periph = (struct cam_periph *)malloc(sizeof(*periph), M_CAMPERIPH,
185 return (CAM_RESRC_UNAVAIL);
190 sim = xpt_path_sim(path);
191 path_id = xpt_path_path_id(path);
192 target_id = xpt_path_target_id(path);
193 lun_id = xpt_path_lun_id(path);
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 cam_periph_acquire(struct cam_periph *periph)
312 status = CAM_REQ_CMP_ERR;
317 if ((periph->flags & CAM_PERIPH_INVALID) == 0) {
319 status = CAM_REQ_CMP;
327 cam_periph_release_locked_buses(struct cam_periph *periph)
329 if (periph->refcount != 0) {
332 panic("%s: release of %p when refcount is zero\n ", __func__,
335 if (periph->refcount == 0
336 && (periph->flags & CAM_PERIPH_INVALID)) {
337 camperiphfree(periph);
342 cam_periph_release_locked(struct cam_periph *periph)
349 cam_periph_release_locked_buses(periph);
354 cam_periph_release(struct cam_periph *periph)
362 mtx_assert(sim->mtx, MA_NOTOWNED);
364 cam_periph_release_locked(periph);
365 mtx_unlock(sim->mtx);
369 cam_periph_hold(struct cam_periph *periph, int priority)
374 * Increment the reference count on the peripheral
375 * while we wait for our lock attempt to succeed
376 * to ensure the peripheral doesn't disappear out
377 * from user us while we sleep.
380 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
383 mtx_assert(periph->sim->mtx, MA_OWNED);
384 while ((periph->flags & CAM_PERIPH_LOCKED) != 0) {
385 periph->flags |= CAM_PERIPH_LOCK_WANTED;
386 if ((error = mtx_sleep(periph, periph->sim->mtx, priority,
387 "caplck", 0)) != 0) {
388 cam_periph_release_locked(periph);
393 periph->flags |= CAM_PERIPH_LOCKED;
398 cam_periph_unhold(struct cam_periph *periph)
401 mtx_assert(periph->sim->mtx, MA_OWNED);
403 periph->flags &= ~CAM_PERIPH_LOCKED;
404 if ((periph->flags & CAM_PERIPH_LOCK_WANTED) != 0) {
405 periph->flags &= ~CAM_PERIPH_LOCK_WANTED;
409 cam_periph_release_locked(periph);
413 * Look for the next unit number that is not currently in use for this
414 * peripheral type starting at "newunit". Also exclude unit numbers that
415 * are reserved by for future "hardwiring" unless we already know that this
416 * is a potential wired device. Only assume that the device is "wired" the
417 * first time through the loop since after that we'll be looking at unit
418 * numbers that did not match a wiring entry.
421 camperiphnextunit(struct periph_driver *p_drv, u_int newunit, int wired,
422 path_id_t pathid, target_id_t target, lun_id_t lun)
424 struct cam_periph *periph;
426 int i, val, dunit, r;
427 const char *dname, *strval;
429 periph_name = p_drv->driver_name;
432 for (periph = TAILQ_FIRST(&p_drv->units);
433 periph != NULL && periph->unit_number != newunit;
434 periph = TAILQ_NEXT(periph, unit_links))
437 if (periph != NULL && periph->unit_number == newunit) {
439 xpt_print(periph->path, "Duplicate Wired "
441 xpt_print(periph->path, "Second device (%s "
442 "device at scbus%d target %d lun %d) will "
443 "not be wired\n", periph_name, pathid,
453 * Don't match entries like "da 4" as a wired down
454 * device, but do match entries like "da 4 target 5"
455 * or even "da 4 scbus 1".
460 r = resource_find_dev(&i, dname, &dunit, NULL, NULL);
463 /* if no "target" and no specific scbus, skip */
464 if (resource_int_value(dname, dunit, "target", &val) &&
465 (resource_string_value(dname, dunit, "at",&strval)||
466 strcmp(strval, "scbus") == 0))
468 if (newunit == dunit)
478 camperiphunit(struct periph_driver *p_drv, path_id_t pathid,
479 target_id_t target, lun_id_t lun)
482 int wired, i, val, dunit;
483 const char *dname, *strval;
484 char pathbuf[32], *periph_name;
486 periph_name = p_drv->driver_name;
487 snprintf(pathbuf, sizeof(pathbuf), "scbus%d", pathid);
491 for (wired = 0; resource_find_dev(&i, dname, &dunit, NULL, NULL) == 0;
493 if (resource_string_value(dname, dunit, "at", &strval) == 0) {
494 if (strcmp(strval, pathbuf) != 0)
498 if (resource_int_value(dname, dunit, "target", &val) == 0) {
503 if (resource_int_value(dname, dunit, "lun", &val) == 0) {
515 * Either start from 0 looking for the next unit or from
516 * the unit number given in the resource config. This way,
517 * if we have wildcard matches, we don't return the same
520 unit = camperiphnextunit(p_drv, unit, wired, pathid, target, lun);
526 cam_periph_invalidate(struct cam_periph *periph)
530 * We only call this routine the first time a peripheral is
533 if (((periph->flags & CAM_PERIPH_INVALID) == 0)
534 && (periph->periph_oninval != NULL))
535 periph->periph_oninval(periph);
537 periph->flags |= CAM_PERIPH_INVALID;
538 periph->flags &= ~CAM_PERIPH_NEW_DEV_FOUND;
541 if (periph->refcount == 0)
542 camperiphfree(periph);
547 camperiphfree(struct cam_periph *periph)
549 struct periph_driver **p_drv;
551 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
552 if (strcmp((*p_drv)->driver_name, periph->periph_name) == 0)
555 if (*p_drv == NULL) {
556 printf("camperiphfree: attempt to free non-existant periph\n");
560 TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
561 (*p_drv)->generation++;
564 if (periph->periph_dtor != NULL)
565 periph->periph_dtor(periph);
566 xpt_remove_periph(periph);
568 if (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) {
572 switch (periph->deferred_ac) {
573 case AC_FOUND_DEVICE:
574 ccb.ccb_h.func_code = XPT_GDEV_TYPE;
575 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
579 case AC_PATH_REGISTERED:
580 ccb.ccb_h.func_code = XPT_PATH_INQ;
581 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
589 periph->deferred_callback(NULL, periph->deferred_ac,
592 xpt_free_path(periph->path);
593 free(periph, M_CAMPERIPH);
598 * Map user virtual pointers into kernel virtual address space, so we can
599 * access the memory. This won't work on physical pointers, for now it's
600 * up to the caller to check for that. (XXX KDM -- should we do that here
601 * instead?) This also only works for up to MAXPHYS memory. Since we use
602 * buffers to map stuff in and out, we're limited to the buffer size.
605 cam_periph_mapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
608 int flags[CAM_PERIPH_MAXMAPS];
609 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
610 u_int32_t lengths[CAM_PERIPH_MAXMAPS];
611 u_int32_t dirs[CAM_PERIPH_MAXMAPS];
612 /* Some controllers may not be able to handle more data. */
613 size_t maxmap = DFLTPHYS;
615 switch(ccb->ccb_h.func_code) {
617 if (ccb->cdm.match_buf_len == 0) {
618 printf("cam_periph_mapmem: invalid match buffer "
622 if (ccb->cdm.pattern_buf_len > 0) {
623 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
624 lengths[0] = ccb->cdm.pattern_buf_len;
625 dirs[0] = CAM_DIR_OUT;
626 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
627 lengths[1] = ccb->cdm.match_buf_len;
628 dirs[1] = CAM_DIR_IN;
631 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
632 lengths[0] = ccb->cdm.match_buf_len;
633 dirs[0] = CAM_DIR_IN;
637 * This request will not go to the hardware, no reason
638 * to be so strict. vmapbuf() is able to map up to MAXPHYS.
643 case XPT_CONT_TARGET_IO:
644 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
647 data_ptrs[0] = &ccb->csio.data_ptr;
648 lengths[0] = ccb->csio.dxfer_len;
649 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
653 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
656 data_ptrs[0] = &ccb->ataio.data_ptr;
657 lengths[0] = ccb->ataio.dxfer_len;
658 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
663 break; /* NOTREACHED */
667 * Check the transfer length and permissions first, so we don't
668 * have to unmap any previously mapped buffers.
670 for (i = 0; i < numbufs; i++) {
675 * The userland data pointer passed in may not be page
676 * aligned. vmapbuf() truncates the address to a page
677 * boundary, so if the address isn't page aligned, we'll
678 * need enough space for the given transfer length, plus
679 * whatever extra space is necessary to make it to the page
683 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)) > maxmap){
684 printf("cam_periph_mapmem: attempt to map %lu bytes, "
685 "which is greater than %lu\n",
687 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)),
692 if (dirs[i] & CAM_DIR_OUT) {
693 flags[i] = BIO_WRITE;
696 if (dirs[i] & CAM_DIR_IN) {
702 /* this keeps the current process from getting swapped */
704 * XXX KDM should I use P_NOSWAP instead?
708 for (i = 0; i < numbufs; i++) {
712 mapinfo->bp[i] = getpbuf(NULL);
714 /* save the buffer's data address */
715 mapinfo->bp[i]->b_saveaddr = mapinfo->bp[i]->b_data;
717 /* put our pointer in the data slot */
718 mapinfo->bp[i]->b_data = *data_ptrs[i];
720 /* set the transfer length, we know it's < MAXPHYS */
721 mapinfo->bp[i]->b_bufsize = lengths[i];
723 /* set the direction */
724 mapinfo->bp[i]->b_iocmd = flags[i];
727 * Map the buffer into kernel memory.
729 * Note that useracc() alone is not a sufficient test.
730 * vmapbuf() can still fail due to a smaller file mapped
731 * into a larger area of VM, or if userland races against
732 * vmapbuf() after the useracc() check.
734 if (vmapbuf(mapinfo->bp[i]) < 0) {
735 for (j = 0; j < i; ++j) {
736 *data_ptrs[j] = mapinfo->bp[j]->b_saveaddr;
737 vunmapbuf(mapinfo->bp[j]);
738 relpbuf(mapinfo->bp[j], NULL);
740 relpbuf(mapinfo->bp[i], NULL);
745 /* set our pointer to the new mapped area */
746 *data_ptrs[i] = mapinfo->bp[i]->b_data;
748 mapinfo->num_bufs_used++;
752 * Now that we've gotten this far, change ownership to the kernel
753 * of the buffers so that we don't run afoul of returning to user
754 * space with locks (on the buffer) held.
756 for (i = 0; i < numbufs; i++) {
757 BUF_KERNPROC(mapinfo->bp[i]);
765 * Unmap memory segments mapped into kernel virtual address space by
766 * cam_periph_mapmem().
769 cam_periph_unmapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
772 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
774 if (mapinfo->num_bufs_used <= 0) {
775 /* allow ourselves to be swapped once again */
780 switch (ccb->ccb_h.func_code) {
782 numbufs = min(mapinfo->num_bufs_used, 2);
785 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
787 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
788 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
792 case XPT_CONT_TARGET_IO:
793 data_ptrs[0] = &ccb->csio.data_ptr;
794 numbufs = min(mapinfo->num_bufs_used, 1);
797 data_ptrs[0] = &ccb->ataio.data_ptr;
798 numbufs = min(mapinfo->num_bufs_used, 1);
801 /* allow ourselves to be swapped once again */
804 break; /* NOTREACHED */
807 for (i = 0; i < numbufs; i++) {
808 /* Set the user's pointer back to the original value */
809 *data_ptrs[i] = mapinfo->bp[i]->b_saveaddr;
811 /* unmap the buffer */
812 vunmapbuf(mapinfo->bp[i]);
814 /* release the buffer */
815 relpbuf(mapinfo->bp[i], NULL);
818 /* allow ourselves to be swapped once again */
823 cam_periph_getccb(struct cam_periph *periph, u_int32_t priority)
825 struct ccb_hdr *ccb_h;
827 mtx_assert(periph->sim->mtx, MA_OWNED);
828 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdgetccb\n"));
830 while (SLIST_FIRST(&periph->ccb_list) == NULL) {
831 if (periph->immediate_priority > priority)
832 periph->immediate_priority = priority;
833 xpt_schedule(periph, priority);
834 if ((SLIST_FIRST(&periph->ccb_list) != NULL)
835 && (SLIST_FIRST(&periph->ccb_list)->pinfo.priority == priority))
837 mtx_assert(periph->sim->mtx, MA_OWNED);
838 mtx_sleep(&periph->ccb_list, periph->sim->mtx, PRIBIO, "cgticb",
842 ccb_h = SLIST_FIRST(&periph->ccb_list);
843 SLIST_REMOVE_HEAD(&periph->ccb_list, periph_links.sle);
844 return ((union ccb *)ccb_h);
848 cam_periph_ccbwait(union ccb *ccb)
852 sim = xpt_path_sim(ccb->ccb_h.path);
853 if ((ccb->ccb_h.pinfo.index != CAM_UNQUEUED_INDEX)
854 || ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG))
855 mtx_sleep(&ccb->ccb_h.cbfcnp, sim->mtx, PRIBIO, "cbwait", 0);
859 cam_periph_ioctl(struct cam_periph *periph, u_long cmd, caddr_t addr,
860 int (*error_routine)(union ccb *ccb,
862 u_int32_t sense_flags))
872 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
873 xpt_setup_ccb(&ccb->ccb_h,
875 CAM_PRIORITY_NORMAL);
876 ccb->ccb_h.func_code = XPT_GDEVLIST;
879 * Basically, the point of this is that we go through
880 * getting the list of devices, until we find a passthrough
881 * device. In the current version of the CAM code, the
882 * only way to determine what type of device we're dealing
883 * with is by its name.
887 ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS;
888 while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) {
890 /* we want the next device in the list */
892 if (strncmp(ccb->cgdl.periph_name,
898 if ((ccb->cgdl.status == CAM_GDEVLIST_LAST_DEVICE) &&
900 ccb->cgdl.periph_name[0] = '\0';
901 ccb->cgdl.unit_number = 0;
906 /* copy the result back out */
907 bcopy(ccb, addr, sizeof(union ccb));
909 /* and release the ccb */
910 xpt_release_ccb(ccb);
921 cam_periph_runccb(union ccb *ccb,
922 int (*error_routine)(union ccb *ccb,
924 u_int32_t sense_flags),
925 cam_flags camflags, u_int32_t sense_flags,
932 sim = xpt_path_sim(ccb->ccb_h.path);
933 mtx_assert(sim->mtx, MA_OWNED);
936 * If the user has supplied a stats structure, and if we understand
937 * this particular type of ccb, record the transaction start.
939 if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO ||
940 ccb->ccb_h.func_code == XPT_ATA_IO))
941 devstat_start_transaction(ds, NULL);
946 cam_periph_ccbwait(ccb);
947 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
949 else if (error_routine != NULL)
950 error = (*error_routine)(ccb, camflags, sense_flags);
954 } while (error == ERESTART);
956 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
957 cam_release_devq(ccb->ccb_h.path,
961 /* getcount_only */ FALSE);
962 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
966 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
967 devstat_end_transaction(ds,
969 ccb->csio.tag_action & 0x3,
970 ((ccb->ccb_h.flags & CAM_DIR_MASK) ==
971 CAM_DIR_NONE) ? DEVSTAT_NO_DATA :
972 (ccb->ccb_h.flags & CAM_DIR_OUT) ?
974 DEVSTAT_READ, NULL, NULL);
975 } else if (ccb->ccb_h.func_code == XPT_ATA_IO) {
976 devstat_end_transaction(ds,
977 ccb->ataio.dxfer_len,
978 ccb->ataio.tag_action & 0x3,
979 ((ccb->ccb_h.flags & CAM_DIR_MASK) ==
980 CAM_DIR_NONE) ? DEVSTAT_NO_DATA :
981 (ccb->ccb_h.flags & CAM_DIR_OUT) ?
983 DEVSTAT_READ, NULL, NULL);
991 cam_freeze_devq(struct cam_path *path)
994 cam_freeze_devq_arg(path, 0, 0);
998 cam_freeze_devq_arg(struct cam_path *path, uint32_t flags, uint32_t arg)
1000 struct ccb_relsim crs;
1002 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NONE);
1003 crs.ccb_h.func_code = XPT_FREEZE_QUEUE;
1004 crs.release_flags = flags;
1006 crs.release_timeout = arg;
1007 xpt_action((union ccb *)&crs);
1011 cam_release_devq(struct cam_path *path, u_int32_t relsim_flags,
1012 u_int32_t openings, u_int32_t arg,
1015 struct ccb_relsim crs;
1017 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
1018 crs.ccb_h.func_code = XPT_REL_SIMQ;
1019 crs.ccb_h.flags = getcount_only ? CAM_DEV_QFREEZE : 0;
1020 crs.release_flags = relsim_flags;
1021 crs.openings = openings;
1022 crs.release_timeout = arg;
1023 xpt_action((union ccb *)&crs);
1024 return (crs.qfrozen_cnt);
1027 #define saved_ccb_ptr ppriv_ptr0
1028 #define recovery_depth ppriv_field1
1030 camperiphsensedone(struct cam_periph *periph, union ccb *done_ccb)
1032 union ccb *saved_ccb = (union ccb *)done_ccb->ccb_h.saved_ccb_ptr;
1036 int depth = done_ccb->ccb_h.recovery_depth;
1038 status = done_ccb->ccb_h.status;
1039 if (status & CAM_DEV_QFRZN) {
1042 * Clear freeze flag now for case of retry,
1043 * freeze will be dropped later.
1045 done_ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1047 status &= CAM_STATUS_MASK;
1052 * If we manually retrieved sense into a CCB and got
1053 * something other than "NO SENSE" send the updated CCB
1054 * back to the client via xpt_done() to be processed via
1055 * the error recovery code again.
1057 sense_key = saved_ccb->csio.sense_data.flags;
1058 sense_key &= SSD_KEY;
1059 if (sense_key != SSD_KEY_NO_SENSE) {
1060 saved_ccb->ccb_h.status |=
1063 saved_ccb->ccb_h.status &=
1065 saved_ccb->ccb_h.status |=
1068 saved_ccb->csio.sense_resid = done_ccb->csio.resid;
1069 bcopy(saved_ccb, done_ccb, sizeof(union ccb));
1070 xpt_free_ccb(saved_ccb);
1074 bcopy(saved_ccb, done_ccb, sizeof(union ccb));
1075 xpt_free_ccb(saved_ccb);
1076 done_ccb->ccb_h.status &= ~CAM_STATUS_MASK;
1077 done_ccb->ccb_h.status |= CAM_AUTOSENSE_FAIL;
1080 periph->flags &= ~CAM_PERIPH_SENSE_INPROG;
1082 * If it is the end of recovery, drop freeze, taken due to
1083 * CAM_DEV_QFREEZE flag, set on recovery request.
1086 cam_release_devq(done_ccb->ccb_h.path,
1090 /*getcount_only*/0);
1093 * Copy frozen flag from recovery request if it is set there
1097 done_ccb->ccb_h.status |= CAM_DEV_QFRZN;
1098 (*done_ccb->ccb_h.cbfcnp)(periph, done_ccb);
1102 camperiphdone(struct cam_periph *periph, union ccb *done_ccb)
1104 union ccb *saved_ccb, *save_ccb;
1107 struct scsi_start_stop_unit *scsi_cmd;
1108 u_int32_t relsim_flags, timeout;
1110 status = done_ccb->ccb_h.status;
1111 if (status & CAM_DEV_QFRZN) {
1114 * Clear freeze flag now for case of retry,
1115 * freeze will be dropped later.
1117 done_ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1122 saved_ccb = (union ccb *)done_ccb->ccb_h.saved_ccb_ptr;
1124 switch (status & CAM_STATUS_MASK) {
1128 * If we have successfully taken a device from the not
1129 * ready to ready state, re-scan the device and re-get
1130 * the inquiry information. Many devices (mostly disks)
1131 * don't properly report their inquiry information unless
1134 scsi_cmd = (struct scsi_start_stop_unit *)
1135 &done_ccb->csio.cdb_io.cdb_bytes;
1137 if (scsi_cmd->opcode == START_STOP_UNIT)
1138 xpt_async(AC_INQ_CHANGED,
1139 done_ccb->ccb_h.path, NULL);
1142 case CAM_SCSI_STATUS_ERROR:
1143 scsi_cmd = (struct scsi_start_stop_unit *)
1144 &done_ccb->csio.cdb_io.cdb_bytes;
1145 if (status & CAM_AUTOSNS_VALID) {
1146 struct ccb_getdev cgd;
1147 struct scsi_sense_data *sense;
1148 int error_code, sense_key, asc, ascq;
1149 scsi_sense_action err_action;
1151 sense = &done_ccb->csio.sense_data;
1152 scsi_extract_sense(sense, &error_code,
1153 &sense_key, &asc, &ascq);
1155 * Grab the inquiry data for this device.
1157 xpt_setup_ccb(&cgd.ccb_h, done_ccb->ccb_h.path,
1158 CAM_PRIORITY_NORMAL);
1159 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1160 xpt_action((union ccb *)&cgd);
1161 err_action = scsi_error_action(&done_ccb->csio,
1164 * If the error is "invalid field in CDB",
1165 * and the load/eject flag is set, turn the
1166 * flag off and try again. This is just in
1167 * case the drive in question barfs on the
1168 * load eject flag. The CAM code should set
1169 * the load/eject flag by default for
1173 * Should we check to see what the specific
1174 * scsi status is?? Or does it not matter
1175 * since we already know that there was an
1176 * error, and we know what the specific
1177 * error code was, and we know what the
1180 if ((scsi_cmd->opcode == START_STOP_UNIT) &&
1181 ((scsi_cmd->how & SSS_LOEJ) != 0) &&
1182 (asc == 0x24) && (ascq == 0x00) &&
1183 (done_ccb->ccb_h.retry_count > 0)) {
1185 scsi_cmd->how &= ~SSS_LOEJ;
1186 xpt_action(done_ccb);
1187 } else if ((done_ccb->ccb_h.retry_count > 1)
1188 && ((err_action & SS_MASK) != SS_FAIL)) {
1191 * In this case, the error recovery
1192 * command failed, but we've got
1193 * some retries left on it. Give
1194 * it another try unless this is an
1195 * unretryable error.
1197 /* set the timeout to .5 sec */
1199 RELSIM_RELEASE_AFTER_TIMEOUT;
1201 xpt_action(done_ccb);
1205 * Perform the final retry with the original
1206 * CCB so that final error processing is
1207 * performed by the owner of the CCB.
1212 save_ccb = xpt_alloc_ccb_nowait();
1213 if (save_ccb == NULL)
1215 bcopy(done_ccb, save_ccb, sizeof(*save_ccb));
1216 periph->flags |= CAM_PERIPH_SENSE_INPROG;
1218 * Send a Request Sense to the device. We
1219 * assume that we are in a contingent allegiance
1220 * condition so we do not tag this request.
1222 scsi_request_sense(&done_ccb->csio, /*retries*/1,
1224 &save_ccb->csio.sense_data,
1225 save_ccb->csio.sense_len,
1226 CAM_TAG_ACTION_NONE,
1227 /*sense_len*/SSD_FULL_SIZE,
1229 done_ccb->ccb_h.pinfo.priority--;
1230 done_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1231 done_ccb->ccb_h.saved_ccb_ptr = save_ccb;
1232 done_ccb->ccb_h.recovery_depth++;
1233 xpt_action(done_ccb);
1238 bcopy(saved_ccb, done_ccb, sizeof(*done_ccb));
1239 xpt_free_ccb(saved_ccb);
1240 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1241 xpt_action(done_ccb);
1245 /* decrement the retry count */
1247 * XXX This isn't appropriate in all cases. Restructure,
1248 * so that the retry count is only decremented on an
1249 * actual retry. Remeber that the orignal ccb had its
1250 * retry count dropped before entering recovery, so
1251 * doing it again is a bug.
1253 if (done_ccb->ccb_h.retry_count > 0)
1254 done_ccb->ccb_h.retry_count--;
1256 * Drop freeze taken due to CAM_DEV_QFREEZE flag set on recovery
1259 cam_release_devq(done_ccb->ccb_h.path,
1260 /*relsim_flags*/relsim_flags,
1263 /*getcount_only*/0);
1264 /* Drop freeze taken, if this recovery request got error. */
1266 cam_release_devq(done_ccb->ccb_h.path,
1270 /*getcount_only*/0);
1275 * Generic Async Event handler. Peripheral drivers usually
1276 * filter out the events that require personal attention,
1277 * and leave the rest to this function.
1280 cam_periph_async(struct cam_periph *periph, u_int32_t code,
1281 struct cam_path *path, void *arg)
1284 case AC_LOST_DEVICE:
1285 cam_periph_invalidate(periph);
1293 cam_periph_bus_settle(struct cam_periph *periph, u_int bus_settle)
1295 struct ccb_getdevstats cgds;
1297 xpt_setup_ccb(&cgds.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1298 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1299 xpt_action((union ccb *)&cgds);
1300 cam_periph_freeze_after_event(periph, &cgds.last_reset, bus_settle);
1304 cam_periph_freeze_after_event(struct cam_periph *periph,
1305 struct timeval* event_time, u_int duration_ms)
1307 struct timeval delta;
1308 struct timeval duration_tv;
1311 timevalsub(&delta, event_time);
1312 duration_tv.tv_sec = duration_ms / 1000;
1313 duration_tv.tv_usec = (duration_ms % 1000) * 1000;
1314 if (timevalcmp(&delta, &duration_tv, <)) {
1315 timevalsub(&duration_tv, &delta);
1317 duration_ms = duration_tv.tv_sec * 1000;
1318 duration_ms += duration_tv.tv_usec / 1000;
1319 cam_freeze_devq(periph->path);
1320 cam_release_devq(periph->path,
1321 RELSIM_RELEASE_AFTER_TIMEOUT,
1323 /*timeout*/duration_ms,
1324 /*getcount_only*/0);
1330 camperiphscsistatuserror(union ccb *ccb, cam_flags camflags,
1331 u_int32_t sense_flags,
1332 int *openings, u_int32_t *relsim_flags,
1333 u_int32_t *timeout, const char **action_string)
1337 switch (ccb->csio.scsi_status) {
1338 case SCSI_STATUS_OK:
1339 case SCSI_STATUS_COND_MET:
1340 case SCSI_STATUS_INTERMED:
1341 case SCSI_STATUS_INTERMED_COND_MET:
1344 case SCSI_STATUS_CMD_TERMINATED:
1345 case SCSI_STATUS_CHECK_COND:
1347 xpt_print(ccb->ccb_h.path, "SCSI status error\n");
1348 error = camperiphscsisenseerror(ccb,
1356 case SCSI_STATUS_QUEUE_FULL:
1359 struct ccb_getdevstats cgds;
1362 * First off, find out what the current
1363 * transaction counts are.
1365 xpt_setup_ccb(&cgds.ccb_h,
1367 CAM_PRIORITY_NORMAL);
1368 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1369 xpt_action((union ccb *)&cgds);
1372 * If we were the only transaction active, treat
1373 * the QUEUE FULL as if it were a BUSY condition.
1375 if (cgds.dev_active != 0) {
1379 * Reduce the number of openings to
1380 * be 1 less than the amount it took
1381 * to get a queue full bounded by the
1382 * minimum allowed tag count for this
1385 total_openings = cgds.dev_active + cgds.dev_openings;
1386 *openings = cgds.dev_active;
1387 if (*openings < cgds.mintags)
1388 *openings = cgds.mintags;
1389 if (*openings < total_openings)
1390 *relsim_flags = RELSIM_ADJUST_OPENINGS;
1393 * Some devices report queue full for
1394 * temporary resource shortages. For
1395 * this reason, we allow a minimum
1396 * tag count to be entered via a
1397 * quirk entry to prevent the queue
1398 * count on these devices from falling
1399 * to a pessimisticly low value. We
1400 * still wait for the next successful
1401 * completion, however, before queueing
1402 * more transactions to the device.
1404 *relsim_flags = RELSIM_RELEASE_AFTER_CMDCMPLT;
1409 xpt_print(ccb->ccb_h.path, "Queue full\n");
1415 case SCSI_STATUS_BUSY:
1417 * Restart the queue after either another
1418 * command completes or a 1 second timeout.
1421 xpt_print(ccb->ccb_h.path, "Device busy\n");
1423 if (ccb->ccb_h.retry_count > 0) {
1424 ccb->ccb_h.retry_count--;
1426 *relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT
1427 | RELSIM_RELEASE_AFTER_CMDCMPLT;
1433 case SCSI_STATUS_RESERV_CONFLICT:
1434 xpt_print(ccb->ccb_h.path, "Reservation conflict\n");
1438 xpt_print(ccb->ccb_h.path, "SCSI status 0x%x\n",
1439 ccb->csio.scsi_status);
1447 camperiphscsisenseerror(union ccb *ccb, cam_flags camflags,
1448 u_int32_t sense_flags,
1449 int *openings, u_int32_t *relsim_flags,
1450 u_int32_t *timeout, const char **action_string)
1452 struct cam_periph *periph;
1453 union ccb *orig_ccb = ccb;
1456 periph = xpt_path_periph(ccb->ccb_h.path);
1458 (CAM_PERIPH_RECOVERY_INPROG | CAM_PERIPH_SENSE_INPROG)) {
1460 * If error recovery is already in progress, don't attempt
1461 * to process this error, but requeue it unconditionally
1462 * and attempt to process it once error recovery has
1463 * completed. This failed command is probably related to
1464 * the error that caused the currently active error recovery
1465 * action so our current recovery efforts should also
1466 * address this command. Be aware that the error recovery
1467 * code assumes that only one recovery action is in progress
1468 * on a particular peripheral instance at any given time
1469 * (e.g. only one saved CCB for error recovery) so it is
1470 * imperitive that we don't violate this assumption.
1474 scsi_sense_action err_action;
1475 struct ccb_getdev cgd;
1478 * Grab the inquiry data for this device.
1480 xpt_setup_ccb(&cgd.ccb_h, ccb->ccb_h.path, CAM_PRIORITY_NORMAL);
1481 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1482 xpt_action((union ccb *)&cgd);
1484 if ((ccb->ccb_h.status & CAM_AUTOSNS_VALID) != 0)
1485 err_action = scsi_error_action(&ccb->csio,
1488 else if ((ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)
1489 err_action = SS_REQSENSE;
1491 err_action = SS_RETRY|SSQ_DECREMENT_COUNT|EIO;
1493 error = err_action & SS_ERRMASK;
1496 * If the recovery action will consume a retry,
1497 * make sure we actually have retries available.
1499 if ((err_action & SSQ_DECREMENT_COUNT) != 0) {
1500 if (ccb->ccb_h.retry_count > 0 &&
1501 (periph->flags & CAM_PERIPH_INVALID) == 0)
1502 ccb->ccb_h.retry_count--;
1504 *action_string = "Retries exhausted";
1505 goto sense_error_done;
1509 if ((err_action & SS_MASK) >= SS_START) {
1511 * Do common portions of commands that
1512 * use recovery CCBs.
1514 orig_ccb = xpt_alloc_ccb_nowait();
1515 if (orig_ccb == NULL) {
1516 *action_string = "Can't allocate recovery CCB";
1517 goto sense_error_done;
1520 * Clear freeze flag for original request here, as
1521 * this freeze will be dropped as part of ERESTART.
1523 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1524 bcopy(ccb, orig_ccb, sizeof(*orig_ccb));
1527 switch (err_action & SS_MASK) {
1529 *action_string = "No recovery action needed";
1533 *action_string = "Retrying command (per sense data)";
1537 *action_string = "Unretryable error";
1544 * Send a start unit command to the device, and
1545 * then retry the command.
1547 *action_string = "Attempting to start unit";
1548 periph->flags |= CAM_PERIPH_RECOVERY_INPROG;
1551 * Check for removable media and set
1552 * load/eject flag appropriately.
1554 if (SID_IS_REMOVABLE(&cgd.inq_data))
1559 scsi_start_stop(&ccb->csio,
1573 * Send a Test Unit Ready to the device.
1574 * If the 'many' flag is set, we send 120
1575 * test unit ready commands, one every half
1576 * second. Otherwise, we just send one TUR.
1577 * We only want to do this if the retry
1578 * count has not been exhausted.
1582 if ((err_action & SSQ_MANY) != 0) {
1583 *action_string = "Polling device for readiness";
1586 *action_string = "Testing device for readiness";
1589 periph->flags |= CAM_PERIPH_RECOVERY_INPROG;
1590 scsi_test_unit_ready(&ccb->csio,
1598 * Accomplish our 500ms delay by deferring
1599 * the release of our device queue appropriately.
1601 *relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1607 *action_string = "Requesting SCSI sense data";
1608 periph->flags |= CAM_PERIPH_SENSE_INPROG;
1610 * Send a Request Sense to the device. We
1611 * assume that we are in a contingent allegiance
1612 * condition so we do not tag this request.
1614 scsi_request_sense(&ccb->csio, /*retries*/1,
1616 &orig_ccb->csio.sense_data,
1617 orig_ccb->csio.sense_len,
1618 CAM_TAG_ACTION_NONE,
1619 /*sense_len*/SSD_FULL_SIZE,
1624 panic("Unhandled error action %x", err_action);
1627 if ((err_action & SS_MASK) >= SS_START) {
1629 * Drop the priority, so that the recovery
1630 * CCB is the first to execute. Freeze the queue
1631 * after this command is sent so that we can
1632 * restore the old csio and have it queued in
1633 * the proper order before we release normal
1634 * transactions to the device.
1636 ccb->ccb_h.pinfo.priority--;
1637 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1638 ccb->ccb_h.saved_ccb_ptr = orig_ccb;
1639 ccb->ccb_h.recovery_depth = 0;
1644 if ((err_action & SSQ_PRINT_SENSE) != 0
1645 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID) != 0)
1646 cam_error_print(orig_ccb, CAM_ESF_ALL, CAM_EPF_ALL);
1652 * Generic error handler. Peripheral drivers usually filter
1653 * out the errors that they handle in a unique mannor, then
1654 * call this function.
1657 cam_periph_error(union ccb *ccb, cam_flags camflags,
1658 u_int32_t sense_flags, union ccb *save_ccb)
1660 struct cam_periph *periph;
1661 const char *action_string;
1664 int error, printed = 0;
1666 u_int32_t relsim_flags;
1667 u_int32_t timeout = 0;
1669 periph = xpt_path_periph(ccb->ccb_h.path);
1670 action_string = NULL;
1671 status = ccb->ccb_h.status;
1672 frozen = (status & CAM_DEV_QFRZN) != 0;
1673 status &= CAM_STATUS_MASK;
1674 openings = relsim_flags = 0;
1680 case CAM_SCSI_STATUS_ERROR:
1681 error = camperiphscsistatuserror(ccb,
1689 case CAM_AUTOSENSE_FAIL:
1690 xpt_print(ccb->ccb_h.path, "AutoSense failed\n");
1691 error = EIO; /* we have to kill the command */
1693 case CAM_ATA_STATUS_ERROR:
1694 if (bootverbose && printed == 0) {
1695 xpt_print(ccb->ccb_h.path, "ATA status error\n");
1696 cam_error_print(ccb, CAM_ESF_ALL, CAM_EPF_ALL);
1700 case CAM_REQ_CMP_ERR:
1701 if (bootverbose && printed == 0) {
1702 xpt_print(ccb->ccb_h.path,
1703 "Request completed with CAM_REQ_CMP_ERR\n");
1707 case CAM_CMD_TIMEOUT:
1708 if (bootverbose && printed == 0) {
1709 xpt_print(ccb->ccb_h.path, "Command timed out\n");
1713 case CAM_UNEXP_BUSFREE:
1714 if (bootverbose && printed == 0) {
1715 xpt_print(ccb->ccb_h.path, "Unexpected Bus Free\n");
1719 case CAM_UNCOR_PARITY:
1720 if (bootverbose && printed == 0) {
1721 xpt_print(ccb->ccb_h.path,
1722 "Uncorrected parity error\n");
1726 case CAM_DATA_RUN_ERR:
1727 if (bootverbose && printed == 0) {
1728 xpt_print(ccb->ccb_h.path, "Data overrun\n");
1731 /* decrement the number of retries */
1732 if (ccb->ccb_h.retry_count > 0 &&
1733 (periph->flags & CAM_PERIPH_INVALID) == 0) {
1734 ccb->ccb_h.retry_count--;
1737 action_string = "Retries exhausted";
1743 case CAM_MSG_REJECT_REC:
1744 /* XXX Don't know that these are correct */
1747 case CAM_SEL_TIMEOUT:
1748 if ((camflags & CAM_RETRY_SELTO) != 0) {
1749 if (ccb->ccb_h.retry_count > 0 &&
1750 (periph->flags & CAM_PERIPH_INVALID) == 0) {
1752 ccb->ccb_h.retry_count--;
1754 if (bootverbose && printed == 0) {
1755 xpt_print(ccb->ccb_h.path,
1756 "Selection timeout\n");
1761 * Wait a bit to give the device
1762 * time to recover before we try again.
1764 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1765 timeout = periph_selto_delay;
1768 action_string = "Retries exhausted";
1771 case CAM_DEV_NOT_THERE:
1773 struct cam_path *newpath;
1779 * For a selection timeout, we consider all of the LUNs on
1780 * the target to be gone. If the status is CAM_DEV_NOT_THERE,
1781 * then we only get rid of the device(s) specified by the
1782 * path in the original CCB.
1784 if (status == CAM_DEV_NOT_THERE)
1785 lun_id = xpt_path_lun_id(ccb->ccb_h.path);
1787 lun_id = CAM_LUN_WILDCARD;
1789 /* Should we do more if we can't create the path?? */
1790 if (xpt_create_path(&newpath, periph,
1791 xpt_path_path_id(ccb->ccb_h.path),
1792 xpt_path_target_id(ccb->ccb_h.path),
1793 lun_id) != CAM_REQ_CMP)
1797 * Let peripheral drivers know that this device has gone
1800 xpt_async(AC_LOST_DEVICE, newpath, NULL);
1801 xpt_free_path(newpath);
1804 case CAM_REQ_INVALID:
1805 case CAM_PATH_INVALID:
1807 case CAM_PROVIDE_FAIL:
1808 case CAM_REQ_TOO_BIG:
1809 case CAM_LUN_INVALID:
1810 case CAM_TID_INVALID:
1813 case CAM_SCSI_BUS_RESET:
1816 * Commands that repeatedly timeout and cause these
1817 * kinds of error recovery actions, should return
1818 * CAM_CMD_TIMEOUT, which allows us to safely assume
1819 * that this command was an innocent bystander to
1820 * these events and should be unconditionally
1823 if (bootverbose && printed == 0) {
1824 xpt_print_path(ccb->ccb_h.path);
1825 if (status == CAM_BDR_SENT)
1826 printf("Bus Device Reset sent\n");
1828 printf("Bus Reset issued\n");
1832 case CAM_REQUEUE_REQ:
1833 /* Unconditional requeue */
1834 if (bootverbose && printed == 0) {
1835 xpt_print(ccb->ccb_h.path, "Request requeued\n");
1838 if ((periph->flags & CAM_PERIPH_INVALID) == 0)
1841 action_string = "Retries exhausted";
1845 case CAM_RESRC_UNAVAIL:
1846 /* Wait a bit for the resource shortage to abate. */
1847 timeout = periph_noresrc_delay;
1851 /* Wait a bit for the busy condition to abate. */
1852 timeout = periph_busy_delay;
1854 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1857 /* decrement the number of retries */
1858 if (ccb->ccb_h.retry_count > 0 &&
1859 (periph->flags & CAM_PERIPH_INVALID) == 0) {
1860 ccb->ccb_h.retry_count--;
1862 if (bootverbose && printed == 0) {
1863 xpt_print(ccb->ccb_h.path, "CAM status 0x%x\n",
1869 action_string = "Retries exhausted";
1875 * If we have and error and are booting verbosely, whine
1876 * *unless* this was a non-retryable selection timeout.
1878 if (error != 0 && bootverbose &&
1879 !(status == CAM_SEL_TIMEOUT && (camflags & CAM_RETRY_SELTO) == 0)) {
1880 if (error != ERESTART) {
1881 if (action_string == NULL)
1882 action_string = "Unretryable error";
1883 xpt_print(ccb->ccb_h.path, "Error %d, %s\n",
1884 error, action_string);
1885 } else if (action_string != NULL)
1886 xpt_print(ccb->ccb_h.path, "%s\n", action_string);
1888 xpt_print(ccb->ccb_h.path, "Retrying command\n");
1891 /* Attempt a retry */
1892 if (error == ERESTART || error == 0) {
1894 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1895 if (error == ERESTART)
1898 cam_release_devq(ccb->ccb_h.path,
1902 /*getcount_only*/0);