1daa6eda6SGerd Knorr /* 2daa6eda6SGerd Knorr * SCSI Media Changer device driver for Linux 2.6 3daa6eda6SGerd Knorr * 4daa6eda6SGerd Knorr * (c) 1996-2003 Gerd Knorr <kraxel@bytesex.org> 5daa6eda6SGerd Knorr * 6daa6eda6SGerd Knorr */ 7daa6eda6SGerd Knorr 8daa6eda6SGerd Knorr #define VERSION "0.25" 9daa6eda6SGerd Knorr 10daa6eda6SGerd Knorr #include <linux/module.h> 11daa6eda6SGerd Knorr #include <linux/init.h> 12daa6eda6SGerd Knorr #include <linux/fs.h> 13daa6eda6SGerd Knorr #include <linux/kernel.h> 14daa6eda6SGerd Knorr #include <linux/mm.h> 15daa6eda6SGerd Knorr #include <linux/major.h> 16daa6eda6SGerd Knorr #include <linux/string.h> 17daa6eda6SGerd Knorr #include <linux/errno.h> 18daa6eda6SGerd Knorr #include <linux/interrupt.h> 19daa6eda6SGerd Knorr #include <linux/blkdev.h> 20daa6eda6SGerd Knorr #include <linux/completion.h> 21daa6eda6SGerd Knorr #include <linux/compat.h> 22daa6eda6SGerd Knorr #include <linux/chio.h> /* here are all the ioctls */ 230b950672SArjan van de Ven #include <linux/mutex.h> 24da707c54SFUJITA Tomonori #include <linux/idr.h> 255a0e3ad6STejun Heo #include <linux/slab.h> 26daa6eda6SGerd Knorr 27daa6eda6SGerd Knorr #include <scsi/scsi.h> 28daa6eda6SGerd Knorr #include <scsi/scsi_cmnd.h> 29daa6eda6SGerd Knorr #include <scsi/scsi_driver.h> 30daa6eda6SGerd Knorr #include <scsi/scsi_ioctl.h> 31daa6eda6SGerd Knorr #include <scsi/scsi_host.h> 32daa6eda6SGerd Knorr #include <scsi/scsi_device.h> 3384743bbcSJames Bottomley #include <scsi/scsi_eh.h> 34daa6eda6SGerd Knorr #include <scsi/scsi_dbg.h> 35daa6eda6SGerd Knorr 36daa6eda6SGerd Knorr #define CH_DT_MAX 16 37daa6eda6SGerd Knorr #define CH_TYPES 8 38da707c54SFUJITA Tomonori #define CH_MAX_DEVS 128 39daa6eda6SGerd Knorr 40daa6eda6SGerd Knorr MODULE_DESCRIPTION("device driver for scsi media changer devices"); 41daa6eda6SGerd Knorr MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org>"); 42daa6eda6SGerd Knorr MODULE_LICENSE("GPL"); 43f018fa55SRene Herman MODULE_ALIAS_CHARDEV_MAJOR(SCSI_CHANGER_MAJOR); 4495f6c83fSScott James Remnant MODULE_ALIAS_SCSI_DEVICE(TYPE_MEDIUM_CHANGER); 45daa6eda6SGerd Knorr 46c45d15d2SArnd Bergmann static DEFINE_MUTEX(ch_mutex); 47daa6eda6SGerd Knorr static int init = 1; 48daa6eda6SGerd Knorr module_param(init, int, 0444); 49daa6eda6SGerd Knorr MODULE_PARM_DESC(init, \ 50daa6eda6SGerd Knorr "initialize element status on driver load (default: on)"); 51daa6eda6SGerd Knorr 52daa6eda6SGerd Knorr static int timeout_move = 300; 53daa6eda6SGerd Knorr module_param(timeout_move, int, 0644); 54daa6eda6SGerd Knorr MODULE_PARM_DESC(timeout_move,"timeout for move commands " 55daa6eda6SGerd Knorr "(default: 300 seconds)"); 56daa6eda6SGerd Knorr 57daa6eda6SGerd Knorr static int timeout_init = 3600; 58daa6eda6SGerd Knorr module_param(timeout_init, int, 0644); 59daa6eda6SGerd Knorr MODULE_PARM_DESC(timeout_init,"timeout for INITIALIZE ELEMENT STATUS " 60daa6eda6SGerd Knorr "(default: 3600 seconds)"); 61daa6eda6SGerd Knorr 62daa6eda6SGerd Knorr static int verbose = 1; 63daa6eda6SGerd Knorr module_param(verbose, int, 0644); 64daa6eda6SGerd Knorr MODULE_PARM_DESC(verbose,"be verbose (default: on)"); 65daa6eda6SGerd Knorr 66daa6eda6SGerd Knorr static int debug = 0; 67daa6eda6SGerd Knorr module_param(debug, int, 0644); 68daa6eda6SGerd Knorr MODULE_PARM_DESC(debug,"enable/disable debug messages, also prints more " 69daa6eda6SGerd Knorr "detailed sense codes on scsi errors (default: off)"); 70daa6eda6SGerd Knorr 71daa6eda6SGerd Knorr static int dt_id[CH_DT_MAX] = { [ 0 ... (CH_DT_MAX-1) ] = -1 }; 72daa6eda6SGerd Knorr static int dt_lun[CH_DT_MAX]; 73daa6eda6SGerd Knorr module_param_array(dt_id, int, NULL, 0444); 74daa6eda6SGerd Knorr module_param_array(dt_lun, int, NULL, 0444); 75daa6eda6SGerd Knorr 76daa6eda6SGerd Knorr /* tell the driver about vendor-specific slots */ 77daa6eda6SGerd Knorr static int vendor_firsts[CH_TYPES-4]; 78daa6eda6SGerd Knorr static int vendor_counts[CH_TYPES-4]; 79daa6eda6SGerd Knorr module_param_array(vendor_firsts, int, NULL, 0444); 80daa6eda6SGerd Knorr module_param_array(vendor_counts, int, NULL, 0444); 81daa6eda6SGerd Knorr 820ad78200SArjan van de Ven static const char * vendor_labels[CH_TYPES-4] = { 83daa6eda6SGerd Knorr "v0", "v1", "v2", "v3" 84daa6eda6SGerd Knorr }; 85daa6eda6SGerd Knorr // module_param_string_array(vendor_labels, NULL, 0444); 86daa6eda6SGerd Knorr 8728c31729SHannes Reinecke #define ch_printk(prefix, ch, fmt, a...) \ 8822e0d994SHannes Reinecke sdev_prefix_printk(prefix, (ch)->device, (ch)->name, fmt, ##a) 8928c31729SHannes Reinecke 9087da3235SJoe Perches #define DPRINTK(fmt, arg...) \ 9187da3235SJoe Perches do { \ 9287da3235SJoe Perches if (debug) \ 9328c31729SHannes Reinecke ch_printk(KERN_DEBUG, ch, fmt, ##arg); \ 9487da3235SJoe Perches } while (0) 9587da3235SJoe Perches #define VPRINTK(level, fmt, arg...) \ 9687da3235SJoe Perches do { \ 9787da3235SJoe Perches if (verbose) \ 9828c31729SHannes Reinecke ch_printk(level, ch, fmt, ##arg); \ 9987da3235SJoe Perches } while (0) 100daa6eda6SGerd Knorr 101daa6eda6SGerd Knorr /* ------------------------------------------------------------------- */ 102daa6eda6SGerd Knorr 103daa6eda6SGerd Knorr #define MAX_RETRIES 1 104daa6eda6SGerd Knorr 10521feb5ccSGerd Knorr static struct class * ch_sysfs_class; 106daa6eda6SGerd Knorr 107daa6eda6SGerd Knorr typedef struct { 108085e5676SHannes Reinecke struct kref ref; 109daa6eda6SGerd Knorr struct list_head list; 110daa6eda6SGerd Knorr int minor; 111daa6eda6SGerd Knorr char name[8]; 112daa6eda6SGerd Knorr struct scsi_device *device; 113daa6eda6SGerd Knorr struct scsi_device **dt; /* ptrs to data transfer elements */ 114daa6eda6SGerd Knorr u_int firsts[CH_TYPES]; 115daa6eda6SGerd Knorr u_int counts[CH_TYPES]; 116daa6eda6SGerd Knorr u_int unit_attention; 117daa6eda6SGerd Knorr u_int voltags; 1180b950672SArjan van de Ven struct mutex lock; 119daa6eda6SGerd Knorr } scsi_changer; 120daa6eda6SGerd Knorr 121da707c54SFUJITA Tomonori static DEFINE_IDR(ch_index_idr); 122da707c54SFUJITA Tomonori static DEFINE_SPINLOCK(ch_index_lock); 123daa6eda6SGerd Knorr 1240ad78200SArjan van de Ven static const struct { 125daa6eda6SGerd Knorr unsigned char sense; 126daa6eda6SGerd Knorr unsigned char asc; 127daa6eda6SGerd Knorr unsigned char ascq; 128daa6eda6SGerd Knorr int errno; 129d70d4667SHarvey Harrison } ch_err[] = { 130daa6eda6SGerd Knorr /* Just filled in what looks right. Hav'nt checked any standard paper for 131daa6eda6SGerd Knorr these errno assignments, so they may be wrong... */ 132daa6eda6SGerd Knorr { 133daa6eda6SGerd Knorr .sense = ILLEGAL_REQUEST, 134daa6eda6SGerd Knorr .asc = 0x21, 135daa6eda6SGerd Knorr .ascq = 0x01, 136daa6eda6SGerd Knorr .errno = EBADSLT, /* Invalid element address */ 137daa6eda6SGerd Knorr },{ 138daa6eda6SGerd Knorr .sense = ILLEGAL_REQUEST, 139daa6eda6SGerd Knorr .asc = 0x28, 140daa6eda6SGerd Knorr .ascq = 0x01, 141daa6eda6SGerd Knorr .errno = EBADE, /* Import or export element accessed */ 142daa6eda6SGerd Knorr },{ 143daa6eda6SGerd Knorr .sense = ILLEGAL_REQUEST, 144daa6eda6SGerd Knorr .asc = 0x3B, 145daa6eda6SGerd Knorr .ascq = 0x0D, 146daa6eda6SGerd Knorr .errno = EXFULL, /* Medium destination element full */ 147daa6eda6SGerd Knorr },{ 148daa6eda6SGerd Knorr .sense = ILLEGAL_REQUEST, 149daa6eda6SGerd Knorr .asc = 0x3B, 150daa6eda6SGerd Knorr .ascq = 0x0E, 151daa6eda6SGerd Knorr .errno = EBADE, /* Medium source element empty */ 152daa6eda6SGerd Knorr },{ 153daa6eda6SGerd Knorr .sense = ILLEGAL_REQUEST, 154daa6eda6SGerd Knorr .asc = 0x20, 155daa6eda6SGerd Knorr .ascq = 0x00, 156daa6eda6SGerd Knorr .errno = EBADRQC, /* Invalid command operation code */ 157daa6eda6SGerd Knorr },{ 158daa6eda6SGerd Knorr /* end of list */ 159daa6eda6SGerd Knorr } 160daa6eda6SGerd Knorr }; 161daa6eda6SGerd Knorr 162daa6eda6SGerd Knorr /* ------------------------------------------------------------------- */ 163daa6eda6SGerd Knorr 16484743bbcSJames Bottomley static int ch_find_errno(struct scsi_sense_hdr *sshdr) 165daa6eda6SGerd Knorr { 166daa6eda6SGerd Knorr int i,errno = 0; 167daa6eda6SGerd Knorr 168daa6eda6SGerd Knorr /* Check to see if additional sense information is available */ 16984743bbcSJames Bottomley if (scsi_sense_valid(sshdr) && 17084743bbcSJames Bottomley sshdr->asc != 0) { 171d70d4667SHarvey Harrison for (i = 0; ch_err[i].errno != 0; i++) { 172d70d4667SHarvey Harrison if (ch_err[i].sense == sshdr->sense_key && 173d70d4667SHarvey Harrison ch_err[i].asc == sshdr->asc && 174d70d4667SHarvey Harrison ch_err[i].ascq == sshdr->ascq) { 175d70d4667SHarvey Harrison errno = -ch_err[i].errno; 176daa6eda6SGerd Knorr break; 177daa6eda6SGerd Knorr } 178daa6eda6SGerd Knorr } 179daa6eda6SGerd Knorr } 180daa6eda6SGerd Knorr if (errno == 0) 181daa6eda6SGerd Knorr errno = -EIO; 182daa6eda6SGerd Knorr return errno; 183daa6eda6SGerd Knorr } 184daa6eda6SGerd Knorr 185daa6eda6SGerd Knorr static int 186a9a47bf5SHannes Reinecke ch_do_scsi(scsi_changer *ch, unsigned char *cmd, int cmd_len, 187daa6eda6SGerd Knorr void *buffer, unsigned buflength, 188daa6eda6SGerd Knorr enum dma_data_direction direction) 189daa6eda6SGerd Knorr { 19084743bbcSJames Bottomley int errno, retries = 0, timeout, result; 19184743bbcSJames Bottomley struct scsi_sense_hdr sshdr; 192daa6eda6SGerd Knorr 193daa6eda6SGerd Knorr timeout = (cmd[0] == INITIALIZE_ELEMENT_STATUS) 194daa6eda6SGerd Knorr ? timeout_init : timeout_move; 195daa6eda6SGerd Knorr 196daa6eda6SGerd Knorr retry: 197daa6eda6SGerd Knorr errno = 0; 19884743bbcSJames Bottomley result = scsi_execute_req(ch->device, cmd, direction, buffer, 19984743bbcSJames Bottomley buflength, &sshdr, timeout * HZ, 200f4f4e47eSFUJITA Tomonori MAX_RETRIES, NULL); 201daa6eda6SGerd Knorr 202c65be1a6SJohannes Thumshirn if (driver_byte(result) == DRIVER_SENSE) { 203daa6eda6SGerd Knorr if (debug) 204d811b848SHannes Reinecke scsi_print_sense_hdr(ch->device, ch->name, &sshdr); 20584743bbcSJames Bottomley errno = ch_find_errno(&sshdr); 206daa6eda6SGerd Knorr 20784743bbcSJames Bottomley switch(sshdr.sense_key) { 208daa6eda6SGerd Knorr case UNIT_ATTENTION: 209daa6eda6SGerd Knorr ch->unit_attention = 1; 210daa6eda6SGerd Knorr if (retries++ < 3) 211daa6eda6SGerd Knorr goto retry; 212daa6eda6SGerd Knorr break; 213daa6eda6SGerd Knorr } 214daa6eda6SGerd Knorr } 215daa6eda6SGerd Knorr return errno; 216daa6eda6SGerd Knorr } 217daa6eda6SGerd Knorr 218daa6eda6SGerd Knorr /* ------------------------------------------------------------------------ */ 219daa6eda6SGerd Knorr 220daa6eda6SGerd Knorr static int 221daa6eda6SGerd Knorr ch_elem_to_typecode(scsi_changer *ch, u_int elem) 222daa6eda6SGerd Knorr { 223daa6eda6SGerd Knorr int i; 224daa6eda6SGerd Knorr 225daa6eda6SGerd Knorr for (i = 0; i < CH_TYPES; i++) { 226daa6eda6SGerd Knorr if (elem >= ch->firsts[i] && 227daa6eda6SGerd Knorr elem < ch->firsts[i] + 228daa6eda6SGerd Knorr ch->counts[i]) 229daa6eda6SGerd Knorr return i+1; 230daa6eda6SGerd Knorr } 231daa6eda6SGerd Knorr return 0; 232daa6eda6SGerd Knorr } 233daa6eda6SGerd Knorr 234daa6eda6SGerd Knorr static int 235daa6eda6SGerd Knorr ch_read_element_status(scsi_changer *ch, u_int elem, char *data) 236daa6eda6SGerd Knorr { 237daa6eda6SGerd Knorr u_char cmd[12]; 238daa6eda6SGerd Knorr u_char *buffer; 239daa6eda6SGerd Knorr int result; 240daa6eda6SGerd Knorr 241daa6eda6SGerd Knorr buffer = kmalloc(512, GFP_KERNEL | GFP_DMA); 242daa6eda6SGerd Knorr if(!buffer) 243daa6eda6SGerd Knorr return -ENOMEM; 244daa6eda6SGerd Knorr 245daa6eda6SGerd Knorr retry: 246daa6eda6SGerd Knorr memset(cmd,0,sizeof(cmd)); 247daa6eda6SGerd Knorr cmd[0] = READ_ELEMENT_STATUS; 2489cb78c16SHannes Reinecke cmd[1] = ((ch->device->lun & 0x7) << 5) | 249daa6eda6SGerd Knorr (ch->voltags ? 0x10 : 0) | 250daa6eda6SGerd Knorr ch_elem_to_typecode(ch,elem); 251daa6eda6SGerd Knorr cmd[2] = (elem >> 8) & 0xff; 252daa6eda6SGerd Knorr cmd[3] = elem & 0xff; 253daa6eda6SGerd Knorr cmd[5] = 1; 254daa6eda6SGerd Knorr cmd[9] = 255; 255a9a47bf5SHannes Reinecke if (0 == (result = ch_do_scsi(ch, cmd, 12, 256a9a47bf5SHannes Reinecke buffer, 256, DMA_FROM_DEVICE))) { 257daa6eda6SGerd Knorr if (((buffer[16] << 8) | buffer[17]) != elem) { 25887da3235SJoe Perches DPRINTK("asked for element 0x%02x, got 0x%02x\n", 259daa6eda6SGerd Knorr elem,(buffer[16] << 8) | buffer[17]); 260daa6eda6SGerd Knorr kfree(buffer); 261daa6eda6SGerd Knorr return -EIO; 262daa6eda6SGerd Knorr } 263daa6eda6SGerd Knorr memcpy(data,buffer+16,16); 264daa6eda6SGerd Knorr } else { 265daa6eda6SGerd Knorr if (ch->voltags) { 266daa6eda6SGerd Knorr ch->voltags = 0; 26787da3235SJoe Perches VPRINTK(KERN_INFO, "device has no volume tag support\n"); 268daa6eda6SGerd Knorr goto retry; 269daa6eda6SGerd Knorr } 27087da3235SJoe Perches DPRINTK("READ ELEMENT STATUS for element 0x%x failed\n",elem); 271daa6eda6SGerd Knorr } 272daa6eda6SGerd Knorr kfree(buffer); 273daa6eda6SGerd Knorr return result; 274daa6eda6SGerd Knorr } 275daa6eda6SGerd Knorr 276daa6eda6SGerd Knorr static int 277daa6eda6SGerd Knorr ch_init_elem(scsi_changer *ch) 278daa6eda6SGerd Knorr { 279daa6eda6SGerd Knorr int err; 280daa6eda6SGerd Knorr u_char cmd[6]; 281daa6eda6SGerd Knorr 28287da3235SJoe Perches VPRINTK(KERN_INFO, "INITIALIZE ELEMENT STATUS, may take some time ...\n"); 283daa6eda6SGerd Knorr memset(cmd,0,sizeof(cmd)); 284daa6eda6SGerd Knorr cmd[0] = INITIALIZE_ELEMENT_STATUS; 2859cb78c16SHannes Reinecke cmd[1] = (ch->device->lun & 0x7) << 5; 286a9a47bf5SHannes Reinecke err = ch_do_scsi(ch, cmd, 6, NULL, 0, DMA_NONE); 28787da3235SJoe Perches VPRINTK(KERN_INFO, "... finished\n"); 288daa6eda6SGerd Knorr return err; 289daa6eda6SGerd Knorr } 290daa6eda6SGerd Knorr 291daa6eda6SGerd Knorr static int 292daa6eda6SGerd Knorr ch_readconfig(scsi_changer *ch) 293daa6eda6SGerd Knorr { 294daa6eda6SGerd Knorr u_char cmd[10], data[16]; 295daa6eda6SGerd Knorr u_char *buffer; 296daa6eda6SGerd Knorr int result,id,lun,i; 297daa6eda6SGerd Knorr u_int elem; 298daa6eda6SGerd Knorr 2994530a169Svignesh.babu@wipro.com buffer = kzalloc(512, GFP_KERNEL | GFP_DMA); 300daa6eda6SGerd Knorr if (!buffer) 301daa6eda6SGerd Knorr return -ENOMEM; 302daa6eda6SGerd Knorr 303daa6eda6SGerd Knorr memset(cmd,0,sizeof(cmd)); 304daa6eda6SGerd Knorr cmd[0] = MODE_SENSE; 3059cb78c16SHannes Reinecke cmd[1] = (ch->device->lun & 0x7) << 5; 306daa6eda6SGerd Knorr cmd[2] = 0x1d; 307daa6eda6SGerd Knorr cmd[4] = 255; 308a9a47bf5SHannes Reinecke result = ch_do_scsi(ch, cmd, 10, buffer, 255, DMA_FROM_DEVICE); 309daa6eda6SGerd Knorr if (0 != result) { 310daa6eda6SGerd Knorr cmd[1] |= (1<<3); 311a9a47bf5SHannes Reinecke result = ch_do_scsi(ch, cmd, 10, buffer, 255, DMA_FROM_DEVICE); 312daa6eda6SGerd Knorr } 313daa6eda6SGerd Knorr if (0 == result) { 314daa6eda6SGerd Knorr ch->firsts[CHET_MT] = 315daa6eda6SGerd Knorr (buffer[buffer[3]+ 6] << 8) | buffer[buffer[3]+ 7]; 316daa6eda6SGerd Knorr ch->counts[CHET_MT] = 317daa6eda6SGerd Knorr (buffer[buffer[3]+ 8] << 8) | buffer[buffer[3]+ 9]; 318daa6eda6SGerd Knorr ch->firsts[CHET_ST] = 319daa6eda6SGerd Knorr (buffer[buffer[3]+10] << 8) | buffer[buffer[3]+11]; 320daa6eda6SGerd Knorr ch->counts[CHET_ST] = 321daa6eda6SGerd Knorr (buffer[buffer[3]+12] << 8) | buffer[buffer[3]+13]; 322daa6eda6SGerd Knorr ch->firsts[CHET_IE] = 323daa6eda6SGerd Knorr (buffer[buffer[3]+14] << 8) | buffer[buffer[3]+15]; 324daa6eda6SGerd Knorr ch->counts[CHET_IE] = 325daa6eda6SGerd Knorr (buffer[buffer[3]+16] << 8) | buffer[buffer[3]+17]; 326daa6eda6SGerd Knorr ch->firsts[CHET_DT] = 327daa6eda6SGerd Knorr (buffer[buffer[3]+18] << 8) | buffer[buffer[3]+19]; 328daa6eda6SGerd Knorr ch->counts[CHET_DT] = 329daa6eda6SGerd Knorr (buffer[buffer[3]+20] << 8) | buffer[buffer[3]+21]; 33087da3235SJoe Perches VPRINTK(KERN_INFO, "type #1 (mt): 0x%x+%d [medium transport]\n", 331daa6eda6SGerd Knorr ch->firsts[CHET_MT], 332daa6eda6SGerd Knorr ch->counts[CHET_MT]); 33387da3235SJoe Perches VPRINTK(KERN_INFO, "type #2 (st): 0x%x+%d [storage]\n", 334daa6eda6SGerd Knorr ch->firsts[CHET_ST], 335daa6eda6SGerd Knorr ch->counts[CHET_ST]); 33687da3235SJoe Perches VPRINTK(KERN_INFO, "type #3 (ie): 0x%x+%d [import/export]\n", 337daa6eda6SGerd Knorr ch->firsts[CHET_IE], 338daa6eda6SGerd Knorr ch->counts[CHET_IE]); 33987da3235SJoe Perches VPRINTK(KERN_INFO, "type #4 (dt): 0x%x+%d [data transfer]\n", 340daa6eda6SGerd Knorr ch->firsts[CHET_DT], 341daa6eda6SGerd Knorr ch->counts[CHET_DT]); 342daa6eda6SGerd Knorr } else { 343f42cf8d6SMasanari Iida VPRINTK(KERN_INFO, "reading element address assignment page failed!\n"); 344daa6eda6SGerd Knorr } 345daa6eda6SGerd Knorr 346daa6eda6SGerd Knorr /* vendor specific element types */ 347daa6eda6SGerd Knorr for (i = 0; i < 4; i++) { 348daa6eda6SGerd Knorr if (0 == vendor_counts[i]) 349daa6eda6SGerd Knorr continue; 350daa6eda6SGerd Knorr if (NULL == vendor_labels[i]) 351daa6eda6SGerd Knorr continue; 352daa6eda6SGerd Knorr ch->firsts[CHET_V1+i] = vendor_firsts[i]; 353daa6eda6SGerd Knorr ch->counts[CHET_V1+i] = vendor_counts[i]; 35487da3235SJoe Perches VPRINTK(KERN_INFO, "type #%d (v%d): 0x%x+%d [%s, vendor specific]\n", 355daa6eda6SGerd Knorr i+5,i+1,vendor_firsts[i],vendor_counts[i], 356daa6eda6SGerd Knorr vendor_labels[i]); 357daa6eda6SGerd Knorr } 358daa6eda6SGerd Knorr 359daa6eda6SGerd Knorr /* look up the devices of the data transfer elements */ 36085bc081fSJulia Lawall ch->dt = kcalloc(ch->counts[CHET_DT], sizeof(*ch->dt), 361daa6eda6SGerd Knorr GFP_KERNEL); 362a2cf8a63SDavidlohr Bueso A 363a2cf8a63SDavidlohr Bueso A if (!ch->dt) { 364a2cf8a63SDavidlohr Bueso A kfree(buffer); 365a2cf8a63SDavidlohr Bueso A return -ENOMEM; 366a2cf8a63SDavidlohr Bueso A } 367a2cf8a63SDavidlohr Bueso A 368daa6eda6SGerd Knorr for (elem = 0; elem < ch->counts[CHET_DT]; elem++) { 369daa6eda6SGerd Knorr id = -1; 370daa6eda6SGerd Knorr lun = 0; 371daa6eda6SGerd Knorr if (elem < CH_DT_MAX && -1 != dt_id[elem]) { 372daa6eda6SGerd Knorr id = dt_id[elem]; 373daa6eda6SGerd Knorr lun = dt_lun[elem]; 37487da3235SJoe Perches VPRINTK(KERN_INFO, "dt 0x%x: [insmod option] ", 375daa6eda6SGerd Knorr elem+ch->firsts[CHET_DT]); 376daa6eda6SGerd Knorr } else if (0 != ch_read_element_status 377daa6eda6SGerd Knorr (ch,elem+ch->firsts[CHET_DT],data)) { 37887da3235SJoe Perches VPRINTK(KERN_INFO, "dt 0x%x: READ ELEMENT STATUS failed\n", 379daa6eda6SGerd Knorr elem+ch->firsts[CHET_DT]); 380daa6eda6SGerd Knorr } else { 38187da3235SJoe Perches VPRINTK(KERN_INFO, "dt 0x%x: ",elem+ch->firsts[CHET_DT]); 382daa6eda6SGerd Knorr if (data[6] & 0x80) { 38387da3235SJoe Perches VPRINTK(KERN_CONT, "not this SCSI bus\n"); 384daa6eda6SGerd Knorr ch->dt[elem] = NULL; 385daa6eda6SGerd Knorr } else if (0 == (data[6] & 0x30)) { 38687da3235SJoe Perches VPRINTK(KERN_CONT, "ID/LUN unknown\n"); 387daa6eda6SGerd Knorr ch->dt[elem] = NULL; 388daa6eda6SGerd Knorr } else { 389daa6eda6SGerd Knorr id = ch->device->id; 390daa6eda6SGerd Knorr lun = 0; 391daa6eda6SGerd Knorr if (data[6] & 0x20) id = data[7]; 392daa6eda6SGerd Knorr if (data[6] & 0x10) lun = data[6] & 7; 393daa6eda6SGerd Knorr } 394daa6eda6SGerd Knorr } 395daa6eda6SGerd Knorr if (-1 != id) { 39687da3235SJoe Perches VPRINTK(KERN_CONT, "ID %i, LUN %i, ",id,lun); 397daa6eda6SGerd Knorr ch->dt[elem] = 398daa6eda6SGerd Knorr scsi_device_lookup(ch->device->host, 399daa6eda6SGerd Knorr ch->device->channel, 400daa6eda6SGerd Knorr id,lun); 401daa6eda6SGerd Knorr if (!ch->dt[elem]) { 402daa6eda6SGerd Knorr /* should not happen */ 40387da3235SJoe Perches VPRINTK(KERN_CONT, "Huh? device not found!\n"); 404daa6eda6SGerd Knorr } else { 40587da3235SJoe Perches VPRINTK(KERN_CONT, "name: %8.8s %16.16s %4.4s\n", 406daa6eda6SGerd Knorr ch->dt[elem]->vendor, 407daa6eda6SGerd Knorr ch->dt[elem]->model, 408daa6eda6SGerd Knorr ch->dt[elem]->rev); 409daa6eda6SGerd Knorr } 410daa6eda6SGerd Knorr } 411daa6eda6SGerd Knorr } 412daa6eda6SGerd Knorr ch->voltags = 1; 413daa6eda6SGerd Knorr kfree(buffer); 414daa6eda6SGerd Knorr 415daa6eda6SGerd Knorr return 0; 416daa6eda6SGerd Knorr } 417daa6eda6SGerd Knorr 418daa6eda6SGerd Knorr /* ------------------------------------------------------------------------ */ 419daa6eda6SGerd Knorr 420daa6eda6SGerd Knorr static int 421daa6eda6SGerd Knorr ch_position(scsi_changer *ch, u_int trans, u_int elem, int rotate) 422daa6eda6SGerd Knorr { 423daa6eda6SGerd Knorr u_char cmd[10]; 424daa6eda6SGerd Knorr 42587da3235SJoe Perches DPRINTK("position: 0x%x\n",elem); 426daa6eda6SGerd Knorr if (0 == trans) 427daa6eda6SGerd Knorr trans = ch->firsts[CHET_MT]; 428daa6eda6SGerd Knorr memset(cmd,0,sizeof(cmd)); 429daa6eda6SGerd Knorr cmd[0] = POSITION_TO_ELEMENT; 4309cb78c16SHannes Reinecke cmd[1] = (ch->device->lun & 0x7) << 5; 431daa6eda6SGerd Knorr cmd[2] = (trans >> 8) & 0xff; 432daa6eda6SGerd Knorr cmd[3] = trans & 0xff; 433daa6eda6SGerd Knorr cmd[4] = (elem >> 8) & 0xff; 434daa6eda6SGerd Knorr cmd[5] = elem & 0xff; 435daa6eda6SGerd Knorr cmd[8] = rotate ? 1 : 0; 436a9a47bf5SHannes Reinecke return ch_do_scsi(ch, cmd, 10, NULL, 0, DMA_NONE); 437daa6eda6SGerd Knorr } 438daa6eda6SGerd Knorr 439daa6eda6SGerd Knorr static int 440daa6eda6SGerd Knorr ch_move(scsi_changer *ch, u_int trans, u_int src, u_int dest, int rotate) 441daa6eda6SGerd Knorr { 442daa6eda6SGerd Knorr u_char cmd[12]; 443daa6eda6SGerd Knorr 44487da3235SJoe Perches DPRINTK("move: 0x%x => 0x%x\n",src,dest); 445daa6eda6SGerd Knorr if (0 == trans) 446daa6eda6SGerd Knorr trans = ch->firsts[CHET_MT]; 447daa6eda6SGerd Knorr memset(cmd,0,sizeof(cmd)); 448daa6eda6SGerd Knorr cmd[0] = MOVE_MEDIUM; 4499cb78c16SHannes Reinecke cmd[1] = (ch->device->lun & 0x7) << 5; 450daa6eda6SGerd Knorr cmd[2] = (trans >> 8) & 0xff; 451daa6eda6SGerd Knorr cmd[3] = trans & 0xff; 452daa6eda6SGerd Knorr cmd[4] = (src >> 8) & 0xff; 453daa6eda6SGerd Knorr cmd[5] = src & 0xff; 454daa6eda6SGerd Knorr cmd[6] = (dest >> 8) & 0xff; 455daa6eda6SGerd Knorr cmd[7] = dest & 0xff; 456daa6eda6SGerd Knorr cmd[10] = rotate ? 1 : 0; 457a9a47bf5SHannes Reinecke return ch_do_scsi(ch, cmd, 12, NULL,0, DMA_NONE); 458daa6eda6SGerd Knorr } 459daa6eda6SGerd Knorr 460daa6eda6SGerd Knorr static int 461daa6eda6SGerd Knorr ch_exchange(scsi_changer *ch, u_int trans, u_int src, 462daa6eda6SGerd Knorr u_int dest1, u_int dest2, int rotate1, int rotate2) 463daa6eda6SGerd Knorr { 464daa6eda6SGerd Knorr u_char cmd[12]; 465daa6eda6SGerd Knorr 46687da3235SJoe Perches DPRINTK("exchange: 0x%x => 0x%x => 0x%x\n", 467daa6eda6SGerd Knorr src,dest1,dest2); 468daa6eda6SGerd Knorr if (0 == trans) 469daa6eda6SGerd Knorr trans = ch->firsts[CHET_MT]; 470daa6eda6SGerd Knorr memset(cmd,0,sizeof(cmd)); 471daa6eda6SGerd Knorr cmd[0] = EXCHANGE_MEDIUM; 4729cb78c16SHannes Reinecke cmd[1] = (ch->device->lun & 0x7) << 5; 473daa6eda6SGerd Knorr cmd[2] = (trans >> 8) & 0xff; 474daa6eda6SGerd Knorr cmd[3] = trans & 0xff; 475daa6eda6SGerd Knorr cmd[4] = (src >> 8) & 0xff; 476daa6eda6SGerd Knorr cmd[5] = src & 0xff; 477daa6eda6SGerd Knorr cmd[6] = (dest1 >> 8) & 0xff; 478daa6eda6SGerd Knorr cmd[7] = dest1 & 0xff; 479daa6eda6SGerd Knorr cmd[8] = (dest2 >> 8) & 0xff; 480daa6eda6SGerd Knorr cmd[9] = dest2 & 0xff; 481daa6eda6SGerd Knorr cmd[10] = (rotate1 ? 1 : 0) | (rotate2 ? 2 : 0); 482daa6eda6SGerd Knorr 483a9a47bf5SHannes Reinecke return ch_do_scsi(ch, cmd, 12, NULL, 0, DMA_NONE); 484daa6eda6SGerd Knorr } 485daa6eda6SGerd Knorr 486daa6eda6SGerd Knorr static void 487daa6eda6SGerd Knorr ch_check_voltag(char *tag) 488daa6eda6SGerd Knorr { 489daa6eda6SGerd Knorr int i; 490daa6eda6SGerd Knorr 491daa6eda6SGerd Knorr for (i = 0; i < 32; i++) { 492daa6eda6SGerd Knorr /* restrict to ascii */ 493daa6eda6SGerd Knorr if (tag[i] >= 0x7f || tag[i] < 0x20) 494daa6eda6SGerd Knorr tag[i] = ' '; 495daa6eda6SGerd Knorr /* don't allow search wildcards */ 496daa6eda6SGerd Knorr if (tag[i] == '?' || 497daa6eda6SGerd Knorr tag[i] == '*') 498daa6eda6SGerd Knorr tag[i] = ' '; 499daa6eda6SGerd Knorr } 500daa6eda6SGerd Knorr } 501daa6eda6SGerd Knorr 502daa6eda6SGerd Knorr static int 503daa6eda6SGerd Knorr ch_set_voltag(scsi_changer *ch, u_int elem, 504daa6eda6SGerd Knorr int alternate, int clear, u_char *tag) 505daa6eda6SGerd Knorr { 506daa6eda6SGerd Knorr u_char cmd[12]; 507daa6eda6SGerd Knorr u_char *buffer; 508daa6eda6SGerd Knorr int result; 509daa6eda6SGerd Knorr 5104530a169Svignesh.babu@wipro.com buffer = kzalloc(512, GFP_KERNEL); 511daa6eda6SGerd Knorr if (!buffer) 512daa6eda6SGerd Knorr return -ENOMEM; 513daa6eda6SGerd Knorr 51487da3235SJoe Perches DPRINTK("%s %s voltag: 0x%x => \"%s\"\n", 515daa6eda6SGerd Knorr clear ? "clear" : "set", 516daa6eda6SGerd Knorr alternate ? "alternate" : "primary", 517daa6eda6SGerd Knorr elem, tag); 518daa6eda6SGerd Knorr memset(cmd,0,sizeof(cmd)); 519daa6eda6SGerd Knorr cmd[0] = SEND_VOLUME_TAG; 5209cb78c16SHannes Reinecke cmd[1] = ((ch->device->lun & 0x7) << 5) | 521daa6eda6SGerd Knorr ch_elem_to_typecode(ch,elem); 522daa6eda6SGerd Knorr cmd[2] = (elem >> 8) & 0xff; 523daa6eda6SGerd Knorr cmd[3] = elem & 0xff; 524daa6eda6SGerd Knorr cmd[5] = clear 525daa6eda6SGerd Knorr ? (alternate ? 0x0d : 0x0c) 526daa6eda6SGerd Knorr : (alternate ? 0x0b : 0x0a); 527daa6eda6SGerd Knorr 528daa6eda6SGerd Knorr cmd[9] = 255; 529daa6eda6SGerd Knorr 530daa6eda6SGerd Knorr memcpy(buffer,tag,32); 531daa6eda6SGerd Knorr ch_check_voltag(buffer); 532daa6eda6SGerd Knorr 533a9a47bf5SHannes Reinecke result = ch_do_scsi(ch, cmd, 12, buffer, 256, DMA_TO_DEVICE); 534daa6eda6SGerd Knorr kfree(buffer); 535daa6eda6SGerd Knorr return result; 536daa6eda6SGerd Knorr } 537daa6eda6SGerd Knorr 538fe08ac31Sviro@ZenIV.linux.org.uk static int ch_gstatus(scsi_changer *ch, int type, unsigned char __user *dest) 539daa6eda6SGerd Knorr { 540daa6eda6SGerd Knorr int retval = 0; 541daa6eda6SGerd Knorr u_char data[16]; 542daa6eda6SGerd Knorr unsigned int i; 543daa6eda6SGerd Knorr 5440b950672SArjan van de Ven mutex_lock(&ch->lock); 545daa6eda6SGerd Knorr for (i = 0; i < ch->counts[type]; i++) { 546daa6eda6SGerd Knorr if (0 != ch_read_element_status 547daa6eda6SGerd Knorr (ch, ch->firsts[type]+i,data)) { 548daa6eda6SGerd Knorr retval = -EIO; 549daa6eda6SGerd Knorr break; 550daa6eda6SGerd Knorr } 551daa6eda6SGerd Knorr put_user(data[2], dest+i); 552daa6eda6SGerd Knorr if (data[2] & CESTATUS_EXCEPT) 55387da3235SJoe Perches VPRINTK(KERN_INFO, "element 0x%x: asc=0x%x, ascq=0x%x\n", 554daa6eda6SGerd Knorr ch->firsts[type]+i, 555daa6eda6SGerd Knorr (int)data[4],(int)data[5]); 556daa6eda6SGerd Knorr retval = ch_read_element_status 557daa6eda6SGerd Knorr (ch, ch->firsts[type]+i,data); 558daa6eda6SGerd Knorr if (0 != retval) 559daa6eda6SGerd Knorr break; 560daa6eda6SGerd Knorr } 5610b950672SArjan van de Ven mutex_unlock(&ch->lock); 562daa6eda6SGerd Knorr return retval; 563daa6eda6SGerd Knorr } 564daa6eda6SGerd Knorr 565daa6eda6SGerd Knorr /* ------------------------------------------------------------------------ */ 566daa6eda6SGerd Knorr 567085e5676SHannes Reinecke static void ch_destroy(struct kref *ref) 568085e5676SHannes Reinecke { 569085e5676SHannes Reinecke scsi_changer *ch = container_of(ref, scsi_changer, ref); 570085e5676SHannes Reinecke 571085e5676SHannes Reinecke kfree(ch->dt); 572085e5676SHannes Reinecke kfree(ch); 573085e5676SHannes Reinecke } 574085e5676SHannes Reinecke 575daa6eda6SGerd Knorr static int 576daa6eda6SGerd Knorr ch_release(struct inode *inode, struct file *file) 577daa6eda6SGerd Knorr { 578daa6eda6SGerd Knorr scsi_changer *ch = file->private_data; 579daa6eda6SGerd Knorr 580daa6eda6SGerd Knorr scsi_device_put(ch->device); 581085e5676SHannes Reinecke ch->device = NULL; 582daa6eda6SGerd Knorr file->private_data = NULL; 583085e5676SHannes Reinecke kref_put(&ch->ref, ch_destroy); 584daa6eda6SGerd Knorr return 0; 585daa6eda6SGerd Knorr } 586daa6eda6SGerd Knorr 587daa6eda6SGerd Knorr static int 588daa6eda6SGerd Knorr ch_open(struct inode *inode, struct file *file) 589daa6eda6SGerd Knorr { 590da707c54SFUJITA Tomonori scsi_changer *ch; 591daa6eda6SGerd Knorr int minor = iminor(inode); 592daa6eda6SGerd Knorr 593c45d15d2SArnd Bergmann mutex_lock(&ch_mutex); 594da707c54SFUJITA Tomonori spin_lock(&ch_index_lock); 595da707c54SFUJITA Tomonori ch = idr_find(&ch_index_idr, minor); 596da707c54SFUJITA Tomonori 597daa6eda6SGerd Knorr if (NULL == ch || scsi_device_get(ch->device)) { 598da707c54SFUJITA Tomonori spin_unlock(&ch_index_lock); 599c45d15d2SArnd Bergmann mutex_unlock(&ch_mutex); 600daa6eda6SGerd Knorr return -ENXIO; 601daa6eda6SGerd Knorr } 602085e5676SHannes Reinecke kref_get(&ch->ref); 603da707c54SFUJITA Tomonori spin_unlock(&ch_index_lock); 604daa6eda6SGerd Knorr 605daa6eda6SGerd Knorr file->private_data = ch; 606c45d15d2SArnd Bergmann mutex_unlock(&ch_mutex); 607daa6eda6SGerd Knorr return 0; 608daa6eda6SGerd Knorr } 609daa6eda6SGerd Knorr 610daa6eda6SGerd Knorr static int 611daa6eda6SGerd Knorr ch_checkrange(scsi_changer *ch, unsigned int type, unsigned int unit) 612daa6eda6SGerd Knorr { 613daa6eda6SGerd Knorr if (type >= CH_TYPES || unit >= ch->counts[type]) 614daa6eda6SGerd Knorr return -1; 615daa6eda6SGerd Knorr return 0; 616daa6eda6SGerd Knorr } 617daa6eda6SGerd Knorr 618f7fea185SMathieu Segaud static long ch_ioctl(struct file *file, 619daa6eda6SGerd Knorr unsigned int cmd, unsigned long arg) 620daa6eda6SGerd Knorr { 621daa6eda6SGerd Knorr scsi_changer *ch = file->private_data; 622daa6eda6SGerd Knorr int retval; 623fe08ac31Sviro@ZenIV.linux.org.uk void __user *argp = (void __user *)arg; 624daa6eda6SGerd Knorr 625906d15fbSChristoph Hellwig retval = scsi_ioctl_block_when_processing_errors(ch->device, cmd, 626906d15fbSChristoph Hellwig file->f_flags & O_NDELAY); 627906d15fbSChristoph Hellwig if (retval) 628906d15fbSChristoph Hellwig return retval; 629906d15fbSChristoph Hellwig 630daa6eda6SGerd Knorr switch (cmd) { 631daa6eda6SGerd Knorr case CHIOGPARAMS: 632daa6eda6SGerd Knorr { 633daa6eda6SGerd Knorr struct changer_params params; 634daa6eda6SGerd Knorr 635daa6eda6SGerd Knorr params.cp_curpicker = 0; 636daa6eda6SGerd Knorr params.cp_npickers = ch->counts[CHET_MT]; 637daa6eda6SGerd Knorr params.cp_nslots = ch->counts[CHET_ST]; 638daa6eda6SGerd Knorr params.cp_nportals = ch->counts[CHET_IE]; 639daa6eda6SGerd Knorr params.cp_ndrives = ch->counts[CHET_DT]; 640daa6eda6SGerd Knorr 641fe08ac31Sviro@ZenIV.linux.org.uk if (copy_to_user(argp, ¶ms, sizeof(params))) 642daa6eda6SGerd Knorr return -EFAULT; 643daa6eda6SGerd Knorr return 0; 644daa6eda6SGerd Knorr } 645daa6eda6SGerd Knorr case CHIOGVPARAMS: 646daa6eda6SGerd Knorr { 647daa6eda6SGerd Knorr struct changer_vendor_params vparams; 648daa6eda6SGerd Knorr 649daa6eda6SGerd Knorr memset(&vparams,0,sizeof(vparams)); 650daa6eda6SGerd Knorr if (ch->counts[CHET_V1]) { 651daa6eda6SGerd Knorr vparams.cvp_n1 = ch->counts[CHET_V1]; 652daa6eda6SGerd Knorr strncpy(vparams.cvp_label1,vendor_labels[0],16); 653daa6eda6SGerd Knorr } 654daa6eda6SGerd Knorr if (ch->counts[CHET_V2]) { 655daa6eda6SGerd Knorr vparams.cvp_n2 = ch->counts[CHET_V2]; 656daa6eda6SGerd Knorr strncpy(vparams.cvp_label2,vendor_labels[1],16); 657daa6eda6SGerd Knorr } 658daa6eda6SGerd Knorr if (ch->counts[CHET_V3]) { 659daa6eda6SGerd Knorr vparams.cvp_n3 = ch->counts[CHET_V3]; 660daa6eda6SGerd Knorr strncpy(vparams.cvp_label3,vendor_labels[2],16); 661daa6eda6SGerd Knorr } 662daa6eda6SGerd Knorr if (ch->counts[CHET_V4]) { 663daa6eda6SGerd Knorr vparams.cvp_n4 = ch->counts[CHET_V4]; 664daa6eda6SGerd Knorr strncpy(vparams.cvp_label4,vendor_labels[3],16); 665daa6eda6SGerd Knorr } 666fe08ac31Sviro@ZenIV.linux.org.uk if (copy_to_user(argp, &vparams, sizeof(vparams))) 667daa6eda6SGerd Knorr return -EFAULT; 668daa6eda6SGerd Knorr return 0; 669daa6eda6SGerd Knorr } 670daa6eda6SGerd Knorr 671daa6eda6SGerd Knorr case CHIOPOSITION: 672daa6eda6SGerd Knorr { 673daa6eda6SGerd Knorr struct changer_position pos; 674daa6eda6SGerd Knorr 675fe08ac31Sviro@ZenIV.linux.org.uk if (copy_from_user(&pos, argp, sizeof (pos))) 676daa6eda6SGerd Knorr return -EFAULT; 677daa6eda6SGerd Knorr 678daa6eda6SGerd Knorr if (0 != ch_checkrange(ch, pos.cp_type, pos.cp_unit)) { 67987da3235SJoe Perches DPRINTK("CHIOPOSITION: invalid parameter\n"); 680daa6eda6SGerd Knorr return -EBADSLT; 681daa6eda6SGerd Knorr } 6820b950672SArjan van de Ven mutex_lock(&ch->lock); 683daa6eda6SGerd Knorr retval = ch_position(ch,0, 684daa6eda6SGerd Knorr ch->firsts[pos.cp_type] + pos.cp_unit, 685daa6eda6SGerd Knorr pos.cp_flags & CP_INVERT); 6860b950672SArjan van de Ven mutex_unlock(&ch->lock); 687daa6eda6SGerd Knorr return retval; 688daa6eda6SGerd Knorr } 689daa6eda6SGerd Knorr 690daa6eda6SGerd Knorr case CHIOMOVE: 691daa6eda6SGerd Knorr { 692daa6eda6SGerd Knorr struct changer_move mv; 693daa6eda6SGerd Knorr 694fe08ac31Sviro@ZenIV.linux.org.uk if (copy_from_user(&mv, argp, sizeof (mv))) 695daa6eda6SGerd Knorr return -EFAULT; 696daa6eda6SGerd Knorr 697daa6eda6SGerd Knorr if (0 != ch_checkrange(ch, mv.cm_fromtype, mv.cm_fromunit) || 698daa6eda6SGerd Knorr 0 != ch_checkrange(ch, mv.cm_totype, mv.cm_tounit )) { 69987da3235SJoe Perches DPRINTK("CHIOMOVE: invalid parameter\n"); 700daa6eda6SGerd Knorr return -EBADSLT; 701daa6eda6SGerd Knorr } 702daa6eda6SGerd Knorr 7030b950672SArjan van de Ven mutex_lock(&ch->lock); 704daa6eda6SGerd Knorr retval = ch_move(ch,0, 705daa6eda6SGerd Knorr ch->firsts[mv.cm_fromtype] + mv.cm_fromunit, 706daa6eda6SGerd Knorr ch->firsts[mv.cm_totype] + mv.cm_tounit, 707daa6eda6SGerd Knorr mv.cm_flags & CM_INVERT); 7080b950672SArjan van de Ven mutex_unlock(&ch->lock); 709daa6eda6SGerd Knorr return retval; 710daa6eda6SGerd Knorr } 711daa6eda6SGerd Knorr 712daa6eda6SGerd Knorr case CHIOEXCHANGE: 713daa6eda6SGerd Knorr { 714daa6eda6SGerd Knorr struct changer_exchange mv; 715daa6eda6SGerd Knorr 716fe08ac31Sviro@ZenIV.linux.org.uk if (copy_from_user(&mv, argp, sizeof (mv))) 717daa6eda6SGerd Knorr return -EFAULT; 718daa6eda6SGerd Knorr 719daa6eda6SGerd Knorr if (0 != ch_checkrange(ch, mv.ce_srctype, mv.ce_srcunit ) || 720daa6eda6SGerd Knorr 0 != ch_checkrange(ch, mv.ce_fdsttype, mv.ce_fdstunit) || 721daa6eda6SGerd Knorr 0 != ch_checkrange(ch, mv.ce_sdsttype, mv.ce_sdstunit)) { 72287da3235SJoe Perches DPRINTK("CHIOEXCHANGE: invalid parameter\n"); 723daa6eda6SGerd Knorr return -EBADSLT; 724daa6eda6SGerd Knorr } 725daa6eda6SGerd Knorr 7260b950672SArjan van de Ven mutex_lock(&ch->lock); 727daa6eda6SGerd Knorr retval = ch_exchange 728daa6eda6SGerd Knorr (ch,0, 729daa6eda6SGerd Knorr ch->firsts[mv.ce_srctype] + mv.ce_srcunit, 730daa6eda6SGerd Knorr ch->firsts[mv.ce_fdsttype] + mv.ce_fdstunit, 731daa6eda6SGerd Knorr ch->firsts[mv.ce_sdsttype] + mv.ce_sdstunit, 732daa6eda6SGerd Knorr mv.ce_flags & CE_INVERT1, mv.ce_flags & CE_INVERT2); 7330b950672SArjan van de Ven mutex_unlock(&ch->lock); 734daa6eda6SGerd Knorr return retval; 735daa6eda6SGerd Knorr } 736daa6eda6SGerd Knorr 737daa6eda6SGerd Knorr case CHIOGSTATUS: 738daa6eda6SGerd Knorr { 739daa6eda6SGerd Knorr struct changer_element_status ces; 740daa6eda6SGerd Knorr 741fe08ac31Sviro@ZenIV.linux.org.uk if (copy_from_user(&ces, argp, sizeof (ces))) 742daa6eda6SGerd Knorr return -EFAULT; 743daa6eda6SGerd Knorr if (ces.ces_type < 0 || ces.ces_type >= CH_TYPES) 744daa6eda6SGerd Knorr return -EINVAL; 745daa6eda6SGerd Knorr 746daa6eda6SGerd Knorr return ch_gstatus(ch, ces.ces_type, ces.ces_data); 747daa6eda6SGerd Knorr } 748daa6eda6SGerd Knorr 749daa6eda6SGerd Knorr case CHIOGELEM: 750daa6eda6SGerd Knorr { 751daa6eda6SGerd Knorr struct changer_get_element cge; 752d70d4667SHarvey Harrison u_char ch_cmd[12]; 753daa6eda6SGerd Knorr u_char *buffer; 754daa6eda6SGerd Knorr unsigned int elem; 755daa6eda6SGerd Knorr int result,i; 756daa6eda6SGerd Knorr 757fe08ac31Sviro@ZenIV.linux.org.uk if (copy_from_user(&cge, argp, sizeof (cge))) 758daa6eda6SGerd Knorr return -EFAULT; 759daa6eda6SGerd Knorr 760daa6eda6SGerd Knorr if (0 != ch_checkrange(ch, cge.cge_type, cge.cge_unit)) 761daa6eda6SGerd Knorr return -EINVAL; 762daa6eda6SGerd Knorr elem = ch->firsts[cge.cge_type] + cge.cge_unit; 763daa6eda6SGerd Knorr 764daa6eda6SGerd Knorr buffer = kmalloc(512, GFP_KERNEL | GFP_DMA); 765daa6eda6SGerd Knorr if (!buffer) 766daa6eda6SGerd Knorr return -ENOMEM; 7670b950672SArjan van de Ven mutex_lock(&ch->lock); 768daa6eda6SGerd Knorr 769daa6eda6SGerd Knorr voltag_retry: 770d70d4667SHarvey Harrison memset(ch_cmd, 0, sizeof(ch_cmd)); 771d70d4667SHarvey Harrison ch_cmd[0] = READ_ELEMENT_STATUS; 7729cb78c16SHannes Reinecke ch_cmd[1] = ((ch->device->lun & 0x7) << 5) | 773daa6eda6SGerd Knorr (ch->voltags ? 0x10 : 0) | 774daa6eda6SGerd Knorr ch_elem_to_typecode(ch,elem); 775d70d4667SHarvey Harrison ch_cmd[2] = (elem >> 8) & 0xff; 776d70d4667SHarvey Harrison ch_cmd[3] = elem & 0xff; 777d70d4667SHarvey Harrison ch_cmd[5] = 1; 778d70d4667SHarvey Harrison ch_cmd[9] = 255; 779daa6eda6SGerd Knorr 780a9a47bf5SHannes Reinecke result = ch_do_scsi(ch, ch_cmd, 12, 781a9a47bf5SHannes Reinecke buffer, 256, DMA_FROM_DEVICE); 782d70d4667SHarvey Harrison if (!result) { 783daa6eda6SGerd Knorr cge.cge_status = buffer[18]; 784daa6eda6SGerd Knorr cge.cge_flags = 0; 785daa6eda6SGerd Knorr if (buffer[18] & CESTATUS_EXCEPT) { 786daa6eda6SGerd Knorr cge.cge_errno = EIO; 787daa6eda6SGerd Knorr } 788daa6eda6SGerd Knorr if (buffer[25] & 0x80) { 789daa6eda6SGerd Knorr cge.cge_flags |= CGE_SRC; 790daa6eda6SGerd Knorr if (buffer[25] & 0x40) 791daa6eda6SGerd Knorr cge.cge_flags |= CGE_INVERT; 792daa6eda6SGerd Knorr elem = (buffer[26]<<8) | buffer[27]; 793daa6eda6SGerd Knorr for (i = 0; i < 4; i++) { 794daa6eda6SGerd Knorr if (elem >= ch->firsts[i] && 795daa6eda6SGerd Knorr elem < ch->firsts[i] + ch->counts[i]) { 796daa6eda6SGerd Knorr cge.cge_srctype = i; 797daa6eda6SGerd Knorr cge.cge_srcunit = elem-ch->firsts[i]; 798daa6eda6SGerd Knorr } 799daa6eda6SGerd Knorr } 800daa6eda6SGerd Knorr } 801daa6eda6SGerd Knorr if ((buffer[22] & 0x30) == 0x30) { 802daa6eda6SGerd Knorr cge.cge_flags |= CGE_IDLUN; 803daa6eda6SGerd Knorr cge.cge_id = buffer[23]; 804daa6eda6SGerd Knorr cge.cge_lun = buffer[22] & 7; 805daa6eda6SGerd Knorr } 806daa6eda6SGerd Knorr if (buffer[9] & 0x80) { 807daa6eda6SGerd Knorr cge.cge_flags |= CGE_PVOLTAG; 808daa6eda6SGerd Knorr memcpy(cge.cge_pvoltag,buffer+28,36); 809daa6eda6SGerd Knorr } 810daa6eda6SGerd Knorr if (buffer[9] & 0x40) { 811daa6eda6SGerd Knorr cge.cge_flags |= CGE_AVOLTAG; 812daa6eda6SGerd Knorr memcpy(cge.cge_avoltag,buffer+64,36); 813daa6eda6SGerd Knorr } 814daa6eda6SGerd Knorr } else if (ch->voltags) { 815daa6eda6SGerd Knorr ch->voltags = 0; 81687da3235SJoe Perches VPRINTK(KERN_INFO, "device has no volume tag support\n"); 817daa6eda6SGerd Knorr goto voltag_retry; 818daa6eda6SGerd Knorr } 819daa6eda6SGerd Knorr kfree(buffer); 8200b950672SArjan van de Ven mutex_unlock(&ch->lock); 821daa6eda6SGerd Knorr 822fe08ac31Sviro@ZenIV.linux.org.uk if (copy_to_user(argp, &cge, sizeof (cge))) 823daa6eda6SGerd Knorr return -EFAULT; 824daa6eda6SGerd Knorr return result; 825daa6eda6SGerd Knorr } 826daa6eda6SGerd Knorr 827daa6eda6SGerd Knorr case CHIOINITELEM: 828daa6eda6SGerd Knorr { 8290b950672SArjan van de Ven mutex_lock(&ch->lock); 830daa6eda6SGerd Knorr retval = ch_init_elem(ch); 8310b950672SArjan van de Ven mutex_unlock(&ch->lock); 832daa6eda6SGerd Knorr return retval; 833daa6eda6SGerd Knorr } 834daa6eda6SGerd Knorr 835daa6eda6SGerd Knorr case CHIOSVOLTAG: 836daa6eda6SGerd Knorr { 837daa6eda6SGerd Knorr struct changer_set_voltag csv; 838daa6eda6SGerd Knorr int elem; 839daa6eda6SGerd Knorr 840fe08ac31Sviro@ZenIV.linux.org.uk if (copy_from_user(&csv, argp, sizeof(csv))) 841daa6eda6SGerd Knorr return -EFAULT; 842daa6eda6SGerd Knorr 843daa6eda6SGerd Knorr if (0 != ch_checkrange(ch, csv.csv_type, csv.csv_unit)) { 84487da3235SJoe Perches DPRINTK("CHIOSVOLTAG: invalid parameter\n"); 845daa6eda6SGerd Knorr return -EBADSLT; 846daa6eda6SGerd Knorr } 847daa6eda6SGerd Knorr elem = ch->firsts[csv.csv_type] + csv.csv_unit; 8480b950672SArjan van de Ven mutex_lock(&ch->lock); 849daa6eda6SGerd Knorr retval = ch_set_voltag(ch, elem, 850daa6eda6SGerd Knorr csv.csv_flags & CSV_AVOLTAG, 851daa6eda6SGerd Knorr csv.csv_flags & CSV_CLEARTAG, 852daa6eda6SGerd Knorr csv.csv_voltag); 8530b950672SArjan van de Ven mutex_unlock(&ch->lock); 854daa6eda6SGerd Knorr return retval; 855daa6eda6SGerd Knorr } 856daa6eda6SGerd Knorr 857daa6eda6SGerd Knorr default: 858fe08ac31Sviro@ZenIV.linux.org.uk return scsi_ioctl(ch->device, cmd, argp); 859daa6eda6SGerd Knorr 860daa6eda6SGerd Knorr } 861daa6eda6SGerd Knorr } 862daa6eda6SGerd Knorr 863daa6eda6SGerd Knorr #ifdef CONFIG_COMPAT 864daa6eda6SGerd Knorr 865daa6eda6SGerd Knorr struct changer_element_status32 { 866daa6eda6SGerd Knorr int ces_type; 867daa6eda6SGerd Knorr compat_uptr_t ces_data; 868daa6eda6SGerd Knorr }; 869daa6eda6SGerd Knorr #define CHIOGSTATUS32 _IOW('c', 8,struct changer_element_status32) 870daa6eda6SGerd Knorr 871daa6eda6SGerd Knorr static long ch_ioctl_compat(struct file * file, 872daa6eda6SGerd Knorr unsigned int cmd, unsigned long arg) 873daa6eda6SGerd Knorr { 874daa6eda6SGerd Knorr scsi_changer *ch = file->private_data; 875daa6eda6SGerd Knorr 876daa6eda6SGerd Knorr switch (cmd) { 877daa6eda6SGerd Knorr case CHIOGPARAMS: 878daa6eda6SGerd Knorr case CHIOGVPARAMS: 879daa6eda6SGerd Knorr case CHIOPOSITION: 880daa6eda6SGerd Knorr case CHIOMOVE: 881daa6eda6SGerd Knorr case CHIOEXCHANGE: 882daa6eda6SGerd Knorr case CHIOGELEM: 883daa6eda6SGerd Knorr case CHIOINITELEM: 884daa6eda6SGerd Knorr case CHIOSVOLTAG: 885daa6eda6SGerd Knorr /* compatible */ 886f7fea185SMathieu Segaud return ch_ioctl(file, cmd, arg); 887daa6eda6SGerd Knorr case CHIOGSTATUS32: 888daa6eda6SGerd Knorr { 889daa6eda6SGerd Knorr struct changer_element_status32 ces32; 890fe08ac31Sviro@ZenIV.linux.org.uk unsigned char __user *data; 891daa6eda6SGerd Knorr 892fe08ac31Sviro@ZenIV.linux.org.uk if (copy_from_user(&ces32, (void __user *)arg, sizeof (ces32))) 893daa6eda6SGerd Knorr return -EFAULT; 894daa6eda6SGerd Knorr if (ces32.ces_type < 0 || ces32.ces_type >= CH_TYPES) 895daa6eda6SGerd Knorr return -EINVAL; 896daa6eda6SGerd Knorr 897daa6eda6SGerd Knorr data = compat_ptr(ces32.ces_data); 898daa6eda6SGerd Knorr return ch_gstatus(ch, ces32.ces_type, data); 899daa6eda6SGerd Knorr } 900daa6eda6SGerd Knorr default: 901daa6eda6SGerd Knorr // return scsi_ioctl_compat(ch->device, cmd, (void*)arg); 902daa6eda6SGerd Knorr return -ENOIOCTLCMD; 903daa6eda6SGerd Knorr 904daa6eda6SGerd Knorr } 905daa6eda6SGerd Knorr } 906daa6eda6SGerd Knorr #endif 907daa6eda6SGerd Knorr 908daa6eda6SGerd Knorr /* ------------------------------------------------------------------------ */ 909daa6eda6SGerd Knorr 910daa6eda6SGerd Knorr static int ch_probe(struct device *dev) 911daa6eda6SGerd Knorr { 912daa6eda6SGerd Knorr struct scsi_device *sd = to_scsi_device(dev); 913ee959b00STony Jones struct device *class_dev; 914b98c52b5STejun Heo int ret; 915daa6eda6SGerd Knorr scsi_changer *ch; 916daa6eda6SGerd Knorr 917daa6eda6SGerd Knorr if (sd->type != TYPE_MEDIUM_CHANGER) 918daa6eda6SGerd Knorr return -ENODEV; 919daa6eda6SGerd Knorr 9204530a169Svignesh.babu@wipro.com ch = kzalloc(sizeof(*ch), GFP_KERNEL); 921daa6eda6SGerd Knorr if (NULL == ch) 922daa6eda6SGerd Knorr return -ENOMEM; 923daa6eda6SGerd Knorr 924b98c52b5STejun Heo idr_preload(GFP_KERNEL); 925da707c54SFUJITA Tomonori spin_lock(&ch_index_lock); 926b98c52b5STejun Heo ret = idr_alloc(&ch_index_idr, ch, 0, CH_MAX_DEVS + 1, GFP_NOWAIT); 927da707c54SFUJITA Tomonori spin_unlock(&ch_index_lock); 928b98c52b5STejun Heo idr_preload_end(); 929da707c54SFUJITA Tomonori 930b98c52b5STejun Heo if (ret < 0) { 931b98c52b5STejun Heo if (ret == -ENOSPC) 932da707c54SFUJITA Tomonori ret = -ENODEV; 933b98c52b5STejun Heo goto free_ch; 934da707c54SFUJITA Tomonori } 935da707c54SFUJITA Tomonori 936b98c52b5STejun Heo ch->minor = ret; 937daa6eda6SGerd Knorr sprintf(ch->name,"ch%d",ch->minor); 938a3d2c2e8SFUJITA Tomonori 939d73a1a67SGreg Kroah-Hartman class_dev = device_create(ch_sysfs_class, dev, 940d73a1a67SGreg Kroah-Hartman MKDEV(SCSI_CHANGER_MAJOR, ch->minor), ch, 941d73a1a67SGreg Kroah-Hartman "s%s", ch->name); 942a3d2c2e8SFUJITA Tomonori if (IS_ERR(class_dev)) { 94328c31729SHannes Reinecke sdev_printk(KERN_WARNING, sd, "ch%d: device_create failed\n", 944a3d2c2e8SFUJITA Tomonori ch->minor); 945da707c54SFUJITA Tomonori ret = PTR_ERR(class_dev); 946da707c54SFUJITA Tomonori goto remove_idr; 947a3d2c2e8SFUJITA Tomonori } 948a3d2c2e8SFUJITA Tomonori 9490b950672SArjan van de Ven mutex_init(&ch->lock); 950085e5676SHannes Reinecke kref_init(&ch->ref); 951daa6eda6SGerd Knorr ch->device = sd; 952085e5676SHannes Reinecke ret = ch_readconfig(ch); 953085e5676SHannes Reinecke if (ret) 954085e5676SHannes Reinecke goto destroy_dev; 955daa6eda6SGerd Knorr if (init) 956daa6eda6SGerd Knorr ch_init_elem(ch); 957daa6eda6SGerd Knorr 9583d164fb0SFUJITA Tomonori dev_set_drvdata(dev, ch); 959017560fcSJeff Garzik sdev_printk(KERN_INFO, sd, "Attached scsi changer %s\n", ch->name); 960daa6eda6SGerd Knorr 961daa6eda6SGerd Knorr return 0; 962085e5676SHannes Reinecke destroy_dev: 963085e5676SHannes Reinecke device_destroy(ch_sysfs_class, MKDEV(SCSI_CHANGER_MAJOR, ch->minor)); 964da707c54SFUJITA Tomonori remove_idr: 965b98c52b5STejun Heo idr_remove(&ch_index_idr, ch->minor); 966da707c54SFUJITA Tomonori free_ch: 967da707c54SFUJITA Tomonori kfree(ch); 968da707c54SFUJITA Tomonori return ret; 969daa6eda6SGerd Knorr } 970daa6eda6SGerd Knorr 971daa6eda6SGerd Knorr static int ch_remove(struct device *dev) 972daa6eda6SGerd Knorr { 973da707c54SFUJITA Tomonori scsi_changer *ch = dev_get_drvdata(dev); 974daa6eda6SGerd Knorr 975da707c54SFUJITA Tomonori spin_lock(&ch_index_lock); 976da707c54SFUJITA Tomonori idr_remove(&ch_index_idr, ch->minor); 977da707c54SFUJITA Tomonori spin_unlock(&ch_index_lock); 978daa6eda6SGerd Knorr 979ee959b00STony Jones device_destroy(ch_sysfs_class, MKDEV(SCSI_CHANGER_MAJOR,ch->minor)); 980085e5676SHannes Reinecke kref_put(&ch->ref, ch_destroy); 981daa6eda6SGerd Knorr return 0; 982daa6eda6SGerd Knorr } 983daa6eda6SGerd Knorr 9845aa22af3SFUJITA Tomonori static struct scsi_driver ch_template = { 9855aa22af3SFUJITA Tomonori .gendrv = { 9865aa22af3SFUJITA Tomonori .name = "ch", 9873af6b352SChristoph Hellwig .owner = THIS_MODULE, 9885aa22af3SFUJITA Tomonori .probe = ch_probe, 9895aa22af3SFUJITA Tomonori .remove = ch_remove, 9905aa22af3SFUJITA Tomonori }, 9915aa22af3SFUJITA Tomonori }; 9925aa22af3SFUJITA Tomonori 9935aa22af3SFUJITA Tomonori static const struct file_operations changer_fops = { 9945aa22af3SFUJITA Tomonori .owner = THIS_MODULE, 9955aa22af3SFUJITA Tomonori .open = ch_open, 9965aa22af3SFUJITA Tomonori .release = ch_release, 9975aa22af3SFUJITA Tomonori .unlocked_ioctl = ch_ioctl, 9985aa22af3SFUJITA Tomonori #ifdef CONFIG_COMPAT 9995aa22af3SFUJITA Tomonori .compat_ioctl = ch_ioctl_compat, 10005aa22af3SFUJITA Tomonori #endif 10016038f373SArnd Bergmann .llseek = noop_llseek, 10025aa22af3SFUJITA Tomonori }; 10035aa22af3SFUJITA Tomonori 1004daa6eda6SGerd Knorr static int __init init_ch_module(void) 1005daa6eda6SGerd Knorr { 1006daa6eda6SGerd Knorr int rc; 1007daa6eda6SGerd Knorr 1008daa6eda6SGerd Knorr printk(KERN_INFO "SCSI Media Changer driver v" VERSION " \n"); 100921feb5ccSGerd Knorr ch_sysfs_class = class_create(THIS_MODULE, "scsi_changer"); 1010daa6eda6SGerd Knorr if (IS_ERR(ch_sysfs_class)) { 1011daa6eda6SGerd Knorr rc = PTR_ERR(ch_sysfs_class); 1012daa6eda6SGerd Knorr return rc; 1013daa6eda6SGerd Knorr } 1014daa6eda6SGerd Knorr rc = register_chrdev(SCSI_CHANGER_MAJOR,"ch",&changer_fops); 1015daa6eda6SGerd Knorr if (rc < 0) { 1016daa6eda6SGerd Knorr printk("Unable to get major %d for SCSI-Changer\n", 1017daa6eda6SGerd Knorr SCSI_CHANGER_MAJOR); 1018daa6eda6SGerd Knorr goto fail1; 1019daa6eda6SGerd Knorr } 1020daa6eda6SGerd Knorr rc = scsi_register_driver(&ch_template.gendrv); 1021daa6eda6SGerd Knorr if (rc < 0) 1022daa6eda6SGerd Knorr goto fail2; 1023daa6eda6SGerd Knorr return 0; 1024daa6eda6SGerd Knorr 1025daa6eda6SGerd Knorr fail2: 1026daa6eda6SGerd Knorr unregister_chrdev(SCSI_CHANGER_MAJOR, "ch"); 1027daa6eda6SGerd Knorr fail1: 102821feb5ccSGerd Knorr class_destroy(ch_sysfs_class); 1029daa6eda6SGerd Knorr return rc; 1030daa6eda6SGerd Knorr } 1031daa6eda6SGerd Knorr 1032daa6eda6SGerd Knorr static void __exit exit_ch_module(void) 1033daa6eda6SGerd Knorr { 1034daa6eda6SGerd Knorr scsi_unregister_driver(&ch_template.gendrv); 1035daa6eda6SGerd Knorr unregister_chrdev(SCSI_CHANGER_MAJOR, "ch"); 103621feb5ccSGerd Knorr class_destroy(ch_sysfs_class); 1037da707c54SFUJITA Tomonori idr_destroy(&ch_index_idr); 1038daa6eda6SGerd Knorr } 1039daa6eda6SGerd Knorr 1040daa6eda6SGerd Knorr module_init(init_ch_module); 1041daa6eda6SGerd Knorr module_exit(exit_ch_module); 1042daa6eda6SGerd Knorr 1043daa6eda6SGerd Knorr /* 1044daa6eda6SGerd Knorr * Local variables: 1045daa6eda6SGerd Knorr * c-basic-offset: 8 1046daa6eda6SGerd Knorr * End: 1047daa6eda6SGerd Knorr */ 1048