ch.c revision 1.15 1 /* $NetBSD: ch.c,v 1.15 1996/02/14 21:47:07 christos Exp $ */
2
3 /*
4 * Copyright (c) 1994 Charles Hannum. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Charles Hannum.
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Originally written by grefen@?????
34 * Based on scsi drivers by Julian Elischer (julian (at) tfs.com)
35 */
36
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/errno.h>
41 #include <sys/ioctl.h>
42 #include <sys/buf.h>
43 #include <sys/proc.h>
44 #include <sys/user.h>
45 #include <sys/chio.h>
46 #include <sys/device.h>
47
48 #include <scsi/scsi_all.h>
49 #include <scsi/scsi_changer.h>
50 #include <scsi/scsiconf.h>
51 #include <scsi/scsi_conf.h>
52
53 #define CHRETRIES 2
54
55 #define CHMODE(z) (minor(z) & 0x0f)
56 #define CHUNIT(z) (minor(z) >> 4)
57
58 struct ch_softc {
59 struct device sc_dev;
60
61 struct scsi_link *sc_link; /* all the inter level info */
62 u_int16_t chmo; /* Offset of first CHM */
63 u_int16_t chms; /* No. of CHM */
64 u_int16_t slots; /* No. of Storage Elements */
65 u_int16_t sloto; /* Offset of first SE */
66 u_int16_t imexs; /* No. of Import/Export Slots */
67 u_int16_t imexo; /* Offset of first IM/EX */
68 u_int16_t drives; /* No. of CTS */
69 u_int16_t driveo; /* Offset of first CTS */
70 u_int16_t rot; /* CHM can rotate */
71 u_long op_matrix; /* possible operations */
72 u_int16_t lsterr; /* details of lasterror */
73 u_char stor; /* posible Storage locations */
74 };
75
76 int chmatch __P((struct device *, void *, void *));
77 void chattach __P((struct device *, struct device *, void *));
78 int ch_getelem __P((struct ch_softc *, short *, int, int , char *, int));
79 int ch_move __P((struct ch_softc *, short *, int, int , int , int ));
80 int ch_position __P((struct ch_softc *, short *, int, int , int ));
81 int ch_mode_sense __P((struct ch_softc *, int));
82
83 struct cfdriver chcd = {
84 NULL, "ch", chmatch, chattach, DV_DULL, sizeof(struct ch_softc)
85 };
86
87 /*
88 * This driver is so simple it uses all the default services
89 */
90 struct scsi_device ch_switch = {
91 NULL,
92 NULL,
93 NULL,
94 NULL,
95 };
96
97 struct scsi_inquiry_pattern ch_patterns[] = {
98 {T_CHANGER, T_REMOV,
99 "", "", ""},
100 };
101
102 int
103 chmatch(parent, match, aux)
104 struct device *parent;
105 void *match, *aux;
106 {
107 struct scsibus_attach_args *sa = aux;
108 int priority;
109
110 (void)scsi_inqmatch(sa->sa_inqbuf,
111 (caddr_t)ch_patterns, sizeof(ch_patterns)/sizeof(ch_patterns[0]),
112 sizeof(ch_patterns[0]), &priority);
113 return (priority);
114 }
115
116 /*
117 * The routine called by the low level scsi routine when it discovers
118 * a device suitable for this driver.
119 */
120 void
121 chattach(parent, self, aux)
122 struct device *parent, *self;
123 void *aux;
124 {
125 struct ch_softc *ch = (void *)self;
126 struct scsibus_attach_args *sa = aux;
127 struct scsi_link *sc_link = sa->sa_sc_link;
128
129 SC_DEBUG(sc_link, SDEV_DB2, ("chattach: "));
130
131 /*
132 * Store information needed to contact our base driver
133 */
134 ch->sc_link = sc_link;
135 sc_link->device = &ch_switch;
136 sc_link->device_softc = ch;
137 sc_link->openings = 1;
138
139 /*
140 * Use the subdriver to request information regarding
141 * the drive. We cannot use interrupts yet, so the
142 * request must specify this.
143 */
144 if (ch_mode_sense(ch, SCSI_AUTOCONF) != 0)
145 printf(": offline\n");
146 else
147 printf(": %d slot(s), %d drive(s), %d arm(s), %d i/e-slot(s)\n",
148 ch->slots, ch->drives, ch->chms, ch->imexs);
149 }
150
151 /*
152 * open the device.
153 */
154 int
155 chopen(dev, flags, mode, p)
156 dev_t dev;
157 int flags;
158 int mode;
159 struct proc *p;
160 {
161 int error = 0;
162 int unit;
163 struct ch_softc *ch;
164 struct scsi_link *sc_link;
165
166 unit = CHUNIT(dev);
167 if (unit >= chcd.cd_ndevs)
168 return ENXIO;
169 ch = chcd.cd_devs[unit];
170 if (!ch)
171 return ENXIO;
172
173 sc_link = ch->sc_link;
174
175 SC_DEBUG(sc_link, SDEV_DB1,
176 ("chopen: dev=0x%x (unit %d (of %d))\n", dev, unit, chcd.cd_ndevs));
177
178 /*
179 * Only allow one at a time
180 */
181 if (sc_link->flags & SDEV_OPEN) {
182 printf("%s: already open\n", ch->sc_dev.dv_xname);
183 return EBUSY;
184 }
185
186 /*
187 * Catch any unit attention errors.
188 */
189 error = scsi_test_unit_ready(sc_link, SCSI_IGNORE_MEDIA_CHANGE);
190 if (error)
191 goto bad;
192
193 sc_link->flags |= SDEV_OPEN; /* unit attn are now errors */
194
195 /*
196 * Make sure data is loaded
197 */
198 if ((error = ch_mode_sense(ch, 0)) != 0) {
199 printf("%s: offline\n", ch->sc_dev.dv_xname);
200 goto bad;
201 }
202
203 SC_DEBUG(sc_link, SDEV_DB3, ("open complete\n"));
204 return 0;
205
206 bad:
207 sc_link->flags &= ~SDEV_OPEN;
208 return error;
209 }
210
211 /*
212 * close the device.. only called if we are the LAST
213 * occurence of an open device
214 */
215 int
216 chclose(dev, flags, mode, p)
217 dev_t dev;
218 int flags;
219 int mode;
220 struct proc *p;
221 {
222 struct ch_softc *ch = chcd.cd_devs[CHUNIT(dev)];
223
224 SC_DEBUG(ch->sc_link, SDEV_DB1, ("closing\n"));
225 ch->sc_link->flags &= ~SDEV_OPEN;
226
227 return 0;
228 }
229
230 /*
231 * Perform special action on behalf of the user
232 * Knows about the internals of this device
233 */
234 int
235 chioctl(dev, cmd, arg, mode, p)
236 dev_t dev;
237 u_long cmd;
238 caddr_t arg;
239 int mode;
240 struct proc *p;
241 {
242 struct ch_softc *ch = chcd.cd_devs[CHUNIT(dev)];
243 struct scsi_link *sc_link = ch->sc_link;
244 int flags;
245
246 /*
247 * Find the device that the user is talking about
248 */
249 flags = 0; /* give error messages, act on errors etc. */
250
251 switch (cmd) {
252 case CHIOOP: {
253 struct chop *chop = (struct chop *) arg;
254 SC_DEBUG(sc_link, SDEV_DB2, ("[chtape_chop: %x]\n",
255 chop->ch_op));
256
257 switch (chop->ch_op) {
258 case CHGETPARAM:
259 chop->u.getparam.chmo = ch->chmo;
260 chop->u.getparam.chms = ch->chms;
261 chop->u.getparam.sloto = ch->sloto;
262 chop->u.getparam.slots = ch->slots;
263 chop->u.getparam.imexo = ch->imexo;
264 chop->u.getparam.imexs = ch->imexs;
265 chop->u.getparam.driveo = ch->driveo;
266 chop->u.getparam.drives = ch->drives;
267 chop->u.getparam.rot = ch->rot;
268 chop->result = 0;
269 return 0;
270 break;
271 case CHPOSITION:
272 return ch_position(ch, &chop->result,
273 chop->u.position.chm, chop->u.position.to, flags);
274 case CHMOVE:
275 return ch_move(ch, &chop->result, chop->u.position.chm,
276 chop->u.move.from, chop->u.move.to, flags);
277 case CHGETELEM:
278 return ch_getelem(ch, &chop->result,
279 chop->u.get_elem_stat.type,
280 chop->u.get_elem_stat.from,
281 (char *) &chop->u.get_elem_stat.elem_data, flags);
282 default:
283 return EINVAL;
284 }
285 }
286 default:
287 return scsi_do_ioctl(sc_link, dev, cmd, arg, mode, p);
288 }
289 #ifdef DIAGNOSTIC
290 panic("chioctl: impossible");
291 #endif
292 }
293
294 int
295 ch_getelem(ch, stat, type, from, data, flags)
296 struct ch_softc *ch;
297 short *stat;
298 int type, from;
299 char *data;
300 int flags;
301 {
302 struct scsi_read_element_status scsi_cmd;
303 char elbuf[32];
304 int error;
305
306 bzero(&scsi_cmd, sizeof(scsi_cmd));
307 scsi_cmd.opcode = READ_ELEMENT_STATUS;
308 scsi_cmd.byte2 = type;
309 scsi_cmd.starting_element_addr[0] = (from >> 8) & 0xff;
310 scsi_cmd.starting_element_addr[1] = from & 0xff;
311 scsi_cmd.number_of_elements[1] = 1;
312 scsi_cmd.allocation_length[2] = 32;
313
314 error = scsi_scsi_cmd(ch->sc_link, (struct scsi_generic *) &scsi_cmd,
315 sizeof(scsi_cmd), (u_char *) elbuf, 32, CHRETRIES, 100000, NULL,
316 SCSI_DATA_IN | flags);
317 if (error)
318 *stat = ch->lsterr;
319 else
320 *stat = 0;
321 bcopy(elbuf + 16, data, 16);
322 return error;
323 }
324
325 int
326 ch_move(ch, stat, chm, from, to, flags)
327 struct ch_softc *ch;
328 short *stat;
329 int chm, from, to, flags;
330 {
331 struct scsi_move_medium scsi_cmd;
332 int error;
333
334 bzero(&scsi_cmd, sizeof(scsi_cmd));
335 scsi_cmd.opcode = MOVE_MEDIUM;
336 scsi_cmd.transport_element_address[0] = (chm >> 8) & 0xff;
337 scsi_cmd.transport_element_address[1] = chm & 0xff;
338 scsi_cmd.source_address[0] = (from >> 8) & 0xff;
339 scsi_cmd.source_address[1] = from & 0xff;
340 scsi_cmd.destination_address[0] = (to >> 8) & 0xff;
341 scsi_cmd.destination_address[1] = to & 0xff;
342 scsi_cmd.invert = (chm & CH_INVERT) ? 1 : 0;
343 error = scsi_scsi_cmd(ch->sc_link, (struct scsi_generic *) &scsi_cmd,
344 sizeof(scsi_cmd), NULL, 0, CHRETRIES, 100000, NULL, flags);
345 if (error)
346 *stat = ch->lsterr;
347 else
348 *stat = 0;
349 return error;
350 }
351
352 int
353 ch_position(ch, stat, chm, to, flags)
354 struct ch_softc *ch;
355 short *stat;
356 int chm, to, flags;
357 {
358 struct scsi_position_to_element scsi_cmd;
359 int error;
360
361 bzero(&scsi_cmd, sizeof(scsi_cmd));
362 scsi_cmd.opcode = POSITION_TO_ELEMENT;
363 scsi_cmd.transport_element_address[0] = (chm >> 8) & 0xff;
364 scsi_cmd.transport_element_address[1] = chm & 0xff;
365 scsi_cmd.source_address[0] = (to >> 8) & 0xff;
366 scsi_cmd.source_address[1] = to & 0xff;
367 scsi_cmd.invert = (chm & CH_INVERT) ? 1 : 0;
368 error = scsi_scsi_cmd(ch->sc_link, (struct scsi_generic *) &scsi_cmd,
369 sizeof(scsi_cmd), NULL, 0, CHRETRIES, 100000, NULL, flags);
370 if (error)
371 *stat = ch->lsterr;
372 else
373 *stat = 0;
374 return error;
375 }
376
377 /*
378 * Get the scsi driver to send a full inquiry to the
379 * device and use the results to fill out the global
380 * parameter structure.
381 */
382 int
383 ch_mode_sense(ch, flags)
384 struct ch_softc *ch;
385 int flags;
386 {
387 struct scsi_mode_sense scsi_cmd;
388 u_char scsi_sense[128]; /* Can't use scsi_mode_sense_data because of
389 * missing block descriptor.
390 */
391 u_char *b;
392 int i, l;
393 int error;
394 struct scsi_link *sc_link = ch->sc_link;
395
396 /*
397 * First check if we have it all loaded
398 */
399 if (sc_link->flags & SDEV_MEDIA_LOADED)
400 return 0;
401
402 /*
403 * First do a mode sense
404 */
405 bzero(&scsi_cmd, sizeof(scsi_cmd));
406 scsi_cmd.opcode = MODE_SENSE;
407 scsi_cmd.byte2 = SMS_DBD;
408 scsi_cmd.page = 0x3f; /* All Pages */
409 scsi_cmd.length = sizeof(scsi_sense);
410
411 /*
412 * Read in the pages
413 */
414 error = scsi_scsi_cmd(sc_link, (struct scsi_generic *) &scsi_cmd,
415 sizeof(scsi_cmd), (u_char *) &scsi_sense,
416 sizeof(scsi_sense), CHRETRIES, 5000, NULL,
417 flags | SCSI_DATA_IN);
418 if (error) {
419 printf("%s: could not mode sense\n", ch->sc_dev.dv_xname);
420 return error;
421 }
422
423 sc_link->flags |= SDEV_MEDIA_LOADED;
424 l = scsi_sense[0] - 3;
425 b = &scsi_sense[4];
426
427 /*
428 * To avoid alignment problems
429 */
430 /* XXXX - FIX THIS FOR MSB */
431 #define p2copy(valp) (valp[1] | (valp[0]<<8)); valp+=2
432 #define p4copy(valp) (valp[3] | (valp[2]<<8) | (valp[1]<<16) | (valp[0]<<24)); valp+=4
433 #if 0
434 printf("\nmode_sense %d\n", l);
435 for (i = 0; i < l + 4; i++)
436 printf("%x%c", scsi_sense[i], i % 8 == 7 ? '\n' : ':');
437 printf("\n");
438 #endif
439 for (i = 0; i < l;) {
440 u_char pc = (*b++) & 0x3f;
441 u_char pl = *b++;
442 u_char *bb = b;
443 switch (pc) {
444 case 0x1d:
445 ch->chmo = p2copy(bb);
446 ch->chms = p2copy(bb);
447 ch->sloto = p2copy(bb);
448 ch->slots = p2copy(bb);
449 ch->imexo = p2copy(bb);
450 ch->imexs = p2copy(bb);
451 ch->driveo = p2copy(bb);
452 ch->drives = p2copy(bb);
453 break;
454 case 0x1e:
455 ch->rot = *b & 0x1;
456 break;
457 case 0x1f:
458 ch->stor = *b & 0xf;
459 bb += 2;
460 ch->stor = p4copy(bb);
461 break;
462 default:
463 break;
464 }
465 b += pl;
466 i += pl + 2;
467 }
468 SC_DEBUG(sc_link, SDEV_DB2,
469 (" cht(%d-%d)slot(%d-%d)imex(%d-%d)cts(%d-%d) %s rotate\n",
470 ch->chmo, ch->chms, ch->sloto, ch->slots, ch->imexo, ch->imexs,
471 ch->driveo, ch->drives, ch->rot ? "can" : "can't"));
472 return 0;
473 }
474