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,
74 u_int32_t sense_flags,
76 u_int32_t *relsim_flags,
79 const char **action_string);
80 static int camperiphscsisenseerror(union ccb *ccb,
83 u_int32_t sense_flags,
85 u_int32_t *relsim_flags,
88 const char **action_string);
90 static int nperiph_drivers;
91 static int initialized = 0;
92 struct periph_driver **periph_drivers;
94 static MALLOC_DEFINE(M_CAMPERIPH, "CAM periph", "CAM peripheral buffers");
96 static int periph_selto_delay = 1000;
97 TUNABLE_INT("kern.cam.periph_selto_delay", &periph_selto_delay);
98 static int periph_noresrc_delay = 500;
99 TUNABLE_INT("kern.cam.periph_noresrc_delay", &periph_noresrc_delay);
100 static int periph_busy_delay = 500;
101 TUNABLE_INT("kern.cam.periph_busy_delay", &periph_busy_delay);
105 periphdriver_register(void *data)
107 struct periph_driver *drv = (struct periph_driver *)data;
108 struct periph_driver **newdrivers, **old;
111 ndrivers = nperiph_drivers + 2;
112 newdrivers = malloc(sizeof(*newdrivers) * ndrivers, M_CAMPERIPH,
115 bcopy(periph_drivers, newdrivers,
116 sizeof(*newdrivers) * nperiph_drivers);
117 newdrivers[nperiph_drivers] = drv;
118 newdrivers[nperiph_drivers + 1] = NULL;
119 old = periph_drivers;
120 periph_drivers = newdrivers;
122 free(old, M_CAMPERIPH);
124 /* If driver marked as early or it is late now, initialize it. */
125 if (((drv->flags & CAM_PERIPH_DRV_EARLY) != 0 && initialized > 0) ||
131 periphdriver_init(int level)
135 initialized = max(initialized, level);
136 for (i = 0; periph_drivers[i] != NULL; i++) {
137 early = (periph_drivers[i]->flags & CAM_PERIPH_DRV_EARLY) ? 1 : 2;
138 if (early == initialized)
139 (*periph_drivers[i]->init)();
144 cam_periph_alloc(periph_ctor_t *periph_ctor,
145 periph_oninv_t *periph_oninvalidate,
146 periph_dtor_t *periph_dtor, periph_start_t *periph_start,
147 char *name, cam_periph_type type, struct cam_path *path,
148 ac_callback_t *ac_callback, ac_code code, void *arg)
150 struct periph_driver **p_drv;
152 struct cam_periph *periph;
153 struct cam_periph *cur_periph;
155 target_id_t target_id;
162 * Handle Hot-Plug scenarios. If there is already a peripheral
163 * of our type assigned to this path, we are likely waiting for
164 * final close on an old, invalidated, peripheral. If this is
165 * the case, queue up a deferred call to the peripheral's async
166 * handler. If it looks like a mistaken re-allocation, complain.
168 if ((periph = cam_periph_find(path, name)) != NULL) {
170 if ((periph->flags & CAM_PERIPH_INVALID) != 0
171 && (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) == 0) {
172 periph->flags |= CAM_PERIPH_NEW_DEV_FOUND;
173 periph->deferred_callback = ac_callback;
174 periph->deferred_ac = code;
175 return (CAM_REQ_INPROG);
177 printf("cam_periph_alloc: attempt to re-allocate "
178 "valid device %s%d rejected flags %#x "
179 "refcount %d\n", periph->periph_name,
180 periph->unit_number, periph->flags,
183 return (CAM_REQ_INVALID);
186 periph = (struct cam_periph *)malloc(sizeof(*periph), M_CAMPERIPH,
190 return (CAM_RESRC_UNAVAIL);
195 sim = xpt_path_sim(path);
196 path_id = xpt_path_path_id(path);
197 target_id = xpt_path_target_id(path);
198 lun_id = xpt_path_lun_id(path);
199 periph->periph_start = periph_start;
200 periph->periph_dtor = periph_dtor;
201 periph->periph_oninval = periph_oninvalidate;
203 periph->periph_name = name;
204 periph->scheduled_priority = CAM_PRIORITY_NONE;
205 periph->immediate_priority = CAM_PRIORITY_NONE;
206 periph->refcount = 1; /* Dropped by invalidation. */
208 SLIST_INIT(&periph->ccb_list);
209 status = xpt_create_path(&path, periph, path_id, target_id, lun_id);
210 if (status != CAM_REQ_CMP)
215 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
216 if (strcmp((*p_drv)->driver_name, name) == 0)
219 if (*p_drv == NULL) {
220 printf("cam_periph_alloc: invalid periph name '%s'\n", name);
222 xpt_free_path(periph->path);
223 free(periph, M_CAMPERIPH);
224 return (CAM_REQ_INVALID);
226 periph->unit_number = camperiphunit(*p_drv, path_id, target_id, lun_id);
227 cur_periph = TAILQ_FIRST(&(*p_drv)->units);
228 while (cur_periph != NULL
229 && cur_periph->unit_number < periph->unit_number)
230 cur_periph = TAILQ_NEXT(cur_periph, unit_links);
231 if (cur_periph != NULL) {
232 KASSERT(cur_periph->unit_number != periph->unit_number, ("duplicate units on periph list"));
233 TAILQ_INSERT_BEFORE(cur_periph, periph, unit_links);
235 TAILQ_INSERT_TAIL(&(*p_drv)->units, periph, unit_links);
236 (*p_drv)->generation++;
242 status = xpt_add_periph(periph);
243 if (status != CAM_REQ_CMP)
247 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph created\n"));
249 status = periph_ctor(periph, arg);
251 if (status == CAM_REQ_CMP)
255 switch (init_level) {
257 /* Initialized successfully */
260 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph destroyed\n"));
261 xpt_remove_periph(periph);
265 TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
267 xpt_free_path(periph->path);
270 free(periph, M_CAMPERIPH);
273 /* No cleanup to perform. */
276 panic("%s: Unknown init level", __func__);
282 * Find a peripheral structure with the specified path, target, lun,
283 * and (optionally) type. If the name is NULL, this function will return
284 * the first peripheral driver that matches the specified path.
287 cam_periph_find(struct cam_path *path, char *name)
289 struct periph_driver **p_drv;
290 struct cam_periph *periph;
293 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
295 if (name != NULL && (strcmp((*p_drv)->driver_name, name) != 0))
298 TAILQ_FOREACH(periph, &(*p_drv)->units, unit_links) {
299 if (xpt_path_comp(periph->path, path) == 0) {
301 cam_periph_assert(periph, MA_OWNED);
315 * Find peripheral driver instances attached to the specified path.
318 cam_periph_list(struct cam_path *path, struct sbuf *sb)
320 struct sbuf local_sb;
321 struct periph_driver **p_drv;
322 struct cam_periph *periph;
328 sbuf_new(&local_sb, NULL, sbuf_alloc_len, SBUF_FIXEDLEN);
331 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
333 TAILQ_FOREACH(periph, &(*p_drv)->units, unit_links) {
334 if (xpt_path_comp(periph->path, path) != 0)
337 if (sbuf_len(&local_sb) != 0)
338 sbuf_cat(&local_sb, ",");
340 sbuf_printf(&local_sb, "%s%d", periph->periph_name,
341 periph->unit_number);
343 if (sbuf_error(&local_sb) == ENOMEM) {
346 sbuf_delete(&local_sb);
353 sbuf_finish(&local_sb);
354 sbuf_cpy(sb, sbuf_data(&local_sb));
355 sbuf_delete(&local_sb);
360 cam_periph_acquire(struct cam_periph *periph)
364 status = CAM_REQ_CMP_ERR;
369 if ((periph->flags & CAM_PERIPH_INVALID) == 0) {
371 status = CAM_REQ_CMP;
379 cam_periph_doacquire(struct cam_periph *periph)
383 KASSERT(periph->refcount >= 1,
384 ("cam_periph_doacquire() with refcount == %d", periph->refcount));
390 cam_periph_release_locked_buses(struct cam_periph *periph)
393 cam_periph_assert(periph, MA_OWNED);
394 KASSERT(periph->refcount >= 1, ("periph->refcount >= 1"));
395 if (--periph->refcount == 0)
396 camperiphfree(periph);
400 cam_periph_release_locked(struct cam_periph *periph)
407 cam_periph_release_locked_buses(periph);
412 cam_periph_release(struct cam_periph *periph)
419 cam_periph_assert(periph, MA_NOTOWNED);
420 mtx = cam_periph_mtx(periph);
422 cam_periph_release_locked(periph);
427 cam_periph_hold(struct cam_periph *periph, int priority)
432 * Increment the reference count on the peripheral
433 * while we wait for our lock attempt to succeed
434 * to ensure the peripheral doesn't disappear out
435 * from user us while we sleep.
438 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
441 cam_periph_assert(periph, MA_OWNED);
442 while ((periph->flags & CAM_PERIPH_LOCKED) != 0) {
443 periph->flags |= CAM_PERIPH_LOCK_WANTED;
444 if ((error = cam_periph_sleep(periph, periph, priority,
445 "caplck", 0)) != 0) {
446 cam_periph_release_locked(periph);
449 if (periph->flags & CAM_PERIPH_INVALID) {
450 cam_periph_release_locked(periph);
455 periph->flags |= CAM_PERIPH_LOCKED;
460 cam_periph_unhold(struct cam_periph *periph)
463 cam_periph_assert(periph, MA_OWNED);
465 periph->flags &= ~CAM_PERIPH_LOCKED;
466 if ((periph->flags & CAM_PERIPH_LOCK_WANTED) != 0) {
467 periph->flags &= ~CAM_PERIPH_LOCK_WANTED;
471 cam_periph_release_locked(periph);
475 * Look for the next unit number that is not currently in use for this
476 * peripheral type starting at "newunit". Also exclude unit numbers that
477 * are reserved by for future "hardwiring" unless we already know that this
478 * is a potential wired device. Only assume that the device is "wired" the
479 * first time through the loop since after that we'll be looking at unit
480 * numbers that did not match a wiring entry.
483 camperiphnextunit(struct periph_driver *p_drv, u_int newunit, int wired,
484 path_id_t pathid, target_id_t target, lun_id_t lun)
486 struct cam_periph *periph;
488 int i, val, dunit, r;
489 const char *dname, *strval;
491 periph_name = p_drv->driver_name;
494 for (periph = TAILQ_FIRST(&p_drv->units);
495 periph != NULL && periph->unit_number != newunit;
496 periph = TAILQ_NEXT(periph, unit_links))
499 if (periph != NULL && periph->unit_number == newunit) {
501 xpt_print(periph->path, "Duplicate Wired "
503 xpt_print(periph->path, "Second device (%s "
504 "device at scbus%d target %d lun %d) will "
505 "not be wired\n", periph_name, pathid,
515 * Don't match entries like "da 4" as a wired down
516 * device, but do match entries like "da 4 target 5"
517 * or even "da 4 scbus 1".
522 r = resource_find_dev(&i, dname, &dunit, NULL, NULL);
525 /* if no "target" and no specific scbus, skip */
526 if (resource_int_value(dname, dunit, "target", &val) &&
527 (resource_string_value(dname, dunit, "at",&strval)||
528 strcmp(strval, "scbus") == 0))
530 if (newunit == dunit)
540 camperiphunit(struct periph_driver *p_drv, path_id_t pathid,
541 target_id_t target, lun_id_t lun)
544 int wired, i, val, dunit;
545 const char *dname, *strval;
546 char pathbuf[32], *periph_name;
548 periph_name = p_drv->driver_name;
549 snprintf(pathbuf, sizeof(pathbuf), "scbus%d", pathid);
553 for (wired = 0; resource_find_dev(&i, dname, &dunit, NULL, NULL) == 0;
555 if (resource_string_value(dname, dunit, "at", &strval) == 0) {
556 if (strcmp(strval, pathbuf) != 0)
560 if (resource_int_value(dname, dunit, "target", &val) == 0) {
565 if (resource_int_value(dname, dunit, "lun", &val) == 0) {
577 * Either start from 0 looking for the next unit or from
578 * the unit number given in the resource config. This way,
579 * if we have wildcard matches, we don't return the same
582 unit = camperiphnextunit(p_drv, unit, wired, pathid, target, lun);
588 cam_periph_invalidate(struct cam_periph *periph)
591 cam_periph_assert(periph, MA_OWNED);
593 * We only call this routine the first time a peripheral is
596 if ((periph->flags & CAM_PERIPH_INVALID) != 0)
599 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph invalidated\n"));
600 if ((periph->flags & CAM_PERIPH_ANNOUNCED) && !rebooting)
601 xpt_denounce_periph(periph);
602 periph->flags |= CAM_PERIPH_INVALID;
603 periph->flags &= ~CAM_PERIPH_NEW_DEV_FOUND;
604 if (periph->periph_oninval != NULL)
605 periph->periph_oninval(periph);
606 cam_periph_release_locked(periph);
610 camperiphfree(struct cam_periph *periph)
612 struct periph_driver **p_drv;
614 cam_periph_assert(periph, MA_OWNED);
615 KASSERT(periph->periph_allocating == 0, ("%s%d: freed while allocating",
616 periph->periph_name, periph->unit_number));
617 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
618 if (strcmp((*p_drv)->driver_name, periph->periph_name) == 0)
621 if (*p_drv == NULL) {
622 printf("camperiphfree: attempt to free non-existant periph\n");
627 * We need to set this flag before dropping the topology lock, to
628 * let anyone who is traversing the list that this peripheral is
629 * about to be freed, and there will be no more reference count
632 periph->flags |= CAM_PERIPH_FREE;
635 * The peripheral destructor semantics dictate calling with only the
636 * SIM mutex held. Since it might sleep, it should not be called
637 * with the topology lock held.
642 * We need to call the peripheral destructor prior to removing the
643 * peripheral from the list. Otherwise, we risk running into a
644 * scenario where the peripheral unit number may get reused
645 * (because it has been removed from the list), but some resources
646 * used by the peripheral are still hanging around. In particular,
647 * the devfs nodes used by some peripherals like the pass(4) driver
648 * aren't fully cleaned up until the destructor is run. If the
649 * unit number is reused before the devfs instance is fully gone,
652 if (periph->periph_dtor != NULL)
653 periph->periph_dtor(periph);
656 * The peripheral list is protected by the topology lock.
660 TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
661 (*p_drv)->generation++;
663 xpt_remove_periph(periph);
666 if ((periph->flags & CAM_PERIPH_ANNOUNCED) && !rebooting)
667 xpt_print(periph->path, "Periph destroyed\n");
669 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph destroyed\n"));
671 if (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) {
675 switch (periph->deferred_ac) {
676 case AC_FOUND_DEVICE:
677 ccb.ccb_h.func_code = XPT_GDEV_TYPE;
678 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
682 case AC_PATH_REGISTERED:
683 ccb.ccb_h.func_code = XPT_PATH_INQ;
684 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
692 periph->deferred_callback(NULL, periph->deferred_ac,
695 xpt_free_path(periph->path);
696 free(periph, M_CAMPERIPH);
701 * Map user virtual pointers into kernel virtual address space, so we can
702 * access the memory. This is now a generic function that centralizes most
703 * of the sanity checks on the data flags, if any.
704 * This also only works for up to MAXPHYS memory. Since we use
705 * buffers to map stuff in and out, we're limited to the buffer size.
708 cam_periph_mapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
711 int flags[CAM_PERIPH_MAXMAPS];
712 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
713 u_int32_t lengths[CAM_PERIPH_MAXMAPS];
714 u_int32_t dirs[CAM_PERIPH_MAXMAPS];
715 /* Some controllers may not be able to handle more data. */
716 size_t maxmap = DFLTPHYS;
718 switch(ccb->ccb_h.func_code) {
720 if (ccb->cdm.match_buf_len == 0) {
721 printf("cam_periph_mapmem: invalid match buffer "
725 if (ccb->cdm.pattern_buf_len > 0) {
726 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
727 lengths[0] = ccb->cdm.pattern_buf_len;
728 dirs[0] = CAM_DIR_OUT;
729 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
730 lengths[1] = ccb->cdm.match_buf_len;
731 dirs[1] = CAM_DIR_IN;
734 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
735 lengths[0] = ccb->cdm.match_buf_len;
736 dirs[0] = CAM_DIR_IN;
740 * This request will not go to the hardware, no reason
741 * to be so strict. vmapbuf() is able to map up to MAXPHYS.
746 case XPT_CONT_TARGET_IO:
747 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
749 if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR)
751 data_ptrs[0] = &ccb->csio.data_ptr;
752 lengths[0] = ccb->csio.dxfer_len;
753 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
757 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
759 if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR)
761 data_ptrs[0] = &ccb->ataio.data_ptr;
762 lengths[0] = ccb->ataio.dxfer_len;
763 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
767 data_ptrs[0] = &ccb->smpio.smp_request;
768 lengths[0] = ccb->smpio.smp_request_len;
769 dirs[0] = CAM_DIR_OUT;
770 data_ptrs[1] = &ccb->smpio.smp_response;
771 lengths[1] = ccb->smpio.smp_response_len;
772 dirs[1] = CAM_DIR_IN;
775 case XPT_DEV_ADVINFO:
776 if (ccb->cdai.bufsiz == 0)
779 data_ptrs[0] = (uint8_t **)&ccb->cdai.buf;
780 lengths[0] = ccb->cdai.bufsiz;
781 dirs[0] = CAM_DIR_IN;
785 * This request will not go to the hardware, no reason
786 * to be so strict. vmapbuf() is able to map up to MAXPHYS.
792 break; /* NOTREACHED */
796 * Check the transfer length and permissions first, so we don't
797 * have to unmap any previously mapped buffers.
799 for (i = 0; i < numbufs; i++) {
804 * The userland data pointer passed in may not be page
805 * aligned. vmapbuf() truncates the address to a page
806 * boundary, so if the address isn't page aligned, we'll
807 * need enough space for the given transfer length, plus
808 * whatever extra space is necessary to make it to the page
812 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)) > maxmap){
813 printf("cam_periph_mapmem: attempt to map %lu bytes, "
814 "which is greater than %lu\n",
816 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)),
821 if (dirs[i] & CAM_DIR_OUT) {
822 flags[i] = BIO_WRITE;
825 if (dirs[i] & CAM_DIR_IN) {
832 * This keeps the the kernel stack of current thread from getting
833 * swapped. In low-memory situations where the kernel stack might
834 * otherwise get swapped out, this holds it and allows the thread
835 * to make progress and release the kernel mapped pages sooner.
837 * XXX KDM should I use P_NOSWAP instead?
841 for (i = 0; i < numbufs; i++) {
845 mapinfo->bp[i] = getpbuf(NULL);
847 /* save the buffer's data address */
848 mapinfo->bp[i]->b_saveaddr = mapinfo->bp[i]->b_data;
850 /* put our pointer in the data slot */
851 mapinfo->bp[i]->b_data = *data_ptrs[i];
853 /* set the transfer length, we know it's < MAXPHYS */
854 mapinfo->bp[i]->b_bufsize = lengths[i];
856 /* set the direction */
857 mapinfo->bp[i]->b_iocmd = flags[i];
860 * Map the buffer into kernel memory.
862 * Note that useracc() alone is not a sufficient test.
863 * vmapbuf() can still fail due to a smaller file mapped
864 * into a larger area of VM, or if userland races against
865 * vmapbuf() after the useracc() check.
867 if (vmapbuf(mapinfo->bp[i], 1) < 0) {
868 for (j = 0; j < i; ++j) {
869 *data_ptrs[j] = mapinfo->bp[j]->b_saveaddr;
870 vunmapbuf(mapinfo->bp[j]);
871 relpbuf(mapinfo->bp[j], NULL);
873 relpbuf(mapinfo->bp[i], NULL);
878 /* set our pointer to the new mapped area */
879 *data_ptrs[i] = mapinfo->bp[i]->b_data;
881 mapinfo->num_bufs_used++;
885 * Now that we've gotten this far, change ownership to the kernel
886 * of the buffers so that we don't run afoul of returning to user
887 * space with locks (on the buffer) held.
889 for (i = 0; i < numbufs; i++) {
890 BUF_KERNPROC(mapinfo->bp[i]);
898 * Unmap memory segments mapped into kernel virtual address space by
899 * cam_periph_mapmem().
902 cam_periph_unmapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
905 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
907 if (mapinfo->num_bufs_used <= 0) {
908 /* nothing to free and the process wasn't held. */
912 switch (ccb->ccb_h.func_code) {
914 numbufs = min(mapinfo->num_bufs_used, 2);
917 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
919 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
920 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
924 case XPT_CONT_TARGET_IO:
925 data_ptrs[0] = &ccb->csio.data_ptr;
926 numbufs = min(mapinfo->num_bufs_used, 1);
929 data_ptrs[0] = &ccb->ataio.data_ptr;
930 numbufs = min(mapinfo->num_bufs_used, 1);
933 numbufs = min(mapinfo->num_bufs_used, 2);
934 data_ptrs[0] = &ccb->smpio.smp_request;
935 data_ptrs[1] = &ccb->smpio.smp_response;
937 case XPT_DEV_ADVINFO:
938 numbufs = min(mapinfo->num_bufs_used, 1);
939 data_ptrs[0] = (uint8_t **)&ccb->cdai.buf;
942 /* allow ourselves to be swapped once again */
945 break; /* NOTREACHED */
948 for (i = 0; i < numbufs; i++) {
949 /* Set the user's pointer back to the original value */
950 *data_ptrs[i] = mapinfo->bp[i]->b_saveaddr;
952 /* unmap the buffer */
953 vunmapbuf(mapinfo->bp[i]);
955 /* release the buffer */
956 relpbuf(mapinfo->bp[i], NULL);
959 /* allow ourselves to be swapped once again */
964 cam_periph_ccbwait(union ccb *ccb)
967 if ((ccb->ccb_h.pinfo.index != CAM_UNQUEUED_INDEX)
968 || ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG))
969 xpt_path_sleep(ccb->ccb_h.path, &ccb->ccb_h.cbfcnp, PRIBIO,
974 cam_periph_ioctl(struct cam_periph *periph, u_long cmd, caddr_t addr,
975 int (*error_routine)(union ccb *ccb,
977 u_int32_t sense_flags))
987 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
988 xpt_setup_ccb(&ccb->ccb_h,
990 CAM_PRIORITY_NORMAL);
991 ccb->ccb_h.func_code = XPT_GDEVLIST;
994 * Basically, the point of this is that we go through
995 * getting the list of devices, until we find a passthrough
996 * device. In the current version of the CAM code, the
997 * only way to determine what type of device we're dealing
998 * with is by its name.
1000 while (found == 0) {
1001 ccb->cgdl.index = 0;
1002 ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS;
1003 while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) {
1005 /* we want the next device in the list */
1007 if (strncmp(ccb->cgdl.periph_name,
1013 if ((ccb->cgdl.status == CAM_GDEVLIST_LAST_DEVICE) &&
1015 ccb->cgdl.periph_name[0] = '\0';
1016 ccb->cgdl.unit_number = 0;
1021 /* copy the result back out */
1022 bcopy(ccb, addr, sizeof(union ccb));
1024 /* and release the ccb */
1025 xpt_release_ccb(ccb);
1036 cam_periph_done(struct cam_periph *periph, union ccb *done_ccb)
1039 /* Caller will release the CCB */
1040 wakeup(&done_ccb->ccb_h.cbfcnp);
1044 cam_periph_runccb(union ccb *ccb,
1045 int (*error_routine)(union ccb *ccb,
1047 u_int32_t sense_flags),
1048 cam_flags camflags, u_int32_t sense_flags,
1053 xpt_path_assert(ccb->ccb_h.path, MA_OWNED);
1056 * If the user has supplied a stats structure, and if we understand
1057 * this particular type of ccb, record the transaction start.
1059 if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO ||
1060 ccb->ccb_h.func_code == XPT_ATA_IO))
1061 devstat_start_transaction(ds, NULL);
1063 ccb->ccb_h.cbfcnp = cam_periph_done;
1067 cam_periph_ccbwait(ccb);
1068 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
1070 else if (error_routine != NULL)
1071 error = (*error_routine)(ccb, camflags, sense_flags);
1075 } while (error == ERESTART);
1077 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1078 cam_release_devq(ccb->ccb_h.path,
1079 /* relsim_flags */0,
1082 /* getcount_only */ FALSE);
1083 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1087 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1088 devstat_end_transaction(ds,
1089 ccb->csio.dxfer_len,
1090 ccb->csio.tag_action & 0x3,
1091 ((ccb->ccb_h.flags & CAM_DIR_MASK) ==
1092 CAM_DIR_NONE) ? DEVSTAT_NO_DATA :
1093 (ccb->ccb_h.flags & CAM_DIR_OUT) ?
1095 DEVSTAT_READ, NULL, NULL);
1096 } else if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1097 devstat_end_transaction(ds,
1098 ccb->ataio.dxfer_len,
1099 ccb->ataio.tag_action & 0x3,
1100 ((ccb->ccb_h.flags & CAM_DIR_MASK) ==
1101 CAM_DIR_NONE) ? DEVSTAT_NO_DATA :
1102 (ccb->ccb_h.flags & CAM_DIR_OUT) ?
1104 DEVSTAT_READ, NULL, NULL);
1112 cam_freeze_devq(struct cam_path *path)
1114 struct ccb_hdr ccb_h;
1116 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("cam_freeze_devq\n"));
1117 xpt_setup_ccb(&ccb_h, path, /*priority*/1);
1118 ccb_h.func_code = XPT_NOOP;
1119 ccb_h.flags = CAM_DEV_QFREEZE;
1120 xpt_action((union ccb *)&ccb_h);
1124 cam_release_devq(struct cam_path *path, u_int32_t relsim_flags,
1125 u_int32_t openings, u_int32_t arg,
1128 struct ccb_relsim crs;
1130 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("cam_release_devq(%u, %u, %u, %d)\n",
1131 relsim_flags, openings, arg, getcount_only));
1132 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
1133 crs.ccb_h.func_code = XPT_REL_SIMQ;
1134 crs.ccb_h.flags = getcount_only ? CAM_DEV_QFREEZE : 0;
1135 crs.release_flags = relsim_flags;
1136 crs.openings = openings;
1137 crs.release_timeout = arg;
1138 xpt_action((union ccb *)&crs);
1139 return (crs.qfrozen_cnt);
1142 #define saved_ccb_ptr ppriv_ptr0
1144 camperiphdone(struct cam_periph *periph, union ccb *done_ccb)
1146 union ccb *saved_ccb;
1148 struct scsi_start_stop_unit *scsi_cmd;
1149 int error_code, sense_key, asc, ascq;
1151 scsi_cmd = (struct scsi_start_stop_unit *)
1152 &done_ccb->csio.cdb_io.cdb_bytes;
1153 status = done_ccb->ccb_h.status;
1155 if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1156 if (scsi_extract_sense_ccb(done_ccb,
1157 &error_code, &sense_key, &asc, &ascq)) {
1159 * If the error is "invalid field in CDB",
1160 * and the load/eject flag is set, turn the
1161 * flag off and try again. This is just in
1162 * case the drive in question barfs on the
1163 * load eject flag. The CAM code should set
1164 * the load/eject flag by default for
1167 if ((scsi_cmd->opcode == START_STOP_UNIT) &&
1168 ((scsi_cmd->how & SSS_LOEJ) != 0) &&
1169 (asc == 0x24) && (ascq == 0x00)) {
1170 scsi_cmd->how &= ~SSS_LOEJ;
1171 if (status & CAM_DEV_QFRZN) {
1172 cam_release_devq(done_ccb->ccb_h.path,
1174 done_ccb->ccb_h.status &=
1177 xpt_action(done_ccb);
1181 if (cam_periph_error(done_ccb,
1182 0, SF_RETRY_UA | SF_NO_PRINT, NULL) == ERESTART)
1184 if (done_ccb->ccb_h.status & CAM_DEV_QFRZN) {
1185 cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0);
1186 done_ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1190 * If we have successfully taken a device from the not
1191 * ready to ready state, re-scan the device and re-get
1192 * the inquiry information. Many devices (mostly disks)
1193 * don't properly report their inquiry information unless
1196 if (scsi_cmd->opcode == START_STOP_UNIT)
1197 xpt_async(AC_INQ_CHANGED, done_ccb->ccb_h.path, NULL);
1201 * Perform the final retry with the original CCB so that final
1202 * error processing is performed by the owner of the CCB.
1204 saved_ccb = (union ccb *)done_ccb->ccb_h.saved_ccb_ptr;
1205 bcopy(saved_ccb, done_ccb, sizeof(*done_ccb));
1206 xpt_free_ccb(saved_ccb);
1207 if (done_ccb->ccb_h.cbfcnp != camperiphdone)
1208 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1209 xpt_action(done_ccb);
1212 /* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
1213 cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0);
1217 * Generic Async Event handler. Peripheral drivers usually
1218 * filter out the events that require personal attention,
1219 * and leave the rest to this function.
1222 cam_periph_async(struct cam_periph *periph, u_int32_t code,
1223 struct cam_path *path, void *arg)
1226 case AC_LOST_DEVICE:
1227 cam_periph_invalidate(periph);
1235 cam_periph_bus_settle(struct cam_periph *periph, u_int bus_settle)
1237 struct ccb_getdevstats cgds;
1239 xpt_setup_ccb(&cgds.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1240 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1241 xpt_action((union ccb *)&cgds);
1242 cam_periph_freeze_after_event(periph, &cgds.last_reset, bus_settle);
1246 cam_periph_freeze_after_event(struct cam_periph *periph,
1247 struct timeval* event_time, u_int duration_ms)
1249 struct timeval delta;
1250 struct timeval duration_tv;
1252 if (!timevalisset(event_time))
1256 timevalsub(&delta, event_time);
1257 duration_tv.tv_sec = duration_ms / 1000;
1258 duration_tv.tv_usec = (duration_ms % 1000) * 1000;
1259 if (timevalcmp(&delta, &duration_tv, <)) {
1260 timevalsub(&duration_tv, &delta);
1262 duration_ms = duration_tv.tv_sec * 1000;
1263 duration_ms += duration_tv.tv_usec / 1000;
1264 cam_freeze_devq(periph->path);
1265 cam_release_devq(periph->path,
1266 RELSIM_RELEASE_AFTER_TIMEOUT,
1268 /*timeout*/duration_ms,
1269 /*getcount_only*/0);
1275 camperiphscsistatuserror(union ccb *ccb, union ccb **orig_ccb,
1276 cam_flags camflags, u_int32_t sense_flags,
1277 int *openings, u_int32_t *relsim_flags,
1278 u_int32_t *timeout, u_int32_t *action, const char **action_string)
1282 switch (ccb->csio.scsi_status) {
1283 case SCSI_STATUS_OK:
1284 case SCSI_STATUS_COND_MET:
1285 case SCSI_STATUS_INTERMED:
1286 case SCSI_STATUS_INTERMED_COND_MET:
1289 case SCSI_STATUS_CMD_TERMINATED:
1290 case SCSI_STATUS_CHECK_COND:
1291 error = camperiphscsisenseerror(ccb, orig_ccb,
1300 case SCSI_STATUS_QUEUE_FULL:
1303 struct ccb_getdevstats cgds;
1306 * First off, find out what the current
1307 * transaction counts are.
1309 xpt_setup_ccb(&cgds.ccb_h,
1311 CAM_PRIORITY_NORMAL);
1312 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1313 xpt_action((union ccb *)&cgds);
1316 * If we were the only transaction active, treat
1317 * the QUEUE FULL as if it were a BUSY condition.
1319 if (cgds.dev_active != 0) {
1323 * Reduce the number of openings to
1324 * be 1 less than the amount it took
1325 * to get a queue full bounded by the
1326 * minimum allowed tag count for this
1329 total_openings = cgds.dev_active + cgds.dev_openings;
1330 *openings = cgds.dev_active;
1331 if (*openings < cgds.mintags)
1332 *openings = cgds.mintags;
1333 if (*openings < total_openings)
1334 *relsim_flags = RELSIM_ADJUST_OPENINGS;
1337 * Some devices report queue full for
1338 * temporary resource shortages. For
1339 * this reason, we allow a minimum
1340 * tag count to be entered via a
1341 * quirk entry to prevent the queue
1342 * count on these devices from falling
1343 * to a pessimisticly low value. We
1344 * still wait for the next successful
1345 * completion, however, before queueing
1346 * more transactions to the device.
1348 *relsim_flags = RELSIM_RELEASE_AFTER_CMDCMPLT;
1352 *action &= ~SSQ_PRINT_SENSE;
1357 case SCSI_STATUS_BUSY:
1359 * Restart the queue after either another
1360 * command completes or a 1 second timeout.
1362 if (ccb->ccb_h.retry_count > 0) {
1363 ccb->ccb_h.retry_count--;
1365 *relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT
1366 | RELSIM_RELEASE_AFTER_CMDCMPLT;
1372 case SCSI_STATUS_RESERV_CONFLICT:
1381 camperiphscsisenseerror(union ccb *ccb, union ccb **orig,
1382 cam_flags camflags, u_int32_t sense_flags,
1383 int *openings, u_int32_t *relsim_flags,
1384 u_int32_t *timeout, u_int32_t *action, const char **action_string)
1386 struct cam_periph *periph;
1387 union ccb *orig_ccb = ccb;
1388 int error, recoveryccb;
1390 periph = xpt_path_periph(ccb->ccb_h.path);
1391 recoveryccb = (ccb->ccb_h.cbfcnp == camperiphdone);
1392 if ((periph->flags & CAM_PERIPH_RECOVERY_INPROG) && !recoveryccb) {
1394 * If error recovery is already in progress, don't attempt
1395 * to process this error, but requeue it unconditionally
1396 * and attempt to process it once error recovery has
1397 * completed. This failed command is probably related to
1398 * the error that caused the currently active error recovery
1399 * action so our current recovery efforts should also
1400 * address this command. Be aware that the error recovery
1401 * code assumes that only one recovery action is in progress
1402 * on a particular peripheral instance at any given time
1403 * (e.g. only one saved CCB for error recovery) so it is
1404 * imperitive that we don't violate this assumption.
1407 *action &= ~SSQ_PRINT_SENSE;
1409 scsi_sense_action err_action;
1410 struct ccb_getdev cgd;
1413 * Grab the inquiry data for this device.
1415 xpt_setup_ccb(&cgd.ccb_h, ccb->ccb_h.path, CAM_PRIORITY_NORMAL);
1416 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1417 xpt_action((union ccb *)&cgd);
1419 err_action = scsi_error_action(&ccb->csio, &cgd.inq_data,
1421 error = err_action & SS_ERRMASK;
1424 * Do not autostart sequential access devices
1425 * to avoid unexpected tape loading.
1427 if ((err_action & SS_MASK) == SS_START &&
1428 SID_TYPE(&cgd.inq_data) == T_SEQUENTIAL) {
1429 *action_string = "Will not autostart a "
1430 "sequential access device";
1431 goto sense_error_done;
1435 * Avoid recovery recursion if recovery action is the same.
1437 if ((err_action & SS_MASK) >= SS_START && recoveryccb) {
1438 if (((err_action & SS_MASK) == SS_START &&
1439 ccb->csio.cdb_io.cdb_bytes[0] == START_STOP_UNIT) ||
1440 ((err_action & SS_MASK) == SS_TUR &&
1441 (ccb->csio.cdb_io.cdb_bytes[0] == TEST_UNIT_READY))) {
1442 err_action = SS_RETRY|SSQ_DECREMENT_COUNT|EIO;
1443 *relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1449 * If the recovery action will consume a retry,
1450 * make sure we actually have retries available.
1452 if ((err_action & SSQ_DECREMENT_COUNT) != 0) {
1453 if (ccb->ccb_h.retry_count > 0 &&
1454 (periph->flags & CAM_PERIPH_INVALID) == 0)
1455 ccb->ccb_h.retry_count--;
1457 *action_string = "Retries exhausted";
1458 goto sense_error_done;
1462 if ((err_action & SS_MASK) >= SS_START) {
1464 * Do common portions of commands that
1465 * use recovery CCBs.
1467 orig_ccb = xpt_alloc_ccb_nowait();
1468 if (orig_ccb == NULL) {
1469 *action_string = "Can't allocate recovery CCB";
1470 goto sense_error_done;
1473 * Clear freeze flag for original request here, as
1474 * this freeze will be dropped as part of ERESTART.
1476 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1477 bcopy(ccb, orig_ccb, sizeof(*orig_ccb));
1480 switch (err_action & SS_MASK) {
1482 *action_string = "No recovery action needed";
1486 *action_string = "Retrying command (per sense data)";
1490 *action_string = "Unretryable error";
1497 * Send a start unit command to the device, and
1498 * then retry the command.
1500 *action_string = "Attempting to start unit";
1501 periph->flags |= CAM_PERIPH_RECOVERY_INPROG;
1504 * Check for removable media and set
1505 * load/eject flag appropriately.
1507 if (SID_IS_REMOVABLE(&cgd.inq_data))
1512 scsi_start_stop(&ccb->csio,
1526 * Send a Test Unit Ready to the device.
1527 * If the 'many' flag is set, we send 120
1528 * test unit ready commands, one every half
1529 * second. Otherwise, we just send one TUR.
1530 * We only want to do this if the retry
1531 * count has not been exhausted.
1535 if ((err_action & SSQ_MANY) != 0) {
1536 *action_string = "Polling device for readiness";
1539 *action_string = "Testing device for readiness";
1542 periph->flags |= CAM_PERIPH_RECOVERY_INPROG;
1543 scsi_test_unit_ready(&ccb->csio,
1551 * Accomplish our 500ms delay by deferring
1552 * the release of our device queue appropriately.
1554 *relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1559 panic("Unhandled error action %x", err_action);
1562 if ((err_action & SS_MASK) >= SS_START) {
1564 * Drop the priority, so that the recovery
1565 * CCB is the first to execute. Freeze the queue
1566 * after this command is sent so that we can
1567 * restore the old csio and have it queued in
1568 * the proper order before we release normal
1569 * transactions to the device.
1571 ccb->ccb_h.pinfo.priority--;
1572 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1573 ccb->ccb_h.saved_ccb_ptr = orig_ccb;
1579 *action = err_action;
1585 * Generic error handler. Peripheral drivers usually filter
1586 * out the errors that they handle in a unique mannor, then
1587 * call this function.
1590 cam_periph_error(union ccb *ccb, cam_flags camflags,
1591 u_int32_t sense_flags, union ccb *save_ccb)
1593 struct cam_path *newpath;
1594 union ccb *orig_ccb, *scan_ccb;
1595 struct cam_periph *periph;
1596 const char *action_string;
1598 int frozen, error, openings;
1599 u_int32_t action, relsim_flags, timeout;
1601 action = SSQ_PRINT_SENSE;
1602 periph = xpt_path_periph(ccb->ccb_h.path);
1603 action_string = NULL;
1604 status = ccb->ccb_h.status;
1605 frozen = (status & CAM_DEV_QFRZN) != 0;
1606 status &= CAM_STATUS_MASK;
1607 openings = relsim_flags = timeout = 0;
1613 action &= ~SSQ_PRINT_SENSE;
1615 case CAM_SCSI_STATUS_ERROR:
1616 error = camperiphscsistatuserror(ccb, &orig_ccb,
1617 camflags, sense_flags, &openings, &relsim_flags,
1618 &timeout, &action, &action_string);
1620 case CAM_AUTOSENSE_FAIL:
1621 error = EIO; /* we have to kill the command */
1625 case CAM_MSG_REJECT_REC:
1626 /* XXX Don't know that these are correct */
1629 case CAM_SEL_TIMEOUT:
1630 if ((camflags & CAM_RETRY_SELTO) != 0) {
1631 if (ccb->ccb_h.retry_count > 0 &&
1632 (periph->flags & CAM_PERIPH_INVALID) == 0) {
1633 ccb->ccb_h.retry_count--;
1637 * Wait a bit to give the device
1638 * time to recover before we try again.
1640 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1641 timeout = periph_selto_delay;
1644 action_string = "Retries exhausted";
1647 case CAM_DEV_NOT_THERE:
1651 case CAM_REQ_INVALID:
1652 case CAM_PATH_INVALID:
1654 case CAM_PROVIDE_FAIL:
1655 case CAM_REQ_TOO_BIG:
1656 case CAM_LUN_INVALID:
1657 case CAM_TID_INVALID:
1658 case CAM_FUNC_NOTAVAIL:
1661 case CAM_SCSI_BUS_RESET:
1664 * Commands that repeatedly timeout and cause these
1665 * kinds of error recovery actions, should return
1666 * CAM_CMD_TIMEOUT, which allows us to safely assume
1667 * that this command was an innocent bystander to
1668 * these events and should be unconditionally
1671 case CAM_REQUEUE_REQ:
1672 /* Unconditional requeue if device is still there */
1673 if (periph->flags & CAM_PERIPH_INVALID) {
1674 action_string = "Periph was invalidated";
1676 } else if (sense_flags & SF_NO_RETRY) {
1678 action_string = "Retry was blocked";
1681 action &= ~SSQ_PRINT_SENSE;
1684 case CAM_RESRC_UNAVAIL:
1685 /* Wait a bit for the resource shortage to abate. */
1686 timeout = periph_noresrc_delay;
1690 /* Wait a bit for the busy condition to abate. */
1691 timeout = periph_busy_delay;
1693 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1695 case CAM_ATA_STATUS_ERROR:
1696 case CAM_REQ_CMP_ERR:
1697 case CAM_CMD_TIMEOUT:
1698 case CAM_UNEXP_BUSFREE:
1699 case CAM_UNCOR_PARITY:
1700 case CAM_DATA_RUN_ERR:
1702 if (periph->flags & CAM_PERIPH_INVALID) {
1704 action_string = "Periph was invalidated";
1705 } else if (ccb->ccb_h.retry_count == 0) {
1707 action_string = "Retries exhausted";
1708 } else if (sense_flags & SF_NO_RETRY) {
1710 action_string = "Retry was blocked";
1712 ccb->ccb_h.retry_count--;
1718 if ((sense_flags & SF_PRINT_ALWAYS) ||
1719 CAM_DEBUGGED(ccb->ccb_h.path, CAM_DEBUG_INFO))
1720 action |= SSQ_PRINT_SENSE;
1721 else if (sense_flags & SF_NO_PRINT)
1722 action &= ~SSQ_PRINT_SENSE;
1723 if ((action & SSQ_PRINT_SENSE) != 0)
1724 cam_error_print(orig_ccb, CAM_ESF_ALL, CAM_EPF_ALL);
1725 if (error != 0 && (action & SSQ_PRINT_SENSE) != 0) {
1726 if (error != ERESTART) {
1727 if (action_string == NULL)
1728 action_string = "Unretryable error";
1729 xpt_print(ccb->ccb_h.path, "Error %d, %s\n",
1730 error, action_string);
1731 } else if (action_string != NULL)
1732 xpt_print(ccb->ccb_h.path, "%s\n", action_string);
1734 xpt_print(ccb->ccb_h.path, "Retrying command\n");
1737 if ((action & SSQ_LOST) != 0) {
1741 * For a selection timeout, we consider all of the LUNs on
1742 * the target to be gone. If the status is CAM_DEV_NOT_THERE,
1743 * then we only get rid of the device(s) specified by the
1744 * path in the original CCB.
1746 if (status == CAM_SEL_TIMEOUT)
1747 lun_id = CAM_LUN_WILDCARD;
1749 lun_id = xpt_path_lun_id(ccb->ccb_h.path);
1751 /* Should we do more if we can't create the path?? */
1752 if (xpt_create_path(&newpath, periph,
1753 xpt_path_path_id(ccb->ccb_h.path),
1754 xpt_path_target_id(ccb->ccb_h.path),
1755 lun_id) == CAM_REQ_CMP) {
1758 * Let peripheral drivers know that this
1759 * device has gone away.
1761 xpt_async(AC_LOST_DEVICE, newpath, NULL);
1762 xpt_free_path(newpath);
1766 /* Broadcast UNIT ATTENTIONs to all periphs. */
1767 if ((action & SSQ_UA) != 0)
1768 xpt_async(AC_UNIT_ATTENTION, orig_ccb->ccb_h.path, orig_ccb);
1770 /* Rescan target on "Reported LUNs data has changed" */
1771 if ((action & SSQ_RESCAN) != 0) {
1772 if (xpt_create_path(&newpath, NULL,
1773 xpt_path_path_id(ccb->ccb_h.path),
1774 xpt_path_target_id(ccb->ccb_h.path),
1775 CAM_LUN_WILDCARD) == CAM_REQ_CMP) {
1777 scan_ccb = xpt_alloc_ccb_nowait();
1778 if (scan_ccb != NULL) {
1779 scan_ccb->ccb_h.path = newpath;
1780 scan_ccb->ccb_h.func_code = XPT_SCAN_TGT;
1781 scan_ccb->crcn.flags = 0;
1782 xpt_rescan(scan_ccb);
1785 "Can't allocate CCB to rescan target\n");
1786 xpt_free_path(newpath);
1791 /* Attempt a retry */
1792 if (error == ERESTART || error == 0) {
1794 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1795 if (error == ERESTART)
1798 cam_release_devq(ccb->ccb_h.path,
1802 /*getcount_only*/0);