FreeBSD/Linux Kernel Cross Reference
sys/cam/cam_periph.c
1 /*-
2 * Common functions for CAM "type" (peripheral) drivers.
3 *
4 * Copyright (c) 1997, 1998 Justin T. Gibbs.
5 * Copyright (c) 1997, 1998, 1999, 2000 Kenneth D. Merry.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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.
16 *
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
27 * SUCH DAMAGE.
28 */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
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>
39 #include <sys/bio.h>
40 #include <sys/lock.h>
41 #include <sys/mutex.h>
42 #include <sys/buf.h>
43 #include <sys/proc.h>
44 #include <sys/devicestat.h>
45 #include <sys/bus.h>
46 #include <vm/vm.h>
47 #include <vm/vm_extern.h>
48
49 #include <cam/cam.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>
54
55 #include <cam/scsi/scsi_all.h>
56 #include <cam/scsi/scsi_message.h>
57 #include <cam/scsi/scsi_pass.h>
58
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,
62 lun_id_t lun);
63 static u_int camperiphunit(struct periph_driver *p_drv,
64 path_id_t pathid, target_id_t target,
65 lun_id_t lun);
66 static void camperiphdone(struct cam_periph *periph,
67 union ccb *done_ccb);
68 static void camperiphfree(struct cam_periph *periph);
69 static int camperiphscsistatuserror(union ccb *ccb,
70 cam_flags camflags,
71 u_int32_t sense_flags,
72 union ccb *save_ccb,
73 int *openings,
74 u_int32_t *relsim_flags,
75 u_int32_t *timeout);
76 static int camperiphscsisenseerror(union ccb *ccb,
77 cam_flags camflags,
78 u_int32_t sense_flags,
79 union ccb *save_ccb,
80 int *openings,
81 u_int32_t *relsim_flags,
82 u_int32_t *timeout);
83
84 static int nperiph_drivers;
85 struct periph_driver **periph_drivers;
86
87 MALLOC_DEFINE(M_CAMPERIPH, "CAM periph", "CAM peripheral buffers");
88
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);
95
96
97 void
98 periphdriver_register(void *data)
99 {
100 struct periph_driver **newdrivers, **old;
101 int ndrivers;
102
103 ndrivers = nperiph_drivers + 2;
104 newdrivers = malloc(sizeof(*newdrivers) * ndrivers, M_CAMPERIPH,
105 M_WAITOK);
106 if (periph_drivers)
107 bcopy(periph_drivers, newdrivers,
108 sizeof(*newdrivers) * nperiph_drivers);
109 newdrivers[nperiph_drivers] = (struct periph_driver *)data;
110 newdrivers[nperiph_drivers + 1] = NULL;
111 old = periph_drivers;
112 periph_drivers = newdrivers;
113 if (old)
114 free(old, M_CAMPERIPH);
115 nperiph_drivers++;
116 }
117
118 cam_status
119 cam_periph_alloc(periph_ctor_t *periph_ctor,
120 periph_oninv_t *periph_oninvalidate,
121 periph_dtor_t *periph_dtor, periph_start_t *periph_start,
122 char *name, cam_periph_type type, struct cam_path *path,
123 ac_callback_t *ac_callback, ac_code code, void *arg)
124 {
125 struct periph_driver **p_drv;
126 struct cam_periph *periph;
127 struct cam_periph *cur_periph;
128 path_id_t path_id;
129 target_id_t target_id;
130 lun_id_t lun_id;
131 cam_status status;
132 u_int init_level;
133 int s;
134
135 init_level = 0;
136 /*
137 * Handle Hot-Plug scenarios. If there is already a peripheral
138 * of our type assigned to this path, we are likely waiting for
139 * final close on an old, invalidated, peripheral. If this is
140 * the case, queue up a deferred call to the peripheral's async
141 * handler. If it looks like a mistaken re-allocation, complain.
142 */
143 if ((periph = cam_periph_find(path, name)) != NULL) {
144
145 if ((periph->flags & CAM_PERIPH_INVALID) != 0
146 && (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) == 0) {
147 periph->flags |= CAM_PERIPH_NEW_DEV_FOUND;
148 periph->deferred_callback = ac_callback;
149 periph->deferred_ac = code;
150 return (CAM_REQ_INPROG);
151 } else {
152 printf("cam_periph_alloc: attempt to re-allocate "
153 "valid device %s%d rejected\n",
154 periph->periph_name, periph->unit_number);
155 }
156 return (CAM_REQ_INVALID);
157 }
158
159 periph = (struct cam_periph *)malloc(sizeof(*periph), M_CAMPERIPH,
160 M_NOWAIT);
161
162 if (periph == NULL)
163 return (CAM_RESRC_UNAVAIL);
164
165 init_level++;
166
167 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
168 if (strcmp((*p_drv)->driver_name, name) == 0)
169 break;
170 }
171
172 path_id = xpt_path_path_id(path);
173 target_id = xpt_path_target_id(path);
174 lun_id = xpt_path_lun_id(path);
175 bzero(periph, sizeof(*periph));
176 cam_init_pinfo(&periph->pinfo);
177 periph->periph_start = periph_start;
178 periph->periph_dtor = periph_dtor;
179 periph->periph_oninval = periph_oninvalidate;
180 periph->type = type;
181 periph->periph_name = name;
182 periph->unit_number = camperiphunit(*p_drv, path_id, target_id, lun_id);
183 periph->immediate_priority = CAM_PRIORITY_NONE;
184 periph->refcount = 0;
185 SLIST_INIT(&periph->ccb_list);
186 status = xpt_create_path(&path, periph, path_id, target_id, lun_id);
187 if (status != CAM_REQ_CMP)
188 goto failure;
189
190 periph->path = path;
191 init_level++;
192
193 status = xpt_add_periph(periph);
194
195 if (status != CAM_REQ_CMP)
196 goto failure;
197
198 s = splsoftcam();
199 cur_periph = TAILQ_FIRST(&(*p_drv)->units);
200 while (cur_periph != NULL
201 && cur_periph->unit_number < periph->unit_number)
202 cur_periph = TAILQ_NEXT(cur_periph, unit_links);
203
204 if (cur_periph != NULL)
205 TAILQ_INSERT_BEFORE(cur_periph, periph, unit_links);
206 else {
207 TAILQ_INSERT_TAIL(&(*p_drv)->units, periph, unit_links);
208 (*p_drv)->generation++;
209 }
210
211 splx(s);
212
213 init_level++;
214
215 status = periph_ctor(periph, arg);
216
217 if (status == CAM_REQ_CMP)
218 init_level++;
219
220 failure:
221 switch (init_level) {
222 case 4:
223 /* Initialized successfully */
224 break;
225 case 3:
226 s = splsoftcam();
227 TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
228 splx(s);
229 xpt_remove_periph(periph);
230 /* FALLTHROUGH */
231 case 2:
232 xpt_free_path(periph->path);
233 /* FALLTHROUGH */
234 case 1:
235 free(periph, M_CAMPERIPH);
236 /* FALLTHROUGH */
237 case 0:
238 /* No cleanup to perform. */
239 break;
240 default:
241 panic("cam_periph_alloc: Unkown init level");
242 }
243 return(status);
244 }
245
246 /*
247 * Find a peripheral structure with the specified path, target, lun,
248 * and (optionally) type. If the name is NULL, this function will return
249 * the first peripheral driver that matches the specified path.
250 */
251 struct cam_periph *
252 cam_periph_find(struct cam_path *path, char *name)
253 {
254 struct periph_driver **p_drv;
255 struct cam_periph *periph;
256 int s;
257
258 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
259
260 if (name != NULL && (strcmp((*p_drv)->driver_name, name) != 0))
261 continue;
262
263 s = splsoftcam();
264 TAILQ_FOREACH(periph, &(*p_drv)->units, unit_links) {
265 if (xpt_path_comp(periph->path, path) == 0) {
266 splx(s);
267 return(periph);
268 }
269 }
270 splx(s);
271 if (name != NULL)
272 return(NULL);
273 }
274 return(NULL);
275 }
276
277 cam_status
278 cam_periph_acquire(struct cam_periph *periph)
279 {
280 int s;
281
282 if (periph == NULL)
283 return(CAM_REQ_CMP_ERR);
284
285 s = splsoftcam();
286 periph->refcount++;
287 splx(s);
288
289 return(CAM_REQ_CMP);
290 }
291
292 void
293 cam_periph_release(struct cam_periph *periph)
294 {
295 int s;
296
297 if (periph == NULL)
298 return;
299
300 s = splsoftcam();
301 if ((--periph->refcount == 0)
302 && (periph->flags & CAM_PERIPH_INVALID)) {
303 camperiphfree(periph);
304 }
305 splx(s);
306
307 }
308
309 /*
310 * Look for the next unit number that is not currently in use for this
311 * peripheral type starting at "newunit". Also exclude unit numbers that
312 * are reserved by for future "hardwiring" unless we already know that this
313 * is a potential wired device. Only assume that the device is "wired" the
314 * first time through the loop since after that we'll be looking at unit
315 * numbers that did not match a wiring entry.
316 */
317 static u_int
318 camperiphnextunit(struct periph_driver *p_drv, u_int newunit, int wired,
319 path_id_t pathid, target_id_t target, lun_id_t lun)
320 {
321 struct cam_periph *periph;
322 char *periph_name;
323 int s;
324 int i, val, dunit, r;
325 const char *dname, *strval;
326
327 s = splsoftcam();
328 periph_name = p_drv->driver_name;
329 for (;;newunit++) {
330
331 for (periph = TAILQ_FIRST(&p_drv->units);
332 periph != NULL && periph->unit_number != newunit;
333 periph = TAILQ_NEXT(periph, unit_links))
334 ;
335
336 if (periph != NULL && periph->unit_number == newunit) {
337 if (wired != 0) {
338 xpt_print_path(periph->path);
339 printf("Duplicate Wired Device entry!\n");
340 xpt_print_path(periph->path);
341 printf("Second device (%s device at scbus%d "
342 "target %d lun %d) will not be wired\n",
343 periph_name, pathid, target, lun);
344 wired = 0;
345 }
346 continue;
347 }
348 if (wired)
349 break;
350
351 /*
352 * Don't match entries like "da 4" as a wired down
353 * device, but do match entries like "da 4 target 5"
354 * or even "da 4 scbus 1".
355 */
356 i = 0;
357 dname = periph_name;
358 for (;;) {
359 r = resource_find_dev(&i, dname, &dunit, NULL, NULL);
360 if (r != 0)
361 break;
362 /* if no "target" and no specific scbus, skip */
363 if (resource_int_value(dname, dunit, "target", &val) &&
364 (resource_string_value(dname, dunit, "at",&strval)||
365 strcmp(strval, "scbus") == 0))
366 continue;
367 if (newunit == dunit)
368 break;
369 }
370 if (r != 0)
371 break;
372 }
373 splx(s);
374 return (newunit);
375 }
376
377 static u_int
378 camperiphunit(struct periph_driver *p_drv, path_id_t pathid,
379 target_id_t target, lun_id_t lun)
380 {
381 u_int unit;
382 int wired, i, val, dunit;
383 const char *dname, *strval;
384 char pathbuf[32], *periph_name;
385
386 periph_name = p_drv->driver_name;
387 snprintf(pathbuf, sizeof(pathbuf), "scbus%d", pathid);
388 unit = 0;
389 i = 0;
390 dname = periph_name;
391 for (wired = 0; resource_find_dev(&i, dname, &dunit, NULL, NULL) == 0;
392 wired = 0) {
393 if (resource_string_value(dname, dunit, "at", &strval) == 0) {
394 if (strcmp(strval, pathbuf) != 0)
395 continue;
396 wired++;
397 }
398 if (resource_int_value(dname, dunit, "target", &val) == 0) {
399 if (val != target)
400 continue;
401 wired++;
402 }
403 if (resource_int_value(dname, dunit, "lun", &val) == 0) {
404 if (val != lun)
405 continue;
406 wired++;
407 }
408 if (wired != 0) {
409 unit = dunit;
410 break;
411 }
412 }
413
414 /*
415 * Either start from 0 looking for the next unit or from
416 * the unit number given in the resource config. This way,
417 * if we have wildcard matches, we don't return the same
418 * unit number twice.
419 */
420 unit = camperiphnextunit(p_drv, unit, wired, pathid, target, lun);
421
422 return (unit);
423 }
424
425 void
426 cam_periph_invalidate(struct cam_periph *periph)
427 {
428 int s;
429
430 s = splsoftcam();
431 /*
432 * We only call this routine the first time a peripheral is
433 * invalidated. The oninvalidate() routine is always called at
434 * splsoftcam().
435 */
436 if (((periph->flags & CAM_PERIPH_INVALID) == 0)
437 && (periph->periph_oninval != NULL))
438 periph->periph_oninval(periph);
439
440 periph->flags |= CAM_PERIPH_INVALID;
441 periph->flags &= ~CAM_PERIPH_NEW_DEV_FOUND;
442
443 if (periph->refcount == 0)
444 camperiphfree(periph);
445 else if (periph->refcount < 0)
446 printf("cam_invalidate_periph: refcount < 0!!\n");
447 splx(s);
448 }
449
450 static void
451 camperiphfree(struct cam_periph *periph)
452 {
453 int s;
454 struct periph_driver **p_drv;
455
456 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
457 if (strcmp((*p_drv)->driver_name, periph->periph_name) == 0)
458 break;
459 }
460 if (*p_drv == NULL) {
461 printf("camperiphfree: attempt to free non-existant periph\n");
462 return;
463 }
464
465 if (periph->periph_dtor != NULL)
466 periph->periph_dtor(periph);
467
468 s = splsoftcam();
469 TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
470 (*p_drv)->generation++;
471 splx(s);
472
473 xpt_remove_periph(periph);
474
475 if (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) {
476 union ccb ccb;
477 void *arg;
478
479 switch (periph->deferred_ac) {
480 case AC_FOUND_DEVICE:
481 ccb.ccb_h.func_code = XPT_GDEV_TYPE;
482 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/ 1);
483 xpt_action(&ccb);
484 arg = &ccb;
485 break;
486 case AC_PATH_REGISTERED:
487 ccb.ccb_h.func_code = XPT_PATH_INQ;
488 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/ 1);
489 xpt_action(&ccb);
490 arg = &ccb;
491 break;
492 default:
493 arg = NULL;
494 break;
495 }
496 periph->deferred_callback(NULL, periph->deferred_ac,
497 periph->path, arg);
498 }
499 xpt_free_path(periph->path);
500 free(periph, M_CAMPERIPH);
501 }
502
503 /*
504 * Wait interruptibly for an exclusive lock.
505 */
506 int
507 cam_periph_lock(struct cam_periph *periph, int priority)
508 {
509 int error;
510
511 /*
512 * Increment the reference count on the peripheral
513 * while we wait for our lock attempt to succeed
514 * to ensure the peripheral doesn't disappear out
515 * from under us while we sleep.
516 */
517 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
518 return(ENXIO);
519
520 while ((periph->flags & CAM_PERIPH_LOCKED) != 0) {
521 periph->flags |= CAM_PERIPH_LOCK_WANTED;
522 if ((error = tsleep(periph, priority, "caplck", 0)) != 0) {
523 cam_periph_release(periph);
524 return error;
525 }
526 }
527
528 periph->flags |= CAM_PERIPH_LOCKED;
529 return 0;
530 }
531
532 /*
533 * Unlock and wake up any waiters.
534 */
535 void
536 cam_periph_unlock(struct cam_periph *periph)
537 {
538 periph->flags &= ~CAM_PERIPH_LOCKED;
539 if ((periph->flags & CAM_PERIPH_LOCK_WANTED) != 0) {
540 periph->flags &= ~CAM_PERIPH_LOCK_WANTED;
541 wakeup(periph);
542 }
543
544 cam_periph_release(periph);
545 }
546
547 /*
548 * Map user virtual pointers into kernel virtual address space, so we can
549 * access the memory. This won't work on physical pointers, for now it's
550 * up to the caller to check for that. (XXX KDM -- should we do that here
551 * instead?) This also only works for up to MAXPHYS memory. Since we use
552 * buffers to map stuff in and out, we're limited to the buffer size.
553 */
554 int
555 cam_periph_mapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
556 {
557 int numbufs, i, j;
558 int flags[CAM_PERIPH_MAXMAPS];
559 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
560 u_int32_t lengths[CAM_PERIPH_MAXMAPS];
561 u_int32_t dirs[CAM_PERIPH_MAXMAPS];
562
563 switch(ccb->ccb_h.func_code) {
564 case XPT_DEV_MATCH:
565 if (ccb->cdm.match_buf_len == 0) {
566 printf("cam_periph_mapmem: invalid match buffer "
567 "length 0\n");
568 return(EINVAL);
569 }
570 if (ccb->cdm.pattern_buf_len > 0) {
571 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
572 lengths[0] = ccb->cdm.pattern_buf_len;
573 dirs[0] = CAM_DIR_OUT;
574 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
575 lengths[1] = ccb->cdm.match_buf_len;
576 dirs[1] = CAM_DIR_IN;
577 numbufs = 2;
578 } else {
579 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
580 lengths[0] = ccb->cdm.match_buf_len;
581 dirs[0] = CAM_DIR_IN;
582 numbufs = 1;
583 }
584 break;
585 case XPT_SCSI_IO:
586 case XPT_CONT_TARGET_IO:
587 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
588 return(0);
589
590 data_ptrs[0] = &ccb->csio.data_ptr;
591 lengths[0] = ccb->csio.dxfer_len;
592 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
593 numbufs = 1;
594 break;
595 default:
596 return(EINVAL);
597 break; /* NOTREACHED */
598 }
599
600 /*
601 * Check the transfer length and permissions first, so we don't
602 * have to unmap any previously mapped buffers.
603 */
604 for (i = 0; i < numbufs; i++) {
605
606 flags[i] = 0;
607
608 /*
609 * The userland data pointer passed in may not be page
610 * aligned. vmapbuf() truncates the address to a page
611 * boundary, so if the address isn't page aligned, we'll
612 * need enough space for the given transfer length, plus
613 * whatever extra space is necessary to make it to the page
614 * boundary.
615 */
616 if ((lengths[i] +
617 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)) > DFLTPHYS){
618 printf("cam_periph_mapmem: attempt to map %lu bytes, "
619 "which is greater than DFLTPHYS(%d)\n",
620 (long)(lengths[i] +
621 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)),
622 DFLTPHYS);
623 return(E2BIG);
624 }
625
626 if (dirs[i] & CAM_DIR_OUT) {
627 flags[i] = BIO_WRITE;
628 }
629
630 if (dirs[i] & CAM_DIR_IN) {
631 flags[i] = BIO_READ;
632 }
633
634 }
635
636 /* this keeps the current process from getting swapped */
637 /*
638 * XXX KDM should I use P_NOSWAP instead?
639 */
640 PHOLD(curproc);
641
642 for (i = 0; i < numbufs; i++) {
643 /*
644 * Get the buffer.
645 */
646 mapinfo->bp[i] = getpbuf(NULL);
647
648 /* save the buffer's data address */
649 mapinfo->bp[i]->b_saveaddr = mapinfo->bp[i]->b_data;
650
651 /* put our pointer in the data slot */
652 mapinfo->bp[i]->b_data = *data_ptrs[i];
653
654 /* set the transfer length, we know it's < DFLTPHYS */
655 mapinfo->bp[i]->b_bufsize = lengths[i];
656
657 /* set the direction */
658 mapinfo->bp[i]->b_iocmd = flags[i];
659
660 /*
661 * Map the buffer into kernel memory.
662 *
663 * Note that useracc() alone is not a sufficient test.
664 * vmapbuf() can still fail due to a smaller file mapped
665 * into a larger area of VM, or if userland races against
666 * vmapbuf() after the useracc() check.
667 */
668 if (vmapbuf(mapinfo->bp[i]) < 0) {
669 for (j = 0; j < i; ++j) {
670 *data_ptrs[j] = mapinfo->bp[j]->b_saveaddr;
671 vunmapbuf(mapinfo->bp[j]);
672 relpbuf(mapinfo->bp[j], NULL);
673 }
674 relpbuf(mapinfo->bp[i], NULL);
675 PRELE(curproc);
676 return(EACCES);
677 }
678
679 /* set our pointer to the new mapped area */
680 *data_ptrs[i] = mapinfo->bp[i]->b_data;
681
682 mapinfo->num_bufs_used++;
683 }
684
685 return(0);
686 }
687
688 /*
689 * Unmap memory segments mapped into kernel virtual address space by
690 * cam_periph_mapmem().
691 */
692 void
693 cam_periph_unmapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
694 {
695 int numbufs, i;
696 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
697
698 if (mapinfo->num_bufs_used <= 0) {
699 /* allow ourselves to be swapped once again */
700 PRELE(curproc);
701 return;
702 }
703
704 switch (ccb->ccb_h.func_code) {
705 case XPT_DEV_MATCH:
706 numbufs = min(mapinfo->num_bufs_used, 2);
707
708 if (numbufs == 1) {
709 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
710 } else {
711 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
712 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
713 }
714 break;
715 case XPT_SCSI_IO:
716 case XPT_CONT_TARGET_IO:
717 data_ptrs[0] = &ccb->csio.data_ptr;
718 numbufs = min(mapinfo->num_bufs_used, 1);
719 break;
720 default:
721 /* allow ourselves to be swapped once again */
722 PRELE(curproc);
723 return;
724 break; /* NOTREACHED */
725 }
726
727 for (i = 0; i < numbufs; i++) {
728 /* Set the user's pointer back to the original value */
729 *data_ptrs[i] = mapinfo->bp[i]->b_saveaddr;
730
731 /* unmap the buffer */
732 vunmapbuf(mapinfo->bp[i]);
733
734 /* release the buffer */
735 relpbuf(mapinfo->bp[i], NULL);
736 }
737
738 /* allow ourselves to be swapped once again */
739 PRELE(curproc);
740 }
741
742 union ccb *
743 cam_periph_getccb(struct cam_periph *periph, u_int32_t priority)
744 {
745 struct ccb_hdr *ccb_h;
746 int s;
747
748 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdgetccb\n"));
749
750 s = splsoftcam();
751
752 while (SLIST_FIRST(&periph->ccb_list) == NULL) {
753 if (periph->immediate_priority > priority)
754 periph->immediate_priority = priority;
755 xpt_schedule(periph, priority);
756 if ((SLIST_FIRST(&periph->ccb_list) != NULL)
757 && (SLIST_FIRST(&periph->ccb_list)->pinfo.priority == priority))
758 break;
759 tsleep(&periph->ccb_list, PRIBIO, "cgticb", 0);
760 }
761
762 ccb_h = SLIST_FIRST(&periph->ccb_list);
763 SLIST_REMOVE_HEAD(&periph->ccb_list, periph_links.sle);
764 splx(s);
765 return ((union ccb *)ccb_h);
766 }
767
768 void
769 cam_periph_ccbwait(union ccb *ccb)
770 {
771 int s;
772
773 s = splsoftcam();
774 if ((ccb->ccb_h.pinfo.index != CAM_UNQUEUED_INDEX)
775 || ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG))
776 tsleep(&ccb->ccb_h.cbfcnp, PRIBIO, "cbwait", 0);
777
778 splx(s);
779 }
780
781 int
782 cam_periph_ioctl(struct cam_periph *periph, int cmd, caddr_t addr,
783 int (*error_routine)(union ccb *ccb,
784 cam_flags camflags,
785 u_int32_t sense_flags))
786 {
787 union ccb *ccb;
788 int error;
789 int found;
790
791 error = found = 0;
792
793 switch(cmd){
794 case CAMGETPASSTHRU:
795 ccb = cam_periph_getccb(periph, /* priority */ 1);
796 xpt_setup_ccb(&ccb->ccb_h,
797 ccb->ccb_h.path,
798 /*priority*/1);
799 ccb->ccb_h.func_code = XPT_GDEVLIST;
800
801 /*
802 * Basically, the point of this is that we go through
803 * getting the list of devices, until we find a passthrough
804 * device. In the current version of the CAM code, the
805 * only way to determine what type of device we're dealing
806 * with is by its name.
807 */
808 while (found == 0) {
809 ccb->cgdl.index = 0;
810 ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS;
811 while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) {
812
813 /* we want the next device in the list */
814 xpt_action(ccb);
815 if (strncmp(ccb->cgdl.periph_name,
816 "pass", 4) == 0){
817 found = 1;
818 break;
819 }
820 }
821 if ((ccb->cgdl.status == CAM_GDEVLIST_LAST_DEVICE) &&
822 (found == 0)) {
823 ccb->cgdl.periph_name[0] = '\0';
824 ccb->cgdl.unit_number = 0;
825 break;
826 }
827 }
828
829 /* copy the result back out */
830 bcopy(ccb, addr, sizeof(union ccb));
831
832 /* and release the ccb */
833 xpt_release_ccb(ccb);
834
835 break;
836 default:
837 error = ENOTTY;
838 break;
839 }
840 return(error);
841 }
842
843 int
844 cam_periph_runccb(union ccb *ccb,
845 int (*error_routine)(union ccb *ccb,
846 cam_flags camflags,
847 u_int32_t sense_flags),
848 cam_flags camflags, u_int32_t sense_flags,
849 struct devstat *ds)
850 {
851 int error;
852
853 error = 0;
854
855 /*
856 * If the user has supplied a stats structure, and if we understand
857 * this particular type of ccb, record the transaction start.
858 */
859 if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO))
860 devstat_start_transaction(ds, NULL);
861
862 xpt_action(ccb);
863
864 do {
865 cam_periph_ccbwait(ccb);
866 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
867 error = 0;
868 else if (error_routine != NULL)
869 error = (*error_routine)(ccb, camflags, sense_flags);
870 else
871 error = 0;
872
873 } while (error == ERESTART);
874
875 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
876 cam_release_devq(ccb->ccb_h.path,
877 /* relsim_flags */0,
878 /* openings */0,
879 /* timeout */0,
880 /* getcount_only */ FALSE);
881
882 if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO))
883 devstat_end_transaction(ds,
884 ccb->csio.dxfer_len,
885 ccb->csio.tag_action & 0xf,
886 ((ccb->ccb_h.flags & CAM_DIR_MASK) ==
887 CAM_DIR_NONE) ? DEVSTAT_NO_DATA :
888 (ccb->ccb_h.flags & CAM_DIR_OUT) ?
889 DEVSTAT_WRITE :
890 DEVSTAT_READ, NULL, NULL);
891
892 return(error);
893 }
894
895 void
896 cam_freeze_devq(struct cam_path *path)
897 {
898 struct ccb_hdr ccb_h;
899
900 xpt_setup_ccb(&ccb_h, path, /*priority*/1);
901 ccb_h.func_code = XPT_NOOP;
902 ccb_h.flags = CAM_DEV_QFREEZE;
903 xpt_action((union ccb *)&ccb_h);
904 }
905
906 u_int32_t
907 cam_release_devq(struct cam_path *path, u_int32_t relsim_flags,
908 u_int32_t openings, u_int32_t timeout,
909 int getcount_only)
910 {
911 struct ccb_relsim crs;
912
913 xpt_setup_ccb(&crs.ccb_h, path,
914 /*priority*/1);
915 crs.ccb_h.func_code = XPT_REL_SIMQ;
916 crs.ccb_h.flags = getcount_only ? CAM_DEV_QFREEZE : 0;
917 crs.release_flags = relsim_flags;
918 crs.openings = openings;
919 crs.release_timeout = timeout;
920 xpt_action((union ccb *)&crs);
921 return (crs.qfrozen_cnt);
922 }
923
924 #define saved_ccb_ptr ppriv_ptr0
925 static void
926 camperiphdone(struct cam_periph *periph, union ccb *done_ccb)
927 {
928 union ccb *saved_ccb;
929 cam_status status;
930 int frozen;
931 int sense;
932 struct scsi_start_stop_unit *scsi_cmd;
933 u_int32_t relsim_flags, timeout;
934 u_int32_t qfrozen_cnt;
935 int xpt_done_ccb;
936
937 xpt_done_ccb = FALSE;
938 status = done_ccb->ccb_h.status;
939 frozen = (status & CAM_DEV_QFRZN) != 0;
940 sense = (status & CAM_AUTOSNS_VALID) != 0;
941 status &= CAM_STATUS_MASK;
942
943 timeout = 0;
944 relsim_flags = 0;
945 saved_ccb = (union ccb *)done_ccb->ccb_h.saved_ccb_ptr;
946
947 /*
948 * Unfreeze the queue once if it is already frozen..
949 */
950 if (frozen != 0) {
951 qfrozen_cnt = cam_release_devq(done_ccb->ccb_h.path,
952 /*relsim_flags*/0,
953 /*openings*/0,
954 /*timeout*/0,
955 /*getcount_only*/0);
956 }
957
958 switch (status) {
959 case CAM_REQ_CMP:
960 {
961 /*
962 * If we have successfully taken a device from the not
963 * ready to ready state, re-scan the device and re-get
964 * the inquiry information. Many devices (mostly disks)
965 * don't properly report their inquiry information unless
966 * they are spun up.
967 *
968 * If we manually retrieved sense into a CCB and got
969 * something other than "NO SENSE" send the updated CCB
970 * back to the client via xpt_done() to be processed via
971 * the error recovery code again.
972 */
973 if (done_ccb->ccb_h.func_code == XPT_SCSI_IO) {
974 scsi_cmd = (struct scsi_start_stop_unit *)
975 &done_ccb->csio.cdb_io.cdb_bytes;
976
977 if (scsi_cmd->opcode == START_STOP_UNIT)
978 xpt_async(AC_INQ_CHANGED,
979 done_ccb->ccb_h.path, NULL);
980 if (scsi_cmd->opcode == REQUEST_SENSE) {
981 u_int sense_key;
982
983 sense_key = saved_ccb->csio.sense_data.flags;
984 sense_key &= SSD_KEY;
985 if (sense_key != SSD_KEY_NO_SENSE) {
986 saved_ccb->ccb_h.status |=
987 CAM_AUTOSNS_VALID;
988 #if 0
989 xpt_print_path(saved_ccb->ccb_h.path);
990 printf("Recovered Sense\n");
991 scsi_sense_print(&saved_ccb->csio);
992 cam_error_print(saved_ccb, CAM_ESF_ALL,
993 CAM_EPF_ALL);
994 #endif
995 xpt_done_ccb = TRUE;
996 }
997 }
998 }
999 bcopy(done_ccb->ccb_h.saved_ccb_ptr, done_ccb,
1000 sizeof(union ccb));
1001
1002 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1003
1004 if (xpt_done_ccb == FALSE)
1005 xpt_action(done_ccb);
1006
1007 break;
1008 }
1009 case CAM_SCSI_STATUS_ERROR:
1010 scsi_cmd = (struct scsi_start_stop_unit *)
1011 &done_ccb->csio.cdb_io.cdb_bytes;
1012 if (sense != 0) {
1013 struct ccb_getdev cgd;
1014 struct scsi_sense_data *sense;
1015 int error_code, sense_key, asc, ascq;
1016 scsi_sense_action err_action;
1017
1018 sense = &done_ccb->csio.sense_data;
1019 scsi_extract_sense(sense, &error_code,
1020 &sense_key, &asc, &ascq);
1021
1022 /*
1023 * Grab the inquiry data for this device.
1024 */
1025 xpt_setup_ccb(&cgd.ccb_h, done_ccb->ccb_h.path,
1026 /*priority*/ 1);
1027 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1028 xpt_action((union ccb *)&cgd);
1029 err_action = scsi_error_action(&done_ccb->csio,
1030 &cgd.inq_data, 0);
1031
1032 /*
1033 * If the error is "invalid field in CDB",
1034 * and the load/eject flag is set, turn the
1035 * flag off and try again. This is just in
1036 * case the drive in question barfs on the
1037 * load eject flag. The CAM code should set
1038 * the load/eject flag by default for
1039 * removable media.
1040 */
1041
1042 /* XXX KDM
1043 * Should we check to see what the specific
1044 * scsi status is?? Or does it not matter
1045 * since we already know that there was an
1046 * error, and we know what the specific
1047 * error code was, and we know what the
1048 * opcode is..
1049 */
1050 if ((scsi_cmd->opcode == START_STOP_UNIT) &&
1051 ((scsi_cmd->how & SSS_LOEJ) != 0) &&
1052 (asc == 0x24) && (ascq == 0x00) &&
1053 (done_ccb->ccb_h.retry_count > 0)) {
1054
1055 scsi_cmd->how &= ~SSS_LOEJ;
1056
1057 xpt_action(done_ccb);
1058
1059 } else if ((done_ccb->ccb_h.retry_count > 1)
1060 && ((err_action & SS_MASK) != SS_FAIL)) {
1061
1062 /*
1063 * In this case, the error recovery
1064 * command failed, but we've got
1065 * some retries left on it. Give
1066 * it another try unless this is an
1067 * unretryable error.
1068 */
1069
1070 /* set the timeout to .5 sec */
1071 relsim_flags =
1072 RELSIM_RELEASE_AFTER_TIMEOUT;
1073 timeout = 500;
1074
1075 xpt_action(done_ccb);
1076
1077 break;
1078
1079 } else {
1080 /*
1081 * Perform the final retry with the original
1082 * CCB so that final error processing is
1083 * performed by the owner of the CCB.
1084 */
1085 bcopy(done_ccb->ccb_h.saved_ccb_ptr,
1086 done_ccb, sizeof(union ccb));
1087
1088 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1089
1090 xpt_action(done_ccb);
1091 }
1092 } else {
1093 /*
1094 * Eh?? The command failed, but we don't
1095 * have any sense. What's up with that?
1096 * Fire the CCB again to return it to the
1097 * caller.
1098 */
1099 bcopy(done_ccb->ccb_h.saved_ccb_ptr,
1100 done_ccb, sizeof(union ccb));
1101
1102 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1103
1104 xpt_action(done_ccb);
1105
1106 }
1107 break;
1108 default:
1109 bcopy(done_ccb->ccb_h.saved_ccb_ptr, done_ccb,
1110 sizeof(union ccb));
1111
1112 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1113
1114 xpt_action(done_ccb);
1115
1116 break;
1117 }
1118
1119 /* decrement the retry count */
1120 /*
1121 * XXX This isn't appropriate in all cases. Restructure,
1122 * so that the retry count is only decremented on an
1123 * actual retry. Remeber that the orignal ccb had its
1124 * retry count dropped before entering recovery, so
1125 * doing it again is a bug.
1126 */
1127 if (done_ccb->ccb_h.retry_count > 0)
1128 done_ccb->ccb_h.retry_count--;
1129
1130 qfrozen_cnt = cam_release_devq(done_ccb->ccb_h.path,
1131 /*relsim_flags*/relsim_flags,
1132 /*openings*/0,
1133 /*timeout*/timeout,
1134 /*getcount_only*/0);
1135 if (xpt_done_ccb == TRUE)
1136 (*done_ccb->ccb_h.cbfcnp)(periph, done_ccb);
1137 }
1138
1139 /*
1140 * Generic Async Event handler. Peripheral drivers usually
1141 * filter out the events that require personal attention,
1142 * and leave the rest to this function.
1143 */
1144 void
1145 cam_periph_async(struct cam_periph *periph, u_int32_t code,
1146 struct cam_path *path, void *arg)
1147 {
1148 switch (code) {
1149 case AC_LOST_DEVICE:
1150 cam_periph_invalidate(periph);
1151 break;
1152 case AC_SENT_BDR:
1153 case AC_BUS_RESET:
1154 {
1155 cam_periph_bus_settle(periph, scsi_delay);
1156 break;
1157 }
1158 default:
1159 break;
1160 }
1161 }
1162
1163 void
1164 cam_periph_bus_settle(struct cam_periph *periph, u_int bus_settle)
1165 {
1166 struct ccb_getdevstats cgds;
1167
1168 xpt_setup_ccb(&cgds.ccb_h, periph->path, /*priority*/1);
1169 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1170 xpt_action((union ccb *)&cgds);
1171 cam_periph_freeze_after_event(periph, &cgds.last_reset, bus_settle);
1172 }
1173
1174 void
1175 cam_periph_freeze_after_event(struct cam_periph *periph,
1176 struct timeval* event_time, u_int duration_ms)
1177 {
1178 struct timeval delta;
1179 struct timeval duration_tv;
1180 int s;
1181
1182 s = splclock();
1183 microtime(&delta);
1184 splx(s);
1185 timevalsub(&delta, event_time);
1186 duration_tv.tv_sec = duration_ms / 1000;
1187 duration_tv.tv_usec = (duration_ms % 1000) * 1000;
1188 if (timevalcmp(&delta, &duration_tv, <)) {
1189 timevalsub(&duration_tv, &delta);
1190
1191 duration_ms = duration_tv.tv_sec * 1000;
1192 duration_ms += duration_tv.tv_usec / 1000;
1193 cam_freeze_devq(periph->path);
1194 cam_release_devq(periph->path,
1195 RELSIM_RELEASE_AFTER_TIMEOUT,
1196 /*reduction*/0,
1197 /*timeout*/duration_ms,
1198 /*getcount_only*/0);
1199 }
1200
1201 }
1202
1203 static int
1204 camperiphscsistatuserror(union ccb *ccb, cam_flags camflags,
1205 u_int32_t sense_flags, union ccb *save_ccb,
1206 int *openings, u_int32_t *relsim_flags,
1207 u_int32_t *timeout)
1208 {
1209 int error;
1210
1211 switch (ccb->csio.scsi_status) {
1212 case SCSI_STATUS_OK:
1213 case SCSI_STATUS_COND_MET:
1214 case SCSI_STATUS_INTERMED:
1215 case SCSI_STATUS_INTERMED_COND_MET:
1216 error = 0;
1217 break;
1218 case SCSI_STATUS_CMD_TERMINATED:
1219 case SCSI_STATUS_CHECK_COND:
1220 error = camperiphscsisenseerror(ccb,
1221 camflags,
1222 sense_flags,
1223 save_ccb,
1224 openings,
1225 relsim_flags,
1226 timeout);
1227 break;
1228 case SCSI_STATUS_QUEUE_FULL:
1229 {
1230 /* no decrement */
1231 struct ccb_getdevstats cgds;
1232
1233 /*
1234 * First off, find out what the current
1235 * transaction counts are.
1236 */
1237 xpt_setup_ccb(&cgds.ccb_h,
1238 ccb->ccb_h.path,
1239 /*priority*/1);
1240 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1241 xpt_action((union ccb *)&cgds);
1242
1243 /*
1244 * If we were the only transaction active, treat
1245 * the QUEUE FULL as if it were a BUSY condition.
1246 */
1247 if (cgds.dev_active != 0) {
1248 int total_openings;
1249
1250 /*
1251 * Reduce the number of openings to
1252 * be 1 less than the amount it took
1253 * to get a queue full bounded by the
1254 * minimum allowed tag count for this
1255 * device.
1256 */
1257 total_openings = cgds.dev_active + cgds.dev_openings;
1258 *openings = cgds.dev_active;
1259 if (*openings < cgds.mintags)
1260 *openings = cgds.mintags;
1261 if (*openings < total_openings)
1262 *relsim_flags = RELSIM_ADJUST_OPENINGS;
1263 else {
1264 /*
1265 * Some devices report queue full for
1266 * temporary resource shortages. For
1267 * this reason, we allow a minimum
1268 * tag count to be entered via a
1269 * quirk entry to prevent the queue
1270 * count on these devices from falling
1271 * to a pessimisticly low value. We
1272 * still wait for the next successful
1273 * completion, however, before queueing
1274 * more transactions to the device.
1275 */
1276 *relsim_flags = RELSIM_RELEASE_AFTER_CMDCMPLT;
1277 }
1278 *timeout = 0;
1279 error = ERESTART;
1280 if (bootverbose) {
1281 xpt_print_path(ccb->ccb_h.path);
1282 printf("Queue Full\n");
1283 }
1284 break;
1285 }
1286 /* FALLTHROUGH */
1287 }
1288 case SCSI_STATUS_BUSY:
1289 /*
1290 * Restart the queue after either another
1291 * command completes or a 1 second timeout.
1292 */
1293 if (bootverbose) {
1294 xpt_print_path(ccb->ccb_h.path);
1295 printf("Device Busy\n");
1296 }
1297 if (ccb->ccb_h.retry_count > 0) {
1298 ccb->ccb_h.retry_count--;
1299 error = ERESTART;
1300 *relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT
1301 | RELSIM_RELEASE_AFTER_CMDCMPLT;
1302 *timeout = 1000;
1303 } else {
1304 error = EIO;
1305 }
1306 break;
1307 case SCSI_STATUS_RESERV_CONFLICT:
1308 xpt_print_path(ccb->ccb_h.path);
1309 printf("Reservation Conflict\n");
1310 error = EIO;
1311 break;
1312 default:
1313 xpt_print_path(ccb->ccb_h.path);
1314 printf("SCSI Status 0x%x\n", ccb->csio.scsi_status);
1315 error = EIO;
1316 break;
1317 }
1318 return (error);
1319 }
1320
1321 static int
1322 camperiphscsisenseerror(union ccb *ccb, cam_flags camflags,
1323 u_int32_t sense_flags, union ccb *save_ccb,
1324 int *openings, u_int32_t *relsim_flags,
1325 u_int32_t *timeout)
1326 {
1327 struct cam_periph *periph;
1328 int error;
1329
1330 periph = xpt_path_periph(ccb->ccb_h.path);
1331 if (periph->flags & CAM_PERIPH_RECOVERY_INPROG) {
1332
1333 /*
1334 * If error recovery is already in progress, don't attempt
1335 * to process this error, but requeue it unconditionally
1336 * and attempt to process it once error recovery has
1337 * completed. This failed command is probably related to
1338 * the error that caused the currently active error recovery
1339 * action so our current recovery efforts should also
1340 * address this command. Be aware that the error recovery
1341 * code assumes that only one recovery action is in progress
1342 * on a particular peripheral instance at any given time
1343 * (e.g. only one saved CCB for error recovery) so it is
1344 * imperitive that we don't violate this assumption.
1345 */
1346 error = ERESTART;
1347 } else {
1348 scsi_sense_action err_action;
1349 struct ccb_getdev cgd;
1350 const char *action_string;
1351 union ccb* print_ccb;
1352
1353 /* A description of the error recovery action performed */
1354 action_string = NULL;
1355
1356 /*
1357 * The location of the orignal ccb
1358 * for sense printing purposes.
1359 */
1360 print_ccb = ccb;
1361
1362 /*
1363 * Grab the inquiry data for this device.
1364 */
1365 xpt_setup_ccb(&cgd.ccb_h, ccb->ccb_h.path, /*priority*/ 1);
1366 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1367 xpt_action((union ccb *)&cgd);
1368
1369 if ((ccb->ccb_h.status & CAM_AUTOSNS_VALID) != 0)
1370 err_action = scsi_error_action(&ccb->csio,
1371 &cgd.inq_data,
1372 sense_flags);
1373 else if ((ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)
1374 err_action = SS_REQSENSE;
1375 else
1376 err_action = SS_RETRY|SSQ_DECREMENT_COUNT|EIO;
1377
1378 error = err_action & SS_ERRMASK;
1379
1380 /*
1381 * If the recovery action will consume a retry,
1382 * make sure we actually have retries available.
1383 */
1384 if ((err_action & SSQ_DECREMENT_COUNT) != 0) {
1385 if (ccb->ccb_h.retry_count > 0)
1386 ccb->ccb_h.retry_count--;
1387 else {
1388 action_string = "Retries Exhausted";
1389 goto sense_error_done;
1390 }
1391 }
1392
1393 if ((err_action & SS_MASK) >= SS_START) {
1394 /*
1395 * Do common portions of commands that
1396 * use recovery CCBs.
1397 */
1398 if (save_ccb == NULL) {
1399 action_string = "No recovery CCB supplied";
1400 goto sense_error_done;
1401 }
1402 bcopy(ccb, save_ccb, sizeof(*save_ccb));
1403 print_ccb = save_ccb;
1404 periph->flags |= CAM_PERIPH_RECOVERY_INPROG;
1405 }
1406
1407 switch (err_action & SS_MASK) {
1408 case SS_NOP:
1409 action_string = "No Recovery Action Needed";
1410 error = 0;
1411 break;
1412 case SS_RETRY:
1413 action_string = "Retrying Command (per Sense Data)";
1414 error = ERESTART;
1415 break;
1416 case SS_FAIL:
1417 action_string = "Unretryable error";
1418 break;
1419 case SS_START:
1420 {
1421 int le;
1422
1423 /*
1424 * Send a start unit command to the device, and
1425 * then retry the command.
1426 */
1427 action_string = "Attempting to Start Unit";
1428
1429 /*
1430 * Check for removable media and set
1431 * load/eject flag appropriately.
1432 */
1433 if (SID_IS_REMOVABLE(&cgd.inq_data))
1434 le = TRUE;
1435 else
1436 le = FALSE;
1437
1438 scsi_start_stop(&ccb->csio,
1439 /*retries*/1,
1440 camperiphdone,
1441 MSG_SIMPLE_Q_TAG,
1442 /*start*/TRUE,
1443 /*load/eject*/le,
1444 /*immediate*/FALSE,
1445 SSD_FULL_SIZE,
1446 /*timeout*/50000);
1447 break;
1448 }
1449 case SS_TUR:
1450 {
1451 /*
1452 * Send a Test Unit Ready to the device.
1453 * If the 'many' flag is set, we send 120
1454 * test unit ready commands, one every half
1455 * second. Otherwise, we just send one TUR.
1456 * We only want to do this if the retry
1457 * count has not been exhausted.
1458 */
1459 int retries;
1460
1461 if ((err_action & SSQ_MANY) != 0) {
1462 action_string = "Polling device for readiness";
1463 retries = 120;
1464 } else {
1465 action_string = "Testing device for readiness";
1466 retries = 1;
1467 }
1468 scsi_test_unit_ready(&ccb->csio,
1469 retries,
1470 camperiphdone,
1471 MSG_SIMPLE_Q_TAG,
1472 SSD_FULL_SIZE,
1473 /*timeout*/5000);
1474
1475 /*
1476 * Accomplish our 500ms delay by deferring
1477 * the release of our device queue appropriately.
1478 */
1479 *relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1480 *timeout = 500;
1481 break;
1482 }
1483 case SS_REQSENSE:
1484 {
1485 /*
1486 * Send a Request Sense to the device. We
1487 * assume that we are in a contingent allegiance
1488 * condition so we do not tag this request.
1489 */
1490 scsi_request_sense(&ccb->csio, /*retries*/1,
1491 camperiphdone,
1492 &save_ccb->csio.sense_data,
1493 sizeof(save_ccb->csio.sense_data),
1494 CAM_TAG_ACTION_NONE,
1495 /*sense_len*/SSD_FULL_SIZE,
1496 /*timeout*/5000);
1497 break;
1498 }
1499 default:
1500 panic("Unhandled error action %x", err_action);
1501 }
1502
1503 if ((err_action & SS_MASK) >= SS_START) {
1504 /*
1505 * Drop the priority to 0 so that the recovery
1506 * CCB is the first to execute. Freeze the queue
1507 * after this command is sent so that we can
1508 * restore the old csio and have it queued in
1509 * the proper order before we release normal
1510 * transactions to the device.
1511 */
1512 ccb->ccb_h.pinfo.priority = 0;
1513 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1514 ccb->ccb_h.saved_ccb_ptr = save_ccb;
1515 error = ERESTART;
1516 }
1517
1518 sense_error_done:
1519 if ((err_action & SSQ_PRINT_SENSE) != 0
1520 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID) != 0) {
1521 cam_error_print(print_ccb, CAM_ESF_ALL, CAM_EPF_ALL);
1522 xpt_print_path(ccb->ccb_h.path);
1523 if (bootverbose)
1524 scsi_sense_print(&print_ccb->csio);
1525 printf("%s\n", action_string);
1526 }
1527 }
1528 return (error);
1529 }
1530
1531 /*
1532 * Generic error handler. Peripheral drivers usually filter
1533 * out the errors that they handle in a unique mannor, then
1534 * call this function.
1535 */
1536 int
1537 cam_periph_error(union ccb *ccb, cam_flags camflags,
1538 u_int32_t sense_flags, union ccb *save_ccb)
1539 {
1540 const char *action_string;
1541 cam_status status;
1542 int frozen;
1543 int error, printed = 0;
1544 int openings;
1545 u_int32_t relsim_flags;
1546 u_int32_t timeout = 0;
1547
1548 action_string = NULL;
1549 status = ccb->ccb_h.status;
1550 frozen = (status & CAM_DEV_QFRZN) != 0;
1551 status &= CAM_STATUS_MASK;
1552 openings = relsim_flags = 0;
1553
1554 switch (status) {
1555 case CAM_REQ_CMP:
1556 error = 0;
1557 break;
1558 case CAM_SCSI_STATUS_ERROR:
1559 error = camperiphscsistatuserror(ccb,
1560 camflags,
1561 sense_flags,
1562 save_ccb,
1563 &openings,
1564 &relsim_flags,
1565 &timeout);
1566 break;
1567 case CAM_AUTOSENSE_FAIL:
1568 xpt_print_path(ccb->ccb_h.path);
1569 printf("AutoSense Failed\n");
1570 error = EIO; /* we have to kill the command */
1571 break;
1572 case CAM_REQ_CMP_ERR:
1573 if (bootverbose && printed == 0) {
1574 xpt_print_path(ccb->ccb_h.path);
1575 printf("Request completed with CAM_REQ_CMP_ERR\n");
1576 printed++;
1577 }
1578 /* FALLTHROUGH */
1579 case CAM_CMD_TIMEOUT:
1580 if (bootverbose && printed == 0) {
1581 xpt_print_path(ccb->ccb_h.path);
1582 printf("Command timed out\n");
1583 printed++;
1584 }
1585 /* FALLTHROUGH */
1586 case CAM_UNEXP_BUSFREE:
1587 if (bootverbose && printed == 0) {
1588 xpt_print_path(ccb->ccb_h.path);
1589 printf("Unexpected Bus Free\n");
1590 printed++;
1591 }
1592 /* FALLTHROUGH */
1593 case CAM_UNCOR_PARITY:
1594 if (bootverbose && printed == 0) {
1595 xpt_print_path(ccb->ccb_h.path);
1596 printf("Uncorrected Parity Error\n");
1597 printed++;
1598 }
1599 /* FALLTHROUGH */
1600 case CAM_DATA_RUN_ERR:
1601 if (bootverbose && printed == 0) {
1602 xpt_print_path(ccb->ccb_h.path);
1603 printf("Data Overrun\n");
1604 printed++;
1605 }
1606 error = EIO; /* we have to kill the command */
1607 /* decrement the number of retries */
1608 if (ccb->ccb_h.retry_count > 0) {
1609 ccb->ccb_h.retry_count--;
1610 error = ERESTART;
1611 } else {
1612 action_string = "Retries Exausted";
1613 error = EIO;
1614 }
1615 break;
1616 case CAM_UA_ABORT:
1617 case CAM_UA_TERMIO:
1618 case CAM_MSG_REJECT_REC:
1619 /* XXX Don't know that these are correct */
1620 error = EIO;
1621 break;
1622 case CAM_SEL_TIMEOUT:
1623 {
1624 struct cam_path *newpath;
1625
1626 if ((camflags & CAM_RETRY_SELTO) != 0) {
1627 if (ccb->ccb_h.retry_count > 0) {
1628
1629 ccb->ccb_h.retry_count--;
1630 error = ERESTART;
1631 if (bootverbose && printed == 0) {
1632 xpt_print_path(ccb->ccb_h.path);
1633 printf("Selection Timeout\n");
1634 printed++;
1635 }
1636
1637 /*
1638 * Wait a bit to give the device
1639 * time to recover before we try again.
1640 */
1641 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1642 timeout = periph_selto_delay;
1643 break;
1644 }
1645 }
1646 error = ENXIO;
1647 /* Should we do more if we can't create the path?? */
1648 if (xpt_create_path(&newpath, xpt_path_periph(ccb->ccb_h.path),
1649 xpt_path_path_id(ccb->ccb_h.path),
1650 xpt_path_target_id(ccb->ccb_h.path),
1651 CAM_LUN_WILDCARD) != CAM_REQ_CMP)
1652 break;
1653
1654 /*
1655 * Let peripheral drivers know that this device has gone
1656 * away.
1657 */
1658 xpt_async(AC_LOST_DEVICE, newpath, NULL);
1659 xpt_free_path(newpath);
1660 break;
1661 }
1662 case CAM_REQ_INVALID:
1663 case CAM_PATH_INVALID:
1664 case CAM_DEV_NOT_THERE:
1665 case CAM_NO_HBA:
1666 case CAM_PROVIDE_FAIL:
1667 case CAM_REQ_TOO_BIG:
1668 case CAM_LUN_INVALID:
1669 case CAM_TID_INVALID:
1670 error = EINVAL;
1671 break;
1672 case CAM_SCSI_BUS_RESET:
1673 case CAM_BDR_SENT:
1674 /*
1675 * Commands that repeatedly timeout and cause these
1676 * kinds of error recovery actions, should return
1677 * CAM_CMD_TIMEOUT, which allows us to safely assume
1678 * that this command was an innocent bystander to
1679 * these events and should be unconditionally
1680 * retried.
1681 */
1682 if (bootverbose && printed == 0) {
1683 xpt_print_path(ccb->ccb_h.path);
1684 if (status == CAM_BDR_SENT)
1685 printf("Bus Device Reset sent\n");
1686 else
1687 printf("Bus Reset issued\n");
1688 printed++;
1689 }
1690 /* FALLTHROUGH */
1691 case CAM_REQUEUE_REQ:
1692 /* Unconditional requeue */
1693 error = ERESTART;
1694 if (bootverbose && printed == 0) {
1695 xpt_print_path(ccb->ccb_h.path);
1696 printf("Request Requeued\n");
1697 printed++;
1698 }
1699 break;
1700 case CAM_RESRC_UNAVAIL:
1701 /* Wait a bit for the resource shortage to abate. */
1702 timeout = periph_noresrc_delay;
1703 /* FALLTHROUGH */
1704 case CAM_BUSY:
1705 if (timeout == 0) {
1706 /* Wait a bit for the busy condition to abate. */
1707 timeout = periph_busy_delay;
1708 }
1709 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1710 /* FALLTHROUGH */
1711 default:
1712 /* decrement the number of retries */
1713 if (ccb->ccb_h.retry_count > 0) {
1714 ccb->ccb_h.retry_count--;
1715 error = ERESTART;
1716 if (bootverbose && printed == 0) {
1717 xpt_print_path(ccb->ccb_h.path);
1718 printf("CAM Status 0x%x\n", status);
1719 printed++;
1720 }
1721 } else {
1722 error = EIO;
1723 action_string = "Retries Exhausted";
1724 }
1725 break;
1726 }
1727
1728 /* Attempt a retry */
1729 if (error == ERESTART || error == 0) {
1730 if (frozen != 0)
1731 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1732
1733 if (error == ERESTART) {
1734 action_string = "Retrying Command";
1735 xpt_action(ccb);
1736 }
1737
1738 if (frozen != 0)
1739 cam_release_devq(ccb->ccb_h.path,
1740 relsim_flags,
1741 openings,
1742 timeout,
1743 /*getcount_only*/0);
1744 }
1745
1746 /*
1747 * If we have and error and are booting verbosely, whine
1748 * *unless* this was a non-retryable selection timeout.
1749 */
1750 if (error != 0 && bootverbose &&
1751 !(status == CAM_SEL_TIMEOUT && (camflags & CAM_RETRY_SELTO) == 0)) {
1752
1753
1754 if (action_string == NULL)
1755 action_string = "Unretryable Error";
1756 if (error != ERESTART) {
1757 xpt_print_path(ccb->ccb_h.path);
1758 printf("error %d\n", error);
1759 }
1760 xpt_print_path(ccb->ccb_h.path);
1761 printf("%s\n", action_string);
1762 }
1763
1764 return (error);
1765 }
Cache object: 61537ecdac4c241fc162c9e3f9731513
|