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