2 * Common functions for CAM "type" (peripheral) drivers.
4 * Copyright (c) 1997, 1998 Justin T. Gibbs.
5 * Copyright (c) 1997, 1998, 1999 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
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/types.h>
35 #include <sys/malloc.h>
36 #include <sys/linker_set.h>
39 #include <sys/devicestat.h>
42 #include <vm/vm_extern.h>
45 #include <cam/cam_ccb.h>
46 #include <cam/cam_xpt_periph.h>
47 #include <cam/cam_periph.h>
48 #include <cam/cam_debug.h>
50 #include <cam/scsi/scsi_all.h>
51 #include <cam/scsi/scsi_message.h>
52 #include <cam/scsi/scsi_da.h>
53 #include <cam/scsi/scsi_pass.h>
55 static u_int camperiphnextunit(struct periph_driver *p_drv,
56 u_int newunit, int wired);
57 static u_int camperiphunit(struct periph_driver *p_drv,
59 target_id_t target, lun_id_t lun);
60 static void camperiphdone(struct cam_periph *periph,
62 static void camperiphfree(struct cam_periph *periph);
65 cam_periph_alloc(periph_ctor_t *periph_ctor,
66 periph_oninv_t *periph_oninvalidate,
67 periph_dtor_t *periph_dtor, periph_start_t *periph_start,
68 char *name, cam_periph_type type, struct cam_path *path,
69 ac_callback_t *ac_callback, ac_code code, void *arg)
71 struct periph_driver **p_drv;
72 struct cam_periph *periph;
73 struct cam_periph *cur_periph;
75 target_id_t target_id;
83 * Handle Hot-Plug scenarios. If there is already a peripheral
84 * of our type assigned to this path, we are likely waiting for
85 * final close on an old, invalidated, peripheral. If this is
86 * the case, queue up a deferred call to the peripheral's async
87 * handler. If it looks like a mistaken re-alloation, complain.
89 if ((periph = cam_periph_find(path, name)) != NULL) {
91 if ((periph->flags & CAM_PERIPH_INVALID) != 0
92 && (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) == 0) {
93 periph->flags |= CAM_PERIPH_NEW_DEV_FOUND;
94 periph->deferred_callback = ac_callback;
95 periph->deferred_ac = code;
96 return (CAM_REQ_INPROG);
98 printf("cam_periph_alloc: attempt to re-allocate "
99 "valid device %s%d rejected\n",
100 periph->periph_name, periph->unit_number);
102 return (CAM_REQ_INVALID);
105 periph = (struct cam_periph *)malloc(sizeof(*periph), M_DEVBUF,
109 return (CAM_RESRC_UNAVAIL);
113 for (p_drv = (struct periph_driver **)periphdriver_set.ls_items;
114 *p_drv != NULL; p_drv++) {
115 if (strcmp((*p_drv)->driver_name, name) == 0)
119 path_id = xpt_path_path_id(path);
120 target_id = xpt_path_target_id(path);
121 lun_id = xpt_path_lun_id(path);
122 bzero(periph, sizeof(*periph));
123 cam_init_pinfo(&periph->pinfo);
124 periph->periph_start = periph_start;
125 periph->periph_dtor = periph_dtor;
126 periph->periph_oninval = periph_oninvalidate;
128 periph->periph_name = name;
129 periph->unit_number = camperiphunit(*p_drv, path_id, target_id, lun_id);
130 periph->immediate_priority = CAM_PRIORITY_NONE;
131 periph->refcount = 0;
132 SLIST_INIT(&periph->ccb_list);
133 status = xpt_create_path(&path, periph, path_id, target_id, lun_id);
134 if (status != CAM_REQ_CMP)
140 status = xpt_add_periph(periph);
142 if (status != CAM_REQ_CMP)
146 cur_periph = TAILQ_FIRST(&(*p_drv)->units);
147 while (cur_periph != NULL
148 && cur_periph->unit_number < periph->unit_number)
149 cur_periph = TAILQ_NEXT(cur_periph, unit_links);
151 if (cur_periph != NULL)
152 TAILQ_INSERT_BEFORE(cur_periph, periph, unit_links);
154 TAILQ_INSERT_TAIL(&(*p_drv)->units, periph, unit_links);
155 (*p_drv)->generation++;
162 status = periph_ctor(periph, arg);
164 if (status == CAM_REQ_CMP)
168 switch (init_level) {
170 /* Initialized successfully */
174 TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
176 xpt_remove_periph(periph);
178 xpt_free_path(periph->path);
180 free(periph, M_DEVBUF);
182 /* No cleanup to perform. */
185 panic("cam_periph_alloc: Unkown init level");
191 * Find a peripheral structure with the specified path, target, lun,
192 * and (optionally) type. If the name is NULL, this function will return
193 * the first peripheral driver that matches the specified path.
196 cam_periph_find(struct cam_path *path, char *name)
198 struct periph_driver **p_drv;
199 struct cam_periph *periph;
202 for (p_drv = (struct periph_driver **)periphdriver_set.ls_items;
203 *p_drv != NULL; p_drv++) {
205 if (name != NULL && (strcmp((*p_drv)->driver_name, name) != 0))
209 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
210 periph = TAILQ_NEXT(periph, unit_links)) {
211 if (xpt_path_comp(periph->path, path) == 0) {
224 cam_periph_acquire(struct cam_periph *periph)
229 return(CAM_REQ_CMP_ERR);
239 cam_periph_release(struct cam_periph *periph)
247 if ((--periph->refcount == 0)
248 && (periph->flags & CAM_PERIPH_INVALID)) {
249 camperiphfree(periph);
256 * Look for the next unit number that is not currently in use for this
257 * peripheral type starting at "newunit". Also exclude unit numbers that
258 * are reserved by for future "hardwiring" unless we already know that this
259 * is a potential wired device. Only assume that the device is "wired" the
260 * first time through the loop since after that we'll be looking at unit
261 * numbers that did not match a wiring entry.
264 camperiphnextunit(struct periph_driver *p_drv, u_int newunit, int wired)
266 struct cam_periph *periph;
267 char *periph_name, *strval;
273 periph_name = p_drv->driver_name;
276 for (periph = TAILQ_FIRST(&p_drv->units);
277 periph != NULL && periph->unit_number != newunit;
278 periph = TAILQ_NEXT(periph, unit_links))
281 if (periph != NULL && periph->unit_number == newunit) {
283 xpt_print_path(periph->path);
284 printf("Duplicate Wired Device entry!\n");
285 xpt_print_path(periph->path);
286 printf("Second device will not be wired\n");
295 * Don't match entries like "da 4" as a wired down
296 * device, but do match entries like "da 4 target 5"
297 * or even "da 4 scbus 1".
300 while ((i = resource_locate(i, periph_name)) != -1) {
301 dname = resource_query_name(i);
302 dunit = resource_query_unit(i);
303 /* if no "target" and no specific scbus, skip */
304 if (resource_int_value(dname, dunit, "target", &val) &&
305 (resource_string_value(dname, dunit, "at",&strval)||
306 strcmp(strval, "scbus") == 0))
308 if (newunit == dunit)
319 camperiphunit(struct periph_driver *p_drv, path_id_t pathid,
320 target_id_t target, lun_id_t lun)
323 int hit, i, val, dunit;
325 char pathbuf[32], *strval, *periph_name;
330 periph_name = p_drv->driver_name;
331 snprintf(pathbuf, sizeof(pathbuf), "scbus%d", pathid);
333 while ((i = resource_locate(i, periph_name)) != -1) {
334 dname = resource_query_name(i);
335 dunit = resource_query_unit(i);
336 if (resource_string_value(dname, dunit, "at", &strval) == 0) {
337 if (strcmp(strval, pathbuf) != 0)
341 if (resource_int_value(dname, dunit, "target", &val) == 0) {
346 if (resource_int_value(dname, dunit, "lun", &val) == 0) {
358 * Either start from 0 looking for the next unit or from
359 * the unit number given in the resource config. This way,
360 * if we have wildcard matches, we don't return the same
363 unit = camperiphnextunit(p_drv, unit, /*wired*/hit);
369 cam_periph_invalidate(struct cam_periph *periph)
375 * We only call this routine the first time a peripheral is
376 * invalidated. The oninvalidate() routine is always called at
379 if (((periph->flags & CAM_PERIPH_INVALID) == 0)
380 && (periph->periph_oninval != NULL))
381 periph->periph_oninval(periph);
383 periph->flags |= CAM_PERIPH_INVALID;
384 periph->flags &= ~CAM_PERIPH_NEW_DEV_FOUND;
386 if (periph->refcount == 0)
387 camperiphfree(periph);
388 else if (periph->refcount < 0)
389 printf("cam_invalidate_periph: refcount < 0!!\n");
394 camperiphfree(struct cam_periph *periph)
397 struct periph_driver **p_drv;
399 for (p_drv = (struct periph_driver **)periphdriver_set.ls_items;
400 *p_drv != NULL; p_drv++) {
401 if (strcmp((*p_drv)->driver_name, periph->periph_name) == 0)
405 if (periph->periph_dtor != NULL)
406 periph->periph_dtor(periph);
409 TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
410 (*p_drv)->generation++;
413 xpt_remove_periph(periph);
415 if (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) {
419 switch (periph->deferred_ac) {
420 case AC_FOUND_DEVICE:
421 ccb.ccb_h.func_code = XPT_GDEV_TYPE;
422 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/ 1);
426 case AC_PATH_REGISTERED:
427 ccb.ccb_h.func_code = XPT_PATH_INQ;
428 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/ 1);
436 periph->deferred_callback(NULL, periph->deferred_ac,
439 xpt_free_path(periph->path);
440 free(periph, M_DEVBUF);
444 * Wait interruptibly for an exclusive lock.
447 cam_periph_lock(struct cam_periph *periph, int priority)
451 while ((periph->flags & CAM_PERIPH_LOCKED) != 0) {
452 periph->flags |= CAM_PERIPH_LOCK_WANTED;
453 if ((error = tsleep(periph, priority, "caplck", 0)) != 0)
457 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
460 periph->flags |= CAM_PERIPH_LOCKED;
465 * Unlock and wake up any waiters.
468 cam_periph_unlock(struct cam_periph *periph)
470 periph->flags &= ~CAM_PERIPH_LOCKED;
471 if ((periph->flags & CAM_PERIPH_LOCK_WANTED) != 0) {
472 periph->flags &= ~CAM_PERIPH_LOCK_WANTED;
476 cam_periph_release(periph);
480 * Map user virtual pointers into kernel virtual address space, so we can
481 * access the memory. This won't work on physical pointers, for now it's
482 * up to the caller to check for that. (XXX KDM -- should we do that here
483 * instead?) This also only works for up to MAXPHYS memory. Since we use
484 * buffers to map stuff in and out, we're limited to the buffer size.
487 cam_periph_mapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
490 int flags[CAM_PERIPH_MAXMAPS];
491 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
492 u_int32_t lengths[CAM_PERIPH_MAXMAPS];
493 u_int32_t dirs[CAM_PERIPH_MAXMAPS];
495 switch(ccb->ccb_h.func_code) {
497 if (ccb->cdm.match_buf_len == 0) {
498 printf("cam_periph_mapmem: invalid match buffer "
502 if (ccb->cdm.pattern_buf_len > 0) {
503 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
504 lengths[0] = ccb->cdm.pattern_buf_len;
505 dirs[0] = CAM_DIR_OUT;
506 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
507 lengths[1] = ccb->cdm.match_buf_len;
508 dirs[1] = CAM_DIR_IN;
511 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
512 lengths[0] = ccb->cdm.match_buf_len;
513 dirs[0] = CAM_DIR_IN;
518 case XPT_CONT_TARGET_IO:
519 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
522 data_ptrs[0] = &ccb->csio.data_ptr;
523 lengths[0] = ccb->csio.dxfer_len;
524 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
529 break; /* NOTREACHED */
533 * Check the transfer length and permissions first, so we don't
534 * have to unmap any previously mapped buffers.
536 for (i = 0; i < numbufs; i++) {
541 * The userland data pointer passed in may not be page
542 * aligned. vmapbuf() truncates the address to a page
543 * boundary, so if the address isn't page aligned, we'll
544 * need enough space for the given transfer length, plus
545 * whatever extra space is necessary to make it to the page
549 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)) > DFLTPHYS){
550 printf("cam_periph_mapmem: attempt to map %lu bytes, "
551 "which is greater than DFLTPHYS(%d)\n",
553 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)),
558 if (dirs[i] & CAM_DIR_OUT) {
559 flags[i] = BIO_WRITE;
560 if (!useracc(*data_ptrs[i], lengths[i],
562 printf("cam_periph_mapmem: error, "
563 "address %p, length %lu isn't "
564 "user accessible for READ\n",
565 (void *)*data_ptrs[i],
571 if (dirs[i] & CAM_DIR_IN) {
573 if (!useracc(*data_ptrs[i], lengths[i],
575 printf("cam_periph_mapmem: error, "
576 "address %p, length %lu isn't "
577 "user accessible for WRITE\n",
578 (void *)*data_ptrs[i],
587 /* this keeps the current process from getting swapped */
589 * XXX KDM should I use P_NOSWAP instead?
593 for (i = 0; i < numbufs; i++) {
597 mapinfo->bp[i] = getpbuf(NULL);
599 /* save the buffer's data address */
600 mapinfo->bp[i]->b_saveaddr = mapinfo->bp[i]->b_data;
602 /* put our pointer in the data slot */
603 mapinfo->bp[i]->b_data = *data_ptrs[i];
605 /* set the transfer length, we know it's < DFLTPHYS */
606 mapinfo->bp[i]->b_bufsize = lengths[i];
609 mapinfo->bp[i]->b_flags = B_PHYS;
611 /* set the direction */
612 mapinfo->bp[i]->b_iocmd = flags[i];
614 /* map the buffer into kernel memory */
615 vmapbuf(mapinfo->bp[i]);
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 */
642 switch (ccb->ccb_h.func_code) {
644 numbufs = min(mapinfo->num_bufs_used, 2);
647 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
649 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
650 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
654 case XPT_CONT_TARGET_IO:
655 data_ptrs[0] = &ccb->csio.data_ptr;
656 numbufs = min(mapinfo->num_bufs_used, 1);
659 /* allow ourselves to be swapped once again */
662 break; /* NOTREACHED */
665 for (i = 0; i < numbufs; i++) {
666 /* Set the user's pointer back to the original value */
667 *data_ptrs[i] = mapinfo->bp[i]->b_saveaddr;
669 /* unmap the buffer */
670 vunmapbuf(mapinfo->bp[i]);
672 /* clear the flags we set above */
673 mapinfo->bp[i]->b_flags &= ~B_PHYS;
675 /* release the buffer */
676 relpbuf(mapinfo->bp[i], NULL);
679 /* allow ourselves to be swapped once again */
684 cam_periph_getccb(struct cam_periph *periph, u_int32_t priority)
686 struct ccb_hdr *ccb_h;
689 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdgetccb\n"));
693 while (periph->ccb_list.slh_first == NULL) {
694 if (periph->immediate_priority > priority)
695 periph->immediate_priority = priority;
696 xpt_schedule(periph, priority);
697 if ((periph->ccb_list.slh_first != NULL)
698 && (periph->ccb_list.slh_first->pinfo.priority == priority))
700 tsleep(&periph->ccb_list, PRIBIO, "cgticb", 0);
703 ccb_h = periph->ccb_list.slh_first;
704 SLIST_REMOVE_HEAD(&periph->ccb_list, periph_links.sle);
706 return ((union ccb *)ccb_h);
710 cam_periph_ccbwait(union ccb *ccb)
715 if ((ccb->ccb_h.pinfo.index != CAM_UNQUEUED_INDEX)
716 || ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG))
717 tsleep(&ccb->ccb_h.cbfcnp, PRIBIO, "cbwait", 0);
723 cam_periph_ioctl(struct cam_periph *periph, int cmd, caddr_t addr,
724 int (*error_routine)(union ccb *ccb,
726 u_int32_t sense_flags))
736 ccb = cam_periph_getccb(periph, /* priority */ 1);
737 xpt_setup_ccb(&ccb->ccb_h,
740 ccb->ccb_h.func_code = XPT_GDEVLIST;
743 * Basically, the point of this is that we go through
744 * getting the list of devices, until we find a passthrough
745 * device. In the current version of the CAM code, the
746 * only way to determine what type of device we're dealing
747 * with is by its name.
751 ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS;
752 while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) {
754 /* we want the next device in the list */
756 if (strncmp(ccb->cgdl.periph_name,
762 if ((ccb->cgdl.status == CAM_GDEVLIST_LAST_DEVICE) &&
764 ccb->cgdl.periph_name[0] = '\0';
765 ccb->cgdl.unit_number = 0;
770 /* copy the result back out */
771 bcopy(ccb, addr, sizeof(union ccb));
773 /* and release the ccb */
774 xpt_release_ccb(ccb);
785 cam_periph_runccb(union ccb *ccb,
786 int (*error_routine)(union ccb *ccb,
788 u_int32_t sense_flags),
789 cam_flags camflags, u_int32_t sense_flags,
797 * If the user has supplied a stats structure, and if we understand
798 * this particular type of ccb, record the transaction start.
800 if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO))
801 devstat_start_transaction(ds);
806 cam_periph_ccbwait(ccb);
807 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
809 else if (error_routine != NULL)
810 error = (*error_routine)(ccb, camflags, sense_flags);
814 } while (error == ERESTART);
816 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
817 cam_release_devq(ccb->ccb_h.path,
821 /* getcount_only */ FALSE);
823 if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO))
824 devstat_end_transaction(ds,
826 ccb->csio.tag_action & 0xf,
827 ((ccb->ccb_h.flags & CAM_DIR_MASK) ==
828 CAM_DIR_NONE) ? DEVSTAT_NO_DATA :
829 (ccb->ccb_h.flags & CAM_DIR_OUT) ?
837 cam_freeze_devq(struct cam_path *path)
839 struct ccb_hdr ccb_h;
841 xpt_setup_ccb(&ccb_h, path, /*priority*/1);
842 ccb_h.func_code = XPT_NOOP;
843 ccb_h.flags = CAM_DEV_QFREEZE;
844 xpt_action((union ccb *)&ccb_h);
848 cam_release_devq(struct cam_path *path, u_int32_t relsim_flags,
849 u_int32_t openings, u_int32_t timeout,
852 struct ccb_relsim crs;
854 xpt_setup_ccb(&crs.ccb_h, path,
856 crs.ccb_h.func_code = XPT_REL_SIMQ;
857 crs.ccb_h.flags = getcount_only ? CAM_DEV_QFREEZE : 0;
858 crs.release_flags = relsim_flags;
859 crs.openings = openings;
860 crs.release_timeout = timeout;
861 xpt_action((union ccb *)&crs);
862 return (crs.qfrozen_cnt);
865 #define saved_ccb_ptr ppriv_ptr0
867 camperiphdone(struct cam_periph *periph, union ccb *done_ccb)
872 struct scsi_start_stop_unit *scsi_cmd;
873 u_int32_t relsim_flags, timeout;
874 u_int32_t qfrozen_cnt;
876 status = done_ccb->ccb_h.status;
877 frozen = (status & CAM_DEV_QFRZN) != 0;
878 sense = (status & CAM_AUTOSNS_VALID) != 0;
879 status &= CAM_STATUS_MASK;
885 * Unfreeze the queue once if it is already frozen..
888 qfrozen_cnt = cam_release_devq(done_ccb->ccb_h.path,
900 * If we have successfully taken a device from the not
901 * ready to ready state, re-scan the device and re-get the
902 * inquiry information. Many devices (mostly disks) don't
903 * properly report their inquiry information unless they
906 if (done_ccb->ccb_h.func_code == XPT_SCSI_IO) {
907 scsi_cmd = (struct scsi_start_stop_unit *)
908 &done_ccb->csio.cdb_io.cdb_bytes;
910 if (scsi_cmd->opcode == START_STOP_UNIT)
911 xpt_async(AC_INQ_CHANGED,
912 done_ccb->ccb_h.path, NULL);
914 bcopy(done_ccb->ccb_h.saved_ccb_ptr, done_ccb,
917 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
919 xpt_action(done_ccb);
922 case CAM_SCSI_STATUS_ERROR:
923 scsi_cmd = (struct scsi_start_stop_unit *)
924 &done_ccb->csio.cdb_io.cdb_bytes;
926 struct scsi_sense_data *sense;
927 int error_code, sense_key, asc, ascq;
929 sense = &done_ccb->csio.sense_data;
930 scsi_extract_sense(sense, &error_code,
931 &sense_key, &asc, &ascq);
934 * If the error is "invalid field in CDB",
935 * and the load/eject flag is set, turn the
936 * flag off and try again. This is just in
937 * case the drive in question barfs on the
938 * load eject flag. The CAM code should set
939 * the load/eject flag by default for
944 * Should we check to see what the specific
945 * scsi status is?? Or does it not matter
946 * since we already know that there was an
947 * error, and we know what the specific
948 * error code was, and we know what the
951 if ((scsi_cmd->opcode == START_STOP_UNIT) &&
952 ((scsi_cmd->how & SSS_LOEJ) != 0) &&
953 (asc == 0x24) && (ascq == 0x00) &&
954 (done_ccb->ccb_h.retry_count > 0)) {
956 scsi_cmd->how &= ~SSS_LOEJ;
958 xpt_action(done_ccb);
960 } else if (done_ccb->ccb_h.retry_count > 0) {
962 * In this case, the error recovery
963 * command failed, but we've got
964 * some retries left on it. Give
968 /* set the timeout to .5 sec */
970 RELSIM_RELEASE_AFTER_TIMEOUT;
973 xpt_action(done_ccb);
979 * Copy the original CCB back and
980 * send it back to the caller.
982 bcopy(done_ccb->ccb_h.saved_ccb_ptr,
983 done_ccb, sizeof(union ccb));
985 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
987 xpt_action(done_ccb);
991 * Eh?? The command failed, but we don't
992 * have any sense. What's up with that?
993 * Fire the CCB again to return it to the
996 bcopy(done_ccb->ccb_h.saved_ccb_ptr,
997 done_ccb, sizeof(union ccb));
999 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1001 xpt_action(done_ccb);
1006 bcopy(done_ccb->ccb_h.saved_ccb_ptr, done_ccb,
1009 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1011 xpt_action(done_ccb);
1016 /* decrement the retry count */
1017 if (done_ccb->ccb_h.retry_count > 0)
1018 done_ccb->ccb_h.retry_count--;
1020 qfrozen_cnt = cam_release_devq(done_ccb->ccb_h.path,
1021 /*relsim_flags*/relsim_flags,
1024 /*getcount_only*/0);
1028 * Generic Async Event handler. Peripheral drivers usually
1029 * filter out the events that require personal attention,
1030 * and leave the rest to this function.
1033 cam_periph_async(struct cam_periph *periph, u_int32_t code,
1034 struct cam_path *path, void *arg)
1037 case AC_LOST_DEVICE:
1038 cam_periph_invalidate(periph);
1043 cam_periph_bus_settle(periph, SCSI_DELAY);
1052 cam_periph_bus_settle(struct cam_periph *periph, u_int bus_settle)
1054 struct ccb_getdevstats cgds;
1056 xpt_setup_ccb(&cgds.ccb_h, periph->path, /*priority*/1);
1057 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1058 xpt_action((union ccb *)&cgds);
1059 cam_periph_freeze_after_event(periph, &cgds.last_reset, bus_settle);
1063 cam_periph_freeze_after_event(struct cam_periph *periph,
1064 struct timeval* event_time, u_int duration_ms)
1066 struct timeval delta;
1067 struct timeval duration_tv;
1073 timevalsub(&delta, event_time);
1074 duration_tv.tv_sec = duration_ms / 1000;
1075 duration_tv.tv_usec = (duration_ms % 1000) * 1000;
1076 if (timevalcmp(&delta, &duration_tv, <)) {
1077 timevalsub(&duration_tv, &delta);
1079 duration_ms = duration_tv.tv_sec * 1000;
1080 duration_ms += duration_tv.tv_usec / 1000;
1081 cam_freeze_devq(periph->path);
1082 cam_release_devq(periph->path,
1083 RELSIM_RELEASE_AFTER_TIMEOUT,
1085 /*timeout*/duration_ms,
1086 /*getcount_only*/0);
1092 * Generic error handler. Peripheral drivers usually filter
1093 * out the errors that they handle in a unique mannor, then
1094 * call this function.
1097 cam_periph_error(union ccb *ccb, cam_flags camflags,
1098 u_int32_t sense_flags, union ccb *save_ccb)
1106 u_int32_t relsim_flags;
1109 status = ccb->ccb_h.status;
1110 frozen = (status & CAM_DEV_QFRZN) != 0;
1111 sense = (status & CAM_AUTOSNS_VALID) != 0;
1112 status &= CAM_STATUS_MASK;
1117 /* decrement the number of retries */
1118 retry = ccb->ccb_h.retry_count > 0;
1120 ccb->ccb_h.retry_count--;
1123 case CAM_AUTOSENSE_FAIL:
1124 case CAM_SCSI_STATUS_ERROR:
1126 switch (ccb->csio.scsi_status) {
1127 case SCSI_STATUS_OK:
1128 case SCSI_STATUS_COND_MET:
1129 case SCSI_STATUS_INTERMED:
1130 case SCSI_STATUS_INTERMED_COND_MET:
1133 case SCSI_STATUS_CMD_TERMINATED:
1134 case SCSI_STATUS_CHECK_COND:
1136 struct scsi_sense_data *sense;
1137 int error_code, sense_key, asc, ascq;
1138 struct cam_periph *periph;
1139 scsi_sense_action err_action;
1140 struct ccb_getdev cgd;
1142 sense = &ccb->csio.sense_data;
1143 scsi_extract_sense(sense, &error_code,
1144 &sense_key, &asc, &ascq);
1145 periph = xpt_path_periph(ccb->ccb_h.path);
1148 * Grab the inquiry data for this device.
1150 xpt_setup_ccb(&cgd.ccb_h, ccb->ccb_h.path,
1152 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1153 xpt_action((union ccb *)&cgd);
1155 err_action = scsi_error_action(asc, ascq,
1159 * Send a Test Unit Ready to the device.
1160 * If the 'many' flag is set, we send 120
1161 * test unit ready commands, one every half
1162 * second. Otherwise, we just send one TUR.
1163 * We only want to do this if the retry
1164 * count has not been exhausted.
1166 if (((err_action & SS_MASK) == SS_TUR)
1168 && ccb->ccb_h.retry_count > 0) {
1171 * Since error recovery is already
1172 * in progress, don't attempt to
1173 * process this error. It is probably
1174 * related to the error that caused
1175 * the currently active error recovery
1176 * action. Also, we only have
1177 * space for one saved CCB, so if we
1178 * had two concurrent error recovery
1179 * actions, we would end up
1180 * over-writing one error recovery
1181 * CCB with another one.
1184 CAM_PERIPH_RECOVERY_INPROG) {
1190 CAM_PERIPH_RECOVERY_INPROG;
1192 /* decrement the number of retries */
1194 SSQ_DECREMENT_COUNT) != 0) {
1196 ccb->ccb_h.retry_count--;
1199 bcopy(ccb, save_ccb, sizeof(*save_ccb));
1202 * We retry this one every half
1203 * second for a minute. If the
1204 * device hasn't become ready in a
1205 * minute's time, it's unlikely to
1206 * ever become ready. If the table
1207 * doesn't specify SSQ_MANY, we can
1208 * only try this once. Oh well.
1210 if ((err_action & SSQ_MANY) != 0)
1211 scsi_test_unit_ready(&ccb->csio,
1218 scsi_test_unit_ready(&ccb->csio,
1225 /* release the queue after .5 sec. */
1227 RELSIM_RELEASE_AFTER_TIMEOUT;
1230 * Drop the priority to 0 so that
1231 * we are the first to execute. Also
1232 * freeze the queue after this command
1233 * is sent so that we can restore the
1234 * old csio and have it queued in the
1235 * proper order before we let normal
1236 * transactions go to the drive.
1238 ccb->ccb_h.pinfo.priority = 0;
1239 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1242 * Save a pointer to the original
1243 * CCB in the new CCB.
1245 ccb->ccb_h.saved_ccb_ptr = save_ccb;
1250 * Send a start unit command to the device,
1251 * and then retry the command. We only
1252 * want to do this if the retry count has
1253 * not been exhausted. If the user
1254 * specified 0 retries, then we follow
1255 * their request and do not retry.
1257 else if (((err_action & SS_MASK) == SS_START)
1259 && ccb->ccb_h.retry_count > 0) {
1263 * Only one error recovery action
1264 * at a time. See above.
1267 CAM_PERIPH_RECOVERY_INPROG) {
1273 CAM_PERIPH_RECOVERY_INPROG;
1275 /* decrement the number of retries */
1277 ccb->ccb_h.retry_count--;
1280 * Check for removable media and
1281 * set load/eject flag
1284 if (SID_IS_REMOVABLE(&cgd.inq_data))
1290 * Attempt to start the drive up.
1292 * Save the current ccb so it can
1293 * be restored and retried once the
1294 * drive is started up.
1296 bcopy(ccb, save_ccb, sizeof(*save_ccb));
1298 scsi_start_stop(&ccb->csio,
1308 * Drop the priority to 0 so that
1309 * we are the first to execute. Also
1310 * freeze the queue after this command
1311 * is sent so that we can restore the
1312 * old csio and have it queued in the
1313 * proper order before we let normal
1314 * transactions go to the drive.
1316 ccb->ccb_h.pinfo.priority = 0;
1317 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1320 * Save a pointer to the original
1321 * CCB in the new CCB.
1323 ccb->ccb_h.saved_ccb_ptr = save_ccb;
1326 } else if ((sense_flags & SF_RETRY_UA) != 0) {
1328 * XXX KDM this is a *horrible*
1331 error = scsi_interpret_sense(ccb,
1340 * Theoretically, this code should send a
1341 * test unit ready to the given device, and
1342 * if it returns and error, send a start
1343 * unit command. Since we don't yet have
1344 * the capability to do two-command error
1345 * recovery, just send a start unit.
1348 else if (((err_action & SS_MASK) == SS_TURSTART)
1350 && ccb->ccb_h.retry_count > 0) {
1354 * Only one error recovery action
1355 * at a time. See above.
1358 CAM_PERIPH_RECOVERY_INPROG) {
1364 CAM_PERIPH_RECOVERY_INPROG;
1366 /* decrement the number of retries */
1368 ccb->ccb_h.retry_count--;
1371 * Check for removable media and
1372 * set load/eject flag
1375 if (SID_IS_REMOVABLE(&cgd.inq_data))
1381 * Attempt to start the drive up.
1383 * Save the current ccb so it can
1384 * be restored and retried once the
1385 * drive is started up.
1387 bcopy(ccb, save_ccb, sizeof(*save_ccb));
1389 scsi_start_stop(&ccb->csio,
1399 /* release the queue after .5 sec. */
1401 RELSIM_RELEASE_AFTER_TIMEOUT;
1404 * Drop the priority to 0 so that
1405 * we are the first to execute. Also
1406 * freeze the queue after this command
1407 * is sent so that we can restore the
1408 * old csio and have it queued in the
1409 * proper order before we let normal
1410 * transactions go to the drive.
1412 ccb->ccb_h.pinfo.priority = 0;
1413 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1416 * Save a pointer to the original
1417 * CCB in the new CCB.
1419 ccb->ccb_h.saved_ccb_ptr = save_ccb;
1423 error = scsi_interpret_sense(ccb,
1430 } else if (ccb->csio.scsi_status ==
1431 SCSI_STATUS_CHECK_COND
1432 && status != CAM_AUTOSENSE_FAIL) {
1433 /* no point in decrementing the retry count */
1434 panic("cam_periph_error: scsi status of "
1435 "CHECK COND returned but no sense "
1436 "information is availible. "
1437 "Controller should have returned "
1438 "CAM_AUTOSENSE_FAILED");
1441 } else if (ccb->ccb_h.retry_count == 0) {
1443 * XXX KDM shouldn't there be a better
1444 * argument to return??
1448 /* decrement the number of retries */
1449 retry = ccb->ccb_h.retry_count > 0;
1451 ccb->ccb_h.retry_count--;
1453 * If it was aborted with no
1454 * clue as to the reason, just
1460 case SCSI_STATUS_QUEUE_FULL:
1463 struct ccb_getdevstats cgds;
1466 * First off, find out what the current
1467 * transaction counts are.
1469 xpt_setup_ccb(&cgds.ccb_h,
1472 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1473 xpt_action((union ccb *)&cgds);
1476 * If we were the only transaction active, treat
1477 * the QUEUE FULL as if it were a BUSY condition.
1479 if (cgds.dev_active != 0) {
1483 * Reduce the number of openings to
1484 * be 1 less than the amount it took
1485 * to get a queue full bounded by the
1486 * minimum allowed tag count for this
1490 cgds.dev_active+cgds.dev_openings;
1491 openings = cgds.dev_active;
1492 if (openings < cgds.mintags)
1493 openings = cgds.mintags;
1494 if (openings < total_openings)
1495 relsim_flags = RELSIM_ADJUST_OPENINGS;
1498 * Some devices report queue full for
1499 * temporary resource shortages. For
1500 * this reason, we allow a minimum
1501 * tag count to be entered via a
1502 * quirk entry to prevent the queue
1503 * count on these devices from falling
1504 * to a pessimisticly low value. We
1505 * still wait for the next successful
1506 * completion, however, before queueing
1507 * more transactions to the device.
1510 RELSIM_RELEASE_AFTER_CMDCMPLT;
1518 case SCSI_STATUS_BUSY:
1520 * Restart the queue after either another
1521 * command completes or a 1 second timeout.
1522 * If we have any retries left, that is.
1524 retry = ccb->ccb_h.retry_count > 0;
1526 ccb->ccb_h.retry_count--;
1528 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT
1529 | RELSIM_RELEASE_AFTER_CMDCMPLT;
1535 case SCSI_STATUS_RESERV_CONFLICT:
1543 case CAM_REQ_CMP_ERR:
1544 case CAM_CMD_TIMEOUT:
1545 case CAM_UNEXP_BUSFREE:
1546 case CAM_UNCOR_PARITY:
1547 case CAM_DATA_RUN_ERR:
1548 /* decrement the number of retries */
1549 retry = ccb->ccb_h.retry_count > 0;
1551 ccb->ccb_h.retry_count--;
1559 case CAM_MSG_REJECT_REC:
1560 /* XXX Don't know that these are correct */
1563 case CAM_SEL_TIMEOUT:
1567 * A single selection timeout should not be enough
1568 * to invalidate a device. We should retry for multiple
1569 * seconds assuming this isn't a probe. We'll probably
1570 * need a special flag for that.
1573 struct cam_path *newpath;
1575 /* Should we do more if we can't create the path?? */
1576 if (xpt_create_path(&newpath, xpt_path_periph(ccb->ccb_h.path),
1577 xpt_path_path_id(ccb->ccb_h.path),
1578 xpt_path_target_id(ccb->ccb_h.path),
1579 CAM_LUN_WILDCARD) != CAM_REQ_CMP)
1582 * Let peripheral drivers know that this device has gone
1585 xpt_async(AC_LOST_DEVICE, newpath, NULL);
1586 xpt_free_path(newpath);
1588 if ((sense_flags & SF_RETRY_SELTO) != 0) {
1589 retry = ccb->ccb_h.retry_count > 0;
1591 ccb->ccb_h.retry_count--;
1594 * Wait half a second to give the device
1595 * time to recover before we try again.
1597 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1607 case CAM_REQ_INVALID:
1608 case CAM_PATH_INVALID:
1609 case CAM_DEV_NOT_THERE:
1611 case CAM_PROVIDE_FAIL:
1612 case CAM_REQ_TOO_BIG:
1615 case CAM_SCSI_BUS_RESET:
1617 case CAM_REQUEUE_REQ:
1618 /* Unconditional requeue, dammit */
1621 case CAM_RESRC_UNAVAIL:
1625 /* decrement the number of retries */
1626 retry = ccb->ccb_h.retry_count > 0;
1628 ccb->ccb_h.retry_count--;
1631 /* Check the sense codes */
1637 /* Attempt a retry */
1638 if (error == ERESTART || error == 0) {
1640 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1642 if (error == ERESTART)
1646 cam_release_devq(ccb->ccb_h.path,
1650 /*getcount_only*/0);