ch.c revision 1.59 1 /* $NetBSD: ch.c,v 1.59 2004/04/23 21:52:17 itojun Exp $ */
2
3 /*-
4 * Copyright (c) 1996, 1997, 1998, 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: ch.c,v 1.59 2004/04/23 21:52:17 itojun Exp $");
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/errno.h>
47 #include <sys/ioctl.h>
48 #include <sys/buf.h>
49 #include <sys/proc.h>
50 #include <sys/user.h>
51 #include <sys/chio.h>
52 #include <sys/device.h>
53 #include <sys/malloc.h>
54 #include <sys/conf.h>
55 #include <sys/fcntl.h>
56 #include <sys/vnode.h>
57 #include <sys/time.h>
58 #include <sys/select.h>
59 #include <sys/poll.h>
60
61 #include <dev/scsipi/scsipi_all.h>
62 #include <dev/scsipi/scsi_all.h>
63 #include <dev/scsipi/scsi_changer.h>
64 #include <dev/scsipi/scsiconf.h>
65
66 #define CHRETRIES 2
67 #define CHUNIT(x) (minor((x)))
68
69 struct ch_softc {
70 struct device sc_dev; /* generic device info */
71 struct scsipi_periph *sc_periph;/* our periph data */
72
73 u_int sc_events; /* event bitmask */
74 struct selinfo sc_selq; /* select/poll queue for events */
75
76 int sc_flags; /* misc. info */
77
78 int sc_picker; /* current picker */
79
80 /*
81 * The following information is obtained from the
82 * element address assignment page.
83 */
84 int sc_firsts[4]; /* firsts, indexed by CHET_* */
85 int sc_counts[4]; /* counts, indexed by CHET_* */
86
87 /*
88 * The following mask defines the legal combinations
89 * of elements for the MOVE MEDIUM command.
90 */
91 u_int8_t sc_movemask[4];
92
93 /*
94 * As above, but for EXCHANGE MEDIUM.
95 */
96 u_int8_t sc_exchangemask[4];
97
98 /*
99 * Quirks; see below.
100 */
101 int sc_settledelay; /* delay for settle */
102
103 };
104
105 /* sc_flags */
106 #define CHF_ROTATE 0x01 /* picker can rotate */
107
108 /* Autoconfiguration glue */
109 int chmatch __P((struct device *, struct cfdata *, void *));
110 void chattach __P((struct device *, struct device *, void *));
111
112 CFATTACH_DECL(ch, sizeof(struct ch_softc),
113 chmatch, chattach, NULL, NULL);
114
115 extern struct cfdriver ch_cd;
116
117 struct scsipi_inquiry_pattern ch_patterns[] = {
118 {T_CHANGER, T_REMOV,
119 "", "", ""},
120 };
121
122 dev_type_open(chopen);
123 dev_type_close(chclose);
124 dev_type_read(chread);
125 dev_type_ioctl(chioctl);
126 dev_type_poll(chpoll);
127 dev_type_kqfilter(chkqfilter);
128
129 const struct cdevsw ch_cdevsw = {
130 chopen, chclose, chread, nowrite, chioctl,
131 nostop, notty, chpoll, nommap, chkqfilter,
132 };
133
134 /* SCSI glue */
135 int ch_interpret_sense __P((struct scsipi_xfer *));
136
137 const struct scsipi_periphsw ch_switch = {
138 ch_interpret_sense, /* check our error handler first */
139 NULL, /* no queue; our commands are synchronous */
140 NULL, /* have no async handler */
141 NULL, /* nothing to be done when xfer is done */
142 };
143
144 int ch_move __P((struct ch_softc *, struct changer_move_request *));
145 int ch_exchange __P((struct ch_softc *, struct changer_exchange_request *));
146 int ch_position __P((struct ch_softc *, struct changer_position_request *));
147 int ch_ielem __P((struct ch_softc *));
148 int ch_ousergetelemstatus __P((struct ch_softc *, int, u_int8_t *));
149 int ch_usergetelemstatus __P((struct ch_softc *,
150 struct changer_element_status_request *));
151 int ch_getelemstatus __P((struct ch_softc *, int, int, void *,
152 size_t, int, int));
153 int ch_setvoltag __P((struct ch_softc *,
154 struct changer_set_voltag_request *));
155 int ch_get_params __P((struct ch_softc *, int));
156 void ch_get_quirks __P((struct ch_softc *,
157 struct scsipi_inquiry_pattern *));
158 void ch_event __P((struct ch_softc *, u_int));
159 int ch_map_element __P((struct ch_softc *, u_int16_t, int *, int *));
160
161 void ch_voltag_convert_in __P((const struct changer_volume_tag *,
162 struct changer_voltag *));
163 int ch_voltag_convert_out __P((const struct changer_voltag *,
164 struct changer_volume_tag *));
165
166 /*
167 * SCSI changer quirks.
168 */
169 struct chquirk {
170 struct scsipi_inquiry_pattern cq_match; /* device id pattern */
171 int cq_settledelay; /* settle delay, in seconds */
172 };
173
174 const struct chquirk chquirks[] = {
175 {{T_CHANGER, T_REMOV,
176 "SPECTRA", "9000", "0200"},
177 75},
178 };
179
180 int
181 chmatch(parent, match, aux)
182 struct device *parent;
183 struct cfdata *match;
184 void *aux;
185 {
186 struct scsipibus_attach_args *sa = aux;
187 int priority;
188
189 (void)scsipi_inqmatch(&sa->sa_inqbuf,
190 (caddr_t)ch_patterns, sizeof(ch_patterns) / sizeof(ch_patterns[0]),
191 sizeof(ch_patterns[0]), &priority);
192
193 return (priority);
194 }
195
196 void
197 chattach(parent, self, aux)
198 struct device *parent, *self;
199 void *aux;
200 {
201 struct ch_softc *sc = (struct ch_softc *)self;
202 struct scsipibus_attach_args *sa = aux;
203 struct scsipi_periph *periph = sa->sa_periph;
204
205 /* Glue into the SCSI bus */
206 sc->sc_periph = periph;
207 periph->periph_dev = &sc->sc_dev;
208 periph->periph_switch = &ch_switch;
209
210 printf("\n");
211
212 /*
213 * Find out our device's quirks.
214 */
215 ch_get_quirks(sc, &sa->sa_inqbuf);
216
217 /*
218 * Some changers require a long time to settle out, to do
219 * tape inventory, for instance.
220 */
221 if (sc->sc_settledelay) {
222 printf("%s: waiting %d seconds for changer to settle...\n",
223 sc->sc_dev.dv_xname, sc->sc_settledelay);
224 delay(1000000 * sc->sc_settledelay);
225 }
226
227 /*
228 * Get information about the device. Note we can't use
229 * interrupts yet.
230 */
231 if (ch_get_params(sc, XS_CTL_DISCOVERY|XS_CTL_IGNORE_MEDIA_CHANGE))
232 printf("%s: offline\n", sc->sc_dev.dv_xname);
233 else {
234 #define PLURAL(c) (c) == 1 ? "" : "s"
235 printf("%s: %d slot%s, %d drive%s, %d picker%s, %d portal%s\n",
236 sc->sc_dev.dv_xname,
237 sc->sc_counts[CHET_ST], PLURAL(sc->sc_counts[CHET_ST]),
238 sc->sc_counts[CHET_DT], PLURAL(sc->sc_counts[CHET_DT]),
239 sc->sc_counts[CHET_MT], PLURAL(sc->sc_counts[CHET_MT]),
240 sc->sc_counts[CHET_IE], PLURAL(sc->sc_counts[CHET_IE]));
241 #undef PLURAL
242 #ifdef CHANGER_DEBUG
243 printf("%s: move mask: 0x%x 0x%x 0x%x 0x%x\n",
244 sc->sc_dev.dv_xname,
245 sc->sc_movemask[CHET_MT], sc->sc_movemask[CHET_ST],
246 sc->sc_movemask[CHET_IE], sc->sc_movemask[CHET_DT]);
247 printf("%s: exchange mask: 0x%x 0x%x 0x%x 0x%x\n",
248 sc->sc_dev.dv_xname,
249 sc->sc_exchangemask[CHET_MT], sc->sc_exchangemask[CHET_ST],
250 sc->sc_exchangemask[CHET_IE], sc->sc_exchangemask[CHET_DT]);
251 #endif /* CHANGER_DEBUG */
252 }
253
254 /* Default the current picker. */
255 sc->sc_picker = sc->sc_firsts[CHET_MT];
256 }
257
258 int
259 chopen(dev, flags, fmt, p)
260 dev_t dev;
261 int flags, fmt;
262 struct proc *p;
263 {
264 struct ch_softc *sc;
265 struct scsipi_periph *periph;
266 struct scsipi_adapter *adapt;
267 int unit, error;
268
269 unit = CHUNIT(dev);
270 if ((unit >= ch_cd.cd_ndevs) ||
271 ((sc = ch_cd.cd_devs[unit]) == NULL))
272 return (ENXIO);
273
274 periph = sc->sc_periph;
275 adapt = periph->periph_channel->chan_adapter;
276
277 /*
278 * Only allow one open at a time.
279 */
280 if (periph->periph_flags & PERIPH_OPEN)
281 return (EBUSY);
282
283 if ((error = scsipi_adapter_addref(adapt)) != 0)
284 return (error);
285
286 /*
287 * Make sure the unit is on-line. If a UNIT ATTENTION
288 * occurs, we will mark that an Init-Element-Status is
289 * needed in ch_get_params().
290 *
291 * We ignore NOT READY in case e.g a magazine isn't actually
292 * loaded into the changer or a tape isn't in the drive.
293 */
294 error = scsipi_test_unit_ready(periph, XS_CTL_IGNORE_NOT_READY);
295 if (error)
296 goto bad;
297
298 periph->periph_flags |= PERIPH_OPEN;
299
300 /*
301 * Make sure our parameters are up to date.
302 */
303 if ((error = ch_get_params(sc, 0)) != 0)
304 goto bad;
305
306 return (0);
307
308 bad:
309 scsipi_adapter_delref(adapt);
310 periph->periph_flags &= ~PERIPH_OPEN;
311 return (error);
312 }
313
314 int
315 chclose(dev, flags, fmt, p)
316 dev_t dev;
317 int flags, fmt;
318 struct proc *p;
319 {
320 struct ch_softc *sc = ch_cd.cd_devs[CHUNIT(dev)];
321 struct scsipi_periph *periph = sc->sc_periph;
322 struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
323
324 scsipi_wait_drain(periph);
325
326 scsipi_adapter_delref(adapt);
327
328 sc->sc_events = 0;
329
330 periph->periph_flags &= ~PERIPH_OPEN;
331 return (0);
332 }
333
334 int
335 chread(dev, uio, flags)
336 dev_t dev;
337 struct uio *uio;
338 int flags;
339 {
340 struct ch_softc *sc = ch_cd.cd_devs[CHUNIT(dev)];
341 int error;
342
343 if (uio->uio_resid != CHANGER_EVENT_SIZE)
344 return (EINVAL);
345
346 /*
347 * Read never blocks; if there are no events pending, we just
348 * return an all-clear bitmask.
349 */
350 error = uiomove(&sc->sc_events, CHANGER_EVENT_SIZE, uio);
351 if (error == 0)
352 sc->sc_events = 0;
353 return (error);
354 }
355
356 int
357 chioctl(dev, cmd, data, flags, p)
358 dev_t dev;
359 u_long cmd;
360 caddr_t data;
361 int flags;
362 struct proc *p;
363 {
364 struct ch_softc *sc = ch_cd.cd_devs[CHUNIT(dev)];
365 int error = 0;
366
367 /*
368 * If this command can change the device's state, we must
369 * have the device open for writing.
370 */
371 switch (cmd) {
372 case CHIOGPICKER:
373 case CHIOGPARAMS:
374 case OCHIOGSTATUS:
375 break;
376
377 default:
378 if ((flags & FWRITE) == 0)
379 return (EBADF);
380 }
381
382 switch (cmd) {
383 case CHIOMOVE:
384 error = ch_move(sc, (struct changer_move_request *)data);
385 break;
386
387 case CHIOEXCHANGE:
388 error = ch_exchange(sc,
389 (struct changer_exchange_request *)data);
390 break;
391
392 case CHIOPOSITION:
393 error = ch_position(sc,
394 (struct changer_position_request *)data);
395 break;
396
397 case CHIOGPICKER:
398 *(int *)data = sc->sc_picker - sc->sc_firsts[CHET_MT];
399 break;
400
401 case CHIOSPICKER:
402 {
403 int new_picker = *(int *)data;
404
405 if (new_picker > (sc->sc_counts[CHET_MT] - 1))
406 return (EINVAL);
407 sc->sc_picker = sc->sc_firsts[CHET_MT] + new_picker;
408 break;
409 }
410
411 case CHIOGPARAMS:
412 {
413 struct changer_params *cp = (struct changer_params *)data;
414
415 cp->cp_curpicker = sc->sc_picker - sc->sc_firsts[CHET_MT];
416 cp->cp_npickers = sc->sc_counts[CHET_MT];
417 cp->cp_nslots = sc->sc_counts[CHET_ST];
418 cp->cp_nportals = sc->sc_counts[CHET_IE];
419 cp->cp_ndrives = sc->sc_counts[CHET_DT];
420 break;
421 }
422
423 case CHIOIELEM:
424 error = ch_ielem(sc);
425 if (error == 0) {
426 sc->sc_periph->periph_flags |= PERIPH_MEDIA_LOADED;
427 }
428 break;
429
430 case OCHIOGSTATUS:
431 {
432 struct ochanger_element_status_request *cesr =
433 (struct ochanger_element_status_request *)data;
434
435 error = ch_ousergetelemstatus(sc, cesr->cesr_type,
436 cesr->cesr_data);
437 break;
438 }
439
440 case CHIOGSTATUS:
441 error = ch_usergetelemstatus(sc,
442 (struct changer_element_status_request *)data);
443 break;
444
445 case CHIOSVOLTAG:
446 error = ch_setvoltag(sc,
447 (struct changer_set_voltag_request *)data);
448 break;
449
450 /* Implement prevent/allow? */
451
452 default:
453 error = scsipi_do_ioctl(sc->sc_periph, dev, cmd, data,
454 flags, p);
455 break;
456 }
457
458 return (error);
459 }
460
461 int
462 chpoll(dev, events, p)
463 dev_t dev;
464 int events;
465 struct proc *p;
466 {
467 struct ch_softc *sc = ch_cd.cd_devs[CHUNIT(dev)];
468 int revents;
469
470 revents = events & (POLLOUT | POLLWRNORM);
471
472 if ((events & (POLLIN | POLLRDNORM)) == 0)
473 return (revents);
474
475 if (sc->sc_events == 0)
476 revents |= events & (POLLIN | POLLRDNORM);
477 else
478 selrecord(p, &sc->sc_selq);
479
480 return (revents);
481 }
482
483 static void
484 filt_chdetach(struct knote *kn)
485 {
486 struct ch_softc *sc = kn->kn_hook;
487
488 SLIST_REMOVE(&sc->sc_selq.sel_klist, kn, knote, kn_selnext);
489 }
490
491 static int
492 filt_chread(struct knote *kn, long hint)
493 {
494 struct ch_softc *sc = kn->kn_hook;
495
496 if (sc->sc_events == 0)
497 return (0);
498 kn->kn_data = CHANGER_EVENT_SIZE;
499 return (1);
500 }
501
502 static const struct filterops chread_filtops =
503 { 1, NULL, filt_chdetach, filt_chread };
504
505 static const struct filterops chwrite_filtops =
506 { 1, NULL, filt_chdetach, filt_seltrue };
507
508 int
509 chkqfilter(dev_t dev, struct knote *kn)
510 {
511 struct ch_softc *sc = ch_cd.cd_devs[CHUNIT(dev)];
512 struct klist *klist;
513
514 switch (kn->kn_filter) {
515 case EVFILT_READ:
516 klist = &sc->sc_selq.sel_klist;
517 kn->kn_fop = &chread_filtops;
518 break;
519
520 case EVFILT_WRITE:
521 klist = &sc->sc_selq.sel_klist;
522 kn->kn_fop = &chwrite_filtops;
523 break;
524
525 default:
526 return (1);
527 }
528
529 kn->kn_hook = sc;
530
531 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
532
533 return (0);
534 }
535
536 int
537 ch_interpret_sense(xs)
538 struct scsipi_xfer *xs;
539 {
540 struct scsipi_periph *periph = xs->xs_periph;
541 struct scsipi_sense_data *sense = &xs->sense.scsi_sense;
542 struct ch_softc *sc = (void *)periph->periph_dev;
543 u_int16_t asc_ascq;
544
545 /*
546 * If the periph is already recovering, just do the
547 * normal error recovering.
548 */
549 if (periph->periph_flags & PERIPH_RECOVERING)
550 return (EJUSTRETURN);
551
552 /*
553 * If it isn't an extended or extended/deferred error, let
554 * the generic code handle it.
555 */
556 if ((sense->error_code & SSD_ERRCODE) != 0x70 &&
557 (sense->error_code & SSD_ERRCODE) != 0x71)
558 return (EJUSTRETURN);
559
560 /*
561 * We're only interested in condtions that
562 * indicate potential inventory violation.
563 *
564 * We use ASC/ASCQ codes for this.
565 */
566
567 asc_ascq = (((u_int16_t) sense->add_sense_code) << 8) |
568 sense->add_sense_code_qual;
569
570 switch (asc_ascq) {
571 case 0x2800:
572 /* "Not Ready To Ready Transition (Medium May Have Changed)" */
573 case 0x2900:
574 /* "Power On, Reset, or Bus Device Reset Occurred" */
575 sc->sc_periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
576 /*
577 * Enqueue an Element-Status-Changed event, and wake up
578 * any processes waiting for them.
579 */
580 if ((xs->xs_control & XS_CTL_IGNORE_MEDIA_CHANGE) == 0)
581 ch_event(sc, CHEV_ELEMENT_STATUS_CHANGED);
582 if ((periph->periph_flags & PERIPH_OPEN) == 0) {
583 /*
584 * if the device is not yet open, we can ignore this
585 * information.
586 */
587 return (0);
588 }
589 break;
590 default:
591 break;
592 }
593
594 return (EJUSTRETURN);
595 }
596
597 void
598 ch_event(sc, event)
599 struct ch_softc *sc;
600 u_int event;
601 {
602
603 sc->sc_events |= event;
604 selnotify(&sc->sc_selq, 0);
605 }
606
607 int
608 ch_move(sc, cm)
609 struct ch_softc *sc;
610 struct changer_move_request *cm;
611 {
612 struct scsi_move_medium cmd;
613 u_int16_t fromelem, toelem;
614
615 /*
616 * Check arguments.
617 */
618 if ((cm->cm_fromtype > CHET_DT) || (cm->cm_totype > CHET_DT))
619 return (EINVAL);
620 if ((cm->cm_fromunit > (sc->sc_counts[cm->cm_fromtype] - 1)) ||
621 (cm->cm_tounit > (sc->sc_counts[cm->cm_totype] - 1)))
622 return (ENODEV);
623
624 /*
625 * Check the request against the changer's capabilities.
626 */
627 if ((sc->sc_movemask[cm->cm_fromtype] & (1 << cm->cm_totype)) == 0)
628 return (ENODEV);
629
630 /*
631 * Calculate the source and destination elements.
632 */
633 fromelem = sc->sc_firsts[cm->cm_fromtype] + cm->cm_fromunit;
634 toelem = sc->sc_firsts[cm->cm_totype] + cm->cm_tounit;
635
636 /*
637 * Build the SCSI command.
638 */
639 memset(&cmd, 0, sizeof(cmd));
640 cmd.opcode = MOVE_MEDIUM;
641 _lto2b(sc->sc_picker, cmd.tea);
642 _lto2b(fromelem, cmd.src);
643 _lto2b(toelem, cmd.dst);
644 if (cm->cm_flags & CM_INVERT)
645 cmd.flags |= MOVE_MEDIUM_INVERT;
646
647 /*
648 * Send command to changer.
649 */
650 return (scsipi_command(sc->sc_periph,
651 (struct scsipi_generic *)&cmd, sizeof(cmd), NULL, 0, CHRETRIES,
652 100000, NULL, 0));
653 }
654
655 int
656 ch_exchange(sc, ce)
657 struct ch_softc *sc;
658 struct changer_exchange_request *ce;
659 {
660 struct scsi_exchange_medium cmd;
661 u_int16_t src, dst1, dst2;
662
663 /*
664 * Check arguments.
665 */
666 if ((ce->ce_srctype > CHET_DT) || (ce->ce_fdsttype > CHET_DT) ||
667 (ce->ce_sdsttype > CHET_DT))
668 return (EINVAL);
669 if ((ce->ce_srcunit > (sc->sc_counts[ce->ce_srctype] - 1)) ||
670 (ce->ce_fdstunit > (sc->sc_counts[ce->ce_fdsttype] - 1)) ||
671 (ce->ce_sdstunit > (sc->sc_counts[ce->ce_sdsttype] - 1)))
672 return (ENODEV);
673
674 /*
675 * Check the request against the changer's capabilities.
676 */
677 if (((sc->sc_exchangemask[ce->ce_srctype] &
678 (1 << ce->ce_fdsttype)) == 0) ||
679 ((sc->sc_exchangemask[ce->ce_fdsttype] &
680 (1 << ce->ce_sdsttype)) == 0))
681 return (ENODEV);
682
683 /*
684 * Calculate the source and destination elements.
685 */
686 src = sc->sc_firsts[ce->ce_srctype] + ce->ce_srcunit;
687 dst1 = sc->sc_firsts[ce->ce_fdsttype] + ce->ce_fdstunit;
688 dst2 = sc->sc_firsts[ce->ce_sdsttype] + ce->ce_sdstunit;
689
690 /*
691 * Build the SCSI command.
692 */
693 memset(&cmd, 0, sizeof(cmd));
694 cmd.opcode = EXCHANGE_MEDIUM;
695 _lto2b(sc->sc_picker, cmd.tea);
696 _lto2b(src, cmd.src);
697 _lto2b(dst1, cmd.fdst);
698 _lto2b(dst2, cmd.sdst);
699 if (ce->ce_flags & CE_INVERT1)
700 cmd.flags |= EXCHANGE_MEDIUM_INV1;
701 if (ce->ce_flags & CE_INVERT2)
702 cmd.flags |= EXCHANGE_MEDIUM_INV2;
703
704 /*
705 * Send command to changer.
706 */
707 return (scsipi_command(sc->sc_periph,
708 (struct scsipi_generic *)&cmd, sizeof(cmd), NULL, 0, CHRETRIES,
709 100000, NULL, 0));
710 }
711
712 int
713 ch_position(sc, cp)
714 struct ch_softc *sc;
715 struct changer_position_request *cp;
716 {
717 struct scsi_position_to_element cmd;
718 u_int16_t dst;
719
720 /*
721 * Check arguments.
722 */
723 if (cp->cp_type > CHET_DT)
724 return (EINVAL);
725 if (cp->cp_unit > (sc->sc_counts[cp->cp_type] - 1))
726 return (ENODEV);
727
728 /*
729 * Calculate the destination element.
730 */
731 dst = sc->sc_firsts[cp->cp_type] + cp->cp_unit;
732
733 /*
734 * Build the SCSI command.
735 */
736 memset(&cmd, 0, sizeof(cmd));
737 cmd.opcode = POSITION_TO_ELEMENT;
738 _lto2b(sc->sc_picker, cmd.tea);
739 _lto2b(dst, cmd.dst);
740 if (cp->cp_flags & CP_INVERT)
741 cmd.flags |= POSITION_TO_ELEMENT_INVERT;
742
743 /*
744 * Send command to changer.
745 */
746 return (scsipi_command(sc->sc_periph,
747 (struct scsipi_generic *)&cmd, sizeof(cmd), NULL, 0, CHRETRIES,
748 100000, NULL, 0));
749 }
750
751 /*
752 * Perform a READ ELEMENT STATUS on behalf of the user, and return to
753 * the user only the data the user is interested in. This returns the
754 * old data format.
755 */
756 int
757 ch_ousergetelemstatus(sc, chet, uptr)
758 struct ch_softc *sc;
759 int chet;
760 u_int8_t *uptr;
761 {
762 struct read_element_status_header *st_hdrp, st_hdr;
763 struct read_element_status_page_header *pg_hdrp;
764 struct read_element_status_descriptor *desc;
765 size_t size, desclen;
766 caddr_t data;
767 int avail, i, error = 0;
768 u_int8_t user_data;
769
770 /*
771 * If there are no elements of the requested type in the changer,
772 * the request is invalid.
773 */
774 if (sc->sc_counts[chet] == 0)
775 return (EINVAL);
776
777 /*
778 * Do the request the user wants, but only read the status header.
779 * This will tell us the amount of storage we must allocate in
780 * order to read all data.
781 */
782 error = ch_getelemstatus(sc, sc->sc_firsts[chet],
783 sc->sc_counts[chet], &st_hdr, sizeof(st_hdr),
784 XS_CTL_DATA_ONSTACK, 0);
785 if (error)
786 return (error);
787
788 size = sizeof(struct read_element_status_header) +
789 _3btol(st_hdr.nbytes);
790
791 /*
792 * We must have at least room for the status header and
793 * one page header (since we only ask for one element type
794 * at a time).
795 */
796 if (size < (sizeof(struct read_element_status_header) +
797 sizeof(struct read_element_status_page_header)))
798 return (EIO);
799
800 /*
801 * Allocate the storage and do the request again.
802 */
803 data = malloc(size, M_DEVBUF, M_WAITOK);
804 error = ch_getelemstatus(sc, sc->sc_firsts[chet],
805 sc->sc_counts[chet], data, size, 0, 0);
806 if (error)
807 goto done;
808
809 st_hdrp = (struct read_element_status_header *)data;
810 pg_hdrp = (struct read_element_status_page_header *)((u_long)st_hdrp +
811 sizeof(struct read_element_status_header));
812 desclen = _2btol(pg_hdrp->edl);
813
814 /*
815 * Fill in the user status array.
816 */
817 avail = _2btol(st_hdrp->count);
818
819 if (avail != sc->sc_counts[chet])
820 printf("%s: warning, READ ELEMENT STATUS avail != count\n",
821 sc->sc_dev.dv_xname);
822
823 desc = (struct read_element_status_descriptor *)((u_long)data +
824 sizeof(struct read_element_status_header) +
825 sizeof(struct read_element_status_page_header));
826 for (i = 0; i < avail; ++i) {
827 user_data = desc->flags1;
828 error = copyout(&user_data, &uptr[i], avail);
829 if (error)
830 break;
831 desc = (struct read_element_status_descriptor *)((u_long)desc
832 + desclen);
833 }
834
835 done:
836 if (data != NULL)
837 free(data, M_DEVBUF);
838 return (error);
839 }
840
841 /*
842 * Perform a READ ELEMENT STATUS on behalf of the user. This returns
843 * the new (more complete) data format.
844 */
845 int
846 ch_usergetelemstatus(sc, cesr)
847 struct ch_softc *sc;
848 struct changer_element_status_request *cesr;
849 {
850 struct scsipi_channel *chan = sc->sc_periph->periph_channel;
851 struct scsipi_periph *dtperiph;
852 struct read_element_status_header *st_hdrp, st_hdr;
853 struct read_element_status_page_header *pg_hdrp;
854 struct read_element_status_descriptor *desc;
855 struct changer_volume_tag *avol, *pvol;
856 size_t size, desclen, stddesclen, offset;
857 int first, avail, i, error = 0;
858 caddr_t data;
859 void *uvendptr;
860 struct changer_element_status ces;
861
862 /*
863 * Check arguments.
864 */
865 if (cesr->cesr_type > CHET_DT)
866 return (EINVAL);
867 if (sc->sc_counts[cesr->cesr_type] == 0)
868 return (ENODEV);
869 if (cesr->cesr_unit > (sc->sc_counts[cesr->cesr_type] - 1))
870 return (ENODEV);
871 if (cesr->cesr_count >
872 (sc->sc_counts[cesr->cesr_type] + cesr->cesr_unit))
873 return (EINVAL);
874
875 /*
876 * Do the request the user wants, but only read the status header.
877 * This will tell us the amount of storage we must allocate
878 * in order to read all the data.
879 */
880 error = ch_getelemstatus(sc, sc->sc_firsts[cesr->cesr_type] +
881 cesr->cesr_unit, cesr->cesr_count, &st_hdr, sizeof(st_hdr), 0,
882 cesr->cesr_flags);
883 if (error)
884 return (error);
885
886 size = sizeof(struct read_element_status_header) +
887 _3btol(st_hdr.nbytes);
888
889 /*
890 * We must have at least room for the status header and
891 * one page header (since we only ask for oen element type
892 * at a time).
893 */
894 if (size < (sizeof(struct read_element_status_header) +
895 sizeof(struct read_element_status_page_header)))
896 return (EIO);
897
898 /*
899 * Allocate the storage and do the request again.
900 */
901 data = malloc(size, M_DEVBUF, M_WAITOK);
902 error = ch_getelemstatus(sc, sc->sc_firsts[cesr->cesr_type] +
903 cesr->cesr_unit, cesr->cesr_count, data, size, 0,
904 cesr->cesr_flags);
905 if (error)
906 goto done;
907
908 st_hdrp = (struct read_element_status_header *)data;
909 pg_hdrp = (struct read_element_status_page_header *)((u_long)st_hdrp +
910 sizeof(struct read_element_status_header));
911 desclen = _2btol(pg_hdrp->edl);
912
913 /*
914 * Fill in the user status array.
915 */
916 first = _2btol(st_hdrp->fear);
917 if (first < (sc->sc_firsts[cesr->cesr_type] + cesr->cesr_unit) ||
918 first >= (sc->sc_firsts[cesr->cesr_type] + cesr->cesr_unit +
919 cesr->cesr_count)) {
920 error = EIO;
921 goto done;
922 }
923 first -= sc->sc_firsts[cesr->cesr_type] + cesr->cesr_unit;
924
925 avail = _2btol(st_hdrp->count);
926 if (avail <= 0 || avail > cesr->cesr_count) {
927 error = EIO;
928 goto done;
929 }
930
931 offset = sizeof(struct read_element_status_header) +
932 sizeof(struct read_element_status_page_header);
933
934 for (i = 0; i < cesr->cesr_count; i++) {
935 memset(&ces, 0, sizeof(ces));
936 if (i < first || i >= (first + avail)) {
937 error = copyout(&ces, &cesr->cesr_data[i],
938 sizeof(ces));
939 if (error)
940 goto done;
941 }
942
943 desc = (struct read_element_status_descriptor *)
944 (data + offset);
945 stddesclen = sizeof(struct read_element_status_descriptor);
946 offset += desclen;
947
948 ces.ces_flags = CESTATUS_STATUS_VALID;
949
950 /*
951 * The SCSI flags conveniently map directly to the
952 * chio API flags.
953 */
954 ces.ces_flags |= (desc->flags1 & 0x3f);
955
956 ces.ces_asc = desc->sense_code;
957 ces.ces_ascq = desc->sense_qual;
958
959 /*
960 * For Data Transport elemenets, get the SCSI ID and LUN,
961 * and attempt to map them to a device name if they're
962 * on the same SCSI bus.
963 */
964 if (desc->dt_scsi_flags & READ_ELEMENT_STATUS_DT_IDVALID) {
965 ces.ces_target = desc->dt_scsi_addr;
966 ces.ces_flags |= CESTATUS_TARGET_VALID;
967 }
968 if (desc->dt_scsi_flags & READ_ELEMENT_STATUS_DT_LUVALID) {
969 ces.ces_lun = desc->dt_scsi_flags &
970 READ_ELEMENT_STATUS_DT_LUNMASK;
971 ces.ces_flags |= CESTATUS_LUN_VALID;
972 }
973 if (desc->dt_scsi_flags & READ_ELEMENT_STATUS_DT_NOTBUS)
974 ces.ces_flags |= CESTATUS_NOTBUS;
975 else if ((ces.ces_flags &
976 (CESTATUS_TARGET_VALID|CESTATUS_LUN_VALID)) ==
977 (CESTATUS_TARGET_VALID|CESTATUS_LUN_VALID)) {
978 if (ces.ces_target < chan->chan_ntargets &&
979 ces.ces_lun < chan->chan_nluns &&
980 (dtperiph = scsipi_lookup_periph(chan,
981 ces.ces_target, ces.ces_lun)) != NULL &&
982 dtperiph->periph_dev != NULL) {
983 strlcpy(ces.ces_xname,
984 dtperiph->periph_dev->dv_xname,
985 sizeof(ces.ces_xname));
986 ces.ces_flags |= CESTATUS_XNAME_VALID;
987 }
988 }
989
990 if (desc->flags2 & READ_ELEMENT_STATUS_INVERT)
991 ces.ces_flags |= CESTATUS_INVERTED;
992
993 if (desc->flags2 & READ_ELEMENT_STATUS_SVALID) {
994 if (ch_map_element(sc, _2btol(desc->ssea),
995 &ces.ces_from_type, &ces.ces_from_unit))
996 ces.ces_flags |= CESTATUS_FROM_VALID;
997 }
998
999 /*
1000 * Extract volume tag information.
1001 */
1002 switch (pg_hdrp->flags &
1003 (READ_ELEMENT_STATUS_PVOLTAG|READ_ELEMENT_STATUS_AVOLTAG)) {
1004 case (READ_ELEMENT_STATUS_PVOLTAG|READ_ELEMENT_STATUS_AVOLTAG):
1005 pvol = (struct changer_volume_tag *)(desc + 1);
1006 avol = pvol + 1;
1007 break;
1008
1009 case READ_ELEMENT_STATUS_PVOLTAG:
1010 pvol = (struct changer_volume_tag *)(desc + 1);
1011 avol = NULL;
1012 break;
1013
1014 case READ_ELEMENT_STATUS_AVOLTAG:
1015 pvol = NULL;
1016 avol = (struct changer_volume_tag *)(desc + 1);
1017 break;
1018
1019 default:
1020 avol = pvol = NULL;
1021 break;
1022 }
1023
1024 if (pvol != NULL) {
1025 ch_voltag_convert_in(pvol, &ces.ces_pvoltag);
1026 ces.ces_flags |= CESTATUS_PVOL_VALID;
1027 stddesclen += sizeof(struct changer_volume_tag);
1028 }
1029 if (avol != NULL) {
1030 ch_voltag_convert_in(avol, &ces.ces_avoltag);
1031 ces.ces_flags |= CESTATUS_AVOL_VALID;
1032 stddesclen += sizeof(struct changer_volume_tag);
1033 }
1034
1035 /*
1036 * Compute vendor-specific length. Note the 4 reserved
1037 * bytes between the volume tags and the vendor-specific
1038 * data. Copy it out of the user wants it.
1039 */
1040 stddesclen += 4;
1041 if (desclen > stddesclen)
1042 ces.ces_vendor_len = desclen - stddesclen;
1043
1044 if (ces.ces_vendor_len != 0 && cesr->cesr_vendor_data != NULL) {
1045 error = copyin(&cesr->cesr_vendor_data[i], &uvendptr,
1046 sizeof(uvendptr));
1047 if (error)
1048 goto done;
1049 error = copyout((void *)((u_long)desc + stddesclen),
1050 uvendptr, ces.ces_vendor_len);
1051 if (error)
1052 goto done;
1053 }
1054
1055 /*
1056 * Now copy out the status descriptor we've constructed.
1057 */
1058 error = copyout(&ces, &cesr->cesr_data[i], sizeof(ces));
1059 if (error)
1060 goto done;
1061 }
1062
1063 done:
1064 if (data != NULL)
1065 free(data, M_DEVBUF);
1066 return (error);
1067 }
1068
1069 int
1070 ch_getelemstatus(sc, first, count, data, datalen, scsiflags, flags)
1071 struct ch_softc *sc;
1072 int first, count;
1073 void *data;
1074 size_t datalen;
1075 int scsiflags;
1076 int flags;
1077 {
1078 struct scsi_read_element_status cmd;
1079
1080 /*
1081 * Build SCSI command.
1082 */
1083 memset(&cmd, 0, sizeof(cmd));
1084 cmd.opcode = READ_ELEMENT_STATUS;
1085 cmd.byte2 = ELEMENT_TYPE_ALL;
1086 if (flags & CESR_VOLTAGS)
1087 cmd.byte2 |= READ_ELEMENT_STATUS_VOLTAG;
1088 _lto2b(first, cmd.sea);
1089 _lto2b(count, cmd.count);
1090 _lto3b(datalen, cmd.len);
1091
1092 /*
1093 * Send command to changer.
1094 */
1095 return (scsipi_command(sc->sc_periph,
1096 (struct scsipi_generic *)&cmd, sizeof(cmd),
1097 (u_char *)data, datalen, CHRETRIES, 100000, NULL,
1098 scsiflags | XS_CTL_DATA_IN));
1099 }
1100
1101 int
1102 ch_setvoltag(sc, csvr)
1103 struct ch_softc *sc;
1104 struct changer_set_voltag_request *csvr;
1105 {
1106 struct scsi_send_volume_tag cmd;
1107 struct changer_volume_tag voltag;
1108 void *data = NULL;
1109 size_t datalen = 0;
1110 int error;
1111 u_int16_t dst;
1112
1113 /*
1114 * Check arguments.
1115 */
1116 if (csvr->csvr_type > CHET_DT)
1117 return (EINVAL);
1118 if (csvr->csvr_unit > (sc->sc_counts[csvr->csvr_type] - 1))
1119 return (ENODEV);
1120
1121 dst = sc->sc_firsts[csvr->csvr_type] + csvr->csvr_unit;
1122
1123 /*
1124 * Build the SCSI command.
1125 */
1126 memset(&cmd, 0, sizeof(cmd));
1127 cmd.opcode = SEND_VOLUME_TAG;
1128 _lto2b(dst, cmd.eaddr);
1129
1130 #define ALTERNATE (csvr->csvr_flags & CSVR_ALTERNATE)
1131
1132 switch (csvr->csvr_flags & CSVR_MODE_MASK) {
1133 case CSVR_MODE_SET:
1134 cmd.sac = ALTERNATE ? SAC_ASSERT_ALT : SAC_ASSERT_PRIMARY;
1135 break;
1136
1137 case CSVR_MODE_REPLACE:
1138 cmd.sac = ALTERNATE ? SAC_REPLACE_ALT : SAC_REPLACE_PRIMARY;
1139 break;
1140
1141 case CSVR_MODE_CLEAR:
1142 cmd.sac = ALTERNATE ? SAC_UNDEFINED_ALT : SAC_UNDEFINED_PRIMARY;
1143 break;
1144
1145 default:
1146 return (EINVAL);
1147 }
1148
1149 #undef ALTERNATE
1150
1151 if (cmd.sac < SAC_UNDEFINED_PRIMARY) {
1152 error = ch_voltag_convert_out(&csvr->csvr_voltag, &voltag);
1153 if (error)
1154 return (error);
1155 data = &voltag;
1156 datalen = sizeof(voltag);
1157 _lto2b(datalen, cmd.length);
1158 }
1159
1160 /*
1161 * Send command to changer.
1162 */
1163 return (scsipi_command(sc->sc_periph,
1164 (struct scsipi_generic *)&cmd, sizeof(cmd),
1165 (u_char *)data, datalen, CHRETRIES, 100000, NULL,
1166 datalen ? XS_CTL_DATA_OUT | XS_CTL_DATA_ONSTACK : 0));
1167 }
1168
1169 int
1170 ch_ielem(sc)
1171 struct ch_softc *sc;
1172 {
1173 int tmo;
1174 struct scsi_initialize_element_status cmd;
1175
1176 /*
1177 * Build SCSI command.
1178 */
1179 memset(&cmd, 0, sizeof(cmd));
1180 cmd.opcode = INITIALIZE_ELEMENT_STATUS;
1181
1182 /*
1183 * Send command to changer.
1184 *
1185 * The problem is, how long to allow for the command?
1186 * It can take a *really* long time, and also depends
1187 * on unknowable factors such as whether there are
1188 * *almost* readable labels on tapes that a barcode
1189 * reader is trying to decipher.
1190 *
1191 * I'm going to make this long enough to allow 5 minutes
1192 * per element plus an initial 10 minute wait.
1193 */
1194 tmo = sc->sc_counts[CHET_MT] +
1195 sc->sc_counts[CHET_ST] +
1196 sc->sc_counts[CHET_IE] +
1197 sc->sc_counts[CHET_DT];
1198 tmo *= 5 * 60 * 1000;
1199 tmo += (10 * 60 * 1000);
1200
1201 return (scsipi_command(sc->sc_periph,
1202 (struct scsipi_generic *)&cmd, sizeof(cmd),
1203 NULL, 0, CHRETRIES, tmo, NULL, XS_CTL_IGNORE_ILLEGAL_REQUEST));
1204 }
1205
1206 /*
1207 * Ask the device about itself and fill in the parameters in our
1208 * softc.
1209 */
1210 int
1211 ch_get_params(sc, scsiflags)
1212 struct ch_softc *sc;
1213 int scsiflags;
1214 {
1215 struct scsi_mode_sense_data {
1216 struct scsipi_mode_header header;
1217 union {
1218 struct page_element_address_assignment ea;
1219 struct page_transport_geometry_parameters tg;
1220 struct page_device_capabilities cap;
1221 } pages;
1222 } sense_data;
1223 int error, from;
1224 u_int8_t *moves, *exchanges;
1225
1226 /*
1227 * Grab info from the element address assignment page.
1228 */
1229 memset(&sense_data, 0, sizeof(sense_data));
1230 error = scsipi_mode_sense(sc->sc_periph, SMS_DBD, 0x1d,
1231 &sense_data.header, sizeof(sense_data),
1232 scsiflags | XS_CTL_DATA_ONSTACK, CHRETRIES, 6000);
1233 if (error) {
1234 printf("%s: could not sense element address page\n",
1235 sc->sc_dev.dv_xname);
1236 return (error);
1237 }
1238
1239 sc->sc_firsts[CHET_MT] = _2btol(sense_data.pages.ea.mtea);
1240 sc->sc_counts[CHET_MT] = _2btol(sense_data.pages.ea.nmte);
1241 sc->sc_firsts[CHET_ST] = _2btol(sense_data.pages.ea.fsea);
1242 sc->sc_counts[CHET_ST] = _2btol(sense_data.pages.ea.nse);
1243 sc->sc_firsts[CHET_IE] = _2btol(sense_data.pages.ea.fieea);
1244 sc->sc_counts[CHET_IE] = _2btol(sense_data.pages.ea.niee);
1245 sc->sc_firsts[CHET_DT] = _2btol(sense_data.pages.ea.fdtea);
1246 sc->sc_counts[CHET_DT] = _2btol(sense_data.pages.ea.ndte);
1247
1248 /* XXX ask for transport geometry page XXX */
1249
1250 /*
1251 * Grab info from the capabilities page.
1252 */
1253 memset(&sense_data, 0, sizeof(sense_data));
1254 /*
1255 * XXX: Note: not all changers can deal with disabled block descriptors
1256 */
1257 error = scsipi_mode_sense(sc->sc_periph, SMS_DBD, 0x1f,
1258 &sense_data.header, sizeof(sense_data),
1259 scsiflags | XS_CTL_DATA_ONSTACK, CHRETRIES, 6000);
1260 if (error) {
1261 printf("%s: could not sense capabilities page\n",
1262 sc->sc_dev.dv_xname);
1263 return (error);
1264 }
1265
1266 memset(sc->sc_movemask, 0, sizeof(sc->sc_movemask));
1267 memset(sc->sc_exchangemask, 0, sizeof(sc->sc_exchangemask));
1268 moves = &sense_data.pages.cap.move_from_mt;
1269 exchanges = &sense_data.pages.cap.exchange_with_mt;
1270 for (from = CHET_MT; from <= CHET_DT; ++from) {
1271 sc->sc_movemask[from] = moves[from];
1272 sc->sc_exchangemask[from] = exchanges[from];
1273 }
1274
1275 #ifdef CH_AUTOMATIC_IELEM_POLICY
1276 /*
1277 * If we need to do an Init-Element-Status,
1278 * do that now that we know what's in the changer.
1279 */
1280 if ((scsiflags & XS_CTL_IGNORE_MEDIA_CHANGE) == 0) {
1281 if ((sc->sc_periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
1282 error = ch_ielem(sc);
1283 if (error == 0)
1284 sc->sc_periph->periph_flags |= PERIPH_MEDIA_LOADED;
1285 else
1286 sc->sc_periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
1287 }
1288 #endif
1289 return (error);
1290 }
1291
1292 void
1293 ch_get_quirks(sc, inqbuf)
1294 struct ch_softc *sc;
1295 struct scsipi_inquiry_pattern *inqbuf;
1296 {
1297 struct chquirk *match;
1298 int priority;
1299
1300 sc->sc_settledelay = 0;
1301
1302 match = (struct chquirk *)scsipi_inqmatch(inqbuf,
1303 (caddr_t)chquirks,
1304 sizeof(chquirks) / sizeof(chquirks[0]),
1305 sizeof(chquirks[0]), &priority);
1306 if (priority != 0)
1307 sc->sc_settledelay = match->cq_settledelay;
1308 }
1309
1310 int
1311 ch_map_element(sc, elem, typep, unitp)
1312 struct ch_softc *sc;
1313 u_int16_t elem;
1314 int *typep, *unitp;
1315 {
1316 int chet;
1317
1318 for (chet = CHET_MT; chet <= CHET_DT; chet++) {
1319 if (elem >= sc->sc_firsts[chet] &&
1320 elem < (sc->sc_firsts[chet] + sc->sc_counts[chet])) {
1321 *typep = chet;
1322 *unitp = elem - sc->sc_firsts[chet];
1323 return (1);
1324 }
1325 }
1326 return (0);
1327 }
1328
1329 void
1330 ch_voltag_convert_in(sv, cv)
1331 const struct changer_volume_tag *sv;
1332 struct changer_voltag *cv;
1333 {
1334 int i;
1335
1336 memset(cv, 0, sizeof(struct changer_voltag));
1337
1338 /*
1339 * Copy the volume tag string from the SCSI representation.
1340 * Per the SCSI-2 spec, we stop at the first blank character.
1341 */
1342 for (i = 0; i < sizeof(sv->volid); i++) {
1343 if (sv->volid[i] == ' ')
1344 break;
1345 cv->cv_tag[i] = sv->volid[i];
1346 }
1347 cv->cv_tag[i] = '\0';
1348
1349 cv->cv_serial = _2btol(sv->volseq);
1350 }
1351
1352 int
1353 ch_voltag_convert_out(cv, sv)
1354 const struct changer_voltag *cv;
1355 struct changer_volume_tag *sv;
1356 {
1357 int i;
1358
1359 memset(sv, ' ', sizeof(struct changer_volume_tag));
1360
1361 for (i = 0; i < sizeof(sv->volid); i++) {
1362 if (cv->cv_tag[i] == '\0')
1363 break;
1364 /*
1365 * Limit the character set to what is suggested in
1366 * the SCSI-2 spec.
1367 */
1368 if ((cv->cv_tag[i] < '0' || cv->cv_tag[i] > '9') &&
1369 (cv->cv_tag[i] < 'A' || cv->cv_tag[i] > 'Z') &&
1370 (cv->cv_tag[i] != '_'))
1371 return (EINVAL);
1372 sv->volid[i] = cv->cv_tag[i];
1373 }
1374
1375 _lto2b(cv->cv_serial, sv->volseq);
1376
1377 return (0);
1378 }
1379