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 - 2004 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 char *verstr = "20040403";
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/ioctl.h>
33 #include <linux/fcntl.h>
34 #include <linux/spinlock.h>
35 #include <linux/blkdev.h>
36 #include <linux/moduleparam.h>
37 #include <linux/devfs_fs_kernel.h>
38 #include <linux/cdev.h>
39 #include <asm/uaccess.h>
41 #include <asm/system.h>
43 /* The driver prints some debugging information on the console if DEBUG
44 is defined and non-zero. */
48 /* The message level for the debug messages is currently set to KERN_NOTICE
49 so that people can easily see the messages. Later when the debugging messages
50 in the drivers are more widely classified, this may be changed to KERN_DEBUG. */
51 #define ST_DEB_MSG KERN_NOTICE
53 #define DEBC(a) if (debugging) { a ; }
63 #include <scsi/scsi_driver.h>
64 #include <scsi/scsi_ioctl.h>
66 #define ST_KILOBYTE 1024
68 #include "st_options.h"
71 static int buffer_kbs;
72 static int max_sg_segs;
73 static int try_direct_io = TRY_DIRECT_IO;
74 static int try_rdio = TRUE;
75 static int try_wdio = TRUE;
77 static int st_dev_max;
80 static struct class_simple *st_sysfs_class;
82 MODULE_AUTHOR("Kai Makisara");
83 MODULE_DESCRIPTION("SCSI Tape Driver");
84 MODULE_LICENSE("GPL");
86 /* Set 'perm' (4th argument) to 0 to disable module_param's definition
87 * of sysfs parameters (which module_param doesn't yet support).
88 * Sysfs parameters defined explicitly later.
90 module_param_named(buffer_kbs, buffer_kbs, int, 0);
91 MODULE_PARM_DESC(buffer_kbs, "Default driver buffer size for fixed block mode (KB; 32)");
92 module_param_named(max_sg_segs, max_sg_segs, int, 0);
93 MODULE_PARM_DESC(max_sg_segs, "Maximum number of scatter/gather segments to use (256)");
94 module_param_named(try_direct_io, try_direct_io, int, 0);
95 MODULE_PARM_DESC(try_direct_io, "Try direct I/O between user buffer and tape drive (1)");
97 /* Extra parameters for testing */
98 module_param_named(try_rdio, try_rdio, int, 0);
99 MODULE_PARM_DESC(try_rdio, "Try direct read i/o when possible");
100 module_param_named(try_wdio, try_wdio, int, 0);
101 MODULE_PARM_DESC(try_wdio, "Try direct write i/o when possible");
104 static int write_threshold_kbs; /* retained for compatibility */
105 static struct st_dev_parm {
108 } parms[] __initdata = {
110 "buffer_kbs", &buffer_kbs
112 { /* Retained for compatibility with 2.4 */
113 "write_threshold_kbs", &write_threshold_kbs
119 "try_direct_io", &try_direct_io
124 /* Restrict the number of modes so that names for all are assigned */
125 #if ST_NBR_MODES > 16
126 #error "Maximum number of modes is 16"
128 /* Bit reversed order to get same names for same minors with all
130 static char *st_formats[] = {
131 "", "r", "k", "s", "l", "t", "o", "u",
132 "m", "v", "p", "x", "a", "y", "q", "z"};
134 /* The default definitions have been moved to st_options.h */
136 #define ST_FIXED_BUFFER_SIZE (ST_FIXED_BUFFER_BLOCKS * ST_KILOBYTE)
138 /* The buffer size should fit into the 24 bits for length in the
139 6-byte SCSI read and write commands. */
140 #if ST_FIXED_BUFFER_SIZE >= (2 << 24 - 1)
141 #error "Buffer size should not exceed (2 << 24 - 1) bytes!"
144 DEB( static int debugging = DEBUG; )
146 #define MAX_RETRIES 0
147 #define MAX_WRITE_RETRIES 0
148 #define MAX_READY_RETRIES 0
149 #define NO_TAPE NOT_READY
151 #define ST_TIMEOUT (900 * HZ)
152 #define ST_LONG_TIMEOUT (14000 * HZ)
154 /* Remove mode bits and auto-rewind bit (7) */
155 #define TAPE_NR(x) ( ((iminor(x) & ~255) >> (ST_NBR_MODE_BITS + 1)) | \
156 (iminor(x) & ~(-1 << ST_MODE_SHIFT)) )
157 #define TAPE_MODE(x) ((iminor(x) & ST_MODE_MASK) >> ST_MODE_SHIFT)
159 /* Construct the minor number from the device (d), mode (m), and non-rewind (n) data */
160 #define TAPE_MINOR(d, m, n) (((d & ~(255 >> (ST_NBR_MODE_BITS + 1))) << (ST_NBR_MODE_BITS + 1)) | \
161 (d & (255 >> (ST_NBR_MODE_BITS + 1))) | (m << ST_MODE_SHIFT) | ((n != 0) << 7) )
163 /* Internal ioctl to set both density (uppermost 8 bits) and blocksize (lower
165 #define SET_DENS_AND_BLK 0x10001
167 static rwlock_t st_dev_arr_lock = RW_LOCK_UNLOCKED;
169 static int st_fixed_buffer_size = ST_FIXED_BUFFER_SIZE;
170 static int st_max_sg_segs = ST_MAX_SG;
172 static Scsi_Tape **scsi_tapes = NULL;
174 static int modes_defined;
176 static ST_buffer *new_tape_buffer(int, int, int);
177 static int enlarge_buffer(ST_buffer *, int, int);
178 static void normalize_buffer(ST_buffer *);
179 static int append_to_buffer(const char __user *, ST_buffer *, int);
180 static int from_buffer(ST_buffer *, char __user *, int);
181 static void move_buffer_data(ST_buffer *, int);
182 static void buf_to_sg(ST_buffer *, unsigned int);
184 static int st_map_user_pages(struct scatterlist *, const unsigned int,
185 unsigned long, size_t, int, unsigned long);
186 static int sgl_map_user_pages(struct scatterlist *, const unsigned int,
187 unsigned long, size_t, int);
188 static int sgl_unmap_user_pages(struct scatterlist *, const unsigned int, int);
190 static int st_probe(struct device *);
191 static int st_remove(struct device *);
192 static int st_init_command(struct scsi_cmnd *);
194 static void do_create_driverfs_files(void);
195 static void do_remove_driverfs_files(void);
196 static void do_create_class_files(Scsi_Tape *, int, int);
198 static struct scsi_driver st_template = {
199 .owner = THIS_MODULE,
205 .init_command = st_init_command,
208 static int st_compression(Scsi_Tape *, int);
210 static int find_partition(Scsi_Tape *);
211 static int switch_partition(Scsi_Tape *);
213 static int st_int_ioctl(Scsi_Tape *, unsigned int, unsigned long);
216 #include "osst_detect.h"
217 #ifndef SIGS_FROM_OSST
218 #define SIGS_FROM_OSST \
219 {"OnStream", "SC-", "", "osst"}, \
220 {"OnStream", "DI-", "", "osst"}, \
221 {"OnStream", "DP-", "", "osst"}, \
222 {"OnStream", "USB", "", "osst"}, \
223 {"OnStream", "FW-", "", "osst"}
226 struct st_reject_data {
230 char *driver_hint; /* Name of the correct driver, NULL if unknown */
233 static struct st_reject_data reject_list[] = {
234 /* {"XXX", "Yy-", "", NULL}, example */
238 /* If the device signature is on the list of incompatible drives, the
239 function returns a pointer to the name of the correct driver (if known) */
240 static char * st_incompatible(Scsi_Device* SDp)
242 struct st_reject_data *rp;
244 for (rp=&(reject_list[0]); rp->vendor != NULL; rp++)
245 if (!strncmp(rp->vendor, SDp->vendor, strlen(rp->vendor)) &&
246 !strncmp(rp->model, SDp->model, strlen(rp->model)) &&
247 !strncmp(rp->rev, SDp->rev, strlen(rp->rev))) {
249 return rp->driver_hint;
257 static inline char *tape_name(Scsi_Tape *tape)
259 return tape->disk->disk_name;
262 /* Convert the result to success code */
263 static int st_chk_result(Scsi_Tape *STp, Scsi_Request * SRpnt)
265 int result = SRpnt->sr_result;
266 unsigned char *sense = SRpnt->sr_sense_buffer, scode;
267 DEB(const char *stp;)
268 char *name = tape_name(STp);
271 sense[0] = 0; /* We don't have sense data if this byte is zero */
275 if ((driver_byte(result) & DRIVER_MASK) == DRIVER_SENSE)
276 scode = sense[2] & 0x0f;
284 printk(ST_DEB_MSG "%s: Error: %x, cmd: %x %x %x %x %x %x Len: %d\n",
286 SRpnt->sr_cmnd[0], SRpnt->sr_cmnd[1], SRpnt->sr_cmnd[2],
287 SRpnt->sr_cmnd[3], SRpnt->sr_cmnd[4], SRpnt->sr_cmnd[5],
289 if (driver_byte(result) & DRIVER_SENSE)
290 print_req_sense("st", SRpnt);
291 } else ) /* end DEB */
292 if (!(driver_byte(result) & DRIVER_SENSE) ||
293 ((sense[0] & 0x70) == 0x70 &&
295 scode != RECOVERED_ERROR &&
296 /* scode != UNIT_ATTENTION && */
297 scode != BLANK_CHECK &&
298 scode != VOLUME_OVERFLOW &&
299 SRpnt->sr_cmnd[0] != MODE_SENSE &&
300 SRpnt->sr_cmnd[0] != TEST_UNIT_READY)) { /* Abnormal conditions for tape */
301 if (driver_byte(result) & DRIVER_SENSE) {
302 printk(KERN_WARNING "%s: Error with sense data: ", name);
303 print_req_sense("st", SRpnt);
306 "%s: Error %x (sugg. bt 0x%x, driver bt 0x%x, host bt 0x%x).\n",
307 name, result, suggestion(result),
308 driver_byte(result) & DRIVER_MASK, host_byte(result));
311 if (STp->cln_mode >= EXTENDED_SENSE_START) {
312 if (STp->cln_sense_value)
313 STp->cleaning_req |= ((SRpnt->sr_sense_buffer[STp->cln_mode] &
314 STp->cln_sense_mask) == STp->cln_sense_value);
316 STp->cleaning_req |= ((SRpnt->sr_sense_buffer[STp->cln_mode] &
317 STp->cln_sense_mask) != 0);
319 if (sense[12] == 0 && sense[13] == 0x17) /* ASC and ASCQ => cleaning requested */
320 STp->cleaning_req = 1;
322 STp->pos_unknown |= STp->device->was_reset;
324 if ((sense[0] & 0x70) == 0x70 &&
325 scode == RECOVERED_ERROR
326 #if ST_RECOVERED_WRITE_FATAL
327 && SRpnt->sr_cmnd[0] != WRITE_6
328 && SRpnt->sr_cmnd[0] != WRITE_FILEMARKS
331 STp->recover_count++;
336 if (SRpnt->sr_cmnd[0] == READ_6)
338 else if (SRpnt->sr_cmnd[0] == WRITE_6)
342 printk(ST_DEB_MSG "%s: Recovered %s error (%d).\n", name, stp,
346 if ((sense[2] & 0xe0) == 0)
353 /* Wakeup from interrupt */
354 static void st_sleep_done(Scsi_Cmnd * SCpnt)
357 Scsi_Tape *STp = container_of(SCpnt->request->rq_disk->private_data,
360 if ((STp->buffer)->writing &&
361 (SCpnt->sense_buffer[0] & 0x70) == 0x70 &&
362 (SCpnt->sense_buffer[2] & 0x40)) {
363 /* EOM at write-behind, has all been written? */
364 if ((SCpnt->sense_buffer[0] & 0x80) != 0)
365 remainder = (SCpnt->sense_buffer[3] << 24) |
366 (SCpnt->sense_buffer[4] << 16) |
367 (SCpnt->sense_buffer[5] << 8) |
368 SCpnt->sense_buffer[6];
371 if ((SCpnt->sense_buffer[2] & 0x0f) == VOLUME_OVERFLOW ||
373 (STp->buffer)->midlevel_result = SCpnt->result; /* Error */
375 (STp->buffer)->midlevel_result = INT_MAX; /* OK */
377 (STp->buffer)->midlevel_result = SCpnt->result;
378 SCpnt->request->rq_status = RQ_SCSI_DONE;
379 (STp->buffer)->last_SRpnt = SCpnt->sc_request;
380 DEB( STp->write_pending = 0; )
382 complete(SCpnt->request->waiting);
385 /* Do the scsi command. Waits until command performed if do_wait is true.
386 Otherwise write_behind_check() is used to check that the command
388 static Scsi_Request *
389 st_do_scsi(Scsi_Request * SRpnt, Scsi_Tape * STp, unsigned char *cmd, int bytes,
390 int direction, int timeout, int retries, int do_wait)
395 SRpnt = scsi_allocate_request(STp->device, GFP_ATOMIC);
397 DEBC( printk(KERN_ERR "%s: Can't get SCSI request.\n",
399 if (signal_pending(current))
400 (STp->buffer)->syscall_result = (-EINTR);
402 (STp->buffer)->syscall_result = (-EBUSY);
407 init_completion(&STp->wait);
408 SRpnt->sr_use_sg = STp->buffer->do_dio || (bytes > (STp->buffer)->frp[0].length);
409 if (SRpnt->sr_use_sg) {
410 if (!STp->buffer->do_dio)
411 buf_to_sg(STp->buffer, bytes);
412 SRpnt->sr_use_sg = (STp->buffer)->sg_segs;
413 bp = (char *) &((STp->buffer)->sg[0]);
415 bp = (STp->buffer)->b_data;
416 SRpnt->sr_data_direction = direction;
417 SRpnt->sr_cmd_len = 0;
418 SRpnt->sr_request->waiting = &(STp->wait);
419 SRpnt->sr_request->rq_status = RQ_SCSI_BUSY;
420 SRpnt->sr_request->rq_disk = STp->disk;
422 scsi_do_req(SRpnt, (void *) cmd, bp, bytes,
423 st_sleep_done, timeout, retries);
426 wait_for_completion(SRpnt->sr_request->waiting);
427 SRpnt->sr_request->waiting = NULL;
428 (STp->buffer)->syscall_result = st_chk_result(STp, SRpnt);
434 /* Handle the write-behind checking (downs the semaphore) */
435 static void write_behind_check(Scsi_Tape * STp)
440 STbuffer = STp->buffer;
443 if (STp->write_pending)
449 wait_for_completion(&(STp->wait));
450 (STp->buffer)->last_SRpnt->sr_request->waiting = NULL;
452 (STp->buffer)->syscall_result = st_chk_result(STp, (STp->buffer)->last_SRpnt);
453 scsi_release_request((STp->buffer)->last_SRpnt);
455 STbuffer->buffer_bytes -= STbuffer->writing;
456 STps = &(STp->ps[STp->partition]);
457 if (STps->drv_block >= 0) {
458 if (STp->block_size == 0)
461 STps->drv_block += STbuffer->writing / STp->block_size;
463 STbuffer->writing = 0;
469 /* Step over EOF if it has been inadvertently crossed (ioctl not used because
470 it messes up the block number). */
471 static int cross_eof(Scsi_Tape * STp, int forward)
474 unsigned char cmd[MAX_COMMAND_SIZE];
477 cmd[1] = 0x01; /* Space FileMarks */
482 cmd[2] = cmd[3] = cmd[4] = 0xff; /* -1 filemarks */
485 DEBC(printk(ST_DEB_MSG "%s: Stepping over filemark %s.\n",
486 tape_name(STp), forward ? "forward" : "backward"));
488 SRpnt = st_do_scsi(NULL, STp, cmd, 0, SCSI_DATA_NONE,
489 STp->device->timeout, MAX_RETRIES, TRUE);
491 return (STp->buffer)->syscall_result;
493 scsi_release_request(SRpnt);
496 if ((STp->buffer)->midlevel_result != 0)
497 printk(KERN_ERR "%s: Stepping over filemark %s failed.\n",
498 tape_name(STp), forward ? "forward" : "backward");
500 return (STp->buffer)->syscall_result;
504 /* Flush the write buffer (never need to write if variable blocksize). */
505 static int flush_write_buffer(Scsi_Tape * STp)
507 int offset, transfer, blks;
509 unsigned char cmd[MAX_COMMAND_SIZE];
513 if ((STp->buffer)->writing) {
514 write_behind_check(STp);
515 if ((STp->buffer)->syscall_result) {
516 DEBC(printk(ST_DEB_MSG
517 "%s: Async write error (flush) %x.\n",
518 tape_name(STp), (STp->buffer)->midlevel_result))
519 if ((STp->buffer)->midlevel_result == INT_MAX)
524 if (STp->block_size == 0)
528 if (STp->dirty == 1) {
530 offset = (STp->buffer)->buffer_bytes;
531 transfer = ((offset + STp->block_size - 1) /
532 STp->block_size) * STp->block_size;
533 DEBC(printk(ST_DEB_MSG "%s: Flushing %d bytes.\n",
534 tape_name(STp), transfer));
536 memset((STp->buffer)->b_data + offset, 0, transfer - offset);
538 memset(cmd, 0, MAX_COMMAND_SIZE);
541 blks = transfer / STp->block_size;
546 SRpnt = st_do_scsi(NULL, STp, cmd, transfer, SCSI_DATA_WRITE,
547 STp->device->timeout, MAX_WRITE_RETRIES, TRUE);
549 return (STp->buffer)->syscall_result;
551 STps = &(STp->ps[STp->partition]);
552 if ((STp->buffer)->syscall_result != 0) {
553 if ((SRpnt->sr_sense_buffer[0] & 0x70) == 0x70 &&
554 (SRpnt->sr_sense_buffer[2] & 0x40) &&
555 (SRpnt->sr_sense_buffer[2] & 0x0f) == NO_SENSE) {
557 (STp->buffer)->buffer_bytes = 0;
560 printk(KERN_ERR "%s: Error on flush.\n",
564 STps->drv_block = (-1);
566 if (STps->drv_block >= 0)
567 STps->drv_block += blks;
569 (STp->buffer)->buffer_bytes = 0;
571 scsi_release_request(SRpnt);
578 /* Flush the tape buffer. The tape will be positioned correctly unless
579 seek_next is true. */
580 static int flush_buffer(Scsi_Tape *STp, int seek_next)
582 int backspace, result;
586 STbuffer = STp->buffer;
589 * If there was a bus reset, block further access
592 if (STp->pos_unknown)
595 if (STp->ready != ST_READY)
597 STps = &(STp->ps[STp->partition]);
598 if (STps->rw == ST_WRITING) /* Writing */
599 return flush_write_buffer(STp);
601 if (STp->block_size == 0)
604 backspace = ((STp->buffer)->buffer_bytes +
605 (STp->buffer)->read_pointer) / STp->block_size -
606 ((STp->buffer)->read_pointer + STp->block_size - 1) /
608 (STp->buffer)->buffer_bytes = 0;
609 (STp->buffer)->read_pointer = 0;
612 if (STps->eof == ST_FM_HIT) {
613 result = cross_eof(STp, FALSE); /* Back over the EOF hit */
615 STps->eof = ST_NOEOF;
617 if (STps->drv_file >= 0)
622 if (!result && backspace > 0)
623 result = st_int_ioctl(STp, MTBSR, backspace);
624 } else if (STps->eof == ST_FM_HIT) {
625 if (STps->drv_file >= 0)
628 STps->eof = ST_NOEOF;
634 /* Set the mode parameters */
635 static int set_mode_densblk(Scsi_Tape * STp, ST_mode * STm)
639 char *name = tape_name(STp);
641 if (!STp->density_changed &&
642 STm->default_density >= 0 &&
643 STm->default_density != STp->density) {
644 arg = STm->default_density;
648 arg <<= MT_ST_DENSITY_SHIFT;
649 if (!STp->blksize_changed &&
650 STm->default_blksize >= 0 &&
651 STm->default_blksize != STp->block_size) {
652 arg |= STm->default_blksize;
655 arg |= STp->block_size;
657 st_int_ioctl(STp, SET_DENS_AND_BLK, arg)) {
659 "%s: Can't set default block size to %d bytes and density %x.\n",
660 name, STm->default_blksize, STm->default_density);
668 /* Lock or unlock the drive door. Don't use when Scsi_Request allocated. */
669 static int do_door_lock(Scsi_Tape * STp, int do_lock)
672 DEB(char *name = tape_name(STp);)
675 cmd = do_lock ? SCSI_IOCTL_DOORLOCK : SCSI_IOCTL_DOORUNLOCK;
676 DEBC(printk(ST_DEB_MSG "%s: %socking drive door.\n", name,
677 do_lock ? "L" : "Unl"));
678 retval = scsi_ioctl(STp->device, cmd, NULL);
680 STp->door_locked = do_lock ? ST_LOCKED_EXPLICIT : ST_UNLOCKED;
683 STp->door_locked = ST_LOCK_FAILS;
689 /* Set the internal state after reset */
690 static void reset_state(Scsi_Tape *STp)
695 STp->pos_unknown = 0;
696 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
697 STps = &(STp->ps[i]);
699 STps->eof = ST_NOEOF;
701 STps->last_block_valid = FALSE;
702 STps->drv_block = -1;
705 if (STp->can_partitions) {
706 STp->partition = find_partition(STp);
707 if (STp->partition < 0)
709 STp->new_partition = STp->partition;
713 /* Test if the drive is ready. Returns either one of the codes below or a negative system
715 #define CHKRES_READY 0
716 #define CHKRES_NEW_SESSION 1
717 #define CHKRES_NOT_READY 2
718 #define CHKRES_NO_TAPE 3
720 #define MAX_ATTENTIONS 10
722 static int test_ready(Scsi_Tape *STp, int do_wait)
724 int attentions, waits, max_wait, scode;
725 int retval = CHKRES_READY, new_session = FALSE;
726 unsigned char cmd[MAX_COMMAND_SIZE];
727 Scsi_Request *SRpnt = NULL;
729 max_wait = do_wait ? ST_BLOCK_SECONDS : 0;
731 for (attentions=waits=0; ; ) {
732 memset((void *) &cmd[0], 0, MAX_COMMAND_SIZE);
733 cmd[0] = TEST_UNIT_READY;
734 SRpnt = st_do_scsi(SRpnt, STp, cmd, 0, SCSI_DATA_NONE,
735 STp->long_timeout, MAX_READY_RETRIES, TRUE);
738 retval = (STp->buffer)->syscall_result;
742 if ((SRpnt->sr_sense_buffer[0] & 0x70) == 0x70) {
744 scode = (SRpnt->sr_sense_buffer[2] & 0x0f);
746 if (scode == UNIT_ATTENTION) { /* New media? */
748 if (attentions < MAX_ATTENTIONS) {
758 if (scode == NOT_READY) {
759 if (waits < max_wait) {
760 set_current_state(TASK_INTERRUPTIBLE);
761 schedule_timeout(HZ);
762 if (signal_pending(current)) {
770 if ((STp->device)->scsi_level >= SCSI_2 &&
771 SRpnt->sr_sense_buffer[12] == 0x3a) /* Check ASC */
772 retval = CHKRES_NO_TAPE;
774 retval = CHKRES_NOT_READY;
780 retval = (STp->buffer)->syscall_result;
782 retval = new_session ? CHKRES_NEW_SESSION : CHKRES_READY;
787 scsi_release_request(SRpnt);
792 /* See if the drive is ready and gather information about the tape. Return values:
793 < 0 negative error code from errno.h
795 1 drive not ready (possibly no tape)
797 static int check_tape(Scsi_Tape *STp, struct file *filp)
799 int i, retval, new_session = FALSE, do_wait;
800 unsigned char cmd[MAX_COMMAND_SIZE], saved_cleaning;
801 unsigned short st_flags = filp->f_flags;
802 Scsi_Request *SRpnt = NULL;
805 char *name = tape_name(STp);
806 struct inode *inode = filp->f_dentry->d_inode;
807 int mode = TAPE_MODE(inode);
809 STp->ready = ST_READY;
811 if (mode != STp->current_mode) {
812 DEBC(printk(ST_DEB_MSG "%s: Mode change from %d to %d.\n",
813 name, STp->current_mode, mode));
815 STp->current_mode = mode;
817 STm = &(STp->modes[STp->current_mode]);
819 saved_cleaning = STp->cleaning_req;
820 STp->cleaning_req = 0;
822 do_wait = ((filp->f_flags & O_NONBLOCK) == 0);
823 retval = test_ready(STp, do_wait);
828 if (retval == CHKRES_NEW_SESSION) {
829 STp->pos_unknown = 0;
830 STp->partition = STp->new_partition = 0;
831 if (STp->can_partitions)
832 STp->nbr_partitions = 1; /* This guess will be updated later
834 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
835 STps = &(STp->ps[i]);
837 STps->eof = ST_NOEOF;
839 STps->last_block_valid = FALSE;
846 STp->cleaning_req |= saved_cleaning;
848 if (retval == CHKRES_NOT_READY || retval == CHKRES_NO_TAPE) {
849 if (retval == CHKRES_NO_TAPE)
850 STp->ready = ST_NO_TAPE;
852 STp->ready = ST_NOT_READY;
854 STp->density = 0; /* Clear the erroneous "residue" */
857 STp->ps[0].drv_file = STp->ps[0].drv_block = (-1);
858 STp->partition = STp->new_partition = 0;
859 STp->door_locked = ST_UNLOCKED;
860 return CHKRES_NOT_READY;
864 if (STp->omit_blklims)
865 STp->min_block = STp->max_block = (-1);
867 memset((void *) &cmd[0], 0, MAX_COMMAND_SIZE);
868 cmd[0] = READ_BLOCK_LIMITS;
870 SRpnt = st_do_scsi(SRpnt, STp, cmd, 6, SCSI_DATA_READ, STp->device->timeout,
871 MAX_READY_RETRIES, TRUE);
873 retval = (STp->buffer)->syscall_result;
877 if (!SRpnt->sr_result && !SRpnt->sr_sense_buffer[0]) {
878 STp->max_block = ((STp->buffer)->b_data[1] << 16) |
879 ((STp->buffer)->b_data[2] << 8) | (STp->buffer)->b_data[3];
880 STp->min_block = ((STp->buffer)->b_data[4] << 8) |
881 (STp->buffer)->b_data[5];
882 if ( DEB( debugging || ) !STp->inited)
884 "%s: Block limits %d - %d bytes.\n", name,
885 STp->min_block, STp->max_block);
887 STp->min_block = STp->max_block = (-1);
888 DEBC(printk(ST_DEB_MSG "%s: Can't read block limits.\n",
893 memset((void *) &cmd[0], 0, MAX_COMMAND_SIZE);
897 SRpnt = st_do_scsi(SRpnt, STp, cmd, 12, SCSI_DATA_READ, STp->device->timeout,
898 MAX_READY_RETRIES, TRUE);
900 retval = (STp->buffer)->syscall_result;
904 if ((STp->buffer)->syscall_result != 0) {
905 DEBC(printk(ST_DEB_MSG "%s: No Mode Sense.\n", name));
906 STp->block_size = ST_DEFAULT_BLOCK; /* Educated guess (?) */
907 (STp->buffer)->syscall_result = 0; /* Prevent error propagation */
908 STp->drv_write_prot = 0;
910 DEBC(printk(ST_DEB_MSG
911 "%s: Mode sense. Length %d, medium %x, WBS %x, BLL %d\n",
913 (STp->buffer)->b_data[0], (STp->buffer)->b_data[1],
914 (STp->buffer)->b_data[2], (STp->buffer)->b_data[3]));
916 if ((STp->buffer)->b_data[3] >= 8) {
917 STp->drv_buffer = ((STp->buffer)->b_data[2] >> 4) & 7;
918 STp->density = (STp->buffer)->b_data[4];
919 STp->block_size = (STp->buffer)->b_data[9] * 65536 +
920 (STp->buffer)->b_data[10] * 256 + (STp->buffer)->b_data[11];
921 DEBC(printk(ST_DEB_MSG
922 "%s: Density %x, tape length: %x, drv buffer: %d\n",
923 name, STp->density, (STp->buffer)->b_data[5] * 65536 +
924 (STp->buffer)->b_data[6] * 256 + (STp->buffer)->b_data[7],
927 STp->drv_write_prot = ((STp->buffer)->b_data[2] & 0x80) != 0;
929 scsi_release_request(SRpnt);
933 if (STp->block_size > 0)
934 (STp->buffer)->buffer_blocks =
935 (STp->buffer)->buffer_size / STp->block_size;
937 (STp->buffer)->buffer_blocks = 1;
938 (STp->buffer)->buffer_bytes = (STp->buffer)->read_pointer = 0;
940 DEBC(printk(ST_DEB_MSG
941 "%s: Block size: %d, buffer size: %d (%d blocks).\n", name,
942 STp->block_size, (STp->buffer)->buffer_size,
943 (STp->buffer)->buffer_blocks));
945 if (STp->drv_write_prot) {
948 DEBC(printk(ST_DEB_MSG "%s: Write protected\n", name));
951 ((st_flags & O_ACCMODE) == O_WRONLY ||
952 (st_flags & O_ACCMODE) == O_RDWR)) {
958 if (STp->can_partitions && STp->nbr_partitions < 1) {
959 /* This code is reached when the device is opened for the first time
960 after the driver has been initialized with tape in the drive and the
961 partition support has been enabled. */
962 DEBC(printk(ST_DEB_MSG
963 "%s: Updating partition number in status.\n", name));
964 if ((STp->partition = find_partition(STp)) < 0) {
965 retval = STp->partition;
968 STp->new_partition = STp->partition;
969 STp->nbr_partitions = 1; /* This guess will be updated when necessary */
972 if (new_session) { /* Change the drive parameters for the new mode */
973 STp->density_changed = STp->blksize_changed = FALSE;
974 STp->compression_changed = FALSE;
975 if (!(STm->defaults_for_writes) &&
976 (retval = set_mode_densblk(STp, STm)) < 0)
979 if (STp->default_drvbuffer != 0xff) {
980 if (st_int_ioctl(STp, MTSETDRVBUFFER, STp->default_drvbuffer))
982 "%s: Can't set default drive buffering to %d.\n",
983 name, STp->default_drvbuffer);
994 \f/* Open the device. Needs to be called with BKL only because of incrementing the SCSI host
996 static int st_open(struct inode *inode, struct file *filp)
998 int i, retval = (-EIO);
1001 int dev = TAPE_NR(inode);
1004 write_lock(&st_dev_arr_lock);
1005 if (dev >= st_dev_max || scsi_tapes == NULL ||
1006 ((STp = scsi_tapes[dev]) == NULL)) {
1007 write_unlock(&st_dev_arr_lock);
1010 filp->private_data = STp;
1011 name = tape_name(STp);
1014 write_unlock(&st_dev_arr_lock);
1015 DEB( printk(ST_DEB_MSG "%s: Device already in use.\n", name); )
1019 if(scsi_device_get(STp->device)) {
1020 write_unlock(&st_dev_arr_lock);
1024 write_unlock(&st_dev_arr_lock);
1025 STp->rew_at_close = STp->autorew_dev = (iminor(inode) & 0x80) == 0;
1027 if (!scsi_block_when_processing_errors(STp->device)) {
1032 /* See that we have at least a one page buffer available */
1033 if (!enlarge_buffer(STp->buffer, PAGE_SIZE, STp->restr_dma)) {
1034 printk(KERN_WARNING "%s: Can't allocate one page tape buffer.\n",
1036 retval = (-EOVERFLOW);
1040 (STp->buffer)->writing = 0;
1041 (STp->buffer)->syscall_result = 0;
1043 STp->write_prot = ((filp->f_flags & O_ACCMODE) == O_RDONLY);
1046 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
1047 STps = &(STp->ps[i]);
1050 STp->recover_count = 0;
1051 DEB( STp->nbr_waits = STp->nbr_finished = 0;
1052 STp->nbr_requests = STp->nbr_dio = STp->nbr_pages = STp->nbr_combinable = 0; )
1054 retval = check_tape(STp, filp);
1057 if ((filp->f_flags & O_NONBLOCK) == 0 &&
1058 retval != CHKRES_READY) {
1065 normalize_buffer(STp->buffer);
1067 scsi_device_put(STp->device);
1073 /* Flush the tape buffer before close */
1074 static int st_flush(struct file *filp)
1076 int result = 0, result2;
1077 unsigned char cmd[MAX_COMMAND_SIZE];
1078 Scsi_Request *SRpnt;
1079 Scsi_Tape *STp = filp->private_data;
1080 ST_mode *STm = &(STp->modes[STp->current_mode]);
1081 ST_partstat *STps = &(STp->ps[STp->partition]);
1082 char *name = tape_name(STp);
1084 if (file_count(filp) > 1)
1087 if (STps->rw == ST_WRITING && !STp->pos_unknown) {
1088 result = flush_write_buffer(STp);
1089 if (result != 0 && result != (-ENOSPC))
1093 if (STp->can_partitions &&
1094 (result2 = switch_partition(STp)) < 0) {
1095 DEBC(printk(ST_DEB_MSG
1096 "%s: switch_partition at close failed.\n", name));
1102 DEBC( if (STp->nbr_requests)
1103 printk(KERN_WARNING "%s: Number of r/w requests %d, dio used in %d, pages %d (%d).\n",
1104 name, STp->nbr_requests, STp->nbr_dio, STp->nbr_pages, STp->nbr_combinable));
1106 if (STps->rw == ST_WRITING && !STp->pos_unknown) {
1108 DEBC(printk(ST_DEB_MSG "%s: File length %ld bytes.\n",
1109 name, (long) (filp->f_pos));
1110 printk(ST_DEB_MSG "%s: Async write waits %d, finished %d.\n",
1111 name, STp->nbr_waits, STp->nbr_finished);
1114 memset(cmd, 0, MAX_COMMAND_SIZE);
1115 cmd[0] = WRITE_FILEMARKS;
1116 cmd[4] = 1 + STp->two_fm;
1118 SRpnt = st_do_scsi(NULL, STp, cmd, 0, SCSI_DATA_NONE,
1119 STp->device->timeout, MAX_WRITE_RETRIES, TRUE);
1121 result = (STp->buffer)->syscall_result;
1125 if ((STp->buffer)->syscall_result != 0 &&
1126 ((SRpnt->sr_sense_buffer[0] & 0x70) != 0x70 ||
1127 (SRpnt->sr_sense_buffer[2] & 0x4f) != 0x40 ||
1128 ((SRpnt->sr_sense_buffer[0] & 0x80) != 0 &&
1129 (SRpnt->sr_sense_buffer[3] | SRpnt->sr_sense_buffer[4] |
1130 SRpnt->sr_sense_buffer[5] |
1131 SRpnt->sr_sense_buffer[6]) != 0))) {
1132 /* Filter out successful write at EOM */
1133 scsi_release_request(SRpnt);
1135 printk(KERN_ERR "%s: Error on write filemark.\n", name);
1139 scsi_release_request(SRpnt);
1141 if (STps->drv_file >= 0)
1143 STps->drv_block = 0;
1145 cross_eof(STp, FALSE);
1149 DEBC(printk(ST_DEB_MSG "%s: Buffer flushed, %d EOF(s) written\n",
1151 } else if (!STp->rew_at_close) {
1152 STps = &(STp->ps[STp->partition]);
1153 if (!STm->sysv || STps->rw != ST_READING) {
1155 result = flush_buffer(STp, 0);
1156 else if (STps->eof == ST_FM_HIT) {
1157 result = cross_eof(STp, FALSE);
1159 if (STps->drv_file >= 0)
1161 STps->drv_block = 0;
1164 STps->eof = ST_NOEOF;
1166 } else if ((STps->eof == ST_NOEOF &&
1167 !(result = cross_eof(STp, TRUE))) ||
1168 STps->eof == ST_FM_HIT) {
1169 if (STps->drv_file >= 0)
1171 STps->drv_block = 0;
1177 if (STp->rew_at_close) {
1178 result2 = st_int_ioctl(STp, MTREW, 1);
1186 /* Close the device and release it. BKL is not needed: this is the only thread
1187 accessing this tape. */
1188 static int st_release(struct inode *inode, struct file *filp)
1191 Scsi_Tape *STp = filp->private_data;
1193 if (STp->door_locked == ST_LOCKED_AUTO)
1194 do_door_lock(STp, 0);
1196 normalize_buffer(STp->buffer);
1197 write_lock(&st_dev_arr_lock);
1199 write_unlock(&st_dev_arr_lock);
1200 scsi_device_put(STp->device);
1205 /* The checks common to both reading and writing */
1206 static ssize_t rw_checks(Scsi_Tape *STp, struct file *filp, size_t count, loff_t *ppos)
1211 * If we are in the middle of error recovery, don't let anyone
1212 * else try and use this device. Also, if error recovery fails, it
1213 * may try and take the device offline, in which case all further
1214 * access to the device is prohibited.
1216 if (!scsi_block_when_processing_errors(STp->device)) {
1221 if (ppos != &filp->f_pos) {
1222 /* "A request was outside the capabilities of the device." */
1227 if (STp->ready != ST_READY) {
1228 if (STp->ready == ST_NO_TAPE)
1229 retval = (-ENOMEDIUM);
1235 if (! STp->modes[STp->current_mode].defined) {
1242 * If there was a bus reset, block further access
1245 if (STp->pos_unknown) {
1255 printk(ST_DEB_MSG "%s: Incorrect device.\n", tape_name(STp));
1260 if (STp->can_partitions &&
1261 (retval = switch_partition(STp)) < 0)
1264 if (STp->block_size == 0 && STp->max_block > 0 &&
1265 (count < STp->min_block || count > STp->max_block)) {
1270 if (STp->do_auto_lock && STp->door_locked == ST_UNLOCKED &&
1271 !do_door_lock(STp, 1))
1272 STp->door_locked = ST_LOCKED_AUTO;
1279 static int setup_buffering(Scsi_Tape *STp, const char __user *buf, size_t count, int is_read)
1281 int i, bufsize, retval = 0;
1282 ST_buffer *STbp = STp->buffer;
1285 i = STp->try_dio && try_rdio;
1287 i = STp->try_dio && try_wdio;
1288 if (i && ((unsigned long)buf & queue_dma_alignment(
1289 STp->device->request_queue)) == 0) {
1290 i = st_map_user_pages(&(STbp->sg[0]), STbp->use_sg,
1291 (unsigned long)buf, count, (is_read ? READ : WRITE),
1295 STbp->buffer_bytes = 0; /* can be used as transfer counter */
1298 STbp->do_dio = FALSE; /* fall back to buffering with any error */
1299 STbp->sg_segs = STbp->do_dio;
1300 STbp->frp_sg_current = 0;
1304 STp->nbr_pages += STbp->do_dio;
1305 for (i=1; i < STbp->do_dio; i++)
1306 if (page_to_pfn(STbp->sg[i].page) == page_to_pfn(STbp->sg[i-1].page) + 1)
1307 STp->nbr_combinable++;
1311 STbp->do_dio = FALSE;
1312 DEB( STp->nbr_requests++; )
1314 if (!STbp->do_dio) {
1315 if (STp->block_size)
1316 bufsize = STp->block_size > st_fixed_buffer_size ?
1317 STp->block_size : st_fixed_buffer_size;
1320 if (bufsize > STbp->buffer_size &&
1321 !enlarge_buffer(STbp, bufsize, STp->restr_dma)) {
1322 printk(KERN_WARNING "%s: Can't allocate %d byte tape buffer.\n",
1323 tape_name(STp), bufsize);
1324 retval = (-EOVERFLOW);
1327 if (STp->block_size)
1328 STbp->buffer_blocks = bufsize / STp->block_size;
1336 /* Can be called more than once after each setup_buffer() */
1337 static void release_buffering(Scsi_Tape *STp)
1343 sgl_unmap_user_pages(&(STbp->sg[0]), STbp->do_dio, FALSE);
1351 st_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
1354 ssize_t i, do_count, blks, transfer;
1356 int undone, retry_eot = 0, scode;
1358 unsigned char cmd[MAX_COMMAND_SIZE];
1359 const char __user *b_point;
1360 Scsi_Request *SRpnt = NULL;
1361 Scsi_Tape *STp = filp->private_data;
1365 char *name = tape_name(STp);
1367 if (down_interruptible(&STp->lock))
1368 return -ERESTARTSYS;
1370 retval = rw_checks(STp, filp, count, ppos);
1371 if (retval || count == 0)
1374 /* Write must be integral number of blocks */
1375 if (STp->block_size != 0 && (count % STp->block_size) != 0) {
1376 printk(KERN_WARNING "%s: Write not multiple of tape block size.\n",
1382 STm = &(STp->modes[STp->current_mode]);
1383 STps = &(STp->ps[STp->partition]);
1385 if (STp->write_prot) {
1391 if (STps->rw == ST_READING) {
1392 retval = flush_buffer(STp, 0);
1395 STps->rw = ST_WRITING;
1396 } else if (STps->rw != ST_WRITING &&
1397 STps->drv_file == 0 && STps->drv_block == 0) {
1398 if ((retval = set_mode_densblk(STp, STm)) < 0)
1400 if (STm->default_compression != ST_DONT_TOUCH &&
1401 !(STp->compression_changed)) {
1402 if (st_compression(STp, (STm->default_compression == ST_YES))) {
1403 printk(KERN_WARNING "%s: Can't set default compression.\n",
1405 if (modes_defined) {
1414 if (STbp->writing) {
1415 write_behind_check(STp);
1416 if (STbp->syscall_result) {
1417 DEBC(printk(ST_DEB_MSG "%s: Async write error (write) %x.\n",
1418 name, STbp->midlevel_result));
1419 if (STbp->midlevel_result == INT_MAX)
1420 STps->eof = ST_EOM_OK;
1422 STps->eof = ST_EOM_ERROR;
1426 if (STps->eof == ST_EOM_OK) {
1427 STps->eof = ST_EOD_1; /* allow next write */
1431 else if (STps->eof == ST_EOM_ERROR) {
1436 /* Check the buffer readability in cases where copy_user might catch
1437 the problems after some tape movement. */
1438 if (STp->block_size != 0 &&
1440 (copy_from_user(&i, buf, 1) != 0 ||
1441 copy_from_user(&i, buf + count - 1, 1) != 0)) {
1446 retval = setup_buffering(STp, buf, count, FALSE);
1452 memset(cmd, 0, MAX_COMMAND_SIZE);
1454 cmd[1] = (STp->block_size != 0);
1456 STps->rw = ST_WRITING;
1459 while (count > 0 && !retry_eot) {
1465 if (STp->block_size == 0)
1468 do_count = STbp->buffer_blocks * STp->block_size -
1470 if (do_count > count)
1474 i = append_to_buffer(b_point, STbp, do_count);
1481 filp->f_pos += do_count;
1482 b_point += do_count;
1484 async_write = STp->block_size == 0 && !STbp->do_dio &&
1485 STm->do_async_writes && STps->eof < ST_EOM_OK;
1487 if (STp->block_size != 0 && STm->do_buffer_writes &&
1488 !(STp->try_dio && try_wdio) && STps->eof < ST_EOM_OK &&
1489 STbp->buffer_bytes < STbp->buffer_size) {
1491 /* Don't write a buffer that is not full enough. */
1492 if (!async_write && count == 0)
1497 if (STp->block_size == 0)
1498 blks = transfer = do_count;
1501 blks = STbp->buffer_bytes;
1504 blks /= STp->block_size;
1505 transfer = blks * STp->block_size;
1507 cmd[2] = blks >> 16;
1511 SRpnt = st_do_scsi(SRpnt, STp, cmd, transfer, SCSI_DATA_WRITE,
1512 STp->device->timeout, MAX_WRITE_RETRIES, !async_write);
1514 retval = STbp->syscall_result;
1518 STbp->writing = transfer;
1519 STp->dirty = !(STbp->writing ==
1520 STbp->buffer_bytes);
1521 SRpnt = NULL; /* Prevent releasing this request! */
1522 DEB( STp->write_pending = 1; )
1526 if (STbp->syscall_result != 0) {
1527 DEBC(printk(ST_DEB_MSG "%s: Error on write:\n", name));
1528 if ((SRpnt->sr_sense_buffer[0] & 0x70) == 0x70 &&
1529 (SRpnt->sr_sense_buffer[2] & 0x40)) {
1530 scode = SRpnt->sr_sense_buffer[2] & 0x0f;
1531 if ((SRpnt->sr_sense_buffer[0] & 0x80) != 0)
1532 undone = (SRpnt->sr_sense_buffer[3] << 24) |
1533 (SRpnt->sr_sense_buffer[4] << 16) |
1534 (SRpnt->sr_sense_buffer[5] << 8) |
1535 SRpnt->sr_sense_buffer[6];
1536 else if (STp->block_size == 0 &&
1537 scode == VOLUME_OVERFLOW)
1541 if (STp->block_size != 0)
1542 undone *= STp->block_size;
1543 filp->f_pos -= undone;
1544 if (undone <= do_count) {
1545 /* Only data from this write is not written */
1548 if (STp->block_size)
1549 blks = (transfer - undone) / STp->block_size;
1550 STps->eof = ST_EOM_OK;
1551 /* Continue in fixed block mode if all written
1552 in this request but still something left to write
1553 (retval left to zero)
1555 if (STp->block_size == 0 ||
1556 undone > 0 || count == 0)
1557 retval = (-ENOSPC); /* EOM within current request */
1558 DEBC(printk(ST_DEB_MSG
1559 "%s: EOM with %d bytes unwritten.\n",
1562 /* EOT within data buffered earlier (possible only
1563 in fixed block mode without direct i/o) */
1564 if (!retry_eot && (SRpnt->sr_sense_buffer[0] & 1) == 0 &&
1565 (scode == NO_SENSE || scode == RECOVERED_ERROR)) {
1566 move_buffer_data(STp->buffer, transfer - undone);
1568 if (STps->drv_block >= 0) {
1569 STps->drv_block += (transfer - undone) /
1572 STps->eof = ST_EOM_OK;
1573 DEBC(printk(ST_DEB_MSG
1574 "%s: Retry write of %d bytes at EOM.\n",
1575 name, STp->buffer->buffer_bytes));
1579 /* Either error within data buffered by driver or
1582 blks = do_count = 0;
1583 STps->eof = ST_EOM_ERROR;
1584 STps->drv_block = (-1); /* Too cautious? */
1585 retval = (-EIO); /* EOM for old data */
1586 DEBC(printk(ST_DEB_MSG
1587 "%s: EOM with lost data.\n",
1592 filp->f_pos -= do_count;
1594 STps->drv_block = (-1); /* Too cautious? */
1600 if (STps->drv_block >= 0) {
1601 if (STp->block_size == 0)
1602 STps->drv_block += (do_count > 0);
1604 STps->drv_block += blks;
1607 STbp->buffer_bytes = 0;
1610 if (retval || retry_eot) {
1612 retval = total - count;
1617 if (STps->eof == ST_EOD_1)
1618 STps->eof = ST_EOM_OK;
1619 else if (STps->eof != ST_EOM_OK)
1620 STps->eof = ST_NOEOF;
1621 retval = total - count;
1625 scsi_release_request(SRpnt);
1626 release_buffering(STp);
1632 /* Read data from the tape. Returns zero in the normal case, one if the
1633 eof status has changed, and the negative error code in case of a
1634 fatal error. Otherwise updates the buffer and the eof state.
1636 Does release user buffer mapping if it is set.
1638 static long read_tape(Scsi_Tape *STp, long count, Scsi_Request ** aSRpnt)
1640 int transfer, blks, bytes;
1641 unsigned char cmd[MAX_COMMAND_SIZE];
1642 Scsi_Request *SRpnt;
1647 char *name = tape_name(STp);
1652 STm = &(STp->modes[STp->current_mode]);
1653 STps = &(STp->ps[STp->partition]);
1654 if (STps->eof == ST_FM_HIT)
1658 if (STp->block_size == 0)
1659 blks = bytes = count;
1661 if (!(STp->try_dio && try_rdio) && STm->do_read_ahead) {
1662 blks = (STp->buffer)->buffer_blocks;
1663 bytes = blks * STp->block_size;
1666 if (!STbp->do_dio && bytes > (STp->buffer)->buffer_size)
1667 bytes = (STp->buffer)->buffer_size;
1668 blks = bytes / STp->block_size;
1669 bytes = blks * STp->block_size;
1673 memset(cmd, 0, MAX_COMMAND_SIZE);
1675 cmd[1] = (STp->block_size != 0);
1676 cmd[2] = blks >> 16;
1681 SRpnt = st_do_scsi(SRpnt, STp, cmd, bytes, SCSI_DATA_READ,
1682 STp->device->timeout, MAX_RETRIES, TRUE);
1683 release_buffering(STp);
1686 return STbp->syscall_result;
1688 STbp->read_pointer = 0;
1691 /* Something to check */
1692 if (STbp->syscall_result) {
1694 DEBC(printk(ST_DEB_MSG "%s: Sense: %2x %2x %2x %2x %2x %2x %2x %2x\n",
1696 SRpnt->sr_sense_buffer[0], SRpnt->sr_sense_buffer[1],
1697 SRpnt->sr_sense_buffer[2], SRpnt->sr_sense_buffer[3],
1698 SRpnt->sr_sense_buffer[4], SRpnt->sr_sense_buffer[5],
1699 SRpnt->sr_sense_buffer[6], SRpnt->sr_sense_buffer[7]));
1700 if ((SRpnt->sr_sense_buffer[0] & 0x70) == 0x70) { /* extended sense */
1702 if ((SRpnt->sr_sense_buffer[2] & 0x0f) == BLANK_CHECK)
1703 SRpnt->sr_sense_buffer[2] &= 0xcf; /* No need for EOM in this case */
1705 if ((SRpnt->sr_sense_buffer[2] & 0xe0) != 0) { /* EOF, EOM, or ILI */
1706 /* Compute the residual count */
1707 if ((SRpnt->sr_sense_buffer[0] & 0x80) != 0)
1708 transfer = (SRpnt->sr_sense_buffer[3] << 24) |
1709 (SRpnt->sr_sense_buffer[4] << 16) |
1710 (SRpnt->sr_sense_buffer[5] << 8) |
1711 SRpnt->sr_sense_buffer[6];
1714 if (STp->block_size == 0 &&
1715 (SRpnt->sr_sense_buffer[2] & 0x0f) == MEDIUM_ERROR)
1718 if (SRpnt->sr_sense_buffer[2] & 0x20) { /* ILI */
1719 if (STp->block_size == 0) {
1720 if (transfer <= 0) {
1723 "%s: Failed to read %d byte block with %d byte transfer.\n",
1724 name, bytes - transfer, bytes);
1725 if (STps->drv_block >= 0)
1726 STps->drv_block += 1;
1727 STbp->buffer_bytes = 0;
1730 STbp->buffer_bytes = bytes - transfer;
1732 scsi_release_request(SRpnt);
1733 SRpnt = *aSRpnt = NULL;
1734 if (transfer == blks) { /* We did not get anything, error */
1735 printk(KERN_NOTICE "%s: Incorrect block size.\n", name);
1736 if (STps->drv_block >= 0)
1737 STps->drv_block += blks - transfer + 1;
1738 st_int_ioctl(STp, MTBSR, 1);
1741 /* We have some data, deliver it */
1742 STbp->buffer_bytes = (blks - transfer) *
1744 DEBC(printk(ST_DEB_MSG
1745 "%s: ILI but enough data received %ld %d.\n",
1746 name, count, STbp->buffer_bytes));
1747 if (STps->drv_block >= 0)
1748 STps->drv_block += 1;
1749 if (st_int_ioctl(STp, MTBSR, 1))
1752 } else if (SRpnt->sr_sense_buffer[2] & 0x80) { /* FM overrides EOM */
1753 if (STps->eof != ST_FM_HIT)
1754 STps->eof = ST_FM_HIT;
1756 STps->eof = ST_EOD_2;
1757 if (STp->block_size == 0)
1758 STbp->buffer_bytes = 0;
1760 STbp->buffer_bytes =
1761 bytes - transfer * STp->block_size;
1762 DEBC(printk(ST_DEB_MSG
1763 "%s: EOF detected (%d bytes read).\n",
1764 name, STbp->buffer_bytes));
1765 } else if (SRpnt->sr_sense_buffer[2] & 0x40) {
1766 if (STps->eof == ST_FM)
1767 STps->eof = ST_EOD_1;
1769 STps->eof = ST_EOM_OK;
1770 if (STp->block_size == 0)
1771 STbp->buffer_bytes = bytes - transfer;
1773 STbp->buffer_bytes =
1774 bytes - transfer * STp->block_size;
1776 DEBC(printk(ST_DEB_MSG "%s: EOM detected (%d bytes read).\n",
1777 name, STbp->buffer_bytes));
1780 /* end of EOF, EOM, ILI test */
1781 else { /* nonzero sense key */
1782 DEBC(printk(ST_DEB_MSG
1783 "%s: Tape error while reading.\n", name));
1784 STps->drv_block = (-1);
1785 if (STps->eof == ST_FM &&
1786 (SRpnt->sr_sense_buffer[2] & 0x0f) == BLANK_CHECK) {
1787 DEBC(printk(ST_DEB_MSG
1788 "%s: Zero returned for first BLANK CHECK after EOF.\n",
1790 STps->eof = ST_EOD_2; /* First BLANK_CHECK after FM */
1791 } else /* Some other extended sense code */
1795 if (STbp->buffer_bytes < 0) /* Caused by bogus sense data */
1796 STbp->buffer_bytes = 0;
1798 /* End of extended sense test */
1799 else { /* Non-extended sense */
1800 retval = STbp->syscall_result;
1804 /* End of error handling */
1805 else /* Read successful */
1806 STbp->buffer_bytes = bytes;
1808 if (STps->drv_block >= 0) {
1809 if (STp->block_size == 0)
1812 STps->drv_block += STbp->buffer_bytes / STp->block_size;
1820 st_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
1824 ssize_t i, transfer;
1825 int special, do_dio = 0;
1826 Scsi_Request *SRpnt = NULL;
1827 Scsi_Tape *STp = filp->private_data;
1830 ST_buffer *STbp = STp->buffer;
1831 DEB( char *name = tape_name(STp); )
1833 if (down_interruptible(&STp->lock))
1834 return -ERESTARTSYS;
1836 retval = rw_checks(STp, filp, count, ppos);
1837 if (retval || count == 0)
1840 STm = &(STp->modes[STp->current_mode]);
1841 if (!(STm->do_read_ahead) && STp->block_size != 0 &&
1842 (count % STp->block_size) != 0) {
1843 retval = (-EINVAL); /* Read must be integral number of blocks */
1847 STps = &(STp->ps[STp->partition]);
1848 if (STps->rw == ST_WRITING) {
1849 retval = flush_buffer(STp, 0);
1852 STps->rw = ST_READING;
1855 if (debugging && STps->eof != ST_NOEOF)
1856 printk(ST_DEB_MSG "%s: EOF/EOM flag up (%d). Bytes %d\n", name,
1857 STps->eof, STbp->buffer_bytes);
1860 retval = setup_buffering(STp, buf, count, TRUE);
1863 do_dio = STbp->do_dio;
1865 if (STbp->buffer_bytes == 0 &&
1866 STps->eof >= ST_EOD_1) {
1867 if (STps->eof < ST_EOD) {
1872 retval = (-EIO); /* EOM or Blank Check */
1877 /* Check the buffer writability before any tape movement. Don't alter
1879 if (copy_from_user(&i, buf, 1) != 0 ||
1880 copy_to_user(buf, &i, 1) != 0 ||
1881 copy_from_user(&i, buf + count - 1, 1) != 0 ||
1882 copy_to_user(buf + count - 1, &i, 1) != 0) {
1888 STps->rw = ST_READING;
1891 /* Loop until enough data in buffer or a special condition found */
1892 for (total = 0, special = 0; total < count && !special;) {
1894 /* Get new data if the buffer is empty */
1895 if (STbp->buffer_bytes == 0) {
1896 special = read_tape(STp, count - total, &SRpnt);
1897 if (special < 0) { /* No need to continue read */
1903 /* Move the data from driver buffer to user buffer */
1904 if (STbp->buffer_bytes > 0) {
1906 if (debugging && STps->eof != ST_NOEOF)
1908 "%s: EOF up (%d). Left %d, needed %d.\n", name,
1909 STps->eof, STbp->buffer_bytes,
1912 transfer = STbp->buffer_bytes < count - total ?
1913 STbp->buffer_bytes : count - total;
1915 i = from_buffer(STbp, buf, transfer);
1921 filp->f_pos += transfer;
1926 if (STp->block_size == 0)
1927 break; /* Read only one variable length block */
1929 } /* for (total = 0, special = 0;
1930 total < count && !special; ) */
1932 /* Change the eof state if no data from tape or buffer */
1934 if (STps->eof == ST_FM_HIT) {
1936 STps->drv_block = 0;
1937 if (STps->drv_file >= 0)
1939 } else if (STps->eof == ST_EOD_1) {
1940 STps->eof = ST_EOD_2;
1941 STps->drv_block = 0;
1942 if (STps->drv_file >= 0)
1944 } else if (STps->eof == ST_EOD_2)
1946 } else if (STps->eof == ST_FM)
1947 STps->eof = ST_NOEOF;
1951 if (SRpnt != NULL) {
1952 scsi_release_request(SRpnt);
1956 release_buffering(STp);
1957 STbp->buffer_bytes = 0;
1967 /* Set the driver options */
1968 static void st_log_options(Scsi_Tape * STp, ST_mode * STm, char *name)
1972 "%s: Mode %d options: buffer writes: %d, async writes: %d, read ahead: %d\n",
1973 name, STp->current_mode, STm->do_buffer_writes, STm->do_async_writes,
1974 STm->do_read_ahead);
1976 "%s: can bsr: %d, two FMs: %d, fast mteom: %d, auto lock: %d,\n",
1977 name, STp->can_bsr, STp->two_fm, STp->fast_mteom, STp->do_auto_lock);
1979 "%s: defs for wr: %d, no block limits: %d, partitions: %d, s2 log: %d\n",
1980 name, STm->defaults_for_writes, STp->omit_blklims, STp->can_partitions,
1981 STp->scsi2_logical);
1983 "%s: sysv: %d nowait: %d\n", name, STm->sysv, STp->immediate);
1984 printk(KERN_INFO "%s: debugging: %d\n",
1991 static int st_set_options(Scsi_Tape *STp, long options)
1996 char *name = tape_name(STp);
1997 struct cdev *cd0, *cd1;
1999 STm = &(STp->modes[STp->current_mode]);
2000 if (!STm->defined) {
2001 cd0 = STm->cdevs[0]; cd1 = STm->cdevs[1];
2002 memcpy(STm, &(STp->modes[0]), sizeof(ST_mode));
2003 STm->cdevs[0] = cd0; STm->cdevs[1] = cd1;
2004 modes_defined = TRUE;
2005 DEBC(printk(ST_DEB_MSG
2006 "%s: Initialized mode %d definition from mode 0\n",
2007 name, STp->current_mode));
2010 code = options & MT_ST_OPTIONS;
2011 if (code == MT_ST_BOOLEANS) {
2012 STm->do_buffer_writes = (options & MT_ST_BUFFER_WRITES) != 0;
2013 STm->do_async_writes = (options & MT_ST_ASYNC_WRITES) != 0;
2014 STm->defaults_for_writes = (options & MT_ST_DEF_WRITES) != 0;
2015 STm->do_read_ahead = (options & MT_ST_READ_AHEAD) != 0;
2016 STp->two_fm = (options & MT_ST_TWO_FM) != 0;
2017 STp->fast_mteom = (options & MT_ST_FAST_MTEOM) != 0;
2018 STp->do_auto_lock = (options & MT_ST_AUTO_LOCK) != 0;
2019 STp->can_bsr = (options & MT_ST_CAN_BSR) != 0;
2020 STp->omit_blklims = (options & MT_ST_NO_BLKLIMS) != 0;
2021 if ((STp->device)->scsi_level >= SCSI_2)
2022 STp->can_partitions = (options & MT_ST_CAN_PARTITIONS) != 0;
2023 STp->scsi2_logical = (options & MT_ST_SCSI2LOGICAL) != 0;
2024 STp->immediate = (options & MT_ST_NOWAIT) != 0;
2025 STm->sysv = (options & MT_ST_SYSV) != 0;
2026 DEB( debugging = (options & MT_ST_DEBUGGING) != 0;
2027 st_log_options(STp, STm, name); )
2028 } else if (code == MT_ST_SETBOOLEANS || code == MT_ST_CLEARBOOLEANS) {
2029 value = (code == MT_ST_SETBOOLEANS);
2030 if ((options & MT_ST_BUFFER_WRITES) != 0)
2031 STm->do_buffer_writes = value;
2032 if ((options & MT_ST_ASYNC_WRITES) != 0)
2033 STm->do_async_writes = value;
2034 if ((options & MT_ST_DEF_WRITES) != 0)
2035 STm->defaults_for_writes = value;
2036 if ((options & MT_ST_READ_AHEAD) != 0)
2037 STm->do_read_ahead = value;
2038 if ((options & MT_ST_TWO_FM) != 0)
2039 STp->two_fm = value;
2040 if ((options & MT_ST_FAST_MTEOM) != 0)
2041 STp->fast_mteom = value;
2042 if ((options & MT_ST_AUTO_LOCK) != 0)
2043 STp->do_auto_lock = value;
2044 if ((options & MT_ST_CAN_BSR) != 0)
2045 STp->can_bsr = value;
2046 if ((options & MT_ST_NO_BLKLIMS) != 0)
2047 STp->omit_blklims = value;
2048 if ((STp->device)->scsi_level >= SCSI_2 &&
2049 (options & MT_ST_CAN_PARTITIONS) != 0)
2050 STp->can_partitions = value;
2051 if ((options & MT_ST_SCSI2LOGICAL) != 0)
2052 STp->scsi2_logical = value;
2053 if ((options & MT_ST_NOWAIT) != 0)
2054 STp->immediate = value;
2055 if ((options & MT_ST_SYSV) != 0)
2058 if ((options & MT_ST_DEBUGGING) != 0)
2060 st_log_options(STp, STm, name); )
2061 } else if (code == MT_ST_WRITE_THRESHOLD) {
2062 /* Retained for compatibility */
2063 } else if (code == MT_ST_DEF_BLKSIZE) {
2064 value = (options & ~MT_ST_OPTIONS);
2065 if (value == ~MT_ST_OPTIONS) {
2066 STm->default_blksize = (-1);
2067 DEBC( printk(KERN_INFO "%s: Default block size disabled.\n", name));
2069 STm->default_blksize = value;
2070 DEBC( printk(KERN_INFO "%s: Default block size set to %d bytes.\n",
2071 name, STm->default_blksize));
2072 if (STp->ready == ST_READY) {
2073 STp->blksize_changed = FALSE;
2074 set_mode_densblk(STp, STm);
2077 } else if (code == MT_ST_TIMEOUTS) {
2078 value = (options & ~MT_ST_OPTIONS);
2079 if ((value & MT_ST_SET_LONG_TIMEOUT) != 0) {
2080 STp->long_timeout = (value & ~MT_ST_SET_LONG_TIMEOUT) * HZ;
2081 DEBC( printk(KERN_INFO "%s: Long timeout set to %d seconds.\n", name,
2082 (value & ~MT_ST_SET_LONG_TIMEOUT)));
2084 STp->device->timeout = value * HZ;
2085 DEBC( printk(KERN_INFO "%s: Normal timeout set to %d seconds.\n",
2088 } else if (code == MT_ST_SET_CLN) {
2089 value = (options & ~MT_ST_OPTIONS) & 0xff;
2091 value < EXTENDED_SENSE_START && value >= SCSI_SENSE_BUFFERSIZE)
2093 STp->cln_mode = value;
2094 STp->cln_sense_mask = (options >> 8) & 0xff;
2095 STp->cln_sense_value = (options >> 16) & 0xff;
2097 "%s: Cleaning request mode %d, mask %02x, value %02x\n",
2098 name, value, STp->cln_sense_mask, STp->cln_sense_value);
2099 } else if (code == MT_ST_DEF_OPTIONS) {
2100 code = (options & ~MT_ST_CLEAR_DEFAULT);
2101 value = (options & MT_ST_CLEAR_DEFAULT);
2102 if (code == MT_ST_DEF_DENSITY) {
2103 if (value == MT_ST_CLEAR_DEFAULT) {
2104 STm->default_density = (-1);
2105 DEBC( printk(KERN_INFO "%s: Density default disabled.\n",
2108 STm->default_density = value & 0xff;
2109 DEBC( printk(KERN_INFO "%s: Density default set to %x\n",
2110 name, STm->default_density));
2111 if (STp->ready == ST_READY) {
2112 STp->density_changed = FALSE;
2113 set_mode_densblk(STp, STm);
2116 } else if (code == MT_ST_DEF_DRVBUFFER) {
2117 if (value == MT_ST_CLEAR_DEFAULT) {
2118 STp->default_drvbuffer = 0xff;
2119 DEBC( printk(KERN_INFO
2120 "%s: Drive buffer default disabled.\n", name));
2122 STp->default_drvbuffer = value & 7;
2123 DEBC( printk(KERN_INFO
2124 "%s: Drive buffer default set to %x\n",
2125 name, STp->default_drvbuffer));
2126 if (STp->ready == ST_READY)
2127 st_int_ioctl(STp, MTSETDRVBUFFER, STp->default_drvbuffer);
2129 } else if (code == MT_ST_DEF_COMPRESSION) {
2130 if (value == MT_ST_CLEAR_DEFAULT) {
2131 STm->default_compression = ST_DONT_TOUCH;
2132 DEBC( printk(KERN_INFO
2133 "%s: Compression default disabled.\n", name));
2135 if ((value & 0xff00) != 0) {
2136 STp->c_algo = (value & 0xff00) >> 8;
2137 DEBC( printk(KERN_INFO "%s: Compression algorithm set to 0x%x.\n",
2138 name, STp->c_algo));
2140 if ((value & 0xff) != 0xff) {
2141 STm->default_compression = (value & 1 ? ST_YES : ST_NO);
2142 DEBC( printk(KERN_INFO "%s: Compression default set to %x\n",
2143 name, (value & 1)));
2144 if (STp->ready == ST_READY) {
2145 STp->compression_changed = FALSE;
2146 st_compression(STp, (STm->default_compression == ST_YES));
2157 #define MODE_HEADER_LENGTH 4
2159 /* Mode header and page byte offsets */
2160 #define MH_OFF_DATA_LENGTH 0
2161 #define MH_OFF_MEDIUM_TYPE 1
2162 #define MH_OFF_DEV_SPECIFIC 2
2163 #define MH_OFF_BDESCS_LENGTH 3
2164 #define MP_OFF_PAGE_NBR 0
2165 #define MP_OFF_PAGE_LENGTH 1
2167 /* Mode header and page bit masks */
2168 #define MH_BIT_WP 0x80
2169 #define MP_MSK_PAGE_NBR 0x3f
2171 /* Don't return block descriptors */
2172 #define MODE_SENSE_OMIT_BDESCS 0x08
2174 #define MODE_SELECT_PAGE_FORMAT 0x10
2176 /* Read a mode page into the tape buffer. The block descriptors are included
2177 if incl_block_descs is true. The page control is ored to the page number
2178 parameter, if necessary. */
2179 static int read_mode_page(Scsi_Tape *STp, int page, int omit_block_descs)
2181 unsigned char cmd[MAX_COMMAND_SIZE];
2182 Scsi_Request *SRpnt = NULL;
2184 memset(cmd, 0, MAX_COMMAND_SIZE);
2185 cmd[0] = MODE_SENSE;
2186 if (omit_block_descs)
2187 cmd[1] = MODE_SENSE_OMIT_BDESCS;
2191 SRpnt = st_do_scsi(SRpnt, STp, cmd, cmd[4], SCSI_DATA_READ,
2192 STp->device->timeout, 0, TRUE);
2194 return (STp->buffer)->syscall_result;
2196 scsi_release_request(SRpnt);
2198 return (STp->buffer)->syscall_result;
2202 /* Send the mode page in the tape buffer to the drive. Assumes that the mode data
2203 in the buffer is correctly formatted. The long timeout is used if slow is non-zero. */
2204 static int write_mode_page(Scsi_Tape *STp, int page, int slow)
2207 unsigned char cmd[MAX_COMMAND_SIZE];
2208 Scsi_Request *SRpnt = NULL;
2210 memset(cmd, 0, MAX_COMMAND_SIZE);
2211 cmd[0] = MODE_SELECT;
2212 cmd[1] = MODE_SELECT_PAGE_FORMAT;
2213 pgo = MODE_HEADER_LENGTH + (STp->buffer)->b_data[MH_OFF_BDESCS_LENGTH];
2214 cmd[4] = pgo + (STp->buffer)->b_data[pgo + MP_OFF_PAGE_LENGTH] + 2;
2216 /* Clear reserved fields */
2217 (STp->buffer)->b_data[MH_OFF_DATA_LENGTH] = 0;
2218 (STp->buffer)->b_data[MH_OFF_MEDIUM_TYPE] = 0;
2219 (STp->buffer)->b_data[MH_OFF_DEV_SPECIFIC] &= ~MH_BIT_WP;
2220 (STp->buffer)->b_data[pgo + MP_OFF_PAGE_NBR] &= MP_MSK_PAGE_NBR;
2222 SRpnt = st_do_scsi(SRpnt, STp, cmd, cmd[4], SCSI_DATA_WRITE,
2223 (slow ? STp->long_timeout : STp->device->timeout), 0, TRUE);
2225 return (STp->buffer)->syscall_result;
2227 scsi_release_request(SRpnt);
2229 return (STp->buffer)->syscall_result;
2233 #define COMPRESSION_PAGE 0x0f
2234 #define COMPRESSION_PAGE_LENGTH 16
2236 #define CP_OFF_DCE_DCC 2
2237 #define CP_OFF_C_ALGO 7
2239 #define DCE_MASK 0x80
2240 #define DCC_MASK 0x40
2241 #define RED_MASK 0x60
2244 /* Control the compression with mode page 15. Algorithm not changed if zero.
2246 The block descriptors are read and written because Sony SDT-7000 does not
2247 work without this (suggestion from Michael Schaefer <Michael.Schaefer@dlr.de>).
2248 Including block descriptors should not cause any harm to other drives. */
2250 static int st_compression(Scsi_Tape * STp, int state)
2253 int mpoffs; /* Offset to mode page start */
2254 unsigned char *b_data = (STp->buffer)->b_data;
2255 DEB( char *name = tape_name(STp); )
2257 if (STp->ready != ST_READY)
2260 /* Read the current page contents */
2261 retval = read_mode_page(STp, COMPRESSION_PAGE, FALSE);
2263 DEBC(printk(ST_DEB_MSG "%s: Compression mode page not supported.\n",
2268 mpoffs = MODE_HEADER_LENGTH + b_data[MH_OFF_BDESCS_LENGTH];
2269 DEBC(printk(ST_DEB_MSG "%s: Compression state is %d.\n", name,
2270 (b_data[mpoffs + CP_OFF_DCE_DCC] & DCE_MASK ? 1 : 0)));
2272 /* Check if compression can be changed */
2273 if ((b_data[mpoffs + CP_OFF_DCE_DCC] & DCC_MASK) == 0) {
2274 DEBC(printk(ST_DEB_MSG "%s: Compression not supported.\n", name));
2280 b_data[mpoffs + CP_OFF_DCE_DCC] |= DCE_MASK;
2281 if (STp->c_algo != 0)
2282 b_data[mpoffs + CP_OFF_C_ALGO] = STp->c_algo;
2285 b_data[mpoffs + CP_OFF_DCE_DCC] &= ~DCE_MASK;
2286 if (STp->c_algo != 0)
2287 b_data[mpoffs + CP_OFF_C_ALGO] = 0; /* no compression */
2290 retval = write_mode_page(STp, COMPRESSION_PAGE, FALSE);
2292 DEBC(printk(ST_DEB_MSG "%s: Compression change failed.\n", name));
2295 DEBC(printk(ST_DEB_MSG "%s: Compression state changed to %d.\n",
2298 STp->compression_changed = TRUE;
2303 /* Process the load and unload commands (does unload if the load code is zero) */
2304 static int do_load_unload(Scsi_Tape *STp, struct file *filp, int load_code)
2306 int retval = (-EIO), timeout;
2307 DEB( char *name = tape_name(STp); )
2308 unsigned char cmd[MAX_COMMAND_SIZE];
2310 Scsi_Request *SRpnt;
2312 if (STp->ready != ST_READY && !load_code) {
2313 if (STp->ready == ST_NO_TAPE)
2314 return (-ENOMEDIUM);
2319 memset(cmd, 0, MAX_COMMAND_SIZE);
2320 cmd[0] = START_STOP;
2324 * If arg >= 1 && arg <= 6 Enhanced load/unload in HP C1553A
2326 if (load_code >= 1 + MT_ST_HPLOADER_OFFSET
2327 && load_code <= 6 + MT_ST_HPLOADER_OFFSET) {
2328 DEBC(printk(ST_DEB_MSG "%s: Enhanced %sload slot %2d.\n",
2329 name, (cmd[4]) ? "" : "un",
2330 load_code - MT_ST_HPLOADER_OFFSET));
2331 cmd[3] = load_code - MT_ST_HPLOADER_OFFSET; /* MediaID field of C1553A */
2333 if (STp->immediate) {
2334 cmd[1] = 1; /* Don't wait for completion */
2335 timeout = STp->device->timeout;
2338 timeout = STp->long_timeout;
2342 printk(ST_DEB_MSG "%s: Unloading tape.\n", name);
2344 printk(ST_DEB_MSG "%s: Loading tape.\n", name);
2347 SRpnt = st_do_scsi(NULL, STp, cmd, 0, SCSI_DATA_NONE,
2348 timeout, MAX_RETRIES, TRUE);
2350 return (STp->buffer)->syscall_result;
2352 retval = (STp->buffer)->syscall_result;
2353 scsi_release_request(SRpnt);
2355 if (!retval) { /* SCSI command successful */
2358 STp->rew_at_close = 0;
2359 STp->ready = ST_NO_TAPE;
2362 STp->rew_at_close = STp->autorew_dev;
2363 retval = check_tape(STp, filp);
2369 STps = &(STp->ps[STp->partition]);
2370 STps->drv_file = STps->drv_block = (-1);
2377 /* Internal ioctl function */
2378 static int st_int_ioctl(Scsi_Tape *STp, unsigned int cmd_in, unsigned long arg)
2384 unsigned char cmd[MAX_COMMAND_SIZE];
2385 Scsi_Request *SRpnt;
2387 int fileno, blkno, at_sm, undone;
2388 int datalen = 0, direction = SCSI_DATA_NONE;
2389 char *name = tape_name(STp);
2391 WARN_ON(STp->buffer->do_dio != 0);
2392 if (STp->ready != ST_READY) {
2393 if (STp->ready == ST_NO_TAPE)
2394 return (-ENOMEDIUM);
2398 timeout = STp->long_timeout;
2399 STps = &(STp->ps[STp->partition]);
2400 fileno = STps->drv_file;
2401 blkno = STps->drv_block;
2402 at_sm = STps->at_sm;
2404 memset(cmd, 0, MAX_COMMAND_SIZE);
2407 chg_eof = FALSE; /* Changed from the FSF after this */
2410 cmd[1] = 0x01; /* Space FileMarks */
2411 cmd[2] = (arg >> 16);
2412 cmd[3] = (arg >> 8);
2414 DEBC(printk(ST_DEB_MSG "%s: Spacing tape forward over %d filemarks.\n",
2415 name, cmd[2] * 65536 + cmd[3] * 256 + cmd[4]));
2419 at_sm &= (arg == 0);
2422 chg_eof = FALSE; /* Changed from the FSF after this */
2425 cmd[1] = 0x01; /* Space FileMarks */
2427 cmd[2] = (ltmp >> 16);
2428 cmd[3] = (ltmp >> 8);
2433 ltmp = ltmp | (cmd[2] << 16) | (cmd[3] << 8) | cmd[4];
2435 "%s: Spacing tape backward over %ld filemarks.\n",
2440 blkno = (-1); /* We can't know the block number */
2441 at_sm &= (arg == 0);
2445 cmd[1] = 0x00; /* Space Blocks */
2446 cmd[2] = (arg >> 16);
2447 cmd[3] = (arg >> 8);
2449 DEBC(printk(ST_DEB_MSG "%s: Spacing tape forward %d blocks.\n", name,
2450 cmd[2] * 65536 + cmd[3] * 256 + cmd[4]));
2453 at_sm &= (arg == 0);
2457 cmd[1] = 0x00; /* Space Blocks */
2459 cmd[2] = (ltmp >> 16);
2460 cmd[3] = (ltmp >> 8);
2465 ltmp = ltmp | (cmd[2] << 16) | (cmd[3] << 8) | cmd[4];
2467 "%s: Spacing tape backward %ld blocks.\n", name, (-ltmp));
2471 at_sm &= (arg == 0);
2475 cmd[1] = 0x04; /* Space Setmarks */
2476 cmd[2] = (arg >> 16);
2477 cmd[3] = (arg >> 8);
2479 DEBC(printk(ST_DEB_MSG "%s: Spacing tape forward %d setmarks.\n", name,
2480 cmd[2] * 65536 + cmd[3] * 256 + cmd[4]));
2482 blkno = fileno = (-1);
2488 cmd[1] = 0x04; /* Space Setmarks */
2490 cmd[2] = (ltmp >> 16);
2491 cmd[3] = (ltmp >> 8);
2496 ltmp = ltmp | (cmd[2] << 16) | (cmd[3] << 8) | cmd[4];
2497 printk(ST_DEB_MSG "%s: Spacing tape backward %ld setmarks.\n",
2501 blkno = fileno = (-1);
2507 if (STp->write_prot)
2509 cmd[0] = WRITE_FILEMARKS;
2510 if (cmd_in == MTWSM)
2512 cmd[2] = (arg >> 16);
2513 cmd[3] = (arg >> 8);
2515 timeout = STp->device->timeout;
2517 if (cmd_in == MTWEOF)
2518 printk(ST_DEB_MSG "%s: Writing %d filemarks.\n", name,
2519 cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
2521 printk(ST_DEB_MSG "%s: Writing %d setmarks.\n", name,
2522 cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
2527 at_sm = (cmd_in == MTWSM);
2530 cmd[0] = REZERO_UNIT;
2531 if (STp->immediate) {
2532 cmd[1] = 1; /* Don't wait for completion */
2533 timeout = STp->device->timeout;
2535 DEBC(printk(ST_DEB_MSG "%s: Rewinding tape.\n", name));
2536 fileno = blkno = at_sm = 0;
2539 DEBC(printk(ST_DEB_MSG "%s: No op on tape.\n", name));
2540 return 0; /* Should do something ? */
2543 cmd[0] = START_STOP;
2544 if (STp->immediate) {
2545 cmd[1] = 1; /* Don't wait for completion */
2546 timeout = STp->device->timeout;
2549 DEBC(printk(ST_DEB_MSG "%s: Retensioning tape.\n", name));
2550 fileno = blkno = at_sm = 0;
2553 if (!STp->fast_mteom) {
2554 /* space to the end of tape */
2555 ioctl_result = st_int_ioctl(STp, MTFSF, 0x7fffff);
2556 fileno = STps->drv_file;
2557 if (STps->eof >= ST_EOD_1)
2559 /* The next lines would hide the number of spaced FileMarks
2560 That's why I inserted the previous lines. I had no luck
2561 with detecting EOM with FSF, so we go now to EOM.
2567 DEBC(printk(ST_DEB_MSG "%s: Spacing to end of recorded medium.\n",
2573 if (STp->write_prot)
2576 cmd[1] = (arg ? 1 : 0); /* Long erase with non-zero argument */
2577 if (STp->immediate) {
2578 cmd[1] |= 2; /* Don't wait for completion */
2579 timeout = STp->device->timeout;
2582 timeout = STp->long_timeout * 8;
2584 DEBC(printk(ST_DEB_MSG "%s: Erasing tape.\n", name));
2585 fileno = blkno = at_sm = 0;
2587 case MTSETBLK: /* Set block length */
2588 case MTSETDENSITY: /* Set tape density */
2589 case MTSETDRVBUFFER: /* Set drive buffering */
2590 case SET_DENS_AND_BLK: /* Set density and block size */
2592 if (STp->dirty || (STp->buffer)->buffer_bytes != 0)
2593 return (-EIO); /* Not allowed if data in buffer */
2594 if ((cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) &&
2595 (arg & MT_ST_BLKSIZE_MASK) != 0 &&
2596 STp->max_block > 0 &&
2597 ((arg & MT_ST_BLKSIZE_MASK) < STp->min_block ||
2598 (arg & MT_ST_BLKSIZE_MASK) > STp->max_block)) {
2599 printk(KERN_WARNING "%s: Illegal block size.\n", name);
2602 cmd[0] = MODE_SELECT;
2603 if ((STp->use_pf & USE_PF))
2604 cmd[1] = MODE_SELECT_PAGE_FORMAT;
2605 cmd[4] = datalen = 12;
2606 direction = SCSI_DATA_WRITE;
2608 memset((STp->buffer)->b_data, 0, 12);
2609 if (cmd_in == MTSETDRVBUFFER)
2610 (STp->buffer)->b_data[2] = (arg & 7) << 4;
2612 (STp->buffer)->b_data[2] =
2613 STp->drv_buffer << 4;
2614 (STp->buffer)->b_data[3] = 8; /* block descriptor length */
2615 if (cmd_in == MTSETDENSITY) {
2616 (STp->buffer)->b_data[4] = arg;
2617 STp->density_changed = TRUE; /* At least we tried ;-) */
2618 } else if (cmd_in == SET_DENS_AND_BLK)
2619 (STp->buffer)->b_data[4] = arg >> 24;
2621 (STp->buffer)->b_data[4] = STp->density;
2622 if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) {
2623 ltmp = arg & MT_ST_BLKSIZE_MASK;
2624 if (cmd_in == MTSETBLK)
2625 STp->blksize_changed = TRUE; /* At least we tried ;-) */
2627 ltmp = STp->block_size;
2628 (STp->buffer)->b_data[9] = (ltmp >> 16);
2629 (STp->buffer)->b_data[10] = (ltmp >> 8);
2630 (STp->buffer)->b_data[11] = ltmp;
2631 timeout = STp->device->timeout;
2633 if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK)
2635 "%s: Setting block size to %d bytes.\n", name,
2636 (STp->buffer)->b_data[9] * 65536 +
2637 (STp->buffer)->b_data[10] * 256 +
2638 (STp->buffer)->b_data[11]);
2639 if (cmd_in == MTSETDENSITY || cmd_in == SET_DENS_AND_BLK)
2641 "%s: Setting density code to %x.\n", name,
2642 (STp->buffer)->b_data[4]);
2643 if (cmd_in == MTSETDRVBUFFER)
2645 "%s: Setting drive buffer code to %d.\n", name,
2646 ((STp->buffer)->b_data[2] >> 4) & 7);
2653 SRpnt = st_do_scsi(NULL, STp, cmd, datalen, direction,
2654 timeout, MAX_RETRIES, TRUE);
2656 return (STp->buffer)->syscall_result;
2658 ioctl_result = (STp->buffer)->syscall_result;
2660 if (!ioctl_result) { /* SCSI command successful */
2661 scsi_release_request(SRpnt);
2663 STps->drv_block = blkno;
2664 STps->drv_file = fileno;
2665 STps->at_sm = at_sm;
2667 if (cmd_in == MTBSFM)
2668 ioctl_result = st_int_ioctl(STp, MTFSF, 1);
2669 else if (cmd_in == MTFSFM)
2670 ioctl_result = st_int_ioctl(STp, MTBSF, 1);
2672 if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) {
2673 int old_block_size = STp->block_size;
2674 STp->block_size = arg & MT_ST_BLKSIZE_MASK;
2675 if (STp->block_size != 0) {
2676 if (old_block_size == 0)
2677 normalize_buffer(STp->buffer);
2678 (STp->buffer)->buffer_blocks =
2679 (STp->buffer)->buffer_size / STp->block_size;
2681 (STp->buffer)->buffer_bytes = (STp->buffer)->read_pointer = 0;
2682 if (cmd_in == SET_DENS_AND_BLK)
2683 STp->density = arg >> MT_ST_DENSITY_SHIFT;
2684 } else if (cmd_in == MTSETDRVBUFFER)
2685 STp->drv_buffer = (arg & 7);
2686 else if (cmd_in == MTSETDENSITY)
2689 if (cmd_in == MTEOM)
2691 else if (cmd_in == MTFSF)
2694 STps->eof = ST_NOEOF;
2696 } else { /* SCSI command was not completely successful. Don't return
2697 from this block without releasing the SCSI command block! */
2699 if (SRpnt->sr_sense_buffer[2] & 0x40) {
2700 if (cmd_in != MTBSF && cmd_in != MTBSFM &&
2701 cmd_in != MTBSR && cmd_in != MTBSS)
2702 STps->eof = ST_EOM_OK;
2703 STps->drv_block = 0;
2706 undone = ((SRpnt->sr_sense_buffer[3] << 24) +
2707 (SRpnt->sr_sense_buffer[4] << 16) +
2708 (SRpnt->sr_sense_buffer[5] << 8) +
2709 SRpnt->sr_sense_buffer[6]);
2711 if (cmd_in == MTWEOF &&
2712 (SRpnt->sr_sense_buffer[0] & 0x70) == 0x70 &&
2713 (SRpnt->sr_sense_buffer[2] & 0x4f) == 0x40 &&
2714 ((SRpnt->sr_sense_buffer[0] & 0x80) == 0 || undone == 0)) {
2715 ioctl_result = 0; /* EOF written succesfully at EOM */
2718 STps->drv_file = fileno;
2719 STps->eof = ST_NOEOF;
2720 } else if ((cmd_in == MTFSF) || (cmd_in == MTFSFM)) {
2722 STps->drv_file = fileno - undone;
2724 STps->drv_file = fileno;
2725 STps->drv_block = 0;
2726 STps->eof = ST_NOEOF;
2727 } else if ((cmd_in == MTBSF) || (cmd_in == MTBSFM)) {
2728 if (arg > 0 && undone < 0) /* Some drives get this wrong */
2730 if (STps->drv_file >= 0)
2731 STps->drv_file = fileno + undone;
2732 STps->drv_block = 0;
2733 STps->eof = ST_NOEOF;
2734 } else if (cmd_in == MTFSR) {
2735 if (SRpnt->sr_sense_buffer[2] & 0x80) { /* Hit filemark */
2736 if (STps->drv_file >= 0)
2738 STps->drv_block = 0;
2741 if (blkno >= undone)
2742 STps->drv_block = blkno - undone;
2744 STps->drv_block = (-1);
2745 STps->eof = ST_NOEOF;
2747 } else if (cmd_in == MTBSR) {
2748 if (SRpnt->sr_sense_buffer[2] & 0x80) { /* Hit filemark */
2750 STps->drv_block = (-1);
2752 if (arg > 0 && undone < 0) /* Some drives get this wrong */
2754 if (STps->drv_block >= 0)
2755 STps->drv_block = blkno + undone;
2757 STps->eof = ST_NOEOF;
2758 } else if (cmd_in == MTEOM) {
2759 STps->drv_file = (-1);
2760 STps->drv_block = (-1);
2762 } else if (cmd_in == MTSETBLK ||
2763 cmd_in == MTSETDENSITY ||
2764 cmd_in == MTSETDRVBUFFER ||
2765 cmd_in == SET_DENS_AND_BLK) {
2766 if ((SRpnt->sr_sense_buffer[2] & 0x0f) == ILLEGAL_REQUEST &&
2767 !(STp->use_pf & PF_TESTED)) {
2768 /* Try the other possible state of Page Format if not
2770 STp->use_pf = !STp->use_pf | PF_TESTED;
2771 scsi_release_request(SRpnt);
2773 return st_int_ioctl(STp, cmd_in, arg);
2776 STps->eof = ST_NOEOF;
2778 if ((SRpnt->sr_sense_buffer[2] & 0x0f) == BLANK_CHECK)
2781 scsi_release_request(SRpnt);
2785 return ioctl_result;
2789 /* Get the tape position. If bt == 2, arg points into a kernel space mt_loc
2792 static int get_location(Scsi_Tape *STp, unsigned int *block, int *partition,
2796 unsigned char scmd[MAX_COMMAND_SIZE];
2797 Scsi_Request *SRpnt;
2798 DEB( char *name = tape_name(STp); )
2800 if (STp->ready != ST_READY)
2803 memset(scmd, 0, MAX_COMMAND_SIZE);
2804 if ((STp->device)->scsi_level < SCSI_2) {
2805 scmd[0] = QFA_REQUEST_BLOCK;
2808 scmd[0] = READ_POSITION;
2809 if (!logical && !STp->scsi2_logical)
2812 SRpnt = st_do_scsi(NULL, STp, scmd, 20, SCSI_DATA_READ, STp->device->timeout,
2813 MAX_READY_RETRIES, TRUE);
2815 return (STp->buffer)->syscall_result;
2817 if ((STp->buffer)->syscall_result != 0 ||
2818 (STp->device->scsi_level >= SCSI_2 &&
2819 ((STp->buffer)->b_data[0] & 4) != 0)) {
2820 *block = *partition = 0;
2821 DEBC(printk(ST_DEB_MSG "%s: Can't read tape position.\n", name));
2825 if ((STp->device)->scsi_level < SCSI_2) {
2826 *block = ((STp->buffer)->b_data[0] << 16)
2827 + ((STp->buffer)->b_data[1] << 8)
2828 + (STp->buffer)->b_data[2];
2831 *block = ((STp->buffer)->b_data[4] << 24)
2832 + ((STp->buffer)->b_data[5] << 16)
2833 + ((STp->buffer)->b_data[6] << 8)
2834 + (STp->buffer)->b_data[7];
2835 *partition = (STp->buffer)->b_data[1];
2836 if (((STp->buffer)->b_data[0] & 0x80) &&
2837 (STp->buffer)->b_data[1] == 0) /* BOP of partition 0 */
2838 STp->ps[0].drv_block = STp->ps[0].drv_file = 0;
2840 DEBC(printk(ST_DEB_MSG "%s: Got tape pos. blk %d part %d.\n", name,
2841 *block, *partition));
2843 scsi_release_request(SRpnt);
2850 /* Set the tape block and partition. Negative partition means that only the
2851 block should be set in vendor specific way. */
2852 static int set_location(Scsi_Tape *STp, unsigned int block, int partition,
2859 unsigned char scmd[MAX_COMMAND_SIZE];
2860 Scsi_Request *SRpnt;
2861 DEB( char *name = tape_name(STp); )
2863 if (STp->ready != ST_READY)
2865 timeout = STp->long_timeout;
2866 STps = &(STp->ps[STp->partition]);
2868 DEBC(printk(ST_DEB_MSG "%s: Setting block to %d and partition to %d.\n",
2869 name, block, partition));
2870 DEB(if (partition < 0)
2873 /* Update the location at the partition we are leaving */
2874 if ((!STp->can_partitions && partition != 0) ||
2875 partition >= ST_NBR_PARTITIONS)
2877 if (partition != STp->partition) {
2878 if (get_location(STp, &blk, &p, 1))
2879 STps->last_block_valid = FALSE;
2881 STps->last_block_valid = TRUE;
2882 STps->last_block_visited = blk;
2883 DEBC(printk(ST_DEB_MSG
2884 "%s: Visited block %d for partition %d saved.\n",
2885 name, blk, STp->partition));
2889 memset(scmd, 0, MAX_COMMAND_SIZE);
2890 if ((STp->device)->scsi_level < SCSI_2) {
2891 scmd[0] = QFA_SEEK_BLOCK;
2892 scmd[2] = (block >> 16);
2893 scmd[3] = (block >> 8);
2898 scmd[3] = (block >> 24);
2899 scmd[4] = (block >> 16);
2900 scmd[5] = (block >> 8);
2902 if (!logical && !STp->scsi2_logical)
2904 if (STp->partition != partition) {
2906 scmd[8] = partition;
2907 DEBC(printk(ST_DEB_MSG
2908 "%s: Trying to change partition from %d to %d\n",
2909 name, STp->partition, partition));
2912 if (STp->immediate) {
2913 scmd[1] |= 1; /* Don't wait for completion */
2914 timeout = STp->device->timeout;
2917 SRpnt = st_do_scsi(NULL, STp, scmd, 0, SCSI_DATA_NONE,
2918 timeout, MAX_READY_RETRIES, TRUE);
2920 return (STp->buffer)->syscall_result;
2922 STps->drv_block = STps->drv_file = (-1);
2923 STps->eof = ST_NOEOF;
2924 if ((STp->buffer)->syscall_result != 0) {
2926 if (STp->can_partitions &&
2927 (STp->device)->scsi_level >= SCSI_2 &&
2928 (p = find_partition(STp)) >= 0)
2931 if (STp->can_partitions) {
2932 STp->partition = partition;
2933 STps = &(STp->ps[partition]);
2934 if (!STps->last_block_valid ||
2935 STps->last_block_visited != block) {
2942 STps->drv_block = STps->drv_file = 0;
2946 scsi_release_request(SRpnt);
2953 /* Find the current partition number for the drive status. Called from open and
2954 returns either partition number of negative error code. */
2955 static int find_partition(Scsi_Tape *STp)
2960 if ((i = get_location(STp, &block, &partition, 1)) < 0)
2962 if (partition >= ST_NBR_PARTITIONS)
2968 /* Change the partition if necessary */
2969 static int switch_partition(Scsi_Tape *STp)
2973 if (STp->partition == STp->new_partition)
2975 STps = &(STp->ps[STp->new_partition]);
2976 if (!STps->last_block_valid)
2977 STps->last_block_visited = 0;
2978 return set_location(STp, STps->last_block_visited, STp->new_partition, 1);
2981 /* Functions for reading and writing the medium partition mode page. */
2983 #define PART_PAGE 0x11
2984 #define PART_PAGE_FIXED_LENGTH 8
2986 #define PP_OFF_MAX_ADD_PARTS 2
2987 #define PP_OFF_NBR_ADD_PARTS 3
2988 #define PP_OFF_FLAGS 4
2989 #define PP_OFF_PART_UNITS 6
2990 #define PP_OFF_RESERVED 7
2992 #define PP_BIT_IDP 0x20
2993 #define PP_MSK_PSUM_MB 0x10
2995 /* Get the number of partitions on the tape. As a side effect reads the
2996 mode page into the tape buffer. */
2997 static int nbr_partitions(Scsi_Tape *STp)
3000 DEB( char *name = tape_name(STp); )
3002 if (STp->ready != ST_READY)
3005 result = read_mode_page(STp, PART_PAGE, TRUE);
3008 DEBC(printk(ST_DEB_MSG "%s: Can't read medium partition page.\n",
3012 result = (STp->buffer)->b_data[MODE_HEADER_LENGTH +
3013 PP_OFF_NBR_ADD_PARTS] + 1;
3014 DEBC(printk(ST_DEB_MSG "%s: Number of partitions %d.\n", name, result));
3021 /* Partition the tape into two partitions if size > 0 or one partition if
3024 The block descriptors are read and written because Sony SDT-7000 does not
3025 work without this (suggestion from Michael Schaefer <Michael.Schaefer@dlr.de>).
3027 My HP C1533A drive returns only one partition size field. This is used to
3028 set the size of partition 1. There is no size field for the default partition.
3029 Michael Schaefer's Sony SDT-7000 returns two descriptors and the second is
3030 used to set the size of partition 1 (this is what the SCSI-3 standard specifies).
3031 The following algorithm is used to accommodate both drives: if the number of
3032 partition size fields is greater than the maximum number of additional partitions
3033 in the mode page, the second field is used. Otherwise the first field is used.
3035 For Seagate DDS drives the page length must be 8 when no partitions is defined
3036 and 10 when 1 partition is defined (information from Eric Lee Green). This is
3037 is acceptable also to some other old drives and enforced if the first partition
3038 size field is used for the first additional partition size.
3040 static int partition_tape(Scsi_Tape *STp, int size)
3042 char *name = tape_name(STp);
3044 int pgo, psd_cnt, psdo;
3047 result = read_mode_page(STp, PART_PAGE, FALSE);
3049 DEBC(printk(ST_DEB_MSG "%s: Can't read partition mode page.\n", name));
3052 /* The mode page is in the buffer. Let's modify it and write it. */
3053 bp = (STp->buffer)->b_data;
3054 pgo = MODE_HEADER_LENGTH + bp[MH_OFF_BDESCS_LENGTH];
3055 DEBC(printk(ST_DEB_MSG "%s: Partition page length is %d bytes.\n",
3056 name, bp[pgo + MP_OFF_PAGE_LENGTH] + 2));
3058 psd_cnt = (bp[pgo + MP_OFF_PAGE_LENGTH] + 2 - PART_PAGE_FIXED_LENGTH) / 2;
3059 psdo = pgo + PART_PAGE_FIXED_LENGTH;
3060 if (psd_cnt > bp[pgo + PP_OFF_MAX_ADD_PARTS]) {
3061 bp[psdo] = bp[psdo + 1] = 0xff; /* Rest of the tape */
3064 memset(bp + psdo, 0, bp[pgo + PP_OFF_NBR_ADD_PARTS] * 2);
3066 DEBC(printk("%s: psd_cnt %d, max.parts %d, nbr_parts %d\n", name,
3067 psd_cnt, bp[pgo + PP_OFF_MAX_ADD_PARTS],
3068 bp[pgo + PP_OFF_NBR_ADD_PARTS]));
3071 bp[pgo + PP_OFF_NBR_ADD_PARTS] = 0;
3072 if (psd_cnt <= bp[pgo + PP_OFF_MAX_ADD_PARTS])
3073 bp[pgo + MP_OFF_PAGE_LENGTH] = 6;
3074 DEBC(printk(ST_DEB_MSG "%s: Formatting tape with one partition.\n",
3077 bp[psdo] = (size >> 8) & 0xff;
3078 bp[psdo + 1] = size & 0xff;
3080 if (bp[pgo + MP_OFF_PAGE_LENGTH] < 8)
3081 bp[pgo + MP_OFF_PAGE_LENGTH] = 8;
3082 DEBC(printk(ST_DEB_MSG
3083 "%s: Formatting tape with two partitions (1 = %d MB).\n",
3086 bp[pgo + PP_OFF_PART_UNITS] = 0;
3087 bp[pgo + PP_OFF_RESERVED] = 0;
3088 bp[pgo + PP_OFF_FLAGS] = PP_BIT_IDP | PP_MSK_PSUM_MB;
3090 result = write_mode_page(STp, PART_PAGE, TRUE);
3092 printk(KERN_INFO "%s: Partitioning of tape failed.\n", name);
3101 /* The ioctl command */
3102 static int st_ioctl(struct inode *inode, struct file *file,
3103 unsigned int cmd_in, unsigned long arg)
3105 int i, cmd_nr, cmd_type, bt;
3108 Scsi_Tape *STp = file->private_data;
3111 char *name = tape_name(STp);
3112 void __user *p = (void __user *)arg;
3114 if (down_interruptible(&STp->lock))
3115 return -ERESTARTSYS;
3118 if (debugging && !STp->in_use) {
3119 printk(ST_DEB_MSG "%s: Incorrect device.\n", name);
3124 STm = &(STp->modes[STp->current_mode]);
3125 STps = &(STp->ps[STp->partition]);
3128 * If we are in the middle of error recovery, don't let anyone
3129 * else try and use this device. Also, if error recovery fails, it
3130 * may try and take the device offline, in which case all further
3131 * access to the device is prohibited.
3133 if (!scsi_block_when_processing_errors(STp->device)) {
3137 cmd_type = _IOC_TYPE(cmd_in);
3138 cmd_nr = _IOC_NR(cmd_in);
3140 if (cmd_type == _IOC_TYPE(MTIOCTOP) && cmd_nr == _IOC_NR(MTIOCTOP)) {
3143 if (_IOC_SIZE(cmd_in) != sizeof(mtc)) {
3148 i = copy_from_user(&mtc, p, sizeof(struct mtop));
3154 if (mtc.mt_op == MTSETDRVBUFFER && !capable(CAP_SYS_ADMIN)) {
3156 "%s: MTSETDRVBUFFER only allowed for root.\n", name);
3160 if (!STm->defined &&
3161 (mtc.mt_op != MTSETDRVBUFFER &&
3162 (mtc.mt_count & MT_ST_OPTIONS) == 0)) {
3167 if (!STp->pos_unknown) {
3169 if (STps->eof == ST_FM_HIT) {
3170 if (mtc.mt_op == MTFSF || mtc.mt_op == MTFSFM ||
3171 mtc.mt_op == MTEOM) {
3173 if (STps->drv_file >= 0)
3174 STps->drv_file += 1;
3175 } else if (mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM) {
3177 if (STps->drv_file >= 0)
3178 STps->drv_file += 1;
3182 if (mtc.mt_op == MTSEEK) {
3183 /* Old position must be restored if partition will be
3185 i = !STp->can_partitions ||
3186 (STp->new_partition != STp->partition);
3188 i = mtc.mt_op == MTREW || mtc.mt_op == MTOFFL ||
3189 mtc.mt_op == MTRETEN || mtc.mt_op == MTEOM ||
3190 mtc.mt_op == MTLOCK || mtc.mt_op == MTLOAD ||
3191 mtc.mt_op == MTFSF || mtc.mt_op == MTFSFM ||
3192 mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM ||
3193 mtc.mt_op == MTCOMPRESSION;
3195 i = flush_buffer(STp, i);
3202 * If there was a bus reset, block further access
3203 * to this device. If the user wants to rewind the tape,
3204 * then reset the flag and allow access again.
3206 if (mtc.mt_op != MTREW &&
3207 mtc.mt_op != MTOFFL &&
3208 mtc.mt_op != MTRETEN &&
3209 mtc.mt_op != MTERASE &&
3210 mtc.mt_op != MTSEEK &&
3211 mtc.mt_op != MTEOM) {
3216 /* remove this when the midlevel properly clears was_reset */
3217 STp->device->was_reset = 0;
3220 if (mtc.mt_op != MTNOP && mtc.mt_op != MTSETBLK &&
3221 mtc.mt_op != MTSETDENSITY && mtc.mt_op != MTWSM &&
3222 mtc.mt_op != MTSETDRVBUFFER && mtc.mt_op != MTSETPART)
3223 STps->rw = ST_IDLE; /* Prevent automatic WEOF and fsf */
3225 if (mtc.mt_op == MTOFFL && STp->door_locked != ST_UNLOCKED)
3226 do_door_lock(STp, 0); /* Ignore result! */
3228 if (mtc.mt_op == MTSETDRVBUFFER &&
3229 (mtc.mt_count & MT_ST_OPTIONS) != 0) {
3230 retval = st_set_options(STp, mtc.mt_count);
3234 if (mtc.mt_op == MTSETPART) {
3235 if (!STp->can_partitions ||
3236 mtc.mt_count < 0 || mtc.mt_count >= ST_NBR_PARTITIONS) {
3240 if (mtc.mt_count >= STp->nbr_partitions &&
3241 (STp->nbr_partitions = nbr_partitions(STp)) < 0) {
3245 if (mtc.mt_count >= STp->nbr_partitions) {
3249 STp->new_partition = mtc.mt_count;
3254 if (mtc.mt_op == MTMKPART) {
3255 if (!STp->can_partitions) {
3259 if ((i = st_int_ioctl(STp, MTREW, 0)) < 0 ||
3260 (i = partition_tape(STp, mtc.mt_count)) < 0) {
3264 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
3265 STp->ps[i].rw = ST_IDLE;
3266 STp->ps[i].at_sm = 0;
3267 STp->ps[i].last_block_valid = FALSE;
3269 STp->partition = STp->new_partition = 0;
3270 STp->nbr_partitions = 1; /* Bad guess ?-) */
3271 STps->drv_block = STps->drv_file = 0;
3276 if (mtc.mt_op == MTSEEK) {
3277 i = set_location(STp, mtc.mt_count, STp->new_partition, 0);
3278 if (!STp->can_partitions)
3279 STp->ps[0].rw = ST_IDLE;
3284 if (mtc.mt_op == MTUNLOAD || mtc.mt_op == MTOFFL) {
3285 retval = do_load_unload(STp, file, 0);
3289 if (mtc.mt_op == MTLOAD) {
3290 retval = do_load_unload(STp, file, max(1, mtc.mt_count));
3294 if (mtc.mt_op == MTLOCK || mtc.mt_op == MTUNLOCK) {
3295 retval = do_door_lock(STp, (mtc.mt_op == MTLOCK));
3299 if (STp->can_partitions && STp->ready == ST_READY &&
3300 (i = switch_partition(STp)) < 0) {
3305 if (mtc.mt_op == MTCOMPRESSION)
3306 retval = st_compression(STp, (mtc.mt_count & 1));
3308 retval = st_int_ioctl(STp, mtc.mt_op, mtc.mt_count);
3311 if (!STm->defined) {
3316 if ((i = flush_buffer(STp, FALSE)) < 0) {
3320 if (STp->can_partitions &&
3321 (i = switch_partition(STp)) < 0) {
3326 if (cmd_type == _IOC_TYPE(MTIOCGET) && cmd_nr == _IOC_NR(MTIOCGET)) {
3327 struct mtget mt_status;
3329 if (_IOC_SIZE(cmd_in) != sizeof(struct mtget)) {
3334 mt_status.mt_type = STp->tape_type;
3335 mt_status.mt_dsreg =
3336 ((STp->block_size << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK) |
3337 ((STp->density << MT_ST_DENSITY_SHIFT) & MT_ST_DENSITY_MASK);
3338 mt_status.mt_blkno = STps->drv_block;
3339 mt_status.mt_fileno = STps->drv_file;
3340 if (STp->block_size != 0) {
3341 if (STps->rw == ST_WRITING)
3342 mt_status.mt_blkno +=
3343 (STp->buffer)->buffer_bytes / STp->block_size;
3344 else if (STps->rw == ST_READING)
3345 mt_status.mt_blkno -=
3346 ((STp->buffer)->buffer_bytes +
3347 STp->block_size - 1) / STp->block_size;
3350 mt_status.mt_gstat = 0;
3351 if (STp->drv_write_prot)
3352 mt_status.mt_gstat |= GMT_WR_PROT(0xffffffff);
3353 if (mt_status.mt_blkno == 0) {
3354 if (mt_status.mt_fileno == 0)
3355 mt_status.mt_gstat |= GMT_BOT(0xffffffff);
3357 mt_status.mt_gstat |= GMT_EOF(0xffffffff);
3359 mt_status.mt_erreg = (STp->recover_reg << MT_ST_SOFTERR_SHIFT);
3360 mt_status.mt_resid = STp->partition;
3361 if (STps->eof == ST_EOM_OK || STps->eof == ST_EOM_ERROR)
3362 mt_status.mt_gstat |= GMT_EOT(0xffffffff);
3363 else if (STps->eof >= ST_EOM_OK)
3364 mt_status.mt_gstat |= GMT_EOD(0xffffffff);
3365 if (STp->density == 1)
3366 mt_status.mt_gstat |= GMT_D_800(0xffffffff);
3367 else if (STp->density == 2)
3368 mt_status.mt_gstat |= GMT_D_1600(0xffffffff);
3369 else if (STp->density == 3)
3370 mt_status.mt_gstat |= GMT_D_6250(0xffffffff);
3371 if (STp->ready == ST_READY)
3372 mt_status.mt_gstat |= GMT_ONLINE(0xffffffff);
3373 if (STp->ready == ST_NO_TAPE)
3374 mt_status.mt_gstat |= GMT_DR_OPEN(0xffffffff);
3376 mt_status.mt_gstat |= GMT_SM(0xffffffff);
3377 if (STm->do_async_writes ||
3378 (STm->do_buffer_writes && STp->block_size != 0) ||
3379 STp->drv_buffer != 0)
3380 mt_status.mt_gstat |= GMT_IM_REP_EN(0xffffffff);
3381 if (STp->cleaning_req)
3382 mt_status.mt_gstat |= GMT_CLN(0xffffffff);
3384 i = copy_to_user(p, &mt_status, sizeof(struct mtget));
3390 STp->recover_reg = 0; /* Clear after read */
3393 } /* End of MTIOCGET */
3394 if (cmd_type == _IOC_TYPE(MTIOCPOS) && cmd_nr == _IOC_NR(MTIOCPOS)) {
3395 struct mtpos mt_pos;
3396 if (_IOC_SIZE(cmd_in) != sizeof(struct mtpos)) {
3400 if ((i = get_location(STp, &blk, &bt, 0)) < 0) {
3404 mt_pos.mt_blkno = blk;
3405 i = copy_to_user(p, &mt_pos, sizeof(struct mtpos));
3412 case SCSI_IOCTL_GET_IDLUN:
3413 case SCSI_IOCTL_GET_BUS_NUMBER:
3416 i = scsi_cmd_ioctl(STp->disk, cmd_in, p);
3421 return scsi_ioctl(STp->device, cmd_in, p);
3429 /* Try to allocate a new tape buffer. Calling function must not hold
3432 new_tape_buffer(int from_initialization, int need_dma, int max_sg)
3434 int i, priority, got = 0, segs = 0;
3437 if (from_initialization)
3438 priority = GFP_ATOMIC;
3440 priority = GFP_KERNEL;
3442 i = sizeof(ST_buffer) + (max_sg - 1) * sizeof(struct scatterlist) +
3443 max_sg * sizeof(struct st_buf_fragment);
3444 tb = kmalloc(i, priority);
3446 printk(KERN_NOTICE "st: Can't allocate new tape buffer.\n");
3450 tb->frp_segs = tb->orig_frp_segs = segs;
3451 tb->use_sg = max_sg;
3453 tb->b_data = page_address(tb->sg[0].page);
3454 tb->frp = (struct st_buf_fragment *)(&(tb->sg[0]) + max_sg);
3458 tb->buffer_size = got;
3464 /* Try to allocate enough space in the tape buffer */
3465 static int enlarge_buffer(ST_buffer * STbuffer, int new_size, int need_dma)
3467 int segs, nbr, max_segs, b_size, priority, order, got;
3469 if (new_size <= STbuffer->buffer_size)
3472 if (STbuffer->buffer_size <= PAGE_SIZE)
3473 normalize_buffer(STbuffer); /* Avoid extra segment */
3475 max_segs = STbuffer->use_sg;
3476 nbr = max_segs - STbuffer->frp_segs;
3480 priority = GFP_KERNEL | __GFP_NOWARN;
3482 priority |= GFP_DMA;
3483 for (b_size = PAGE_SIZE, order=0;
3484 b_size < new_size - STbuffer->buffer_size;
3485 order++, b_size *= 2)
3488 for (segs = STbuffer->frp_segs, got = STbuffer->buffer_size;
3489 segs < max_segs && got < new_size;) {
3490 STbuffer->frp[segs].page = alloc_pages(priority, order);
3491 if (STbuffer->frp[segs].page == NULL) {
3492 if (new_size - got <= (max_segs - segs) * b_size / 2) {
3493 b_size /= 2; /* Large enough for the rest of the buffers */
3497 DEB(STbuffer->buffer_size = got);
3498 normalize_buffer(STbuffer);
3501 STbuffer->frp[segs].length = b_size;
3502 STbuffer->frp_segs += 1;
3504 STbuffer->buffer_size = got;
3507 STbuffer->b_data = page_address(STbuffer->frp[0].page);
3513 /* Release the extra buffer */
3514 static void normalize_buffer(ST_buffer * STbuffer)
3518 for (i = STbuffer->orig_frp_segs; i < STbuffer->frp_segs; i++) {
3519 order = get_order(STbuffer->frp[i].length);
3520 __free_pages(STbuffer->frp[i].page, order);
3521 STbuffer->buffer_size -= STbuffer->frp[i].length;
3523 STbuffer->frp_segs = STbuffer->orig_frp_segs;
3524 STbuffer->frp_sg_current = 0;
3528 /* Move data from the user buffer to the tape buffer. Returns zero (success) or
3529 negative error code. */
3530 static int append_to_buffer(const char __user *ubp, ST_buffer * st_bp, int do_count)
3532 int i, cnt, res, offset;
3534 for (i = 0, offset = st_bp->buffer_bytes;
3535 i < st_bp->frp_segs && offset >= st_bp->frp[i].length; i++)
3536 offset -= st_bp->frp[i].length;
3537 if (i == st_bp->frp_segs) { /* Should never happen */
3538 printk(KERN_WARNING "st: append_to_buffer offset overflow.\n");
3541 for (; i < st_bp->frp_segs && do_count > 0; i++) {
3542 cnt = st_bp->frp[i].length - offset < do_count ?
3543 st_bp->frp[i].length - offset : do_count;
3544 res = copy_from_user(page_address(st_bp->frp[i].page) + offset, ubp, cnt);
3548 st_bp->buffer_bytes += cnt;
3552 if (do_count) /* Should never happen */
3559 /* Move data from the tape buffer to the user buffer. Returns zero (success) or
3560 negative error code. */
3561 static int from_buffer(ST_buffer * st_bp, char __user *ubp, int do_count)
3563 int i, cnt, res, offset;
3565 for (i = 0, offset = st_bp->read_pointer;
3566 i < st_bp->frp_segs && offset >= st_bp->frp[i].length; i++)
3567 offset -= st_bp->frp[i].length;
3568 if (i == st_bp->frp_segs) { /* Should never happen */
3569 printk(KERN_WARNING "st: from_buffer offset overflow.\n");
3572 for (; i < st_bp->frp_segs && do_count > 0; i++) {
3573 cnt = st_bp->frp[i].length - offset < do_count ?
3574 st_bp->frp[i].length - offset : do_count;
3575 res = copy_to_user(ubp, page_address(st_bp->frp[i].page) + offset, cnt);
3579 st_bp->buffer_bytes -= cnt;
3580 st_bp->read_pointer += cnt;
3584 if (do_count) /* Should never happen */
3591 /* Move data towards start of buffer */
3592 static void move_buffer_data(ST_buffer * st_bp, int offset)
3594 int src_seg, dst_seg, src_offset = 0, dst_offset;
3600 total=st_bp->buffer_bytes - offset;
3601 for (src_seg=0; src_seg < st_bp->frp_segs; src_seg++) {
3602 src_offset = offset;
3603 if (src_offset < st_bp->frp[src_seg].length)
3605 offset -= st_bp->frp[src_seg].length;
3608 st_bp->buffer_bytes = st_bp->read_pointer = total;
3609 for (dst_seg=dst_offset=0; total > 0; ) {
3610 count = min(st_bp->frp[dst_seg].length - dst_offset,
3611 st_bp->frp[src_seg].length - src_offset);
3612 memmove(page_address(st_bp->frp[dst_seg].page) + dst_offset,
3613 page_address(st_bp->frp[src_seg].page) + src_offset, count);
3614 src_offset += count;
3615 if (src_offset >= st_bp->frp[src_seg].length) {
3619 dst_offset += count;
3620 if (dst_offset >= st_bp->frp[dst_seg].length) {
3629 /* Fill the s/g list up to the length required for this transfer */
3630 static void buf_to_sg(ST_buffer *STbp, unsigned int length)
3634 struct scatterlist *sg;
3635 struct st_buf_fragment *frp;
3637 if (length == STbp->frp_sg_current)
3638 return; /* work already done */
3640 sg = &(STbp->sg[0]);
3642 for (i=count=0; count < length; i++) {
3643 sg[i].page = frp[i].page;
3644 if (length - count > frp[i].length)
3645 sg[i].length = frp[i].length;
3647 sg[i].length = length - count;
3648 count += sg[i].length;
3652 STbp->frp_sg_current = length;
3656 /* Validate the options from command line or module parameters */
3657 static void validate_options(void)
3660 st_fixed_buffer_size = buffer_kbs * ST_KILOBYTE;
3661 if (max_sg_segs >= ST_FIRST_SG)
3662 st_max_sg_segs = max_sg_segs;
3666 /* Set the boot options. Syntax is defined in Documenation/scsi/st.txt.
3668 static int __init st_setup(char *str)
3670 int i, len, ints[5];
3673 stp = get_options(str, ARRAY_SIZE(ints), ints);
3676 for (i = 0; i < ints[0] && i < ARRAY_SIZE(parms); i++)
3678 *parms[i].val = ints[i + 1];
3680 while (stp != NULL) {
3681 for (i = 0; i < ARRAY_SIZE(parms); i++) {
3682 len = strlen(parms[i].name);
3683 if (!strncmp(stp, parms[i].name, len) &&
3684 (*(stp + len) == ':' || *(stp + len) == '=')) {
3687 simple_strtoul(stp + len + 1, NULL, 0);
3689 printk(KERN_WARNING "st: Obsolete parameter %s\n",
3694 if (i >= sizeof(parms) / sizeof(struct st_dev_parm))
3695 printk(KERN_WARNING "st: invalid parameter in '%s'\n",
3697 stp = strchr(stp, ',');
3708 __setup("st=", st_setup);
3712 static struct file_operations st_fops =
3714 .owner = THIS_MODULE,
3720 .release = st_release,
3723 static int st_probe(struct device *dev)
3725 struct scsi_device *SDp = to_scsi_device(dev);
3726 struct gendisk *disk = NULL;
3727 struct cdev *cdev = NULL;
3728 Scsi_Tape *tpnt = NULL;
3732 int i, j, mode, dev_num, error;
3736 if (SDp->type != TYPE_TAPE)
3738 if ((stp = st_incompatible(SDp))) {
3740 "st: Found incompatible tape at scsi%d, channel %d, id %d, lun %d\n",
3741 SDp->host->host_no, SDp->channel, SDp->id, SDp->lun);
3742 printk(KERN_INFO "st: The suggested driver is %s.\n", stp);
3746 i = SDp->host->sg_tablesize;
3747 if (st_max_sg_segs < i)
3749 buffer = new_tape_buffer(TRUE, (SDp->host)->unchecked_isa_dma, i);
3750 if (buffer == NULL) {
3751 printk(KERN_ERR "st: Can't allocate new tape buffer. Device not attached.\n");
3755 disk = alloc_disk(1);
3757 printk(KERN_ERR "st: out of memory. Device not attached.\n");
3758 goto out_buffer_free;
3761 write_lock(&st_dev_arr_lock);
3762 if (st_nr_dev >= st_dev_max) {
3766 tmp_dev_max = max(st_nr_dev * 2, 8);
3767 if (tmp_dev_max > ST_MAX_TAPES)
3768 tmp_dev_max = ST_MAX_TAPES;
3769 if (tmp_dev_max <= st_nr_dev) {
3770 write_unlock(&st_dev_arr_lock);
3771 printk(KERN_ERR "st: Too many tape devices (max. %d).\n",
3776 tmp_da = kmalloc(tmp_dev_max * sizeof(Scsi_Tape *), GFP_ATOMIC);
3777 if (tmp_da == NULL) {
3778 write_unlock(&st_dev_arr_lock);
3779 printk(KERN_ERR "st: Can't extend device array.\n");
3783 memset(tmp_da, 0, tmp_dev_max * sizeof(Scsi_Tape *));
3784 if (scsi_tapes != NULL) {
3785 memcpy(tmp_da, scsi_tapes,
3786 st_dev_max * sizeof(Scsi_Tape *));
3789 scsi_tapes = tmp_da;
3791 st_dev_max = tmp_dev_max;
3794 for (i = 0; i < st_dev_max; i++)
3795 if (scsi_tapes[i] == NULL)
3797 if (i >= st_dev_max)
3798 panic("scsi_devices corrupt (st)");
3800 tpnt = kmalloc(sizeof(Scsi_Tape), GFP_ATOMIC);
3802 write_unlock(&st_dev_arr_lock);
3803 printk(KERN_ERR "st: Can't allocate device descriptor.\n");
3806 memset(tpnt, 0, sizeof(Scsi_Tape));
3808 sprintf(disk->disk_name, "st%d", i);
3809 disk->private_data = &tpnt->driver;
3810 disk->queue = SDp->request_queue;
3811 tpnt->driver = &st_template;
3812 scsi_tapes[i] = tpnt;
3816 if (SDp->scsi_level <= 2)
3817 tpnt->tape_type = MT_ISSCSI1;
3819 tpnt->tape_type = MT_ISSCSI2;
3821 tpnt->buffer = buffer;
3826 tpnt->drv_buffer = 1; /* Try buffering if no mode sense */
3827 tpnt->restr_dma = (SDp->host)->unchecked_isa_dma;
3828 tpnt->use_pf = (SDp->scsi_level >= SCSI_2);
3830 tpnt->do_auto_lock = ST_AUTO_LOCK;
3831 tpnt->can_bsr = (SDp->scsi_level > 2 ? 1 : ST_IN_FILE_POS); /* BSR mandatory in SCSI3 */
3832 tpnt->can_partitions = 0;
3833 tpnt->two_fm = ST_TWO_FM;
3834 tpnt->fast_mteom = ST_FAST_MTEOM;
3835 tpnt->scsi2_logical = ST_SCSI2LOGICAL;
3836 tpnt->immediate = ST_NOWAIT;
3837 tpnt->default_drvbuffer = 0xff; /* No forced buffering */
3838 tpnt->partition = 0;
3839 tpnt->new_partition = 0;
3840 tpnt->nbr_partitions = 0;
3841 tpnt->device->timeout = ST_TIMEOUT;
3842 tpnt->long_timeout = ST_LONG_TIMEOUT;
3843 tpnt->try_dio = try_direct_io && !SDp->host->unchecked_isa_dma;
3845 bounce_limit = scsi_calculate_bounce_limit(SDp->host) >> PAGE_SHIFT;
3846 if (bounce_limit > ULONG_MAX)
3847 bounce_limit = ULONG_MAX;
3848 tpnt->max_pfn = bounce_limit;
3850 for (i = 0; i < ST_NBR_MODES; i++) {
3851 STm = &(tpnt->modes[i]);
3852 STm->defined = FALSE;
3853 STm->sysv = ST_SYSV;
3854 STm->defaults_for_writes = 0;
3855 STm->do_async_writes = ST_ASYNC_WRITES;
3856 STm->do_buffer_writes = ST_BUFFER_WRITES;
3857 STm->do_read_ahead = ST_READ_AHEAD;
3858 STm->default_compression = ST_DONT_TOUCH;
3859 STm->default_blksize = (-1); /* No forced size */
3860 STm->default_density = (-1); /* No forced density */
3863 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
3864 STps = &(tpnt->ps[i]);
3866 STps->eof = ST_NOEOF;
3868 STps->last_block_valid = FALSE;
3869 STps->drv_block = (-1);
3870 STps->drv_file = (-1);
3873 tpnt->current_mode = 0;
3874 tpnt->modes[0].defined = TRUE;
3876 tpnt->density_changed = tpnt->compression_changed =
3877 tpnt->blksize_changed = FALSE;
3878 init_MUTEX(&tpnt->lock);
3881 write_unlock(&st_dev_arr_lock);
3883 for (mode = 0; mode < ST_NBR_MODES; ++mode) {
3884 STm = &(tpnt->modes[mode]);
3885 for (j=0; j < 2; j++) {
3886 cdev = cdev_alloc();
3889 "st%d: out of memory. Device not attached.\n",
3893 cdev->owner = THIS_MODULE;
3894 cdev->ops = &st_fops;
3896 error = cdev_add(cdev,
3897 MKDEV(SCSI_TAPE_MAJOR, TAPE_MINOR(dev_num, mode, j)),
3900 printk(KERN_ERR "st%d: Can't add %s-rewind mode %d\n",
3901 dev_num, j ? "non" : "auto", mode);
3902 printk(KERN_ERR "st%d: Device not attached.\n", dev_num);
3905 STm->cdevs[j] = cdev;
3908 do_create_class_files(tpnt, dev_num, mode);
3911 for (mode = 0; mode < ST_NBR_MODES; ++mode) {
3912 /* Make sure that the minor numbers corresponding to the four
3913 first modes always get the same names */
3914 i = mode << (4 - ST_NBR_MODE_BITS);
3916 devfs_mk_cdev(MKDEV(SCSI_TAPE_MAJOR, TAPE_MINOR(dev_num, mode, 0)),
3917 S_IFCHR | S_IRUGO | S_IWUGO,
3918 "%s/mt%s", SDp->devfs_name, st_formats[i]);
3919 /* No-rewind entry */
3920 devfs_mk_cdev(MKDEV(SCSI_TAPE_MAJOR, TAPE_MINOR(dev_num, mode, 1)),
3921 S_IFCHR | S_IRUGO | S_IWUGO,
3922 "%s/mt%sn", SDp->devfs_name, st_formats[i]);
3924 disk->number = devfs_register_tape(SDp->devfs_name);
3927 "Attached scsi tape %s at scsi%d, channel %d, id %d, lun %d\n",
3928 tape_name(tpnt), SDp->host->host_no, SDp->channel, SDp->id, SDp->lun);
3929 printk(KERN_WARNING "%s: try direct i/o: %s (alignment %d B), max page reachable by HBA %lu\n",
3930 tape_name(tpnt), tpnt->try_dio ? "yes" : "no",
3931 queue_dma_alignment(SDp->request_queue) + 1, tpnt->max_pfn);
3936 for (mode=0; mode < ST_NBR_MODES; mode++) {
3937 STm = &(tpnt->modes[mode]);
3938 sysfs_remove_link(&tpnt->device->sdev_gendev.kobj,
3940 for (j=0; j < 2; j++) {
3941 if (STm->cdevs[j]) {
3942 if (cdev == STm->cdevs[j])
3944 class_simple_device_remove(MKDEV(SCSI_TAPE_MAJOR,
3945 TAPE_MINOR(i, mode, j)));
3946 cdev_del(STm->cdevs[j]);
3952 write_lock(&st_dev_arr_lock);
3953 scsi_tapes[dev_num] = NULL;
3955 write_unlock(&st_dev_arr_lock);
3967 static int st_remove(struct device *dev)
3969 Scsi_Device *SDp = to_scsi_device(dev);
3973 write_lock(&st_dev_arr_lock);
3974 for (i = 0; i < st_dev_max; i++) {
3975 tpnt = scsi_tapes[i];
3976 if (tpnt != NULL && tpnt->device == SDp) {
3979 write_unlock(&st_dev_arr_lock);
3980 devfs_unregister_tape(tpnt->disk->number);
3981 sysfs_remove_link(&tpnt->device->sdev_gendev.kobj,
3983 for (mode = 0; mode < ST_NBR_MODES; ++mode) {
3984 j = mode << (4 - ST_NBR_MODE_BITS);
3985 devfs_remove("%s/mt%s", SDp->devfs_name, st_formats[j]);
3986 devfs_remove("%s/mt%sn", SDp->devfs_name, st_formats[j]);
3987 for (j=0; j < 2; j++) {
3988 class_simple_device_remove(MKDEV(SCSI_TAPE_MAJOR,
3989 TAPE_MINOR(i, mode, j)));
3990 cdev_del(tpnt->modes[mode].cdevs[j]);
3991 tpnt->modes[mode].cdevs[j] = NULL;
3994 tpnt->device = NULL;
3997 tpnt->buffer->orig_frp_segs = 0;
3998 normalize_buffer(tpnt->buffer);
3999 kfree(tpnt->buffer);
4001 put_disk(tpnt->disk);
4007 write_unlock(&st_dev_arr_lock);
4011 static void st_intr(struct scsi_cmnd *SCpnt)
4013 scsi_io_completion(SCpnt, (SCpnt->result ? 0: SCpnt->bufflen), 1);
4017 * st_init_command: only called via the scsi_cmd_ioctl (block SG_IO)
4018 * interface for REQ_BLOCK_PC commands.
4020 static int st_init_command(struct scsi_cmnd *SCpnt)
4024 if (!(SCpnt->request->flags & REQ_BLOCK_PC))
4027 rq = SCpnt->request;
4028 if (sizeof(rq->cmd) > sizeof(SCpnt->cmnd))
4031 memcpy(SCpnt->cmnd, rq->cmd, sizeof(SCpnt->cmnd));
4033 if (rq_data_dir(rq) == WRITE)
4034 SCpnt->sc_data_direction = DMA_TO_DEVICE;
4035 else if (rq->data_len)
4036 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
4038 SCpnt->sc_data_direction = DMA_NONE;
4040 SCpnt->timeout_per_command = rq->timeout;
4041 SCpnt->transfersize = rq->data_len;
4042 SCpnt->done = st_intr;
4046 static int __init init_st(void)
4051 "st: Version %s, fixed bufsize %d, s/g segs %d\n",
4052 verstr, st_fixed_buffer_size, st_max_sg_segs);
4054 st_sysfs_class = class_simple_create(THIS_MODULE, "scsi_tape");
4055 if (IS_ERR(st_sysfs_class)) {
4056 st_sysfs_class = NULL;
4057 printk(KERN_ERR "Unable create sysfs class for SCSI tapes\n");
4061 if (!register_chrdev_region(MKDEV(SCSI_TAPE_MAJOR, 0),
4062 ST_MAX_TAPE_ENTRIES, "st")) {
4063 if (scsi_register_driver(&st_template.gendrv) == 0) {
4064 do_create_driverfs_files();
4068 class_simple_destroy(st_sysfs_class);
4069 unregister_chrdev_region(MKDEV(SCSI_TAPE_MAJOR, 0),
4071 ST_MAX_TAPE_ENTRIES);
4074 printk(KERN_ERR "Unable to get major %d for SCSI tapes\n", SCSI_TAPE_MAJOR);
4078 static void __exit exit_st(void)
4081 class_simple_destroy(st_sysfs_class);
4082 st_sysfs_class = NULL;
4083 do_remove_driverfs_files();
4084 scsi_unregister_driver(&st_template.gendrv);
4085 unregister_chrdev_region(MKDEV(SCSI_TAPE_MAJOR, 0),
4086 ST_MAX_TAPE_ENTRIES);
4088 printk(KERN_INFO "st: Unloaded.\n");
4091 module_init(init_st);
4092 module_exit(exit_st);
4095 /* The sysfs driver interface. Read-only at the moment */
4096 static ssize_t st_try_direct_io_show(struct device_driver *ddp, char *buf)
4098 return snprintf(buf, PAGE_SIZE, "%d\n", try_direct_io);
4100 static DRIVER_ATTR(try_direct_io, S_IRUGO, st_try_direct_io_show, NULL);
4102 static ssize_t st_fixed_buffer_size_show(struct device_driver *ddp, char *buf)
4104 return snprintf(buf, PAGE_SIZE, "%d\n", st_fixed_buffer_size);
4106 static DRIVER_ATTR(fixed_buffer_size, S_IRUGO, st_fixed_buffer_size_show, NULL);
4108 static ssize_t st_max_sg_segs_show(struct device_driver *ddp, char *buf)
4110 return snprintf(buf, PAGE_SIZE, "%d\n", st_max_sg_segs);
4112 static DRIVER_ATTR(max_sg_segs, S_IRUGO, st_max_sg_segs_show, NULL);
4114 static ssize_t st_version_show(struct device_driver *ddd, char *buf)
4116 return snprintf(buf, PAGE_SIZE, "[%s]\n", verstr);
4118 static DRIVER_ATTR(version, S_IRUGO, st_version_show, NULL);
4120 static void do_create_driverfs_files(void)
4122 struct device_driver *driverfs = &st_template.gendrv;
4124 driver_create_file(driverfs, &driver_attr_try_direct_io);
4125 driver_create_file(driverfs, &driver_attr_fixed_buffer_size);
4126 driver_create_file(driverfs, &driver_attr_max_sg_segs);
4127 driver_create_file(driverfs, &driver_attr_version);
4130 static void do_remove_driverfs_files(void)
4132 struct device_driver *driverfs = &st_template.gendrv;
4134 driver_remove_file(driverfs, &driver_attr_version);
4135 driver_remove_file(driverfs, &driver_attr_max_sg_segs);
4136 driver_remove_file(driverfs, &driver_attr_fixed_buffer_size);
4137 driver_remove_file(driverfs, &driver_attr_try_direct_io);
4141 /* The sysfs simple class interface */
4142 static ssize_t st_defined_show(struct class_device *class_dev, char *buf)
4144 ST_mode *STm = (ST_mode *)class_get_devdata(class_dev);
4147 l = snprintf(buf, PAGE_SIZE, "%d\n", STm->defined);
4151 CLASS_DEVICE_ATTR(defined, S_IRUGO, st_defined_show, NULL);
4153 static ssize_t st_defblk_show(struct class_device *class_dev, char *buf)
4155 ST_mode *STm = (ST_mode *)class_get_devdata(class_dev);
4158 l = snprintf(buf, PAGE_SIZE, "%d\n", STm->default_blksize);
4162 CLASS_DEVICE_ATTR(default_blksize, S_IRUGO, st_defblk_show, NULL);
4164 static ssize_t st_defdensity_show(struct class_device *class_dev, char *buf)
4166 ST_mode *STm = (ST_mode *)class_get_devdata(class_dev);
4170 fmt = STm->default_density >= 0 ? "0x%02x\n" : "%d\n";
4171 l = snprintf(buf, PAGE_SIZE, fmt, STm->default_density);
4175 CLASS_DEVICE_ATTR(default_density, S_IRUGO, st_defdensity_show, NULL);
4177 static ssize_t st_defcompression_show(struct class_device *class_dev, char *buf)
4179 ST_mode *STm = (ST_mode *)class_get_devdata(class_dev);
4182 l = snprintf(buf, PAGE_SIZE, "%d\n", STm->default_compression - 1);
4186 CLASS_DEVICE_ATTR(default_compression, S_IRUGO, st_defcompression_show, NULL);
4188 static void do_create_class_files(Scsi_Tape *STp, int dev_num, int mode)
4192 struct class_device *st_class_member;
4194 if (!st_sysfs_class)
4197 for (rew=0; rew < 2; rew++) {
4198 /* Make sure that the minor numbers corresponding to the four
4199 first modes always get the same names */
4200 i = mode << (4 - ST_NBR_MODE_BITS);
4201 snprintf(name, 10, "%s%s%s", rew ? "n" : "",
4202 STp->disk->disk_name, st_formats[i]);
4204 class_simple_device_add(st_sysfs_class,
4205 MKDEV(SCSI_TAPE_MAJOR,
4206 TAPE_MINOR(dev_num, mode, rew)),
4207 &STp->device->sdev_gendev, "%s", name);
4208 if (IS_ERR(st_class_member)) {
4209 printk(KERN_WARNING "st%d: class_simple_device_add failed\n",
4213 class_set_devdata(st_class_member, &STp->modes[mode]);
4215 class_device_create_file(st_class_member,
4216 &class_device_attr_defined);
4217 class_device_create_file(st_class_member,
4218 &class_device_attr_default_blksize);
4219 class_device_create_file(st_class_member,
4220 &class_device_attr_default_density);
4221 class_device_create_file(st_class_member,
4222 &class_device_attr_default_compression);
4223 if (mode == 0 && rew == 0) {
4224 error = sysfs_create_link(&STp->device->sdev_gendev.kobj,
4225 &st_class_member->kobj,
4229 "st%d: Can't create sysfs link from SCSI device.\n",
4239 /* Pin down user pages and put them into a scatter gather list. Returns <= 0 if
4240 - mapping of all pages not successful
4241 - any page is above max_pfn
4242 (i.e., either completely successful or fails)
4244 static int st_map_user_pages(struct scatterlist *sgl, const unsigned int max_pages,
4245 unsigned long uaddr, size_t count, int rw,
4246 unsigned long max_pfn)
4250 nr_pages = sgl_map_user_pages(sgl, max_pages, uaddr, count, rw);
4254 for (i=0; i < nr_pages; i++) {
4255 if (page_to_pfn(sgl[i].page) > max_pfn)
4261 sgl_unmap_user_pages(sgl, nr_pages, FALSE);
4266 /* The following functions may be useful for a larger audience. */
4267 static int sgl_map_user_pages(struct scatterlist *sgl, const unsigned int max_pages,
4268 unsigned long uaddr, size_t count, int rw)
4271 unsigned int nr_pages;
4272 struct page **pages;
4274 nr_pages = ((uaddr & ~PAGE_MASK) + count + ~PAGE_MASK) >> PAGE_SHIFT;
4276 /* User attempted Overflow! */
4277 if ((uaddr + count) < uaddr)
4281 if (nr_pages > max_pages)
4288 if ((pages = kmalloc(max_pages * sizeof(*pages), GFP_KERNEL)) == NULL)
4291 /* Try to fault in all of the necessary pages */
4292 down_read(¤t->mm->mmap_sem);
4293 /* rw==READ means read from drive, write into memory area */
4294 res = get_user_pages(
4300 0, /* don't force */
4303 up_read(¤t->mm->mmap_sem);
4305 /* Errors and no page mapped should return here */
4309 for (i=0; i < nr_pages; i++) {
4310 /* FIXME: flush superflous for rw==READ,
4311 * probably wrong function for rw==WRITE
4313 flush_dcache_page(pages[i]);
4316 /* Populate the scatter/gather list */
4317 sgl[0].page = pages[0];
4318 sgl[0].offset = uaddr & ~PAGE_MASK;
4320 sgl[0].length = PAGE_SIZE - sgl[0].offset;
4321 count -= sgl[0].length;
4322 for (i=1; i < nr_pages ; i++) {
4324 sgl[i].page = pages[i];
4325 sgl[i].length = count < PAGE_SIZE ? count : PAGE_SIZE;
4330 sgl[0].length = count;
4338 for (j=0; j < res; j++)
4339 page_cache_release(pages[j]);
4346 /* And unmap them... */
4347 static int sgl_unmap_user_pages(struct scatterlist *sgl, const unsigned int nr_pages,
4352 for (i=0; i < nr_pages; i++) {
4353 if (dirtied && !PageReserved(sgl[i].page))
4354 SetPageDirty(sgl[i].page);
4355 /* FIXME: cache flush missing for rw==READ
4356 * FIXME: call the correct reference counting function
4358 page_cache_release(sgl[i].page);