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