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