]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - sys/cam/scsi/scsi_ch.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.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               = 100000;
103 static const u_int32_t  CH_TIMEOUT_EXCHANGE_MEDIUM           = 100000;
104 static const u_int32_t  CH_TIMEOUT_POSITION_TO_ELEMENT       = 100000;
105 static const u_int32_t  CH_TIMEOUT_READ_ELEMENT_STATUS       = 10000;
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_FLAG_OPEN            = 0x002
112 } ch_flags;
113
114 typedef enum {
115         CH_STATE_PROBE,
116         CH_STATE_NORMAL
117 } ch_state;
118
119 typedef enum {
120         CH_CCB_PROBE,
121         CH_CCB_WAITING
122 } ch_ccb_types;
123
124 typedef enum {
125         CH_Q_NONE       = 0x00,
126         CH_Q_NO_DBD     = 0x01
127 } ch_quirks;
128
129 #define ccb_state       ppriv_field0
130 #define ccb_bp          ppriv_ptr1
131
132 struct scsi_mode_sense_data {
133         struct scsi_mode_header_6 header;
134         struct scsi_mode_blk_desc blk_desc;
135         union {
136                 struct page_element_address_assignment ea;
137                 struct page_transport_geometry_parameters tg;
138                 struct page_device_capabilities cap;
139         } pages;
140 };
141
142 struct ch_softc {
143         ch_flags        flags;
144         ch_state        state;
145         ch_quirks       quirks;
146         union ccb       saved_ccb;
147         struct devstat  *device_stats;
148         struct cdev *dev;
149
150         int             sc_picker;      /* current picker */
151
152         /*
153          * The following information is obtained from the
154          * element address assignment page.
155          */
156         int             sc_firsts[CHET_MAX + 1];        /* firsts */
157         int             sc_counts[CHET_MAX + 1];        /* counts */
158
159         /*
160          * The following mask defines the legal combinations
161          * of elements for the MOVE MEDIUM command.
162          */
163         u_int8_t        sc_movemask[CHET_MAX + 1];
164
165         /*
166          * As above, but for EXCHANGE MEDIUM.
167          */
168         u_int8_t        sc_exchangemask[CHET_MAX + 1];
169
170         /*
171          * Quirks; see below.  XXX KDM not implemented yet
172          */
173         int             sc_settledelay; /* delay for settle */
174 };
175
176 static  d_open_t        chopen;
177 static  d_close_t       chclose;
178 static  d_ioctl_t       chioctl;
179 static  periph_init_t   chinit;
180 static  periph_ctor_t   chregister;
181 static  periph_oninv_t  choninvalidate;
182 static  periph_dtor_t   chcleanup;
183 static  periph_start_t  chstart;
184 static  void            chasync(void *callback_arg, u_int32_t code,
185                                 struct cam_path *path, void *arg);
186 static  void            chdone(struct cam_periph *periph,
187                                union ccb *done_ccb);
188 static  int             cherror(union ccb *ccb, u_int32_t cam_flags,
189                                 u_int32_t sense_flags);
190 static  int             chmove(struct cam_periph *periph,
191                                struct changer_move *cm);
192 static  int             chexchange(struct cam_periph *periph,
193                                    struct changer_exchange *ce);
194 static  int             chposition(struct cam_periph *periph,
195                                    struct changer_position *cp);
196 static  int             chgetelemstatus(struct cam_periph *periph,
197                                 struct changer_element_status_request *csr);
198 static  int             chsetvoltag(struct cam_periph *periph,
199                                     struct changer_set_voltag_request *csvr);
200 static  int             chielem(struct cam_periph *periph, 
201                                 unsigned int timeout);
202 static  int             chgetparams(struct cam_periph *periph);
203
204 static struct periph_driver chdriver =
205 {
206         chinit, "ch",
207         TAILQ_HEAD_INITIALIZER(chdriver.units), /* generation */ 0
208 };
209
210 PERIPHDRIVER_DECLARE(ch, chdriver);
211
212 static struct cdevsw ch_cdevsw = {
213         .d_version =    D_VERSION,
214         .d_flags =      0,
215         .d_open =       chopen,
216         .d_close =      chclose,
217         .d_ioctl =      chioctl,
218         .d_name =       "ch",
219 };
220
221 MALLOC_DEFINE(M_SCSICH, "scsi_ch", "scsi_ch buffers");
222
223 static void
224 chinit(void)
225 {
226         cam_status status;
227
228         /*
229          * Install a global async callback.  This callback will
230          * receive async callbacks like "new device found".
231          */
232         status = xpt_register_async(AC_FOUND_DEVICE, chasync, NULL, NULL);
233
234         if (status != CAM_REQ_CMP) {
235                 printf("ch: Failed to attach master async callback "
236                        "due to status 0x%x!\n", status);
237         }
238 }
239
240 static void
241 choninvalidate(struct cam_periph *periph)
242 {
243         struct ch_softc *softc;
244
245         softc = (struct ch_softc *)periph->softc;
246
247         /*
248          * De-register any async callbacks.
249          */
250         xpt_register_async(0, chasync, periph, periph->path);
251
252         softc->flags |= CH_FLAG_INVALID;
253
254         xpt_print(periph->path, "lost device\n");
255
256 }
257
258 static void
259 chcleanup(struct cam_periph *periph)
260 {
261         struct ch_softc *softc;
262
263         softc = (struct ch_softc *)periph->softc;
264
265         xpt_print(periph->path, "removing device entry\n");
266         devstat_remove_entry(softc->device_stats);
267         cam_periph_unlock(periph);
268         destroy_dev(softc->dev);
269         cam_periph_lock(periph);
270         free(softc, M_DEVBUF);
271 }
272
273 static void
274 chasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
275 {
276         struct cam_periph *periph;
277
278         periph = (struct cam_periph *)callback_arg;
279
280         switch(code) {
281         case AC_FOUND_DEVICE:
282         {
283                 struct ccb_getdev *cgd;
284                 cam_status status;
285
286                 cgd = (struct ccb_getdev *)arg;
287                 if (cgd == NULL)
288                         break;
289
290                 if (cgd->protocol != PROTO_SCSI)
291                         break;
292
293                 if (SID_TYPE(&cgd->inq_data)!= T_CHANGER)
294                         break;
295
296                 /*
297                  * Allocate a peripheral instance for
298                  * this device and start the probe
299                  * process.
300                  */
301                 status = cam_periph_alloc(chregister, choninvalidate,
302                                           chcleanup, chstart, "ch",
303                                           CAM_PERIPH_BIO, cgd->ccb_h.path,
304                                           chasync, AC_FOUND_DEVICE, cgd);
305
306                 if (status != CAM_REQ_CMP
307                  && status != CAM_REQ_INPROG)
308                         printf("chasync: Unable to probe new device "
309                                "due to status 0x%x\n", status);
310
311                 break;
312
313         }
314         default:
315                 cam_periph_async(periph, code, path, arg);
316                 break;
317         }
318 }
319
320 static cam_status
321 chregister(struct cam_periph *periph, void *arg)
322 {
323         struct ch_softc *softc;
324         struct ccb_getdev *cgd;
325         struct ccb_pathinq cpi;
326
327         cgd = (struct ccb_getdev *)arg;
328         if (periph == NULL) {
329                 printf("chregister: periph was NULL!!\n");
330                 return(CAM_REQ_CMP_ERR);
331         }
332
333         if (cgd == NULL) {
334                 printf("chregister: no getdev CCB, can't register device\n");
335                 return(CAM_REQ_CMP_ERR);
336         }
337
338         softc = (struct ch_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT);
339
340         if (softc == NULL) {
341                 printf("chregister: Unable to probe new device. "
342                        "Unable to allocate softc\n");                           
343                 return(CAM_REQ_CMP_ERR);
344         }
345
346         bzero(softc, sizeof(*softc));
347         softc->state = CH_STATE_PROBE;
348         periph->softc = softc;
349         softc->quirks = CH_Q_NONE;
350
351         bzero(&cpi, sizeof(cpi));
352         xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
353         cpi.ccb_h.func_code = XPT_PATH_INQ;
354         xpt_action((union ccb *)&cpi);
355
356         /*
357          * Changers don't have a blocksize, and obviously don't support
358          * tagged queueing.
359          */
360         cam_periph_unlock(periph);
361         softc->device_stats = devstat_new_entry("ch",
362                           periph->unit_number, 0,
363                           DEVSTAT_NO_BLOCKSIZE | DEVSTAT_NO_ORDERED_TAGS,
364                           SID_TYPE(&cgd->inq_data) |
365                           XPORT_DEVSTAT_TYPE(cpi.transport),
366                           DEVSTAT_PRIORITY_OTHER);
367
368         /* Register the device */
369         softc->dev = make_dev(&ch_cdevsw, periph->unit_number, UID_ROOT,
370                               GID_OPERATOR, 0600, "%s%d", periph->periph_name,
371                               periph->unit_number);
372         cam_periph_lock(periph);
373         softc->dev->si_drv1 = periph;
374
375         /*
376          * Add an async callback so that we get
377          * notified if this device goes away.
378          */
379         xpt_register_async(AC_LOST_DEVICE, chasync, periph, periph->path);
380
381         /*
382          * Lock this periph until we are setup.
383          * This first call can't block
384          */
385         (void)cam_periph_hold(periph, PRIBIO);
386         xpt_schedule(periph, CAM_PRIORITY_DEV);
387
388         return(CAM_REQ_CMP);
389 }
390
391 static int
392 chopen(struct cdev *dev, int flags, int fmt, struct thread *td)
393 {
394         struct cam_periph *periph;
395         struct ch_softc *softc;
396         int error;
397
398         periph = (struct cam_periph *)dev->si_drv1;
399         if (cam_periph_acquire(periph) != CAM_REQ_CMP)
400                 return (ENXIO);
401
402         softc = (struct ch_softc *)periph->softc;
403
404         cam_periph_lock(periph);
405         
406         if (softc->flags & CH_FLAG_INVALID) {
407                 cam_periph_unlock(periph);
408                 cam_periph_release(periph);
409                 return(ENXIO);
410         }
411
412         if ((softc->flags & CH_FLAG_OPEN) == 0)
413                 softc->flags |= CH_FLAG_OPEN;
414         else
415                 cam_periph_release(periph);
416
417         if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) {
418                 cam_periph_unlock(periph);
419                 cam_periph_release(periph);
420                 return (error);
421         }
422
423         /*
424          * Load information about this changer device into the softc.
425          */
426         if ((error = chgetparams(periph)) != 0) {
427                 softc->flags &= ~CH_FLAG_OPEN;
428                 cam_periph_unlock(periph);
429                 cam_periph_release(periph);
430                 return(error);
431         }
432
433         cam_periph_unhold(periph);
434         cam_periph_unlock(periph);
435
436         return(error);
437 }
438
439 static int
440 chclose(struct cdev *dev, int flag, int fmt, struct thread *td)
441 {
442         struct  cam_periph *periph;
443         struct  ch_softc *softc;
444         int     error;
445
446         error = 0;
447
448         periph = (struct cam_periph *)dev->si_drv1;
449         if (periph == NULL)
450                 return(ENXIO);
451
452         softc = (struct ch_softc *)periph->softc;
453
454         cam_periph_lock(periph);
455
456         softc->flags &= ~CH_FLAG_OPEN;
457
458         cam_periph_unlock(periph);
459         cam_periph_release(periph);
460
461         return(0);
462 }
463
464 static void
465 chstart(struct cam_periph *periph, union ccb *start_ccb)
466 {
467         struct ch_softc *softc;
468
469         softc = (struct ch_softc *)periph->softc;
470
471         switch (softc->state) {
472         case CH_STATE_NORMAL:
473         {
474                 if (periph->immediate_priority <= periph->pinfo.priority){
475                         start_ccb->ccb_h.ccb_state = CH_CCB_WAITING;
476
477                         SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
478                                           periph_links.sle);
479                         periph->immediate_priority = CAM_PRIORITY_NONE;
480                         wakeup(&periph->ccb_list);
481                 }
482                 break;
483         }
484         case CH_STATE_PROBE:
485         {
486                 int mode_buffer_len;
487                 void *mode_buffer;
488
489                 /*
490                  * Include the block descriptor when calculating the mode
491                  * buffer length,
492                  */
493                 mode_buffer_len = sizeof(struct scsi_mode_header_6) +
494                                   sizeof(struct scsi_mode_blk_desc) +
495                                  sizeof(struct page_element_address_assignment);
496
497                 mode_buffer = malloc(mode_buffer_len, M_SCSICH, M_NOWAIT);
498
499                 if (mode_buffer == NULL) {
500                         printf("chstart: couldn't malloc mode sense data\n");
501                         break;
502                 }
503                 bzero(mode_buffer, mode_buffer_len);
504
505                 /*
506                  * Get the element address assignment page.
507                  */
508                 scsi_mode_sense(&start_ccb->csio,
509                                 /* retries */ 1,
510                                 /* cbfcnp */ chdone,
511                                 /* tag_action */ MSG_SIMPLE_Q_TAG,
512                                 /* dbd */ (softc->quirks & CH_Q_NO_DBD) ?
513                                         FALSE : TRUE,
514                                 /* page_code */ SMS_PAGE_CTRL_CURRENT,
515                                 /* page */ CH_ELEMENT_ADDR_ASSIGN_PAGE,
516                                 /* param_buf */ (u_int8_t *)mode_buffer,
517                                 /* param_len */ mode_buffer_len,
518                                 /* sense_len */ SSD_FULL_SIZE,
519                                 /* timeout */ CH_TIMEOUT_MODE_SENSE);
520
521                 start_ccb->ccb_h.ccb_bp = NULL;
522                 start_ccb->ccb_h.ccb_state = CH_CCB_PROBE;
523                 xpt_action(start_ccb);
524                 break;
525         }
526         }
527 }
528
529 static void
530 chdone(struct cam_periph *periph, union ccb *done_ccb)
531 {
532         struct ch_softc *softc;
533         struct ccb_scsiio *csio;
534
535         softc = (struct ch_softc *)periph->softc;
536         csio = &done_ccb->csio;
537
538         switch(done_ccb->ccb_h.ccb_state) {
539         case CH_CCB_PROBE:
540         {
541                 struct scsi_mode_header_6 *mode_header;
542                 struct page_element_address_assignment *ea;
543                 char announce_buf[80];
544
545
546                 mode_header = (struct scsi_mode_header_6 *)csio->data_ptr;
547
548                 ea = (struct page_element_address_assignment *)
549                         find_mode_page_6(mode_header);
550
551                 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP){
552                         
553                         softc->sc_firsts[CHET_MT] = scsi_2btoul(ea->mtea);
554                         softc->sc_counts[CHET_MT] = scsi_2btoul(ea->nmte);
555                         softc->sc_firsts[CHET_ST] = scsi_2btoul(ea->fsea);
556                         softc->sc_counts[CHET_ST] = scsi_2btoul(ea->nse);
557                         softc->sc_firsts[CHET_IE] = scsi_2btoul(ea->fieea);
558                         softc->sc_counts[CHET_IE] = scsi_2btoul(ea->niee);
559                         softc->sc_firsts[CHET_DT] = scsi_2btoul(ea->fdtea);
560                         softc->sc_counts[CHET_DT] = scsi_2btoul(ea->ndte);
561                         softc->sc_picker = softc->sc_firsts[CHET_MT];
562
563 #define PLURAL(c)       (c) == 1 ? "" : "s"
564                         snprintf(announce_buf, sizeof(announce_buf),
565                                 "%d slot%s, %d drive%s, "
566                                 "%d picker%s, %d portal%s",
567                                 softc->sc_counts[CHET_ST],
568                                 PLURAL(softc->sc_counts[CHET_ST]),
569                                 softc->sc_counts[CHET_DT],
570                                 PLURAL(softc->sc_counts[CHET_DT]),
571                                 softc->sc_counts[CHET_MT],
572                                 PLURAL(softc->sc_counts[CHET_MT]),
573                                 softc->sc_counts[CHET_IE],
574                                 PLURAL(softc->sc_counts[CHET_IE]));
575 #undef PLURAL
576                 } else {
577                         int error;
578
579                         error = cherror(done_ccb, CAM_RETRY_SELTO,
580                                         SF_RETRY_UA | SF_NO_PRINT);
581                         /*
582                          * Retry any UNIT ATTENTION type errors.  They
583                          * are expected at boot.
584                          */
585                         if (error == ERESTART) {
586                                 /*
587                                  * A retry was scheuled, so
588                                  * just return.
589                                  */
590                                 return;
591                         } else if (error != 0) {
592                                 int retry_scheduled;
593                                 struct scsi_mode_sense_6 *sms;
594
595                                 sms = (struct scsi_mode_sense_6 *)
596                                         done_ccb->csio.cdb_io.cdb_bytes;
597
598                                 /*
599                                  * Check to see if block descriptors were
600                                  * disabled.  Some devices don't like that.
601                                  * We're taking advantage of the fact that
602                                  * the first few bytes of the 6 and 10 byte
603                                  * mode sense commands are the same.  If
604                                  * block descriptors were disabled, enable
605                                  * them and re-send the command.
606                                  */
607                                 if (sms->byte2 & SMS_DBD) {
608                                         sms->byte2 &= ~SMS_DBD;
609                                         xpt_action(done_ccb);
610                                         softc->quirks |= CH_Q_NO_DBD;
611                                         retry_scheduled = 1;
612                                 } else
613                                         retry_scheduled = 0;
614
615                                 /* Don't wedge this device's queue */
616                                 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
617                                         cam_release_devq(done_ccb->ccb_h.path,
618                                                  /*relsim_flags*/0,
619                                                  /*reduction*/0,
620                                                  /*timeout*/0,
621                                                  /*getcount_only*/0);
622
623                                 if (retry_scheduled)
624                                         return;
625
626                                 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK)
627                                     == CAM_SCSI_STATUS_ERROR) 
628                                         scsi_sense_print(&done_ccb->csio);
629                                 else {
630                                         xpt_print(periph->path,
631                                             "got CAM status %#x\n",
632                                             done_ccb->ccb_h.status);
633                                 }
634                                 xpt_print(periph->path, "fatal error, failed "
635                                     "to attach to device\n");
636
637                                 cam_periph_invalidate(periph);
638
639                                 announce_buf[0] = '\0';
640                         }
641                 }
642                 if (announce_buf[0] != '\0')
643                         xpt_announce_periph(periph, announce_buf);
644                 softc->state = CH_STATE_NORMAL;
645                 free(mode_header, M_SCSICH);
646                 /*
647                  * Since our peripheral may be invalidated by an error
648                  * above or an external event, we must release our CCB
649                  * before releasing the probe lock on the peripheral.
650                  * The peripheral will only go away once the last lock
651                  * is removed, and we need it around for the CCB release
652                  * operation.
653                  */
654                 xpt_release_ccb(done_ccb);
655                 cam_periph_unhold(periph);
656                 return;
657         }
658         case CH_CCB_WAITING:
659         {
660                 /* Caller will release the CCB */
661                 wakeup(&done_ccb->ccb_h.cbfcnp);
662                 return;
663         }
664         default:
665                 break;
666         }
667         xpt_release_ccb(done_ccb);
668 }
669
670 static int
671 cherror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
672 {
673         struct ch_softc *softc;
674         struct cam_periph *periph;
675
676         periph = xpt_path_periph(ccb->ccb_h.path);
677         softc = (struct ch_softc *)periph->softc;
678
679         return (cam_periph_error(ccb, cam_flags, sense_flags,
680                                  &softc->saved_ccb));
681 }
682
683 static int
684 chioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
685 {
686         struct cam_periph *periph;
687         struct ch_softc *softc;
688         int error;
689
690         periph = (struct cam_periph *)dev->si_drv1;
691         if (periph == NULL)
692                 return(ENXIO);
693
694         cam_periph_lock(periph);
695         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering chioctl\n"));
696
697         softc = (struct ch_softc *)periph->softc;
698
699         error = 0;
700
701         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 
702                   ("trying to do ioctl %#lx\n", cmd));
703
704         /*
705          * If this command can change the device's state, we must
706          * have the device open for writing.
707          */
708         switch (cmd) {
709         case CHIOGPICKER:
710         case CHIOGPARAMS:
711         case CHIOGSTATUS:
712                 break;
713
714         default:
715                 if ((flag & FWRITE) == 0) {
716                         cam_periph_unlock(periph);
717                         return (EBADF);
718                 }
719         }
720
721         switch (cmd) {
722         case CHIOMOVE:
723                 error = chmove(periph, (struct changer_move *)addr);
724                 break;
725
726         case CHIOEXCHANGE:
727                 error = chexchange(periph, (struct changer_exchange *)addr);
728                 break;
729
730         case CHIOPOSITION:
731                 error = chposition(periph, (struct changer_position *)addr);
732                 break;
733
734         case CHIOGPICKER:
735                 *(int *)addr = softc->sc_picker - softc->sc_firsts[CHET_MT];
736                 break;
737
738         case CHIOSPICKER:
739         {
740                 int new_picker = *(int *)addr;
741
742                 if (new_picker > (softc->sc_counts[CHET_MT] - 1)) {
743                         error = EINVAL;
744                         break;
745                 }
746                 softc->sc_picker = softc->sc_firsts[CHET_MT] + new_picker;
747                 break;
748         }
749         case CHIOGPARAMS:
750         {
751                 struct changer_params *cp = (struct changer_params *)addr;
752
753                 cp->cp_npickers = softc->sc_counts[CHET_MT];
754                 cp->cp_nslots = softc->sc_counts[CHET_ST];
755                 cp->cp_nportals = softc->sc_counts[CHET_IE];
756                 cp->cp_ndrives = softc->sc_counts[CHET_DT];
757                 break;
758         }
759         case CHIOIELEM:
760                 error = chielem(periph, *(unsigned int *)addr);
761                 break;
762
763         case CHIOGSTATUS:
764         {
765                 error = chgetelemstatus(periph,
766                                (struct changer_element_status_request *) addr);
767                 break;
768         }
769
770         case CHIOSETVOLTAG:
771         {
772                 error = chsetvoltag(periph,
773                                     (struct changer_set_voltag_request *) addr);
774                 break;
775         }
776
777         /* Implement prevent/allow? */
778
779         default:
780                 error = cam_periph_ioctl(periph, cmd, addr, cherror);
781                 break;
782         }
783
784         cam_periph_unlock(periph);
785         return (error);
786 }
787
788 static int
789 chmove(struct cam_periph *periph, struct changer_move *cm)
790 {
791         struct ch_softc *softc;
792         u_int16_t fromelem, toelem;
793         union ccb *ccb;
794         int error;
795
796         error = 0;
797         softc = (struct ch_softc *)periph->softc;
798
799         /*
800          * Check arguments.
801          */
802         if ((cm->cm_fromtype > CHET_DT) || (cm->cm_totype > CHET_DT))
803                 return (EINVAL);
804         if ((cm->cm_fromunit > (softc->sc_counts[cm->cm_fromtype] - 1)) ||
805             (cm->cm_tounit > (softc->sc_counts[cm->cm_totype] - 1)))
806                 return (ENODEV);
807
808         /*
809          * Check the request against the changer's capabilities.
810          */
811         if ((softc->sc_movemask[cm->cm_fromtype] & (1 << cm->cm_totype)) == 0)
812                 return (ENODEV);
813
814         /*
815          * Calculate the source and destination elements.
816          */
817         fromelem = softc->sc_firsts[cm->cm_fromtype] + cm->cm_fromunit;
818         toelem = softc->sc_firsts[cm->cm_totype] + cm->cm_tounit;
819
820         ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
821
822         scsi_move_medium(&ccb->csio,
823                          /* retries */ 1,
824                          /* cbfcnp */ chdone,
825                          /* tag_action */ MSG_SIMPLE_Q_TAG,
826                          /* tea */ softc->sc_picker,
827                          /* src */ fromelem,
828                          /* dst */ toelem,
829                          /* invert */ (cm->cm_flags & CM_INVERT) ? TRUE : FALSE,
830                          /* sense_len */ SSD_FULL_SIZE,
831                          /* timeout */ CH_TIMEOUT_MOVE_MEDIUM);
832
833         error = cam_periph_runccb(ccb, cherror, /*cam_flags*/CAM_RETRY_SELTO,
834                                   /*sense_flags*/ SF_RETRY_UA,
835                                   softc->device_stats);
836
837         xpt_release_ccb(ccb);
838
839         return(error);
840 }
841
842 static int
843 chexchange(struct cam_periph *periph, struct changer_exchange *ce)
844 {
845         struct ch_softc *softc;
846         u_int16_t src, dst1, dst2;
847         union ccb *ccb;
848         int error;
849
850         error = 0;
851         softc = (struct ch_softc *)periph->softc;
852         /*
853          * Check arguments.
854          */
855         if ((ce->ce_srctype > CHET_DT) || (ce->ce_fdsttype > CHET_DT) ||
856             (ce->ce_sdsttype > CHET_DT))
857                 return (EINVAL);
858         if ((ce->ce_srcunit > (softc->sc_counts[ce->ce_srctype] - 1)) ||
859             (ce->ce_fdstunit > (softc->sc_counts[ce->ce_fdsttype] - 1)) ||
860             (ce->ce_sdstunit > (softc->sc_counts[ce->ce_sdsttype] - 1)))
861                 return (ENODEV);
862
863         /*
864          * Check the request against the changer's capabilities.
865          */
866         if (((softc->sc_exchangemask[ce->ce_srctype] &
867              (1 << ce->ce_fdsttype)) == 0) ||
868             ((softc->sc_exchangemask[ce->ce_fdsttype] &
869              (1 << ce->ce_sdsttype)) == 0))
870                 return (ENODEV);
871
872         /*
873          * Calculate the source and destination elements.
874          */
875         src = softc->sc_firsts[ce->ce_srctype] + ce->ce_srcunit;
876         dst1 = softc->sc_firsts[ce->ce_fdsttype] + ce->ce_fdstunit;
877         dst2 = softc->sc_firsts[ce->ce_sdsttype] + ce->ce_sdstunit;
878
879         ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
880
881         scsi_exchange_medium(&ccb->csio,
882                              /* retries */ 1,
883                              /* cbfcnp */ chdone,
884                              /* tag_action */ MSG_SIMPLE_Q_TAG,
885                              /* tea */ softc->sc_picker,
886                              /* src */ src,
887                              /* dst1 */ dst1,
888                              /* dst2 */ dst2,
889                              /* invert1 */ (ce->ce_flags & CE_INVERT1) ?
890                                            TRUE : FALSE,
891                              /* invert2 */ (ce->ce_flags & CE_INVERT2) ?
892                                            TRUE : FALSE,
893                              /* sense_len */ SSD_FULL_SIZE,
894                              /* timeout */ CH_TIMEOUT_EXCHANGE_MEDIUM);
895
896         error = cam_periph_runccb(ccb, cherror, /*cam_flags*/CAM_RETRY_SELTO,
897                                   /*sense_flags*/ SF_RETRY_UA,
898                                   softc->device_stats);
899
900         xpt_release_ccb(ccb);
901
902         return(error);
903 }
904
905 static int
906 chposition(struct cam_periph *periph, struct changer_position *cp)
907 {
908         struct ch_softc *softc;
909         u_int16_t dst;
910         union ccb *ccb;
911         int error;
912
913         error = 0;
914         softc = (struct ch_softc *)periph->softc;
915
916         /*
917          * Check arguments.
918          */
919         if (cp->cp_type > CHET_DT)
920                 return (EINVAL);
921         if (cp->cp_unit > (softc->sc_counts[cp->cp_type] - 1))
922                 return (ENODEV);
923
924         /*
925          * Calculate the destination element.
926          */
927         dst = softc->sc_firsts[cp->cp_type] + cp->cp_unit;
928
929         ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
930
931         scsi_position_to_element(&ccb->csio,
932                                  /* retries */ 1,
933                                  /* cbfcnp */ chdone,
934                                  /* tag_action */ MSG_SIMPLE_Q_TAG,
935                                  /* tea */ softc->sc_picker,
936                                  /* dst */ dst,
937                                  /* invert */ (cp->cp_flags & CP_INVERT) ?
938                                               TRUE : FALSE,
939                                  /* sense_len */ SSD_FULL_SIZE,
940                                  /* timeout */ CH_TIMEOUT_POSITION_TO_ELEMENT);
941
942         error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
943                                   /*sense_flags*/ SF_RETRY_UA,
944                                   softc->device_stats);
945
946         xpt_release_ccb(ccb);
947
948         return(error);
949 }
950
951 /*
952  * Copy a volume tag to a volume_tag struct, converting SCSI byte order
953  * to host native byte order in the volume serial number.  The volume
954  * label as returned by the changer is transferred to user mode as
955  * nul-terminated string.  Volume labels are truncated at the first
956  * space, as suggested by SCSI-2.
957  */
958 static  void
959 copy_voltag(struct changer_voltag *uvoltag, struct volume_tag *voltag)
960 {
961         int i;
962         for (i=0; i<CH_VOLTAG_MAXLEN; i++) {
963                 char c = voltag->vif[i];
964                 if (c && c != ' ')
965                         uvoltag->cv_volid[i] = c;
966                 else
967                         break;
968         }
969         uvoltag->cv_serial = scsi_2btoul(voltag->vsn);
970 }
971
972 /*
973  * Copy an an element status descriptor to a user-mode
974  * changer_element_status structure.
975  */
976
977 static  void
978 copy_element_status(struct ch_softc *softc,
979                     u_int16_t flags,
980                     struct read_element_status_descriptor *desc,
981                     struct changer_element_status *ces)
982 {
983         u_int16_t eaddr = scsi_2btoul(desc->eaddr);
984         u_int16_t et;
985
986         ces->ces_int_addr = eaddr;
987         /* set up logical address in element status */
988         for (et = CHET_MT; et <= CHET_DT; et++) {
989                 if ((softc->sc_firsts[et] <= eaddr)
990                     && ((softc->sc_firsts[et] + softc->sc_counts[et])
991                         > eaddr)) {
992                         ces->ces_addr = eaddr - softc->sc_firsts[et];
993                         ces->ces_type = et;
994                         break;
995                 }
996         }
997
998         ces->ces_flags = desc->flags1;
999
1000         ces->ces_sensecode = desc->sense_code;
1001         ces->ces_sensequal = desc->sense_qual;
1002
1003         if (desc->flags2 & READ_ELEMENT_STATUS_INVERT)
1004                 ces->ces_flags |= CES_INVERT;
1005
1006         if (desc->flags2 & READ_ELEMENT_STATUS_SVALID) {
1007
1008                 eaddr = scsi_2btoul(desc->ssea);
1009
1010                 /* convert source address to logical format */
1011                 for (et = CHET_MT; et <= CHET_DT; et++) {
1012                         if ((softc->sc_firsts[et] <= eaddr)
1013                             && ((softc->sc_firsts[et] + softc->sc_counts[et])
1014                                 > eaddr)) {
1015                                 ces->ces_source_addr = 
1016                                         eaddr - softc->sc_firsts[et];
1017                                 ces->ces_source_type = et;
1018                                 ces->ces_flags |= CES_SOURCE_VALID;
1019                                 break;
1020                         }
1021                 }
1022
1023                 if (!(ces->ces_flags & CES_SOURCE_VALID))
1024                         printf("ch: warning: could not map element source "
1025                                "address %ud to a valid element type\n",
1026                                eaddr);
1027         }
1028                         
1029
1030         if (flags & READ_ELEMENT_STATUS_PVOLTAG)
1031                 copy_voltag(&(ces->ces_pvoltag), &(desc->pvoltag));
1032         if (flags & READ_ELEMENT_STATUS_AVOLTAG)
1033                 copy_voltag(&(ces->ces_avoltag), &(desc->avoltag));
1034
1035         if (desc->dt_scsi_flags & READ_ELEMENT_STATUS_DT_IDVALID) {
1036                 ces->ces_flags |= CES_SCSIID_VALID;
1037                 ces->ces_scsi_id = desc->dt_scsi_addr;
1038         }
1039
1040         if (desc->dt_scsi_addr & READ_ELEMENT_STATUS_DT_LUVALID) {
1041                 ces->ces_flags |= CES_LUN_VALID;
1042                 ces->ces_scsi_lun = 
1043                         desc->dt_scsi_flags & READ_ELEMENT_STATUS_DT_LUNMASK;
1044         }
1045 }
1046
1047 static int
1048 chgetelemstatus(struct cam_periph *periph, 
1049                 struct changer_element_status_request *cesr)
1050 {
1051         struct read_element_status_header *st_hdr;
1052         struct read_element_status_page_header *pg_hdr;
1053         struct read_element_status_descriptor *desc;
1054         caddr_t data = NULL;
1055         size_t size, desclen;
1056         int avail, i, error = 0;
1057         struct changer_element_status *user_data = NULL;
1058         struct ch_softc *softc;
1059         union ccb *ccb;
1060         int chet = cesr->cesr_element_type;
1061         int want_voltags = (cesr->cesr_flags & CESR_VOLTAGS) ? 1 : 0;
1062
1063         softc = (struct ch_softc *)periph->softc;
1064
1065         /* perform argument checking */
1066
1067         /*
1068          * Perform a range check on the cesr_element_{base,count}
1069          * request argument fields.
1070          */
1071         if ((softc->sc_counts[chet] - cesr->cesr_element_base) <= 0
1072             || (cesr->cesr_element_base + cesr->cesr_element_count)
1073                 > softc->sc_counts[chet])
1074                 return (EINVAL);
1075
1076         /*
1077          * Request one descriptor for the given element type.  This
1078          * is used to determine the size of the descriptor so that
1079          * we can allocate enough storage for all of them.  We assume
1080          * that the first one can fit into 1k.
1081          */
1082         cam_periph_unlock(periph);
1083         data = (caddr_t)malloc(1024, M_DEVBUF, M_WAITOK);
1084
1085         cam_periph_lock(periph);
1086         ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1087
1088         scsi_read_element_status(&ccb->csio,
1089                                  /* retries */ 1,
1090                                  /* cbfcnp */ chdone,
1091                                  /* tag_action */ MSG_SIMPLE_Q_TAG,
1092                                  /* voltag */ want_voltags,
1093                                  /* sea */ softc->sc_firsts[chet],
1094                                  /* count */ 1,
1095                                  /* data_ptr */ data,
1096                                  /* dxfer_len */ 1024,
1097                                  /* sense_len */ SSD_FULL_SIZE,
1098                                  /* timeout */ CH_TIMEOUT_READ_ELEMENT_STATUS);
1099
1100         error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1101                                   /*sense_flags*/ SF_RETRY_UA,
1102                                   softc->device_stats);
1103
1104         if (error)
1105                 goto done;
1106         cam_periph_unlock(periph);
1107
1108         st_hdr = (struct read_element_status_header *)data;
1109         pg_hdr = (struct read_element_status_page_header *)((uintptr_t)st_hdr +
1110                   sizeof(struct read_element_status_header));
1111         desclen = scsi_2btoul(pg_hdr->edl);
1112
1113         size = sizeof(struct read_element_status_header) +
1114                sizeof(struct read_element_status_page_header) +
1115                (desclen * cesr->cesr_element_count);
1116
1117         /*
1118          * Reallocate storage for descriptors and get them from the
1119          * device.
1120          */
1121         free(data, M_DEVBUF);
1122         data = (caddr_t)malloc(size, M_DEVBUF, M_WAITOK);
1123
1124         cam_periph_lock(periph);
1125         scsi_read_element_status(&ccb->csio,
1126                                  /* retries */ 1,
1127                                  /* cbfcnp */ chdone,
1128                                  /* tag_action */ MSG_SIMPLE_Q_TAG,
1129                                  /* voltag */ want_voltags,
1130                                  /* sea */ softc->sc_firsts[chet]
1131                                  + cesr->cesr_element_base,
1132                                  /* count */ cesr->cesr_element_count,
1133                                  /* data_ptr */ data,
1134                                  /* dxfer_len */ size,
1135                                  /* sense_len */ SSD_FULL_SIZE,
1136                                  /* timeout */ CH_TIMEOUT_READ_ELEMENT_STATUS);
1137         
1138         error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1139                                   /*sense_flags*/ SF_RETRY_UA,
1140                                   softc->device_stats);
1141
1142         if (error)
1143                 goto done;
1144         cam_periph_unlock(periph);
1145
1146         /*
1147          * Fill in the user status array.
1148          */
1149         st_hdr = (struct read_element_status_header *)data;
1150         pg_hdr = (struct read_element_status_page_header *)((uintptr_t)st_hdr +
1151                   sizeof(struct read_element_status_header));
1152         avail = scsi_2btoul(st_hdr->count);
1153
1154         if (avail != cesr->cesr_element_count) {
1155                 xpt_print(periph->path,
1156                     "warning, READ ELEMENT STATUS avail != count\n");
1157         }
1158
1159         user_data = (struct changer_element_status *)
1160                 malloc(avail * sizeof(struct changer_element_status),
1161                        M_DEVBUF, M_WAITOK | M_ZERO);
1162
1163         desc = (struct read_element_status_descriptor *)((uintptr_t)data +
1164                 sizeof(struct read_element_status_header) +
1165                 sizeof(struct read_element_status_page_header));
1166         /*
1167          * Set up the individual element status structures
1168          */
1169         for (i = 0; i < avail; ++i) {
1170                 struct changer_element_status *ces = &(user_data[i]);
1171
1172                 copy_element_status(softc, pg_hdr->flags, desc, ces);
1173
1174                 desc = (struct read_element_status_descriptor *)
1175                        ((uintptr_t)desc + desclen);
1176         }
1177
1178         /* Copy element status structures out to userspace. */
1179         error = copyout(user_data,
1180                         cesr->cesr_element_status,
1181                         avail * sizeof(struct changer_element_status));
1182         cam_periph_lock(periph);
1183
1184  done:
1185         xpt_release_ccb(ccb);
1186
1187         if (data != NULL)
1188                 free(data, M_DEVBUF);
1189         if (user_data != NULL)
1190                 free(user_data, M_DEVBUF);
1191
1192         return (error);
1193 }
1194
1195 static int
1196 chielem(struct cam_periph *periph,
1197         unsigned int timeout)
1198 {
1199         union ccb *ccb;
1200         struct ch_softc *softc;
1201         int error;
1202
1203         if (!timeout) {
1204                 timeout = CH_TIMEOUT_INITIALIZE_ELEMENT_STATUS;
1205         } else {
1206                 timeout *= 1000;
1207         }
1208
1209         error = 0;
1210         softc = (struct ch_softc *)periph->softc;
1211
1212         ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1213
1214         scsi_initialize_element_status(&ccb->csio,
1215                                       /* retries */ 1,
1216                                       /* cbfcnp */ chdone,
1217                                       /* tag_action */ MSG_SIMPLE_Q_TAG,
1218                                       /* sense_len */ SSD_FULL_SIZE,
1219                                       /* timeout */ timeout);
1220
1221         error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1222                                   /*sense_flags*/ SF_RETRY_UA,
1223                                   softc->device_stats);
1224
1225         xpt_release_ccb(ccb);
1226
1227         return(error);
1228 }
1229
1230 static int
1231 chsetvoltag(struct cam_periph *periph,
1232             struct changer_set_voltag_request *csvr)
1233 {
1234         union ccb *ccb;
1235         struct ch_softc *softc;
1236         u_int16_t ea;
1237         u_int8_t sac;
1238         struct scsi_send_volume_tag_parameters ssvtp;
1239         int error;
1240         int i;
1241
1242         error = 0;
1243         softc = (struct ch_softc *)periph->softc;
1244
1245         bzero(&ssvtp, sizeof(ssvtp));
1246         for (i=0; i<sizeof(ssvtp.vitf); i++) {
1247                 ssvtp.vitf[i] = ' ';
1248         }
1249
1250         /*
1251          * Check arguments.
1252          */
1253         if (csvr->csvr_type > CHET_DT)
1254                 return EINVAL;
1255         if (csvr->csvr_addr > (softc->sc_counts[csvr->csvr_type] - 1))
1256                 return ENODEV;
1257
1258         ea = softc->sc_firsts[csvr->csvr_type] + csvr->csvr_addr;
1259
1260         if (csvr->csvr_flags & CSVR_ALTERNATE) {
1261                 switch (csvr->csvr_flags & CSVR_MODE_MASK) {
1262                 case CSVR_MODE_SET:
1263                         sac = SEND_VOLUME_TAG_ASSERT_ALTERNATE;
1264                         break;
1265                 case CSVR_MODE_REPLACE:
1266                         sac = SEND_VOLUME_TAG_REPLACE_ALTERNATE;
1267                         break;
1268                 case CSVR_MODE_CLEAR:
1269                         sac = SEND_VOLUME_TAG_UNDEFINED_ALTERNATE;
1270                         break;
1271                 default:
1272                         error = EINVAL;
1273                         goto out;
1274                 }
1275         } else {
1276                 switch (csvr->csvr_flags & CSVR_MODE_MASK) {
1277                 case CSVR_MODE_SET:
1278                         sac = SEND_VOLUME_TAG_ASSERT_PRIMARY;
1279                         break;
1280                 case CSVR_MODE_REPLACE:
1281                         sac = SEND_VOLUME_TAG_REPLACE_PRIMARY;
1282                         break;
1283                 case CSVR_MODE_CLEAR:
1284                         sac = SEND_VOLUME_TAG_UNDEFINED_PRIMARY;
1285                         break;
1286                 default:
1287                         error = EINVAL;
1288                         goto out;
1289                 }
1290         }
1291
1292         memcpy(ssvtp.vitf, csvr->csvr_voltag.cv_volid,
1293                min(strlen(csvr->csvr_voltag.cv_volid), sizeof(ssvtp.vitf)));
1294         scsi_ulto2b(csvr->csvr_voltag.cv_serial, ssvtp.minvsn);
1295
1296         ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1297
1298         scsi_send_volume_tag(&ccb->csio,
1299                              /* retries */ 1,
1300                              /* cbfcnp */ chdone,
1301                              /* tag_action */ MSG_SIMPLE_Q_TAG,
1302                              /* element_address */ ea,
1303                              /* send_action_code */ sac,
1304                              /* parameters */ &ssvtp,
1305                              /* sense_len */ SSD_FULL_SIZE,
1306                              /* timeout */ CH_TIMEOUT_SEND_VOLTAG);
1307         
1308         error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1309                                   /*sense_flags*/ SF_RETRY_UA,
1310                                   softc->device_stats);
1311
1312         xpt_release_ccb(ccb);
1313
1314  out:
1315         return error;
1316 }
1317
1318 static int
1319 chgetparams(struct cam_periph *periph)
1320 {
1321         union ccb *ccb;
1322         struct ch_softc *softc;
1323         void *mode_buffer;
1324         int mode_buffer_len;
1325         struct page_element_address_assignment *ea;
1326         struct page_device_capabilities *cap;
1327         int error, from, dbd;
1328         u_int8_t *moves, *exchanges;
1329
1330         error = 0;
1331
1332         softc = (struct ch_softc *)periph->softc;
1333
1334         ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1335
1336         /*
1337          * The scsi_mode_sense_data structure is just a convenience
1338          * structure that allows us to easily calculate the worst-case
1339          * storage size of the mode sense buffer.
1340          */
1341         mode_buffer_len = sizeof(struct scsi_mode_sense_data);
1342
1343         mode_buffer = malloc(mode_buffer_len, M_SCSICH, M_NOWAIT);
1344
1345         if (mode_buffer == NULL) {
1346                 printf("chgetparams: couldn't malloc mode sense data\n");
1347                 return(ENOSPC);
1348         }
1349
1350         bzero(mode_buffer, mode_buffer_len);
1351
1352         if (softc->quirks & CH_Q_NO_DBD)
1353                 dbd = FALSE;
1354         else
1355                 dbd = TRUE;
1356
1357         /*
1358          * Get the element address assignment page.
1359          */
1360         scsi_mode_sense(&ccb->csio,
1361                         /* retries */ 1,
1362                         /* cbfcnp */ chdone,
1363                         /* tag_action */ MSG_SIMPLE_Q_TAG,
1364                         /* dbd */ dbd,
1365                         /* page_code */ SMS_PAGE_CTRL_CURRENT,
1366                         /* page */ CH_ELEMENT_ADDR_ASSIGN_PAGE,
1367                         /* param_buf */ (u_int8_t *)mode_buffer,
1368                         /* param_len */ mode_buffer_len,
1369                         /* sense_len */ SSD_FULL_SIZE,
1370                         /* timeout */ CH_TIMEOUT_MODE_SENSE);
1371
1372         error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1373                                   /* sense_flags */ SF_RETRY_UA|SF_NO_PRINT,
1374                                   softc->device_stats);
1375
1376         if (error) {
1377                 if (dbd) {
1378                         struct scsi_mode_sense_6 *sms;
1379
1380                         sms = (struct scsi_mode_sense_6 *)
1381                                 ccb->csio.cdb_io.cdb_bytes;
1382
1383                         sms->byte2 &= ~SMS_DBD;
1384                         error = cam_periph_runccb(ccb, cherror,
1385                                                   /*cam_flags*/ CAM_RETRY_SELTO,
1386                                                   /*sense_flags*/ SF_RETRY_UA,
1387                                                   softc->device_stats);
1388                 } else {
1389                         /*
1390                          * Since we disabled sense printing above, print
1391                          * out the sense here since we got an error.
1392                          */
1393                         scsi_sense_print(&ccb->csio);
1394                 }
1395
1396                 if (error) {
1397                         xpt_print(periph->path,
1398                             "chgetparams: error getting element "
1399                             "address page\n");
1400                         xpt_release_ccb(ccb);
1401                         free(mode_buffer, M_SCSICH);
1402                         return(error);
1403                 }
1404         }
1405
1406         ea = (struct page_element_address_assignment *)
1407                 find_mode_page_6((struct scsi_mode_header_6 *)mode_buffer);
1408
1409         softc->sc_firsts[CHET_MT] = scsi_2btoul(ea->mtea);
1410         softc->sc_counts[CHET_MT] = scsi_2btoul(ea->nmte);
1411         softc->sc_firsts[CHET_ST] = scsi_2btoul(ea->fsea);
1412         softc->sc_counts[CHET_ST] = scsi_2btoul(ea->nse);
1413         softc->sc_firsts[CHET_IE] = scsi_2btoul(ea->fieea);
1414         softc->sc_counts[CHET_IE] = scsi_2btoul(ea->niee);
1415         softc->sc_firsts[CHET_DT] = scsi_2btoul(ea->fdtea);
1416         softc->sc_counts[CHET_DT] = scsi_2btoul(ea->ndte);
1417
1418         bzero(mode_buffer, mode_buffer_len);
1419
1420         /*
1421          * Now get the device capabilities page.
1422          */
1423         scsi_mode_sense(&ccb->csio,
1424                         /* retries */ 1,
1425                         /* cbfcnp */ chdone,
1426                         /* tag_action */ MSG_SIMPLE_Q_TAG,
1427                         /* dbd */ dbd,
1428                         /* page_code */ SMS_PAGE_CTRL_CURRENT,
1429                         /* page */ CH_DEVICE_CAP_PAGE,
1430                         /* param_buf */ (u_int8_t *)mode_buffer,
1431                         /* param_len */ mode_buffer_len,
1432                         /* sense_len */ SSD_FULL_SIZE,
1433                         /* timeout */ CH_TIMEOUT_MODE_SENSE);
1434         
1435         error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1436                                   /* sense_flags */ SF_RETRY_UA | SF_NO_PRINT,
1437                                   softc->device_stats);
1438
1439         if (error) {
1440                 if (dbd) {
1441                         struct scsi_mode_sense_6 *sms;
1442
1443                         sms = (struct scsi_mode_sense_6 *)
1444                                 ccb->csio.cdb_io.cdb_bytes;
1445
1446                         sms->byte2 &= ~SMS_DBD;
1447                         error = cam_periph_runccb(ccb, cherror,
1448                                                   /*cam_flags*/ CAM_RETRY_SELTO,
1449                                                   /*sense_flags*/ SF_RETRY_UA,
1450                                                   softc->device_stats);
1451                 } else {
1452                         /*
1453                          * Since we disabled sense printing above, print
1454                          * out the sense here since we got an error.
1455                          */
1456                         scsi_sense_print(&ccb->csio);
1457                 }
1458
1459                 if (error) {
1460                         xpt_print(periph->path,
1461                             "chgetparams: error getting device "
1462                             "capabilities page\n");
1463                         xpt_release_ccb(ccb);
1464                         free(mode_buffer, M_SCSICH);
1465                         return(error);
1466                 }
1467         }
1468
1469         xpt_release_ccb(ccb);
1470
1471         cap = (struct page_device_capabilities *)
1472                 find_mode_page_6((struct scsi_mode_header_6 *)mode_buffer);
1473
1474         bzero(softc->sc_movemask, sizeof(softc->sc_movemask));
1475         bzero(softc->sc_exchangemask, sizeof(softc->sc_exchangemask));
1476         moves = cap->move_from;
1477         exchanges = cap->exchange_with;
1478         for (from = CHET_MT; from <= CHET_MAX; ++from) {
1479                 softc->sc_movemask[from] = moves[from];
1480                 softc->sc_exchangemask[from] = exchanges[from];
1481         }
1482
1483         free(mode_buffer, M_SCSICH);
1484
1485         return(error);
1486 }
1487
1488 void
1489 scsi_move_medium(struct ccb_scsiio *csio, u_int32_t retries,
1490                  void (*cbfcnp)(struct cam_periph *, union ccb *),
1491                  u_int8_t tag_action, u_int32_t tea, u_int32_t src,
1492                  u_int32_t dst, int invert, u_int8_t sense_len,
1493                  u_int32_t timeout)
1494 {
1495         struct scsi_move_medium *scsi_cmd;
1496
1497         scsi_cmd = (struct scsi_move_medium *)&csio->cdb_io.cdb_bytes;
1498         bzero(scsi_cmd, sizeof(*scsi_cmd));
1499
1500         scsi_cmd->opcode = MOVE_MEDIUM;
1501
1502         scsi_ulto2b(tea, scsi_cmd->tea);
1503         scsi_ulto2b(src, scsi_cmd->src);
1504         scsi_ulto2b(dst, scsi_cmd->dst);
1505
1506         if (invert)
1507                 scsi_cmd->invert |= MOVE_MEDIUM_INVERT;
1508
1509         cam_fill_csio(csio,
1510                       retries,
1511                       cbfcnp,
1512                       /*flags*/ CAM_DIR_NONE,
1513                       tag_action,
1514                       /*data_ptr*/ NULL,
1515                       /*dxfer_len*/ 0,
1516                       sense_len,
1517                       sizeof(*scsi_cmd),
1518                       timeout);
1519 }
1520
1521 void
1522 scsi_exchange_medium(struct ccb_scsiio *csio, u_int32_t retries,
1523                      void (*cbfcnp)(struct cam_periph *, union ccb *),
1524                      u_int8_t tag_action, u_int32_t tea, u_int32_t src,
1525                      u_int32_t dst1, u_int32_t dst2, int invert1,
1526                      int invert2, u_int8_t sense_len, u_int32_t timeout)
1527 {
1528         struct scsi_exchange_medium *scsi_cmd;
1529
1530         scsi_cmd = (struct scsi_exchange_medium *)&csio->cdb_io.cdb_bytes;
1531         bzero(scsi_cmd, sizeof(*scsi_cmd));
1532
1533         scsi_cmd->opcode = EXCHANGE_MEDIUM;
1534
1535         scsi_ulto2b(tea, scsi_cmd->tea);
1536         scsi_ulto2b(src, scsi_cmd->src);
1537         scsi_ulto2b(dst1, scsi_cmd->fdst);
1538         scsi_ulto2b(dst2, scsi_cmd->sdst);
1539
1540         if (invert1)
1541                 scsi_cmd->invert |= EXCHANGE_MEDIUM_INV1;
1542
1543         if (invert2)
1544                 scsi_cmd->invert |= EXCHANGE_MEDIUM_INV2;
1545
1546         cam_fill_csio(csio,
1547                       retries,
1548                       cbfcnp,
1549                       /*flags*/ CAM_DIR_NONE,
1550                       tag_action,
1551                       /*data_ptr*/ NULL,
1552                       /*dxfer_len*/ 0,
1553                       sense_len,
1554                       sizeof(*scsi_cmd),
1555                       timeout);
1556 }
1557
1558 void
1559 scsi_position_to_element(struct ccb_scsiio *csio, u_int32_t retries,
1560                          void (*cbfcnp)(struct cam_periph *, union ccb *),
1561                          u_int8_t tag_action, u_int32_t tea, u_int32_t dst,
1562                          int invert, u_int8_t sense_len, u_int32_t timeout)
1563 {
1564         struct scsi_position_to_element *scsi_cmd;
1565
1566         scsi_cmd = (struct scsi_position_to_element *)&csio->cdb_io.cdb_bytes;
1567         bzero(scsi_cmd, sizeof(*scsi_cmd));
1568
1569         scsi_cmd->opcode = POSITION_TO_ELEMENT;
1570
1571         scsi_ulto2b(tea, scsi_cmd->tea);
1572         scsi_ulto2b(dst, scsi_cmd->dst);
1573
1574         if (invert)
1575                 scsi_cmd->invert |= POSITION_TO_ELEMENT_INVERT;
1576
1577         cam_fill_csio(csio,
1578                       retries,
1579                       cbfcnp,
1580                       /*flags*/ CAM_DIR_NONE,
1581                       tag_action,
1582                       /*data_ptr*/ NULL,
1583                       /*dxfer_len*/ 0,
1584                       sense_len,
1585                       sizeof(*scsi_cmd),
1586                       timeout);
1587 }
1588
1589 void
1590 scsi_read_element_status(struct ccb_scsiio *csio, u_int32_t retries,
1591                          void (*cbfcnp)(struct cam_periph *, union ccb *),
1592                          u_int8_t tag_action, int voltag, u_int32_t sea,
1593                          u_int32_t count, u_int8_t *data_ptr,
1594                          u_int32_t dxfer_len, u_int8_t sense_len,
1595                          u_int32_t timeout)
1596 {
1597         struct scsi_read_element_status *scsi_cmd;
1598
1599         scsi_cmd = (struct scsi_read_element_status *)&csio->cdb_io.cdb_bytes;
1600         bzero(scsi_cmd, sizeof(*scsi_cmd));
1601
1602         scsi_cmd->opcode = READ_ELEMENT_STATUS;
1603
1604         scsi_ulto2b(sea, scsi_cmd->sea);
1605         scsi_ulto2b(count, scsi_cmd->count);
1606         scsi_ulto3b(dxfer_len, scsi_cmd->len);
1607
1608         if (voltag)
1609                 scsi_cmd->byte2 |= READ_ELEMENT_STATUS_VOLTAG;
1610
1611         cam_fill_csio(csio,
1612                       retries,
1613                       cbfcnp,
1614                       /*flags*/ CAM_DIR_IN,
1615                       tag_action,
1616                       data_ptr,
1617                       dxfer_len,
1618                       sense_len,
1619                       sizeof(*scsi_cmd),
1620                       timeout);
1621 }
1622
1623 void
1624 scsi_initialize_element_status(struct ccb_scsiio *csio, u_int32_t retries,
1625                                void (*cbfcnp)(struct cam_periph *, union ccb *),
1626                                u_int8_t tag_action, u_int8_t sense_len,
1627                                u_int32_t timeout)
1628 {
1629         struct scsi_initialize_element_status *scsi_cmd;
1630
1631         scsi_cmd = (struct scsi_initialize_element_status *)
1632                     &csio->cdb_io.cdb_bytes;
1633         bzero(scsi_cmd, sizeof(*scsi_cmd));
1634
1635         scsi_cmd->opcode = INITIALIZE_ELEMENT_STATUS;
1636
1637         cam_fill_csio(csio,
1638                       retries,
1639                       cbfcnp,
1640                       /*flags*/ CAM_DIR_NONE,
1641                       tag_action,
1642                       /* data_ptr */ NULL,
1643                       /* dxfer_len */ 0,
1644                       sense_len,
1645                       sizeof(*scsi_cmd),
1646                       timeout);
1647 }
1648
1649 void
1650 scsi_send_volume_tag(struct ccb_scsiio *csio, u_int32_t retries,
1651                      void (*cbfcnp)(struct cam_periph *, union ccb *),
1652                      u_int8_t tag_action, 
1653                      u_int16_t element_address,
1654                      u_int8_t send_action_code,
1655                      struct scsi_send_volume_tag_parameters *parameters,
1656                      u_int8_t sense_len, u_int32_t timeout)
1657 {
1658         struct scsi_send_volume_tag *scsi_cmd;
1659
1660         scsi_cmd = (struct scsi_send_volume_tag *) &csio->cdb_io.cdb_bytes;
1661         bzero(scsi_cmd, sizeof(*scsi_cmd));
1662
1663         scsi_cmd->opcode = SEND_VOLUME_TAG;
1664         scsi_ulto2b(element_address, scsi_cmd->ea);
1665         scsi_cmd->sac = send_action_code;
1666         scsi_ulto2b(sizeof(*parameters), scsi_cmd->pll);
1667
1668         cam_fill_csio(csio,
1669                       retries,
1670                       cbfcnp,
1671                       /*flags*/ CAM_DIR_OUT,
1672                       tag_action,
1673                       /* data_ptr */ (u_int8_t *) parameters,
1674                       sizeof(*parameters),
1675                       sense_len,
1676                       sizeof(*scsi_cmd),
1677                       timeout);
1678 }