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