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