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