ch.c revision 1.20 1 1.20 thorpej /* $NetBSD: ch.c,v 1.20 1996/04/03 00:25:39 thorpej Exp $ */
2 1.8 cgd
3 1.1 cgd /*
4 1.20 thorpej * Copyright (c) 1996 Jason R. Thorpe <thorpej (at) and.com>
5 1.20 thorpej * All rights reserved.
6 1.20 thorpej *
7 1.20 thorpej * Partially based on an autochanger driver written by Stefan Grefen
8 1.20 thorpej * and on an autochanger driver written by the Systems Programming Group
9 1.20 thorpej * at the University of Utah Computer Science Department.
10 1.6 mycroft *
11 1.6 mycroft * Redistribution and use in source and binary forms, with or without
12 1.6 mycroft * modification, are permitted provided that the following conditions
13 1.6 mycroft * are met:
14 1.6 mycroft * 1. Redistributions of source code must retain the above copyright
15 1.6 mycroft * notice, this list of conditions and the following disclaimer.
16 1.6 mycroft * 2. Redistributions in binary form must reproduce the above copyright
17 1.6 mycroft * notice, this list of conditions and the following disclaimer in the
18 1.6 mycroft * documentation and/or other materials provided with the distribution.
19 1.6 mycroft * 3. All advertising materials mentioning features or use of this software
20 1.20 thorpej * must display the following acknowledgements:
21 1.20 thorpej * This product includes software developed by Jason R. Thorpe
22 1.20 thorpej * for And Communications, http://www.and.com/
23 1.6 mycroft * 4. The name of the author may not be used to endorse or promote products
24 1.6 mycroft * derived from this software without specific prior written permission.
25 1.6 mycroft *
26 1.6 mycroft * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27 1.6 mycroft * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 1.6 mycroft * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 1.6 mycroft * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30 1.20 thorpej * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31 1.20 thorpej * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 1.20 thorpej * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
33 1.20 thorpej * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
34 1.20 thorpej * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.20 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.20 thorpej * SUCH DAMAGE.
37 1.6 mycroft */
38 1.1 cgd
39 1.1 cgd #include <sys/param.h>
40 1.1 cgd #include <sys/systm.h>
41 1.1 cgd #include <sys/errno.h>
42 1.1 cgd #include <sys/ioctl.h>
43 1.1 cgd #include <sys/buf.h>
44 1.1 cgd #include <sys/proc.h>
45 1.1 cgd #include <sys/user.h>
46 1.20 thorpej #include <sys/chio.h>
47 1.6 mycroft #include <sys/device.h>
48 1.20 thorpej #include <sys/malloc.h>
49 1.1 cgd
50 1.1 cgd #include <scsi/scsi_all.h>
51 1.1 cgd #include <scsi/scsi_changer.h>
52 1.1 cgd #include <scsi/scsiconf.h>
53 1.1 cgd
54 1.20 thorpej #define CHRETRIES 2
55 1.20 thorpej #define CHUNIT(x) (minor((x)))
56 1.20 thorpej
57 1.20 thorpej struct ch_softc {
58 1.20 thorpej struct device sc_dev; /* generic device info */
59 1.20 thorpej struct scsi_link *sc_link; /* link in the SCSI bus */
60 1.20 thorpej
61 1.20 thorpej int sc_picker; /* current picker */
62 1.1 cgd
63 1.20 thorpej /*
64 1.20 thorpej * The following information is obtained from the
65 1.20 thorpej * element address assignment page.
66 1.20 thorpej */
67 1.20 thorpej int sc_firsts[4]; /* firsts, indexed by CHET_* */
68 1.20 thorpej int sc_counts[4]; /* counts, indexed by CHET_* */
69 1.1 cgd
70 1.20 thorpej /*
71 1.20 thorpej * The following mask defines the legal combinations
72 1.20 thorpej * of elements for the MOVE MEDIUM command.
73 1.20 thorpej */
74 1.20 thorpej u_int8_t sc_movemask[4];
75 1.20 thorpej
76 1.20 thorpej /*
77 1.20 thorpej * As above, but for EXCHANGE MEDIUM.
78 1.20 thorpej */
79 1.20 thorpej u_int8_t sc_exchangemask[4];
80 1.1 cgd
81 1.20 thorpej int flags; /* misc. info */
82 1.6 mycroft };
83 1.6 mycroft
84 1.20 thorpej /* sc_flags */
85 1.20 thorpej #define CHF_ROTATE 0x01 /* picker can rotate */
86 1.20 thorpej
87 1.20 thorpej /* Autoconfiguration glue */
88 1.15 christos int chmatch __P((struct device *, void *, void *));
89 1.15 christos void chattach __P((struct device *, struct device *, void *));
90 1.6 mycroft
91 1.17 thorpej struct cfattach ch_ca = {
92 1.17 thorpej sizeof(struct ch_softc), chmatch, chattach
93 1.17 thorpej };
94 1.17 thorpej
95 1.17 thorpej struct cfdriver ch_cd = {
96 1.17 thorpej NULL, "ch", DV_DULL
97 1.6 mycroft };
98 1.1 cgd
99 1.20 thorpej struct scsi_inquiry_pattern ch_patterns[] = {
100 1.20 thorpej {T_CHANGER, T_REMOV,
101 1.20 thorpej "", "", ""},
102 1.20 thorpej };
103 1.20 thorpej
104 1.20 thorpej /* SCSI glue */
105 1.6 mycroft struct scsi_device ch_switch = {
106 1.20 thorpej NULL, NULL, NULL, NULL
107 1.6 mycroft };
108 1.6 mycroft
109 1.20 thorpej int ch_move __P((struct ch_softc *, struct changer_move *));
110 1.20 thorpej int ch_exchange __P((struct ch_softc *, struct changer_exchange *));
111 1.20 thorpej int ch_position __P((struct ch_softc *, struct changer_position *));
112 1.20 thorpej int ch_usergetelemstatus __P((struct ch_softc *, int, u_int8_t *));
113 1.20 thorpej int ch_getelemstatus __P((struct ch_softc *, int, int, caddr_t, size_t));
114 1.20 thorpej int ch_get_params __P((struct ch_softc *, int));
115 1.13 mycroft
116 1.13 mycroft int
117 1.13 mycroft chmatch(parent, match, aux)
118 1.13 mycroft struct device *parent;
119 1.13 mycroft void *match, *aux;
120 1.13 mycroft {
121 1.13 mycroft struct scsibus_attach_args *sa = aux;
122 1.13 mycroft int priority;
123 1.13 mycroft
124 1.13 mycroft (void)scsi_inqmatch(sa->sa_inqbuf,
125 1.13 mycroft (caddr_t)ch_patterns, sizeof(ch_patterns)/sizeof(ch_patterns[0]),
126 1.13 mycroft sizeof(ch_patterns[0]), &priority);
127 1.20 thorpej
128 1.13 mycroft return (priority);
129 1.13 mycroft }
130 1.13 mycroft
131 1.20 thorpej void
132 1.6 mycroft chattach(parent, self, aux)
133 1.6 mycroft struct device *parent, *self;
134 1.6 mycroft void *aux;
135 1.1 cgd {
136 1.20 thorpej struct ch_softc *sc = (struct ch_softc *)self;
137 1.13 mycroft struct scsibus_attach_args *sa = aux;
138 1.20 thorpej struct scsi_link *link = sa->sa_sc_link;
139 1.20 thorpej
140 1.20 thorpej /* Glue into the SCSI bus */
141 1.20 thorpej sc->sc_link = link;
142 1.20 thorpej link->device = &ch_switch;
143 1.20 thorpej link->device_softc = sc;
144 1.20 thorpej link->openings = 1;
145 1.1 cgd
146 1.20 thorpej printf("\n");
147 1.1 cgd
148 1.6 mycroft /*
149 1.20 thorpej * Get information about the device. Note we can't use
150 1.20 thorpej * interrupts yet.
151 1.6 mycroft */
152 1.20 thorpej if (ch_get_params(sc, SCSI_AUTOCONF))
153 1.20 thorpej printf("%s: offline\n", sc->sc_dev.dv_xname);
154 1.20 thorpej else {
155 1.20 thorpej printf("%s: %d slot%s, %d drive%s, %d picker%s",
156 1.20 thorpej sc->sc_dev.dv_xname,
157 1.20 thorpej sc->sc_counts[CHET_ST], (sc->sc_counts[CHET_ST] > 1) ?
158 1.20 thorpej "s" : "",
159 1.20 thorpej sc->sc_counts[CHET_DT], (sc->sc_counts[CHET_DT] > 1) ?
160 1.20 thorpej "s" : "",
161 1.20 thorpej sc->sc_counts[CHET_MT], (sc->sc_counts[CHET_MT] > 1) ?
162 1.20 thorpej "s" : "");
163 1.20 thorpej if (sc->sc_counts[CHET_IE])
164 1.20 thorpej printf(", %d portal%s", sc->sc_counts[CHET_IE],
165 1.20 thorpej (sc->sc_counts[CHET_IE] > 1) ? "s" : "");
166 1.20 thorpej printf("\n");
167 1.20 thorpej #ifdef CHANGER_DEBUG
168 1.20 thorpej printf("%s: move mask: 0x%x 0x%x 0x%x 0x%x\n",
169 1.20 thorpej sc->sc_dev.dv_xname,
170 1.20 thorpej sc->sc_movemask[CHET_MT], sc->sc_movemask[CHET_ST],
171 1.20 thorpej sc->sc_movemask[CHET_IE], sc->sc_movemask[CHET_DT]);
172 1.20 thorpej printf("%s: exchange mask: 0x%x 0x%x 0x%x 0x%x\n",
173 1.20 thorpej sc->sc_dev.dv_xname,
174 1.20 thorpej sc->sc_exchangemask[CHET_MT], sc->sc_exchangemask[CHET_ST],
175 1.20 thorpej sc->sc_exchangemask[CHET_IE], sc->sc_exchangemask[CHET_DT]);
176 1.20 thorpej #endif /* CHANGER_DEBUG */
177 1.20 thorpej }
178 1.6 mycroft
179 1.20 thorpej /* Default the current picker. */
180 1.20 thorpej sc->sc_picker = sc->sc_firsts[CHET_MT];
181 1.1 cgd }
182 1.1 cgd
183 1.20 thorpej int
184 1.20 thorpej chopen(dev, flags, fmt, p)
185 1.6 mycroft dev_t dev;
186 1.20 thorpej int flags, fmt;
187 1.15 christos struct proc *p;
188 1.1 cgd {
189 1.20 thorpej struct ch_softc *sc;
190 1.20 thorpej int unit, error = 0;
191 1.6 mycroft
192 1.6 mycroft unit = CHUNIT(dev);
193 1.20 thorpej if ((unit >= ch_cd.cd_ndevs) ||
194 1.20 thorpej ((sc = ch_cd.cd_devs[unit]) == NULL))
195 1.20 thorpej return (ENXIO);
196 1.12 mycroft
197 1.12 mycroft /*
198 1.20 thorpej * Only allow one open at a time.
199 1.12 mycroft */
200 1.20 thorpej if (sc->sc_link->flags & SDEV_OPEN)
201 1.20 thorpej return (EBUSY);
202 1.20 thorpej
203 1.20 thorpej sc->sc_link->flags |= SDEV_OPEN;
204 1.6 mycroft
205 1.6 mycroft /*
206 1.20 thorpej * Absorb any unit attention errors. Ignore "not ready"
207 1.20 thorpej * since this might occur if e.g. a tape isn't actually
208 1.20 thorpej * loaded in the drive.
209 1.6 mycroft */
210 1.20 thorpej if (error = scsi_test_unit_ready(sc->sc_link,
211 1.20 thorpej SCSI_IGNORE_NOT_READY|SCSI_IGNORE_MEDIA_CHANGE))
212 1.13 mycroft goto bad;
213 1.6 mycroft
214 1.6 mycroft /*
215 1.20 thorpej * Make sure our parameters are up to date.
216 1.6 mycroft */
217 1.20 thorpej if (error = ch_get_params(sc, 0))
218 1.12 mycroft goto bad;
219 1.6 mycroft
220 1.20 thorpej return (0);
221 1.12 mycroft
222 1.20 thorpej bad:
223 1.20 thorpej sc->sc_link->flags &= ~SDEV_OPEN;
224 1.20 thorpej return (error);
225 1.6 mycroft }
226 1.6 mycroft
227 1.20 thorpej int
228 1.20 thorpej chclose(dev, flags, fmt, p)
229 1.6 mycroft dev_t dev;
230 1.20 thorpej int flags, fmt;
231 1.15 christos struct proc *p;
232 1.1 cgd {
233 1.20 thorpej struct ch_softc *sc = ch_cd.cd_devs[CHUNIT(dev)];
234 1.6 mycroft
235 1.20 thorpej sc->sc_link->flags &= ~SDEV_OPEN;
236 1.20 thorpej return (0);
237 1.6 mycroft }
238 1.6 mycroft
239 1.20 thorpej int
240 1.20 thorpej chioctl(dev, cmd, data, flags, p)
241 1.6 mycroft dev_t dev;
242 1.10 cgd u_long cmd;
243 1.20 thorpej caddr_t data;
244 1.20 thorpej int flags;
245 1.13 mycroft struct proc *p;
246 1.6 mycroft {
247 1.20 thorpej struct ch_softc *sc = ch_cd.cd_devs[CHUNIT(dev)];
248 1.20 thorpej caddr_t elemdata;
249 1.20 thorpej int error = 0;
250 1.20 thorpej
251 1.20 thorpej switch (cmd) {
252 1.20 thorpej case CHIOMOVE:
253 1.20 thorpej error = ch_move(sc, (struct changer_move *)data);
254 1.20 thorpej break;
255 1.20 thorpej
256 1.20 thorpej case CHIOEXCHANGE:
257 1.20 thorpej error = ch_exchange(sc, (struct changer_exchange *)data);
258 1.20 thorpej break;
259 1.20 thorpej
260 1.20 thorpej case CHIOPOSITION:
261 1.20 thorpej error = ch_position(sc, (struct changer_position *)data);
262 1.20 thorpej break;
263 1.20 thorpej
264 1.20 thorpej case CHIOGPICKER:
265 1.20 thorpej *(int *)data = sc->sc_picker - sc->sc_firsts[CHET_MT];
266 1.20 thorpej break;
267 1.20 thorpej
268 1.20 thorpej case CHIOSPICKER: {
269 1.20 thorpej int new_picker = *(int *)data;
270 1.20 thorpej
271 1.20 thorpej if (new_picker > (sc->sc_counts[CHET_MT] - 1))
272 1.20 thorpej return (EINVAL);
273 1.20 thorpej sc->sc_picker = sc->sc_firsts[CHET_MT] + new_picker;
274 1.20 thorpej break; }
275 1.20 thorpej
276 1.20 thorpej case CHIOGPARAMS: {
277 1.20 thorpej struct changer_params *cp = (struct changer_params *)data;
278 1.20 thorpej
279 1.20 thorpej cp->cp_curpicker = sc->sc_picker - sc->sc_firsts[CHET_MT];
280 1.20 thorpej cp->cp_npickers = sc->sc_counts[CHET_MT];
281 1.20 thorpej cp->cp_nslots = sc->sc_counts[CHET_ST];
282 1.20 thorpej cp->cp_nportals = sc->sc_counts[CHET_IE];
283 1.20 thorpej cp->cp_ndrives = sc->sc_counts[CHET_DT];
284 1.20 thorpej break; }
285 1.20 thorpej
286 1.20 thorpej case CHIOGSTATUS: {
287 1.20 thorpej struct changer_element_status *ces =
288 1.20 thorpej (struct changer_element_status *)data;
289 1.20 thorpej
290 1.20 thorpej error = ch_usergetelemstatus(sc, ces->ces_type, ces->ces_data);
291 1.20 thorpej break; }
292 1.6 mycroft
293 1.20 thorpej /* Implement prevent/allow? */
294 1.6 mycroft
295 1.1 cgd default:
296 1.20 thorpej error = scsi_do_ioctl(sc->sc_link, dev, cmd, data, flags, p);
297 1.20 thorpej break;
298 1.1 cgd }
299 1.20 thorpej
300 1.20 thorpej return (error);
301 1.1 cgd }
302 1.1 cgd
303 1.20 thorpej int
304 1.20 thorpej ch_move(sc, cm)
305 1.20 thorpej struct ch_softc *sc;
306 1.20 thorpej struct changer_move *cm;
307 1.1 cgd {
308 1.20 thorpej struct scsi_move_medium cmd;
309 1.20 thorpej u_int16_t fromelem, toelem;
310 1.20 thorpej
311 1.20 thorpej /*
312 1.20 thorpej * Check arguments.
313 1.20 thorpej */
314 1.20 thorpej if ((cm->cm_fromtype > CHET_DT) || (cm->cm_totype > CHET_DT))
315 1.20 thorpej return (EINVAL);
316 1.20 thorpej if ((cm->cm_fromunit > (sc->sc_counts[cm->cm_fromtype] - 1)) ||
317 1.20 thorpej (cm->cm_tounit > (sc->sc_counts[cm->cm_totype] - 1)))
318 1.20 thorpej return (ENODEV);
319 1.20 thorpej
320 1.20 thorpej /*
321 1.20 thorpej * Check the request against the changer's capabilities.
322 1.20 thorpej */
323 1.20 thorpej if ((sc->sc_movemask[cm->cm_fromtype] & (1 << cm->cm_totype)) == 0)
324 1.20 thorpej return (EINVAL);
325 1.20 thorpej
326 1.20 thorpej /*
327 1.20 thorpej * Calculate the source and destination elements.
328 1.20 thorpej */
329 1.20 thorpej fromelem = sc->sc_firsts[cm->cm_fromtype] + cm->cm_fromunit;
330 1.20 thorpej toelem = sc->sc_firsts[cm->cm_totype] + cm->cm_tounit;
331 1.20 thorpej
332 1.20 thorpej /*
333 1.20 thorpej * Build the SCSI command.
334 1.20 thorpej */
335 1.20 thorpej bzero(&cmd, sizeof(cmd));
336 1.20 thorpej cmd.opcode = MOVE_MEDIUM;
337 1.20 thorpej _lto2b(sc->sc_picker, cmd.tea);
338 1.20 thorpej _lto2b(fromelem, cmd.src);
339 1.20 thorpej _lto2b(toelem, cmd.dst);
340 1.20 thorpej if (cm->cm_flags & CM_INVERT)
341 1.20 thorpej cmd.flags |= MOVE_MEDIUM_INVERT;
342 1.20 thorpej
343 1.20 thorpej /*
344 1.20 thorpej * Send command to changer.
345 1.20 thorpej */
346 1.20 thorpej return (scsi_scsi_cmd(sc->sc_link, (struct scsi_generic *)&cmd,
347 1.20 thorpej sizeof(cmd), NULL, 0, CHRETRIES, 100000, NULL, 0));
348 1.1 cgd }
349 1.1 cgd
350 1.20 thorpej int
351 1.20 thorpej ch_exchange(sc, ce)
352 1.20 thorpej struct ch_softc *sc;
353 1.20 thorpej struct changer_exchange *ce;
354 1.20 thorpej {
355 1.20 thorpej struct scsi_exchange_medium cmd;
356 1.20 thorpej u_int16_t src, dst1, dst2;
357 1.20 thorpej
358 1.20 thorpej /*
359 1.20 thorpej * Check arguments.
360 1.20 thorpej */
361 1.20 thorpej if ((ce->ce_srctype > CHET_DT) || (ce->ce_fdsttype > CHET_DT) ||
362 1.20 thorpej (ce->ce_sdsttype > CHET_DT))
363 1.20 thorpej return (EINVAL);
364 1.20 thorpej if ((ce->ce_srcunit > (sc->sc_counts[ce->ce_srctype] - 1)) ||
365 1.20 thorpej (ce->ce_fdstunit > (sc->sc_counts[ce->ce_fdsttype] - 1)) ||
366 1.20 thorpej (ce->ce_sdstunit > (sc->sc_counts[ce->ce_sdsttype] - 1)))
367 1.20 thorpej return (ENODEV);
368 1.20 thorpej
369 1.20 thorpej /*
370 1.20 thorpej * Check the request against the changer's capabilities.
371 1.20 thorpej */
372 1.20 thorpej if (((sc->sc_exchangemask[ce->ce_srctype] &
373 1.20 thorpej (1 << ce->ce_fdsttype)) == 0) ||
374 1.20 thorpej ((sc->sc_exchangemask[ce->ce_fdsttype] &
375 1.20 thorpej (1 << ce->ce_sdsttype)) == 0))
376 1.20 thorpej return (EINVAL);
377 1.20 thorpej
378 1.20 thorpej /*
379 1.20 thorpej * Calculate the source and destination elements.
380 1.20 thorpej */
381 1.20 thorpej src = sc->sc_firsts[ce->ce_srctype] + ce->ce_srcunit;
382 1.20 thorpej dst1 = sc->sc_firsts[ce->ce_fdsttype] + ce->ce_fdstunit;
383 1.20 thorpej dst2 = sc->sc_firsts[ce->ce_sdsttype] + ce->ce_sdstunit;
384 1.20 thorpej
385 1.20 thorpej /*
386 1.20 thorpej * Build the SCSI command.
387 1.20 thorpej */
388 1.20 thorpej bzero(&cmd, sizeof(cmd));
389 1.20 thorpej cmd.opcode = EXCHANGE_MEDIUM;
390 1.20 thorpej _lto2b(sc->sc_picker, cmd.tea);
391 1.20 thorpej _lto2b(src, cmd.src);
392 1.20 thorpej _lto2b(dst1, cmd.fdst);
393 1.20 thorpej _lto2b(dst2, cmd.sdst);
394 1.20 thorpej if (ce->ce_flags & CE_INVERT1)
395 1.20 thorpej cmd.flags |= EXCHANGE_MEDIUM_INV1;
396 1.20 thorpej if (ce->ce_flags & CE_INVERT2)
397 1.20 thorpej cmd.flags |= EXCHANGE_MEDIUM_INV2;
398 1.20 thorpej
399 1.20 thorpej /*
400 1.20 thorpej * Send command to changer.
401 1.20 thorpej */
402 1.20 thorpej return (scsi_scsi_cmd(sc->sc_link, (struct scsi_generic *)&cmd,
403 1.20 thorpej sizeof(cmd), NULL, 0, CHRETRIES, 100000, NULL, 0));
404 1.1 cgd }
405 1.1 cgd
406 1.20 thorpej int
407 1.20 thorpej ch_position(sc, cp)
408 1.20 thorpej struct ch_softc *sc;
409 1.20 thorpej struct changer_position *cp;
410 1.20 thorpej {
411 1.20 thorpej struct scsi_position_to_element cmd;
412 1.20 thorpej u_int16_t dst;
413 1.20 thorpej
414 1.20 thorpej /*
415 1.20 thorpej * Check arguments.
416 1.20 thorpej */
417 1.20 thorpej if (cp->cp_type > CHET_DT)
418 1.20 thorpej return (EINVAL);
419 1.20 thorpej if (cp->cp_unit > (sc->sc_counts[cp->cp_type] - 1))
420 1.20 thorpej return (ENODEV);
421 1.20 thorpej
422 1.20 thorpej /*
423 1.20 thorpej * Calculate the destination element.
424 1.20 thorpej */
425 1.20 thorpej dst = sc->sc_firsts[cp->cp_type] + cp->cp_unit;
426 1.20 thorpej
427 1.20 thorpej /*
428 1.20 thorpej * Build the SCSI command.
429 1.20 thorpej */
430 1.20 thorpej bzero(&cmd, sizeof(cmd));
431 1.20 thorpej cmd.opcode = POSITION_TO_ELEMENT;
432 1.20 thorpej _lto2b(sc->sc_picker, cmd.tea);
433 1.20 thorpej _lto2b(dst, cmd.dst);
434 1.20 thorpej if (cp->cp_flags & CP_INVERT)
435 1.20 thorpej cmd.flags |= POSITION_TO_ELEMENT_INVERT;
436 1.20 thorpej
437 1.20 thorpej /*
438 1.20 thorpej * Send command to changer.
439 1.20 thorpej */
440 1.20 thorpej return (scsi_scsi_cmd(sc->sc_link, (struct scsi_generic *)&cmd,
441 1.20 thorpej sizeof(cmd), NULL, 0, CHRETRIES, 100000, NULL, 0));
442 1.1 cgd }
443 1.1 cgd
444 1.6 mycroft /*
445 1.20 thorpej * Perform a READ ELEMENT STATUS on behalf of the user, and return to
446 1.20 thorpej * the user only the data the user is interested in (i.e. an array of
447 1.20 thorpej * flags bytes).
448 1.6 mycroft */
449 1.20 thorpej int
450 1.20 thorpej ch_usergetelemstatus(sc, chet, uptr)
451 1.20 thorpej struct ch_softc *sc;
452 1.20 thorpej int chet;
453 1.20 thorpej u_int8_t *uptr;
454 1.20 thorpej {
455 1.20 thorpej struct read_element_status_header *st_hdr;
456 1.20 thorpej struct read_element_status_page_header *pg_hdr;
457 1.20 thorpej struct read_element_status_descriptor *desc;
458 1.20 thorpej caddr_t data = NULL;
459 1.20 thorpej size_t size, desclen;
460 1.20 thorpej int avail, i, error = 0;
461 1.20 thorpej u_int8_t *user_data = NULL;
462 1.20 thorpej
463 1.20 thorpej /*
464 1.20 thorpej * If there are no elements of the requested type in the changer,
465 1.20 thorpej * the request is invalid.
466 1.20 thorpej */
467 1.20 thorpej if (sc->sc_counts[chet] == 0)
468 1.20 thorpej return (EINVAL);
469 1.20 thorpej
470 1.20 thorpej /*
471 1.20 thorpej * Request one descriptor for the given element type. This
472 1.20 thorpej * is used to determine the size of the descriptor so that
473 1.20 thorpej * we can allocate enough storage for all of them. We assume
474 1.20 thorpej * that the first one can fit into 1k.
475 1.20 thorpej */
476 1.20 thorpej data = (caddr_t)malloc(1024, M_DEVBUF, M_WAITOK);
477 1.20 thorpej if (error = ch_getelemstatus(sc, sc->sc_firsts[chet], 1, data, 1024))
478 1.20 thorpej goto done;
479 1.20 thorpej
480 1.20 thorpej st_hdr = (struct read_element_status_header *)data;
481 1.20 thorpej pg_hdr = (struct read_element_status_page_header *)((u_long)st_hdr +
482 1.20 thorpej sizeof(struct read_element_status_header));
483 1.20 thorpej desclen = _2btol(pg_hdr->edl);
484 1.20 thorpej
485 1.20 thorpej size = sizeof(struct read_element_status_header) +
486 1.20 thorpej sizeof(struct read_element_status_page_header) +
487 1.20 thorpej (desclen * sc->sc_counts[chet]);
488 1.20 thorpej
489 1.20 thorpej /*
490 1.20 thorpej * Reallocate storage for descriptors and get them from the
491 1.20 thorpej * device.
492 1.20 thorpej */
493 1.20 thorpej free(data, M_DEVBUF);
494 1.20 thorpej data = (caddr_t)malloc(size, M_DEVBUF, M_WAITOK);
495 1.20 thorpej if (error = ch_getelemstatus(sc, sc->sc_firsts[chet],
496 1.20 thorpej sc->sc_counts[chet], data, size))
497 1.20 thorpej goto done;
498 1.20 thorpej
499 1.20 thorpej /*
500 1.20 thorpej * Fill in the user status array.
501 1.20 thorpej */
502 1.20 thorpej st_hdr = (struct read_element_status_header *)data;
503 1.20 thorpej avail = _2btol(st_hdr->count);
504 1.20 thorpej if (avail != sc->sc_counts[chet])
505 1.20 thorpej printf("%s: warning, READ ELEMENT STATUS avail != count\n",
506 1.20 thorpej sc->sc_dev.dv_xname);
507 1.20 thorpej
508 1.20 thorpej user_data = (u_int8_t *)malloc(avail, M_DEVBUF, M_WAITOK);
509 1.20 thorpej
510 1.20 thorpej desc = (struct read_element_status_descriptor *)((u_long)data +
511 1.20 thorpej sizeof(struct read_element_status_header) +
512 1.20 thorpej sizeof(struct read_element_status_page_header));
513 1.20 thorpej for (i = 0; i < avail; ++i) {
514 1.20 thorpej user_data[i] = desc->flags1;
515 1.20 thorpej (u_long)desc += desclen;
516 1.20 thorpej }
517 1.20 thorpej
518 1.20 thorpej /* Copy flags array out to userspace. */
519 1.20 thorpej error = copyout(user_data, uptr, avail);
520 1.20 thorpej
521 1.20 thorpej done:
522 1.20 thorpej if (data != NULL)
523 1.20 thorpej free(data, M_DEVBUF);
524 1.20 thorpej if (user_data != NULL)
525 1.20 thorpej free(user_data, M_DEVBUF);
526 1.20 thorpej return (error);
527 1.20 thorpej }
528 1.20 thorpej
529 1.20 thorpej int
530 1.20 thorpej ch_getelemstatus(sc, first, count, data, datalen)
531 1.20 thorpej struct ch_softc *sc;
532 1.20 thorpej int first, count;
533 1.20 thorpej caddr_t data;
534 1.20 thorpej size_t datalen;
535 1.1 cgd {
536 1.20 thorpej struct scsi_read_element_status cmd;
537 1.6 mycroft
538 1.6 mycroft /*
539 1.20 thorpej * Build SCSI command.
540 1.6 mycroft */
541 1.20 thorpej bzero(&cmd, sizeof(cmd));
542 1.20 thorpej cmd.opcode = READ_ELEMENT_STATUS;
543 1.20 thorpej _lto2b(first, cmd.sea);
544 1.20 thorpej _lto2b(count, cmd.count);
545 1.20 thorpej _lto3b(datalen, cmd.len);
546 1.6 mycroft
547 1.6 mycroft /*
548 1.20 thorpej * Send command to changer.
549 1.6 mycroft */
550 1.20 thorpej return (scsi_scsi_cmd(sc->sc_link, (struct scsi_generic *)&cmd,
551 1.20 thorpej sizeof(cmd), (u_char *)data, datalen, CHRETRIES, 100000, NULL, 0));
552 1.20 thorpej }
553 1.20 thorpej
554 1.20 thorpej
555 1.20 thorpej /*
556 1.20 thorpej * Ask the device about itself and fill in the parameters in our
557 1.20 thorpej * softc.
558 1.20 thorpej */
559 1.20 thorpej int
560 1.20 thorpej ch_get_params(sc, scsiflags)
561 1.20 thorpej struct ch_softc *sc;
562 1.20 thorpej int scsiflags;
563 1.20 thorpej {
564 1.20 thorpej struct scsi_mode_sense cmd;
565 1.20 thorpej struct scsi_mode_sense_data {
566 1.20 thorpej struct scsi_mode_header header;
567 1.20 thorpej union {
568 1.20 thorpej struct page_element_address_assignment ea;
569 1.20 thorpej struct page_transport_geometry_parameters tg;
570 1.20 thorpej struct page_device_capabilities cap;
571 1.20 thorpej } pages;
572 1.20 thorpej } sense_data;
573 1.20 thorpej int error, from;
574 1.20 thorpej u_int8_t *moves, *exchanges;
575 1.6 mycroft
576 1.6 mycroft /*
577 1.20 thorpej * Grab info from the element address assignment page.
578 1.6 mycroft */
579 1.20 thorpej bzero(&cmd, sizeof(cmd));
580 1.20 thorpej bzero(&sense_data, sizeof(sense_data));
581 1.20 thorpej cmd.opcode = MODE_SENSE;
582 1.20 thorpej cmd.byte2 |= 0x08; /* disable block descriptors */
583 1.20 thorpej cmd.page = 0x1d;
584 1.20 thorpej cmd.length = (sizeof(sense_data) & 0xff);
585 1.20 thorpej error = scsi_scsi_cmd(sc->sc_link, (struct scsi_generic *)&cmd,
586 1.20 thorpej sizeof(cmd), (u_char *)&sense_data, sizeof(sense_data), CHRETRIES,
587 1.20 thorpej 6000, NULL, scsiflags | SCSI_DATA_IN);
588 1.15 christos if (error) {
589 1.20 thorpej printf("%s: could not sense element address page\n");
590 1.20 thorpej return (error);
591 1.6 mycroft }
592 1.6 mycroft
593 1.20 thorpej sc->sc_firsts[CHET_MT] = _2btol(sense_data.pages.ea.mtea);
594 1.20 thorpej sc->sc_counts[CHET_MT] = _2btol(sense_data.pages.ea.nmte);
595 1.20 thorpej sc->sc_firsts[CHET_ST] = _2btol(sense_data.pages.ea.fsea);
596 1.20 thorpej sc->sc_counts[CHET_ST] = _2btol(sense_data.pages.ea.nse);
597 1.20 thorpej sc->sc_firsts[CHET_IE] = _2btol(sense_data.pages.ea.fieea);
598 1.20 thorpej sc->sc_counts[CHET_IE] = _2btol(sense_data.pages.ea.niee);
599 1.20 thorpej sc->sc_firsts[CHET_DT] = _2btol(sense_data.pages.ea.fdtea);
600 1.20 thorpej sc->sc_counts[CHET_DT] = _2btol(sense_data.pages.ea.ndte);
601 1.20 thorpej
602 1.20 thorpej /* XXX ask for page trasport geom */
603 1.6 mycroft
604 1.6 mycroft /*
605 1.20 thorpej * Grab info from the capabilities page.
606 1.20 thorpej */
607 1.20 thorpej bzero(&cmd, sizeof(cmd));
608 1.20 thorpej bzero(&sense_data, sizeof(sense_data));
609 1.20 thorpej cmd.opcode = MODE_SENSE;
610 1.20 thorpej cmd.byte2 |= 0x08; /* disable block descriptors */
611 1.20 thorpej cmd.page = 0x1f;
612 1.20 thorpej cmd.length = (sizeof(sense_data) & 0xff);
613 1.20 thorpej error = scsi_scsi_cmd(sc->sc_link, (struct scsi_generic *)&cmd,
614 1.20 thorpej sizeof(cmd), (u_char *)&sense_data, sizeof(sense_data), CHRETRIES,
615 1.20 thorpej 6000, NULL, scsiflags | SCSI_DATA_IN);
616 1.20 thorpej if (error) {
617 1.20 thorpej printf("%s: could not sense capabilities page\n");
618 1.20 thorpej return (error);
619 1.20 thorpej }
620 1.20 thorpej
621 1.20 thorpej bzero(sc->sc_movemask, sizeof(sc->sc_movemask));
622 1.20 thorpej bzero(sc->sc_exchangemask, sizeof(sc->sc_exchangemask));
623 1.20 thorpej moves = &sense_data.pages.cap.move_from_mt;
624 1.20 thorpej exchanges = &sense_data.pages.cap.exchange_with_mt;
625 1.20 thorpej for (from = CHET_MT; from <= CHET_DT; ++from) {
626 1.20 thorpej sc->sc_movemask[from] = moves[from];
627 1.20 thorpej sc->sc_exchangemask[from] = exchanges[from];
628 1.1 cgd }
629 1.20 thorpej
630 1.20 thorpej sc->sc_link->flags |= SDEV_MEDIA_LOADED;
631 1.20 thorpej return (0);
632 1.1 cgd }
633