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