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