ch.c revision 1.16 1 /* $NetBSD: ch.c,v 1.16 1996/03/05 00:15:09 thorpej 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 printf("\n");
145 printf("%s: ", ch->sc_dev.dv_xname);
146 if (ch_mode_sense(ch, SCSI_AUTOCONF) != 0)
147 printf("offline\n");
148 else
149 printf("%d slot(s), %d drive(s), %d arm(s), %d i/e-slot(s)\n",
150 ch->slots, ch->drives, ch->chms, ch->imexs);
151 }
152
153 /*
154 * open the device.
155 */
156 int
157 chopen(dev, flags, mode, p)
158 dev_t dev;
159 int flags;
160 int mode;
161 struct proc *p;
162 {
163 int error = 0;
164 int unit;
165 struct ch_softc *ch;
166 struct scsi_link *sc_link;
167
168 unit = CHUNIT(dev);
169 if (unit >= chcd.cd_ndevs)
170 return ENXIO;
171 ch = chcd.cd_devs[unit];
172 if (!ch)
173 return ENXIO;
174
175 sc_link = ch->sc_link;
176
177 SC_DEBUG(sc_link, SDEV_DB1,
178 ("chopen: dev=0x%x (unit %d (of %d))\n", dev, unit, chcd.cd_ndevs));
179
180 /*
181 * Only allow one at a time
182 */
183 if (sc_link->flags & SDEV_OPEN) {
184 printf("%s: already open\n", ch->sc_dev.dv_xname);
185 return EBUSY;
186 }
187
188 /*
189 * Catch any unit attention errors.
190 */
191 error = scsi_test_unit_ready(sc_link, SCSI_IGNORE_MEDIA_CHANGE);
192 if (error)
193 goto bad;
194
195 sc_link->flags |= SDEV_OPEN; /* unit attn are now errors */
196
197 /*
198 * Make sure data is loaded
199 */
200 if ((error = ch_mode_sense(ch, 0)) != 0) {
201 printf("%s: offline\n", ch->sc_dev.dv_xname);
202 goto bad;
203 }
204
205 SC_DEBUG(sc_link, SDEV_DB3, ("open complete\n"));
206 return 0;
207
208 bad:
209 sc_link->flags &= ~SDEV_OPEN;
210 return error;
211 }
212
213 /*
214 * close the device.. only called if we are the LAST
215 * occurence of an open device
216 */
217 int
218 chclose(dev, flags, mode, p)
219 dev_t dev;
220 int flags;
221 int mode;
222 struct proc *p;
223 {
224 struct ch_softc *ch = chcd.cd_devs[CHUNIT(dev)];
225
226 SC_DEBUG(ch->sc_link, SDEV_DB1, ("closing\n"));
227 ch->sc_link->flags &= ~SDEV_OPEN;
228
229 return 0;
230 }
231
232 /*
233 * Perform special action on behalf of the user
234 * Knows about the internals of this device
235 */
236 int
237 chioctl(dev, cmd, arg, mode, p)
238 dev_t dev;
239 u_long cmd;
240 caddr_t arg;
241 int mode;
242 struct proc *p;
243 {
244 struct ch_softc *ch = chcd.cd_devs[CHUNIT(dev)];
245 struct scsi_link *sc_link = ch->sc_link;
246 int flags;
247
248 /*
249 * Find the device that the user is talking about
250 */
251 flags = 0; /* give error messages, act on errors etc. */
252
253 switch (cmd) {
254 case CHIOOP: {
255 struct chop *chop = (struct chop *) arg;
256 SC_DEBUG(sc_link, SDEV_DB2, ("[chtape_chop: %x]\n",
257 chop->ch_op));
258
259 switch (chop->ch_op) {
260 case CHGETPARAM:
261 chop->u.getparam.chmo = ch->chmo;
262 chop->u.getparam.chms = ch->chms;
263 chop->u.getparam.sloto = ch->sloto;
264 chop->u.getparam.slots = ch->slots;
265 chop->u.getparam.imexo = ch->imexo;
266 chop->u.getparam.imexs = ch->imexs;
267 chop->u.getparam.driveo = ch->driveo;
268 chop->u.getparam.drives = ch->drives;
269 chop->u.getparam.rot = ch->rot;
270 chop->result = 0;
271 return 0;
272 break;
273 case CHPOSITION:
274 return ch_position(ch, &chop->result,
275 chop->u.position.chm, chop->u.position.to, flags);
276 case CHMOVE:
277 return ch_move(ch, &chop->result, chop->u.position.chm,
278 chop->u.move.from, chop->u.move.to, flags);
279 case CHGETELEM:
280 return ch_getelem(ch, &chop->result,
281 chop->u.get_elem_stat.type,
282 chop->u.get_elem_stat.from,
283 (char *) &chop->u.get_elem_stat.elem_data, flags);
284 default:
285 return EINVAL;
286 }
287 }
288 default:
289 return scsi_do_ioctl(sc_link, dev, cmd, arg, mode, p);
290 }
291 #ifdef DIAGNOSTIC
292 panic("chioctl: impossible");
293 #endif
294 }
295
296 int
297 ch_getelem(ch, stat, type, from, data, flags)
298 struct ch_softc *ch;
299 short *stat;
300 int type, from;
301 char *data;
302 int flags;
303 {
304 struct scsi_read_element_status scsi_cmd;
305 char elbuf[32];
306 int error;
307
308 bzero(&scsi_cmd, sizeof(scsi_cmd));
309 scsi_cmd.opcode = READ_ELEMENT_STATUS;
310 scsi_cmd.byte2 = type;
311 scsi_cmd.starting_element_addr[0] = (from >> 8) & 0xff;
312 scsi_cmd.starting_element_addr[1] = from & 0xff;
313 scsi_cmd.number_of_elements[1] = 1;
314 scsi_cmd.allocation_length[2] = 32;
315
316 error = scsi_scsi_cmd(ch->sc_link, (struct scsi_generic *) &scsi_cmd,
317 sizeof(scsi_cmd), (u_char *) elbuf, 32, CHRETRIES, 100000, NULL,
318 SCSI_DATA_IN | flags);
319 if (error)
320 *stat = ch->lsterr;
321 else
322 *stat = 0;
323 bcopy(elbuf + 16, data, 16);
324 return error;
325 }
326
327 int
328 ch_move(ch, stat, chm, from, to, flags)
329 struct ch_softc *ch;
330 short *stat;
331 int chm, from, to, flags;
332 {
333 struct scsi_move_medium scsi_cmd;
334 int error;
335
336 bzero(&scsi_cmd, sizeof(scsi_cmd));
337 scsi_cmd.opcode = MOVE_MEDIUM;
338 scsi_cmd.transport_element_address[0] = (chm >> 8) & 0xff;
339 scsi_cmd.transport_element_address[1] = chm & 0xff;
340 scsi_cmd.source_address[0] = (from >> 8) & 0xff;
341 scsi_cmd.source_address[1] = from & 0xff;
342 scsi_cmd.destination_address[0] = (to >> 8) & 0xff;
343 scsi_cmd.destination_address[1] = to & 0xff;
344 scsi_cmd.invert = (chm & CH_INVERT) ? 1 : 0;
345 error = scsi_scsi_cmd(ch->sc_link, (struct scsi_generic *) &scsi_cmd,
346 sizeof(scsi_cmd), NULL, 0, CHRETRIES, 100000, NULL, flags);
347 if (error)
348 *stat = ch->lsterr;
349 else
350 *stat = 0;
351 return error;
352 }
353
354 int
355 ch_position(ch, stat, chm, to, flags)
356 struct ch_softc *ch;
357 short *stat;
358 int chm, to, flags;
359 {
360 struct scsi_position_to_element scsi_cmd;
361 int error;
362
363 bzero(&scsi_cmd, sizeof(scsi_cmd));
364 scsi_cmd.opcode = POSITION_TO_ELEMENT;
365 scsi_cmd.transport_element_address[0] = (chm >> 8) & 0xff;
366 scsi_cmd.transport_element_address[1] = chm & 0xff;
367 scsi_cmd.source_address[0] = (to >> 8) & 0xff;
368 scsi_cmd.source_address[1] = to & 0xff;
369 scsi_cmd.invert = (chm & CH_INVERT) ? 1 : 0;
370 error = scsi_scsi_cmd(ch->sc_link, (struct scsi_generic *) &scsi_cmd,
371 sizeof(scsi_cmd), NULL, 0, CHRETRIES, 100000, NULL, flags);
372 if (error)
373 *stat = ch->lsterr;
374 else
375 *stat = 0;
376 return error;
377 }
378
379 /*
380 * Get the scsi driver to send a full inquiry to the
381 * device and use the results to fill out the global
382 * parameter structure.
383 */
384 int
385 ch_mode_sense(ch, flags)
386 struct ch_softc *ch;
387 int flags;
388 {
389 struct scsi_mode_sense scsi_cmd;
390 u_char scsi_sense[128]; /* Can't use scsi_mode_sense_data because of
391 * missing block descriptor.
392 */
393 u_char *b;
394 int i, l;
395 int error;
396 struct scsi_link *sc_link = ch->sc_link;
397
398 /*
399 * First check if we have it all loaded
400 */
401 if (sc_link->flags & SDEV_MEDIA_LOADED)
402 return 0;
403
404 /*
405 * First do a mode sense
406 */
407 bzero(&scsi_cmd, sizeof(scsi_cmd));
408 scsi_cmd.opcode = MODE_SENSE;
409 scsi_cmd.byte2 = SMS_DBD;
410 scsi_cmd.page = 0x3f; /* All Pages */
411 scsi_cmd.length = sizeof(scsi_sense);
412
413 /*
414 * Read in the pages
415 */
416 error = scsi_scsi_cmd(sc_link, (struct scsi_generic *) &scsi_cmd,
417 sizeof(scsi_cmd), (u_char *) &scsi_sense,
418 sizeof(scsi_sense), CHRETRIES, 5000, NULL,
419 flags | SCSI_DATA_IN);
420 if (error) {
421 printf("%s: could not mode sense\n", ch->sc_dev.dv_xname);
422 return error;
423 }
424
425 sc_link->flags |= SDEV_MEDIA_LOADED;
426 l = scsi_sense[0] - 3;
427 b = &scsi_sense[4];
428
429 /*
430 * To avoid alignment problems
431 */
432 /* XXXX - FIX THIS FOR MSB */
433 #define p2copy(valp) (valp[1] | (valp[0]<<8)); valp+=2
434 #define p4copy(valp) (valp[3] | (valp[2]<<8) | (valp[1]<<16) | (valp[0]<<24)); valp+=4
435 #if 0
436 printf("\nmode_sense %d\n", l);
437 for (i = 0; i < l + 4; i++)
438 printf("%x%c", scsi_sense[i], i % 8 == 7 ? '\n' : ':');
439 printf("\n");
440 #endif
441 for (i = 0; i < l;) {
442 u_char pc = (*b++) & 0x3f;
443 u_char pl = *b++;
444 u_char *bb = b;
445 switch (pc) {
446 case 0x1d:
447 ch->chmo = p2copy(bb);
448 ch->chms = p2copy(bb);
449 ch->sloto = p2copy(bb);
450 ch->slots = p2copy(bb);
451 ch->imexo = p2copy(bb);
452 ch->imexs = p2copy(bb);
453 ch->driveo = p2copy(bb);
454 ch->drives = p2copy(bb);
455 break;
456 case 0x1e:
457 ch->rot = *b & 0x1;
458 break;
459 case 0x1f:
460 ch->stor = *b & 0xf;
461 bb += 2;
462 ch->stor = p4copy(bb);
463 break;
464 default:
465 break;
466 }
467 b += pl;
468 i += pl + 2;
469 }
470 SC_DEBUG(sc_link, SDEV_DB2,
471 (" cht(%d-%d)slot(%d-%d)imex(%d-%d)cts(%d-%d) %s rotate\n",
472 ch->chmo, ch->chms, ch->sloto, ch->slots, ch->imexo, ch->imexs,
473 ch->driveo, ch->drives, ch->rot ? "can" : "can't"));
474 return 0;
475 }
476