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.8 2004/03/12 03:23:13 dillon Exp $
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/types.h>
36 #include <sys/malloc.h>
37 #include <sys/linker_set.h>
40 #include <sys/devicestat.h>
43 #include <vm/vm_extern.h>
47 #include "cam_xpt_periph.h"
48 #include "cam_periph.h"
49 #include "cam_debug.h"
51 #include <bus/cam/scsi/scsi_all.h>
52 #include <bus/cam/scsi/scsi_message.h>
53 #include <bus/cam/scsi/scsi_da.h>
54 #include <bus/cam/scsi/scsi_pass.h>
56 static u_int camperiphnextunit(struct periph_driver *p_drv,
57 u_int newunit, int wired,
58 path_id_t pathid, target_id_t target,
60 static u_int camperiphunit(struct periph_driver *p_drv,
61 path_id_t pathid, target_id_t target,
63 static void camperiphdone(struct cam_periph *periph,
65 static void camperiphfree(struct cam_periph *periph);
68 cam_periph_alloc(periph_ctor_t *periph_ctor,
69 periph_oninv_t *periph_oninvalidate,
70 periph_dtor_t *periph_dtor, periph_start_t *periph_start,
71 char *name, cam_periph_type type, struct cam_path *path,
72 ac_callback_t *ac_callback, ac_code code, void *arg)
74 struct periph_driver **p_drv;
75 struct cam_periph *periph;
76 struct cam_periph *cur_periph;
78 target_id_t target_id;
86 * Handle Hot-Plug scenarios. If there is already a peripheral
87 * of our type assigned to this path, we are likely waiting for
88 * final close on an old, invalidated, peripheral. If this is
89 * the case, queue up a deferred call to the peripheral's async
90 * handler. If it looks like a mistaken re-alloation, complain.
92 if ((periph = cam_periph_find(path, name)) != NULL) {
94 if ((periph->flags & CAM_PERIPH_INVALID) != 0
95 && (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) == 0) {
96 periph->flags |= CAM_PERIPH_NEW_DEV_FOUND;
97 periph->deferred_callback = ac_callback;
98 periph->deferred_ac = code;
99 return (CAM_REQ_INPROG);
101 printf("cam_periph_alloc: attempt to re-allocate "
102 "valid device %s%d rejected\n",
103 periph->periph_name, periph->unit_number);
105 return (CAM_REQ_INVALID);
108 periph = malloc(sizeof(*periph), M_DEVBUF, M_INTWAIT);
112 SET_FOREACH(p_drv, periphdriver_set) {
113 if (strcmp((*p_drv)->driver_name, name) == 0)
117 path_id = xpt_path_path_id(path);
118 target_id = xpt_path_target_id(path);
119 lun_id = xpt_path_lun_id(path);
120 bzero(periph, sizeof(*periph));
121 cam_init_pinfo(&periph->pinfo);
122 periph->periph_start = periph_start;
123 periph->periph_dtor = periph_dtor;
124 periph->periph_oninval = periph_oninvalidate;
126 periph->periph_name = name;
127 periph->unit_number = camperiphunit(*p_drv, path_id, target_id, lun_id);
128 periph->immediate_priority = CAM_PRIORITY_NONE;
129 periph->refcount = 0;
130 SLIST_INIT(&periph->ccb_list);
131 status = xpt_create_path(&path, periph, path_id, target_id, lun_id);
132 if (status != CAM_REQ_CMP)
138 status = xpt_add_periph(periph);
140 if (status != CAM_REQ_CMP)
144 cur_periph = TAILQ_FIRST(&(*p_drv)->units);
145 while (cur_periph != NULL
146 && cur_periph->unit_number < periph->unit_number)
147 cur_periph = TAILQ_NEXT(cur_periph, unit_links);
149 if (cur_periph != NULL)
150 TAILQ_INSERT_BEFORE(cur_periph, periph, unit_links);
152 TAILQ_INSERT_TAIL(&(*p_drv)->units, periph, unit_links);
153 (*p_drv)->generation++;
160 status = periph_ctor(periph, arg);
162 if (status == CAM_REQ_CMP)
166 switch (init_level) {
168 /* Initialized successfully */
172 TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
174 xpt_remove_periph(periph);
176 xpt_free_path(periph->path);
178 free(periph, M_DEVBUF);
180 /* No cleanup to perform. */
183 panic("cam_periph_alloc: Unkown init level");
189 * Find a peripheral structure with the specified path, target, lun,
190 * and (optionally) type. If the name is NULL, this function will return
191 * the first peripheral driver that matches the specified path.
194 cam_periph_find(struct cam_path *path, char *name)
196 struct periph_driver **p_drv;
197 struct cam_periph *periph;
200 SET_FOREACH(p_drv, periphdriver_set) {
201 if (name != NULL && (strcmp((*p_drv)->driver_name, name) != 0))
205 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
206 periph = TAILQ_NEXT(periph, unit_links)) {
207 if (xpt_path_comp(periph->path, path) == 0) {
220 cam_periph_acquire(struct cam_periph *periph)
225 return(CAM_REQ_CMP_ERR);
235 cam_periph_release(struct cam_periph *periph)
243 if ((--periph->refcount == 0)
244 && (periph->flags & CAM_PERIPH_INVALID)) {
245 camperiphfree(periph);
252 * Look for the next unit number that is not currently in use for this
253 * peripheral type starting at "newunit". Also exclude unit numbers that
254 * are reserved by for future "hardwiring" unless we already know that this
255 * is a potential wired device. Only assume that the device is "wired" the
256 * first time through the loop since after that we'll be looking at unit
257 * numbers that did not match a wiring entry.
260 camperiphnextunit(struct periph_driver *p_drv, u_int newunit, int wired,
261 path_id_t pathid, target_id_t target, lun_id_t lun)
263 struct cam_periph *periph;
264 char *periph_name, *strval;
270 periph_name = p_drv->driver_name;
273 for (periph = TAILQ_FIRST(&p_drv->units);
274 periph != NULL && periph->unit_number != newunit;
275 periph = TAILQ_NEXT(periph, unit_links))
278 if (periph != NULL && periph->unit_number == newunit) {
280 xpt_print_path(periph->path);
281 printf("Duplicate Wired Device entry!\n");
282 xpt_print_path(periph->path);
283 printf("Second device (%s device at scbus%d "
284 "target %d lun %d) will not be wired\n",
285 periph_name, pathid, target, lun);
294 * Don't match entries like "da 4" as a wired down
295 * device, but do match entries like "da 4 target 5"
296 * or even "da 4 scbus 1".
299 while ((i = resource_locate(i, periph_name)) != -1) {
300 dname = resource_query_name(i);
301 dunit = resource_query_unit(i);
302 /* if no "target" and no specific scbus, skip */
303 if (resource_int_value(dname, dunit, "target", &val) &&
304 (resource_string_value(dname, dunit, "at",&strval)||
305 strcmp(strval, "scbus") == 0))
307 if (newunit == dunit)
318 camperiphunit(struct periph_driver *p_drv, path_id_t pathid,
319 target_id_t target, lun_id_t lun)
322 int hit, i, val, dunit;
324 char pathbuf[32], *strval, *periph_name;
328 periph_name = p_drv->driver_name;
329 snprintf(pathbuf, sizeof(pathbuf), "scbus%d", pathid);
331 for (hit = 0; (i = resource_locate(i, periph_name)) != -1; hit = 0) {
332 dname = resource_query_name(i);
333 dunit = resource_query_unit(i);
334 if (resource_string_value(dname, dunit, "at", &strval) == 0) {
335 if (strcmp(strval, pathbuf) != 0)
339 if (resource_int_value(dname, dunit, "target", &val) == 0) {
344 if (resource_int_value(dname, dunit, "lun", &val) == 0) {
356 * Either start from 0 looking for the next unit or from
357 * the unit number given in the resource config. This way,
358 * if we have wildcard matches, we don't return the same
361 unit = camperiphnextunit(p_drv, unit, /*wired*/hit, pathid,
368 cam_periph_invalidate(struct cam_periph *periph)
374 * We only call this routine the first time a peripheral is
375 * invalidated. The oninvalidate() routine is always called at
378 if (((periph->flags & CAM_PERIPH_INVALID) == 0)
379 && (periph->periph_oninval != NULL))
380 periph->periph_oninval(periph);
382 periph->flags |= CAM_PERIPH_INVALID;
383 periph->flags &= ~CAM_PERIPH_NEW_DEV_FOUND;
385 if (periph->refcount == 0)
386 camperiphfree(periph);
387 else if (periph->refcount < 0)
388 printf("cam_invalidate_periph: refcount < 0!!\n");
393 camperiphfree(struct cam_periph *periph)
396 struct periph_driver **p_drv;
398 SET_FOREACH(p_drv, periphdriver_set) {
399 if (strcmp((*p_drv)->driver_name, periph->periph_name) == 0)
403 if (periph->periph_dtor != NULL)
404 periph->periph_dtor(periph);
407 TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
408 (*p_drv)->generation++;
411 xpt_remove_periph(periph);
413 if (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) {
417 switch (periph->deferred_ac) {
418 case AC_FOUND_DEVICE:
419 ccb.ccb_h.func_code = XPT_GDEV_TYPE;
420 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/ 1);
424 case AC_PATH_REGISTERED:
425 ccb.ccb_h.func_code = XPT_PATH_INQ;
426 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/ 1);
434 periph->deferred_callback(NULL, periph->deferred_ac,
437 xpt_free_path(periph->path);
438 free(periph, M_DEVBUF);
442 * Wait interruptibly for an exclusive lock.
445 cam_periph_lock(struct cam_periph *periph, int flags)
449 while ((periph->flags & CAM_PERIPH_LOCKED) != 0) {
450 periph->flags |= CAM_PERIPH_LOCK_WANTED;
451 if ((error = tsleep(periph, flags, "caplck", 0)) != 0)
455 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
458 periph->flags |= CAM_PERIPH_LOCKED;
463 * Unlock and wake up any waiters.
466 cam_periph_unlock(struct cam_periph *periph)
468 periph->flags &= ~CAM_PERIPH_LOCKED;
469 if ((periph->flags & CAM_PERIPH_LOCK_WANTED) != 0) {
470 periph->flags &= ~CAM_PERIPH_LOCK_WANTED;
474 cam_periph_release(periph);
478 * Map user virtual pointers into kernel virtual address space, so we can
479 * access the memory. This won't work on physical pointers, for now it's
480 * up to the caller to check for that. (XXX KDM -- should we do that here
481 * instead?) This also only works for up to MAXPHYS memory. Since we use
482 * buffers to map stuff in and out, we're limited to the buffer size.
485 cam_periph_mapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
488 int flags[CAM_PERIPH_MAXMAPS];
489 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
490 u_int32_t lengths[CAM_PERIPH_MAXMAPS];
491 u_int32_t dirs[CAM_PERIPH_MAXMAPS];
493 switch(ccb->ccb_h.func_code) {
495 if (ccb->cdm.match_buf_len == 0) {
496 printf("cam_periph_mapmem: invalid match buffer "
500 if (ccb->cdm.pattern_buf_len > 0) {
501 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
502 lengths[0] = ccb->cdm.pattern_buf_len;
503 dirs[0] = CAM_DIR_OUT;
504 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
505 lengths[1] = ccb->cdm.match_buf_len;
506 dirs[1] = CAM_DIR_IN;
509 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
510 lengths[0] = ccb->cdm.match_buf_len;
511 dirs[0] = CAM_DIR_IN;
516 case XPT_CONT_TARGET_IO:
517 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
520 data_ptrs[0] = &ccb->csio.data_ptr;
521 lengths[0] = ccb->csio.dxfer_len;
522 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
527 break; /* NOTREACHED */
531 * Check the transfer length and permissions first, so we don't
532 * have to unmap any previously mapped buffers.
534 for (i = 0; i < numbufs; i++) {
539 * The userland data pointer passed in may not be page
540 * aligned. vmapbuf() truncates the address to a page
541 * boundary, so if the address isn't page aligned, we'll
542 * need enough space for the given transfer length, plus
543 * whatever extra space is necessary to make it to the page
547 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)) > DFLTPHYS){
548 printf("cam_periph_mapmem: attempt to map %lu bytes, "
549 "which is greater than DFLTPHYS(%d)\n",
551 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)),
556 if (dirs[i] & CAM_DIR_OUT) {
558 if (!useracc(*data_ptrs[i], lengths[i],
560 printf("cam_periph_mapmem: error, "
561 "address %p, length %lu isn't "
562 "user accessible for READ\n",
563 (void *)*data_ptrs[i],
570 * XXX this check is really bogus, since B_WRITE currently
571 * is all 0's, and so it is "set" all the time.
573 if (dirs[i] & CAM_DIR_IN) {
575 if (!useracc(*data_ptrs[i], lengths[i],
577 printf("cam_periph_mapmem: error, "
578 "address %p, length %lu isn't "
579 "user accessible for WRITE\n",
580 (void *)*data_ptrs[i],
589 for (i = 0; i < numbufs; i++) {
593 mapinfo->bp[i] = getpbuf(NULL);
595 /* save the buffer's data address */
596 mapinfo->bp[i]->b_saveaddr = mapinfo->bp[i]->b_data;
598 /* put our pointer in the data slot */
599 mapinfo->bp[i]->b_data = *data_ptrs[i];
601 /* set the transfer length, we know it's < DFLTPHYS */
602 mapinfo->bp[i]->b_bufsize = lengths[i];
605 mapinfo->bp[i]->b_flags = flags[i] | B_PHYS;
607 /* map the buffer into kernel memory */
608 if (vmapbuf(mapinfo->bp[i]) < 0) {
609 printf("cam_periph_mapmem: error, "
610 "address %p, length %lu isn't "
611 "user accessible any more\n",
612 (void *)*data_ptrs[i],
614 for (j = 0; j < i; ++j) {
615 *data_ptrs[j] = mapinfo->bp[j]->b_saveaddr;
616 mapinfo->bp[j]->b_flags &= ~B_PHYS;
617 relpbuf(mapinfo->bp[j], NULL);
622 /* set our pointer to the new mapped area */
623 *data_ptrs[i] = mapinfo->bp[i]->b_data;
625 mapinfo->num_bufs_used++;
632 * Unmap memory segments mapped into kernel virtual address space by
633 * cam_periph_mapmem().
636 cam_periph_unmapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
639 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
641 if (mapinfo->num_bufs_used <= 0) {
642 /* allow ourselves to be swapped once again */
646 switch (ccb->ccb_h.func_code) {
648 numbufs = min(mapinfo->num_bufs_used, 2);
651 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
653 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
654 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
658 case XPT_CONT_TARGET_IO:
659 data_ptrs[0] = &ccb->csio.data_ptr;
660 numbufs = min(mapinfo->num_bufs_used, 1);
663 /* allow ourselves to be swapped once again */
665 break; /* NOTREACHED */
668 for (i = 0; i < numbufs; i++) {
669 /* Set the user's pointer back to the original value */
670 *data_ptrs[i] = mapinfo->bp[i]->b_saveaddr;
672 /* unmap the buffer */
673 vunmapbuf(mapinfo->bp[i]);
675 /* clear the flags we set above */
676 mapinfo->bp[i]->b_flags &= ~B_PHYS;
678 /* release the buffer */
679 relpbuf(mapinfo->bp[i], NULL);
682 /* allow ourselves to be swapped once again */
686 cam_periph_getccb(struct cam_periph *periph, u_int32_t priority)
688 struct ccb_hdr *ccb_h;
691 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdgetccb\n"));
695 while (periph->ccb_list.slh_first == NULL) {
696 if (periph->immediate_priority > priority)
697 periph->immediate_priority = priority;
698 xpt_schedule(periph, priority);
699 if ((periph->ccb_list.slh_first != NULL)
700 && (periph->ccb_list.slh_first->pinfo.priority == priority))
702 tsleep(&periph->ccb_list, 0, "cgticb", 0);
705 ccb_h = periph->ccb_list.slh_first;
706 SLIST_REMOVE_HEAD(&periph->ccb_list, periph_links.sle);
708 return ((union ccb *)ccb_h);
712 cam_periph_ccbwait(union ccb *ccb)
717 if ((ccb->ccb_h.pinfo.index != CAM_UNQUEUED_INDEX)
718 || ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG))
719 tsleep(&ccb->ccb_h.cbfcnp, 0, "cbwait", 0);
725 cam_periph_ioctl(struct cam_periph *periph, int cmd, caddr_t addr,
726 int (*error_routine)(union ccb *ccb,
728 u_int32_t sense_flags))
738 ccb = cam_periph_getccb(periph, /* priority */ 1);
739 xpt_setup_ccb(&ccb->ccb_h,
742 ccb->ccb_h.func_code = XPT_GDEVLIST;
745 * Basically, the point of this is that we go through
746 * getting the list of devices, until we find a passthrough
747 * device. In the current version of the CAM code, the
748 * only way to determine what type of device we're dealing
749 * with is by its name.
753 ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS;
754 while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) {
756 /* we want the next device in the list */
758 if (strncmp(ccb->cgdl.periph_name,
764 if ((ccb->cgdl.status == CAM_GDEVLIST_LAST_DEVICE) &&
766 ccb->cgdl.periph_name[0] = '\0';
767 ccb->cgdl.unit_number = 0;
772 /* copy the result back out */
773 bcopy(ccb, addr, sizeof(union ccb));
775 /* and release the ccb */
776 xpt_release_ccb(ccb);
787 cam_periph_runccb(union ccb *ccb,
788 int (*error_routine)(union ccb *ccb,
790 u_int32_t sense_flags),
791 cam_flags camflags, u_int32_t sense_flags,
799 * If the user has supplied a stats structure, and if we understand
800 * this particular type of ccb, record the transaction start.
802 if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO))
803 devstat_start_transaction(ds);
808 cam_periph_ccbwait(ccb);
809 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
811 else if (error_routine != NULL)
812 error = (*error_routine)(ccb, camflags, sense_flags);
816 } while (error == ERESTART);
818 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
819 cam_release_devq(ccb->ccb_h.path,
823 /* getcount_only */ FALSE);
825 if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO))
826 devstat_end_transaction(ds,
828 ccb->csio.tag_action & 0xf,
829 ((ccb->ccb_h.flags & CAM_DIR_MASK) ==
830 CAM_DIR_NONE) ? DEVSTAT_NO_DATA :
831 (ccb->ccb_h.flags & CAM_DIR_OUT) ?
839 cam_freeze_devq(struct cam_path *path)
841 struct ccb_hdr ccb_h;
843 xpt_setup_ccb(&ccb_h, path, /*priority*/1);
844 ccb_h.func_code = XPT_NOOP;
845 ccb_h.flags = CAM_DEV_QFREEZE;
846 xpt_action((union ccb *)&ccb_h);
850 cam_release_devq(struct cam_path *path, u_int32_t relsim_flags,
851 u_int32_t openings, u_int32_t timeout,
854 struct ccb_relsim crs;
856 xpt_setup_ccb(&crs.ccb_h, path,
858 crs.ccb_h.func_code = XPT_REL_SIMQ;
859 crs.ccb_h.flags = getcount_only ? CAM_DEV_QFREEZE : 0;
860 crs.release_flags = relsim_flags;
861 crs.openings = openings;
862 crs.release_timeout = timeout;
863 xpt_action((union ccb *)&crs);
864 return (crs.qfrozen_cnt);
867 #define saved_ccb_ptr ppriv_ptr0
869 camperiphdone(struct cam_periph *periph, union ccb *done_ccb)
874 struct scsi_start_stop_unit *scsi_cmd;
875 u_int32_t relsim_flags, timeout;
876 u_int32_t qfrozen_cnt;
878 status = done_ccb->ccb_h.status;
879 frozen = (status & CAM_DEV_QFRZN) != 0;
880 sense = (status & CAM_AUTOSNS_VALID) != 0;
881 status &= CAM_STATUS_MASK;
887 * Unfreeze the queue once if it is already frozen..
890 qfrozen_cnt = cam_release_devq(done_ccb->ccb_h.path,
902 * If we have successfully taken a device from the not
903 * ready to ready state, re-scan the device and re-get the
904 * inquiry information. Many devices (mostly disks) don't
905 * properly report their inquiry information unless they
908 if (done_ccb->ccb_h.func_code == XPT_SCSI_IO) {
909 scsi_cmd = (struct scsi_start_stop_unit *)
910 &done_ccb->csio.cdb_io.cdb_bytes;
912 if (scsi_cmd->opcode == START_STOP_UNIT)
913 xpt_async(AC_INQ_CHANGED,
914 done_ccb->ccb_h.path, NULL);
916 bcopy(done_ccb->ccb_h.saved_ccb_ptr, done_ccb,
919 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
921 xpt_action(done_ccb);
924 case CAM_SCSI_STATUS_ERROR:
925 scsi_cmd = (struct scsi_start_stop_unit *)
926 &done_ccb->csio.cdb_io.cdb_bytes;
928 struct scsi_sense_data *sense;
929 int error_code, sense_key, asc, ascq;
931 sense = &done_ccb->csio.sense_data;
932 scsi_extract_sense(sense, &error_code,
933 &sense_key, &asc, &ascq);
936 * If the error is "invalid field in CDB",
937 * and the load/eject flag is set, turn the
938 * flag off and try again. This is just in
939 * case the drive in question barfs on the
940 * load eject flag. The CAM code should set
941 * the load/eject flag by default for
946 * Should we check to see what the specific
947 * scsi status is?? Or does it not matter
948 * since we already know that there was an
949 * error, and we know what the specific
950 * error code was, and we know what the
953 if ((scsi_cmd->opcode == START_STOP_UNIT) &&
954 ((scsi_cmd->how & SSS_LOEJ) != 0) &&
955 (asc == 0x24) && (ascq == 0x00) &&
956 (done_ccb->ccb_h.retry_count > 0)) {
958 scsi_cmd->how &= ~SSS_LOEJ;
960 xpt_action(done_ccb);
962 } else if (done_ccb->ccb_h.retry_count > 0) {
964 * In this case, the error recovery
965 * command failed, but we've got
966 * some retries left on it. Give
970 /* set the timeout to .5 sec */
972 RELSIM_RELEASE_AFTER_TIMEOUT;
975 xpt_action(done_ccb);
981 * Copy the original CCB back and
982 * send it back to the caller.
984 bcopy(done_ccb->ccb_h.saved_ccb_ptr,
985 done_ccb, sizeof(union ccb));
987 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
989 xpt_action(done_ccb);
993 * Eh?? The command failed, but we don't
994 * have any sense. What's up with that?
995 * Fire the CCB again to return it to the
998 bcopy(done_ccb->ccb_h.saved_ccb_ptr,
999 done_ccb, sizeof(union ccb));
1001 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1003 xpt_action(done_ccb);
1008 bcopy(done_ccb->ccb_h.saved_ccb_ptr, done_ccb,
1011 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1013 xpt_action(done_ccb);
1018 /* decrement the retry count */
1019 if (done_ccb->ccb_h.retry_count > 0)
1020 done_ccb->ccb_h.retry_count--;
1022 qfrozen_cnt = cam_release_devq(done_ccb->ccb_h.path,
1023 /*relsim_flags*/relsim_flags,
1026 /*getcount_only*/0);
1030 * Generic Async Event handler. Peripheral drivers usually
1031 * filter out the events that require personal attention,
1032 * and leave the rest to this function.
1035 cam_periph_async(struct cam_periph *periph, u_int32_t code,
1036 struct cam_path *path, void *arg)
1039 case AC_LOST_DEVICE:
1040 cam_periph_invalidate(periph);
1045 cam_periph_bus_settle(periph, SCSI_DELAY);
1054 cam_periph_bus_settle(struct cam_periph *periph, u_int bus_settle)
1056 struct ccb_getdevstats cgds;
1058 xpt_setup_ccb(&cgds.ccb_h, periph->path, /*priority*/1);
1059 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1060 xpt_action((union ccb *)&cgds);
1061 cam_periph_freeze_after_event(periph, &cgds.last_reset, bus_settle);
1065 cam_periph_freeze_after_event(struct cam_periph *periph,
1066 struct timeval* event_time, u_int duration_ms)
1068 struct timeval delta;
1069 struct timeval duration_tv;
1071 microuptime(&delta);
1072 timevalsub(&delta, event_time);
1073 duration_tv.tv_sec = duration_ms / 1000;
1074 duration_tv.tv_usec = (duration_ms % 1000) * 1000;
1075 if (timevalcmp(&delta, &duration_tv, <)) {
1076 timevalsub(&duration_tv, &delta);
1078 duration_ms = duration_tv.tv_sec * 1000;
1079 duration_ms += duration_tv.tv_usec / 1000;
1080 cam_freeze_devq(periph->path);
1081 cam_release_devq(periph->path,
1082 RELSIM_RELEASE_AFTER_TIMEOUT,
1084 /*timeout*/duration_ms,
1085 /*getcount_only*/0);
1091 * Generic error handler. Peripheral drivers usually filter
1092 * out the errors that they handle in a unique mannor, then
1093 * call this function.
1096 cam_periph_error(union ccb *ccb, cam_flags camflags,
1097 u_int32_t sense_flags, union ccb *save_ccb)
1105 u_int32_t relsim_flags;
1108 status = ccb->ccb_h.status;
1109 frozen = (status & CAM_DEV_QFRZN) != 0;
1110 sense = (status & CAM_AUTOSNS_VALID) != 0;
1111 status &= CAM_STATUS_MASK;
1116 /* decrement the number of retries */
1117 retry = ccb->ccb_h.retry_count > 0;
1119 ccb->ccb_h.retry_count--;
1122 case CAM_AUTOSENSE_FAIL:
1123 case CAM_SCSI_STATUS_ERROR:
1125 switch (ccb->csio.scsi_status) {
1126 case SCSI_STATUS_OK:
1127 case SCSI_STATUS_COND_MET:
1128 case SCSI_STATUS_INTERMED:
1129 case SCSI_STATUS_INTERMED_COND_MET:
1132 case SCSI_STATUS_CMD_TERMINATED:
1133 case SCSI_STATUS_CHECK_COND:
1135 struct scsi_sense_data *sense;
1136 int error_code, sense_key, asc, ascq;
1137 struct cam_periph *periph;
1138 scsi_sense_action err_action;
1139 struct ccb_getdev cgd;
1141 sense = &ccb->csio.sense_data;
1142 scsi_extract_sense(sense, &error_code,
1143 &sense_key, &asc, &ascq);
1144 periph = xpt_path_periph(ccb->ccb_h.path);
1147 * Grab the inquiry data for this device.
1149 xpt_setup_ccb(&cgd.ccb_h, ccb->ccb_h.path,
1151 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1152 xpt_action((union ccb *)&cgd);
1154 err_action = scsi_error_action(asc, ascq,
1158 * Send a Test Unit Ready to the device.
1159 * If the 'many' flag is set, we send 120
1160 * test unit ready commands, one every half
1161 * second. Otherwise, we just send one TUR.
1162 * We only want to do this if the retry
1163 * count has not been exhausted.
1165 if (((err_action & SS_MASK) == SS_TUR)
1167 && ccb->ccb_h.retry_count > 0) {
1170 * Since error recovery is already
1171 * in progress, don't attempt to
1172 * process this error. It is probably
1173 * related to the error that caused
1174 * the currently active error recovery
1175 * action. Also, we only have
1176 * space for one saved CCB, so if we
1177 * had two concurrent error recovery
1178 * actions, we would end up
1179 * over-writing one error recovery
1180 * CCB with another one.
1183 CAM_PERIPH_RECOVERY_INPROG) {
1189 CAM_PERIPH_RECOVERY_INPROG;
1191 /* decrement the number of retries */
1193 SSQ_DECREMENT_COUNT) != 0) {
1195 ccb->ccb_h.retry_count--;
1198 bcopy(ccb, save_ccb, sizeof(*save_ccb));
1201 * We retry this one every half
1202 * second for a minute. If the
1203 * device hasn't become ready in a
1204 * minute's time, it's unlikely to
1205 * ever become ready. If the table
1206 * doesn't specify SSQ_MANY, we can
1207 * only try this once. Oh well.
1209 if ((err_action & SSQ_MANY) != 0)
1210 scsi_test_unit_ready(&ccb->csio,
1217 scsi_test_unit_ready(&ccb->csio,
1224 /* release the queue after .5 sec. */
1226 RELSIM_RELEASE_AFTER_TIMEOUT;
1229 * Drop the priority to 0 so that
1230 * we are the first to execute. Also
1231 * freeze the queue after this command
1232 * is sent so that we can restore the
1233 * old csio and have it queued in the
1234 * proper order before we let normal
1235 * transactions go to the drive.
1237 ccb->ccb_h.pinfo.priority = 0;
1238 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1241 * Save a pointer to the original
1242 * CCB in the new CCB.
1244 ccb->ccb_h.saved_ccb_ptr = save_ccb;
1249 * Send a start unit command to the device,
1250 * and then retry the command. We only
1251 * want to do this if the retry count has
1252 * not been exhausted. If the user
1253 * specified 0 retries, then we follow
1254 * their request and do not retry.
1256 else if (((err_action & SS_MASK) == SS_START)
1258 && ccb->ccb_h.retry_count > 0) {
1262 * Only one error recovery action
1263 * at a time. See above.
1266 CAM_PERIPH_RECOVERY_INPROG) {
1272 CAM_PERIPH_RECOVERY_INPROG;
1274 /* decrement the number of retries */
1276 ccb->ccb_h.retry_count--;
1279 * Check for removable media and
1280 * set load/eject flag
1283 if (SID_IS_REMOVABLE(&cgd.inq_data))
1289 * Attempt to start the drive up.
1291 * Save the current ccb so it can
1292 * be restored and retried once the
1293 * drive is started up.
1295 bcopy(ccb, save_ccb, sizeof(*save_ccb));
1297 scsi_start_stop(&ccb->csio,
1307 * Drop the priority to 0 so that
1308 * we are the first to execute. Also
1309 * freeze the queue after this command
1310 * is sent so that we can restore the
1311 * old csio and have it queued in the
1312 * proper order before we let normal
1313 * transactions go to the drive.
1315 ccb->ccb_h.pinfo.priority = 0;
1316 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1319 * Save a pointer to the original
1320 * CCB in the new CCB.
1322 ccb->ccb_h.saved_ccb_ptr = save_ccb;
1325 } else if ((sense_flags & SF_RETRY_UA) != 0) {
1327 * XXX KDM this is a *horrible*
1330 error = scsi_interpret_sense(ccb,
1339 * Theoretically, this code should send a
1340 * test unit ready to the given device, and
1341 * if it returns and error, send a start
1342 * unit command. Since we don't yet have
1343 * the capability to do two-command error
1344 * recovery, just send a start unit.
1347 else if (((err_action & SS_MASK) == SS_TURSTART)
1349 && ccb->ccb_h.retry_count > 0) {
1353 * Only one error recovery action
1354 * at a time. See above.
1357 CAM_PERIPH_RECOVERY_INPROG) {
1363 CAM_PERIPH_RECOVERY_INPROG;
1365 /* decrement the number of retries */
1367 ccb->ccb_h.retry_count--;
1370 * Check for removable media and
1371 * set load/eject flag
1374 if (SID_IS_REMOVABLE(&cgd.inq_data))
1380 * Attempt to start the drive up.
1382 * Save the current ccb so it can
1383 * be restored and retried once the
1384 * drive is started up.
1386 bcopy(ccb, save_ccb, sizeof(*save_ccb));
1388 scsi_start_stop(&ccb->csio,
1398 /* release the queue after .5 sec. */
1400 RELSIM_RELEASE_AFTER_TIMEOUT;
1403 * Drop the priority to 0 so that
1404 * we are the first to execute. Also
1405 * freeze the queue after this command
1406 * is sent so that we can restore the
1407 * old csio and have it queued in the
1408 * proper order before we let normal
1409 * transactions go to the drive.
1411 ccb->ccb_h.pinfo.priority = 0;
1412 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1415 * Save a pointer to the original
1416 * CCB in the new CCB.
1418 ccb->ccb_h.saved_ccb_ptr = save_ccb;
1422 error = scsi_interpret_sense(ccb,
1429 } else if (ccb->csio.scsi_status ==
1430 SCSI_STATUS_CHECK_COND
1431 && status != CAM_AUTOSENSE_FAIL) {
1432 /* no point in decrementing the retry count */
1433 panic("cam_periph_error: scsi status of "
1434 "CHECK COND returned but no sense "
1435 "information is availible. "
1436 "Controller should have returned "
1437 "CAM_AUTOSENSE_FAILED");
1440 } else if (ccb->ccb_h.retry_count == 0) {
1442 * XXX KDM shouldn't there be a better
1443 * argument to return??
1447 /* decrement the number of retries */
1448 retry = ccb->ccb_h.retry_count > 0;
1450 ccb->ccb_h.retry_count--;
1452 * If it was aborted with no
1453 * clue as to the reason, just
1459 case SCSI_STATUS_QUEUE_FULL:
1462 struct ccb_getdevstats cgds;
1465 * First off, find out what the current
1466 * transaction counts are.
1468 xpt_setup_ccb(&cgds.ccb_h,
1471 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1472 xpt_action((union ccb *)&cgds);
1475 * If we were the only transaction active, treat
1476 * the QUEUE FULL as if it were a BUSY condition.
1478 if (cgds.dev_active != 0) {
1482 * Reduce the number of openings to
1483 * be 1 less than the amount it took
1484 * to get a queue full bounded by the
1485 * minimum allowed tag count for this
1489 cgds.dev_active+cgds.dev_openings;
1490 openings = cgds.dev_active;
1491 if (openings < cgds.mintags)
1492 openings = cgds.mintags;
1493 if (openings < total_openings)
1494 relsim_flags = RELSIM_ADJUST_OPENINGS;
1497 * Some devices report queue full for
1498 * temporary resource shortages. For
1499 * this reason, we allow a minimum
1500 * tag count to be entered via a
1501 * quirk entry to prevent the queue
1502 * count on these devices from falling
1503 * to a pessimisticly low value. We
1504 * still wait for the next successful
1505 * completion, however, before queueing
1506 * more transactions to the device.
1509 RELSIM_RELEASE_AFTER_CMDCMPLT;
1517 case SCSI_STATUS_BUSY:
1519 * Restart the queue after either another
1520 * command completes or a 1 second timeout.
1521 * If we have any retries left, that is.
1523 retry = ccb->ccb_h.retry_count > 0;
1525 ccb->ccb_h.retry_count--;
1527 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT
1528 | RELSIM_RELEASE_AFTER_CMDCMPLT;
1534 case SCSI_STATUS_RESERV_CONFLICT:
1542 case CAM_REQ_CMP_ERR:
1543 case CAM_CMD_TIMEOUT:
1544 case CAM_UNEXP_BUSFREE:
1545 case CAM_UNCOR_PARITY:
1546 case CAM_DATA_RUN_ERR:
1547 /* decrement the number of retries */
1548 retry = ccb->ccb_h.retry_count > 0;
1550 ccb->ccb_h.retry_count--;
1558 case CAM_MSG_REJECT_REC:
1559 /* XXX Don't know that these are correct */
1562 case CAM_SEL_TIMEOUT:
1566 * A single selection timeout should not be enough
1567 * to invalidate a device. We should retry for multiple
1568 * seconds assuming this isn't a probe. We'll probably
1569 * need a special flag for that.
1572 struct cam_path *newpath;
1574 /* Should we do more if we can't create the path?? */
1575 if (xpt_create_path(&newpath, xpt_path_periph(ccb->ccb_h.path),
1576 xpt_path_path_id(ccb->ccb_h.path),
1577 xpt_path_target_id(ccb->ccb_h.path),
1578 CAM_LUN_WILDCARD) != CAM_REQ_CMP)
1581 * Let peripheral drivers know that this device has gone
1584 xpt_async(AC_LOST_DEVICE, newpath, NULL);
1585 xpt_free_path(newpath);
1587 if ((sense_flags & SF_RETRY_SELTO) != 0) {
1588 retry = ccb->ccb_h.retry_count > 0;
1590 ccb->ccb_h.retry_count--;
1593 * Wait half a second to give the device
1594 * time to recover before we try again.
1596 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1606 case CAM_REQ_INVALID:
1607 case CAM_PATH_INVALID:
1608 case CAM_DEV_NOT_THERE:
1610 case CAM_PROVIDE_FAIL:
1611 case CAM_REQ_TOO_BIG:
1614 case CAM_SCSI_BUS_RESET:
1616 case CAM_REQUEUE_REQ:
1617 /* Unconditional requeue, dammit */
1620 case CAM_RESRC_UNAVAIL:
1624 /* decrement the number of retries */
1625 retry = ccb->ccb_h.retry_count > 0;
1627 ccb->ccb_h.retry_count--;
1630 /* Check the sense codes */
1636 /* Attempt a retry */
1637 if (error == ERESTART || error == 0) {
1639 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1641 if (error == ERESTART)
1645 cam_release_devq(ccb->ccb_h.path,
1649 /*getcount_only*/0);