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 - 2008 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 = "20080224";
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/errno.h>
31 #include <linux/mtio.h>
32 #include <linux/cdrom.h>
33 #include <linux/ioctl.h>
34 #include <linux/fcntl.h>
35 #include <linux/spinlock.h>
36 #include <linux/blkdev.h>
37 #include <linux/moduleparam.h>
38 #include <linux/cdev.h>
39 #include <linux/delay.h>
40 #include <linux/mutex.h>
42 #include <asm/uaccess.h>
44 #include <asm/system.h>
46 #include <scsi/scsi.h>
47 #include <scsi/scsi_dbg.h>
48 #include <scsi/scsi_device.h>
49 #include <scsi/scsi_driver.h>
50 #include <scsi/scsi_eh.h>
51 #include <scsi/scsi_host.h>
52 #include <scsi/scsi_ioctl.h>
56 /* The driver prints some debugging information on the console if DEBUG
57 is defined and non-zero. */
61 /* The message level for the debug messages is currently set to KERN_NOTICE
62 so that people can easily see the messages. Later when the debugging messages
63 in the drivers are more widely classified, this may be changed to KERN_DEBUG. */
64 #define ST_DEB_MSG KERN_NOTICE
66 #define DEBC(a) if (debugging) { a ; }
72 #define ST_KILOBYTE 1024
74 #include "st_options.h"
77 static int buffer_kbs;
78 static int max_sg_segs;
79 static int try_direct_io = TRY_DIRECT_IO;
80 static int try_rdio = 1;
81 static int try_wdio = 1;
83 static int st_dev_max;
86 static struct class *st_sysfs_class;
88 MODULE_AUTHOR("Kai Makisara");
89 MODULE_DESCRIPTION("SCSI tape (st) driver");
90 MODULE_LICENSE("GPL");
91 MODULE_ALIAS_CHARDEV_MAJOR(SCSI_TAPE_MAJOR);
92 MODULE_ALIAS_SCSI_DEVICE(TYPE_TAPE);
94 /* Set 'perm' (4th argument) to 0 to disable module_param's definition
95 * of sysfs parameters (which module_param doesn't yet support).
96 * Sysfs parameters defined explicitly later.
98 module_param_named(buffer_kbs, buffer_kbs, int, 0);
99 MODULE_PARM_DESC(buffer_kbs, "Default driver buffer size for fixed block mode (KB; 32)");
100 module_param_named(max_sg_segs, max_sg_segs, int, 0);
101 MODULE_PARM_DESC(max_sg_segs, "Maximum number of scatter/gather segments to use (256)");
102 module_param_named(try_direct_io, try_direct_io, int, 0);
103 MODULE_PARM_DESC(try_direct_io, "Try direct I/O between user buffer and tape drive (1)");
105 /* Extra parameters for testing */
106 module_param_named(try_rdio, try_rdio, int, 0);
107 MODULE_PARM_DESC(try_rdio, "Try direct read i/o when possible");
108 module_param_named(try_wdio, try_wdio, int, 0);
109 MODULE_PARM_DESC(try_wdio, "Try direct write i/o when possible");
112 static int write_threshold_kbs; /* retained for compatibility */
113 static struct st_dev_parm {
116 } parms[] __initdata = {
118 "buffer_kbs", &buffer_kbs
120 { /* Retained for compatibility with 2.4 */
121 "write_threshold_kbs", &write_threshold_kbs
127 "try_direct_io", &try_direct_io
132 /* Restrict the number of modes so that names for all are assigned */
133 #if ST_NBR_MODES > 16
134 #error "Maximum number of modes is 16"
136 /* Bit reversed order to get same names for same minors with all
138 static const char *st_formats[] = {
139 "", "r", "k", "s", "l", "t", "o", "u",
140 "m", "v", "p", "x", "a", "y", "q", "z"};
142 /* The default definitions have been moved to st_options.h */
144 #define ST_FIXED_BUFFER_SIZE (ST_FIXED_BUFFER_BLOCKS * ST_KILOBYTE)
146 /* The buffer size should fit into the 24 bits for length in the
147 6-byte SCSI read and write commands. */
148 #if ST_FIXED_BUFFER_SIZE >= (2 << 24 - 1)
149 #error "Buffer size should not exceed (2 << 24 - 1) bytes!"
152 static int debugging = DEBUG;
154 #define MAX_RETRIES 0
155 #define MAX_WRITE_RETRIES 0
156 #define MAX_READY_RETRIES 0
157 #define NO_TAPE NOT_READY
159 #define ST_TIMEOUT (900 * HZ)
160 #define ST_LONG_TIMEOUT (14000 * HZ)
162 /* Remove mode bits and auto-rewind bit (7) */
163 #define TAPE_NR(x) ( ((iminor(x) & ~255) >> (ST_NBR_MODE_BITS + 1)) | \
164 (iminor(x) & ~(-1 << ST_MODE_SHIFT)) )
165 #define TAPE_MODE(x) ((iminor(x) & ST_MODE_MASK) >> ST_MODE_SHIFT)
167 /* Construct the minor number from the device (d), mode (m), and non-rewind (n) data */
168 #define TAPE_MINOR(d, m, n) (((d & ~(255 >> (ST_NBR_MODE_BITS + 1))) << (ST_NBR_MODE_BITS + 1)) | \
169 (d & (255 >> (ST_NBR_MODE_BITS + 1))) | (m << ST_MODE_SHIFT) | ((n != 0) << 7) )
171 /* Internal ioctl to set both density (uppermost 8 bits) and blocksize (lower
173 #define SET_DENS_AND_BLK 0x10001
175 static DEFINE_RWLOCK(st_dev_arr_lock);
177 static int st_fixed_buffer_size = ST_FIXED_BUFFER_SIZE;
178 static int st_max_sg_segs = ST_MAX_SG;
180 static struct scsi_tape **scsi_tapes = NULL;
182 static int modes_defined;
184 static struct st_buffer *new_tape_buffer(int, int, int);
185 static int enlarge_buffer(struct st_buffer *, int, int);
186 static void clear_buffer(struct st_buffer *);
187 static void normalize_buffer(struct st_buffer *);
188 static int append_to_buffer(const char __user *, struct st_buffer *, int);
189 static int from_buffer(struct st_buffer *, char __user *, int);
190 static void move_buffer_data(struct st_buffer *, int);
191 static void buf_to_sg(struct st_buffer *, unsigned int);
193 static int sgl_map_user_pages(struct scatterlist *, const unsigned int,
194 unsigned long, size_t, int);
195 static int sgl_unmap_user_pages(struct scatterlist *, const unsigned int, int);
197 static int st_probe(struct device *);
198 static int st_remove(struct device *);
200 static int do_create_sysfs_files(void);
201 static void do_remove_sysfs_files(void);
202 static int do_create_class_files(struct scsi_tape *, int, int);
204 static struct scsi_driver st_template = {
205 .owner = THIS_MODULE,
213 static int st_compression(struct scsi_tape *, int);
215 static int find_partition(struct scsi_tape *);
216 static int switch_partition(struct scsi_tape *);
218 static int st_int_ioctl(struct scsi_tape *, unsigned int, unsigned long);
220 static void scsi_tape_release(struct kref *);
222 #define to_scsi_tape(obj) container_of(obj, struct scsi_tape, kref)
224 static DEFINE_MUTEX(st_ref_mutex);
227 #include "osst_detect.h"
228 #ifndef SIGS_FROM_OSST
229 #define SIGS_FROM_OSST \
230 {"OnStream", "SC-", "", "osst"}, \
231 {"OnStream", "DI-", "", "osst"}, \
232 {"OnStream", "DP-", "", "osst"}, \
233 {"OnStream", "USB", "", "osst"}, \
234 {"OnStream", "FW-", "", "osst"}
237 static struct scsi_tape *scsi_tape_get(int dev)
239 struct scsi_tape *STp = NULL;
241 mutex_lock(&st_ref_mutex);
242 write_lock(&st_dev_arr_lock);
244 if (dev < st_dev_max && scsi_tapes != NULL)
245 STp = scsi_tapes[dev];
248 kref_get(&STp->kref);
253 if (scsi_device_get(STp->device))
259 kref_put(&STp->kref, scsi_tape_release);
262 write_unlock(&st_dev_arr_lock);
263 mutex_unlock(&st_ref_mutex);
267 static void scsi_tape_put(struct scsi_tape *STp)
269 struct scsi_device *sdev = STp->device;
271 mutex_lock(&st_ref_mutex);
272 kref_put(&STp->kref, scsi_tape_release);
273 scsi_device_put(sdev);
274 mutex_unlock(&st_ref_mutex);
277 struct st_reject_data {
281 char *driver_hint; /* Name of the correct driver, NULL if unknown */
284 static struct st_reject_data reject_list[] = {
285 /* {"XXX", "Yy-", "", NULL}, example */
289 /* If the device signature is on the list of incompatible drives, the
290 function returns a pointer to the name of the correct driver (if known) */
291 static char * st_incompatible(struct scsi_device* SDp)
293 struct st_reject_data *rp;
295 for (rp=&(reject_list[0]); rp->vendor != NULL; rp++)
296 if (!strncmp(rp->vendor, SDp->vendor, strlen(rp->vendor)) &&
297 !strncmp(rp->model, SDp->model, strlen(rp->model)) &&
298 !strncmp(rp->rev, SDp->rev, strlen(rp->rev))) {
300 return rp->driver_hint;
308 static inline char *tape_name(struct scsi_tape *tape)
310 return tape->disk->disk_name;
314 static void st_analyze_sense(struct st_request *SRpnt, struct st_cmdstatus *s)
317 const u8 *sense = SRpnt->sense;
319 s->have_sense = scsi_normalize_sense(SRpnt->sense,
320 SCSI_SENSE_BUFFERSIZE, &s->sense_hdr);
326 scsi_get_sense_info_fld(sense, SCSI_SENSE_BUFFERSIZE, &s->uremainder64);
327 switch (sense[0] & 0x7f) {
332 s->flags = sense[2] & 0xe0;
338 ucp = scsi_sense_desc_find(sense, SCSI_SENSE_BUFFERSIZE, 4);
339 s->flags = ucp ? (ucp[3] & 0xe0) : 0;
346 /* Convert the result to success code */
347 static int st_chk_result(struct scsi_tape *STp, struct st_request * SRpnt)
349 int result = SRpnt->result;
351 DEB(const char *stp;)
352 char *name = tape_name(STp);
353 struct st_cmdstatus *cmdstatp;
358 cmdstatp = &STp->buffer->cmdstat;
359 st_analyze_sense(SRpnt, cmdstatp);
361 if (cmdstatp->have_sense)
362 scode = STp->buffer->cmdstat.sense_hdr.sense_key;
368 printk(ST_DEB_MSG "%s: Error: %x, cmd: %x %x %x %x %x %x\n",
370 SRpnt->cmd[0], SRpnt->cmd[1], SRpnt->cmd[2],
371 SRpnt->cmd[3], SRpnt->cmd[4], SRpnt->cmd[5]);
372 if (cmdstatp->have_sense)
373 __scsi_print_sense(name, SRpnt->sense, SCSI_SENSE_BUFFERSIZE);
375 if (!debugging) { /* Abnormal conditions for tape */
376 if (!cmdstatp->have_sense)
378 "%s: Error %x (sugg. bt 0x%x, driver bt 0x%x, host bt 0x%x).\n",
379 name, result, suggestion(result),
380 driver_byte(result) & DRIVER_MASK, host_byte(result));
381 else if (cmdstatp->have_sense &&
383 scode != RECOVERED_ERROR &&
384 /* scode != UNIT_ATTENTION && */
385 scode != BLANK_CHECK &&
386 scode != VOLUME_OVERFLOW &&
387 SRpnt->cmd[0] != MODE_SENSE &&
388 SRpnt->cmd[0] != TEST_UNIT_READY) {
390 __scsi_print_sense(name, SRpnt->sense, SCSI_SENSE_BUFFERSIZE);
394 if (cmdstatp->fixed_format &&
395 STp->cln_mode >= EXTENDED_SENSE_START) { /* Only fixed format sense */
396 if (STp->cln_sense_value)
397 STp->cleaning_req |= ((SRpnt->sense[STp->cln_mode] &
398 STp->cln_sense_mask) == STp->cln_sense_value);
400 STp->cleaning_req |= ((SRpnt->sense[STp->cln_mode] &
401 STp->cln_sense_mask) != 0);
403 if (cmdstatp->have_sense &&
404 cmdstatp->sense_hdr.asc == 0 && cmdstatp->sense_hdr.ascq == 0x17)
405 STp->cleaning_req = 1; /* ASC and ASCQ => cleaning requested */
407 STp->pos_unknown |= STp->device->was_reset;
409 if (cmdstatp->have_sense &&
410 scode == RECOVERED_ERROR
411 #if ST_RECOVERED_WRITE_FATAL
412 && SRpnt->cmd[0] != WRITE_6
413 && SRpnt->cmd[0] != WRITE_FILEMARKS
416 STp->recover_count++;
421 if (SRpnt->cmd[0] == READ_6)
423 else if (SRpnt->cmd[0] == WRITE_6)
427 printk(ST_DEB_MSG "%s: Recovered %s error (%d).\n", name, stp,
431 if (cmdstatp->flags == 0)
438 /* Wakeup from interrupt */
439 static void st_sleep_done(void *data, char *sense, int result, int resid)
441 struct st_request *SRpnt = data;
442 struct scsi_tape *STp = SRpnt->stp;
444 memcpy(SRpnt->sense, sense, SCSI_SENSE_BUFFERSIZE);
445 (STp->buffer)->cmdstat.midlevel_result = SRpnt->result = result;
446 (STp->buffer)->cmdstat.residual = resid;
447 DEB( STp->write_pending = 0; )
450 complete(SRpnt->waiting);
453 static struct st_request *st_allocate_request(void)
455 return kzalloc(sizeof(struct st_request), GFP_KERNEL);
458 static void st_release_request(struct st_request *streq)
463 /* Do the scsi command. Waits until command performed if do_wait is true.
464 Otherwise write_behind_check() is used to check that the command
466 static struct st_request *
467 st_do_scsi(struct st_request * SRpnt, struct scsi_tape * STp, unsigned char *cmd,
468 int bytes, int direction, int timeout, int retries, int do_wait)
470 struct completion *waiting;
472 /* if async, make sure there's no command outstanding */
473 if (!do_wait && ((STp->buffer)->last_SRpnt)) {
474 printk(KERN_ERR "%s: Async command already active.\n",
476 if (signal_pending(current))
477 (STp->buffer)->syscall_result = (-EINTR);
479 (STp->buffer)->syscall_result = (-EBUSY);
484 SRpnt = st_allocate_request();
486 DEBC( printk(KERN_ERR "%s: Can't get SCSI request.\n",
488 if (signal_pending(current))
489 (STp->buffer)->syscall_result = (-EINTR);
491 (STp->buffer)->syscall_result = (-EBUSY);
497 /* If async IO, set last_SRpnt. This ptr tells write_behind_check
498 which IO is outstanding. It's nulled out when the IO completes. */
500 (STp->buffer)->last_SRpnt = SRpnt;
502 waiting = &STp->wait;
503 init_completion(waiting);
504 SRpnt->waiting = waiting;
506 if (!STp->buffer->do_dio)
507 buf_to_sg(STp->buffer, bytes);
509 memcpy(SRpnt->cmd, cmd, sizeof(SRpnt->cmd));
510 STp->buffer->cmdstat.have_sense = 0;
511 STp->buffer->syscall_result = 0;
513 if (scsi_execute_async(STp->device, cmd, COMMAND_SIZE(cmd[0]), direction,
514 &((STp->buffer)->sg[0]), bytes, (STp->buffer)->sg_segs,
515 timeout, retries, SRpnt, st_sleep_done, GFP_KERNEL)) {
516 /* could not allocate the buffer or request was too large */
517 (STp->buffer)->syscall_result = (-EBUSY);
518 (STp->buffer)->last_SRpnt = NULL;
521 wait_for_completion(waiting);
522 SRpnt->waiting = NULL;
523 (STp->buffer)->syscall_result = st_chk_result(STp, SRpnt);
530 /* Handle the write-behind checking (waits for completion). Returns -ENOSPC if
531 write has been correct but EOM early warning reached, -EIO if write ended in
532 error or zero if write successful. Asynchronous writes are used only in
533 variable block mode. */
534 static int write_behind_check(struct scsi_tape * STp)
537 struct st_buffer *STbuffer;
538 struct st_partstat *STps;
539 struct st_cmdstatus *cmdstatp;
540 struct st_request *SRpnt;
542 STbuffer = STp->buffer;
543 if (!STbuffer->writing)
547 if (STp->write_pending)
553 wait_for_completion(&(STp->wait));
554 SRpnt = STbuffer->last_SRpnt;
555 STbuffer->last_SRpnt = NULL;
556 SRpnt->waiting = NULL;
558 (STp->buffer)->syscall_result = st_chk_result(STp, SRpnt);
559 st_release_request(SRpnt);
561 STbuffer->buffer_bytes -= STbuffer->writing;
562 STps = &(STp->ps[STp->partition]);
563 if (STps->drv_block >= 0) {
564 if (STp->block_size == 0)
567 STps->drv_block += STbuffer->writing / STp->block_size;
570 cmdstatp = &STbuffer->cmdstat;
571 if (STbuffer->syscall_result) {
573 if (cmdstatp->have_sense && !cmdstatp->deferred &&
574 (cmdstatp->flags & SENSE_EOM) &&
575 (cmdstatp->sense_hdr.sense_key == NO_SENSE ||
576 cmdstatp->sense_hdr.sense_key == RECOVERED_ERROR)) {
577 /* EOM at write-behind, has all data been written? */
578 if (!cmdstatp->remainder_valid ||
579 cmdstatp->uremainder64 == 0)
583 STps->drv_block = -1;
585 STbuffer->writing = 0;
587 DEB(if (debugging && retval)
588 printk(ST_DEB_MSG "%s: Async write error %x, return value %d.\n",
589 tape_name(STp), STbuffer->cmdstat.midlevel_result, retval);) /* end DEB */
595 /* Step over EOF if it has been inadvertently crossed (ioctl not used because
596 it messes up the block number). */
597 static int cross_eof(struct scsi_tape * STp, int forward)
599 struct st_request *SRpnt;
600 unsigned char cmd[MAX_COMMAND_SIZE];
603 cmd[1] = 0x01; /* Space FileMarks */
608 cmd[2] = cmd[3] = cmd[4] = 0xff; /* -1 filemarks */
611 DEBC(printk(ST_DEB_MSG "%s: Stepping over filemark %s.\n",
612 tape_name(STp), forward ? "forward" : "backward"));
614 SRpnt = st_do_scsi(NULL, STp, cmd, 0, DMA_NONE,
615 STp->device->timeout, MAX_RETRIES, 1);
617 return (STp->buffer)->syscall_result;
619 st_release_request(SRpnt);
622 if ((STp->buffer)->cmdstat.midlevel_result != 0)
623 printk(KERN_ERR "%s: Stepping over filemark %s failed.\n",
624 tape_name(STp), forward ? "forward" : "backward");
626 return (STp->buffer)->syscall_result;
630 /* Flush the write buffer (never need to write if variable blocksize). */
631 static int st_flush_write_buffer(struct scsi_tape * STp)
633 int offset, transfer, blks;
635 unsigned char cmd[MAX_COMMAND_SIZE];
636 struct st_request *SRpnt;
637 struct st_partstat *STps;
639 result = write_behind_check(STp);
644 if (STp->dirty == 1) {
646 offset = (STp->buffer)->buffer_bytes;
647 transfer = ((offset + STp->block_size - 1) /
648 STp->block_size) * STp->block_size;
649 DEBC(printk(ST_DEB_MSG "%s: Flushing %d bytes.\n",
650 tape_name(STp), transfer));
652 memset((STp->buffer)->b_data + offset, 0, transfer - offset);
654 memset(cmd, 0, MAX_COMMAND_SIZE);
657 blks = transfer / STp->block_size;
662 SRpnt = st_do_scsi(NULL, STp, cmd, transfer, DMA_TO_DEVICE,
663 STp->device->timeout, MAX_WRITE_RETRIES, 1);
665 return (STp->buffer)->syscall_result;
667 STps = &(STp->ps[STp->partition]);
668 if ((STp->buffer)->syscall_result != 0) {
669 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
671 if (cmdstatp->have_sense && !cmdstatp->deferred &&
672 (cmdstatp->flags & SENSE_EOM) &&
673 (cmdstatp->sense_hdr.sense_key == NO_SENSE ||
674 cmdstatp->sense_hdr.sense_key == RECOVERED_ERROR) &&
675 (!cmdstatp->remainder_valid ||
676 cmdstatp->uremainder64 == 0)) { /* All written at EOM early warning */
678 (STp->buffer)->buffer_bytes = 0;
679 if (STps->drv_block >= 0)
680 STps->drv_block += blks;
683 printk(KERN_ERR "%s: Error on flush.\n",
685 STps->drv_block = (-1);
689 if (STps->drv_block >= 0)
690 STps->drv_block += blks;
692 (STp->buffer)->buffer_bytes = 0;
694 st_release_request(SRpnt);
701 /* Flush the tape buffer. The tape will be positioned correctly unless
702 seek_next is true. */
703 static int flush_buffer(struct scsi_tape *STp, int seek_next)
705 int backspace, result;
706 struct st_buffer *STbuffer;
707 struct st_partstat *STps;
709 STbuffer = STp->buffer;
712 * If there was a bus reset, block further access
715 if (STp->pos_unknown)
718 if (STp->ready != ST_READY)
720 STps = &(STp->ps[STp->partition]);
721 if (STps->rw == ST_WRITING) /* Writing */
722 return st_flush_write_buffer(STp);
724 if (STp->block_size == 0)
727 backspace = ((STp->buffer)->buffer_bytes +
728 (STp->buffer)->read_pointer) / STp->block_size -
729 ((STp->buffer)->read_pointer + STp->block_size - 1) /
731 (STp->buffer)->buffer_bytes = 0;
732 (STp->buffer)->read_pointer = 0;
735 if (STps->eof == ST_FM_HIT) {
736 result = cross_eof(STp, 0); /* Back over the EOF hit */
738 STps->eof = ST_NOEOF;
740 if (STps->drv_file >= 0)
745 if (!result && backspace > 0)
746 result = st_int_ioctl(STp, MTBSR, backspace);
747 } else if (STps->eof == ST_FM_HIT) {
748 if (STps->drv_file >= 0)
751 STps->eof = ST_NOEOF;
757 /* Set the mode parameters */
758 static int set_mode_densblk(struct scsi_tape * STp, struct st_modedef * STm)
762 char *name = tape_name(STp);
764 if (!STp->density_changed &&
765 STm->default_density >= 0 &&
766 STm->default_density != STp->density) {
767 arg = STm->default_density;
771 arg <<= MT_ST_DENSITY_SHIFT;
772 if (!STp->blksize_changed &&
773 STm->default_blksize >= 0 &&
774 STm->default_blksize != STp->block_size) {
775 arg |= STm->default_blksize;
778 arg |= STp->block_size;
780 st_int_ioctl(STp, SET_DENS_AND_BLK, arg)) {
782 "%s: Can't set default block size to %d bytes and density %x.\n",
783 name, STm->default_blksize, STm->default_density);
791 /* Lock or unlock the drive door. Don't use when st_request allocated. */
792 static int do_door_lock(struct scsi_tape * STp, int do_lock)
795 DEB(char *name = tape_name(STp);)
798 cmd = do_lock ? SCSI_IOCTL_DOORLOCK : SCSI_IOCTL_DOORUNLOCK;
799 DEBC(printk(ST_DEB_MSG "%s: %socking drive door.\n", name,
800 do_lock ? "L" : "Unl"));
801 retval = scsi_ioctl(STp->device, cmd, NULL);
803 STp->door_locked = do_lock ? ST_LOCKED_EXPLICIT : ST_UNLOCKED;
806 STp->door_locked = ST_LOCK_FAILS;
812 /* Set the internal state after reset */
813 static void reset_state(struct scsi_tape *STp)
816 struct st_partstat *STps;
818 STp->pos_unknown = 0;
819 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
820 STps = &(STp->ps[i]);
822 STps->eof = ST_NOEOF;
824 STps->last_block_valid = 0;
825 STps->drv_block = -1;
828 if (STp->can_partitions) {
829 STp->partition = find_partition(STp);
830 if (STp->partition < 0)
832 STp->new_partition = STp->partition;
836 /* Test if the drive is ready. Returns either one of the codes below or a negative system
838 #define CHKRES_READY 0
839 #define CHKRES_NEW_SESSION 1
840 #define CHKRES_NOT_READY 2
841 #define CHKRES_NO_TAPE 3
843 #define MAX_ATTENTIONS 10
845 static int test_ready(struct scsi_tape *STp, int do_wait)
847 int attentions, waits, max_wait, scode;
848 int retval = CHKRES_READY, new_session = 0;
849 unsigned char cmd[MAX_COMMAND_SIZE];
850 struct st_request *SRpnt = NULL;
851 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
853 max_wait = do_wait ? ST_BLOCK_SECONDS : 0;
855 for (attentions=waits=0; ; ) {
856 memset((void *) &cmd[0], 0, MAX_COMMAND_SIZE);
857 cmd[0] = TEST_UNIT_READY;
858 SRpnt = st_do_scsi(SRpnt, STp, cmd, 0, DMA_NONE,
859 STp->long_timeout, MAX_READY_RETRIES, 1);
862 retval = (STp->buffer)->syscall_result;
866 if (cmdstatp->have_sense) {
868 scode = cmdstatp->sense_hdr.sense_key;
870 if (scode == UNIT_ATTENTION) { /* New media? */
872 if (attentions < MAX_ATTENTIONS) {
882 if (scode == NOT_READY) {
883 if (waits < max_wait) {
884 if (msleep_interruptible(1000)) {
892 if ((STp->device)->scsi_level >= SCSI_2 &&
893 cmdstatp->sense_hdr.asc == 0x3a) /* Check ASC */
894 retval = CHKRES_NO_TAPE;
896 retval = CHKRES_NOT_READY;
902 retval = (STp->buffer)->syscall_result;
904 retval = new_session ? CHKRES_NEW_SESSION : CHKRES_READY;
909 st_release_request(SRpnt);
914 /* See if the drive is ready and gather information about the tape. Return values:
915 < 0 negative error code from errno.h
917 1 drive not ready (possibly no tape)
919 static int check_tape(struct scsi_tape *STp, struct file *filp)
921 int i, retval, new_session = 0, do_wait;
922 unsigned char cmd[MAX_COMMAND_SIZE], saved_cleaning;
923 unsigned short st_flags = filp->f_flags;
924 struct st_request *SRpnt = NULL;
925 struct st_modedef *STm;
926 struct st_partstat *STps;
927 char *name = tape_name(STp);
928 struct inode *inode = filp->f_path.dentry->d_inode;
929 int mode = TAPE_MODE(inode);
931 STp->ready = ST_READY;
933 if (mode != STp->current_mode) {
934 DEBC(printk(ST_DEB_MSG "%s: Mode change from %d to %d.\n",
935 name, STp->current_mode, mode));
937 STp->current_mode = mode;
939 STm = &(STp->modes[STp->current_mode]);
941 saved_cleaning = STp->cleaning_req;
942 STp->cleaning_req = 0;
944 do_wait = ((filp->f_flags & O_NONBLOCK) == 0);
945 retval = test_ready(STp, do_wait);
950 if (retval == CHKRES_NEW_SESSION) {
951 STp->pos_unknown = 0;
952 STp->partition = STp->new_partition = 0;
953 if (STp->can_partitions)
954 STp->nbr_partitions = 1; /* This guess will be updated later
956 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
957 STps = &(STp->ps[i]);
959 STps->eof = ST_NOEOF;
961 STps->last_block_valid = 0;
968 STp->cleaning_req |= saved_cleaning;
970 if (retval == CHKRES_NOT_READY || retval == CHKRES_NO_TAPE) {
971 if (retval == CHKRES_NO_TAPE)
972 STp->ready = ST_NO_TAPE;
974 STp->ready = ST_NOT_READY;
976 STp->density = 0; /* Clear the erroneous "residue" */
979 STp->ps[0].drv_file = STp->ps[0].drv_block = (-1);
980 STp->partition = STp->new_partition = 0;
981 STp->door_locked = ST_UNLOCKED;
982 return CHKRES_NOT_READY;
986 if (STp->omit_blklims)
987 STp->min_block = STp->max_block = (-1);
989 memset((void *) &cmd[0], 0, MAX_COMMAND_SIZE);
990 cmd[0] = READ_BLOCK_LIMITS;
992 SRpnt = st_do_scsi(SRpnt, STp, cmd, 6, DMA_FROM_DEVICE,
993 STp->device->timeout, MAX_READY_RETRIES, 1);
995 retval = (STp->buffer)->syscall_result;
999 if (!SRpnt->result && !STp->buffer->cmdstat.have_sense) {
1000 STp->max_block = ((STp->buffer)->b_data[1] << 16) |
1001 ((STp->buffer)->b_data[2] << 8) | (STp->buffer)->b_data[3];
1002 STp->min_block = ((STp->buffer)->b_data[4] << 8) |
1003 (STp->buffer)->b_data[5];
1004 if ( DEB( debugging || ) !STp->inited)
1006 "%s: Block limits %d - %d bytes.\n", name,
1007 STp->min_block, STp->max_block);
1009 STp->min_block = STp->max_block = (-1);
1010 DEBC(printk(ST_DEB_MSG "%s: Can't read block limits.\n",
1015 memset((void *) &cmd[0], 0, MAX_COMMAND_SIZE);
1016 cmd[0] = MODE_SENSE;
1019 SRpnt = st_do_scsi(SRpnt, STp, cmd, 12, DMA_FROM_DEVICE,
1020 STp->device->timeout, MAX_READY_RETRIES, 1);
1022 retval = (STp->buffer)->syscall_result;
1026 if ((STp->buffer)->syscall_result != 0) {
1027 DEBC(printk(ST_DEB_MSG "%s: No Mode Sense.\n", name));
1028 STp->block_size = ST_DEFAULT_BLOCK; /* Educated guess (?) */
1029 (STp->buffer)->syscall_result = 0; /* Prevent error propagation */
1030 STp->drv_write_prot = 0;
1032 DEBC(printk(ST_DEB_MSG
1033 "%s: Mode sense. Length %d, medium %x, WBS %x, BLL %d\n",
1035 (STp->buffer)->b_data[0], (STp->buffer)->b_data[1],
1036 (STp->buffer)->b_data[2], (STp->buffer)->b_data[3]));
1038 if ((STp->buffer)->b_data[3] >= 8) {
1039 STp->drv_buffer = ((STp->buffer)->b_data[2] >> 4) & 7;
1040 STp->density = (STp->buffer)->b_data[4];
1041 STp->block_size = (STp->buffer)->b_data[9] * 65536 +
1042 (STp->buffer)->b_data[10] * 256 + (STp->buffer)->b_data[11];
1043 DEBC(printk(ST_DEB_MSG
1044 "%s: Density %x, tape length: %x, drv buffer: %d\n",
1045 name, STp->density, (STp->buffer)->b_data[5] * 65536 +
1046 (STp->buffer)->b_data[6] * 256 + (STp->buffer)->b_data[7],
1049 STp->drv_write_prot = ((STp->buffer)->b_data[2] & 0x80) != 0;
1051 st_release_request(SRpnt);
1055 if (STp->block_size > 0)
1056 (STp->buffer)->buffer_blocks =
1057 (STp->buffer)->buffer_size / STp->block_size;
1059 (STp->buffer)->buffer_blocks = 1;
1060 (STp->buffer)->buffer_bytes = (STp->buffer)->read_pointer = 0;
1062 DEBC(printk(ST_DEB_MSG
1063 "%s: Block size: %d, buffer size: %d (%d blocks).\n", name,
1064 STp->block_size, (STp->buffer)->buffer_size,
1065 (STp->buffer)->buffer_blocks));
1067 if (STp->drv_write_prot) {
1068 STp->write_prot = 1;
1070 DEBC(printk(ST_DEB_MSG "%s: Write protected\n", name));
1073 ((st_flags & O_ACCMODE) == O_WRONLY ||
1074 (st_flags & O_ACCMODE) == O_RDWR)) {
1080 if (STp->can_partitions && STp->nbr_partitions < 1) {
1081 /* This code is reached when the device is opened for the first time
1082 after the driver has been initialized with tape in the drive and the
1083 partition support has been enabled. */
1084 DEBC(printk(ST_DEB_MSG
1085 "%s: Updating partition number in status.\n", name));
1086 if ((STp->partition = find_partition(STp)) < 0) {
1087 retval = STp->partition;
1090 STp->new_partition = STp->partition;
1091 STp->nbr_partitions = 1; /* This guess will be updated when necessary */
1094 if (new_session) { /* Change the drive parameters for the new mode */
1095 STp->density_changed = STp->blksize_changed = 0;
1096 STp->compression_changed = 0;
1097 if (!(STm->defaults_for_writes) &&
1098 (retval = set_mode_densblk(STp, STm)) < 0)
1101 if (STp->default_drvbuffer != 0xff) {
1102 if (st_int_ioctl(STp, MTSETDRVBUFFER, STp->default_drvbuffer))
1104 "%s: Can't set default drive buffering to %d.\n",
1105 name, STp->default_drvbuffer);
1109 return CHKRES_READY;
1116 \f/* Open the device. Needs to be called with BKL only because of incrementing the SCSI host
1118 static int st_open(struct inode *inode, struct file *filp)
1120 int i, retval = (-EIO);
1121 struct scsi_tape *STp;
1122 struct st_partstat *STps;
1123 int dev = TAPE_NR(inode);
1127 * We really want to do nonseekable_open(inode, filp); here, but some
1128 * versions of tar incorrectly call lseek on tapes and bail out if that
1129 * fails. So we disallow pread() and pwrite(), but permit lseeks.
1131 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
1133 if (!(STp = scsi_tape_get(dev)))
1136 write_lock(&st_dev_arr_lock);
1137 filp->private_data = STp;
1138 name = tape_name(STp);
1141 write_unlock(&st_dev_arr_lock);
1143 DEB( printk(ST_DEB_MSG "%s: Device already in use.\n", name); )
1148 write_unlock(&st_dev_arr_lock);
1149 STp->rew_at_close = STp->autorew_dev = (iminor(inode) & 0x80) == 0;
1151 if (!scsi_block_when_processing_errors(STp->device)) {
1156 /* See that we have at least a one page buffer available */
1157 if (!enlarge_buffer(STp->buffer, PAGE_SIZE, STp->restr_dma)) {
1158 printk(KERN_WARNING "%s: Can't allocate one page tape buffer.\n",
1160 retval = (-EOVERFLOW);
1164 (STp->buffer)->cleared = 0;
1165 (STp->buffer)->writing = 0;
1166 (STp->buffer)->syscall_result = 0;
1168 STp->write_prot = ((filp->f_flags & O_ACCMODE) == O_RDONLY);
1171 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
1172 STps = &(STp->ps[i]);
1175 STp->try_dio_now = STp->try_dio;
1176 STp->recover_count = 0;
1177 DEB( STp->nbr_waits = STp->nbr_finished = 0;
1178 STp->nbr_requests = STp->nbr_dio = STp->nbr_pages = 0; )
1180 retval = check_tape(STp, filp);
1183 if ((filp->f_flags & O_NONBLOCK) == 0 &&
1184 retval != CHKRES_READY) {
1185 if (STp->ready == NO_TAPE)
1186 retval = (-ENOMEDIUM);
1194 normalize_buffer(STp->buffer);
1202 /* Flush the tape buffer before close */
1203 static int st_flush(struct file *filp, fl_owner_t id)
1205 int result = 0, result2;
1206 unsigned char cmd[MAX_COMMAND_SIZE];
1207 struct st_request *SRpnt;
1208 struct scsi_tape *STp = filp->private_data;
1209 struct st_modedef *STm = &(STp->modes[STp->current_mode]);
1210 struct st_partstat *STps = &(STp->ps[STp->partition]);
1211 char *name = tape_name(STp);
1213 if (file_count(filp) > 1)
1216 if (STps->rw == ST_WRITING && !STp->pos_unknown) {
1217 result = st_flush_write_buffer(STp);
1218 if (result != 0 && result != (-ENOSPC))
1222 if (STp->can_partitions &&
1223 (result2 = switch_partition(STp)) < 0) {
1224 DEBC(printk(ST_DEB_MSG
1225 "%s: switch_partition at close failed.\n", name));
1231 DEBC( if (STp->nbr_requests)
1232 printk(KERN_DEBUG "%s: Number of r/w requests %d, dio used in %d, pages %d.\n",
1233 name, STp->nbr_requests, STp->nbr_dio, STp->nbr_pages));
1235 if (STps->rw == ST_WRITING && !STp->pos_unknown) {
1236 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
1238 DEBC(printk(ST_DEB_MSG "%s: Async write waits %d, finished %d.\n",
1239 name, STp->nbr_waits, STp->nbr_finished);
1242 memset(cmd, 0, MAX_COMMAND_SIZE);
1243 cmd[0] = WRITE_FILEMARKS;
1244 cmd[4] = 1 + STp->two_fm;
1246 SRpnt = st_do_scsi(NULL, STp, cmd, 0, DMA_NONE,
1247 STp->device->timeout, MAX_WRITE_RETRIES, 1);
1249 result = (STp->buffer)->syscall_result;
1253 if (STp->buffer->syscall_result == 0 ||
1254 (cmdstatp->have_sense && !cmdstatp->deferred &&
1255 (cmdstatp->flags & SENSE_EOM) &&
1256 (cmdstatp->sense_hdr.sense_key == NO_SENSE ||
1257 cmdstatp->sense_hdr.sense_key == RECOVERED_ERROR) &&
1258 (!cmdstatp->remainder_valid || cmdstatp->uremainder64 == 0))) {
1259 /* Write successful at EOM */
1260 st_release_request(SRpnt);
1262 if (STps->drv_file >= 0)
1264 STps->drv_block = 0;
1269 else { /* Write error */
1270 st_release_request(SRpnt);
1272 printk(KERN_ERR "%s: Error on write filemark.\n", name);
1277 DEBC(printk(ST_DEB_MSG "%s: Buffer flushed, %d EOF(s) written\n",
1279 } else if (!STp->rew_at_close) {
1280 STps = &(STp->ps[STp->partition]);
1281 if (!STm->sysv || STps->rw != ST_READING) {
1283 result = flush_buffer(STp, 0);
1284 else if (STps->eof == ST_FM_HIT) {
1285 result = cross_eof(STp, 0);
1287 if (STps->drv_file >= 0)
1289 STps->drv_block = 0;
1292 STps->eof = ST_NOEOF;
1294 } else if ((STps->eof == ST_NOEOF &&
1295 !(result = cross_eof(STp, 1))) ||
1296 STps->eof == ST_FM_HIT) {
1297 if (STps->drv_file >= 0)
1299 STps->drv_block = 0;
1305 if (STp->rew_at_close) {
1306 result2 = st_int_ioctl(STp, MTREW, 1);
1314 /* Close the device and release it. BKL is not needed: this is the only thread
1315 accessing this tape. */
1316 static int st_release(struct inode *inode, struct file *filp)
1319 struct scsi_tape *STp = filp->private_data;
1321 if (STp->door_locked == ST_LOCKED_AUTO)
1322 do_door_lock(STp, 0);
1324 normalize_buffer(STp->buffer);
1325 write_lock(&st_dev_arr_lock);
1327 write_unlock(&st_dev_arr_lock);
1333 /* The checks common to both reading and writing */
1334 static ssize_t rw_checks(struct scsi_tape *STp, struct file *filp, size_t count)
1339 * If we are in the middle of error recovery, don't let anyone
1340 * else try and use this device. Also, if error recovery fails, it
1341 * may try and take the device offline, in which case all further
1342 * access to the device is prohibited.
1344 if (!scsi_block_when_processing_errors(STp->device)) {
1349 if (STp->ready != ST_READY) {
1350 if (STp->ready == ST_NO_TAPE)
1351 retval = (-ENOMEDIUM);
1357 if (! STp->modes[STp->current_mode].defined) {
1364 * If there was a bus reset, block further access
1367 if (STp->pos_unknown) {
1377 printk(ST_DEB_MSG "%s: Incorrect device.\n", tape_name(STp));
1382 if (STp->can_partitions &&
1383 (retval = switch_partition(STp)) < 0)
1386 if (STp->block_size == 0 && STp->max_block > 0 &&
1387 (count < STp->min_block || count > STp->max_block)) {
1392 if (STp->do_auto_lock && STp->door_locked == ST_UNLOCKED &&
1393 !do_door_lock(STp, 1))
1394 STp->door_locked = ST_LOCKED_AUTO;
1401 static int setup_buffering(struct scsi_tape *STp, const char __user *buf,
1402 size_t count, int is_read)
1404 int i, bufsize, retval = 0;
1405 struct st_buffer *STbp = STp->buffer;
1408 i = STp->try_dio_now && try_rdio;
1410 i = STp->try_dio_now && try_wdio;
1412 if (i && ((unsigned long)buf & queue_dma_alignment(
1413 STp->device->request_queue)) == 0) {
1414 i = sgl_map_user_pages(&(STbp->sg[0]), STbp->use_sg,
1415 (unsigned long)buf, count, (is_read ? READ : WRITE));
1418 STbp->buffer_bytes = 0; /* can be used as transfer counter */
1421 STbp->do_dio = 0; /* fall back to buffering with any error */
1422 STbp->sg_segs = STbp->do_dio;
1423 STbp->frp_sg_current = 0;
1427 STp->nbr_pages += STbp->do_dio;
1432 DEB( STp->nbr_requests++; )
1434 if (!STbp->do_dio) {
1435 if (STp->block_size)
1436 bufsize = STp->block_size > st_fixed_buffer_size ?
1437 STp->block_size : st_fixed_buffer_size;
1440 /* Make sure that data from previous user is not leaked even if
1441 HBA does not return correct residual */
1442 if (is_read && STp->sili && !STbp->cleared)
1446 if (bufsize > STbp->buffer_size &&
1447 !enlarge_buffer(STbp, bufsize, STp->restr_dma)) {
1448 printk(KERN_WARNING "%s: Can't allocate %d byte tape buffer.\n",
1449 tape_name(STp), bufsize);
1450 retval = (-EOVERFLOW);
1453 if (STp->block_size)
1454 STbp->buffer_blocks = bufsize / STp->block_size;
1462 /* Can be called more than once after each setup_buffer() */
1463 static void release_buffering(struct scsi_tape *STp, int is_read)
1465 struct st_buffer *STbp;
1469 sgl_unmap_user_pages(&(STbp->sg[0]), STbp->do_dio, is_read);
1478 st_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
1481 ssize_t i, do_count, blks, transfer;
1483 int undone, retry_eot = 0, scode;
1485 unsigned char cmd[MAX_COMMAND_SIZE];
1486 const char __user *b_point;
1487 struct st_request *SRpnt = NULL;
1488 struct scsi_tape *STp = filp->private_data;
1489 struct st_modedef *STm;
1490 struct st_partstat *STps;
1491 struct st_buffer *STbp;
1492 char *name = tape_name(STp);
1494 if (mutex_lock_interruptible(&STp->lock))
1495 return -ERESTARTSYS;
1497 retval = rw_checks(STp, filp, count);
1498 if (retval || count == 0)
1501 /* Write must be integral number of blocks */
1502 if (STp->block_size != 0 && (count % STp->block_size) != 0) {
1503 printk(KERN_WARNING "%s: Write not multiple of tape block size.\n",
1509 STm = &(STp->modes[STp->current_mode]);
1510 STps = &(STp->ps[STp->partition]);
1512 if (STp->write_prot) {
1518 if (STps->rw == ST_READING) {
1519 retval = flush_buffer(STp, 0);
1522 STps->rw = ST_WRITING;
1523 } else if (STps->rw != ST_WRITING &&
1524 STps->drv_file == 0 && STps->drv_block == 0) {
1525 if ((retval = set_mode_densblk(STp, STm)) < 0)
1527 if (STm->default_compression != ST_DONT_TOUCH &&
1528 !(STp->compression_changed)) {
1529 if (st_compression(STp, (STm->default_compression == ST_YES))) {
1530 printk(KERN_WARNING "%s: Can't set default compression.\n",
1532 if (modes_defined) {
1541 i = write_behind_check(STp);
1544 STps->eof = ST_EOM_OK;
1546 STps->eof = ST_EOM_ERROR;
1549 if (STps->eof == ST_EOM_OK) {
1550 STps->eof = ST_EOD_1; /* allow next write */
1554 else if (STps->eof == ST_EOM_ERROR) {
1559 /* Check the buffer readability in cases where copy_user might catch
1560 the problems after some tape movement. */
1561 if (STp->block_size != 0 &&
1563 (copy_from_user(&i, buf, 1) != 0 ||
1564 copy_from_user(&i, buf + count - 1, 1) != 0)) {
1569 retval = setup_buffering(STp, buf, count, 0);
1575 memset(cmd, 0, MAX_COMMAND_SIZE);
1577 cmd[1] = (STp->block_size != 0);
1579 STps->rw = ST_WRITING;
1582 while (count > 0 && !retry_eot) {
1588 if (STp->block_size == 0)
1591 do_count = STbp->buffer_blocks * STp->block_size -
1593 if (do_count > count)
1597 i = append_to_buffer(b_point, STbp, do_count);
1604 b_point += do_count;
1606 async_write = STp->block_size == 0 && !STbp->do_dio &&
1607 STm->do_async_writes && STps->eof < ST_EOM_OK;
1609 if (STp->block_size != 0 && STm->do_buffer_writes &&
1610 !(STp->try_dio_now && try_wdio) && STps->eof < ST_EOM_OK &&
1611 STbp->buffer_bytes < STbp->buffer_size) {
1613 /* Don't write a buffer that is not full enough. */
1614 if (!async_write && count == 0)
1619 if (STp->block_size == 0)
1620 blks = transfer = do_count;
1623 blks = STbp->buffer_bytes;
1626 blks /= STp->block_size;
1627 transfer = blks * STp->block_size;
1629 cmd[2] = blks >> 16;
1633 SRpnt = st_do_scsi(SRpnt, STp, cmd, transfer, DMA_TO_DEVICE,
1634 STp->device->timeout, MAX_WRITE_RETRIES, !async_write);
1636 retval = STbp->syscall_result;
1639 if (async_write && !STbp->syscall_result) {
1640 STbp->writing = transfer;
1641 STp->dirty = !(STbp->writing ==
1642 STbp->buffer_bytes);
1643 SRpnt = NULL; /* Prevent releasing this request! */
1644 DEB( STp->write_pending = 1; )
1648 if (STbp->syscall_result != 0) {
1649 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
1651 DEBC(printk(ST_DEB_MSG "%s: Error on write:\n", name));
1652 if (cmdstatp->have_sense && (cmdstatp->flags & SENSE_EOM)) {
1653 scode = cmdstatp->sense_hdr.sense_key;
1654 if (cmdstatp->remainder_valid)
1655 undone = (int)cmdstatp->uremainder64;
1656 else if (STp->block_size == 0 &&
1657 scode == VOLUME_OVERFLOW)
1661 if (STp->block_size != 0)
1662 undone *= STp->block_size;
1663 if (undone <= do_count) {
1664 /* Only data from this write is not written */
1667 if (STp->block_size)
1668 blks = (transfer - undone) / STp->block_size;
1669 STps->eof = ST_EOM_OK;
1670 /* Continue in fixed block mode if all written
1671 in this request but still something left to write
1672 (retval left to zero)
1674 if (STp->block_size == 0 ||
1675 undone > 0 || count == 0)
1676 retval = (-ENOSPC); /* EOM within current request */
1677 DEBC(printk(ST_DEB_MSG
1678 "%s: EOM with %d bytes unwritten.\n",
1681 /* EOT within data buffered earlier (possible only
1682 in fixed block mode without direct i/o) */
1683 if (!retry_eot && !cmdstatp->deferred &&
1684 (scode == NO_SENSE || scode == RECOVERED_ERROR)) {
1685 move_buffer_data(STp->buffer, transfer - undone);
1687 if (STps->drv_block >= 0) {
1688 STps->drv_block += (transfer - undone) /
1691 STps->eof = ST_EOM_OK;
1692 DEBC(printk(ST_DEB_MSG
1693 "%s: Retry write of %d bytes at EOM.\n",
1694 name, STp->buffer->buffer_bytes));
1698 /* Either error within data buffered by driver or
1701 blks = do_count = 0;
1702 STps->eof = ST_EOM_ERROR;
1703 STps->drv_block = (-1); /* Too cautious? */
1704 retval = (-EIO); /* EOM for old data */
1705 DEBC(printk(ST_DEB_MSG
1706 "%s: EOM with lost data.\n",
1712 STps->drv_block = (-1); /* Too cautious? */
1713 retval = STbp->syscall_result;
1718 if (STps->drv_block >= 0) {
1719 if (STp->block_size == 0)
1720 STps->drv_block += (do_count > 0);
1722 STps->drv_block += blks;
1725 STbp->buffer_bytes = 0;
1728 if (retval || retry_eot) {
1730 retval = total - count;
1735 if (STps->eof == ST_EOD_1)
1736 STps->eof = ST_EOM_OK;
1737 else if (STps->eof != ST_EOM_OK)
1738 STps->eof = ST_NOEOF;
1739 retval = total - count;
1743 st_release_request(SRpnt);
1744 release_buffering(STp, 0);
1745 mutex_unlock(&STp->lock);
1750 /* Read data from the tape. Returns zero in the normal case, one if the
1751 eof status has changed, and the negative error code in case of a
1752 fatal error. Otherwise updates the buffer and the eof state.
1754 Does release user buffer mapping if it is set.
1756 static long read_tape(struct scsi_tape *STp, long count,
1757 struct st_request ** aSRpnt)
1759 int transfer, blks, bytes;
1760 unsigned char cmd[MAX_COMMAND_SIZE];
1761 struct st_request *SRpnt;
1762 struct st_modedef *STm;
1763 struct st_partstat *STps;
1764 struct st_buffer *STbp;
1766 char *name = tape_name(STp);
1771 STm = &(STp->modes[STp->current_mode]);
1772 STps = &(STp->ps[STp->partition]);
1773 if (STps->eof == ST_FM_HIT)
1777 if (STp->block_size == 0)
1778 blks = bytes = count;
1780 if (!(STp->try_dio_now && try_rdio) && STm->do_read_ahead) {
1781 blks = (STp->buffer)->buffer_blocks;
1782 bytes = blks * STp->block_size;
1785 if (!STbp->do_dio && bytes > (STp->buffer)->buffer_size)
1786 bytes = (STp->buffer)->buffer_size;
1787 blks = bytes / STp->block_size;
1788 bytes = blks * STp->block_size;
1792 memset(cmd, 0, MAX_COMMAND_SIZE);
1794 cmd[1] = (STp->block_size != 0);
1795 if (!cmd[1] && STp->sili)
1797 cmd[2] = blks >> 16;
1802 SRpnt = st_do_scsi(SRpnt, STp, cmd, bytes, DMA_FROM_DEVICE,
1803 STp->device->timeout, MAX_RETRIES, 1);
1804 release_buffering(STp, 1);
1807 return STbp->syscall_result;
1809 STbp->read_pointer = 0;
1812 /* Something to check */
1813 if (STbp->syscall_result) {
1814 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
1817 DEBC(printk(ST_DEB_MSG "%s: Sense: %2x %2x %2x %2x %2x %2x %2x %2x\n",
1819 SRpnt->sense[0], SRpnt->sense[1],
1820 SRpnt->sense[2], SRpnt->sense[3],
1821 SRpnt->sense[4], SRpnt->sense[5],
1822 SRpnt->sense[6], SRpnt->sense[7]));
1823 if (cmdstatp->have_sense) {
1825 if (cmdstatp->sense_hdr.sense_key == BLANK_CHECK)
1826 cmdstatp->flags &= 0xcf; /* No need for EOM in this case */
1828 if (cmdstatp->flags != 0) { /* EOF, EOM, or ILI */
1829 /* Compute the residual count */
1830 if (cmdstatp->remainder_valid)
1831 transfer = (int)cmdstatp->uremainder64;
1834 if (STp->block_size == 0 &&
1835 cmdstatp->sense_hdr.sense_key == MEDIUM_ERROR)
1838 if (cmdstatp->flags & SENSE_ILI) { /* ILI */
1839 if (STp->block_size == 0) {
1840 if (transfer <= 0) {
1843 "%s: Failed to read %d byte block with %d byte transfer.\n",
1844 name, bytes - transfer, bytes);
1845 if (STps->drv_block >= 0)
1846 STps->drv_block += 1;
1847 STbp->buffer_bytes = 0;
1850 STbp->buffer_bytes = bytes - transfer;
1852 st_release_request(SRpnt);
1853 SRpnt = *aSRpnt = NULL;
1854 if (transfer == blks) { /* We did not get anything, error */
1855 printk(KERN_NOTICE "%s: Incorrect block size.\n", name);
1856 if (STps->drv_block >= 0)
1857 STps->drv_block += blks - transfer + 1;
1858 st_int_ioctl(STp, MTBSR, 1);
1861 /* We have some data, deliver it */
1862 STbp->buffer_bytes = (blks - transfer) *
1864 DEBC(printk(ST_DEB_MSG
1865 "%s: ILI but enough data received %ld %d.\n",
1866 name, count, STbp->buffer_bytes));
1867 if (STps->drv_block >= 0)
1868 STps->drv_block += 1;
1869 if (st_int_ioctl(STp, MTBSR, 1))
1872 } else if (cmdstatp->flags & SENSE_FMK) { /* FM overrides EOM */
1873 if (STps->eof != ST_FM_HIT)
1874 STps->eof = ST_FM_HIT;
1876 STps->eof = ST_EOD_2;
1877 if (STp->block_size == 0)
1878 STbp->buffer_bytes = 0;
1880 STbp->buffer_bytes =
1881 bytes - transfer * STp->block_size;
1882 DEBC(printk(ST_DEB_MSG
1883 "%s: EOF detected (%d bytes read).\n",
1884 name, STbp->buffer_bytes));
1885 } else if (cmdstatp->flags & SENSE_EOM) {
1886 if (STps->eof == ST_FM)
1887 STps->eof = ST_EOD_1;
1889 STps->eof = ST_EOM_OK;
1890 if (STp->block_size == 0)
1891 STbp->buffer_bytes = bytes - transfer;
1893 STbp->buffer_bytes =
1894 bytes - transfer * STp->block_size;
1896 DEBC(printk(ST_DEB_MSG "%s: EOM detected (%d bytes read).\n",
1897 name, STbp->buffer_bytes));
1900 /* end of EOF, EOM, ILI test */
1901 else { /* nonzero sense key */
1902 DEBC(printk(ST_DEB_MSG
1903 "%s: Tape error while reading.\n", name));
1904 STps->drv_block = (-1);
1905 if (STps->eof == ST_FM &&
1906 cmdstatp->sense_hdr.sense_key == BLANK_CHECK) {
1907 DEBC(printk(ST_DEB_MSG
1908 "%s: Zero returned for first BLANK CHECK after EOF.\n",
1910 STps->eof = ST_EOD_2; /* First BLANK_CHECK after FM */
1911 } else /* Some other extended sense code */
1915 if (STbp->buffer_bytes < 0) /* Caused by bogus sense data */
1916 STbp->buffer_bytes = 0;
1918 /* End of extended sense test */
1919 else { /* Non-extended sense */
1920 retval = STbp->syscall_result;
1924 /* End of error handling */
1925 else { /* Read successful */
1926 STbp->buffer_bytes = bytes;
1927 if (STp->sili) /* In fixed block mode residual is always zero here */
1928 STbp->buffer_bytes -= STp->buffer->cmdstat.residual;
1931 if (STps->drv_block >= 0) {
1932 if (STp->block_size == 0)
1935 STps->drv_block += STbp->buffer_bytes / STp->block_size;
1943 st_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
1947 ssize_t i, transfer;
1948 int special, do_dio = 0;
1949 struct st_request *SRpnt = NULL;
1950 struct scsi_tape *STp = filp->private_data;
1951 struct st_modedef *STm;
1952 struct st_partstat *STps;
1953 struct st_buffer *STbp = STp->buffer;
1954 DEB( char *name = tape_name(STp); )
1956 if (mutex_lock_interruptible(&STp->lock))
1957 return -ERESTARTSYS;
1959 retval = rw_checks(STp, filp, count);
1960 if (retval || count == 0)
1963 STm = &(STp->modes[STp->current_mode]);
1964 if (STp->block_size != 0 && (count % STp->block_size) != 0) {
1965 if (!STm->do_read_ahead) {
1966 retval = (-EINVAL); /* Read must be integral number of blocks */
1969 STp->try_dio_now = 0; /* Direct i/o can't handle split blocks */
1972 STps = &(STp->ps[STp->partition]);
1973 if (STps->rw == ST_WRITING) {
1974 retval = flush_buffer(STp, 0);
1977 STps->rw = ST_READING;
1980 if (debugging && STps->eof != ST_NOEOF)
1981 printk(ST_DEB_MSG "%s: EOF/EOM flag up (%d). Bytes %d\n", name,
1982 STps->eof, STbp->buffer_bytes);
1985 retval = setup_buffering(STp, buf, count, 1);
1988 do_dio = STbp->do_dio;
1990 if (STbp->buffer_bytes == 0 &&
1991 STps->eof >= ST_EOD_1) {
1992 if (STps->eof < ST_EOD) {
1997 retval = (-EIO); /* EOM or Blank Check */
2002 /* Check the buffer writability before any tape movement. Don't alter
2004 if (copy_from_user(&i, buf, 1) != 0 ||
2005 copy_to_user(buf, &i, 1) != 0 ||
2006 copy_from_user(&i, buf + count - 1, 1) != 0 ||
2007 copy_to_user(buf + count - 1, &i, 1) != 0) {
2013 STps->rw = ST_READING;
2016 /* Loop until enough data in buffer or a special condition found */
2017 for (total = 0, special = 0; total < count && !special;) {
2019 /* Get new data if the buffer is empty */
2020 if (STbp->buffer_bytes == 0) {
2021 special = read_tape(STp, count - total, &SRpnt);
2022 if (special < 0) { /* No need to continue read */
2028 /* Move the data from driver buffer to user buffer */
2029 if (STbp->buffer_bytes > 0) {
2031 if (debugging && STps->eof != ST_NOEOF)
2033 "%s: EOF up (%d). Left %d, needed %d.\n", name,
2034 STps->eof, STbp->buffer_bytes,
2035 (int)(count - total));
2037 transfer = STbp->buffer_bytes < count - total ?
2038 STbp->buffer_bytes : count - total;
2040 i = from_buffer(STbp, buf, transfer);
2050 if (STp->block_size == 0)
2051 break; /* Read only one variable length block */
2053 } /* for (total = 0, special = 0;
2054 total < count && !special; ) */
2056 /* Change the eof state if no data from tape or buffer */
2058 if (STps->eof == ST_FM_HIT) {
2060 STps->drv_block = 0;
2061 if (STps->drv_file >= 0)
2063 } else if (STps->eof == ST_EOD_1) {
2064 STps->eof = ST_EOD_2;
2065 STps->drv_block = 0;
2066 if (STps->drv_file >= 0)
2068 } else if (STps->eof == ST_EOD_2)
2070 } else if (STps->eof == ST_FM)
2071 STps->eof = ST_NOEOF;
2075 if (SRpnt != NULL) {
2076 st_release_request(SRpnt);
2080 release_buffering(STp, 1);
2081 STbp->buffer_bytes = 0;
2083 mutex_unlock(&STp->lock);
2091 /* Set the driver options */
2092 static void st_log_options(struct scsi_tape * STp, struct st_modedef * STm, char *name)
2096 "%s: Mode %d options: buffer writes: %d, async writes: %d, read ahead: %d\n",
2097 name, STp->current_mode, STm->do_buffer_writes, STm->do_async_writes,
2098 STm->do_read_ahead);
2100 "%s: can bsr: %d, two FMs: %d, fast mteom: %d, auto lock: %d,\n",
2101 name, STp->can_bsr, STp->two_fm, STp->fast_mteom, STp->do_auto_lock);
2103 "%s: defs for wr: %d, no block limits: %d, partitions: %d, s2 log: %d\n",
2104 name, STm->defaults_for_writes, STp->omit_blklims, STp->can_partitions,
2105 STp->scsi2_logical);
2107 "%s: sysv: %d nowait: %d sili: %d\n", name, STm->sysv, STp->immediate,
2109 printk(KERN_INFO "%s: debugging: %d\n",
2116 static int st_set_options(struct scsi_tape *STp, long options)
2120 struct st_modedef *STm;
2121 char *name = tape_name(STp);
2122 struct cdev *cd0, *cd1;
2124 STm = &(STp->modes[STp->current_mode]);
2125 if (!STm->defined) {
2126 cd0 = STm->cdevs[0]; cd1 = STm->cdevs[1];
2127 memcpy(STm, &(STp->modes[0]), sizeof(struct st_modedef));
2128 STm->cdevs[0] = cd0; STm->cdevs[1] = cd1;
2130 DEBC(printk(ST_DEB_MSG
2131 "%s: Initialized mode %d definition from mode 0\n",
2132 name, STp->current_mode));
2135 code = options & MT_ST_OPTIONS;
2136 if (code == MT_ST_BOOLEANS) {
2137 STm->do_buffer_writes = (options & MT_ST_BUFFER_WRITES) != 0;
2138 STm->do_async_writes = (options & MT_ST_ASYNC_WRITES) != 0;
2139 STm->defaults_for_writes = (options & MT_ST_DEF_WRITES) != 0;
2140 STm->do_read_ahead = (options & MT_ST_READ_AHEAD) != 0;
2141 STp->two_fm = (options & MT_ST_TWO_FM) != 0;
2142 STp->fast_mteom = (options & MT_ST_FAST_MTEOM) != 0;
2143 STp->do_auto_lock = (options & MT_ST_AUTO_LOCK) != 0;
2144 STp->can_bsr = (options & MT_ST_CAN_BSR) != 0;
2145 STp->omit_blklims = (options & MT_ST_NO_BLKLIMS) != 0;
2146 if ((STp->device)->scsi_level >= SCSI_2)
2147 STp->can_partitions = (options & MT_ST_CAN_PARTITIONS) != 0;
2148 STp->scsi2_logical = (options & MT_ST_SCSI2LOGICAL) != 0;
2149 STp->immediate = (options & MT_ST_NOWAIT) != 0;
2150 STm->sysv = (options & MT_ST_SYSV) != 0;
2151 STp->sili = (options & MT_ST_SILI) != 0;
2152 DEB( debugging = (options & MT_ST_DEBUGGING) != 0;
2153 st_log_options(STp, STm, name); )
2154 } else if (code == MT_ST_SETBOOLEANS || code == MT_ST_CLEARBOOLEANS) {
2155 value = (code == MT_ST_SETBOOLEANS);
2156 if ((options & MT_ST_BUFFER_WRITES) != 0)
2157 STm->do_buffer_writes = value;
2158 if ((options & MT_ST_ASYNC_WRITES) != 0)
2159 STm->do_async_writes = value;
2160 if ((options & MT_ST_DEF_WRITES) != 0)
2161 STm->defaults_for_writes = value;
2162 if ((options & MT_ST_READ_AHEAD) != 0)
2163 STm->do_read_ahead = value;
2164 if ((options & MT_ST_TWO_FM) != 0)
2165 STp->two_fm = value;
2166 if ((options & MT_ST_FAST_MTEOM) != 0)
2167 STp->fast_mteom = value;
2168 if ((options & MT_ST_AUTO_LOCK) != 0)
2169 STp->do_auto_lock = value;
2170 if ((options & MT_ST_CAN_BSR) != 0)
2171 STp->can_bsr = value;
2172 if ((options & MT_ST_NO_BLKLIMS) != 0)
2173 STp->omit_blklims = value;
2174 if ((STp->device)->scsi_level >= SCSI_2 &&
2175 (options & MT_ST_CAN_PARTITIONS) != 0)
2176 STp->can_partitions = value;
2177 if ((options & MT_ST_SCSI2LOGICAL) != 0)
2178 STp->scsi2_logical = value;
2179 if ((options & MT_ST_NOWAIT) != 0)
2180 STp->immediate = value;
2181 if ((options & MT_ST_SYSV) != 0)
2183 if ((options & MT_ST_SILI) != 0)
2186 if ((options & MT_ST_DEBUGGING) != 0)
2188 st_log_options(STp, STm, name); )
2189 } else if (code == MT_ST_WRITE_THRESHOLD) {
2190 /* Retained for compatibility */
2191 } else if (code == MT_ST_DEF_BLKSIZE) {
2192 value = (options & ~MT_ST_OPTIONS);
2193 if (value == ~MT_ST_OPTIONS) {
2194 STm->default_blksize = (-1);
2195 DEBC( printk(KERN_INFO "%s: Default block size disabled.\n", name));
2197 STm->default_blksize = value;
2198 DEBC( printk(KERN_INFO "%s: Default block size set to %d bytes.\n",
2199 name, STm->default_blksize));
2200 if (STp->ready == ST_READY) {
2201 STp->blksize_changed = 0;
2202 set_mode_densblk(STp, STm);
2205 } else if (code == MT_ST_TIMEOUTS) {
2206 value = (options & ~MT_ST_OPTIONS);
2207 if ((value & MT_ST_SET_LONG_TIMEOUT) != 0) {
2208 STp->long_timeout = (value & ~MT_ST_SET_LONG_TIMEOUT) * HZ;
2209 DEBC( printk(KERN_INFO "%s: Long timeout set to %d seconds.\n", name,
2210 (value & ~MT_ST_SET_LONG_TIMEOUT)));
2212 STp->device->timeout = value * HZ;
2213 DEBC( printk(KERN_INFO "%s: Normal timeout set to %d seconds.\n",
2216 } else if (code == MT_ST_SET_CLN) {
2217 value = (options & ~MT_ST_OPTIONS) & 0xff;
2219 value < EXTENDED_SENSE_START && value >= SCSI_SENSE_BUFFERSIZE)
2221 STp->cln_mode = value;
2222 STp->cln_sense_mask = (options >> 8) & 0xff;
2223 STp->cln_sense_value = (options >> 16) & 0xff;
2225 "%s: Cleaning request mode %d, mask %02x, value %02x\n",
2226 name, value, STp->cln_sense_mask, STp->cln_sense_value);
2227 } else if (code == MT_ST_DEF_OPTIONS) {
2228 code = (options & ~MT_ST_CLEAR_DEFAULT);
2229 value = (options & MT_ST_CLEAR_DEFAULT);
2230 if (code == MT_ST_DEF_DENSITY) {
2231 if (value == MT_ST_CLEAR_DEFAULT) {
2232 STm->default_density = (-1);
2233 DEBC( printk(KERN_INFO "%s: Density default disabled.\n",
2236 STm->default_density = value & 0xff;
2237 DEBC( printk(KERN_INFO "%s: Density default set to %x\n",
2238 name, STm->default_density));
2239 if (STp->ready == ST_READY) {
2240 STp->density_changed = 0;
2241 set_mode_densblk(STp, STm);
2244 } else if (code == MT_ST_DEF_DRVBUFFER) {
2245 if (value == MT_ST_CLEAR_DEFAULT) {
2246 STp->default_drvbuffer = 0xff;
2247 DEBC( printk(KERN_INFO
2248 "%s: Drive buffer default disabled.\n", name));
2250 STp->default_drvbuffer = value & 7;
2251 DEBC( printk(KERN_INFO
2252 "%s: Drive buffer default set to %x\n",
2253 name, STp->default_drvbuffer));
2254 if (STp->ready == ST_READY)
2255 st_int_ioctl(STp, MTSETDRVBUFFER, STp->default_drvbuffer);
2257 } else if (code == MT_ST_DEF_COMPRESSION) {
2258 if (value == MT_ST_CLEAR_DEFAULT) {
2259 STm->default_compression = ST_DONT_TOUCH;
2260 DEBC( printk(KERN_INFO
2261 "%s: Compression default disabled.\n", name));
2263 if ((value & 0xff00) != 0) {
2264 STp->c_algo = (value & 0xff00) >> 8;
2265 DEBC( printk(KERN_INFO "%s: Compression algorithm set to 0x%x.\n",
2266 name, STp->c_algo));
2268 if ((value & 0xff) != 0xff) {
2269 STm->default_compression = (value & 1 ? ST_YES : ST_NO);
2270 DEBC( printk(KERN_INFO "%s: Compression default set to %x\n",
2271 name, (value & 1)));
2272 if (STp->ready == ST_READY) {
2273 STp->compression_changed = 0;
2274 st_compression(STp, (STm->default_compression == ST_YES));
2285 #define MODE_HEADER_LENGTH 4
2287 /* Mode header and page byte offsets */
2288 #define MH_OFF_DATA_LENGTH 0
2289 #define MH_OFF_MEDIUM_TYPE 1
2290 #define MH_OFF_DEV_SPECIFIC 2
2291 #define MH_OFF_BDESCS_LENGTH 3
2292 #define MP_OFF_PAGE_NBR 0
2293 #define MP_OFF_PAGE_LENGTH 1
2295 /* Mode header and page bit masks */
2296 #define MH_BIT_WP 0x80
2297 #define MP_MSK_PAGE_NBR 0x3f
2299 /* Don't return block descriptors */
2300 #define MODE_SENSE_OMIT_BDESCS 0x08
2302 #define MODE_SELECT_PAGE_FORMAT 0x10
2304 /* Read a mode page into the tape buffer. The block descriptors are included
2305 if incl_block_descs is true. The page control is ored to the page number
2306 parameter, if necessary. */
2307 static int read_mode_page(struct scsi_tape *STp, int page, int omit_block_descs)
2309 unsigned char cmd[MAX_COMMAND_SIZE];
2310 struct st_request *SRpnt = NULL;
2312 memset(cmd, 0, MAX_COMMAND_SIZE);
2313 cmd[0] = MODE_SENSE;
2314 if (omit_block_descs)
2315 cmd[1] = MODE_SENSE_OMIT_BDESCS;
2319 SRpnt = st_do_scsi(SRpnt, STp, cmd, cmd[4], DMA_FROM_DEVICE,
2320 STp->device->timeout, 0, 1);
2322 return (STp->buffer)->syscall_result;
2324 st_release_request(SRpnt);
2326 return (STp->buffer)->syscall_result;
2330 /* Send the mode page in the tape buffer to the drive. Assumes that the mode data
2331 in the buffer is correctly formatted. The long timeout is used if slow is non-zero. */
2332 static int write_mode_page(struct scsi_tape *STp, int page, int slow)
2335 unsigned char cmd[MAX_COMMAND_SIZE];
2336 struct st_request *SRpnt = NULL;
2338 memset(cmd, 0, MAX_COMMAND_SIZE);
2339 cmd[0] = MODE_SELECT;
2340 cmd[1] = MODE_SELECT_PAGE_FORMAT;
2341 pgo = MODE_HEADER_LENGTH + (STp->buffer)->b_data[MH_OFF_BDESCS_LENGTH];
2342 cmd[4] = pgo + (STp->buffer)->b_data[pgo + MP_OFF_PAGE_LENGTH] + 2;
2344 /* Clear reserved fields */
2345 (STp->buffer)->b_data[MH_OFF_DATA_LENGTH] = 0;
2346 (STp->buffer)->b_data[MH_OFF_MEDIUM_TYPE] = 0;
2347 (STp->buffer)->b_data[MH_OFF_DEV_SPECIFIC] &= ~MH_BIT_WP;
2348 (STp->buffer)->b_data[pgo + MP_OFF_PAGE_NBR] &= MP_MSK_PAGE_NBR;
2350 SRpnt = st_do_scsi(SRpnt, STp, cmd, cmd[4], DMA_TO_DEVICE,
2351 (slow ? STp->long_timeout : STp->device->timeout), 0, 1);
2353 return (STp->buffer)->syscall_result;
2355 st_release_request(SRpnt);
2357 return (STp->buffer)->syscall_result;
2361 #define COMPRESSION_PAGE 0x0f
2362 #define COMPRESSION_PAGE_LENGTH 16
2364 #define CP_OFF_DCE_DCC 2
2365 #define CP_OFF_C_ALGO 7
2367 #define DCE_MASK 0x80
2368 #define DCC_MASK 0x40
2369 #define RED_MASK 0x60
2372 /* Control the compression with mode page 15. Algorithm not changed if zero.
2374 The block descriptors are read and written because Sony SDT-7000 does not
2375 work without this (suggestion from Michael Schaefer <Michael.Schaefer@dlr.de>).
2376 Including block descriptors should not cause any harm to other drives. */
2378 static int st_compression(struct scsi_tape * STp, int state)
2381 int mpoffs; /* Offset to mode page start */
2382 unsigned char *b_data = (STp->buffer)->b_data;
2383 DEB( char *name = tape_name(STp); )
2385 if (STp->ready != ST_READY)
2388 /* Read the current page contents */
2389 retval = read_mode_page(STp, COMPRESSION_PAGE, 0);
2391 DEBC(printk(ST_DEB_MSG "%s: Compression mode page not supported.\n",
2396 mpoffs = MODE_HEADER_LENGTH + b_data[MH_OFF_BDESCS_LENGTH];
2397 DEBC(printk(ST_DEB_MSG "%s: Compression state is %d.\n", name,
2398 (b_data[mpoffs + CP_OFF_DCE_DCC] & DCE_MASK ? 1 : 0)));
2400 /* Check if compression can be changed */
2401 if ((b_data[mpoffs + CP_OFF_DCE_DCC] & DCC_MASK) == 0) {
2402 DEBC(printk(ST_DEB_MSG "%s: Compression not supported.\n", name));
2408 b_data[mpoffs + CP_OFF_DCE_DCC] |= DCE_MASK;
2409 if (STp->c_algo != 0)
2410 b_data[mpoffs + CP_OFF_C_ALGO] = STp->c_algo;
2413 b_data[mpoffs + CP_OFF_DCE_DCC] &= ~DCE_MASK;
2414 if (STp->c_algo != 0)
2415 b_data[mpoffs + CP_OFF_C_ALGO] = 0; /* no compression */
2418 retval = write_mode_page(STp, COMPRESSION_PAGE, 0);
2420 DEBC(printk(ST_DEB_MSG "%s: Compression change failed.\n", name));
2423 DEBC(printk(ST_DEB_MSG "%s: Compression state changed to %d.\n",
2426 STp->compression_changed = 1;
2431 /* Process the load and unload commands (does unload if the load code is zero) */
2432 static int do_load_unload(struct scsi_tape *STp, struct file *filp, int load_code)
2434 int retval = (-EIO), timeout;
2435 DEB( char *name = tape_name(STp); )
2436 unsigned char cmd[MAX_COMMAND_SIZE];
2437 struct st_partstat *STps;
2438 struct st_request *SRpnt;
2440 if (STp->ready != ST_READY && !load_code) {
2441 if (STp->ready == ST_NO_TAPE)
2442 return (-ENOMEDIUM);
2447 memset(cmd, 0, MAX_COMMAND_SIZE);
2448 cmd[0] = START_STOP;
2452 * If arg >= 1 && arg <= 6 Enhanced load/unload in HP C1553A
2454 if (load_code >= 1 + MT_ST_HPLOADER_OFFSET
2455 && load_code <= 6 + MT_ST_HPLOADER_OFFSET) {
2456 DEBC(printk(ST_DEB_MSG "%s: Enhanced %sload slot %2d.\n",
2457 name, (cmd[4]) ? "" : "un",
2458 load_code - MT_ST_HPLOADER_OFFSET));
2459 cmd[3] = load_code - MT_ST_HPLOADER_OFFSET; /* MediaID field of C1553A */
2461 if (STp->immediate) {
2462 cmd[1] = 1; /* Don't wait for completion */
2463 timeout = STp->device->timeout;
2466 timeout = STp->long_timeout;
2470 printk(ST_DEB_MSG "%s: Unloading tape.\n", name);
2472 printk(ST_DEB_MSG "%s: Loading tape.\n", name);
2475 SRpnt = st_do_scsi(NULL, STp, cmd, 0, DMA_NONE,
2476 timeout, MAX_RETRIES, 1);
2478 return (STp->buffer)->syscall_result;
2480 retval = (STp->buffer)->syscall_result;
2481 st_release_request(SRpnt);
2483 if (!retval) { /* SCSI command successful */
2486 STp->rew_at_close = 0;
2487 STp->ready = ST_NO_TAPE;
2490 STp->rew_at_close = STp->autorew_dev;
2491 retval = check_tape(STp, filp);
2497 STps = &(STp->ps[STp->partition]);
2498 STps->drv_file = STps->drv_block = (-1);
2505 #define ST_DEB_FORWARD 0
2506 #define ST_DEB_BACKWARD 1
2507 static void deb_space_print(char *name, int direction, char *units, unsigned char *cmd)
2511 sc = cmd[2] & 0x80 ? 0xff000000 : 0;
2512 sc |= (cmd[2] << 16) | (cmd[3] << 8) | cmd[4];
2515 printk(ST_DEB_MSG "%s: Spacing tape %s over %d %s.\n", name,
2516 direction ? "backward" : "forward", sc, units);
2521 /* Internal ioctl function */
2522 static int st_int_ioctl(struct scsi_tape *STp, unsigned int cmd_in, unsigned long arg)
2528 unsigned char cmd[MAX_COMMAND_SIZE];
2529 struct st_request *SRpnt;
2530 struct st_partstat *STps;
2531 int fileno, blkno, at_sm, undone;
2532 int datalen = 0, direction = DMA_NONE;
2533 char *name = tape_name(STp);
2535 WARN_ON(STp->buffer->do_dio != 0);
2536 if (STp->ready != ST_READY) {
2537 if (STp->ready == ST_NO_TAPE)
2538 return (-ENOMEDIUM);
2542 timeout = STp->long_timeout;
2543 STps = &(STp->ps[STp->partition]);
2544 fileno = STps->drv_file;
2545 blkno = STps->drv_block;
2546 at_sm = STps->at_sm;
2548 memset(cmd, 0, MAX_COMMAND_SIZE);
2551 chg_eof = 0; /* Changed from the FSF after this */
2554 cmd[1] = 0x01; /* Space FileMarks */
2555 cmd[2] = (arg >> 16);
2556 cmd[3] = (arg >> 8);
2558 DEBC(deb_space_print(name, ST_DEB_FORWARD, "filemarks", cmd);)
2562 at_sm &= (arg == 0);
2565 chg_eof = 0; /* Changed from the FSF after this */
2568 cmd[1] = 0x01; /* Space FileMarks */
2570 cmd[2] = (ltmp >> 16);
2571 cmd[3] = (ltmp >> 8);
2573 DEBC(deb_space_print(name, ST_DEB_BACKWARD, "filemarks", cmd);)
2576 blkno = (-1); /* We can't know the block number */
2577 at_sm &= (arg == 0);
2581 cmd[1] = 0x00; /* Space Blocks */
2582 cmd[2] = (arg >> 16);
2583 cmd[3] = (arg >> 8);
2585 DEBC(deb_space_print(name, ST_DEB_FORWARD, "blocks", cmd);)
2588 at_sm &= (arg == 0);
2592 cmd[1] = 0x00; /* Space Blocks */
2594 cmd[2] = (ltmp >> 16);
2595 cmd[3] = (ltmp >> 8);
2597 DEBC(deb_space_print(name, ST_DEB_BACKWARD, "blocks", cmd);)
2600 at_sm &= (arg == 0);
2604 cmd[1] = 0x04; /* Space Setmarks */
2605 cmd[2] = (arg >> 16);
2606 cmd[3] = (arg >> 8);
2608 DEBC(deb_space_print(name, ST_DEB_FORWARD, "setmarks", cmd);)
2610 blkno = fileno = (-1);
2616 cmd[1] = 0x04; /* Space Setmarks */
2618 cmd[2] = (ltmp >> 16);
2619 cmd[3] = (ltmp >> 8);
2621 DEBC(deb_space_print(name, ST_DEB_BACKWARD, "setmarks", cmd);)
2623 blkno = fileno = (-1);
2629 if (STp->write_prot)
2631 cmd[0] = WRITE_FILEMARKS;
2632 if (cmd_in == MTWSM)
2634 cmd[2] = (arg >> 16);
2635 cmd[3] = (arg >> 8);
2637 timeout = STp->device->timeout;
2639 if (cmd_in == MTWEOF)
2640 printk(ST_DEB_MSG "%s: Writing %d filemarks.\n", name,
2641 cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
2643 printk(ST_DEB_MSG "%s: Writing %d setmarks.\n", name,
2644 cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
2649 at_sm = (cmd_in == MTWSM);
2652 cmd[0] = REZERO_UNIT;
2653 if (STp->immediate) {
2654 cmd[1] = 1; /* Don't wait for completion */
2655 timeout = STp->device->timeout;
2657 DEBC(printk(ST_DEB_MSG "%s: Rewinding tape.\n", name));
2658 fileno = blkno = at_sm = 0;
2661 DEBC(printk(ST_DEB_MSG "%s: No op on tape.\n", name));
2662 return 0; /* Should do something ? */
2665 cmd[0] = START_STOP;
2666 if (STp->immediate) {
2667 cmd[1] = 1; /* Don't wait for completion */
2668 timeout = STp->device->timeout;
2671 DEBC(printk(ST_DEB_MSG "%s: Retensioning tape.\n", name));
2672 fileno = blkno = at_sm = 0;
2675 if (!STp->fast_mteom) {
2676 /* space to the end of tape */
2677 ioctl_result = st_int_ioctl(STp, MTFSF, 0x7fffff);
2678 fileno = STps->drv_file;
2679 if (STps->eof >= ST_EOD_1)
2681 /* The next lines would hide the number of spaced FileMarks
2682 That's why I inserted the previous lines. I had no luck
2683 with detecting EOM with FSF, so we go now to EOM.
2689 DEBC(printk(ST_DEB_MSG "%s: Spacing to end of recorded medium.\n",
2695 if (STp->write_prot)
2698 cmd[1] = (arg ? 1 : 0); /* Long erase with non-zero argument */
2699 if (STp->immediate) {
2700 cmd[1] |= 2; /* Don't wait for completion */
2701 timeout = STp->device->timeout;
2704 timeout = STp->long_timeout * 8;
2706 DEBC(printk(ST_DEB_MSG "%s: Erasing tape.\n", name));
2707 fileno = blkno = at_sm = 0;
2709 case MTSETBLK: /* Set block length */
2710 case MTSETDENSITY: /* Set tape density */
2711 case MTSETDRVBUFFER: /* Set drive buffering */
2712 case SET_DENS_AND_BLK: /* Set density and block size */
2714 if (STp->dirty || (STp->buffer)->buffer_bytes != 0)
2715 return (-EIO); /* Not allowed if data in buffer */
2716 if ((cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) &&
2717 (arg & MT_ST_BLKSIZE_MASK) != 0 &&
2718 STp->max_block > 0 &&
2719 ((arg & MT_ST_BLKSIZE_MASK) < STp->min_block ||
2720 (arg & MT_ST_BLKSIZE_MASK) > STp->max_block)) {
2721 printk(KERN_WARNING "%s: Illegal block size.\n", name);
2724 cmd[0] = MODE_SELECT;
2725 if ((STp->use_pf & USE_PF))
2726 cmd[1] = MODE_SELECT_PAGE_FORMAT;
2727 cmd[4] = datalen = 12;
2728 direction = DMA_TO_DEVICE;
2730 memset((STp->buffer)->b_data, 0, 12);
2731 if (cmd_in == MTSETDRVBUFFER)
2732 (STp->buffer)->b_data[2] = (arg & 7) << 4;
2734 (STp->buffer)->b_data[2] =
2735 STp->drv_buffer << 4;
2736 (STp->buffer)->b_data[3] = 8; /* block descriptor length */
2737 if (cmd_in == MTSETDENSITY) {
2738 (STp->buffer)->b_data[4] = arg;
2739 STp->density_changed = 1; /* At least we tried ;-) */
2740 } else if (cmd_in == SET_DENS_AND_BLK)
2741 (STp->buffer)->b_data[4] = arg >> 24;
2743 (STp->buffer)->b_data[4] = STp->density;
2744 if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) {
2745 ltmp = arg & MT_ST_BLKSIZE_MASK;
2746 if (cmd_in == MTSETBLK)
2747 STp->blksize_changed = 1; /* At least we tried ;-) */
2749 ltmp = STp->block_size;
2750 (STp->buffer)->b_data[9] = (ltmp >> 16);
2751 (STp->buffer)->b_data[10] = (ltmp >> 8);
2752 (STp->buffer)->b_data[11] = ltmp;
2753 timeout = STp->device->timeout;
2755 if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK)
2757 "%s: Setting block size to %d bytes.\n", name,
2758 (STp->buffer)->b_data[9] * 65536 +
2759 (STp->buffer)->b_data[10] * 256 +
2760 (STp->buffer)->b_data[11]);
2761 if (cmd_in == MTSETDENSITY || cmd_in == SET_DENS_AND_BLK)
2763 "%s: Setting density code to %x.\n", name,
2764 (STp->buffer)->b_data[4]);
2765 if (cmd_in == MTSETDRVBUFFER)
2767 "%s: Setting drive buffer code to %d.\n", name,
2768 ((STp->buffer)->b_data[2] >> 4) & 7);
2775 SRpnt = st_do_scsi(NULL, STp, cmd, datalen, direction,
2776 timeout, MAX_RETRIES, 1);
2778 return (STp->buffer)->syscall_result;
2780 ioctl_result = (STp->buffer)->syscall_result;
2782 if (!ioctl_result) { /* SCSI command successful */
2783 st_release_request(SRpnt);
2785 STps->drv_block = blkno;
2786 STps->drv_file = fileno;
2787 STps->at_sm = at_sm;
2789 if (cmd_in == MTBSFM)
2790 ioctl_result = st_int_ioctl(STp, MTFSF, 1);
2791 else if (cmd_in == MTFSFM)
2792 ioctl_result = st_int_ioctl(STp, MTBSF, 1);
2794 if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) {
2795 int old_block_size = STp->block_size;
2796 STp->block_size = arg & MT_ST_BLKSIZE_MASK;
2797 if (STp->block_size != 0) {
2798 if (old_block_size == 0)
2799 normalize_buffer(STp->buffer);
2800 (STp->buffer)->buffer_blocks =
2801 (STp->buffer)->buffer_size / STp->block_size;
2803 (STp->buffer)->buffer_bytes = (STp->buffer)->read_pointer = 0;
2804 if (cmd_in == SET_DENS_AND_BLK)
2805 STp->density = arg >> MT_ST_DENSITY_SHIFT;
2806 } else if (cmd_in == MTSETDRVBUFFER)
2807 STp->drv_buffer = (arg & 7);
2808 else if (cmd_in == MTSETDENSITY)
2811 if (cmd_in == MTEOM)
2813 else if (cmd_in == MTFSF)
2816 STps->eof = ST_NOEOF;
2818 if (cmd_in == MTWEOF)
2820 } else { /* SCSI command was not completely successful. Don't return
2821 from this block without releasing the SCSI command block! */
2822 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
2824 if (cmdstatp->flags & SENSE_EOM) {
2825 if (cmd_in != MTBSF && cmd_in != MTBSFM &&
2826 cmd_in != MTBSR && cmd_in != MTBSS)
2827 STps->eof = ST_EOM_OK;
2828 STps->drv_block = 0;
2831 if (cmdstatp->remainder_valid)
2832 undone = (int)cmdstatp->uremainder64;
2836 if (cmd_in == MTWEOF &&
2837 cmdstatp->have_sense &&
2838 (cmdstatp->flags & SENSE_EOM)) {
2839 if (cmdstatp->sense_hdr.sense_key == NO_SENSE ||
2840 cmdstatp->sense_hdr.sense_key == RECOVERED_ERROR) {
2841 ioctl_result = 0; /* EOF(s) written successfully at EOM */
2842 STps->eof = ST_NOEOF;
2843 } else { /* Writing EOF(s) failed */
2847 STps->eof = ST_NOEOF;
2849 STps->drv_file = fileno;
2850 } else if ((cmd_in == MTFSF) || (cmd_in == MTFSFM)) {
2852 STps->drv_file = fileno - undone;
2854 STps->drv_file = fileno;
2855 STps->drv_block = -1;
2856 STps->eof = ST_NOEOF;
2857 } else if ((cmd_in == MTBSF) || (cmd_in == MTBSFM)) {
2858 if (arg > 0 && undone < 0) /* Some drives get this wrong */
2860 if (STps->drv_file >= 0)
2861 STps->drv_file = fileno + undone;
2862 STps->drv_block = 0;
2863 STps->eof = ST_NOEOF;
2864 } else if (cmd_in == MTFSR) {
2865 if (cmdstatp->flags & SENSE_FMK) { /* Hit filemark */
2866 if (STps->drv_file >= 0)
2868 STps->drv_block = 0;
2871 if (blkno >= undone)
2872 STps->drv_block = blkno - undone;
2874 STps->drv_block = (-1);
2875 STps->eof = ST_NOEOF;
2877 } else if (cmd_in == MTBSR) {
2878 if (cmdstatp->flags & SENSE_FMK) { /* Hit filemark */
2880 STps->drv_block = (-1);
2882 if (arg > 0 && undone < 0) /* Some drives get this wrong */
2884 if (STps->drv_block >= 0)
2885 STps->drv_block = blkno + undone;
2887 STps->eof = ST_NOEOF;
2888 } else if (cmd_in == MTEOM) {
2889 STps->drv_file = (-1);
2890 STps->drv_block = (-1);
2892 } else if (cmd_in == MTSETBLK ||
2893 cmd_in == MTSETDENSITY ||
2894 cmd_in == MTSETDRVBUFFER ||
2895 cmd_in == SET_DENS_AND_BLK) {
2896 if (cmdstatp->sense_hdr.sense_key == ILLEGAL_REQUEST &&
2897 !(STp->use_pf & PF_TESTED)) {
2898 /* Try the other possible state of Page Format if not
2900 STp->use_pf = !STp->use_pf | PF_TESTED;
2901 st_release_request(SRpnt);
2903 return st_int_ioctl(STp, cmd_in, arg);
2906 STps->eof = ST_NOEOF;
2908 if (cmdstatp->sense_hdr.sense_key == BLANK_CHECK)
2911 st_release_request(SRpnt);
2915 return ioctl_result;
2919 /* Get the tape position. If bt == 2, arg points into a kernel space mt_loc
2922 static int get_location(struct scsi_tape *STp, unsigned int *block, int *partition,
2926 unsigned char scmd[MAX_COMMAND_SIZE];
2927 struct st_request *SRpnt;
2928 DEB( char *name = tape_name(STp); )
2930 if (STp->ready != ST_READY)
2933 memset(scmd, 0, MAX_COMMAND_SIZE);
2934 if ((STp->device)->scsi_level < SCSI_2) {
2935 scmd[0] = QFA_REQUEST_BLOCK;
2938 scmd[0] = READ_POSITION;
2939 if (!logical && !STp->scsi2_logical)
2942 SRpnt = st_do_scsi(NULL, STp, scmd, 20, DMA_FROM_DEVICE,
2943 STp->device->timeout, MAX_READY_RETRIES, 1);
2945 return (STp->buffer)->syscall_result;
2947 if ((STp->buffer)->syscall_result != 0 ||
2948 (STp->device->scsi_level >= SCSI_2 &&
2949 ((STp->buffer)->b_data[0] & 4) != 0)) {
2950 *block = *partition = 0;
2951 DEBC(printk(ST_DEB_MSG "%s: Can't read tape position.\n", name));
2955 if ((STp->device)->scsi_level < SCSI_2) {
2956 *block = ((STp->buffer)->b_data[0] << 16)
2957 + ((STp->buffer)->b_data[1] << 8)
2958 + (STp->buffer)->b_data[2];
2961 *block = ((STp->buffer)->b_data[4] << 24)
2962 + ((STp->buffer)->b_data[5] << 16)
2963 + ((STp->buffer)->b_data[6] << 8)
2964 + (STp->buffer)->b_data[7];
2965 *partition = (STp->buffer)->b_data[1];
2966 if (((STp->buffer)->b_data[0] & 0x80) &&
2967 (STp->buffer)->b_data[1] == 0) /* BOP of partition 0 */
2968 STp->ps[0].drv_block = STp->ps[0].drv_file = 0;
2970 DEBC(printk(ST_DEB_MSG "%s: Got tape pos. blk %d part %d.\n", name,
2971 *block, *partition));
2973 st_release_request(SRpnt);
2980 /* Set the tape block and partition. Negative partition means that only the
2981 block should be set in vendor specific way. */
2982 static int set_location(struct scsi_tape *STp, unsigned int block, int partition,
2985 struct st_partstat *STps;
2989 unsigned char scmd[MAX_COMMAND_SIZE];
2990 struct st_request *SRpnt;
2991 DEB( char *name = tape_name(STp); )
2993 if (STp->ready != ST_READY)
2995 timeout = STp->long_timeout;
2996 STps = &(STp->ps[STp->partition]);
2998 DEBC(printk(ST_DEB_MSG "%s: Setting block to %d and partition to %d.\n",
2999 name, block, partition));
3000 DEB(if (partition < 0)
3003 /* Update the location at the partition we are leaving */
3004 if ((!STp->can_partitions && partition != 0) ||
3005 partition >= ST_NBR_PARTITIONS)
3007 if (partition != STp->partition) {
3008 if (get_location(STp, &blk, &p, 1))
3009 STps->last_block_valid = 0;
3011 STps->last_block_valid = 1;
3012 STps->last_block_visited = blk;
3013 DEBC(printk(ST_DEB_MSG
3014 "%s: Visited block %d for partition %d saved.\n",
3015 name, blk, STp->partition));
3019 memset(scmd, 0, MAX_COMMAND_SIZE);
3020 if ((STp->device)->scsi_level < SCSI_2) {
3021 scmd[0] = QFA_SEEK_BLOCK;
3022 scmd[2] = (block >> 16);
3023 scmd[3] = (block >> 8);
3028 scmd[3] = (block >> 24);
3029 scmd[4] = (block >> 16);
3030 scmd[5] = (block >> 8);
3032 if (!logical && !STp->scsi2_logical)
3034 if (STp->partition != partition) {
3036 scmd[8] = partition;
3037 DEBC(printk(ST_DEB_MSG
3038 "%s: Trying to change partition from %d to %d\n",
3039 name, STp->partition, partition));
3042 if (STp->immediate) {
3043 scmd[1] |= 1; /* Don't wait for completion */
3044 timeout = STp->device->timeout;
3047 SRpnt = st_do_scsi(NULL, STp, scmd, 0, DMA_NONE,
3048 timeout, MAX_READY_RETRIES, 1);
3050 return (STp->buffer)->syscall_result;
3052 STps->drv_block = STps->drv_file = (-1);
3053 STps->eof = ST_NOEOF;
3054 if ((STp->buffer)->syscall_result != 0) {
3056 if (STp->can_partitions &&
3057 (STp->device)->scsi_level >= SCSI_2 &&
3058 (p = find_partition(STp)) >= 0)
3061 if (STp->can_partitions) {
3062 STp->partition = partition;
3063 STps = &(STp->ps[partition]);
3064 if (!STps->last_block_valid ||
3065 STps->last_block_visited != block) {
3072 STps->drv_block = STps->drv_file = 0;
3076 st_release_request(SRpnt);
3083 /* Find the current partition number for the drive status. Called from open and
3084 returns either partition number of negative error code. */
3085 static int find_partition(struct scsi_tape *STp)
3090 if ((i = get_location(STp, &block, &partition, 1)) < 0)
3092 if (partition >= ST_NBR_PARTITIONS)
3098 /* Change the partition if necessary */
3099 static int switch_partition(struct scsi_tape *STp)
3101 struct st_partstat *STps;
3103 if (STp->partition == STp->new_partition)
3105 STps = &(STp->ps[STp->new_partition]);
3106 if (!STps->last_block_valid)
3107 STps->last_block_visited = 0;
3108 return set_location(STp, STps->last_block_visited, STp->new_partition, 1);
3111 /* Functions for reading and writing the medium partition mode page. */
3113 #define PART_PAGE 0x11
3114 #define PART_PAGE_FIXED_LENGTH 8
3116 #define PP_OFF_MAX_ADD_PARTS 2
3117 #define PP_OFF_NBR_ADD_PARTS 3
3118 #define PP_OFF_FLAGS 4
3119 #define PP_OFF_PART_UNITS 6
3120 #define PP_OFF_RESERVED 7
3122 #define PP_BIT_IDP 0x20
3123 #define PP_MSK_PSUM_MB 0x10
3125 /* Get the number of partitions on the tape. As a side effect reads the
3126 mode page into the tape buffer. */
3127 static int nbr_partitions(struct scsi_tape *STp)
3130 DEB( char *name = tape_name(STp); )
3132 if (STp->ready != ST_READY)
3135 result = read_mode_page(STp, PART_PAGE, 1);
3138 DEBC(printk(ST_DEB_MSG "%s: Can't read medium partition page.\n",
3142 result = (STp->buffer)->b_data[MODE_HEADER_LENGTH +
3143 PP_OFF_NBR_ADD_PARTS] + 1;
3144 DEBC(printk(ST_DEB_MSG "%s: Number of partitions %d.\n", name, result));
3151 /* Partition the tape into two partitions if size > 0 or one partition if
3154 The block descriptors are read and written because Sony SDT-7000 does not
3155 work without this (suggestion from Michael Schaefer <Michael.Schaefer@dlr.de>).
3157 My HP C1533A drive returns only one partition size field. This is used to
3158 set the size of partition 1. There is no size field for the default partition.
3159 Michael Schaefer's Sony SDT-7000 returns two descriptors and the second is
3160 used to set the size of partition 1 (this is what the SCSI-3 standard specifies).
3161 The following algorithm is used to accommodate both drives: if the number of
3162 partition size fields is greater than the maximum number of additional partitions
3163 in the mode page, the second field is used. Otherwise the first field is used.
3165 For Seagate DDS drives the page length must be 8 when no partitions is defined
3166 and 10 when 1 partition is defined (information from Eric Lee Green). This is
3167 is acceptable also to some other old drives and enforced if the first partition
3168 size field is used for the first additional partition size.
3170 static int partition_tape(struct scsi_tape *STp, int size)
3172 char *name = tape_name(STp);
3174 int pgo, psd_cnt, psdo;
3177 result = read_mode_page(STp, PART_PAGE, 0);
3179 DEBC(printk(ST_DEB_MSG "%s: Can't read partition mode page.\n", name));
3182 /* The mode page is in the buffer. Let's modify it and write it. */
3183 bp = (STp->buffer)->b_data;
3184 pgo = MODE_HEADER_LENGTH + bp[MH_OFF_BDESCS_LENGTH];
3185 DEBC(printk(ST_DEB_MSG "%s: Partition page length is %d bytes.\n",
3186 name, bp[pgo + MP_OFF_PAGE_LENGTH] + 2));
3188 psd_cnt = (bp[pgo + MP_OFF_PAGE_LENGTH] + 2 - PART_PAGE_FIXED_LENGTH) / 2;
3189 psdo = pgo + PART_PAGE_FIXED_LENGTH;
3190 if (psd_cnt > bp[pgo + PP_OFF_MAX_ADD_PARTS]) {
3191 bp[psdo] = bp[psdo + 1] = 0xff; /* Rest of the tape */
3194 memset(bp + psdo, 0, bp[pgo + PP_OFF_NBR_ADD_PARTS] * 2);
3196 DEBC(printk("%s: psd_cnt %d, max.parts %d, nbr_parts %d\n", name,
3197 psd_cnt, bp[pgo + PP_OFF_MAX_ADD_PARTS],
3198 bp[pgo + PP_OFF_NBR_ADD_PARTS]));
3201 bp[pgo + PP_OFF_NBR_ADD_PARTS] = 0;
3202 if (psd_cnt <= bp[pgo + PP_OFF_MAX_ADD_PARTS])
3203 bp[pgo + MP_OFF_PAGE_LENGTH] = 6;
3204 DEBC(printk(ST_DEB_MSG "%s: Formatting tape with one partition.\n",
3207 bp[psdo] = (size >> 8) & 0xff;
3208 bp[psdo + 1] = size & 0xff;
3210 if (bp[pgo + MP_OFF_PAGE_LENGTH] < 8)
3211 bp[pgo + MP_OFF_PAGE_LENGTH] = 8;
3212 DEBC(printk(ST_DEB_MSG
3213 "%s: Formatting tape with two partitions (1 = %d MB).\n",
3216 bp[pgo + PP_OFF_PART_UNITS] = 0;
3217 bp[pgo + PP_OFF_RESERVED] = 0;
3218 bp[pgo + PP_OFF_FLAGS] = PP_BIT_IDP | PP_MSK_PSUM_MB;
3220 result = write_mode_page(STp, PART_PAGE, 1);
3222 printk(KERN_INFO "%s: Partitioning of tape failed.\n", name);
3231 /* The ioctl command */
3232 static long st_ioctl(struct file *file, unsigned int cmd_in, unsigned long arg)
3234 int i, cmd_nr, cmd_type, bt;
3237 struct scsi_tape *STp = file->private_data;
3238 struct st_modedef *STm;
3239 struct st_partstat *STps;
3240 char *name = tape_name(STp);
3241 void __user *p = (void __user *)arg;
3243 if (mutex_lock_interruptible(&STp->lock))
3244 return -ERESTARTSYS;
3247 if (debugging && !STp->in_use) {
3248 printk(ST_DEB_MSG "%s: Incorrect device.\n", name);
3253 STm = &(STp->modes[STp->current_mode]);
3254 STps = &(STp->ps[STp->partition]);
3257 * If we are in the middle of error recovery, don't let anyone
3258 * else try and use this device. Also, if error recovery fails, it
3259 * may try and take the device offline, in which case all further
3260 * access to the device is prohibited.
3262 retval = scsi_nonblockable_ioctl(STp->device, cmd_in, p, file);
3263 if (!scsi_block_when_processing_errors(STp->device) || retval != -ENODEV)
3267 cmd_type = _IOC_TYPE(cmd_in);
3268 cmd_nr = _IOC_NR(cmd_in);
3270 if (cmd_type == _IOC_TYPE(MTIOCTOP) && cmd_nr == _IOC_NR(MTIOCTOP)) {
3273 if (_IOC_SIZE(cmd_in) != sizeof(mtc)) {
3278 i = copy_from_user(&mtc, p, sizeof(struct mtop));
3284 if (mtc.mt_op == MTSETDRVBUFFER && !capable(CAP_SYS_ADMIN)) {
3286 "%s: MTSETDRVBUFFER only allowed for root.\n", name);
3290 if (!STm->defined &&
3291 (mtc.mt_op != MTSETDRVBUFFER &&
3292 (mtc.mt_count & MT_ST_OPTIONS) == 0)) {
3297 if (!STp->pos_unknown) {
3299 if (STps->eof == ST_FM_HIT) {
3300 if (mtc.mt_op == MTFSF || mtc.mt_op == MTFSFM ||
3301 mtc.mt_op == MTEOM) {
3303 if (STps->drv_file >= 0)
3304 STps->drv_file += 1;
3305 } else if (mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM) {
3307 if (STps->drv_file >= 0)
3308 STps->drv_file += 1;
3312 if (mtc.mt_op == MTSEEK) {
3313 /* Old position must be restored if partition will be
3315 i = !STp->can_partitions ||
3316 (STp->new_partition != STp->partition);
3318 i = mtc.mt_op == MTREW || mtc.mt_op == MTOFFL ||
3319 mtc.mt_op == MTRETEN || mtc.mt_op == MTEOM ||
3320 mtc.mt_op == MTLOCK || mtc.mt_op == MTLOAD ||
3321 mtc.mt_op == MTFSF || mtc.mt_op == MTFSFM ||
3322 mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM ||
3323 mtc.mt_op == MTCOMPRESSION;
3325 i = flush_buffer(STp, i);
3330 if (STps->rw == ST_WRITING &&
3331 (mtc.mt_op == MTREW || mtc.mt_op == MTOFFL ||
3332 mtc.mt_op == MTSEEK ||
3333 mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM)) {
3334 i = st_int_ioctl(STp, MTWEOF, 1);
3339 if (mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM)
3346 * If there was a bus reset, block further access
3347 * to this device. If the user wants to rewind the tape,
3348 * then reset the flag and allow access again.
3350 if (mtc.mt_op != MTREW &&
3351 mtc.mt_op != MTOFFL &&
3352 mtc.mt_op != MTRETEN &&
3353 mtc.mt_op != MTERASE &&
3354 mtc.mt_op != MTSEEK &&
3355 mtc.mt_op != MTEOM) {
3360 /* remove this when the midlevel properly clears was_reset */
3361 STp->device->was_reset = 0;
3364 if (mtc.mt_op != MTNOP && mtc.mt_op != MTSETBLK &&
3365 mtc.mt_op != MTSETDENSITY && mtc.mt_op != MTWSM &&
3366 mtc.mt_op != MTSETDRVBUFFER && mtc.mt_op != MTSETPART)
3367 STps->rw = ST_IDLE; /* Prevent automatic WEOF and fsf */
3369 if (mtc.mt_op == MTOFFL && STp->door_locked != ST_UNLOCKED)
3370 do_door_lock(STp, 0); /* Ignore result! */
3372 if (mtc.mt_op == MTSETDRVBUFFER &&
3373 (mtc.mt_count & MT_ST_OPTIONS) != 0) {
3374 retval = st_set_options(STp, mtc.mt_count);
3378 if (mtc.mt_op == MTSETPART) {
3379 if (!STp->can_partitions ||
3380 mtc.mt_count < 0 || mtc.mt_count >= ST_NBR_PARTITIONS) {
3384 if (mtc.mt_count >= STp->nbr_partitions &&
3385 (STp->nbr_partitions = nbr_partitions(STp)) < 0) {
3389 if (mtc.mt_count >= STp->nbr_partitions) {
3393 STp->new_partition = mtc.mt_count;
3398 if (mtc.mt_op == MTMKPART) {
3399 if (!STp->can_partitions) {
3403 if ((i = st_int_ioctl(STp, MTREW, 0)) < 0 ||
3404 (i = partition_tape(STp, mtc.mt_count)) < 0) {
3408 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
3409 STp->ps[i].rw = ST_IDLE;
3410 STp->ps[i].at_sm = 0;
3411 STp->ps[i].last_block_valid = 0;
3413 STp->partition = STp->new_partition = 0;
3414 STp->nbr_partitions = 1; /* Bad guess ?-) */
3415 STps->drv_block = STps->drv_file = 0;
3420 if (mtc.mt_op == MTSEEK) {
3421 i = set_location(STp, mtc.mt_count, STp->new_partition, 0);
3422 if (!STp->can_partitions)
3423 STp->ps[0].rw = ST_IDLE;
3428 if (mtc.mt_op == MTUNLOAD || mtc.mt_op == MTOFFL) {
3429 retval = do_load_unload(STp, file, 0);
3433 if (mtc.mt_op == MTLOAD) {
3434 retval = do_load_unload(STp, file, max(1, mtc.mt_count));
3438 if (mtc.mt_op == MTLOCK || mtc.mt_op == MTUNLOCK) {
3439 retval = do_door_lock(STp, (mtc.mt_op == MTLOCK));
3443 if (STp->can_partitions && STp->ready == ST_READY &&
3444 (i = switch_partition(STp)) < 0) {
3449 if (mtc.mt_op == MTCOMPRESSION)
3450 retval = st_compression(STp, (mtc.mt_count & 1));
3452 retval = st_int_ioctl(STp, mtc.mt_op, mtc.mt_count);
3455 if (!STm->defined) {
3460 if ((i = flush_buffer(STp, 0)) < 0) {
3464 if (STp->can_partitions &&
3465 (i = switch_partition(STp)) < 0) {
3470 if (cmd_type == _IOC_TYPE(MTIOCGET) && cmd_nr == _IOC_NR(MTIOCGET)) {
3471 struct mtget mt_status;
3473 if (_IOC_SIZE(cmd_in) != sizeof(struct mtget)) {
3478 mt_status.mt_type = STp->tape_type;
3479 mt_status.mt_dsreg =
3480 ((STp->block_size << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK) |
3481 ((STp->density << MT_ST_DENSITY_SHIFT) & MT_ST_DENSITY_MASK);
3482 mt_status.mt_blkno = STps->drv_block;
3483 mt_status.mt_fileno = STps->drv_file;
3484 if (STp->block_size != 0) {
3485 if (STps->rw == ST_WRITING)
3486 mt_status.mt_blkno +=
3487 (STp->buffer)->buffer_bytes / STp->block_size;
3488 else if (STps->rw == ST_READING)
3489 mt_status.mt_blkno -=
3490 ((STp->buffer)->buffer_bytes +
3491 STp->block_size - 1) / STp->block_size;
3494 mt_status.mt_gstat = 0;
3495 if (STp->drv_write_prot)
3496 mt_status.mt_gstat |= GMT_WR_PROT(0xffffffff);
3497 if (mt_status.mt_blkno == 0) {
3498 if (mt_status.mt_fileno == 0)
3499 mt_status.mt_gstat |= GMT_BOT(0xffffffff);
3501 mt_status.mt_gstat |= GMT_EOF(0xffffffff);
3503 mt_status.mt_erreg = (STp->recover_reg << MT_ST_SOFTERR_SHIFT);
3504 mt_status.mt_resid = STp->partition;
3505 if (STps->eof == ST_EOM_OK || STps->eof == ST_EOM_ERROR)
3506 mt_status.mt_gstat |= GMT_EOT(0xffffffff);
3507 else if (STps->eof >= ST_EOM_OK)
3508 mt_status.mt_gstat |= GMT_EOD(0xffffffff);
3509 if (STp->density == 1)
3510 mt_status.mt_gstat |= GMT_D_800(0xffffffff);
3511 else if (STp->density == 2)
3512 mt_status.mt_gstat |= GMT_D_1600(0xffffffff);
3513 else if (STp->density == 3)
3514 mt_status.mt_gstat |= GMT_D_6250(0xffffffff);
3515 if (STp->ready == ST_READY)
3516 mt_status.mt_gstat |= GMT_ONLINE(0xffffffff);
3517 if (STp->ready == ST_NO_TAPE)
3518 mt_status.mt_gstat |= GMT_DR_OPEN(0xffffffff);
3520 mt_status.mt_gstat |= GMT_SM(0xffffffff);
3521 if (STm->do_async_writes ||
3522 (STm->do_buffer_writes && STp->block_size != 0) ||
3523 STp->drv_buffer != 0)
3524 mt_status.mt_gstat |= GMT_IM_REP_EN(0xffffffff);
3525 if (STp->cleaning_req)
3526 mt_status.mt_gstat |= GMT_CLN(0xffffffff);
3528 i = copy_to_user(p, &mt_status, sizeof(struct mtget));
3534 STp->recover_reg = 0; /* Clear after read */
3537 } /* End of MTIOCGET */
3538 if (cmd_type == _IOC_TYPE(MTIOCPOS) && cmd_nr == _IOC_NR(MTIOCPOS)) {
3539 struct mtpos mt_pos;
3540 if (_IOC_SIZE(cmd_in) != sizeof(struct mtpos)) {
3544 if ((i = get_location(STp, &blk, &bt, 0)) < 0) {
3548 mt_pos.mt_blkno = blk;
3549 i = copy_to_user(p, &mt_pos, sizeof(struct mtpos));
3554 mutex_unlock(&STp->lock);
3556 case SCSI_IOCTL_GET_IDLUN:
3557 case SCSI_IOCTL_GET_BUS_NUMBER:
3560 if ((cmd_in == SG_IO ||
3561 cmd_in == SCSI_IOCTL_SEND_COMMAND ||
3562 cmd_in == CDROM_SEND_PACKET) &&
3563 !capable(CAP_SYS_RAWIO))
3566 i = scsi_cmd_ioctl(file, STp->disk->queue,
3567 STp->disk, cmd_in, p);
3572 retval = scsi_ioctl(STp->device, cmd_in, p);
3573 if (!retval && cmd_in == SCSI_IOCTL_STOP_UNIT) { /* unload */
3574 STp->rew_at_close = 0;
3575 STp->ready = ST_NO_TAPE;
3580 mutex_unlock(&STp->lock);
3584 #ifdef CONFIG_COMPAT
3585 static long st_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3587 struct scsi_tape *STp = file->private_data;
3588 struct scsi_device *sdev = STp->device;
3589 int ret = -ENOIOCTLCMD;
3590 if (sdev->host->hostt->compat_ioctl) {
3592 ret = sdev->host->hostt->compat_ioctl(sdev, cmd, (void __user *)arg);
3601 /* Try to allocate a new tape buffer. Calling function must not hold
3603 static struct st_buffer *
3604 new_tape_buffer(int from_initialization, int need_dma, int max_sg)
3608 struct st_buffer *tb;
3610 if (from_initialization)
3611 priority = GFP_ATOMIC;
3613 priority = GFP_KERNEL;
3615 i = sizeof(struct st_buffer) + (max_sg - 1) * sizeof(struct scatterlist) +
3616 max_sg * sizeof(struct st_buf_fragment);
3617 tb = kzalloc(i, priority);
3619 printk(KERN_NOTICE "st: Can't allocate new tape buffer.\n");
3622 tb->frp_segs = tb->orig_frp_segs = 0;
3623 tb->use_sg = max_sg;
3624 tb->frp = (struct st_buf_fragment *)(&(tb->sg[0]) + max_sg);
3627 tb->buffer_size = got;
3628 sg_init_table(tb->sg, max_sg);
3634 /* Try to allocate enough space in the tape buffer */
3635 static int enlarge_buffer(struct st_buffer * STbuffer, int new_size, int need_dma)
3637 int segs, nbr, max_segs, b_size, order, got;
3640 if (new_size <= STbuffer->buffer_size)
3643 if (STbuffer->buffer_size <= PAGE_SIZE)
3644 normalize_buffer(STbuffer); /* Avoid extra segment */
3646 max_segs = STbuffer->use_sg;
3647 nbr = max_segs - STbuffer->frp_segs;
3651 priority = GFP_KERNEL | __GFP_NOWARN;
3653 priority |= GFP_DMA;
3654 for (b_size = PAGE_SIZE, order=0; order <= 6 &&
3655 b_size < new_size - STbuffer->buffer_size;
3656 order++, b_size *= 2)
3659 for (segs = STbuffer->frp_segs, got = STbuffer->buffer_size;
3660 segs < max_segs && got < new_size;) {
3661 STbuffer->frp[segs].page = alloc_pages(priority, order);
3662 if (STbuffer->frp[segs].page == NULL) {
3663 if (new_size - got <= (max_segs - segs) * b_size / 2) {
3664 b_size /= 2; /* Large enough for the rest of the buffers */
3668 DEB(STbuffer->buffer_size = got);
3669 normalize_buffer(STbuffer);
3672 STbuffer->frp[segs].length = b_size;
3673 STbuffer->frp_segs += 1;
3675 STbuffer->buffer_size = got;
3676 if (STbuffer->cleared)
3677 memset(page_address(STbuffer->frp[segs].page), 0, b_size);
3680 STbuffer->b_data = page_address(STbuffer->frp[0].page);
3686 /* Make sure that no data from previous user is in the internal buffer */
3687 static void clear_buffer(struct st_buffer * st_bp)
3691 for (i=0; i < st_bp->frp_segs; i++)
3692 memset(page_address(st_bp->frp[i].page), 0, st_bp->frp[i].length);
3697 /* Release the extra buffer */
3698 static void normalize_buffer(struct st_buffer * STbuffer)
3702 for (i = STbuffer->orig_frp_segs; i < STbuffer->frp_segs; i++) {
3703 order = get_order(STbuffer->frp[i].length);
3704 __free_pages(STbuffer->frp[i].page, order);
3705 STbuffer->buffer_size -= STbuffer->frp[i].length;
3707 STbuffer->frp_segs = STbuffer->orig_frp_segs;
3708 STbuffer->frp_sg_current = 0;
3709 STbuffer->sg_segs = 0;
3713 /* Move data from the user buffer to the tape buffer. Returns zero (success) or
3714 negative error code. */
3715 static int append_to_buffer(const char __user *ubp, struct st_buffer * st_bp, int do_count)
3717 int i, cnt, res, offset;
3719 for (i = 0, offset = st_bp->buffer_bytes;
3720 i < st_bp->frp_segs && offset >= st_bp->frp[i].length; i++)
3721 offset -= st_bp->frp[i].length;
3722 if (i == st_bp->frp_segs) { /* Should never happen */
3723 printk(KERN_WARNING "st: append_to_buffer offset overflow.\n");
3726 for (; i < st_bp->frp_segs && do_count > 0; i++) {
3727 cnt = st_bp->frp[i].length - offset < do_count ?
3728 st_bp->frp[i].length - offset : do_count;
3729 res = copy_from_user(page_address(st_bp->frp[i].page) + offset, ubp, cnt);
3733 st_bp->buffer_bytes += cnt;
3737 if (do_count) /* Should never happen */
3744 /* Move data from the tape buffer to the user buffer. Returns zero (success) or
3745 negative error code. */
3746 static int from_buffer(struct st_buffer * st_bp, char __user *ubp, int do_count)
3748 int i, cnt, res, offset;
3750 for (i = 0, offset = st_bp->read_pointer;
3751 i < st_bp->frp_segs && offset >= st_bp->frp[i].length; i++)
3752 offset -= st_bp->frp[i].length;
3753 if (i == st_bp->frp_segs) { /* Should never happen */
3754 printk(KERN_WARNING "st: from_buffer offset overflow.\n");
3757 for (; i < st_bp->frp_segs && do_count > 0; i++) {
3758 cnt = st_bp->frp[i].length - offset < do_count ?
3759 st_bp->frp[i].length - offset : do_count;
3760 res = copy_to_user(ubp, page_address(st_bp->frp[i].page) + offset, cnt);
3764 st_bp->buffer_bytes -= cnt;
3765 st_bp->read_pointer += cnt;
3769 if (do_count) /* Should never happen */
3776 /* Move data towards start of buffer */
3777 static void move_buffer_data(struct st_buffer * st_bp, int offset)
3779 int src_seg, dst_seg, src_offset = 0, dst_offset;
3785 total=st_bp->buffer_bytes - offset;
3786 for (src_seg=0; src_seg < st_bp->frp_segs; src_seg++) {
3787 src_offset = offset;
3788 if (src_offset < st_bp->frp[src_seg].length)
3790 offset -= st_bp->frp[src_seg].length;
3793 st_bp->buffer_bytes = st_bp->read_pointer = total;
3794 for (dst_seg=dst_offset=0; total > 0; ) {
3795 count = min(st_bp->frp[dst_seg].length - dst_offset,
3796 st_bp->frp[src_seg].length - src_offset);
3797 memmove(page_address(st_bp->frp[dst_seg].page) + dst_offset,
3798 page_address(st_bp->frp[src_seg].page) + src_offset, count);
3799 src_offset += count;
3800 if (src_offset >= st_bp->frp[src_seg].length) {
3804 dst_offset += count;
3805 if (dst_offset >= st_bp->frp[dst_seg].length) {
3814 /* Fill the s/g list up to the length required for this transfer */
3815 static void buf_to_sg(struct st_buffer *STbp, unsigned int length)
3819 struct scatterlist *sg;
3820 struct st_buf_fragment *frp;
3822 if (length == STbp->frp_sg_current)
3823 return; /* work already done */
3825 sg = &(STbp->sg[0]);
3827 for (i=count=0; count < length; i++) {
3828 if (length - count > frp[i].length)
3829 sg_set_page(&sg[i], frp[i].page, frp[i].length, 0);
3831 sg_set_page(&sg[i], frp[i].page, length - count, 0);
3832 count += sg[i].length;
3835 STbp->frp_sg_current = length;
3839 /* Validate the options from command line or module parameters */
3840 static void validate_options(void)
3843 st_fixed_buffer_size = buffer_kbs * ST_KILOBYTE;
3844 if (max_sg_segs >= ST_FIRST_SG)
3845 st_max_sg_segs = max_sg_segs;
3849 /* Set the boot options. Syntax is defined in Documenation/scsi/st.txt.
3851 static int __init st_setup(char *str)
3853 int i, len, ints[5];
3856 stp = get_options(str, ARRAY_SIZE(ints), ints);
3859 for (i = 0; i < ints[0] && i < ARRAY_SIZE(parms); i++)
3861 *parms[i].val = ints[i + 1];
3863 while (stp != NULL) {
3864 for (i = 0; i < ARRAY_SIZE(parms); i++) {
3865 len = strlen(parms[i].name);
3866 if (!strncmp(stp, parms[i].name, len) &&
3867 (*(stp + len) == ':' || *(stp + len) == '=')) {
3870 simple_strtoul(stp + len + 1, NULL, 0);
3872 printk(KERN_WARNING "st: Obsolete parameter %s\n",
3877 if (i >= ARRAY_SIZE(parms))
3878 printk(KERN_WARNING "st: invalid parameter in '%s'\n",
3880 stp = strchr(stp, ',');
3891 __setup("st=", st_setup);
3895 static const struct file_operations st_fops =
3897 .owner = THIS_MODULE,
3900 .unlocked_ioctl = st_ioctl,
3901 #ifdef CONFIG_COMPAT
3902 .compat_ioctl = st_compat_ioctl,
3906 .release = st_release,
3909 static int st_probe(struct device *dev)
3911 struct scsi_device *SDp = to_scsi_device(dev);
3912 struct gendisk *disk = NULL;
3913 struct cdev *cdev = NULL;
3914 struct scsi_tape *tpnt = NULL;
3915 struct st_modedef *STm;
3916 struct st_partstat *STps;
3917 struct st_buffer *buffer;
3918 int i, j, mode, dev_num, error;
3921 if (SDp->type != TYPE_TAPE)
3923 if ((stp = st_incompatible(SDp))) {
3924 sdev_printk(KERN_INFO, SDp, "Found incompatible tape\n");
3925 printk(KERN_INFO "st: The suggested driver is %s.\n", stp);
3929 i = min(SDp->request_queue->max_hw_segments,
3930 SDp->request_queue->max_phys_segments);
3931 if (st_max_sg_segs < i)
3933 buffer = new_tape_buffer(1, (SDp->host)->unchecked_isa_dma, i);
3934 if (buffer == NULL) {
3936 "st: Can't allocate new tape buffer. Device not attached.\n");
3940 disk = alloc_disk(1);
3942 printk(KERN_ERR "st: out of memory. Device not attached.\n");
3943 goto out_buffer_free;
3946 write_lock(&st_dev_arr_lock);
3947 if (st_nr_dev >= st_dev_max) {
3948 struct scsi_tape **tmp_da;
3951 tmp_dev_max = max(st_nr_dev * 2, 8);
3952 if (tmp_dev_max > ST_MAX_TAPES)
3953 tmp_dev_max = ST_MAX_TAPES;
3954 if (tmp_dev_max <= st_nr_dev) {
3955 write_unlock(&st_dev_arr_lock);
3956 printk(KERN_ERR "st: Too many tape devices (max. %d).\n",
3961 tmp_da = kzalloc(tmp_dev_max * sizeof(struct scsi_tape *), GFP_ATOMIC);
3962 if (tmp_da == NULL) {
3963 write_unlock(&st_dev_arr_lock);
3964 printk(KERN_ERR "st: Can't extend device array.\n");
3968 if (scsi_tapes != NULL) {
3969 memcpy(tmp_da, scsi_tapes,
3970 st_dev_max * sizeof(struct scsi_tape *));
3973 scsi_tapes = tmp_da;
3975 st_dev_max = tmp_dev_max;
3978 for (i = 0; i < st_dev_max; i++)
3979 if (scsi_tapes[i] == NULL)
3981 if (i >= st_dev_max)
3982 panic("scsi_devices corrupt (st)");
3984 tpnt = kzalloc(sizeof(struct scsi_tape), GFP_ATOMIC);
3986 write_unlock(&st_dev_arr_lock);
3987 printk(KERN_ERR "st: Can't allocate device descriptor.\n");
3990 kref_init(&tpnt->kref);
3992 sprintf(disk->disk_name, "st%d", i);
3993 disk->private_data = &tpnt->driver;
3994 disk->queue = SDp->request_queue;
3995 tpnt->driver = &st_template;
3996 scsi_tapes[i] = tpnt;
4000 if (SDp->scsi_level <= 2)
4001 tpnt->tape_type = MT_ISSCSI1;
4003 tpnt->tape_type = MT_ISSCSI2;
4005 tpnt->buffer = buffer;
4006 tpnt->buffer->last_SRpnt = NULL;
4011 tpnt->drv_buffer = 1; /* Try buffering if no mode sense */
4012 tpnt->restr_dma = (SDp->host)->unchecked_isa_dma;
4013 tpnt->use_pf = (SDp->scsi_level >= SCSI_2);
4015 tpnt->do_auto_lock = ST_AUTO_LOCK;
4016 tpnt->can_bsr = (SDp->scsi_level > 2 ? 1 : ST_IN_FILE_POS); /* BSR mandatory in SCSI3 */
4017 tpnt->can_partitions = 0;
4018 tpnt->two_fm = ST_TWO_FM;
4019 tpnt->fast_mteom = ST_FAST_MTEOM;
4020 tpnt->scsi2_logical = ST_SCSI2LOGICAL;
4021 tpnt->sili = ST_SILI;
4022 tpnt->immediate = ST_NOWAIT;
4023 tpnt->default_drvbuffer = 0xff; /* No forced buffering */
4024 tpnt->partition = 0;
4025 tpnt->new_partition = 0;
4026 tpnt->nbr_partitions = 0;
4027 tpnt->device->timeout = ST_TIMEOUT;
4028 tpnt->long_timeout = ST_LONG_TIMEOUT;
4029 tpnt->try_dio = try_direct_io && !SDp->host->unchecked_isa_dma;
4031 for (i = 0; i < ST_NBR_MODES; i++) {
4032 STm = &(tpnt->modes[i]);
4034 STm->sysv = ST_SYSV;
4035 STm->defaults_for_writes = 0;
4036 STm->do_async_writes = ST_ASYNC_WRITES;
4037 STm->do_buffer_writes = ST_BUFFER_WRITES;
4038 STm->do_read_ahead = ST_READ_AHEAD;
4039 STm->default_compression = ST_DONT_TOUCH;
4040 STm->default_blksize = (-1); /* No forced size */
4041 STm->default_density = (-1); /* No forced density */
4044 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
4045 STps = &(tpnt->ps[i]);
4047 STps->eof = ST_NOEOF;
4049 STps->last_block_valid = 0;
4050 STps->drv_block = (-1);
4051 STps->drv_file = (-1);
4054 tpnt->current_mode = 0;
4055 tpnt->modes[0].defined = 1;
4057 tpnt->density_changed = tpnt->compression_changed =
4058 tpnt->blksize_changed = 0;
4059 mutex_init(&tpnt->lock);
4062 write_unlock(&st_dev_arr_lock);
4064 for (mode = 0; mode < ST_NBR_MODES; ++mode) {
4065 STm = &(tpnt->modes[mode]);
4066 for (j=0; j < 2; j++) {
4067 cdev = cdev_alloc();
4070 "st%d: out of memory. Device not attached.\n",
4074 cdev->owner = THIS_MODULE;
4075 cdev->ops = &st_fops;
4077 error = cdev_add(cdev,
4078 MKDEV(SCSI_TAPE_MAJOR, TAPE_MINOR(dev_num, mode, j)),
4081 printk(KERN_ERR "st%d: Can't add %s-rewind mode %d\n",
4082 dev_num, j ? "non" : "auto", mode);
4083 printk(KERN_ERR "st%d: Device not attached.\n", dev_num);
4086 STm->cdevs[j] = cdev;
4089 error = do_create_class_files(tpnt, dev_num, mode);
4094 sdev_printk(KERN_NOTICE, SDp,
4095 "Attached scsi tape %s\n", tape_name(tpnt));
4096 sdev_printk(KERN_INFO, SDp, "%s: try direct i/o: %s (alignment %d B)\n",
4097 tape_name(tpnt), tpnt->try_dio ? "yes" : "no",
4098 queue_dma_alignment(SDp->request_queue) + 1);
4103 for (mode=0; mode < ST_NBR_MODES; mode++) {
4104 STm = &(tpnt->modes[mode]);
4105 sysfs_remove_link(&tpnt->device->sdev_gendev.kobj,
4107 for (j=0; j < 2; j++) {
4108 if (STm->cdevs[j]) {
4109 if (cdev == STm->cdevs[j])
4111 device_destroy(st_sysfs_class,
4112 MKDEV(SCSI_TAPE_MAJOR,
4113 TAPE_MINOR(i, mode, j)));
4114 cdev_del(STm->cdevs[j]);
4120 write_lock(&st_dev_arr_lock);
4121 scsi_tapes[dev_num] = NULL;
4123 write_unlock(&st_dev_arr_lock);
4134 static int st_remove(struct device *dev)
4136 struct scsi_device *SDp = to_scsi_device(dev);
4137 struct scsi_tape *tpnt;
4140 write_lock(&st_dev_arr_lock);
4141 for (i = 0; i < st_dev_max; i++) {
4142 tpnt = scsi_tapes[i];
4143 if (tpnt != NULL && tpnt->device == SDp) {
4144 scsi_tapes[i] = NULL;
4146 write_unlock(&st_dev_arr_lock);
4147 sysfs_remove_link(&tpnt->device->sdev_gendev.kobj,
4149 for (mode = 0; mode < ST_NBR_MODES; ++mode) {
4150 for (j=0; j < 2; j++) {
4151 device_destroy(st_sysfs_class,
4152 MKDEV(SCSI_TAPE_MAJOR,
4153 TAPE_MINOR(i, mode, j)));
4154 cdev_del(tpnt->modes[mode].cdevs[j]);
4155 tpnt->modes[mode].cdevs[j] = NULL;
4159 mutex_lock(&st_ref_mutex);
4160 kref_put(&tpnt->kref, scsi_tape_release);
4161 mutex_unlock(&st_ref_mutex);
4166 write_unlock(&st_dev_arr_lock);
4171 * scsi_tape_release - Called to free the Scsi_Tape structure
4172 * @kref: pointer to embedded kref
4174 * st_ref_mutex must be held entering this routine. Because it is
4175 * called on last put, you should always use the scsi_tape_get()
4176 * scsi_tape_put() helpers which manipulate the semaphore directly
4177 * and never do a direct kref_put().
4179 static void scsi_tape_release(struct kref *kref)
4181 struct scsi_tape *tpnt = to_scsi_tape(kref);
4182 struct gendisk *disk = tpnt->disk;
4184 tpnt->device = NULL;
4187 tpnt->buffer->orig_frp_segs = 0;
4188 normalize_buffer(tpnt->buffer);
4189 kfree(tpnt->buffer);
4192 disk->private_data = NULL;
4198 static int __init init_st(void)
4204 printk(KERN_INFO "st: Version %s, fixed bufsize %d, s/g segs %d\n",
4205 verstr, st_fixed_buffer_size, st_max_sg_segs);
4207 st_sysfs_class = class_create(THIS_MODULE, "scsi_tape");
4208 if (IS_ERR(st_sysfs_class)) {
4209 printk(KERN_ERR "Unable create sysfs class for SCSI tapes\n");
4210 return PTR_ERR(st_sysfs_class);
4213 err = register_chrdev_region(MKDEV(SCSI_TAPE_MAJOR, 0),
4214 ST_MAX_TAPE_ENTRIES, "st");
4216 printk(KERN_ERR "Unable to get major %d for SCSI tapes\n",
4221 err = scsi_register_driver(&st_template.gendrv);
4225 err = do_create_sysfs_files();
4232 scsi_unregister_driver(&st_template.gendrv);
4234 unregister_chrdev_region(MKDEV(SCSI_TAPE_MAJOR, 0),
4235 ST_MAX_TAPE_ENTRIES);
4237 class_destroy(st_sysfs_class);
4241 static void __exit exit_st(void)
4243 do_remove_sysfs_files();
4244 scsi_unregister_driver(&st_template.gendrv);
4245 unregister_chrdev_region(MKDEV(SCSI_TAPE_MAJOR, 0),
4246 ST_MAX_TAPE_ENTRIES);
4247 class_destroy(st_sysfs_class);
4249 printk(KERN_INFO "st: Unloaded.\n");
4252 module_init(init_st);
4253 module_exit(exit_st);
4256 /* The sysfs driver interface. Read-only at the moment */
4257 static ssize_t st_try_direct_io_show(struct device_driver *ddp, char *buf)
4259 return snprintf(buf, PAGE_SIZE, "%d\n", try_direct_io);
4261 static DRIVER_ATTR(try_direct_io, S_IRUGO, st_try_direct_io_show, NULL);
4263 static ssize_t st_fixed_buffer_size_show(struct device_driver *ddp, char *buf)
4265 return snprintf(buf, PAGE_SIZE, "%d\n", st_fixed_buffer_size);
4267 static DRIVER_ATTR(fixed_buffer_size, S_IRUGO, st_fixed_buffer_size_show, NULL);
4269 static ssize_t st_max_sg_segs_show(struct device_driver *ddp, char *buf)
4271 return snprintf(buf, PAGE_SIZE, "%d\n", st_max_sg_segs);
4273 static DRIVER_ATTR(max_sg_segs, S_IRUGO, st_max_sg_segs_show, NULL);
4275 static ssize_t st_version_show(struct device_driver *ddd, char *buf)
4277 return snprintf(buf, PAGE_SIZE, "[%s]\n", verstr);
4279 static DRIVER_ATTR(version, S_IRUGO, st_version_show, NULL);
4281 static int do_create_sysfs_files(void)
4283 struct device_driver *sysfs = &st_template.gendrv;
4286 err = driver_create_file(sysfs, &driver_attr_try_direct_io);
4289 err = driver_create_file(sysfs, &driver_attr_fixed_buffer_size);
4291 goto err_try_direct_io;
4292 err = driver_create_file(sysfs, &driver_attr_max_sg_segs);
4294 goto err_attr_fixed_buf;
4295 err = driver_create_file(sysfs, &driver_attr_version);
4297 goto err_attr_max_sg;
4302 driver_remove_file(sysfs, &driver_attr_max_sg_segs);
4304 driver_remove_file(sysfs, &driver_attr_fixed_buffer_size);
4306 driver_remove_file(sysfs, &driver_attr_try_direct_io);
4310 static void do_remove_sysfs_files(void)
4312 struct device_driver *sysfs = &st_template.gendrv;
4314 driver_remove_file(sysfs, &driver_attr_version);
4315 driver_remove_file(sysfs, &driver_attr_max_sg_segs);
4316 driver_remove_file(sysfs, &driver_attr_fixed_buffer_size);
4317 driver_remove_file(sysfs, &driver_attr_try_direct_io);
4321 /* The sysfs simple class interface */
4323 st_defined_show(struct device *dev, struct device_attribute *attr, char *buf)
4325 struct st_modedef *STm = (struct st_modedef *)dev_get_drvdata(dev);
4328 l = snprintf(buf, PAGE_SIZE, "%d\n", STm->defined);
4332 DEVICE_ATTR(defined, S_IRUGO, st_defined_show, NULL);
4335 st_defblk_show(struct device *dev, struct device_attribute *attr, char *buf)
4337 struct st_modedef *STm = (struct st_modedef *)dev_get_drvdata(dev);
4340 l = snprintf(buf, PAGE_SIZE, "%d\n", STm->default_blksize);
4344 DEVICE_ATTR(default_blksize, S_IRUGO, st_defblk_show, NULL);
4347 st_defdensity_show(struct device *dev, struct device_attribute *attr, char *buf)
4349 struct st_modedef *STm = (struct st_modedef *)dev_get_drvdata(dev);
4353 fmt = STm->default_density >= 0 ? "0x%02x\n" : "%d\n";
4354 l = snprintf(buf, PAGE_SIZE, fmt, STm->default_density);
4358 DEVICE_ATTR(default_density, S_IRUGO, st_defdensity_show, NULL);
4361 st_defcompression_show(struct device *dev, struct device_attribute *attr,
4364 struct st_modedef *STm = (struct st_modedef *)dev_get_drvdata(dev);
4367 l = snprintf(buf, PAGE_SIZE, "%d\n", STm->default_compression - 1);
4371 DEVICE_ATTR(default_compression, S_IRUGO, st_defcompression_show, NULL);
4374 st_options_show(struct device *dev, struct device_attribute *attr, char *buf)
4376 struct st_modedef *STm = (struct st_modedef *)dev_get_drvdata(dev);
4377 struct scsi_tape *STp;
4381 for (i=0; i < st_dev_max; i++) {
4382 for (j=0; j < ST_NBR_MODES; j++)
4383 if (&scsi_tapes[i]->modes[j] == STm)
4385 if (j < ST_NBR_MODES)
4388 if (i == st_dev_max)
4389 return 0; /* should never happen */
4391 STp = scsi_tapes[i];
4393 options = STm->do_buffer_writes ? MT_ST_BUFFER_WRITES : 0;
4394 options |= STm->do_async_writes ? MT_ST_ASYNC_WRITES : 0;
4395 options |= STm->do_read_ahead ? MT_ST_READ_AHEAD : 0;
4396 DEB( options |= debugging ? MT_ST_DEBUGGING : 0 );
4397 options |= STp->two_fm ? MT_ST_TWO_FM : 0;
4398 options |= STp->fast_mteom ? MT_ST_FAST_MTEOM : 0;
4399 options |= STm->defaults_for_writes ? MT_ST_DEF_WRITES : 0;
4400 options |= STp->can_bsr ? MT_ST_CAN_BSR : 0;
4401 options |= STp->omit_blklims ? MT_ST_NO_BLKLIMS : 0;
4402 options |= STp->can_partitions ? MT_ST_CAN_PARTITIONS : 0;
4403 options |= STp->scsi2_logical ? MT_ST_SCSI2LOGICAL : 0;
4404 options |= STm->sysv ? MT_ST_SYSV : 0;
4405 options |= STp->immediate ? MT_ST_NOWAIT : 0;
4406 options |= STp->sili ? MT_ST_SILI : 0;
4408 l = snprintf(buf, PAGE_SIZE, "0x%08x\n", options);
4412 DEVICE_ATTR(options, S_IRUGO, st_options_show, NULL);
4414 static int do_create_class_files(struct scsi_tape *STp, int dev_num, int mode)
4418 struct device *st_class_member;
4420 for (rew=0; rew < 2; rew++) {
4421 /* Make sure that the minor numbers corresponding to the four
4422 first modes always get the same names */
4423 i = mode << (4 - ST_NBR_MODE_BITS);
4424 snprintf(name, 10, "%s%s%s", rew ? "n" : "",
4425 STp->disk->disk_name, st_formats[i]);
4427 device_create(st_sysfs_class, &STp->device->sdev_gendev,
4428 MKDEV(SCSI_TAPE_MAJOR,
4429 TAPE_MINOR(dev_num, mode, rew)),
4431 if (IS_ERR(st_class_member)) {
4432 printk(KERN_WARNING "st%d: device_create failed\n",
4434 error = PTR_ERR(st_class_member);
4437 dev_set_drvdata(st_class_member, &STp->modes[mode]);
4439 error = device_create_file(st_class_member,
4441 if (error) goto out;
4442 error = device_create_file(st_class_member,
4443 &dev_attr_default_blksize);
4444 if (error) goto out;
4445 error = device_create_file(st_class_member,
4446 &dev_attr_default_density);
4447 if (error) goto out;
4448 error = device_create_file(st_class_member,
4449 &dev_attr_default_compression);
4450 if (error) goto out;
4451 error = device_create_file(st_class_member,
4453 if (error) goto out;
4455 if (mode == 0 && rew == 0) {
4456 error = sysfs_create_link(&STp->device->sdev_gendev.kobj,
4457 &st_class_member->kobj,
4461 "st%d: Can't create sysfs link from SCSI device.\n",
4474 /* The following functions may be useful for a larger audience. */
4475 static int sgl_map_user_pages(struct scatterlist *sgl, const unsigned int max_pages,
4476 unsigned long uaddr, size_t count, int rw)
4478 unsigned long end = (uaddr + count + PAGE_SIZE - 1) >> PAGE_SHIFT;
4479 unsigned long start = uaddr >> PAGE_SHIFT;
4480 const int nr_pages = end - start;
4482 struct page **pages;
4484 /* User attempted Overflow! */
4485 if ((uaddr + count) < uaddr)
4489 if (nr_pages > max_pages)
4496 if ((pages = kmalloc(max_pages * sizeof(*pages), GFP_KERNEL)) == NULL)
4499 /* Try to fault in all of the necessary pages */
4500 down_read(¤t->mm->mmap_sem);
4501 /* rw==READ means read from drive, write into memory area */
4502 res = get_user_pages(
4508 0, /* don't force */
4511 up_read(¤t->mm->mmap_sem);
4513 /* Errors and no page mapped should return here */
4517 for (i=0; i < nr_pages; i++) {
4518 /* FIXME: flush superflous for rw==READ,
4519 * probably wrong function for rw==WRITE
4521 flush_dcache_page(pages[i]);
4524 /* Populate the scatter/gather list */
4525 sg_set_page(&sgl[0], pages[0], 0, uaddr & ~PAGE_MASK);
4527 sgl[0].length = PAGE_SIZE - sgl[0].offset;
4528 count -= sgl[0].length;
4529 for (i=1; i < nr_pages ; i++) {
4530 sg_set_page(&sgl[i], pages[i],
4531 count < PAGE_SIZE ? count : PAGE_SIZE, 0);;
4536 sgl[0].length = count;
4544 for (j=0; j < res; j++)
4545 page_cache_release(pages[j]);
4553 /* And unmap them... */
4554 static int sgl_unmap_user_pages(struct scatterlist *sgl, const unsigned int nr_pages,
4559 for (i=0; i < nr_pages; i++) {
4560 struct page *page = sg_page(&sgl[i]);
4564 /* FIXME: cache flush missing for rw==READ
4565 * FIXME: call the correct reference counting function
4567 page_cache_release(page);