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
29 * $FreeBSD: src/sys/cam/cam_periph.c,v 1.24.2.3 2003/01/25 19:04:40 dillon Exp $
30 * $DragonFly: src/sys/bus/cam/cam_periph.c,v 1.37 2007/12/01 22:21:17 pavalos Exp $
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/devicestat.h>
43 #include <vm/vm_extern.h>
45 #include <sys/thread2.h>
49 #include "cam_xpt_periph.h"
50 #include "cam_periph.h"
51 #include "cam_debug.h"
53 #include <bus/cam/scsi/scsi_all.h>
54 #include <bus/cam/scsi/scsi_message.h>
55 #include <bus/cam/scsi/scsi_pass.h>
57 static u_int camperiphnextunit(struct periph_driver *p_drv,
58 u_int newunit, int wired,
59 path_id_t pathid, target_id_t target,
61 static u_int camperiphunit(struct periph_driver *p_drv,
62 path_id_t pathid, target_id_t target,
64 static void camperiphdone(struct cam_periph *periph,
66 static void camperiphfree(struct cam_periph *periph);
67 static int camperiphscsistatuserror(union ccb *ccb,
69 u_int32_t sense_flags,
72 u_int32_t *relsim_flags,
74 static int camperiphscsisenseerror(union ccb *ccb,
76 u_int32_t sense_flags,
79 u_int32_t *relsim_flags,
82 static int nperiph_drivers;
83 struct periph_driver **periph_drivers;
85 MALLOC_DEFINE(M_CAMPERIPH, "CAM periph", "CAM peripheral buffers");
88 periphdriver_register(void *data)
90 struct periph_driver **newdrivers, **old;
93 ndrivers = nperiph_drivers + 2;
94 newdrivers = kmalloc(sizeof(*newdrivers) * ndrivers, M_TEMP, M_WAITOK);
96 bcopy(periph_drivers, newdrivers,
97 sizeof(*newdrivers) * nperiph_drivers);
98 newdrivers[nperiph_drivers] = (struct periph_driver *)data;
99 newdrivers[nperiph_drivers + 1] = NULL;
100 old = periph_drivers;
101 periph_drivers = newdrivers;
108 cam_periph_alloc(periph_ctor_t *periph_ctor,
109 periph_oninv_t *periph_oninvalidate,
110 periph_dtor_t *periph_dtor, periph_start_t *periph_start,
111 char *name, cam_periph_type type, struct cam_path *path,
112 ac_callback_t *ac_callback, ac_code code, void *arg)
114 struct periph_driver **p_drv;
115 struct cam_periph *periph;
116 struct cam_periph *cur_periph;
118 target_id_t target_id;
125 * Handle Hot-Plug scenarios. If there is already a peripheral
126 * of our type assigned to this path, we are likely waiting for
127 * final close on an old, invalidated, peripheral. If this is
128 * the case, queue up a deferred call to the peripheral's async
129 * handler. If it looks like a mistaken re-allocation, complain.
131 if ((periph = cam_periph_find(path, name)) != NULL) {
133 if ((periph->flags & CAM_PERIPH_INVALID) != 0
134 && (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) == 0) {
135 periph->flags |= CAM_PERIPH_NEW_DEV_FOUND;
136 periph->deferred_callback = ac_callback;
137 periph->deferred_ac = code;
138 return (CAM_REQ_INPROG);
140 kprintf("cam_periph_alloc: attempt to re-allocate "
141 "valid device %s%d rejected\n",
142 periph->periph_name, periph->unit_number);
144 return (CAM_REQ_INVALID);
147 periph = kmalloc(sizeof(*periph), M_CAMPERIPH, M_INTWAIT | M_ZERO);
151 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
152 if (strcmp((*p_drv)->driver_name, name) == 0)
156 path_id = xpt_path_path_id(path);
157 target_id = xpt_path_target_id(path);
158 lun_id = xpt_path_lun_id(path);
159 cam_init_pinfo(&periph->pinfo);
160 periph->periph_start = periph_start;
161 periph->periph_dtor = periph_dtor;
162 periph->periph_oninval = periph_oninvalidate;
164 periph->periph_name = name;
165 periph->unit_number = camperiphunit(*p_drv, path_id, target_id, lun_id);
166 periph->immediate_priority = CAM_PRIORITY_NONE;
167 periph->refcount = 0;
168 SLIST_INIT(&periph->ccb_list);
169 status = xpt_create_path(&path, periph, path_id, target_id, lun_id);
170 if (status != CAM_REQ_CMP)
176 status = xpt_add_periph(periph);
178 if (status != CAM_REQ_CMP)
182 cur_periph = TAILQ_FIRST(&(*p_drv)->units);
183 while (cur_periph != NULL
184 && cur_periph->unit_number < periph->unit_number)
185 cur_periph = TAILQ_NEXT(cur_periph, unit_links);
187 if (cur_periph != NULL)
188 TAILQ_INSERT_BEFORE(cur_periph, periph, unit_links);
190 TAILQ_INSERT_TAIL(&(*p_drv)->units, periph, unit_links);
191 (*p_drv)->generation++;
198 status = periph_ctor(periph, arg);
200 if (status == CAM_REQ_CMP)
204 switch (init_level) {
206 /* Initialized successfully */
210 TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
212 xpt_remove_periph(periph);
214 xpt_free_path(periph->path);
216 kfree(periph, M_CAMPERIPH);
218 /* No cleanup to perform. */
221 panic("cam_periph_alloc: Unknown init level");
227 * Find a peripheral structure with the specified path, target, lun,
228 * and (optionally) type. If the name is NULL, this function will return
229 * the first peripheral driver that matches the specified path.
232 cam_periph_find(struct cam_path *path, char *name)
234 struct periph_driver **p_drv;
235 struct cam_periph *periph;
237 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
238 if (name != NULL && (strcmp((*p_drv)->driver_name, name) != 0))
242 TAILQ_FOREACH(periph, &(*p_drv)->units, unit_links) {
243 if (xpt_path_comp(periph->path, path) == 0) {
256 cam_periph_acquire(struct cam_periph *periph)
259 return(CAM_REQ_CMP_ERR);
269 cam_periph_release(struct cam_periph *periph)
275 if ((--periph->refcount == 0)
276 && (periph->flags & CAM_PERIPH_INVALID)) {
277 camperiphfree(periph);
283 * Look for the next unit number that is not currently in use for this
284 * peripheral type starting at "newunit". Also exclude unit numbers that
285 * are reserved by for future "hardwiring" unless we already know that this
286 * is a potential wired device. Only assume that the device is "wired" the
287 * first time through the loop since after that we'll be looking at unit
288 * numbers that did not match a wiring entry.
291 camperiphnextunit(struct periph_driver *p_drv, u_int newunit, int wired,
292 path_id_t pathid, target_id_t target, lun_id_t lun)
294 struct cam_periph *periph;
295 char *periph_name, *strval;
300 periph_name = p_drv->driver_name;
303 for (periph = TAILQ_FIRST(&p_drv->units);
304 periph != NULL && periph->unit_number != newunit;
305 periph = TAILQ_NEXT(periph, unit_links))
308 if (periph != NULL && periph->unit_number == newunit) {
310 xpt_print_path(periph->path);
311 kprintf("Duplicate Wired Device entry!\n");
312 xpt_print_path(periph->path);
313 kprintf("Second device (%s device at scbus%d "
314 "target %d lun %d) will not be wired\n",
315 periph_name, pathid, target, lun);
324 * Don't match entries like "da 4" as a wired down
325 * device, but do match entries like "da 4 target 5"
326 * or even "da 4 scbus 1".
329 while ((i = resource_locate(i, periph_name)) != -1) {
330 dname = resource_query_name(i);
331 dunit = resource_query_unit(i);
332 /* if no "target" and no specific scbus, skip */
333 if (resource_int_value(dname, dunit, "target", &val) &&
334 (resource_string_value(dname, dunit, "at",&strval)||
335 strcmp(strval, "scbus") == 0))
337 if (newunit == dunit)
348 camperiphunit(struct periph_driver *p_drv, path_id_t pathid,
349 target_id_t target, lun_id_t lun)
352 int hit, i, val, dunit;
354 char pathbuf[32], *strval, *periph_name;
358 periph_name = p_drv->driver_name;
359 ksnprintf(pathbuf, sizeof(pathbuf), "scbus%d", pathid);
361 for (hit = 0; (i = resource_locate(i, periph_name)) != -1; hit = 0) {
362 dname = resource_query_name(i);
363 dunit = resource_query_unit(i);
364 if (resource_string_value(dname, dunit, "at", &strval) == 0) {
365 if (strcmp(strval, pathbuf) != 0)
369 if (resource_int_value(dname, dunit, "target", &val) == 0) {
374 if (resource_int_value(dname, dunit, "lun", &val) == 0) {
386 * Either start from 0 looking for the next unit or from
387 * the unit number given in the resource config. This way,
388 * if we have wildcard matches, we don't return the same
391 unit = camperiphnextunit(p_drv, unit, /*wired*/hit, pathid,
398 cam_periph_invalidate(struct cam_periph *periph)
401 * We only call this routine the first time a peripheral is
402 * invalidated. The oninvalidate() routine is always called in
403 * a critical section.
406 if (((periph->flags & CAM_PERIPH_INVALID) == 0)
407 && (periph->periph_oninval != NULL))
408 periph->periph_oninval(periph);
410 periph->flags |= CAM_PERIPH_INVALID;
411 periph->flags &= ~CAM_PERIPH_NEW_DEV_FOUND;
413 if (periph->refcount == 0)
414 camperiphfree(periph);
415 else if (periph->refcount < 0)
416 kprintf("cam_invalidate_periph: refcount < 0!!\n");
421 camperiphfree(struct cam_periph *periph)
423 struct periph_driver **p_drv;
425 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
426 if (strcmp((*p_drv)->driver_name, periph->periph_name) == 0)
430 if (*p_drv == NULL) {
431 kprintf("camperiphfree: attempt to free "
432 "non-existent periph: %s\n", periph->periph_name);
436 if (periph->periph_dtor != NULL)
437 periph->periph_dtor(periph);
440 TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
441 (*p_drv)->generation++;
444 xpt_remove_periph(periph);
446 if (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) {
450 switch (periph->deferred_ac) {
451 case AC_FOUND_DEVICE:
452 ccb.ccb_h.func_code = XPT_GDEV_TYPE;
453 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/ 1);
457 case AC_PATH_REGISTERED:
458 ccb.ccb_h.func_code = XPT_PATH_INQ;
459 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/ 1);
467 periph->deferred_callback(NULL, periph->deferred_ac,
470 xpt_free_path(periph->path);
471 kfree(periph, M_CAMPERIPH);
475 * Wait interruptibly for an exclusive lock.
478 cam_periph_lock(struct cam_periph *periph, int flags)
483 * Increment the reference count on the peripheral
484 * while we wait for our lock attempt to succeed
485 * to ensure the peripheral doesn't disappear out
486 * from under us while we sleep.
488 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
491 while ((periph->flags & CAM_PERIPH_LOCKED) != 0) {
492 periph->flags |= CAM_PERIPH_LOCK_WANTED;
493 if ((error = tsleep(periph, flags, "caplck", 0)) != 0) {
494 cam_periph_release(periph);
499 periph->flags |= CAM_PERIPH_LOCKED;
504 * Unlock and wake up any waiters.
507 cam_periph_unlock(struct cam_periph *periph)
509 periph->flags &= ~CAM_PERIPH_LOCKED;
510 if ((periph->flags & CAM_PERIPH_LOCK_WANTED) != 0) {
511 periph->flags &= ~CAM_PERIPH_LOCK_WANTED;
515 cam_periph_release(periph);
519 * Map user virtual pointers into kernel virtual address space, so we can
520 * access the memory. This won't work on physical pointers, for now it's
521 * up to the caller to check for that. (XXX KDM -- should we do that here
522 * instead?) This also only works for up to MAXPHYS memory. Since we use
523 * buffers to map stuff in and out, we're limited to the buffer size.
526 cam_periph_mapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
529 buf_cmd_t cmd[CAM_PERIPH_MAXMAPS];
530 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
531 u_int32_t lengths[CAM_PERIPH_MAXMAPS];
532 u_int32_t dirs[CAM_PERIPH_MAXMAPS];
534 switch(ccb->ccb_h.func_code) {
536 if (ccb->cdm.match_buf_len == 0) {
537 kprintf("cam_periph_mapmem: invalid match buffer "
541 if (ccb->cdm.pattern_buf_len > 0) {
542 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
543 lengths[0] = ccb->cdm.pattern_buf_len;
544 dirs[0] = CAM_DIR_OUT;
545 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
546 lengths[1] = ccb->cdm.match_buf_len;
547 dirs[1] = CAM_DIR_IN;
550 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
551 lengths[0] = ccb->cdm.match_buf_len;
552 dirs[0] = CAM_DIR_IN;
557 case XPT_CONT_TARGET_IO:
558 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
561 data_ptrs[0] = &ccb->csio.data_ptr;
562 lengths[0] = ccb->csio.dxfer_len;
563 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
568 break; /* NOTREACHED */
572 * Check the transfer length and permissions first, so we don't
573 * have to unmap any previously mapped buffers.
575 for (i = 0; i < numbufs; i++) {
577 * Its kinda bogus, we need a R+W command. For now the
578 * buffer needs some sort of command. Use BUF_CMD_WRITE
579 * to indicate a write and BUF_CMD_READ to indicate R+W.
581 cmd[i] = BUF_CMD_WRITE;
584 * The userland data pointer passed in may not be page
585 * aligned. vmapbuf() truncates the address to a page
586 * boundary, so if the address isn't page aligned, we'll
587 * need enough space for the given transfer length, plus
588 * whatever extra space is necessary to make it to the page
592 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)) > DFLTPHYS){
593 kprintf("cam_periph_mapmem: attempt to map %lu bytes, "
594 "which is greater than DFLTPHYS(%d)\n",
596 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)),
601 if (dirs[i] & CAM_DIR_OUT) {
602 if (!useracc(*data_ptrs[i], lengths[i],
604 kprintf("cam_periph_mapmem: error, "
605 "address %p, length %lu isn't "
606 "user accessible for READ\n",
607 (void *)*data_ptrs[i],
613 if (dirs[i] & CAM_DIR_IN) {
614 cmd[i] = BUF_CMD_READ;
615 if (!useracc(*data_ptrs[i], lengths[i],
617 kprintf("cam_periph_mapmem: error, "
618 "address %p, length %lu isn't "
619 "user accessible for WRITE\n",
620 (void *)*data_ptrs[i],
629 for (i = 0; i < numbufs; i++) {
633 mapinfo->bp[i] = getpbuf(NULL);
635 /* save the original user pointer */
636 mapinfo->saved_ptrs[i] = *data_ptrs[i];
639 mapinfo->bp[i]->b_cmd = cmd[i];
641 /* map the user buffer into kernel memory */
642 if (vmapbuf(mapinfo->bp[i], *data_ptrs[i], lengths[i]) < 0) {
643 kprintf("cam_periph_mapmem: error, "
644 "address %p, length %lu isn't "
645 "user accessible any more\n",
646 (void *)*data_ptrs[i],
648 for (j = 0; j < i; ++j) {
649 *data_ptrs[j] = mapinfo->saved_ptrs[j];
650 vunmapbuf(mapinfo->bp[j]);
651 relpbuf(mapinfo->bp[j], NULL);
653 mapinfo->num_bufs_used -= i;
657 /* set our pointer to the new mapped area */
658 *data_ptrs[i] = mapinfo->bp[i]->b_data;
660 mapinfo->num_bufs_used++;
667 * Unmap memory segments mapped into kernel virtual address space by
668 * cam_periph_mapmem().
671 cam_periph_unmapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
674 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
676 if (mapinfo->num_bufs_used <= 0) {
677 /* allow ourselves to be swapped once again */
681 switch (ccb->ccb_h.func_code) {
683 numbufs = min(mapinfo->num_bufs_used, 2);
686 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
688 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
689 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
693 case XPT_CONT_TARGET_IO:
694 data_ptrs[0] = &ccb->csio.data_ptr;
695 numbufs = min(mapinfo->num_bufs_used, 1);
698 /* allow ourselves to be swapped once again */
700 break; /* NOTREACHED */
703 for (i = 0; i < numbufs; i++) {
704 /* Set the user's pointer back to the original value */
705 *data_ptrs[i] = mapinfo->saved_ptrs[i];
707 /* unmap the buffer */
708 vunmapbuf(mapinfo->bp[i]);
710 /* release the buffer */
711 relpbuf(mapinfo->bp[i], NULL);
714 /* allow ourselves to be swapped once again */
718 cam_periph_getccb(struct cam_periph *periph, u_int32_t priority)
720 struct ccb_hdr *ccb_h;
722 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdgetccb\n"));
726 while (SLIST_FIRST(&periph->ccb_list) == NULL) {
727 if (periph->immediate_priority > priority)
728 periph->immediate_priority = priority;
729 xpt_schedule(periph, priority);
730 if ((SLIST_FIRST(&periph->ccb_list) != NULL)
731 && (SLIST_FIRST(&periph->ccb_list)->pinfo.priority == priority))
733 tsleep(&periph->ccb_list, 0, "cgticb", 0);
736 ccb_h = SLIST_FIRST(&periph->ccb_list);
737 SLIST_REMOVE_HEAD(&periph->ccb_list, periph_links.sle);
739 return ((union ccb *)ccb_h);
743 cam_periph_ccbwait(union ccb *ccb)
746 if ((ccb->ccb_h.pinfo.index != CAM_UNQUEUED_INDEX)
747 || ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG))
748 tsleep(&ccb->ccb_h.cbfcnp, 0, "cbwait", 0);
753 cam_periph_ioctl(struct cam_periph *periph, int cmd, caddr_t addr,
754 int (*error_routine)(union ccb *ccb,
756 u_int32_t sense_flags))
766 ccb = cam_periph_getccb(periph, /* priority */ 1);
767 xpt_setup_ccb(&ccb->ccb_h,
770 ccb->ccb_h.func_code = XPT_GDEVLIST;
773 * Basically, the point of this is that we go through
774 * getting the list of devices, until we find a passthrough
775 * device. In the current version of the CAM code, the
776 * only way to determine what type of device we're dealing
777 * with is by its name.
781 ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS;
782 while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) {
784 /* we want the next device in the list */
786 if (strncmp(ccb->cgdl.periph_name,
792 if ((ccb->cgdl.status == CAM_GDEVLIST_LAST_DEVICE) &&
794 ccb->cgdl.periph_name[0] = '\0';
795 ccb->cgdl.unit_number = 0;
800 /* copy the result back out */
801 bcopy(ccb, addr, sizeof(union ccb));
803 /* and release the ccb */
804 xpt_release_ccb(ccb);
815 cam_periph_runccb(union ccb *ccb,
816 int (*error_routine)(union ccb *ccb,
818 u_int32_t sense_flags),
819 cam_flags camflags, u_int32_t sense_flags,
827 * If the user has supplied a stats structure, and if we understand
828 * this particular type of ccb, record the transaction start.
830 if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO))
831 devstat_start_transaction(ds);
836 cam_periph_ccbwait(ccb);
837 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
839 else if (error_routine != NULL)
840 error = (*error_routine)(ccb, camflags, sense_flags);
844 } while (error == ERESTART);
846 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
847 cam_release_devq(ccb->ccb_h.path,
851 /* getcount_only */ FALSE);
853 if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO))
854 devstat_end_transaction(ds,
856 ccb->csio.tag_action & 0xf,
857 ((ccb->ccb_h.flags & CAM_DIR_MASK) ==
858 CAM_DIR_NONE) ? DEVSTAT_NO_DATA :
859 (ccb->ccb_h.flags & CAM_DIR_OUT) ?
867 cam_freeze_devq(struct cam_path *path)
869 struct ccb_hdr ccb_h;
871 xpt_setup_ccb(&ccb_h, path, /*priority*/1);
872 ccb_h.func_code = XPT_NOOP;
873 ccb_h.flags = CAM_DEV_QFREEZE;
874 xpt_action((union ccb *)&ccb_h);
878 cam_release_devq(struct cam_path *path, u_int32_t relsim_flags,
879 u_int32_t openings, u_int32_t timeout,
882 struct ccb_relsim crs;
884 xpt_setup_ccb(&crs.ccb_h, path,
886 crs.ccb_h.func_code = XPT_REL_SIMQ;
887 crs.ccb_h.flags = getcount_only ? CAM_DEV_QFREEZE : 0;
888 crs.release_flags = relsim_flags;
889 crs.openings = openings;
890 crs.release_timeout = timeout;
891 xpt_action((union ccb *)&crs);
892 return (crs.qfrozen_cnt);
895 #define saved_ccb_ptr ppriv_ptr0
897 camperiphdone(struct cam_periph *periph, union ccb *done_ccb)
899 union ccb *saved_ccb;
903 struct scsi_start_stop_unit *scsi_cmd;
904 u_int32_t relsim_flags, timeout;
905 u_int32_t qfrozen_cnt;
908 xpt_done_ccb = FALSE;
909 status = done_ccb->ccb_h.status;
910 frozen = (status & CAM_DEV_QFRZN) != 0;
911 sense = (status & CAM_AUTOSNS_VALID) != 0;
912 status &= CAM_STATUS_MASK;
916 saved_ccb = (union ccb *)done_ccb->ccb_h.saved_ccb_ptr;
919 * Unfreeze the queue once if it is already frozen..
922 qfrozen_cnt = cam_release_devq(done_ccb->ccb_h.path,
933 * If we have successfully taken a device from the not
934 * ready to ready state, re-scan the device and re-get
935 * the inquiry information. Many devices (mostly disks)
936 * don't properly report their inquiry information unless
939 * If we manually retrieved sense into a CCB and got
940 * something other than "NO SENSE" send the updated CCB
941 * back to the client via xpt_done() to be processed via
942 * the error recovery code again.
944 if (done_ccb->ccb_h.func_code == XPT_SCSI_IO) {
945 scsi_cmd = (struct scsi_start_stop_unit *)
946 &done_ccb->csio.cdb_io.cdb_bytes;
948 if (scsi_cmd->opcode == START_STOP_UNIT)
949 xpt_async(AC_INQ_CHANGED,
950 done_ccb->ccb_h.path, NULL);
951 if (scsi_cmd->opcode == REQUEST_SENSE) {
954 sense_key = saved_ccb->csio.sense_data.flags;
955 sense_key &= SSD_KEY;
956 if (sense_key != SSD_KEY_NO_SENSE) {
957 saved_ccb->ccb_h.status |=
960 xpt_print_path(saved_ccb->ccb_h.path);
961 kprintf("Recovered Sense\n");
962 scsi_sense_print(&saved_ccb->csio);
963 cam_error_print(saved_ccb, CAM_ESF_ALL,
970 bcopy(done_ccb->ccb_h.saved_ccb_ptr, done_ccb,
973 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
975 if (xpt_done_ccb == FALSE)
976 xpt_action(done_ccb);
980 case CAM_SCSI_STATUS_ERROR:
981 scsi_cmd = (struct scsi_start_stop_unit *)
982 &done_ccb->csio.cdb_io.cdb_bytes;
984 struct ccb_getdev cgd;
985 struct scsi_sense_data *sense;
986 int error_code, sense_key, asc, ascq;
987 scsi_sense_action err_action;
989 sense = &done_ccb->csio.sense_data;
990 scsi_extract_sense(sense, &error_code,
991 &sense_key, &asc, &ascq);
994 * Grab the inquiry data for this device.
996 xpt_setup_ccb(&cgd.ccb_h, done_ccb->ccb_h.path,
998 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
999 xpt_action((union ccb *)&cgd);
1000 err_action = scsi_error_action(&done_ccb->csio,
1004 * If the error is "invalid field in CDB",
1005 * and the load/eject flag is set, turn the
1006 * flag off and try again. This is just in
1007 * case the drive in question barfs on the
1008 * load eject flag. The CAM code should set
1009 * the load/eject flag by default for
1014 * Should we check to see what the specific
1015 * scsi status is?? Or does it not matter
1016 * since we already know that there was an
1017 * error, and we know what the specific
1018 * error code was, and we know what the
1021 if ((scsi_cmd->opcode == START_STOP_UNIT) &&
1022 ((scsi_cmd->how & SSS_LOEJ) != 0) &&
1023 (asc == 0x24) && (ascq == 0x00) &&
1024 (done_ccb->ccb_h.retry_count > 0)) {
1026 scsi_cmd->how &= ~SSS_LOEJ;
1028 xpt_action(done_ccb);
1030 } else if ((done_ccb->ccb_h.retry_count > 1)
1031 && ((err_action & SS_MASK) != SS_FAIL)) {
1034 * In this case, the error recovery
1035 * command failed, but we've got
1036 * some retries left on it. Give
1037 * it another try unless this is an
1038 * unretryable error.
1041 /* set the timeout to .5 sec */
1043 RELSIM_RELEASE_AFTER_TIMEOUT;
1046 xpt_action(done_ccb);
1052 * Perform the final retry with the original
1053 * CCB so that final error processing is
1054 * performed by the owner of the CCB.
1056 bcopy(done_ccb->ccb_h.saved_ccb_ptr,
1057 done_ccb, sizeof(union ccb));
1059 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1061 xpt_action(done_ccb);
1065 * Eh?? The command failed, but we don't
1066 * have any sense. What's up with that?
1067 * Fire the CCB again to return it to the
1070 bcopy(done_ccb->ccb_h.saved_ccb_ptr,
1071 done_ccb, sizeof(union ccb));
1073 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1075 xpt_action(done_ccb);
1080 bcopy(done_ccb->ccb_h.saved_ccb_ptr, done_ccb,
1083 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1085 xpt_action(done_ccb);
1090 /* decrement the retry count */
1092 * XXX This isn't appropriate in all cases. Restructure,
1093 * so that the retry count is only decremented on an
1094 * actual retry. Remeber that the orignal ccb had its
1095 * retry count dropped before entering recovery, so
1096 * doing it again is a bug.
1098 if (done_ccb->ccb_h.retry_count > 0)
1099 done_ccb->ccb_h.retry_count--;
1101 qfrozen_cnt = cam_release_devq(done_ccb->ccb_h.path,
1102 /*relsim_flags*/relsim_flags,
1105 /*getcount_only*/0);
1106 if (xpt_done_ccb == TRUE)
1107 (*done_ccb->ccb_h.cbfcnp)(periph, done_ccb);
1111 * Generic Async Event handler. Peripheral drivers usually
1112 * filter out the events that require personal attention,
1113 * and leave the rest to this function.
1116 cam_periph_async(struct cam_periph *periph, u_int32_t code,
1117 struct cam_path *path, void *arg)
1120 case AC_LOST_DEVICE:
1121 cam_periph_invalidate(periph);
1126 cam_periph_bus_settle(periph, scsi_delay);
1135 cam_periph_bus_settle(struct cam_periph *periph, u_int bus_settle)
1137 struct ccb_getdevstats cgds;
1139 xpt_setup_ccb(&cgds.ccb_h, periph->path, /*priority*/1);
1140 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1141 xpt_action((union ccb *)&cgds);
1142 cam_periph_freeze_after_event(periph, &cgds.last_reset, bus_settle);
1146 cam_periph_freeze_after_event(struct cam_periph *periph,
1147 struct timeval* event_time, u_int duration_ms)
1149 struct timeval delta;
1150 struct timeval duration_tv;
1152 microuptime(&delta);
1153 timevalsub(&delta, event_time);
1154 duration_tv.tv_sec = duration_ms / 1000;
1155 duration_tv.tv_usec = (duration_ms % 1000) * 1000;
1156 if (timevalcmp(&delta, &duration_tv, <)) {
1157 timevalsub(&duration_tv, &delta);
1159 duration_ms = duration_tv.tv_sec * 1000;
1160 duration_ms += duration_tv.tv_usec / 1000;
1161 cam_freeze_devq(periph->path);
1162 cam_release_devq(periph->path,
1163 RELSIM_RELEASE_AFTER_TIMEOUT,
1165 /*timeout*/duration_ms,
1166 /*getcount_only*/0);
1172 camperiphscsistatuserror(union ccb *ccb, cam_flags camflags,
1173 u_int32_t sense_flags, union ccb *save_ccb,
1174 int *openings, u_int32_t *relsim_flags,
1179 switch (ccb->csio.scsi_status) {
1180 case SCSI_STATUS_OK:
1181 case SCSI_STATUS_COND_MET:
1182 case SCSI_STATUS_INTERMED:
1183 case SCSI_STATUS_INTERMED_COND_MET:
1186 case SCSI_STATUS_CMD_TERMINATED:
1187 case SCSI_STATUS_CHECK_COND:
1188 error = camperiphscsisenseerror(ccb,
1196 case SCSI_STATUS_QUEUE_FULL:
1199 struct ccb_getdevstats cgds;
1202 * First off, find out what the current
1203 * transaction counts are.
1205 xpt_setup_ccb(&cgds.ccb_h,
1208 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1209 xpt_action((union ccb *)&cgds);
1212 * If we were the only transaction active, treat
1213 * the QUEUE FULL as if it were a BUSY condition.
1215 if (cgds.dev_active != 0) {
1219 * Reduce the number of openings to
1220 * be 1 less than the amount it took
1221 * to get a queue full bounded by the
1222 * minimum allowed tag count for this
1225 total_openings = cgds.dev_active + cgds.dev_openings;
1226 *openings = cgds.dev_active;
1227 if (*openings < cgds.mintags)
1228 *openings = cgds.mintags;
1229 if (*openings < total_openings)
1230 *relsim_flags = RELSIM_ADJUST_OPENINGS;
1233 * Some devices report queue full for
1234 * temporary resource shortages. For
1235 * this reason, we allow a minimum
1236 * tag count to be entered via a
1237 * quirk entry to prevent the queue
1238 * count on these devices from falling
1239 * to a pessimisticly low value. We
1240 * still wait for the next successful
1241 * completion, however, before queueing
1242 * more transactions to the device.
1244 *relsim_flags = RELSIM_RELEASE_AFTER_CMDCMPLT;
1249 xpt_print_path(ccb->ccb_h.path);
1250 kprintf("Queue Full\n");
1256 case SCSI_STATUS_BUSY:
1258 * Restart the queue after either another
1259 * command completes or a 1 second timeout.
1262 xpt_print_path(ccb->ccb_h.path);
1263 kprintf("Device Busy\n");
1265 if (ccb->ccb_h.retry_count > 0) {
1266 ccb->ccb_h.retry_count--;
1268 *relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT
1269 | RELSIM_RELEASE_AFTER_CMDCMPLT;
1275 case SCSI_STATUS_RESERV_CONFLICT:
1276 xpt_print_path(ccb->ccb_h.path);
1277 kprintf("Reservation Conflict\n");
1281 xpt_print_path(ccb->ccb_h.path);
1282 kprintf("SCSI Status 0x%x\n", ccb->csio.scsi_status);
1290 camperiphscsisenseerror(union ccb *ccb, cam_flags camflags,
1291 u_int32_t sense_flags, union ccb *save_ccb,
1292 int *openings, u_int32_t *relsim_flags,
1295 struct cam_periph *periph;
1298 periph = xpt_path_periph(ccb->ccb_h.path);
1299 if (periph->flags & CAM_PERIPH_RECOVERY_INPROG) {
1302 * If error recovery is already in progress, don't attempt
1303 * to process this error, but requeue it unconditionally
1304 * and attempt to process it once error recovery has
1305 * completed. This failed command is probably related to
1306 * the error that caused the currently active error recovery
1307 * action so our current recovery efforts should also
1308 * address this command. Be aware that the error recovery
1309 * code assumes that only one recovery action is in progress
1310 * on a particular peripheral instance at any given time
1311 * (e.g. only one saved CCB for error recovery) so it is
1312 * imperitive that we don't violate this assumption.
1316 scsi_sense_action err_action;
1317 struct ccb_getdev cgd;
1318 const char *action_string;
1319 union ccb* print_ccb;
1321 /* A description of the error recovery action performed */
1322 action_string = NULL;
1325 * The location of the orignal ccb
1326 * for sense printing purposes.
1331 * Grab the inquiry data for this device.
1333 xpt_setup_ccb(&cgd.ccb_h, ccb->ccb_h.path, /*priority*/ 1);
1334 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1335 xpt_action((union ccb *)&cgd);
1337 if ((ccb->ccb_h.status & CAM_AUTOSNS_VALID) != 0)
1338 err_action = scsi_error_action(&ccb->csio,
1341 else if ((ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)
1342 err_action = SS_REQSENSE;
1344 err_action = SS_RETRY|SSQ_DECREMENT_COUNT|EIO;
1346 error = err_action & SS_ERRMASK;
1349 * If the recovery action will consume a retry,
1350 * make sure we actually have retries available.
1352 if ((err_action & SSQ_DECREMENT_COUNT) != 0) {
1353 if (ccb->ccb_h.retry_count > 0)
1354 ccb->ccb_h.retry_count--;
1356 action_string = "Retries Exhausted";
1357 goto sense_error_done;
1361 if ((err_action & SS_MASK) >= SS_START) {
1363 * Do common portions of commands that
1364 * use recovery CCBs.
1366 if (save_ccb == NULL) {
1367 action_string = "No recovery CCB supplied";
1368 goto sense_error_done;
1370 bcopy(ccb, save_ccb, sizeof(*save_ccb));
1371 print_ccb = save_ccb;
1372 periph->flags |= CAM_PERIPH_RECOVERY_INPROG;
1375 switch (err_action & SS_MASK) {
1377 action_string = "No Recovery Action Needed";
1381 action_string = "Retrying Command (per Sense Data)";
1385 action_string = "Unretryable error";
1392 * Send a start unit command to the device, and
1393 * then retry the command.
1395 action_string = "Attempting to Start Unit";
1398 * Check for removable media and set
1399 * load/eject flag appropriately.
1401 if (SID_IS_REMOVABLE(&cgd.inq_data))
1406 scsi_start_stop(&ccb->csio,
1420 * Send a Test Unit Ready to the device.
1421 * If the 'many' flag is set, we send 120
1422 * test unit ready commands, one every half
1423 * second. Otherwise, we just send one TUR.
1424 * We only want to do this if the retry
1425 * count has not been exhausted.
1429 if ((err_action & SSQ_MANY) != 0) {
1430 action_string = "Polling device for readiness";
1433 action_string = "Testing device for readiness";
1436 scsi_test_unit_ready(&ccb->csio,
1444 * Accomplish our 500ms delay by deferring
1445 * the release of our device queue appropriately.
1447 *relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1454 * Send a Request Sense to the device. We
1455 * assume that we are in a contingent allegiance
1456 * condition so we do not tag this request.
1458 scsi_request_sense(&ccb->csio, /*retries*/1,
1460 &save_ccb->csio.sense_data,
1461 sizeof(save_ccb->csio.sense_data),
1462 CAM_TAG_ACTION_NONE,
1463 /*sense_len*/SSD_FULL_SIZE,
1468 panic("Unhandled error action %x", err_action);
1471 if ((err_action & SS_MASK) >= SS_START) {
1473 * Drop the priority to 0 so that the recovery
1474 * CCB is the first to execute. Freeze the queue
1475 * after this command is sent so that we can
1476 * restore the old csio and have it queued in
1477 * the proper order before we release normal
1478 * transactions to the device.
1480 ccb->ccb_h.pinfo.priority = 0;
1481 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1482 ccb->ccb_h.saved_ccb_ptr = save_ccb;
1487 if ((err_action & SSQ_PRINT_SENSE) != 0
1488 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID) != 0) {
1489 cam_error_print(print_ccb, CAM_ESF_ALL, CAM_EPF_ALL);
1490 xpt_print_path(ccb->ccb_h.path);
1492 scsi_sense_print(&print_ccb->csio);
1493 kprintf("%s\n", action_string);
1500 * Generic error handler. Peripheral drivers usually filter
1501 * out the errors that they handle in a unique mannor, then
1502 * call this function.
1505 cam_periph_error(union ccb *ccb, cam_flags camflags,
1506 u_int32_t sense_flags, union ccb *save_ccb)
1508 const char *action_string;
1511 int error, printed = 0;
1513 u_int32_t relsim_flags;
1516 action_string = NULL;
1517 status = ccb->ccb_h.status;
1518 frozen = (status & CAM_DEV_QFRZN) != 0;
1519 status &= CAM_STATUS_MASK;
1520 openings = relsim_flags = 0;
1526 case CAM_SCSI_STATUS_ERROR:
1527 error = camperiphscsistatuserror(ccb,
1535 case CAM_AUTOSENSE_FAIL:
1536 xpt_print_path(ccb->ccb_h.path);
1537 kprintf("AutoSense Failed\n");
1538 error = EIO; /* we have to kill the command */
1540 case CAM_REQ_CMP_ERR:
1541 if (bootverbose && printed == 0) {
1542 xpt_print_path(ccb->ccb_h.path);
1543 kprintf("Request completed with CAM_REQ_CMP_ERR\n");
1546 case CAM_CMD_TIMEOUT:
1547 if (bootverbose && printed == 0) {
1548 xpt_print_path(ccb->ccb_h.path);
1549 kprintf("Command timed out\n");
1552 case CAM_UNEXP_BUSFREE:
1553 if (bootverbose && printed == 0) {
1554 xpt_print_path(ccb->ccb_h.path);
1555 kprintf("Unexpected Bus Free\n");
1558 case CAM_UNCOR_PARITY:
1559 if (bootverbose && printed == 0) {
1560 xpt_print_path(ccb->ccb_h.path);
1561 kprintf("Uncorrected Parity Error\n");
1564 case CAM_DATA_RUN_ERR:
1565 if (bootverbose && printed == 0) {
1566 xpt_print_path(ccb->ccb_h.path);
1567 kprintf("Data Overrun\n");
1570 error = EIO; /* we have to kill the command */
1571 /* decrement the number of retries */
1572 if (ccb->ccb_h.retry_count > 0) {
1573 ccb->ccb_h.retry_count--;
1576 action_string = "Retries Exausted";
1582 case CAM_MSG_REJECT_REC:
1583 /* XXX Don't know that these are correct */
1586 case CAM_SEL_TIMEOUT:
1588 struct cam_path *newpath;
1590 if ((camflags & CAM_RETRY_SELTO) != 0) {
1591 if (ccb->ccb_h.retry_count > 0) {
1593 ccb->ccb_h.retry_count--;
1595 if (bootverbose && printed == 0) {
1596 xpt_print_path(ccb->ccb_h.path);
1597 kprintf("Selection Timeout\n");
1602 * Wait a second to give the device
1603 * time to recover before we try again.
1605 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1611 /* Should we do more if we can't create the path?? */
1612 if (xpt_create_path(&newpath, xpt_path_periph(ccb->ccb_h.path),
1613 xpt_path_path_id(ccb->ccb_h.path),
1614 xpt_path_target_id(ccb->ccb_h.path),
1615 CAM_LUN_WILDCARD) != CAM_REQ_CMP)
1619 * Let peripheral drivers know that this device has gone
1622 xpt_async(AC_LOST_DEVICE, newpath, NULL);
1623 xpt_free_path(newpath);
1626 case CAM_REQ_INVALID:
1627 case CAM_PATH_INVALID:
1628 case CAM_DEV_NOT_THERE:
1630 case CAM_PROVIDE_FAIL:
1631 case CAM_REQ_TOO_BIG:
1634 case CAM_SCSI_BUS_RESET:
1637 * Commands that repeatedly timeout and cause these
1638 * kinds of error recovery actions, should return
1639 * CAM_CMD_TIMEOUT, which allows us to safely assume
1640 * that this command was an innocent bystander to
1641 * these events and should be unconditionally
1644 if (bootverbose && printed == 0) {
1645 xpt_print_path(ccb->ccb_h.path);
1646 if (status == CAM_BDR_SENT)
1647 kprintf("Bus Device Reset sent\n");
1649 kprintf("Bus Reset issued\n");
1653 case CAM_REQUEUE_REQ:
1654 /* Unconditional requeue */
1656 if (bootverbose && printed == 0) {
1657 xpt_print_path(ccb->ccb_h.path);
1658 kprintf("Request Requeued\n");
1662 case CAM_RESRC_UNAVAIL:
1666 /* decrement the number of retries */
1667 if (ccb->ccb_h.retry_count > 0) {
1668 ccb->ccb_h.retry_count--;
1670 if (bootverbose && printed == 0) {
1671 xpt_print_path(ccb->ccb_h.path);
1672 kprintf("CAM Status 0x%x\n", status);
1677 action_string = "Retries Exhausted";
1682 /* Attempt a retry */
1683 if (error == ERESTART || error == 0) {
1685 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1687 if (error == ERESTART) {
1688 action_string = "Retrying Command";
1693 cam_release_devq(ccb->ccb_h.path,
1697 /*getcount_only*/0);
1701 * If we have an error and are booting verbosely, whine
1702 * *unless* this was a non-retryable selection timeout.
1704 if (error != 0 && bootverbose &&
1705 !(status == CAM_SEL_TIMEOUT && (camflags & CAM_RETRY_SELTO) == 0)) {
1708 if (action_string == NULL)
1709 action_string = "Unretryable Error";
1710 if (error != ERESTART) {
1711 xpt_print_path(ccb->ccb_h.path);
1712 kprintf("error %d\n", error);
1714 xpt_print_path(ccb->ccb_h.path);
1715 kprintf("%s\n", action_string);