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