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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/types.h>
36 #include <sys/malloc.h>
37 #include <sys/kernel.h>
38 #include <sys/linker_set.h>
41 #include <sys/mutex.h>
44 #include <sys/devicestat.h>
47 #include <vm/vm_extern.h>
50 #include <cam/cam_ccb.h>
51 #include <cam/cam_xpt_periph.h>
52 #include <cam/cam_periph.h>
53 #include <cam/cam_debug.h>
55 #include <cam/scsi/scsi_all.h>
56 #include <cam/scsi/scsi_message.h>
57 #include <cam/scsi/scsi_pass.h>
59 static u_int camperiphnextunit(struct periph_driver *p_drv,
60 u_int newunit, int wired,
61 path_id_t pathid, target_id_t target,
63 static u_int camperiphunit(struct periph_driver *p_drv,
64 path_id_t pathid, target_id_t target,
66 static void camperiphdone(struct cam_periph *periph,
68 static void camperiphfree(struct cam_periph *periph);
69 static int camperiphscsistatuserror(union ccb *ccb,
71 u_int32_t sense_flags,
74 u_int32_t *relsim_flags,
76 static int camperiphscsisenseerror(union ccb *ccb,
78 u_int32_t sense_flags,
81 u_int32_t *relsim_flags,
84 static int nperiph_drivers;
85 struct periph_driver **periph_drivers;
87 MALLOC_DEFINE(M_CAMPERIPH, "CAM periph", "CAM peripheral buffers");
90 periphdriver_register(void *data)
92 struct periph_driver **newdrivers, **old;
95 ndrivers = nperiph_drivers + 2;
96 newdrivers = malloc(sizeof(*newdrivers) * ndrivers, M_TEMP, M_WAITOK);
98 bcopy(periph_drivers, newdrivers,
99 sizeof(*newdrivers) * nperiph_drivers);
100 newdrivers[nperiph_drivers] = (struct periph_driver *)data;
101 newdrivers[nperiph_drivers + 1] = NULL;
102 old = periph_drivers;
103 periph_drivers = newdrivers;
110 cam_periph_alloc(periph_ctor_t *periph_ctor,
111 periph_oninv_t *periph_oninvalidate,
112 periph_dtor_t *periph_dtor, periph_start_t *periph_start,
113 char *name, cam_periph_type type, struct cam_path *path,
114 ac_callback_t *ac_callback, ac_code code, void *arg)
116 struct periph_driver **p_drv;
117 struct cam_periph *periph;
118 struct cam_periph *cur_periph;
120 target_id_t target_id;
128 * Handle Hot-Plug scenarios. If there is already a peripheral
129 * of our type assigned to this path, we are likely waiting for
130 * final close on an old, invalidated, peripheral. If this is
131 * the case, queue up a deferred call to the peripheral's async
132 * handler. If it looks like a mistaken re-allocation, complain.
134 if ((periph = cam_periph_find(path, name)) != NULL) {
136 if ((periph->flags & CAM_PERIPH_INVALID) != 0
137 && (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) == 0) {
138 periph->flags |= CAM_PERIPH_NEW_DEV_FOUND;
139 periph->deferred_callback = ac_callback;
140 periph->deferred_ac = code;
141 return (CAM_REQ_INPROG);
143 printf("cam_periph_alloc: attempt to re-allocate "
144 "valid device %s%d rejected\n",
145 periph->periph_name, periph->unit_number);
147 return (CAM_REQ_INVALID);
150 periph = (struct cam_periph *)malloc(sizeof(*periph), M_CAMPERIPH,
154 return (CAM_RESRC_UNAVAIL);
158 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
159 if (strcmp((*p_drv)->driver_name, name) == 0)
163 path_id = xpt_path_path_id(path);
164 target_id = xpt_path_target_id(path);
165 lun_id = xpt_path_lun_id(path);
166 bzero(periph, sizeof(*periph));
167 cam_init_pinfo(&periph->pinfo);
168 periph->periph_start = periph_start;
169 periph->periph_dtor = periph_dtor;
170 periph->periph_oninval = periph_oninvalidate;
172 periph->periph_name = name;
173 periph->unit_number = camperiphunit(*p_drv, path_id, target_id, lun_id);
174 periph->immediate_priority = CAM_PRIORITY_NONE;
175 periph->refcount = 0;
176 SLIST_INIT(&periph->ccb_list);
177 status = xpt_create_path(&path, periph, path_id, target_id, lun_id);
178 if (status != CAM_REQ_CMP)
184 status = xpt_add_periph(periph);
186 if (status != CAM_REQ_CMP)
190 cur_periph = TAILQ_FIRST(&(*p_drv)->units);
191 while (cur_periph != NULL
192 && cur_periph->unit_number < periph->unit_number)
193 cur_periph = TAILQ_NEXT(cur_periph, unit_links);
195 if (cur_periph != NULL)
196 TAILQ_INSERT_BEFORE(cur_periph, periph, unit_links);
198 TAILQ_INSERT_TAIL(&(*p_drv)->units, periph, unit_links);
199 (*p_drv)->generation++;
206 status = periph_ctor(periph, arg);
208 if (status == CAM_REQ_CMP)
212 switch (init_level) {
214 /* Initialized successfully */
218 TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
220 xpt_remove_periph(periph);
223 xpt_free_path(periph->path);
226 free(periph, M_CAMPERIPH);
229 /* No cleanup to perform. */
232 panic("cam_periph_alloc: Unkown init level");
238 * Find a peripheral structure with the specified path, target, lun,
239 * and (optionally) type. If the name is NULL, this function will return
240 * the first peripheral driver that matches the specified path.
243 cam_periph_find(struct cam_path *path, char *name)
245 struct periph_driver **p_drv;
246 struct cam_periph *periph;
249 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
251 if (name != NULL && (strcmp((*p_drv)->driver_name, name) != 0))
255 TAILQ_FOREACH(periph, &(*p_drv)->units, unit_links) {
256 if (xpt_path_comp(periph->path, path) == 0) {
269 cam_periph_acquire(struct cam_periph *periph)
274 return(CAM_REQ_CMP_ERR);
284 cam_periph_release(struct cam_periph *periph)
292 if ((--periph->refcount == 0)
293 && (periph->flags & CAM_PERIPH_INVALID)) {
294 camperiphfree(periph);
301 * Look for the next unit number that is not currently in use for this
302 * peripheral type starting at "newunit". Also exclude unit numbers that
303 * are reserved by for future "hardwiring" unless we already know that this
304 * is a potential wired device. Only assume that the device is "wired" the
305 * first time through the loop since after that we'll be looking at unit
306 * numbers that did not match a wiring entry.
309 camperiphnextunit(struct periph_driver *p_drv, u_int newunit, int wired,
310 path_id_t pathid, target_id_t target, lun_id_t lun)
312 struct cam_periph *periph;
315 int i, val, dunit, r;
316 const char *dname, *strval;
319 periph_name = p_drv->driver_name;
322 for (periph = TAILQ_FIRST(&p_drv->units);
323 periph != NULL && periph->unit_number != newunit;
324 periph = TAILQ_NEXT(periph, unit_links))
327 if (periph != NULL && periph->unit_number == newunit) {
329 xpt_print_path(periph->path);
330 printf("Duplicate Wired Device entry!\n");
331 xpt_print_path(periph->path);
332 printf("Second device (%s device at scbus%d "
333 "target %d lun %d) will not be wired\n",
334 periph_name, pathid, target, lun);
343 * Don't match entries like "da 4" as a wired down
344 * device, but do match entries like "da 4 target 5"
345 * or even "da 4 scbus 1".
350 r = resource_find_dev(&i, dname, &dunit, NULL, NULL);
353 /* if no "target" and no specific scbus, skip */
354 if (resource_int_value(dname, dunit, "target", &val) &&
355 (resource_string_value(dname, dunit, "at",&strval)||
356 strcmp(strval, "scbus") == 0))
358 if (newunit == dunit)
369 camperiphunit(struct periph_driver *p_drv, path_id_t pathid,
370 target_id_t target, lun_id_t lun)
373 int wired, i, val, dunit;
374 const char *dname, *strval;
375 char pathbuf[32], *periph_name;
377 periph_name = p_drv->driver_name;
378 snprintf(pathbuf, sizeof(pathbuf), "scbus%d", pathid);
382 for (wired = 0; resource_find_dev(&i, dname, &dunit, NULL, NULL) == 0;
384 if (resource_string_value(dname, dunit, "at", &strval) == 0) {
385 if (strcmp(strval, pathbuf) != 0)
389 if (resource_int_value(dname, dunit, "target", &val) == 0) {
394 if (resource_int_value(dname, dunit, "lun", &val) == 0) {
406 * Either start from 0 looking for the next unit or from
407 * the unit number given in the resource config. This way,
408 * if we have wildcard matches, we don't return the same
411 unit = camperiphnextunit(p_drv, unit, wired, pathid, target, lun);
417 cam_periph_invalidate(struct cam_periph *periph)
423 * We only call this routine the first time a peripheral is
424 * invalidated. The oninvalidate() routine is always called at
427 if (((periph->flags & CAM_PERIPH_INVALID) == 0)
428 && (periph->periph_oninval != NULL))
429 periph->periph_oninval(periph);
431 periph->flags |= CAM_PERIPH_INVALID;
432 periph->flags &= ~CAM_PERIPH_NEW_DEV_FOUND;
434 if (periph->refcount == 0)
435 camperiphfree(periph);
436 else if (periph->refcount < 0)
437 printf("cam_invalidate_periph: refcount < 0!!\n");
442 camperiphfree(struct cam_periph *periph)
445 struct periph_driver **p_drv;
447 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
448 if (strcmp((*p_drv)->driver_name, periph->periph_name) == 0)
451 if (*p_drv == NULL) {
452 printf("camperiphfree: attempt to free non-existant periph\n");
456 if (periph->periph_dtor != NULL)
457 periph->periph_dtor(periph);
460 TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
461 (*p_drv)->generation++;
464 xpt_remove_periph(periph);
466 if (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) {
470 switch (periph->deferred_ac) {
471 case AC_FOUND_DEVICE:
472 ccb.ccb_h.func_code = XPT_GDEV_TYPE;
473 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/ 1);
477 case AC_PATH_REGISTERED:
478 ccb.ccb_h.func_code = XPT_PATH_INQ;
479 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/ 1);
487 periph->deferred_callback(NULL, periph->deferred_ac,
490 xpt_free_path(periph->path);
491 free(periph, M_CAMPERIPH);
495 * Wait interruptibly for an exclusive lock.
498 cam_periph_lock(struct cam_periph *periph, int priority)
503 * Increment the reference count on the peripheral
504 * while we wait for our lock attempt to succeed
505 * to ensure the peripheral doesn't disappear out
506 * from under us while we sleep.
508 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
511 while ((periph->flags & CAM_PERIPH_LOCKED) != 0) {
512 periph->flags |= CAM_PERIPH_LOCK_WANTED;
513 if ((error = tsleep(periph, priority, "caplck", 0)) != 0) {
514 cam_periph_release(periph);
519 periph->flags |= CAM_PERIPH_LOCKED;
524 * Unlock and wake up any waiters.
527 cam_periph_unlock(struct cam_periph *periph)
529 periph->flags &= ~CAM_PERIPH_LOCKED;
530 if ((periph->flags & CAM_PERIPH_LOCK_WANTED) != 0) {
531 periph->flags &= ~CAM_PERIPH_LOCK_WANTED;
535 cam_periph_release(periph);
539 * Map user virtual pointers into kernel virtual address space, so we can
540 * access the memory. This won't work on physical pointers, for now it's
541 * up to the caller to check for that. (XXX KDM -- should we do that here
542 * instead?) This also only works for up to MAXPHYS memory. Since we use
543 * buffers to map stuff in and out, we're limited to the buffer size.
546 cam_periph_mapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
549 int flags[CAM_PERIPH_MAXMAPS];
550 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
551 u_int32_t lengths[CAM_PERIPH_MAXMAPS];
552 u_int32_t dirs[CAM_PERIPH_MAXMAPS];
554 switch(ccb->ccb_h.func_code) {
556 if (ccb->cdm.match_buf_len == 0) {
557 printf("cam_periph_mapmem: invalid match buffer "
561 if (ccb->cdm.pattern_buf_len > 0) {
562 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
563 lengths[0] = ccb->cdm.pattern_buf_len;
564 dirs[0] = CAM_DIR_OUT;
565 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
566 lengths[1] = ccb->cdm.match_buf_len;
567 dirs[1] = CAM_DIR_IN;
570 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
571 lengths[0] = ccb->cdm.match_buf_len;
572 dirs[0] = CAM_DIR_IN;
577 case XPT_CONT_TARGET_IO:
578 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
581 data_ptrs[0] = &ccb->csio.data_ptr;
582 lengths[0] = ccb->csio.dxfer_len;
583 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
588 break; /* NOTREACHED */
592 * Check the transfer length and permissions first, so we don't
593 * have to unmap any previously mapped buffers.
595 for (i = 0; i < numbufs; i++) {
600 * The userland data pointer passed in may not be page
601 * aligned. vmapbuf() truncates the address to a page
602 * boundary, so if the address isn't page aligned, we'll
603 * need enough space for the given transfer length, plus
604 * whatever extra space is necessary to make it to the page
608 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)) > DFLTPHYS){
609 printf("cam_periph_mapmem: attempt to map %lu bytes, "
610 "which is greater than DFLTPHYS(%d)\n",
612 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)),
617 if (dirs[i] & CAM_DIR_OUT) {
618 flags[i] = BIO_WRITE;
621 if (dirs[i] & CAM_DIR_IN) {
627 /* this keeps the current process from getting swapped */
629 * XXX KDM should I use P_NOSWAP instead?
633 for (i = 0; i < numbufs; i++) {
637 mapinfo->bp[i] = getpbuf(NULL);
639 /* save the buffer's data address */
640 mapinfo->bp[i]->b_saveaddr = mapinfo->bp[i]->b_data;
642 /* put our pointer in the data slot */
643 mapinfo->bp[i]->b_data = *data_ptrs[i];
645 /* set the transfer length, we know it's < DFLTPHYS */
646 mapinfo->bp[i]->b_bufsize = lengths[i];
648 /* set the direction */
649 mapinfo->bp[i]->b_iocmd = flags[i];
652 * Map the buffer into kernel memory.
654 * Note that useracc() alone is not a sufficient test.
655 * vmapbuf() can still fail due to a smaller file mapped
656 * into a larger area of VM, or if userland races against
657 * vmapbuf() after the useracc() check.
659 if (vmapbuf(mapinfo->bp[i]) < 0) {
660 for (j = 0; j < i; ++j) {
661 *data_ptrs[j] = mapinfo->bp[j]->b_saveaddr;
662 vunmapbuf(mapinfo->bp[j]);
663 relpbuf(mapinfo->bp[j], NULL);
665 relpbuf(mapinfo->bp[i], NULL);
670 /* set our pointer to the new mapped area */
671 *data_ptrs[i] = mapinfo->bp[i]->b_data;
673 mapinfo->num_bufs_used++;
677 * Now that we've gotten this far, change ownership to the kernel
678 * of the buffers so that we don't run afoul of returning to user
679 * space with locks (on the buffer) held.
681 for (i = 0; i < numbufs; i++) {
682 BUF_KERNPROC(mapinfo->bp[i]);
690 * Unmap memory segments mapped into kernel virtual address space by
691 * cam_periph_mapmem().
694 cam_periph_unmapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
697 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
699 if (mapinfo->num_bufs_used <= 0) {
700 /* allow ourselves to be swapped once again */
705 switch (ccb->ccb_h.func_code) {
707 numbufs = min(mapinfo->num_bufs_used, 2);
710 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
712 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
713 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
717 case XPT_CONT_TARGET_IO:
718 data_ptrs[0] = &ccb->csio.data_ptr;
719 numbufs = min(mapinfo->num_bufs_used, 1);
722 /* allow ourselves to be swapped once again */
725 break; /* NOTREACHED */
728 for (i = 0; i < numbufs; i++) {
729 /* Set the user's pointer back to the original value */
730 *data_ptrs[i] = mapinfo->bp[i]->b_saveaddr;
732 /* unmap the buffer */
733 vunmapbuf(mapinfo->bp[i]);
735 /* release the buffer */
736 relpbuf(mapinfo->bp[i], NULL);
739 /* allow ourselves to be swapped once again */
744 cam_periph_getccb(struct cam_periph *periph, u_int32_t priority)
746 struct ccb_hdr *ccb_h;
749 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdgetccb\n"));
753 while (SLIST_FIRST(&periph->ccb_list) == NULL) {
754 if (periph->immediate_priority > priority)
755 periph->immediate_priority = priority;
756 xpt_schedule(periph, priority);
757 if ((SLIST_FIRST(&periph->ccb_list) != NULL)
758 && (SLIST_FIRST(&periph->ccb_list)->pinfo.priority == priority))
760 tsleep(&periph->ccb_list, PRIBIO, "cgticb", 0);
763 ccb_h = SLIST_FIRST(&periph->ccb_list);
764 SLIST_REMOVE_HEAD(&periph->ccb_list, periph_links.sle);
766 return ((union ccb *)ccb_h);
770 cam_periph_ccbwait(union ccb *ccb)
775 if ((ccb->ccb_h.pinfo.index != CAM_UNQUEUED_INDEX)
776 || ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG))
777 tsleep(&ccb->ccb_h.cbfcnp, PRIBIO, "cbwait", 0);
783 cam_periph_ioctl(struct cam_periph *periph, int cmd, caddr_t addr,
784 int (*error_routine)(union ccb *ccb,
786 u_int32_t sense_flags))
796 ccb = cam_periph_getccb(periph, /* priority */ 1);
797 xpt_setup_ccb(&ccb->ccb_h,
800 ccb->ccb_h.func_code = XPT_GDEVLIST;
803 * Basically, the point of this is that we go through
804 * getting the list of devices, until we find a passthrough
805 * device. In the current version of the CAM code, the
806 * only way to determine what type of device we're dealing
807 * with is by its name.
811 ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS;
812 while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) {
814 /* we want the next device in the list */
816 if (strncmp(ccb->cgdl.periph_name,
822 if ((ccb->cgdl.status == CAM_GDEVLIST_LAST_DEVICE) &&
824 ccb->cgdl.periph_name[0] = '\0';
825 ccb->cgdl.unit_number = 0;
830 /* copy the result back out */
831 bcopy(ccb, addr, sizeof(union ccb));
833 /* and release the ccb */
834 xpt_release_ccb(ccb);
845 cam_periph_runccb(union ccb *ccb,
846 int (*error_routine)(union ccb *ccb,
848 u_int32_t sense_flags),
849 cam_flags camflags, u_int32_t sense_flags,
857 * If the user has supplied a stats structure, and if we understand
858 * this particular type of ccb, record the transaction start.
860 if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO))
861 devstat_start_transaction(ds, NULL);
866 cam_periph_ccbwait(ccb);
867 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
869 else if (error_routine != NULL)
870 error = (*error_routine)(ccb, camflags, sense_flags);
874 } while (error == ERESTART);
876 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
877 cam_release_devq(ccb->ccb_h.path,
881 /* getcount_only */ FALSE);
883 if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO))
884 devstat_end_transaction(ds,
886 ccb->csio.tag_action & 0xf,
887 ((ccb->ccb_h.flags & CAM_DIR_MASK) ==
888 CAM_DIR_NONE) ? DEVSTAT_NO_DATA :
889 (ccb->ccb_h.flags & CAM_DIR_OUT) ?
891 DEVSTAT_READ, NULL, NULL);
897 cam_freeze_devq(struct cam_path *path)
899 struct ccb_hdr ccb_h;
901 xpt_setup_ccb(&ccb_h, path, /*priority*/1);
902 ccb_h.func_code = XPT_NOOP;
903 ccb_h.flags = CAM_DEV_QFREEZE;
904 xpt_action((union ccb *)&ccb_h);
908 cam_release_devq(struct cam_path *path, u_int32_t relsim_flags,
909 u_int32_t openings, u_int32_t timeout,
912 struct ccb_relsim crs;
914 xpt_setup_ccb(&crs.ccb_h, path,
916 crs.ccb_h.func_code = XPT_REL_SIMQ;
917 crs.ccb_h.flags = getcount_only ? CAM_DEV_QFREEZE : 0;
918 crs.release_flags = relsim_flags;
919 crs.openings = openings;
920 crs.release_timeout = timeout;
921 xpt_action((union ccb *)&crs);
922 return (crs.qfrozen_cnt);
925 #define saved_ccb_ptr ppriv_ptr0
927 camperiphdone(struct cam_periph *periph, union ccb *done_ccb)
929 union ccb *saved_ccb;
933 struct scsi_start_stop_unit *scsi_cmd;
934 u_int32_t relsim_flags, timeout;
935 u_int32_t qfrozen_cnt;
938 xpt_done_ccb = FALSE;
939 status = done_ccb->ccb_h.status;
940 frozen = (status & CAM_DEV_QFRZN) != 0;
941 sense = (status & CAM_AUTOSNS_VALID) != 0;
942 status &= CAM_STATUS_MASK;
946 saved_ccb = (union ccb *)done_ccb->ccb_h.saved_ccb_ptr;
949 * Unfreeze the queue once if it is already frozen..
952 qfrozen_cnt = cam_release_devq(done_ccb->ccb_h.path,
963 * If we have successfully taken a device from the not
964 * ready to ready state, re-scan the device and re-get
965 * the inquiry information. Many devices (mostly disks)
966 * don't properly report their inquiry information unless
969 * If we manually retrieved sense into a CCB and got
970 * something other than "NO SENSE" send the updated CCB
971 * back to the client via xpt_done() to be processed via
972 * the error recovery code again.
974 if (done_ccb->ccb_h.func_code == XPT_SCSI_IO) {
975 scsi_cmd = (struct scsi_start_stop_unit *)
976 &done_ccb->csio.cdb_io.cdb_bytes;
978 if (scsi_cmd->opcode == START_STOP_UNIT)
979 xpt_async(AC_INQ_CHANGED,
980 done_ccb->ccb_h.path, NULL);
981 if (scsi_cmd->opcode == REQUEST_SENSE) {
984 sense_key = saved_ccb->csio.sense_data.flags;
985 sense_key &= SSD_KEY;
986 if (sense_key != SSD_KEY_NO_SENSE) {
987 saved_ccb->ccb_h.status |=
990 xpt_print_path(saved_ccb->ccb_h.path);
991 printf("Recovered Sense\n");
992 scsi_sense_print(&saved_ccb->csio);
993 cam_error_print(saved_ccb, CAM_ESF_ALL,
1000 bcopy(done_ccb->ccb_h.saved_ccb_ptr, done_ccb,
1003 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1005 if (xpt_done_ccb == FALSE)
1006 xpt_action(done_ccb);
1010 case CAM_SCSI_STATUS_ERROR:
1011 scsi_cmd = (struct scsi_start_stop_unit *)
1012 &done_ccb->csio.cdb_io.cdb_bytes;
1014 struct ccb_getdev cgd;
1015 struct scsi_sense_data *sense;
1016 int error_code, sense_key, asc, ascq;
1017 scsi_sense_action err_action;
1019 sense = &done_ccb->csio.sense_data;
1020 scsi_extract_sense(sense, &error_code,
1021 &sense_key, &asc, &ascq);
1024 * Grab the inquiry data for this device.
1026 xpt_setup_ccb(&cgd.ccb_h, done_ccb->ccb_h.path,
1028 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1029 xpt_action((union ccb *)&cgd);
1030 err_action = scsi_error_action(&done_ccb->csio,
1034 * If the error is "invalid field in CDB",
1035 * and the load/eject flag is set, turn the
1036 * flag off and try again. This is just in
1037 * case the drive in question barfs on the
1038 * load eject flag. The CAM code should set
1039 * the load/eject flag by default for
1044 * Should we check to see what the specific
1045 * scsi status is?? Or does it not matter
1046 * since we already know that there was an
1047 * error, and we know what the specific
1048 * error code was, and we know what the
1051 if ((scsi_cmd->opcode == START_STOP_UNIT) &&
1052 ((scsi_cmd->how & SSS_LOEJ) != 0) &&
1053 (asc == 0x24) && (ascq == 0x00) &&
1054 (done_ccb->ccb_h.retry_count > 0)) {
1056 scsi_cmd->how &= ~SSS_LOEJ;
1058 xpt_action(done_ccb);
1060 } else if ((done_ccb->ccb_h.retry_count > 1)
1061 && ((err_action & SS_MASK) != SS_FAIL)) {
1064 * In this case, the error recovery
1065 * command failed, but we've got
1066 * some retries left on it. Give
1067 * it another try unless this is an
1068 * unretryable error.
1071 /* set the timeout to .5 sec */
1073 RELSIM_RELEASE_AFTER_TIMEOUT;
1076 xpt_action(done_ccb);
1082 * Perform the final retry with the original
1083 * CCB so that final error processing is
1084 * performed by the owner of the CCB.
1086 bcopy(done_ccb->ccb_h.saved_ccb_ptr,
1087 done_ccb, sizeof(union ccb));
1089 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1091 xpt_action(done_ccb);
1095 * Eh?? The command failed, but we don't
1096 * have any sense. What's up with that?
1097 * Fire the CCB again to return it to the
1100 bcopy(done_ccb->ccb_h.saved_ccb_ptr,
1101 done_ccb, sizeof(union ccb));
1103 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1105 xpt_action(done_ccb);
1110 bcopy(done_ccb->ccb_h.saved_ccb_ptr, done_ccb,
1113 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1115 xpt_action(done_ccb);
1120 /* decrement the retry count */
1122 * XXX This isn't appropriate in all cases. Restructure,
1123 * so that the retry count is only decremented on an
1124 * actual retry. Remeber that the orignal ccb had its
1125 * retry count dropped before entering recovery, so
1126 * doing it again is a bug.
1128 if (done_ccb->ccb_h.retry_count > 0)
1129 done_ccb->ccb_h.retry_count--;
1131 qfrozen_cnt = cam_release_devq(done_ccb->ccb_h.path,
1132 /*relsim_flags*/relsim_flags,
1135 /*getcount_only*/0);
1136 if (xpt_done_ccb == TRUE)
1137 (*done_ccb->ccb_h.cbfcnp)(periph, done_ccb);
1141 * Generic Async Event handler. Peripheral drivers usually
1142 * filter out the events that require personal attention,
1143 * and leave the rest to this function.
1146 cam_periph_async(struct cam_periph *periph, u_int32_t code,
1147 struct cam_path *path, void *arg)
1150 case AC_LOST_DEVICE:
1151 cam_periph_invalidate(periph);
1156 cam_periph_bus_settle(periph, scsi_delay);
1165 cam_periph_bus_settle(struct cam_periph *periph, u_int bus_settle)
1167 struct ccb_getdevstats cgds;
1169 xpt_setup_ccb(&cgds.ccb_h, periph->path, /*priority*/1);
1170 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1171 xpt_action((union ccb *)&cgds);
1172 cam_periph_freeze_after_event(periph, &cgds.last_reset, bus_settle);
1176 cam_periph_freeze_after_event(struct cam_periph *periph,
1177 struct timeval* event_time, u_int duration_ms)
1179 struct timeval delta;
1180 struct timeval duration_tv;
1186 timevalsub(&delta, event_time);
1187 duration_tv.tv_sec = duration_ms / 1000;
1188 duration_tv.tv_usec = (duration_ms % 1000) * 1000;
1189 if (timevalcmp(&delta, &duration_tv, <)) {
1190 timevalsub(&duration_tv, &delta);
1192 duration_ms = duration_tv.tv_sec * 1000;
1193 duration_ms += duration_tv.tv_usec / 1000;
1194 cam_freeze_devq(periph->path);
1195 cam_release_devq(periph->path,
1196 RELSIM_RELEASE_AFTER_TIMEOUT,
1198 /*timeout*/duration_ms,
1199 /*getcount_only*/0);
1205 camperiphscsistatuserror(union ccb *ccb, cam_flags camflags,
1206 u_int32_t sense_flags, union ccb *save_ccb,
1207 int *openings, u_int32_t *relsim_flags,
1212 switch (ccb->csio.scsi_status) {
1213 case SCSI_STATUS_OK:
1214 case SCSI_STATUS_COND_MET:
1215 case SCSI_STATUS_INTERMED:
1216 case SCSI_STATUS_INTERMED_COND_MET:
1219 case SCSI_STATUS_CMD_TERMINATED:
1220 case SCSI_STATUS_CHECK_COND:
1221 error = camperiphscsisenseerror(ccb,
1229 case SCSI_STATUS_QUEUE_FULL:
1232 struct ccb_getdevstats cgds;
1235 * First off, find out what the current
1236 * transaction counts are.
1238 xpt_setup_ccb(&cgds.ccb_h,
1241 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1242 xpt_action((union ccb *)&cgds);
1245 * If we were the only transaction active, treat
1246 * the QUEUE FULL as if it were a BUSY condition.
1248 if (cgds.dev_active != 0) {
1252 * Reduce the number of openings to
1253 * be 1 less than the amount it took
1254 * to get a queue full bounded by the
1255 * minimum allowed tag count for this
1258 total_openings = cgds.dev_active + cgds.dev_openings;
1259 *openings = cgds.dev_active;
1260 if (*openings < cgds.mintags)
1261 *openings = cgds.mintags;
1262 if (*openings < total_openings)
1263 *relsim_flags = RELSIM_ADJUST_OPENINGS;
1266 * Some devices report queue full for
1267 * temporary resource shortages. For
1268 * this reason, we allow a minimum
1269 * tag count to be entered via a
1270 * quirk entry to prevent the queue
1271 * count on these devices from falling
1272 * to a pessimisticly low value. We
1273 * still wait for the next successful
1274 * completion, however, before queueing
1275 * more transactions to the device.
1277 *relsim_flags = RELSIM_RELEASE_AFTER_CMDCMPLT;
1282 xpt_print_path(ccb->ccb_h.path);
1283 printf("Queue Full\n");
1289 case SCSI_STATUS_BUSY:
1291 * Restart the queue after either another
1292 * command completes or a 1 second timeout.
1295 xpt_print_path(ccb->ccb_h.path);
1296 printf("Device Busy\n");
1298 if (ccb->ccb_h.retry_count > 0) {
1299 ccb->ccb_h.retry_count--;
1301 *relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT
1302 | RELSIM_RELEASE_AFTER_CMDCMPLT;
1308 case SCSI_STATUS_RESERV_CONFLICT:
1309 xpt_print_path(ccb->ccb_h.path);
1310 printf("Reservation Conflict\n");
1314 xpt_print_path(ccb->ccb_h.path);
1315 printf("SCSI Status 0x%x\n", ccb->csio.scsi_status);
1323 camperiphscsisenseerror(union ccb *ccb, cam_flags camflags,
1324 u_int32_t sense_flags, union ccb *save_ccb,
1325 int *openings, u_int32_t *relsim_flags,
1328 struct cam_periph *periph;
1331 periph = xpt_path_periph(ccb->ccb_h.path);
1332 if (periph->flags & CAM_PERIPH_RECOVERY_INPROG) {
1335 * If error recovery is already in progress, don't attempt
1336 * to process this error, but requeue it unconditionally
1337 * and attempt to process it once error recovery has
1338 * completed. This failed command is probably related to
1339 * the error that caused the currently active error recovery
1340 * action so our current recovery efforts should also
1341 * address this command. Be aware that the error recovery
1342 * code assumes that only one recovery action is in progress
1343 * on a particular peripheral instance at any given time
1344 * (e.g. only one saved CCB for error recovery) so it is
1345 * imperitive that we don't violate this assumption.
1349 scsi_sense_action err_action;
1350 struct ccb_getdev cgd;
1351 const char *action_string;
1352 union ccb* print_ccb;
1354 /* A description of the error recovery action performed */
1355 action_string = NULL;
1358 * The location of the orignal ccb
1359 * for sense printing purposes.
1364 * Grab the inquiry data for this device.
1366 xpt_setup_ccb(&cgd.ccb_h, ccb->ccb_h.path, /*priority*/ 1);
1367 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1368 xpt_action((union ccb *)&cgd);
1370 if ((ccb->ccb_h.status & CAM_AUTOSNS_VALID) != 0)
1371 err_action = scsi_error_action(&ccb->csio,
1374 else if ((ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)
1375 err_action = SS_REQSENSE;
1377 err_action = SS_RETRY|SSQ_DECREMENT_COUNT|EIO;
1379 error = err_action & SS_ERRMASK;
1382 * If the recovery action will consume a retry,
1383 * make sure we actually have retries available.
1385 if ((err_action & SSQ_DECREMENT_COUNT) != 0) {
1386 if (ccb->ccb_h.retry_count > 0)
1387 ccb->ccb_h.retry_count--;
1389 action_string = "Retries Exhausted";
1390 goto sense_error_done;
1394 if ((err_action & SS_MASK) >= SS_START) {
1396 * Do common portions of commands that
1397 * use recovery CCBs.
1399 if (save_ccb == NULL) {
1400 action_string = "No recovery CCB supplied";
1401 goto sense_error_done;
1403 bcopy(ccb, save_ccb, sizeof(*save_ccb));
1404 print_ccb = save_ccb;
1405 periph->flags |= CAM_PERIPH_RECOVERY_INPROG;
1408 switch (err_action & SS_MASK) {
1410 action_string = "No Recovery Action Needed";
1414 action_string = "Retrying Command (per Sense Data)";
1418 action_string = "Unretryable error";
1425 * Send a start unit command to the device, and
1426 * then retry the command.
1428 action_string = "Attempting to Start Unit";
1431 * Check for removable media and set
1432 * load/eject flag appropriately.
1434 if (SID_IS_REMOVABLE(&cgd.inq_data))
1439 scsi_start_stop(&ccb->csio,
1453 * Send a Test Unit Ready to the device.
1454 * If the 'many' flag is set, we send 120
1455 * test unit ready commands, one every half
1456 * second. Otherwise, we just send one TUR.
1457 * We only want to do this if the retry
1458 * count has not been exhausted.
1462 if ((err_action & SSQ_MANY) != 0) {
1463 action_string = "Polling device for readiness";
1466 action_string = "Testing device for readiness";
1469 scsi_test_unit_ready(&ccb->csio,
1477 * Accomplish our 500ms delay by deferring
1478 * the release of our device queue appropriately.
1480 *relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1487 * Send a Request Sense to the device. We
1488 * assume that we are in a contingent allegiance
1489 * condition so we do not tag this request.
1491 scsi_request_sense(&ccb->csio, /*retries*/1,
1493 &save_ccb->csio.sense_data,
1494 sizeof(save_ccb->csio.sense_data),
1495 CAM_TAG_ACTION_NONE,
1496 /*sense_len*/SSD_FULL_SIZE,
1501 panic("Unhandled error action %x", err_action);
1504 if ((err_action & SS_MASK) >= SS_START) {
1506 * Drop the priority to 0 so that the recovery
1507 * CCB is the first to execute. Freeze the queue
1508 * after this command is sent so that we can
1509 * restore the old csio and have it queued in
1510 * the proper order before we release normal
1511 * transactions to the device.
1513 ccb->ccb_h.pinfo.priority = 0;
1514 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1515 ccb->ccb_h.saved_ccb_ptr = save_ccb;
1520 if ((err_action & SSQ_PRINT_SENSE) != 0
1521 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID) != 0) {
1522 cam_error_print(print_ccb, CAM_ESF_ALL, CAM_EPF_ALL);
1523 xpt_print_path(ccb->ccb_h.path);
1525 scsi_sense_print(&print_ccb->csio);
1526 printf("%s\n", action_string);
1533 * Generic error handler. Peripheral drivers usually filter
1534 * out the errors that they handle in a unique mannor, then
1535 * call this function.
1538 cam_periph_error(union ccb *ccb, cam_flags camflags,
1539 u_int32_t sense_flags, union ccb *save_ccb)
1541 const char *action_string;
1544 int error, printed = 0;
1546 u_int32_t relsim_flags;
1549 action_string = NULL;
1550 status = ccb->ccb_h.status;
1551 frozen = (status & CAM_DEV_QFRZN) != 0;
1552 status &= CAM_STATUS_MASK;
1553 openings = relsim_flags = 0;
1559 case CAM_SCSI_STATUS_ERROR:
1560 error = camperiphscsistatuserror(ccb,
1568 case CAM_AUTOSENSE_FAIL:
1569 xpt_print_path(ccb->ccb_h.path);
1570 printf("AutoSense Failed\n");
1571 error = EIO; /* we have to kill the command */
1573 case CAM_REQ_CMP_ERR:
1574 if (bootverbose && printed == 0) {
1575 xpt_print_path(ccb->ccb_h.path);
1576 printf("Request completed with CAM_REQ_CMP_ERR\n");
1580 case CAM_CMD_TIMEOUT:
1581 if (bootverbose && printed == 0) {
1582 xpt_print_path(ccb->ccb_h.path);
1583 printf("Command timed out\n");
1587 case CAM_UNEXP_BUSFREE:
1588 if (bootverbose && printed == 0) {
1589 xpt_print_path(ccb->ccb_h.path);
1590 printf("Unexpected Bus Free\n");
1594 case CAM_UNCOR_PARITY:
1595 if (bootverbose && printed == 0) {
1596 xpt_print_path(ccb->ccb_h.path);
1597 printf("Uncorrected Parity Error\n");
1601 case CAM_DATA_RUN_ERR:
1602 if (bootverbose && printed == 0) {
1603 xpt_print_path(ccb->ccb_h.path);
1604 printf("Data Overrun\n");
1607 error = EIO; /* we have to kill the command */
1608 /* decrement the number of retries */
1609 if (ccb->ccb_h.retry_count > 0) {
1610 ccb->ccb_h.retry_count--;
1613 action_string = "Retries Exausted";
1619 case CAM_MSG_REJECT_REC:
1620 /* XXX Don't know that these are correct */
1623 case CAM_SEL_TIMEOUT:
1625 struct cam_path *newpath;
1627 if ((camflags & CAM_RETRY_SELTO) != 0) {
1628 if (ccb->ccb_h.retry_count > 0) {
1630 ccb->ccb_h.retry_count--;
1632 if (bootverbose && printed == 0) {
1633 xpt_print_path(ccb->ccb_h.path);
1634 printf("Selection Timeout\n");
1639 * Wait a second to give the device
1640 * time to recover before we try again.
1642 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1648 /* Should we do more if we can't create the path?? */
1649 if (xpt_create_path(&newpath, xpt_path_periph(ccb->ccb_h.path),
1650 xpt_path_path_id(ccb->ccb_h.path),
1651 xpt_path_target_id(ccb->ccb_h.path),
1652 CAM_LUN_WILDCARD) != CAM_REQ_CMP)
1656 * Let peripheral drivers know that this device has gone
1659 xpt_async(AC_LOST_DEVICE, newpath, NULL);
1660 xpt_free_path(newpath);
1663 case CAM_REQ_INVALID:
1664 case CAM_PATH_INVALID:
1665 case CAM_DEV_NOT_THERE:
1667 case CAM_PROVIDE_FAIL:
1668 case CAM_REQ_TOO_BIG:
1669 case CAM_LUN_INVALID:
1670 case CAM_TID_INVALID:
1673 case CAM_SCSI_BUS_RESET:
1676 * Commands that repeatedly timeout and cause these
1677 * kinds of error recovery actions, should return
1678 * CAM_CMD_TIMEOUT, which allows us to safely assume
1679 * that this command was an innocent bystander to
1680 * these events and should be unconditionally
1683 if (bootverbose && printed == 0) {
1684 xpt_print_path(ccb->ccb_h.path);
1685 if (status == CAM_BDR_SENT)
1686 printf("Bus Device Reset sent\n");
1688 printf("Bus Reset issued\n");
1692 case CAM_REQUEUE_REQ:
1693 /* Unconditional requeue */
1695 if (bootverbose && printed == 0) {
1696 xpt_print_path(ccb->ccb_h.path);
1697 printf("Request Requeued\n");
1701 case CAM_RESRC_UNAVAIL:
1705 /* decrement the number of retries */
1706 if (ccb->ccb_h.retry_count > 0) {
1707 ccb->ccb_h.retry_count--;
1709 if (bootverbose && printed == 0) {
1710 xpt_print_path(ccb->ccb_h.path);
1711 printf("CAM Status 0x%x\n", status);
1716 action_string = "Retries Exhausted";
1721 /* Attempt a retry */
1722 if (error == ERESTART || error == 0) {
1724 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1726 if (error == ERESTART) {
1727 action_string = "Retrying Command";
1732 cam_release_devq(ccb->ccb_h.path,
1736 /*getcount_only*/0);
1740 * If we have and error and are booting verbosely, whine
1741 * *unless* this was a non-retryable selection timeout.
1743 if (error != 0 && bootverbose &&
1744 !(status == CAM_SEL_TIMEOUT && (camflags & CAM_RETRY_SELTO) == 0)) {
1747 if (action_string == NULL)
1748 action_string = "Unretryable Error";
1749 if (error != ERESTART) {
1750 xpt_print_path(ccb->ccb_h.path);
1751 printf("error %d\n", error);
1753 xpt_print_path(ccb->ccb_h.path);
1754 printf("%s\n", action_string);