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