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.23 2007/11/14 02:05:35 pavalos Exp $
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/types.h>
36 #include <sys/malloc.h>
37 #include <sys/linker_set.h>
40 #include <sys/devicestat.h>
43 #include <vm/vm_extern.h>
45 #include <sys/thread2.h>
49 #include "cam_xpt_periph.h"
50 #include "cam_periph.h"
51 #include "cam_debug.h"
53 #include <bus/cam/scsi/scsi_all.h>
54 #include <bus/cam/scsi/scsi_message.h>
55 #include <bus/cam/scsi/scsi_pass.h>
57 static u_int camperiphnextunit(struct periph_driver *p_drv,
58 u_int newunit, int wired,
59 path_id_t pathid, target_id_t target,
61 static u_int camperiphunit(struct periph_driver *p_drv,
62 path_id_t pathid, target_id_t target,
64 static void camperiphdone(struct cam_periph *periph,
66 static void camperiphfree(struct cam_periph *periph);
69 cam_periph_alloc(periph_ctor_t *periph_ctor,
70 periph_oninv_t *periph_oninvalidate,
71 periph_dtor_t *periph_dtor, periph_start_t *periph_start,
72 char *name, cam_periph_type type, struct cam_path *path,
73 ac_callback_t *ac_callback, ac_code code, void *arg)
75 struct periph_driver **p_drv;
76 struct cam_periph *periph;
77 struct cam_periph *cur_periph;
79 target_id_t target_id;
86 * Handle Hot-Plug scenarios. If there is already a peripheral
87 * of our type assigned to this path, we are likely waiting for
88 * final close on an old, invalidated, peripheral. If this is
89 * the case, queue up a deferred call to the peripheral's async
90 * handler. If it looks like a mistaken re-alloation, complain.
92 if ((periph = cam_periph_find(path, name)) != NULL) {
94 if ((periph->flags & CAM_PERIPH_INVALID) != 0
95 && (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) == 0) {
96 periph->flags |= CAM_PERIPH_NEW_DEV_FOUND;
97 periph->deferred_callback = ac_callback;
98 periph->deferred_ac = code;
99 return (CAM_REQ_INPROG);
101 kprintf("cam_periph_alloc: attempt to re-allocate "
102 "valid device %s%d rejected\n",
103 periph->periph_name, periph->unit_number);
105 return (CAM_REQ_INVALID);
108 periph = kmalloc(sizeof(*periph), M_DEVBUF, M_INTWAIT | M_ZERO);
112 SET_FOREACH(p_drv, periphdriver_set) {
113 if (strcmp((*p_drv)->driver_name, name) == 0)
117 path_id = xpt_path_path_id(path);
118 target_id = xpt_path_target_id(path);
119 lun_id = xpt_path_lun_id(path);
120 cam_init_pinfo(&periph->pinfo);
121 periph->periph_start = periph_start;
122 periph->periph_dtor = periph_dtor;
123 periph->periph_oninval = periph_oninvalidate;
125 periph->periph_name = name;
126 periph->unit_number = camperiphunit(*p_drv, path_id, target_id, lun_id);
127 periph->immediate_priority = CAM_PRIORITY_NONE;
128 periph->refcount = 0;
129 SLIST_INIT(&periph->ccb_list);
130 status = xpt_create_path(&path, periph, path_id, target_id, lun_id);
131 if (status != CAM_REQ_CMP)
137 status = xpt_add_periph(periph);
139 if (status != CAM_REQ_CMP)
143 cur_periph = TAILQ_FIRST(&(*p_drv)->units);
144 while (cur_periph != NULL
145 && cur_periph->unit_number < periph->unit_number)
146 cur_periph = TAILQ_NEXT(cur_periph, unit_links);
148 if (cur_periph != NULL)
149 TAILQ_INSERT_BEFORE(cur_periph, periph, unit_links);
151 TAILQ_INSERT_TAIL(&(*p_drv)->units, periph, unit_links);
152 (*p_drv)->generation++;
159 status = periph_ctor(periph, arg);
161 if (status == CAM_REQ_CMP)
165 switch (init_level) {
167 /* Initialized successfully */
171 TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
173 xpt_remove_periph(periph);
175 xpt_free_path(periph->path);
177 kfree(periph, M_DEVBUF);
179 /* No cleanup to perform. */
182 panic("cam_periph_alloc: Unknown init level");
188 * Find a peripheral structure with the specified path, target, lun,
189 * and (optionally) type. If the name is NULL, this function will return
190 * the first peripheral driver that matches the specified path.
193 cam_periph_find(struct cam_path *path, char *name)
195 struct periph_driver **p_drv;
196 struct cam_periph *periph;
198 SET_FOREACH(p_drv, periphdriver_set) {
199 if (name != NULL && (strcmp((*p_drv)->driver_name, name) != 0))
203 TAILQ_FOREACH(periph, &(*p_drv)->units, unit_links) {
204 if (xpt_path_comp(periph->path, path) == 0) {
217 cam_periph_acquire(struct cam_periph *periph)
220 return(CAM_REQ_CMP_ERR);
230 cam_periph_release(struct cam_periph *periph)
236 if ((--periph->refcount == 0)
237 && (periph->flags & CAM_PERIPH_INVALID)) {
238 camperiphfree(periph);
244 * Look for the next unit number that is not currently in use for this
245 * peripheral type starting at "newunit". Also exclude unit numbers that
246 * are reserved by for future "hardwiring" unless we already know that this
247 * is a potential wired device. Only assume that the device is "wired" the
248 * first time through the loop since after that we'll be looking at unit
249 * numbers that did not match a wiring entry.
252 camperiphnextunit(struct periph_driver *p_drv, u_int newunit, int wired,
253 path_id_t pathid, target_id_t target, lun_id_t lun)
255 struct cam_periph *periph;
256 char *periph_name, *strval;
261 periph_name = p_drv->driver_name;
264 for (periph = TAILQ_FIRST(&p_drv->units);
265 periph != NULL && periph->unit_number != newunit;
266 periph = TAILQ_NEXT(periph, unit_links))
269 if (periph != NULL && periph->unit_number == newunit) {
271 xpt_print_path(periph->path);
272 kprintf("Duplicate Wired Device entry!\n");
273 xpt_print_path(periph->path);
274 kprintf("Second device (%s device at scbus%d "
275 "target %d lun %d) will not be wired\n",
276 periph_name, pathid, target, lun);
285 * Don't match entries like "da 4" as a wired down
286 * device, but do match entries like "da 4 target 5"
287 * or even "da 4 scbus 1".
290 while ((i = resource_locate(i, periph_name)) != -1) {
291 dname = resource_query_name(i);
292 dunit = resource_query_unit(i);
293 /* if no "target" and no specific scbus, skip */
294 if (resource_int_value(dname, dunit, "target", &val) &&
295 (resource_string_value(dname, dunit, "at",&strval)||
296 strcmp(strval, "scbus") == 0))
298 if (newunit == dunit)
309 camperiphunit(struct periph_driver *p_drv, path_id_t pathid,
310 target_id_t target, lun_id_t lun)
313 int hit, i, val, dunit;
315 char pathbuf[32], *strval, *periph_name;
319 periph_name = p_drv->driver_name;
320 ksnprintf(pathbuf, sizeof(pathbuf), "scbus%d", pathid);
322 for (hit = 0; (i = resource_locate(i, periph_name)) != -1; hit = 0) {
323 dname = resource_query_name(i);
324 dunit = resource_query_unit(i);
325 if (resource_string_value(dname, dunit, "at", &strval) == 0) {
326 if (strcmp(strval, pathbuf) != 0)
330 if (resource_int_value(dname, dunit, "target", &val) == 0) {
335 if (resource_int_value(dname, dunit, "lun", &val) == 0) {
347 * Either start from 0 looking for the next unit or from
348 * the unit number given in the resource config. This way,
349 * if we have wildcard matches, we don't return the same
352 unit = camperiphnextunit(p_drv, unit, /*wired*/hit, pathid,
359 cam_periph_invalidate(struct cam_periph *periph)
362 * We only call this routine the first time a peripheral is
363 * invalidated. The oninvalidate() routine is always called in
364 * a critical section.
367 if (((periph->flags & CAM_PERIPH_INVALID) == 0)
368 && (periph->periph_oninval != NULL))
369 periph->periph_oninval(periph);
371 periph->flags |= CAM_PERIPH_INVALID;
372 periph->flags &= ~CAM_PERIPH_NEW_DEV_FOUND;
374 if (periph->refcount == 0)
375 camperiphfree(periph);
376 else if (periph->refcount < 0)
377 kprintf("cam_invalidate_periph: refcount < 0!!\n");
382 camperiphfree(struct cam_periph *periph)
384 struct periph_driver **p_drv;
386 SET_FOREACH(p_drv, periphdriver_set) {
387 if (strcmp((*p_drv)->driver_name, periph->periph_name) == 0)
391 if (*p_drv == NULL) {
392 kprintf("camperiphfree: attempt to free "
393 "non-existent periph: %s\n", periph->periph_name);
397 if (periph->periph_dtor != NULL)
398 periph->periph_dtor(periph);
401 TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
402 (*p_drv)->generation++;
405 xpt_remove_periph(periph);
407 if (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) {
411 switch (periph->deferred_ac) {
412 case AC_FOUND_DEVICE:
413 ccb.ccb_h.func_code = XPT_GDEV_TYPE;
414 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/ 1);
418 case AC_PATH_REGISTERED:
419 ccb.ccb_h.func_code = XPT_PATH_INQ;
420 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/ 1);
428 periph->deferred_callback(NULL, periph->deferred_ac,
431 xpt_free_path(periph->path);
432 kfree(periph, M_DEVBUF);
436 * Wait interruptibly for an exclusive lock.
439 cam_periph_lock(struct cam_periph *periph, int flags)
443 while ((periph->flags & CAM_PERIPH_LOCKED) != 0) {
444 periph->flags |= CAM_PERIPH_LOCK_WANTED;
445 if ((error = tsleep(periph, flags, "caplck", 0)) != 0)
449 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
452 periph->flags |= CAM_PERIPH_LOCKED;
457 * Unlock and wake up any waiters.
460 cam_periph_unlock(struct cam_periph *periph)
462 periph->flags &= ~CAM_PERIPH_LOCKED;
463 if ((periph->flags & CAM_PERIPH_LOCK_WANTED) != 0) {
464 periph->flags &= ~CAM_PERIPH_LOCK_WANTED;
468 cam_periph_release(periph);
472 * Map user virtual pointers into kernel virtual address space, so we can
473 * access the memory. This won't work on physical pointers, for now it's
474 * up to the caller to check for that. (XXX KDM -- should we do that here
475 * instead?) This also only works for up to MAXPHYS memory. Since we use
476 * buffers to map stuff in and out, we're limited to the buffer size.
479 cam_periph_mapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
482 buf_cmd_t cmd[CAM_PERIPH_MAXMAPS];
483 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
484 u_int32_t lengths[CAM_PERIPH_MAXMAPS];
485 u_int32_t dirs[CAM_PERIPH_MAXMAPS];
487 switch(ccb->ccb_h.func_code) {
489 if (ccb->cdm.match_buf_len == 0) {
490 kprintf("cam_periph_mapmem: invalid match buffer "
494 if (ccb->cdm.pattern_buf_len > 0) {
495 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
496 lengths[0] = ccb->cdm.pattern_buf_len;
497 dirs[0] = CAM_DIR_OUT;
498 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
499 lengths[1] = ccb->cdm.match_buf_len;
500 dirs[1] = CAM_DIR_IN;
503 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
504 lengths[0] = ccb->cdm.match_buf_len;
505 dirs[0] = CAM_DIR_IN;
510 case XPT_CONT_TARGET_IO:
511 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
514 data_ptrs[0] = &ccb->csio.data_ptr;
515 lengths[0] = ccb->csio.dxfer_len;
516 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
521 break; /* NOTREACHED */
525 * Check the transfer length and permissions first, so we don't
526 * have to unmap any previously mapped buffers.
528 for (i = 0; i < numbufs; i++) {
530 * Its kinda bogus, we need a R+W command. For now the
531 * buffer needs some sort of command. Use BUF_CMD_WRITE
532 * to indicate a write and BUF_CMD_READ to indicate R+W.
534 cmd[i] = BUF_CMD_WRITE;
537 * The userland data pointer passed in may not be page
538 * aligned. vmapbuf() truncates the address to a page
539 * boundary, so if the address isn't page aligned, we'll
540 * need enough space for the given transfer length, plus
541 * whatever extra space is necessary to make it to the page
545 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)) > DFLTPHYS){
546 kprintf("cam_periph_mapmem: attempt to map %lu bytes, "
547 "which is greater than DFLTPHYS(%d)\n",
549 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)),
554 if (dirs[i] & CAM_DIR_OUT) {
555 if (!useracc(*data_ptrs[i], lengths[i],
557 kprintf("cam_periph_mapmem: error, "
558 "address %p, length %lu isn't "
559 "user accessible for READ\n",
560 (void *)*data_ptrs[i],
566 if (dirs[i] & CAM_DIR_IN) {
567 cmd[i] = BUF_CMD_READ;
568 if (!useracc(*data_ptrs[i], lengths[i],
570 kprintf("cam_periph_mapmem: error, "
571 "address %p, length %lu isn't "
572 "user accessible for WRITE\n",
573 (void *)*data_ptrs[i],
582 for (i = 0; i < numbufs; i++) {
586 mapinfo->bp[i] = getpbuf(NULL);
588 /* save the original user pointer */
589 mapinfo->saved_ptrs[i] = *data_ptrs[i];
592 mapinfo->bp[i]->b_cmd = cmd[i];
594 /* map the user buffer into kernel memory */
595 if (vmapbuf(mapinfo->bp[i], *data_ptrs[i], lengths[i]) < 0) {
596 kprintf("cam_periph_mapmem: error, "
597 "address %p, length %lu isn't "
598 "user accessible any more\n",
599 (void *)*data_ptrs[i],
601 for (j = 0; j < i; ++j) {
602 *data_ptrs[j] = mapinfo->saved_ptrs[j];
603 vunmapbuf(mapinfo->bp[j]);
604 relpbuf(mapinfo->bp[j], NULL);
606 mapinfo->num_bufs_used -= i;
610 /* set our pointer to the new mapped area */
611 *data_ptrs[i] = mapinfo->bp[i]->b_data;
613 mapinfo->num_bufs_used++;
620 * Unmap memory segments mapped into kernel virtual address space by
621 * cam_periph_mapmem().
624 cam_periph_unmapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
627 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
629 if (mapinfo->num_bufs_used <= 0) {
630 /* allow ourselves to be swapped once again */
634 switch (ccb->ccb_h.func_code) {
636 numbufs = min(mapinfo->num_bufs_used, 2);
639 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
641 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
642 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
646 case XPT_CONT_TARGET_IO:
647 data_ptrs[0] = &ccb->csio.data_ptr;
648 numbufs = min(mapinfo->num_bufs_used, 1);
651 /* allow ourselves to be swapped once again */
653 break; /* NOTREACHED */
656 for (i = 0; i < numbufs; i++) {
657 /* Set the user's pointer back to the original value */
658 *data_ptrs[i] = mapinfo->saved_ptrs[i];
660 /* unmap the buffer */
661 vunmapbuf(mapinfo->bp[i]);
663 /* release the buffer */
664 relpbuf(mapinfo->bp[i], NULL);
667 /* allow ourselves to be swapped once again */
671 cam_periph_getccb(struct cam_periph *periph, u_int32_t priority)
673 struct ccb_hdr *ccb_h;
675 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdgetccb\n"));
679 while (SLIST_FIRST(&periph->ccb_list) == NULL) {
680 if (periph->immediate_priority > priority)
681 periph->immediate_priority = priority;
682 xpt_schedule(periph, priority);
683 if ((SLIST_FIRST(&periph->ccb_list) != NULL)
684 && (SLIST_FIRST(&periph->ccb_list)->pinfo.priority == priority))
686 tsleep(&periph->ccb_list, 0, "cgticb", 0);
689 ccb_h = SLIST_FIRST(&periph->ccb_list);
690 SLIST_REMOVE_HEAD(&periph->ccb_list, periph_links.sle);
692 return ((union ccb *)ccb_h);
696 cam_periph_ccbwait(union ccb *ccb)
699 if ((ccb->ccb_h.pinfo.index != CAM_UNQUEUED_INDEX)
700 || ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG))
701 tsleep(&ccb->ccb_h.cbfcnp, 0, "cbwait", 0);
706 cam_periph_ioctl(struct cam_periph *periph, int cmd, caddr_t addr,
707 int (*error_routine)(union ccb *ccb,
709 u_int32_t sense_flags))
719 ccb = cam_periph_getccb(periph, /* priority */ 1);
720 xpt_setup_ccb(&ccb->ccb_h,
723 ccb->ccb_h.func_code = XPT_GDEVLIST;
726 * Basically, the point of this is that we go through
727 * getting the list of devices, until we find a passthrough
728 * device. In the current version of the CAM code, the
729 * only way to determine what type of device we're dealing
730 * with is by its name.
734 ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS;
735 while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) {
737 /* we want the next device in the list */
739 if (strncmp(ccb->cgdl.periph_name,
745 if ((ccb->cgdl.status == CAM_GDEVLIST_LAST_DEVICE) &&
747 ccb->cgdl.periph_name[0] = '\0';
748 ccb->cgdl.unit_number = 0;
753 /* copy the result back out */
754 bcopy(ccb, addr, sizeof(union ccb));
756 /* and release the ccb */
757 xpt_release_ccb(ccb);
768 cam_periph_runccb(union ccb *ccb,
769 int (*error_routine)(union ccb *ccb,
771 u_int32_t sense_flags),
772 cam_flags camflags, u_int32_t sense_flags,
780 * If the user has supplied a stats structure, and if we understand
781 * this particular type of ccb, record the transaction start.
783 if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO))
784 devstat_start_transaction(ds);
789 cam_periph_ccbwait(ccb);
790 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
792 else if (error_routine != NULL)
793 error = (*error_routine)(ccb, camflags, sense_flags);
797 } while (error == ERESTART);
799 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
800 cam_release_devq(ccb->ccb_h.path,
804 /* getcount_only */ FALSE);
806 if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO))
807 devstat_end_transaction(ds,
809 ccb->csio.tag_action & 0xf,
810 ((ccb->ccb_h.flags & CAM_DIR_MASK) ==
811 CAM_DIR_NONE) ? DEVSTAT_NO_DATA :
812 (ccb->ccb_h.flags & CAM_DIR_OUT) ?
820 cam_freeze_devq(struct cam_path *path)
822 struct ccb_hdr ccb_h;
824 xpt_setup_ccb(&ccb_h, path, /*priority*/1);
825 ccb_h.func_code = XPT_NOOP;
826 ccb_h.flags = CAM_DEV_QFREEZE;
827 xpt_action((union ccb *)&ccb_h);
831 cam_release_devq(struct cam_path *path, u_int32_t relsim_flags,
832 u_int32_t openings, u_int32_t timeout,
835 struct ccb_relsim crs;
837 xpt_setup_ccb(&crs.ccb_h, path,
839 crs.ccb_h.func_code = XPT_REL_SIMQ;
840 crs.ccb_h.flags = getcount_only ? CAM_DEV_QFREEZE : 0;
841 crs.release_flags = relsim_flags;
842 crs.openings = openings;
843 crs.release_timeout = timeout;
844 xpt_action((union ccb *)&crs);
845 return (crs.qfrozen_cnt);
848 #define saved_ccb_ptr ppriv_ptr0
850 camperiphdone(struct cam_periph *periph, union ccb *done_ccb)
855 struct scsi_start_stop_unit *scsi_cmd;
856 u_int32_t relsim_flags, timeout;
857 u_int32_t qfrozen_cnt;
859 status = done_ccb->ccb_h.status;
860 frozen = (status & CAM_DEV_QFRZN) != 0;
861 sense = (status & CAM_AUTOSNS_VALID) != 0;
862 status &= CAM_STATUS_MASK;
868 * Unfreeze the queue once if it is already frozen..
871 qfrozen_cnt = cam_release_devq(done_ccb->ccb_h.path,
883 * If we have successfully taken a device from the not
884 * ready to ready state, re-scan the device and re-get the
885 * inquiry information. Many devices (mostly disks) don't
886 * properly report their inquiry information unless they
889 if (done_ccb->ccb_h.func_code == XPT_SCSI_IO) {
890 scsi_cmd = (struct scsi_start_stop_unit *)
891 &done_ccb->csio.cdb_io.cdb_bytes;
893 if (scsi_cmd->opcode == START_STOP_UNIT)
894 xpt_async(AC_INQ_CHANGED,
895 done_ccb->ccb_h.path, NULL);
897 bcopy(done_ccb->ccb_h.saved_ccb_ptr, done_ccb,
900 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
902 xpt_action(done_ccb);
905 case CAM_SCSI_STATUS_ERROR:
906 scsi_cmd = (struct scsi_start_stop_unit *)
907 &done_ccb->csio.cdb_io.cdb_bytes;
909 struct scsi_sense_data *sense;
910 int error_code, sense_key, asc, ascq;
912 sense = &done_ccb->csio.sense_data;
913 scsi_extract_sense(sense, &error_code,
914 &sense_key, &asc, &ascq);
917 * If the error is "invalid field in CDB",
918 * and the load/eject flag is set, turn the
919 * flag off and try again. This is just in
920 * case the drive in question barfs on the
921 * load eject flag. The CAM code should set
922 * the load/eject flag by default for
927 * Should we check to see what the specific
928 * scsi status is?? Or does it not matter
929 * since we already know that there was an
930 * error, and we know what the specific
931 * error code was, and we know what the
934 if ((scsi_cmd->opcode == START_STOP_UNIT) &&
935 ((scsi_cmd->how & SSS_LOEJ) != 0) &&
936 (asc == 0x24) && (ascq == 0x00) &&
937 (done_ccb->ccb_h.retry_count > 0)) {
939 scsi_cmd->how &= ~SSS_LOEJ;
941 xpt_action(done_ccb);
943 } else if (done_ccb->ccb_h.retry_count > 0) {
945 * In this case, the error recovery
946 * command failed, but we've got
947 * some retries left on it. Give
951 /* set the timeout to .5 sec */
953 RELSIM_RELEASE_AFTER_TIMEOUT;
956 xpt_action(done_ccb);
962 * Copy the original CCB back and
963 * send it back to the caller.
965 bcopy(done_ccb->ccb_h.saved_ccb_ptr,
966 done_ccb, sizeof(union ccb));
968 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
970 xpt_action(done_ccb);
974 * Eh?? The command failed, but we don't
975 * have any sense. What's up with that?
976 * Fire the CCB again to return it to the
979 bcopy(done_ccb->ccb_h.saved_ccb_ptr,
980 done_ccb, sizeof(union ccb));
982 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
984 xpt_action(done_ccb);
989 bcopy(done_ccb->ccb_h.saved_ccb_ptr, done_ccb,
992 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
994 xpt_action(done_ccb);
999 /* decrement the retry count */
1000 if (done_ccb->ccb_h.retry_count > 0)
1001 done_ccb->ccb_h.retry_count--;
1003 qfrozen_cnt = cam_release_devq(done_ccb->ccb_h.path,
1004 /*relsim_flags*/relsim_flags,
1007 /*getcount_only*/0);
1011 * Generic Async Event handler. Peripheral drivers usually
1012 * filter out the events that require personal attention,
1013 * and leave the rest to this function.
1016 cam_periph_async(struct cam_periph *periph, u_int32_t code,
1017 struct cam_path *path, void *arg)
1020 case AC_LOST_DEVICE:
1021 cam_periph_invalidate(periph);
1026 cam_periph_bus_settle(periph, SCSI_DELAY);
1035 cam_periph_bus_settle(struct cam_periph *periph, u_int bus_settle)
1037 struct ccb_getdevstats cgds;
1039 xpt_setup_ccb(&cgds.ccb_h, periph->path, /*priority*/1);
1040 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1041 xpt_action((union ccb *)&cgds);
1042 cam_periph_freeze_after_event(periph, &cgds.last_reset, bus_settle);
1046 cam_periph_freeze_after_event(struct cam_periph *periph,
1047 struct timeval* event_time, u_int duration_ms)
1049 struct timeval delta;
1050 struct timeval duration_tv;
1052 microuptime(&delta);
1053 timevalsub(&delta, event_time);
1054 duration_tv.tv_sec = duration_ms / 1000;
1055 duration_tv.tv_usec = (duration_ms % 1000) * 1000;
1056 if (timevalcmp(&delta, &duration_tv, <)) {
1057 timevalsub(&duration_tv, &delta);
1059 duration_ms = duration_tv.tv_sec * 1000;
1060 duration_ms += duration_tv.tv_usec / 1000;
1061 cam_freeze_devq(periph->path);
1062 cam_release_devq(periph->path,
1063 RELSIM_RELEASE_AFTER_TIMEOUT,
1065 /*timeout*/duration_ms,
1066 /*getcount_only*/0);
1072 * Generic error handler. Peripheral drivers usually filter
1073 * out the errors that they handle in a unique mannor, then
1074 * call this function.
1077 cam_periph_error(union ccb *ccb, cam_flags camflags,
1078 u_int32_t sense_flags, union ccb *save_ccb)
1086 u_int32_t relsim_flags;
1089 status = ccb->ccb_h.status;
1090 frozen = (status & CAM_DEV_QFRZN) != 0;
1091 sense = (status & CAM_AUTOSNS_VALID) != 0;
1092 status &= CAM_STATUS_MASK;
1097 /* decrement the number of retries */
1098 retry = ccb->ccb_h.retry_count > 0;
1100 ccb->ccb_h.retry_count--;
1103 case CAM_AUTOSENSE_FAIL:
1104 case CAM_SCSI_STATUS_ERROR:
1106 switch (ccb->csio.scsi_status) {
1107 case SCSI_STATUS_OK:
1108 case SCSI_STATUS_COND_MET:
1109 case SCSI_STATUS_INTERMED:
1110 case SCSI_STATUS_INTERMED_COND_MET:
1113 case SCSI_STATUS_CMD_TERMINATED:
1114 case SCSI_STATUS_CHECK_COND:
1116 struct scsi_sense_data *sense;
1117 int error_code, sense_key, asc, ascq;
1118 struct cam_periph *periph;
1119 scsi_sense_action err_action;
1120 struct ccb_getdev cgd;
1122 sense = &ccb->csio.sense_data;
1123 scsi_extract_sense(sense, &error_code,
1124 &sense_key, &asc, &ascq);
1125 periph = xpt_path_periph(ccb->ccb_h.path);
1128 * Grab the inquiry data for this device.
1130 xpt_setup_ccb(&cgd.ccb_h, ccb->ccb_h.path,
1132 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1133 xpt_action((union ccb *)&cgd);
1135 err_action = scsi_error_action(asc, ascq,
1139 * Send a Test Unit Ready to the device.
1140 * If the 'many' flag is set, we send 120
1141 * test unit ready commands, one every half
1142 * second. Otherwise, we just send one TUR.
1143 * We only want to do this if the retry
1144 * count has not been exhausted.
1146 if (((err_action & SS_MASK) == SS_TUR)
1148 && ccb->ccb_h.retry_count > 0) {
1151 * Since error recovery is already
1152 * in progress, don't attempt to
1153 * process this error. It is probably
1154 * related to the error that caused
1155 * the currently active error recovery
1156 * action. Also, we only have
1157 * space for one saved CCB, so if we
1158 * had two concurrent error recovery
1159 * actions, we would end up
1160 * over-writing one error recovery
1161 * CCB with another one.
1164 CAM_PERIPH_RECOVERY_INPROG) {
1170 CAM_PERIPH_RECOVERY_INPROG;
1172 /* decrement the number of retries */
1174 SSQ_DECREMENT_COUNT) != 0) {
1176 ccb->ccb_h.retry_count--;
1179 bcopy(ccb, save_ccb, sizeof(*save_ccb));
1182 * We retry this one every half
1183 * second for a minute. If the
1184 * device hasn't become ready in a
1185 * minute's time, it's unlikely to
1186 * ever become ready. If the table
1187 * doesn't specify SSQ_MANY, we can
1188 * only try this once. Oh well.
1190 if ((err_action & SSQ_MANY) != 0)
1191 scsi_test_unit_ready(&ccb->csio,
1198 scsi_test_unit_ready(&ccb->csio,
1205 /* release the queue after .5 sec. */
1207 RELSIM_RELEASE_AFTER_TIMEOUT;
1210 * Drop the priority to 0 so that
1211 * we are the first to execute. Also
1212 * freeze the queue after this command
1213 * is sent so that we can restore the
1214 * old csio and have it queued in the
1215 * proper order before we let normal
1216 * transactions go to the drive.
1218 ccb->ccb_h.pinfo.priority = 0;
1219 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1222 * Save a pointer to the original
1223 * CCB in the new CCB.
1225 ccb->ccb_h.saved_ccb_ptr = save_ccb;
1230 * Send a start unit command to the device,
1231 * and then retry the command. We only
1232 * want to do this if the retry count has
1233 * not been exhausted. If the user
1234 * specified 0 retries, then we follow
1235 * their request and do not retry.
1237 else if (((err_action & SS_MASK) == SS_START)
1239 && ccb->ccb_h.retry_count > 0) {
1243 * Only one error recovery action
1244 * at a time. See above.
1247 CAM_PERIPH_RECOVERY_INPROG) {
1253 CAM_PERIPH_RECOVERY_INPROG;
1255 /* decrement the number of retries */
1257 ccb->ccb_h.retry_count--;
1260 * Check for removable media and
1261 * set load/eject flag
1264 if (SID_IS_REMOVABLE(&cgd.inq_data))
1270 * Attempt to start the drive up.
1272 * Save the current ccb so it can
1273 * be restored and retried once the
1274 * drive is started up.
1276 bcopy(ccb, save_ccb, sizeof(*save_ccb));
1278 scsi_start_stop(&ccb->csio,
1288 * Drop the priority to 0 so that
1289 * we are the first to execute. Also
1290 * freeze the queue after this command
1291 * is sent so that we can restore the
1292 * old csio and have it queued in the
1293 * proper order before we let normal
1294 * transactions go to the drive.
1296 ccb->ccb_h.pinfo.priority = 0;
1297 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1300 * Save a pointer to the original
1301 * CCB in the new CCB.
1303 ccb->ccb_h.saved_ccb_ptr = save_ccb;
1306 } else if ((sense_flags & SF_RETRY_UA) != 0) {
1308 * XXX KDM this is a *horrible*
1311 error = scsi_interpret_sense(ccb,
1320 * Theoretically, this code should send a
1321 * test unit ready to the given device, and
1322 * if it returns and error, send a start
1323 * unit command. Since we don't yet have
1324 * the capability to do two-command error
1325 * recovery, just send a start unit.
1328 else if (((err_action & SS_MASK) == SS_TURSTART)
1330 && ccb->ccb_h.retry_count > 0) {
1334 * Only one error recovery action
1335 * at a time. See above.
1338 CAM_PERIPH_RECOVERY_INPROG) {
1344 CAM_PERIPH_RECOVERY_INPROG;
1346 /* decrement the number of retries */
1348 ccb->ccb_h.retry_count--;
1351 * Check for removable media and
1352 * set load/eject flag
1355 if (SID_IS_REMOVABLE(&cgd.inq_data))
1361 * Attempt to start the drive up.
1363 * Save the current ccb so it can
1364 * be restored and retried once the
1365 * drive is started up.
1367 bcopy(ccb, save_ccb, sizeof(*save_ccb));
1369 scsi_start_stop(&ccb->csio,
1379 /* release the queue after .5 sec. */
1381 RELSIM_RELEASE_AFTER_TIMEOUT;
1384 * Drop the priority to 0 so that
1385 * we are the first to execute. Also
1386 * freeze the queue after this command
1387 * is sent so that we can restore the
1388 * old csio and have it queued in the
1389 * proper order before we let normal
1390 * transactions go to the drive.
1392 ccb->ccb_h.pinfo.priority = 0;
1393 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1396 * Save a pointer to the original
1397 * CCB in the new CCB.
1399 ccb->ccb_h.saved_ccb_ptr = save_ccb;
1403 error = scsi_interpret_sense(ccb,
1410 } else if (ccb->csio.scsi_status ==
1411 SCSI_STATUS_CHECK_COND
1412 && status != CAM_AUTOSENSE_FAIL) {
1413 /* no point in decrementing the retry count */
1414 panic("cam_periph_error: scsi status of "
1415 "CHECK COND returned but no sense "
1416 "information is available. "
1417 "Controller should have returned "
1418 "CAM_AUTOSENSE_FAILED");
1421 } else if (ccb->ccb_h.retry_count == 0) {
1423 * XXX KDM shouldn't there be a better
1424 * argument to return??
1428 /* decrement the number of retries */
1429 retry = ccb->ccb_h.retry_count > 0;
1431 ccb->ccb_h.retry_count--;
1433 * If it was aborted with no
1434 * clue as to the reason, just
1440 case SCSI_STATUS_QUEUE_FULL:
1443 struct ccb_getdevstats cgds;
1446 * First off, find out what the current
1447 * transaction counts are.
1449 xpt_setup_ccb(&cgds.ccb_h,
1452 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1453 xpt_action((union ccb *)&cgds);
1456 * If we were the only transaction active, treat
1457 * the QUEUE FULL as if it were a BUSY condition.
1459 if (cgds.dev_active != 0) {
1463 * Reduce the number of openings to
1464 * be 1 less than the amount it took
1465 * to get a queue full bounded by the
1466 * minimum allowed tag count for this
1470 cgds.dev_active+cgds.dev_openings;
1471 openings = cgds.dev_active;
1472 if (openings < cgds.mintags)
1473 openings = cgds.mintags;
1474 if (openings < total_openings)
1475 relsim_flags = RELSIM_ADJUST_OPENINGS;
1478 * Some devices report queue full for
1479 * temporary resource shortages. For
1480 * this reason, we allow a minimum
1481 * tag count to be entered via a
1482 * quirk entry to prevent the queue
1483 * count on these devices from falling
1484 * to a pessimisticly low value. We
1485 * still wait for the next successful
1486 * completion, however, before queueing
1487 * more transactions to the device.
1490 RELSIM_RELEASE_AFTER_CMDCMPLT;
1498 case SCSI_STATUS_BUSY:
1500 * Restart the queue after either another
1501 * command completes or a 1 second timeout.
1502 * If we have any retries left, that is.
1504 retry = ccb->ccb_h.retry_count > 0;
1506 ccb->ccb_h.retry_count--;
1508 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT
1509 | RELSIM_RELEASE_AFTER_CMDCMPLT;
1515 case SCSI_STATUS_RESERV_CONFLICT:
1523 case CAM_REQ_CMP_ERR:
1524 case CAM_CMD_TIMEOUT:
1525 case CAM_UNEXP_BUSFREE:
1526 case CAM_UNCOR_PARITY:
1527 case CAM_DATA_RUN_ERR:
1528 /* decrement the number of retries */
1529 retry = ccb->ccb_h.retry_count > 0;
1531 ccb->ccb_h.retry_count--;
1539 case CAM_MSG_REJECT_REC:
1540 /* XXX Don't know that these are correct */
1543 case CAM_SEL_TIMEOUT:
1547 * A single selection timeout should not be enough
1548 * to invalidate a device. We should retry for multiple
1549 * seconds assuming this isn't a probe. We'll probably
1550 * need a special flag for that.
1553 struct cam_path *newpath;
1555 /* Should we do more if we can't create the path?? */
1556 if (xpt_create_path(&newpath, xpt_path_periph(ccb->ccb_h.path),
1557 xpt_path_path_id(ccb->ccb_h.path),
1558 xpt_path_target_id(ccb->ccb_h.path),
1559 CAM_LUN_WILDCARD) != CAM_REQ_CMP)
1562 * Let peripheral drivers know that this device has gone
1565 xpt_async(AC_LOST_DEVICE, newpath, NULL);
1566 xpt_free_path(newpath);
1568 if ((sense_flags & SF_RETRY_SELTO) != 0) {
1569 retry = ccb->ccb_h.retry_count > 0;
1571 ccb->ccb_h.retry_count--;
1574 * Wait half a second to give the device
1575 * time to recover before we try again.
1577 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1587 case CAM_REQ_INVALID:
1588 case CAM_PATH_INVALID:
1589 case CAM_DEV_NOT_THERE:
1591 case CAM_PROVIDE_FAIL:
1592 case CAM_REQ_TOO_BIG:
1595 case CAM_SCSI_BUS_RESET:
1597 case CAM_REQUEUE_REQ:
1598 /* Unconditional requeue, dammit */
1601 case CAM_RESRC_UNAVAIL:
1605 /* decrement the number of retries */
1606 retry = ccb->ccb_h.retry_count > 0;
1608 ccb->ccb_h.retry_count--;
1611 /* Check the sense codes */
1617 /* Attempt a retry */
1618 if (error == ERESTART || error == 0) {
1620 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1622 if (error == ERESTART)
1626 cam_release_devq(ccb->ccb_h.path,
1630 /*getcount_only*/0);