]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - sys/cam/scsi/scsi_ch.c
MFC r307902:
[FreeBSD/stable/9.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                         if (announce_buf[0] != '\0') {
653                                 xpt_announce_periph(periph, announce_buf);
654                                 xpt_announce_quirks(periph, softc->quirks,
655                                     CH_Q_BIT_STRING);
656                         }
657                 } else {
658                         int error;
659
660                         error = cherror(done_ccb, CAM_RETRY_SELTO,
661                                         SF_RETRY_UA | SF_NO_PRINT);
662                         /*
663                          * Retry any UNIT ATTENTION type errors.  They
664                          * are expected at boot.
665                          */
666                         if (error == ERESTART) {
667                                 /*
668                                  * A retry was scheuled, so
669                                  * just return.
670                                  */
671                                 return;
672                         } else if (error != 0) {
673                                 int retry_scheduled;
674                                 struct scsi_mode_sense_6 *sms;
675
676                                 sms = (struct scsi_mode_sense_6 *)
677                                         done_ccb->csio.cdb_io.cdb_bytes;
678
679                                 /*
680                                  * Check to see if block descriptors were
681                                  * disabled.  Some devices don't like that.
682                                  * We're taking advantage of the fact that
683                                  * the first few bytes of the 6 and 10 byte
684                                  * mode sense commands are the same.  If
685                                  * block descriptors were disabled, enable
686                                  * them and re-send the command.
687                                  */
688                                 if (sms->byte2 & SMS_DBD) {
689                                         sms->byte2 &= ~SMS_DBD;
690                                         xpt_action(done_ccb);
691                                         softc->quirks |= CH_Q_NO_DBD;
692                                         retry_scheduled = 1;
693                                 } else
694                                         retry_scheduled = 0;
695
696                                 /* Don't wedge this device's queue */
697                                 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
698                                         cam_release_devq(done_ccb->ccb_h.path,
699                                                  /*relsim_flags*/0,
700                                                  /*reduction*/0,
701                                                  /*timeout*/0,
702                                                  /*getcount_only*/0);
703
704                                 if (retry_scheduled)
705                                         return;
706
707                                 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK)
708                                     == CAM_SCSI_STATUS_ERROR) 
709                                         scsi_sense_print(&done_ccb->csio);
710                                 else {
711                                         xpt_print(periph->path,
712                                             "got CAM status %#x\n",
713                                             done_ccb->ccb_h.status);
714                                 }
715                                 xpt_print(periph->path, "fatal error, failed "
716                                     "to attach to device\n");
717
718                                 cam_periph_invalidate(periph);
719
720                         }
721                 }
722                 softc->state = CH_STATE_NORMAL;
723                 free(mode_header, M_SCSICH);
724                 /*
725                  * Since our peripheral may be invalidated by an error
726                  * above or an external event, we must release our CCB
727                  * before releasing the probe lock on the peripheral.
728                  * The peripheral will only go away once the last lock
729                  * is removed, and we need it around for the CCB release
730                  * operation.
731                  */
732                 xpt_release_ccb(done_ccb);
733                 cam_periph_unhold(periph);
734                 return;
735         }
736         case CH_CCB_WAITING:
737         {
738                 /* Caller will release the CCB */
739                 wakeup(&done_ccb->ccb_h.cbfcnp);
740                 return;
741         }
742         default:
743                 break;
744         }
745         xpt_release_ccb(done_ccb);
746 }
747
748 static int
749 cherror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
750 {
751         struct ch_softc *softc;
752         struct cam_periph *periph;
753
754         periph = xpt_path_periph(ccb->ccb_h.path);
755         softc = (struct ch_softc *)periph->softc;
756
757         return (cam_periph_error(ccb, cam_flags, sense_flags,
758                                  &softc->saved_ccb));
759 }
760
761 static int
762 chioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
763 {
764         struct cam_periph *periph;
765         struct ch_softc *softc;
766         int error;
767
768         periph = (struct cam_periph *)dev->si_drv1;
769         if (periph == NULL)
770                 return(ENXIO);
771
772         cam_periph_lock(periph);
773         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering chioctl\n"));
774
775         softc = (struct ch_softc *)periph->softc;
776
777         error = 0;
778
779         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 
780                   ("trying to do ioctl %#lx\n", cmd));
781
782         /*
783          * If this command can change the device's state, we must
784          * have the device open for writing.
785          */
786         switch (cmd) {
787         case CHIOGPICKER:
788         case CHIOGPARAMS:
789         case OCHIOGSTATUS:
790         case CHIOGSTATUS:
791                 break;
792
793         default:
794                 if ((flag & FWRITE) == 0) {
795                         cam_periph_unlock(periph);
796                         return (EBADF);
797                 }
798         }
799
800         switch (cmd) {
801         case CHIOMOVE:
802                 error = chmove(periph, (struct changer_move *)addr);
803                 break;
804
805         case CHIOEXCHANGE:
806                 error = chexchange(periph, (struct changer_exchange *)addr);
807                 break;
808
809         case CHIOPOSITION:
810                 error = chposition(periph, (struct changer_position *)addr);
811                 break;
812
813         case CHIOGPICKER:
814                 *(int *)addr = softc->sc_picker - softc->sc_firsts[CHET_MT];
815                 break;
816
817         case CHIOSPICKER:
818         {
819                 int new_picker = *(int *)addr;
820
821                 if (new_picker > (softc->sc_counts[CHET_MT] - 1)) {
822                         error = EINVAL;
823                         break;
824                 }
825                 softc->sc_picker = softc->sc_firsts[CHET_MT] + new_picker;
826                 break;
827         }
828         case CHIOGPARAMS:
829         {
830                 struct changer_params *cp = (struct changer_params *)addr;
831
832                 cp->cp_npickers = softc->sc_counts[CHET_MT];
833                 cp->cp_nslots = softc->sc_counts[CHET_ST];
834                 cp->cp_nportals = softc->sc_counts[CHET_IE];
835                 cp->cp_ndrives = softc->sc_counts[CHET_DT];
836                 break;
837         }
838         case CHIOIELEM:
839                 error = chielem(periph, *(unsigned int *)addr);
840                 break;
841
842         case OCHIOGSTATUS:
843         {
844                 error = chgetelemstatus(periph, SCSI_REV_2, cmd,
845                     (struct changer_element_status_request *)addr);
846                 break;
847         }
848
849         case CHIOGSTATUS:
850         {
851                 int scsi_version;
852
853                 scsi_version = chscsiversion(periph);
854                 if (scsi_version >= SCSI_REV_0) {
855                         error = chgetelemstatus(periph, scsi_version, cmd,
856                             (struct changer_element_status_request *)addr);
857                 }
858                 else { /* unable to determine the SCSI version */
859                         cam_periph_unlock(periph);
860                         return (ENXIO);
861                 }
862                 break;
863         }
864
865         case CHIOSETVOLTAG:
866         {
867                 error = chsetvoltag(periph,
868                                     (struct changer_set_voltag_request *) addr);
869                 break;
870         }
871
872         /* Implement prevent/allow? */
873
874         default:
875                 error = cam_periph_ioctl(periph, cmd, addr, cherror);
876                 break;
877         }
878
879         cam_periph_unlock(periph);
880         return (error);
881 }
882
883 static int
884 chmove(struct cam_periph *periph, struct changer_move *cm)
885 {
886         struct ch_softc *softc;
887         u_int16_t fromelem, toelem;
888         union ccb *ccb;
889         int error;
890
891         error = 0;
892         softc = (struct ch_softc *)periph->softc;
893
894         /*
895          * Check arguments.
896          */
897         if ((cm->cm_fromtype > CHET_DT) || (cm->cm_totype > CHET_DT))
898                 return (EINVAL);
899         if ((cm->cm_fromunit > (softc->sc_counts[cm->cm_fromtype] - 1)) ||
900             (cm->cm_tounit > (softc->sc_counts[cm->cm_totype] - 1)))
901                 return (ENODEV);
902
903         /*
904          * Check the request against the changer's capabilities.
905          */
906         if ((softc->sc_movemask[cm->cm_fromtype] & (1 << cm->cm_totype)) == 0)
907                 return (ENODEV);
908
909         /*
910          * Calculate the source and destination elements.
911          */
912         fromelem = softc->sc_firsts[cm->cm_fromtype] + cm->cm_fromunit;
913         toelem = softc->sc_firsts[cm->cm_totype] + cm->cm_tounit;
914
915         ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
916
917         scsi_move_medium(&ccb->csio,
918                          /* retries */ 1,
919                          /* cbfcnp */ chdone,
920                          /* tag_action */ MSG_SIMPLE_Q_TAG,
921                          /* tea */ softc->sc_picker,
922                          /* src */ fromelem,
923                          /* dst */ toelem,
924                          /* invert */ (cm->cm_flags & CM_INVERT) ? TRUE : FALSE,
925                          /* sense_len */ SSD_FULL_SIZE,
926                          /* timeout */ CH_TIMEOUT_MOVE_MEDIUM);
927
928         error = cam_periph_runccb(ccb, cherror, /*cam_flags*/CAM_RETRY_SELTO,
929                                   /*sense_flags*/ SF_RETRY_UA,
930                                   softc->device_stats);
931
932         xpt_release_ccb(ccb);
933
934         return(error);
935 }
936
937 static int
938 chexchange(struct cam_periph *periph, struct changer_exchange *ce)
939 {
940         struct ch_softc *softc;
941         u_int16_t src, dst1, dst2;
942         union ccb *ccb;
943         int error;
944
945         error = 0;
946         softc = (struct ch_softc *)periph->softc;
947         /*
948          * Check arguments.
949          */
950         if ((ce->ce_srctype > CHET_DT) || (ce->ce_fdsttype > CHET_DT) ||
951             (ce->ce_sdsttype > CHET_DT))
952                 return (EINVAL);
953         if ((ce->ce_srcunit > (softc->sc_counts[ce->ce_srctype] - 1)) ||
954             (ce->ce_fdstunit > (softc->sc_counts[ce->ce_fdsttype] - 1)) ||
955             (ce->ce_sdstunit > (softc->sc_counts[ce->ce_sdsttype] - 1)))
956                 return (ENODEV);
957
958         /*
959          * Check the request against the changer's capabilities.
960          */
961         if (((softc->sc_exchangemask[ce->ce_srctype] &
962              (1 << ce->ce_fdsttype)) == 0) ||
963             ((softc->sc_exchangemask[ce->ce_fdsttype] &
964              (1 << ce->ce_sdsttype)) == 0))
965                 return (ENODEV);
966
967         /*
968          * Calculate the source and destination elements.
969          */
970         src = softc->sc_firsts[ce->ce_srctype] + ce->ce_srcunit;
971         dst1 = softc->sc_firsts[ce->ce_fdsttype] + ce->ce_fdstunit;
972         dst2 = softc->sc_firsts[ce->ce_sdsttype] + ce->ce_sdstunit;
973
974         ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
975
976         scsi_exchange_medium(&ccb->csio,
977                              /* retries */ 1,
978                              /* cbfcnp */ chdone,
979                              /* tag_action */ MSG_SIMPLE_Q_TAG,
980                              /* tea */ softc->sc_picker,
981                              /* src */ src,
982                              /* dst1 */ dst1,
983                              /* dst2 */ dst2,
984                              /* invert1 */ (ce->ce_flags & CE_INVERT1) ?
985                                            TRUE : FALSE,
986                              /* invert2 */ (ce->ce_flags & CE_INVERT2) ?
987                                            TRUE : FALSE,
988                              /* sense_len */ SSD_FULL_SIZE,
989                              /* timeout */ CH_TIMEOUT_EXCHANGE_MEDIUM);
990
991         error = cam_periph_runccb(ccb, cherror, /*cam_flags*/CAM_RETRY_SELTO,
992                                   /*sense_flags*/ SF_RETRY_UA,
993                                   softc->device_stats);
994
995         xpt_release_ccb(ccb);
996
997         return(error);
998 }
999
1000 static int
1001 chposition(struct cam_periph *periph, struct changer_position *cp)
1002 {
1003         struct ch_softc *softc;
1004         u_int16_t dst;
1005         union ccb *ccb;
1006         int error;
1007
1008         error = 0;
1009         softc = (struct ch_softc *)periph->softc;
1010
1011         /*
1012          * Check arguments.
1013          */
1014         if (cp->cp_type > CHET_DT)
1015                 return (EINVAL);
1016         if (cp->cp_unit > (softc->sc_counts[cp->cp_type] - 1))
1017                 return (ENODEV);
1018
1019         /*
1020          * Calculate the destination element.
1021          */
1022         dst = softc->sc_firsts[cp->cp_type] + cp->cp_unit;
1023
1024         ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1025
1026         scsi_position_to_element(&ccb->csio,
1027                                  /* retries */ 1,
1028                                  /* cbfcnp */ chdone,
1029                                  /* tag_action */ MSG_SIMPLE_Q_TAG,
1030                                  /* tea */ softc->sc_picker,
1031                                  /* dst */ dst,
1032                                  /* invert */ (cp->cp_flags & CP_INVERT) ?
1033                                               TRUE : FALSE,
1034                                  /* sense_len */ SSD_FULL_SIZE,
1035                                  /* timeout */ CH_TIMEOUT_POSITION_TO_ELEMENT);
1036
1037         error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1038                                   /*sense_flags*/ SF_RETRY_UA,
1039                                   softc->device_stats);
1040
1041         xpt_release_ccb(ccb);
1042
1043         return(error);
1044 }
1045
1046 /*
1047  * Copy a volume tag to a volume_tag struct, converting SCSI byte order
1048  * to host native byte order in the volume serial number.  The volume
1049  * label as returned by the changer is transferred to user mode as
1050  * nul-terminated string.  Volume labels are truncated at the first
1051  * space, as suggested by SCSI-2.
1052  */
1053 static  void
1054 copy_voltag(struct changer_voltag *uvoltag, struct volume_tag *voltag)
1055 {
1056         int i;
1057         for (i=0; i<CH_VOLTAG_MAXLEN; i++) {
1058                 char c = voltag->vif[i];
1059                 if (c && c != ' ')
1060                         uvoltag->cv_volid[i] = c;
1061                 else
1062                         break;
1063         }
1064         uvoltag->cv_serial = scsi_2btoul(voltag->vsn);
1065 }
1066
1067 /*
1068  * Copy an element status descriptor to a user-mode
1069  * changer_element_status structure.
1070  */
1071 static void
1072 copy_element_status(struct ch_softc *softc,
1073                     u_int16_t flags,
1074                     struct read_element_status_descriptor *desc,
1075                     struct changer_element_status *ces,
1076                     int scsi_version)
1077 {
1078         u_int16_t eaddr = scsi_2btoul(desc->eaddr);
1079         u_int16_t et;
1080         struct volume_tag *pvol_tag = NULL, *avol_tag = NULL;
1081         struct read_element_status_device_id *devid = NULL;
1082
1083         ces->ces_int_addr = eaddr;
1084         /* set up logical address in element status */
1085         for (et = CHET_MT; et <= CHET_DT; et++) {
1086                 if ((softc->sc_firsts[et] <= eaddr)
1087                     && ((softc->sc_firsts[et] + softc->sc_counts[et])
1088                         > eaddr)) {
1089                         ces->ces_addr = eaddr - softc->sc_firsts[et];
1090                         ces->ces_type = et;
1091                         break;
1092                 }
1093         }
1094
1095         ces->ces_flags = desc->flags1;
1096
1097         ces->ces_sensecode = desc->sense_code;
1098         ces->ces_sensequal = desc->sense_qual;
1099
1100         if (desc->flags2 & READ_ELEMENT_STATUS_INVERT)
1101                 ces->ces_flags |= CES_INVERT;
1102
1103         if (desc->flags2 & READ_ELEMENT_STATUS_SVALID) {
1104
1105                 eaddr = scsi_2btoul(desc->ssea);
1106
1107                 /* convert source address to logical format */
1108                 for (et = CHET_MT; et <= CHET_DT; et++) {
1109                         if ((softc->sc_firsts[et] <= eaddr)
1110                             && ((softc->sc_firsts[et] + softc->sc_counts[et])
1111                                 > eaddr)) {
1112                                 ces->ces_source_addr =
1113                                         eaddr - softc->sc_firsts[et];
1114                                 ces->ces_source_type = et;
1115                                 ces->ces_flags |= CES_SOURCE_VALID;
1116                                 break;
1117                         }
1118                 }
1119
1120                 if (!(ces->ces_flags & CES_SOURCE_VALID))
1121                         printf("ch: warning: could not map element source "
1122                                "address %ud to a valid element type\n",
1123                                eaddr);
1124         }
1125
1126         /*
1127          * pvoltag and avoltag are common between SCSI-2 and later versions
1128          */
1129         if (flags & READ_ELEMENT_STATUS_PVOLTAG)
1130                 pvol_tag = &desc->voltag_devid.pvoltag;
1131         if (flags & READ_ELEMENT_STATUS_AVOLTAG)
1132                 avol_tag = (flags & READ_ELEMENT_STATUS_PVOLTAG) ?
1133                     &desc->voltag_devid.voltag[1] :&desc->voltag_devid.pvoltag;
1134         /*
1135          * For SCSI-3 and later, element status can carry designator and
1136          * other information.
1137          */
1138         if (scsi_version >= SCSI_REV_SPC) {
1139                 if ((flags & READ_ELEMENT_STATUS_PVOLTAG) ^
1140                     (flags & READ_ELEMENT_STATUS_AVOLTAG))
1141                         devid = &desc->voltag_devid.pvol_and_devid.devid;
1142                 else if (!(flags & READ_ELEMENT_STATUS_PVOLTAG) &&
1143                          !(flags & READ_ELEMENT_STATUS_AVOLTAG))
1144                         devid = &desc->voltag_devid.devid;
1145                 else /* Have both PVOLTAG and AVOLTAG */
1146                         devid = &desc->voltag_devid.vol_tags_and_devid.devid;
1147         }
1148
1149         if (pvol_tag)
1150                 copy_voltag(&(ces->ces_pvoltag), pvol_tag);
1151         if (avol_tag)
1152                 copy_voltag(&(ces->ces_pvoltag), avol_tag);
1153         if (devid != NULL) {
1154                 if (devid->designator_length > 0) {
1155                         bcopy((void *)devid->designator,
1156                               (void *)ces->ces_designator,
1157                               devid->designator_length);
1158                         ces->ces_designator_length = devid->designator_length;
1159                         /*
1160                          * Make sure we are always NUL terminated.  The
1161                          * This won't matter for the binary code set,
1162                          * since the user will only pay attention to the
1163                          * length field.
1164                          */
1165                         ces->ces_designator[devid->designator_length]= '\0';
1166                 }
1167                 if (devid->piv_assoc_designator_type &
1168                     READ_ELEMENT_STATUS_PIV_SET) {
1169                         ces->ces_flags |= CES_PIV;
1170                         ces->ces_protocol_id =
1171                             READ_ELEMENT_STATUS_PROTOCOL_ID(
1172                             devid->prot_code_set);
1173                 }
1174                 ces->ces_code_set =
1175                     READ_ELEMENT_STATUS_CODE_SET(devid->prot_code_set);
1176                 ces->ces_assoc = READ_ELEMENT_STATUS_ASSOCIATION(
1177                     devid->piv_assoc_designator_type);
1178                 ces->ces_designator_type = READ_ELEMENT_STATUS_DESIGNATOR_TYPE(
1179                     devid->piv_assoc_designator_type);
1180         } else if (scsi_version > SCSI_REV_2) {
1181                 /* SCSI-SPC and No devid, no designator */
1182                 ces->ces_designator_length = 0;
1183                 ces->ces_designator[0] = '\0';
1184                 ces->ces_protocol_id = CES_PROTOCOL_ID_FCP_4;
1185         }
1186
1187         if (scsi_version <= SCSI_REV_2) {
1188                 if (desc->dt_or_obsolete.scsi_2.dt_scsi_flags &
1189                     READ_ELEMENT_STATUS_DT_IDVALID) {
1190                         ces->ces_flags |= CES_SCSIID_VALID;
1191                         ces->ces_scsi_id =
1192                             desc->dt_or_obsolete.scsi_2.dt_scsi_addr;
1193                 }
1194
1195                 if (desc->dt_or_obsolete.scsi_2.dt_scsi_addr &
1196                     READ_ELEMENT_STATUS_DT_LUVALID) {
1197                         ces->ces_flags |= CES_LUN_VALID;
1198                         ces->ces_scsi_lun =
1199                             desc->dt_or_obsolete.scsi_2.dt_scsi_flags &
1200                             READ_ELEMENT_STATUS_DT_LUNMASK;
1201                 }
1202         }
1203 }
1204
1205 static int
1206 chgetelemstatus(struct cam_periph *periph, int scsi_version, u_long cmd,
1207                 struct changer_element_status_request *cesr)
1208 {
1209         struct read_element_status_header *st_hdr;
1210         struct read_element_status_page_header *pg_hdr;
1211         struct read_element_status_descriptor *desc;
1212         caddr_t data = NULL;
1213         size_t size, desclen;
1214         int avail, i, error = 0;
1215         int curdata, dvcid, sense_flags;
1216         int try_no_dvcid = 0;
1217         struct changer_element_status *user_data = NULL;
1218         struct ch_softc *softc;
1219         union ccb *ccb;
1220         int chet = cesr->cesr_element_type;
1221         int want_voltags = (cesr->cesr_flags & CESR_VOLTAGS) ? 1 : 0;
1222
1223         softc = (struct ch_softc *)periph->softc;
1224
1225         /* perform argument checking */
1226
1227         /*
1228          * Perform a range check on the cesr_element_{base,count}
1229          * request argument fields.
1230          */
1231         if ((softc->sc_counts[chet] - cesr->cesr_element_base) <= 0
1232             || (cesr->cesr_element_base + cesr->cesr_element_count)
1233                 > softc->sc_counts[chet])
1234                 return (EINVAL);
1235
1236         /*
1237          * Request one descriptor for the given element type.  This
1238          * is used to determine the size of the descriptor so that
1239          * we can allocate enough storage for all of them.  We assume
1240          * that the first one can fit into 1k.
1241          */
1242         cam_periph_unlock(periph);
1243         data = (caddr_t)malloc(1024, M_DEVBUF, M_WAITOK);
1244
1245         cam_periph_lock(periph);
1246         ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1247
1248         sense_flags = SF_RETRY_UA;
1249         if (softc->quirks & CH_Q_NO_DVCID) {
1250                 dvcid = 0;
1251                 curdata = 0;
1252         } else {
1253                 dvcid = 1;
1254                 curdata = 1;
1255                 /*
1256                  * Don't print anything for an Illegal Request, because
1257                  * these flags can cause some changers to complain.  We'll
1258                  * retry without them if we get an error.
1259                  */
1260                 sense_flags |= SF_QUIET_IR;
1261         }
1262
1263 retry_einval:
1264
1265         scsi_read_element_status(&ccb->csio,
1266                                  /* retries */ 1,
1267                                  /* cbfcnp */ chdone,
1268                                  /* tag_action */ MSG_SIMPLE_Q_TAG,
1269                                  /* voltag */ want_voltags,
1270                                  /* sea */ softc->sc_firsts[chet],
1271                                  /* curdata */ curdata,
1272                                  /* dvcid */ dvcid,
1273                                  /* count */ 1,
1274                                  /* data_ptr */ data,
1275                                  /* dxfer_len */ 1024,
1276                                  /* sense_len */ SSD_FULL_SIZE,
1277                                  /* timeout */ CH_TIMEOUT_READ_ELEMENT_STATUS);
1278
1279         error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1280                                   /*sense_flags*/ sense_flags,
1281                                   softc->device_stats);
1282
1283         /*
1284          * An Illegal Request sense key (only used if there is no asc/ascq)
1285          * or 0x24,0x00 for an ASC/ASCQ both map to EINVAL.  If dvcid or
1286          * curdata are set (we set both or neither), try turning them off
1287          * and see if the command is successful.
1288          */
1289         if ((error == EINVAL)
1290          && (dvcid || curdata))  {
1291                 dvcid = 0;
1292                 curdata = 0;
1293                 error = 0;
1294                 /* At this point we want to report any Illegal Request */
1295                 sense_flags &= ~SF_QUIET_IR;
1296                 try_no_dvcid = 1;
1297                 goto retry_einval;
1298         }
1299
1300         /*
1301          * In this case, we tried a read element status with dvcid and
1302          * curdata set, and it failed.  We retried without those bits, and
1303          * it succeeded.  Suggest to the user that he set a quirk, so we
1304          * don't go through the retry process the first time in the future.
1305          * This should only happen on changers that claim SCSI-3 or higher,
1306          * but don't support these bits.
1307          */
1308         if ((try_no_dvcid != 0)
1309          && (error == 0))
1310                 softc->quirks |= CH_Q_NO_DVCID;
1311
1312         if (error)
1313                 goto done;
1314         cam_periph_unlock(periph);
1315
1316         st_hdr = (struct read_element_status_header *)data;
1317         pg_hdr = (struct read_element_status_page_header *)((uintptr_t)st_hdr +
1318                   sizeof(struct read_element_status_header));
1319         desclen = scsi_2btoul(pg_hdr->edl);
1320
1321         size = sizeof(struct read_element_status_header) +
1322                sizeof(struct read_element_status_page_header) +
1323                (desclen * cesr->cesr_element_count);
1324         /*
1325          * Reallocate storage for descriptors and get them from the
1326          * device.
1327          */
1328         free(data, M_DEVBUF);
1329         data = (caddr_t)malloc(size, M_DEVBUF, M_WAITOK);
1330
1331         cam_periph_lock(periph);
1332         scsi_read_element_status(&ccb->csio,
1333                                  /* retries */ 1,
1334                                  /* cbfcnp */ chdone,
1335                                  /* tag_action */ MSG_SIMPLE_Q_TAG,
1336                                  /* voltag */ want_voltags,
1337                                  /* sea */ softc->sc_firsts[chet]
1338                                  + cesr->cesr_element_base,
1339                                  /* curdata */ curdata,
1340                                  /* dvcid */ dvcid,
1341                                  /* count */ cesr->cesr_element_count,
1342                                  /* data_ptr */ data,
1343                                  /* dxfer_len */ size,
1344                                  /* sense_len */ SSD_FULL_SIZE,
1345                                  /* timeout */ CH_TIMEOUT_READ_ELEMENT_STATUS);
1346
1347         error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1348                                   /*sense_flags*/ SF_RETRY_UA,
1349                                   softc->device_stats);
1350
1351         if (error)
1352                 goto done;
1353         cam_periph_unlock(periph);
1354
1355         /*
1356          * Fill in the user status array.
1357          */
1358         st_hdr = (struct read_element_status_header *)data;
1359         pg_hdr = (struct read_element_status_page_header *)((uintptr_t)st_hdr +
1360                   sizeof(struct read_element_status_header));
1361         avail = scsi_2btoul(st_hdr->count);
1362
1363         if (avail != cesr->cesr_element_count) {
1364                 xpt_print(periph->path,
1365                     "warning, READ ELEMENT STATUS avail != count\n");
1366         }
1367
1368         user_data = (struct changer_element_status *)
1369                 malloc(avail * sizeof(struct changer_element_status),
1370                        M_DEVBUF, M_WAITOK | M_ZERO);
1371
1372         desc = (struct read_element_status_descriptor *)((uintptr_t)data +
1373                 sizeof(struct read_element_status_header) +
1374                 sizeof(struct read_element_status_page_header));
1375         /*
1376          * Set up the individual element status structures
1377          */
1378         for (i = 0; i < avail; ++i) {
1379                 struct changer_element_status *ces;
1380
1381                 /*
1382                  * In the changer_element_status structure, fields from
1383                  * the beginning to the field of ces_scsi_lun are common
1384                  * between SCSI-2 and SCSI-3, while all the rest are new
1385                  * from SCSI-3. In order to maintain backward compatibility
1386                  * of the chio command, the ces pointer, below, is computed
1387                  * such that it lines up with the structure boundary
1388                  * corresponding to the SCSI version.
1389                  */
1390                 ces = cmd == OCHIOGSTATUS ?
1391                     (struct changer_element_status *)
1392                     ((unsigned char *)user_data + i *
1393                      (offsetof(struct changer_element_status,ces_scsi_lun)+1)):
1394                     &user_data[i];
1395
1396                 copy_element_status(softc, pg_hdr->flags, desc,
1397                                     ces, scsi_version);
1398
1399                 desc = (struct read_element_status_descriptor *)
1400                        ((unsigned char *)desc + desclen);
1401         }
1402
1403         /* Copy element status structures out to userspace. */
1404         if (cmd == OCHIOGSTATUS)
1405                 error = copyout(user_data,
1406                                 cesr->cesr_element_status,
1407                                 avail* (offsetof(struct changer_element_status,
1408                                 ces_scsi_lun) + 1));
1409         else
1410                 error = copyout(user_data,
1411                                 cesr->cesr_element_status,
1412                                 avail * sizeof(struct changer_element_status));
1413
1414         cam_periph_lock(periph);
1415
1416  done:
1417         xpt_release_ccb(ccb);
1418
1419         if (data != NULL)
1420                 free(data, M_DEVBUF);
1421         if (user_data != NULL)
1422                 free(user_data, M_DEVBUF);
1423
1424         return (error);
1425 }
1426
1427 static int
1428 chielem(struct cam_periph *periph,
1429         unsigned int timeout)
1430 {
1431         union ccb *ccb;
1432         struct ch_softc *softc;
1433         int error;
1434
1435         if (!timeout) {
1436                 timeout = CH_TIMEOUT_INITIALIZE_ELEMENT_STATUS;
1437         } else {
1438                 timeout *= 1000;
1439         }
1440
1441         error = 0;
1442         softc = (struct ch_softc *)periph->softc;
1443
1444         ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1445
1446         scsi_initialize_element_status(&ccb->csio,
1447                                       /* retries */ 1,
1448                                       /* cbfcnp */ chdone,
1449                                       /* tag_action */ MSG_SIMPLE_Q_TAG,
1450                                       /* sense_len */ SSD_FULL_SIZE,
1451                                       /* timeout */ timeout);
1452
1453         error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1454                                   /*sense_flags*/ SF_RETRY_UA,
1455                                   softc->device_stats);
1456
1457         xpt_release_ccb(ccb);
1458
1459         return(error);
1460 }
1461
1462 static int
1463 chsetvoltag(struct cam_periph *periph,
1464             struct changer_set_voltag_request *csvr)
1465 {
1466         union ccb *ccb;
1467         struct ch_softc *softc;
1468         u_int16_t ea;
1469         u_int8_t sac;
1470         struct scsi_send_volume_tag_parameters ssvtp;
1471         int error;
1472         int i;
1473
1474         error = 0;
1475         softc = (struct ch_softc *)periph->softc;
1476
1477         bzero(&ssvtp, sizeof(ssvtp));
1478         for (i=0; i<sizeof(ssvtp.vitf); i++) {
1479                 ssvtp.vitf[i] = ' ';
1480         }
1481
1482         /*
1483          * Check arguments.
1484          */
1485         if (csvr->csvr_type > CHET_DT)
1486                 return EINVAL;
1487         if (csvr->csvr_addr > (softc->sc_counts[csvr->csvr_type] - 1))
1488                 return ENODEV;
1489
1490         ea = softc->sc_firsts[csvr->csvr_type] + csvr->csvr_addr;
1491
1492         if (csvr->csvr_flags & CSVR_ALTERNATE) {
1493                 switch (csvr->csvr_flags & CSVR_MODE_MASK) {
1494                 case CSVR_MODE_SET:
1495                         sac = SEND_VOLUME_TAG_ASSERT_ALTERNATE;
1496                         break;
1497                 case CSVR_MODE_REPLACE:
1498                         sac = SEND_VOLUME_TAG_REPLACE_ALTERNATE;
1499                         break;
1500                 case CSVR_MODE_CLEAR:
1501                         sac = SEND_VOLUME_TAG_UNDEFINED_ALTERNATE;
1502                         break;
1503                 default:
1504                         error = EINVAL;
1505                         goto out;
1506                 }
1507         } else {
1508                 switch (csvr->csvr_flags & CSVR_MODE_MASK) {
1509                 case CSVR_MODE_SET:
1510                         sac = SEND_VOLUME_TAG_ASSERT_PRIMARY;
1511                         break;
1512                 case CSVR_MODE_REPLACE:
1513                         sac = SEND_VOLUME_TAG_REPLACE_PRIMARY;
1514                         break;
1515                 case CSVR_MODE_CLEAR:
1516                         sac = SEND_VOLUME_TAG_UNDEFINED_PRIMARY;
1517                         break;
1518                 default:
1519                         error = EINVAL;
1520                         goto out;
1521                 }
1522         }
1523
1524         memcpy(ssvtp.vitf, csvr->csvr_voltag.cv_volid,
1525                min(strlen(csvr->csvr_voltag.cv_volid), sizeof(ssvtp.vitf)));
1526         scsi_ulto2b(csvr->csvr_voltag.cv_serial, ssvtp.minvsn);
1527
1528         ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1529
1530         scsi_send_volume_tag(&ccb->csio,
1531                              /* retries */ 1,
1532                              /* cbfcnp */ chdone,
1533                              /* tag_action */ MSG_SIMPLE_Q_TAG,
1534                              /* element_address */ ea,
1535                              /* send_action_code */ sac,
1536                              /* parameters */ &ssvtp,
1537                              /* sense_len */ SSD_FULL_SIZE,
1538                              /* timeout */ CH_TIMEOUT_SEND_VOLTAG);
1539         
1540         error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1541                                   /*sense_flags*/ SF_RETRY_UA,
1542                                   softc->device_stats);
1543
1544         xpt_release_ccb(ccb);
1545
1546  out:
1547         return error;
1548 }
1549
1550 static int
1551 chgetparams(struct cam_periph *periph)
1552 {
1553         union ccb *ccb;
1554         struct ch_softc *softc;
1555         void *mode_buffer;
1556         int mode_buffer_len;
1557         struct page_element_address_assignment *ea;
1558         struct page_device_capabilities *cap;
1559         int error, from, dbd;
1560         u_int8_t *moves, *exchanges;
1561
1562         error = 0;
1563
1564         softc = (struct ch_softc *)periph->softc;
1565
1566         ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1567
1568         /*
1569          * The scsi_mode_sense_data structure is just a convenience
1570          * structure that allows us to easily calculate the worst-case
1571          * storage size of the mode sense buffer.
1572          */
1573         mode_buffer_len = sizeof(struct scsi_mode_sense_data);
1574
1575         mode_buffer = malloc(mode_buffer_len, M_SCSICH, M_NOWAIT);
1576
1577         if (mode_buffer == NULL) {
1578                 printf("chgetparams: couldn't malloc mode sense data\n");
1579                 return(ENOSPC);
1580         }
1581
1582         bzero(mode_buffer, mode_buffer_len);
1583
1584         if (softc->quirks & CH_Q_NO_DBD)
1585                 dbd = FALSE;
1586         else
1587                 dbd = TRUE;
1588
1589         /*
1590          * Get the element address assignment page.
1591          */
1592         scsi_mode_sense(&ccb->csio,
1593                         /* retries */ 1,
1594                         /* cbfcnp */ chdone,
1595                         /* tag_action */ MSG_SIMPLE_Q_TAG,
1596                         /* dbd */ dbd,
1597                         /* page_code */ SMS_PAGE_CTRL_CURRENT,
1598                         /* page */ CH_ELEMENT_ADDR_ASSIGN_PAGE,
1599                         /* param_buf */ (u_int8_t *)mode_buffer,
1600                         /* param_len */ mode_buffer_len,
1601                         /* sense_len */ SSD_FULL_SIZE,
1602                         /* timeout */ CH_TIMEOUT_MODE_SENSE);
1603
1604         error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1605                                   /* sense_flags */ SF_RETRY_UA|SF_NO_PRINT,
1606                                   softc->device_stats);
1607
1608         if (error) {
1609                 if (dbd) {
1610                         struct scsi_mode_sense_6 *sms;
1611
1612                         sms = (struct scsi_mode_sense_6 *)
1613                                 ccb->csio.cdb_io.cdb_bytes;
1614
1615                         sms->byte2 &= ~SMS_DBD;
1616                         error = cam_periph_runccb(ccb, cherror,
1617                                                   /*cam_flags*/ CAM_RETRY_SELTO,
1618                                                   /*sense_flags*/ SF_RETRY_UA,
1619                                                   softc->device_stats);
1620                 } else {
1621                         /*
1622                          * Since we disabled sense printing above, print
1623                          * out the sense here since we got an error.
1624                          */
1625                         scsi_sense_print(&ccb->csio);
1626                 }
1627
1628                 if (error) {
1629                         xpt_print(periph->path,
1630                             "chgetparams: error getting element "
1631                             "address page\n");
1632                         xpt_release_ccb(ccb);
1633                         free(mode_buffer, M_SCSICH);
1634                         return(error);
1635                 }
1636         }
1637
1638         ea = (struct page_element_address_assignment *)
1639                 find_mode_page_6((struct scsi_mode_header_6 *)mode_buffer);
1640
1641         softc->sc_firsts[CHET_MT] = scsi_2btoul(ea->mtea);
1642         softc->sc_counts[CHET_MT] = scsi_2btoul(ea->nmte);
1643         softc->sc_firsts[CHET_ST] = scsi_2btoul(ea->fsea);
1644         softc->sc_counts[CHET_ST] = scsi_2btoul(ea->nse);
1645         softc->sc_firsts[CHET_IE] = scsi_2btoul(ea->fieea);
1646         softc->sc_counts[CHET_IE] = scsi_2btoul(ea->niee);
1647         softc->sc_firsts[CHET_DT] = scsi_2btoul(ea->fdtea);
1648         softc->sc_counts[CHET_DT] = scsi_2btoul(ea->ndte);
1649
1650         bzero(mode_buffer, mode_buffer_len);
1651
1652         /*
1653          * Now get the device capabilities page.
1654          */
1655         scsi_mode_sense(&ccb->csio,
1656                         /* retries */ 1,
1657                         /* cbfcnp */ chdone,
1658                         /* tag_action */ MSG_SIMPLE_Q_TAG,
1659                         /* dbd */ dbd,
1660                         /* page_code */ SMS_PAGE_CTRL_CURRENT,
1661                         /* page */ CH_DEVICE_CAP_PAGE,
1662                         /* param_buf */ (u_int8_t *)mode_buffer,
1663                         /* param_len */ mode_buffer_len,
1664                         /* sense_len */ SSD_FULL_SIZE,
1665                         /* timeout */ CH_TIMEOUT_MODE_SENSE);
1666         
1667         error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1668                                   /* sense_flags */ SF_RETRY_UA | SF_NO_PRINT,
1669                                   softc->device_stats);
1670
1671         if (error) {
1672                 if (dbd) {
1673                         struct scsi_mode_sense_6 *sms;
1674
1675                         sms = (struct scsi_mode_sense_6 *)
1676                                 ccb->csio.cdb_io.cdb_bytes;
1677
1678                         sms->byte2 &= ~SMS_DBD;
1679                         error = cam_periph_runccb(ccb, cherror,
1680                                                   /*cam_flags*/ CAM_RETRY_SELTO,
1681                                                   /*sense_flags*/ SF_RETRY_UA,
1682                                                   softc->device_stats);
1683                 } else {
1684                         /*
1685                          * Since we disabled sense printing above, print
1686                          * out the sense here since we got an error.
1687                          */
1688                         scsi_sense_print(&ccb->csio);
1689                 }
1690
1691                 if (error) {
1692                         xpt_print(periph->path,
1693                             "chgetparams: error getting device "
1694                             "capabilities page\n");
1695                         xpt_release_ccb(ccb);
1696                         free(mode_buffer, M_SCSICH);
1697                         return(error);
1698                 }
1699         }
1700
1701         xpt_release_ccb(ccb);
1702
1703         cap = (struct page_device_capabilities *)
1704                 find_mode_page_6((struct scsi_mode_header_6 *)mode_buffer);
1705
1706         bzero(softc->sc_movemask, sizeof(softc->sc_movemask));
1707         bzero(softc->sc_exchangemask, sizeof(softc->sc_exchangemask));
1708         moves = cap->move_from;
1709         exchanges = cap->exchange_with;
1710         for (from = CHET_MT; from <= CHET_MAX; ++from) {
1711                 softc->sc_movemask[from] = moves[from];
1712                 softc->sc_exchangemask[from] = exchanges[from];
1713         }
1714
1715         free(mode_buffer, M_SCSICH);
1716
1717         return(error);
1718 }
1719
1720 static int
1721 chscsiversion(struct cam_periph *periph)
1722 {
1723         struct scsi_inquiry_data *inq_data;
1724         struct ccb_getdev *cgd;
1725         int dev_scsi_version;
1726         struct cam_sim *sim;
1727
1728         sim = xpt_path_sim(periph->path);
1729         mtx_assert(sim->mtx, MA_OWNED);
1730         if ((cgd = (struct ccb_getdev *)xpt_alloc_ccb_nowait()) == NULL)
1731                 return (-1);
1732         /*
1733          * Get the device information.
1734          */
1735         xpt_setup_ccb(&cgd->ccb_h,
1736                       periph->path,
1737                       CAM_PRIORITY_NORMAL);
1738         cgd->ccb_h.func_code = XPT_GDEV_TYPE;
1739         xpt_action((union ccb *)cgd);
1740
1741         if (cgd->ccb_h.status != CAM_REQ_CMP) {
1742                 xpt_free_ccb((union ccb *)cgd);
1743                 return -1;
1744         }
1745
1746         inq_data = &cgd->inq_data;
1747         dev_scsi_version = inq_data->version;
1748         xpt_free_ccb((union ccb *)cgd);
1749
1750         return dev_scsi_version;
1751 }
1752
1753 void
1754 scsi_move_medium(struct ccb_scsiio *csio, u_int32_t retries,
1755                  void (*cbfcnp)(struct cam_periph *, union ccb *),
1756                  u_int8_t tag_action, u_int32_t tea, u_int32_t src,
1757                  u_int32_t dst, int invert, u_int8_t sense_len,
1758                  u_int32_t timeout)
1759 {
1760         struct scsi_move_medium *scsi_cmd;
1761
1762         scsi_cmd = (struct scsi_move_medium *)&csio->cdb_io.cdb_bytes;
1763         bzero(scsi_cmd, sizeof(*scsi_cmd));
1764
1765         scsi_cmd->opcode = MOVE_MEDIUM;
1766
1767         scsi_ulto2b(tea, scsi_cmd->tea);
1768         scsi_ulto2b(src, scsi_cmd->src);
1769         scsi_ulto2b(dst, scsi_cmd->dst);
1770
1771         if (invert)
1772                 scsi_cmd->invert |= MOVE_MEDIUM_INVERT;
1773
1774         cam_fill_csio(csio,
1775                       retries,
1776                       cbfcnp,
1777                       /*flags*/ CAM_DIR_NONE,
1778                       tag_action,
1779                       /*data_ptr*/ NULL,
1780                       /*dxfer_len*/ 0,
1781                       sense_len,
1782                       sizeof(*scsi_cmd),
1783                       timeout);
1784 }
1785
1786 void
1787 scsi_exchange_medium(struct ccb_scsiio *csio, u_int32_t retries,
1788                      void (*cbfcnp)(struct cam_periph *, union ccb *),
1789                      u_int8_t tag_action, u_int32_t tea, u_int32_t src,
1790                      u_int32_t dst1, u_int32_t dst2, int invert1,
1791                      int invert2, u_int8_t sense_len, u_int32_t timeout)
1792 {
1793         struct scsi_exchange_medium *scsi_cmd;
1794
1795         scsi_cmd = (struct scsi_exchange_medium *)&csio->cdb_io.cdb_bytes;
1796         bzero(scsi_cmd, sizeof(*scsi_cmd));
1797
1798         scsi_cmd->opcode = EXCHANGE_MEDIUM;
1799
1800         scsi_ulto2b(tea, scsi_cmd->tea);
1801         scsi_ulto2b(src, scsi_cmd->src);
1802         scsi_ulto2b(dst1, scsi_cmd->fdst);
1803         scsi_ulto2b(dst2, scsi_cmd->sdst);
1804
1805         if (invert1)
1806                 scsi_cmd->invert |= EXCHANGE_MEDIUM_INV1;
1807
1808         if (invert2)
1809                 scsi_cmd->invert |= EXCHANGE_MEDIUM_INV2;
1810
1811         cam_fill_csio(csio,
1812                       retries,
1813                       cbfcnp,
1814                       /*flags*/ CAM_DIR_NONE,
1815                       tag_action,
1816                       /*data_ptr*/ NULL,
1817                       /*dxfer_len*/ 0,
1818                       sense_len,
1819                       sizeof(*scsi_cmd),
1820                       timeout);
1821 }
1822
1823 void
1824 scsi_position_to_element(struct ccb_scsiio *csio, u_int32_t retries,
1825                          void (*cbfcnp)(struct cam_periph *, union ccb *),
1826                          u_int8_t tag_action, u_int32_t tea, u_int32_t dst,
1827                          int invert, u_int8_t sense_len, u_int32_t timeout)
1828 {
1829         struct scsi_position_to_element *scsi_cmd;
1830
1831         scsi_cmd = (struct scsi_position_to_element *)&csio->cdb_io.cdb_bytes;
1832         bzero(scsi_cmd, sizeof(*scsi_cmd));
1833
1834         scsi_cmd->opcode = POSITION_TO_ELEMENT;
1835
1836         scsi_ulto2b(tea, scsi_cmd->tea);
1837         scsi_ulto2b(dst, scsi_cmd->dst);
1838
1839         if (invert)
1840                 scsi_cmd->invert |= POSITION_TO_ELEMENT_INVERT;
1841
1842         cam_fill_csio(csio,
1843                       retries,
1844                       cbfcnp,
1845                       /*flags*/ CAM_DIR_NONE,
1846                       tag_action,
1847                       /*data_ptr*/ NULL,
1848                       /*dxfer_len*/ 0,
1849                       sense_len,
1850                       sizeof(*scsi_cmd),
1851                       timeout);
1852 }
1853
1854 void
1855 scsi_read_element_status(struct ccb_scsiio *csio, u_int32_t retries,
1856                          void (*cbfcnp)(struct cam_periph *, union ccb *),
1857                          u_int8_t tag_action, int voltag, u_int32_t sea,
1858                          int curdata, int dvcid,
1859                          u_int32_t count, u_int8_t *data_ptr,
1860                          u_int32_t dxfer_len, u_int8_t sense_len,
1861                          u_int32_t timeout)
1862 {
1863         struct scsi_read_element_status *scsi_cmd;
1864
1865         scsi_cmd = (struct scsi_read_element_status *)&csio->cdb_io.cdb_bytes;
1866         bzero(scsi_cmd, sizeof(*scsi_cmd));
1867
1868         scsi_cmd->opcode = READ_ELEMENT_STATUS;
1869
1870         scsi_ulto2b(sea, scsi_cmd->sea);
1871         scsi_ulto2b(count, scsi_cmd->count);
1872         scsi_ulto3b(dxfer_len, scsi_cmd->len);
1873         if (dvcid)
1874                 scsi_cmd->flags |= READ_ELEMENT_STATUS_DVCID;
1875         if (curdata)
1876                 scsi_cmd->flags |= READ_ELEMENT_STATUS_CURDATA;
1877
1878         if (voltag)
1879                 scsi_cmd->byte2 |= READ_ELEMENT_STATUS_VOLTAG;
1880
1881         cam_fill_csio(csio,
1882                       retries,
1883                       cbfcnp,
1884                       /*flags*/ CAM_DIR_IN,
1885                       tag_action,
1886                       data_ptr,
1887                       dxfer_len,
1888                       sense_len,
1889                       sizeof(*scsi_cmd),
1890                       timeout);
1891 }
1892
1893 void
1894 scsi_initialize_element_status(struct ccb_scsiio *csio, u_int32_t retries,
1895                                void (*cbfcnp)(struct cam_periph *, union ccb *),
1896                                u_int8_t tag_action, u_int8_t sense_len,
1897                                u_int32_t timeout)
1898 {
1899         struct scsi_initialize_element_status *scsi_cmd;
1900
1901         scsi_cmd = (struct scsi_initialize_element_status *)
1902                     &csio->cdb_io.cdb_bytes;
1903         bzero(scsi_cmd, sizeof(*scsi_cmd));
1904
1905         scsi_cmd->opcode = INITIALIZE_ELEMENT_STATUS;
1906
1907         cam_fill_csio(csio,
1908                       retries,
1909                       cbfcnp,
1910                       /*flags*/ CAM_DIR_NONE,
1911                       tag_action,
1912                       /* data_ptr */ NULL,
1913                       /* dxfer_len */ 0,
1914                       sense_len,
1915                       sizeof(*scsi_cmd),
1916                       timeout);
1917 }
1918
1919 void
1920 scsi_send_volume_tag(struct ccb_scsiio *csio, u_int32_t retries,
1921                      void (*cbfcnp)(struct cam_periph *, union ccb *),
1922                      u_int8_t tag_action, 
1923                      u_int16_t element_address,
1924                      u_int8_t send_action_code,
1925                      struct scsi_send_volume_tag_parameters *parameters,
1926                      u_int8_t sense_len, u_int32_t timeout)
1927 {
1928         struct scsi_send_volume_tag *scsi_cmd;
1929
1930         scsi_cmd = (struct scsi_send_volume_tag *) &csio->cdb_io.cdb_bytes;
1931         bzero(scsi_cmd, sizeof(*scsi_cmd));
1932
1933         scsi_cmd->opcode = SEND_VOLUME_TAG;
1934         scsi_ulto2b(element_address, scsi_cmd->ea);
1935         scsi_cmd->sac = send_action_code;
1936         scsi_ulto2b(sizeof(*parameters), scsi_cmd->pll);
1937
1938         cam_fill_csio(csio,
1939                       retries,
1940                       cbfcnp,
1941                       /*flags*/ CAM_DIR_OUT,
1942                       tag_action,
1943                       /* data_ptr */ (u_int8_t *) parameters,
1944                       sizeof(*parameters),
1945                       sense_len,
1946                       sizeof(*scsi_cmd),
1947                       timeout);
1948 }