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 - 2010 Kai Makisara
13 email Kai.Makisara@kolumbus.fi
15 Some small formal changes - aeb, 950809
17 Last modified: 18-JAN-1998 Richard Gooch <rgooch@atnf.csiro.au> Devfs support
20 static const char *verstr
= "20101219";
22 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/sched.h>
28 #include <linux/init.h>
29 #include <linux/string.h>
30 #include <linux/slab.h>
31 #include <linux/errno.h>
32 #include <linux/mtio.h>
33 #include <linux/cdrom.h>
34 #include <linux/ioctl.h>
35 #include <linux/fcntl.h>
36 #include <linux/spinlock.h>
37 #include <linux/blkdev.h>
38 #include <linux/moduleparam.h>
39 #include <linux/cdev.h>
40 #include <linux/idr.h>
41 #include <linux/delay.h>
42 #include <linux/mutex.h>
44 #include <asm/uaccess.h>
47 #include <scsi/scsi.h>
48 #include <scsi/scsi_dbg.h>
49 #include <scsi/scsi_device.h>
50 #include <scsi/scsi_driver.h>
51 #include <scsi/scsi_eh.h>
52 #include <scsi/scsi_host.h>
53 #include <scsi/scsi_ioctl.h>
57 /* The driver prints some debugging information on the console if DEBUG
58 is defined and non-zero. */
61 #define ST_DEB_MSG KERN_NOTICE
63 /* The message level for the debug messages is currently set to KERN_NOTICE
64 so that people can easily see the messages. Later when the debugging messages
65 in the drivers are more widely classified, this may be changed to KERN_DEBUG. */
67 #define DEBC(a) if (debugging) { a ; }
73 #define ST_KILOBYTE 1024
75 #include "st_options.h"
78 static int buffer_kbs
;
79 static int max_sg_segs
;
80 static int try_direct_io
= TRY_DIRECT_IO
;
81 static int try_rdio
= 1;
82 static int try_wdio
= 1;
84 static struct class st_sysfs_class
;
85 static const struct attribute_group
*st_dev_groups
[];
87 MODULE_AUTHOR("Kai Makisara");
88 MODULE_DESCRIPTION("SCSI tape (st) driver");
89 MODULE_LICENSE("GPL");
90 MODULE_ALIAS_CHARDEV_MAJOR(SCSI_TAPE_MAJOR
);
91 MODULE_ALIAS_SCSI_DEVICE(TYPE_TAPE
);
93 /* Set 'perm' (4th argument) to 0 to disable module_param's definition
94 * of sysfs parameters (which module_param doesn't yet support).
95 * Sysfs parameters defined explicitly later.
97 module_param_named(buffer_kbs
, buffer_kbs
, int, 0);
98 MODULE_PARM_DESC(buffer_kbs
, "Default driver buffer size for fixed block mode (KB; 32)");
99 module_param_named(max_sg_segs
, max_sg_segs
, int, 0);
100 MODULE_PARM_DESC(max_sg_segs
, "Maximum number of scatter/gather segments to use (256)");
101 module_param_named(try_direct_io
, try_direct_io
, int, 0);
102 MODULE_PARM_DESC(try_direct_io
, "Try direct I/O between user buffer and tape drive (1)");
104 /* Extra parameters for testing */
105 module_param_named(try_rdio
, try_rdio
, int, 0);
106 MODULE_PARM_DESC(try_rdio
, "Try direct read i/o when possible");
107 module_param_named(try_wdio
, try_wdio
, int, 0);
108 MODULE_PARM_DESC(try_wdio
, "Try direct write i/o when possible");
111 static int write_threshold_kbs
; /* retained for compatibility */
112 static struct st_dev_parm
{
115 } parms
[] __initdata
= {
117 "buffer_kbs", &buffer_kbs
119 { /* Retained for compatibility with 2.4 */
120 "write_threshold_kbs", &write_threshold_kbs
126 "try_direct_io", &try_direct_io
131 /* Restrict the number of modes so that names for all are assigned */
132 #if ST_NBR_MODES > 16
133 #error "Maximum number of modes is 16"
135 /* Bit reversed order to get same names for same minors with all
137 static const char *st_formats
[] = {
138 "", "r", "k", "s", "l", "t", "o", "u",
139 "m", "v", "p", "x", "a", "y", "q", "z"};
141 /* The default definitions have been moved to st_options.h */
143 #define ST_FIXED_BUFFER_SIZE (ST_FIXED_BUFFER_BLOCKS * ST_KILOBYTE)
145 /* The buffer size should fit into the 24 bits for length in the
146 6-byte SCSI read and write commands. */
147 #if ST_FIXED_BUFFER_SIZE >= (2 << 24 - 1)
148 #error "Buffer size should not exceed (2 << 24 - 1) bytes!"
151 static int debugging
= DEBUG
;
153 #define MAX_RETRIES 0
154 #define MAX_WRITE_RETRIES 0
155 #define MAX_READY_RETRIES 0
156 #define NO_TAPE NOT_READY
158 #define ST_TIMEOUT (900 * HZ)
159 #define ST_LONG_TIMEOUT (14000 * HZ)
161 /* Remove mode bits and auto-rewind bit (7) */
162 #define TAPE_NR(x) ( ((iminor(x) & ~255) >> (ST_NBR_MODE_BITS + 1)) | \
163 (iminor(x) & ~(-1 << ST_MODE_SHIFT)) )
164 #define TAPE_MODE(x) ((iminor(x) & ST_MODE_MASK) >> ST_MODE_SHIFT)
166 /* Construct the minor number from the device (d), mode (m), and non-rewind (n) data */
167 #define TAPE_MINOR(d, m, n) (((d & ~(255 >> (ST_NBR_MODE_BITS + 1))) << (ST_NBR_MODE_BITS + 1)) | \
168 (d & (255 >> (ST_NBR_MODE_BITS + 1))) | (m << ST_MODE_SHIFT) | ((n != 0) << 7) )
170 /* Internal ioctl to set both density (uppermost 8 bits) and blocksize (lower
172 #define SET_DENS_AND_BLK 0x10001
174 static int st_fixed_buffer_size
= ST_FIXED_BUFFER_SIZE
;
175 static int st_max_sg_segs
= ST_MAX_SG
;
177 static int modes_defined
;
179 static int enlarge_buffer(struct st_buffer
*, int, int);
180 static void clear_buffer(struct st_buffer
*);
181 static void normalize_buffer(struct st_buffer
*);
182 static int append_to_buffer(const char __user
*, struct st_buffer
*, int);
183 static int from_buffer(struct st_buffer
*, char __user
*, int);
184 static void move_buffer_data(struct st_buffer
*, int);
186 static int sgl_map_user_pages(struct st_buffer
*, const unsigned int,
187 unsigned long, size_t, int);
188 static int sgl_unmap_user_pages(struct st_buffer
*, const unsigned int, int);
190 static int st_probe(struct device
*);
191 static int st_remove(struct device
*);
193 static int do_create_sysfs_files(void);
194 static void do_remove_sysfs_files(void);
196 static struct scsi_driver st_template
= {
197 .owner
= THIS_MODULE
,
205 static int st_compression(struct scsi_tape
*, int);
207 static int find_partition(struct scsi_tape
*);
208 static int switch_partition(struct scsi_tape
*);
210 static int st_int_ioctl(struct scsi_tape
*, unsigned int, unsigned long);
212 static void scsi_tape_release(struct kref
*);
214 #define to_scsi_tape(obj) container_of(obj, struct scsi_tape, kref)
216 static DEFINE_MUTEX(st_ref_mutex
);
217 static DEFINE_SPINLOCK(st_index_lock
);
218 static DEFINE_SPINLOCK(st_use_lock
);
219 static DEFINE_IDR(st_index_idr
);
223 #include "osst_detect.h"
224 #ifndef SIGS_FROM_OSST
225 #define SIGS_FROM_OSST \
226 {"OnStream", "SC-", "", "osst"}, \
227 {"OnStream", "DI-", "", "osst"}, \
228 {"OnStream", "DP-", "", "osst"}, \
229 {"OnStream", "USB", "", "osst"}, \
230 {"OnStream", "FW-", "", "osst"}
233 static struct scsi_tape
*scsi_tape_get(int dev
)
235 struct scsi_tape
*STp
= NULL
;
237 mutex_lock(&st_ref_mutex
);
238 spin_lock(&st_index_lock
);
240 STp
= idr_find(&st_index_idr
, dev
);
243 kref_get(&STp
->kref
);
248 if (scsi_device_get(STp
->device
))
254 kref_put(&STp
->kref
, scsi_tape_release
);
257 spin_unlock(&st_index_lock
);
258 mutex_unlock(&st_ref_mutex
);
262 static void scsi_tape_put(struct scsi_tape
*STp
)
264 struct scsi_device
*sdev
= STp
->device
;
266 mutex_lock(&st_ref_mutex
);
267 kref_put(&STp
->kref
, scsi_tape_release
);
268 scsi_device_put(sdev
);
269 mutex_unlock(&st_ref_mutex
);
272 struct st_reject_data
{
276 char *driver_hint
; /* Name of the correct driver, NULL if unknown */
279 static struct st_reject_data reject_list
[] = {
280 /* {"XXX", "Yy-", "", NULL}, example */
284 /* If the device signature is on the list of incompatible drives, the
285 function returns a pointer to the name of the correct driver (if known) */
286 static char * st_incompatible(struct scsi_device
* SDp
)
288 struct st_reject_data
*rp
;
290 for (rp
=&(reject_list
[0]); rp
->vendor
!= NULL
; rp
++)
291 if (!strncmp(rp
->vendor
, SDp
->vendor
, strlen(rp
->vendor
)) &&
292 !strncmp(rp
->model
, SDp
->model
, strlen(rp
->model
)) &&
293 !strncmp(rp
->rev
, SDp
->rev
, strlen(rp
->rev
))) {
295 return rp
->driver_hint
;
303 static inline char *tape_name(struct scsi_tape
*tape
)
305 return tape
->disk
->disk_name
;
308 #define st_printk(prefix, t, fmt, a...) \
309 sdev_printk(prefix, (t)->device, "%s: " fmt, \
312 #define DEBC_printk(t, fmt, a...) \
313 if (debugging) { st_printk(ST_DEB_MSG, t, fmt, ##a ); }
315 #define DEBC_printk(t, fmt, a...)
318 static void st_analyze_sense(struct st_request
*SRpnt
, struct st_cmdstatus
*s
)
321 const u8
*sense
= SRpnt
->sense
;
323 s
->have_sense
= scsi_normalize_sense(SRpnt
->sense
,
324 SCSI_SENSE_BUFFERSIZE
, &s
->sense_hdr
);
330 scsi_get_sense_info_fld(sense
, SCSI_SENSE_BUFFERSIZE
, &s
->uremainder64
);
331 switch (sense
[0] & 0x7f) {
336 s
->flags
= sense
[2] & 0xe0;
342 ucp
= scsi_sense_desc_find(sense
, SCSI_SENSE_BUFFERSIZE
, 4);
343 s
->flags
= ucp
? (ucp
[3] & 0xe0) : 0;
350 /* Convert the result to success code */
351 static int st_chk_result(struct scsi_tape
*STp
, struct st_request
* SRpnt
)
353 int result
= SRpnt
->result
;
355 DEB(const char *stp
;)
356 char *name
= tape_name(STp
);
357 struct st_cmdstatus
*cmdstatp
;
362 cmdstatp
= &STp
->buffer
->cmdstat
;
363 st_analyze_sense(SRpnt
, cmdstatp
);
365 if (cmdstatp
->have_sense
)
366 scode
= STp
->buffer
->cmdstat
.sense_hdr
.sense_key
;
372 st_printk(ST_DEB_MSG
, STp
,
373 "Error: %x, cmd: %x %x %x %x %x %x\n", result
,
374 SRpnt
->cmd
[0], SRpnt
->cmd
[1], SRpnt
->cmd
[2],
375 SRpnt
->cmd
[3], SRpnt
->cmd
[4], SRpnt
->cmd
[5]);
376 if (cmdstatp
->have_sense
)
377 __scsi_print_sense(name
, SRpnt
->sense
, SCSI_SENSE_BUFFERSIZE
);
379 if (!debugging
) { /* Abnormal conditions for tape */
380 if (!cmdstatp
->have_sense
)
381 st_printk(KERN_WARNING
, STp
,
382 "Error %x (driver bt 0x%x, host bt 0x%x).\n",
383 result
, driver_byte(result
), host_byte(result
));
384 else if (cmdstatp
->have_sense
&&
386 scode
!= RECOVERED_ERROR
&&
387 /* scode != UNIT_ATTENTION && */
388 scode
!= BLANK_CHECK
&&
389 scode
!= VOLUME_OVERFLOW
&&
390 SRpnt
->cmd
[0] != MODE_SENSE
&&
391 SRpnt
->cmd
[0] != TEST_UNIT_READY
) {
393 __scsi_print_sense(name
, SRpnt
->sense
, SCSI_SENSE_BUFFERSIZE
);
397 if (cmdstatp
->fixed_format
&&
398 STp
->cln_mode
>= EXTENDED_SENSE_START
) { /* Only fixed format sense */
399 if (STp
->cln_sense_value
)
400 STp
->cleaning_req
|= ((SRpnt
->sense
[STp
->cln_mode
] &
401 STp
->cln_sense_mask
) == STp
->cln_sense_value
);
403 STp
->cleaning_req
|= ((SRpnt
->sense
[STp
->cln_mode
] &
404 STp
->cln_sense_mask
) != 0);
406 if (cmdstatp
->have_sense
&&
407 cmdstatp
->sense_hdr
.asc
== 0 && cmdstatp
->sense_hdr
.ascq
== 0x17)
408 STp
->cleaning_req
= 1; /* ASC and ASCQ => cleaning requested */
410 STp
->pos_unknown
|= STp
->device
->was_reset
;
412 if (cmdstatp
->have_sense
&&
413 scode
== RECOVERED_ERROR
414 #if ST_RECOVERED_WRITE_FATAL
415 && SRpnt
->cmd
[0] != WRITE_6
416 && SRpnt
->cmd
[0] != WRITE_FILEMARKS
419 STp
->recover_count
++;
424 if (SRpnt
->cmd
[0] == READ_6
)
426 else if (SRpnt
->cmd
[0] == WRITE_6
)
430 st_printk(ST_DEB_MSG
, STp
,
431 "Recovered %s error (%d).\n",
432 stp
, STp
->recover_count
);
435 if (cmdstatp
->flags
== 0)
441 static struct st_request
*st_allocate_request(struct scsi_tape
*stp
)
443 struct st_request
*streq
;
445 streq
= kzalloc(sizeof(*streq
), GFP_KERNEL
);
449 st_printk(KERN_ERR
, stp
,
450 "Can't get SCSI request.\n");
451 if (signal_pending(current
))
452 stp
->buffer
->syscall_result
= -EINTR
;
454 stp
->buffer
->syscall_result
= -EBUSY
;
460 static void st_release_request(struct st_request
*streq
)
465 static void st_scsi_execute_end(struct request
*req
, int uptodate
)
467 struct st_request
*SRpnt
= req
->end_io_data
;
468 struct scsi_tape
*STp
= SRpnt
->stp
;
471 STp
->buffer
->cmdstat
.midlevel_result
= SRpnt
->result
= req
->errors
;
472 STp
->buffer
->cmdstat
.residual
= req
->resid_len
;
476 complete(SRpnt
->waiting
);
478 blk_rq_unmap_user(tmp
);
479 __blk_put_request(req
->q
, req
);
482 static int st_scsi_execute(struct st_request
*SRpnt
, const unsigned char *cmd
,
483 int data_direction
, void *buffer
, unsigned bufflen
,
484 int timeout
, int retries
)
487 struct rq_map_data
*mdata
= &SRpnt
->stp
->buffer
->map_data
;
489 int write
= (data_direction
== DMA_TO_DEVICE
);
491 req
= blk_get_request(SRpnt
->stp
->device
->request_queue
, write
,
494 return DRIVER_ERROR
<< 24;
496 blk_rq_set_block_pc(req
);
497 req
->cmd_flags
|= REQ_QUIET
;
499 mdata
->null_mapped
= 1;
502 err
= blk_rq_map_user(req
->q
, req
, mdata
, NULL
, bufflen
,
505 blk_put_request(req
);
506 return DRIVER_ERROR
<< 24;
510 SRpnt
->bio
= req
->bio
;
511 req
->cmd_len
= COMMAND_SIZE(cmd
[0]);
512 memset(req
->cmd
, 0, BLK_MAX_CDB
);
513 memcpy(req
->cmd
, cmd
, req
->cmd_len
);
514 req
->sense
= SRpnt
->sense
;
516 req
->timeout
= timeout
;
517 req
->retries
= retries
;
518 req
->end_io_data
= SRpnt
;
520 blk_execute_rq_nowait(req
->q
, NULL
, req
, 1, st_scsi_execute_end
);
524 /* Do the scsi command. Waits until command performed if do_wait is true.
525 Otherwise write_behind_check() is used to check that the command
527 static struct st_request
*
528 st_do_scsi(struct st_request
* SRpnt
, struct scsi_tape
* STp
, unsigned char *cmd
,
529 int bytes
, int direction
, int timeout
, int retries
, int do_wait
)
531 struct completion
*waiting
;
532 struct rq_map_data
*mdata
= &STp
->buffer
->map_data
;
535 /* if async, make sure there's no command outstanding */
536 if (!do_wait
&& ((STp
->buffer
)->last_SRpnt
)) {
537 st_printk(KERN_ERR
, STp
,
538 "Async command already active.\n");
539 if (signal_pending(current
))
540 (STp
->buffer
)->syscall_result
= (-EINTR
);
542 (STp
->buffer
)->syscall_result
= (-EBUSY
);
547 SRpnt
= st_allocate_request(STp
);
552 /* If async IO, set last_SRpnt. This ptr tells write_behind_check
553 which IO is outstanding. It's nulled out when the IO completes. */
555 (STp
->buffer
)->last_SRpnt
= SRpnt
;
557 waiting
= &STp
->wait
;
558 init_completion(waiting
);
559 SRpnt
->waiting
= waiting
;
561 if (STp
->buffer
->do_dio
) {
562 mdata
->page_order
= 0;
563 mdata
->nr_entries
= STp
->buffer
->sg_segs
;
564 mdata
->pages
= STp
->buffer
->mapped_pages
;
566 mdata
->page_order
= STp
->buffer
->reserved_page_order
;
568 DIV_ROUND_UP(bytes
, PAGE_SIZE
<< mdata
->page_order
);
569 mdata
->pages
= STp
->buffer
->reserved_pages
;
573 memcpy(SRpnt
->cmd
, cmd
, sizeof(SRpnt
->cmd
));
574 STp
->buffer
->cmdstat
.have_sense
= 0;
575 STp
->buffer
->syscall_result
= 0;
577 ret
= st_scsi_execute(SRpnt
, cmd
, direction
, NULL
, bytes
, timeout
,
580 /* could not allocate the buffer or request was too large */
581 (STp
->buffer
)->syscall_result
= (-EBUSY
);
582 (STp
->buffer
)->last_SRpnt
= NULL
;
583 } else if (do_wait
) {
584 wait_for_completion(waiting
);
585 SRpnt
->waiting
= NULL
;
586 (STp
->buffer
)->syscall_result
= st_chk_result(STp
, SRpnt
);
593 /* Handle the write-behind checking (waits for completion). Returns -ENOSPC if
594 write has been correct but EOM early warning reached, -EIO if write ended in
595 error or zero if write successful. Asynchronous writes are used only in
596 variable block mode. */
597 static int write_behind_check(struct scsi_tape
* STp
)
600 struct st_buffer
*STbuffer
;
601 struct st_partstat
*STps
;
602 struct st_cmdstatus
*cmdstatp
;
603 struct st_request
*SRpnt
;
605 STbuffer
= STp
->buffer
;
606 if (!STbuffer
->writing
)
610 if (STp
->write_pending
)
616 wait_for_completion(&(STp
->wait
));
617 SRpnt
= STbuffer
->last_SRpnt
;
618 STbuffer
->last_SRpnt
= NULL
;
619 SRpnt
->waiting
= NULL
;
621 (STp
->buffer
)->syscall_result
= st_chk_result(STp
, SRpnt
);
622 st_release_request(SRpnt
);
624 STbuffer
->buffer_bytes
-= STbuffer
->writing
;
625 STps
= &(STp
->ps
[STp
->partition
]);
626 if (STps
->drv_block
>= 0) {
627 if (STp
->block_size
== 0)
630 STps
->drv_block
+= STbuffer
->writing
/ STp
->block_size
;
633 cmdstatp
= &STbuffer
->cmdstat
;
634 if (STbuffer
->syscall_result
) {
636 if (cmdstatp
->have_sense
&& !cmdstatp
->deferred
&&
637 (cmdstatp
->flags
& SENSE_EOM
) &&
638 (cmdstatp
->sense_hdr
.sense_key
== NO_SENSE
||
639 cmdstatp
->sense_hdr
.sense_key
== RECOVERED_ERROR
)) {
640 /* EOM at write-behind, has all data been written? */
641 if (!cmdstatp
->remainder_valid
||
642 cmdstatp
->uremainder64
== 0)
646 STps
->drv_block
= -1;
648 STbuffer
->writing
= 0;
650 DEB(if (debugging
&& retval
)
651 st_printk(ST_DEB_MSG
, STp
,
652 "Async write error %x, return value %d.\n",
653 STbuffer
->cmdstat
.midlevel_result
, retval
);) /* end DEB */
659 /* Step over EOF if it has been inadvertently crossed (ioctl not used because
660 it messes up the block number). */
661 static int cross_eof(struct scsi_tape
* STp
, int forward
)
663 struct st_request
*SRpnt
;
664 unsigned char cmd
[MAX_COMMAND_SIZE
];
667 cmd
[1] = 0x01; /* Space FileMarks */
672 cmd
[2] = cmd
[3] = cmd
[4] = 0xff; /* -1 filemarks */
675 DEBC_printk(STp
, "Stepping over filemark %s.\n",
676 forward
? "forward" : "backward");
678 SRpnt
= st_do_scsi(NULL
, STp
, cmd
, 0, DMA_NONE
,
679 STp
->device
->request_queue
->rq_timeout
,
682 return (STp
->buffer
)->syscall_result
;
684 st_release_request(SRpnt
);
687 if ((STp
->buffer
)->cmdstat
.midlevel_result
!= 0)
688 st_printk(KERN_ERR
, STp
,
689 "Stepping over filemark %s failed.\n",
690 forward
? "forward" : "backward");
692 return (STp
->buffer
)->syscall_result
;
696 /* Flush the write buffer (never need to write if variable blocksize). */
697 static int st_flush_write_buffer(struct scsi_tape
* STp
)
701 unsigned char cmd
[MAX_COMMAND_SIZE
];
702 struct st_request
*SRpnt
;
703 struct st_partstat
*STps
;
705 result
= write_behind_check(STp
);
710 if (STp
->dirty
== 1) {
712 transfer
= STp
->buffer
->buffer_bytes
;
713 DEBC_printk(STp
, "Flushing %d bytes.\n", transfer
);
715 memset(cmd
, 0, MAX_COMMAND_SIZE
);
718 blks
= transfer
/ STp
->block_size
;
723 SRpnt
= st_do_scsi(NULL
, STp
, cmd
, transfer
, DMA_TO_DEVICE
,
724 STp
->device
->request_queue
->rq_timeout
,
725 MAX_WRITE_RETRIES
, 1);
727 return (STp
->buffer
)->syscall_result
;
729 STps
= &(STp
->ps
[STp
->partition
]);
730 if ((STp
->buffer
)->syscall_result
!= 0) {
731 struct st_cmdstatus
*cmdstatp
= &STp
->buffer
->cmdstat
;
733 if (cmdstatp
->have_sense
&& !cmdstatp
->deferred
&&
734 (cmdstatp
->flags
& SENSE_EOM
) &&
735 (cmdstatp
->sense_hdr
.sense_key
== NO_SENSE
||
736 cmdstatp
->sense_hdr
.sense_key
== RECOVERED_ERROR
) &&
737 (!cmdstatp
->remainder_valid
||
738 cmdstatp
->uremainder64
== 0)) { /* All written at EOM early warning */
740 (STp
->buffer
)->buffer_bytes
= 0;
741 if (STps
->drv_block
>= 0)
742 STps
->drv_block
+= blks
;
745 st_printk(KERN_ERR
, STp
, "Error on flush.\n");
746 STps
->drv_block
= (-1);
750 if (STps
->drv_block
>= 0)
751 STps
->drv_block
+= blks
;
753 (STp
->buffer
)->buffer_bytes
= 0;
755 st_release_request(SRpnt
);
762 /* Flush the tape buffer. The tape will be positioned correctly unless
763 seek_next is true. */
764 static int flush_buffer(struct scsi_tape
*STp
, int seek_next
)
766 int backspace
, result
;
767 struct st_buffer
*STbuffer
;
768 struct st_partstat
*STps
;
770 STbuffer
= STp
->buffer
;
773 * If there was a bus reset, block further access
776 if (STp
->pos_unknown
)
779 if (STp
->ready
!= ST_READY
)
781 STps
= &(STp
->ps
[STp
->partition
]);
782 if (STps
->rw
== ST_WRITING
) /* Writing */
783 return st_flush_write_buffer(STp
);
785 if (STp
->block_size
== 0)
788 backspace
= ((STp
->buffer
)->buffer_bytes
+
789 (STp
->buffer
)->read_pointer
) / STp
->block_size
-
790 ((STp
->buffer
)->read_pointer
+ STp
->block_size
- 1) /
792 (STp
->buffer
)->buffer_bytes
= 0;
793 (STp
->buffer
)->read_pointer
= 0;
796 if (STps
->eof
== ST_FM_HIT
) {
797 result
= cross_eof(STp
, 0); /* Back over the EOF hit */
799 STps
->eof
= ST_NOEOF
;
801 if (STps
->drv_file
>= 0)
806 if (!result
&& backspace
> 0)
807 result
= st_int_ioctl(STp
, MTBSR
, backspace
);
808 } else if (STps
->eof
== ST_FM_HIT
) {
809 if (STps
->drv_file
>= 0)
812 STps
->eof
= ST_NOEOF
;
818 /* Set the mode parameters */
819 static int set_mode_densblk(struct scsi_tape
* STp
, struct st_modedef
* STm
)
824 if (!STp
->density_changed
&&
825 STm
->default_density
>= 0 &&
826 STm
->default_density
!= STp
->density
) {
827 arg
= STm
->default_density
;
831 arg
<<= MT_ST_DENSITY_SHIFT
;
832 if (!STp
->blksize_changed
&&
833 STm
->default_blksize
>= 0 &&
834 STm
->default_blksize
!= STp
->block_size
) {
835 arg
|= STm
->default_blksize
;
838 arg
|= STp
->block_size
;
840 st_int_ioctl(STp
, SET_DENS_AND_BLK
, arg
)) {
841 st_printk(KERN_WARNING
, STp
,
842 "Can't set default block size to %d bytes "
844 STm
->default_blksize
, STm
->default_density
);
852 /* Lock or unlock the drive door. Don't use when st_request allocated. */
853 static int do_door_lock(struct scsi_tape
* STp
, int do_lock
)
857 cmd
= do_lock
? SCSI_IOCTL_DOORLOCK
: SCSI_IOCTL_DOORUNLOCK
;
858 DEBC_printk(STp
, "%socking drive door.\n", do_lock
? "L" : "Unl");
859 retval
= scsi_ioctl(STp
->device
, cmd
, NULL
);
861 STp
->door_locked
= do_lock
? ST_LOCKED_EXPLICIT
: ST_UNLOCKED
;
864 STp
->door_locked
= ST_LOCK_FAILS
;
870 /* Set the internal state after reset */
871 static void reset_state(struct scsi_tape
*STp
)
874 struct st_partstat
*STps
;
876 STp
->pos_unknown
= 0;
877 for (i
= 0; i
< ST_NBR_PARTITIONS
; i
++) {
878 STps
= &(STp
->ps
[i
]);
880 STps
->eof
= ST_NOEOF
;
882 STps
->last_block_valid
= 0;
883 STps
->drv_block
= -1;
886 if (STp
->can_partitions
) {
887 STp
->partition
= find_partition(STp
);
888 if (STp
->partition
< 0)
890 STp
->new_partition
= STp
->partition
;
894 /* Test if the drive is ready. Returns either one of the codes below or a negative system
896 #define CHKRES_READY 0
897 #define CHKRES_NEW_SESSION 1
898 #define CHKRES_NOT_READY 2
899 #define CHKRES_NO_TAPE 3
901 #define MAX_ATTENTIONS 10
903 static int test_ready(struct scsi_tape
*STp
, int do_wait
)
905 int attentions
, waits
, max_wait
, scode
;
906 int retval
= CHKRES_READY
, new_session
= 0;
907 unsigned char cmd
[MAX_COMMAND_SIZE
];
908 struct st_request
*SRpnt
= NULL
;
909 struct st_cmdstatus
*cmdstatp
= &STp
->buffer
->cmdstat
;
911 max_wait
= do_wait
? ST_BLOCK_SECONDS
: 0;
913 for (attentions
=waits
=0; ; ) {
914 memset((void *) &cmd
[0], 0, MAX_COMMAND_SIZE
);
915 cmd
[0] = TEST_UNIT_READY
;
916 SRpnt
= st_do_scsi(SRpnt
, STp
, cmd
, 0, DMA_NONE
,
917 STp
->long_timeout
, MAX_READY_RETRIES
, 1);
920 retval
= (STp
->buffer
)->syscall_result
;
924 if (cmdstatp
->have_sense
) {
926 scode
= cmdstatp
->sense_hdr
.sense_key
;
928 if (scode
== UNIT_ATTENTION
) { /* New media? */
930 if (attentions
< MAX_ATTENTIONS
) {
940 if (scode
== NOT_READY
) {
941 if (waits
< max_wait
) {
942 if (msleep_interruptible(1000)) {
950 if ((STp
->device
)->scsi_level
>= SCSI_2
&&
951 cmdstatp
->sense_hdr
.asc
== 0x3a) /* Check ASC */
952 retval
= CHKRES_NO_TAPE
;
954 retval
= CHKRES_NOT_READY
;
960 retval
= (STp
->buffer
)->syscall_result
;
962 retval
= new_session
? CHKRES_NEW_SESSION
: CHKRES_READY
;
967 st_release_request(SRpnt
);
972 /* See if the drive is ready and gather information about the tape. Return values:
973 < 0 negative error code from errno.h
975 1 drive not ready (possibly no tape)
977 static int check_tape(struct scsi_tape
*STp
, struct file
*filp
)
979 int i
, retval
, new_session
= 0, do_wait
;
980 unsigned char cmd
[MAX_COMMAND_SIZE
], saved_cleaning
;
981 unsigned short st_flags
= filp
->f_flags
;
982 struct st_request
*SRpnt
= NULL
;
983 struct st_modedef
*STm
;
984 struct st_partstat
*STps
;
985 struct inode
*inode
= file_inode(filp
);
986 int mode
= TAPE_MODE(inode
);
988 STp
->ready
= ST_READY
;
990 if (mode
!= STp
->current_mode
) {
991 DEBC_printk(STp
, "Mode change from %d to %d.\n",
992 STp
->current_mode
, mode
);
994 STp
->current_mode
= mode
;
996 STm
= &(STp
->modes
[STp
->current_mode
]);
998 saved_cleaning
= STp
->cleaning_req
;
999 STp
->cleaning_req
= 0;
1001 do_wait
= ((filp
->f_flags
& O_NONBLOCK
) == 0);
1002 retval
= test_ready(STp
, do_wait
);
1007 if (retval
== CHKRES_NEW_SESSION
) {
1008 STp
->pos_unknown
= 0;
1009 STp
->partition
= STp
->new_partition
= 0;
1010 if (STp
->can_partitions
)
1011 STp
->nbr_partitions
= 1; /* This guess will be updated later
1013 for (i
= 0; i
< ST_NBR_PARTITIONS
; i
++) {
1014 STps
= &(STp
->ps
[i
]);
1016 STps
->eof
= ST_NOEOF
;
1018 STps
->last_block_valid
= 0;
1019 STps
->drv_block
= 0;
1025 STp
->cleaning_req
|= saved_cleaning
;
1027 if (retval
== CHKRES_NOT_READY
|| retval
== CHKRES_NO_TAPE
) {
1028 if (retval
== CHKRES_NO_TAPE
)
1029 STp
->ready
= ST_NO_TAPE
;
1031 STp
->ready
= ST_NOT_READY
;
1033 STp
->density
= 0; /* Clear the erroneous "residue" */
1034 STp
->write_prot
= 0;
1035 STp
->block_size
= 0;
1036 STp
->ps
[0].drv_file
= STp
->ps
[0].drv_block
= (-1);
1037 STp
->partition
= STp
->new_partition
= 0;
1038 STp
->door_locked
= ST_UNLOCKED
;
1039 return CHKRES_NOT_READY
;
1043 if (STp
->omit_blklims
)
1044 STp
->min_block
= STp
->max_block
= (-1);
1046 memset((void *) &cmd
[0], 0, MAX_COMMAND_SIZE
);
1047 cmd
[0] = READ_BLOCK_LIMITS
;
1049 SRpnt
= st_do_scsi(SRpnt
, STp
, cmd
, 6, DMA_FROM_DEVICE
,
1050 STp
->device
->request_queue
->rq_timeout
,
1051 MAX_READY_RETRIES
, 1);
1053 retval
= (STp
->buffer
)->syscall_result
;
1057 if (!SRpnt
->result
&& !STp
->buffer
->cmdstat
.have_sense
) {
1058 STp
->max_block
= ((STp
->buffer
)->b_data
[1] << 16) |
1059 ((STp
->buffer
)->b_data
[2] << 8) | (STp
->buffer
)->b_data
[3];
1060 STp
->min_block
= ((STp
->buffer
)->b_data
[4] << 8) |
1061 (STp
->buffer
)->b_data
[5];
1062 if ( DEB( debugging
|| ) !STp
->inited
)
1063 st_printk(KERN_INFO
, STp
,
1064 "Block limits %d - %d bytes.\n",
1065 STp
->min_block
, STp
->max_block
);
1067 STp
->min_block
= STp
->max_block
= (-1);
1068 DEBC_printk(STp
, "Can't read block limits.\n");
1072 memset((void *) &cmd
[0], 0, MAX_COMMAND_SIZE
);
1073 cmd
[0] = MODE_SENSE
;
1076 SRpnt
= st_do_scsi(SRpnt
, STp
, cmd
, 12, DMA_FROM_DEVICE
,
1077 STp
->device
->request_queue
->rq_timeout
,
1078 MAX_READY_RETRIES
, 1);
1080 retval
= (STp
->buffer
)->syscall_result
;
1084 if ((STp
->buffer
)->syscall_result
!= 0) {
1085 DEBC_printk(STp
, "No Mode Sense.\n");
1086 STp
->block_size
= ST_DEFAULT_BLOCK
; /* Educated guess (?) */
1087 (STp
->buffer
)->syscall_result
= 0; /* Prevent error propagation */
1088 STp
->drv_write_prot
= 0;
1090 DEBC_printk(STp
,"Mode sense. Length %d, "
1091 "medium %x, WBS %x, BLL %d\n",
1092 (STp
->buffer
)->b_data
[0],
1093 (STp
->buffer
)->b_data
[1],
1094 (STp
->buffer
)->b_data
[2],
1095 (STp
->buffer
)->b_data
[3]);
1097 if ((STp
->buffer
)->b_data
[3] >= 8) {
1098 STp
->drv_buffer
= ((STp
->buffer
)->b_data
[2] >> 4) & 7;
1099 STp
->density
= (STp
->buffer
)->b_data
[4];
1100 STp
->block_size
= (STp
->buffer
)->b_data
[9] * 65536 +
1101 (STp
->buffer
)->b_data
[10] * 256 + (STp
->buffer
)->b_data
[11];
1102 DEBC_printk(STp
, "Density %x, tape length: %x, "
1105 (STp
->buffer
)->b_data
[5] * 65536 +
1106 (STp
->buffer
)->b_data
[6] * 256 +
1107 (STp
->buffer
)->b_data
[7],
1110 STp
->drv_write_prot
= ((STp
->buffer
)->b_data
[2] & 0x80) != 0;
1111 if (!STp
->drv_buffer
&& STp
->immediate_filemark
) {
1112 st_printk(KERN_WARNING
, STp
,
1113 "non-buffered tape: disabling "
1114 "writing immediate filemarks\n");
1115 STp
->immediate_filemark
= 0;
1118 st_release_request(SRpnt
);
1122 if (STp
->block_size
> 0)
1123 (STp
->buffer
)->buffer_blocks
=
1124 (STp
->buffer
)->buffer_size
/ STp
->block_size
;
1126 (STp
->buffer
)->buffer_blocks
= 1;
1127 (STp
->buffer
)->buffer_bytes
= (STp
->buffer
)->read_pointer
= 0;
1129 DEBC_printk(STp
, "Block size: %d, buffer size: %d (%d blocks).\n",
1130 STp
->block_size
, (STp
->buffer
)->buffer_size
,
1131 (STp
->buffer
)->buffer_blocks
);
1133 if (STp
->drv_write_prot
) {
1134 STp
->write_prot
= 1;
1136 DEBC_printk(STp
, "Write protected\n");
1139 ((st_flags
& O_ACCMODE
) == O_WRONLY
||
1140 (st_flags
& O_ACCMODE
) == O_RDWR
)) {
1146 if (STp
->can_partitions
&& STp
->nbr_partitions
< 1) {
1147 /* This code is reached when the device is opened for the first time
1148 after the driver has been initialized with tape in the drive and the
1149 partition support has been enabled. */
1150 DEBC_printk(STp
, "Updating partition number in status.\n");
1151 if ((STp
->partition
= find_partition(STp
)) < 0) {
1152 retval
= STp
->partition
;
1155 STp
->new_partition
= STp
->partition
;
1156 STp
->nbr_partitions
= 1; /* This guess will be updated when necessary */
1159 if (new_session
) { /* Change the drive parameters for the new mode */
1160 STp
->density_changed
= STp
->blksize_changed
= 0;
1161 STp
->compression_changed
= 0;
1162 if (!(STm
->defaults_for_writes
) &&
1163 (retval
= set_mode_densblk(STp
, STm
)) < 0)
1166 if (STp
->default_drvbuffer
!= 0xff) {
1167 if (st_int_ioctl(STp
, MTSETDRVBUFFER
, STp
->default_drvbuffer
))
1168 st_printk(KERN_WARNING
, STp
,
1169 "Can't set default drive "
1170 "buffering to %d.\n",
1171 STp
->default_drvbuffer
);
1175 return CHKRES_READY
;
1182 \f/* Open the device. Needs to take the BKL only because of incrementing the SCSI host
1184 static int st_open(struct inode
*inode
, struct file
*filp
)
1186 int i
, retval
= (-EIO
);
1188 struct scsi_tape
*STp
;
1189 struct st_partstat
*STps
;
1190 int dev
= TAPE_NR(inode
);
1193 * We really want to do nonseekable_open(inode, filp); here, but some
1194 * versions of tar incorrectly call lseek on tapes and bail out if that
1195 * fails. So we disallow pread() and pwrite(), but permit lseeks.
1197 filp
->f_mode
&= ~(FMODE_PREAD
| FMODE_PWRITE
);
1199 if (!(STp
= scsi_tape_get(dev
))) {
1203 filp
->private_data
= STp
;
1205 spin_lock(&st_use_lock
);
1207 spin_unlock(&st_use_lock
);
1209 DEBC_printk(STp
, "Device already in use.\n");
1214 spin_unlock(&st_use_lock
);
1215 STp
->rew_at_close
= STp
->autorew_dev
= (iminor(inode
) & 0x80) == 0;
1217 if (scsi_autopm_get_device(STp
->device
) < 0) {
1222 if (!scsi_block_when_processing_errors(STp
->device
)) {
1227 /* See that we have at least a one page buffer available */
1228 if (!enlarge_buffer(STp
->buffer
, PAGE_SIZE
, STp
->restr_dma
)) {
1229 st_printk(KERN_WARNING
, STp
,
1230 "Can't allocate one page tape buffer.\n");
1231 retval
= (-EOVERFLOW
);
1235 (STp
->buffer
)->cleared
= 0;
1236 (STp
->buffer
)->writing
= 0;
1237 (STp
->buffer
)->syscall_result
= 0;
1239 STp
->write_prot
= ((filp
->f_flags
& O_ACCMODE
) == O_RDONLY
);
1242 for (i
= 0; i
< ST_NBR_PARTITIONS
; i
++) {
1243 STps
= &(STp
->ps
[i
]);
1246 STp
->try_dio_now
= STp
->try_dio
;
1247 STp
->recover_count
= 0;
1248 DEB( STp
->nbr_waits
= STp
->nbr_finished
= 0;
1249 STp
->nbr_requests
= STp
->nbr_dio
= STp
->nbr_pages
= 0; )
1251 retval
= check_tape(STp
, filp
);
1254 if ((filp
->f_flags
& O_NONBLOCK
) == 0 &&
1255 retval
!= CHKRES_READY
) {
1256 if (STp
->ready
== NO_TAPE
)
1257 retval
= (-ENOMEDIUM
);
1265 normalize_buffer(STp
->buffer
);
1266 spin_lock(&st_use_lock
);
1268 spin_unlock(&st_use_lock
);
1271 scsi_autopm_put_device(STp
->device
);
1277 /* Flush the tape buffer before close */
1278 static int st_flush(struct file
*filp
, fl_owner_t id
)
1280 int result
= 0, result2
;
1281 unsigned char cmd
[MAX_COMMAND_SIZE
];
1282 struct st_request
*SRpnt
;
1283 struct scsi_tape
*STp
= filp
->private_data
;
1284 struct st_modedef
*STm
= &(STp
->modes
[STp
->current_mode
]);
1285 struct st_partstat
*STps
= &(STp
->ps
[STp
->partition
]);
1287 if (file_count(filp
) > 1)
1290 if (STps
->rw
== ST_WRITING
&& !STp
->pos_unknown
) {
1291 result
= st_flush_write_buffer(STp
);
1292 if (result
!= 0 && result
!= (-ENOSPC
))
1296 if (STp
->can_partitions
&&
1297 (result2
= switch_partition(STp
)) < 0) {
1298 DEBC_printk(STp
, "switch_partition at close failed.\n");
1304 DEBC( if (STp
->nbr_requests
)
1305 st_printk(KERN_DEBUG
, STp
,
1306 "Number of r/w requests %d, dio used in %d, "
1307 "pages %d.\n", STp
->nbr_requests
, STp
->nbr_dio
,
1310 if (STps
->rw
== ST_WRITING
&& !STp
->pos_unknown
) {
1311 struct st_cmdstatus
*cmdstatp
= &STp
->buffer
->cmdstat
;
1314 DEBC_printk(STp
, "Async write waits %d, finished %d.\n",
1315 STp
->nbr_waits
, STp
->nbr_finished
);
1317 memset(cmd
, 0, MAX_COMMAND_SIZE
);
1318 cmd
[0] = WRITE_FILEMARKS
;
1319 if (STp
->immediate_filemark
)
1321 cmd
[4] = 1 + STp
->two_fm
;
1323 SRpnt
= st_do_scsi(NULL
, STp
, cmd
, 0, DMA_NONE
,
1324 STp
->device
->request_queue
->rq_timeout
,
1325 MAX_WRITE_RETRIES
, 1);
1327 result
= (STp
->buffer
)->syscall_result
;
1331 if (STp
->buffer
->syscall_result
== 0 ||
1332 (cmdstatp
->have_sense
&& !cmdstatp
->deferred
&&
1333 (cmdstatp
->flags
& SENSE_EOM
) &&
1334 (cmdstatp
->sense_hdr
.sense_key
== NO_SENSE
||
1335 cmdstatp
->sense_hdr
.sense_key
== RECOVERED_ERROR
) &&
1336 (!cmdstatp
->remainder_valid
|| cmdstatp
->uremainder64
== 0))) {
1337 /* Write successful at EOM */
1338 st_release_request(SRpnt
);
1340 if (STps
->drv_file
>= 0)
1342 STps
->drv_block
= 0;
1347 else { /* Write error */
1348 st_release_request(SRpnt
);
1350 st_printk(KERN_ERR
, STp
,
1351 "Error on write filemark.\n");
1356 DEBC_printk(STp
, "Buffer flushed, %d EOF(s) written\n", cmd
[4]);
1357 } else if (!STp
->rew_at_close
) {
1358 STps
= &(STp
->ps
[STp
->partition
]);
1359 if (!STm
->sysv
|| STps
->rw
!= ST_READING
) {
1361 result
= flush_buffer(STp
, 0);
1362 else if (STps
->eof
== ST_FM_HIT
) {
1363 result
= cross_eof(STp
, 0);
1365 if (STps
->drv_file
>= 0)
1367 STps
->drv_block
= 0;
1370 STps
->eof
= ST_NOEOF
;
1372 } else if ((STps
->eof
== ST_NOEOF
&&
1373 !(result
= cross_eof(STp
, 1))) ||
1374 STps
->eof
== ST_FM_HIT
) {
1375 if (STps
->drv_file
>= 0)
1377 STps
->drv_block
= 0;
1383 if (STp
->rew_at_close
) {
1384 result2
= st_int_ioctl(STp
, MTREW
, 1);
1392 /* Close the device and release it. BKL is not needed: this is the only thread
1393 accessing this tape. */
1394 static int st_release(struct inode
*inode
, struct file
*filp
)
1397 struct scsi_tape
*STp
= filp
->private_data
;
1399 if (STp
->door_locked
== ST_LOCKED_AUTO
)
1400 do_door_lock(STp
, 0);
1402 normalize_buffer(STp
->buffer
);
1403 spin_lock(&st_use_lock
);
1405 spin_unlock(&st_use_lock
);
1406 scsi_autopm_put_device(STp
->device
);
1412 /* The checks common to both reading and writing */
1413 static ssize_t
rw_checks(struct scsi_tape
*STp
, struct file
*filp
, size_t count
)
1418 * If we are in the middle of error recovery, don't let anyone
1419 * else try and use this device. Also, if error recovery fails, it
1420 * may try and take the device offline, in which case all further
1421 * access to the device is prohibited.
1423 if (!scsi_block_when_processing_errors(STp
->device
)) {
1428 if (STp
->ready
!= ST_READY
) {
1429 if (STp
->ready
== ST_NO_TAPE
)
1430 retval
= (-ENOMEDIUM
);
1436 if (! STp
->modes
[STp
->current_mode
].defined
) {
1443 * If there was a bus reset, block further access
1446 if (STp
->pos_unknown
) {
1456 st_printk(ST_DEB_MSG
, STp
,
1457 "Incorrect device.\n");
1462 if (STp
->can_partitions
&&
1463 (retval
= switch_partition(STp
)) < 0)
1466 if (STp
->block_size
== 0 && STp
->max_block
> 0 &&
1467 (count
< STp
->min_block
|| count
> STp
->max_block
)) {
1472 if (STp
->do_auto_lock
&& STp
->door_locked
== ST_UNLOCKED
&&
1473 !do_door_lock(STp
, 1))
1474 STp
->door_locked
= ST_LOCKED_AUTO
;
1481 static int setup_buffering(struct scsi_tape
*STp
, const char __user
*buf
,
1482 size_t count
, int is_read
)
1484 int i
, bufsize
, retval
= 0;
1485 struct st_buffer
*STbp
= STp
->buffer
;
1488 i
= STp
->try_dio_now
&& try_rdio
;
1490 i
= STp
->try_dio_now
&& try_wdio
;
1492 if (i
&& ((unsigned long)buf
& queue_dma_alignment(
1493 STp
->device
->request_queue
)) == 0) {
1494 i
= sgl_map_user_pages(STbp
, STbp
->use_sg
, (unsigned long)buf
,
1495 count
, (is_read
? READ
: WRITE
));
1498 STbp
->buffer_bytes
= 0; /* can be used as transfer counter */
1501 STbp
->do_dio
= 0; /* fall back to buffering with any error */
1502 STbp
->sg_segs
= STbp
->do_dio
;
1506 STp
->nbr_pages
+= STbp
->do_dio
;
1511 DEB( STp
->nbr_requests
++; )
1513 if (!STbp
->do_dio
) {
1514 if (STp
->block_size
)
1515 bufsize
= STp
->block_size
> st_fixed_buffer_size
?
1516 STp
->block_size
: st_fixed_buffer_size
;
1519 /* Make sure that data from previous user is not leaked even if
1520 HBA does not return correct residual */
1521 if (is_read
&& STp
->sili
&& !STbp
->cleared
)
1525 if (bufsize
> STbp
->buffer_size
&&
1526 !enlarge_buffer(STbp
, bufsize
, STp
->restr_dma
)) {
1527 st_printk(KERN_WARNING
, STp
,
1528 "Can't allocate %d byte tape buffer.\n",
1530 retval
= (-EOVERFLOW
);
1533 if (STp
->block_size
)
1534 STbp
->buffer_blocks
= bufsize
/ STp
->block_size
;
1542 /* Can be called more than once after each setup_buffer() */
1543 static void release_buffering(struct scsi_tape
*STp
, int is_read
)
1545 struct st_buffer
*STbp
;
1549 sgl_unmap_user_pages(STbp
, STbp
->do_dio
, is_read
);
1558 st_write(struct file
*filp
, const char __user
*buf
, size_t count
, loff_t
* ppos
)
1561 ssize_t i
, do_count
, blks
, transfer
;
1563 int undone
, retry_eot
= 0, scode
;
1565 unsigned char cmd
[MAX_COMMAND_SIZE
];
1566 const char __user
*b_point
;
1567 struct st_request
*SRpnt
= NULL
;
1568 struct scsi_tape
*STp
= filp
->private_data
;
1569 struct st_modedef
*STm
;
1570 struct st_partstat
*STps
;
1571 struct st_buffer
*STbp
;
1573 if (mutex_lock_interruptible(&STp
->lock
))
1574 return -ERESTARTSYS
;
1576 retval
= rw_checks(STp
, filp
, count
);
1577 if (retval
|| count
== 0)
1580 /* Write must be integral number of blocks */
1581 if (STp
->block_size
!= 0 && (count
% STp
->block_size
) != 0) {
1582 st_printk(KERN_WARNING
, STp
,
1583 "Write not multiple of tape block size.\n");
1588 STm
= &(STp
->modes
[STp
->current_mode
]);
1589 STps
= &(STp
->ps
[STp
->partition
]);
1591 if (STp
->write_prot
) {
1597 if (STps
->rw
== ST_READING
) {
1598 retval
= flush_buffer(STp
, 0);
1601 STps
->rw
= ST_WRITING
;
1602 } else if (STps
->rw
!= ST_WRITING
&&
1603 STps
->drv_file
== 0 && STps
->drv_block
== 0) {
1604 if ((retval
= set_mode_densblk(STp
, STm
)) < 0)
1606 if (STm
->default_compression
!= ST_DONT_TOUCH
&&
1607 !(STp
->compression_changed
)) {
1608 if (st_compression(STp
, (STm
->default_compression
== ST_YES
))) {
1609 st_printk(KERN_WARNING
, STp
,
1610 "Can't set default compression.\n");
1611 if (modes_defined
) {
1620 i
= write_behind_check(STp
);
1623 STps
->eof
= ST_EOM_OK
;
1625 STps
->eof
= ST_EOM_ERROR
;
1628 if (STps
->eof
== ST_EOM_OK
) {
1629 STps
->eof
= ST_EOD_1
; /* allow next write */
1633 else if (STps
->eof
== ST_EOM_ERROR
) {
1638 /* Check the buffer readability in cases where copy_user might catch
1639 the problems after some tape movement. */
1640 if (STp
->block_size
!= 0 &&
1642 (copy_from_user(&i
, buf
, 1) != 0 ||
1643 copy_from_user(&i
, buf
+ count
- 1, 1) != 0)) {
1648 retval
= setup_buffering(STp
, buf
, count
, 0);
1654 memset(cmd
, 0, MAX_COMMAND_SIZE
);
1656 cmd
[1] = (STp
->block_size
!= 0);
1658 STps
->rw
= ST_WRITING
;
1661 while (count
> 0 && !retry_eot
) {
1667 if (STp
->block_size
== 0)
1670 do_count
= STbp
->buffer_blocks
* STp
->block_size
-
1672 if (do_count
> count
)
1676 i
= append_to_buffer(b_point
, STbp
, do_count
);
1683 b_point
+= do_count
;
1685 async_write
= STp
->block_size
== 0 && !STbp
->do_dio
&&
1686 STm
->do_async_writes
&& STps
->eof
< ST_EOM_OK
;
1688 if (STp
->block_size
!= 0 && STm
->do_buffer_writes
&&
1689 !(STp
->try_dio_now
&& try_wdio
) && STps
->eof
< ST_EOM_OK
&&
1690 STbp
->buffer_bytes
< STbp
->buffer_size
) {
1692 /* Don't write a buffer that is not full enough. */
1693 if (!async_write
&& count
== 0)
1698 if (STp
->block_size
== 0)
1699 blks
= transfer
= do_count
;
1702 blks
= STbp
->buffer_bytes
;
1705 blks
/= STp
->block_size
;
1706 transfer
= blks
* STp
->block_size
;
1708 cmd
[2] = blks
>> 16;
1712 SRpnt
= st_do_scsi(SRpnt
, STp
, cmd
, transfer
, DMA_TO_DEVICE
,
1713 STp
->device
->request_queue
->rq_timeout
,
1714 MAX_WRITE_RETRIES
, !async_write
);
1716 retval
= STbp
->syscall_result
;
1719 if (async_write
&& !STbp
->syscall_result
) {
1720 STbp
->writing
= transfer
;
1721 STp
->dirty
= !(STbp
->writing
==
1722 STbp
->buffer_bytes
);
1723 SRpnt
= NULL
; /* Prevent releasing this request! */
1724 DEB( STp
->write_pending
= 1; )
1728 if (STbp
->syscall_result
!= 0) {
1729 struct st_cmdstatus
*cmdstatp
= &STp
->buffer
->cmdstat
;
1731 DEBC_printk(STp
, "Error on write:\n");
1732 if (cmdstatp
->have_sense
&& (cmdstatp
->flags
& SENSE_EOM
)) {
1733 scode
= cmdstatp
->sense_hdr
.sense_key
;
1734 if (cmdstatp
->remainder_valid
)
1735 undone
= (int)cmdstatp
->uremainder64
;
1736 else if (STp
->block_size
== 0 &&
1737 scode
== VOLUME_OVERFLOW
)
1741 if (STp
->block_size
!= 0)
1742 undone
*= STp
->block_size
;
1743 if (undone
<= do_count
) {
1744 /* Only data from this write is not written */
1748 if (STp
->block_size
)
1749 blks
= (transfer
- undone
) / STp
->block_size
;
1750 STps
->eof
= ST_EOM_OK
;
1751 /* Continue in fixed block mode if all written
1752 in this request but still something left to write
1753 (retval left to zero)
1755 if (STp
->block_size
== 0 ||
1756 undone
> 0 || count
== 0)
1757 retval
= (-ENOSPC
); /* EOM within current request */
1758 DEBC_printk(STp
, "EOM with %d "
1759 "bytes unwritten.\n",
1762 /* EOT within data buffered earlier (possible only
1763 in fixed block mode without direct i/o) */
1764 if (!retry_eot
&& !cmdstatp
->deferred
&&
1765 (scode
== NO_SENSE
|| scode
== RECOVERED_ERROR
)) {
1766 move_buffer_data(STp
->buffer
, transfer
- undone
);
1768 if (STps
->drv_block
>= 0) {
1769 STps
->drv_block
+= (transfer
- undone
) /
1772 STps
->eof
= ST_EOM_OK
;
1773 DEBC_printk(STp
, "Retry "
1776 STp
->buffer
->buffer_bytes
);
1780 /* Either error within data buffered by driver or
1783 blks
= do_count
= 0;
1784 STps
->eof
= ST_EOM_ERROR
;
1785 STps
->drv_block
= (-1); /* Too cautious? */
1786 retval
= (-EIO
); /* EOM for old data */
1787 DEBC_printk(STp
, "EOM with "
1793 STps
->drv_block
= (-1); /* Too cautious? */
1794 retval
= STbp
->syscall_result
;
1799 if (STps
->drv_block
>= 0) {
1800 if (STp
->block_size
== 0)
1801 STps
->drv_block
+= (do_count
> 0);
1803 STps
->drv_block
+= blks
;
1806 STbp
->buffer_bytes
= 0;
1809 if (retval
|| retry_eot
) {
1811 retval
= total
- count
;
1816 if (STps
->eof
== ST_EOD_1
)
1817 STps
->eof
= ST_EOM_OK
;
1818 else if (STps
->eof
!= ST_EOM_OK
)
1819 STps
->eof
= ST_NOEOF
;
1820 retval
= total
- count
;
1824 st_release_request(SRpnt
);
1825 release_buffering(STp
, 0);
1826 mutex_unlock(&STp
->lock
);
1831 /* Read data from the tape. Returns zero in the normal case, one if the
1832 eof status has changed, and the negative error code in case of a
1833 fatal error. Otherwise updates the buffer and the eof state.
1835 Does release user buffer mapping if it is set.
1837 static long read_tape(struct scsi_tape
*STp
, long count
,
1838 struct st_request
** aSRpnt
)
1840 int transfer
, blks
, bytes
;
1841 unsigned char cmd
[MAX_COMMAND_SIZE
];
1842 struct st_request
*SRpnt
;
1843 struct st_modedef
*STm
;
1844 struct st_partstat
*STps
;
1845 struct st_buffer
*STbp
;
1851 STm
= &(STp
->modes
[STp
->current_mode
]);
1852 STps
= &(STp
->ps
[STp
->partition
]);
1853 if (STps
->eof
== ST_FM_HIT
)
1857 if (STp
->block_size
== 0)
1858 blks
= bytes
= count
;
1860 if (!(STp
->try_dio_now
&& try_rdio
) && STm
->do_read_ahead
) {
1861 blks
= (STp
->buffer
)->buffer_blocks
;
1862 bytes
= blks
* STp
->block_size
;
1865 if (!STbp
->do_dio
&& bytes
> (STp
->buffer
)->buffer_size
)
1866 bytes
= (STp
->buffer
)->buffer_size
;
1867 blks
= bytes
/ STp
->block_size
;
1868 bytes
= blks
* STp
->block_size
;
1872 memset(cmd
, 0, MAX_COMMAND_SIZE
);
1874 cmd
[1] = (STp
->block_size
!= 0);
1875 if (!cmd
[1] && STp
->sili
)
1877 cmd
[2] = blks
>> 16;
1882 SRpnt
= st_do_scsi(SRpnt
, STp
, cmd
, bytes
, DMA_FROM_DEVICE
,
1883 STp
->device
->request_queue
->rq_timeout
,
1885 release_buffering(STp
, 1);
1888 return STbp
->syscall_result
;
1890 STbp
->read_pointer
= 0;
1893 /* Something to check */
1894 if (STbp
->syscall_result
) {
1895 struct st_cmdstatus
*cmdstatp
= &STp
->buffer
->cmdstat
;
1899 "Sense: %2x %2x %2x %2x %2x %2x %2x %2x\n",
1900 SRpnt
->sense
[0], SRpnt
->sense
[1],
1901 SRpnt
->sense
[2], SRpnt
->sense
[3],
1902 SRpnt
->sense
[4], SRpnt
->sense
[5],
1903 SRpnt
->sense
[6], SRpnt
->sense
[7]);
1904 if (cmdstatp
->have_sense
) {
1906 if (cmdstatp
->sense_hdr
.sense_key
== BLANK_CHECK
)
1907 cmdstatp
->flags
&= 0xcf; /* No need for EOM in this case */
1909 if (cmdstatp
->flags
!= 0) { /* EOF, EOM, or ILI */
1910 /* Compute the residual count */
1911 if (cmdstatp
->remainder_valid
)
1912 transfer
= (int)cmdstatp
->uremainder64
;
1915 if (STp
->block_size
== 0 &&
1916 cmdstatp
->sense_hdr
.sense_key
== MEDIUM_ERROR
)
1919 if (cmdstatp
->flags
& SENSE_ILI
) { /* ILI */
1920 if (STp
->block_size
== 0 &&
1922 st_printk(KERN_NOTICE
, STp
,
1923 "Failed to read %d "
1924 "byte block with %d "
1928 if (STps
->drv_block
>= 0)
1929 STps
->drv_block
+= 1;
1930 STbp
->buffer_bytes
= 0;
1932 } else if (STp
->block_size
== 0) {
1933 STbp
->buffer_bytes
= bytes
- transfer
;
1935 st_release_request(SRpnt
);
1936 SRpnt
= *aSRpnt
= NULL
;
1937 if (transfer
== blks
) { /* We did not get anything, error */
1938 st_printk(KERN_NOTICE
, STp
,
1941 if (STps
->drv_block
>= 0)
1942 STps
->drv_block
+= blks
- transfer
+ 1;
1943 st_int_ioctl(STp
, MTBSR
, 1);
1946 /* We have some data, deliver it */
1947 STbp
->buffer_bytes
= (blks
- transfer
) *
1949 DEBC_printk(STp
, "ILI but "
1953 STbp
->buffer_bytes
);
1954 if (STps
->drv_block
>= 0)
1955 STps
->drv_block
+= 1;
1956 if (st_int_ioctl(STp
, MTBSR
, 1))
1959 } else if (cmdstatp
->flags
& SENSE_FMK
) { /* FM overrides EOM */
1960 if (STps
->eof
!= ST_FM_HIT
)
1961 STps
->eof
= ST_FM_HIT
;
1963 STps
->eof
= ST_EOD_2
;
1964 if (STp
->block_size
== 0)
1965 STbp
->buffer_bytes
= 0;
1967 STbp
->buffer_bytes
=
1968 bytes
- transfer
* STp
->block_size
;
1969 DEBC_printk(STp
, "EOF detected (%d "
1971 STbp
->buffer_bytes
);
1972 } else if (cmdstatp
->flags
& SENSE_EOM
) {
1973 if (STps
->eof
== ST_FM
)
1974 STps
->eof
= ST_EOD_1
;
1976 STps
->eof
= ST_EOM_OK
;
1977 if (STp
->block_size
== 0)
1978 STbp
->buffer_bytes
= bytes
- transfer
;
1980 STbp
->buffer_bytes
=
1981 bytes
- transfer
* STp
->block_size
;
1983 DEBC_printk(STp
, "EOM detected (%d "
1985 STbp
->buffer_bytes
);
1988 /* end of EOF, EOM, ILI test */
1989 else { /* nonzero sense key */
1990 DEBC_printk(STp
, "Tape error while reading.\n");
1991 STps
->drv_block
= (-1);
1992 if (STps
->eof
== ST_FM
&&
1993 cmdstatp
->sense_hdr
.sense_key
== BLANK_CHECK
) {
1994 DEBC_printk(STp
, "Zero returned for "
1995 "first BLANK CHECK "
1997 STps
->eof
= ST_EOD_2
; /* First BLANK_CHECK after FM */
1998 } else /* Some other extended sense code */
2002 if (STbp
->buffer_bytes
< 0) /* Caused by bogus sense data */
2003 STbp
->buffer_bytes
= 0;
2005 /* End of extended sense test */
2006 else { /* Non-extended sense */
2007 retval
= STbp
->syscall_result
;
2011 /* End of error handling */
2012 else { /* Read successful */
2013 STbp
->buffer_bytes
= bytes
;
2014 if (STp
->sili
) /* In fixed block mode residual is always zero here */
2015 STbp
->buffer_bytes
-= STp
->buffer
->cmdstat
.residual
;
2018 if (STps
->drv_block
>= 0) {
2019 if (STp
->block_size
== 0)
2022 STps
->drv_block
+= STbp
->buffer_bytes
/ STp
->block_size
;
2030 st_read(struct file
*filp
, char __user
*buf
, size_t count
, loff_t
* ppos
)
2034 ssize_t i
, transfer
;
2035 int special
, do_dio
= 0;
2036 struct st_request
*SRpnt
= NULL
;
2037 struct scsi_tape
*STp
= filp
->private_data
;
2038 struct st_modedef
*STm
;
2039 struct st_partstat
*STps
;
2040 struct st_buffer
*STbp
= STp
->buffer
;
2042 if (mutex_lock_interruptible(&STp
->lock
))
2043 return -ERESTARTSYS
;
2045 retval
= rw_checks(STp
, filp
, count
);
2046 if (retval
|| count
== 0)
2049 STm
= &(STp
->modes
[STp
->current_mode
]);
2050 if (STp
->block_size
!= 0 && (count
% STp
->block_size
) != 0) {
2051 if (!STm
->do_read_ahead
) {
2052 retval
= (-EINVAL
); /* Read must be integral number of blocks */
2055 STp
->try_dio_now
= 0; /* Direct i/o can't handle split blocks */
2058 STps
= &(STp
->ps
[STp
->partition
]);
2059 if (STps
->rw
== ST_WRITING
) {
2060 retval
= flush_buffer(STp
, 0);
2063 STps
->rw
= ST_READING
;
2066 if (debugging
&& STps
->eof
!= ST_NOEOF
)
2067 st_printk(ST_DEB_MSG
, STp
,
2068 "EOF/EOM flag up (%d). Bytes %d\n",
2069 STps
->eof
, STbp
->buffer_bytes
);
2072 retval
= setup_buffering(STp
, buf
, count
, 1);
2075 do_dio
= STbp
->do_dio
;
2077 if (STbp
->buffer_bytes
== 0 &&
2078 STps
->eof
>= ST_EOD_1
) {
2079 if (STps
->eof
< ST_EOD
) {
2084 retval
= (-EIO
); /* EOM or Blank Check */
2089 /* Check the buffer writability before any tape movement. Don't alter
2091 if (copy_from_user(&i
, buf
, 1) != 0 ||
2092 copy_to_user(buf
, &i
, 1) != 0 ||
2093 copy_from_user(&i
, buf
+ count
- 1, 1) != 0 ||
2094 copy_to_user(buf
+ count
- 1, &i
, 1) != 0) {
2100 STps
->rw
= ST_READING
;
2103 /* Loop until enough data in buffer or a special condition found */
2104 for (total
= 0, special
= 0; total
< count
&& !special
;) {
2106 /* Get new data if the buffer is empty */
2107 if (STbp
->buffer_bytes
== 0) {
2108 special
= read_tape(STp
, count
- total
, &SRpnt
);
2109 if (special
< 0) { /* No need to continue read */
2115 /* Move the data from driver buffer to user buffer */
2116 if (STbp
->buffer_bytes
> 0) {
2118 if (debugging
&& STps
->eof
!= ST_NOEOF
)
2119 st_printk(ST_DEB_MSG
, STp
,
2120 "EOF up (%d). Left %d, needed %d.\n",
2121 STps
->eof
, STbp
->buffer_bytes
,
2122 (int)(count
- total
));
2124 transfer
= STbp
->buffer_bytes
< count
- total
?
2125 STbp
->buffer_bytes
: count
- total
;
2127 i
= from_buffer(STbp
, buf
, transfer
);
2137 if (STp
->block_size
== 0)
2138 break; /* Read only one variable length block */
2140 } /* for (total = 0, special = 0;
2141 total < count && !special; ) */
2143 /* Change the eof state if no data from tape or buffer */
2145 if (STps
->eof
== ST_FM_HIT
) {
2147 STps
->drv_block
= 0;
2148 if (STps
->drv_file
>= 0)
2150 } else if (STps
->eof
== ST_EOD_1
) {
2151 STps
->eof
= ST_EOD_2
;
2152 STps
->drv_block
= 0;
2153 if (STps
->drv_file
>= 0)
2155 } else if (STps
->eof
== ST_EOD_2
)
2157 } else if (STps
->eof
== ST_FM
)
2158 STps
->eof
= ST_NOEOF
;
2162 if (SRpnt
!= NULL
) {
2163 st_release_request(SRpnt
);
2167 release_buffering(STp
, 1);
2168 STbp
->buffer_bytes
= 0;
2170 mutex_unlock(&STp
->lock
);
2178 /* Set the driver options */
2179 static void st_log_options(struct scsi_tape
* STp
, struct st_modedef
* STm
)
2182 st_printk(KERN_INFO
, STp
,
2183 "Mode %d options: buffer writes: %d, "
2184 "async writes: %d, read ahead: %d\n",
2185 STp
->current_mode
, STm
->do_buffer_writes
,
2186 STm
->do_async_writes
, STm
->do_read_ahead
);
2187 st_printk(KERN_INFO
, STp
,
2188 " can bsr: %d, two FMs: %d, "
2189 "fast mteom: %d, auto lock: %d,\n",
2190 STp
->can_bsr
, STp
->two_fm
, STp
->fast_mteom
,
2192 st_printk(KERN_INFO
, STp
,
2193 " defs for wr: %d, no block limits: %d, "
2194 "partitions: %d, s2 log: %d\n",
2195 STm
->defaults_for_writes
, STp
->omit_blklims
,
2196 STp
->can_partitions
, STp
->scsi2_logical
);
2197 st_printk(KERN_INFO
, STp
,
2198 " sysv: %d nowait: %d sili: %d "
2199 "nowait_filemark: %d\n",
2200 STm
->sysv
, STp
->immediate
, STp
->sili
,
2201 STp
->immediate_filemark
);
2202 st_printk(KERN_INFO
, STp
, " debugging: %d\n", debugging
);
2208 static int st_set_options(struct scsi_tape
*STp
, long options
)
2212 struct st_modedef
*STm
;
2213 struct cdev
*cd0
, *cd1
;
2214 struct device
*d0
, *d1
;
2216 STm
= &(STp
->modes
[STp
->current_mode
]);
2217 if (!STm
->defined
) {
2218 cd0
= STm
->cdevs
[0];
2219 cd1
= STm
->cdevs
[1];
2222 memcpy(STm
, &(STp
->modes
[0]), sizeof(struct st_modedef
));
2223 STm
->cdevs
[0] = cd0
;
2224 STm
->cdevs
[1] = cd1
;
2228 DEBC_printk(STp
, "Initialized mode %d definition from mode 0\n",
2232 code
= options
& MT_ST_OPTIONS
;
2233 if (code
== MT_ST_BOOLEANS
) {
2234 STm
->do_buffer_writes
= (options
& MT_ST_BUFFER_WRITES
) != 0;
2235 STm
->do_async_writes
= (options
& MT_ST_ASYNC_WRITES
) != 0;
2236 STm
->defaults_for_writes
= (options
& MT_ST_DEF_WRITES
) != 0;
2237 STm
->do_read_ahead
= (options
& MT_ST_READ_AHEAD
) != 0;
2238 STp
->two_fm
= (options
& MT_ST_TWO_FM
) != 0;
2239 STp
->fast_mteom
= (options
& MT_ST_FAST_MTEOM
) != 0;
2240 STp
->do_auto_lock
= (options
& MT_ST_AUTO_LOCK
) != 0;
2241 STp
->can_bsr
= (options
& MT_ST_CAN_BSR
) != 0;
2242 STp
->omit_blklims
= (options
& MT_ST_NO_BLKLIMS
) != 0;
2243 if ((STp
->device
)->scsi_level
>= SCSI_2
)
2244 STp
->can_partitions
= (options
& MT_ST_CAN_PARTITIONS
) != 0;
2245 STp
->scsi2_logical
= (options
& MT_ST_SCSI2LOGICAL
) != 0;
2246 STp
->immediate
= (options
& MT_ST_NOWAIT
) != 0;
2247 STp
->immediate_filemark
= (options
& MT_ST_NOWAIT_EOF
) != 0;
2248 STm
->sysv
= (options
& MT_ST_SYSV
) != 0;
2249 STp
->sili
= (options
& MT_ST_SILI
) != 0;
2250 DEB( debugging
= (options
& MT_ST_DEBUGGING
) != 0;
2251 st_log_options(STp
, STm
); )
2252 } else if (code
== MT_ST_SETBOOLEANS
|| code
== MT_ST_CLEARBOOLEANS
) {
2253 value
= (code
== MT_ST_SETBOOLEANS
);
2254 if ((options
& MT_ST_BUFFER_WRITES
) != 0)
2255 STm
->do_buffer_writes
= value
;
2256 if ((options
& MT_ST_ASYNC_WRITES
) != 0)
2257 STm
->do_async_writes
= value
;
2258 if ((options
& MT_ST_DEF_WRITES
) != 0)
2259 STm
->defaults_for_writes
= value
;
2260 if ((options
& MT_ST_READ_AHEAD
) != 0)
2261 STm
->do_read_ahead
= value
;
2262 if ((options
& MT_ST_TWO_FM
) != 0)
2263 STp
->two_fm
= value
;
2264 if ((options
& MT_ST_FAST_MTEOM
) != 0)
2265 STp
->fast_mteom
= value
;
2266 if ((options
& MT_ST_AUTO_LOCK
) != 0)
2267 STp
->do_auto_lock
= value
;
2268 if ((options
& MT_ST_CAN_BSR
) != 0)
2269 STp
->can_bsr
= value
;
2270 if ((options
& MT_ST_NO_BLKLIMS
) != 0)
2271 STp
->omit_blklims
= value
;
2272 if ((STp
->device
)->scsi_level
>= SCSI_2
&&
2273 (options
& MT_ST_CAN_PARTITIONS
) != 0)
2274 STp
->can_partitions
= value
;
2275 if ((options
& MT_ST_SCSI2LOGICAL
) != 0)
2276 STp
->scsi2_logical
= value
;
2277 if ((options
& MT_ST_NOWAIT
) != 0)
2278 STp
->immediate
= value
;
2279 if ((options
& MT_ST_NOWAIT_EOF
) != 0)
2280 STp
->immediate_filemark
= value
;
2281 if ((options
& MT_ST_SYSV
) != 0)
2283 if ((options
& MT_ST_SILI
) != 0)
2286 if ((options
& MT_ST_DEBUGGING
) != 0)
2288 st_log_options(STp
, STm
); )
2289 } else if (code
== MT_ST_WRITE_THRESHOLD
) {
2290 /* Retained for compatibility */
2291 } else if (code
== MT_ST_DEF_BLKSIZE
) {
2292 value
= (options
& ~MT_ST_OPTIONS
);
2293 if (value
== ~MT_ST_OPTIONS
) {
2294 STm
->default_blksize
= (-1);
2295 DEBC_printk(STp
, "Default block size disabled.\n");
2297 STm
->default_blksize
= value
;
2298 DEBC_printk(STp
,"Default block size set to "
2299 "%d bytes.\n", STm
->default_blksize
);
2300 if (STp
->ready
== ST_READY
) {
2301 STp
->blksize_changed
= 0;
2302 set_mode_densblk(STp
, STm
);
2305 } else if (code
== MT_ST_TIMEOUTS
) {
2306 value
= (options
& ~MT_ST_OPTIONS
);
2307 if ((value
& MT_ST_SET_LONG_TIMEOUT
) != 0) {
2308 STp
->long_timeout
= (value
& ~MT_ST_SET_LONG_TIMEOUT
) * HZ
;
2309 DEBC_printk(STp
, "Long timeout set to %d seconds.\n",
2310 (value
& ~MT_ST_SET_LONG_TIMEOUT
));
2312 blk_queue_rq_timeout(STp
->device
->request_queue
,
2314 DEBC_printk(STp
, "Normal timeout set to %d seconds.\n",
2317 } else if (code
== MT_ST_SET_CLN
) {
2318 value
= (options
& ~MT_ST_OPTIONS
) & 0xff;
2320 (value
< EXTENDED_SENSE_START
||
2321 value
>= SCSI_SENSE_BUFFERSIZE
))
2323 STp
->cln_mode
= value
;
2324 STp
->cln_sense_mask
= (options
>> 8) & 0xff;
2325 STp
->cln_sense_value
= (options
>> 16) & 0xff;
2326 st_printk(KERN_INFO
, STp
,
2327 "Cleaning request mode %d, mask %02x, value %02x\n",
2328 value
, STp
->cln_sense_mask
, STp
->cln_sense_value
);
2329 } else if (code
== MT_ST_DEF_OPTIONS
) {
2330 code
= (options
& ~MT_ST_CLEAR_DEFAULT
);
2331 value
= (options
& MT_ST_CLEAR_DEFAULT
);
2332 if (code
== MT_ST_DEF_DENSITY
) {
2333 if (value
== MT_ST_CLEAR_DEFAULT
) {
2334 STm
->default_density
= (-1);
2336 "Density default disabled.\n");
2338 STm
->default_density
= value
& 0xff;
2339 DEBC_printk(STp
, "Density default set to %x\n",
2340 STm
->default_density
);
2341 if (STp
->ready
== ST_READY
) {
2342 STp
->density_changed
= 0;
2343 set_mode_densblk(STp
, STm
);
2346 } else if (code
== MT_ST_DEF_DRVBUFFER
) {
2347 if (value
== MT_ST_CLEAR_DEFAULT
) {
2348 STp
->default_drvbuffer
= 0xff;
2350 "Drive buffer default disabled.\n");
2352 STp
->default_drvbuffer
= value
& 7;
2354 "Drive buffer default set to %x\n",
2355 STp
->default_drvbuffer
);
2356 if (STp
->ready
== ST_READY
)
2357 st_int_ioctl(STp
, MTSETDRVBUFFER
, STp
->default_drvbuffer
);
2359 } else if (code
== MT_ST_DEF_COMPRESSION
) {
2360 if (value
== MT_ST_CLEAR_DEFAULT
) {
2361 STm
->default_compression
= ST_DONT_TOUCH
;
2363 "Compression default disabled.\n");
2365 if ((value
& 0xff00) != 0) {
2366 STp
->c_algo
= (value
& 0xff00) >> 8;
2367 DEBC_printk(STp
, "Compression "
2368 "algorithm set to 0x%x.\n",
2371 if ((value
& 0xff) != 0xff) {
2372 STm
->default_compression
= (value
& 1 ? ST_YES
: ST_NO
);
2373 DEBC_printk(STp
, "Compression default "
2376 if (STp
->ready
== ST_READY
) {
2377 STp
->compression_changed
= 0;
2378 st_compression(STp
, (STm
->default_compression
== ST_YES
));
2389 #define MODE_HEADER_LENGTH 4
2391 /* Mode header and page byte offsets */
2392 #define MH_OFF_DATA_LENGTH 0
2393 #define MH_OFF_MEDIUM_TYPE 1
2394 #define MH_OFF_DEV_SPECIFIC 2
2395 #define MH_OFF_BDESCS_LENGTH 3
2396 #define MP_OFF_PAGE_NBR 0
2397 #define MP_OFF_PAGE_LENGTH 1
2399 /* Mode header and page bit masks */
2400 #define MH_BIT_WP 0x80
2401 #define MP_MSK_PAGE_NBR 0x3f
2403 /* Don't return block descriptors */
2404 #define MODE_SENSE_OMIT_BDESCS 0x08
2406 #define MODE_SELECT_PAGE_FORMAT 0x10
2408 /* Read a mode page into the tape buffer. The block descriptors are included
2409 if incl_block_descs is true. The page control is ored to the page number
2410 parameter, if necessary. */
2411 static int read_mode_page(struct scsi_tape
*STp
, int page
, int omit_block_descs
)
2413 unsigned char cmd
[MAX_COMMAND_SIZE
];
2414 struct st_request
*SRpnt
;
2416 memset(cmd
, 0, MAX_COMMAND_SIZE
);
2417 cmd
[0] = MODE_SENSE
;
2418 if (omit_block_descs
)
2419 cmd
[1] = MODE_SENSE_OMIT_BDESCS
;
2423 SRpnt
= st_do_scsi(NULL
, STp
, cmd
, cmd
[4], DMA_FROM_DEVICE
,
2424 STp
->device
->request_queue
->rq_timeout
, 0, 1);
2426 return (STp
->buffer
)->syscall_result
;
2428 st_release_request(SRpnt
);
2430 return STp
->buffer
->syscall_result
;
2434 /* Send the mode page in the tape buffer to the drive. Assumes that the mode data
2435 in the buffer is correctly formatted. The long timeout is used if slow is non-zero. */
2436 static int write_mode_page(struct scsi_tape
*STp
, int page
, int slow
)
2439 unsigned char cmd
[MAX_COMMAND_SIZE
];
2440 struct st_request
*SRpnt
;
2443 memset(cmd
, 0, MAX_COMMAND_SIZE
);
2444 cmd
[0] = MODE_SELECT
;
2445 cmd
[1] = MODE_SELECT_PAGE_FORMAT
;
2446 pgo
= MODE_HEADER_LENGTH
+ (STp
->buffer
)->b_data
[MH_OFF_BDESCS_LENGTH
];
2447 cmd
[4] = pgo
+ (STp
->buffer
)->b_data
[pgo
+ MP_OFF_PAGE_LENGTH
] + 2;
2449 /* Clear reserved fields */
2450 (STp
->buffer
)->b_data
[MH_OFF_DATA_LENGTH
] = 0;
2451 (STp
->buffer
)->b_data
[MH_OFF_MEDIUM_TYPE
] = 0;
2452 (STp
->buffer
)->b_data
[MH_OFF_DEV_SPECIFIC
] &= ~MH_BIT_WP
;
2453 (STp
->buffer
)->b_data
[pgo
+ MP_OFF_PAGE_NBR
] &= MP_MSK_PAGE_NBR
;
2456 STp
->long_timeout
: STp
->device
->request_queue
->rq_timeout
;
2457 SRpnt
= st_do_scsi(NULL
, STp
, cmd
, cmd
[4], DMA_TO_DEVICE
,
2460 return (STp
->buffer
)->syscall_result
;
2462 st_release_request(SRpnt
);
2464 return STp
->buffer
->syscall_result
;
2468 #define COMPRESSION_PAGE 0x0f
2469 #define COMPRESSION_PAGE_LENGTH 16
2471 #define CP_OFF_DCE_DCC 2
2472 #define CP_OFF_C_ALGO 7
2474 #define DCE_MASK 0x80
2475 #define DCC_MASK 0x40
2476 #define RED_MASK 0x60
2479 /* Control the compression with mode page 15. Algorithm not changed if zero.
2481 The block descriptors are read and written because Sony SDT-7000 does not
2482 work without this (suggestion from Michael Schaefer <Michael.Schaefer@dlr.de>).
2483 Including block descriptors should not cause any harm to other drives. */
2485 static int st_compression(struct scsi_tape
* STp
, int state
)
2488 int mpoffs
; /* Offset to mode page start */
2489 unsigned char *b_data
= (STp
->buffer
)->b_data
;
2491 if (STp
->ready
!= ST_READY
)
2494 /* Read the current page contents */
2495 retval
= read_mode_page(STp
, COMPRESSION_PAGE
, 0);
2497 DEBC_printk(STp
, "Compression mode page not supported.\n");
2501 mpoffs
= MODE_HEADER_LENGTH
+ b_data
[MH_OFF_BDESCS_LENGTH
];
2502 DEBC_printk(STp
, "Compression state is %d.\n",
2503 (b_data
[mpoffs
+ CP_OFF_DCE_DCC
] & DCE_MASK
? 1 : 0));
2505 /* Check if compression can be changed */
2506 if ((b_data
[mpoffs
+ CP_OFF_DCE_DCC
] & DCC_MASK
) == 0) {
2507 DEBC_printk(STp
, "Compression not supported.\n");
2513 b_data
[mpoffs
+ CP_OFF_DCE_DCC
] |= DCE_MASK
;
2514 if (STp
->c_algo
!= 0)
2515 b_data
[mpoffs
+ CP_OFF_C_ALGO
] = STp
->c_algo
;
2518 b_data
[mpoffs
+ CP_OFF_DCE_DCC
] &= ~DCE_MASK
;
2519 if (STp
->c_algo
!= 0)
2520 b_data
[mpoffs
+ CP_OFF_C_ALGO
] = 0; /* no compression */
2523 retval
= write_mode_page(STp
, COMPRESSION_PAGE
, 0);
2525 DEBC_printk(STp
, "Compression change failed.\n");
2528 DEBC_printk(STp
, "Compression state changed to %d.\n", state
);
2530 STp
->compression_changed
= 1;
2535 /* Process the load and unload commands (does unload if the load code is zero) */
2536 static int do_load_unload(struct scsi_tape
*STp
, struct file
*filp
, int load_code
)
2538 int retval
= (-EIO
), timeout
;
2539 unsigned char cmd
[MAX_COMMAND_SIZE
];
2540 struct st_partstat
*STps
;
2541 struct st_request
*SRpnt
;
2543 if (STp
->ready
!= ST_READY
&& !load_code
) {
2544 if (STp
->ready
== ST_NO_TAPE
)
2545 return (-ENOMEDIUM
);
2550 memset(cmd
, 0, MAX_COMMAND_SIZE
);
2551 cmd
[0] = START_STOP
;
2555 * If arg >= 1 && arg <= 6 Enhanced load/unload in HP C1553A
2557 if (load_code
>= 1 + MT_ST_HPLOADER_OFFSET
2558 && load_code
<= 6 + MT_ST_HPLOADER_OFFSET
) {
2559 DEBC_printk(STp
, " Enhanced %sload slot %2d.\n",
2560 (cmd
[4]) ? "" : "un",
2561 load_code
- MT_ST_HPLOADER_OFFSET
);
2562 cmd
[3] = load_code
- MT_ST_HPLOADER_OFFSET
; /* MediaID field of C1553A */
2564 if (STp
->immediate
) {
2565 cmd
[1] = 1; /* Don't wait for completion */
2566 timeout
= STp
->device
->request_queue
->rq_timeout
;
2569 timeout
= STp
->long_timeout
;
2573 st_printk(ST_DEB_MSG
, STp
, "Unloading tape.\n");
2575 st_printk(ST_DEB_MSG
, STp
, "Loading tape.\n");
2578 SRpnt
= st_do_scsi(NULL
, STp
, cmd
, 0, DMA_NONE
,
2579 timeout
, MAX_RETRIES
, 1);
2581 return (STp
->buffer
)->syscall_result
;
2583 retval
= (STp
->buffer
)->syscall_result
;
2584 st_release_request(SRpnt
);
2586 if (!retval
) { /* SCSI command successful */
2589 STp
->rew_at_close
= 0;
2590 STp
->ready
= ST_NO_TAPE
;
2593 STp
->rew_at_close
= STp
->autorew_dev
;
2594 retval
= check_tape(STp
, filp
);
2600 STps
= &(STp
->ps
[STp
->partition
]);
2601 STps
->drv_file
= STps
->drv_block
= (-1);
2608 #define ST_DEB_FORWARD 0
2609 #define ST_DEB_BACKWARD 1
2610 static void deb_space_print(struct scsi_tape
*STp
, int direction
, char *units
, unsigned char *cmd
)
2617 sc
= cmd
[2] & 0x80 ? 0xff000000 : 0;
2618 sc
|= (cmd
[2] << 16) | (cmd
[3] << 8) | cmd
[4];
2621 st_printk(ST_DEB_MSG
, STp
, "Spacing tape %s over %d %s.\n",
2622 direction
? "backward" : "forward", sc
, units
);
2625 #define ST_DEB_FORWARD 0
2626 #define ST_DEB_BACKWARD 1
2627 static void deb_space_print(struct scsi_tape
*STp
, int direction
, char *units
, unsigned char *cmd
) {}
2631 /* Internal ioctl function */
2632 static int st_int_ioctl(struct scsi_tape
*STp
, unsigned int cmd_in
, unsigned long arg
)
2638 unsigned char cmd
[MAX_COMMAND_SIZE
];
2639 struct st_request
*SRpnt
;
2640 struct st_partstat
*STps
;
2641 int fileno
, blkno
, at_sm
, undone
;
2642 int datalen
= 0, direction
= DMA_NONE
;
2644 WARN_ON(STp
->buffer
->do_dio
!= 0);
2645 if (STp
->ready
!= ST_READY
) {
2646 if (STp
->ready
== ST_NO_TAPE
)
2647 return (-ENOMEDIUM
);
2651 timeout
= STp
->long_timeout
;
2652 STps
= &(STp
->ps
[STp
->partition
]);
2653 fileno
= STps
->drv_file
;
2654 blkno
= STps
->drv_block
;
2655 at_sm
= STps
->at_sm
;
2657 memset(cmd
, 0, MAX_COMMAND_SIZE
);
2660 chg_eof
= 0; /* Changed from the FSF after this */
2663 cmd
[1] = 0x01; /* Space FileMarks */
2664 cmd
[2] = (arg
>> 16);
2665 cmd
[3] = (arg
>> 8);
2667 deb_space_print(STp
, ST_DEB_FORWARD
, "filemarks", cmd
);
2671 at_sm
&= (arg
== 0);
2674 chg_eof
= 0; /* Changed from the FSF after this */
2677 cmd
[1] = 0x01; /* Space FileMarks */
2679 cmd
[2] = (ltmp
>> 16);
2680 cmd
[3] = (ltmp
>> 8);
2682 deb_space_print(STp
, ST_DEB_BACKWARD
, "filemarks", cmd
);
2685 blkno
= (-1); /* We can't know the block number */
2686 at_sm
&= (arg
== 0);
2690 cmd
[1] = 0x00; /* Space Blocks */
2691 cmd
[2] = (arg
>> 16);
2692 cmd
[3] = (arg
>> 8);
2694 deb_space_print(STp
, ST_DEB_FORWARD
, "blocks", cmd
);
2697 at_sm
&= (arg
== 0);
2701 cmd
[1] = 0x00; /* Space Blocks */
2703 cmd
[2] = (ltmp
>> 16);
2704 cmd
[3] = (ltmp
>> 8);
2706 deb_space_print(STp
, ST_DEB_BACKWARD
, "blocks", cmd
);
2709 at_sm
&= (arg
== 0);
2713 cmd
[1] = 0x04; /* Space Setmarks */
2714 cmd
[2] = (arg
>> 16);
2715 cmd
[3] = (arg
>> 8);
2717 deb_space_print(STp
, ST_DEB_FORWARD
, "setmarks", cmd
);
2719 blkno
= fileno
= (-1);
2725 cmd
[1] = 0x04; /* Space Setmarks */
2727 cmd
[2] = (ltmp
>> 16);
2728 cmd
[3] = (ltmp
>> 8);
2730 deb_space_print(STp
, ST_DEB_BACKWARD
, "setmarks", cmd
);
2732 blkno
= fileno
= (-1);
2739 if (STp
->write_prot
)
2741 cmd
[0] = WRITE_FILEMARKS
;
2742 if (cmd_in
== MTWSM
)
2744 if (cmd_in
== MTWEOFI
||
2745 (cmd_in
== MTWEOF
&& STp
->immediate_filemark
))
2747 cmd
[2] = (arg
>> 16);
2748 cmd
[3] = (arg
>> 8);
2750 timeout
= STp
->device
->request_queue
->rq_timeout
;
2752 if (cmd_in
!= MTWSM
)
2753 st_printk(ST_DEB_MSG
, STp
,
2754 "Writing %d filemarks.\n",
2759 st_printk(ST_DEB_MSG
, STp
,
2760 "Writing %d setmarks.\n",
2768 at_sm
= (cmd_in
== MTWSM
);
2771 cmd
[0] = REZERO_UNIT
;
2772 if (STp
->immediate
) {
2773 cmd
[1] = 1; /* Don't wait for completion */
2774 timeout
= STp
->device
->request_queue
->rq_timeout
;
2776 DEBC_printk(STp
, "Rewinding tape.\n");
2777 fileno
= blkno
= at_sm
= 0;
2780 DEBC_printk(STp
, "No op on tape.\n");
2781 return 0; /* Should do something ? */
2784 cmd
[0] = START_STOP
;
2785 if (STp
->immediate
) {
2786 cmd
[1] = 1; /* Don't wait for completion */
2787 timeout
= STp
->device
->request_queue
->rq_timeout
;
2790 DEBC_printk(STp
, "Retensioning tape.\n");
2791 fileno
= blkno
= at_sm
= 0;
2794 if (!STp
->fast_mteom
) {
2795 /* space to the end of tape */
2796 ioctl_result
= st_int_ioctl(STp
, MTFSF
, 0x7fffff);
2797 fileno
= STps
->drv_file
;
2798 if (STps
->eof
>= ST_EOD_1
)
2800 /* The next lines would hide the number of spaced FileMarks
2801 That's why I inserted the previous lines. I had no luck
2802 with detecting EOM with FSF, so we go now to EOM.
2808 DEBC_printk(STp
, "Spacing to end of recorded medium.\n");
2813 if (STp
->write_prot
)
2816 cmd
[1] = (arg
? 1 : 0); /* Long erase with non-zero argument */
2817 if (STp
->immediate
) {
2818 cmd
[1] |= 2; /* Don't wait for completion */
2819 timeout
= STp
->device
->request_queue
->rq_timeout
;
2822 timeout
= STp
->long_timeout
* 8;
2824 DEBC_printk(STp
, "Erasing tape.\n");
2825 fileno
= blkno
= at_sm
= 0;
2827 case MTSETBLK
: /* Set block length */
2828 case MTSETDENSITY
: /* Set tape density */
2829 case MTSETDRVBUFFER
: /* Set drive buffering */
2830 case SET_DENS_AND_BLK
: /* Set density and block size */
2832 if (STp
->dirty
|| (STp
->buffer
)->buffer_bytes
!= 0)
2833 return (-EIO
); /* Not allowed if data in buffer */
2834 if ((cmd_in
== MTSETBLK
|| cmd_in
== SET_DENS_AND_BLK
) &&
2835 (arg
& MT_ST_BLKSIZE_MASK
) != 0 &&
2836 STp
->max_block
> 0 &&
2837 ((arg
& MT_ST_BLKSIZE_MASK
) < STp
->min_block
||
2838 (arg
& MT_ST_BLKSIZE_MASK
) > STp
->max_block
)) {
2839 st_printk(KERN_WARNING
, STp
, "Illegal block size.\n");
2842 cmd
[0] = MODE_SELECT
;
2843 if ((STp
->use_pf
& USE_PF
))
2844 cmd
[1] = MODE_SELECT_PAGE_FORMAT
;
2845 cmd
[4] = datalen
= 12;
2846 direction
= DMA_TO_DEVICE
;
2848 memset((STp
->buffer
)->b_data
, 0, 12);
2849 if (cmd_in
== MTSETDRVBUFFER
)
2850 (STp
->buffer
)->b_data
[2] = (arg
& 7) << 4;
2852 (STp
->buffer
)->b_data
[2] =
2853 STp
->drv_buffer
<< 4;
2854 (STp
->buffer
)->b_data
[3] = 8; /* block descriptor length */
2855 if (cmd_in
== MTSETDENSITY
) {
2856 (STp
->buffer
)->b_data
[4] = arg
;
2857 STp
->density_changed
= 1; /* At least we tried ;-) */
2858 } else if (cmd_in
== SET_DENS_AND_BLK
)
2859 (STp
->buffer
)->b_data
[4] = arg
>> 24;
2861 (STp
->buffer
)->b_data
[4] = STp
->density
;
2862 if (cmd_in
== MTSETBLK
|| cmd_in
== SET_DENS_AND_BLK
) {
2863 ltmp
= arg
& MT_ST_BLKSIZE_MASK
;
2864 if (cmd_in
== MTSETBLK
)
2865 STp
->blksize_changed
= 1; /* At least we tried ;-) */
2867 ltmp
= STp
->block_size
;
2868 (STp
->buffer
)->b_data
[9] = (ltmp
>> 16);
2869 (STp
->buffer
)->b_data
[10] = (ltmp
>> 8);
2870 (STp
->buffer
)->b_data
[11] = ltmp
;
2871 timeout
= STp
->device
->request_queue
->rq_timeout
;
2873 if (cmd_in
== MTSETBLK
|| cmd_in
== SET_DENS_AND_BLK
)
2874 st_printk(ST_DEB_MSG
, STp
,
2875 "Setting block size to %d bytes.\n",
2876 (STp
->buffer
)->b_data
[9] * 65536 +
2877 (STp
->buffer
)->b_data
[10] * 256 +
2878 (STp
->buffer
)->b_data
[11]);
2879 if (cmd_in
== MTSETDENSITY
|| cmd_in
== SET_DENS_AND_BLK
)
2880 st_printk(ST_DEB_MSG
, STp
,
2881 "Setting density code to %x.\n",
2882 (STp
->buffer
)->b_data
[4]);
2883 if (cmd_in
== MTSETDRVBUFFER
)
2884 st_printk(ST_DEB_MSG
, STp
,
2885 "Setting drive buffer code to %d.\n",
2886 ((STp
->buffer
)->b_data
[2] >> 4) & 7);
2893 SRpnt
= st_do_scsi(NULL
, STp
, cmd
, datalen
, direction
,
2894 timeout
, MAX_RETRIES
, 1);
2896 return (STp
->buffer
)->syscall_result
;
2898 ioctl_result
= (STp
->buffer
)->syscall_result
;
2900 if (!ioctl_result
) { /* SCSI command successful */
2901 st_release_request(SRpnt
);
2903 STps
->drv_block
= blkno
;
2904 STps
->drv_file
= fileno
;
2905 STps
->at_sm
= at_sm
;
2907 if (cmd_in
== MTBSFM
)
2908 ioctl_result
= st_int_ioctl(STp
, MTFSF
, 1);
2909 else if (cmd_in
== MTFSFM
)
2910 ioctl_result
= st_int_ioctl(STp
, MTBSF
, 1);
2912 if (cmd_in
== MTSETBLK
|| cmd_in
== SET_DENS_AND_BLK
) {
2913 STp
->block_size
= arg
& MT_ST_BLKSIZE_MASK
;
2914 if (STp
->block_size
!= 0) {
2915 (STp
->buffer
)->buffer_blocks
=
2916 (STp
->buffer
)->buffer_size
/ STp
->block_size
;
2918 (STp
->buffer
)->buffer_bytes
= (STp
->buffer
)->read_pointer
= 0;
2919 if (cmd_in
== SET_DENS_AND_BLK
)
2920 STp
->density
= arg
>> MT_ST_DENSITY_SHIFT
;
2921 } else if (cmd_in
== MTSETDRVBUFFER
)
2922 STp
->drv_buffer
= (arg
& 7);
2923 else if (cmd_in
== MTSETDENSITY
)
2926 if (cmd_in
== MTEOM
)
2928 else if (cmd_in
== MTFSF
)
2931 STps
->eof
= ST_NOEOF
;
2933 if (cmd_in
== MTWEOF
|| cmd_in
== MTWEOFI
)
2934 STps
->rw
= ST_IDLE
; /* prevent automatic WEOF at close */
2935 } else { /* SCSI command was not completely successful. Don't return
2936 from this block without releasing the SCSI command block! */
2937 struct st_cmdstatus
*cmdstatp
= &STp
->buffer
->cmdstat
;
2939 if (cmdstatp
->flags
& SENSE_EOM
) {
2940 if (cmd_in
!= MTBSF
&& cmd_in
!= MTBSFM
&&
2941 cmd_in
!= MTBSR
&& cmd_in
!= MTBSS
)
2942 STps
->eof
= ST_EOM_OK
;
2943 STps
->drv_block
= 0;
2946 if (cmdstatp
->remainder_valid
)
2947 undone
= (int)cmdstatp
->uremainder64
;
2951 if ((cmd_in
== MTWEOF
|| cmd_in
== MTWEOFI
) &&
2952 cmdstatp
->have_sense
&&
2953 (cmdstatp
->flags
& SENSE_EOM
)) {
2954 if (cmdstatp
->sense_hdr
.sense_key
== NO_SENSE
||
2955 cmdstatp
->sense_hdr
.sense_key
== RECOVERED_ERROR
) {
2956 ioctl_result
= 0; /* EOF(s) written successfully at EOM */
2957 STps
->eof
= ST_NOEOF
;
2958 } else { /* Writing EOF(s) failed */
2962 STps
->eof
= ST_NOEOF
;
2964 STps
->drv_file
= fileno
;
2965 } else if ((cmd_in
== MTFSF
) || (cmd_in
== MTFSFM
)) {
2967 STps
->drv_file
= fileno
- undone
;
2969 STps
->drv_file
= fileno
;
2970 STps
->drv_block
= -1;
2971 STps
->eof
= ST_NOEOF
;
2972 } else if ((cmd_in
== MTBSF
) || (cmd_in
== MTBSFM
)) {
2973 if (arg
> 0 && undone
< 0) /* Some drives get this wrong */
2975 if (STps
->drv_file
>= 0)
2976 STps
->drv_file
= fileno
+ undone
;
2977 STps
->drv_block
= 0;
2978 STps
->eof
= ST_NOEOF
;
2979 } else if (cmd_in
== MTFSR
) {
2980 if (cmdstatp
->flags
& SENSE_FMK
) { /* Hit filemark */
2981 if (STps
->drv_file
>= 0)
2983 STps
->drv_block
= 0;
2986 if (blkno
>= undone
)
2987 STps
->drv_block
= blkno
- undone
;
2989 STps
->drv_block
= (-1);
2990 STps
->eof
= ST_NOEOF
;
2992 } else if (cmd_in
== MTBSR
) {
2993 if (cmdstatp
->flags
& SENSE_FMK
) { /* Hit filemark */
2995 STps
->drv_block
= (-1);
2997 if (arg
> 0 && undone
< 0) /* Some drives get this wrong */
2999 if (STps
->drv_block
>= 0)
3000 STps
->drv_block
= blkno
+ undone
;
3002 STps
->eof
= ST_NOEOF
;
3003 } else if (cmd_in
== MTEOM
) {
3004 STps
->drv_file
= (-1);
3005 STps
->drv_block
= (-1);
3007 } else if (cmd_in
== MTSETBLK
||
3008 cmd_in
== MTSETDENSITY
||
3009 cmd_in
== MTSETDRVBUFFER
||
3010 cmd_in
== SET_DENS_AND_BLK
) {
3011 if (cmdstatp
->sense_hdr
.sense_key
== ILLEGAL_REQUEST
&&
3012 !(STp
->use_pf
& PF_TESTED
)) {
3013 /* Try the other possible state of Page Format if not
3015 STp
->use_pf
= (STp
->use_pf
^ USE_PF
) | PF_TESTED
;
3016 st_release_request(SRpnt
);
3018 return st_int_ioctl(STp
, cmd_in
, arg
);
3021 STps
->eof
= ST_NOEOF
;
3023 if (cmdstatp
->sense_hdr
.sense_key
== BLANK_CHECK
)
3026 st_release_request(SRpnt
);
3030 return ioctl_result
;
3034 /* Get the tape position. If bt == 2, arg points into a kernel space mt_loc
3037 static int get_location(struct scsi_tape
*STp
, unsigned int *block
, int *partition
,
3041 unsigned char scmd
[MAX_COMMAND_SIZE
];
3042 struct st_request
*SRpnt
;
3044 if (STp
->ready
!= ST_READY
)
3047 memset(scmd
, 0, MAX_COMMAND_SIZE
);
3048 if ((STp
->device
)->scsi_level
< SCSI_2
) {
3049 scmd
[0] = QFA_REQUEST_BLOCK
;
3052 scmd
[0] = READ_POSITION
;
3053 if (!logical
&& !STp
->scsi2_logical
)
3056 SRpnt
= st_do_scsi(NULL
, STp
, scmd
, 20, DMA_FROM_DEVICE
,
3057 STp
->device
->request_queue
->rq_timeout
,
3058 MAX_READY_RETRIES
, 1);
3060 return (STp
->buffer
)->syscall_result
;
3062 if ((STp
->buffer
)->syscall_result
!= 0 ||
3063 (STp
->device
->scsi_level
>= SCSI_2
&&
3064 ((STp
->buffer
)->b_data
[0] & 4) != 0)) {
3065 *block
= *partition
= 0;
3066 DEBC_printk(STp
, " Can't read tape position.\n");
3070 if ((STp
->device
)->scsi_level
< SCSI_2
) {
3071 *block
= ((STp
->buffer
)->b_data
[0] << 16)
3072 + ((STp
->buffer
)->b_data
[1] << 8)
3073 + (STp
->buffer
)->b_data
[2];
3076 *block
= ((STp
->buffer
)->b_data
[4] << 24)
3077 + ((STp
->buffer
)->b_data
[5] << 16)
3078 + ((STp
->buffer
)->b_data
[6] << 8)
3079 + (STp
->buffer
)->b_data
[7];
3080 *partition
= (STp
->buffer
)->b_data
[1];
3081 if (((STp
->buffer
)->b_data
[0] & 0x80) &&
3082 (STp
->buffer
)->b_data
[1] == 0) /* BOP of partition 0 */
3083 STp
->ps
[0].drv_block
= STp
->ps
[0].drv_file
= 0;
3085 DEBC_printk(STp
, "Got tape pos. blk %d part %d.\n",
3086 *block
, *partition
);
3088 st_release_request(SRpnt
);
3095 /* Set the tape block and partition. Negative partition means that only the
3096 block should be set in vendor specific way. */
3097 static int set_location(struct scsi_tape
*STp
, unsigned int block
, int partition
,
3100 struct st_partstat
*STps
;
3104 unsigned char scmd
[MAX_COMMAND_SIZE
];
3105 struct st_request
*SRpnt
;
3107 if (STp
->ready
!= ST_READY
)
3109 timeout
= STp
->long_timeout
;
3110 STps
= &(STp
->ps
[STp
->partition
]);
3112 DEBC_printk(STp
, "Setting block to %d and partition to %d.\n",
3114 DEB(if (partition
< 0)
3117 /* Update the location at the partition we are leaving */
3118 if ((!STp
->can_partitions
&& partition
!= 0) ||
3119 partition
>= ST_NBR_PARTITIONS
)
3121 if (partition
!= STp
->partition
) {
3122 if (get_location(STp
, &blk
, &p
, 1))
3123 STps
->last_block_valid
= 0;
3125 STps
->last_block_valid
= 1;
3126 STps
->last_block_visited
= blk
;
3127 DEBC_printk(STp
, "Visited block %d for "
3128 "partition %d saved.\n",
3129 blk
, STp
->partition
);
3133 memset(scmd
, 0, MAX_COMMAND_SIZE
);
3134 if ((STp
->device
)->scsi_level
< SCSI_2
) {
3135 scmd
[0] = QFA_SEEK_BLOCK
;
3136 scmd
[2] = (block
>> 16);
3137 scmd
[3] = (block
>> 8);
3142 scmd
[3] = (block
>> 24);
3143 scmd
[4] = (block
>> 16);
3144 scmd
[5] = (block
>> 8);
3146 if (!logical
&& !STp
->scsi2_logical
)
3148 if (STp
->partition
!= partition
) {
3150 scmd
[8] = partition
;
3151 DEBC_printk(STp
, "Trying to change partition "
3152 "from %d to %d\n", STp
->partition
,
3156 if (STp
->immediate
) {
3157 scmd
[1] |= 1; /* Don't wait for completion */
3158 timeout
= STp
->device
->request_queue
->rq_timeout
;
3161 SRpnt
= st_do_scsi(NULL
, STp
, scmd
, 0, DMA_NONE
,
3162 timeout
, MAX_READY_RETRIES
, 1);
3164 return (STp
->buffer
)->syscall_result
;
3166 STps
->drv_block
= STps
->drv_file
= (-1);
3167 STps
->eof
= ST_NOEOF
;
3168 if ((STp
->buffer
)->syscall_result
!= 0) {
3170 if (STp
->can_partitions
&&
3171 (STp
->device
)->scsi_level
>= SCSI_2
&&
3172 (p
= find_partition(STp
)) >= 0)
3175 if (STp
->can_partitions
) {
3176 STp
->partition
= partition
;
3177 STps
= &(STp
->ps
[partition
]);
3178 if (!STps
->last_block_valid
||
3179 STps
->last_block_visited
!= block
) {
3186 STps
->drv_block
= STps
->drv_file
= 0;
3190 st_release_request(SRpnt
);
3197 /* Find the current partition number for the drive status. Called from open and
3198 returns either partition number of negative error code. */
3199 static int find_partition(struct scsi_tape
*STp
)
3204 if ((i
= get_location(STp
, &block
, &partition
, 1)) < 0)
3206 if (partition
>= ST_NBR_PARTITIONS
)
3212 /* Change the partition if necessary */
3213 static int switch_partition(struct scsi_tape
*STp
)
3215 struct st_partstat
*STps
;
3217 if (STp
->partition
== STp
->new_partition
)
3219 STps
= &(STp
->ps
[STp
->new_partition
]);
3220 if (!STps
->last_block_valid
)
3221 STps
->last_block_visited
= 0;
3222 return set_location(STp
, STps
->last_block_visited
, STp
->new_partition
, 1);
3225 /* Functions for reading and writing the medium partition mode page. */
3227 #define PART_PAGE 0x11
3228 #define PART_PAGE_FIXED_LENGTH 8
3230 #define PP_OFF_MAX_ADD_PARTS 2
3231 #define PP_OFF_NBR_ADD_PARTS 3
3232 #define PP_OFF_FLAGS 4
3233 #define PP_OFF_PART_UNITS 6
3234 #define PP_OFF_RESERVED 7
3236 #define PP_BIT_IDP 0x20
3237 #define PP_MSK_PSUM_MB 0x10
3239 /* Get the number of partitions on the tape. As a side effect reads the
3240 mode page into the tape buffer. */
3241 static int nbr_partitions(struct scsi_tape
*STp
)
3245 if (STp
->ready
!= ST_READY
)
3248 result
= read_mode_page(STp
, PART_PAGE
, 1);
3251 DEBC_printk(STp
, "Can't read medium partition page.\n");
3254 result
= (STp
->buffer
)->b_data
[MODE_HEADER_LENGTH
+
3255 PP_OFF_NBR_ADD_PARTS
] + 1;
3256 DEBC_printk(STp
, "Number of partitions %d.\n", result
);
3263 /* Partition the tape into two partitions if size > 0 or one partition if
3266 The block descriptors are read and written because Sony SDT-7000 does not
3267 work without this (suggestion from Michael Schaefer <Michael.Schaefer@dlr.de>).
3269 My HP C1533A drive returns only one partition size field. This is used to
3270 set the size of partition 1. There is no size field for the default partition.
3271 Michael Schaefer's Sony SDT-7000 returns two descriptors and the second is
3272 used to set the size of partition 1 (this is what the SCSI-3 standard specifies).
3273 The following algorithm is used to accommodate both drives: if the number of
3274 partition size fields is greater than the maximum number of additional partitions
3275 in the mode page, the second field is used. Otherwise the first field is used.
3277 For Seagate DDS drives the page length must be 8 when no partitions is defined
3278 and 10 when 1 partition is defined (information from Eric Lee Green). This is
3279 is acceptable also to some other old drives and enforced if the first partition
3280 size field is used for the first additional partition size.
3282 static int partition_tape(struct scsi_tape
*STp
, int size
)
3285 int pgo
, psd_cnt
, psdo
;
3288 result
= read_mode_page(STp
, PART_PAGE
, 0);
3290 DEBC_printk(STp
, "Can't read partition mode page.\n");
3293 /* The mode page is in the buffer. Let's modify it and write it. */
3294 bp
= (STp
->buffer
)->b_data
;
3295 pgo
= MODE_HEADER_LENGTH
+ bp
[MH_OFF_BDESCS_LENGTH
];
3296 DEBC_printk(STp
, "Partition page length is %d bytes.\n",
3297 bp
[pgo
+ MP_OFF_PAGE_LENGTH
] + 2);
3299 psd_cnt
= (bp
[pgo
+ MP_OFF_PAGE_LENGTH
] + 2 - PART_PAGE_FIXED_LENGTH
) / 2;
3300 psdo
= pgo
+ PART_PAGE_FIXED_LENGTH
;
3301 if (psd_cnt
> bp
[pgo
+ PP_OFF_MAX_ADD_PARTS
]) {
3302 bp
[psdo
] = bp
[psdo
+ 1] = 0xff; /* Rest of the tape */
3305 memset(bp
+ psdo
, 0, bp
[pgo
+ PP_OFF_NBR_ADD_PARTS
] * 2);
3307 DEBC_printk(STp
, "psd_cnt %d, max.parts %d, nbr_parts %d\n",
3308 psd_cnt
, bp
[pgo
+ PP_OFF_MAX_ADD_PARTS
],
3309 bp
[pgo
+ PP_OFF_NBR_ADD_PARTS
]);
3312 bp
[pgo
+ PP_OFF_NBR_ADD_PARTS
] = 0;
3313 if (psd_cnt
<= bp
[pgo
+ PP_OFF_MAX_ADD_PARTS
])
3314 bp
[pgo
+ MP_OFF_PAGE_LENGTH
] = 6;
3315 DEBC_printk(STp
, "Formatting tape with one partition.\n");
3317 bp
[psdo
] = (size
>> 8) & 0xff;
3318 bp
[psdo
+ 1] = size
& 0xff;
3320 if (bp
[pgo
+ MP_OFF_PAGE_LENGTH
] < 8)
3321 bp
[pgo
+ MP_OFF_PAGE_LENGTH
] = 8;
3322 DEBC_printk(STp
, "Formatting tape with two partitions "
3323 "(1 = %d MB).\n", size
);
3325 bp
[pgo
+ PP_OFF_PART_UNITS
] = 0;
3326 bp
[pgo
+ PP_OFF_RESERVED
] = 0;
3327 bp
[pgo
+ PP_OFF_FLAGS
] = PP_BIT_IDP
| PP_MSK_PSUM_MB
;
3329 result
= write_mode_page(STp
, PART_PAGE
, 1);
3331 st_printk(KERN_INFO
, STp
, "Partitioning of tape failed.\n");
3340 /* The ioctl command */
3341 static long st_ioctl(struct file
*file
, unsigned int cmd_in
, unsigned long arg
)
3343 int i
, cmd_nr
, cmd_type
, bt
;
3346 struct scsi_tape
*STp
= file
->private_data
;
3347 struct st_modedef
*STm
;
3348 struct st_partstat
*STps
;
3349 void __user
*p
= (void __user
*)arg
;
3351 if (mutex_lock_interruptible(&STp
->lock
))
3352 return -ERESTARTSYS
;
3355 if (debugging
&& !STp
->in_use
) {
3356 st_printk(ST_DEB_MSG
, STp
, "Incorrect device.\n");
3361 STm
= &(STp
->modes
[STp
->current_mode
]);
3362 STps
= &(STp
->ps
[STp
->partition
]);
3365 * If we are in the middle of error recovery, don't let anyone
3366 * else try and use this device. Also, if error recovery fails, it
3367 * may try and take the device offline, in which case all further
3368 * access to the device is prohibited.
3370 retval
= scsi_nonblockable_ioctl(STp
->device
, cmd_in
, p
,
3371 file
->f_flags
& O_NDELAY
);
3372 if (!scsi_block_when_processing_errors(STp
->device
) || retval
!= -ENODEV
)
3376 cmd_type
= _IOC_TYPE(cmd_in
);
3377 cmd_nr
= _IOC_NR(cmd_in
);
3379 if (cmd_type
== _IOC_TYPE(MTIOCTOP
) && cmd_nr
== _IOC_NR(MTIOCTOP
)) {
3382 if (_IOC_SIZE(cmd_in
) != sizeof(mtc
)) {
3387 i
= copy_from_user(&mtc
, p
, sizeof(struct mtop
));
3393 if (mtc
.mt_op
== MTSETDRVBUFFER
&& !capable(CAP_SYS_ADMIN
)) {
3394 st_printk(KERN_WARNING
, STp
,
3395 "MTSETDRVBUFFER only allowed for root.\n");
3399 if (!STm
->defined
&&
3400 (mtc
.mt_op
!= MTSETDRVBUFFER
&&
3401 (mtc
.mt_count
& MT_ST_OPTIONS
) == 0)) {
3406 if (!STp
->pos_unknown
) {
3408 if (STps
->eof
== ST_FM_HIT
) {
3409 if (mtc
.mt_op
== MTFSF
|| mtc
.mt_op
== MTFSFM
||
3410 mtc
.mt_op
== MTEOM
) {
3412 if (STps
->drv_file
>= 0)
3413 STps
->drv_file
+= 1;
3414 } else if (mtc
.mt_op
== MTBSF
|| mtc
.mt_op
== MTBSFM
) {
3416 if (STps
->drv_file
>= 0)
3417 STps
->drv_file
+= 1;
3421 if (mtc
.mt_op
== MTSEEK
) {
3422 /* Old position must be restored if partition will be
3424 i
= !STp
->can_partitions
||
3425 (STp
->new_partition
!= STp
->partition
);
3427 i
= mtc
.mt_op
== MTREW
|| mtc
.mt_op
== MTOFFL
||
3428 mtc
.mt_op
== MTRETEN
|| mtc
.mt_op
== MTEOM
||
3429 mtc
.mt_op
== MTLOCK
|| mtc
.mt_op
== MTLOAD
||
3430 mtc
.mt_op
== MTFSF
|| mtc
.mt_op
== MTFSFM
||
3431 mtc
.mt_op
== MTBSF
|| mtc
.mt_op
== MTBSFM
||
3432 mtc
.mt_op
== MTCOMPRESSION
;
3434 i
= flush_buffer(STp
, i
);
3439 if (STps
->rw
== ST_WRITING
&&
3440 (mtc
.mt_op
== MTREW
|| mtc
.mt_op
== MTOFFL
||
3441 mtc
.mt_op
== MTSEEK
||
3442 mtc
.mt_op
== MTBSF
|| mtc
.mt_op
== MTBSFM
)) {
3443 i
= st_int_ioctl(STp
, MTWEOF
, 1);
3448 if (mtc
.mt_op
== MTBSF
|| mtc
.mt_op
== MTBSFM
)
3455 * If there was a bus reset, block further access
3456 * to this device. If the user wants to rewind the tape,
3457 * then reset the flag and allow access again.
3459 if (mtc
.mt_op
!= MTREW
&&
3460 mtc
.mt_op
!= MTOFFL
&&
3461 mtc
.mt_op
!= MTRETEN
&&
3462 mtc
.mt_op
!= MTERASE
&&
3463 mtc
.mt_op
!= MTSEEK
&&
3464 mtc
.mt_op
!= MTEOM
) {
3469 /* remove this when the midlevel properly clears was_reset */
3470 STp
->device
->was_reset
= 0;
3473 if (mtc
.mt_op
!= MTNOP
&& mtc
.mt_op
!= MTSETBLK
&&
3474 mtc
.mt_op
!= MTSETDENSITY
&& mtc
.mt_op
!= MTWSM
&&
3475 mtc
.mt_op
!= MTSETDRVBUFFER
&& mtc
.mt_op
!= MTSETPART
)
3476 STps
->rw
= ST_IDLE
; /* Prevent automatic WEOF and fsf */
3478 if (mtc
.mt_op
== MTOFFL
&& STp
->door_locked
!= ST_UNLOCKED
)
3479 do_door_lock(STp
, 0); /* Ignore result! */
3481 if (mtc
.mt_op
== MTSETDRVBUFFER
&&
3482 (mtc
.mt_count
& MT_ST_OPTIONS
) != 0) {
3483 retval
= st_set_options(STp
, mtc
.mt_count
);
3487 if (mtc
.mt_op
== MTSETPART
) {
3488 if (!STp
->can_partitions
||
3489 mtc
.mt_count
< 0 || mtc
.mt_count
>= ST_NBR_PARTITIONS
) {
3493 if (mtc
.mt_count
>= STp
->nbr_partitions
&&
3494 (STp
->nbr_partitions
= nbr_partitions(STp
)) < 0) {
3498 if (mtc
.mt_count
>= STp
->nbr_partitions
) {
3502 STp
->new_partition
= mtc
.mt_count
;
3507 if (mtc
.mt_op
== MTMKPART
) {
3508 if (!STp
->can_partitions
) {
3512 if ((i
= st_int_ioctl(STp
, MTREW
, 0)) < 0 ||
3513 (i
= partition_tape(STp
, mtc
.mt_count
)) < 0) {
3517 for (i
= 0; i
< ST_NBR_PARTITIONS
; i
++) {
3518 STp
->ps
[i
].rw
= ST_IDLE
;
3519 STp
->ps
[i
].at_sm
= 0;
3520 STp
->ps
[i
].last_block_valid
= 0;
3522 STp
->partition
= STp
->new_partition
= 0;
3523 STp
->nbr_partitions
= 1; /* Bad guess ?-) */
3524 STps
->drv_block
= STps
->drv_file
= 0;
3529 if (mtc
.mt_op
== MTSEEK
) {
3530 i
= set_location(STp
, mtc
.mt_count
, STp
->new_partition
, 0);
3531 if (!STp
->can_partitions
)
3532 STp
->ps
[0].rw
= ST_IDLE
;
3537 if (mtc
.mt_op
== MTUNLOAD
|| mtc
.mt_op
== MTOFFL
) {
3538 retval
= do_load_unload(STp
, file
, 0);
3542 if (mtc
.mt_op
== MTLOAD
) {
3543 retval
= do_load_unload(STp
, file
, max(1, mtc
.mt_count
));
3547 if (mtc
.mt_op
== MTLOCK
|| mtc
.mt_op
== MTUNLOCK
) {
3548 retval
= do_door_lock(STp
, (mtc
.mt_op
== MTLOCK
));
3552 if (STp
->can_partitions
&& STp
->ready
== ST_READY
&&
3553 (i
= switch_partition(STp
)) < 0) {
3558 if (mtc
.mt_op
== MTCOMPRESSION
)
3559 retval
= st_compression(STp
, (mtc
.mt_count
& 1));
3561 retval
= st_int_ioctl(STp
, mtc
.mt_op
, mtc
.mt_count
);
3564 if (!STm
->defined
) {
3569 if ((i
= flush_buffer(STp
, 0)) < 0) {
3573 if (STp
->can_partitions
&&
3574 (i
= switch_partition(STp
)) < 0) {
3579 if (cmd_type
== _IOC_TYPE(MTIOCGET
) && cmd_nr
== _IOC_NR(MTIOCGET
)) {
3580 struct mtget mt_status
;
3582 if (_IOC_SIZE(cmd_in
) != sizeof(struct mtget
)) {
3587 mt_status
.mt_type
= STp
->tape_type
;
3588 mt_status
.mt_dsreg
=
3589 ((STp
->block_size
<< MT_ST_BLKSIZE_SHIFT
) & MT_ST_BLKSIZE_MASK
) |
3590 ((STp
->density
<< MT_ST_DENSITY_SHIFT
) & MT_ST_DENSITY_MASK
);
3591 mt_status
.mt_blkno
= STps
->drv_block
;
3592 mt_status
.mt_fileno
= STps
->drv_file
;
3593 if (STp
->block_size
!= 0) {
3594 if (STps
->rw
== ST_WRITING
)
3595 mt_status
.mt_blkno
+=
3596 (STp
->buffer
)->buffer_bytes
/ STp
->block_size
;
3597 else if (STps
->rw
== ST_READING
)
3598 mt_status
.mt_blkno
-=
3599 ((STp
->buffer
)->buffer_bytes
+
3600 STp
->block_size
- 1) / STp
->block_size
;
3603 mt_status
.mt_gstat
= 0;
3604 if (STp
->drv_write_prot
)
3605 mt_status
.mt_gstat
|= GMT_WR_PROT(0xffffffff);
3606 if (mt_status
.mt_blkno
== 0) {
3607 if (mt_status
.mt_fileno
== 0)
3608 mt_status
.mt_gstat
|= GMT_BOT(0xffffffff);
3610 mt_status
.mt_gstat
|= GMT_EOF(0xffffffff);
3612 mt_status
.mt_erreg
= (STp
->recover_reg
<< MT_ST_SOFTERR_SHIFT
);
3613 mt_status
.mt_resid
= STp
->partition
;
3614 if (STps
->eof
== ST_EOM_OK
|| STps
->eof
== ST_EOM_ERROR
)
3615 mt_status
.mt_gstat
|= GMT_EOT(0xffffffff);
3616 else if (STps
->eof
>= ST_EOM_OK
)
3617 mt_status
.mt_gstat
|= GMT_EOD(0xffffffff);
3618 if (STp
->density
== 1)
3619 mt_status
.mt_gstat
|= GMT_D_800(0xffffffff);
3620 else if (STp
->density
== 2)
3621 mt_status
.mt_gstat
|= GMT_D_1600(0xffffffff);
3622 else if (STp
->density
== 3)
3623 mt_status
.mt_gstat
|= GMT_D_6250(0xffffffff);
3624 if (STp
->ready
== ST_READY
)
3625 mt_status
.mt_gstat
|= GMT_ONLINE(0xffffffff);
3626 if (STp
->ready
== ST_NO_TAPE
)
3627 mt_status
.mt_gstat
|= GMT_DR_OPEN(0xffffffff);
3629 mt_status
.mt_gstat
|= GMT_SM(0xffffffff);
3630 if (STm
->do_async_writes
||
3631 (STm
->do_buffer_writes
&& STp
->block_size
!= 0) ||
3632 STp
->drv_buffer
!= 0)
3633 mt_status
.mt_gstat
|= GMT_IM_REP_EN(0xffffffff);
3634 if (STp
->cleaning_req
)
3635 mt_status
.mt_gstat
|= GMT_CLN(0xffffffff);
3637 i
= copy_to_user(p
, &mt_status
, sizeof(struct mtget
));
3643 STp
->recover_reg
= 0; /* Clear after read */
3646 } /* End of MTIOCGET */
3647 if (cmd_type
== _IOC_TYPE(MTIOCPOS
) && cmd_nr
== _IOC_NR(MTIOCPOS
)) {
3648 struct mtpos mt_pos
;
3649 if (_IOC_SIZE(cmd_in
) != sizeof(struct mtpos
)) {
3653 if ((i
= get_location(STp
, &blk
, &bt
, 0)) < 0) {
3657 mt_pos
.mt_blkno
= blk
;
3658 i
= copy_to_user(p
, &mt_pos
, sizeof(struct mtpos
));
3663 mutex_unlock(&STp
->lock
);
3665 case SCSI_IOCTL_GET_IDLUN
:
3666 case SCSI_IOCTL_GET_BUS_NUMBER
:
3669 if ((cmd_in
== SG_IO
||
3670 cmd_in
== SCSI_IOCTL_SEND_COMMAND
||
3671 cmd_in
== CDROM_SEND_PACKET
) &&
3672 !capable(CAP_SYS_RAWIO
))
3675 i
= scsi_cmd_ioctl(STp
->disk
->queue
, STp
->disk
,
3676 file
->f_mode
, cmd_in
, p
);
3681 retval
= scsi_ioctl(STp
->device
, cmd_in
, p
);
3682 if (!retval
&& cmd_in
== SCSI_IOCTL_STOP_UNIT
) { /* unload */
3683 STp
->rew_at_close
= 0;
3684 STp
->ready
= ST_NO_TAPE
;
3689 mutex_unlock(&STp
->lock
);
3693 #ifdef CONFIG_COMPAT
3694 static long st_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
3696 struct scsi_tape
*STp
= file
->private_data
;
3697 struct scsi_device
*sdev
= STp
->device
;
3698 int ret
= -ENOIOCTLCMD
;
3699 if (sdev
->host
->hostt
->compat_ioctl
) {
3701 ret
= sdev
->host
->hostt
->compat_ioctl(sdev
, cmd
, (void __user
*)arg
);
3710 /* Try to allocate a new tape buffer. Calling function must not hold
3712 static struct st_buffer
*new_tape_buffer(int need_dma
, int max_sg
)
3714 struct st_buffer
*tb
;
3716 tb
= kzalloc(sizeof(struct st_buffer
), GFP_ATOMIC
);
3718 printk(KERN_NOTICE
"st: Can't allocate new tape buffer.\n");
3722 tb
->use_sg
= max_sg
;
3724 tb
->buffer_size
= 0;
3726 tb
->reserved_pages
= kzalloc(max_sg
* sizeof(struct page
*),
3728 if (!tb
->reserved_pages
) {
3737 /* Try to allocate enough space in the tape buffer */
3738 #define ST_MAX_ORDER 6
3740 static int enlarge_buffer(struct st_buffer
* STbuffer
, int new_size
, int need_dma
)
3742 int segs
, max_segs
, b_size
, order
, got
;
3745 if (new_size
<= STbuffer
->buffer_size
)
3748 if (STbuffer
->buffer_size
<= PAGE_SIZE
)
3749 normalize_buffer(STbuffer
); /* Avoid extra segment */
3751 max_segs
= STbuffer
->use_sg
;
3753 priority
= GFP_KERNEL
| __GFP_NOWARN
;
3755 priority
|= GFP_DMA
;
3757 if (STbuffer
->cleared
)
3758 priority
|= __GFP_ZERO
;
3760 if (STbuffer
->frp_segs
) {
3761 order
= STbuffer
->reserved_page_order
;
3762 b_size
= PAGE_SIZE
<< order
;
3764 for (b_size
= PAGE_SIZE
, order
= 0;
3765 order
< ST_MAX_ORDER
&&
3766 max_segs
* (PAGE_SIZE
<< order
) < new_size
;
3767 order
++, b_size
*= 2)
3769 STbuffer
->reserved_page_order
= order
;
3771 if (max_segs
* (PAGE_SIZE
<< order
) < new_size
) {
3772 if (order
== ST_MAX_ORDER
)
3774 normalize_buffer(STbuffer
);
3775 return enlarge_buffer(STbuffer
, new_size
, need_dma
);
3778 for (segs
= STbuffer
->frp_segs
, got
= STbuffer
->buffer_size
;
3779 segs
< max_segs
&& got
< new_size
;) {
3782 page
= alloc_pages(priority
, order
);
3784 DEB(STbuffer
->buffer_size
= got
);
3785 normalize_buffer(STbuffer
);
3789 STbuffer
->frp_segs
+= 1;
3791 STbuffer
->buffer_size
= got
;
3792 STbuffer
->reserved_pages
[segs
] = page
;
3795 STbuffer
->b_data
= page_address(STbuffer
->reserved_pages
[0]);
3801 /* Make sure that no data from previous user is in the internal buffer */
3802 static void clear_buffer(struct st_buffer
* st_bp
)
3806 for (i
=0; i
< st_bp
->frp_segs
; i
++)
3807 memset(page_address(st_bp
->reserved_pages
[i
]), 0,
3808 PAGE_SIZE
<< st_bp
->reserved_page_order
);
3813 /* Release the extra buffer */
3814 static void normalize_buffer(struct st_buffer
* STbuffer
)
3816 int i
, order
= STbuffer
->reserved_page_order
;
3818 for (i
= 0; i
< STbuffer
->frp_segs
; i
++) {
3819 __free_pages(STbuffer
->reserved_pages
[i
], order
);
3820 STbuffer
->buffer_size
-= (PAGE_SIZE
<< order
);
3822 STbuffer
->frp_segs
= 0;
3823 STbuffer
->sg_segs
= 0;
3824 STbuffer
->reserved_page_order
= 0;
3825 STbuffer
->map_data
.offset
= 0;
3829 /* Move data from the user buffer to the tape buffer. Returns zero (success) or
3830 negative error code. */
3831 static int append_to_buffer(const char __user
*ubp
, struct st_buffer
* st_bp
, int do_count
)
3833 int i
, cnt
, res
, offset
;
3834 int length
= PAGE_SIZE
<< st_bp
->reserved_page_order
;
3836 for (i
= 0, offset
= st_bp
->buffer_bytes
;
3837 i
< st_bp
->frp_segs
&& offset
>= length
; i
++)
3839 if (i
== st_bp
->frp_segs
) { /* Should never happen */
3840 printk(KERN_WARNING
"st: append_to_buffer offset overflow.\n");
3843 for (; i
< st_bp
->frp_segs
&& do_count
> 0; i
++) {
3844 struct page
*page
= st_bp
->reserved_pages
[i
];
3845 cnt
= length
- offset
< do_count
? length
- offset
: do_count
;
3846 res
= copy_from_user(page_address(page
) + offset
, ubp
, cnt
);
3850 st_bp
->buffer_bytes
+= cnt
;
3854 if (do_count
) /* Should never happen */
3861 /* Move data from the tape buffer to the user buffer. Returns zero (success) or
3862 negative error code. */
3863 static int from_buffer(struct st_buffer
* st_bp
, char __user
*ubp
, int do_count
)
3865 int i
, cnt
, res
, offset
;
3866 int length
= PAGE_SIZE
<< st_bp
->reserved_page_order
;
3868 for (i
= 0, offset
= st_bp
->read_pointer
;
3869 i
< st_bp
->frp_segs
&& offset
>= length
; i
++)
3871 if (i
== st_bp
->frp_segs
) { /* Should never happen */
3872 printk(KERN_WARNING
"st: from_buffer offset overflow.\n");
3875 for (; i
< st_bp
->frp_segs
&& do_count
> 0; i
++) {
3876 struct page
*page
= st_bp
->reserved_pages
[i
];
3877 cnt
= length
- offset
< do_count
? length
- offset
: do_count
;
3878 res
= copy_to_user(ubp
, page_address(page
) + offset
, cnt
);
3882 st_bp
->buffer_bytes
-= cnt
;
3883 st_bp
->read_pointer
+= cnt
;
3887 if (do_count
) /* Should never happen */
3894 /* Move data towards start of buffer */
3895 static void move_buffer_data(struct st_buffer
* st_bp
, int offset
)
3897 int src_seg
, dst_seg
, src_offset
= 0, dst_offset
;
3899 int length
= PAGE_SIZE
<< st_bp
->reserved_page_order
;
3904 total
=st_bp
->buffer_bytes
- offset
;
3905 for (src_seg
=0; src_seg
< st_bp
->frp_segs
; src_seg
++) {
3906 src_offset
= offset
;
3907 if (src_offset
< length
)
3912 st_bp
->buffer_bytes
= st_bp
->read_pointer
= total
;
3913 for (dst_seg
=dst_offset
=0; total
> 0; ) {
3914 struct page
*dpage
= st_bp
->reserved_pages
[dst_seg
];
3915 struct page
*spage
= st_bp
->reserved_pages
[src_seg
];
3917 count
= min(length
- dst_offset
, length
- src_offset
);
3918 memmove(page_address(dpage
) + dst_offset
,
3919 page_address(spage
) + src_offset
, count
);
3920 src_offset
+= count
;
3921 if (src_offset
>= length
) {
3925 dst_offset
+= count
;
3926 if (dst_offset
>= length
) {
3934 /* Validate the options from command line or module parameters */
3935 static void validate_options(void)
3938 st_fixed_buffer_size
= buffer_kbs
* ST_KILOBYTE
;
3939 if (max_sg_segs
>= ST_FIRST_SG
)
3940 st_max_sg_segs
= max_sg_segs
;
3944 /* Set the boot options. Syntax is defined in Documenation/scsi/st.txt.
3946 static int __init
st_setup(char *str
)
3948 int i
, len
, ints
[5];
3951 stp
= get_options(str
, ARRAY_SIZE(ints
), ints
);
3954 for (i
= 0; i
< ints
[0] && i
< ARRAY_SIZE(parms
); i
++)
3956 *parms
[i
].val
= ints
[i
+ 1];
3958 while (stp
!= NULL
) {
3959 for (i
= 0; i
< ARRAY_SIZE(parms
); i
++) {
3960 len
= strlen(parms
[i
].name
);
3961 if (!strncmp(stp
, parms
[i
].name
, len
) &&
3962 (*(stp
+ len
) == ':' || *(stp
+ len
) == '=')) {
3965 simple_strtoul(stp
+ len
+ 1, NULL
, 0);
3967 printk(KERN_WARNING
"st: Obsolete parameter %s\n",
3972 if (i
>= ARRAY_SIZE(parms
))
3973 printk(KERN_WARNING
"st: invalid parameter in '%s'\n",
3975 stp
= strchr(stp
, ',');
3986 __setup("st=", st_setup
);
3990 static const struct file_operations st_fops
=
3992 .owner
= THIS_MODULE
,
3995 .unlocked_ioctl
= st_ioctl
,
3996 #ifdef CONFIG_COMPAT
3997 .compat_ioctl
= st_compat_ioctl
,
4001 .release
= st_release
,
4002 .llseek
= noop_llseek
,
4005 static int create_one_cdev(struct scsi_tape
*tape
, int mode
, int rew
)
4011 struct st_modedef
*STm
= &(tape
->modes
[mode
]);
4013 int dev_num
= tape
->index
;
4015 cdev_devno
= MKDEV(SCSI_TAPE_MAJOR
, TAPE_MINOR(dev_num
, mode
, rew
));
4017 cdev
= cdev_alloc();
4019 pr_err("st%d: out of memory. Device not attached.\n", dev_num
);
4023 cdev
->owner
= THIS_MODULE
;
4024 cdev
->ops
= &st_fops
;
4026 error
= cdev_add(cdev
, cdev_devno
, 1);
4028 pr_err("st%d: Can't add %s-rewind mode %d\n", dev_num
,
4029 rew
? "non" : "auto", mode
);
4030 pr_err("st%d: Device not attached.\n", dev_num
);
4033 STm
->cdevs
[rew
] = cdev
;
4035 i
= mode
<< (4 - ST_NBR_MODE_BITS
);
4036 snprintf(name
, 10, "%s%s%s", rew
? "n" : "",
4037 tape
->disk
->disk_name
, st_formats
[i
]);
4039 dev
= device_create(&st_sysfs_class
, &tape
->device
->sdev_gendev
,
4040 cdev_devno
, &tape
->modes
[mode
], "%s", name
);
4042 pr_err("st%d: device_create failed\n", dev_num
);
4043 error
= PTR_ERR(dev
);
4047 STm
->devs
[rew
] = dev
;
4051 cdev_del(STm
->cdevs
[rew
]);
4052 STm
->cdevs
[rew
] = NULL
;
4057 static int create_cdevs(struct scsi_tape
*tape
)
4060 for (mode
= 0; mode
< ST_NBR_MODES
; ++mode
) {
4061 error
= create_one_cdev(tape
, mode
, 0);
4064 error
= create_one_cdev(tape
, mode
, 1);
4069 return sysfs_create_link(&tape
->device
->sdev_gendev
.kobj
,
4070 &tape
->modes
[0].devs
[0]->kobj
, "tape");
4073 static void remove_cdevs(struct scsi_tape
*tape
)
4076 sysfs_remove_link(&tape
->device
->sdev_gendev
.kobj
, "tape");
4077 for (mode
= 0; mode
< ST_NBR_MODES
; mode
++) {
4078 struct st_modedef
*STm
= &(tape
->modes
[mode
]);
4079 for (rew
= 0; rew
< 2; rew
++) {
4080 if (STm
->cdevs
[rew
])
4081 cdev_del(STm
->cdevs
[rew
]);
4083 device_unregister(STm
->devs
[rew
]);
4088 static int st_probe(struct device
*dev
)
4090 struct scsi_device
*SDp
= to_scsi_device(dev
);
4091 struct gendisk
*disk
= NULL
;
4092 struct scsi_tape
*tpnt
= NULL
;
4093 struct st_modedef
*STm
;
4094 struct st_partstat
*STps
;
4095 struct st_buffer
*buffer
;
4099 if (SDp
->type
!= TYPE_TAPE
)
4101 if ((stp
= st_incompatible(SDp
))) {
4102 sdev_printk(KERN_INFO
, SDp
, "Found incompatible tape\n");
4103 sdev_printk(KERN_INFO
, SDp
,
4104 "st: The suggested driver is %s.\n", stp
);
4108 i
= queue_max_segments(SDp
->request_queue
);
4109 if (st_max_sg_segs
< i
)
4111 buffer
= new_tape_buffer((SDp
->host
)->unchecked_isa_dma
, i
);
4112 if (buffer
== NULL
) {
4113 sdev_printk(KERN_ERR
, SDp
,
4114 "st: Can't allocate new tape buffer. "
4115 "Device not attached.\n");
4119 disk
= alloc_disk(1);
4121 sdev_printk(KERN_ERR
, SDp
,
4122 "st: out of memory. Device not attached.\n");
4123 goto out_buffer_free
;
4126 tpnt
= kzalloc(sizeof(struct scsi_tape
), GFP_ATOMIC
);
4128 sdev_printk(KERN_ERR
, SDp
,
4129 "st: Can't allocate device descriptor.\n");
4132 kref_init(&tpnt
->kref
);
4134 disk
->private_data
= &tpnt
->driver
;
4135 disk
->queue
= SDp
->request_queue
;
4136 /* SCSI tape doesn't register this gendisk via add_disk(). Manually
4137 * take queue reference that release_disk() expects. */
4138 if (!blk_get_queue(disk
->queue
))
4140 tpnt
->driver
= &st_template
;
4143 if (SDp
->scsi_level
<= 2)
4144 tpnt
->tape_type
= MT_ISSCSI1
;
4146 tpnt
->tape_type
= MT_ISSCSI2
;
4148 tpnt
->buffer
= buffer
;
4149 tpnt
->buffer
->last_SRpnt
= NULL
;
4154 tpnt
->drv_buffer
= 1; /* Try buffering if no mode sense */
4155 tpnt
->restr_dma
= (SDp
->host
)->unchecked_isa_dma
;
4156 tpnt
->use_pf
= (SDp
->scsi_level
>= SCSI_2
);
4158 tpnt
->do_auto_lock
= ST_AUTO_LOCK
;
4159 tpnt
->can_bsr
= (SDp
->scsi_level
> 2 ? 1 : ST_IN_FILE_POS
); /* BSR mandatory in SCSI3 */
4160 tpnt
->can_partitions
= 0;
4161 tpnt
->two_fm
= ST_TWO_FM
;
4162 tpnt
->fast_mteom
= ST_FAST_MTEOM
;
4163 tpnt
->scsi2_logical
= ST_SCSI2LOGICAL
;
4164 tpnt
->sili
= ST_SILI
;
4165 tpnt
->immediate
= ST_NOWAIT
;
4166 tpnt
->immediate_filemark
= 0;
4167 tpnt
->default_drvbuffer
= 0xff; /* No forced buffering */
4168 tpnt
->partition
= 0;
4169 tpnt
->new_partition
= 0;
4170 tpnt
->nbr_partitions
= 0;
4171 blk_queue_rq_timeout(tpnt
->device
->request_queue
, ST_TIMEOUT
);
4172 tpnt
->long_timeout
= ST_LONG_TIMEOUT
;
4173 tpnt
->try_dio
= try_direct_io
&& !SDp
->host
->unchecked_isa_dma
;
4175 for (i
= 0; i
< ST_NBR_MODES
; i
++) {
4176 STm
= &(tpnt
->modes
[i
]);
4178 STm
->sysv
= ST_SYSV
;
4179 STm
->defaults_for_writes
= 0;
4180 STm
->do_async_writes
= ST_ASYNC_WRITES
;
4181 STm
->do_buffer_writes
= ST_BUFFER_WRITES
;
4182 STm
->do_read_ahead
= ST_READ_AHEAD
;
4183 STm
->default_compression
= ST_DONT_TOUCH
;
4184 STm
->default_blksize
= (-1); /* No forced size */
4185 STm
->default_density
= (-1); /* No forced density */
4189 for (i
= 0; i
< ST_NBR_PARTITIONS
; i
++) {
4190 STps
= &(tpnt
->ps
[i
]);
4192 STps
->eof
= ST_NOEOF
;
4194 STps
->last_block_valid
= 0;
4195 STps
->drv_block
= (-1);
4196 STps
->drv_file
= (-1);
4199 tpnt
->current_mode
= 0;
4200 tpnt
->modes
[0].defined
= 1;
4202 tpnt
->density_changed
= tpnt
->compression_changed
=
4203 tpnt
->blksize_changed
= 0;
4204 mutex_init(&tpnt
->lock
);
4206 idr_preload(GFP_KERNEL
);
4207 spin_lock(&st_index_lock
);
4208 error
= idr_alloc(&st_index_idr
, tpnt
, 0, ST_MAX_TAPES
+ 1, GFP_NOWAIT
);
4209 spin_unlock(&st_index_lock
);
4212 pr_warn("st: idr allocation failed: %d\n", error
);
4215 tpnt
->index
= error
;
4216 sprintf(disk
->disk_name
, "st%d", tpnt
->index
);
4218 dev_set_drvdata(dev
, tpnt
);
4221 error
= create_cdevs(tpnt
);
4223 goto out_remove_devs
;
4224 scsi_autopm_put_device(SDp
);
4226 sdev_printk(KERN_NOTICE
, SDp
,
4227 "Attached scsi tape %s\n", tape_name(tpnt
));
4228 sdev_printk(KERN_INFO
, SDp
, "%s: try direct i/o: %s (alignment %d B)\n",
4229 tape_name(tpnt
), tpnt
->try_dio
? "yes" : "no",
4230 queue_dma_alignment(SDp
->request_queue
) + 1);
4236 spin_lock(&st_index_lock
);
4237 idr_remove(&st_index_idr
, tpnt
->index
);
4238 spin_unlock(&st_index_lock
);
4240 blk_put_queue(disk
->queue
);
4251 static int st_remove(struct device
*dev
)
4253 struct scsi_tape
*tpnt
= dev_get_drvdata(dev
);
4254 int index
= tpnt
->index
;
4256 scsi_autopm_get_device(to_scsi_device(dev
));
4259 mutex_lock(&st_ref_mutex
);
4260 kref_put(&tpnt
->kref
, scsi_tape_release
);
4261 mutex_unlock(&st_ref_mutex
);
4262 spin_lock(&st_index_lock
);
4263 idr_remove(&st_index_idr
, index
);
4264 spin_unlock(&st_index_lock
);
4269 * scsi_tape_release - Called to free the Scsi_Tape structure
4270 * @kref: pointer to embedded kref
4272 * st_ref_mutex must be held entering this routine. Because it is
4273 * called on last put, you should always use the scsi_tape_get()
4274 * scsi_tape_put() helpers which manipulate the semaphore directly
4275 * and never do a direct kref_put().
4277 static void scsi_tape_release(struct kref
*kref
)
4279 struct scsi_tape
*tpnt
= to_scsi_tape(kref
);
4280 struct gendisk
*disk
= tpnt
->disk
;
4282 tpnt
->device
= NULL
;
4285 normalize_buffer(tpnt
->buffer
);
4286 kfree(tpnt
->buffer
->reserved_pages
);
4287 kfree(tpnt
->buffer
);
4290 disk
->private_data
= NULL
;
4296 static struct class st_sysfs_class
= {
4297 .name
= "scsi_tape",
4298 .dev_groups
= st_dev_groups
,
4301 static int __init
init_st(void)
4307 printk(KERN_INFO
"st: Version %s, fixed bufsize %d, s/g segs %d\n",
4308 verstr
, st_fixed_buffer_size
, st_max_sg_segs
);
4310 err
= class_register(&st_sysfs_class
);
4312 pr_err("Unable register sysfs class for SCSI tapes\n");
4316 err
= register_chrdev_region(MKDEV(SCSI_TAPE_MAJOR
, 0),
4317 ST_MAX_TAPE_ENTRIES
, "st");
4319 printk(KERN_ERR
"Unable to get major %d for SCSI tapes\n",
4324 err
= scsi_register_driver(&st_template
.gendrv
);
4328 err
= do_create_sysfs_files();
4335 scsi_unregister_driver(&st_template
.gendrv
);
4337 unregister_chrdev_region(MKDEV(SCSI_TAPE_MAJOR
, 0),
4338 ST_MAX_TAPE_ENTRIES
);
4340 class_unregister(&st_sysfs_class
);
4344 static void __exit
exit_st(void)
4346 do_remove_sysfs_files();
4347 scsi_unregister_driver(&st_template
.gendrv
);
4348 unregister_chrdev_region(MKDEV(SCSI_TAPE_MAJOR
, 0),
4349 ST_MAX_TAPE_ENTRIES
);
4350 class_unregister(&st_sysfs_class
);
4351 printk(KERN_INFO
"st: Unloaded.\n");
4354 module_init(init_st
);
4355 module_exit(exit_st
);
4358 /* The sysfs driver interface. Read-only at the moment */
4359 static ssize_t
st_try_direct_io_show(struct device_driver
*ddp
, char *buf
)
4361 return snprintf(buf
, PAGE_SIZE
, "%d\n", try_direct_io
);
4363 static DRIVER_ATTR(try_direct_io
, S_IRUGO
, st_try_direct_io_show
, NULL
);
4365 static ssize_t
st_fixed_buffer_size_show(struct device_driver
*ddp
, char *buf
)
4367 return snprintf(buf
, PAGE_SIZE
, "%d\n", st_fixed_buffer_size
);
4369 static DRIVER_ATTR(fixed_buffer_size
, S_IRUGO
, st_fixed_buffer_size_show
, NULL
);
4371 static ssize_t
st_max_sg_segs_show(struct device_driver
*ddp
, char *buf
)
4373 return snprintf(buf
, PAGE_SIZE
, "%d\n", st_max_sg_segs
);
4375 static DRIVER_ATTR(max_sg_segs
, S_IRUGO
, st_max_sg_segs_show
, NULL
);
4377 static ssize_t
st_version_show(struct device_driver
*ddd
, char *buf
)
4379 return snprintf(buf
, PAGE_SIZE
, "[%s]\n", verstr
);
4381 static DRIVER_ATTR(version
, S_IRUGO
, st_version_show
, NULL
);
4383 static int do_create_sysfs_files(void)
4385 struct device_driver
*sysfs
= &st_template
.gendrv
;
4388 err
= driver_create_file(sysfs
, &driver_attr_try_direct_io
);
4391 err
= driver_create_file(sysfs
, &driver_attr_fixed_buffer_size
);
4393 goto err_try_direct_io
;
4394 err
= driver_create_file(sysfs
, &driver_attr_max_sg_segs
);
4396 goto err_attr_fixed_buf
;
4397 err
= driver_create_file(sysfs
, &driver_attr_version
);
4399 goto err_attr_max_sg
;
4404 driver_remove_file(sysfs
, &driver_attr_max_sg_segs
);
4406 driver_remove_file(sysfs
, &driver_attr_fixed_buffer_size
);
4408 driver_remove_file(sysfs
, &driver_attr_try_direct_io
);
4412 static void do_remove_sysfs_files(void)
4414 struct device_driver
*sysfs
= &st_template
.gendrv
;
4416 driver_remove_file(sysfs
, &driver_attr_version
);
4417 driver_remove_file(sysfs
, &driver_attr_max_sg_segs
);
4418 driver_remove_file(sysfs
, &driver_attr_fixed_buffer_size
);
4419 driver_remove_file(sysfs
, &driver_attr_try_direct_io
);
4422 /* The sysfs simple class interface */
4424 defined_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
4426 struct st_modedef
*STm
= dev_get_drvdata(dev
);
4429 l
= snprintf(buf
, PAGE_SIZE
, "%d\n", STm
->defined
);
4432 static DEVICE_ATTR_RO(defined
);
4435 default_blksize_show(struct device
*dev
, struct device_attribute
*attr
,
4438 struct st_modedef
*STm
= dev_get_drvdata(dev
);
4441 l
= snprintf(buf
, PAGE_SIZE
, "%d\n", STm
->default_blksize
);
4444 static DEVICE_ATTR_RO(default_blksize
);
4447 default_density_show(struct device
*dev
, struct device_attribute
*attr
,
4450 struct st_modedef
*STm
= dev_get_drvdata(dev
);
4454 fmt
= STm
->default_density
>= 0 ? "0x%02x\n" : "%d\n";
4455 l
= snprintf(buf
, PAGE_SIZE
, fmt
, STm
->default_density
);
4458 static DEVICE_ATTR_RO(default_density
);
4461 default_compression_show(struct device
*dev
, struct device_attribute
*attr
,
4464 struct st_modedef
*STm
= dev_get_drvdata(dev
);
4467 l
= snprintf(buf
, PAGE_SIZE
, "%d\n", STm
->default_compression
- 1);
4470 static DEVICE_ATTR_RO(default_compression
);
4473 options_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
4475 struct st_modedef
*STm
= dev_get_drvdata(dev
);
4476 struct scsi_tape
*STp
= STm
->tape
;
4480 options
= STm
->do_buffer_writes
? MT_ST_BUFFER_WRITES
: 0;
4481 options
|= STm
->do_async_writes
? MT_ST_ASYNC_WRITES
: 0;
4482 options
|= STm
->do_read_ahead
? MT_ST_READ_AHEAD
: 0;
4483 DEB( options
|= debugging
? MT_ST_DEBUGGING
: 0 );
4484 options
|= STp
->two_fm
? MT_ST_TWO_FM
: 0;
4485 options
|= STp
->fast_mteom
? MT_ST_FAST_MTEOM
: 0;
4486 options
|= STm
->defaults_for_writes
? MT_ST_DEF_WRITES
: 0;
4487 options
|= STp
->can_bsr
? MT_ST_CAN_BSR
: 0;
4488 options
|= STp
->omit_blklims
? MT_ST_NO_BLKLIMS
: 0;
4489 options
|= STp
->can_partitions
? MT_ST_CAN_PARTITIONS
: 0;
4490 options
|= STp
->scsi2_logical
? MT_ST_SCSI2LOGICAL
: 0;
4491 options
|= STm
->sysv
? MT_ST_SYSV
: 0;
4492 options
|= STp
->immediate
? MT_ST_NOWAIT
: 0;
4493 options
|= STp
->immediate_filemark
? MT_ST_NOWAIT_EOF
: 0;
4494 options
|= STp
->sili
? MT_ST_SILI
: 0;
4496 l
= snprintf(buf
, PAGE_SIZE
, "0x%08x\n", options
);
4499 static DEVICE_ATTR_RO(options
);
4501 static struct attribute
*st_dev_attrs
[] = {
4502 &dev_attr_defined
.attr
,
4503 &dev_attr_default_blksize
.attr
,
4504 &dev_attr_default_density
.attr
,
4505 &dev_attr_default_compression
.attr
,
4506 &dev_attr_options
.attr
,
4509 ATTRIBUTE_GROUPS(st_dev
);
4511 /* The following functions may be useful for a larger audience. */
4512 static int sgl_map_user_pages(struct st_buffer
*STbp
,
4513 const unsigned int max_pages
, unsigned long uaddr
,
4514 size_t count
, int rw
)
4516 unsigned long end
= (uaddr
+ count
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
4517 unsigned long start
= uaddr
>> PAGE_SHIFT
;
4518 const int nr_pages
= end
- start
;
4520 struct page
**pages
;
4521 struct rq_map_data
*mdata
= &STbp
->map_data
;
4523 /* User attempted Overflow! */
4524 if ((uaddr
+ count
) < uaddr
)
4528 if (nr_pages
> max_pages
)
4535 if ((pages
= kmalloc(max_pages
* sizeof(*pages
), GFP_KERNEL
)) == NULL
)
4538 /* Try to fault in all of the necessary pages */
4539 down_read(¤t
->mm
->mmap_sem
);
4540 /* rw==READ means read from drive, write into memory area */
4541 res
= get_user_pages(
4547 0, /* don't force */
4550 up_read(¤t
->mm
->mmap_sem
);
4552 /* Errors and no page mapped should return here */
4556 for (i
=0; i
< nr_pages
; i
++) {
4557 /* FIXME: flush superflous for rw==READ,
4558 * probably wrong function for rw==WRITE
4560 flush_dcache_page(pages
[i
]);
4563 mdata
->offset
= uaddr
& ~PAGE_MASK
;
4564 STbp
->mapped_pages
= pages
;
4569 for (j
=0; j
< res
; j
++)
4570 page_cache_release(pages
[j
]);
4578 /* And unmap them... */
4579 static int sgl_unmap_user_pages(struct st_buffer
*STbp
,
4580 const unsigned int nr_pages
, int dirtied
)
4584 for (i
=0; i
< nr_pages
; i
++) {
4585 struct page
*page
= STbp
->mapped_pages
[i
];
4589 /* FIXME: cache flush missing for rw==READ
4590 * FIXME: call the correct reference counting function
4592 page_cache_release(page
);
4594 kfree(STbp
->mapped_pages
);
4595 STbp
->mapped_pages
= NULL
;