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