2 * Common functions for CAM "type" (peripheral) drivers.
4 * Copyright (c) 1997, 1998 Justin T. Gibbs.
5 * Copyright (c) 1997, 1998, 1999, 2000 Kenneth D. Merry.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions, and the following disclaimer,
13 * without modification, immediately at the beginning of the file.
14 * 2. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/types.h>
36 #include <sys/malloc.h>
37 #include <sys/kernel.h>
40 #include <sys/mutex.h>
43 #include <sys/devicestat.h>
46 #include <sys/sysctl.h>
48 #include <vm/vm_extern.h>
51 #include <cam/cam_ccb.h>
52 #include <cam/cam_queue.h>
53 #include <cam/cam_xpt_periph.h>
54 #include <cam/cam_periph.h>
55 #include <cam/cam_debug.h>
56 #include <cam/cam_sim.h>
58 #include <cam/scsi/scsi_all.h>
59 #include <cam/scsi/scsi_message.h>
60 #include <cam/scsi/scsi_pass.h>
62 static u_int camperiphnextunit(struct periph_driver *p_drv,
63 u_int newunit, int wired,
64 path_id_t pathid, target_id_t target,
66 static u_int camperiphunit(struct periph_driver *p_drv,
67 path_id_t pathid, target_id_t target,
69 static void camperiphdone(struct cam_periph *periph,
71 static void camperiphfree(struct cam_periph *periph);
72 static int camperiphscsistatuserror(union ccb *ccb,
75 u_int32_t sense_flags,
77 u_int32_t *relsim_flags,
80 const char **action_string);
81 static int camperiphscsisenseerror(union ccb *ccb,
84 u_int32_t sense_flags,
86 u_int32_t *relsim_flags,
89 const char **action_string);
90 static void cam_periph_devctl_notify(union ccb *ccb);
92 static int nperiph_drivers;
93 static int initialized = 0;
94 struct periph_driver **periph_drivers;
96 static MALLOC_DEFINE(M_CAMPERIPH, "CAM periph", "CAM peripheral buffers");
98 static int periph_selto_delay = 1000;
99 TUNABLE_INT("kern.cam.periph_selto_delay", &periph_selto_delay);
100 static int periph_noresrc_delay = 500;
101 TUNABLE_INT("kern.cam.periph_noresrc_delay", &periph_noresrc_delay);
102 static int periph_busy_delay = 500;
103 TUNABLE_INT("kern.cam.periph_busy_delay", &periph_busy_delay);
105 static u_int periph_mapmem_thresh = 65536;
106 SYSCTL_UINT(_kern_cam, OID_AUTO, mapmem_thresh, CTLFLAG_RWTUN,
107 &periph_mapmem_thresh, 0, "Threshold for user-space buffer mapping");
110 periphdriver_register(void *data)
112 struct periph_driver *drv = (struct periph_driver *)data;
113 struct periph_driver **newdrivers, **old;
117 ndrivers = nperiph_drivers + 2;
118 newdrivers = malloc(sizeof(*newdrivers) * ndrivers, M_CAMPERIPH,
121 if (ndrivers != nperiph_drivers + 2) {
123 * Lost race against itself; go around.
126 free(newdrivers, M_CAMPERIPH);
130 bcopy(periph_drivers, newdrivers,
131 sizeof(*newdrivers) * nperiph_drivers);
132 newdrivers[nperiph_drivers] = drv;
133 newdrivers[nperiph_drivers + 1] = NULL;
134 old = periph_drivers;
135 periph_drivers = newdrivers;
139 free(old, M_CAMPERIPH);
140 /* If driver marked as early or it is late now, initialize it. */
141 if (((drv->flags & CAM_PERIPH_DRV_EARLY) != 0 && initialized > 0) ||
147 periphdriver_unregister(void *data)
149 struct periph_driver *drv = (struct periph_driver *)data;
152 /* If driver marked as early or it is late now, deinitialize it. */
153 if (((drv->flags & CAM_PERIPH_DRV_EARLY) != 0 && initialized > 0) ||
155 if (drv->deinit == NULL) {
156 printf("CAM periph driver '%s' doesn't have deinit.\n",
160 error = drv->deinit();
166 for (n = 0; n < nperiph_drivers && periph_drivers[n] != drv; n++)
168 KASSERT(n < nperiph_drivers,
169 ("Periph driver '%s' was not registered", drv->driver_name));
170 for (; n + 1 < nperiph_drivers; n++)
171 periph_drivers[n] = periph_drivers[n + 1];
172 periph_drivers[n + 1] = NULL;
179 periphdriver_init(int level)
183 initialized = max(initialized, level);
184 for (i = 0; periph_drivers[i] != NULL; i++) {
185 early = (periph_drivers[i]->flags & CAM_PERIPH_DRV_EARLY) ? 1 : 2;
186 if (early == initialized)
187 (*periph_drivers[i]->init)();
192 cam_periph_alloc(periph_ctor_t *periph_ctor,
193 periph_oninv_t *periph_oninvalidate,
194 periph_dtor_t *periph_dtor, periph_start_t *periph_start,
195 char *name, cam_periph_type type, struct cam_path *path,
196 ac_callback_t *ac_callback, ac_code code, void *arg)
198 struct periph_driver **p_drv;
200 struct cam_periph *periph;
201 struct cam_periph *cur_periph;
203 target_id_t target_id;
210 * Handle Hot-Plug scenarios. If there is already a peripheral
211 * of our type assigned to this path, we are likely waiting for
212 * final close on an old, invalidated, peripheral. If this is
213 * the case, queue up a deferred call to the peripheral's async
214 * handler. If it looks like a mistaken re-allocation, complain.
216 if ((periph = cam_periph_find(path, name)) != NULL) {
218 if ((periph->flags & CAM_PERIPH_INVALID) != 0
219 && (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) == 0) {
220 periph->flags |= CAM_PERIPH_NEW_DEV_FOUND;
221 periph->deferred_callback = ac_callback;
222 periph->deferred_ac = code;
223 return (CAM_REQ_INPROG);
225 printf("cam_periph_alloc: attempt to re-allocate "
226 "valid device %s%d rejected flags %#x "
227 "refcount %d\n", periph->periph_name,
228 periph->unit_number, periph->flags,
231 return (CAM_REQ_INVALID);
234 periph = (struct cam_periph *)malloc(sizeof(*periph), M_CAMPERIPH,
238 return (CAM_RESRC_UNAVAIL);
243 sim = xpt_path_sim(path);
244 path_id = xpt_path_path_id(path);
245 target_id = xpt_path_target_id(path);
246 lun_id = xpt_path_lun_id(path);
247 periph->periph_start = periph_start;
248 periph->periph_dtor = periph_dtor;
249 periph->periph_oninval = periph_oninvalidate;
251 periph->periph_name = name;
252 periph->scheduled_priority = CAM_PRIORITY_NONE;
253 periph->immediate_priority = CAM_PRIORITY_NONE;
254 periph->refcount = 1; /* Dropped by invalidation. */
256 SLIST_INIT(&periph->ccb_list);
257 status = xpt_create_path(&path, periph, path_id, target_id, lun_id);
258 if (status != CAM_REQ_CMP)
263 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
264 if (strcmp((*p_drv)->driver_name, name) == 0)
267 if (*p_drv == NULL) {
268 printf("cam_periph_alloc: invalid periph name '%s'\n", name);
270 xpt_free_path(periph->path);
271 free(periph, M_CAMPERIPH);
272 return (CAM_REQ_INVALID);
274 periph->unit_number = camperiphunit(*p_drv, path_id, target_id, lun_id);
275 cur_periph = TAILQ_FIRST(&(*p_drv)->units);
276 while (cur_periph != NULL
277 && cur_periph->unit_number < periph->unit_number)
278 cur_periph = TAILQ_NEXT(cur_periph, unit_links);
279 if (cur_periph != NULL) {
280 KASSERT(cur_periph->unit_number != periph->unit_number, ("duplicate units on periph list"));
281 TAILQ_INSERT_BEFORE(cur_periph, periph, unit_links);
283 TAILQ_INSERT_TAIL(&(*p_drv)->units, periph, unit_links);
284 (*p_drv)->generation++;
290 status = xpt_add_periph(periph);
291 if (status != CAM_REQ_CMP)
295 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph created\n"));
297 status = periph_ctor(periph, arg);
299 if (status == CAM_REQ_CMP)
303 switch (init_level) {
305 /* Initialized successfully */
308 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph destroyed\n"));
309 xpt_remove_periph(periph);
313 TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
315 xpt_free_path(periph->path);
318 free(periph, M_CAMPERIPH);
321 /* No cleanup to perform. */
324 panic("%s: Unknown init level", __func__);
330 * Find a peripheral structure with the specified path, target, lun,
331 * and (optionally) type. If the name is NULL, this function will return
332 * the first peripheral driver that matches the specified path.
335 cam_periph_find(struct cam_path *path, char *name)
337 struct periph_driver **p_drv;
338 struct cam_periph *periph;
341 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
343 if (name != NULL && (strcmp((*p_drv)->driver_name, name) != 0))
346 TAILQ_FOREACH(periph, &(*p_drv)->units, unit_links) {
347 if (xpt_path_comp(periph->path, path) == 0) {
349 cam_periph_assert(periph, MA_OWNED);
363 * Find peripheral driver instances attached to the specified path.
366 cam_periph_list(struct cam_path *path, struct sbuf *sb)
368 struct sbuf local_sb;
369 struct periph_driver **p_drv;
370 struct cam_periph *periph;
376 sbuf_new(&local_sb, NULL, sbuf_alloc_len, SBUF_FIXEDLEN);
379 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
381 TAILQ_FOREACH(periph, &(*p_drv)->units, unit_links) {
382 if (xpt_path_comp(periph->path, path) != 0)
385 if (sbuf_len(&local_sb) != 0)
386 sbuf_cat(&local_sb, ",");
388 sbuf_printf(&local_sb, "%s%d", periph->periph_name,
389 periph->unit_number);
391 if (sbuf_error(&local_sb) == ENOMEM) {
394 sbuf_delete(&local_sb);
401 sbuf_finish(&local_sb);
402 sbuf_cpy(sb, sbuf_data(&local_sb));
403 sbuf_delete(&local_sb);
408 cam_periph_acquire(struct cam_periph *periph)
412 status = CAM_REQ_CMP_ERR;
417 if ((periph->flags & CAM_PERIPH_INVALID) == 0) {
419 status = CAM_REQ_CMP;
427 cam_periph_doacquire(struct cam_periph *periph)
431 KASSERT(periph->refcount >= 1,
432 ("cam_periph_doacquire() with refcount == %d", periph->refcount));
438 cam_periph_release_locked_buses(struct cam_periph *periph)
441 cam_periph_assert(periph, MA_OWNED);
442 KASSERT(periph->refcount >= 1, ("periph->refcount >= 1"));
443 if (--periph->refcount == 0)
444 camperiphfree(periph);
448 cam_periph_release_locked(struct cam_periph *periph)
455 cam_periph_release_locked_buses(periph);
460 cam_periph_release(struct cam_periph *periph)
467 cam_periph_assert(periph, MA_NOTOWNED);
468 mtx = cam_periph_mtx(periph);
470 cam_periph_release_locked(periph);
475 cam_periph_hold(struct cam_periph *periph, int priority)
480 * Increment the reference count on the peripheral
481 * while we wait for our lock attempt to succeed
482 * to ensure the peripheral doesn't disappear out
483 * from user us while we sleep.
486 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
489 cam_periph_assert(periph, MA_OWNED);
490 while ((periph->flags & CAM_PERIPH_LOCKED) != 0) {
491 periph->flags |= CAM_PERIPH_LOCK_WANTED;
492 if ((error = cam_periph_sleep(periph, periph, priority,
493 "caplck", 0)) != 0) {
494 cam_periph_release_locked(periph);
497 if (periph->flags & CAM_PERIPH_INVALID) {
498 cam_periph_release_locked(periph);
503 periph->flags |= CAM_PERIPH_LOCKED;
508 cam_periph_unhold(struct cam_periph *periph)
511 cam_periph_assert(periph, MA_OWNED);
513 periph->flags &= ~CAM_PERIPH_LOCKED;
514 if ((periph->flags & CAM_PERIPH_LOCK_WANTED) != 0) {
515 periph->flags &= ~CAM_PERIPH_LOCK_WANTED;
519 cam_periph_release_locked(periph);
523 * Look for the next unit number that is not currently in use for this
524 * peripheral type starting at "newunit". Also exclude unit numbers that
525 * are reserved by for future "hardwiring" unless we already know that this
526 * is a potential wired device. Only assume that the device is "wired" the
527 * first time through the loop since after that we'll be looking at unit
528 * numbers that did not match a wiring entry.
531 camperiphnextunit(struct periph_driver *p_drv, u_int newunit, int wired,
532 path_id_t pathid, target_id_t target, lun_id_t lun)
534 struct cam_periph *periph;
536 int i, val, dunit, r;
537 const char *dname, *strval;
539 periph_name = p_drv->driver_name;
542 for (periph = TAILQ_FIRST(&p_drv->units);
543 periph != NULL && periph->unit_number != newunit;
544 periph = TAILQ_NEXT(periph, unit_links))
547 if (periph != NULL && periph->unit_number == newunit) {
549 xpt_print(periph->path, "Duplicate Wired "
551 xpt_print(periph->path, "Second device (%s "
552 "device at scbus%d target %d lun %d) will "
553 "not be wired\n", periph_name, pathid,
563 * Don't match entries like "da 4" as a wired down
564 * device, but do match entries like "da 4 target 5"
565 * or even "da 4 scbus 1".
570 r = resource_find_dev(&i, dname, &dunit, NULL, NULL);
573 /* if no "target" and no specific scbus, skip */
574 if (resource_int_value(dname, dunit, "target", &val) &&
575 (resource_string_value(dname, dunit, "at",&strval)||
576 strcmp(strval, "scbus") == 0))
578 if (newunit == dunit)
588 camperiphunit(struct periph_driver *p_drv, path_id_t pathid,
589 target_id_t target, lun_id_t lun)
592 int wired, i, val, dunit;
593 const char *dname, *strval;
594 char pathbuf[32], *periph_name;
596 periph_name = p_drv->driver_name;
597 snprintf(pathbuf, sizeof(pathbuf), "scbus%d", pathid);
601 for (wired = 0; resource_find_dev(&i, dname, &dunit, NULL, NULL) == 0;
603 if (resource_string_value(dname, dunit, "at", &strval) == 0) {
604 if (strcmp(strval, pathbuf) != 0)
608 if (resource_int_value(dname, dunit, "target", &val) == 0) {
613 if (resource_int_value(dname, dunit, "lun", &val) == 0) {
625 * Either start from 0 looking for the next unit or from
626 * the unit number given in the resource config. This way,
627 * if we have wildcard matches, we don't return the same
630 unit = camperiphnextunit(p_drv, unit, wired, pathid, target, lun);
636 cam_periph_invalidate(struct cam_periph *periph)
639 cam_periph_assert(periph, MA_OWNED);
641 * We only call this routine the first time a peripheral is
644 if ((periph->flags & CAM_PERIPH_INVALID) != 0)
647 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph invalidated\n"));
648 if ((periph->flags & CAM_PERIPH_ANNOUNCED) && !rebooting)
649 xpt_denounce_periph(periph);
650 periph->flags |= CAM_PERIPH_INVALID;
651 periph->flags &= ~CAM_PERIPH_NEW_DEV_FOUND;
652 if (periph->periph_oninval != NULL)
653 periph->periph_oninval(periph);
654 cam_periph_release_locked(periph);
658 camperiphfree(struct cam_periph *periph)
660 struct periph_driver **p_drv;
661 struct periph_driver *drv;
663 cam_periph_assert(periph, MA_OWNED);
664 KASSERT(periph->periph_allocating == 0, ("%s%d: freed while allocating",
665 periph->periph_name, periph->unit_number));
666 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
667 if (strcmp((*p_drv)->driver_name, periph->periph_name) == 0)
670 if (*p_drv == NULL) {
671 printf("camperiphfree: attempt to free non-existant periph\n");
675 * Cache a pointer to the periph_driver structure. If a
676 * periph_driver is added or removed from the array (see
677 * periphdriver_register()) while we drop the toplogy lock
678 * below, p_drv may change. This doesn't protect against this
679 * particular periph_driver going away. That will require full
680 * reference counting in the periph_driver infrastructure.
685 * We need to set this flag before dropping the topology lock, to
686 * let anyone who is traversing the list that this peripheral is
687 * about to be freed, and there will be no more reference count
690 periph->flags |= CAM_PERIPH_FREE;
693 * The peripheral destructor semantics dictate calling with only the
694 * SIM mutex held. Since it might sleep, it should not be called
695 * with the topology lock held.
700 * We need to call the peripheral destructor prior to removing the
701 * peripheral from the list. Otherwise, we risk running into a
702 * scenario where the peripheral unit number may get reused
703 * (because it has been removed from the list), but some resources
704 * used by the peripheral are still hanging around. In particular,
705 * the devfs nodes used by some peripherals like the pass(4) driver
706 * aren't fully cleaned up until the destructor is run. If the
707 * unit number is reused before the devfs instance is fully gone,
710 if (periph->periph_dtor != NULL)
711 periph->periph_dtor(periph);
714 * The peripheral list is protected by the topology lock.
718 TAILQ_REMOVE(&drv->units, periph, unit_links);
721 xpt_remove_periph(periph);
724 if ((periph->flags & CAM_PERIPH_ANNOUNCED) && !rebooting)
725 xpt_print(periph->path, "Periph destroyed\n");
727 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph destroyed\n"));
729 if (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) {
733 switch (periph->deferred_ac) {
734 case AC_FOUND_DEVICE:
735 ccb.ccb_h.func_code = XPT_GDEV_TYPE;
736 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
740 case AC_PATH_REGISTERED:
741 ccb.ccb_h.func_code = XPT_PATH_INQ;
742 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
750 periph->deferred_callback(NULL, periph->deferred_ac,
753 xpt_free_path(periph->path);
754 free(periph, M_CAMPERIPH);
759 * Map user virtual pointers into kernel virtual address space, so we can
760 * access the memory. This is now a generic function that centralizes most
761 * of the sanity checks on the data flags, if any.
762 * This also only works for up to MAXPHYS memory. Since we use
763 * buffers to map stuff in and out, we're limited to the buffer size.
766 cam_periph_mapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo,
770 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
771 u_int32_t lengths[CAM_PERIPH_MAXMAPS];
772 u_int32_t dirs[CAM_PERIPH_MAXMAPS];
774 bzero(mapinfo, sizeof(*mapinfo));
776 maxmap = DFLTPHYS; /* traditional default */
777 else if (maxmap > MAXPHYS)
778 maxmap = MAXPHYS; /* for safety */
779 switch(ccb->ccb_h.func_code) {
781 if (ccb->cdm.match_buf_len == 0) {
782 printf("cam_periph_mapmem: invalid match buffer "
786 if (ccb->cdm.pattern_buf_len > 0) {
787 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
788 lengths[0] = ccb->cdm.pattern_buf_len;
789 dirs[0] = CAM_DIR_OUT;
790 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
791 lengths[1] = ccb->cdm.match_buf_len;
792 dirs[1] = CAM_DIR_IN;
795 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
796 lengths[0] = ccb->cdm.match_buf_len;
797 dirs[0] = CAM_DIR_IN;
801 * This request will not go to the hardware, no reason
802 * to be so strict. vmapbuf() is able to map up to MAXPHYS.
807 case XPT_CONT_TARGET_IO:
808 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
810 if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR)
812 data_ptrs[0] = &ccb->csio.data_ptr;
813 lengths[0] = ccb->csio.dxfer_len;
814 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
818 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
820 if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR)
822 data_ptrs[0] = &ccb->ataio.data_ptr;
823 lengths[0] = ccb->ataio.dxfer_len;
824 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
828 data_ptrs[0] = &ccb->smpio.smp_request;
829 lengths[0] = ccb->smpio.smp_request_len;
830 dirs[0] = CAM_DIR_OUT;
831 data_ptrs[1] = &ccb->smpio.smp_response;
832 lengths[1] = ccb->smpio.smp_response_len;
833 dirs[1] = CAM_DIR_IN;
838 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
840 if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR)
842 data_ptrs[0] = &ccb->nvmeio.data_ptr;
843 lengths[0] = ccb->nvmeio.dxfer_len;
844 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
847 case XPT_DEV_ADVINFO:
848 if (ccb->cdai.bufsiz == 0)
851 data_ptrs[0] = (uint8_t **)&ccb->cdai.buf;
852 lengths[0] = ccb->cdai.bufsiz;
853 dirs[0] = CAM_DIR_IN;
857 * This request will not go to the hardware, no reason
858 * to be so strict. vmapbuf() is able to map up to MAXPHYS.
864 break; /* NOTREACHED */
868 * Check the transfer length and permissions first, so we don't
869 * have to unmap any previously mapped buffers.
871 for (i = 0; i < numbufs; i++) {
874 * The userland data pointer passed in may not be page
875 * aligned. vmapbuf() truncates the address to a page
876 * boundary, so if the address isn't page aligned, we'll
877 * need enough space for the given transfer length, plus
878 * whatever extra space is necessary to make it to the page
882 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)) > maxmap){
883 printf("cam_periph_mapmem: attempt to map %lu bytes, "
884 "which is greater than %lu\n",
886 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)),
893 * This keeps the kernel stack of current thread from getting
894 * swapped. In low-memory situations where the kernel stack might
895 * otherwise get swapped out, this holds it and allows the thread
896 * to make progress and release the kernel mapped pages sooner.
898 * XXX KDM should I use P_NOSWAP instead?
902 for (i = 0; i < numbufs; i++) {
904 /* Save the user's data address. */
905 mapinfo->orig[i] = *data_ptrs[i];
908 * For small buffers use malloc+copyin/copyout instead of
909 * mapping to KVA to avoid expensive TLB shootdowns. For
910 * small allocations malloc is backed by UMA, and so much
911 * cheaper on SMP systems.
913 if (lengths[i] <= periph_mapmem_thresh) {
914 *data_ptrs[i] = malloc(lengths[i], M_CAMPERIPH,
916 if (dirs[i] != CAM_DIR_IN) {
917 if (copyin(mapinfo->orig[i], *data_ptrs[i],
919 free(*data_ptrs[i], M_CAMPERIPH);
920 *data_ptrs[i] = mapinfo->orig[i];
924 bzero(*data_ptrs[i], lengths[i]);
931 mapinfo->bp[i] = getpbuf(NULL);
933 /* put our pointer in the data slot */
934 mapinfo->bp[i]->b_data = *data_ptrs[i];
936 /* set the transfer length, we know it's < MAXPHYS */
937 mapinfo->bp[i]->b_bufsize = lengths[i];
939 /* set the direction */
940 mapinfo->bp[i]->b_iocmd = (dirs[i] == CAM_DIR_OUT) ?
941 BIO_WRITE : BIO_READ;
944 * Map the buffer into kernel memory.
946 * Note that useracc() alone is not a sufficient test.
947 * vmapbuf() can still fail due to a smaller file mapped
948 * into a larger area of VM, or if userland races against
949 * vmapbuf() after the useracc() check.
951 if (vmapbuf(mapinfo->bp[i], 1) < 0) {
952 relpbuf(mapinfo->bp[i], NULL);
956 /* set our pointer to the new mapped area */
957 *data_ptrs[i] = mapinfo->bp[i]->b_data;
961 * Now that we've gotten this far, change ownership to the kernel
962 * of the buffers so that we don't run afoul of returning to user
963 * space with locks (on the buffer) held.
965 for (i = 0; i < numbufs; i++) {
967 BUF_KERNPROC(mapinfo->bp[i]);
970 mapinfo->num_bufs_used = numbufs;
974 for (i--; i >= 0; i--) {
975 if (mapinfo->bp[i]) {
976 vunmapbuf(mapinfo->bp[i]);
977 relpbuf(mapinfo->bp[i], NULL);
979 free(*data_ptrs[i], M_CAMPERIPH);
980 *data_ptrs[i] = mapinfo->orig[i];
987 * Unmap memory segments mapped into kernel virtual address space by
988 * cam_periph_mapmem().
991 cam_periph_unmapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
994 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
995 u_int32_t lengths[CAM_PERIPH_MAXMAPS];
996 u_int32_t dirs[CAM_PERIPH_MAXMAPS];
998 if (mapinfo->num_bufs_used <= 0) {
999 /* nothing to free and the process wasn't held. */
1003 switch (ccb->ccb_h.func_code) {
1005 if (ccb->cdm.pattern_buf_len > 0) {
1006 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
1007 lengths[0] = ccb->cdm.pattern_buf_len;
1008 dirs[0] = CAM_DIR_OUT;
1009 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
1010 lengths[1] = ccb->cdm.match_buf_len;
1011 dirs[1] = CAM_DIR_IN;
1014 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
1015 lengths[0] = ccb->cdm.match_buf_len;
1016 dirs[0] = CAM_DIR_IN;
1021 case XPT_CONT_TARGET_IO:
1022 data_ptrs[0] = &ccb->csio.data_ptr;
1023 lengths[0] = ccb->csio.dxfer_len;
1024 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
1028 data_ptrs[0] = &ccb->ataio.data_ptr;
1029 lengths[0] = ccb->ataio.dxfer_len;
1030 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
1034 data_ptrs[0] = &ccb->smpio.smp_request;
1035 lengths[0] = ccb->smpio.smp_request_len;
1036 dirs[0] = CAM_DIR_OUT;
1037 data_ptrs[1] = &ccb->smpio.smp_response;
1038 lengths[1] = ccb->smpio.smp_response_len;
1039 dirs[1] = CAM_DIR_IN;
1043 case XPT_NVME_ADMIN:
1044 data_ptrs[0] = &ccb->nvmeio.data_ptr;
1045 lengths[0] = ccb->nvmeio.dxfer_len;
1046 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
1049 case XPT_DEV_ADVINFO:
1050 data_ptrs[0] = (uint8_t **)&ccb->cdai.buf;
1051 lengths[0] = ccb->cdai.bufsiz;
1052 dirs[0] = CAM_DIR_IN;
1056 /* allow ourselves to be swapped once again */
1059 break; /* NOTREACHED */
1062 for (i = 0; i < numbufs; i++) {
1063 if (mapinfo->bp[i]) {
1064 /* unmap the buffer */
1065 vunmapbuf(mapinfo->bp[i]);
1067 /* release the buffer */
1068 relpbuf(mapinfo->bp[i], NULL);
1070 if (dirs[i] != CAM_DIR_OUT) {
1071 copyout(*data_ptrs[i], mapinfo->orig[i],
1074 free(*data_ptrs[i], M_CAMPERIPH);
1077 /* Set the user's pointer back to the original value */
1078 *data_ptrs[i] = mapinfo->orig[i];
1081 /* allow ourselves to be swapped once again */
1086 cam_periph_ioctl(struct cam_periph *periph, u_long cmd, caddr_t addr,
1087 int (*error_routine)(union ccb *ccb,
1089 u_int32_t sense_flags))
1098 case CAMGETPASSTHRU:
1099 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1100 xpt_setup_ccb(&ccb->ccb_h,
1102 CAM_PRIORITY_NORMAL);
1103 ccb->ccb_h.func_code = XPT_GDEVLIST;
1106 * Basically, the point of this is that we go through
1107 * getting the list of devices, until we find a passthrough
1108 * device. In the current version of the CAM code, the
1109 * only way to determine what type of device we're dealing
1110 * with is by its name.
1112 while (found == 0) {
1113 ccb->cgdl.index = 0;
1114 ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS;
1115 while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) {
1117 /* we want the next device in the list */
1119 if (strncmp(ccb->cgdl.periph_name,
1125 if ((ccb->cgdl.status == CAM_GDEVLIST_LAST_DEVICE) &&
1127 ccb->cgdl.periph_name[0] = '\0';
1128 ccb->cgdl.unit_number = 0;
1133 /* copy the result back out */
1134 bcopy(ccb, addr, sizeof(union ccb));
1136 /* and release the ccb */
1137 xpt_release_ccb(ccb);
1148 cam_periph_done_panic(struct cam_periph *periph, union ccb *done_ccb)
1151 panic("%s: already done with ccb %p", __func__, done_ccb);
1155 cam_periph_done(struct cam_periph *periph, union ccb *done_ccb)
1158 /* Caller will release the CCB */
1159 xpt_path_assert(done_ccb->ccb_h.path, MA_OWNED);
1160 done_ccb->ccb_h.cbfcnp = cam_periph_done_panic;
1161 wakeup(&done_ccb->ccb_h.cbfcnp);
1165 cam_periph_ccbwait(union ccb *ccb)
1168 if ((ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
1169 while (ccb->ccb_h.cbfcnp != cam_periph_done_panic)
1170 xpt_path_sleep(ccb->ccb_h.path, &ccb->ccb_h.cbfcnp,
1171 PRIBIO, "cbwait", 0);
1173 KASSERT(ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX &&
1174 (ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG,
1175 ("%s: proceeding with incomplete ccb: ccb=%p, func_code=%#x, "
1176 "status=%#x, index=%d", __func__, ccb, ccb->ccb_h.func_code,
1177 ccb->ccb_h.status, ccb->ccb_h.pinfo.index));
1181 cam_periph_runccb(union ccb *ccb,
1182 int (*error_routine)(union ccb *ccb,
1184 u_int32_t sense_flags),
1185 cam_flags camflags, u_int32_t sense_flags,
1188 struct bintime *starttime;
1189 struct bintime ltime;
1193 xpt_path_assert(ccb->ccb_h.path, MA_OWNED);
1194 KASSERT((ccb->ccb_h.flags & CAM_UNLOCKED) == 0,
1195 ("%s: ccb=%p, func_code=%#x, flags=%#x", __func__, ccb,
1196 ccb->ccb_h.func_code, ccb->ccb_h.flags));
1199 * If the user has supplied a stats structure, and if we understand
1200 * this particular type of ccb, record the transaction start.
1202 if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO ||
1203 ccb->ccb_h.func_code == XPT_ATA_IO)) {
1205 binuptime(starttime);
1206 devstat_start_transaction(ds, starttime);
1209 ccb->ccb_h.cbfcnp = cam_periph_done;
1213 cam_periph_ccbwait(ccb);
1214 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
1216 else if (error_routine != NULL) {
1217 ccb->ccb_h.cbfcnp = cam_periph_done;
1218 error = (*error_routine)(ccb, camflags, sense_flags);
1222 } while (error == ERESTART);
1224 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1225 cam_release_devq(ccb->ccb_h.path,
1226 /* relsim_flags */0,
1229 /* getcount_only */ FALSE);
1230 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1234 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1235 devstat_end_transaction(ds,
1236 ccb->csio.dxfer_len - ccb->csio.resid,
1237 ccb->csio.tag_action & 0x3,
1238 ((ccb->ccb_h.flags & CAM_DIR_MASK) ==
1239 CAM_DIR_NONE) ? DEVSTAT_NO_DATA :
1240 (ccb->ccb_h.flags & CAM_DIR_OUT) ?
1242 DEVSTAT_READ, NULL, starttime);
1243 } else if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1244 devstat_end_transaction(ds,
1245 ccb->ataio.dxfer_len - ccb->ataio.resid,
1246 0, /* Not used in ATA */
1247 ((ccb->ccb_h.flags & CAM_DIR_MASK) ==
1248 CAM_DIR_NONE) ? DEVSTAT_NO_DATA :
1249 (ccb->ccb_h.flags & CAM_DIR_OUT) ?
1251 DEVSTAT_READ, NULL, starttime);
1259 cam_freeze_devq(struct cam_path *path)
1261 struct ccb_hdr ccb_h;
1263 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("cam_freeze_devq\n"));
1264 xpt_setup_ccb(&ccb_h, path, /*priority*/1);
1265 ccb_h.func_code = XPT_NOOP;
1266 ccb_h.flags = CAM_DEV_QFREEZE;
1267 xpt_action((union ccb *)&ccb_h);
1271 cam_release_devq(struct cam_path *path, u_int32_t relsim_flags,
1272 u_int32_t openings, u_int32_t arg,
1275 struct ccb_relsim crs;
1277 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("cam_release_devq(%u, %u, %u, %d)\n",
1278 relsim_flags, openings, arg, getcount_only));
1279 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
1280 crs.ccb_h.func_code = XPT_REL_SIMQ;
1281 crs.ccb_h.flags = getcount_only ? CAM_DEV_QFREEZE : 0;
1282 crs.release_flags = relsim_flags;
1283 crs.openings = openings;
1284 crs.release_timeout = arg;
1285 xpt_action((union ccb *)&crs);
1286 return (crs.qfrozen_cnt);
1289 #define saved_ccb_ptr ppriv_ptr0
1291 camperiphdone(struct cam_periph *periph, union ccb *done_ccb)
1293 union ccb *saved_ccb;
1295 struct scsi_start_stop_unit *scsi_cmd;
1296 int error = 0, error_code, sense_key, asc, ascq;
1298 scsi_cmd = (struct scsi_start_stop_unit *)
1299 &done_ccb->csio.cdb_io.cdb_bytes;
1300 status = done_ccb->ccb_h.status;
1302 if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1303 if (scsi_extract_sense_ccb(done_ccb,
1304 &error_code, &sense_key, &asc, &ascq)) {
1306 * If the error is "invalid field in CDB",
1307 * and the load/eject flag is set, turn the
1308 * flag off and try again. This is just in
1309 * case the drive in question barfs on the
1310 * load eject flag. The CAM code should set
1311 * the load/eject flag by default for
1314 if ((scsi_cmd->opcode == START_STOP_UNIT) &&
1315 ((scsi_cmd->how & SSS_LOEJ) != 0) &&
1316 (asc == 0x24) && (ascq == 0x00)) {
1317 scsi_cmd->how &= ~SSS_LOEJ;
1318 if (status & CAM_DEV_QFRZN) {
1319 cam_release_devq(done_ccb->ccb_h.path,
1321 done_ccb->ccb_h.status &=
1324 xpt_action(done_ccb);
1328 error = cam_periph_error(done_ccb, 0,
1329 SF_RETRY_UA | SF_NO_PRINT, NULL);
1330 if (error == ERESTART)
1332 if (done_ccb->ccb_h.status & CAM_DEV_QFRZN) {
1333 cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0);
1334 done_ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1338 * If we have successfully taken a device from the not
1339 * ready to ready state, re-scan the device and re-get
1340 * the inquiry information. Many devices (mostly disks)
1341 * don't properly report their inquiry information unless
1344 if (scsi_cmd->opcode == START_STOP_UNIT)
1345 xpt_async(AC_INQ_CHANGED, done_ccb->ccb_h.path, NULL);
1349 * After recovery action(s) completed, return to the original CCB.
1350 * If the recovery CCB has failed, considering its own possible
1351 * retries and recovery, assume we are back in state where we have
1352 * been originally, but without recovery hopes left. In such case,
1353 * after the final attempt below, we cancel any further retries,
1354 * blocking by that also any new recovery attempts for this CCB,
1355 * and the result will be the final one returned to the CCB owher.
1357 saved_ccb = (union ccb *)done_ccb->ccb_h.saved_ccb_ptr;
1358 bcopy(saved_ccb, done_ccb, sizeof(*done_ccb));
1359 xpt_free_ccb(saved_ccb);
1360 if (done_ccb->ccb_h.cbfcnp != camperiphdone)
1361 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1363 done_ccb->ccb_h.retry_count = 0;
1364 xpt_action(done_ccb);
1367 /* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
1368 cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0);
1372 * Generic Async Event handler. Peripheral drivers usually
1373 * filter out the events that require personal attention,
1374 * and leave the rest to this function.
1377 cam_periph_async(struct cam_periph *periph, u_int32_t code,
1378 struct cam_path *path, void *arg)
1381 case AC_LOST_DEVICE:
1382 cam_periph_invalidate(periph);
1390 cam_periph_bus_settle(struct cam_periph *periph, u_int bus_settle)
1392 struct ccb_getdevstats cgds;
1394 xpt_setup_ccb(&cgds.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1395 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1396 xpt_action((union ccb *)&cgds);
1397 cam_periph_freeze_after_event(periph, &cgds.last_reset, bus_settle);
1401 cam_periph_freeze_after_event(struct cam_periph *periph,
1402 struct timeval* event_time, u_int duration_ms)
1404 struct timeval delta;
1405 struct timeval duration_tv;
1407 if (!timevalisset(event_time))
1411 timevalsub(&delta, event_time);
1412 duration_tv.tv_sec = duration_ms / 1000;
1413 duration_tv.tv_usec = (duration_ms % 1000) * 1000;
1414 if (timevalcmp(&delta, &duration_tv, <)) {
1415 timevalsub(&duration_tv, &delta);
1417 duration_ms = duration_tv.tv_sec * 1000;
1418 duration_ms += duration_tv.tv_usec / 1000;
1419 cam_freeze_devq(periph->path);
1420 cam_release_devq(periph->path,
1421 RELSIM_RELEASE_AFTER_TIMEOUT,
1423 /*timeout*/duration_ms,
1424 /*getcount_only*/0);
1430 camperiphscsistatuserror(union ccb *ccb, union ccb **orig_ccb,
1431 cam_flags camflags, u_int32_t sense_flags,
1432 int *openings, u_int32_t *relsim_flags,
1433 u_int32_t *timeout, u_int32_t *action, const char **action_string)
1435 struct cam_periph *periph;
1438 switch (ccb->csio.scsi_status) {
1439 case SCSI_STATUS_OK:
1440 case SCSI_STATUS_COND_MET:
1441 case SCSI_STATUS_INTERMED:
1442 case SCSI_STATUS_INTERMED_COND_MET:
1445 case SCSI_STATUS_CMD_TERMINATED:
1446 case SCSI_STATUS_CHECK_COND:
1447 error = camperiphscsisenseerror(ccb, orig_ccb,
1456 case SCSI_STATUS_QUEUE_FULL:
1459 struct ccb_getdevstats cgds;
1462 * First off, find out what the current
1463 * transaction counts are.
1465 xpt_setup_ccb(&cgds.ccb_h,
1467 CAM_PRIORITY_NORMAL);
1468 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1469 xpt_action((union ccb *)&cgds);
1472 * If we were the only transaction active, treat
1473 * the QUEUE FULL as if it were a BUSY condition.
1475 if (cgds.dev_active != 0) {
1479 * Reduce the number of openings to
1480 * be 1 less than the amount it took
1481 * to get a queue full bounded by the
1482 * minimum allowed tag count for this
1485 total_openings = cgds.dev_active + cgds.dev_openings;
1486 *openings = cgds.dev_active;
1487 if (*openings < cgds.mintags)
1488 *openings = cgds.mintags;
1489 if (*openings < total_openings)
1490 *relsim_flags = RELSIM_ADJUST_OPENINGS;
1493 * Some devices report queue full for
1494 * temporary resource shortages. For
1495 * this reason, we allow a minimum
1496 * tag count to be entered via a
1497 * quirk entry to prevent the queue
1498 * count on these devices from falling
1499 * to a pessimisticly low value. We
1500 * still wait for the next successful
1501 * completion, however, before queueing
1502 * more transactions to the device.
1504 *relsim_flags = RELSIM_RELEASE_AFTER_CMDCMPLT;
1508 *action &= ~SSQ_PRINT_SENSE;
1513 case SCSI_STATUS_BUSY:
1515 * Restart the queue after either another
1516 * command completes or a 1 second timeout.
1518 periph = xpt_path_periph(ccb->ccb_h.path);
1519 if (periph->flags & CAM_PERIPH_INVALID) {
1521 *action_string = "Periph was invalidated";
1522 } else if ((sense_flags & SF_RETRY_BUSY) != 0 ||
1523 ccb->ccb_h.retry_count > 0) {
1524 if ((sense_flags & SF_RETRY_BUSY) == 0)
1525 ccb->ccb_h.retry_count--;
1527 *relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT
1528 | RELSIM_RELEASE_AFTER_CMDCMPLT;
1532 *action_string = "Retries exhausted";
1535 case SCSI_STATUS_RESERV_CONFLICT:
1544 camperiphscsisenseerror(union ccb *ccb, union ccb **orig,
1545 cam_flags camflags, u_int32_t sense_flags,
1546 int *openings, u_int32_t *relsim_flags,
1547 u_int32_t *timeout, u_int32_t *action, const char **action_string)
1549 struct cam_periph *periph;
1550 union ccb *orig_ccb = ccb;
1551 int error, recoveryccb;
1553 periph = xpt_path_periph(ccb->ccb_h.path);
1554 recoveryccb = (ccb->ccb_h.cbfcnp == camperiphdone);
1555 if ((periph->flags & CAM_PERIPH_RECOVERY_INPROG) && !recoveryccb) {
1557 * If error recovery is already in progress, don't attempt
1558 * to process this error, but requeue it unconditionally
1559 * and attempt to process it once error recovery has
1560 * completed. This failed command is probably related to
1561 * the error that caused the currently active error recovery
1562 * action so our current recovery efforts should also
1563 * address this command. Be aware that the error recovery
1564 * code assumes that only one recovery action is in progress
1565 * on a particular peripheral instance at any given time
1566 * (e.g. only one saved CCB for error recovery) so it is
1567 * imperitive that we don't violate this assumption.
1570 *action &= ~SSQ_PRINT_SENSE;
1572 scsi_sense_action err_action;
1573 struct ccb_getdev cgd;
1576 * Grab the inquiry data for this device.
1578 xpt_setup_ccb(&cgd.ccb_h, ccb->ccb_h.path, CAM_PRIORITY_NORMAL);
1579 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1580 xpt_action((union ccb *)&cgd);
1582 err_action = scsi_error_action(&ccb->csio, &cgd.inq_data,
1584 error = err_action & SS_ERRMASK;
1587 * Do not autostart sequential access devices
1588 * to avoid unexpected tape loading.
1590 if ((err_action & SS_MASK) == SS_START &&
1591 SID_TYPE(&cgd.inq_data) == T_SEQUENTIAL) {
1592 *action_string = "Will not autostart a "
1593 "sequential access device";
1594 goto sense_error_done;
1598 * Avoid recovery recursion if recovery action is the same.
1600 if ((err_action & SS_MASK) >= SS_START && recoveryccb) {
1601 if (((err_action & SS_MASK) == SS_START &&
1602 ccb->csio.cdb_io.cdb_bytes[0] == START_STOP_UNIT) ||
1603 ((err_action & SS_MASK) == SS_TUR &&
1604 (ccb->csio.cdb_io.cdb_bytes[0] == TEST_UNIT_READY))) {
1605 err_action = SS_RETRY|SSQ_DECREMENT_COUNT|EIO;
1606 *relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1612 * If the recovery action will consume a retry,
1613 * make sure we actually have retries available.
1615 if ((err_action & SSQ_DECREMENT_COUNT) != 0) {
1616 if (ccb->ccb_h.retry_count > 0 &&
1617 (periph->flags & CAM_PERIPH_INVALID) == 0)
1618 ccb->ccb_h.retry_count--;
1620 *action_string = "Retries exhausted";
1621 goto sense_error_done;
1625 if ((err_action & SS_MASK) >= SS_START) {
1627 * Do common portions of commands that
1628 * use recovery CCBs.
1630 orig_ccb = xpt_alloc_ccb_nowait();
1631 if (orig_ccb == NULL) {
1632 *action_string = "Can't allocate recovery CCB";
1633 goto sense_error_done;
1636 * Clear freeze flag for original request here, as
1637 * this freeze will be dropped as part of ERESTART.
1639 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1640 bcopy(ccb, orig_ccb, sizeof(*orig_ccb));
1643 switch (err_action & SS_MASK) {
1645 *action_string = "No recovery action needed";
1649 *action_string = "Retrying command (per sense data)";
1653 *action_string = "Unretryable error";
1660 * Send a start unit command to the device, and
1661 * then retry the command.
1663 *action_string = "Attempting to start unit";
1664 periph->flags |= CAM_PERIPH_RECOVERY_INPROG;
1667 * Check for removable media and set
1668 * load/eject flag appropriately.
1670 if (SID_IS_REMOVABLE(&cgd.inq_data))
1675 scsi_start_stop(&ccb->csio,
1689 * Send a Test Unit Ready to the device.
1690 * If the 'many' flag is set, we send 120
1691 * test unit ready commands, one every half
1692 * second. Otherwise, we just send one TUR.
1693 * We only want to do this if the retry
1694 * count has not been exhausted.
1698 if ((err_action & SSQ_MANY) != 0) {
1699 *action_string = "Polling device for readiness";
1702 *action_string = "Testing device for readiness";
1705 periph->flags |= CAM_PERIPH_RECOVERY_INPROG;
1706 scsi_test_unit_ready(&ccb->csio,
1714 * Accomplish our 500ms delay by deferring
1715 * the release of our device queue appropriately.
1717 *relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1722 panic("Unhandled error action %x", err_action);
1725 if ((err_action & SS_MASK) >= SS_START) {
1727 * Drop the priority, so that the recovery
1728 * CCB is the first to execute. Freeze the queue
1729 * after this command is sent so that we can
1730 * restore the old csio and have it queued in
1731 * the proper order before we release normal
1732 * transactions to the device.
1734 ccb->ccb_h.pinfo.priority--;
1735 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1736 ccb->ccb_h.saved_ccb_ptr = orig_ccb;
1742 *action = err_action;
1748 * Generic error handler. Peripheral drivers usually filter
1749 * out the errors that they handle in a unique manner, then
1750 * call this function.
1753 cam_periph_error(union ccb *ccb, cam_flags camflags,
1754 u_int32_t sense_flags, union ccb *save_ccb)
1756 struct cam_path *newpath;
1757 union ccb *orig_ccb, *scan_ccb;
1758 struct cam_periph *periph;
1759 const char *action_string;
1761 int frozen, error, openings, devctl_err;
1762 u_int32_t action, relsim_flags, timeout;
1764 action = SSQ_PRINT_SENSE;
1765 periph = xpt_path_periph(ccb->ccb_h.path);
1766 action_string = NULL;
1767 status = ccb->ccb_h.status;
1768 frozen = (status & CAM_DEV_QFRZN) != 0;
1769 status &= CAM_STATUS_MASK;
1770 devctl_err = openings = relsim_flags = timeout = 0;
1773 /* Filter the errors that should be reported via devctl */
1774 switch (ccb->ccb_h.status & CAM_STATUS_MASK) {
1775 case CAM_CMD_TIMEOUT:
1776 case CAM_REQ_ABORTED:
1777 case CAM_REQ_CMP_ERR:
1778 case CAM_REQ_TERMIO:
1779 case CAM_UNREC_HBA_ERROR:
1780 case CAM_DATA_RUN_ERR:
1781 case CAM_SCSI_STATUS_ERROR:
1782 case CAM_ATA_STATUS_ERROR:
1783 case CAM_SMP_STATUS_ERROR:
1793 action &= ~SSQ_PRINT_SENSE;
1795 case CAM_SCSI_STATUS_ERROR:
1796 error = camperiphscsistatuserror(ccb, &orig_ccb,
1797 camflags, sense_flags, &openings, &relsim_flags,
1798 &timeout, &action, &action_string);
1800 case CAM_AUTOSENSE_FAIL:
1801 error = EIO; /* we have to kill the command */
1805 case CAM_MSG_REJECT_REC:
1806 /* XXX Don't know that these are correct */
1809 case CAM_SEL_TIMEOUT:
1810 if ((camflags & CAM_RETRY_SELTO) != 0) {
1811 if (ccb->ccb_h.retry_count > 0 &&
1812 (periph->flags & CAM_PERIPH_INVALID) == 0) {
1813 ccb->ccb_h.retry_count--;
1817 * Wait a bit to give the device
1818 * time to recover before we try again.
1820 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1821 timeout = periph_selto_delay;
1824 action_string = "Retries exhausted";
1827 case CAM_DEV_NOT_THERE:
1831 case CAM_REQ_INVALID:
1832 case CAM_PATH_INVALID:
1834 case CAM_PROVIDE_FAIL:
1835 case CAM_REQ_TOO_BIG:
1836 case CAM_LUN_INVALID:
1837 case CAM_TID_INVALID:
1838 case CAM_FUNC_NOTAVAIL:
1841 case CAM_SCSI_BUS_RESET:
1844 * Commands that repeatedly timeout and cause these
1845 * kinds of error recovery actions, should return
1846 * CAM_CMD_TIMEOUT, which allows us to safely assume
1847 * that this command was an innocent bystander to
1848 * these events and should be unconditionally
1851 case CAM_REQUEUE_REQ:
1852 /* Unconditional requeue if device is still there */
1853 if (periph->flags & CAM_PERIPH_INVALID) {
1854 action_string = "Periph was invalidated";
1856 } else if (sense_flags & SF_NO_RETRY) {
1858 action_string = "Retry was blocked";
1861 action &= ~SSQ_PRINT_SENSE;
1864 case CAM_RESRC_UNAVAIL:
1865 /* Wait a bit for the resource shortage to abate. */
1866 timeout = periph_noresrc_delay;
1870 /* Wait a bit for the busy condition to abate. */
1871 timeout = periph_busy_delay;
1873 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1875 case CAM_ATA_STATUS_ERROR:
1876 case CAM_REQ_CMP_ERR:
1877 case CAM_CMD_TIMEOUT:
1878 case CAM_UNEXP_BUSFREE:
1879 case CAM_UNCOR_PARITY:
1880 case CAM_DATA_RUN_ERR:
1882 if (periph->flags & CAM_PERIPH_INVALID) {
1884 action_string = "Periph was invalidated";
1885 } else if (ccb->ccb_h.retry_count == 0) {
1887 action_string = "Retries exhausted";
1888 } else if (sense_flags & SF_NO_RETRY) {
1890 action_string = "Retry was blocked";
1892 ccb->ccb_h.retry_count--;
1898 if ((sense_flags & SF_PRINT_ALWAYS) ||
1899 CAM_DEBUGGED(ccb->ccb_h.path, CAM_DEBUG_INFO))
1900 action |= SSQ_PRINT_SENSE;
1901 else if (sense_flags & SF_NO_PRINT)
1902 action &= ~SSQ_PRINT_SENSE;
1903 if ((action & SSQ_PRINT_SENSE) != 0)
1904 cam_error_print(orig_ccb, CAM_ESF_ALL, CAM_EPF_ALL);
1905 if (error != 0 && (action & SSQ_PRINT_SENSE) != 0) {
1906 if (error != ERESTART) {
1907 if (action_string == NULL)
1908 action_string = "Unretryable error";
1909 xpt_print(ccb->ccb_h.path, "Error %d, %s\n",
1910 error, action_string);
1911 } else if (action_string != NULL)
1912 xpt_print(ccb->ccb_h.path, "%s\n", action_string);
1914 xpt_print(ccb->ccb_h.path, "Retrying command\n");
1917 if (devctl_err && (error != 0 || (action & SSQ_PRINT_SENSE) != 0))
1918 cam_periph_devctl_notify(orig_ccb);
1920 if ((action & SSQ_LOST) != 0) {
1924 * For a selection timeout, we consider all of the LUNs on
1925 * the target to be gone. If the status is CAM_DEV_NOT_THERE,
1926 * then we only get rid of the device(s) specified by the
1927 * path in the original CCB.
1929 if (status == CAM_SEL_TIMEOUT)
1930 lun_id = CAM_LUN_WILDCARD;
1932 lun_id = xpt_path_lun_id(ccb->ccb_h.path);
1934 /* Should we do more if we can't create the path?? */
1935 if (xpt_create_path(&newpath, periph,
1936 xpt_path_path_id(ccb->ccb_h.path),
1937 xpt_path_target_id(ccb->ccb_h.path),
1938 lun_id) == CAM_REQ_CMP) {
1941 * Let peripheral drivers know that this
1942 * device has gone away.
1944 xpt_async(AC_LOST_DEVICE, newpath, NULL);
1945 xpt_free_path(newpath);
1949 /* Broadcast UNIT ATTENTIONs to all periphs. */
1950 if ((action & SSQ_UA) != 0)
1951 xpt_async(AC_UNIT_ATTENTION, orig_ccb->ccb_h.path, orig_ccb);
1953 /* Rescan target on "Reported LUNs data has changed" */
1954 if ((action & SSQ_RESCAN) != 0) {
1955 if (xpt_create_path(&newpath, NULL,
1956 xpt_path_path_id(ccb->ccb_h.path),
1957 xpt_path_target_id(ccb->ccb_h.path),
1958 CAM_LUN_WILDCARD) == CAM_REQ_CMP) {
1960 scan_ccb = xpt_alloc_ccb_nowait();
1961 if (scan_ccb != NULL) {
1962 scan_ccb->ccb_h.path = newpath;
1963 scan_ccb->ccb_h.func_code = XPT_SCAN_TGT;
1964 scan_ccb->crcn.flags = 0;
1965 xpt_rescan(scan_ccb);
1968 "Can't allocate CCB to rescan target\n");
1969 xpt_free_path(newpath);
1974 /* Attempt a retry */
1975 if (error == ERESTART || error == 0) {
1977 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1978 if (error == ERESTART)
1981 cam_release_devq(ccb->ccb_h.path,
1985 /*getcount_only*/0);
1991 #define CAM_PERIPH_DEVD_MSG_SIZE 256
1994 cam_periph_devctl_notify(union ccb *ccb)
1996 struct cam_periph *periph;
1997 struct ccb_getdev *cgd;
1999 int serr, sk, asc, ascq;
2002 sbmsg = malloc(CAM_PERIPH_DEVD_MSG_SIZE, M_CAMPERIPH, M_NOWAIT);
2006 sbuf_new(&sb, sbmsg, CAM_PERIPH_DEVD_MSG_SIZE, SBUF_FIXEDLEN);
2008 periph = xpt_path_periph(ccb->ccb_h.path);
2009 sbuf_printf(&sb, "device=%s%d ", periph->periph_name,
2010 periph->unit_number);
2012 sbuf_printf(&sb, "serial=\"");
2013 if ((cgd = (struct ccb_getdev *)xpt_alloc_ccb_nowait()) != NULL) {
2014 xpt_setup_ccb(&cgd->ccb_h, ccb->ccb_h.path,
2015 CAM_PRIORITY_NORMAL);
2016 cgd->ccb_h.func_code = XPT_GDEV_TYPE;
2017 xpt_action((union ccb *)cgd);
2019 if (cgd->ccb_h.status == CAM_REQ_CMP)
2020 sbuf_bcat(&sb, cgd->serial_num, cgd->serial_num_len);
2021 xpt_free_ccb((union ccb *)cgd);
2023 sbuf_printf(&sb, "\" ");
2024 sbuf_printf(&sb, "cam_status=\"0x%x\" ", ccb->ccb_h.status);
2026 switch (ccb->ccb_h.status & CAM_STATUS_MASK) {
2027 case CAM_CMD_TIMEOUT:
2028 sbuf_printf(&sb, "timeout=%d ", ccb->ccb_h.timeout);
2031 case CAM_SCSI_STATUS_ERROR:
2032 sbuf_printf(&sb, "scsi_status=%d ", ccb->csio.scsi_status);
2033 if (scsi_extract_sense_ccb(ccb, &serr, &sk, &asc, &ascq))
2034 sbuf_printf(&sb, "scsi_sense=\"%02x %02x %02x %02x\" ",
2035 serr, sk, asc, ascq);
2038 case CAM_ATA_STATUS_ERROR:
2039 sbuf_printf(&sb, "RES=\"");
2040 ata_res_sbuf(&ccb->ataio.res, &sb);
2041 sbuf_printf(&sb, "\" ");
2049 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
2050 sbuf_printf(&sb, "CDB=\"");
2051 scsi_cdb_sbuf(scsiio_cdb_ptr(&ccb->csio), &sb);
2052 sbuf_printf(&sb, "\" ");
2053 } else if (ccb->ccb_h.func_code == XPT_ATA_IO) {
2054 sbuf_printf(&sb, "ACB=\"");
2055 ata_cmd_sbuf(&ccb->ataio.cmd, &sb);
2056 sbuf_printf(&sb, "\" ");
2059 if (sbuf_finish(&sb) == 0)
2060 devctl_notify("CAM", "periph", type, sbuf_data(&sb));
2062 free(sbmsg, M_CAMPERIPH);