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/mutex.h>
45 #include <sys/devicestat.h>
49 #include <vm/vm_extern.h>
52 #include <cam/cam_ccb.h>
53 #include <cam/cam_queue.h>
54 #include <cam/cam_xpt_periph.h>
55 #include <cam/cam_periph.h>
56 #include <cam/cam_debug.h>
57 #include <cam/cam_sim.h>
59 #include <cam/scsi/scsi_all.h>
60 #include <cam/scsi/scsi_message.h>
61 #include <cam/scsi/scsi_pass.h>
63 static u_int camperiphnextunit(struct periph_driver *p_drv,
64 u_int newunit, int wired,
65 path_id_t pathid, target_id_t target,
67 static u_int camperiphunit(struct periph_driver *p_drv,
68 path_id_t pathid, target_id_t target,
70 static void camperiphdone(struct cam_periph *periph,
72 static void camperiphfree(struct cam_periph *periph);
73 static int camperiphscsistatuserror(union ccb *ccb,
76 u_int32_t sense_flags,
78 u_int32_t *relsim_flags,
81 const char **action_string);
82 static int camperiphscsisenseerror(union ccb *ccb,
85 u_int32_t sense_flags,
87 u_int32_t *relsim_flags,
90 const char **action_string);
91 static void cam_periph_devctl_notify(union ccb *ccb);
93 static int nperiph_drivers;
94 static int initialized = 0;
95 struct periph_driver **periph_drivers;
97 static MALLOC_DEFINE(M_CAMPERIPH, "CAM periph", "CAM peripheral buffers");
99 static int periph_selto_delay = 1000;
100 TUNABLE_INT("kern.cam.periph_selto_delay", &periph_selto_delay);
101 static int periph_noresrc_delay = 500;
102 TUNABLE_INT("kern.cam.periph_noresrc_delay", &periph_noresrc_delay);
103 static int periph_busy_delay = 500;
104 TUNABLE_INT("kern.cam.periph_busy_delay", &periph_busy_delay);
108 periphdriver_register(void *data)
110 struct periph_driver *drv = (struct periph_driver *)data;
111 struct periph_driver **newdrivers, **old;
115 ndrivers = nperiph_drivers + 2;
116 newdrivers = malloc(sizeof(*newdrivers) * ndrivers, M_CAMPERIPH,
119 if (ndrivers != nperiph_drivers + 2) {
121 * Lost race against itself; go around.
124 free(newdrivers, M_CAMPERIPH);
128 bcopy(periph_drivers, newdrivers,
129 sizeof(*newdrivers) * nperiph_drivers);
130 newdrivers[nperiph_drivers] = drv;
131 newdrivers[nperiph_drivers + 1] = NULL;
132 old = periph_drivers;
133 periph_drivers = newdrivers;
137 free(old, M_CAMPERIPH);
138 /* If driver marked as early or it is late now, initialize it. */
139 if (((drv->flags & CAM_PERIPH_DRV_EARLY) != 0 && initialized > 0) ||
145 periphdriver_unregister(void *data)
147 struct periph_driver *drv = (struct periph_driver *)data;
150 /* If driver marked as early or it is late now, deinitialize it. */
151 if (((drv->flags & CAM_PERIPH_DRV_EARLY) != 0 && initialized > 0) ||
153 if (drv->deinit == NULL) {
154 printf("CAM periph driver '%s' doesn't have deinit.\n",
158 error = drv->deinit();
164 for (n = 0; n < nperiph_drivers && periph_drivers[n] != drv; n++)
166 KASSERT(n < nperiph_drivers,
167 ("Periph driver '%s' was not registered", drv->driver_name));
168 for (; n + 1 < nperiph_drivers; n++)
169 periph_drivers[n] = periph_drivers[n + 1];
170 periph_drivers[n + 1] = NULL;
177 periphdriver_init(int level)
181 initialized = max(initialized, level);
182 for (i = 0; periph_drivers[i] != NULL; i++) {
183 early = (periph_drivers[i]->flags & CAM_PERIPH_DRV_EARLY) ? 1 : 2;
184 if (early == initialized)
185 (*periph_drivers[i]->init)();
190 cam_periph_alloc(periph_ctor_t *periph_ctor,
191 periph_oninv_t *periph_oninvalidate,
192 periph_dtor_t *periph_dtor, periph_start_t *periph_start,
193 char *name, cam_periph_type type, struct cam_path *path,
194 ac_callback_t *ac_callback, ac_code code, void *arg)
196 struct periph_driver **p_drv;
198 struct cam_periph *periph;
199 struct cam_periph *cur_periph;
201 target_id_t target_id;
208 * Handle Hot-Plug scenarios. If there is already a peripheral
209 * of our type assigned to this path, we are likely waiting for
210 * final close on an old, invalidated, peripheral. If this is
211 * the case, queue up a deferred call to the peripheral's async
212 * handler. If it looks like a mistaken re-allocation, complain.
214 if ((periph = cam_periph_find(path, name)) != NULL) {
216 if ((periph->flags & CAM_PERIPH_INVALID) != 0
217 && (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) == 0) {
218 periph->flags |= CAM_PERIPH_NEW_DEV_FOUND;
219 periph->deferred_callback = ac_callback;
220 periph->deferred_ac = code;
221 return (CAM_REQ_INPROG);
223 printf("cam_periph_alloc: attempt to re-allocate "
224 "valid device %s%d rejected flags %#x "
225 "refcount %d\n", periph->periph_name,
226 periph->unit_number, periph->flags,
229 return (CAM_REQ_INVALID);
232 periph = (struct cam_periph *)malloc(sizeof(*periph), M_CAMPERIPH,
236 return (CAM_RESRC_UNAVAIL);
241 sim = xpt_path_sim(path);
242 path_id = xpt_path_path_id(path);
243 target_id = xpt_path_target_id(path);
244 lun_id = xpt_path_lun_id(path);
245 periph->periph_start = periph_start;
246 periph->periph_dtor = periph_dtor;
247 periph->periph_oninval = periph_oninvalidate;
249 periph->periph_name = name;
250 periph->scheduled_priority = CAM_PRIORITY_NONE;
251 periph->immediate_priority = CAM_PRIORITY_NONE;
252 periph->refcount = 1; /* Dropped by invalidation. */
254 SLIST_INIT(&periph->ccb_list);
255 status = xpt_create_path(&path, periph, path_id, target_id, lun_id);
256 if (status != CAM_REQ_CMP)
261 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
262 if (strcmp((*p_drv)->driver_name, name) == 0)
265 if (*p_drv == NULL) {
266 printf("cam_periph_alloc: invalid periph name '%s'\n", name);
268 xpt_free_path(periph->path);
269 free(periph, M_CAMPERIPH);
270 return (CAM_REQ_INVALID);
272 periph->unit_number = camperiphunit(*p_drv, path_id, target_id, lun_id);
273 cur_periph = TAILQ_FIRST(&(*p_drv)->units);
274 while (cur_periph != NULL
275 && cur_periph->unit_number < periph->unit_number)
276 cur_periph = TAILQ_NEXT(cur_periph, unit_links);
277 if (cur_periph != NULL) {
278 KASSERT(cur_periph->unit_number != periph->unit_number, ("duplicate units on periph list"));
279 TAILQ_INSERT_BEFORE(cur_periph, periph, unit_links);
281 TAILQ_INSERT_TAIL(&(*p_drv)->units, periph, unit_links);
282 (*p_drv)->generation++;
288 status = xpt_add_periph(periph);
289 if (status != CAM_REQ_CMP)
293 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph created\n"));
295 status = periph_ctor(periph, arg);
297 if (status == CAM_REQ_CMP)
301 switch (init_level) {
303 /* Initialized successfully */
306 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph destroyed\n"));
307 xpt_remove_periph(periph);
311 TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
313 xpt_free_path(periph->path);
316 free(periph, M_CAMPERIPH);
319 /* No cleanup to perform. */
322 panic("%s: Unknown init level", __func__);
328 * Find a peripheral structure with the specified path, target, lun,
329 * and (optionally) type. If the name is NULL, this function will return
330 * the first peripheral driver that matches the specified path.
333 cam_periph_find(struct cam_path *path, char *name)
335 struct periph_driver **p_drv;
336 struct cam_periph *periph;
339 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
341 if (name != NULL && (strcmp((*p_drv)->driver_name, name) != 0))
344 TAILQ_FOREACH(periph, &(*p_drv)->units, unit_links) {
345 if (xpt_path_comp(periph->path, path) == 0) {
347 cam_periph_assert(periph, MA_OWNED);
361 * Find peripheral driver instances attached to the specified path.
364 cam_periph_list(struct cam_path *path, struct sbuf *sb)
366 struct sbuf local_sb;
367 struct periph_driver **p_drv;
368 struct cam_periph *periph;
374 sbuf_new(&local_sb, NULL, sbuf_alloc_len, SBUF_FIXEDLEN);
377 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
379 TAILQ_FOREACH(periph, &(*p_drv)->units, unit_links) {
380 if (xpt_path_comp(periph->path, path) != 0)
383 if (sbuf_len(&local_sb) != 0)
384 sbuf_cat(&local_sb, ",");
386 sbuf_printf(&local_sb, "%s%d", periph->periph_name,
387 periph->unit_number);
389 if (sbuf_error(&local_sb) == ENOMEM) {
392 sbuf_delete(&local_sb);
399 sbuf_finish(&local_sb);
400 sbuf_cpy(sb, sbuf_data(&local_sb));
401 sbuf_delete(&local_sb);
406 cam_periph_acquire(struct cam_periph *periph)
410 status = CAM_REQ_CMP_ERR;
415 if ((periph->flags & CAM_PERIPH_INVALID) == 0) {
417 status = CAM_REQ_CMP;
425 cam_periph_doacquire(struct cam_periph *periph)
429 KASSERT(periph->refcount >= 1,
430 ("cam_periph_doacquire() with refcount == %d", periph->refcount));
436 cam_periph_release_locked_buses(struct cam_periph *periph)
439 cam_periph_assert(periph, MA_OWNED);
440 KASSERT(periph->refcount >= 1, ("periph->refcount >= 1"));
441 if (--periph->refcount == 0)
442 camperiphfree(periph);
446 cam_periph_release_locked(struct cam_periph *periph)
453 cam_periph_release_locked_buses(periph);
458 cam_periph_release(struct cam_periph *periph)
465 cam_periph_assert(periph, MA_NOTOWNED);
466 mtx = cam_periph_mtx(periph);
468 cam_periph_release_locked(periph);
473 cam_periph_hold(struct cam_periph *periph, int priority)
478 * Increment the reference count on the peripheral
479 * while we wait for our lock attempt to succeed
480 * to ensure the peripheral doesn't disappear out
481 * from user us while we sleep.
484 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
487 cam_periph_assert(periph, MA_OWNED);
488 while ((periph->flags & CAM_PERIPH_LOCKED) != 0) {
489 periph->flags |= CAM_PERIPH_LOCK_WANTED;
490 if ((error = cam_periph_sleep(periph, periph, priority,
491 "caplck", 0)) != 0) {
492 cam_periph_release_locked(periph);
495 if (periph->flags & CAM_PERIPH_INVALID) {
496 cam_periph_release_locked(periph);
501 periph->flags |= CAM_PERIPH_LOCKED;
506 cam_periph_unhold(struct cam_periph *periph)
509 cam_periph_assert(periph, MA_OWNED);
511 periph->flags &= ~CAM_PERIPH_LOCKED;
512 if ((periph->flags & CAM_PERIPH_LOCK_WANTED) != 0) {
513 periph->flags &= ~CAM_PERIPH_LOCK_WANTED;
517 cam_periph_release_locked(periph);
521 * Look for the next unit number that is not currently in use for this
522 * peripheral type starting at "newunit". Also exclude unit numbers that
523 * are reserved by for future "hardwiring" unless we already know that this
524 * is a potential wired device. Only assume that the device is "wired" the
525 * first time through the loop since after that we'll be looking at unit
526 * numbers that did not match a wiring entry.
529 camperiphnextunit(struct periph_driver *p_drv, u_int newunit, int wired,
530 path_id_t pathid, target_id_t target, lun_id_t lun)
532 struct cam_periph *periph;
534 int i, val, dunit, r;
535 const char *dname, *strval;
537 periph_name = p_drv->driver_name;
540 for (periph = TAILQ_FIRST(&p_drv->units);
541 periph != NULL && periph->unit_number != newunit;
542 periph = TAILQ_NEXT(periph, unit_links))
545 if (periph != NULL && periph->unit_number == newunit) {
547 xpt_print(periph->path, "Duplicate Wired "
549 xpt_print(periph->path, "Second device (%s "
550 "device at scbus%d target %d lun %d) will "
551 "not be wired\n", periph_name, pathid,
561 * Don't match entries like "da 4" as a wired down
562 * device, but do match entries like "da 4 target 5"
563 * or even "da 4 scbus 1".
568 r = resource_find_dev(&i, dname, &dunit, NULL, NULL);
571 /* if no "target" and no specific scbus, skip */
572 if (resource_int_value(dname, dunit, "target", &val) &&
573 (resource_string_value(dname, dunit, "at",&strval)||
574 strcmp(strval, "scbus") == 0))
576 if (newunit == dunit)
586 camperiphunit(struct periph_driver *p_drv, path_id_t pathid,
587 target_id_t target, lun_id_t lun)
590 int wired, i, val, dunit;
591 const char *dname, *strval;
592 char pathbuf[32], *periph_name;
594 periph_name = p_drv->driver_name;
595 snprintf(pathbuf, sizeof(pathbuf), "scbus%d", pathid);
599 for (wired = 0; resource_find_dev(&i, dname, &dunit, NULL, NULL) == 0;
601 if (resource_string_value(dname, dunit, "at", &strval) == 0) {
602 if (strcmp(strval, pathbuf) != 0)
606 if (resource_int_value(dname, dunit, "target", &val) == 0) {
611 if (resource_int_value(dname, dunit, "lun", &val) == 0) {
623 * Either start from 0 looking for the next unit or from
624 * the unit number given in the resource config. This way,
625 * if we have wildcard matches, we don't return the same
628 unit = camperiphnextunit(p_drv, unit, wired, pathid, target, lun);
634 cam_periph_invalidate(struct cam_periph *periph)
637 cam_periph_assert(periph, MA_OWNED);
639 * We only call this routine the first time a peripheral is
642 if ((periph->flags & CAM_PERIPH_INVALID) != 0)
645 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph invalidated\n"));
646 if ((periph->flags & CAM_PERIPH_ANNOUNCED) && !rebooting) {
650 sbuf_new(&sb, buffer, 160, SBUF_FIXEDLEN);
651 xpt_denounce_periph_sbuf(periph, &sb);
655 periph->flags |= CAM_PERIPH_INVALID;
656 periph->flags &= ~CAM_PERIPH_NEW_DEV_FOUND;
657 if (periph->periph_oninval != NULL)
658 periph->periph_oninval(periph);
659 cam_periph_release_locked(periph);
663 camperiphfree(struct cam_periph *periph)
665 struct periph_driver **p_drv;
666 struct periph_driver *drv;
668 cam_periph_assert(periph, MA_OWNED);
669 KASSERT(periph->periph_allocating == 0, ("%s%d: freed while allocating",
670 periph->periph_name, periph->unit_number));
671 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
672 if (strcmp((*p_drv)->driver_name, periph->periph_name) == 0)
675 if (*p_drv == NULL) {
676 printf("camperiphfree: attempt to free non-existant periph\n");
680 * Cache a pointer to the periph_driver structure. If a
681 * periph_driver is added or removed from the array (see
682 * periphdriver_register()) while we drop the toplogy lock
683 * below, p_drv may change. This doesn't protect against this
684 * particular periph_driver going away. That will require full
685 * reference counting in the periph_driver infrastructure.
690 * We need to set this flag before dropping the topology lock, to
691 * let anyone who is traversing the list that this peripheral is
692 * about to be freed, and there will be no more reference count
695 periph->flags |= CAM_PERIPH_FREE;
698 * The peripheral destructor semantics dictate calling with only the
699 * SIM mutex held. Since it might sleep, it should not be called
700 * with the topology lock held.
705 * We need to call the peripheral destructor prior to removing the
706 * peripheral from the list. Otherwise, we risk running into a
707 * scenario where the peripheral unit number may get reused
708 * (because it has been removed from the list), but some resources
709 * used by the peripheral are still hanging around. In particular,
710 * the devfs nodes used by some peripherals like the pass(4) driver
711 * aren't fully cleaned up until the destructor is run. If the
712 * unit number is reused before the devfs instance is fully gone,
715 if (periph->periph_dtor != NULL)
716 periph->periph_dtor(periph);
719 * The peripheral list is protected by the topology lock.
723 TAILQ_REMOVE(&drv->units, periph, unit_links);
726 xpt_remove_periph(periph);
729 if ((periph->flags & CAM_PERIPH_ANNOUNCED) && !rebooting)
730 xpt_print(periph->path, "Periph destroyed\n");
732 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph destroyed\n"));
734 if (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) {
738 switch (periph->deferred_ac) {
739 case AC_FOUND_DEVICE:
740 ccb.ccb_h.func_code = XPT_GDEV_TYPE;
741 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
745 case AC_PATH_REGISTERED:
746 xpt_path_inq(&ccb.cpi, periph->path);
753 periph->deferred_callback(NULL, periph->deferred_ac,
756 xpt_free_path(periph->path);
757 free(periph, M_CAMPERIPH);
762 * Map user virtual pointers into kernel virtual address space, so we can
763 * access the memory. This is now a generic function that centralizes most
764 * of the sanity checks on the data flags, if any.
765 * This also only works for up to MAXPHYS memory. Since we use
766 * buffers to map stuff in and out, we're limited to the buffer size.
769 cam_periph_mapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo,
773 int flags[CAM_PERIPH_MAXMAPS];
774 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
775 u_int32_t lengths[CAM_PERIPH_MAXMAPS];
776 u_int32_t dirs[CAM_PERIPH_MAXMAPS];
779 maxmap = DFLTPHYS; /* traditional default */
780 else if (maxmap > MAXPHYS)
781 maxmap = MAXPHYS; /* for safety */
782 switch(ccb->ccb_h.func_code) {
784 if (ccb->cdm.match_buf_len == 0) {
785 printf("cam_periph_mapmem: invalid match buffer "
789 if (ccb->cdm.pattern_buf_len > 0) {
790 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
791 lengths[0] = ccb->cdm.pattern_buf_len;
792 dirs[0] = CAM_DIR_OUT;
793 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
794 lengths[1] = ccb->cdm.match_buf_len;
795 dirs[1] = CAM_DIR_IN;
798 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
799 lengths[0] = ccb->cdm.match_buf_len;
800 dirs[0] = CAM_DIR_IN;
804 * This request will not go to the hardware, no reason
805 * to be so strict. vmapbuf() is able to map up to MAXPHYS.
810 case XPT_CONT_TARGET_IO:
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->csio.data_ptr;
816 lengths[0] = ccb->csio.dxfer_len;
817 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
821 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
823 if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR)
825 data_ptrs[0] = &ccb->ataio.data_ptr;
826 lengths[0] = ccb->ataio.dxfer_len;
827 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
831 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
833 /* Two mappings: one for cmd->data and one for cmd->data->data */
834 data_ptrs[0] = (unsigned char **)&ccb->mmcio.cmd.data;
835 lengths[0] = sizeof(struct mmc_data *);
836 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
837 data_ptrs[1] = (unsigned char **)&ccb->mmcio.cmd.data->data;
838 lengths[1] = ccb->mmcio.cmd.data->len;
839 dirs[1] = ccb->ccb_h.flags & CAM_DIR_MASK;
843 data_ptrs[0] = &ccb->smpio.smp_request;
844 lengths[0] = ccb->smpio.smp_request_len;
845 dirs[0] = CAM_DIR_OUT;
846 data_ptrs[1] = &ccb->smpio.smp_response;
847 lengths[1] = ccb->smpio.smp_response_len;
848 dirs[1] = CAM_DIR_IN;
853 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
855 if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR)
857 data_ptrs[0] = &ccb->nvmeio.data_ptr;
858 lengths[0] = ccb->nvmeio.dxfer_len;
859 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
862 case XPT_DEV_ADVINFO:
863 if (ccb->cdai.bufsiz == 0)
866 data_ptrs[0] = (uint8_t **)&ccb->cdai.buf;
867 lengths[0] = ccb->cdai.bufsiz;
868 dirs[0] = CAM_DIR_IN;
872 * This request will not go to the hardware, no reason
873 * to be so strict. vmapbuf() is able to map up to MAXPHYS.
879 break; /* NOTREACHED */
883 * Check the transfer length and permissions first, so we don't
884 * have to unmap any previously mapped buffers.
886 for (i = 0; i < numbufs; i++) {
891 * The userland data pointer passed in may not be page
892 * aligned. vmapbuf() truncates the address to a page
893 * boundary, so if the address isn't page aligned, we'll
894 * need enough space for the given transfer length, plus
895 * whatever extra space is necessary to make it to the page
899 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)) > maxmap){
900 printf("cam_periph_mapmem: attempt to map %lu bytes, "
901 "which is greater than %lu\n",
903 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)),
908 if (dirs[i] & CAM_DIR_OUT) {
909 flags[i] = BIO_WRITE;
912 if (dirs[i] & CAM_DIR_IN) {
919 * This keeps the kernel stack of current thread from getting
920 * swapped. In low-memory situations where the kernel stack might
921 * otherwise get swapped out, this holds it and allows the thread
922 * to make progress and release the kernel mapped pages sooner.
924 * XXX KDM should I use P_NOSWAP instead?
928 for (i = 0; i < numbufs; i++) {
932 mapinfo->bp[i] = getpbuf(NULL);
934 /* put our pointer in the data slot */
935 mapinfo->bp[i]->b_data = *data_ptrs[i];
937 /* save the user's data address */
938 mapinfo->bp[i]->b_caller1 = *data_ptrs[i];
940 /* set the transfer length, we know it's < MAXPHYS */
941 mapinfo->bp[i]->b_bufsize = lengths[i];
943 /* set the direction */
944 mapinfo->bp[i]->b_iocmd = flags[i];
947 * Map the buffer into kernel memory.
949 * Note that useracc() alone is not a sufficient test.
950 * vmapbuf() can still fail due to a smaller file mapped
951 * into a larger area of VM, or if userland races against
952 * vmapbuf() after the useracc() check.
954 if (vmapbuf(mapinfo->bp[i], 1) < 0) {
955 for (j = 0; j < i; ++j) {
956 *data_ptrs[j] = mapinfo->bp[j]->b_caller1;
957 vunmapbuf(mapinfo->bp[j]);
958 relpbuf(mapinfo->bp[j], NULL);
960 relpbuf(mapinfo->bp[i], NULL);
965 /* set our pointer to the new mapped area */
966 *data_ptrs[i] = mapinfo->bp[i]->b_data;
968 mapinfo->num_bufs_used++;
972 * Now that we've gotten this far, change ownership to the kernel
973 * of the buffers so that we don't run afoul of returning to user
974 * space with locks (on the buffer) held.
976 for (i = 0; i < numbufs; i++) {
977 BUF_KERNPROC(mapinfo->bp[i]);
985 * Unmap memory segments mapped into kernel virtual address space by
986 * cam_periph_mapmem().
989 cam_periph_unmapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
992 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
994 if (mapinfo->num_bufs_used <= 0) {
995 /* nothing to free and the process wasn't held. */
999 switch (ccb->ccb_h.func_code) {
1001 numbufs = min(mapinfo->num_bufs_used, 2);
1004 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
1006 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
1007 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
1011 case XPT_CONT_TARGET_IO:
1012 data_ptrs[0] = &ccb->csio.data_ptr;
1013 numbufs = min(mapinfo->num_bufs_used, 1);
1016 data_ptrs[0] = &ccb->ataio.data_ptr;
1017 numbufs = min(mapinfo->num_bufs_used, 1);
1020 numbufs = min(mapinfo->num_bufs_used, 2);
1021 data_ptrs[0] = &ccb->smpio.smp_request;
1022 data_ptrs[1] = &ccb->smpio.smp_response;
1024 case XPT_DEV_ADVINFO:
1025 numbufs = min(mapinfo->num_bufs_used, 1);
1026 data_ptrs[0] = (uint8_t **)&ccb->cdai.buf;
1029 case XPT_NVME_ADMIN:
1030 data_ptrs[0] = &ccb->nvmeio.data_ptr;
1031 numbufs = min(mapinfo->num_bufs_used, 1);
1034 /* allow ourselves to be swapped once again */
1037 break; /* NOTREACHED */
1040 for (i = 0; i < numbufs; i++) {
1041 /* Set the user's pointer back to the original value */
1042 *data_ptrs[i] = mapinfo->bp[i]->b_caller1;
1044 /* unmap the buffer */
1045 vunmapbuf(mapinfo->bp[i]);
1047 /* release the buffer */
1048 relpbuf(mapinfo->bp[i], NULL);
1051 /* allow ourselves to be swapped once again */
1056 cam_periph_ioctl(struct cam_periph *periph, u_long cmd, caddr_t addr,
1057 int (*error_routine)(union ccb *ccb,
1059 u_int32_t sense_flags))
1068 case CAMGETPASSTHRU:
1069 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1070 xpt_setup_ccb(&ccb->ccb_h,
1072 CAM_PRIORITY_NORMAL);
1073 ccb->ccb_h.func_code = XPT_GDEVLIST;
1076 * Basically, the point of this is that we go through
1077 * getting the list of devices, until we find a passthrough
1078 * device. In the current version of the CAM code, the
1079 * only way to determine what type of device we're dealing
1080 * with is by its name.
1082 while (found == 0) {
1083 ccb->cgdl.index = 0;
1084 ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS;
1085 while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) {
1087 /* we want the next device in the list */
1089 if (strncmp(ccb->cgdl.periph_name,
1095 if ((ccb->cgdl.status == CAM_GDEVLIST_LAST_DEVICE) &&
1097 ccb->cgdl.periph_name[0] = '\0';
1098 ccb->cgdl.unit_number = 0;
1103 /* copy the result back out */
1104 bcopy(ccb, addr, sizeof(union ccb));
1106 /* and release the ccb */
1107 xpt_release_ccb(ccb);
1118 cam_periph_done_panic(struct cam_periph *periph, union ccb *done_ccb)
1121 panic("%s: already done with ccb %p", __func__, done_ccb);
1125 cam_periph_done(struct cam_periph *periph, union ccb *done_ccb)
1128 /* Caller will release the CCB */
1129 xpt_path_assert(done_ccb->ccb_h.path, MA_OWNED);
1130 done_ccb->ccb_h.cbfcnp = cam_periph_done_panic;
1131 wakeup(&done_ccb->ccb_h.cbfcnp);
1135 cam_periph_ccbwait(union ccb *ccb)
1138 if ((ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
1139 while (ccb->ccb_h.cbfcnp != cam_periph_done_panic)
1140 xpt_path_sleep(ccb->ccb_h.path, &ccb->ccb_h.cbfcnp,
1141 PRIBIO, "cbwait", 0);
1143 KASSERT(ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX &&
1144 (ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG,
1145 ("%s: proceeding with incomplete ccb: ccb=%p, func_code=%#x, "
1146 "status=%#x, index=%d", __func__, ccb, ccb->ccb_h.func_code,
1147 ccb->ccb_h.status, ccb->ccb_h.pinfo.index));
1151 cam_periph_runccb(union ccb *ccb,
1152 int (*error_routine)(union ccb *ccb,
1154 u_int32_t sense_flags),
1155 cam_flags camflags, u_int32_t sense_flags,
1158 struct bintime *starttime;
1159 struct bintime ltime;
1162 struct mtx *periph_mtx;
1163 struct cam_periph *periph;
1164 uint32_t timeout = 1;
1167 xpt_path_assert(ccb->ccb_h.path, MA_OWNED);
1168 KASSERT((ccb->ccb_h.flags & CAM_UNLOCKED) == 0,
1169 ("%s: ccb=%p, func_code=%#x, flags=%#x", __func__, ccb,
1170 ccb->ccb_h.func_code, ccb->ccb_h.flags));
1173 * If the user has supplied a stats structure, and if we understand
1174 * this particular type of ccb, record the transaction start.
1177 (ccb->ccb_h.func_code == XPT_SCSI_IO ||
1178 ccb->ccb_h.func_code == XPT_ATA_IO ||
1179 ccb->ccb_h.func_code == XPT_NVME_IO)) {
1181 binuptime(starttime);
1182 devstat_start_transaction(ds, starttime);
1185 sched_stopped = SCHEDULER_STOPPED();
1186 ccb->ccb_h.cbfcnp = cam_periph_done;
1187 periph = xpt_path_periph(ccb->ccb_h.path);
1188 periph_mtx = cam_periph_mtx(periph);
1191 * If we're polling, then we need to ensure that we have ample resources
1192 * in the periph. We also need to drop the periph lock while we're polling.
1193 * cam_periph_error can reschedule the ccb by calling xpt_action and returning
1194 * ERESTART, so we have to effect the polling in the do loop below.
1196 if (sched_stopped) {
1197 mtx_unlock(periph_mtx);
1198 timeout = xpt_poll_setup(ccb);
1202 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1208 cam_periph_ccbwait(ccb);
1210 xpt_pollwait(ccb, timeout);
1211 timeout = ccb->ccb_h.timeout * 10;
1213 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
1215 else if (error_routine != NULL) {
1216 ccb->ccb_h.cbfcnp = cam_periph_done;
1217 error = (*error_routine)(ccb, camflags, sense_flags);
1220 } while (error == ERESTART);
1224 mtx_lock(periph_mtx);
1226 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1227 cam_release_devq(ccb->ccb_h.path,
1228 /* relsim_flags */0,
1231 /* getcount_only */ FALSE);
1232 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1237 devstat_tag_type tag;
1240 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1241 bytes = ccb->csio.dxfer_len - ccb->csio.resid;
1242 tag = (devstat_tag_type)(ccb->csio.tag_action & 0x3);
1243 } else if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1244 bytes = ccb->ataio.dxfer_len - ccb->ataio.resid;
1245 tag = (devstat_tag_type)0;
1246 } else if (ccb->ccb_h.func_code == XPT_NVME_IO) {
1247 bytes = ccb->nvmeio.dxfer_len; /* NB: resid no possible */
1248 tag = (devstat_tag_type)0;
1253 devstat_end_transaction(ds, bytes, tag,
1254 ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE) ?
1255 DEVSTAT_NO_DATA : (ccb->ccb_h.flags & CAM_DIR_OUT) ?
1256 DEVSTAT_WRITE : DEVSTAT_READ, NULL, starttime);
1263 cam_freeze_devq(struct cam_path *path)
1265 struct ccb_hdr ccb_h;
1267 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("cam_freeze_devq\n"));
1268 xpt_setup_ccb(&ccb_h, path, /*priority*/1);
1269 ccb_h.func_code = XPT_NOOP;
1270 ccb_h.flags = CAM_DEV_QFREEZE;
1271 xpt_action((union ccb *)&ccb_h);
1275 cam_release_devq(struct cam_path *path, u_int32_t relsim_flags,
1276 u_int32_t openings, u_int32_t arg,
1279 struct ccb_relsim crs;
1281 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("cam_release_devq(%u, %u, %u, %d)\n",
1282 relsim_flags, openings, arg, getcount_only));
1283 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
1284 crs.ccb_h.func_code = XPT_REL_SIMQ;
1285 crs.ccb_h.flags = getcount_only ? CAM_DEV_QFREEZE : 0;
1286 crs.release_flags = relsim_flags;
1287 crs.openings = openings;
1288 crs.release_timeout = arg;
1289 xpt_action((union ccb *)&crs);
1290 return (crs.qfrozen_cnt);
1293 #define saved_ccb_ptr ppriv_ptr0
1295 camperiphdone(struct cam_periph *periph, union ccb *done_ccb)
1297 union ccb *saved_ccb;
1299 struct scsi_start_stop_unit *scsi_cmd;
1300 int error_code, sense_key, asc, ascq;
1302 scsi_cmd = (struct scsi_start_stop_unit *)
1303 &done_ccb->csio.cdb_io.cdb_bytes;
1304 status = done_ccb->ccb_h.status;
1306 if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1307 if (scsi_extract_sense_ccb(done_ccb,
1308 &error_code, &sense_key, &asc, &ascq)) {
1310 * If the error is "invalid field in CDB",
1311 * and the load/eject flag is set, turn the
1312 * flag off and try again. This is just in
1313 * case the drive in question barfs on the
1314 * load eject flag. The CAM code should set
1315 * the load/eject flag by default for
1318 if ((scsi_cmd->opcode == START_STOP_UNIT) &&
1319 ((scsi_cmd->how & SSS_LOEJ) != 0) &&
1320 (asc == 0x24) && (ascq == 0x00)) {
1321 scsi_cmd->how &= ~SSS_LOEJ;
1322 if (status & CAM_DEV_QFRZN) {
1323 cam_release_devq(done_ccb->ccb_h.path,
1325 done_ccb->ccb_h.status &=
1328 xpt_action(done_ccb);
1332 if (cam_periph_error(done_ccb,
1333 0, SF_RETRY_UA | SF_NO_PRINT) == ERESTART)
1335 if (done_ccb->ccb_h.status & CAM_DEV_QFRZN) {
1336 cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0);
1337 done_ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1341 * If we have successfully taken a device from the not
1342 * ready to ready state, re-scan the device and re-get
1343 * the inquiry information. Many devices (mostly disks)
1344 * don't properly report their inquiry information unless
1347 if (scsi_cmd->opcode == START_STOP_UNIT)
1348 xpt_async(AC_INQ_CHANGED, done_ccb->ccb_h.path, NULL);
1352 * Perform the final retry with the original CCB so that final
1353 * error processing is performed by the owner of the CCB.
1355 saved_ccb = (union ccb *)done_ccb->ccb_h.saved_ccb_ptr;
1356 bcopy(saved_ccb, done_ccb, sizeof(*done_ccb));
1357 xpt_free_ccb(saved_ccb);
1358 if (done_ccb->ccb_h.cbfcnp != camperiphdone)
1359 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1360 xpt_action(done_ccb);
1363 /* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
1364 cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0);
1368 * Generic Async Event handler. Peripheral drivers usually
1369 * filter out the events that require personal attention,
1370 * and leave the rest to this function.
1373 cam_periph_async(struct cam_periph *periph, u_int32_t code,
1374 struct cam_path *path, void *arg)
1377 case AC_LOST_DEVICE:
1378 cam_periph_invalidate(periph);
1386 cam_periph_bus_settle(struct cam_periph *periph, u_int bus_settle)
1388 struct ccb_getdevstats cgds;
1390 xpt_setup_ccb(&cgds.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1391 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1392 xpt_action((union ccb *)&cgds);
1393 cam_periph_freeze_after_event(periph, &cgds.last_reset, bus_settle);
1397 cam_periph_freeze_after_event(struct cam_periph *periph,
1398 struct timeval* event_time, u_int duration_ms)
1400 struct timeval delta;
1401 struct timeval duration_tv;
1403 if (!timevalisset(event_time))
1407 timevalsub(&delta, event_time);
1408 duration_tv.tv_sec = duration_ms / 1000;
1409 duration_tv.tv_usec = (duration_ms % 1000) * 1000;
1410 if (timevalcmp(&delta, &duration_tv, <)) {
1411 timevalsub(&duration_tv, &delta);
1413 duration_ms = duration_tv.tv_sec * 1000;
1414 duration_ms += duration_tv.tv_usec / 1000;
1415 cam_freeze_devq(periph->path);
1416 cam_release_devq(periph->path,
1417 RELSIM_RELEASE_AFTER_TIMEOUT,
1419 /*timeout*/duration_ms,
1420 /*getcount_only*/0);
1426 camperiphscsistatuserror(union ccb *ccb, union ccb **orig_ccb,
1427 cam_flags camflags, u_int32_t sense_flags,
1428 int *openings, u_int32_t *relsim_flags,
1429 u_int32_t *timeout, u_int32_t *action, const char **action_string)
1433 switch (ccb->csio.scsi_status) {
1434 case SCSI_STATUS_OK:
1435 case SCSI_STATUS_COND_MET:
1436 case SCSI_STATUS_INTERMED:
1437 case SCSI_STATUS_INTERMED_COND_MET:
1440 case SCSI_STATUS_CMD_TERMINATED:
1441 case SCSI_STATUS_CHECK_COND:
1442 error = camperiphscsisenseerror(ccb, orig_ccb,
1451 case SCSI_STATUS_QUEUE_FULL:
1454 struct ccb_getdevstats cgds;
1457 * First off, find out what the current
1458 * transaction counts are.
1460 xpt_setup_ccb(&cgds.ccb_h,
1462 CAM_PRIORITY_NORMAL);
1463 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1464 xpt_action((union ccb *)&cgds);
1467 * If we were the only transaction active, treat
1468 * the QUEUE FULL as if it were a BUSY condition.
1470 if (cgds.dev_active != 0) {
1474 * Reduce the number of openings to
1475 * be 1 less than the amount it took
1476 * to get a queue full bounded by the
1477 * minimum allowed tag count for this
1480 total_openings = cgds.dev_active + cgds.dev_openings;
1481 *openings = cgds.dev_active;
1482 if (*openings < cgds.mintags)
1483 *openings = cgds.mintags;
1484 if (*openings < total_openings)
1485 *relsim_flags = RELSIM_ADJUST_OPENINGS;
1488 * Some devices report queue full for
1489 * temporary resource shortages. For
1490 * this reason, we allow a minimum
1491 * tag count to be entered via a
1492 * quirk entry to prevent the queue
1493 * count on these devices from falling
1494 * to a pessimisticly low value. We
1495 * still wait for the next successful
1496 * completion, however, before queueing
1497 * more transactions to the device.
1499 *relsim_flags = RELSIM_RELEASE_AFTER_CMDCMPLT;
1503 *action &= ~SSQ_PRINT_SENSE;
1508 case SCSI_STATUS_BUSY:
1510 * Restart the queue after either another
1511 * command completes or a 1 second timeout.
1513 if ((sense_flags & SF_RETRY_BUSY) != 0 ||
1514 (ccb->ccb_h.retry_count--) > 0) {
1516 *relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT
1517 | RELSIM_RELEASE_AFTER_CMDCMPLT;
1523 case SCSI_STATUS_RESERV_CONFLICT:
1532 camperiphscsisenseerror(union ccb *ccb, union ccb **orig,
1533 cam_flags camflags, u_int32_t sense_flags,
1534 int *openings, u_int32_t *relsim_flags,
1535 u_int32_t *timeout, u_int32_t *action, const char **action_string)
1537 struct cam_periph *periph;
1538 union ccb *orig_ccb = ccb;
1539 int error, recoveryccb;
1541 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
1542 if (ccb->ccb_h.func_code == XPT_SCSI_IO && ccb->csio.bio != NULL)
1543 biotrack(ccb->csio.bio, __func__);
1546 periph = xpt_path_periph(ccb->ccb_h.path);
1547 recoveryccb = (ccb->ccb_h.cbfcnp == camperiphdone);
1548 if ((periph->flags & CAM_PERIPH_RECOVERY_INPROG) && !recoveryccb) {
1550 * If error recovery is already in progress, don't attempt
1551 * to process this error, but requeue it unconditionally
1552 * and attempt to process it once error recovery has
1553 * completed. This failed command is probably related to
1554 * the error that caused the currently active error recovery
1555 * action so our current recovery efforts should also
1556 * address this command. Be aware that the error recovery
1557 * code assumes that only one recovery action is in progress
1558 * on a particular peripheral instance at any given time
1559 * (e.g. only one saved CCB for error recovery) so it is
1560 * imperitive that we don't violate this assumption.
1563 *action &= ~SSQ_PRINT_SENSE;
1565 scsi_sense_action err_action;
1566 struct ccb_getdev cgd;
1569 * Grab the inquiry data for this device.
1571 xpt_setup_ccb(&cgd.ccb_h, ccb->ccb_h.path, CAM_PRIORITY_NORMAL);
1572 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1573 xpt_action((union ccb *)&cgd);
1575 err_action = scsi_error_action(&ccb->csio, &cgd.inq_data,
1577 error = err_action & SS_ERRMASK;
1580 * Do not autostart sequential access devices
1581 * to avoid unexpected tape loading.
1583 if ((err_action & SS_MASK) == SS_START &&
1584 SID_TYPE(&cgd.inq_data) == T_SEQUENTIAL) {
1585 *action_string = "Will not autostart a "
1586 "sequential access device";
1587 goto sense_error_done;
1591 * Avoid recovery recursion if recovery action is the same.
1593 if ((err_action & SS_MASK) >= SS_START && recoveryccb) {
1594 if (((err_action & SS_MASK) == SS_START &&
1595 ccb->csio.cdb_io.cdb_bytes[0] == START_STOP_UNIT) ||
1596 ((err_action & SS_MASK) == SS_TUR &&
1597 (ccb->csio.cdb_io.cdb_bytes[0] == TEST_UNIT_READY))) {
1598 err_action = SS_RETRY|SSQ_DECREMENT_COUNT|EIO;
1599 *relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1605 * If the recovery action will consume a retry,
1606 * make sure we actually have retries available.
1608 if ((err_action & SSQ_DECREMENT_COUNT) != 0) {
1609 if (ccb->ccb_h.retry_count > 0 &&
1610 (periph->flags & CAM_PERIPH_INVALID) == 0)
1611 ccb->ccb_h.retry_count--;
1613 *action_string = "Retries exhausted";
1614 goto sense_error_done;
1618 if ((err_action & SS_MASK) >= SS_START) {
1620 * Do common portions of commands that
1621 * use recovery CCBs.
1623 orig_ccb = xpt_alloc_ccb_nowait();
1624 if (orig_ccb == NULL) {
1625 *action_string = "Can't allocate recovery CCB";
1626 goto sense_error_done;
1629 * Clear freeze flag for original request here, as
1630 * this freeze will be dropped as part of ERESTART.
1632 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1633 bcopy(ccb, orig_ccb, sizeof(*orig_ccb));
1636 switch (err_action & SS_MASK) {
1638 *action_string = "No recovery action needed";
1642 *action_string = "Retrying command (per sense data)";
1646 *action_string = "Unretryable error";
1653 * Send a start unit command to the device, and
1654 * then retry the command.
1656 *action_string = "Attempting to start unit";
1657 periph->flags |= CAM_PERIPH_RECOVERY_INPROG;
1660 * Check for removable media and set
1661 * load/eject flag appropriately.
1663 if (SID_IS_REMOVABLE(&cgd.inq_data))
1668 scsi_start_stop(&ccb->csio,
1682 * Send a Test Unit Ready to the device.
1683 * If the 'many' flag is set, we send 120
1684 * test unit ready commands, one every half
1685 * second. Otherwise, we just send one TUR.
1686 * We only want to do this if the retry
1687 * count has not been exhausted.
1691 if ((err_action & SSQ_MANY) != 0) {
1692 *action_string = "Polling device for readiness";
1695 *action_string = "Testing device for readiness";
1698 periph->flags |= CAM_PERIPH_RECOVERY_INPROG;
1699 scsi_test_unit_ready(&ccb->csio,
1707 * Accomplish our 500ms delay by deferring
1708 * the release of our device queue appropriately.
1710 *relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1715 panic("Unhandled error action %x", err_action);
1718 if ((err_action & SS_MASK) >= SS_START) {
1720 * Drop the priority, so that the recovery
1721 * CCB is the first to execute. Freeze the queue
1722 * after this command is sent so that we can
1723 * restore the old csio and have it queued in
1724 * the proper order before we release normal
1725 * transactions to the device.
1727 ccb->ccb_h.pinfo.priority--;
1728 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1729 ccb->ccb_h.saved_ccb_ptr = orig_ccb;
1735 *action = err_action;
1741 * Generic error handler. Peripheral drivers usually filter
1742 * out the errors that they handle in a unique manner, then
1743 * call this function.
1746 cam_periph_error(union ccb *ccb, cam_flags camflags,
1747 u_int32_t sense_flags)
1749 struct cam_path *newpath;
1750 union ccb *orig_ccb, *scan_ccb;
1751 struct cam_periph *periph;
1752 const char *action_string;
1754 int frozen, error, openings, devctl_err;
1755 u_int32_t action, relsim_flags, timeout;
1757 action = SSQ_PRINT_SENSE;
1758 periph = xpt_path_periph(ccb->ccb_h.path);
1759 action_string = NULL;
1760 status = ccb->ccb_h.status;
1761 frozen = (status & CAM_DEV_QFRZN) != 0;
1762 status &= CAM_STATUS_MASK;
1763 devctl_err = openings = relsim_flags = timeout = 0;
1766 /* Filter the errors that should be reported via devctl */
1767 switch (ccb->ccb_h.status & CAM_STATUS_MASK) {
1768 case CAM_CMD_TIMEOUT:
1769 case CAM_REQ_ABORTED:
1770 case CAM_REQ_CMP_ERR:
1771 case CAM_REQ_TERMIO:
1772 case CAM_UNREC_HBA_ERROR:
1773 case CAM_DATA_RUN_ERR:
1774 case CAM_SCSI_STATUS_ERROR:
1775 case CAM_ATA_STATUS_ERROR:
1776 case CAM_SMP_STATUS_ERROR:
1786 action &= ~SSQ_PRINT_SENSE;
1788 case CAM_SCSI_STATUS_ERROR:
1789 error = camperiphscsistatuserror(ccb, &orig_ccb,
1790 camflags, sense_flags, &openings, &relsim_flags,
1791 &timeout, &action, &action_string);
1793 case CAM_AUTOSENSE_FAIL:
1794 error = EIO; /* we have to kill the command */
1798 case CAM_MSG_REJECT_REC:
1799 /* XXX Don't know that these are correct */
1802 case CAM_SEL_TIMEOUT:
1803 if ((camflags & CAM_RETRY_SELTO) != 0) {
1804 if (ccb->ccb_h.retry_count > 0 &&
1805 (periph->flags & CAM_PERIPH_INVALID) == 0) {
1806 ccb->ccb_h.retry_count--;
1810 * Wait a bit to give the device
1811 * time to recover before we try again.
1813 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1814 timeout = periph_selto_delay;
1817 action_string = "Retries exhausted";
1820 case CAM_DEV_NOT_THERE:
1824 case CAM_REQ_INVALID:
1825 case CAM_PATH_INVALID:
1827 case CAM_PROVIDE_FAIL:
1828 case CAM_REQ_TOO_BIG:
1829 case CAM_LUN_INVALID:
1830 case CAM_TID_INVALID:
1831 case CAM_FUNC_NOTAVAIL:
1834 case CAM_SCSI_BUS_RESET:
1837 * Commands that repeatedly timeout and cause these
1838 * kinds of error recovery actions, should return
1839 * CAM_CMD_TIMEOUT, which allows us to safely assume
1840 * that this command was an innocent bystander to
1841 * these events and should be unconditionally
1844 case CAM_REQUEUE_REQ:
1845 /* Unconditional requeue if device is still there */
1846 if (periph->flags & CAM_PERIPH_INVALID) {
1847 action_string = "Periph was invalidated";
1849 } else if (sense_flags & SF_NO_RETRY) {
1851 action_string = "Retry was blocked";
1854 action &= ~SSQ_PRINT_SENSE;
1857 case CAM_RESRC_UNAVAIL:
1858 /* Wait a bit for the resource shortage to abate. */
1859 timeout = periph_noresrc_delay;
1863 /* Wait a bit for the busy condition to abate. */
1864 timeout = periph_busy_delay;
1866 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1868 case CAM_ATA_STATUS_ERROR:
1869 case CAM_REQ_CMP_ERR:
1870 case CAM_CMD_TIMEOUT:
1871 case CAM_UNEXP_BUSFREE:
1872 case CAM_UNCOR_PARITY:
1873 case CAM_DATA_RUN_ERR:
1875 if (periph->flags & CAM_PERIPH_INVALID) {
1877 action_string = "Periph was invalidated";
1878 } else if (ccb->ccb_h.retry_count == 0) {
1880 action_string = "Retries exhausted";
1881 } else if (sense_flags & SF_NO_RETRY) {
1883 action_string = "Retry was blocked";
1885 ccb->ccb_h.retry_count--;
1891 if ((sense_flags & SF_PRINT_ALWAYS) ||
1892 CAM_DEBUGGED(ccb->ccb_h.path, CAM_DEBUG_INFO))
1893 action |= SSQ_PRINT_SENSE;
1894 else if (sense_flags & SF_NO_PRINT)
1895 action &= ~SSQ_PRINT_SENSE;
1896 if ((action & SSQ_PRINT_SENSE) != 0)
1897 cam_error_print(orig_ccb, CAM_ESF_ALL, CAM_EPF_ALL);
1898 if (error != 0 && (action & SSQ_PRINT_SENSE) != 0) {
1899 if (error != ERESTART) {
1900 if (action_string == NULL)
1901 action_string = "Unretryable error";
1902 xpt_print(ccb->ccb_h.path, "Error %d, %s\n",
1903 error, action_string);
1904 } else if (action_string != NULL)
1905 xpt_print(ccb->ccb_h.path, "%s\n", action_string);
1907 xpt_print(ccb->ccb_h.path, "Retrying command\n");
1910 if (devctl_err && (error != 0 || (action & SSQ_PRINT_SENSE) != 0))
1911 cam_periph_devctl_notify(orig_ccb);
1913 if ((action & SSQ_LOST) != 0) {
1917 * For a selection timeout, we consider all of the LUNs on
1918 * the target to be gone. If the status is CAM_DEV_NOT_THERE,
1919 * then we only get rid of the device(s) specified by the
1920 * path in the original CCB.
1922 if (status == CAM_SEL_TIMEOUT)
1923 lun_id = CAM_LUN_WILDCARD;
1925 lun_id = xpt_path_lun_id(ccb->ccb_h.path);
1927 /* Should we do more if we can't create the path?? */
1928 if (xpt_create_path(&newpath, periph,
1929 xpt_path_path_id(ccb->ccb_h.path),
1930 xpt_path_target_id(ccb->ccb_h.path),
1931 lun_id) == CAM_REQ_CMP) {
1934 * Let peripheral drivers know that this
1935 * device has gone away.
1937 xpt_async(AC_LOST_DEVICE, newpath, NULL);
1938 xpt_free_path(newpath);
1942 /* Broadcast UNIT ATTENTIONs to all periphs. */
1943 if ((action & SSQ_UA) != 0)
1944 xpt_async(AC_UNIT_ATTENTION, orig_ccb->ccb_h.path, orig_ccb);
1946 /* Rescan target on "Reported LUNs data has changed" */
1947 if ((action & SSQ_RESCAN) != 0) {
1948 if (xpt_create_path(&newpath, NULL,
1949 xpt_path_path_id(ccb->ccb_h.path),
1950 xpt_path_target_id(ccb->ccb_h.path),
1951 CAM_LUN_WILDCARD) == CAM_REQ_CMP) {
1953 scan_ccb = xpt_alloc_ccb_nowait();
1954 if (scan_ccb != NULL) {
1955 scan_ccb->ccb_h.path = newpath;
1956 scan_ccb->ccb_h.func_code = XPT_SCAN_TGT;
1957 scan_ccb->crcn.flags = 0;
1958 xpt_rescan(scan_ccb);
1961 "Can't allocate CCB to rescan target\n");
1962 xpt_free_path(newpath);
1967 /* Attempt a retry */
1968 if (error == ERESTART || error == 0) {
1970 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1971 if (error == ERESTART)
1974 cam_release_devq(ccb->ccb_h.path,
1978 /*getcount_only*/0);
1984 #define CAM_PERIPH_DEVD_MSG_SIZE 256
1987 cam_periph_devctl_notify(union ccb *ccb)
1989 struct cam_periph *periph;
1990 struct ccb_getdev *cgd;
1992 int serr, sk, asc, ascq;
1995 sbmsg = malloc(CAM_PERIPH_DEVD_MSG_SIZE, M_CAMPERIPH, M_NOWAIT);
1999 sbuf_new(&sb, sbmsg, CAM_PERIPH_DEVD_MSG_SIZE, SBUF_FIXEDLEN);
2001 periph = xpt_path_periph(ccb->ccb_h.path);
2002 sbuf_printf(&sb, "device=%s%d ", periph->periph_name,
2003 periph->unit_number);
2005 sbuf_printf(&sb, "serial=\"");
2006 if ((cgd = (struct ccb_getdev *)xpt_alloc_ccb_nowait()) != NULL) {
2007 xpt_setup_ccb(&cgd->ccb_h, ccb->ccb_h.path,
2008 CAM_PRIORITY_NORMAL);
2009 cgd->ccb_h.func_code = XPT_GDEV_TYPE;
2010 xpt_action((union ccb *)cgd);
2012 if (cgd->ccb_h.status == CAM_REQ_CMP)
2013 sbuf_bcat(&sb, cgd->serial_num, cgd->serial_num_len);
2014 xpt_free_ccb((union ccb *)cgd);
2016 sbuf_printf(&sb, "\" ");
2017 sbuf_printf(&sb, "cam_status=\"0x%x\" ", ccb->ccb_h.status);
2019 switch (ccb->ccb_h.status & CAM_STATUS_MASK) {
2020 case CAM_CMD_TIMEOUT:
2021 sbuf_printf(&sb, "timeout=%d ", ccb->ccb_h.timeout);
2024 case CAM_SCSI_STATUS_ERROR:
2025 sbuf_printf(&sb, "scsi_status=%d ", ccb->csio.scsi_status);
2026 if (scsi_extract_sense_ccb(ccb, &serr, &sk, &asc, &ascq))
2027 sbuf_printf(&sb, "scsi_sense=\"%02x %02x %02x %02x\" ",
2028 serr, sk, asc, ascq);
2031 case CAM_ATA_STATUS_ERROR:
2032 sbuf_printf(&sb, "RES=\"");
2033 ata_res_sbuf(&ccb->ataio.res, &sb);
2034 sbuf_printf(&sb, "\" ");
2042 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
2043 sbuf_printf(&sb, "CDB=\"");
2044 scsi_cdb_sbuf(scsiio_cdb_ptr(&ccb->csio), &sb);
2045 sbuf_printf(&sb, "\" ");
2046 } else if (ccb->ccb_h.func_code == XPT_ATA_IO) {
2047 sbuf_printf(&sb, "ACB=\"");
2048 ata_cmd_sbuf(&ccb->ataio.cmd, &sb);
2049 sbuf_printf(&sb, "\" ");
2052 if (sbuf_finish(&sb) == 0)
2053 devctl_notify("CAM", "periph", type, sbuf_data(&sb));
2055 free(sbmsg, M_CAMPERIPH);