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 $
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/types.h>
35 #include <sys/malloc.h>
36 #include <sys/linker_set.h>
39 #include <sys/devicestat.h>
42 #include <vm/vm_extern.h>
45 #include <cam/cam_ccb.h>
46 #include <cam/cam_xpt_periph.h>
47 #include <cam/cam_periph.h>
48 #include <cam/cam_debug.h>
50 #include <cam/scsi/scsi_all.h>
51 #include <cam/scsi/scsi_message.h>
52 #include <cam/scsi/scsi_da.h>
53 #include <cam/scsi/scsi_pass.h>
55 static u_int camperiphnextunit(struct periph_driver *p_drv,
56 u_int newunit, int wired,
57 path_id_t pathid, target_id_t target,
59 static u_int camperiphunit(struct periph_driver *p_drv,
60 path_id_t pathid, target_id_t target,
62 static void camperiphdone(struct cam_periph *periph,
64 static void camperiphfree(struct cam_periph *periph);
67 cam_periph_alloc(periph_ctor_t *periph_ctor,
68 periph_oninv_t *periph_oninvalidate,
69 periph_dtor_t *periph_dtor, periph_start_t *periph_start,
70 char *name, cam_periph_type type, struct cam_path *path,
71 ac_callback_t *ac_callback, ac_code code, void *arg)
73 struct periph_driver **p_drv;
74 struct cam_periph *periph;
75 struct cam_periph *cur_periph;
77 target_id_t target_id;
85 * Handle Hot-Plug scenarios. If there is already a peripheral
86 * of our type assigned to this path, we are likely waiting for
87 * final close on an old, invalidated, peripheral. If this is
88 * the case, queue up a deferred call to the peripheral's async
89 * handler. If it looks like a mistaken re-alloation, complain.
91 if ((periph = cam_periph_find(path, name)) != NULL) {
93 if ((periph->flags & CAM_PERIPH_INVALID) != 0
94 && (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) == 0) {
95 periph->flags |= CAM_PERIPH_NEW_DEV_FOUND;
96 periph->deferred_callback = ac_callback;
97 periph->deferred_ac = code;
98 return (CAM_REQ_INPROG);
100 printf("cam_periph_alloc: attempt to re-allocate "
101 "valid device %s%d rejected\n",
102 periph->periph_name, periph->unit_number);
104 return (CAM_REQ_INVALID);
107 periph = (struct cam_periph *)malloc(sizeof(*periph), M_DEVBUF,
111 return (CAM_RESRC_UNAVAIL);
115 for (p_drv = (struct periph_driver **)periphdriver_set.ls_items;
116 *p_drv != NULL; p_drv++) {
117 if (strcmp((*p_drv)->driver_name, name) == 0)
121 path_id = xpt_path_path_id(path);
122 target_id = xpt_path_target_id(path);
123 lun_id = xpt_path_lun_id(path);
124 bzero(periph, sizeof(*periph));
125 cam_init_pinfo(&periph->pinfo);
126 periph->periph_start = periph_start;
127 periph->periph_dtor = periph_dtor;
128 periph->periph_oninval = periph_oninvalidate;
130 periph->periph_name = name;
131 periph->unit_number = camperiphunit(*p_drv, path_id, target_id, lun_id);
132 periph->immediate_priority = CAM_PRIORITY_NONE;
133 periph->refcount = 0;
134 SLIST_INIT(&periph->ccb_list);
135 status = xpt_create_path(&path, periph, path_id, target_id, lun_id);
136 if (status != CAM_REQ_CMP)
142 status = xpt_add_periph(periph);
144 if (status != CAM_REQ_CMP)
148 cur_periph = TAILQ_FIRST(&(*p_drv)->units);
149 while (cur_periph != NULL
150 && cur_periph->unit_number < periph->unit_number)
151 cur_periph = TAILQ_NEXT(cur_periph, unit_links);
153 if (cur_periph != NULL)
154 TAILQ_INSERT_BEFORE(cur_periph, periph, unit_links);
156 TAILQ_INSERT_TAIL(&(*p_drv)->units, periph, unit_links);
157 (*p_drv)->generation++;
164 status = periph_ctor(periph, arg);
166 if (status == CAM_REQ_CMP)
170 switch (init_level) {
172 /* Initialized successfully */
176 TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
178 xpt_remove_periph(periph);
180 xpt_free_path(periph->path);
182 free(periph, M_DEVBUF);
184 /* No cleanup to perform. */
187 panic("cam_periph_alloc: Unkown init level");
193 * Find a peripheral structure with the specified path, target, lun,
194 * and (optionally) type. If the name is NULL, this function will return
195 * the first peripheral driver that matches the specified path.
198 cam_periph_find(struct cam_path *path, char *name)
200 struct periph_driver **p_drv;
201 struct cam_periph *periph;
204 for (p_drv = (struct periph_driver **)periphdriver_set.ls_items;
205 *p_drv != NULL; p_drv++) {
207 if (name != NULL && (strcmp((*p_drv)->driver_name, name) != 0))
211 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
212 periph = TAILQ_NEXT(periph, unit_links)) {
213 if (xpt_path_comp(periph->path, path) == 0) {
226 cam_periph_acquire(struct cam_periph *periph)
231 return(CAM_REQ_CMP_ERR);
241 cam_periph_release(struct cam_periph *periph)
249 if ((--periph->refcount == 0)
250 && (periph->flags & CAM_PERIPH_INVALID)) {
251 camperiphfree(periph);
258 * Look for the next unit number that is not currently in use for this
259 * peripheral type starting at "newunit". Also exclude unit numbers that
260 * are reserved by for future "hardwiring" unless we already know that this
261 * is a potential wired device. Only assume that the device is "wired" the
262 * first time through the loop since after that we'll be looking at unit
263 * numbers that did not match a wiring entry.
266 camperiphnextunit(struct periph_driver *p_drv, u_int newunit, int wired,
267 path_id_t pathid, target_id_t target, lun_id_t lun)
269 struct cam_periph *periph;
270 char *periph_name, *strval;
276 periph_name = p_drv->driver_name;
279 for (periph = TAILQ_FIRST(&p_drv->units);
280 periph != NULL && periph->unit_number != newunit;
281 periph = TAILQ_NEXT(periph, unit_links))
284 if (periph != NULL && periph->unit_number == newunit) {
286 xpt_print_path(periph->path);
287 printf("Duplicate Wired Device entry!\n");
288 xpt_print_path(periph->path);
289 printf("Second device (%s device at scbus%d "
290 "target %d lun %d) will not be wired\n",
291 periph_name, pathid, target, lun);
300 * Don't match entries like "da 4" as a wired down
301 * device, but do match entries like "da 4 target 5"
302 * or even "da 4 scbus 1".
305 while ((i = resource_locate(i, periph_name)) != -1) {
306 dname = resource_query_name(i);
307 dunit = resource_query_unit(i);
308 /* if no "target" and no specific scbus, skip */
309 if (resource_int_value(dname, dunit, "target", &val) &&
310 (resource_string_value(dname, dunit, "at",&strval)||
311 strcmp(strval, "scbus") == 0))
313 if (newunit == dunit)
324 camperiphunit(struct periph_driver *p_drv, path_id_t pathid,
325 target_id_t target, lun_id_t lun)
328 int hit, i, val, dunit;
330 char pathbuf[32], *strval, *periph_name;
334 periph_name = p_drv->driver_name;
335 snprintf(pathbuf, sizeof(pathbuf), "scbus%d", pathid);
337 for (hit = 0; (i = resource_locate(i, periph_name)) != -1; hit = 0) {
338 dname = resource_query_name(i);
339 dunit = resource_query_unit(i);
340 if (resource_string_value(dname, dunit, "at", &strval) == 0) {
341 if (strcmp(strval, pathbuf) != 0)
345 if (resource_int_value(dname, dunit, "target", &val) == 0) {
350 if (resource_int_value(dname, dunit, "lun", &val) == 0) {
362 * Either start from 0 looking for the next unit or from
363 * the unit number given in the resource config. This way,
364 * if we have wildcard matches, we don't return the same
367 unit = camperiphnextunit(p_drv, unit, /*wired*/hit, pathid,
374 cam_periph_invalidate(struct cam_periph *periph)
380 * We only call this routine the first time a peripheral is
381 * invalidated. The oninvalidate() routine is always called at
384 if (((periph->flags & CAM_PERIPH_INVALID) == 0)
385 && (periph->periph_oninval != NULL))
386 periph->periph_oninval(periph);
388 periph->flags |= CAM_PERIPH_INVALID;
389 periph->flags &= ~CAM_PERIPH_NEW_DEV_FOUND;
391 if (periph->refcount == 0)
392 camperiphfree(periph);
393 else if (periph->refcount < 0)
394 printf("cam_invalidate_periph: refcount < 0!!\n");
399 camperiphfree(struct cam_periph *periph)
402 struct periph_driver **p_drv;
404 for (p_drv = (struct periph_driver **)periphdriver_set.ls_items;
405 *p_drv != NULL; p_drv++) {
406 if (strcmp((*p_drv)->driver_name, periph->periph_name) == 0)
410 if (periph->periph_dtor != NULL)
411 periph->periph_dtor(periph);
414 TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
415 (*p_drv)->generation++;
418 xpt_remove_periph(periph);
420 if (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) {
424 switch (periph->deferred_ac) {
425 case AC_FOUND_DEVICE:
426 ccb.ccb_h.func_code = XPT_GDEV_TYPE;
427 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/ 1);
431 case AC_PATH_REGISTERED:
432 ccb.ccb_h.func_code = XPT_PATH_INQ;
433 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/ 1);
441 periph->deferred_callback(NULL, periph->deferred_ac,
444 xpt_free_path(periph->path);
445 free(periph, M_DEVBUF);
449 * Wait interruptibly for an exclusive lock.
452 cam_periph_lock(struct cam_periph *periph, int priority)
456 while ((periph->flags & CAM_PERIPH_LOCKED) != 0) {
457 periph->flags |= CAM_PERIPH_LOCK_WANTED;
458 if ((error = tsleep(periph, priority, "caplck", 0)) != 0)
462 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
465 periph->flags |= CAM_PERIPH_LOCKED;
470 * Unlock and wake up any waiters.
473 cam_periph_unlock(struct cam_periph *periph)
475 periph->flags &= ~CAM_PERIPH_LOCKED;
476 if ((periph->flags & CAM_PERIPH_LOCK_WANTED) != 0) {
477 periph->flags &= ~CAM_PERIPH_LOCK_WANTED;
481 cam_periph_release(periph);
485 * Map user virtual pointers into kernel virtual address space, so we can
486 * access the memory. This won't work on physical pointers, for now it's
487 * up to the caller to check for that. (XXX KDM -- should we do that here
488 * instead?) This also only works for up to MAXPHYS memory. Since we use
489 * buffers to map stuff in and out, we're limited to the buffer size.
492 cam_periph_mapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
495 int flags[CAM_PERIPH_MAXMAPS];
496 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
497 u_int32_t lengths[CAM_PERIPH_MAXMAPS];
498 u_int32_t dirs[CAM_PERIPH_MAXMAPS];
500 switch(ccb->ccb_h.func_code) {
502 if (ccb->cdm.match_buf_len == 0) {
503 printf("cam_periph_mapmem: invalid match buffer "
507 if (ccb->cdm.pattern_buf_len > 0) {
508 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
509 lengths[0] = ccb->cdm.pattern_buf_len;
510 dirs[0] = CAM_DIR_OUT;
511 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
512 lengths[1] = ccb->cdm.match_buf_len;
513 dirs[1] = CAM_DIR_IN;
516 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
517 lengths[0] = ccb->cdm.match_buf_len;
518 dirs[0] = CAM_DIR_IN;
523 case XPT_CONT_TARGET_IO:
524 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
527 data_ptrs[0] = &ccb->csio.data_ptr;
528 lengths[0] = ccb->csio.dxfer_len;
529 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
534 break; /* NOTREACHED */
538 * Check the transfer length and permissions first, so we don't
539 * have to unmap any previously mapped buffers.
541 for (i = 0; i < numbufs; i++) {
546 * The userland data pointer passed in may not be page
547 * aligned. vmapbuf() truncates the address to a page
548 * boundary, so if the address isn't page aligned, we'll
549 * need enough space for the given transfer length, plus
550 * whatever extra space is necessary to make it to the page
554 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)) > DFLTPHYS){
555 printf("cam_periph_mapmem: attempt to map %lu bytes, "
556 "which is greater than DFLTPHYS(%d)\n",
558 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)),
563 if (dirs[i] & CAM_DIR_OUT) {
565 if (!useracc(*data_ptrs[i], lengths[i],
567 printf("cam_periph_mapmem: error, "
568 "address %p, length %lu isn't "
569 "user accessible for READ\n",
570 (void *)*data_ptrs[i],
577 * XXX this check is really bogus, since B_WRITE currently
578 * is all 0's, and so it is "set" all the time.
580 if (dirs[i] & CAM_DIR_IN) {
582 if (!useracc(*data_ptrs[i], lengths[i],
584 printf("cam_periph_mapmem: error, "
585 "address %p, length %lu isn't "
586 "user accessible for WRITE\n",
587 (void *)*data_ptrs[i],
596 /* this keeps the current process from getting swapped */
598 * XXX KDM should I use P_NOSWAP instead?
602 for (i = 0; i < numbufs; i++) {
606 mapinfo->bp[i] = getpbuf(NULL);
608 /* save the buffer's data address */
609 mapinfo->bp[i]->b_saveaddr = mapinfo->bp[i]->b_data;
611 /* put our pointer in the data slot */
612 mapinfo->bp[i]->b_data = *data_ptrs[i];
614 /* set the transfer length, we know it's < DFLTPHYS */
615 mapinfo->bp[i]->b_bufsize = lengths[i];
618 mapinfo->bp[i]->b_flags = flags[i] | B_PHYS;
620 /* map the buffer into kernel memory */
621 if (vmapbuf(mapinfo->bp[i]) < 0) {
622 printf("cam_periph_mapmem: error, "
623 "address %p, length %lu isn't "
624 "user accessible any more\n",
625 (void *)*data_ptrs[i],
627 for (j = 0; j < i; ++j) {
628 *data_ptrs[j] = mapinfo->bp[j]->b_saveaddr;
629 mapinfo->bp[j]->b_flags &= ~B_PHYS;
630 relpbuf(mapinfo->bp[j], NULL);
636 /* set our pointer to the new mapped area */
637 *data_ptrs[i] = mapinfo->bp[i]->b_data;
639 mapinfo->num_bufs_used++;
646 * Unmap memory segments mapped into kernel virtual address space by
647 * cam_periph_mapmem().
650 cam_periph_unmapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
653 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
655 if (mapinfo->num_bufs_used <= 0) {
656 /* allow ourselves to be swapped once again */
661 switch (ccb->ccb_h.func_code) {
663 numbufs = min(mapinfo->num_bufs_used, 2);
666 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
668 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
669 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
673 case XPT_CONT_TARGET_IO:
674 data_ptrs[0] = &ccb->csio.data_ptr;
675 numbufs = min(mapinfo->num_bufs_used, 1);
678 /* allow ourselves to be swapped once again */
681 break; /* NOTREACHED */
684 for (i = 0; i < numbufs; i++) {
685 /* Set the user's pointer back to the original value */
686 *data_ptrs[i] = mapinfo->bp[i]->b_saveaddr;
688 /* unmap the buffer */
689 vunmapbuf(mapinfo->bp[i]);
691 /* clear the flags we set above */
692 mapinfo->bp[i]->b_flags &= ~B_PHYS;
694 /* release the buffer */
695 relpbuf(mapinfo->bp[i], NULL);
698 /* allow ourselves to be swapped once again */
703 cam_periph_getccb(struct cam_periph *periph, u_int32_t priority)
705 struct ccb_hdr *ccb_h;
708 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdgetccb\n"));
712 while (periph->ccb_list.slh_first == NULL) {
713 if (periph->immediate_priority > priority)
714 periph->immediate_priority = priority;
715 xpt_schedule(periph, priority);
716 if ((periph->ccb_list.slh_first != NULL)
717 && (periph->ccb_list.slh_first->pinfo.priority == priority))
719 tsleep(&periph->ccb_list, PRIBIO, "cgticb", 0);
722 ccb_h = periph->ccb_list.slh_first;
723 SLIST_REMOVE_HEAD(&periph->ccb_list, periph_links.sle);
725 return ((union ccb *)ccb_h);
729 cam_periph_ccbwait(union ccb *ccb)
734 if ((ccb->ccb_h.pinfo.index != CAM_UNQUEUED_INDEX)
735 || ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG))
736 tsleep(&ccb->ccb_h.cbfcnp, PRIBIO, "cbwait", 0);
742 cam_periph_ioctl(struct cam_periph *periph, int cmd, caddr_t addr,
743 int (*error_routine)(union ccb *ccb,
745 u_int32_t sense_flags))
755 ccb = cam_periph_getccb(periph, /* priority */ 1);
756 xpt_setup_ccb(&ccb->ccb_h,
759 ccb->ccb_h.func_code = XPT_GDEVLIST;
762 * Basically, the point of this is that we go through
763 * getting the list of devices, until we find a passthrough
764 * device. In the current version of the CAM code, the
765 * only way to determine what type of device we're dealing
766 * with is by its name.
770 ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS;
771 while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) {
773 /* we want the next device in the list */
775 if (strncmp(ccb->cgdl.periph_name,
781 if ((ccb->cgdl.status == CAM_GDEVLIST_LAST_DEVICE) &&
783 ccb->cgdl.periph_name[0] = '\0';
784 ccb->cgdl.unit_number = 0;
789 /* copy the result back out */
790 bcopy(ccb, addr, sizeof(union ccb));
792 /* and release the ccb */
793 xpt_release_ccb(ccb);
804 cam_periph_runccb(union ccb *ccb,
805 int (*error_routine)(union ccb *ccb,
807 u_int32_t sense_flags),
808 cam_flags camflags, u_int32_t sense_flags,
816 * If the user has supplied a stats structure, and if we understand
817 * this particular type of ccb, record the transaction start.
819 if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO))
820 devstat_start_transaction(ds);
825 cam_periph_ccbwait(ccb);
826 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
828 else if (error_routine != NULL)
829 error = (*error_routine)(ccb, camflags, sense_flags);
833 } while (error == ERESTART);
835 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
836 cam_release_devq(ccb->ccb_h.path,
840 /* getcount_only */ FALSE);
842 if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO))
843 devstat_end_transaction(ds,
845 ccb->csio.tag_action & 0xf,
846 ((ccb->ccb_h.flags & CAM_DIR_MASK) ==
847 CAM_DIR_NONE) ? DEVSTAT_NO_DATA :
848 (ccb->ccb_h.flags & CAM_DIR_OUT) ?
856 cam_freeze_devq(struct cam_path *path)
858 struct ccb_hdr ccb_h;
860 xpt_setup_ccb(&ccb_h, path, /*priority*/1);
861 ccb_h.func_code = XPT_NOOP;
862 ccb_h.flags = CAM_DEV_QFREEZE;
863 xpt_action((union ccb *)&ccb_h);
867 cam_release_devq(struct cam_path *path, u_int32_t relsim_flags,
868 u_int32_t openings, u_int32_t timeout,
871 struct ccb_relsim crs;
873 xpt_setup_ccb(&crs.ccb_h, path,
875 crs.ccb_h.func_code = XPT_REL_SIMQ;
876 crs.ccb_h.flags = getcount_only ? CAM_DEV_QFREEZE : 0;
877 crs.release_flags = relsim_flags;
878 crs.openings = openings;
879 crs.release_timeout = timeout;
880 xpt_action((union ccb *)&crs);
881 return (crs.qfrozen_cnt);
884 #define saved_ccb_ptr ppriv_ptr0
886 camperiphdone(struct cam_periph *periph, union ccb *done_ccb)
891 struct scsi_start_stop_unit *scsi_cmd;
892 u_int32_t relsim_flags, timeout;
893 u_int32_t qfrozen_cnt;
895 status = done_ccb->ccb_h.status;
896 frozen = (status & CAM_DEV_QFRZN) != 0;
897 sense = (status & CAM_AUTOSNS_VALID) != 0;
898 status &= CAM_STATUS_MASK;
904 * Unfreeze the queue once if it is already frozen..
907 qfrozen_cnt = cam_release_devq(done_ccb->ccb_h.path,
919 * If we have successfully taken a device from the not
920 * ready to ready state, re-scan the device and re-get the
921 * inquiry information. Many devices (mostly disks) don't
922 * properly report their inquiry information unless they
925 if (done_ccb->ccb_h.func_code == XPT_SCSI_IO) {
926 scsi_cmd = (struct scsi_start_stop_unit *)
927 &done_ccb->csio.cdb_io.cdb_bytes;
929 if (scsi_cmd->opcode == START_STOP_UNIT)
930 xpt_async(AC_INQ_CHANGED,
931 done_ccb->ccb_h.path, NULL);
933 bcopy(done_ccb->ccb_h.saved_ccb_ptr, done_ccb,
936 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
938 xpt_action(done_ccb);
941 case CAM_SCSI_STATUS_ERROR:
942 scsi_cmd = (struct scsi_start_stop_unit *)
943 &done_ccb->csio.cdb_io.cdb_bytes;
945 struct scsi_sense_data *sense;
946 int error_code, sense_key, asc, ascq;
948 sense = &done_ccb->csio.sense_data;
949 scsi_extract_sense(sense, &error_code,
950 &sense_key, &asc, &ascq);
953 * If the error is "invalid field in CDB",
954 * and the load/eject flag is set, turn the
955 * flag off and try again. This is just in
956 * case the drive in question barfs on the
957 * load eject flag. The CAM code should set
958 * the load/eject flag by default for
963 * Should we check to see what the specific
964 * scsi status is?? Or does it not matter
965 * since we already know that there was an
966 * error, and we know what the specific
967 * error code was, and we know what the
970 if ((scsi_cmd->opcode == START_STOP_UNIT) &&
971 ((scsi_cmd->how & SSS_LOEJ) != 0) &&
972 (asc == 0x24) && (ascq == 0x00) &&
973 (done_ccb->ccb_h.retry_count > 0)) {
975 scsi_cmd->how &= ~SSS_LOEJ;
977 xpt_action(done_ccb);
979 } else if (done_ccb->ccb_h.retry_count > 0) {
981 * In this case, the error recovery
982 * command failed, but we've got
983 * some retries left on it. Give
987 /* set the timeout to .5 sec */
989 RELSIM_RELEASE_AFTER_TIMEOUT;
992 xpt_action(done_ccb);
998 * Copy the original CCB back and
999 * send it back to the caller.
1001 bcopy(done_ccb->ccb_h.saved_ccb_ptr,
1002 done_ccb, sizeof(union ccb));
1004 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1006 xpt_action(done_ccb);
1010 * Eh?? The command failed, but we don't
1011 * have any sense. What's up with that?
1012 * Fire the CCB again to return it to the
1015 bcopy(done_ccb->ccb_h.saved_ccb_ptr,
1016 done_ccb, sizeof(union ccb));
1018 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1020 xpt_action(done_ccb);
1025 bcopy(done_ccb->ccb_h.saved_ccb_ptr, done_ccb,
1028 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1030 xpt_action(done_ccb);
1035 /* decrement the retry count */
1036 if (done_ccb->ccb_h.retry_count > 0)
1037 done_ccb->ccb_h.retry_count--;
1039 qfrozen_cnt = cam_release_devq(done_ccb->ccb_h.path,
1040 /*relsim_flags*/relsim_flags,
1043 /*getcount_only*/0);
1047 * Generic Async Event handler. Peripheral drivers usually
1048 * filter out the events that require personal attention,
1049 * and leave the rest to this function.
1052 cam_periph_async(struct cam_periph *periph, u_int32_t code,
1053 struct cam_path *path, void *arg)
1056 case AC_LOST_DEVICE:
1057 cam_periph_invalidate(periph);
1062 cam_periph_bus_settle(periph, SCSI_DELAY);
1071 cam_periph_bus_settle(struct cam_periph *periph, u_int bus_settle)
1073 struct ccb_getdevstats cgds;
1075 xpt_setup_ccb(&cgds.ccb_h, periph->path, /*priority*/1);
1076 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1077 xpt_action((union ccb *)&cgds);
1078 cam_periph_freeze_after_event(periph, &cgds.last_reset, bus_settle);
1082 cam_periph_freeze_after_event(struct cam_periph *periph,
1083 struct timeval* event_time, u_int duration_ms)
1085 struct timeval delta;
1086 struct timeval duration_tv;
1092 timevalsub(&delta, event_time);
1093 duration_tv.tv_sec = duration_ms / 1000;
1094 duration_tv.tv_usec = (duration_ms % 1000) * 1000;
1095 if (timevalcmp(&delta, &duration_tv, <)) {
1096 timevalsub(&duration_tv, &delta);
1098 duration_ms = duration_tv.tv_sec * 1000;
1099 duration_ms += duration_tv.tv_usec / 1000;
1100 cam_freeze_devq(periph->path);
1101 cam_release_devq(periph->path,
1102 RELSIM_RELEASE_AFTER_TIMEOUT,
1104 /*timeout*/duration_ms,
1105 /*getcount_only*/0);
1111 * Generic error handler. Peripheral drivers usually filter
1112 * out the errors that they handle in a unique mannor, then
1113 * call this function.
1116 cam_periph_error(union ccb *ccb, cam_flags camflags,
1117 u_int32_t sense_flags, union ccb *save_ccb)
1125 u_int32_t relsim_flags;
1128 status = ccb->ccb_h.status;
1129 frozen = (status & CAM_DEV_QFRZN) != 0;
1130 sense = (status & CAM_AUTOSNS_VALID) != 0;
1131 status &= CAM_STATUS_MASK;
1136 /* decrement the number of retries */
1137 retry = ccb->ccb_h.retry_count > 0;
1139 ccb->ccb_h.retry_count--;
1142 case CAM_AUTOSENSE_FAIL:
1143 case CAM_SCSI_STATUS_ERROR:
1145 switch (ccb->csio.scsi_status) {
1146 case SCSI_STATUS_OK:
1147 case SCSI_STATUS_COND_MET:
1148 case SCSI_STATUS_INTERMED:
1149 case SCSI_STATUS_INTERMED_COND_MET:
1152 case SCSI_STATUS_CMD_TERMINATED:
1153 case SCSI_STATUS_CHECK_COND:
1155 struct scsi_sense_data *sense;
1156 int error_code, sense_key, asc, ascq;
1157 struct cam_periph *periph;
1158 scsi_sense_action err_action;
1159 struct ccb_getdev cgd;
1161 sense = &ccb->csio.sense_data;
1162 scsi_extract_sense(sense, &error_code,
1163 &sense_key, &asc, &ascq);
1164 periph = xpt_path_periph(ccb->ccb_h.path);
1167 * Grab the inquiry data for this device.
1169 xpt_setup_ccb(&cgd.ccb_h, ccb->ccb_h.path,
1171 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1172 xpt_action((union ccb *)&cgd);
1174 err_action = scsi_error_action(asc, ascq,
1178 * Send a Test Unit Ready to the device.
1179 * If the 'many' flag is set, we send 120
1180 * test unit ready commands, one every half
1181 * second. Otherwise, we just send one TUR.
1182 * We only want to do this if the retry
1183 * count has not been exhausted.
1185 if (((err_action & SS_MASK) == SS_TUR)
1187 && ccb->ccb_h.retry_count > 0) {
1190 * Since error recovery is already
1191 * in progress, don't attempt to
1192 * process this error. It is probably
1193 * related to the error that caused
1194 * the currently active error recovery
1195 * action. Also, we only have
1196 * space for one saved CCB, so if we
1197 * had two concurrent error recovery
1198 * actions, we would end up
1199 * over-writing one error recovery
1200 * CCB with another one.
1203 CAM_PERIPH_RECOVERY_INPROG) {
1209 CAM_PERIPH_RECOVERY_INPROG;
1211 /* decrement the number of retries */
1213 SSQ_DECREMENT_COUNT) != 0) {
1215 ccb->ccb_h.retry_count--;
1218 bcopy(ccb, save_ccb, sizeof(*save_ccb));
1221 * We retry this one every half
1222 * second for a minute. If the
1223 * device hasn't become ready in a
1224 * minute's time, it's unlikely to
1225 * ever become ready. If the table
1226 * doesn't specify SSQ_MANY, we can
1227 * only try this once. Oh well.
1229 if ((err_action & SSQ_MANY) != 0)
1230 scsi_test_unit_ready(&ccb->csio,
1237 scsi_test_unit_ready(&ccb->csio,
1244 /* release the queue after .5 sec. */
1246 RELSIM_RELEASE_AFTER_TIMEOUT;
1249 * Drop the priority to 0 so that
1250 * we are the first to execute. Also
1251 * freeze the queue after this command
1252 * is sent so that we can restore the
1253 * old csio and have it queued in the
1254 * proper order before we let normal
1255 * transactions go to the drive.
1257 ccb->ccb_h.pinfo.priority = 0;
1258 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1261 * Save a pointer to the original
1262 * CCB in the new CCB.
1264 ccb->ccb_h.saved_ccb_ptr = save_ccb;
1269 * Send a start unit command to the device,
1270 * and then retry the command. We only
1271 * want to do this if the retry count has
1272 * not been exhausted. If the user
1273 * specified 0 retries, then we follow
1274 * their request and do not retry.
1276 else if (((err_action & SS_MASK) == SS_START)
1278 && ccb->ccb_h.retry_count > 0) {
1282 * Only one error recovery action
1283 * at a time. See above.
1286 CAM_PERIPH_RECOVERY_INPROG) {
1292 CAM_PERIPH_RECOVERY_INPROG;
1294 /* decrement the number of retries */
1296 ccb->ccb_h.retry_count--;
1299 * Check for removable media and
1300 * set load/eject flag
1303 if (SID_IS_REMOVABLE(&cgd.inq_data))
1309 * Attempt to start the drive up.
1311 * Save the current ccb so it can
1312 * be restored and retried once the
1313 * drive is started up.
1315 bcopy(ccb, save_ccb, sizeof(*save_ccb));
1317 scsi_start_stop(&ccb->csio,
1327 * Drop the priority to 0 so that
1328 * we are the first to execute. Also
1329 * freeze the queue after this command
1330 * is sent so that we can restore the
1331 * old csio and have it queued in the
1332 * proper order before we let normal
1333 * transactions go to the drive.
1335 ccb->ccb_h.pinfo.priority = 0;
1336 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1339 * Save a pointer to the original
1340 * CCB in the new CCB.
1342 ccb->ccb_h.saved_ccb_ptr = save_ccb;
1345 } else if ((sense_flags & SF_RETRY_UA) != 0) {
1347 * XXX KDM this is a *horrible*
1350 error = scsi_interpret_sense(ccb,
1359 * Theoretically, this code should send a
1360 * test unit ready to the given device, and
1361 * if it returns and error, send a start
1362 * unit command. Since we don't yet have
1363 * the capability to do two-command error
1364 * recovery, just send a start unit.
1367 else if (((err_action & SS_MASK) == SS_TURSTART)
1369 && ccb->ccb_h.retry_count > 0) {
1373 * Only one error recovery action
1374 * at a time. See above.
1377 CAM_PERIPH_RECOVERY_INPROG) {
1383 CAM_PERIPH_RECOVERY_INPROG;
1385 /* decrement the number of retries */
1387 ccb->ccb_h.retry_count--;
1390 * Check for removable media and
1391 * set load/eject flag
1394 if (SID_IS_REMOVABLE(&cgd.inq_data))
1400 * Attempt to start the drive up.
1402 * Save the current ccb so it can
1403 * be restored and retried once the
1404 * drive is started up.
1406 bcopy(ccb, save_ccb, sizeof(*save_ccb));
1408 scsi_start_stop(&ccb->csio,
1418 /* release the queue after .5 sec. */
1420 RELSIM_RELEASE_AFTER_TIMEOUT;
1423 * Drop the priority to 0 so that
1424 * we are the first to execute. Also
1425 * freeze the queue after this command
1426 * is sent so that we can restore the
1427 * old csio and have it queued in the
1428 * proper order before we let normal
1429 * transactions go to the drive.
1431 ccb->ccb_h.pinfo.priority = 0;
1432 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1435 * Save a pointer to the original
1436 * CCB in the new CCB.
1438 ccb->ccb_h.saved_ccb_ptr = save_ccb;
1442 error = scsi_interpret_sense(ccb,
1449 } else if (ccb->csio.scsi_status ==
1450 SCSI_STATUS_CHECK_COND
1451 && status != CAM_AUTOSENSE_FAIL) {
1452 /* no point in decrementing the retry count */
1453 panic("cam_periph_error: scsi status of "
1454 "CHECK COND returned but no sense "
1455 "information is availible. "
1456 "Controller should have returned "
1457 "CAM_AUTOSENSE_FAILED");
1460 } else if (ccb->ccb_h.retry_count == 0) {
1462 * XXX KDM shouldn't there be a better
1463 * argument to return??
1467 /* decrement the number of retries */
1468 retry = ccb->ccb_h.retry_count > 0;
1470 ccb->ccb_h.retry_count--;
1472 * If it was aborted with no
1473 * clue as to the reason, just
1479 case SCSI_STATUS_QUEUE_FULL:
1482 struct ccb_getdevstats cgds;
1485 * First off, find out what the current
1486 * transaction counts are.
1488 xpt_setup_ccb(&cgds.ccb_h,
1491 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1492 xpt_action((union ccb *)&cgds);
1495 * If we were the only transaction active, treat
1496 * the QUEUE FULL as if it were a BUSY condition.
1498 if (cgds.dev_active != 0) {
1502 * Reduce the number of openings to
1503 * be 1 less than the amount it took
1504 * to get a queue full bounded by the
1505 * minimum allowed tag count for this
1509 cgds.dev_active+cgds.dev_openings;
1510 openings = cgds.dev_active;
1511 if (openings < cgds.mintags)
1512 openings = cgds.mintags;
1513 if (openings < total_openings)
1514 relsim_flags = RELSIM_ADJUST_OPENINGS;
1517 * Some devices report queue full for
1518 * temporary resource shortages. For
1519 * this reason, we allow a minimum
1520 * tag count to be entered via a
1521 * quirk entry to prevent the queue
1522 * count on these devices from falling
1523 * to a pessimisticly low value. We
1524 * still wait for the next successful
1525 * completion, however, before queueing
1526 * more transactions to the device.
1529 RELSIM_RELEASE_AFTER_CMDCMPLT;
1537 case SCSI_STATUS_BUSY:
1539 * Restart the queue after either another
1540 * command completes or a 1 second timeout.
1541 * If we have any retries left, that is.
1543 retry = ccb->ccb_h.retry_count > 0;
1545 ccb->ccb_h.retry_count--;
1547 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT
1548 | RELSIM_RELEASE_AFTER_CMDCMPLT;
1554 case SCSI_STATUS_RESERV_CONFLICT:
1562 case CAM_REQ_CMP_ERR:
1563 case CAM_CMD_TIMEOUT:
1564 case CAM_UNEXP_BUSFREE:
1565 case CAM_UNCOR_PARITY:
1566 case CAM_DATA_RUN_ERR:
1567 /* decrement the number of retries */
1568 retry = ccb->ccb_h.retry_count > 0;
1570 ccb->ccb_h.retry_count--;
1578 case CAM_MSG_REJECT_REC:
1579 /* XXX Don't know that these are correct */
1582 case CAM_SEL_TIMEOUT:
1586 * A single selection timeout should not be enough
1587 * to invalidate a device. We should retry for multiple
1588 * seconds assuming this isn't a probe. We'll probably
1589 * need a special flag for that.
1592 struct cam_path *newpath;
1594 /* Should we do more if we can't create the path?? */
1595 if (xpt_create_path(&newpath, xpt_path_periph(ccb->ccb_h.path),
1596 xpt_path_path_id(ccb->ccb_h.path),
1597 xpt_path_target_id(ccb->ccb_h.path),
1598 CAM_LUN_WILDCARD) != CAM_REQ_CMP)
1601 * Let peripheral drivers know that this device has gone
1604 xpt_async(AC_LOST_DEVICE, newpath, NULL);
1605 xpt_free_path(newpath);
1607 if ((sense_flags & SF_RETRY_SELTO) != 0) {
1608 retry = ccb->ccb_h.retry_count > 0;
1610 ccb->ccb_h.retry_count--;
1613 * Wait half a second to give the device
1614 * time to recover before we try again.
1616 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
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:
1636 case CAM_REQUEUE_REQ:
1637 /* Unconditional requeue, dammit */
1640 case CAM_RESRC_UNAVAIL:
1644 /* decrement the number of retries */
1645 retry = ccb->ccb_h.retry_count > 0;
1647 ccb->ccb_h.retry_count--;
1650 /* Check the sense codes */
1656 /* Attempt a retry */
1657 if (error == ERESTART || error == 0) {
1659 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1661 if (error == ERESTART)
1665 cam_release_devq(ccb->ccb_h.path,
1669 /*getcount_only*/0);