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);
89 static void cam_periph_devctl_notify(union ccb *ccb);
91 static int nperiph_drivers;
92 static int initialized = 0;
93 struct periph_driver **periph_drivers;
95 static MALLOC_DEFINE(M_CAMPERIPH, "CAM periph", "CAM peripheral buffers");
97 static int periph_selto_delay = 1000;
98 TUNABLE_INT("kern.cam.periph_selto_delay", &periph_selto_delay);
99 static int periph_noresrc_delay = 500;
100 TUNABLE_INT("kern.cam.periph_noresrc_delay", &periph_noresrc_delay);
101 static int periph_busy_delay = 500;
102 TUNABLE_INT("kern.cam.periph_busy_delay", &periph_busy_delay);
106 periphdriver_register(void *data)
108 struct periph_driver *drv = (struct periph_driver *)data;
109 struct periph_driver **newdrivers, **old;
113 ndrivers = nperiph_drivers + 2;
114 newdrivers = malloc(sizeof(*newdrivers) * ndrivers, M_CAMPERIPH,
117 if (ndrivers != nperiph_drivers + 2) {
119 * Lost race against itself; go around.
122 free(newdrivers, M_CAMPERIPH);
126 bcopy(periph_drivers, newdrivers,
127 sizeof(*newdrivers) * nperiph_drivers);
128 newdrivers[nperiph_drivers] = drv;
129 newdrivers[nperiph_drivers + 1] = NULL;
130 old = periph_drivers;
131 periph_drivers = newdrivers;
135 free(old, M_CAMPERIPH);
136 /* If driver marked as early or it is late now, initialize it. */
137 if (((drv->flags & CAM_PERIPH_DRV_EARLY) != 0 && initialized > 0) ||
143 periphdriver_init(int level)
147 initialized = max(initialized, level);
148 for (i = 0; periph_drivers[i] != NULL; i++) {
149 early = (periph_drivers[i]->flags & CAM_PERIPH_DRV_EARLY) ? 1 : 2;
150 if (early == initialized)
151 (*periph_drivers[i]->init)();
156 cam_periph_alloc(periph_ctor_t *periph_ctor,
157 periph_oninv_t *periph_oninvalidate,
158 periph_dtor_t *periph_dtor, periph_start_t *periph_start,
159 char *name, cam_periph_type type, struct cam_path *path,
160 ac_callback_t *ac_callback, ac_code code, void *arg)
162 struct periph_driver **p_drv;
164 struct cam_periph *periph;
165 struct cam_periph *cur_periph;
167 target_id_t target_id;
174 * Handle Hot-Plug scenarios. If there is already a peripheral
175 * of our type assigned to this path, we are likely waiting for
176 * final close on an old, invalidated, peripheral. If this is
177 * the case, queue up a deferred call to the peripheral's async
178 * handler. If it looks like a mistaken re-allocation, complain.
180 if ((periph = cam_periph_find(path, name)) != NULL) {
182 if ((periph->flags & CAM_PERIPH_INVALID) != 0
183 && (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) == 0) {
184 periph->flags |= CAM_PERIPH_NEW_DEV_FOUND;
185 periph->deferred_callback = ac_callback;
186 periph->deferred_ac = code;
187 return (CAM_REQ_INPROG);
189 printf("cam_periph_alloc: attempt to re-allocate "
190 "valid device %s%d rejected flags %#x "
191 "refcount %d\n", periph->periph_name,
192 periph->unit_number, periph->flags,
195 return (CAM_REQ_INVALID);
198 periph = (struct cam_periph *)malloc(sizeof(*periph), M_CAMPERIPH,
202 return (CAM_RESRC_UNAVAIL);
207 sim = xpt_path_sim(path);
208 path_id = xpt_path_path_id(path);
209 target_id = xpt_path_target_id(path);
210 lun_id = xpt_path_lun_id(path);
211 periph->periph_start = periph_start;
212 periph->periph_dtor = periph_dtor;
213 periph->periph_oninval = periph_oninvalidate;
215 periph->periph_name = name;
216 periph->scheduled_priority = CAM_PRIORITY_NONE;
217 periph->immediate_priority = CAM_PRIORITY_NONE;
218 periph->refcount = 1; /* Dropped by invalidation. */
220 SLIST_INIT(&periph->ccb_list);
221 status = xpt_create_path(&path, periph, path_id, target_id, lun_id);
222 if (status != CAM_REQ_CMP)
227 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
228 if (strcmp((*p_drv)->driver_name, name) == 0)
231 if (*p_drv == NULL) {
232 printf("cam_periph_alloc: invalid periph name '%s'\n", name);
234 xpt_free_path(periph->path);
235 free(periph, M_CAMPERIPH);
236 return (CAM_REQ_INVALID);
238 periph->unit_number = camperiphunit(*p_drv, path_id, target_id, lun_id);
239 cur_periph = TAILQ_FIRST(&(*p_drv)->units);
240 while (cur_periph != NULL
241 && cur_periph->unit_number < periph->unit_number)
242 cur_periph = TAILQ_NEXT(cur_periph, unit_links);
243 if (cur_periph != NULL) {
244 KASSERT(cur_periph->unit_number != periph->unit_number, ("duplicate units on periph list"));
245 TAILQ_INSERT_BEFORE(cur_periph, periph, unit_links);
247 TAILQ_INSERT_TAIL(&(*p_drv)->units, periph, unit_links);
248 (*p_drv)->generation++;
254 status = xpt_add_periph(periph);
255 if (status != CAM_REQ_CMP)
259 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph created\n"));
261 status = periph_ctor(periph, arg);
263 if (status == CAM_REQ_CMP)
267 switch (init_level) {
269 /* Initialized successfully */
272 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph destroyed\n"));
273 xpt_remove_periph(periph);
277 TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
279 xpt_free_path(periph->path);
282 free(periph, M_CAMPERIPH);
285 /* No cleanup to perform. */
288 panic("%s: Unknown init level", __func__);
294 * Find a peripheral structure with the specified path, target, lun,
295 * and (optionally) type. If the name is NULL, this function will return
296 * the first peripheral driver that matches the specified path.
299 cam_periph_find(struct cam_path *path, char *name)
301 struct periph_driver **p_drv;
302 struct cam_periph *periph;
305 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
307 if (name != NULL && (strcmp((*p_drv)->driver_name, name) != 0))
310 TAILQ_FOREACH(periph, &(*p_drv)->units, unit_links) {
311 if (xpt_path_comp(periph->path, path) == 0) {
313 cam_periph_assert(periph, MA_OWNED);
327 * Find peripheral driver instances attached to the specified path.
330 cam_periph_list(struct cam_path *path, struct sbuf *sb)
332 struct sbuf local_sb;
333 struct periph_driver **p_drv;
334 struct cam_periph *periph;
340 sbuf_new(&local_sb, NULL, sbuf_alloc_len, SBUF_FIXEDLEN);
343 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
345 TAILQ_FOREACH(periph, &(*p_drv)->units, unit_links) {
346 if (xpt_path_comp(periph->path, path) != 0)
349 if (sbuf_len(&local_sb) != 0)
350 sbuf_cat(&local_sb, ",");
352 sbuf_printf(&local_sb, "%s%d", periph->periph_name,
353 periph->unit_number);
355 if (sbuf_error(&local_sb) == ENOMEM) {
358 sbuf_delete(&local_sb);
365 sbuf_finish(&local_sb);
366 sbuf_cpy(sb, sbuf_data(&local_sb));
367 sbuf_delete(&local_sb);
372 cam_periph_acquire(struct cam_periph *periph)
376 status = CAM_REQ_CMP_ERR;
381 if ((periph->flags & CAM_PERIPH_INVALID) == 0) {
383 status = CAM_REQ_CMP;
391 cam_periph_doacquire(struct cam_periph *periph)
395 KASSERT(periph->refcount >= 1,
396 ("cam_periph_doacquire() with refcount == %d", periph->refcount));
402 cam_periph_release_locked_buses(struct cam_periph *periph)
405 cam_periph_assert(periph, MA_OWNED);
406 KASSERT(periph->refcount >= 1, ("periph->refcount >= 1"));
407 if (--periph->refcount == 0)
408 camperiphfree(periph);
412 cam_periph_release_locked(struct cam_periph *periph)
419 cam_periph_release_locked_buses(periph);
424 cam_periph_release(struct cam_periph *periph)
431 cam_periph_assert(periph, MA_NOTOWNED);
432 mtx = cam_periph_mtx(periph);
434 cam_periph_release_locked(periph);
439 cam_periph_hold(struct cam_periph *periph, int priority)
444 * Increment the reference count on the peripheral
445 * while we wait for our lock attempt to succeed
446 * to ensure the peripheral doesn't disappear out
447 * from user us while we sleep.
450 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
453 cam_periph_assert(periph, MA_OWNED);
454 while ((periph->flags & CAM_PERIPH_LOCKED) != 0) {
455 periph->flags |= CAM_PERIPH_LOCK_WANTED;
456 if ((error = cam_periph_sleep(periph, periph, priority,
457 "caplck", 0)) != 0) {
458 cam_periph_release_locked(periph);
461 if (periph->flags & CAM_PERIPH_INVALID) {
462 cam_periph_release_locked(periph);
467 periph->flags |= CAM_PERIPH_LOCKED;
472 cam_periph_unhold(struct cam_periph *periph)
475 cam_periph_assert(periph, MA_OWNED);
477 periph->flags &= ~CAM_PERIPH_LOCKED;
478 if ((periph->flags & CAM_PERIPH_LOCK_WANTED) != 0) {
479 periph->flags &= ~CAM_PERIPH_LOCK_WANTED;
483 cam_periph_release_locked(periph);
487 * Look for the next unit number that is not currently in use for this
488 * peripheral type starting at "newunit". Also exclude unit numbers that
489 * are reserved by for future "hardwiring" unless we already know that this
490 * is a potential wired device. Only assume that the device is "wired" the
491 * first time through the loop since after that we'll be looking at unit
492 * numbers that did not match a wiring entry.
495 camperiphnextunit(struct periph_driver *p_drv, u_int newunit, int wired,
496 path_id_t pathid, target_id_t target, lun_id_t lun)
498 struct cam_periph *periph;
500 int i, val, dunit, r;
501 const char *dname, *strval;
503 periph_name = p_drv->driver_name;
506 for (periph = TAILQ_FIRST(&p_drv->units);
507 periph != NULL && periph->unit_number != newunit;
508 periph = TAILQ_NEXT(periph, unit_links))
511 if (periph != NULL && periph->unit_number == newunit) {
513 xpt_print(periph->path, "Duplicate Wired "
515 xpt_print(periph->path, "Second device (%s "
516 "device at scbus%d target %d lun %d) will "
517 "not be wired\n", periph_name, pathid,
527 * Don't match entries like "da 4" as a wired down
528 * device, but do match entries like "da 4 target 5"
529 * or even "da 4 scbus 1".
534 r = resource_find_dev(&i, dname, &dunit, NULL, NULL);
537 /* if no "target" and no specific scbus, skip */
538 if (resource_int_value(dname, dunit, "target", &val) &&
539 (resource_string_value(dname, dunit, "at",&strval)||
540 strcmp(strval, "scbus") == 0))
542 if (newunit == dunit)
552 camperiphunit(struct periph_driver *p_drv, path_id_t pathid,
553 target_id_t target, lun_id_t lun)
556 int wired, i, val, dunit;
557 const char *dname, *strval;
558 char pathbuf[32], *periph_name;
560 periph_name = p_drv->driver_name;
561 snprintf(pathbuf, sizeof(pathbuf), "scbus%d", pathid);
565 for (wired = 0; resource_find_dev(&i, dname, &dunit, NULL, NULL) == 0;
567 if (resource_string_value(dname, dunit, "at", &strval) == 0) {
568 if (strcmp(strval, pathbuf) != 0)
572 if (resource_int_value(dname, dunit, "target", &val) == 0) {
577 if (resource_int_value(dname, dunit, "lun", &val) == 0) {
589 * Either start from 0 looking for the next unit or from
590 * the unit number given in the resource config. This way,
591 * if we have wildcard matches, we don't return the same
594 unit = camperiphnextunit(p_drv, unit, wired, pathid, target, lun);
600 cam_periph_invalidate(struct cam_periph *periph)
603 cam_periph_assert(periph, MA_OWNED);
605 * We only call this routine the first time a peripheral is
608 if ((periph->flags & CAM_PERIPH_INVALID) != 0)
611 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph invalidated\n"));
612 if ((periph->flags & CAM_PERIPH_ANNOUNCED) && !rebooting)
613 xpt_denounce_periph(periph);
614 periph->flags |= CAM_PERIPH_INVALID;
615 periph->flags &= ~CAM_PERIPH_NEW_DEV_FOUND;
616 if (periph->periph_oninval != NULL)
617 periph->periph_oninval(periph);
618 cam_periph_release_locked(periph);
622 camperiphfree(struct cam_periph *periph)
624 struct periph_driver **p_drv;
626 cam_periph_assert(periph, MA_OWNED);
627 KASSERT(periph->periph_allocating == 0, ("%s%d: freed while allocating",
628 periph->periph_name, periph->unit_number));
629 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
630 if (strcmp((*p_drv)->driver_name, periph->periph_name) == 0)
633 if (*p_drv == NULL) {
634 printf("camperiphfree: attempt to free non-existant periph\n");
639 * We need to set this flag before dropping the topology lock, to
640 * let anyone who is traversing the list that this peripheral is
641 * about to be freed, and there will be no more reference count
644 periph->flags |= CAM_PERIPH_FREE;
647 * The peripheral destructor semantics dictate calling with only the
648 * SIM mutex held. Since it might sleep, it should not be called
649 * with the topology lock held.
654 * We need to call the peripheral destructor prior to removing the
655 * peripheral from the list. Otherwise, we risk running into a
656 * scenario where the peripheral unit number may get reused
657 * (because it has been removed from the list), but some resources
658 * used by the peripheral are still hanging around. In particular,
659 * the devfs nodes used by some peripherals like the pass(4) driver
660 * aren't fully cleaned up until the destructor is run. If the
661 * unit number is reused before the devfs instance is fully gone,
664 if (periph->periph_dtor != NULL)
665 periph->periph_dtor(periph);
668 * The peripheral list is protected by the topology lock.
672 TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
673 (*p_drv)->generation++;
675 xpt_remove_periph(periph);
678 if ((periph->flags & CAM_PERIPH_ANNOUNCED) && !rebooting)
679 xpt_print(periph->path, "Periph destroyed\n");
681 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph destroyed\n"));
683 if (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) {
687 switch (periph->deferred_ac) {
688 case AC_FOUND_DEVICE:
689 ccb.ccb_h.func_code = XPT_GDEV_TYPE;
690 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
694 case AC_PATH_REGISTERED:
695 ccb.ccb_h.func_code = XPT_PATH_INQ;
696 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
704 periph->deferred_callback(NULL, periph->deferred_ac,
707 xpt_free_path(periph->path);
708 free(periph, M_CAMPERIPH);
713 * Map user virtual pointers into kernel virtual address space, so we can
714 * access the memory. This is now a generic function that centralizes most
715 * of the sanity checks on the data flags, if any.
716 * This also only works for up to MAXPHYS memory. Since we use
717 * buffers to map stuff in and out, we're limited to the buffer size.
720 cam_periph_mapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo,
724 int flags[CAM_PERIPH_MAXMAPS];
725 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
726 u_int32_t lengths[CAM_PERIPH_MAXMAPS];
727 u_int32_t dirs[CAM_PERIPH_MAXMAPS];
730 maxmap = DFLTPHYS; /* traditional default */
731 else if (maxmap > MAXPHYS)
732 maxmap = MAXPHYS; /* for safety */
733 switch(ccb->ccb_h.func_code) {
735 if (ccb->cdm.match_buf_len == 0) {
736 printf("cam_periph_mapmem: invalid match buffer "
740 if (ccb->cdm.pattern_buf_len > 0) {
741 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
742 lengths[0] = ccb->cdm.pattern_buf_len;
743 dirs[0] = CAM_DIR_OUT;
744 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
745 lengths[1] = ccb->cdm.match_buf_len;
746 dirs[1] = CAM_DIR_IN;
749 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
750 lengths[0] = ccb->cdm.match_buf_len;
751 dirs[0] = CAM_DIR_IN;
755 * This request will not go to the hardware, no reason
756 * to be so strict. vmapbuf() is able to map up to MAXPHYS.
761 case XPT_CONT_TARGET_IO:
762 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
764 if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR)
766 data_ptrs[0] = &ccb->csio.data_ptr;
767 lengths[0] = ccb->csio.dxfer_len;
768 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
772 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
774 if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR)
776 data_ptrs[0] = &ccb->ataio.data_ptr;
777 lengths[0] = ccb->ataio.dxfer_len;
778 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
782 data_ptrs[0] = &ccb->smpio.smp_request;
783 lengths[0] = ccb->smpio.smp_request_len;
784 dirs[0] = CAM_DIR_OUT;
785 data_ptrs[1] = &ccb->smpio.smp_response;
786 lengths[1] = ccb->smpio.smp_response_len;
787 dirs[1] = CAM_DIR_IN;
790 case XPT_DEV_ADVINFO:
791 if (ccb->cdai.bufsiz == 0)
794 data_ptrs[0] = (uint8_t **)&ccb->cdai.buf;
795 lengths[0] = ccb->cdai.bufsiz;
796 dirs[0] = CAM_DIR_IN;
800 * This request will not go to the hardware, no reason
801 * to be so strict. vmapbuf() is able to map up to MAXPHYS.
807 break; /* NOTREACHED */
811 * Check the transfer length and permissions first, so we don't
812 * have to unmap any previously mapped buffers.
814 for (i = 0; i < numbufs; i++) {
819 * The userland data pointer passed in may not be page
820 * aligned. vmapbuf() truncates the address to a page
821 * boundary, so if the address isn't page aligned, we'll
822 * need enough space for the given transfer length, plus
823 * whatever extra space is necessary to make it to the page
827 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)) > maxmap){
828 printf("cam_periph_mapmem: attempt to map %lu bytes, "
829 "which is greater than %lu\n",
831 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)),
836 if (dirs[i] & CAM_DIR_OUT) {
837 flags[i] = BIO_WRITE;
840 if (dirs[i] & CAM_DIR_IN) {
847 * This keeps the kernel stack of current thread from getting
848 * swapped. In low-memory situations where the kernel stack might
849 * otherwise get swapped out, this holds it and allows the thread
850 * to make progress and release the kernel mapped pages sooner.
852 * XXX KDM should I use P_NOSWAP instead?
856 for (i = 0; i < numbufs; i++) {
860 mapinfo->bp[i] = getpbuf(NULL);
862 /* put our pointer in the data slot */
863 mapinfo->bp[i]->b_data = *data_ptrs[i];
865 /* save the user's data address */
866 mapinfo->bp[i]->b_caller1 = *data_ptrs[i];
868 /* set the transfer length, we know it's < MAXPHYS */
869 mapinfo->bp[i]->b_bufsize = lengths[i];
871 /* set the direction */
872 mapinfo->bp[i]->b_iocmd = flags[i];
875 * Map the buffer into kernel memory.
877 * Note that useracc() alone is not a sufficient test.
878 * vmapbuf() can still fail due to a smaller file mapped
879 * into a larger area of VM, or if userland races against
880 * vmapbuf() after the useracc() check.
882 if (vmapbuf(mapinfo->bp[i], 1) < 0) {
883 for (j = 0; j < i; ++j) {
884 *data_ptrs[j] = mapinfo->bp[j]->b_caller1;
885 vunmapbuf(mapinfo->bp[j]);
886 relpbuf(mapinfo->bp[j], NULL);
888 relpbuf(mapinfo->bp[i], NULL);
893 /* set our pointer to the new mapped area */
894 *data_ptrs[i] = mapinfo->bp[i]->b_data;
896 mapinfo->num_bufs_used++;
900 * Now that we've gotten this far, change ownership to the kernel
901 * of the buffers so that we don't run afoul of returning to user
902 * space with locks (on the buffer) held.
904 for (i = 0; i < numbufs; i++) {
905 BUF_KERNPROC(mapinfo->bp[i]);
913 * Unmap memory segments mapped into kernel virtual address space by
914 * cam_periph_mapmem().
917 cam_periph_unmapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
920 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
922 if (mapinfo->num_bufs_used <= 0) {
923 /* nothing to free and the process wasn't held. */
927 switch (ccb->ccb_h.func_code) {
929 numbufs = min(mapinfo->num_bufs_used, 2);
932 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
934 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
935 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
939 case XPT_CONT_TARGET_IO:
940 data_ptrs[0] = &ccb->csio.data_ptr;
941 numbufs = min(mapinfo->num_bufs_used, 1);
944 data_ptrs[0] = &ccb->ataio.data_ptr;
945 numbufs = min(mapinfo->num_bufs_used, 1);
948 numbufs = min(mapinfo->num_bufs_used, 2);
949 data_ptrs[0] = &ccb->smpio.smp_request;
950 data_ptrs[1] = &ccb->smpio.smp_response;
952 case XPT_DEV_ADVINFO:
953 numbufs = min(mapinfo->num_bufs_used, 1);
954 data_ptrs[0] = (uint8_t **)&ccb->cdai.buf;
957 /* allow ourselves to be swapped once again */
960 break; /* NOTREACHED */
963 for (i = 0; i < numbufs; i++) {
964 /* Set the user's pointer back to the original value */
965 *data_ptrs[i] = mapinfo->bp[i]->b_caller1;
967 /* unmap the buffer */
968 vunmapbuf(mapinfo->bp[i]);
970 /* release the buffer */
971 relpbuf(mapinfo->bp[i], NULL);
974 /* allow ourselves to be swapped once again */
979 cam_periph_ioctl(struct cam_periph *periph, u_long cmd, caddr_t addr,
980 int (*error_routine)(union ccb *ccb,
982 u_int32_t sense_flags))
992 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
993 xpt_setup_ccb(&ccb->ccb_h,
995 CAM_PRIORITY_NORMAL);
996 ccb->ccb_h.func_code = XPT_GDEVLIST;
999 * Basically, the point of this is that we go through
1000 * getting the list of devices, until we find a passthrough
1001 * device. In the current version of the CAM code, the
1002 * only way to determine what type of device we're dealing
1003 * with is by its name.
1005 while (found == 0) {
1006 ccb->cgdl.index = 0;
1007 ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS;
1008 while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) {
1010 /* we want the next device in the list */
1012 if (strncmp(ccb->cgdl.periph_name,
1018 if ((ccb->cgdl.status == CAM_GDEVLIST_LAST_DEVICE) &&
1020 ccb->cgdl.periph_name[0] = '\0';
1021 ccb->cgdl.unit_number = 0;
1026 /* copy the result back out */
1027 bcopy(ccb, addr, sizeof(union ccb));
1029 /* and release the ccb */
1030 xpt_release_ccb(ccb);
1041 cam_periph_done_panic(struct cam_periph *periph, union ccb *done_ccb)
1044 panic("%s: already done with ccb %p", __func__, done_ccb);
1048 cam_periph_done(struct cam_periph *periph, union ccb *done_ccb)
1051 /* Caller will release the CCB */
1052 xpt_path_assert(done_ccb->ccb_h.path, MA_OWNED);
1053 done_ccb->ccb_h.cbfcnp = cam_periph_done_panic;
1054 wakeup(&done_ccb->ccb_h.cbfcnp);
1058 cam_periph_ccbwait(union ccb *ccb)
1061 if ((ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
1062 while (ccb->ccb_h.cbfcnp != cam_periph_done_panic)
1063 xpt_path_sleep(ccb->ccb_h.path, &ccb->ccb_h.cbfcnp,
1064 PRIBIO, "cbwait", 0);
1066 KASSERT(ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX &&
1067 (ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG,
1068 ("%s: proceeding with incomplete ccb: ccb=%p, func_code=%#x, "
1069 "status=%#x, index=%d", __func__, ccb, ccb->ccb_h.func_code,
1070 ccb->ccb_h.status, ccb->ccb_h.pinfo.index));
1074 cam_periph_runccb(union ccb *ccb,
1075 int (*error_routine)(union ccb *ccb,
1077 u_int32_t sense_flags),
1078 cam_flags camflags, u_int32_t sense_flags,
1081 struct bintime *starttime;
1082 struct bintime ltime;
1086 xpt_path_assert(ccb->ccb_h.path, MA_OWNED);
1087 KASSERT((ccb->ccb_h.flags & CAM_UNLOCKED) == 0,
1088 ("%s: ccb=%p, func_code=%#x, flags=%#x", __func__, ccb,
1089 ccb->ccb_h.func_code, ccb->ccb_h.flags));
1092 * If the user has supplied a stats structure, and if we understand
1093 * this particular type of ccb, record the transaction start.
1095 if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO ||
1096 ccb->ccb_h.func_code == XPT_ATA_IO)) {
1098 binuptime(starttime);
1099 devstat_start_transaction(ds, starttime);
1102 ccb->ccb_h.cbfcnp = cam_periph_done;
1106 cam_periph_ccbwait(ccb);
1107 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
1109 else if (error_routine != NULL) {
1110 ccb->ccb_h.cbfcnp = cam_periph_done;
1111 error = (*error_routine)(ccb, camflags, sense_flags);
1115 } while (error == ERESTART);
1117 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1118 cam_release_devq(ccb->ccb_h.path,
1119 /* relsim_flags */0,
1122 /* getcount_only */ FALSE);
1123 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1127 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1128 devstat_end_transaction(ds,
1129 ccb->csio.dxfer_len - ccb->csio.resid,
1130 ccb->csio.tag_action & 0x3,
1131 ((ccb->ccb_h.flags & CAM_DIR_MASK) ==
1132 CAM_DIR_NONE) ? DEVSTAT_NO_DATA :
1133 (ccb->ccb_h.flags & CAM_DIR_OUT) ?
1135 DEVSTAT_READ, NULL, starttime);
1136 } else if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1137 devstat_end_transaction(ds,
1138 ccb->ataio.dxfer_len - ccb->ataio.resid,
1139 0, /* Not used in ATA */
1140 ((ccb->ccb_h.flags & CAM_DIR_MASK) ==
1141 CAM_DIR_NONE) ? DEVSTAT_NO_DATA :
1142 (ccb->ccb_h.flags & CAM_DIR_OUT) ?
1144 DEVSTAT_READ, NULL, starttime);
1152 cam_freeze_devq(struct cam_path *path)
1154 struct ccb_hdr ccb_h;
1156 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("cam_freeze_devq\n"));
1157 xpt_setup_ccb(&ccb_h, path, /*priority*/1);
1158 ccb_h.func_code = XPT_NOOP;
1159 ccb_h.flags = CAM_DEV_QFREEZE;
1160 xpt_action((union ccb *)&ccb_h);
1164 cam_release_devq(struct cam_path *path, u_int32_t relsim_flags,
1165 u_int32_t openings, u_int32_t arg,
1168 struct ccb_relsim crs;
1170 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("cam_release_devq(%u, %u, %u, %d)\n",
1171 relsim_flags, openings, arg, getcount_only));
1172 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
1173 crs.ccb_h.func_code = XPT_REL_SIMQ;
1174 crs.ccb_h.flags = getcount_only ? CAM_DEV_QFREEZE : 0;
1175 crs.release_flags = relsim_flags;
1176 crs.openings = openings;
1177 crs.release_timeout = arg;
1178 xpt_action((union ccb *)&crs);
1179 return (crs.qfrozen_cnt);
1182 #define saved_ccb_ptr ppriv_ptr0
1184 camperiphdone(struct cam_periph *periph, union ccb *done_ccb)
1186 union ccb *saved_ccb;
1188 struct scsi_start_stop_unit *scsi_cmd;
1189 int error_code, sense_key, asc, ascq;
1191 scsi_cmd = (struct scsi_start_stop_unit *)
1192 &done_ccb->csio.cdb_io.cdb_bytes;
1193 status = done_ccb->ccb_h.status;
1195 if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1196 if (scsi_extract_sense_ccb(done_ccb,
1197 &error_code, &sense_key, &asc, &ascq)) {
1199 * If the error is "invalid field in CDB",
1200 * and the load/eject flag is set, turn the
1201 * flag off and try again. This is just in
1202 * case the drive in question barfs on the
1203 * load eject flag. The CAM code should set
1204 * the load/eject flag by default for
1207 if ((scsi_cmd->opcode == START_STOP_UNIT) &&
1208 ((scsi_cmd->how & SSS_LOEJ) != 0) &&
1209 (asc == 0x24) && (ascq == 0x00)) {
1210 scsi_cmd->how &= ~SSS_LOEJ;
1211 if (status & CAM_DEV_QFRZN) {
1212 cam_release_devq(done_ccb->ccb_h.path,
1214 done_ccb->ccb_h.status &=
1217 xpt_action(done_ccb);
1221 if (cam_periph_error(done_ccb,
1222 0, SF_RETRY_UA | SF_NO_PRINT, NULL) == ERESTART)
1224 if (done_ccb->ccb_h.status & CAM_DEV_QFRZN) {
1225 cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0);
1226 done_ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1230 * If we have successfully taken a device from the not
1231 * ready to ready state, re-scan the device and re-get
1232 * the inquiry information. Many devices (mostly disks)
1233 * don't properly report their inquiry information unless
1236 if (scsi_cmd->opcode == START_STOP_UNIT)
1237 xpt_async(AC_INQ_CHANGED, done_ccb->ccb_h.path, NULL);
1241 * Perform the final retry with the original CCB so that final
1242 * error processing is performed by the owner of the CCB.
1244 saved_ccb = (union ccb *)done_ccb->ccb_h.saved_ccb_ptr;
1245 bcopy(saved_ccb, done_ccb, sizeof(*done_ccb));
1246 xpt_free_ccb(saved_ccb);
1247 if (done_ccb->ccb_h.cbfcnp != camperiphdone)
1248 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1249 xpt_action(done_ccb);
1252 /* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
1253 cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0);
1257 * Generic Async Event handler. Peripheral drivers usually
1258 * filter out the events that require personal attention,
1259 * and leave the rest to this function.
1262 cam_periph_async(struct cam_periph *periph, u_int32_t code,
1263 struct cam_path *path, void *arg)
1266 case AC_LOST_DEVICE:
1267 cam_periph_invalidate(periph);
1275 cam_periph_bus_settle(struct cam_periph *periph, u_int bus_settle)
1277 struct ccb_getdevstats cgds;
1279 xpt_setup_ccb(&cgds.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1280 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1281 xpt_action((union ccb *)&cgds);
1282 cam_periph_freeze_after_event(periph, &cgds.last_reset, bus_settle);
1286 cam_periph_freeze_after_event(struct cam_periph *periph,
1287 struct timeval* event_time, u_int duration_ms)
1289 struct timeval delta;
1290 struct timeval duration_tv;
1292 if (!timevalisset(event_time))
1296 timevalsub(&delta, event_time);
1297 duration_tv.tv_sec = duration_ms / 1000;
1298 duration_tv.tv_usec = (duration_ms % 1000) * 1000;
1299 if (timevalcmp(&delta, &duration_tv, <)) {
1300 timevalsub(&duration_tv, &delta);
1302 duration_ms = duration_tv.tv_sec * 1000;
1303 duration_ms += duration_tv.tv_usec / 1000;
1304 cam_freeze_devq(periph->path);
1305 cam_release_devq(periph->path,
1306 RELSIM_RELEASE_AFTER_TIMEOUT,
1308 /*timeout*/duration_ms,
1309 /*getcount_only*/0);
1315 camperiphscsistatuserror(union ccb *ccb, union ccb **orig_ccb,
1316 cam_flags camflags, u_int32_t sense_flags,
1317 int *openings, u_int32_t *relsim_flags,
1318 u_int32_t *timeout, u_int32_t *action, const char **action_string)
1322 switch (ccb->csio.scsi_status) {
1323 case SCSI_STATUS_OK:
1324 case SCSI_STATUS_COND_MET:
1325 case SCSI_STATUS_INTERMED:
1326 case SCSI_STATUS_INTERMED_COND_MET:
1329 case SCSI_STATUS_CMD_TERMINATED:
1330 case SCSI_STATUS_CHECK_COND:
1331 error = camperiphscsisenseerror(ccb, orig_ccb,
1340 case SCSI_STATUS_QUEUE_FULL:
1343 struct ccb_getdevstats cgds;
1346 * First off, find out what the current
1347 * transaction counts are.
1349 xpt_setup_ccb(&cgds.ccb_h,
1351 CAM_PRIORITY_NORMAL);
1352 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1353 xpt_action((union ccb *)&cgds);
1356 * If we were the only transaction active, treat
1357 * the QUEUE FULL as if it were a BUSY condition.
1359 if (cgds.dev_active != 0) {
1363 * Reduce the number of openings to
1364 * be 1 less than the amount it took
1365 * to get a queue full bounded by the
1366 * minimum allowed tag count for this
1369 total_openings = cgds.dev_active + cgds.dev_openings;
1370 *openings = cgds.dev_active;
1371 if (*openings < cgds.mintags)
1372 *openings = cgds.mintags;
1373 if (*openings < total_openings)
1374 *relsim_flags = RELSIM_ADJUST_OPENINGS;
1377 * Some devices report queue full for
1378 * temporary resource shortages. For
1379 * this reason, we allow a minimum
1380 * tag count to be entered via a
1381 * quirk entry to prevent the queue
1382 * count on these devices from falling
1383 * to a pessimisticly low value. We
1384 * still wait for the next successful
1385 * completion, however, before queueing
1386 * more transactions to the device.
1388 *relsim_flags = RELSIM_RELEASE_AFTER_CMDCMPLT;
1392 *action &= ~SSQ_PRINT_SENSE;
1397 case SCSI_STATUS_BUSY:
1399 * Restart the queue after either another
1400 * command completes or a 1 second timeout.
1402 if ((sense_flags & SF_RETRY_BUSY) != 0 ||
1403 (ccb->ccb_h.retry_count--) > 0) {
1405 *relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT
1406 | RELSIM_RELEASE_AFTER_CMDCMPLT;
1412 case SCSI_STATUS_RESERV_CONFLICT:
1421 camperiphscsisenseerror(union ccb *ccb, union ccb **orig,
1422 cam_flags camflags, u_int32_t sense_flags,
1423 int *openings, u_int32_t *relsim_flags,
1424 u_int32_t *timeout, u_int32_t *action, const char **action_string)
1426 struct cam_periph *periph;
1427 union ccb *orig_ccb = ccb;
1428 int error, recoveryccb;
1430 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
1431 if (ccb->ccb_h.func_code == XPT_SCSI_IO && ccb->csio.bio != NULL)
1432 biotrack(ccb->csio.bio, __func__);
1435 periph = xpt_path_periph(ccb->ccb_h.path);
1436 recoveryccb = (ccb->ccb_h.cbfcnp == camperiphdone);
1437 if ((periph->flags & CAM_PERIPH_RECOVERY_INPROG) && !recoveryccb) {
1439 * If error recovery is already in progress, don't attempt
1440 * to process this error, but requeue it unconditionally
1441 * and attempt to process it once error recovery has
1442 * completed. This failed command is probably related to
1443 * the error that caused the currently active error recovery
1444 * action so our current recovery efforts should also
1445 * address this command. Be aware that the error recovery
1446 * code assumes that only one recovery action is in progress
1447 * on a particular peripheral instance at any given time
1448 * (e.g. only one saved CCB for error recovery) so it is
1449 * imperitive that we don't violate this assumption.
1452 *action &= ~SSQ_PRINT_SENSE;
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 err_action = scsi_error_action(&ccb->csio, &cgd.inq_data,
1466 error = err_action & SS_ERRMASK;
1469 * Do not autostart sequential access devices
1470 * to avoid unexpected tape loading.
1472 if ((err_action & SS_MASK) == SS_START &&
1473 SID_TYPE(&cgd.inq_data) == T_SEQUENTIAL) {
1474 *action_string = "Will not autostart a "
1475 "sequential access device";
1476 goto sense_error_done;
1480 * Avoid recovery recursion if recovery action is the same.
1482 if ((err_action & SS_MASK) >= SS_START && recoveryccb) {
1483 if (((err_action & SS_MASK) == SS_START &&
1484 ccb->csio.cdb_io.cdb_bytes[0] == START_STOP_UNIT) ||
1485 ((err_action & SS_MASK) == SS_TUR &&
1486 (ccb->csio.cdb_io.cdb_bytes[0] == TEST_UNIT_READY))) {
1487 err_action = SS_RETRY|SSQ_DECREMENT_COUNT|EIO;
1488 *relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1494 * If the recovery action will consume a retry,
1495 * make sure we actually have retries available.
1497 if ((err_action & SSQ_DECREMENT_COUNT) != 0) {
1498 if (ccb->ccb_h.retry_count > 0 &&
1499 (periph->flags & CAM_PERIPH_INVALID) == 0)
1500 ccb->ccb_h.retry_count--;
1502 *action_string = "Retries exhausted";
1503 goto sense_error_done;
1507 if ((err_action & SS_MASK) >= SS_START) {
1509 * Do common portions of commands that
1510 * use recovery CCBs.
1512 orig_ccb = xpt_alloc_ccb_nowait();
1513 if (orig_ccb == NULL) {
1514 *action_string = "Can't allocate recovery CCB";
1515 goto sense_error_done;
1518 * Clear freeze flag for original request here, as
1519 * this freeze will be dropped as part of ERESTART.
1521 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1522 bcopy(ccb, orig_ccb, sizeof(*orig_ccb));
1525 switch (err_action & SS_MASK) {
1527 *action_string = "No recovery action needed";
1531 *action_string = "Retrying command (per sense data)";
1535 *action_string = "Unretryable error";
1542 * Send a start unit command to the device, and
1543 * then retry the command.
1545 *action_string = "Attempting to start unit";
1546 periph->flags |= CAM_PERIPH_RECOVERY_INPROG;
1549 * Check for removable media and set
1550 * load/eject flag appropriately.
1552 if (SID_IS_REMOVABLE(&cgd.inq_data))
1557 scsi_start_stop(&ccb->csio,
1571 * Send a Test Unit Ready to the device.
1572 * If the 'many' flag is set, we send 120
1573 * test unit ready commands, one every half
1574 * second. Otherwise, we just send one TUR.
1575 * We only want to do this if the retry
1576 * count has not been exhausted.
1580 if ((err_action & SSQ_MANY) != 0) {
1581 *action_string = "Polling device for readiness";
1584 *action_string = "Testing device for readiness";
1587 periph->flags |= CAM_PERIPH_RECOVERY_INPROG;
1588 scsi_test_unit_ready(&ccb->csio,
1596 * Accomplish our 500ms delay by deferring
1597 * the release of our device queue appropriately.
1599 *relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1604 panic("Unhandled error action %x", err_action);
1607 if ((err_action & SS_MASK) >= SS_START) {
1609 * Drop the priority, so that the recovery
1610 * CCB is the first to execute. Freeze the queue
1611 * after this command is sent so that we can
1612 * restore the old csio and have it queued in
1613 * the proper order before we release normal
1614 * transactions to the device.
1616 ccb->ccb_h.pinfo.priority--;
1617 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1618 ccb->ccb_h.saved_ccb_ptr = orig_ccb;
1624 *action = err_action;
1630 * Generic error handler. Peripheral drivers usually filter
1631 * out the errors that they handle in a unique manner, then
1632 * call this function.
1635 cam_periph_error(union ccb *ccb, cam_flags camflags,
1636 u_int32_t sense_flags, union ccb *save_ccb)
1638 struct cam_path *newpath;
1639 union ccb *orig_ccb, *scan_ccb;
1640 struct cam_periph *periph;
1641 const char *action_string;
1643 int frozen, error, openings, devctl_err;
1644 u_int32_t action, relsim_flags, timeout;
1646 action = SSQ_PRINT_SENSE;
1647 periph = xpt_path_periph(ccb->ccb_h.path);
1648 action_string = NULL;
1649 status = ccb->ccb_h.status;
1650 frozen = (status & CAM_DEV_QFRZN) != 0;
1651 status &= CAM_STATUS_MASK;
1652 devctl_err = openings = relsim_flags = timeout = 0;
1655 /* Filter the errors that should be reported via devctl */
1656 switch (ccb->ccb_h.status & CAM_STATUS_MASK) {
1657 case CAM_CMD_TIMEOUT:
1658 case CAM_REQ_ABORTED:
1659 case CAM_REQ_CMP_ERR:
1660 case CAM_REQ_TERMIO:
1661 case CAM_UNREC_HBA_ERROR:
1662 case CAM_DATA_RUN_ERR:
1663 case CAM_SCSI_STATUS_ERROR:
1664 case CAM_ATA_STATUS_ERROR:
1665 case CAM_SMP_STATUS_ERROR:
1675 action &= ~SSQ_PRINT_SENSE;
1677 case CAM_SCSI_STATUS_ERROR:
1678 error = camperiphscsistatuserror(ccb, &orig_ccb,
1679 camflags, sense_flags, &openings, &relsim_flags,
1680 &timeout, &action, &action_string);
1682 case CAM_AUTOSENSE_FAIL:
1683 error = EIO; /* we have to kill the command */
1687 case CAM_MSG_REJECT_REC:
1688 /* XXX Don't know that these are correct */
1691 case CAM_SEL_TIMEOUT:
1692 if ((camflags & CAM_RETRY_SELTO) != 0) {
1693 if (ccb->ccb_h.retry_count > 0 &&
1694 (periph->flags & CAM_PERIPH_INVALID) == 0) {
1695 ccb->ccb_h.retry_count--;
1699 * Wait a bit to give the device
1700 * time to recover before we try again.
1702 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1703 timeout = periph_selto_delay;
1706 action_string = "Retries exhausted";
1709 case CAM_DEV_NOT_THERE:
1713 case CAM_REQ_INVALID:
1714 case CAM_PATH_INVALID:
1716 case CAM_PROVIDE_FAIL:
1717 case CAM_REQ_TOO_BIG:
1718 case CAM_LUN_INVALID:
1719 case CAM_TID_INVALID:
1720 case CAM_FUNC_NOTAVAIL:
1723 case CAM_SCSI_BUS_RESET:
1726 * Commands that repeatedly timeout and cause these
1727 * kinds of error recovery actions, should return
1728 * CAM_CMD_TIMEOUT, which allows us to safely assume
1729 * that this command was an innocent bystander to
1730 * these events and should be unconditionally
1733 case CAM_REQUEUE_REQ:
1734 /* Unconditional requeue if device is still there */
1735 if (periph->flags & CAM_PERIPH_INVALID) {
1736 action_string = "Periph was invalidated";
1738 } else if (sense_flags & SF_NO_RETRY) {
1740 action_string = "Retry was blocked";
1743 action &= ~SSQ_PRINT_SENSE;
1746 case CAM_RESRC_UNAVAIL:
1747 /* Wait a bit for the resource shortage to abate. */
1748 timeout = periph_noresrc_delay;
1752 /* Wait a bit for the busy condition to abate. */
1753 timeout = periph_busy_delay;
1755 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1757 case CAM_ATA_STATUS_ERROR:
1758 case CAM_REQ_CMP_ERR:
1759 case CAM_CMD_TIMEOUT:
1760 case CAM_UNEXP_BUSFREE:
1761 case CAM_UNCOR_PARITY:
1762 case CAM_DATA_RUN_ERR:
1764 if (periph->flags & CAM_PERIPH_INVALID) {
1766 action_string = "Periph was invalidated";
1767 } else if (ccb->ccb_h.retry_count == 0) {
1769 action_string = "Retries exhausted";
1770 } else if (sense_flags & SF_NO_RETRY) {
1772 action_string = "Retry was blocked";
1774 ccb->ccb_h.retry_count--;
1780 if ((sense_flags & SF_PRINT_ALWAYS) ||
1781 CAM_DEBUGGED(ccb->ccb_h.path, CAM_DEBUG_INFO))
1782 action |= SSQ_PRINT_SENSE;
1783 else if (sense_flags & SF_NO_PRINT)
1784 action &= ~SSQ_PRINT_SENSE;
1785 if ((action & SSQ_PRINT_SENSE) != 0)
1786 cam_error_print(orig_ccb, CAM_ESF_ALL, CAM_EPF_ALL);
1787 if (error != 0 && (action & SSQ_PRINT_SENSE) != 0) {
1788 if (error != ERESTART) {
1789 if (action_string == NULL)
1790 action_string = "Unretryable error";
1791 xpt_print(ccb->ccb_h.path, "Error %d, %s\n",
1792 error, action_string);
1793 } else if (action_string != NULL)
1794 xpt_print(ccb->ccb_h.path, "%s\n", action_string);
1796 xpt_print(ccb->ccb_h.path, "Retrying command\n");
1799 if (devctl_err && (error != 0 || (action & SSQ_PRINT_SENSE) != 0))
1800 cam_periph_devctl_notify(orig_ccb);
1802 if ((action & SSQ_LOST) != 0) {
1806 * For a selection timeout, we consider all of the LUNs on
1807 * the target to be gone. If the status is CAM_DEV_NOT_THERE,
1808 * then we only get rid of the device(s) specified by the
1809 * path in the original CCB.
1811 if (status == CAM_SEL_TIMEOUT)
1812 lun_id = CAM_LUN_WILDCARD;
1814 lun_id = xpt_path_lun_id(ccb->ccb_h.path);
1816 /* Should we do more if we can't create the path?? */
1817 if (xpt_create_path(&newpath, periph,
1818 xpt_path_path_id(ccb->ccb_h.path),
1819 xpt_path_target_id(ccb->ccb_h.path),
1820 lun_id) == CAM_REQ_CMP) {
1823 * Let peripheral drivers know that this
1824 * device has gone away.
1826 xpt_async(AC_LOST_DEVICE, newpath, NULL);
1827 xpt_free_path(newpath);
1831 /* Broadcast UNIT ATTENTIONs to all periphs. */
1832 if ((action & SSQ_UA) != 0)
1833 xpt_async(AC_UNIT_ATTENTION, orig_ccb->ccb_h.path, orig_ccb);
1835 /* Rescan target on "Reported LUNs data has changed" */
1836 if ((action & SSQ_RESCAN) != 0) {
1837 if (xpt_create_path(&newpath, NULL,
1838 xpt_path_path_id(ccb->ccb_h.path),
1839 xpt_path_target_id(ccb->ccb_h.path),
1840 CAM_LUN_WILDCARD) == CAM_REQ_CMP) {
1842 scan_ccb = xpt_alloc_ccb_nowait();
1843 if (scan_ccb != NULL) {
1844 scan_ccb->ccb_h.path = newpath;
1845 scan_ccb->ccb_h.func_code = XPT_SCAN_TGT;
1846 scan_ccb->crcn.flags = 0;
1847 xpt_rescan(scan_ccb);
1850 "Can't allocate CCB to rescan target\n");
1851 xpt_free_path(newpath);
1856 /* Attempt a retry */
1857 if (error == ERESTART || error == 0) {
1859 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1860 if (error == ERESTART)
1863 cam_release_devq(ccb->ccb_h.path,
1867 /*getcount_only*/0);
1873 #define CAM_PERIPH_DEVD_MSG_SIZE 256
1876 cam_periph_devctl_notify(union ccb *ccb)
1878 struct cam_periph *periph;
1879 struct ccb_getdev *cgd;
1881 int serr, sk, asc, ascq;
1884 sbmsg = malloc(CAM_PERIPH_DEVD_MSG_SIZE, M_CAMPERIPH, M_NOWAIT);
1888 sbuf_new(&sb, sbmsg, CAM_PERIPH_DEVD_MSG_SIZE, SBUF_FIXEDLEN);
1890 periph = xpt_path_periph(ccb->ccb_h.path);
1891 sbuf_printf(&sb, "device=%s%d ", periph->periph_name,
1892 periph->unit_number);
1894 sbuf_printf(&sb, "serial=\"");
1895 if ((cgd = (struct ccb_getdev *)xpt_alloc_ccb_nowait()) != NULL) {
1896 xpt_setup_ccb(&cgd->ccb_h, ccb->ccb_h.path,
1897 CAM_PRIORITY_NORMAL);
1898 cgd->ccb_h.func_code = XPT_GDEV_TYPE;
1899 xpt_action((union ccb *)cgd);
1901 if (cgd->ccb_h.status == CAM_REQ_CMP)
1902 sbuf_bcat(&sb, cgd->serial_num, cgd->serial_num_len);
1903 xpt_free_ccb((union ccb *)cgd);
1905 sbuf_printf(&sb, "\" ");
1906 sbuf_printf(&sb, "cam_status=\"0x%x\" ", ccb->ccb_h.status);
1908 switch (ccb->ccb_h.status & CAM_STATUS_MASK) {
1909 case CAM_CMD_TIMEOUT:
1910 sbuf_printf(&sb, "timeout=%d ", ccb->ccb_h.timeout);
1913 case CAM_SCSI_STATUS_ERROR:
1914 sbuf_printf(&sb, "scsi_status=%d ", ccb->csio.scsi_status);
1915 if (scsi_extract_sense_ccb(ccb, &serr, &sk, &asc, &ascq))
1916 sbuf_printf(&sb, "scsi_sense=\"%02x %02x %02x %02x\" ",
1917 serr, sk, asc, ascq);
1920 case CAM_ATA_STATUS_ERROR:
1921 sbuf_printf(&sb, "RES=\"");
1922 ata_res_sbuf(&ccb->ataio.res, &sb);
1923 sbuf_printf(&sb, "\" ");
1931 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1932 sbuf_printf(&sb, "CDB=\"");
1933 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0)
1934 scsi_cdb_sbuf(ccb->csio.cdb_io.cdb_ptr, &sb);
1936 scsi_cdb_sbuf(ccb->csio.cdb_io.cdb_bytes, &sb);
1937 sbuf_printf(&sb, "\" ");
1938 } else if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1939 sbuf_printf(&sb, "ACB=\"");
1940 ata_cmd_sbuf(&ccb->ataio.cmd, &sb);
1941 sbuf_printf(&sb, "\" ");
1944 if (sbuf_finish(&sb) == 0)
1945 devctl_notify("CAM", "periph", type, sbuf_data(&sb));
1947 free(sbmsg, M_CAMPERIPH);