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.9 2005/03/15 20:42:12 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 (*p_drv == NULL) {
404 printf("camperiphfree: attempt to free "
405 "non-existant periph: %s\n", periph->periph_name);
409 if (periph->periph_dtor != NULL)
410 periph->periph_dtor(periph);
413 TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
414 (*p_drv)->generation++;
417 xpt_remove_periph(periph);
419 if (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) {
423 switch (periph->deferred_ac) {
424 case AC_FOUND_DEVICE:
425 ccb.ccb_h.func_code = XPT_GDEV_TYPE;
426 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/ 1);
430 case AC_PATH_REGISTERED:
431 ccb.ccb_h.func_code = XPT_PATH_INQ;
432 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/ 1);
440 periph->deferred_callback(NULL, periph->deferred_ac,
443 xpt_free_path(periph->path);
444 free(periph, M_DEVBUF);
448 * Wait interruptibly for an exclusive lock.
451 cam_periph_lock(struct cam_periph *periph, int flags)
455 while ((periph->flags & CAM_PERIPH_LOCKED) != 0) {
456 periph->flags |= CAM_PERIPH_LOCK_WANTED;
457 if ((error = tsleep(periph, flags, "caplck", 0)) != 0)
461 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
464 periph->flags |= CAM_PERIPH_LOCKED;
469 * Unlock and wake up any waiters.
472 cam_periph_unlock(struct cam_periph *periph)
474 periph->flags &= ~CAM_PERIPH_LOCKED;
475 if ((periph->flags & CAM_PERIPH_LOCK_WANTED) != 0) {
476 periph->flags &= ~CAM_PERIPH_LOCK_WANTED;
480 cam_periph_release(periph);
484 * Map user virtual pointers into kernel virtual address space, so we can
485 * access the memory. This won't work on physical pointers, for now it's
486 * up to the caller to check for that. (XXX KDM -- should we do that here
487 * instead?) This also only works for up to MAXPHYS memory. Since we use
488 * buffers to map stuff in and out, we're limited to the buffer size.
491 cam_periph_mapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
494 int flags[CAM_PERIPH_MAXMAPS];
495 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
496 u_int32_t lengths[CAM_PERIPH_MAXMAPS];
497 u_int32_t dirs[CAM_PERIPH_MAXMAPS];
499 switch(ccb->ccb_h.func_code) {
501 if (ccb->cdm.match_buf_len == 0) {
502 printf("cam_periph_mapmem: invalid match buffer "
506 if (ccb->cdm.pattern_buf_len > 0) {
507 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
508 lengths[0] = ccb->cdm.pattern_buf_len;
509 dirs[0] = CAM_DIR_OUT;
510 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
511 lengths[1] = ccb->cdm.match_buf_len;
512 dirs[1] = CAM_DIR_IN;
515 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
516 lengths[0] = ccb->cdm.match_buf_len;
517 dirs[0] = CAM_DIR_IN;
522 case XPT_CONT_TARGET_IO:
523 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
526 data_ptrs[0] = &ccb->csio.data_ptr;
527 lengths[0] = ccb->csio.dxfer_len;
528 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
533 break; /* NOTREACHED */
537 * Check the transfer length and permissions first, so we don't
538 * have to unmap any previously mapped buffers.
540 for (i = 0; i < numbufs; i++) {
545 * The userland data pointer passed in may not be page
546 * aligned. vmapbuf() truncates the address to a page
547 * boundary, so if the address isn't page aligned, we'll
548 * need enough space for the given transfer length, plus
549 * whatever extra space is necessary to make it to the page
553 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)) > DFLTPHYS){
554 printf("cam_periph_mapmem: attempt to map %lu bytes, "
555 "which is greater than DFLTPHYS(%d)\n",
557 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)),
562 if (dirs[i] & CAM_DIR_OUT) {
564 if (!useracc(*data_ptrs[i], lengths[i],
566 printf("cam_periph_mapmem: error, "
567 "address %p, length %lu isn't "
568 "user accessible for READ\n",
569 (void *)*data_ptrs[i],
576 * XXX this check is really bogus, since B_WRITE currently
577 * is all 0's, and so it is "set" all the time.
579 if (dirs[i] & CAM_DIR_IN) {
581 if (!useracc(*data_ptrs[i], lengths[i],
583 printf("cam_periph_mapmem: error, "
584 "address %p, length %lu isn't "
585 "user accessible for WRITE\n",
586 (void *)*data_ptrs[i],
595 for (i = 0; i < numbufs; i++) {
599 mapinfo->bp[i] = getpbuf(NULL);
601 /* save the buffer's data address */
602 mapinfo->bp[i]->b_saveaddr = mapinfo->bp[i]->b_data;
604 /* put our pointer in the data slot */
605 mapinfo->bp[i]->b_data = *data_ptrs[i];
607 /* set the transfer length, we know it's < DFLTPHYS */
608 mapinfo->bp[i]->b_bufsize = lengths[i];
611 mapinfo->bp[i]->b_flags = flags[i] | B_PHYS;
613 /* map the buffer into kernel memory */
614 if (vmapbuf(mapinfo->bp[i]) < 0) {
615 printf("cam_periph_mapmem: error, "
616 "address %p, length %lu isn't "
617 "user accessible any more\n",
618 (void *)*data_ptrs[i],
620 for (j = 0; j < i; ++j) {
621 *data_ptrs[j] = mapinfo->bp[j]->b_saveaddr;
622 mapinfo->bp[j]->b_flags &= ~B_PHYS;
623 relpbuf(mapinfo->bp[j], NULL);
628 /* set our pointer to the new mapped area */
629 *data_ptrs[i] = mapinfo->bp[i]->b_data;
631 mapinfo->num_bufs_used++;
638 * Unmap memory segments mapped into kernel virtual address space by
639 * cam_periph_mapmem().
642 cam_periph_unmapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
645 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
647 if (mapinfo->num_bufs_used <= 0) {
648 /* allow ourselves to be swapped once again */
652 switch (ccb->ccb_h.func_code) {
654 numbufs = min(mapinfo->num_bufs_used, 2);
657 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
659 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
660 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
664 case XPT_CONT_TARGET_IO:
665 data_ptrs[0] = &ccb->csio.data_ptr;
666 numbufs = min(mapinfo->num_bufs_used, 1);
669 /* allow ourselves to be swapped once again */
671 break; /* NOTREACHED */
674 for (i = 0; i < numbufs; i++) {
675 /* Set the user's pointer back to the original value */
676 *data_ptrs[i] = mapinfo->bp[i]->b_saveaddr;
678 /* unmap the buffer */
679 vunmapbuf(mapinfo->bp[i]);
681 /* clear the flags we set above */
682 mapinfo->bp[i]->b_flags &= ~B_PHYS;
684 /* release the buffer */
685 relpbuf(mapinfo->bp[i], NULL);
688 /* allow ourselves to be swapped once again */
692 cam_periph_getccb(struct cam_periph *periph, u_int32_t priority)
694 struct ccb_hdr *ccb_h;
697 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdgetccb\n"));
701 while (periph->ccb_list.slh_first == NULL) {
702 if (periph->immediate_priority > priority)
703 periph->immediate_priority = priority;
704 xpt_schedule(periph, priority);
705 if ((periph->ccb_list.slh_first != NULL)
706 && (periph->ccb_list.slh_first->pinfo.priority == priority))
708 tsleep(&periph->ccb_list, 0, "cgticb", 0);
711 ccb_h = periph->ccb_list.slh_first;
712 SLIST_REMOVE_HEAD(&periph->ccb_list, periph_links.sle);
714 return ((union ccb *)ccb_h);
718 cam_periph_ccbwait(union ccb *ccb)
723 if ((ccb->ccb_h.pinfo.index != CAM_UNQUEUED_INDEX)
724 || ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG))
725 tsleep(&ccb->ccb_h.cbfcnp, 0, "cbwait", 0);
731 cam_periph_ioctl(struct cam_periph *periph, int cmd, caddr_t addr,
732 int (*error_routine)(union ccb *ccb,
734 u_int32_t sense_flags))
744 ccb = cam_periph_getccb(periph, /* priority */ 1);
745 xpt_setup_ccb(&ccb->ccb_h,
748 ccb->ccb_h.func_code = XPT_GDEVLIST;
751 * Basically, the point of this is that we go through
752 * getting the list of devices, until we find a passthrough
753 * device. In the current version of the CAM code, the
754 * only way to determine what type of device we're dealing
755 * with is by its name.
759 ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS;
760 while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) {
762 /* we want the next device in the list */
764 if (strncmp(ccb->cgdl.periph_name,
770 if ((ccb->cgdl.status == CAM_GDEVLIST_LAST_DEVICE) &&
772 ccb->cgdl.periph_name[0] = '\0';
773 ccb->cgdl.unit_number = 0;
778 /* copy the result back out */
779 bcopy(ccb, addr, sizeof(union ccb));
781 /* and release the ccb */
782 xpt_release_ccb(ccb);
793 cam_periph_runccb(union ccb *ccb,
794 int (*error_routine)(union ccb *ccb,
796 u_int32_t sense_flags),
797 cam_flags camflags, u_int32_t sense_flags,
805 * If the user has supplied a stats structure, and if we understand
806 * this particular type of ccb, record the transaction start.
808 if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO))
809 devstat_start_transaction(ds);
814 cam_periph_ccbwait(ccb);
815 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
817 else if (error_routine != NULL)
818 error = (*error_routine)(ccb, camflags, sense_flags);
822 } while (error == ERESTART);
824 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
825 cam_release_devq(ccb->ccb_h.path,
829 /* getcount_only */ FALSE);
831 if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO))
832 devstat_end_transaction(ds,
834 ccb->csio.tag_action & 0xf,
835 ((ccb->ccb_h.flags & CAM_DIR_MASK) ==
836 CAM_DIR_NONE) ? DEVSTAT_NO_DATA :
837 (ccb->ccb_h.flags & CAM_DIR_OUT) ?
845 cam_freeze_devq(struct cam_path *path)
847 struct ccb_hdr ccb_h;
849 xpt_setup_ccb(&ccb_h, path, /*priority*/1);
850 ccb_h.func_code = XPT_NOOP;
851 ccb_h.flags = CAM_DEV_QFREEZE;
852 xpt_action((union ccb *)&ccb_h);
856 cam_release_devq(struct cam_path *path, u_int32_t relsim_flags,
857 u_int32_t openings, u_int32_t timeout,
860 struct ccb_relsim crs;
862 xpt_setup_ccb(&crs.ccb_h, path,
864 crs.ccb_h.func_code = XPT_REL_SIMQ;
865 crs.ccb_h.flags = getcount_only ? CAM_DEV_QFREEZE : 0;
866 crs.release_flags = relsim_flags;
867 crs.openings = openings;
868 crs.release_timeout = timeout;
869 xpt_action((union ccb *)&crs);
870 return (crs.qfrozen_cnt);
873 #define saved_ccb_ptr ppriv_ptr0
875 camperiphdone(struct cam_periph *periph, union ccb *done_ccb)
880 struct scsi_start_stop_unit *scsi_cmd;
881 u_int32_t relsim_flags, timeout;
882 u_int32_t qfrozen_cnt;
884 status = done_ccb->ccb_h.status;
885 frozen = (status & CAM_DEV_QFRZN) != 0;
886 sense = (status & CAM_AUTOSNS_VALID) != 0;
887 status &= CAM_STATUS_MASK;
893 * Unfreeze the queue once if it is already frozen..
896 qfrozen_cnt = cam_release_devq(done_ccb->ccb_h.path,
908 * If we have successfully taken a device from the not
909 * ready to ready state, re-scan the device and re-get the
910 * inquiry information. Many devices (mostly disks) don't
911 * properly report their inquiry information unless they
914 if (done_ccb->ccb_h.func_code == XPT_SCSI_IO) {
915 scsi_cmd = (struct scsi_start_stop_unit *)
916 &done_ccb->csio.cdb_io.cdb_bytes;
918 if (scsi_cmd->opcode == START_STOP_UNIT)
919 xpt_async(AC_INQ_CHANGED,
920 done_ccb->ccb_h.path, NULL);
922 bcopy(done_ccb->ccb_h.saved_ccb_ptr, done_ccb,
925 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
927 xpt_action(done_ccb);
930 case CAM_SCSI_STATUS_ERROR:
931 scsi_cmd = (struct scsi_start_stop_unit *)
932 &done_ccb->csio.cdb_io.cdb_bytes;
934 struct scsi_sense_data *sense;
935 int error_code, sense_key, asc, ascq;
937 sense = &done_ccb->csio.sense_data;
938 scsi_extract_sense(sense, &error_code,
939 &sense_key, &asc, &ascq);
942 * If the error is "invalid field in CDB",
943 * and the load/eject flag is set, turn the
944 * flag off and try again. This is just in
945 * case the drive in question barfs on the
946 * load eject flag. The CAM code should set
947 * the load/eject flag by default for
952 * Should we check to see what the specific
953 * scsi status is?? Or does it not matter
954 * since we already know that there was an
955 * error, and we know what the specific
956 * error code was, and we know what the
959 if ((scsi_cmd->opcode == START_STOP_UNIT) &&
960 ((scsi_cmd->how & SSS_LOEJ) != 0) &&
961 (asc == 0x24) && (ascq == 0x00) &&
962 (done_ccb->ccb_h.retry_count > 0)) {
964 scsi_cmd->how &= ~SSS_LOEJ;
966 xpt_action(done_ccb);
968 } else if (done_ccb->ccb_h.retry_count > 0) {
970 * In this case, the error recovery
971 * command failed, but we've got
972 * some retries left on it. Give
976 /* set the timeout to .5 sec */
978 RELSIM_RELEASE_AFTER_TIMEOUT;
981 xpt_action(done_ccb);
987 * Copy the original CCB back and
988 * send it back to the caller.
990 bcopy(done_ccb->ccb_h.saved_ccb_ptr,
991 done_ccb, sizeof(union ccb));
993 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
995 xpt_action(done_ccb);
999 * Eh?? The command failed, but we don't
1000 * have any sense. What's up with that?
1001 * Fire the CCB again to return it to the
1004 bcopy(done_ccb->ccb_h.saved_ccb_ptr,
1005 done_ccb, sizeof(union ccb));
1007 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1009 xpt_action(done_ccb);
1014 bcopy(done_ccb->ccb_h.saved_ccb_ptr, done_ccb,
1017 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1019 xpt_action(done_ccb);
1024 /* decrement the retry count */
1025 if (done_ccb->ccb_h.retry_count > 0)
1026 done_ccb->ccb_h.retry_count--;
1028 qfrozen_cnt = cam_release_devq(done_ccb->ccb_h.path,
1029 /*relsim_flags*/relsim_flags,
1032 /*getcount_only*/0);
1036 * Generic Async Event handler. Peripheral drivers usually
1037 * filter out the events that require personal attention,
1038 * and leave the rest to this function.
1041 cam_periph_async(struct cam_periph *periph, u_int32_t code,
1042 struct cam_path *path, void *arg)
1045 case AC_LOST_DEVICE:
1046 cam_periph_invalidate(periph);
1051 cam_periph_bus_settle(periph, SCSI_DELAY);
1060 cam_periph_bus_settle(struct cam_periph *periph, u_int bus_settle)
1062 struct ccb_getdevstats cgds;
1064 xpt_setup_ccb(&cgds.ccb_h, periph->path, /*priority*/1);
1065 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1066 xpt_action((union ccb *)&cgds);
1067 cam_periph_freeze_after_event(periph, &cgds.last_reset, bus_settle);
1071 cam_periph_freeze_after_event(struct cam_periph *periph,
1072 struct timeval* event_time, u_int duration_ms)
1074 struct timeval delta;
1075 struct timeval duration_tv;
1077 microuptime(&delta);
1078 timevalsub(&delta, event_time);
1079 duration_tv.tv_sec = duration_ms / 1000;
1080 duration_tv.tv_usec = (duration_ms % 1000) * 1000;
1081 if (timevalcmp(&delta, &duration_tv, <)) {
1082 timevalsub(&duration_tv, &delta);
1084 duration_ms = duration_tv.tv_sec * 1000;
1085 duration_ms += duration_tv.tv_usec / 1000;
1086 cam_freeze_devq(periph->path);
1087 cam_release_devq(periph->path,
1088 RELSIM_RELEASE_AFTER_TIMEOUT,
1090 /*timeout*/duration_ms,
1091 /*getcount_only*/0);
1097 * Generic error handler. Peripheral drivers usually filter
1098 * out the errors that they handle in a unique mannor, then
1099 * call this function.
1102 cam_periph_error(union ccb *ccb, cam_flags camflags,
1103 u_int32_t sense_flags, union ccb *save_ccb)
1111 u_int32_t relsim_flags;
1114 status = ccb->ccb_h.status;
1115 frozen = (status & CAM_DEV_QFRZN) != 0;
1116 sense = (status & CAM_AUTOSNS_VALID) != 0;
1117 status &= CAM_STATUS_MASK;
1122 /* decrement the number of retries */
1123 retry = ccb->ccb_h.retry_count > 0;
1125 ccb->ccb_h.retry_count--;
1128 case CAM_AUTOSENSE_FAIL:
1129 case CAM_SCSI_STATUS_ERROR:
1131 switch (ccb->csio.scsi_status) {
1132 case SCSI_STATUS_OK:
1133 case SCSI_STATUS_COND_MET:
1134 case SCSI_STATUS_INTERMED:
1135 case SCSI_STATUS_INTERMED_COND_MET:
1138 case SCSI_STATUS_CMD_TERMINATED:
1139 case SCSI_STATUS_CHECK_COND:
1141 struct scsi_sense_data *sense;
1142 int error_code, sense_key, asc, ascq;
1143 struct cam_periph *periph;
1144 scsi_sense_action err_action;
1145 struct ccb_getdev cgd;
1147 sense = &ccb->csio.sense_data;
1148 scsi_extract_sense(sense, &error_code,
1149 &sense_key, &asc, &ascq);
1150 periph = xpt_path_periph(ccb->ccb_h.path);
1153 * Grab the inquiry data for this device.
1155 xpt_setup_ccb(&cgd.ccb_h, ccb->ccb_h.path,
1157 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1158 xpt_action((union ccb *)&cgd);
1160 err_action = scsi_error_action(asc, ascq,
1164 * Send a Test Unit Ready to the device.
1165 * If the 'many' flag is set, we send 120
1166 * test unit ready commands, one every half
1167 * second. Otherwise, we just send one TUR.
1168 * We only want to do this if the retry
1169 * count has not been exhausted.
1171 if (((err_action & SS_MASK) == SS_TUR)
1173 && ccb->ccb_h.retry_count > 0) {
1176 * Since error recovery is already
1177 * in progress, don't attempt to
1178 * process this error. It is probably
1179 * related to the error that caused
1180 * the currently active error recovery
1181 * action. Also, we only have
1182 * space for one saved CCB, so if we
1183 * had two concurrent error recovery
1184 * actions, we would end up
1185 * over-writing one error recovery
1186 * CCB with another one.
1189 CAM_PERIPH_RECOVERY_INPROG) {
1195 CAM_PERIPH_RECOVERY_INPROG;
1197 /* decrement the number of retries */
1199 SSQ_DECREMENT_COUNT) != 0) {
1201 ccb->ccb_h.retry_count--;
1204 bcopy(ccb, save_ccb, sizeof(*save_ccb));
1207 * We retry this one every half
1208 * second for a minute. If the
1209 * device hasn't become ready in a
1210 * minute's time, it's unlikely to
1211 * ever become ready. If the table
1212 * doesn't specify SSQ_MANY, we can
1213 * only try this once. Oh well.
1215 if ((err_action & SSQ_MANY) != 0)
1216 scsi_test_unit_ready(&ccb->csio,
1223 scsi_test_unit_ready(&ccb->csio,
1230 /* release the queue after .5 sec. */
1232 RELSIM_RELEASE_AFTER_TIMEOUT;
1235 * Drop the priority to 0 so that
1236 * we are the first to execute. Also
1237 * freeze the queue after this command
1238 * is sent so that we can restore the
1239 * old csio and have it queued in the
1240 * proper order before we let normal
1241 * transactions go to the drive.
1243 ccb->ccb_h.pinfo.priority = 0;
1244 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1247 * Save a pointer to the original
1248 * CCB in the new CCB.
1250 ccb->ccb_h.saved_ccb_ptr = save_ccb;
1255 * Send a start unit command to the device,
1256 * and then retry the command. We only
1257 * want to do this if the retry count has
1258 * not been exhausted. If the user
1259 * specified 0 retries, then we follow
1260 * their request and do not retry.
1262 else if (((err_action & SS_MASK) == SS_START)
1264 && ccb->ccb_h.retry_count > 0) {
1268 * Only one error recovery action
1269 * at a time. See above.
1272 CAM_PERIPH_RECOVERY_INPROG) {
1278 CAM_PERIPH_RECOVERY_INPROG;
1280 /* decrement the number of retries */
1282 ccb->ccb_h.retry_count--;
1285 * Check for removable media and
1286 * set load/eject flag
1289 if (SID_IS_REMOVABLE(&cgd.inq_data))
1295 * Attempt to start the drive up.
1297 * Save the current ccb so it can
1298 * be restored and retried once the
1299 * drive is started up.
1301 bcopy(ccb, save_ccb, sizeof(*save_ccb));
1303 scsi_start_stop(&ccb->csio,
1313 * Drop the priority to 0 so that
1314 * we are the first to execute. Also
1315 * freeze the queue after this command
1316 * is sent so that we can restore the
1317 * old csio and have it queued in the
1318 * proper order before we let normal
1319 * transactions go to the drive.
1321 ccb->ccb_h.pinfo.priority = 0;
1322 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1325 * Save a pointer to the original
1326 * CCB in the new CCB.
1328 ccb->ccb_h.saved_ccb_ptr = save_ccb;
1331 } else if ((sense_flags & SF_RETRY_UA) != 0) {
1333 * XXX KDM this is a *horrible*
1336 error = scsi_interpret_sense(ccb,
1345 * Theoretically, this code should send a
1346 * test unit ready to the given device, and
1347 * if it returns and error, send a start
1348 * unit command. Since we don't yet have
1349 * the capability to do two-command error
1350 * recovery, just send a start unit.
1353 else if (((err_action & SS_MASK) == SS_TURSTART)
1355 && ccb->ccb_h.retry_count > 0) {
1359 * Only one error recovery action
1360 * at a time. See above.
1363 CAM_PERIPH_RECOVERY_INPROG) {
1369 CAM_PERIPH_RECOVERY_INPROG;
1371 /* decrement the number of retries */
1373 ccb->ccb_h.retry_count--;
1376 * Check for removable media and
1377 * set load/eject flag
1380 if (SID_IS_REMOVABLE(&cgd.inq_data))
1386 * Attempt to start the drive up.
1388 * Save the current ccb so it can
1389 * be restored and retried once the
1390 * drive is started up.
1392 bcopy(ccb, save_ccb, sizeof(*save_ccb));
1394 scsi_start_stop(&ccb->csio,
1404 /* release the queue after .5 sec. */
1406 RELSIM_RELEASE_AFTER_TIMEOUT;
1409 * Drop the priority to 0 so that
1410 * we are the first to execute. Also
1411 * freeze the queue after this command
1412 * is sent so that we can restore the
1413 * old csio and have it queued in the
1414 * proper order before we let normal
1415 * transactions go to the drive.
1417 ccb->ccb_h.pinfo.priority = 0;
1418 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1421 * Save a pointer to the original
1422 * CCB in the new CCB.
1424 ccb->ccb_h.saved_ccb_ptr = save_ccb;
1428 error = scsi_interpret_sense(ccb,
1435 } else if (ccb->csio.scsi_status ==
1436 SCSI_STATUS_CHECK_COND
1437 && status != CAM_AUTOSENSE_FAIL) {
1438 /* no point in decrementing the retry count */
1439 panic("cam_periph_error: scsi status of "
1440 "CHECK COND returned but no sense "
1441 "information is availible. "
1442 "Controller should have returned "
1443 "CAM_AUTOSENSE_FAILED");
1446 } else if (ccb->ccb_h.retry_count == 0) {
1448 * XXX KDM shouldn't there be a better
1449 * argument to return??
1453 /* decrement the number of retries */
1454 retry = ccb->ccb_h.retry_count > 0;
1456 ccb->ccb_h.retry_count--;
1458 * If it was aborted with no
1459 * clue as to the reason, just
1465 case SCSI_STATUS_QUEUE_FULL:
1468 struct ccb_getdevstats cgds;
1471 * First off, find out what the current
1472 * transaction counts are.
1474 xpt_setup_ccb(&cgds.ccb_h,
1477 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1478 xpt_action((union ccb *)&cgds);
1481 * If we were the only transaction active, treat
1482 * the QUEUE FULL as if it were a BUSY condition.
1484 if (cgds.dev_active != 0) {
1488 * Reduce the number of openings to
1489 * be 1 less than the amount it took
1490 * to get a queue full bounded by the
1491 * minimum allowed tag count for this
1495 cgds.dev_active+cgds.dev_openings;
1496 openings = cgds.dev_active;
1497 if (openings < cgds.mintags)
1498 openings = cgds.mintags;
1499 if (openings < total_openings)
1500 relsim_flags = RELSIM_ADJUST_OPENINGS;
1503 * Some devices report queue full for
1504 * temporary resource shortages. For
1505 * this reason, we allow a minimum
1506 * tag count to be entered via a
1507 * quirk entry to prevent the queue
1508 * count on these devices from falling
1509 * to a pessimisticly low value. We
1510 * still wait for the next successful
1511 * completion, however, before queueing
1512 * more transactions to the device.
1515 RELSIM_RELEASE_AFTER_CMDCMPLT;
1523 case SCSI_STATUS_BUSY:
1525 * Restart the queue after either another
1526 * command completes or a 1 second timeout.
1527 * If we have any retries left, that is.
1529 retry = ccb->ccb_h.retry_count > 0;
1531 ccb->ccb_h.retry_count--;
1533 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT
1534 | RELSIM_RELEASE_AFTER_CMDCMPLT;
1540 case SCSI_STATUS_RESERV_CONFLICT:
1548 case CAM_REQ_CMP_ERR:
1549 case CAM_CMD_TIMEOUT:
1550 case CAM_UNEXP_BUSFREE:
1551 case CAM_UNCOR_PARITY:
1552 case CAM_DATA_RUN_ERR:
1553 /* decrement the number of retries */
1554 retry = ccb->ccb_h.retry_count > 0;
1556 ccb->ccb_h.retry_count--;
1564 case CAM_MSG_REJECT_REC:
1565 /* XXX Don't know that these are correct */
1568 case CAM_SEL_TIMEOUT:
1572 * A single selection timeout should not be enough
1573 * to invalidate a device. We should retry for multiple
1574 * seconds assuming this isn't a probe. We'll probably
1575 * need a special flag for that.
1578 struct cam_path *newpath;
1580 /* Should we do more if we can't create the path?? */
1581 if (xpt_create_path(&newpath, xpt_path_periph(ccb->ccb_h.path),
1582 xpt_path_path_id(ccb->ccb_h.path),
1583 xpt_path_target_id(ccb->ccb_h.path),
1584 CAM_LUN_WILDCARD) != CAM_REQ_CMP)
1587 * Let peripheral drivers know that this device has gone
1590 xpt_async(AC_LOST_DEVICE, newpath, NULL);
1591 xpt_free_path(newpath);
1593 if ((sense_flags & SF_RETRY_SELTO) != 0) {
1594 retry = ccb->ccb_h.retry_count > 0;
1596 ccb->ccb_h.retry_count--;
1599 * Wait half a second to give the device
1600 * time to recover before we try again.
1602 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1612 case CAM_REQ_INVALID:
1613 case CAM_PATH_INVALID:
1614 case CAM_DEV_NOT_THERE:
1616 case CAM_PROVIDE_FAIL:
1617 case CAM_REQ_TOO_BIG:
1620 case CAM_SCSI_BUS_RESET:
1622 case CAM_REQUEUE_REQ:
1623 /* Unconditional requeue, dammit */
1626 case CAM_RESRC_UNAVAIL:
1630 /* decrement the number of retries */
1631 retry = ccb->ccb_h.retry_count > 0;
1633 ccb->ccb_h.retry_count--;
1636 /* Check the sense codes */
1642 /* Attempt a retry */
1643 if (error == ERESTART || error == 0) {
1645 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1647 if (error == ERESTART)
1651 cam_release_devq(ccb->ccb_h.path,
1655 /*getcount_only*/0);