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.24 2007/11/17 20:28:46 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);
67 static int nperiph_drivers;
68 struct periph_driver **periph_drivers;
71 periphdriver_register(void *data)
73 struct periph_driver **newdrivers, **old;
76 ndrivers = nperiph_drivers + 2;
77 newdrivers = kmalloc(sizeof(*newdrivers) * ndrivers, M_TEMP, M_WAITOK);
79 bcopy(periph_drivers, newdrivers,
80 sizeof(*newdrivers) * nperiph_drivers);
81 newdrivers[nperiph_drivers] = (struct periph_driver *)data;
82 newdrivers[nperiph_drivers + 1] = NULL;
84 periph_drivers = newdrivers;
91 cam_periph_alloc(periph_ctor_t *periph_ctor,
92 periph_oninv_t *periph_oninvalidate,
93 periph_dtor_t *periph_dtor, periph_start_t *periph_start,
94 char *name, cam_periph_type type, struct cam_path *path,
95 ac_callback_t *ac_callback, ac_code code, void *arg)
97 struct periph_driver **p_drv;
98 struct cam_periph *periph;
99 struct cam_periph *cur_periph;
101 target_id_t target_id;
108 * Handle Hot-Plug scenarios. If there is already a peripheral
109 * of our type assigned to this path, we are likely waiting for
110 * final close on an old, invalidated, peripheral. If this is
111 * the case, queue up a deferred call to the peripheral's async
112 * handler. If it looks like a mistaken re-alloation, complain.
114 if ((periph = cam_periph_find(path, name)) != NULL) {
116 if ((periph->flags & CAM_PERIPH_INVALID) != 0
117 && (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) == 0) {
118 periph->flags |= CAM_PERIPH_NEW_DEV_FOUND;
119 periph->deferred_callback = ac_callback;
120 periph->deferred_ac = code;
121 return (CAM_REQ_INPROG);
123 kprintf("cam_periph_alloc: attempt to re-allocate "
124 "valid device %s%d rejected\n",
125 periph->periph_name, periph->unit_number);
127 return (CAM_REQ_INVALID);
130 periph = kmalloc(sizeof(*periph), M_DEVBUF, M_INTWAIT | M_ZERO);
134 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
135 if (strcmp((*p_drv)->driver_name, name) == 0)
139 path_id = xpt_path_path_id(path);
140 target_id = xpt_path_target_id(path);
141 lun_id = xpt_path_lun_id(path);
142 cam_init_pinfo(&periph->pinfo);
143 periph->periph_start = periph_start;
144 periph->periph_dtor = periph_dtor;
145 periph->periph_oninval = periph_oninvalidate;
147 periph->periph_name = name;
148 periph->unit_number = camperiphunit(*p_drv, path_id, target_id, lun_id);
149 periph->immediate_priority = CAM_PRIORITY_NONE;
150 periph->refcount = 0;
151 SLIST_INIT(&periph->ccb_list);
152 status = xpt_create_path(&path, periph, path_id, target_id, lun_id);
153 if (status != CAM_REQ_CMP)
159 status = xpt_add_periph(periph);
161 if (status != CAM_REQ_CMP)
165 cur_periph = TAILQ_FIRST(&(*p_drv)->units);
166 while (cur_periph != NULL
167 && cur_periph->unit_number < periph->unit_number)
168 cur_periph = TAILQ_NEXT(cur_periph, unit_links);
170 if (cur_periph != NULL)
171 TAILQ_INSERT_BEFORE(cur_periph, periph, unit_links);
173 TAILQ_INSERT_TAIL(&(*p_drv)->units, periph, unit_links);
174 (*p_drv)->generation++;
181 status = periph_ctor(periph, arg);
183 if (status == CAM_REQ_CMP)
187 switch (init_level) {
189 /* Initialized successfully */
193 TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
195 xpt_remove_periph(periph);
197 xpt_free_path(periph->path);
199 kfree(periph, M_DEVBUF);
201 /* No cleanup to perform. */
204 panic("cam_periph_alloc: Unknown init level");
210 * Find a peripheral structure with the specified path, target, lun,
211 * and (optionally) type. If the name is NULL, this function will return
212 * the first peripheral driver that matches the specified path.
215 cam_periph_find(struct cam_path *path, char *name)
217 struct periph_driver **p_drv;
218 struct cam_periph *periph;
220 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
221 if (name != NULL && (strcmp((*p_drv)->driver_name, name) != 0))
225 TAILQ_FOREACH(periph, &(*p_drv)->units, unit_links) {
226 if (xpt_path_comp(periph->path, path) == 0) {
239 cam_periph_acquire(struct cam_periph *periph)
242 return(CAM_REQ_CMP_ERR);
252 cam_periph_release(struct cam_periph *periph)
258 if ((--periph->refcount == 0)
259 && (periph->flags & CAM_PERIPH_INVALID)) {
260 camperiphfree(periph);
266 * Look for the next unit number that is not currently in use for this
267 * peripheral type starting at "newunit". Also exclude unit numbers that
268 * are reserved by for future "hardwiring" unless we already know that this
269 * is a potential wired device. Only assume that the device is "wired" the
270 * first time through the loop since after that we'll be looking at unit
271 * numbers that did not match a wiring entry.
274 camperiphnextunit(struct periph_driver *p_drv, u_int newunit, int wired,
275 path_id_t pathid, target_id_t target, lun_id_t lun)
277 struct cam_periph *periph;
278 char *periph_name, *strval;
283 periph_name = p_drv->driver_name;
286 for (periph = TAILQ_FIRST(&p_drv->units);
287 periph != NULL && periph->unit_number != newunit;
288 periph = TAILQ_NEXT(periph, unit_links))
291 if (periph != NULL && periph->unit_number == newunit) {
293 xpt_print_path(periph->path);
294 kprintf("Duplicate Wired Device entry!\n");
295 xpt_print_path(periph->path);
296 kprintf("Second device (%s device at scbus%d "
297 "target %d lun %d) will not be wired\n",
298 periph_name, pathid, target, lun);
307 * Don't match entries like "da 4" as a wired down
308 * device, but do match entries like "da 4 target 5"
309 * or even "da 4 scbus 1".
312 while ((i = resource_locate(i, periph_name)) != -1) {
313 dname = resource_query_name(i);
314 dunit = resource_query_unit(i);
315 /* if no "target" and no specific scbus, skip */
316 if (resource_int_value(dname, dunit, "target", &val) &&
317 (resource_string_value(dname, dunit, "at",&strval)||
318 strcmp(strval, "scbus") == 0))
320 if (newunit == dunit)
331 camperiphunit(struct periph_driver *p_drv, path_id_t pathid,
332 target_id_t target, lun_id_t lun)
335 int hit, i, val, dunit;
337 char pathbuf[32], *strval, *periph_name;
341 periph_name = p_drv->driver_name;
342 ksnprintf(pathbuf, sizeof(pathbuf), "scbus%d", pathid);
344 for (hit = 0; (i = resource_locate(i, periph_name)) != -1; hit = 0) {
345 dname = resource_query_name(i);
346 dunit = resource_query_unit(i);
347 if (resource_string_value(dname, dunit, "at", &strval) == 0) {
348 if (strcmp(strval, pathbuf) != 0)
352 if (resource_int_value(dname, dunit, "target", &val) == 0) {
357 if (resource_int_value(dname, dunit, "lun", &val) == 0) {
369 * Either start from 0 looking for the next unit or from
370 * the unit number given in the resource config. This way,
371 * if we have wildcard matches, we don't return the same
374 unit = camperiphnextunit(p_drv, unit, /*wired*/hit, pathid,
381 cam_periph_invalidate(struct cam_periph *periph)
384 * We only call this routine the first time a peripheral is
385 * invalidated. The oninvalidate() routine is always called in
386 * a critical section.
389 if (((periph->flags & CAM_PERIPH_INVALID) == 0)
390 && (periph->periph_oninval != NULL))
391 periph->periph_oninval(periph);
393 periph->flags |= CAM_PERIPH_INVALID;
394 periph->flags &= ~CAM_PERIPH_NEW_DEV_FOUND;
396 if (periph->refcount == 0)
397 camperiphfree(periph);
398 else if (periph->refcount < 0)
399 kprintf("cam_invalidate_periph: refcount < 0!!\n");
404 camperiphfree(struct cam_periph *periph)
406 struct periph_driver **p_drv;
408 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
409 if (strcmp((*p_drv)->driver_name, periph->periph_name) == 0)
413 if (*p_drv == NULL) {
414 kprintf("camperiphfree: attempt to free "
415 "non-existent periph: %s\n", periph->periph_name);
419 if (periph->periph_dtor != NULL)
420 periph->periph_dtor(periph);
423 TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
424 (*p_drv)->generation++;
427 xpt_remove_periph(periph);
429 if (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) {
433 switch (periph->deferred_ac) {
434 case AC_FOUND_DEVICE:
435 ccb.ccb_h.func_code = XPT_GDEV_TYPE;
436 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/ 1);
440 case AC_PATH_REGISTERED:
441 ccb.ccb_h.func_code = XPT_PATH_INQ;
442 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/ 1);
450 periph->deferred_callback(NULL, periph->deferred_ac,
453 xpt_free_path(periph->path);
454 kfree(periph, M_DEVBUF);
458 * Wait interruptibly for an exclusive lock.
461 cam_periph_lock(struct cam_periph *periph, int flags)
465 while ((periph->flags & CAM_PERIPH_LOCKED) != 0) {
466 periph->flags |= CAM_PERIPH_LOCK_WANTED;
467 if ((error = tsleep(periph, flags, "caplck", 0)) != 0)
471 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
474 periph->flags |= CAM_PERIPH_LOCKED;
479 * Unlock and wake up any waiters.
482 cam_periph_unlock(struct cam_periph *periph)
484 periph->flags &= ~CAM_PERIPH_LOCKED;
485 if ((periph->flags & CAM_PERIPH_LOCK_WANTED) != 0) {
486 periph->flags &= ~CAM_PERIPH_LOCK_WANTED;
490 cam_periph_release(periph);
494 * Map user virtual pointers into kernel virtual address space, so we can
495 * access the memory. This won't work on physical pointers, for now it's
496 * up to the caller to check for that. (XXX KDM -- should we do that here
497 * instead?) This also only works for up to MAXPHYS memory. Since we use
498 * buffers to map stuff in and out, we're limited to the buffer size.
501 cam_periph_mapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
504 buf_cmd_t cmd[CAM_PERIPH_MAXMAPS];
505 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
506 u_int32_t lengths[CAM_PERIPH_MAXMAPS];
507 u_int32_t dirs[CAM_PERIPH_MAXMAPS];
509 switch(ccb->ccb_h.func_code) {
511 if (ccb->cdm.match_buf_len == 0) {
512 kprintf("cam_periph_mapmem: invalid match buffer "
516 if (ccb->cdm.pattern_buf_len > 0) {
517 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
518 lengths[0] = ccb->cdm.pattern_buf_len;
519 dirs[0] = CAM_DIR_OUT;
520 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
521 lengths[1] = ccb->cdm.match_buf_len;
522 dirs[1] = CAM_DIR_IN;
525 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
526 lengths[0] = ccb->cdm.match_buf_len;
527 dirs[0] = CAM_DIR_IN;
532 case XPT_CONT_TARGET_IO:
533 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
536 data_ptrs[0] = &ccb->csio.data_ptr;
537 lengths[0] = ccb->csio.dxfer_len;
538 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
543 break; /* NOTREACHED */
547 * Check the transfer length and permissions first, so we don't
548 * have to unmap any previously mapped buffers.
550 for (i = 0; i < numbufs; i++) {
552 * Its kinda bogus, we need a R+W command. For now the
553 * buffer needs some sort of command. Use BUF_CMD_WRITE
554 * to indicate a write and BUF_CMD_READ to indicate R+W.
556 cmd[i] = BUF_CMD_WRITE;
559 * The userland data pointer passed in may not be page
560 * aligned. vmapbuf() truncates the address to a page
561 * boundary, so if the address isn't page aligned, we'll
562 * need enough space for the given transfer length, plus
563 * whatever extra space is necessary to make it to the page
567 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)) > DFLTPHYS){
568 kprintf("cam_periph_mapmem: attempt to map %lu bytes, "
569 "which is greater than DFLTPHYS(%d)\n",
571 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)),
576 if (dirs[i] & CAM_DIR_OUT) {
577 if (!useracc(*data_ptrs[i], lengths[i],
579 kprintf("cam_periph_mapmem: error, "
580 "address %p, length %lu isn't "
581 "user accessible for READ\n",
582 (void *)*data_ptrs[i],
588 if (dirs[i] & CAM_DIR_IN) {
589 cmd[i] = BUF_CMD_READ;
590 if (!useracc(*data_ptrs[i], lengths[i],
592 kprintf("cam_periph_mapmem: error, "
593 "address %p, length %lu isn't "
594 "user accessible for WRITE\n",
595 (void *)*data_ptrs[i],
604 for (i = 0; i < numbufs; i++) {
608 mapinfo->bp[i] = getpbuf(NULL);
610 /* save the original user pointer */
611 mapinfo->saved_ptrs[i] = *data_ptrs[i];
614 mapinfo->bp[i]->b_cmd = cmd[i];
616 /* map the user buffer into kernel memory */
617 if (vmapbuf(mapinfo->bp[i], *data_ptrs[i], lengths[i]) < 0) {
618 kprintf("cam_periph_mapmem: error, "
619 "address %p, length %lu isn't "
620 "user accessible any more\n",
621 (void *)*data_ptrs[i],
623 for (j = 0; j < i; ++j) {
624 *data_ptrs[j] = mapinfo->saved_ptrs[j];
625 vunmapbuf(mapinfo->bp[j]);
626 relpbuf(mapinfo->bp[j], NULL);
628 mapinfo->num_bufs_used -= i;
632 /* set our pointer to the new mapped area */
633 *data_ptrs[i] = mapinfo->bp[i]->b_data;
635 mapinfo->num_bufs_used++;
642 * Unmap memory segments mapped into kernel virtual address space by
643 * cam_periph_mapmem().
646 cam_periph_unmapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
649 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
651 if (mapinfo->num_bufs_used <= 0) {
652 /* allow ourselves to be swapped once again */
656 switch (ccb->ccb_h.func_code) {
658 numbufs = min(mapinfo->num_bufs_used, 2);
661 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
663 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
664 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
668 case XPT_CONT_TARGET_IO:
669 data_ptrs[0] = &ccb->csio.data_ptr;
670 numbufs = min(mapinfo->num_bufs_used, 1);
673 /* allow ourselves to be swapped once again */
675 break; /* NOTREACHED */
678 for (i = 0; i < numbufs; i++) {
679 /* Set the user's pointer back to the original value */
680 *data_ptrs[i] = mapinfo->saved_ptrs[i];
682 /* unmap the buffer */
683 vunmapbuf(mapinfo->bp[i]);
685 /* release the buffer */
686 relpbuf(mapinfo->bp[i], NULL);
689 /* allow ourselves to be swapped once again */
693 cam_periph_getccb(struct cam_periph *periph, u_int32_t priority)
695 struct ccb_hdr *ccb_h;
697 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdgetccb\n"));
701 while (SLIST_FIRST(&periph->ccb_list) == NULL) {
702 if (periph->immediate_priority > priority)
703 periph->immediate_priority = priority;
704 xpt_schedule(periph, priority);
705 if ((SLIST_FIRST(&periph->ccb_list) != NULL)
706 && (SLIST_FIRST(&periph->ccb_list)->pinfo.priority == priority))
708 tsleep(&periph->ccb_list, 0, "cgticb", 0);
711 ccb_h = SLIST_FIRST(&periph->ccb_list);
712 SLIST_REMOVE_HEAD(&periph->ccb_list, periph_links.sle);
714 return ((union ccb *)ccb_h);
718 cam_periph_ccbwait(union ccb *ccb)
721 if ((ccb->ccb_h.pinfo.index != CAM_UNQUEUED_INDEX)
722 || ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG))
723 tsleep(&ccb->ccb_h.cbfcnp, 0, "cbwait", 0);
728 cam_periph_ioctl(struct cam_periph *periph, int cmd, caddr_t addr,
729 int (*error_routine)(union ccb *ccb,
731 u_int32_t sense_flags))
741 ccb = cam_periph_getccb(periph, /* priority */ 1);
742 xpt_setup_ccb(&ccb->ccb_h,
745 ccb->ccb_h.func_code = XPT_GDEVLIST;
748 * Basically, the point of this is that we go through
749 * getting the list of devices, until we find a passthrough
750 * device. In the current version of the CAM code, the
751 * only way to determine what type of device we're dealing
752 * with is by its name.
756 ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS;
757 while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) {
759 /* we want the next device in the list */
761 if (strncmp(ccb->cgdl.periph_name,
767 if ((ccb->cgdl.status == CAM_GDEVLIST_LAST_DEVICE) &&
769 ccb->cgdl.periph_name[0] = '\0';
770 ccb->cgdl.unit_number = 0;
775 /* copy the result back out */
776 bcopy(ccb, addr, sizeof(union ccb));
778 /* and release the ccb */
779 xpt_release_ccb(ccb);
790 cam_periph_runccb(union ccb *ccb,
791 int (*error_routine)(union ccb *ccb,
793 u_int32_t sense_flags),
794 cam_flags camflags, u_int32_t sense_flags,
802 * If the user has supplied a stats structure, and if we understand
803 * this particular type of ccb, record the transaction start.
805 if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO))
806 devstat_start_transaction(ds);
811 cam_periph_ccbwait(ccb);
812 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
814 else if (error_routine != NULL)
815 error = (*error_routine)(ccb, camflags, sense_flags);
819 } while (error == ERESTART);
821 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
822 cam_release_devq(ccb->ccb_h.path,
826 /* getcount_only */ FALSE);
828 if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO))
829 devstat_end_transaction(ds,
831 ccb->csio.tag_action & 0xf,
832 ((ccb->ccb_h.flags & CAM_DIR_MASK) ==
833 CAM_DIR_NONE) ? DEVSTAT_NO_DATA :
834 (ccb->ccb_h.flags & CAM_DIR_OUT) ?
842 cam_freeze_devq(struct cam_path *path)
844 struct ccb_hdr ccb_h;
846 xpt_setup_ccb(&ccb_h, path, /*priority*/1);
847 ccb_h.func_code = XPT_NOOP;
848 ccb_h.flags = CAM_DEV_QFREEZE;
849 xpt_action((union ccb *)&ccb_h);
853 cam_release_devq(struct cam_path *path, u_int32_t relsim_flags,
854 u_int32_t openings, u_int32_t timeout,
857 struct ccb_relsim crs;
859 xpt_setup_ccb(&crs.ccb_h, path,
861 crs.ccb_h.func_code = XPT_REL_SIMQ;
862 crs.ccb_h.flags = getcount_only ? CAM_DEV_QFREEZE : 0;
863 crs.release_flags = relsim_flags;
864 crs.openings = openings;
865 crs.release_timeout = timeout;
866 xpt_action((union ccb *)&crs);
867 return (crs.qfrozen_cnt);
870 #define saved_ccb_ptr ppriv_ptr0
872 camperiphdone(struct cam_periph *periph, union ccb *done_ccb)
877 struct scsi_start_stop_unit *scsi_cmd;
878 u_int32_t relsim_flags, timeout;
879 u_int32_t qfrozen_cnt;
881 status = done_ccb->ccb_h.status;
882 frozen = (status & CAM_DEV_QFRZN) != 0;
883 sense = (status & CAM_AUTOSNS_VALID) != 0;
884 status &= CAM_STATUS_MASK;
890 * Unfreeze the queue once if it is already frozen..
893 qfrozen_cnt = cam_release_devq(done_ccb->ccb_h.path,
905 * If we have successfully taken a device from the not
906 * ready to ready state, re-scan the device and re-get the
907 * inquiry information. Many devices (mostly disks) don't
908 * properly report their inquiry information unless they
911 if (done_ccb->ccb_h.func_code == XPT_SCSI_IO) {
912 scsi_cmd = (struct scsi_start_stop_unit *)
913 &done_ccb->csio.cdb_io.cdb_bytes;
915 if (scsi_cmd->opcode == START_STOP_UNIT)
916 xpt_async(AC_INQ_CHANGED,
917 done_ccb->ccb_h.path, NULL);
919 bcopy(done_ccb->ccb_h.saved_ccb_ptr, done_ccb,
922 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
924 xpt_action(done_ccb);
927 case CAM_SCSI_STATUS_ERROR:
928 scsi_cmd = (struct scsi_start_stop_unit *)
929 &done_ccb->csio.cdb_io.cdb_bytes;
931 struct scsi_sense_data *sense;
932 int error_code, sense_key, asc, ascq;
934 sense = &done_ccb->csio.sense_data;
935 scsi_extract_sense(sense, &error_code,
936 &sense_key, &asc, &ascq);
939 * If the error is "invalid field in CDB",
940 * and the load/eject flag is set, turn the
941 * flag off and try again. This is just in
942 * case the drive in question barfs on the
943 * load eject flag. The CAM code should set
944 * the load/eject flag by default for
949 * Should we check to see what the specific
950 * scsi status is?? Or does it not matter
951 * since we already know that there was an
952 * error, and we know what the specific
953 * error code was, and we know what the
956 if ((scsi_cmd->opcode == START_STOP_UNIT) &&
957 ((scsi_cmd->how & SSS_LOEJ) != 0) &&
958 (asc == 0x24) && (ascq == 0x00) &&
959 (done_ccb->ccb_h.retry_count > 0)) {
961 scsi_cmd->how &= ~SSS_LOEJ;
963 xpt_action(done_ccb);
965 } else if (done_ccb->ccb_h.retry_count > 0) {
967 * In this case, the error recovery
968 * command failed, but we've got
969 * some retries left on it. Give
973 /* set the timeout to .5 sec */
975 RELSIM_RELEASE_AFTER_TIMEOUT;
978 xpt_action(done_ccb);
984 * Copy the original CCB back and
985 * send it back to the caller.
987 bcopy(done_ccb->ccb_h.saved_ccb_ptr,
988 done_ccb, sizeof(union ccb));
990 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
992 xpt_action(done_ccb);
996 * Eh?? The command failed, but we don't
997 * have any sense. What's up with that?
998 * Fire the CCB again to return it to the
1001 bcopy(done_ccb->ccb_h.saved_ccb_ptr,
1002 done_ccb, sizeof(union ccb));
1004 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1006 xpt_action(done_ccb);
1011 bcopy(done_ccb->ccb_h.saved_ccb_ptr, done_ccb,
1014 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1016 xpt_action(done_ccb);
1021 /* decrement the retry count */
1022 if (done_ccb->ccb_h.retry_count > 0)
1023 done_ccb->ccb_h.retry_count--;
1025 qfrozen_cnt = cam_release_devq(done_ccb->ccb_h.path,
1026 /*relsim_flags*/relsim_flags,
1029 /*getcount_only*/0);
1033 * Generic Async Event handler. Peripheral drivers usually
1034 * filter out the events that require personal attention,
1035 * and leave the rest to this function.
1038 cam_periph_async(struct cam_periph *periph, u_int32_t code,
1039 struct cam_path *path, void *arg)
1042 case AC_LOST_DEVICE:
1043 cam_periph_invalidate(periph);
1048 cam_periph_bus_settle(periph, SCSI_DELAY);
1057 cam_periph_bus_settle(struct cam_periph *periph, u_int bus_settle)
1059 struct ccb_getdevstats cgds;
1061 xpt_setup_ccb(&cgds.ccb_h, periph->path, /*priority*/1);
1062 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1063 xpt_action((union ccb *)&cgds);
1064 cam_periph_freeze_after_event(periph, &cgds.last_reset, bus_settle);
1068 cam_periph_freeze_after_event(struct cam_periph *periph,
1069 struct timeval* event_time, u_int duration_ms)
1071 struct timeval delta;
1072 struct timeval duration_tv;
1074 microuptime(&delta);
1075 timevalsub(&delta, event_time);
1076 duration_tv.tv_sec = duration_ms / 1000;
1077 duration_tv.tv_usec = (duration_ms % 1000) * 1000;
1078 if (timevalcmp(&delta, &duration_tv, <)) {
1079 timevalsub(&duration_tv, &delta);
1081 duration_ms = duration_tv.tv_sec * 1000;
1082 duration_ms += duration_tv.tv_usec / 1000;
1083 cam_freeze_devq(periph->path);
1084 cam_release_devq(periph->path,
1085 RELSIM_RELEASE_AFTER_TIMEOUT,
1087 /*timeout*/duration_ms,
1088 /*getcount_only*/0);
1094 * Generic error handler. Peripheral drivers usually filter
1095 * out the errors that they handle in a unique mannor, then
1096 * call this function.
1099 cam_periph_error(union ccb *ccb, cam_flags camflags,
1100 u_int32_t sense_flags, union ccb *save_ccb)
1108 u_int32_t relsim_flags;
1111 status = ccb->ccb_h.status;
1112 frozen = (status & CAM_DEV_QFRZN) != 0;
1113 sense = (status & CAM_AUTOSNS_VALID) != 0;
1114 status &= CAM_STATUS_MASK;
1119 /* decrement the number of retries */
1120 retry = ccb->ccb_h.retry_count > 0;
1122 ccb->ccb_h.retry_count--;
1125 case CAM_AUTOSENSE_FAIL:
1126 case CAM_SCSI_STATUS_ERROR:
1128 switch (ccb->csio.scsi_status) {
1129 case SCSI_STATUS_OK:
1130 case SCSI_STATUS_COND_MET:
1131 case SCSI_STATUS_INTERMED:
1132 case SCSI_STATUS_INTERMED_COND_MET:
1135 case SCSI_STATUS_CMD_TERMINATED:
1136 case SCSI_STATUS_CHECK_COND:
1138 struct scsi_sense_data *sense;
1139 int error_code, sense_key, asc, ascq;
1140 struct cam_periph *periph;
1141 scsi_sense_action err_action;
1142 struct ccb_getdev cgd;
1144 sense = &ccb->csio.sense_data;
1145 scsi_extract_sense(sense, &error_code,
1146 &sense_key, &asc, &ascq);
1147 periph = xpt_path_periph(ccb->ccb_h.path);
1150 * Grab the inquiry data for this device.
1152 xpt_setup_ccb(&cgd.ccb_h, ccb->ccb_h.path,
1154 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1155 xpt_action((union ccb *)&cgd);
1157 err_action = scsi_error_action(asc, ascq,
1161 * Send a Test Unit Ready to the device.
1162 * If the 'many' flag is set, we send 120
1163 * test unit ready commands, one every half
1164 * second. Otherwise, we just send one TUR.
1165 * We only want to do this if the retry
1166 * count has not been exhausted.
1168 if (((err_action & SS_MASK) == SS_TUR)
1170 && ccb->ccb_h.retry_count > 0) {
1173 * Since error recovery is already
1174 * in progress, don't attempt to
1175 * process this error. It is probably
1176 * related to the error that caused
1177 * the currently active error recovery
1178 * action. Also, we only have
1179 * space for one saved CCB, so if we
1180 * had two concurrent error recovery
1181 * actions, we would end up
1182 * over-writing one error recovery
1183 * CCB with another one.
1186 CAM_PERIPH_RECOVERY_INPROG) {
1192 CAM_PERIPH_RECOVERY_INPROG;
1194 /* decrement the number of retries */
1196 SSQ_DECREMENT_COUNT) != 0) {
1198 ccb->ccb_h.retry_count--;
1201 bcopy(ccb, save_ccb, sizeof(*save_ccb));
1204 * We retry this one every half
1205 * second for a minute. If the
1206 * device hasn't become ready in a
1207 * minute's time, it's unlikely to
1208 * ever become ready. If the table
1209 * doesn't specify SSQ_MANY, we can
1210 * only try this once. Oh well.
1212 if ((err_action & SSQ_MANY) != 0)
1213 scsi_test_unit_ready(&ccb->csio,
1220 scsi_test_unit_ready(&ccb->csio,
1227 /* release the queue after .5 sec. */
1229 RELSIM_RELEASE_AFTER_TIMEOUT;
1232 * Drop the priority to 0 so that
1233 * we are the first to execute. Also
1234 * freeze the queue after this command
1235 * is sent so that we can restore the
1236 * old csio and have it queued in the
1237 * proper order before we let normal
1238 * transactions go to the drive.
1240 ccb->ccb_h.pinfo.priority = 0;
1241 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1244 * Save a pointer to the original
1245 * CCB in the new CCB.
1247 ccb->ccb_h.saved_ccb_ptr = save_ccb;
1252 * Send a start unit command to the device,
1253 * and then retry the command. We only
1254 * want to do this if the retry count has
1255 * not been exhausted. If the user
1256 * specified 0 retries, then we follow
1257 * their request and do not retry.
1259 else if (((err_action & SS_MASK) == SS_START)
1261 && ccb->ccb_h.retry_count > 0) {
1265 * Only one error recovery action
1266 * at a time. See above.
1269 CAM_PERIPH_RECOVERY_INPROG) {
1275 CAM_PERIPH_RECOVERY_INPROG;
1277 /* decrement the number of retries */
1279 ccb->ccb_h.retry_count--;
1282 * Check for removable media and
1283 * set load/eject flag
1286 if (SID_IS_REMOVABLE(&cgd.inq_data))
1292 * Attempt to start the drive up.
1294 * Save the current ccb so it can
1295 * be restored and retried once the
1296 * drive is started up.
1298 bcopy(ccb, save_ccb, sizeof(*save_ccb));
1300 scsi_start_stop(&ccb->csio,
1310 * Drop the priority to 0 so that
1311 * we are the first to execute. Also
1312 * freeze the queue after this command
1313 * is sent so that we can restore the
1314 * old csio and have it queued in the
1315 * proper order before we let normal
1316 * transactions go to the drive.
1318 ccb->ccb_h.pinfo.priority = 0;
1319 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1322 * Save a pointer to the original
1323 * CCB in the new CCB.
1325 ccb->ccb_h.saved_ccb_ptr = save_ccb;
1328 } else if ((sense_flags & SF_RETRY_UA) != 0) {
1330 * XXX KDM this is a *horrible*
1333 error = scsi_interpret_sense(ccb,
1342 * Theoretically, this code should send a
1343 * test unit ready to the given device, and
1344 * if it returns and error, send a start
1345 * unit command. Since we don't yet have
1346 * the capability to do two-command error
1347 * recovery, just send a start unit.
1350 else if (((err_action & SS_MASK) == SS_TURSTART)
1352 && ccb->ccb_h.retry_count > 0) {
1356 * Only one error recovery action
1357 * at a time. See above.
1360 CAM_PERIPH_RECOVERY_INPROG) {
1366 CAM_PERIPH_RECOVERY_INPROG;
1368 /* decrement the number of retries */
1370 ccb->ccb_h.retry_count--;
1373 * Check for removable media and
1374 * set load/eject flag
1377 if (SID_IS_REMOVABLE(&cgd.inq_data))
1383 * Attempt to start the drive up.
1385 * Save the current ccb so it can
1386 * be restored and retried once the
1387 * drive is started up.
1389 bcopy(ccb, save_ccb, sizeof(*save_ccb));
1391 scsi_start_stop(&ccb->csio,
1401 /* release the queue after .5 sec. */
1403 RELSIM_RELEASE_AFTER_TIMEOUT;
1406 * Drop the priority to 0 so that
1407 * we are the first to execute. Also
1408 * freeze the queue after this command
1409 * is sent so that we can restore the
1410 * old csio and have it queued in the
1411 * proper order before we let normal
1412 * transactions go to the drive.
1414 ccb->ccb_h.pinfo.priority = 0;
1415 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1418 * Save a pointer to the original
1419 * CCB in the new CCB.
1421 ccb->ccb_h.saved_ccb_ptr = save_ccb;
1425 error = scsi_interpret_sense(ccb,
1432 } else if (ccb->csio.scsi_status ==
1433 SCSI_STATUS_CHECK_COND
1434 && status != CAM_AUTOSENSE_FAIL) {
1435 /* no point in decrementing the retry count */
1436 panic("cam_periph_error: scsi status of "
1437 "CHECK COND returned but no sense "
1438 "information is available. "
1439 "Controller should have returned "
1440 "CAM_AUTOSENSE_FAILED");
1443 } else if (ccb->ccb_h.retry_count == 0) {
1445 * XXX KDM shouldn't there be a better
1446 * argument to return??
1450 /* decrement the number of retries */
1451 retry = ccb->ccb_h.retry_count > 0;
1453 ccb->ccb_h.retry_count--;
1455 * If it was aborted with no
1456 * clue as to the reason, just
1462 case SCSI_STATUS_QUEUE_FULL:
1465 struct ccb_getdevstats cgds;
1468 * First off, find out what the current
1469 * transaction counts are.
1471 xpt_setup_ccb(&cgds.ccb_h,
1474 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1475 xpt_action((union ccb *)&cgds);
1478 * If we were the only transaction active, treat
1479 * the QUEUE FULL as if it were a BUSY condition.
1481 if (cgds.dev_active != 0) {
1485 * Reduce the number of openings to
1486 * be 1 less than the amount it took
1487 * to get a queue full bounded by the
1488 * minimum allowed tag count for this
1492 cgds.dev_active+cgds.dev_openings;
1493 openings = cgds.dev_active;
1494 if (openings < cgds.mintags)
1495 openings = cgds.mintags;
1496 if (openings < total_openings)
1497 relsim_flags = RELSIM_ADJUST_OPENINGS;
1500 * Some devices report queue full for
1501 * temporary resource shortages. For
1502 * this reason, we allow a minimum
1503 * tag count to be entered via a
1504 * quirk entry to prevent the queue
1505 * count on these devices from falling
1506 * to a pessimisticly low value. We
1507 * still wait for the next successful
1508 * completion, however, before queueing
1509 * more transactions to the device.
1512 RELSIM_RELEASE_AFTER_CMDCMPLT;
1520 case SCSI_STATUS_BUSY:
1522 * Restart the queue after either another
1523 * command completes or a 1 second timeout.
1524 * If we have any retries left, that is.
1526 retry = ccb->ccb_h.retry_count > 0;
1528 ccb->ccb_h.retry_count--;
1530 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT
1531 | RELSIM_RELEASE_AFTER_CMDCMPLT;
1537 case SCSI_STATUS_RESERV_CONFLICT:
1545 case CAM_REQ_CMP_ERR:
1546 case CAM_CMD_TIMEOUT:
1547 case CAM_UNEXP_BUSFREE:
1548 case CAM_UNCOR_PARITY:
1549 case CAM_DATA_RUN_ERR:
1550 /* decrement the number of retries */
1551 retry = ccb->ccb_h.retry_count > 0;
1553 ccb->ccb_h.retry_count--;
1561 case CAM_MSG_REJECT_REC:
1562 /* XXX Don't know that these are correct */
1565 case CAM_SEL_TIMEOUT:
1569 * A single selection timeout should not be enough
1570 * to invalidate a device. We should retry for multiple
1571 * seconds assuming this isn't a probe. We'll probably
1572 * need a special flag for that.
1575 struct cam_path *newpath;
1577 /* Should we do more if we can't create the path?? */
1578 if (xpt_create_path(&newpath, xpt_path_periph(ccb->ccb_h.path),
1579 xpt_path_path_id(ccb->ccb_h.path),
1580 xpt_path_target_id(ccb->ccb_h.path),
1581 CAM_LUN_WILDCARD) != CAM_REQ_CMP)
1584 * Let peripheral drivers know that this device has gone
1587 xpt_async(AC_LOST_DEVICE, newpath, NULL);
1588 xpt_free_path(newpath);
1590 if ((sense_flags & SF_RETRY_SELTO) != 0) {
1591 retry = ccb->ccb_h.retry_count > 0;
1593 ccb->ccb_h.retry_count--;
1596 * Wait half a second to give the device
1597 * time to recover before we try again.
1599 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1609 case CAM_REQ_INVALID:
1610 case CAM_PATH_INVALID:
1611 case CAM_DEV_NOT_THERE:
1613 case CAM_PROVIDE_FAIL:
1614 case CAM_REQ_TOO_BIG:
1617 case CAM_SCSI_BUS_RESET:
1619 case CAM_REQUEUE_REQ:
1620 /* Unconditional requeue, dammit */
1623 case CAM_RESRC_UNAVAIL:
1627 /* decrement the number of retries */
1628 retry = ccb->ccb_h.retry_count > 0;
1630 ccb->ccb_h.retry_count--;
1633 /* Check the sense codes */
1639 /* Attempt a retry */
1640 if (error == ERESTART || error == 0) {
1642 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1644 if (error == ERESTART)
1648 cam_release_devq(ccb->ccb_h.path,
1652 /*getcount_only*/0);