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>
43 #include <sys/mutex.h>
46 #include <sys/devicestat.h>
50 #include <vm/vm_extern.h>
53 #include <cam/cam_ccb.h>
54 #include <cam/cam_queue.h>
55 #include <cam/cam_xpt_periph.h>
56 #include <cam/cam_periph.h>
57 #include <cam/cam_debug.h>
58 #include <cam/cam_sim.h>
60 #include <cam/scsi/scsi_all.h>
61 #include <cam/scsi/scsi_message.h>
62 #include <cam/scsi/scsi_pass.h>
64 static u_int camperiphnextunit(struct periph_driver *p_drv,
65 u_int newunit, int wired,
66 path_id_t pathid, target_id_t target,
68 static u_int camperiphunit(struct periph_driver *p_drv,
69 path_id_t pathid, target_id_t target,
71 static void camperiphdone(struct cam_periph *periph,
73 static void camperiphfree(struct cam_periph *periph);
74 static int camperiphscsistatuserror(union ccb *ccb,
77 u_int32_t sense_flags,
79 u_int32_t *relsim_flags,
82 const char **action_string);
83 static int camperiphscsisenseerror(union ccb *ccb,
86 u_int32_t sense_flags,
88 u_int32_t *relsim_flags,
91 const char **action_string);
92 static void cam_periph_devctl_notify(union ccb *ccb);
94 static int nperiph_drivers;
95 static int initialized = 0;
96 struct periph_driver **periph_drivers;
98 static MALLOC_DEFINE(M_CAMPERIPH, "CAM periph", "CAM peripheral buffers");
100 static int periph_selto_delay = 1000;
101 TUNABLE_INT("kern.cam.periph_selto_delay", &periph_selto_delay);
102 static int periph_noresrc_delay = 500;
103 TUNABLE_INT("kern.cam.periph_noresrc_delay", &periph_noresrc_delay);
104 static int periph_busy_delay = 500;
105 TUNABLE_INT("kern.cam.periph_busy_delay", &periph_busy_delay);
109 periphdriver_register(void *data)
111 struct periph_driver *drv = (struct periph_driver *)data;
112 struct periph_driver **newdrivers, **old;
116 ndrivers = nperiph_drivers + 2;
117 newdrivers = malloc(sizeof(*newdrivers) * ndrivers, M_CAMPERIPH,
120 if (ndrivers != nperiph_drivers + 2) {
122 * Lost race against itself; go around.
125 free(newdrivers, M_CAMPERIPH);
129 bcopy(periph_drivers, newdrivers,
130 sizeof(*newdrivers) * nperiph_drivers);
131 newdrivers[nperiph_drivers] = drv;
132 newdrivers[nperiph_drivers + 1] = NULL;
133 old = periph_drivers;
134 periph_drivers = newdrivers;
138 free(old, M_CAMPERIPH);
139 /* If driver marked as early or it is late now, initialize it. */
140 if (((drv->flags & CAM_PERIPH_DRV_EARLY) != 0 && initialized > 0) ||
146 periphdriver_unregister(void *data)
148 struct periph_driver *drv = (struct periph_driver *)data;
151 /* If driver marked as early or it is late now, deinitialize it. */
152 if (((drv->flags & CAM_PERIPH_DRV_EARLY) != 0 && initialized > 0) ||
154 if (drv->deinit == NULL) {
155 printf("CAM periph driver '%s' doesn't have deinit.\n",
159 error = drv->deinit();
165 for (n = 0; n < nperiph_drivers && periph_drivers[n] != drv; n++)
167 KASSERT(n < nperiph_drivers,
168 ("Periph driver '%s' was not registered", drv->driver_name));
169 for (; n + 1 < nperiph_drivers; n++)
170 periph_drivers[n] = periph_drivers[n + 1];
171 periph_drivers[n + 1] = NULL;
178 periphdriver_init(int level)
182 initialized = max(initialized, level);
183 for (i = 0; periph_drivers[i] != NULL; i++) {
184 early = (periph_drivers[i]->flags & CAM_PERIPH_DRV_EARLY) ? 1 : 2;
185 if (early == initialized)
186 (*periph_drivers[i]->init)();
191 cam_periph_alloc(periph_ctor_t *periph_ctor,
192 periph_oninv_t *periph_oninvalidate,
193 periph_dtor_t *periph_dtor, periph_start_t *periph_start,
194 char *name, cam_periph_type type, struct cam_path *path,
195 ac_callback_t *ac_callback, ac_code code, void *arg)
197 struct periph_driver **p_drv;
199 struct cam_periph *periph;
200 struct cam_periph *cur_periph;
202 target_id_t target_id;
209 * Handle Hot-Plug scenarios. If there is already a peripheral
210 * of our type assigned to this path, we are likely waiting for
211 * final close on an old, invalidated, peripheral. If this is
212 * the case, queue up a deferred call to the peripheral's async
213 * handler. If it looks like a mistaken re-allocation, complain.
215 if ((periph = cam_periph_find(path, name)) != NULL) {
217 if ((periph->flags & CAM_PERIPH_INVALID) != 0
218 && (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) == 0) {
219 periph->flags |= CAM_PERIPH_NEW_DEV_FOUND;
220 periph->deferred_callback = ac_callback;
221 periph->deferred_ac = code;
222 return (CAM_REQ_INPROG);
224 printf("cam_periph_alloc: attempt to re-allocate "
225 "valid device %s%d rejected flags %#x "
226 "refcount %d\n", periph->periph_name,
227 periph->unit_number, periph->flags,
230 return (CAM_REQ_INVALID);
233 periph = (struct cam_periph *)malloc(sizeof(*periph), M_CAMPERIPH,
237 return (CAM_RESRC_UNAVAIL);
242 sim = xpt_path_sim(path);
243 path_id = xpt_path_path_id(path);
244 target_id = xpt_path_target_id(path);
245 lun_id = xpt_path_lun_id(path);
246 periph->periph_start = periph_start;
247 periph->periph_dtor = periph_dtor;
248 periph->periph_oninval = periph_oninvalidate;
250 periph->periph_name = name;
251 periph->scheduled_priority = CAM_PRIORITY_NONE;
252 periph->immediate_priority = CAM_PRIORITY_NONE;
253 periph->refcount = 1; /* Dropped by invalidation. */
255 SLIST_INIT(&periph->ccb_list);
256 status = xpt_create_path(&path, periph, path_id, target_id, lun_id);
257 if (status != CAM_REQ_CMP)
262 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
263 if (strcmp((*p_drv)->driver_name, name) == 0)
266 if (*p_drv == NULL) {
267 printf("cam_periph_alloc: invalid periph name '%s'\n", name);
269 xpt_free_path(periph->path);
270 free(periph, M_CAMPERIPH);
271 return (CAM_REQ_INVALID);
273 periph->unit_number = camperiphunit(*p_drv, path_id, target_id, lun_id);
274 cur_periph = TAILQ_FIRST(&(*p_drv)->units);
275 while (cur_periph != NULL
276 && cur_periph->unit_number < periph->unit_number)
277 cur_periph = TAILQ_NEXT(cur_periph, unit_links);
278 if (cur_periph != NULL) {
279 KASSERT(cur_periph->unit_number != periph->unit_number, ("duplicate units on periph list"));
280 TAILQ_INSERT_BEFORE(cur_periph, periph, unit_links);
282 TAILQ_INSERT_TAIL(&(*p_drv)->units, periph, unit_links);
283 (*p_drv)->generation++;
289 status = xpt_add_periph(periph);
290 if (status != CAM_REQ_CMP)
294 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph created\n"));
296 status = periph_ctor(periph, arg);
298 if (status == CAM_REQ_CMP)
302 switch (init_level) {
304 /* Initialized successfully */
307 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph destroyed\n"));
308 xpt_remove_periph(periph);
312 TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
314 xpt_free_path(periph->path);
317 free(periph, M_CAMPERIPH);
320 /* No cleanup to perform. */
323 panic("%s: Unknown init level", __func__);
329 * Find a peripheral structure with the specified path, target, lun,
330 * and (optionally) type. If the name is NULL, this function will return
331 * the first peripheral driver that matches the specified path.
334 cam_periph_find(struct cam_path *path, char *name)
336 struct periph_driver **p_drv;
337 struct cam_periph *periph;
340 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
342 if (name != NULL && (strcmp((*p_drv)->driver_name, name) != 0))
345 TAILQ_FOREACH(periph, &(*p_drv)->units, unit_links) {
346 if (xpt_path_comp(periph->path, path) == 0) {
348 cam_periph_assert(periph, MA_OWNED);
362 * Find peripheral driver instances attached to the specified path.
365 cam_periph_list(struct cam_path *path, struct sbuf *sb)
367 struct sbuf local_sb;
368 struct periph_driver **p_drv;
369 struct cam_periph *periph;
375 sbuf_new(&local_sb, NULL, sbuf_alloc_len, SBUF_FIXEDLEN);
378 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
380 TAILQ_FOREACH(periph, &(*p_drv)->units, unit_links) {
381 if (xpt_path_comp(periph->path, path) != 0)
384 if (sbuf_len(&local_sb) != 0)
385 sbuf_cat(&local_sb, ",");
387 sbuf_printf(&local_sb, "%s%d", periph->periph_name,
388 periph->unit_number);
390 if (sbuf_error(&local_sb) == ENOMEM) {
393 sbuf_delete(&local_sb);
400 sbuf_finish(&local_sb);
401 sbuf_cpy(sb, sbuf_data(&local_sb));
402 sbuf_delete(&local_sb);
407 cam_periph_acquire(struct cam_periph *periph)
416 if ((periph->flags & CAM_PERIPH_INVALID) == 0) {
426 cam_periph_doacquire(struct cam_periph *periph)
430 KASSERT(periph->refcount >= 1,
431 ("cam_periph_doacquire() with refcount == %d", periph->refcount));
437 cam_periph_release_locked_buses(struct cam_periph *periph)
440 cam_periph_assert(periph, MA_OWNED);
441 KASSERT(periph->refcount >= 1, ("periph->refcount >= 1"));
442 if (--periph->refcount == 0)
443 camperiphfree(periph);
447 cam_periph_release_locked(struct cam_periph *periph)
454 cam_periph_release_locked_buses(periph);
459 cam_periph_release(struct cam_periph *periph)
466 cam_periph_assert(periph, MA_NOTOWNED);
467 mtx = cam_periph_mtx(periph);
469 cam_periph_release_locked(periph);
474 cam_periph_hold(struct cam_periph *periph, int priority)
479 * Increment the reference count on the peripheral
480 * while we wait for our lock attempt to succeed
481 * to ensure the peripheral doesn't disappear out
482 * from user us while we sleep.
485 if (cam_periph_acquire(periph) != 0)
488 cam_periph_assert(periph, MA_OWNED);
489 while ((periph->flags & CAM_PERIPH_LOCKED) != 0) {
490 periph->flags |= CAM_PERIPH_LOCK_WANTED;
491 if ((error = cam_periph_sleep(periph, periph, priority,
492 "caplck", 0)) != 0) {
493 cam_periph_release_locked(periph);
496 if (periph->flags & CAM_PERIPH_INVALID) {
497 cam_periph_release_locked(periph);
502 periph->flags |= CAM_PERIPH_LOCKED;
507 cam_periph_unhold(struct cam_periph *periph)
510 cam_periph_assert(periph, MA_OWNED);
512 periph->flags &= ~CAM_PERIPH_LOCKED;
513 if ((periph->flags & CAM_PERIPH_LOCK_WANTED) != 0) {
514 periph->flags &= ~CAM_PERIPH_LOCK_WANTED;
518 cam_periph_release_locked(periph);
522 * Look for the next unit number that is not currently in use for this
523 * peripheral type starting at "newunit". Also exclude unit numbers that
524 * are reserved by for future "hardwiring" unless we already know that this
525 * is a potential wired device. Only assume that the device is "wired" the
526 * first time through the loop since after that we'll be looking at unit
527 * numbers that did not match a wiring entry.
530 camperiphnextunit(struct periph_driver *p_drv, u_int newunit, int wired,
531 path_id_t pathid, target_id_t target, lun_id_t lun)
533 struct cam_periph *periph;
535 int i, val, dunit, r;
536 const char *dname, *strval;
538 periph_name = p_drv->driver_name;
541 for (periph = TAILQ_FIRST(&p_drv->units);
542 periph != NULL && periph->unit_number != newunit;
543 periph = TAILQ_NEXT(periph, unit_links))
546 if (periph != NULL && periph->unit_number == newunit) {
548 xpt_print(periph->path, "Duplicate Wired "
550 xpt_print(periph->path, "Second device (%s "
551 "device at scbus%d target %d lun %d) will "
552 "not be wired\n", periph_name, pathid,
562 * Don't match entries like "da 4" as a wired down
563 * device, but do match entries like "da 4 target 5"
564 * or even "da 4 scbus 1".
569 r = resource_find_dev(&i, dname, &dunit, NULL, NULL);
572 /* if no "target" and no specific scbus, skip */
573 if (resource_int_value(dname, dunit, "target", &val) &&
574 (resource_string_value(dname, dunit, "at",&strval)||
575 strcmp(strval, "scbus") == 0))
577 if (newunit == dunit)
587 camperiphunit(struct periph_driver *p_drv, path_id_t pathid,
588 target_id_t target, lun_id_t lun)
591 int wired, i, val, dunit;
592 const char *dname, *strval;
593 char pathbuf[32], *periph_name;
595 periph_name = p_drv->driver_name;
596 snprintf(pathbuf, sizeof(pathbuf), "scbus%d", pathid);
600 for (wired = 0; resource_find_dev(&i, dname, &dunit, NULL, NULL) == 0;
602 if (resource_string_value(dname, dunit, "at", &strval) == 0) {
603 if (strcmp(strval, pathbuf) != 0)
607 if (resource_int_value(dname, dunit, "target", &val) == 0) {
612 if (resource_int_value(dname, dunit, "lun", &val) == 0) {
624 * Either start from 0 looking for the next unit or from
625 * the unit number given in the resource config. This way,
626 * if we have wildcard matches, we don't return the same
629 unit = camperiphnextunit(p_drv, unit, wired, pathid, target, lun);
635 cam_periph_invalidate(struct cam_periph *periph)
638 cam_periph_assert(periph, MA_OWNED);
640 * We only call this routine the first time a peripheral is
643 if ((periph->flags & CAM_PERIPH_INVALID) != 0)
646 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph invalidated\n"));
647 if ((periph->flags & CAM_PERIPH_ANNOUNCED) && !rebooting) {
651 sbuf_new(&sb, buffer, 160, SBUF_FIXEDLEN);
652 xpt_denounce_periph_sbuf(periph, &sb);
656 periph->flags |= CAM_PERIPH_INVALID;
657 periph->flags &= ~CAM_PERIPH_NEW_DEV_FOUND;
658 if (periph->periph_oninval != NULL)
659 periph->periph_oninval(periph);
660 cam_periph_release_locked(periph);
664 camperiphfree(struct cam_periph *periph)
666 struct periph_driver **p_drv;
667 struct periph_driver *drv;
669 cam_periph_assert(periph, MA_OWNED);
670 KASSERT(periph->periph_allocating == 0, ("%s%d: freed while allocating",
671 periph->periph_name, periph->unit_number));
672 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
673 if (strcmp((*p_drv)->driver_name, periph->periph_name) == 0)
676 if (*p_drv == NULL) {
677 printf("camperiphfree: attempt to free non-existant periph\n");
681 * Cache a pointer to the periph_driver structure. If a
682 * periph_driver is added or removed from the array (see
683 * periphdriver_register()) while we drop the toplogy lock
684 * below, p_drv may change. This doesn't protect against this
685 * particular periph_driver going away. That will require full
686 * reference counting in the periph_driver infrastructure.
691 * We need to set this flag before dropping the topology lock, to
692 * let anyone who is traversing the list that this peripheral is
693 * about to be freed, and there will be no more reference count
696 periph->flags |= CAM_PERIPH_FREE;
699 * The peripheral destructor semantics dictate calling with only the
700 * SIM mutex held. Since it might sleep, it should not be called
701 * with the topology lock held.
706 * We need to call the peripheral destructor prior to removing the
707 * peripheral from the list. Otherwise, we risk running into a
708 * scenario where the peripheral unit number may get reused
709 * (because it has been removed from the list), but some resources
710 * used by the peripheral are still hanging around. In particular,
711 * the devfs nodes used by some peripherals like the pass(4) driver
712 * aren't fully cleaned up until the destructor is run. If the
713 * unit number is reused before the devfs instance is fully gone,
716 if (periph->periph_dtor != NULL)
717 periph->periph_dtor(periph);
720 * The peripheral list is protected by the topology lock.
724 TAILQ_REMOVE(&drv->units, periph, unit_links);
727 xpt_remove_periph(periph);
730 if ((periph->flags & CAM_PERIPH_ANNOUNCED) && !rebooting)
731 xpt_print(periph->path, "Periph destroyed\n");
733 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph destroyed\n"));
735 if (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) {
739 switch (periph->deferred_ac) {
740 case AC_FOUND_DEVICE:
741 ccb.ccb_h.func_code = XPT_GDEV_TYPE;
742 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
746 case AC_PATH_REGISTERED:
747 xpt_path_inq(&ccb.cpi, periph->path);
754 periph->deferred_callback(NULL, periph->deferred_ac,
757 xpt_free_path(periph->path);
758 free(periph, M_CAMPERIPH);
763 * Map user virtual pointers into kernel virtual address space, so we can
764 * access the memory. This is now a generic function that centralizes most
765 * of the sanity checks on the data flags, if any.
766 * This also only works for up to MAXPHYS memory. Since we use
767 * buffers to map stuff in and out, we're limited to the buffer size.
770 cam_periph_mapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo,
774 int flags[CAM_PERIPH_MAXMAPS];
775 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
776 u_int32_t lengths[CAM_PERIPH_MAXMAPS];
777 u_int32_t dirs[CAM_PERIPH_MAXMAPS];
780 maxmap = DFLTPHYS; /* traditional default */
781 else if (maxmap > MAXPHYS)
782 maxmap = MAXPHYS; /* for safety */
783 switch(ccb->ccb_h.func_code) {
785 if (ccb->cdm.match_buf_len == 0) {
786 printf("cam_periph_mapmem: invalid match buffer "
790 if (ccb->cdm.pattern_buf_len > 0) {
791 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
792 lengths[0] = ccb->cdm.pattern_buf_len;
793 dirs[0] = CAM_DIR_OUT;
794 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
795 lengths[1] = ccb->cdm.match_buf_len;
796 dirs[1] = CAM_DIR_IN;
799 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
800 lengths[0] = ccb->cdm.match_buf_len;
801 dirs[0] = CAM_DIR_IN;
805 * This request will not go to the hardware, no reason
806 * to be so strict. vmapbuf() is able to map up to MAXPHYS.
811 case XPT_CONT_TARGET_IO:
812 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
814 if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR)
816 data_ptrs[0] = &ccb->csio.data_ptr;
817 lengths[0] = ccb->csio.dxfer_len;
818 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
822 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
824 if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR)
826 data_ptrs[0] = &ccb->ataio.data_ptr;
827 lengths[0] = ccb->ataio.dxfer_len;
828 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
832 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
834 /* Two mappings: one for cmd->data and one for cmd->data->data */
835 data_ptrs[0] = (unsigned char **)&ccb->mmcio.cmd.data;
836 lengths[0] = sizeof(struct mmc_data *);
837 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
838 data_ptrs[1] = (unsigned char **)&ccb->mmcio.cmd.data->data;
839 lengths[1] = ccb->mmcio.cmd.data->len;
840 dirs[1] = ccb->ccb_h.flags & CAM_DIR_MASK;
844 data_ptrs[0] = &ccb->smpio.smp_request;
845 lengths[0] = ccb->smpio.smp_request_len;
846 dirs[0] = CAM_DIR_OUT;
847 data_ptrs[1] = &ccb->smpio.smp_response;
848 lengths[1] = ccb->smpio.smp_response_len;
849 dirs[1] = CAM_DIR_IN;
854 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
856 if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR)
858 data_ptrs[0] = &ccb->nvmeio.data_ptr;
859 lengths[0] = ccb->nvmeio.dxfer_len;
860 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
863 case XPT_DEV_ADVINFO:
864 if (ccb->cdai.bufsiz == 0)
867 data_ptrs[0] = (uint8_t **)&ccb->cdai.buf;
868 lengths[0] = ccb->cdai.bufsiz;
869 dirs[0] = CAM_DIR_IN;
873 * This request will not go to the hardware, no reason
874 * to be so strict. vmapbuf() is able to map up to MAXPHYS.
880 break; /* NOTREACHED */
884 * Check the transfer length and permissions first, so we don't
885 * have to unmap any previously mapped buffers.
887 for (i = 0; i < numbufs; i++) {
892 * The userland data pointer passed in may not be page
893 * aligned. vmapbuf() truncates the address to a page
894 * boundary, so if the address isn't page aligned, we'll
895 * need enough space for the given transfer length, plus
896 * whatever extra space is necessary to make it to the page
900 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)) > maxmap){
901 printf("cam_periph_mapmem: attempt to map %lu bytes, "
902 "which is greater than %lu\n",
904 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)),
909 if (dirs[i] & CAM_DIR_OUT) {
910 flags[i] = BIO_WRITE;
913 if (dirs[i] & CAM_DIR_IN) {
920 * This keeps the kernel stack of current thread from getting
921 * swapped. In low-memory situations where the kernel stack might
922 * otherwise get swapped out, this holds it and allows the thread
923 * to make progress and release the kernel mapped pages sooner.
925 * XXX KDM should I use P_NOSWAP instead?
929 for (i = 0; i < numbufs; i++) {
933 mapinfo->bp[i] = getpbuf(NULL);
935 /* put our pointer in the data slot */
936 mapinfo->bp[i]->b_data = *data_ptrs[i];
938 /* save the user's data address */
939 mapinfo->bp[i]->b_caller1 = *data_ptrs[i];
941 /* set the transfer length, we know it's < MAXPHYS */
942 mapinfo->bp[i]->b_bufsize = lengths[i];
944 /* set the direction */
945 mapinfo->bp[i]->b_iocmd = flags[i];
948 * Map the buffer into kernel memory.
950 * Note that useracc() alone is not a sufficient test.
951 * vmapbuf() can still fail due to a smaller file mapped
952 * into a larger area of VM, or if userland races against
953 * vmapbuf() after the useracc() check.
955 if (vmapbuf(mapinfo->bp[i], 1) < 0) {
956 for (j = 0; j < i; ++j) {
957 *data_ptrs[j] = mapinfo->bp[j]->b_caller1;
958 vunmapbuf(mapinfo->bp[j]);
959 relpbuf(mapinfo->bp[j], NULL);
961 relpbuf(mapinfo->bp[i], NULL);
966 /* set our pointer to the new mapped area */
967 *data_ptrs[i] = mapinfo->bp[i]->b_data;
969 mapinfo->num_bufs_used++;
973 * Now that we've gotten this far, change ownership to the kernel
974 * of the buffers so that we don't run afoul of returning to user
975 * space with locks (on the buffer) held.
977 for (i = 0; i < numbufs; i++) {
978 BUF_KERNPROC(mapinfo->bp[i]);
986 * Unmap memory segments mapped into kernel virtual address space by
987 * cam_periph_mapmem().
990 cam_periph_unmapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
993 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
995 if (mapinfo->num_bufs_used <= 0) {
996 /* nothing to free and the process wasn't held. */
1000 switch (ccb->ccb_h.func_code) {
1002 numbufs = min(mapinfo->num_bufs_used, 2);
1005 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
1007 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
1008 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
1012 case XPT_CONT_TARGET_IO:
1013 data_ptrs[0] = &ccb->csio.data_ptr;
1014 numbufs = min(mapinfo->num_bufs_used, 1);
1017 data_ptrs[0] = &ccb->ataio.data_ptr;
1018 numbufs = min(mapinfo->num_bufs_used, 1);
1021 numbufs = min(mapinfo->num_bufs_used, 2);
1022 data_ptrs[0] = &ccb->smpio.smp_request;
1023 data_ptrs[1] = &ccb->smpio.smp_response;
1025 case XPT_DEV_ADVINFO:
1026 numbufs = min(mapinfo->num_bufs_used, 1);
1027 data_ptrs[0] = (uint8_t **)&ccb->cdai.buf;
1030 case XPT_NVME_ADMIN:
1031 data_ptrs[0] = &ccb->nvmeio.data_ptr;
1032 numbufs = min(mapinfo->num_bufs_used, 1);
1035 /* allow ourselves to be swapped once again */
1038 break; /* NOTREACHED */
1041 for (i = 0; i < numbufs; i++) {
1042 /* Set the user's pointer back to the original value */
1043 *data_ptrs[i] = mapinfo->bp[i]->b_caller1;
1045 /* unmap the buffer */
1046 vunmapbuf(mapinfo->bp[i]);
1048 /* release the buffer */
1049 relpbuf(mapinfo->bp[i], NULL);
1052 /* allow ourselves to be swapped once again */
1057 cam_periph_ioctl(struct cam_periph *periph, u_long cmd, caddr_t addr,
1058 int (*error_routine)(union ccb *ccb,
1060 u_int32_t sense_flags))
1069 case CAMGETPASSTHRU:
1070 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1071 xpt_setup_ccb(&ccb->ccb_h,
1073 CAM_PRIORITY_NORMAL);
1074 ccb->ccb_h.func_code = XPT_GDEVLIST;
1077 * Basically, the point of this is that we go through
1078 * getting the list of devices, until we find a passthrough
1079 * device. In the current version of the CAM code, the
1080 * only way to determine what type of device we're dealing
1081 * with is by its name.
1083 while (found == 0) {
1084 ccb->cgdl.index = 0;
1085 ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS;
1086 while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) {
1088 /* we want the next device in the list */
1090 if (strncmp(ccb->cgdl.periph_name,
1096 if ((ccb->cgdl.status == CAM_GDEVLIST_LAST_DEVICE) &&
1098 ccb->cgdl.periph_name[0] = '\0';
1099 ccb->cgdl.unit_number = 0;
1104 /* copy the result back out */
1105 bcopy(ccb, addr, sizeof(union ccb));
1107 /* and release the ccb */
1108 xpt_release_ccb(ccb);
1119 cam_periph_done_panic(struct cam_periph *periph, union ccb *done_ccb)
1122 panic("%s: already done with ccb %p", __func__, done_ccb);
1126 cam_periph_done(struct cam_periph *periph, union ccb *done_ccb)
1129 /* Caller will release the CCB */
1130 xpt_path_assert(done_ccb->ccb_h.path, MA_OWNED);
1131 done_ccb->ccb_h.cbfcnp = cam_periph_done_panic;
1132 wakeup(&done_ccb->ccb_h.cbfcnp);
1136 cam_periph_ccbwait(union ccb *ccb)
1139 if ((ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
1140 while (ccb->ccb_h.cbfcnp != cam_periph_done_panic)
1141 xpt_path_sleep(ccb->ccb_h.path, &ccb->ccb_h.cbfcnp,
1142 PRIBIO, "cbwait", 0);
1144 KASSERT(ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX &&
1145 (ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG,
1146 ("%s: proceeding with incomplete ccb: ccb=%p, func_code=%#x, "
1147 "status=%#x, index=%d", __func__, ccb, ccb->ccb_h.func_code,
1148 ccb->ccb_h.status, ccb->ccb_h.pinfo.index));
1152 * Dispatch a CCB and wait for it to complete. If the CCB has set a
1153 * callback function (ccb->ccb_h.cbfcnp), it will be overwritten and lost.
1156 cam_periph_runccb(union ccb *ccb,
1157 int (*error_routine)(union ccb *ccb,
1159 u_int32_t sense_flags),
1160 cam_flags camflags, u_int32_t sense_flags,
1163 struct bintime *starttime;
1164 struct bintime ltime;
1167 uint32_t timeout = 1;
1170 xpt_path_assert(ccb->ccb_h.path, MA_OWNED);
1171 KASSERT((ccb->ccb_h.flags & CAM_UNLOCKED) == 0,
1172 ("%s: ccb=%p, func_code=%#x, flags=%#x", __func__, ccb,
1173 ccb->ccb_h.func_code, ccb->ccb_h.flags));
1176 * If the user has supplied a stats structure, and if we understand
1177 * this particular type of ccb, record the transaction start.
1180 (ccb->ccb_h.func_code == XPT_SCSI_IO ||
1181 ccb->ccb_h.func_code == XPT_ATA_IO ||
1182 ccb->ccb_h.func_code == XPT_NVME_IO)) {
1184 binuptime(starttime);
1185 devstat_start_transaction(ds, starttime);
1189 * We must poll the I/O while we're dumping. The scheduler is normally
1190 * stopped for dumping, except when we call doadump from ddb. While the
1191 * scheduler is running in this case, we still need to poll the I/O to
1192 * avoid sleeping waiting for the ccb to complete.
1194 * A panic triggered dump stops the scheduler, any callback from the
1195 * shutdown_post_sync event will run with the scheduler stopped, but
1196 * before we're officially dumping. To avoid hanging in adashutdown
1197 * initiated commands (or other similar situations), we have to test for
1198 * either SCHEDULER_STOPPED() here as well.
1200 * To avoid locking problems, dumping/polling callers must call
1201 * without a periph lock held.
1203 must_poll = dumping || SCHEDULER_STOPPED();
1204 ccb->ccb_h.cbfcnp = cam_periph_done;
1207 * If we're polling, then we need to ensure that we have ample resources
1208 * in the periph. cam_periph_error can reschedule the ccb by calling
1209 * xpt_action and returning ERESTART, so we have to effect the polling
1210 * in the do loop below.
1213 timeout = xpt_poll_setup(ccb);
1217 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1223 xpt_pollwait(ccb, timeout);
1224 timeout = ccb->ccb_h.timeout * 10;
1226 cam_periph_ccbwait(ccb);
1228 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
1230 else if (error_routine != NULL) {
1231 ccb->ccb_h.cbfcnp = cam_periph_done;
1232 error = (*error_routine)(ccb, camflags, sense_flags);
1235 } while (error == ERESTART);
1238 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1239 cam_release_devq(ccb->ccb_h.path,
1240 /* relsim_flags */0,
1243 /* getcount_only */ FALSE);
1244 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1249 devstat_tag_type tag;
1252 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1253 bytes = ccb->csio.dxfer_len - ccb->csio.resid;
1254 tag = (devstat_tag_type)(ccb->csio.tag_action & 0x3);
1255 } else if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1256 bytes = ccb->ataio.dxfer_len - ccb->ataio.resid;
1257 tag = (devstat_tag_type)0;
1258 } else if (ccb->ccb_h.func_code == XPT_NVME_IO) {
1259 bytes = ccb->nvmeio.dxfer_len; /* NB: resid no possible */
1260 tag = (devstat_tag_type)0;
1265 devstat_end_transaction(ds, bytes, tag,
1266 ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE) ?
1267 DEVSTAT_NO_DATA : (ccb->ccb_h.flags & CAM_DIR_OUT) ?
1268 DEVSTAT_WRITE : DEVSTAT_READ, NULL, starttime);
1275 cam_freeze_devq(struct cam_path *path)
1277 struct ccb_hdr ccb_h;
1279 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("cam_freeze_devq\n"));
1280 xpt_setup_ccb(&ccb_h, path, /*priority*/1);
1281 ccb_h.func_code = XPT_NOOP;
1282 ccb_h.flags = CAM_DEV_QFREEZE;
1283 xpt_action((union ccb *)&ccb_h);
1287 cam_release_devq(struct cam_path *path, u_int32_t relsim_flags,
1288 u_int32_t openings, u_int32_t arg,
1291 struct ccb_relsim crs;
1293 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("cam_release_devq(%u, %u, %u, %d)\n",
1294 relsim_flags, openings, arg, getcount_only));
1295 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
1296 crs.ccb_h.func_code = XPT_REL_SIMQ;
1297 crs.ccb_h.flags = getcount_only ? CAM_DEV_QFREEZE : 0;
1298 crs.release_flags = relsim_flags;
1299 crs.openings = openings;
1300 crs.release_timeout = arg;
1301 xpt_action((union ccb *)&crs);
1302 return (crs.qfrozen_cnt);
1305 #define saved_ccb_ptr ppriv_ptr0
1307 camperiphdone(struct cam_periph *periph, union ccb *done_ccb)
1309 union ccb *saved_ccb;
1311 struct scsi_start_stop_unit *scsi_cmd;
1312 int error = 0, error_code, sense_key, asc, ascq;
1314 scsi_cmd = (struct scsi_start_stop_unit *)
1315 &done_ccb->csio.cdb_io.cdb_bytes;
1316 status = done_ccb->ccb_h.status;
1318 if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1319 if (scsi_extract_sense_ccb(done_ccb,
1320 &error_code, &sense_key, &asc, &ascq)) {
1322 * If the error is "invalid field in CDB",
1323 * and the load/eject flag is set, turn the
1324 * flag off and try again. This is just in
1325 * case the drive in question barfs on the
1326 * load eject flag. The CAM code should set
1327 * the load/eject flag by default for
1330 if ((scsi_cmd->opcode == START_STOP_UNIT) &&
1331 ((scsi_cmd->how & SSS_LOEJ) != 0) &&
1332 (asc == 0x24) && (ascq == 0x00)) {
1333 scsi_cmd->how &= ~SSS_LOEJ;
1334 if (status & CAM_DEV_QFRZN) {
1335 cam_release_devq(done_ccb->ccb_h.path,
1337 done_ccb->ccb_h.status &=
1340 xpt_action(done_ccb);
1344 error = cam_periph_error(done_ccb, 0,
1345 SF_RETRY_UA | SF_NO_PRINT);
1346 if (error == ERESTART)
1348 if (done_ccb->ccb_h.status & CAM_DEV_QFRZN) {
1349 cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0);
1350 done_ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1354 * If we have successfully taken a device from the not
1355 * ready to ready state, re-scan the device and re-get
1356 * the inquiry information. Many devices (mostly disks)
1357 * don't properly report their inquiry information unless
1360 if (scsi_cmd->opcode == START_STOP_UNIT)
1361 xpt_async(AC_INQ_CHANGED, done_ccb->ccb_h.path, NULL);
1365 * After recovery action(s) completed, return to the original CCB.
1366 * If the recovery CCB has failed, considering its own possible
1367 * retries and recovery, assume we are back in state where we have
1368 * been originally, but without recovery hopes left. In such case,
1369 * after the final attempt below, we cancel any further retries,
1370 * blocking by that also any new recovery attempts for this CCB,
1371 * and the result will be the final one returned to the CCB owher.
1373 saved_ccb = (union ccb *)done_ccb->ccb_h.saved_ccb_ptr;
1374 bcopy(saved_ccb, done_ccb, sizeof(*done_ccb));
1375 xpt_free_ccb(saved_ccb);
1376 if (done_ccb->ccb_h.cbfcnp != camperiphdone)
1377 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1379 done_ccb->ccb_h.retry_count = 0;
1380 xpt_action(done_ccb);
1383 /* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
1384 cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0);
1388 * Generic Async Event handler. Peripheral drivers usually
1389 * filter out the events that require personal attention,
1390 * and leave the rest to this function.
1393 cam_periph_async(struct cam_periph *periph, u_int32_t code,
1394 struct cam_path *path, void *arg)
1397 case AC_LOST_DEVICE:
1398 cam_periph_invalidate(periph);
1406 cam_periph_bus_settle(struct cam_periph *periph, u_int bus_settle)
1408 struct ccb_getdevstats cgds;
1410 xpt_setup_ccb(&cgds.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1411 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1412 xpt_action((union ccb *)&cgds);
1413 cam_periph_freeze_after_event(periph, &cgds.last_reset, bus_settle);
1417 cam_periph_freeze_after_event(struct cam_periph *periph,
1418 struct timeval* event_time, u_int duration_ms)
1420 struct timeval delta;
1421 struct timeval duration_tv;
1423 if (!timevalisset(event_time))
1427 timevalsub(&delta, event_time);
1428 duration_tv.tv_sec = duration_ms / 1000;
1429 duration_tv.tv_usec = (duration_ms % 1000) * 1000;
1430 if (timevalcmp(&delta, &duration_tv, <)) {
1431 timevalsub(&duration_tv, &delta);
1433 duration_ms = duration_tv.tv_sec * 1000;
1434 duration_ms += duration_tv.tv_usec / 1000;
1435 cam_freeze_devq(periph->path);
1436 cam_release_devq(periph->path,
1437 RELSIM_RELEASE_AFTER_TIMEOUT,
1439 /*timeout*/duration_ms,
1440 /*getcount_only*/0);
1446 camperiphscsistatuserror(union ccb *ccb, union ccb **orig_ccb,
1447 cam_flags camflags, u_int32_t sense_flags,
1448 int *openings, u_int32_t *relsim_flags,
1449 u_int32_t *timeout, u_int32_t *action, const char **action_string)
1453 switch (ccb->csio.scsi_status) {
1454 case SCSI_STATUS_OK:
1455 case SCSI_STATUS_COND_MET:
1456 case SCSI_STATUS_INTERMED:
1457 case SCSI_STATUS_INTERMED_COND_MET:
1460 case SCSI_STATUS_CMD_TERMINATED:
1461 case SCSI_STATUS_CHECK_COND:
1462 error = camperiphscsisenseerror(ccb, orig_ccb,
1471 case SCSI_STATUS_QUEUE_FULL:
1474 struct ccb_getdevstats cgds;
1477 * First off, find out what the current
1478 * transaction counts are.
1480 xpt_setup_ccb(&cgds.ccb_h,
1482 CAM_PRIORITY_NORMAL);
1483 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1484 xpt_action((union ccb *)&cgds);
1487 * If we were the only transaction active, treat
1488 * the QUEUE FULL as if it were a BUSY condition.
1490 if (cgds.dev_active != 0) {
1494 * Reduce the number of openings to
1495 * be 1 less than the amount it took
1496 * to get a queue full bounded by the
1497 * minimum allowed tag count for this
1500 total_openings = cgds.dev_active + cgds.dev_openings;
1501 *openings = cgds.dev_active;
1502 if (*openings < cgds.mintags)
1503 *openings = cgds.mintags;
1504 if (*openings < total_openings)
1505 *relsim_flags = RELSIM_ADJUST_OPENINGS;
1508 * Some devices report queue full for
1509 * temporary resource shortages. For
1510 * this reason, we allow a minimum
1511 * tag count to be entered via a
1512 * quirk entry to prevent the queue
1513 * count on these devices from falling
1514 * to a pessimisticly low value. We
1515 * still wait for the next successful
1516 * completion, however, before queueing
1517 * more transactions to the device.
1519 *relsim_flags = RELSIM_RELEASE_AFTER_CMDCMPLT;
1523 *action &= ~SSQ_PRINT_SENSE;
1528 case SCSI_STATUS_BUSY:
1530 * Restart the queue after either another
1531 * command completes or a 1 second timeout.
1533 if ((sense_flags & SF_RETRY_BUSY) != 0 ||
1534 (ccb->ccb_h.retry_count--) > 0) {
1536 *relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT
1537 | RELSIM_RELEASE_AFTER_CMDCMPLT;
1543 case SCSI_STATUS_RESERV_CONFLICT:
1552 camperiphscsisenseerror(union ccb *ccb, union ccb **orig,
1553 cam_flags camflags, u_int32_t sense_flags,
1554 int *openings, u_int32_t *relsim_flags,
1555 u_int32_t *timeout, u_int32_t *action, const char **action_string)
1557 struct cam_periph *periph;
1558 union ccb *orig_ccb = ccb;
1559 int error, recoveryccb;
1561 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
1562 if (ccb->ccb_h.func_code == XPT_SCSI_IO && ccb->csio.bio != NULL)
1563 biotrack(ccb->csio.bio, __func__);
1566 periph = xpt_path_periph(ccb->ccb_h.path);
1567 recoveryccb = (ccb->ccb_h.cbfcnp == camperiphdone);
1568 if ((periph->flags & CAM_PERIPH_RECOVERY_INPROG) && !recoveryccb) {
1570 * If error recovery is already in progress, don't attempt
1571 * to process this error, but requeue it unconditionally
1572 * and attempt to process it once error recovery has
1573 * completed. This failed command is probably related to
1574 * the error that caused the currently active error recovery
1575 * action so our current recovery efforts should also
1576 * address this command. Be aware that the error recovery
1577 * code assumes that only one recovery action is in progress
1578 * on a particular peripheral instance at any given time
1579 * (e.g. only one saved CCB for error recovery) so it is
1580 * imperitive that we don't violate this assumption.
1583 *action &= ~SSQ_PRINT_SENSE;
1585 scsi_sense_action err_action;
1586 struct ccb_getdev cgd;
1589 * Grab the inquiry data for this device.
1591 xpt_setup_ccb(&cgd.ccb_h, ccb->ccb_h.path, CAM_PRIORITY_NORMAL);
1592 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1593 xpt_action((union ccb *)&cgd);
1595 err_action = scsi_error_action(&ccb->csio, &cgd.inq_data,
1597 error = err_action & SS_ERRMASK;
1600 * Do not autostart sequential access devices
1601 * to avoid unexpected tape loading.
1603 if ((err_action & SS_MASK) == SS_START &&
1604 SID_TYPE(&cgd.inq_data) == T_SEQUENTIAL) {
1605 *action_string = "Will not autostart a "
1606 "sequential access device";
1607 goto sense_error_done;
1611 * Avoid recovery recursion if recovery action is the same.
1613 if ((err_action & SS_MASK) >= SS_START && recoveryccb) {
1614 if (((err_action & SS_MASK) == SS_START &&
1615 ccb->csio.cdb_io.cdb_bytes[0] == START_STOP_UNIT) ||
1616 ((err_action & SS_MASK) == SS_TUR &&
1617 (ccb->csio.cdb_io.cdb_bytes[0] == TEST_UNIT_READY))) {
1618 err_action = SS_RETRY|SSQ_DECREMENT_COUNT|EIO;
1619 *relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1625 * If the recovery action will consume a retry,
1626 * make sure we actually have retries available.
1628 if ((err_action & SSQ_DECREMENT_COUNT) != 0) {
1629 if (ccb->ccb_h.retry_count > 0 &&
1630 (periph->flags & CAM_PERIPH_INVALID) == 0)
1631 ccb->ccb_h.retry_count--;
1633 *action_string = "Retries exhausted";
1634 goto sense_error_done;
1638 if ((err_action & SS_MASK) >= SS_START) {
1640 * Do common portions of commands that
1641 * use recovery CCBs.
1643 orig_ccb = xpt_alloc_ccb_nowait();
1644 if (orig_ccb == NULL) {
1645 *action_string = "Can't allocate recovery CCB";
1646 goto sense_error_done;
1649 * Clear freeze flag for original request here, as
1650 * this freeze will be dropped as part of ERESTART.
1652 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1653 bcopy(ccb, orig_ccb, sizeof(*orig_ccb));
1656 switch (err_action & SS_MASK) {
1658 *action_string = "No recovery action needed";
1662 *action_string = "Retrying command (per sense data)";
1666 *action_string = "Unretryable error";
1673 * Send a start unit command to the device, and
1674 * then retry the command.
1676 *action_string = "Attempting to start unit";
1677 periph->flags |= CAM_PERIPH_RECOVERY_INPROG;
1680 * Check for removable media and set
1681 * load/eject flag appropriately.
1683 if (SID_IS_REMOVABLE(&cgd.inq_data))
1688 scsi_start_stop(&ccb->csio,
1702 * Send a Test Unit Ready to the device.
1703 * If the 'many' flag is set, we send 120
1704 * test unit ready commands, one every half
1705 * second. Otherwise, we just send one TUR.
1706 * We only want to do this if the retry
1707 * count has not been exhausted.
1711 if ((err_action & SSQ_MANY) != 0) {
1712 *action_string = "Polling device for readiness";
1715 *action_string = "Testing device for readiness";
1718 periph->flags |= CAM_PERIPH_RECOVERY_INPROG;
1719 scsi_test_unit_ready(&ccb->csio,
1727 * Accomplish our 500ms delay by deferring
1728 * the release of our device queue appropriately.
1730 *relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1735 panic("Unhandled error action %x", err_action);
1738 if ((err_action & SS_MASK) >= SS_START) {
1740 * Drop the priority, so that the recovery
1741 * CCB is the first to execute. Freeze the queue
1742 * after this command is sent so that we can
1743 * restore the old csio and have it queued in
1744 * the proper order before we release normal
1745 * transactions to the device.
1747 ccb->ccb_h.pinfo.priority--;
1748 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1749 ccb->ccb_h.saved_ccb_ptr = orig_ccb;
1755 *action = err_action;
1761 * Generic error handler. Peripheral drivers usually filter
1762 * out the errors that they handle in a unique manner, then
1763 * call this function.
1766 cam_periph_error(union ccb *ccb, cam_flags camflags,
1767 u_int32_t sense_flags)
1769 struct cam_path *newpath;
1770 union ccb *orig_ccb, *scan_ccb;
1771 struct cam_periph *periph;
1772 const char *action_string;
1774 int frozen, error, openings, devctl_err;
1775 u_int32_t action, relsim_flags, timeout;
1777 action = SSQ_PRINT_SENSE;
1778 periph = xpt_path_periph(ccb->ccb_h.path);
1779 action_string = NULL;
1780 status = ccb->ccb_h.status;
1781 frozen = (status & CAM_DEV_QFRZN) != 0;
1782 status &= CAM_STATUS_MASK;
1783 devctl_err = openings = relsim_flags = timeout = 0;
1786 /* Filter the errors that should be reported via devctl */
1787 switch (ccb->ccb_h.status & CAM_STATUS_MASK) {
1788 case CAM_CMD_TIMEOUT:
1789 case CAM_REQ_ABORTED:
1790 case CAM_REQ_CMP_ERR:
1791 case CAM_REQ_TERMIO:
1792 case CAM_UNREC_HBA_ERROR:
1793 case CAM_DATA_RUN_ERR:
1794 case CAM_SCSI_STATUS_ERROR:
1795 case CAM_ATA_STATUS_ERROR:
1796 case CAM_SMP_STATUS_ERROR:
1806 action &= ~SSQ_PRINT_SENSE;
1808 case CAM_SCSI_STATUS_ERROR:
1809 error = camperiphscsistatuserror(ccb, &orig_ccb,
1810 camflags, sense_flags, &openings, &relsim_flags,
1811 &timeout, &action, &action_string);
1813 case CAM_AUTOSENSE_FAIL:
1814 error = EIO; /* we have to kill the command */
1818 case CAM_MSG_REJECT_REC:
1819 /* XXX Don't know that these are correct */
1822 case CAM_SEL_TIMEOUT:
1823 if ((camflags & CAM_RETRY_SELTO) != 0) {
1824 if (ccb->ccb_h.retry_count > 0 &&
1825 (periph->flags & CAM_PERIPH_INVALID) == 0) {
1826 ccb->ccb_h.retry_count--;
1830 * Wait a bit to give the device
1831 * time to recover before we try again.
1833 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1834 timeout = periph_selto_delay;
1837 action_string = "Retries exhausted";
1840 case CAM_DEV_NOT_THERE:
1844 case CAM_REQ_INVALID:
1845 case CAM_PATH_INVALID:
1847 case CAM_PROVIDE_FAIL:
1848 case CAM_REQ_TOO_BIG:
1849 case CAM_LUN_INVALID:
1850 case CAM_TID_INVALID:
1851 case CAM_FUNC_NOTAVAIL:
1854 case CAM_SCSI_BUS_RESET:
1857 * Commands that repeatedly timeout and cause these
1858 * kinds of error recovery actions, should return
1859 * CAM_CMD_TIMEOUT, which allows us to safely assume
1860 * that this command was an innocent bystander to
1861 * these events and should be unconditionally
1864 case CAM_REQUEUE_REQ:
1865 /* Unconditional requeue if device is still there */
1866 if (periph->flags & CAM_PERIPH_INVALID) {
1867 action_string = "Periph was invalidated";
1869 } else if (sense_flags & SF_NO_RETRY) {
1871 action_string = "Retry was blocked";
1874 action &= ~SSQ_PRINT_SENSE;
1877 case CAM_RESRC_UNAVAIL:
1878 /* Wait a bit for the resource shortage to abate. */
1879 timeout = periph_noresrc_delay;
1883 /* Wait a bit for the busy condition to abate. */
1884 timeout = periph_busy_delay;
1886 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1888 case CAM_ATA_STATUS_ERROR:
1889 case CAM_REQ_CMP_ERR:
1890 case CAM_CMD_TIMEOUT:
1891 case CAM_UNEXP_BUSFREE:
1892 case CAM_UNCOR_PARITY:
1893 case CAM_DATA_RUN_ERR:
1895 if (periph->flags & CAM_PERIPH_INVALID) {
1897 action_string = "Periph was invalidated";
1898 } else if (ccb->ccb_h.retry_count == 0) {
1900 action_string = "Retries exhausted";
1901 } else if (sense_flags & SF_NO_RETRY) {
1903 action_string = "Retry was blocked";
1905 ccb->ccb_h.retry_count--;
1911 if ((sense_flags & SF_PRINT_ALWAYS) ||
1912 CAM_DEBUGGED(ccb->ccb_h.path, CAM_DEBUG_INFO))
1913 action |= SSQ_PRINT_SENSE;
1914 else if (sense_flags & SF_NO_PRINT)
1915 action &= ~SSQ_PRINT_SENSE;
1916 if ((action & SSQ_PRINT_SENSE) != 0)
1917 cam_error_print(orig_ccb, CAM_ESF_ALL, CAM_EPF_ALL);
1918 if (error != 0 && (action & SSQ_PRINT_SENSE) != 0) {
1919 if (error != ERESTART) {
1920 if (action_string == NULL)
1921 action_string = "Unretryable error";
1922 xpt_print(ccb->ccb_h.path, "Error %d, %s\n",
1923 error, action_string);
1924 } else if (action_string != NULL)
1925 xpt_print(ccb->ccb_h.path, "%s\n", action_string);
1927 xpt_print(ccb->ccb_h.path,
1928 "Retrying command, %d more tries remain\n",
1929 ccb->ccb_h.retry_count);
1933 if (devctl_err && (error != 0 || (action & SSQ_PRINT_SENSE) != 0))
1934 cam_periph_devctl_notify(orig_ccb);
1936 if ((action & SSQ_LOST) != 0) {
1940 * For a selection timeout, we consider all of the LUNs on
1941 * the target to be gone. If the status is CAM_DEV_NOT_THERE,
1942 * then we only get rid of the device(s) specified by the
1943 * path in the original CCB.
1945 if (status == CAM_SEL_TIMEOUT)
1946 lun_id = CAM_LUN_WILDCARD;
1948 lun_id = xpt_path_lun_id(ccb->ccb_h.path);
1950 /* Should we do more if we can't create the path?? */
1951 if (xpt_create_path(&newpath, periph,
1952 xpt_path_path_id(ccb->ccb_h.path),
1953 xpt_path_target_id(ccb->ccb_h.path),
1954 lun_id) == CAM_REQ_CMP) {
1957 * Let peripheral drivers know that this
1958 * device has gone away.
1960 xpt_async(AC_LOST_DEVICE, newpath, NULL);
1961 xpt_free_path(newpath);
1965 /* Broadcast UNIT ATTENTIONs to all periphs. */
1966 if ((action & SSQ_UA) != 0)
1967 xpt_async(AC_UNIT_ATTENTION, orig_ccb->ccb_h.path, orig_ccb);
1969 /* Rescan target on "Reported LUNs data has changed" */
1970 if ((action & SSQ_RESCAN) != 0) {
1971 if (xpt_create_path(&newpath, NULL,
1972 xpt_path_path_id(ccb->ccb_h.path),
1973 xpt_path_target_id(ccb->ccb_h.path),
1974 CAM_LUN_WILDCARD) == CAM_REQ_CMP) {
1976 scan_ccb = xpt_alloc_ccb_nowait();
1977 if (scan_ccb != NULL) {
1978 scan_ccb->ccb_h.path = newpath;
1979 scan_ccb->ccb_h.func_code = XPT_SCAN_TGT;
1980 scan_ccb->crcn.flags = 0;
1981 xpt_rescan(scan_ccb);
1984 "Can't allocate CCB to rescan target\n");
1985 xpt_free_path(newpath);
1990 /* Attempt a retry */
1991 if (error == ERESTART || error == 0) {
1993 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1994 if (error == ERESTART)
1997 cam_release_devq(ccb->ccb_h.path,
2001 /*getcount_only*/0);
2007 #define CAM_PERIPH_DEVD_MSG_SIZE 256
2010 cam_periph_devctl_notify(union ccb *ccb)
2012 struct cam_periph *periph;
2013 struct ccb_getdev *cgd;
2015 int serr, sk, asc, ascq;
2018 sbmsg = malloc(CAM_PERIPH_DEVD_MSG_SIZE, M_CAMPERIPH, M_NOWAIT);
2022 sbuf_new(&sb, sbmsg, CAM_PERIPH_DEVD_MSG_SIZE, SBUF_FIXEDLEN);
2024 periph = xpt_path_periph(ccb->ccb_h.path);
2025 sbuf_printf(&sb, "device=%s%d ", periph->periph_name,
2026 periph->unit_number);
2028 sbuf_printf(&sb, "serial=\"");
2029 if ((cgd = (struct ccb_getdev *)xpt_alloc_ccb_nowait()) != NULL) {
2030 xpt_setup_ccb(&cgd->ccb_h, ccb->ccb_h.path,
2031 CAM_PRIORITY_NORMAL);
2032 cgd->ccb_h.func_code = XPT_GDEV_TYPE;
2033 xpt_action((union ccb *)cgd);
2035 if (cgd->ccb_h.status == CAM_REQ_CMP)
2036 sbuf_bcat(&sb, cgd->serial_num, cgd->serial_num_len);
2037 xpt_free_ccb((union ccb *)cgd);
2039 sbuf_printf(&sb, "\" ");
2040 sbuf_printf(&sb, "cam_status=\"0x%x\" ", ccb->ccb_h.status);
2042 switch (ccb->ccb_h.status & CAM_STATUS_MASK) {
2043 case CAM_CMD_TIMEOUT:
2044 sbuf_printf(&sb, "timeout=%d ", ccb->ccb_h.timeout);
2047 case CAM_SCSI_STATUS_ERROR:
2048 sbuf_printf(&sb, "scsi_status=%d ", ccb->csio.scsi_status);
2049 if (scsi_extract_sense_ccb(ccb, &serr, &sk, &asc, &ascq))
2050 sbuf_printf(&sb, "scsi_sense=\"%02x %02x %02x %02x\" ",
2051 serr, sk, asc, ascq);
2054 case CAM_ATA_STATUS_ERROR:
2055 sbuf_printf(&sb, "RES=\"");
2056 ata_res_sbuf(&ccb->ataio.res, &sb);
2057 sbuf_printf(&sb, "\" ");
2065 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
2066 sbuf_printf(&sb, "CDB=\"");
2067 scsi_cdb_sbuf(scsiio_cdb_ptr(&ccb->csio), &sb);
2068 sbuf_printf(&sb, "\" ");
2069 } else if (ccb->ccb_h.func_code == XPT_ATA_IO) {
2070 sbuf_printf(&sb, "ACB=\"");
2071 ata_cmd_sbuf(&ccb->ataio.cmd, &sb);
2072 sbuf_printf(&sb, "\" ");
2075 if (sbuf_finish(&sb) == 0)
2076 devctl_notify("CAM", "periph", type, sbuf_data(&sb));
2078 free(sbmsg, M_CAMPERIPH);
2082 * Sysctl to force an invalidation of the drive right now. Can be
2083 * called with CTLFLAG_MPSAFE since we take periph lock.
2086 cam_periph_invalidate_sysctl(SYSCTL_HANDLER_ARGS)
2088 struct cam_periph *periph;
2093 error = sysctl_handle_int(oidp, &value, 0, req);
2094 if (error != 0 || req->newptr == NULL || value != 1)
2097 cam_periph_lock(periph);
2098 cam_periph_invalidate(periph);
2099 cam_periph_unlock(periph);