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 - 2005 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 = "20050312";
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 <linux/delay.h>
41 #include <asm/uaccess.h>
43 #include <asm/system.h>
45 #include <scsi/scsi.h>
46 #include <scsi/scsi_dbg.h>
47 #include <scsi/scsi_device.h>
48 #include <scsi/scsi_driver.h>
49 #include <scsi/scsi_eh.h>
50 #include <scsi/scsi_host.h>
51 #include <scsi/scsi_ioctl.h>
52 #include <scsi/scsi_request.h>
55 /* The driver prints some debugging information on the console if DEBUG
56 is defined and non-zero. */
60 /* The message level for the debug messages is currently set to KERN_NOTICE
61 so that people can easily see the messages. Later when the debugging messages
62 in the drivers are more widely classified, this may be changed to KERN_DEBUG. */
63 #define ST_DEB_MSG KERN_NOTICE
65 #define DEBC(a) if (debugging) { a ; }
71 #define ST_KILOBYTE 1024
73 #include "st_options.h"
76 static int buffer_kbs;
77 static int max_sg_segs;
78 static int try_direct_io = TRY_DIRECT_IO;
79 static int try_rdio = 1;
80 static int try_wdio = 1;
82 static int st_dev_max;
85 static struct class_simple *st_sysfs_class;
87 MODULE_AUTHOR("Kai Makisara");
88 MODULE_DESCRIPTION("SCSI Tape Driver");
89 MODULE_LICENSE("GPL");
91 /* Set 'perm' (4th argument) to 0 to disable module_param's definition
92 * of sysfs parameters (which module_param doesn't yet support).
93 * Sysfs parameters defined explicitly later.
95 module_param_named(buffer_kbs, buffer_kbs, int, 0);
96 MODULE_PARM_DESC(buffer_kbs, "Default driver buffer size for fixed block mode (KB; 32)");
97 module_param_named(max_sg_segs, max_sg_segs, int, 0);
98 MODULE_PARM_DESC(max_sg_segs, "Maximum number of scatter/gather segments to use (256)");
99 module_param_named(try_direct_io, try_direct_io, int, 0);
100 MODULE_PARM_DESC(try_direct_io, "Try direct I/O between user buffer and tape drive (1)");
102 /* Extra parameters for testing */
103 module_param_named(try_rdio, try_rdio, int, 0);
104 MODULE_PARM_DESC(try_rdio, "Try direct read i/o when possible");
105 module_param_named(try_wdio, try_wdio, int, 0);
106 MODULE_PARM_DESC(try_wdio, "Try direct write i/o when possible");
109 static int write_threshold_kbs; /* retained for compatibility */
110 static struct st_dev_parm {
113 } parms[] __initdata = {
115 "buffer_kbs", &buffer_kbs
117 { /* Retained for compatibility with 2.4 */
118 "write_threshold_kbs", &write_threshold_kbs
124 "try_direct_io", &try_direct_io
129 /* Restrict the number of modes so that names for all are assigned */
130 #if ST_NBR_MODES > 16
131 #error "Maximum number of modes is 16"
133 /* Bit reversed order to get same names for same minors with all
135 static char *st_formats[] = {
136 "", "r", "k", "s", "l", "t", "o", "u",
137 "m", "v", "p", "x", "a", "y", "q", "z"};
139 /* The default definitions have been moved to st_options.h */
141 #define ST_FIXED_BUFFER_SIZE (ST_FIXED_BUFFER_BLOCKS * ST_KILOBYTE)
143 /* The buffer size should fit into the 24 bits for length in the
144 6-byte SCSI read and write commands. */
145 #if ST_FIXED_BUFFER_SIZE >= (2 << 24 - 1)
146 #error "Buffer size should not exceed (2 << 24 - 1) bytes!"
149 static int debugging = DEBUG;
151 #define MAX_RETRIES 0
152 #define MAX_WRITE_RETRIES 0
153 #define MAX_READY_RETRIES 0
154 #define NO_TAPE NOT_READY
156 #define ST_TIMEOUT (900 * HZ)
157 #define ST_LONG_TIMEOUT (14000 * HZ)
159 /* Remove mode bits and auto-rewind bit (7) */
160 #define TAPE_NR(x) ( ((iminor(x) & ~255) >> (ST_NBR_MODE_BITS + 1)) | \
161 (iminor(x) & ~(-1 << ST_MODE_SHIFT)) )
162 #define TAPE_MODE(x) ((iminor(x) & ST_MODE_MASK) >> ST_MODE_SHIFT)
164 /* Construct the minor number from the device (d), mode (m), and non-rewind (n) data */
165 #define TAPE_MINOR(d, m, n) (((d & ~(255 >> (ST_NBR_MODE_BITS + 1))) << (ST_NBR_MODE_BITS + 1)) | \
166 (d & (255 >> (ST_NBR_MODE_BITS + 1))) | (m << ST_MODE_SHIFT) | ((n != 0) << 7) )
168 /* Internal ioctl to set both density (uppermost 8 bits) and blocksize (lower
170 #define SET_DENS_AND_BLK 0x10001
172 static DEFINE_RWLOCK(st_dev_arr_lock);
174 static int st_fixed_buffer_size = ST_FIXED_BUFFER_SIZE;
175 static int st_max_sg_segs = ST_MAX_SG;
177 static struct scsi_tape **scsi_tapes = NULL;
179 static int modes_defined;
181 static struct st_buffer *new_tape_buffer(int, int, int);
182 static int enlarge_buffer(struct st_buffer *, int, int);
183 static void normalize_buffer(struct st_buffer *);
184 static int append_to_buffer(const char __user *, struct st_buffer *, int);
185 static int from_buffer(struct st_buffer *, char __user *, int);
186 static void move_buffer_data(struct st_buffer *, int);
187 static void buf_to_sg(struct st_buffer *, unsigned int);
189 static int st_map_user_pages(struct scatterlist *, const unsigned int,
190 unsigned long, size_t, int, unsigned long);
191 static int sgl_map_user_pages(struct scatterlist *, const unsigned int,
192 unsigned long, size_t, int);
193 static int sgl_unmap_user_pages(struct scatterlist *, const unsigned int, int);
195 static int st_probe(struct device *);
196 static int st_remove(struct device *);
197 static int st_init_command(struct scsi_cmnd *);
199 static void do_create_driverfs_files(void);
200 static void do_remove_driverfs_files(void);
201 static void do_create_class_files(struct scsi_tape *, int, int);
203 static struct scsi_driver st_template = {
204 .owner = THIS_MODULE,
210 .init_command = st_init_command,
213 static int st_compression(struct scsi_tape *, int);
215 static int find_partition(struct scsi_tape *);
216 static int switch_partition(struct scsi_tape *);
218 static int st_int_ioctl(struct scsi_tape *, unsigned int, unsigned long);
221 #include "osst_detect.h"
222 #ifndef SIGS_FROM_OSST
223 #define SIGS_FROM_OSST \
224 {"OnStream", "SC-", "", "osst"}, \
225 {"OnStream", "DI-", "", "osst"}, \
226 {"OnStream", "DP-", "", "osst"}, \
227 {"OnStream", "USB", "", "osst"}, \
228 {"OnStream", "FW-", "", "osst"}
231 struct st_reject_data {
235 char *driver_hint; /* Name of the correct driver, NULL if unknown */
238 static struct st_reject_data reject_list[] = {
239 /* {"XXX", "Yy-", "", NULL}, example */
243 /* If the device signature is on the list of incompatible drives, the
244 function returns a pointer to the name of the correct driver (if known) */
245 static char * st_incompatible(struct scsi_device* SDp)
247 struct st_reject_data *rp;
249 for (rp=&(reject_list[0]); rp->vendor != NULL; rp++)
250 if (!strncmp(rp->vendor, SDp->vendor, strlen(rp->vendor)) &&
251 !strncmp(rp->model, SDp->model, strlen(rp->model)) &&
252 !strncmp(rp->rev, SDp->rev, strlen(rp->rev))) {
254 return rp->driver_hint;
262 static inline char *tape_name(struct scsi_tape *tape)
264 return tape->disk->disk_name;
268 static void st_analyze_sense(struct scsi_request *SRpnt, struct st_cmdstatus *s)
271 const u8 *sense = SRpnt->sr_sense_buffer;
273 s->have_sense = scsi_request_normalize_sense(SRpnt, &s->sense_hdr);
279 scsi_get_sense_info_fld(sense, SCSI_SENSE_BUFFERSIZE, &s->uremainder64);
280 switch (sense[0] & 0x7f) {
285 s->flags = sense[2] & 0xe0;
291 ucp = scsi_sense_desc_find(sense, SCSI_SENSE_BUFFERSIZE, 4);
292 s->flags = ucp ? (ucp[3] & 0xe0) : 0;
299 /* Convert the result to success code */
300 static int st_chk_result(struct scsi_tape *STp, struct scsi_request * SRpnt)
302 int result = SRpnt->sr_result;
304 DEB(const char *stp;)
305 char *name = tape_name(STp);
306 struct st_cmdstatus *cmdstatp;
311 cmdstatp = &STp->buffer->cmdstat;
312 st_analyze_sense(STp->buffer->last_SRpnt, cmdstatp);
314 if (cmdstatp->have_sense)
315 scode = STp->buffer->cmdstat.sense_hdr.sense_key;
321 printk(ST_DEB_MSG "%s: Error: %x, cmd: %x %x %x %x %x %x Len: %d\n",
323 SRpnt->sr_cmnd[0], SRpnt->sr_cmnd[1], SRpnt->sr_cmnd[2],
324 SRpnt->sr_cmnd[3], SRpnt->sr_cmnd[4], SRpnt->sr_cmnd[5],
326 if (cmdstatp->have_sense)
327 scsi_print_req_sense("st", SRpnt);
329 if (!debugging) { /* Abnormal conditions for tape */
330 if (!cmdstatp->have_sense)
332 "%s: Error %x (sugg. bt 0x%x, driver bt 0x%x, host bt 0x%x).\n",
333 name, result, suggestion(result),
334 driver_byte(result) & DRIVER_MASK, host_byte(result));
335 else if (cmdstatp->have_sense &&
337 scode != RECOVERED_ERROR &&
338 /* scode != UNIT_ATTENTION && */
339 scode != BLANK_CHECK &&
340 scode != VOLUME_OVERFLOW &&
341 SRpnt->sr_cmnd[0] != MODE_SENSE &&
342 SRpnt->sr_cmnd[0] != TEST_UNIT_READY) {
343 printk(KERN_WARNING "%s: Error with sense data: ", name);
344 scsi_print_req_sense("st", SRpnt);
348 if (cmdstatp->fixed_format &&
349 STp->cln_mode >= EXTENDED_SENSE_START) { /* Only fixed format sense */
350 if (STp->cln_sense_value)
351 STp->cleaning_req |= ((SRpnt->sr_sense_buffer[STp->cln_mode] &
352 STp->cln_sense_mask) == STp->cln_sense_value);
354 STp->cleaning_req |= ((SRpnt->sr_sense_buffer[STp->cln_mode] &
355 STp->cln_sense_mask) != 0);
357 if (cmdstatp->have_sense &&
358 cmdstatp->sense_hdr.asc == 0 && cmdstatp->sense_hdr.ascq == 0x17)
359 STp->cleaning_req = 1; /* ASC and ASCQ => cleaning requested */
361 STp->pos_unknown |= STp->device->was_reset;
363 if (cmdstatp->have_sense &&
364 scode == RECOVERED_ERROR
365 #if ST_RECOVERED_WRITE_FATAL
366 && SRpnt->sr_cmnd[0] != WRITE_6
367 && SRpnt->sr_cmnd[0] != WRITE_FILEMARKS
370 STp->recover_count++;
375 if (SRpnt->sr_cmnd[0] == READ_6)
377 else if (SRpnt->sr_cmnd[0] == WRITE_6)
381 printk(ST_DEB_MSG "%s: Recovered %s error (%d).\n", name, stp,
385 if (cmdstatp->flags == 0)
392 /* Wakeup from interrupt */
393 static void st_sleep_done(struct scsi_cmnd * SCpnt)
395 struct scsi_tape *STp = container_of(SCpnt->request->rq_disk->private_data,
396 struct scsi_tape, driver);
398 (STp->buffer)->cmdstat.midlevel_result = SCpnt->result;
399 SCpnt->request->rq_status = RQ_SCSI_DONE;
400 (STp->buffer)->last_SRpnt = SCpnt->sc_request;
401 DEB( STp->write_pending = 0; )
403 complete(SCpnt->request->waiting);
406 /* Do the scsi command. Waits until command performed if do_wait is true.
407 Otherwise write_behind_check() is used to check that the command
409 static struct scsi_request *
410 st_do_scsi(struct scsi_request * SRpnt, struct scsi_tape * STp, unsigned char *cmd,
411 int bytes, int direction, int timeout, int retries, int do_wait)
416 SRpnt = scsi_allocate_request(STp->device, GFP_ATOMIC);
418 DEBC( printk(KERN_ERR "%s: Can't get SCSI request.\n",
420 if (signal_pending(current))
421 (STp->buffer)->syscall_result = (-EINTR);
423 (STp->buffer)->syscall_result = (-EBUSY);
428 init_completion(&STp->wait);
429 SRpnt->sr_use_sg = STp->buffer->do_dio || (bytes > (STp->buffer)->frp[0].length);
430 if (SRpnt->sr_use_sg) {
431 if (!STp->buffer->do_dio)
432 buf_to_sg(STp->buffer, bytes);
433 SRpnt->sr_use_sg = (STp->buffer)->sg_segs;
434 bp = (char *) &((STp->buffer)->sg[0]);
436 bp = (STp->buffer)->b_data;
437 SRpnt->sr_data_direction = direction;
438 SRpnt->sr_cmd_len = 0;
439 SRpnt->sr_request->waiting = &(STp->wait);
440 SRpnt->sr_request->rq_status = RQ_SCSI_BUSY;
441 SRpnt->sr_request->rq_disk = STp->disk;
442 STp->buffer->cmdstat.have_sense = 0;
444 scsi_do_req(SRpnt, (void *) cmd, bp, bytes,
445 st_sleep_done, timeout, retries);
448 wait_for_completion(SRpnt->sr_request->waiting);
449 SRpnt->sr_request->waiting = NULL;
450 (STp->buffer)->syscall_result = st_chk_result(STp, SRpnt);
456 /* Handle the write-behind checking (waits for completion). Returns -ENOSPC if
457 write has been correct but EOM early warning reached, -EIO if write ended in
458 error or zero if write successful. Asynchronous writes are used only in
459 variable block mode. */
460 static int write_behind_check(struct scsi_tape * STp)
463 struct st_buffer *STbuffer;
464 struct st_partstat *STps;
465 struct st_cmdstatus *cmdstatp;
467 STbuffer = STp->buffer;
468 if (!STbuffer->writing)
472 if (STp->write_pending)
478 wait_for_completion(&(STp->wait));
479 (STp->buffer)->last_SRpnt->sr_request->waiting = NULL;
481 (STp->buffer)->syscall_result = st_chk_result(STp, (STp->buffer)->last_SRpnt);
482 scsi_release_request((STp->buffer)->last_SRpnt);
484 STbuffer->buffer_bytes -= STbuffer->writing;
485 STps = &(STp->ps[STp->partition]);
486 if (STps->drv_block >= 0) {
487 if (STp->block_size == 0)
490 STps->drv_block += STbuffer->writing / STp->block_size;
493 cmdstatp = &STbuffer->cmdstat;
494 if (STbuffer->syscall_result) {
496 if (cmdstatp->have_sense && !cmdstatp->deferred &&
497 (cmdstatp->flags & SENSE_EOM) &&
498 (cmdstatp->sense_hdr.sense_key == NO_SENSE ||
499 cmdstatp->sense_hdr.sense_key == RECOVERED_ERROR)) {
500 /* EOM at write-behind, has all data been written? */
501 if (!cmdstatp->remainder_valid ||
502 cmdstatp->uremainder64 == 0)
506 STps->drv_block = -1;
508 STbuffer->writing = 0;
510 DEB(if (debugging && retval)
511 printk(ST_DEB_MSG "%s: Async write error %x, return value %d.\n",
512 tape_name(STp), STbuffer->cmdstat.midlevel_result, retval);) /* end DEB */
518 /* Step over EOF if it has been inadvertently crossed (ioctl not used because
519 it messes up the block number). */
520 static int cross_eof(struct scsi_tape * STp, int forward)
522 struct scsi_request *SRpnt;
523 unsigned char cmd[MAX_COMMAND_SIZE];
526 cmd[1] = 0x01; /* Space FileMarks */
531 cmd[2] = cmd[3] = cmd[4] = 0xff; /* -1 filemarks */
534 DEBC(printk(ST_DEB_MSG "%s: Stepping over filemark %s.\n",
535 tape_name(STp), forward ? "forward" : "backward"));
537 SRpnt = st_do_scsi(NULL, STp, cmd, 0, DMA_NONE,
538 STp->device->timeout, MAX_RETRIES, 1);
540 return (STp->buffer)->syscall_result;
542 scsi_release_request(SRpnt);
545 if ((STp->buffer)->cmdstat.midlevel_result != 0)
546 printk(KERN_ERR "%s: Stepping over filemark %s failed.\n",
547 tape_name(STp), forward ? "forward" : "backward");
549 return (STp->buffer)->syscall_result;
553 /* Flush the write buffer (never need to write if variable blocksize). */
554 static int flush_write_buffer(struct scsi_tape * STp)
556 int offset, transfer, blks;
558 unsigned char cmd[MAX_COMMAND_SIZE];
559 struct scsi_request *SRpnt;
560 struct st_partstat *STps;
562 result = write_behind_check(STp);
567 if (STp->dirty == 1) {
569 offset = (STp->buffer)->buffer_bytes;
570 transfer = ((offset + STp->block_size - 1) /
571 STp->block_size) * STp->block_size;
572 DEBC(printk(ST_DEB_MSG "%s: Flushing %d bytes.\n",
573 tape_name(STp), transfer));
575 memset((STp->buffer)->b_data + offset, 0, transfer - offset);
577 memset(cmd, 0, MAX_COMMAND_SIZE);
580 blks = transfer / STp->block_size;
585 SRpnt = st_do_scsi(NULL, STp, cmd, transfer, DMA_TO_DEVICE,
586 STp->device->timeout, MAX_WRITE_RETRIES, 1);
588 return (STp->buffer)->syscall_result;
590 STps = &(STp->ps[STp->partition]);
591 if ((STp->buffer)->syscall_result != 0) {
592 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
594 if (cmdstatp->have_sense && !cmdstatp->deferred &&
595 (cmdstatp->flags & SENSE_EOM) &&
596 (cmdstatp->sense_hdr.sense_key == NO_SENSE ||
597 cmdstatp->sense_hdr.sense_key == RECOVERED_ERROR) &&
598 (!cmdstatp->remainder_valid ||
599 cmdstatp->uremainder64 == 0)) { /* All written at EOM early warning */
601 (STp->buffer)->buffer_bytes = 0;
602 if (STps->drv_block >= 0)
603 STps->drv_block += blks;
606 printk(KERN_ERR "%s: Error on flush.\n",
608 STps->drv_block = (-1);
612 if (STps->drv_block >= 0)
613 STps->drv_block += blks;
615 (STp->buffer)->buffer_bytes = 0;
617 scsi_release_request(SRpnt);
624 /* Flush the tape buffer. The tape will be positioned correctly unless
625 seek_next is true. */
626 static int flush_buffer(struct scsi_tape *STp, int seek_next)
628 int backspace, result;
629 struct st_buffer *STbuffer;
630 struct st_partstat *STps;
632 STbuffer = STp->buffer;
635 * If there was a bus reset, block further access
638 if (STp->pos_unknown)
641 if (STp->ready != ST_READY)
643 STps = &(STp->ps[STp->partition]);
644 if (STps->rw == ST_WRITING) /* Writing */
645 return flush_write_buffer(STp);
647 if (STp->block_size == 0)
650 backspace = ((STp->buffer)->buffer_bytes +
651 (STp->buffer)->read_pointer) / STp->block_size -
652 ((STp->buffer)->read_pointer + STp->block_size - 1) /
654 (STp->buffer)->buffer_bytes = 0;
655 (STp->buffer)->read_pointer = 0;
658 if (STps->eof == ST_FM_HIT) {
659 result = cross_eof(STp, 0); /* Back over the EOF hit */
661 STps->eof = ST_NOEOF;
663 if (STps->drv_file >= 0)
668 if (!result && backspace > 0)
669 result = st_int_ioctl(STp, MTBSR, backspace);
670 } else if (STps->eof == ST_FM_HIT) {
671 if (STps->drv_file >= 0)
674 STps->eof = ST_NOEOF;
680 /* Set the mode parameters */
681 static int set_mode_densblk(struct scsi_tape * STp, struct st_modedef * STm)
685 char *name = tape_name(STp);
687 if (!STp->density_changed &&
688 STm->default_density >= 0 &&
689 STm->default_density != STp->density) {
690 arg = STm->default_density;
694 arg <<= MT_ST_DENSITY_SHIFT;
695 if (!STp->blksize_changed &&
696 STm->default_blksize >= 0 &&
697 STm->default_blksize != STp->block_size) {
698 arg |= STm->default_blksize;
701 arg |= STp->block_size;
703 st_int_ioctl(STp, SET_DENS_AND_BLK, arg)) {
705 "%s: Can't set default block size to %d bytes and density %x.\n",
706 name, STm->default_blksize, STm->default_density);
714 /* Lock or unlock the drive door. Don't use when scsi_request allocated. */
715 static int do_door_lock(struct scsi_tape * STp, int do_lock)
718 DEB(char *name = tape_name(STp);)
721 cmd = do_lock ? SCSI_IOCTL_DOORLOCK : SCSI_IOCTL_DOORUNLOCK;
722 DEBC(printk(ST_DEB_MSG "%s: %socking drive door.\n", name,
723 do_lock ? "L" : "Unl"));
724 retval = scsi_ioctl(STp->device, cmd, NULL);
726 STp->door_locked = do_lock ? ST_LOCKED_EXPLICIT : ST_UNLOCKED;
729 STp->door_locked = ST_LOCK_FAILS;
735 /* Set the internal state after reset */
736 static void reset_state(struct scsi_tape *STp)
739 struct st_partstat *STps;
741 STp->pos_unknown = 0;
742 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
743 STps = &(STp->ps[i]);
745 STps->eof = ST_NOEOF;
747 STps->last_block_valid = 0;
748 STps->drv_block = -1;
751 if (STp->can_partitions) {
752 STp->partition = find_partition(STp);
753 if (STp->partition < 0)
755 STp->new_partition = STp->partition;
759 /* Test if the drive is ready. Returns either one of the codes below or a negative system
761 #define CHKRES_READY 0
762 #define CHKRES_NEW_SESSION 1
763 #define CHKRES_NOT_READY 2
764 #define CHKRES_NO_TAPE 3
766 #define MAX_ATTENTIONS 10
768 static int test_ready(struct scsi_tape *STp, int do_wait)
770 int attentions, waits, max_wait, scode;
771 int retval = CHKRES_READY, new_session = 0;
772 unsigned char cmd[MAX_COMMAND_SIZE];
773 struct scsi_request *SRpnt = NULL;
774 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
776 max_wait = do_wait ? ST_BLOCK_SECONDS : 0;
778 for (attentions=waits=0; ; ) {
779 memset((void *) &cmd[0], 0, MAX_COMMAND_SIZE);
780 cmd[0] = TEST_UNIT_READY;
781 SRpnt = st_do_scsi(SRpnt, STp, cmd, 0, DMA_NONE,
782 STp->long_timeout, MAX_READY_RETRIES, 1);
785 retval = (STp->buffer)->syscall_result;
789 if (cmdstatp->have_sense) {
791 scode = cmdstatp->sense_hdr.sense_key;
793 if (scode == UNIT_ATTENTION) { /* New media? */
795 if (attentions < MAX_ATTENTIONS) {
805 if (scode == NOT_READY) {
806 if (waits < max_wait) {
807 if (msleep_interruptible(1000)) {
815 if ((STp->device)->scsi_level >= SCSI_2 &&
816 cmdstatp->sense_hdr.asc == 0x3a) /* Check ASC */
817 retval = CHKRES_NO_TAPE;
819 retval = CHKRES_NOT_READY;
825 retval = (STp->buffer)->syscall_result;
827 retval = new_session ? CHKRES_NEW_SESSION : CHKRES_READY;
832 scsi_release_request(SRpnt);
837 /* See if the drive is ready and gather information about the tape. Return values:
838 < 0 negative error code from errno.h
840 1 drive not ready (possibly no tape)
842 static int check_tape(struct scsi_tape *STp, struct file *filp)
844 int i, retval, new_session = 0, do_wait;
845 unsigned char cmd[MAX_COMMAND_SIZE], saved_cleaning;
846 unsigned short st_flags = filp->f_flags;
847 struct scsi_request *SRpnt = NULL;
848 struct st_modedef *STm;
849 struct st_partstat *STps;
850 char *name = tape_name(STp);
851 struct inode *inode = filp->f_dentry->d_inode;
852 int mode = TAPE_MODE(inode);
854 STp->ready = ST_READY;
856 if (mode != STp->current_mode) {
857 DEBC(printk(ST_DEB_MSG "%s: Mode change from %d to %d.\n",
858 name, STp->current_mode, mode));
860 STp->current_mode = mode;
862 STm = &(STp->modes[STp->current_mode]);
864 saved_cleaning = STp->cleaning_req;
865 STp->cleaning_req = 0;
867 do_wait = ((filp->f_flags & O_NONBLOCK) == 0);
868 retval = test_ready(STp, do_wait);
873 if (retval == CHKRES_NEW_SESSION) {
874 STp->pos_unknown = 0;
875 STp->partition = STp->new_partition = 0;
876 if (STp->can_partitions)
877 STp->nbr_partitions = 1; /* This guess will be updated later
879 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
880 STps = &(STp->ps[i]);
882 STps->eof = ST_NOEOF;
884 STps->last_block_valid = 0;
891 STp->cleaning_req |= saved_cleaning;
893 if (retval == CHKRES_NOT_READY || retval == CHKRES_NO_TAPE) {
894 if (retval == CHKRES_NO_TAPE)
895 STp->ready = ST_NO_TAPE;
897 STp->ready = ST_NOT_READY;
899 STp->density = 0; /* Clear the erroneous "residue" */
902 STp->ps[0].drv_file = STp->ps[0].drv_block = (-1);
903 STp->partition = STp->new_partition = 0;
904 STp->door_locked = ST_UNLOCKED;
905 return CHKRES_NOT_READY;
909 if (STp->omit_blklims)
910 STp->min_block = STp->max_block = (-1);
912 memset((void *) &cmd[0], 0, MAX_COMMAND_SIZE);
913 cmd[0] = READ_BLOCK_LIMITS;
915 SRpnt = st_do_scsi(SRpnt, STp, cmd, 6, DMA_FROM_DEVICE,
916 STp->device->timeout, MAX_READY_RETRIES, 1);
918 retval = (STp->buffer)->syscall_result;
922 if (!SRpnt->sr_result && !STp->buffer->cmdstat.have_sense) {
923 STp->max_block = ((STp->buffer)->b_data[1] << 16) |
924 ((STp->buffer)->b_data[2] << 8) | (STp->buffer)->b_data[3];
925 STp->min_block = ((STp->buffer)->b_data[4] << 8) |
926 (STp->buffer)->b_data[5];
927 if ( DEB( debugging || ) !STp->inited)
929 "%s: Block limits %d - %d bytes.\n", name,
930 STp->min_block, STp->max_block);
932 STp->min_block = STp->max_block = (-1);
933 DEBC(printk(ST_DEB_MSG "%s: Can't read block limits.\n",
938 memset((void *) &cmd[0], 0, MAX_COMMAND_SIZE);
942 SRpnt = st_do_scsi(SRpnt, STp, cmd, 12, DMA_FROM_DEVICE,
943 STp->device->timeout, MAX_READY_RETRIES, 1);
945 retval = (STp->buffer)->syscall_result;
949 if ((STp->buffer)->syscall_result != 0) {
950 DEBC(printk(ST_DEB_MSG "%s: No Mode Sense.\n", name));
951 STp->block_size = ST_DEFAULT_BLOCK; /* Educated guess (?) */
952 (STp->buffer)->syscall_result = 0; /* Prevent error propagation */
953 STp->drv_write_prot = 0;
955 DEBC(printk(ST_DEB_MSG
956 "%s: Mode sense. Length %d, medium %x, WBS %x, BLL %d\n",
958 (STp->buffer)->b_data[0], (STp->buffer)->b_data[1],
959 (STp->buffer)->b_data[2], (STp->buffer)->b_data[3]));
961 if ((STp->buffer)->b_data[3] >= 8) {
962 STp->drv_buffer = ((STp->buffer)->b_data[2] >> 4) & 7;
963 STp->density = (STp->buffer)->b_data[4];
964 STp->block_size = (STp->buffer)->b_data[9] * 65536 +
965 (STp->buffer)->b_data[10] * 256 + (STp->buffer)->b_data[11];
966 DEBC(printk(ST_DEB_MSG
967 "%s: Density %x, tape length: %x, drv buffer: %d\n",
968 name, STp->density, (STp->buffer)->b_data[5] * 65536 +
969 (STp->buffer)->b_data[6] * 256 + (STp->buffer)->b_data[7],
972 STp->drv_write_prot = ((STp->buffer)->b_data[2] & 0x80) != 0;
974 scsi_release_request(SRpnt);
978 if (STp->block_size > 0)
979 (STp->buffer)->buffer_blocks =
980 (STp->buffer)->buffer_size / STp->block_size;
982 (STp->buffer)->buffer_blocks = 1;
983 (STp->buffer)->buffer_bytes = (STp->buffer)->read_pointer = 0;
985 DEBC(printk(ST_DEB_MSG
986 "%s: Block size: %d, buffer size: %d (%d blocks).\n", name,
987 STp->block_size, (STp->buffer)->buffer_size,
988 (STp->buffer)->buffer_blocks));
990 if (STp->drv_write_prot) {
993 DEBC(printk(ST_DEB_MSG "%s: Write protected\n", name));
996 ((st_flags & O_ACCMODE) == O_WRONLY ||
997 (st_flags & O_ACCMODE) == O_RDWR)) {
1003 if (STp->can_partitions && STp->nbr_partitions < 1) {
1004 /* This code is reached when the device is opened for the first time
1005 after the driver has been initialized with tape in the drive and the
1006 partition support has been enabled. */
1007 DEBC(printk(ST_DEB_MSG
1008 "%s: Updating partition number in status.\n", name));
1009 if ((STp->partition = find_partition(STp)) < 0) {
1010 retval = STp->partition;
1013 STp->new_partition = STp->partition;
1014 STp->nbr_partitions = 1; /* This guess will be updated when necessary */
1017 if (new_session) { /* Change the drive parameters for the new mode */
1018 STp->density_changed = STp->blksize_changed = 0;
1019 STp->compression_changed = 0;
1020 if (!(STm->defaults_for_writes) &&
1021 (retval = set_mode_densblk(STp, STm)) < 0)
1024 if (STp->default_drvbuffer != 0xff) {
1025 if (st_int_ioctl(STp, MTSETDRVBUFFER, STp->default_drvbuffer))
1027 "%s: Can't set default drive buffering to %d.\n",
1028 name, STp->default_drvbuffer);
1032 return CHKRES_READY;
1039 \f/* Open the device. Needs to be called with BKL only because of incrementing the SCSI host
1041 static int st_open(struct inode *inode, struct file *filp)
1043 int i, retval = (-EIO);
1044 struct scsi_tape *STp;
1045 struct st_partstat *STps;
1046 int dev = TAPE_NR(inode);
1050 * We really want to do nonseekable_open(inode, filp); here, but some
1051 * versions of tar incorrectly call lseek on tapes and bail out if that
1052 * fails. So we disallow pread() and pwrite(), but permit lseeks.
1054 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
1056 write_lock(&st_dev_arr_lock);
1057 if (dev >= st_dev_max || scsi_tapes == NULL ||
1058 ((STp = scsi_tapes[dev]) == NULL)) {
1059 write_unlock(&st_dev_arr_lock);
1062 filp->private_data = STp;
1063 name = tape_name(STp);
1066 write_unlock(&st_dev_arr_lock);
1067 DEB( printk(ST_DEB_MSG "%s: Device already in use.\n", name); )
1071 if(scsi_device_get(STp->device)) {
1072 write_unlock(&st_dev_arr_lock);
1076 write_unlock(&st_dev_arr_lock);
1077 STp->rew_at_close = STp->autorew_dev = (iminor(inode) & 0x80) == 0;
1079 if (!scsi_block_when_processing_errors(STp->device)) {
1084 /* See that we have at least a one page buffer available */
1085 if (!enlarge_buffer(STp->buffer, PAGE_SIZE, STp->restr_dma)) {
1086 printk(KERN_WARNING "%s: Can't allocate one page tape buffer.\n",
1088 retval = (-EOVERFLOW);
1092 (STp->buffer)->writing = 0;
1093 (STp->buffer)->syscall_result = 0;
1095 STp->write_prot = ((filp->f_flags & O_ACCMODE) == O_RDONLY);
1098 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
1099 STps = &(STp->ps[i]);
1102 STp->recover_count = 0;
1103 DEB( STp->nbr_waits = STp->nbr_finished = 0;
1104 STp->nbr_requests = STp->nbr_dio = STp->nbr_pages = STp->nbr_combinable = 0; )
1106 retval = check_tape(STp, filp);
1109 if ((filp->f_flags & O_NONBLOCK) == 0 &&
1110 retval != CHKRES_READY) {
1117 normalize_buffer(STp->buffer);
1119 scsi_device_put(STp->device);
1125 /* Flush the tape buffer before close */
1126 static int st_flush(struct file *filp)
1128 int result = 0, result2;
1129 unsigned char cmd[MAX_COMMAND_SIZE];
1130 struct scsi_request *SRpnt;
1131 struct scsi_tape *STp = filp->private_data;
1132 struct st_modedef *STm = &(STp->modes[STp->current_mode]);
1133 struct st_partstat *STps = &(STp->ps[STp->partition]);
1134 char *name = tape_name(STp);
1136 if (file_count(filp) > 1)
1139 if (STps->rw == ST_WRITING && !STp->pos_unknown) {
1140 result = flush_write_buffer(STp);
1141 if (result != 0 && result != (-ENOSPC))
1145 if (STp->can_partitions &&
1146 (result2 = switch_partition(STp)) < 0) {
1147 DEBC(printk(ST_DEB_MSG
1148 "%s: switch_partition at close failed.\n", name));
1154 DEBC( if (STp->nbr_requests)
1155 printk(KERN_WARNING "%s: Number of r/w requests %d, dio used in %d, pages %d (%d).\n",
1156 name, STp->nbr_requests, STp->nbr_dio, STp->nbr_pages, STp->nbr_combinable));
1158 if (STps->rw == ST_WRITING && !STp->pos_unknown) {
1159 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
1161 DEBC(printk(ST_DEB_MSG "%s: Async write waits %d, finished %d.\n",
1162 name, STp->nbr_waits, STp->nbr_finished);
1165 memset(cmd, 0, MAX_COMMAND_SIZE);
1166 cmd[0] = WRITE_FILEMARKS;
1167 cmd[4] = 1 + STp->two_fm;
1169 SRpnt = st_do_scsi(NULL, STp, cmd, 0, DMA_NONE,
1170 STp->device->timeout, MAX_WRITE_RETRIES, 1);
1172 result = (STp->buffer)->syscall_result;
1176 if (STp->buffer->syscall_result == 0 ||
1177 (cmdstatp->have_sense && !cmdstatp->deferred &&
1178 (cmdstatp->flags & SENSE_EOM) &&
1179 (cmdstatp->sense_hdr.sense_key == NO_SENSE ||
1180 cmdstatp->sense_hdr.sense_key == RECOVERED_ERROR) &&
1181 (!cmdstatp->remainder_valid || cmdstatp->uremainder64 == 0))) {
1182 /* Write successful at EOM */
1183 scsi_release_request(SRpnt);
1185 if (STps->drv_file >= 0)
1187 STps->drv_block = 0;
1192 else { /* Write error */
1193 scsi_release_request(SRpnt);
1195 printk(KERN_ERR "%s: Error on write filemark.\n", name);
1200 DEBC(printk(ST_DEB_MSG "%s: Buffer flushed, %d EOF(s) written\n",
1202 } else if (!STp->rew_at_close) {
1203 STps = &(STp->ps[STp->partition]);
1204 if (!STm->sysv || STps->rw != ST_READING) {
1206 result = flush_buffer(STp, 0);
1207 else if (STps->eof == ST_FM_HIT) {
1208 result = cross_eof(STp, 0);
1210 if (STps->drv_file >= 0)
1212 STps->drv_block = 0;
1215 STps->eof = ST_NOEOF;
1217 } else if ((STps->eof == ST_NOEOF &&
1218 !(result = cross_eof(STp, 1))) ||
1219 STps->eof == ST_FM_HIT) {
1220 if (STps->drv_file >= 0)
1222 STps->drv_block = 0;
1228 if (STp->rew_at_close) {
1229 result2 = st_int_ioctl(STp, MTREW, 1);
1237 /* Close the device and release it. BKL is not needed: this is the only thread
1238 accessing this tape. */
1239 static int st_release(struct inode *inode, struct file *filp)
1242 struct scsi_tape *STp = filp->private_data;
1244 if (STp->door_locked == ST_LOCKED_AUTO)
1245 do_door_lock(STp, 0);
1247 normalize_buffer(STp->buffer);
1248 write_lock(&st_dev_arr_lock);
1250 write_unlock(&st_dev_arr_lock);
1251 scsi_device_put(STp->device);
1256 /* The checks common to both reading and writing */
1257 static ssize_t rw_checks(struct scsi_tape *STp, struct file *filp, size_t count)
1262 * If we are in the middle of error recovery, don't let anyone
1263 * else try and use this device. Also, if error recovery fails, it
1264 * may try and take the device offline, in which case all further
1265 * access to the device is prohibited.
1267 if (!scsi_block_when_processing_errors(STp->device)) {
1272 if (STp->ready != ST_READY) {
1273 if (STp->ready == ST_NO_TAPE)
1274 retval = (-ENOMEDIUM);
1280 if (! STp->modes[STp->current_mode].defined) {
1287 * If there was a bus reset, block further access
1290 if (STp->pos_unknown) {
1300 printk(ST_DEB_MSG "%s: Incorrect device.\n", tape_name(STp));
1305 if (STp->can_partitions &&
1306 (retval = switch_partition(STp)) < 0)
1309 if (STp->block_size == 0 && STp->max_block > 0 &&
1310 (count < STp->min_block || count > STp->max_block)) {
1315 if (STp->do_auto_lock && STp->door_locked == ST_UNLOCKED &&
1316 !do_door_lock(STp, 1))
1317 STp->door_locked = ST_LOCKED_AUTO;
1324 static int setup_buffering(struct scsi_tape *STp, const char __user *buf,
1325 size_t count, int is_read)
1327 int i, bufsize, retval = 0;
1328 struct st_buffer *STbp = STp->buffer;
1331 i = STp->try_dio && try_rdio;
1333 i = STp->try_dio && try_wdio;
1334 if (i && ((unsigned long)buf & queue_dma_alignment(
1335 STp->device->request_queue)) == 0) {
1336 i = st_map_user_pages(&(STbp->sg[0]), STbp->use_sg,
1337 (unsigned long)buf, count, (is_read ? READ : WRITE),
1341 STbp->buffer_bytes = 0; /* can be used as transfer counter */
1344 STbp->do_dio = 0; /* fall back to buffering with any error */
1345 STbp->sg_segs = STbp->do_dio;
1346 STbp->frp_sg_current = 0;
1350 STp->nbr_pages += STbp->do_dio;
1351 for (i=1; i < STbp->do_dio; i++)
1352 if (page_to_pfn(STbp->sg[i].page) == page_to_pfn(STbp->sg[i-1].page) + 1)
1353 STp->nbr_combinable++;
1358 DEB( STp->nbr_requests++; )
1360 if (!STbp->do_dio) {
1361 if (STp->block_size)
1362 bufsize = STp->block_size > st_fixed_buffer_size ?
1363 STp->block_size : st_fixed_buffer_size;
1366 if (bufsize > STbp->buffer_size &&
1367 !enlarge_buffer(STbp, bufsize, STp->restr_dma)) {
1368 printk(KERN_WARNING "%s: Can't allocate %d byte tape buffer.\n",
1369 tape_name(STp), bufsize);
1370 retval = (-EOVERFLOW);
1373 if (STp->block_size)
1374 STbp->buffer_blocks = bufsize / STp->block_size;
1382 /* Can be called more than once after each setup_buffer() */
1383 static void release_buffering(struct scsi_tape *STp)
1385 struct st_buffer *STbp;
1389 sgl_unmap_user_pages(&(STbp->sg[0]), STbp->do_dio, 0);
1397 st_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
1400 ssize_t i, do_count, blks, transfer;
1402 int undone, retry_eot = 0, scode;
1404 unsigned char cmd[MAX_COMMAND_SIZE];
1405 const char __user *b_point;
1406 struct scsi_request *SRpnt = NULL;
1407 struct scsi_tape *STp = filp->private_data;
1408 struct st_modedef *STm;
1409 struct st_partstat *STps;
1410 struct st_buffer *STbp;
1411 char *name = tape_name(STp);
1413 if (down_interruptible(&STp->lock))
1414 return -ERESTARTSYS;
1416 retval = rw_checks(STp, filp, count);
1417 if (retval || count == 0)
1420 /* Write must be integral number of blocks */
1421 if (STp->block_size != 0 && (count % STp->block_size) != 0) {
1422 printk(KERN_WARNING "%s: Write not multiple of tape block size.\n",
1428 STm = &(STp->modes[STp->current_mode]);
1429 STps = &(STp->ps[STp->partition]);
1431 if (STp->write_prot) {
1437 if (STps->rw == ST_READING) {
1438 retval = flush_buffer(STp, 0);
1441 STps->rw = ST_WRITING;
1442 } else if (STps->rw != ST_WRITING &&
1443 STps->drv_file == 0 && STps->drv_block == 0) {
1444 if ((retval = set_mode_densblk(STp, STm)) < 0)
1446 if (STm->default_compression != ST_DONT_TOUCH &&
1447 !(STp->compression_changed)) {
1448 if (st_compression(STp, (STm->default_compression == ST_YES))) {
1449 printk(KERN_WARNING "%s: Can't set default compression.\n",
1451 if (modes_defined) {
1460 i = write_behind_check(STp);
1463 STps->eof = ST_EOM_OK;
1465 STps->eof = ST_EOM_ERROR;
1468 if (STps->eof == ST_EOM_OK) {
1469 STps->eof = ST_EOD_1; /* allow next write */
1473 else if (STps->eof == ST_EOM_ERROR) {
1478 /* Check the buffer readability in cases where copy_user might catch
1479 the problems after some tape movement. */
1480 if (STp->block_size != 0 &&
1482 (copy_from_user(&i, buf, 1) != 0 ||
1483 copy_from_user(&i, buf + count - 1, 1) != 0)) {
1488 retval = setup_buffering(STp, buf, count, 0);
1494 memset(cmd, 0, MAX_COMMAND_SIZE);
1496 cmd[1] = (STp->block_size != 0);
1498 STps->rw = ST_WRITING;
1501 while (count > 0 && !retry_eot) {
1507 if (STp->block_size == 0)
1510 do_count = STbp->buffer_blocks * STp->block_size -
1512 if (do_count > count)
1516 i = append_to_buffer(b_point, STbp, do_count);
1523 b_point += do_count;
1525 async_write = STp->block_size == 0 && !STbp->do_dio &&
1526 STm->do_async_writes && STps->eof < ST_EOM_OK;
1528 if (STp->block_size != 0 && STm->do_buffer_writes &&
1529 !(STp->try_dio && try_wdio) && STps->eof < ST_EOM_OK &&
1530 STbp->buffer_bytes < STbp->buffer_size) {
1532 /* Don't write a buffer that is not full enough. */
1533 if (!async_write && count == 0)
1538 if (STp->block_size == 0)
1539 blks = transfer = do_count;
1542 blks = STbp->buffer_bytes;
1545 blks /= STp->block_size;
1546 transfer = blks * STp->block_size;
1548 cmd[2] = blks >> 16;
1552 SRpnt = st_do_scsi(SRpnt, STp, cmd, transfer, DMA_TO_DEVICE,
1553 STp->device->timeout, MAX_WRITE_RETRIES, !async_write);
1555 retval = STbp->syscall_result;
1559 STbp->writing = transfer;
1560 STp->dirty = !(STbp->writing ==
1561 STbp->buffer_bytes);
1562 SRpnt = NULL; /* Prevent releasing this request! */
1563 DEB( STp->write_pending = 1; )
1567 if (STbp->syscall_result != 0) {
1568 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
1570 DEBC(printk(ST_DEB_MSG "%s: Error on write:\n", name));
1571 if (cmdstatp->have_sense && (cmdstatp->flags & SENSE_EOM)) {
1572 scode = cmdstatp->sense_hdr.sense_key;
1573 if (cmdstatp->remainder_valid)
1574 undone = (int)cmdstatp->uremainder64;
1575 else if (STp->block_size == 0 &&
1576 scode == VOLUME_OVERFLOW)
1580 if (STp->block_size != 0)
1581 undone *= STp->block_size;
1582 if (undone <= do_count) {
1583 /* Only data from this write is not written */
1586 if (STp->block_size)
1587 blks = (transfer - undone) / STp->block_size;
1588 STps->eof = ST_EOM_OK;
1589 /* Continue in fixed block mode if all written
1590 in this request but still something left to write
1591 (retval left to zero)
1593 if (STp->block_size == 0 ||
1594 undone > 0 || count == 0)
1595 retval = (-ENOSPC); /* EOM within current request */
1596 DEBC(printk(ST_DEB_MSG
1597 "%s: EOM with %d bytes unwritten.\n",
1600 /* EOT within data buffered earlier (possible only
1601 in fixed block mode without direct i/o) */
1602 if (!retry_eot && !cmdstatp->deferred &&
1603 (scode == NO_SENSE || scode == RECOVERED_ERROR)) {
1604 move_buffer_data(STp->buffer, transfer - undone);
1606 if (STps->drv_block >= 0) {
1607 STps->drv_block += (transfer - undone) /
1610 STps->eof = ST_EOM_OK;
1611 DEBC(printk(ST_DEB_MSG
1612 "%s: Retry write of %d bytes at EOM.\n",
1613 name, STp->buffer->buffer_bytes));
1617 /* Either error within data buffered by driver or
1620 blks = do_count = 0;
1621 STps->eof = ST_EOM_ERROR;
1622 STps->drv_block = (-1); /* Too cautious? */
1623 retval = (-EIO); /* EOM for old data */
1624 DEBC(printk(ST_DEB_MSG
1625 "%s: EOM with lost data.\n",
1631 STps->drv_block = (-1); /* Too cautious? */
1637 if (STps->drv_block >= 0) {
1638 if (STp->block_size == 0)
1639 STps->drv_block += (do_count > 0);
1641 STps->drv_block += blks;
1644 STbp->buffer_bytes = 0;
1647 if (retval || retry_eot) {
1649 retval = total - count;
1654 if (STps->eof == ST_EOD_1)
1655 STps->eof = ST_EOM_OK;
1656 else if (STps->eof != ST_EOM_OK)
1657 STps->eof = ST_NOEOF;
1658 retval = total - count;
1662 scsi_release_request(SRpnt);
1663 release_buffering(STp);
1669 /* Read data from the tape. Returns zero in the normal case, one if the
1670 eof status has changed, and the negative error code in case of a
1671 fatal error. Otherwise updates the buffer and the eof state.
1673 Does release user buffer mapping if it is set.
1675 static long read_tape(struct scsi_tape *STp, long count,
1676 struct scsi_request ** aSRpnt)
1678 int transfer, blks, bytes;
1679 unsigned char cmd[MAX_COMMAND_SIZE];
1680 struct scsi_request *SRpnt;
1681 struct st_modedef *STm;
1682 struct st_partstat *STps;
1683 struct st_buffer *STbp;
1685 char *name = tape_name(STp);
1690 STm = &(STp->modes[STp->current_mode]);
1691 STps = &(STp->ps[STp->partition]);
1692 if (STps->eof == ST_FM_HIT)
1696 if (STp->block_size == 0)
1697 blks = bytes = count;
1699 if (!(STp->try_dio && try_rdio) && STm->do_read_ahead) {
1700 blks = (STp->buffer)->buffer_blocks;
1701 bytes = blks * STp->block_size;
1704 if (!STbp->do_dio && bytes > (STp->buffer)->buffer_size)
1705 bytes = (STp->buffer)->buffer_size;
1706 blks = bytes / STp->block_size;
1707 bytes = blks * STp->block_size;
1711 memset(cmd, 0, MAX_COMMAND_SIZE);
1713 cmd[1] = (STp->block_size != 0);
1714 cmd[2] = blks >> 16;
1719 SRpnt = st_do_scsi(SRpnt, STp, cmd, bytes, DMA_FROM_DEVICE,
1720 STp->device->timeout, MAX_RETRIES, 1);
1721 release_buffering(STp);
1724 return STbp->syscall_result;
1726 STbp->read_pointer = 0;
1729 /* Something to check */
1730 if (STbp->syscall_result) {
1731 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
1734 DEBC(printk(ST_DEB_MSG "%s: Sense: %2x %2x %2x %2x %2x %2x %2x %2x\n",
1736 SRpnt->sr_sense_buffer[0], SRpnt->sr_sense_buffer[1],
1737 SRpnt->sr_sense_buffer[2], SRpnt->sr_sense_buffer[3],
1738 SRpnt->sr_sense_buffer[4], SRpnt->sr_sense_buffer[5],
1739 SRpnt->sr_sense_buffer[6], SRpnt->sr_sense_buffer[7]));
1740 if (cmdstatp->have_sense) {
1742 if (cmdstatp->sense_hdr.sense_key == BLANK_CHECK)
1743 cmdstatp->flags &= 0xcf; /* No need for EOM in this case */
1745 if (cmdstatp->flags != 0) { /* EOF, EOM, or ILI */
1746 /* Compute the residual count */
1747 if (cmdstatp->remainder_valid)
1748 transfer = (int)cmdstatp->uremainder64;
1751 if (STp->block_size == 0 &&
1752 cmdstatp->sense_hdr.sense_key == MEDIUM_ERROR)
1755 if (cmdstatp->flags & SENSE_ILI) { /* ILI */
1756 if (STp->block_size == 0) {
1757 if (transfer <= 0) {
1760 "%s: Failed to read %d byte block with %d byte transfer.\n",
1761 name, bytes - transfer, bytes);
1762 if (STps->drv_block >= 0)
1763 STps->drv_block += 1;
1764 STbp->buffer_bytes = 0;
1767 STbp->buffer_bytes = bytes - transfer;
1769 scsi_release_request(SRpnt);
1770 SRpnt = *aSRpnt = NULL;
1771 if (transfer == blks) { /* We did not get anything, error */
1772 printk(KERN_NOTICE "%s: Incorrect block size.\n", name);
1773 if (STps->drv_block >= 0)
1774 STps->drv_block += blks - transfer + 1;
1775 st_int_ioctl(STp, MTBSR, 1);
1778 /* We have some data, deliver it */
1779 STbp->buffer_bytes = (blks - transfer) *
1781 DEBC(printk(ST_DEB_MSG
1782 "%s: ILI but enough data received %ld %d.\n",
1783 name, count, STbp->buffer_bytes));
1784 if (STps->drv_block >= 0)
1785 STps->drv_block += 1;
1786 if (st_int_ioctl(STp, MTBSR, 1))
1789 } else if (cmdstatp->flags & SENSE_FMK) { /* FM overrides EOM */
1790 if (STps->eof != ST_FM_HIT)
1791 STps->eof = ST_FM_HIT;
1793 STps->eof = ST_EOD_2;
1794 if (STp->block_size == 0)
1795 STbp->buffer_bytes = 0;
1797 STbp->buffer_bytes =
1798 bytes - transfer * STp->block_size;
1799 DEBC(printk(ST_DEB_MSG
1800 "%s: EOF detected (%d bytes read).\n",
1801 name, STbp->buffer_bytes));
1802 } else if (cmdstatp->flags & SENSE_EOM) {
1803 if (STps->eof == ST_FM)
1804 STps->eof = ST_EOD_1;
1806 STps->eof = ST_EOM_OK;
1807 if (STp->block_size == 0)
1808 STbp->buffer_bytes = bytes - transfer;
1810 STbp->buffer_bytes =
1811 bytes - transfer * STp->block_size;
1813 DEBC(printk(ST_DEB_MSG "%s: EOM detected (%d bytes read).\n",
1814 name, STbp->buffer_bytes));
1817 /* end of EOF, EOM, ILI test */
1818 else { /* nonzero sense key */
1819 DEBC(printk(ST_DEB_MSG
1820 "%s: Tape error while reading.\n", name));
1821 STps->drv_block = (-1);
1822 if (STps->eof == ST_FM &&
1823 cmdstatp->sense_hdr.sense_key == BLANK_CHECK) {
1824 DEBC(printk(ST_DEB_MSG
1825 "%s: Zero returned for first BLANK CHECK after EOF.\n",
1827 STps->eof = ST_EOD_2; /* First BLANK_CHECK after FM */
1828 } else /* Some other extended sense code */
1832 if (STbp->buffer_bytes < 0) /* Caused by bogus sense data */
1833 STbp->buffer_bytes = 0;
1835 /* End of extended sense test */
1836 else { /* Non-extended sense */
1837 retval = STbp->syscall_result;
1841 /* End of error handling */
1842 else /* Read successful */
1843 STbp->buffer_bytes = bytes;
1845 if (STps->drv_block >= 0) {
1846 if (STp->block_size == 0)
1849 STps->drv_block += STbp->buffer_bytes / STp->block_size;
1857 st_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
1861 ssize_t i, transfer;
1862 int special, do_dio = 0;
1863 struct scsi_request *SRpnt = NULL;
1864 struct scsi_tape *STp = filp->private_data;
1865 struct st_modedef *STm;
1866 struct st_partstat *STps;
1867 struct st_buffer *STbp = STp->buffer;
1868 DEB( char *name = tape_name(STp); )
1870 if (down_interruptible(&STp->lock))
1871 return -ERESTARTSYS;
1873 retval = rw_checks(STp, filp, count);
1874 if (retval || count == 0)
1877 STm = &(STp->modes[STp->current_mode]);
1878 if (!(STm->do_read_ahead) && STp->block_size != 0 &&
1879 (count % STp->block_size) != 0) {
1880 retval = (-EINVAL); /* Read must be integral number of blocks */
1884 STps = &(STp->ps[STp->partition]);
1885 if (STps->rw == ST_WRITING) {
1886 retval = flush_buffer(STp, 0);
1889 STps->rw = ST_READING;
1892 if (debugging && STps->eof != ST_NOEOF)
1893 printk(ST_DEB_MSG "%s: EOF/EOM flag up (%d). Bytes %d\n", name,
1894 STps->eof, STbp->buffer_bytes);
1897 retval = setup_buffering(STp, buf, count, 1);
1900 do_dio = STbp->do_dio;
1902 if (STbp->buffer_bytes == 0 &&
1903 STps->eof >= ST_EOD_1) {
1904 if (STps->eof < ST_EOD) {
1909 retval = (-EIO); /* EOM or Blank Check */
1914 /* Check the buffer writability before any tape movement. Don't alter
1916 if (copy_from_user(&i, buf, 1) != 0 ||
1917 copy_to_user(buf, &i, 1) != 0 ||
1918 copy_from_user(&i, buf + count - 1, 1) != 0 ||
1919 copy_to_user(buf + count - 1, &i, 1) != 0) {
1925 STps->rw = ST_READING;
1928 /* Loop until enough data in buffer or a special condition found */
1929 for (total = 0, special = 0; total < count && !special;) {
1931 /* Get new data if the buffer is empty */
1932 if (STbp->buffer_bytes == 0) {
1933 special = read_tape(STp, count - total, &SRpnt);
1934 if (special < 0) { /* No need to continue read */
1940 /* Move the data from driver buffer to user buffer */
1941 if (STbp->buffer_bytes > 0) {
1943 if (debugging && STps->eof != ST_NOEOF)
1945 "%s: EOF up (%d). Left %d, needed %d.\n", name,
1946 STps->eof, STbp->buffer_bytes,
1947 (int)(count - total));
1949 transfer = STbp->buffer_bytes < count - total ?
1950 STbp->buffer_bytes : count - total;
1952 i = from_buffer(STbp, buf, transfer);
1962 if (STp->block_size == 0)
1963 break; /* Read only one variable length block */
1965 } /* for (total = 0, special = 0;
1966 total < count && !special; ) */
1968 /* Change the eof state if no data from tape or buffer */
1970 if (STps->eof == ST_FM_HIT) {
1972 STps->drv_block = 0;
1973 if (STps->drv_file >= 0)
1975 } else if (STps->eof == ST_EOD_1) {
1976 STps->eof = ST_EOD_2;
1977 STps->drv_block = 0;
1978 if (STps->drv_file >= 0)
1980 } else if (STps->eof == ST_EOD_2)
1982 } else if (STps->eof == ST_FM)
1983 STps->eof = ST_NOEOF;
1987 if (SRpnt != NULL) {
1988 scsi_release_request(SRpnt);
1992 release_buffering(STp);
1993 STbp->buffer_bytes = 0;
2003 /* Set the driver options */
2004 static void st_log_options(struct scsi_tape * STp, struct st_modedef * STm, char *name)
2008 "%s: Mode %d options: buffer writes: %d, async writes: %d, read ahead: %d\n",
2009 name, STp->current_mode, STm->do_buffer_writes, STm->do_async_writes,
2010 STm->do_read_ahead);
2012 "%s: can bsr: %d, two FMs: %d, fast mteom: %d, auto lock: %d,\n",
2013 name, STp->can_bsr, STp->two_fm, STp->fast_mteom, STp->do_auto_lock);
2015 "%s: defs for wr: %d, no block limits: %d, partitions: %d, s2 log: %d\n",
2016 name, STm->defaults_for_writes, STp->omit_blklims, STp->can_partitions,
2017 STp->scsi2_logical);
2019 "%s: sysv: %d nowait: %d\n", name, STm->sysv, STp->immediate);
2020 printk(KERN_INFO "%s: debugging: %d\n",
2027 static int st_set_options(struct scsi_tape *STp, long options)
2031 struct st_modedef *STm;
2032 char *name = tape_name(STp);
2033 struct cdev *cd0, *cd1;
2035 STm = &(STp->modes[STp->current_mode]);
2036 if (!STm->defined) {
2037 cd0 = STm->cdevs[0]; cd1 = STm->cdevs[1];
2038 memcpy(STm, &(STp->modes[0]), sizeof(struct st_modedef));
2039 STm->cdevs[0] = cd0; STm->cdevs[1] = cd1;
2041 DEBC(printk(ST_DEB_MSG
2042 "%s: Initialized mode %d definition from mode 0\n",
2043 name, STp->current_mode));
2046 code = options & MT_ST_OPTIONS;
2047 if (code == MT_ST_BOOLEANS) {
2048 STm->do_buffer_writes = (options & MT_ST_BUFFER_WRITES) != 0;
2049 STm->do_async_writes = (options & MT_ST_ASYNC_WRITES) != 0;
2050 STm->defaults_for_writes = (options & MT_ST_DEF_WRITES) != 0;
2051 STm->do_read_ahead = (options & MT_ST_READ_AHEAD) != 0;
2052 STp->two_fm = (options & MT_ST_TWO_FM) != 0;
2053 STp->fast_mteom = (options & MT_ST_FAST_MTEOM) != 0;
2054 STp->do_auto_lock = (options & MT_ST_AUTO_LOCK) != 0;
2055 STp->can_bsr = (options & MT_ST_CAN_BSR) != 0;
2056 STp->omit_blklims = (options & MT_ST_NO_BLKLIMS) != 0;
2057 if ((STp->device)->scsi_level >= SCSI_2)
2058 STp->can_partitions = (options & MT_ST_CAN_PARTITIONS) != 0;
2059 STp->scsi2_logical = (options & MT_ST_SCSI2LOGICAL) != 0;
2060 STp->immediate = (options & MT_ST_NOWAIT) != 0;
2061 STm->sysv = (options & MT_ST_SYSV) != 0;
2062 DEB( debugging = (options & MT_ST_DEBUGGING) != 0;
2063 st_log_options(STp, STm, name); )
2064 } else if (code == MT_ST_SETBOOLEANS || code == MT_ST_CLEARBOOLEANS) {
2065 value = (code == MT_ST_SETBOOLEANS);
2066 if ((options & MT_ST_BUFFER_WRITES) != 0)
2067 STm->do_buffer_writes = value;
2068 if ((options & MT_ST_ASYNC_WRITES) != 0)
2069 STm->do_async_writes = value;
2070 if ((options & MT_ST_DEF_WRITES) != 0)
2071 STm->defaults_for_writes = value;
2072 if ((options & MT_ST_READ_AHEAD) != 0)
2073 STm->do_read_ahead = value;
2074 if ((options & MT_ST_TWO_FM) != 0)
2075 STp->two_fm = value;
2076 if ((options & MT_ST_FAST_MTEOM) != 0)
2077 STp->fast_mteom = value;
2078 if ((options & MT_ST_AUTO_LOCK) != 0)
2079 STp->do_auto_lock = value;
2080 if ((options & MT_ST_CAN_BSR) != 0)
2081 STp->can_bsr = value;
2082 if ((options & MT_ST_NO_BLKLIMS) != 0)
2083 STp->omit_blklims = value;
2084 if ((STp->device)->scsi_level >= SCSI_2 &&
2085 (options & MT_ST_CAN_PARTITIONS) != 0)
2086 STp->can_partitions = value;
2087 if ((options & MT_ST_SCSI2LOGICAL) != 0)
2088 STp->scsi2_logical = value;
2089 if ((options & MT_ST_NOWAIT) != 0)
2090 STp->immediate = value;
2091 if ((options & MT_ST_SYSV) != 0)
2094 if ((options & MT_ST_DEBUGGING) != 0)
2096 st_log_options(STp, STm, name); )
2097 } else if (code == MT_ST_WRITE_THRESHOLD) {
2098 /* Retained for compatibility */
2099 } else if (code == MT_ST_DEF_BLKSIZE) {
2100 value = (options & ~MT_ST_OPTIONS);
2101 if (value == ~MT_ST_OPTIONS) {
2102 STm->default_blksize = (-1);
2103 DEBC( printk(KERN_INFO "%s: Default block size disabled.\n", name));
2105 STm->default_blksize = value;
2106 DEBC( printk(KERN_INFO "%s: Default block size set to %d bytes.\n",
2107 name, STm->default_blksize));
2108 if (STp->ready == ST_READY) {
2109 STp->blksize_changed = 0;
2110 set_mode_densblk(STp, STm);
2113 } else if (code == MT_ST_TIMEOUTS) {
2114 value = (options & ~MT_ST_OPTIONS);
2115 if ((value & MT_ST_SET_LONG_TIMEOUT) != 0) {
2116 STp->long_timeout = (value & ~MT_ST_SET_LONG_TIMEOUT) * HZ;
2117 DEBC( printk(KERN_INFO "%s: Long timeout set to %d seconds.\n", name,
2118 (value & ~MT_ST_SET_LONG_TIMEOUT)));
2120 STp->device->timeout = value * HZ;
2121 DEBC( printk(KERN_INFO "%s: Normal timeout set to %d seconds.\n",
2124 } else if (code == MT_ST_SET_CLN) {
2125 value = (options & ~MT_ST_OPTIONS) & 0xff;
2127 value < EXTENDED_SENSE_START && value >= SCSI_SENSE_BUFFERSIZE)
2129 STp->cln_mode = value;
2130 STp->cln_sense_mask = (options >> 8) & 0xff;
2131 STp->cln_sense_value = (options >> 16) & 0xff;
2133 "%s: Cleaning request mode %d, mask %02x, value %02x\n",
2134 name, value, STp->cln_sense_mask, STp->cln_sense_value);
2135 } else if (code == MT_ST_DEF_OPTIONS) {
2136 code = (options & ~MT_ST_CLEAR_DEFAULT);
2137 value = (options & MT_ST_CLEAR_DEFAULT);
2138 if (code == MT_ST_DEF_DENSITY) {
2139 if (value == MT_ST_CLEAR_DEFAULT) {
2140 STm->default_density = (-1);
2141 DEBC( printk(KERN_INFO "%s: Density default disabled.\n",
2144 STm->default_density = value & 0xff;
2145 DEBC( printk(KERN_INFO "%s: Density default set to %x\n",
2146 name, STm->default_density));
2147 if (STp->ready == ST_READY) {
2148 STp->density_changed = 0;
2149 set_mode_densblk(STp, STm);
2152 } else if (code == MT_ST_DEF_DRVBUFFER) {
2153 if (value == MT_ST_CLEAR_DEFAULT) {
2154 STp->default_drvbuffer = 0xff;
2155 DEBC( printk(KERN_INFO
2156 "%s: Drive buffer default disabled.\n", name));
2158 STp->default_drvbuffer = value & 7;
2159 DEBC( printk(KERN_INFO
2160 "%s: Drive buffer default set to %x\n",
2161 name, STp->default_drvbuffer));
2162 if (STp->ready == ST_READY)
2163 st_int_ioctl(STp, MTSETDRVBUFFER, STp->default_drvbuffer);
2165 } else if (code == MT_ST_DEF_COMPRESSION) {
2166 if (value == MT_ST_CLEAR_DEFAULT) {
2167 STm->default_compression = ST_DONT_TOUCH;
2168 DEBC( printk(KERN_INFO
2169 "%s: Compression default disabled.\n", name));
2171 if ((value & 0xff00) != 0) {
2172 STp->c_algo = (value & 0xff00) >> 8;
2173 DEBC( printk(KERN_INFO "%s: Compression algorithm set to 0x%x.\n",
2174 name, STp->c_algo));
2176 if ((value & 0xff) != 0xff) {
2177 STm->default_compression = (value & 1 ? ST_YES : ST_NO);
2178 DEBC( printk(KERN_INFO "%s: Compression default set to %x\n",
2179 name, (value & 1)));
2180 if (STp->ready == ST_READY) {
2181 STp->compression_changed = 0;
2182 st_compression(STp, (STm->default_compression == ST_YES));
2193 #define MODE_HEADER_LENGTH 4
2195 /* Mode header and page byte offsets */
2196 #define MH_OFF_DATA_LENGTH 0
2197 #define MH_OFF_MEDIUM_TYPE 1
2198 #define MH_OFF_DEV_SPECIFIC 2
2199 #define MH_OFF_BDESCS_LENGTH 3
2200 #define MP_OFF_PAGE_NBR 0
2201 #define MP_OFF_PAGE_LENGTH 1
2203 /* Mode header and page bit masks */
2204 #define MH_BIT_WP 0x80
2205 #define MP_MSK_PAGE_NBR 0x3f
2207 /* Don't return block descriptors */
2208 #define MODE_SENSE_OMIT_BDESCS 0x08
2210 #define MODE_SELECT_PAGE_FORMAT 0x10
2212 /* Read a mode page into the tape buffer. The block descriptors are included
2213 if incl_block_descs is true. The page control is ored to the page number
2214 parameter, if necessary. */
2215 static int read_mode_page(struct scsi_tape *STp, int page, int omit_block_descs)
2217 unsigned char cmd[MAX_COMMAND_SIZE];
2218 struct scsi_request *SRpnt = NULL;
2220 memset(cmd, 0, MAX_COMMAND_SIZE);
2221 cmd[0] = MODE_SENSE;
2222 if (omit_block_descs)
2223 cmd[1] = MODE_SENSE_OMIT_BDESCS;
2227 SRpnt = st_do_scsi(SRpnt, STp, cmd, cmd[4], DMA_FROM_DEVICE,
2228 STp->device->timeout, 0, 1);
2230 return (STp->buffer)->syscall_result;
2232 scsi_release_request(SRpnt);
2234 return (STp->buffer)->syscall_result;
2238 /* Send the mode page in the tape buffer to the drive. Assumes that the mode data
2239 in the buffer is correctly formatted. The long timeout is used if slow is non-zero. */
2240 static int write_mode_page(struct scsi_tape *STp, int page, int slow)
2243 unsigned char cmd[MAX_COMMAND_SIZE];
2244 struct scsi_request *SRpnt = NULL;
2246 memset(cmd, 0, MAX_COMMAND_SIZE);
2247 cmd[0] = MODE_SELECT;
2248 cmd[1] = MODE_SELECT_PAGE_FORMAT;
2249 pgo = MODE_HEADER_LENGTH + (STp->buffer)->b_data[MH_OFF_BDESCS_LENGTH];
2250 cmd[4] = pgo + (STp->buffer)->b_data[pgo + MP_OFF_PAGE_LENGTH] + 2;
2252 /* Clear reserved fields */
2253 (STp->buffer)->b_data[MH_OFF_DATA_LENGTH] = 0;
2254 (STp->buffer)->b_data[MH_OFF_MEDIUM_TYPE] = 0;
2255 (STp->buffer)->b_data[MH_OFF_DEV_SPECIFIC] &= ~MH_BIT_WP;
2256 (STp->buffer)->b_data[pgo + MP_OFF_PAGE_NBR] &= MP_MSK_PAGE_NBR;
2258 SRpnt = st_do_scsi(SRpnt, STp, cmd, cmd[4], DMA_TO_DEVICE,
2259 (slow ? STp->long_timeout : STp->device->timeout), 0, 1);
2261 return (STp->buffer)->syscall_result;
2263 scsi_release_request(SRpnt);
2265 return (STp->buffer)->syscall_result;
2269 #define COMPRESSION_PAGE 0x0f
2270 #define COMPRESSION_PAGE_LENGTH 16
2272 #define CP_OFF_DCE_DCC 2
2273 #define CP_OFF_C_ALGO 7
2275 #define DCE_MASK 0x80
2276 #define DCC_MASK 0x40
2277 #define RED_MASK 0x60
2280 /* Control the compression with mode page 15. Algorithm not changed if zero.
2282 The block descriptors are read and written because Sony SDT-7000 does not
2283 work without this (suggestion from Michael Schaefer <Michael.Schaefer@dlr.de>).
2284 Including block descriptors should not cause any harm to other drives. */
2286 static int st_compression(struct scsi_tape * STp, int state)
2289 int mpoffs; /* Offset to mode page start */
2290 unsigned char *b_data = (STp->buffer)->b_data;
2291 DEB( char *name = tape_name(STp); )
2293 if (STp->ready != ST_READY)
2296 /* Read the current page contents */
2297 retval = read_mode_page(STp, COMPRESSION_PAGE, 0);
2299 DEBC(printk(ST_DEB_MSG "%s: Compression mode page not supported.\n",
2304 mpoffs = MODE_HEADER_LENGTH + b_data[MH_OFF_BDESCS_LENGTH];
2305 DEBC(printk(ST_DEB_MSG "%s: Compression state is %d.\n", name,
2306 (b_data[mpoffs + CP_OFF_DCE_DCC] & DCE_MASK ? 1 : 0)));
2308 /* Check if compression can be changed */
2309 if ((b_data[mpoffs + CP_OFF_DCE_DCC] & DCC_MASK) == 0) {
2310 DEBC(printk(ST_DEB_MSG "%s: Compression not supported.\n", name));
2316 b_data[mpoffs + CP_OFF_DCE_DCC] |= DCE_MASK;
2317 if (STp->c_algo != 0)
2318 b_data[mpoffs + CP_OFF_C_ALGO] = STp->c_algo;
2321 b_data[mpoffs + CP_OFF_DCE_DCC] &= ~DCE_MASK;
2322 if (STp->c_algo != 0)
2323 b_data[mpoffs + CP_OFF_C_ALGO] = 0; /* no compression */
2326 retval = write_mode_page(STp, COMPRESSION_PAGE, 0);
2328 DEBC(printk(ST_DEB_MSG "%s: Compression change failed.\n", name));
2331 DEBC(printk(ST_DEB_MSG "%s: Compression state changed to %d.\n",
2334 STp->compression_changed = 1;
2339 /* Process the load and unload commands (does unload if the load code is zero) */
2340 static int do_load_unload(struct scsi_tape *STp, struct file *filp, int load_code)
2342 int retval = (-EIO), timeout;
2343 DEB( char *name = tape_name(STp); )
2344 unsigned char cmd[MAX_COMMAND_SIZE];
2345 struct st_partstat *STps;
2346 struct scsi_request *SRpnt;
2348 if (STp->ready != ST_READY && !load_code) {
2349 if (STp->ready == ST_NO_TAPE)
2350 return (-ENOMEDIUM);
2355 memset(cmd, 0, MAX_COMMAND_SIZE);
2356 cmd[0] = START_STOP;
2360 * If arg >= 1 && arg <= 6 Enhanced load/unload in HP C1553A
2362 if (load_code >= 1 + MT_ST_HPLOADER_OFFSET
2363 && load_code <= 6 + MT_ST_HPLOADER_OFFSET) {
2364 DEBC(printk(ST_DEB_MSG "%s: Enhanced %sload slot %2d.\n",
2365 name, (cmd[4]) ? "" : "un",
2366 load_code - MT_ST_HPLOADER_OFFSET));
2367 cmd[3] = load_code - MT_ST_HPLOADER_OFFSET; /* MediaID field of C1553A */
2369 if (STp->immediate) {
2370 cmd[1] = 1; /* Don't wait for completion */
2371 timeout = STp->device->timeout;
2374 timeout = STp->long_timeout;
2378 printk(ST_DEB_MSG "%s: Unloading tape.\n", name);
2380 printk(ST_DEB_MSG "%s: Loading tape.\n", name);
2383 SRpnt = st_do_scsi(NULL, STp, cmd, 0, DMA_NONE,
2384 timeout, MAX_RETRIES, 1);
2386 return (STp->buffer)->syscall_result;
2388 retval = (STp->buffer)->syscall_result;
2389 scsi_release_request(SRpnt);
2391 if (!retval) { /* SCSI command successful */
2394 STp->rew_at_close = 0;
2395 STp->ready = ST_NO_TAPE;
2398 STp->rew_at_close = STp->autorew_dev;
2399 retval = check_tape(STp, filp);
2405 STps = &(STp->ps[STp->partition]);
2406 STps->drv_file = STps->drv_block = (-1);
2413 #define ST_DEB_FORWARD 0
2414 #define ST_DEB_BACKWARD 1
2415 static void deb_space_print(char *name, int direction, char *units, unsigned char *cmd)
2419 sc = cmd[2] & 0x80 ? 0xff000000 : 0;
2420 sc |= (cmd[2] << 16) | (cmd[3] << 8) | cmd[4];
2423 printk(ST_DEB_MSG "%s: Spacing tape %s over %d %s.\n", name,
2424 direction ? "backward" : "forward", sc, units);
2429 /* Internal ioctl function */
2430 static int st_int_ioctl(struct scsi_tape *STp, unsigned int cmd_in, unsigned long arg)
2436 unsigned char cmd[MAX_COMMAND_SIZE];
2437 struct scsi_request *SRpnt;
2438 struct st_partstat *STps;
2439 int fileno, blkno, at_sm, undone;
2440 int datalen = 0, direction = DMA_NONE;
2441 char *name = tape_name(STp);
2443 WARN_ON(STp->buffer->do_dio != 0);
2444 if (STp->ready != ST_READY) {
2445 if (STp->ready == ST_NO_TAPE)
2446 return (-ENOMEDIUM);
2450 timeout = STp->long_timeout;
2451 STps = &(STp->ps[STp->partition]);
2452 fileno = STps->drv_file;
2453 blkno = STps->drv_block;
2454 at_sm = STps->at_sm;
2456 memset(cmd, 0, MAX_COMMAND_SIZE);
2459 chg_eof = 0; /* Changed from the FSF after this */
2462 cmd[1] = 0x01; /* Space FileMarks */
2463 cmd[2] = (arg >> 16);
2464 cmd[3] = (arg >> 8);
2466 DEBC(deb_space_print(name, ST_DEB_FORWARD, "filemarks", cmd);)
2470 at_sm &= (arg == 0);
2473 chg_eof = 0; /* Changed from the FSF after this */
2476 cmd[1] = 0x01; /* Space FileMarks */
2478 cmd[2] = (ltmp >> 16);
2479 cmd[3] = (ltmp >> 8);
2481 DEBC(deb_space_print(name, ST_DEB_BACKWARD, "filemarks", cmd);)
2484 blkno = (-1); /* We can't know the block number */
2485 at_sm &= (arg == 0);
2489 cmd[1] = 0x00; /* Space Blocks */
2490 cmd[2] = (arg >> 16);
2491 cmd[3] = (arg >> 8);
2493 DEBC(deb_space_print(name, ST_DEB_FORWARD, "blocks", cmd);)
2496 at_sm &= (arg == 0);
2500 cmd[1] = 0x00; /* Space Blocks */
2502 cmd[2] = (ltmp >> 16);
2503 cmd[3] = (ltmp >> 8);
2505 DEBC(deb_space_print(name, ST_DEB_BACKWARD, "blocks", cmd);)
2508 at_sm &= (arg == 0);
2512 cmd[1] = 0x04; /* Space Setmarks */
2513 cmd[2] = (arg >> 16);
2514 cmd[3] = (arg >> 8);
2516 DEBC(deb_space_print(name, ST_DEB_FORWARD, "setmarks", cmd);)
2518 blkno = fileno = (-1);
2524 cmd[1] = 0x04; /* Space Setmarks */
2526 cmd[2] = (ltmp >> 16);
2527 cmd[3] = (ltmp >> 8);
2529 DEBC(deb_space_print(name, ST_DEB_BACKWARD, "setmarks", cmd);)
2531 blkno = fileno = (-1);
2537 if (STp->write_prot)
2539 cmd[0] = WRITE_FILEMARKS;
2540 if (cmd_in == MTWSM)
2542 cmd[2] = (arg >> 16);
2543 cmd[3] = (arg >> 8);
2545 timeout = STp->device->timeout;
2547 if (cmd_in == MTWEOF)
2548 printk(ST_DEB_MSG "%s: Writing %d filemarks.\n", name,
2549 cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
2551 printk(ST_DEB_MSG "%s: Writing %d setmarks.\n", name,
2552 cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
2557 at_sm = (cmd_in == MTWSM);
2560 cmd[0] = REZERO_UNIT;
2561 if (STp->immediate) {
2562 cmd[1] = 1; /* Don't wait for completion */
2563 timeout = STp->device->timeout;
2565 DEBC(printk(ST_DEB_MSG "%s: Rewinding tape.\n", name));
2566 fileno = blkno = at_sm = 0;
2569 DEBC(printk(ST_DEB_MSG "%s: No op on tape.\n", name));
2570 return 0; /* Should do something ? */
2573 cmd[0] = START_STOP;
2574 if (STp->immediate) {
2575 cmd[1] = 1; /* Don't wait for completion */
2576 timeout = STp->device->timeout;
2579 DEBC(printk(ST_DEB_MSG "%s: Retensioning tape.\n", name));
2580 fileno = blkno = at_sm = 0;
2583 if (!STp->fast_mteom) {
2584 /* space to the end of tape */
2585 ioctl_result = st_int_ioctl(STp, MTFSF, 0x7fffff);
2586 fileno = STps->drv_file;
2587 if (STps->eof >= ST_EOD_1)
2589 /* The next lines would hide the number of spaced FileMarks
2590 That's why I inserted the previous lines. I had no luck
2591 with detecting EOM with FSF, so we go now to EOM.
2597 DEBC(printk(ST_DEB_MSG "%s: Spacing to end of recorded medium.\n",
2603 if (STp->write_prot)
2606 cmd[1] = (arg ? 1 : 0); /* Long erase with non-zero argument */
2607 if (STp->immediate) {
2608 cmd[1] |= 2; /* Don't wait for completion */
2609 timeout = STp->device->timeout;
2612 timeout = STp->long_timeout * 8;
2614 DEBC(printk(ST_DEB_MSG "%s: Erasing tape.\n", name));
2615 fileno = blkno = at_sm = 0;
2617 case MTSETBLK: /* Set block length */
2618 case MTSETDENSITY: /* Set tape density */
2619 case MTSETDRVBUFFER: /* Set drive buffering */
2620 case SET_DENS_AND_BLK: /* Set density and block size */
2622 if (STp->dirty || (STp->buffer)->buffer_bytes != 0)
2623 return (-EIO); /* Not allowed if data in buffer */
2624 if ((cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) &&
2625 (arg & MT_ST_BLKSIZE_MASK) != 0 &&
2626 STp->max_block > 0 &&
2627 ((arg & MT_ST_BLKSIZE_MASK) < STp->min_block ||
2628 (arg & MT_ST_BLKSIZE_MASK) > STp->max_block)) {
2629 printk(KERN_WARNING "%s: Illegal block size.\n", name);
2632 cmd[0] = MODE_SELECT;
2633 if ((STp->use_pf & USE_PF))
2634 cmd[1] = MODE_SELECT_PAGE_FORMAT;
2635 cmd[4] = datalen = 12;
2636 direction = DMA_TO_DEVICE;
2638 memset((STp->buffer)->b_data, 0, 12);
2639 if (cmd_in == MTSETDRVBUFFER)
2640 (STp->buffer)->b_data[2] = (arg & 7) << 4;
2642 (STp->buffer)->b_data[2] =
2643 STp->drv_buffer << 4;
2644 (STp->buffer)->b_data[3] = 8; /* block descriptor length */
2645 if (cmd_in == MTSETDENSITY) {
2646 (STp->buffer)->b_data[4] = arg;
2647 STp->density_changed = 1; /* At least we tried ;-) */
2648 } else if (cmd_in == SET_DENS_AND_BLK)
2649 (STp->buffer)->b_data[4] = arg >> 24;
2651 (STp->buffer)->b_data[4] = STp->density;
2652 if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) {
2653 ltmp = arg & MT_ST_BLKSIZE_MASK;
2654 if (cmd_in == MTSETBLK)
2655 STp->blksize_changed = 1; /* At least we tried ;-) */
2657 ltmp = STp->block_size;
2658 (STp->buffer)->b_data[9] = (ltmp >> 16);
2659 (STp->buffer)->b_data[10] = (ltmp >> 8);
2660 (STp->buffer)->b_data[11] = ltmp;
2661 timeout = STp->device->timeout;
2663 if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK)
2665 "%s: Setting block size to %d bytes.\n", name,
2666 (STp->buffer)->b_data[9] * 65536 +
2667 (STp->buffer)->b_data[10] * 256 +
2668 (STp->buffer)->b_data[11]);
2669 if (cmd_in == MTSETDENSITY || cmd_in == SET_DENS_AND_BLK)
2671 "%s: Setting density code to %x.\n", name,
2672 (STp->buffer)->b_data[4]);
2673 if (cmd_in == MTSETDRVBUFFER)
2675 "%s: Setting drive buffer code to %d.\n", name,
2676 ((STp->buffer)->b_data[2] >> 4) & 7);
2683 SRpnt = st_do_scsi(NULL, STp, cmd, datalen, direction,
2684 timeout, MAX_RETRIES, 1);
2686 return (STp->buffer)->syscall_result;
2688 ioctl_result = (STp->buffer)->syscall_result;
2690 if (!ioctl_result) { /* SCSI command successful */
2691 scsi_release_request(SRpnt);
2693 STps->drv_block = blkno;
2694 STps->drv_file = fileno;
2695 STps->at_sm = at_sm;
2697 if (cmd_in == MTBSFM)
2698 ioctl_result = st_int_ioctl(STp, MTFSF, 1);
2699 else if (cmd_in == MTFSFM)
2700 ioctl_result = st_int_ioctl(STp, MTBSF, 1);
2702 if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) {
2703 int old_block_size = STp->block_size;
2704 STp->block_size = arg & MT_ST_BLKSIZE_MASK;
2705 if (STp->block_size != 0) {
2706 if (old_block_size == 0)
2707 normalize_buffer(STp->buffer);
2708 (STp->buffer)->buffer_blocks =
2709 (STp->buffer)->buffer_size / STp->block_size;
2711 (STp->buffer)->buffer_bytes = (STp->buffer)->read_pointer = 0;
2712 if (cmd_in == SET_DENS_AND_BLK)
2713 STp->density = arg >> MT_ST_DENSITY_SHIFT;
2714 } else if (cmd_in == MTSETDRVBUFFER)
2715 STp->drv_buffer = (arg & 7);
2716 else if (cmd_in == MTSETDENSITY)
2719 if (cmd_in == MTEOM)
2721 else if (cmd_in == MTFSF)
2724 STps->eof = ST_NOEOF;
2726 if (cmd_in == MTWEOF)
2728 } else { /* SCSI command was not completely successful. Don't return
2729 from this block without releasing the SCSI command block! */
2730 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
2732 if (cmdstatp->flags & SENSE_EOM) {
2733 if (cmd_in != MTBSF && cmd_in != MTBSFM &&
2734 cmd_in != MTBSR && cmd_in != MTBSS)
2735 STps->eof = ST_EOM_OK;
2736 STps->drv_block = 0;
2739 if (cmdstatp->remainder_valid)
2740 undone = (int)cmdstatp->uremainder64;
2744 if (cmd_in == MTWEOF &&
2745 cmdstatp->have_sense &&
2746 (cmdstatp->flags & SENSE_EOM) &&
2747 (cmdstatp->sense_hdr.sense_key == NO_SENSE ||
2748 cmdstatp->sense_hdr.sense_key == RECOVERED_ERROR) &&
2750 ioctl_result = 0; /* EOF written succesfully at EOM */
2753 STps->drv_file = fileno;
2754 STps->eof = ST_NOEOF;
2755 } else if ((cmd_in == MTFSF) || (cmd_in == MTFSFM)) {
2757 STps->drv_file = fileno - undone;
2759 STps->drv_file = fileno;
2760 STps->drv_block = -1;
2761 STps->eof = ST_NOEOF;
2762 } else if ((cmd_in == MTBSF) || (cmd_in == MTBSFM)) {
2763 if (arg > 0 && undone < 0) /* Some drives get this wrong */
2765 if (STps->drv_file >= 0)
2766 STps->drv_file = fileno + undone;
2767 STps->drv_block = 0;
2768 STps->eof = ST_NOEOF;
2769 } else if (cmd_in == MTFSR) {
2770 if (cmdstatp->flags & SENSE_FMK) { /* Hit filemark */
2771 if (STps->drv_file >= 0)
2773 STps->drv_block = 0;
2776 if (blkno >= undone)
2777 STps->drv_block = blkno - undone;
2779 STps->drv_block = (-1);
2780 STps->eof = ST_NOEOF;
2782 } else if (cmd_in == MTBSR) {
2783 if (cmdstatp->flags & SENSE_FMK) { /* Hit filemark */
2785 STps->drv_block = (-1);
2787 if (arg > 0 && undone < 0) /* Some drives get this wrong */
2789 if (STps->drv_block >= 0)
2790 STps->drv_block = blkno + undone;
2792 STps->eof = ST_NOEOF;
2793 } else if (cmd_in == MTEOM) {
2794 STps->drv_file = (-1);
2795 STps->drv_block = (-1);
2797 } else if (cmd_in == MTSETBLK ||
2798 cmd_in == MTSETDENSITY ||
2799 cmd_in == MTSETDRVBUFFER ||
2800 cmd_in == SET_DENS_AND_BLK) {
2801 if (cmdstatp->sense_hdr.sense_key == ILLEGAL_REQUEST &&
2802 !(STp->use_pf & PF_TESTED)) {
2803 /* Try the other possible state of Page Format if not
2805 STp->use_pf = !STp->use_pf | PF_TESTED;
2806 scsi_release_request(SRpnt);
2808 return st_int_ioctl(STp, cmd_in, arg);
2811 STps->eof = ST_NOEOF;
2813 if (cmdstatp->sense_hdr.sense_key == BLANK_CHECK)
2816 scsi_release_request(SRpnt);
2820 return ioctl_result;
2824 /* Get the tape position. If bt == 2, arg points into a kernel space mt_loc
2827 static int get_location(struct scsi_tape *STp, unsigned int *block, int *partition,
2831 unsigned char scmd[MAX_COMMAND_SIZE];
2832 struct scsi_request *SRpnt;
2833 DEB( char *name = tape_name(STp); )
2835 if (STp->ready != ST_READY)
2838 memset(scmd, 0, MAX_COMMAND_SIZE);
2839 if ((STp->device)->scsi_level < SCSI_2) {
2840 scmd[0] = QFA_REQUEST_BLOCK;
2843 scmd[0] = READ_POSITION;
2844 if (!logical && !STp->scsi2_logical)
2847 SRpnt = st_do_scsi(NULL, STp, scmd, 20, DMA_FROM_DEVICE,
2848 STp->device->timeout, MAX_READY_RETRIES, 1);
2850 return (STp->buffer)->syscall_result;
2852 if ((STp->buffer)->syscall_result != 0 ||
2853 (STp->device->scsi_level >= SCSI_2 &&
2854 ((STp->buffer)->b_data[0] & 4) != 0)) {
2855 *block = *partition = 0;
2856 DEBC(printk(ST_DEB_MSG "%s: Can't read tape position.\n", name));
2860 if ((STp->device)->scsi_level < SCSI_2) {
2861 *block = ((STp->buffer)->b_data[0] << 16)
2862 + ((STp->buffer)->b_data[1] << 8)
2863 + (STp->buffer)->b_data[2];
2866 *block = ((STp->buffer)->b_data[4] << 24)
2867 + ((STp->buffer)->b_data[5] << 16)
2868 + ((STp->buffer)->b_data[6] << 8)
2869 + (STp->buffer)->b_data[7];
2870 *partition = (STp->buffer)->b_data[1];
2871 if (((STp->buffer)->b_data[0] & 0x80) &&
2872 (STp->buffer)->b_data[1] == 0) /* BOP of partition 0 */
2873 STp->ps[0].drv_block = STp->ps[0].drv_file = 0;
2875 DEBC(printk(ST_DEB_MSG "%s: Got tape pos. blk %d part %d.\n", name,
2876 *block, *partition));
2878 scsi_release_request(SRpnt);
2885 /* Set the tape block and partition. Negative partition means that only the
2886 block should be set in vendor specific way. */
2887 static int set_location(struct scsi_tape *STp, unsigned int block, int partition,
2890 struct st_partstat *STps;
2894 unsigned char scmd[MAX_COMMAND_SIZE];
2895 struct scsi_request *SRpnt;
2896 DEB( char *name = tape_name(STp); )
2898 if (STp->ready != ST_READY)
2900 timeout = STp->long_timeout;
2901 STps = &(STp->ps[STp->partition]);
2903 DEBC(printk(ST_DEB_MSG "%s: Setting block to %d and partition to %d.\n",
2904 name, block, partition));
2905 DEB(if (partition < 0)
2908 /* Update the location at the partition we are leaving */
2909 if ((!STp->can_partitions && partition != 0) ||
2910 partition >= ST_NBR_PARTITIONS)
2912 if (partition != STp->partition) {
2913 if (get_location(STp, &blk, &p, 1))
2914 STps->last_block_valid = 0;
2916 STps->last_block_valid = 1;
2917 STps->last_block_visited = blk;
2918 DEBC(printk(ST_DEB_MSG
2919 "%s: Visited block %d for partition %d saved.\n",
2920 name, blk, STp->partition));
2924 memset(scmd, 0, MAX_COMMAND_SIZE);
2925 if ((STp->device)->scsi_level < SCSI_2) {
2926 scmd[0] = QFA_SEEK_BLOCK;
2927 scmd[2] = (block >> 16);
2928 scmd[3] = (block >> 8);
2933 scmd[3] = (block >> 24);
2934 scmd[4] = (block >> 16);
2935 scmd[5] = (block >> 8);
2937 if (!logical && !STp->scsi2_logical)
2939 if (STp->partition != partition) {
2941 scmd[8] = partition;
2942 DEBC(printk(ST_DEB_MSG
2943 "%s: Trying to change partition from %d to %d\n",
2944 name, STp->partition, partition));
2947 if (STp->immediate) {
2948 scmd[1] |= 1; /* Don't wait for completion */
2949 timeout = STp->device->timeout;
2952 SRpnt = st_do_scsi(NULL, STp, scmd, 0, DMA_NONE,
2953 timeout, MAX_READY_RETRIES, 1);
2955 return (STp->buffer)->syscall_result;
2957 STps->drv_block = STps->drv_file = (-1);
2958 STps->eof = ST_NOEOF;
2959 if ((STp->buffer)->syscall_result != 0) {
2961 if (STp->can_partitions &&
2962 (STp->device)->scsi_level >= SCSI_2 &&
2963 (p = find_partition(STp)) >= 0)
2966 if (STp->can_partitions) {
2967 STp->partition = partition;
2968 STps = &(STp->ps[partition]);
2969 if (!STps->last_block_valid ||
2970 STps->last_block_visited != block) {
2977 STps->drv_block = STps->drv_file = 0;
2981 scsi_release_request(SRpnt);
2988 /* Find the current partition number for the drive status. Called from open and
2989 returns either partition number of negative error code. */
2990 static int find_partition(struct scsi_tape *STp)
2995 if ((i = get_location(STp, &block, &partition, 1)) < 0)
2997 if (partition >= ST_NBR_PARTITIONS)
3003 /* Change the partition if necessary */
3004 static int switch_partition(struct scsi_tape *STp)
3006 struct st_partstat *STps;
3008 if (STp->partition == STp->new_partition)
3010 STps = &(STp->ps[STp->new_partition]);
3011 if (!STps->last_block_valid)
3012 STps->last_block_visited = 0;
3013 return set_location(STp, STps->last_block_visited, STp->new_partition, 1);
3016 /* Functions for reading and writing the medium partition mode page. */
3018 #define PART_PAGE 0x11
3019 #define PART_PAGE_FIXED_LENGTH 8
3021 #define PP_OFF_MAX_ADD_PARTS 2
3022 #define PP_OFF_NBR_ADD_PARTS 3
3023 #define PP_OFF_FLAGS 4
3024 #define PP_OFF_PART_UNITS 6
3025 #define PP_OFF_RESERVED 7
3027 #define PP_BIT_IDP 0x20
3028 #define PP_MSK_PSUM_MB 0x10
3030 /* Get the number of partitions on the tape. As a side effect reads the
3031 mode page into the tape buffer. */
3032 static int nbr_partitions(struct scsi_tape *STp)
3035 DEB( char *name = tape_name(STp); )
3037 if (STp->ready != ST_READY)
3040 result = read_mode_page(STp, PART_PAGE, 1);
3043 DEBC(printk(ST_DEB_MSG "%s: Can't read medium partition page.\n",
3047 result = (STp->buffer)->b_data[MODE_HEADER_LENGTH +
3048 PP_OFF_NBR_ADD_PARTS] + 1;
3049 DEBC(printk(ST_DEB_MSG "%s: Number of partitions %d.\n", name, result));
3056 /* Partition the tape into two partitions if size > 0 or one partition if
3059 The block descriptors are read and written because Sony SDT-7000 does not
3060 work without this (suggestion from Michael Schaefer <Michael.Schaefer@dlr.de>).
3062 My HP C1533A drive returns only one partition size field. This is used to
3063 set the size of partition 1. There is no size field for the default partition.
3064 Michael Schaefer's Sony SDT-7000 returns two descriptors and the second is
3065 used to set the size of partition 1 (this is what the SCSI-3 standard specifies).
3066 The following algorithm is used to accommodate both drives: if the number of
3067 partition size fields is greater than the maximum number of additional partitions
3068 in the mode page, the second field is used. Otherwise the first field is used.
3070 For Seagate DDS drives the page length must be 8 when no partitions is defined
3071 and 10 when 1 partition is defined (information from Eric Lee Green). This is
3072 is acceptable also to some other old drives and enforced if the first partition
3073 size field is used for the first additional partition size.
3075 static int partition_tape(struct scsi_tape *STp, int size)
3077 char *name = tape_name(STp);
3079 int pgo, psd_cnt, psdo;
3082 result = read_mode_page(STp, PART_PAGE, 0);
3084 DEBC(printk(ST_DEB_MSG "%s: Can't read partition mode page.\n", name));
3087 /* The mode page is in the buffer. Let's modify it and write it. */
3088 bp = (STp->buffer)->b_data;
3089 pgo = MODE_HEADER_LENGTH + bp[MH_OFF_BDESCS_LENGTH];
3090 DEBC(printk(ST_DEB_MSG "%s: Partition page length is %d bytes.\n",
3091 name, bp[pgo + MP_OFF_PAGE_LENGTH] + 2));
3093 psd_cnt = (bp[pgo + MP_OFF_PAGE_LENGTH] + 2 - PART_PAGE_FIXED_LENGTH) / 2;
3094 psdo = pgo + PART_PAGE_FIXED_LENGTH;
3095 if (psd_cnt > bp[pgo + PP_OFF_MAX_ADD_PARTS]) {
3096 bp[psdo] = bp[psdo + 1] = 0xff; /* Rest of the tape */
3099 memset(bp + psdo, 0, bp[pgo + PP_OFF_NBR_ADD_PARTS] * 2);
3101 DEBC(printk("%s: psd_cnt %d, max.parts %d, nbr_parts %d\n", name,
3102 psd_cnt, bp[pgo + PP_OFF_MAX_ADD_PARTS],
3103 bp[pgo + PP_OFF_NBR_ADD_PARTS]));
3106 bp[pgo + PP_OFF_NBR_ADD_PARTS] = 0;
3107 if (psd_cnt <= bp[pgo + PP_OFF_MAX_ADD_PARTS])
3108 bp[pgo + MP_OFF_PAGE_LENGTH] = 6;
3109 DEBC(printk(ST_DEB_MSG "%s: Formatting tape with one partition.\n",
3112 bp[psdo] = (size >> 8) & 0xff;
3113 bp[psdo + 1] = size & 0xff;
3115 if (bp[pgo + MP_OFF_PAGE_LENGTH] < 8)
3116 bp[pgo + MP_OFF_PAGE_LENGTH] = 8;
3117 DEBC(printk(ST_DEB_MSG
3118 "%s: Formatting tape with two partitions (1 = %d MB).\n",
3121 bp[pgo + PP_OFF_PART_UNITS] = 0;
3122 bp[pgo + PP_OFF_RESERVED] = 0;
3123 bp[pgo + PP_OFF_FLAGS] = PP_BIT_IDP | PP_MSK_PSUM_MB;
3125 result = write_mode_page(STp, PART_PAGE, 1);
3127 printk(KERN_INFO "%s: Partitioning of tape failed.\n", name);
3136 /* The ioctl command */
3137 static int st_ioctl(struct inode *inode, struct file *file,
3138 unsigned int cmd_in, unsigned long arg)
3140 int i, cmd_nr, cmd_type, bt;
3143 struct scsi_tape *STp = file->private_data;
3144 struct st_modedef *STm;
3145 struct st_partstat *STps;
3146 char *name = tape_name(STp);
3147 void __user *p = (void __user *)arg;
3149 if (down_interruptible(&STp->lock))
3150 return -ERESTARTSYS;
3153 if (debugging && !STp->in_use) {
3154 printk(ST_DEB_MSG "%s: Incorrect device.\n", name);
3159 STm = &(STp->modes[STp->current_mode]);
3160 STps = &(STp->ps[STp->partition]);
3163 * If we are in the middle of error recovery, don't let anyone
3164 * else try and use this device. Also, if error recovery fails, it
3165 * may try and take the device offline, in which case all further
3166 * access to the device is prohibited.
3168 retval = scsi_nonblockable_ioctl(STp->device, cmd_in, p, file);
3169 if (!scsi_block_when_processing_errors(STp->device) || retval != -ENODEV)
3173 cmd_type = _IOC_TYPE(cmd_in);
3174 cmd_nr = _IOC_NR(cmd_in);
3176 if (cmd_type == _IOC_TYPE(MTIOCTOP) && cmd_nr == _IOC_NR(MTIOCTOP)) {
3179 if (_IOC_SIZE(cmd_in) != sizeof(mtc)) {
3184 i = copy_from_user(&mtc, p, sizeof(struct mtop));
3190 if (mtc.mt_op == MTSETDRVBUFFER && !capable(CAP_SYS_ADMIN)) {
3192 "%s: MTSETDRVBUFFER only allowed for root.\n", name);
3196 if (!STm->defined &&
3197 (mtc.mt_op != MTSETDRVBUFFER &&
3198 (mtc.mt_count & MT_ST_OPTIONS) == 0)) {
3203 if (!STp->pos_unknown) {
3205 if (STps->eof == ST_FM_HIT) {
3206 if (mtc.mt_op == MTFSF || mtc.mt_op == MTFSFM ||
3207 mtc.mt_op == MTEOM) {
3209 if (STps->drv_file >= 0)
3210 STps->drv_file += 1;
3211 } else if (mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM) {
3213 if (STps->drv_file >= 0)
3214 STps->drv_file += 1;
3218 if (mtc.mt_op == MTSEEK) {
3219 /* Old position must be restored if partition will be
3221 i = !STp->can_partitions ||
3222 (STp->new_partition != STp->partition);
3224 i = mtc.mt_op == MTREW || mtc.mt_op == MTOFFL ||
3225 mtc.mt_op == MTRETEN || mtc.mt_op == MTEOM ||
3226 mtc.mt_op == MTLOCK || mtc.mt_op == MTLOAD ||
3227 mtc.mt_op == MTFSF || mtc.mt_op == MTFSFM ||
3228 mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM ||
3229 mtc.mt_op == MTCOMPRESSION;
3231 i = flush_buffer(STp, i);
3236 if (STps->rw == ST_WRITING &&
3237 (mtc.mt_op == MTREW || mtc.mt_op == MTOFFL ||
3238 mtc.mt_op == MTSEEK ||
3239 mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM)) {
3240 i = st_int_ioctl(STp, MTWEOF, 1);
3245 if (mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM)
3252 * If there was a bus reset, block further access
3253 * to this device. If the user wants to rewind the tape,
3254 * then reset the flag and allow access again.
3256 if (mtc.mt_op != MTREW &&
3257 mtc.mt_op != MTOFFL &&
3258 mtc.mt_op != MTRETEN &&
3259 mtc.mt_op != MTERASE &&
3260 mtc.mt_op != MTSEEK &&
3261 mtc.mt_op != MTEOM) {
3266 /* remove this when the midlevel properly clears was_reset */
3267 STp->device->was_reset = 0;
3270 if (mtc.mt_op != MTNOP && mtc.mt_op != MTSETBLK &&
3271 mtc.mt_op != MTSETDENSITY && mtc.mt_op != MTWSM &&
3272 mtc.mt_op != MTSETDRVBUFFER && mtc.mt_op != MTSETPART)
3273 STps->rw = ST_IDLE; /* Prevent automatic WEOF and fsf */
3275 if (mtc.mt_op == MTOFFL && STp->door_locked != ST_UNLOCKED)
3276 do_door_lock(STp, 0); /* Ignore result! */
3278 if (mtc.mt_op == MTSETDRVBUFFER &&
3279 (mtc.mt_count & MT_ST_OPTIONS) != 0) {
3280 retval = st_set_options(STp, mtc.mt_count);
3284 if (mtc.mt_op == MTSETPART) {
3285 if (!STp->can_partitions ||
3286 mtc.mt_count < 0 || mtc.mt_count >= ST_NBR_PARTITIONS) {
3290 if (mtc.mt_count >= STp->nbr_partitions &&
3291 (STp->nbr_partitions = nbr_partitions(STp)) < 0) {
3295 if (mtc.mt_count >= STp->nbr_partitions) {
3299 STp->new_partition = mtc.mt_count;
3304 if (mtc.mt_op == MTMKPART) {
3305 if (!STp->can_partitions) {
3309 if ((i = st_int_ioctl(STp, MTREW, 0)) < 0 ||
3310 (i = partition_tape(STp, mtc.mt_count)) < 0) {
3314 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
3315 STp->ps[i].rw = ST_IDLE;
3316 STp->ps[i].at_sm = 0;
3317 STp->ps[i].last_block_valid = 0;
3319 STp->partition = STp->new_partition = 0;
3320 STp->nbr_partitions = 1; /* Bad guess ?-) */
3321 STps->drv_block = STps->drv_file = 0;
3326 if (mtc.mt_op == MTSEEK) {
3327 i = set_location(STp, mtc.mt_count, STp->new_partition, 0);
3328 if (!STp->can_partitions)
3329 STp->ps[0].rw = ST_IDLE;
3334 if (mtc.mt_op == MTUNLOAD || mtc.mt_op == MTOFFL) {
3335 retval = do_load_unload(STp, file, 0);
3339 if (mtc.mt_op == MTLOAD) {
3340 retval = do_load_unload(STp, file, max(1, mtc.mt_count));
3344 if (mtc.mt_op == MTLOCK || mtc.mt_op == MTUNLOCK) {
3345 retval = do_door_lock(STp, (mtc.mt_op == MTLOCK));
3349 if (STp->can_partitions && STp->ready == ST_READY &&
3350 (i = switch_partition(STp)) < 0) {
3355 if (mtc.mt_op == MTCOMPRESSION)
3356 retval = st_compression(STp, (mtc.mt_count & 1));
3358 retval = st_int_ioctl(STp, mtc.mt_op, mtc.mt_count);
3361 if (!STm->defined) {
3366 if ((i = flush_buffer(STp, 0)) < 0) {
3370 if (STp->can_partitions &&
3371 (i = switch_partition(STp)) < 0) {
3376 if (cmd_type == _IOC_TYPE(MTIOCGET) && cmd_nr == _IOC_NR(MTIOCGET)) {
3377 struct mtget mt_status;
3379 if (_IOC_SIZE(cmd_in) != sizeof(struct mtget)) {
3384 mt_status.mt_type = STp->tape_type;
3385 mt_status.mt_dsreg =
3386 ((STp->block_size << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK) |
3387 ((STp->density << MT_ST_DENSITY_SHIFT) & MT_ST_DENSITY_MASK);
3388 mt_status.mt_blkno = STps->drv_block;
3389 mt_status.mt_fileno = STps->drv_file;
3390 if (STp->block_size != 0) {
3391 if (STps->rw == ST_WRITING)
3392 mt_status.mt_blkno +=
3393 (STp->buffer)->buffer_bytes / STp->block_size;
3394 else if (STps->rw == ST_READING)
3395 mt_status.mt_blkno -=
3396 ((STp->buffer)->buffer_bytes +
3397 STp->block_size - 1) / STp->block_size;
3400 mt_status.mt_gstat = 0;
3401 if (STp->drv_write_prot)
3402 mt_status.mt_gstat |= GMT_WR_PROT(0xffffffff);
3403 if (mt_status.mt_blkno == 0) {
3404 if (mt_status.mt_fileno == 0)
3405 mt_status.mt_gstat |= GMT_BOT(0xffffffff);
3407 mt_status.mt_gstat |= GMT_EOF(0xffffffff);
3409 mt_status.mt_erreg = (STp->recover_reg << MT_ST_SOFTERR_SHIFT);
3410 mt_status.mt_resid = STp->partition;
3411 if (STps->eof == ST_EOM_OK || STps->eof == ST_EOM_ERROR)
3412 mt_status.mt_gstat |= GMT_EOT(0xffffffff);
3413 else if (STps->eof >= ST_EOM_OK)
3414 mt_status.mt_gstat |= GMT_EOD(0xffffffff);
3415 if (STp->density == 1)
3416 mt_status.mt_gstat |= GMT_D_800(0xffffffff);
3417 else if (STp->density == 2)
3418 mt_status.mt_gstat |= GMT_D_1600(0xffffffff);
3419 else if (STp->density == 3)
3420 mt_status.mt_gstat |= GMT_D_6250(0xffffffff);
3421 if (STp->ready == ST_READY)
3422 mt_status.mt_gstat |= GMT_ONLINE(0xffffffff);
3423 if (STp->ready == ST_NO_TAPE)
3424 mt_status.mt_gstat |= GMT_DR_OPEN(0xffffffff);
3426 mt_status.mt_gstat |= GMT_SM(0xffffffff);
3427 if (STm->do_async_writes ||
3428 (STm->do_buffer_writes && STp->block_size != 0) ||
3429 STp->drv_buffer != 0)
3430 mt_status.mt_gstat |= GMT_IM_REP_EN(0xffffffff);
3431 if (STp->cleaning_req)
3432 mt_status.mt_gstat |= GMT_CLN(0xffffffff);
3434 i = copy_to_user(p, &mt_status, sizeof(struct mtget));
3440 STp->recover_reg = 0; /* Clear after read */
3443 } /* End of MTIOCGET */
3444 if (cmd_type == _IOC_TYPE(MTIOCPOS) && cmd_nr == _IOC_NR(MTIOCPOS)) {
3445 struct mtpos mt_pos;
3446 if (_IOC_SIZE(cmd_in) != sizeof(struct mtpos)) {
3450 if ((i = get_location(STp, &blk, &bt, 0)) < 0) {
3454 mt_pos.mt_blkno = blk;
3455 i = copy_to_user(p, &mt_pos, sizeof(struct mtpos));
3462 case SCSI_IOCTL_GET_IDLUN:
3463 case SCSI_IOCTL_GET_BUS_NUMBER:
3466 if (!capable(CAP_SYS_ADMIN))
3469 i = scsi_cmd_ioctl(file, STp->disk, cmd_in, p);
3474 if (!capable(CAP_SYS_ADMIN) &&
3475 (cmd_in == SCSI_IOCTL_START_UNIT || cmd_in == SCSI_IOCTL_STOP_UNIT))
3477 return scsi_ioctl(STp->device, cmd_in, p);
3484 #ifdef CONFIG_COMPAT
3485 static long st_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3487 struct scsi_tape *STp = file->private_data;
3488 struct scsi_device *sdev = STp->device;
3489 int ret = -ENOIOCTLCMD;
3490 if (sdev->host->hostt->compat_ioctl) {
3492 ret = sdev->host->hostt->compat_ioctl(sdev, cmd, (void __user *)arg);
3501 /* Try to allocate a new tape buffer. Calling function must not hold
3503 static struct st_buffer *
3504 new_tape_buffer(int from_initialization, int need_dma, int max_sg)
3506 int i, priority, got = 0, segs = 0;
3507 struct st_buffer *tb;
3509 if (from_initialization)
3510 priority = GFP_ATOMIC;
3512 priority = GFP_KERNEL;
3514 i = sizeof(struct st_buffer) + (max_sg - 1) * sizeof(struct scatterlist) +
3515 max_sg * sizeof(struct st_buf_fragment);
3516 tb = kmalloc(i, priority);
3518 printk(KERN_NOTICE "st: Can't allocate new tape buffer.\n");
3522 tb->frp_segs = tb->orig_frp_segs = segs;
3523 tb->use_sg = max_sg;
3525 tb->b_data = page_address(tb->sg[0].page);
3526 tb->frp = (struct st_buf_fragment *)(&(tb->sg[0]) + max_sg);
3530 tb->buffer_size = got;
3536 /* Try to allocate enough space in the tape buffer */
3537 static int enlarge_buffer(struct st_buffer * STbuffer, int new_size, int need_dma)
3539 int segs, nbr, max_segs, b_size, priority, order, got;
3541 if (new_size <= STbuffer->buffer_size)
3544 if (STbuffer->buffer_size <= PAGE_SIZE)
3545 normalize_buffer(STbuffer); /* Avoid extra segment */
3547 max_segs = STbuffer->use_sg;
3548 nbr = max_segs - STbuffer->frp_segs;
3552 priority = GFP_KERNEL | __GFP_NOWARN;
3554 priority |= GFP_DMA;
3555 for (b_size = PAGE_SIZE, order=0;
3556 b_size < new_size - STbuffer->buffer_size;
3557 order++, b_size *= 2)
3560 for (segs = STbuffer->frp_segs, got = STbuffer->buffer_size;
3561 segs < max_segs && got < new_size;) {
3562 STbuffer->frp[segs].page = alloc_pages(priority, order);
3563 if (STbuffer->frp[segs].page == NULL) {
3564 if (new_size - got <= (max_segs - segs) * b_size / 2) {
3565 b_size /= 2; /* Large enough for the rest of the buffers */
3569 DEB(STbuffer->buffer_size = got);
3570 normalize_buffer(STbuffer);
3573 STbuffer->frp[segs].length = b_size;
3574 STbuffer->frp_segs += 1;
3576 STbuffer->buffer_size = got;
3579 STbuffer->b_data = page_address(STbuffer->frp[0].page);
3585 /* Release the extra buffer */
3586 static void normalize_buffer(struct st_buffer * STbuffer)
3590 for (i = STbuffer->orig_frp_segs; i < STbuffer->frp_segs; i++) {
3591 order = get_order(STbuffer->frp[i].length);
3592 __free_pages(STbuffer->frp[i].page, order);
3593 STbuffer->buffer_size -= STbuffer->frp[i].length;
3595 STbuffer->frp_segs = STbuffer->orig_frp_segs;
3596 STbuffer->frp_sg_current = 0;
3600 /* Move data from the user buffer to the tape buffer. Returns zero (success) or
3601 negative error code. */
3602 static int append_to_buffer(const char __user *ubp, struct st_buffer * st_bp, int do_count)
3604 int i, cnt, res, offset;
3606 for (i = 0, offset = st_bp->buffer_bytes;
3607 i < st_bp->frp_segs && offset >= st_bp->frp[i].length; i++)
3608 offset -= st_bp->frp[i].length;
3609 if (i == st_bp->frp_segs) { /* Should never happen */
3610 printk(KERN_WARNING "st: append_to_buffer offset overflow.\n");
3613 for (; i < st_bp->frp_segs && do_count > 0; i++) {
3614 cnt = st_bp->frp[i].length - offset < do_count ?
3615 st_bp->frp[i].length - offset : do_count;
3616 res = copy_from_user(page_address(st_bp->frp[i].page) + offset, ubp, cnt);
3620 st_bp->buffer_bytes += cnt;
3624 if (do_count) /* Should never happen */
3631 /* Move data from the tape buffer to the user buffer. Returns zero (success) or
3632 negative error code. */
3633 static int from_buffer(struct st_buffer * st_bp, char __user *ubp, int do_count)
3635 int i, cnt, res, offset;
3637 for (i = 0, offset = st_bp->read_pointer;
3638 i < st_bp->frp_segs && offset >= st_bp->frp[i].length; i++)
3639 offset -= st_bp->frp[i].length;
3640 if (i == st_bp->frp_segs) { /* Should never happen */
3641 printk(KERN_WARNING "st: from_buffer offset overflow.\n");
3644 for (; i < st_bp->frp_segs && do_count > 0; i++) {
3645 cnt = st_bp->frp[i].length - offset < do_count ?
3646 st_bp->frp[i].length - offset : do_count;
3647 res = copy_to_user(ubp, page_address(st_bp->frp[i].page) + offset, cnt);
3651 st_bp->buffer_bytes -= cnt;
3652 st_bp->read_pointer += cnt;
3656 if (do_count) /* Should never happen */
3663 /* Move data towards start of buffer */
3664 static void move_buffer_data(struct st_buffer * st_bp, int offset)
3666 int src_seg, dst_seg, src_offset = 0, dst_offset;
3672 total=st_bp->buffer_bytes - offset;
3673 for (src_seg=0; src_seg < st_bp->frp_segs; src_seg++) {
3674 src_offset = offset;
3675 if (src_offset < st_bp->frp[src_seg].length)
3677 offset -= st_bp->frp[src_seg].length;
3680 st_bp->buffer_bytes = st_bp->read_pointer = total;
3681 for (dst_seg=dst_offset=0; total > 0; ) {
3682 count = min(st_bp->frp[dst_seg].length - dst_offset,
3683 st_bp->frp[src_seg].length - src_offset);
3684 memmove(page_address(st_bp->frp[dst_seg].page) + dst_offset,
3685 page_address(st_bp->frp[src_seg].page) + src_offset, count);
3686 src_offset += count;
3687 if (src_offset >= st_bp->frp[src_seg].length) {
3691 dst_offset += count;
3692 if (dst_offset >= st_bp->frp[dst_seg].length) {
3701 /* Fill the s/g list up to the length required for this transfer */
3702 static void buf_to_sg(struct st_buffer *STbp, unsigned int length)
3706 struct scatterlist *sg;
3707 struct st_buf_fragment *frp;
3709 if (length == STbp->frp_sg_current)
3710 return; /* work already done */
3712 sg = &(STbp->sg[0]);
3714 for (i=count=0; count < length; i++) {
3715 sg[i].page = frp[i].page;
3716 if (length - count > frp[i].length)
3717 sg[i].length = frp[i].length;
3719 sg[i].length = length - count;
3720 count += sg[i].length;
3724 STbp->frp_sg_current = length;
3728 /* Validate the options from command line or module parameters */
3729 static void validate_options(void)
3732 st_fixed_buffer_size = buffer_kbs * ST_KILOBYTE;
3733 if (max_sg_segs >= ST_FIRST_SG)
3734 st_max_sg_segs = max_sg_segs;
3738 /* Set the boot options. Syntax is defined in Documenation/scsi/st.txt.
3740 static int __init st_setup(char *str)
3742 int i, len, ints[5];
3745 stp = get_options(str, ARRAY_SIZE(ints), ints);
3748 for (i = 0; i < ints[0] && i < ARRAY_SIZE(parms); i++)
3750 *parms[i].val = ints[i + 1];
3752 while (stp != NULL) {
3753 for (i = 0; i < ARRAY_SIZE(parms); i++) {
3754 len = strlen(parms[i].name);
3755 if (!strncmp(stp, parms[i].name, len) &&
3756 (*(stp + len) == ':' || *(stp + len) == '=')) {
3759 simple_strtoul(stp + len + 1, NULL, 0);
3761 printk(KERN_WARNING "st: Obsolete parameter %s\n",
3766 if (i >= sizeof(parms) / sizeof(struct st_dev_parm))
3767 printk(KERN_WARNING "st: invalid parameter in '%s'\n",
3769 stp = strchr(stp, ',');
3780 __setup("st=", st_setup);
3784 static struct file_operations st_fops =
3786 .owner = THIS_MODULE,
3790 #ifdef CONFIG_COMPAT
3791 .compat_ioctl = st_compat_ioctl,
3795 .release = st_release,
3798 static int st_probe(struct device *dev)
3800 struct scsi_device *SDp = to_scsi_device(dev);
3801 struct gendisk *disk = NULL;
3802 struct cdev *cdev = NULL;
3803 struct scsi_tape *tpnt = NULL;
3804 struct st_modedef *STm;
3805 struct st_partstat *STps;
3806 struct st_buffer *buffer;
3807 int i, j, mode, dev_num, error;
3811 if (SDp->type != TYPE_TAPE)
3813 if ((stp = st_incompatible(SDp))) {
3815 "st: Found incompatible tape at scsi%d, channel %d, id %d, lun %d\n",
3816 SDp->host->host_no, SDp->channel, SDp->id, SDp->lun);
3817 printk(KERN_INFO "st: The suggested driver is %s.\n", stp);
3821 i = SDp->host->sg_tablesize;
3822 if (st_max_sg_segs < i)
3824 buffer = new_tape_buffer(1, (SDp->host)->unchecked_isa_dma, i);
3825 if (buffer == NULL) {
3827 "st: Can't allocate new tape buffer. Device not attached.\n");
3831 disk = alloc_disk(1);
3833 printk(KERN_ERR "st: out of memory. Device not attached.\n");
3834 goto out_buffer_free;
3837 write_lock(&st_dev_arr_lock);
3838 if (st_nr_dev >= st_dev_max) {
3839 struct scsi_tape **tmp_da;
3842 tmp_dev_max = max(st_nr_dev * 2, 8);
3843 if (tmp_dev_max > ST_MAX_TAPES)
3844 tmp_dev_max = ST_MAX_TAPES;
3845 if (tmp_dev_max <= st_nr_dev) {
3846 write_unlock(&st_dev_arr_lock);
3847 printk(KERN_ERR "st: Too many tape devices (max. %d).\n",
3852 tmp_da = kmalloc(tmp_dev_max * sizeof(struct scsi_tape *), GFP_ATOMIC);
3853 if (tmp_da == NULL) {
3854 write_unlock(&st_dev_arr_lock);
3855 printk(KERN_ERR "st: Can't extend device array.\n");
3859 memset(tmp_da, 0, tmp_dev_max * sizeof(struct scsi_tape *));
3860 if (scsi_tapes != NULL) {
3861 memcpy(tmp_da, scsi_tapes,
3862 st_dev_max * sizeof(struct scsi_tape *));
3865 scsi_tapes = tmp_da;
3867 st_dev_max = tmp_dev_max;
3870 for (i = 0; i < st_dev_max; i++)
3871 if (scsi_tapes[i] == NULL)
3873 if (i >= st_dev_max)
3874 panic("scsi_devices corrupt (st)");
3876 tpnt = kmalloc(sizeof(struct scsi_tape), GFP_ATOMIC);
3878 write_unlock(&st_dev_arr_lock);
3879 printk(KERN_ERR "st: Can't allocate device descriptor.\n");
3882 memset(tpnt, 0, sizeof(struct scsi_tape));
3884 sprintf(disk->disk_name, "st%d", i);
3885 disk->private_data = &tpnt->driver;
3886 disk->queue = SDp->request_queue;
3887 tpnt->driver = &st_template;
3888 scsi_tapes[i] = tpnt;
3892 if (SDp->scsi_level <= 2)
3893 tpnt->tape_type = MT_ISSCSI1;
3895 tpnt->tape_type = MT_ISSCSI2;
3897 tpnt->buffer = buffer;
3902 tpnt->drv_buffer = 1; /* Try buffering if no mode sense */
3903 tpnt->restr_dma = (SDp->host)->unchecked_isa_dma;
3904 tpnt->use_pf = (SDp->scsi_level >= SCSI_2);
3906 tpnt->do_auto_lock = ST_AUTO_LOCK;
3907 tpnt->can_bsr = (SDp->scsi_level > 2 ? 1 : ST_IN_FILE_POS); /* BSR mandatory in SCSI3 */
3908 tpnt->can_partitions = 0;
3909 tpnt->two_fm = ST_TWO_FM;
3910 tpnt->fast_mteom = ST_FAST_MTEOM;
3911 tpnt->scsi2_logical = ST_SCSI2LOGICAL;
3912 tpnt->immediate = ST_NOWAIT;
3913 tpnt->default_drvbuffer = 0xff; /* No forced buffering */
3914 tpnt->partition = 0;
3915 tpnt->new_partition = 0;
3916 tpnt->nbr_partitions = 0;
3917 tpnt->device->timeout = ST_TIMEOUT;
3918 tpnt->long_timeout = ST_LONG_TIMEOUT;
3919 tpnt->try_dio = try_direct_io && !SDp->host->unchecked_isa_dma;
3921 bounce_limit = scsi_calculate_bounce_limit(SDp->host) >> PAGE_SHIFT;
3922 if (bounce_limit > ULONG_MAX)
3923 bounce_limit = ULONG_MAX;
3924 tpnt->max_pfn = bounce_limit;
3926 for (i = 0; i < ST_NBR_MODES; i++) {
3927 STm = &(tpnt->modes[i]);
3929 STm->sysv = ST_SYSV;
3930 STm->defaults_for_writes = 0;
3931 STm->do_async_writes = ST_ASYNC_WRITES;
3932 STm->do_buffer_writes = ST_BUFFER_WRITES;
3933 STm->do_read_ahead = ST_READ_AHEAD;
3934 STm->default_compression = ST_DONT_TOUCH;
3935 STm->default_blksize = (-1); /* No forced size */
3936 STm->default_density = (-1); /* No forced density */
3939 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
3940 STps = &(tpnt->ps[i]);
3942 STps->eof = ST_NOEOF;
3944 STps->last_block_valid = 0;
3945 STps->drv_block = (-1);
3946 STps->drv_file = (-1);
3949 tpnt->current_mode = 0;
3950 tpnt->modes[0].defined = 1;
3952 tpnt->density_changed = tpnt->compression_changed =
3953 tpnt->blksize_changed = 0;
3954 init_MUTEX(&tpnt->lock);
3957 write_unlock(&st_dev_arr_lock);
3959 for (mode = 0; mode < ST_NBR_MODES; ++mode) {
3960 STm = &(tpnt->modes[mode]);
3961 for (j=0; j < 2; j++) {
3962 cdev = cdev_alloc();
3965 "st%d: out of memory. Device not attached.\n",
3969 cdev->owner = THIS_MODULE;
3970 cdev->ops = &st_fops;
3972 error = cdev_add(cdev,
3973 MKDEV(SCSI_TAPE_MAJOR, TAPE_MINOR(dev_num, mode, j)),
3976 printk(KERN_ERR "st%d: Can't add %s-rewind mode %d\n",
3977 dev_num, j ? "non" : "auto", mode);
3978 printk(KERN_ERR "st%d: Device not attached.\n", dev_num);
3981 STm->cdevs[j] = cdev;
3984 do_create_class_files(tpnt, dev_num, mode);
3987 for (mode = 0; mode < ST_NBR_MODES; ++mode) {
3988 /* Make sure that the minor numbers corresponding to the four
3989 first modes always get the same names */
3990 i = mode << (4 - ST_NBR_MODE_BITS);
3992 devfs_mk_cdev(MKDEV(SCSI_TAPE_MAJOR, TAPE_MINOR(dev_num, mode, 0)),
3993 S_IFCHR | S_IRUGO | S_IWUGO,
3994 "%s/mt%s", SDp->devfs_name, st_formats[i]);
3995 /* No-rewind entry */
3996 devfs_mk_cdev(MKDEV(SCSI_TAPE_MAJOR, TAPE_MINOR(dev_num, mode, 1)),
3997 S_IFCHR | S_IRUGO | S_IWUGO,
3998 "%s/mt%sn", SDp->devfs_name, st_formats[i]);
4000 disk->number = devfs_register_tape(SDp->devfs_name);
4003 "Attached scsi tape %s at scsi%d, channel %d, id %d, lun %d\n",
4004 tape_name(tpnt), SDp->host->host_no, SDp->channel, SDp->id, SDp->lun);
4005 printk(KERN_WARNING "%s: try direct i/o: %s (alignment %d B), max page reachable by HBA %lu\n",
4006 tape_name(tpnt), tpnt->try_dio ? "yes" : "no",
4007 queue_dma_alignment(SDp->request_queue) + 1, tpnt->max_pfn);
4012 for (mode=0; mode < ST_NBR_MODES; mode++) {
4013 STm = &(tpnt->modes[mode]);
4014 sysfs_remove_link(&tpnt->device->sdev_gendev.kobj,
4016 for (j=0; j < 2; j++) {
4017 if (STm->cdevs[j]) {
4018 if (cdev == STm->cdevs[j])
4020 class_simple_device_remove(MKDEV(SCSI_TAPE_MAJOR,
4021 TAPE_MINOR(i, mode, j)));
4022 cdev_del(STm->cdevs[j]);
4028 write_lock(&st_dev_arr_lock);
4029 scsi_tapes[dev_num] = NULL;
4031 write_unlock(&st_dev_arr_lock);
4043 static int st_remove(struct device *dev)
4045 struct scsi_device *SDp = to_scsi_device(dev);
4046 struct scsi_tape *tpnt;
4049 write_lock(&st_dev_arr_lock);
4050 for (i = 0; i < st_dev_max; i++) {
4051 tpnt = scsi_tapes[i];
4052 if (tpnt != NULL && tpnt->device == SDp) {
4053 scsi_tapes[i] = NULL;
4055 write_unlock(&st_dev_arr_lock);
4056 devfs_unregister_tape(tpnt->disk->number);
4057 sysfs_remove_link(&tpnt->device->sdev_gendev.kobj,
4059 for (mode = 0; mode < ST_NBR_MODES; ++mode) {
4060 j = mode << (4 - ST_NBR_MODE_BITS);
4061 devfs_remove("%s/mt%s", SDp->devfs_name, st_formats[j]);
4062 devfs_remove("%s/mt%sn", SDp->devfs_name, st_formats[j]);
4063 for (j=0; j < 2; j++) {
4064 class_simple_device_remove(MKDEV(SCSI_TAPE_MAJOR,
4065 TAPE_MINOR(i, mode, j)));
4066 cdev_del(tpnt->modes[mode].cdevs[j]);
4067 tpnt->modes[mode].cdevs[j] = NULL;
4070 tpnt->device = NULL;
4073 tpnt->buffer->orig_frp_segs = 0;
4074 normalize_buffer(tpnt->buffer);
4075 kfree(tpnt->buffer);
4077 put_disk(tpnt->disk);
4083 write_unlock(&st_dev_arr_lock);
4087 static void st_intr(struct scsi_cmnd *SCpnt)
4089 scsi_io_completion(SCpnt, (SCpnt->result ? 0: SCpnt->bufflen), 1);
4093 * st_init_command: only called via the scsi_cmd_ioctl (block SG_IO)
4094 * interface for REQ_BLOCK_PC commands.
4096 static int st_init_command(struct scsi_cmnd *SCpnt)
4100 if (!(SCpnt->request->flags & REQ_BLOCK_PC))
4103 rq = SCpnt->request;
4104 if (sizeof(rq->cmd) > sizeof(SCpnt->cmnd))
4107 memcpy(SCpnt->cmnd, rq->cmd, sizeof(SCpnt->cmnd));
4109 if (rq_data_dir(rq) == WRITE)
4110 SCpnt->sc_data_direction = DMA_TO_DEVICE;
4111 else if (rq->data_len)
4112 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
4114 SCpnt->sc_data_direction = DMA_NONE;
4116 SCpnt->timeout_per_command = rq->timeout;
4117 SCpnt->transfersize = rq->data_len;
4118 SCpnt->done = st_intr;
4122 static int __init init_st(void)
4127 "st: Version %s, fixed bufsize %d, s/g segs %d\n",
4128 verstr, st_fixed_buffer_size, st_max_sg_segs);
4130 st_sysfs_class = class_simple_create(THIS_MODULE, "scsi_tape");
4131 if (IS_ERR(st_sysfs_class)) {
4132 st_sysfs_class = NULL;
4133 printk(KERN_ERR "Unable create sysfs class for SCSI tapes\n");
4137 if (!register_chrdev_region(MKDEV(SCSI_TAPE_MAJOR, 0),
4138 ST_MAX_TAPE_ENTRIES, "st")) {
4139 if (scsi_register_driver(&st_template.gendrv) == 0) {
4140 do_create_driverfs_files();
4144 class_simple_destroy(st_sysfs_class);
4145 unregister_chrdev_region(MKDEV(SCSI_TAPE_MAJOR, 0),
4147 ST_MAX_TAPE_ENTRIES);
4150 printk(KERN_ERR "Unable to get major %d for SCSI tapes\n", SCSI_TAPE_MAJOR);
4154 static void __exit exit_st(void)
4157 class_simple_destroy(st_sysfs_class);
4158 st_sysfs_class = NULL;
4159 do_remove_driverfs_files();
4160 scsi_unregister_driver(&st_template.gendrv);
4161 unregister_chrdev_region(MKDEV(SCSI_TAPE_MAJOR, 0),
4162 ST_MAX_TAPE_ENTRIES);
4164 printk(KERN_INFO "st: Unloaded.\n");
4167 module_init(init_st);
4168 module_exit(exit_st);
4171 /* The sysfs driver interface. Read-only at the moment */
4172 static ssize_t st_try_direct_io_show(struct device_driver *ddp, char *buf)
4174 return snprintf(buf, PAGE_SIZE, "%d\n", try_direct_io);
4176 static DRIVER_ATTR(try_direct_io, S_IRUGO, st_try_direct_io_show, NULL);
4178 static ssize_t st_fixed_buffer_size_show(struct device_driver *ddp, char *buf)
4180 return snprintf(buf, PAGE_SIZE, "%d\n", st_fixed_buffer_size);
4182 static DRIVER_ATTR(fixed_buffer_size, S_IRUGO, st_fixed_buffer_size_show, NULL);
4184 static ssize_t st_max_sg_segs_show(struct device_driver *ddp, char *buf)
4186 return snprintf(buf, PAGE_SIZE, "%d\n", st_max_sg_segs);
4188 static DRIVER_ATTR(max_sg_segs, S_IRUGO, st_max_sg_segs_show, NULL);
4190 static ssize_t st_version_show(struct device_driver *ddd, char *buf)
4192 return snprintf(buf, PAGE_SIZE, "[%s]\n", verstr);
4194 static DRIVER_ATTR(version, S_IRUGO, st_version_show, NULL);
4196 static void do_create_driverfs_files(void)
4198 struct device_driver *driverfs = &st_template.gendrv;
4200 driver_create_file(driverfs, &driver_attr_try_direct_io);
4201 driver_create_file(driverfs, &driver_attr_fixed_buffer_size);
4202 driver_create_file(driverfs, &driver_attr_max_sg_segs);
4203 driver_create_file(driverfs, &driver_attr_version);
4206 static void do_remove_driverfs_files(void)
4208 struct device_driver *driverfs = &st_template.gendrv;
4210 driver_remove_file(driverfs, &driver_attr_version);
4211 driver_remove_file(driverfs, &driver_attr_max_sg_segs);
4212 driver_remove_file(driverfs, &driver_attr_fixed_buffer_size);
4213 driver_remove_file(driverfs, &driver_attr_try_direct_io);
4217 /* The sysfs simple class interface */
4218 static ssize_t st_defined_show(struct class_device *class_dev, char *buf)
4220 struct st_modedef *STm = (struct st_modedef *)class_get_devdata(class_dev);
4223 l = snprintf(buf, PAGE_SIZE, "%d\n", STm->defined);
4227 CLASS_DEVICE_ATTR(defined, S_IRUGO, st_defined_show, NULL);
4229 static ssize_t st_defblk_show(struct class_device *class_dev, char *buf)
4231 struct st_modedef *STm = (struct st_modedef *)class_get_devdata(class_dev);
4234 l = snprintf(buf, PAGE_SIZE, "%d\n", STm->default_blksize);
4238 CLASS_DEVICE_ATTR(default_blksize, S_IRUGO, st_defblk_show, NULL);
4240 static ssize_t st_defdensity_show(struct class_device *class_dev, char *buf)
4242 struct st_modedef *STm = (struct st_modedef *)class_get_devdata(class_dev);
4246 fmt = STm->default_density >= 0 ? "0x%02x\n" : "%d\n";
4247 l = snprintf(buf, PAGE_SIZE, fmt, STm->default_density);
4251 CLASS_DEVICE_ATTR(default_density, S_IRUGO, st_defdensity_show, NULL);
4253 static ssize_t st_defcompression_show(struct class_device *class_dev, char *buf)
4255 struct st_modedef *STm = (struct st_modedef *)class_get_devdata(class_dev);
4258 l = snprintf(buf, PAGE_SIZE, "%d\n", STm->default_compression - 1);
4262 CLASS_DEVICE_ATTR(default_compression, S_IRUGO, st_defcompression_show, NULL);
4264 static void do_create_class_files(struct scsi_tape *STp, int dev_num, int mode)
4268 struct class_device *st_class_member;
4270 if (!st_sysfs_class)
4273 for (rew=0; rew < 2; rew++) {
4274 /* Make sure that the minor numbers corresponding to the four
4275 first modes always get the same names */
4276 i = mode << (4 - ST_NBR_MODE_BITS);
4277 snprintf(name, 10, "%s%s%s", rew ? "n" : "",
4278 STp->disk->disk_name, st_formats[i]);
4280 class_simple_device_add(st_sysfs_class,
4281 MKDEV(SCSI_TAPE_MAJOR,
4282 TAPE_MINOR(dev_num, mode, rew)),
4283 &STp->device->sdev_gendev, "%s", name);
4284 if (IS_ERR(st_class_member)) {
4285 printk(KERN_WARNING "st%d: class_simple_device_add failed\n",
4289 class_set_devdata(st_class_member, &STp->modes[mode]);
4291 class_device_create_file(st_class_member,
4292 &class_device_attr_defined);
4293 class_device_create_file(st_class_member,
4294 &class_device_attr_default_blksize);
4295 class_device_create_file(st_class_member,
4296 &class_device_attr_default_density);
4297 class_device_create_file(st_class_member,
4298 &class_device_attr_default_compression);
4299 if (mode == 0 && rew == 0) {
4300 error = sysfs_create_link(&STp->device->sdev_gendev.kobj,
4301 &st_class_member->kobj,
4305 "st%d: Can't create sysfs link from SCSI device.\n",
4315 /* Pin down user pages and put them into a scatter gather list. Returns <= 0 if
4316 - mapping of all pages not successful
4317 - any page is above max_pfn
4318 (i.e., either completely successful or fails)
4320 static int st_map_user_pages(struct scatterlist *sgl, const unsigned int max_pages,
4321 unsigned long uaddr, size_t count, int rw,
4322 unsigned long max_pfn)
4326 nr_pages = sgl_map_user_pages(sgl, max_pages, uaddr, count, rw);
4330 for (i=0; i < nr_pages; i++) {
4331 if (page_to_pfn(sgl[i].page) > max_pfn)
4337 sgl_unmap_user_pages(sgl, nr_pages, 0);
4342 /* The following functions may be useful for a larger audience. */
4343 static int sgl_map_user_pages(struct scatterlist *sgl, const unsigned int max_pages,
4344 unsigned long uaddr, size_t count, int rw)
4347 unsigned int nr_pages;
4348 struct page **pages;
4350 nr_pages = ((uaddr & ~PAGE_MASK) + count + ~PAGE_MASK) >> PAGE_SHIFT;
4352 /* User attempted Overflow! */
4353 if ((uaddr + count) < uaddr)
4357 if (nr_pages > max_pages)
4364 if ((pages = kmalloc(max_pages * sizeof(*pages), GFP_KERNEL)) == NULL)
4367 /* Try to fault in all of the necessary pages */
4368 down_read(¤t->mm->mmap_sem);
4369 /* rw==READ means read from drive, write into memory area */
4370 res = get_user_pages(
4376 0, /* don't force */
4379 up_read(¤t->mm->mmap_sem);
4381 /* Errors and no page mapped should return here */
4385 for (i=0; i < nr_pages; i++) {
4386 /* FIXME: flush superflous for rw==READ,
4387 * probably wrong function for rw==WRITE
4389 flush_dcache_page(pages[i]);
4392 /* Populate the scatter/gather list */
4393 sgl[0].page = pages[0];
4394 sgl[0].offset = uaddr & ~PAGE_MASK;
4396 sgl[0].length = PAGE_SIZE - sgl[0].offset;
4397 count -= sgl[0].length;
4398 for (i=1; i < nr_pages ; i++) {
4400 sgl[i].page = pages[i];
4401 sgl[i].length = count < PAGE_SIZE ? count : PAGE_SIZE;
4406 sgl[0].length = count;
4414 for (j=0; j < res; j++)
4415 page_cache_release(pages[j]);
4422 /* And unmap them... */
4423 static int sgl_unmap_user_pages(struct scatterlist *sgl, const unsigned int nr_pages,
4428 for (i=0; i < nr_pages; i++) {
4429 if (dirtied && !PageReserved(sgl[i].page))
4430 SetPageDirty(sgl[i].page);
4431 /* FIXME: cache flush missing for rw==READ
4432 * FIXME: call the correct reference counting function
4434 page_cache_release(sgl[i].page);