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