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