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, Eric Lee Green, Wolfgang Denk, Steve Hirsch,
9 Andreas Koppenh"ofer, Michael Leodolter, Eyal Lebedinsky, Michael Schaefer,
10 J"org Weule, and Eric Youngdale.
12 Copyright 1992 - 2000 Kai Makisara
13 email Kai.Makisara@metla.fi
15 Last modified: Tue Aug 15 16:56:35 2000 by makisara@kai.makisara.local
16 Some small formal changes - aeb, 950809
18 Last modified: 18-JAN-1998 Richard Gooch <rgooch@atnf.csiro.au> Devfs support
20 Reminder: write_lock_irqsave() can be replaced by write_lock() when the old SCSI
21 error handling will be discarded.
24 #include <linux/module.h>
27 #include <linux/kernel.h>
28 #include <linux/sched.h>
30 #include <linux/init.h>
31 #include <linux/string.h>
32 #include <linux/errno.h>
33 #include <linux/mtio.h>
34 #include <linux/ioctl.h>
35 #include <linux/fcntl.h>
36 #include <linux/spinlock.h>
37 #include <linux/smp_lock.h>
38 #include <asm/uaccess.h>
40 #include <asm/system.h>
42 /* The driver prints some debugging information on the console if DEBUG
43 is defined and non-zero. */
47 /* The message level for the debug messages is currently set to KERN_NOTICE
48 so that people can easily see the messages. Later when the debugging messages
49 in the drivers are more widely classified, this may be changed to KERN_DEBUG. */
50 #define ST_DEB_MSG KERN_NOTICE
52 #define DEBC(a) if (debugging) { a ; }
58 #define MAJOR_NR SCSI_TAPE_MAJOR
59 #include <linux/blk.h>
63 #include <scsi/scsi_ioctl.h>
65 #define ST_KILOBYTE 1024
67 #include "st_options.h"
70 #include "constants.h"
72 static int buffer_kbs
;
73 static int write_threshold_kbs
;
74 static int max_buffers
= (-1);
75 static int max_sg_segs
;
77 MODULE_AUTHOR("Kai Makisara");
78 MODULE_DESCRIPTION("SCSI Tape Driver");
79 MODULE_PARM(buffer_kbs
, "i");
80 MODULE_PARM(write_threshold_kbs
, "i");
81 MODULE_PARM(max_buffers
, "i");
82 MODULE_PARM(max_sg_segs
, "i");
85 static struct st_dev_parm
{
88 } parms
[] __initdata
= {
90 "buffer_kbs", &buffer_kbs
93 "write_threshold_kbs", &write_threshold_kbs
96 "max_buffers", &max_buffers
99 "max_sg_segs", &max_sg_segs
105 /* The default definitions have been moved to st_options.h */
107 #define ST_BUFFER_SIZE (ST_BUFFER_BLOCKS * ST_KILOBYTE)
108 #define ST_WRITE_THRESHOLD (ST_WRITE_THRESHOLD_BLOCKS * ST_KILOBYTE)
110 /* The buffer size should fit into the 24 bits for length in the
111 6-byte SCSI read and write commands. */
112 #if ST_BUFFER_SIZE >= (2 << 24 - 1)
113 #error "Buffer size should not exceed (2 << 24 - 1) bytes!"
116 DEB( static int debugging
= DEBUG
; )
118 #define MAX_RETRIES 0
119 #define MAX_WRITE_RETRIES 0
120 #define MAX_READY_RETRIES 5
121 #define NO_TAPE NOT_READY
123 #define ST_TIMEOUT (900 * HZ)
124 #define ST_LONG_TIMEOUT (14000 * HZ)
126 #define TAPE_NR(x) (MINOR(x) & ~(128 | ST_MODE_MASK))
127 #define TAPE_MODE(x) ((MINOR(x) & ST_MODE_MASK) >> ST_MODE_SHIFT)
129 /* Internal ioctl to set both density (uppermost 8 bits) and blocksize (lower
131 #define SET_DENS_AND_BLK 0x10001
133 #define ST_DEV_ARR_LUMP 6
134 static rwlock_t st_dev_arr_lock
= RW_LOCK_UNLOCKED
;
136 static int st_nbr_buffers
;
137 static ST_buffer
**st_buffers
= NULL
;
138 static int st_buffer_size
= ST_BUFFER_SIZE
;
139 static int st_write_threshold
= ST_WRITE_THRESHOLD
;
140 static int st_max_buffers
= ST_MAX_BUFFERS
;
141 static int st_max_sg_segs
= ST_MAX_SG
;
143 static Scsi_Tape
**scsi_tapes
= NULL
;
145 static int modes_defined
;
147 static ST_buffer
*new_tape_buffer(int, int, int);
148 static int enlarge_buffer(ST_buffer
*, int, int);
149 static void normalize_buffer(ST_buffer
*);
150 static int append_to_buffer(const char *, ST_buffer
*, int);
151 static int from_buffer(ST_buffer
*, char *, int);
153 static int st_init(void);
154 static int st_attach(Scsi_Device
*);
155 static int st_detect(Scsi_Device
*);
156 static void st_detach(Scsi_Device
*);
158 struct Scsi_Device_Template st_template
=
163 major
:SCSI_TAPE_MAJOR
,
170 static int st_compression(Scsi_Tape
*, int);
172 static int find_partition(Scsi_Tape
*);
173 static int update_partition(Scsi_Tape
*);
175 static int st_int_ioctl(Scsi_Tape
*, unsigned int, unsigned long);
178 /* Convert the result to success code */
179 static int st_chk_result(Scsi_Tape
*STp
, Scsi_Request
* SRpnt
)
182 int result
= SRpnt
->sr_result
;
183 unsigned char *sense
= SRpnt
->sr_sense_buffer
, scode
;
184 DEB(const char *stp
;)
187 sense
[0] = 0; /* We don't have sense data if this byte is zero */
191 if (driver_byte(result
) & DRIVER_SENSE
)
192 scode
= sense
[2] & 0x0f;
198 dev
= TAPE_NR(SRpnt
->sr_request
.rq_dev
);
201 printk(ST_DEB_MSG
"st%d: Error: %x, cmd: %x %x %x %x %x %x Len: %d\n",
203 SRpnt
->sr_cmnd
[0], SRpnt
->sr_cmnd
[1], SRpnt
->sr_cmnd
[2],
204 SRpnt
->sr_cmnd
[3], SRpnt
->sr_cmnd
[4], SRpnt
->sr_cmnd
[5],
206 if (driver_byte(result
) & DRIVER_SENSE
)
207 print_req_sense("st", SRpnt
);
208 } else ) /* end DEB */
209 if (!(driver_byte(result
) & DRIVER_SENSE
) ||
210 ((sense
[0] & 0x70) == 0x70 &&
212 scode
!= RECOVERED_ERROR
&&
213 /* scode != UNIT_ATTENTION && */
214 scode
!= BLANK_CHECK
&&
215 scode
!= VOLUME_OVERFLOW
&&
216 SRpnt
->sr_cmnd
[0] != MODE_SENSE
&&
217 SRpnt
->sr_cmnd
[0] != TEST_UNIT_READY
)) { /* Abnormal conditions for tape */
218 if (driver_byte(result
) & DRIVER_SENSE
) {
219 printk(KERN_WARNING
"st%d: Error with sense data: ", dev
);
220 print_req_sense("st", SRpnt
);
223 "st%d: Error %x (sugg. bt 0x%x, driver bt 0x%x, host bt 0x%x).\n",
224 dev
, result
, suggestion(result
),
225 driver_byte(result
) & DRIVER_MASK
, host_byte(result
));
228 if ((sense
[0] & 0x70) == 0x70 &&
229 scode
== RECOVERED_ERROR
230 #if ST_RECOVERED_WRITE_FATAL
231 && SRpnt
->sr_cmnd
[0] != WRITE_6
232 && SRpnt
->sr_cmnd
[0] != WRITE_FILEMARKS
235 STp
->recover_count
++;
240 if (SRpnt
->sr_cmnd
[0] == READ_6
)
242 else if (SRpnt
->sr_cmnd
[0] == WRITE_6
)
246 printk(ST_DEB_MSG
"st%d: Recovered %s error (%d).\n", dev
, stp
,
250 if ((sense
[2] & 0xe0) == 0)
257 /* Wakeup from interrupt */
258 static void st_sleep_done(Scsi_Cmnd
* SCpnt
)
264 if ((st_nbr
= TAPE_NR(SCpnt
->request
.rq_dev
)) < st_template
.nr_dev
) {
265 read_lock(&st_dev_arr_lock
);
266 STp
= scsi_tapes
[st_nbr
];
267 read_unlock(&st_dev_arr_lock
);
268 if ((STp
->buffer
)->writing
&&
269 (SCpnt
->sense_buffer
[0] & 0x70) == 0x70 &&
270 (SCpnt
->sense_buffer
[2] & 0x40)) {
271 /* EOM at write-behind, has all been written? */
272 if ((SCpnt
->sense_buffer
[0] & 0x80) != 0)
273 remainder
= (SCpnt
->sense_buffer
[3] << 24) |
274 (SCpnt
->sense_buffer
[4] << 16) |
275 (SCpnt
->sense_buffer
[5] << 8) |
276 SCpnt
->sense_buffer
[6];
279 if ((SCpnt
->sense_buffer
[2] & 0x0f) == VOLUME_OVERFLOW
||
281 (STp
->buffer
)->midlevel_result
= SCpnt
->result
; /* Error */
283 (STp
->buffer
)->midlevel_result
= INT_MAX
; /* OK */
285 (STp
->buffer
)->midlevel_result
= SCpnt
->result
;
286 SCpnt
->request
.rq_status
= RQ_SCSI_DONE
;
287 (STp
->buffer
)->last_SRpnt
= SCpnt
->sc_request
;
288 DEB( STp
->write_pending
= 0; )
290 up(SCpnt
->request
.sem
);
294 printk(KERN_ERR
"st?: Illegal interrupt device %x\n", st_nbr
);
299 /* Do the scsi command. Waits until command performed if do_wait is true.
300 Otherwise write_behind_check() is used to check that the command
302 static Scsi_Request
*
303 st_do_scsi(Scsi_Request
* SRpnt
, Scsi_Tape
* STp
, unsigned char *cmd
, int bytes
,
304 int direction
, int timeout
, int retries
, int do_wait
)
309 SRpnt
= scsi_allocate_request(STp
->device
);
311 DEBC( printk(KERN_ERR
"st%d: Can't get SCSI request.\n",
312 TAPE_NR(STp
->devt
)); );
313 if (signal_pending(current
))
314 (STp
->buffer
)->syscall_result
= (-EINTR
);
316 (STp
->buffer
)->syscall_result
= (-EBUSY
);
321 cmd
[1] |= (SRpnt
->sr_device
->lun
<< 5) & 0xe0;
322 init_MUTEX_LOCKED(&STp
->sem
);
323 SRpnt
->sr_use_sg
= (bytes
> (STp
->buffer
)->sg
[0].length
) ?
324 (STp
->buffer
)->use_sg
: 0;
325 if (SRpnt
->sr_use_sg
) {
326 bp
= (char *) &((STp
->buffer
)->sg
[0]);
327 if ((STp
->buffer
)->sg_segs
< SRpnt
->sr_use_sg
)
328 SRpnt
->sr_use_sg
= (STp
->buffer
)->sg_segs
;
330 bp
= (STp
->buffer
)->b_data
;
331 SRpnt
->sr_data_direction
= direction
;
332 SRpnt
->sr_cmd_len
= 0;
333 SRpnt
->sr_request
.sem
= &(STp
->sem
);
334 SRpnt
->sr_request
.rq_status
= RQ_SCSI_BUSY
;
335 SRpnt
->sr_request
.rq_dev
= STp
->devt
;
337 scsi_do_req(SRpnt
, (void *) cmd
, bp
, bytes
,
338 st_sleep_done
, timeout
, retries
);
341 down(SRpnt
->sr_request
.sem
);
342 SRpnt
->sr_request
.sem
= NULL
;
343 (STp
->buffer
)->syscall_result
= st_chk_result(STp
, SRpnt
);
349 /* Handle the write-behind checking (downs the semaphore) */
350 static void write_behind_check(Scsi_Tape
* STp
)
355 STbuffer
= STp
->buffer
;
358 if (STp
->write_pending
)
365 (STp
->buffer
)->last_SRpnt
->sr_request
.sem
= NULL
;
367 (STp
->buffer
)->syscall_result
= st_chk_result(STp
, (STp
->buffer
)->last_SRpnt
);
368 scsi_release_request((STp
->buffer
)->last_SRpnt
);
370 if (STbuffer
->writing
< STbuffer
->buffer_bytes
)
372 memcpy(STbuffer
->b_data
,
373 STbuffer
->b_data
+ STbuffer
->writing
,
374 STbuffer
->buffer_bytes
- STbuffer
->writing
);
377 "st: write_behind_check: something left in buffer!\n");
379 STbuffer
->buffer_bytes
-= STbuffer
->writing
;
380 STps
= &(STp
->ps
[STp
->partition
]);
381 if (STps
->drv_block
>= 0) {
382 if (STp
->block_size
== 0)
385 STps
->drv_block
+= STbuffer
->writing
/ STp
->block_size
;
387 STbuffer
->writing
= 0;
393 /* Step over EOF if it has been inadvertently crossed (ioctl not used because
394 it messes up the block number). */
395 static int cross_eof(Scsi_Tape
* STp
, int forward
)
398 unsigned char cmd
[MAX_COMMAND_SIZE
];
401 cmd
[1] = 0x01; /* Space FileMarks */
406 cmd
[2] = cmd
[3] = cmd
[4] = 0xff; /* -1 filemarks */
409 DEBC(printk(ST_DEB_MSG
"st%d: Stepping over filemark %s.\n",
410 TAPE_NR(STp
->devt
), forward
? "forward" : "backward"));
412 SRpnt
= st_do_scsi(NULL
, STp
, cmd
, 0, SCSI_DATA_NONE
,
413 STp
->timeout
, MAX_RETRIES
, TRUE
);
415 return (STp
->buffer
)->syscall_result
;
417 scsi_release_request(SRpnt
);
420 if ((STp
->buffer
)->midlevel_result
!= 0)
421 printk(KERN_ERR
"st%d: Stepping over filemark %s failed.\n",
422 TAPE_NR(STp
->devt
), forward
? "forward" : "backward");
424 return (STp
->buffer
)->syscall_result
;
428 /* Flush the write buffer (never need to write if variable blocksize). */
429 static int flush_write_buffer(Scsi_Tape
* STp
)
431 int offset
, transfer
, blks
;
433 unsigned char cmd
[MAX_COMMAND_SIZE
];
437 if ((STp
->buffer
)->writing
) {
438 write_behind_check(STp
);
439 if ((STp
->buffer
)->syscall_result
) {
440 DEBC(printk(ST_DEB_MSG
441 "st%d: Async write error (flush) %x.\n",
442 TAPE_NR(STp
->devt
), (STp
->buffer
)->midlevel_result
))
443 if ((STp
->buffer
)->midlevel_result
== INT_MAX
)
448 if (STp
->block_size
== 0)
452 if (STp
->dirty
== 1) {
454 offset
= (STp
->buffer
)->buffer_bytes
;
455 transfer
= ((offset
+ STp
->block_size
- 1) /
456 STp
->block_size
) * STp
->block_size
;
457 DEBC(printk(ST_DEB_MSG
"st%d: Flushing %d bytes.\n",
458 TAPE_NR(STp
->devt
), transfer
));
460 memset((STp
->buffer
)->b_data
+ offset
, 0, transfer
- offset
);
462 memset(cmd
, 0, MAX_COMMAND_SIZE
);
465 blks
= transfer
/ STp
->block_size
;
470 SRpnt
= st_do_scsi(NULL
, STp
, cmd
, transfer
, SCSI_DATA_WRITE
,
471 STp
->timeout
, MAX_WRITE_RETRIES
, TRUE
);
473 return (STp
->buffer
)->syscall_result
;
475 STps
= &(STp
->ps
[STp
->partition
]);
476 if ((STp
->buffer
)->syscall_result
!= 0) {
477 if ((SRpnt
->sr_sense_buffer
[0] & 0x70) == 0x70 &&
478 (SRpnt
->sr_sense_buffer
[2] & 0x40) &&
479 (SRpnt
->sr_sense_buffer
[2] & 0x0f) == NO_SENSE
) {
481 (STp
->buffer
)->buffer_bytes
= 0;
484 printk(KERN_ERR
"st%d: Error on flush.\n",
488 STps
->drv_block
= (-1);
490 if (STps
->drv_block
>= 0)
491 STps
->drv_block
+= blks
;
493 (STp
->buffer
)->buffer_bytes
= 0;
495 scsi_release_request(SRpnt
);
502 /* Flush the tape buffer. The tape will be positioned correctly unless
503 seek_next is true. */
504 static int flush_buffer(Scsi_Tape
*STp
, int seek_next
)
506 int backspace
, result
;
510 STbuffer
= STp
->buffer
;
513 * If there was a bus reset, block further access
516 if (STp
->device
->was_reset
)
519 if (STp
->ready
!= ST_READY
)
522 STps
= &(STp
->ps
[STp
->partition
]);
523 if (STps
->rw
== ST_WRITING
) /* Writing */
524 return flush_write_buffer(STp
);
526 if (STp
->block_size
== 0)
529 backspace
= ((STp
->buffer
)->buffer_bytes
+
530 (STp
->buffer
)->read_pointer
) / STp
->block_size
-
531 ((STp
->buffer
)->read_pointer
+ STp
->block_size
- 1) /
533 (STp
->buffer
)->buffer_bytes
= 0;
534 (STp
->buffer
)->read_pointer
= 0;
537 if (STps
->eof
== ST_FM_HIT
) {
538 result
= cross_eof(STp
, FALSE
); /* Back over the EOF hit */
540 STps
->eof
= ST_NOEOF
;
542 if (STps
->drv_file
>= 0)
547 if (!result
&& backspace
> 0)
548 result
= st_int_ioctl(STp
, MTBSR
, backspace
);
549 } else if (STps
->eof
== ST_FM_HIT
) {
550 if (STps
->drv_file
>= 0)
553 STps
->eof
= ST_NOEOF
;
559 /* Set the mode parameters */
560 static int set_mode_densblk(Scsi_Tape
* STp
, ST_mode
* STm
)
564 int dev
= TAPE_NR(STp
->devt
);
566 if (!STp
->density_changed
&&
567 STm
->default_density
>= 0 &&
568 STm
->default_density
!= STp
->density
) {
569 arg
= STm
->default_density
;
573 arg
<<= MT_ST_DENSITY_SHIFT
;
574 if (!STp
->blksize_changed
&&
575 STm
->default_blksize
>= 0 &&
576 STm
->default_blksize
!= STp
->block_size
) {
577 arg
|= STm
->default_blksize
;
580 arg
|= STp
->block_size
;
582 st_int_ioctl(STp
, SET_DENS_AND_BLK
, arg
)) {
584 "st%d: Can't set default block size to %d bytes and density %x.\n",
585 dev
, STm
->default_blksize
, STm
->default_density
);
593 /* Open the device. Needs to be called with BKL only because of incrementing the SCSI host
595 static int st_open(struct inode
*inode
, struct file
*filp
)
597 unsigned short st_flags
;
598 int i
, need_dma_buffer
, new_session
= FALSE
;
600 unsigned char cmd
[MAX_COMMAND_SIZE
];
605 int dev
= TAPE_NR(inode
->i_rdev
);
606 int mode
= TAPE_MODE(inode
->i_rdev
);
609 write_lock_irqsave(&st_dev_arr_lock
, flags
);
610 STp
= scsi_tapes
[dev
];
611 if (dev
>= st_template
.dev_max
|| STp
== NULL
) {
612 write_unlock_irqrestore(&st_dev_arr_lock
, flags
);
617 write_unlock_irqrestore(&st_dev_arr_lock
, flags
);
618 DEB( printk(ST_DEB_MSG
"st%d: Device already in use.\n", dev
); )
622 write_unlock_irqrestore(&st_dev_arr_lock
, flags
);
623 STp
->rew_at_close
= STp
->autorew_dev
= (MINOR(inode
->i_rdev
) & 0x80) == 0;
625 if (STp
->device
->host
->hostt
->module
)
626 __MOD_INC_USE_COUNT(STp
->device
->host
->hostt
->module
);
628 if (!scsi_block_when_processing_errors(STp
->device
)) {
633 if (mode
!= STp
->current_mode
) {
634 DEBC(printk(ST_DEB_MSG
"st%d: Mode change from %d to %d.\n",
635 dev
, STp
->current_mode
, mode
));
637 STp
->current_mode
= mode
;
639 STm
= &(STp
->modes
[STp
->current_mode
]);
641 /* Allocate a buffer for this user */
642 need_dma_buffer
= STp
->restr_dma
;
643 write_lock_irqsave(&st_dev_arr_lock
, flags
);
644 for (i
= 0; i
< st_nbr_buffers
; i
++)
645 if (!st_buffers
[i
]->in_use
&&
646 (!need_dma_buffer
|| st_buffers
[i
]->dma
)) {
647 STp
->buffer
= st_buffers
[i
];
648 (STp
->buffer
)->in_use
= 1;
651 write_unlock_irqrestore(&st_dev_arr_lock
, flags
);
652 if (i
>= st_nbr_buffers
) {
653 STp
->buffer
= new_tape_buffer(FALSE
, need_dma_buffer
, TRUE
);
654 if (STp
->buffer
== NULL
) {
655 printk(KERN_WARNING
"st%d: Can't allocate tape buffer.\n", dev
);
661 (STp
->buffer
)->writing
= 0;
662 (STp
->buffer
)->syscall_result
= 0;
663 (STp
->buffer
)->use_sg
= STp
->device
->host
->sg_tablesize
;
665 /* Compute the usable buffer size for this SCSI adapter */
666 if (!(STp
->buffer
)->use_sg
)
667 (STp
->buffer
)->buffer_size
= (STp
->buffer
)->sg
[0].length
;
669 for (i
= 0, (STp
->buffer
)->buffer_size
= 0; i
< (STp
->buffer
)->use_sg
&&
670 i
< (STp
->buffer
)->sg_segs
; i
++)
671 (STp
->buffer
)->buffer_size
+= (STp
->buffer
)->sg
[i
].length
;
674 st_flags
= filp
->f_flags
;
675 STp
->write_prot
= ((st_flags
& O_ACCMODE
) == O_RDONLY
);
678 for (i
= 0; i
< ST_NBR_PARTITIONS
; i
++) {
679 STps
= &(STp
->ps
[i
]);
682 STp
->ready
= ST_READY
;
683 STp
->recover_count
= 0;
684 DEB( STp
->nbr_waits
= STp
->nbr_finished
= 0; )
686 memset((void *) &cmd
[0], 0, MAX_COMMAND_SIZE
);
687 cmd
[0] = TEST_UNIT_READY
;
689 SRpnt
= st_do_scsi(NULL
, STp
, cmd
, 0, SCSI_DATA_NONE
, STp
->long_timeout
,
690 MAX_READY_RETRIES
, TRUE
);
692 retval
= (STp
->buffer
)->syscall_result
;
696 if ((SRpnt
->sr_sense_buffer
[0] & 0x70) == 0x70 &&
697 (SRpnt
->sr_sense_buffer
[2] & 0x0f) == UNIT_ATTENTION
) { /* New media? */
699 /* Flush the queued UNIT ATTENTION sense data */
700 for (i
=0; i
< 10; i
++) {
701 memset((void *) &cmd
[0], 0, MAX_COMMAND_SIZE
);
702 cmd
[0] = TEST_UNIT_READY
;
703 SRpnt
= st_do_scsi(SRpnt
, STp
, cmd
, 0, SCSI_DATA_NONE
,
704 STp
->long_timeout
, MAX_READY_RETRIES
, TRUE
);
705 if ((SRpnt
->sr_sense_buffer
[0] & 0x70) != 0x70 ||
706 (SRpnt
->sr_sense_buffer
[2] & 0x0f) != UNIT_ATTENTION
)
710 (STp
->device
)->was_reset
= 0;
711 STp
->partition
= STp
->new_partition
= 0;
712 if (STp
->can_partitions
)
713 STp
->nbr_partitions
= 1; /* This guess will be updated later
715 for (i
= 0; i
< ST_NBR_PARTITIONS
; i
++) {
716 STps
= &(STp
->ps
[i
]);
718 STps
->eof
= ST_NOEOF
;
720 STps
->last_block_valid
= FALSE
;
727 if ((STp
->buffer
)->syscall_result
!= 0) {
728 if ((STp
->device
)->scsi_level
>= SCSI_2
&&
729 (SRpnt
->sr_sense_buffer
[0] & 0x70) == 0x70 &&
730 (SRpnt
->sr_sense_buffer
[2] & 0x0f) == NOT_READY
&&
731 SRpnt
->sr_sense_buffer
[12] == 0x3a) { /* Check ASC */
732 STp
->ready
= ST_NO_TAPE
;
734 STp
->ready
= ST_NOT_READY
;
735 scsi_release_request(SRpnt
);
737 STp
->density
= 0; /* Clear the erroneous "residue" */
740 STp
->ps
[0].drv_file
= STp
->ps
[0].drv_block
= (-1);
741 STp
->partition
= STp
->new_partition
= 0;
742 STp
->door_locked
= ST_UNLOCKED
;
746 if (STp
->omit_blklims
)
747 STp
->min_block
= STp
->max_block
= (-1);
749 memset((void *) &cmd
[0], 0, MAX_COMMAND_SIZE
);
750 cmd
[0] = READ_BLOCK_LIMITS
;
752 SRpnt
= st_do_scsi(SRpnt
, STp
, cmd
, 6, SCSI_DATA_READ
, STp
->timeout
,
753 MAX_READY_RETRIES
, TRUE
);
755 if (!SRpnt
->sr_result
&& !SRpnt
->sr_sense_buffer
[0]) {
756 STp
->max_block
= ((STp
->buffer
)->b_data
[1] << 16) |
757 ((STp
->buffer
)->b_data
[2] << 8) | (STp
->buffer
)->b_data
[3];
758 STp
->min_block
= ((STp
->buffer
)->b_data
[4] << 8) |
759 (STp
->buffer
)->b_data
[5];
760 if ( DEB( debugging
|| ) !STp
->inited
)
762 "st%d: Block limits %d - %d bytes.\n", dev
,
763 STp
->min_block
, STp
->max_block
);
765 STp
->min_block
= STp
->max_block
= (-1);
766 DEBC(printk(ST_DEB_MSG
"st%d: Can't read block limits.\n",
771 memset((void *) &cmd
[0], 0, MAX_COMMAND_SIZE
);
775 SRpnt
= st_do_scsi(SRpnt
, STp
, cmd
, 12, SCSI_DATA_READ
, STp
->timeout
,
776 MAX_READY_RETRIES
, TRUE
);
778 if ((STp
->buffer
)->syscall_result
!= 0) {
779 DEBC(printk(ST_DEB_MSG
"st%d: No Mode Sense.\n", dev
));
780 STp
->block_size
= ST_DEFAULT_BLOCK
; /* Educated guess (?) */
781 (STp
->buffer
)->syscall_result
= 0; /* Prevent error propagation */
782 STp
->drv_write_prot
= 0;
784 DEBC(printk(ST_DEB_MSG
785 "st%d: Mode sense. Length %d, medium %x, WBS %x, BLL %d\n",
787 (STp
->buffer
)->b_data
[0], (STp
->buffer
)->b_data
[1],
788 (STp
->buffer
)->b_data
[2], (STp
->buffer
)->b_data
[3]));
790 if ((STp
->buffer
)->b_data
[3] >= 8) {
791 STp
->drv_buffer
= ((STp
->buffer
)->b_data
[2] >> 4) & 7;
792 STp
->density
= (STp
->buffer
)->b_data
[4];
793 STp
->block_size
= (STp
->buffer
)->b_data
[9] * 65536 +
794 (STp
->buffer
)->b_data
[10] * 256 + (STp
->buffer
)->b_data
[11];
795 DEBC(printk(ST_DEB_MSG
796 "st%d: Density %x, tape length: %x, drv buffer: %d\n",
797 dev
, STp
->density
, (STp
->buffer
)->b_data
[5] * 65536 +
798 (STp
->buffer
)->b_data
[6] * 256 + (STp
->buffer
)->b_data
[7],
801 STp
->drv_write_prot
= ((STp
->buffer
)->b_data
[2] & 0x80) != 0;
803 scsi_release_request(SRpnt
);
807 if (STp
->block_size
> 0)
808 (STp
->buffer
)->buffer_blocks
=
809 (STp
->buffer
)->buffer_size
/ STp
->block_size
;
811 (STp
->buffer
)->buffer_blocks
= 1;
812 (STp
->buffer
)->buffer_bytes
= (STp
->buffer
)->read_pointer
= 0;
814 DEBC(printk(ST_DEB_MSG
815 "st%d: Block size: %d, buffer size: %d (%d blocks).\n", dev
,
816 STp
->block_size
, (STp
->buffer
)->buffer_size
,
817 (STp
->buffer
)->buffer_blocks
));
819 if (STp
->drv_write_prot
) {
822 DEBC(printk(ST_DEB_MSG
"st%d: Write protected\n", dev
));
824 if ((st_flags
& O_ACCMODE
) == O_WRONLY
|| (st_flags
& O_ACCMODE
) == O_RDWR
) {
830 if (STp
->can_partitions
&& STp
->nbr_partitions
< 1) {
831 /* This code is reached when the device is opened for the first time
832 after the driver has been initialized with tape in the drive and the
833 partition support has been enabled. */
834 DEBC(printk(ST_DEB_MSG
835 "st%d: Updating partition number in status.\n", dev
));
836 if ((STp
->partition
= find_partition(STp
)) < 0) {
837 retval
= STp
->partition
;
840 STp
->new_partition
= STp
->partition
;
841 STp
->nbr_partitions
= 1; /* This guess will be updated when necessary */
844 if (new_session
) { /* Change the drive parameters for the new mode */
845 STp
->density_changed
= STp
->blksize_changed
= FALSE
;
846 STp
->compression_changed
= FALSE
;
847 if (!(STm
->defaults_for_writes
) &&
848 (retval
= set_mode_densblk(STp
, STm
)) < 0)
851 if (STp
->default_drvbuffer
!= 0xff) {
852 if (st_int_ioctl(STp
, MTSETDRVBUFFER
, STp
->default_drvbuffer
))
854 "st%d: Can't set default drive buffering to %d.\n",
855 dev
, STp
->default_drvbuffer
);
862 if (STp
->buffer
!= NULL
) {
863 (STp
->buffer
)->in_use
= 0;
867 if (STp
->device
->host
->hostt
->module
)
868 __MOD_DEC_USE_COUNT(STp
->device
->host
->hostt
->module
);
874 /* Flush the tape buffer before close */
875 static int st_flush(struct file
*filp
)
877 int result
= 0, result2
;
878 unsigned char cmd
[MAX_COMMAND_SIZE
];
884 struct inode
*inode
= filp
->f_dentry
->d_inode
;
885 kdev_t devt
= inode
->i_rdev
;
888 if (file_count(filp
) > 1)
892 read_lock(&st_dev_arr_lock
);
893 STp
= scsi_tapes
[dev
];
894 read_unlock(&st_dev_arr_lock
);
895 STm
= &(STp
->modes
[STp
->current_mode
]);
896 STps
= &(STp
->ps
[STp
->partition
]);
898 if (STps
->rw
== ST_WRITING
&& !(STp
->device
)->was_reset
) {
899 result
= flush_write_buffer(STp
);
900 if (result
!= 0 && result
!= (-ENOSPC
))
904 if (STp
->can_partitions
&&
905 (result2
= update_partition(STp
)) < 0) {
906 DEBC(printk(ST_DEB_MSG
907 "st%d: update_partition at close failed.\n", dev
));
913 if (STps
->rw
== ST_WRITING
&& !(STp
->device
)->was_reset
) {
915 DEBC(printk(ST_DEB_MSG
"st%d: File length %ld bytes.\n",
916 dev
, (long) (filp
->f_pos
));
917 printk(ST_DEB_MSG
"st%d: Async write waits %d, finished %d.\n",
918 dev
, STp
->nbr_waits
, STp
->nbr_finished
);
921 memset(cmd
, 0, MAX_COMMAND_SIZE
);
922 cmd
[0] = WRITE_FILEMARKS
;
923 cmd
[4] = 1 + STp
->two_fm
;
925 SRpnt
= st_do_scsi(NULL
, STp
, cmd
, 0, SCSI_DATA_NONE
,
926 STp
->timeout
, MAX_WRITE_RETRIES
, TRUE
);
928 result
= (STp
->buffer
)->syscall_result
;
932 if ((STp
->buffer
)->syscall_result
!= 0 &&
933 ((SRpnt
->sr_sense_buffer
[0] & 0x70) != 0x70 ||
934 (SRpnt
->sr_sense_buffer
[2] & 0x4f) != 0x40 ||
935 ((SRpnt
->sr_sense_buffer
[0] & 0x80) != 0 &&
936 (SRpnt
->sr_sense_buffer
[3] | SRpnt
->sr_sense_buffer
[4] |
937 SRpnt
->sr_sense_buffer
[5] |
938 SRpnt
->sr_sense_buffer
[6]) == 0))) {
939 /* Filter out successful write at EOM */
940 scsi_release_request(SRpnt
);
942 printk(KERN_ERR
"st%d: Error on write filemark.\n", dev
);
946 scsi_release_request(SRpnt
);
948 if (STps
->drv_file
>= 0)
952 cross_eof(STp
, FALSE
);
956 DEBC(printk(ST_DEB_MSG
"st%d: Buffer flushed, %d EOF(s) written\n",
958 } else if (!STp
->rew_at_close
) {
959 STps
= &(STp
->ps
[STp
->partition
]);
960 if (!STm
->sysv
|| STps
->rw
!= ST_READING
) {
962 result
= flush_buffer(STp
, 0);
963 else if (STps
->eof
== ST_FM_HIT
) {
964 result
= cross_eof(STp
, FALSE
);
966 if (STps
->drv_file
>= 0)
971 STps
->eof
= ST_NOEOF
;
973 } else if ((STps
->eof
== ST_NOEOF
&&
974 !(result
= cross_eof(STp
, TRUE
))) ||
975 STps
->eof
== ST_FM_HIT
) {
976 if (STps
->drv_file
>= 0)
984 if (STp
->rew_at_close
) {
985 result2
= st_int_ioctl(STp
, MTREW
, 1);
993 /* Close the device and release it. BKL is not needed: this is the only thread
994 accessing this tape. */
995 static int st_release(struct inode
*inode
, struct file
*filp
)
1001 kdev_t devt
= inode
->i_rdev
;
1004 dev
= TAPE_NR(devt
);
1005 read_lock(&st_dev_arr_lock
);
1006 STp
= scsi_tapes
[dev
];
1007 read_unlock(&st_dev_arr_lock
);
1009 if (STp
->door_locked
== ST_LOCKED_AUTO
)
1010 st_int_ioctl(STp
, MTUNLOCK
, 0);
1012 if (STp
->buffer
!= NULL
) {
1013 normalize_buffer(STp
->buffer
);
1014 write_lock_irqsave(&st_dev_arr_lock
, flags
);
1015 (STp
->buffer
)->in_use
= 0;
1019 write_lock_irqsave(&st_dev_arr_lock
, flags
);
1023 write_unlock_irqrestore(&st_dev_arr_lock
, flags
);
1024 if (STp
->device
->host
->hostt
->module
)
1025 __MOD_DEC_USE_COUNT(STp
->device
->host
->hostt
->module
);
1033 st_write(struct file
*filp
, const char *buf
, size_t count
, loff_t
* ppos
)
1035 struct inode
*inode
= filp
->f_dentry
->d_inode
;
1037 ssize_t i
, do_count
, blks
, transfer
;
1039 int write_threshold
;
1040 int doing_write
= 0;
1041 unsigned char cmd
[MAX_COMMAND_SIZE
];
1042 const char *b_point
;
1043 Scsi_Request
*SRpnt
= NULL
;
1047 int dev
= TAPE_NR(inode
->i_rdev
);
1049 read_lock(&st_dev_arr_lock
);
1050 STp
= scsi_tapes
[dev
];
1051 read_unlock(&st_dev_arr_lock
);
1053 if (down_interruptible(&STp
->lock
))
1054 return -ERESTARTSYS
;
1057 * If we are in the middle of error recovery, don't let anyone
1058 * else try and use this device. Also, if error recovery fails, it
1059 * may try and take the device offline, in which case all further
1060 * access to the device is prohibited.
1062 if (!scsi_block_when_processing_errors(STp
->device
)) {
1067 if (ppos
!= &filp
->f_pos
) {
1068 /* "A request was outside the capabilities of the device." */
1073 if (STp
->ready
!= ST_READY
) {
1074 if (STp
->ready
== ST_NO_TAPE
)
1075 retval
= (-ENOMEDIUM
);
1081 STm
= &(STp
->modes
[STp
->current_mode
]);
1082 if (!STm
->defined
) {
1090 * If there was a bus reset, block further access
1093 if (STp
->device
->was_reset
) {
1100 printk(ST_DEB_MSG
"st%d: Incorrect device.\n", dev
);
1105 /* Write must be integral number of blocks */
1106 if (STp
->block_size
!= 0 && (count
% STp
->block_size
) != 0) {
1107 printk(KERN_WARNING
"st%d: Write not multiple of tape block size.\n",
1113 if (STp
->can_partitions
&&
1114 (retval
= update_partition(STp
)) < 0)
1116 STps
= &(STp
->ps
[STp
->partition
]);
1118 if (STp
->write_prot
) {
1123 if (STp
->block_size
== 0) {
1124 if (STp
->max_block
> 0 &&
1125 (count
< STp
->min_block
|| count
> STp
->max_block
)) {
1129 if (count
> (STp
->buffer
)->buffer_size
&&
1130 !enlarge_buffer(STp
->buffer
, count
, STp
->restr_dma
)) {
1131 retval
= (-EOVERFLOW
);
1135 if ((STp
->buffer
)->buffer_blocks
< 1) {
1136 /* Fixed block mode with too small buffer */
1137 if (!enlarge_buffer(STp
->buffer
, STp
->block_size
, STp
->restr_dma
)) {
1138 retval
= (-EOVERFLOW
);
1141 (STp
->buffer
)->buffer_blocks
= 1;
1144 if (STp
->do_auto_lock
&& STp
->door_locked
== ST_UNLOCKED
&&
1145 !st_int_ioctl(STp
, MTLOCK
, 0))
1146 STp
->door_locked
= ST_LOCKED_AUTO
;
1148 if (STps
->rw
== ST_READING
) {
1149 retval
= flush_buffer(STp
, 0);
1152 STps
->rw
= ST_WRITING
;
1153 } else if (STps
->rw
!= ST_WRITING
&&
1154 STps
->drv_file
== 0 && STps
->drv_block
== 0) {
1155 if ((retval
= set_mode_densblk(STp
, STm
)) < 0)
1157 if (STm
->default_compression
!= ST_DONT_TOUCH
&&
1158 !(STp
->compression_changed
)) {
1159 if (st_compression(STp
, (STm
->default_compression
== ST_YES
))) {
1160 printk(KERN_WARNING
"st%d: Can't set default compression.\n",
1162 if (modes_defined
) {
1170 if ((STp
->buffer
)->writing
) {
1171 write_behind_check(STp
);
1172 if ((STp
->buffer
)->syscall_result
) {
1173 DEBC(printk(ST_DEB_MSG
"st%d: Async write error (write) %x.\n",
1174 dev
, (STp
->buffer
)->midlevel_result
));
1175 if ((STp
->buffer
)->midlevel_result
== INT_MAX
)
1176 STps
->eof
= ST_EOM_OK
;
1178 STps
->eof
= ST_EOM_ERROR
;
1182 if (STps
->eof
== ST_EOM_OK
) {
1186 else if (STps
->eof
== ST_EOM_ERROR
) {
1191 /* Check the buffer readability in cases where copy_user might catch
1192 the problems after some tape movement. */
1193 if (STp
->block_size
!= 0 &&
1194 (copy_from_user(&i
, buf
, 1) != 0 ||
1195 copy_from_user(&i
, buf
+ count
- 1, 1) != 0)) {
1200 if (!STm
->do_buffer_writes
) {
1202 if (STp
->block_size
!= 0 && (count
% STp
->block_size
) != 0) {
1203 retval
= (-EINVAL
); /* Write must be integral number of blocks */
1207 write_threshold
= 1;
1209 write_threshold
= (STp
->buffer
)->buffer_blocks
* STp
->block_size
;
1210 if (!STm
->do_async_writes
)
1215 memset(cmd
, 0, MAX_COMMAND_SIZE
);
1217 cmd
[1] = (STp
->block_size
!= 0);
1219 STps
->rw
= ST_WRITING
;
1222 while ((STp
->block_size
== 0 && !STm
->do_async_writes
&& count
> 0) ||
1223 (STp
->block_size
!= 0 &&
1224 (STp
->buffer
)->buffer_bytes
+ count
> write_threshold
)) {
1226 if (STp
->block_size
== 0)
1229 do_count
= (STp
->buffer
)->buffer_blocks
* STp
->block_size
-
1230 (STp
->buffer
)->buffer_bytes
;
1231 if (do_count
> count
)
1235 i
= append_to_buffer(b_point
, STp
->buffer
, do_count
);
1241 if (STp
->block_size
== 0)
1242 blks
= transfer
= do_count
;
1244 blks
= (STp
->buffer
)->buffer_bytes
/
1246 transfer
= blks
* STp
->block_size
;
1248 cmd
[2] = blks
>> 16;
1252 SRpnt
= st_do_scsi(SRpnt
, STp
, cmd
, transfer
, SCSI_DATA_WRITE
,
1253 STp
->timeout
, MAX_WRITE_RETRIES
, TRUE
);
1255 retval
= (STp
->buffer
)->syscall_result
;
1259 if ((STp
->buffer
)->syscall_result
!= 0) {
1260 DEBC(printk(ST_DEB_MSG
"st%d: Error on write:\n", dev
));
1261 if ((SRpnt
->sr_sense_buffer
[0] & 0x70) == 0x70 &&
1262 (SRpnt
->sr_sense_buffer
[2] & 0x40)) {
1263 if (STp
->block_size
!= 0 &&
1264 (SRpnt
->sr_sense_buffer
[0] & 0x80) != 0)
1265 transfer
= (SRpnt
->sr_sense_buffer
[3] << 24) |
1266 (SRpnt
->sr_sense_buffer
[4] << 16) |
1267 (SRpnt
->sr_sense_buffer
[5] << 8) |
1268 SRpnt
->sr_sense_buffer
[6];
1269 else if (STp
->block_size
== 0 &&
1270 (SRpnt
->sr_sense_buffer
[2] & 0x0f) ==
1272 transfer
= do_count
;
1275 if (STp
->block_size
!= 0)
1276 transfer
*= STp
->block_size
;
1277 if (transfer
<= do_count
) {
1278 filp
->f_pos
+= do_count
- transfer
;
1279 count
-= do_count
- transfer
;
1280 if (STps
->drv_block
>= 0) {
1281 if (STp
->block_size
== 0 &&
1282 transfer
< do_count
)
1284 else if (STp
->block_size
!= 0)
1286 (do_count
- transfer
) /
1289 STps
->eof
= ST_EOM_OK
;
1290 retval
= (-ENOSPC
); /* EOM within current request */
1291 DEBC(printk(ST_DEB_MSG
1292 "st%d: EOM with %d bytes unwritten.\n",
1295 STps
->eof
= ST_EOM_ERROR
;
1296 STps
->drv_block
= (-1); /* Too cautious? */
1297 retval
= (-EIO
); /* EOM for old data */
1298 DEBC(printk(ST_DEB_MSG
1299 "st%d: EOM with lost data.\n",
1303 STps
->drv_block
= (-1); /* Too cautious? */
1307 scsi_release_request(SRpnt
);
1309 (STp
->buffer
)->buffer_bytes
= 0;
1312 retval
= total
- count
;
1315 filp
->f_pos
+= do_count
;
1316 b_point
+= do_count
;
1318 if (STps
->drv_block
>= 0) {
1319 if (STp
->block_size
== 0)
1322 STps
->drv_block
+= blks
;
1324 (STp
->buffer
)->buffer_bytes
= 0;
1329 i
= append_to_buffer(b_point
, STp
->buffer
, count
);
1334 filp
->f_pos
+= count
;
1338 if (doing_write
&& (STp
->buffer
)->syscall_result
!= 0) {
1339 retval
= (STp
->buffer
)->syscall_result
;
1343 if (STm
->do_async_writes
&&
1344 (((STp
->buffer
)->buffer_bytes
>= STp
->write_threshold
&&
1345 (STp
->buffer
)->buffer_bytes
>= STp
->block_size
) ||
1346 STp
->block_size
== 0)) {
1347 /* Schedule an asynchronous write */
1348 if (STp
->block_size
== 0)
1349 (STp
->buffer
)->writing
= (STp
->buffer
)->buffer_bytes
;
1351 (STp
->buffer
)->writing
= ((STp
->buffer
)->buffer_bytes
/
1352 STp
->block_size
) * STp
->block_size
;
1353 STp
->dirty
= !((STp
->buffer
)->writing
==
1354 (STp
->buffer
)->buffer_bytes
);
1356 if (STp
->block_size
== 0)
1357 blks
= (STp
->buffer
)->writing
;
1359 blks
= (STp
->buffer
)->writing
/ STp
->block_size
;
1360 cmd
[2] = blks
>> 16;
1363 DEB( STp
->write_pending
= 1; )
1365 SRpnt
= st_do_scsi(SRpnt
, STp
, cmd
, (STp
->buffer
)->writing
,
1366 SCSI_DATA_WRITE
, STp
->timeout
,
1367 MAX_WRITE_RETRIES
, FALSE
);
1368 if (SRpnt
== NULL
) {
1369 retval
= (STp
->buffer
)->syscall_result
;
1372 SRpnt
= NULL
; /* Prevent releasing this request! */
1375 STps
->at_sm
&= (total
== 0);
1377 STps
->eof
= ST_NOEOF
;
1382 scsi_release_request(SRpnt
);
1388 /* Read data from the tape. Returns zero in the normal case, one if the
1389 eof status has changed, and the negative error code in case of a
1390 fatal error. Otherwise updates the buffer and the eof state. */
1391 static long read_tape(Scsi_Tape
*STp
, long count
, Scsi_Request
** aSRpnt
)
1393 int transfer
, blks
, bytes
;
1394 unsigned char cmd
[MAX_COMMAND_SIZE
];
1395 Scsi_Request
*SRpnt
;
1398 int dev
= TAPE_NR(STp
->devt
);
1404 STm
= &(STp
->modes
[STp
->current_mode
]);
1405 STps
= &(STp
->ps
[STp
->partition
]);
1406 if (STps
->eof
== ST_FM_HIT
)
1409 memset(cmd
, 0, MAX_COMMAND_SIZE
);
1411 cmd
[1] = (STp
->block_size
!= 0);
1412 if (STp
->block_size
== 0)
1413 blks
= bytes
= count
;
1415 if (STm
->do_read_ahead
) {
1416 blks
= (STp
->buffer
)->buffer_blocks
;
1417 bytes
= blks
* STp
->block_size
;
1420 if (bytes
> (STp
->buffer
)->buffer_size
)
1421 bytes
= (STp
->buffer
)->buffer_size
;
1422 blks
= bytes
/ STp
->block_size
;
1423 bytes
= blks
* STp
->block_size
;
1426 cmd
[2] = blks
>> 16;
1431 SRpnt
= st_do_scsi(SRpnt
, STp
, cmd
, bytes
, SCSI_DATA_READ
,
1432 STp
->timeout
, MAX_RETRIES
, TRUE
);
1435 return (STp
->buffer
)->syscall_result
;
1437 (STp
->buffer
)->read_pointer
= 0;
1440 /* Something to check */
1441 if ((STp
->buffer
)->syscall_result
) {
1443 DEBC(printk(ST_DEB_MSG
"st%d: Sense: %2x %2x %2x %2x %2x %2x %2x %2x\n",
1445 SRpnt
->sr_sense_buffer
[0], SRpnt
->sr_sense_buffer
[1],
1446 SRpnt
->sr_sense_buffer
[2], SRpnt
->sr_sense_buffer
[3],
1447 SRpnt
->sr_sense_buffer
[4], SRpnt
->sr_sense_buffer
[5],
1448 SRpnt
->sr_sense_buffer
[6], SRpnt
->sr_sense_buffer
[7]));
1449 if ((SRpnt
->sr_sense_buffer
[0] & 0x70) == 0x70) { /* extended sense */
1451 if ((SRpnt
->sr_sense_buffer
[2] & 0x0f) == BLANK_CHECK
)
1452 SRpnt
->sr_sense_buffer
[2] &= 0xcf; /* No need for EOM in this case */
1454 if ((SRpnt
->sr_sense_buffer
[2] & 0xe0) != 0) { /* EOF, EOM, or ILI */
1455 /* Compute the residual count */
1456 if ((SRpnt
->sr_sense_buffer
[0] & 0x80) != 0)
1457 transfer
= (SRpnt
->sr_sense_buffer
[3] << 24) |
1458 (SRpnt
->sr_sense_buffer
[4] << 16) |
1459 (SRpnt
->sr_sense_buffer
[5] << 8) |
1460 SRpnt
->sr_sense_buffer
[6];
1463 if (STp
->block_size
== 0 &&
1464 (SRpnt
->sr_sense_buffer
[2] & 0x0f) == MEDIUM_ERROR
)
1467 if (SRpnt
->sr_sense_buffer
[2] & 0x20) { /* ILI */
1468 if (STp
->block_size
== 0) {
1470 if (STps
->drv_block
>= 0)
1471 STps
->drv_block
+= 1;
1474 (STp
->buffer
)->buffer_bytes
= bytes
- transfer
;
1476 scsi_release_request(SRpnt
);
1477 SRpnt
= *aSRpnt
= NULL
;
1478 if (transfer
== blks
) { /* We did not get anything, error */
1479 printk(KERN_NOTICE
"st%d: Incorrect block size.\n", dev
);
1480 if (STps
->drv_block
>= 0)
1481 STps
->drv_block
+= blks
- transfer
+ 1;
1482 st_int_ioctl(STp
, MTBSR
, 1);
1485 /* We have some data, deliver it */
1486 (STp
->buffer
)->buffer_bytes
= (blks
- transfer
) *
1488 DEBC(printk(ST_DEB_MSG
1489 "st%d: ILI but enough data received %ld %d.\n",
1490 dev
, count
, (STp
->buffer
)->buffer_bytes
));
1491 if (STps
->drv_block
>= 0)
1492 STps
->drv_block
+= 1;
1493 if (st_int_ioctl(STp
, MTBSR
, 1))
1496 } else if (SRpnt
->sr_sense_buffer
[2] & 0x80) { /* FM overrides EOM */
1497 if (STps
->eof
!= ST_FM_HIT
)
1498 STps
->eof
= ST_FM_HIT
;
1500 STps
->eof
= ST_EOD_2
;
1501 if (STp
->block_size
== 0)
1502 (STp
->buffer
)->buffer_bytes
= 0;
1504 (STp
->buffer
)->buffer_bytes
=
1505 bytes
- transfer
* STp
->block_size
;
1506 DEBC(printk(ST_DEB_MSG
1507 "st%d: EOF detected (%d bytes read).\n",
1508 dev
, (STp
->buffer
)->buffer_bytes
));
1509 } else if (SRpnt
->sr_sense_buffer
[2] & 0x40) {
1510 if (STps
->eof
== ST_FM
)
1511 STps
->eof
= ST_EOD_1
;
1513 STps
->eof
= ST_EOM_OK
;
1514 if (STp
->block_size
== 0)
1515 (STp
->buffer
)->buffer_bytes
= bytes
- transfer
;
1517 (STp
->buffer
)->buffer_bytes
=
1518 bytes
- transfer
* STp
->block_size
;
1520 DEBC(printk(ST_DEB_MSG
"st%d: EOM detected (%d bytes read).\n",
1521 dev
, (STp
->buffer
)->buffer_bytes
));
1524 /* end of EOF, EOM, ILI test */
1525 else { /* nonzero sense key */
1526 DEBC(printk(ST_DEB_MSG
1527 "st%d: Tape error while reading.\n", dev
));
1528 STps
->drv_block
= (-1);
1529 if (STps
->eof
== ST_FM
&&
1530 (SRpnt
->sr_sense_buffer
[2] & 0x0f) == BLANK_CHECK
) {
1531 DEBC(printk(ST_DEB_MSG
1532 "st%d: Zero returned for first BLANK CHECK after EOF.\n",
1534 STps
->eof
= ST_EOD_2
; /* First BLANK_CHECK after FM */
1535 } else /* Some other extended sense code */
1539 /* End of extended sense test */
1540 else { /* Non-extended sense */
1541 retval
= (STp
->buffer
)->syscall_result
;
1545 /* End of error handling */
1546 else /* Read successful */
1547 (STp
->buffer
)->buffer_bytes
= bytes
;
1549 if (STps
->drv_block
>= 0) {
1550 if (STp
->block_size
== 0)
1553 STps
->drv_block
+= (STp
->buffer
)->buffer_bytes
/ STp
->block_size
;
1561 st_read(struct file
*filp
, char *buf
, size_t count
, loff_t
* ppos
)
1563 struct inode
*inode
= filp
->f_dentry
->d_inode
;
1566 ssize_t i
, transfer
;
1568 Scsi_Request
*SRpnt
= NULL
;
1572 int dev
= TAPE_NR(inode
->i_rdev
);
1574 read_lock(&st_dev_arr_lock
);
1575 STp
= scsi_tapes
[dev
];
1576 read_unlock(&st_dev_arr_lock
);
1578 if (down_interruptible(&STp
->lock
))
1579 return -ERESTARTSYS
;
1582 * If we are in the middle of error recovery, don't let anyone
1583 * else try and use this device. Also, if error recovery fails, it
1584 * may try and take the device offline, in which case all further
1585 * access to the device is prohibited.
1587 if (!scsi_block_when_processing_errors(STp
->device
)) {
1592 if (ppos
!= &filp
->f_pos
) {
1593 /* "A request was outside the capabilities of the device." */
1598 if (STp
->ready
!= ST_READY
) {
1599 if (STp
->ready
== ST_NO_TAPE
)
1600 retval
= (-ENOMEDIUM
);
1605 STm
= &(STp
->modes
[STp
->current_mode
]);
1606 if (!STm
->defined
) {
1612 printk(ST_DEB_MSG
"st%d: Incorrect device.\n", dev
);
1617 if (STp
->can_partitions
&&
1618 (retval
= update_partition(STp
)) < 0)
1621 if (STp
->block_size
== 0) {
1622 if (STp
->max_block
> 0 &&
1623 (count
< STp
->min_block
|| count
> STp
->max_block
)) {
1627 if (count
> (STp
->buffer
)->buffer_size
&&
1628 !enlarge_buffer(STp
->buffer
, count
, STp
->restr_dma
)) {
1629 retval
= (-EOVERFLOW
);
1633 if ((STp
->buffer
)->buffer_blocks
< 1) {
1634 /* Fixed block mode with too small buffer */
1635 if (!enlarge_buffer(STp
->buffer
, STp
->block_size
, STp
->restr_dma
)) {
1636 retval
= (-EOVERFLOW
);
1639 (STp
->buffer
)->buffer_blocks
= 1;
1642 if (!(STm
->do_read_ahead
) && STp
->block_size
!= 0 &&
1643 (count
% STp
->block_size
) != 0) {
1644 retval
= (-EINVAL
); /* Read must be integral number of blocks */
1648 if (STp
->do_auto_lock
&& STp
->door_locked
== ST_UNLOCKED
&&
1649 !st_int_ioctl(STp
, MTLOCK
, 0))
1650 STp
->door_locked
= ST_LOCKED_AUTO
;
1652 STps
= &(STp
->ps
[STp
->partition
]);
1653 if (STps
->rw
== ST_WRITING
) {
1654 retval
= flush_buffer(STp
, 0);
1657 STps
->rw
= ST_READING
;
1660 if (debugging
&& STps
->eof
!= ST_NOEOF
)
1661 printk(ST_DEB_MSG
"st%d: EOF/EOM flag up (%d). Bytes %d\n", dev
,
1662 STps
->eof
, (STp
->buffer
)->buffer_bytes
);
1665 if ((STp
->buffer
)->buffer_bytes
== 0 &&
1666 STps
->eof
>= ST_EOD_1
) {
1667 if (STps
->eof
< ST_EOD
) {
1672 retval
= (-EIO
); /* EOM or Blank Check */
1676 /* Check the buffer writability before any tape movement. Don't alter
1678 if (copy_from_user(&i
, buf
, 1) != 0 ||
1679 copy_to_user(buf
, &i
, 1) != 0 ||
1680 copy_from_user(&i
, buf
+ count
- 1, 1) != 0 ||
1681 copy_to_user(buf
+ count
- 1, &i
, 1) != 0) {
1686 STps
->rw
= ST_READING
;
1689 /* Loop until enough data in buffer or a special condition found */
1690 for (total
= 0, special
= 0; total
< count
&& !special
;) {
1692 /* Get new data if the buffer is empty */
1693 if ((STp
->buffer
)->buffer_bytes
== 0) {
1694 special
= read_tape(STp
, count
- total
, &SRpnt
);
1695 if (special
< 0) { /* No need to continue read */
1701 /* Move the data from driver buffer to user buffer */
1702 if ((STp
->buffer
)->buffer_bytes
> 0) {
1704 if (debugging
&& STps
->eof
!= ST_NOEOF
)
1706 "st%d: EOF up (%d). Left %d, needed %d.\n", dev
,
1707 STps
->eof
, (STp
->buffer
)->buffer_bytes
,
1710 transfer
= (STp
->buffer
)->buffer_bytes
< count
- total
?
1711 (STp
->buffer
)->buffer_bytes
: count
- total
;
1712 i
= from_buffer(STp
->buffer
, buf
, transfer
);
1717 filp
->f_pos
+= transfer
;
1722 if (STp
->block_size
== 0)
1723 break; /* Read only one variable length block */
1725 } /* for (total = 0, special = 0;
1726 total < count && !special; ) */
1728 /* Change the eof state if no data from tape or buffer */
1730 if (STps
->eof
== ST_FM_HIT
) {
1732 STps
->drv_block
= 0;
1733 if (STps
->drv_file
>= 0)
1735 } else if (STps
->eof
== ST_EOD_1
) {
1736 STps
->eof
= ST_EOD_2
;
1737 STps
->drv_block
= 0;
1738 if (STps
->drv_file
>= 0)
1740 } else if (STps
->eof
== ST_EOD_2
)
1742 } else if (STps
->eof
== ST_FM
)
1743 STps
->eof
= ST_NOEOF
;
1747 if (SRpnt
!= NULL
) {
1748 scsi_release_request(SRpnt
);
1758 /* Set the driver options */
1759 static void st_log_options(Scsi_Tape
* STp
, ST_mode
* STm
, int dev
)
1762 "st%d: Mode %d options: buffer writes: %d, async writes: %d, read ahead: %d\n",
1763 dev
, STp
->current_mode
, STm
->do_buffer_writes
, STm
->do_async_writes
,
1764 STm
->do_read_ahead
);
1766 "st%d: can bsr: %d, two FMs: %d, fast mteom: %d, auto lock: %d,\n",
1767 dev
, STp
->can_bsr
, STp
->two_fm
, STp
->fast_mteom
, STp
->do_auto_lock
);
1769 "st%d: defs for wr: %d, no block limits: %d, partitions: %d, s2 log: %d\n",
1770 dev
, STm
->defaults_for_writes
, STp
->omit_blklims
, STp
->can_partitions
,
1771 STp
->scsi2_logical
);
1773 "st%d: sysv: %d\n", dev
, STm
->sysv
);
1774 DEB(printk(KERN_INFO
1775 "st%d: debugging: %d\n",
1780 static int st_set_options(Scsi_Tape
*STp
, long options
)
1785 int dev
= TAPE_NR(STp
->devt
);
1787 STm
= &(STp
->modes
[STp
->current_mode
]);
1788 if (!STm
->defined
) {
1789 memcpy(STm
, &(STp
->modes
[0]), sizeof(ST_mode
));
1790 modes_defined
= TRUE
;
1791 DEBC(printk(ST_DEB_MSG
1792 "st%d: Initialized mode %d definition from mode 0\n",
1793 dev
, STp
->current_mode
));
1796 code
= options
& MT_ST_OPTIONS
;
1797 if (code
== MT_ST_BOOLEANS
) {
1798 STm
->do_buffer_writes
= (options
& MT_ST_BUFFER_WRITES
) != 0;
1799 STm
->do_async_writes
= (options
& MT_ST_ASYNC_WRITES
) != 0;
1800 STm
->defaults_for_writes
= (options
& MT_ST_DEF_WRITES
) != 0;
1801 STm
->do_read_ahead
= (options
& MT_ST_READ_AHEAD
) != 0;
1802 STp
->two_fm
= (options
& MT_ST_TWO_FM
) != 0;
1803 STp
->fast_mteom
= (options
& MT_ST_FAST_MTEOM
) != 0;
1804 STp
->do_auto_lock
= (options
& MT_ST_AUTO_LOCK
) != 0;
1805 STp
->can_bsr
= (options
& MT_ST_CAN_BSR
) != 0;
1806 STp
->omit_blklims
= (options
& MT_ST_NO_BLKLIMS
) != 0;
1807 if ((STp
->device
)->scsi_level
>= SCSI_2
)
1808 STp
->can_partitions
= (options
& MT_ST_CAN_PARTITIONS
) != 0;
1809 STp
->scsi2_logical
= (options
& MT_ST_SCSI2LOGICAL
) != 0;
1810 STm
->sysv
= (options
& MT_ST_SYSV
) != 0;
1811 DEB( debugging
= (options
& MT_ST_DEBUGGING
) != 0; )
1812 st_log_options(STp
, STm
, dev
);
1813 } else if (code
== MT_ST_SETBOOLEANS
|| code
== MT_ST_CLEARBOOLEANS
) {
1814 value
= (code
== MT_ST_SETBOOLEANS
);
1815 if ((options
& MT_ST_BUFFER_WRITES
) != 0)
1816 STm
->do_buffer_writes
= value
;
1817 if ((options
& MT_ST_ASYNC_WRITES
) != 0)
1818 STm
->do_async_writes
= value
;
1819 if ((options
& MT_ST_DEF_WRITES
) != 0)
1820 STm
->defaults_for_writes
= value
;
1821 if ((options
& MT_ST_READ_AHEAD
) != 0)
1822 STm
->do_read_ahead
= value
;
1823 if ((options
& MT_ST_TWO_FM
) != 0)
1824 STp
->two_fm
= value
;
1825 if ((options
& MT_ST_FAST_MTEOM
) != 0)
1826 STp
->fast_mteom
= value
;
1827 if ((options
& MT_ST_AUTO_LOCK
) != 0)
1828 STp
->do_auto_lock
= value
;
1829 if ((options
& MT_ST_CAN_BSR
) != 0)
1830 STp
->can_bsr
= value
;
1831 if ((options
& MT_ST_NO_BLKLIMS
) != 0)
1832 STp
->omit_blklims
= value
;
1833 if ((STp
->device
)->scsi_level
>= SCSI_2
&&
1834 (options
& MT_ST_CAN_PARTITIONS
) != 0)
1835 STp
->can_partitions
= value
;
1836 if ((options
& MT_ST_SCSI2LOGICAL
) != 0)
1837 STp
->scsi2_logical
= value
;
1838 if ((options
& MT_ST_SYSV
) != 0)
1841 if ((options
& MT_ST_DEBUGGING
) != 0)
1842 debugging
= value
; )
1843 st_log_options(STp
, STm
, dev
);
1844 } else if (code
== MT_ST_WRITE_THRESHOLD
) {
1845 value
= (options
& ~MT_ST_OPTIONS
) * ST_KILOBYTE
;
1846 if (value
< 1 || value
> st_buffer_size
) {
1848 "st%d: Write threshold %d too small or too large.\n",
1852 STp
->write_threshold
= value
;
1853 printk(KERN_INFO
"st%d: Write threshold set to %d bytes.\n",
1855 } else if (code
== MT_ST_DEF_BLKSIZE
) {
1856 value
= (options
& ~MT_ST_OPTIONS
);
1857 if (value
== ~MT_ST_OPTIONS
) {
1858 STm
->default_blksize
= (-1);
1859 printk(KERN_INFO
"st%d: Default block size disabled.\n", dev
);
1861 STm
->default_blksize
= value
;
1862 printk(KERN_INFO
"st%d: Default block size set to %d bytes.\n",
1863 dev
, STm
->default_blksize
);
1865 } else if (code
== MT_ST_TIMEOUTS
) {
1866 value
= (options
& ~MT_ST_OPTIONS
);
1867 if ((value
& MT_ST_SET_LONG_TIMEOUT
) != 0) {
1868 STp
->long_timeout
= (value
& ~MT_ST_SET_LONG_TIMEOUT
) * HZ
;
1869 printk(KERN_INFO
"st%d: Long timeout set to %d seconds.\n", dev
,
1870 (value
& ~MT_ST_SET_LONG_TIMEOUT
));
1872 STp
->timeout
= value
* HZ
;
1873 printk(KERN_INFO
"st%d: Normal timeout set to %d seconds.\n",
1876 } else if (code
== MT_ST_DEF_OPTIONS
) {
1877 code
= (options
& ~MT_ST_CLEAR_DEFAULT
);
1878 value
= (options
& MT_ST_CLEAR_DEFAULT
);
1879 if (code
== MT_ST_DEF_DENSITY
) {
1880 if (value
== MT_ST_CLEAR_DEFAULT
) {
1881 STm
->default_density
= (-1);
1882 printk(KERN_INFO
"st%d: Density default disabled.\n",
1885 STm
->default_density
= value
& 0xff;
1886 printk(KERN_INFO
"st%d: Density default set to %x\n",
1887 dev
, STm
->default_density
);
1889 } else if (code
== MT_ST_DEF_DRVBUFFER
) {
1890 if (value
== MT_ST_CLEAR_DEFAULT
) {
1891 STp
->default_drvbuffer
= 0xff;
1893 "st%d: Drive buffer default disabled.\n", dev
);
1895 STp
->default_drvbuffer
= value
& 7;
1897 "st%d: Drive buffer default set to %x\n",
1898 dev
, STp
->default_drvbuffer
);
1900 } else if (code
== MT_ST_DEF_COMPRESSION
) {
1901 if (value
== MT_ST_CLEAR_DEFAULT
) {
1902 STm
->default_compression
= ST_DONT_TOUCH
;
1904 "st%d: Compression default disabled.\n", dev
);
1906 STm
->default_compression
= (value
& 1 ? ST_YES
: ST_NO
);
1907 printk(KERN_INFO
"st%d: Compression default set to %x\n",
1917 #define MODE_HEADER_LENGTH 4
1919 /* Mode header and page byte offsets */
1920 #define MH_OFF_DATA_LENGTH 0
1921 #define MH_OFF_MEDIUM_TYPE 1
1922 #define MH_OFF_DEV_SPECIFIC 2
1923 #define MH_OFF_BDESCS_LENGTH 3
1924 #define MP_OFF_PAGE_NBR 0
1925 #define MP_OFF_PAGE_LENGTH 1
1927 /* Mode header and page bit masks */
1928 #define MH_BIT_WP 0x80
1929 #define MP_MSK_PAGE_NBR 0x3f
1931 /* Don't return block descriptors */
1932 #define MODE_SENSE_OMIT_BDESCS 0x08
1934 #define MODE_SELECT_PAGE_FORMAT 0x10
1936 /* Read a mode page into the tape buffer. The block descriptors are included
1937 if incl_block_descs is true. */
1938 static int read_mode_page(Scsi_Tape
*STp
, int page
, int omit_block_descs
)
1940 unsigned char cmd
[MAX_COMMAND_SIZE
];
1941 Scsi_Request
*SRpnt
= NULL
;
1943 memset(cmd
, 0, MAX_COMMAND_SIZE
);
1944 cmd
[0] = MODE_SENSE
;
1945 if (omit_block_descs
)
1946 cmd
[1] = MODE_SENSE_OMIT_BDESCS
;
1950 SRpnt
= st_do_scsi(SRpnt
, STp
, cmd
, cmd
[4], SCSI_DATA_READ
,
1951 STp
->timeout
, 0, TRUE
);
1953 return (STp
->buffer
)->syscall_result
;
1955 scsi_release_request(SRpnt
);
1957 return (STp
->buffer
)->syscall_result
;
1961 /* Send the mode page in the tape buffer to the drive. Assumes that the mode data
1962 in the buffer is correctly formatted. */
1963 static int write_mode_page(Scsi_Tape
*STp
, int page
)
1966 unsigned char cmd
[MAX_COMMAND_SIZE
];
1967 Scsi_Request
*SRpnt
= NULL
;
1969 memset(cmd
, 0, MAX_COMMAND_SIZE
);
1970 cmd
[0] = MODE_SELECT
;
1971 cmd
[1] = MODE_SELECT_PAGE_FORMAT
;
1972 pgo
= MODE_HEADER_LENGTH
+ (STp
->buffer
)->b_data
[MH_OFF_BDESCS_LENGTH
];
1973 cmd
[4] = pgo
+ (STp
->buffer
)->b_data
[pgo
+ MP_OFF_PAGE_LENGTH
] + 2;
1975 /* Clear reserved fields */
1976 (STp
->buffer
)->b_data
[MH_OFF_DATA_LENGTH
] = 0;
1977 (STp
->buffer
)->b_data
[MH_OFF_MEDIUM_TYPE
] = 0;
1978 (STp
->buffer
)->b_data
[MH_OFF_DEV_SPECIFIC
] &= ~MH_BIT_WP
;
1979 (STp
->buffer
)->b_data
[pgo
+ MP_OFF_PAGE_NBR
] &= MP_MSK_PAGE_NBR
;
1981 SRpnt
= st_do_scsi(SRpnt
, STp
, cmd
, cmd
[4], SCSI_DATA_WRITE
,
1982 STp
->timeout
, 0, TRUE
);
1984 return (STp
->buffer
)->syscall_result
;
1986 scsi_release_request(SRpnt
);
1988 return (STp
->buffer
)->syscall_result
;
1992 #define COMPRESSION_PAGE 0x0f
1993 #define COMPRESSION_PAGE_LENGTH 16
1995 #define CP_OFF_DCE_DCC 2
1997 #define DCE_MASK 0x80
1998 #define DCC_MASK 0x40
1999 #define RED_MASK 0x60
2002 /* Control the compression with mode page 15. Algorithm not changed if zero.
2004 The block descriptors are read and written because Sony SDT-7000 does not
2005 work without this (suggestion from Michael Schaefer <Michael.Schaefer@dlr.de>).
2006 Including block descriptors should not cause any harm to other drives. */
2008 static int st_compression(Scsi_Tape
* STp
, int state
)
2011 int mpoffs
; /* Offset to mode page start */
2012 unsigned char *b_data
= (STp
->buffer
)->b_data
;
2013 DEB( int dev
= TAPE_NR(STp
->devt
); )
2015 if (STp
->ready
!= ST_READY
)
2018 /* Read the current page contents */
2019 retval
= read_mode_page(STp
, COMPRESSION_PAGE
, FALSE
);
2021 DEBC(printk(ST_DEB_MSG
"st%d: Compression mode page not supported.\n",
2026 mpoffs
= MODE_HEADER_LENGTH
+ b_data
[MH_OFF_BDESCS_LENGTH
];
2027 DEBC(printk(ST_DEB_MSG
"st%d: Compression state is %d.\n", dev
,
2028 (b_data
[mpoffs
+ CP_OFF_DCE_DCC
] & DCE_MASK
? 1 : 0)));
2030 /* Check if compression can be changed */
2031 if ((b_data
[mpoffs
+ 2] & DCC_MASK
) == 0) {
2032 DEBC(printk(ST_DEB_MSG
"st%d: Compression not supported.\n", dev
));
2038 b_data
[mpoffs
+ CP_OFF_DCE_DCC
] |= DCE_MASK
;
2040 b_data
[mpoffs
+ CP_OFF_DCE_DCC
] &= ~DCE_MASK
;
2042 retval
= write_mode_page(STp
, COMPRESSION_PAGE
);
2044 DEBC(printk(ST_DEB_MSG
"st%d: Compression change failed.\n", dev
));
2047 DEBC(printk(ST_DEB_MSG
"st%d: Compression state changed to %d.\n",
2050 STp
->compression_changed
= TRUE
;
2055 /* Internal ioctl function */
2056 static int st_int_ioctl(Scsi_Tape
*STp
, unsigned int cmd_in
, unsigned long arg
)
2060 int i
, ioctl_result
;
2062 unsigned char cmd
[MAX_COMMAND_SIZE
];
2063 Scsi_Request
*SRpnt
;
2065 int fileno
, blkno
, at_sm
, undone
;
2066 int datalen
= 0, direction
= SCSI_DATA_NONE
;
2067 int dev
= TAPE_NR(STp
->devt
);
2069 if (STp
->ready
!= ST_READY
&& cmd_in
!= MTLOAD
) {
2070 if (STp
->ready
== ST_NO_TAPE
)
2071 return (-ENOMEDIUM
);
2075 timeout
= STp
->long_timeout
;
2076 STps
= &(STp
->ps
[STp
->partition
]);
2077 fileno
= STps
->drv_file
;
2078 blkno
= STps
->drv_block
;
2079 at_sm
= STps
->at_sm
;
2081 memset(cmd
, 0, MAX_COMMAND_SIZE
);
2084 chg_eof
= FALSE
; /* Changed from the FSF after this */
2087 cmd
[1] = 0x01; /* Space FileMarks */
2088 cmd
[2] = (arg
>> 16);
2089 cmd
[3] = (arg
>> 8);
2091 DEBC(printk(ST_DEB_MSG
"st%d: Spacing tape forward over %d filemarks.\n",
2092 dev
, cmd
[2] * 65536 + cmd
[3] * 256 + cmd
[4]));
2096 at_sm
&= (arg
== 0);
2099 chg_eof
= FALSE
; /* Changed from the FSF after this */
2102 cmd
[1] = 0x01; /* Space FileMarks */
2104 cmd
[2] = (ltmp
>> 16);
2105 cmd
[3] = (ltmp
>> 8);
2110 ltmp
= ltmp
| (cmd
[2] << 16) | (cmd
[3] << 8) | cmd
[4];
2112 "st%d: Spacing tape backward over %ld filemarks.\n",
2117 blkno
= (-1); /* We can't know the block number */
2118 at_sm
&= (arg
== 0);
2122 cmd
[1] = 0x00; /* Space Blocks */
2123 cmd
[2] = (arg
>> 16);
2124 cmd
[3] = (arg
>> 8);
2126 DEBC(printk(ST_DEB_MSG
"st%d: Spacing tape forward %d blocks.\n", dev
,
2127 cmd
[2] * 65536 + cmd
[3] * 256 + cmd
[4]));
2130 at_sm
&= (arg
== 0);
2134 cmd
[1] = 0x00; /* Space Blocks */
2136 cmd
[2] = (ltmp
>> 16);
2137 cmd
[3] = (ltmp
>> 8);
2142 ltmp
= ltmp
| (cmd
[2] << 16) | (cmd
[3] << 8) | cmd
[4];
2144 "st%d: Spacing tape backward %ld blocks.\n", dev
, (-ltmp
));
2148 at_sm
&= (arg
== 0);
2152 cmd
[1] = 0x04; /* Space Setmarks */
2153 cmd
[2] = (arg
>> 16);
2154 cmd
[3] = (arg
>> 8);
2156 DEBC(printk(ST_DEB_MSG
"st%d: Spacing tape forward %d setmarks.\n", dev
,
2157 cmd
[2] * 65536 + cmd
[3] * 256 + cmd
[4]));
2159 blkno
= fileno
= (-1);
2165 cmd
[1] = 0x04; /* Space Setmarks */
2167 cmd
[2] = (ltmp
>> 16);
2168 cmd
[3] = (ltmp
>> 8);
2173 ltmp
= ltmp
| (cmd
[2] << 16) | (cmd
[3] << 8) | cmd
[4];
2174 printk(ST_DEB_MSG
"st%d: Spacing tape backward %ld setmarks.\n",
2178 blkno
= fileno
= (-1);
2184 if (STp
->write_prot
)
2186 cmd
[0] = WRITE_FILEMARKS
;
2187 if (cmd_in
== MTWSM
)
2189 cmd
[2] = (arg
>> 16);
2190 cmd
[3] = (arg
>> 8);
2192 timeout
= STp
->timeout
;
2194 if (cmd_in
== MTWEOF
)
2195 printk(ST_DEB_MSG
"st%d: Writing %d filemarks.\n", dev
,
2196 cmd
[2] * 65536 + cmd
[3] * 256 + cmd
[4]);
2198 printk(ST_DEB_MSG
"st%d: Writing %d setmarks.\n", dev
,
2199 cmd
[2] * 65536 + cmd
[3] * 256 + cmd
[4]);
2204 at_sm
= (cmd_in
== MTWSM
);
2207 cmd
[0] = REZERO_UNIT
;
2209 cmd
[1] = 1; /* Don't wait for completion */
2210 timeout
= STp
->timeout
;
2212 DEBC(printk(ST_DEB_MSG
"st%d: Rewinding tape.\n", dev
));
2213 fileno
= blkno
= at_sm
= 0;
2218 cmd
[0] = START_STOP
;
2219 if (cmd_in
== MTLOAD
)
2222 * If arg >= 1 && arg <= 6 Enhanced load/unload in HP C1553A
2224 if (cmd_in
!= MTOFFL
&&
2225 arg
>= 1 + MT_ST_HPLOADER_OFFSET
2226 && arg
<= 6 + MT_ST_HPLOADER_OFFSET
) {
2227 DEBC(printk(ST_DEB_MSG
"st%d: Enhanced %sload slot %2ld.\n",
2228 dev
, (cmd
[4]) ? "" : "un",
2229 arg
- MT_ST_HPLOADER_OFFSET
));
2230 cmd
[3] = arg
- MT_ST_HPLOADER_OFFSET
; /* MediaID field of C1553A */
2233 cmd
[1] = 1; /* Don't wait for completion */
2234 timeout
= STp
->timeout
;
2236 timeout
= STp
->long_timeout
;
2239 if (cmd_in
!= MTLOAD
)
2240 printk(ST_DEB_MSG
"st%d: Unloading tape.\n", dev
);
2242 printk(ST_DEB_MSG
"st%d: Loading tape.\n", dev
);
2244 fileno
= blkno
= at_sm
= 0;
2247 DEBC(printk(ST_DEB_MSG
"st%d: No op on tape.\n", dev
));
2248 return 0; /* Should do something ? */
2251 cmd
[0] = START_STOP
;
2253 cmd
[1] = 1; /* Don't wait for completion */
2254 timeout
= STp
->timeout
;
2257 DEBC(printk(ST_DEB_MSG
"st%d: Retensioning tape.\n", dev
));
2258 fileno
= blkno
= at_sm
= 0;
2261 if (!STp
->fast_mteom
) {
2262 /* space to the end of tape */
2263 ioctl_result
= st_int_ioctl(STp
, MTFSF
, 0x3fff);
2264 fileno
= STps
->drv_file
;
2265 if (STps
->eof
>= ST_EOD_1
)
2267 /* The next lines would hide the number of spaced FileMarks
2268 That's why I inserted the previous lines. I had no luck
2269 with detecting EOM with FSF, so we go now to EOM.
2275 DEBC(printk(ST_DEB_MSG
"st%d: Spacing to end of recorded medium.\n",
2281 if (STp
->write_prot
)
2284 cmd
[1] = 1; /* To the end of tape */
2286 cmd
[1] |= 2; /* Don't wait for completion */
2287 timeout
= STp
->timeout
;
2289 timeout
= STp
->long_timeout
* 8;
2291 DEBC(printk(ST_DEB_MSG
"st%d: Erasing tape.\n", dev
));
2292 fileno
= blkno
= at_sm
= 0;
2296 cmd
[0] = ALLOW_MEDIUM_REMOVAL
;
2297 cmd
[4] = SCSI_REMOVAL_PREVENT
;
2298 DEBC(printk(ST_DEB_MSG
"st%d: Locking drive door.\n", dev
));
2302 cmd
[0] = ALLOW_MEDIUM_REMOVAL
;
2303 cmd
[4] = SCSI_REMOVAL_ALLOW
;
2304 DEBC(printk(ST_DEB_MSG
"st%d: Unlocking drive door.\n", dev
));
2306 case MTSETBLK
: /* Set block length */
2307 case MTSETDENSITY
: /* Set tape density */
2308 case MTSETDRVBUFFER
: /* Set drive buffering */
2309 case SET_DENS_AND_BLK
: /* Set density and block size */
2311 if (STp
->dirty
|| (STp
->buffer
)->buffer_bytes
!= 0)
2312 return (-EIO
); /* Not allowed if data in buffer */
2313 if ((cmd_in
== MTSETBLK
|| cmd_in
== SET_DENS_AND_BLK
) &&
2314 (arg
& MT_ST_BLKSIZE_MASK
) != 0 &&
2315 STp
->max_block
> 0 &&
2316 ((arg
& MT_ST_BLKSIZE_MASK
) < STp
->min_block
||
2317 (arg
& MT_ST_BLKSIZE_MASK
) > STp
->max_block
)) {
2318 printk(KERN_WARNING
"st%d: Illegal block size.\n", dev
);
2321 cmd
[0] = MODE_SELECT
;
2322 cmd
[4] = datalen
= 12;
2323 direction
= SCSI_DATA_WRITE
;
2325 memset((STp
->buffer
)->b_data
, 0, 12);
2326 if (cmd_in
== MTSETDRVBUFFER
)
2327 (STp
->buffer
)->b_data
[2] = (arg
& 7) << 4;
2329 (STp
->buffer
)->b_data
[2] =
2330 STp
->drv_buffer
<< 4;
2331 (STp
->buffer
)->b_data
[3] = 8; /* block descriptor length */
2332 if (cmd_in
== MTSETDENSITY
) {
2333 (STp
->buffer
)->b_data
[4] = arg
;
2334 STp
->density_changed
= TRUE
; /* At least we tried ;-) */
2335 } else if (cmd_in
== SET_DENS_AND_BLK
)
2336 (STp
->buffer
)->b_data
[4] = arg
>> 24;
2338 (STp
->buffer
)->b_data
[4] = STp
->density
;
2339 if (cmd_in
== MTSETBLK
|| cmd_in
== SET_DENS_AND_BLK
) {
2340 ltmp
= arg
& MT_ST_BLKSIZE_MASK
;
2341 if (cmd_in
== MTSETBLK
)
2342 STp
->blksize_changed
= TRUE
; /* At least we tried ;-) */
2344 ltmp
= STp
->block_size
;
2345 (STp
->buffer
)->b_data
[9] = (ltmp
>> 16);
2346 (STp
->buffer
)->b_data
[10] = (ltmp
>> 8);
2347 (STp
->buffer
)->b_data
[11] = ltmp
;
2348 timeout
= STp
->timeout
;
2350 if (cmd_in
== MTSETBLK
|| cmd_in
== SET_DENS_AND_BLK
)
2352 "st%d: Setting block size to %d bytes.\n", dev
,
2353 (STp
->buffer
)->b_data
[9] * 65536 +
2354 (STp
->buffer
)->b_data
[10] * 256 +
2355 (STp
->buffer
)->b_data
[11]);
2356 if (cmd_in
== MTSETDENSITY
|| cmd_in
== SET_DENS_AND_BLK
)
2358 "st%d: Setting density code to %x.\n", dev
,
2359 (STp
->buffer
)->b_data
[4]);
2360 if (cmd_in
== MTSETDRVBUFFER
)
2362 "st%d: Setting drive buffer code to %d.\n", dev
,
2363 ((STp
->buffer
)->b_data
[2] >> 4) & 7);
2370 SRpnt
= st_do_scsi(NULL
, STp
, cmd
, datalen
, direction
,
2371 timeout
, MAX_RETRIES
, TRUE
);
2373 return (STp
->buffer
)->syscall_result
;
2375 ioctl_result
= (STp
->buffer
)->syscall_result
;
2377 if (!ioctl_result
) { /* SCSI command successful */
2378 scsi_release_request(SRpnt
);
2380 STps
->drv_block
= blkno
;
2381 STps
->drv_file
= fileno
;
2382 STps
->at_sm
= at_sm
;
2384 if (cmd_in
== MTLOCK
)
2385 STp
->door_locked
= ST_LOCKED_EXPLICIT
;
2386 else if (cmd_in
== MTUNLOCK
)
2387 STp
->door_locked
= ST_UNLOCKED
;
2389 if (cmd_in
== MTBSFM
)
2390 ioctl_result
= st_int_ioctl(STp
, MTFSF
, 1);
2391 else if (cmd_in
== MTFSFM
)
2392 ioctl_result
= st_int_ioctl(STp
, MTBSF
, 1);
2394 if (cmd_in
== MTSETBLK
|| cmd_in
== SET_DENS_AND_BLK
) {
2395 STp
->block_size
= arg
& MT_ST_BLKSIZE_MASK
;
2396 if (STp
->block_size
!= 0)
2397 (STp
->buffer
)->buffer_blocks
=
2398 (STp
->buffer
)->buffer_size
/ STp
->block_size
;
2399 (STp
->buffer
)->buffer_bytes
= (STp
->buffer
)->read_pointer
= 0;
2400 if (cmd_in
== SET_DENS_AND_BLK
)
2401 STp
->density
= arg
>> MT_ST_DENSITY_SHIFT
;
2402 } else if (cmd_in
== MTSETDRVBUFFER
)
2403 STp
->drv_buffer
= (arg
& 7);
2404 else if (cmd_in
== MTSETDENSITY
)
2407 if (cmd_in
== MTEOM
)
2409 else if (cmd_in
== MTFSF
)
2412 STps
->eof
= ST_NOEOF
;
2415 if (cmd_in
== MTOFFL
|| cmd_in
== MTUNLOAD
)
2416 STp
->rew_at_close
= 0;
2417 else if (cmd_in
== MTLOAD
) {
2418 STp
->rew_at_close
= STp
->autorew_dev
;
2419 for (i
= 0; i
< ST_NBR_PARTITIONS
; i
++) {
2420 STp
->ps
[i
].rw
= ST_IDLE
;
2421 STp
->ps
[i
].last_block_valid
= FALSE
;
2425 } else { /* SCSI command was not completely successful. Don't return
2426 from this block without releasing the SCSI command block! */
2428 if (SRpnt
->sr_sense_buffer
[2] & 0x40) {
2429 if (cmd_in
!= MTBSF
&& cmd_in
!= MTBSFM
&&
2430 cmd_in
!= MTBSR
&& cmd_in
!= MTBSS
)
2431 STps
->eof
= ST_EOM_OK
;
2432 STps
->drv_block
= 0;
2436 (SRpnt
->sr_sense_buffer
[3] << 24) +
2437 (SRpnt
->sr_sense_buffer
[4] << 16) +
2438 (SRpnt
->sr_sense_buffer
[5] << 8) +
2439 SRpnt
->sr_sense_buffer
[6]);
2440 if (cmd_in
== MTWEOF
&&
2441 (SRpnt
->sr_sense_buffer
[0] & 0x70) == 0x70 &&
2442 (SRpnt
->sr_sense_buffer
[2] & 0x4f) == 0x40 &&
2443 ((SRpnt
->sr_sense_buffer
[0] & 0x80) == 0 || undone
== 0)) {
2444 ioctl_result
= 0; /* EOF written succesfully at EOM */
2447 STps
->drv_file
= fileno
;
2448 STps
->eof
= ST_NOEOF
;
2449 } else if ((cmd_in
== MTFSF
) || (cmd_in
== MTFSFM
)) {
2451 STps
->drv_file
= fileno
- undone
;
2453 STps
->drv_file
= fileno
;
2454 STps
->drv_block
= 0;
2455 STps
->eof
= ST_NOEOF
;
2456 } else if ((cmd_in
== MTBSF
) || (cmd_in
== MTBSFM
)) {
2458 STps
->drv_file
= fileno
+ undone
;
2460 STps
->drv_file
= fileno
;
2461 STps
->drv_block
= 0;
2462 STps
->eof
= ST_NOEOF
;
2463 } else if (cmd_in
== MTFSR
) {
2464 if (SRpnt
->sr_sense_buffer
[2] & 0x80) { /* Hit filemark */
2465 if (STps
->drv_file
>= 0)
2467 STps
->drv_block
= 0;
2470 if (blkno
>= undone
)
2471 STps
->drv_block
= blkno
- undone
;
2473 STps
->drv_block
= (-1);
2474 STps
->eof
= ST_NOEOF
;
2476 } else if (cmd_in
== MTBSR
) {
2477 if (SRpnt
->sr_sense_buffer
[2] & 0x80) { /* Hit filemark */
2479 STps
->drv_block
= (-1);
2482 STps
->drv_block
= blkno
+ undone
;
2484 STps
->drv_block
= (-1);
2486 STps
->eof
= ST_NOEOF
;
2487 } else if (cmd_in
== MTEOM
) {
2488 STps
->drv_file
= (-1);
2489 STps
->drv_block
= (-1);
2492 STps
->eof
= ST_NOEOF
;
2494 if ((SRpnt
->sr_sense_buffer
[2] & 0x0f) == BLANK_CHECK
)
2497 if (cmd_in
== MTLOCK
)
2498 STp
->door_locked
= ST_LOCK_FAILS
;
2500 scsi_release_request(SRpnt
);
2504 return ioctl_result
;
2508 /* Get the tape position. If bt == 2, arg points into a kernel space mt_loc
2511 static int get_location(Scsi_Tape
*STp
, unsigned int *block
, int *partition
,
2515 unsigned char scmd
[MAX_COMMAND_SIZE
];
2516 Scsi_Request
*SRpnt
;
2517 DEB( int dev
= TAPE_NR(STp
->devt
); )
2519 if (STp
->ready
!= ST_READY
)
2522 memset(scmd
, 0, MAX_COMMAND_SIZE
);
2523 if ((STp
->device
)->scsi_level
< SCSI_2
) {
2524 scmd
[0] = QFA_REQUEST_BLOCK
;
2527 scmd
[0] = READ_POSITION
;
2528 if (!logical
&& !STp
->scsi2_logical
)
2531 SRpnt
= st_do_scsi(NULL
, STp
, scmd
, 20, SCSI_DATA_READ
, STp
->timeout
,
2532 MAX_READY_RETRIES
, TRUE
);
2534 return (STp
->buffer
)->syscall_result
;
2536 if ((STp
->buffer
)->syscall_result
!= 0 ||
2537 (STp
->device
->scsi_level
>= SCSI_2
&&
2538 ((STp
->buffer
)->b_data
[0] & 4) != 0)) {
2539 *block
= *partition
= 0;
2540 DEBC(printk(ST_DEB_MSG
"st%d: Can't read tape position.\n", dev
));
2544 if ((STp
->device
)->scsi_level
< SCSI_2
) {
2545 *block
= ((STp
->buffer
)->b_data
[0] << 16)
2546 + ((STp
->buffer
)->b_data
[1] << 8)
2547 + (STp
->buffer
)->b_data
[2];
2550 *block
= ((STp
->buffer
)->b_data
[4] << 24)
2551 + ((STp
->buffer
)->b_data
[5] << 16)
2552 + ((STp
->buffer
)->b_data
[6] << 8)
2553 + (STp
->buffer
)->b_data
[7];
2554 *partition
= (STp
->buffer
)->b_data
[1];
2555 if (((STp
->buffer
)->b_data
[0] & 0x80) &&
2556 (STp
->buffer
)->b_data
[1] == 0) /* BOP of partition 0 */
2557 STp
->ps
[0].drv_block
= STp
->ps
[0].drv_file
= 0;
2559 DEBC(printk(ST_DEB_MSG
"st%d: Got tape pos. blk %d part %d.\n", dev
,
2560 *block
, *partition
));
2562 scsi_release_request(SRpnt
);
2569 /* Set the tape block and partition. Negative partition means that only the
2570 block should be set in vendor specific way. */
2571 static int set_location(Scsi_Tape
*STp
, unsigned int block
, int partition
,
2578 unsigned char scmd
[MAX_COMMAND_SIZE
];
2579 Scsi_Request
*SRpnt
;
2580 DEB( int dev
= TAPE_NR(STp
->devt
); )
2582 if (STp
->ready
!= ST_READY
)
2584 timeout
= STp
->long_timeout
;
2585 STps
= &(STp
->ps
[STp
->partition
]);
2587 DEBC(printk(ST_DEB_MSG
"st%d: Setting block to %d and partition to %d.\n",
2588 dev
, block
, partition
));
2589 DEB(if (partition
< 0)
2592 /* Update the location at the partition we are leaving */
2593 if ((!STp
->can_partitions
&& partition
!= 0) ||
2594 partition
>= ST_NBR_PARTITIONS
)
2596 if (partition
!= STp
->partition
) {
2597 if (get_location(STp
, &blk
, &p
, 1))
2598 STps
->last_block_valid
= FALSE
;
2600 STps
->last_block_valid
= TRUE
;
2601 STps
->last_block_visited
= blk
;
2602 DEBC(printk(ST_DEB_MSG
2603 "st%d: Visited block %d for partition %d saved.\n",
2604 dev
, blk
, STp
->partition
));
2608 memset(scmd
, 0, MAX_COMMAND_SIZE
);
2609 if ((STp
->device
)->scsi_level
< SCSI_2
) {
2610 scmd
[0] = QFA_SEEK_BLOCK
;
2611 scmd
[2] = (block
>> 16);
2612 scmd
[3] = (block
>> 8);
2617 scmd
[3] = (block
>> 24);
2618 scmd
[4] = (block
>> 16);
2619 scmd
[5] = (block
>> 8);
2621 if (!logical
&& !STp
->scsi2_logical
)
2623 if (STp
->partition
!= partition
) {
2625 scmd
[8] = partition
;
2626 DEBC(printk(ST_DEB_MSG
2627 "st%d: Trying to change partition from %d to %d\n",
2628 dev
, STp
->partition
, partition
));
2632 scmd
[1] |= 1; /* Don't wait for completion */
2633 timeout
= STp
->timeout
;
2636 SRpnt
= st_do_scsi(NULL
, STp
, scmd
, 0, SCSI_DATA_NONE
,
2637 timeout
, MAX_READY_RETRIES
, TRUE
);
2639 return (STp
->buffer
)->syscall_result
;
2641 STps
->drv_block
= STps
->drv_file
= (-1);
2642 STps
->eof
= ST_NOEOF
;
2643 if ((STp
->buffer
)->syscall_result
!= 0) {
2645 if (STp
->can_partitions
&&
2646 (STp
->device
)->scsi_level
>= SCSI_2
&&
2647 (p
= find_partition(STp
)) >= 0)
2650 if (STp
->can_partitions
) {
2651 STp
->partition
= partition
;
2652 STps
= &(STp
->ps
[partition
]);
2653 if (!STps
->last_block_valid
||
2654 STps
->last_block_visited
!= block
) {
2661 STps
->drv_block
= STps
->drv_file
= 0;
2665 scsi_release_request(SRpnt
);
2672 /* Find the current partition number for the drive status. Called from open and
2673 returns either partition number of negative error code. */
2674 static int find_partition(Scsi_Tape
*STp
)
2679 if ((i
= get_location(STp
, &block
, &partition
, 1)) < 0)
2681 if (partition
>= ST_NBR_PARTITIONS
)
2687 /* Change the partition if necessary */
2688 static int update_partition(Scsi_Tape
*STp
)
2692 if (STp
->partition
== STp
->new_partition
)
2694 STps
= &(STp
->ps
[STp
->new_partition
]);
2695 if (!STps
->last_block_valid
)
2696 STps
->last_block_visited
= 0;
2697 return set_location(STp
, STps
->last_block_visited
, STp
->new_partition
, 1);
2700 /* Functions for reading and writing the medium partition mode page. */
2702 #define PART_PAGE 0x11
2703 #define PART_PAGE_FIXED_LENGTH 8
2705 #define PP_OFF_MAX_ADD_PARTS 2
2706 #define PP_OFF_NBR_ADD_PARTS 3
2707 #define PP_OFF_FLAGS 4
2708 #define PP_OFF_PART_UNITS 6
2709 #define PP_OFF_RESERVED 7
2711 #define PP_BIT_IDP 0x20
2712 #define PP_MSK_PSUM_MB 0x10
2714 /* Get the number of partitions on the tape. As a side effect reads the
2715 mode page into the tape buffer. */
2716 static int nbr_partitions(Scsi_Tape
*STp
)
2719 DEB( int dev
= TAPE_NR(STp
->devt
) );
2721 if (STp
->ready
!= ST_READY
)
2724 result
= read_mode_page(STp
, PART_PAGE
, TRUE
);
2727 DEBC(printk(ST_DEB_MSG
"st%d: Can't read medium partition page.\n",
2731 result
= (STp
->buffer
)->b_data
[MODE_HEADER_LENGTH
+
2732 PP_OFF_NBR_ADD_PARTS
] + 1;
2733 DEBC(printk(ST_DEB_MSG
"st%d: Number of partitions %d.\n", dev
, result
));
2740 /* Partition the tape into two partitions if size > 0 or one partition if
2743 The block descriptors are read and written because Sony SDT-7000 does not
2744 work without this (suggestion from Michael Schaefer <Michael.Schaefer@dlr.de>).
2746 My HP C1533A drive returns only one partition size field. This is used to
2747 set the size of partition 1. There is no size field for the default partition.
2748 Michael Schaefer's Sony SDT-7000 returns two descriptors and the second is
2749 used to set the size of partition 1 (this is what the SCSI-3 standard specifies).
2750 The following algorithm is used to accomodate both drives: if the number of
2751 partition size fields is greater than the maximum number of additional partitions
2752 in the mode page, the second field is used. Otherwise the first field is used.
2754 For Seagate DDS drives the page length must be 8 when no partitions is defined
2755 and 10 when 1 partition is defined (information from Eric Lee Green). This is
2756 is acceptable also to some other old drives and enforced if the first partition
2757 size field is used for the first additional partition size.
2759 static int partition_tape(Scsi_Tape
*STp
, int size
)
2761 int dev
= TAPE_NR(STp
->devt
), result
;
2762 int pgo
, psd_cnt
, psdo
;
2765 result
= read_mode_page(STp
, PART_PAGE
, FALSE
);
2767 DEBC(printk(ST_DEB_MSG
"st%d: Can't read partition mode page.\n", dev
));
2770 /* The mode page is in the buffer. Let's modify it and write it. */
2771 bp
= (STp
->buffer
)->b_data
;
2772 pgo
= MODE_HEADER_LENGTH
+ bp
[MH_OFF_BDESCS_LENGTH
];
2773 DEBC(printk(ST_DEB_MSG
"st%d: Partition page length is %d bytes.\n",
2774 dev
, bp
[pgo
+ MP_OFF_PAGE_LENGTH
] + 2));
2776 psd_cnt
= (bp
[pgo
+ MP_OFF_PAGE_LENGTH
] + 2 - PART_PAGE_FIXED_LENGTH
) / 2;
2777 psdo
= pgo
+ PART_PAGE_FIXED_LENGTH
;
2778 if (psd_cnt
> bp
[pgo
+ PP_OFF_MAX_ADD_PARTS
]) {
2779 bp
[psdo
] = bp
[psdo
+ 1] = 0xff; /* Rest of the tape */
2782 memset(bp
+ psdo
, 0, bp
[pgo
+ PP_OFF_NBR_ADD_PARTS
] * 2);
2784 DEBC(printk("st%d: psd_cnt %d, max.parts %d, nbr_parts %d\n", dev
,
2785 psd_cnt
, bp
[pgo
+ PP_OFF_MAX_ADD_PARTS
],
2786 bp
[pgo
+ PP_OFF_NBR_ADD_PARTS
]));
2789 bp
[pgo
+ PP_OFF_NBR_ADD_PARTS
] = 0;
2790 if (psd_cnt
<= bp
[pgo
+ PP_OFF_MAX_ADD_PARTS
])
2791 bp
[pgo
+ MP_OFF_PAGE_LENGTH
] = 6;
2792 DEBC(printk(ST_DEB_MSG
"st%d: Formatting tape with one partition.\n",
2795 bp
[psdo
] = (size
>> 8) & 0xff;
2796 bp
[psdo
+ 1] = size
& 0xff;
2798 if (bp
[pgo
+ MP_OFF_PAGE_LENGTH
] < 8)
2799 bp
[pgo
+ MP_OFF_PAGE_LENGTH
] = 8;
2800 DEBC(printk(ST_DEB_MSG
2801 "st%d: Formatting tape with two partitions (1 = %d MB).\n",
2804 bp
[pgo
+ PP_OFF_PART_UNITS
] = 0;
2805 bp
[pgo
+ PP_OFF_RESERVED
] = 0;
2806 bp
[pgo
+ PP_OFF_FLAGS
] = PP_BIT_IDP
| PP_MSK_PSUM_MB
;
2808 result
= write_mode_page(STp
, PART_PAGE
);
2810 printk(KERN_INFO
"st%d: Partitioning of tape failed.\n", dev
);
2819 /* The ioctl command */
2820 static int st_ioctl(struct inode
*inode
, struct file
*file
,
2821 unsigned int cmd_in
, unsigned long arg
)
2823 int i
, cmd_nr
, cmd_type
, bt
;
2829 int dev
= TAPE_NR(inode
->i_rdev
);
2831 read_lock(&st_dev_arr_lock
);
2832 STp
= scsi_tapes
[dev
];
2833 read_unlock(&st_dev_arr_lock
);
2835 if (down_interruptible(&STp
->lock
))
2836 return -ERESTARTSYS
;
2839 if (debugging
&& !STp
->in_use
) {
2840 printk(ST_DEB_MSG
"st%d: Incorrect device.\n", dev
);
2845 STm
= &(STp
->modes
[STp
->current_mode
]);
2846 STps
= &(STp
->ps
[STp
->partition
]);
2849 * If we are in the middle of error recovery, don't let anyone
2850 * else try and use this device. Also, if error recovery fails, it
2851 * may try and take the device offline, in which case all further
2852 * access to the device is prohibited.
2854 if (!scsi_block_when_processing_errors(STp
->device
)) {
2858 cmd_type
= _IOC_TYPE(cmd_in
);
2859 cmd_nr
= _IOC_NR(cmd_in
);
2861 if (cmd_type
== _IOC_TYPE(MTIOCTOP
) && cmd_nr
== _IOC_NR(MTIOCTOP
)) {
2864 if (_IOC_SIZE(cmd_in
) != sizeof(mtc
)) {
2869 i
= copy_from_user((char *) &mtc
, (char *) arg
, sizeof(struct mtop
));
2875 if (mtc
.mt_op
== MTSETDRVBUFFER
&& !capable(CAP_SYS_ADMIN
)) {
2877 "st%d: MTSETDRVBUFFER only allowed for root.\n", dev
);
2881 if (!STm
->defined
&&
2882 (mtc
.mt_op
!= MTSETDRVBUFFER
&&
2883 (mtc
.mt_count
& MT_ST_OPTIONS
) == 0)) {
2888 if (!(STp
->device
)->was_reset
) {
2890 if (STps
->eof
== ST_FM_HIT
) {
2891 if (mtc
.mt_op
== MTFSF
|| mtc
.mt_op
== MTFSFM
||
2892 mtc
.mt_op
== MTEOM
) {
2894 if (STps
->drv_file
>= 0)
2895 STps
->drv_file
+= 1;
2896 } else if (mtc
.mt_op
== MTBSF
|| mtc
.mt_op
== MTBSFM
) {
2898 if (STps
->drv_file
>= 0)
2899 STps
->drv_file
+= 1;
2903 if (mtc
.mt_op
== MTSEEK
) {
2904 /* Old position must be restored if partition will be
2906 i
= !STp
->can_partitions
||
2907 (STp
->new_partition
!= STp
->partition
);
2909 i
= mtc
.mt_op
== MTREW
|| mtc
.mt_op
== MTOFFL
||
2910 mtc
.mt_op
== MTRETEN
|| mtc
.mt_op
== MTEOM
||
2911 mtc
.mt_op
== MTLOCK
|| mtc
.mt_op
== MTLOAD
||
2912 mtc
.mt_op
== MTFSF
|| mtc
.mt_op
== MTFSFM
||
2913 mtc
.mt_op
== MTBSF
|| mtc
.mt_op
== MTBSFM
||
2914 mtc
.mt_op
== MTCOMPRESSION
;
2916 i
= flush_buffer(STp
, i
);
2923 * If there was a bus reset, block further access
2924 * to this device. If the user wants to rewind the tape,
2925 * then reset the flag and allow access again.
2927 if (mtc
.mt_op
!= MTREW
&&
2928 mtc
.mt_op
!= MTOFFL
&&
2929 mtc
.mt_op
!= MTRETEN
&&
2930 mtc
.mt_op
!= MTERASE
&&
2931 mtc
.mt_op
!= MTSEEK
&&
2932 mtc
.mt_op
!= MTEOM
) {
2936 STp
->device
->was_reset
= 0;
2937 if (STp
->door_locked
!= ST_UNLOCKED
&&
2938 STp
->door_locked
!= ST_LOCK_FAILS
) {
2939 if (st_int_ioctl(STp
, MTLOCK
, 0)) {
2941 "st%d: Could not relock door after bus reset.\n",
2943 STp
->door_locked
= ST_UNLOCKED
;
2948 if (mtc
.mt_op
!= MTNOP
&& mtc
.mt_op
!= MTSETBLK
&&
2949 mtc
.mt_op
!= MTSETDENSITY
&& mtc
.mt_op
!= MTWSM
&&
2950 mtc
.mt_op
!= MTSETDRVBUFFER
&& mtc
.mt_op
!= MTSETPART
)
2951 STps
->rw
= ST_IDLE
; /* Prevent automatic WEOF and fsf */
2953 if (mtc
.mt_op
== MTOFFL
&& STp
->door_locked
!= ST_UNLOCKED
)
2954 st_int_ioctl(STp
, MTUNLOCK
, 0); /* Ignore result! */
2956 if (mtc
.mt_op
== MTSETDRVBUFFER
&&
2957 (mtc
.mt_count
& MT_ST_OPTIONS
) != 0) {
2958 retval
= st_set_options(STp
, mtc
.mt_count
);
2962 if (mtc
.mt_op
== MTSETPART
) {
2963 if (!STp
->can_partitions
||
2964 mtc
.mt_count
< 0 || mtc
.mt_count
>= ST_NBR_PARTITIONS
) {
2968 if (mtc
.mt_count
>= STp
->nbr_partitions
&&
2969 (STp
->nbr_partitions
= nbr_partitions(STp
)) < 0) {
2973 if (mtc
.mt_count
>= STp
->nbr_partitions
) {
2977 STp
->new_partition
= mtc
.mt_count
;
2982 if (mtc
.mt_op
== MTMKPART
) {
2983 if (!STp
->can_partitions
) {
2987 if ((i
= st_int_ioctl(STp
, MTREW
, 0)) < 0 ||
2988 (i
= partition_tape(STp
, mtc
.mt_count
)) < 0) {
2992 for (i
= 0; i
< ST_NBR_PARTITIONS
; i
++) {
2993 STp
->ps
[i
].rw
= ST_IDLE
;
2994 STp
->ps
[i
].at_sm
= 0;
2995 STp
->ps
[i
].last_block_valid
= FALSE
;
2997 STp
->partition
= STp
->new_partition
= 0;
2998 STp
->nbr_partitions
= 1; /* Bad guess ?-) */
2999 STps
->drv_block
= STps
->drv_file
= 0;
3004 if (mtc
.mt_op
== MTSEEK
) {
3005 i
= set_location(STp
, mtc
.mt_count
, STp
->new_partition
, 0);
3006 if (!STp
->can_partitions
)
3007 STp
->ps
[0].rw
= ST_IDLE
;
3012 if (STp
->can_partitions
&& STp
->ready
== ST_READY
&&
3013 (i
= update_partition(STp
)) < 0) {
3018 if (mtc
.mt_op
== MTCOMPRESSION
)
3019 retval
= st_compression(STp
, (mtc
.mt_count
& 1));
3021 retval
= st_int_ioctl(STp
, mtc
.mt_op
, mtc
.mt_count
);
3024 if (!STm
->defined
) {
3029 if ((i
= flush_buffer(STp
, FALSE
)) < 0) {
3033 if (STp
->can_partitions
&&
3034 (i
= update_partition(STp
)) < 0) {
3039 if (cmd_type
== _IOC_TYPE(MTIOCGET
) && cmd_nr
== _IOC_NR(MTIOCGET
)) {
3040 struct mtget mt_status
;
3042 if (_IOC_SIZE(cmd_in
) != sizeof(struct mtget
)) {
3047 mt_status
.mt_type
= STp
->tape_type
;
3048 mt_status
.mt_dsreg
=
3049 ((STp
->block_size
<< MT_ST_BLKSIZE_SHIFT
) & MT_ST_BLKSIZE_MASK
) |
3050 ((STp
->density
<< MT_ST_DENSITY_SHIFT
) & MT_ST_DENSITY_MASK
);
3051 mt_status
.mt_blkno
= STps
->drv_block
;
3052 mt_status
.mt_fileno
= STps
->drv_file
;
3053 if (STp
->block_size
!= 0) {
3054 if (STps
->rw
== ST_WRITING
)
3055 mt_status
.mt_blkno
+=
3056 (STp
->buffer
)->buffer_bytes
/ STp
->block_size
;
3057 else if (STps
->rw
== ST_READING
)
3058 mt_status
.mt_blkno
-=
3059 ((STp
->buffer
)->buffer_bytes
+
3060 STp
->block_size
- 1) / STp
->block_size
;
3063 mt_status
.mt_gstat
= 0;
3064 if (STp
->drv_write_prot
)
3065 mt_status
.mt_gstat
|= GMT_WR_PROT(0xffffffff);
3066 if (mt_status
.mt_blkno
== 0) {
3067 if (mt_status
.mt_fileno
== 0)
3068 mt_status
.mt_gstat
|= GMT_BOT(0xffffffff);
3070 mt_status
.mt_gstat
|= GMT_EOF(0xffffffff);
3072 mt_status
.mt_erreg
= (STp
->recover_reg
<< MT_ST_SOFTERR_SHIFT
);
3073 mt_status
.mt_resid
= STp
->partition
;
3074 if (STps
->eof
== ST_EOM_OK
|| STps
->eof
== ST_EOM_ERROR
)
3075 mt_status
.mt_gstat
|= GMT_EOT(0xffffffff);
3076 else if (STps
->eof
>= ST_EOM_OK
)
3077 mt_status
.mt_gstat
|= GMT_EOD(0xffffffff);
3078 if (STp
->density
== 1)
3079 mt_status
.mt_gstat
|= GMT_D_800(0xffffffff);
3080 else if (STp
->density
== 2)
3081 mt_status
.mt_gstat
|= GMT_D_1600(0xffffffff);
3082 else if (STp
->density
== 3)
3083 mt_status
.mt_gstat
|= GMT_D_6250(0xffffffff);
3084 if (STp
->ready
== ST_READY
)
3085 mt_status
.mt_gstat
|= GMT_ONLINE(0xffffffff);
3086 if (STp
->ready
== ST_NO_TAPE
)
3087 mt_status
.mt_gstat
|= GMT_DR_OPEN(0xffffffff);
3089 mt_status
.mt_gstat
|= GMT_SM(0xffffffff);
3090 if (STm
->do_async_writes
||
3091 (STm
->do_buffer_writes
&& STp
->block_size
!= 0) ||
3092 STp
->drv_buffer
!= 0)
3093 mt_status
.mt_gstat
|= GMT_IM_REP_EN(0xffffffff);
3095 i
= copy_to_user((char *) arg
, (char *) &(mt_status
),
3096 sizeof(struct mtget
));
3102 STp
->recover_reg
= 0; /* Clear after read */
3105 } /* End of MTIOCGET */
3106 if (cmd_type
== _IOC_TYPE(MTIOCPOS
) && cmd_nr
== _IOC_NR(MTIOCPOS
)) {
3107 struct mtpos mt_pos
;
3108 if (_IOC_SIZE(cmd_in
) != sizeof(struct mtpos
)) {
3112 if ((i
= get_location(STp
, &blk
, &bt
, 0)) < 0) {
3116 mt_pos
.mt_blkno
= blk
;
3117 i
= copy_to_user((char *) arg
, (char *) (&mt_pos
), sizeof(struct mtpos
));
3123 return scsi_ioctl(STp
->device
, cmd_in
, (void *) arg
);
3131 /* Try to allocate a new tape buffer. Calling function must not hold
3134 new_tape_buffer(int from_initialization
, int need_dma
, int in_use
)
3136 int i
, priority
, b_size
, order
, got
= 0, segs
= 0;
3137 unsigned long flags
;
3140 read_lock(&st_dev_arr_lock
);
3141 if (st_nbr_buffers
>= st_template
.dev_max
) {
3142 read_unlock(&st_dev_arr_lock
);
3143 return NULL
; /* Should never happen */
3145 read_unlock(&st_dev_arr_lock
);
3147 if (from_initialization
)
3148 priority
= GFP_ATOMIC
;
3150 priority
= GFP_KERNEL
;
3152 i
= sizeof(ST_buffer
) + (st_max_sg_segs
- 1) * sizeof(struct scatterlist
);
3153 tb
= kmalloc(i
, priority
);
3156 priority
|= GFP_DMA
;
3158 /* Try to allocate the first segment up to ST_FIRST_ORDER and the
3159 others big enough to reach the goal */
3160 for (b_size
= PAGE_SIZE
, order
=0;
3161 b_size
< st_buffer_size
&& order
< ST_FIRST_ORDER
;
3162 order
++, b_size
*= 2)
3164 for ( ; b_size
>= PAGE_SIZE
; order
--, b_size
/= 2) {
3166 (unsigned char *) __get_free_pages(priority
, order
);
3167 if (tb
->sg
[0].address
!= NULL
) {
3168 tb
->sg
[0].alt_address
= NULL
;
3169 tb
->sg
[0].length
= b_size
;
3173 if (tb
->sg
[segs
].address
== NULL
) {
3176 } else { /* Got something, continue */
3178 for (b_size
= PAGE_SIZE
, order
=0;
3180 tb
->sg
[0].length
+ (ST_FIRST_SG
- 1) * b_size
;
3181 order
++, b_size
*= 2)
3183 for (segs
= 1, got
= tb
->sg
[0].length
;
3184 got
< st_buffer_size
&& segs
< ST_FIRST_SG
;) {
3185 tb
->sg
[segs
].address
=
3186 (unsigned char *) __get_free_pages(priority
,
3188 if (tb
->sg
[segs
].address
== NULL
) {
3189 if (st_buffer_size
- got
<=
3190 (ST_FIRST_SG
- segs
) * b_size
/ 2) {
3191 b_size
/= 2; /* Large enough for the
3192 rest of the buffers */
3197 tb
->orig_sg_segs
= 0;
3198 DEB(tb
->buffer_size
= got
);
3199 normalize_buffer(tb
);
3204 tb
->sg
[segs
].alt_address
= NULL
;
3205 tb
->sg
[segs
].length
= b_size
;
3213 printk(KERN_NOTICE
"st: Can't allocate new tape buffer (nbr %d).\n",
3217 tb
->sg_segs
= tb
->orig_sg_segs
= segs
;
3218 tb
->b_data
= tb
->sg
[0].address
;
3220 DEBC(printk(ST_DEB_MSG
3221 "st: Allocated tape buffer %d (%d bytes, %d segments, dma: %d, a: %p).\n",
3222 st_nbr_buffers
, got
, tb
->sg_segs
, need_dma
, tb
->b_data
);
3224 "st: segment sizes: first %d, last %d bytes.\n",
3225 tb
->sg
[0].length
, tb
->sg
[segs
- 1].length
);
3227 tb
->in_use
= in_use
;
3229 tb
->buffer_size
= got
;
3232 write_lock_irqsave(&st_dev_arr_lock
, flags
);
3233 st_buffers
[st_nbr_buffers
++] = tb
;
3234 write_unlock_irqrestore(&st_dev_arr_lock
, flags
);
3240 /* Try to allocate a temporary enlarged tape buffer */
3241 static int enlarge_buffer(ST_buffer
* STbuffer
, int new_size
, int need_dma
)
3243 int segs
, nbr
, max_segs
, b_size
, priority
, order
, got
;
3245 normalize_buffer(STbuffer
);
3247 max_segs
= STbuffer
->use_sg
;
3248 if (max_segs
> st_max_sg_segs
)
3249 max_segs
= st_max_sg_segs
;
3250 nbr
= max_segs
- STbuffer
->sg_segs
;
3254 priority
= GFP_KERNEL
;
3256 priority
|= GFP_DMA
;
3257 for (b_size
= PAGE_SIZE
, order
=0;
3258 b_size
* nbr
< new_size
- STbuffer
->buffer_size
;
3259 order
++, b_size
*= 2)
3262 for (segs
= STbuffer
->sg_segs
, got
= STbuffer
->buffer_size
;
3263 segs
< max_segs
&& got
< new_size
;) {
3264 STbuffer
->sg
[segs
].address
=
3265 (unsigned char *) __get_free_pages(priority
, order
);
3266 if (STbuffer
->sg
[segs
].address
== NULL
) {
3267 if (new_size
- got
<= (max_segs
- segs
) * b_size
/ 2) {
3268 b_size
/= 2; /* Large enough for the rest of the buffers */
3272 printk(KERN_NOTICE
"st: failed to enlarge buffer to %d bytes.\n",
3274 DEB(STbuffer
->buffer_size
= got
);
3275 normalize_buffer(STbuffer
);
3278 STbuffer
->sg
[segs
].alt_address
= NULL
;
3279 STbuffer
->sg
[segs
].length
= b_size
;
3280 STbuffer
->sg_segs
+= 1;
3282 STbuffer
->buffer_size
= got
;
3285 DEBC(printk(ST_DEB_MSG
3286 "st: Succeeded to enlarge buffer to %d bytes (segs %d->%d, %d).\n",
3287 got
, STbuffer
->orig_sg_segs
, STbuffer
->sg_segs
, b_size
));
3293 /* Release the extra buffer */
3294 static void normalize_buffer(ST_buffer
* STbuffer
)
3296 int i
, order
, b_size
;
3298 for (i
= STbuffer
->orig_sg_segs
; i
< STbuffer
->sg_segs
; i
++) {
3299 for (b_size
=PAGE_SIZE
, order
=0; b_size
< STbuffer
->sg
[i
].length
;
3300 order
++, b_size
*= 2)
3302 free_pages((unsigned long)(STbuffer
->sg
[i
].address
), order
);
3303 STbuffer
->buffer_size
-= STbuffer
->sg
[i
].length
;
3306 if (debugging
&& STbuffer
->orig_sg_segs
< STbuffer
->sg_segs
)
3307 printk(ST_DEB_MSG
"st: Buffer at %p normalized to %d bytes (segs %d).\n",
3308 STbuffer
->sg
[0].address
, STbuffer
->buffer_size
,
3311 STbuffer
->sg_segs
= STbuffer
->orig_sg_segs
;
3315 /* Move data from the user buffer to the tape buffer. Returns zero (success) or
3316 negative error code. */
3317 static int append_to_buffer(const char *ubp
, ST_buffer
* st_bp
, int do_count
)
3319 int i
, cnt
, res
, offset
;
3321 for (i
= 0, offset
= st_bp
->buffer_bytes
;
3322 i
< st_bp
->sg_segs
&& offset
>= st_bp
->sg
[i
].length
; i
++)
3323 offset
-= st_bp
->sg
[i
].length
;
3324 if (i
== st_bp
->sg_segs
) { /* Should never happen */
3325 printk(KERN_WARNING
"st: append_to_buffer offset overflow.\n");
3328 for (; i
< st_bp
->sg_segs
&& do_count
> 0; i
++) {
3329 cnt
= st_bp
->sg
[i
].length
- offset
< do_count
?
3330 st_bp
->sg
[i
].length
- offset
: do_count
;
3331 res
= copy_from_user(st_bp
->sg
[i
].address
+ offset
, ubp
, cnt
);
3335 st_bp
->buffer_bytes
+= cnt
;
3339 if (do_count
) { /* Should never happen */
3340 printk(KERN_WARNING
"st: append_to_buffer overflow (left %d).\n",
3348 /* Move data from the tape buffer to the user buffer. Returns zero (success) or
3349 negative error code. */
3350 static int from_buffer(ST_buffer
* st_bp
, char *ubp
, int do_count
)
3352 int i
, cnt
, res
, offset
;
3354 for (i
= 0, offset
= st_bp
->read_pointer
;
3355 i
< st_bp
->sg_segs
&& offset
>= st_bp
->sg
[i
].length
; i
++)
3356 offset
-= st_bp
->sg
[i
].length
;
3357 if (i
== st_bp
->sg_segs
) { /* Should never happen */
3358 printk(KERN_WARNING
"st: from_buffer offset overflow.\n");
3361 for (; i
< st_bp
->sg_segs
&& do_count
> 0; i
++) {
3362 cnt
= st_bp
->sg
[i
].length
- offset
< do_count
?
3363 st_bp
->sg
[i
].length
- offset
: do_count
;
3364 res
= copy_to_user(ubp
, st_bp
->sg
[i
].address
+ offset
, cnt
);
3368 st_bp
->buffer_bytes
-= cnt
;
3369 st_bp
->read_pointer
+= cnt
;
3373 if (do_count
) { /* Should never happen */
3374 printk(KERN_WARNING
"st: from_buffer overflow (left %d).\n",
3382 /* Validate the options from command line or module parameters */
3383 static void validate_options(void)
3386 st_buffer_size
= buffer_kbs
* ST_KILOBYTE
;
3387 if (write_threshold_kbs
> 0)
3388 st_write_threshold
= write_threshold_kbs
* ST_KILOBYTE
;
3389 else if (buffer_kbs
> 0)
3390 st_write_threshold
= st_buffer_size
- 2048;
3391 if (st_write_threshold
> st_buffer_size
) {
3392 st_write_threshold
= st_buffer_size
;
3393 printk(KERN_WARNING
"st: write_threshold limited to %d bytes.\n",
3394 st_write_threshold
);
3396 if (max_buffers
>= 0)
3397 st_max_buffers
= max_buffers
;
3398 if (max_sg_segs
>= ST_FIRST_SG
)
3399 st_max_sg_segs
= max_sg_segs
;
3403 /* Set the boot options. Syntax is defined in README.st.
3405 static int __init
st_setup(char *str
)
3407 int i
, len
, ints
[5];
3410 stp
= get_options(str
, ARRAY_SIZE(ints
), ints
);
3413 for (i
= 0; i
< ints
[0] && i
< ARRAY_SIZE(parms
); i
++)
3414 *parms
[i
].val
= ints
[i
+ 1];
3416 while (stp
!= NULL
) {
3417 for (i
= 0; i
< ARRAY_SIZE(parms
); i
++) {
3418 len
= strlen(parms
[i
].name
);
3419 if (!strncmp(stp
, parms
[i
].name
, len
) &&
3420 (*(stp
+ len
) == ':' || *(stp
+ len
) == '=')) {
3422 simple_strtoul(stp
+ len
+ 1, NULL
, 0);
3426 if (i
>= sizeof(parms
) / sizeof(struct st_dev_parm
))
3427 printk(KERN_WARNING
"st: illegal parameter in '%s'\n",
3429 stp
= strchr(stp
, ',');
3440 __setup("st=", st_setup
);
3445 static struct file_operations st_fops
=
3453 release
: st_release
,
3456 static int st_attach(Scsi_Device
* SDp
)
3461 int i
, mode
, target_nbr
;
3462 unsigned long flags
= 0;
3464 if (SDp
->type
!= TYPE_TAPE
)
3467 write_lock_irqsave(&st_dev_arr_lock
, flags
);
3468 if (st_template
.nr_dev
>= st_template
.dev_max
) {
3473 tmp_dev_max
= st_template
.nr_dev
+ ST_DEV_ARR_LUMP
;
3474 if (tmp_dev_max
> ST_MAX_TAPES
)
3475 tmp_dev_max
= ST_MAX_TAPES
;
3476 if (tmp_dev_max
<= st_template
.nr_dev
) {
3478 write_unlock_irqrestore(&st_dev_arr_lock
, flags
);
3479 printk(KERN_ERR
"st: Too many tape devices (max. %d).\n",
3484 tmp_da
= kmalloc(tmp_dev_max
* sizeof(Scsi_Tape
*), GFP_ATOMIC
);
3485 tmp_ba
= kmalloc(tmp_dev_max
* sizeof(ST_buffer
*), GFP_ATOMIC
);
3486 if (tmp_da
== NULL
|| tmp_ba
== NULL
) {
3490 write_unlock_irqrestore(&st_dev_arr_lock
, flags
);
3491 printk(KERN_ERR
"st: Can't extend device array.\n");
3495 memset(tmp_da
, 0, tmp_dev_max
* sizeof(Scsi_Tape
*));
3496 if (scsi_tapes
!= NULL
) {
3497 memcpy(tmp_da
, scsi_tapes
,
3498 st_template
.dev_max
* sizeof(Scsi_Tape
*));
3501 scsi_tapes
= tmp_da
;
3503 memset(tmp_ba
, 0, tmp_dev_max
* sizeof(ST_buffer
*));
3504 if (st_buffers
!= NULL
) {
3505 memcpy(tmp_ba
, st_buffers
,
3506 st_template
.dev_max
* sizeof(ST_buffer
*));
3509 st_buffers
= tmp_ba
;
3511 st_template
.dev_max
= tmp_dev_max
;
3514 for (i
= 0; i
< st_template
.dev_max
; i
++)
3515 if (scsi_tapes
[i
] == NULL
)
3517 if (i
>= st_template
.dev_max
)
3518 panic("scsi_devices corrupt (st)");
3520 tpnt
= kmalloc(sizeof(Scsi_Tape
), GFP_ATOMIC
);
3523 write_unlock_irqrestore(&st_dev_arr_lock
, flags
);
3524 printk(KERN_ERR
"st: Can't allocate device descriptor.\n");
3527 memset(tpnt
, 0, sizeof(Scsi_Tape
));
3528 scsi_tapes
[i
] = tpnt
;
3530 for (mode
= 0; mode
< ST_NBR_MODES
; ++mode
) {
3532 static char *formats
[ST_NBR_MODES
] ={"", "l", "m", "a"};
3535 sprintf (name
, "mt%s", formats
[mode
]);
3537 devfs_register (SDp
->de
, name
, DEVFS_FL_DEFAULT
,
3538 MAJOR_NR
, i
+ (mode
<< 5),
3539 S_IFCHR
| S_IRUGO
| S_IWUGO
,
3541 /* No-rewind entry */
3542 sprintf (name
, "mt%sn", formats
[mode
]);
3544 devfs_register (SDp
->de
, name
, DEVFS_FL_DEFAULT
,
3545 MAJOR_NR
, i
+ (mode
<< 5) + 128,
3546 S_IFCHR
| S_IRUGO
| S_IWUGO
,
3549 devfs_register_tape (tpnt
->de_r
[0]);
3551 if (SDp
->scsi_level
<= 2)
3552 tpnt
->tape_type
= MT_ISSCSI1
;
3554 tpnt
->tape_type
= MT_ISSCSI2
;
3557 tpnt
->devt
= MKDEV(SCSI_TAPE_MAJOR
, i
);
3560 tpnt
->drv_buffer
= 1; /* Try buffering if no mode sense */
3561 tpnt
->restr_dma
= (SDp
->host
)->unchecked_isa_dma
;
3563 tpnt
->do_auto_lock
= ST_AUTO_LOCK
;
3564 tpnt
->can_bsr
= ST_IN_FILE_POS
;
3565 tpnt
->can_partitions
= 0;
3566 tpnt
->two_fm
= ST_TWO_FM
;
3567 tpnt
->fast_mteom
= ST_FAST_MTEOM
;
3568 tpnt
->scsi2_logical
= ST_SCSI2LOGICAL
;
3569 tpnt
->write_threshold
= st_write_threshold
;
3570 tpnt
->default_drvbuffer
= 0xff; /* No forced buffering */
3571 tpnt
->partition
= 0;
3572 tpnt
->new_partition
= 0;
3573 tpnt
->nbr_partitions
= 0;
3574 tpnt
->timeout
= ST_TIMEOUT
;
3575 tpnt
->long_timeout
= ST_LONG_TIMEOUT
;
3577 for (i
= 0; i
< ST_NBR_MODES
; i
++) {
3578 STm
= &(tpnt
->modes
[i
]);
3579 STm
->defined
= FALSE
;
3580 STm
->sysv
= ST_SYSV
;
3581 STm
->defaults_for_writes
= 0;
3582 STm
->do_async_writes
= ST_ASYNC_WRITES
;
3583 STm
->do_buffer_writes
= ST_BUFFER_WRITES
;
3584 STm
->do_read_ahead
= ST_READ_AHEAD
;
3585 STm
->default_compression
= ST_DONT_TOUCH
;
3586 STm
->default_blksize
= (-1); /* No forced size */
3587 STm
->default_density
= (-1); /* No forced density */
3590 for (i
= 0; i
< ST_NBR_PARTITIONS
; i
++) {
3591 STps
= &(tpnt
->ps
[i
]);
3593 STps
->eof
= ST_NOEOF
;
3595 STps
->last_block_valid
= FALSE
;
3596 STps
->drv_block
= (-1);
3597 STps
->drv_file
= (-1);
3600 tpnt
->current_mode
= 0;
3601 tpnt
->modes
[0].defined
= TRUE
;
3603 tpnt
->density_changed
= tpnt
->compression_changed
=
3604 tpnt
->blksize_changed
= FALSE
;
3605 init_MUTEX(&tpnt
->lock
);
3607 st_template
.nr_dev
++;
3608 write_unlock_irqrestore(&st_dev_arr_lock
, flags
);
3610 /* See if we need to allocate more static buffers */
3611 target_nbr
= st_template
.nr_dev
;
3612 if (target_nbr
> st_max_buffers
)
3613 target_nbr
= st_max_buffers
;
3614 for (i
=st_nbr_buffers
; i
< target_nbr
; i
++)
3615 if (!new_tape_buffer(TRUE
, TRUE
, FALSE
)) {
3616 printk(KERN_INFO
"st: Unable to allocate new static buffer.\n");
3619 /* If the previous allocation fails, we will try again when the buffer is
3625 static int st_detect(Scsi_Device
* SDp
)
3627 if (SDp
->type
!= TYPE_TAPE
)
3631 "Detected scsi tape st%d at scsi%d, channel %d, id %d, lun %d\n",
3632 st_template
.dev_noticed
++,
3633 SDp
->host
->host_no
, SDp
->channel
, SDp
->id
, SDp
->lun
);
3638 static int st_registered
= 0;
3640 /* Driver initialization (not __init because may be called later) */
3641 static int st_init()
3643 unsigned long flags
;
3645 if (st_template
.dev_noticed
== 0 || st_registered
)
3648 printk(KERN_INFO
"st: bufsize %d, wrt %d, max init. buffers %d, s/g segs %d.\n",
3649 st_buffer_size
, st_write_threshold
, st_max_buffers
, st_max_sg_segs
);
3651 write_lock_irqsave(&st_dev_arr_lock
, flags
);
3652 if (!st_registered
) {
3653 if (devfs_register_chrdev(SCSI_TAPE_MAJOR
, "st", &st_fops
)) {
3654 write_unlock_irqrestore(&st_dev_arr_lock
, flags
);
3655 printk(KERN_ERR
"Unable to get major %d for SCSI tapes\n",
3662 st_template
.dev_max
= 0;
3664 write_unlock_irqrestore(&st_dev_arr_lock
, flags
);
3669 static void st_detach(Scsi_Device
* SDp
)
3673 unsigned long flags
;
3675 write_lock_irqsave(&st_dev_arr_lock
, flags
);
3676 for (i
= 0; i
< st_template
.dev_max
; i
++) {
3677 tpnt
= scsi_tapes
[i
];
3678 if (tpnt
!= NULL
&& tpnt
->device
== SDp
) {
3679 tpnt
->device
= NULL
;
3680 for (mode
= 0; mode
< ST_NBR_MODES
; ++mode
) {
3681 devfs_unregister (tpnt
->de_r
[mode
]);
3682 tpnt
->de_r
[mode
] = NULL
;
3683 devfs_unregister (tpnt
->de_n
[mode
]);
3684 tpnt
->de_n
[mode
] = NULL
;
3689 st_template
.nr_dev
--;
3690 st_template
.dev_noticed
--;
3691 write_unlock_irqrestore(&st_dev_arr_lock
, flags
);
3696 write_unlock_irqrestore(&st_dev_arr_lock
, flags
);
3703 int __init
init_module(void)
3707 st_template
.module
= &__this_module
;
3708 return scsi_register_module(MODULE_SCSI_DEV
, &st_template
);
3711 void cleanup_module(void)
3715 scsi_unregister_module(MODULE_SCSI_DEV
, &st_template
);
3716 devfs_unregister_chrdev(SCSI_TAPE_MAJOR
, "st");
3718 if (scsi_tapes
!= NULL
) {
3719 for (i
=0; i
< st_template
.dev_max
; ++i
)
3721 kfree(scsi_tapes
[i
]);
3723 if (st_buffers
!= NULL
) {
3724 for (i
= 0; i
< st_nbr_buffers
; i
++) {
3725 if (st_buffers
[i
] != NULL
) {
3726 st_buffers
[i
]->orig_sg_segs
= 0;
3727 normalize_buffer(st_buffers
[i
]);
3728 kfree(st_buffers
[i
]);
3734 st_template
.dev_max
= 0;
3735 printk(KERN_INFO
"st: Unloaded.\n");