xref: /openbsd/sys/scsi/ch.c (revision 3cab2bb3)
1 /*	$OpenBSD: ch.c,v 1.65 2020/06/30 18:43:37 krw Exp $	*/
2 /*	$NetBSD: ch.c,v 1.26 1997/02/21 22:06:52 thorpej Exp $	*/
3 
4 /*
5  * Copyright (c) 1996, 1997 Jason R. Thorpe <thorpej@and.com>
6  * All rights reserved.
7  *
8  * Partially based on an autochanger driver written by Stefan Grefen
9  * and on an autochanger driver written by the Systems Programming Group
10  * at the University of Utah Computer Science Department.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgements:
22  *	This product includes software developed by Jason R. Thorpe
23  *	for And Communications, http://www.and.com/
24  * 4. The name of the author may not be used to endorse or promote products
25  *    derived from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
28  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
31  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
32  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
34  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
35  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  */
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/errno.h>
43 #include <sys/ioctl.h>
44 #include <sys/chio.h>
45 #include <sys/device.h>
46 #include <sys/malloc.h>
47 #include <sys/pool.h>
48 #include <sys/conf.h>
49 #include <sys/fcntl.h>
50 
51 #include <scsi/scsi_all.h>
52 #include <scsi/scsi_changer.h>
53 #include <scsi/scsiconf.h>
54 
55 #define CHRETRIES	2
56 #define CHUNIT(x)	(minor((x)))
57 
58 struct ch_softc {
59 	struct device		 sc_dev;	/* generic device info */
60 	struct scsi_link	*sc_link;	/* link in the SCSI bus */
61 
62 	int			 sc_picker;	/* current picker */
63 
64 	/*
65 	 * The following information is obtained from the
66 	 * element address assignment page.
67 	 */
68 	int			 sc_firsts[4];	/* firsts, indexed by CHET_* */
69 	int			 sc_counts[4];	/* counts, indexed by CHET_* */
70 
71 	/*
72 	 * The following mask defines the legal combinations
73 	 * of elements for the MOVE MEDIUM command.
74 	 */
75 	u_int8_t		 sc_movemask[4];
76 
77 	/*
78 	 * As above, but for EXCHANGE MEDIUM.
79 	 */
80 	u_int8_t		 sc_exchangemask[4];
81 
82 	int			 flags;		/* misc. info */
83 
84 	/*
85 	 * Quirks; see below.
86 	 */
87 	int			 sc_settledelay; /* delay for settle */
88 
89 };
90 
91 /* sc_flags */
92 #define CHF_ROTATE	0x01		/* picker can rotate */
93 
94 /* Autoconfiguration glue */
95 int	chmatch(struct device *, void *, void *);
96 void	chattach(struct device *, struct device *, void *);
97 
98 struct cfattach ch_ca = {
99 	sizeof(struct ch_softc), chmatch, chattach
100 };
101 
102 struct cfdriver ch_cd = {
103 	NULL, "ch", DV_DULL
104 };
105 
106 const struct scsi_inquiry_pattern ch_patterns[] = {
107 	{T_CHANGER, T_REMOV,
108 	 "",		"",		""},
109 };
110 
111 int	ch_move(struct ch_softc *, struct changer_move *);
112 int	ch_exchange(struct ch_softc *, struct changer_exchange *);
113 int	ch_position(struct ch_softc *, struct changer_position *);
114 int	ch_usergetelemstatus(struct ch_softc *,
115 	    struct changer_element_status_request *);
116 int	ch_getelemstatus(struct ch_softc *, int, int, caddr_t, size_t, int);
117 int	ch_get_params(struct ch_softc *, int);
118 int	ch_interpret_sense(struct scsi_xfer *xs);
119 void	ch_get_quirks(struct ch_softc *, struct scsi_inquiry_data *);
120 
121 /*
122  * SCSI changer quirks.
123  */
124 struct chquirk {
125 	struct	scsi_inquiry_pattern cq_match; /* device id pattern */
126 	int	cq_settledelay;	/* settle delay, in seconds */
127 };
128 
129 struct chquirk chquirks[] = {
130 	{{T_CHANGER, T_REMOV,
131 	  "SPECTRA",	"9000",		"0200"},
132 	 75},
133 };
134 
135 int
136 chmatch(struct device *parent, void *match, void *aux)
137 {
138 	struct scsi_attach_args		*sa = aux;
139 	struct scsi_inquiry_data	*inq = &sa->sa_sc_link->inqdata;
140 	int				 priority;
141 
142 	(void)scsi_inqmatch(inq, ch_patterns, nitems(ch_patterns),
143 	    sizeof(ch_patterns[0]), &priority);
144 
145 	return priority;
146 }
147 
148 void
149 chattach(struct device *parent, struct device *self, void *aux)
150 {
151 	struct ch_softc			*sc = (struct ch_softc *)self;
152 	struct scsi_attach_args		*sa = aux;
153 	struct scsi_link		*link = sa->sa_sc_link;
154 
155 	/* Glue into the SCSI bus */
156 	sc->sc_link = link;
157 	link->interpret_sense = ch_interpret_sense;
158 	link->device_softc = sc;
159 	link->openings = 1;
160 
161 	printf("\n");
162 
163 	/*
164 	 * Store our our device's quirks.
165 	 */
166 	ch_get_quirks(sc, &link->inqdata);
167 }
168 
169 int
170 chopen(dev_t dev, int flags, int fmt, struct proc *p)
171 {
172 	struct ch_softc		*sc;
173 	int			 oldcounts[4];
174 	int			 i, unit, error = 0;
175 
176 	unit = CHUNIT(dev);
177 	if ((unit >= ch_cd.cd_ndevs) ||
178 	    ((sc = ch_cd.cd_devs[unit]) == NULL))
179 		return ENXIO;
180 
181 	/*
182 	 * Only allow one open at a time.
183 	 */
184 	if (ISSET(sc->sc_link->flags, SDEV_OPEN))
185 		return EBUSY;
186 
187 	SET(sc->sc_link->flags, SDEV_OPEN);
188 
189 	/*
190 	 * Absorb any unit attention errors. We must notice
191 	 * "Not ready" errors as a changer will report "In the
192 	 * process of getting ready" any time it must rescan
193 	 * itself to determine the state of the changer.
194 	 */
195 	error = scsi_test_unit_ready(sc->sc_link, TEST_READY_RETRIES,
196 	    SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_MEDIA_CHANGE);
197 	if (error)
198 		goto bad;
199 
200 	/*
201 	 * Get information about the device. Save old information
202 	 * so we can decide whether to be verbose about new parameters.
203 	 */
204 	for (i = 0; i < 4; i++) {
205 		oldcounts[i] = sc->sc_counts[i];
206 	}
207 	error = ch_get_params(sc, scsi_autoconf);
208 	if (error)
209 		goto bad;
210 
211 	for (i = 0; i < 4; i++) {
212 		if (oldcounts[i] != sc->sc_counts[i]) {
213 			break;
214 		}
215 	}
216 	if (i < 4) {
217 #ifdef CHANGER_DEBUG
218 #define PLURAL(c)	(c) == 1 ? "" : "s"
219 		printf("%s: %d slot%s, %d drive%s, %d picker%s, %d portal%s\n",
220 		    sc->sc_dev.dv_xname,
221 		    sc->sc_counts[CHET_ST], PLURAL(sc->sc_counts[CHET_ST]),
222 		    sc->sc_counts[CHET_DT], PLURAL(sc->sc_counts[CHET_DT]),
223 		    sc->sc_counts[CHET_MT], PLURAL(sc->sc_counts[CHET_MT]),
224 		    sc->sc_counts[CHET_IE], PLURAL(sc->sc_counts[CHET_IE]));
225 #undef PLURAL
226 		printf("%s: move mask: 0x%x 0x%x 0x%x 0x%x\n",
227 		    sc->sc_dev.dv_xname,
228 		    sc->sc_movemask[CHET_MT], sc->sc_movemask[CHET_ST],
229 		    sc->sc_movemask[CHET_IE], sc->sc_movemask[CHET_DT]);
230 		printf("%s: exchange mask: 0x%x 0x%x 0x%x 0x%x\n",
231 		    sc->sc_dev.dv_xname,
232 		    sc->sc_exchangemask[CHET_MT], sc->sc_exchangemask[CHET_ST],
233 		    sc->sc_exchangemask[CHET_IE], sc->sc_exchangemask[CHET_DT]);
234 #endif /* CHANGER_DEBUG */
235 	}
236 
237 	/* Default the current picker. */
238 	sc->sc_picker = sc->sc_firsts[CHET_MT];
239 
240 	return 0;
241 
242 bad:
243 	CLR(sc->sc_link->flags, SDEV_OPEN);
244 	return error;
245 }
246 
247 int
248 chclose(dev_t dev, int flags, int fmt, struct proc *p)
249 {
250 	struct ch_softc *sc = ch_cd.cd_devs[CHUNIT(dev)];
251 
252 	CLR(sc->sc_link->flags, SDEV_OPEN);
253 	return 0;
254 }
255 
256 int
257 chioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
258 {
259 	struct ch_softc		*sc = ch_cd.cd_devs[CHUNIT(dev)];
260 	int			 error = 0;
261 
262 	/*
263 	 * If this command can change the device's state, we must
264 	 * have the device open for writing.
265 	 */
266 	switch (cmd) {
267 	case CHIOGPICKER:
268 	case CHIOGPARAMS:
269 	case CHIOGSTATUS:
270 		break;
271 
272 	default:
273 		if (!ISSET(flags, FWRITE))
274 			return EBADF;
275 	}
276 
277 	switch (cmd) {
278 	case CHIOMOVE:
279 		error = ch_move(sc, (struct changer_move *)data);
280 		break;
281 
282 	case CHIOEXCHANGE:
283 		error = ch_exchange(sc, (struct changer_exchange *)data);
284 		break;
285 
286 	case CHIOPOSITION:
287 		error = ch_position(sc, (struct changer_position *)data);
288 		break;
289 
290 	case CHIOGPICKER:
291 		*(int *)data = sc->sc_picker - sc->sc_firsts[CHET_MT];
292 		break;
293 
294 	case CHIOSPICKER:	{
295 		int new_picker = *(int *)data;
296 
297 		if (new_picker > (sc->sc_counts[CHET_MT] - 1))
298 			return EINVAL;
299 		sc->sc_picker = sc->sc_firsts[CHET_MT] + new_picker;
300 		break;		}
301 
302 	case CHIOGPARAMS:	{
303 		struct changer_params *cp = (struct changer_params *)data;
304 
305 		cp->cp_curpicker = sc->sc_picker - sc->sc_firsts[CHET_MT];
306 		cp->cp_npickers = sc->sc_counts[CHET_MT];
307 		cp->cp_nslots = sc->sc_counts[CHET_ST];
308 		cp->cp_nportals = sc->sc_counts[CHET_IE];
309 		cp->cp_ndrives = sc->sc_counts[CHET_DT];
310 		break;		}
311 
312 	case CHIOGSTATUS:	{
313 		struct changer_element_status_request *cesr =
314 		    (struct changer_element_status_request *)data;
315 
316 		error = ch_usergetelemstatus(sc, cesr);
317 		break;		}
318 
319 	/* Implement prevent/allow? */
320 
321 	default:
322 		error = scsi_do_ioctl(sc->sc_link, cmd, data, flags);
323 		break;
324 	}
325 
326 	return error;
327 }
328 
329 int
330 ch_move(struct ch_softc *sc, struct changer_move *cm)
331 {
332 	struct scsi_move_medium		*cmd;
333 	struct scsi_xfer		*xs;
334 	int				 error;
335 	u_int16_t			 fromelem, toelem;
336 
337 	/*
338 	 * Check arguments.
339 	 */
340 	if ((cm->cm_fromtype > CHET_DT) || (cm->cm_totype > CHET_DT))
341 		return EINVAL;
342 	if ((cm->cm_fromunit > (sc->sc_counts[cm->cm_fromtype] - 1)) ||
343 	    (cm->cm_tounit > (sc->sc_counts[cm->cm_totype] - 1)))
344 		return ENODEV;
345 
346 	/*
347 	 * Check the request against the changer's capabilities.
348 	 */
349 	if ((sc->sc_movemask[cm->cm_fromtype] & (1 << cm->cm_totype)) == 0)
350 		return EINVAL;
351 
352 	/*
353 	 * Calculate the source and destination elements.
354 	 */
355 	fromelem = sc->sc_firsts[cm->cm_fromtype] + cm->cm_fromunit;
356 	toelem = sc->sc_firsts[cm->cm_totype] + cm->cm_tounit;
357 
358 	/*
359 	 * Build the SCSI command.
360 	 */
361 	xs = scsi_xs_get(sc->sc_link, 0);
362 	if (xs == NULL)
363 		return ENOMEM;
364 	xs->cmdlen = sizeof(*cmd);
365 	xs->retries = CHRETRIES;
366 	xs->timeout = 100000;
367 
368 	cmd = (struct scsi_move_medium *)xs->cmd;
369 	cmd->opcode = MOVE_MEDIUM;
370 	_lto2b(sc->sc_picker, cmd->tea);
371 	_lto2b(fromelem, cmd->src);
372 	_lto2b(toelem, cmd->dst);
373 	if (ISSET(cm->cm_flags, CM_INVERT))
374 		SET(cmd->flags, MOVE_MEDIUM_INVERT);
375 
376 	error = scsi_xs_sync(xs);
377 	scsi_xs_put(xs);
378 
379 	return error;
380 }
381 
382 int
383 ch_exchange(struct ch_softc *sc, struct changer_exchange *ce)
384 {
385 	struct scsi_exchange_medium	*cmd;
386 	struct scsi_xfer		*xs;
387 	int				 error;
388 	u_int16_t			 src, dst1, dst2;
389 
390 	/*
391 	 * Check arguments.
392 	 */
393 	if ((ce->ce_srctype > CHET_DT) || (ce->ce_fdsttype > CHET_DT) ||
394 	    (ce->ce_sdsttype > CHET_DT))
395 		return EINVAL;
396 	if ((ce->ce_srcunit > (sc->sc_counts[ce->ce_srctype] - 1)) ||
397 	    (ce->ce_fdstunit > (sc->sc_counts[ce->ce_fdsttype] - 1)) ||
398 	    (ce->ce_sdstunit > (sc->sc_counts[ce->ce_sdsttype] - 1)))
399 		return ENODEV;
400 
401 	/*
402 	 * Check the request against the changer's capabilities.
403 	 */
404 	if (((sc->sc_exchangemask[ce->ce_srctype] &
405 	    (1 << ce->ce_fdsttype)) == 0) ||
406 	    ((sc->sc_exchangemask[ce->ce_fdsttype] &
407 	    (1 << ce->ce_sdsttype)) == 0))
408 		return EINVAL;
409 
410 	/*
411 	 * Calculate the source and destination elements.
412 	 */
413 	src = sc->sc_firsts[ce->ce_srctype] + ce->ce_srcunit;
414 	dst1 = sc->sc_firsts[ce->ce_fdsttype] + ce->ce_fdstunit;
415 	dst2 = sc->sc_firsts[ce->ce_sdsttype] + ce->ce_sdstunit;
416 
417 	/*
418 	 * Build the SCSI command.
419 	 */
420 	xs = scsi_xs_get(sc->sc_link, 0);
421 	if (xs == NULL)
422 		return ENOMEM;
423 	xs->cmdlen = sizeof(*cmd);
424 	xs->retries = CHRETRIES;
425 	xs->timeout = 100000;
426 
427 	cmd = (struct scsi_exchange_medium *)xs->cmd;
428 	cmd->opcode = EXCHANGE_MEDIUM;
429 	_lto2b(sc->sc_picker, cmd->tea);
430 	_lto2b(src, cmd->src);
431 	_lto2b(dst1, cmd->fdst);
432 	_lto2b(dst2, cmd->sdst);
433 	if (ISSET(ce->ce_flags, CE_INVERT1))
434 		SET(cmd->flags, EXCHANGE_MEDIUM_INV1);
435 	if (ISSET(ce->ce_flags, CE_INVERT2))
436 		SET(cmd->flags, EXCHANGE_MEDIUM_INV2);
437 
438 	error = scsi_xs_sync(xs);
439 	scsi_xs_put(xs);
440 
441 	return error;
442 }
443 
444 int
445 ch_position(struct ch_softc *sc, struct changer_position *cp)
446 {
447 	struct scsi_position_to_element		*cmd;
448 	struct scsi_xfer			*xs;
449 	int					 error;
450 	u_int16_t				 dst;
451 
452 	/*
453 	 * Check arguments.
454 	 */
455 	if (cp->cp_type > CHET_DT)
456 		return EINVAL;
457 	if (cp->cp_unit > (sc->sc_counts[cp->cp_type] - 1))
458 		return ENODEV;
459 
460 	/*
461 	 * Calculate the destination element.
462 	 */
463 	dst = sc->sc_firsts[cp->cp_type] + cp->cp_unit;
464 
465 	/*
466 	 * Build the SCSI command.
467 	 */
468 	xs = scsi_xs_get(sc->sc_link, 0);
469 	if (xs == NULL)
470 		return ENOMEM;
471 	xs->cmdlen = sizeof(*cmd);
472 	xs->retries = CHRETRIES;
473 	xs->timeout = 100000;
474 
475 	cmd = (struct scsi_position_to_element *)xs->cmd;
476 	cmd->opcode = POSITION_TO_ELEMENT;
477 	_lto2b(sc->sc_picker, cmd->tea);
478 	_lto2b(dst, cmd->dst);
479 	if (ISSET(cp->cp_flags, CP_INVERT))
480 		SET(cmd->flags, POSITION_TO_ELEMENT_INVERT);
481 
482 	error = scsi_xs_sync(xs);
483 	scsi_xs_put(xs);
484 
485 	return error;
486 }
487 
488 /*
489  * Copy a volume tag to a volume_tag struct, converting SCSI byte order
490  * to host native byte order in the volume serial number.  The volume
491  * label as returned by the changer is transferred to user mode as
492  * nul-terminated string.  Volume labels are truncated at the first
493  * space, as suggested by SCSI-2.
494  */
495 static  void
496 copy_voltag(struct changer_voltag *uvoltag, struct volume_tag *voltag)
497 {
498 	int i;
499 
500 	for (i=0; i<CH_VOLTAG_MAXLEN; i++) {
501 		char c = voltag->vif[i];
502 		if (c && c != ' ')
503 			uvoltag->cv_volid[i] = c;
504 		else
505 			break;
506 	}
507 	uvoltag->cv_volid[i] = '\0';
508 	uvoltag->cv_serial = _2btol(voltag->vsn);
509 }
510 
511 /*
512  * Copy an an element status descriptor to a user-mode
513  * changer_element_status structure.
514  */
515 static void
516 copy_element_status(int flags,	struct read_element_status_descriptor *desc,
517     struct changer_element_status *ces)
518 {
519 	ces->ces_flags = desc->flags1;
520 
521 	if (ISSET(flags, READ_ELEMENT_STATUS_PVOLTAG))
522 		copy_voltag(&ces->ces_pvoltag, &desc->pvoltag);
523 	if (ISSET(flags, READ_ELEMENT_STATUS_AVOLTAG))
524 		copy_voltag(&ces->ces_avoltag, &desc->avoltag);
525 }
526 
527 /*
528  * Perform a READ ELEMENT STATUS on behalf of the user, and return to
529  * the user only the data the user is interested in (i.e. an array of
530  * changer_element_status structures)
531  */
532 int
533 ch_usergetelemstatus(struct ch_softc *sc,
534     struct changer_element_status_request *cesr)
535 {
536 	struct changer_element_status		*user_data = NULL;
537 	struct read_element_status_header	*st_hdr;
538 	struct read_element_status_page_header	*pg_hdr;
539 	caddr_t					 desc;
540 	caddr_t					 data = NULL;
541 	size_t					 size, desclen, udsize;
542 	int					 avail, chet, i, want_voltags;
543 	int					 error = 0;
544 
545 	chet = cesr->cesr_type;
546 	want_voltags = (cesr->cesr_flags & CESR_VOLTAGS) ? 1 : 0;
547 
548 	/*
549 	 * If there are no elements of the requested type in the changer,
550 	 * the request is invalid.
551 	 */
552 	if (sc->sc_counts[chet] == 0)
553 		return EINVAL;
554 
555 	/*
556 	 * Request one descriptor for the given element type.  This
557 	 * is used to determine the size of the descriptor so that
558 	 * we can allocate enough storage for all of them.  We assume
559 	 * that the first one can fit into 1k.
560 	 */
561 	size = 1024;
562 	data = dma_alloc(size, PR_WAITOK);
563 	error = ch_getelemstatus(sc, sc->sc_firsts[chet], 1, data, size,
564 	    want_voltags);
565 	if (error)
566 		goto done;
567 
568 	st_hdr = (struct read_element_status_header *)data;
569 	pg_hdr = (struct read_element_status_page_header *) (st_hdr + 1);
570 	desclen = _2btol(pg_hdr->edl);
571 
572 	dma_free(data, size);
573 
574 	/*
575 	 * Reallocate storage for descriptors and get them from the
576 	 * device.
577 	 */
578 	size = sizeof(struct read_element_status_header) +
579 	    sizeof(struct read_element_status_page_header) +
580 	    (desclen * sc->sc_counts[chet]);
581 	data = dma_alloc(size, PR_WAITOK);
582 	error = ch_getelemstatus(sc, sc->sc_firsts[chet],
583 	    sc->sc_counts[chet], data, size, want_voltags);
584 	if (error)
585 		goto done;
586 
587 	/*
588 	 * Fill in the user status array.
589 	 */
590 	st_hdr = (struct read_element_status_header *)data;
591 	pg_hdr = (struct read_element_status_page_header *) (st_hdr + 1);
592 
593 	avail = _2btol(st_hdr->count);
594 	if (avail != sc->sc_counts[chet]) {
595 		error = EINVAL;
596 		goto done;
597 	}
598 
599 	user_data = mallocarray(avail, sizeof(struct changer_element_status),
600 	    M_DEVBUF, M_WAITOK | M_ZERO);
601 	udsize = avail * sizeof(struct changer_element_status);
602 
603 	desc = (caddr_t)(pg_hdr + 1);
604 	for (i = 0; i < avail; ++i) {
605 		struct changer_element_status *ces = &(user_data[i]);
606 		copy_element_status(pg_hdr->flags,
607 		    (struct read_element_status_descriptor *)desc, ces);
608 		desc += desclen;
609 	}
610 
611 	/* Copy array out to userspace. */
612 	error = copyout(user_data, cesr->cesr_data, udsize);
613 
614 done:
615 	if (data != NULL)
616 		dma_free(data, size);
617 	if (user_data != NULL)
618 		free(user_data, M_DEVBUF, udsize);
619 	return error;
620 }
621 
622 int
623 ch_getelemstatus(struct ch_softc *sc, int first, int count, caddr_t data,
624     size_t datalen, int voltag)
625 {
626 	struct scsi_read_element_status		*cmd;
627 	struct scsi_xfer			*xs;
628 	int					 error;
629 
630 	/*
631 	 * Build SCSI command.
632 	 */
633 	xs = scsi_xs_get(sc->sc_link, SCSI_DATA_IN);
634 	if (xs == NULL)
635 		return ENOMEM;
636 	xs->cmdlen = sizeof(*cmd);
637 	xs->data = data;
638 	xs->datalen = datalen;
639 	xs->retries = CHRETRIES;
640 	xs->timeout = 100000;
641 
642 	cmd = (struct scsi_read_element_status *)xs->cmd;
643 	cmd->opcode = READ_ELEMENT_STATUS;
644 	_lto2b(first, cmd->sea);
645 	_lto2b(count, cmd->count);
646 	_lto3b(datalen, cmd->len);
647 	if (voltag)
648 		SET(cmd->byte2, READ_ELEMENT_STATUS_VOLTAG);
649 
650 	error = scsi_xs_sync(xs);
651 	scsi_xs_put(xs);
652 
653 	return error;
654 }
655 
656 /*
657  * Ask the device about itself and fill in the parameters in our
658  * softc.
659  */
660 int
661 ch_get_params(struct ch_softc *sc, int flags)
662 {
663 	union scsi_mode_sense_buf			*data;
664 	struct page_element_address_assignment		*ea;
665 	struct page_device_capabilities			*cap;
666 	u_int8_t					*moves, *exchanges;
667 	int						 big, error, from;
668 
669 	data = dma_alloc(sizeof(*data), PR_NOWAIT);
670 	if (data == NULL)
671 		return ENOMEM;
672 
673 	/*
674 	 * Grab info from the element address assignment page (0x1d).
675 	 */
676 	error = scsi_do_mode_sense(sc->sc_link, EA_PAGE, data,
677 	    (void **)&ea, sizeof(*ea), flags, &big);
678 	if (error == 0 && ea == NULL)
679 		error = EIO;
680 	if (error != 0) {
681 #ifdef CHANGER_DEBUG
682 		printf("%s: could not sense element address page\n",
683 		    sc->sc_dev.dv_xname);
684 #endif /* CHANGER_DEBUG */
685 		dma_free(data, sizeof(*data));
686 		return error;
687 	}
688 
689 	sc->sc_firsts[CHET_MT] = _2btol(ea->mtea);
690 	sc->sc_counts[CHET_MT] = _2btol(ea->nmte);
691 	sc->sc_firsts[CHET_ST] = _2btol(ea->fsea);
692 	sc->sc_counts[CHET_ST] = _2btol(ea->nse);
693 	sc->sc_firsts[CHET_IE] = _2btol(ea->fieea);
694 	sc->sc_counts[CHET_IE] = _2btol(ea->niee);
695 	sc->sc_firsts[CHET_DT] = _2btol(ea->fdtea);
696 	sc->sc_counts[CHET_DT] = _2btol(ea->ndte);
697 
698 	/* XXX Ask for transport geometry page. */
699 
700 	/*
701 	 * Grab info from the capabilities page (0x1f).
702 	 */
703 	error = scsi_do_mode_sense(sc->sc_link, CAP_PAGE, data,
704 	    (void **)&cap, sizeof(*cap), flags, &big);
705 	if (error == 0 && cap == NULL)
706 		error = EIO;
707 	if (error != 0) {
708 #ifdef CHANGER_DEBUG
709 		printf("%s: could not sense capabilities page\n",
710 		    sc->sc_dev.dv_xname);
711 #endif /* CHANGER_DEBUG */
712 		dma_free(data, sizeof(*data));
713 		return error;
714 	}
715 
716 	bzero(sc->sc_movemask, sizeof(sc->sc_movemask));
717 	bzero(sc->sc_exchangemask, sizeof(sc->sc_exchangemask));
718 	moves = &cap->move_from_mt;
719 	exchanges = &cap->exchange_with_mt;
720 	for (from = CHET_MT; from <= CHET_DT; ++from) {
721 		sc->sc_movemask[from] = moves[from];
722 		sc->sc_exchangemask[from] = exchanges[from];
723 	}
724 
725 	SET(sc->sc_link->flags, SDEV_MEDIA_LOADED);
726 	dma_free(data, sizeof(*data));
727 	return 0;
728 }
729 
730 void
731 ch_get_quirks(struct ch_softc *sc, struct scsi_inquiry_data *inqbuf)
732 {
733 	const struct chquirk		*match;
734 	int				 priority;
735 
736 	sc->sc_settledelay = 0;
737 
738 	match = (const struct chquirk *)scsi_inqmatch(inqbuf,
739 	    (caddr_t)chquirks,
740 	    sizeof(chquirks) / sizeof(chquirks[0]),
741 	    sizeof(chquirks[0]), &priority);
742 	if (priority != 0) {
743 		sc->sc_settledelay = match->cq_settledelay;
744 	}
745 }
746 
747 /*
748  * Look at the returned sense and act on the error and detirmine
749  * The unix error number to pass back... (0 = report no error)
750  *                            (-1 = continue processing)
751  */
752 int
753 ch_interpret_sense(struct scsi_xfer *xs)
754 {
755 	struct scsi_sense_data		*sense = &xs->sense;
756 	struct scsi_link		*link = xs->sc_link;
757 	u_int8_t			 serr, skey;
758 
759 	serr = sense->error_code & SSD_ERRCODE;
760 	skey = sense->flags & SSD_KEY;
761 
762 	if (!ISSET(link->flags, SDEV_OPEN) ||
763 	    (serr != SSD_ERRCODE_CURRENT && serr != SSD_ERRCODE_DEFERRED))
764 		return scsi_interpret_sense(xs);
765 
766 	switch (skey) {
767 
768 	/*
769 	 * We do custom processing in ch for the unit becoming ready
770 	 * case.  in this case we do not allow xs->retries to be
771 	 * decremented only on the "Unit Becoming Ready" case. This is
772 	 * because tape changers report "Unit Becoming Ready" when they
773 	 * rescan their state (i.e. when the door got opened) and can
774 	 * take a long time for large units. Rather than having a
775 	 * massive timeout for all operations (which would cause other
776 	 * problems) we allow changers to wait (but be interruptable
777 	 * with Ctrl-C) forever as long as they are reporting that they
778 	 * are becoming ready.  all other cases are handled as per the
779 	 * default.
780 	 */
781 	case SKEY_NOT_READY:
782 		if (ISSET(xs->flags, SCSI_IGNORE_NOT_READY))
783 			return 0;
784 		switch (ASC_ASCQ(sense)) {
785 		case SENSE_NOT_READY_BECOMING_READY:
786 			SC_DEBUG(link, SDEV_DB1, ("not ready: busy (%#x)\n",
787 			    sense->add_sense_code_qual));
788 			/* don't count this as a retry */
789 			xs->retries++;
790 			return scsi_delay(xs, 1);
791 		default:
792 			return scsi_interpret_sense(xs);
793 		}
794 	default:
795 		return scsi_interpret_sense(xs);
796 	}
797 }
798