xref: /netbsd-src/sys/dev/scsipi/ch.c (revision ae9172d6cd9432a6a1a56760d86b32c57a66c39c)
1 /*	$NetBSD: ch.c,v 1.12 1994/11/21 11:28:47 mycroft 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@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 
52 #define	CHRETRIES	2
53 
54 #define CHMODE(z)	(minor(z) & 0x0f)
55 #define CHUNIT(z)	(minor(z) >> 4)
56 
57 struct ch_data {
58 	struct device sc_dev;
59 
60 	struct scsi_link *sc_link;	/* all the inter level info */
61 	u_int16 chmo;			/* Offset of first CHM */
62 	u_int16 chms;			/* No. of CHM */
63 	u_int16 slots;			/* No. of Storage Elements */
64 	u_int16 sloto;			/* Offset of first SE */
65 	u_int16 imexs;			/* No. of Import/Export Slots */
66 	u_int16 imexo;			/* Offset of first IM/EX */
67 	u_int16 drives;			/* No. of CTS */
68 	u_int16 driveo;			/* Offset of first CTS */
69 	u_int16 rot;			/* CHM can rotate */
70 	u_long  op_matrix;		/* possible opertaions */
71 	u_int16 lsterr;			/* details of lasterror */
72 	u_char  stor;			/* posible Storage locations */
73 };
74 
75 void chattach __P((struct device *, struct device *, void *));
76 
77 struct cfdriver chcd = {
78 	NULL, "ch", scsi_targmatch, chattach, DV_DULL, sizeof(struct ch_data)
79 };
80 
81 /*
82  * This driver is so simple it uses all the default services
83  */
84 struct scsi_device ch_switch = {
85 	NULL,
86 	NULL,
87 	NULL,
88 	NULL,
89 	"ch",
90 	0
91 };
92 
93 /*
94  * The routine called by the low level scsi routine when it discovers
95  * a device suitable for this driver.
96  */
97 void
98 chattach(parent, self, aux)
99 	struct device *parent, *self;
100 	void *aux;
101 {
102 	struct ch_data *ch = (void *)self;
103 	struct scsi_link *sc_link = aux;
104 	unsigned char *tbl;
105 
106 	SC_DEBUG(sc_link, SDEV_DB2, ("chattach: "));
107 
108 	/*
109 	 * Store information needed to contact our base driver
110 	 */
111 	ch->sc_link = sc_link;
112 	sc_link->device = &ch_switch;
113 	sc_link->device_softc = ch;
114 
115 	/*
116 	 * Use the subdriver to request information regarding
117 	 * the drive. We cannot use interrupts yet, so the
118 	 * request must specify this.
119 	 */
120 	if ((ch_mode_sense(ch, SCSI_NOSLEEP | SCSI_NOMASK)))
121 		printf(": offline\n");
122 	else
123 		printf(": %d slot(s), %d drive(s), %d arm(s), %d i/e-slot(s)\n",
124 		    ch->slots, ch->drives, ch->chms, ch->imexs);
125 }
126 
127 /*
128  *    open the device.
129  */
130 int
131 chopen(dev)
132 	dev_t dev;
133 {
134 	int error = 0;
135 	int unit, mode;
136 	struct ch_data *ch;
137 	struct scsi_link *sc_link;
138 
139 	unit = CHUNIT(dev);
140 	if (unit >= chcd.cd_ndevs)
141 		return ENXIO;
142 	ch = chcd.cd_devs[unit];
143 	if (!ch)
144 		return ENXIO;
145 
146 	mode = CHMODE(dev);
147 	sc_link = ch->sc_link;
148 
149 	SC_DEBUG(sc_link, SDEV_DB1,
150 	    ("chopen: dev=0x%x (unit %d (of %d))\n", dev, unit, chcd.cd_ndevs));
151 
152 	/*
153 	 * Only allow one at a time
154 	 */
155 	if (sc_link->flags & SDEV_OPEN) {
156 		printf("%s: already open\n", ch->sc_dev.dv_xname);
157 		return EBUSY;
158 	}
159 
160 	/*
161 	 * Catch any unit attention errors.
162 	 */
163 	scsi_test_unit_ready(sc_link, SCSI_SILENT);
164 
165 	/*
166 	 * Check that it is still responding and ok.
167 	 */
168 	sc_link->flags |= SDEV_OPEN;
169 	if (scsi_test_unit_ready(sc_link, 0)) {
170 		SC_DEBUG(sc_link, SDEV_DB3, ("device not responding\n"));
171 		error = ENXIO;
172 		goto bad;
173 	}
174 	SC_DEBUG(sc_link, SDEV_DB3, ("device ok\n"));
175 
176 	/*
177 	 * Make sure data is loaded
178 	 */
179 	if (error = (ch_mode_sense(ch, SCSI_NOSLEEP | SCSI_NOMASK))) {
180 		printf("%s: offline\n", ch->sc_dev.dv_xname);
181 		goto bad;
182 	}
183 
184 	SC_DEBUG(sc_link, SDEV_DB3, ("open complete\n"));
185 	return 0;
186 
187 bad:
188 	sc_link->flags &= ~SDEV_OPEN;
189 	return error;
190 }
191 
192 /*
193  * close the device.. only called if we are the LAST
194  * occurence of an open device
195  */
196 int
197 chclose(dev)
198 	dev_t dev;
199 {
200 	struct ch_data *ch = chcd.cd_devs[CHUNIT(dev)];
201 
202 	SC_DEBUG(ch->sc_link, SDEV_DB1, ("closing\n"));
203 	ch->sc_link->flags &= ~SDEV_OPEN;
204 
205 	return 0;
206 }
207 
208 /*
209  * Perform special action on behalf of the user
210  * Knows about the internals of this device
211  */
212 int
213 chioctl(dev, cmd, arg, mode)
214 	dev_t dev;
215 	u_long cmd;
216 	caddr_t arg;
217 	int mode;
218 {
219 	struct ch_data *ch = chcd.cd_devs[CHUNIT(dev)];
220 	struct scsi_link *sc_link = ch->sc_link;
221 	int number;
222 	int flags;
223 
224 	/*
225 	 * Find the device that the user is talking about
226 	 */
227 	flags = 0;		/* give error messages, act on errors etc. */
228 
229 	switch (cmd) {
230 	case CHIOOP: {
231 		struct chop *chop = (struct chop *) arg;
232 		SC_DEBUG(sc_link, SDEV_DB2, ("[chtape_chop: %x]\n",
233 		    chop->ch_op));
234 
235 		switch (chop->ch_op) {
236 		case CHGETPARAM:
237 			chop->u.getparam.chmo = ch->chmo;
238 			chop->u.getparam.chms = ch->chms;
239 			chop->u.getparam.sloto = ch->sloto;
240 			chop->u.getparam.slots = ch->slots;
241 			chop->u.getparam.imexo = ch->imexo;
242 			chop->u.getparam.imexs = ch->imexs;
243 			chop->u.getparam.driveo = ch->driveo;
244 			chop->u.getparam.drives = ch->drives;
245 			chop->u.getparam.rot = ch->rot;
246 			chop->result = 0;
247 			return 0;
248 			break;
249 		case CHPOSITION:
250 			return ch_position(ch, &chop->result,
251 			    chop->u.position.chm, chop->u.position.to, flags);
252 		case CHMOVE:
253 			return ch_move(ch, &chop->result, chop->u.position.chm,
254 			    chop->u.move.from, chop->u.move.to, flags);
255 		case CHGETELEM:
256 			return ch_getelem(ch, &chop->result,
257 			    chop->u.get_elem_stat.type,
258 			    chop->u.get_elem_stat.from,
259 			    &chop->u.get_elem_stat.elem_data, flags);
260 		default:
261 			return EINVAL;
262 		}
263 	}
264 	default:
265 		return scsi_do_ioctl(sc_link, dev, cmd, arg, mode);
266 	}
267 #ifdef DIAGNOSTIC
268 	panic("chioctl: impossible");
269 #endif
270 }
271 
272 int
273 ch_getelem(ch, stat, type, from, data, flags)
274 	struct ch_data *ch;
275 	short *stat;
276 	int type, from;
277 	char *data;
278 	int flags;
279 {
280 	struct scsi_read_element_status scsi_cmd;
281 	char elbuf[32];
282 	int error;
283 
284 	bzero(&scsi_cmd, sizeof(scsi_cmd));
285 	scsi_cmd.op_code = READ_ELEMENT_STATUS;
286 	scsi_cmd.byte2 = type;
287 	scsi_cmd.starting_element_addr[0] = (from >> 8) & 0xff;
288 	scsi_cmd.starting_element_addr[1] = from & 0xff;
289 	scsi_cmd.number_of_elements[1] = 1;
290 	scsi_cmd.allocation_length[2] = 32;
291 
292 	error = scsi_scsi_cmd(ch->sc_link, (struct scsi_generic *) &scsi_cmd,
293 	    sizeof(scsi_cmd), (u_char *) elbuf, 32, CHRETRIES, 100000, NULL,
294 	    SCSI_DATA_IN | flags);
295 	if (error)
296 		*stat = ch->lsterr;
297 	else
298 		*stat = 0;
299 	bcopy(elbuf + 16, data, 16);
300 	return error;
301 }
302 
303 int
304 ch_move(ch, stat, chm, from, to, flags)
305 	struct ch_data *ch;
306 	short *stat;
307 	int chm, from, to, flags;
308 {
309 	struct scsi_move_medium scsi_cmd;
310 	int error;
311 
312 	bzero(&scsi_cmd, sizeof(scsi_cmd));
313 	scsi_cmd.op_code = MOVE_MEDIUM;
314 	scsi_cmd.transport_element_address[0] = (chm >> 8) & 0xff;
315 	scsi_cmd.transport_element_address[1] = chm & 0xff;
316 	scsi_cmd.source_address[0] = (from >> 8) & 0xff;
317 	scsi_cmd.source_address[1] = from & 0xff;
318 	scsi_cmd.destination_address[0] = (to >> 8) & 0xff;
319 	scsi_cmd.destination_address[1] = to & 0xff;
320 	scsi_cmd.invert = (chm & CH_INVERT) ? 1 : 0;
321 	error = scsi_scsi_cmd(ch->sc_link, (struct scsi_generic *) &scsi_cmd,
322 	    sizeof(scsi_cmd), NULL, 0, CHRETRIES, 100000, NULL, flags);
323 	if (error)
324 		*stat = ch->lsterr;
325 	else
326 		*stat = 0;
327 	return error;
328 }
329 
330 int
331 ch_position(ch, stat, chm, to, flags)
332 	struct ch_data *ch;
333 	short *stat;
334 	int chm, to, flags;
335 {
336 	struct scsi_position_to_element scsi_cmd;
337 	int error;
338 
339 	bzero(&scsi_cmd, sizeof(scsi_cmd));
340 	scsi_cmd.op_code = POSITION_TO_ELEMENT;
341 	scsi_cmd.transport_element_address[0] = (chm >> 8) & 0xff;
342 	scsi_cmd.transport_element_address[1] = chm & 0xff;
343 	scsi_cmd.source_address[0] = (to >> 8) & 0xff;
344 	scsi_cmd.source_address[1] = to & 0xff;
345 	scsi_cmd.invert = (chm & CH_INVERT) ? 1 : 0;
346 	error = scsi_scsi_cmd(ch->sc_link, (struct scsi_generic *) &scsi_cmd,
347 	    sizeof(scsi_cmd), NULL, 0, CHRETRIES, 100000, NULL, flags);
348 	if (error)
349 		*stat = ch->lsterr;
350 	else
351 		*stat = 0;
352 	return error;
353 }
354 
355 #ifdef	__STDC__
356 #define b2tol(a)	(((unsigned)(a##_1) << 8) | (unsigned)a##_0)
357 #else
358 #define b2tol(a)	(((unsigned)(a/**/_1) << 8) | (unsigned)a/**/_0)
359 #endif
360 
361 /*
362  * Get the scsi driver to send a full inquiry to the
363  * device and use the results to fill out the global
364  * parameter structure.
365  */
366 int
367 ch_mode_sense(ch, flags)
368 	struct ch_data *ch;
369 	int flags;
370 {
371 	struct scsi_mode_sense scsi_cmd;
372 	u_char scsi_sense[128];	/* Can't use scsi_mode_sense_data because of
373 				 * missing block descriptor.
374 				 */
375 	u_char *b;
376 	int i, l;
377 	int error;
378 	struct scsi_link *sc_link = ch->sc_link;
379 
380 	/*
381 	 * First check if we have it all loaded
382 	 */
383 	if (sc_link->flags & SDEV_MEDIA_LOADED)
384 		return 0;
385 
386 	/*
387 	 * First do a mode sense
388 	 */
389 	/* sc_link->flags &= ~SDEV_MEDIA_LOADED; *//*XXX */
390 	bzero(&scsi_cmd, sizeof(scsi_cmd));
391 	scsi_cmd.op_code = MODE_SENSE;
392 	scsi_cmd.byte2 = SMS_DBD;
393 	scsi_cmd.page = 0x3f;	/* All Pages */
394 	scsi_cmd.length = sizeof(scsi_sense);
395 
396 	/*
397 	 * Read in the pages
398 	 */
399 	error = scsi_scsi_cmd(sc_link, (struct scsi_generic *) &scsi_cmd,
400 	    sizeof(scsi_cmd), (u_char *) &scsi_sense, sizeof(scsi_sense),
401 	    CHRETRIES, 5000, NULL, flags | SCSI_DATA_IN);
402 	if (error) {
403 		if (!(flags & SCSI_SILENT))
404 			printf("%s: could not mode sense\n",
405 			    ch->sc_dev.dv_xname);
406 		return error;
407 	}
408 
409 	sc_link->flags |= SDEV_MEDIA_LOADED;
410 	l = scsi_sense[0] - 3;
411 	b = &scsi_sense[4];
412 
413 	/*
414 	 * To avoid alignment problems
415 	 */
416 /* XXXX - FIX THIS FOR MSB */
417 #define p2copy(valp)	 (valp[1] | (valp[0]<<8)); valp+=2
418 #define p4copy(valp)	 (valp[3] | (valp[2]<<8) | (valp[1]<<16) | (valp[0]<<24)); valp+=4
419 #if 0
420 	printf("\nmode_sense %d\n", l);
421 	for (i = 0; i < l + 4; i++)
422 		printf("%x%c", scsi_sense[i], i % 8 == 7 ? '\n' : ':');
423 	printf("\n");
424 #endif
425 	for (i = 0; i < l;) {
426 		u_char pc = (*b++) & 0x3f;
427 		u_char pl = *b++;
428 		u_char *bb = b;
429 		switch (pc) {
430 		case 0x1d:
431 			ch->chmo = p2copy(bb);
432 			ch->chms = p2copy(bb);
433 			ch->sloto = p2copy(bb);
434 			ch->slots = p2copy(bb);
435 			ch->imexo = p2copy(bb);
436 			ch->imexs = p2copy(bb);
437 			ch->driveo = p2copy(bb);
438 			ch->drives = p2copy(bb);
439 			break;
440 		case 0x1e:
441 			ch->rot = *b & 0x1;
442 			break;
443 		case 0x1f:
444 			ch->stor = *b & 0xf;
445 			bb += 2;
446 			ch->stor = p4copy(bb);
447 			break;
448 		default:
449 			break;
450 		}
451 		b += pl;
452 		i += pl + 2;
453 	}
454 	SC_DEBUG(sc_link, SDEV_DB2,
455 	    (" cht(%d-%d)slot(%d-%d)imex(%d-%d)cts(%d-%d) %s rotate\n",
456 	    ch->chmo, ch->chms, ch->sloto, ch->slots, ch->imexo, ch->imexs,
457 	    ch->driveo, ch->drives, ch->rot ? "can" : "can't"));
458 	return 0;
459 }
460