ch.c revision 1.47.2.1 1 /* $NetBSD: ch.c,v 1.47.2.1 2001/09/07 04:45: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 <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(devvp->v_rdev);
258 if ((unit >= ch_cd.cd_ndevs) ||
259 ((sc = ch_cd.cd_devs[unit]) == NULL))
260 return (ENXIO);
261
262 devvp->v_devcookie = 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 = devvp->v_devcookie;
311 struct scsipi_periph *periph = sc->sc_periph;
312 struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
313
314 scsipi_wait_drain(periph);
315
316 scsipi_adapter_delref(adapt);
317
318 sc->sc_events = 0;
319
320 periph->periph_flags &= ~PERIPH_OPEN;
321 return (0);
322 }
323
324 int
325 chread(devvp, uio, flags)
326 struct vnode *devvp;
327 struct uio *uio;
328 int flags;
329 {
330 struct ch_softc *sc = devvp->v_devcookie;
331 int error;
332
333 if (uio->uio_resid != CHANGER_EVENT_SIZE)
334 return (EINVAL);
335
336 /*
337 * Read never blocks; if there are no events pending, we just
338 * return an all-clear bitmask.
339 */
340 error = uiomove(&sc->sc_events, CHANGER_EVENT_SIZE, uio);
341 if (error == 0)
342 sc->sc_events = 0;
343 return (error);
344 }
345
346 int
347 chioctl(devvp, cmd, data, flags, p)
348 struct vnode *devvp;
349 u_long cmd;
350 caddr_t data;
351 int flags;
352 struct proc *p;
353 {
354 struct ch_softc *sc = devvp->v_devcookie;
355 int error = 0;
356
357 /*
358 * If this command can change the device's state, we must
359 * have the device open for writing.
360 */
361 switch (cmd) {
362 case CHIOGPICKER:
363 case CHIOGPARAMS:
364 case OCHIOGSTATUS:
365 break;
366
367 default:
368 if ((flags & FWRITE) == 0)
369 return (EBADF);
370 }
371
372 switch (cmd) {
373 case CHIOMOVE:
374 error = ch_move(sc, (struct changer_move_request *)data);
375 break;
376
377 case CHIOEXCHANGE:
378 error = ch_exchange(sc,
379 (struct changer_exchange_request *)data);
380 break;
381
382 case CHIOPOSITION:
383 error = ch_position(sc,
384 (struct changer_position_request *)data);
385 break;
386
387 case CHIOGPICKER:
388 *(int *)data = sc->sc_picker - sc->sc_firsts[CHET_MT];
389 break;
390
391 case CHIOSPICKER:
392 {
393 int new_picker = *(int *)data;
394
395 if (new_picker > (sc->sc_counts[CHET_MT] - 1))
396 return (EINVAL);
397 sc->sc_picker = sc->sc_firsts[CHET_MT] + new_picker;
398 break;
399 }
400
401 case CHIOGPARAMS:
402 {
403 struct changer_params *cp = (struct changer_params *)data;
404
405 cp->cp_curpicker = sc->sc_picker - sc->sc_firsts[CHET_MT];
406 cp->cp_npickers = sc->sc_counts[CHET_MT];
407 cp->cp_nslots = sc->sc_counts[CHET_ST];
408 cp->cp_nportals = sc->sc_counts[CHET_IE];
409 cp->cp_ndrives = sc->sc_counts[CHET_DT];
410 break;
411 }
412
413 case CHIOIELEM:
414 error = ch_ielem(sc);
415 if (error == 0) {
416 sc->sc_periph->periph_flags |= PERIPH_MEDIA_LOADED;
417 }
418 break;
419
420 case OCHIOGSTATUS:
421 {
422 struct ochanger_element_status_request *cesr =
423 (struct ochanger_element_status_request *)data;
424
425 error = ch_ousergetelemstatus(sc, cesr->cesr_type,
426 cesr->cesr_data);
427 break;
428 }
429
430 case CHIOGSTATUS:
431 error = ch_usergetelemstatus(sc,
432 (struct changer_element_status_request *)data);
433 break;
434
435 case CHIOSVOLTAG:
436 error = ch_setvoltag(sc,
437 (struct changer_set_voltag_request *)data);
438 break;
439
440 /* Implement prevent/allow? */
441
442 default:
443 error = scsipi_do_ioctl(sc->sc_periph, devvp, cmd, data,
444 flags, p);
445 break;
446 }
447
448 return (error);
449 }
450
451 int
452 chpoll(devvp, events, p)
453 struct vnode *devvp;
454 int events;
455 struct proc *p;
456 {
457 struct ch_softc *sc = devvp->v_devcookie;
458 int revents;
459
460 revents = events & (POLLOUT | POLLWRNORM);
461
462 if ((events & (POLLIN | POLLRDNORM)) == 0)
463 return (revents);
464
465 if (sc->sc_events == 0)
466 revents |= events & (POLLIN | POLLRDNORM);
467 else
468 selrecord(p, &sc->sc_selq);
469
470 return (revents);
471 }
472
473 int
474 ch_interpret_sense(xs)
475 struct scsipi_xfer *xs;
476 {
477 struct scsipi_periph *periph = xs->xs_periph;
478 struct scsipi_sense_data *sense = &xs->sense.scsi_sense;
479 struct ch_softc *sc = (void *)periph->periph_dev;
480 u_int16_t asc_ascq;
481
482 /*
483 * If the periph is already recovering, just do the
484 * normal error recovering.
485 */
486 if (periph->periph_flags & PERIPH_RECOVERING)
487 return (EJUSTRETURN);
488
489 /*
490 * If it isn't an extended or extended/deferred error, let
491 * the generic code handle it.
492 */
493 if ((sense->error_code & SSD_ERRCODE) != 0x70 &&
494 (sense->error_code & SSD_ERRCODE) != 0x71)
495 return (EJUSTRETURN);
496
497 /*
498 * We're only interested in condtions that
499 * indicate potential inventory violation.
500 *
501 * We use ASC/ASCQ codes for this.
502 */
503
504 asc_ascq = (((u_int16_t) sense->add_sense_code) << 8) |
505 sense->add_sense_code_qual;
506
507 switch (asc_ascq) {
508 case 0x2800:
509 /* "Not Ready To Ready Transition (Medium May Have Changed)" */
510 case 0x2900:
511 /* "Power On, Reset, or Bus Device Reset Occurred" */
512 sc->sc_periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
513 /*
514 * Enqueue an Element-Status-Changed event, and
515 * wake up any processes waiting for them.
516 */
517 /*
518 * Enqueue an Element-Status-Changed event, and wake up
519 * any processes waiting for them.
520 */
521 if ((xs->xs_control & XS_CTL_IGNORE_MEDIA_CHANGE) == 0)
522 ch_event(sc, CHEV_ELEMENT_STATUS_CHANGED);
523 break;
524 default:
525 break;
526 }
527
528 return (EJUSTRETURN);
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 memset(&cmd, 0, 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 memset(&cmd, 0, 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 memset(&cmd, 0, 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),
718 XS_CTL_DATA_ONSTACK, 0);
719 if (error)
720 return (error);
721
722 size = sizeof(struct read_element_status_header) +
723 _3btol(st_hdr.nbytes);
724
725 /*
726 * We must have at least room for the status header and
727 * one page header (since we only ask for one element type
728 * at a time).
729 */
730 if (size < (sizeof(struct read_element_status_header) +
731 sizeof(struct read_element_status_page_header)))
732 return (EIO);
733
734 /*
735 * Allocate the storage and do the request again.
736 */
737 data = malloc(size, M_DEVBUF, M_WAITOK);
738 error = ch_getelemstatus(sc, sc->sc_firsts[chet],
739 sc->sc_counts[chet], data, size, 0, 0);
740 if (error)
741 goto done;
742
743 st_hdrp = (struct read_element_status_header *)data;
744 pg_hdrp = (struct read_element_status_page_header *)((u_long)st_hdrp +
745 sizeof(struct read_element_status_header));
746 desclen = _2btol(pg_hdrp->edl);
747
748 /*
749 * Fill in the user status array.
750 */
751 avail = _2btol(st_hdrp->count);
752
753 if (avail != sc->sc_counts[chet])
754 printf("%s: warning, READ ELEMENT STATUS avail != count\n",
755 sc->sc_dev.dv_xname);
756
757 desc = (struct read_element_status_descriptor *)((u_long)data +
758 sizeof(struct read_element_status_header) +
759 sizeof(struct read_element_status_page_header));
760 for (i = 0; i < avail; ++i) {
761 user_data = desc->flags1;
762 error = copyout(&user_data, &uptr[i], avail);
763 if (error)
764 break;
765 desc = (struct read_element_status_descriptor *)((u_long)desc
766 + desclen);
767 }
768
769 done:
770 if (data != NULL)
771 free(data, M_DEVBUF);
772 return (error);
773 }
774
775 /*
776 * Perform a READ ELEMENT STATUS on behalf of the user. This returns
777 * the new (more complete) data format.
778 */
779 int
780 ch_usergetelemstatus(sc, cesr)
781 struct ch_softc *sc;
782 struct changer_element_status_request *cesr;
783 {
784 struct scsipi_channel *chan = sc->sc_periph->periph_channel;
785 struct scsipi_periph *dtperiph;
786 struct read_element_status_header *st_hdrp, st_hdr;
787 struct read_element_status_page_header *pg_hdrp;
788 struct read_element_status_descriptor *desc;
789 struct changer_volume_tag *avol, *pvol;
790 size_t size, desclen, stddesclen, offset;
791 int first, avail, i, error = 0;
792 caddr_t data;
793 void *uvendptr;
794 struct changer_element_status ces;
795
796 /*
797 * Check arguments.
798 */
799 if (cesr->cesr_type > CHET_DT)
800 return (EINVAL);
801 if (sc->sc_counts[cesr->cesr_type] == 0)
802 return (ENODEV);
803 if (cesr->cesr_unit > (sc->sc_counts[cesr->cesr_type] - 1))
804 return (ENODEV);
805 if (cesr->cesr_count >
806 (sc->sc_counts[cesr->cesr_type] + cesr->cesr_unit))
807 return (EINVAL);
808
809 /*
810 * Do the request the user wants, but only read the status header.
811 * This will tell us the amount of storage we must allocate
812 * in order to read all the data.
813 */
814 error = ch_getelemstatus(sc, sc->sc_firsts[cesr->cesr_type] +
815 cesr->cesr_unit, cesr->cesr_count, &st_hdr, sizeof(st_hdr), 0,
816 cesr->cesr_flags);
817 if (error)
818 return (error);
819
820 size = sizeof(struct read_element_status_header) +
821 _3btol(st_hdr.nbytes);
822
823 /*
824 * We must have at least room for the status header and
825 * one page header (since we only ask for oen element type
826 * at a time).
827 */
828 if (size < (sizeof(struct read_element_status_header) +
829 sizeof(struct read_element_status_page_header)))
830 return (EIO);
831
832 /*
833 * Allocate the storage and do the request again.
834 */
835 data = malloc(size, M_DEVBUF, M_WAITOK);
836 error = ch_getelemstatus(sc, sc->sc_firsts[cesr->cesr_type] +
837 cesr->cesr_unit, cesr->cesr_count, data, size, 0,
838 cesr->cesr_flags);
839 if (error)
840 goto done;
841
842 st_hdrp = (struct read_element_status_header *)data;
843 pg_hdrp = (struct read_element_status_page_header *)((u_long)st_hdrp +
844 sizeof(struct read_element_status_header));
845 desclen = _2btol(pg_hdrp->edl);
846
847 /*
848 * Fill in the user status array.
849 */
850 first = _2btol(st_hdrp->fear);
851 if (first < (sc->sc_firsts[cesr->cesr_type] + cesr->cesr_unit) ||
852 first >= (sc->sc_firsts[cesr->cesr_type] + cesr->cesr_unit +
853 cesr->cesr_count)) {
854 error = EIO;
855 goto done;
856 }
857 first -= sc->sc_firsts[cesr->cesr_type] + cesr->cesr_unit;
858
859 avail = _2btol(st_hdrp->count);
860 if (avail <= 0 || avail > cesr->cesr_count) {
861 error = EIO;
862 goto done;
863 }
864
865 offset = sizeof(struct read_element_status_header) +
866 sizeof(struct read_element_status_page_header);
867
868 for (i = 0; i < cesr->cesr_count; i++) {
869 memset(&ces, 0, sizeof(ces));
870 if (i < first || i >= (first + avail)) {
871 error = copyout(&ces, &cesr->cesr_data[i],
872 sizeof(ces));
873 if (error)
874 goto done;
875 }
876
877 desc = (struct read_element_status_descriptor *)
878 (data + offset);
879 stddesclen = sizeof(struct read_element_status_descriptor);
880 offset += desclen;
881
882 ces.ces_flags = CESTATUS_STATUS_VALID;
883
884 /*
885 * The SCSI flags conveniently map directly to the
886 * chio API flags.
887 */
888 ces.ces_flags |= (desc->flags1 & 0x3f);
889
890 ces.ces_asc = desc->sense_code;
891 ces.ces_ascq = desc->sense_qual;
892
893 /*
894 * For Data Transport elemenets, get the SCSI ID and LUN,
895 * and attempt to map them to a device name if they're
896 * on the same SCSI bus.
897 */
898 if (desc->dt_scsi_flags & READ_ELEMENT_STATUS_DT_IDVALID) {
899 ces.ces_target = desc->dt_scsi_addr;
900 ces.ces_flags |= CESTATUS_TARGET_VALID;
901 }
902 if (desc->dt_scsi_flags & READ_ELEMENT_STATUS_DT_LUVALID) {
903 ces.ces_lun = desc->dt_scsi_flags &
904 READ_ELEMENT_STATUS_DT_LUNMASK;
905 ces.ces_flags |= CESTATUS_LUN_VALID;
906 }
907 if (desc->dt_scsi_flags & READ_ELEMENT_STATUS_DT_NOTBUS)
908 ces.ces_flags |= CESTATUS_NOTBUS;
909 else if ((ces.ces_flags &
910 (CESTATUS_TARGET_VALID|CESTATUS_LUN_VALID)) ==
911 (CESTATUS_TARGET_VALID|CESTATUS_LUN_VALID)) {
912 if (ces.ces_target < chan->chan_ntargets &&
913 ces.ces_lun < chan->chan_nluns &&
914 (dtperiph = scsipi_lookup_periph(chan,
915 ces.ces_target, ces.ces_lun)) != NULL &&
916 dtperiph->periph_dev != NULL) {
917 strcpy(ces.ces_xname,
918 dtperiph->periph_dev->dv_xname);
919 ces.ces_flags |= CESTATUS_XNAME_VALID;
920 }
921 }
922
923 if (desc->flags2 & READ_ELEMENT_STATUS_INVERT)
924 ces.ces_flags |= CESTATUS_INVERTED;
925
926 if (desc->flags2 & READ_ELEMENT_STATUS_SVALID) {
927 if (ch_map_element(sc, _2btol(desc->ssea),
928 &ces.ces_from_type, &ces.ces_from_unit))
929 ces.ces_flags |= CESTATUS_FROM_VALID;
930 }
931
932 /*
933 * Extract volume tag information.
934 */
935 switch (pg_hdrp->flags &
936 (READ_ELEMENT_STATUS_PVOLTAG|READ_ELEMENT_STATUS_AVOLTAG)) {
937 case (READ_ELEMENT_STATUS_PVOLTAG|READ_ELEMENT_STATUS_AVOLTAG):
938 pvol = (struct changer_volume_tag *)(desc + 1);
939 avol = pvol + 1;
940 break;
941
942 case READ_ELEMENT_STATUS_PVOLTAG:
943 pvol = (struct changer_volume_tag *)(desc + 1);
944 avol = NULL;
945 break;
946
947 case READ_ELEMENT_STATUS_AVOLTAG:
948 pvol = NULL;
949 avol = (struct changer_volume_tag *)(desc + 1);
950 break;
951
952 default:
953 avol = pvol = NULL;
954 break;
955 }
956
957 if (pvol != NULL) {
958 ch_voltag_convert_in(pvol, &ces.ces_pvoltag);
959 ces.ces_flags |= CESTATUS_PVOL_VALID;
960 stddesclen += sizeof(struct changer_volume_tag);
961 }
962 if (avol != NULL) {
963 ch_voltag_convert_in(avol, &ces.ces_avoltag);
964 ces.ces_flags |= CESTATUS_AVOL_VALID;
965 stddesclen += sizeof(struct changer_volume_tag);
966 }
967
968 /*
969 * Compute vendor-specific length. Note the 4 reserved
970 * bytes between the volume tags and the vendor-specific
971 * data. Copy it out of the user wants it.
972 */
973 stddesclen += 4;
974 if (desclen > stddesclen)
975 ces.ces_vendor_len = desclen - stddesclen;
976
977 if (ces.ces_vendor_len != 0 && cesr->cesr_vendor_data != NULL) {
978 error = copyin(&cesr->cesr_vendor_data[i], &uvendptr,
979 sizeof(uvendptr));
980 if (error)
981 goto done;
982 error = copyout((void *)((u_long)desc + stddesclen),
983 uvendptr, ces.ces_vendor_len);
984 if (error)
985 goto done;
986 }
987
988 /*
989 * Now copy out the status descriptor we've constructed.
990 */
991 error = copyout(&ces, &cesr->cesr_data[i], sizeof(ces));
992 if (error)
993 goto done;
994 }
995
996 done:
997 if (data != NULL)
998 free(data, M_DEVBUF);
999 return (error);
1000 }
1001
1002 int
1003 ch_getelemstatus(sc, first, count, data, datalen, scsiflags, flags)
1004 struct ch_softc *sc;
1005 int first, count;
1006 void *data;
1007 size_t datalen;
1008 int scsiflags;
1009 int flags;
1010 {
1011 struct scsi_read_element_status cmd;
1012
1013 /*
1014 * Build SCSI command.
1015 */
1016 memset(&cmd, 0, sizeof(cmd));
1017 cmd.opcode = READ_ELEMENT_STATUS;
1018 cmd.byte2 = ELEMENT_TYPE_ALL;
1019 if (flags & CESR_VOLTAGS)
1020 cmd.byte2 |= READ_ELEMENT_STATUS_VOLTAG;
1021 _lto2b(first, cmd.sea);
1022 _lto2b(count, cmd.count);
1023 _lto3b(datalen, cmd.len);
1024
1025 /*
1026 * Send command to changer.
1027 */
1028 return (scsipi_command(sc->sc_periph,
1029 (struct scsipi_generic *)&cmd, sizeof(cmd),
1030 (u_char *)data, datalen, CHRETRIES, 100000, NULL,
1031 scsiflags | XS_CTL_DATA_IN));
1032 }
1033
1034 int
1035 ch_setvoltag(sc, csvr)
1036 struct ch_softc *sc;
1037 struct changer_set_voltag_request *csvr;
1038 {
1039 struct scsi_send_volume_tag cmd;
1040 struct changer_volume_tag voltag;
1041 void *data = NULL;
1042 size_t datalen = 0;
1043 int error;
1044 u_int16_t dst;
1045
1046 /*
1047 * Check arguments.
1048 */
1049 if (csvr->csvr_type > CHET_DT)
1050 return (EINVAL);
1051 if (csvr->csvr_unit > (sc->sc_counts[csvr->csvr_type] - 1))
1052 return (ENODEV);
1053
1054 dst = sc->sc_firsts[csvr->csvr_type] + csvr->csvr_unit;
1055
1056 /*
1057 * Build the SCSI command.
1058 */
1059 memset(&cmd, 0, sizeof(cmd));
1060 cmd.opcode = SEND_VOLUME_TAG;
1061 _lto2b(dst, cmd.eaddr);
1062
1063 #define ALTERNATE (csvr->csvr_flags & CSVR_ALTERNATE)
1064
1065 switch (csvr->csvr_flags & CSVR_MODE_MASK) {
1066 case CSVR_MODE_SET:
1067 cmd.sac = ALTERNATE ? SAC_ASSERT_ALT : SAC_ASSERT_PRIMARY;
1068 break;
1069
1070 case CSVR_MODE_REPLACE:
1071 cmd.sac = ALTERNATE ? SAC_REPLACE_ALT : SAC_REPLACE_PRIMARY;
1072 break;
1073
1074 case CSVR_MODE_CLEAR:
1075 cmd.sac = ALTERNATE ? SAC_UNDEFINED_ALT : SAC_UNDEFINED_PRIMARY;
1076 break;
1077
1078 default:
1079 return (EINVAL);
1080 }
1081
1082 #undef ALTERNATE
1083
1084 if (cmd.sac < SAC_UNDEFINED_PRIMARY) {
1085 error = ch_voltag_convert_out(&csvr->csvr_voltag, &voltag);
1086 if (error)
1087 return (error);
1088 data = &voltag;
1089 datalen = sizeof(voltag);
1090 _lto2b(datalen, cmd.length);
1091 }
1092
1093 /*
1094 * Send command to changer.
1095 */
1096 return (scsipi_command(sc->sc_periph,
1097 (struct scsipi_generic *)&cmd, sizeof(cmd),
1098 (u_char *)data, datalen, CHRETRIES, 100000, NULL,
1099 datalen ? XS_CTL_DATA_OUT | XS_CTL_DATA_ONSTACK : 0));
1100 }
1101
1102 int
1103 ch_ielem(sc)
1104 struct ch_softc *sc;
1105 {
1106 int tmo;
1107 struct scsi_initialize_element_status cmd;
1108
1109 /*
1110 * Build SCSI command.
1111 */
1112 memset(&cmd, 0, sizeof(cmd));
1113 cmd.opcode = INITIALIZE_ELEMENT_STATUS;
1114
1115 /*
1116 * Send command to changer.
1117 *
1118 * The problem is, how long to allow for the command?
1119 * It can take a *really* long time, and also depends
1120 * on unknowable factors such as whether there are
1121 * *almost* readable labels on tapes that a barcode
1122 * reader is trying to decipher.
1123 *
1124 * I'm going to make this long enough to allow 5 minutes
1125 * per element plus an initial 10 minute wait.
1126 */
1127 tmo = sc->sc_counts[CHET_MT] +
1128 sc->sc_counts[CHET_ST] +
1129 sc->sc_counts[CHET_IE] +
1130 sc->sc_counts[CHET_DT];
1131 tmo *= 5 * 60 * 1000;
1132 tmo += (10 * 60 * 1000);
1133
1134 return (scsipi_command(sc->sc_periph,
1135 (struct scsipi_generic *)&cmd, sizeof(cmd),
1136 NULL, 0, CHRETRIES, tmo, NULL, XS_CTL_IGNORE_ILLEGAL_REQUEST));
1137 }
1138
1139 /*
1140 * Ask the device about itself and fill in the parameters in our
1141 * softc.
1142 */
1143 int
1144 ch_get_params(sc, scsiflags)
1145 struct ch_softc *sc;
1146 int scsiflags;
1147 {
1148 struct scsi_mode_sense_data {
1149 struct scsipi_mode_header header;
1150 union {
1151 struct page_element_address_assignment ea;
1152 struct page_transport_geometry_parameters tg;
1153 struct page_device_capabilities cap;
1154 } pages;
1155 } sense_data;
1156 int error, from;
1157 u_int8_t *moves, *exchanges;
1158
1159 /*
1160 * Grab info from the element address assignment page.
1161 */
1162 memset(&sense_data, 0, sizeof(sense_data));
1163 error = scsipi_mode_sense(sc->sc_periph, SMS_DBD, 0x1d,
1164 &sense_data.header, sizeof(sense_data),
1165 scsiflags | XS_CTL_DATA_ONSTACK, CHRETRIES, 6000);
1166 if (error) {
1167 printf("%s: could not sense element address page\n",
1168 sc->sc_dev.dv_xname);
1169 return (error);
1170 }
1171
1172 sc->sc_firsts[CHET_MT] = _2btol(sense_data.pages.ea.mtea);
1173 sc->sc_counts[CHET_MT] = _2btol(sense_data.pages.ea.nmte);
1174 sc->sc_firsts[CHET_ST] = _2btol(sense_data.pages.ea.fsea);
1175 sc->sc_counts[CHET_ST] = _2btol(sense_data.pages.ea.nse);
1176 sc->sc_firsts[CHET_IE] = _2btol(sense_data.pages.ea.fieea);
1177 sc->sc_counts[CHET_IE] = _2btol(sense_data.pages.ea.niee);
1178 sc->sc_firsts[CHET_DT] = _2btol(sense_data.pages.ea.fdtea);
1179 sc->sc_counts[CHET_DT] = _2btol(sense_data.pages.ea.ndte);
1180
1181 /* XXX ask for transport geometry page XXX */
1182
1183 /*
1184 * Grab info from the capabilities page.
1185 */
1186 memset(&sense_data, 0, sizeof(sense_data));
1187 /*
1188 * XXX: Note: not all changers can deal with disabled block descriptors
1189 */
1190 error = scsipi_mode_sense(sc->sc_periph, SMS_DBD, 0x1f,
1191 &sense_data.header, sizeof(sense_data),
1192 scsiflags | XS_CTL_DATA_ONSTACK, CHRETRIES, 6000);
1193 if (error) {
1194 printf("%s: could not sense capabilities page\n",
1195 sc->sc_dev.dv_xname);
1196 return (error);
1197 }
1198
1199 memset(sc->sc_movemask, 0, sizeof(sc->sc_movemask));
1200 memset(sc->sc_exchangemask, 0, sizeof(sc->sc_exchangemask));
1201 moves = &sense_data.pages.cap.move_from_mt;
1202 exchanges = &sense_data.pages.cap.exchange_with_mt;
1203 for (from = CHET_MT; from <= CHET_DT; ++from) {
1204 sc->sc_movemask[from] = moves[from];
1205 sc->sc_exchangemask[from] = exchanges[from];
1206 }
1207
1208 #ifdef CH_AUTOMATIC_IELEM_POLICY
1209 /*
1210 * If we need to do an Init-Element-Status,
1211 * do that now that we know what's in the changer.
1212 */
1213 if ((scsiflags & XS_CTL_IGNORE_MEDIA_CHANGE) == 0) {
1214 if ((sc->sc_periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
1215 error = ch_ielem(sc);
1216 if (error == 0)
1217 sc->sc_periph->periph_flags |= PERIPH_MEDIA_LOADED;
1218 else
1219 sc->sc_periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
1220 }
1221 #endif
1222 return (error);
1223 }
1224
1225 void
1226 ch_get_quirks(sc, inqbuf)
1227 struct ch_softc *sc;
1228 struct scsipi_inquiry_pattern *inqbuf;
1229 {
1230 struct chquirk *match;
1231 int priority;
1232
1233 sc->sc_settledelay = 0;
1234
1235 match = (struct chquirk *)scsipi_inqmatch(inqbuf,
1236 (caddr_t)chquirks,
1237 sizeof(chquirks) / sizeof(chquirks[0]),
1238 sizeof(chquirks[0]), &priority);
1239 if (priority != 0)
1240 sc->sc_settledelay = match->cq_settledelay;
1241 }
1242
1243 int
1244 ch_map_element(sc, elem, typep, unitp)
1245 struct ch_softc *sc;
1246 u_int16_t elem;
1247 int *typep, *unitp;
1248 {
1249 int chet;
1250
1251 for (chet = CHET_MT; chet <= CHET_DT; chet++) {
1252 if (elem >= sc->sc_firsts[chet] &&
1253 elem < (sc->sc_firsts[chet] + sc->sc_counts[chet])) {
1254 *typep = chet;
1255 *unitp = elem - sc->sc_firsts[chet];
1256 return (1);
1257 }
1258 }
1259 return (0);
1260 }
1261
1262 void
1263 ch_voltag_convert_in(sv, cv)
1264 const struct changer_volume_tag *sv;
1265 struct changer_voltag *cv;
1266 {
1267 int i;
1268
1269 memset(cv, 0, sizeof(struct changer_voltag));
1270
1271 /*
1272 * Copy the volume tag string from the SCSI representation.
1273 * Per the SCSI-2 spec, we stop at the first blank character.
1274 */
1275 for (i = 0; i < sizeof(sv->volid); i++) {
1276 if (sv->volid[i] == ' ')
1277 break;
1278 cv->cv_tag[i] = sv->volid[i];
1279 }
1280 cv->cv_tag[i] = '\0';
1281
1282 cv->cv_serial = _2btol(sv->volseq);
1283 }
1284
1285 int
1286 ch_voltag_convert_out(cv, sv)
1287 const struct changer_voltag *cv;
1288 struct changer_volume_tag *sv;
1289 {
1290 int i;
1291
1292 memset(sv, ' ', sizeof(struct changer_volume_tag));
1293
1294 for (i = 0; i < sizeof(sv->volid); i++) {
1295 if (cv->cv_tag[i] == '\0')
1296 break;
1297 /*
1298 * Limit the character set to what is suggested in
1299 * the SCSI-2 spec.
1300 */
1301 if ((cv->cv_tag[i] < '0' || cv->cv_tag[i] > '9') &&
1302 (cv->cv_tag[i] < 'A' || cv->cv_tag[i] > 'Z') &&
1303 (cv->cv_tag[i] != '_'))
1304 return (EINVAL);
1305 sv->volid[i] = cv->cv_tag[i];
1306 }
1307
1308 _lto2b(cv->cv_serial, sv->volseq);
1309
1310 return (0);
1311 }
1312