]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - sys/cam/scsi/scsi_ch.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / sys / cam / scsi / scsi_ch.c
1 /*-
2  * Copyright (c) 1997 Justin T. Gibbs.
3  * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions, and the following disclaimer,
11  *    without modification, immediately at the beginning of the file.
12  * 2. The name of the author may not be used to endorse or promote products
13  *    derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27
28 /*
29  * Derived from the NetBSD SCSI changer driver.
30  *
31  *      $NetBSD: ch.c,v 1.32 1998/01/12 09:49:12 thorpej Exp $
32  *
33  */
34 /*-
35  * Copyright (c) 1996, 1997 Jason R. Thorpe <thorpej@and.com>
36  * All rights reserved.
37  *
38  * Partially based on an autochanger driver written by Stefan Grefen
39  * and on an autochanger driver written by the Systems Programming Group
40  * at the University of Utah Computer Science Department.
41  *
42  * Redistribution and use in source and binary forms, with or without
43  * modification, are permitted provided that the following conditions
44  * are met:
45  * 1. Redistributions of source code must retain the above copyright
46  *    notice, this list of conditions and the following disclaimer.
47  * 2. Redistributions in binary form must reproduce the above copyright
48  *    notice, this list of conditions and the following disclaimer in the
49  *    documentation and/or other materials provided with the distribution.
50  * 3. All advertising materials mentioning features or use of this software
51  *    must display the following acknowledgements:
52  *      This product includes software developed by Jason R. Thorpe
53  *      for And Communications, http://www.and.com/
54  * 4. The name of the author may not be used to endorse or promote products
55  *    derived from this software without specific prior written permission.
56  *
57  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
58  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
59  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
60  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
61  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
62  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
63  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
64  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
65  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67  * SUCH DAMAGE.
68  */
69
70 #include <sys/cdefs.h>
71 __FBSDID("$FreeBSD$");
72
73 #include <sys/param.h>
74 #include <sys/queue.h>
75 #include <sys/systm.h>
76 #include <sys/kernel.h>
77 #include <sys/types.h>
78 #include <sys/malloc.h>
79 #include <sys/fcntl.h>
80 #include <sys/conf.h>
81 #include <sys/chio.h>
82 #include <sys/errno.h>
83 #include <sys/devicestat.h>
84
85 #include <cam/cam.h>
86 #include <cam/cam_ccb.h>
87 #include <cam/cam_periph.h>
88 #include <cam/cam_xpt_periph.h>
89 #include <cam/cam_debug.h>
90
91 #include <cam/scsi/scsi_all.h>
92 #include <cam/scsi/scsi_message.h>
93 #include <cam/scsi/scsi_ch.h>
94
95 /*
96  * Timeout definitions for various changer related commands.  They may
97  * be too short for some devices (especially the timeout for INITIALIZE
98  * ELEMENT STATUS).
99  */
100
101 static const u_int32_t  CH_TIMEOUT_MODE_SENSE                = 6000;
102 static const u_int32_t  CH_TIMEOUT_MOVE_MEDIUM               = 15 * 60 * 1000;
103 static const u_int32_t  CH_TIMEOUT_EXCHANGE_MEDIUM           = 15 * 60 * 1000;
104 static const u_int32_t  CH_TIMEOUT_POSITION_TO_ELEMENT       = 15 * 60 * 1000;
105 static const u_int32_t  CH_TIMEOUT_READ_ELEMENT_STATUS       = 5 * 60 * 1000;
106 static const u_int32_t  CH_TIMEOUT_SEND_VOLTAG               = 10000;
107 static const u_int32_t  CH_TIMEOUT_INITIALIZE_ELEMENT_STATUS = 500000;
108
109 typedef enum {
110         CH_FLAG_INVALID         = 0x001
111 } ch_flags;
112
113 typedef enum {
114         CH_STATE_PROBE,
115         CH_STATE_NORMAL
116 } ch_state;
117
118 typedef enum {
119         CH_CCB_PROBE,
120         CH_CCB_WAITING
121 } ch_ccb_types;
122
123 typedef enum {
124         CH_Q_NONE       = 0x00,
125         CH_Q_NO_DBD     = 0x01,
126         CH_Q_NO_DVCID   = 0x02
127 } ch_quirks;
128
129 #define CH_Q_BIT_STRING \
130         "\020"          \
131         "\001NO_DBD"    \
132         "\002NO_DVCID"
133
134 #define ccb_state       ppriv_field0
135 #define ccb_bp          ppriv_ptr1
136
137 struct scsi_mode_sense_data {
138         struct scsi_mode_header_6 header;
139         struct scsi_mode_blk_desc blk_desc;
140         union {
141                 struct page_element_address_assignment ea;
142                 struct page_transport_geometry_parameters tg;
143                 struct page_device_capabilities cap;
144         } pages;
145 };
146
147 struct ch_softc {
148         ch_flags        flags;
149         ch_state        state;
150         ch_quirks       quirks;
151         union ccb       saved_ccb;
152         struct devstat  *device_stats;
153         struct cdev     *dev;
154         int             open_count;
155
156         int             sc_picker;      /* current picker */
157
158         /*
159          * The following information is obtained from the
160          * element address assignment page.
161          */
162         int             sc_firsts[CHET_MAX + 1];        /* firsts */
163         int             sc_counts[CHET_MAX + 1];        /* counts */
164
165         /*
166          * The following mask defines the legal combinations
167          * of elements for the MOVE MEDIUM command.
168          */
169         u_int8_t        sc_movemask[CHET_MAX + 1];
170
171         /*
172          * As above, but for EXCHANGE MEDIUM.
173          */
174         u_int8_t        sc_exchangemask[CHET_MAX + 1];
175
176         /*
177          * Quirks; see below.  XXX KDM not implemented yet
178          */
179         int             sc_settledelay; /* delay for settle */
180 };
181
182 static  d_open_t        chopen;
183 static  d_close_t       chclose;
184 static  d_ioctl_t       chioctl;
185 static  periph_init_t   chinit;
186 static  periph_ctor_t   chregister;
187 static  periph_oninv_t  choninvalidate;
188 static  periph_dtor_t   chcleanup;
189 static  periph_start_t  chstart;
190 static  void            chasync(void *callback_arg, u_int32_t code,
191                                 struct cam_path *path, void *arg);
192 static  void            chdone(struct cam_periph *periph,
193                                union ccb *done_ccb);
194 static  int             cherror(union ccb *ccb, u_int32_t cam_flags,
195                                 u_int32_t sense_flags);
196 static  int             chmove(struct cam_periph *periph,
197                                struct changer_move *cm);
198 static  int             chexchange(struct cam_periph *periph,
199                                    struct changer_exchange *ce);
200 static  int             chposition(struct cam_periph *periph,
201                                    struct changer_position *cp);
202 static  int             chgetelemstatus(struct cam_periph *periph,
203                                 int scsi_version, u_long cmd,
204                                 struct changer_element_status_request *csr);
205 static  int             chsetvoltag(struct cam_periph *periph,
206                                     struct changer_set_voltag_request *csvr);
207 static  int             chielem(struct cam_periph *periph, 
208                                 unsigned int timeout);
209 static  int             chgetparams(struct cam_periph *periph);
210 static  int             chscsiversion(struct cam_periph *periph);
211
212 static struct periph_driver chdriver =
213 {
214         chinit, "ch",
215         TAILQ_HEAD_INITIALIZER(chdriver.units), /* generation */ 0
216 };
217
218 PERIPHDRIVER_DECLARE(ch, chdriver);
219
220 static struct cdevsw ch_cdevsw = {
221         .d_version =    D_VERSION,
222         .d_flags =      D_TRACKCLOSE,
223         .d_open =       chopen,
224         .d_close =      chclose,
225         .d_ioctl =      chioctl,
226         .d_name =       "ch",
227 };
228
229 static MALLOC_DEFINE(M_SCSICH, "scsi_ch", "scsi_ch buffers");
230
231 static void
232 chinit(void)
233 {
234         cam_status status;
235
236         /*
237          * Install a global async callback.  This callback will
238          * receive async callbacks like "new device found".
239          */
240         status = xpt_register_async(AC_FOUND_DEVICE, chasync, NULL, NULL);
241
242         if (status != CAM_REQ_CMP) {
243                 printf("ch: Failed to attach master async callback "
244                        "due to status 0x%x!\n", status);
245         }
246 }
247
248 static void
249 chdevgonecb(void *arg)
250 {
251         struct cam_sim    *sim;
252         struct ch_softc   *softc;
253         struct cam_periph *periph;
254         int i;
255
256         periph = (struct cam_periph *)arg;
257         sim = periph->sim;
258         softc = (struct ch_softc *)periph->softc;
259
260         KASSERT(softc->open_count >= 0, ("Negative open count %d",
261                 softc->open_count));
262
263         mtx_lock(sim->mtx);
264
265         /*
266          * When we get this callback, we will get no more close calls from
267          * devfs.  So if we have any dangling opens, we need to release the
268          * reference held for that particular context.
269          */
270         for (i = 0; i < softc->open_count; i++)
271                 cam_periph_release_locked(periph);
272
273         softc->open_count = 0;
274
275         /*
276          * Release the reference held for the device node, it is gone now.
277          */
278         cam_periph_release_locked(periph);
279
280         /*
281          * We reference the SIM lock directly here, instead of using
282          * cam_periph_unlock().  The reason is that the final call to
283          * cam_periph_release_locked() above could result in the periph
284          * getting freed.  If that is the case, dereferencing the periph
285          * with a cam_periph_unlock() call would cause a page fault.
286          */
287         mtx_unlock(sim->mtx);
288 }
289
290 static void
291 choninvalidate(struct cam_periph *periph)
292 {
293         struct ch_softc *softc;
294
295         softc = (struct ch_softc *)periph->softc;
296
297         /*
298          * De-register any async callbacks.
299          */
300         xpt_register_async(0, chasync, periph, periph->path);
301
302         softc->flags |= CH_FLAG_INVALID;
303
304         /*
305          * Tell devfs this device has gone away, and ask for a callback
306          * when it has cleaned up its state.
307          */
308         destroy_dev_sched_cb(softc->dev, chdevgonecb, periph);
309 }
310
311 static void
312 chcleanup(struct cam_periph *periph)
313 {
314         struct ch_softc *softc;
315
316         softc = (struct ch_softc *)periph->softc;
317
318         devstat_remove_entry(softc->device_stats);
319
320         free(softc, M_DEVBUF);
321 }
322
323 static void
324 chasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
325 {
326         struct cam_periph *periph;
327
328         periph = (struct cam_periph *)callback_arg;
329
330         switch(code) {
331         case AC_FOUND_DEVICE:
332         {
333                 struct ccb_getdev *cgd;
334                 cam_status status;
335
336                 cgd = (struct ccb_getdev *)arg;
337                 if (cgd == NULL)
338                         break;
339
340                 if (cgd->protocol != PROTO_SCSI)
341                         break;
342
343                 if (SID_TYPE(&cgd->inq_data)!= T_CHANGER)
344                         break;
345
346                 /*
347                  * Allocate a peripheral instance for
348                  * this device and start the probe
349                  * process.
350                  */
351                 status = cam_periph_alloc(chregister, choninvalidate,
352                                           chcleanup, chstart, "ch",
353                                           CAM_PERIPH_BIO, cgd->ccb_h.path,
354                                           chasync, AC_FOUND_DEVICE, cgd);
355
356                 if (status != CAM_REQ_CMP
357                  && status != CAM_REQ_INPROG)
358                         printf("chasync: Unable to probe new device "
359                                "due to status 0x%x\n", status);
360
361                 break;
362
363         }
364         default:
365                 cam_periph_async(periph, code, path, arg);
366                 break;
367         }
368 }
369
370 static cam_status
371 chregister(struct cam_periph *periph, void *arg)
372 {
373         struct ch_softc *softc;
374         struct ccb_getdev *cgd;
375         struct ccb_pathinq cpi;
376
377         cgd = (struct ccb_getdev *)arg;
378         if (cgd == NULL) {
379                 printf("chregister: no getdev CCB, can't register device\n");
380                 return(CAM_REQ_CMP_ERR);
381         }
382
383         softc = (struct ch_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT);
384
385         if (softc == NULL) {
386                 printf("chregister: Unable to probe new device. "
387                        "Unable to allocate softc\n");                           
388                 return(CAM_REQ_CMP_ERR);
389         }
390
391         bzero(softc, sizeof(*softc));
392         softc->state = CH_STATE_PROBE;
393         periph->softc = softc;
394         softc->quirks = CH_Q_NONE;
395
396         /*
397          * The DVCID and CURDATA bits were not introduced until the SMC
398          * spec.  If this device claims SCSI-2 or earlier support, then it
399          * very likely does not support these bits.
400          */
401         if (cgd->inq_data.version <= SCSI_REV_2)
402                 softc->quirks |= CH_Q_NO_DVCID;
403
404         bzero(&cpi, sizeof(cpi));
405         xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
406         cpi.ccb_h.func_code = XPT_PATH_INQ;
407         xpt_action((union ccb *)&cpi);
408
409         /*
410          * Changers don't have a blocksize, and obviously don't support
411          * tagged queueing.
412          */
413         cam_periph_unlock(periph);
414         softc->device_stats = devstat_new_entry("ch",
415                           periph->unit_number, 0,
416                           DEVSTAT_NO_BLOCKSIZE | DEVSTAT_NO_ORDERED_TAGS,
417                           SID_TYPE(&cgd->inq_data) |
418                           XPORT_DEVSTAT_TYPE(cpi.transport),
419                           DEVSTAT_PRIORITY_OTHER);
420
421         /*
422          * Acquire a reference to the periph before we create the devfs
423          * instance for it.  We'll release this reference once the devfs
424          * instance has been freed.
425          */
426         if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
427                 xpt_print(periph->path, "%s: lost periph during "
428                           "registration!\n", __func__);
429                 cam_periph_lock(periph);
430                 return (CAM_REQ_CMP_ERR);
431         }
432
433
434         /* Register the device */
435         softc->dev = make_dev(&ch_cdevsw, periph->unit_number, UID_ROOT,
436                               GID_OPERATOR, 0600, "%s%d", periph->periph_name,
437                               periph->unit_number);
438         cam_periph_lock(periph);
439         softc->dev->si_drv1 = periph;
440
441         /*
442          * Add an async callback so that we get
443          * notified if this device goes away.
444          */
445         xpt_register_async(AC_LOST_DEVICE, chasync, periph, periph->path);
446
447         /*
448          * Lock this periph until we are setup.
449          * This first call can't block
450          */
451         (void)cam_periph_hold(periph, PRIBIO);
452         xpt_schedule(periph, CAM_PRIORITY_DEV);
453
454         return(CAM_REQ_CMP);
455 }
456
457 static int
458 chopen(struct cdev *dev, int flags, int fmt, struct thread *td)
459 {
460         struct cam_periph *periph;
461         struct ch_softc *softc;
462         int error;
463
464         periph = (struct cam_periph *)dev->si_drv1;
465         if (cam_periph_acquire(periph) != CAM_REQ_CMP)
466                 return (ENXIO);
467
468         softc = (struct ch_softc *)periph->softc;
469
470         cam_periph_lock(periph);
471         
472         if (softc->flags & CH_FLAG_INVALID) {
473                 cam_periph_release_locked(periph);
474                 cam_periph_unlock(periph);
475                 return(ENXIO);
476         }
477
478         if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) {
479                 cam_periph_unlock(periph);
480                 cam_periph_release(periph);
481                 return (error);
482         }
483
484         /*
485          * Load information about this changer device into the softc.
486          */
487         if ((error = chgetparams(periph)) != 0) {
488                 cam_periph_unhold(periph);
489                 cam_periph_release_locked(periph);
490                 cam_periph_unlock(periph);
491                 return(error);
492         }
493
494         cam_periph_unhold(periph);
495
496         softc->open_count++;
497
498         cam_periph_unlock(periph);
499
500         return(error);
501 }
502
503 static int
504 chclose(struct cdev *dev, int flag, int fmt, struct thread *td)
505 {
506         struct  cam_sim *sim;
507         struct  cam_periph *periph;
508         struct  ch_softc *softc;
509
510         periph = (struct cam_periph *)dev->si_drv1;
511         if (periph == NULL)
512                 return(ENXIO);
513
514         sim = periph->sim;
515         softc = (struct ch_softc *)periph->softc;
516
517         mtx_lock(sim->mtx);
518
519         softc->open_count--;
520
521         cam_periph_release_locked(periph);
522
523         /*
524          * We reference the SIM lock directly here, instead of using
525          * cam_periph_unlock().  The reason is that the call to
526          * cam_periph_release_locked() above could result in the periph
527          * getting freed.  If that is the case, dereferencing the periph
528          * with a cam_periph_unlock() call would cause a page fault.
529          *
530          * cam_periph_release() avoids this problem using the same method,
531          * but we're manually acquiring and dropping the lock here to
532          * protect the open count and avoid another lock acquisition and
533          * release.
534          */
535         mtx_unlock(sim->mtx);
536
537         return(0);
538 }
539
540 static void
541 chstart(struct cam_periph *periph, union ccb *start_ccb)
542 {
543         struct ch_softc *softc;
544
545         softc = (struct ch_softc *)periph->softc;
546
547         switch (softc->state) {
548         case CH_STATE_NORMAL:
549         {
550                 if (periph->immediate_priority <= periph->pinfo.priority){
551                         start_ccb->ccb_h.ccb_state = CH_CCB_WAITING;
552
553                         SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
554                                           periph_links.sle);
555                         periph->immediate_priority = CAM_PRIORITY_NONE;
556                         wakeup(&periph->ccb_list);
557                 }
558                 break;
559         }
560         case CH_STATE_PROBE:
561         {
562                 int mode_buffer_len;
563                 void *mode_buffer;
564
565                 /*
566                  * Include the block descriptor when calculating the mode
567                  * buffer length,
568                  */
569                 mode_buffer_len = sizeof(struct scsi_mode_header_6) +
570                                   sizeof(struct scsi_mode_blk_desc) +
571                                  sizeof(struct page_element_address_assignment);
572
573                 mode_buffer = malloc(mode_buffer_len, M_SCSICH, M_NOWAIT);
574
575                 if (mode_buffer == NULL) {
576                         printf("chstart: couldn't malloc mode sense data\n");
577                         break;
578                 }
579                 bzero(mode_buffer, mode_buffer_len);
580
581                 /*
582                  * Get the element address assignment page.
583                  */
584                 scsi_mode_sense(&start_ccb->csio,
585                                 /* retries */ 1,
586                                 /* cbfcnp */ chdone,
587                                 /* tag_action */ MSG_SIMPLE_Q_TAG,
588                                 /* dbd */ (softc->quirks & CH_Q_NO_DBD) ?
589                                         FALSE : TRUE,
590                                 /* page_code */ SMS_PAGE_CTRL_CURRENT,
591                                 /* page */ CH_ELEMENT_ADDR_ASSIGN_PAGE,
592                                 /* param_buf */ (u_int8_t *)mode_buffer,
593                                 /* param_len */ mode_buffer_len,
594                                 /* sense_len */ SSD_FULL_SIZE,
595                                 /* timeout */ CH_TIMEOUT_MODE_SENSE);
596
597                 start_ccb->ccb_h.ccb_bp = NULL;
598                 start_ccb->ccb_h.ccb_state = CH_CCB_PROBE;
599                 xpt_action(start_ccb);
600                 break;
601         }
602         }
603 }
604
605 static void
606 chdone(struct cam_periph *periph, union ccb *done_ccb)
607 {
608         struct ch_softc *softc;
609         struct ccb_scsiio *csio;
610
611         softc = (struct ch_softc *)periph->softc;
612         csio = &done_ccb->csio;
613
614         switch(done_ccb->ccb_h.ccb_state) {
615         case CH_CCB_PROBE:
616         {
617                 struct scsi_mode_header_6 *mode_header;
618                 struct page_element_address_assignment *ea;
619                 char announce_buf[80];
620
621
622                 mode_header = (struct scsi_mode_header_6 *)csio->data_ptr;
623
624                 ea = (struct page_element_address_assignment *)
625                         find_mode_page_6(mode_header);
626
627                 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP){
628                         
629                         softc->sc_firsts[CHET_MT] = scsi_2btoul(ea->mtea);
630                         softc->sc_counts[CHET_MT] = scsi_2btoul(ea->nmte);
631                         softc->sc_firsts[CHET_ST] = scsi_2btoul(ea->fsea);
632                         softc->sc_counts[CHET_ST] = scsi_2btoul(ea->nse);
633                         softc->sc_firsts[CHET_IE] = scsi_2btoul(ea->fieea);
634                         softc->sc_counts[CHET_IE] = scsi_2btoul(ea->niee);
635                         softc->sc_firsts[CHET_DT] = scsi_2btoul(ea->fdtea);
636                         softc->sc_counts[CHET_DT] = scsi_2btoul(ea->ndte);
637                         softc->sc_picker = softc->sc_firsts[CHET_MT];
638
639 #define PLURAL(c)       (c) == 1 ? "" : "s"
640                         snprintf(announce_buf, sizeof(announce_buf),
641                                 "%d slot%s, %d drive%s, "
642                                 "%d picker%s, %d portal%s",
643                                 softc->sc_counts[CHET_ST],
644                                 PLURAL(softc->sc_counts[CHET_ST]),
645                                 softc->sc_counts[CHET_DT],
646                                 PLURAL(softc->sc_counts[CHET_DT]),
647                                 softc->sc_counts[CHET_MT],
648                                 PLURAL(softc->sc_counts[CHET_MT]),
649                                 softc->sc_counts[CHET_IE],
650                                 PLURAL(softc->sc_counts[CHET_IE]));
651 #undef PLURAL
652                 } else {
653                         int error;
654
655                         error = cherror(done_ccb, CAM_RETRY_SELTO,
656                                         SF_RETRY_UA | SF_NO_PRINT);
657                         /*
658                          * Retry any UNIT ATTENTION type errors.  They
659                          * are expected at boot.
660                          */
661                         if (error == ERESTART) {
662                                 /*
663                                  * A retry was scheuled, so
664                                  * just return.
665                                  */
666                                 return;
667                         } else if (error != 0) {
668                                 int retry_scheduled;
669                                 struct scsi_mode_sense_6 *sms;
670
671                                 sms = (struct scsi_mode_sense_6 *)
672                                         done_ccb->csio.cdb_io.cdb_bytes;
673
674                                 /*
675                                  * Check to see if block descriptors were
676                                  * disabled.  Some devices don't like that.
677                                  * We're taking advantage of the fact that
678                                  * the first few bytes of the 6 and 10 byte
679                                  * mode sense commands are the same.  If
680                                  * block descriptors were disabled, enable
681                                  * them and re-send the command.
682                                  */
683                                 if (sms->byte2 & SMS_DBD) {
684                                         sms->byte2 &= ~SMS_DBD;
685                                         xpt_action(done_ccb);
686                                         softc->quirks |= CH_Q_NO_DBD;
687                                         retry_scheduled = 1;
688                                 } else
689                                         retry_scheduled = 0;
690
691                                 /* Don't wedge this device's queue */
692                                 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
693                                         cam_release_devq(done_ccb->ccb_h.path,
694                                                  /*relsim_flags*/0,
695                                                  /*reduction*/0,
696                                                  /*timeout*/0,
697                                                  /*getcount_only*/0);
698
699                                 if (retry_scheduled)
700                                         return;
701
702                                 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK)
703                                     == CAM_SCSI_STATUS_ERROR) 
704                                         scsi_sense_print(&done_ccb->csio);
705                                 else {
706                                         xpt_print(periph->path,
707                                             "got CAM status %#x\n",
708                                             done_ccb->ccb_h.status);
709                                 }
710                                 xpt_print(periph->path, "fatal error, failed "
711                                     "to attach to device\n");
712
713                                 cam_periph_invalidate(periph);
714
715                                 announce_buf[0] = '\0';
716                         }
717                 }
718                 if (announce_buf[0] != '\0') {
719                         xpt_announce_periph(periph, announce_buf);
720                         xpt_announce_quirks(periph, softc->quirks,
721                             CH_Q_BIT_STRING);
722                 }
723                 softc->state = CH_STATE_NORMAL;
724                 free(mode_header, M_SCSICH);
725                 /*
726                  * Since our peripheral may be invalidated by an error
727                  * above or an external event, we must release our CCB
728                  * before releasing the probe lock on the peripheral.
729                  * The peripheral will only go away once the last lock
730                  * is removed, and we need it around for the CCB release
731                  * operation.
732                  */
733                 xpt_release_ccb(done_ccb);
734                 cam_periph_unhold(periph);
735                 return;
736         }
737         case CH_CCB_WAITING:
738         {
739                 /* Caller will release the CCB */
740                 wakeup(&done_ccb->ccb_h.cbfcnp);
741                 return;
742         }
743         default:
744                 break;
745         }
746         xpt_release_ccb(done_ccb);
747 }
748
749 static int
750 cherror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
751 {
752         struct ch_softc *softc;
753         struct cam_periph *periph;
754
755         periph = xpt_path_periph(ccb->ccb_h.path);
756         softc = (struct ch_softc *)periph->softc;
757
758         return (cam_periph_error(ccb, cam_flags, sense_flags,
759                                  &softc->saved_ccb));
760 }
761
762 static int
763 chioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
764 {
765         struct cam_periph *periph;
766         struct ch_softc *softc;
767         int error;
768
769         periph = (struct cam_periph *)dev->si_drv1;
770         if (periph == NULL)
771                 return(ENXIO);
772
773         cam_periph_lock(periph);
774         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering chioctl\n"));
775
776         softc = (struct ch_softc *)periph->softc;
777
778         error = 0;
779
780         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 
781                   ("trying to do ioctl %#lx\n", cmd));
782
783         /*
784          * If this command can change the device's state, we must
785          * have the device open for writing.
786          */
787         switch (cmd) {
788         case CHIOGPICKER:
789         case CHIOGPARAMS:
790         case OCHIOGSTATUS:
791         case CHIOGSTATUS:
792                 break;
793
794         default:
795                 if ((flag & FWRITE) == 0) {
796                         cam_periph_unlock(periph);
797                         return (EBADF);
798                 }
799         }
800
801         switch (cmd) {
802         case CHIOMOVE:
803                 error = chmove(periph, (struct changer_move *)addr);
804                 break;
805
806         case CHIOEXCHANGE:
807                 error = chexchange(periph, (struct changer_exchange *)addr);
808                 break;
809
810         case CHIOPOSITION:
811                 error = chposition(periph, (struct changer_position *)addr);
812                 break;
813
814         case CHIOGPICKER:
815                 *(int *)addr = softc->sc_picker - softc->sc_firsts[CHET_MT];
816                 break;
817
818         case CHIOSPICKER:
819         {
820                 int new_picker = *(int *)addr;
821
822                 if (new_picker > (softc->sc_counts[CHET_MT] - 1)) {
823                         error = EINVAL;
824                         break;
825                 }
826                 softc->sc_picker = softc->sc_firsts[CHET_MT] + new_picker;
827                 break;
828         }
829         case CHIOGPARAMS:
830         {
831                 struct changer_params *cp = (struct changer_params *)addr;
832
833                 cp->cp_npickers = softc->sc_counts[CHET_MT];
834                 cp->cp_nslots = softc->sc_counts[CHET_ST];
835                 cp->cp_nportals = softc->sc_counts[CHET_IE];
836                 cp->cp_ndrives = softc->sc_counts[CHET_DT];
837                 break;
838         }
839         case CHIOIELEM:
840                 error = chielem(periph, *(unsigned int *)addr);
841                 break;
842
843         case OCHIOGSTATUS:
844         {
845                 error = chgetelemstatus(periph, SCSI_REV_2, cmd,
846                     (struct changer_element_status_request *)addr);
847                 break;
848         }
849
850         case CHIOGSTATUS:
851         {
852                 int scsi_version;
853
854                 scsi_version = chscsiversion(periph);
855                 if (scsi_version >= SCSI_REV_0) {
856                         error = chgetelemstatus(periph, scsi_version, cmd,
857                             (struct changer_element_status_request *)addr);
858                 }
859                 else { /* unable to determine the SCSI version */
860                         cam_periph_unlock(periph);
861                         return (ENXIO);
862                 }
863                 break;
864         }
865
866         case CHIOSETVOLTAG:
867         {
868                 error = chsetvoltag(periph,
869                                     (struct changer_set_voltag_request *) addr);
870                 break;
871         }
872
873         /* Implement prevent/allow? */
874
875         default:
876                 error = cam_periph_ioctl(periph, cmd, addr, cherror);
877                 break;
878         }
879
880         cam_periph_unlock(periph);
881         return (error);
882 }
883
884 static int
885 chmove(struct cam_periph *periph, struct changer_move *cm)
886 {
887         struct ch_softc *softc;
888         u_int16_t fromelem, toelem;
889         union ccb *ccb;
890         int error;
891
892         error = 0;
893         softc = (struct ch_softc *)periph->softc;
894
895         /*
896          * Check arguments.
897          */
898         if ((cm->cm_fromtype > CHET_DT) || (cm->cm_totype > CHET_DT))
899                 return (EINVAL);
900         if ((cm->cm_fromunit > (softc->sc_counts[cm->cm_fromtype] - 1)) ||
901             (cm->cm_tounit > (softc->sc_counts[cm->cm_totype] - 1)))
902                 return (ENODEV);
903
904         /*
905          * Check the request against the changer's capabilities.
906          */
907         if ((softc->sc_movemask[cm->cm_fromtype] & (1 << cm->cm_totype)) == 0)
908                 return (ENODEV);
909
910         /*
911          * Calculate the source and destination elements.
912          */
913         fromelem = softc->sc_firsts[cm->cm_fromtype] + cm->cm_fromunit;
914         toelem = softc->sc_firsts[cm->cm_totype] + cm->cm_tounit;
915
916         ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
917
918         scsi_move_medium(&ccb->csio,
919                          /* retries */ 1,
920                          /* cbfcnp */ chdone,
921                          /* tag_action */ MSG_SIMPLE_Q_TAG,
922                          /* tea */ softc->sc_picker,
923                          /* src */ fromelem,
924                          /* dst */ toelem,
925                          /* invert */ (cm->cm_flags & CM_INVERT) ? TRUE : FALSE,
926                          /* sense_len */ SSD_FULL_SIZE,
927                          /* timeout */ CH_TIMEOUT_MOVE_MEDIUM);
928
929         error = cam_periph_runccb(ccb, cherror, /*cam_flags*/CAM_RETRY_SELTO,
930                                   /*sense_flags*/ SF_RETRY_UA,
931                                   softc->device_stats);
932
933         xpt_release_ccb(ccb);
934
935         return(error);
936 }
937
938 static int
939 chexchange(struct cam_periph *periph, struct changer_exchange *ce)
940 {
941         struct ch_softc *softc;
942         u_int16_t src, dst1, dst2;
943         union ccb *ccb;
944         int error;
945
946         error = 0;
947         softc = (struct ch_softc *)periph->softc;
948         /*
949          * Check arguments.
950          */
951         if ((ce->ce_srctype > CHET_DT) || (ce->ce_fdsttype > CHET_DT) ||
952             (ce->ce_sdsttype > CHET_DT))
953                 return (EINVAL);
954         if ((ce->ce_srcunit > (softc->sc_counts[ce->ce_srctype] - 1)) ||
955             (ce->ce_fdstunit > (softc->sc_counts[ce->ce_fdsttype] - 1)) ||
956             (ce->ce_sdstunit > (softc->sc_counts[ce->ce_sdsttype] - 1)))
957                 return (ENODEV);
958
959         /*
960          * Check the request against the changer's capabilities.
961          */
962         if (((softc->sc_exchangemask[ce->ce_srctype] &
963              (1 << ce->ce_fdsttype)) == 0) ||
964             ((softc->sc_exchangemask[ce->ce_fdsttype] &
965              (1 << ce->ce_sdsttype)) == 0))
966                 return (ENODEV);
967
968         /*
969          * Calculate the source and destination elements.
970          */
971         src = softc->sc_firsts[ce->ce_srctype] + ce->ce_srcunit;
972         dst1 = softc->sc_firsts[ce->ce_fdsttype] + ce->ce_fdstunit;
973         dst2 = softc->sc_firsts[ce->ce_sdsttype] + ce->ce_sdstunit;
974
975         ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
976
977         scsi_exchange_medium(&ccb->csio,
978                              /* retries */ 1,
979                              /* cbfcnp */ chdone,
980                              /* tag_action */ MSG_SIMPLE_Q_TAG,
981                              /* tea */ softc->sc_picker,
982                              /* src */ src,
983                              /* dst1 */ dst1,
984                              /* dst2 */ dst2,
985                              /* invert1 */ (ce->ce_flags & CE_INVERT1) ?
986                                            TRUE : FALSE,
987                              /* invert2 */ (ce->ce_flags & CE_INVERT2) ?
988                                            TRUE : FALSE,
989                              /* sense_len */ SSD_FULL_SIZE,
990                              /* timeout */ CH_TIMEOUT_EXCHANGE_MEDIUM);
991
992         error = cam_periph_runccb(ccb, cherror, /*cam_flags*/CAM_RETRY_SELTO,
993                                   /*sense_flags*/ SF_RETRY_UA,
994                                   softc->device_stats);
995
996         xpt_release_ccb(ccb);
997
998         return(error);
999 }
1000
1001 static int
1002 chposition(struct cam_periph *periph, struct changer_position *cp)
1003 {
1004         struct ch_softc *softc;
1005         u_int16_t dst;
1006         union ccb *ccb;
1007         int error;
1008
1009         error = 0;
1010         softc = (struct ch_softc *)periph->softc;
1011
1012         /*
1013          * Check arguments.
1014          */
1015         if (cp->cp_type > CHET_DT)
1016                 return (EINVAL);
1017         if (cp->cp_unit > (softc->sc_counts[cp->cp_type] - 1))
1018                 return (ENODEV);
1019
1020         /*
1021          * Calculate the destination element.
1022          */
1023         dst = softc->sc_firsts[cp->cp_type] + cp->cp_unit;
1024
1025         ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1026
1027         scsi_position_to_element(&ccb->csio,
1028                                  /* retries */ 1,
1029                                  /* cbfcnp */ chdone,
1030                                  /* tag_action */ MSG_SIMPLE_Q_TAG,
1031                                  /* tea */ softc->sc_picker,
1032                                  /* dst */ dst,
1033                                  /* invert */ (cp->cp_flags & CP_INVERT) ?
1034                                               TRUE : FALSE,
1035                                  /* sense_len */ SSD_FULL_SIZE,
1036                                  /* timeout */ CH_TIMEOUT_POSITION_TO_ELEMENT);
1037
1038         error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1039                                   /*sense_flags*/ SF_RETRY_UA,
1040                                   softc->device_stats);
1041
1042         xpt_release_ccb(ccb);
1043
1044         return(error);
1045 }
1046
1047 /*
1048  * Copy a volume tag to a volume_tag struct, converting SCSI byte order
1049  * to host native byte order in the volume serial number.  The volume
1050  * label as returned by the changer is transferred to user mode as
1051  * nul-terminated string.  Volume labels are truncated at the first
1052  * space, as suggested by SCSI-2.
1053  */
1054 static  void
1055 copy_voltag(struct changer_voltag *uvoltag, struct volume_tag *voltag)
1056 {
1057         int i;
1058         for (i=0; i<CH_VOLTAG_MAXLEN; i++) {
1059                 char c = voltag->vif[i];
1060                 if (c && c != ' ')
1061                         uvoltag->cv_volid[i] = c;
1062                 else
1063                         break;
1064         }
1065         uvoltag->cv_serial = scsi_2btoul(voltag->vsn);
1066 }
1067
1068 /*
1069  * Copy an element status descriptor to a user-mode
1070  * changer_element_status structure.
1071  */
1072 static void
1073 copy_element_status(struct ch_softc *softc,
1074                     u_int16_t flags,
1075                     struct read_element_status_descriptor *desc,
1076                     struct changer_element_status *ces,
1077                     int scsi_version)
1078 {
1079         u_int16_t eaddr = scsi_2btoul(desc->eaddr);
1080         u_int16_t et;
1081         struct volume_tag *pvol_tag = NULL, *avol_tag = NULL;
1082         struct read_element_status_device_id *devid = NULL;
1083
1084         ces->ces_int_addr = eaddr;
1085         /* set up logical address in element status */
1086         for (et = CHET_MT; et <= CHET_DT; et++) {
1087                 if ((softc->sc_firsts[et] <= eaddr)
1088                     && ((softc->sc_firsts[et] + softc->sc_counts[et])
1089                         > eaddr)) {
1090                         ces->ces_addr = eaddr - softc->sc_firsts[et];
1091                         ces->ces_type = et;
1092                         break;
1093                 }
1094         }
1095
1096         ces->ces_flags = desc->flags1;
1097
1098         ces->ces_sensecode = desc->sense_code;
1099         ces->ces_sensequal = desc->sense_qual;
1100
1101         if (desc->flags2 & READ_ELEMENT_STATUS_INVERT)
1102                 ces->ces_flags |= CES_INVERT;
1103
1104         if (desc->flags2 & READ_ELEMENT_STATUS_SVALID) {
1105
1106                 eaddr = scsi_2btoul(desc->ssea);
1107
1108                 /* convert source address to logical format */
1109                 for (et = CHET_MT; et <= CHET_DT; et++) {
1110                         if ((softc->sc_firsts[et] <= eaddr)
1111                             && ((softc->sc_firsts[et] + softc->sc_counts[et])
1112                                 > eaddr)) {
1113                                 ces->ces_source_addr =
1114                                         eaddr - softc->sc_firsts[et];
1115                                 ces->ces_source_type = et;
1116                                 ces->ces_flags |= CES_SOURCE_VALID;
1117                                 break;
1118                         }
1119                 }
1120
1121                 if (!(ces->ces_flags & CES_SOURCE_VALID))
1122                         printf("ch: warning: could not map element source "
1123                                "address %ud to a valid element type\n",
1124                                eaddr);
1125         }
1126
1127         /*
1128          * pvoltag and avoltag are common between SCSI-2 and later versions
1129          */
1130         if (flags & READ_ELEMENT_STATUS_PVOLTAG)
1131                 pvol_tag = &desc->voltag_devid.pvoltag;
1132         if (flags & READ_ELEMENT_STATUS_AVOLTAG)
1133                 avol_tag = (flags & READ_ELEMENT_STATUS_PVOLTAG) ?
1134                     &desc->voltag_devid.voltag[1] :&desc->voltag_devid.pvoltag;
1135         /*
1136          * For SCSI-3 and later, element status can carry designator and
1137          * other information.
1138          */
1139         if (scsi_version >= SCSI_REV_SPC) {
1140                 if ((flags & READ_ELEMENT_STATUS_PVOLTAG) ^
1141                     (flags & READ_ELEMENT_STATUS_AVOLTAG))
1142                         devid = &desc->voltag_devid.pvol_and_devid.devid;
1143                 else if (!(flags & READ_ELEMENT_STATUS_PVOLTAG) &&
1144                          !(flags & READ_ELEMENT_STATUS_AVOLTAG))
1145                         devid = &desc->voltag_devid.devid;
1146                 else /* Have both PVOLTAG and AVOLTAG */
1147                         devid = &desc->voltag_devid.vol_tags_and_devid.devid;
1148         }
1149
1150         if (pvol_tag)
1151                 copy_voltag(&(ces->ces_pvoltag), pvol_tag);
1152         if (avol_tag)
1153                 copy_voltag(&(ces->ces_pvoltag), avol_tag);
1154         if (devid != NULL) {
1155                 if (devid->designator_length > 0) {
1156                         bcopy((void *)devid->designator,
1157                               (void *)ces->ces_designator,
1158                               devid->designator_length);
1159                         ces->ces_designator_length = devid->designator_length;
1160                         /*
1161                          * Make sure we are always NUL terminated.  The
1162                          * This won't matter for the binary code set,
1163                          * since the user will only pay attention to the
1164                          * length field.
1165                          */
1166                         ces->ces_designator[devid->designator_length]= '\0';
1167                 }
1168                 if (devid->piv_assoc_designator_type &
1169                     READ_ELEMENT_STATUS_PIV_SET) {
1170                         ces->ces_flags |= CES_PIV;
1171                         ces->ces_protocol_id =
1172                             READ_ELEMENT_STATUS_PROTOCOL_ID(
1173                             devid->prot_code_set);
1174                 }
1175                 ces->ces_code_set =
1176                     READ_ELEMENT_STATUS_CODE_SET(devid->prot_code_set);
1177                 ces->ces_assoc = READ_ELEMENT_STATUS_ASSOCIATION(
1178                     devid->piv_assoc_designator_type);
1179                 ces->ces_designator_type = READ_ELEMENT_STATUS_DESIGNATOR_TYPE(
1180                     devid->piv_assoc_designator_type);
1181         } else if (scsi_version > SCSI_REV_2) {
1182                 /* SCSI-SPC and No devid, no designator */
1183                 ces->ces_designator_length = 0;
1184                 ces->ces_designator[0] = '\0';
1185                 ces->ces_protocol_id = CES_PROTOCOL_ID_FCP_4;
1186         }
1187
1188         if (scsi_version <= SCSI_REV_2) {
1189                 if (desc->dt_or_obsolete.scsi_2.dt_scsi_flags &
1190                     READ_ELEMENT_STATUS_DT_IDVALID) {
1191                         ces->ces_flags |= CES_SCSIID_VALID;
1192                         ces->ces_scsi_id =
1193                             desc->dt_or_obsolete.scsi_2.dt_scsi_addr;
1194                 }
1195
1196                 if (desc->dt_or_obsolete.scsi_2.dt_scsi_addr &
1197                     READ_ELEMENT_STATUS_DT_LUVALID) {
1198                         ces->ces_flags |= CES_LUN_VALID;
1199                         ces->ces_scsi_lun =
1200                             desc->dt_or_obsolete.scsi_2.dt_scsi_flags &
1201                             READ_ELEMENT_STATUS_DT_LUNMASK;
1202                 }
1203         }
1204 }
1205
1206 static int
1207 chgetelemstatus(struct cam_periph *periph, int scsi_version, u_long cmd,
1208                 struct changer_element_status_request *cesr)
1209 {
1210         struct read_element_status_header *st_hdr;
1211         struct read_element_status_page_header *pg_hdr;
1212         struct read_element_status_descriptor *desc;
1213         caddr_t data = NULL;
1214         size_t size, desclen;
1215         int avail, i, error = 0;
1216         int curdata, dvcid, sense_flags;
1217         int try_no_dvcid = 0;
1218         struct changer_element_status *user_data = NULL;
1219         struct ch_softc *softc;
1220         union ccb *ccb;
1221         int chet = cesr->cesr_element_type;
1222         int want_voltags = (cesr->cesr_flags & CESR_VOLTAGS) ? 1 : 0;
1223
1224         softc = (struct ch_softc *)periph->softc;
1225
1226         /* perform argument checking */
1227
1228         /*
1229          * Perform a range check on the cesr_element_{base,count}
1230          * request argument fields.
1231          */
1232         if ((softc->sc_counts[chet] - cesr->cesr_element_base) <= 0
1233             || (cesr->cesr_element_base + cesr->cesr_element_count)
1234                 > softc->sc_counts[chet])
1235                 return (EINVAL);
1236
1237         /*
1238          * Request one descriptor for the given element type.  This
1239          * is used to determine the size of the descriptor so that
1240          * we can allocate enough storage for all of them.  We assume
1241          * that the first one can fit into 1k.
1242          */
1243         cam_periph_unlock(periph);
1244         data = (caddr_t)malloc(1024, M_DEVBUF, M_WAITOK);
1245
1246         cam_periph_lock(periph);
1247         ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1248
1249         sense_flags = SF_RETRY_UA;
1250         if (softc->quirks & CH_Q_NO_DVCID) {
1251                 dvcid = 0;
1252                 curdata = 0;
1253         } else {
1254                 dvcid = 1;
1255                 curdata = 1;
1256                 /*
1257                  * Don't print anything for an Illegal Request, because
1258                  * these flags can cause some changers to complain.  We'll
1259                  * retry without them if we get an error.
1260                  */
1261                 sense_flags |= SF_QUIET_IR;
1262         }
1263
1264 retry_einval:
1265
1266         scsi_read_element_status(&ccb->csio,
1267                                  /* retries */ 1,
1268                                  /* cbfcnp */ chdone,
1269                                  /* tag_action */ MSG_SIMPLE_Q_TAG,
1270                                  /* voltag */ want_voltags,
1271                                  /* sea */ softc->sc_firsts[chet],
1272                                  /* curdata */ curdata,
1273                                  /* dvcid */ dvcid,
1274                                  /* count */ 1,
1275                                  /* data_ptr */ data,
1276                                  /* dxfer_len */ 1024,
1277                                  /* sense_len */ SSD_FULL_SIZE,
1278                                  /* timeout */ CH_TIMEOUT_READ_ELEMENT_STATUS);
1279
1280         error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1281                                   /*sense_flags*/ sense_flags,
1282                                   softc->device_stats);
1283
1284         /*
1285          * An Illegal Request sense key (only used if there is no asc/ascq)
1286          * or 0x24,0x00 for an ASC/ASCQ both map to EINVAL.  If dvcid or
1287          * curdata are set (we set both or neither), try turning them off
1288          * and see if the command is successful.
1289          */
1290         if ((error == EINVAL)
1291          && (dvcid || curdata))  {
1292                 dvcid = 0;
1293                 curdata = 0;
1294                 error = 0;
1295                 /* At this point we want to report any Illegal Request */
1296                 sense_flags &= ~SF_QUIET_IR;
1297                 try_no_dvcid = 1;
1298                 goto retry_einval;
1299         }
1300
1301         /*
1302          * In this case, we tried a read element status with dvcid and
1303          * curdata set, and it failed.  We retried without those bits, and
1304          * it succeeded.  Suggest to the user that he set a quirk, so we
1305          * don't go through the retry process the first time in the future.
1306          * This should only happen on changers that claim SCSI-3 or higher,
1307          * but don't support these bits.
1308          */
1309         if ((try_no_dvcid != 0)
1310          && (error == 0))
1311                 softc->quirks |= CH_Q_NO_DVCID;
1312
1313         if (error)
1314                 goto done;
1315         cam_periph_unlock(periph);
1316
1317         st_hdr = (struct read_element_status_header *)data;
1318         pg_hdr = (struct read_element_status_page_header *)((uintptr_t)st_hdr +
1319                   sizeof(struct read_element_status_header));
1320         desclen = scsi_2btoul(pg_hdr->edl);
1321
1322         size = sizeof(struct read_element_status_header) +
1323                sizeof(struct read_element_status_page_header) +
1324                (desclen * cesr->cesr_element_count);
1325         /*
1326          * Reallocate storage for descriptors and get them from the
1327          * device.
1328          */
1329         free(data, M_DEVBUF);
1330         data = (caddr_t)malloc(size, M_DEVBUF, M_WAITOK);
1331
1332         cam_periph_lock(periph);
1333         scsi_read_element_status(&ccb->csio,
1334                                  /* retries */ 1,
1335                                  /* cbfcnp */ chdone,
1336                                  /* tag_action */ MSG_SIMPLE_Q_TAG,
1337                                  /* voltag */ want_voltags,
1338                                  /* sea */ softc->sc_firsts[chet]
1339                                  + cesr->cesr_element_base,
1340                                  /* curdata */ curdata,
1341                                  /* dvcid */ dvcid,
1342                                  /* count */ cesr->cesr_element_count,
1343                                  /* data_ptr */ data,
1344                                  /* dxfer_len */ size,
1345                                  /* sense_len */ SSD_FULL_SIZE,
1346                                  /* timeout */ CH_TIMEOUT_READ_ELEMENT_STATUS);
1347
1348         error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1349                                   /*sense_flags*/ SF_RETRY_UA,
1350                                   softc->device_stats);
1351
1352         if (error)
1353                 goto done;
1354         cam_periph_unlock(periph);
1355
1356         /*
1357          * Fill in the user status array.
1358          */
1359         st_hdr = (struct read_element_status_header *)data;
1360         pg_hdr = (struct read_element_status_page_header *)((uintptr_t)st_hdr +
1361                   sizeof(struct read_element_status_header));
1362         avail = scsi_2btoul(st_hdr->count);
1363
1364         if (avail != cesr->cesr_element_count) {
1365                 xpt_print(periph->path,
1366                     "warning, READ ELEMENT STATUS avail != count\n");
1367         }
1368
1369         user_data = (struct changer_element_status *)
1370                 malloc(avail * sizeof(struct changer_element_status),
1371                        M_DEVBUF, M_WAITOK | M_ZERO);
1372
1373         desc = (struct read_element_status_descriptor *)((uintptr_t)data +
1374                 sizeof(struct read_element_status_header) +
1375                 sizeof(struct read_element_status_page_header));
1376         /*
1377          * Set up the individual element status structures
1378          */
1379         for (i = 0; i < avail; ++i) {
1380                 struct changer_element_status *ces;
1381
1382                 /*
1383                  * In the changer_element_status structure, fields from
1384                  * the beginning to the field of ces_scsi_lun are common
1385                  * between SCSI-2 and SCSI-3, while all the rest are new
1386                  * from SCSI-3. In order to maintain backward compatibility
1387                  * of the chio command, the ces pointer, below, is computed
1388                  * such that it lines up with the structure boundary
1389                  * corresponding to the SCSI version.
1390                  */
1391                 ces = cmd == OCHIOGSTATUS ?
1392                     (struct changer_element_status *)
1393                     ((unsigned char *)user_data + i *
1394                      (offsetof(struct changer_element_status,ces_scsi_lun)+1)):
1395                     &user_data[i];
1396
1397                 copy_element_status(softc, pg_hdr->flags, desc,
1398                                     ces, scsi_version);
1399
1400                 desc = (struct read_element_status_descriptor *)
1401                        ((unsigned char *)desc + desclen);
1402         }
1403
1404         /* Copy element status structures out to userspace. */
1405         if (cmd == OCHIOGSTATUS)
1406                 error = copyout(user_data,
1407                                 cesr->cesr_element_status,
1408                                 avail* (offsetof(struct changer_element_status,
1409                                 ces_scsi_lun) + 1));
1410         else
1411                 error = copyout(user_data,
1412                                 cesr->cesr_element_status,
1413                                 avail * sizeof(struct changer_element_status));
1414
1415         cam_periph_lock(periph);
1416
1417  done:
1418         xpt_release_ccb(ccb);
1419
1420         if (data != NULL)
1421                 free(data, M_DEVBUF);
1422         if (user_data != NULL)
1423                 free(user_data, M_DEVBUF);
1424
1425         return (error);
1426 }
1427
1428 static int
1429 chielem(struct cam_periph *periph,
1430         unsigned int timeout)
1431 {
1432         union ccb *ccb;
1433         struct ch_softc *softc;
1434         int error;
1435
1436         if (!timeout) {
1437                 timeout = CH_TIMEOUT_INITIALIZE_ELEMENT_STATUS;
1438         } else {
1439                 timeout *= 1000;
1440         }
1441
1442         error = 0;
1443         softc = (struct ch_softc *)periph->softc;
1444
1445         ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1446
1447         scsi_initialize_element_status(&ccb->csio,
1448                                       /* retries */ 1,
1449                                       /* cbfcnp */ chdone,
1450                                       /* tag_action */ MSG_SIMPLE_Q_TAG,
1451                                       /* sense_len */ SSD_FULL_SIZE,
1452                                       /* timeout */ timeout);
1453
1454         error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1455                                   /*sense_flags*/ SF_RETRY_UA,
1456                                   softc->device_stats);
1457
1458         xpt_release_ccb(ccb);
1459
1460         return(error);
1461 }
1462
1463 static int
1464 chsetvoltag(struct cam_periph *periph,
1465             struct changer_set_voltag_request *csvr)
1466 {
1467         union ccb *ccb;
1468         struct ch_softc *softc;
1469         u_int16_t ea;
1470         u_int8_t sac;
1471         struct scsi_send_volume_tag_parameters ssvtp;
1472         int error;
1473         int i;
1474
1475         error = 0;
1476         softc = (struct ch_softc *)periph->softc;
1477
1478         bzero(&ssvtp, sizeof(ssvtp));
1479         for (i=0; i<sizeof(ssvtp.vitf); i++) {
1480                 ssvtp.vitf[i] = ' ';
1481         }
1482
1483         /*
1484          * Check arguments.
1485          */
1486         if (csvr->csvr_type > CHET_DT)
1487                 return EINVAL;
1488         if (csvr->csvr_addr > (softc->sc_counts[csvr->csvr_type] - 1))
1489                 return ENODEV;
1490
1491         ea = softc->sc_firsts[csvr->csvr_type] + csvr->csvr_addr;
1492
1493         if (csvr->csvr_flags & CSVR_ALTERNATE) {
1494                 switch (csvr->csvr_flags & CSVR_MODE_MASK) {
1495                 case CSVR_MODE_SET:
1496                         sac = SEND_VOLUME_TAG_ASSERT_ALTERNATE;
1497                         break;
1498                 case CSVR_MODE_REPLACE:
1499                         sac = SEND_VOLUME_TAG_REPLACE_ALTERNATE;
1500                         break;
1501                 case CSVR_MODE_CLEAR:
1502                         sac = SEND_VOLUME_TAG_UNDEFINED_ALTERNATE;
1503                         break;
1504                 default:
1505                         error = EINVAL;
1506                         goto out;
1507                 }
1508         } else {
1509                 switch (csvr->csvr_flags & CSVR_MODE_MASK) {
1510                 case CSVR_MODE_SET:
1511                         sac = SEND_VOLUME_TAG_ASSERT_PRIMARY;
1512                         break;
1513                 case CSVR_MODE_REPLACE:
1514                         sac = SEND_VOLUME_TAG_REPLACE_PRIMARY;
1515                         break;
1516                 case CSVR_MODE_CLEAR:
1517                         sac = SEND_VOLUME_TAG_UNDEFINED_PRIMARY;
1518                         break;
1519                 default:
1520                         error = EINVAL;
1521                         goto out;
1522                 }
1523         }
1524
1525         memcpy(ssvtp.vitf, csvr->csvr_voltag.cv_volid,
1526                min(strlen(csvr->csvr_voltag.cv_volid), sizeof(ssvtp.vitf)));
1527         scsi_ulto2b(csvr->csvr_voltag.cv_serial, ssvtp.minvsn);
1528
1529         ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1530
1531         scsi_send_volume_tag(&ccb->csio,
1532                              /* retries */ 1,
1533                              /* cbfcnp */ chdone,
1534                              /* tag_action */ MSG_SIMPLE_Q_TAG,
1535                              /* element_address */ ea,
1536                              /* send_action_code */ sac,
1537                              /* parameters */ &ssvtp,
1538                              /* sense_len */ SSD_FULL_SIZE,
1539                              /* timeout */ CH_TIMEOUT_SEND_VOLTAG);
1540         
1541         error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1542                                   /*sense_flags*/ SF_RETRY_UA,
1543                                   softc->device_stats);
1544
1545         xpt_release_ccb(ccb);
1546
1547  out:
1548         return error;
1549 }
1550
1551 static int
1552 chgetparams(struct cam_periph *periph)
1553 {
1554         union ccb *ccb;
1555         struct ch_softc *softc;
1556         void *mode_buffer;
1557         int mode_buffer_len;
1558         struct page_element_address_assignment *ea;
1559         struct page_device_capabilities *cap;
1560         int error, from, dbd;
1561         u_int8_t *moves, *exchanges;
1562
1563         error = 0;
1564
1565         softc = (struct ch_softc *)periph->softc;
1566
1567         ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1568
1569         /*
1570          * The scsi_mode_sense_data structure is just a convenience
1571          * structure that allows us to easily calculate the worst-case
1572          * storage size of the mode sense buffer.
1573          */
1574         mode_buffer_len = sizeof(struct scsi_mode_sense_data);
1575
1576         mode_buffer = malloc(mode_buffer_len, M_SCSICH, M_NOWAIT);
1577
1578         if (mode_buffer == NULL) {
1579                 printf("chgetparams: couldn't malloc mode sense data\n");
1580                 return(ENOSPC);
1581         }
1582
1583         bzero(mode_buffer, mode_buffer_len);
1584
1585         if (softc->quirks & CH_Q_NO_DBD)
1586                 dbd = FALSE;
1587         else
1588                 dbd = TRUE;
1589
1590         /*
1591          * Get the element address assignment page.
1592          */
1593         scsi_mode_sense(&ccb->csio,
1594                         /* retries */ 1,
1595                         /* cbfcnp */ chdone,
1596                         /* tag_action */ MSG_SIMPLE_Q_TAG,
1597                         /* dbd */ dbd,
1598                         /* page_code */ SMS_PAGE_CTRL_CURRENT,
1599                         /* page */ CH_ELEMENT_ADDR_ASSIGN_PAGE,
1600                         /* param_buf */ (u_int8_t *)mode_buffer,
1601                         /* param_len */ mode_buffer_len,
1602                         /* sense_len */ SSD_FULL_SIZE,
1603                         /* timeout */ CH_TIMEOUT_MODE_SENSE);
1604
1605         error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1606                                   /* sense_flags */ SF_RETRY_UA|SF_NO_PRINT,
1607                                   softc->device_stats);
1608
1609         if (error) {
1610                 if (dbd) {
1611                         struct scsi_mode_sense_6 *sms;
1612
1613                         sms = (struct scsi_mode_sense_6 *)
1614                                 ccb->csio.cdb_io.cdb_bytes;
1615
1616                         sms->byte2 &= ~SMS_DBD;
1617                         error = cam_periph_runccb(ccb, cherror,
1618                                                   /*cam_flags*/ CAM_RETRY_SELTO,
1619                                                   /*sense_flags*/ SF_RETRY_UA,
1620                                                   softc->device_stats);
1621                 } else {
1622                         /*
1623                          * Since we disabled sense printing above, print
1624                          * out the sense here since we got an error.
1625                          */
1626                         scsi_sense_print(&ccb->csio);
1627                 }
1628
1629                 if (error) {
1630                         xpt_print(periph->path,
1631                             "chgetparams: error getting element "
1632                             "address page\n");
1633                         xpt_release_ccb(ccb);
1634                         free(mode_buffer, M_SCSICH);
1635                         return(error);
1636                 }
1637         }
1638
1639         ea = (struct page_element_address_assignment *)
1640                 find_mode_page_6((struct scsi_mode_header_6 *)mode_buffer);
1641
1642         softc->sc_firsts[CHET_MT] = scsi_2btoul(ea->mtea);
1643         softc->sc_counts[CHET_MT] = scsi_2btoul(ea->nmte);
1644         softc->sc_firsts[CHET_ST] = scsi_2btoul(ea->fsea);
1645         softc->sc_counts[CHET_ST] = scsi_2btoul(ea->nse);
1646         softc->sc_firsts[CHET_IE] = scsi_2btoul(ea->fieea);
1647         softc->sc_counts[CHET_IE] = scsi_2btoul(ea->niee);
1648         softc->sc_firsts[CHET_DT] = scsi_2btoul(ea->fdtea);
1649         softc->sc_counts[CHET_DT] = scsi_2btoul(ea->ndte);
1650
1651         bzero(mode_buffer, mode_buffer_len);
1652
1653         /*
1654          * Now get the device capabilities page.
1655          */
1656         scsi_mode_sense(&ccb->csio,
1657                         /* retries */ 1,
1658                         /* cbfcnp */ chdone,
1659                         /* tag_action */ MSG_SIMPLE_Q_TAG,
1660                         /* dbd */ dbd,
1661                         /* page_code */ SMS_PAGE_CTRL_CURRENT,
1662                         /* page */ CH_DEVICE_CAP_PAGE,
1663                         /* param_buf */ (u_int8_t *)mode_buffer,
1664                         /* param_len */ mode_buffer_len,
1665                         /* sense_len */ SSD_FULL_SIZE,
1666                         /* timeout */ CH_TIMEOUT_MODE_SENSE);
1667         
1668         error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1669                                   /* sense_flags */ SF_RETRY_UA | SF_NO_PRINT,
1670                                   softc->device_stats);
1671
1672         if (error) {
1673                 if (dbd) {
1674                         struct scsi_mode_sense_6 *sms;
1675
1676                         sms = (struct scsi_mode_sense_6 *)
1677                                 ccb->csio.cdb_io.cdb_bytes;
1678
1679                         sms->byte2 &= ~SMS_DBD;
1680                         error = cam_periph_runccb(ccb, cherror,
1681                                                   /*cam_flags*/ CAM_RETRY_SELTO,
1682                                                   /*sense_flags*/ SF_RETRY_UA,
1683                                                   softc->device_stats);
1684                 } else {
1685                         /*
1686                          * Since we disabled sense printing above, print
1687                          * out the sense here since we got an error.
1688                          */
1689                         scsi_sense_print(&ccb->csio);
1690                 }
1691
1692                 if (error) {
1693                         xpt_print(periph->path,
1694                             "chgetparams: error getting device "
1695                             "capabilities page\n");
1696                         xpt_release_ccb(ccb);
1697                         free(mode_buffer, M_SCSICH);
1698                         return(error);
1699                 }
1700         }
1701
1702         xpt_release_ccb(ccb);
1703
1704         cap = (struct page_device_capabilities *)
1705                 find_mode_page_6((struct scsi_mode_header_6 *)mode_buffer);
1706
1707         bzero(softc->sc_movemask, sizeof(softc->sc_movemask));
1708         bzero(softc->sc_exchangemask, sizeof(softc->sc_exchangemask));
1709         moves = cap->move_from;
1710         exchanges = cap->exchange_with;
1711         for (from = CHET_MT; from <= CHET_MAX; ++from) {
1712                 softc->sc_movemask[from] = moves[from];
1713                 softc->sc_exchangemask[from] = exchanges[from];
1714         }
1715
1716         free(mode_buffer, M_SCSICH);
1717
1718         return(error);
1719 }
1720
1721 static int
1722 chscsiversion(struct cam_periph *periph)
1723 {
1724         struct scsi_inquiry_data *inq_data;
1725         struct ccb_getdev *cgd;
1726         int dev_scsi_version;
1727         struct cam_sim *sim;
1728
1729         sim = xpt_path_sim(periph->path);
1730         mtx_assert(sim->mtx, MA_OWNED);
1731         if ((cgd = (struct ccb_getdev *)xpt_alloc_ccb_nowait()) == NULL)
1732                 return (-1);
1733         /*
1734          * Get the device information.
1735          */
1736         xpt_setup_ccb(&cgd->ccb_h,
1737                       periph->path,
1738                       CAM_PRIORITY_NORMAL);
1739         cgd->ccb_h.func_code = XPT_GDEV_TYPE;
1740         xpt_action((union ccb *)cgd);
1741
1742         if (cgd->ccb_h.status != CAM_REQ_CMP) {
1743                 xpt_free_ccb((union ccb *)cgd);
1744                 return -1;
1745         }
1746
1747         inq_data = &cgd->inq_data;
1748         dev_scsi_version = inq_data->version;
1749         xpt_free_ccb((union ccb *)cgd);
1750
1751         return dev_scsi_version;
1752 }
1753
1754 void
1755 scsi_move_medium(struct ccb_scsiio *csio, u_int32_t retries,
1756                  void (*cbfcnp)(struct cam_periph *, union ccb *),
1757                  u_int8_t tag_action, u_int32_t tea, u_int32_t src,
1758                  u_int32_t dst, int invert, u_int8_t sense_len,
1759                  u_int32_t timeout)
1760 {
1761         struct scsi_move_medium *scsi_cmd;
1762
1763         scsi_cmd = (struct scsi_move_medium *)&csio->cdb_io.cdb_bytes;
1764         bzero(scsi_cmd, sizeof(*scsi_cmd));
1765
1766         scsi_cmd->opcode = MOVE_MEDIUM;
1767
1768         scsi_ulto2b(tea, scsi_cmd->tea);
1769         scsi_ulto2b(src, scsi_cmd->src);
1770         scsi_ulto2b(dst, scsi_cmd->dst);
1771
1772         if (invert)
1773                 scsi_cmd->invert |= MOVE_MEDIUM_INVERT;
1774
1775         cam_fill_csio(csio,
1776                       retries,
1777                       cbfcnp,
1778                       /*flags*/ CAM_DIR_NONE,
1779                       tag_action,
1780                       /*data_ptr*/ NULL,
1781                       /*dxfer_len*/ 0,
1782                       sense_len,
1783                       sizeof(*scsi_cmd),
1784                       timeout);
1785 }
1786
1787 void
1788 scsi_exchange_medium(struct ccb_scsiio *csio, u_int32_t retries,
1789                      void (*cbfcnp)(struct cam_periph *, union ccb *),
1790                      u_int8_t tag_action, u_int32_t tea, u_int32_t src,
1791                      u_int32_t dst1, u_int32_t dst2, int invert1,
1792                      int invert2, u_int8_t sense_len, u_int32_t timeout)
1793 {
1794         struct scsi_exchange_medium *scsi_cmd;
1795
1796         scsi_cmd = (struct scsi_exchange_medium *)&csio->cdb_io.cdb_bytes;
1797         bzero(scsi_cmd, sizeof(*scsi_cmd));
1798
1799         scsi_cmd->opcode = EXCHANGE_MEDIUM;
1800
1801         scsi_ulto2b(tea, scsi_cmd->tea);
1802         scsi_ulto2b(src, scsi_cmd->src);
1803         scsi_ulto2b(dst1, scsi_cmd->fdst);
1804         scsi_ulto2b(dst2, scsi_cmd->sdst);
1805
1806         if (invert1)
1807                 scsi_cmd->invert |= EXCHANGE_MEDIUM_INV1;
1808
1809         if (invert2)
1810                 scsi_cmd->invert |= EXCHANGE_MEDIUM_INV2;
1811
1812         cam_fill_csio(csio,
1813                       retries,
1814                       cbfcnp,
1815                       /*flags*/ CAM_DIR_NONE,
1816                       tag_action,
1817                       /*data_ptr*/ NULL,
1818                       /*dxfer_len*/ 0,
1819                       sense_len,
1820                       sizeof(*scsi_cmd),
1821                       timeout);
1822 }
1823
1824 void
1825 scsi_position_to_element(struct ccb_scsiio *csio, u_int32_t retries,
1826                          void (*cbfcnp)(struct cam_periph *, union ccb *),
1827                          u_int8_t tag_action, u_int32_t tea, u_int32_t dst,
1828                          int invert, u_int8_t sense_len, u_int32_t timeout)
1829 {
1830         struct scsi_position_to_element *scsi_cmd;
1831
1832         scsi_cmd = (struct scsi_position_to_element *)&csio->cdb_io.cdb_bytes;
1833         bzero(scsi_cmd, sizeof(*scsi_cmd));
1834
1835         scsi_cmd->opcode = POSITION_TO_ELEMENT;
1836
1837         scsi_ulto2b(tea, scsi_cmd->tea);
1838         scsi_ulto2b(dst, scsi_cmd->dst);
1839
1840         if (invert)
1841                 scsi_cmd->invert |= POSITION_TO_ELEMENT_INVERT;
1842
1843         cam_fill_csio(csio,
1844                       retries,
1845                       cbfcnp,
1846                       /*flags*/ CAM_DIR_NONE,
1847                       tag_action,
1848                       /*data_ptr*/ NULL,
1849                       /*dxfer_len*/ 0,
1850                       sense_len,
1851                       sizeof(*scsi_cmd),
1852                       timeout);
1853 }
1854
1855 void
1856 scsi_read_element_status(struct ccb_scsiio *csio, u_int32_t retries,
1857                          void (*cbfcnp)(struct cam_periph *, union ccb *),
1858                          u_int8_t tag_action, int voltag, u_int32_t sea,
1859                          int curdata, int dvcid,
1860                          u_int32_t count, u_int8_t *data_ptr,
1861                          u_int32_t dxfer_len, u_int8_t sense_len,
1862                          u_int32_t timeout)
1863 {
1864         struct scsi_read_element_status *scsi_cmd;
1865
1866         scsi_cmd = (struct scsi_read_element_status *)&csio->cdb_io.cdb_bytes;
1867         bzero(scsi_cmd, sizeof(*scsi_cmd));
1868
1869         scsi_cmd->opcode = READ_ELEMENT_STATUS;
1870
1871         scsi_ulto2b(sea, scsi_cmd->sea);
1872         scsi_ulto2b(count, scsi_cmd->count);
1873         scsi_ulto3b(dxfer_len, scsi_cmd->len);
1874         if (dvcid)
1875                 scsi_cmd->flags |= READ_ELEMENT_STATUS_DVCID;
1876         if (curdata)
1877                 scsi_cmd->flags |= READ_ELEMENT_STATUS_CURDATA;
1878
1879         if (voltag)
1880                 scsi_cmd->byte2 |= READ_ELEMENT_STATUS_VOLTAG;
1881
1882         cam_fill_csio(csio,
1883                       retries,
1884                       cbfcnp,
1885                       /*flags*/ CAM_DIR_IN,
1886                       tag_action,
1887                       data_ptr,
1888                       dxfer_len,
1889                       sense_len,
1890                       sizeof(*scsi_cmd),
1891                       timeout);
1892 }
1893
1894 void
1895 scsi_initialize_element_status(struct ccb_scsiio *csio, u_int32_t retries,
1896                                void (*cbfcnp)(struct cam_periph *, union ccb *),
1897                                u_int8_t tag_action, u_int8_t sense_len,
1898                                u_int32_t timeout)
1899 {
1900         struct scsi_initialize_element_status *scsi_cmd;
1901
1902         scsi_cmd = (struct scsi_initialize_element_status *)
1903                     &csio->cdb_io.cdb_bytes;
1904         bzero(scsi_cmd, sizeof(*scsi_cmd));
1905
1906         scsi_cmd->opcode = INITIALIZE_ELEMENT_STATUS;
1907
1908         cam_fill_csio(csio,
1909                       retries,
1910                       cbfcnp,
1911                       /*flags*/ CAM_DIR_NONE,
1912                       tag_action,
1913                       /* data_ptr */ NULL,
1914                       /* dxfer_len */ 0,
1915                       sense_len,
1916                       sizeof(*scsi_cmd),
1917                       timeout);
1918 }
1919
1920 void
1921 scsi_send_volume_tag(struct ccb_scsiio *csio, u_int32_t retries,
1922                      void (*cbfcnp)(struct cam_periph *, union ccb *),
1923                      u_int8_t tag_action, 
1924                      u_int16_t element_address,
1925                      u_int8_t send_action_code,
1926                      struct scsi_send_volume_tag_parameters *parameters,
1927                      u_int8_t sense_len, u_int32_t timeout)
1928 {
1929         struct scsi_send_volume_tag *scsi_cmd;
1930
1931         scsi_cmd = (struct scsi_send_volume_tag *) &csio->cdb_io.cdb_bytes;
1932         bzero(scsi_cmd, sizeof(*scsi_cmd));
1933
1934         scsi_cmd->opcode = SEND_VOLUME_TAG;
1935         scsi_ulto2b(element_address, scsi_cmd->ea);
1936         scsi_cmd->sac = send_action_code;
1937         scsi_ulto2b(sizeof(*parameters), scsi_cmd->pll);
1938
1939         cam_fill_csio(csio,
1940                       retries,
1941                       cbfcnp,
1942                       /*flags*/ CAM_DIR_OUT,
1943                       tag_action,
1944                       /* data_ptr */ (u_int8_t *) parameters,
1945                       sizeof(*parameters),
1946                       sense_len,
1947                       sizeof(*scsi_cmd),
1948                       timeout);
1949 }