2 SCSI Tape Driver for Linux version 1.1 and newer. See the accompanying 3 file README.st for more information. 6 Rewritten from Dwayne Forsyth's SCSI tape driver by Kai Makisara. 7 Contribution and ideas from several people including (in alphabetical 8 order) Klaus Ehrenfried, Wolfgang Denk, Steve Hirsch, Andreas Koppenh"ofer, 9 Michael Leodolter, Eyal Lebedinsky, J"org Weule, and Eric Youngdale. 11 Copyright 1992 - 1999 Kai Makisara 12 email Kai.Makisara@metla.fi 14 Last modified: Sat Aug 7 13:54:31 1999 by makisara@kai.makisara.local 15 Some small formal changes - aeb, 950809 18 #include <linux/module.h> 21 #include <linux/kernel.h> 22 #include <linux/sched.h> 24 #include <linux/init.h> 25 #include <linux/string.h> 26 #include <linux/errno.h> 27 #include <linux/mtio.h> 28 #include <linux/ioctl.h> 29 #include <linux/fcntl.h> 30 #include <linux/spinlock.h> 31 #include <asm/uaccess.h> 33 #include <asm/system.h> 35 /* The driver prints some debugging information on the console if DEBUG 36 is defined and non-zero. */ 39 /* The message level for the debug messages is currently set to KERN_NOTICE 40 so that people can easily see the messages. Later when the debugging messages 41 in the drivers are more widely classified, this may be changed to KERN_DEBUG. */ 42 #define ST_DEB_MSG KERN_NOTICE 44 #define MAJOR_NR SCSI_TAPE_MAJOR 45 #include <linux/blk.h> 49 #include <scsi/scsi_ioctl.h> 51 #define ST_KILOBYTE 1024 53 #include"st_options.h" 58 static int buffer_kbs
=0; 59 static int write_threshold_kbs
=0; 60 static int max_buffers
= (-1); 61 static int max_sg_segs
=0; 63 MODULE_AUTHOR("Kai Makisara"); 64 MODULE_DESCRIPTION("SCSI Tape Driver"); 65 MODULE_PARM(buffer_kbs
,"i"); 66 MODULE_PARM(write_threshold_kbs
,"i"); 67 MODULE_PARM(max_buffers
,"i"); 68 MODULE_PARM(max_sg_segs
,"i"); 71 static struct st_dev_parm
{ 74 } parms
[] __initdata
= { 76 "buffer_kbs", &buffer_kbs
79 "write_threshold_kbs", &write_threshold_kbs
82 "max_buffers", &max_buffers
85 "max_sg_segs", &max_sg_segs
92 /* The default definitions have been moved to st_options.h */ 94 #define ST_BUFFER_SIZE (ST_BUFFER_BLOCKS * ST_KILOBYTE) 95 #define ST_WRITE_THRESHOLD (ST_WRITE_THRESHOLD_BLOCKS * ST_KILOBYTE) 97 /* The buffer size should fit into the 24 bits for length in the 98 6-byte SCSI read and write commands. */ 99 #if ST_BUFFER_SIZE >= (2 << 24 - 1) 100 #error"Buffer size should not exceed (2 << 24 - 1) bytes!" 104 static int debugging
=1; 107 #define MAX_RETRIES 0 108 #define MAX_WRITE_RETRIES 0 109 #define MAX_READY_RETRIES 5 110 #define NO_TAPE NOT_READY 112 #define ST_TIMEOUT (900 * HZ) 113 #define ST_LONG_TIMEOUT (14000 * HZ) 115 #define TAPE_NR(x) (MINOR(x) & ~(128 | ST_MODE_MASK)) 116 #define TAPE_MODE(x) ((MINOR(x) & ST_MODE_MASK) >> ST_MODE_SHIFT) 118 /* Internal ioctl to set both density (uppermost 8 bits) and blocksize (lower 120 #define SET_DENS_AND_BLK 0x10001 122 static int st_nbr_buffers
; 123 static ST_buffer
**st_buffers
; 124 static int st_buffer_size
= ST_BUFFER_SIZE
; 125 static int st_write_threshold
= ST_WRITE_THRESHOLD
; 126 static int st_max_buffers
= ST_MAX_BUFFERS
; 127 static int st_max_sg_segs
= ST_MAX_SG
; 129 static Scsi_Tape
*scsi_tapes
= NULL
; 131 static int modes_defined
= FALSE
; 133 static ST_buffer
*new_tape_buffer(int,int); 134 static intenlarge_buffer(ST_buffer
*,int,int); 135 static voidnormalize_buffer(ST_buffer
*); 136 static intappend_to_buffer(const char*, ST_buffer
*,int); 137 static intfrom_buffer(ST_buffer
*,char*,int); 139 static intst_init(void); 140 static intst_attach(Scsi_Device
*); 141 static intst_detect(Scsi_Device
*); 142 static voidst_detach(Scsi_Device
*); 144 struct Scsi_Device_Template st_template
= 145 {NULL
,"tape","st", NULL
, TYPE_TAPE
, 146 SCSI_TAPE_MAJOR
,0,0,0,0, 148 NULL
, st_attach
, st_detach
}; 150 static intst_compression(Scsi_Tape
*,int); 152 static intfind_partition(struct inode
*); 153 static intupdate_partition(struct inode
*); 155 static intst_int_ioctl(struct inode
*inode
,unsigned int cmd_in
, 161 /* Convert the result to success code */ 162 static intst_chk_result(Scsi_Cmnd
* SCpnt
) 164 int dev
=TAPE_NR(SCpnt
->request
.rq_dev
); 165 int result
= SCpnt
->result
; 166 unsigned char*sense
= SCpnt
->sense_buffer
, scode
; 171 if(!result
/* && SCpnt->sense_buffer[0] == 0 */) 174 if(driver_byte(result
) & DRIVER_SENSE
) 175 scode
= sense
[2] &0x0f; 177 sense
[0] =0;/* We don't have sense data if this byte is zero */ 183 printk(ST_DEB_MSG
"st%d: Error: %x, cmd: %x %x %x %x %x %x Len: %d\n", 185 SCpnt
->data_cmnd
[0], SCpnt
->data_cmnd
[1], SCpnt
->data_cmnd
[2], 186 SCpnt
->data_cmnd
[3], SCpnt
->data_cmnd
[4], SCpnt
->data_cmnd
[5], 187 SCpnt
->request_bufflen
); 188 if(driver_byte(result
) & DRIVER_SENSE
) 189 print_sense("st", SCpnt
); 192 if(!(driver_byte(result
) & DRIVER_SENSE
) || 193 ((sense
[0] &0x70) ==0x70&& 195 scode
!= RECOVERED_ERROR
&& 196 /* scode != UNIT_ATTENTION && */ 197 scode
!= BLANK_CHECK
&& 198 scode
!= VOLUME_OVERFLOW
&& 199 SCpnt
->data_cmnd
[0] != MODE_SENSE
&& 200 SCpnt
->data_cmnd
[0] != TEST_UNIT_READY
)) {/* Abnormal conditions for tape */ 201 if(driver_byte(result
) & DRIVER_SENSE
) { 202 printk(KERN_WARNING
"st%d: Error with sense data: ", dev
); 203 print_sense("st", SCpnt
); 206 "st%d: Error %x (sugg. bt 0x%x, driver bt 0x%x, host bt 0x%x).\n", 207 dev
, result
,suggestion(result
),driver_byte(result
) & DRIVER_MASK
, 210 if((sense
[0] &0x70) ==0x70&& 211 scode
== RECOVERED_ERROR
212 #if ST_RECOVERED_WRITE_FATAL 213 && SCpnt
->data_cmnd
[0] != WRITE_6
214 && SCpnt
->data_cmnd
[0] != WRITE_FILEMARKS
217 scsi_tapes
[dev
].recover_count
++; 218 scsi_tapes
[dev
].mt_status
->mt_erreg
+= (1<< MT_ST_SOFTERR_SHIFT
); 221 if(SCpnt
->data_cmnd
[0] == READ_6
) 223 else if(SCpnt
->data_cmnd
[0] == WRITE_6
) 227 printk(ST_DEB_MSG
"st%d: Recovered %s error (%d).\n", dev
, stp
, 228 scsi_tapes
[dev
].recover_count
); 231 if((sense
[2] &0xe0) ==0) 238 /* Wakeup from interrupt */ 239 static voidst_sleep_done(Scsi_Cmnd
* SCpnt
) 245 if((st_nbr
=TAPE_NR(SCpnt
->request
.rq_dev
)) < st_template
.nr_dev
) { 246 STp
= &(scsi_tapes
[st_nbr
]); 247 if((STp
->buffer
)->writing
&& 248 (SCpnt
->sense_buffer
[0] &0x70) ==0x70&& 249 (SCpnt
->sense_buffer
[2] &0x40)) { 250 /* EOM at write-behind, has all been written? */ 251 if((SCpnt
->sense_buffer
[0] &0x80) !=0) 252 remainder
= (SCpnt
->sense_buffer
[3] <<24) | 253 (SCpnt
->sense_buffer
[4] <<16) | 254 (SCpnt
->sense_buffer
[5] <<8) | SCpnt
->sense_buffer
[6]; 257 if((SCpnt
->sense_buffer
[2] &0x0f) == VOLUME_OVERFLOW
|| 259 (STp
->buffer
)->last_result
= SCpnt
->result
;/* Error */ 261 (STp
->buffer
)->last_result
= INT_MAX
;/* OK */ 263 (STp
->buffer
)->last_result
= SCpnt
->result
; 264 SCpnt
->request
.rq_status
= RQ_SCSI_DONE
; 265 (STp
->buffer
)->last_SCpnt
= SCpnt
; 268 STp
->write_pending
=0; 270 up(SCpnt
->request
.sem
); 274 printk(KERN_ERR
"st?: Illegal interrupt device %x\n", st_nbr
); 279 /* Do the scsi command. Waits until command performed if do_wait is true. 280 Otherwise write_behind_check() is used to check that the command 283 st_do_scsi(Scsi_Cmnd
* SCpnt
, Scsi_Tape
* STp
,unsigned char*cmd
,int bytes
, 284 int timeout
,int retries
,int do_wait
) 289 spin_lock_irqsave(&io_request_lock
, flags
); 291 if((SCpnt
=scsi_allocate_device(NULL
, STp
->device
,1)) == NULL
) { 292 printk(KERN_ERR
"st%d: Can't get SCSI request.\n",TAPE_NR(STp
->devt
)); 293 spin_unlock_irqrestore(&io_request_lock
, flags
); 296 cmd
[1] |= (SCpnt
->lun
<<5) &0xe0; 297 init_MUTEX_LOCKED(&STp
->sem
); 298 SCpnt
->use_sg
= (bytes
> (STp
->buffer
)->sg
[0].length
) ? 299 (STp
->buffer
)->use_sg
:0; 301 bp
= (char*) &((STp
->buffer
)->sg
[0]); 302 if((STp
->buffer
)->sg_segs
< SCpnt
->use_sg
) 303 SCpnt
->use_sg
= (STp
->buffer
)->sg_segs
; 305 bp
= (STp
->buffer
)->b_data
; 307 SCpnt
->request
.sem
= &(STp
->sem
); 308 SCpnt
->request
.rq_status
= RQ_SCSI_BUSY
; 309 SCpnt
->request
.rq_dev
= STp
->devt
; 311 scsi_do_cmd(SCpnt
, (void*) cmd
, bp
, bytes
, 312 st_sleep_done
, timeout
, retries
); 313 spin_unlock_irqrestore(&io_request_lock
, flags
); 316 down(SCpnt
->request
.sem
); 318 (STp
->buffer
)->last_result_fatal
=st_chk_result(SCpnt
); 324 /* Handle the write-behind checking (downs the semaphore) */ 325 static voidwrite_behind_check(Scsi_Tape
* STp
) 330 STbuffer
= STp
->buffer
; 333 if(STp
->write_pending
) 341 (STp
->buffer
)->last_result_fatal
=st_chk_result((STp
->buffer
)->last_SCpnt
); 342 scsi_release_command((STp
->buffer
)->last_SCpnt
); 344 if(STbuffer
->writing
< STbuffer
->buffer_bytes
) 346 memcpy(STbuffer
->b_data
, 347 STbuffer
->b_data
+ STbuffer
->writing
, 348 STbuffer
->buffer_bytes
- STbuffer
->writing
); 350 printk(KERN_WARNING
"st: write_behind_check: something left in buffer!\n"); 352 STbuffer
->buffer_bytes
-= STbuffer
->writing
; 353 STps
= &(STp
->ps
[STp
->partition
]); 354 if(STps
->drv_block
>=0) { 355 if(STp
->block_size
==0) 358 STps
->drv_block
+= STbuffer
->writing
/ STp
->block_size
; 360 STbuffer
->writing
=0; 366 /* Step over EOF if it has been inadvertently crossed (ioctl not used because 367 it messes up the block number). */ 368 static intcross_eof(Scsi_Tape
* STp
,int forward
) 371 unsigned char cmd
[10]; 374 cmd
[1] =0x01;/* Space FileMarks */ 379 cmd
[2] = cmd
[3] = cmd
[4] =0xff;/* -1 filemarks */ 383 printk(ST_DEB_MSG
"st%d: Stepping over filemark %s.\n", 384 TAPE_NR(STp
->devt
), forward
?"forward":"backward"); 387 SCpnt
=st_do_scsi(NULL
, STp
, cmd
,0, STp
->timeout
, MAX_RETRIES
, TRUE
); 391 scsi_release_command(SCpnt
); 394 if((STp
->buffer
)->last_result
!=0) 395 printk(KERN_ERR
"st%d: Stepping over filemark %s failed.\n", 396 TAPE_NR(STp
->devt
), forward
?"forward":"backward"); 398 return(STp
->buffer
)->last_result_fatal
; 402 /* Flush the write buffer (never need to write if variable blocksize). */ 403 static intflush_write_buffer(Scsi_Tape
* STp
) 405 int offset
, transfer
, blks
; 407 unsigned char cmd
[10]; 411 if((STp
->buffer
)->writing
) { 412 write_behind_check(STp
); 413 if((STp
->buffer
)->last_result_fatal
) { 416 printk(ST_DEB_MSG
"st%d: Async write error (flush) %x.\n", 417 TAPE_NR(STp
->devt
), (STp
->buffer
)->last_result
); 419 if((STp
->buffer
)->last_result
== INT_MAX
) 424 if(STp
->block_size
==0) 430 offset
= (STp
->buffer
)->buffer_bytes
; 431 transfer
= ((offset
+ STp
->block_size
-1) / 432 STp
->block_size
) * STp
->block_size
; 435 printk(ST_DEB_MSG
"st%d: Flushing %d bytes.\n",TAPE_NR(STp
->devt
), transfer
); 437 memset((STp
->buffer
)->b_data
+ offset
,0, transfer
- offset
); 442 blks
= transfer
/ STp
->block_size
; 447 SCpnt
=st_do_scsi(NULL
, STp
, cmd
, transfer
, STp
->timeout
, MAX_WRITE_RETRIES
, 452 STps
= &(STp
->ps
[STp
->partition
]); 453 if((STp
->buffer
)->last_result_fatal
!=0) { 454 if((SCpnt
->sense_buffer
[0] &0x70) ==0x70&& 455 (SCpnt
->sense_buffer
[2] &0x40) && 456 (SCpnt
->sense_buffer
[2] &0x0f) == NO_SENSE
) { 458 (STp
->buffer
)->buffer_bytes
=0; 461 printk(KERN_ERR
"st%d: Error on flush.\n",TAPE_NR(STp
->devt
)); 464 STps
->drv_block
= (-1); 466 if(STps
->drv_block
>=0) 467 STps
->drv_block
+= blks
; 469 (STp
->buffer
)->buffer_bytes
=0; 471 scsi_release_command(SCpnt
); 478 /* Flush the tape buffer. The tape will be positioned correctly unless 479 seek_next is true. */ 480 static intflush_buffer(struct inode
*inode
,struct file
*filp
,int seek_next
) 482 int backspace
, result
; 486 int dev
=TAPE_NR(inode
->i_rdev
); 488 STp
= &(scsi_tapes
[dev
]); 489 STbuffer
= STp
->buffer
; 492 * If there was a bus reset, block further access 495 if(STp
->device
->was_reset
) 498 if(STp
->ready
!= ST_READY
) 501 STps
= &(STp
->ps
[STp
->partition
]); 502 if(STps
->rw
== ST_WRITING
)/* Writing */ 503 returnflush_write_buffer(STp
); 505 if(STp
->block_size
==0) 508 backspace
= ((STp
->buffer
)->buffer_bytes
+ 509 (STp
->buffer
)->read_pointer
) / STp
->block_size
- 510 ((STp
->buffer
)->read_pointer
+ STp
->block_size
-1) / 512 (STp
->buffer
)->buffer_bytes
=0; 513 (STp
->buffer
)->read_pointer
=0; 516 if(STps
->eof
== ST_FM_HIT
) { 517 result
=cross_eof(STp
, FALSE
);/* Back over the EOF hit */ 519 STps
->eof
= ST_NOEOF
; 521 if(STps
->drv_file
>=0) 526 if(!result
&& backspace
>0) 527 result
=st_int_ioctl(inode
, MTBSR
, backspace
); 528 }else if(STps
->eof
== ST_FM_HIT
) { 529 if(STps
->drv_file
>=0) 532 STps
->eof
= ST_NOEOF
; 538 /* Set the mode parameters */ 539 static intset_mode_densblk(struct inode
*inode
, Scsi_Tape
* STp
, ST_mode
* STm
) 543 int dev
=TAPE_NR(inode
->i_rdev
); 545 if(!STp
->density_changed
&& 546 STm
->default_density
>=0&& 547 STm
->default_density
!= STp
->density
) { 548 arg
= STm
->default_density
; 552 arg
<<= MT_ST_DENSITY_SHIFT
; 553 if(!STp
->blksize_changed
&& 554 STm
->default_blksize
>=0&& 555 STm
->default_blksize
!= STp
->block_size
) { 556 arg
|= STm
->default_blksize
; 559 arg
|= STp
->block_size
; 561 st_int_ioctl(inode
, SET_DENS_AND_BLK
, arg
)) { 563 "st%d: Can't set default block size to %d bytes and density %x.\n", 564 dev
, STm
->default_blksize
, STm
->default_density
); 572 /* Open the device */ 573 static intscsi_tape_open(struct inode
*inode
,struct file
*filp
) 575 unsigned short flags
; 576 int i
, need_dma_buffer
, new_session
= FALSE
; 577 unsigned char cmd
[10]; 582 int dev
=TAPE_NR(inode
->i_rdev
); 583 int mode
=TAPE_MODE(inode
->i_rdev
); 585 if(dev
>= st_template
.dev_max
|| !scsi_tapes
[dev
].device
) 588 if(!scsi_block_when_processing_errors(scsi_tapes
[dev
].device
)) { 591 STp
= &(scsi_tapes
[dev
]); 594 printk(ST_DEB_MSG
"st%d: Device already in use.\n", dev
); 598 STp
->rew_at_close
= (MINOR(inode
->i_rdev
) &0x80) ==0; 600 if(mode
!= STp
->current_mode
) { 603 printk(ST_DEB_MSG
"st%d: Mode change from %d to %d.\n", 604 dev
, STp
->current_mode
, mode
); 607 STp
->current_mode
= mode
; 609 STm
= &(STp
->modes
[STp
->current_mode
]); 611 /* Allocate a buffer for this user */ 612 need_dma_buffer
= STp
->restr_dma
; 613 for(i
=0; i
< st_nbr_buffers
; i
++) 614 if(!st_buffers
[i
]->in_use
&& 615 (!need_dma_buffer
|| st_buffers
[i
]->dma
)) 617 if(i
>= st_nbr_buffers
) { 618 STp
->buffer
=new_tape_buffer(FALSE
, need_dma_buffer
); 619 if(STp
->buffer
== NULL
) { 620 printk(KERN_WARNING
"st%d: Can't allocate tape buffer.\n", dev
); 624 STp
->buffer
= st_buffers
[i
]; 625 (STp
->buffer
)->in_use
=1; 626 (STp
->buffer
)->writing
=0; 627 (STp
->buffer
)->last_result_fatal
=0; 628 (STp
->buffer
)->use_sg
= STp
->device
->host
->sg_tablesize
; 630 /* Compute the usable buffer size for this SCSI adapter */ 631 if(!(STp
->buffer
)->use_sg
) 632 (STp
->buffer
)->buffer_size
= (STp
->buffer
)->sg
[0].length
; 634 for(i
=0, (STp
->buffer
)->buffer_size
=0; i
< (STp
->buffer
)->use_sg
&& 635 i
< (STp
->buffer
)->sg_segs
; i
++) 636 (STp
->buffer
)->buffer_size
+= (STp
->buffer
)->sg
[i
].length
; 639 flags
= filp
->f_flags
; 640 STp
->write_prot
= ((flags
& O_ACCMODE
) == O_RDONLY
); 643 for(i
=0; i
< ST_NBR_PARTITIONS
; i
++) { 644 STps
= &(STp
->ps
[i
]); 647 STp
->ready
= ST_READY
; 648 STp
->recover_count
=0; 650 STp
->nbr_waits
= STp
->nbr_finished
=0; 653 if(scsi_tapes
[dev
].device
->host
->hostt
->module
) 654 __MOD_INC_USE_COUNT(scsi_tapes
[dev
].device
->host
->hostt
->module
); 655 if(st_template
.module
) 656 __MOD_INC_USE_COUNT(st_template
.module
); 658 memset((void*) &cmd
[0],0,10); 659 cmd
[0] = TEST_UNIT_READY
; 661 SCpnt
=st_do_scsi(NULL
, STp
, cmd
,0, STp
->long_timeout
, MAX_READY_RETRIES
, 664 if(scsi_tapes
[dev
].device
->host
->hostt
->module
) 665 __MOD_DEC_USE_COUNT(scsi_tapes
[dev
].device
->host
->hostt
->module
); 666 if(st_template
.module
) 667 __MOD_DEC_USE_COUNT(st_template
.module
); 670 if((SCpnt
->sense_buffer
[0] &0x70) ==0x70&& 671 (SCpnt
->sense_buffer
[2] &0x0f) == UNIT_ATTENTION
) {/* New media? */ 672 memset((void*) &cmd
[0],0,10); 673 cmd
[0] = TEST_UNIT_READY
; 675 SCpnt
=st_do_scsi(SCpnt
, STp
, cmd
,0, STp
->long_timeout
, MAX_READY_RETRIES
, 678 (STp
->device
)->was_reset
=0; 679 STp
->partition
= STp
->new_partition
=0; 680 if(STp
->can_partitions
) 681 STp
->nbr_partitions
=1;/* This guess will be updated later if necessary */ 682 for(i
=0; i
< ST_NBR_PARTITIONS
; i
++) { 683 STps
= &(STp
->ps
[i
]); 685 STps
->eof
= ST_NOEOF
; 687 STps
->last_block_valid
= FALSE
; 693 if((STp
->buffer
)->last_result_fatal
!=0) { 694 if((STp
->device
)->scsi_level
>= SCSI_2
&& 695 (SCpnt
->sense_buffer
[0] &0x70) ==0x70&& 696 (SCpnt
->sense_buffer
[2] &0x0f) == NOT_READY
&& 697 SCpnt
->sense_buffer
[12] ==0x3a) {/* Check ASC */ 698 STp
->ready
= ST_NO_TAPE
; 700 STp
->ready
= ST_NOT_READY
; 701 scsi_release_command(SCpnt
); 703 STp
->density
=0;/* Clear the erroneous "residue" */ 706 STp
->ps
[0].drv_file
= STp
->ps
[0].drv_block
= (-1); 707 STp
->partition
= STp
->new_partition
=0; 708 STp
->door_locked
= ST_UNLOCKED
; 712 if(STp
->omit_blklims
) 713 STp
->min_block
= STp
->max_block
= (-1); 715 memset((void*) &cmd
[0],0,10); 716 cmd
[0] = READ_BLOCK_LIMITS
; 718 SCpnt
=st_do_scsi(SCpnt
, STp
, cmd
,6, STp
->timeout
, MAX_READY_RETRIES
, TRUE
); 720 if(!SCpnt
->result
&& !SCpnt
->sense_buffer
[0]) { 721 STp
->max_block
= ((STp
->buffer
)->b_data
[1] <<16) | 722 ((STp
->buffer
)->b_data
[2] <<8) | (STp
->buffer
)->b_data
[3]; 723 STp
->min_block
= ((STp
->buffer
)->b_data
[4] <<8) | 724 (STp
->buffer
)->b_data
[5]; 727 printk(ST_DEB_MSG
"st%d: Block limits %d - %d bytes.\n", dev
, STp
->min_block
, 731 STp
->min_block
= STp
->max_block
= (-1); 734 printk(ST_DEB_MSG
"st%d: Can't read block limits.\n", dev
); 739 memset((void*) &cmd
[0],0,10); 743 SCpnt
=st_do_scsi(SCpnt
, STp
, cmd
,12, STp
->timeout
, MAX_READY_RETRIES
, TRUE
); 745 if((STp
->buffer
)->last_result_fatal
!=0) { 748 printk(ST_DEB_MSG
"st%d: No Mode Sense.\n", dev
); 750 STp
->block_size
= ST_DEFAULT_BLOCK
;/* Educated guess (?) */ 751 (STp
->buffer
)->last_result_fatal
=0;/* Prevent error propagation */ 752 STp
->drv_write_prot
=0; 757 printk(ST_DEB_MSG
"st%d: Mode sense. Length %d, medium %x, WBS %x, BLL %d\n", 759 (STp
->buffer
)->b_data
[0], (STp
->buffer
)->b_data
[1], 760 (STp
->buffer
)->b_data
[2], (STp
->buffer
)->b_data
[3]); 763 if((STp
->buffer
)->b_data
[3] >=8) { 764 STp
->drv_buffer
= ((STp
->buffer
)->b_data
[2] >>4) &7; 765 STp
->density
= (STp
->buffer
)->b_data
[4]; 766 STp
->block_size
= (STp
->buffer
)->b_data
[9] *65536+ 767 (STp
->buffer
)->b_data
[10] *256+ (STp
->buffer
)->b_data
[11]; 770 printk(ST_DEB_MSG
"st%d: Density %x, tape length: %x, drv buffer: %d\n", 771 dev
, STp
->density
, (STp
->buffer
)->b_data
[5] *65536+ 772 (STp
->buffer
)->b_data
[6] *256+ (STp
->buffer
)->b_data
[7], 776 if(STp
->block_size
> (STp
->buffer
)->buffer_size
&& 777 !enlarge_buffer(STp
->buffer
, STp
->block_size
, STp
->restr_dma
)) { 778 printk(KERN_NOTICE
"st%d: Blocksize %d too large for buffer.\n", dev
, 780 scsi_release_command(SCpnt
); 781 (STp
->buffer
)->in_use
=0; 783 if(scsi_tapes
[dev
].device
->host
->hostt
->module
) 784 __MOD_DEC_USE_COUNT(scsi_tapes
[dev
].device
->host
->hostt
->module
); 785 if(st_template
.module
) 786 __MOD_DEC_USE_COUNT(st_template
.module
); 789 STp
->drv_write_prot
= ((STp
->buffer
)->b_data
[2] &0x80) !=0; 791 scsi_release_command(SCpnt
); 794 if(STp
->block_size
>0) 795 (STp
->buffer
)->buffer_blocks
= (STp
->buffer
)->buffer_size
/ STp
->block_size
; 797 (STp
->buffer
)->buffer_blocks
=1; 798 (STp
->buffer
)->buffer_bytes
= (STp
->buffer
)->read_pointer
=0; 802 printk(ST_DEB_MSG
"st%d: Block size: %d, buffer size: %d (%d blocks).\n", dev
, 803 STp
->block_size
, (STp
->buffer
)->buffer_size
, 804 (STp
->buffer
)->buffer_blocks
); 807 if(STp
->drv_write_prot
) { 811 printk(ST_DEB_MSG
"st%d: Write protected\n", dev
); 813 if((flags
& O_ACCMODE
) == O_WRONLY
|| (flags
& O_ACCMODE
) == O_RDWR
) { 814 (STp
->buffer
)->in_use
=0; 816 if(scsi_tapes
[dev
].device
->host
->hostt
->module
) 817 __MOD_DEC_USE_COUNT(scsi_tapes
[dev
].device
->host
->hostt
->module
); 818 if(st_template
.module
) 819 __MOD_DEC_USE_COUNT(st_template
.module
); 823 if(STp
->can_partitions
&& STp
->nbr_partitions
<1) { 824 /* This code is reached when the device is opened for the first time 825 after the driver has been initialized with tape in the drive and the 826 partition support has been enabled. */ 829 printk(ST_DEB_MSG
"st%d: Updating partition number in status.\n", dev
); 831 if((STp
->partition
=find_partition(inode
)) <0) { 832 (STp
->buffer
)->in_use
=0; 834 if(scsi_tapes
[dev
].device
->host
->hostt
->module
) 835 __MOD_DEC_USE_COUNT(scsi_tapes
[dev
].device
->host
->hostt
->module
); 836 if(st_template
.module
) 837 __MOD_DEC_USE_COUNT(st_template
.module
); 838 return STp
->partition
; 840 STp
->new_partition
= STp
->partition
; 841 STp
->nbr_partitions
=1;/* This guess will be updated when necessary */ 843 if(new_session
) {/* Change the drive parameters for the new mode */ 844 STp
->density_changed
= STp
->blksize_changed
= FALSE
; 845 STp
->compression_changed
= FALSE
; 846 if(!(STm
->defaults_for_writes
) && 847 (i
=set_mode_densblk(inode
, STp
, STm
)) <0) { 848 (STp
->buffer
)->in_use
=0; 850 if(scsi_tapes
[dev
].device
->host
->hostt
->module
) 851 __MOD_DEC_USE_COUNT(scsi_tapes
[dev
].device
->host
->hostt
->module
); 852 if(st_template
.module
) 853 __MOD_DEC_USE_COUNT(st_template
.module
); 856 if(STp
->default_drvbuffer
!=0xff) { 857 if(st_int_ioctl(inode
, MTSETDRVBUFFER
, STp
->default_drvbuffer
)) 858 printk(KERN_WARNING
"st%d: Can't set default drive buffering to %d.\n", 859 dev
, STp
->default_drvbuffer
); 868 /* Flush the tape buffer before close */ 869 static intscsi_tape_flush(struct file
*filp
) 871 int result
=0, result2
; 872 static unsigned char cmd
[10]; 878 struct inode
*inode
= filp
->f_dentry
->d_inode
; 879 kdev_t devt
= inode
->i_rdev
; 882 if(file_count(filp
) >1) 886 STp
= &(scsi_tapes
[dev
]); 887 STm
= &(STp
->modes
[STp
->current_mode
]); 888 STps
= &(STp
->ps
[STp
->partition
]); 890 if(STp
->can_partitions
&& 891 (result
=update_partition(inode
)) <0) { 894 printk(ST_DEB_MSG
"st%d: update_partition at close failed.\n", dev
); 898 if(STps
->rw
== ST_WRITING
&& !(STp
->device
)->was_reset
) { 900 result
=flush_write_buffer(STp
); 904 printk(ST_DEB_MSG
"st%d: File length %ld bytes.\n", 905 dev
, (long) (filp
->f_pos
)); 906 printk(ST_DEB_MSG
"st%d: Async write waits %d, finished %d.\n", 907 dev
, STp
->nbr_waits
, STp
->nbr_finished
); 911 if(result
==0|| result
== (-ENOSPC
)) { 914 cmd
[0] = WRITE_FILEMARKS
; 915 cmd
[4] =1+ STp
->two_fm
; 917 SCpnt
=st_do_scsi(NULL
, STp
, cmd
,0, STp
->timeout
, MAX_WRITE_RETRIES
, 922 if((STp
->buffer
)->last_result_fatal
!=0&& 923 ((SCpnt
->sense_buffer
[0] &0x70) !=0x70|| 924 (SCpnt
->sense_buffer
[2] &0x4f) !=0x40|| 925 ((SCpnt
->sense_buffer
[0] &0x80) !=0&& 926 (SCpnt
->sense_buffer
[3] | SCpnt
->sense_buffer
[4] | 927 SCpnt
->sense_buffer
[5] | 928 SCpnt
->sense_buffer
[6]) ==0))) { 929 /* Filter out successful write at EOM */ 930 scsi_release_command(SCpnt
); 932 printk(KERN_ERR
"st%d: Error on write filemark.\n", dev
); 936 scsi_release_command(SCpnt
); 938 if(STps
->drv_file
>=0) 942 cross_eof(STp
, FALSE
); 948 printk(ST_DEB_MSG
"st%d: Buffer flushed, %d EOF(s) written\n", 951 }else if(!STp
->rew_at_close
) { 952 STps
= &(STp
->ps
[STp
->partition
]); 953 if(!STm
->sysv
|| STps
->rw
!= ST_READING
) { 955 result
=flush_buffer(inode
, filp
,0); 956 else if(STps
->eof
== ST_FM_HIT
) { 957 result
=cross_eof(STp
, FALSE
); 959 if(STps
->drv_file
>=0) 964 STps
->eof
= ST_NOEOF
; 966 }else if((STps
->eof
== ST_NOEOF
&& 967 !(result
=cross_eof(STp
, TRUE
))) || 968 STps
->eof
== ST_FM_HIT
) { 969 if(STps
->drv_file
>=0) 976 if(STp
->rew_at_close
) { 977 result2
=st_int_ioctl(inode
, MTREW
,1); 985 /* Close the device and release it */ 986 static intscsi_tape_close(struct inode
*inode
,struct file
*filp
) 991 kdev_t devt
= inode
->i_rdev
; 995 STp
= &(scsi_tapes
[dev
]); 997 if(STp
->door_locked
== ST_LOCKED_AUTO
) 998 st_int_ioctl(inode
, MTUNLOCK
,0); 1000 if(STp
->buffer
!= NULL
) { 1001 normalize_buffer(STp
->buffer
); 1002 (STp
->buffer
)->in_use
=0; 1005 if(scsi_tapes
[dev
].device
->host
->hostt
->module
) 1006 __MOD_DEC_USE_COUNT(scsi_tapes
[dev
].device
->host
->hostt
->module
); 1007 if(st_template
.module
) 1008 __MOD_DEC_USE_COUNT(st_template
.module
); 1016 st_write(struct file
*filp
,const char*buf
,size_t count
, loff_t
* ppos
) 1018 struct inode
*inode
= filp
->f_dentry
->d_inode
; 1020 ssize_t i
, do_count
, blks
, retval
, transfer
; 1021 int write_threshold
; 1023 static unsigned char cmd
[10]; 1025 Scsi_Cmnd
*SCpnt
= NULL
; 1029 int dev
=TAPE_NR(inode
->i_rdev
); 1031 STp
= &(scsi_tapes
[dev
]); 1034 * If we are in the middle of error recovery, don't let anyone 1035 * else try and use this device. Also, if error recovery fails, it 1036 * may try and take the device offline, in which case all further 1037 * access to the device is prohibited. 1039 if(!scsi_block_when_processing_errors(STp
->device
)) { 1042 if(ppos
!= &filp
->f_pos
) { 1043 /* "A request was outside the capabilities of the device." */ 1046 if(STp
->ready
!= ST_READY
) { 1047 if(STp
->ready
== ST_NO_TAPE
) 1052 STm
= &(STp
->modes
[STp
->current_mode
]); 1059 * If there was a bus reset, block further access 1062 if(STp
->device
->was_reset
) 1067 printk(ST_DEB_MSG
"st%d: Incorrect device.\n", dev
); 1072 /* Write must be integral number of blocks */ 1073 if(STp
->block_size
!=0&& (count
% STp
->block_size
) !=0) { 1074 printk(KERN_WARNING
"st%d: Write not multiple of tape block size.\n", 1078 if(STp
->can_partitions
&& 1079 (retval
=update_partition(inode
)) <0) 1081 STps
= &(STp
->ps
[STp
->partition
]); 1086 if(STp
->block_size
==0&& 1087 count
> (STp
->buffer
)->buffer_size
&& 1088 !enlarge_buffer(STp
->buffer
, count
, STp
->restr_dma
)) 1091 if(STp
->do_auto_lock
&& STp
->door_locked
== ST_UNLOCKED
&& 1092 !st_int_ioctl(inode
, MTLOCK
,0)) 1093 STp
->door_locked
= ST_LOCKED_AUTO
; 1095 if(STps
->rw
== ST_READING
) { 1096 retval
=flush_buffer(inode
, filp
,0); 1099 STps
->rw
= ST_WRITING
; 1100 }else if(STps
->rw
!= ST_WRITING
&& 1101 STps
->drv_file
==0&& STps
->drv_block
==0) { 1102 if((retval
=set_mode_densblk(inode
, STp
, STm
)) <0) 1104 if(STm
->default_compression
!= ST_DONT_TOUCH
&& 1105 !(STp
->compression_changed
)) { 1106 if(st_compression(STp
, (STm
->default_compression
== ST_YES
))) { 1107 printk(KERN_WARNING
"st%d: Can't set default compression.\n", 1114 if((STp
->buffer
)->writing
) { 1115 write_behind_check(STp
); 1116 if((STp
->buffer
)->last_result_fatal
) { 1119 printk(ST_DEB_MSG
"st%d: Async write error (write) %x.\n", dev
, 1120 (STp
->buffer
)->last_result
); 1122 if((STp
->buffer
)->last_result
== INT_MAX
) 1123 STps
->eof
= ST_EOM_OK
; 1125 STps
->eof
= ST_EOM_ERROR
; 1128 if(STps
->eof
== ST_EOM_OK
) 1130 else if(STps
->eof
== ST_EOM_ERROR
) 1133 /* Check the buffer readability in cases where copy_user might catch 1134 the problems after some tape movement. */ 1135 if(STp
->block_size
!=0&& 1136 (copy_from_user(&i
, buf
,1) !=0|| 1137 copy_from_user(&i
, buf
+ count
-1,1) !=0)) 1140 if(!STm
->do_buffer_writes
) { 1142 if(STp
->block_size
!=0&& (count
% STp
->block_size
) !=0) 1143 return(-EIO
);/* Write must be integral number of blocks */ 1147 write_threshold
= (STp
->buffer
)->buffer_blocks
* STp
->block_size
; 1148 if(!STm
->do_async_writes
) 1155 cmd
[1] = (STp
->block_size
!=0); 1157 STps
->rw
= ST_WRITING
; 1160 while((STp
->block_size
==0&& !STm
->do_async_writes
&& count
>0) || 1161 (STp
->block_size
!=0&& 1162 (STp
->buffer
)->buffer_bytes
+ count
> write_threshold
)) { 1164 if(STp
->block_size
==0) 1167 do_count
= (STp
->buffer
)->buffer_blocks
* STp
->block_size
- 1168 (STp
->buffer
)->buffer_bytes
; 1169 if(do_count
> count
) 1173 i
=append_to_buffer(b_point
, STp
->buffer
, do_count
); 1176 scsi_release_command(SCpnt
); 1181 if(STp
->block_size
==0) 1182 blks
= transfer
= do_count
; 1184 blks
= (STp
->buffer
)->buffer_bytes
/ 1186 transfer
= blks
* STp
->block_size
; 1192 SCpnt
=st_do_scsi(SCpnt
, STp
, cmd
, transfer
, STp
->timeout
, 1193 MAX_WRITE_RETRIES
, TRUE
); 1197 if((STp
->buffer
)->last_result_fatal
!=0) { 1200 printk(ST_DEB_MSG
"st%d: Error on write:\n", dev
); 1202 if((SCpnt
->sense_buffer
[0] &0x70) ==0x70&& 1203 (SCpnt
->sense_buffer
[2] &0x40)) { 1204 if(STp
->block_size
!=0&& (SCpnt
->sense_buffer
[0] &0x80) !=0) 1205 transfer
= (SCpnt
->sense_buffer
[3] <<24) | 1206 (SCpnt
->sense_buffer
[4] <<16) | 1207 (SCpnt
->sense_buffer
[5] <<8) | SCpnt
->sense_buffer
[6]; 1208 else if(STp
->block_size
==0&& 1209 (SCpnt
->sense_buffer
[2] &0x0f) == VOLUME_OVERFLOW
) 1210 transfer
= do_count
; 1213 if(STp
->block_size
!=0) 1214 transfer
*= STp
->block_size
; 1215 if(transfer
<= do_count
) { 1216 filp
->f_pos
+= do_count
- transfer
; 1217 count
-= do_count
- transfer
; 1218 if(STps
->drv_block
>=0) { 1219 if(STp
->block_size
==0&& transfer
< do_count
) 1221 else if(STp
->block_size
!=0) 1222 STps
->drv_block
+= (do_count
- transfer
) / STp
->block_size
; 1224 STps
->eof
= ST_EOM_OK
; 1225 retval
= (-ENOSPC
);/* EOM within current request */ 1228 printk(ST_DEB_MSG
"st%d: EOM with %d bytes unwritten.\n", 1232 STps
->eof
= ST_EOM_ERROR
; 1233 STps
->drv_block
= (-1);/* Too cautious? */ 1234 retval
= (-EIO
);/* EOM for old data */ 1237 printk(ST_DEB_MSG
"st%d: EOM with lost data.\n", dev
); 1241 STps
->drv_block
= (-1);/* Too cautious? */ 1245 scsi_release_command(SCpnt
); 1247 (STp
->buffer
)->buffer_bytes
=0; 1250 return total
- count
; 1254 filp
->f_pos
+= do_count
; 1255 b_point
+= do_count
; 1257 if(STps
->drv_block
>=0) { 1258 if(STp
->block_size
==0) 1261 STps
->drv_block
+= blks
; 1263 (STp
->buffer
)->buffer_bytes
=0; 1268 i
=append_to_buffer(b_point
, STp
->buffer
, count
); 1271 scsi_release_command(SCpnt
); 1276 filp
->f_pos
+= count
; 1279 if(doing_write
&& (STp
->buffer
)->last_result_fatal
!=0) { 1280 scsi_release_command(SCpnt
); 1282 return(STp
->buffer
)->last_result_fatal
; 1284 if(STm
->do_async_writes
&& 1285 (((STp
->buffer
)->buffer_bytes
>= STp
->write_threshold
&& 1286 (STp
->buffer
)->buffer_bytes
>= STp
->block_size
) || 1287 STp
->block_size
==0)) { 1288 /* Schedule an asynchronous write */ 1289 if(STp
->block_size
==0) 1290 (STp
->buffer
)->writing
= (STp
->buffer
)->buffer_bytes
; 1292 (STp
->buffer
)->writing
= ((STp
->buffer
)->buffer_bytes
/ 1293 STp
->block_size
) * STp
->block_size
; 1294 STp
->dirty
= !((STp
->buffer
)->writing
== 1295 (STp
->buffer
)->buffer_bytes
); 1297 if(STp
->block_size
==0) 1298 blks
= (STp
->buffer
)->writing
; 1300 blks
= (STp
->buffer
)->writing
/ STp
->block_size
; 1305 STp
->write_pending
=1; 1308 SCpnt
=st_do_scsi(SCpnt
, STp
, cmd
, (STp
->buffer
)->writing
, STp
->timeout
, 1309 MAX_WRITE_RETRIES
, FALSE
); 1312 }else if(SCpnt
!= NULL
) { 1313 scsi_release_command(SCpnt
); 1316 STps
->at_sm
&= (total
==0); 1318 STps
->eof
= ST_NOEOF
; 1323 /* Read data from the tape. Returns zero in the normal case, one if the 1324 eof status has changed, and the negative error code in case of a 1325 fatal error. Otherwise updates the buffer and the eof state. */ 1326 static longread_tape(struct inode
*inode
,long count
, Scsi_Cmnd
** aSCpnt
) 1328 int transfer
, blks
, bytes
; 1329 static unsigned char cmd
[10]; 1334 int dev
=TAPE_NR(inode
->i_rdev
); 1340 STp
= &(scsi_tapes
[dev
]); 1341 STm
= &(STp
->modes
[STp
->current_mode
]); 1342 STps
= &(STp
->ps
[STp
->partition
]); 1343 if(STps
->eof
== ST_FM_HIT
) 1348 cmd
[1] = (STp
->block_size
!=0); 1349 if(STp
->block_size
==0) 1350 blks
= bytes
= count
; 1352 if(STm
->do_read_ahead
) { 1353 blks
= (STp
->buffer
)->buffer_blocks
; 1354 bytes
= blks
* STp
->block_size
; 1357 if(bytes
> (STp
->buffer
)->buffer_size
) 1358 bytes
= (STp
->buffer
)->buffer_size
; 1359 blks
= bytes
/ STp
->block_size
; 1360 bytes
= blks
* STp
->block_size
; 1368 SCpnt
=st_do_scsi(SCpnt
, STp
, cmd
, bytes
, STp
->timeout
, MAX_RETRIES
, TRUE
); 1373 (STp
->buffer
)->read_pointer
=0; 1376 /* Something to check */ 1377 if((STp
->buffer
)->last_result_fatal
) { 1381 printk(ST_DEB_MSG
"st%d: Sense: %2x %2x %2x %2x %2x %2x %2x %2x\n", 1383 SCpnt
->sense_buffer
[0], SCpnt
->sense_buffer
[1], 1384 SCpnt
->sense_buffer
[2], SCpnt
->sense_buffer
[3], 1385 SCpnt
->sense_buffer
[4], SCpnt
->sense_buffer
[5], 1386 SCpnt
->sense_buffer
[6], SCpnt
->sense_buffer
[7]); 1388 if((SCpnt
->sense_buffer
[0] &0x70) ==0x70) {/* extended sense */ 1390 if((SCpnt
->sense_buffer
[2] &0x0f) == BLANK_CHECK
) 1391 SCpnt
->sense_buffer
[2] &=0xcf;/* No need for EOM in this case */ 1393 if((SCpnt
->sense_buffer
[2] &0xe0) !=0) {/* EOF, EOM, or ILI */ 1394 /* Compute the residual count */ 1395 if((SCpnt
->sense_buffer
[0] &0x80) !=0) 1396 transfer
= (SCpnt
->sense_buffer
[3] <<24) | 1397 (SCpnt
->sense_buffer
[4] <<16) | 1398 (SCpnt
->sense_buffer
[5] <<8) | SCpnt
->sense_buffer
[6]; 1401 if(STp
->block_size
==0&& 1402 (SCpnt
->sense_buffer
[2] &0x0f) == MEDIUM_ERROR
) 1405 if(SCpnt
->sense_buffer
[2] &0x20) {/* ILI */ 1406 if(STp
->block_size
==0) { 1409 (STp
->buffer
)->buffer_bytes
= bytes
- transfer
; 1411 scsi_release_command(SCpnt
); 1412 SCpnt
= *aSCpnt
= NULL
; 1413 if(transfer
== blks
) {/* We did not get anything, error */ 1414 printk(KERN_NOTICE
"st%d: Incorrect block size.\n", dev
); 1415 if(STps
->drv_block
>=0) 1416 STps
->drv_block
+= blks
- transfer
+1; 1417 st_int_ioctl(inode
, MTBSR
,1); 1420 /* We have some data, deliver it */ 1421 (STp
->buffer
)->buffer_bytes
= (blks
- transfer
) * 1426 "st%d: ILI but enough data received %ld %d.\n", 1427 dev
, count
, (STp
->buffer
)->buffer_bytes
); 1429 if(STps
->drv_block
>=0) 1430 STps
->drv_block
+=1; 1431 if(st_int_ioctl(inode
, MTBSR
,1)) 1434 }else if(SCpnt
->sense_buffer
[2] &0x80) {/* FM overrides EOM */ 1435 if(STps
->eof
!= ST_FM_HIT
) 1436 STps
->eof
= ST_FM_HIT
; 1438 STps
->eof
= ST_EOD_2
; 1439 if(STp
->block_size
==0) 1440 (STp
->buffer
)->buffer_bytes
=0; 1442 (STp
->buffer
)->buffer_bytes
= 1443 bytes
- transfer
* STp
->block_size
; 1447 "st%d: EOF detected (%d bytes read).\n", 1448 dev
, (STp
->buffer
)->buffer_bytes
); 1450 }else if(SCpnt
->sense_buffer
[2] &0x40) { 1451 if(STps
->eof
== ST_FM
) 1452 STps
->eof
= ST_EOD_1
; 1454 STps
->eof
= ST_EOM_OK
; 1455 if(STp
->block_size
==0) 1456 (STp
->buffer
)->buffer_bytes
= bytes
- transfer
; 1458 (STp
->buffer
)->buffer_bytes
= 1459 bytes
- transfer
* STp
->block_size
; 1462 printk(ST_DEB_MSG
"st%d: EOM detected (%d bytes read).\n", 1463 dev
, (STp
->buffer
)->buffer_bytes
); 1467 /* end of EOF, EOM, ILI test */ 1468 else{/* nonzero sense key */ 1471 printk(ST_DEB_MSG
"st%d: Tape error while reading.\n", dev
); 1473 STps
->drv_block
= (-1); 1474 if(STps
->eof
== ST_FM
&& 1475 (SCpnt
->sense_buffer
[2] &0x0f) == BLANK_CHECK
) { 1479 "st%d: Zero returned for first BLANK CHECK after EOF.\n", 1482 STps
->eof
= ST_EOD_2
;/* First BLANK_CHECK after FM */ 1483 }else/* Some other extended sense code */ 1487 /* End of extended sense test */ 1488 else{/* Non-extended sense */ 1489 retval
= (STp
->buffer
)->last_result_fatal
; 1493 /* End of error handling */ 1494 else/* Read successful */ 1495 (STp
->buffer
)->buffer_bytes
= bytes
; 1497 if(STps
->drv_block
>=0) { 1498 if(STp
->block_size
==0) 1501 STps
->drv_block
+= (STp
->buffer
)->buffer_bytes
/ STp
->block_size
; 1509 st_read(struct file
*filp
,char*buf
,size_t count
, loff_t
* ppos
) 1511 struct inode
*inode
= filp
->f_dentry
->d_inode
; 1513 ssize_t i
, transfer
; 1515 Scsi_Cmnd
*SCpnt
= NULL
; 1519 int dev
=TAPE_NR(inode
->i_rdev
); 1521 STp
= &(scsi_tapes
[dev
]); 1524 * If we are in the middle of error recovery, don't let anyone 1525 * else try and use this device. Also, if error recovery fails, it 1526 * may try and take the device offline, in which case all further 1527 * access to the device is prohibited. 1529 if(!scsi_block_when_processing_errors(STp
->device
)) { 1532 if(ppos
!= &filp
->f_pos
) { 1533 /* "A request was outside the capabilities of the device." */ 1536 if(STp
->ready
!= ST_READY
) { 1537 if(STp
->ready
== ST_NO_TAPE
) 1542 STm
= &(STp
->modes
[STp
->current_mode
]); 1547 printk(ST_DEB_MSG
"st%d: Incorrect device.\n", dev
); 1552 if(STp
->can_partitions
&& 1553 (total
=update_partition(inode
)) <0) 1556 if(STp
->block_size
==0&& 1557 count
> (STp
->buffer
)->buffer_size
&& 1558 !enlarge_buffer(STp
->buffer
, count
, STp
->restr_dma
)) 1561 if(!(STm
->do_read_ahead
) && STp
->block_size
!=0&& 1562 (count
% STp
->block_size
) !=0) 1563 return(-EIO
);/* Read must be integral number of blocks */ 1565 if(STp
->do_auto_lock
&& STp
->door_locked
== ST_UNLOCKED
&& 1566 !st_int_ioctl(inode
, MTLOCK
,0)) 1567 STp
->door_locked
= ST_LOCKED_AUTO
; 1569 STps
= &(STp
->ps
[STp
->partition
]); 1570 if(STps
->rw
== ST_WRITING
) { 1571 transfer
=flush_buffer(inode
, filp
,0); 1574 STps
->rw
= ST_READING
; 1577 if(debugging
&& STps
->eof
!= ST_NOEOF
) 1578 printk(ST_DEB_MSG
"st%d: EOF/EOM flag up (%d). Bytes %d\n", dev
, 1579 STps
->eof
, (STp
->buffer
)->buffer_bytes
); 1581 if((STp
->buffer
)->buffer_bytes
==0&& 1582 STps
->eof
>= ST_EOD_1
) { 1583 if(STps
->eof
< ST_EOD
) { 1587 return(-EIO
);/* EOM or Blank Check */ 1589 /* Check the buffer writability before any tape movement. Don't alter 1591 if(copy_from_user(&i
, buf
,1) !=0|| 1592 copy_to_user(buf
, &i
,1) !=0|| 1593 copy_from_user(&i
, buf
+ count
-1,1) !=0|| 1594 copy_to_user(buf
+ count
-1, &i
,1) !=0) 1597 STps
->rw
= ST_READING
; 1600 /* Loop until enough data in buffer or a special condition found */ 1601 for(total
=0, special
=0; total
< count
&& !special
;) { 1603 /* Get new data if the buffer is empty */ 1604 if((STp
->buffer
)->buffer_bytes
==0) { 1605 special
=read_tape(inode
, count
- total
, &SCpnt
); 1606 if(special
<0) {/* No need to continue read */ 1608 scsi_release_command(SCpnt
); 1613 /* Move the data from driver buffer to user buffer */ 1614 if((STp
->buffer
)->buffer_bytes
>0) { 1616 if(debugging
&& STps
->eof
!= ST_NOEOF
) 1617 printk(ST_DEB_MSG
"st%d: EOF up (%d). Left %d, needed %d.\n", dev
, 1618 STps
->eof
, (STp
->buffer
)->buffer_bytes
, count
- total
); 1620 transfer
= (STp
->buffer
)->buffer_bytes
< count
- total
? 1621 (STp
->buffer
)->buffer_bytes
: count
- total
; 1622 i
=from_buffer(STp
->buffer
, buf
, transfer
); 1625 scsi_release_command(SCpnt
); 1630 filp
->f_pos
+= transfer
; 1634 if(STp
->block_size
==0) 1635 break;/* Read only one variable length block */ 1637 }/* for (total = 0, special = 0; total < count && !special; ) */ 1640 scsi_release_command(SCpnt
); 1643 /* Change the eof state if no data from tape or buffer */ 1645 if(STps
->eof
== ST_FM_HIT
) { 1648 if(STps
->drv_file
>=0) 1650 }else if(STps
->eof
== ST_EOD_1
) { 1651 STps
->eof
= ST_EOD_2
; 1653 if(STps
->drv_file
>=0) 1655 }else if(STps
->eof
== ST_EOD_2
) 1657 }else if(STps
->eof
== ST_FM
) 1658 STps
->eof
= ST_NOEOF
; 1665 /* Set the driver options */ 1666 static voidst_log_options(Scsi_Tape
* STp
, ST_mode
* STm
,int dev
) 1669 "st%d: Mode %d options: buffer writes: %d, async writes: %d, read ahead: %d\n", 1670 dev
, STp
->current_mode
, STm
->do_buffer_writes
, STm
->do_async_writes
, 1671 STm
->do_read_ahead
); 1673 "st%d: can bsr: %d, two FMs: %d, fast mteom: %d, auto lock: %d,\n", 1674 dev
, STp
->can_bsr
, STp
->two_fm
, STp
->fast_mteom
, STp
->do_auto_lock
); 1676 "st%d: defs for wr: %d, no block limits: %d, partitions: %d, s2 log: %d\n", 1677 dev
, STm
->defaults_for_writes
, STp
->omit_blklims
, STp
->can_partitions
, 1678 STp
->scsi2_logical
); 1680 "st%d: sysv: %d\n", dev
, STm
->sysv
); 1683 "st%d: debugging: %d\n", 1689 static intst_set_options(struct inode
*inode
,long options
) 1695 int dev
=TAPE_NR(inode
->i_rdev
); 1697 STp
= &(scsi_tapes
[dev
]); 1698 STm
= &(STp
->modes
[STp
->current_mode
]); 1700 memcpy(STm
, &(STp
->modes
[0]),sizeof(ST_mode
)); 1701 modes_defined
= TRUE
; 1704 printk(ST_DEB_MSG
"st%d: Initialized mode %d definition from mode 0\n", 1705 dev
, STp
->current_mode
); 1708 code
= options
& MT_ST_OPTIONS
; 1709 if(code
== MT_ST_BOOLEANS
) { 1710 STm
->do_buffer_writes
= (options
& MT_ST_BUFFER_WRITES
) !=0; 1711 STm
->do_async_writes
= (options
& MT_ST_ASYNC_WRITES
) !=0; 1712 STm
->defaults_for_writes
= (options
& MT_ST_DEF_WRITES
) !=0; 1713 STm
->do_read_ahead
= (options
& MT_ST_READ_AHEAD
) !=0; 1714 STp
->two_fm
= (options
& MT_ST_TWO_FM
) !=0; 1715 STp
->fast_mteom
= (options
& MT_ST_FAST_MTEOM
) !=0; 1716 STp
->do_auto_lock
= (options
& MT_ST_AUTO_LOCK
) !=0; 1717 STp
->can_bsr
= (options
& MT_ST_CAN_BSR
) !=0; 1718 STp
->omit_blklims
= (options
& MT_ST_NO_BLKLIMS
) !=0; 1719 if((STp
->device
)->scsi_level
>= SCSI_2
) 1720 STp
->can_partitions
= (options
& MT_ST_CAN_PARTITIONS
) !=0; 1721 STp
->scsi2_logical
= (options
& MT_ST_SCSI2LOGICAL
) !=0; 1722 STm
->sysv
= (options
& MT_ST_SYSV
) !=0; 1724 debugging
= (options
& MT_ST_DEBUGGING
) !=0; 1726 st_log_options(STp
, STm
, dev
); 1727 }else if(code
== MT_ST_SETBOOLEANS
|| code
== MT_ST_CLEARBOOLEANS
) { 1728 value
= (code
== MT_ST_SETBOOLEANS
); 1729 if((options
& MT_ST_BUFFER_WRITES
) !=0) 1730 STm
->do_buffer_writes
= value
; 1731 if((options
& MT_ST_ASYNC_WRITES
) !=0) 1732 STm
->do_async_writes
= value
; 1733 if((options
& MT_ST_DEF_WRITES
) !=0) 1734 STm
->defaults_for_writes
= value
; 1735 if((options
& MT_ST_READ_AHEAD
) !=0) 1736 STm
->do_read_ahead
= value
; 1737 if((options
& MT_ST_TWO_FM
) !=0) 1738 STp
->two_fm
= value
; 1739 if((options
& MT_ST_FAST_MTEOM
) !=0) 1740 STp
->fast_mteom
= value
; 1741 if((options
& MT_ST_AUTO_LOCK
) !=0) 1742 STp
->do_auto_lock
= value
; 1743 if((options
& MT_ST_CAN_BSR
) !=0) 1744 STp
->can_bsr
= value
; 1745 if((options
& MT_ST_NO_BLKLIMS
) !=0) 1746 STp
->omit_blklims
= value
; 1747 if((STp
->device
)->scsi_level
>= SCSI_2
&& 1748 (options
& MT_ST_CAN_PARTITIONS
) !=0) 1749 STp
->can_partitions
= value
; 1750 if((options
& MT_ST_SCSI2LOGICAL
) !=0) 1751 STp
->scsi2_logical
= value
; 1752 if((options
& MT_ST_SYSV
) !=0) 1755 if((options
& MT_ST_DEBUGGING
) !=0) 1758 st_log_options(STp
, STm
, dev
); 1759 }else if(code
== MT_ST_WRITE_THRESHOLD
) { 1760 value
= (options
& ~MT_ST_OPTIONS
) * ST_KILOBYTE
; 1761 if(value
<1|| value
> st_buffer_size
) { 1762 printk(KERN_WARNING
"st%d: Write threshold %d too small or too large.\n", 1766 STp
->write_threshold
= value
; 1767 printk(KERN_INFO
"st%d: Write threshold set to %d bytes.\n", 1769 }else if(code
== MT_ST_DEF_BLKSIZE
) { 1770 value
= (options
& ~MT_ST_OPTIONS
); 1771 if(value
== ~MT_ST_OPTIONS
) { 1772 STm
->default_blksize
= (-1); 1773 printk(KERN_INFO
"st%d: Default block size disabled.\n", dev
); 1775 STm
->default_blksize
= value
; 1776 printk(KERN_INFO
"st%d: Default block size set to %d bytes.\n", 1777 dev
, STm
->default_blksize
); 1779 }else if(code
== MT_ST_TIMEOUTS
) { 1780 value
= (options
& ~MT_ST_OPTIONS
); 1781 if((value
& MT_ST_SET_LONG_TIMEOUT
) !=0) { 1782 STp
->long_timeout
= (value
& ~MT_ST_SET_LONG_TIMEOUT
) * HZ
; 1783 printk(KERN_INFO
"st%d: Long timeout set to %d seconds.\n", dev
, 1784 (value
& ~MT_ST_SET_LONG_TIMEOUT
)); 1786 STp
->timeout
= value
* HZ
; 1787 printk(KERN_INFO
"st%d: Normal timeout set to %d seconds.\n", dev
, 1790 }else if(code
== MT_ST_DEF_OPTIONS
) { 1791 code
= (options
& ~MT_ST_CLEAR_DEFAULT
); 1792 value
= (options
& MT_ST_CLEAR_DEFAULT
); 1793 if(code
== MT_ST_DEF_DENSITY
) { 1794 if(value
== MT_ST_CLEAR_DEFAULT
) { 1795 STm
->default_density
= (-1); 1796 printk(KERN_INFO
"st%d: Density default disabled.\n", dev
); 1798 STm
->default_density
= value
&0xff; 1799 printk(KERN_INFO
"st%d: Density default set to %x\n", 1800 dev
, STm
->default_density
); 1802 }else if(code
== MT_ST_DEF_DRVBUFFER
) { 1803 if(value
== MT_ST_CLEAR_DEFAULT
) { 1804 STp
->default_drvbuffer
=0xff; 1805 printk(KERN_INFO
"st%d: Drive buffer default disabled.\n", dev
); 1807 STp
->default_drvbuffer
= value
&7; 1808 printk(KERN_INFO
"st%d: Drive buffer default set to %x\n", 1809 dev
, STp
->default_drvbuffer
); 1811 }else if(code
== MT_ST_DEF_COMPRESSION
) { 1812 if(value
== MT_ST_CLEAR_DEFAULT
) { 1813 STm
->default_compression
= ST_DONT_TOUCH
; 1814 printk(KERN_INFO
"st%d: Compression default disabled.\n", dev
); 1816 STm
->default_compression
= (value
&1? ST_YES
: ST_NO
); 1817 printk(KERN_INFO
"st%d: Compression default set to %x\n", 1828 #define COMPRESSION_PAGE 0x0f 1829 #define COMPRESSION_PAGE_LENGTH 16 1831 #define MODE_HEADER_LENGTH 4 1833 #define DCE_MASK 0x80 1834 #define DCC_MASK 0x40 1835 #define RED_MASK 0x60 1838 /* Control the compression with mode page 15. Algorithm not changed if zero. */ 1839 static intst_compression(Scsi_Tape
* STp
,int state
) 1842 unsigned char cmd
[10]; 1843 Scsi_Cmnd
*SCpnt
= NULL
; 1845 if(STp
->ready
!= ST_READY
) 1848 /* Read the current page contents */ 1850 cmd
[0] = MODE_SENSE
; 1852 cmd
[2] = COMPRESSION_PAGE
; 1853 cmd
[4] = COMPRESSION_PAGE_LENGTH
+ MODE_HEADER_LENGTH
; 1855 SCpnt
=st_do_scsi(SCpnt
, STp
, cmd
, cmd
[4], STp
->timeout
,0, TRUE
); 1858 dev
=TAPE_NR(SCpnt
->request
.rq_dev
); 1860 if((STp
->buffer
)->last_result_fatal
!=0) { 1863 printk(ST_DEB_MSG
"st%d: Compression mode page not supported.\n", dev
); 1865 scsi_release_command(SCpnt
); 1871 printk(ST_DEB_MSG
"st%d: Compression state is %d.\n", dev
, 1872 ((STp
->buffer
)->b_data
[MODE_HEADER_LENGTH
+2] & DCE_MASK
?1:0)); 1875 /* Check if compression can be changed */ 1876 if(((STp
->buffer
)->b_data
[MODE_HEADER_LENGTH
+2] & DCC_MASK
) ==0) { 1879 printk(ST_DEB_MSG
"st%d: Compression not supported.\n", dev
); 1881 scsi_release_command(SCpnt
); 1887 (STp
->buffer
)->b_data
[MODE_HEADER_LENGTH
+2] |= DCE_MASK
; 1889 (STp
->buffer
)->b_data
[MODE_HEADER_LENGTH
+2] &= ~DCE_MASK
; 1892 cmd
[0] = MODE_SELECT
; 1894 cmd
[4] = COMPRESSION_PAGE_LENGTH
+ MODE_HEADER_LENGTH
; 1896 (STp
->buffer
)->b_data
[0] =0;/* Reserved data length */ 1897 (STp
->buffer
)->b_data
[1] =0;/* Reserved media type byte */ 1898 (STp
->buffer
)->b_data
[MODE_HEADER_LENGTH
] &=0x3f; 1899 SCpnt
=st_do_scsi(SCpnt
, STp
, cmd
, cmd
[4], STp
->timeout
,0, TRUE
); 1901 if((STp
->buffer
)->last_result_fatal
!=0) { 1904 printk(ST_DEB_MSG
"st%d: Compression change failed.\n", dev
); 1906 scsi_release_command(SCpnt
); 1912 printk(ST_DEB_MSG
"st%d: Compression state changed to %d.\n", 1916 scsi_release_command(SCpnt
); 1918 STp
->compression_changed
= TRUE
; 1923 /* Internal ioctl function */ 1924 static intst_int_ioctl(struct inode
*inode
, 1925 unsigned int cmd_in
,unsigned long arg
) 1929 int i
, ioctl_result
; 1931 unsigned char cmd
[10]; 1935 int fileno
, blkno
, at_sm
, undone
, datalen
; 1936 int dev
=TAPE_NR(inode
->i_rdev
); 1938 STp
= &(scsi_tapes
[dev
]); 1939 if(STp
->ready
!= ST_READY
&& cmd_in
!= MTLOAD
) { 1940 if(STp
->ready
== ST_NO_TAPE
) 1945 timeout
= STp
->long_timeout
; 1946 STps
= &(STp
->ps
[STp
->partition
]); 1947 fileno
= STps
->drv_file
; 1948 blkno
= STps
->drv_block
; 1949 at_sm
= STps
->at_sm
; 1955 chg_eof
= FALSE
;/* Changed from the FSF after this */ 1958 cmd
[1] =0x01;/* Space FileMarks */ 1959 cmd
[2] = (arg
>>16); 1964 printk(ST_DEB_MSG
"st%d: Spacing tape forward over %d filemarks.\n", 1965 dev
, cmd
[2] *65536+ cmd
[3] *256+ cmd
[4]); 1973 chg_eof
= FALSE
;/* Changed from the FSF after this */ 1976 cmd
[1] =0x01;/* Space FileMarks */ 1978 cmd
[2] = (ltmp
>>16); 1979 cmd
[3] = (ltmp
>>8); 1985 ltmp
= ltmp
| (cmd
[2] <<16) | (cmd
[3] <<8) | cmd
[4]; 1986 printk(ST_DEB_MSG
"st%d: Spacing tape backward over %ld filemarks.\n", 1992 blkno
= (-1);/* We can't know the block number */ 1997 cmd
[1] =0x00;/* Space Blocks */ 1998 cmd
[2] = (arg
>>16); 2003 printk(ST_DEB_MSG
"st%d: Spacing tape forward %d blocks.\n", dev
, 2004 cmd
[2] *65536+ cmd
[3] *256+ cmd
[4]); 2012 cmd
[1] =0x00;/* Space Blocks */ 2014 cmd
[2] = (ltmp
>>16); 2015 cmd
[3] = (ltmp
>>8); 2021 ltmp
= ltmp
| (cmd
[2] <<16) | (cmd
[3] <<8) | cmd
[4]; 2022 printk(ST_DEB_MSG
"st%d: Spacing tape backward %ld blocks.\n", dev
, (-ltmp
)); 2031 cmd
[1] =0x04;/* Space Setmarks */ 2032 cmd
[2] = (arg
>>16); 2037 printk(ST_DEB_MSG
"st%d: Spacing tape forward %d setmarks.\n", dev
, 2038 cmd
[2] *65536+ cmd
[3] *256+ cmd
[4]); 2041 blkno
= fileno
= (-1); 2047 cmd
[1] =0x04;/* Space Setmarks */ 2049 cmd
[2] = (ltmp
>>16); 2050 cmd
[3] = (ltmp
>>8); 2056 ltmp
= ltmp
| (cmd
[2] <<16) | (cmd
[3] <<8) | cmd
[4]; 2057 printk(ST_DEB_MSG
"st%d: Spacing tape backward %ld setmarks.\n", 2062 blkno
= fileno
= (-1); 2070 cmd
[0] = WRITE_FILEMARKS
; 2073 cmd
[2] = (arg
>>16); 2076 timeout
= STp
->timeout
; 2079 if(cmd_in
== MTWEOF
) 2080 printk(ST_DEB_MSG
"st%d: Writing %d filemarks.\n", dev
, 2081 cmd
[2] *65536+ cmd
[3] *256+ cmd
[4]); 2083 printk(ST_DEB_MSG
"st%d: Writing %d setmarks.\n", dev
, 2084 cmd
[2] *65536+ cmd
[3] *256+ cmd
[4]); 2090 at_sm
= (cmd_in
== MTWSM
); 2093 cmd
[0] = REZERO_UNIT
; 2095 cmd
[1] =1;/* Don't wait for completion */ 2096 timeout
= STp
->timeout
; 2100 printk(ST_DEB_MSG
"st%d: Rewinding tape.\n", dev
); 2102 fileno
= blkno
= at_sm
=0; 2107 cmd
[0] = START_STOP
; 2108 if(cmd_in
== MTLOAD
) 2111 * If arg >= 1 && arg <= 6 Enhanced load/unload in HP C1553A 2113 if(cmd_in
!= MTOFFL
&& 2114 arg
>=1+ MT_ST_HPLOADER_OFFSET
2115 && arg
<=6+ MT_ST_HPLOADER_OFFSET
) { 2118 printk(ST_DEB_MSG
"st%d: Enhanced %sload slot %2ld.\n", 2119 dev
, (cmd
[4]) ?"":"un", 2120 arg
- MT_ST_HPLOADER_OFFSET
); 2123 cmd
[3] = arg
- MT_ST_HPLOADER_OFFSET
;/* MediaID field of C1553A */ 2126 cmd
[1] =1;/* Don't wait for completion */ 2127 timeout
= STp
->timeout
; 2129 timeout
= STp
->long_timeout
; 2133 if(cmd_in
!= MTLOAD
) 2134 printk(ST_DEB_MSG
"st%d: Unloading tape.\n", dev
); 2136 printk(ST_DEB_MSG
"st%d: Loading tape.\n", dev
); 2139 fileno
= blkno
= at_sm
=0; 2144 printk(ST_DEB_MSG
"st%d: No op on tape.\n", dev
); 2146 return0;/* Should do something ? */ 2149 cmd
[0] = START_STOP
; 2151 cmd
[1] =1;/* Don't wait for completion */ 2152 timeout
= STp
->timeout
; 2157 printk(ST_DEB_MSG
"st%d: Retensioning tape.\n", dev
); 2159 fileno
= blkno
= at_sm
=0; 2162 if(!STp
->fast_mteom
) { 2163 /* space to the end of tape */ 2164 ioctl_result
=st_int_ioctl(inode
, MTFSF
,0x3fff); 2165 fileno
= STps
->drv_file
; 2166 if(STps
->eof
>= ST_EOD_1
) 2168 /* The next lines would hide the number of spaced FileMarks 2169 That's why I inserted the previous lines. I had no luck 2170 with detecting EOM with FSF, so we go now to EOM. 2178 printk(ST_DEB_MSG
"st%d: Spacing to end of recorded medium.\n", dev
); 2187 cmd
[1] =1;/* To the end of tape */ 2189 cmd
[1] |=2;/* Don't wait for completion */ 2190 timeout
= STp
->timeout
; 2192 timeout
= STp
->long_timeout
*8; 2196 printk(ST_DEB_MSG
"st%d: Erasing tape.\n", dev
); 2198 fileno
= blkno
= at_sm
=0; 2202 cmd
[0] = ALLOW_MEDIUM_REMOVAL
; 2203 cmd
[4] = SCSI_REMOVAL_PREVENT
; 2206 printk(ST_DEB_MSG
"st%d: Locking drive door.\n", dev
); 2211 cmd
[0] = ALLOW_MEDIUM_REMOVAL
; 2212 cmd
[4] = SCSI_REMOVAL_ALLOW
; 2215 printk(ST_DEB_MSG
"st%d: Unlocking drive door.\n", dev
); 2218 case MTSETBLK
:/* Set block length */ 2219 case MTSETDENSITY
:/* Set tape density */ 2220 case MTSETDRVBUFFER
:/* Set drive buffering */ 2221 case SET_DENS_AND_BLK
:/* Set density and block size */ 2223 if(STp
->dirty
|| (STp
->buffer
)->buffer_bytes
!=0) 2224 return(-EIO
);/* Not allowed if data in buffer */ 2225 if((cmd_in
== MTSETBLK
|| cmd_in
== SET_DENS_AND_BLK
) && 2226 (arg
& MT_ST_BLKSIZE_MASK
) !=0&& 2227 ((arg
& MT_ST_BLKSIZE_MASK
) < STp
->min_block
|| 2228 (arg
& MT_ST_BLKSIZE_MASK
) > STp
->max_block
|| 2229 (arg
& MT_ST_BLKSIZE_MASK
) > st_buffer_size
)) { 2230 printk(KERN_WARNING
"st%d: Illegal block size.\n", dev
); 2233 cmd
[0] = MODE_SELECT
; 2234 cmd
[4] = datalen
=12; 2236 memset((STp
->buffer
)->b_data
,0,12); 2237 if(cmd_in
== MTSETDRVBUFFER
) 2238 (STp
->buffer
)->b_data
[2] = (arg
&7) <<4; 2240 (STp
->buffer
)->b_data
[2] = 2241 STp
->drv_buffer
<<4; 2242 (STp
->buffer
)->b_data
[3] =8;/* block descriptor length */ 2243 if(cmd_in
== MTSETDENSITY
) { 2244 (STp
->buffer
)->b_data
[4] = arg
; 2245 STp
->density_changed
= TRUE
;/* At least we tried ;-) */ 2246 }else if(cmd_in
== SET_DENS_AND_BLK
) 2247 (STp
->buffer
)->b_data
[4] = arg
>>24; 2249 (STp
->buffer
)->b_data
[4] = STp
->density
; 2250 if(cmd_in
== MTSETBLK
|| cmd_in
== SET_DENS_AND_BLK
) { 2251 ltmp
= arg
& MT_ST_BLKSIZE_MASK
; 2252 if(cmd_in
== MTSETBLK
) 2253 STp
->blksize_changed
= TRUE
;/* At least we tried ;-) */ 2255 ltmp
= STp
->block_size
; 2256 (STp
->buffer
)->b_data
[9] = (ltmp
>>16); 2257 (STp
->buffer
)->b_data
[10] = (ltmp
>>8); 2258 (STp
->buffer
)->b_data
[11] = ltmp
; 2259 timeout
= STp
->timeout
; 2262 if(cmd_in
== MTSETBLK
|| cmd_in
== SET_DENS_AND_BLK
) 2263 printk(ST_DEB_MSG
"st%d: Setting block size to %d bytes.\n", dev
, 2264 (STp
->buffer
)->b_data
[9] *65536+ 2265 (STp
->buffer
)->b_data
[10] *256+ 2266 (STp
->buffer
)->b_data
[11]); 2267 if(cmd_in
== MTSETDENSITY
|| cmd_in
== SET_DENS_AND_BLK
) 2268 printk(ST_DEB_MSG
"st%d: Setting density code to %x.\n", dev
, 2269 (STp
->buffer
)->b_data
[4]); 2270 if(cmd_in
== MTSETDRVBUFFER
) 2271 printk(ST_DEB_MSG
"st%d: Setting drive buffer code to %d.\n", dev
, 2272 ((STp
->buffer
)->b_data
[2] >>4) &7); 2280 SCpnt
=st_do_scsi(NULL
, STp
, cmd
, datalen
, timeout
, MAX_RETRIES
, TRUE
); 2284 ioctl_result
= (STp
->buffer
)->last_result_fatal
; 2286 if(!ioctl_result
) {/* SCSI command successful */ 2287 scsi_release_command(SCpnt
); 2289 STps
->drv_block
= blkno
; 2290 STps
->drv_file
= fileno
; 2291 STps
->at_sm
= at_sm
; 2293 if(cmd_in
== MTLOCK
) 2294 STp
->door_locked
= ST_LOCKED_EXPLICIT
; 2295 else if(cmd_in
== MTUNLOCK
) 2296 STp
->door_locked
= ST_UNLOCKED
; 2298 if(cmd_in
== MTBSFM
) 2299 ioctl_result
=st_int_ioctl(inode
, MTFSF
,1); 2300 else if(cmd_in
== MTFSFM
) 2301 ioctl_result
=st_int_ioctl(inode
, MTBSF
,1); 2303 if(cmd_in
== MTSETBLK
|| cmd_in
== SET_DENS_AND_BLK
) { 2304 STp
->block_size
= arg
& MT_ST_BLKSIZE_MASK
; 2305 if(STp
->block_size
!=0) 2306 (STp
->buffer
)->buffer_blocks
= 2307 (STp
->buffer
)->buffer_size
/ STp
->block_size
; 2308 (STp
->buffer
)->buffer_bytes
= (STp
->buffer
)->read_pointer
=0; 2309 if(cmd_in
== SET_DENS_AND_BLK
) 2310 STp
->density
= arg
>> MT_ST_DENSITY_SHIFT
; 2311 }else if(cmd_in
== MTSETDRVBUFFER
) 2312 STp
->drv_buffer
= (arg
&7); 2313 else if(cmd_in
== MTSETDENSITY
) 2318 else if(cmd_in
== MTFSF
) 2321 STps
->eof
= ST_NOEOF
; 2324 if(cmd_in
== MTOFFL
|| cmd_in
== MTUNLOAD
) 2325 STp
->rew_at_close
=0; 2326 else if(cmd_in
== MTLOAD
) { 2327 STp
->rew_at_close
= (MINOR(inode
->i_rdev
) &0x80) ==0; 2328 for(i
=0; i
< ST_NBR_PARTITIONS
; i
++) { 2329 STp
->ps
[i
].rw
= ST_IDLE
; 2330 STp
->ps
[i
].last_block_valid
= FALSE
; 2334 }else{/* SCSI command was not completely successful. Don't return 2335 from this block without releasing the SCSI command block! */ 2337 if(SCpnt
->sense_buffer
[2] &0x40) { 2338 if(cmd_in
!= MTBSF
&& cmd_in
!= MTBSFM
&& 2339 cmd_in
!= MTBSR
&& cmd_in
!= MTBSS
) 2340 STps
->eof
= ST_EOM_OK
; 2344 (SCpnt
->sense_buffer
[3] <<24) + 2345 (SCpnt
->sense_buffer
[4] <<16) + 2346 (SCpnt
->sense_buffer
[5] <<8) + 2347 SCpnt
->sense_buffer
[6]); 2348 if(cmd_in
== MTWEOF
&& 2349 (SCpnt
->sense_buffer
[0] &0x70) ==0x70&& 2350 (SCpnt
->sense_buffer
[2] &0x4f) ==0x40&& 2351 ((SCpnt
->sense_buffer
[0] &0x80) ==0|| undone
==0)) { 2352 ioctl_result
=0;/* EOF written succesfully at EOM */ 2355 STps
->drv_file
= fileno
; 2356 STps
->eof
= ST_NOEOF
; 2357 }else if((cmd_in
== MTFSF
) || (cmd_in
== MTFSFM
)) { 2359 STps
->drv_file
= fileno
- undone
; 2361 STps
->drv_file
= fileno
; 2363 STps
->eof
= ST_NOEOF
; 2364 }else if((cmd_in
== MTBSF
) || (cmd_in
== MTBSFM
)) { 2366 STps
->drv_file
= fileno
+ undone
; 2368 STps
->drv_file
= fileno
; 2370 STps
->eof
= ST_NOEOF
; 2371 }else if(cmd_in
== MTFSR
) { 2372 if(SCpnt
->sense_buffer
[2] &0x80) {/* Hit filemark */ 2373 if(STps
->drv_file
>=0) 2379 STps
->drv_block
= blkno
- undone
; 2381 STps
->drv_block
= (-1); 2382 STps
->eof
= ST_NOEOF
; 2384 }else if(cmd_in
== MTBSR
) { 2385 if(SCpnt
->sense_buffer
[2] &0x80) {/* Hit filemark */ 2387 STps
->drv_block
= (-1); 2390 STps
->drv_block
= blkno
+ undone
; 2392 STps
->drv_block
= (-1); 2394 STps
->eof
= ST_NOEOF
; 2395 }else if(cmd_in
== MTEOM
) { 2396 STps
->drv_file
= (-1); 2397 STps
->drv_block
= (-1); 2400 STps
->eof
= ST_NOEOF
; 2402 if((SCpnt
->sense_buffer
[2] &0x0f) == BLANK_CHECK
) 2405 if(cmd_in
== MTLOCK
) 2406 STp
->door_locked
= ST_LOCK_FAILS
; 2408 scsi_release_command(SCpnt
); 2412 return ioctl_result
; 2416 /* Get the tape position. If bt == 2, arg points into a kernel space mt_loc 2419 static intget_location(struct inode
*inode
,unsigned int*block
,int*partition
, 2423 int dev
=TAPE_NR(inode
->i_rdev
); 2425 unsigned char scmd
[10]; 2428 STp
= &(scsi_tapes
[dev
]); 2429 if(STp
->ready
!= ST_READY
) 2433 if((STp
->device
)->scsi_level
< SCSI_2
) { 2434 scmd
[0] = QFA_REQUEST_BLOCK
; 2437 scmd
[0] = READ_POSITION
; 2438 if(!logical
&& !STp
->scsi2_logical
) 2441 SCpnt
=st_do_scsi(NULL
, STp
, scmd
,20, STp
->timeout
, MAX_READY_RETRIES
, TRUE
); 2445 if((STp
->buffer
)->last_result_fatal
!=0|| 2446 (STp
->device
->scsi_level
>= SCSI_2
&& 2447 ((STp
->buffer
)->b_data
[0] &4) !=0)) { 2448 *block
= *partition
=0; 2451 printk(ST_DEB_MSG
"st%d: Can't read tape position.\n", dev
); 2456 if((STp
->device
)->scsi_level
< SCSI_2
) { 2457 *block
= ((STp
->buffer
)->b_data
[0] <<16) 2458 + ((STp
->buffer
)->b_data
[1] <<8) 2459 + (STp
->buffer
)->b_data
[2]; 2462 *block
= ((STp
->buffer
)->b_data
[4] <<24) 2463 + ((STp
->buffer
)->b_data
[5] <<16) 2464 + ((STp
->buffer
)->b_data
[6] <<8) 2465 + (STp
->buffer
)->b_data
[7]; 2466 *partition
= (STp
->buffer
)->b_data
[1]; 2467 if(((STp
->buffer
)->b_data
[0] &0x80) && 2468 (STp
->buffer
)->b_data
[1] ==0)/* BOP of partition 0 */ 2469 STp
->ps
[0].drv_block
= STp
->ps
[0].drv_file
=0; 2473 printk(ST_DEB_MSG
"st%d: Got tape pos. blk %d part %d.\n", dev
, 2474 *block
, *partition
); 2478 scsi_release_command(SCpnt
); 2485 /* Set the tape block and partition. Negative partition means that only the 2486 block should be set in vendor specific way. */ 2487 static intset_location(struct inode
*inode
,unsigned int block
,int partition
, 2492 int dev
=TAPE_NR(inode
->i_rdev
); 2496 unsigned char scmd
[10]; 2499 STp
= &(scsi_tapes
[dev
]); 2500 if(STp
->ready
!= ST_READY
) 2502 timeout
= STp
->long_timeout
; 2503 STps
= &(STp
->ps
[STp
->partition
]); 2507 printk(ST_DEB_MSG
"st%d: Setting block to %d and partition to %d.\n", 2508 dev
, block
, partition
); 2513 /* Update the location at the partition we are leaving */ 2514 if((!STp
->can_partitions
&& partition
!=0) || 2515 partition
>= ST_NBR_PARTITIONS
) 2517 if(partition
!= STp
->partition
) { 2518 if(get_location(inode
, &blk
, &p
,1)) 2519 STps
->last_block_valid
= FALSE
; 2521 STps
->last_block_valid
= TRUE
; 2522 STps
->last_block_visited
= blk
; 2525 printk(ST_DEB_MSG
"st%d: Visited block %d for partition %d saved.\n", 2526 dev
, blk
, STp
->partition
); 2531 if((STp
->device
)->scsi_level
< SCSI_2
) { 2532 scmd
[0] = QFA_SEEK_BLOCK
; 2533 scmd
[2] = (block
>>16); 2534 scmd
[3] = (block
>>8); 2539 scmd
[3] = (block
>>24); 2540 scmd
[4] = (block
>>16); 2541 scmd
[5] = (block
>>8); 2543 if(!logical
&& !STp
->scsi2_logical
) 2545 if(STp
->partition
!= partition
) { 2547 scmd
[8] = partition
; 2550 printk(ST_DEB_MSG
"st%d: Trying to change partition from %d to %d\n", 2551 dev
, STp
->partition
, partition
); 2556 scmd
[1] |=1;/* Don't wait for completion */ 2557 timeout
= STp
->timeout
; 2560 SCpnt
=st_do_scsi(NULL
, STp
, scmd
,20, timeout
, MAX_READY_RETRIES
, TRUE
); 2564 STps
->drv_block
= STps
->drv_file
= (-1); 2565 STps
->eof
= ST_NOEOF
; 2566 if((STp
->buffer
)->last_result_fatal
!=0) { 2568 if(STp
->can_partitions
&& 2569 (STp
->device
)->scsi_level
>= SCSI_2
&& 2570 (p
=find_partition(inode
)) >=0) 2573 if(STp
->can_partitions
) { 2574 STp
->partition
= partition
; 2575 STps
= &(STp
->ps
[partition
]); 2576 if(!STps
->last_block_valid
|| 2577 STps
->last_block_visited
!= block
) { 2584 STps
->drv_block
= STps
->drv_file
=0; 2588 scsi_release_command(SCpnt
); 2595 /* Find the current partition number for the drive status. Called from open and 2596 returns either partition number of negative error code. */ 2597 static intfind_partition(struct inode
*inode
) 2602 if((i
=get_location(inode
, &block
, &partition
,1)) <0) 2604 if(partition
>= ST_NBR_PARTITIONS
) 2610 /* Change the partition if necessary */ 2611 static intupdate_partition(struct inode
*inode
) 2613 int dev
=TAPE_NR(inode
->i_rdev
); 2617 STp
= &(scsi_tapes
[dev
]); 2618 if(STp
->partition
== STp
->new_partition
) 2620 STps
= &(STp
->ps
[STp
->new_partition
]); 2621 if(!STps
->last_block_valid
) 2622 STps
->last_block_visited
=0; 2623 returnset_location(inode
, STps
->last_block_visited
, STp
->new_partition
,1); 2626 /* Functions for reading and writing the medium partition mode page. These 2627 seem to work with Wangtek 6200HS and HP C1533A. */ 2629 #define PART_PAGE 0x11 2630 #define PART_PAGE_LENGTH 10 2632 /* Get the number of partitions on the tape. As a side effect reads the 2633 mode page into the tape buffer. */ 2634 static intnbr_partitions(struct inode
*inode
) 2636 int dev
=TAPE_NR(inode
->i_rdev
), result
; 2638 Scsi_Cmnd
*SCpnt
= NULL
; 2639 unsigned char cmd
[10]; 2641 STp
= &(scsi_tapes
[dev
]); 2642 if(STp
->ready
!= ST_READY
) 2645 memset((void*) &cmd
[0],0,10); 2646 cmd
[0] = MODE_SENSE
; 2647 cmd
[1] =8;/* Page format */ 2651 SCpnt
=st_do_scsi(SCpnt
, STp
, cmd
,200, STp
->timeout
, MAX_READY_RETRIES
, TRUE
); 2654 scsi_release_command(SCpnt
); 2657 if((STp
->buffer
)->last_result_fatal
!=0) { 2660 printk(ST_DEB_MSG
"st%d: Can't read medium partition page.\n", dev
); 2664 result
= (STp
->buffer
)->b_data
[MODE_HEADER_LENGTH
+3] +1; 2667 printk(ST_DEB_MSG
"st%d: Number of partitions %d.\n", dev
, result
); 2675 /* Partition the tape into two partitions if size > 0 or one partition if 2677 static intpartition_tape(struct inode
*inode
,int size
) 2679 int dev
=TAPE_NR(inode
->i_rdev
), result
; 2682 Scsi_Cmnd
*SCpnt
= NULL
; 2683 unsigned char cmd
[10], *bp
; 2685 if((result
=nbr_partitions(inode
)) <0) 2687 STp
= &(scsi_tapes
[dev
]); 2689 /* The mode page is in the buffer. Let's modify it and write it. */ 2690 bp
= &((STp
->buffer
)->b_data
[0]); 2693 bp
[MODE_HEADER_LENGTH
+3] =0; 2696 printk(ST_DEB_MSG
"st%d: Formatting tape with one partition.\n", dev
); 2700 bp
[MODE_HEADER_LENGTH
+3] =1; 2701 bp
[MODE_HEADER_LENGTH
+8] = (size
>>8) &0xff; 2702 bp
[MODE_HEADER_LENGTH
+9] = size
&0xff; 2705 printk(ST_DEB_MSG
"st%d: Formatting tape with two partition (1 = %d MB).\n", 2709 bp
[MODE_HEADER_LENGTH
+6] =0; 2710 bp
[MODE_HEADER_LENGTH
+7] =0; 2711 bp
[MODE_HEADER_LENGTH
+4] =0x30;/* IDP | PSUM = MB */ 2715 bp
[MODE_HEADER_LENGTH
] &=0x3f; 2716 bp
[MODE_HEADER_LENGTH
+1] = length
-2; 2719 cmd
[0] = MODE_SELECT
; 2721 cmd
[4] = length
+ MODE_HEADER_LENGTH
; 2723 SCpnt
=st_do_scsi(SCpnt
, STp
, cmd
, cmd
[4], STp
->long_timeout
, 2724 MAX_READY_RETRIES
, TRUE
); 2727 scsi_release_command(SCpnt
); 2730 if((STp
->buffer
)->last_result_fatal
!=0) { 2731 printk(KERN_INFO
"st%d: Partitioning of tape failed.\n", dev
); 2741 /* The ioctl command */ 2742 static intst_ioctl(struct inode
*inode
,struct file
*file
, 2743 unsigned int cmd_in
,unsigned long arg
) 2745 int i
, cmd_nr
, cmd_type
, bt
; 2748 struct mtpos mt_pos
; 2752 int dev
=TAPE_NR(inode
->i_rdev
); 2754 STp
= &(scsi_tapes
[dev
]); 2756 if(debugging
&& !STp
->in_use
) { 2757 printk(ST_DEB_MSG
"st%d: Incorrect device.\n", dev
); 2761 STm
= &(STp
->modes
[STp
->current_mode
]); 2762 STps
= &(STp
->ps
[STp
->partition
]); 2765 * If we are in the middle of error recovery, don't let anyone 2766 * else try and use this device. Also, if error recovery fails, it 2767 * may try and take the device offline, in which case all further 2768 * access to the device is prohibited. 2770 if(!scsi_block_when_processing_errors(STp
->device
)) { 2773 cmd_type
=_IOC_TYPE(cmd_in
); 2774 cmd_nr
=_IOC_NR(cmd_in
); 2776 if(cmd_type
==_IOC_TYPE(MTIOCTOP
) && cmd_nr
==_IOC_NR(MTIOCTOP
)) { 2777 if(_IOC_SIZE(cmd_in
) !=sizeof(mtc
)) 2780 i
=copy_from_user((char*) &mtc
, (char*) arg
,sizeof(struct mtop
)); 2784 if(mtc
.mt_op
== MTSETDRVBUFFER
&& !capable(CAP_SYS_ADMIN
)) { 2785 printk(KERN_WARNING
"st%d: MTSETDRVBUFFER only allowed for root.\n", dev
); 2789 (mtc
.mt_op
!= MTSETDRVBUFFER
&& (mtc
.mt_count
& MT_ST_OPTIONS
) ==0)) 2792 if(!(STp
->device
)->was_reset
) { 2794 if(STps
->eof
== ST_FM_HIT
) { 2795 if(mtc
.mt_op
== MTFSF
|| mtc
.mt_op
== MTFSFM
|| mtc
.mt_op
== MTEOM
) { 2797 if(STps
->drv_file
>=0) 2799 }else if(mtc
.mt_op
== MTBSF
|| mtc
.mt_op
== MTBSFM
) { 2801 if(STps
->drv_file
>=0) 2805 if(mtc
.mt_op
== MTSEEK
) { 2806 /* Old position must be restored if partition will be changed */ 2807 i
= !STp
->can_partitions
|| 2808 (STp
->new_partition
!= STp
->partition
); 2810 i
= mtc
.mt_op
== MTREW
|| mtc
.mt_op
== MTOFFL
|| 2811 mtc
.mt_op
== MTRETEN
|| mtc
.mt_op
== MTEOM
|| 2812 mtc
.mt_op
== MTLOCK
|| mtc
.mt_op
== MTLOAD
|| 2813 mtc
.mt_op
== MTCOMPRESSION
; 2815 i
=flush_buffer(inode
, file
, i
); 2820 * If there was a bus reset, block further access 2821 * to this device. If the user wants to rewind the tape, 2822 * then reset the flag and allow access again. 2824 if(mtc
.mt_op
!= MTREW
&& 2825 mtc
.mt_op
!= MTOFFL
&& 2826 mtc
.mt_op
!= MTRETEN
&& 2827 mtc
.mt_op
!= MTERASE
&& 2828 mtc
.mt_op
!= MTSEEK
&& 2831 STp
->device
->was_reset
=0; 2832 if(STp
->door_locked
!= ST_UNLOCKED
&& 2833 STp
->door_locked
!= ST_LOCK_FAILS
) { 2834 if(st_int_ioctl(inode
, MTLOCK
,0)) { 2835 printk(KERN_NOTICE
"st%d: Could not relock door after bus reset.\n", 2837 STp
->door_locked
= ST_UNLOCKED
; 2842 if(mtc
.mt_op
!= MTNOP
&& mtc
.mt_op
!= MTSETBLK
&& 2843 mtc
.mt_op
!= MTSETDENSITY
&& mtc
.mt_op
!= MTWSM
&& 2844 mtc
.mt_op
!= MTSETDRVBUFFER
&& mtc
.mt_op
!= MTSETPART
) 2845 STps
->rw
= ST_IDLE
;/* Prevent automatic WEOF and fsf */ 2847 if(mtc
.mt_op
== MTOFFL
&& STp
->door_locked
!= ST_UNLOCKED
) 2848 st_int_ioctl(inode
, MTUNLOCK
,0);/* Ignore result! */ 2850 if(mtc
.mt_op
== MTSETDRVBUFFER
&& 2851 (mtc
.mt_count
& MT_ST_OPTIONS
) !=0) 2852 returnst_set_options(inode
, mtc
.mt_count
); 2853 if(mtc
.mt_op
== MTSETPART
) { 2854 if(!STp
->can_partitions
|| 2855 mtc
.mt_count
<0|| mtc
.mt_count
>= ST_NBR_PARTITIONS
) 2857 if(mtc
.mt_count
>= STp
->nbr_partitions
&& 2858 (STp
->nbr_partitions
=nbr_partitions(inode
)) <0) 2860 if(mtc
.mt_count
>= STp
->nbr_partitions
) 2862 STp
->new_partition
= mtc
.mt_count
; 2865 if(mtc
.mt_op
== MTMKPART
) { 2866 if(!STp
->can_partitions
) 2868 if((i
=st_int_ioctl(inode
, MTREW
,0)) <0|| 2869 (i
=partition_tape(inode
, mtc
.mt_count
)) <0) 2871 for(i
=0; i
< ST_NBR_PARTITIONS
; i
++) { 2872 STp
->ps
[i
].rw
= ST_IDLE
; 2873 STp
->ps
[i
].at_sm
=0; 2874 STp
->ps
[i
].last_block_valid
= FALSE
; 2876 STp
->partition
= STp
->new_partition
=0; 2877 STp
->nbr_partitions
=1;/* Bad guess ?-) */ 2878 STps
->drv_block
= STps
->drv_file
=0; 2881 if(mtc
.mt_op
== MTSEEK
) { 2882 i
=set_location(inode
, mtc
.mt_count
, STp
->new_partition
,0); 2883 if(!STp
->can_partitions
) 2884 STp
->ps
[0].rw
= ST_IDLE
; 2887 if(STp
->can_partitions
&& STp
->ready
== ST_READY
&& 2888 (i
=update_partition(inode
)) <0) 2890 if(mtc
.mt_op
== MTCOMPRESSION
) 2891 returnst_compression(STp
, (mtc
.mt_count
&1)); 2893 returnst_int_ioctl(inode
, mtc
.mt_op
, mtc
.mt_count
); 2898 if((i
=flush_buffer(inode
, file
, FALSE
)) <0) 2900 if(STp
->can_partitions
&& 2901 (i
=update_partition(inode
)) <0) 2904 if(cmd_type
==_IOC_TYPE(MTIOCGET
) && cmd_nr
==_IOC_NR(MTIOCGET
)) { 2906 if(_IOC_SIZE(cmd_in
) !=sizeof(struct mtget
)) 2909 (STp
->mt_status
)->mt_dsreg
= 2910 ((STp
->block_size
<< MT_ST_BLKSIZE_SHIFT
) & MT_ST_BLKSIZE_MASK
) | 2911 ((STp
->density
<< MT_ST_DENSITY_SHIFT
) & MT_ST_DENSITY_MASK
); 2912 (STp
->mt_status
)->mt_blkno
= STps
->drv_block
; 2913 (STp
->mt_status
)->mt_fileno
= STps
->drv_file
; 2914 if(STp
->block_size
!=0) { 2915 if(STps
->rw
== ST_WRITING
) 2916 (STp
->mt_status
)->mt_blkno
+= 2917 (STp
->buffer
)->buffer_bytes
/ STp
->block_size
; 2918 else if(STps
->rw
== ST_READING
) 2919 (STp
->mt_status
)->mt_blkno
-= ((STp
->buffer
)->buffer_bytes
+ 2920 STp
->block_size
-1) / STp
->block_size
; 2922 (STp
->mt_status
)->mt_gstat
=0; 2923 if(STp
->drv_write_prot
) 2924 (STp
->mt_status
)->mt_gstat
|=GMT_WR_PROT(0xffffffff); 2925 if((STp
->mt_status
)->mt_blkno
==0) { 2926 if((STp
->mt_status
)->mt_fileno
==0) 2927 (STp
->mt_status
)->mt_gstat
|=GMT_BOT(0xffffffff); 2929 (STp
->mt_status
)->mt_gstat
|=GMT_EOF(0xffffffff); 2931 (STp
->mt_status
)->mt_resid
= STp
->partition
; 2932 if(STps
->eof
== ST_EOM_OK
|| STps
->eof
== ST_EOM_ERROR
) 2933 (STp
->mt_status
)->mt_gstat
|=GMT_EOT(0xffffffff); 2934 else if(STps
->eof
>= ST_EOM_OK
) 2935 (STp
->mt_status
)->mt_gstat
|=GMT_EOD(0xffffffff); 2936 if(STp
->density
==1) 2937 (STp
->mt_status
)->mt_gstat
|=GMT_D_800(0xffffffff); 2938 else if(STp
->density
==2) 2939 (STp
->mt_status
)->mt_gstat
|=GMT_D_1600(0xffffffff); 2940 else if(STp
->density
==3) 2941 (STp
->mt_status
)->mt_gstat
|=GMT_D_6250(0xffffffff); 2942 if(STp
->ready
== ST_READY
) 2943 (STp
->mt_status
)->mt_gstat
|=GMT_ONLINE(0xffffffff); 2944 if(STp
->ready
== ST_NO_TAPE
) 2945 (STp
->mt_status
)->mt_gstat
|=GMT_DR_OPEN(0xffffffff); 2947 (STp
->mt_status
)->mt_gstat
|=GMT_SM(0xffffffff); 2948 if(STm
->do_async_writes
|| (STm
->do_buffer_writes
&& STp
->block_size
!=0) || 2949 STp
->drv_buffer
!=0) 2950 (STp
->mt_status
)->mt_gstat
|=GMT_IM_REP_EN(0xffffffff); 2952 i
=copy_to_user((char*) arg
, (char*) (STp
->mt_status
), 2953 sizeof(struct mtget
)); 2957 (STp
->mt_status
)->mt_erreg
=0;/* Clear after read */ 2959 }/* End of MTIOCGET */ 2960 if(cmd_type
==_IOC_TYPE(MTIOCPOS
) && cmd_nr
==_IOC_NR(MTIOCPOS
)) { 2961 if(_IOC_SIZE(cmd_in
) !=sizeof(struct mtpos
)) 2963 if((i
=get_location(inode
, &blk
, &bt
,0)) <0) 2965 mt_pos
.mt_blkno
= blk
; 2966 i
=copy_to_user((char*) arg
, (char*) (&mt_pos
),sizeof(struct mtpos
)); 2971 returnscsi_ioctl(STp
->device
, cmd_in
, (void*) arg
); 2975 /* Try to allocate a new tape buffer */ 2977 new_tape_buffer(int from_initialization
,int need_dma
) 2979 int i
, priority
, b_size
, got
=0, segs
=0; 2982 if(st_nbr_buffers
>= st_template
.dev_max
) 2983 return NULL
;/* Should never happen */ 2985 if(from_initialization
) 2986 priority
= GFP_ATOMIC
; 2988 priority
= GFP_KERNEL
; 2990 i
=sizeof(ST_buffer
) + (st_max_sg_segs
-1) *sizeof(struct scatterlist
); 2991 tb
= (ST_buffer
*)scsi_init_malloc(i
, priority
); 2995 priority
|= GFP_DMA
; 2997 /* Try to allocate the first segment up to ST_FIRST_ORDER and the 2998 others big enough to reach the goal */ 2999 for(b_size
= PAGE_SIZE
<< ST_FIRST_ORDER
; 3000 b_size
/2>= st_buffer_size
&& b_size
> PAGE_SIZE
;) 3002 for(; b_size
>= PAGE_SIZE
; b_size
/=2) { 3004 (unsigned char*)scsi_init_malloc(b_size
, priority
); 3005 if(tb
->sg
[0].address
!= NULL
) { 3006 tb
->sg
[0].alt_address
= NULL
; 3007 tb
->sg
[0].length
= b_size
; 3011 if(tb
->sg
[segs
].address
== NULL
) { 3012 scsi_init_free((char*) tb
, tb
->this_size
); 3014 }else{/* Got something, continue */ 3016 for(b_size
= PAGE_SIZE
; 3017 st_buffer_size
> tb
->sg
[0].length
+ (ST_FIRST_SG
-1) * b_size
;) 3020 for(segs
=1, got
= tb
->sg
[0].length
; 3021 got
< st_buffer_size
&& segs
< ST_FIRST_SG
;) { 3022 tb
->sg
[segs
].address
= 3023 (unsigned char*)scsi_init_malloc(b_size
, priority
); 3024 if(tb
->sg
[segs
].address
== NULL
) { 3025 if(st_buffer_size
- got
<= 3026 (ST_FIRST_SG
- segs
) * b_size
/2) { 3027 b_size
/=2;/* Large enough for the rest of the buffers */ 3030 for(i
=0; i
< segs
-1; i
++) 3031 scsi_init_free(tb
->sg
[i
].address
, tb
->sg
[i
].length
); 3032 scsi_init_free((char*) tb
, tb
->this_size
); 3036 tb
->sg
[segs
].alt_address
= NULL
; 3037 tb
->sg
[segs
].length
= b_size
; 3044 printk(KERN_NOTICE
"st: Can't allocate new tape buffer (nbr %d).\n", 3048 tb
->sg_segs
= tb
->orig_sg_segs
= segs
; 3049 tb
->b_data
= tb
->sg
[0].address
; 3054 "st: Allocated tape buffer %d (%d bytes, %d segments, dma: %d, a: %p).\n", 3055 st_nbr_buffers
, got
, tb
->sg_segs
, need_dma
, tb
->b_data
); 3057 "st: segment sizes: first %d, last %d bytes.\n", 3058 tb
->sg
[0].length
, tb
->sg
[segs
-1].length
); 3063 tb
->buffer_size
= got
; 3065 st_buffers
[st_nbr_buffers
++] = tb
; 3071 /* Try to allocate a temporary enlarged tape buffer */ 3072 static intenlarge_buffer(ST_buffer
* STbuffer
,int new_size
,int need_dma
) 3074 int segs
, nbr
, max_segs
, b_size
, priority
, got
; 3076 normalize_buffer(STbuffer
); 3078 max_segs
= STbuffer
->use_sg
; 3079 if(max_segs
> st_max_sg_segs
) 3080 max_segs
= st_max_sg_segs
; 3081 nbr
= max_segs
- STbuffer
->sg_segs
; 3085 priority
= GFP_KERNEL
; 3087 priority
|= GFP_DMA
; 3088 for(b_size
= PAGE_SIZE
; b_size
* nbr
< new_size
- STbuffer
->buffer_size
;) 3091 for(segs
= STbuffer
->sg_segs
, got
= STbuffer
->buffer_size
; 3092 segs
< max_segs
&& got
< new_size
;) { 3093 STbuffer
->sg
[segs
].address
= 3094 (unsigned char*)scsi_init_malloc(b_size
, priority
); 3095 if(STbuffer
->sg
[segs
].address
== NULL
) { 3096 if(new_size
- got
<= (max_segs
- segs
) * b_size
/2) { 3097 b_size
/=2;/* Large enough for the rest of the buffers */ 3100 printk(KERN_NOTICE
"st: failed to enlarge buffer to %d bytes.\n", 3102 normalize_buffer(STbuffer
); 3105 STbuffer
->sg
[segs
].alt_address
= NULL
; 3106 STbuffer
->sg
[segs
].length
= b_size
; 3107 STbuffer
->sg_segs
+=1; 3109 STbuffer
->buffer_size
= got
; 3115 "st: Succeeded to enlarge buffer to %d bytes (segs %d->%d, %d).\n", 3116 got
, STbuffer
->orig_sg_segs
, STbuffer
->sg_segs
, b_size
); 3123 /* Release the extra buffer */ 3124 static voidnormalize_buffer(ST_buffer
* STbuffer
) 3128 for(i
= STbuffer
->orig_sg_segs
; i
< STbuffer
->sg_segs
; i
++) { 3129 scsi_init_free(STbuffer
->sg
[i
].address
, STbuffer
->sg
[i
].length
); 3130 STbuffer
->buffer_size
-= STbuffer
->sg
[i
].length
; 3133 if(debugging
&& STbuffer
->orig_sg_segs
< STbuffer
->sg_segs
) 3134 printk(ST_DEB_MSG
"st: Buffer at %p normalized to %d bytes (segs %d).\n", 3135 STbuffer
->b_data
, STbuffer
->buffer_size
, STbuffer
->sg_segs
); 3137 STbuffer
->sg_segs
= STbuffer
->orig_sg_segs
; 3141 /* Move data from the user buffer to the tape buffer. Returns zero (success) or 3142 negative error code. */ 3143 static intappend_to_buffer(const char*ubp
, ST_buffer
* st_bp
,int do_count
) 3145 int i
, cnt
, res
, offset
; 3147 for(i
=0, offset
= st_bp
->buffer_bytes
; 3148 i
< st_bp
->sg_segs
&& offset
>= st_bp
->sg
[i
].length
; i
++) 3149 offset
-= st_bp
->sg
[i
].length
; 3150 if(i
== st_bp
->sg_segs
) {/* Should never happen */ 3151 printk(KERN_WARNING
"st: append_to_buffer offset overflow.\n"); 3154 for(; i
< st_bp
->sg_segs
&& do_count
>0; i
++) { 3155 cnt
= st_bp
->sg
[i
].length
- offset
< do_count
? 3156 st_bp
->sg
[i
].length
- offset
: do_count
; 3157 res
=copy_from_user(st_bp
->sg
[i
].address
+ offset
, ubp
, cnt
); 3161 st_bp
->buffer_bytes
+= cnt
; 3165 if(do_count
) {/* Should never happen */ 3166 printk(KERN_WARNING
"st: append_to_buffer overflow (left %d).\n", 3174 /* Move data from the tape buffer to the user buffer. Returns zero (success) or 3175 negative error code. */ 3176 static intfrom_buffer(ST_buffer
* st_bp
,char*ubp
,int do_count
) 3178 int i
, cnt
, res
, offset
; 3180 for(i
=0, offset
= st_bp
->read_pointer
; 3181 i
< st_bp
->sg_segs
&& offset
>= st_bp
->sg
[i
].length
; i
++) 3182 offset
-= st_bp
->sg
[i
].length
; 3183 if(i
== st_bp
->sg_segs
) {/* Should never happen */ 3184 printk(KERN_WARNING
"st: from_buffer offset overflow.\n"); 3187 for(; i
< st_bp
->sg_segs
&& do_count
>0; i
++) { 3188 cnt
= st_bp
->sg
[i
].length
- offset
< do_count
? 3189 st_bp
->sg
[i
].length
- offset
: do_count
; 3190 res
=copy_to_user(ubp
, st_bp
->sg
[i
].address
+ offset
, cnt
); 3194 st_bp
->buffer_bytes
-= cnt
; 3195 st_bp
->read_pointer
+= cnt
; 3199 if(do_count
) {/* Should never happen */ 3200 printk(KERN_WARNING
"st: from_buffer overflow (left %d).\n", 3208 /* Validate the options from command line or module parameters */ 3209 static voidvalidate_options(void) 3212 st_buffer_size
= buffer_kbs
* ST_KILOBYTE
; 3213 if(write_threshold_kbs
>0) 3214 st_write_threshold
= write_threshold_kbs
* ST_KILOBYTE
; 3215 else if(buffer_kbs
>0) 3216 st_write_threshold
= st_buffer_size
-2048; 3217 if(st_write_threshold
> st_buffer_size
) { 3218 st_write_threshold
= st_buffer_size
; 3219 printk(KERN_WARNING
"st: write_threshold limited to %d bytes.\n", 3220 st_write_threshold
); 3223 st_max_buffers
= max_buffers
; 3224 if(max_sg_segs
>= ST_FIRST_SG
) 3225 st_max_sg_segs
= max_sg_segs
; 3229 /* Set the boot options. Syntax is defined in README.st. 3231 static int __init
st_setup(char*str
) 3233 int i
, len
, ints
[5]; 3236 stp
=get_options(str
,ARRAY_SIZE(ints
), ints
); 3239 for(i
=0; i
< ints
[0] && i
<ARRAY_SIZE(parms
); i
++) 3240 *parms
[i
].val
= ints
[i
+1]; 3242 while(stp
!= NULL
) { 3243 for(i
=0; i
<ARRAY_SIZE(parms
); i
++) { 3244 len
=strlen(parms
[i
].name
); 3245 if(!strncmp(stp
, parms
[i
].name
, len
) && 3246 (*(stp
+ len
) ==':'|| *(stp
+ len
) =='=')) { 3247 *parms
[i
].val
=simple_strtoul(stp
+ len
+1, NULL
,0); 3251 if(i
>=sizeof(parms
) /sizeof(struct st_dev_parm
)) 3252 printk(KERN_WARNING
"st: illegal parameter in '%s'\n", 3254 stp
=strchr(stp
,','); 3265 __setup("st=", st_setup
); 3270 static struct file_operations st_fops
= 3272 NULL
,/* lseek - default */ 3273 st_read
,/* read - general block-dev read */ 3274 st_write
,/* write - general block-dev write */ 3275 NULL
,/* readdir - bad */ 3277 st_ioctl
,/* ioctl */ 3279 scsi_tape_open
,/* open */ 3280 scsi_tape_flush
,/* flush */ 3281 scsi_tape_close
,/* release */ 3285 static intst_attach(Scsi_Device
* SDp
) 3292 if(SDp
->type
!= TYPE_TAPE
) 3295 if(st_template
.nr_dev
>= st_template
.dev_max
) { 3299 for(tpnt
= scsi_tapes
, i
=0; i
< st_template
.dev_max
; i
++, tpnt
++) 3303 if(i
>= st_template
.dev_max
) 3304 panic("scsi_devices corrupt (st)"); 3306 scsi_tapes
[i
].device
= SDp
; 3307 if(SDp
->scsi_level
<=2) 3308 scsi_tapes
[i
].mt_status
->mt_type
= MT_ISSCSI1
; 3310 scsi_tapes
[i
].mt_status
->mt_type
= MT_ISSCSI2
; 3312 tpnt
->devt
=MKDEV(SCSI_TAPE_MAJOR
, i
); 3315 tpnt
->drv_buffer
=1;/* Try buffering if no mode sense */ 3316 tpnt
->restr_dma
= (SDp
->host
)->unchecked_isa_dma
; 3318 tpnt
->do_auto_lock
= ST_AUTO_LOCK
; 3319 tpnt
->can_bsr
= ST_IN_FILE_POS
; 3320 tpnt
->can_partitions
=0; 3321 tpnt
->two_fm
= ST_TWO_FM
; 3322 tpnt
->fast_mteom
= ST_FAST_MTEOM
; 3323 tpnt
->scsi2_logical
= ST_SCSI2LOGICAL
; 3324 tpnt
->write_threshold
= st_write_threshold
; 3325 tpnt
->default_drvbuffer
=0xff;/* No forced buffering */ 3327 tpnt
->new_partition
=0; 3328 tpnt
->nbr_partitions
=0; 3329 tpnt
->timeout
= ST_TIMEOUT
; 3330 tpnt
->long_timeout
= ST_LONG_TIMEOUT
; 3332 for(i
=0; i
< ST_NBR_MODES
; i
++) { 3333 STm
= &(tpnt
->modes
[i
]); 3334 STm
->defined
= FALSE
; 3335 STm
->sysv
= ST_SYSV
; 3336 STm
->defaults_for_writes
=0; 3337 STm
->do_async_writes
= ST_ASYNC_WRITES
; 3338 STm
->do_buffer_writes
= ST_BUFFER_WRITES
; 3339 STm
->do_read_ahead
= ST_READ_AHEAD
; 3340 STm
->default_compression
= ST_DONT_TOUCH
; 3341 STm
->default_blksize
= (-1);/* No forced size */ 3342 STm
->default_density
= (-1);/* No forced density */ 3345 for(i
=0; i
< ST_NBR_PARTITIONS
; i
++) { 3346 STps
= &(tpnt
->ps
[i
]); 3348 STps
->eof
= ST_NOEOF
; 3350 STps
->last_block_valid
= FALSE
; 3351 STps
->drv_block
= (-1); 3352 STps
->drv_file
= (-1); 3355 tpnt
->current_mode
=0; 3356 tpnt
->modes
[0].defined
= TRUE
; 3358 tpnt
->density_changed
= tpnt
->compression_changed
= 3359 tpnt
->blksize_changed
= FALSE
; 3361 st_template
.nr_dev
++; 3365 static intst_detect(Scsi_Device
* SDp
) 3367 if(SDp
->type
!= TYPE_TAPE
) 3371 "Detected scsi tape st%d at scsi%d, channel %d, id %d, lun %d\n", 3372 st_template
.dev_noticed
++, 3373 SDp
->host
->host_no
, SDp
->channel
, SDp
->id
, SDp
->lun
); 3378 static int st_registered
=0; 3380 /* Driver initialization (not __init because may be called later) */ 3387 if(st_template
.dev_noticed
==0) 3390 printk(KERN_INFO
"st: bufsize %d, wrt %d, max init. buffers %d, s/g segs %d.\n", 3391 st_buffer_size
, st_write_threshold
, st_max_buffers
, st_max_sg_segs
); 3393 if(!st_registered
) { 3394 if(register_chrdev(SCSI_TAPE_MAJOR
,"st", &st_fops
)) { 3395 printk(KERN_ERR
"Unable to get major %d for SCSI tapes\n", MAJOR_NR
); 3402 st_template
.dev_max
= st_template
.dev_noticed
+ ST_EXTRA_DEVS
; 3403 if(st_template
.dev_max
< ST_MAX_TAPES
) 3404 st_template
.dev_max
= ST_MAX_TAPES
; 3405 if(st_template
.dev_max
>128/ ST_NBR_MODES
) 3406 printk(KERN_INFO
"st: Only %d tapes accessible.\n",128/ ST_NBR_MODES
); 3408 (Scsi_Tape
*)scsi_init_malloc(st_template
.dev_max
*sizeof(Scsi_Tape
), 3410 if(scsi_tapes
== NULL
) { 3411 printk(KERN_ERR
"Unable to allocate descriptors for SCSI tapes.\n"); 3412 unregister_chrdev(SCSI_TAPE_MAJOR
,"st"); 3416 printk(ST_DEB_MSG
"st: Buffer size %d bytes, write threshold %d bytes.\n", 3417 st_buffer_size
, st_write_threshold
); 3420 memset(scsi_tapes
,0, st_template
.dev_max
*sizeof(Scsi_Tape
)); 3421 for(i
=0; i
< st_template
.dev_max
; ++i
) { 3422 STp
= &(scsi_tapes
[i
]); 3423 STp
->capacity
=0xfffff; 3424 STp
->mt_status
= (struct mtget
*)scsi_init_malloc(sizeof(struct mtget
), 3426 /* Initialize status */ 3427 memset((void*) scsi_tapes
[i
].mt_status
,0,sizeof(struct mtget
)); 3430 /* Allocate the buffers */ 3432 (ST_buffer
**)scsi_init_malloc(st_template
.dev_max
*sizeof(ST_buffer
*), 3434 if(st_buffers
== NULL
) { 3435 printk(KERN_ERR
"Unable to allocate tape buffer pointers.\n"); 3436 unregister_chrdev(SCSI_TAPE_MAJOR
,"st"); 3437 scsi_init_free((char*) scsi_tapes
, 3438 st_template
.dev_max
*sizeof(Scsi_Tape
)); 3441 target_nbr
= st_template
.dev_noticed
; 3442 if(target_nbr
< ST_EXTRA_DEVS
) 3443 target_nbr
= ST_EXTRA_DEVS
; 3444 if(target_nbr
> st_max_buffers
) 3445 target_nbr
= st_max_buffers
; 3447 for(i
= st_nbr_buffers
=0; i
< target_nbr
; i
++) { 3448 if(!new_tape_buffer(TRUE
, TRUE
)) { 3450 printk(KERN_INFO
"No tape buffers allocated at initialization.\n"); 3453 printk(KERN_INFO
"Number of tape buffers adjusted.\n"); 3461 static voidst_detach(Scsi_Device
* SDp
) 3466 for(tpnt
= scsi_tapes
, i
=0; i
< st_template
.dev_max
; i
++, tpnt
++) 3467 if(tpnt
->device
== SDp
) { 3468 tpnt
->device
= NULL
; 3470 st_template
.nr_dev
--; 3471 st_template
.dev_noticed
--; 3480 int __init
init_module(void) 3486 st_template
.module
= &__this_module
; 3487 result
=scsi_register_module(MODULE_SCSI_DEV
, &st_template
); 3494 voidcleanup_module(void) 3498 scsi_unregister_module(MODULE_SCSI_DEV
, &st_template
); 3499 unregister_chrdev(SCSI_TAPE_MAJOR
,"st"); 3501 if(scsi_tapes
!= NULL
) { 3502 scsi_init_free((char*) scsi_tapes
, 3503 st_template
.dev_max
*sizeof(Scsi_Tape
)); 3505 if(st_buffers
!= NULL
) { 3506 for(i
=0; i
< st_nbr_buffers
; i
++) 3508 if(st_buffers
[i
] != NULL
) { 3509 for(j
=0; j
< st_buffers
[i
]->sg_segs
; j
++) 3510 scsi_init_free((char*) st_buffers
[i
]->sg
[j
].address
, 3511 st_buffers
[i
]->sg
[j
].length
); 3512 scsi_init_free((char*) st_buffers
[i
], st_buffers
[i
]->this_size
); 3515 scsi_init_free((char*) st_buffers
, st_template
.dev_max
*sizeof(ST_buffer
*)); 3518 st_template
.dev_max
=0; 3519 printk(KERN_INFO
"st: Unloaded.\n");