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.4 2003/07/19 21:14:11 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>
46 #include <cam/cam_ccb.h>
47 #include <cam/cam_xpt_periph.h>
48 #include <cam/cam_periph.h>
49 #include <cam/cam_debug.h>
51 #include <cam/scsi/scsi_all.h>
52 #include <cam/scsi/scsi_message.h>
53 #include <cam/scsi/scsi_da.h>
54 #include <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 = (struct cam_periph *)malloc(sizeof(*periph), M_DEVBUF,
112 return (CAM_RESRC_UNAVAIL);
116 for (p_drv = (struct periph_driver **)periphdriver_set.ls_items;
117 *p_drv != NULL; p_drv++) {
118 if (strcmp((*p_drv)->driver_name, name) == 0)
122 path_id = xpt_path_path_id(path);
123 target_id = xpt_path_target_id(path);
124 lun_id = xpt_path_lun_id(path);
125 bzero(periph, sizeof(*periph));
126 cam_init_pinfo(&periph->pinfo);
127 periph->periph_start = periph_start;
128 periph->periph_dtor = periph_dtor;
129 periph->periph_oninval = periph_oninvalidate;
131 periph->periph_name = name;
132 periph->unit_number = camperiphunit(*p_drv, path_id, target_id, lun_id);
133 periph->immediate_priority = CAM_PRIORITY_NONE;
134 periph->refcount = 0;
135 SLIST_INIT(&periph->ccb_list);
136 status = xpt_create_path(&path, periph, path_id, target_id, lun_id);
137 if (status != CAM_REQ_CMP)
143 status = xpt_add_periph(periph);
145 if (status != CAM_REQ_CMP)
149 cur_periph = TAILQ_FIRST(&(*p_drv)->units);
150 while (cur_periph != NULL
151 && cur_periph->unit_number < periph->unit_number)
152 cur_periph = TAILQ_NEXT(cur_periph, unit_links);
154 if (cur_periph != NULL)
155 TAILQ_INSERT_BEFORE(cur_periph, periph, unit_links);
157 TAILQ_INSERT_TAIL(&(*p_drv)->units, periph, unit_links);
158 (*p_drv)->generation++;
165 status = periph_ctor(periph, arg);
167 if (status == CAM_REQ_CMP)
171 switch (init_level) {
173 /* Initialized successfully */
177 TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
179 xpt_remove_periph(periph);
181 xpt_free_path(periph->path);
183 free(periph, M_DEVBUF);
185 /* No cleanup to perform. */
188 panic("cam_periph_alloc: Unkown init level");
194 * Find a peripheral structure with the specified path, target, lun,
195 * and (optionally) type. If the name is NULL, this function will return
196 * the first peripheral driver that matches the specified path.
199 cam_periph_find(struct cam_path *path, char *name)
201 struct periph_driver **p_drv;
202 struct cam_periph *periph;
205 for (p_drv = (struct periph_driver **)periphdriver_set.ls_items;
206 *p_drv != NULL; p_drv++) {
208 if (name != NULL && (strcmp((*p_drv)->driver_name, name) != 0))
212 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
213 periph = TAILQ_NEXT(periph, unit_links)) {
214 if (xpt_path_comp(periph->path, path) == 0) {
227 cam_periph_acquire(struct cam_periph *periph)
232 return(CAM_REQ_CMP_ERR);
242 cam_periph_release(struct cam_periph *periph)
250 if ((--periph->refcount == 0)
251 && (periph->flags & CAM_PERIPH_INVALID)) {
252 camperiphfree(periph);
259 * Look for the next unit number that is not currently in use for this
260 * peripheral type starting at "newunit". Also exclude unit numbers that
261 * are reserved by for future "hardwiring" unless we already know that this
262 * is a potential wired device. Only assume that the device is "wired" the
263 * first time through the loop since after that we'll be looking at unit
264 * numbers that did not match a wiring entry.
267 camperiphnextunit(struct periph_driver *p_drv, u_int newunit, int wired,
268 path_id_t pathid, target_id_t target, lun_id_t lun)
270 struct cam_periph *periph;
271 char *periph_name, *strval;
277 periph_name = p_drv->driver_name;
280 for (periph = TAILQ_FIRST(&p_drv->units);
281 periph != NULL && periph->unit_number != newunit;
282 periph = TAILQ_NEXT(periph, unit_links))
285 if (periph != NULL && periph->unit_number == newunit) {
287 xpt_print_path(periph->path);
288 printf("Duplicate Wired Device entry!\n");
289 xpt_print_path(periph->path);
290 printf("Second device (%s device at scbus%d "
291 "target %d lun %d) will not be wired\n",
292 periph_name, pathid, target, lun);
301 * Don't match entries like "da 4" as a wired down
302 * device, but do match entries like "da 4 target 5"
303 * or even "da 4 scbus 1".
306 while ((i = resource_locate(i, periph_name)) != -1) {
307 dname = resource_query_name(i);
308 dunit = resource_query_unit(i);
309 /* if no "target" and no specific scbus, skip */
310 if (resource_int_value(dname, dunit, "target", &val) &&
311 (resource_string_value(dname, dunit, "at",&strval)||
312 strcmp(strval, "scbus") == 0))
314 if (newunit == dunit)
325 camperiphunit(struct periph_driver *p_drv, path_id_t pathid,
326 target_id_t target, lun_id_t lun)
329 int hit, i, val, dunit;
331 char pathbuf[32], *strval, *periph_name;
335 periph_name = p_drv->driver_name;
336 snprintf(pathbuf, sizeof(pathbuf), "scbus%d", pathid);
338 for (hit = 0; (i = resource_locate(i, periph_name)) != -1; hit = 0) {
339 dname = resource_query_name(i);
340 dunit = resource_query_unit(i);
341 if (resource_string_value(dname, dunit, "at", &strval) == 0) {
342 if (strcmp(strval, pathbuf) != 0)
346 if (resource_int_value(dname, dunit, "target", &val) == 0) {
351 if (resource_int_value(dname, dunit, "lun", &val) == 0) {
363 * Either start from 0 looking for the next unit or from
364 * the unit number given in the resource config. This way,
365 * if we have wildcard matches, we don't return the same
368 unit = camperiphnextunit(p_drv, unit, /*wired*/hit, pathid,
375 cam_periph_invalidate(struct cam_periph *periph)
381 * We only call this routine the first time a peripheral is
382 * invalidated. The oninvalidate() routine is always called at
385 if (((periph->flags & CAM_PERIPH_INVALID) == 0)
386 && (periph->periph_oninval != NULL))
387 periph->periph_oninval(periph);
389 periph->flags |= CAM_PERIPH_INVALID;
390 periph->flags &= ~CAM_PERIPH_NEW_DEV_FOUND;
392 if (periph->refcount == 0)
393 camperiphfree(periph);
394 else if (periph->refcount < 0)
395 printf("cam_invalidate_periph: refcount < 0!!\n");
400 camperiphfree(struct cam_periph *periph)
403 struct periph_driver **p_drv;
405 for (p_drv = (struct periph_driver **)periphdriver_set.ls_items;
406 *p_drv != NULL; p_drv++) {
407 if (strcmp((*p_drv)->driver_name, periph->periph_name) == 0)
411 if (periph->periph_dtor != NULL)
412 periph->periph_dtor(periph);
415 TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
416 (*p_drv)->generation++;
419 xpt_remove_periph(periph);
421 if (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) {
425 switch (periph->deferred_ac) {
426 case AC_FOUND_DEVICE:
427 ccb.ccb_h.func_code = XPT_GDEV_TYPE;
428 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/ 1);
432 case AC_PATH_REGISTERED:
433 ccb.ccb_h.func_code = XPT_PATH_INQ;
434 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/ 1);
442 periph->deferred_callback(NULL, periph->deferred_ac,
445 xpt_free_path(periph->path);
446 free(periph, M_DEVBUF);
450 * Wait interruptibly for an exclusive lock.
453 cam_periph_lock(struct cam_periph *periph, int flags)
457 while ((periph->flags & CAM_PERIPH_LOCKED) != 0) {
458 periph->flags |= CAM_PERIPH_LOCK_WANTED;
459 if ((error = tsleep(periph, flags, "caplck", 0)) != 0)
463 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
466 periph->flags |= CAM_PERIPH_LOCKED;
471 * Unlock and wake up any waiters.
474 cam_periph_unlock(struct cam_periph *periph)
476 periph->flags &= ~CAM_PERIPH_LOCKED;
477 if ((periph->flags & CAM_PERIPH_LOCK_WANTED) != 0) {
478 periph->flags &= ~CAM_PERIPH_LOCK_WANTED;
482 cam_periph_release(periph);
486 * Map user virtual pointers into kernel virtual address space, so we can
487 * access the memory. This won't work on physical pointers, for now it's
488 * up to the caller to check for that. (XXX KDM -- should we do that here
489 * instead?) This also only works for up to MAXPHYS memory. Since we use
490 * buffers to map stuff in and out, we're limited to the buffer size.
493 cam_periph_mapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
496 int flags[CAM_PERIPH_MAXMAPS];
497 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
498 u_int32_t lengths[CAM_PERIPH_MAXMAPS];
499 u_int32_t dirs[CAM_PERIPH_MAXMAPS];
501 switch(ccb->ccb_h.func_code) {
503 if (ccb->cdm.match_buf_len == 0) {
504 printf("cam_periph_mapmem: invalid match buffer "
508 if (ccb->cdm.pattern_buf_len > 0) {
509 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
510 lengths[0] = ccb->cdm.pattern_buf_len;
511 dirs[0] = CAM_DIR_OUT;
512 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
513 lengths[1] = ccb->cdm.match_buf_len;
514 dirs[1] = CAM_DIR_IN;
517 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
518 lengths[0] = ccb->cdm.match_buf_len;
519 dirs[0] = CAM_DIR_IN;
524 case XPT_CONT_TARGET_IO:
525 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
528 data_ptrs[0] = &ccb->csio.data_ptr;
529 lengths[0] = ccb->csio.dxfer_len;
530 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
535 break; /* NOTREACHED */
539 * Check the transfer length and permissions first, so we don't
540 * have to unmap any previously mapped buffers.
542 for (i = 0; i < numbufs; i++) {
547 * The userland data pointer passed in may not be page
548 * aligned. vmapbuf() truncates the address to a page
549 * boundary, so if the address isn't page aligned, we'll
550 * need enough space for the given transfer length, plus
551 * whatever extra space is necessary to make it to the page
555 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)) > DFLTPHYS){
556 printf("cam_periph_mapmem: attempt to map %lu bytes, "
557 "which is greater than DFLTPHYS(%d)\n",
559 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)),
564 if (dirs[i] & CAM_DIR_OUT) {
566 if (!useracc(*data_ptrs[i], lengths[i],
568 printf("cam_periph_mapmem: error, "
569 "address %p, length %lu isn't "
570 "user accessible for READ\n",
571 (void *)*data_ptrs[i],
578 * XXX this check is really bogus, since B_WRITE currently
579 * is all 0's, and so it is "set" all the time.
581 if (dirs[i] & CAM_DIR_IN) {
583 if (!useracc(*data_ptrs[i], lengths[i],
585 printf("cam_periph_mapmem: error, "
586 "address %p, length %lu isn't "
587 "user accessible for WRITE\n",
588 (void *)*data_ptrs[i],
597 for (i = 0; i < numbufs; i++) {
601 mapinfo->bp[i] = getpbuf(NULL);
603 /* save the buffer's data address */
604 mapinfo->bp[i]->b_saveaddr = mapinfo->bp[i]->b_data;
606 /* put our pointer in the data slot */
607 mapinfo->bp[i]->b_data = *data_ptrs[i];
609 /* set the transfer length, we know it's < DFLTPHYS */
610 mapinfo->bp[i]->b_bufsize = lengths[i];
613 mapinfo->bp[i]->b_flags = flags[i] | B_PHYS;
615 /* map the buffer into kernel memory */
616 if (vmapbuf(mapinfo->bp[i]) < 0) {
617 printf("cam_periph_mapmem: error, "
618 "address %p, length %lu isn't "
619 "user accessible any more\n",
620 (void *)*data_ptrs[i],
622 for (j = 0; j < i; ++j) {
623 *data_ptrs[j] = mapinfo->bp[j]->b_saveaddr;
624 mapinfo->bp[j]->b_flags &= ~B_PHYS;
625 relpbuf(mapinfo->bp[j], NULL);
630 /* set our pointer to the new mapped area */
631 *data_ptrs[i] = mapinfo->bp[i]->b_data;
633 mapinfo->num_bufs_used++;
640 * Unmap memory segments mapped into kernel virtual address space by
641 * cam_periph_mapmem().
644 cam_periph_unmapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
647 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
649 if (mapinfo->num_bufs_used <= 0) {
650 /* allow ourselves to be swapped once again */
654 switch (ccb->ccb_h.func_code) {
656 numbufs = min(mapinfo->num_bufs_used, 2);
659 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
661 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
662 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
666 case XPT_CONT_TARGET_IO:
667 data_ptrs[0] = &ccb->csio.data_ptr;
668 numbufs = min(mapinfo->num_bufs_used, 1);
671 /* allow ourselves to be swapped once again */
673 break; /* NOTREACHED */
676 for (i = 0; i < numbufs; i++) {
677 /* Set the user's pointer back to the original value */
678 *data_ptrs[i] = mapinfo->bp[i]->b_saveaddr;
680 /* unmap the buffer */
681 vunmapbuf(mapinfo->bp[i]);
683 /* clear the flags we set above */
684 mapinfo->bp[i]->b_flags &= ~B_PHYS;
686 /* release the buffer */
687 relpbuf(mapinfo->bp[i], NULL);
690 /* allow ourselves to be swapped once again */
694 cam_periph_getccb(struct cam_periph *periph, u_int32_t priority)
696 struct ccb_hdr *ccb_h;
699 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdgetccb\n"));
703 while (periph->ccb_list.slh_first == NULL) {
704 if (periph->immediate_priority > priority)
705 periph->immediate_priority = priority;
706 xpt_schedule(periph, priority);
707 if ((periph->ccb_list.slh_first != NULL)
708 && (periph->ccb_list.slh_first->pinfo.priority == priority))
710 tsleep(&periph->ccb_list, 0, "cgticb", 0);
713 ccb_h = periph->ccb_list.slh_first;
714 SLIST_REMOVE_HEAD(&periph->ccb_list, periph_links.sle);
716 return ((union ccb *)ccb_h);
720 cam_periph_ccbwait(union ccb *ccb)
725 if ((ccb->ccb_h.pinfo.index != CAM_UNQUEUED_INDEX)
726 || ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG))
727 tsleep(&ccb->ccb_h.cbfcnp, 0, "cbwait", 0);
733 cam_periph_ioctl(struct cam_periph *periph, int cmd, caddr_t addr,
734 int (*error_routine)(union ccb *ccb,
736 u_int32_t sense_flags))
746 ccb = cam_periph_getccb(periph, /* priority */ 1);
747 xpt_setup_ccb(&ccb->ccb_h,
750 ccb->ccb_h.func_code = XPT_GDEVLIST;
753 * Basically, the point of this is that we go through
754 * getting the list of devices, until we find a passthrough
755 * device. In the current version of the CAM code, the
756 * only way to determine what type of device we're dealing
757 * with is by its name.
761 ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS;
762 while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) {
764 /* we want the next device in the list */
766 if (strncmp(ccb->cgdl.periph_name,
772 if ((ccb->cgdl.status == CAM_GDEVLIST_LAST_DEVICE) &&
774 ccb->cgdl.periph_name[0] = '\0';
775 ccb->cgdl.unit_number = 0;
780 /* copy the result back out */
781 bcopy(ccb, addr, sizeof(union ccb));
783 /* and release the ccb */
784 xpt_release_ccb(ccb);
795 cam_periph_runccb(union ccb *ccb,
796 int (*error_routine)(union ccb *ccb,
798 u_int32_t sense_flags),
799 cam_flags camflags, u_int32_t sense_flags,
807 * If the user has supplied a stats structure, and if we understand
808 * this particular type of ccb, record the transaction start.
810 if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO))
811 devstat_start_transaction(ds);
816 cam_periph_ccbwait(ccb);
817 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
819 else if (error_routine != NULL)
820 error = (*error_routine)(ccb, camflags, sense_flags);
824 } while (error == ERESTART);
826 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
827 cam_release_devq(ccb->ccb_h.path,
831 /* getcount_only */ FALSE);
833 if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO))
834 devstat_end_transaction(ds,
836 ccb->csio.tag_action & 0xf,
837 ((ccb->ccb_h.flags & CAM_DIR_MASK) ==
838 CAM_DIR_NONE) ? DEVSTAT_NO_DATA :
839 (ccb->ccb_h.flags & CAM_DIR_OUT) ?
847 cam_freeze_devq(struct cam_path *path)
849 struct ccb_hdr ccb_h;
851 xpt_setup_ccb(&ccb_h, path, /*priority*/1);
852 ccb_h.func_code = XPT_NOOP;
853 ccb_h.flags = CAM_DEV_QFREEZE;
854 xpt_action((union ccb *)&ccb_h);
858 cam_release_devq(struct cam_path *path, u_int32_t relsim_flags,
859 u_int32_t openings, u_int32_t timeout,
862 struct ccb_relsim crs;
864 xpt_setup_ccb(&crs.ccb_h, path,
866 crs.ccb_h.func_code = XPT_REL_SIMQ;
867 crs.ccb_h.flags = getcount_only ? CAM_DEV_QFREEZE : 0;
868 crs.release_flags = relsim_flags;
869 crs.openings = openings;
870 crs.release_timeout = timeout;
871 xpt_action((union ccb *)&crs);
872 return (crs.qfrozen_cnt);
875 #define saved_ccb_ptr ppriv_ptr0
877 camperiphdone(struct cam_periph *periph, union ccb *done_ccb)
882 struct scsi_start_stop_unit *scsi_cmd;
883 u_int32_t relsim_flags, timeout;
884 u_int32_t qfrozen_cnt;
886 status = done_ccb->ccb_h.status;
887 frozen = (status & CAM_DEV_QFRZN) != 0;
888 sense = (status & CAM_AUTOSNS_VALID) != 0;
889 status &= CAM_STATUS_MASK;
895 * Unfreeze the queue once if it is already frozen..
898 qfrozen_cnt = cam_release_devq(done_ccb->ccb_h.path,
910 * If we have successfully taken a device from the not
911 * ready to ready state, re-scan the device and re-get the
912 * inquiry information. Many devices (mostly disks) don't
913 * properly report their inquiry information unless they
916 if (done_ccb->ccb_h.func_code == XPT_SCSI_IO) {
917 scsi_cmd = (struct scsi_start_stop_unit *)
918 &done_ccb->csio.cdb_io.cdb_bytes;
920 if (scsi_cmd->opcode == START_STOP_UNIT)
921 xpt_async(AC_INQ_CHANGED,
922 done_ccb->ccb_h.path, NULL);
924 bcopy(done_ccb->ccb_h.saved_ccb_ptr, done_ccb,
927 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
929 xpt_action(done_ccb);
932 case CAM_SCSI_STATUS_ERROR:
933 scsi_cmd = (struct scsi_start_stop_unit *)
934 &done_ccb->csio.cdb_io.cdb_bytes;
936 struct scsi_sense_data *sense;
937 int error_code, sense_key, asc, ascq;
939 sense = &done_ccb->csio.sense_data;
940 scsi_extract_sense(sense, &error_code,
941 &sense_key, &asc, &ascq);
944 * If the error is "invalid field in CDB",
945 * and the load/eject flag is set, turn the
946 * flag off and try again. This is just in
947 * case the drive in question barfs on the
948 * load eject flag. The CAM code should set
949 * the load/eject flag by default for
954 * Should we check to see what the specific
955 * scsi status is?? Or does it not matter
956 * since we already know that there was an
957 * error, and we know what the specific
958 * error code was, and we know what the
961 if ((scsi_cmd->opcode == START_STOP_UNIT) &&
962 ((scsi_cmd->how & SSS_LOEJ) != 0) &&
963 (asc == 0x24) && (ascq == 0x00) &&
964 (done_ccb->ccb_h.retry_count > 0)) {
966 scsi_cmd->how &= ~SSS_LOEJ;
968 xpt_action(done_ccb);
970 } else if (done_ccb->ccb_h.retry_count > 0) {
972 * In this case, the error recovery
973 * command failed, but we've got
974 * some retries left on it. Give
978 /* set the timeout to .5 sec */
980 RELSIM_RELEASE_AFTER_TIMEOUT;
983 xpt_action(done_ccb);
989 * Copy the original CCB back and
990 * send it back to the caller.
992 bcopy(done_ccb->ccb_h.saved_ccb_ptr,
993 done_ccb, sizeof(union ccb));
995 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
997 xpt_action(done_ccb);
1001 * Eh?? The command failed, but we don't
1002 * have any sense. What's up with that?
1003 * Fire the CCB again to return it to the
1006 bcopy(done_ccb->ccb_h.saved_ccb_ptr,
1007 done_ccb, sizeof(union ccb));
1009 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1011 xpt_action(done_ccb);
1016 bcopy(done_ccb->ccb_h.saved_ccb_ptr, done_ccb,
1019 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1021 xpt_action(done_ccb);
1026 /* decrement the retry count */
1027 if (done_ccb->ccb_h.retry_count > 0)
1028 done_ccb->ccb_h.retry_count--;
1030 qfrozen_cnt = cam_release_devq(done_ccb->ccb_h.path,
1031 /*relsim_flags*/relsim_flags,
1034 /*getcount_only*/0);
1038 * Generic Async Event handler. Peripheral drivers usually
1039 * filter out the events that require personal attention,
1040 * and leave the rest to this function.
1043 cam_periph_async(struct cam_periph *periph, u_int32_t code,
1044 struct cam_path *path, void *arg)
1047 case AC_LOST_DEVICE:
1048 cam_periph_invalidate(periph);
1053 cam_periph_bus_settle(periph, SCSI_DELAY);
1062 cam_periph_bus_settle(struct cam_periph *periph, u_int bus_settle)
1064 struct ccb_getdevstats cgds;
1066 xpt_setup_ccb(&cgds.ccb_h, periph->path, /*priority*/1);
1067 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1068 xpt_action((union ccb *)&cgds);
1069 cam_periph_freeze_after_event(periph, &cgds.last_reset, bus_settle);
1073 cam_periph_freeze_after_event(struct cam_periph *periph,
1074 struct timeval* event_time, u_int duration_ms)
1076 struct timeval delta;
1077 struct timeval duration_tv;
1083 timevalsub(&delta, event_time);
1084 duration_tv.tv_sec = duration_ms / 1000;
1085 duration_tv.tv_usec = (duration_ms % 1000) * 1000;
1086 if (timevalcmp(&delta, &duration_tv, <)) {
1087 timevalsub(&duration_tv, &delta);
1089 duration_ms = duration_tv.tv_sec * 1000;
1090 duration_ms += duration_tv.tv_usec / 1000;
1091 cam_freeze_devq(periph->path);
1092 cam_release_devq(periph->path,
1093 RELSIM_RELEASE_AFTER_TIMEOUT,
1095 /*timeout*/duration_ms,
1096 /*getcount_only*/0);
1102 * Generic error handler. Peripheral drivers usually filter
1103 * out the errors that they handle in a unique mannor, then
1104 * call this function.
1107 cam_periph_error(union ccb *ccb, cam_flags camflags,
1108 u_int32_t sense_flags, union ccb *save_ccb)
1116 u_int32_t relsim_flags;
1119 status = ccb->ccb_h.status;
1120 frozen = (status & CAM_DEV_QFRZN) != 0;
1121 sense = (status & CAM_AUTOSNS_VALID) != 0;
1122 status &= CAM_STATUS_MASK;
1127 /* decrement the number of retries */
1128 retry = ccb->ccb_h.retry_count > 0;
1130 ccb->ccb_h.retry_count--;
1133 case CAM_AUTOSENSE_FAIL:
1134 case CAM_SCSI_STATUS_ERROR:
1136 switch (ccb->csio.scsi_status) {
1137 case SCSI_STATUS_OK:
1138 case SCSI_STATUS_COND_MET:
1139 case SCSI_STATUS_INTERMED:
1140 case SCSI_STATUS_INTERMED_COND_MET:
1143 case SCSI_STATUS_CMD_TERMINATED:
1144 case SCSI_STATUS_CHECK_COND:
1146 struct scsi_sense_data *sense;
1147 int error_code, sense_key, asc, ascq;
1148 struct cam_periph *periph;
1149 scsi_sense_action err_action;
1150 struct ccb_getdev cgd;
1152 sense = &ccb->csio.sense_data;
1153 scsi_extract_sense(sense, &error_code,
1154 &sense_key, &asc, &ascq);
1155 periph = xpt_path_periph(ccb->ccb_h.path);
1158 * Grab the inquiry data for this device.
1160 xpt_setup_ccb(&cgd.ccb_h, ccb->ccb_h.path,
1162 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1163 xpt_action((union ccb *)&cgd);
1165 err_action = scsi_error_action(asc, ascq,
1169 * Send a Test Unit Ready to the device.
1170 * If the 'many' flag is set, we send 120
1171 * test unit ready commands, one every half
1172 * second. Otherwise, we just send one TUR.
1173 * We only want to do this if the retry
1174 * count has not been exhausted.
1176 if (((err_action & SS_MASK) == SS_TUR)
1178 && ccb->ccb_h.retry_count > 0) {
1181 * Since error recovery is already
1182 * in progress, don't attempt to
1183 * process this error. It is probably
1184 * related to the error that caused
1185 * the currently active error recovery
1186 * action. Also, we only have
1187 * space for one saved CCB, so if we
1188 * had two concurrent error recovery
1189 * actions, we would end up
1190 * over-writing one error recovery
1191 * CCB with another one.
1194 CAM_PERIPH_RECOVERY_INPROG) {
1200 CAM_PERIPH_RECOVERY_INPROG;
1202 /* decrement the number of retries */
1204 SSQ_DECREMENT_COUNT) != 0) {
1206 ccb->ccb_h.retry_count--;
1209 bcopy(ccb, save_ccb, sizeof(*save_ccb));
1212 * We retry this one every half
1213 * second for a minute. If the
1214 * device hasn't become ready in a
1215 * minute's time, it's unlikely to
1216 * ever become ready. If the table
1217 * doesn't specify SSQ_MANY, we can
1218 * only try this once. Oh well.
1220 if ((err_action & SSQ_MANY) != 0)
1221 scsi_test_unit_ready(&ccb->csio,
1228 scsi_test_unit_ready(&ccb->csio,
1235 /* release the queue after .5 sec. */
1237 RELSIM_RELEASE_AFTER_TIMEOUT;
1240 * Drop the priority to 0 so that
1241 * we are the first to execute. Also
1242 * freeze the queue after this command
1243 * is sent so that we can restore the
1244 * old csio and have it queued in the
1245 * proper order before we let normal
1246 * transactions go to the drive.
1248 ccb->ccb_h.pinfo.priority = 0;
1249 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1252 * Save a pointer to the original
1253 * CCB in the new CCB.
1255 ccb->ccb_h.saved_ccb_ptr = save_ccb;
1260 * Send a start unit command to the device,
1261 * and then retry the command. We only
1262 * want to do this if the retry count has
1263 * not been exhausted. If the user
1264 * specified 0 retries, then we follow
1265 * their request and do not retry.
1267 else if (((err_action & SS_MASK) == SS_START)
1269 && ccb->ccb_h.retry_count > 0) {
1273 * Only one error recovery action
1274 * at a time. See above.
1277 CAM_PERIPH_RECOVERY_INPROG) {
1283 CAM_PERIPH_RECOVERY_INPROG;
1285 /* decrement the number of retries */
1287 ccb->ccb_h.retry_count--;
1290 * Check for removable media and
1291 * set load/eject flag
1294 if (SID_IS_REMOVABLE(&cgd.inq_data))
1300 * Attempt to start the drive up.
1302 * Save the current ccb so it can
1303 * be restored and retried once the
1304 * drive is started up.
1306 bcopy(ccb, save_ccb, sizeof(*save_ccb));
1308 scsi_start_stop(&ccb->csio,
1318 * Drop the priority to 0 so that
1319 * we are the first to execute. Also
1320 * freeze the queue after this command
1321 * is sent so that we can restore the
1322 * old csio and have it queued in the
1323 * proper order before we let normal
1324 * transactions go to the drive.
1326 ccb->ccb_h.pinfo.priority = 0;
1327 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1330 * Save a pointer to the original
1331 * CCB in the new CCB.
1333 ccb->ccb_h.saved_ccb_ptr = save_ccb;
1336 } else if ((sense_flags & SF_RETRY_UA) != 0) {
1338 * XXX KDM this is a *horrible*
1341 error = scsi_interpret_sense(ccb,
1350 * Theoretically, this code should send a
1351 * test unit ready to the given device, and
1352 * if it returns and error, send a start
1353 * unit command. Since we don't yet have
1354 * the capability to do two-command error
1355 * recovery, just send a start unit.
1358 else if (((err_action & SS_MASK) == SS_TURSTART)
1360 && ccb->ccb_h.retry_count > 0) {
1364 * Only one error recovery action
1365 * at a time. See above.
1368 CAM_PERIPH_RECOVERY_INPROG) {
1374 CAM_PERIPH_RECOVERY_INPROG;
1376 /* decrement the number of retries */
1378 ccb->ccb_h.retry_count--;
1381 * Check for removable media and
1382 * set load/eject flag
1385 if (SID_IS_REMOVABLE(&cgd.inq_data))
1391 * Attempt to start the drive up.
1393 * Save the current ccb so it can
1394 * be restored and retried once the
1395 * drive is started up.
1397 bcopy(ccb, save_ccb, sizeof(*save_ccb));
1399 scsi_start_stop(&ccb->csio,
1409 /* release the queue after .5 sec. */
1411 RELSIM_RELEASE_AFTER_TIMEOUT;
1414 * Drop the priority to 0 so that
1415 * we are the first to execute. Also
1416 * freeze the queue after this command
1417 * is sent so that we can restore the
1418 * old csio and have it queued in the
1419 * proper order before we let normal
1420 * transactions go to the drive.
1422 ccb->ccb_h.pinfo.priority = 0;
1423 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1426 * Save a pointer to the original
1427 * CCB in the new CCB.
1429 ccb->ccb_h.saved_ccb_ptr = save_ccb;
1433 error = scsi_interpret_sense(ccb,
1440 } else if (ccb->csio.scsi_status ==
1441 SCSI_STATUS_CHECK_COND
1442 && status != CAM_AUTOSENSE_FAIL) {
1443 /* no point in decrementing the retry count */
1444 panic("cam_periph_error: scsi status of "
1445 "CHECK COND returned but no sense "
1446 "information is availible. "
1447 "Controller should have returned "
1448 "CAM_AUTOSENSE_FAILED");
1451 } else if (ccb->ccb_h.retry_count == 0) {
1453 * XXX KDM shouldn't there be a better
1454 * argument to return??
1458 /* decrement the number of retries */
1459 retry = ccb->ccb_h.retry_count > 0;
1461 ccb->ccb_h.retry_count--;
1463 * If it was aborted with no
1464 * clue as to the reason, just
1470 case SCSI_STATUS_QUEUE_FULL:
1473 struct ccb_getdevstats cgds;
1476 * First off, find out what the current
1477 * transaction counts are.
1479 xpt_setup_ccb(&cgds.ccb_h,
1482 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1483 xpt_action((union ccb *)&cgds);
1486 * If we were the only transaction active, treat
1487 * the QUEUE FULL as if it were a BUSY condition.
1489 if (cgds.dev_active != 0) {
1493 * Reduce the number of openings to
1494 * be 1 less than the amount it took
1495 * to get a queue full bounded by the
1496 * minimum allowed tag count for this
1500 cgds.dev_active+cgds.dev_openings;
1501 openings = cgds.dev_active;
1502 if (openings < cgds.mintags)
1503 openings = cgds.mintags;
1504 if (openings < total_openings)
1505 relsim_flags = RELSIM_ADJUST_OPENINGS;
1508 * Some devices report queue full for
1509 * temporary resource shortages. For
1510 * this reason, we allow a minimum
1511 * tag count to be entered via a
1512 * quirk entry to prevent the queue
1513 * count on these devices from falling
1514 * to a pessimisticly low value. We
1515 * still wait for the next successful
1516 * completion, however, before queueing
1517 * more transactions to the device.
1520 RELSIM_RELEASE_AFTER_CMDCMPLT;
1528 case SCSI_STATUS_BUSY:
1530 * Restart the queue after either another
1531 * command completes or a 1 second timeout.
1532 * If we have any retries left, that is.
1534 retry = ccb->ccb_h.retry_count > 0;
1536 ccb->ccb_h.retry_count--;
1538 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT
1539 | RELSIM_RELEASE_AFTER_CMDCMPLT;
1545 case SCSI_STATUS_RESERV_CONFLICT:
1553 case CAM_REQ_CMP_ERR:
1554 case CAM_CMD_TIMEOUT:
1555 case CAM_UNEXP_BUSFREE:
1556 case CAM_UNCOR_PARITY:
1557 case CAM_DATA_RUN_ERR:
1558 /* decrement the number of retries */
1559 retry = ccb->ccb_h.retry_count > 0;
1561 ccb->ccb_h.retry_count--;
1569 case CAM_MSG_REJECT_REC:
1570 /* XXX Don't know that these are correct */
1573 case CAM_SEL_TIMEOUT:
1577 * A single selection timeout should not be enough
1578 * to invalidate a device. We should retry for multiple
1579 * seconds assuming this isn't a probe. We'll probably
1580 * need a special flag for that.
1583 struct cam_path *newpath;
1585 /* Should we do more if we can't create the path?? */
1586 if (xpt_create_path(&newpath, xpt_path_periph(ccb->ccb_h.path),
1587 xpt_path_path_id(ccb->ccb_h.path),
1588 xpt_path_target_id(ccb->ccb_h.path),
1589 CAM_LUN_WILDCARD) != CAM_REQ_CMP)
1592 * Let peripheral drivers know that this device has gone
1595 xpt_async(AC_LOST_DEVICE, newpath, NULL);
1596 xpt_free_path(newpath);
1598 if ((sense_flags & SF_RETRY_SELTO) != 0) {
1599 retry = ccb->ccb_h.retry_count > 0;
1601 ccb->ccb_h.retry_count--;
1604 * Wait half a second to give the device
1605 * time to recover before we try again.
1607 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1617 case CAM_REQ_INVALID:
1618 case CAM_PATH_INVALID:
1619 case CAM_DEV_NOT_THERE:
1621 case CAM_PROVIDE_FAIL:
1622 case CAM_REQ_TOO_BIG:
1625 case CAM_SCSI_BUS_RESET:
1627 case CAM_REQUEUE_REQ:
1628 /* Unconditional requeue, dammit */
1631 case CAM_RESRC_UNAVAIL:
1635 /* decrement the number of retries */
1636 retry = ccb->ccb_h.retry_count > 0;
1638 ccb->ccb_h.retry_count--;
1641 /* Check the sense codes */
1647 /* Attempt a retry */
1648 if (error == ERESTART || error == 0) {
1650 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1652 if (error == ERESTART)
1656 cam_release_devq(ccb->ccb_h.path,
1660 /*getcount_only*/0);