2 SCSI Tape Driver for Linux version 1.1 and newer. See the accompanying
3 file Documentation/scsi/st.txt 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, Eugene Exarevsky, Eric Lee Green, Wolfgang Denk,
9 Steve Hirsch, Andreas Koppenh"ofer, Michael Leodolter, Eyal Lebedinsky,
10 Michael Schaefer, J"org Weule, and Eric Youngdale.
12 Copyright 1992 - 2002 Kai Makisara
13 email Kai.Makisara@metla.fi
15 Last modified: Tue Oct 15 22:01:04 2002 by makisara
16 Some small formal changes - aeb, 950809
18 Last modified: 18-JAN-1998 Richard Gooch <rgooch@atnf.csiro.au> Devfs support
21 static char *verstr
= "20021015";
23 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/sched.h>
29 #include <linux/init.h>
30 #include <linux/string.h>
31 #include <linux/errno.h>
32 #include <linux/mtio.h>
33 #include <linux/ioctl.h>
34 #include <linux/fcntl.h>
35 #include <linux/spinlock.h>
36 #include <linux/smp_lock.h>
37 #include <asm/uaccess.h>
39 #include <asm/system.h>
41 /* The driver prints some debugging information on the console if DEBUG
42 is defined and non-zero. */
46 /* The message level for the debug messages is currently set to KERN_NOTICE
47 so that people can easily see the messages. Later when the debugging messages
48 in the drivers are more widely classified, this may be changed to KERN_DEBUG. */
49 #define ST_DEB_MSG KERN_NOTICE
51 #define DEBC(a) if (debugging) { a ; }
57 #define MAJOR_NR SCSI_TAPE_MAJOR
58 #define DEVICE_NR(device) (minor(device) & 0x7f)
59 #include <linux/blk.h>
63 #include <scsi/scsi_ioctl.h>
65 #define ST_KILOBYTE 1024
67 #include "st_options.h"
70 static int buffer_kbs
;
71 static int write_threshold_kbs
;
72 static int max_sg_segs
;
73 static int try_direct_io
= TRY_DIRECT_IO
;
74 static int try_rdio
= TRUE
;
75 static int try_wdio
= TRUE
;
77 static int st_dev_max
;
80 MODULE_AUTHOR("Kai Makisara");
81 MODULE_DESCRIPTION("SCSI Tape Driver");
82 MODULE_LICENSE("GPL");
84 MODULE_PARM(buffer_kbs
, "i");
85 MODULE_PARM_DESC(buffer_kbs
, "Default driver buffer size for fixed block mode (KB; 32)");
86 MODULE_PARM(write_threshold_kbs
, "i");
87 MODULE_PARM_DESC(write_threshold_kbs
, "Asynchronous write threshold (KB; 30)");
88 MODULE_PARM(max_sg_segs
, "i");
89 MODULE_PARM_DESC(max_sg_segs
, "Maximum number of scatter/gather segments to use (256)");
90 MODULE_PARM(try_direct_io
, "i");
91 MODULE_PARM_DESC(try_direct_io
, "Try direct I/O between user buffer and tape drive (1)");
93 /* Extra parameters for testing */
94 MODULE_PARM(try_rdio
, "i");
95 MODULE_PARM_DESC(try_rdio
, "Try direct read i/o when possible");
96 MODULE_PARM(try_wdio
, "i");
97 MODULE_PARM_DESC(try_wdio
, "Try direct write i/o when possible");
100 static struct st_dev_parm
{
103 } parms
[] __initdata
= {
105 "buffer_kbs", &buffer_kbs
108 "write_threshold_kbs", &write_threshold_kbs
111 "max_sg_segs", &max_sg_segs
114 "try_direct_io", &try_direct_io
120 /* The default definitions have been moved to st_options.h */
122 #define ST_FIXED_BUFFER_SIZE (ST_FIXED_BUFFER_BLOCKS * ST_KILOBYTE)
123 #define ST_WRITE_THRESHOLD (ST_WRITE_THRESHOLD_BLOCKS * ST_KILOBYTE)
125 /* The buffer size should fit into the 24 bits for length in the
126 6-byte SCSI read and write commands. */
127 #if ST_FIXED_BUFFER_SIZE >= (2 << 24 - 1)
128 #error "Buffer size should not exceed (2 << 24 - 1) bytes!"
131 DEB( static int debugging
= DEBUG
; )
133 #define MAX_RETRIES 0
134 #define MAX_WRITE_RETRIES 0
135 #define MAX_READY_RETRIES 0
136 #define NO_TAPE NOT_READY
138 #define ST_TIMEOUT (900 * HZ)
139 #define ST_LONG_TIMEOUT (14000 * HZ)
141 #define TAPE_NR(x) (minor(x) & ~(-1 << ST_MODE_SHIFT))
142 #define TAPE_MODE(x) ((minor(x) & ST_MODE_MASK) >> ST_MODE_SHIFT)
144 /* Internal ioctl to set both density (uppermost 8 bits) and blocksize (lower
146 #define SET_DENS_AND_BLK 0x10001
148 #define ST_DEV_ARR_LUMP 6
149 static rwlock_t st_dev_arr_lock
= RW_LOCK_UNLOCKED
;
151 static int st_fixed_buffer_size
= ST_FIXED_BUFFER_SIZE
;
152 static int st_write_threshold
= ST_WRITE_THRESHOLD
;
153 static int st_max_sg_segs
= ST_MAX_SG
;
155 static Scsi_Tape
**scsi_tapes
= NULL
;
157 static int modes_defined
;
159 static ST_buffer
*new_tape_buffer(int, int, int);
160 static int enlarge_buffer(ST_buffer
*, int, int);
161 static void normalize_buffer(ST_buffer
*);
162 static int append_to_buffer(const char *, ST_buffer
*, int);
163 static int from_buffer(ST_buffer
*, char *, int);
164 static void buf_to_sg(ST_buffer
*, unsigned int);
166 static int st_map_user_pages(struct scatterlist
*, const unsigned int,
167 unsigned long, size_t, int, unsigned long);
168 static int sgl_map_user_pages(struct scatterlist
*, const unsigned int,
169 unsigned long, size_t, int);
170 static int sgl_unmap_user_pages(struct scatterlist
*, const unsigned int, int);
172 static int st_attach(Scsi_Device
*);
173 static void st_detach(Scsi_Device
*);
175 static struct Scsi_Device_Template st_template
= {
176 .module
= THIS_MODULE
,
177 .list
= LIST_HEAD_INIT(st_template
.list
),
180 .scsi_type
= TYPE_TAPE
,
185 static int st_compression(Scsi_Tape
*, int);
187 static int find_partition(Scsi_Tape
*);
188 static int switch_partition(Scsi_Tape
*);
190 static int st_int_ioctl(Scsi_Tape
*, unsigned int, unsigned long);
193 #include "osst_detect.h"
194 #ifndef SIGS_FROM_OSST
195 #define SIGS_FROM_OSST \
196 {"OnStream", "SC-", "", "osst"}, \
197 {"OnStream", "DI-", "", "osst"}, \
198 {"OnStream", "DP-", "", "osst"}, \
199 {"OnStream", "USB", "", "osst"}, \
200 {"OnStream", "FW-", "", "osst"}
203 struct st_reject_data
{
207 char *driver_hint
; /* Name of the correct driver, NULL if unknown */
210 static struct st_reject_data reject_list
[] = {
211 /* {"XXX", "Yy-", "", NULL}, example */
215 /* If the device signature is on the list of incompatible drives, the
216 function returns a pointer to the name of the correct driver (if known) */
217 static char * st_incompatible(Scsi_Device
* SDp
)
219 struct st_reject_data
*rp
;
221 for (rp
=&(reject_list
[0]); rp
->vendor
!= NULL
; rp
++)
222 if (!strncmp(rp
->vendor
, SDp
->vendor
, strlen(rp
->vendor
)) &&
223 !strncmp(rp
->model
, SDp
->model
, strlen(rp
->model
)) &&
224 !strncmp(rp
->rev
, SDp
->rev
, strlen(rp
->rev
))) {
226 return rp
->driver_hint
;
234 static inline char *tape_name(Scsi_Tape
*tape
)
236 return tape
->disk
->disk_name
;
239 /* Convert the result to success code */
240 static int st_chk_result(Scsi_Tape
*STp
, Scsi_Request
* SRpnt
)
242 int result
= SRpnt
->sr_result
;
243 unsigned char *sense
= SRpnt
->sr_sense_buffer
, scode
;
244 DEB(const char *stp
;)
245 char *name
= tape_name(STp
);
248 sense
[0] = 0; /* We don't have sense data if this byte is zero */
252 if ((driver_byte(result
) & DRIVER_MASK
) == DRIVER_SENSE
)
253 scode
= sense
[2] & 0x0f;
261 printk(ST_DEB_MSG
"%s: Error: %x, cmd: %x %x %x %x %x %x Len: %d\n",
263 SRpnt
->sr_cmnd
[0], SRpnt
->sr_cmnd
[1], SRpnt
->sr_cmnd
[2],
264 SRpnt
->sr_cmnd
[3], SRpnt
->sr_cmnd
[4], SRpnt
->sr_cmnd
[5],
266 if (driver_byte(result
) & DRIVER_SENSE
)
267 print_req_sense("st", SRpnt
);
268 } else ) /* end DEB */
269 if (!(driver_byte(result
) & DRIVER_SENSE
) ||
270 ((sense
[0] & 0x70) == 0x70 &&
272 scode
!= RECOVERED_ERROR
&&
273 /* scode != UNIT_ATTENTION && */
274 scode
!= BLANK_CHECK
&&
275 scode
!= VOLUME_OVERFLOW
&&
276 SRpnt
->sr_cmnd
[0] != MODE_SENSE
&&
277 SRpnt
->sr_cmnd
[0] != TEST_UNIT_READY
)) { /* Abnormal conditions for tape */
278 if (driver_byte(result
) & DRIVER_SENSE
) {
279 printk(KERN_WARNING
"%s: Error with sense data: ", name
);
280 print_req_sense("st", SRpnt
);
283 "%s: Error %x (sugg. bt 0x%x, driver bt 0x%x, host bt 0x%x).\n",
284 name
, result
, suggestion(result
),
285 driver_byte(result
) & DRIVER_MASK
, host_byte(result
));
288 if (STp
->cln_mode
>= EXTENDED_SENSE_START
) {
289 if (STp
->cln_sense_value
)
290 STp
->cleaning_req
|= ((SRpnt
->sr_sense_buffer
[STp
->cln_mode
] &
291 STp
->cln_sense_mask
) == STp
->cln_sense_value
);
293 STp
->cleaning_req
|= ((SRpnt
->sr_sense_buffer
[STp
->cln_mode
] &
294 STp
->cln_sense_mask
) != 0);
296 if (sense
[12] == 0 && sense
[13] == 0x17) /* ASC and ASCQ => cleaning requested */
297 STp
->cleaning_req
= 1;
299 STp
->pos_unknown
|= STp
->device
->was_reset
;
301 if ((sense
[0] & 0x70) == 0x70 &&
302 scode
== RECOVERED_ERROR
303 #if ST_RECOVERED_WRITE_FATAL
304 && SRpnt
->sr_cmnd
[0] != WRITE_6
305 && SRpnt
->sr_cmnd
[0] != WRITE_FILEMARKS
308 STp
->recover_count
++;
313 if (SRpnt
->sr_cmnd
[0] == READ_6
)
315 else if (SRpnt
->sr_cmnd
[0] == WRITE_6
)
319 printk(ST_DEB_MSG
"%s: Recovered %s error (%d).\n", name
, stp
,
323 if ((sense
[2] & 0xe0) == 0)
330 /* Wakeup from interrupt */
331 static void st_sleep_done(Scsi_Cmnd
* SCpnt
)
334 Scsi_Tape
*STp
= container_of(SCpnt
->request
->rq_disk
->private_data
,
337 if ((STp
->buffer
)->writing
&&
338 (SCpnt
->sense_buffer
[0] & 0x70) == 0x70 &&
339 (SCpnt
->sense_buffer
[2] & 0x40)) {
340 /* EOM at write-behind, has all been written? */
341 if ((SCpnt
->sense_buffer
[0] & 0x80) != 0)
342 remainder
= (SCpnt
->sense_buffer
[3] << 24) |
343 (SCpnt
->sense_buffer
[4] << 16) |
344 (SCpnt
->sense_buffer
[5] << 8) |
345 SCpnt
->sense_buffer
[6];
348 if ((SCpnt
->sense_buffer
[2] & 0x0f) == VOLUME_OVERFLOW
||
350 (STp
->buffer
)->midlevel_result
= SCpnt
->result
; /* Error */
352 (STp
->buffer
)->midlevel_result
= INT_MAX
; /* OK */
354 (STp
->buffer
)->midlevel_result
= SCpnt
->result
;
355 SCpnt
->request
->rq_status
= RQ_SCSI_DONE
;
356 (STp
->buffer
)->last_SRpnt
= SCpnt
->sc_request
;
357 DEB( STp
->write_pending
= 0; )
359 complete(SCpnt
->request
->waiting
);
362 /* Do the scsi command. Waits until command performed if do_wait is true.
363 Otherwise write_behind_check() is used to check that the command
365 static Scsi_Request
*
366 st_do_scsi(Scsi_Request
* SRpnt
, Scsi_Tape
* STp
, unsigned char *cmd
, int bytes
,
367 int direction
, int timeout
, int retries
, int do_wait
)
372 SRpnt
= scsi_allocate_request(STp
->device
);
374 DEBC( printk(KERN_ERR
"%s: Can't get SCSI request.\n",
376 if (signal_pending(current
))
377 (STp
->buffer
)->syscall_result
= (-EINTR
);
379 (STp
->buffer
)->syscall_result
= (-EBUSY
);
384 init_completion(&STp
->wait
);
385 SRpnt
->sr_use_sg
= STp
->buffer
->do_dio
|| (bytes
> (STp
->buffer
)->frp
[0].length
);
386 if (SRpnt
->sr_use_sg
) {
387 if (!STp
->buffer
->do_dio
)
388 buf_to_sg(STp
->buffer
, bytes
);
389 SRpnt
->sr_use_sg
= (STp
->buffer
)->sg_segs
;
390 bp
= (char *) &((STp
->buffer
)->sg
[0]);
392 bp
= (STp
->buffer
)->b_data
;
393 SRpnt
->sr_data_direction
= direction
;
394 SRpnt
->sr_cmd_len
= 0;
395 SRpnt
->sr_request
->waiting
= &(STp
->wait
);
396 SRpnt
->sr_request
->rq_status
= RQ_SCSI_BUSY
;
397 SRpnt
->sr_request
->rq_disk
= STp
->disk
;
399 scsi_do_req(SRpnt
, (void *) cmd
, bp
, bytes
,
400 st_sleep_done
, timeout
, retries
);
403 wait_for_completion(SRpnt
->sr_request
->waiting
);
404 SRpnt
->sr_request
->waiting
= NULL
;
405 (STp
->buffer
)->syscall_result
= st_chk_result(STp
, SRpnt
);
411 /* Handle the write-behind checking (downs the semaphore) */
412 static void write_behind_check(Scsi_Tape
* STp
)
417 STbuffer
= STp
->buffer
;
420 if (STp
->write_pending
)
426 wait_for_completion(&(STp
->wait
));
427 (STp
->buffer
)->last_SRpnt
->sr_request
->waiting
= NULL
;
429 (STp
->buffer
)->syscall_result
= st_chk_result(STp
, (STp
->buffer
)->last_SRpnt
);
430 scsi_release_request((STp
->buffer
)->last_SRpnt
);
432 STbuffer
->buffer_bytes
-= STbuffer
->writing
;
433 STps
= &(STp
->ps
[STp
->partition
]);
434 if (STps
->drv_block
>= 0) {
435 if (STp
->block_size
== 0)
438 STps
->drv_block
+= STbuffer
->writing
/ STp
->block_size
;
440 STbuffer
->writing
= 0;
446 /* Step over EOF if it has been inadvertently crossed (ioctl not used because
447 it messes up the block number). */
448 static int cross_eof(Scsi_Tape
* STp
, int forward
)
451 unsigned char cmd
[MAX_COMMAND_SIZE
];
454 cmd
[1] = 0x01; /* Space FileMarks */
459 cmd
[2] = cmd
[3] = cmd
[4] = 0xff; /* -1 filemarks */
462 DEBC(printk(ST_DEB_MSG
"%s: Stepping over filemark %s.\n",
463 tape_name(STp
), forward
? "forward" : "backward"));
465 SRpnt
= st_do_scsi(NULL
, STp
, cmd
, 0, SCSI_DATA_NONE
,
466 STp
->timeout
, MAX_RETRIES
, TRUE
);
468 return (STp
->buffer
)->syscall_result
;
470 scsi_release_request(SRpnt
);
473 if ((STp
->buffer
)->midlevel_result
!= 0)
474 printk(KERN_ERR
"%s: Stepping over filemark %s failed.\n",
475 tape_name(STp
), forward
? "forward" : "backward");
477 return (STp
->buffer
)->syscall_result
;
481 /* Flush the write buffer (never need to write if variable blocksize). */
482 static int flush_write_buffer(Scsi_Tape
* STp
)
484 int offset
, transfer
, blks
;
486 unsigned char cmd
[MAX_COMMAND_SIZE
];
490 if ((STp
->buffer
)->writing
) {
491 write_behind_check(STp
);
492 if ((STp
->buffer
)->syscall_result
) {
493 DEBC(printk(ST_DEB_MSG
494 "%s: Async write error (flush) %x.\n",
495 tape_name(STp
), (STp
->buffer
)->midlevel_result
))
496 if ((STp
->buffer
)->midlevel_result
== INT_MAX
)
501 if (STp
->block_size
== 0)
505 if (STp
->dirty
== 1) {
507 offset
= (STp
->buffer
)->buffer_bytes
;
508 transfer
= ((offset
+ STp
->block_size
- 1) /
509 STp
->block_size
) * STp
->block_size
;
510 DEBC(printk(ST_DEB_MSG
"%s: Flushing %d bytes.\n",
511 tape_name(STp
), transfer
));
513 memset((STp
->buffer
)->b_data
+ offset
, 0, transfer
- offset
);
515 memset(cmd
, 0, MAX_COMMAND_SIZE
);
518 blks
= transfer
/ STp
->block_size
;
523 SRpnt
= st_do_scsi(NULL
, STp
, cmd
, transfer
, SCSI_DATA_WRITE
,
524 STp
->timeout
, MAX_WRITE_RETRIES
, TRUE
);
526 return (STp
->buffer
)->syscall_result
;
528 STps
= &(STp
->ps
[STp
->partition
]);
529 if ((STp
->buffer
)->syscall_result
!= 0) {
530 if ((SRpnt
->sr_sense_buffer
[0] & 0x70) == 0x70 &&
531 (SRpnt
->sr_sense_buffer
[2] & 0x40) &&
532 (SRpnt
->sr_sense_buffer
[2] & 0x0f) == NO_SENSE
) {
534 (STp
->buffer
)->buffer_bytes
= 0;
537 printk(KERN_ERR
"%s: Error on flush.\n",
541 STps
->drv_block
= (-1);
543 if (STps
->drv_block
>= 0)
544 STps
->drv_block
+= blks
;
546 (STp
->buffer
)->buffer_bytes
= 0;
548 scsi_release_request(SRpnt
);
555 /* Flush the tape buffer. The tape will be positioned correctly unless
556 seek_next is true. */
557 static int flush_buffer(Scsi_Tape
*STp
, int seek_next
)
559 int backspace
, result
;
563 STbuffer
= STp
->buffer
;
566 * If there was a bus reset, block further access
569 if (STp
->pos_unknown
)
572 if (STp
->ready
!= ST_READY
)
574 STps
= &(STp
->ps
[STp
->partition
]);
575 if (STps
->rw
== ST_WRITING
) /* Writing */
576 return flush_write_buffer(STp
);
578 if (STp
->block_size
== 0)
581 backspace
= ((STp
->buffer
)->buffer_bytes
+
582 (STp
->buffer
)->read_pointer
) / STp
->block_size
-
583 ((STp
->buffer
)->read_pointer
+ STp
->block_size
- 1) /
585 (STp
->buffer
)->buffer_bytes
= 0;
586 (STp
->buffer
)->read_pointer
= 0;
589 if (STps
->eof
== ST_FM_HIT
) {
590 result
= cross_eof(STp
, FALSE
); /* Back over the EOF hit */
592 STps
->eof
= ST_NOEOF
;
594 if (STps
->drv_file
>= 0)
599 if (!result
&& backspace
> 0)
600 result
= st_int_ioctl(STp
, MTBSR
, backspace
);
601 } else if (STps
->eof
== ST_FM_HIT
) {
602 if (STps
->drv_file
>= 0)
605 STps
->eof
= ST_NOEOF
;
611 /* Set the mode parameters */
612 static int set_mode_densblk(Scsi_Tape
* STp
, ST_mode
* STm
)
616 char *name
= tape_name(STp
);
618 if (!STp
->density_changed
&&
619 STm
->default_density
>= 0 &&
620 STm
->default_density
!= STp
->density
) {
621 arg
= STm
->default_density
;
625 arg
<<= MT_ST_DENSITY_SHIFT
;
626 if (!STp
->blksize_changed
&&
627 STm
->default_blksize
>= 0 &&
628 STm
->default_blksize
!= STp
->block_size
) {
629 arg
|= STm
->default_blksize
;
632 arg
|= STp
->block_size
;
634 st_int_ioctl(STp
, SET_DENS_AND_BLK
, arg
)) {
636 "%s: Can't set default block size to %d bytes and density %x.\n",
637 name
, STm
->default_blksize
, STm
->default_density
);
645 /* Lock or unlock the drive door. Don't use when Scsi_Request allocated. */
646 static int do_door_lock(Scsi_Tape
* STp
, int do_lock
)
649 DEB(char *name
= tape_name(STp
);)
652 cmd
= do_lock
? SCSI_IOCTL_DOORLOCK
: SCSI_IOCTL_DOORUNLOCK
;
653 DEBC(printk(ST_DEB_MSG
"%s: %socking drive door.\n", name
,
654 do_lock
? "L" : "Unl"));
655 retval
= scsi_ioctl(STp
->device
, cmd
, NULL
);
657 STp
->door_locked
= do_lock
? ST_LOCKED_EXPLICIT
: ST_UNLOCKED
;
660 STp
->door_locked
= ST_LOCK_FAILS
;
666 /* Set the internal state after reset */
667 static void reset_state(Scsi_Tape
*STp
)
672 STp
->pos_unknown
= 0;
673 for (i
= 0; i
< ST_NBR_PARTITIONS
; i
++) {
674 STps
= &(STp
->ps
[i
]);
676 STps
->eof
= ST_NOEOF
;
678 STps
->last_block_valid
= FALSE
;
679 STps
->drv_block
= -1;
682 if (STp
->can_partitions
) {
683 STp
->partition
= find_partition(STp
);
684 if (STp
->partition
< 0)
686 STp
->new_partition
= STp
->partition
;
690 /* Test if the drive is ready. Returns either one of the codes below or a negative system
692 #define CHKRES_READY 0
693 #define CHKRES_NEW_SESSION 1
694 #define CHKRES_NOT_READY 2
695 #define CHKRES_NO_TAPE 3
697 #define MAX_ATTENTIONS 10
699 static int test_ready(Scsi_Tape
*STp
, int do_wait
)
701 int attentions
, waits
, max_wait
, scode
;
702 int retval
= CHKRES_READY
, new_session
= FALSE
;
703 unsigned char cmd
[MAX_COMMAND_SIZE
];
704 Scsi_Request
*SRpnt
= NULL
;
706 max_wait
= do_wait
? ST_BLOCK_SECONDS
: 0;
708 for (attentions
=waits
=0; ; ) {
709 memset((void *) &cmd
[0], 0, MAX_COMMAND_SIZE
);
710 cmd
[0] = TEST_UNIT_READY
;
711 SRpnt
= st_do_scsi(SRpnt
, STp
, cmd
, 0, SCSI_DATA_NONE
,
712 STp
->long_timeout
, MAX_READY_RETRIES
, TRUE
);
715 retval
= (STp
->buffer
)->syscall_result
;
719 if ((SRpnt
->sr_sense_buffer
[0] & 0x70) == 0x70) {
721 scode
= (SRpnt
->sr_sense_buffer
[2] & 0x0f);
723 if (scode
== UNIT_ATTENTION
) { /* New media? */
725 if (attentions
< MAX_ATTENTIONS
) {
735 if (scode
== NOT_READY
) {
736 if (waits
< max_wait
) {
737 set_current_state(TASK_INTERRUPTIBLE
);
738 schedule_timeout(HZ
);
739 if (signal_pending(current
)) {
747 if ((STp
->device
)->scsi_level
>= SCSI_2
&&
748 SRpnt
->sr_sense_buffer
[12] == 0x3a) /* Check ASC */
749 retval
= CHKRES_NO_TAPE
;
751 retval
= CHKRES_NOT_READY
;
757 retval
= (STp
->buffer
)->syscall_result
;
759 retval
= new_session
? CHKRES_NEW_SESSION
: CHKRES_READY
;
764 scsi_release_request(SRpnt
);
769 /* See if the drive is ready and gather information about the tape. Return values:
770 < 0 negative error code from errno.h
772 1 drive not ready (possibly no tape)
774 static int check_tape(Scsi_Tape
*STp
, struct file
*filp
)
776 int i
, retval
, new_session
= FALSE
, do_wait
;
777 unsigned char cmd
[MAX_COMMAND_SIZE
], saved_cleaning
;
778 unsigned short st_flags
= filp
->f_flags
;
779 Scsi_Request
*SRpnt
= NULL
;
782 char *name
= tape_name(STp
);
783 struct inode
*inode
= filp
->f_dentry
->d_inode
;
784 int mode
= TAPE_MODE(inode
->i_rdev
);
786 STp
->ready
= ST_READY
;
788 if (mode
!= STp
->current_mode
) {
789 DEBC(printk(ST_DEB_MSG
"%s: Mode change from %d to %d.\n",
790 name
, STp
->current_mode
, mode
));
792 STp
->current_mode
= mode
;
794 STm
= &(STp
->modes
[STp
->current_mode
]);
796 saved_cleaning
= STp
->cleaning_req
;
797 STp
->cleaning_req
= 0;
799 do_wait
= ((filp
->f_flags
& O_NONBLOCK
) == 0);
800 retval
= test_ready(STp
, do_wait
);
805 if (retval
== CHKRES_NEW_SESSION
) {
806 STp
->pos_unknown
= 0;
807 STp
->partition
= STp
->new_partition
= 0;
808 if (STp
->can_partitions
)
809 STp
->nbr_partitions
= 1; /* This guess will be updated later
811 for (i
= 0; i
< ST_NBR_PARTITIONS
; i
++) {
812 STps
= &(STp
->ps
[i
]);
814 STps
->eof
= ST_NOEOF
;
816 STps
->last_block_valid
= FALSE
;
823 STp
->cleaning_req
|= saved_cleaning
;
825 if (retval
== CHKRES_NOT_READY
|| retval
== CHKRES_NO_TAPE
) {
826 if (retval
== CHKRES_NO_TAPE
)
827 STp
->ready
= ST_NO_TAPE
;
829 STp
->ready
= ST_NOT_READY
;
831 STp
->density
= 0; /* Clear the erroneous "residue" */
834 STp
->ps
[0].drv_file
= STp
->ps
[0].drv_block
= (-1);
835 STp
->partition
= STp
->new_partition
= 0;
836 STp
->door_locked
= ST_UNLOCKED
;
837 return CHKRES_NOT_READY
;
841 if (STp
->omit_blklims
)
842 STp
->min_block
= STp
->max_block
= (-1);
844 memset((void *) &cmd
[0], 0, MAX_COMMAND_SIZE
);
845 cmd
[0] = READ_BLOCK_LIMITS
;
847 SRpnt
= st_do_scsi(SRpnt
, STp
, cmd
, 6, SCSI_DATA_READ
, STp
->timeout
,
848 MAX_READY_RETRIES
, TRUE
);
850 retval
= (STp
->buffer
)->syscall_result
;
854 if (!SRpnt
->sr_result
&& !SRpnt
->sr_sense_buffer
[0]) {
855 STp
->max_block
= ((STp
->buffer
)->b_data
[1] << 16) |
856 ((STp
->buffer
)->b_data
[2] << 8) | (STp
->buffer
)->b_data
[3];
857 STp
->min_block
= ((STp
->buffer
)->b_data
[4] << 8) |
858 (STp
->buffer
)->b_data
[5];
859 if ( DEB( debugging
|| ) !STp
->inited
)
861 "%s: Block limits %d - %d bytes.\n", name
,
862 STp
->min_block
, STp
->max_block
);
864 STp
->min_block
= STp
->max_block
= (-1);
865 DEBC(printk(ST_DEB_MSG
"%s: Can't read block limits.\n",
870 memset((void *) &cmd
[0], 0, MAX_COMMAND_SIZE
);
874 SRpnt
= st_do_scsi(SRpnt
, STp
, cmd
, 12, SCSI_DATA_READ
, STp
->timeout
,
875 MAX_READY_RETRIES
, TRUE
);
877 retval
= (STp
->buffer
)->syscall_result
;
881 if ((STp
->buffer
)->syscall_result
!= 0) {
882 DEBC(printk(ST_DEB_MSG
"%s: No Mode Sense.\n", name
));
883 STp
->block_size
= ST_DEFAULT_BLOCK
; /* Educated guess (?) */
884 (STp
->buffer
)->syscall_result
= 0; /* Prevent error propagation */
885 STp
->drv_write_prot
= 0;
887 DEBC(printk(ST_DEB_MSG
888 "%s: Mode sense. Length %d, medium %x, WBS %x, BLL %d\n",
890 (STp
->buffer
)->b_data
[0], (STp
->buffer
)->b_data
[1],
891 (STp
->buffer
)->b_data
[2], (STp
->buffer
)->b_data
[3]));
893 if ((STp
->buffer
)->b_data
[3] >= 8) {
894 STp
->drv_buffer
= ((STp
->buffer
)->b_data
[2] >> 4) & 7;
895 STp
->density
= (STp
->buffer
)->b_data
[4];
896 STp
->block_size
= (STp
->buffer
)->b_data
[9] * 65536 +
897 (STp
->buffer
)->b_data
[10] * 256 + (STp
->buffer
)->b_data
[11];
898 DEBC(printk(ST_DEB_MSG
899 "%s: Density %x, tape length: %x, drv buffer: %d\n",
900 name
, STp
->density
, (STp
->buffer
)->b_data
[5] * 65536 +
901 (STp
->buffer
)->b_data
[6] * 256 + (STp
->buffer
)->b_data
[7],
904 STp
->drv_write_prot
= ((STp
->buffer
)->b_data
[2] & 0x80) != 0;
906 scsi_release_request(SRpnt
);
910 if (STp
->block_size
> 0)
911 (STp
->buffer
)->buffer_blocks
=
912 (STp
->buffer
)->buffer_size
/ STp
->block_size
;
914 (STp
->buffer
)->buffer_blocks
= 1;
915 (STp
->buffer
)->buffer_bytes
= (STp
->buffer
)->read_pointer
= 0;
917 DEBC(printk(ST_DEB_MSG
918 "%s: Block size: %d, buffer size: %d (%d blocks).\n", name
,
919 STp
->block_size
, (STp
->buffer
)->buffer_size
,
920 (STp
->buffer
)->buffer_blocks
));
922 if (STp
->drv_write_prot
) {
925 DEBC(printk(ST_DEB_MSG
"%s: Write protected\n", name
));
927 if ((st_flags
& O_ACCMODE
) == O_WRONLY
||
928 (st_flags
& O_ACCMODE
) == O_RDWR
) {
934 if (STp
->can_partitions
&& STp
->nbr_partitions
< 1) {
935 /* This code is reached when the device is opened for the first time
936 after the driver has been initialized with tape in the drive and the
937 partition support has been enabled. */
938 DEBC(printk(ST_DEB_MSG
939 "%s: Updating partition number in status.\n", name
));
940 if ((STp
->partition
= find_partition(STp
)) < 0) {
941 retval
= STp
->partition
;
944 STp
->new_partition
= STp
->partition
;
945 STp
->nbr_partitions
= 1; /* This guess will be updated when necessary */
948 if (new_session
) { /* Change the drive parameters for the new mode */
949 STp
->density_changed
= STp
->blksize_changed
= FALSE
;
950 STp
->compression_changed
= FALSE
;
951 if (!(STm
->defaults_for_writes
) &&
952 (retval
= set_mode_densblk(STp
, STm
)) < 0)
955 if (STp
->default_drvbuffer
!= 0xff) {
956 if (st_int_ioctl(STp
, MTSETDRVBUFFER
, STp
->default_drvbuffer
))
958 "%s: Can't set default drive buffering to %d.\n",
959 name
, STp
->default_drvbuffer
);
970 \f/* Open the device. Needs to be called with BKL only because of incrementing the SCSI host
972 static int st_open(struct inode
*inode
, struct file
*filp
)
974 int i
, retval
= (-EIO
);
977 int dev
= TAPE_NR(inode
->i_rdev
);
980 write_lock(&st_dev_arr_lock
);
981 if (dev
>= st_dev_max
|| scsi_tapes
== NULL
||
982 ((STp
= scsi_tapes
[dev
]) == NULL
)) {
983 write_unlock(&st_dev_arr_lock
);
986 filp
->private_data
= STp
;
987 name
= tape_name(STp
);
990 write_unlock(&st_dev_arr_lock
);
991 DEB( printk(ST_DEB_MSG
"%s: Device already in use.\n", name
); )
994 if(!try_module_get(STp
->device
->host
->hostt
->module
))
996 STp
->device
->access_count
++;
998 write_unlock(&st_dev_arr_lock
);
999 STp
->rew_at_close
= STp
->autorew_dev
= (minor(inode
->i_rdev
) & 0x80) == 0;
1002 if (!scsi_block_when_processing_errors(STp
->device
)) {
1007 /* See that we have at least a one page buffer available */
1008 if (!enlarge_buffer(STp
->buffer
, PAGE_SIZE
, STp
->restr_dma
)) {
1009 printk(KERN_WARNING
"%s: Can't allocate tape buffer.\n", name
);
1010 retval
= (-EOVERFLOW
);
1014 (STp
->buffer
)->writing
= 0;
1015 (STp
->buffer
)->syscall_result
= 0;
1017 STp
->write_prot
= ((filp
->f_flags
& O_ACCMODE
) == O_RDONLY
);
1020 for (i
= 0; i
< ST_NBR_PARTITIONS
; i
++) {
1021 STps
= &(STp
->ps
[i
]);
1024 STp
->recover_count
= 0;
1025 DEB( STp
->nbr_waits
= STp
->nbr_finished
= 0;
1026 STp
->nbr_requests
= STp
->nbr_dio
= STp
->nbr_pages
= STp
->nbr_combinable
= 0; )
1028 retval
= check_tape(STp
, filp
);
1031 if ((filp
->f_flags
& O_NONBLOCK
) == 0 &&
1032 retval
!= CHKRES_READY
) {
1039 normalize_buffer(STp
->buffer
);
1041 STp
->device
->access_count
--;
1042 module_put(STp
->device
->host
->hostt
->module
);
1048 /* Flush the tape buffer before close */
1049 static int st_flush(struct file
*filp
)
1051 int result
= 0, result2
;
1052 unsigned char cmd
[MAX_COMMAND_SIZE
];
1053 Scsi_Request
*SRpnt
;
1054 Scsi_Tape
*STp
= filp
->private_data
;
1055 ST_mode
*STm
= &(STp
->modes
[STp
->current_mode
]);
1056 ST_partstat
*STps
= &(STp
->ps
[STp
->partition
]);
1057 char *name
= tape_name(STp
);
1059 if (file_count(filp
) > 1)
1062 if (STps
->rw
== ST_WRITING
&& !STp
->pos_unknown
) {
1063 result
= flush_write_buffer(STp
);
1064 if (result
!= 0 && result
!= (-ENOSPC
))
1068 if (STp
->can_partitions
&&
1069 (result2
= switch_partition(STp
)) < 0) {
1070 DEBC(printk(ST_DEB_MSG
1071 "%s: switch_partition at close failed.\n", name
));
1077 DEBC( if (STp
->nbr_requests
)
1078 printk(KERN_WARNING
"%s: Number of r/w requests %d, dio used in %d, pages %d (%d).\n",
1079 name
, STp
->nbr_requests
, STp
->nbr_dio
, STp
->nbr_pages
, STp
->nbr_combinable
));
1081 if (STps
->rw
== ST_WRITING
&& !STp
->pos_unknown
) {
1083 DEBC(printk(ST_DEB_MSG
"%s: File length %ld bytes.\n",
1084 name
, (long) (filp
->f_pos
));
1085 printk(ST_DEB_MSG
"%s: Async write waits %d, finished %d.\n",
1086 name
, STp
->nbr_waits
, STp
->nbr_finished
);
1089 memset(cmd
, 0, MAX_COMMAND_SIZE
);
1090 cmd
[0] = WRITE_FILEMARKS
;
1091 cmd
[4] = 1 + STp
->two_fm
;
1093 SRpnt
= st_do_scsi(NULL
, STp
, cmd
, 0, SCSI_DATA_NONE
,
1094 STp
->timeout
, MAX_WRITE_RETRIES
, TRUE
);
1096 result
= (STp
->buffer
)->syscall_result
;
1100 if ((STp
->buffer
)->syscall_result
!= 0 &&
1101 ((SRpnt
->sr_sense_buffer
[0] & 0x70) != 0x70 ||
1102 (SRpnt
->sr_sense_buffer
[2] & 0x4f) != 0x40 ||
1103 ((SRpnt
->sr_sense_buffer
[0] & 0x80) != 0 &&
1104 (SRpnt
->sr_sense_buffer
[3] | SRpnt
->sr_sense_buffer
[4] |
1105 SRpnt
->sr_sense_buffer
[5] |
1106 SRpnt
->sr_sense_buffer
[6]) != 0))) {
1107 /* Filter out successful write at EOM */
1108 scsi_release_request(SRpnt
);
1110 printk(KERN_ERR
"%s: Error on write filemark.\n", name
);
1114 scsi_release_request(SRpnt
);
1116 if (STps
->drv_file
>= 0)
1118 STps
->drv_block
= 0;
1120 cross_eof(STp
, FALSE
);
1124 DEBC(printk(ST_DEB_MSG
"%s: Buffer flushed, %d EOF(s) written\n",
1126 } else if (!STp
->rew_at_close
) {
1127 STps
= &(STp
->ps
[STp
->partition
]);
1128 if (!STm
->sysv
|| STps
->rw
!= ST_READING
) {
1130 result
= flush_buffer(STp
, 0);
1131 else if (STps
->eof
== ST_FM_HIT
) {
1132 result
= cross_eof(STp
, FALSE
);
1134 if (STps
->drv_file
>= 0)
1136 STps
->drv_block
= 0;
1139 STps
->eof
= ST_NOEOF
;
1141 } else if ((STps
->eof
== ST_NOEOF
&&
1142 !(result
= cross_eof(STp
, TRUE
))) ||
1143 STps
->eof
== ST_FM_HIT
) {
1144 if (STps
->drv_file
>= 0)
1146 STps
->drv_block
= 0;
1152 if (STp
->rew_at_close
) {
1153 result2
= st_int_ioctl(STp
, MTREW
, 1);
1161 /* Close the device and release it. BKL is not needed: this is the only thread
1162 accessing this tape. */
1163 static int st_release(struct inode
*inode
, struct file
*filp
)
1166 Scsi_Tape
*STp
= filp
->private_data
;
1168 if (STp
->door_locked
== ST_LOCKED_AUTO
)
1169 do_door_lock(STp
, 0);
1171 normalize_buffer(STp
->buffer
);
1172 write_lock(&st_dev_arr_lock
);
1174 write_unlock(&st_dev_arr_lock
);
1175 STp
->device
->access_count
--;
1176 module_put(STp
->device
->host
->hostt
->module
);
1181 /* The checks common to both reading and writing */
1182 static ssize_t
rw_checks(Scsi_Tape
*STp
, struct file
*filp
, size_t count
, loff_t
*ppos
)
1187 * If we are in the middle of error recovery, don't let anyone
1188 * else try and use this device. Also, if error recovery fails, it
1189 * may try and take the device offline, in which case all further
1190 * access to the device is prohibited.
1192 if (!scsi_block_when_processing_errors(STp
->device
)) {
1197 if (ppos
!= &filp
->f_pos
) {
1198 /* "A request was outside the capabilities of the device." */
1203 if (STp
->ready
!= ST_READY
) {
1204 if (STp
->ready
== ST_NO_TAPE
)
1205 retval
= (-ENOMEDIUM
);
1211 if (! STp
->modes
[STp
->current_mode
].defined
) {
1218 * If there was a bus reset, block further access
1221 if (STp
->pos_unknown
) {
1231 printk(ST_DEB_MSG
"%s: Incorrect device.\n", tape_name(STp
));
1236 if (STp
->can_partitions
&&
1237 (retval
= switch_partition(STp
)) < 0)
1240 if (STp
->block_size
== 0 && STp
->max_block
> 0 &&
1241 (count
< STp
->min_block
|| count
> STp
->max_block
)) {
1246 if (STp
->do_auto_lock
&& STp
->door_locked
== ST_UNLOCKED
&&
1247 !do_door_lock(STp
, 1))
1248 STp
->door_locked
= ST_LOCKED_AUTO
;
1255 static int setup_buffering(Scsi_Tape
*STp
, const char *buf
, size_t count
, int is_read
)
1257 int i
, bufsize
, retval
= 0;
1258 ST_buffer
*STbp
= STp
->buffer
;
1261 i
= STp
->try_dio
&& try_rdio
;
1263 i
= STp
->try_dio
&& try_wdio
;
1265 i
= st_map_user_pages(&(STbp
->sg
[0]), STbp
->use_sg
,
1266 (unsigned long)buf
, count
, (is_read
? READ
: WRITE
),
1270 STbp
->buffer_bytes
= 0; /* can be used as transfer counter */
1273 STbp
->do_dio
= FALSE
; /* fall back to buffering with any error */
1274 STbp
->sg_segs
= STbp
->do_dio
;
1275 STbp
->frp_sg_current
= 0;
1279 STp
->nbr_pages
+= STbp
->do_dio
;
1280 for (i
=1; i
< STbp
->do_dio
; i
++)
1281 if (page_to_pfn(STbp
->sg
[i
].page
) == page_to_pfn(STbp
->sg
[i
-1].page
) + 1)
1282 STp
->nbr_combinable
++;
1286 STbp
->do_dio
= FALSE
;
1287 DEB( STp
->nbr_requests
++; )
1289 if (!STbp
->do_dio
) {
1290 if (STp
->block_size
)
1291 bufsize
= STp
->block_size
> st_fixed_buffer_size
?
1292 STp
->block_size
: st_fixed_buffer_size
;
1295 if (bufsize
> STbp
->buffer_size
&&
1296 !enlarge_buffer(STbp
, bufsize
, STp
->restr_dma
)) {
1297 retval
= (-EOVERFLOW
);
1300 if (STp
->block_size
)
1301 STbp
->buffer_blocks
= bufsize
/ STp
->block_size
;
1311 st_write(struct file
*filp
, const char *buf
, size_t count
, loff_t
* ppos
)
1314 ssize_t i
, do_count
, blks
, transfer
;
1318 unsigned char cmd
[MAX_COMMAND_SIZE
];
1319 const char *b_point
;
1320 Scsi_Request
*SRpnt
= NULL
;
1321 Scsi_Tape
*STp
= filp
->private_data
;
1325 char *name
= tape_name(STp
);
1327 if (down_interruptible(&STp
->lock
))
1328 return -ERESTARTSYS
;
1330 retval
= rw_checks(STp
, filp
, count
, ppos
);
1331 if (retval
|| count
== 0)
1334 /* Write must be integral number of blocks */
1335 if (STp
->block_size
!= 0 && (count
% STp
->block_size
) != 0) {
1336 printk(KERN_WARNING
"%s: Write not multiple of tape block size.\n",
1342 STm
= &(STp
->modes
[STp
->current_mode
]);
1343 STps
= &(STp
->ps
[STp
->partition
]);
1345 if (STp
->write_prot
) {
1351 if (STps
->rw
== ST_READING
) {
1352 retval
= flush_buffer(STp
, 0);
1355 STps
->rw
= ST_WRITING
;
1356 } else if (STps
->rw
!= ST_WRITING
&&
1357 STps
->drv_file
== 0 && STps
->drv_block
== 0) {
1358 if ((retval
= set_mode_densblk(STp
, STm
)) < 0)
1360 if (STm
->default_compression
!= ST_DONT_TOUCH
&&
1361 !(STp
->compression_changed
)) {
1362 if (st_compression(STp
, (STm
->default_compression
== ST_YES
))) {
1363 printk(KERN_WARNING
"%s: Can't set default compression.\n",
1365 if (modes_defined
) {
1374 if (STbp
->writing
) {
1375 write_behind_check(STp
);
1376 if (STbp
->syscall_result
) {
1377 DEBC(printk(ST_DEB_MSG
"%s: Async write error (write) %x.\n",
1378 name
, STbp
->midlevel_result
));
1379 if (STbp
->midlevel_result
== INT_MAX
)
1380 STps
->eof
= ST_EOM_OK
;
1382 STps
->eof
= ST_EOM_ERROR
;
1386 if (STps
->eof
== ST_EOM_OK
) {
1387 STps
->eof
= ST_EOD_1
; /* allow next write */
1391 else if (STps
->eof
== ST_EOM_ERROR
) {
1396 /* Check the buffer readability in cases where copy_user might catch
1397 the problems after some tape movement. */
1398 if (STp
->block_size
!= 0 &&
1400 (copy_from_user(&i
, buf
, 1) != 0 ||
1401 copy_from_user(&i
, buf
+ count
- 1, 1) != 0)) {
1406 retval
= setup_buffering(STp
, buf
, count
, FALSE
);
1412 memset(cmd
, 0, MAX_COMMAND_SIZE
);
1414 cmd
[1] = (STp
->block_size
!= 0);
1416 STps
->rw
= ST_WRITING
;
1425 if (STp
->block_size
== 0)
1428 do_count
= STbp
->buffer_blocks
* STp
->block_size
-
1430 if (do_count
> count
)
1434 i
= append_to_buffer(b_point
, STbp
, do_count
);
1441 filp
->f_pos
+= do_count
;
1442 b_point
+= do_count
;
1444 async_write
= !STbp
->do_dio
&&
1445 STm
->do_async_writes
&& STps
->eof
< ST_EOM_OK
;
1446 if (STp
->block_size
!= 0)
1447 async_write
&= count
== 0 &&
1448 (!STm
->do_buffer_writes
||
1449 STbp
->buffer_bytes
>= STp
->write_threshold
);
1451 if (STp
->block_size
!= 0 && STm
->do_buffer_writes
&&
1452 !(STp
->try_dio
&& try_wdio
) && STps
->eof
< ST_EOM_OK
&&
1453 STbp
->buffer_bytes
< STbp
->buffer_size
) {
1455 /* Don't write a buffer that is not full enough. */
1456 if (!async_write
&& count
== 0)
1460 if (STp
->block_size
== 0)
1461 blks
= transfer
= do_count
;
1464 do_count
= STbp
->buffer_bytes
;
1465 blks
= do_count
/ STp
->block_size
;
1466 transfer
= blks
* STp
->block_size
;
1468 cmd
[2] = blks
>> 16;
1472 SRpnt
= st_do_scsi(SRpnt
, STp
, cmd
, transfer
, SCSI_DATA_WRITE
,
1473 STp
->timeout
, MAX_WRITE_RETRIES
, !async_write
);
1475 retval
= STbp
->syscall_result
;
1479 STbp
->writing
= transfer
;
1480 STp
->dirty
= !(STbp
->writing
==
1481 STbp
->buffer_bytes
);
1482 SRpnt
= NULL
; /* Prevent releasing this request! */
1483 DEB( STp
->write_pending
= 1; )
1487 if (STbp
->syscall_result
!= 0) {
1488 DEBC(printk(ST_DEB_MSG
"%s: Error on write:\n", name
));
1489 if ((SRpnt
->sr_sense_buffer
[0] & 0x70) == 0x70 &&
1490 (SRpnt
->sr_sense_buffer
[2] & 0x40)) {
1491 if ((SRpnt
->sr_sense_buffer
[0] & 0x80) != 0)
1492 undone
= (SRpnt
->sr_sense_buffer
[3] << 24) |
1493 (SRpnt
->sr_sense_buffer
[4] << 16) |
1494 (SRpnt
->sr_sense_buffer
[5] << 8) |
1495 SRpnt
->sr_sense_buffer
[6];
1496 else if (STp
->block_size
== 0 &&
1497 (SRpnt
->sr_sense_buffer
[2] & 0x0f) ==
1502 if (STp
->block_size
!= 0)
1503 undone
*= STp
->block_size
;
1504 filp
->f_pos
-= undone
;
1505 if (undone
<= do_count
) {
1506 /* Only data from this write is not written */
1509 if (STp
->block_size
)
1510 blks
= (transfer
- undone
) / STp
->block_size
;
1511 STps
->eof
= ST_EOM_OK
;
1512 /* Continue in fixed block mode if all written
1513 in this request but still something left to write
1514 (retval left to zero)
1516 if (STp
->block_size
== 0 ||
1517 undone
> 0 || count
== 0)
1518 retval
= (-ENOSPC
); /* EOM within current request */
1519 DEBC(printk(ST_DEB_MSG
1520 "%s: EOM with %d bytes unwritten.\n",
1523 /* Previously buffered data not written */
1525 blks
= do_count
= 0;
1526 STps
->eof
= ST_EOM_ERROR
;
1527 STps
->drv_block
= (-1); /* Too cautious? */
1528 retval
= (-EIO
); /* EOM for old data */
1529 DEBC(printk(ST_DEB_MSG
1530 "%s: EOM with lost data.\n",
1534 filp
->f_pos
-= do_count
;
1535 STps
->drv_block
= (-1); /* Too cautious? */
1541 if (STps
->drv_block
>= 0) {
1542 if (STp
->block_size
== 0)
1543 STps
->drv_block
+= (do_count
> 0);
1545 STps
->drv_block
+= blks
;
1548 STbp
->buffer_bytes
= 0;
1553 retval
= total
- count
;
1558 if (STps
->eof
== ST_EOD_1
)
1559 STps
->eof
= ST_EOM_OK
;
1561 STps
->eof
= ST_NOEOF
;
1566 scsi_release_request(SRpnt
);
1569 sgl_unmap_user_pages(&(STbp
->sg
[0]), STbp
->do_dio
, FALSE
);
1577 /* Read data from the tape. Returns zero in the normal case, one if the
1578 eof status has changed, and the negative error code in case of a
1579 fatal error. Otherwise updates the buffer and the eof state. */
1580 static long read_tape(Scsi_Tape
*STp
, long count
, Scsi_Request
** aSRpnt
)
1582 int transfer
, blks
, bytes
;
1583 unsigned char cmd
[MAX_COMMAND_SIZE
];
1584 Scsi_Request
*SRpnt
;
1589 char *name
= tape_name(STp
);
1594 STm
= &(STp
->modes
[STp
->current_mode
]);
1595 STps
= &(STp
->ps
[STp
->partition
]);
1596 if (STps
->eof
== ST_FM_HIT
)
1600 if (STp
->block_size
== 0)
1601 blks
= bytes
= count
;
1603 if (!(STp
->try_dio
&& try_rdio
) && STm
->do_read_ahead
) {
1604 blks
= (STp
->buffer
)->buffer_blocks
;
1605 bytes
= blks
* STp
->block_size
;
1608 if (!STbp
->do_dio
&& bytes
> (STp
->buffer
)->buffer_size
)
1609 bytes
= (STp
->buffer
)->buffer_size
;
1610 blks
= bytes
/ STp
->block_size
;
1611 bytes
= blks
* STp
->block_size
;
1615 memset(cmd
, 0, MAX_COMMAND_SIZE
);
1617 cmd
[1] = (STp
->block_size
!= 0);
1618 cmd
[2] = blks
>> 16;
1623 SRpnt
= st_do_scsi(SRpnt
, STp
, cmd
, bytes
, SCSI_DATA_READ
,
1624 STp
->timeout
, MAX_RETRIES
, TRUE
);
1627 return STbp
->syscall_result
;
1629 STbp
->read_pointer
= 0;
1632 /* Something to check */
1633 if (STbp
->syscall_result
) {
1635 DEBC(printk(ST_DEB_MSG
"%s: Sense: %2x %2x %2x %2x %2x %2x %2x %2x\n",
1637 SRpnt
->sr_sense_buffer
[0], SRpnt
->sr_sense_buffer
[1],
1638 SRpnt
->sr_sense_buffer
[2], SRpnt
->sr_sense_buffer
[3],
1639 SRpnt
->sr_sense_buffer
[4], SRpnt
->sr_sense_buffer
[5],
1640 SRpnt
->sr_sense_buffer
[6], SRpnt
->sr_sense_buffer
[7]));
1641 if ((SRpnt
->sr_sense_buffer
[0] & 0x70) == 0x70) { /* extended sense */
1643 if ((SRpnt
->sr_sense_buffer
[2] & 0x0f) == BLANK_CHECK
)
1644 SRpnt
->sr_sense_buffer
[2] &= 0xcf; /* No need for EOM in this case */
1646 if ((SRpnt
->sr_sense_buffer
[2] & 0xe0) != 0) { /* EOF, EOM, or ILI */
1647 /* Compute the residual count */
1648 if ((SRpnt
->sr_sense_buffer
[0] & 0x80) != 0)
1649 transfer
= (SRpnt
->sr_sense_buffer
[3] << 24) |
1650 (SRpnt
->sr_sense_buffer
[4] << 16) |
1651 (SRpnt
->sr_sense_buffer
[5] << 8) |
1652 SRpnt
->sr_sense_buffer
[6];
1655 if (STp
->block_size
== 0 &&
1656 (SRpnt
->sr_sense_buffer
[2] & 0x0f) == MEDIUM_ERROR
)
1659 if (SRpnt
->sr_sense_buffer
[2] & 0x20) { /* ILI */
1660 if (STp
->block_size
== 0) {
1662 if (STps
->drv_block
>= 0)
1663 STps
->drv_block
+= 1;
1666 STbp
->buffer_bytes
= bytes
- transfer
;
1668 scsi_release_request(SRpnt
);
1669 SRpnt
= *aSRpnt
= NULL
;
1670 if (transfer
== blks
) { /* We did not get anything, error */
1671 printk(KERN_NOTICE
"%s: Incorrect block size.\n", name
);
1672 if (STps
->drv_block
>= 0)
1673 STps
->drv_block
+= blks
- transfer
+ 1;
1674 st_int_ioctl(STp
, MTBSR
, 1);
1677 /* We have some data, deliver it */
1678 STbp
->buffer_bytes
= (blks
- transfer
) *
1680 DEBC(printk(ST_DEB_MSG
1681 "%s: ILI but enough data received %ld %d.\n",
1682 name
, count
, STbp
->buffer_bytes
));
1683 if (STps
->drv_block
>= 0)
1684 STps
->drv_block
+= 1;
1685 if (st_int_ioctl(STp
, MTBSR
, 1))
1688 } else if (SRpnt
->sr_sense_buffer
[2] & 0x80) { /* FM overrides EOM */
1689 if (STps
->eof
!= ST_FM_HIT
)
1690 STps
->eof
= ST_FM_HIT
;
1692 STps
->eof
= ST_EOD_2
;
1693 if (STp
->block_size
== 0)
1694 STbp
->buffer_bytes
= 0;
1696 STbp
->buffer_bytes
=
1697 bytes
- transfer
* STp
->block_size
;
1698 DEBC(printk(ST_DEB_MSG
1699 "%s: EOF detected (%d bytes read).\n",
1700 name
, STbp
->buffer_bytes
));
1701 } else if (SRpnt
->sr_sense_buffer
[2] & 0x40) {
1702 if (STps
->eof
== ST_FM
)
1703 STps
->eof
= ST_EOD_1
;
1705 STps
->eof
= ST_EOM_OK
;
1706 if (STp
->block_size
== 0)
1707 STbp
->buffer_bytes
= bytes
- transfer
;
1709 STbp
->buffer_bytes
=
1710 bytes
- transfer
* STp
->block_size
;
1712 DEBC(printk(ST_DEB_MSG
"%s: EOM detected (%d bytes read).\n",
1713 name
, STbp
->buffer_bytes
));
1716 /* end of EOF, EOM, ILI test */
1717 else { /* nonzero sense key */
1718 DEBC(printk(ST_DEB_MSG
1719 "%s: Tape error while reading.\n", name
));
1720 STps
->drv_block
= (-1);
1721 if (STps
->eof
== ST_FM
&&
1722 (SRpnt
->sr_sense_buffer
[2] & 0x0f) == BLANK_CHECK
) {
1723 DEBC(printk(ST_DEB_MSG
1724 "%s: Zero returned for first BLANK CHECK after EOF.\n",
1726 STps
->eof
= ST_EOD_2
; /* First BLANK_CHECK after FM */
1727 } else /* Some other extended sense code */
1731 /* End of extended sense test */
1732 else { /* Non-extended sense */
1733 retval
= STbp
->syscall_result
;
1737 /* End of error handling */
1738 else /* Read successful */
1739 STbp
->buffer_bytes
= bytes
;
1741 if (STps
->drv_block
>= 0) {
1742 if (STp
->block_size
== 0)
1745 STps
->drv_block
+= STbp
->buffer_bytes
/ STp
->block_size
;
1753 st_read(struct file
*filp
, char *buf
, size_t count
, loff_t
* ppos
)
1757 ssize_t i
, transfer
;
1759 Scsi_Request
*SRpnt
= NULL
;
1760 Scsi_Tape
*STp
= filp
->private_data
;
1763 ST_buffer
*STbp
= STp
->buffer
;
1764 DEB( char *name
= tape_name(STp
); )
1766 if (down_interruptible(&STp
->lock
))
1767 return -ERESTARTSYS
;
1769 retval
= rw_checks(STp
, filp
, count
, ppos
);
1770 if (retval
|| count
== 0)
1773 STm
= &(STp
->modes
[STp
->current_mode
]);
1774 if (!(STm
->do_read_ahead
) && STp
->block_size
!= 0 &&
1775 (count
% STp
->block_size
) != 0) {
1776 retval
= (-EINVAL
); /* Read must be integral number of blocks */
1780 STps
= &(STp
->ps
[STp
->partition
]);
1781 if (STps
->rw
== ST_WRITING
) {
1782 retval
= flush_buffer(STp
, 0);
1785 STps
->rw
= ST_READING
;
1788 if (debugging
&& STps
->eof
!= ST_NOEOF
)
1789 printk(ST_DEB_MSG
"%s: EOF/EOM flag up (%d). Bytes %d\n", name
,
1790 STps
->eof
, STbp
->buffer_bytes
);
1793 retval
= setup_buffering(STp
, buf
, count
, TRUE
);
1797 if (STbp
->buffer_bytes
== 0 &&
1798 STps
->eof
>= ST_EOD_1
) {
1799 if (STps
->eof
< ST_EOD
) {
1804 retval
= (-EIO
); /* EOM or Blank Check */
1808 if (!STbp
->do_dio
) {
1809 /* Check the buffer writability before any tape movement. Don't alter
1811 if (copy_from_user(&i
, buf
, 1) != 0 ||
1812 copy_to_user(buf
, &i
, 1) != 0 ||
1813 copy_from_user(&i
, buf
+ count
- 1, 1) != 0 ||
1814 copy_to_user(buf
+ count
- 1, &i
, 1) != 0) {
1820 STps
->rw
= ST_READING
;
1823 /* Loop until enough data in buffer or a special condition found */
1824 for (total
= 0, special
= 0; total
< count
&& !special
;) {
1826 /* Get new data if the buffer is empty */
1827 if (STbp
->buffer_bytes
== 0) {
1828 special
= read_tape(STp
, count
- total
, &SRpnt
);
1829 if (special
< 0) { /* No need to continue read */
1835 /* Move the data from driver buffer to user buffer */
1836 if (STbp
->buffer_bytes
> 0) {
1838 if (debugging
&& STps
->eof
!= ST_NOEOF
)
1840 "%s: EOF up (%d). Left %d, needed %d.\n", name
,
1841 STps
->eof
, STbp
->buffer_bytes
,
1844 transfer
= STbp
->buffer_bytes
< count
- total
?
1845 STbp
->buffer_bytes
: count
- total
;
1846 if (!STbp
->do_dio
) {
1847 i
= from_buffer(STbp
, buf
, transfer
);
1853 filp
->f_pos
+= transfer
;
1858 if (STp
->block_size
== 0)
1859 break; /* Read only one variable length block */
1861 } /* for (total = 0, special = 0;
1862 total < count && !special; ) */
1864 /* Change the eof state if no data from tape or buffer */
1866 if (STps
->eof
== ST_FM_HIT
) {
1868 STps
->drv_block
= 0;
1869 if (STps
->drv_file
>= 0)
1871 } else if (STps
->eof
== ST_EOD_1
) {
1872 STps
->eof
= ST_EOD_2
;
1873 STps
->drv_block
= 0;
1874 if (STps
->drv_file
>= 0)
1876 } else if (STps
->eof
== ST_EOD_2
)
1878 } else if (STps
->eof
== ST_FM
)
1879 STps
->eof
= ST_NOEOF
;
1883 if (SRpnt
!= NULL
) {
1884 scsi_release_request(SRpnt
);
1888 sgl_unmap_user_pages(&(STbp
->sg
[0]), STbp
->do_dio
, TRUE
);
1890 STbp
->buffer_bytes
= 0;
1899 /* Set the driver options */
1900 static void st_log_options(Scsi_Tape
* STp
, ST_mode
* STm
, char *name
)
1903 "%s: Mode %d options: buffer writes: %d, async writes: %d, read ahead: %d\n",
1904 name
, STp
->current_mode
, STm
->do_buffer_writes
, STm
->do_async_writes
,
1905 STm
->do_read_ahead
);
1907 "%s: can bsr: %d, two FMs: %d, fast mteom: %d, auto lock: %d,\n",
1908 name
, STp
->can_bsr
, STp
->two_fm
, STp
->fast_mteom
, STp
->do_auto_lock
);
1910 "%s: defs for wr: %d, no block limits: %d, partitions: %d, s2 log: %d\n",
1911 name
, STm
->defaults_for_writes
, STp
->omit_blklims
, STp
->can_partitions
,
1912 STp
->scsi2_logical
);
1914 "%s: sysv: %d nowait: %d\n", name
, STm
->sysv
, STp
->immediate
);
1915 DEB(printk(KERN_INFO
1916 "%s: debugging: %d\n",
1921 static int st_set_options(Scsi_Tape
*STp
, long options
)
1926 char *name
= tape_name(STp
);
1928 STm
= &(STp
->modes
[STp
->current_mode
]);
1929 if (!STm
->defined
) {
1930 memcpy(STm
, &(STp
->modes
[0]), sizeof(ST_mode
));
1931 modes_defined
= TRUE
;
1932 DEBC(printk(ST_DEB_MSG
1933 "%s: Initialized mode %d definition from mode 0\n",
1934 name
, STp
->current_mode
));
1937 code
= options
& MT_ST_OPTIONS
;
1938 if (code
== MT_ST_BOOLEANS
) {
1939 STm
->do_buffer_writes
= (options
& MT_ST_BUFFER_WRITES
) != 0;
1940 STm
->do_async_writes
= (options
& MT_ST_ASYNC_WRITES
) != 0;
1941 STm
->defaults_for_writes
= (options
& MT_ST_DEF_WRITES
) != 0;
1942 STm
->do_read_ahead
= (options
& MT_ST_READ_AHEAD
) != 0;
1943 STp
->two_fm
= (options
& MT_ST_TWO_FM
) != 0;
1944 STp
->fast_mteom
= (options
& MT_ST_FAST_MTEOM
) != 0;
1945 STp
->do_auto_lock
= (options
& MT_ST_AUTO_LOCK
) != 0;
1946 STp
->can_bsr
= (options
& MT_ST_CAN_BSR
) != 0;
1947 STp
->omit_blklims
= (options
& MT_ST_NO_BLKLIMS
) != 0;
1948 if ((STp
->device
)->scsi_level
>= SCSI_2
)
1949 STp
->can_partitions
= (options
& MT_ST_CAN_PARTITIONS
) != 0;
1950 STp
->scsi2_logical
= (options
& MT_ST_SCSI2LOGICAL
) != 0;
1951 STp
->immediate
= (options
& MT_ST_NOWAIT
) != 0;
1952 STm
->sysv
= (options
& MT_ST_SYSV
) != 0;
1953 DEB( debugging
= (options
& MT_ST_DEBUGGING
) != 0; )
1954 st_log_options(STp
, STm
, name
);
1955 } else if (code
== MT_ST_SETBOOLEANS
|| code
== MT_ST_CLEARBOOLEANS
) {
1956 value
= (code
== MT_ST_SETBOOLEANS
);
1957 if ((options
& MT_ST_BUFFER_WRITES
) != 0)
1958 STm
->do_buffer_writes
= value
;
1959 if ((options
& MT_ST_ASYNC_WRITES
) != 0)
1960 STm
->do_async_writes
= value
;
1961 if ((options
& MT_ST_DEF_WRITES
) != 0)
1962 STm
->defaults_for_writes
= value
;
1963 if ((options
& MT_ST_READ_AHEAD
) != 0)
1964 STm
->do_read_ahead
= value
;
1965 if ((options
& MT_ST_TWO_FM
) != 0)
1966 STp
->two_fm
= value
;
1967 if ((options
& MT_ST_FAST_MTEOM
) != 0)
1968 STp
->fast_mteom
= value
;
1969 if ((options
& MT_ST_AUTO_LOCK
) != 0)
1970 STp
->do_auto_lock
= value
;
1971 if ((options
& MT_ST_CAN_BSR
) != 0)
1972 STp
->can_bsr
= value
;
1973 if ((options
& MT_ST_NO_BLKLIMS
) != 0)
1974 STp
->omit_blklims
= value
;
1975 if ((STp
->device
)->scsi_level
>= SCSI_2
&&
1976 (options
& MT_ST_CAN_PARTITIONS
) != 0)
1977 STp
->can_partitions
= value
;
1978 if ((options
& MT_ST_SCSI2LOGICAL
) != 0)
1979 STp
->scsi2_logical
= value
;
1980 if ((options
& MT_ST_NOWAIT
) != 0)
1981 STp
->immediate
= value
;
1982 if ((options
& MT_ST_SYSV
) != 0)
1985 if ((options
& MT_ST_DEBUGGING
) != 0)
1986 debugging
= value
; )
1987 st_log_options(STp
, STm
, name
);
1988 } else if (code
== MT_ST_WRITE_THRESHOLD
) {
1989 value
= (options
& ~MT_ST_OPTIONS
) * ST_KILOBYTE
;
1990 if (value
< 1 || value
> st_fixed_buffer_size
) {
1992 "%s: Write threshold %d too small or too large.\n",
1996 STp
->write_threshold
= value
;
1997 printk(KERN_INFO
"%s: Write threshold set to %d bytes.\n",
1999 } else if (code
== MT_ST_DEF_BLKSIZE
) {
2000 value
= (options
& ~MT_ST_OPTIONS
);
2001 if (value
== ~MT_ST_OPTIONS
) {
2002 STm
->default_blksize
= (-1);
2003 printk(KERN_INFO
"%s: Default block size disabled.\n", name
);
2005 STm
->default_blksize
= value
;
2006 printk(KERN_INFO
"%s: Default block size set to %d bytes.\n",
2007 name
, STm
->default_blksize
);
2008 if (STp
->ready
== ST_READY
) {
2009 STp
->blksize_changed
= FALSE
;
2010 set_mode_densblk(STp
, STm
);
2013 } else if (code
== MT_ST_TIMEOUTS
) {
2014 value
= (options
& ~MT_ST_OPTIONS
);
2015 if ((value
& MT_ST_SET_LONG_TIMEOUT
) != 0) {
2016 STp
->long_timeout
= (value
& ~MT_ST_SET_LONG_TIMEOUT
) * HZ
;
2017 printk(KERN_INFO
"%s: Long timeout set to %d seconds.\n", name
,
2018 (value
& ~MT_ST_SET_LONG_TIMEOUT
));
2020 STp
->timeout
= value
* HZ
;
2021 printk(KERN_INFO
"%s: Normal timeout set to %d seconds.\n",
2024 } else if (code
== MT_ST_SET_CLN
) {
2025 value
= (options
& ~MT_ST_OPTIONS
) & 0xff;
2027 value
< EXTENDED_SENSE_START
&& value
>= SCSI_SENSE_BUFFERSIZE
)
2029 STp
->cln_mode
= value
;
2030 STp
->cln_sense_mask
= (options
>> 8) & 0xff;
2031 STp
->cln_sense_value
= (options
>> 16) & 0xff;
2033 "%s: Cleaning request mode %d, mask %02x, value %02x\n",
2034 name
, value
, STp
->cln_sense_mask
, STp
->cln_sense_value
);
2035 } else if (code
== MT_ST_DEF_OPTIONS
) {
2036 code
= (options
& ~MT_ST_CLEAR_DEFAULT
);
2037 value
= (options
& MT_ST_CLEAR_DEFAULT
);
2038 if (code
== MT_ST_DEF_DENSITY
) {
2039 if (value
== MT_ST_CLEAR_DEFAULT
) {
2040 STm
->default_density
= (-1);
2041 printk(KERN_INFO
"%s: Density default disabled.\n",
2044 STm
->default_density
= value
& 0xff;
2045 printk(KERN_INFO
"%s: Density default set to %x\n",
2046 name
, STm
->default_density
);
2047 if (STp
->ready
== ST_READY
) {
2048 STp
->density_changed
= FALSE
;
2049 set_mode_densblk(STp
, STm
);
2052 } else if (code
== MT_ST_DEF_DRVBUFFER
) {
2053 if (value
== MT_ST_CLEAR_DEFAULT
) {
2054 STp
->default_drvbuffer
= 0xff;
2056 "%s: Drive buffer default disabled.\n", name
);
2058 STp
->default_drvbuffer
= value
& 7;
2060 "%s: Drive buffer default set to %x\n",
2061 name
, STp
->default_drvbuffer
);
2062 if (STp
->ready
== ST_READY
)
2063 st_int_ioctl(STp
, MTSETDRVBUFFER
, STp
->default_drvbuffer
);
2065 } else if (code
== MT_ST_DEF_COMPRESSION
) {
2066 if (value
== MT_ST_CLEAR_DEFAULT
) {
2067 STm
->default_compression
= ST_DONT_TOUCH
;
2069 "%s: Compression default disabled.\n", name
);
2071 if ((value
& 0xff00) != 0) {
2072 STp
->c_algo
= (value
& 0xff00) >> 8;
2073 printk(KERN_INFO
"%s: Compression algorithm set to 0x%x.\n",
2076 if ((value
& 0xff) != 0xff) {
2077 STm
->default_compression
= (value
& 1 ? ST_YES
: ST_NO
);
2078 printk(KERN_INFO
"%s: Compression default set to %x\n",
2080 if (STp
->ready
== ST_READY
) {
2081 STp
->compression_changed
= FALSE
;
2082 st_compression(STp
, (STm
->default_compression
== ST_YES
));
2093 #define MODE_HEADER_LENGTH 4
2095 /* Mode header and page byte offsets */
2096 #define MH_OFF_DATA_LENGTH 0
2097 #define MH_OFF_MEDIUM_TYPE 1
2098 #define MH_OFF_DEV_SPECIFIC 2
2099 #define MH_OFF_BDESCS_LENGTH 3
2100 #define MP_OFF_PAGE_NBR 0
2101 #define MP_OFF_PAGE_LENGTH 1
2103 /* Mode header and page bit masks */
2104 #define MH_BIT_WP 0x80
2105 #define MP_MSK_PAGE_NBR 0x3f
2107 /* Don't return block descriptors */
2108 #define MODE_SENSE_OMIT_BDESCS 0x08
2110 #define MODE_SELECT_PAGE_FORMAT 0x10
2112 /* Read a mode page into the tape buffer. The block descriptors are included
2113 if incl_block_descs is true. The page control is ored to the page number
2114 parameter, if necessary. */
2115 static int read_mode_page(Scsi_Tape
*STp
, int page
, int omit_block_descs
)
2117 unsigned char cmd
[MAX_COMMAND_SIZE
];
2118 Scsi_Request
*SRpnt
= NULL
;
2120 memset(cmd
, 0, MAX_COMMAND_SIZE
);
2121 cmd
[0] = MODE_SENSE
;
2122 if (omit_block_descs
)
2123 cmd
[1] = MODE_SENSE_OMIT_BDESCS
;
2127 SRpnt
= st_do_scsi(SRpnt
, STp
, cmd
, cmd
[4], SCSI_DATA_READ
,
2128 STp
->timeout
, 0, TRUE
);
2130 return (STp
->buffer
)->syscall_result
;
2132 scsi_release_request(SRpnt
);
2134 return (STp
->buffer
)->syscall_result
;
2138 /* Send the mode page in the tape buffer to the drive. Assumes that the mode data
2139 in the buffer is correctly formatted. */
2140 static int write_mode_page(Scsi_Tape
*STp
, int page
)
2143 unsigned char cmd
[MAX_COMMAND_SIZE
];
2144 Scsi_Request
*SRpnt
= NULL
;
2146 memset(cmd
, 0, MAX_COMMAND_SIZE
);
2147 cmd
[0] = MODE_SELECT
;
2148 cmd
[1] = MODE_SELECT_PAGE_FORMAT
;
2149 pgo
= MODE_HEADER_LENGTH
+ (STp
->buffer
)->b_data
[MH_OFF_BDESCS_LENGTH
];
2150 cmd
[4] = pgo
+ (STp
->buffer
)->b_data
[pgo
+ MP_OFF_PAGE_LENGTH
] + 2;
2152 /* Clear reserved fields */
2153 (STp
->buffer
)->b_data
[MH_OFF_DATA_LENGTH
] = 0;
2154 (STp
->buffer
)->b_data
[MH_OFF_MEDIUM_TYPE
] = 0;
2155 (STp
->buffer
)->b_data
[MH_OFF_DEV_SPECIFIC
] &= ~MH_BIT_WP
;
2156 (STp
->buffer
)->b_data
[pgo
+ MP_OFF_PAGE_NBR
] &= MP_MSK_PAGE_NBR
;
2158 SRpnt
= st_do_scsi(SRpnt
, STp
, cmd
, cmd
[4], SCSI_DATA_WRITE
,
2159 STp
->timeout
, 0, TRUE
);
2161 return (STp
->buffer
)->syscall_result
;
2163 scsi_release_request(SRpnt
);
2165 return (STp
->buffer
)->syscall_result
;
2169 #define COMPRESSION_PAGE 0x0f
2170 #define COMPRESSION_PAGE_LENGTH 16
2172 #define CP_OFF_DCE_DCC 2
2173 #define CP_OFF_C_ALGO 7
2175 #define DCE_MASK 0x80
2176 #define DCC_MASK 0x40
2177 #define RED_MASK 0x60
2180 /* Control the compression with mode page 15. Algorithm not changed if zero.
2182 The block descriptors are read and written because Sony SDT-7000 does not
2183 work without this (suggestion from Michael Schaefer <Michael.Schaefer@dlr.de>).
2184 Including block descriptors should not cause any harm to other drives. */
2186 static int st_compression(Scsi_Tape
* STp
, int state
)
2189 int mpoffs
; /* Offset to mode page start */
2190 unsigned char *b_data
= (STp
->buffer
)->b_data
;
2191 DEB( char *name
= tape_name(STp
); )
2193 if (STp
->ready
!= ST_READY
)
2196 /* Read the current page contents */
2197 retval
= read_mode_page(STp
, COMPRESSION_PAGE
, FALSE
);
2199 DEBC(printk(ST_DEB_MSG
"%s: Compression mode page not supported.\n",
2204 mpoffs
= MODE_HEADER_LENGTH
+ b_data
[MH_OFF_BDESCS_LENGTH
];
2205 DEBC(printk(ST_DEB_MSG
"%s: Compression state is %d.\n", name
,
2206 (b_data
[mpoffs
+ CP_OFF_DCE_DCC
] & DCE_MASK
? 1 : 0)));
2208 /* Check if compression can be changed */
2209 if ((b_data
[mpoffs
+ CP_OFF_DCE_DCC
] & DCC_MASK
) == 0) {
2210 DEBC(printk(ST_DEB_MSG
"%s: Compression not supported.\n", name
));
2216 b_data
[mpoffs
+ CP_OFF_DCE_DCC
] |= DCE_MASK
;
2217 if (STp
->c_algo
!= 0)
2218 b_data
[mpoffs
+ CP_OFF_C_ALGO
] = STp
->c_algo
;
2221 b_data
[mpoffs
+ CP_OFF_DCE_DCC
] &= ~DCE_MASK
;
2222 if (STp
->c_algo
!= 0)
2223 b_data
[mpoffs
+ CP_OFF_C_ALGO
] = 0; /* no compression */
2226 retval
= write_mode_page(STp
, COMPRESSION_PAGE
);
2228 DEBC(printk(ST_DEB_MSG
"%s: Compression change failed.\n", name
));
2231 DEBC(printk(ST_DEB_MSG
"%s: Compression state changed to %d.\n",
2234 STp
->compression_changed
= TRUE
;
2239 /* Process the load and unload commands (does unload if the load code is zero) */
2240 static int do_load_unload(Scsi_Tape
*STp
, struct file
*filp
, int load_code
)
2242 int retval
= (-EIO
), timeout
;
2243 DEB( char *name
= tape_name(STp
); )
2244 unsigned char cmd
[MAX_COMMAND_SIZE
];
2246 Scsi_Request
*SRpnt
;
2248 if (STp
->ready
!= ST_READY
&& !load_code
) {
2249 if (STp
->ready
== ST_NO_TAPE
)
2250 return (-ENOMEDIUM
);
2255 memset(cmd
, 0, MAX_COMMAND_SIZE
);
2256 cmd
[0] = START_STOP
;
2260 * If arg >= 1 && arg <= 6 Enhanced load/unload in HP C1553A
2262 if (load_code
>= 1 + MT_ST_HPLOADER_OFFSET
2263 && load_code
<= 6 + MT_ST_HPLOADER_OFFSET
) {
2264 DEBC(printk(ST_DEB_MSG
"%s: Enhanced %sload slot %2d.\n",
2265 name
, (cmd
[4]) ? "" : "un",
2266 load_code
- MT_ST_HPLOADER_OFFSET
));
2267 cmd
[3] = load_code
- MT_ST_HPLOADER_OFFSET
; /* MediaID field of C1553A */
2269 if (STp
->immediate
) {
2270 cmd
[1] = 1; /* Don't wait for completion */
2271 timeout
= STp
->timeout
;
2274 timeout
= STp
->long_timeout
;
2278 printk(ST_DEB_MSG
"%s: Unloading tape.\n", name
);
2280 printk(ST_DEB_MSG
"%s: Loading tape.\n", name
);
2283 SRpnt
= st_do_scsi(NULL
, STp
, cmd
, 0, SCSI_DATA_NONE
,
2284 timeout
, MAX_RETRIES
, TRUE
);
2286 return (STp
->buffer
)->syscall_result
;
2288 retval
= (STp
->buffer
)->syscall_result
;
2289 scsi_release_request(SRpnt
);
2291 if (!retval
) { /* SCSI command successful */
2294 STp
->rew_at_close
= 0;
2295 STp
->ready
= ST_NO_TAPE
;
2298 STp
->rew_at_close
= STp
->autorew_dev
;
2299 retval
= check_tape(STp
, filp
);
2305 STps
= &(STp
->ps
[STp
->partition
]);
2306 STps
->drv_file
= STps
->drv_block
= (-1);
2313 /* Internal ioctl function */
2314 static int st_int_ioctl(Scsi_Tape
*STp
, unsigned int cmd_in
, unsigned long arg
)
2320 unsigned char cmd
[MAX_COMMAND_SIZE
];
2321 Scsi_Request
*SRpnt
;
2323 int fileno
, blkno
, at_sm
, undone
;
2324 int datalen
= 0, direction
= SCSI_DATA_NONE
;
2325 char *name
= tape_name(STp
);
2327 if (STp
->ready
!= ST_READY
) {
2328 if (STp
->ready
== ST_NO_TAPE
)
2329 return (-ENOMEDIUM
);
2333 timeout
= STp
->long_timeout
;
2334 STps
= &(STp
->ps
[STp
->partition
]);
2335 fileno
= STps
->drv_file
;
2336 blkno
= STps
->drv_block
;
2337 at_sm
= STps
->at_sm
;
2339 memset(cmd
, 0, MAX_COMMAND_SIZE
);
2342 chg_eof
= FALSE
; /* Changed from the FSF after this */
2345 cmd
[1] = 0x01; /* Space FileMarks */
2346 cmd
[2] = (arg
>> 16);
2347 cmd
[3] = (arg
>> 8);
2349 DEBC(printk(ST_DEB_MSG
"%s: Spacing tape forward over %d filemarks.\n",
2350 name
, cmd
[2] * 65536 + cmd
[3] * 256 + cmd
[4]));
2354 at_sm
&= (arg
== 0);
2357 chg_eof
= FALSE
; /* Changed from the FSF after this */
2360 cmd
[1] = 0x01; /* Space FileMarks */
2362 cmd
[2] = (ltmp
>> 16);
2363 cmd
[3] = (ltmp
>> 8);
2368 ltmp
= ltmp
| (cmd
[2] << 16) | (cmd
[3] << 8) | cmd
[4];
2370 "%s: Spacing tape backward over %ld filemarks.\n",
2375 blkno
= (-1); /* We can't know the block number */
2376 at_sm
&= (arg
== 0);
2380 cmd
[1] = 0x00; /* Space Blocks */
2381 cmd
[2] = (arg
>> 16);
2382 cmd
[3] = (arg
>> 8);
2384 DEBC(printk(ST_DEB_MSG
"%s: Spacing tape forward %d blocks.\n", name
,
2385 cmd
[2] * 65536 + cmd
[3] * 256 + cmd
[4]));
2388 at_sm
&= (arg
== 0);
2392 cmd
[1] = 0x00; /* Space Blocks */
2394 cmd
[2] = (ltmp
>> 16);
2395 cmd
[3] = (ltmp
>> 8);
2400 ltmp
= ltmp
| (cmd
[2] << 16) | (cmd
[3] << 8) | cmd
[4];
2402 "%s: Spacing tape backward %ld blocks.\n", name
, (-ltmp
));
2406 at_sm
&= (arg
== 0);
2410 cmd
[1] = 0x04; /* Space Setmarks */
2411 cmd
[2] = (arg
>> 16);
2412 cmd
[3] = (arg
>> 8);
2414 DEBC(printk(ST_DEB_MSG
"%s: Spacing tape forward %d setmarks.\n", name
,
2415 cmd
[2] * 65536 + cmd
[3] * 256 + cmd
[4]));
2417 blkno
= fileno
= (-1);
2423 cmd
[1] = 0x04; /* Space Setmarks */
2425 cmd
[2] = (ltmp
>> 16);
2426 cmd
[3] = (ltmp
>> 8);
2431 ltmp
= ltmp
| (cmd
[2] << 16) | (cmd
[3] << 8) | cmd
[4];
2432 printk(ST_DEB_MSG
"%s: Spacing tape backward %ld setmarks.\n",
2436 blkno
= fileno
= (-1);
2442 if (STp
->write_prot
)
2444 cmd
[0] = WRITE_FILEMARKS
;
2445 if (cmd_in
== MTWSM
)
2447 cmd
[2] = (arg
>> 16);
2448 cmd
[3] = (arg
>> 8);
2450 timeout
= STp
->timeout
;
2452 if (cmd_in
== MTWEOF
)
2453 printk(ST_DEB_MSG
"%s: Writing %d filemarks.\n", name
,
2454 cmd
[2] * 65536 + cmd
[3] * 256 + cmd
[4]);
2456 printk(ST_DEB_MSG
"%s: Writing %d setmarks.\n", name
,
2457 cmd
[2] * 65536 + cmd
[3] * 256 + cmd
[4]);
2462 at_sm
= (cmd_in
== MTWSM
);
2465 cmd
[0] = REZERO_UNIT
;
2466 if (STp
->immediate
) {
2467 cmd
[1] = 1; /* Don't wait for completion */
2468 timeout
= STp
->timeout
;
2470 DEBC(printk(ST_DEB_MSG
"%s: Rewinding tape.\n", name
));
2471 fileno
= blkno
= at_sm
= 0;
2474 DEBC(printk(ST_DEB_MSG
"%s: No op on tape.\n", name
));
2475 return 0; /* Should do something ? */
2478 cmd
[0] = START_STOP
;
2479 if (STp
->immediate
) {
2480 cmd
[1] = 1; /* Don't wait for completion */
2481 timeout
= STp
->timeout
;
2484 DEBC(printk(ST_DEB_MSG
"%s: Retensioning tape.\n", name
));
2485 fileno
= blkno
= at_sm
= 0;
2488 if (!STp
->fast_mteom
) {
2489 /* space to the end of tape */
2490 ioctl_result
= st_int_ioctl(STp
, MTFSF
, 0x7fffff);
2491 fileno
= STps
->drv_file
;
2492 if (STps
->eof
>= ST_EOD_1
)
2494 /* The next lines would hide the number of spaced FileMarks
2495 That's why I inserted the previous lines. I had no luck
2496 with detecting EOM with FSF, so we go now to EOM.
2502 DEBC(printk(ST_DEB_MSG
"%s: Spacing to end of recorded medium.\n",
2508 if (STp
->write_prot
)
2511 cmd
[1] = (arg
? 1 : 0); /* Long erase with non-zero argument */
2512 if (STp
->immediate
) {
2513 cmd
[1] |= 2; /* Don't wait for completion */
2514 timeout
= STp
->timeout
;
2517 timeout
= STp
->long_timeout
* 8;
2519 DEBC(printk(ST_DEB_MSG
"%s: Erasing tape.\n", name
));
2520 fileno
= blkno
= at_sm
= 0;
2522 case MTSETBLK
: /* Set block length */
2523 case MTSETDENSITY
: /* Set tape density */
2524 case MTSETDRVBUFFER
: /* Set drive buffering */
2525 case SET_DENS_AND_BLK
: /* Set density and block size */
2527 if (STp
->dirty
|| (STp
->buffer
)->buffer_bytes
!= 0)
2528 return (-EIO
); /* Not allowed if data in buffer */
2529 if ((cmd_in
== MTSETBLK
|| cmd_in
== SET_DENS_AND_BLK
) &&
2530 (arg
& MT_ST_BLKSIZE_MASK
) != 0 &&
2531 STp
->max_block
> 0 &&
2532 ((arg
& MT_ST_BLKSIZE_MASK
) < STp
->min_block
||
2533 (arg
& MT_ST_BLKSIZE_MASK
) > STp
->max_block
)) {
2534 printk(KERN_WARNING
"%s: Illegal block size.\n", name
);
2537 cmd
[0] = MODE_SELECT
;
2538 if ((STp
->use_pf
& USE_PF
))
2539 cmd
[1] = MODE_SELECT_PAGE_FORMAT
;
2540 cmd
[4] = datalen
= 12;
2541 direction
= SCSI_DATA_WRITE
;
2543 memset((STp
->buffer
)->b_data
, 0, 12);
2544 if (cmd_in
== MTSETDRVBUFFER
)
2545 (STp
->buffer
)->b_data
[2] = (arg
& 7) << 4;
2547 (STp
->buffer
)->b_data
[2] =
2548 STp
->drv_buffer
<< 4;
2549 (STp
->buffer
)->b_data
[3] = 8; /* block descriptor length */
2550 if (cmd_in
== MTSETDENSITY
) {
2551 (STp
->buffer
)->b_data
[4] = arg
;
2552 STp
->density_changed
= TRUE
; /* At least we tried ;-) */
2553 } else if (cmd_in
== SET_DENS_AND_BLK
)
2554 (STp
->buffer
)->b_data
[4] = arg
>> 24;
2556 (STp
->buffer
)->b_data
[4] = STp
->density
;
2557 if (cmd_in
== MTSETBLK
|| cmd_in
== SET_DENS_AND_BLK
) {
2558 ltmp
= arg
& MT_ST_BLKSIZE_MASK
;
2559 if (cmd_in
== MTSETBLK
)
2560 STp
->blksize_changed
= TRUE
; /* At least we tried ;-) */
2562 ltmp
= STp
->block_size
;
2563 (STp
->buffer
)->b_data
[9] = (ltmp
>> 16);
2564 (STp
->buffer
)->b_data
[10] = (ltmp
>> 8);
2565 (STp
->buffer
)->b_data
[11] = ltmp
;
2566 timeout
= STp
->timeout
;
2568 if (cmd_in
== MTSETBLK
|| cmd_in
== SET_DENS_AND_BLK
)
2570 "%s: Setting block size to %d bytes.\n", name
,
2571 (STp
->buffer
)->b_data
[9] * 65536 +
2572 (STp
->buffer
)->b_data
[10] * 256 +
2573 (STp
->buffer
)->b_data
[11]);
2574 if (cmd_in
== MTSETDENSITY
|| cmd_in
== SET_DENS_AND_BLK
)
2576 "%s: Setting density code to %x.\n", name
,
2577 (STp
->buffer
)->b_data
[4]);
2578 if (cmd_in
== MTSETDRVBUFFER
)
2580 "%s: Setting drive buffer code to %d.\n", name
,
2581 ((STp
->buffer
)->b_data
[2] >> 4) & 7);
2588 SRpnt
= st_do_scsi(NULL
, STp
, cmd
, datalen
, direction
,
2589 timeout
, MAX_RETRIES
, TRUE
);
2591 return (STp
->buffer
)->syscall_result
;
2593 ioctl_result
= (STp
->buffer
)->syscall_result
;
2595 if (!ioctl_result
) { /* SCSI command successful */
2596 scsi_release_request(SRpnt
);
2598 STps
->drv_block
= blkno
;
2599 STps
->drv_file
= fileno
;
2600 STps
->at_sm
= at_sm
;
2602 if (cmd_in
== MTBSFM
)
2603 ioctl_result
= st_int_ioctl(STp
, MTFSF
, 1);
2604 else if (cmd_in
== MTFSFM
)
2605 ioctl_result
= st_int_ioctl(STp
, MTBSF
, 1);
2607 if (cmd_in
== MTSETBLK
|| cmd_in
== SET_DENS_AND_BLK
) {
2608 int old_block_size
= STp
->block_size
;
2609 STp
->block_size
= arg
& MT_ST_BLKSIZE_MASK
;
2610 if (STp
->block_size
!= 0) {
2611 if (old_block_size
== 0)
2612 normalize_buffer(STp
->buffer
);
2613 (STp
->buffer
)->buffer_blocks
=
2614 (STp
->buffer
)->buffer_size
/ STp
->block_size
;
2616 (STp
->buffer
)->buffer_bytes
= (STp
->buffer
)->read_pointer
= 0;
2617 if (cmd_in
== SET_DENS_AND_BLK
)
2618 STp
->density
= arg
>> MT_ST_DENSITY_SHIFT
;
2619 } else if (cmd_in
== MTSETDRVBUFFER
)
2620 STp
->drv_buffer
= (arg
& 7);
2621 else if (cmd_in
== MTSETDENSITY
)
2624 if (cmd_in
== MTEOM
)
2626 else if (cmd_in
== MTFSF
)
2629 STps
->eof
= ST_NOEOF
;
2631 } else { /* SCSI command was not completely successful. Don't return
2632 from this block without releasing the SCSI command block! */
2634 if (SRpnt
->sr_sense_buffer
[2] & 0x40) {
2635 if (cmd_in
!= MTBSF
&& cmd_in
!= MTBSFM
&&
2636 cmd_in
!= MTBSR
&& cmd_in
!= MTBSS
)
2637 STps
->eof
= ST_EOM_OK
;
2638 STps
->drv_block
= 0;
2641 undone
= ((SRpnt
->sr_sense_buffer
[3] << 24) +
2642 (SRpnt
->sr_sense_buffer
[4] << 16) +
2643 (SRpnt
->sr_sense_buffer
[5] << 8) +
2644 SRpnt
->sr_sense_buffer
[6]);
2646 if (cmd_in
== MTWEOF
&&
2647 (SRpnt
->sr_sense_buffer
[0] & 0x70) == 0x70 &&
2648 (SRpnt
->sr_sense_buffer
[2] & 0x4f) == 0x40 &&
2649 ((SRpnt
->sr_sense_buffer
[0] & 0x80) == 0 || undone
== 0)) {
2650 ioctl_result
= 0; /* EOF written succesfully at EOM */
2653 STps
->drv_file
= fileno
;
2654 STps
->eof
= ST_NOEOF
;
2655 } else if ((cmd_in
== MTFSF
) || (cmd_in
== MTFSFM
)) {
2657 STps
->drv_file
= fileno
- undone
;
2659 STps
->drv_file
= fileno
;
2660 STps
->drv_block
= 0;
2661 STps
->eof
= ST_NOEOF
;
2662 } else if ((cmd_in
== MTBSF
) || (cmd_in
== MTBSFM
)) {
2663 if (arg
> 0 && undone
< 0) /* Some drives get this wrong */
2665 if (STps
->drv_file
>= 0)
2666 STps
->drv_file
= fileno
+ undone
;
2667 STps
->drv_block
= 0;
2668 STps
->eof
= ST_NOEOF
;
2669 } else if (cmd_in
== MTFSR
) {
2670 if (SRpnt
->sr_sense_buffer
[2] & 0x80) { /* Hit filemark */
2671 if (STps
->drv_file
>= 0)
2673 STps
->drv_block
= 0;
2676 if (blkno
>= undone
)
2677 STps
->drv_block
= blkno
- undone
;
2679 STps
->drv_block
= (-1);
2680 STps
->eof
= ST_NOEOF
;
2682 } else if (cmd_in
== MTBSR
) {
2683 if (SRpnt
->sr_sense_buffer
[2] & 0x80) { /* Hit filemark */
2685 STps
->drv_block
= (-1);
2687 if (arg
> 0 && undone
< 0) /* Some drives get this wrong */
2689 if (STps
->drv_block
>= 0)
2690 STps
->drv_block
= blkno
+ undone
;
2692 STps
->eof
= ST_NOEOF
;
2693 } else if (cmd_in
== MTEOM
) {
2694 STps
->drv_file
= (-1);
2695 STps
->drv_block
= (-1);
2697 } else if (cmd_in
== MTSETBLK
||
2698 cmd_in
== MTSETDENSITY
||
2699 cmd_in
== MTSETDRVBUFFER
||
2700 cmd_in
== SET_DENS_AND_BLK
) {
2701 if ((SRpnt
->sr_sense_buffer
[2] & 0x0f) == ILLEGAL_REQUEST
&&
2702 !(STp
->use_pf
& PF_TESTED
)) {
2703 /* Try the other possible state of Page Format if not
2705 STp
->use_pf
= !STp
->use_pf
| PF_TESTED
;
2706 scsi_release_request(SRpnt
);
2708 return st_int_ioctl(STp
, cmd_in
, arg
);
2711 STps
->eof
= ST_NOEOF
;
2713 if ((SRpnt
->sr_sense_buffer
[2] & 0x0f) == BLANK_CHECK
)
2716 scsi_release_request(SRpnt
);
2720 return ioctl_result
;
2724 /* Get the tape position. If bt == 2, arg points into a kernel space mt_loc
2727 static int get_location(Scsi_Tape
*STp
, unsigned int *block
, int *partition
,
2731 unsigned char scmd
[MAX_COMMAND_SIZE
];
2732 Scsi_Request
*SRpnt
;
2733 DEB( char *name
= tape_name(STp
); )
2735 if (STp
->ready
!= ST_READY
)
2738 memset(scmd
, 0, MAX_COMMAND_SIZE
);
2739 if ((STp
->device
)->scsi_level
< SCSI_2
) {
2740 scmd
[0] = QFA_REQUEST_BLOCK
;
2743 scmd
[0] = READ_POSITION
;
2744 if (!logical
&& !STp
->scsi2_logical
)
2747 SRpnt
= st_do_scsi(NULL
, STp
, scmd
, 20, SCSI_DATA_READ
, STp
->timeout
,
2748 MAX_READY_RETRIES
, TRUE
);
2750 return (STp
->buffer
)->syscall_result
;
2752 if ((STp
->buffer
)->syscall_result
!= 0 ||
2753 (STp
->device
->scsi_level
>= SCSI_2
&&
2754 ((STp
->buffer
)->b_data
[0] & 4) != 0)) {
2755 *block
= *partition
= 0;
2756 DEBC(printk(ST_DEB_MSG
"%s: Can't read tape position.\n", name
));
2760 if ((STp
->device
)->scsi_level
< SCSI_2
) {
2761 *block
= ((STp
->buffer
)->b_data
[0] << 16)
2762 + ((STp
->buffer
)->b_data
[1] << 8)
2763 + (STp
->buffer
)->b_data
[2];
2766 *block
= ((STp
->buffer
)->b_data
[4] << 24)
2767 + ((STp
->buffer
)->b_data
[5] << 16)
2768 + ((STp
->buffer
)->b_data
[6] << 8)
2769 + (STp
->buffer
)->b_data
[7];
2770 *partition
= (STp
->buffer
)->b_data
[1];
2771 if (((STp
->buffer
)->b_data
[0] & 0x80) &&
2772 (STp
->buffer
)->b_data
[1] == 0) /* BOP of partition 0 */
2773 STp
->ps
[0].drv_block
= STp
->ps
[0].drv_file
= 0;
2775 DEBC(printk(ST_DEB_MSG
"%s: Got tape pos. blk %d part %d.\n", name
,
2776 *block
, *partition
));
2778 scsi_release_request(SRpnt
);
2785 /* Set the tape block and partition. Negative partition means that only the
2786 block should be set in vendor specific way. */
2787 static int set_location(Scsi_Tape
*STp
, unsigned int block
, int partition
,
2794 unsigned char scmd
[MAX_COMMAND_SIZE
];
2795 Scsi_Request
*SRpnt
;
2796 DEB( char *name
= tape_name(STp
); )
2798 if (STp
->ready
!= ST_READY
)
2800 timeout
= STp
->long_timeout
;
2801 STps
= &(STp
->ps
[STp
->partition
]);
2803 DEBC(printk(ST_DEB_MSG
"%s: Setting block to %d and partition to %d.\n",
2804 name
, block
, partition
));
2805 DEB(if (partition
< 0)
2808 /* Update the location at the partition we are leaving */
2809 if ((!STp
->can_partitions
&& partition
!= 0) ||
2810 partition
>= ST_NBR_PARTITIONS
)
2812 if (partition
!= STp
->partition
) {
2813 if (get_location(STp
, &blk
, &p
, 1))
2814 STps
->last_block_valid
= FALSE
;
2816 STps
->last_block_valid
= TRUE
;
2817 STps
->last_block_visited
= blk
;
2818 DEBC(printk(ST_DEB_MSG
2819 "%s: Visited block %d for partition %d saved.\n",
2820 name
, blk
, STp
->partition
));
2824 memset(scmd
, 0, MAX_COMMAND_SIZE
);
2825 if ((STp
->device
)->scsi_level
< SCSI_2
) {
2826 scmd
[0] = QFA_SEEK_BLOCK
;
2827 scmd
[2] = (block
>> 16);
2828 scmd
[3] = (block
>> 8);
2833 scmd
[3] = (block
>> 24);
2834 scmd
[4] = (block
>> 16);
2835 scmd
[5] = (block
>> 8);
2837 if (!logical
&& !STp
->scsi2_logical
)
2839 if (STp
->partition
!= partition
) {
2841 scmd
[8] = partition
;
2842 DEBC(printk(ST_DEB_MSG
2843 "%s: Trying to change partition from %d to %d\n",
2844 name
, STp
->partition
, partition
));
2847 if (STp
->immediate
) {
2848 scmd
[1] |= 1; /* Don't wait for completion */
2849 timeout
= STp
->timeout
;
2852 SRpnt
= st_do_scsi(NULL
, STp
, scmd
, 0, SCSI_DATA_NONE
,
2853 timeout
, MAX_READY_RETRIES
, TRUE
);
2855 return (STp
->buffer
)->syscall_result
;
2857 STps
->drv_block
= STps
->drv_file
= (-1);
2858 STps
->eof
= ST_NOEOF
;
2859 if ((STp
->buffer
)->syscall_result
!= 0) {
2861 if (STp
->can_partitions
&&
2862 (STp
->device
)->scsi_level
>= SCSI_2
&&
2863 (p
= find_partition(STp
)) >= 0)
2866 if (STp
->can_partitions
) {
2867 STp
->partition
= partition
;
2868 STps
= &(STp
->ps
[partition
]);
2869 if (!STps
->last_block_valid
||
2870 STps
->last_block_visited
!= block
) {
2877 STps
->drv_block
= STps
->drv_file
= 0;
2881 scsi_release_request(SRpnt
);
2888 /* Find the current partition number for the drive status. Called from open and
2889 returns either partition number of negative error code. */
2890 static int find_partition(Scsi_Tape
*STp
)
2895 if ((i
= get_location(STp
, &block
, &partition
, 1)) < 0)
2897 if (partition
>= ST_NBR_PARTITIONS
)
2903 /* Change the partition if necessary */
2904 static int switch_partition(Scsi_Tape
*STp
)
2908 if (STp
->partition
== STp
->new_partition
)
2910 STps
= &(STp
->ps
[STp
->new_partition
]);
2911 if (!STps
->last_block_valid
)
2912 STps
->last_block_visited
= 0;
2913 return set_location(STp
, STps
->last_block_visited
, STp
->new_partition
, 1);
2916 /* Functions for reading and writing the medium partition mode page. */
2918 #define PART_PAGE 0x11
2919 #define PART_PAGE_FIXED_LENGTH 8
2921 #define PP_OFF_MAX_ADD_PARTS 2
2922 #define PP_OFF_NBR_ADD_PARTS 3
2923 #define PP_OFF_FLAGS 4
2924 #define PP_OFF_PART_UNITS 6
2925 #define PP_OFF_RESERVED 7
2927 #define PP_BIT_IDP 0x20
2928 #define PP_MSK_PSUM_MB 0x10
2930 /* Get the number of partitions on the tape. As a side effect reads the
2931 mode page into the tape buffer. */
2932 static int nbr_partitions(Scsi_Tape
*STp
)
2935 DEB( char *name
= tape_name(STp
); )
2937 if (STp
->ready
!= ST_READY
)
2940 result
= read_mode_page(STp
, PART_PAGE
, TRUE
);
2943 DEBC(printk(ST_DEB_MSG
"%s: Can't read medium partition page.\n",
2947 result
= (STp
->buffer
)->b_data
[MODE_HEADER_LENGTH
+
2948 PP_OFF_NBR_ADD_PARTS
] + 1;
2949 DEBC(printk(ST_DEB_MSG
"%s: Number of partitions %d.\n", name
, result
));
2956 /* Partition the tape into two partitions if size > 0 or one partition if
2959 The block descriptors are read and written because Sony SDT-7000 does not
2960 work without this (suggestion from Michael Schaefer <Michael.Schaefer@dlr.de>).
2962 My HP C1533A drive returns only one partition size field. This is used to
2963 set the size of partition 1. There is no size field for the default partition.
2964 Michael Schaefer's Sony SDT-7000 returns two descriptors and the second is
2965 used to set the size of partition 1 (this is what the SCSI-3 standard specifies).
2966 The following algorithm is used to accomodate both drives: if the number of
2967 partition size fields is greater than the maximum number of additional partitions
2968 in the mode page, the second field is used. Otherwise the first field is used.
2970 For Seagate DDS drives the page length must be 8 when no partitions is defined
2971 and 10 when 1 partition is defined (information from Eric Lee Green). This is
2972 is acceptable also to some other old drives and enforced if the first partition
2973 size field is used for the first additional partition size.
2975 static int partition_tape(Scsi_Tape
*STp
, int size
)
2977 char *name
= tape_name(STp
);
2979 int pgo
, psd_cnt
, psdo
;
2982 result
= read_mode_page(STp
, PART_PAGE
, FALSE
);
2984 DEBC(printk(ST_DEB_MSG
"%s: Can't read partition mode page.\n", name
));
2987 /* The mode page is in the buffer. Let's modify it and write it. */
2988 bp
= (STp
->buffer
)->b_data
;
2989 pgo
= MODE_HEADER_LENGTH
+ bp
[MH_OFF_BDESCS_LENGTH
];
2990 DEBC(printk(ST_DEB_MSG
"%s: Partition page length is %d bytes.\n",
2991 name
, bp
[pgo
+ MP_OFF_PAGE_LENGTH
] + 2));
2993 psd_cnt
= (bp
[pgo
+ MP_OFF_PAGE_LENGTH
] + 2 - PART_PAGE_FIXED_LENGTH
) / 2;
2994 psdo
= pgo
+ PART_PAGE_FIXED_LENGTH
;
2995 if (psd_cnt
> bp
[pgo
+ PP_OFF_MAX_ADD_PARTS
]) {
2996 bp
[psdo
] = bp
[psdo
+ 1] = 0xff; /* Rest of the tape */
2999 memset(bp
+ psdo
, 0, bp
[pgo
+ PP_OFF_NBR_ADD_PARTS
] * 2);
3001 DEBC(printk("%s: psd_cnt %d, max.parts %d, nbr_parts %d\n", name
,
3002 psd_cnt
, bp
[pgo
+ PP_OFF_MAX_ADD_PARTS
],
3003 bp
[pgo
+ PP_OFF_NBR_ADD_PARTS
]));
3006 bp
[pgo
+ PP_OFF_NBR_ADD_PARTS
] = 0;
3007 if (psd_cnt
<= bp
[pgo
+ PP_OFF_MAX_ADD_PARTS
])
3008 bp
[pgo
+ MP_OFF_PAGE_LENGTH
] = 6;
3009 DEBC(printk(ST_DEB_MSG
"%s: Formatting tape with one partition.\n",
3012 bp
[psdo
] = (size
>> 8) & 0xff;
3013 bp
[psdo
+ 1] = size
& 0xff;
3015 if (bp
[pgo
+ MP_OFF_PAGE_LENGTH
] < 8)
3016 bp
[pgo
+ MP_OFF_PAGE_LENGTH
] = 8;
3017 DEBC(printk(ST_DEB_MSG
3018 "%s: Formatting tape with two partitions (1 = %d MB).\n",
3021 bp
[pgo
+ PP_OFF_PART_UNITS
] = 0;
3022 bp
[pgo
+ PP_OFF_RESERVED
] = 0;
3023 bp
[pgo
+ PP_OFF_FLAGS
] = PP_BIT_IDP
| PP_MSK_PSUM_MB
;
3025 result
= write_mode_page(STp
, PART_PAGE
);
3027 printk(KERN_INFO
"%s: Partitioning of tape failed.\n", name
);
3036 /* The ioctl command */
3037 static int st_ioctl(struct inode
*inode
, struct file
*file
,
3038 unsigned int cmd_in
, unsigned long arg
)
3040 int i
, cmd_nr
, cmd_type
, bt
;
3043 Scsi_Tape
*STp
= file
->private_data
;
3046 char *name
= tape_name(STp
);
3048 if (down_interruptible(&STp
->lock
))
3049 return -ERESTARTSYS
;
3052 if (debugging
&& !STp
->in_use
) {
3053 printk(ST_DEB_MSG
"%s: Incorrect device.\n", name
);
3058 STm
= &(STp
->modes
[STp
->current_mode
]);
3059 STps
= &(STp
->ps
[STp
->partition
]);
3062 * If we are in the middle of error recovery, don't let anyone
3063 * else try and use this device. Also, if error recovery fails, it
3064 * may try and take the device offline, in which case all further
3065 * access to the device is prohibited.
3067 if (!scsi_block_when_processing_errors(STp
->device
)) {
3071 cmd_type
= _IOC_TYPE(cmd_in
);
3072 cmd_nr
= _IOC_NR(cmd_in
);
3074 if (cmd_type
== _IOC_TYPE(MTIOCTOP
) && cmd_nr
== _IOC_NR(MTIOCTOP
)) {
3077 if (_IOC_SIZE(cmd_in
) != sizeof(mtc
)) {
3082 i
= copy_from_user((char *) &mtc
, (char *) arg
, sizeof(struct mtop
));
3088 if (mtc
.mt_op
== MTSETDRVBUFFER
&& !capable(CAP_SYS_ADMIN
)) {
3090 "%s: MTSETDRVBUFFER only allowed for root.\n", name
);
3094 if (!STm
->defined
&&
3095 (mtc
.mt_op
!= MTSETDRVBUFFER
&&
3096 (mtc
.mt_count
& MT_ST_OPTIONS
) == 0)) {
3101 if (!STp
->pos_unknown
) {
3103 if (STps
->eof
== ST_FM_HIT
) {
3104 if (mtc
.mt_op
== MTFSF
|| mtc
.mt_op
== MTFSFM
||
3105 mtc
.mt_op
== MTEOM
) {
3107 if (STps
->drv_file
>= 0)
3108 STps
->drv_file
+= 1;
3109 } else if (mtc
.mt_op
== MTBSF
|| mtc
.mt_op
== MTBSFM
) {
3111 if (STps
->drv_file
>= 0)
3112 STps
->drv_file
+= 1;
3116 if (mtc
.mt_op
== MTSEEK
) {
3117 /* Old position must be restored if partition will be
3119 i
= !STp
->can_partitions
||
3120 (STp
->new_partition
!= STp
->partition
);
3122 i
= mtc
.mt_op
== MTREW
|| mtc
.mt_op
== MTOFFL
||
3123 mtc
.mt_op
== MTRETEN
|| mtc
.mt_op
== MTEOM
||
3124 mtc
.mt_op
== MTLOCK
|| mtc
.mt_op
== MTLOAD
||
3125 mtc
.mt_op
== MTFSF
|| mtc
.mt_op
== MTFSFM
||
3126 mtc
.mt_op
== MTBSF
|| mtc
.mt_op
== MTBSFM
||
3127 mtc
.mt_op
== MTCOMPRESSION
;
3129 i
= flush_buffer(STp
, i
);
3136 * If there was a bus reset, block further access
3137 * to this device. If the user wants to rewind the tape,
3138 * then reset the flag and allow access again.
3140 if (mtc
.mt_op
!= MTREW
&&
3141 mtc
.mt_op
!= MTOFFL
&&
3142 mtc
.mt_op
!= MTRETEN
&&
3143 mtc
.mt_op
!= MTERASE
&&
3144 mtc
.mt_op
!= MTSEEK
&&
3145 mtc
.mt_op
!= MTEOM
) {
3150 /* remove this when the midlevel properly clears was_reset */
3151 STp
->device
->was_reset
= 0;
3154 if (mtc
.mt_op
!= MTNOP
&& mtc
.mt_op
!= MTSETBLK
&&
3155 mtc
.mt_op
!= MTSETDENSITY
&& mtc
.mt_op
!= MTWSM
&&
3156 mtc
.mt_op
!= MTSETDRVBUFFER
&& mtc
.mt_op
!= MTSETPART
)
3157 STps
->rw
= ST_IDLE
; /* Prevent automatic WEOF and fsf */
3159 if (mtc
.mt_op
== MTOFFL
&& STp
->door_locked
!= ST_UNLOCKED
)
3160 do_door_lock(STp
, 0); /* Ignore result! */
3162 if (mtc
.mt_op
== MTSETDRVBUFFER
&&
3163 (mtc
.mt_count
& MT_ST_OPTIONS
) != 0) {
3164 retval
= st_set_options(STp
, mtc
.mt_count
);
3168 if (mtc
.mt_op
== MTSETPART
) {
3169 if (!STp
->can_partitions
||
3170 mtc
.mt_count
< 0 || mtc
.mt_count
>= ST_NBR_PARTITIONS
) {
3174 if (mtc
.mt_count
>= STp
->nbr_partitions
&&
3175 (STp
->nbr_partitions
= nbr_partitions(STp
)) < 0) {
3179 if (mtc
.mt_count
>= STp
->nbr_partitions
) {
3183 STp
->new_partition
= mtc
.mt_count
;
3188 if (mtc
.mt_op
== MTMKPART
) {
3189 if (!STp
->can_partitions
) {
3193 if ((i
= st_int_ioctl(STp
, MTREW
, 0)) < 0 ||
3194 (i
= partition_tape(STp
, mtc
.mt_count
)) < 0) {
3198 for (i
= 0; i
< ST_NBR_PARTITIONS
; i
++) {
3199 STp
->ps
[i
].rw
= ST_IDLE
;
3200 STp
->ps
[i
].at_sm
= 0;
3201 STp
->ps
[i
].last_block_valid
= FALSE
;
3203 STp
->partition
= STp
->new_partition
= 0;
3204 STp
->nbr_partitions
= 1; /* Bad guess ?-) */
3205 STps
->drv_block
= STps
->drv_file
= 0;
3210 if (mtc
.mt_op
== MTSEEK
) {
3211 i
= set_location(STp
, mtc
.mt_count
, STp
->new_partition
, 0);
3212 if (!STp
->can_partitions
)
3213 STp
->ps
[0].rw
= ST_IDLE
;
3218 if (mtc
.mt_op
== MTUNLOAD
|| mtc
.mt_op
== MTOFFL
) {
3219 retval
= do_load_unload(STp
, file
, 0);
3223 if (mtc
.mt_op
== MTLOAD
) {
3224 retval
= do_load_unload(STp
, file
, max(1, mtc
.mt_count
));
3228 if (mtc
.mt_op
== MTLOCK
|| mtc
.mt_op
== MTUNLOCK
) {
3229 retval
= do_door_lock(STp
, (mtc
.mt_op
== MTLOCK
));
3233 if (STp
->can_partitions
&& STp
->ready
== ST_READY
&&
3234 (i
= switch_partition(STp
)) < 0) {
3239 if (mtc
.mt_op
== MTCOMPRESSION
)
3240 retval
= st_compression(STp
, (mtc
.mt_count
& 1));
3242 retval
= st_int_ioctl(STp
, mtc
.mt_op
, mtc
.mt_count
);
3245 if (!STm
->defined
) {
3250 if ((i
= flush_buffer(STp
, FALSE
)) < 0) {
3254 if (STp
->can_partitions
&&
3255 (i
= switch_partition(STp
)) < 0) {
3260 if (cmd_type
== _IOC_TYPE(MTIOCGET
) && cmd_nr
== _IOC_NR(MTIOCGET
)) {
3261 struct mtget mt_status
;
3263 if (_IOC_SIZE(cmd_in
) != sizeof(struct mtget
)) {
3268 mt_status
.mt_type
= STp
->tape_type
;
3269 mt_status
.mt_dsreg
=
3270 ((STp
->block_size
<< MT_ST_BLKSIZE_SHIFT
) & MT_ST_BLKSIZE_MASK
) |
3271 ((STp
->density
<< MT_ST_DENSITY_SHIFT
) & MT_ST_DENSITY_MASK
);
3272 mt_status
.mt_blkno
= STps
->drv_block
;
3273 mt_status
.mt_fileno
= STps
->drv_file
;
3274 if (STp
->block_size
!= 0) {
3275 if (STps
->rw
== ST_WRITING
)
3276 mt_status
.mt_blkno
+=
3277 (STp
->buffer
)->buffer_bytes
/ STp
->block_size
;
3278 else if (STps
->rw
== ST_READING
)
3279 mt_status
.mt_blkno
-=
3280 ((STp
->buffer
)->buffer_bytes
+
3281 STp
->block_size
- 1) / STp
->block_size
;
3284 mt_status
.mt_gstat
= 0;
3285 if (STp
->drv_write_prot
)
3286 mt_status
.mt_gstat
|= GMT_WR_PROT(0xffffffff);
3287 if (mt_status
.mt_blkno
== 0) {
3288 if (mt_status
.mt_fileno
== 0)
3289 mt_status
.mt_gstat
|= GMT_BOT(0xffffffff);
3291 mt_status
.mt_gstat
|= GMT_EOF(0xffffffff);
3293 mt_status
.mt_erreg
= (STp
->recover_reg
<< MT_ST_SOFTERR_SHIFT
);
3294 mt_status
.mt_resid
= STp
->partition
;
3295 if (STps
->eof
== ST_EOM_OK
|| STps
->eof
== ST_EOM_ERROR
)
3296 mt_status
.mt_gstat
|= GMT_EOT(0xffffffff);
3297 else if (STps
->eof
>= ST_EOM_OK
)
3298 mt_status
.mt_gstat
|= GMT_EOD(0xffffffff);
3299 if (STp
->density
== 1)
3300 mt_status
.mt_gstat
|= GMT_D_800(0xffffffff);
3301 else if (STp
->density
== 2)
3302 mt_status
.mt_gstat
|= GMT_D_1600(0xffffffff);
3303 else if (STp
->density
== 3)
3304 mt_status
.mt_gstat
|= GMT_D_6250(0xffffffff);
3305 if (STp
->ready
== ST_READY
)
3306 mt_status
.mt_gstat
|= GMT_ONLINE(0xffffffff);
3307 if (STp
->ready
== ST_NO_TAPE
)
3308 mt_status
.mt_gstat
|= GMT_DR_OPEN(0xffffffff);
3310 mt_status
.mt_gstat
|= GMT_SM(0xffffffff);
3311 if (STm
->do_async_writes
||
3312 (STm
->do_buffer_writes
&& STp
->block_size
!= 0) ||
3313 STp
->drv_buffer
!= 0)
3314 mt_status
.mt_gstat
|= GMT_IM_REP_EN(0xffffffff);
3315 if (STp
->cleaning_req
)
3316 mt_status
.mt_gstat
|= GMT_CLN(0xffffffff);
3318 i
= copy_to_user((char *) arg
, (char *) &(mt_status
),
3319 sizeof(struct mtget
));
3325 STp
->recover_reg
= 0; /* Clear after read */
3328 } /* End of MTIOCGET */
3329 if (cmd_type
== _IOC_TYPE(MTIOCPOS
) && cmd_nr
== _IOC_NR(MTIOCPOS
)) {
3330 struct mtpos mt_pos
;
3331 if (_IOC_SIZE(cmd_in
) != sizeof(struct mtpos
)) {
3335 if ((i
= get_location(STp
, &blk
, &bt
, 0)) < 0) {
3339 mt_pos
.mt_blkno
= blk
;
3340 i
= copy_to_user((char *) arg
, (char *) (&mt_pos
), sizeof(struct mtpos
));
3346 return scsi_ioctl(STp
->device
, cmd_in
, (void *) arg
);
3354 /* Try to allocate a new tape buffer. Calling function must not hold
3357 new_tape_buffer(int from_initialization
, int need_dma
, int max_sg
)
3359 int i
, priority
, got
= 0, segs
= 0;
3362 if (from_initialization
)
3363 priority
= GFP_ATOMIC
;
3365 priority
= GFP_KERNEL
;
3367 i
= sizeof(ST_buffer
) + (max_sg
- 1) * sizeof(struct scatterlist
) +
3368 max_sg
* sizeof(struct st_buf_fragment
);
3369 tb
= kmalloc(i
, priority
);
3371 printk(KERN_NOTICE
"st: Can't allocate new tape buffer.\n");
3375 tb
->frp_segs
= tb
->orig_frp_segs
= segs
;
3376 tb
->use_sg
= max_sg
;
3378 tb
->b_data
= page_address(tb
->sg
[0].page
);
3379 tb
->frp
= (struct st_buf_fragment
*)(&(tb
->sg
[0]) + max_sg
);
3383 tb
->buffer_size
= got
;
3389 /* Try to allocate enough space in the tape buffer */
3390 static int enlarge_buffer(ST_buffer
* STbuffer
, int new_size
, int need_dma
)
3392 int segs
, nbr
, max_segs
, b_size
, priority
, order
, got
;
3394 if (new_size
<= STbuffer
->buffer_size
)
3397 if (STbuffer
->buffer_size
<= PAGE_SIZE
)
3398 normalize_buffer(STbuffer
); /* Avoid extra segment */
3400 max_segs
= STbuffer
->use_sg
;
3401 nbr
= max_segs
- STbuffer
->frp_segs
;
3405 priority
= GFP_KERNEL
;
3407 priority
|= GFP_DMA
;
3408 for (b_size
= PAGE_SIZE
, order
=0;
3409 b_size
< new_size
- STbuffer
->buffer_size
;
3410 order
++, b_size
*= 2)
3413 for (segs
= STbuffer
->frp_segs
, got
= STbuffer
->buffer_size
;
3414 segs
< max_segs
&& got
< new_size
;) {
3415 STbuffer
->frp
[segs
].page
= alloc_pages(priority
, order
);
3416 if (STbuffer
->frp
[segs
].page
== NULL
) {
3417 if (new_size
- got
<= (max_segs
- segs
) * b_size
/ 2) {
3418 b_size
/= 2; /* Large enough for the rest of the buffers */
3422 printk(KERN_NOTICE
"st: failed to enlarge buffer to %d bytes.\n",
3424 DEB(STbuffer
->buffer_size
= got
);
3425 normalize_buffer(STbuffer
);
3428 STbuffer
->frp
[segs
].length
= b_size
;
3429 STbuffer
->frp_segs
+= 1;
3431 STbuffer
->buffer_size
= got
;
3434 STbuffer
->b_data
= page_address(STbuffer
->frp
[0].page
);
3435 DEBC(printk(ST_DEB_MSG
3436 "st: Succeeded to enlarge buffer at %p to %d bytes (segs %d->%d, %d).\n",
3437 STbuffer
, got
, STbuffer
->orig_frp_segs
, STbuffer
->frp_segs
, b_size
));
3443 /* Release the extra buffer */
3444 static void normalize_buffer(ST_buffer
* STbuffer
)
3448 for (i
= STbuffer
->orig_frp_segs
; i
< STbuffer
->frp_segs
; i
++) {
3449 order
= get_order(STbuffer
->frp
[i
].length
);
3450 __free_pages(STbuffer
->frp
[i
].page
, order
);
3451 STbuffer
->buffer_size
-= STbuffer
->frp
[i
].length
;
3454 if (debugging
&& STbuffer
->orig_frp_segs
< STbuffer
->frp_segs
)
3455 printk(ST_DEB_MSG
"st: Buffer at %p normalized to %d bytes (segs %d->%d).\n",
3456 STbuffer
, STbuffer
->buffer_size
, STbuffer
->frp_segs
, STbuffer
->orig_frp_segs
);
3458 STbuffer
->frp_segs
= STbuffer
->orig_frp_segs
;
3459 STbuffer
->frp_sg_current
= 0;
3463 /* Move data from the user buffer to the tape buffer. Returns zero (success) or
3464 negative error code. */
3465 static int append_to_buffer(const char *ubp
, ST_buffer
* st_bp
, int do_count
)
3467 int i
, cnt
, res
, offset
;
3469 for (i
= 0, offset
= st_bp
->buffer_bytes
;
3470 i
< st_bp
->frp_segs
&& offset
>= st_bp
->frp
[i
].length
; i
++)
3471 offset
-= st_bp
->frp
[i
].length
;
3472 if (i
== st_bp
->frp_segs
) { /* Should never happen */
3473 printk(KERN_WARNING
"st: append_to_buffer offset overflow.\n");
3476 for (; i
< st_bp
->frp_segs
&& do_count
> 0; i
++) {
3477 cnt
= st_bp
->frp
[i
].length
- offset
< do_count
?
3478 st_bp
->frp
[i
].length
- offset
: do_count
;
3479 res
= copy_from_user(page_address(st_bp
->frp
[i
].page
) + offset
, ubp
, cnt
);
3483 st_bp
->buffer_bytes
+= cnt
;
3487 if (do_count
) { /* Should never happen */
3488 printk(KERN_WARNING
"st: append_to_buffer overflow (left %d).\n",
3496 /* Move data from the tape buffer to the user buffer. Returns zero (success) or
3497 negative error code. */
3498 static int from_buffer(ST_buffer
* st_bp
, char *ubp
, int do_count
)
3500 int i
, cnt
, res
, offset
;
3502 for (i
= 0, offset
= st_bp
->read_pointer
;
3503 i
< st_bp
->frp_segs
&& offset
>= st_bp
->frp
[i
].length
; i
++)
3504 offset
-= st_bp
->frp
[i
].length
;
3505 if (i
== st_bp
->frp_segs
) { /* Should never happen */
3506 printk(KERN_WARNING
"st: from_buffer offset overflow.\n");
3509 for (; i
< st_bp
->frp_segs
&& do_count
> 0; i
++) {
3510 cnt
= st_bp
->frp
[i
].length
- offset
< do_count
?
3511 st_bp
->frp
[i
].length
- offset
: do_count
;
3512 res
= copy_to_user(ubp
, page_address(st_bp
->frp
[i
].page
) + offset
, cnt
);
3516 st_bp
->buffer_bytes
-= cnt
;
3517 st_bp
->read_pointer
+= cnt
;
3521 if (do_count
) { /* Should never happen */
3522 printk(KERN_WARNING
"st: from_buffer overflow (left %d).\n",
3530 /* Fill the s/g list up to the length required for this transfer */
3531 static void buf_to_sg(ST_buffer
*STbp
, unsigned int length
)
3535 struct scatterlist
*sg
;
3536 struct st_buf_fragment
*frp
;
3538 if (length
== STbp
->frp_sg_current
)
3539 return; /* work already done */
3541 sg
= &(STbp
->sg
[0]);
3543 for (i
=count
=0; count
< length
; i
++) {
3544 sg
[i
].page
= frp
[i
].page
;
3545 if (length
- count
> frp
[i
].length
)
3546 sg
[i
].length
= frp
[i
].length
;
3548 sg
[i
].length
= length
- count
;
3549 count
+= sg
[i
].length
;
3553 STbp
->frp_sg_current
= length
;
3557 /* Validate the options from command line or module parameters */
3558 static void validate_options(void)
3561 st_fixed_buffer_size
= buffer_kbs
* ST_KILOBYTE
;
3562 if (write_threshold_kbs
> 0)
3563 st_write_threshold
= write_threshold_kbs
* ST_KILOBYTE
;
3564 else if (buffer_kbs
> 0)
3565 st_write_threshold
= st_fixed_buffer_size
- 2048;
3566 if (st_write_threshold
> st_fixed_buffer_size
) {
3567 st_write_threshold
= st_fixed_buffer_size
;
3568 printk(KERN_WARNING
"st: write_threshold limited to %d bytes.\n",
3569 st_write_threshold
);
3571 if (max_sg_segs
>= ST_FIRST_SG
)
3572 st_max_sg_segs
= max_sg_segs
;
3576 /* Set the boot options. Syntax is defined in Documenation/scsi/st.txt.
3578 static int __init
st_setup(char *str
)
3580 int i
, len
, ints
[5];
3583 stp
= get_options(str
, ARRAY_SIZE(ints
), ints
);
3586 for (i
= 0; i
< ints
[0] && i
< ARRAY_SIZE(parms
); i
++)
3587 *parms
[i
].val
= ints
[i
+ 1];
3589 while (stp
!= NULL
) {
3590 for (i
= 0; i
< ARRAY_SIZE(parms
); i
++) {
3591 len
= strlen(parms
[i
].name
);
3592 if (!strncmp(stp
, parms
[i
].name
, len
) &&
3593 (*(stp
+ len
) == ':' || *(stp
+ len
) == '=')) {
3595 simple_strtoul(stp
+ len
+ 1, NULL
, 0);
3599 if (i
>= sizeof(parms
) / sizeof(struct st_dev_parm
))
3600 printk(KERN_WARNING
"st: illegal parameter in '%s'\n",
3602 stp
= strchr(stp
, ',');
3613 __setup("st=", st_setup
);
3617 /* Driverfs file support */
3618 static ssize_t
st_device_kdev_read(struct device
*driverfs_dev
,
3619 char *page
, size_t count
, loff_t off
)
3622 kdev
.value
=(int)(long)driverfs_dev
->driver_data
;
3623 return off
? 0 : sprintf(page
, "%x\n",kdev
.value
);
3625 static DEVICE_ATTR(kdev
,S_IRUGO
,st_device_kdev_read
,NULL
);
3627 static ssize_t
st_device_type_read(struct device
*driverfs_dev
,
3628 char *page
, size_t count
, loff_t off
)
3630 return off
? 0 : sprintf (page
, "CHR\n");
3632 static DEVICE_ATTR(type
,S_IRUGO
,st_device_type_read
,NULL
);
3635 static struct file_operations st_fops
=
3637 .owner
= THIS_MODULE
,
3643 .release
= st_release
,
3646 static int st_attach(Scsi_Device
* SDp
)
3648 struct gendisk
*disk
;
3653 int i
, mode
, dev_num
;
3657 if (SDp
->type
!= TYPE_TAPE
)
3659 if ((stp
= st_incompatible(SDp
))) {
3661 "st: Found incompatible tape at scsi%d, channel %d, id %d, lun %d\n",
3662 SDp
->host
->host_no
, SDp
->channel
, SDp
->id
, SDp
->lun
);
3663 printk(KERN_INFO
"st: The suggested driver is %s.\n", stp
);
3667 if (scsi_slave_attach(SDp
))
3670 i
= SDp
->host
->sg_tablesize
;
3671 if (st_max_sg_segs
< i
)
3673 buffer
= new_tape_buffer(TRUE
, (SDp
->host
)->unchecked_isa_dma
, i
);
3674 if (buffer
== NULL
) {
3675 printk(KERN_ERR
"st: Can't allocate new tape buffer. Device not attached.\n");
3679 disk
= alloc_disk(1);
3681 printk(KERN_ERR
"st: out of memory. Device not attached.\n");
3685 write_lock(&st_dev_arr_lock
);
3686 if (st_nr_dev
>= st_dev_max
) {
3691 tmp_dev_max
= st_nr_dev
+ ST_DEV_ARR_LUMP
;
3692 if (tmp_dev_max
> ST_MAX_TAPES
)
3693 tmp_dev_max
= ST_MAX_TAPES
;
3694 if (tmp_dev_max
<= st_nr_dev
) {
3695 write_unlock(&st_dev_arr_lock
);
3696 printk(KERN_ERR
"st: Too many tape devices (max. %d).\n",
3699 scsi_slave_detach(SDp
);
3703 tmp_da
= kmalloc(tmp_dev_max
* sizeof(Scsi_Tape
*), GFP_ATOMIC
);
3704 tmp_ba
= kmalloc(tmp_dev_max
* sizeof(ST_buffer
*), GFP_ATOMIC
);
3705 if (tmp_da
== NULL
|| tmp_ba
== NULL
) {
3710 write_unlock(&st_dev_arr_lock
);
3711 printk(KERN_ERR
"st: Can't extend device array.\n");
3713 scsi_slave_detach(SDp
);
3717 memset(tmp_da
, 0, tmp_dev_max
* sizeof(Scsi_Tape
*));
3718 if (scsi_tapes
!= NULL
) {
3719 memcpy(tmp_da
, scsi_tapes
,
3720 st_dev_max
* sizeof(Scsi_Tape
*));
3723 scsi_tapes
= tmp_da
;
3725 st_dev_max
= tmp_dev_max
;
3728 for (i
= 0; i
< st_dev_max
; i
++)
3729 if (scsi_tapes
[i
] == NULL
)
3731 if (i
>= st_dev_max
)
3732 panic("scsi_devices corrupt (st)");
3734 tpnt
= kmalloc(sizeof(Scsi_Tape
), GFP_ATOMIC
);
3736 write_unlock(&st_dev_arr_lock
);
3737 printk(KERN_ERR
"st: Can't allocate device descriptor.\n");
3739 scsi_slave_detach(SDp
);
3742 memset(tpnt
, 0, sizeof(Scsi_Tape
));
3744 sprintf(disk
->disk_name
, "st%d", i
);
3745 disk
->private_data
= &tpnt
->driver
;
3746 tpnt
->driver
= &st_template
;
3747 scsi_tapes
[i
] = tpnt
;
3751 if (SDp
->scsi_level
<= 2)
3752 tpnt
->tape_type
= MT_ISSCSI1
;
3754 tpnt
->tape_type
= MT_ISSCSI2
;
3756 tpnt
->buffer
= buffer
;
3761 tpnt
->drv_buffer
= 1; /* Try buffering if no mode sense */
3762 tpnt
->restr_dma
= (SDp
->host
)->unchecked_isa_dma
;
3763 tpnt
->use_pf
= (SDp
->scsi_level
>= SCSI_2
);
3765 tpnt
->do_auto_lock
= ST_AUTO_LOCK
;
3766 tpnt
->can_bsr
= (SDp
->scsi_level
> 2 ? 1 : ST_IN_FILE_POS
); /* BSR mandatory in SCSI3 */
3767 tpnt
->can_partitions
= 0;
3768 tpnt
->two_fm
= ST_TWO_FM
;
3769 tpnt
->fast_mteom
= ST_FAST_MTEOM
;
3770 tpnt
->scsi2_logical
= ST_SCSI2LOGICAL
;
3771 tpnt
->immediate
= ST_NOWAIT
;
3772 tpnt
->write_threshold
= st_write_threshold
;
3773 tpnt
->default_drvbuffer
= 0xff; /* No forced buffering */
3774 tpnt
->partition
= 0;
3775 tpnt
->new_partition
= 0;
3776 tpnt
->nbr_partitions
= 0;
3777 tpnt
->timeout
= ST_TIMEOUT
;
3778 tpnt
->long_timeout
= ST_LONG_TIMEOUT
;
3780 tpnt
->try_dio
= try_direct_io
&& !SDp
->host
->unchecked_isa_dma
;
3781 bounce_limit
= BLK_BOUNCE_HIGH
; /* Borrowed from scsi_merge.c */
3782 if (SDp
->host
->highmem_io
) {
3783 if (!PCI_DMA_BUS_IS_PHYS
)
3784 /* Platforms with virtual-DMA translation
3785 * hardware have no practical limit.
3787 bounce_limit
= BLK_BOUNCE_ANY
;
3788 else if (SDp
->host
->pci_dev
)
3789 bounce_limit
= SDp
->host
->pci_dev
->dma_mask
;
3790 } else if (SDp
->host
->unchecked_isa_dma
)
3791 bounce_limit
= BLK_BOUNCE_ISA
;
3792 bounce_limit
>>= PAGE_SHIFT
;
3793 if (bounce_limit
> ULONG_MAX
)
3794 bounce_limit
= ULONG_MAX
;
3795 tpnt
->max_pfn
= bounce_limit
;
3797 for (i
= 0; i
< ST_NBR_MODES
; i
++) {
3798 STm
= &(tpnt
->modes
[i
]);
3799 STm
->defined
= FALSE
;
3800 STm
->sysv
= ST_SYSV
;
3801 STm
->defaults_for_writes
= 0;
3802 STm
->do_async_writes
= ST_ASYNC_WRITES
;
3803 STm
->do_buffer_writes
= ST_BUFFER_WRITES
;
3804 STm
->do_read_ahead
= ST_READ_AHEAD
;
3805 STm
->default_compression
= ST_DONT_TOUCH
;
3806 STm
->default_blksize
= (-1); /* No forced size */
3807 STm
->default_density
= (-1); /* No forced density */
3810 for (i
= 0; i
< ST_NBR_PARTITIONS
; i
++) {
3811 STps
= &(tpnt
->ps
[i
]);
3813 STps
->eof
= ST_NOEOF
;
3815 STps
->last_block_valid
= FALSE
;
3816 STps
->drv_block
= (-1);
3817 STps
->drv_file
= (-1);
3820 tpnt
->current_mode
= 0;
3821 tpnt
->modes
[0].defined
= TRUE
;
3823 tpnt
->density_changed
= tpnt
->compression_changed
=
3824 tpnt
->blksize_changed
= FALSE
;
3825 init_MUTEX(&tpnt
->lock
);
3828 write_unlock(&st_dev_arr_lock
);
3830 for (mode
= 0; mode
< ST_NBR_MODES
; ++mode
) {
3832 static char *formats
[ST_NBR_MODES
] ={"", "l", "m", "a"};
3835 sprintf (name
, "mt%s", formats
[mode
]);
3836 sprintf(tpnt
->driverfs_dev_r
[mode
].bus_id
, "%s:%s",
3837 SDp
->sdev_driverfs_dev
.bus_id
, name
);
3838 sprintf(tpnt
->driverfs_dev_r
[mode
].name
, "%s%s",
3839 SDp
->sdev_driverfs_dev
.name
, name
);
3840 tpnt
->driverfs_dev_r
[mode
].parent
= &SDp
->sdev_driverfs_dev
;
3841 tpnt
->driverfs_dev_r
[mode
].bus
= &scsi_driverfs_bus_type
;
3842 tpnt
->driverfs_dev_r
[mode
].driver_data
=
3843 (void *)(long)__mkdev(MAJOR_NR
, dev_num
+ (mode
<< 5));
3844 device_register(&tpnt
->driverfs_dev_r
[mode
]);
3845 device_create_file(&tpnt
->driverfs_dev_r
[mode
],
3847 device_create_file(&tpnt
->driverfs_dev_r
[mode
], &dev_attr_kdev
);
3849 devfs_register (SDp
->de
, name
, DEVFS_FL_DEFAULT
,
3850 MAJOR_NR
, dev_num
+ (mode
<< 5),
3851 S_IFCHR
| S_IRUGO
| S_IWUGO
,
3853 /* No-rewind entry */
3854 sprintf (name
, "mt%sn", formats
[mode
]);
3855 sprintf(tpnt
->driverfs_dev_n
[mode
].bus_id
, "%s:%s",
3856 SDp
->sdev_driverfs_dev
.bus_id
, name
);
3857 sprintf(tpnt
->driverfs_dev_n
[mode
].name
, "%s%s",
3858 SDp
->sdev_driverfs_dev
.name
, name
);
3859 tpnt
->driverfs_dev_n
[mode
].parent
= &SDp
->sdev_driverfs_dev
;
3860 tpnt
->driverfs_dev_n
[mode
].bus
= &scsi_driverfs_bus_type
;
3861 tpnt
->driverfs_dev_n
[mode
].driver_data
=
3862 (void *)(long)__mkdev(MAJOR_NR
, dev_num
+ (mode
<< 5) + 128);
3863 device_register(&tpnt
->driverfs_dev_n
[mode
]);
3864 device_create_file(&tpnt
->driverfs_dev_n
[mode
],
3866 device_create_file(&tpnt
->driverfs_dev_n
[mode
],
3869 devfs_register (SDp
->de
, name
, DEVFS_FL_DEFAULT
,
3870 MAJOR_NR
, dev_num
+ (mode
<< 5) + 128,
3871 S_IFCHR
| S_IRUGO
| S_IWUGO
,
3874 disk
->number
= devfs_register_tape(SDp
->de
);
3877 "Attached scsi tape %s at scsi%d, channel %d, id %d, lun %d\n",
3878 tape_name(tpnt
), SDp
->host
->host_no
, SDp
->channel
, SDp
->id
, SDp
->lun
);
3879 printk(KERN_WARNING
"%s: try direct i/o: %s, max page reachable by HBA %lu\n",
3880 tape_name(tpnt
), tpnt
->try_dio
? "yes" : "no", tpnt
->max_pfn
);
3885 static void st_detach(Scsi_Device
* SDp
)
3890 write_lock(&st_dev_arr_lock
);
3891 for (i
= 0; i
< st_dev_max
; i
++) {
3892 tpnt
= scsi_tapes
[i
];
3893 if (tpnt
!= NULL
&& tpnt
->device
== SDp
) {
3894 tpnt
->device
= NULL
;
3895 for (mode
= 0; mode
< ST_NBR_MODES
; ++mode
) {
3896 devfs_unregister (tpnt
->de_r
[mode
]);
3897 tpnt
->de_r
[mode
] = NULL
;
3898 devfs_unregister (tpnt
->de_n
[mode
]);
3899 tpnt
->de_n
[mode
] = NULL
;
3901 devfs_unregister_tape(tpnt
->disk
->number
);
3903 scsi_slave_detach(SDp
);
3905 write_unlock(&st_dev_arr_lock
);
3907 for (mode
= 0; mode
< ST_NBR_MODES
; ++mode
) {
3908 device_remove_file(&tpnt
->driverfs_dev_r
[mode
],
3910 device_remove_file(&tpnt
->driverfs_dev_r
[mode
],
3912 device_unregister(&tpnt
->driverfs_dev_r
[mode
]);
3913 device_remove_file(&tpnt
->driverfs_dev_n
[mode
],
3915 device_remove_file(&tpnt
->driverfs_dev_n
[mode
],
3917 device_unregister(&tpnt
->driverfs_dev_n
[mode
]);
3920 tpnt
->buffer
->orig_frp_segs
= 0;
3921 normalize_buffer(tpnt
->buffer
);
3922 kfree(tpnt
->buffer
);
3924 put_disk(tpnt
->disk
);
3930 write_unlock(&st_dev_arr_lock
);
3934 static int __init
init_st(void)
3939 "st: Version %s, fixed bufsize %d, wrt %d, "
3941 verstr
, st_fixed_buffer_size
, st_write_threshold
,
3944 if (register_chrdev(SCSI_TAPE_MAJOR
, "st", &st_fops
) >= 0) {
3945 if (scsi_register_device(&st_template
) == 0)
3949 printk(KERN_ERR
"Unable to get major %d for SCSI tapes\n", MAJOR_NR
);
3953 static void __exit
exit_st(void)
3957 scsi_unregister_device(&st_template
);
3958 unregister_chrdev(SCSI_TAPE_MAJOR
, "st");
3959 if (scsi_tapes
!= NULL
) {
3960 for (i
=0; i
< st_dev_max
; ++i
)
3961 if (scsi_tapes
[i
]) {
3962 put_disk(scsi_tapes
[i
]->disk
);
3963 kfree(scsi_tapes
[i
]);
3967 printk(KERN_INFO
"st: Unloaded.\n");
3970 module_init(init_st
);
3971 module_exit(exit_st
);
3974 /* Pin down user pages and put them into a scatter gather list. Returns <= 0 if
3975 - mapping of all pages not successful
3976 - any page is above max_pfn
3977 (i.e., either completely successful or fails)
3979 static int st_map_user_pages(struct scatterlist
*sgl
, const unsigned int max_pages
,
3980 unsigned long uaddr
, size_t count
, int rw
,
3981 unsigned long max_pfn
)
3985 nr_pages
= sgl_map_user_pages(sgl
, max_pages
, uaddr
, count
, rw
);
3989 for (i
=0; i
< nr_pages
; i
++) {
3990 if (page_to_pfn(sgl
[i
].page
) > max_pfn
)
3996 sgl_unmap_user_pages(sgl
, nr_pages
, FALSE
);
4001 /* The following functions may be useful for a larger audience. */
4002 static int sgl_map_user_pages(struct scatterlist
*sgl
, const unsigned int max_pages
,
4003 unsigned long uaddr
, size_t count
, int rw
)
4006 unsigned int nr_pages
;
4007 struct page
**pages
;
4009 nr_pages
= ((uaddr
& ~PAGE_MASK
) + count
- 1 + ~PAGE_MASK
) >> PAGE_SHIFT
;
4011 /* User attempted Overflow! */
4012 if ((uaddr
+ count
) < uaddr
)
4016 if (nr_pages
> max_pages
)
4023 if ((pages
= kmalloc(max_pages
* sizeof(*pages
), GFP_KERNEL
)) == NULL
)
4026 /* Try to fault in all of the necessary pages */
4027 down_read(¤t
->mm
->mmap_sem
);
4028 /* rw==READ means read from drive, write into memory area */
4029 res
= get_user_pages(
4035 0, /* don't force */
4038 up_read(¤t
->mm
->mmap_sem
);
4040 /* Errors and no page mapped should return here */
4044 for (i
=0; i
< nr_pages
; i
++) {
4045 /* FIXME: flush superflous for rw==READ,
4046 * probably wrong function for rw==WRITE
4048 flush_dcache_page(pages
[i
]);
4051 /* Populate the scatter/gather list */
4052 sgl
[0].page
= pages
[0];
4053 sgl
[0].offset
= uaddr
& ~PAGE_MASK
;
4055 sgl
[0].length
= PAGE_SIZE
- sgl
[0].offset
;
4056 count
-= sgl
[0].length
;
4057 for (i
=1; i
< nr_pages
; i
++) {
4059 sgl
[i
].page
= pages
[i
];
4060 sgl
[i
].length
= count
< PAGE_SIZE
? count
: PAGE_SIZE
;
4065 sgl
[0].length
= count
;
4073 for (j
=0; j
< res
; j
++)
4074 page_cache_release(pages
[j
]);
4081 /* And unmap them... */
4082 static int sgl_unmap_user_pages(struct scatterlist
*sgl
, const unsigned int nr_pages
,
4087 for (i
=0; i
< nr_pages
; i
++) {
4088 if (dirtied
&& !PageReserved(sgl
[i
].page
))
4089 SetPageDirty(sgl
[i
].page
);
4090 /* FIXME: cache flush missing for rw==READ
4091 * FIXME: call the correct reference counting function
4093 page_cache_release(sgl
[i
].page
);