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