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 = "20050501";
22 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/sched.h>
28 #include <linux/init.h>
29 #include <linux/string.h>
30 #include <linux/errno.h>
31 #include <linux/mtio.h>
32 #include <linux/cdrom.h>
33 #include <linux/ioctl.h>
34 #include <linux/fcntl.h>
35 #include <linux/spinlock.h>
36 #include <linux/blkdev.h>
37 #include <linux/moduleparam.h>
38 #include <linux/devfs_fs_kernel.h>
39 #include <linux/cdev.h>
40 #include <linux/delay.h>
42 #include <asm/uaccess.h>
44 #include <asm/system.h>
46 #include <scsi/scsi.h>
47 #include <scsi/scsi_dbg.h>
48 #include <scsi/scsi_device.h>
49 #include <scsi/scsi_driver.h>
50 #include <scsi/scsi_eh.h>
51 #include <scsi/scsi_host.h>
52 #include <scsi/scsi_ioctl.h>
53 #include <scsi/scsi_request.h>
57 /* The driver prints some debugging information on the console if DEBUG
58 is defined and non-zero. */
62 /* The message level for the debug messages is currently set to KERN_NOTICE
63 so that people can easily see the messages. Later when the debugging messages
64 in the drivers are more widely classified, this may be changed to KERN_DEBUG. */
65 #define ST_DEB_MSG KERN_NOTICE
67 #define DEBC(a) if (debugging) { a ; }
73 #define ST_KILOBYTE 1024
75 #include "st_options.h"
78 static int buffer_kbs;
79 static int max_sg_segs;
80 static int try_direct_io = TRY_DIRECT_IO;
81 static int try_rdio = 1;
82 static int try_wdio = 1;
84 static int st_dev_max;
87 static struct class *st_sysfs_class;
89 MODULE_AUTHOR("Kai Makisara");
90 MODULE_DESCRIPTION("SCSI Tape Driver");
91 MODULE_LICENSE("GPL");
93 /* Set 'perm' (4th argument) to 0 to disable module_param's definition
94 * of sysfs parameters (which module_param doesn't yet support).
95 * Sysfs parameters defined explicitly later.
97 module_param_named(buffer_kbs, buffer_kbs, int, 0);
98 MODULE_PARM_DESC(buffer_kbs, "Default driver buffer size for fixed block mode (KB; 32)");
99 module_param_named(max_sg_segs, max_sg_segs, int, 0);
100 MODULE_PARM_DESC(max_sg_segs, "Maximum number of scatter/gather segments to use (256)");
101 module_param_named(try_direct_io, try_direct_io, int, 0);
102 MODULE_PARM_DESC(try_direct_io, "Try direct I/O between user buffer and tape drive (1)");
104 /* Extra parameters for testing */
105 module_param_named(try_rdio, try_rdio, int, 0);
106 MODULE_PARM_DESC(try_rdio, "Try direct read i/o when possible");
107 module_param_named(try_wdio, try_wdio, int, 0);
108 MODULE_PARM_DESC(try_wdio, "Try direct write i/o when possible");
111 static int write_threshold_kbs; /* retained for compatibility */
112 static struct st_dev_parm {
115 } parms[] __initdata = {
117 "buffer_kbs", &buffer_kbs
119 { /* Retained for compatibility with 2.4 */
120 "write_threshold_kbs", &write_threshold_kbs
126 "try_direct_io", &try_direct_io
131 /* Restrict the number of modes so that names for all are assigned */
132 #if ST_NBR_MODES > 16
133 #error "Maximum number of modes is 16"
135 /* Bit reversed order to get same names for same minors with all
137 static char *st_formats[] = {
138 "", "r", "k", "s", "l", "t", "o", "u",
139 "m", "v", "p", "x", "a", "y", "q", "z"};
141 /* The default definitions have been moved to st_options.h */
143 #define ST_FIXED_BUFFER_SIZE (ST_FIXED_BUFFER_BLOCKS * ST_KILOBYTE)
145 /* The buffer size should fit into the 24 bits for length in the
146 6-byte SCSI read and write commands. */
147 #if ST_FIXED_BUFFER_SIZE >= (2 << 24 - 1)
148 #error "Buffer size should not exceed (2 << 24 - 1) bytes!"
151 static int debugging = DEBUG;
153 #define MAX_RETRIES 0
154 #define MAX_WRITE_RETRIES 0
155 #define MAX_READY_RETRIES 0
156 #define NO_TAPE NOT_READY
158 #define ST_TIMEOUT (900 * HZ)
159 #define ST_LONG_TIMEOUT (14000 * HZ)
161 /* Remove mode bits and auto-rewind bit (7) */
162 #define TAPE_NR(x) ( ((iminor(x) & ~255) >> (ST_NBR_MODE_BITS + 1)) | \
163 (iminor(x) & ~(-1 << ST_MODE_SHIFT)) )
164 #define TAPE_MODE(x) ((iminor(x) & ST_MODE_MASK) >> ST_MODE_SHIFT)
166 /* Construct the minor number from the device (d), mode (m), and non-rewind (n) data */
167 #define TAPE_MINOR(d, m, n) (((d & ~(255 >> (ST_NBR_MODE_BITS + 1))) << (ST_NBR_MODE_BITS + 1)) | \
168 (d & (255 >> (ST_NBR_MODE_BITS + 1))) | (m << ST_MODE_SHIFT) | ((n != 0) << 7) )
170 /* Internal ioctl to set both density (uppermost 8 bits) and blocksize (lower
172 #define SET_DENS_AND_BLK 0x10001
174 static DEFINE_RWLOCK(st_dev_arr_lock);
176 static int st_fixed_buffer_size = ST_FIXED_BUFFER_SIZE;
177 static int st_max_sg_segs = ST_MAX_SG;
179 static struct scsi_tape **scsi_tapes = NULL;
181 static int modes_defined;
183 static struct st_buffer *new_tape_buffer(int, int, int);
184 static int enlarge_buffer(struct st_buffer *, int, int);
185 static void normalize_buffer(struct st_buffer *);
186 static int append_to_buffer(const char __user *, struct st_buffer *, int);
187 static int from_buffer(struct st_buffer *, char __user *, int);
188 static void move_buffer_data(struct st_buffer *, int);
189 static void buf_to_sg(struct st_buffer *, unsigned int);
191 static int st_map_user_pages(struct scatterlist *, const unsigned int,
192 unsigned long, size_t, int, unsigned long);
193 static int sgl_map_user_pages(struct scatterlist *, const unsigned int,
194 unsigned long, size_t, int);
195 static int sgl_unmap_user_pages(struct scatterlist *, const unsigned int, int);
197 static int st_probe(struct device *);
198 static int st_remove(struct device *);
199 static int st_init_command(struct scsi_cmnd *);
201 static void do_create_driverfs_files(void);
202 static void do_remove_driverfs_files(void);
203 static void do_create_class_files(struct scsi_tape *, int, int);
205 static struct scsi_driver st_template = {
206 .owner = THIS_MODULE,
212 .init_command = st_init_command,
215 static int st_compression(struct scsi_tape *, int);
217 static int find_partition(struct scsi_tape *);
218 static int switch_partition(struct scsi_tape *);
220 static int st_int_ioctl(struct scsi_tape *, unsigned int, unsigned long);
223 #include "osst_detect.h"
224 #ifndef SIGS_FROM_OSST
225 #define SIGS_FROM_OSST \
226 {"OnStream", "SC-", "", "osst"}, \
227 {"OnStream", "DI-", "", "osst"}, \
228 {"OnStream", "DP-", "", "osst"}, \
229 {"OnStream", "USB", "", "osst"}, \
230 {"OnStream", "FW-", "", "osst"}
233 struct st_reject_data {
237 char *driver_hint; /* Name of the correct driver, NULL if unknown */
240 static struct st_reject_data reject_list[] = {
241 /* {"XXX", "Yy-", "", NULL}, example */
245 /* If the device signature is on the list of incompatible drives, the
246 function returns a pointer to the name of the correct driver (if known) */
247 static char * st_incompatible(struct scsi_device* SDp)
249 struct st_reject_data *rp;
251 for (rp=&(reject_list[0]); rp->vendor != NULL; rp++)
252 if (!strncmp(rp->vendor, SDp->vendor, strlen(rp->vendor)) &&
253 !strncmp(rp->model, SDp->model, strlen(rp->model)) &&
254 !strncmp(rp->rev, SDp->rev, strlen(rp->rev))) {
256 return rp->driver_hint;
264 static inline char *tape_name(struct scsi_tape *tape)
266 return tape->disk->disk_name;
270 static void st_analyze_sense(struct scsi_request *SRpnt, struct st_cmdstatus *s)
273 const u8 *sense = SRpnt->sr_sense_buffer;
275 s->have_sense = scsi_request_normalize_sense(SRpnt, &s->sense_hdr);
281 scsi_get_sense_info_fld(sense, SCSI_SENSE_BUFFERSIZE, &s->uremainder64);
282 switch (sense[0] & 0x7f) {
287 s->flags = sense[2] & 0xe0;
293 ucp = scsi_sense_desc_find(sense, SCSI_SENSE_BUFFERSIZE, 4);
294 s->flags = ucp ? (ucp[3] & 0xe0) : 0;
301 /* Convert the result to success code */
302 static int st_chk_result(struct scsi_tape *STp, struct scsi_request * SRpnt)
304 int result = SRpnt->sr_result;
306 DEB(const char *stp;)
307 char *name = tape_name(STp);
308 struct st_cmdstatus *cmdstatp;
313 cmdstatp = &STp->buffer->cmdstat;
314 st_analyze_sense(STp->buffer->last_SRpnt, cmdstatp);
316 if (cmdstatp->have_sense)
317 scode = STp->buffer->cmdstat.sense_hdr.sense_key;
323 printk(ST_DEB_MSG "%s: Error: %x, cmd: %x %x %x %x %x %x Len: %d\n",
325 SRpnt->sr_cmnd[0], SRpnt->sr_cmnd[1], SRpnt->sr_cmnd[2],
326 SRpnt->sr_cmnd[3], SRpnt->sr_cmnd[4], SRpnt->sr_cmnd[5],
328 if (cmdstatp->have_sense)
329 scsi_print_req_sense("st", SRpnt);
331 if (!debugging) { /* Abnormal conditions for tape */
332 if (!cmdstatp->have_sense)
334 "%s: Error %x (sugg. bt 0x%x, driver bt 0x%x, host bt 0x%x).\n",
335 name, result, suggestion(result),
336 driver_byte(result) & DRIVER_MASK, host_byte(result));
337 else if (cmdstatp->have_sense &&
339 scode != RECOVERED_ERROR &&
340 /* scode != UNIT_ATTENTION && */
341 scode != BLANK_CHECK &&
342 scode != VOLUME_OVERFLOW &&
343 SRpnt->sr_cmnd[0] != MODE_SENSE &&
344 SRpnt->sr_cmnd[0] != TEST_UNIT_READY) {
345 printk(KERN_WARNING "%s: Error with sense data: ", name);
346 scsi_print_req_sense("st", SRpnt);
350 if (cmdstatp->fixed_format &&
351 STp->cln_mode >= EXTENDED_SENSE_START) { /* Only fixed format sense */
352 if (STp->cln_sense_value)
353 STp->cleaning_req |= ((SRpnt->sr_sense_buffer[STp->cln_mode] &
354 STp->cln_sense_mask) == STp->cln_sense_value);
356 STp->cleaning_req |= ((SRpnt->sr_sense_buffer[STp->cln_mode] &
357 STp->cln_sense_mask) != 0);
359 if (cmdstatp->have_sense &&
360 cmdstatp->sense_hdr.asc == 0 && cmdstatp->sense_hdr.ascq == 0x17)
361 STp->cleaning_req = 1; /* ASC and ASCQ => cleaning requested */
363 STp->pos_unknown |= STp->device->was_reset;
365 if (cmdstatp->have_sense &&
366 scode == RECOVERED_ERROR
367 #if ST_RECOVERED_WRITE_FATAL
368 && SRpnt->sr_cmnd[0] != WRITE_6
369 && SRpnt->sr_cmnd[0] != WRITE_FILEMARKS
372 STp->recover_count++;
377 if (SRpnt->sr_cmnd[0] == READ_6)
379 else if (SRpnt->sr_cmnd[0] == WRITE_6)
383 printk(ST_DEB_MSG "%s: Recovered %s error (%d).\n", name, stp,
387 if (cmdstatp->flags == 0)
394 /* Wakeup from interrupt */
395 static void st_sleep_done(struct scsi_cmnd * SCpnt)
397 struct scsi_tape *STp = container_of(SCpnt->request->rq_disk->private_data,
398 struct scsi_tape, driver);
400 (STp->buffer)->cmdstat.midlevel_result = SCpnt->result;
401 SCpnt->request->rq_status = RQ_SCSI_DONE;
402 (STp->buffer)->last_SRpnt = SCpnt->sc_request;
403 DEB( STp->write_pending = 0; )
405 complete(SCpnt->request->waiting);
408 /* Do the scsi command. Waits until command performed if do_wait is true.
409 Otherwise write_behind_check() is used to check that the command
411 static struct scsi_request *
412 st_do_scsi(struct scsi_request * SRpnt, struct scsi_tape * STp, unsigned char *cmd,
413 int bytes, int direction, int timeout, int retries, int do_wait)
418 SRpnt = scsi_allocate_request(STp->device, GFP_ATOMIC);
420 DEBC( printk(KERN_ERR "%s: Can't get SCSI request.\n",
422 if (signal_pending(current))
423 (STp->buffer)->syscall_result = (-EINTR);
425 (STp->buffer)->syscall_result = (-EBUSY);
430 init_completion(&STp->wait);
431 SRpnt->sr_use_sg = STp->buffer->do_dio || (bytes > (STp->buffer)->frp[0].length);
432 if (SRpnt->sr_use_sg) {
433 if (!STp->buffer->do_dio)
434 buf_to_sg(STp->buffer, bytes);
435 SRpnt->sr_use_sg = (STp->buffer)->sg_segs;
436 bp = (char *) &((STp->buffer)->sg[0]);
438 bp = (STp->buffer)->b_data;
439 SRpnt->sr_data_direction = direction;
440 SRpnt->sr_cmd_len = 0;
441 SRpnt->sr_request->waiting = &(STp->wait);
442 SRpnt->sr_request->rq_status = RQ_SCSI_BUSY;
443 SRpnt->sr_request->rq_disk = STp->disk;
444 STp->buffer->cmdstat.have_sense = 0;
446 scsi_do_req(SRpnt, (void *) cmd, bp, bytes,
447 st_sleep_done, timeout, retries);
450 wait_for_completion(SRpnt->sr_request->waiting);
451 SRpnt->sr_request->waiting = NULL;
452 (STp->buffer)->syscall_result = st_chk_result(STp, SRpnt);
458 /* Handle the write-behind checking (waits for completion). Returns -ENOSPC if
459 write has been correct but EOM early warning reached, -EIO if write ended in
460 error or zero if write successful. Asynchronous writes are used only in
461 variable block mode. */
462 static int write_behind_check(struct scsi_tape * STp)
465 struct st_buffer *STbuffer;
466 struct st_partstat *STps;
467 struct st_cmdstatus *cmdstatp;
469 STbuffer = STp->buffer;
470 if (!STbuffer->writing)
474 if (STp->write_pending)
480 wait_for_completion(&(STp->wait));
481 (STp->buffer)->last_SRpnt->sr_request->waiting = NULL;
483 (STp->buffer)->syscall_result = st_chk_result(STp, (STp->buffer)->last_SRpnt);
484 scsi_release_request((STp->buffer)->last_SRpnt);
486 STbuffer->buffer_bytes -= STbuffer->writing;
487 STps = &(STp->ps[STp->partition]);
488 if (STps->drv_block >= 0) {
489 if (STp->block_size == 0)
492 STps->drv_block += STbuffer->writing / STp->block_size;
495 cmdstatp = &STbuffer->cmdstat;
496 if (STbuffer->syscall_result) {
498 if (cmdstatp->have_sense && !cmdstatp->deferred &&
499 (cmdstatp->flags & SENSE_EOM) &&
500 (cmdstatp->sense_hdr.sense_key == NO_SENSE ||
501 cmdstatp->sense_hdr.sense_key == RECOVERED_ERROR)) {
502 /* EOM at write-behind, has all data been written? */
503 if (!cmdstatp->remainder_valid ||
504 cmdstatp->uremainder64 == 0)
508 STps->drv_block = -1;
510 STbuffer->writing = 0;
512 DEB(if (debugging && retval)
513 printk(ST_DEB_MSG "%s: Async write error %x, return value %d.\n",
514 tape_name(STp), STbuffer->cmdstat.midlevel_result, retval);) /* end DEB */
520 /* Step over EOF if it has been inadvertently crossed (ioctl not used because
521 it messes up the block number). */
522 static int cross_eof(struct scsi_tape * STp, int forward)
524 struct scsi_request *SRpnt;
525 unsigned char cmd[MAX_COMMAND_SIZE];
528 cmd[1] = 0x01; /* Space FileMarks */
533 cmd[2] = cmd[3] = cmd[4] = 0xff; /* -1 filemarks */
536 DEBC(printk(ST_DEB_MSG "%s: Stepping over filemark %s.\n",
537 tape_name(STp), forward ? "forward" : "backward"));
539 SRpnt = st_do_scsi(NULL, STp, cmd, 0, DMA_NONE,
540 STp->device->timeout, MAX_RETRIES, 1);
542 return (STp->buffer)->syscall_result;
544 scsi_release_request(SRpnt);
547 if ((STp->buffer)->cmdstat.midlevel_result != 0)
548 printk(KERN_ERR "%s: Stepping over filemark %s failed.\n",
549 tape_name(STp), forward ? "forward" : "backward");
551 return (STp->buffer)->syscall_result;
555 /* Flush the write buffer (never need to write if variable blocksize). */
556 static int flush_write_buffer(struct scsi_tape * STp)
558 int offset, transfer, blks;
560 unsigned char cmd[MAX_COMMAND_SIZE];
561 struct scsi_request *SRpnt;
562 struct st_partstat *STps;
564 result = write_behind_check(STp);
569 if (STp->dirty == 1) {
571 offset = (STp->buffer)->buffer_bytes;
572 transfer = ((offset + STp->block_size - 1) /
573 STp->block_size) * STp->block_size;
574 DEBC(printk(ST_DEB_MSG "%s: Flushing %d bytes.\n",
575 tape_name(STp), transfer));
577 memset((STp->buffer)->b_data + offset, 0, transfer - offset);
579 memset(cmd, 0, MAX_COMMAND_SIZE);
582 blks = transfer / STp->block_size;
587 SRpnt = st_do_scsi(NULL, STp, cmd, transfer, DMA_TO_DEVICE,
588 STp->device->timeout, MAX_WRITE_RETRIES, 1);
590 return (STp->buffer)->syscall_result;
592 STps = &(STp->ps[STp->partition]);
593 if ((STp->buffer)->syscall_result != 0) {
594 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
596 if (cmdstatp->have_sense && !cmdstatp->deferred &&
597 (cmdstatp->flags & SENSE_EOM) &&
598 (cmdstatp->sense_hdr.sense_key == NO_SENSE ||
599 cmdstatp->sense_hdr.sense_key == RECOVERED_ERROR) &&
600 (!cmdstatp->remainder_valid ||
601 cmdstatp->uremainder64 == 0)) { /* All written at EOM early warning */
603 (STp->buffer)->buffer_bytes = 0;
604 if (STps->drv_block >= 0)
605 STps->drv_block += blks;
608 printk(KERN_ERR "%s: Error on flush.\n",
610 STps->drv_block = (-1);
614 if (STps->drv_block >= 0)
615 STps->drv_block += blks;
617 (STp->buffer)->buffer_bytes = 0;
619 scsi_release_request(SRpnt);
626 /* Flush the tape buffer. The tape will be positioned correctly unless
627 seek_next is true. */
628 static int flush_buffer(struct scsi_tape *STp, int seek_next)
630 int backspace, result;
631 struct st_buffer *STbuffer;
632 struct st_partstat *STps;
634 STbuffer = STp->buffer;
637 * If there was a bus reset, block further access
640 if (STp->pos_unknown)
643 if (STp->ready != ST_READY)
645 STps = &(STp->ps[STp->partition]);
646 if (STps->rw == ST_WRITING) /* Writing */
647 return flush_write_buffer(STp);
649 if (STp->block_size == 0)
652 backspace = ((STp->buffer)->buffer_bytes +
653 (STp->buffer)->read_pointer) / STp->block_size -
654 ((STp->buffer)->read_pointer + STp->block_size - 1) /
656 (STp->buffer)->buffer_bytes = 0;
657 (STp->buffer)->read_pointer = 0;
660 if (STps->eof == ST_FM_HIT) {
661 result = cross_eof(STp, 0); /* Back over the EOF hit */
663 STps->eof = ST_NOEOF;
665 if (STps->drv_file >= 0)
670 if (!result && backspace > 0)
671 result = st_int_ioctl(STp, MTBSR, backspace);
672 } else if (STps->eof == ST_FM_HIT) {
673 if (STps->drv_file >= 0)
676 STps->eof = ST_NOEOF;
682 /* Set the mode parameters */
683 static int set_mode_densblk(struct scsi_tape * STp, struct st_modedef * STm)
687 char *name = tape_name(STp);
689 if (!STp->density_changed &&
690 STm->default_density >= 0 &&
691 STm->default_density != STp->density) {
692 arg = STm->default_density;
696 arg <<= MT_ST_DENSITY_SHIFT;
697 if (!STp->blksize_changed &&
698 STm->default_blksize >= 0 &&
699 STm->default_blksize != STp->block_size) {
700 arg |= STm->default_blksize;
703 arg |= STp->block_size;
705 st_int_ioctl(STp, SET_DENS_AND_BLK, arg)) {
707 "%s: Can't set default block size to %d bytes and density %x.\n",
708 name, STm->default_blksize, STm->default_density);
716 /* Lock or unlock the drive door. Don't use when scsi_request allocated. */
717 static int do_door_lock(struct scsi_tape * STp, int do_lock)
720 DEB(char *name = tape_name(STp);)
723 cmd = do_lock ? SCSI_IOCTL_DOORLOCK : SCSI_IOCTL_DOORUNLOCK;
724 DEBC(printk(ST_DEB_MSG "%s: %socking drive door.\n", name,
725 do_lock ? "L" : "Unl"));
726 retval = scsi_ioctl(STp->device, cmd, NULL);
728 STp->door_locked = do_lock ? ST_LOCKED_EXPLICIT : ST_UNLOCKED;
731 STp->door_locked = ST_LOCK_FAILS;
737 /* Set the internal state after reset */
738 static void reset_state(struct scsi_tape *STp)
741 struct st_partstat *STps;
743 STp->pos_unknown = 0;
744 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
745 STps = &(STp->ps[i]);
747 STps->eof = ST_NOEOF;
749 STps->last_block_valid = 0;
750 STps->drv_block = -1;
753 if (STp->can_partitions) {
754 STp->partition = find_partition(STp);
755 if (STp->partition < 0)
757 STp->new_partition = STp->partition;
761 /* Test if the drive is ready. Returns either one of the codes below or a negative system
763 #define CHKRES_READY 0
764 #define CHKRES_NEW_SESSION 1
765 #define CHKRES_NOT_READY 2
766 #define CHKRES_NO_TAPE 3
768 #define MAX_ATTENTIONS 10
770 static int test_ready(struct scsi_tape *STp, int do_wait)
772 int attentions, waits, max_wait, scode;
773 int retval = CHKRES_READY, new_session = 0;
774 unsigned char cmd[MAX_COMMAND_SIZE];
775 struct scsi_request *SRpnt = NULL;
776 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
778 max_wait = do_wait ? ST_BLOCK_SECONDS : 0;
780 for (attentions=waits=0; ; ) {
781 memset((void *) &cmd[0], 0, MAX_COMMAND_SIZE);
782 cmd[0] = TEST_UNIT_READY;
783 SRpnt = st_do_scsi(SRpnt, STp, cmd, 0, DMA_NONE,
784 STp->long_timeout, MAX_READY_RETRIES, 1);
787 retval = (STp->buffer)->syscall_result;
791 if (cmdstatp->have_sense) {
793 scode = cmdstatp->sense_hdr.sense_key;
795 if (scode == UNIT_ATTENTION) { /* New media? */
797 if (attentions < MAX_ATTENTIONS) {
807 if (scode == NOT_READY) {
808 if (waits < max_wait) {
809 if (msleep_interruptible(1000)) {
817 if ((STp->device)->scsi_level >= SCSI_2 &&
818 cmdstatp->sense_hdr.asc == 0x3a) /* Check ASC */
819 retval = CHKRES_NO_TAPE;
821 retval = CHKRES_NOT_READY;
827 retval = (STp->buffer)->syscall_result;
829 retval = new_session ? CHKRES_NEW_SESSION : CHKRES_READY;
834 scsi_release_request(SRpnt);
839 /* See if the drive is ready and gather information about the tape. Return values:
840 < 0 negative error code from errno.h
842 1 drive not ready (possibly no tape)
844 static int check_tape(struct scsi_tape *STp, struct file *filp)
846 int i, retval, new_session = 0, do_wait;
847 unsigned char cmd[MAX_COMMAND_SIZE], saved_cleaning;
848 unsigned short st_flags = filp->f_flags;
849 struct scsi_request *SRpnt = NULL;
850 struct st_modedef *STm;
851 struct st_partstat *STps;
852 char *name = tape_name(STp);
853 struct inode *inode = filp->f_dentry->d_inode;
854 int mode = TAPE_MODE(inode);
856 STp->ready = ST_READY;
858 if (mode != STp->current_mode) {
859 DEBC(printk(ST_DEB_MSG "%s: Mode change from %d to %d.\n",
860 name, STp->current_mode, mode));
862 STp->current_mode = mode;
864 STm = &(STp->modes[STp->current_mode]);
866 saved_cleaning = STp->cleaning_req;
867 STp->cleaning_req = 0;
869 do_wait = ((filp->f_flags & O_NONBLOCK) == 0);
870 retval = test_ready(STp, do_wait);
875 if (retval == CHKRES_NEW_SESSION) {
876 STp->pos_unknown = 0;
877 STp->partition = STp->new_partition = 0;
878 if (STp->can_partitions)
879 STp->nbr_partitions = 1; /* This guess will be updated later
881 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
882 STps = &(STp->ps[i]);
884 STps->eof = ST_NOEOF;
886 STps->last_block_valid = 0;
893 STp->cleaning_req |= saved_cleaning;
895 if (retval == CHKRES_NOT_READY || retval == CHKRES_NO_TAPE) {
896 if (retval == CHKRES_NO_TAPE)
897 STp->ready = ST_NO_TAPE;
899 STp->ready = ST_NOT_READY;
901 STp->density = 0; /* Clear the erroneous "residue" */
904 STp->ps[0].drv_file = STp->ps[0].drv_block = (-1);
905 STp->partition = STp->new_partition = 0;
906 STp->door_locked = ST_UNLOCKED;
907 return CHKRES_NOT_READY;
911 if (STp->omit_blklims)
912 STp->min_block = STp->max_block = (-1);
914 memset((void *) &cmd[0], 0, MAX_COMMAND_SIZE);
915 cmd[0] = READ_BLOCK_LIMITS;
917 SRpnt = st_do_scsi(SRpnt, STp, cmd, 6, DMA_FROM_DEVICE,
918 STp->device->timeout, MAX_READY_RETRIES, 1);
920 retval = (STp->buffer)->syscall_result;
924 if (!SRpnt->sr_result && !STp->buffer->cmdstat.have_sense) {
925 STp->max_block = ((STp->buffer)->b_data[1] << 16) |
926 ((STp->buffer)->b_data[2] << 8) | (STp->buffer)->b_data[3];
927 STp->min_block = ((STp->buffer)->b_data[4] << 8) |
928 (STp->buffer)->b_data[5];
929 if ( DEB( debugging || ) !STp->inited)
931 "%s: Block limits %d - %d bytes.\n", name,
932 STp->min_block, STp->max_block);
934 STp->min_block = STp->max_block = (-1);
935 DEBC(printk(ST_DEB_MSG "%s: Can't read block limits.\n",
940 memset((void *) &cmd[0], 0, MAX_COMMAND_SIZE);
944 SRpnt = st_do_scsi(SRpnt, STp, cmd, 12, DMA_FROM_DEVICE,
945 STp->device->timeout, MAX_READY_RETRIES, 1);
947 retval = (STp->buffer)->syscall_result;
951 if ((STp->buffer)->syscall_result != 0) {
952 DEBC(printk(ST_DEB_MSG "%s: No Mode Sense.\n", name));
953 STp->block_size = ST_DEFAULT_BLOCK; /* Educated guess (?) */
954 (STp->buffer)->syscall_result = 0; /* Prevent error propagation */
955 STp->drv_write_prot = 0;
957 DEBC(printk(ST_DEB_MSG
958 "%s: Mode sense. Length %d, medium %x, WBS %x, BLL %d\n",
960 (STp->buffer)->b_data[0], (STp->buffer)->b_data[1],
961 (STp->buffer)->b_data[2], (STp->buffer)->b_data[3]));
963 if ((STp->buffer)->b_data[3] >= 8) {
964 STp->drv_buffer = ((STp->buffer)->b_data[2] >> 4) & 7;
965 STp->density = (STp->buffer)->b_data[4];
966 STp->block_size = (STp->buffer)->b_data[9] * 65536 +
967 (STp->buffer)->b_data[10] * 256 + (STp->buffer)->b_data[11];
968 DEBC(printk(ST_DEB_MSG
969 "%s: Density %x, tape length: %x, drv buffer: %d\n",
970 name, STp->density, (STp->buffer)->b_data[5] * 65536 +
971 (STp->buffer)->b_data[6] * 256 + (STp->buffer)->b_data[7],
974 STp->drv_write_prot = ((STp->buffer)->b_data[2] & 0x80) != 0;
976 scsi_release_request(SRpnt);
980 if (STp->block_size > 0)
981 (STp->buffer)->buffer_blocks =
982 (STp->buffer)->buffer_size / STp->block_size;
984 (STp->buffer)->buffer_blocks = 1;
985 (STp->buffer)->buffer_bytes = (STp->buffer)->read_pointer = 0;
987 DEBC(printk(ST_DEB_MSG
988 "%s: Block size: %d, buffer size: %d (%d blocks).\n", name,
989 STp->block_size, (STp->buffer)->buffer_size,
990 (STp->buffer)->buffer_blocks));
992 if (STp->drv_write_prot) {
995 DEBC(printk(ST_DEB_MSG "%s: Write protected\n", name));
998 ((st_flags & O_ACCMODE) == O_WRONLY ||
999 (st_flags & O_ACCMODE) == O_RDWR)) {
1005 if (STp->can_partitions && STp->nbr_partitions < 1) {
1006 /* This code is reached when the device is opened for the first time
1007 after the driver has been initialized with tape in the drive and the
1008 partition support has been enabled. */
1009 DEBC(printk(ST_DEB_MSG
1010 "%s: Updating partition number in status.\n", name));
1011 if ((STp->partition = find_partition(STp)) < 0) {
1012 retval = STp->partition;
1015 STp->new_partition = STp->partition;
1016 STp->nbr_partitions = 1; /* This guess will be updated when necessary */
1019 if (new_session) { /* Change the drive parameters for the new mode */
1020 STp->density_changed = STp->blksize_changed = 0;
1021 STp->compression_changed = 0;
1022 if (!(STm->defaults_for_writes) &&
1023 (retval = set_mode_densblk(STp, STm)) < 0)
1026 if (STp->default_drvbuffer != 0xff) {
1027 if (st_int_ioctl(STp, MTSETDRVBUFFER, STp->default_drvbuffer))
1029 "%s: Can't set default drive buffering to %d.\n",
1030 name, STp->default_drvbuffer);
1034 return CHKRES_READY;
1041 \f/* Open the device. Needs to be called with BKL only because of incrementing the SCSI host
1043 static int st_open(struct inode *inode, struct file *filp)
1045 int i, retval = (-EIO);
1046 struct scsi_tape *STp;
1047 struct st_partstat *STps;
1048 int dev = TAPE_NR(inode);
1052 * We really want to do nonseekable_open(inode, filp); here, but some
1053 * versions of tar incorrectly call lseek on tapes and bail out if that
1054 * fails. So we disallow pread() and pwrite(), but permit lseeks.
1056 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
1058 write_lock(&st_dev_arr_lock);
1059 if (dev >= st_dev_max || scsi_tapes == NULL ||
1060 ((STp = scsi_tapes[dev]) == NULL)) {
1061 write_unlock(&st_dev_arr_lock);
1064 filp->private_data = STp;
1065 name = tape_name(STp);
1068 write_unlock(&st_dev_arr_lock);
1069 DEB( printk(ST_DEB_MSG "%s: Device already in use.\n", name); )
1073 if(scsi_device_get(STp->device)) {
1074 write_unlock(&st_dev_arr_lock);
1078 write_unlock(&st_dev_arr_lock);
1079 STp->rew_at_close = STp->autorew_dev = (iminor(inode) & 0x80) == 0;
1081 if (!scsi_block_when_processing_errors(STp->device)) {
1086 /* See that we have at least a one page buffer available */
1087 if (!enlarge_buffer(STp->buffer, PAGE_SIZE, STp->restr_dma)) {
1088 printk(KERN_WARNING "%s: Can't allocate one page tape buffer.\n",
1090 retval = (-EOVERFLOW);
1094 (STp->buffer)->writing = 0;
1095 (STp->buffer)->syscall_result = 0;
1097 STp->write_prot = ((filp->f_flags & O_ACCMODE) == O_RDONLY);
1100 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
1101 STps = &(STp->ps[i]);
1104 STp->recover_count = 0;
1105 DEB( STp->nbr_waits = STp->nbr_finished = 0;
1106 STp->nbr_requests = STp->nbr_dio = STp->nbr_pages = STp->nbr_combinable = 0; )
1108 retval = check_tape(STp, filp);
1111 if ((filp->f_flags & O_NONBLOCK) == 0 &&
1112 retval != CHKRES_READY) {
1119 normalize_buffer(STp->buffer);
1121 scsi_device_put(STp->device);
1127 /* Flush the tape buffer before close */
1128 static int st_flush(struct file *filp)
1130 int result = 0, result2;
1131 unsigned char cmd[MAX_COMMAND_SIZE];
1132 struct scsi_request *SRpnt;
1133 struct scsi_tape *STp = filp->private_data;
1134 struct st_modedef *STm = &(STp->modes[STp->current_mode]);
1135 struct st_partstat *STps = &(STp->ps[STp->partition]);
1136 char *name = tape_name(STp);
1138 if (file_count(filp) > 1)
1141 if (STps->rw == ST_WRITING && !STp->pos_unknown) {
1142 result = flush_write_buffer(STp);
1143 if (result != 0 && result != (-ENOSPC))
1147 if (STp->can_partitions &&
1148 (result2 = switch_partition(STp)) < 0) {
1149 DEBC(printk(ST_DEB_MSG
1150 "%s: switch_partition at close failed.\n", name));
1156 DEBC( if (STp->nbr_requests)
1157 printk(KERN_WARNING "%s: Number of r/w requests %d, dio used in %d, pages %d (%d).\n",
1158 name, STp->nbr_requests, STp->nbr_dio, STp->nbr_pages, STp->nbr_combinable));
1160 if (STps->rw == ST_WRITING && !STp->pos_unknown) {
1161 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
1163 DEBC(printk(ST_DEB_MSG "%s: Async write waits %d, finished %d.\n",
1164 name, STp->nbr_waits, STp->nbr_finished);
1167 memset(cmd, 0, MAX_COMMAND_SIZE);
1168 cmd[0] = WRITE_FILEMARKS;
1169 cmd[4] = 1 + STp->two_fm;
1171 SRpnt = st_do_scsi(NULL, STp, cmd, 0, DMA_NONE,
1172 STp->device->timeout, MAX_WRITE_RETRIES, 1);
1174 result = (STp->buffer)->syscall_result;
1178 if (STp->buffer->syscall_result == 0 ||
1179 (cmdstatp->have_sense && !cmdstatp->deferred &&
1180 (cmdstatp->flags & SENSE_EOM) &&
1181 (cmdstatp->sense_hdr.sense_key == NO_SENSE ||
1182 cmdstatp->sense_hdr.sense_key == RECOVERED_ERROR) &&
1183 (!cmdstatp->remainder_valid || cmdstatp->uremainder64 == 0))) {
1184 /* Write successful at EOM */
1185 scsi_release_request(SRpnt);
1187 if (STps->drv_file >= 0)
1189 STps->drv_block = 0;
1194 else { /* Write error */
1195 scsi_release_request(SRpnt);
1197 printk(KERN_ERR "%s: Error on write filemark.\n", name);
1202 DEBC(printk(ST_DEB_MSG "%s: Buffer flushed, %d EOF(s) written\n",
1204 } else if (!STp->rew_at_close) {
1205 STps = &(STp->ps[STp->partition]);
1206 if (!STm->sysv || STps->rw != ST_READING) {
1208 result = flush_buffer(STp, 0);
1209 else if (STps->eof == ST_FM_HIT) {
1210 result = cross_eof(STp, 0);
1212 if (STps->drv_file >= 0)
1214 STps->drv_block = 0;
1217 STps->eof = ST_NOEOF;
1219 } else if ((STps->eof == ST_NOEOF &&
1220 !(result = cross_eof(STp, 1))) ||
1221 STps->eof == ST_FM_HIT) {
1222 if (STps->drv_file >= 0)
1224 STps->drv_block = 0;
1230 if (STp->rew_at_close) {
1231 result2 = st_int_ioctl(STp, MTREW, 1);
1239 /* Close the device and release it. BKL is not needed: this is the only thread
1240 accessing this tape. */
1241 static int st_release(struct inode *inode, struct file *filp)
1244 struct scsi_tape *STp = filp->private_data;
1246 if (STp->door_locked == ST_LOCKED_AUTO)
1247 do_door_lock(STp, 0);
1249 normalize_buffer(STp->buffer);
1250 write_lock(&st_dev_arr_lock);
1252 write_unlock(&st_dev_arr_lock);
1253 scsi_device_put(STp->device);
1258 /* The checks common to both reading and writing */
1259 static ssize_t rw_checks(struct scsi_tape *STp, struct file *filp, size_t count)
1264 * If we are in the middle of error recovery, don't let anyone
1265 * else try and use this device. Also, if error recovery fails, it
1266 * may try and take the device offline, in which case all further
1267 * access to the device is prohibited.
1269 if (!scsi_block_when_processing_errors(STp->device)) {
1274 if (STp->ready != ST_READY) {
1275 if (STp->ready == ST_NO_TAPE)
1276 retval = (-ENOMEDIUM);
1282 if (! STp->modes[STp->current_mode].defined) {
1289 * If there was a bus reset, block further access
1292 if (STp->pos_unknown) {
1302 printk(ST_DEB_MSG "%s: Incorrect device.\n", tape_name(STp));
1307 if (STp->can_partitions &&
1308 (retval = switch_partition(STp)) < 0)
1311 if (STp->block_size == 0 && STp->max_block > 0 &&
1312 (count < STp->min_block || count > STp->max_block)) {
1317 if (STp->do_auto_lock && STp->door_locked == ST_UNLOCKED &&
1318 !do_door_lock(STp, 1))
1319 STp->door_locked = ST_LOCKED_AUTO;
1326 static int setup_buffering(struct scsi_tape *STp, const char __user *buf,
1327 size_t count, int is_read)
1329 int i, bufsize, retval = 0;
1330 struct st_buffer *STbp = STp->buffer;
1333 i = STp->try_dio && try_rdio;
1335 i = STp->try_dio && try_wdio;
1336 if (i && ((unsigned long)buf & queue_dma_alignment(
1337 STp->device->request_queue)) == 0) {
1338 i = st_map_user_pages(&(STbp->sg[0]), STbp->use_sg,
1339 (unsigned long)buf, count, (is_read ? READ : WRITE),
1343 STbp->buffer_bytes = 0; /* can be used as transfer counter */
1346 STbp->do_dio = 0; /* fall back to buffering with any error */
1347 STbp->sg_segs = STbp->do_dio;
1348 STbp->frp_sg_current = 0;
1352 STp->nbr_pages += STbp->do_dio;
1353 for (i=1; i < STbp->do_dio; i++)
1354 if (page_to_pfn(STbp->sg[i].page) == page_to_pfn(STbp->sg[i-1].page) + 1)
1355 STp->nbr_combinable++;
1360 DEB( STp->nbr_requests++; )
1362 if (!STbp->do_dio) {
1363 if (STp->block_size)
1364 bufsize = STp->block_size > st_fixed_buffer_size ?
1365 STp->block_size : st_fixed_buffer_size;
1368 if (bufsize > STbp->buffer_size &&
1369 !enlarge_buffer(STbp, bufsize, STp->restr_dma)) {
1370 printk(KERN_WARNING "%s: Can't allocate %d byte tape buffer.\n",
1371 tape_name(STp), bufsize);
1372 retval = (-EOVERFLOW);
1375 if (STp->block_size)
1376 STbp->buffer_blocks = bufsize / STp->block_size;
1384 /* Can be called more than once after each setup_buffer() */
1385 static void release_buffering(struct scsi_tape *STp)
1387 struct st_buffer *STbp;
1391 sgl_unmap_user_pages(&(STbp->sg[0]), STbp->do_dio, 0);
1399 st_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
1402 ssize_t i, do_count, blks, transfer;
1404 int undone, retry_eot = 0, scode;
1406 unsigned char cmd[MAX_COMMAND_SIZE];
1407 const char __user *b_point;
1408 struct scsi_request *SRpnt = NULL;
1409 struct scsi_tape *STp = filp->private_data;
1410 struct st_modedef *STm;
1411 struct st_partstat *STps;
1412 struct st_buffer *STbp;
1413 char *name = tape_name(STp);
1415 if (down_interruptible(&STp->lock))
1416 return -ERESTARTSYS;
1418 retval = rw_checks(STp, filp, count);
1419 if (retval || count == 0)
1422 /* Write must be integral number of blocks */
1423 if (STp->block_size != 0 && (count % STp->block_size) != 0) {
1424 printk(KERN_WARNING "%s: Write not multiple of tape block size.\n",
1430 STm = &(STp->modes[STp->current_mode]);
1431 STps = &(STp->ps[STp->partition]);
1433 if (STp->write_prot) {
1439 if (STps->rw == ST_READING) {
1440 retval = flush_buffer(STp, 0);
1443 STps->rw = ST_WRITING;
1444 } else if (STps->rw != ST_WRITING &&
1445 STps->drv_file == 0 && STps->drv_block == 0) {
1446 if ((retval = set_mode_densblk(STp, STm)) < 0)
1448 if (STm->default_compression != ST_DONT_TOUCH &&
1449 !(STp->compression_changed)) {
1450 if (st_compression(STp, (STm->default_compression == ST_YES))) {
1451 printk(KERN_WARNING "%s: Can't set default compression.\n",
1453 if (modes_defined) {
1462 i = write_behind_check(STp);
1465 STps->eof = ST_EOM_OK;
1467 STps->eof = ST_EOM_ERROR;
1470 if (STps->eof == ST_EOM_OK) {
1471 STps->eof = ST_EOD_1; /* allow next write */
1475 else if (STps->eof == ST_EOM_ERROR) {
1480 /* Check the buffer readability in cases where copy_user might catch
1481 the problems after some tape movement. */
1482 if (STp->block_size != 0 &&
1484 (copy_from_user(&i, buf, 1) != 0 ||
1485 copy_from_user(&i, buf + count - 1, 1) != 0)) {
1490 retval = setup_buffering(STp, buf, count, 0);
1496 memset(cmd, 0, MAX_COMMAND_SIZE);
1498 cmd[1] = (STp->block_size != 0);
1500 STps->rw = ST_WRITING;
1503 while (count > 0 && !retry_eot) {
1509 if (STp->block_size == 0)
1512 do_count = STbp->buffer_blocks * STp->block_size -
1514 if (do_count > count)
1518 i = append_to_buffer(b_point, STbp, do_count);
1525 b_point += do_count;
1527 async_write = STp->block_size == 0 && !STbp->do_dio &&
1528 STm->do_async_writes && STps->eof < ST_EOM_OK;
1530 if (STp->block_size != 0 && STm->do_buffer_writes &&
1531 !(STp->try_dio && try_wdio) && STps->eof < ST_EOM_OK &&
1532 STbp->buffer_bytes < STbp->buffer_size) {
1534 /* Don't write a buffer that is not full enough. */
1535 if (!async_write && count == 0)
1540 if (STp->block_size == 0)
1541 blks = transfer = do_count;
1544 blks = STbp->buffer_bytes;
1547 blks /= STp->block_size;
1548 transfer = blks * STp->block_size;
1550 cmd[2] = blks >> 16;
1554 SRpnt = st_do_scsi(SRpnt, STp, cmd, transfer, DMA_TO_DEVICE,
1555 STp->device->timeout, MAX_WRITE_RETRIES, !async_write);
1557 retval = STbp->syscall_result;
1561 STbp->writing = transfer;
1562 STp->dirty = !(STbp->writing ==
1563 STbp->buffer_bytes);
1564 SRpnt = NULL; /* Prevent releasing this request! */
1565 DEB( STp->write_pending = 1; )
1569 if (STbp->syscall_result != 0) {
1570 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
1572 DEBC(printk(ST_DEB_MSG "%s: Error on write:\n", name));
1573 if (cmdstatp->have_sense && (cmdstatp->flags & SENSE_EOM)) {
1574 scode = cmdstatp->sense_hdr.sense_key;
1575 if (cmdstatp->remainder_valid)
1576 undone = (int)cmdstatp->uremainder64;
1577 else if (STp->block_size == 0 &&
1578 scode == VOLUME_OVERFLOW)
1582 if (STp->block_size != 0)
1583 undone *= STp->block_size;
1584 if (undone <= do_count) {
1585 /* Only data from this write is not written */
1588 if (STp->block_size)
1589 blks = (transfer - undone) / STp->block_size;
1590 STps->eof = ST_EOM_OK;
1591 /* Continue in fixed block mode if all written
1592 in this request but still something left to write
1593 (retval left to zero)
1595 if (STp->block_size == 0 ||
1596 undone > 0 || count == 0)
1597 retval = (-ENOSPC); /* EOM within current request */
1598 DEBC(printk(ST_DEB_MSG
1599 "%s: EOM with %d bytes unwritten.\n",
1602 /* EOT within data buffered earlier (possible only
1603 in fixed block mode without direct i/o) */
1604 if (!retry_eot && !cmdstatp->deferred &&
1605 (scode == NO_SENSE || scode == RECOVERED_ERROR)) {
1606 move_buffer_data(STp->buffer, transfer - undone);
1608 if (STps->drv_block >= 0) {
1609 STps->drv_block += (transfer - undone) /
1612 STps->eof = ST_EOM_OK;
1613 DEBC(printk(ST_DEB_MSG
1614 "%s: Retry write of %d bytes at EOM.\n",
1615 name, STp->buffer->buffer_bytes));
1619 /* Either error within data buffered by driver or
1622 blks = do_count = 0;
1623 STps->eof = ST_EOM_ERROR;
1624 STps->drv_block = (-1); /* Too cautious? */
1625 retval = (-EIO); /* EOM for old data */
1626 DEBC(printk(ST_DEB_MSG
1627 "%s: EOM with lost data.\n",
1633 STps->drv_block = (-1); /* Too cautious? */
1639 if (STps->drv_block >= 0) {
1640 if (STp->block_size == 0)
1641 STps->drv_block += (do_count > 0);
1643 STps->drv_block += blks;
1646 STbp->buffer_bytes = 0;
1649 if (retval || retry_eot) {
1651 retval = total - count;
1656 if (STps->eof == ST_EOD_1)
1657 STps->eof = ST_EOM_OK;
1658 else if (STps->eof != ST_EOM_OK)
1659 STps->eof = ST_NOEOF;
1660 retval = total - count;
1664 scsi_release_request(SRpnt);
1665 release_buffering(STp);
1671 /* Read data from the tape. Returns zero in the normal case, one if the
1672 eof status has changed, and the negative error code in case of a
1673 fatal error. Otherwise updates the buffer and the eof state.
1675 Does release user buffer mapping if it is set.
1677 static long read_tape(struct scsi_tape *STp, long count,
1678 struct scsi_request ** aSRpnt)
1680 int transfer, blks, bytes;
1681 unsigned char cmd[MAX_COMMAND_SIZE];
1682 struct scsi_request *SRpnt;
1683 struct st_modedef *STm;
1684 struct st_partstat *STps;
1685 struct st_buffer *STbp;
1687 char *name = tape_name(STp);
1692 STm = &(STp->modes[STp->current_mode]);
1693 STps = &(STp->ps[STp->partition]);
1694 if (STps->eof == ST_FM_HIT)
1698 if (STp->block_size == 0)
1699 blks = bytes = count;
1701 if (!(STp->try_dio && try_rdio) && STm->do_read_ahead) {
1702 blks = (STp->buffer)->buffer_blocks;
1703 bytes = blks * STp->block_size;
1706 if (!STbp->do_dio && bytes > (STp->buffer)->buffer_size)
1707 bytes = (STp->buffer)->buffer_size;
1708 blks = bytes / STp->block_size;
1709 bytes = blks * STp->block_size;
1713 memset(cmd, 0, MAX_COMMAND_SIZE);
1715 cmd[1] = (STp->block_size != 0);
1716 cmd[2] = blks >> 16;
1721 SRpnt = st_do_scsi(SRpnt, STp, cmd, bytes, DMA_FROM_DEVICE,
1722 STp->device->timeout, MAX_RETRIES, 1);
1723 release_buffering(STp);
1726 return STbp->syscall_result;
1728 STbp->read_pointer = 0;
1731 /* Something to check */
1732 if (STbp->syscall_result) {
1733 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
1736 DEBC(printk(ST_DEB_MSG "%s: Sense: %2x %2x %2x %2x %2x %2x %2x %2x\n",
1738 SRpnt->sr_sense_buffer[0], SRpnt->sr_sense_buffer[1],
1739 SRpnt->sr_sense_buffer[2], SRpnt->sr_sense_buffer[3],
1740 SRpnt->sr_sense_buffer[4], SRpnt->sr_sense_buffer[5],
1741 SRpnt->sr_sense_buffer[6], SRpnt->sr_sense_buffer[7]));
1742 if (cmdstatp->have_sense) {
1744 if (cmdstatp->sense_hdr.sense_key == BLANK_CHECK)
1745 cmdstatp->flags &= 0xcf; /* No need for EOM in this case */
1747 if (cmdstatp->flags != 0) { /* EOF, EOM, or ILI */
1748 /* Compute the residual count */
1749 if (cmdstatp->remainder_valid)
1750 transfer = (int)cmdstatp->uremainder64;
1753 if (STp->block_size == 0 &&
1754 cmdstatp->sense_hdr.sense_key == MEDIUM_ERROR)
1757 if (cmdstatp->flags & SENSE_ILI) { /* ILI */
1758 if (STp->block_size == 0) {
1759 if (transfer <= 0) {
1762 "%s: Failed to read %d byte block with %d byte transfer.\n",
1763 name, bytes - transfer, bytes);
1764 if (STps->drv_block >= 0)
1765 STps->drv_block += 1;
1766 STbp->buffer_bytes = 0;
1769 STbp->buffer_bytes = bytes - transfer;
1771 scsi_release_request(SRpnt);
1772 SRpnt = *aSRpnt = NULL;
1773 if (transfer == blks) { /* We did not get anything, error */
1774 printk(KERN_NOTICE "%s: Incorrect block size.\n", name);
1775 if (STps->drv_block >= 0)
1776 STps->drv_block += blks - transfer + 1;
1777 st_int_ioctl(STp, MTBSR, 1);
1780 /* We have some data, deliver it */
1781 STbp->buffer_bytes = (blks - transfer) *
1783 DEBC(printk(ST_DEB_MSG
1784 "%s: ILI but enough data received %ld %d.\n",
1785 name, count, STbp->buffer_bytes));
1786 if (STps->drv_block >= 0)
1787 STps->drv_block += 1;
1788 if (st_int_ioctl(STp, MTBSR, 1))
1791 } else if (cmdstatp->flags & SENSE_FMK) { /* FM overrides EOM */
1792 if (STps->eof != ST_FM_HIT)
1793 STps->eof = ST_FM_HIT;
1795 STps->eof = ST_EOD_2;
1796 if (STp->block_size == 0)
1797 STbp->buffer_bytes = 0;
1799 STbp->buffer_bytes =
1800 bytes - transfer * STp->block_size;
1801 DEBC(printk(ST_DEB_MSG
1802 "%s: EOF detected (%d bytes read).\n",
1803 name, STbp->buffer_bytes));
1804 } else if (cmdstatp->flags & SENSE_EOM) {
1805 if (STps->eof == ST_FM)
1806 STps->eof = ST_EOD_1;
1808 STps->eof = ST_EOM_OK;
1809 if (STp->block_size == 0)
1810 STbp->buffer_bytes = bytes - transfer;
1812 STbp->buffer_bytes =
1813 bytes - transfer * STp->block_size;
1815 DEBC(printk(ST_DEB_MSG "%s: EOM detected (%d bytes read).\n",
1816 name, STbp->buffer_bytes));
1819 /* end of EOF, EOM, ILI test */
1820 else { /* nonzero sense key */
1821 DEBC(printk(ST_DEB_MSG
1822 "%s: Tape error while reading.\n", name));
1823 STps->drv_block = (-1);
1824 if (STps->eof == ST_FM &&
1825 cmdstatp->sense_hdr.sense_key == BLANK_CHECK) {
1826 DEBC(printk(ST_DEB_MSG
1827 "%s: Zero returned for first BLANK CHECK after EOF.\n",
1829 STps->eof = ST_EOD_2; /* First BLANK_CHECK after FM */
1830 } else /* Some other extended sense code */
1834 if (STbp->buffer_bytes < 0) /* Caused by bogus sense data */
1835 STbp->buffer_bytes = 0;
1837 /* End of extended sense test */
1838 else { /* Non-extended sense */
1839 retval = STbp->syscall_result;
1843 /* End of error handling */
1844 else /* Read successful */
1845 STbp->buffer_bytes = bytes;
1847 if (STps->drv_block >= 0) {
1848 if (STp->block_size == 0)
1851 STps->drv_block += STbp->buffer_bytes / STp->block_size;
1859 st_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
1863 ssize_t i, transfer;
1864 int special, do_dio = 0;
1865 struct scsi_request *SRpnt = NULL;
1866 struct scsi_tape *STp = filp->private_data;
1867 struct st_modedef *STm;
1868 struct st_partstat *STps;
1869 struct st_buffer *STbp = STp->buffer;
1870 DEB( char *name = tape_name(STp); )
1872 if (down_interruptible(&STp->lock))
1873 return -ERESTARTSYS;
1875 retval = rw_checks(STp, filp, count);
1876 if (retval || count == 0)
1879 STm = &(STp->modes[STp->current_mode]);
1880 if (!(STm->do_read_ahead) && STp->block_size != 0 &&
1881 (count % STp->block_size) != 0) {
1882 retval = (-EINVAL); /* Read must be integral number of blocks */
1886 STps = &(STp->ps[STp->partition]);
1887 if (STps->rw == ST_WRITING) {
1888 retval = flush_buffer(STp, 0);
1891 STps->rw = ST_READING;
1894 if (debugging && STps->eof != ST_NOEOF)
1895 printk(ST_DEB_MSG "%s: EOF/EOM flag up (%d). Bytes %d\n", name,
1896 STps->eof, STbp->buffer_bytes);
1899 retval = setup_buffering(STp, buf, count, 1);
1902 do_dio = STbp->do_dio;
1904 if (STbp->buffer_bytes == 0 &&
1905 STps->eof >= ST_EOD_1) {
1906 if (STps->eof < ST_EOD) {
1911 retval = (-EIO); /* EOM or Blank Check */
1916 /* Check the buffer writability before any tape movement. Don't alter
1918 if (copy_from_user(&i, buf, 1) != 0 ||
1919 copy_to_user(buf, &i, 1) != 0 ||
1920 copy_from_user(&i, buf + count - 1, 1) != 0 ||
1921 copy_to_user(buf + count - 1, &i, 1) != 0) {
1927 STps->rw = ST_READING;
1930 /* Loop until enough data in buffer or a special condition found */
1931 for (total = 0, special = 0; total < count && !special;) {
1933 /* Get new data if the buffer is empty */
1934 if (STbp->buffer_bytes == 0) {
1935 special = read_tape(STp, count - total, &SRpnt);
1936 if (special < 0) { /* No need to continue read */
1942 /* Move the data from driver buffer to user buffer */
1943 if (STbp->buffer_bytes > 0) {
1945 if (debugging && STps->eof != ST_NOEOF)
1947 "%s: EOF up (%d). Left %d, needed %d.\n", name,
1948 STps->eof, STbp->buffer_bytes,
1949 (int)(count - total));
1951 transfer = STbp->buffer_bytes < count - total ?
1952 STbp->buffer_bytes : count - total;
1954 i = from_buffer(STbp, buf, transfer);
1964 if (STp->block_size == 0)
1965 break; /* Read only one variable length block */
1967 } /* for (total = 0, special = 0;
1968 total < count && !special; ) */
1970 /* Change the eof state if no data from tape or buffer */
1972 if (STps->eof == ST_FM_HIT) {
1974 STps->drv_block = 0;
1975 if (STps->drv_file >= 0)
1977 } else if (STps->eof == ST_EOD_1) {
1978 STps->eof = ST_EOD_2;
1979 STps->drv_block = 0;
1980 if (STps->drv_file >= 0)
1982 } else if (STps->eof == ST_EOD_2)
1984 } else if (STps->eof == ST_FM)
1985 STps->eof = ST_NOEOF;
1989 if (SRpnt != NULL) {
1990 scsi_release_request(SRpnt);
1994 release_buffering(STp);
1995 STbp->buffer_bytes = 0;
2005 /* Set the driver options */
2006 static void st_log_options(struct scsi_tape * STp, struct st_modedef * STm, char *name)
2010 "%s: Mode %d options: buffer writes: %d, async writes: %d, read ahead: %d\n",
2011 name, STp->current_mode, STm->do_buffer_writes, STm->do_async_writes,
2012 STm->do_read_ahead);
2014 "%s: can bsr: %d, two FMs: %d, fast mteom: %d, auto lock: %d,\n",
2015 name, STp->can_bsr, STp->two_fm, STp->fast_mteom, STp->do_auto_lock);
2017 "%s: defs for wr: %d, no block limits: %d, partitions: %d, s2 log: %d\n",
2018 name, STm->defaults_for_writes, STp->omit_blklims, STp->can_partitions,
2019 STp->scsi2_logical);
2021 "%s: sysv: %d nowait: %d\n", name, STm->sysv, STp->immediate);
2022 printk(KERN_INFO "%s: debugging: %d\n",
2029 static int st_set_options(struct scsi_tape *STp, long options)
2033 struct st_modedef *STm;
2034 char *name = tape_name(STp);
2035 struct cdev *cd0, *cd1;
2037 STm = &(STp->modes[STp->current_mode]);
2038 if (!STm->defined) {
2039 cd0 = STm->cdevs[0]; cd1 = STm->cdevs[1];
2040 memcpy(STm, &(STp->modes[0]), sizeof(struct st_modedef));
2041 STm->cdevs[0] = cd0; STm->cdevs[1] = cd1;
2043 DEBC(printk(ST_DEB_MSG
2044 "%s: Initialized mode %d definition from mode 0\n",
2045 name, STp->current_mode));
2048 code = options & MT_ST_OPTIONS;
2049 if (code == MT_ST_BOOLEANS) {
2050 STm->do_buffer_writes = (options & MT_ST_BUFFER_WRITES) != 0;
2051 STm->do_async_writes = (options & MT_ST_ASYNC_WRITES) != 0;
2052 STm->defaults_for_writes = (options & MT_ST_DEF_WRITES) != 0;
2053 STm->do_read_ahead = (options & MT_ST_READ_AHEAD) != 0;
2054 STp->two_fm = (options & MT_ST_TWO_FM) != 0;
2055 STp->fast_mteom = (options & MT_ST_FAST_MTEOM) != 0;
2056 STp->do_auto_lock = (options & MT_ST_AUTO_LOCK) != 0;
2057 STp->can_bsr = (options & MT_ST_CAN_BSR) != 0;
2058 STp->omit_blklims = (options & MT_ST_NO_BLKLIMS) != 0;
2059 if ((STp->device)->scsi_level >= SCSI_2)
2060 STp->can_partitions = (options & MT_ST_CAN_PARTITIONS) != 0;
2061 STp->scsi2_logical = (options & MT_ST_SCSI2LOGICAL) != 0;
2062 STp->immediate = (options & MT_ST_NOWAIT) != 0;
2063 STm->sysv = (options & MT_ST_SYSV) != 0;
2064 DEB( debugging = (options & MT_ST_DEBUGGING) != 0;
2065 st_log_options(STp, STm, name); )
2066 } else if (code == MT_ST_SETBOOLEANS || code == MT_ST_CLEARBOOLEANS) {
2067 value = (code == MT_ST_SETBOOLEANS);
2068 if ((options & MT_ST_BUFFER_WRITES) != 0)
2069 STm->do_buffer_writes = value;
2070 if ((options & MT_ST_ASYNC_WRITES) != 0)
2071 STm->do_async_writes = value;
2072 if ((options & MT_ST_DEF_WRITES) != 0)
2073 STm->defaults_for_writes = value;
2074 if ((options & MT_ST_READ_AHEAD) != 0)
2075 STm->do_read_ahead = value;
2076 if ((options & MT_ST_TWO_FM) != 0)
2077 STp->two_fm = value;
2078 if ((options & MT_ST_FAST_MTEOM) != 0)
2079 STp->fast_mteom = value;
2080 if ((options & MT_ST_AUTO_LOCK) != 0)
2081 STp->do_auto_lock = value;
2082 if ((options & MT_ST_CAN_BSR) != 0)
2083 STp->can_bsr = value;
2084 if ((options & MT_ST_NO_BLKLIMS) != 0)
2085 STp->omit_blklims = value;
2086 if ((STp->device)->scsi_level >= SCSI_2 &&
2087 (options & MT_ST_CAN_PARTITIONS) != 0)
2088 STp->can_partitions = value;
2089 if ((options & MT_ST_SCSI2LOGICAL) != 0)
2090 STp->scsi2_logical = value;
2091 if ((options & MT_ST_NOWAIT) != 0)
2092 STp->immediate = value;
2093 if ((options & MT_ST_SYSV) != 0)
2096 if ((options & MT_ST_DEBUGGING) != 0)
2098 st_log_options(STp, STm, name); )
2099 } else if (code == MT_ST_WRITE_THRESHOLD) {
2100 /* Retained for compatibility */
2101 } else if (code == MT_ST_DEF_BLKSIZE) {
2102 value = (options & ~MT_ST_OPTIONS);
2103 if (value == ~MT_ST_OPTIONS) {
2104 STm->default_blksize = (-1);
2105 DEBC( printk(KERN_INFO "%s: Default block size disabled.\n", name));
2107 STm->default_blksize = value;
2108 DEBC( printk(KERN_INFO "%s: Default block size set to %d bytes.\n",
2109 name, STm->default_blksize));
2110 if (STp->ready == ST_READY) {
2111 STp->blksize_changed = 0;
2112 set_mode_densblk(STp, STm);
2115 } else if (code == MT_ST_TIMEOUTS) {
2116 value = (options & ~MT_ST_OPTIONS);
2117 if ((value & MT_ST_SET_LONG_TIMEOUT) != 0) {
2118 STp->long_timeout = (value & ~MT_ST_SET_LONG_TIMEOUT) * HZ;
2119 DEBC( printk(KERN_INFO "%s: Long timeout set to %d seconds.\n", name,
2120 (value & ~MT_ST_SET_LONG_TIMEOUT)));
2122 STp->device->timeout = value * HZ;
2123 DEBC( printk(KERN_INFO "%s: Normal timeout set to %d seconds.\n",
2126 } else if (code == MT_ST_SET_CLN) {
2127 value = (options & ~MT_ST_OPTIONS) & 0xff;
2129 value < EXTENDED_SENSE_START && value >= SCSI_SENSE_BUFFERSIZE)
2131 STp->cln_mode = value;
2132 STp->cln_sense_mask = (options >> 8) & 0xff;
2133 STp->cln_sense_value = (options >> 16) & 0xff;
2135 "%s: Cleaning request mode %d, mask %02x, value %02x\n",
2136 name, value, STp->cln_sense_mask, STp->cln_sense_value);
2137 } else if (code == MT_ST_DEF_OPTIONS) {
2138 code = (options & ~MT_ST_CLEAR_DEFAULT);
2139 value = (options & MT_ST_CLEAR_DEFAULT);
2140 if (code == MT_ST_DEF_DENSITY) {
2141 if (value == MT_ST_CLEAR_DEFAULT) {
2142 STm->default_density = (-1);
2143 DEBC( printk(KERN_INFO "%s: Density default disabled.\n",
2146 STm->default_density = value & 0xff;
2147 DEBC( printk(KERN_INFO "%s: Density default set to %x\n",
2148 name, STm->default_density));
2149 if (STp->ready == ST_READY) {
2150 STp->density_changed = 0;
2151 set_mode_densblk(STp, STm);
2154 } else if (code == MT_ST_DEF_DRVBUFFER) {
2155 if (value == MT_ST_CLEAR_DEFAULT) {
2156 STp->default_drvbuffer = 0xff;
2157 DEBC( printk(KERN_INFO
2158 "%s: Drive buffer default disabled.\n", name));
2160 STp->default_drvbuffer = value & 7;
2161 DEBC( printk(KERN_INFO
2162 "%s: Drive buffer default set to %x\n",
2163 name, STp->default_drvbuffer));
2164 if (STp->ready == ST_READY)
2165 st_int_ioctl(STp, MTSETDRVBUFFER, STp->default_drvbuffer);
2167 } else if (code == MT_ST_DEF_COMPRESSION) {
2168 if (value == MT_ST_CLEAR_DEFAULT) {
2169 STm->default_compression = ST_DONT_TOUCH;
2170 DEBC( printk(KERN_INFO
2171 "%s: Compression default disabled.\n", name));
2173 if ((value & 0xff00) != 0) {
2174 STp->c_algo = (value & 0xff00) >> 8;
2175 DEBC( printk(KERN_INFO "%s: Compression algorithm set to 0x%x.\n",
2176 name, STp->c_algo));
2178 if ((value & 0xff) != 0xff) {
2179 STm->default_compression = (value & 1 ? ST_YES : ST_NO);
2180 DEBC( printk(KERN_INFO "%s: Compression default set to %x\n",
2181 name, (value & 1)));
2182 if (STp->ready == ST_READY) {
2183 STp->compression_changed = 0;
2184 st_compression(STp, (STm->default_compression == ST_YES));
2195 #define MODE_HEADER_LENGTH 4
2197 /* Mode header and page byte offsets */
2198 #define MH_OFF_DATA_LENGTH 0
2199 #define MH_OFF_MEDIUM_TYPE 1
2200 #define MH_OFF_DEV_SPECIFIC 2
2201 #define MH_OFF_BDESCS_LENGTH 3
2202 #define MP_OFF_PAGE_NBR 0
2203 #define MP_OFF_PAGE_LENGTH 1
2205 /* Mode header and page bit masks */
2206 #define MH_BIT_WP 0x80
2207 #define MP_MSK_PAGE_NBR 0x3f
2209 /* Don't return block descriptors */
2210 #define MODE_SENSE_OMIT_BDESCS 0x08
2212 #define MODE_SELECT_PAGE_FORMAT 0x10
2214 /* Read a mode page into the tape buffer. The block descriptors are included
2215 if incl_block_descs is true. The page control is ored to the page number
2216 parameter, if necessary. */
2217 static int read_mode_page(struct scsi_tape *STp, int page, int omit_block_descs)
2219 unsigned char cmd[MAX_COMMAND_SIZE];
2220 struct scsi_request *SRpnt = NULL;
2222 memset(cmd, 0, MAX_COMMAND_SIZE);
2223 cmd[0] = MODE_SENSE;
2224 if (omit_block_descs)
2225 cmd[1] = MODE_SENSE_OMIT_BDESCS;
2229 SRpnt = st_do_scsi(SRpnt, STp, cmd, cmd[4], DMA_FROM_DEVICE,
2230 STp->device->timeout, 0, 1);
2232 return (STp->buffer)->syscall_result;
2234 scsi_release_request(SRpnt);
2236 return (STp->buffer)->syscall_result;
2240 /* Send the mode page in the tape buffer to the drive. Assumes that the mode data
2241 in the buffer is correctly formatted. The long timeout is used if slow is non-zero. */
2242 static int write_mode_page(struct scsi_tape *STp, int page, int slow)
2245 unsigned char cmd[MAX_COMMAND_SIZE];
2246 struct scsi_request *SRpnt = NULL;
2248 memset(cmd, 0, MAX_COMMAND_SIZE);
2249 cmd[0] = MODE_SELECT;
2250 cmd[1] = MODE_SELECT_PAGE_FORMAT;
2251 pgo = MODE_HEADER_LENGTH + (STp->buffer)->b_data[MH_OFF_BDESCS_LENGTH];
2252 cmd[4] = pgo + (STp->buffer)->b_data[pgo + MP_OFF_PAGE_LENGTH] + 2;
2254 /* Clear reserved fields */
2255 (STp->buffer)->b_data[MH_OFF_DATA_LENGTH] = 0;
2256 (STp->buffer)->b_data[MH_OFF_MEDIUM_TYPE] = 0;
2257 (STp->buffer)->b_data[MH_OFF_DEV_SPECIFIC] &= ~MH_BIT_WP;
2258 (STp->buffer)->b_data[pgo + MP_OFF_PAGE_NBR] &= MP_MSK_PAGE_NBR;
2260 SRpnt = st_do_scsi(SRpnt, STp, cmd, cmd[4], DMA_TO_DEVICE,
2261 (slow ? STp->long_timeout : STp->device->timeout), 0, 1);
2263 return (STp->buffer)->syscall_result;
2265 scsi_release_request(SRpnt);
2267 return (STp->buffer)->syscall_result;
2271 #define COMPRESSION_PAGE 0x0f
2272 #define COMPRESSION_PAGE_LENGTH 16
2274 #define CP_OFF_DCE_DCC 2
2275 #define CP_OFF_C_ALGO 7
2277 #define DCE_MASK 0x80
2278 #define DCC_MASK 0x40
2279 #define RED_MASK 0x60
2282 /* Control the compression with mode page 15. Algorithm not changed if zero.
2284 The block descriptors are read and written because Sony SDT-7000 does not
2285 work without this (suggestion from Michael Schaefer <Michael.Schaefer@dlr.de>).
2286 Including block descriptors should not cause any harm to other drives. */
2288 static int st_compression(struct scsi_tape * STp, int state)
2291 int mpoffs; /* Offset to mode page start */
2292 unsigned char *b_data = (STp->buffer)->b_data;
2293 DEB( char *name = tape_name(STp); )
2295 if (STp->ready != ST_READY)
2298 /* Read the current page contents */
2299 retval = read_mode_page(STp, COMPRESSION_PAGE, 0);
2301 DEBC(printk(ST_DEB_MSG "%s: Compression mode page not supported.\n",
2306 mpoffs = MODE_HEADER_LENGTH + b_data[MH_OFF_BDESCS_LENGTH];
2307 DEBC(printk(ST_DEB_MSG "%s: Compression state is %d.\n", name,
2308 (b_data[mpoffs + CP_OFF_DCE_DCC] & DCE_MASK ? 1 : 0)));
2310 /* Check if compression can be changed */
2311 if ((b_data[mpoffs + CP_OFF_DCE_DCC] & DCC_MASK) == 0) {
2312 DEBC(printk(ST_DEB_MSG "%s: Compression not supported.\n", name));
2318 b_data[mpoffs + CP_OFF_DCE_DCC] |= DCE_MASK;
2319 if (STp->c_algo != 0)
2320 b_data[mpoffs + CP_OFF_C_ALGO] = STp->c_algo;
2323 b_data[mpoffs + CP_OFF_DCE_DCC] &= ~DCE_MASK;
2324 if (STp->c_algo != 0)
2325 b_data[mpoffs + CP_OFF_C_ALGO] = 0; /* no compression */
2328 retval = write_mode_page(STp, COMPRESSION_PAGE, 0);
2330 DEBC(printk(ST_DEB_MSG "%s: Compression change failed.\n", name));
2333 DEBC(printk(ST_DEB_MSG "%s: Compression state changed to %d.\n",
2336 STp->compression_changed = 1;
2341 /* Process the load and unload commands (does unload if the load code is zero) */
2342 static int do_load_unload(struct scsi_tape *STp, struct file *filp, int load_code)
2344 int retval = (-EIO), timeout;
2345 DEB( char *name = tape_name(STp); )
2346 unsigned char cmd[MAX_COMMAND_SIZE];
2347 struct st_partstat *STps;
2348 struct scsi_request *SRpnt;
2350 if (STp->ready != ST_READY && !load_code) {
2351 if (STp->ready == ST_NO_TAPE)
2352 return (-ENOMEDIUM);
2357 memset(cmd, 0, MAX_COMMAND_SIZE);
2358 cmd[0] = START_STOP;
2362 * If arg >= 1 && arg <= 6 Enhanced load/unload in HP C1553A
2364 if (load_code >= 1 + MT_ST_HPLOADER_OFFSET
2365 && load_code <= 6 + MT_ST_HPLOADER_OFFSET) {
2366 DEBC(printk(ST_DEB_MSG "%s: Enhanced %sload slot %2d.\n",
2367 name, (cmd[4]) ? "" : "un",
2368 load_code - MT_ST_HPLOADER_OFFSET));
2369 cmd[3] = load_code - MT_ST_HPLOADER_OFFSET; /* MediaID field of C1553A */
2371 if (STp->immediate) {
2372 cmd[1] = 1; /* Don't wait for completion */
2373 timeout = STp->device->timeout;
2376 timeout = STp->long_timeout;
2380 printk(ST_DEB_MSG "%s: Unloading tape.\n", name);
2382 printk(ST_DEB_MSG "%s: Loading tape.\n", name);
2385 SRpnt = st_do_scsi(NULL, STp, cmd, 0, DMA_NONE,
2386 timeout, MAX_RETRIES, 1);
2388 return (STp->buffer)->syscall_result;
2390 retval = (STp->buffer)->syscall_result;
2391 scsi_release_request(SRpnt);
2393 if (!retval) { /* SCSI command successful */
2396 STp->rew_at_close = 0;
2397 STp->ready = ST_NO_TAPE;
2400 STp->rew_at_close = STp->autorew_dev;
2401 retval = check_tape(STp, filp);
2407 STps = &(STp->ps[STp->partition]);
2408 STps->drv_file = STps->drv_block = (-1);
2415 #define ST_DEB_FORWARD 0
2416 #define ST_DEB_BACKWARD 1
2417 static void deb_space_print(char *name, int direction, char *units, unsigned char *cmd)
2421 sc = cmd[2] & 0x80 ? 0xff000000 : 0;
2422 sc |= (cmd[2] << 16) | (cmd[3] << 8) | cmd[4];
2425 printk(ST_DEB_MSG "%s: Spacing tape %s over %d %s.\n", name,
2426 direction ? "backward" : "forward", sc, units);
2431 /* Internal ioctl function */
2432 static int st_int_ioctl(struct scsi_tape *STp, unsigned int cmd_in, unsigned long arg)
2438 unsigned char cmd[MAX_COMMAND_SIZE];
2439 struct scsi_request *SRpnt;
2440 struct st_partstat *STps;
2441 int fileno, blkno, at_sm, undone;
2442 int datalen = 0, direction = DMA_NONE;
2443 char *name = tape_name(STp);
2445 WARN_ON(STp->buffer->do_dio != 0);
2446 if (STp->ready != ST_READY) {
2447 if (STp->ready == ST_NO_TAPE)
2448 return (-ENOMEDIUM);
2452 timeout = STp->long_timeout;
2453 STps = &(STp->ps[STp->partition]);
2454 fileno = STps->drv_file;
2455 blkno = STps->drv_block;
2456 at_sm = STps->at_sm;
2458 memset(cmd, 0, MAX_COMMAND_SIZE);
2461 chg_eof = 0; /* Changed from the FSF after this */
2464 cmd[1] = 0x01; /* Space FileMarks */
2465 cmd[2] = (arg >> 16);
2466 cmd[3] = (arg >> 8);
2468 DEBC(deb_space_print(name, ST_DEB_FORWARD, "filemarks", cmd);)
2472 at_sm &= (arg == 0);
2475 chg_eof = 0; /* Changed from the FSF after this */
2478 cmd[1] = 0x01; /* Space FileMarks */
2480 cmd[2] = (ltmp >> 16);
2481 cmd[3] = (ltmp >> 8);
2483 DEBC(deb_space_print(name, ST_DEB_BACKWARD, "filemarks", cmd);)
2486 blkno = (-1); /* We can't know the block number */
2487 at_sm &= (arg == 0);
2491 cmd[1] = 0x00; /* Space Blocks */
2492 cmd[2] = (arg >> 16);
2493 cmd[3] = (arg >> 8);
2495 DEBC(deb_space_print(name, ST_DEB_FORWARD, "blocks", cmd);)
2498 at_sm &= (arg == 0);
2502 cmd[1] = 0x00; /* Space Blocks */
2504 cmd[2] = (ltmp >> 16);
2505 cmd[3] = (ltmp >> 8);
2507 DEBC(deb_space_print(name, ST_DEB_BACKWARD, "blocks", cmd);)
2510 at_sm &= (arg == 0);
2514 cmd[1] = 0x04; /* Space Setmarks */
2515 cmd[2] = (arg >> 16);
2516 cmd[3] = (arg >> 8);
2518 DEBC(deb_space_print(name, ST_DEB_FORWARD, "setmarks", cmd);)
2520 blkno = fileno = (-1);
2526 cmd[1] = 0x04; /* Space Setmarks */
2528 cmd[2] = (ltmp >> 16);
2529 cmd[3] = (ltmp >> 8);
2531 DEBC(deb_space_print(name, ST_DEB_BACKWARD, "setmarks", cmd);)
2533 blkno = fileno = (-1);
2539 if (STp->write_prot)
2541 cmd[0] = WRITE_FILEMARKS;
2542 if (cmd_in == MTWSM)
2544 cmd[2] = (arg >> 16);
2545 cmd[3] = (arg >> 8);
2547 timeout = STp->device->timeout;
2549 if (cmd_in == MTWEOF)
2550 printk(ST_DEB_MSG "%s: Writing %d filemarks.\n", name,
2551 cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
2553 printk(ST_DEB_MSG "%s: Writing %d setmarks.\n", name,
2554 cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
2559 at_sm = (cmd_in == MTWSM);
2562 cmd[0] = REZERO_UNIT;
2563 if (STp->immediate) {
2564 cmd[1] = 1; /* Don't wait for completion */
2565 timeout = STp->device->timeout;
2567 DEBC(printk(ST_DEB_MSG "%s: Rewinding tape.\n", name));
2568 fileno = blkno = at_sm = 0;
2571 DEBC(printk(ST_DEB_MSG "%s: No op on tape.\n", name));
2572 return 0; /* Should do something ? */
2575 cmd[0] = START_STOP;
2576 if (STp->immediate) {
2577 cmd[1] = 1; /* Don't wait for completion */
2578 timeout = STp->device->timeout;
2581 DEBC(printk(ST_DEB_MSG "%s: Retensioning tape.\n", name));
2582 fileno = blkno = at_sm = 0;
2585 if (!STp->fast_mteom) {
2586 /* space to the end of tape */
2587 ioctl_result = st_int_ioctl(STp, MTFSF, 0x7fffff);
2588 fileno = STps->drv_file;
2589 if (STps->eof >= ST_EOD_1)
2591 /* The next lines would hide the number of spaced FileMarks
2592 That's why I inserted the previous lines. I had no luck
2593 with detecting EOM with FSF, so we go now to EOM.
2599 DEBC(printk(ST_DEB_MSG "%s: Spacing to end of recorded medium.\n",
2605 if (STp->write_prot)
2608 cmd[1] = (arg ? 1 : 0); /* Long erase with non-zero argument */
2609 if (STp->immediate) {
2610 cmd[1] |= 2; /* Don't wait for completion */
2611 timeout = STp->device->timeout;
2614 timeout = STp->long_timeout * 8;
2616 DEBC(printk(ST_DEB_MSG "%s: Erasing tape.\n", name));
2617 fileno = blkno = at_sm = 0;
2619 case MTSETBLK: /* Set block length */
2620 case MTSETDENSITY: /* Set tape density */
2621 case MTSETDRVBUFFER: /* Set drive buffering */
2622 case SET_DENS_AND_BLK: /* Set density and block size */
2624 if (STp->dirty || (STp->buffer)->buffer_bytes != 0)
2625 return (-EIO); /* Not allowed if data in buffer */
2626 if ((cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) &&
2627 (arg & MT_ST_BLKSIZE_MASK) != 0 &&
2628 STp->max_block > 0 &&
2629 ((arg & MT_ST_BLKSIZE_MASK) < STp->min_block ||
2630 (arg & MT_ST_BLKSIZE_MASK) > STp->max_block)) {
2631 printk(KERN_WARNING "%s: Illegal block size.\n", name);
2634 cmd[0] = MODE_SELECT;
2635 if ((STp->use_pf & USE_PF))
2636 cmd[1] = MODE_SELECT_PAGE_FORMAT;
2637 cmd[4] = datalen = 12;
2638 direction = DMA_TO_DEVICE;
2640 memset((STp->buffer)->b_data, 0, 12);
2641 if (cmd_in == MTSETDRVBUFFER)
2642 (STp->buffer)->b_data[2] = (arg & 7) << 4;
2644 (STp->buffer)->b_data[2] =
2645 STp->drv_buffer << 4;
2646 (STp->buffer)->b_data[3] = 8; /* block descriptor length */
2647 if (cmd_in == MTSETDENSITY) {
2648 (STp->buffer)->b_data[4] = arg;
2649 STp->density_changed = 1; /* At least we tried ;-) */
2650 } else if (cmd_in == SET_DENS_AND_BLK)
2651 (STp->buffer)->b_data[4] = arg >> 24;
2653 (STp->buffer)->b_data[4] = STp->density;
2654 if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) {
2655 ltmp = arg & MT_ST_BLKSIZE_MASK;
2656 if (cmd_in == MTSETBLK)
2657 STp->blksize_changed = 1; /* At least we tried ;-) */
2659 ltmp = STp->block_size;
2660 (STp->buffer)->b_data[9] = (ltmp >> 16);
2661 (STp->buffer)->b_data[10] = (ltmp >> 8);
2662 (STp->buffer)->b_data[11] = ltmp;
2663 timeout = STp->device->timeout;
2665 if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK)
2667 "%s: Setting block size to %d bytes.\n", name,
2668 (STp->buffer)->b_data[9] * 65536 +
2669 (STp->buffer)->b_data[10] * 256 +
2670 (STp->buffer)->b_data[11]);
2671 if (cmd_in == MTSETDENSITY || cmd_in == SET_DENS_AND_BLK)
2673 "%s: Setting density code to %x.\n", name,
2674 (STp->buffer)->b_data[4]);
2675 if (cmd_in == MTSETDRVBUFFER)
2677 "%s: Setting drive buffer code to %d.\n", name,
2678 ((STp->buffer)->b_data[2] >> 4) & 7);
2685 SRpnt = st_do_scsi(NULL, STp, cmd, datalen, direction,
2686 timeout, MAX_RETRIES, 1);
2688 return (STp->buffer)->syscall_result;
2690 ioctl_result = (STp->buffer)->syscall_result;
2692 if (!ioctl_result) { /* SCSI command successful */
2693 scsi_release_request(SRpnt);
2695 STps->drv_block = blkno;
2696 STps->drv_file = fileno;
2697 STps->at_sm = at_sm;
2699 if (cmd_in == MTBSFM)
2700 ioctl_result = st_int_ioctl(STp, MTFSF, 1);
2701 else if (cmd_in == MTFSFM)
2702 ioctl_result = st_int_ioctl(STp, MTBSF, 1);
2704 if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) {
2705 int old_block_size = STp->block_size;
2706 STp->block_size = arg & MT_ST_BLKSIZE_MASK;
2707 if (STp->block_size != 0) {
2708 if (old_block_size == 0)
2709 normalize_buffer(STp->buffer);
2710 (STp->buffer)->buffer_blocks =
2711 (STp->buffer)->buffer_size / STp->block_size;
2713 (STp->buffer)->buffer_bytes = (STp->buffer)->read_pointer = 0;
2714 if (cmd_in == SET_DENS_AND_BLK)
2715 STp->density = arg >> MT_ST_DENSITY_SHIFT;
2716 } else if (cmd_in == MTSETDRVBUFFER)
2717 STp->drv_buffer = (arg & 7);
2718 else if (cmd_in == MTSETDENSITY)
2721 if (cmd_in == MTEOM)
2723 else if (cmd_in == MTFSF)
2726 STps->eof = ST_NOEOF;
2728 if (cmd_in == MTWEOF)
2730 } else { /* SCSI command was not completely successful. Don't return
2731 from this block without releasing the SCSI command block! */
2732 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
2734 if (cmdstatp->flags & SENSE_EOM) {
2735 if (cmd_in != MTBSF && cmd_in != MTBSFM &&
2736 cmd_in != MTBSR && cmd_in != MTBSS)
2737 STps->eof = ST_EOM_OK;
2738 STps->drv_block = 0;
2741 if (cmdstatp->remainder_valid)
2742 undone = (int)cmdstatp->uremainder64;
2746 if (cmd_in == MTWEOF &&
2747 cmdstatp->have_sense &&
2748 (cmdstatp->flags & SENSE_EOM) &&
2749 (cmdstatp->sense_hdr.sense_key == NO_SENSE ||
2750 cmdstatp->sense_hdr.sense_key == RECOVERED_ERROR) &&
2752 ioctl_result = 0; /* EOF written succesfully at EOM */
2755 STps->drv_file = fileno;
2756 STps->eof = ST_NOEOF;
2757 } else if ((cmd_in == MTFSF) || (cmd_in == MTFSFM)) {
2759 STps->drv_file = fileno - undone;
2761 STps->drv_file = fileno;
2762 STps->drv_block = -1;
2763 STps->eof = ST_NOEOF;
2764 } else if ((cmd_in == MTBSF) || (cmd_in == MTBSFM)) {
2765 if (arg > 0 && undone < 0) /* Some drives get this wrong */
2767 if (STps->drv_file >= 0)
2768 STps->drv_file = fileno + undone;
2769 STps->drv_block = 0;
2770 STps->eof = ST_NOEOF;
2771 } else if (cmd_in == MTFSR) {
2772 if (cmdstatp->flags & SENSE_FMK) { /* Hit filemark */
2773 if (STps->drv_file >= 0)
2775 STps->drv_block = 0;
2778 if (blkno >= undone)
2779 STps->drv_block = blkno - undone;
2781 STps->drv_block = (-1);
2782 STps->eof = ST_NOEOF;
2784 } else if (cmd_in == MTBSR) {
2785 if (cmdstatp->flags & SENSE_FMK) { /* Hit filemark */
2787 STps->drv_block = (-1);
2789 if (arg > 0 && undone < 0) /* Some drives get this wrong */
2791 if (STps->drv_block >= 0)
2792 STps->drv_block = blkno + undone;
2794 STps->eof = ST_NOEOF;
2795 } else if (cmd_in == MTEOM) {
2796 STps->drv_file = (-1);
2797 STps->drv_block = (-1);
2799 } else if (cmd_in == MTSETBLK ||
2800 cmd_in == MTSETDENSITY ||
2801 cmd_in == MTSETDRVBUFFER ||
2802 cmd_in == SET_DENS_AND_BLK) {
2803 if (cmdstatp->sense_hdr.sense_key == ILLEGAL_REQUEST &&
2804 !(STp->use_pf & PF_TESTED)) {
2805 /* Try the other possible state of Page Format if not
2807 STp->use_pf = !STp->use_pf | PF_TESTED;
2808 scsi_release_request(SRpnt);
2810 return st_int_ioctl(STp, cmd_in, arg);
2813 STps->eof = ST_NOEOF;
2815 if (cmdstatp->sense_hdr.sense_key == BLANK_CHECK)
2818 scsi_release_request(SRpnt);
2822 return ioctl_result;
2826 /* Get the tape position. If bt == 2, arg points into a kernel space mt_loc
2829 static int get_location(struct scsi_tape *STp, unsigned int *block, int *partition,
2833 unsigned char scmd[MAX_COMMAND_SIZE];
2834 struct scsi_request *SRpnt;
2835 DEB( char *name = tape_name(STp); )
2837 if (STp->ready != ST_READY)
2840 memset(scmd, 0, MAX_COMMAND_SIZE);
2841 if ((STp->device)->scsi_level < SCSI_2) {
2842 scmd[0] = QFA_REQUEST_BLOCK;
2845 scmd[0] = READ_POSITION;
2846 if (!logical && !STp->scsi2_logical)
2849 SRpnt = st_do_scsi(NULL, STp, scmd, 20, DMA_FROM_DEVICE,
2850 STp->device->timeout, MAX_READY_RETRIES, 1);
2852 return (STp->buffer)->syscall_result;
2854 if ((STp->buffer)->syscall_result != 0 ||
2855 (STp->device->scsi_level >= SCSI_2 &&
2856 ((STp->buffer)->b_data[0] & 4) != 0)) {
2857 *block = *partition = 0;
2858 DEBC(printk(ST_DEB_MSG "%s: Can't read tape position.\n", name));
2862 if ((STp->device)->scsi_level < SCSI_2) {
2863 *block = ((STp->buffer)->b_data[0] << 16)
2864 + ((STp->buffer)->b_data[1] << 8)
2865 + (STp->buffer)->b_data[2];
2868 *block = ((STp->buffer)->b_data[4] << 24)
2869 + ((STp->buffer)->b_data[5] << 16)
2870 + ((STp->buffer)->b_data[6] << 8)
2871 + (STp->buffer)->b_data[7];
2872 *partition = (STp->buffer)->b_data[1];
2873 if (((STp->buffer)->b_data[0] & 0x80) &&
2874 (STp->buffer)->b_data[1] == 0) /* BOP of partition 0 */
2875 STp->ps[0].drv_block = STp->ps[0].drv_file = 0;
2877 DEBC(printk(ST_DEB_MSG "%s: Got tape pos. blk %d part %d.\n", name,
2878 *block, *partition));
2880 scsi_release_request(SRpnt);
2887 /* Set the tape block and partition. Negative partition means that only the
2888 block should be set in vendor specific way. */
2889 static int set_location(struct scsi_tape *STp, unsigned int block, int partition,
2892 struct st_partstat *STps;
2896 unsigned char scmd[MAX_COMMAND_SIZE];
2897 struct scsi_request *SRpnt;
2898 DEB( char *name = tape_name(STp); )
2900 if (STp->ready != ST_READY)
2902 timeout = STp->long_timeout;
2903 STps = &(STp->ps[STp->partition]);
2905 DEBC(printk(ST_DEB_MSG "%s: Setting block to %d and partition to %d.\n",
2906 name, block, partition));
2907 DEB(if (partition < 0)
2910 /* Update the location at the partition we are leaving */
2911 if ((!STp->can_partitions && partition != 0) ||
2912 partition >= ST_NBR_PARTITIONS)
2914 if (partition != STp->partition) {
2915 if (get_location(STp, &blk, &p, 1))
2916 STps->last_block_valid = 0;
2918 STps->last_block_valid = 1;
2919 STps->last_block_visited = blk;
2920 DEBC(printk(ST_DEB_MSG
2921 "%s: Visited block %d for partition %d saved.\n",
2922 name, blk, STp->partition));
2926 memset(scmd, 0, MAX_COMMAND_SIZE);
2927 if ((STp->device)->scsi_level < SCSI_2) {
2928 scmd[0] = QFA_SEEK_BLOCK;
2929 scmd[2] = (block >> 16);
2930 scmd[3] = (block >> 8);
2935 scmd[3] = (block >> 24);
2936 scmd[4] = (block >> 16);
2937 scmd[5] = (block >> 8);
2939 if (!logical && !STp->scsi2_logical)
2941 if (STp->partition != partition) {
2943 scmd[8] = partition;
2944 DEBC(printk(ST_DEB_MSG
2945 "%s: Trying to change partition from %d to %d\n",
2946 name, STp->partition, partition));
2949 if (STp->immediate) {
2950 scmd[1] |= 1; /* Don't wait for completion */
2951 timeout = STp->device->timeout;
2954 SRpnt = st_do_scsi(NULL, STp, scmd, 0, DMA_NONE,
2955 timeout, MAX_READY_RETRIES, 1);
2957 return (STp->buffer)->syscall_result;
2959 STps->drv_block = STps->drv_file = (-1);
2960 STps->eof = ST_NOEOF;
2961 if ((STp->buffer)->syscall_result != 0) {
2963 if (STp->can_partitions &&
2964 (STp->device)->scsi_level >= SCSI_2 &&
2965 (p = find_partition(STp)) >= 0)
2968 if (STp->can_partitions) {
2969 STp->partition = partition;
2970 STps = &(STp->ps[partition]);
2971 if (!STps->last_block_valid ||
2972 STps->last_block_visited != block) {
2979 STps->drv_block = STps->drv_file = 0;
2983 scsi_release_request(SRpnt);
2990 /* Find the current partition number for the drive status. Called from open and
2991 returns either partition number of negative error code. */
2992 static int find_partition(struct scsi_tape *STp)
2997 if ((i = get_location(STp, &block, &partition, 1)) < 0)
2999 if (partition >= ST_NBR_PARTITIONS)
3005 /* Change the partition if necessary */
3006 static int switch_partition(struct scsi_tape *STp)
3008 struct st_partstat *STps;
3010 if (STp->partition == STp->new_partition)
3012 STps = &(STp->ps[STp->new_partition]);
3013 if (!STps->last_block_valid)
3014 STps->last_block_visited = 0;
3015 return set_location(STp, STps->last_block_visited, STp->new_partition, 1);
3018 /* Functions for reading and writing the medium partition mode page. */
3020 #define PART_PAGE 0x11
3021 #define PART_PAGE_FIXED_LENGTH 8
3023 #define PP_OFF_MAX_ADD_PARTS 2
3024 #define PP_OFF_NBR_ADD_PARTS 3
3025 #define PP_OFF_FLAGS 4
3026 #define PP_OFF_PART_UNITS 6
3027 #define PP_OFF_RESERVED 7
3029 #define PP_BIT_IDP 0x20
3030 #define PP_MSK_PSUM_MB 0x10
3032 /* Get the number of partitions on the tape. As a side effect reads the
3033 mode page into the tape buffer. */
3034 static int nbr_partitions(struct scsi_tape *STp)
3037 DEB( char *name = tape_name(STp); )
3039 if (STp->ready != ST_READY)
3042 result = read_mode_page(STp, PART_PAGE, 1);
3045 DEBC(printk(ST_DEB_MSG "%s: Can't read medium partition page.\n",
3049 result = (STp->buffer)->b_data[MODE_HEADER_LENGTH +
3050 PP_OFF_NBR_ADD_PARTS] + 1;
3051 DEBC(printk(ST_DEB_MSG "%s: Number of partitions %d.\n", name, result));
3058 /* Partition the tape into two partitions if size > 0 or one partition if
3061 The block descriptors are read and written because Sony SDT-7000 does not
3062 work without this (suggestion from Michael Schaefer <Michael.Schaefer@dlr.de>).
3064 My HP C1533A drive returns only one partition size field. This is used to
3065 set the size of partition 1. There is no size field for the default partition.
3066 Michael Schaefer's Sony SDT-7000 returns two descriptors and the second is
3067 used to set the size of partition 1 (this is what the SCSI-3 standard specifies).
3068 The following algorithm is used to accommodate both drives: if the number of
3069 partition size fields is greater than the maximum number of additional partitions
3070 in the mode page, the second field is used. Otherwise the first field is used.
3072 For Seagate DDS drives the page length must be 8 when no partitions is defined
3073 and 10 when 1 partition is defined (information from Eric Lee Green). This is
3074 is acceptable also to some other old drives and enforced if the first partition
3075 size field is used for the first additional partition size.
3077 static int partition_tape(struct scsi_tape *STp, int size)
3079 char *name = tape_name(STp);
3081 int pgo, psd_cnt, psdo;
3084 result = read_mode_page(STp, PART_PAGE, 0);
3086 DEBC(printk(ST_DEB_MSG "%s: Can't read partition mode page.\n", name));
3089 /* The mode page is in the buffer. Let's modify it and write it. */
3090 bp = (STp->buffer)->b_data;
3091 pgo = MODE_HEADER_LENGTH + bp[MH_OFF_BDESCS_LENGTH];
3092 DEBC(printk(ST_DEB_MSG "%s: Partition page length is %d bytes.\n",
3093 name, bp[pgo + MP_OFF_PAGE_LENGTH] + 2));
3095 psd_cnt = (bp[pgo + MP_OFF_PAGE_LENGTH] + 2 - PART_PAGE_FIXED_LENGTH) / 2;
3096 psdo = pgo + PART_PAGE_FIXED_LENGTH;
3097 if (psd_cnt > bp[pgo + PP_OFF_MAX_ADD_PARTS]) {
3098 bp[psdo] = bp[psdo + 1] = 0xff; /* Rest of the tape */
3101 memset(bp + psdo, 0, bp[pgo + PP_OFF_NBR_ADD_PARTS] * 2);
3103 DEBC(printk("%s: psd_cnt %d, max.parts %d, nbr_parts %d\n", name,
3104 psd_cnt, bp[pgo + PP_OFF_MAX_ADD_PARTS],
3105 bp[pgo + PP_OFF_NBR_ADD_PARTS]));
3108 bp[pgo + PP_OFF_NBR_ADD_PARTS] = 0;
3109 if (psd_cnt <= bp[pgo + PP_OFF_MAX_ADD_PARTS])
3110 bp[pgo + MP_OFF_PAGE_LENGTH] = 6;
3111 DEBC(printk(ST_DEB_MSG "%s: Formatting tape with one partition.\n",
3114 bp[psdo] = (size >> 8) & 0xff;
3115 bp[psdo + 1] = size & 0xff;
3117 if (bp[pgo + MP_OFF_PAGE_LENGTH] < 8)
3118 bp[pgo + MP_OFF_PAGE_LENGTH] = 8;
3119 DEBC(printk(ST_DEB_MSG
3120 "%s: Formatting tape with two partitions (1 = %d MB).\n",
3123 bp[pgo + PP_OFF_PART_UNITS] = 0;
3124 bp[pgo + PP_OFF_RESERVED] = 0;
3125 bp[pgo + PP_OFF_FLAGS] = PP_BIT_IDP | PP_MSK_PSUM_MB;
3127 result = write_mode_page(STp, PART_PAGE, 1);
3129 printk(KERN_INFO "%s: Partitioning of tape failed.\n", name);
3138 /* The ioctl command */
3139 static int st_ioctl(struct inode *inode, struct file *file,
3140 unsigned int cmd_in, unsigned long arg)
3142 int i, cmd_nr, cmd_type, bt;
3145 struct scsi_tape *STp = file->private_data;
3146 struct st_modedef *STm;
3147 struct st_partstat *STps;
3148 char *name = tape_name(STp);
3149 void __user *p = (void __user *)arg;
3151 if (down_interruptible(&STp->lock))
3152 return -ERESTARTSYS;
3155 if (debugging && !STp->in_use) {
3156 printk(ST_DEB_MSG "%s: Incorrect device.\n", name);
3161 STm = &(STp->modes[STp->current_mode]);
3162 STps = &(STp->ps[STp->partition]);
3165 * If we are in the middle of error recovery, don't let anyone
3166 * else try and use this device. Also, if error recovery fails, it
3167 * may try and take the device offline, in which case all further
3168 * access to the device is prohibited.
3170 retval = scsi_nonblockable_ioctl(STp->device, cmd_in, p, file);
3171 if (!scsi_block_when_processing_errors(STp->device) || retval != -ENODEV)
3175 cmd_type = _IOC_TYPE(cmd_in);
3176 cmd_nr = _IOC_NR(cmd_in);
3178 if (cmd_type == _IOC_TYPE(MTIOCTOP) && cmd_nr == _IOC_NR(MTIOCTOP)) {
3181 if (_IOC_SIZE(cmd_in) != sizeof(mtc)) {
3186 i = copy_from_user(&mtc, p, sizeof(struct mtop));
3192 if (mtc.mt_op == MTSETDRVBUFFER && !capable(CAP_SYS_ADMIN)) {
3194 "%s: MTSETDRVBUFFER only allowed for root.\n", name);
3198 if (!STm->defined &&
3199 (mtc.mt_op != MTSETDRVBUFFER &&
3200 (mtc.mt_count & MT_ST_OPTIONS) == 0)) {
3205 if (!STp->pos_unknown) {
3207 if (STps->eof == ST_FM_HIT) {
3208 if (mtc.mt_op == MTFSF || mtc.mt_op == MTFSFM ||
3209 mtc.mt_op == MTEOM) {
3211 if (STps->drv_file >= 0)
3212 STps->drv_file += 1;
3213 } else if (mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM) {
3215 if (STps->drv_file >= 0)
3216 STps->drv_file += 1;
3220 if (mtc.mt_op == MTSEEK) {
3221 /* Old position must be restored if partition will be
3223 i = !STp->can_partitions ||
3224 (STp->new_partition != STp->partition);
3226 i = mtc.mt_op == MTREW || mtc.mt_op == MTOFFL ||
3227 mtc.mt_op == MTRETEN || mtc.mt_op == MTEOM ||
3228 mtc.mt_op == MTLOCK || mtc.mt_op == MTLOAD ||
3229 mtc.mt_op == MTFSF || mtc.mt_op == MTFSFM ||
3230 mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM ||
3231 mtc.mt_op == MTCOMPRESSION;
3233 i = flush_buffer(STp, i);
3238 if (STps->rw == ST_WRITING &&
3239 (mtc.mt_op == MTREW || mtc.mt_op == MTOFFL ||
3240 mtc.mt_op == MTSEEK ||
3241 mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM)) {
3242 i = st_int_ioctl(STp, MTWEOF, 1);
3247 if (mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM)
3254 * If there was a bus reset, block further access
3255 * to this device. If the user wants to rewind the tape,
3256 * then reset the flag and allow access again.
3258 if (mtc.mt_op != MTREW &&
3259 mtc.mt_op != MTOFFL &&
3260 mtc.mt_op != MTRETEN &&
3261 mtc.mt_op != MTERASE &&
3262 mtc.mt_op != MTSEEK &&
3263 mtc.mt_op != MTEOM) {
3268 /* remove this when the midlevel properly clears was_reset */
3269 STp->device->was_reset = 0;
3272 if (mtc.mt_op != MTNOP && mtc.mt_op != MTSETBLK &&
3273 mtc.mt_op != MTSETDENSITY && mtc.mt_op != MTWSM &&
3274 mtc.mt_op != MTSETDRVBUFFER && mtc.mt_op != MTSETPART)
3275 STps->rw = ST_IDLE; /* Prevent automatic WEOF and fsf */
3277 if (mtc.mt_op == MTOFFL && STp->door_locked != ST_UNLOCKED)
3278 do_door_lock(STp, 0); /* Ignore result! */
3280 if (mtc.mt_op == MTSETDRVBUFFER &&
3281 (mtc.mt_count & MT_ST_OPTIONS) != 0) {
3282 retval = st_set_options(STp, mtc.mt_count);
3286 if (mtc.mt_op == MTSETPART) {
3287 if (!STp->can_partitions ||
3288 mtc.mt_count < 0 || mtc.mt_count >= ST_NBR_PARTITIONS) {
3292 if (mtc.mt_count >= STp->nbr_partitions &&
3293 (STp->nbr_partitions = nbr_partitions(STp)) < 0) {
3297 if (mtc.mt_count >= STp->nbr_partitions) {
3301 STp->new_partition = mtc.mt_count;
3306 if (mtc.mt_op == MTMKPART) {
3307 if (!STp->can_partitions) {
3311 if ((i = st_int_ioctl(STp, MTREW, 0)) < 0 ||
3312 (i = partition_tape(STp, mtc.mt_count)) < 0) {
3316 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
3317 STp->ps[i].rw = ST_IDLE;
3318 STp->ps[i].at_sm = 0;
3319 STp->ps[i].last_block_valid = 0;
3321 STp->partition = STp->new_partition = 0;
3322 STp->nbr_partitions = 1; /* Bad guess ?-) */
3323 STps->drv_block = STps->drv_file = 0;
3328 if (mtc.mt_op == MTSEEK) {
3329 i = set_location(STp, mtc.mt_count, STp->new_partition, 0);
3330 if (!STp->can_partitions)
3331 STp->ps[0].rw = ST_IDLE;
3336 if (mtc.mt_op == MTUNLOAD || mtc.mt_op == MTOFFL) {
3337 retval = do_load_unload(STp, file, 0);
3341 if (mtc.mt_op == MTLOAD) {
3342 retval = do_load_unload(STp, file, max(1, mtc.mt_count));
3346 if (mtc.mt_op == MTLOCK || mtc.mt_op == MTUNLOCK) {
3347 retval = do_door_lock(STp, (mtc.mt_op == MTLOCK));
3351 if (STp->can_partitions && STp->ready == ST_READY &&
3352 (i = switch_partition(STp)) < 0) {
3357 if (mtc.mt_op == MTCOMPRESSION)
3358 retval = st_compression(STp, (mtc.mt_count & 1));
3360 retval = st_int_ioctl(STp, mtc.mt_op, mtc.mt_count);
3363 if (!STm->defined) {
3368 if ((i = flush_buffer(STp, 0)) < 0) {
3372 if (STp->can_partitions &&
3373 (i = switch_partition(STp)) < 0) {
3378 if (cmd_type == _IOC_TYPE(MTIOCGET) && cmd_nr == _IOC_NR(MTIOCGET)) {
3379 struct mtget mt_status;
3381 if (_IOC_SIZE(cmd_in) != sizeof(struct mtget)) {
3386 mt_status.mt_type = STp->tape_type;
3387 mt_status.mt_dsreg =
3388 ((STp->block_size << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK) |
3389 ((STp->density << MT_ST_DENSITY_SHIFT) & MT_ST_DENSITY_MASK);
3390 mt_status.mt_blkno = STps->drv_block;
3391 mt_status.mt_fileno = STps->drv_file;
3392 if (STp->block_size != 0) {
3393 if (STps->rw == ST_WRITING)
3394 mt_status.mt_blkno +=
3395 (STp->buffer)->buffer_bytes / STp->block_size;
3396 else if (STps->rw == ST_READING)
3397 mt_status.mt_blkno -=
3398 ((STp->buffer)->buffer_bytes +
3399 STp->block_size - 1) / STp->block_size;
3402 mt_status.mt_gstat = 0;
3403 if (STp->drv_write_prot)
3404 mt_status.mt_gstat |= GMT_WR_PROT(0xffffffff);
3405 if (mt_status.mt_blkno == 0) {
3406 if (mt_status.mt_fileno == 0)
3407 mt_status.mt_gstat |= GMT_BOT(0xffffffff);
3409 mt_status.mt_gstat |= GMT_EOF(0xffffffff);
3411 mt_status.mt_erreg = (STp->recover_reg << MT_ST_SOFTERR_SHIFT);
3412 mt_status.mt_resid = STp->partition;
3413 if (STps->eof == ST_EOM_OK || STps->eof == ST_EOM_ERROR)
3414 mt_status.mt_gstat |= GMT_EOT(0xffffffff);
3415 else if (STps->eof >= ST_EOM_OK)
3416 mt_status.mt_gstat |= GMT_EOD(0xffffffff);
3417 if (STp->density == 1)
3418 mt_status.mt_gstat |= GMT_D_800(0xffffffff);
3419 else if (STp->density == 2)
3420 mt_status.mt_gstat |= GMT_D_1600(0xffffffff);
3421 else if (STp->density == 3)
3422 mt_status.mt_gstat |= GMT_D_6250(0xffffffff);
3423 if (STp->ready == ST_READY)
3424 mt_status.mt_gstat |= GMT_ONLINE(0xffffffff);
3425 if (STp->ready == ST_NO_TAPE)
3426 mt_status.mt_gstat |= GMT_DR_OPEN(0xffffffff);
3428 mt_status.mt_gstat |= GMT_SM(0xffffffff);
3429 if (STm->do_async_writes ||
3430 (STm->do_buffer_writes && STp->block_size != 0) ||
3431 STp->drv_buffer != 0)
3432 mt_status.mt_gstat |= GMT_IM_REP_EN(0xffffffff);
3433 if (STp->cleaning_req)
3434 mt_status.mt_gstat |= GMT_CLN(0xffffffff);
3436 i = copy_to_user(p, &mt_status, sizeof(struct mtget));
3442 STp->recover_reg = 0; /* Clear after read */
3445 } /* End of MTIOCGET */
3446 if (cmd_type == _IOC_TYPE(MTIOCPOS) && cmd_nr == _IOC_NR(MTIOCPOS)) {
3447 struct mtpos mt_pos;
3448 if (_IOC_SIZE(cmd_in) != sizeof(struct mtpos)) {
3452 if ((i = get_location(STp, &blk, &bt, 0)) < 0) {
3456 mt_pos.mt_blkno = blk;
3457 i = copy_to_user(p, &mt_pos, sizeof(struct mtpos));
3464 case SCSI_IOCTL_GET_IDLUN:
3465 case SCSI_IOCTL_GET_BUS_NUMBER:
3468 if ((cmd_in == SG_IO ||
3469 cmd_in == SCSI_IOCTL_SEND_COMMAND ||
3470 cmd_in == CDROM_SEND_PACKET) &&
3471 !capable(CAP_SYS_RAWIO))
3474 i = scsi_cmd_ioctl(file, STp->disk, cmd_in, p);
3479 retval = scsi_ioctl(STp->device, cmd_in, p);
3480 if (!retval && cmd_in == SCSI_IOCTL_STOP_UNIT) { /* unload */
3481 STp->rew_at_close = 0;
3482 STp->ready = ST_NO_TAPE;
3491 #ifdef CONFIG_COMPAT
3492 static long st_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3494 struct scsi_tape *STp = file->private_data;
3495 struct scsi_device *sdev = STp->device;
3496 int ret = -ENOIOCTLCMD;
3497 if (sdev->host->hostt->compat_ioctl) {
3499 ret = sdev->host->hostt->compat_ioctl(sdev, cmd, (void __user *)arg);
3508 /* Try to allocate a new tape buffer. Calling function must not hold
3510 static struct st_buffer *
3511 new_tape_buffer(int from_initialization, int need_dma, int max_sg)
3513 int i, priority, got = 0, segs = 0;
3514 struct st_buffer *tb;
3516 if (from_initialization)
3517 priority = GFP_ATOMIC;
3519 priority = GFP_KERNEL;
3521 i = sizeof(struct st_buffer) + (max_sg - 1) * sizeof(struct scatterlist) +
3522 max_sg * sizeof(struct st_buf_fragment);
3523 tb = kmalloc(i, priority);
3525 printk(KERN_NOTICE "st: Can't allocate new tape buffer.\n");
3529 tb->frp_segs = tb->orig_frp_segs = segs;
3530 tb->use_sg = max_sg;
3532 tb->b_data = page_address(tb->sg[0].page);
3533 tb->frp = (struct st_buf_fragment *)(&(tb->sg[0]) + max_sg);
3537 tb->buffer_size = got;
3543 /* Try to allocate enough space in the tape buffer */
3544 static int enlarge_buffer(struct st_buffer * STbuffer, int new_size, int need_dma)
3546 int segs, nbr, max_segs, b_size, priority, order, got;
3548 if (new_size <= STbuffer->buffer_size)
3551 if (STbuffer->buffer_size <= PAGE_SIZE)
3552 normalize_buffer(STbuffer); /* Avoid extra segment */
3554 max_segs = STbuffer->use_sg;
3555 nbr = max_segs - STbuffer->frp_segs;
3559 priority = GFP_KERNEL | __GFP_NOWARN;
3561 priority |= GFP_DMA;
3562 for (b_size = PAGE_SIZE, order=0;
3563 b_size < new_size - STbuffer->buffer_size;
3564 order++, b_size *= 2)
3567 for (segs = STbuffer->frp_segs, got = STbuffer->buffer_size;
3568 segs < max_segs && got < new_size;) {
3569 STbuffer->frp[segs].page = alloc_pages(priority, order);
3570 if (STbuffer->frp[segs].page == NULL) {
3571 if (new_size - got <= (max_segs - segs) * b_size / 2) {
3572 b_size /= 2; /* Large enough for the rest of the buffers */
3576 DEB(STbuffer->buffer_size = got);
3577 normalize_buffer(STbuffer);
3580 STbuffer->frp[segs].length = b_size;
3581 STbuffer->frp_segs += 1;
3583 STbuffer->buffer_size = got;
3586 STbuffer->b_data = page_address(STbuffer->frp[0].page);
3592 /* Release the extra buffer */
3593 static void normalize_buffer(struct st_buffer * STbuffer)
3597 for (i = STbuffer->orig_frp_segs; i < STbuffer->frp_segs; i++) {
3598 order = get_order(STbuffer->frp[i].length);
3599 __free_pages(STbuffer->frp[i].page, order);
3600 STbuffer->buffer_size -= STbuffer->frp[i].length;
3602 STbuffer->frp_segs = STbuffer->orig_frp_segs;
3603 STbuffer->frp_sg_current = 0;
3607 /* Move data from the user buffer to the tape buffer. Returns zero (success) or
3608 negative error code. */
3609 static int append_to_buffer(const char __user *ubp, struct st_buffer * st_bp, int do_count)
3611 int i, cnt, res, offset;
3613 for (i = 0, offset = st_bp->buffer_bytes;
3614 i < st_bp->frp_segs && offset >= st_bp->frp[i].length; i++)
3615 offset -= st_bp->frp[i].length;
3616 if (i == st_bp->frp_segs) { /* Should never happen */
3617 printk(KERN_WARNING "st: append_to_buffer offset overflow.\n");
3620 for (; i < st_bp->frp_segs && do_count > 0; i++) {
3621 cnt = st_bp->frp[i].length - offset < do_count ?
3622 st_bp->frp[i].length - offset : do_count;
3623 res = copy_from_user(page_address(st_bp->frp[i].page) + offset, ubp, cnt);
3627 st_bp->buffer_bytes += cnt;
3631 if (do_count) /* Should never happen */
3638 /* Move data from the tape buffer to the user buffer. Returns zero (success) or
3639 negative error code. */
3640 static int from_buffer(struct st_buffer * st_bp, char __user *ubp, int do_count)
3642 int i, cnt, res, offset;
3644 for (i = 0, offset = st_bp->read_pointer;
3645 i < st_bp->frp_segs && offset >= st_bp->frp[i].length; i++)
3646 offset -= st_bp->frp[i].length;
3647 if (i == st_bp->frp_segs) { /* Should never happen */
3648 printk(KERN_WARNING "st: from_buffer offset overflow.\n");
3651 for (; i < st_bp->frp_segs && do_count > 0; i++) {
3652 cnt = st_bp->frp[i].length - offset < do_count ?
3653 st_bp->frp[i].length - offset : do_count;
3654 res = copy_to_user(ubp, page_address(st_bp->frp[i].page) + offset, cnt);
3658 st_bp->buffer_bytes -= cnt;
3659 st_bp->read_pointer += cnt;
3663 if (do_count) /* Should never happen */
3670 /* Move data towards start of buffer */
3671 static void move_buffer_data(struct st_buffer * st_bp, int offset)
3673 int src_seg, dst_seg, src_offset = 0, dst_offset;
3679 total=st_bp->buffer_bytes - offset;
3680 for (src_seg=0; src_seg < st_bp->frp_segs; src_seg++) {
3681 src_offset = offset;
3682 if (src_offset < st_bp->frp[src_seg].length)
3684 offset -= st_bp->frp[src_seg].length;
3687 st_bp->buffer_bytes = st_bp->read_pointer = total;
3688 for (dst_seg=dst_offset=0; total > 0; ) {
3689 count = min(st_bp->frp[dst_seg].length - dst_offset,
3690 st_bp->frp[src_seg].length - src_offset);
3691 memmove(page_address(st_bp->frp[dst_seg].page) + dst_offset,
3692 page_address(st_bp->frp[src_seg].page) + src_offset, count);
3693 src_offset += count;
3694 if (src_offset >= st_bp->frp[src_seg].length) {
3698 dst_offset += count;
3699 if (dst_offset >= st_bp->frp[dst_seg].length) {
3708 /* Fill the s/g list up to the length required for this transfer */
3709 static void buf_to_sg(struct st_buffer *STbp, unsigned int length)
3713 struct scatterlist *sg;
3714 struct st_buf_fragment *frp;
3716 if (length == STbp->frp_sg_current)
3717 return; /* work already done */
3719 sg = &(STbp->sg[0]);
3721 for (i=count=0; count < length; i++) {
3722 sg[i].page = frp[i].page;
3723 if (length - count > frp[i].length)
3724 sg[i].length = frp[i].length;
3726 sg[i].length = length - count;
3727 count += sg[i].length;
3731 STbp->frp_sg_current = length;
3735 /* Validate the options from command line or module parameters */
3736 static void validate_options(void)
3739 st_fixed_buffer_size = buffer_kbs * ST_KILOBYTE;
3740 if (max_sg_segs >= ST_FIRST_SG)
3741 st_max_sg_segs = max_sg_segs;
3745 /* Set the boot options. Syntax is defined in Documenation/scsi/st.txt.
3747 static int __init st_setup(char *str)
3749 int i, len, ints[5];
3752 stp = get_options(str, ARRAY_SIZE(ints), ints);
3755 for (i = 0; i < ints[0] && i < ARRAY_SIZE(parms); i++)
3757 *parms[i].val = ints[i + 1];
3759 while (stp != NULL) {
3760 for (i = 0; i < ARRAY_SIZE(parms); i++) {
3761 len = strlen(parms[i].name);
3762 if (!strncmp(stp, parms[i].name, len) &&
3763 (*(stp + len) == ':' || *(stp + len) == '=')) {
3766 simple_strtoul(stp + len + 1, NULL, 0);
3768 printk(KERN_WARNING "st: Obsolete parameter %s\n",
3773 if (i >= sizeof(parms) / sizeof(struct st_dev_parm))
3774 printk(KERN_WARNING "st: invalid parameter in '%s'\n",
3776 stp = strchr(stp, ',');
3787 __setup("st=", st_setup);
3791 static struct file_operations st_fops =
3793 .owner = THIS_MODULE,
3797 #ifdef CONFIG_COMPAT
3798 .compat_ioctl = st_compat_ioctl,
3802 .release = st_release,
3805 static int st_probe(struct device *dev)
3807 struct scsi_device *SDp = to_scsi_device(dev);
3808 struct gendisk *disk = NULL;
3809 struct cdev *cdev = NULL;
3810 struct scsi_tape *tpnt = NULL;
3811 struct st_modedef *STm;
3812 struct st_partstat *STps;
3813 struct st_buffer *buffer;
3814 int i, j, mode, dev_num, error;
3818 if (SDp->type != TYPE_TAPE)
3820 if ((stp = st_incompatible(SDp))) {
3822 "st: Found incompatible tape at scsi%d, channel %d, id %d, lun %d\n",
3823 SDp->host->host_no, SDp->channel, SDp->id, SDp->lun);
3824 printk(KERN_INFO "st: The suggested driver is %s.\n", stp);
3828 i = SDp->host->sg_tablesize;
3829 if (st_max_sg_segs < i)
3831 buffer = new_tape_buffer(1, (SDp->host)->unchecked_isa_dma, i);
3832 if (buffer == NULL) {
3834 "st: Can't allocate new tape buffer. Device not attached.\n");
3838 disk = alloc_disk(1);
3840 printk(KERN_ERR "st: out of memory. Device not attached.\n");
3841 goto out_buffer_free;
3844 write_lock(&st_dev_arr_lock);
3845 if (st_nr_dev >= st_dev_max) {
3846 struct scsi_tape **tmp_da;
3849 tmp_dev_max = max(st_nr_dev * 2, 8);
3850 if (tmp_dev_max > ST_MAX_TAPES)
3851 tmp_dev_max = ST_MAX_TAPES;
3852 if (tmp_dev_max <= st_nr_dev) {
3853 write_unlock(&st_dev_arr_lock);
3854 printk(KERN_ERR "st: Too many tape devices (max. %d).\n",
3859 tmp_da = kmalloc(tmp_dev_max * sizeof(struct scsi_tape *), GFP_ATOMIC);
3860 if (tmp_da == NULL) {
3861 write_unlock(&st_dev_arr_lock);
3862 printk(KERN_ERR "st: Can't extend device array.\n");
3866 memset(tmp_da, 0, tmp_dev_max * sizeof(struct scsi_tape *));
3867 if (scsi_tapes != NULL) {
3868 memcpy(tmp_da, scsi_tapes,
3869 st_dev_max * sizeof(struct scsi_tape *));
3872 scsi_tapes = tmp_da;
3874 st_dev_max = tmp_dev_max;
3877 for (i = 0; i < st_dev_max; i++)
3878 if (scsi_tapes[i] == NULL)
3880 if (i >= st_dev_max)
3881 panic("scsi_devices corrupt (st)");
3883 tpnt = kmalloc(sizeof(struct scsi_tape), GFP_ATOMIC);
3885 write_unlock(&st_dev_arr_lock);
3886 printk(KERN_ERR "st: Can't allocate device descriptor.\n");
3889 memset(tpnt, 0, sizeof(struct scsi_tape));
3891 sprintf(disk->disk_name, "st%d", i);
3892 disk->private_data = &tpnt->driver;
3893 disk->queue = SDp->request_queue;
3894 tpnt->driver = &st_template;
3895 scsi_tapes[i] = tpnt;
3899 if (SDp->scsi_level <= 2)
3900 tpnt->tape_type = MT_ISSCSI1;
3902 tpnt->tape_type = MT_ISSCSI2;
3904 tpnt->buffer = buffer;
3909 tpnt->drv_buffer = 1; /* Try buffering if no mode sense */
3910 tpnt->restr_dma = (SDp->host)->unchecked_isa_dma;
3911 tpnt->use_pf = (SDp->scsi_level >= SCSI_2);
3913 tpnt->do_auto_lock = ST_AUTO_LOCK;
3914 tpnt->can_bsr = (SDp->scsi_level > 2 ? 1 : ST_IN_FILE_POS); /* BSR mandatory in SCSI3 */
3915 tpnt->can_partitions = 0;
3916 tpnt->two_fm = ST_TWO_FM;
3917 tpnt->fast_mteom = ST_FAST_MTEOM;
3918 tpnt->scsi2_logical = ST_SCSI2LOGICAL;
3919 tpnt->immediate = ST_NOWAIT;
3920 tpnt->default_drvbuffer = 0xff; /* No forced buffering */
3921 tpnt->partition = 0;
3922 tpnt->new_partition = 0;
3923 tpnt->nbr_partitions = 0;
3924 tpnt->device->timeout = ST_TIMEOUT;
3925 tpnt->long_timeout = ST_LONG_TIMEOUT;
3926 tpnt->try_dio = try_direct_io && !SDp->host->unchecked_isa_dma;
3928 bounce_limit = scsi_calculate_bounce_limit(SDp->host) >> PAGE_SHIFT;
3929 if (bounce_limit > ULONG_MAX)
3930 bounce_limit = ULONG_MAX;
3931 tpnt->max_pfn = bounce_limit;
3933 for (i = 0; i < ST_NBR_MODES; i++) {
3934 STm = &(tpnt->modes[i]);
3936 STm->sysv = ST_SYSV;
3937 STm->defaults_for_writes = 0;
3938 STm->do_async_writes = ST_ASYNC_WRITES;
3939 STm->do_buffer_writes = ST_BUFFER_WRITES;
3940 STm->do_read_ahead = ST_READ_AHEAD;
3941 STm->default_compression = ST_DONT_TOUCH;
3942 STm->default_blksize = (-1); /* No forced size */
3943 STm->default_density = (-1); /* No forced density */
3946 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
3947 STps = &(tpnt->ps[i]);
3949 STps->eof = ST_NOEOF;
3951 STps->last_block_valid = 0;
3952 STps->drv_block = (-1);
3953 STps->drv_file = (-1);
3956 tpnt->current_mode = 0;
3957 tpnt->modes[0].defined = 1;
3959 tpnt->density_changed = tpnt->compression_changed =
3960 tpnt->blksize_changed = 0;
3961 init_MUTEX(&tpnt->lock);
3964 write_unlock(&st_dev_arr_lock);
3966 for (mode = 0; mode < ST_NBR_MODES; ++mode) {
3967 STm = &(tpnt->modes[mode]);
3968 for (j=0; j < 2; j++) {
3969 cdev = cdev_alloc();
3972 "st%d: out of memory. Device not attached.\n",
3976 cdev->owner = THIS_MODULE;
3977 cdev->ops = &st_fops;
3979 error = cdev_add(cdev,
3980 MKDEV(SCSI_TAPE_MAJOR, TAPE_MINOR(dev_num, mode, j)),
3983 printk(KERN_ERR "st%d: Can't add %s-rewind mode %d\n",
3984 dev_num, j ? "non" : "auto", mode);
3985 printk(KERN_ERR "st%d: Device not attached.\n", dev_num);
3988 STm->cdevs[j] = cdev;
3991 do_create_class_files(tpnt, dev_num, mode);
3994 for (mode = 0; mode < ST_NBR_MODES; ++mode) {
3995 /* Make sure that the minor numbers corresponding to the four
3996 first modes always get the same names */
3997 i = mode << (4 - ST_NBR_MODE_BITS);
3999 devfs_mk_cdev(MKDEV(SCSI_TAPE_MAJOR, TAPE_MINOR(dev_num, mode, 0)),
4000 S_IFCHR | S_IRUGO | S_IWUGO,
4001 "%s/mt%s", SDp->devfs_name, st_formats[i]);
4002 /* No-rewind entry */
4003 devfs_mk_cdev(MKDEV(SCSI_TAPE_MAJOR, TAPE_MINOR(dev_num, mode, 1)),
4004 S_IFCHR | S_IRUGO | S_IWUGO,
4005 "%s/mt%sn", SDp->devfs_name, st_formats[i]);
4007 disk->number = devfs_register_tape(SDp->devfs_name);
4010 "Attached scsi tape %s at scsi%d, channel %d, id %d, lun %d\n",
4011 tape_name(tpnt), SDp->host->host_no, SDp->channel, SDp->id, SDp->lun);
4012 printk(KERN_WARNING "%s: try direct i/o: %s (alignment %d B), max page reachable by HBA %lu\n",
4013 tape_name(tpnt), tpnt->try_dio ? "yes" : "no",
4014 queue_dma_alignment(SDp->request_queue) + 1, tpnt->max_pfn);
4019 for (mode=0; mode < ST_NBR_MODES; mode++) {
4020 STm = &(tpnt->modes[mode]);
4021 sysfs_remove_link(&tpnt->device->sdev_gendev.kobj,
4023 for (j=0; j < 2; j++) {
4024 if (STm->cdevs[j]) {
4025 if (cdev == STm->cdevs[j])
4027 class_device_destroy(st_sysfs_class,
4028 MKDEV(SCSI_TAPE_MAJOR,
4029 TAPE_MINOR(i, mode, j)));
4030 cdev_del(STm->cdevs[j]);
4036 write_lock(&st_dev_arr_lock);
4037 scsi_tapes[dev_num] = NULL;
4039 write_unlock(&st_dev_arr_lock);
4051 static int st_remove(struct device *dev)
4053 struct scsi_device *SDp = to_scsi_device(dev);
4054 struct scsi_tape *tpnt;
4057 write_lock(&st_dev_arr_lock);
4058 for (i = 0; i < st_dev_max; i++) {
4059 tpnt = scsi_tapes[i];
4060 if (tpnt != NULL && tpnt->device == SDp) {
4061 scsi_tapes[i] = NULL;
4063 write_unlock(&st_dev_arr_lock);
4064 devfs_unregister_tape(tpnt->disk->number);
4065 sysfs_remove_link(&tpnt->device->sdev_gendev.kobj,
4067 for (mode = 0; mode < ST_NBR_MODES; ++mode) {
4068 j = mode << (4 - ST_NBR_MODE_BITS);
4069 devfs_remove("%s/mt%s", SDp->devfs_name, st_formats[j]);
4070 devfs_remove("%s/mt%sn", SDp->devfs_name, st_formats[j]);
4071 for (j=0; j < 2; j++) {
4072 class_device_destroy(st_sysfs_class,
4073 MKDEV(SCSI_TAPE_MAJOR,
4074 TAPE_MINOR(i, mode, j)));
4075 cdev_del(tpnt->modes[mode].cdevs[j]);
4076 tpnt->modes[mode].cdevs[j] = NULL;
4079 tpnt->device = NULL;
4082 tpnt->buffer->orig_frp_segs = 0;
4083 normalize_buffer(tpnt->buffer);
4084 kfree(tpnt->buffer);
4086 put_disk(tpnt->disk);
4092 write_unlock(&st_dev_arr_lock);
4096 static void st_intr(struct scsi_cmnd *SCpnt)
4098 scsi_io_completion(SCpnt, (SCpnt->result ? 0: SCpnt->bufflen), 1);
4102 * st_init_command: only called via the scsi_cmd_ioctl (block SG_IO)
4103 * interface for REQ_BLOCK_PC commands.
4105 static int st_init_command(struct scsi_cmnd *SCpnt)
4109 if (!(SCpnt->request->flags & REQ_BLOCK_PC))
4112 rq = SCpnt->request;
4113 if (sizeof(rq->cmd) > sizeof(SCpnt->cmnd))
4116 memcpy(SCpnt->cmnd, rq->cmd, sizeof(SCpnt->cmnd));
4118 if (rq_data_dir(rq) == WRITE)
4119 SCpnt->sc_data_direction = DMA_TO_DEVICE;
4120 else if (rq->data_len)
4121 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
4123 SCpnt->sc_data_direction = DMA_NONE;
4125 SCpnt->timeout_per_command = rq->timeout;
4126 SCpnt->transfersize = rq->data_len;
4127 SCpnt->done = st_intr;
4131 static int __init init_st(void)
4136 "st: Version %s, fixed bufsize %d, s/g segs %d\n",
4137 verstr, st_fixed_buffer_size, st_max_sg_segs);
4139 st_sysfs_class = class_create(THIS_MODULE, "scsi_tape");
4140 if (IS_ERR(st_sysfs_class)) {
4141 st_sysfs_class = NULL;
4142 printk(KERN_ERR "Unable create sysfs class for SCSI tapes\n");
4146 if (!register_chrdev_region(MKDEV(SCSI_TAPE_MAJOR, 0),
4147 ST_MAX_TAPE_ENTRIES, "st")) {
4148 if (scsi_register_driver(&st_template.gendrv) == 0) {
4149 do_create_driverfs_files();
4152 unregister_chrdev_region(MKDEV(SCSI_TAPE_MAJOR, 0),
4153 ST_MAX_TAPE_ENTRIES);
4155 class_destroy(st_sysfs_class);
4157 printk(KERN_ERR "Unable to get major %d for SCSI tapes\n", SCSI_TAPE_MAJOR);
4161 static void __exit exit_st(void)
4163 do_remove_driverfs_files();
4164 scsi_unregister_driver(&st_template.gendrv);
4165 unregister_chrdev_region(MKDEV(SCSI_TAPE_MAJOR, 0),
4166 ST_MAX_TAPE_ENTRIES);
4167 class_destroy(st_sysfs_class);
4169 printk(KERN_INFO "st: Unloaded.\n");
4172 module_init(init_st);
4173 module_exit(exit_st);
4176 /* The sysfs driver interface. Read-only at the moment */
4177 static ssize_t st_try_direct_io_show(struct device_driver *ddp, char *buf)
4179 return snprintf(buf, PAGE_SIZE, "%d\n", try_direct_io);
4181 static DRIVER_ATTR(try_direct_io, S_IRUGO, st_try_direct_io_show, NULL);
4183 static ssize_t st_fixed_buffer_size_show(struct device_driver *ddp, char *buf)
4185 return snprintf(buf, PAGE_SIZE, "%d\n", st_fixed_buffer_size);
4187 static DRIVER_ATTR(fixed_buffer_size, S_IRUGO, st_fixed_buffer_size_show, NULL);
4189 static ssize_t st_max_sg_segs_show(struct device_driver *ddp, char *buf)
4191 return snprintf(buf, PAGE_SIZE, "%d\n", st_max_sg_segs);
4193 static DRIVER_ATTR(max_sg_segs, S_IRUGO, st_max_sg_segs_show, NULL);
4195 static ssize_t st_version_show(struct device_driver *ddd, char *buf)
4197 return snprintf(buf, PAGE_SIZE, "[%s]\n", verstr);
4199 static DRIVER_ATTR(version, S_IRUGO, st_version_show, NULL);
4201 static void do_create_driverfs_files(void)
4203 struct device_driver *driverfs = &st_template.gendrv;
4205 driver_create_file(driverfs, &driver_attr_try_direct_io);
4206 driver_create_file(driverfs, &driver_attr_fixed_buffer_size);
4207 driver_create_file(driverfs, &driver_attr_max_sg_segs);
4208 driver_create_file(driverfs, &driver_attr_version);
4211 static void do_remove_driverfs_files(void)
4213 struct device_driver *driverfs = &st_template.gendrv;
4215 driver_remove_file(driverfs, &driver_attr_version);
4216 driver_remove_file(driverfs, &driver_attr_max_sg_segs);
4217 driver_remove_file(driverfs, &driver_attr_fixed_buffer_size);
4218 driver_remove_file(driverfs, &driver_attr_try_direct_io);
4222 /* The sysfs simple class interface */
4223 static ssize_t st_defined_show(struct class_device *class_dev, char *buf)
4225 struct st_modedef *STm = (struct st_modedef *)class_get_devdata(class_dev);
4228 l = snprintf(buf, PAGE_SIZE, "%d\n", STm->defined);
4232 CLASS_DEVICE_ATTR(defined, S_IRUGO, st_defined_show, NULL);
4234 static ssize_t st_defblk_show(struct class_device *class_dev, char *buf)
4236 struct st_modedef *STm = (struct st_modedef *)class_get_devdata(class_dev);
4239 l = snprintf(buf, PAGE_SIZE, "%d\n", STm->default_blksize);
4243 CLASS_DEVICE_ATTR(default_blksize, S_IRUGO, st_defblk_show, NULL);
4245 static ssize_t st_defdensity_show(struct class_device *class_dev, char *buf)
4247 struct st_modedef *STm = (struct st_modedef *)class_get_devdata(class_dev);
4251 fmt = STm->default_density >= 0 ? "0x%02x\n" : "%d\n";
4252 l = snprintf(buf, PAGE_SIZE, fmt, STm->default_density);
4256 CLASS_DEVICE_ATTR(default_density, S_IRUGO, st_defdensity_show, NULL);
4258 static ssize_t st_defcompression_show(struct class_device *class_dev, char *buf)
4260 struct st_modedef *STm = (struct st_modedef *)class_get_devdata(class_dev);
4263 l = snprintf(buf, PAGE_SIZE, "%d\n", STm->default_compression - 1);
4267 CLASS_DEVICE_ATTR(default_compression, S_IRUGO, st_defcompression_show, NULL);
4269 static void do_create_class_files(struct scsi_tape *STp, int dev_num, int mode)
4273 struct class_device *st_class_member;
4275 if (!st_sysfs_class)
4278 for (rew=0; rew < 2; rew++) {
4279 /* Make sure that the minor numbers corresponding to the four
4280 first modes always get the same names */
4281 i = mode << (4 - ST_NBR_MODE_BITS);
4282 snprintf(name, 10, "%s%s%s", rew ? "n" : "",
4283 STp->disk->disk_name, st_formats[i]);
4285 class_device_create(st_sysfs_class,
4286 MKDEV(SCSI_TAPE_MAJOR,
4287 TAPE_MINOR(dev_num, mode, rew)),
4288 &STp->device->sdev_gendev, "%s", name);
4289 if (IS_ERR(st_class_member)) {
4290 printk(KERN_WARNING "st%d: class_device_create failed\n",
4294 class_set_devdata(st_class_member, &STp->modes[mode]);
4296 class_device_create_file(st_class_member,
4297 &class_device_attr_defined);
4298 class_device_create_file(st_class_member,
4299 &class_device_attr_default_blksize);
4300 class_device_create_file(st_class_member,
4301 &class_device_attr_default_density);
4302 class_device_create_file(st_class_member,
4303 &class_device_attr_default_compression);
4304 if (mode == 0 && rew == 0) {
4305 error = sysfs_create_link(&STp->device->sdev_gendev.kobj,
4306 &st_class_member->kobj,
4310 "st%d: Can't create sysfs link from SCSI device.\n",
4320 /* Pin down user pages and put them into a scatter gather list. Returns <= 0 if
4321 - mapping of all pages not successful
4322 - any page is above max_pfn
4323 (i.e., either completely successful or fails)
4325 static int st_map_user_pages(struct scatterlist *sgl, const unsigned int max_pages,
4326 unsigned long uaddr, size_t count, int rw,
4327 unsigned long max_pfn)
4331 nr_pages = sgl_map_user_pages(sgl, max_pages, uaddr, count, rw);
4335 for (i=0; i < nr_pages; i++) {
4336 if (page_to_pfn(sgl[i].page) > max_pfn)
4342 sgl_unmap_user_pages(sgl, nr_pages, 0);
4347 /* The following functions may be useful for a larger audience. */
4348 static int sgl_map_user_pages(struct scatterlist *sgl, const unsigned int max_pages,
4349 unsigned long uaddr, size_t count, int rw)
4352 unsigned int nr_pages;
4353 struct page **pages;
4355 nr_pages = ((uaddr & ~PAGE_MASK) + count + ~PAGE_MASK) >> PAGE_SHIFT;
4357 /* User attempted Overflow! */
4358 if ((uaddr + count) < uaddr)
4362 if (nr_pages > max_pages)
4369 if ((pages = kmalloc(max_pages * sizeof(*pages), GFP_KERNEL)) == NULL)
4372 /* Try to fault in all of the necessary pages */
4373 down_read(¤t->mm->mmap_sem);
4374 /* rw==READ means read from drive, write into memory area */
4375 res = get_user_pages(
4381 0, /* don't force */
4384 up_read(¤t->mm->mmap_sem);
4386 /* Errors and no page mapped should return here */
4390 for (i=0; i < nr_pages; i++) {
4391 /* FIXME: flush superflous for rw==READ,
4392 * probably wrong function for rw==WRITE
4394 flush_dcache_page(pages[i]);
4397 /* Populate the scatter/gather list */
4398 sgl[0].page = pages[0];
4399 sgl[0].offset = uaddr & ~PAGE_MASK;
4401 sgl[0].length = PAGE_SIZE - sgl[0].offset;
4402 count -= sgl[0].length;
4403 for (i=1; i < nr_pages ; i++) {
4405 sgl[i].page = pages[i];
4406 sgl[i].length = count < PAGE_SIZE ? count : PAGE_SIZE;
4411 sgl[0].length = count;
4419 for (j=0; j < res; j++)
4420 page_cache_release(pages[j]);
4427 /* And unmap them... */
4428 static int sgl_unmap_user_pages(struct scatterlist *sgl, const unsigned int nr_pages,
4433 for (i=0; i < nr_pages; i++) {
4434 if (dirtied && !PageReserved(sgl[i].page))
4435 SetPageDirty(sgl[i].page);
4436 /* FIXME: cache flush missing for rw==READ
4437 * FIXME: call the correct reference counting function
4439 page_cache_release(sgl[i].page);