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.26 2007/11/18 18:49:53 pavalos Exp $
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/types.h>
36 #include <sys/malloc.h>
39 #include <sys/devicestat.h>
42 #include <vm/vm_extern.h>
44 #include <sys/thread2.h>
48 #include "cam_xpt_periph.h"
49 #include "cam_periph.h"
50 #include "cam_debug.h"
52 #include <bus/cam/scsi/scsi_all.h>
53 #include <bus/cam/scsi/scsi_message.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
);
66 static int camperiphscsistatuserror(union ccb
*ccb
,
68 u_int32_t sense_flags
,
71 u_int32_t
*relsim_flags
,
73 static int camperiphscsisenseerror(union ccb
*ccb
,
75 u_int32_t sense_flags
,
78 u_int32_t
*relsim_flags
,
81 static int nperiph_drivers
;
82 struct periph_driver
**periph_drivers
;
85 periphdriver_register(void *data
)
87 struct periph_driver
**newdrivers
, **old
;
90 ndrivers
= nperiph_drivers
+ 2;
91 newdrivers
= kmalloc(sizeof(*newdrivers
) * ndrivers
, M_TEMP
, M_WAITOK
);
93 bcopy(periph_drivers
, newdrivers
,
94 sizeof(*newdrivers
) * nperiph_drivers
);
95 newdrivers
[nperiph_drivers
] = (struct periph_driver
*)data
;
96 newdrivers
[nperiph_drivers
+ 1] = NULL
;
98 periph_drivers
= newdrivers
;
105 cam_periph_alloc(periph_ctor_t
*periph_ctor
,
106 periph_oninv_t
*periph_oninvalidate
,
107 periph_dtor_t
*periph_dtor
, periph_start_t
*periph_start
,
108 char *name
, cam_periph_type type
, struct cam_path
*path
,
109 ac_callback_t
*ac_callback
, ac_code code
, void *arg
)
111 struct periph_driver
**p_drv
;
112 struct cam_periph
*periph
;
113 struct cam_periph
*cur_periph
;
115 target_id_t target_id
;
122 * Handle Hot-Plug scenarios. If there is already a peripheral
123 * of our type assigned to this path, we are likely waiting for
124 * final close on an old, invalidated, peripheral. If this is
125 * the case, queue up a deferred call to the peripheral's async
126 * handler. If it looks like a mistaken re-alloation, complain.
128 if ((periph
= cam_periph_find(path
, name
)) != NULL
) {
130 if ((periph
->flags
& CAM_PERIPH_INVALID
) != 0
131 && (periph
->flags
& CAM_PERIPH_NEW_DEV_FOUND
) == 0) {
132 periph
->flags
|= CAM_PERIPH_NEW_DEV_FOUND
;
133 periph
->deferred_callback
= ac_callback
;
134 periph
->deferred_ac
= code
;
135 return (CAM_REQ_INPROG
);
137 kprintf("cam_periph_alloc: attempt to re-allocate "
138 "valid device %s%d rejected\n",
139 periph
->periph_name
, periph
->unit_number
);
141 return (CAM_REQ_INVALID
);
144 periph
= kmalloc(sizeof(*periph
), M_DEVBUF
, M_INTWAIT
| M_ZERO
);
148 for (p_drv
= periph_drivers
; *p_drv
!= NULL
; p_drv
++) {
149 if (strcmp((*p_drv
)->driver_name
, name
) == 0)
153 path_id
= xpt_path_path_id(path
);
154 target_id
= xpt_path_target_id(path
);
155 lun_id
= xpt_path_lun_id(path
);
156 cam_init_pinfo(&periph
->pinfo
);
157 periph
->periph_start
= periph_start
;
158 periph
->periph_dtor
= periph_dtor
;
159 periph
->periph_oninval
= periph_oninvalidate
;
161 periph
->periph_name
= name
;
162 periph
->unit_number
= camperiphunit(*p_drv
, path_id
, target_id
, lun_id
);
163 periph
->immediate_priority
= CAM_PRIORITY_NONE
;
164 periph
->refcount
= 0;
165 SLIST_INIT(&periph
->ccb_list
);
166 status
= xpt_create_path(&path
, periph
, path_id
, target_id
, lun_id
);
167 if (status
!= CAM_REQ_CMP
)
173 status
= xpt_add_periph(periph
);
175 if (status
!= CAM_REQ_CMP
)
179 cur_periph
= TAILQ_FIRST(&(*p_drv
)->units
);
180 while (cur_periph
!= NULL
181 && cur_periph
->unit_number
< periph
->unit_number
)
182 cur_periph
= TAILQ_NEXT(cur_periph
, unit_links
);
184 if (cur_periph
!= NULL
)
185 TAILQ_INSERT_BEFORE(cur_periph
, periph
, unit_links
);
187 TAILQ_INSERT_TAIL(&(*p_drv
)->units
, periph
, unit_links
);
188 (*p_drv
)->generation
++;
195 status
= periph_ctor(periph
, arg
);
197 if (status
== CAM_REQ_CMP
)
201 switch (init_level
) {
203 /* Initialized successfully */
207 TAILQ_REMOVE(&(*p_drv
)->units
, periph
, unit_links
);
209 xpt_remove_periph(periph
);
211 xpt_free_path(periph
->path
);
213 kfree(periph
, M_DEVBUF
);
215 /* No cleanup to perform. */
218 panic("cam_periph_alloc: Unknown init level");
224 * Find a peripheral structure with the specified path, target, lun,
225 * and (optionally) type. If the name is NULL, this function will return
226 * the first peripheral driver that matches the specified path.
229 cam_periph_find(struct cam_path
*path
, char *name
)
231 struct periph_driver
**p_drv
;
232 struct cam_periph
*periph
;
234 for (p_drv
= periph_drivers
; *p_drv
!= NULL
; p_drv
++) {
235 if (name
!= NULL
&& (strcmp((*p_drv
)->driver_name
, name
) != 0))
239 TAILQ_FOREACH(periph
, &(*p_drv
)->units
, unit_links
) {
240 if (xpt_path_comp(periph
->path
, path
) == 0) {
253 cam_periph_acquire(struct cam_periph
*periph
)
256 return(CAM_REQ_CMP_ERR
);
266 cam_periph_release(struct cam_periph
*periph
)
272 if ((--periph
->refcount
== 0)
273 && (periph
->flags
& CAM_PERIPH_INVALID
)) {
274 camperiphfree(periph
);
280 * Look for the next unit number that is not currently in use for this
281 * peripheral type starting at "newunit". Also exclude unit numbers that
282 * are reserved by for future "hardwiring" unless we already know that this
283 * is a potential wired device. Only assume that the device is "wired" the
284 * first time through the loop since after that we'll be looking at unit
285 * numbers that did not match a wiring entry.
288 camperiphnextunit(struct periph_driver
*p_drv
, u_int newunit
, int wired
,
289 path_id_t pathid
, target_id_t target
, lun_id_t lun
)
291 struct cam_periph
*periph
;
292 char *periph_name
, *strval
;
297 periph_name
= p_drv
->driver_name
;
300 for (periph
= TAILQ_FIRST(&p_drv
->units
);
301 periph
!= NULL
&& periph
->unit_number
!= newunit
;
302 periph
= TAILQ_NEXT(periph
, unit_links
))
305 if (periph
!= NULL
&& periph
->unit_number
== newunit
) {
307 xpt_print_path(periph
->path
);
308 kprintf("Duplicate Wired Device entry!\n");
309 xpt_print_path(periph
->path
);
310 kprintf("Second device (%s device at scbus%d "
311 "target %d lun %d) will not be wired\n",
312 periph_name
, pathid
, target
, lun
);
321 * Don't match entries like "da 4" as a wired down
322 * device, but do match entries like "da 4 target 5"
323 * or even "da 4 scbus 1".
326 while ((i
= resource_locate(i
, periph_name
)) != -1) {
327 dname
= resource_query_name(i
);
328 dunit
= resource_query_unit(i
);
329 /* if no "target" and no specific scbus, skip */
330 if (resource_int_value(dname
, dunit
, "target", &val
) &&
331 (resource_string_value(dname
, dunit
, "at",&strval
)||
332 strcmp(strval
, "scbus") == 0))
334 if (newunit
== dunit
)
345 camperiphunit(struct periph_driver
*p_drv
, path_id_t pathid
,
346 target_id_t target
, lun_id_t lun
)
349 int hit
, i
, val
, dunit
;
351 char pathbuf
[32], *strval
, *periph_name
;
355 periph_name
= p_drv
->driver_name
;
356 ksnprintf(pathbuf
, sizeof(pathbuf
), "scbus%d", pathid
);
358 for (hit
= 0; (i
= resource_locate(i
, periph_name
)) != -1; hit
= 0) {
359 dname
= resource_query_name(i
);
360 dunit
= resource_query_unit(i
);
361 if (resource_string_value(dname
, dunit
, "at", &strval
) == 0) {
362 if (strcmp(strval
, pathbuf
) != 0)
366 if (resource_int_value(dname
, dunit
, "target", &val
) == 0) {
371 if (resource_int_value(dname
, dunit
, "lun", &val
) == 0) {
383 * Either start from 0 looking for the next unit or from
384 * the unit number given in the resource config. This way,
385 * if we have wildcard matches, we don't return the same
388 unit
= camperiphnextunit(p_drv
, unit
, /*wired*/hit
, pathid
,
395 cam_periph_invalidate(struct cam_periph
*periph
)
398 * We only call this routine the first time a peripheral is
399 * invalidated. The oninvalidate() routine is always called in
400 * a critical section.
403 if (((periph
->flags
& CAM_PERIPH_INVALID
) == 0)
404 && (periph
->periph_oninval
!= NULL
))
405 periph
->periph_oninval(periph
);
407 periph
->flags
|= CAM_PERIPH_INVALID
;
408 periph
->flags
&= ~CAM_PERIPH_NEW_DEV_FOUND
;
410 if (periph
->refcount
== 0)
411 camperiphfree(periph
);
412 else if (periph
->refcount
< 0)
413 kprintf("cam_invalidate_periph: refcount < 0!!\n");
418 camperiphfree(struct cam_periph
*periph
)
420 struct periph_driver
**p_drv
;
422 for (p_drv
= periph_drivers
; *p_drv
!= NULL
; p_drv
++) {
423 if (strcmp((*p_drv
)->driver_name
, periph
->periph_name
) == 0)
427 if (*p_drv
== NULL
) {
428 kprintf("camperiphfree: attempt to free "
429 "non-existent periph: %s\n", periph
->periph_name
);
433 if (periph
->periph_dtor
!= NULL
)
434 periph
->periph_dtor(periph
);
437 TAILQ_REMOVE(&(*p_drv
)->units
, periph
, unit_links
);
438 (*p_drv
)->generation
++;
441 xpt_remove_periph(periph
);
443 if (periph
->flags
& CAM_PERIPH_NEW_DEV_FOUND
) {
447 switch (periph
->deferred_ac
) {
448 case AC_FOUND_DEVICE
:
449 ccb
.ccb_h
.func_code
= XPT_GDEV_TYPE
;
450 xpt_setup_ccb(&ccb
.ccb_h
, periph
->path
, /*priority*/ 1);
454 case AC_PATH_REGISTERED
:
455 ccb
.ccb_h
.func_code
= XPT_PATH_INQ
;
456 xpt_setup_ccb(&ccb
.ccb_h
, periph
->path
, /*priority*/ 1);
464 periph
->deferred_callback(NULL
, periph
->deferred_ac
,
467 xpt_free_path(periph
->path
);
468 kfree(periph
, M_DEVBUF
);
472 * Wait interruptibly for an exclusive lock.
475 cam_periph_lock(struct cam_periph
*periph
, int flags
)
480 * Increment the reference count on the peripheral
481 * while we wait for our lock attempt to succeed
482 * to ensure the peripheral doesn't dissappear
483 * out from under us while we sleep.
485 if (cam_periph_acquire(periph
) != CAM_REQ_CMP
)
488 while ((periph
->flags
& CAM_PERIPH_LOCKED
) != 0) {
489 periph
->flags
|= CAM_PERIPH_LOCK_WANTED
;
490 if ((error
= tsleep(periph
, flags
, "caplck", 0)) != 0) {
491 cam_periph_release(periph
);
496 periph
->flags
|= CAM_PERIPH_LOCKED
;
501 * Unlock and wake up any waiters.
504 cam_periph_unlock(struct cam_periph
*periph
)
506 periph
->flags
&= ~CAM_PERIPH_LOCKED
;
507 if ((periph
->flags
& CAM_PERIPH_LOCK_WANTED
) != 0) {
508 periph
->flags
&= ~CAM_PERIPH_LOCK_WANTED
;
512 cam_periph_release(periph
);
516 * Map user virtual pointers into kernel virtual address space, so we can
517 * access the memory. This won't work on physical pointers, for now it's
518 * up to the caller to check for that. (XXX KDM -- should we do that here
519 * instead?) This also only works for up to MAXPHYS memory. Since we use
520 * buffers to map stuff in and out, we're limited to the buffer size.
523 cam_periph_mapmem(union ccb
*ccb
, struct cam_periph_map_info
*mapinfo
)
526 buf_cmd_t cmd
[CAM_PERIPH_MAXMAPS
];
527 u_int8_t
**data_ptrs
[CAM_PERIPH_MAXMAPS
];
528 u_int32_t lengths
[CAM_PERIPH_MAXMAPS
];
529 u_int32_t dirs
[CAM_PERIPH_MAXMAPS
];
531 switch(ccb
->ccb_h
.func_code
) {
533 if (ccb
->cdm
.match_buf_len
== 0) {
534 kprintf("cam_periph_mapmem: invalid match buffer "
538 if (ccb
->cdm
.pattern_buf_len
> 0) {
539 data_ptrs
[0] = (u_int8_t
**)&ccb
->cdm
.patterns
;
540 lengths
[0] = ccb
->cdm
.pattern_buf_len
;
541 dirs
[0] = CAM_DIR_OUT
;
542 data_ptrs
[1] = (u_int8_t
**)&ccb
->cdm
.matches
;
543 lengths
[1] = ccb
->cdm
.match_buf_len
;
544 dirs
[1] = CAM_DIR_IN
;
547 data_ptrs
[0] = (u_int8_t
**)&ccb
->cdm
.matches
;
548 lengths
[0] = ccb
->cdm
.match_buf_len
;
549 dirs
[0] = CAM_DIR_IN
;
554 case XPT_CONT_TARGET_IO
:
555 if ((ccb
->ccb_h
.flags
& CAM_DIR_MASK
) == CAM_DIR_NONE
)
558 data_ptrs
[0] = &ccb
->csio
.data_ptr
;
559 lengths
[0] = ccb
->csio
.dxfer_len
;
560 dirs
[0] = ccb
->ccb_h
.flags
& CAM_DIR_MASK
;
565 break; /* NOTREACHED */
569 * Check the transfer length and permissions first, so we don't
570 * have to unmap any previously mapped buffers.
572 for (i
= 0; i
< numbufs
; i
++) {
574 * Its kinda bogus, we need a R+W command. For now the
575 * buffer needs some sort of command. Use BUF_CMD_WRITE
576 * to indicate a write and BUF_CMD_READ to indicate R+W.
578 cmd
[i
] = BUF_CMD_WRITE
;
581 * The userland data pointer passed in may not be page
582 * aligned. vmapbuf() truncates the address to a page
583 * boundary, so if the address isn't page aligned, we'll
584 * need enough space for the given transfer length, plus
585 * whatever extra space is necessary to make it to the page
589 (((vm_offset_t
)(*data_ptrs
[i
])) & PAGE_MASK
)) > DFLTPHYS
){
590 kprintf("cam_periph_mapmem: attempt to map %lu bytes, "
591 "which is greater than DFLTPHYS(%d)\n",
593 (((vm_offset_t
)(*data_ptrs
[i
])) & PAGE_MASK
)),
598 if (dirs
[i
] & CAM_DIR_OUT
) {
599 if (!useracc(*data_ptrs
[i
], lengths
[i
],
601 kprintf("cam_periph_mapmem: error, "
602 "address %p, length %lu isn't "
603 "user accessible for READ\n",
604 (void *)*data_ptrs
[i
],
610 if (dirs
[i
] & CAM_DIR_IN
) {
611 cmd
[i
] = BUF_CMD_READ
;
612 if (!useracc(*data_ptrs
[i
], lengths
[i
],
614 kprintf("cam_periph_mapmem: error, "
615 "address %p, length %lu isn't "
616 "user accessible for WRITE\n",
617 (void *)*data_ptrs
[i
],
626 for (i
= 0; i
< numbufs
; i
++) {
630 mapinfo
->bp
[i
] = getpbuf(NULL
);
632 /* save the original user pointer */
633 mapinfo
->saved_ptrs
[i
] = *data_ptrs
[i
];
636 mapinfo
->bp
[i
]->b_cmd
= cmd
[i
];
638 /* map the user buffer into kernel memory */
639 if (vmapbuf(mapinfo
->bp
[i
], *data_ptrs
[i
], lengths
[i
]) < 0) {
640 kprintf("cam_periph_mapmem: error, "
641 "address %p, length %lu isn't "
642 "user accessible any more\n",
643 (void *)*data_ptrs
[i
],
645 for (j
= 0; j
< i
; ++j
) {
646 *data_ptrs
[j
] = mapinfo
->saved_ptrs
[j
];
647 vunmapbuf(mapinfo
->bp
[j
]);
648 relpbuf(mapinfo
->bp
[j
], NULL
);
650 mapinfo
->num_bufs_used
-= i
;
654 /* set our pointer to the new mapped area */
655 *data_ptrs
[i
] = mapinfo
->bp
[i
]->b_data
;
657 mapinfo
->num_bufs_used
++;
664 * Unmap memory segments mapped into kernel virtual address space by
665 * cam_periph_mapmem().
668 cam_periph_unmapmem(union ccb
*ccb
, struct cam_periph_map_info
*mapinfo
)
671 u_int8_t
**data_ptrs
[CAM_PERIPH_MAXMAPS
];
673 if (mapinfo
->num_bufs_used
<= 0) {
674 /* allow ourselves to be swapped once again */
678 switch (ccb
->ccb_h
.func_code
) {
680 numbufs
= min(mapinfo
->num_bufs_used
, 2);
683 data_ptrs
[0] = (u_int8_t
**)&ccb
->cdm
.matches
;
685 data_ptrs
[0] = (u_int8_t
**)&ccb
->cdm
.patterns
;
686 data_ptrs
[1] = (u_int8_t
**)&ccb
->cdm
.matches
;
690 case XPT_CONT_TARGET_IO
:
691 data_ptrs
[0] = &ccb
->csio
.data_ptr
;
692 numbufs
= min(mapinfo
->num_bufs_used
, 1);
695 /* allow ourselves to be swapped once again */
697 break; /* NOTREACHED */
700 for (i
= 0; i
< numbufs
; i
++) {
701 /* Set the user's pointer back to the original value */
702 *data_ptrs
[i
] = mapinfo
->saved_ptrs
[i
];
704 /* unmap the buffer */
705 vunmapbuf(mapinfo
->bp
[i
]);
707 /* release the buffer */
708 relpbuf(mapinfo
->bp
[i
], NULL
);
711 /* allow ourselves to be swapped once again */
715 cam_periph_getccb(struct cam_periph
*periph
, u_int32_t priority
)
717 struct ccb_hdr
*ccb_h
;
719 CAM_DEBUG(periph
->path
, CAM_DEBUG_TRACE
, ("entering cdgetccb\n"));
723 while (SLIST_FIRST(&periph
->ccb_list
) == NULL
) {
724 if (periph
->immediate_priority
> priority
)
725 periph
->immediate_priority
= priority
;
726 xpt_schedule(periph
, priority
);
727 if ((SLIST_FIRST(&periph
->ccb_list
) != NULL
)
728 && (SLIST_FIRST(&periph
->ccb_list
)->pinfo
.priority
== priority
))
730 tsleep(&periph
->ccb_list
, 0, "cgticb", 0);
733 ccb_h
= SLIST_FIRST(&periph
->ccb_list
);
734 SLIST_REMOVE_HEAD(&periph
->ccb_list
, periph_links
.sle
);
736 return ((union ccb
*)ccb_h
);
740 cam_periph_ccbwait(union ccb
*ccb
)
743 if ((ccb
->ccb_h
.pinfo
.index
!= CAM_UNQUEUED_INDEX
)
744 || ((ccb
->ccb_h
.status
& CAM_STATUS_MASK
) == CAM_REQ_INPROG
))
745 tsleep(&ccb
->ccb_h
.cbfcnp
, 0, "cbwait", 0);
750 cam_periph_ioctl(struct cam_periph
*periph
, int cmd
, caddr_t addr
,
751 int (*error_routine
)(union ccb
*ccb
,
753 u_int32_t sense_flags
))
763 ccb
= cam_periph_getccb(periph
, /* priority */ 1);
764 xpt_setup_ccb(&ccb
->ccb_h
,
767 ccb
->ccb_h
.func_code
= XPT_GDEVLIST
;
770 * Basically, the point of this is that we go through
771 * getting the list of devices, until we find a passthrough
772 * device. In the current version of the CAM code, the
773 * only way to determine what type of device we're dealing
774 * with is by its name.
778 ccb
->cgdl
.status
= CAM_GDEVLIST_MORE_DEVS
;
779 while (ccb
->cgdl
.status
== CAM_GDEVLIST_MORE_DEVS
) {
781 /* we want the next device in the list */
783 if (strncmp(ccb
->cgdl
.periph_name
,
789 if ((ccb
->cgdl
.status
== CAM_GDEVLIST_LAST_DEVICE
) &&
791 ccb
->cgdl
.periph_name
[0] = '\0';
792 ccb
->cgdl
.unit_number
= 0;
797 /* copy the result back out */
798 bcopy(ccb
, addr
, sizeof(union ccb
));
800 /* and release the ccb */
801 xpt_release_ccb(ccb
);
812 cam_periph_runccb(union ccb
*ccb
,
813 int (*error_routine
)(union ccb
*ccb
,
815 u_int32_t sense_flags
),
816 cam_flags camflags
, u_int32_t sense_flags
,
824 * If the user has supplied a stats structure, and if we understand
825 * this particular type of ccb, record the transaction start.
827 if ((ds
!= NULL
) && (ccb
->ccb_h
.func_code
== XPT_SCSI_IO
))
828 devstat_start_transaction(ds
);
833 cam_periph_ccbwait(ccb
);
834 if ((ccb
->ccb_h
.status
& CAM_STATUS_MASK
) == CAM_REQ_CMP
)
836 else if (error_routine
!= NULL
)
837 error
= (*error_routine
)(ccb
, camflags
, sense_flags
);
841 } while (error
== ERESTART
);
843 if ((ccb
->ccb_h
.status
& CAM_DEV_QFRZN
) != 0)
844 cam_release_devq(ccb
->ccb_h
.path
,
848 /* getcount_only */ FALSE
);
850 if ((ds
!= NULL
) && (ccb
->ccb_h
.func_code
== XPT_SCSI_IO
))
851 devstat_end_transaction(ds
,
853 ccb
->csio
.tag_action
& 0xf,
854 ((ccb
->ccb_h
.flags
& CAM_DIR_MASK
) ==
855 CAM_DIR_NONE
) ? DEVSTAT_NO_DATA
:
856 (ccb
->ccb_h
.flags
& CAM_DIR_OUT
) ?
864 cam_freeze_devq(struct cam_path
*path
)
866 struct ccb_hdr ccb_h
;
868 xpt_setup_ccb(&ccb_h
, path
, /*priority*/1);
869 ccb_h
.func_code
= XPT_NOOP
;
870 ccb_h
.flags
= CAM_DEV_QFREEZE
;
871 xpt_action((union ccb
*)&ccb_h
);
875 cam_release_devq(struct cam_path
*path
, u_int32_t relsim_flags
,
876 u_int32_t openings
, u_int32_t timeout
,
879 struct ccb_relsim crs
;
881 xpt_setup_ccb(&crs
.ccb_h
, path
,
883 crs
.ccb_h
.func_code
= XPT_REL_SIMQ
;
884 crs
.ccb_h
.flags
= getcount_only
? CAM_DEV_QFREEZE
: 0;
885 crs
.release_flags
= relsim_flags
;
886 crs
.openings
= openings
;
887 crs
.release_timeout
= timeout
;
888 xpt_action((union ccb
*)&crs
);
889 return (crs
.qfrozen_cnt
);
892 #define saved_ccb_ptr ppriv_ptr0
894 camperiphdone(struct cam_periph
*periph
, union ccb
*done_ccb
)
896 union ccb
*saved_ccb
;
900 struct scsi_start_stop_unit
*scsi_cmd
;
901 u_int32_t relsim_flags
, timeout
;
902 u_int32_t qfrozen_cnt
;
905 xpt_done_ccb
= FALSE
;
906 status
= done_ccb
->ccb_h
.status
;
907 frozen
= (status
& CAM_DEV_QFRZN
) != 0;
908 sense
= (status
& CAM_AUTOSNS_VALID
) != 0;
909 status
&= CAM_STATUS_MASK
;
913 saved_ccb
= (union ccb
*)done_ccb
->ccb_h
.saved_ccb_ptr
;
916 * Unfreeze the queue once if it is already frozen..
919 qfrozen_cnt
= cam_release_devq(done_ccb
->ccb_h
.path
,
930 * If we have successfully taken a device from the not
931 * ready to ready state, re-scan the device and re-get
932 * the inquiry information. Many devices (mostly disks)
933 * don't properly report their inquiry information unless
936 * If we manually retrieved sense into a CCB and got
937 * something other than "NO SENSE" send the updated CCB
938 * back to the client via xpt_done() to be processed via
939 * the error recovery code again.
941 if (done_ccb
->ccb_h
.func_code
== XPT_SCSI_IO
) {
942 scsi_cmd
= (struct scsi_start_stop_unit
*)
943 &done_ccb
->csio
.cdb_io
.cdb_bytes
;
945 if (scsi_cmd
->opcode
== START_STOP_UNIT
)
946 xpt_async(AC_INQ_CHANGED
,
947 done_ccb
->ccb_h
.path
, NULL
);
948 if (scsi_cmd
->opcode
== REQUEST_SENSE
) {
951 sense_key
= saved_ccb
->csio
.sense_data
.flags
;
952 sense_key
&= SSD_KEY
;
953 if (sense_key
!= SSD_KEY_NO_SENSE
) {
954 saved_ccb
->ccb_h
.flags
|=
956 xpt_print_path(saved_ccb
->ccb_h
.path
);
957 kprintf("Recovered Sense\n");
959 scsi_sense_print(&saved_ccb
->csio
);
961 cam_error_print(saved_ccb
, CAM_ESF_ALL
,
967 bcopy(done_ccb
->ccb_h
.saved_ccb_ptr
, done_ccb
,
970 periph
->flags
&= ~CAM_PERIPH_RECOVERY_INPROG
;
972 if (xpt_done_ccb
== FALSE
)
973 xpt_action(done_ccb
);
977 case CAM_SCSI_STATUS_ERROR
:
978 scsi_cmd
= (struct scsi_start_stop_unit
*)
979 &done_ccb
->csio
.cdb_io
.cdb_bytes
;
981 struct scsi_sense_data
*sense
;
982 int error_code
, sense_key
, asc
, ascq
;
984 sense
= &done_ccb
->csio
.sense_data
;
985 scsi_extract_sense(sense
, &error_code
,
986 &sense_key
, &asc
, &ascq
);
989 * If the error is "invalid field in CDB",
990 * and the load/eject flag is set, turn the
991 * flag off and try again. This is just in
992 * case the drive in question barfs on the
993 * load eject flag. The CAM code should set
994 * the load/eject flag by default for
999 * Should we check to see what the specific
1000 * scsi status is?? Or does it not matter
1001 * since we already know that there was an
1002 * error, and we know what the specific
1003 * error code was, and we know what the
1006 if ((scsi_cmd
->opcode
== START_STOP_UNIT
) &&
1007 ((scsi_cmd
->how
& SSS_LOEJ
) != 0) &&
1008 (asc
== 0x24) && (ascq
== 0x00) &&
1009 (done_ccb
->ccb_h
.retry_count
> 0)) {
1011 scsi_cmd
->how
&= ~SSS_LOEJ
;
1013 xpt_action(done_ccb
);
1015 } else if (done_ccb
->ccb_h
.retry_count
> 1) {
1017 * In this case, the error recovery
1018 * command failed, but we've got
1019 * some retries left on it. Give
1023 /* set the timeout to .5 sec */
1025 RELSIM_RELEASE_AFTER_TIMEOUT
;
1028 xpt_action(done_ccb
);
1034 * Perform the final retry with the original
1035 * CCB so that final error processing is
1036 * performed by the owner of the CCB.
1038 bcopy(done_ccb
->ccb_h
.saved_ccb_ptr
,
1039 done_ccb
, sizeof(union ccb
));
1041 periph
->flags
&= ~CAM_PERIPH_RECOVERY_INPROG
;
1043 xpt_action(done_ccb
);
1047 * Eh?? The command failed, but we don't
1048 * have any sense. What's up with that?
1049 * Fire the CCB again to return it to the
1052 bcopy(done_ccb
->ccb_h
.saved_ccb_ptr
,
1053 done_ccb
, sizeof(union ccb
));
1055 periph
->flags
&= ~CAM_PERIPH_RECOVERY_INPROG
;
1057 xpt_action(done_ccb
);
1062 bcopy(done_ccb
->ccb_h
.saved_ccb_ptr
, done_ccb
,
1065 periph
->flags
&= ~CAM_PERIPH_RECOVERY_INPROG
;
1067 xpt_action(done_ccb
);
1072 /* decrement the retry count */
1074 * XXX This isn't appropriate in all cases. Restructure,
1075 * so that the retry count is only decremented on an
1076 * actual retry. Remeber that the orignal ccb had its
1077 * retry count dropped before entering recovery, so
1078 * doing it again is a bug.
1080 if (done_ccb
->ccb_h
.retry_count
> 0)
1081 done_ccb
->ccb_h
.retry_count
--;
1083 qfrozen_cnt
= cam_release_devq(done_ccb
->ccb_h
.path
,
1084 /*relsim_flags*/relsim_flags
,
1087 /*getcount_only*/0);
1088 if (xpt_done_ccb
== TRUE
)
1089 (*done_ccb
->ccb_h
.cbfcnp
)(periph
, done_ccb
);
1093 * Generic Async Event handler. Peripheral drivers usually
1094 * filter out the events that require personal attention,
1095 * and leave the rest to this function.
1098 cam_periph_async(struct cam_periph
*periph
, u_int32_t code
,
1099 struct cam_path
*path
, void *arg
)
1102 case AC_LOST_DEVICE
:
1103 cam_periph_invalidate(periph
);
1108 cam_periph_bus_settle(periph
, SCSI_DELAY
);
1117 cam_periph_bus_settle(struct cam_periph
*periph
, u_int bus_settle
)
1119 struct ccb_getdevstats cgds
;
1121 xpt_setup_ccb(&cgds
.ccb_h
, periph
->path
, /*priority*/1);
1122 cgds
.ccb_h
.func_code
= XPT_GDEV_STATS
;
1123 xpt_action((union ccb
*)&cgds
);
1124 cam_periph_freeze_after_event(periph
, &cgds
.last_reset
, bus_settle
);
1128 cam_periph_freeze_after_event(struct cam_periph
*periph
,
1129 struct timeval
* event_time
, u_int duration_ms
)
1131 struct timeval delta
;
1132 struct timeval duration_tv
;
1134 microuptime(&delta
);
1135 timevalsub(&delta
, event_time
);
1136 duration_tv
.tv_sec
= duration_ms
/ 1000;
1137 duration_tv
.tv_usec
= (duration_ms
% 1000) * 1000;
1138 if (timevalcmp(&delta
, &duration_tv
, <)) {
1139 timevalsub(&duration_tv
, &delta
);
1141 duration_ms
= duration_tv
.tv_sec
* 1000;
1142 duration_ms
+= duration_tv
.tv_usec
/ 1000;
1143 cam_freeze_devq(periph
->path
);
1144 cam_release_devq(periph
->path
,
1145 RELSIM_RELEASE_AFTER_TIMEOUT
,
1147 /*timeout*/duration_ms
,
1148 /*getcount_only*/0);
1154 camperiphscsistatuserror(union ccb
*ccb
, cam_flags camflags
,
1155 u_int32_t sense_flags
, union ccb
*save_ccb
,
1156 int *openings
, u_int32_t
*relsim_flags
,
1161 switch (ccb
->csio
.scsi_status
) {
1162 case SCSI_STATUS_OK
:
1163 case SCSI_STATUS_COND_MET
:
1164 case SCSI_STATUS_INTERMED
:
1165 case SCSI_STATUS_INTERMED_COND_MET
:
1168 case SCSI_STATUS_CMD_TERMINATED
:
1169 case SCSI_STATUS_CHECK_COND
:
1170 error
= camperiphscsisenseerror(ccb
,
1178 case SCSI_STATUS_QUEUE_FULL
:
1181 struct ccb_getdevstats cgds
;
1184 * First off, find out what the current
1185 * transaction counts are.
1187 xpt_setup_ccb(&cgds
.ccb_h
,
1190 cgds
.ccb_h
.func_code
= XPT_GDEV_STATS
;
1191 xpt_action((union ccb
*)&cgds
);
1194 * If we were the only transaction active, treat
1195 * the QUEUE FULL as if it were a BUSY condition.
1197 if (cgds
.dev_active
!= 0) {
1201 * Reduce the number of openings to
1202 * be 1 less than the amount it took
1203 * to get a queue full bounded by the
1204 * minimum allowed tag count for this
1207 total_openings
= cgds
.dev_active
+ cgds
.dev_openings
;
1208 *openings
= cgds
.dev_active
;
1209 if (*openings
< cgds
.mintags
)
1210 *openings
= cgds
.mintags
;
1211 if (*openings
< total_openings
)
1212 *relsim_flags
= RELSIM_ADJUST_OPENINGS
;
1215 * Some devices report queue full for
1216 * temporary resource shortages. For
1217 * this reason, we allow a minimum
1218 * tag count to be entered via a
1219 * quirk entry to prevent the queue
1220 * count on these devices from falling
1221 * to a pessimisticly low value. We
1222 * still wait for the next successful
1223 * completion, however, before queueing
1224 * more transactions to the device.
1226 *relsim_flags
= RELSIM_RELEASE_AFTER_CMDCMPLT
;
1234 case SCSI_STATUS_BUSY
:
1236 * Restart the queue after either another
1237 * command completes or a 1 second timeout.
1239 if (ccb
->ccb_h
.retry_count
> 0) {
1240 ccb
->ccb_h
.retry_count
--;
1242 *relsim_flags
= RELSIM_RELEASE_AFTER_TIMEOUT
1243 | RELSIM_RELEASE_AFTER_CMDCMPLT
;
1249 case SCSI_STATUS_RESERV_CONFLICT
:
1260 camperiphscsisenseerror(union ccb
*ccb
, cam_flags camflags
,
1261 u_int32_t sense_flags
, union ccb
*save_ccb
,
1262 int *openings
, u_int32_t
*relsim_flags
,
1265 struct cam_periph
*periph
;
1268 periph
= xpt_path_periph(ccb
->ccb_h
.path
);
1269 if (periph
->flags
& CAM_PERIPH_RECOVERY_INPROG
) {
1272 * If error recovery is already in progress, don't attempt
1273 * to process this error, but requeue it unconditionally
1274 * and attempt to process it once error recovery has
1275 * completed. This failed command is probably related to
1276 * the error that caused the currently active error recovery
1277 * action so our current recovery efforts should also
1278 * address this command. Be aware that the error recovery
1279 * code assumes that only one recovery action is in progress
1280 * on a particular peripheral instance at any given time
1281 * (e.g. only one saved CCB for error recovery) so it is
1282 * imperitive that we don't violate this assumption.
1286 scsi_sense_action err_action
;
1287 struct ccb_getdev cgd
;
1288 const char *action_string
;
1289 union ccb
* print_ccb
;
1291 /* A description of the error recovery action performed */
1292 action_string
= NULL
;
1295 * The location of the orignal ccb
1296 * for sense printing purposes.
1301 * Grab the inquiry data for this device.
1303 xpt_setup_ccb(&cgd
.ccb_h
, ccb
->ccb_h
.path
, /*priority*/ 1);
1304 cgd
.ccb_h
.func_code
= XPT_GDEV_TYPE
;
1305 xpt_action((union ccb
*)&cgd
);
1307 if ((ccb
->ccb_h
.status
& CAM_AUTOSNS_VALID
) != 0)
1308 err_action
= scsi_error_action(&ccb
->csio
,
1311 else if ((ccb
->ccb_h
.flags
& CAM_DIS_AUTOSENSE
) == 0)
1312 err_action
= SS_REQSENSE
;
1314 err_action
= SS_RETRY
|SSQ_DECREMENT_COUNT
|EIO
;
1316 error
= err_action
& SS_ERRMASK
;
1319 * If the recovery action will consume a retry,
1320 * make sure we actually have retries available.
1322 if ((err_action
& SSQ_DECREMENT_COUNT
) != 0) {
1323 if (ccb
->ccb_h
.retry_count
> 0)
1324 ccb
->ccb_h
.retry_count
--;
1326 action_string
= "Retries Exhausted";
1327 goto sense_error_done
;
1331 if ((err_action
& SS_MASK
) >= SS_START
) {
1333 * Do common portions of commands that
1334 * use recovery CCBs.
1336 if (save_ccb
== NULL
) {
1337 action_string
= "No recovery CCB supplied";
1338 goto sense_error_done
;
1340 bcopy(ccb
, save_ccb
, sizeof(*save_ccb
));
1341 print_ccb
= save_ccb
;
1342 periph
->flags
|= CAM_PERIPH_RECOVERY_INPROG
;
1345 switch (err_action
& SS_MASK
) {
1348 action_string
= "Retrying Command";
1352 action_string
= "Unretryable error";
1359 * Send a start unit command to the device, and
1360 * then retry the command.
1362 action_string
= "Attempting to Start Unit";
1365 * Check for removable media and set
1366 * load/eject flag appropriately.
1368 if (SID_IS_REMOVABLE(&cgd
.inq_data
))
1373 scsi_start_stop(&ccb
->csio
,
1387 * Send a Test Unit Ready to the device.
1388 * If the 'many' flag is set, we send 120
1389 * test unit ready commands, one every half
1390 * second. Otherwise, we just send one TUR.
1391 * We only want to do this if the retry
1392 * count has not been exhausted.
1396 if ((err_action
& SSQ_MANY
) != 0) {
1397 action_string
= "Polling device for readiness";
1400 action_string
= "Testing device for readiness";
1403 scsi_test_unit_ready(&ccb
->csio
,
1411 * Accomplish our 500ms delay by deferring
1412 * the release of our device queue appropriately.
1414 *relsim_flags
= RELSIM_RELEASE_AFTER_TIMEOUT
;
1421 * Send a Request Sense to the device. We
1422 * assume that we are in a contingent allegiance
1423 * condition so we do not tag this request.
1425 scsi_request_sense(&ccb
->csio
, /*retries*/1,
1427 &save_ccb
->csio
.sense_data
,
1428 sizeof(save_ccb
->csio
.sense_data
),
1429 CAM_TAG_ACTION_NONE
,
1430 /*sense_len*/SSD_FULL_SIZE
,
1435 panic("Unhandled error action %x\n", err_action
);
1438 if ((err_action
& SS_MASK
) >= SS_START
) {
1440 * Drop the priority to 0 so that the recovery
1441 * CCB is the first to execute. Freeze the queue
1442 * after this command is sent so that we can
1443 * restore the old csio and have it queued in
1444 * the proper order before we release normal
1445 * transactions to the device.
1447 ccb
->ccb_h
.pinfo
.priority
= 0;
1448 ccb
->ccb_h
.flags
|= CAM_DEV_QFREEZE
;
1449 ccb
->ccb_h
.saved_ccb_ptr
= save_ccb
;
1454 if ((err_action
& SSQ_PRINT_SENSE
) != 0
1455 && (ccb
->ccb_h
.status
& CAM_AUTOSNS_VALID
) != 0) {
1457 scsi_sense_print(&print_ccb
->csio
);
1459 cam_error_print(print_ccb
, CAM_ESF_ALL
, CAM_EPF_ALL
);
1460 xpt_print_path(ccb
->ccb_h
.path
);
1461 kprintf("%s\n", action_string
);
1468 * Generic error handler. Peripheral drivers usually filter
1469 * out the errors that they handle in a unique mannor, then
1470 * call this function.
1473 cam_periph_error(union ccb
*ccb
, cam_flags camflags
,
1474 u_int32_t sense_flags
, union ccb
*save_ccb
)
1476 const char *action_string
;
1481 u_int32_t relsim_flags
;
1484 action_string
= NULL
;
1485 status
= ccb
->ccb_h
.status
;
1486 frozen
= (status
& CAM_DEV_QFRZN
) != 0;
1487 status
&= CAM_STATUS_MASK
;
1494 case CAM_SCSI_STATUS_ERROR
:
1495 error
= camperiphscsistatuserror(ccb
,
1503 case CAM_AUTOSENSE_FAIL
:
1504 xpt_print_path(ccb
->ccb_h
.path
);
1505 kprintf("AutoSense Failed\n");
1506 case CAM_REQ_CMP_ERR
:
1507 case CAM_CMD_TIMEOUT
:
1508 case CAM_UNEXP_BUSFREE
:
1509 case CAM_UNCOR_PARITY
:
1510 case CAM_DATA_RUN_ERR
:
1511 /* decrement the number of retries */
1512 if (ccb
->ccb_h
.retry_count
> 0) {
1513 ccb
->ccb_h
.retry_count
--;
1516 action_string
= "Retries Exausted";
1522 case CAM_MSG_REJECT_REC
:
1523 /* XXX Don't know that these are correct */
1526 case CAM_SEL_TIMEOUT
:
1528 struct cam_path
*newpath
;
1530 if ((camflags
& CAM_RETRY_SELTO
) != 0) {
1531 if (ccb
->ccb_h
.retry_count
> 0) {
1533 ccb
->ccb_h
.retry_count
--;
1537 * Wait a second to give the device
1538 * time to recover before we try again.
1540 relsim_flags
= RELSIM_RELEASE_AFTER_TIMEOUT
;
1546 /* Should we do more if we can't create the path?? */
1547 if (xpt_create_path(&newpath
, xpt_path_periph(ccb
->ccb_h
.path
),
1548 xpt_path_path_id(ccb
->ccb_h
.path
),
1549 xpt_path_target_id(ccb
->ccb_h
.path
),
1550 CAM_LUN_WILDCARD
) != CAM_REQ_CMP
)
1554 * Let peripheral drivers know that this device has gone
1557 xpt_async(AC_LOST_DEVICE
, newpath
, NULL
);
1558 xpt_free_path(newpath
);
1561 case CAM_REQ_INVALID
:
1562 case CAM_PATH_INVALID
:
1563 case CAM_DEV_NOT_THERE
:
1565 case CAM_PROVIDE_FAIL
:
1566 case CAM_REQ_TOO_BIG
:
1569 case CAM_SCSI_BUS_RESET
:
1572 * Commands that repeatedly timeout and cause these
1573 * kinds of error recovery actions, should return
1574 * CAM_CMD_TIMEOUT, which allows us to safely assume
1575 * that this command was an innocent bystander to
1576 * these events and should be unconditionally
1580 case CAM_REQUEUE_REQ
:
1581 /* Unconditional requeue */
1584 case CAM_RESRC_UNAVAIL
:
1588 /* decrement the number of retries */
1589 if (ccb
->ccb_h
.retry_count
> 0) {
1590 ccb
->ccb_h
.retry_count
--;
1594 action_string
= "Retries Exhausted";
1599 /* Attempt a retry */
1600 if (error
== ERESTART
|| error
== 0) {
1602 ccb
->ccb_h
.status
&= ~CAM_DEV_QFRZN
;
1604 if (error
== ERESTART
) {
1605 action_string
= "Retrying Command";
1610 cam_release_devq(ccb
->ccb_h
.path
,
1614 /*getcount_only*/0);
1618 * If we have an error and are booting verbosely, whine
1619 * *unless* this was a non-retryable selection timeout.
1621 if (error
!= 0 && bootverbose
&&
1622 !(status
== CAM_SEL_TIMEOUT
&& (camflags
& CAM_RETRY_SELTO
) == 0)) {
1625 if (action_string
== NULL
)
1626 action_string
= "Unretryable Error";
1627 if (error
!= ERESTART
) {
1628 xpt_print_path(ccb
->ccb_h
.path
);
1629 kprintf("error %d\n", error
);
1631 xpt_print_path(ccb
->ccb_h
.path
);
1632 kprintf("%s\n", action_string
);