xref: /linux/drivers/scsi/ch.c (revision 78c3925c048c752334873f56c3a3d1c9d53e0416)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * SCSI Media Changer device driver for Linux 2.6
4  *
5  *     (c) 1996-2003 Gerd Knorr <kraxel@bytesex.org>
6  *
7  */
8 
9 #define VERSION "0.25"
10 
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/fs.h>
14 #include <linux/kernel.h>
15 #include <linux/mm.h>
16 #include <linux/major.h>
17 #include <linux/string.h>
18 #include <linux/errno.h>
19 #include <linux/interrupt.h>
20 #include <linux/blkdev.h>
21 #include <linux/completion.h>
22 #include <linux/compat.h>
23 #include <linux/chio.h>			/* here are all the ioctls */
24 #include <linux/mutex.h>
25 #include <linux/idr.h>
26 #include <linux/slab.h>
27 
28 #include <scsi/scsi.h>
29 #include <scsi/scsi_cmnd.h>
30 #include <scsi/scsi_driver.h>
31 #include <scsi/scsi_ioctl.h>
32 #include <scsi/scsi_host.h>
33 #include <scsi/scsi_device.h>
34 #include <scsi/scsi_eh.h>
35 #include <scsi/scsi_dbg.h>
36 
37 #define CH_DT_MAX       16
38 #define CH_TYPES        8
39 #define CH_MAX_DEVS     128
40 
41 MODULE_DESCRIPTION("device driver for scsi media changer devices");
42 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org>");
43 MODULE_LICENSE("GPL");
44 MODULE_ALIAS_CHARDEV_MAJOR(SCSI_CHANGER_MAJOR);
45 MODULE_ALIAS_SCSI_DEVICE(TYPE_MEDIUM_CHANGER);
46 
47 static int init = 1;
48 module_param(init, int, 0444);
49 MODULE_PARM_DESC(init, \
50     "initialize element status on driver load (default: on)");
51 
52 static int timeout_move = 300;
53 module_param(timeout_move, int, 0644);
54 MODULE_PARM_DESC(timeout_move,"timeout for move commands "
55 		 "(default: 300 seconds)");
56 
57 static int timeout_init = 3600;
58 module_param(timeout_init, int, 0644);
59 MODULE_PARM_DESC(timeout_init,"timeout for INITIALIZE ELEMENT STATUS "
60 		 "(default: 3600 seconds)");
61 
62 static int verbose = 1;
63 module_param(verbose, int, 0644);
64 MODULE_PARM_DESC(verbose,"be verbose (default: on)");
65 
66 static int debug;
67 module_param(debug, int, 0644);
68 MODULE_PARM_DESC(debug,"enable/disable debug messages, also prints more "
69 		 "detailed sense codes on scsi errors (default: off)");
70 
71 static int dt_id[CH_DT_MAX] = { [ 0 ... (CH_DT_MAX-1) ] = -1 };
72 static int dt_lun[CH_DT_MAX];
73 module_param_array(dt_id,  int, NULL, 0444);
74 module_param_array(dt_lun, int, NULL, 0444);
75 
76 /* tell the driver about vendor-specific slots */
77 static int vendor_firsts[CH_TYPES-4];
78 static int vendor_counts[CH_TYPES-4];
79 module_param_array(vendor_firsts, int, NULL, 0444);
80 module_param_array(vendor_counts, int, NULL, 0444);
81 
82 static const char * vendor_labels[CH_TYPES-4] = {
83 	"v0", "v1", "v2", "v3"
84 };
85 // module_param_string_array(vendor_labels, NULL, 0444);
86 
87 #define ch_printk(prefix, ch, fmt, a...) \
88 	sdev_prefix_printk(prefix, (ch)->device, (ch)->name, fmt, ##a)
89 
90 #define DPRINTK(fmt, arg...)						\
91 do {									\
92 	if (debug)							\
93 		ch_printk(KERN_DEBUG, ch, fmt, ##arg);			\
94 } while (0)
95 #define VPRINTK(level, fmt, arg...)					\
96 do {									\
97 	if (verbose)							\
98 		ch_printk(level, ch, fmt, ##arg);			\
99 } while (0)
100 
101 /* ------------------------------------------------------------------- */
102 
103 #define MAX_RETRIES   1
104 
105 static struct class * ch_sysfs_class;
106 
107 typedef struct {
108 	struct kref         ref;
109 	struct list_head    list;
110 	int                 minor;
111 	char                name[8];
112 	struct scsi_device  *device;
113 	struct scsi_device  **dt;        /* ptrs to data transfer elements */
114 	u_int               firsts[CH_TYPES];
115 	u_int               counts[CH_TYPES];
116 	u_int		    voltags;
117 	struct mutex	    lock;
118 } scsi_changer;
119 
120 static DEFINE_IDR(ch_index_idr);
121 static DEFINE_SPINLOCK(ch_index_lock);
122 
123 static const struct {
124 	unsigned char  sense;
125 	unsigned char  asc;
126 	unsigned char  ascq;
127 	int	       errno;
128 } ch_err[] = {
129 /* Just filled in what looks right. Hav'nt checked any standard paper for
130    these errno assignments, so they may be wrong... */
131 	{
132 		.sense  = ILLEGAL_REQUEST,
133 		.asc    = 0x21,
134 		.ascq   = 0x01,
135 		.errno  = EBADSLT, /* Invalid element address */
136 	},{
137 		.sense  = ILLEGAL_REQUEST,
138 		.asc    = 0x28,
139 		.ascq   = 0x01,
140 		.errno  = EBADE,   /* Import or export element accessed */
141 	},{
142 		.sense  = ILLEGAL_REQUEST,
143 		.asc    = 0x3B,
144 		.ascq   = 0x0D,
145 		.errno  = EXFULL,  /* Medium destination element full */
146 	},{
147 		.sense  = ILLEGAL_REQUEST,
148 		.asc    = 0x3B,
149 		.ascq   = 0x0E,
150 		.errno  = EBADE,   /* Medium source element empty */
151 	},{
152 		.sense  = ILLEGAL_REQUEST,
153 		.asc    = 0x20,
154 		.ascq   = 0x00,
155 		.errno  = EBADRQC, /* Invalid command operation code */
156 	},{
157 	        /* end of list */
158 	}
159 };
160 
161 /* ------------------------------------------------------------------- */
162 
163 static int ch_find_errno(struct scsi_sense_hdr *sshdr)
164 {
165 	int i,errno = 0;
166 
167 	/* Check to see if additional sense information is available */
168 	if (scsi_sense_valid(sshdr) &&
169 	    sshdr->asc != 0) {
170 		for (i = 0; ch_err[i].errno != 0; i++) {
171 			if (ch_err[i].sense == sshdr->sense_key &&
172 			    ch_err[i].asc   == sshdr->asc &&
173 			    ch_err[i].ascq  == sshdr->ascq) {
174 				errno = -ch_err[i].errno;
175 				break;
176 			}
177 		}
178 	}
179 	if (errno == 0)
180 		errno = -EIO;
181 	return errno;
182 }
183 
184 static int
185 ch_do_scsi(scsi_changer *ch, unsigned char *cmd, int cmd_len,
186 	   void *buffer, unsigned int buflength, enum req_op op)
187 {
188 	int errno = 0, timeout, result;
189 	struct scsi_sense_hdr sshdr;
190 	struct scsi_failure failure_defs[] = {
191 		{
192 			.sense = UNIT_ATTENTION,
193 			.asc = SCMD_FAILURE_ASC_ANY,
194 			.ascq = SCMD_FAILURE_ASCQ_ANY,
195 			.allowed = 3,
196 			.result = SAM_STAT_CHECK_CONDITION,
197 		},
198 		{}
199 	};
200 	struct scsi_failures failures = {
201 		.failure_definitions = failure_defs,
202 	};
203 	const struct scsi_exec_args exec_args = {
204 		.sshdr = &sshdr,
205 		.failures = &failures,
206 	};
207 
208 	timeout = (cmd[0] == INITIALIZE_ELEMENT_STATUS)
209 		? timeout_init : timeout_move;
210 
211 	result = scsi_execute_cmd(ch->device, cmd, op, buffer, buflength,
212 				  timeout * HZ, MAX_RETRIES, &exec_args);
213 	if (result < 0)
214 		return result;
215 	if (scsi_sense_valid(&sshdr)) {
216 		if (debug)
217 			scsi_print_sense_hdr(ch->device, ch->name, &sshdr);
218 		errno = ch_find_errno(&sshdr);
219 	}
220 	return errno;
221 }
222 
223 /* ------------------------------------------------------------------------ */
224 
225 static int
226 ch_elem_to_typecode(scsi_changer *ch, u_int elem)
227 {
228 	int i;
229 
230 	for (i = 0; i < CH_TYPES; i++) {
231 		if (elem >= ch->firsts[i]  &&
232 		    elem <  ch->firsts[i] +
233 	            ch->counts[i])
234 			return i+1;
235 	}
236 	return 0;
237 }
238 
239 static int
240 ch_read_element_status(scsi_changer *ch, u_int elem, char *data)
241 {
242 	u_char  cmd[12];
243 	u_char  *buffer;
244 	int     result;
245 
246 	buffer = kmalloc(512, GFP_KERNEL);
247 	if(!buffer)
248 		return -ENOMEM;
249 
250  retry:
251 	memset(cmd,0,sizeof(cmd));
252 	cmd[0] = READ_ELEMENT_STATUS;
253 	cmd[1] = ((ch->device->lun & 0x7) << 5) |
254 		(ch->voltags ? 0x10 : 0) |
255 		ch_elem_to_typecode(ch,elem);
256 	cmd[2] = (elem >> 8) & 0xff;
257 	cmd[3] = elem        & 0xff;
258 	cmd[5] = 1;
259 	cmd[9] = 255;
260 	if (0 == (result = ch_do_scsi(ch, cmd, 12,
261 				      buffer, 256, REQ_OP_DRV_IN))) {
262 		if (((buffer[16] << 8) | buffer[17]) != elem) {
263 			DPRINTK("asked for element 0x%02x, got 0x%02x\n",
264 				elem,(buffer[16] << 8) | buffer[17]);
265 			kfree(buffer);
266 			return -EIO;
267 		}
268 		memcpy(data,buffer+16,16);
269 	} else {
270 		if (ch->voltags) {
271 			ch->voltags = 0;
272 			VPRINTK(KERN_INFO, "device has no volume tag support\n");
273 			goto retry;
274 		}
275 		DPRINTK("READ ELEMENT STATUS for element 0x%x failed\n",elem);
276 	}
277 	kfree(buffer);
278 	return result;
279 }
280 
281 static int
282 ch_init_elem(scsi_changer *ch)
283 {
284 	int err;
285 	u_char cmd[6];
286 
287 	VPRINTK(KERN_INFO, "INITIALIZE ELEMENT STATUS, may take some time ...\n");
288 	memset(cmd,0,sizeof(cmd));
289 	cmd[0] = INITIALIZE_ELEMENT_STATUS;
290 	cmd[1] = (ch->device->lun & 0x7) << 5;
291 	err = ch_do_scsi(ch, cmd, 6, NULL, 0, REQ_OP_DRV_IN);
292 	VPRINTK(KERN_INFO, "... finished\n");
293 	return err;
294 }
295 
296 static int
297 ch_readconfig(scsi_changer *ch)
298 {
299 	u_char  cmd[10], data[16];
300 	u_char  *buffer;
301 	int     result,id,lun,i;
302 	u_int   elem;
303 
304 	buffer = kzalloc(512, GFP_KERNEL);
305 	if (!buffer)
306 		return -ENOMEM;
307 
308 	memset(cmd,0,sizeof(cmd));
309 	cmd[0] = MODE_SENSE;
310 	cmd[1] = (ch->device->lun & 0x7) << 5;
311 	cmd[2] = 0x1d;
312 	cmd[4] = 255;
313 	result = ch_do_scsi(ch, cmd, 10, buffer, 255, REQ_OP_DRV_IN);
314 	if (0 != result) {
315 		cmd[1] |= (1<<3);
316 		result  = ch_do_scsi(ch, cmd, 10, buffer, 255, REQ_OP_DRV_IN);
317 	}
318 	if (0 == result) {
319 		ch->firsts[CHET_MT] =
320 			(buffer[buffer[3]+ 6] << 8) | buffer[buffer[3]+ 7];
321 		ch->counts[CHET_MT] =
322 			(buffer[buffer[3]+ 8] << 8) | buffer[buffer[3]+ 9];
323 		ch->firsts[CHET_ST] =
324 			(buffer[buffer[3]+10] << 8) | buffer[buffer[3]+11];
325 		ch->counts[CHET_ST] =
326 			(buffer[buffer[3]+12] << 8) | buffer[buffer[3]+13];
327 		ch->firsts[CHET_IE] =
328 			(buffer[buffer[3]+14] << 8) | buffer[buffer[3]+15];
329 		ch->counts[CHET_IE] =
330 			(buffer[buffer[3]+16] << 8) | buffer[buffer[3]+17];
331 		ch->firsts[CHET_DT] =
332 			(buffer[buffer[3]+18] << 8) | buffer[buffer[3]+19];
333 		ch->counts[CHET_DT] =
334 			(buffer[buffer[3]+20] << 8) | buffer[buffer[3]+21];
335 		VPRINTK(KERN_INFO, "type #1 (mt): 0x%x+%d [medium transport]\n",
336 			ch->firsts[CHET_MT],
337 			ch->counts[CHET_MT]);
338 		VPRINTK(KERN_INFO, "type #2 (st): 0x%x+%d [storage]\n",
339 			ch->firsts[CHET_ST],
340 			ch->counts[CHET_ST]);
341 		VPRINTK(KERN_INFO, "type #3 (ie): 0x%x+%d [import/export]\n",
342 			ch->firsts[CHET_IE],
343 			ch->counts[CHET_IE]);
344 		VPRINTK(KERN_INFO, "type #4 (dt): 0x%x+%d [data transfer]\n",
345 			ch->firsts[CHET_DT],
346 			ch->counts[CHET_DT]);
347 	} else {
348 		VPRINTK(KERN_INFO, "reading element address assignment page failed!\n");
349 	}
350 
351 	/* vendor specific element types */
352 	for (i = 0; i < 4; i++) {
353 		if (0 == vendor_counts[i])
354 			continue;
355 		if (NULL == vendor_labels[i])
356 			continue;
357 		ch->firsts[CHET_V1+i] = vendor_firsts[i];
358 		ch->counts[CHET_V1+i] = vendor_counts[i];
359 		VPRINTK(KERN_INFO, "type #%d (v%d): 0x%x+%d [%s, vendor specific]\n",
360 			i+5,i+1,vendor_firsts[i],vendor_counts[i],
361 			vendor_labels[i]);
362 	}
363 
364 	/* look up the devices of the data transfer elements */
365 	ch->dt = kcalloc(ch->counts[CHET_DT], sizeof(*ch->dt),
366 			 GFP_KERNEL);
367 
368 	if (!ch->dt) {
369 		kfree(buffer);
370 		return -ENOMEM;
371 	}
372 
373 	for (elem = 0; elem < ch->counts[CHET_DT]; elem++) {
374 		id  = -1;
375 		lun = 0;
376 		if (elem < CH_DT_MAX  &&  -1 != dt_id[elem]) {
377 			id  = dt_id[elem];
378 			lun = dt_lun[elem];
379 			VPRINTK(KERN_INFO, "dt 0x%x: [insmod option] ",
380 				elem+ch->firsts[CHET_DT]);
381 		} else if (0 != ch_read_element_status
382 			   (ch,elem+ch->firsts[CHET_DT],data)) {
383 			VPRINTK(KERN_INFO, "dt 0x%x: READ ELEMENT STATUS failed\n",
384 				elem+ch->firsts[CHET_DT]);
385 		} else {
386 			VPRINTK(KERN_INFO, "dt 0x%x: ",elem+ch->firsts[CHET_DT]);
387 			if (data[6] & 0x80) {
388 				VPRINTK(KERN_CONT, "not this SCSI bus\n");
389 				ch->dt[elem] = NULL;
390 			} else if (0 == (data[6] & 0x30)) {
391 				VPRINTK(KERN_CONT, "ID/LUN unknown\n");
392 				ch->dt[elem] = NULL;
393 			} else {
394 				id  = ch->device->id;
395 				lun = 0;
396 				if (data[6] & 0x20) id  = data[7];
397 				if (data[6] & 0x10) lun = data[6] & 7;
398 			}
399 		}
400 		if (-1 != id) {
401 			VPRINTK(KERN_CONT, "ID %i, LUN %i, ",id,lun);
402 			ch->dt[elem] =
403 				scsi_device_lookup(ch->device->host,
404 						   ch->device->channel,
405 						   id,lun);
406 			if (!ch->dt[elem]) {
407 				/* should not happen */
408 				VPRINTK(KERN_CONT, "Huh? device not found!\n");
409 			} else {
410 				VPRINTK(KERN_CONT, "name: %8.8s %16.16s %4.4s\n",
411 					ch->dt[elem]->vendor,
412 					ch->dt[elem]->model,
413 					ch->dt[elem]->rev);
414 			}
415 		}
416 	}
417 	ch->voltags = 1;
418 	kfree(buffer);
419 
420 	return 0;
421 }
422 
423 /* ------------------------------------------------------------------------ */
424 
425 static int
426 ch_position(scsi_changer *ch, u_int trans, u_int elem, int rotate)
427 {
428 	u_char  cmd[10];
429 
430 	DPRINTK("position: 0x%x\n",elem);
431 	if (0 == trans)
432 		trans = ch->firsts[CHET_MT];
433 	memset(cmd,0,sizeof(cmd));
434 	cmd[0]  = POSITION_TO_ELEMENT;
435 	cmd[1]  = (ch->device->lun & 0x7) << 5;
436 	cmd[2]  = (trans >> 8) & 0xff;
437 	cmd[3]  =  trans       & 0xff;
438 	cmd[4]  = (elem  >> 8) & 0xff;
439 	cmd[5]  =  elem        & 0xff;
440 	cmd[8]  = rotate ? 1 : 0;
441 	return ch_do_scsi(ch, cmd, 10, NULL, 0, REQ_OP_DRV_IN);
442 }
443 
444 static int
445 ch_move(scsi_changer *ch, u_int trans, u_int src, u_int dest, int rotate)
446 {
447 	u_char  cmd[12];
448 
449 	DPRINTK("move: 0x%x => 0x%x\n",src,dest);
450 	if (0 == trans)
451 		trans = ch->firsts[CHET_MT];
452 	memset(cmd,0,sizeof(cmd));
453 	cmd[0]  = MOVE_MEDIUM;
454 	cmd[1]  = (ch->device->lun & 0x7) << 5;
455 	cmd[2]  = (trans >> 8) & 0xff;
456 	cmd[3]  =  trans       & 0xff;
457 	cmd[4]  = (src   >> 8) & 0xff;
458 	cmd[5]  =  src         & 0xff;
459 	cmd[6]  = (dest  >> 8) & 0xff;
460 	cmd[7]  =  dest        & 0xff;
461 	cmd[10] = rotate ? 1 : 0;
462 	return ch_do_scsi(ch, cmd, 12, NULL, 0, REQ_OP_DRV_IN);
463 }
464 
465 static int
466 ch_exchange(scsi_changer *ch, u_int trans, u_int src,
467 	    u_int dest1, u_int dest2, int rotate1, int rotate2)
468 {
469 	u_char  cmd[12];
470 
471 	DPRINTK("exchange: 0x%x => 0x%x => 0x%x\n",
472 		src,dest1,dest2);
473 	if (0 == trans)
474 		trans = ch->firsts[CHET_MT];
475 	memset(cmd,0,sizeof(cmd));
476 	cmd[0]  = EXCHANGE_MEDIUM;
477 	cmd[1]  = (ch->device->lun & 0x7) << 5;
478 	cmd[2]  = (trans >> 8) & 0xff;
479 	cmd[3]  =  trans       & 0xff;
480 	cmd[4]  = (src   >> 8) & 0xff;
481 	cmd[5]  =  src         & 0xff;
482 	cmd[6]  = (dest1 >> 8) & 0xff;
483 	cmd[7]  =  dest1       & 0xff;
484 	cmd[8]  = (dest2 >> 8) & 0xff;
485 	cmd[9]  =  dest2       & 0xff;
486 	cmd[10] = (rotate1 ? 1 : 0) | (rotate2 ? 2 : 0);
487 
488 	return ch_do_scsi(ch, cmd, 12, NULL, 0, REQ_OP_DRV_IN);
489 }
490 
491 static void
492 ch_check_voltag(char *tag)
493 {
494 	int i;
495 
496 	for (i = 0; i < 32; i++) {
497 		/* restrict to ascii */
498 		if (tag[i] >= 0x7f || tag[i] < 0x20)
499 			tag[i] = ' ';
500 		/* don't allow search wildcards */
501 		if (tag[i] == '?' ||
502 		    tag[i] == '*')
503 			tag[i] = ' ';
504 	}
505 }
506 
507 static int
508 ch_set_voltag(scsi_changer *ch, u_int elem,
509 	      int alternate, int clear, u_char *tag)
510 {
511 	u_char  cmd[12];
512 	u_char  *buffer;
513 	int result;
514 
515 	buffer = kzalloc(512, GFP_KERNEL);
516 	if (!buffer)
517 		return -ENOMEM;
518 
519 	DPRINTK("%s %s voltag: 0x%x => \"%s\"\n",
520 		clear     ? "clear"     : "set",
521 		alternate ? "alternate" : "primary",
522 		elem, tag);
523 	memset(cmd,0,sizeof(cmd));
524 	cmd[0]  = SEND_VOLUME_TAG;
525 	cmd[1] = ((ch->device->lun & 0x7) << 5) |
526 		ch_elem_to_typecode(ch,elem);
527 	cmd[2] = (elem >> 8) & 0xff;
528 	cmd[3] = elem        & 0xff;
529 	cmd[5] = clear
530 		? (alternate ? 0x0d : 0x0c)
531 		: (alternate ? 0x0b : 0x0a);
532 
533 	cmd[9] = 255;
534 
535 	memcpy(buffer,tag,32);
536 	ch_check_voltag(buffer);
537 
538 	result = ch_do_scsi(ch, cmd, 12, buffer, 256, REQ_OP_DRV_OUT);
539 	kfree(buffer);
540 	return result;
541 }
542 
543 static int ch_gstatus(scsi_changer *ch, int type, unsigned char __user *dest)
544 {
545 	int retval = 0;
546 	u_char data[16];
547 	unsigned int i;
548 
549 	mutex_lock(&ch->lock);
550 	for (i = 0; i < ch->counts[type]; i++) {
551 		if (0 != ch_read_element_status
552 		    (ch, ch->firsts[type]+i,data)) {
553 			retval = -EIO;
554 			break;
555 		}
556 		put_user(data[2], dest+i);
557 		if (data[2] & CESTATUS_EXCEPT)
558 			VPRINTK(KERN_INFO, "element 0x%x: asc=0x%x, ascq=0x%x\n",
559 				ch->firsts[type]+i,
560 				(int)data[4],(int)data[5]);
561 		retval = ch_read_element_status
562 			(ch, ch->firsts[type]+i,data);
563 		if (0 != retval)
564 			break;
565 	}
566 	mutex_unlock(&ch->lock);
567 	return retval;
568 }
569 
570 /* ------------------------------------------------------------------------ */
571 
572 static void ch_destroy(struct kref *ref)
573 {
574 	scsi_changer *ch = container_of(ref, scsi_changer, ref);
575 
576 	ch->device = NULL;
577 	kfree(ch->dt);
578 	kfree(ch);
579 }
580 
581 static int
582 ch_release(struct inode *inode, struct file *file)
583 {
584 	scsi_changer *ch = file->private_data;
585 
586 	scsi_device_put(ch->device);
587 	file->private_data = NULL;
588 	kref_put(&ch->ref, ch_destroy);
589 	return 0;
590 }
591 
592 static int
593 ch_open(struct inode *inode, struct file *file)
594 {
595 	scsi_changer *ch;
596 	int minor = iminor(inode);
597 
598 	spin_lock(&ch_index_lock);
599 	ch = idr_find(&ch_index_idr, minor);
600 
601 	if (ch == NULL || !kref_get_unless_zero(&ch->ref)) {
602 		spin_unlock(&ch_index_lock);
603 		return -ENXIO;
604 	}
605 	spin_unlock(&ch_index_lock);
606 	if (scsi_device_get(ch->device)) {
607 		kref_put(&ch->ref, ch_destroy);
608 		return -ENXIO;
609 	}
610 	/* Synchronize with ch_probe() */
611 	mutex_lock(&ch->lock);
612 	file->private_data = ch;
613 	mutex_unlock(&ch->lock);
614 	return 0;
615 }
616 
617 static int
618 ch_checkrange(scsi_changer *ch, unsigned int type, unsigned int unit)
619 {
620 	if (type >= CH_TYPES  ||  unit >= ch->counts[type])
621 		return -1;
622 	return 0;
623 }
624 
625 struct changer_element_status32 {
626 	int		ces_type;
627 	compat_uptr_t	ces_data;
628 };
629 #define CHIOGSTATUS32  _IOW('c', 8, struct changer_element_status32)
630 
631 static long ch_ioctl(struct file *file,
632 		    unsigned int cmd, unsigned long arg)
633 {
634 	scsi_changer *ch = file->private_data;
635 	int retval;
636 	void __user *argp = (void __user *)arg;
637 
638 	retval = scsi_ioctl_block_when_processing_errors(ch->device, cmd,
639 			file->f_flags & O_NDELAY);
640 	if (retval)
641 		return retval;
642 
643 	switch (cmd) {
644 	case CHIOGPARAMS:
645 	{
646 		struct changer_params params;
647 
648 		params.cp_curpicker = 0;
649 		params.cp_npickers  = ch->counts[CHET_MT];
650 		params.cp_nslots    = ch->counts[CHET_ST];
651 		params.cp_nportals  = ch->counts[CHET_IE];
652 		params.cp_ndrives   = ch->counts[CHET_DT];
653 
654 		if (copy_to_user(argp, &params, sizeof(params)))
655 			return -EFAULT;
656 		return 0;
657 	}
658 	case CHIOGVPARAMS:
659 	{
660 		struct changer_vendor_params vparams;
661 
662 		memset(&vparams,0,sizeof(vparams));
663 		if (ch->counts[CHET_V1]) {
664 			vparams.cvp_n1  = ch->counts[CHET_V1];
665 			strscpy(vparams.cvp_label1, vendor_labels[0],
666 				sizeof(vparams.cvp_label1));
667 		}
668 		if (ch->counts[CHET_V2]) {
669 			vparams.cvp_n2  = ch->counts[CHET_V2];
670 			strscpy(vparams.cvp_label2, vendor_labels[1],
671 				sizeof(vparams.cvp_label2));
672 		}
673 		if (ch->counts[CHET_V3]) {
674 			vparams.cvp_n3  = ch->counts[CHET_V3];
675 			strscpy(vparams.cvp_label3, vendor_labels[2],
676 				sizeof(vparams.cvp_label3));
677 		}
678 		if (ch->counts[CHET_V4]) {
679 			vparams.cvp_n4  = ch->counts[CHET_V4];
680 			strscpy(vparams.cvp_label4, vendor_labels[3],
681 				sizeof(vparams.cvp_label4));
682 		}
683 		if (copy_to_user(argp, &vparams, sizeof(vparams)))
684 			return -EFAULT;
685 		return 0;
686 	}
687 
688 	case CHIOPOSITION:
689 	{
690 		struct changer_position pos;
691 
692 		if (copy_from_user(&pos, argp, sizeof (pos)))
693 			return -EFAULT;
694 
695 		if (0 != ch_checkrange(ch, pos.cp_type, pos.cp_unit)) {
696 			DPRINTK("CHIOPOSITION: invalid parameter\n");
697 			return -EBADSLT;
698 		}
699 		mutex_lock(&ch->lock);
700 		retval = ch_position(ch,0,
701 				     ch->firsts[pos.cp_type] + pos.cp_unit,
702 				     pos.cp_flags & CP_INVERT);
703 		mutex_unlock(&ch->lock);
704 		return retval;
705 	}
706 
707 	case CHIOMOVE:
708 	{
709 		struct changer_move mv;
710 
711 		if (copy_from_user(&mv, argp, sizeof (mv)))
712 			return -EFAULT;
713 
714 		if (0 != ch_checkrange(ch, mv.cm_fromtype, mv.cm_fromunit) ||
715 		    0 != ch_checkrange(ch, mv.cm_totype,   mv.cm_tounit  )) {
716 			DPRINTK("CHIOMOVE: invalid parameter\n");
717 			return -EBADSLT;
718 		}
719 
720 		mutex_lock(&ch->lock);
721 		retval = ch_move(ch,0,
722 				 ch->firsts[mv.cm_fromtype] + mv.cm_fromunit,
723 				 ch->firsts[mv.cm_totype]   + mv.cm_tounit,
724 				 mv.cm_flags & CM_INVERT);
725 		mutex_unlock(&ch->lock);
726 		return retval;
727 	}
728 
729 	case CHIOEXCHANGE:
730 	{
731 		struct changer_exchange mv;
732 
733 		if (copy_from_user(&mv, argp, sizeof (mv)))
734 			return -EFAULT;
735 
736 		if (0 != ch_checkrange(ch, mv.ce_srctype,  mv.ce_srcunit ) ||
737 		    0 != ch_checkrange(ch, mv.ce_fdsttype, mv.ce_fdstunit) ||
738 		    0 != ch_checkrange(ch, mv.ce_sdsttype, mv.ce_sdstunit)) {
739 			DPRINTK("CHIOEXCHANGE: invalid parameter\n");
740 			return -EBADSLT;
741 		}
742 
743 		mutex_lock(&ch->lock);
744 		retval = ch_exchange
745 			(ch,0,
746 			 ch->firsts[mv.ce_srctype]  + mv.ce_srcunit,
747 			 ch->firsts[mv.ce_fdsttype] + mv.ce_fdstunit,
748 			 ch->firsts[mv.ce_sdsttype] + mv.ce_sdstunit,
749 			 mv.ce_flags & CE_INVERT1, mv.ce_flags & CE_INVERT2);
750 		mutex_unlock(&ch->lock);
751 		return retval;
752 	}
753 
754 	case CHIOGSTATUS:
755 	{
756 		struct changer_element_status ces;
757 
758 		if (copy_from_user(&ces, argp, sizeof (ces)))
759 			return -EFAULT;
760 		if (ces.ces_type < 0 || ces.ces_type >= CH_TYPES)
761 			return -EINVAL;
762 
763 		return ch_gstatus(ch, ces.ces_type, ces.ces_data);
764 	}
765 #ifdef CONFIG_COMPAT
766 	case CHIOGSTATUS32:
767 	{
768 		struct changer_element_status32 ces32;
769 
770 		if (copy_from_user(&ces32, argp, sizeof(ces32)))
771 			return -EFAULT;
772 		if (ces32.ces_type < 0 || ces32.ces_type >= CH_TYPES)
773 			return -EINVAL;
774 
775 		return ch_gstatus(ch, ces32.ces_type,
776 				  compat_ptr(ces32.ces_data));
777 	}
778 #endif
779 	case CHIOGELEM:
780 	{
781 		struct changer_get_element cge;
782 		u_char ch_cmd[12];
783 		u_char *buffer;
784 		unsigned int elem;
785 		int     result,i;
786 
787 		if (copy_from_user(&cge, argp, sizeof (cge)))
788 			return -EFAULT;
789 
790 		if (0 != ch_checkrange(ch, cge.cge_type, cge.cge_unit))
791 			return -EINVAL;
792 		elem = ch->firsts[cge.cge_type] + cge.cge_unit;
793 
794 		buffer = kmalloc(512, GFP_KERNEL);
795 		if (!buffer)
796 			return -ENOMEM;
797 		mutex_lock(&ch->lock);
798 
799 	voltag_retry:
800 		memset(ch_cmd, 0, sizeof(ch_cmd));
801 		ch_cmd[0] = READ_ELEMENT_STATUS;
802 		ch_cmd[1] = ((ch->device->lun & 0x7) << 5) |
803 			(ch->voltags ? 0x10 : 0) |
804 			ch_elem_to_typecode(ch,elem);
805 		ch_cmd[2] = (elem >> 8) & 0xff;
806 		ch_cmd[3] = elem        & 0xff;
807 		ch_cmd[5] = 1;
808 		ch_cmd[9] = 255;
809 
810 		result = ch_do_scsi(ch, ch_cmd, 12, buffer, 256, REQ_OP_DRV_IN);
811 		if (!result) {
812 			cge.cge_status = buffer[18];
813 			cge.cge_flags = 0;
814 			if (buffer[18] & CESTATUS_EXCEPT) {
815 				cge.cge_errno = EIO;
816 			}
817 			if (buffer[25] & 0x80) {
818 				cge.cge_flags |= CGE_SRC;
819 				if (buffer[25] & 0x40)
820 					cge.cge_flags |= CGE_INVERT;
821 				elem = (buffer[26]<<8) | buffer[27];
822 				for (i = 0; i < 4; i++) {
823 					if (elem >= ch->firsts[i] &&
824 					    elem <  ch->firsts[i] + ch->counts[i]) {
825 						cge.cge_srctype = i;
826 						cge.cge_srcunit = elem-ch->firsts[i];
827 					}
828 				}
829 			}
830 			if ((buffer[22] & 0x30) == 0x30) {
831 				cge.cge_flags |= CGE_IDLUN;
832 				cge.cge_id  = buffer[23];
833 				cge.cge_lun = buffer[22] & 7;
834 			}
835 			if (buffer[9] & 0x80) {
836 				cge.cge_flags |= CGE_PVOLTAG;
837 				memcpy(cge.cge_pvoltag,buffer+28,36);
838 			}
839 			if (buffer[9] & 0x40) {
840 				cge.cge_flags |= CGE_AVOLTAG;
841 				memcpy(cge.cge_avoltag,buffer+64,36);
842 			}
843 		} else if (ch->voltags) {
844 			ch->voltags = 0;
845 			VPRINTK(KERN_INFO, "device has no volume tag support\n");
846 			goto voltag_retry;
847 		}
848 		kfree(buffer);
849 		mutex_unlock(&ch->lock);
850 
851 		if (copy_to_user(argp, &cge, sizeof (cge)))
852 			return -EFAULT;
853 		return result;
854 	}
855 
856 	case CHIOINITELEM:
857 	{
858 		mutex_lock(&ch->lock);
859 		retval = ch_init_elem(ch);
860 		mutex_unlock(&ch->lock);
861 		return retval;
862 	}
863 
864 	case CHIOSVOLTAG:
865 	{
866 		struct changer_set_voltag csv;
867 		int elem;
868 
869 		if (copy_from_user(&csv, argp, sizeof(csv)))
870 			return -EFAULT;
871 
872 		if (0 != ch_checkrange(ch, csv.csv_type, csv.csv_unit)) {
873 			DPRINTK("CHIOSVOLTAG: invalid parameter\n");
874 			return -EBADSLT;
875 		}
876 		elem = ch->firsts[csv.csv_type] + csv.csv_unit;
877 		mutex_lock(&ch->lock);
878 		retval = ch_set_voltag(ch, elem,
879 				       csv.csv_flags & CSV_AVOLTAG,
880 				       csv.csv_flags & CSV_CLEARTAG,
881 				       csv.csv_voltag);
882 		mutex_unlock(&ch->lock);
883 		return retval;
884 	}
885 
886 	default:
887 		return scsi_ioctl(ch->device, file->f_mode & FMODE_WRITE, cmd,
888 				  argp);
889 
890 	}
891 }
892 
893 /* ------------------------------------------------------------------------ */
894 
895 static int ch_probe(struct device *dev)
896 {
897 	struct scsi_device *sd = to_scsi_device(dev);
898 	struct device *class_dev;
899 	int ret;
900 	scsi_changer *ch;
901 
902 	if (sd->type != TYPE_MEDIUM_CHANGER)
903 		return -ENODEV;
904 
905 	ch = kzalloc(sizeof(*ch), GFP_KERNEL);
906 	if (NULL == ch)
907 		return -ENOMEM;
908 
909 	idr_preload(GFP_KERNEL);
910 	spin_lock(&ch_index_lock);
911 	ret = idr_alloc(&ch_index_idr, ch, 0, CH_MAX_DEVS + 1, GFP_NOWAIT);
912 	spin_unlock(&ch_index_lock);
913 	idr_preload_end();
914 
915 	if (ret < 0) {
916 		if (ret == -ENOSPC)
917 			ret = -ENODEV;
918 		goto free_ch;
919 	}
920 
921 	ch->minor = ret;
922 	sprintf(ch->name,"ch%d",ch->minor);
923 	ret = scsi_device_get(sd);
924 	if (ret) {
925 		sdev_printk(KERN_WARNING, sd, "ch%d: failed to get device\n",
926 			    ch->minor);
927 		goto remove_idr;
928 	}
929 
930 	mutex_init(&ch->lock);
931 	kref_init(&ch->ref);
932 	ch->device = sd;
933 	class_dev = device_create(ch_sysfs_class, dev,
934 				  MKDEV(SCSI_CHANGER_MAJOR, ch->minor), ch,
935 				  "s%s", ch->name);
936 	if (IS_ERR(class_dev)) {
937 		sdev_printk(KERN_WARNING, sd, "ch%d: device_create failed\n",
938 			    ch->minor);
939 		ret = PTR_ERR(class_dev);
940 		goto put_device;
941 	}
942 
943 	mutex_lock(&ch->lock);
944 	ret = ch_readconfig(ch);
945 	if (ret) {
946 		mutex_unlock(&ch->lock);
947 		goto destroy_dev;
948 	}
949 	if (init)
950 		ch_init_elem(ch);
951 
952 	mutex_unlock(&ch->lock);
953 	dev_set_drvdata(dev, ch);
954 	sdev_printk(KERN_INFO, sd, "Attached scsi changer %s\n", ch->name);
955 
956 	return 0;
957 destroy_dev:
958 	device_destroy(ch_sysfs_class, MKDEV(SCSI_CHANGER_MAJOR, ch->minor));
959 put_device:
960 	scsi_device_put(sd);
961 remove_idr:
962 	idr_remove(&ch_index_idr, ch->minor);
963 free_ch:
964 	kfree(ch);
965 	return ret;
966 }
967 
968 static int ch_remove(struct device *dev)
969 {
970 	scsi_changer *ch = dev_get_drvdata(dev);
971 
972 	spin_lock(&ch_index_lock);
973 	idr_remove(&ch_index_idr, ch->minor);
974 	dev_set_drvdata(dev, NULL);
975 	spin_unlock(&ch_index_lock);
976 
977 	device_destroy(ch_sysfs_class, MKDEV(SCSI_CHANGER_MAJOR,ch->minor));
978 	scsi_device_put(ch->device);
979 	kref_put(&ch->ref, ch_destroy);
980 	return 0;
981 }
982 
983 static struct scsi_driver ch_template = {
984 	.gendrv     	= {
985 		.name	= "ch",
986 		.owner	= THIS_MODULE,
987 		.probe  = ch_probe,
988 		.remove = ch_remove,
989 	},
990 };
991 
992 static const struct file_operations changer_fops = {
993 	.owner		= THIS_MODULE,
994 	.open		= ch_open,
995 	.release	= ch_release,
996 	.unlocked_ioctl	= ch_ioctl,
997 	.compat_ioctl	= compat_ptr_ioctl,
998 	.llseek		= noop_llseek,
999 };
1000 
1001 static int __init init_ch_module(void)
1002 {
1003 	int rc;
1004 
1005 	printk(KERN_INFO "SCSI Media Changer driver v" VERSION " \n");
1006         ch_sysfs_class = class_create("scsi_changer");
1007         if (IS_ERR(ch_sysfs_class)) {
1008 		rc = PTR_ERR(ch_sysfs_class);
1009 		return rc;
1010         }
1011 	rc = register_chrdev(SCSI_CHANGER_MAJOR,"ch",&changer_fops);
1012 	if (rc < 0) {
1013 		printk("Unable to get major %d for SCSI-Changer\n",
1014 		       SCSI_CHANGER_MAJOR);
1015 		goto fail1;
1016 	}
1017 	rc = scsi_register_driver(&ch_template.gendrv);
1018 	if (rc < 0)
1019 		goto fail2;
1020 	return 0;
1021 
1022  fail2:
1023 	unregister_chrdev(SCSI_CHANGER_MAJOR, "ch");
1024  fail1:
1025 	class_destroy(ch_sysfs_class);
1026 	return rc;
1027 }
1028 
1029 static void __exit exit_ch_module(void)
1030 {
1031 	scsi_unregister_driver(&ch_template.gendrv);
1032 	unregister_chrdev(SCSI_CHANGER_MAJOR, "ch");
1033 	class_destroy(ch_sysfs_class);
1034 	idr_destroy(&ch_index_idr);
1035 }
1036 
1037 module_init(init_ch_module);
1038 module_exit(exit_ch_module);
1039