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.2 2003/06/17 04:28:18 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 priority)
457 while ((periph->flags & CAM_PERIPH_LOCKED) != 0) {
458 periph->flags |= CAM_PERIPH_LOCK_WANTED;
459 if ((error = tsleep(periph, priority, "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 /* this keeps the current process from getting swapped */
599 * XXX KDM should I use P_NOSWAP instead?
603 for (i = 0; i < numbufs; i++) {
607 mapinfo->bp[i] = getpbuf(NULL);
609 /* save the buffer's data address */
610 mapinfo->bp[i]->b_saveaddr = mapinfo->bp[i]->b_data;
612 /* put our pointer in the data slot */
613 mapinfo->bp[i]->b_data = *data_ptrs[i];
615 /* set the transfer length, we know it's < DFLTPHYS */
616 mapinfo->bp[i]->b_bufsize = lengths[i];
619 mapinfo->bp[i]->b_flags = flags[i] | B_PHYS;
621 /* map the buffer into kernel memory */
622 if (vmapbuf(mapinfo->bp[i]) < 0) {
623 printf("cam_periph_mapmem: error, "
624 "address %p, length %lu isn't "
625 "user accessible any more\n",
626 (void *)*data_ptrs[i],
628 for (j = 0; j < i; ++j) {
629 *data_ptrs[j] = mapinfo->bp[j]->b_saveaddr;
630 mapinfo->bp[j]->b_flags &= ~B_PHYS;
631 relpbuf(mapinfo->bp[j], NULL);
637 /* set our pointer to the new mapped area */
638 *data_ptrs[i] = mapinfo->bp[i]->b_data;
640 mapinfo->num_bufs_used++;
647 * Unmap memory segments mapped into kernel virtual address space by
648 * cam_periph_mapmem().
651 cam_periph_unmapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
654 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
656 if (mapinfo->num_bufs_used <= 0) {
657 /* allow ourselves to be swapped once again */
662 switch (ccb->ccb_h.func_code) {
664 numbufs = min(mapinfo->num_bufs_used, 2);
667 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
669 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
670 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
674 case XPT_CONT_TARGET_IO:
675 data_ptrs[0] = &ccb->csio.data_ptr;
676 numbufs = min(mapinfo->num_bufs_used, 1);
679 /* allow ourselves to be swapped once again */
682 break; /* NOTREACHED */
685 for (i = 0; i < numbufs; i++) {
686 /* Set the user's pointer back to the original value */
687 *data_ptrs[i] = mapinfo->bp[i]->b_saveaddr;
689 /* unmap the buffer */
690 vunmapbuf(mapinfo->bp[i]);
692 /* clear the flags we set above */
693 mapinfo->bp[i]->b_flags &= ~B_PHYS;
695 /* release the buffer */
696 relpbuf(mapinfo->bp[i], NULL);
699 /* allow ourselves to be swapped once again */
704 cam_periph_getccb(struct cam_periph *periph, u_int32_t priority)
706 struct ccb_hdr *ccb_h;
709 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdgetccb\n"));
713 while (periph->ccb_list.slh_first == NULL) {
714 if (periph->immediate_priority > priority)
715 periph->immediate_priority = priority;
716 xpt_schedule(periph, priority);
717 if ((periph->ccb_list.slh_first != NULL)
718 && (periph->ccb_list.slh_first->pinfo.priority == priority))
720 tsleep(&periph->ccb_list, PRIBIO, "cgticb", 0);
723 ccb_h = periph->ccb_list.slh_first;
724 SLIST_REMOVE_HEAD(&periph->ccb_list, periph_links.sle);
726 return ((union ccb *)ccb_h);
730 cam_periph_ccbwait(union ccb *ccb)
735 if ((ccb->ccb_h.pinfo.index != CAM_UNQUEUED_INDEX)
736 || ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG))
737 tsleep(&ccb->ccb_h.cbfcnp, PRIBIO, "cbwait", 0);
743 cam_periph_ioctl(struct cam_periph *periph, int cmd, caddr_t addr,
744 int (*error_routine)(union ccb *ccb,
746 u_int32_t sense_flags))
756 ccb = cam_periph_getccb(periph, /* priority */ 1);
757 xpt_setup_ccb(&ccb->ccb_h,
760 ccb->ccb_h.func_code = XPT_GDEVLIST;
763 * Basically, the point of this is that we go through
764 * getting the list of devices, until we find a passthrough
765 * device. In the current version of the CAM code, the
766 * only way to determine what type of device we're dealing
767 * with is by its name.
771 ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS;
772 while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) {
774 /* we want the next device in the list */
776 if (strncmp(ccb->cgdl.periph_name,
782 if ((ccb->cgdl.status == CAM_GDEVLIST_LAST_DEVICE) &&
784 ccb->cgdl.periph_name[0] = '\0';
785 ccb->cgdl.unit_number = 0;
790 /* copy the result back out */
791 bcopy(ccb, addr, sizeof(union ccb));
793 /* and release the ccb */
794 xpt_release_ccb(ccb);
805 cam_periph_runccb(union ccb *ccb,
806 int (*error_routine)(union ccb *ccb,
808 u_int32_t sense_flags),
809 cam_flags camflags, u_int32_t sense_flags,
817 * If the user has supplied a stats structure, and if we understand
818 * this particular type of ccb, record the transaction start.
820 if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO))
821 devstat_start_transaction(ds);
826 cam_periph_ccbwait(ccb);
827 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
829 else if (error_routine != NULL)
830 error = (*error_routine)(ccb, camflags, sense_flags);
834 } while (error == ERESTART);
836 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
837 cam_release_devq(ccb->ccb_h.path,
841 /* getcount_only */ FALSE);
843 if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO))
844 devstat_end_transaction(ds,
846 ccb->csio.tag_action & 0xf,
847 ((ccb->ccb_h.flags & CAM_DIR_MASK) ==
848 CAM_DIR_NONE) ? DEVSTAT_NO_DATA :
849 (ccb->ccb_h.flags & CAM_DIR_OUT) ?
857 cam_freeze_devq(struct cam_path *path)
859 struct ccb_hdr ccb_h;
861 xpt_setup_ccb(&ccb_h, path, /*priority*/1);
862 ccb_h.func_code = XPT_NOOP;
863 ccb_h.flags = CAM_DEV_QFREEZE;
864 xpt_action((union ccb *)&ccb_h);
868 cam_release_devq(struct cam_path *path, u_int32_t relsim_flags,
869 u_int32_t openings, u_int32_t timeout,
872 struct ccb_relsim crs;
874 xpt_setup_ccb(&crs.ccb_h, path,
876 crs.ccb_h.func_code = XPT_REL_SIMQ;
877 crs.ccb_h.flags = getcount_only ? CAM_DEV_QFREEZE : 0;
878 crs.release_flags = relsim_flags;
879 crs.openings = openings;
880 crs.release_timeout = timeout;
881 xpt_action((union ccb *)&crs);
882 return (crs.qfrozen_cnt);
885 #define saved_ccb_ptr ppriv_ptr0
887 camperiphdone(struct cam_periph *periph, union ccb *done_ccb)
892 struct scsi_start_stop_unit *scsi_cmd;
893 u_int32_t relsim_flags, timeout;
894 u_int32_t qfrozen_cnt;
896 status = done_ccb->ccb_h.status;
897 frozen = (status & CAM_DEV_QFRZN) != 0;
898 sense = (status & CAM_AUTOSNS_VALID) != 0;
899 status &= CAM_STATUS_MASK;
905 * Unfreeze the queue once if it is already frozen..
908 qfrozen_cnt = cam_release_devq(done_ccb->ccb_h.path,
920 * If we have successfully taken a device from the not
921 * ready to ready state, re-scan the device and re-get the
922 * inquiry information. Many devices (mostly disks) don't
923 * properly report their inquiry information unless they
926 if (done_ccb->ccb_h.func_code == XPT_SCSI_IO) {
927 scsi_cmd = (struct scsi_start_stop_unit *)
928 &done_ccb->csio.cdb_io.cdb_bytes;
930 if (scsi_cmd->opcode == START_STOP_UNIT)
931 xpt_async(AC_INQ_CHANGED,
932 done_ccb->ccb_h.path, NULL);
934 bcopy(done_ccb->ccb_h.saved_ccb_ptr, done_ccb,
937 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
939 xpt_action(done_ccb);
942 case CAM_SCSI_STATUS_ERROR:
943 scsi_cmd = (struct scsi_start_stop_unit *)
944 &done_ccb->csio.cdb_io.cdb_bytes;
946 struct scsi_sense_data *sense;
947 int error_code, sense_key, asc, ascq;
949 sense = &done_ccb->csio.sense_data;
950 scsi_extract_sense(sense, &error_code,
951 &sense_key, &asc, &ascq);
954 * If the error is "invalid field in CDB",
955 * and the load/eject flag is set, turn the
956 * flag off and try again. This is just in
957 * case the drive in question barfs on the
958 * load eject flag. The CAM code should set
959 * the load/eject flag by default for
964 * Should we check to see what the specific
965 * scsi status is?? Or does it not matter
966 * since we already know that there was an
967 * error, and we know what the specific
968 * error code was, and we know what the
971 if ((scsi_cmd->opcode == START_STOP_UNIT) &&
972 ((scsi_cmd->how & SSS_LOEJ) != 0) &&
973 (asc == 0x24) && (ascq == 0x00) &&
974 (done_ccb->ccb_h.retry_count > 0)) {
976 scsi_cmd->how &= ~SSS_LOEJ;
978 xpt_action(done_ccb);
980 } else if (done_ccb->ccb_h.retry_count > 0) {
982 * In this case, the error recovery
983 * command failed, but we've got
984 * some retries left on it. Give
988 /* set the timeout to .5 sec */
990 RELSIM_RELEASE_AFTER_TIMEOUT;
993 xpt_action(done_ccb);
999 * Copy the original CCB back and
1000 * send it back to the caller.
1002 bcopy(done_ccb->ccb_h.saved_ccb_ptr,
1003 done_ccb, sizeof(union ccb));
1005 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1007 xpt_action(done_ccb);
1011 * Eh?? The command failed, but we don't
1012 * have any sense. What's up with that?
1013 * Fire the CCB again to return it to the
1016 bcopy(done_ccb->ccb_h.saved_ccb_ptr,
1017 done_ccb, sizeof(union ccb));
1019 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1021 xpt_action(done_ccb);
1026 bcopy(done_ccb->ccb_h.saved_ccb_ptr, done_ccb,
1029 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1031 xpt_action(done_ccb);
1036 /* decrement the retry count */
1037 if (done_ccb->ccb_h.retry_count > 0)
1038 done_ccb->ccb_h.retry_count--;
1040 qfrozen_cnt = cam_release_devq(done_ccb->ccb_h.path,
1041 /*relsim_flags*/relsim_flags,
1044 /*getcount_only*/0);
1048 * Generic Async Event handler. Peripheral drivers usually
1049 * filter out the events that require personal attention,
1050 * and leave the rest to this function.
1053 cam_periph_async(struct cam_periph *periph, u_int32_t code,
1054 struct cam_path *path, void *arg)
1057 case AC_LOST_DEVICE:
1058 cam_periph_invalidate(periph);
1063 cam_periph_bus_settle(periph, SCSI_DELAY);
1072 cam_periph_bus_settle(struct cam_periph *periph, u_int bus_settle)
1074 struct ccb_getdevstats cgds;
1076 xpt_setup_ccb(&cgds.ccb_h, periph->path, /*priority*/1);
1077 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1078 xpt_action((union ccb *)&cgds);
1079 cam_periph_freeze_after_event(periph, &cgds.last_reset, bus_settle);
1083 cam_periph_freeze_after_event(struct cam_periph *periph,
1084 struct timeval* event_time, u_int duration_ms)
1086 struct timeval delta;
1087 struct timeval duration_tv;
1093 timevalsub(&delta, event_time);
1094 duration_tv.tv_sec = duration_ms / 1000;
1095 duration_tv.tv_usec = (duration_ms % 1000) * 1000;
1096 if (timevalcmp(&delta, &duration_tv, <)) {
1097 timevalsub(&duration_tv, &delta);
1099 duration_ms = duration_tv.tv_sec * 1000;
1100 duration_ms += duration_tv.tv_usec / 1000;
1101 cam_freeze_devq(periph->path);
1102 cam_release_devq(periph->path,
1103 RELSIM_RELEASE_AFTER_TIMEOUT,
1105 /*timeout*/duration_ms,
1106 /*getcount_only*/0);
1112 * Generic error handler. Peripheral drivers usually filter
1113 * out the errors that they handle in a unique mannor, then
1114 * call this function.
1117 cam_periph_error(union ccb *ccb, cam_flags camflags,
1118 u_int32_t sense_flags, union ccb *save_ccb)
1126 u_int32_t relsim_flags;
1129 status = ccb->ccb_h.status;
1130 frozen = (status & CAM_DEV_QFRZN) != 0;
1131 sense = (status & CAM_AUTOSNS_VALID) != 0;
1132 status &= CAM_STATUS_MASK;
1137 /* decrement the number of retries */
1138 retry = ccb->ccb_h.retry_count > 0;
1140 ccb->ccb_h.retry_count--;
1143 case CAM_AUTOSENSE_FAIL:
1144 case CAM_SCSI_STATUS_ERROR:
1146 switch (ccb->csio.scsi_status) {
1147 case SCSI_STATUS_OK:
1148 case SCSI_STATUS_COND_MET:
1149 case SCSI_STATUS_INTERMED:
1150 case SCSI_STATUS_INTERMED_COND_MET:
1153 case SCSI_STATUS_CMD_TERMINATED:
1154 case SCSI_STATUS_CHECK_COND:
1156 struct scsi_sense_data *sense;
1157 int error_code, sense_key, asc, ascq;
1158 struct cam_periph *periph;
1159 scsi_sense_action err_action;
1160 struct ccb_getdev cgd;
1162 sense = &ccb->csio.sense_data;
1163 scsi_extract_sense(sense, &error_code,
1164 &sense_key, &asc, &ascq);
1165 periph = xpt_path_periph(ccb->ccb_h.path);
1168 * Grab the inquiry data for this device.
1170 xpt_setup_ccb(&cgd.ccb_h, ccb->ccb_h.path,
1172 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1173 xpt_action((union ccb *)&cgd);
1175 err_action = scsi_error_action(asc, ascq,
1179 * Send a Test Unit Ready to the device.
1180 * If the 'many' flag is set, we send 120
1181 * test unit ready commands, one every half
1182 * second. Otherwise, we just send one TUR.
1183 * We only want to do this if the retry
1184 * count has not been exhausted.
1186 if (((err_action & SS_MASK) == SS_TUR)
1188 && ccb->ccb_h.retry_count > 0) {
1191 * Since error recovery is already
1192 * in progress, don't attempt to
1193 * process this error. It is probably
1194 * related to the error that caused
1195 * the currently active error recovery
1196 * action. Also, we only have
1197 * space for one saved CCB, so if we
1198 * had two concurrent error recovery
1199 * actions, we would end up
1200 * over-writing one error recovery
1201 * CCB with another one.
1204 CAM_PERIPH_RECOVERY_INPROG) {
1210 CAM_PERIPH_RECOVERY_INPROG;
1212 /* decrement the number of retries */
1214 SSQ_DECREMENT_COUNT) != 0) {
1216 ccb->ccb_h.retry_count--;
1219 bcopy(ccb, save_ccb, sizeof(*save_ccb));
1222 * We retry this one every half
1223 * second for a minute. If the
1224 * device hasn't become ready in a
1225 * minute's time, it's unlikely to
1226 * ever become ready. If the table
1227 * doesn't specify SSQ_MANY, we can
1228 * only try this once. Oh well.
1230 if ((err_action & SSQ_MANY) != 0)
1231 scsi_test_unit_ready(&ccb->csio,
1238 scsi_test_unit_ready(&ccb->csio,
1245 /* release the queue after .5 sec. */
1247 RELSIM_RELEASE_AFTER_TIMEOUT;
1250 * Drop the priority to 0 so that
1251 * we are the first to execute. Also
1252 * freeze the queue after this command
1253 * is sent so that we can restore the
1254 * old csio and have it queued in the
1255 * proper order before we let normal
1256 * transactions go to the drive.
1258 ccb->ccb_h.pinfo.priority = 0;
1259 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1262 * Save a pointer to the original
1263 * CCB in the new CCB.
1265 ccb->ccb_h.saved_ccb_ptr = save_ccb;
1270 * Send a start unit command to the device,
1271 * and then retry the command. We only
1272 * want to do this if the retry count has
1273 * not been exhausted. If the user
1274 * specified 0 retries, then we follow
1275 * their request and do not retry.
1277 else if (((err_action & SS_MASK) == SS_START)
1279 && ccb->ccb_h.retry_count > 0) {
1283 * Only one error recovery action
1284 * at a time. See above.
1287 CAM_PERIPH_RECOVERY_INPROG) {
1293 CAM_PERIPH_RECOVERY_INPROG;
1295 /* decrement the number of retries */
1297 ccb->ccb_h.retry_count--;
1300 * Check for removable media and
1301 * set load/eject flag
1304 if (SID_IS_REMOVABLE(&cgd.inq_data))
1310 * Attempt to start the drive up.
1312 * Save the current ccb so it can
1313 * be restored and retried once the
1314 * drive is started up.
1316 bcopy(ccb, save_ccb, sizeof(*save_ccb));
1318 scsi_start_stop(&ccb->csio,
1328 * Drop the priority to 0 so that
1329 * we are the first to execute. Also
1330 * freeze the queue after this command
1331 * is sent so that we can restore the
1332 * old csio and have it queued in the
1333 * proper order before we let normal
1334 * transactions go to the drive.
1336 ccb->ccb_h.pinfo.priority = 0;
1337 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1340 * Save a pointer to the original
1341 * CCB in the new CCB.
1343 ccb->ccb_h.saved_ccb_ptr = save_ccb;
1346 } else if ((sense_flags & SF_RETRY_UA) != 0) {
1348 * XXX KDM this is a *horrible*
1351 error = scsi_interpret_sense(ccb,
1360 * Theoretically, this code should send a
1361 * test unit ready to the given device, and
1362 * if it returns and error, send a start
1363 * unit command. Since we don't yet have
1364 * the capability to do two-command error
1365 * recovery, just send a start unit.
1368 else if (((err_action & SS_MASK) == SS_TURSTART)
1370 && ccb->ccb_h.retry_count > 0) {
1374 * Only one error recovery action
1375 * at a time. See above.
1378 CAM_PERIPH_RECOVERY_INPROG) {
1384 CAM_PERIPH_RECOVERY_INPROG;
1386 /* decrement the number of retries */
1388 ccb->ccb_h.retry_count--;
1391 * Check for removable media and
1392 * set load/eject flag
1395 if (SID_IS_REMOVABLE(&cgd.inq_data))
1401 * Attempt to start the drive up.
1403 * Save the current ccb so it can
1404 * be restored and retried once the
1405 * drive is started up.
1407 bcopy(ccb, save_ccb, sizeof(*save_ccb));
1409 scsi_start_stop(&ccb->csio,
1419 /* release the queue after .5 sec. */
1421 RELSIM_RELEASE_AFTER_TIMEOUT;
1424 * Drop the priority to 0 so that
1425 * we are the first to execute. Also
1426 * freeze the queue after this command
1427 * is sent so that we can restore the
1428 * old csio and have it queued in the
1429 * proper order before we let normal
1430 * transactions go to the drive.
1432 ccb->ccb_h.pinfo.priority = 0;
1433 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1436 * Save a pointer to the original
1437 * CCB in the new CCB.
1439 ccb->ccb_h.saved_ccb_ptr = save_ccb;
1443 error = scsi_interpret_sense(ccb,
1450 } else if (ccb->csio.scsi_status ==
1451 SCSI_STATUS_CHECK_COND
1452 && status != CAM_AUTOSENSE_FAIL) {
1453 /* no point in decrementing the retry count */
1454 panic("cam_periph_error: scsi status of "
1455 "CHECK COND returned but no sense "
1456 "information is availible. "
1457 "Controller should have returned "
1458 "CAM_AUTOSENSE_FAILED");
1461 } else if (ccb->ccb_h.retry_count == 0) {
1463 * XXX KDM shouldn't there be a better
1464 * argument to return??
1468 /* decrement the number of retries */
1469 retry = ccb->ccb_h.retry_count > 0;
1471 ccb->ccb_h.retry_count--;
1473 * If it was aborted with no
1474 * clue as to the reason, just
1480 case SCSI_STATUS_QUEUE_FULL:
1483 struct ccb_getdevstats cgds;
1486 * First off, find out what the current
1487 * transaction counts are.
1489 xpt_setup_ccb(&cgds.ccb_h,
1492 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1493 xpt_action((union ccb *)&cgds);
1496 * If we were the only transaction active, treat
1497 * the QUEUE FULL as if it were a BUSY condition.
1499 if (cgds.dev_active != 0) {
1503 * Reduce the number of openings to
1504 * be 1 less than the amount it took
1505 * to get a queue full bounded by the
1506 * minimum allowed tag count for this
1510 cgds.dev_active+cgds.dev_openings;
1511 openings = cgds.dev_active;
1512 if (openings < cgds.mintags)
1513 openings = cgds.mintags;
1514 if (openings < total_openings)
1515 relsim_flags = RELSIM_ADJUST_OPENINGS;
1518 * Some devices report queue full for
1519 * temporary resource shortages. For
1520 * this reason, we allow a minimum
1521 * tag count to be entered via a
1522 * quirk entry to prevent the queue
1523 * count on these devices from falling
1524 * to a pessimisticly low value. We
1525 * still wait for the next successful
1526 * completion, however, before queueing
1527 * more transactions to the device.
1530 RELSIM_RELEASE_AFTER_CMDCMPLT;
1538 case SCSI_STATUS_BUSY:
1540 * Restart the queue after either another
1541 * command completes or a 1 second timeout.
1542 * If we have any retries left, that is.
1544 retry = ccb->ccb_h.retry_count > 0;
1546 ccb->ccb_h.retry_count--;
1548 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT
1549 | RELSIM_RELEASE_AFTER_CMDCMPLT;
1555 case SCSI_STATUS_RESERV_CONFLICT:
1563 case CAM_REQ_CMP_ERR:
1564 case CAM_CMD_TIMEOUT:
1565 case CAM_UNEXP_BUSFREE:
1566 case CAM_UNCOR_PARITY:
1567 case CAM_DATA_RUN_ERR:
1568 /* decrement the number of retries */
1569 retry = ccb->ccb_h.retry_count > 0;
1571 ccb->ccb_h.retry_count--;
1579 case CAM_MSG_REJECT_REC:
1580 /* XXX Don't know that these are correct */
1583 case CAM_SEL_TIMEOUT:
1587 * A single selection timeout should not be enough
1588 * to invalidate a device. We should retry for multiple
1589 * seconds assuming this isn't a probe. We'll probably
1590 * need a special flag for that.
1593 struct cam_path *newpath;
1595 /* Should we do more if we can't create the path?? */
1596 if (xpt_create_path(&newpath, xpt_path_periph(ccb->ccb_h.path),
1597 xpt_path_path_id(ccb->ccb_h.path),
1598 xpt_path_target_id(ccb->ccb_h.path),
1599 CAM_LUN_WILDCARD) != CAM_REQ_CMP)
1602 * Let peripheral drivers know that this device has gone
1605 xpt_async(AC_LOST_DEVICE, newpath, NULL);
1606 xpt_free_path(newpath);
1608 if ((sense_flags & SF_RETRY_SELTO) != 0) {
1609 retry = ccb->ccb_h.retry_count > 0;
1611 ccb->ccb_h.retry_count--;
1614 * Wait half a second to give the device
1615 * time to recover before we try again.
1617 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1627 case CAM_REQ_INVALID:
1628 case CAM_PATH_INVALID:
1629 case CAM_DEV_NOT_THERE:
1631 case CAM_PROVIDE_FAIL:
1632 case CAM_REQ_TOO_BIG:
1635 case CAM_SCSI_BUS_RESET:
1637 case CAM_REQUEUE_REQ:
1638 /* Unconditional requeue, dammit */
1641 case CAM_RESRC_UNAVAIL:
1645 /* decrement the number of retries */
1646 retry = ccb->ccb_h.retry_count > 0;
1648 ccb->ccb_h.retry_count--;
1651 /* Check the sense codes */
1657 /* Attempt a retry */
1658 if (error == ERESTART || error == 0) {
1660 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1662 if (error == ERESTART)
1666 cam_release_devq(ccb->ccb_h.path,
1670 /*getcount_only*/0);