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>
38 #include <sys/linker_set.h>
41 #include <sys/mutex.h>
44 #include <sys/devicestat.h>
47 #include <vm/vm_extern.h>
50 #include <cam/cam_ccb.h>
51 #include <cam/cam_xpt_periph.h>
52 #include <cam/cam_periph.h>
53 #include <cam/cam_debug.h>
54 #include <cam/cam_sim.h>
56 #include <cam/scsi/scsi_all.h>
57 #include <cam/scsi/scsi_message.h>
58 #include <cam/scsi/scsi_pass.h>
60 static u_int camperiphnextunit(struct periph_driver *p_drv,
61 u_int newunit, int wired,
62 path_id_t pathid, target_id_t target,
64 static u_int camperiphunit(struct periph_driver *p_drv,
65 path_id_t pathid, target_id_t target,
67 static void camperiphdone(struct cam_periph *periph,
69 static void camperiphfree(struct cam_periph *periph);
70 static int camperiphscsistatuserror(union ccb *ccb,
72 u_int32_t sense_flags,
75 u_int32_t *relsim_flags,
77 static int camperiphscsisenseerror(union ccb *ccb,
79 u_int32_t sense_flags,
82 u_int32_t *relsim_flags,
85 static int nperiph_drivers;
86 struct periph_driver **periph_drivers;
88 MALLOC_DEFINE(M_CAMPERIPH, "CAM periph", "CAM peripheral buffers");
90 static int periph_selto_delay = 1000;
91 TUNABLE_INT("kern.cam.periph_selto_delay", &periph_selto_delay);
92 static int periph_noresrc_delay = 500;
93 TUNABLE_INT("kern.cam.periph_noresrc_delay", &periph_noresrc_delay);
94 static int periph_busy_delay = 500;
95 TUNABLE_INT("kern.cam.periph_busy_delay", &periph_busy_delay);
99 periphdriver_register(void *data)
101 struct periph_driver **newdrivers, **old;
104 ndrivers = nperiph_drivers + 2;
105 newdrivers = malloc(sizeof(*newdrivers) * ndrivers, M_CAMPERIPH,
108 bcopy(periph_drivers, newdrivers,
109 sizeof(*newdrivers) * nperiph_drivers);
110 newdrivers[nperiph_drivers] = (struct periph_driver *)data;
111 newdrivers[nperiph_drivers + 1] = NULL;
112 old = periph_drivers;
113 periph_drivers = newdrivers;
115 free(old, M_CAMPERIPH);
120 cam_periph_alloc(periph_ctor_t *periph_ctor,
121 periph_oninv_t *periph_oninvalidate,
122 periph_dtor_t *periph_dtor, periph_start_t *periph_start,
123 char *name, cam_periph_type type, struct cam_path *path,
124 ac_callback_t *ac_callback, ac_code code, void *arg)
126 struct periph_driver **p_drv;
128 struct cam_periph *periph;
129 struct cam_periph *cur_periph;
131 target_id_t target_id;
138 * Handle Hot-Plug scenarios. If there is already a peripheral
139 * of our type assigned to this path, we are likely waiting for
140 * final close on an old, invalidated, peripheral. If this is
141 * the case, queue up a deferred call to the peripheral's async
142 * handler. If it looks like a mistaken re-allocation, complain.
144 if ((periph = cam_periph_find(path, name)) != NULL) {
146 if ((periph->flags & CAM_PERIPH_INVALID) != 0
147 && (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) == 0) {
148 periph->flags |= CAM_PERIPH_NEW_DEV_FOUND;
149 periph->deferred_callback = ac_callback;
150 periph->deferred_ac = code;
151 return (CAM_REQ_INPROG);
153 printf("cam_periph_alloc: attempt to re-allocate "
154 "valid device %s%d rejected\n",
155 periph->periph_name, periph->unit_number);
157 return (CAM_REQ_INVALID);
160 periph = (struct cam_periph *)malloc(sizeof(*periph), M_CAMPERIPH,
164 return (CAM_RESRC_UNAVAIL);
169 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
170 if (strcmp((*p_drv)->driver_name, name) == 0)
174 if (*p_drv == NULL) {
175 printf("cam_periph_alloc: invalid periph name '%s'\n", name);
176 return (CAM_REQ_INVALID);
179 sim = xpt_path_sim(path);
180 path_id = xpt_path_path_id(path);
181 target_id = xpt_path_target_id(path);
182 lun_id = xpt_path_lun_id(path);
183 bzero(periph, sizeof(*periph));
184 cam_init_pinfo(&periph->pinfo);
185 periph->periph_start = periph_start;
186 periph->periph_dtor = periph_dtor;
187 periph->periph_oninval = periph_oninvalidate;
189 periph->periph_name = name;
190 periph->unit_number = camperiphunit(*p_drv, path_id, target_id, lun_id);
191 periph->immediate_priority = CAM_PRIORITY_NONE;
192 periph->refcount = 0;
194 SLIST_INIT(&periph->ccb_list);
195 status = xpt_create_path(&path, periph, path_id, target_id, lun_id);
196 if (status != CAM_REQ_CMP)
202 status = xpt_add_periph(periph);
204 if (status != CAM_REQ_CMP)
207 cur_periph = TAILQ_FIRST(&(*p_drv)->units);
208 while (cur_periph != NULL
209 && cur_periph->unit_number < periph->unit_number)
210 cur_periph = TAILQ_NEXT(cur_periph, unit_links);
212 if (cur_periph != NULL)
213 TAILQ_INSERT_BEFORE(cur_periph, periph, unit_links);
215 TAILQ_INSERT_TAIL(&(*p_drv)->units, periph, unit_links);
216 (*p_drv)->generation++;
221 status = periph_ctor(periph, arg);
223 if (status == CAM_REQ_CMP)
227 switch (init_level) {
229 /* Initialized successfully */
232 TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
233 xpt_remove_periph(periph);
236 xpt_free_path(periph->path);
239 free(periph, M_CAMPERIPH);
242 /* No cleanup to perform. */
245 panic("cam_periph_alloc: Unkown init level");
251 * Find a peripheral structure with the specified path, target, lun,
252 * and (optionally) type. If the name is NULL, this function will return
253 * the first peripheral driver that matches the specified path.
256 cam_periph_find(struct cam_path *path, char *name)
258 struct periph_driver **p_drv;
259 struct cam_periph *periph;
262 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
264 if (name != NULL && (strcmp((*p_drv)->driver_name, name) != 0))
267 TAILQ_FOREACH(periph, &(*p_drv)->units, unit_links) {
268 if (xpt_path_comp(periph->path, path) == 0) {
283 cam_periph_acquire(struct cam_periph *periph)
287 return(CAM_REQ_CMP_ERR);
297 cam_periph_release_locked(struct cam_periph *periph)
304 if ((--periph->refcount == 0)
305 && (periph->flags & CAM_PERIPH_INVALID)) {
306 camperiphfree(periph);
312 cam_periph_release(struct cam_periph *periph)
320 mtx_assert(sim->mtx, MA_NOTOWNED);
322 cam_periph_release_locked(periph);
323 mtx_unlock(sim->mtx);
327 cam_periph_hold(struct cam_periph *periph, int priority)
333 * Increment the reference count on the peripheral
334 * while we wait for our lock attempt to succeed
335 * to ensure the peripheral doesn't disappear out
336 * from user us while we sleep.
339 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
342 mtx = periph->sim->mtx;
343 mtx_assert(mtx, MA_OWNED);
347 while ((periph->flags & CAM_PERIPH_LOCKED) != 0) {
348 periph->flags |= CAM_PERIPH_LOCK_WANTED;
349 if ((error = msleep(periph, mtx, priority, "caplck", 0)) != 0) {
350 cam_periph_release_locked(periph);
355 periph->flags |= CAM_PERIPH_LOCKED;
360 cam_periph_unhold(struct cam_periph *periph)
363 mtx_assert(periph->sim->mtx, MA_OWNED);
365 periph->flags &= ~CAM_PERIPH_LOCKED;
366 if ((periph->flags & CAM_PERIPH_LOCK_WANTED) != 0) {
367 periph->flags &= ~CAM_PERIPH_LOCK_WANTED;
371 cam_periph_release_locked(periph);
375 * Look for the next unit number that is not currently in use for this
376 * peripheral type starting at "newunit". Also exclude unit numbers that
377 * are reserved by for future "hardwiring" unless we already know that this
378 * is a potential wired device. Only assume that the device is "wired" the
379 * first time through the loop since after that we'll be looking at unit
380 * numbers that did not match a wiring entry.
383 camperiphnextunit(struct periph_driver *p_drv, u_int newunit, int wired,
384 path_id_t pathid, target_id_t target, lun_id_t lun)
386 struct cam_periph *periph;
388 int i, val, dunit, r;
389 const char *dname, *strval;
391 periph_name = p_drv->driver_name;
394 for (periph = TAILQ_FIRST(&p_drv->units);
395 periph != NULL && periph->unit_number != newunit;
396 periph = TAILQ_NEXT(periph, unit_links))
399 if (periph != NULL && periph->unit_number == newunit) {
401 xpt_print(periph->path, "Duplicate Wired "
403 xpt_print(periph->path, "Second device (%s "
404 "device at scbus%d target %d lun %d) will "
405 "not be wired\n", periph_name, pathid,
415 * Don't match entries like "da 4" as a wired down
416 * device, but do match entries like "da 4 target 5"
417 * or even "da 4 scbus 1".
422 r = resource_find_dev(&i, dname, &dunit, NULL, NULL);
425 /* if no "target" and no specific scbus, skip */
426 if (resource_int_value(dname, dunit, "target", &val) &&
427 (resource_string_value(dname, dunit, "at",&strval)||
428 strcmp(strval, "scbus") == 0))
430 if (newunit == dunit)
440 camperiphunit(struct periph_driver *p_drv, path_id_t pathid,
441 target_id_t target, lun_id_t lun)
444 int wired, i, val, dunit;
445 const char *dname, *strval;
446 char pathbuf[32], *periph_name;
448 periph_name = p_drv->driver_name;
449 snprintf(pathbuf, sizeof(pathbuf), "scbus%d", pathid);
453 for (wired = 0; resource_find_dev(&i, dname, &dunit, NULL, NULL) == 0;
455 if (resource_string_value(dname, dunit, "at", &strval) == 0) {
456 if (strcmp(strval, pathbuf) != 0)
460 if (resource_int_value(dname, dunit, "target", &val) == 0) {
465 if (resource_int_value(dname, dunit, "lun", &val) == 0) {
477 * Either start from 0 looking for the next unit or from
478 * the unit number given in the resource config. This way,
479 * if we have wildcard matches, we don't return the same
482 unit = camperiphnextunit(p_drv, unit, wired, pathid, target, lun);
488 cam_periph_invalidate(struct cam_periph *periph)
492 * We only call this routine the first time a peripheral is
495 if (((periph->flags & CAM_PERIPH_INVALID) == 0)
496 && (periph->periph_oninval != NULL))
497 periph->periph_oninval(periph);
499 periph->flags |= CAM_PERIPH_INVALID;
500 periph->flags &= ~CAM_PERIPH_NEW_DEV_FOUND;
503 if (periph->refcount == 0)
504 camperiphfree(periph);
505 else if (periph->refcount < 0)
506 printf("cam_invalidate_periph: refcount < 0!!\n");
511 camperiphfree(struct cam_periph *periph)
513 struct periph_driver **p_drv;
515 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
516 if (strcmp((*p_drv)->driver_name, periph->periph_name) == 0)
519 if (*p_drv == NULL) {
520 printf("camperiphfree: attempt to free non-existant periph\n");
524 TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
525 (*p_drv)->generation++;
528 if (periph->periph_dtor != NULL)
529 periph->periph_dtor(periph);
530 xpt_remove_periph(periph);
532 if (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) {
536 switch (periph->deferred_ac) {
537 case AC_FOUND_DEVICE:
538 ccb.ccb_h.func_code = XPT_GDEV_TYPE;
539 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/ 1);
543 case AC_PATH_REGISTERED:
544 ccb.ccb_h.func_code = XPT_PATH_INQ;
545 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/ 1);
553 periph->deferred_callback(NULL, periph->deferred_ac,
556 xpt_free_path(periph->path);
557 free(periph, M_CAMPERIPH);
562 * Map user virtual pointers into kernel virtual address space, so we can
563 * access the memory. This won't work on physical pointers, for now it's
564 * up to the caller to check for that. (XXX KDM -- should we do that here
565 * instead?) This also only works for up to MAXPHYS memory. Since we use
566 * buffers to map stuff in and out, we're limited to the buffer size.
569 cam_periph_mapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
572 int flags[CAM_PERIPH_MAXMAPS];
573 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
574 u_int32_t lengths[CAM_PERIPH_MAXMAPS];
575 u_int32_t dirs[CAM_PERIPH_MAXMAPS];
577 switch(ccb->ccb_h.func_code) {
579 if (ccb->cdm.match_buf_len == 0) {
580 printf("cam_periph_mapmem: invalid match buffer "
584 if (ccb->cdm.pattern_buf_len > 0) {
585 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
586 lengths[0] = ccb->cdm.pattern_buf_len;
587 dirs[0] = CAM_DIR_OUT;
588 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
589 lengths[1] = ccb->cdm.match_buf_len;
590 dirs[1] = CAM_DIR_IN;
593 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
594 lengths[0] = ccb->cdm.match_buf_len;
595 dirs[0] = CAM_DIR_IN;
600 case XPT_CONT_TARGET_IO:
601 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
604 data_ptrs[0] = &ccb->csio.data_ptr;
605 lengths[0] = ccb->csio.dxfer_len;
606 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
611 break; /* NOTREACHED */
615 * Check the transfer length and permissions first, so we don't
616 * have to unmap any previously mapped buffers.
618 for (i = 0; i < numbufs; i++) {
623 * The userland data pointer passed in may not be page
624 * aligned. vmapbuf() truncates the address to a page
625 * boundary, so if the address isn't page aligned, we'll
626 * need enough space for the given transfer length, plus
627 * whatever extra space is necessary to make it to the page
631 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)) > DFLTPHYS){
632 printf("cam_periph_mapmem: attempt to map %lu bytes, "
633 "which is greater than DFLTPHYS(%d)\n",
635 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)),
640 if (dirs[i] & CAM_DIR_OUT) {
641 flags[i] = BIO_WRITE;
644 if (dirs[i] & CAM_DIR_IN) {
650 /* this keeps the current process from getting swapped */
652 * XXX KDM should I use P_NOSWAP instead?
656 for (i = 0; i < numbufs; i++) {
660 mapinfo->bp[i] = getpbuf(NULL);
662 /* save the buffer's data address */
663 mapinfo->bp[i]->b_saveaddr = mapinfo->bp[i]->b_data;
665 /* put our pointer in the data slot */
666 mapinfo->bp[i]->b_data = *data_ptrs[i];
668 /* set the transfer length, we know it's < DFLTPHYS */
669 mapinfo->bp[i]->b_bufsize = lengths[i];
671 /* set the direction */
672 mapinfo->bp[i]->b_iocmd = flags[i];
675 * Map the buffer into kernel memory.
677 * Note that useracc() alone is not a sufficient test.
678 * vmapbuf() can still fail due to a smaller file mapped
679 * into a larger area of VM, or if userland races against
680 * vmapbuf() after the useracc() check.
682 if (vmapbuf(mapinfo->bp[i]) < 0) {
683 for (j = 0; j < i; ++j) {
684 *data_ptrs[j] = mapinfo->bp[j]->b_saveaddr;
685 vunmapbuf(mapinfo->bp[j]);
686 relpbuf(mapinfo->bp[j], NULL);
688 relpbuf(mapinfo->bp[i], NULL);
693 /* set our pointer to the new mapped area */
694 *data_ptrs[i] = mapinfo->bp[i]->b_data;
696 mapinfo->num_bufs_used++;
700 * Now that we've gotten this far, change ownership to the kernel
701 * of the buffers so that we don't run afoul of returning to user
702 * space with locks (on the buffer) held.
704 for (i = 0; i < numbufs; i++) {
705 BUF_KERNPROC(mapinfo->bp[i]);
713 * Unmap memory segments mapped into kernel virtual address space by
714 * cam_periph_mapmem().
717 cam_periph_unmapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
720 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
722 if (mapinfo->num_bufs_used <= 0) {
723 /* allow ourselves to be swapped once again */
728 switch (ccb->ccb_h.func_code) {
730 numbufs = min(mapinfo->num_bufs_used, 2);
733 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
735 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
736 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
740 case XPT_CONT_TARGET_IO:
741 data_ptrs[0] = &ccb->csio.data_ptr;
742 numbufs = min(mapinfo->num_bufs_used, 1);
745 /* allow ourselves to be swapped once again */
748 break; /* NOTREACHED */
751 for (i = 0; i < numbufs; i++) {
752 /* Set the user's pointer back to the original value */
753 *data_ptrs[i] = mapinfo->bp[i]->b_saveaddr;
755 /* unmap the buffer */
756 vunmapbuf(mapinfo->bp[i]);
758 /* release the buffer */
759 relpbuf(mapinfo->bp[i], NULL);
762 /* allow ourselves to be swapped once again */
767 cam_periph_getccb(struct cam_periph *periph, u_int32_t priority)
769 struct ccb_hdr *ccb_h;
772 mtx_assert(periph->sim->mtx, MA_OWNED);
773 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdgetccb\n"));
775 while (SLIST_FIRST(&periph->ccb_list) == NULL) {
776 if (periph->immediate_priority > priority)
777 periph->immediate_priority = priority;
778 xpt_schedule(periph, priority);
779 if ((SLIST_FIRST(&periph->ccb_list) != NULL)
780 && (SLIST_FIRST(&periph->ccb_list)->pinfo.priority == priority))
782 mtx_assert(periph->sim->mtx, MA_OWNED);
783 if (periph->sim->mtx == &Giant)
786 mtx = periph->sim->mtx;
787 msleep(&periph->ccb_list, mtx, PRIBIO, "cgticb", 0);
790 ccb_h = SLIST_FIRST(&periph->ccb_list);
791 SLIST_REMOVE_HEAD(&periph->ccb_list, periph_links.sle);
792 return ((union ccb *)ccb_h);
796 cam_periph_ccbwait(union ccb *ccb)
801 sim = xpt_path_sim(ccb->ccb_h.path);
802 if (sim->mtx == &Giant)
806 if ((ccb->ccb_h.pinfo.index != CAM_UNQUEUED_INDEX)
807 || ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG))
808 msleep(&ccb->ccb_h.cbfcnp, mtx, PRIBIO, "cbwait", 0);
812 cam_periph_ioctl(struct cam_periph *periph, int cmd, caddr_t addr,
813 int (*error_routine)(union ccb *ccb,
815 u_int32_t sense_flags))
825 ccb = cam_periph_getccb(periph, /* priority */ 1);
826 xpt_setup_ccb(&ccb->ccb_h,
829 ccb->ccb_h.func_code = XPT_GDEVLIST;
832 * Basically, the point of this is that we go through
833 * getting the list of devices, until we find a passthrough
834 * device. In the current version of the CAM code, the
835 * only way to determine what type of device we're dealing
836 * with is by its name.
840 ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS;
841 while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) {
843 /* we want the next device in the list */
845 if (strncmp(ccb->cgdl.periph_name,
851 if ((ccb->cgdl.status == CAM_GDEVLIST_LAST_DEVICE) &&
853 ccb->cgdl.periph_name[0] = '\0';
854 ccb->cgdl.unit_number = 0;
859 /* copy the result back out */
860 bcopy(ccb, addr, sizeof(union ccb));
862 /* and release the ccb */
863 xpt_release_ccb(ccb);
874 cam_periph_runccb(union ccb *ccb,
875 int (*error_routine)(union ccb *ccb,
877 u_int32_t sense_flags),
878 cam_flags camflags, u_int32_t sense_flags,
885 sim = xpt_path_sim(ccb->ccb_h.path);
886 mtx_assert(sim->mtx, MA_OWNED);
889 * If the user has supplied a stats structure, and if we understand
890 * this particular type of ccb, record the transaction start.
892 if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO))
893 devstat_start_transaction(ds, NULL);
898 cam_periph_ccbwait(ccb);
899 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
901 else if (error_routine != NULL)
902 error = (*error_routine)(ccb, camflags, sense_flags);
906 } while (error == ERESTART);
908 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
909 cam_release_devq(ccb->ccb_h.path,
913 /* getcount_only */ FALSE);
915 if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO))
916 devstat_end_transaction(ds,
918 ccb->csio.tag_action & 0xf,
919 ((ccb->ccb_h.flags & CAM_DIR_MASK) ==
920 CAM_DIR_NONE) ? DEVSTAT_NO_DATA :
921 (ccb->ccb_h.flags & CAM_DIR_OUT) ?
923 DEVSTAT_READ, NULL, NULL);
929 cam_freeze_devq(struct cam_path *path)
931 struct ccb_hdr ccb_h;
933 xpt_setup_ccb(&ccb_h, path, /*priority*/1);
934 ccb_h.func_code = XPT_NOOP;
935 ccb_h.flags = CAM_DEV_QFREEZE;
936 xpt_action((union ccb *)&ccb_h);
940 cam_release_devq(struct cam_path *path, u_int32_t relsim_flags,
941 u_int32_t openings, u_int32_t timeout,
944 struct ccb_relsim crs;
946 xpt_setup_ccb(&crs.ccb_h, path,
948 crs.ccb_h.func_code = XPT_REL_SIMQ;
949 crs.ccb_h.flags = getcount_only ? CAM_DEV_QFREEZE : 0;
950 crs.release_flags = relsim_flags;
951 crs.openings = openings;
952 crs.release_timeout = timeout;
953 xpt_action((union ccb *)&crs);
954 return (crs.qfrozen_cnt);
957 #define saved_ccb_ptr ppriv_ptr0
959 camperiphdone(struct cam_periph *periph, union ccb *done_ccb)
961 union ccb *saved_ccb;
965 struct scsi_start_stop_unit *scsi_cmd;
966 u_int32_t relsim_flags, timeout;
967 u_int32_t qfrozen_cnt;
970 xpt_done_ccb = FALSE;
971 status = done_ccb->ccb_h.status;
972 frozen = (status & CAM_DEV_QFRZN) != 0;
973 sense = (status & CAM_AUTOSNS_VALID) != 0;
974 status &= CAM_STATUS_MASK;
978 saved_ccb = (union ccb *)done_ccb->ccb_h.saved_ccb_ptr;
981 * Unfreeze the queue once if it is already frozen..
984 qfrozen_cnt = cam_release_devq(done_ccb->ccb_h.path,
995 * If we have successfully taken a device from the not
996 * ready to ready state, re-scan the device and re-get
997 * the inquiry information. Many devices (mostly disks)
998 * don't properly report their inquiry information unless
1001 * If we manually retrieved sense into a CCB and got
1002 * something other than "NO SENSE" send the updated CCB
1003 * back to the client via xpt_done() to be processed via
1004 * the error recovery code again.
1006 if (done_ccb->ccb_h.func_code == XPT_SCSI_IO) {
1007 scsi_cmd = (struct scsi_start_stop_unit *)
1008 &done_ccb->csio.cdb_io.cdb_bytes;
1010 if (scsi_cmd->opcode == START_STOP_UNIT)
1011 xpt_async(AC_INQ_CHANGED,
1012 done_ccb->ccb_h.path, NULL);
1013 if (scsi_cmd->opcode == REQUEST_SENSE) {
1016 sense_key = saved_ccb->csio.sense_data.flags;
1017 sense_key &= SSD_KEY;
1018 if (sense_key != SSD_KEY_NO_SENSE) {
1019 saved_ccb->ccb_h.status |=
1022 xpt_print(saved_ccb->ccb_h.path,
1023 "Recovered Sense\n");
1024 scsi_sense_print(&saved_ccb->csio);
1025 cam_error_print(saved_ccb, CAM_ESF_ALL,
1028 xpt_done_ccb = TRUE;
1032 bcopy(done_ccb->ccb_h.saved_ccb_ptr, done_ccb,
1035 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1037 if (xpt_done_ccb == FALSE)
1038 xpt_action(done_ccb);
1042 case CAM_SCSI_STATUS_ERROR:
1043 scsi_cmd = (struct scsi_start_stop_unit *)
1044 &done_ccb->csio.cdb_io.cdb_bytes;
1046 struct ccb_getdev cgd;
1047 struct scsi_sense_data *sense;
1048 int error_code, sense_key, asc, ascq;
1049 scsi_sense_action err_action;
1051 sense = &done_ccb->csio.sense_data;
1052 scsi_extract_sense(sense, &error_code,
1053 &sense_key, &asc, &ascq);
1056 * Grab the inquiry data for this device.
1058 xpt_setup_ccb(&cgd.ccb_h, done_ccb->ccb_h.path,
1060 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1061 xpt_action((union ccb *)&cgd);
1062 err_action = scsi_error_action(&done_ccb->csio,
1066 * If the error is "invalid field in CDB",
1067 * and the load/eject flag is set, turn the
1068 * flag off and try again. This is just in
1069 * case the drive in question barfs on the
1070 * load eject flag. The CAM code should set
1071 * the load/eject flag by default for
1076 * Should we check to see what the specific
1077 * scsi status is?? Or does it not matter
1078 * since we already know that there was an
1079 * error, and we know what the specific
1080 * error code was, and we know what the
1083 if ((scsi_cmd->opcode == START_STOP_UNIT) &&
1084 ((scsi_cmd->how & SSS_LOEJ) != 0) &&
1085 (asc == 0x24) && (ascq == 0x00) &&
1086 (done_ccb->ccb_h.retry_count > 0)) {
1088 scsi_cmd->how &= ~SSS_LOEJ;
1090 xpt_action(done_ccb);
1092 } else if ((done_ccb->ccb_h.retry_count > 1)
1093 && ((err_action & SS_MASK) != SS_FAIL)) {
1096 * In this case, the error recovery
1097 * command failed, but we've got
1098 * some retries left on it. Give
1099 * it another try unless this is an
1100 * unretryable error.
1103 /* set the timeout to .5 sec */
1105 RELSIM_RELEASE_AFTER_TIMEOUT;
1108 xpt_action(done_ccb);
1114 * Perform the final retry with the original
1115 * CCB so that final error processing is
1116 * performed by the owner of the CCB.
1118 bcopy(done_ccb->ccb_h.saved_ccb_ptr,
1119 done_ccb, sizeof(union ccb));
1121 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1123 xpt_action(done_ccb);
1127 * Eh?? The command failed, but we don't
1128 * have any sense. What's up with that?
1129 * Fire the CCB again to return it to the
1132 bcopy(done_ccb->ccb_h.saved_ccb_ptr,
1133 done_ccb, sizeof(union ccb));
1135 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1137 xpt_action(done_ccb);
1142 bcopy(done_ccb->ccb_h.saved_ccb_ptr, done_ccb,
1145 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1147 xpt_action(done_ccb);
1152 /* decrement the retry count */
1154 * XXX This isn't appropriate in all cases. Restructure,
1155 * so that the retry count is only decremented on an
1156 * actual retry. Remeber that the orignal ccb had its
1157 * retry count dropped before entering recovery, so
1158 * doing it again is a bug.
1160 if (done_ccb->ccb_h.retry_count > 0)
1161 done_ccb->ccb_h.retry_count--;
1163 qfrozen_cnt = cam_release_devq(done_ccb->ccb_h.path,
1164 /*relsim_flags*/relsim_flags,
1167 /*getcount_only*/0);
1168 if (xpt_done_ccb == TRUE)
1169 (*done_ccb->ccb_h.cbfcnp)(periph, done_ccb);
1173 * Generic Async Event handler. Peripheral drivers usually
1174 * filter out the events that require personal attention,
1175 * and leave the rest to this function.
1178 cam_periph_async(struct cam_periph *periph, u_int32_t code,
1179 struct cam_path *path, void *arg)
1182 case AC_LOST_DEVICE:
1183 cam_periph_invalidate(periph);
1188 cam_periph_bus_settle(periph, scsi_delay);
1197 cam_periph_bus_settle(struct cam_periph *periph, u_int bus_settle)
1199 struct ccb_getdevstats cgds;
1201 xpt_setup_ccb(&cgds.ccb_h, periph->path, /*priority*/1);
1202 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1203 xpt_action((union ccb *)&cgds);
1204 cam_periph_freeze_after_event(periph, &cgds.last_reset, bus_settle);
1208 cam_periph_freeze_after_event(struct cam_periph *periph,
1209 struct timeval* event_time, u_int duration_ms)
1211 struct timeval delta;
1212 struct timeval duration_tv;
1215 timevalsub(&delta, event_time);
1216 duration_tv.tv_sec = duration_ms / 1000;
1217 duration_tv.tv_usec = (duration_ms % 1000) * 1000;
1218 if (timevalcmp(&delta, &duration_tv, <)) {
1219 timevalsub(&duration_tv, &delta);
1221 duration_ms = duration_tv.tv_sec * 1000;
1222 duration_ms += duration_tv.tv_usec / 1000;
1223 cam_freeze_devq(periph->path);
1224 cam_release_devq(periph->path,
1225 RELSIM_RELEASE_AFTER_TIMEOUT,
1227 /*timeout*/duration_ms,
1228 /*getcount_only*/0);
1234 camperiphscsistatuserror(union ccb *ccb, cam_flags camflags,
1235 u_int32_t sense_flags, union ccb *save_ccb,
1236 int *openings, u_int32_t *relsim_flags,
1241 switch (ccb->csio.scsi_status) {
1242 case SCSI_STATUS_OK:
1243 case SCSI_STATUS_COND_MET:
1244 case SCSI_STATUS_INTERMED:
1245 case SCSI_STATUS_INTERMED_COND_MET:
1248 case SCSI_STATUS_CMD_TERMINATED:
1249 case SCSI_STATUS_CHECK_COND:
1250 error = camperiphscsisenseerror(ccb,
1258 case SCSI_STATUS_QUEUE_FULL:
1261 struct ccb_getdevstats cgds;
1264 * First off, find out what the current
1265 * transaction counts are.
1267 xpt_setup_ccb(&cgds.ccb_h,
1270 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1271 xpt_action((union ccb *)&cgds);
1274 * If we were the only transaction active, treat
1275 * the QUEUE FULL as if it were a BUSY condition.
1277 if (cgds.dev_active != 0) {
1281 * Reduce the number of openings to
1282 * be 1 less than the amount it took
1283 * to get a queue full bounded by the
1284 * minimum allowed tag count for this
1287 total_openings = cgds.dev_active + cgds.dev_openings;
1288 *openings = cgds.dev_active;
1289 if (*openings < cgds.mintags)
1290 *openings = cgds.mintags;
1291 if (*openings < total_openings)
1292 *relsim_flags = RELSIM_ADJUST_OPENINGS;
1295 * Some devices report queue full for
1296 * temporary resource shortages. For
1297 * this reason, we allow a minimum
1298 * tag count to be entered via a
1299 * quirk entry to prevent the queue
1300 * count on these devices from falling
1301 * to a pessimisticly low value. We
1302 * still wait for the next successful
1303 * completion, however, before queueing
1304 * more transactions to the device.
1306 *relsim_flags = RELSIM_RELEASE_AFTER_CMDCMPLT;
1311 xpt_print(ccb->ccb_h.path, "Queue Full\n");
1317 case SCSI_STATUS_BUSY:
1319 * Restart the queue after either another
1320 * command completes or a 1 second timeout.
1323 xpt_print(ccb->ccb_h.path, "Device Busy\n");
1325 if (ccb->ccb_h.retry_count > 0) {
1326 ccb->ccb_h.retry_count--;
1328 *relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT
1329 | RELSIM_RELEASE_AFTER_CMDCMPLT;
1335 case SCSI_STATUS_RESERV_CONFLICT:
1336 xpt_print(ccb->ccb_h.path, "Reservation Conflict\n");
1340 xpt_print(ccb->ccb_h.path, "SCSI Status 0x%x\n",
1341 ccb->csio.scsi_status);
1349 camperiphscsisenseerror(union ccb *ccb, cam_flags camflags,
1350 u_int32_t sense_flags, union ccb *save_ccb,
1351 int *openings, u_int32_t *relsim_flags,
1354 struct cam_periph *periph;
1357 periph = xpt_path_periph(ccb->ccb_h.path);
1358 if (periph->flags & CAM_PERIPH_RECOVERY_INPROG) {
1361 * If error recovery is already in progress, don't attempt
1362 * to process this error, but requeue it unconditionally
1363 * and attempt to process it once error recovery has
1364 * completed. This failed command is probably related to
1365 * the error that caused the currently active error recovery
1366 * action so our current recovery efforts should also
1367 * address this command. Be aware that the error recovery
1368 * code assumes that only one recovery action is in progress
1369 * on a particular peripheral instance at any given time
1370 * (e.g. only one saved CCB for error recovery) so it is
1371 * imperitive that we don't violate this assumption.
1375 scsi_sense_action err_action;
1376 struct ccb_getdev cgd;
1377 const char *action_string;
1378 union ccb* print_ccb;
1380 /* A description of the error recovery action performed */
1381 action_string = NULL;
1384 * The location of the orignal ccb
1385 * for sense printing purposes.
1390 * Grab the inquiry data for this device.
1392 xpt_setup_ccb(&cgd.ccb_h, ccb->ccb_h.path, /*priority*/ 1);
1393 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1394 xpt_action((union ccb *)&cgd);
1396 if ((ccb->ccb_h.status & CAM_AUTOSNS_VALID) != 0)
1397 err_action = scsi_error_action(&ccb->csio,
1400 else if ((ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)
1401 err_action = SS_REQSENSE;
1403 err_action = SS_RETRY|SSQ_DECREMENT_COUNT|EIO;
1405 error = err_action & SS_ERRMASK;
1408 * If the recovery action will consume a retry,
1409 * make sure we actually have retries available.
1411 if ((err_action & SSQ_DECREMENT_COUNT) != 0) {
1412 if (ccb->ccb_h.retry_count > 0)
1413 ccb->ccb_h.retry_count--;
1415 action_string = "Retries Exhausted";
1416 goto sense_error_done;
1420 if ((err_action & SS_MASK) >= SS_START) {
1422 * Do common portions of commands that
1423 * use recovery CCBs.
1425 if (save_ccb == NULL) {
1426 action_string = "No recovery CCB supplied";
1427 goto sense_error_done;
1429 bcopy(ccb, save_ccb, sizeof(*save_ccb));
1430 print_ccb = save_ccb;
1431 periph->flags |= CAM_PERIPH_RECOVERY_INPROG;
1434 switch (err_action & SS_MASK) {
1436 action_string = "No Recovery Action Needed";
1440 action_string = "Retrying Command (per Sense Data)";
1444 action_string = "Unretryable error";
1451 * Send a start unit command to the device, and
1452 * then retry the command.
1454 action_string = "Attempting to Start Unit";
1457 * Check for removable media and set
1458 * load/eject flag appropriately.
1460 if (SID_IS_REMOVABLE(&cgd.inq_data))
1465 scsi_start_stop(&ccb->csio,
1479 * Send a Test Unit Ready to the device.
1480 * If the 'many' flag is set, we send 120
1481 * test unit ready commands, one every half
1482 * second. Otherwise, we just send one TUR.
1483 * We only want to do this if the retry
1484 * count has not been exhausted.
1488 if ((err_action & SSQ_MANY) != 0) {
1489 action_string = "Polling device for readiness";
1492 action_string = "Testing device for readiness";
1495 scsi_test_unit_ready(&ccb->csio,
1503 * Accomplish our 500ms delay by deferring
1504 * the release of our device queue appropriately.
1506 *relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1513 * Send a Request Sense to the device. We
1514 * assume that we are in a contingent allegiance
1515 * condition so we do not tag this request.
1517 scsi_request_sense(&ccb->csio, /*retries*/1,
1519 &save_ccb->csio.sense_data,
1520 sizeof(save_ccb->csio.sense_data),
1521 CAM_TAG_ACTION_NONE,
1522 /*sense_len*/SSD_FULL_SIZE,
1527 panic("Unhandled error action %x", err_action);
1530 if ((err_action & SS_MASK) >= SS_START) {
1532 * Drop the priority to 0 so that the recovery
1533 * CCB is the first to execute. Freeze the queue
1534 * after this command is sent so that we can
1535 * restore the old csio and have it queued in
1536 * the proper order before we release normal
1537 * transactions to the device.
1539 ccb->ccb_h.pinfo.priority = 0;
1540 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1541 ccb->ccb_h.saved_ccb_ptr = save_ccb;
1546 if ((err_action & SSQ_PRINT_SENSE) != 0
1547 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID) != 0) {
1548 cam_error_print(print_ccb, CAM_ESF_ALL, CAM_EPF_ALL);
1549 xpt_print_path(ccb->ccb_h.path);
1551 scsi_sense_print(&print_ccb->csio);
1552 printf("%s\n", action_string);
1559 * Generic error handler. Peripheral drivers usually filter
1560 * out the errors that they handle in a unique mannor, then
1561 * call this function.
1564 cam_periph_error(union ccb *ccb, cam_flags camflags,
1565 u_int32_t sense_flags, union ccb *save_ccb)
1567 const char *action_string;
1570 int error, printed = 0;
1572 u_int32_t relsim_flags;
1573 u_int32_t timeout = 0;
1575 action_string = NULL;
1576 status = ccb->ccb_h.status;
1577 frozen = (status & CAM_DEV_QFRZN) != 0;
1578 status &= CAM_STATUS_MASK;
1579 openings = relsim_flags = 0;
1585 case CAM_SCSI_STATUS_ERROR:
1586 error = camperiphscsistatuserror(ccb,
1594 case CAM_AUTOSENSE_FAIL:
1595 xpt_print(ccb->ccb_h.path, "AutoSense Failed\n");
1596 error = EIO; /* we have to kill the command */
1598 case CAM_REQ_CMP_ERR:
1599 if (bootverbose && printed == 0) {
1600 xpt_print(ccb->ccb_h.path,
1601 "Request completed with CAM_REQ_CMP_ERR\n");
1605 case CAM_CMD_TIMEOUT:
1606 if (bootverbose && printed == 0) {
1607 xpt_print(ccb->ccb_h.path, "Command timed out\n");
1611 case CAM_UNEXP_BUSFREE:
1612 if (bootverbose && printed == 0) {
1613 xpt_print(ccb->ccb_h.path, "Unexpected Bus Free\n");
1617 case CAM_UNCOR_PARITY:
1618 if (bootverbose && printed == 0) {
1619 xpt_print(ccb->ccb_h.path,
1620 "Uncorrected Parity Error\n");
1624 case CAM_DATA_RUN_ERR:
1625 if (bootverbose && printed == 0) {
1626 xpt_print(ccb->ccb_h.path, "Data Overrun\n");
1629 error = EIO; /* we have to kill the command */
1630 /* decrement the number of retries */
1631 if (ccb->ccb_h.retry_count > 0) {
1632 ccb->ccb_h.retry_count--;
1635 action_string = "Retries Exhausted";
1641 case CAM_MSG_REJECT_REC:
1642 /* XXX Don't know that these are correct */
1645 case CAM_SEL_TIMEOUT:
1647 struct cam_path *newpath;
1649 if ((camflags & CAM_RETRY_SELTO) != 0) {
1650 if (ccb->ccb_h.retry_count > 0) {
1652 ccb->ccb_h.retry_count--;
1654 if (bootverbose && printed == 0) {
1655 xpt_print(ccb->ccb_h.path,
1656 "Selection Timeout\n");
1661 * Wait a bit to give the device
1662 * time to recover before we try again.
1664 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1665 timeout = periph_selto_delay;
1670 /* Should we do more if we can't create the path?? */
1671 if (xpt_create_path(&newpath, xpt_path_periph(ccb->ccb_h.path),
1672 xpt_path_path_id(ccb->ccb_h.path),
1673 xpt_path_target_id(ccb->ccb_h.path),
1674 CAM_LUN_WILDCARD) != CAM_REQ_CMP)
1678 * Let peripheral drivers know that this device has gone
1681 xpt_async(AC_LOST_DEVICE, newpath, NULL);
1682 xpt_free_path(newpath);
1685 case CAM_REQ_INVALID:
1686 case CAM_PATH_INVALID:
1687 case CAM_DEV_NOT_THERE:
1689 case CAM_PROVIDE_FAIL:
1690 case CAM_REQ_TOO_BIG:
1691 case CAM_LUN_INVALID:
1692 case CAM_TID_INVALID:
1695 case CAM_SCSI_BUS_RESET:
1698 * Commands that repeatedly timeout and cause these
1699 * kinds of error recovery actions, should return
1700 * CAM_CMD_TIMEOUT, which allows us to safely assume
1701 * that this command was an innocent bystander to
1702 * these events and should be unconditionally
1705 if (bootverbose && printed == 0) {
1706 xpt_print_path(ccb->ccb_h.path);
1707 if (status == CAM_BDR_SENT)
1708 printf("Bus Device Reset sent\n");
1710 printf("Bus Reset issued\n");
1714 case CAM_REQUEUE_REQ:
1715 /* Unconditional requeue */
1717 if (bootverbose && printed == 0) {
1718 xpt_print(ccb->ccb_h.path, "Request Requeued\n");
1722 case CAM_RESRC_UNAVAIL:
1723 /* Wait a bit for the resource shortage to abate. */
1724 timeout = periph_noresrc_delay;
1728 /* Wait a bit for the busy condition to abate. */
1729 timeout = periph_busy_delay;
1731 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1734 /* decrement the number of retries */
1735 if (ccb->ccb_h.retry_count > 0) {
1736 ccb->ccb_h.retry_count--;
1738 if (bootverbose && printed == 0) {
1739 xpt_print(ccb->ccb_h.path, "CAM Status 0x%x\n",
1745 action_string = "Retries Exhausted";
1750 /* Attempt a retry */
1751 if (error == ERESTART || error == 0) {
1753 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1755 if (error == ERESTART) {
1756 action_string = "Retrying Command";
1761 cam_release_devq(ccb->ccb_h.path,
1765 /*getcount_only*/0);
1769 * If we have and error and are booting verbosely, whine
1770 * *unless* this was a non-retryable selection timeout.
1772 if (error != 0 && bootverbose &&
1773 !(status == CAM_SEL_TIMEOUT && (camflags & CAM_RETRY_SELTO) == 0)) {
1776 if (action_string == NULL)
1777 action_string = "Unretryable Error";
1778 if (error != ERESTART) {
1779 xpt_print(ccb->ccb_h.path, "error %d\n", error);
1781 xpt_print(ccb->ccb_h.path, "%s\n", action_string);