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_unregister(void *data)
145 struct periph_driver *drv = (struct periph_driver *)data;
148 /* If driver marked as early or it is late now, deinitialize it. */
149 if (((drv->flags & CAM_PERIPH_DRV_EARLY) != 0 && initialized > 0) ||
151 if (drv->deinit == NULL) {
152 printf("CAM periph driver '%s' doesn't have deinit.\n",
156 error = drv->deinit();
162 for (n = 0; n < nperiph_drivers && periph_drivers[n] != drv; n++)
164 KASSERT(n < nperiph_drivers,
165 ("Periph driver '%s' was not registered", drv->driver_name));
166 for (; n + 1 < nperiph_drivers; n++)
167 periph_drivers[n] = periph_drivers[n + 1];
168 periph_drivers[n + 1] = NULL;
175 periphdriver_init(int level)
179 initialized = max(initialized, level);
180 for (i = 0; periph_drivers[i] != NULL; i++) {
181 early = (periph_drivers[i]->flags & CAM_PERIPH_DRV_EARLY) ? 1 : 2;
182 if (early == initialized)
183 (*periph_drivers[i]->init)();
188 cam_periph_alloc(periph_ctor_t *periph_ctor,
189 periph_oninv_t *periph_oninvalidate,
190 periph_dtor_t *periph_dtor, periph_start_t *periph_start,
191 char *name, cam_periph_type type, struct cam_path *path,
192 ac_callback_t *ac_callback, ac_code code, void *arg)
194 struct periph_driver **p_drv;
196 struct cam_periph *periph;
197 struct cam_periph *cur_periph;
199 target_id_t target_id;
206 * Handle Hot-Plug scenarios. If there is already a peripheral
207 * of our type assigned to this path, we are likely waiting for
208 * final close on an old, invalidated, peripheral. If this is
209 * the case, queue up a deferred call to the peripheral's async
210 * handler. If it looks like a mistaken re-allocation, complain.
212 if ((periph = cam_periph_find(path, name)) != NULL) {
214 if ((periph->flags & CAM_PERIPH_INVALID) != 0
215 && (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) == 0) {
216 periph->flags |= CAM_PERIPH_NEW_DEV_FOUND;
217 periph->deferred_callback = ac_callback;
218 periph->deferred_ac = code;
219 return (CAM_REQ_INPROG);
221 printf("cam_periph_alloc: attempt to re-allocate "
222 "valid device %s%d rejected flags %#x "
223 "refcount %d\n", periph->periph_name,
224 periph->unit_number, periph->flags,
227 return (CAM_REQ_INVALID);
230 periph = (struct cam_periph *)malloc(sizeof(*periph), M_CAMPERIPH,
234 return (CAM_RESRC_UNAVAIL);
239 sim = xpt_path_sim(path);
240 path_id = xpt_path_path_id(path);
241 target_id = xpt_path_target_id(path);
242 lun_id = xpt_path_lun_id(path);
243 periph->periph_start = periph_start;
244 periph->periph_dtor = periph_dtor;
245 periph->periph_oninval = periph_oninvalidate;
247 periph->periph_name = name;
248 periph->scheduled_priority = CAM_PRIORITY_NONE;
249 periph->immediate_priority = CAM_PRIORITY_NONE;
250 periph->refcount = 1; /* Dropped by invalidation. */
252 SLIST_INIT(&periph->ccb_list);
253 status = xpt_create_path(&path, periph, path_id, target_id, lun_id);
254 if (status != CAM_REQ_CMP)
259 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
260 if (strcmp((*p_drv)->driver_name, name) == 0)
263 if (*p_drv == NULL) {
264 printf("cam_periph_alloc: invalid periph name '%s'\n", name);
266 xpt_free_path(periph->path);
267 free(periph, M_CAMPERIPH);
268 return (CAM_REQ_INVALID);
270 periph->unit_number = camperiphunit(*p_drv, path_id, target_id, lun_id);
271 cur_periph = TAILQ_FIRST(&(*p_drv)->units);
272 while (cur_periph != NULL
273 && cur_periph->unit_number < periph->unit_number)
274 cur_periph = TAILQ_NEXT(cur_periph, unit_links);
275 if (cur_periph != NULL) {
276 KASSERT(cur_periph->unit_number != periph->unit_number, ("duplicate units on periph list"));
277 TAILQ_INSERT_BEFORE(cur_periph, periph, unit_links);
279 TAILQ_INSERT_TAIL(&(*p_drv)->units, periph, unit_links);
280 (*p_drv)->generation++;
286 status = xpt_add_periph(periph);
287 if (status != CAM_REQ_CMP)
291 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph created\n"));
293 status = periph_ctor(periph, arg);
295 if (status == CAM_REQ_CMP)
299 switch (init_level) {
301 /* Initialized successfully */
304 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph destroyed\n"));
305 xpt_remove_periph(periph);
309 TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
311 xpt_free_path(periph->path);
314 free(periph, M_CAMPERIPH);
317 /* No cleanup to perform. */
320 panic("%s: Unknown init level", __func__);
326 * Find a peripheral structure with the specified path, target, lun,
327 * and (optionally) type. If the name is NULL, this function will return
328 * the first peripheral driver that matches the specified path.
331 cam_periph_find(struct cam_path *path, char *name)
333 struct periph_driver **p_drv;
334 struct cam_periph *periph;
337 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
339 if (name != NULL && (strcmp((*p_drv)->driver_name, name) != 0))
342 TAILQ_FOREACH(periph, &(*p_drv)->units, unit_links) {
343 if (xpt_path_comp(periph->path, path) == 0) {
345 cam_periph_assert(periph, MA_OWNED);
359 * Find peripheral driver instances attached to the specified path.
362 cam_periph_list(struct cam_path *path, struct sbuf *sb)
364 struct sbuf local_sb;
365 struct periph_driver **p_drv;
366 struct cam_periph *periph;
372 sbuf_new(&local_sb, NULL, sbuf_alloc_len, SBUF_FIXEDLEN);
375 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
377 TAILQ_FOREACH(periph, &(*p_drv)->units, unit_links) {
378 if (xpt_path_comp(periph->path, path) != 0)
381 if (sbuf_len(&local_sb) != 0)
382 sbuf_cat(&local_sb, ",");
384 sbuf_printf(&local_sb, "%s%d", periph->periph_name,
385 periph->unit_number);
387 if (sbuf_error(&local_sb) == ENOMEM) {
390 sbuf_delete(&local_sb);
397 sbuf_finish(&local_sb);
398 sbuf_cpy(sb, sbuf_data(&local_sb));
399 sbuf_delete(&local_sb);
404 cam_periph_acquire(struct cam_periph *periph)
408 status = CAM_REQ_CMP_ERR;
413 if ((periph->flags & CAM_PERIPH_INVALID) == 0) {
415 status = CAM_REQ_CMP;
423 cam_periph_doacquire(struct cam_periph *periph)
427 KASSERT(periph->refcount >= 1,
428 ("cam_periph_doacquire() with refcount == %d", periph->refcount));
434 cam_periph_release_locked_buses(struct cam_periph *periph)
437 cam_periph_assert(periph, MA_OWNED);
438 KASSERT(periph->refcount >= 1, ("periph->refcount >= 1"));
439 if (--periph->refcount == 0)
440 camperiphfree(periph);
444 cam_periph_release_locked(struct cam_periph *periph)
451 cam_periph_release_locked_buses(periph);
456 cam_periph_release(struct cam_periph *periph)
463 cam_periph_assert(periph, MA_NOTOWNED);
464 mtx = cam_periph_mtx(periph);
466 cam_periph_release_locked(periph);
471 cam_periph_hold(struct cam_periph *periph, int priority)
476 * Increment the reference count on the peripheral
477 * while we wait for our lock attempt to succeed
478 * to ensure the peripheral doesn't disappear out
479 * from user us while we sleep.
482 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
485 cam_periph_assert(periph, MA_OWNED);
486 while ((periph->flags & CAM_PERIPH_LOCKED) != 0) {
487 periph->flags |= CAM_PERIPH_LOCK_WANTED;
488 if ((error = cam_periph_sleep(periph, periph, priority,
489 "caplck", 0)) != 0) {
490 cam_periph_release_locked(periph);
493 if (periph->flags & CAM_PERIPH_INVALID) {
494 cam_periph_release_locked(periph);
499 periph->flags |= CAM_PERIPH_LOCKED;
504 cam_periph_unhold(struct cam_periph *periph)
507 cam_periph_assert(periph, MA_OWNED);
509 periph->flags &= ~CAM_PERIPH_LOCKED;
510 if ((periph->flags & CAM_PERIPH_LOCK_WANTED) != 0) {
511 periph->flags &= ~CAM_PERIPH_LOCK_WANTED;
515 cam_periph_release_locked(periph);
519 * Look for the next unit number that is not currently in use for this
520 * peripheral type starting at "newunit". Also exclude unit numbers that
521 * are reserved by for future "hardwiring" unless we already know that this
522 * is a potential wired device. Only assume that the device is "wired" the
523 * first time through the loop since after that we'll be looking at unit
524 * numbers that did not match a wiring entry.
527 camperiphnextunit(struct periph_driver *p_drv, u_int newunit, int wired,
528 path_id_t pathid, target_id_t target, lun_id_t lun)
530 struct cam_periph *periph;
532 int i, val, dunit, r;
533 const char *dname, *strval;
535 periph_name = p_drv->driver_name;
538 for (periph = TAILQ_FIRST(&p_drv->units);
539 periph != NULL && periph->unit_number != newunit;
540 periph = TAILQ_NEXT(periph, unit_links))
543 if (periph != NULL && periph->unit_number == newunit) {
545 xpt_print(periph->path, "Duplicate Wired "
547 xpt_print(periph->path, "Second device (%s "
548 "device at scbus%d target %d lun %d) will "
549 "not be wired\n", periph_name, pathid,
559 * Don't match entries like "da 4" as a wired down
560 * device, but do match entries like "da 4 target 5"
561 * or even "da 4 scbus 1".
566 r = resource_find_dev(&i, dname, &dunit, NULL, NULL);
569 /* if no "target" and no specific scbus, skip */
570 if (resource_int_value(dname, dunit, "target", &val) &&
571 (resource_string_value(dname, dunit, "at",&strval)||
572 strcmp(strval, "scbus") == 0))
574 if (newunit == dunit)
584 camperiphunit(struct periph_driver *p_drv, path_id_t pathid,
585 target_id_t target, lun_id_t lun)
588 int wired, i, val, dunit;
589 const char *dname, *strval;
590 char pathbuf[32], *periph_name;
592 periph_name = p_drv->driver_name;
593 snprintf(pathbuf, sizeof(pathbuf), "scbus%d", pathid);
597 for (wired = 0; resource_find_dev(&i, dname, &dunit, NULL, NULL) == 0;
599 if (resource_string_value(dname, dunit, "at", &strval) == 0) {
600 if (strcmp(strval, pathbuf) != 0)
604 if (resource_int_value(dname, dunit, "target", &val) == 0) {
609 if (resource_int_value(dname, dunit, "lun", &val) == 0) {
621 * Either start from 0 looking for the next unit or from
622 * the unit number given in the resource config. This way,
623 * if we have wildcard matches, we don't return the same
626 unit = camperiphnextunit(p_drv, unit, wired, pathid, target, lun);
632 cam_periph_invalidate(struct cam_periph *periph)
635 cam_periph_assert(periph, MA_OWNED);
637 * We only call this routine the first time a peripheral is
640 if ((periph->flags & CAM_PERIPH_INVALID) != 0)
643 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph invalidated\n"));
644 if ((periph->flags & CAM_PERIPH_ANNOUNCED) && !rebooting) {
648 sbuf_new(&sb, buffer, 160, SBUF_FIXEDLEN);
649 xpt_denounce_periph_sbuf(periph, &sb);
653 periph->flags |= CAM_PERIPH_INVALID;
654 periph->flags &= ~CAM_PERIPH_NEW_DEV_FOUND;
655 if (periph->periph_oninval != NULL)
656 periph->periph_oninval(periph);
657 cam_periph_release_locked(periph);
661 camperiphfree(struct cam_periph *periph)
663 struct periph_driver **p_drv;
665 cam_periph_assert(periph, MA_OWNED);
666 KASSERT(periph->periph_allocating == 0, ("%s%d: freed while allocating",
667 periph->periph_name, periph->unit_number));
668 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
669 if (strcmp((*p_drv)->driver_name, periph->periph_name) == 0)
672 if (*p_drv == NULL) {
673 printf("camperiphfree: attempt to free non-existant periph\n");
678 * We need to set this flag before dropping the topology lock, to
679 * let anyone who is traversing the list that this peripheral is
680 * about to be freed, and there will be no more reference count
683 periph->flags |= CAM_PERIPH_FREE;
686 * The peripheral destructor semantics dictate calling with only the
687 * SIM mutex held. Since it might sleep, it should not be called
688 * with the topology lock held.
693 * We need to call the peripheral destructor prior to removing the
694 * peripheral from the list. Otherwise, we risk running into a
695 * scenario where the peripheral unit number may get reused
696 * (because it has been removed from the list), but some resources
697 * used by the peripheral are still hanging around. In particular,
698 * the devfs nodes used by some peripherals like the pass(4) driver
699 * aren't fully cleaned up until the destructor is run. If the
700 * unit number is reused before the devfs instance is fully gone,
703 if (periph->periph_dtor != NULL)
704 periph->periph_dtor(periph);
707 * The peripheral list is protected by the topology lock.
711 TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
712 (*p_drv)->generation++;
714 xpt_remove_periph(periph);
717 if ((periph->flags & CAM_PERIPH_ANNOUNCED) && !rebooting)
718 xpt_print(periph->path, "Periph destroyed\n");
720 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph destroyed\n"));
722 if (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) {
726 switch (periph->deferred_ac) {
727 case AC_FOUND_DEVICE:
728 ccb.ccb_h.func_code = XPT_GDEV_TYPE;
729 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
733 case AC_PATH_REGISTERED:
734 ccb.ccb_h.func_code = XPT_PATH_INQ;
735 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
743 periph->deferred_callback(NULL, periph->deferred_ac,
746 xpt_free_path(periph->path);
747 free(periph, M_CAMPERIPH);
752 * Map user virtual pointers into kernel virtual address space, so we can
753 * access the memory. This is now a generic function that centralizes most
754 * of the sanity checks on the data flags, if any.
755 * This also only works for up to MAXPHYS memory. Since we use
756 * buffers to map stuff in and out, we're limited to the buffer size.
759 cam_periph_mapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo,
763 int flags[CAM_PERIPH_MAXMAPS];
764 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
765 u_int32_t lengths[CAM_PERIPH_MAXMAPS];
766 u_int32_t dirs[CAM_PERIPH_MAXMAPS];
769 maxmap = DFLTPHYS; /* traditional default */
770 else if (maxmap > MAXPHYS)
771 maxmap = MAXPHYS; /* for safety */
772 switch(ccb->ccb_h.func_code) {
774 if (ccb->cdm.match_buf_len == 0) {
775 printf("cam_periph_mapmem: invalid match buffer "
779 if (ccb->cdm.pattern_buf_len > 0) {
780 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
781 lengths[0] = ccb->cdm.pattern_buf_len;
782 dirs[0] = CAM_DIR_OUT;
783 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
784 lengths[1] = ccb->cdm.match_buf_len;
785 dirs[1] = CAM_DIR_IN;
788 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
789 lengths[0] = ccb->cdm.match_buf_len;
790 dirs[0] = CAM_DIR_IN;
794 * This request will not go to the hardware, no reason
795 * to be so strict. vmapbuf() is able to map up to MAXPHYS.
800 case XPT_CONT_TARGET_IO:
801 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
803 if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR)
805 data_ptrs[0] = &ccb->csio.data_ptr;
806 lengths[0] = ccb->csio.dxfer_len;
807 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
811 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
813 if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR)
815 data_ptrs[0] = &ccb->ataio.data_ptr;
816 lengths[0] = ccb->ataio.dxfer_len;
817 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
821 data_ptrs[0] = &ccb->smpio.smp_request;
822 lengths[0] = ccb->smpio.smp_request_len;
823 dirs[0] = CAM_DIR_OUT;
824 data_ptrs[1] = &ccb->smpio.smp_response;
825 lengths[1] = ccb->smpio.smp_response_len;
826 dirs[1] = CAM_DIR_IN;
829 case XPT_DEV_ADVINFO:
830 if (ccb->cdai.bufsiz == 0)
833 data_ptrs[0] = (uint8_t **)&ccb->cdai.buf;
834 lengths[0] = ccb->cdai.bufsiz;
835 dirs[0] = CAM_DIR_IN;
839 * This request will not go to the hardware, no reason
840 * to be so strict. vmapbuf() is able to map up to MAXPHYS.
846 break; /* NOTREACHED */
850 * Check the transfer length and permissions first, so we don't
851 * have to unmap any previously mapped buffers.
853 for (i = 0; i < numbufs; i++) {
858 * The userland data pointer passed in may not be page
859 * aligned. vmapbuf() truncates the address to a page
860 * boundary, so if the address isn't page aligned, we'll
861 * need enough space for the given transfer length, plus
862 * whatever extra space is necessary to make it to the page
866 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)) > maxmap){
867 printf("cam_periph_mapmem: attempt to map %lu bytes, "
868 "which is greater than %lu\n",
870 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)),
875 if (dirs[i] & CAM_DIR_OUT) {
876 flags[i] = BIO_WRITE;
879 if (dirs[i] & CAM_DIR_IN) {
886 * This keeps the kernel stack of current thread from getting
887 * swapped. In low-memory situations where the kernel stack might
888 * otherwise get swapped out, this holds it and allows the thread
889 * to make progress and release the kernel mapped pages sooner.
891 * XXX KDM should I use P_NOSWAP instead?
895 for (i = 0; i < numbufs; i++) {
899 mapinfo->bp[i] = getpbuf(NULL);
901 /* put our pointer in the data slot */
902 mapinfo->bp[i]->b_data = *data_ptrs[i];
904 /* save the user's data address */
905 mapinfo->bp[i]->b_caller1 = *data_ptrs[i];
907 /* set the transfer length, we know it's < MAXPHYS */
908 mapinfo->bp[i]->b_bufsize = lengths[i];
910 /* set the direction */
911 mapinfo->bp[i]->b_iocmd = flags[i];
914 * Map the buffer into kernel memory.
916 * Note that useracc() alone is not a sufficient test.
917 * vmapbuf() can still fail due to a smaller file mapped
918 * into a larger area of VM, or if userland races against
919 * vmapbuf() after the useracc() check.
921 if (vmapbuf(mapinfo->bp[i], 1) < 0) {
922 for (j = 0; j < i; ++j) {
923 *data_ptrs[j] = mapinfo->bp[j]->b_caller1;
924 vunmapbuf(mapinfo->bp[j]);
925 relpbuf(mapinfo->bp[j], NULL);
927 relpbuf(mapinfo->bp[i], NULL);
932 /* set our pointer to the new mapped area */
933 *data_ptrs[i] = mapinfo->bp[i]->b_data;
935 mapinfo->num_bufs_used++;
939 * Now that we've gotten this far, change ownership to the kernel
940 * of the buffers so that we don't run afoul of returning to user
941 * space with locks (on the buffer) held.
943 for (i = 0; i < numbufs; i++) {
944 BUF_KERNPROC(mapinfo->bp[i]);
952 * Unmap memory segments mapped into kernel virtual address space by
953 * cam_periph_mapmem().
956 cam_periph_unmapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
959 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
961 if (mapinfo->num_bufs_used <= 0) {
962 /* nothing to free and the process wasn't held. */
966 switch (ccb->ccb_h.func_code) {
968 numbufs = min(mapinfo->num_bufs_used, 2);
971 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
973 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
974 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
978 case XPT_CONT_TARGET_IO:
979 data_ptrs[0] = &ccb->csio.data_ptr;
980 numbufs = min(mapinfo->num_bufs_used, 1);
983 data_ptrs[0] = &ccb->ataio.data_ptr;
984 numbufs = min(mapinfo->num_bufs_used, 1);
987 numbufs = min(mapinfo->num_bufs_used, 2);
988 data_ptrs[0] = &ccb->smpio.smp_request;
989 data_ptrs[1] = &ccb->smpio.smp_response;
991 case XPT_DEV_ADVINFO:
992 numbufs = min(mapinfo->num_bufs_used, 1);
993 data_ptrs[0] = (uint8_t **)&ccb->cdai.buf;
996 /* allow ourselves to be swapped once again */
999 break; /* NOTREACHED */
1002 for (i = 0; i < numbufs; i++) {
1003 /* Set the user's pointer back to the original value */
1004 *data_ptrs[i] = mapinfo->bp[i]->b_caller1;
1006 /* unmap the buffer */
1007 vunmapbuf(mapinfo->bp[i]);
1009 /* release the buffer */
1010 relpbuf(mapinfo->bp[i], NULL);
1013 /* allow ourselves to be swapped once again */
1018 cam_periph_ioctl(struct cam_periph *periph, u_long cmd, caddr_t addr,
1019 int (*error_routine)(union ccb *ccb,
1021 u_int32_t sense_flags))
1030 case CAMGETPASSTHRU:
1031 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1032 xpt_setup_ccb(&ccb->ccb_h,
1034 CAM_PRIORITY_NORMAL);
1035 ccb->ccb_h.func_code = XPT_GDEVLIST;
1038 * Basically, the point of this is that we go through
1039 * getting the list of devices, until we find a passthrough
1040 * device. In the current version of the CAM code, the
1041 * only way to determine what type of device we're dealing
1042 * with is by its name.
1044 while (found == 0) {
1045 ccb->cgdl.index = 0;
1046 ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS;
1047 while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) {
1049 /* we want the next device in the list */
1051 if (strncmp(ccb->cgdl.periph_name,
1057 if ((ccb->cgdl.status == CAM_GDEVLIST_LAST_DEVICE) &&
1059 ccb->cgdl.periph_name[0] = '\0';
1060 ccb->cgdl.unit_number = 0;
1065 /* copy the result back out */
1066 bcopy(ccb, addr, sizeof(union ccb));
1068 /* and release the ccb */
1069 xpt_release_ccb(ccb);
1080 cam_periph_done_panic(struct cam_periph *periph, union ccb *done_ccb)
1083 panic("%s: already done with ccb %p", __func__, done_ccb);
1087 cam_periph_done(struct cam_periph *periph, union ccb *done_ccb)
1090 /* Caller will release the CCB */
1091 xpt_path_assert(done_ccb->ccb_h.path, MA_OWNED);
1092 done_ccb->ccb_h.cbfcnp = cam_periph_done_panic;
1093 wakeup(&done_ccb->ccb_h.cbfcnp);
1097 cam_periph_ccbwait(union ccb *ccb)
1100 if ((ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
1101 while (ccb->ccb_h.cbfcnp != cam_periph_done_panic)
1102 xpt_path_sleep(ccb->ccb_h.path, &ccb->ccb_h.cbfcnp,
1103 PRIBIO, "cbwait", 0);
1105 KASSERT(ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX &&
1106 (ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG,
1107 ("%s: proceeding with incomplete ccb: ccb=%p, func_code=%#x, "
1108 "status=%#x, index=%d", __func__, ccb, ccb->ccb_h.func_code,
1109 ccb->ccb_h.status, ccb->ccb_h.pinfo.index));
1113 cam_periph_runccb(union ccb *ccb,
1114 int (*error_routine)(union ccb *ccb,
1116 u_int32_t sense_flags),
1117 cam_flags camflags, u_int32_t sense_flags,
1120 struct bintime *starttime;
1121 struct bintime ltime;
1125 xpt_path_assert(ccb->ccb_h.path, MA_OWNED);
1126 KASSERT((ccb->ccb_h.flags & CAM_UNLOCKED) == 0,
1127 ("%s: ccb=%p, func_code=%#x, flags=%#x", __func__, ccb,
1128 ccb->ccb_h.func_code, ccb->ccb_h.flags));
1131 * If the user has supplied a stats structure, and if we understand
1132 * this particular type of ccb, record the transaction start.
1134 if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO ||
1135 ccb->ccb_h.func_code == XPT_ATA_IO)) {
1137 binuptime(starttime);
1138 devstat_start_transaction(ds, starttime);
1141 ccb->ccb_h.cbfcnp = cam_periph_done;
1145 cam_periph_ccbwait(ccb);
1146 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
1148 else if (error_routine != NULL) {
1149 ccb->ccb_h.cbfcnp = cam_periph_done;
1150 error = (*error_routine)(ccb, camflags, sense_flags);
1154 } while (error == ERESTART);
1156 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1157 cam_release_devq(ccb->ccb_h.path,
1158 /* relsim_flags */0,
1161 /* getcount_only */ FALSE);
1162 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1166 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1167 devstat_end_transaction(ds,
1168 ccb->csio.dxfer_len - ccb->csio.resid,
1169 ccb->csio.tag_action & 0x3,
1170 ((ccb->ccb_h.flags & CAM_DIR_MASK) ==
1171 CAM_DIR_NONE) ? DEVSTAT_NO_DATA :
1172 (ccb->ccb_h.flags & CAM_DIR_OUT) ?
1174 DEVSTAT_READ, NULL, starttime);
1175 } else if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1176 devstat_end_transaction(ds,
1177 ccb->ataio.dxfer_len - ccb->ataio.resid,
1178 0, /* Not used in ATA */
1179 ((ccb->ccb_h.flags & CAM_DIR_MASK) ==
1180 CAM_DIR_NONE) ? DEVSTAT_NO_DATA :
1181 (ccb->ccb_h.flags & CAM_DIR_OUT) ?
1183 DEVSTAT_READ, NULL, starttime);
1191 cam_freeze_devq(struct cam_path *path)
1193 struct ccb_hdr ccb_h;
1195 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("cam_freeze_devq\n"));
1196 xpt_setup_ccb(&ccb_h, path, /*priority*/1);
1197 ccb_h.func_code = XPT_NOOP;
1198 ccb_h.flags = CAM_DEV_QFREEZE;
1199 xpt_action((union ccb *)&ccb_h);
1203 cam_release_devq(struct cam_path *path, u_int32_t relsim_flags,
1204 u_int32_t openings, u_int32_t arg,
1207 struct ccb_relsim crs;
1209 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("cam_release_devq(%u, %u, %u, %d)\n",
1210 relsim_flags, openings, arg, getcount_only));
1211 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
1212 crs.ccb_h.func_code = XPT_REL_SIMQ;
1213 crs.ccb_h.flags = getcount_only ? CAM_DEV_QFREEZE : 0;
1214 crs.release_flags = relsim_flags;
1215 crs.openings = openings;
1216 crs.release_timeout = arg;
1217 xpt_action((union ccb *)&crs);
1218 return (crs.qfrozen_cnt);
1221 #define saved_ccb_ptr ppriv_ptr0
1223 camperiphdone(struct cam_periph *periph, union ccb *done_ccb)
1225 union ccb *saved_ccb;
1227 struct scsi_start_stop_unit *scsi_cmd;
1228 int error_code, sense_key, asc, ascq;
1230 scsi_cmd = (struct scsi_start_stop_unit *)
1231 &done_ccb->csio.cdb_io.cdb_bytes;
1232 status = done_ccb->ccb_h.status;
1234 if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1235 if (scsi_extract_sense_ccb(done_ccb,
1236 &error_code, &sense_key, &asc, &ascq)) {
1238 * If the error is "invalid field in CDB",
1239 * and the load/eject flag is set, turn the
1240 * flag off and try again. This is just in
1241 * case the drive in question barfs on the
1242 * load eject flag. The CAM code should set
1243 * the load/eject flag by default for
1246 if ((scsi_cmd->opcode == START_STOP_UNIT) &&
1247 ((scsi_cmd->how & SSS_LOEJ) != 0) &&
1248 (asc == 0x24) && (ascq == 0x00)) {
1249 scsi_cmd->how &= ~SSS_LOEJ;
1250 if (status & CAM_DEV_QFRZN) {
1251 cam_release_devq(done_ccb->ccb_h.path,
1253 done_ccb->ccb_h.status &=
1256 xpt_action(done_ccb);
1260 if (cam_periph_error(done_ccb,
1261 0, SF_RETRY_UA | SF_NO_PRINT, NULL) == ERESTART)
1263 if (done_ccb->ccb_h.status & CAM_DEV_QFRZN) {
1264 cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0);
1265 done_ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1269 * If we have successfully taken a device from the not
1270 * ready to ready state, re-scan the device and re-get
1271 * the inquiry information. Many devices (mostly disks)
1272 * don't properly report their inquiry information unless
1275 if (scsi_cmd->opcode == START_STOP_UNIT)
1276 xpt_async(AC_INQ_CHANGED, done_ccb->ccb_h.path, NULL);
1280 * Perform the final retry with the original CCB so that final
1281 * error processing is performed by the owner of the CCB.
1283 saved_ccb = (union ccb *)done_ccb->ccb_h.saved_ccb_ptr;
1284 bcopy(saved_ccb, done_ccb, sizeof(*done_ccb));
1285 xpt_free_ccb(saved_ccb);
1286 if (done_ccb->ccb_h.cbfcnp != camperiphdone)
1287 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1288 xpt_action(done_ccb);
1291 /* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
1292 cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0);
1296 * Generic Async Event handler. Peripheral drivers usually
1297 * filter out the events that require personal attention,
1298 * and leave the rest to this function.
1301 cam_periph_async(struct cam_periph *periph, u_int32_t code,
1302 struct cam_path *path, void *arg)
1305 case AC_LOST_DEVICE:
1306 cam_periph_invalidate(periph);
1314 cam_periph_bus_settle(struct cam_periph *periph, u_int bus_settle)
1316 struct ccb_getdevstats cgds;
1318 xpt_setup_ccb(&cgds.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1319 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1320 xpt_action((union ccb *)&cgds);
1321 cam_periph_freeze_after_event(periph, &cgds.last_reset, bus_settle);
1325 cam_periph_freeze_after_event(struct cam_periph *periph,
1326 struct timeval* event_time, u_int duration_ms)
1328 struct timeval delta;
1329 struct timeval duration_tv;
1331 if (!timevalisset(event_time))
1335 timevalsub(&delta, event_time);
1336 duration_tv.tv_sec = duration_ms / 1000;
1337 duration_tv.tv_usec = (duration_ms % 1000) * 1000;
1338 if (timevalcmp(&delta, &duration_tv, <)) {
1339 timevalsub(&duration_tv, &delta);
1341 duration_ms = duration_tv.tv_sec * 1000;
1342 duration_ms += duration_tv.tv_usec / 1000;
1343 cam_freeze_devq(periph->path);
1344 cam_release_devq(periph->path,
1345 RELSIM_RELEASE_AFTER_TIMEOUT,
1347 /*timeout*/duration_ms,
1348 /*getcount_only*/0);
1354 camperiphscsistatuserror(union ccb *ccb, union ccb **orig_ccb,
1355 cam_flags camflags, u_int32_t sense_flags,
1356 int *openings, u_int32_t *relsim_flags,
1357 u_int32_t *timeout, u_int32_t *action, const char **action_string)
1361 switch (ccb->csio.scsi_status) {
1362 case SCSI_STATUS_OK:
1363 case SCSI_STATUS_COND_MET:
1364 case SCSI_STATUS_INTERMED:
1365 case SCSI_STATUS_INTERMED_COND_MET:
1368 case SCSI_STATUS_CMD_TERMINATED:
1369 case SCSI_STATUS_CHECK_COND:
1370 error = camperiphscsisenseerror(ccb, orig_ccb,
1379 case SCSI_STATUS_QUEUE_FULL:
1382 struct ccb_getdevstats cgds;
1385 * First off, find out what the current
1386 * transaction counts are.
1388 xpt_setup_ccb(&cgds.ccb_h,
1390 CAM_PRIORITY_NORMAL);
1391 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1392 xpt_action((union ccb *)&cgds);
1395 * If we were the only transaction active, treat
1396 * the QUEUE FULL as if it were a BUSY condition.
1398 if (cgds.dev_active != 0) {
1402 * Reduce the number of openings to
1403 * be 1 less than the amount it took
1404 * to get a queue full bounded by the
1405 * minimum allowed tag count for this
1408 total_openings = cgds.dev_active + cgds.dev_openings;
1409 *openings = cgds.dev_active;
1410 if (*openings < cgds.mintags)
1411 *openings = cgds.mintags;
1412 if (*openings < total_openings)
1413 *relsim_flags = RELSIM_ADJUST_OPENINGS;
1416 * Some devices report queue full for
1417 * temporary resource shortages. For
1418 * this reason, we allow a minimum
1419 * tag count to be entered via a
1420 * quirk entry to prevent the queue
1421 * count on these devices from falling
1422 * to a pessimisticly low value. We
1423 * still wait for the next successful
1424 * completion, however, before queueing
1425 * more transactions to the device.
1427 *relsim_flags = RELSIM_RELEASE_AFTER_CMDCMPLT;
1431 *action &= ~SSQ_PRINT_SENSE;
1436 case SCSI_STATUS_BUSY:
1438 * Restart the queue after either another
1439 * command completes or a 1 second timeout.
1441 if ((sense_flags & SF_RETRY_BUSY) != 0 ||
1442 (ccb->ccb_h.retry_count--) > 0) {
1444 *relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT
1445 | RELSIM_RELEASE_AFTER_CMDCMPLT;
1451 case SCSI_STATUS_RESERV_CONFLICT:
1460 camperiphscsisenseerror(union ccb *ccb, union ccb **orig,
1461 cam_flags camflags, u_int32_t sense_flags,
1462 int *openings, u_int32_t *relsim_flags,
1463 u_int32_t *timeout, u_int32_t *action, const char **action_string)
1465 struct cam_periph *periph;
1466 union ccb *orig_ccb = ccb;
1467 int error, recoveryccb;
1469 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
1470 if (ccb->ccb_h.func_code == XPT_SCSI_IO && ccb->csio.bio != NULL)
1471 biotrack(ccb->csio.bio, __func__);
1474 periph = xpt_path_periph(ccb->ccb_h.path);
1475 recoveryccb = (ccb->ccb_h.cbfcnp == camperiphdone);
1476 if ((periph->flags & CAM_PERIPH_RECOVERY_INPROG) && !recoveryccb) {
1478 * If error recovery is already in progress, don't attempt
1479 * to process this error, but requeue it unconditionally
1480 * and attempt to process it once error recovery has
1481 * completed. This failed command is probably related to
1482 * the error that caused the currently active error recovery
1483 * action so our current recovery efforts should also
1484 * address this command. Be aware that the error recovery
1485 * code assumes that only one recovery action is in progress
1486 * on a particular peripheral instance at any given time
1487 * (e.g. only one saved CCB for error recovery) so it is
1488 * imperitive that we don't violate this assumption.
1491 *action &= ~SSQ_PRINT_SENSE;
1493 scsi_sense_action err_action;
1494 struct ccb_getdev cgd;
1497 * Grab the inquiry data for this device.
1499 xpt_setup_ccb(&cgd.ccb_h, ccb->ccb_h.path, CAM_PRIORITY_NORMAL);
1500 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1501 xpt_action((union ccb *)&cgd);
1503 err_action = scsi_error_action(&ccb->csio, &cgd.inq_data,
1505 error = err_action & SS_ERRMASK;
1508 * Do not autostart sequential access devices
1509 * to avoid unexpected tape loading.
1511 if ((err_action & SS_MASK) == SS_START &&
1512 SID_TYPE(&cgd.inq_data) == T_SEQUENTIAL) {
1513 *action_string = "Will not autostart a "
1514 "sequential access device";
1515 goto sense_error_done;
1519 * Avoid recovery recursion if recovery action is the same.
1521 if ((err_action & SS_MASK) >= SS_START && recoveryccb) {
1522 if (((err_action & SS_MASK) == SS_START &&
1523 ccb->csio.cdb_io.cdb_bytes[0] == START_STOP_UNIT) ||
1524 ((err_action & SS_MASK) == SS_TUR &&
1525 (ccb->csio.cdb_io.cdb_bytes[0] == TEST_UNIT_READY))) {
1526 err_action = SS_RETRY|SSQ_DECREMENT_COUNT|EIO;
1527 *relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1533 * If the recovery action will consume a retry,
1534 * make sure we actually have retries available.
1536 if ((err_action & SSQ_DECREMENT_COUNT) != 0) {
1537 if (ccb->ccb_h.retry_count > 0 &&
1538 (periph->flags & CAM_PERIPH_INVALID) == 0)
1539 ccb->ccb_h.retry_count--;
1541 *action_string = "Retries exhausted";
1542 goto sense_error_done;
1546 if ((err_action & SS_MASK) >= SS_START) {
1548 * Do common portions of commands that
1549 * use recovery CCBs.
1551 orig_ccb = xpt_alloc_ccb_nowait();
1552 if (orig_ccb == NULL) {
1553 *action_string = "Can't allocate recovery CCB";
1554 goto sense_error_done;
1557 * Clear freeze flag for original request here, as
1558 * this freeze will be dropped as part of ERESTART.
1560 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1561 bcopy(ccb, orig_ccb, sizeof(*orig_ccb));
1564 switch (err_action & SS_MASK) {
1566 *action_string = "No recovery action needed";
1570 *action_string = "Retrying command (per sense data)";
1574 *action_string = "Unretryable error";
1581 * Send a start unit command to the device, and
1582 * then retry the command.
1584 *action_string = "Attempting to start unit";
1585 periph->flags |= CAM_PERIPH_RECOVERY_INPROG;
1588 * Check for removable media and set
1589 * load/eject flag appropriately.
1591 if (SID_IS_REMOVABLE(&cgd.inq_data))
1596 scsi_start_stop(&ccb->csio,
1610 * Send a Test Unit Ready to the device.
1611 * If the 'many' flag is set, we send 120
1612 * test unit ready commands, one every half
1613 * second. Otherwise, we just send one TUR.
1614 * We only want to do this if the retry
1615 * count has not been exhausted.
1619 if ((err_action & SSQ_MANY) != 0) {
1620 *action_string = "Polling device for readiness";
1623 *action_string = "Testing device for readiness";
1626 periph->flags |= CAM_PERIPH_RECOVERY_INPROG;
1627 scsi_test_unit_ready(&ccb->csio,
1635 * Accomplish our 500ms delay by deferring
1636 * the release of our device queue appropriately.
1638 *relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1643 panic("Unhandled error action %x", err_action);
1646 if ((err_action & SS_MASK) >= SS_START) {
1648 * Drop the priority, so that the recovery
1649 * CCB is the first to execute. Freeze the queue
1650 * after this command is sent so that we can
1651 * restore the old csio and have it queued in
1652 * the proper order before we release normal
1653 * transactions to the device.
1655 ccb->ccb_h.pinfo.priority--;
1656 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1657 ccb->ccb_h.saved_ccb_ptr = orig_ccb;
1663 *action = err_action;
1669 * Generic error handler. Peripheral drivers usually filter
1670 * out the errors that they handle in a unique manner, then
1671 * call this function.
1674 cam_periph_error(union ccb *ccb, cam_flags camflags,
1675 u_int32_t sense_flags, union ccb *save_ccb)
1677 struct cam_path *newpath;
1678 union ccb *orig_ccb, *scan_ccb;
1679 struct cam_periph *periph;
1680 const char *action_string;
1682 int frozen, error, openings, devctl_err;
1683 u_int32_t action, relsim_flags, timeout;
1685 action = SSQ_PRINT_SENSE;
1686 periph = xpt_path_periph(ccb->ccb_h.path);
1687 action_string = NULL;
1688 status = ccb->ccb_h.status;
1689 frozen = (status & CAM_DEV_QFRZN) != 0;
1690 status &= CAM_STATUS_MASK;
1691 devctl_err = openings = relsim_flags = timeout = 0;
1694 /* Filter the errors that should be reported via devctl */
1695 switch (ccb->ccb_h.status & CAM_STATUS_MASK) {
1696 case CAM_CMD_TIMEOUT:
1697 case CAM_REQ_ABORTED:
1698 case CAM_REQ_CMP_ERR:
1699 case CAM_REQ_TERMIO:
1700 case CAM_UNREC_HBA_ERROR:
1701 case CAM_DATA_RUN_ERR:
1702 case CAM_SCSI_STATUS_ERROR:
1703 case CAM_ATA_STATUS_ERROR:
1704 case CAM_SMP_STATUS_ERROR:
1714 action &= ~SSQ_PRINT_SENSE;
1716 case CAM_SCSI_STATUS_ERROR:
1717 error = camperiphscsistatuserror(ccb, &orig_ccb,
1718 camflags, sense_flags, &openings, &relsim_flags,
1719 &timeout, &action, &action_string);
1721 case CAM_AUTOSENSE_FAIL:
1722 error = EIO; /* we have to kill the command */
1726 case CAM_MSG_REJECT_REC:
1727 /* XXX Don't know that these are correct */
1730 case CAM_SEL_TIMEOUT:
1731 if ((camflags & CAM_RETRY_SELTO) != 0) {
1732 if (ccb->ccb_h.retry_count > 0 &&
1733 (periph->flags & CAM_PERIPH_INVALID) == 0) {
1734 ccb->ccb_h.retry_count--;
1738 * Wait a bit to give the device
1739 * time to recover before we try again.
1741 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1742 timeout = periph_selto_delay;
1745 action_string = "Retries exhausted";
1748 case CAM_DEV_NOT_THERE:
1752 case CAM_REQ_INVALID:
1753 case CAM_PATH_INVALID:
1755 case CAM_PROVIDE_FAIL:
1756 case CAM_REQ_TOO_BIG:
1757 case CAM_LUN_INVALID:
1758 case CAM_TID_INVALID:
1759 case CAM_FUNC_NOTAVAIL:
1762 case CAM_SCSI_BUS_RESET:
1765 * Commands that repeatedly timeout and cause these
1766 * kinds of error recovery actions, should return
1767 * CAM_CMD_TIMEOUT, which allows us to safely assume
1768 * that this command was an innocent bystander to
1769 * these events and should be unconditionally
1772 case CAM_REQUEUE_REQ:
1773 /* Unconditional requeue if device is still there */
1774 if (periph->flags & CAM_PERIPH_INVALID) {
1775 action_string = "Periph was invalidated";
1777 } else if (sense_flags & SF_NO_RETRY) {
1779 action_string = "Retry was blocked";
1782 action &= ~SSQ_PRINT_SENSE;
1785 case CAM_RESRC_UNAVAIL:
1786 /* Wait a bit for the resource shortage to abate. */
1787 timeout = periph_noresrc_delay;
1791 /* Wait a bit for the busy condition to abate. */
1792 timeout = periph_busy_delay;
1794 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1796 case CAM_ATA_STATUS_ERROR:
1797 case CAM_REQ_CMP_ERR:
1798 case CAM_CMD_TIMEOUT:
1799 case CAM_UNEXP_BUSFREE:
1800 case CAM_UNCOR_PARITY:
1801 case CAM_DATA_RUN_ERR:
1803 if (periph->flags & CAM_PERIPH_INVALID) {
1805 action_string = "Periph was invalidated";
1806 } else if (ccb->ccb_h.retry_count == 0) {
1808 action_string = "Retries exhausted";
1809 } else if (sense_flags & SF_NO_RETRY) {
1811 action_string = "Retry was blocked";
1813 ccb->ccb_h.retry_count--;
1819 if ((sense_flags & SF_PRINT_ALWAYS) ||
1820 CAM_DEBUGGED(ccb->ccb_h.path, CAM_DEBUG_INFO))
1821 action |= SSQ_PRINT_SENSE;
1822 else if (sense_flags & SF_NO_PRINT)
1823 action &= ~SSQ_PRINT_SENSE;
1824 if ((action & SSQ_PRINT_SENSE) != 0)
1825 cam_error_print(orig_ccb, CAM_ESF_ALL, CAM_EPF_ALL);
1826 if (error != 0 && (action & SSQ_PRINT_SENSE) != 0) {
1827 if (error != ERESTART) {
1828 if (action_string == NULL)
1829 action_string = "Unretryable error";
1830 xpt_print(ccb->ccb_h.path, "Error %d, %s\n",
1831 error, action_string);
1832 } else if (action_string != NULL)
1833 xpt_print(ccb->ccb_h.path, "%s\n", action_string);
1835 xpt_print(ccb->ccb_h.path, "Retrying command\n");
1838 if (devctl_err && (error != 0 || (action & SSQ_PRINT_SENSE) != 0))
1839 cam_periph_devctl_notify(orig_ccb);
1841 if ((action & SSQ_LOST) != 0) {
1845 * For a selection timeout, we consider all of the LUNs on
1846 * the target to be gone. If the status is CAM_DEV_NOT_THERE,
1847 * then we only get rid of the device(s) specified by the
1848 * path in the original CCB.
1850 if (status == CAM_SEL_TIMEOUT)
1851 lun_id = CAM_LUN_WILDCARD;
1853 lun_id = xpt_path_lun_id(ccb->ccb_h.path);
1855 /* Should we do more if we can't create the path?? */
1856 if (xpt_create_path(&newpath, periph,
1857 xpt_path_path_id(ccb->ccb_h.path),
1858 xpt_path_target_id(ccb->ccb_h.path),
1859 lun_id) == CAM_REQ_CMP) {
1862 * Let peripheral drivers know that this
1863 * device has gone away.
1865 xpt_async(AC_LOST_DEVICE, newpath, NULL);
1866 xpt_free_path(newpath);
1870 /* Broadcast UNIT ATTENTIONs to all periphs. */
1871 if ((action & SSQ_UA) != 0)
1872 xpt_async(AC_UNIT_ATTENTION, orig_ccb->ccb_h.path, orig_ccb);
1874 /* Rescan target on "Reported LUNs data has changed" */
1875 if ((action & SSQ_RESCAN) != 0) {
1876 if (xpt_create_path(&newpath, NULL,
1877 xpt_path_path_id(ccb->ccb_h.path),
1878 xpt_path_target_id(ccb->ccb_h.path),
1879 CAM_LUN_WILDCARD) == CAM_REQ_CMP) {
1881 scan_ccb = xpt_alloc_ccb_nowait();
1882 if (scan_ccb != NULL) {
1883 scan_ccb->ccb_h.path = newpath;
1884 scan_ccb->ccb_h.func_code = XPT_SCAN_TGT;
1885 scan_ccb->crcn.flags = 0;
1886 xpt_rescan(scan_ccb);
1889 "Can't allocate CCB to rescan target\n");
1890 xpt_free_path(newpath);
1895 /* Attempt a retry */
1896 if (error == ERESTART || error == 0) {
1898 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1899 if (error == ERESTART)
1902 cam_release_devq(ccb->ccb_h.path,
1906 /*getcount_only*/0);
1912 #define CAM_PERIPH_DEVD_MSG_SIZE 256
1915 cam_periph_devctl_notify(union ccb *ccb)
1917 struct cam_periph *periph;
1918 struct ccb_getdev *cgd;
1920 int serr, sk, asc, ascq;
1923 sbmsg = malloc(CAM_PERIPH_DEVD_MSG_SIZE, M_CAMPERIPH, M_NOWAIT);
1927 sbuf_new(&sb, sbmsg, CAM_PERIPH_DEVD_MSG_SIZE, SBUF_FIXEDLEN);
1929 periph = xpt_path_periph(ccb->ccb_h.path);
1930 sbuf_printf(&sb, "device=%s%d ", periph->periph_name,
1931 periph->unit_number);
1933 sbuf_printf(&sb, "serial=\"");
1934 if ((cgd = (struct ccb_getdev *)xpt_alloc_ccb_nowait()) != NULL) {
1935 xpt_setup_ccb(&cgd->ccb_h, ccb->ccb_h.path,
1936 CAM_PRIORITY_NORMAL);
1937 cgd->ccb_h.func_code = XPT_GDEV_TYPE;
1938 xpt_action((union ccb *)cgd);
1940 if (cgd->ccb_h.status == CAM_REQ_CMP)
1941 sbuf_bcat(&sb, cgd->serial_num, cgd->serial_num_len);
1942 xpt_free_ccb((union ccb *)cgd);
1944 sbuf_printf(&sb, "\" ");
1945 sbuf_printf(&sb, "cam_status=\"0x%x\" ", ccb->ccb_h.status);
1947 switch (ccb->ccb_h.status & CAM_STATUS_MASK) {
1948 case CAM_CMD_TIMEOUT:
1949 sbuf_printf(&sb, "timeout=%d ", ccb->ccb_h.timeout);
1952 case CAM_SCSI_STATUS_ERROR:
1953 sbuf_printf(&sb, "scsi_status=%d ", ccb->csio.scsi_status);
1954 if (scsi_extract_sense_ccb(ccb, &serr, &sk, &asc, &ascq))
1955 sbuf_printf(&sb, "scsi_sense=\"%02x %02x %02x %02x\" ",
1956 serr, sk, asc, ascq);
1959 case CAM_ATA_STATUS_ERROR:
1960 sbuf_printf(&sb, "RES=\"");
1961 ata_res_sbuf(&ccb->ataio.res, &sb);
1962 sbuf_printf(&sb, "\" ");
1970 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1971 sbuf_printf(&sb, "CDB=\"");
1972 scsi_cdb_sbuf(scsiio_cdb_ptr(&ccb->csio), &sb);
1973 sbuf_printf(&sb, "\" ");
1974 } else if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1975 sbuf_printf(&sb, "ACB=\"");
1976 ata_cmd_sbuf(&ccb->ataio.cmd, &sb);
1977 sbuf_printf(&sb, "\" ");
1980 if (sbuf_finish(&sb) == 0)
1981 devctl_notify("CAM", "periph", type, sbuf_data(&sb));
1983 free(sbmsg, M_CAMPERIPH);