2 * Common functions for CAM "type" (peripheral) drivers.
4 * Copyright (c) 1997, 1998 Justin T. Gibbs.
5 * Copyright (c) 1997, 1998, 1999, 2000 Kenneth D. Merry.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions, and the following disclaimer,
13 * without modification, immediately at the beginning of the file.
14 * 2. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/types.h>
36 #include <sys/malloc.h>
37 #include <sys/kernel.h>
38 #include <sys/linker_set.h>
41 #include <sys/mutex.h>
44 #include <sys/devicestat.h>
47 #include <vm/vm_extern.h>
50 #include <cam/cam_ccb.h>
51 #include <cam/cam_xpt_periph.h>
52 #include <cam/cam_periph.h>
53 #include <cam/cam_debug.h>
55 #include <cam/scsi/scsi_all.h>
56 #include <cam/scsi/scsi_message.h>
57 #include <cam/scsi/scsi_pass.h>
59 static u_int camperiphnextunit(struct periph_driver *p_drv,
60 u_int newunit, int wired,
61 path_id_t pathid, target_id_t target,
63 static u_int camperiphunit(struct periph_driver *p_drv,
64 path_id_t pathid, target_id_t target,
66 static void camperiphdone(struct cam_periph *periph,
68 static void camperiphfree(struct cam_periph *periph);
69 static int camperiphscsistatuserror(union ccb *ccb,
71 u_int32_t sense_flags,
74 u_int32_t *relsim_flags,
76 static int camperiphscsisenseerror(union ccb *ccb,
78 u_int32_t sense_flags,
81 u_int32_t *relsim_flags,
84 static int nperiph_drivers;
85 struct periph_driver **periph_drivers;
87 MALLOC_DEFINE(M_CAMPERIPH, "CAM periph", "CAM peripheral buffers");
89 static int periph_selto_delay = 1000;
90 TUNABLE_INT("kern.cam.periph_selto_delay", &periph_selto_delay);
91 static int periph_noresrc_delay = 500;
92 TUNABLE_INT("kern.cam.periph_noresrc_delay", &periph_noresrc_delay);
93 static int periph_busy_delay = 500;
94 TUNABLE_INT("kern.cam.periph_busy_delay", &periph_busy_delay);
98 periphdriver_register(void *data)
100 struct periph_driver **newdrivers, **old;
103 ndrivers = nperiph_drivers + 2;
104 newdrivers = malloc(sizeof(*newdrivers) * ndrivers, M_TEMP, M_WAITOK);
106 bcopy(periph_drivers, newdrivers,
107 sizeof(*newdrivers) * nperiph_drivers);
108 newdrivers[nperiph_drivers] = (struct periph_driver *)data;
109 newdrivers[nperiph_drivers + 1] = NULL;
110 old = periph_drivers;
111 periph_drivers = newdrivers;
118 cam_periph_alloc(periph_ctor_t *periph_ctor,
119 periph_oninv_t *periph_oninvalidate,
120 periph_dtor_t *periph_dtor, periph_start_t *periph_start,
121 char *name, cam_periph_type type, struct cam_path *path,
122 ac_callback_t *ac_callback, ac_code code, void *arg)
124 struct periph_driver **p_drv;
125 struct cam_periph *periph;
126 struct cam_periph *cur_periph;
128 target_id_t target_id;
136 * Handle Hot-Plug scenarios. If there is already a peripheral
137 * of our type assigned to this path, we are likely waiting for
138 * final close on an old, invalidated, peripheral. If this is
139 * the case, queue up a deferred call to the peripheral's async
140 * handler. If it looks like a mistaken re-allocation, complain.
142 if ((periph = cam_periph_find(path, name)) != NULL) {
144 if ((periph->flags & CAM_PERIPH_INVALID) != 0
145 && (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) == 0) {
146 periph->flags |= CAM_PERIPH_NEW_DEV_FOUND;
147 periph->deferred_callback = ac_callback;
148 periph->deferred_ac = code;
149 return (CAM_REQ_INPROG);
151 printf("cam_periph_alloc: attempt to re-allocate "
152 "valid device %s%d rejected\n",
153 periph->periph_name, periph->unit_number);
155 return (CAM_REQ_INVALID);
158 periph = (struct cam_periph *)malloc(sizeof(*periph), M_CAMPERIPH,
162 return (CAM_RESRC_UNAVAIL);
166 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
167 if (strcmp((*p_drv)->driver_name, name) == 0)
171 path_id = xpt_path_path_id(path);
172 target_id = xpt_path_target_id(path);
173 lun_id = xpt_path_lun_id(path);
174 bzero(periph, sizeof(*periph));
175 cam_init_pinfo(&periph->pinfo);
176 periph->periph_start = periph_start;
177 periph->periph_dtor = periph_dtor;
178 periph->periph_oninval = periph_oninvalidate;
180 periph->periph_name = name;
181 periph->unit_number = camperiphunit(*p_drv, path_id, target_id, lun_id);
182 periph->immediate_priority = CAM_PRIORITY_NONE;
183 periph->refcount = 0;
184 SLIST_INIT(&periph->ccb_list);
185 status = xpt_create_path(&path, periph, path_id, target_id, lun_id);
186 if (status != CAM_REQ_CMP)
192 status = xpt_add_periph(periph);
194 if (status != CAM_REQ_CMP)
198 cur_periph = TAILQ_FIRST(&(*p_drv)->units);
199 while (cur_periph != NULL
200 && cur_periph->unit_number < periph->unit_number)
201 cur_periph = TAILQ_NEXT(cur_periph, unit_links);
203 if (cur_periph != NULL)
204 TAILQ_INSERT_BEFORE(cur_periph, periph, unit_links);
206 TAILQ_INSERT_TAIL(&(*p_drv)->units, periph, unit_links);
207 (*p_drv)->generation++;
214 status = periph_ctor(periph, arg);
216 if (status == CAM_REQ_CMP)
220 switch (init_level) {
222 /* Initialized successfully */
226 TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
228 xpt_remove_periph(periph);
231 xpt_free_path(periph->path);
234 free(periph, M_CAMPERIPH);
237 /* No cleanup to perform. */
240 panic("cam_periph_alloc: Unkown init level");
246 * Find a peripheral structure with the specified path, target, lun,
247 * and (optionally) type. If the name is NULL, this function will return
248 * the first peripheral driver that matches the specified path.
251 cam_periph_find(struct cam_path *path, char *name)
253 struct periph_driver **p_drv;
254 struct cam_periph *periph;
257 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
259 if (name != NULL && (strcmp((*p_drv)->driver_name, name) != 0))
263 TAILQ_FOREACH(periph, &(*p_drv)->units, unit_links) {
264 if (xpt_path_comp(periph->path, path) == 0) {
277 cam_periph_acquire(struct cam_periph *periph)
282 return(CAM_REQ_CMP_ERR);
292 cam_periph_release(struct cam_periph *periph)
300 if ((--periph->refcount == 0)
301 && (periph->flags & CAM_PERIPH_INVALID)) {
302 camperiphfree(periph);
309 * Look for the next unit number that is not currently in use for this
310 * peripheral type starting at "newunit". Also exclude unit numbers that
311 * are reserved by for future "hardwiring" unless we already know that this
312 * is a potential wired device. Only assume that the device is "wired" the
313 * first time through the loop since after that we'll be looking at unit
314 * numbers that did not match a wiring entry.
317 camperiphnextunit(struct periph_driver *p_drv, u_int newunit, int wired,
318 path_id_t pathid, target_id_t target, lun_id_t lun)
320 struct cam_periph *periph;
323 int i, val, dunit, r;
324 const char *dname, *strval;
327 periph_name = p_drv->driver_name;
330 for (periph = TAILQ_FIRST(&p_drv->units);
331 periph != NULL && periph->unit_number != newunit;
332 periph = TAILQ_NEXT(periph, unit_links))
335 if (periph != NULL && periph->unit_number == newunit) {
337 xpt_print(periph->path, "Duplicate Wired "
339 xpt_print(periph->path, "Second device (%s "
340 "device at scbus%d target %d lun %d) will "
341 "not be wired\n", periph_name, pathid,
351 * Don't match entries like "da 4" as a wired down
352 * device, but do match entries like "da 4 target 5"
353 * or even "da 4 scbus 1".
358 r = resource_find_dev(&i, dname, &dunit, NULL, NULL);
361 /* if no "target" and no specific scbus, skip */
362 if (resource_int_value(dname, dunit, "target", &val) &&
363 (resource_string_value(dname, dunit, "at",&strval)||
364 strcmp(strval, "scbus") == 0))
366 if (newunit == dunit)
377 camperiphunit(struct periph_driver *p_drv, path_id_t pathid,
378 target_id_t target, lun_id_t lun)
381 int wired, i, val, dunit;
382 const char *dname, *strval;
383 char pathbuf[32], *periph_name;
385 periph_name = p_drv->driver_name;
386 snprintf(pathbuf, sizeof(pathbuf), "scbus%d", pathid);
390 for (wired = 0; resource_find_dev(&i, dname, &dunit, NULL, NULL) == 0;
392 if (resource_string_value(dname, dunit, "at", &strval) == 0) {
393 if (strcmp(strval, pathbuf) != 0)
397 if (resource_int_value(dname, dunit, "target", &val) == 0) {
402 if (resource_int_value(dname, dunit, "lun", &val) == 0) {
414 * Either start from 0 looking for the next unit or from
415 * the unit number given in the resource config. This way,
416 * if we have wildcard matches, we don't return the same
419 unit = camperiphnextunit(p_drv, unit, wired, pathid, target, lun);
425 cam_periph_invalidate(struct cam_periph *periph)
431 * We only call this routine the first time a peripheral is
432 * invalidated. The oninvalidate() routine is always called at
435 if (((periph->flags & CAM_PERIPH_INVALID) == 0)
436 && (periph->periph_oninval != NULL))
437 periph->periph_oninval(periph);
439 periph->flags |= CAM_PERIPH_INVALID;
440 periph->flags &= ~CAM_PERIPH_NEW_DEV_FOUND;
442 if (periph->refcount == 0)
443 camperiphfree(periph);
444 else if (periph->refcount < 0)
445 printf("cam_invalidate_periph: refcount < 0!!\n");
450 camperiphfree(struct cam_periph *periph)
453 struct periph_driver **p_drv;
455 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
456 if (strcmp((*p_drv)->driver_name, periph->periph_name) == 0)
459 if (*p_drv == NULL) {
460 printf("camperiphfree: attempt to free non-existant periph\n");
464 if (periph->periph_dtor != NULL)
465 periph->periph_dtor(periph);
468 TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
469 (*p_drv)->generation++;
472 xpt_remove_periph(periph);
474 if (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) {
478 switch (periph->deferred_ac) {
479 case AC_FOUND_DEVICE:
480 ccb.ccb_h.func_code = XPT_GDEV_TYPE;
481 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/ 1);
485 case AC_PATH_REGISTERED:
486 ccb.ccb_h.func_code = XPT_PATH_INQ;
487 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/ 1);
495 periph->deferred_callback(NULL, periph->deferred_ac,
498 xpt_free_path(periph->path);
499 free(periph, M_CAMPERIPH);
503 * Wait interruptibly for an exclusive lock.
506 cam_periph_lock(struct cam_periph *periph, int priority)
511 * Increment the reference count on the peripheral
512 * while we wait for our lock attempt to succeed
513 * to ensure the peripheral doesn't disappear out
514 * from under us while we sleep.
516 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
519 while ((periph->flags & CAM_PERIPH_LOCKED) != 0) {
520 periph->flags |= CAM_PERIPH_LOCK_WANTED;
521 if ((error = tsleep(periph, priority, "caplck", 0)) != 0) {
522 cam_periph_release(periph);
527 periph->flags |= CAM_PERIPH_LOCKED;
532 * Unlock and wake up any waiters.
535 cam_periph_unlock(struct cam_periph *periph)
537 periph->flags &= ~CAM_PERIPH_LOCKED;
538 if ((periph->flags & CAM_PERIPH_LOCK_WANTED) != 0) {
539 periph->flags &= ~CAM_PERIPH_LOCK_WANTED;
543 cam_periph_release(periph);
547 * Map user virtual pointers into kernel virtual address space, so we can
548 * access the memory. This won't work on physical pointers, for now it's
549 * up to the caller to check for that. (XXX KDM -- should we do that here
550 * instead?) This also only works for up to MAXPHYS memory. Since we use
551 * buffers to map stuff in and out, we're limited to the buffer size.
554 cam_periph_mapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
557 int flags[CAM_PERIPH_MAXMAPS];
558 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
559 u_int32_t lengths[CAM_PERIPH_MAXMAPS];
560 u_int32_t dirs[CAM_PERIPH_MAXMAPS];
562 switch(ccb->ccb_h.func_code) {
564 if (ccb->cdm.match_buf_len == 0) {
565 printf("cam_periph_mapmem: invalid match buffer "
569 if (ccb->cdm.pattern_buf_len > 0) {
570 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
571 lengths[0] = ccb->cdm.pattern_buf_len;
572 dirs[0] = CAM_DIR_OUT;
573 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
574 lengths[1] = ccb->cdm.match_buf_len;
575 dirs[1] = CAM_DIR_IN;
578 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
579 lengths[0] = ccb->cdm.match_buf_len;
580 dirs[0] = CAM_DIR_IN;
585 case XPT_CONT_TARGET_IO:
586 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
589 data_ptrs[0] = &ccb->csio.data_ptr;
590 lengths[0] = ccb->csio.dxfer_len;
591 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
596 break; /* NOTREACHED */
600 * Check the transfer length and permissions first, so we don't
601 * have to unmap any previously mapped buffers.
603 for (i = 0; i < numbufs; i++) {
608 * The userland data pointer passed in may not be page
609 * aligned. vmapbuf() truncates the address to a page
610 * boundary, so if the address isn't page aligned, we'll
611 * need enough space for the given transfer length, plus
612 * whatever extra space is necessary to make it to the page
616 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)) > DFLTPHYS){
617 printf("cam_periph_mapmem: attempt to map %lu bytes, "
618 "which is greater than DFLTPHYS(%d)\n",
620 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)),
625 if (dirs[i] & CAM_DIR_OUT) {
626 flags[i] = BIO_WRITE;
629 if (dirs[i] & CAM_DIR_IN) {
635 /* this keeps the current process from getting swapped */
637 * XXX KDM should I use P_NOSWAP instead?
641 for (i = 0; i < numbufs; i++) {
645 mapinfo->bp[i] = getpbuf(NULL);
647 /* save the buffer's data address */
648 mapinfo->bp[i]->b_saveaddr = mapinfo->bp[i]->b_data;
650 /* put our pointer in the data slot */
651 mapinfo->bp[i]->b_data = *data_ptrs[i];
653 /* set the transfer length, we know it's < DFLTPHYS */
654 mapinfo->bp[i]->b_bufsize = lengths[i];
656 /* set the direction */
657 mapinfo->bp[i]->b_iocmd = flags[i];
660 * Map the buffer into kernel memory.
662 * Note that useracc() alone is not a sufficient test.
663 * vmapbuf() can still fail due to a smaller file mapped
664 * into a larger area of VM, or if userland races against
665 * vmapbuf() after the useracc() check.
667 if (vmapbuf(mapinfo->bp[i]) < 0) {
668 for (j = 0; j < i; ++j) {
669 *data_ptrs[j] = mapinfo->bp[j]->b_saveaddr;
670 vunmapbuf(mapinfo->bp[j]);
671 relpbuf(mapinfo->bp[j], NULL);
673 relpbuf(mapinfo->bp[i], NULL);
678 /* set our pointer to the new mapped area */
679 *data_ptrs[i] = mapinfo->bp[i]->b_data;
681 mapinfo->num_bufs_used++;
685 * Now that we've gotten this far, change ownership to the kernel
686 * of the buffers so that we don't run afoul of returning to user
687 * space with locks (on the buffer) held.
689 for (i = 0; i < numbufs; i++) {
690 BUF_KERNPROC(mapinfo->bp[i]);
698 * Unmap memory segments mapped into kernel virtual address space by
699 * cam_periph_mapmem().
702 cam_periph_unmapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
705 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
707 if (mapinfo->num_bufs_used <= 0) {
708 /* allow ourselves to be swapped once again */
713 switch (ccb->ccb_h.func_code) {
715 numbufs = min(mapinfo->num_bufs_used, 2);
718 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
720 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
721 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
725 case XPT_CONT_TARGET_IO:
726 data_ptrs[0] = &ccb->csio.data_ptr;
727 numbufs = min(mapinfo->num_bufs_used, 1);
730 /* allow ourselves to be swapped once again */
733 break; /* NOTREACHED */
736 for (i = 0; i < numbufs; i++) {
737 /* Set the user's pointer back to the original value */
738 *data_ptrs[i] = mapinfo->bp[i]->b_saveaddr;
740 /* unmap the buffer */
741 vunmapbuf(mapinfo->bp[i]);
743 /* release the buffer */
744 relpbuf(mapinfo->bp[i], NULL);
747 /* allow ourselves to be swapped once again */
752 cam_periph_getccb(struct cam_periph *periph, u_int32_t priority)
754 struct ccb_hdr *ccb_h;
757 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdgetccb\n"));
761 while (SLIST_FIRST(&periph->ccb_list) == NULL) {
762 if (periph->immediate_priority > priority)
763 periph->immediate_priority = priority;
764 xpt_schedule(periph, priority);
765 if ((SLIST_FIRST(&periph->ccb_list) != NULL)
766 && (SLIST_FIRST(&periph->ccb_list)->pinfo.priority == priority))
768 tsleep(&periph->ccb_list, PRIBIO, "cgticb", 0);
771 ccb_h = SLIST_FIRST(&periph->ccb_list);
772 SLIST_REMOVE_HEAD(&periph->ccb_list, periph_links.sle);
774 return ((union ccb *)ccb_h);
778 cam_periph_ccbwait(union ccb *ccb)
783 if ((ccb->ccb_h.pinfo.index != CAM_UNQUEUED_INDEX)
784 || ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG))
785 tsleep(&ccb->ccb_h.cbfcnp, PRIBIO, "cbwait", 0);
791 cam_periph_ioctl(struct cam_periph *periph, int cmd, caddr_t addr,
792 int (*error_routine)(union ccb *ccb,
794 u_int32_t sense_flags))
804 ccb = cam_periph_getccb(periph, /* priority */ 1);
805 xpt_setup_ccb(&ccb->ccb_h,
808 ccb->ccb_h.func_code = XPT_GDEVLIST;
811 * Basically, the point of this is that we go through
812 * getting the list of devices, until we find a passthrough
813 * device. In the current version of the CAM code, the
814 * only way to determine what type of device we're dealing
815 * with is by its name.
819 ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS;
820 while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) {
822 /* we want the next device in the list */
824 if (strncmp(ccb->cgdl.periph_name,
830 if ((ccb->cgdl.status == CAM_GDEVLIST_LAST_DEVICE) &&
832 ccb->cgdl.periph_name[0] = '\0';
833 ccb->cgdl.unit_number = 0;
838 /* copy the result back out */
839 bcopy(ccb, addr, sizeof(union ccb));
841 /* and release the ccb */
842 xpt_release_ccb(ccb);
853 cam_periph_runccb(union ccb *ccb,
854 int (*error_routine)(union ccb *ccb,
856 u_int32_t sense_flags),
857 cam_flags camflags, u_int32_t sense_flags,
865 * If the user has supplied a stats structure, and if we understand
866 * this particular type of ccb, record the transaction start.
868 if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO))
869 devstat_start_transaction(ds, NULL);
874 cam_periph_ccbwait(ccb);
875 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
877 else if (error_routine != NULL)
878 error = (*error_routine)(ccb, camflags, sense_flags);
882 } while (error == ERESTART);
884 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
885 cam_release_devq(ccb->ccb_h.path,
889 /* getcount_only */ FALSE);
891 if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO))
892 devstat_end_transaction(ds,
894 ccb->csio.tag_action & 0xf,
895 ((ccb->ccb_h.flags & CAM_DIR_MASK) ==
896 CAM_DIR_NONE) ? DEVSTAT_NO_DATA :
897 (ccb->ccb_h.flags & CAM_DIR_OUT) ?
899 DEVSTAT_READ, NULL, NULL);
905 cam_freeze_devq(struct cam_path *path)
907 struct ccb_hdr ccb_h;
909 xpt_setup_ccb(&ccb_h, path, /*priority*/1);
910 ccb_h.func_code = XPT_NOOP;
911 ccb_h.flags = CAM_DEV_QFREEZE;
912 xpt_action((union ccb *)&ccb_h);
916 cam_release_devq(struct cam_path *path, u_int32_t relsim_flags,
917 u_int32_t openings, u_int32_t timeout,
920 struct ccb_relsim crs;
922 xpt_setup_ccb(&crs.ccb_h, path,
924 crs.ccb_h.func_code = XPT_REL_SIMQ;
925 crs.ccb_h.flags = getcount_only ? CAM_DEV_QFREEZE : 0;
926 crs.release_flags = relsim_flags;
927 crs.openings = openings;
928 crs.release_timeout = timeout;
929 xpt_action((union ccb *)&crs);
930 return (crs.qfrozen_cnt);
933 #define saved_ccb_ptr ppriv_ptr0
935 camperiphdone(struct cam_periph *periph, union ccb *done_ccb)
937 union ccb *saved_ccb;
941 struct scsi_start_stop_unit *scsi_cmd;
942 u_int32_t relsim_flags, timeout;
943 u_int32_t qfrozen_cnt;
946 xpt_done_ccb = FALSE;
947 status = done_ccb->ccb_h.status;
948 frozen = (status & CAM_DEV_QFRZN) != 0;
949 sense = (status & CAM_AUTOSNS_VALID) != 0;
950 status &= CAM_STATUS_MASK;
954 saved_ccb = (union ccb *)done_ccb->ccb_h.saved_ccb_ptr;
957 * Unfreeze the queue once if it is already frozen..
960 qfrozen_cnt = cam_release_devq(done_ccb->ccb_h.path,
971 * If we have successfully taken a device from the not
972 * ready to ready state, re-scan the device and re-get
973 * the inquiry information. Many devices (mostly disks)
974 * don't properly report their inquiry information unless
977 * If we manually retrieved sense into a CCB and got
978 * something other than "NO SENSE" send the updated CCB
979 * back to the client via xpt_done() to be processed via
980 * the error recovery code again.
982 if (done_ccb->ccb_h.func_code == XPT_SCSI_IO) {
983 scsi_cmd = (struct scsi_start_stop_unit *)
984 &done_ccb->csio.cdb_io.cdb_bytes;
986 if (scsi_cmd->opcode == START_STOP_UNIT)
987 xpt_async(AC_INQ_CHANGED,
988 done_ccb->ccb_h.path, NULL);
989 if (scsi_cmd->opcode == REQUEST_SENSE) {
992 sense_key = saved_ccb->csio.sense_data.flags;
993 sense_key &= SSD_KEY;
994 if (sense_key != SSD_KEY_NO_SENSE) {
995 saved_ccb->ccb_h.status |=
998 xpt_print(saved_ccb->ccb_h.path,
999 "Recovered Sense\n");
1000 scsi_sense_print(&saved_ccb->csio);
1001 cam_error_print(saved_ccb, CAM_ESF_ALL,
1004 xpt_done_ccb = TRUE;
1008 bcopy(done_ccb->ccb_h.saved_ccb_ptr, done_ccb,
1011 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1013 if (xpt_done_ccb == FALSE)
1014 xpt_action(done_ccb);
1018 case CAM_SCSI_STATUS_ERROR:
1019 scsi_cmd = (struct scsi_start_stop_unit *)
1020 &done_ccb->csio.cdb_io.cdb_bytes;
1022 struct ccb_getdev cgd;
1023 struct scsi_sense_data *sense;
1024 int error_code, sense_key, asc, ascq;
1025 scsi_sense_action err_action;
1027 sense = &done_ccb->csio.sense_data;
1028 scsi_extract_sense(sense, &error_code,
1029 &sense_key, &asc, &ascq);
1032 * Grab the inquiry data for this device.
1034 xpt_setup_ccb(&cgd.ccb_h, done_ccb->ccb_h.path,
1036 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1037 xpt_action((union ccb *)&cgd);
1038 err_action = scsi_error_action(&done_ccb->csio,
1042 * If the error is "invalid field in CDB",
1043 * and the load/eject flag is set, turn the
1044 * flag off and try again. This is just in
1045 * case the drive in question barfs on the
1046 * load eject flag. The CAM code should set
1047 * the load/eject flag by default for
1052 * Should we check to see what the specific
1053 * scsi status is?? Or does it not matter
1054 * since we already know that there was an
1055 * error, and we know what the specific
1056 * error code was, and we know what the
1059 if ((scsi_cmd->opcode == START_STOP_UNIT) &&
1060 ((scsi_cmd->how & SSS_LOEJ) != 0) &&
1061 (asc == 0x24) && (ascq == 0x00) &&
1062 (done_ccb->ccb_h.retry_count > 0)) {
1064 scsi_cmd->how &= ~SSS_LOEJ;
1066 xpt_action(done_ccb);
1068 } else if ((done_ccb->ccb_h.retry_count > 1)
1069 && ((err_action & SS_MASK) != SS_FAIL)) {
1072 * In this case, the error recovery
1073 * command failed, but we've got
1074 * some retries left on it. Give
1075 * it another try unless this is an
1076 * unretryable error.
1079 /* set the timeout to .5 sec */
1081 RELSIM_RELEASE_AFTER_TIMEOUT;
1084 xpt_action(done_ccb);
1090 * Perform the final retry with the original
1091 * CCB so that final error processing is
1092 * performed by the owner of the CCB.
1094 bcopy(done_ccb->ccb_h.saved_ccb_ptr,
1095 done_ccb, sizeof(union ccb));
1097 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1099 xpt_action(done_ccb);
1103 * Eh?? The command failed, but we don't
1104 * have any sense. What's up with that?
1105 * Fire the CCB again to return it to the
1108 bcopy(done_ccb->ccb_h.saved_ccb_ptr,
1109 done_ccb, sizeof(union ccb));
1111 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1113 xpt_action(done_ccb);
1118 bcopy(done_ccb->ccb_h.saved_ccb_ptr, done_ccb,
1121 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1123 xpt_action(done_ccb);
1128 /* decrement the retry count */
1130 * XXX This isn't appropriate in all cases. Restructure,
1131 * so that the retry count is only decremented on an
1132 * actual retry. Remeber that the orignal ccb had its
1133 * retry count dropped before entering recovery, so
1134 * doing it again is a bug.
1136 if (done_ccb->ccb_h.retry_count > 0)
1137 done_ccb->ccb_h.retry_count--;
1139 qfrozen_cnt = cam_release_devq(done_ccb->ccb_h.path,
1140 /*relsim_flags*/relsim_flags,
1143 /*getcount_only*/0);
1144 if (xpt_done_ccb == TRUE)
1145 (*done_ccb->ccb_h.cbfcnp)(periph, done_ccb);
1149 * Generic Async Event handler. Peripheral drivers usually
1150 * filter out the events that require personal attention,
1151 * and leave the rest to this function.
1154 cam_periph_async(struct cam_periph *periph, u_int32_t code,
1155 struct cam_path *path, void *arg)
1158 case AC_LOST_DEVICE:
1159 cam_periph_invalidate(periph);
1164 cam_periph_bus_settle(periph, scsi_delay);
1173 cam_periph_bus_settle(struct cam_periph *periph, u_int bus_settle)
1175 struct ccb_getdevstats cgds;
1177 xpt_setup_ccb(&cgds.ccb_h, periph->path, /*priority*/1);
1178 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1179 xpt_action((union ccb *)&cgds);
1180 cam_periph_freeze_after_event(periph, &cgds.last_reset, bus_settle);
1184 cam_periph_freeze_after_event(struct cam_periph *periph,
1185 struct timeval* event_time, u_int duration_ms)
1187 struct timeval delta;
1188 struct timeval duration_tv;
1194 timevalsub(&delta, event_time);
1195 duration_tv.tv_sec = duration_ms / 1000;
1196 duration_tv.tv_usec = (duration_ms % 1000) * 1000;
1197 if (timevalcmp(&delta, &duration_tv, <)) {
1198 timevalsub(&duration_tv, &delta);
1200 duration_ms = duration_tv.tv_sec * 1000;
1201 duration_ms += duration_tv.tv_usec / 1000;
1202 cam_freeze_devq(periph->path);
1203 cam_release_devq(periph->path,
1204 RELSIM_RELEASE_AFTER_TIMEOUT,
1206 /*timeout*/duration_ms,
1207 /*getcount_only*/0);
1213 camperiphscsistatuserror(union ccb *ccb, cam_flags camflags,
1214 u_int32_t sense_flags, union ccb *save_ccb,
1215 int *openings, u_int32_t *relsim_flags,
1220 switch (ccb->csio.scsi_status) {
1221 case SCSI_STATUS_OK:
1222 case SCSI_STATUS_COND_MET:
1223 case SCSI_STATUS_INTERMED:
1224 case SCSI_STATUS_INTERMED_COND_MET:
1227 case SCSI_STATUS_CMD_TERMINATED:
1228 case SCSI_STATUS_CHECK_COND:
1229 error = camperiphscsisenseerror(ccb,
1237 case SCSI_STATUS_QUEUE_FULL:
1240 struct ccb_getdevstats cgds;
1243 * First off, find out what the current
1244 * transaction counts are.
1246 xpt_setup_ccb(&cgds.ccb_h,
1249 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1250 xpt_action((union ccb *)&cgds);
1253 * If we were the only transaction active, treat
1254 * the QUEUE FULL as if it were a BUSY condition.
1256 if (cgds.dev_active != 0) {
1260 * Reduce the number of openings to
1261 * be 1 less than the amount it took
1262 * to get a queue full bounded by the
1263 * minimum allowed tag count for this
1266 total_openings = cgds.dev_active + cgds.dev_openings;
1267 *openings = cgds.dev_active;
1268 if (*openings < cgds.mintags)
1269 *openings = cgds.mintags;
1270 if (*openings < total_openings)
1271 *relsim_flags = RELSIM_ADJUST_OPENINGS;
1274 * Some devices report queue full for
1275 * temporary resource shortages. For
1276 * this reason, we allow a minimum
1277 * tag count to be entered via a
1278 * quirk entry to prevent the queue
1279 * count on these devices from falling
1280 * to a pessimisticly low value. We
1281 * still wait for the next successful
1282 * completion, however, before queueing
1283 * more transactions to the device.
1285 *relsim_flags = RELSIM_RELEASE_AFTER_CMDCMPLT;
1290 xpt_print(ccb->ccb_h.path, "Queue Full\n");
1296 case SCSI_STATUS_BUSY:
1298 * Restart the queue after either another
1299 * command completes or a 1 second timeout.
1302 xpt_print(ccb->ccb_h.path, "Device Busy\n");
1304 if (ccb->ccb_h.retry_count > 0) {
1305 ccb->ccb_h.retry_count--;
1307 *relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT
1308 | RELSIM_RELEASE_AFTER_CMDCMPLT;
1314 case SCSI_STATUS_RESERV_CONFLICT:
1315 xpt_print(ccb->ccb_h.path, "Reservation Conflict\n");
1319 xpt_print(ccb->ccb_h.path, "SCSI Status 0x%x\n",
1320 ccb->csio.scsi_status);
1328 camperiphscsisenseerror(union ccb *ccb, cam_flags camflags,
1329 u_int32_t sense_flags, union ccb *save_ccb,
1330 int *openings, u_int32_t *relsim_flags,
1333 struct cam_periph *periph;
1336 periph = xpt_path_periph(ccb->ccb_h.path);
1337 if (periph->flags & CAM_PERIPH_RECOVERY_INPROG) {
1340 * If error recovery is already in progress, don't attempt
1341 * to process this error, but requeue it unconditionally
1342 * and attempt to process it once error recovery has
1343 * completed. This failed command is probably related to
1344 * the error that caused the currently active error recovery
1345 * action so our current recovery efforts should also
1346 * address this command. Be aware that the error recovery
1347 * code assumes that only one recovery action is in progress
1348 * on a particular peripheral instance at any given time
1349 * (e.g. only one saved CCB for error recovery) so it is
1350 * imperitive that we don't violate this assumption.
1354 scsi_sense_action err_action;
1355 struct ccb_getdev cgd;
1356 const char *action_string;
1357 union ccb* print_ccb;
1359 /* A description of the error recovery action performed */
1360 action_string = NULL;
1363 * The location of the orignal ccb
1364 * for sense printing purposes.
1369 * Grab the inquiry data for this device.
1371 xpt_setup_ccb(&cgd.ccb_h, ccb->ccb_h.path, /*priority*/ 1);
1372 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1373 xpt_action((union ccb *)&cgd);
1375 if ((ccb->ccb_h.status & CAM_AUTOSNS_VALID) != 0)
1376 err_action = scsi_error_action(&ccb->csio,
1379 else if ((ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)
1380 err_action = SS_REQSENSE;
1382 err_action = SS_RETRY|SSQ_DECREMENT_COUNT|EIO;
1384 error = err_action & SS_ERRMASK;
1387 * If the recovery action will consume a retry,
1388 * make sure we actually have retries available.
1390 if ((err_action & SSQ_DECREMENT_COUNT) != 0) {
1391 if (ccb->ccb_h.retry_count > 0)
1392 ccb->ccb_h.retry_count--;
1394 action_string = "Retries Exhausted";
1395 goto sense_error_done;
1399 if ((err_action & SS_MASK) >= SS_START) {
1401 * Do common portions of commands that
1402 * use recovery CCBs.
1404 if (save_ccb == NULL) {
1405 action_string = "No recovery CCB supplied";
1406 goto sense_error_done;
1408 bcopy(ccb, save_ccb, sizeof(*save_ccb));
1409 print_ccb = save_ccb;
1410 periph->flags |= CAM_PERIPH_RECOVERY_INPROG;
1413 switch (err_action & SS_MASK) {
1415 action_string = "No Recovery Action Needed";
1419 action_string = "Retrying Command (per Sense Data)";
1423 action_string = "Unretryable error";
1430 * Send a start unit command to the device, and
1431 * then retry the command.
1433 action_string = "Attempting to Start Unit";
1436 * Check for removable media and set
1437 * load/eject flag appropriately.
1439 if (SID_IS_REMOVABLE(&cgd.inq_data))
1444 scsi_start_stop(&ccb->csio,
1458 * Send a Test Unit Ready to the device.
1459 * If the 'many' flag is set, we send 120
1460 * test unit ready commands, one every half
1461 * second. Otherwise, we just send one TUR.
1462 * We only want to do this if the retry
1463 * count has not been exhausted.
1467 if ((err_action & SSQ_MANY) != 0) {
1468 action_string = "Polling device for readiness";
1471 action_string = "Testing device for readiness";
1474 scsi_test_unit_ready(&ccb->csio,
1482 * Accomplish our 500ms delay by deferring
1483 * the release of our device queue appropriately.
1485 *relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1492 * Send a Request Sense to the device. We
1493 * assume that we are in a contingent allegiance
1494 * condition so we do not tag this request.
1496 scsi_request_sense(&ccb->csio, /*retries*/1,
1498 &save_ccb->csio.sense_data,
1499 sizeof(save_ccb->csio.sense_data),
1500 CAM_TAG_ACTION_NONE,
1501 /*sense_len*/SSD_FULL_SIZE,
1506 panic("Unhandled error action %x", err_action);
1509 if ((err_action & SS_MASK) >= SS_START) {
1511 * Drop the priority to 0 so that the recovery
1512 * CCB is the first to execute. Freeze the queue
1513 * after this command is sent so that we can
1514 * restore the old csio and have it queued in
1515 * the proper order before we release normal
1516 * transactions to the device.
1518 ccb->ccb_h.pinfo.priority = 0;
1519 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1520 ccb->ccb_h.saved_ccb_ptr = save_ccb;
1525 if ((err_action & SSQ_PRINT_SENSE) != 0
1526 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID) != 0) {
1527 cam_error_print(print_ccb, CAM_ESF_ALL, CAM_EPF_ALL);
1528 xpt_print_path(ccb->ccb_h.path);
1530 scsi_sense_print(&print_ccb->csio);
1531 printf("%s\n", action_string);
1538 * Generic error handler. Peripheral drivers usually filter
1539 * out the errors that they handle in a unique mannor, then
1540 * call this function.
1543 cam_periph_error(union ccb *ccb, cam_flags camflags,
1544 u_int32_t sense_flags, union ccb *save_ccb)
1546 const char *action_string;
1549 int error, printed = 0;
1551 u_int32_t relsim_flags;
1552 u_int32_t timeout = 0;
1554 action_string = NULL;
1555 status = ccb->ccb_h.status;
1556 frozen = (status & CAM_DEV_QFRZN) != 0;
1557 status &= CAM_STATUS_MASK;
1558 openings = relsim_flags = 0;
1564 case CAM_SCSI_STATUS_ERROR:
1565 error = camperiphscsistatuserror(ccb,
1573 case CAM_AUTOSENSE_FAIL:
1574 xpt_print(ccb->ccb_h.path, "AutoSense Failed\n");
1575 error = EIO; /* we have to kill the command */
1577 case CAM_REQ_CMP_ERR:
1578 if (bootverbose && printed == 0) {
1579 xpt_print(ccb->ccb_h.path,
1580 "Request completed with CAM_REQ_CMP_ERR\n");
1584 case CAM_CMD_TIMEOUT:
1585 if (bootverbose && printed == 0) {
1586 xpt_print(ccb->ccb_h.path, "Command timed out\n");
1590 case CAM_UNEXP_BUSFREE:
1591 if (bootverbose && printed == 0) {
1592 xpt_print(ccb->ccb_h.path, "Unexpected Bus Free\n");
1596 case CAM_UNCOR_PARITY:
1597 if (bootverbose && printed == 0) {
1598 xpt_print(ccb->ccb_h.path,
1599 "Uncorrected Parity Error\n");
1603 case CAM_DATA_RUN_ERR:
1604 if (bootverbose && printed == 0) {
1605 xpt_print(ccb->ccb_h.path, "Data Overrun\n");
1608 error = EIO; /* we have to kill the command */
1609 /* decrement the number of retries */
1610 if (ccb->ccb_h.retry_count > 0) {
1611 ccb->ccb_h.retry_count--;
1614 action_string = "Retries Exausted";
1620 case CAM_MSG_REJECT_REC:
1621 /* XXX Don't know that these are correct */
1624 case CAM_SEL_TIMEOUT:
1626 struct cam_path *newpath;
1628 if ((camflags & CAM_RETRY_SELTO) != 0) {
1629 if (ccb->ccb_h.retry_count > 0) {
1631 ccb->ccb_h.retry_count--;
1633 if (bootverbose && printed == 0) {
1634 xpt_print(ccb->ccb_h.path,
1635 "Selection Timeout\n");
1640 * Wait a bit to give the device
1641 * time to recover before we try again.
1643 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1644 timeout = periph_selto_delay;
1649 /* Should we do more if we can't create the path?? */
1650 if (xpt_create_path(&newpath, xpt_path_periph(ccb->ccb_h.path),
1651 xpt_path_path_id(ccb->ccb_h.path),
1652 xpt_path_target_id(ccb->ccb_h.path),
1653 CAM_LUN_WILDCARD) != CAM_REQ_CMP)
1657 * Let peripheral drivers know that this device has gone
1660 xpt_async(AC_LOST_DEVICE, newpath, NULL);
1661 xpt_free_path(newpath);
1664 case CAM_REQ_INVALID:
1665 case CAM_PATH_INVALID:
1666 case CAM_DEV_NOT_THERE:
1668 case CAM_PROVIDE_FAIL:
1669 case CAM_REQ_TOO_BIG:
1670 case CAM_LUN_INVALID:
1671 case CAM_TID_INVALID:
1674 case CAM_SCSI_BUS_RESET:
1677 * Commands that repeatedly timeout and cause these
1678 * kinds of error recovery actions, should return
1679 * CAM_CMD_TIMEOUT, which allows us to safely assume
1680 * that this command was an innocent bystander to
1681 * these events and should be unconditionally
1684 if (bootverbose && printed == 0) {
1685 xpt_print_path(ccb->ccb_h.path);
1686 if (status == CAM_BDR_SENT)
1687 printf("Bus Device Reset sent\n");
1689 printf("Bus Reset issued\n");
1693 case CAM_REQUEUE_REQ:
1694 /* Unconditional requeue */
1696 if (bootverbose && printed == 0) {
1697 xpt_print(ccb->ccb_h.path, "Request Requeued\n");
1701 case CAM_RESRC_UNAVAIL:
1702 /* Wait a bit for the resource shortage to abate. */
1703 timeout = periph_noresrc_delay;
1707 /* Wait a bit for the busy condition to abate. */
1708 timeout = periph_busy_delay;
1710 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1713 /* decrement the number of retries */
1714 if (ccb->ccb_h.retry_count > 0) {
1715 ccb->ccb_h.retry_count--;
1717 if (bootverbose && printed == 0) {
1718 xpt_print(ccb->ccb_h.path, "CAM Status 0x%x\n",
1724 action_string = "Retries Exhausted";
1729 /* Attempt a retry */
1730 if (error == ERESTART || error == 0) {
1732 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1734 if (error == ERESTART) {
1735 action_string = "Retrying Command";
1740 cam_release_devq(ccb->ccb_h.path,
1744 /*getcount_only*/0);
1748 * If we have and error and are booting verbosely, whine
1749 * *unless* this was a non-retryable selection timeout.
1751 if (error != 0 && bootverbose &&
1752 !(status == CAM_SEL_TIMEOUT && (camflags & CAM_RETRY_SELTO) == 0)) {
1755 if (action_string == NULL)
1756 action_string = "Unretryable Error";
1757 if (error != ERESTART) {
1758 xpt_print(ccb->ccb_h.path, "error %d\n", error);
1760 xpt_print(ccb->ccb_h.path, "%s\n", action_string);