2 * Common functions for CAM "type" (peripheral) drivers.
4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
6 * Copyright (c) 1997, 1998 Justin T. Gibbs.
7 * Copyright (c) 1997, 1998, 1999, 2000 Kenneth D. Merry.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions, and the following disclaimer,
15 * without modification, immediately at the beginning of the file.
16 * 2. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/types.h>
38 #include <sys/malloc.h>
39 #include <sys/kernel.h>
42 #include <sys/devctl.h>
44 #include <sys/mutex.h>
47 #include <sys/devicestat.h>
49 #include <sys/sysctl.h>
51 #include <vm/vm_extern.h>
54 #include <cam/cam_ccb.h>
55 #include <cam/cam_queue.h>
56 #include <cam/cam_xpt_periph.h>
57 #include <cam/cam_xpt_internal.h>
58 #include <cam/cam_periph.h>
59 #include <cam/cam_debug.h>
60 #include <cam/cam_sim.h>
62 #include <cam/scsi/scsi_all.h>
63 #include <cam/scsi/scsi_message.h>
64 #include <cam/scsi/scsi_pass.h>
66 static u_int camperiphnextunit(struct periph_driver *p_drv,
67 u_int newunit, bool wired,
68 path_id_t pathid, target_id_t target,
70 static u_int camperiphunit(struct periph_driver *p_drv,
71 path_id_t pathid, target_id_t target,
74 static void camperiphdone(struct cam_periph *periph,
76 static void camperiphfree(struct cam_periph *periph);
77 static int camperiphscsistatuserror(union ccb *ccb,
80 u_int32_t sense_flags,
82 u_int32_t *relsim_flags,
85 const char **action_string);
86 static int camperiphscsisenseerror(union ccb *ccb,
89 u_int32_t sense_flags,
91 u_int32_t *relsim_flags,
94 const char **action_string);
95 static void cam_periph_devctl_notify(union ccb *ccb);
97 static int nperiph_drivers;
98 static int initialized = 0;
99 struct periph_driver **periph_drivers;
101 static MALLOC_DEFINE(M_CAMPERIPH, "CAM periph", "CAM peripheral buffers");
103 static int periph_selto_delay = 1000;
104 TUNABLE_INT("kern.cam.periph_selto_delay", &periph_selto_delay);
105 static int periph_noresrc_delay = 500;
106 TUNABLE_INT("kern.cam.periph_noresrc_delay", &periph_noresrc_delay);
107 static int periph_busy_delay = 500;
108 TUNABLE_INT("kern.cam.periph_busy_delay", &periph_busy_delay);
110 static u_int periph_mapmem_thresh = 65536;
111 SYSCTL_UINT(_kern_cam, OID_AUTO, mapmem_thresh, CTLFLAG_RWTUN,
112 &periph_mapmem_thresh, 0, "Threshold for user-space buffer mapping");
115 periphdriver_register(void *data)
117 struct periph_driver *drv = (struct periph_driver *)data;
118 struct periph_driver **newdrivers, **old;
122 ndrivers = nperiph_drivers + 2;
123 newdrivers = malloc(sizeof(*newdrivers) * ndrivers, M_CAMPERIPH,
126 if (ndrivers != nperiph_drivers + 2) {
128 * Lost race against itself; go around.
131 free(newdrivers, M_CAMPERIPH);
135 bcopy(periph_drivers, newdrivers,
136 sizeof(*newdrivers) * nperiph_drivers);
137 newdrivers[nperiph_drivers] = drv;
138 newdrivers[nperiph_drivers + 1] = NULL;
139 old = periph_drivers;
140 periph_drivers = newdrivers;
144 free(old, M_CAMPERIPH);
145 /* If driver marked as early or it is late now, initialize it. */
146 if (((drv->flags & CAM_PERIPH_DRV_EARLY) != 0 && initialized > 0) ||
152 periphdriver_unregister(void *data)
154 struct periph_driver *drv = (struct periph_driver *)data;
157 /* If driver marked as early or it is late now, deinitialize it. */
158 if (((drv->flags & CAM_PERIPH_DRV_EARLY) != 0 && initialized > 0) ||
160 if (drv->deinit == NULL) {
161 printf("CAM periph driver '%s' doesn't have deinit.\n",
165 error = drv->deinit();
171 for (n = 0; n < nperiph_drivers && periph_drivers[n] != drv; n++)
173 KASSERT(n < nperiph_drivers,
174 ("Periph driver '%s' was not registered", drv->driver_name));
175 for (; n + 1 < nperiph_drivers; n++)
176 periph_drivers[n] = periph_drivers[n + 1];
177 periph_drivers[n + 1] = NULL;
184 periphdriver_init(int level)
188 initialized = max(initialized, level);
189 for (i = 0; periph_drivers[i] != NULL; i++) {
190 early = (periph_drivers[i]->flags & CAM_PERIPH_DRV_EARLY) ? 1 : 2;
191 if (early == initialized)
192 (*periph_drivers[i]->init)();
197 cam_periph_alloc(periph_ctor_t *periph_ctor,
198 periph_oninv_t *periph_oninvalidate,
199 periph_dtor_t *periph_dtor, periph_start_t *periph_start,
200 char *name, cam_periph_type type, struct cam_path *path,
201 ac_callback_t *ac_callback, ac_code code, void *arg)
203 struct periph_driver **p_drv;
205 struct cam_periph *periph;
206 struct cam_periph *cur_periph;
208 target_id_t target_id;
215 * Handle Hot-Plug scenarios. If there is already a peripheral
216 * of our type assigned to this path, we are likely waiting for
217 * final close on an old, invalidated, peripheral. If this is
218 * the case, queue up a deferred call to the peripheral's async
219 * handler. If it looks like a mistaken re-allocation, complain.
221 if ((periph = cam_periph_find(path, name)) != NULL) {
222 if ((periph->flags & CAM_PERIPH_INVALID) != 0
223 && (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) == 0) {
224 periph->flags |= CAM_PERIPH_NEW_DEV_FOUND;
225 periph->deferred_callback = ac_callback;
226 periph->deferred_ac = code;
227 return (CAM_REQ_INPROG);
229 printf("cam_periph_alloc: attempt to re-allocate "
230 "valid device %s%d rejected flags %#x "
231 "refcount %d\n", periph->periph_name,
232 periph->unit_number, periph->flags,
235 return (CAM_REQ_INVALID);
238 periph = (struct cam_periph *)malloc(sizeof(*periph), M_CAMPERIPH,
242 return (CAM_RESRC_UNAVAIL);
246 sim = xpt_path_sim(path);
247 path_id = xpt_path_path_id(path);
248 target_id = xpt_path_target_id(path);
249 lun_id = xpt_path_lun_id(path);
250 periph->periph_start = periph_start;
251 periph->periph_dtor = periph_dtor;
252 periph->periph_oninval = periph_oninvalidate;
254 periph->periph_name = name;
255 periph->scheduled_priority = CAM_PRIORITY_NONE;
256 periph->immediate_priority = CAM_PRIORITY_NONE;
257 periph->refcount = 1; /* Dropped by invalidation. */
259 SLIST_INIT(&periph->ccb_list);
260 status = xpt_create_path(&path, periph, path_id, target_id, lun_id);
261 if (status != CAM_REQ_CMP)
266 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
267 if (strcmp((*p_drv)->driver_name, name) == 0)
270 if (*p_drv == NULL) {
271 printf("cam_periph_alloc: invalid periph name '%s'\n", name);
273 xpt_free_path(periph->path);
274 free(periph, M_CAMPERIPH);
275 return (CAM_REQ_INVALID);
277 periph->unit_number = camperiphunit(*p_drv, path_id, target_id, lun_id,
278 path->device->serial_num);
279 cur_periph = TAILQ_FIRST(&(*p_drv)->units);
280 while (cur_periph != NULL
281 && cur_periph->unit_number < periph->unit_number)
282 cur_periph = TAILQ_NEXT(cur_periph, unit_links);
283 if (cur_periph != NULL) {
284 KASSERT(cur_periph->unit_number != periph->unit_number,
285 ("duplicate units on periph list"));
286 TAILQ_INSERT_BEFORE(cur_periph, periph, unit_links);
288 TAILQ_INSERT_TAIL(&(*p_drv)->units, periph, unit_links);
289 (*p_drv)->generation++;
295 status = xpt_add_periph(periph);
296 if (status != CAM_REQ_CMP)
300 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph created\n"));
302 status = periph_ctor(periph, arg);
304 if (status == CAM_REQ_CMP)
308 switch (init_level) {
310 /* Initialized successfully */
313 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph destroyed\n"));
314 xpt_remove_periph(periph);
318 TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
320 xpt_free_path(periph->path);
323 free(periph, M_CAMPERIPH);
326 /* No cleanup to perform. */
329 panic("%s: Unknown init level", __func__);
335 * Find a peripheral structure with the specified path, target, lun,
336 * and (optionally) type. If the name is NULL, this function will return
337 * the first peripheral driver that matches the specified path.
340 cam_periph_find(struct cam_path *path, char *name)
342 struct periph_driver **p_drv;
343 struct cam_periph *periph;
346 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
347 if (name != NULL && (strcmp((*p_drv)->driver_name, name) != 0))
350 TAILQ_FOREACH(periph, &(*p_drv)->units, unit_links) {
351 if (xpt_path_comp(periph->path, path) == 0) {
353 cam_periph_assert(periph, MA_OWNED);
367 * Find peripheral driver instances attached to the specified path.
370 cam_periph_list(struct cam_path *path, struct sbuf *sb)
372 struct sbuf local_sb;
373 struct periph_driver **p_drv;
374 struct cam_periph *periph;
380 sbuf_new(&local_sb, NULL, sbuf_alloc_len, SBUF_FIXEDLEN);
383 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
384 TAILQ_FOREACH(periph, &(*p_drv)->units, unit_links) {
385 if (xpt_path_comp(periph->path, path) != 0)
388 if (sbuf_len(&local_sb) != 0)
389 sbuf_cat(&local_sb, ",");
391 sbuf_printf(&local_sb, "%s%d", periph->periph_name,
392 periph->unit_number);
394 if (sbuf_error(&local_sb) == ENOMEM) {
397 sbuf_delete(&local_sb);
404 sbuf_finish(&local_sb);
405 if (sbuf_len(sb) != 0)
407 sbuf_cat(sb, sbuf_data(&local_sb));
408 sbuf_delete(&local_sb);
413 cam_periph_acquire(struct cam_periph *periph)
422 if ((periph->flags & CAM_PERIPH_INVALID) == 0) {
432 cam_periph_doacquire(struct cam_periph *periph)
436 KASSERT(periph->refcount >= 1,
437 ("cam_periph_doacquire() with refcount == %d", periph->refcount));
443 cam_periph_release_locked_buses(struct cam_periph *periph)
446 cam_periph_assert(periph, MA_OWNED);
447 KASSERT(periph->refcount >= 1, ("periph->refcount >= 1"));
448 if (--periph->refcount == 0)
449 camperiphfree(periph);
453 cam_periph_release_locked(struct cam_periph *periph)
460 cam_periph_release_locked_buses(periph);
465 cam_periph_release(struct cam_periph *periph)
472 cam_periph_assert(periph, MA_NOTOWNED);
473 mtx = cam_periph_mtx(periph);
475 cam_periph_release_locked(periph);
480 * hold/unhold act as mutual exclusion for sections of the code that
481 * need to sleep and want to make sure that other sections that
482 * will interfere are held off. This only protects exclusive sections
486 cam_periph_hold(struct cam_periph *periph, int priority)
491 * Increment the reference count on the peripheral
492 * while we wait for our lock attempt to succeed
493 * to ensure the peripheral doesn't disappear out
494 * from user us while we sleep.
497 if (cam_periph_acquire(periph) != 0)
500 cam_periph_assert(periph, MA_OWNED);
501 while ((periph->flags & CAM_PERIPH_LOCKED) != 0) {
502 periph->flags |= CAM_PERIPH_LOCK_WANTED;
503 if ((error = cam_periph_sleep(periph, periph, priority,
504 "caplck", 0)) != 0) {
505 cam_periph_release_locked(periph);
508 if (periph->flags & CAM_PERIPH_INVALID) {
509 cam_periph_release_locked(periph);
514 periph->flags |= CAM_PERIPH_LOCKED;
519 cam_periph_unhold(struct cam_periph *periph)
522 cam_periph_assert(periph, MA_OWNED);
524 periph->flags &= ~CAM_PERIPH_LOCKED;
525 if ((periph->flags & CAM_PERIPH_LOCK_WANTED) != 0) {
526 periph->flags &= ~CAM_PERIPH_LOCK_WANTED;
530 cam_periph_release_locked(periph);
534 * Look for the next unit number that is not currently in use for this
535 * peripheral type starting at "newunit". Also exclude unit numbers that
536 * are reserved by for future "hardwiring" unless we already know that this
537 * is a potential wired device. Only assume that the device is "wired" the
538 * first time through the loop since after that we'll be looking at unit
539 * numbers that did not match a wiring entry.
542 camperiphnextunit(struct periph_driver *p_drv, u_int newunit, bool wired,
543 path_id_t pathid, target_id_t target, lun_id_t lun)
545 struct cam_periph *periph;
547 int i, val, dunit, r;
548 const char *dname, *strval;
550 periph_name = p_drv->driver_name;
552 for (periph = TAILQ_FIRST(&p_drv->units);
553 periph != NULL && periph->unit_number != newunit;
554 periph = TAILQ_NEXT(periph, unit_links))
557 if (periph != NULL && periph->unit_number == newunit) {
559 xpt_print(periph->path, "Duplicate Wired "
561 xpt_print(periph->path, "Second device (%s "
562 "device at scbus%d target %d lun %d) will "
563 "not be wired\n", periph_name, pathid,
573 * Don't allow the mere presence of any attributes of a device
574 * means that it is for a wired down entry. Instead, insist that
575 * one of the matching criteria from camperiphunit be present
581 r = resource_find_dev(&i, dname, &dunit, NULL, NULL);
585 if (newunit != dunit)
587 if (resource_string_value(dname, dunit, "sn", &strval) == 0 ||
588 resource_int_value(dname, dunit, "lun", &val) == 0 ||
589 resource_int_value(dname, dunit, "target", &val) == 0 ||
590 resource_string_value(dname, dunit, "at", &strval) == 0)
600 camperiphunit(struct periph_driver *p_drv, path_id_t pathid,
601 target_id_t target, lun_id_t lun, const char *sn)
606 const char *dname, *strval;
607 char pathbuf[32], *periph_name;
609 periph_name = p_drv->driver_name;
610 snprintf(pathbuf, sizeof(pathbuf), "scbus%d", pathid);
615 for (wired = false; resource_find_dev(&i, dname, &dunit, NULL, NULL) == 0;
617 if (resource_string_value(dname, dunit, "at", &strval) == 0) {
618 if (strcmp(strval, pathbuf) != 0)
622 if (resource_int_value(dname, dunit, "target", &val) == 0) {
627 if (resource_int_value(dname, dunit, "lun", &val) == 0) {
632 if (resource_string_value(dname, dunit, "sn", &strval) == 0) {
633 if (sn == NULL || strcmp(strval, sn) != 0)
644 * Either start from 0 looking for the next unit or from
645 * the unit number given in the resource config. This way,
646 * if we have wildcard matches, we don't return the same
649 unit = camperiphnextunit(p_drv, unit, wired, pathid, target, lun);
655 cam_periph_invalidate(struct cam_periph *periph)
658 cam_periph_assert(periph, MA_OWNED);
660 * We only tear down the device the first time a peripheral is
663 if ((periph->flags & CAM_PERIPH_INVALID) != 0)
666 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph invalidated\n"));
667 if ((periph->flags & CAM_PERIPH_ANNOUNCED) && !rebooting) {
671 sbuf_new(&sb, buffer, 160, SBUF_FIXEDLEN);
672 xpt_denounce_periph_sbuf(periph, &sb);
676 periph->flags |= CAM_PERIPH_INVALID;
677 periph->flags &= ~CAM_PERIPH_NEW_DEV_FOUND;
678 if (periph->periph_oninval != NULL)
679 periph->periph_oninval(periph);
680 cam_periph_release_locked(periph);
684 camperiphfree(struct cam_periph *periph)
686 struct periph_driver **p_drv;
687 struct periph_driver *drv;
689 cam_periph_assert(periph, MA_OWNED);
690 KASSERT(periph->periph_allocating == 0, ("%s%d: freed while allocating",
691 periph->periph_name, periph->unit_number));
692 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
693 if (strcmp((*p_drv)->driver_name, periph->periph_name) == 0)
696 if (*p_drv == NULL) {
697 printf("camperiphfree: attempt to free non-existant periph\n");
701 * Cache a pointer to the periph_driver structure. If a
702 * periph_driver is added or removed from the array (see
703 * periphdriver_register()) while we drop the toplogy lock
704 * below, p_drv may change. This doesn't protect against this
705 * particular periph_driver going away. That will require full
706 * reference counting in the periph_driver infrastructure.
711 * We need to set this flag before dropping the topology lock, to
712 * let anyone who is traversing the list that this peripheral is
713 * about to be freed, and there will be no more reference count
716 periph->flags |= CAM_PERIPH_FREE;
719 * The peripheral destructor semantics dictate calling with only the
720 * SIM mutex held. Since it might sleep, it should not be called
721 * with the topology lock held.
726 * We need to call the peripheral destructor prior to removing the
727 * peripheral from the list. Otherwise, we risk running into a
728 * scenario where the peripheral unit number may get reused
729 * (because it has been removed from the list), but some resources
730 * used by the peripheral are still hanging around. In particular,
731 * the devfs nodes used by some peripherals like the pass(4) driver
732 * aren't fully cleaned up until the destructor is run. If the
733 * unit number is reused before the devfs instance is fully gone,
736 if (periph->periph_dtor != NULL)
737 periph->periph_dtor(periph);
740 * The peripheral list is protected by the topology lock. We have to
741 * remove the periph from the drv list before we call deferred_ac. The
742 * AC_FOUND_DEVICE callback won't create a new periph if it's still there.
746 TAILQ_REMOVE(&drv->units, periph, unit_links);
749 xpt_remove_periph(periph);
752 if ((periph->flags & CAM_PERIPH_ANNOUNCED) && !rebooting)
753 xpt_print(periph->path, "Periph destroyed\n");
755 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph destroyed\n"));
757 if (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) {
761 memset(&ccb, 0, sizeof(ccb));
762 switch (periph->deferred_ac) {
763 case AC_FOUND_DEVICE:
764 ccb.ccb_h.func_code = XPT_GDEV_TYPE;
765 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
769 case AC_PATH_REGISTERED:
770 xpt_path_inq(&ccb.cpi, periph->path);
777 periph->deferred_callback(NULL, periph->deferred_ac,
780 xpt_free_path(periph->path);
781 free(periph, M_CAMPERIPH);
786 * Map user virtual pointers into kernel virtual address space, so we can
787 * access the memory. This is now a generic function that centralizes most
788 * of the sanity checks on the data flags, if any.
789 * This also only works for up to maxphys memory. Since we use
790 * buffers to map stuff in and out, we're limited to the buffer size.
793 cam_periph_mapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo,
797 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
798 u_int32_t lengths[CAM_PERIPH_MAXMAPS];
799 u_int32_t dirs[CAM_PERIPH_MAXMAPS];
801 bzero(mapinfo, sizeof(*mapinfo));
803 maxmap = DFLTPHYS; /* traditional default */
804 else if (maxmap > maxphys)
805 maxmap = maxphys; /* for safety */
806 switch(ccb->ccb_h.func_code) {
808 if (ccb->cdm.match_buf_len == 0) {
809 printf("cam_periph_mapmem: invalid match buffer "
813 if (ccb->cdm.pattern_buf_len > 0) {
814 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
815 lengths[0] = ccb->cdm.pattern_buf_len;
816 dirs[0] = CAM_DIR_OUT;
817 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
818 lengths[1] = ccb->cdm.match_buf_len;
819 dirs[1] = CAM_DIR_IN;
822 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
823 lengths[0] = ccb->cdm.match_buf_len;
824 dirs[0] = CAM_DIR_IN;
828 * This request will not go to the hardware, no reason
829 * to be so strict. vmapbuf() is able to map up to maxphys.
834 case XPT_CONT_TARGET_IO:
835 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
837 if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR)
839 data_ptrs[0] = &ccb->csio.data_ptr;
840 lengths[0] = ccb->csio.dxfer_len;
841 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
845 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
847 if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR)
849 data_ptrs[0] = &ccb->ataio.data_ptr;
850 lengths[0] = ccb->ataio.dxfer_len;
851 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
855 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
857 /* Two mappings: one for cmd->data and one for cmd->data->data */
858 data_ptrs[0] = (unsigned char **)&ccb->mmcio.cmd.data;
859 lengths[0] = sizeof(struct mmc_data *);
860 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
861 data_ptrs[1] = (unsigned char **)&ccb->mmcio.cmd.data->data;
862 lengths[1] = ccb->mmcio.cmd.data->len;
863 dirs[1] = ccb->ccb_h.flags & CAM_DIR_MASK;
867 data_ptrs[0] = &ccb->smpio.smp_request;
868 lengths[0] = ccb->smpio.smp_request_len;
869 dirs[0] = CAM_DIR_OUT;
870 data_ptrs[1] = &ccb->smpio.smp_response;
871 lengths[1] = ccb->smpio.smp_response_len;
872 dirs[1] = CAM_DIR_IN;
877 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
879 if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR)
881 data_ptrs[0] = &ccb->nvmeio.data_ptr;
882 lengths[0] = ccb->nvmeio.dxfer_len;
883 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
886 case XPT_DEV_ADVINFO:
887 if (ccb->cdai.bufsiz == 0)
890 data_ptrs[0] = (uint8_t **)&ccb->cdai.buf;
891 lengths[0] = ccb->cdai.bufsiz;
892 dirs[0] = CAM_DIR_IN;
896 * This request will not go to the hardware, no reason
897 * to be so strict. vmapbuf() is able to map up to maxphys.
903 break; /* NOTREACHED */
907 * Check the transfer length and permissions first, so we don't
908 * have to unmap any previously mapped buffers.
910 for (i = 0; i < numbufs; i++) {
911 if (lengths[i] > maxmap) {
912 printf("cam_periph_mapmem: attempt to map %lu bytes, "
913 "which is greater than %lu\n",
914 (long)(lengths[i]), (u_long)maxmap);
920 * This keeps the kernel stack of current thread from getting
921 * swapped. In low-memory situations where the kernel stack might
922 * otherwise get swapped out, this holds it and allows the thread
923 * to make progress and release the kernel mapped pages sooner.
925 * XXX KDM should I use P_NOSWAP instead?
929 for (i = 0; i < numbufs; i++) {
930 /* Save the user's data address. */
931 mapinfo->orig[i] = *data_ptrs[i];
934 * For small buffers use malloc+copyin/copyout instead of
935 * mapping to KVA to avoid expensive TLB shootdowns. For
936 * small allocations malloc is backed by UMA, and so much
937 * cheaper on SMP systems.
939 if (lengths[i] <= periph_mapmem_thresh &&
940 ccb->ccb_h.func_code != XPT_MMC_IO) {
941 *data_ptrs[i] = malloc(lengths[i], M_CAMPERIPH,
943 if (dirs[i] != CAM_DIR_IN) {
944 if (copyin(mapinfo->orig[i], *data_ptrs[i],
946 free(*data_ptrs[i], M_CAMPERIPH);
947 *data_ptrs[i] = mapinfo->orig[i];
951 bzero(*data_ptrs[i], lengths[i]);
958 mapinfo->bp[i] = uma_zalloc(pbuf_zone, M_WAITOK);
960 /* set the direction */
961 mapinfo->bp[i]->b_iocmd = (dirs[i] == CAM_DIR_OUT) ?
962 BIO_WRITE : BIO_READ;
964 /* Map the buffer into kernel memory. */
965 if (vmapbuf(mapinfo->bp[i], *data_ptrs[i], lengths[i], 1) < 0) {
966 uma_zfree(pbuf_zone, mapinfo->bp[i]);
970 /* set our pointer to the new mapped area */
971 *data_ptrs[i] = mapinfo->bp[i]->b_data;
975 * Now that we've gotten this far, change ownership to the kernel
976 * of the buffers so that we don't run afoul of returning to user
977 * space with locks (on the buffer) held.
979 for (i = 0; i < numbufs; i++) {
981 BUF_KERNPROC(mapinfo->bp[i]);
984 mapinfo->num_bufs_used = numbufs;
988 for (i--; i >= 0; i--) {
989 if (mapinfo->bp[i]) {
990 vunmapbuf(mapinfo->bp[i]);
991 uma_zfree(pbuf_zone, mapinfo->bp[i]);
993 free(*data_ptrs[i], M_CAMPERIPH);
994 *data_ptrs[i] = mapinfo->orig[i];
1001 * Unmap memory segments mapped into kernel virtual address space by
1002 * cam_periph_mapmem().
1005 cam_periph_unmapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
1008 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
1009 u_int32_t lengths[CAM_PERIPH_MAXMAPS];
1010 u_int32_t dirs[CAM_PERIPH_MAXMAPS];
1012 if (mapinfo->num_bufs_used <= 0) {
1013 /* nothing to free and the process wasn't held. */
1017 switch (ccb->ccb_h.func_code) {
1019 if (ccb->cdm.pattern_buf_len > 0) {
1020 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
1021 lengths[0] = ccb->cdm.pattern_buf_len;
1022 dirs[0] = CAM_DIR_OUT;
1023 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
1024 lengths[1] = ccb->cdm.match_buf_len;
1025 dirs[1] = CAM_DIR_IN;
1028 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
1029 lengths[0] = ccb->cdm.match_buf_len;
1030 dirs[0] = CAM_DIR_IN;
1035 case XPT_CONT_TARGET_IO:
1036 data_ptrs[0] = &ccb->csio.data_ptr;
1037 lengths[0] = ccb->csio.dxfer_len;
1038 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
1042 data_ptrs[0] = &ccb->ataio.data_ptr;
1043 lengths[0] = ccb->ataio.dxfer_len;
1044 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
1048 data_ptrs[0] = (u_int8_t **)&ccb->mmcio.cmd.data;
1049 lengths[0] = sizeof(struct mmc_data *);
1050 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
1051 data_ptrs[1] = (u_int8_t **)&ccb->mmcio.cmd.data->data;
1052 lengths[1] = ccb->mmcio.cmd.data->len;
1053 dirs[1] = ccb->ccb_h.flags & CAM_DIR_MASK;
1057 data_ptrs[0] = &ccb->smpio.smp_request;
1058 lengths[0] = ccb->smpio.smp_request_len;
1059 dirs[0] = CAM_DIR_OUT;
1060 data_ptrs[1] = &ccb->smpio.smp_response;
1061 lengths[1] = ccb->smpio.smp_response_len;
1062 dirs[1] = CAM_DIR_IN;
1066 case XPT_NVME_ADMIN:
1067 data_ptrs[0] = &ccb->nvmeio.data_ptr;
1068 lengths[0] = ccb->nvmeio.dxfer_len;
1069 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
1072 case XPT_DEV_ADVINFO:
1073 data_ptrs[0] = (uint8_t **)&ccb->cdai.buf;
1074 lengths[0] = ccb->cdai.bufsiz;
1075 dirs[0] = CAM_DIR_IN;
1079 /* allow ourselves to be swapped once again */
1082 break; /* NOTREACHED */
1085 for (i = 0; i < numbufs; i++) {
1086 if (mapinfo->bp[i]) {
1087 /* unmap the buffer */
1088 vunmapbuf(mapinfo->bp[i]);
1090 /* release the buffer */
1091 uma_zfree(pbuf_zone, mapinfo->bp[i]);
1093 if (dirs[i] != CAM_DIR_OUT) {
1094 copyout(*data_ptrs[i], mapinfo->orig[i],
1097 free(*data_ptrs[i], M_CAMPERIPH);
1100 /* Set the user's pointer back to the original value */
1101 *data_ptrs[i] = mapinfo->orig[i];
1104 /* allow ourselves to be swapped once again */
1109 cam_periph_ioctl(struct cam_periph *periph, u_long cmd, caddr_t addr,
1110 int (*error_routine)(union ccb *ccb,
1112 u_int32_t sense_flags))
1121 case CAMGETPASSTHRU:
1122 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1123 xpt_setup_ccb(&ccb->ccb_h,
1125 CAM_PRIORITY_NORMAL);
1126 ccb->ccb_h.func_code = XPT_GDEVLIST;
1129 * Basically, the point of this is that we go through
1130 * getting the list of devices, until we find a passthrough
1131 * device. In the current version of the CAM code, the
1132 * only way to determine what type of device we're dealing
1133 * with is by its name.
1135 while (found == 0) {
1136 ccb->cgdl.index = 0;
1137 ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS;
1138 while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) {
1139 /* we want the next device in the list */
1141 if (strncmp(ccb->cgdl.periph_name,
1147 if ((ccb->cgdl.status == CAM_GDEVLIST_LAST_DEVICE) &&
1149 ccb->cgdl.periph_name[0] = '\0';
1150 ccb->cgdl.unit_number = 0;
1155 /* copy the result back out */
1156 bcopy(ccb, addr, sizeof(union ccb));
1158 /* and release the ccb */
1159 xpt_release_ccb(ccb);
1170 cam_periph_done_panic(struct cam_periph *periph, union ccb *done_ccb)
1173 panic("%s: already done with ccb %p", __func__, done_ccb);
1177 cam_periph_done(struct cam_periph *periph, union ccb *done_ccb)
1180 /* Caller will release the CCB */
1181 xpt_path_assert(done_ccb->ccb_h.path, MA_OWNED);
1182 done_ccb->ccb_h.cbfcnp = cam_periph_done_panic;
1183 wakeup(&done_ccb->ccb_h.cbfcnp);
1187 cam_periph_ccbwait(union ccb *ccb)
1190 if ((ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
1191 while (ccb->ccb_h.cbfcnp != cam_periph_done_panic)
1192 xpt_path_sleep(ccb->ccb_h.path, &ccb->ccb_h.cbfcnp,
1193 PRIBIO, "cbwait", 0);
1195 KASSERT(ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX &&
1196 (ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG,
1197 ("%s: proceeding with incomplete ccb: ccb=%p, func_code=%#x, "
1198 "status=%#x, index=%d", __func__, ccb, ccb->ccb_h.func_code,
1199 ccb->ccb_h.status, ccb->ccb_h.pinfo.index));
1203 * Dispatch a CCB and wait for it to complete. If the CCB has set a
1204 * callback function (ccb->ccb_h.cbfcnp), it will be overwritten and lost.
1207 cam_periph_runccb(union ccb *ccb,
1208 int (*error_routine)(union ccb *ccb,
1210 u_int32_t sense_flags),
1211 cam_flags camflags, u_int32_t sense_flags,
1214 struct bintime *starttime;
1215 struct bintime ltime;
1218 uint32_t timeout = 1;
1221 xpt_path_assert(ccb->ccb_h.path, MA_OWNED);
1222 KASSERT((ccb->ccb_h.flags & CAM_UNLOCKED) == 0,
1223 ("%s: ccb=%p, func_code=%#x, flags=%#x", __func__, ccb,
1224 ccb->ccb_h.func_code, ccb->ccb_h.flags));
1227 * If the user has supplied a stats structure, and if we understand
1228 * this particular type of ccb, record the transaction start.
1231 (ccb->ccb_h.func_code == XPT_SCSI_IO ||
1232 ccb->ccb_h.func_code == XPT_ATA_IO ||
1233 ccb->ccb_h.func_code == XPT_NVME_IO)) {
1235 binuptime(starttime);
1236 devstat_start_transaction(ds, starttime);
1240 * We must poll the I/O while we're dumping. The scheduler is normally
1241 * stopped for dumping, except when we call doadump from ddb. While the
1242 * scheduler is running in this case, we still need to poll the I/O to
1243 * avoid sleeping waiting for the ccb to complete.
1245 * A panic triggered dump stops the scheduler, any callback from the
1246 * shutdown_post_sync event will run with the scheduler stopped, but
1247 * before we're officially dumping. To avoid hanging in adashutdown
1248 * initiated commands (or other similar situations), we have to test for
1249 * either SCHEDULER_STOPPED() here as well.
1251 * To avoid locking problems, dumping/polling callers must call
1252 * without a periph lock held.
1254 must_poll = dumping || SCHEDULER_STOPPED();
1255 ccb->ccb_h.cbfcnp = cam_periph_done;
1258 * If we're polling, then we need to ensure that we have ample resources
1259 * in the periph. cam_periph_error can reschedule the ccb by calling
1260 * xpt_action and returning ERESTART, so we have to effect the polling
1261 * in the do loop below.
1264 if (cam_sim_pollable(ccb->ccb_h.path->bus->sim))
1265 timeout = xpt_poll_setup(ccb);
1271 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1277 xpt_pollwait(ccb, timeout);
1278 timeout = ccb->ccb_h.timeout * 10;
1280 cam_periph_ccbwait(ccb);
1282 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
1284 else if (error_routine != NULL) {
1285 ccb->ccb_h.cbfcnp = cam_periph_done;
1286 error = (*error_routine)(ccb, camflags, sense_flags);
1289 } while (error == ERESTART);
1292 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1293 cam_release_devq(ccb->ccb_h.path,
1294 /* relsim_flags */0,
1297 /* getcount_only */ FALSE);
1298 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1303 devstat_tag_type tag;
1306 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1307 bytes = ccb->csio.dxfer_len - ccb->csio.resid;
1308 tag = (devstat_tag_type)(ccb->csio.tag_action & 0x3);
1309 } else if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1310 bytes = ccb->ataio.dxfer_len - ccb->ataio.resid;
1311 tag = (devstat_tag_type)0;
1312 } else if (ccb->ccb_h.func_code == XPT_NVME_IO) {
1313 bytes = ccb->nvmeio.dxfer_len; /* NB: resid no possible */
1314 tag = (devstat_tag_type)0;
1319 devstat_end_transaction(ds, bytes, tag,
1320 ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE) ?
1321 DEVSTAT_NO_DATA : (ccb->ccb_h.flags & CAM_DIR_OUT) ?
1322 DEVSTAT_WRITE : DEVSTAT_READ, NULL, starttime);
1329 cam_freeze_devq(struct cam_path *path)
1331 struct ccb_hdr ccb_h;
1333 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("cam_freeze_devq\n"));
1334 memset(&ccb_h, 0, sizeof(ccb_h));
1335 xpt_setup_ccb(&ccb_h, path, /*priority*/1);
1336 ccb_h.func_code = XPT_NOOP;
1337 ccb_h.flags = CAM_DEV_QFREEZE;
1338 xpt_action((union ccb *)&ccb_h);
1342 cam_release_devq(struct cam_path *path, u_int32_t relsim_flags,
1343 u_int32_t openings, u_int32_t arg,
1346 struct ccb_relsim crs;
1348 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("cam_release_devq(%u, %u, %u, %d)\n",
1349 relsim_flags, openings, arg, getcount_only));
1350 memset(&crs, 0, sizeof(crs));
1351 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
1352 crs.ccb_h.func_code = XPT_REL_SIMQ;
1353 crs.ccb_h.flags = getcount_only ? CAM_DEV_QFREEZE : 0;
1354 crs.release_flags = relsim_flags;
1355 crs.openings = openings;
1356 crs.release_timeout = arg;
1357 xpt_action((union ccb *)&crs);
1358 return (crs.qfrozen_cnt);
1361 #define saved_ccb_ptr ppriv_ptr0
1363 camperiphdone(struct cam_periph *periph, union ccb *done_ccb)
1365 union ccb *saved_ccb;
1367 struct scsi_start_stop_unit *scsi_cmd;
1368 int error = 0, error_code, sense_key, asc, ascq;
1369 u_int16_t done_flags;
1371 scsi_cmd = (struct scsi_start_stop_unit *)
1372 &done_ccb->csio.cdb_io.cdb_bytes;
1373 status = done_ccb->ccb_h.status;
1375 if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1376 if (scsi_extract_sense_ccb(done_ccb,
1377 &error_code, &sense_key, &asc, &ascq)) {
1379 * If the error is "invalid field in CDB",
1380 * and the load/eject flag is set, turn the
1381 * flag off and try again. This is just in
1382 * case the drive in question barfs on the
1383 * load eject flag. The CAM code should set
1384 * the load/eject flag by default for
1387 if ((scsi_cmd->opcode == START_STOP_UNIT) &&
1388 ((scsi_cmd->how & SSS_LOEJ) != 0) &&
1389 (asc == 0x24) && (ascq == 0x00)) {
1390 scsi_cmd->how &= ~SSS_LOEJ;
1391 if (status & CAM_DEV_QFRZN) {
1392 cam_release_devq(done_ccb->ccb_h.path,
1394 done_ccb->ccb_h.status &=
1397 xpt_action(done_ccb);
1401 error = cam_periph_error(done_ccb, 0,
1402 SF_RETRY_UA | SF_NO_PRINT);
1403 if (error == ERESTART)
1405 if (done_ccb->ccb_h.status & CAM_DEV_QFRZN) {
1406 cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0);
1407 done_ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1411 * If we have successfully taken a device from the not
1412 * ready to ready state, re-scan the device and re-get
1413 * the inquiry information. Many devices (mostly disks)
1414 * don't properly report their inquiry information unless
1417 if (scsi_cmd->opcode == START_STOP_UNIT)
1418 xpt_async(AC_INQ_CHANGED, done_ccb->ccb_h.path, NULL);
1421 /* If we tried long wait and still failed, remember that. */
1422 if ((periph->flags & CAM_PERIPH_RECOVERY_WAIT) &&
1423 (done_ccb->csio.cdb_io.cdb_bytes[0] == TEST_UNIT_READY)) {
1424 periph->flags &= ~CAM_PERIPH_RECOVERY_WAIT;
1425 if (error != 0 && done_ccb->ccb_h.retry_count == 0)
1426 periph->flags |= CAM_PERIPH_RECOVERY_WAIT_FAILED;
1430 * After recovery action(s) completed, return to the original CCB.
1431 * If the recovery CCB has failed, considering its own possible
1432 * retries and recovery, assume we are back in state where we have
1433 * been originally, but without recovery hopes left. In such case,
1434 * after the final attempt below, we cancel any further retries,
1435 * blocking by that also any new recovery attempts for this CCB,
1436 * and the result will be the final one returned to the CCB owher.
1438 saved_ccb = (union ccb *)done_ccb->ccb_h.saved_ccb_ptr;
1439 KASSERT(saved_ccb->ccb_h.func_code == XPT_SCSI_IO,
1440 ("%s: saved_ccb func_code %#x != XPT_SCSI_IO",
1441 __func__, saved_ccb->ccb_h.func_code));
1442 KASSERT(done_ccb->ccb_h.func_code == XPT_SCSI_IO,
1443 ("%s: done_ccb func_code %#x != XPT_SCSI_IO",
1444 __func__, done_ccb->ccb_h.func_code));
1445 saved_ccb->ccb_h.periph_links = done_ccb->ccb_h.periph_links;
1446 done_flags = done_ccb->ccb_h.alloc_flags;
1447 bcopy(saved_ccb, done_ccb, sizeof(struct ccb_scsiio));
1448 done_ccb->ccb_h.alloc_flags = done_flags;
1449 xpt_free_ccb(saved_ccb);
1450 if (done_ccb->ccb_h.cbfcnp != camperiphdone)
1451 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1453 done_ccb->ccb_h.retry_count = 0;
1454 xpt_action(done_ccb);
1457 /* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
1458 cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0);
1462 * Generic Async Event handler. Peripheral drivers usually
1463 * filter out the events that require personal attention,
1464 * and leave the rest to this function.
1467 cam_periph_async(struct cam_periph *periph, u_int32_t code,
1468 struct cam_path *path, void *arg)
1471 case AC_LOST_DEVICE:
1472 cam_periph_invalidate(periph);
1480 cam_periph_bus_settle(struct cam_periph *periph, u_int bus_settle)
1482 struct ccb_getdevstats cgds;
1484 memset(&cgds, 0, sizeof(cgds));
1485 xpt_setup_ccb(&cgds.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1486 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1487 xpt_action((union ccb *)&cgds);
1488 cam_periph_freeze_after_event(periph, &cgds.last_reset, bus_settle);
1492 cam_periph_freeze_after_event(struct cam_periph *periph,
1493 struct timeval* event_time, u_int duration_ms)
1495 struct timeval delta;
1496 struct timeval duration_tv;
1498 if (!timevalisset(event_time))
1502 timevalsub(&delta, event_time);
1503 duration_tv.tv_sec = duration_ms / 1000;
1504 duration_tv.tv_usec = (duration_ms % 1000) * 1000;
1505 if (timevalcmp(&delta, &duration_tv, <)) {
1506 timevalsub(&duration_tv, &delta);
1508 duration_ms = duration_tv.tv_sec * 1000;
1509 duration_ms += duration_tv.tv_usec / 1000;
1510 cam_freeze_devq(periph->path);
1511 cam_release_devq(periph->path,
1512 RELSIM_RELEASE_AFTER_TIMEOUT,
1514 /*timeout*/duration_ms,
1515 /*getcount_only*/0);
1521 camperiphscsistatuserror(union ccb *ccb, union ccb **orig_ccb,
1522 cam_flags camflags, u_int32_t sense_flags,
1523 int *openings, u_int32_t *relsim_flags,
1524 u_int32_t *timeout, u_int32_t *action, const char **action_string)
1526 struct cam_periph *periph;
1529 switch (ccb->csio.scsi_status) {
1530 case SCSI_STATUS_OK:
1531 case SCSI_STATUS_COND_MET:
1532 case SCSI_STATUS_INTERMED:
1533 case SCSI_STATUS_INTERMED_COND_MET:
1536 case SCSI_STATUS_CMD_TERMINATED:
1537 case SCSI_STATUS_CHECK_COND:
1538 error = camperiphscsisenseerror(ccb, orig_ccb,
1547 case SCSI_STATUS_QUEUE_FULL:
1550 struct ccb_getdevstats cgds;
1553 * First off, find out what the current
1554 * transaction counts are.
1556 memset(&cgds, 0, sizeof(cgds));
1557 xpt_setup_ccb(&cgds.ccb_h,
1559 CAM_PRIORITY_NORMAL);
1560 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1561 xpt_action((union ccb *)&cgds);
1564 * If we were the only transaction active, treat
1565 * the QUEUE FULL as if it were a BUSY condition.
1567 if (cgds.dev_active != 0) {
1571 * Reduce the number of openings to
1572 * be 1 less than the amount it took
1573 * to get a queue full bounded by the
1574 * minimum allowed tag count for this
1577 total_openings = cgds.dev_active + cgds.dev_openings;
1578 *openings = cgds.dev_active;
1579 if (*openings < cgds.mintags)
1580 *openings = cgds.mintags;
1581 if (*openings < total_openings)
1582 *relsim_flags = RELSIM_ADJUST_OPENINGS;
1585 * Some devices report queue full for
1586 * temporary resource shortages. For
1587 * this reason, we allow a minimum
1588 * tag count to be entered via a
1589 * quirk entry to prevent the queue
1590 * count on these devices from falling
1591 * to a pessimisticly low value. We
1592 * still wait for the next successful
1593 * completion, however, before queueing
1594 * more transactions to the device.
1596 *relsim_flags = RELSIM_RELEASE_AFTER_CMDCMPLT;
1600 *action &= ~SSQ_PRINT_SENSE;
1605 case SCSI_STATUS_BUSY:
1607 * Restart the queue after either another
1608 * command completes or a 1 second timeout.
1610 periph = xpt_path_periph(ccb->ccb_h.path);
1611 if (periph->flags & CAM_PERIPH_INVALID) {
1613 *action_string = "Periph was invalidated";
1614 } else if ((sense_flags & SF_RETRY_BUSY) != 0 ||
1615 ccb->ccb_h.retry_count > 0) {
1616 if ((sense_flags & SF_RETRY_BUSY) == 0)
1617 ccb->ccb_h.retry_count--;
1619 *relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT
1620 | RELSIM_RELEASE_AFTER_CMDCMPLT;
1624 *action_string = "Retries exhausted";
1627 case SCSI_STATUS_RESERV_CONFLICT:
1636 camperiphscsisenseerror(union ccb *ccb, union ccb **orig,
1637 cam_flags camflags, u_int32_t sense_flags,
1638 int *openings, u_int32_t *relsim_flags,
1639 u_int32_t *timeout, u_int32_t *action, const char **action_string)
1641 struct cam_periph *periph;
1642 union ccb *orig_ccb = ccb;
1643 int error, recoveryccb;
1646 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
1647 if (ccb->ccb_h.func_code == XPT_SCSI_IO && ccb->csio.bio != NULL)
1648 biotrack(ccb->csio.bio, __func__);
1651 periph = xpt_path_periph(ccb->ccb_h.path);
1652 recoveryccb = (ccb->ccb_h.cbfcnp == camperiphdone);
1653 if ((periph->flags & CAM_PERIPH_RECOVERY_INPROG) && !recoveryccb) {
1655 * If error recovery is already in progress, don't attempt
1656 * to process this error, but requeue it unconditionally
1657 * and attempt to process it once error recovery has
1658 * completed. This failed command is probably related to
1659 * the error that caused the currently active error recovery
1660 * action so our current recovery efforts should also
1661 * address this command. Be aware that the error recovery
1662 * code assumes that only one recovery action is in progress
1663 * on a particular peripheral instance at any given time
1664 * (e.g. only one saved CCB for error recovery) so it is
1665 * imperitive that we don't violate this assumption.
1668 *action &= ~SSQ_PRINT_SENSE;
1670 scsi_sense_action err_action;
1671 struct ccb_getdev cgd;
1674 * Grab the inquiry data for this device.
1676 memset(&cgd, 0, sizeof(cgd));
1677 xpt_setup_ccb(&cgd.ccb_h, ccb->ccb_h.path, CAM_PRIORITY_NORMAL);
1678 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1679 xpt_action((union ccb *)&cgd);
1681 err_action = scsi_error_action(&ccb->csio, &cgd.inq_data,
1683 error = err_action & SS_ERRMASK;
1686 * Do not autostart sequential access devices
1687 * to avoid unexpected tape loading.
1689 if ((err_action & SS_MASK) == SS_START &&
1690 SID_TYPE(&cgd.inq_data) == T_SEQUENTIAL) {
1691 *action_string = "Will not autostart a "
1692 "sequential access device";
1693 goto sense_error_done;
1697 * Avoid recovery recursion if recovery action is the same.
1699 if ((err_action & SS_MASK) >= SS_START && recoveryccb) {
1700 if (((err_action & SS_MASK) == SS_START &&
1701 ccb->csio.cdb_io.cdb_bytes[0] == START_STOP_UNIT) ||
1702 ((err_action & SS_MASK) == SS_TUR &&
1703 (ccb->csio.cdb_io.cdb_bytes[0] == TEST_UNIT_READY))) {
1704 err_action = SS_RETRY|SSQ_DECREMENT_COUNT|EIO;
1705 *relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1711 * If the recovery action will consume a retry,
1712 * make sure we actually have retries available.
1714 if ((err_action & SSQ_DECREMENT_COUNT) != 0) {
1715 if (ccb->ccb_h.retry_count > 0 &&
1716 (periph->flags & CAM_PERIPH_INVALID) == 0)
1717 ccb->ccb_h.retry_count--;
1719 *action_string = "Retries exhausted";
1720 goto sense_error_done;
1724 if ((err_action & SS_MASK) >= SS_START) {
1726 * Do common portions of commands that
1727 * use recovery CCBs.
1729 orig_ccb = xpt_alloc_ccb_nowait();
1730 if (orig_ccb == NULL) {
1731 *action_string = "Can't allocate recovery CCB";
1732 goto sense_error_done;
1735 * Clear freeze flag for original request here, as
1736 * this freeze will be dropped as part of ERESTART.
1738 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1740 KASSERT(ccb->ccb_h.func_code == XPT_SCSI_IO,
1741 ("%s: ccb func_code %#x != XPT_SCSI_IO",
1742 __func__, ccb->ccb_h.func_code));
1743 flags = orig_ccb->ccb_h.alloc_flags;
1744 bcopy(ccb, orig_ccb, sizeof(struct ccb_scsiio));
1745 orig_ccb->ccb_h.alloc_flags = flags;
1748 switch (err_action & SS_MASK) {
1750 *action_string = "No recovery action needed";
1754 *action_string = "Retrying command (per sense data)";
1758 *action_string = "Unretryable error";
1765 * Send a start unit command to the device, and
1766 * then retry the command.
1768 *action_string = "Attempting to start unit";
1769 periph->flags |= CAM_PERIPH_RECOVERY_INPROG;
1772 * Check for removable media and set
1773 * load/eject flag appropriately.
1775 if (SID_IS_REMOVABLE(&cgd.inq_data))
1780 scsi_start_stop(&ccb->csio,
1794 * Send a Test Unit Ready to the device.
1795 * If the 'many' flag is set, we send 120
1796 * test unit ready commands, one every half
1797 * second. Otherwise, we just send one TUR.
1798 * We only want to do this if the retry
1799 * count has not been exhausted.
1803 if ((err_action & SSQ_MANY) != 0 && (periph->flags &
1804 CAM_PERIPH_RECOVERY_WAIT_FAILED) == 0) {
1805 periph->flags |= CAM_PERIPH_RECOVERY_WAIT;
1806 *action_string = "Polling device for readiness";
1809 *action_string = "Testing device for readiness";
1812 periph->flags |= CAM_PERIPH_RECOVERY_INPROG;
1813 scsi_test_unit_ready(&ccb->csio,
1821 * Accomplish our 500ms delay by deferring
1822 * the release of our device queue appropriately.
1824 *relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1829 panic("Unhandled error action %x", err_action);
1832 if ((err_action & SS_MASK) >= SS_START) {
1834 * Drop the priority, so that the recovery
1835 * CCB is the first to execute. Freeze the queue
1836 * after this command is sent so that we can
1837 * restore the old csio and have it queued in
1838 * the proper order before we release normal
1839 * transactions to the device.
1841 ccb->ccb_h.pinfo.priority--;
1842 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1843 ccb->ccb_h.saved_ccb_ptr = orig_ccb;
1849 *action = err_action;
1855 * Generic error handler. Peripheral drivers usually filter
1856 * out the errors that they handle in a unique manner, then
1857 * call this function.
1860 cam_periph_error(union ccb *ccb, cam_flags camflags,
1861 u_int32_t sense_flags)
1863 struct cam_path *newpath;
1864 union ccb *orig_ccb, *scan_ccb;
1865 struct cam_periph *periph;
1866 const char *action_string;
1868 int frozen, error, openings, devctl_err;
1869 u_int32_t action, relsim_flags, timeout;
1871 action = SSQ_PRINT_SENSE;
1872 periph = xpt_path_periph(ccb->ccb_h.path);
1873 action_string = NULL;
1874 status = ccb->ccb_h.status;
1875 frozen = (status & CAM_DEV_QFRZN) != 0;
1876 status &= CAM_STATUS_MASK;
1877 devctl_err = openings = relsim_flags = timeout = 0;
1880 /* Filter the errors that should be reported via devctl */
1881 switch (ccb->ccb_h.status & CAM_STATUS_MASK) {
1882 case CAM_CMD_TIMEOUT:
1883 case CAM_REQ_ABORTED:
1884 case CAM_REQ_CMP_ERR:
1885 case CAM_REQ_TERMIO:
1886 case CAM_UNREC_HBA_ERROR:
1887 case CAM_DATA_RUN_ERR:
1888 case CAM_SCSI_STATUS_ERROR:
1889 case CAM_ATA_STATUS_ERROR:
1890 case CAM_SMP_STATUS_ERROR:
1900 action &= ~SSQ_PRINT_SENSE;
1902 case CAM_SCSI_STATUS_ERROR:
1903 error = camperiphscsistatuserror(ccb, &orig_ccb,
1904 camflags, sense_flags, &openings, &relsim_flags,
1905 &timeout, &action, &action_string);
1907 case CAM_AUTOSENSE_FAIL:
1908 error = EIO; /* we have to kill the command */
1912 case CAM_MSG_REJECT_REC:
1913 /* XXX Don't know that these are correct */
1916 case CAM_SEL_TIMEOUT:
1917 if ((camflags & CAM_RETRY_SELTO) != 0) {
1918 if (ccb->ccb_h.retry_count > 0 &&
1919 (periph->flags & CAM_PERIPH_INVALID) == 0) {
1920 ccb->ccb_h.retry_count--;
1924 * Wait a bit to give the device
1925 * time to recover before we try again.
1927 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1928 timeout = periph_selto_delay;
1931 action_string = "Retries exhausted";
1934 case CAM_DEV_NOT_THERE:
1938 case CAM_REQ_INVALID:
1939 case CAM_PATH_INVALID:
1941 case CAM_PROVIDE_FAIL:
1942 case CAM_REQ_TOO_BIG:
1943 case CAM_LUN_INVALID:
1944 case CAM_TID_INVALID:
1945 case CAM_FUNC_NOTAVAIL:
1948 case CAM_SCSI_BUS_RESET:
1951 * Commands that repeatedly timeout and cause these
1952 * kinds of error recovery actions, should return
1953 * CAM_CMD_TIMEOUT, which allows us to safely assume
1954 * that this command was an innocent bystander to
1955 * these events and should be unconditionally
1958 case CAM_REQUEUE_REQ:
1959 /* Unconditional requeue if device is still there */
1960 if (periph->flags & CAM_PERIPH_INVALID) {
1961 action_string = "Periph was invalidated";
1963 } else if (sense_flags & SF_NO_RETRY) {
1965 action_string = "Retry was blocked";
1968 action &= ~SSQ_PRINT_SENSE;
1971 case CAM_RESRC_UNAVAIL:
1972 /* Wait a bit for the resource shortage to abate. */
1973 timeout = periph_noresrc_delay;
1977 /* Wait a bit for the busy condition to abate. */
1978 timeout = periph_busy_delay;
1980 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1982 case CAM_ATA_STATUS_ERROR:
1983 case CAM_REQ_CMP_ERR:
1984 case CAM_CMD_TIMEOUT:
1985 case CAM_UNEXP_BUSFREE:
1986 case CAM_UNCOR_PARITY:
1987 case CAM_DATA_RUN_ERR:
1989 if (periph->flags & CAM_PERIPH_INVALID) {
1991 action_string = "Periph was invalidated";
1992 } else if (ccb->ccb_h.retry_count == 0) {
1994 action_string = "Retries exhausted";
1995 } else if (sense_flags & SF_NO_RETRY) {
1997 action_string = "Retry was blocked";
1999 ccb->ccb_h.retry_count--;
2005 if ((sense_flags & SF_PRINT_ALWAYS) ||
2006 CAM_DEBUGGED(ccb->ccb_h.path, CAM_DEBUG_INFO))
2007 action |= SSQ_PRINT_SENSE;
2008 else if (sense_flags & SF_NO_PRINT)
2009 action &= ~SSQ_PRINT_SENSE;
2010 if ((action & SSQ_PRINT_SENSE) != 0)
2011 cam_error_print(orig_ccb, CAM_ESF_ALL, CAM_EPF_ALL);
2012 if (error != 0 && (action & SSQ_PRINT_SENSE) != 0) {
2013 if (error != ERESTART) {
2014 if (action_string == NULL)
2015 action_string = "Unretryable error";
2016 xpt_print(ccb->ccb_h.path, "Error %d, %s\n",
2017 error, action_string);
2018 } else if (action_string != NULL)
2019 xpt_print(ccb->ccb_h.path, "%s\n", action_string);
2021 xpt_print(ccb->ccb_h.path,
2022 "Retrying command, %d more tries remain\n",
2023 ccb->ccb_h.retry_count);
2027 if (devctl_err && (error != 0 || (action & SSQ_PRINT_SENSE) != 0))
2028 cam_periph_devctl_notify(orig_ccb);
2030 if ((action & SSQ_LOST) != 0) {
2034 * For a selection timeout, we consider all of the LUNs on
2035 * the target to be gone. If the status is CAM_DEV_NOT_THERE,
2036 * then we only get rid of the device(s) specified by the
2037 * path in the original CCB.
2039 if (status == CAM_SEL_TIMEOUT)
2040 lun_id = CAM_LUN_WILDCARD;
2042 lun_id = xpt_path_lun_id(ccb->ccb_h.path);
2044 /* Should we do more if we can't create the path?? */
2045 if (xpt_create_path(&newpath, periph,
2046 xpt_path_path_id(ccb->ccb_h.path),
2047 xpt_path_target_id(ccb->ccb_h.path),
2048 lun_id) == CAM_REQ_CMP) {
2050 * Let peripheral drivers know that this
2051 * device has gone away.
2053 xpt_async(AC_LOST_DEVICE, newpath, NULL);
2054 xpt_free_path(newpath);
2058 /* Broadcast UNIT ATTENTIONs to all periphs. */
2059 if ((action & SSQ_UA) != 0)
2060 xpt_async(AC_UNIT_ATTENTION, orig_ccb->ccb_h.path, orig_ccb);
2062 /* Rescan target on "Reported LUNs data has changed" */
2063 if ((action & SSQ_RESCAN) != 0) {
2064 if (xpt_create_path(&newpath, NULL,
2065 xpt_path_path_id(ccb->ccb_h.path),
2066 xpt_path_target_id(ccb->ccb_h.path),
2067 CAM_LUN_WILDCARD) == CAM_REQ_CMP) {
2068 scan_ccb = xpt_alloc_ccb_nowait();
2069 if (scan_ccb != NULL) {
2070 scan_ccb->ccb_h.path = newpath;
2071 scan_ccb->ccb_h.func_code = XPT_SCAN_TGT;
2072 scan_ccb->crcn.flags = 0;
2073 xpt_rescan(scan_ccb);
2076 "Can't allocate CCB to rescan target\n");
2077 xpt_free_path(newpath);
2082 /* Attempt a retry */
2083 if (error == ERESTART || error == 0) {
2085 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
2086 if (error == ERESTART)
2089 cam_release_devq(ccb->ccb_h.path,
2093 /*getcount_only*/0);
2099 #define CAM_PERIPH_DEVD_MSG_SIZE 256
2102 cam_periph_devctl_notify(union ccb *ccb)
2104 struct cam_periph *periph;
2105 struct ccb_getdev *cgd;
2107 int serr, sk, asc, ascq;
2110 sbmsg = malloc(CAM_PERIPH_DEVD_MSG_SIZE, M_CAMPERIPH, M_NOWAIT);
2114 sbuf_new(&sb, sbmsg, CAM_PERIPH_DEVD_MSG_SIZE, SBUF_FIXEDLEN);
2116 periph = xpt_path_periph(ccb->ccb_h.path);
2117 sbuf_printf(&sb, "device=%s%d ", periph->periph_name,
2118 periph->unit_number);
2120 sbuf_printf(&sb, "serial=\"");
2121 if ((cgd = (struct ccb_getdev *)xpt_alloc_ccb_nowait()) != NULL) {
2122 xpt_setup_ccb(&cgd->ccb_h, ccb->ccb_h.path,
2123 CAM_PRIORITY_NORMAL);
2124 cgd->ccb_h.func_code = XPT_GDEV_TYPE;
2125 xpt_action((union ccb *)cgd);
2127 if (cgd->ccb_h.status == CAM_REQ_CMP)
2128 sbuf_bcat(&sb, cgd->serial_num, cgd->serial_num_len);
2129 xpt_free_ccb((union ccb *)cgd);
2131 sbuf_printf(&sb, "\" ");
2132 sbuf_printf(&sb, "cam_status=\"0x%x\" ", ccb->ccb_h.status);
2134 switch (ccb->ccb_h.status & CAM_STATUS_MASK) {
2135 case CAM_CMD_TIMEOUT:
2136 sbuf_printf(&sb, "timeout=%d ", ccb->ccb_h.timeout);
2139 case CAM_SCSI_STATUS_ERROR:
2140 sbuf_printf(&sb, "scsi_status=%d ", ccb->csio.scsi_status);
2141 if (scsi_extract_sense_ccb(ccb, &serr, &sk, &asc, &ascq))
2142 sbuf_printf(&sb, "scsi_sense=\"%02x %02x %02x %02x\" ",
2143 serr, sk, asc, ascq);
2146 case CAM_ATA_STATUS_ERROR:
2147 sbuf_printf(&sb, "RES=\"");
2148 ata_res_sbuf(&ccb->ataio.res, &sb);
2149 sbuf_printf(&sb, "\" ");
2157 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
2158 sbuf_printf(&sb, "CDB=\"");
2159 scsi_cdb_sbuf(scsiio_cdb_ptr(&ccb->csio), &sb);
2160 sbuf_printf(&sb, "\" ");
2161 } else if (ccb->ccb_h.func_code == XPT_ATA_IO) {
2162 sbuf_printf(&sb, "ACB=\"");
2163 ata_cmd_sbuf(&ccb->ataio.cmd, &sb);
2164 sbuf_printf(&sb, "\" ");
2167 if (sbuf_finish(&sb) == 0)
2168 devctl_notify("CAM", "periph", type, sbuf_data(&sb));
2170 free(sbmsg, M_CAMPERIPH);
2174 * Sysctl to force an invalidation of the drive right now. Can be
2175 * called with CTLFLAG_MPSAFE since we take periph lock.
2178 cam_periph_invalidate_sysctl(SYSCTL_HANDLER_ARGS)
2180 struct cam_periph *periph;
2185 error = sysctl_handle_int(oidp, &value, 0, req);
2186 if (error != 0 || req->newptr == NULL || value != 1)
2189 cam_periph_lock(periph);
2190 cam_periph_invalidate(periph);
2191 cam_periph_unlock(periph);