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.22 2007/11/13 00:28:27 pavalos 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>
45 #include <sys/thread2.h>
49 #include "cam_xpt_periph.h"
50 #include "cam_periph.h"
51 #include "cam_debug.h"
53 #include <bus/cam/scsi/scsi_all.h>
54 #include <bus/cam/scsi/scsi_message.h>
55 #include <bus/cam/scsi/scsi_pass.h>
57 static u_int camperiphnextunit(struct periph_driver *p_drv,
58 u_int newunit, int wired,
59 path_id_t pathid, target_id_t target,
61 static u_int camperiphunit(struct periph_driver *p_drv,
62 path_id_t pathid, target_id_t target,
64 static void camperiphdone(struct cam_periph *periph,
66 static void camperiphfree(struct cam_periph *periph);
69 cam_periph_alloc(periph_ctor_t *periph_ctor,
70 periph_oninv_t *periph_oninvalidate,
71 periph_dtor_t *periph_dtor, periph_start_t *periph_start,
72 char *name, cam_periph_type type, struct cam_path *path,
73 ac_callback_t *ac_callback, ac_code code, void *arg)
75 struct periph_driver **p_drv;
76 struct cam_periph *periph;
77 struct cam_periph *cur_periph;
79 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 kprintf("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 = kmalloc(sizeof(*periph), M_DEVBUF, M_INTWAIT | M_ZERO);
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 cam_init_pinfo(&periph->pinfo);
121 periph->periph_start = periph_start;
122 periph->periph_dtor = periph_dtor;
123 periph->periph_oninval = periph_oninvalidate;
125 periph->periph_name = name;
126 periph->unit_number = camperiphunit(*p_drv, path_id, target_id, lun_id);
127 periph->immediate_priority = CAM_PRIORITY_NONE;
128 periph->refcount = 0;
129 SLIST_INIT(&periph->ccb_list);
130 status = xpt_create_path(&path, periph, path_id, target_id, lun_id);
131 if (status != CAM_REQ_CMP)
137 status = xpt_add_periph(periph);
139 if (status != CAM_REQ_CMP)
143 cur_periph = TAILQ_FIRST(&(*p_drv)->units);
144 while (cur_periph != NULL
145 && cur_periph->unit_number < periph->unit_number)
146 cur_periph = TAILQ_NEXT(cur_periph, unit_links);
148 if (cur_periph != NULL)
149 TAILQ_INSERT_BEFORE(cur_periph, periph, unit_links);
151 TAILQ_INSERT_TAIL(&(*p_drv)->units, periph, unit_links);
152 (*p_drv)->generation++;
159 status = periph_ctor(periph, arg);
161 if (status == CAM_REQ_CMP)
165 switch (init_level) {
167 /* Initialized successfully */
171 TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
173 xpt_remove_periph(periph);
175 xpt_free_path(periph->path);
177 kfree(periph, M_DEVBUF);
179 /* No cleanup to perform. */
182 panic("cam_periph_alloc: Unknown init level");
188 * Find a peripheral structure with the specified path, target, lun,
189 * and (optionally) type. If the name is NULL, this function will return
190 * the first peripheral driver that matches the specified path.
193 cam_periph_find(struct cam_path *path, char *name)
195 struct periph_driver **p_drv;
196 struct cam_periph *periph;
198 SET_FOREACH(p_drv, periphdriver_set) {
199 if (name != NULL && (strcmp((*p_drv)->driver_name, name) != 0))
203 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
204 periph = TAILQ_NEXT(periph, unit_links)) {
205 if (xpt_path_comp(periph->path, path) == 0) {
218 cam_periph_acquire(struct cam_periph *periph)
221 return(CAM_REQ_CMP_ERR);
231 cam_periph_release(struct cam_periph *periph)
237 if ((--periph->refcount == 0)
238 && (periph->flags & CAM_PERIPH_INVALID)) {
239 camperiphfree(periph);
245 * Look for the next unit number that is not currently in use for this
246 * peripheral type starting at "newunit". Also exclude unit numbers that
247 * are reserved by for future "hardwiring" unless we already know that this
248 * is a potential wired device. Only assume that the device is "wired" the
249 * first time through the loop since after that we'll be looking at unit
250 * numbers that did not match a wiring entry.
253 camperiphnextunit(struct periph_driver *p_drv, u_int newunit, int wired,
254 path_id_t pathid, target_id_t target, lun_id_t lun)
256 struct cam_periph *periph;
257 char *periph_name, *strval;
262 periph_name = p_drv->driver_name;
265 for (periph = TAILQ_FIRST(&p_drv->units);
266 periph != NULL && periph->unit_number != newunit;
267 periph = TAILQ_NEXT(periph, unit_links))
270 if (periph != NULL && periph->unit_number == newunit) {
272 xpt_print_path(periph->path);
273 kprintf("Duplicate Wired Device entry!\n");
274 xpt_print_path(periph->path);
275 kprintf("Second device (%s device at scbus%d "
276 "target %d lun %d) will not be wired\n",
277 periph_name, pathid, target, lun);
286 * Don't match entries like "da 4" as a wired down
287 * device, but do match entries like "da 4 target 5"
288 * or even "da 4 scbus 1".
291 while ((i = resource_locate(i, periph_name)) != -1) {
292 dname = resource_query_name(i);
293 dunit = resource_query_unit(i);
294 /* if no "target" and no specific scbus, skip */
295 if (resource_int_value(dname, dunit, "target", &val) &&
296 (resource_string_value(dname, dunit, "at",&strval)||
297 strcmp(strval, "scbus") == 0))
299 if (newunit == dunit)
310 camperiphunit(struct periph_driver *p_drv, path_id_t pathid,
311 target_id_t target, lun_id_t lun)
314 int hit, i, val, dunit;
316 char pathbuf[32], *strval, *periph_name;
320 periph_name = p_drv->driver_name;
321 ksnprintf(pathbuf, sizeof(pathbuf), "scbus%d", pathid);
323 for (hit = 0; (i = resource_locate(i, periph_name)) != -1; hit = 0) {
324 dname = resource_query_name(i);
325 dunit = resource_query_unit(i);
326 if (resource_string_value(dname, dunit, "at", &strval) == 0) {
327 if (strcmp(strval, pathbuf) != 0)
331 if (resource_int_value(dname, dunit, "target", &val) == 0) {
336 if (resource_int_value(dname, dunit, "lun", &val) == 0) {
348 * Either start from 0 looking for the next unit or from
349 * the unit number given in the resource config. This way,
350 * if we have wildcard matches, we don't return the same
353 unit = camperiphnextunit(p_drv, unit, /*wired*/hit, pathid,
360 cam_periph_invalidate(struct cam_periph *periph)
363 * We only call this routine the first time a peripheral is
364 * invalidated. The oninvalidate() routine is always called in
365 * a critical section.
368 if (((periph->flags & CAM_PERIPH_INVALID) == 0)
369 && (periph->periph_oninval != NULL))
370 periph->periph_oninval(periph);
372 periph->flags |= CAM_PERIPH_INVALID;
373 periph->flags &= ~CAM_PERIPH_NEW_DEV_FOUND;
375 if (periph->refcount == 0)
376 camperiphfree(periph);
377 else if (periph->refcount < 0)
378 kprintf("cam_invalidate_periph: refcount < 0!!\n");
383 camperiphfree(struct cam_periph *periph)
385 struct periph_driver **p_drv;
387 SET_FOREACH(p_drv, periphdriver_set) {
388 if (strcmp((*p_drv)->driver_name, periph->periph_name) == 0)
392 if (*p_drv == NULL) {
393 kprintf("camperiphfree: attempt to free "
394 "non-existent periph: %s\n", periph->periph_name);
398 if (periph->periph_dtor != NULL)
399 periph->periph_dtor(periph);
402 TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
403 (*p_drv)->generation++;
406 xpt_remove_periph(periph);
408 if (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) {
412 switch (periph->deferred_ac) {
413 case AC_FOUND_DEVICE:
414 ccb.ccb_h.func_code = XPT_GDEV_TYPE;
415 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/ 1);
419 case AC_PATH_REGISTERED:
420 ccb.ccb_h.func_code = XPT_PATH_INQ;
421 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/ 1);
429 periph->deferred_callback(NULL, periph->deferred_ac,
432 xpt_free_path(periph->path);
433 kfree(periph, M_DEVBUF);
437 * Wait interruptibly for an exclusive lock.
440 cam_periph_lock(struct cam_periph *periph, int flags)
444 while ((periph->flags & CAM_PERIPH_LOCKED) != 0) {
445 periph->flags |= CAM_PERIPH_LOCK_WANTED;
446 if ((error = tsleep(periph, flags, "caplck", 0)) != 0)
450 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
453 periph->flags |= CAM_PERIPH_LOCKED;
458 * Unlock and wake up any waiters.
461 cam_periph_unlock(struct cam_periph *periph)
463 periph->flags &= ~CAM_PERIPH_LOCKED;
464 if ((periph->flags & CAM_PERIPH_LOCK_WANTED) != 0) {
465 periph->flags &= ~CAM_PERIPH_LOCK_WANTED;
469 cam_periph_release(periph);
473 * Map user virtual pointers into kernel virtual address space, so we can
474 * access the memory. This won't work on physical pointers, for now it's
475 * up to the caller to check for that. (XXX KDM -- should we do that here
476 * instead?) This also only works for up to MAXPHYS memory. Since we use
477 * buffers to map stuff in and out, we're limited to the buffer size.
480 cam_periph_mapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
483 buf_cmd_t cmd[CAM_PERIPH_MAXMAPS];
484 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
485 u_int32_t lengths[CAM_PERIPH_MAXMAPS];
486 u_int32_t dirs[CAM_PERIPH_MAXMAPS];
488 switch(ccb->ccb_h.func_code) {
490 if (ccb->cdm.match_buf_len == 0) {
491 kprintf("cam_periph_mapmem: invalid match buffer "
495 if (ccb->cdm.pattern_buf_len > 0) {
496 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
497 lengths[0] = ccb->cdm.pattern_buf_len;
498 dirs[0] = CAM_DIR_OUT;
499 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
500 lengths[1] = ccb->cdm.match_buf_len;
501 dirs[1] = CAM_DIR_IN;
504 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
505 lengths[0] = ccb->cdm.match_buf_len;
506 dirs[0] = CAM_DIR_IN;
511 case XPT_CONT_TARGET_IO:
512 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
515 data_ptrs[0] = &ccb->csio.data_ptr;
516 lengths[0] = ccb->csio.dxfer_len;
517 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
522 break; /* NOTREACHED */
526 * Check the transfer length and permissions first, so we don't
527 * have to unmap any previously mapped buffers.
529 for (i = 0; i < numbufs; i++) {
531 * Its kinda bogus, we need a R+W command. For now the
532 * buffer needs some sort of command. Use BUF_CMD_WRITE
533 * to indicate a write and BUF_CMD_READ to indicate R+W.
535 cmd[i] = BUF_CMD_WRITE;
538 * The userland data pointer passed in may not be page
539 * aligned. vmapbuf() truncates the address to a page
540 * boundary, so if the address isn't page aligned, we'll
541 * need enough space for the given transfer length, plus
542 * whatever extra space is necessary to make it to the page
546 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)) > DFLTPHYS){
547 kprintf("cam_periph_mapmem: attempt to map %lu bytes, "
548 "which is greater than DFLTPHYS(%d)\n",
550 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)),
555 if (dirs[i] & CAM_DIR_OUT) {
556 if (!useracc(*data_ptrs[i], lengths[i],
558 kprintf("cam_periph_mapmem: error, "
559 "address %p, length %lu isn't "
560 "user accessible for READ\n",
561 (void *)*data_ptrs[i],
567 if (dirs[i] & CAM_DIR_IN) {
568 cmd[i] = BUF_CMD_READ;
569 if (!useracc(*data_ptrs[i], lengths[i],
571 kprintf("cam_periph_mapmem: error, "
572 "address %p, length %lu isn't "
573 "user accessible for WRITE\n",
574 (void *)*data_ptrs[i],
583 for (i = 0; i < numbufs; i++) {
587 mapinfo->bp[i] = getpbuf(NULL);
589 /* save the original user pointer */
590 mapinfo->saved_ptrs[i] = *data_ptrs[i];
593 mapinfo->bp[i]->b_cmd = cmd[i];
595 /* map the user buffer into kernel memory */
596 if (vmapbuf(mapinfo->bp[i], *data_ptrs[i], lengths[i]) < 0) {
597 kprintf("cam_periph_mapmem: error, "
598 "address %p, length %lu isn't "
599 "user accessible any more\n",
600 (void *)*data_ptrs[i],
602 for (j = 0; j < i; ++j) {
603 *data_ptrs[j] = mapinfo->saved_ptrs[j];
604 vunmapbuf(mapinfo->bp[j]);
605 relpbuf(mapinfo->bp[j], NULL);
607 mapinfo->num_bufs_used -= i;
611 /* set our pointer to the new mapped area */
612 *data_ptrs[i] = mapinfo->bp[i]->b_data;
614 mapinfo->num_bufs_used++;
621 * Unmap memory segments mapped into kernel virtual address space by
622 * cam_periph_mapmem().
625 cam_periph_unmapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
628 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
630 if (mapinfo->num_bufs_used <= 0) {
631 /* allow ourselves to be swapped once again */
635 switch (ccb->ccb_h.func_code) {
637 numbufs = min(mapinfo->num_bufs_used, 2);
640 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
642 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
643 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
647 case XPT_CONT_TARGET_IO:
648 data_ptrs[0] = &ccb->csio.data_ptr;
649 numbufs = min(mapinfo->num_bufs_used, 1);
652 /* allow ourselves to be swapped once again */
654 break; /* NOTREACHED */
657 for (i = 0; i < numbufs; i++) {
658 /* Set the user's pointer back to the original value */
659 *data_ptrs[i] = mapinfo->saved_ptrs[i];
661 /* unmap the buffer */
662 vunmapbuf(mapinfo->bp[i]);
664 /* release the buffer */
665 relpbuf(mapinfo->bp[i], NULL);
668 /* allow ourselves to be swapped once again */
672 cam_periph_getccb(struct cam_periph *periph, u_int32_t priority)
674 struct ccb_hdr *ccb_h;
676 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdgetccb\n"));
680 while (periph->ccb_list.slh_first == NULL) {
681 if (periph->immediate_priority > priority)
682 periph->immediate_priority = priority;
683 xpt_schedule(periph, priority);
684 if ((periph->ccb_list.slh_first != NULL)
685 && (periph->ccb_list.slh_first->pinfo.priority == priority))
687 tsleep(&periph->ccb_list, 0, "cgticb", 0);
690 ccb_h = periph->ccb_list.slh_first;
691 SLIST_REMOVE_HEAD(&periph->ccb_list, periph_links.sle);
693 return ((union ccb *)ccb_h);
697 cam_periph_ccbwait(union ccb *ccb)
700 if ((ccb->ccb_h.pinfo.index != CAM_UNQUEUED_INDEX)
701 || ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG))
702 tsleep(&ccb->ccb_h.cbfcnp, 0, "cbwait", 0);
707 cam_periph_ioctl(struct cam_periph *periph, int cmd, caddr_t addr,
708 int (*error_routine)(union ccb *ccb,
710 u_int32_t sense_flags))
720 ccb = cam_periph_getccb(periph, /* priority */ 1);
721 xpt_setup_ccb(&ccb->ccb_h,
724 ccb->ccb_h.func_code = XPT_GDEVLIST;
727 * Basically, the point of this is that we go through
728 * getting the list of devices, until we find a passthrough
729 * device. In the current version of the CAM code, the
730 * only way to determine what type of device we're dealing
731 * with is by its name.
735 ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS;
736 while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) {
738 /* we want the next device in the list */
740 if (strncmp(ccb->cgdl.periph_name,
746 if ((ccb->cgdl.status == CAM_GDEVLIST_LAST_DEVICE) &&
748 ccb->cgdl.periph_name[0] = '\0';
749 ccb->cgdl.unit_number = 0;
754 /* copy the result back out */
755 bcopy(ccb, addr, sizeof(union ccb));
757 /* and release the ccb */
758 xpt_release_ccb(ccb);
769 cam_periph_runccb(union ccb *ccb,
770 int (*error_routine)(union ccb *ccb,
772 u_int32_t sense_flags),
773 cam_flags camflags, u_int32_t sense_flags,
781 * If the user has supplied a stats structure, and if we understand
782 * this particular type of ccb, record the transaction start.
784 if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO))
785 devstat_start_transaction(ds);
790 cam_periph_ccbwait(ccb);
791 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
793 else if (error_routine != NULL)
794 error = (*error_routine)(ccb, camflags, sense_flags);
798 } while (error == ERESTART);
800 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
801 cam_release_devq(ccb->ccb_h.path,
805 /* getcount_only */ FALSE);
807 if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO))
808 devstat_end_transaction(ds,
810 ccb->csio.tag_action & 0xf,
811 ((ccb->ccb_h.flags & CAM_DIR_MASK) ==
812 CAM_DIR_NONE) ? DEVSTAT_NO_DATA :
813 (ccb->ccb_h.flags & CAM_DIR_OUT) ?
821 cam_freeze_devq(struct cam_path *path)
823 struct ccb_hdr ccb_h;
825 xpt_setup_ccb(&ccb_h, path, /*priority*/1);
826 ccb_h.func_code = XPT_NOOP;
827 ccb_h.flags = CAM_DEV_QFREEZE;
828 xpt_action((union ccb *)&ccb_h);
832 cam_release_devq(struct cam_path *path, u_int32_t relsim_flags,
833 u_int32_t openings, u_int32_t timeout,
836 struct ccb_relsim crs;
838 xpt_setup_ccb(&crs.ccb_h, path,
840 crs.ccb_h.func_code = XPT_REL_SIMQ;
841 crs.ccb_h.flags = getcount_only ? CAM_DEV_QFREEZE : 0;
842 crs.release_flags = relsim_flags;
843 crs.openings = openings;
844 crs.release_timeout = timeout;
845 xpt_action((union ccb *)&crs);
846 return (crs.qfrozen_cnt);
849 #define saved_ccb_ptr ppriv_ptr0
851 camperiphdone(struct cam_periph *periph, union ccb *done_ccb)
856 struct scsi_start_stop_unit *scsi_cmd;
857 u_int32_t relsim_flags, timeout;
858 u_int32_t qfrozen_cnt;
860 status = done_ccb->ccb_h.status;
861 frozen = (status & CAM_DEV_QFRZN) != 0;
862 sense = (status & CAM_AUTOSNS_VALID) != 0;
863 status &= CAM_STATUS_MASK;
869 * Unfreeze the queue once if it is already frozen..
872 qfrozen_cnt = cam_release_devq(done_ccb->ccb_h.path,
884 * If we have successfully taken a device from the not
885 * ready to ready state, re-scan the device and re-get the
886 * inquiry information. Many devices (mostly disks) don't
887 * properly report their inquiry information unless they
890 if (done_ccb->ccb_h.func_code == XPT_SCSI_IO) {
891 scsi_cmd = (struct scsi_start_stop_unit *)
892 &done_ccb->csio.cdb_io.cdb_bytes;
894 if (scsi_cmd->opcode == START_STOP_UNIT)
895 xpt_async(AC_INQ_CHANGED,
896 done_ccb->ccb_h.path, NULL);
898 bcopy(done_ccb->ccb_h.saved_ccb_ptr, done_ccb,
901 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
903 xpt_action(done_ccb);
906 case CAM_SCSI_STATUS_ERROR:
907 scsi_cmd = (struct scsi_start_stop_unit *)
908 &done_ccb->csio.cdb_io.cdb_bytes;
910 struct scsi_sense_data *sense;
911 int error_code, sense_key, asc, ascq;
913 sense = &done_ccb->csio.sense_data;
914 scsi_extract_sense(sense, &error_code,
915 &sense_key, &asc, &ascq);
918 * If the error is "invalid field in CDB",
919 * and the load/eject flag is set, turn the
920 * flag off and try again. This is just in
921 * case the drive in question barfs on the
922 * load eject flag. The CAM code should set
923 * the load/eject flag by default for
928 * Should we check to see what the specific
929 * scsi status is?? Or does it not matter
930 * since we already know that there was an
931 * error, and we know what the specific
932 * error code was, and we know what the
935 if ((scsi_cmd->opcode == START_STOP_UNIT) &&
936 ((scsi_cmd->how & SSS_LOEJ) != 0) &&
937 (asc == 0x24) && (ascq == 0x00) &&
938 (done_ccb->ccb_h.retry_count > 0)) {
940 scsi_cmd->how &= ~SSS_LOEJ;
942 xpt_action(done_ccb);
944 } else if (done_ccb->ccb_h.retry_count > 0) {
946 * In this case, the error recovery
947 * command failed, but we've got
948 * some retries left on it. Give
952 /* set the timeout to .5 sec */
954 RELSIM_RELEASE_AFTER_TIMEOUT;
957 xpt_action(done_ccb);
963 * Copy the original CCB back and
964 * send it back to the caller.
966 bcopy(done_ccb->ccb_h.saved_ccb_ptr,
967 done_ccb, sizeof(union ccb));
969 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
971 xpt_action(done_ccb);
975 * Eh?? The command failed, but we don't
976 * have any sense. What's up with that?
977 * Fire the CCB again to return it to the
980 bcopy(done_ccb->ccb_h.saved_ccb_ptr,
981 done_ccb, sizeof(union ccb));
983 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
985 xpt_action(done_ccb);
990 bcopy(done_ccb->ccb_h.saved_ccb_ptr, done_ccb,
993 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
995 xpt_action(done_ccb);
1000 /* decrement the retry count */
1001 if (done_ccb->ccb_h.retry_count > 0)
1002 done_ccb->ccb_h.retry_count--;
1004 qfrozen_cnt = cam_release_devq(done_ccb->ccb_h.path,
1005 /*relsim_flags*/relsim_flags,
1008 /*getcount_only*/0);
1012 * Generic Async Event handler. Peripheral drivers usually
1013 * filter out the events that require personal attention,
1014 * and leave the rest to this function.
1017 cam_periph_async(struct cam_periph *periph, u_int32_t code,
1018 struct cam_path *path, void *arg)
1021 case AC_LOST_DEVICE:
1022 cam_periph_invalidate(periph);
1027 cam_periph_bus_settle(periph, SCSI_DELAY);
1036 cam_periph_bus_settle(struct cam_periph *periph, u_int bus_settle)
1038 struct ccb_getdevstats cgds;
1040 xpt_setup_ccb(&cgds.ccb_h, periph->path, /*priority*/1);
1041 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1042 xpt_action((union ccb *)&cgds);
1043 cam_periph_freeze_after_event(periph, &cgds.last_reset, bus_settle);
1047 cam_periph_freeze_after_event(struct cam_periph *periph,
1048 struct timeval* event_time, u_int duration_ms)
1050 struct timeval delta;
1051 struct timeval duration_tv;
1053 microuptime(&delta);
1054 timevalsub(&delta, event_time);
1055 duration_tv.tv_sec = duration_ms / 1000;
1056 duration_tv.tv_usec = (duration_ms % 1000) * 1000;
1057 if (timevalcmp(&delta, &duration_tv, <)) {
1058 timevalsub(&duration_tv, &delta);
1060 duration_ms = duration_tv.tv_sec * 1000;
1061 duration_ms += duration_tv.tv_usec / 1000;
1062 cam_freeze_devq(periph->path);
1063 cam_release_devq(periph->path,
1064 RELSIM_RELEASE_AFTER_TIMEOUT,
1066 /*timeout*/duration_ms,
1067 /*getcount_only*/0);
1073 * Generic error handler. Peripheral drivers usually filter
1074 * out the errors that they handle in a unique mannor, then
1075 * call this function.
1078 cam_periph_error(union ccb *ccb, cam_flags camflags,
1079 u_int32_t sense_flags, union ccb *save_ccb)
1087 u_int32_t relsim_flags;
1090 status = ccb->ccb_h.status;
1091 frozen = (status & CAM_DEV_QFRZN) != 0;
1092 sense = (status & CAM_AUTOSNS_VALID) != 0;
1093 status &= CAM_STATUS_MASK;
1098 /* decrement the number of retries */
1099 retry = ccb->ccb_h.retry_count > 0;
1101 ccb->ccb_h.retry_count--;
1104 case CAM_AUTOSENSE_FAIL:
1105 case CAM_SCSI_STATUS_ERROR:
1107 switch (ccb->csio.scsi_status) {
1108 case SCSI_STATUS_OK:
1109 case SCSI_STATUS_COND_MET:
1110 case SCSI_STATUS_INTERMED:
1111 case SCSI_STATUS_INTERMED_COND_MET:
1114 case SCSI_STATUS_CMD_TERMINATED:
1115 case SCSI_STATUS_CHECK_COND:
1117 struct scsi_sense_data *sense;
1118 int error_code, sense_key, asc, ascq;
1119 struct cam_periph *periph;
1120 scsi_sense_action err_action;
1121 struct ccb_getdev cgd;
1123 sense = &ccb->csio.sense_data;
1124 scsi_extract_sense(sense, &error_code,
1125 &sense_key, &asc, &ascq);
1126 periph = xpt_path_periph(ccb->ccb_h.path);
1129 * Grab the inquiry data for this device.
1131 xpt_setup_ccb(&cgd.ccb_h, ccb->ccb_h.path,
1133 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1134 xpt_action((union ccb *)&cgd);
1136 err_action = scsi_error_action(asc, ascq,
1140 * Send a Test Unit Ready to the device.
1141 * If the 'many' flag is set, we send 120
1142 * test unit ready commands, one every half
1143 * second. Otherwise, we just send one TUR.
1144 * We only want to do this if the retry
1145 * count has not been exhausted.
1147 if (((err_action & SS_MASK) == SS_TUR)
1149 && ccb->ccb_h.retry_count > 0) {
1152 * Since error recovery is already
1153 * in progress, don't attempt to
1154 * process this error. It is probably
1155 * related to the error that caused
1156 * the currently active error recovery
1157 * action. Also, we only have
1158 * space for one saved CCB, so if we
1159 * had two concurrent error recovery
1160 * actions, we would end up
1161 * over-writing one error recovery
1162 * CCB with another one.
1165 CAM_PERIPH_RECOVERY_INPROG) {
1171 CAM_PERIPH_RECOVERY_INPROG;
1173 /* decrement the number of retries */
1175 SSQ_DECREMENT_COUNT) != 0) {
1177 ccb->ccb_h.retry_count--;
1180 bcopy(ccb, save_ccb, sizeof(*save_ccb));
1183 * We retry this one every half
1184 * second for a minute. If the
1185 * device hasn't become ready in a
1186 * minute's time, it's unlikely to
1187 * ever become ready. If the table
1188 * doesn't specify SSQ_MANY, we can
1189 * only try this once. Oh well.
1191 if ((err_action & SSQ_MANY) != 0)
1192 scsi_test_unit_ready(&ccb->csio,
1199 scsi_test_unit_ready(&ccb->csio,
1206 /* release the queue after .5 sec. */
1208 RELSIM_RELEASE_AFTER_TIMEOUT;
1211 * Drop the priority to 0 so that
1212 * we are the first to execute. Also
1213 * freeze the queue after this command
1214 * is sent so that we can restore the
1215 * old csio and have it queued in the
1216 * proper order before we let normal
1217 * transactions go to the drive.
1219 ccb->ccb_h.pinfo.priority = 0;
1220 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1223 * Save a pointer to the original
1224 * CCB in the new CCB.
1226 ccb->ccb_h.saved_ccb_ptr = save_ccb;
1231 * Send a start unit command to the device,
1232 * and then retry the command. We only
1233 * want to do this if the retry count has
1234 * not been exhausted. If the user
1235 * specified 0 retries, then we follow
1236 * their request and do not retry.
1238 else if (((err_action & SS_MASK) == SS_START)
1240 && ccb->ccb_h.retry_count > 0) {
1244 * Only one error recovery action
1245 * at a time. See above.
1248 CAM_PERIPH_RECOVERY_INPROG) {
1254 CAM_PERIPH_RECOVERY_INPROG;
1256 /* decrement the number of retries */
1258 ccb->ccb_h.retry_count--;
1261 * Check for removable media and
1262 * set load/eject flag
1265 if (SID_IS_REMOVABLE(&cgd.inq_data))
1271 * Attempt to start the drive up.
1273 * Save the current ccb so it can
1274 * be restored and retried once the
1275 * drive is started up.
1277 bcopy(ccb, save_ccb, sizeof(*save_ccb));
1279 scsi_start_stop(&ccb->csio,
1289 * Drop the priority to 0 so that
1290 * we are the first to execute. Also
1291 * freeze the queue after this command
1292 * is sent so that we can restore the
1293 * old csio and have it queued in the
1294 * proper order before we let normal
1295 * transactions go to the drive.
1297 ccb->ccb_h.pinfo.priority = 0;
1298 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1301 * Save a pointer to the original
1302 * CCB in the new CCB.
1304 ccb->ccb_h.saved_ccb_ptr = save_ccb;
1307 } else if ((sense_flags & SF_RETRY_UA) != 0) {
1309 * XXX KDM this is a *horrible*
1312 error = scsi_interpret_sense(ccb,
1321 * Theoretically, this code should send a
1322 * test unit ready to the given device, and
1323 * if it returns and error, send a start
1324 * unit command. Since we don't yet have
1325 * the capability to do two-command error
1326 * recovery, just send a start unit.
1329 else if (((err_action & SS_MASK) == SS_TURSTART)
1331 && ccb->ccb_h.retry_count > 0) {
1335 * Only one error recovery action
1336 * at a time. See above.
1339 CAM_PERIPH_RECOVERY_INPROG) {
1345 CAM_PERIPH_RECOVERY_INPROG;
1347 /* decrement the number of retries */
1349 ccb->ccb_h.retry_count--;
1352 * Check for removable media and
1353 * set load/eject flag
1356 if (SID_IS_REMOVABLE(&cgd.inq_data))
1362 * Attempt to start the drive up.
1364 * Save the current ccb so it can
1365 * be restored and retried once the
1366 * drive is started up.
1368 bcopy(ccb, save_ccb, sizeof(*save_ccb));
1370 scsi_start_stop(&ccb->csio,
1380 /* release the queue after .5 sec. */
1382 RELSIM_RELEASE_AFTER_TIMEOUT;
1385 * Drop the priority to 0 so that
1386 * we are the first to execute. Also
1387 * freeze the queue after this command
1388 * is sent so that we can restore the
1389 * old csio and have it queued in the
1390 * proper order before we let normal
1391 * transactions go to the drive.
1393 ccb->ccb_h.pinfo.priority = 0;
1394 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1397 * Save a pointer to the original
1398 * CCB in the new CCB.
1400 ccb->ccb_h.saved_ccb_ptr = save_ccb;
1404 error = scsi_interpret_sense(ccb,
1411 } else if (ccb->csio.scsi_status ==
1412 SCSI_STATUS_CHECK_COND
1413 && status != CAM_AUTOSENSE_FAIL) {
1414 /* no point in decrementing the retry count */
1415 panic("cam_periph_error: scsi status of "
1416 "CHECK COND returned but no sense "
1417 "information is available. "
1418 "Controller should have returned "
1419 "CAM_AUTOSENSE_FAILED");
1422 } else if (ccb->ccb_h.retry_count == 0) {
1424 * XXX KDM shouldn't there be a better
1425 * argument to return??
1429 /* decrement the number of retries */
1430 retry = ccb->ccb_h.retry_count > 0;
1432 ccb->ccb_h.retry_count--;
1434 * If it was aborted with no
1435 * clue as to the reason, just
1441 case SCSI_STATUS_QUEUE_FULL:
1444 struct ccb_getdevstats cgds;
1447 * First off, find out what the current
1448 * transaction counts are.
1450 xpt_setup_ccb(&cgds.ccb_h,
1453 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1454 xpt_action((union ccb *)&cgds);
1457 * If we were the only transaction active, treat
1458 * the QUEUE FULL as if it were a BUSY condition.
1460 if (cgds.dev_active != 0) {
1464 * Reduce the number of openings to
1465 * be 1 less than the amount it took
1466 * to get a queue full bounded by the
1467 * minimum allowed tag count for this
1471 cgds.dev_active+cgds.dev_openings;
1472 openings = cgds.dev_active;
1473 if (openings < cgds.mintags)
1474 openings = cgds.mintags;
1475 if (openings < total_openings)
1476 relsim_flags = RELSIM_ADJUST_OPENINGS;
1479 * Some devices report queue full for
1480 * temporary resource shortages. For
1481 * this reason, we allow a minimum
1482 * tag count to be entered via a
1483 * quirk entry to prevent the queue
1484 * count on these devices from falling
1485 * to a pessimisticly low value. We
1486 * still wait for the next successful
1487 * completion, however, before queueing
1488 * more transactions to the device.
1491 RELSIM_RELEASE_AFTER_CMDCMPLT;
1499 case SCSI_STATUS_BUSY:
1501 * Restart the queue after either another
1502 * command completes or a 1 second timeout.
1503 * If we have any retries left, that is.
1505 retry = ccb->ccb_h.retry_count > 0;
1507 ccb->ccb_h.retry_count--;
1509 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT
1510 | RELSIM_RELEASE_AFTER_CMDCMPLT;
1516 case SCSI_STATUS_RESERV_CONFLICT:
1524 case CAM_REQ_CMP_ERR:
1525 case CAM_CMD_TIMEOUT:
1526 case CAM_UNEXP_BUSFREE:
1527 case CAM_UNCOR_PARITY:
1528 case CAM_DATA_RUN_ERR:
1529 /* decrement the number of retries */
1530 retry = ccb->ccb_h.retry_count > 0;
1532 ccb->ccb_h.retry_count--;
1540 case CAM_MSG_REJECT_REC:
1541 /* XXX Don't know that these are correct */
1544 case CAM_SEL_TIMEOUT:
1548 * A single selection timeout should not be enough
1549 * to invalidate a device. We should retry for multiple
1550 * seconds assuming this isn't a probe. We'll probably
1551 * need a special flag for that.
1554 struct cam_path *newpath;
1556 /* Should we do more if we can't create the path?? */
1557 if (xpt_create_path(&newpath, xpt_path_periph(ccb->ccb_h.path),
1558 xpt_path_path_id(ccb->ccb_h.path),
1559 xpt_path_target_id(ccb->ccb_h.path),
1560 CAM_LUN_WILDCARD) != CAM_REQ_CMP)
1563 * Let peripheral drivers know that this device has gone
1566 xpt_async(AC_LOST_DEVICE, newpath, NULL);
1567 xpt_free_path(newpath);
1569 if ((sense_flags & SF_RETRY_SELTO) != 0) {
1570 retry = ccb->ccb_h.retry_count > 0;
1572 ccb->ccb_h.retry_count--;
1575 * Wait half a second to give the device
1576 * time to recover before we try again.
1578 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1588 case CAM_REQ_INVALID:
1589 case CAM_PATH_INVALID:
1590 case CAM_DEV_NOT_THERE:
1592 case CAM_PROVIDE_FAIL:
1593 case CAM_REQ_TOO_BIG:
1596 case CAM_SCSI_BUS_RESET:
1598 case CAM_REQUEUE_REQ:
1599 /* Unconditional requeue, dammit */
1602 case CAM_RESRC_UNAVAIL:
1606 /* decrement the number of retries */
1607 retry = ccb->ccb_h.retry_count > 0;
1609 ccb->ccb_h.retry_count--;
1612 /* Check the sense codes */
1618 /* Attempt a retry */
1619 if (error == ERESTART || error == 0) {
1621 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1623 if (error == ERESTART)
1627 cam_release_devq(ccb->ccb_h.path,
1631 /*getcount_only*/0);