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 - 1997 Kai Makisara 12 email Kai.Makisara@metla.fi 14 Last modified: Wed Nov 5 23:39:52 1997 by makisara@home 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 <asm/uaccess.h> 32 #include <asm/system.h> 34 /* The driver prints some debugging information on the console if DEBUG 35 is defined and non-zero. */ 38 /* The message level for the debug messages is currently set to KERN_NOTICE 39 so that people can easily see the messages. Later when the debugging messages 40 in the drivers are more widely classified, this may be changed to KERN_DEBUG. */ 41 #define ST_DEB_MSG KERN_NOTICE 43 #define MAJOR_NR SCSI_TAPE_MAJOR 44 #include <linux/blk.h> 48 #include <scsi/scsi_ioctl.h> 53 MODULE_PARM(buffer_kbs
,"i"); 54 MODULE_PARM(write_threshold_kbs
,"i"); 55 MODULE_PARM(max_buffers
,"i"); 56 static int buffer_kbs
=0; 57 static int write_threshold_kbs
=0; 58 static int max_buffers
=0; 61 /* The default definitions have been moved to st_options.h */ 63 #define ST_KILOBYTE 1024 65 #include"st_options.h" 67 #define ST_BUFFER_SIZE (ST_BUFFER_BLOCKS * ST_KILOBYTE) 68 #define ST_WRITE_THRESHOLD (ST_WRITE_THRESHOLD_BLOCKS * ST_KILOBYTE) 70 /* The buffer size should fit into the 24 bits for length in the 71 6-byte SCSI read and write commands. */ 72 #if ST_BUFFER_SIZE >= (2 << 24 - 1) 73 #error"Buffer size should not exceed (2 << 24 - 1) bytes!" 77 static int debugging
=1; 81 #define MAX_WRITE_RETRIES 0 82 #define MAX_READY_RETRIES 5 83 #define NO_TAPE NOT_READY 85 #define ST_TIMEOUT (900 * HZ) 86 #define ST_LONG_TIMEOUT (14000 * HZ) 88 #define TAPE_NR(x) (MINOR(x) & ~(128 | ST_MODE_MASK)) 89 #define TAPE_MODE(x) ((MINOR(x) & ST_MODE_MASK) >> ST_MODE_SHIFT) 91 /* Internal ioctl to set both density (uppermost 8 bits) and blocksize (lower 93 #define SET_DENS_AND_BLK 0x10001 95 static int st_nbr_buffers
; 96 static ST_buffer
**st_buffers
; 97 static int st_buffer_size
= ST_BUFFER_SIZE
; 98 static int st_write_threshold
= ST_WRITE_THRESHOLD
; 99 static int st_max_buffers
= ST_MAX_BUFFERS
; 101 static Scsi_Tape
* scsi_tapes
= NULL
; 103 static int modes_defined
= FALSE
; 105 static ST_buffer
*new_tape_buffer(int,int); 106 static intenlarge_buffer(ST_buffer
*,int,int); 107 static voidnormalize_buffer(ST_buffer
*); 109 static intst_init(void); 110 static intst_attach(Scsi_Device
*); 111 static intst_detect(Scsi_Device
*); 112 static voidst_detach(Scsi_Device
*); 114 struct Scsi_Device_Template st_template
= {NULL
,"tape","st", NULL
, TYPE_TAPE
, 115 SCSI_TAPE_MAJOR
,0,0,0,0, 117 NULL
, st_attach
, st_detach
}; 119 static intst_compression(Scsi_Tape
*,int); 121 static intfind_partition(struct inode
*); 122 static intupdate_partition(struct inode
*); 124 static intst_int_ioctl(struct inode
* inode
,unsigned int cmd_in
, 130 /* Convert the result to success code */ 132 st_chk_result(Scsi_Cmnd
* SCpnt
) 134 int dev
=TAPE_NR(SCpnt
->request
.rq_dev
); 135 int result
= SCpnt
->result
; 136 unsigned char* sense
= SCpnt
->sense_buffer
, scode
; 141 if(!result
/* && SCpnt->sense_buffer[0] == 0 */) 144 scode
= sense
[2] &0x0f; 148 printk(ST_DEB_MSG
"st%d: Error: %x, cmd: %x %x %x %x %x %x Len: %d\n", 150 SCpnt
->data_cmnd
[0], SCpnt
->data_cmnd
[1], SCpnt
->data_cmnd
[2], 151 SCpnt
->data_cmnd
[3], SCpnt
->data_cmnd
[4], SCpnt
->data_cmnd
[5], 152 SCpnt
->request_bufflen
); 153 if(driver_byte(result
) & DRIVER_SENSE
) 154 print_sense("st", SCpnt
); 160 if(!(driver_byte(result
) & DRIVER_SENSE
) || 161 ((sense
[0] &0x70) ==0x70&& 163 scode
!= RECOVERED_ERROR
&& 164 /* scode != UNIT_ATTENTION && */ 165 scode
!= BLANK_CHECK
&& 166 scode
!= VOLUME_OVERFLOW
&& 167 SCpnt
->data_cmnd
[0] != MODE_SENSE
&& 168 SCpnt
->data_cmnd
[0] != TEST_UNIT_READY
)) {/* Abnormal conditions for tape */ 169 if(driver_byte(result
) & DRIVER_SENSE
) { 170 printk(KERN_WARNING
"st%d: Error with sense data: ", dev
); 171 print_sense("st", SCpnt
); 174 printk(KERN_WARNING
"st%d: Error %x.\n", dev
, result
); 177 if((sense
[0] &0x70) ==0x70&& 178 scode
== RECOVERED_ERROR
179 #if ST_RECOVERED_WRITE_FATAL 180 && SCpnt
->data_cmnd
[0] != WRITE_6
181 && SCpnt
->data_cmnd
[0] != WRITE_FILEMARKS
184 scsi_tapes
[dev
].recover_count
++; 185 scsi_tapes
[dev
].mt_status
->mt_erreg
+= (1<< MT_ST_SOFTERR_SHIFT
); 188 if(SCpnt
->data_cmnd
[0] == READ_6
) 190 else if(SCpnt
->data_cmnd
[0] == WRITE_6
) 194 printk(ST_DEB_MSG
"st%d: Recovered %s error (%d).\n", dev
, stp
, 195 scsi_tapes
[dev
].recover_count
); 198 if((sense
[2] &0xe0) ==0) 205 /* Wakeup from interrupt */ 207 st_sleep_done(Scsi_Cmnd
* SCpnt
) 213 if((st_nbr
=TAPE_NR(SCpnt
->request
.rq_dev
)) < st_template
.nr_dev
) { 214 STp
= &(scsi_tapes
[st_nbr
]); 215 if((STp
->buffer
)->writing
&& 216 (SCpnt
->sense_buffer
[0] &0x70) ==0x70&& 217 (SCpnt
->sense_buffer
[2] &0x40)) { 218 /* EOM at write-behind, has all been written? */ 219 if((SCpnt
->sense_buffer
[0] &0x80) !=0) 220 remainder
= (SCpnt
->sense_buffer
[3] <<24) | 221 (SCpnt
->sense_buffer
[4] <<16) | 222 (SCpnt
->sense_buffer
[5] <<8) | SCpnt
->sense_buffer
[6]; 225 if((SCpnt
->sense_buffer
[2] &0x0f) == VOLUME_OVERFLOW
|| 227 (STp
->buffer
)->last_result
= SCpnt
->result
;/* Error */ 229 (STp
->buffer
)->last_result
= INT_MAX
;/* OK */ 232 (STp
->buffer
)->last_result
= SCpnt
->result
; 233 SCpnt
->request
.rq_status
= RQ_SCSI_DONE
; 234 (STp
->buffer
)->last_SCpnt
= SCpnt
; 237 STp
->write_pending
=0; 239 up(SCpnt
->request
.sem
); 243 printk(KERN_ERR
"st?: Illegal interrupt device %x\n", st_nbr
); 248 /* Do the scsi command */ 250 st_do_scsi(Scsi_Cmnd
*SCpnt
, Scsi_Tape
*STp
,unsigned char*cmd
,int bytes
, 251 int timeout
,int retries
) 254 if((SCpnt
=scsi_allocate_device(NULL
, STp
->device
,1)) == NULL
) { 255 printk(KERN_ERR
"st%d: Can't get SCSI request.\n",TAPE_NR(STp
->devt
)); 259 cmd
[1] |= (SCpnt
->lun
<<5) &0xe0; 260 STp
->sem
= MUTEX_LOCKED
; 261 SCpnt
->request
.sem
= &(STp
->sem
); 262 SCpnt
->request
.rq_status
= RQ_SCSI_BUSY
; 263 SCpnt
->request
.rq_dev
= STp
->devt
; 265 scsi_do_cmd(SCpnt
, (void*)cmd
, (STp
->buffer
)->b_data
, bytes
, 266 st_sleep_done
, timeout
, retries
); 268 down(SCpnt
->request
.sem
); 270 (STp
->buffer
)->last_result_fatal
=st_chk_result(SCpnt
); 276 /* Handle the write-behind checking */ 278 write_behind_check(Scsi_Tape
*STp
) 280 ST_buffer
* STbuffer
; 283 STbuffer
= STp
->buffer
; 286 if(STp
->write_pending
) 294 (STp
->buffer
)->last_result_fatal
=st_chk_result((STp
->buffer
)->last_SCpnt
); 295 scsi_release_command((STp
->buffer
)->last_SCpnt
); 297 if(STbuffer
->writing
< STbuffer
->buffer_bytes
) 298 memcpy(STbuffer
->b_data
, 299 STbuffer
->b_data
+ STbuffer
->writing
, 300 STbuffer
->buffer_bytes
- STbuffer
->writing
); 301 STbuffer
->buffer_bytes
-= STbuffer
->writing
; 302 STps
= &(STp
->ps
[STp
->partition
]); 303 if(STps
->drv_block
>=0) { 304 if(STp
->block_size
==0) 307 STps
->drv_block
+= STbuffer
->writing
/ STp
->block_size
; 309 STbuffer
->writing
=0; 315 /* Step over EOF if it has been inadvertently crossed (ioctl not used because 316 it messes up the block number). */ 318 cross_eof(Scsi_Tape
*STp
,int forward
) 321 unsigned char cmd
[10]; 324 cmd
[1] =0x01;/* Space FileMarks */ 330 cmd
[2] = cmd
[3] = cmd
[4] =0xff;/* -1 filemarks */ 334 printk(ST_DEB_MSG
"st%d: Stepping over filemark %s.\n", 335 TAPE_NR(STp
->devt
), forward
?"forward":"backward"); 338 SCpnt
=st_do_scsi(NULL
, STp
, cmd
,0, STp
->timeout
, MAX_RETRIES
); 342 scsi_release_command(SCpnt
); 345 if((STp
->buffer
)->last_result
!=0) 346 printk(KERN_ERR
"st%d: Stepping over filemark %s failed.\n", 347 TAPE_NR(STp
->devt
), forward
?"forward":"backward"); 349 return(STp
->buffer
)->last_result_fatal
; 353 /* Flush the write buffer (never need to write if variable blocksize). */ 355 flush_write_buffer(Scsi_Tape
*STp
) 357 int offset
, transfer
, blks
; 359 unsigned char cmd
[10]; 363 if((STp
->buffer
)->writing
) { 364 write_behind_check(STp
); 365 if((STp
->buffer
)->last_result_fatal
) { 368 printk(ST_DEB_MSG
"st%d: Async write error (flush) %x.\n", 369 TAPE_NR(STp
->devt
), (STp
->buffer
)->last_result
); 371 if((STp
->buffer
)->last_result
== INT_MAX
) 377 if(STp
->block_size
==0) 383 offset
= (STp
->buffer
)->buffer_bytes
; 384 transfer
= ((offset
+ STp
->block_size
-1) / 385 STp
->block_size
) * STp
->block_size
; 388 printk(ST_DEB_MSG
"st%d: Flushing %d bytes.\n",TAPE_NR(STp
->devt
), transfer
); 390 memset((STp
->buffer
)->b_data
+ offset
,0, transfer
- offset
); 395 blks
= transfer
/ STp
->block_size
; 400 SCpnt
=st_do_scsi(NULL
, STp
, cmd
, transfer
, STp
->timeout
, MAX_WRITE_RETRIES
); 404 STps
= &(STp
->ps
[STp
->partition
]); 405 if((STp
->buffer
)->last_result_fatal
!=0) { 406 if((SCpnt
->sense_buffer
[0] &0x70) ==0x70&& 407 (SCpnt
->sense_buffer
[2] &0x40) && 408 (SCpnt
->sense_buffer
[2] &0x0f) == NO_SENSE
) { 410 (STp
->buffer
)->buffer_bytes
=0; 414 printk(KERN_ERR
"st%d: Error on flush.\n",TAPE_NR(STp
->devt
)); 417 STps
->drv_block
= (-1); 420 if(STps
->drv_block
>=0) 421 STps
->drv_block
+= blks
; 423 (STp
->buffer
)->buffer_bytes
=0; 425 scsi_release_command(SCpnt
); 432 /* Flush the tape buffer. The tape will be positioned correctly unless 433 seek_next is true. */ 435 flush_buffer(struct inode
* inode
,struct file
* filp
,int seek_next
) 437 int backspace
, result
; 439 ST_buffer
* STbuffer
; 441 int dev
=TAPE_NR(inode
->i_rdev
); 443 STp
= &(scsi_tapes
[dev
]); 444 STbuffer
= STp
->buffer
; 447 * If there was a bus reset, block further access 450 if( STp
->device
->was_reset
) 453 if(STp
->ready
!= ST_READY
) 456 STps
= &(STp
->ps
[STp
->partition
]); 457 if(STps
->rw
== ST_WRITING
)/* Writing */ 458 returnflush_write_buffer(STp
); 460 if(STp
->block_size
==0) 463 backspace
= ((STp
->buffer
)->buffer_bytes
+ 464 (STp
->buffer
)->read_pointer
) / STp
->block_size
- 465 ((STp
->buffer
)->read_pointer
+ STp
->block_size
-1) / 467 (STp
->buffer
)->buffer_bytes
=0; 468 (STp
->buffer
)->read_pointer
=0; 471 if(STps
->eof
== ST_FM_HIT
) { 472 result
=cross_eof(STp
, FALSE
);/* Back over the EOF hit */ 474 STps
->eof
= ST_NOEOF
; 476 if(STps
->drv_file
>=0) 481 if(!result
&& backspace
>0) 482 result
=st_int_ioctl(inode
, MTBSR
, backspace
); 484 else if(STps
->eof
== ST_FM_HIT
) { 485 if(STps
->drv_file
>=0) 488 STps
->eof
= ST_NOEOF
; 495 \f/* Set the mode parameters */ 497 set_mode_densblk(struct inode
* inode
, Scsi_Tape
*STp
, ST_mode
*STm
) 501 int dev
=TAPE_NR(inode
->i_rdev
); 503 if(!STp
->density_changed
&& 504 STm
->default_density
>=0&& 505 STm
->default_density
!= STp
->density
) { 506 arg
= STm
->default_density
; 511 arg
<<= MT_ST_DENSITY_SHIFT
; 512 if(!STp
->blksize_changed
&& 513 STm
->default_blksize
>=0&& 514 STm
->default_blksize
!= STp
->block_size
) { 515 arg
|= STm
->default_blksize
; 519 arg
|= STp
->block_size
; 521 st_int_ioctl(inode
, SET_DENS_AND_BLK
, arg
)) { 523 "st%d: Can't set default block size to %d bytes and density %x.\n", 524 dev
, STm
->default_blksize
, STm
->default_density
); 532 /* Open the device */ 534 scsi_tape_open(struct inode
* inode
,struct file
* filp
) 536 unsigned short flags
; 537 int i
, need_dma_buffer
, new_session
= FALSE
; 538 unsigned char cmd
[10]; 543 int dev
=TAPE_NR(inode
->i_rdev
); 544 int mode
=TAPE_MODE(inode
->i_rdev
); 546 if(dev
>= st_template
.dev_max
|| !scsi_tapes
[dev
].device
) 549 if( !scsi_block_when_processing_errors(scsi_tapes
[dev
].device
) ) 554 STp
= &(scsi_tapes
[dev
]); 557 printk(ST_DEB_MSG
"st%d: Device already in use.\n", dev
); 561 STp
->rew_at_close
= (MINOR(inode
->i_rdev
) &0x80) ==0; 563 if(mode
!= STp
->current_mode
) { 566 printk(ST_DEB_MSG
"st%d: Mode change from %d to %d.\n", 567 dev
, STp
->current_mode
, mode
); 570 STp
->current_mode
= mode
; 572 STm
= &(STp
->modes
[STp
->current_mode
]); 574 /* Allocate buffer for this user */ 575 need_dma_buffer
= STp
->restr_dma
; 576 for(i
=0; i
< st_nbr_buffers
; i
++) 577 if(!st_buffers
[i
]->in_use
&& 578 (!need_dma_buffer
|| st_buffers
[i
]->dma
)) 580 if(i
>= st_nbr_buffers
) { 581 STp
->buffer
=new_tape_buffer(FALSE
, need_dma_buffer
); 582 if(STp
->buffer
== NULL
) { 583 printk(KERN_WARNING
"st%d: Can't allocate tape buffer.\n", dev
); 588 STp
->buffer
= st_buffers
[i
]; 589 (STp
->buffer
)->in_use
=1; 590 (STp
->buffer
)->writing
=0; 591 (STp
->buffer
)->last_result_fatal
=0; 593 flags
= filp
->f_flags
; 594 STp
->write_prot
= ((flags
& O_ACCMODE
) == O_RDONLY
); 597 for(i
=0; i
< ST_NBR_PARTITIONS
; i
++) { 598 STps
= &(STp
->ps
[i
]); 601 STp
->ready
= ST_READY
; 602 STp
->recover_count
=0; 604 STp
->nbr_waits
= STp
->nbr_finished
=0; 607 if(scsi_tapes
[dev
].device
->host
->hostt
->module
) 608 __MOD_INC_USE_COUNT(scsi_tapes
[dev
].device
->host
->hostt
->module
); 609 if(st_template
.module
) 610 __MOD_INC_USE_COUNT(st_template
.module
); 612 memset((void*) &cmd
[0],0,10); 613 cmd
[0] = TEST_UNIT_READY
; 615 SCpnt
=st_do_scsi(NULL
, STp
, cmd
,0, STp
->long_timeout
, MAX_READY_RETRIES
); 617 if(scsi_tapes
[dev
].device
->host
->hostt
->module
) 618 __MOD_DEC_USE_COUNT(scsi_tapes
[dev
].device
->host
->hostt
->module
); 619 if(st_template
.module
) 620 __MOD_DEC_USE_COUNT(st_template
.module
); 624 if((SCpnt
->sense_buffer
[0] &0x70) ==0x70&& 625 (SCpnt
->sense_buffer
[2] &0x0f) == UNIT_ATTENTION
) {/* New media? */ 626 memset((void*) &cmd
[0],0,10); 627 cmd
[0] = TEST_UNIT_READY
; 629 SCpnt
=st_do_scsi(SCpnt
, STp
, cmd
,0, STp
->long_timeout
, MAX_READY_RETRIES
); 631 (STp
->device
)->was_reset
=0; 632 STp
->partition
= STp
->new_partition
=0; 633 if(STp
->can_partitions
) 634 STp
->nbr_partitions
=1;/* This guess will be updated later if necessary */ 635 for(i
=0; i
< ST_NBR_PARTITIONS
; i
++) { 636 STps
= &(STp
->ps
[i
]); 638 STps
->eof
= ST_NOEOF
; 640 STps
->last_block_valid
= FALSE
; 647 if((STp
->buffer
)->last_result_fatal
!=0) { 648 if((STp
->device
)->scsi_level
>= SCSI_2
&& 649 (SCpnt
->sense_buffer
[0] &0x70) ==0x70&& 650 (SCpnt
->sense_buffer
[2] &0x0f) == NOT_READY
&& 651 SCpnt
->sense_buffer
[12] ==0x3a) {/* Check ASC */ 652 STp
->ready
= ST_NO_TAPE
; 654 STp
->ready
= ST_NOT_READY
; 655 scsi_release_command(SCpnt
); 657 STp
->density
=0;/* Clear the erroneous "residue" */ 660 STp
->ps
[0].drv_file
= STp
->ps
[0].drv_block
=0; 661 STp
->partition
= STp
->new_partition
=0; 662 STp
->door_locked
= ST_UNLOCKED
; 667 if(STp
->omit_blklims
) 668 STp
->min_block
= STp
->max_block
= (-1); 670 memset((void*) &cmd
[0],0,10); 671 cmd
[0] = READ_BLOCK_LIMITS
; 673 SCpnt
=st_do_scsi(SCpnt
, STp
, cmd
,6, STp
->timeout
, MAX_READY_RETRIES
); 675 if(!SCpnt
->result
&& !SCpnt
->sense_buffer
[0]) { 676 STp
->max_block
= ((STp
->buffer
)->b_data
[1] <<16) | 677 ((STp
->buffer
)->b_data
[2] <<8) | (STp
->buffer
)->b_data
[3]; 678 STp
->min_block
= ((STp
->buffer
)->b_data
[4] <<8) | 679 (STp
->buffer
)->b_data
[5]; 682 printk(ST_DEB_MSG
"st%d: Block limits %d - %d bytes.\n", dev
, STp
->min_block
, 687 STp
->min_block
= STp
->max_block
= (-1); 690 printk(ST_DEB_MSG
"st%d: Can't read block limits.\n", dev
); 695 memset((void*) &cmd
[0],0,10); 699 SCpnt
=st_do_scsi(SCpnt
, STp
, cmd
,12, STp
->timeout
, MAX_READY_RETRIES
); 701 if((STp
->buffer
)->last_result_fatal
!=0) { 704 printk(ST_DEB_MSG
"st%d: No Mode Sense.\n", dev
); 706 STp
->block_size
= ST_DEFAULT_BLOCK
;/* Educated guess (?) */ 707 (STp
->buffer
)->last_result_fatal
=0;/* Prevent error propagation */ 708 STp
->drv_write_prot
=0; 714 printk(ST_DEB_MSG
"st%d: Mode sense. Length %d, medium %x, WBS %x, BLL %d\n", 716 (STp
->buffer
)->b_data
[0], (STp
->buffer
)->b_data
[1], 717 (STp
->buffer
)->b_data
[2], (STp
->buffer
)->b_data
[3]); 720 if((STp
->buffer
)->b_data
[3] >=8) { 721 STp
->drv_buffer
= ((STp
->buffer
)->b_data
[2] >>4) &7; 722 STp
->density
= (STp
->buffer
)->b_data
[4]; 723 STp
->block_size
= (STp
->buffer
)->b_data
[9] *65536+ 724 (STp
->buffer
)->b_data
[10] *256+ (STp
->buffer
)->b_data
[11]; 727 printk(ST_DEB_MSG
"st%d: Density %x, tape length: %x, drv buffer: %d\n", 728 dev
, STp
->density
, (STp
->buffer
)->b_data
[5] *65536+ 729 (STp
->buffer
)->b_data
[6] *256+ (STp
->buffer
)->b_data
[7], 734 if(STp
->block_size
> (STp
->buffer
)->buffer_size
&& 735 !enlarge_buffer(STp
->buffer
, STp
->block_size
, STp
->restr_dma
)) { 736 printk(KERN_NOTICE
"st%d: Blocksize %d too large for buffer.\n", dev
, 738 (STp
->buffer
)->in_use
=0; 740 if(scsi_tapes
[dev
].device
->host
->hostt
->module
) 741 __MOD_DEC_USE_COUNT(scsi_tapes
[dev
].device
->host
->hostt
->module
); 742 if(st_template
.module
) 743 __MOD_DEC_USE_COUNT(st_template
.module
); 746 STp
->drv_write_prot
= ((STp
->buffer
)->b_data
[2] &0x80) !=0; 748 scsi_release_command(SCpnt
); 751 if(STp
->block_size
>0) 752 (STp
->buffer
)->buffer_blocks
= st_buffer_size
/ STp
->block_size
; 754 (STp
->buffer
)->buffer_blocks
=1; 755 (STp
->buffer
)->buffer_bytes
= (STp
->buffer
)->read_pointer
=0; 759 printk(ST_DEB_MSG
"st%d: Block size: %d, buffer size: %d (%d blocks).\n", dev
, 760 STp
->block_size
, (STp
->buffer
)->buffer_size
, 761 (STp
->buffer
)->buffer_blocks
); 764 if(STp
->drv_write_prot
) { 768 printk(ST_DEB_MSG
"st%d: Write protected\n", dev
); 770 if((flags
& O_ACCMODE
) == O_WRONLY
|| (flags
& O_ACCMODE
) == O_RDWR
) { 771 (STp
->buffer
)->in_use
=0; 773 if(scsi_tapes
[dev
].device
->host
->hostt
->module
) 774 __MOD_DEC_USE_COUNT(scsi_tapes
[dev
].device
->host
->hostt
->module
); 775 if(st_template
.module
) 776 __MOD_DEC_USE_COUNT(st_template
.module
); 781 if(STp
->can_partitions
&& STp
->nbr_partitions
<1) { 782 /* This code is reached when the device is opened for the first time 783 after the driver has been initialized with tape in the drive and the 784 partition support has been enabled. */ 787 printk(ST_DEB_MSG
"st%d: Updating partition number in status.\n", dev
); 789 if((STp
->partition
=find_partition(inode
)) <0) { 790 (STp
->buffer
)->in_use
=0; 792 if(scsi_tapes
[dev
].device
->host
->hostt
->module
) 793 __MOD_DEC_USE_COUNT(scsi_tapes
[dev
].device
->host
->hostt
->module
); 794 if(st_template
.module
) 795 __MOD_DEC_USE_COUNT(st_template
.module
); 796 return STp
->partition
; 798 STp
->new_partition
= STp
->partition
; 799 STp
->nbr_partitions
=1;/* This guess will be updated when necessary */ 802 if(new_session
) {/* Change the drive parameters for the new mode */ 803 STp
->density_changed
= STp
->blksize_changed
= FALSE
; 804 STp
->compression_changed
= FALSE
; 805 if(!(STm
->defaults_for_writes
) && 806 (i
=set_mode_densblk(inode
, STp
, STm
)) <0) { 807 (STp
->buffer
)->in_use
=0; 809 if(scsi_tapes
[dev
].device
->host
->hostt
->module
) 810 __MOD_DEC_USE_COUNT(scsi_tapes
[dev
].device
->host
->hostt
->module
); 811 if(st_template
.module
) 812 __MOD_DEC_USE_COUNT(st_template
.module
); 815 if(STp
->default_drvbuffer
!=0xff) { 816 if(st_int_ioctl(inode
, MTSETDRVBUFFER
, STp
->default_drvbuffer
)) 817 printk(KERN_WARNING
"st%d: Can't set default drive buffering to %d.\n", 818 dev
, STp
->default_drvbuffer
); 828 /* Close the device*/ 830 scsi_tape_close(struct inode
* inode
,struct file
* filp
) 832 int result
=0, result2
; 833 static unsigned char cmd
[10]; 839 kdev_t devt
= inode
->i_rdev
; 843 STp
= &(scsi_tapes
[dev
]); 844 STm
= &(STp
->modes
[STp
->current_mode
]); 845 STps
= &(STp
->ps
[STp
->partition
]); 847 if(STp
->can_partitions
&& 848 (result
=update_partition(inode
)) <0) { 851 printk(ST_DEB_MSG
"st%d: update_partition at close failed.\n", dev
); 856 if( STps
->rw
== ST_WRITING
&& !(STp
->device
)->was_reset
) { 858 result
=flush_write_buffer(STp
); 862 printk(ST_DEB_MSG
"st%d: File length %ld bytes.\n", 863 dev
, (long)(filp
->f_pos
)); 864 printk(ST_DEB_MSG
"st%d: Async write waits %d, finished %d.\n", 865 dev
, STp
->nbr_waits
, STp
->nbr_finished
); 869 if(result
==0|| result
== (-ENOSPC
)) { 872 cmd
[0] = WRITE_FILEMARKS
; 873 cmd
[4] =1+ STp
->two_fm
; 875 SCpnt
=st_do_scsi(NULL
, STp
, cmd
,0, STp
->timeout
, MAX_WRITE_RETRIES
); 879 if((STp
->buffer
)->last_result_fatal
!=0&& 880 ((SCpnt
->sense_buffer
[0] &0x70) !=0x70|| 881 (SCpnt
->sense_buffer
[2] &0x4f) !=0x40|| 882 ((SCpnt
->sense_buffer
[0] &0x80) !=0&& 883 (SCpnt
->sense_buffer
[3] | SCpnt
->sense_buffer
[4] | 884 SCpnt
->sense_buffer
[5] | 885 SCpnt
->sense_buffer
[6]) ==0))) { 886 /* Filter out successful write at EOM */ 887 scsi_release_command(SCpnt
); 889 printk(KERN_ERR
"st%d: Error on write filemark.\n", dev
); 894 scsi_release_command(SCpnt
); 896 if(STps
->drv_file
>=0) 900 cross_eof(STp
, FALSE
); 907 printk(ST_DEB_MSG
"st%d: Buffer flushed, %d EOF(s) written\n", 911 else if(!STp
->rew_at_close
) { 912 STps
= &(STp
->ps
[STp
->partition
]); 913 if(!STm
->sysv
|| STps
->rw
!= ST_READING
) { 915 result
=flush_buffer(inode
, filp
,0); 916 else if(STps
->eof
== ST_FM_HIT
) { 917 result
=cross_eof(STp
, FALSE
); 919 if(STps
->drv_file
>=0) 925 STps
->eof
= ST_NOEOF
; 928 else if((STps
->eof
== ST_NOEOF
&& 929 !(result
=cross_eof(STp
, TRUE
))) || 930 STps
->eof
== ST_FM_HIT
) { 931 if(STps
->drv_file
>=0) 939 if(STp
->rew_at_close
) { 940 result2
=st_int_ioctl(inode
, MTREW
,1); 945 if(STp
->door_locked
== ST_LOCKED_AUTO
) 946 st_int_ioctl(inode
, MTUNLOCK
,0); 948 if(STp
->buffer
!= NULL
) { 949 normalize_buffer(STp
->buffer
); 950 (STp
->buffer
)->in_use
=0; 954 if(scsi_tapes
[dev
].device
->host
->hostt
->module
) 955 __MOD_DEC_USE_COUNT(scsi_tapes
[dev
].device
->host
->hostt
->module
); 956 if(st_template
.module
) 957 __MOD_DEC_USE_COUNT(st_template
.module
); 965 st_write(struct file
* filp
,const char* buf
,size_t count
, loff_t
*ppos
) 967 struct inode
*inode
= filp
->f_dentry
->d_inode
; 969 ssize_t i
, do_count
, blks
, retval
, transfer
; 972 static unsigned char cmd
[10]; 974 Scsi_Cmnd
* SCpnt
= NULL
; 978 int dev
=TAPE_NR(inode
->i_rdev
); 980 STp
= &(scsi_tapes
[dev
]); 983 * If we are in the middle of error recovery, don't let anyone 984 * else try and use this device. Also, if error recovery fails, it 985 * may try and take the device offline, in which case all further 986 * access to the device is prohibited. 988 if( !scsi_block_when_processing_errors(STp
->device
) ) 993 if(ppos
!= &filp
->f_pos
) { 994 /* "A request was outside the capabilities of the device." */ 998 if(STp
->ready
!= ST_READY
) { 999 if(STp
->ready
== ST_NO_TAPE
) 1004 STm
= &(STp
->modes
[STp
->current_mode
]); 1011 * If there was a bus reset, block further access 1014 if( STp
->device
->was_reset
) 1019 printk(ST_DEB_MSG
"st%d: Incorrect device.\n", dev
); 1024 if(STp
->can_partitions
&& 1025 (retval
=update_partition(inode
)) <0) 1027 STps
= &(STp
->ps
[STp
->partition
]); 1032 if(STp
->block_size
==0&& 1033 count
> (STp
->buffer
)->buffer_size
&& 1034 !enlarge_buffer(STp
->buffer
, count
, STp
->restr_dma
)) 1037 if(STp
->do_auto_lock
&& STp
->door_locked
== ST_UNLOCKED
&& 1038 !st_int_ioctl(inode
, MTLOCK
,0)) 1039 STp
->door_locked
= ST_LOCKED_AUTO
; 1041 if(STps
->rw
== ST_READING
) { 1042 retval
=flush_buffer(inode
, filp
,0); 1045 STps
->rw
= ST_WRITING
; 1047 else if(STps
->rw
!= ST_WRITING
&& 1048 STps
->drv_file
==0&& STps
->drv_block
==0) { 1049 if((retval
=set_mode_densblk(inode
, STp
, STm
)) <0) 1051 if(STm
->default_compression
!= ST_DONT_TOUCH
&& 1052 !(STp
->compression_changed
)) { 1053 if(st_compression(STp
, (STm
->default_compression
== ST_YES
))) { 1054 printk(KERN_WARNING
"st%d: Can't set default compression.\n", 1062 if((STp
->buffer
)->writing
) { 1063 write_behind_check(STp
); 1064 if((STp
->buffer
)->last_result_fatal
) { 1067 printk(ST_DEB_MSG
"st%d: Async write error (write) %x.\n", dev
, 1068 (STp
->buffer
)->last_result
); 1070 if((STp
->buffer
)->last_result
== INT_MAX
) 1071 STps
->eof
= ST_EOM_OK
; 1073 STps
->eof
= ST_EOM_ERROR
; 1076 if(STps
->eof
== ST_EOM_OK
) 1078 else if(STps
->eof
== ST_EOM_ERROR
) 1081 /* Check the buffer readability in cases where copy_user might catch 1082 the problems after some tape movement. */ 1083 if(STp
->block_size
!=0&& 1084 (copy_from_user(&i
, buf
,1) !=0|| 1085 copy_from_user(&i
, buf
+ count
-1,1) !=0)) 1088 if(!STm
->do_buffer_writes
) { 1089 if(STp
->block_size
!=0&& (count
% STp
->block_size
) !=0) 1090 return(-EIO
);/* Write must be integral number of blocks */ 1094 write_threshold
= (STp
->buffer
)->buffer_blocks
* STp
->block_size
; 1095 if(!STm
->do_async_writes
) 1102 cmd
[1] = (STp
->block_size
!=0); 1104 STps
->rw
= ST_WRITING
; 1107 while((STp
->block_size
==0&& !STm
->do_async_writes
&& count
>0) || 1108 (STp
->block_size
!=0&& 1109 (STp
->buffer
)->buffer_bytes
+ count
> write_threshold
)) 1112 if(STp
->block_size
==0) 1115 do_count
= (STp
->buffer
)->buffer_blocks
* STp
->block_size
- 1116 (STp
->buffer
)->buffer_bytes
; 1117 if(do_count
> count
) 1121 i
=copy_from_user((STp
->buffer
)->b_data
+ 1122 (STp
->buffer
)->buffer_bytes
, b_point
, do_count
); 1126 scsi_release_command(SCpnt
); 1132 if(STp
->block_size
==0) 1133 blks
= transfer
= do_count
; 1135 blks
= ((STp
->buffer
)->buffer_bytes
+ do_count
) / 1137 transfer
= blks
* STp
->block_size
; 1143 SCpnt
=st_do_scsi(SCpnt
, STp
, cmd
, transfer
, STp
->timeout
, MAX_WRITE_RETRIES
); 1147 if((STp
->buffer
)->last_result_fatal
!=0) { 1150 printk(ST_DEB_MSG
"st%d: Error on write:\n", dev
); 1152 if((SCpnt
->sense_buffer
[0] &0x70) ==0x70&& 1153 (SCpnt
->sense_buffer
[2] &0x40)) { 1154 if(STp
->block_size
!=0&& (SCpnt
->sense_buffer
[0] &0x80) !=0) 1155 transfer
= (SCpnt
->sense_buffer
[3] <<24) | 1156 (SCpnt
->sense_buffer
[4] <<16) | 1157 (SCpnt
->sense_buffer
[5] <<8) | SCpnt
->sense_buffer
[6]; 1158 else if(STp
->block_size
==0&& 1159 (SCpnt
->sense_buffer
[2] &0x0f) == VOLUME_OVERFLOW
) 1160 transfer
= do_count
; 1163 if(STp
->block_size
!=0) 1164 transfer
*= STp
->block_size
; 1165 if(transfer
<= do_count
) { 1166 filp
->f_pos
+= do_count
- transfer
; 1167 count
-= do_count
- transfer
; 1168 if(STps
->drv_block
>=0) { 1169 if(STp
->block_size
==0&& transfer
< do_count
) 1171 else if(STp
->block_size
!=0) 1172 STps
->drv_block
+= (do_count
- transfer
) / STp
->block_size
; 1174 STps
->eof
= ST_EOM_OK
; 1175 retval
= (-ENOSPC
);/* EOM within current request */ 1178 printk(ST_DEB_MSG
"st%d: EOM with %d bytes unwritten.\n", 1183 STps
->eof
= ST_EOM_ERROR
; 1184 STps
->drv_block
= (-1);/* Too cautious? */ 1185 retval
= (-EIO
);/* EOM for old data */ 1188 printk(ST_DEB_MSG
"st%d: EOM with lost data.\n", dev
); 1193 STps
->drv_block
= (-1);/* Too cautious? */ 1197 scsi_release_command(SCpnt
); 1199 (STp
->buffer
)->buffer_bytes
=0; 1202 return total
- count
; 1206 filp
->f_pos
+= do_count
; 1207 b_point
+= do_count
; 1209 if(STps
->drv_block
>=0) { 1210 if(STp
->block_size
==0) 1213 STps
->drv_block
+= blks
; 1215 (STp
->buffer
)->buffer_bytes
=0; 1220 i
=copy_from_user((STp
->buffer
)->b_data
+ 1221 (STp
->buffer
)->buffer_bytes
, b_point
, count
); 1225 scsi_release_command(SCpnt
); 1230 filp
->f_pos
+= count
; 1231 (STp
->buffer
)->buffer_bytes
+= count
; 1235 if(doing_write
&& (STp
->buffer
)->last_result_fatal
!=0) { 1236 scsi_release_command(SCpnt
); 1238 return(STp
->buffer
)->last_result_fatal
; 1241 if(STm
->do_async_writes
&& 1242 (((STp
->buffer
)->buffer_bytes
>= STp
->write_threshold
&& 1243 (STp
->buffer
)->buffer_bytes
>= STp
->block_size
) || 1244 STp
->block_size
==0) ) { 1245 /* Schedule an asynchronous write */ 1247 SCpnt
=scsi_allocate_device(NULL
, STp
->device
,1); 1251 if(STp
->block_size
==0) 1252 (STp
->buffer
)->writing
= (STp
->buffer
)->buffer_bytes
; 1254 (STp
->buffer
)->writing
= ((STp
->buffer
)->buffer_bytes
/ 1255 STp
->block_size
) * STp
->block_size
; 1256 STp
->dirty
= !((STp
->buffer
)->writing
== 1257 (STp
->buffer
)->buffer_bytes
); 1259 if(STp
->block_size
==0) 1260 blks
= (STp
->buffer
)->writing
; 1262 blks
= (STp
->buffer
)->writing
/ STp
->block_size
; 1266 STp
->sem
= MUTEX_LOCKED
; 1267 SCpnt
->request
.sem
= &(STp
->sem
); 1268 SCpnt
->request
.rq_status
= RQ_SCSI_BUSY
; 1269 SCpnt
->request
.rq_dev
= STp
->devt
; 1271 STp
->write_pending
=1; 1275 (void*) cmd
, (STp
->buffer
)->b_data
, 1276 (STp
->buffer
)->writing
, 1277 st_sleep_done
, STp
->timeout
, MAX_WRITE_RETRIES
); 1279 else if(SCpnt
!= NULL
) 1281 scsi_release_command(SCpnt
); 1284 STps
->at_sm
&= (total
==0); 1286 STps
->eof
= ST_NOEOF
; 1291 /* Read data from the tape. Returns zero in the normal case, one if the 1292 eof status has changed, and the negative error code in case of a 1293 fatal error. Otherwise updates the buffer and the eof state. */ 1295 read_tape(struct inode
*inode
,long count
, Scsi_Cmnd
**aSCpnt
) 1297 int transfer
, blks
, bytes
; 1298 static unsigned char cmd
[10]; 1303 int dev
=TAPE_NR(inode
->i_rdev
); 1309 STp
= &(scsi_tapes
[dev
]); 1310 STm
= &(STp
->modes
[STp
->current_mode
]); 1311 STps
= &(STp
->ps
[STp
->partition
]); 1312 if(STps
->eof
== ST_FM_HIT
) 1317 cmd
[1] = (STp
->block_size
!=0); 1318 if(STp
->block_size
==0) 1319 blks
= bytes
= count
; 1321 if(STm
->do_read_ahead
) { 1322 blks
= (STp
->buffer
)->buffer_blocks
; 1323 bytes
= blks
* STp
->block_size
; 1327 if(bytes
> (STp
->buffer
)->buffer_size
) 1328 bytes
= (STp
->buffer
)->buffer_size
; 1329 blks
= bytes
/ STp
->block_size
; 1330 bytes
= blks
* STp
->block_size
; 1338 SCpnt
=st_do_scsi(SCpnt
, STp
, cmd
, bytes
, STp
->timeout
, MAX_RETRIES
); 1343 (STp
->buffer
)->read_pointer
=0; 1347 /* Something to check */ 1348 if((STp
->buffer
)->last_result_fatal
) { 1352 printk(ST_DEB_MSG
"st%d: Sense: %2x %2x %2x %2x %2x %2x %2x %2x\n", 1354 SCpnt
->sense_buffer
[0], SCpnt
->sense_buffer
[1], 1355 SCpnt
->sense_buffer
[2], SCpnt
->sense_buffer
[3], 1356 SCpnt
->sense_buffer
[4], SCpnt
->sense_buffer
[5], 1357 SCpnt
->sense_buffer
[6], SCpnt
->sense_buffer
[7]); 1359 if((SCpnt
->sense_buffer
[0] &0x70) ==0x70) {/* extended sense */ 1361 if((SCpnt
->sense_buffer
[2] &0x0f) == BLANK_CHECK
) 1362 SCpnt
->sense_buffer
[2] &=0xcf;/* No need for EOM in this case */ 1364 if((SCpnt
->sense_buffer
[2] &0xe0) !=0) {/* EOF, EOM, or ILI */ 1365 /* Compute the residual count */ 1366 if((SCpnt
->sense_buffer
[0] &0x80) !=0) 1367 transfer
= (SCpnt
->sense_buffer
[3] <<24) | 1368 (SCpnt
->sense_buffer
[4] <<16) | 1369 (SCpnt
->sense_buffer
[5] <<8) | SCpnt
->sense_buffer
[6]; 1372 if(STp
->block_size
==0&& 1373 (SCpnt
->sense_buffer
[2] &0x0f) == MEDIUM_ERROR
) 1376 if(SCpnt
->sense_buffer
[2] &0x20) {/* ILI */ 1377 if(STp
->block_size
==0) { 1380 (STp
->buffer
)->buffer_bytes
= bytes
- transfer
; 1383 scsi_release_command(SCpnt
); 1384 SCpnt
= *aSCpnt
= NULL
; 1385 if(transfer
== blks
) {/* We did not get anything, error */ 1386 printk(KERN_NOTICE
"st%d: Incorrect block size.\n", dev
); 1387 if(STps
->drv_block
>=0) 1388 STps
->drv_block
+= blks
- transfer
+1; 1389 st_int_ioctl(inode
, MTBSR
,1); 1392 /* We have some data, deliver it */ 1393 (STp
->buffer
)->buffer_bytes
= (blks
- transfer
) * 1398 "st%d: ILI but enough data received %ld %d.\n", 1399 dev
, count
, (STp
->buffer
)->buffer_bytes
); 1401 if(STps
->drv_block
>=0) 1402 STps
->drv_block
+=1; 1403 if(st_int_ioctl(inode
, MTBSR
,1)) 1407 else if(SCpnt
->sense_buffer
[2] &0x80) {/* FM overrides EOM */ 1408 if(STps
->eof
!= ST_FM_HIT
) 1409 STps
->eof
= ST_FM_HIT
; 1411 STps
->eof
= ST_EOD_2
; 1412 if(STp
->block_size
==0) 1413 (STp
->buffer
)->buffer_bytes
=0; 1415 (STp
->buffer
)->buffer_bytes
= 1416 bytes
- transfer
* STp
->block_size
; 1420 "st%d: EOF detected (%d bytes read).\n", 1421 dev
, (STp
->buffer
)->buffer_bytes
); 1424 else if(SCpnt
->sense_buffer
[2] &0x40) { 1425 if(STps
->eof
== ST_FM
) 1426 STps
->eof
= ST_EOD_1
; 1428 STps
->eof
= ST_EOM_OK
; 1429 if(STp
->block_size
==0) 1430 (STp
->buffer
)->buffer_bytes
= bytes
- transfer
; 1432 (STp
->buffer
)->buffer_bytes
= 1433 bytes
- transfer
* STp
->block_size
; 1436 printk(ST_DEB_MSG
"st%d: EOM detected (%d bytes read).\n", 1437 dev
, (STp
->buffer
)->buffer_bytes
); 1440 }/* end of EOF, EOM, ILI test */ 1441 else{/* nonzero sense key */ 1444 printk(ST_DEB_MSG
"st%d: Tape error while reading.\n", dev
); 1446 STps
->drv_block
= (-1); 1447 if(STps
->eof
== ST_FM
&& 1448 (SCpnt
->sense_buffer
[2] &0x0f) == BLANK_CHECK
) { 1452 "st%d: Zero returned for first BLANK CHECK after EOF.\n", 1455 STps
->eof
= ST_EOD_2
;/* First BLANK_CHECK after FM */ 1457 else/* Some other extended sense code */ 1460 }/* End of extended sense test */ 1461 else{/* Non-extended sense */ 1462 retval
= (STp
->buffer
)->last_result_fatal
; 1465 }/* End of error handling */ 1466 else/* Read successful */ 1467 (STp
->buffer
)->buffer_bytes
= bytes
; 1469 if(STps
->drv_block
>=0) { 1470 if(STp
->block_size
==0) 1473 STps
->drv_block
+= (STp
->buffer
)->buffer_bytes
/ STp
->block_size
; 1482 st_read(struct file
* filp
,char* buf
,size_t count
, loff_t
*ppos
) 1484 struct inode
* inode
= filp
->f_dentry
->d_inode
; 1486 ssize_t i
, transfer
; 1488 Scsi_Cmnd
* SCpnt
= NULL
; 1492 int dev
=TAPE_NR(inode
->i_rdev
); 1494 STp
= &(scsi_tapes
[dev
]); 1497 * If we are in the middle of error recovery, don't let anyone 1498 * else try and use this device. Also, if error recovery fails, it 1499 * may try and take the device offline, in which case all further 1500 * access to the device is prohibited. 1502 if( !scsi_block_when_processing_errors(STp
->device
) ) 1507 if(ppos
!= &filp
->f_pos
) { 1508 /* "A request was outside the capabilities of the device." */ 1512 if(STp
->ready
!= ST_READY
) { 1513 if(STp
->ready
== ST_NO_TAPE
) 1518 STm
= &(STp
->modes
[STp
->current_mode
]); 1523 printk(ST_DEB_MSG
"st%d: Incorrect device.\n", dev
); 1528 if(STp
->can_partitions
&& 1529 (total
=update_partition(inode
)) <0) 1532 if(STp
->block_size
==0&& 1533 count
> (STp
->buffer
)->buffer_size
&& 1534 !enlarge_buffer(STp
->buffer
, count
, STp
->restr_dma
)) 1537 if(!(STm
->do_read_ahead
) && STp
->block_size
!=0&& 1538 (count
% STp
->block_size
) !=0) 1539 return(-EIO
);/* Read must be integral number of blocks */ 1541 if(STp
->do_auto_lock
&& STp
->door_locked
== ST_UNLOCKED
&& 1542 !st_int_ioctl(inode
, MTLOCK
,0)) 1543 STp
->door_locked
= ST_LOCKED_AUTO
; 1545 STps
= &(STp
->ps
[STp
->partition
]); 1546 if(STps
->rw
== ST_WRITING
) { 1547 transfer
=flush_buffer(inode
, filp
,0); 1550 STps
->rw
= ST_READING
; 1554 if(debugging
&& STps
->eof
!= ST_NOEOF
) 1555 printk(ST_DEB_MSG
"st%d: EOF/EOM flag up (%d). Bytes %d\n", dev
, 1556 STps
->eof
, (STp
->buffer
)->buffer_bytes
); 1558 if((STp
->buffer
)->buffer_bytes
==0&& 1559 STps
->eof
>= ST_EOD_1
) { 1560 if(STps
->eof
< ST_EOD
) { 1564 return(-EIO
);/* EOM or Blank Check */ 1567 /* Check the buffer writability before any tape movement. Don't alter 1569 if(copy_from_user(&i
, buf
,1) !=0|| 1570 copy_to_user(buf
, &i
,1) !=0|| 1571 copy_from_user(&i
, buf
+ count
-1,1) !=0|| 1572 copy_to_user(buf
+ count
-1, &i
,1) !=0) 1575 STps
->rw
= ST_READING
; 1578 /* Loop until enough data in buffer or a special condition found */ 1579 for(total
=0, special
=0; total
< count
&& !special
; ) { 1581 /* Get new data if the buffer is empty */ 1582 if((STp
->buffer
)->buffer_bytes
==0) { 1583 special
=read_tape(inode
, count
- total
, &SCpnt
); 1584 if(special
<0) {/* No need to continue read */ 1587 scsi_release_command(SCpnt
); 1593 /* Move the data from driver buffer to user buffer */ 1594 if((STp
->buffer
)->buffer_bytes
>0) { 1596 if(debugging
&& STps
->eof
!= ST_NOEOF
) 1597 printk(ST_DEB_MSG
"st%d: EOF up (%d). Left %d, needed %d.\n", dev
, 1598 STps
->eof
, (STp
->buffer
)->buffer_bytes
, count
- total
); 1600 transfer
= (STp
->buffer
)->buffer_bytes
< count
- total
? 1601 (STp
->buffer
)->buffer_bytes
: count
- total
; 1602 i
=copy_to_user(buf
, (STp
->buffer
)->b_data
+ 1603 (STp
->buffer
)->read_pointer
, transfer
); 1607 scsi_release_command(SCpnt
); 1612 filp
->f_pos
+= transfer
; 1615 (STp
->buffer
)->buffer_bytes
-= transfer
; 1616 (STp
->buffer
)->read_pointer
+= transfer
; 1619 if(STp
->block_size
==0) 1620 break;/* Read only one variable length block */ 1622 }/* for (total = 0, special = 0; total < count && !special; ) */ 1626 scsi_release_command(SCpnt
); 1630 /* Change the eof state if no data from tape or buffer */ 1632 if(STps
->eof
== ST_FM_HIT
) { 1635 if(STps
->drv_file
>=0) 1638 else if(STps
->eof
== ST_EOD_1
) { 1639 STps
->eof
= ST_EOD_2
; 1641 if(STps
->drv_file
>=0) 1644 else if(STps
->eof
== ST_EOD_2
) 1647 else if(STps
->eof
== ST_FM
) 1648 STps
->eof
= ST_NOEOF
; 1655 /* Set the driver options */ 1657 st_log_options(Scsi_Tape
*STp
, ST_mode
*STm
,int dev
) 1660 "st%d: Mode %d options: buffer writes: %d, async writes: %d, read ahead: %d\n", 1661 dev
, STp
->current_mode
, STm
->do_buffer_writes
, STm
->do_async_writes
, 1662 STm
->do_read_ahead
); 1664 "st%d: can bsr: %d, two FMs: %d, fast mteom: %d, auto lock: %d,\n", 1665 dev
, STp
->can_bsr
, STp
->two_fm
, STp
->fast_mteom
, STp
->do_auto_lock
); 1667 "st%d: defs for wr: %d, no block limits: %d, partitions: %d, s2 log: %d\n", 1668 dev
, STm
->defaults_for_writes
, STp
->omit_blklims
, STp
->can_partitions
, 1669 STp
->scsi2_logical
); 1671 "st%d: sysv: %d\n", dev
, STm
->sysv
); 1674 "st%d: debugging: %d\n", 1681 st_set_options(struct inode
* inode
,long options
) 1687 int dev
=TAPE_NR(inode
->i_rdev
); 1689 STp
= &(scsi_tapes
[dev
]); 1690 STm
= &(STp
->modes
[STp
->current_mode
]); 1692 memcpy(STm
, &(STp
->modes
[0]),sizeof(ST_mode
)); 1693 modes_defined
= TRUE
; 1696 printk(ST_DEB_MSG
"st%d: Initialized mode %d definition from mode 0\n", 1697 dev
, STp
->current_mode
); 1701 code
= options
& MT_ST_OPTIONS
; 1702 if(code
== MT_ST_BOOLEANS
) { 1703 STm
->do_buffer_writes
= (options
& MT_ST_BUFFER_WRITES
) !=0; 1704 STm
->do_async_writes
= (options
& MT_ST_ASYNC_WRITES
) !=0; 1705 STm
->defaults_for_writes
= (options
& MT_ST_DEF_WRITES
) !=0; 1706 STm
->do_read_ahead
= (options
& MT_ST_READ_AHEAD
) !=0; 1707 STp
->two_fm
= (options
& MT_ST_TWO_FM
) !=0; 1708 STp
->fast_mteom
= (options
& MT_ST_FAST_MTEOM
) !=0; 1709 STp
->do_auto_lock
= (options
& MT_ST_AUTO_LOCK
) !=0; 1710 STp
->can_bsr
= (options
& MT_ST_CAN_BSR
) !=0; 1711 STp
->omit_blklims
= (options
& MT_ST_NO_BLKLIMS
) !=0; 1712 if((STp
->device
)->scsi_level
>= SCSI_2
) 1713 STp
->can_partitions
= (options
& MT_ST_CAN_PARTITIONS
) !=0; 1714 STp
->scsi2_logical
= (options
& MT_ST_SCSI2LOGICAL
) !=0; 1715 STm
->sysv
= (options
& MT_ST_SYSV
) !=0; 1717 debugging
= (options
& MT_ST_DEBUGGING
) !=0; 1719 st_log_options(STp
, STm
, dev
); 1721 else if(code
== MT_ST_SETBOOLEANS
|| code
== MT_ST_CLEARBOOLEANS
) { 1722 value
= (code
== MT_ST_SETBOOLEANS
); 1723 if((options
& MT_ST_BUFFER_WRITES
) !=0) 1724 STm
->do_buffer_writes
= value
; 1725 if((options
& MT_ST_ASYNC_WRITES
) !=0) 1726 STm
->do_async_writes
= value
; 1727 if((options
& MT_ST_DEF_WRITES
) !=0) 1728 STm
->defaults_for_writes
= value
; 1729 if((options
& MT_ST_READ_AHEAD
) !=0) 1730 STm
->do_read_ahead
= value
; 1731 if((options
& MT_ST_TWO_FM
) !=0) 1732 STp
->two_fm
= value
; 1733 if((options
& MT_ST_FAST_MTEOM
) !=0) 1734 STp
->fast_mteom
= value
; 1735 if((options
& MT_ST_AUTO_LOCK
) !=0) 1736 STp
->do_auto_lock
= value
; 1737 if((options
& MT_ST_CAN_BSR
) !=0) 1738 STp
->can_bsr
= value
; 1739 if((options
& MT_ST_NO_BLKLIMS
) !=0) 1740 STp
->omit_blklims
= value
; 1741 if((STp
->device
)->scsi_level
>= SCSI_2
&& 1742 (options
& MT_ST_CAN_PARTITIONS
) !=0) 1743 STp
->can_partitions
= value
; 1744 if((options
& MT_ST_SCSI2LOGICAL
) !=0) 1745 STp
->scsi2_logical
= value
; 1746 if((options
& MT_ST_SYSV
) !=0) 1749 if((options
& MT_ST_DEBUGGING
) !=0) 1752 st_log_options(STp
, STm
, dev
); 1754 else if(code
== MT_ST_WRITE_THRESHOLD
) { 1755 value
= (options
& ~MT_ST_OPTIONS
) * ST_KILOBYTE
; 1756 if(value
<1|| value
> st_buffer_size
) { 1757 printk(KERN_WARNING
"st%d: Write threshold %d too small or too large.\n", 1761 STp
->write_threshold
= value
; 1762 printk(KERN_INFO
"st%d: Write threshold set to %d bytes.\n", 1765 else if(code
== MT_ST_DEF_BLKSIZE
) { 1766 value
= (options
& ~MT_ST_OPTIONS
); 1767 if(value
== ~MT_ST_OPTIONS
) { 1768 STm
->default_blksize
= (-1); 1769 printk(KERN_INFO
"st%d: Default block size disabled.\n", dev
); 1772 STm
->default_blksize
= value
; 1773 printk(KERN_INFO
"st%d: Default block size set to %d bytes.\n", 1774 dev
, STm
->default_blksize
); 1777 else if(code
== MT_ST_TIMEOUTS
) { 1778 value
= (options
& ~MT_ST_OPTIONS
); 1779 if((value
& MT_ST_SET_LONG_TIMEOUT
) !=0) { 1780 STp
->long_timeout
= (value
& ~MT_ST_SET_LONG_TIMEOUT
) * HZ
; 1781 printk(KERN_INFO
"st%d: Long timeout set to %d seconds.\n", dev
, 1782 (value
& ~MT_ST_SET_LONG_TIMEOUT
)); 1785 STp
->timeout
= value
* HZ
; 1786 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
); 1799 STm
->default_density
= value
&0xff; 1800 printk(KERN_INFO
"st%d: Density default set to %x\n", 1801 dev
, STm
->default_density
); 1804 else if(code
== MT_ST_DEF_DRVBUFFER
) { 1805 if(value
== MT_ST_CLEAR_DEFAULT
) { 1806 STp
->default_drvbuffer
=0xff; 1807 printk(KERN_INFO
"st%d: Drive buffer default disabled.\n", dev
); 1810 STp
->default_drvbuffer
= value
&7; 1811 printk(KERN_INFO
"st%d: Drive buffer default set to %x\n", 1812 dev
, STp
->default_drvbuffer
); 1815 else if(code
== MT_ST_DEF_COMPRESSION
) { 1816 if(value
== MT_ST_CLEAR_DEFAULT
) { 1817 STm
->default_compression
= ST_DONT_TOUCH
; 1818 printk(KERN_INFO
"st%d: Compression default disabled.\n", dev
); 1821 STm
->default_compression
= (value
&1? ST_YES
: ST_NO
); 1822 printk(KERN_INFO
"st%d: Compression default set to %x\n", 1834 #define COMPRESSION_PAGE 0x0f 1835 #define COMPRESSION_PAGE_LENGTH 16 1837 #define MODE_HEADER_LENGTH 4 1839 #define DCE_MASK 0x80 1840 #define DCC_MASK 0x40 1841 #define RED_MASK 0x60 1844 /* Control the compression with mode page 15. Algorithm not changed if zero. */ 1846 st_compression(Scsi_Tape
* STp
,int state
) 1849 unsigned char cmd
[10]; 1850 Scsi_Cmnd
* SCpnt
= NULL
; 1852 if(STp
->ready
!= ST_READY
) 1855 /* Read the current page contents */ 1857 cmd
[0] = MODE_SENSE
; 1859 cmd
[2] = COMPRESSION_PAGE
; 1860 cmd
[4] = COMPRESSION_PAGE_LENGTH
+ MODE_HEADER_LENGTH
; 1862 SCpnt
=st_do_scsi(SCpnt
, STp
, cmd
, cmd
[4], STp
->timeout
,0); 1865 dev
=TAPE_NR(SCpnt
->request
.rq_dev
); 1867 if((STp
->buffer
)->last_result_fatal
!=0) { 1870 printk(ST_DEB_MSG
"st%d: Compression mode page not supported.\n", dev
); 1872 scsi_release_command(SCpnt
); 1878 printk(ST_DEB_MSG
"st%d: Compression state is %d.\n", dev
, 1879 ((STp
->buffer
)->b_data
[MODE_HEADER_LENGTH
+2] & DCE_MASK
?1:0)); 1882 /* Check if compression can be changed */ 1883 if(((STp
->buffer
)->b_data
[MODE_HEADER_LENGTH
+2] & DCC_MASK
) ==0) { 1886 printk(ST_DEB_MSG
"st%d: Compression not supported.\n", dev
); 1888 scsi_release_command(SCpnt
); 1895 (STp
->buffer
)->b_data
[MODE_HEADER_LENGTH
+2] |= DCE_MASK
; 1897 (STp
->buffer
)->b_data
[MODE_HEADER_LENGTH
+2] &= ~DCE_MASK
; 1900 cmd
[0] = MODE_SELECT
; 1902 cmd
[4] = COMPRESSION_PAGE_LENGTH
+ MODE_HEADER_LENGTH
; 1904 (STp
->buffer
)->b_data
[0] =0;/* Reserved data length */ 1905 (STp
->buffer
)->b_data
[1] =0;/* Reserved media type byte */ 1906 (STp
->buffer
)->b_data
[MODE_HEADER_LENGTH
] &=0x3f; 1907 SCpnt
=st_do_scsi(SCpnt
, STp
, cmd
, cmd
[4], STp
->timeout
,0); 1909 if((STp
->buffer
)->last_result_fatal
!=0) { 1912 printk(ST_DEB_MSG
"st%d: Compression change failed.\n", dev
); 1914 scsi_release_command(SCpnt
); 1921 printk(ST_DEB_MSG
"st%d: Compression state changed to %d.\n", 1925 scsi_release_command(SCpnt
); 1927 STp
->compression_changed
= TRUE
; 1932 /* Internal ioctl function */ 1934 st_int_ioctl(struct inode
* inode
, 1935 unsigned int cmd_in
,unsigned long arg
) 1939 int i
, ioctl_result
; 1941 unsigned char cmd
[10]; 1945 int fileno
, blkno
, at_sm
, undone
, datalen
; 1946 int dev
=TAPE_NR(inode
->i_rdev
); 1948 STp
= &(scsi_tapes
[dev
]); 1949 if(STp
->ready
!= ST_READY
&& cmd_in
!= MTLOAD
) { 1950 if(STp
->ready
== ST_NO_TAPE
) 1955 timeout
= STp
->long_timeout
; 1956 STps
= &(STp
->ps
[STp
->partition
]); 1957 fileno
= STps
->drv_file
; 1958 blkno
= STps
->drv_block
; 1959 at_sm
= STps
->at_sm
; 1965 chg_eof
= FALSE
;/* Changed from the FSF after this */ 1968 cmd
[1] =0x01;/* Space FileMarks */ 1969 cmd
[2] = (arg
>>16); 1974 printk(ST_DEB_MSG
"st%d: Spacing tape forward over %d filemarks.\n", 1975 dev
, cmd
[2] *65536+ cmd
[3] *256+ cmd
[4]); 1983 chg_eof
= FALSE
;/* Changed from the FSF after this */ 1986 cmd
[1] =0x01;/* Space FileMarks */ 1988 cmd
[2] = (ltmp
>>16); 1989 cmd
[3] = (ltmp
>>8); 1995 ltmp
= ltmp
| (cmd
[2] <<16) | (cmd
[3] <<8) | cmd
[4]; 1996 printk(ST_DEB_MSG
"st%d: Spacing tape backward over %ld filemarks.\n", 2002 blkno
= (-1);/* We can't know the block number */ 2007 cmd
[1] =0x00;/* Space Blocks */ 2008 cmd
[2] = (arg
>>16); 2013 printk(ST_DEB_MSG
"st%d: Spacing tape forward %d blocks.\n", dev
, 2014 cmd
[2] *65536+ cmd
[3] *256+ cmd
[4]); 2022 cmd
[1] =0x00;/* Space Blocks */ 2024 cmd
[2] = (ltmp
>>16); 2025 cmd
[3] = (ltmp
>>8); 2031 ltmp
= ltmp
| (cmd
[2] <<16) | (cmd
[3] <<8) | cmd
[4]; 2032 printk(ST_DEB_MSG
"st%d: Spacing tape backward %ld blocks.\n", dev
, (-ltmp
)); 2041 cmd
[1] =0x04;/* Space Setmarks */ 2042 cmd
[2] = (arg
>>16); 2047 printk(ST_DEB_MSG
"st%d: Spacing tape forward %d setmarks.\n", dev
, 2048 cmd
[2] *65536+ cmd
[3] *256+ cmd
[4]); 2051 blkno
= fileno
= (-1); 2057 cmd
[1] =0x04;/* Space Setmarks */ 2059 cmd
[2] = (ltmp
>>16); 2060 cmd
[3] = (ltmp
>>8); 2066 ltmp
= ltmp
| (cmd
[2] <<16) | (cmd
[3] <<8) | cmd
[4]; 2067 printk(ST_DEB_MSG
"st%d: Spacing tape backward %ld setmarks.\n", 2072 blkno
= fileno
= (-1); 2080 cmd
[0] = WRITE_FILEMARKS
; 2083 cmd
[2] = (arg
>>16); 2086 timeout
= STp
->timeout
; 2089 if(cmd_in
== MTWEOF
) 2090 printk(ST_DEB_MSG
"st%d: Writing %d filemarks.\n", dev
, 2091 cmd
[2] *65536+ cmd
[3] *256+ cmd
[4]); 2093 printk(ST_DEB_MSG
"st%d: Writing %d setmarks.\n", dev
, 2094 cmd
[2] *65536+ cmd
[3] *256+ cmd
[4]); 2100 at_sm
= (cmd_in
== MTWSM
); 2103 cmd
[0] = REZERO_UNIT
; 2105 cmd
[1] =1;/* Don't wait for completion */ 2106 timeout
= STp
->timeout
; 2110 printk(ST_DEB_MSG
"st%d: Rewinding tape.\n", dev
); 2112 fileno
= blkno
= at_sm
=0; 2117 cmd
[0] = START_STOP
; 2118 if(cmd_in
== MTLOAD
) 2121 * If arg >= 1 && arg <= 6 Enhanced load/unload in HP C1553A 2123 if(cmd_in
!= MTOFFL
&& 2124 arg
>=1+ MT_ST_HPLOADER_OFFSET
2125 && arg
<=6+ MT_ST_HPLOADER_OFFSET
) { 2128 printk(ST_DEB_MSG
"st%d: Enhanced %sload slot %2ld.\n", 2129 dev
, (cmd
[4]) ?"":"un", 2130 arg
- MT_ST_HPLOADER_OFFSET
); 2133 cmd
[3] = arg
- MT_ST_HPLOADER_OFFSET
;/* MediaID field of C1553A */ 2136 cmd
[1] =1;/* Don't wait for completion */ 2137 timeout
= STp
->timeout
; 2139 timeout
= STp
->long_timeout
; 2143 if(cmd_in
!= MTLOAD
) 2144 printk(ST_DEB_MSG
"st%d: Unloading tape.\n", dev
); 2146 printk(ST_DEB_MSG
"st%d: Loading tape.\n", dev
); 2149 fileno
= blkno
= at_sm
=0; 2154 printk(ST_DEB_MSG
"st%d: No op on tape.\n", dev
); 2156 return0;/* Should do something ? */ 2159 cmd
[0] = START_STOP
; 2161 cmd
[1] =1;/* Don't wait for completion */ 2162 timeout
= STp
->timeout
; 2167 printk(ST_DEB_MSG
"st%d: Retensioning tape.\n", dev
); 2169 fileno
= blkno
= at_sm
=0; 2172 if(!STp
->fast_mteom
) { 2173 /* space to the end of tape */ 2174 ioctl_result
=st_int_ioctl(inode
, MTFSF
,0x3fff); 2175 fileno
= STps
->drv_file
; 2176 if(STps
->eof
>= ST_EOD_1
) 2178 /* The next lines would hide the number of spaced FileMarks 2179 That's why I inserted the previous lines. I had no luck 2180 with detecting EOM with FSF, so we go now to EOM. 2189 printk(ST_DEB_MSG
"st%d: Spacing to end of recorded medium.\n", dev
); 2198 cmd
[1] =1;/* To the end of tape */ 2200 cmd
[1] |=2;/* Don't wait for completion */ 2201 timeout
= STp
->timeout
; 2203 timeout
= STp
->long_timeout
*8; 2207 printk(ST_DEB_MSG
"st%d: Erasing tape.\n", dev
); 2209 fileno
= blkno
= at_sm
=0; 2213 cmd
[0] = ALLOW_MEDIUM_REMOVAL
; 2214 cmd
[4] = SCSI_REMOVAL_PREVENT
; 2217 printk(ST_DEB_MSG
"st%d: Locking drive door.\n", dev
); 2222 cmd
[0] = ALLOW_MEDIUM_REMOVAL
; 2223 cmd
[4] = SCSI_REMOVAL_ALLOW
; 2226 printk(ST_DEB_MSG
"st%d: Unlocking drive door.\n", dev
); 2229 case MTSETBLK
:/* Set block length */ 2230 case MTSETDENSITY
:/* Set tape density */ 2231 case MTSETDRVBUFFER
:/* Set drive buffering */ 2232 case SET_DENS_AND_BLK
:/* Set density and block size */ 2234 if(STp
->dirty
|| (STp
->buffer
)->buffer_bytes
!=0) 2235 return(-EIO
);/* Not allowed if data in buffer */ 2236 if((cmd_in
== MTSETBLK
|| cmd_in
== SET_DENS_AND_BLK
) && 2237 (arg
& MT_ST_BLKSIZE_MASK
) !=0&& 2238 ((arg
& MT_ST_BLKSIZE_MASK
) < STp
->min_block
|| 2239 (arg
& MT_ST_BLKSIZE_MASK
) > STp
->max_block
|| 2240 (arg
& MT_ST_BLKSIZE_MASK
) > st_buffer_size
)) { 2241 printk(KERN_WARNING
"st%d: Illegal block size.\n", dev
); 2244 cmd
[0] = MODE_SELECT
; 2245 cmd
[4] = datalen
=12; 2247 memset((STp
->buffer
)->b_data
,0,12); 2248 if(cmd_in
== MTSETDRVBUFFER
) 2249 (STp
->buffer
)->b_data
[2] = (arg
&7) <<4; 2251 (STp
->buffer
)->b_data
[2] = 2252 STp
->drv_buffer
<<4; 2253 (STp
->buffer
)->b_data
[3] =8;/* block descriptor length */ 2254 if(cmd_in
== MTSETDENSITY
) { 2255 (STp
->buffer
)->b_data
[4] = arg
; 2256 STp
->density_changed
= TRUE
;/* At least we tried ;-) */ 2258 else if(cmd_in
== SET_DENS_AND_BLK
) 2259 (STp
->buffer
)->b_data
[4] = arg
>>24; 2261 (STp
->buffer
)->b_data
[4] = STp
->density
; 2262 if(cmd_in
== MTSETBLK
|| cmd_in
== SET_DENS_AND_BLK
) { 2263 ltmp
= arg
& MT_ST_BLKSIZE_MASK
; 2264 if(cmd_in
== MTSETBLK
) 2265 STp
->blksize_changed
= TRUE
;/* At least we tried ;-) */ 2268 ltmp
= STp
->block_size
; 2269 (STp
->buffer
)->b_data
[9] = (ltmp
>>16); 2270 (STp
->buffer
)->b_data
[10] = (ltmp
>>8); 2271 (STp
->buffer
)->b_data
[11] = ltmp
; 2272 timeout
= STp
->timeout
; 2275 if(cmd_in
== MTSETBLK
|| cmd_in
== SET_DENS_AND_BLK
) 2276 printk(ST_DEB_MSG
"st%d: Setting block size to %d bytes.\n", dev
, 2277 (STp
->buffer
)->b_data
[9] *65536+ 2278 (STp
->buffer
)->b_data
[10] *256+ 2279 (STp
->buffer
)->b_data
[11]); 2280 if(cmd_in
== MTSETDENSITY
|| cmd_in
== SET_DENS_AND_BLK
) 2281 printk(ST_DEB_MSG
"st%d: Setting density code to %x.\n", dev
, 2282 (STp
->buffer
)->b_data
[4]); 2283 if(cmd_in
== MTSETDRVBUFFER
) 2284 printk(ST_DEB_MSG
"st%d: Setting drive buffer code to %d.\n", dev
, 2285 ((STp
->buffer
)->b_data
[2] >>4) &7); 2293 SCpnt
=st_do_scsi(NULL
, STp
, cmd
, datalen
, timeout
, MAX_RETRIES
); 2297 ioctl_result
= (STp
->buffer
)->last_result_fatal
; 2299 if(!ioctl_result
) {/* SCSI command successful */ 2300 scsi_release_command(SCpnt
); 2302 STps
->drv_block
= blkno
; 2303 STps
->drv_file
= fileno
; 2304 STps
->at_sm
= at_sm
; 2306 if(cmd_in
== MTLOCK
) 2307 STp
->door_locked
= ST_LOCKED_EXPLICIT
; 2308 else if(cmd_in
== MTUNLOCK
) 2309 STp
->door_locked
= ST_UNLOCKED
; 2311 if(cmd_in
== MTBSFM
) 2312 ioctl_result
=st_int_ioctl(inode
, MTFSF
,1); 2313 else if(cmd_in
== MTFSFM
) 2314 ioctl_result
=st_int_ioctl(inode
, MTBSF
,1); 2316 if(cmd_in
== MTSETBLK
|| cmd_in
== SET_DENS_AND_BLK
) { 2317 STp
->block_size
= arg
& MT_ST_BLKSIZE_MASK
; 2318 if(STp
->block_size
!=0) 2319 (STp
->buffer
)->buffer_blocks
= 2320 (STp
->buffer
)->buffer_size
/ STp
->block_size
; 2321 (STp
->buffer
)->buffer_bytes
= (STp
->buffer
)->read_pointer
=0; 2322 if(cmd_in
== SET_DENS_AND_BLK
) 2323 STp
->density
= arg
>> MT_ST_DENSITY_SHIFT
; 2325 else if(cmd_in
== MTSETDRVBUFFER
) 2326 STp
->drv_buffer
= (arg
&7); 2327 else if(cmd_in
== MTSETDENSITY
) 2332 else if(cmd_in
== MTFSF
) 2335 STps
->eof
= ST_NOEOF
; 2338 if(cmd_in
== MTOFFL
|| cmd_in
== MTUNLOAD
) 2339 STp
->rew_at_close
=0; 2340 else if(cmd_in
== MTLOAD
) { 2341 STp
->rew_at_close
= (MINOR(inode
->i_rdev
) &0x80) ==0; 2342 for(i
=0; i
< ST_NBR_PARTITIONS
; i
++) { 2343 STp
->ps
[i
].rw
= ST_IDLE
; 2344 STp
->ps
[i
].last_block_valid
= FALSE
; 2349 }else{/* SCSI command was not completely successful. Don't return 2350 from this block without releasing the SCSI command block! */ 2352 if(SCpnt
->sense_buffer
[2] &0x40) { 2353 if(cmd_in
!= MTBSF
&& cmd_in
!= MTBSFM
&& 2354 cmd_in
!= MTBSR
&& cmd_in
!= MTBSS
) 2355 STps
->eof
= ST_EOM_OK
; 2360 (SCpnt
->sense_buffer
[3] <<24) + 2361 (SCpnt
->sense_buffer
[4] <<16) + 2362 (SCpnt
->sense_buffer
[5] <<8) + 2363 SCpnt
->sense_buffer
[6] ); 2364 if(cmd_in
== MTWEOF
&& 2365 (SCpnt
->sense_buffer
[0] &0x70) ==0x70&& 2366 (SCpnt
->sense_buffer
[2] &0x4f) ==0x40&& 2367 ((SCpnt
->sense_buffer
[0] &0x80) ==0|| undone
==0)) { 2368 ioctl_result
=0;/* EOF written succesfully at EOM */ 2371 STps
->drv_file
= fileno
; 2372 STps
->eof
= ST_NOEOF
; 2374 else if( (cmd_in
== MTFSF
) || (cmd_in
== MTFSFM
) ) { 2376 STps
->drv_file
= fileno
- undone
; 2378 STps
->drv_file
= fileno
; 2380 STps
->eof
= ST_NOEOF
; 2382 else if( (cmd_in
== MTBSF
) || (cmd_in
== MTBSFM
) ) { 2384 STps
->drv_file
= fileno
+ undone
; 2386 STps
->drv_file
= fileno
; 2388 STps
->eof
= ST_NOEOF
; 2390 else if(cmd_in
== MTFSR
) { 2391 if(SCpnt
->sense_buffer
[2] &0x80) {/* Hit filemark */ 2392 if(STps
->drv_file
>=0) 2399 STps
->drv_block
= blkno
- undone
; 2401 STps
->drv_block
= (-1); 2402 STps
->eof
= ST_NOEOF
; 2405 else if(cmd_in
== MTBSR
) { 2406 if(SCpnt
->sense_buffer
[2] &0x80) {/* Hit filemark */ 2408 STps
->drv_block
= (-1); 2412 STps
->drv_block
= blkno
+ undone
; 2414 STps
->drv_block
= (-1); 2416 STps
->eof
= ST_NOEOF
; 2418 else if(cmd_in
== MTEOM
) { 2419 STps
->drv_file
= (-1); 2420 STps
->drv_block
= (-1); 2424 STps
->eof
= ST_NOEOF
; 2426 if((SCpnt
->sense_buffer
[2] &0x0f) == BLANK_CHECK
) 2429 if(cmd_in
== MTLOCK
) 2430 STp
->door_locked
= ST_LOCK_FAILS
; 2432 scsi_release_command(SCpnt
); 2436 return ioctl_result
; 2440 \f/* Get the tape position. If bt == 2, arg points into a kernel space mt_loc 2444 get_location(struct inode
* inode
,unsigned int*block
,int*partition
, 2448 int dev
=TAPE_NR(inode
->i_rdev
); 2450 unsigned char scmd
[10]; 2453 STp
= &(scsi_tapes
[dev
]); 2454 if(STp
->ready
!= ST_READY
) 2458 if((STp
->device
)->scsi_level
< SCSI_2
) { 2459 scmd
[0] = QFA_REQUEST_BLOCK
; 2463 scmd
[0] = READ_POSITION
; 2464 if(!logical
&& !STp
->scsi2_logical
) 2467 SCpnt
=st_do_scsi(NULL
, STp
, scmd
,20, STp
->timeout
, MAX_READY_RETRIES
); 2471 if((STp
->buffer
)->last_result_fatal
!=0|| 2472 (STp
->device
->scsi_level
>= SCSI_2
&& 2473 ((STp
->buffer
)->b_data
[0] &4) !=0)) { 2474 *block
= *partition
=0; 2477 printk(ST_DEB_MSG
"st%d: Can't read tape position.\n", dev
); 2483 if((STp
->device
)->scsi_level
< SCSI_2
) { 2484 *block
= ((STp
->buffer
)->b_data
[0] <<16) 2485 + ((STp
->buffer
)->b_data
[1] <<8) 2486 + (STp
->buffer
)->b_data
[2]; 2490 *block
= ((STp
->buffer
)->b_data
[4] <<24) 2491 + ((STp
->buffer
)->b_data
[5] <<16) 2492 + ((STp
->buffer
)->b_data
[6] <<8) 2493 + (STp
->buffer
)->b_data
[7]; 2494 *partition
= (STp
->buffer
)->b_data
[1]; 2495 if(((STp
->buffer
)->b_data
[0] &0x80) && 2496 (STp
->buffer
)->b_data
[1] ==0)/* BOP of partition 0 */ 2497 STp
->ps
[0].drv_block
= STp
->ps
[0].drv_file
=0; 2501 printk(ST_DEB_MSG
"st%d: Got tape pos. blk %d part %d.\n", dev
, 2502 *block
, *partition
); 2506 scsi_release_command(SCpnt
); 2513 /* Set the tape block and partition. Negative partition means that only the 2514 block should be set in vendor specific way. */ 2516 set_location(struct inode
* inode
,unsigned int block
,int partition
, 2521 int dev
=TAPE_NR(inode
->i_rdev
); 2525 unsigned char scmd
[10]; 2528 STp
= &(scsi_tapes
[dev
]); 2529 if(STp
->ready
!= ST_READY
) 2531 timeout
= STp
->long_timeout
; 2532 STps
= &(STp
->ps
[STp
->partition
]); 2536 printk(ST_DEB_MSG
"st%d: Setting block to %d and partition to %d.\n", 2537 dev
, block
, partition
); 2542 /* Update the location at the partition we are leaving */ 2543 if((!STp
->can_partitions
&& partition
!=0) || 2544 partition
>= ST_NBR_PARTITIONS
) 2546 if(partition
!= STp
->partition
) { 2547 if(get_location(inode
, &blk
, &p
,1)) 2548 STps
->last_block_valid
= FALSE
; 2550 STps
->last_block_valid
= TRUE
; 2551 STps
->last_block_visited
= blk
; 2554 printk(ST_DEB_MSG
"st%d: Visited block %d for partition %d saved.\n", 2555 dev
, blk
, STp
->partition
); 2561 if((STp
->device
)->scsi_level
< SCSI_2
) { 2562 scmd
[0] = QFA_SEEK_BLOCK
; 2563 scmd
[2] = (block
>>16); 2564 scmd
[3] = (block
>>8); 2570 scmd
[3] = (block
>>24); 2571 scmd
[4] = (block
>>16); 2572 scmd
[5] = (block
>>8); 2574 if(!logical
&& !STp
->scsi2_logical
) 2576 if(STp
->partition
!= partition
) { 2578 scmd
[8] = partition
; 2581 printk(ST_DEB_MSG
"st%d: Trying to change partition from %d to %d\n", 2582 dev
, STp
->partition
, partition
); 2587 scmd
[1] |=1;/* Don't wait for completion */ 2588 timeout
= STp
->timeout
; 2591 SCpnt
=st_do_scsi(NULL
, STp
, scmd
,20, timeout
, MAX_READY_RETRIES
); 2595 STps
->drv_block
= STps
->drv_file
= (-1); 2596 STps
->eof
= ST_NOEOF
; 2597 if((STp
->buffer
)->last_result_fatal
!=0) { 2599 if(STp
->can_partitions
&& 2600 (STp
->device
)->scsi_level
>= SCSI_2
&& 2601 (p
=find_partition(inode
)) >=0) 2605 if(STp
->can_partitions
) { 2606 STp
->partition
= partition
; 2607 STps
= &(STp
->ps
[partition
]); 2608 if(!STps
->last_block_valid
|| 2609 STps
->last_block_visited
!= block
) { 2617 STps
->drv_block
= STps
->drv_file
=0; 2621 scsi_release_command(SCpnt
); 2628 /* Find the current partition number for the drive status. Called from open and 2629 returns either partition number of negative error code. */ 2631 find_partition(struct inode
*inode
) 2636 if((i
=get_location(inode
, &block
, &partition
,1)) <0) 2638 if(partition
>= ST_NBR_PARTITIONS
) 2644 /* Change the partition if necessary */ 2646 update_partition(struct inode
* inode
) 2648 int dev
=TAPE_NR(inode
->i_rdev
); 2652 STp
= &(scsi_tapes
[dev
]); 2653 if(STp
->partition
== STp
->new_partition
) 2655 STps
= &(STp
->ps
[STp
->new_partition
]); 2656 if(!STps
->last_block_valid
) 2657 STps
->last_block_visited
=0; 2658 returnset_location(inode
, STps
->last_block_visited
, STp
->new_partition
,1); 2661 \f/* Functions for reading and writing the medium partition mode page. These 2662 seem to work with Wangtek 6200HS and HP C1533A. */ 2664 #define PART_PAGE 0x11 2665 #define PART_PAGE_LENGTH 10 2667 /* Get the number of partitions on the tape. As a side effect reads the 2668 mode page into the tape buffer. */ 2670 nbr_partitions(struct inode
* inode
) 2672 int dev
=TAPE_NR(inode
->i_rdev
), result
; 2674 Scsi_Cmnd
* SCpnt
= NULL
; 2675 unsigned char cmd
[10]; 2677 STp
= &(scsi_tapes
[dev
]); 2678 if(STp
->ready
!= ST_READY
) 2681 memset((void*) &cmd
[0],0,10); 2682 cmd
[0] = MODE_SENSE
; 2683 cmd
[1] =8;/* Page format */ 2687 SCpnt
=st_do_scsi(SCpnt
, STp
, cmd
,200, STp
->timeout
, MAX_READY_RETRIES
); 2690 scsi_release_command(SCpnt
); 2693 if((STp
->buffer
)->last_result_fatal
!=0) { 2696 printk(ST_DEB_MSG
"st%d: Can't read medium partition page.\n", dev
); 2701 result
= (STp
->buffer
)->b_data
[MODE_HEADER_LENGTH
+3] +1; 2704 printk(ST_DEB_MSG
"st%d: Number of partitions %d.\n", dev
, result
); 2712 /* Partition the tape into two partitions if size > 0 or one partition if 2715 partition_tape(struct inode
* inode
,int size
) 2717 int dev
=TAPE_NR(inode
->i_rdev
), result
; 2720 Scsi_Cmnd
* SCpnt
= NULL
; 2721 unsigned char cmd
[10], *bp
; 2723 if((result
=nbr_partitions(inode
)) <0) 2725 STp
= &(scsi_tapes
[dev
]); 2727 /* The mode page is in the buffer. Let's modify it and write it. */ 2728 bp
= &((STp
->buffer
)->b_data
[0]); 2731 bp
[MODE_HEADER_LENGTH
+3] =0; 2734 printk(ST_DEB_MSG
"st%d: Formatting tape with one partition.\n", dev
); 2739 bp
[MODE_HEADER_LENGTH
+3] =1; 2740 bp
[MODE_HEADER_LENGTH
+8] = (size
>>8) &0xff; 2741 bp
[MODE_HEADER_LENGTH
+9] = size
&0xff; 2744 printk(ST_DEB_MSG
"st%d: Formatting tape with two partition (1 = %d MB).\n", 2748 bp
[MODE_HEADER_LENGTH
+6] =0; 2749 bp
[MODE_HEADER_LENGTH
+7] =0; 2750 bp
[MODE_HEADER_LENGTH
+4] =0x30;/* IDP | PSUM = MB */ 2754 bp
[MODE_HEADER_LENGTH
] &=0x3f; 2755 bp
[MODE_HEADER_LENGTH
+1] = length
-2; 2758 cmd
[0] = MODE_SELECT
; 2760 cmd
[4] = length
+ MODE_HEADER_LENGTH
; 2762 SCpnt
=st_do_scsi(SCpnt
, STp
, cmd
, cmd
[4], STp
->long_timeout
, MAX_READY_RETRIES
); 2765 scsi_release_command(SCpnt
); 2768 if((STp
->buffer
)->last_result_fatal
!=0) { 2769 printk(KERN_INFO
"st%d: Partitioning of tape failed.\n", dev
); 2780 /* The ioctl command */ 2782 st_ioctl(struct inode
* inode
,struct file
* file
, 2783 unsigned int cmd_in
,unsigned long arg
) 2785 int i
, cmd_nr
, cmd_type
, bt
; 2788 struct mtpos mt_pos
; 2792 int dev
=TAPE_NR(inode
->i_rdev
); 2794 STp
= &(scsi_tapes
[dev
]); 2796 if(debugging
&& !STp
->in_use
) { 2797 printk(ST_DEB_MSG
"st%d: Incorrect device.\n", dev
); 2801 STm
= &(STp
->modes
[STp
->current_mode
]); 2802 STps
= &(STp
->ps
[STp
->partition
]); 2805 * If we are in the middle of error recovery, don't let anyone 2806 * else try and use this device. Also, if error recovery fails, it 2807 * may try and take the device offline, in which case all further 2808 * access to the device is prohibited. 2810 if( !scsi_block_when_processing_errors(STp
->device
) ) 2815 cmd_type
=_IOC_TYPE(cmd_in
); 2816 cmd_nr
=_IOC_NR(cmd_in
); 2818 if(cmd_type
==_IOC_TYPE(MTIOCTOP
) && cmd_nr
==_IOC_NR(MTIOCTOP
)) { 2819 if(_IOC_SIZE(cmd_in
) !=sizeof(mtc
)) 2822 i
=copy_from_user((char*) &mtc
, (char*)arg
,sizeof(struct mtop
)); 2826 if(mtc
.mt_op
== MTSETDRVBUFFER
&& !suser()) { 2827 printk(KERN_WARNING
"st%d: MTSETDRVBUFFER only allowed for root.\n", dev
); 2831 (mtc
.mt_op
!= MTSETDRVBUFFER
&& (mtc
.mt_count
& MT_ST_OPTIONS
) ==0)) 2834 if(!(STp
->device
)->was_reset
) { 2836 if(STps
->eof
== ST_FM_HIT
) { 2837 if(mtc
.mt_op
== MTFSF
|| mtc
.mt_op
== MTFSFM
|| mtc
.mt_op
== MTEOM
) { 2839 if(STps
->drv_file
>=0) 2842 else if(mtc
.mt_op
== MTBSF
|| mtc
.mt_op
== MTBSFM
) { 2844 if(STps
->drv_file
>=0) 2849 if(mtc
.mt_op
== MTSEEK
) { 2850 /* Old position must be restored if partition will be changed */ 2851 i
= !STp
->can_partitions
|| 2852 (STp
->new_partition
!= STp
->partition
); 2855 i
= mtc
.mt_op
== MTREW
|| mtc
.mt_op
== MTOFFL
|| 2856 mtc
.mt_op
== MTRETEN
|| mtc
.mt_op
== MTEOM
|| 2857 mtc
.mt_op
== MTLOCK
|| mtc
.mt_op
== MTLOAD
|| 2858 mtc
.mt_op
== MTCOMPRESSION
; 2860 i
=flush_buffer(inode
, file
, i
); 2866 * If there was a bus reset, block further access 2867 * to this device. If the user wants to rewind the tape, 2868 * then reset the flag and allow access again. 2870 if(mtc
.mt_op
!= MTREW
&& 2871 mtc
.mt_op
!= MTOFFL
&& 2872 mtc
.mt_op
!= MTRETEN
&& 2873 mtc
.mt_op
!= MTERASE
&& 2874 mtc
.mt_op
!= MTSEEK
&& 2877 STp
->device
->was_reset
=0; 2878 if(STp
->door_locked
!= ST_UNLOCKED
&& 2879 STp
->door_locked
!= ST_LOCK_FAILS
) { 2880 if(st_int_ioctl(inode
, MTLOCK
,0)) { 2881 printk(KERN_NOTICE
"st%d: Could not relock door after bus reset.\n", 2883 STp
->door_locked
= ST_UNLOCKED
; 2888 if(mtc
.mt_op
!= MTNOP
&& mtc
.mt_op
!= MTSETBLK
&& 2889 mtc
.mt_op
!= MTSETDENSITY
&& mtc
.mt_op
!= MTWSM
&& 2890 mtc
.mt_op
!= MTSETDRVBUFFER
&& mtc
.mt_op
!= MTSETPART
) 2891 STps
->rw
= ST_IDLE
;/* Prevent automatic WEOF and fsf */ 2893 if(mtc
.mt_op
== MTOFFL
&& STp
->door_locked
!= ST_UNLOCKED
) 2894 st_int_ioctl(inode
, MTUNLOCK
,0);/* Ignore result! */ 2896 if(mtc
.mt_op
== MTSETDRVBUFFER
&& 2897 (mtc
.mt_count
& MT_ST_OPTIONS
) !=0) 2898 returnst_set_options(inode
, mtc
.mt_count
); 2899 if(mtc
.mt_op
== MTSETPART
) { 2900 if(!STp
->can_partitions
|| 2901 mtc
.mt_count
<0|| mtc
.mt_count
>= ST_NBR_PARTITIONS
) 2903 if(mtc
.mt_count
>= STp
->nbr_partitions
&& 2904 (STp
->nbr_partitions
=nbr_partitions(inode
)) <0) 2906 if(mtc
.mt_count
>= STp
->nbr_partitions
) 2908 STp
->new_partition
= mtc
.mt_count
; 2911 if(mtc
.mt_op
== MTMKPART
) { 2912 if(!STp
->can_partitions
) 2914 if((i
=st_int_ioctl(inode
, MTREW
,0)) <0|| 2915 (i
=partition_tape(inode
, mtc
.mt_count
)) <0) 2917 for(i
=0; i
< ST_NBR_PARTITIONS
; i
++) { 2918 STp
->ps
[i
].rw
= ST_IDLE
; 2919 STp
->ps
[i
].at_sm
=0; 2920 STp
->ps
[i
].last_block_valid
= FALSE
; 2922 STp
->partition
= STp
->new_partition
=0; 2923 STp
->nbr_partitions
=1;/* Bad guess ?-) */ 2924 STps
->drv_block
= STps
->drv_file
=0; 2927 if(mtc
.mt_op
== MTSEEK
) { 2928 i
=set_location(inode
, mtc
.mt_count
, STp
->new_partition
,0); 2929 if(!STp
->can_partitions
) 2930 STp
->ps
[0].rw
= ST_IDLE
; 2933 if(STp
->can_partitions
&& STp
->ready
== ST_READY
&& 2934 (i
=update_partition(inode
)) <0) 2936 if(mtc
.mt_op
== MTCOMPRESSION
) 2937 returnst_compression(STp
, (mtc
.mt_count
&1)); 2939 returnst_int_ioctl(inode
, mtc
.mt_op
, mtc
.mt_count
); 2945 if((i
=flush_buffer(inode
, file
, FALSE
)) <0) 2947 if(STp
->can_partitions
&& 2948 (i
=update_partition(inode
)) <0) 2951 if(cmd_type
==_IOC_TYPE(MTIOCGET
) && cmd_nr
==_IOC_NR(MTIOCGET
)) { 2953 if(_IOC_SIZE(cmd_in
) !=sizeof(struct mtget
)) 2956 (STp
->mt_status
)->mt_dsreg
= 2957 ((STp
->block_size
<< MT_ST_BLKSIZE_SHIFT
) & MT_ST_BLKSIZE_MASK
) | 2958 ((STp
->density
<< MT_ST_DENSITY_SHIFT
) & MT_ST_DENSITY_MASK
); 2959 (STp
->mt_status
)->mt_blkno
= STps
->drv_block
; 2960 (STp
->mt_status
)->mt_fileno
= STps
->drv_file
; 2961 if(STp
->block_size
!=0) { 2962 if(STps
->rw
== ST_WRITING
) 2963 (STp
->mt_status
)->mt_blkno
+= 2964 (STp
->buffer
)->buffer_bytes
/ STp
->block_size
; 2965 else if(STps
->rw
== ST_READING
) 2966 (STp
->mt_status
)->mt_blkno
-= ((STp
->buffer
)->buffer_bytes
+ 2967 STp
->block_size
-1) / STp
->block_size
; 2970 (STp
->mt_status
)->mt_gstat
=0; 2971 if(STp
->drv_write_prot
) 2972 (STp
->mt_status
)->mt_gstat
|=GMT_WR_PROT(0xffffffff); 2973 if((STp
->mt_status
)->mt_blkno
==0) { 2974 if((STp
->mt_status
)->mt_fileno
==0) 2975 (STp
->mt_status
)->mt_gstat
|=GMT_BOT(0xffffffff); 2977 (STp
->mt_status
)->mt_gstat
|=GMT_EOF(0xffffffff); 2979 (STp
->mt_status
)->mt_resid
= STp
->partition
; 2980 if(STps
->eof
== ST_EOM_OK
|| STps
->eof
== ST_EOM_ERROR
) 2981 (STp
->mt_status
)->mt_gstat
|=GMT_EOT(0xffffffff); 2982 else if(STps
->eof
>= ST_EOM_OK
) 2983 (STp
->mt_status
)->mt_gstat
|=GMT_EOD(0xffffffff); 2984 if(STp
->density
==1) 2985 (STp
->mt_status
)->mt_gstat
|=GMT_D_800(0xffffffff); 2986 else if(STp
->density
==2) 2987 (STp
->mt_status
)->mt_gstat
|=GMT_D_1600(0xffffffff); 2988 else if(STp
->density
==3) 2989 (STp
->mt_status
)->mt_gstat
|=GMT_D_6250(0xffffffff); 2990 if(STp
->ready
== ST_READY
) 2991 (STp
->mt_status
)->mt_gstat
|=GMT_ONLINE(0xffffffff); 2992 if(STp
->ready
== ST_NO_TAPE
) 2993 (STp
->mt_status
)->mt_gstat
|=GMT_DR_OPEN(0xffffffff); 2995 (STp
->mt_status
)->mt_gstat
|=GMT_SM(0xffffffff); 2996 if(STm
->do_async_writes
|| (STm
->do_buffer_writes
&& STp
->block_size
!=0) || 2997 STp
->drv_buffer
!=0) 2998 (STp
->mt_status
)->mt_gstat
|=GMT_IM_REP_EN(0xffffffff); 3000 i
=copy_to_user((char*)arg
, (char*)(STp
->mt_status
), 3001 sizeof(struct mtget
)); 3005 (STp
->mt_status
)->mt_erreg
=0;/* Clear after read */ 3007 }/* End of MTIOCGET */ 3009 if(cmd_type
==_IOC_TYPE(MTIOCPOS
) && cmd_nr
==_IOC_NR(MTIOCPOS
)) { 3010 if(_IOC_SIZE(cmd_in
) !=sizeof(struct mtpos
)) 3012 if((i
=get_location(inode
, &blk
, &bt
,0)) <0) 3014 mt_pos
.mt_blkno
= blk
; 3015 i
=copy_to_user((char*)arg
, (char*) (&mt_pos
),sizeof(struct mtpos
)); 3021 returnscsi_ioctl(STp
->device
, cmd_in
, (void*) arg
); 3025 /* Try to allocate a new tape buffer */ 3027 new_tape_buffer(int from_initialization
,int need_dma
) 3029 int priority
, a_size
; 3032 if(st_nbr_buffers
>= st_template
.dev_max
) 3033 return NULL
;/* Should never happen */ 3035 if(from_initialization
) { 3036 priority
= GFP_ATOMIC
; 3037 a_size
= st_buffer_size
; 3040 priority
= GFP_KERNEL
; 3041 for(a_size
= PAGE_SIZE
; a_size
< st_buffer_size
; a_size
<<=1) 3042 ;/* Make sure we allocate efficiently */ 3044 tb
= (ST_buffer
*)scsi_init_malloc(sizeof(ST_buffer
), priority
); 3047 priority
|= GFP_DMA
; 3048 tb
->b_data
= (unsigned char*)scsi_init_malloc(a_size
, priority
); 3050 scsi_init_free((char*)tb
,sizeof(ST_buffer
)); 3055 printk(KERN_NOTICE
"st: Can't allocate new tape buffer (nbr %d).\n", 3062 "st: Allocated tape buffer %d (%d bytes, dma: %d, a: %p).\n", 3063 st_nbr_buffers
, a_size
, need_dma
, tb
->b_data
); 3067 tb
->buffer_size
= a_size
; 3069 tb
->orig_b_data
= NULL
; 3070 st_buffers
[st_nbr_buffers
++] = tb
; 3075 /* Try to allocate a temporary enlarged tape buffer */ 3077 enlarge_buffer(ST_buffer
*STbuffer
,int new_size
,int need_dma
) 3079 int a_size
, priority
; 3082 normalize_buffer(STbuffer
); 3084 for(a_size
= PAGE_SIZE
; a_size
< new_size
; a_size
<<=1) 3085 ;/* Make sure that we allocate efficiently */ 3087 priority
= GFP_KERNEL
; 3089 priority
|= GFP_DMA
; 3090 tbd
= (unsigned char*)scsi_init_malloc(a_size
, priority
); 3096 "st: Buffer at %p enlarged to %d bytes (dma: %d, a: %p).\n", 3097 STbuffer
->b_data
, a_size
, need_dma
, tbd
); 3100 STbuffer
->orig_b_data
= STbuffer
->b_data
; 3101 STbuffer
->orig_size
= STbuffer
->buffer_size
; 3102 STbuffer
->b_data
= tbd
; 3103 STbuffer
->buffer_size
= a_size
; 3108 /* Release the extra buffer */ 3110 normalize_buffer(ST_buffer
*STbuffer
) 3112 if(STbuffer
->orig_b_data
== NULL
) 3115 scsi_init_free(STbuffer
->b_data
, STbuffer
->buffer_size
); 3116 STbuffer
->b_data
= STbuffer
->orig_b_data
; 3117 STbuffer
->orig_b_data
= NULL
; 3118 STbuffer
->buffer_size
= STbuffer
->orig_size
; 3122 printk(ST_DEB_MSG
"st: Buffer at %p normalized to %d bytes.\n", 3123 STbuffer
->b_data
, STbuffer
->buffer_size
); 3129 /* Set the boot options. Syntax: st=xxx,yyy 3130 where xxx is buffer size in 1024 byte blocks and yyy is write threshold 3131 in 1024 byte blocks. */ 3133 st_setup(char*str
,int*ints
)) 3135 if(ints
[0] >0&& ints
[1] >0) 3136 st_buffer_size
= ints
[1] * ST_KILOBYTE
; 3137 if(ints
[0] >1&& ints
[2] >0) { 3138 st_write_threshold
= ints
[2] * ST_KILOBYTE
; 3139 if(st_write_threshold
> st_buffer_size
) 3140 st_write_threshold
= st_buffer_size
; 3142 if(ints
[0] >2&& ints
[3] >0) 3143 st_max_buffers
= ints
[3]; 3148 static struct file_operations st_fops
= { 3149 NULL
,/* lseek - default */ 3150 st_read
,/* read - general block-dev read */ 3151 st_write
,/* write - general block-dev write */ 3152 NULL
,/* readdir - bad */ 3154 st_ioctl
,/* ioctl */ 3156 scsi_tape_open
,/* open */ 3157 scsi_tape_close
,/* release */ 3161 static intst_attach(Scsi_Device
* SDp
){ 3167 if(SDp
->type
!= TYPE_TAPE
)return1; 3169 if(st_template
.nr_dev
>= st_template
.dev_max
) 3175 for(tpnt
= scsi_tapes
, i
=0; i
<st_template
.dev_max
; i
++, tpnt
++) 3176 if(!tpnt
->device
)break; 3178 if(i
>= st_template
.dev_max
)panic("scsi_devices corrupt (st)"); 3180 scsi_tapes
[i
].device
= SDp
; 3181 if(SDp
->scsi_level
<=2) 3182 scsi_tapes
[i
].mt_status
->mt_type
= MT_ISSCSI1
; 3184 scsi_tapes
[i
].mt_status
->mt_type
= MT_ISSCSI2
; 3186 tpnt
->devt
=MKDEV(SCSI_TAPE_MAJOR
, i
); 3188 tpnt
->waiting
= NULL
; 3190 tpnt
->drv_buffer
=1;/* Try buffering if no mode sense */ 3191 tpnt
->restr_dma
= (SDp
->host
)->unchecked_isa_dma
; 3193 tpnt
->do_auto_lock
= ST_AUTO_LOCK
; 3194 tpnt
->can_bsr
= ST_IN_FILE_POS
; 3195 tpnt
->can_partitions
=0; 3196 tpnt
->two_fm
= ST_TWO_FM
; 3197 tpnt
->fast_mteom
= ST_FAST_MTEOM
; 3198 tpnt
->scsi2_logical
= ST_SCSI2LOGICAL
; 3199 tpnt
->write_threshold
= st_write_threshold
; 3200 tpnt
->default_drvbuffer
=0xff;/* No forced buffering */ 3202 tpnt
->new_partition
=0; 3203 tpnt
->nbr_partitions
=0; 3204 tpnt
->timeout
= ST_TIMEOUT
; 3205 tpnt
->long_timeout
= ST_LONG_TIMEOUT
; 3207 for(i
=0; i
< ST_NBR_MODES
; i
++) { 3208 STm
= &(tpnt
->modes
[i
]); 3209 STm
->defined
= FALSE
; 3210 STm
->sysv
= ST_SYSV
; 3211 STm
->defaults_for_writes
=0; 3212 STm
->do_async_writes
= ST_ASYNC_WRITES
; 3213 STm
->do_buffer_writes
= ST_BUFFER_WRITES
; 3214 STm
->do_read_ahead
= ST_READ_AHEAD
; 3215 STm
->default_compression
= ST_DONT_TOUCH
; 3216 STm
->default_blksize
= (-1);/* No forced size */ 3217 STm
->default_density
= (-1);/* No forced density */ 3220 for(i
=0; i
< ST_NBR_PARTITIONS
; i
++) { 3221 STps
= &(tpnt
->ps
[i
]); 3223 STps
->eof
= ST_NOEOF
; 3225 STps
->last_block_valid
= FALSE
; 3230 tpnt
->current_mode
=0; 3231 tpnt
->modes
[0].defined
= TRUE
; 3233 tpnt
->density_changed
= tpnt
->compression_changed
= 3234 tpnt
->blksize_changed
= FALSE
; 3236 st_template
.nr_dev
++; 3240 static intst_detect(Scsi_Device
* SDp
) 3242 if(SDp
->type
!= TYPE_TAPE
)return0; 3245 "Detected scsi tape st%d at scsi%d, channel %d, id %d, lun %d\n", 3246 st_template
.dev_noticed
++, 3247 SDp
->host
->host_no
, SDp
->channel
, SDp
->id
, SDp
->lun
); 3252 static int st_registered
=0; 3254 /* Driver initialization (not __initfunc because may be called later) */ 3259 #if !ST_RUNTIME_BUFFERS 3263 if(st_template
.dev_noticed
==0)return0; 3265 if(!st_registered
) { 3266 if(register_chrdev(SCSI_TAPE_MAJOR
,"st",&st_fops
)) { 3267 printk(KERN_ERR
"Unable to get major %d for SCSI tapes\n",MAJOR_NR
); 3273 if(scsi_tapes
)return0; 3274 st_template
.dev_max
= st_template
.dev_noticed
+ ST_EXTRA_DEVS
; 3275 if(st_template
.dev_max
< ST_MAX_TAPES
) 3276 st_template
.dev_max
= ST_MAX_TAPES
; 3277 if(st_template
.dev_max
>128/ ST_NBR_MODES
) 3278 printk(KERN_INFO
"st: Only %d tapes accessible.\n",128/ ST_NBR_MODES
); 3280 (Scsi_Tape
*)scsi_init_malloc(st_template
.dev_max
*sizeof(Scsi_Tape
), 3282 if(scsi_tapes
== NULL
) { 3283 printk(KERN_ERR
"Unable to allocate descriptors for SCSI tapes.\n"); 3284 unregister_chrdev(SCSI_TAPE_MAJOR
,"st"); 3289 printk(ST_DEB_MSG
"st: Buffer size %d bytes, write threshold %d bytes.\n", 3290 st_buffer_size
, st_write_threshold
); 3293 memset(scsi_tapes
,0, st_template
.dev_max
*sizeof(Scsi_Tape
)); 3294 for(i
=0; i
< st_template
.dev_max
; ++i
) { 3295 STp
= &(scsi_tapes
[i
]); 3296 STp
->capacity
=0xfffff; 3297 STp
->mt_status
= (struct mtget
*)scsi_init_malloc(sizeof(struct mtget
), 3299 /* Initialize status */ 3300 memset((void*) scsi_tapes
[i
].mt_status
,0,sizeof(struct mtget
)); 3303 /* Allocate the buffers */ 3305 (ST_buffer
**)scsi_init_malloc(st_template
.dev_max
*sizeof(ST_buffer
*), 3307 if(st_buffers
== NULL
) { 3308 printk(KERN_ERR
"Unable to allocate tape buffer pointers.\n"); 3309 unregister_chrdev(SCSI_TAPE_MAJOR
,"st"); 3310 scsi_init_free((char*) scsi_tapes
, 3311 st_template
.dev_max
*sizeof(Scsi_Tape
)); 3315 #if ST_RUNTIME_BUFFERS 3318 target_nbr
= st_template
.dev_noticed
; 3319 if(target_nbr
< ST_EXTRA_DEVS
) 3320 target_nbr
= ST_EXTRA_DEVS
; 3321 if(target_nbr
> st_max_buffers
) 3322 target_nbr
= st_max_buffers
; 3324 for(i
=st_nbr_buffers
=0; i
< target_nbr
; i
++) { 3325 if(!new_tape_buffer(TRUE
, TRUE
)) { 3328 printk(KERN_ERR
"Can't continue without at least one tape buffer.\n"); 3329 unregister_chrdev(SCSI_TAPE_MAJOR
,"st"); 3330 scsi_init_free((char*) st_buffers
, 3331 st_template
.dev_max
*sizeof(ST_buffer
*)); 3332 scsi_init_free((char*) scsi_tapes
, 3333 st_template
.dev_max
*sizeof(Scsi_Tape
)); 3336 printk(KERN_INFO
"No tape buffers allocated at initialization.\n"); 3340 printk(KERN_INFO
"Number of tape buffers adjusted.\n"); 3348 static voidst_detach(Scsi_Device
* SDp
) 3353 for(tpnt
= scsi_tapes
, i
=0; i
<st_template
.dev_max
; i
++, tpnt
++) 3354 if(tpnt
->device
== SDp
) { 3355 tpnt
->device
= NULL
; 3357 st_template
.nr_dev
--; 3358 st_template
.dev_noticed
--; 3367 intinit_module(void) { 3370 st_template
.module
= &__this_module
; 3371 result
=scsi_register_module(MODULE_SCSI_DEV
, &st_template
); 3376 st_buffer_size
= buffer_kbs
* ST_KILOBYTE
; 3377 if(write_threshold_kbs
>0) 3378 st_write_threshold
= write_threshold_kbs
* ST_KILOBYTE
; 3379 if(st_write_threshold
> st_buffer_size
) 3380 st_write_threshold
= st_buffer_size
; 3382 st_max_buffers
= max_buffers
; 3383 printk(KERN_INFO
"st: bufsize %d, wrt %d, max buffers %d.\n", 3384 st_buffer_size
, st_write_threshold
, st_max_buffers
); 3389 voidcleanup_module(void) 3393 scsi_unregister_module(MODULE_SCSI_DEV
, &st_template
); 3394 unregister_chrdev(SCSI_TAPE_MAJOR
,"st"); 3396 if(scsi_tapes
!= NULL
) { 3397 scsi_init_free((char*) scsi_tapes
, 3398 st_template
.dev_max
*sizeof(Scsi_Tape
)); 3400 if(st_buffers
!= NULL
) { 3401 for(i
=0; i
< st_nbr_buffers
; i
++) 3402 if(st_buffers
[i
] != NULL
) { 3403 scsi_init_free((char*) st_buffers
[i
]->b_data
, 3404 st_buffers
[i
]->buffer_size
); 3405 scsi_init_free((char*) st_buffers
[i
],sizeof(ST_buffer
)); 3408 scsi_init_free((char*) st_buffers
, 3409 st_template
.dev_max
*sizeof(ST_buffer
*)); 3412 st_template
.dev_max
=0; 3413 printk(KERN_INFO
"st: Unloaded.\n");