1037 lines · 24.9 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0-only/* * SCSI Media Changer device driver for Linux 2.6 * *     (c) 1996-2003 Gerd Knorr <kraxel@bytesex.org> * */ #define VERSION "0.25" #include <linux/module.h>#include <linux/init.h>#include <linux/fs.h>#include <linux/kernel.h>#include <linux/mm.h>#include <linux/major.h>#include <linux/string.h>#include <linux/errno.h>#include <linux/interrupt.h>#include <linux/blkdev.h>#include <linux/completion.h>#include <linux/compat.h>#include <linux/chio.h>			/* here are all the ioctls */#include <linux/mutex.h>#include <linux/idr.h>#include <linux/slab.h> #include <scsi/scsi.h>#include <scsi/scsi_cmnd.h>#include <scsi/scsi_driver.h>#include <scsi/scsi_ioctl.h>#include <scsi/scsi_host.h>#include <scsi/scsi_device.h>#include <scsi/scsi_eh.h>#include <scsi/scsi_dbg.h> #define CH_DT_MAX       16#define CH_TYPES        8#define CH_MAX_DEVS     128 MODULE_DESCRIPTION("device driver for scsi media changer devices");MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org>");MODULE_LICENSE("GPL");MODULE_ALIAS_CHARDEV_MAJOR(SCSI_CHANGER_MAJOR);MODULE_ALIAS_SCSI_DEVICE(TYPE_MEDIUM_CHANGER); static int init = 1;module_param(init, int, 0444);MODULE_PARM_DESC(init, \    "initialize element status on driver load (default: on)"); static int timeout_move = 300;module_param(timeout_move, int, 0644);MODULE_PARM_DESC(timeout_move,"timeout for move commands "		 "(default: 300 seconds)"); static int timeout_init = 3600;module_param(timeout_init, int, 0644);MODULE_PARM_DESC(timeout_init,"timeout for INITIALIZE ELEMENT STATUS "		 "(default: 3600 seconds)"); static int verbose = 1;module_param(verbose, int, 0644);MODULE_PARM_DESC(verbose,"be verbose (default: on)"); static int debug;module_param(debug, int, 0644);MODULE_PARM_DESC(debug,"enable/disable debug messages, also prints more "		 "detailed sense codes on scsi errors (default: off)"); static int dt_id[CH_DT_MAX] = { [ 0 ... (CH_DT_MAX-1) ] = -1 };static int dt_lun[CH_DT_MAX];module_param_array(dt_id,  int, NULL, 0444);module_param_array(dt_lun, int, NULL, 0444); /* tell the driver about vendor-specific slots */static int vendor_firsts[CH_TYPES-4];static int vendor_counts[CH_TYPES-4];module_param_array(vendor_firsts, int, NULL, 0444);module_param_array(vendor_counts, int, NULL, 0444); static const char * vendor_labels[CH_TYPES-4] = {	"v0", "v1", "v2", "v3"};// module_param_string_array(vendor_labels, NULL, 0444); #define ch_printk(prefix, ch, fmt, a...) \	sdev_prefix_printk(prefix, (ch)->device, (ch)->name, fmt, ##a) #define DPRINTK(fmt, arg...)						\do {									\	if (debug)							\		ch_printk(KERN_DEBUG, ch, fmt, ##arg);			\} while (0)#define VPRINTK(level, fmt, arg...)					\do {									\	if (verbose)							\		ch_printk(level, ch, fmt, ##arg);			\} while (0) /* ------------------------------------------------------------------- */ #define MAX_RETRIES   1 static const struct class ch_sysfs_class = {	.name = "scsi_changer",}; typedef struct {	struct kref         ref;	struct list_head    list;	int                 minor;	char                name[8];	struct scsi_device  *device;	struct scsi_device  **dt;        /* ptrs to data transfer elements */	u_int               firsts[CH_TYPES];	u_int               counts[CH_TYPES];	u_int		    voltags;	struct mutex	    lock;} scsi_changer; static DEFINE_IDR(ch_index_idr);static DEFINE_SPINLOCK(ch_index_lock); static const struct {	unsigned char  sense;	unsigned char  asc;	unsigned char  ascq;	int	       errno;} ch_err[] = {/* Just filled in what looks right. Hav'nt checked any standard paper for   these errno assignments, so they may be wrong... */	{		.sense  = ILLEGAL_REQUEST,		.asc    = 0x21,		.ascq   = 0x01,		.errno  = EBADSLT, /* Invalid element address */	},{		.sense  = ILLEGAL_REQUEST,		.asc    = 0x28,		.ascq   = 0x01,		.errno  = EBADE,   /* Import or export element accessed */	},{		.sense  = ILLEGAL_REQUEST,		.asc    = 0x3B,		.ascq   = 0x0D,		.errno  = EXFULL,  /* Medium destination element full */	},{		.sense  = ILLEGAL_REQUEST,		.asc    = 0x3B,		.ascq   = 0x0E,		.errno  = EBADE,   /* Medium source element empty */	},{		.sense  = ILLEGAL_REQUEST,		.asc    = 0x20,		.ascq   = 0x00,		.errno  = EBADRQC, /* Invalid command operation code */	},{	        /* end of list */	}}; /* ------------------------------------------------------------------- */ static int ch_find_errno(struct scsi_sense_hdr *sshdr){	int i,errno = 0; 	/* Check to see if additional sense information is available */	if (scsi_sense_valid(sshdr) &&	    sshdr->asc != 0) {		for (i = 0; ch_err[i].errno != 0; i++) {			if (ch_err[i].sense == sshdr->sense_key &&			    ch_err[i].asc   == sshdr->asc &&			    ch_err[i].ascq  == sshdr->ascq) {				errno = -ch_err[i].errno;				break;			}		}	}	if (errno == 0)		errno = -EIO;	return errno;} static intch_do_scsi(scsi_changer *ch, unsigned char *cmd, int cmd_len,	   void *buffer, unsigned int buflength, enum req_op op){	int errno = 0, timeout, result;	struct scsi_sense_hdr sshdr;	struct scsi_failure failure_defs[] = {		{			.sense = UNIT_ATTENTION,			.asc = SCMD_FAILURE_ASC_ANY,			.ascq = SCMD_FAILURE_ASCQ_ANY,			.allowed = 3,			.result = SAM_STAT_CHECK_CONDITION,		},		{}	};	struct scsi_failures failures = {		.failure_definitions = failure_defs,	};	const struct scsi_exec_args exec_args = {		.sshdr = &sshdr,		.failures = &failures,	}; 	timeout = (cmd[0] == INITIALIZE_ELEMENT_STATUS)		? timeout_init : timeout_move; 	result = scsi_execute_cmd(ch->device, cmd, op, buffer, buflength,				  timeout * HZ, MAX_RETRIES, &exec_args);	if (result < 0)		return result;	if (scsi_sense_valid(&sshdr)) {		if (debug)			scsi_print_sense_hdr(ch->device, ch->name, &sshdr);		errno = ch_find_errno(&sshdr);	}	return errno;} /* ------------------------------------------------------------------------ */ static intch_elem_to_typecode(scsi_changer *ch, u_int elem){	int i; 	for (i = 0; i < CH_TYPES; i++) {		if (elem >= ch->firsts[i]  &&		    elem <  ch->firsts[i] +	            ch->counts[i])			return i+1;	}	return 0;} static intch_read_element_status(scsi_changer *ch, u_int elem, char *data){	u_char  cmd[12];	u_char  *buffer;	int     result; 	buffer = kmalloc(512, GFP_KERNEL);	if(!buffer)		return -ENOMEM;  retry:	memset(cmd,0,sizeof(cmd));	cmd[0] = READ_ELEMENT_STATUS;	cmd[1] = ((ch->device->lun & 0x7) << 5) |		(ch->voltags ? 0x10 : 0) |		ch_elem_to_typecode(ch,elem);	cmd[2] = (elem >> 8) & 0xff;	cmd[3] = elem        & 0xff;	cmd[5] = 1;	cmd[9] = 255;	if (0 == (result = ch_do_scsi(ch, cmd, 12,				      buffer, 256, REQ_OP_DRV_IN))) {		if (((buffer[16] << 8) | buffer[17]) != elem) {			DPRINTK("asked for element 0x%02x, got 0x%02x\n",				elem,(buffer[16] << 8) | buffer[17]);			kfree(buffer);			return -EIO;		}		memcpy(data,buffer+16,16);	} else {		if (ch->voltags) {			ch->voltags = 0;			VPRINTK(KERN_INFO, "device has no volume tag support\n");			goto retry;		}		DPRINTK("READ ELEMENT STATUS for element 0x%x failed\n",elem);	}	kfree(buffer);	return result;} static intch_init_elem(scsi_changer *ch){	int err;	u_char cmd[6]; 	VPRINTK(KERN_INFO, "INITIALIZE ELEMENT STATUS, may take some time ...\n");	memset(cmd,0,sizeof(cmd));	cmd[0] = INITIALIZE_ELEMENT_STATUS;	cmd[1] = (ch->device->lun & 0x7) << 5;	err = ch_do_scsi(ch, cmd, 6, NULL, 0, REQ_OP_DRV_IN);	VPRINTK(KERN_INFO, "... finished\n");	return err;} static intch_readconfig(scsi_changer *ch){	u_char  cmd[10], data[16];	u_char  *buffer;	int     result,id,lun,i;	u_int   elem; 	buffer = kzalloc(512, GFP_KERNEL);	if (!buffer)		return -ENOMEM; 	memset(cmd,0,sizeof(cmd));	cmd[0] = MODE_SENSE;	cmd[1] = (ch->device->lun & 0x7) << 5;	cmd[2] = 0x1d;	cmd[4] = 255;	result = ch_do_scsi(ch, cmd, 10, buffer, 255, REQ_OP_DRV_IN);	if (0 != result) {		cmd[1] |= (1<<3);		result  = ch_do_scsi(ch, cmd, 10, buffer, 255, REQ_OP_DRV_IN);	}	if (0 == result) {		ch->firsts[CHET_MT] =			(buffer[buffer[3]+ 6] << 8) | buffer[buffer[3]+ 7];		ch->counts[CHET_MT] =			(buffer[buffer[3]+ 8] << 8) | buffer[buffer[3]+ 9];		ch->firsts[CHET_ST] =			(buffer[buffer[3]+10] << 8) | buffer[buffer[3]+11];		ch->counts[CHET_ST] =			(buffer[buffer[3]+12] << 8) | buffer[buffer[3]+13];		ch->firsts[CHET_IE] =			(buffer[buffer[3]+14] << 8) | buffer[buffer[3]+15];		ch->counts[CHET_IE] =			(buffer[buffer[3]+16] << 8) | buffer[buffer[3]+17];		ch->firsts[CHET_DT] =			(buffer[buffer[3]+18] << 8) | buffer[buffer[3]+19];		ch->counts[CHET_DT] =			(buffer[buffer[3]+20] << 8) | buffer[buffer[3]+21];		VPRINTK(KERN_INFO, "type #1 (mt): 0x%x+%d [medium transport]\n",			ch->firsts[CHET_MT],			ch->counts[CHET_MT]);		VPRINTK(KERN_INFO, "type #2 (st): 0x%x+%d [storage]\n",			ch->firsts[CHET_ST],			ch->counts[CHET_ST]);		VPRINTK(KERN_INFO, "type #3 (ie): 0x%x+%d [import/export]\n",			ch->firsts[CHET_IE],			ch->counts[CHET_IE]);		VPRINTK(KERN_INFO, "type #4 (dt): 0x%x+%d [data transfer]\n",			ch->firsts[CHET_DT],			ch->counts[CHET_DT]);	} else {		VPRINTK(KERN_INFO, "reading element address assignment page failed!\n");	} 	/* vendor specific element types */	for (i = 0; i < 4; i++) {		if (0 == vendor_counts[i])			continue;		if (NULL == vendor_labels[i])			continue;		ch->firsts[CHET_V1+i] = vendor_firsts[i];		ch->counts[CHET_V1+i] = vendor_counts[i];		VPRINTK(KERN_INFO, "type #%d (v%d): 0x%x+%d [%s, vendor specific]\n",			i+5,i+1,vendor_firsts[i],vendor_counts[i],			vendor_labels[i]);	} 	/* look up the devices of the data transfer elements */	ch->dt = kzalloc_objs(*ch->dt, ch->counts[CHET_DT]); 	if (!ch->dt) {		kfree(buffer);		return -ENOMEM;	} 	for (elem = 0; elem < ch->counts[CHET_DT]; elem++) {		id  = -1;		lun = 0;		if (elem < CH_DT_MAX  &&  -1 != dt_id[elem]) {			id  = dt_id[elem];			lun = dt_lun[elem];			VPRINTK(KERN_INFO, "dt 0x%x: [insmod option] ",				elem+ch->firsts[CHET_DT]);		} else if (0 != ch_read_element_status			   (ch,elem+ch->firsts[CHET_DT],data)) {			VPRINTK(KERN_INFO, "dt 0x%x: READ ELEMENT STATUS failed\n",				elem+ch->firsts[CHET_DT]);		} else {			VPRINTK(KERN_INFO, "dt 0x%x: ",elem+ch->firsts[CHET_DT]);			if (data[6] & 0x80) {				VPRINTK(KERN_CONT, "not this SCSI bus\n");				ch->dt[elem] = NULL;			} else if (0 == (data[6] & 0x30)) {				VPRINTK(KERN_CONT, "ID/LUN unknown\n");				ch->dt[elem] = NULL;			} else {				id  = ch->device->id;				lun = 0;				if (data[6] & 0x20) id  = data[7];				if (data[6] & 0x10) lun = data[6] & 7;			}		}		if (-1 != id) {			VPRINTK(KERN_CONT, "ID %i, LUN %i, ",id,lun);			ch->dt[elem] =				scsi_device_lookup(ch->device->host,						   ch->device->channel,						   id,lun);			if (!ch->dt[elem]) {				/* should not happen */				VPRINTK(KERN_CONT, "Huh? device not found!\n");			} else {				VPRINTK(KERN_CONT, "name: %8.8s %16.16s %4.4s\n",					ch->dt[elem]->vendor,					ch->dt[elem]->model,					ch->dt[elem]->rev);			}		}	}	ch->voltags = 1;	kfree(buffer); 	return 0;} /* ------------------------------------------------------------------------ */ static intch_position(scsi_changer *ch, u_int trans, u_int elem, int rotate){	u_char  cmd[10]; 	DPRINTK("position: 0x%x\n",elem);	if (0 == trans)		trans = ch->firsts[CHET_MT];	memset(cmd,0,sizeof(cmd));	cmd[0]  = POSITION_TO_ELEMENT;	cmd[1]  = (ch->device->lun & 0x7) << 5;	cmd[2]  = (trans >> 8) & 0xff;	cmd[3]  =  trans       & 0xff;	cmd[4]  = (elem  >> 8) & 0xff;	cmd[5]  =  elem        & 0xff;	cmd[8]  = rotate ? 1 : 0;	return ch_do_scsi(ch, cmd, 10, NULL, 0, REQ_OP_DRV_IN);} static intch_move(scsi_changer *ch, u_int trans, u_int src, u_int dest, int rotate){	u_char  cmd[12]; 	DPRINTK("move: 0x%x => 0x%x\n",src,dest);	if (0 == trans)		trans = ch->firsts[CHET_MT];	memset(cmd,0,sizeof(cmd));	cmd[0]  = MOVE_MEDIUM;	cmd[1]  = (ch->device->lun & 0x7) << 5;	cmd[2]  = (trans >> 8) & 0xff;	cmd[3]  =  trans       & 0xff;	cmd[4]  = (src   >> 8) & 0xff;	cmd[5]  =  src         & 0xff;	cmd[6]  = (dest  >> 8) & 0xff;	cmd[7]  =  dest        & 0xff;	cmd[10] = rotate ? 1 : 0;	return ch_do_scsi(ch, cmd, 12, NULL, 0, REQ_OP_DRV_IN);} static intch_exchange(scsi_changer *ch, u_int trans, u_int src,	    u_int dest1, u_int dest2, int rotate1, int rotate2){	u_char  cmd[12]; 	DPRINTK("exchange: 0x%x => 0x%x => 0x%x\n",		src,dest1,dest2);	if (0 == trans)		trans = ch->firsts[CHET_MT];	memset(cmd,0,sizeof(cmd));	cmd[0]  = EXCHANGE_MEDIUM;	cmd[1]  = (ch->device->lun & 0x7) << 5;	cmd[2]  = (trans >> 8) & 0xff;	cmd[3]  =  trans       & 0xff;	cmd[4]  = (src   >> 8) & 0xff;	cmd[5]  =  src         & 0xff;	cmd[6]  = (dest1 >> 8) & 0xff;	cmd[7]  =  dest1       & 0xff;	cmd[8]  = (dest2 >> 8) & 0xff;	cmd[9]  =  dest2       & 0xff;	cmd[10] = (rotate1 ? 1 : 0) | (rotate2 ? 2 : 0); 	return ch_do_scsi(ch, cmd, 12, NULL, 0, REQ_OP_DRV_IN);} static voidch_check_voltag(char *tag){	int i; 	for (i = 0; i < 32; i++) {		/* restrict to ascii */		if (tag[i] >= 0x7f || tag[i] < 0x20)			tag[i] = ' ';

Lines 1–500 of 1037.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t