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