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.12 2006/04/28 00:24:44 dillon Exp $
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/types.h>
36 #include <sys/malloc.h>
37 #include <sys/linker_set.h>
40 #include <sys/devicestat.h>
43 #include <vm/vm_extern.h>
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_da.h>
56 #include <bus/cam/scsi/scsi_pass.h>
58 static u_int camperiphnextunit(struct periph_driver *p_drv,
59 u_int newunit, int wired,
60 path_id_t pathid, target_id_t target,
62 static u_int camperiphunit(struct periph_driver *p_drv,
63 path_id_t pathid, target_id_t target,
65 static void camperiphdone(struct cam_periph *periph,
67 static void camperiphfree(struct cam_periph *periph);
70 cam_periph_alloc(periph_ctor_t *periph_ctor,
71 periph_oninv_t *periph_oninvalidate,
72 periph_dtor_t *periph_dtor, periph_start_t *periph_start,
73 char *name, cam_periph_type type, struct cam_path *path,
74 ac_callback_t *ac_callback, ac_code code, void *arg)
76 struct periph_driver **p_drv;
77 struct cam_periph *periph;
78 struct cam_periph *cur_periph;
80 target_id_t target_id;
87 * Handle Hot-Plug scenarios. If there is already a peripheral
88 * of our type assigned to this path, we are likely waiting for
89 * final close on an old, invalidated, peripheral. If this is
90 * the case, queue up a deferred call to the peripheral's async
91 * handler. If it looks like a mistaken re-alloation, complain.
93 if ((periph = cam_periph_find(path, name)) != NULL) {
95 if ((periph->flags & CAM_PERIPH_INVALID) != 0
96 && (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) == 0) {
97 periph->flags |= CAM_PERIPH_NEW_DEV_FOUND;
98 periph->deferred_callback = ac_callback;
99 periph->deferred_ac = code;
100 return (CAM_REQ_INPROG);
102 printf("cam_periph_alloc: attempt to re-allocate "
103 "valid device %s%d rejected\n",
104 periph->periph_name, periph->unit_number);
106 return (CAM_REQ_INVALID);
109 periph = malloc(sizeof(*periph), M_DEVBUF, M_INTWAIT | M_ZERO);
113 SET_FOREACH(p_drv, periphdriver_set) {
114 if (strcmp((*p_drv)->driver_name, name) == 0)
118 path_id = xpt_path_path_id(path);
119 target_id = xpt_path_target_id(path);
120 lun_id = xpt_path_lun_id(path);
121 cam_init_pinfo(&periph->pinfo);
122 periph->periph_start = periph_start;
123 periph->periph_dtor = periph_dtor;
124 periph->periph_oninval = periph_oninvalidate;
126 periph->periph_name = name;
127 periph->unit_number = camperiphunit(*p_drv, path_id, target_id, lun_id);
128 periph->immediate_priority = CAM_PRIORITY_NONE;
129 periph->refcount = 0;
130 SLIST_INIT(&periph->ccb_list);
131 status = xpt_create_path(&path, periph, path_id, target_id, lun_id);
132 if (status != CAM_REQ_CMP)
138 status = xpt_add_periph(periph);
140 if (status != CAM_REQ_CMP)
144 cur_periph = TAILQ_FIRST(&(*p_drv)->units);
145 while (cur_periph != NULL
146 && cur_periph->unit_number < periph->unit_number)
147 cur_periph = TAILQ_NEXT(cur_periph, unit_links);
149 if (cur_periph != NULL)
150 TAILQ_INSERT_BEFORE(cur_periph, periph, unit_links);
152 TAILQ_INSERT_TAIL(&(*p_drv)->units, periph, unit_links);
153 (*p_drv)->generation++;
160 status = periph_ctor(periph, arg);
162 if (status == CAM_REQ_CMP)
166 switch (init_level) {
168 /* Initialized successfully */
172 TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
174 xpt_remove_periph(periph);
176 xpt_free_path(periph->path);
178 free(periph, M_DEVBUF);
180 /* No cleanup to perform. */
183 panic("cam_periph_alloc: Unkown init level");
189 * Find a peripheral structure with the specified path, target, lun,
190 * and (optionally) type. If the name is NULL, this function will return
191 * the first peripheral driver that matches the specified path.
194 cam_periph_find(struct cam_path *path, char *name)
196 struct periph_driver **p_drv;
197 struct cam_periph *periph;
199 SET_FOREACH(p_drv, periphdriver_set) {
200 if (name != NULL && (strcmp((*p_drv)->driver_name, name) != 0))
204 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
205 periph = TAILQ_NEXT(periph, unit_links)) {
206 if (xpt_path_comp(periph->path, path) == 0) {
219 cam_periph_acquire(struct cam_periph *periph)
222 return(CAM_REQ_CMP_ERR);
232 cam_periph_release(struct cam_periph *periph)
238 if ((--periph->refcount == 0)
239 && (periph->flags & CAM_PERIPH_INVALID)) {
240 camperiphfree(periph);
246 * Look for the next unit number that is not currently in use for this
247 * peripheral type starting at "newunit". Also exclude unit numbers that
248 * are reserved by for future "hardwiring" unless we already know that this
249 * is a potential wired device. Only assume that the device is "wired" the
250 * first time through the loop since after that we'll be looking at unit
251 * numbers that did not match a wiring entry.
254 camperiphnextunit(struct periph_driver *p_drv, u_int newunit, int wired,
255 path_id_t pathid, target_id_t target, lun_id_t lun)
257 struct cam_periph *periph;
258 char *periph_name, *strval;
263 periph_name = p_drv->driver_name;
266 for (periph = TAILQ_FIRST(&p_drv->units);
267 periph != NULL && periph->unit_number != newunit;
268 periph = TAILQ_NEXT(periph, unit_links))
271 if (periph != NULL && periph->unit_number == newunit) {
273 xpt_print_path(periph->path);
274 printf("Duplicate Wired Device entry!\n");
275 xpt_print_path(periph->path);
276 printf("Second device (%s device at scbus%d "
277 "target %d lun %d) will not be wired\n",
278 periph_name, pathid, target, lun);
287 * Don't match entries like "da 4" as a wired down
288 * device, but do match entries like "da 4 target 5"
289 * or even "da 4 scbus 1".
292 while ((i = resource_locate(i, periph_name)) != -1) {
293 dname = resource_query_name(i);
294 dunit = resource_query_unit(i);
295 /* if no "target" and no specific scbus, skip */
296 if (resource_int_value(dname, dunit, "target", &val) &&
297 (resource_string_value(dname, dunit, "at",&strval)||
298 strcmp(strval, "scbus") == 0))
300 if (newunit == dunit)
311 camperiphunit(struct periph_driver *p_drv, path_id_t pathid,
312 target_id_t target, lun_id_t lun)
315 int hit, i, val, dunit;
317 char pathbuf[32], *strval, *periph_name;
321 periph_name = p_drv->driver_name;
322 snprintf(pathbuf, sizeof(pathbuf), "scbus%d", pathid);
324 for (hit = 0; (i = resource_locate(i, periph_name)) != -1; hit = 0) {
325 dname = resource_query_name(i);
326 dunit = resource_query_unit(i);
327 if (resource_string_value(dname, dunit, "at", &strval) == 0) {
328 if (strcmp(strval, pathbuf) != 0)
332 if (resource_int_value(dname, dunit, "target", &val) == 0) {
337 if (resource_int_value(dname, dunit, "lun", &val) == 0) {
349 * Either start from 0 looking for the next unit or from
350 * the unit number given in the resource config. This way,
351 * if we have wildcard matches, we don't return the same
354 unit = camperiphnextunit(p_drv, unit, /*wired*/hit, pathid,
361 cam_periph_invalidate(struct cam_periph *periph)
364 * We only call this routine the first time a peripheral is
365 * invalidated. The oninvalidate() routine is always called in
366 * a critical section.
369 if (((periph->flags & CAM_PERIPH_INVALID) == 0)
370 && (periph->periph_oninval != NULL))
371 periph->periph_oninval(periph);
373 periph->flags |= CAM_PERIPH_INVALID;
374 periph->flags &= ~CAM_PERIPH_NEW_DEV_FOUND;
376 if (periph->refcount == 0)
377 camperiphfree(periph);
378 else if (periph->refcount < 0)
379 printf("cam_invalidate_periph: refcount < 0!!\n");
384 camperiphfree(struct cam_periph *periph)
386 struct periph_driver **p_drv;
388 SET_FOREACH(p_drv, periphdriver_set) {
389 if (strcmp((*p_drv)->driver_name, periph->periph_name) == 0)
393 if (*p_drv == NULL) {
394 printf("camperiphfree: attempt to free "
395 "non-existant periph: %s\n", periph->periph_name);
399 if (periph->periph_dtor != NULL)
400 periph->periph_dtor(periph);
403 TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
404 (*p_drv)->generation++;
407 xpt_remove_periph(periph);
409 if (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) {
413 switch (periph->deferred_ac) {
414 case AC_FOUND_DEVICE:
415 ccb.ccb_h.func_code = XPT_GDEV_TYPE;
416 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/ 1);
420 case AC_PATH_REGISTERED:
421 ccb.ccb_h.func_code = XPT_PATH_INQ;
422 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/ 1);
430 periph->deferred_callback(NULL, periph->deferred_ac,
433 xpt_free_path(periph->path);
434 free(periph, M_DEVBUF);
438 * Wait interruptibly for an exclusive lock.
441 cam_periph_lock(struct cam_periph *periph, int flags)
445 while ((periph->flags & CAM_PERIPH_LOCKED) != 0) {
446 periph->flags |= CAM_PERIPH_LOCK_WANTED;
447 if ((error = tsleep(periph, flags, "caplck", 0)) != 0)
451 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
454 periph->flags |= CAM_PERIPH_LOCKED;
459 * Unlock and wake up any waiters.
462 cam_periph_unlock(struct cam_periph *periph)
464 periph->flags &= ~CAM_PERIPH_LOCKED;
465 if ((periph->flags & CAM_PERIPH_LOCK_WANTED) != 0) {
466 periph->flags &= ~CAM_PERIPH_LOCK_WANTED;
470 cam_periph_release(periph);
474 * Map user virtual pointers into kernel virtual address space, so we can
475 * access the memory. This won't work on physical pointers, for now it's
476 * up to the caller to check for that. (XXX KDM -- should we do that here
477 * instead?) This also only works for up to MAXPHYS memory. Since we use
478 * buffers to map stuff in and out, we're limited to the buffer size.
481 cam_periph_mapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
484 int flags[CAM_PERIPH_MAXMAPS];
485 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
486 u_int32_t lengths[CAM_PERIPH_MAXMAPS];
487 u_int32_t dirs[CAM_PERIPH_MAXMAPS];
489 switch(ccb->ccb_h.func_code) {
491 if (ccb->cdm.match_buf_len == 0) {
492 printf("cam_periph_mapmem: invalid match buffer "
496 if (ccb->cdm.pattern_buf_len > 0) {
497 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
498 lengths[0] = ccb->cdm.pattern_buf_len;
499 dirs[0] = CAM_DIR_OUT;
500 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
501 lengths[1] = ccb->cdm.match_buf_len;
502 dirs[1] = CAM_DIR_IN;
505 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
506 lengths[0] = ccb->cdm.match_buf_len;
507 dirs[0] = CAM_DIR_IN;
512 case XPT_CONT_TARGET_IO:
513 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
516 data_ptrs[0] = &ccb->csio.data_ptr;
517 lengths[0] = ccb->csio.dxfer_len;
518 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
523 break; /* NOTREACHED */
527 * Check the transfer length and permissions first, so we don't
528 * have to unmap any previously mapped buffers.
530 for (i = 0; i < numbufs; i++) {
535 * The userland data pointer passed in may not be page
536 * aligned. vmapbuf() truncates the address to a page
537 * boundary, so if the address isn't page aligned, we'll
538 * need enough space for the given transfer length, plus
539 * whatever extra space is necessary to make it to the page
543 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)) > DFLTPHYS){
544 printf("cam_periph_mapmem: attempt to map %lu bytes, "
545 "which is greater than DFLTPHYS(%d)\n",
547 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)),
552 if (dirs[i] & CAM_DIR_OUT) {
554 if (!useracc(*data_ptrs[i], lengths[i],
556 printf("cam_periph_mapmem: error, "
557 "address %p, length %lu isn't "
558 "user accessible for READ\n",
559 (void *)*data_ptrs[i],
566 * XXX this check is really bogus, since B_WRITE currently
567 * is all 0's, and so it is "set" all the time.
569 if (dirs[i] & CAM_DIR_IN) {
571 if (!useracc(*data_ptrs[i], lengths[i],
573 printf("cam_periph_mapmem: error, "
574 "address %p, length %lu isn't "
575 "user accessible for WRITE\n",
576 (void *)*data_ptrs[i],
585 for (i = 0; i < numbufs; i++) {
589 mapinfo->bp[i] = getpbuf(NULL);
591 /* save the buffer's data address */
592 mapinfo->bp[i]->b_saveaddr = mapinfo->bp[i]->b_data;
594 /* put our pointer in the data slot */
595 mapinfo->bp[i]->b_data = *data_ptrs[i];
597 /* set the transfer length, we know it's < DFLTPHYS */
598 mapinfo->bp[i]->b_bufsize = lengths[i];
601 mapinfo->bp[i]->b_flags = flags[i];
603 /* map the buffer into kernel memory */
604 if (vmapbuf(mapinfo->bp[i]) < 0) {
605 printf("cam_periph_mapmem: error, "
606 "address %p, length %lu isn't "
607 "user accessible any more\n",
608 (void *)*data_ptrs[i],
610 for (j = 0; j < i; ++j) {
611 *data_ptrs[j] = mapinfo->bp[j]->b_saveaddr;
612 relpbuf(mapinfo->bp[j], NULL);
617 /* set our pointer to the new mapped area */
618 *data_ptrs[i] = mapinfo->bp[i]->b_data;
620 mapinfo->num_bufs_used++;
627 * Unmap memory segments mapped into kernel virtual address space by
628 * cam_periph_mapmem().
631 cam_periph_unmapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
634 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
636 if (mapinfo->num_bufs_used <= 0) {
637 /* allow ourselves to be swapped once again */
641 switch (ccb->ccb_h.func_code) {
643 numbufs = min(mapinfo->num_bufs_used, 2);
646 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
648 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
649 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
653 case XPT_CONT_TARGET_IO:
654 data_ptrs[0] = &ccb->csio.data_ptr;
655 numbufs = min(mapinfo->num_bufs_used, 1);
658 /* allow ourselves to be swapped once again */
660 break; /* NOTREACHED */
663 for (i = 0; i < numbufs; i++) {
664 /* Set the user's pointer back to the original value */
665 *data_ptrs[i] = mapinfo->bp[i]->b_saveaddr;
667 /* unmap the buffer */
668 vunmapbuf(mapinfo->bp[i]);
670 /* release the buffer */
671 relpbuf(mapinfo->bp[i], NULL);
674 /* allow ourselves to be swapped once again */
678 cam_periph_getccb(struct cam_periph *periph, u_int32_t priority)
680 struct ccb_hdr *ccb_h;
682 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdgetccb\n"));
686 while (periph->ccb_list.slh_first == NULL) {
687 if (periph->immediate_priority > priority)
688 periph->immediate_priority = priority;
689 xpt_schedule(periph, priority);
690 if ((periph->ccb_list.slh_first != NULL)
691 && (periph->ccb_list.slh_first->pinfo.priority == priority))
693 tsleep(&periph->ccb_list, 0, "cgticb", 0);
696 ccb_h = periph->ccb_list.slh_first;
697 SLIST_REMOVE_HEAD(&periph->ccb_list, periph_links.sle);
699 return ((union ccb *)ccb_h);
703 cam_periph_ccbwait(union ccb *ccb)
706 if ((ccb->ccb_h.pinfo.index != CAM_UNQUEUED_INDEX)
707 || ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG))
708 tsleep(&ccb->ccb_h.cbfcnp, 0, "cbwait", 0);
713 cam_periph_ioctl(struct cam_periph *periph, int cmd, caddr_t addr,
714 int (*error_routine)(union ccb *ccb,
716 u_int32_t sense_flags))
726 ccb = cam_periph_getccb(periph, /* priority */ 1);
727 xpt_setup_ccb(&ccb->ccb_h,
730 ccb->ccb_h.func_code = XPT_GDEVLIST;
733 * Basically, the point of this is that we go through
734 * getting the list of devices, until we find a passthrough
735 * device. In the current version of the CAM code, the
736 * only way to determine what type of device we're dealing
737 * with is by its name.
741 ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS;
742 while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) {
744 /* we want the next device in the list */
746 if (strncmp(ccb->cgdl.periph_name,
752 if ((ccb->cgdl.status == CAM_GDEVLIST_LAST_DEVICE) &&
754 ccb->cgdl.periph_name[0] = '\0';
755 ccb->cgdl.unit_number = 0;
760 /* copy the result back out */
761 bcopy(ccb, addr, sizeof(union ccb));
763 /* and release the ccb */
764 xpt_release_ccb(ccb);
775 cam_periph_runccb(union ccb *ccb,
776 int (*error_routine)(union ccb *ccb,
778 u_int32_t sense_flags),
779 cam_flags camflags, u_int32_t sense_flags,
787 * If the user has supplied a stats structure, and if we understand
788 * this particular type of ccb, record the transaction start.
790 if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO))
791 devstat_start_transaction(ds);
796 cam_periph_ccbwait(ccb);
797 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
799 else if (error_routine != NULL)
800 error = (*error_routine)(ccb, camflags, sense_flags);
804 } while (error == ERESTART);
806 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
807 cam_release_devq(ccb->ccb_h.path,
811 /* getcount_only */ FALSE);
813 if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO))
814 devstat_end_transaction(ds,
816 ccb->csio.tag_action & 0xf,
817 ((ccb->ccb_h.flags & CAM_DIR_MASK) ==
818 CAM_DIR_NONE) ? DEVSTAT_NO_DATA :
819 (ccb->ccb_h.flags & CAM_DIR_OUT) ?
827 cam_freeze_devq(struct cam_path *path)
829 struct ccb_hdr ccb_h;
831 xpt_setup_ccb(&ccb_h, path, /*priority*/1);
832 ccb_h.func_code = XPT_NOOP;
833 ccb_h.flags = CAM_DEV_QFREEZE;
834 xpt_action((union ccb *)&ccb_h);
838 cam_release_devq(struct cam_path *path, u_int32_t relsim_flags,
839 u_int32_t openings, u_int32_t timeout,
842 struct ccb_relsim crs;
844 xpt_setup_ccb(&crs.ccb_h, path,
846 crs.ccb_h.func_code = XPT_REL_SIMQ;
847 crs.ccb_h.flags = getcount_only ? CAM_DEV_QFREEZE : 0;
848 crs.release_flags = relsim_flags;
849 crs.openings = openings;
850 crs.release_timeout = timeout;
851 xpt_action((union ccb *)&crs);
852 return (crs.qfrozen_cnt);
855 #define saved_ccb_ptr ppriv_ptr0
857 camperiphdone(struct cam_periph *periph, union ccb *done_ccb)
862 struct scsi_start_stop_unit *scsi_cmd;
863 u_int32_t relsim_flags, timeout;
864 u_int32_t qfrozen_cnt;
866 status = done_ccb->ccb_h.status;
867 frozen = (status & CAM_DEV_QFRZN) != 0;
868 sense = (status & CAM_AUTOSNS_VALID) != 0;
869 status &= CAM_STATUS_MASK;
875 * Unfreeze the queue once if it is already frozen..
878 qfrozen_cnt = cam_release_devq(done_ccb->ccb_h.path,
890 * If we have successfully taken a device from the not
891 * ready to ready state, re-scan the device and re-get the
892 * inquiry information. Many devices (mostly disks) don't
893 * properly report their inquiry information unless they
896 if (done_ccb->ccb_h.func_code == XPT_SCSI_IO) {
897 scsi_cmd = (struct scsi_start_stop_unit *)
898 &done_ccb->csio.cdb_io.cdb_bytes;
900 if (scsi_cmd->opcode == START_STOP_UNIT)
901 xpt_async(AC_INQ_CHANGED,
902 done_ccb->ccb_h.path, NULL);
904 bcopy(done_ccb->ccb_h.saved_ccb_ptr, done_ccb,
907 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
909 xpt_action(done_ccb);
912 case CAM_SCSI_STATUS_ERROR:
913 scsi_cmd = (struct scsi_start_stop_unit *)
914 &done_ccb->csio.cdb_io.cdb_bytes;
916 struct scsi_sense_data *sense;
917 int error_code, sense_key, asc, ascq;
919 sense = &done_ccb->csio.sense_data;
920 scsi_extract_sense(sense, &error_code,
921 &sense_key, &asc, &ascq);
924 * If the error is "invalid field in CDB",
925 * and the load/eject flag is set, turn the
926 * flag off and try again. This is just in
927 * case the drive in question barfs on the
928 * load eject flag. The CAM code should set
929 * the load/eject flag by default for
934 * Should we check to see what the specific
935 * scsi status is?? Or does it not matter
936 * since we already know that there was an
937 * error, and we know what the specific
938 * error code was, and we know what the
941 if ((scsi_cmd->opcode == START_STOP_UNIT) &&
942 ((scsi_cmd->how & SSS_LOEJ) != 0) &&
943 (asc == 0x24) && (ascq == 0x00) &&
944 (done_ccb->ccb_h.retry_count > 0)) {
946 scsi_cmd->how &= ~SSS_LOEJ;
948 xpt_action(done_ccb);
950 } else if (done_ccb->ccb_h.retry_count > 0) {
952 * In this case, the error recovery
953 * command failed, but we've got
954 * some retries left on it. Give
958 /* set the timeout to .5 sec */
960 RELSIM_RELEASE_AFTER_TIMEOUT;
963 xpt_action(done_ccb);
969 * Copy the original CCB back and
970 * send it back to the caller.
972 bcopy(done_ccb->ccb_h.saved_ccb_ptr,
973 done_ccb, sizeof(union ccb));
975 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
977 xpt_action(done_ccb);
981 * Eh?? The command failed, but we don't
982 * have any sense. What's up with that?
983 * Fire the CCB again to return it to the
986 bcopy(done_ccb->ccb_h.saved_ccb_ptr,
987 done_ccb, sizeof(union ccb));
989 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
991 xpt_action(done_ccb);
996 bcopy(done_ccb->ccb_h.saved_ccb_ptr, done_ccb,
999 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1001 xpt_action(done_ccb);
1006 /* decrement the retry count */
1007 if (done_ccb->ccb_h.retry_count > 0)
1008 done_ccb->ccb_h.retry_count--;
1010 qfrozen_cnt = cam_release_devq(done_ccb->ccb_h.path,
1011 /*relsim_flags*/relsim_flags,
1014 /*getcount_only*/0);
1018 * Generic Async Event handler. Peripheral drivers usually
1019 * filter out the events that require personal attention,
1020 * and leave the rest to this function.
1023 cam_periph_async(struct cam_periph *periph, u_int32_t code,
1024 struct cam_path *path, void *arg)
1027 case AC_LOST_DEVICE:
1028 cam_periph_invalidate(periph);
1033 cam_periph_bus_settle(periph, SCSI_DELAY);
1042 cam_periph_bus_settle(struct cam_periph *periph, u_int bus_settle)
1044 struct ccb_getdevstats cgds;
1046 xpt_setup_ccb(&cgds.ccb_h, periph->path, /*priority*/1);
1047 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1048 xpt_action((union ccb *)&cgds);
1049 cam_periph_freeze_after_event(periph, &cgds.last_reset, bus_settle);
1053 cam_periph_freeze_after_event(struct cam_periph *periph,
1054 struct timeval* event_time, u_int duration_ms)
1056 struct timeval delta;
1057 struct timeval duration_tv;
1059 microuptime(&delta);
1060 timevalsub(&delta, event_time);
1061 duration_tv.tv_sec = duration_ms / 1000;
1062 duration_tv.tv_usec = (duration_ms % 1000) * 1000;
1063 if (timevalcmp(&delta, &duration_tv, <)) {
1064 timevalsub(&duration_tv, &delta);
1066 duration_ms = duration_tv.tv_sec * 1000;
1067 duration_ms += duration_tv.tv_usec / 1000;
1068 cam_freeze_devq(periph->path);
1069 cam_release_devq(periph->path,
1070 RELSIM_RELEASE_AFTER_TIMEOUT,
1072 /*timeout*/duration_ms,
1073 /*getcount_only*/0);
1079 * Generic error handler. Peripheral drivers usually filter
1080 * out the errors that they handle in a unique mannor, then
1081 * call this function.
1084 cam_periph_error(union ccb *ccb, cam_flags camflags,
1085 u_int32_t sense_flags, union ccb *save_ccb)
1093 u_int32_t relsim_flags;
1096 status = ccb->ccb_h.status;
1097 frozen = (status & CAM_DEV_QFRZN) != 0;
1098 sense = (status & CAM_AUTOSNS_VALID) != 0;
1099 status &= CAM_STATUS_MASK;
1104 /* decrement the number of retries */
1105 retry = ccb->ccb_h.retry_count > 0;
1107 ccb->ccb_h.retry_count--;
1110 case CAM_AUTOSENSE_FAIL:
1111 case CAM_SCSI_STATUS_ERROR:
1113 switch (ccb->csio.scsi_status) {
1114 case SCSI_STATUS_OK:
1115 case SCSI_STATUS_COND_MET:
1116 case SCSI_STATUS_INTERMED:
1117 case SCSI_STATUS_INTERMED_COND_MET:
1120 case SCSI_STATUS_CMD_TERMINATED:
1121 case SCSI_STATUS_CHECK_COND:
1123 struct scsi_sense_data *sense;
1124 int error_code, sense_key, asc, ascq;
1125 struct cam_periph *periph;
1126 scsi_sense_action err_action;
1127 struct ccb_getdev cgd;
1129 sense = &ccb->csio.sense_data;
1130 scsi_extract_sense(sense, &error_code,
1131 &sense_key, &asc, &ascq);
1132 periph = xpt_path_periph(ccb->ccb_h.path);
1135 * Grab the inquiry data for this device.
1137 xpt_setup_ccb(&cgd.ccb_h, ccb->ccb_h.path,
1139 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1140 xpt_action((union ccb *)&cgd);
1142 err_action = scsi_error_action(asc, ascq,
1146 * Send a Test Unit Ready to the device.
1147 * If the 'many' flag is set, we send 120
1148 * test unit ready commands, one every half
1149 * second. Otherwise, we just send one TUR.
1150 * We only want to do this if the retry
1151 * count has not been exhausted.
1153 if (((err_action & SS_MASK) == SS_TUR)
1155 && ccb->ccb_h.retry_count > 0) {
1158 * Since error recovery is already
1159 * in progress, don't attempt to
1160 * process this error. It is probably
1161 * related to the error that caused
1162 * the currently active error recovery
1163 * action. Also, we only have
1164 * space for one saved CCB, so if we
1165 * had two concurrent error recovery
1166 * actions, we would end up
1167 * over-writing one error recovery
1168 * CCB with another one.
1171 CAM_PERIPH_RECOVERY_INPROG) {
1177 CAM_PERIPH_RECOVERY_INPROG;
1179 /* decrement the number of retries */
1181 SSQ_DECREMENT_COUNT) != 0) {
1183 ccb->ccb_h.retry_count--;
1186 bcopy(ccb, save_ccb, sizeof(*save_ccb));
1189 * We retry this one every half
1190 * second for a minute. If the
1191 * device hasn't become ready in a
1192 * minute's time, it's unlikely to
1193 * ever become ready. If the table
1194 * doesn't specify SSQ_MANY, we can
1195 * only try this once. Oh well.
1197 if ((err_action & SSQ_MANY) != 0)
1198 scsi_test_unit_ready(&ccb->csio,
1205 scsi_test_unit_ready(&ccb->csio,
1212 /* release the queue after .5 sec. */
1214 RELSIM_RELEASE_AFTER_TIMEOUT;
1217 * Drop the priority to 0 so that
1218 * we are the first to execute. Also
1219 * freeze the queue after this command
1220 * is sent so that we can restore the
1221 * old csio and have it queued in the
1222 * proper order before we let normal
1223 * transactions go to the drive.
1225 ccb->ccb_h.pinfo.priority = 0;
1226 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1229 * Save a pointer to the original
1230 * CCB in the new CCB.
1232 ccb->ccb_h.saved_ccb_ptr = save_ccb;
1237 * Send a start unit command to the device,
1238 * and then retry the command. We only
1239 * want to do this if the retry count has
1240 * not been exhausted. If the user
1241 * specified 0 retries, then we follow
1242 * their request and do not retry.
1244 else if (((err_action & SS_MASK) == SS_START)
1246 && ccb->ccb_h.retry_count > 0) {
1250 * Only one error recovery action
1251 * at a time. See above.
1254 CAM_PERIPH_RECOVERY_INPROG) {
1260 CAM_PERIPH_RECOVERY_INPROG;
1262 /* decrement the number of retries */
1264 ccb->ccb_h.retry_count--;
1267 * Check for removable media and
1268 * set load/eject flag
1271 if (SID_IS_REMOVABLE(&cgd.inq_data))
1277 * Attempt to start the drive up.
1279 * Save the current ccb so it can
1280 * be restored and retried once the
1281 * drive is started up.
1283 bcopy(ccb, save_ccb, sizeof(*save_ccb));
1285 scsi_start_stop(&ccb->csio,
1295 * Drop the priority to 0 so that
1296 * we are the first to execute. Also
1297 * freeze the queue after this command
1298 * is sent so that we can restore the
1299 * old csio and have it queued in the
1300 * proper order before we let normal
1301 * transactions go to the drive.
1303 ccb->ccb_h.pinfo.priority = 0;
1304 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1307 * Save a pointer to the original
1308 * CCB in the new CCB.
1310 ccb->ccb_h.saved_ccb_ptr = save_ccb;
1313 } else if ((sense_flags & SF_RETRY_UA) != 0) {
1315 * XXX KDM this is a *horrible*
1318 error = scsi_interpret_sense(ccb,
1327 * Theoretically, this code should send a
1328 * test unit ready to the given device, and
1329 * if it returns and error, send a start
1330 * unit command. Since we don't yet have
1331 * the capability to do two-command error
1332 * recovery, just send a start unit.
1335 else if (((err_action & SS_MASK) == SS_TURSTART)
1337 && ccb->ccb_h.retry_count > 0) {
1341 * Only one error recovery action
1342 * at a time. See above.
1345 CAM_PERIPH_RECOVERY_INPROG) {
1351 CAM_PERIPH_RECOVERY_INPROG;
1353 /* decrement the number of retries */
1355 ccb->ccb_h.retry_count--;
1358 * Check for removable media and
1359 * set load/eject flag
1362 if (SID_IS_REMOVABLE(&cgd.inq_data))
1368 * Attempt to start the drive up.
1370 * Save the current ccb so it can
1371 * be restored and retried once the
1372 * drive is started up.
1374 bcopy(ccb, save_ccb, sizeof(*save_ccb));
1376 scsi_start_stop(&ccb->csio,
1386 /* release the queue after .5 sec. */
1388 RELSIM_RELEASE_AFTER_TIMEOUT;
1391 * Drop the priority to 0 so that
1392 * we are the first to execute. Also
1393 * freeze the queue after this command
1394 * is sent so that we can restore the
1395 * old csio and have it queued in the
1396 * proper order before we let normal
1397 * transactions go to the drive.
1399 ccb->ccb_h.pinfo.priority = 0;
1400 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1403 * Save a pointer to the original
1404 * CCB in the new CCB.
1406 ccb->ccb_h.saved_ccb_ptr = save_ccb;
1410 error = scsi_interpret_sense(ccb,
1417 } else if (ccb->csio.scsi_status ==
1418 SCSI_STATUS_CHECK_COND
1419 && status != CAM_AUTOSENSE_FAIL) {
1420 /* no point in decrementing the retry count */
1421 panic("cam_periph_error: scsi status of "
1422 "CHECK COND returned but no sense "
1423 "information is availible. "
1424 "Controller should have returned "
1425 "CAM_AUTOSENSE_FAILED");
1428 } else if (ccb->ccb_h.retry_count == 0) {
1430 * XXX KDM shouldn't there be a better
1431 * argument to return??
1435 /* decrement the number of retries */
1436 retry = ccb->ccb_h.retry_count > 0;
1438 ccb->ccb_h.retry_count--;
1440 * If it was aborted with no
1441 * clue as to the reason, just
1447 case SCSI_STATUS_QUEUE_FULL:
1450 struct ccb_getdevstats cgds;
1453 * First off, find out what the current
1454 * transaction counts are.
1456 xpt_setup_ccb(&cgds.ccb_h,
1459 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1460 xpt_action((union ccb *)&cgds);
1463 * If we were the only transaction active, treat
1464 * the QUEUE FULL as if it were a BUSY condition.
1466 if (cgds.dev_active != 0) {
1470 * Reduce the number of openings to
1471 * be 1 less than the amount it took
1472 * to get a queue full bounded by the
1473 * minimum allowed tag count for this
1477 cgds.dev_active+cgds.dev_openings;
1478 openings = cgds.dev_active;
1479 if (openings < cgds.mintags)
1480 openings = cgds.mintags;
1481 if (openings < total_openings)
1482 relsim_flags = RELSIM_ADJUST_OPENINGS;
1485 * Some devices report queue full for
1486 * temporary resource shortages. For
1487 * this reason, we allow a minimum
1488 * tag count to be entered via a
1489 * quirk entry to prevent the queue
1490 * count on these devices from falling
1491 * to a pessimisticly low value. We
1492 * still wait for the next successful
1493 * completion, however, before queueing
1494 * more transactions to the device.
1497 RELSIM_RELEASE_AFTER_CMDCMPLT;
1505 case SCSI_STATUS_BUSY:
1507 * Restart the queue after either another
1508 * command completes or a 1 second timeout.
1509 * If we have any retries left, that is.
1511 retry = ccb->ccb_h.retry_count > 0;
1513 ccb->ccb_h.retry_count--;
1515 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT
1516 | RELSIM_RELEASE_AFTER_CMDCMPLT;
1522 case SCSI_STATUS_RESERV_CONFLICT:
1530 case CAM_REQ_CMP_ERR:
1531 case CAM_CMD_TIMEOUT:
1532 case CAM_UNEXP_BUSFREE:
1533 case CAM_UNCOR_PARITY:
1534 case CAM_DATA_RUN_ERR:
1535 /* decrement the number of retries */
1536 retry = ccb->ccb_h.retry_count > 0;
1538 ccb->ccb_h.retry_count--;
1546 case CAM_MSG_REJECT_REC:
1547 /* XXX Don't know that these are correct */
1550 case CAM_SEL_TIMEOUT:
1554 * A single selection timeout should not be enough
1555 * to invalidate a device. We should retry for multiple
1556 * seconds assuming this isn't a probe. We'll probably
1557 * need a special flag for that.
1560 struct cam_path *newpath;
1562 /* Should we do more if we can't create the path?? */
1563 if (xpt_create_path(&newpath, xpt_path_periph(ccb->ccb_h.path),
1564 xpt_path_path_id(ccb->ccb_h.path),
1565 xpt_path_target_id(ccb->ccb_h.path),
1566 CAM_LUN_WILDCARD) != CAM_REQ_CMP)
1569 * Let peripheral drivers know that this device has gone
1572 xpt_async(AC_LOST_DEVICE, newpath, NULL);
1573 xpt_free_path(newpath);
1575 if ((sense_flags & SF_RETRY_SELTO) != 0) {
1576 retry = ccb->ccb_h.retry_count > 0;
1578 ccb->ccb_h.retry_count--;
1581 * Wait half a second to give the device
1582 * time to recover before we try again.
1584 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1594 case CAM_REQ_INVALID:
1595 case CAM_PATH_INVALID:
1596 case CAM_DEV_NOT_THERE:
1598 case CAM_PROVIDE_FAIL:
1599 case CAM_REQ_TOO_BIG:
1602 case CAM_SCSI_BUS_RESET:
1604 case CAM_REQUEUE_REQ:
1605 /* Unconditional requeue, dammit */
1608 case CAM_RESRC_UNAVAIL:
1612 /* decrement the number of retries */
1613 retry = ccb->ccb_h.retry_count > 0;
1615 ccb->ccb_h.retry_count--;
1618 /* Check the sense codes */
1624 /* Attempt a retry */
1625 if (error == ERESTART || error == 0) {
1627 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1629 if (error == ERESTART)
1633 cam_release_devq(ccb->ccb_h.path,
1637 /*getcount_only*/0);