Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / scsi / st.c
blob3ffe942c8ca19a82dad0dab8c54f6895652f793f
1 /*
2 SCSI Tape Driver for Linux version 1.1 and newer. See the accompanying
3 file Documentation/scsi/st.txt for more information.
5 History:
6 Rewritten from Dwayne Forsyth's SCSI tape driver by Kai Makisara.
7 Contribution and ideas from several people including (in alphabetical
8 order) Klaus Ehrenfried, Eugene Exarevsky, Eric Lee Green, Wolfgang Denk,
9 Steve Hirsch, Andreas Koppenh"ofer, Michael Leodolter, Eyal Lebedinsky,
10 Michael Schaefer, J"org Weule, and Eric Youngdale.
12 Copyright 1992 - 2008 Kai Makisara
13 email Kai.Makisara@kolumbus.fi
15 Some small formal changes - aeb, 950809
17 Last modified: 18-JAN-1998 Richard Gooch <rgooch@atnf.csiro.au> Devfs support
20 <<<<<<< HEAD:drivers/scsi/st.c
21 static const char *verstr = "20080117";
22 =======
23 static const char *verstr = "20080221";
24 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/st.c
26 #include <linux/module.h>
28 #include <linux/fs.h>
29 #include <linux/kernel.h>
30 #include <linux/sched.h>
31 #include <linux/mm.h>
32 #include <linux/init.h>
33 #include <linux/string.h>
34 #include <linux/errno.h>
35 #include <linux/mtio.h>
36 #include <linux/cdrom.h>
37 #include <linux/ioctl.h>
38 #include <linux/fcntl.h>
39 #include <linux/spinlock.h>
40 #include <linux/blkdev.h>
41 #include <linux/moduleparam.h>
42 #include <linux/cdev.h>
43 #include <linux/delay.h>
44 #include <linux/mutex.h>
46 #include <asm/uaccess.h>
47 #include <asm/dma.h>
48 #include <asm/system.h>
50 #include <scsi/scsi.h>
51 #include <scsi/scsi_dbg.h>
52 #include <scsi/scsi_device.h>
53 #include <scsi/scsi_driver.h>
54 #include <scsi/scsi_eh.h>
55 #include <scsi/scsi_host.h>
56 #include <scsi/scsi_ioctl.h>
57 #include <scsi/sg.h>
60 /* The driver prints some debugging information on the console if DEBUG
61 is defined and non-zero. */
62 #define DEBUG 0
64 #if DEBUG
65 /* The message level for the debug messages is currently set to KERN_NOTICE
66 so that people can easily see the messages. Later when the debugging messages
67 in the drivers are more widely classified, this may be changed to KERN_DEBUG. */
68 #define ST_DEB_MSG KERN_NOTICE
69 #define DEB(a) a
70 #define DEBC(a) if (debugging) { a ; }
71 #else
72 #define DEB(a)
73 #define DEBC(a)
74 #endif
76 #define ST_KILOBYTE 1024
78 #include "st_options.h"
79 #include "st.h"
81 static int buffer_kbs;
82 static int max_sg_segs;
83 static int try_direct_io = TRY_DIRECT_IO;
84 static int try_rdio = 1;
85 static int try_wdio = 1;
87 static int st_dev_max;
88 static int st_nr_dev;
90 static struct class *st_sysfs_class;
92 MODULE_AUTHOR("Kai Makisara");
93 MODULE_DESCRIPTION("SCSI tape (st) driver");
94 MODULE_LICENSE("GPL");
95 MODULE_ALIAS_CHARDEV_MAJOR(SCSI_TAPE_MAJOR);
96 MODULE_ALIAS_SCSI_DEVICE(TYPE_TAPE);
98 /* Set 'perm' (4th argument) to 0 to disable module_param's definition
99 * of sysfs parameters (which module_param doesn't yet support).
100 * Sysfs parameters defined explicitly later.
102 module_param_named(buffer_kbs, buffer_kbs, int, 0);
103 MODULE_PARM_DESC(buffer_kbs, "Default driver buffer size for fixed block mode (KB; 32)");
104 module_param_named(max_sg_segs, max_sg_segs, int, 0);
105 MODULE_PARM_DESC(max_sg_segs, "Maximum number of scatter/gather segments to use (256)");
106 module_param_named(try_direct_io, try_direct_io, int, 0);
107 MODULE_PARM_DESC(try_direct_io, "Try direct I/O between user buffer and tape drive (1)");
109 /* Extra parameters for testing */
110 module_param_named(try_rdio, try_rdio, int, 0);
111 MODULE_PARM_DESC(try_rdio, "Try direct read i/o when possible");
112 module_param_named(try_wdio, try_wdio, int, 0);
113 MODULE_PARM_DESC(try_wdio, "Try direct write i/o when possible");
115 #ifndef MODULE
116 static int write_threshold_kbs; /* retained for compatibility */
117 static struct st_dev_parm {
118 char *name;
119 int *val;
120 } parms[] __initdata = {
122 "buffer_kbs", &buffer_kbs
124 { /* Retained for compatibility with 2.4 */
125 "write_threshold_kbs", &write_threshold_kbs
128 "max_sg_segs", NULL
131 "try_direct_io", &try_direct_io
134 #endif
136 /* Restrict the number of modes so that names for all are assigned */
137 #if ST_NBR_MODES > 16
138 #error "Maximum number of modes is 16"
139 #endif
140 /* Bit reversed order to get same names for same minors with all
141 mode counts */
142 static const char *st_formats[] = {
143 "", "r", "k", "s", "l", "t", "o", "u",
144 "m", "v", "p", "x", "a", "y", "q", "z"};
146 /* The default definitions have been moved to st_options.h */
148 #define ST_FIXED_BUFFER_SIZE (ST_FIXED_BUFFER_BLOCKS * ST_KILOBYTE)
150 /* The buffer size should fit into the 24 bits for length in the
151 6-byte SCSI read and write commands. */
152 #if ST_FIXED_BUFFER_SIZE >= (2 << 24 - 1)
153 #error "Buffer size should not exceed (2 << 24 - 1) bytes!"
154 #endif
156 static int debugging = DEBUG;
158 #define MAX_RETRIES 0
159 #define MAX_WRITE_RETRIES 0
160 #define MAX_READY_RETRIES 0
161 #define NO_TAPE NOT_READY
163 #define ST_TIMEOUT (900 * HZ)
164 #define ST_LONG_TIMEOUT (14000 * HZ)
166 /* Remove mode bits and auto-rewind bit (7) */
167 #define TAPE_NR(x) ( ((iminor(x) & ~255) >> (ST_NBR_MODE_BITS + 1)) | \
168 (iminor(x) & ~(-1 << ST_MODE_SHIFT)) )
169 #define TAPE_MODE(x) ((iminor(x) & ST_MODE_MASK) >> ST_MODE_SHIFT)
171 /* Construct the minor number from the device (d), mode (m), and non-rewind (n) data */
172 #define TAPE_MINOR(d, m, n) (((d & ~(255 >> (ST_NBR_MODE_BITS + 1))) << (ST_NBR_MODE_BITS + 1)) | \
173 (d & (255 >> (ST_NBR_MODE_BITS + 1))) | (m << ST_MODE_SHIFT) | ((n != 0) << 7) )
175 /* Internal ioctl to set both density (uppermost 8 bits) and blocksize (lower
176 24 bits) */
177 #define SET_DENS_AND_BLK 0x10001
179 static DEFINE_RWLOCK(st_dev_arr_lock);
181 static int st_fixed_buffer_size = ST_FIXED_BUFFER_SIZE;
182 static int st_max_sg_segs = ST_MAX_SG;
184 static struct scsi_tape **scsi_tapes = NULL;
186 static int modes_defined;
188 static struct st_buffer *new_tape_buffer(int, int, int);
189 static int enlarge_buffer(struct st_buffer *, int, int);
190 static void normalize_buffer(struct st_buffer *);
191 static int append_to_buffer(const char __user *, struct st_buffer *, int);
192 static int from_buffer(struct st_buffer *, char __user *, int);
193 static void move_buffer_data(struct st_buffer *, int);
194 static void buf_to_sg(struct st_buffer *, unsigned int);
196 static int sgl_map_user_pages(struct scatterlist *, const unsigned int,
197 unsigned long, size_t, int);
198 static int sgl_unmap_user_pages(struct scatterlist *, const unsigned int, int);
200 static int st_probe(struct device *);
201 static int st_remove(struct device *);
203 static int do_create_sysfs_files(void);
204 static void do_remove_sysfs_files(void);
205 static int do_create_class_files(struct scsi_tape *, int, int);
207 static struct scsi_driver st_template = {
208 .owner = THIS_MODULE,
209 .gendrv = {
210 .name = "st",
211 .probe = st_probe,
212 .remove = st_remove,
216 static int st_compression(struct scsi_tape *, int);
218 static int find_partition(struct scsi_tape *);
219 static int switch_partition(struct scsi_tape *);
221 static int st_int_ioctl(struct scsi_tape *, unsigned int, unsigned long);
223 static void scsi_tape_release(struct kref *);
225 #define to_scsi_tape(obj) container_of(obj, struct scsi_tape, kref)
227 static DEFINE_MUTEX(st_ref_mutex);
230 #include "osst_detect.h"
231 #ifndef SIGS_FROM_OSST
232 #define SIGS_FROM_OSST \
233 {"OnStream", "SC-", "", "osst"}, \
234 {"OnStream", "DI-", "", "osst"}, \
235 {"OnStream", "DP-", "", "osst"}, \
236 {"OnStream", "USB", "", "osst"}, \
237 {"OnStream", "FW-", "", "osst"}
238 #endif
240 static struct scsi_tape *scsi_tape_get(int dev)
242 struct scsi_tape *STp = NULL;
244 mutex_lock(&st_ref_mutex);
245 write_lock(&st_dev_arr_lock);
247 if (dev < st_dev_max && scsi_tapes != NULL)
248 STp = scsi_tapes[dev];
249 if (!STp) goto out;
251 kref_get(&STp->kref);
253 if (!STp->device)
254 goto out_put;
256 if (scsi_device_get(STp->device))
257 goto out_put;
259 goto out;
261 out_put:
262 kref_put(&STp->kref, scsi_tape_release);
263 STp = NULL;
264 out:
265 write_unlock(&st_dev_arr_lock);
266 mutex_unlock(&st_ref_mutex);
267 return STp;
270 static void scsi_tape_put(struct scsi_tape *STp)
272 struct scsi_device *sdev = STp->device;
274 mutex_lock(&st_ref_mutex);
275 kref_put(&STp->kref, scsi_tape_release);
276 scsi_device_put(sdev);
277 mutex_unlock(&st_ref_mutex);
280 struct st_reject_data {
281 char *vendor;
282 char *model;
283 char *rev;
284 char *driver_hint; /* Name of the correct driver, NULL if unknown */
287 static struct st_reject_data reject_list[] = {
288 /* {"XXX", "Yy-", "", NULL}, example */
289 SIGS_FROM_OSST,
290 {NULL, }};
292 /* If the device signature is on the list of incompatible drives, the
293 function returns a pointer to the name of the correct driver (if known) */
294 static char * st_incompatible(struct scsi_device* SDp)
296 struct st_reject_data *rp;
298 for (rp=&(reject_list[0]); rp->vendor != NULL; rp++)
299 if (!strncmp(rp->vendor, SDp->vendor, strlen(rp->vendor)) &&
300 !strncmp(rp->model, SDp->model, strlen(rp->model)) &&
301 !strncmp(rp->rev, SDp->rev, strlen(rp->rev))) {
302 if (rp->driver_hint)
303 return rp->driver_hint;
304 else
305 return "unknown";
307 return NULL;
311 static inline char *tape_name(struct scsi_tape *tape)
313 return tape->disk->disk_name;
317 static void st_analyze_sense(struct st_request *SRpnt, struct st_cmdstatus *s)
319 const u8 *ucp;
320 const u8 *sense = SRpnt->sense;
322 s->have_sense = scsi_normalize_sense(SRpnt->sense,
323 SCSI_SENSE_BUFFERSIZE, &s->sense_hdr);
324 s->flags = 0;
326 if (s->have_sense) {
327 s->deferred = 0;
328 s->remainder_valid =
329 scsi_get_sense_info_fld(sense, SCSI_SENSE_BUFFERSIZE, &s->uremainder64);
330 switch (sense[0] & 0x7f) {
331 case 0x71:
332 s->deferred = 1;
333 case 0x70:
334 s->fixed_format = 1;
335 s->flags = sense[2] & 0xe0;
336 break;
337 case 0x73:
338 s->deferred = 1;
339 case 0x72:
340 s->fixed_format = 0;
341 ucp = scsi_sense_desc_find(sense, SCSI_SENSE_BUFFERSIZE, 4);
342 s->flags = ucp ? (ucp[3] & 0xe0) : 0;
343 break;
349 /* Convert the result to success code */
350 static int st_chk_result(struct scsi_tape *STp, struct st_request * SRpnt)
352 int result = SRpnt->result;
353 u8 scode;
354 DEB(const char *stp;)
355 char *name = tape_name(STp);
356 struct st_cmdstatus *cmdstatp;
358 if (!result)
359 return 0;
361 cmdstatp = &STp->buffer->cmdstat;
362 st_analyze_sense(SRpnt, cmdstatp);
364 if (cmdstatp->have_sense)
365 scode = STp->buffer->cmdstat.sense_hdr.sense_key;
366 else
367 scode = 0;
369 DEB(
370 if (debugging) {
371 printk(ST_DEB_MSG "%s: Error: %x, cmd: %x %x %x %x %x %x\n",
372 name, result,
373 SRpnt->cmd[0], SRpnt->cmd[1], SRpnt->cmd[2],
374 SRpnt->cmd[3], SRpnt->cmd[4], SRpnt->cmd[5]);
375 if (cmdstatp->have_sense)
376 __scsi_print_sense(name, SRpnt->sense, SCSI_SENSE_BUFFERSIZE);
377 } ) /* end DEB */
378 if (!debugging) { /* Abnormal conditions for tape */
379 if (!cmdstatp->have_sense)
380 printk(KERN_WARNING
381 "%s: Error %x (sugg. bt 0x%x, driver bt 0x%x, host bt 0x%x).\n",
382 name, result, suggestion(result),
383 driver_byte(result) & DRIVER_MASK, host_byte(result));
384 else if (cmdstatp->have_sense &&
385 scode != NO_SENSE &&
386 scode != RECOVERED_ERROR &&
387 /* scode != UNIT_ATTENTION && */
388 scode != BLANK_CHECK &&
389 scode != VOLUME_OVERFLOW &&
390 SRpnt->cmd[0] != MODE_SENSE &&
391 SRpnt->cmd[0] != TEST_UNIT_READY) {
393 __scsi_print_sense(name, SRpnt->sense, SCSI_SENSE_BUFFERSIZE);
397 if (cmdstatp->fixed_format &&
398 STp->cln_mode >= EXTENDED_SENSE_START) { /* Only fixed format sense */
399 if (STp->cln_sense_value)
400 STp->cleaning_req |= ((SRpnt->sense[STp->cln_mode] &
401 STp->cln_sense_mask) == STp->cln_sense_value);
402 else
403 STp->cleaning_req |= ((SRpnt->sense[STp->cln_mode] &
404 STp->cln_sense_mask) != 0);
406 if (cmdstatp->have_sense &&
407 cmdstatp->sense_hdr.asc == 0 && cmdstatp->sense_hdr.ascq == 0x17)
408 STp->cleaning_req = 1; /* ASC and ASCQ => cleaning requested */
410 STp->pos_unknown |= STp->device->was_reset;
412 if (cmdstatp->have_sense &&
413 scode == RECOVERED_ERROR
414 #if ST_RECOVERED_WRITE_FATAL
415 && SRpnt->cmd[0] != WRITE_6
416 && SRpnt->cmd[0] != WRITE_FILEMARKS
417 #endif
419 STp->recover_count++;
420 STp->recover_reg++;
422 DEB(
423 if (debugging) {
424 if (SRpnt->cmd[0] == READ_6)
425 stp = "read";
426 else if (SRpnt->cmd[0] == WRITE_6)
427 stp = "write";
428 else
429 stp = "ioctl";
430 printk(ST_DEB_MSG "%s: Recovered %s error (%d).\n", name, stp,
431 STp->recover_count);
432 } ) /* end DEB */
434 if (cmdstatp->flags == 0)
435 return 0;
437 return (-EIO);
441 /* Wakeup from interrupt */
442 static void st_sleep_done(void *data, char *sense, int result, int resid)
444 struct st_request *SRpnt = data;
445 struct scsi_tape *STp = SRpnt->stp;
447 memcpy(SRpnt->sense, sense, SCSI_SENSE_BUFFERSIZE);
448 (STp->buffer)->cmdstat.midlevel_result = SRpnt->result = result;
449 DEB( STp->write_pending = 0; )
451 if (SRpnt->waiting)
452 complete(SRpnt->waiting);
455 static struct st_request *st_allocate_request(void)
457 return kzalloc(sizeof(struct st_request), GFP_KERNEL);
460 static void st_release_request(struct st_request *streq)
462 kfree(streq);
465 /* Do the scsi command. Waits until command performed if do_wait is true.
466 Otherwise write_behind_check() is used to check that the command
467 has finished. */
468 static struct st_request *
469 st_do_scsi(struct st_request * SRpnt, struct scsi_tape * STp, unsigned char *cmd,
470 int bytes, int direction, int timeout, int retries, int do_wait)
472 struct completion *waiting;
474 /* if async, make sure there's no command outstanding */
475 if (!do_wait && ((STp->buffer)->last_SRpnt)) {
476 printk(KERN_ERR "%s: Async command already active.\n",
477 tape_name(STp));
478 if (signal_pending(current))
479 (STp->buffer)->syscall_result = (-EINTR);
480 else
481 (STp->buffer)->syscall_result = (-EBUSY);
482 return NULL;
485 if (SRpnt == NULL) {
486 SRpnt = st_allocate_request();
487 if (SRpnt == NULL) {
488 DEBC( printk(KERN_ERR "%s: Can't get SCSI request.\n",
489 tape_name(STp)); );
490 if (signal_pending(current))
491 (STp->buffer)->syscall_result = (-EINTR);
492 else
493 (STp->buffer)->syscall_result = (-EBUSY);
494 return NULL;
496 SRpnt->stp = STp;
499 /* If async IO, set last_SRpnt. This ptr tells write_behind_check
500 which IO is outstanding. It's nulled out when the IO completes. */
501 if (!do_wait)
502 (STp->buffer)->last_SRpnt = SRpnt;
504 waiting = &STp->wait;
505 init_completion(waiting);
506 SRpnt->waiting = waiting;
508 if (!STp->buffer->do_dio)
509 buf_to_sg(STp->buffer, bytes);
511 memcpy(SRpnt->cmd, cmd, sizeof(SRpnt->cmd));
512 STp->buffer->cmdstat.have_sense = 0;
513 STp->buffer->syscall_result = 0;
515 if (scsi_execute_async(STp->device, cmd, COMMAND_SIZE(cmd[0]), direction,
516 &((STp->buffer)->sg[0]), bytes, (STp->buffer)->sg_segs,
517 timeout, retries, SRpnt, st_sleep_done, GFP_KERNEL)) {
518 /* could not allocate the buffer or request was too large */
519 (STp->buffer)->syscall_result = (-EBUSY);
520 (STp->buffer)->last_SRpnt = NULL;
522 else if (do_wait) {
523 wait_for_completion(waiting);
524 SRpnt->waiting = NULL;
525 (STp->buffer)->syscall_result = st_chk_result(STp, SRpnt);
528 return SRpnt;
532 /* Handle the write-behind checking (waits for completion). Returns -ENOSPC if
533 write has been correct but EOM early warning reached, -EIO if write ended in
534 error or zero if write successful. Asynchronous writes are used only in
535 variable block mode. */
536 static int write_behind_check(struct scsi_tape * STp)
538 int retval = 0;
539 struct st_buffer *STbuffer;
540 struct st_partstat *STps;
541 struct st_cmdstatus *cmdstatp;
542 struct st_request *SRpnt;
544 STbuffer = STp->buffer;
545 if (!STbuffer->writing)
546 return 0;
548 DEB(
549 if (STp->write_pending)
550 STp->nbr_waits++;
551 else
552 STp->nbr_finished++;
553 ) /* end DEB */
555 wait_for_completion(&(STp->wait));
556 SRpnt = STbuffer->last_SRpnt;
557 STbuffer->last_SRpnt = NULL;
558 SRpnt->waiting = NULL;
560 (STp->buffer)->syscall_result = st_chk_result(STp, SRpnt);
561 st_release_request(SRpnt);
563 STbuffer->buffer_bytes -= STbuffer->writing;
564 STps = &(STp->ps[STp->partition]);
565 if (STps->drv_block >= 0) {
566 if (STp->block_size == 0)
567 STps->drv_block++;
568 else
569 STps->drv_block += STbuffer->writing / STp->block_size;
572 cmdstatp = &STbuffer->cmdstat;
573 if (STbuffer->syscall_result) {
574 retval = -EIO;
575 if (cmdstatp->have_sense && !cmdstatp->deferred &&
576 (cmdstatp->flags & SENSE_EOM) &&
577 (cmdstatp->sense_hdr.sense_key == NO_SENSE ||
578 cmdstatp->sense_hdr.sense_key == RECOVERED_ERROR)) {
579 /* EOM at write-behind, has all data been written? */
580 if (!cmdstatp->remainder_valid ||
581 cmdstatp->uremainder64 == 0)
582 retval = -ENOSPC;
584 if (retval == -EIO)
585 STps->drv_block = -1;
587 STbuffer->writing = 0;
589 DEB(if (debugging && retval)
590 printk(ST_DEB_MSG "%s: Async write error %x, return value %d.\n",
591 tape_name(STp), STbuffer->cmdstat.midlevel_result, retval);) /* end DEB */
593 return retval;
597 /* Step over EOF if it has been inadvertently crossed (ioctl not used because
598 it messes up the block number). */
599 static int cross_eof(struct scsi_tape * STp, int forward)
601 struct st_request *SRpnt;
602 unsigned char cmd[MAX_COMMAND_SIZE];
604 cmd[0] = SPACE;
605 cmd[1] = 0x01; /* Space FileMarks */
606 if (forward) {
607 cmd[2] = cmd[3] = 0;
608 cmd[4] = 1;
609 } else
610 cmd[2] = cmd[3] = cmd[4] = 0xff; /* -1 filemarks */
611 cmd[5] = 0;
613 DEBC(printk(ST_DEB_MSG "%s: Stepping over filemark %s.\n",
614 tape_name(STp), forward ? "forward" : "backward"));
616 SRpnt = st_do_scsi(NULL, STp, cmd, 0, DMA_NONE,
617 STp->device->timeout, MAX_RETRIES, 1);
618 if (!SRpnt)
619 return (STp->buffer)->syscall_result;
621 st_release_request(SRpnt);
622 SRpnt = NULL;
624 if ((STp->buffer)->cmdstat.midlevel_result != 0)
625 printk(KERN_ERR "%s: Stepping over filemark %s failed.\n",
626 tape_name(STp), forward ? "forward" : "backward");
628 return (STp->buffer)->syscall_result;
632 /* Flush the write buffer (never need to write if variable blocksize). */
633 static int flush_write_buffer(struct scsi_tape * STp)
635 int offset, transfer, blks;
636 int result;
637 unsigned char cmd[MAX_COMMAND_SIZE];
638 struct st_request *SRpnt;
639 struct st_partstat *STps;
641 result = write_behind_check(STp);
642 if (result)
643 return result;
645 result = 0;
646 if (STp->dirty == 1) {
648 offset = (STp->buffer)->buffer_bytes;
649 transfer = ((offset + STp->block_size - 1) /
650 STp->block_size) * STp->block_size;
651 DEBC(printk(ST_DEB_MSG "%s: Flushing %d bytes.\n",
652 tape_name(STp), transfer));
654 memset((STp->buffer)->b_data + offset, 0, transfer - offset);
656 memset(cmd, 0, MAX_COMMAND_SIZE);
657 cmd[0] = WRITE_6;
658 cmd[1] = 1;
659 blks = transfer / STp->block_size;
660 cmd[2] = blks >> 16;
661 cmd[3] = blks >> 8;
662 cmd[4] = blks;
664 SRpnt = st_do_scsi(NULL, STp, cmd, transfer, DMA_TO_DEVICE,
665 STp->device->timeout, MAX_WRITE_RETRIES, 1);
666 if (!SRpnt)
667 return (STp->buffer)->syscall_result;
669 STps = &(STp->ps[STp->partition]);
670 if ((STp->buffer)->syscall_result != 0) {
671 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
673 if (cmdstatp->have_sense && !cmdstatp->deferred &&
674 (cmdstatp->flags & SENSE_EOM) &&
675 (cmdstatp->sense_hdr.sense_key == NO_SENSE ||
676 cmdstatp->sense_hdr.sense_key == RECOVERED_ERROR) &&
677 (!cmdstatp->remainder_valid ||
678 cmdstatp->uremainder64 == 0)) { /* All written at EOM early warning */
679 STp->dirty = 0;
680 (STp->buffer)->buffer_bytes = 0;
681 if (STps->drv_block >= 0)
682 STps->drv_block += blks;
683 result = (-ENOSPC);
684 } else {
685 printk(KERN_ERR "%s: Error on flush.\n",
686 tape_name(STp));
687 STps->drv_block = (-1);
688 result = (-EIO);
690 } else {
691 if (STps->drv_block >= 0)
692 STps->drv_block += blks;
693 STp->dirty = 0;
694 (STp->buffer)->buffer_bytes = 0;
696 st_release_request(SRpnt);
697 SRpnt = NULL;
699 return result;
703 /* Flush the tape buffer. The tape will be positioned correctly unless
704 seek_next is true. */
705 static int flush_buffer(struct scsi_tape *STp, int seek_next)
707 int backspace, result;
708 struct st_buffer *STbuffer;
709 struct st_partstat *STps;
711 STbuffer = STp->buffer;
714 * If there was a bus reset, block further access
715 * to this device.
717 if (STp->pos_unknown)
718 return (-EIO);
720 if (STp->ready != ST_READY)
721 return 0;
722 STps = &(STp->ps[STp->partition]);
723 if (STps->rw == ST_WRITING) /* Writing */
724 return flush_write_buffer(STp);
726 if (STp->block_size == 0)
727 return 0;
729 backspace = ((STp->buffer)->buffer_bytes +
730 (STp->buffer)->read_pointer) / STp->block_size -
731 ((STp->buffer)->read_pointer + STp->block_size - 1) /
732 STp->block_size;
733 (STp->buffer)->buffer_bytes = 0;
734 (STp->buffer)->read_pointer = 0;
735 result = 0;
736 if (!seek_next) {
737 if (STps->eof == ST_FM_HIT) {
738 result = cross_eof(STp, 0); /* Back over the EOF hit */
739 if (!result)
740 STps->eof = ST_NOEOF;
741 else {
742 if (STps->drv_file >= 0)
743 STps->drv_file++;
744 STps->drv_block = 0;
747 if (!result && backspace > 0)
748 result = st_int_ioctl(STp, MTBSR, backspace);
749 } else if (STps->eof == ST_FM_HIT) {
750 if (STps->drv_file >= 0)
751 STps->drv_file++;
752 STps->drv_block = 0;
753 STps->eof = ST_NOEOF;
755 return result;
759 /* Set the mode parameters */
760 static int set_mode_densblk(struct scsi_tape * STp, struct st_modedef * STm)
762 int set_it = 0;
763 unsigned long arg;
764 char *name = tape_name(STp);
766 if (!STp->density_changed &&
767 STm->default_density >= 0 &&
768 STm->default_density != STp->density) {
769 arg = STm->default_density;
770 set_it = 1;
771 } else
772 arg = STp->density;
773 arg <<= MT_ST_DENSITY_SHIFT;
774 if (!STp->blksize_changed &&
775 STm->default_blksize >= 0 &&
776 STm->default_blksize != STp->block_size) {
777 arg |= STm->default_blksize;
778 set_it = 1;
779 } else
780 arg |= STp->block_size;
781 if (set_it &&
782 st_int_ioctl(STp, SET_DENS_AND_BLK, arg)) {
783 printk(KERN_WARNING
784 "%s: Can't set default block size to %d bytes and density %x.\n",
785 name, STm->default_blksize, STm->default_density);
786 if (modes_defined)
787 return (-EINVAL);
789 return 0;
793 /* Lock or unlock the drive door. Don't use when st_request allocated. */
794 static int do_door_lock(struct scsi_tape * STp, int do_lock)
796 int retval, cmd;
797 DEB(char *name = tape_name(STp);)
800 cmd = do_lock ? SCSI_IOCTL_DOORLOCK : SCSI_IOCTL_DOORUNLOCK;
801 DEBC(printk(ST_DEB_MSG "%s: %socking drive door.\n", name,
802 do_lock ? "L" : "Unl"));
803 retval = scsi_ioctl(STp->device, cmd, NULL);
804 if (!retval) {
805 STp->door_locked = do_lock ? ST_LOCKED_EXPLICIT : ST_UNLOCKED;
807 else {
808 STp->door_locked = ST_LOCK_FAILS;
810 return retval;
814 /* Set the internal state after reset */
815 static void reset_state(struct scsi_tape *STp)
817 int i;
818 struct st_partstat *STps;
820 STp->pos_unknown = 0;
821 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
822 STps = &(STp->ps[i]);
823 STps->rw = ST_IDLE;
824 STps->eof = ST_NOEOF;
825 STps->at_sm = 0;
826 STps->last_block_valid = 0;
827 STps->drv_block = -1;
828 STps->drv_file = -1;
830 if (STp->can_partitions) {
831 STp->partition = find_partition(STp);
832 if (STp->partition < 0)
833 STp->partition = 0;
834 STp->new_partition = STp->partition;
838 /* Test if the drive is ready. Returns either one of the codes below or a negative system
839 error code. */
840 #define CHKRES_READY 0
841 #define CHKRES_NEW_SESSION 1
842 #define CHKRES_NOT_READY 2
843 #define CHKRES_NO_TAPE 3
845 #define MAX_ATTENTIONS 10
847 static int test_ready(struct scsi_tape *STp, int do_wait)
849 int attentions, waits, max_wait, scode;
850 int retval = CHKRES_READY, new_session = 0;
851 unsigned char cmd[MAX_COMMAND_SIZE];
852 struct st_request *SRpnt = NULL;
853 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
855 max_wait = do_wait ? ST_BLOCK_SECONDS : 0;
857 for (attentions=waits=0; ; ) {
858 memset((void *) &cmd[0], 0, MAX_COMMAND_SIZE);
859 cmd[0] = TEST_UNIT_READY;
860 SRpnt = st_do_scsi(SRpnt, STp, cmd, 0, DMA_NONE,
861 STp->long_timeout, MAX_READY_RETRIES, 1);
863 if (!SRpnt) {
864 retval = (STp->buffer)->syscall_result;
865 break;
868 if (cmdstatp->have_sense) {
870 scode = cmdstatp->sense_hdr.sense_key;
872 if (scode == UNIT_ATTENTION) { /* New media? */
873 new_session = 1;
874 if (attentions < MAX_ATTENTIONS) {
875 attentions++;
876 continue;
878 else {
879 retval = (-EIO);
880 break;
884 if (scode == NOT_READY) {
885 if (waits < max_wait) {
886 if (msleep_interruptible(1000)) {
887 retval = (-EINTR);
888 break;
890 waits++;
891 continue;
893 else {
894 if ((STp->device)->scsi_level >= SCSI_2 &&
895 cmdstatp->sense_hdr.asc == 0x3a) /* Check ASC */
896 retval = CHKRES_NO_TAPE;
897 else
898 retval = CHKRES_NOT_READY;
899 break;
904 retval = (STp->buffer)->syscall_result;
905 if (!retval)
906 retval = new_session ? CHKRES_NEW_SESSION : CHKRES_READY;
907 break;
910 if (SRpnt != NULL)
911 st_release_request(SRpnt);
912 return retval;
916 /* See if the drive is ready and gather information about the tape. Return values:
917 < 0 negative error code from errno.h
918 0 drive ready
919 1 drive not ready (possibly no tape)
921 static int check_tape(struct scsi_tape *STp, struct file *filp)
923 int i, retval, new_session = 0, do_wait;
924 unsigned char cmd[MAX_COMMAND_SIZE], saved_cleaning;
925 unsigned short st_flags = filp->f_flags;
926 struct st_request *SRpnt = NULL;
927 struct st_modedef *STm;
928 struct st_partstat *STps;
929 char *name = tape_name(STp);
930 struct inode *inode = filp->f_path.dentry->d_inode;
931 int mode = TAPE_MODE(inode);
933 STp->ready = ST_READY;
935 if (mode != STp->current_mode) {
936 DEBC(printk(ST_DEB_MSG "%s: Mode change from %d to %d.\n",
937 name, STp->current_mode, mode));
938 new_session = 1;
939 STp->current_mode = mode;
941 STm = &(STp->modes[STp->current_mode]);
943 saved_cleaning = STp->cleaning_req;
944 STp->cleaning_req = 0;
946 do_wait = ((filp->f_flags & O_NONBLOCK) == 0);
947 retval = test_ready(STp, do_wait);
949 if (retval < 0)
950 goto err_out;
952 if (retval == CHKRES_NEW_SESSION) {
953 STp->pos_unknown = 0;
954 STp->partition = STp->new_partition = 0;
955 if (STp->can_partitions)
956 STp->nbr_partitions = 1; /* This guess will be updated later
957 if necessary */
958 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
959 STps = &(STp->ps[i]);
960 STps->rw = ST_IDLE;
961 STps->eof = ST_NOEOF;
962 STps->at_sm = 0;
963 STps->last_block_valid = 0;
964 STps->drv_block = 0;
965 STps->drv_file = 0;
967 new_session = 1;
969 else {
970 STp->cleaning_req |= saved_cleaning;
972 if (retval == CHKRES_NOT_READY || retval == CHKRES_NO_TAPE) {
973 if (retval == CHKRES_NO_TAPE)
974 STp->ready = ST_NO_TAPE;
975 else
976 STp->ready = ST_NOT_READY;
978 STp->density = 0; /* Clear the erroneous "residue" */
979 STp->write_prot = 0;
980 STp->block_size = 0;
981 STp->ps[0].drv_file = STp->ps[0].drv_block = (-1);
982 STp->partition = STp->new_partition = 0;
983 STp->door_locked = ST_UNLOCKED;
984 return CHKRES_NOT_READY;
988 if (STp->omit_blklims)
989 STp->min_block = STp->max_block = (-1);
990 else {
991 memset((void *) &cmd[0], 0, MAX_COMMAND_SIZE);
992 cmd[0] = READ_BLOCK_LIMITS;
994 SRpnt = st_do_scsi(SRpnt, STp, cmd, 6, DMA_FROM_DEVICE,
995 STp->device->timeout, MAX_READY_RETRIES, 1);
996 if (!SRpnt) {
997 retval = (STp->buffer)->syscall_result;
998 goto err_out;
1001 if (!SRpnt->result && !STp->buffer->cmdstat.have_sense) {
1002 STp->max_block = ((STp->buffer)->b_data[1] << 16) |
1003 ((STp->buffer)->b_data[2] << 8) | (STp->buffer)->b_data[3];
1004 STp->min_block = ((STp->buffer)->b_data[4] << 8) |
1005 (STp->buffer)->b_data[5];
1006 if ( DEB( debugging || ) !STp->inited)
1007 printk(KERN_INFO
1008 "%s: Block limits %d - %d bytes.\n", name,
1009 STp->min_block, STp->max_block);
1010 } else {
1011 STp->min_block = STp->max_block = (-1);
1012 DEBC(printk(ST_DEB_MSG "%s: Can't read block limits.\n",
1013 name));
1017 memset((void *) &cmd[0], 0, MAX_COMMAND_SIZE);
1018 cmd[0] = MODE_SENSE;
1019 cmd[4] = 12;
1021 SRpnt = st_do_scsi(SRpnt, STp, cmd, 12, DMA_FROM_DEVICE,
1022 STp->device->timeout, MAX_READY_RETRIES, 1);
1023 if (!SRpnt) {
1024 retval = (STp->buffer)->syscall_result;
1025 goto err_out;
1028 if ((STp->buffer)->syscall_result != 0) {
1029 DEBC(printk(ST_DEB_MSG "%s: No Mode Sense.\n", name));
1030 STp->block_size = ST_DEFAULT_BLOCK; /* Educated guess (?) */
1031 (STp->buffer)->syscall_result = 0; /* Prevent error propagation */
1032 STp->drv_write_prot = 0;
1033 } else {
1034 DEBC(printk(ST_DEB_MSG
1035 "%s: Mode sense. Length %d, medium %x, WBS %x, BLL %d\n",
1036 name,
1037 (STp->buffer)->b_data[0], (STp->buffer)->b_data[1],
1038 (STp->buffer)->b_data[2], (STp->buffer)->b_data[3]));
1040 if ((STp->buffer)->b_data[3] >= 8) {
1041 STp->drv_buffer = ((STp->buffer)->b_data[2] >> 4) & 7;
1042 STp->density = (STp->buffer)->b_data[4];
1043 STp->block_size = (STp->buffer)->b_data[9] * 65536 +
1044 (STp->buffer)->b_data[10] * 256 + (STp->buffer)->b_data[11];
1045 DEBC(printk(ST_DEB_MSG
1046 "%s: Density %x, tape length: %x, drv buffer: %d\n",
1047 name, STp->density, (STp->buffer)->b_data[5] * 65536 +
1048 (STp->buffer)->b_data[6] * 256 + (STp->buffer)->b_data[7],
1049 STp->drv_buffer));
1051 STp->drv_write_prot = ((STp->buffer)->b_data[2] & 0x80) != 0;
1053 st_release_request(SRpnt);
1054 SRpnt = NULL;
1055 STp->inited = 1;
1057 if (STp->block_size > 0)
1058 (STp->buffer)->buffer_blocks =
1059 (STp->buffer)->buffer_size / STp->block_size;
1060 else
1061 (STp->buffer)->buffer_blocks = 1;
1062 (STp->buffer)->buffer_bytes = (STp->buffer)->read_pointer = 0;
1064 DEBC(printk(ST_DEB_MSG
1065 "%s: Block size: %d, buffer size: %d (%d blocks).\n", name,
1066 STp->block_size, (STp->buffer)->buffer_size,
1067 (STp->buffer)->buffer_blocks));
1069 if (STp->drv_write_prot) {
1070 STp->write_prot = 1;
1072 DEBC(printk(ST_DEB_MSG "%s: Write protected\n", name));
1074 if (do_wait &&
1075 ((st_flags & O_ACCMODE) == O_WRONLY ||
1076 (st_flags & O_ACCMODE) == O_RDWR)) {
1077 retval = (-EROFS);
1078 goto err_out;
1082 if (STp->can_partitions && STp->nbr_partitions < 1) {
1083 /* This code is reached when the device is opened for the first time
1084 after the driver has been initialized with tape in the drive and the
1085 partition support has been enabled. */
1086 DEBC(printk(ST_DEB_MSG
1087 "%s: Updating partition number in status.\n", name));
1088 if ((STp->partition = find_partition(STp)) < 0) {
1089 retval = STp->partition;
1090 goto err_out;
1092 STp->new_partition = STp->partition;
1093 STp->nbr_partitions = 1; /* This guess will be updated when necessary */
1096 if (new_session) { /* Change the drive parameters for the new mode */
1097 STp->density_changed = STp->blksize_changed = 0;
1098 STp->compression_changed = 0;
1099 if (!(STm->defaults_for_writes) &&
1100 (retval = set_mode_densblk(STp, STm)) < 0)
1101 goto err_out;
1103 if (STp->default_drvbuffer != 0xff) {
1104 if (st_int_ioctl(STp, MTSETDRVBUFFER, STp->default_drvbuffer))
1105 printk(KERN_WARNING
1106 "%s: Can't set default drive buffering to %d.\n",
1107 name, STp->default_drvbuffer);
1111 return CHKRES_READY;
1113 err_out:
1114 return retval;
1118 \f/* Open the device. Needs to be called with BKL only because of incrementing the SCSI host
1119 module count. */
1120 static int st_open(struct inode *inode, struct file *filp)
1122 int i, retval = (-EIO);
1123 struct scsi_tape *STp;
1124 struct st_partstat *STps;
1125 int dev = TAPE_NR(inode);
1126 char *name;
1129 * We really want to do nonseekable_open(inode, filp); here, but some
1130 * versions of tar incorrectly call lseek on tapes and bail out if that
1131 * fails. So we disallow pread() and pwrite(), but permit lseeks.
1133 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
1135 if (!(STp = scsi_tape_get(dev)))
1136 return -ENXIO;
1138 write_lock(&st_dev_arr_lock);
1139 filp->private_data = STp;
1140 name = tape_name(STp);
1142 if (STp->in_use) {
1143 write_unlock(&st_dev_arr_lock);
1144 scsi_tape_put(STp);
1145 DEB( printk(ST_DEB_MSG "%s: Device already in use.\n", name); )
1146 return (-EBUSY);
1149 STp->in_use = 1;
1150 write_unlock(&st_dev_arr_lock);
1151 STp->rew_at_close = STp->autorew_dev = (iminor(inode) & 0x80) == 0;
1153 if (!scsi_block_when_processing_errors(STp->device)) {
1154 retval = (-ENXIO);
1155 goto err_out;
1158 /* See that we have at least a one page buffer available */
1159 if (!enlarge_buffer(STp->buffer, PAGE_SIZE, STp->restr_dma)) {
1160 printk(KERN_WARNING "%s: Can't allocate one page tape buffer.\n",
1161 name);
1162 retval = (-EOVERFLOW);
1163 goto err_out;
1166 (STp->buffer)->writing = 0;
1167 (STp->buffer)->syscall_result = 0;
1169 STp->write_prot = ((filp->f_flags & O_ACCMODE) == O_RDONLY);
1171 STp->dirty = 0;
1172 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
1173 STps = &(STp->ps[i]);
1174 STps->rw = ST_IDLE;
1176 STp->try_dio_now = STp->try_dio;
1177 STp->recover_count = 0;
1178 DEB( STp->nbr_waits = STp->nbr_finished = 0;
1179 <<<<<<< HEAD:drivers/scsi/st.c
1180 STp->nbr_requests = STp->nbr_dio = STp->nbr_pages = STp->nbr_combinable = 0; )
1181 =======
1182 STp->nbr_requests = STp->nbr_dio = STp->nbr_pages = 0; )
1183 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/st.c
1185 retval = check_tape(STp, filp);
1186 if (retval < 0)
1187 goto err_out;
1188 if ((filp->f_flags & O_NONBLOCK) == 0 &&
1189 retval != CHKRES_READY) {
1190 if (STp->ready == NO_TAPE)
1191 retval = (-ENOMEDIUM);
1192 else
1193 retval = (-EIO);
1194 goto err_out;
1196 return 0;
1198 err_out:
1199 normalize_buffer(STp->buffer);
1200 STp->in_use = 0;
1201 scsi_tape_put(STp);
1202 return retval;
1207 /* Flush the tape buffer before close */
1208 static int st_flush(struct file *filp, fl_owner_t id)
1210 int result = 0, result2;
1211 unsigned char cmd[MAX_COMMAND_SIZE];
1212 struct st_request *SRpnt;
1213 struct scsi_tape *STp = filp->private_data;
1214 struct st_modedef *STm = &(STp->modes[STp->current_mode]);
1215 struct st_partstat *STps = &(STp->ps[STp->partition]);
1216 char *name = tape_name(STp);
1218 if (file_count(filp) > 1)
1219 return 0;
1221 if (STps->rw == ST_WRITING && !STp->pos_unknown) {
1222 result = flush_write_buffer(STp);
1223 if (result != 0 && result != (-ENOSPC))
1224 goto out;
1227 if (STp->can_partitions &&
1228 (result2 = switch_partition(STp)) < 0) {
1229 DEBC(printk(ST_DEB_MSG
1230 "%s: switch_partition at close failed.\n", name));
1231 if (result == 0)
1232 result = result2;
1233 goto out;
1236 DEBC( if (STp->nbr_requests)
1237 <<<<<<< HEAD:drivers/scsi/st.c
1238 printk(KERN_DEBUG "%s: Number of r/w requests %d, dio used in %d, pages %d (%d).\n",
1239 name, STp->nbr_requests, STp->nbr_dio, STp->nbr_pages, STp->nbr_combinable));
1240 =======
1241 printk(KERN_DEBUG "%s: Number of r/w requests %d, dio used in %d, pages %d.\n",
1242 name, STp->nbr_requests, STp->nbr_dio, STp->nbr_pages));
1243 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/st.c
1245 if (STps->rw == ST_WRITING && !STp->pos_unknown) {
1246 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
1248 DEBC(printk(ST_DEB_MSG "%s: Async write waits %d, finished %d.\n",
1249 name, STp->nbr_waits, STp->nbr_finished);
1252 memset(cmd, 0, MAX_COMMAND_SIZE);
1253 cmd[0] = WRITE_FILEMARKS;
1254 cmd[4] = 1 + STp->two_fm;
1256 SRpnt = st_do_scsi(NULL, STp, cmd, 0, DMA_NONE,
1257 STp->device->timeout, MAX_WRITE_RETRIES, 1);
1258 if (!SRpnt) {
1259 result = (STp->buffer)->syscall_result;
1260 goto out;
1263 if (STp->buffer->syscall_result == 0 ||
1264 (cmdstatp->have_sense && !cmdstatp->deferred &&
1265 (cmdstatp->flags & SENSE_EOM) &&
1266 (cmdstatp->sense_hdr.sense_key == NO_SENSE ||
1267 cmdstatp->sense_hdr.sense_key == RECOVERED_ERROR) &&
1268 (!cmdstatp->remainder_valid || cmdstatp->uremainder64 == 0))) {
1269 /* Write successful at EOM */
1270 st_release_request(SRpnt);
1271 SRpnt = NULL;
1272 if (STps->drv_file >= 0)
1273 STps->drv_file++;
1274 STps->drv_block = 0;
1275 if (STp->two_fm)
1276 cross_eof(STp, 0);
1277 STps->eof = ST_FM;
1279 else { /* Write error */
1280 st_release_request(SRpnt);
1281 SRpnt = NULL;
1282 printk(KERN_ERR "%s: Error on write filemark.\n", name);
1283 if (result == 0)
1284 result = (-EIO);
1287 DEBC(printk(ST_DEB_MSG "%s: Buffer flushed, %d EOF(s) written\n",
1288 name, cmd[4]));
1289 } else if (!STp->rew_at_close) {
1290 STps = &(STp->ps[STp->partition]);
1291 if (!STm->sysv || STps->rw != ST_READING) {
1292 if (STp->can_bsr)
1293 result = flush_buffer(STp, 0);
1294 else if (STps->eof == ST_FM_HIT) {
1295 result = cross_eof(STp, 0);
1296 if (result) {
1297 if (STps->drv_file >= 0)
1298 STps->drv_file++;
1299 STps->drv_block = 0;
1300 STps->eof = ST_FM;
1301 } else
1302 STps->eof = ST_NOEOF;
1304 } else if ((STps->eof == ST_NOEOF &&
1305 !(result = cross_eof(STp, 1))) ||
1306 STps->eof == ST_FM_HIT) {
1307 if (STps->drv_file >= 0)
1308 STps->drv_file++;
1309 STps->drv_block = 0;
1310 STps->eof = ST_FM;
1314 out:
1315 if (STp->rew_at_close) {
1316 result2 = st_int_ioctl(STp, MTREW, 1);
1317 if (result == 0)
1318 result = result2;
1320 return result;
1324 /* Close the device and release it. BKL is not needed: this is the only thread
1325 accessing this tape. */
1326 static int st_release(struct inode *inode, struct file *filp)
1328 int result = 0;
1329 struct scsi_tape *STp = filp->private_data;
1331 if (STp->door_locked == ST_LOCKED_AUTO)
1332 do_door_lock(STp, 0);
1334 normalize_buffer(STp->buffer);
1335 write_lock(&st_dev_arr_lock);
1336 STp->in_use = 0;
1337 write_unlock(&st_dev_arr_lock);
1338 scsi_tape_put(STp);
1340 return result;
1343 /* The checks common to both reading and writing */
1344 static ssize_t rw_checks(struct scsi_tape *STp, struct file *filp, size_t count)
1346 ssize_t retval = 0;
1349 * If we are in the middle of error recovery, don't let anyone
1350 * else try and use this device. Also, if error recovery fails, it
1351 * may try and take the device offline, in which case all further
1352 * access to the device is prohibited.
1354 if (!scsi_block_when_processing_errors(STp->device)) {
1355 retval = (-ENXIO);
1356 goto out;
1359 if (STp->ready != ST_READY) {
1360 if (STp->ready == ST_NO_TAPE)
1361 retval = (-ENOMEDIUM);
1362 else
1363 retval = (-EIO);
1364 goto out;
1367 if (! STp->modes[STp->current_mode].defined) {
1368 retval = (-ENXIO);
1369 goto out;
1374 * If there was a bus reset, block further access
1375 * to this device.
1377 if (STp->pos_unknown) {
1378 retval = (-EIO);
1379 goto out;
1382 if (count == 0)
1383 goto out;
1385 DEB(
1386 if (!STp->in_use) {
1387 printk(ST_DEB_MSG "%s: Incorrect device.\n", tape_name(STp));
1388 retval = (-EIO);
1389 goto out;
1390 } ) /* end DEB */
1392 if (STp->can_partitions &&
1393 (retval = switch_partition(STp)) < 0)
1394 goto out;
1396 if (STp->block_size == 0 && STp->max_block > 0 &&
1397 (count < STp->min_block || count > STp->max_block)) {
1398 retval = (-EINVAL);
1399 goto out;
1402 if (STp->do_auto_lock && STp->door_locked == ST_UNLOCKED &&
1403 !do_door_lock(STp, 1))
1404 STp->door_locked = ST_LOCKED_AUTO;
1406 out:
1407 return retval;
1411 static int setup_buffering(struct scsi_tape *STp, const char __user *buf,
1412 size_t count, int is_read)
1414 int i, bufsize, retval = 0;
1415 struct st_buffer *STbp = STp->buffer;
1417 if (is_read)
1418 i = STp->try_dio_now && try_rdio;
1419 else
1420 i = STp->try_dio_now && try_wdio;
1422 if (i && ((unsigned long)buf & queue_dma_alignment(
1423 STp->device->request_queue)) == 0) {
1424 i = sgl_map_user_pages(&(STbp->sg[0]), STbp->use_sg,
1425 (unsigned long)buf, count, (is_read ? READ : WRITE));
1426 if (i > 0) {
1427 STbp->do_dio = i;
1428 STbp->buffer_bytes = 0; /* can be used as transfer counter */
1430 else
1431 STbp->do_dio = 0; /* fall back to buffering with any error */
1432 STbp->sg_segs = STbp->do_dio;
1433 STbp->frp_sg_current = 0;
1434 DEB(
1435 if (STbp->do_dio) {
1436 STp->nbr_dio++;
1437 STp->nbr_pages += STbp->do_dio;
1438 <<<<<<< HEAD:drivers/scsi/st.c
1439 for (i=1; i < STbp->do_dio; i++)
1440 if (page_to_pfn(STbp->sg[i].page) == page_to_pfn(STbp->sg[i-1].page) + 1)
1441 STp->nbr_combinable++;
1442 =======
1443 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/st.c
1446 } else
1447 STbp->do_dio = 0;
1448 DEB( STp->nbr_requests++; )
1450 if (!STbp->do_dio) {
1451 if (STp->block_size)
1452 bufsize = STp->block_size > st_fixed_buffer_size ?
1453 STp->block_size : st_fixed_buffer_size;
1454 else
1455 bufsize = count;
1456 if (bufsize > STbp->buffer_size &&
1457 !enlarge_buffer(STbp, bufsize, STp->restr_dma)) {
1458 printk(KERN_WARNING "%s: Can't allocate %d byte tape buffer.\n",
1459 tape_name(STp), bufsize);
1460 retval = (-EOVERFLOW);
1461 goto out;
1463 if (STp->block_size)
1464 STbp->buffer_blocks = bufsize / STp->block_size;
1467 out:
1468 return retval;
1472 /* Can be called more than once after each setup_buffer() */
1473 static void release_buffering(struct scsi_tape *STp, int is_read)
1475 struct st_buffer *STbp;
1477 STbp = STp->buffer;
1478 if (STbp->do_dio) {
1479 sgl_unmap_user_pages(&(STbp->sg[0]), STbp->do_dio, is_read);
1480 STbp->do_dio = 0;
1481 STbp->sg_segs = 0;
1486 /* Write command */
1487 static ssize_t
1488 st_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
1490 ssize_t total;
1491 ssize_t i, do_count, blks, transfer;
1492 ssize_t retval;
1493 int undone, retry_eot = 0, scode;
1494 int async_write;
1495 unsigned char cmd[MAX_COMMAND_SIZE];
1496 const char __user *b_point;
1497 struct st_request *SRpnt = NULL;
1498 struct scsi_tape *STp = filp->private_data;
1499 struct st_modedef *STm;
1500 struct st_partstat *STps;
1501 struct st_buffer *STbp;
1502 char *name = tape_name(STp);
1504 if (mutex_lock_interruptible(&STp->lock))
1505 return -ERESTARTSYS;
1507 retval = rw_checks(STp, filp, count);
1508 if (retval || count == 0)
1509 goto out;
1511 /* Write must be integral number of blocks */
1512 if (STp->block_size != 0 && (count % STp->block_size) != 0) {
1513 printk(KERN_WARNING "%s: Write not multiple of tape block size.\n",
1514 name);
1515 retval = (-EINVAL);
1516 goto out;
1519 STm = &(STp->modes[STp->current_mode]);
1520 STps = &(STp->ps[STp->partition]);
1522 if (STp->write_prot) {
1523 retval = (-EACCES);
1524 goto out;
1528 if (STps->rw == ST_READING) {
1529 retval = flush_buffer(STp, 0);
1530 if (retval)
1531 goto out;
1532 STps->rw = ST_WRITING;
1533 } else if (STps->rw != ST_WRITING &&
1534 STps->drv_file == 0 && STps->drv_block == 0) {
1535 if ((retval = set_mode_densblk(STp, STm)) < 0)
1536 goto out;
1537 if (STm->default_compression != ST_DONT_TOUCH &&
1538 !(STp->compression_changed)) {
1539 if (st_compression(STp, (STm->default_compression == ST_YES))) {
1540 printk(KERN_WARNING "%s: Can't set default compression.\n",
1541 name);
1542 if (modes_defined) {
1543 retval = (-EINVAL);
1544 goto out;
1550 STbp = STp->buffer;
1551 i = write_behind_check(STp);
1552 if (i) {
1553 if (i == -ENOSPC)
1554 STps->eof = ST_EOM_OK;
1555 else
1556 STps->eof = ST_EOM_ERROR;
1559 if (STps->eof == ST_EOM_OK) {
1560 STps->eof = ST_EOD_1; /* allow next write */
1561 retval = (-ENOSPC);
1562 goto out;
1564 else if (STps->eof == ST_EOM_ERROR) {
1565 retval = (-EIO);
1566 goto out;
1569 /* Check the buffer readability in cases where copy_user might catch
1570 the problems after some tape movement. */
1571 if (STp->block_size != 0 &&
1572 !STbp->do_dio &&
1573 (copy_from_user(&i, buf, 1) != 0 ||
1574 copy_from_user(&i, buf + count - 1, 1) != 0)) {
1575 retval = (-EFAULT);
1576 goto out;
1579 retval = setup_buffering(STp, buf, count, 0);
1580 if (retval)
1581 goto out;
1583 total = count;
1585 memset(cmd, 0, MAX_COMMAND_SIZE);
1586 cmd[0] = WRITE_6;
1587 cmd[1] = (STp->block_size != 0);
1589 STps->rw = ST_WRITING;
1591 b_point = buf;
1592 while (count > 0 && !retry_eot) {
1594 if (STbp->do_dio) {
1595 do_count = count;
1597 else {
1598 if (STp->block_size == 0)
1599 do_count = count;
1600 else {
1601 do_count = STbp->buffer_blocks * STp->block_size -
1602 STbp->buffer_bytes;
1603 if (do_count > count)
1604 do_count = count;
1607 i = append_to_buffer(b_point, STbp, do_count);
1608 if (i) {
1609 retval = i;
1610 goto out;
1613 count -= do_count;
1614 b_point += do_count;
1616 async_write = STp->block_size == 0 && !STbp->do_dio &&
1617 STm->do_async_writes && STps->eof < ST_EOM_OK;
1619 if (STp->block_size != 0 && STm->do_buffer_writes &&
1620 !(STp->try_dio_now && try_wdio) && STps->eof < ST_EOM_OK &&
1621 STbp->buffer_bytes < STbp->buffer_size) {
1622 STp->dirty = 1;
1623 /* Don't write a buffer that is not full enough. */
1624 if (!async_write && count == 0)
1625 break;
1628 retry_write:
1629 if (STp->block_size == 0)
1630 blks = transfer = do_count;
1631 else {
1632 if (!STbp->do_dio)
1633 blks = STbp->buffer_bytes;
1634 else
1635 blks = do_count;
1636 blks /= STp->block_size;
1637 transfer = blks * STp->block_size;
1639 cmd[2] = blks >> 16;
1640 cmd[3] = blks >> 8;
1641 cmd[4] = blks;
1643 SRpnt = st_do_scsi(SRpnt, STp, cmd, transfer, DMA_TO_DEVICE,
1644 STp->device->timeout, MAX_WRITE_RETRIES, !async_write);
1645 if (!SRpnt) {
1646 retval = STbp->syscall_result;
1647 goto out;
1649 if (async_write && !STbp->syscall_result) {
1650 STbp->writing = transfer;
1651 STp->dirty = !(STbp->writing ==
1652 STbp->buffer_bytes);
1653 SRpnt = NULL; /* Prevent releasing this request! */
1654 DEB( STp->write_pending = 1; )
1655 break;
1658 if (STbp->syscall_result != 0) {
1659 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
1661 DEBC(printk(ST_DEB_MSG "%s: Error on write:\n", name));
1662 if (cmdstatp->have_sense && (cmdstatp->flags & SENSE_EOM)) {
1663 scode = cmdstatp->sense_hdr.sense_key;
1664 if (cmdstatp->remainder_valid)
1665 undone = (int)cmdstatp->uremainder64;
1666 else if (STp->block_size == 0 &&
1667 scode == VOLUME_OVERFLOW)
1668 undone = transfer;
1669 else
1670 undone = 0;
1671 if (STp->block_size != 0)
1672 undone *= STp->block_size;
1673 if (undone <= do_count) {
1674 /* Only data from this write is not written */
1675 count += undone;
1676 do_count -= undone;
1677 if (STp->block_size)
1678 blks = (transfer - undone) / STp->block_size;
1679 STps->eof = ST_EOM_OK;
1680 /* Continue in fixed block mode if all written
1681 in this request but still something left to write
1682 (retval left to zero)
1684 if (STp->block_size == 0 ||
1685 undone > 0 || count == 0)
1686 retval = (-ENOSPC); /* EOM within current request */
1687 DEBC(printk(ST_DEB_MSG
1688 "%s: EOM with %d bytes unwritten.\n",
1689 name, (int)count));
1690 } else {
1691 /* EOT within data buffered earlier (possible only
1692 in fixed block mode without direct i/o) */
1693 if (!retry_eot && !cmdstatp->deferred &&
1694 (scode == NO_SENSE || scode == RECOVERED_ERROR)) {
1695 move_buffer_data(STp->buffer, transfer - undone);
1696 retry_eot = 1;
1697 if (STps->drv_block >= 0) {
1698 STps->drv_block += (transfer - undone) /
1699 STp->block_size;
1701 STps->eof = ST_EOM_OK;
1702 DEBC(printk(ST_DEB_MSG
1703 "%s: Retry write of %d bytes at EOM.\n",
1704 name, STp->buffer->buffer_bytes));
1705 goto retry_write;
1707 else {
1708 /* Either error within data buffered by driver or
1709 failed retry */
1710 count -= do_count;
1711 blks = do_count = 0;
1712 STps->eof = ST_EOM_ERROR;
1713 STps->drv_block = (-1); /* Too cautious? */
1714 retval = (-EIO); /* EOM for old data */
1715 DEBC(printk(ST_DEB_MSG
1716 "%s: EOM with lost data.\n",
1717 name));
1720 } else {
1721 count += do_count;
1722 STps->drv_block = (-1); /* Too cautious? */
1723 retval = STbp->syscall_result;
1728 if (STps->drv_block >= 0) {
1729 if (STp->block_size == 0)
1730 STps->drv_block += (do_count > 0);
1731 else
1732 STps->drv_block += blks;
1735 STbp->buffer_bytes = 0;
1736 STp->dirty = 0;
1738 if (retval || retry_eot) {
1739 if (count < total)
1740 retval = total - count;
1741 goto out;
1745 if (STps->eof == ST_EOD_1)
1746 STps->eof = ST_EOM_OK;
1747 else if (STps->eof != ST_EOM_OK)
1748 STps->eof = ST_NOEOF;
1749 retval = total - count;
1751 out:
1752 if (SRpnt != NULL)
1753 st_release_request(SRpnt);
1754 release_buffering(STp, 0);
1755 mutex_unlock(&STp->lock);
1757 return retval;
1760 /* Read data from the tape. Returns zero in the normal case, one if the
1761 eof status has changed, and the negative error code in case of a
1762 fatal error. Otherwise updates the buffer and the eof state.
1764 Does release user buffer mapping if it is set.
1766 static long read_tape(struct scsi_tape *STp, long count,
1767 struct st_request ** aSRpnt)
1769 int transfer, blks, bytes;
1770 unsigned char cmd[MAX_COMMAND_SIZE];
1771 struct st_request *SRpnt;
1772 struct st_modedef *STm;
1773 struct st_partstat *STps;
1774 struct st_buffer *STbp;
1775 int retval = 0;
1776 char *name = tape_name(STp);
1778 if (count == 0)
1779 return 0;
1781 STm = &(STp->modes[STp->current_mode]);
1782 STps = &(STp->ps[STp->partition]);
1783 if (STps->eof == ST_FM_HIT)
1784 return 1;
1785 STbp = STp->buffer;
1787 if (STp->block_size == 0)
1788 blks = bytes = count;
1789 else {
1790 if (!(STp->try_dio_now && try_rdio) && STm->do_read_ahead) {
1791 blks = (STp->buffer)->buffer_blocks;
1792 bytes = blks * STp->block_size;
1793 } else {
1794 bytes = count;
1795 if (!STbp->do_dio && bytes > (STp->buffer)->buffer_size)
1796 bytes = (STp->buffer)->buffer_size;
1797 blks = bytes / STp->block_size;
1798 bytes = blks * STp->block_size;
1802 memset(cmd, 0, MAX_COMMAND_SIZE);
1803 cmd[0] = READ_6;
1804 cmd[1] = (STp->block_size != 0);
1805 cmd[2] = blks >> 16;
1806 cmd[3] = blks >> 8;
1807 cmd[4] = blks;
1809 SRpnt = *aSRpnt;
1810 SRpnt = st_do_scsi(SRpnt, STp, cmd, bytes, DMA_FROM_DEVICE,
1811 STp->device->timeout, MAX_RETRIES, 1);
1812 release_buffering(STp, 1);
1813 *aSRpnt = SRpnt;
1814 if (!SRpnt)
1815 return STbp->syscall_result;
1817 STbp->read_pointer = 0;
1818 STps->at_sm = 0;
1820 /* Something to check */
1821 if (STbp->syscall_result) {
1822 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
1824 retval = 1;
1825 DEBC(printk(ST_DEB_MSG "%s: Sense: %2x %2x %2x %2x %2x %2x %2x %2x\n",
1826 name,
1827 SRpnt->sense[0], SRpnt->sense[1],
1828 SRpnt->sense[2], SRpnt->sense[3],
1829 SRpnt->sense[4], SRpnt->sense[5],
1830 SRpnt->sense[6], SRpnt->sense[7]));
1831 if (cmdstatp->have_sense) {
1833 if (cmdstatp->sense_hdr.sense_key == BLANK_CHECK)
1834 cmdstatp->flags &= 0xcf; /* No need for EOM in this case */
1836 if (cmdstatp->flags != 0) { /* EOF, EOM, or ILI */
1837 /* Compute the residual count */
1838 if (cmdstatp->remainder_valid)
1839 transfer = (int)cmdstatp->uremainder64;
1840 else
1841 transfer = 0;
1842 if (STp->block_size == 0 &&
1843 cmdstatp->sense_hdr.sense_key == MEDIUM_ERROR)
1844 transfer = bytes;
1846 if (cmdstatp->flags & SENSE_ILI) { /* ILI */
1847 if (STp->block_size == 0) {
1848 if (transfer <= 0) {
1849 if (transfer < 0)
1850 printk(KERN_NOTICE
1851 "%s: Failed to read %d byte block with %d byte transfer.\n",
1852 name, bytes - transfer, bytes);
1853 if (STps->drv_block >= 0)
1854 STps->drv_block += 1;
1855 STbp->buffer_bytes = 0;
1856 return (-ENOMEM);
1858 STbp->buffer_bytes = bytes - transfer;
1859 } else {
1860 st_release_request(SRpnt);
1861 SRpnt = *aSRpnt = NULL;
1862 if (transfer == blks) { /* We did not get anything, error */
1863 printk(KERN_NOTICE "%s: Incorrect block size.\n", name);
1864 if (STps->drv_block >= 0)
1865 STps->drv_block += blks - transfer + 1;
1866 st_int_ioctl(STp, MTBSR, 1);
1867 return (-EIO);
1869 /* We have some data, deliver it */
1870 STbp->buffer_bytes = (blks - transfer) *
1871 STp->block_size;
1872 DEBC(printk(ST_DEB_MSG
1873 "%s: ILI but enough data received %ld %d.\n",
1874 name, count, STbp->buffer_bytes));
1875 if (STps->drv_block >= 0)
1876 STps->drv_block += 1;
1877 if (st_int_ioctl(STp, MTBSR, 1))
1878 return (-EIO);
1880 } else if (cmdstatp->flags & SENSE_FMK) { /* FM overrides EOM */
1881 if (STps->eof != ST_FM_HIT)
1882 STps->eof = ST_FM_HIT;
1883 else
1884 STps->eof = ST_EOD_2;
1885 if (STp->block_size == 0)
1886 STbp->buffer_bytes = 0;
1887 else
1888 STbp->buffer_bytes =
1889 bytes - transfer * STp->block_size;
1890 DEBC(printk(ST_DEB_MSG
1891 "%s: EOF detected (%d bytes read).\n",
1892 name, STbp->buffer_bytes));
1893 } else if (cmdstatp->flags & SENSE_EOM) {
1894 if (STps->eof == ST_FM)
1895 STps->eof = ST_EOD_1;
1896 else
1897 STps->eof = ST_EOM_OK;
1898 if (STp->block_size == 0)
1899 STbp->buffer_bytes = bytes - transfer;
1900 else
1901 STbp->buffer_bytes =
1902 bytes - transfer * STp->block_size;
1904 DEBC(printk(ST_DEB_MSG "%s: EOM detected (%d bytes read).\n",
1905 name, STbp->buffer_bytes));
1908 /* end of EOF, EOM, ILI test */
1909 else { /* nonzero sense key */
1910 DEBC(printk(ST_DEB_MSG
1911 "%s: Tape error while reading.\n", name));
1912 STps->drv_block = (-1);
1913 if (STps->eof == ST_FM &&
1914 cmdstatp->sense_hdr.sense_key == BLANK_CHECK) {
1915 DEBC(printk(ST_DEB_MSG
1916 "%s: Zero returned for first BLANK CHECK after EOF.\n",
1917 name));
1918 STps->eof = ST_EOD_2; /* First BLANK_CHECK after FM */
1919 } else /* Some other extended sense code */
1920 retval = (-EIO);
1923 if (STbp->buffer_bytes < 0) /* Caused by bogus sense data */
1924 STbp->buffer_bytes = 0;
1926 /* End of extended sense test */
1927 else { /* Non-extended sense */
1928 retval = STbp->syscall_result;
1932 /* End of error handling */
1933 else /* Read successful */
1934 STbp->buffer_bytes = bytes;
1936 if (STps->drv_block >= 0) {
1937 if (STp->block_size == 0)
1938 STps->drv_block++;
1939 else
1940 STps->drv_block += STbp->buffer_bytes / STp->block_size;
1942 return retval;
1946 /* Read command */
1947 static ssize_t
1948 st_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
1950 ssize_t total;
1951 ssize_t retval = 0;
1952 ssize_t i, transfer;
1953 int special, do_dio = 0;
1954 struct st_request *SRpnt = NULL;
1955 struct scsi_tape *STp = filp->private_data;
1956 struct st_modedef *STm;
1957 struct st_partstat *STps;
1958 struct st_buffer *STbp = STp->buffer;
1959 DEB( char *name = tape_name(STp); )
1961 if (mutex_lock_interruptible(&STp->lock))
1962 return -ERESTARTSYS;
1964 retval = rw_checks(STp, filp, count);
1965 if (retval || count == 0)
1966 goto out;
1968 STm = &(STp->modes[STp->current_mode]);
1969 if (STp->block_size != 0 && (count % STp->block_size) != 0) {
1970 if (!STm->do_read_ahead) {
1971 retval = (-EINVAL); /* Read must be integral number of blocks */
1972 goto out;
1974 STp->try_dio_now = 0; /* Direct i/o can't handle split blocks */
1977 STps = &(STp->ps[STp->partition]);
1978 if (STps->rw == ST_WRITING) {
1979 retval = flush_buffer(STp, 0);
1980 if (retval)
1981 goto out;
1982 STps->rw = ST_READING;
1984 DEB(
1985 if (debugging && STps->eof != ST_NOEOF)
1986 printk(ST_DEB_MSG "%s: EOF/EOM flag up (%d). Bytes %d\n", name,
1987 STps->eof, STbp->buffer_bytes);
1988 ) /* end DEB */
1990 retval = setup_buffering(STp, buf, count, 1);
1991 if (retval)
1992 goto out;
1993 do_dio = STbp->do_dio;
1995 if (STbp->buffer_bytes == 0 &&
1996 STps->eof >= ST_EOD_1) {
1997 if (STps->eof < ST_EOD) {
1998 STps->eof += 1;
1999 retval = 0;
2000 goto out;
2002 retval = (-EIO); /* EOM or Blank Check */
2003 goto out;
2006 if (do_dio) {
2007 /* Check the buffer writability before any tape movement. Don't alter
2008 buffer data. */
2009 if (copy_from_user(&i, buf, 1) != 0 ||
2010 copy_to_user(buf, &i, 1) != 0 ||
2011 copy_from_user(&i, buf + count - 1, 1) != 0 ||
2012 copy_to_user(buf + count - 1, &i, 1) != 0) {
2013 retval = (-EFAULT);
2014 goto out;
2018 STps->rw = ST_READING;
2021 /* Loop until enough data in buffer or a special condition found */
2022 for (total = 0, special = 0; total < count && !special;) {
2024 /* Get new data if the buffer is empty */
2025 if (STbp->buffer_bytes == 0) {
2026 special = read_tape(STp, count - total, &SRpnt);
2027 if (special < 0) { /* No need to continue read */
2028 retval = special;
2029 goto out;
2033 /* Move the data from driver buffer to user buffer */
2034 if (STbp->buffer_bytes > 0) {
2035 DEB(
2036 if (debugging && STps->eof != ST_NOEOF)
2037 printk(ST_DEB_MSG
2038 "%s: EOF up (%d). Left %d, needed %d.\n", name,
2039 STps->eof, STbp->buffer_bytes,
2040 (int)(count - total));
2041 ) /* end DEB */
2042 transfer = STbp->buffer_bytes < count - total ?
2043 STbp->buffer_bytes : count - total;
2044 if (!do_dio) {
2045 i = from_buffer(STbp, buf, transfer);
2046 if (i) {
2047 retval = i;
2048 goto out;
2051 buf += transfer;
2052 total += transfer;
2055 if (STp->block_size == 0)
2056 break; /* Read only one variable length block */
2058 } /* for (total = 0, special = 0;
2059 total < count && !special; ) */
2061 /* Change the eof state if no data from tape or buffer */
2062 if (total == 0) {
2063 if (STps->eof == ST_FM_HIT) {
2064 STps->eof = ST_FM;
2065 STps->drv_block = 0;
2066 if (STps->drv_file >= 0)
2067 STps->drv_file++;
2068 } else if (STps->eof == ST_EOD_1) {
2069 STps->eof = ST_EOD_2;
2070 STps->drv_block = 0;
2071 if (STps->drv_file >= 0)
2072 STps->drv_file++;
2073 } else if (STps->eof == ST_EOD_2)
2074 STps->eof = ST_EOD;
2075 } else if (STps->eof == ST_FM)
2076 STps->eof = ST_NOEOF;
2077 retval = total;
2079 out:
2080 if (SRpnt != NULL) {
2081 st_release_request(SRpnt);
2082 SRpnt = NULL;
2084 if (do_dio) {
2085 release_buffering(STp, 1);
2086 STbp->buffer_bytes = 0;
2088 mutex_unlock(&STp->lock);
2090 return retval;
2095 DEB(
2096 /* Set the driver options */
2097 static void st_log_options(struct scsi_tape * STp, struct st_modedef * STm, char *name)
2099 if (debugging) {
2100 printk(KERN_INFO
2101 "%s: Mode %d options: buffer writes: %d, async writes: %d, read ahead: %d\n",
2102 name, STp->current_mode, STm->do_buffer_writes, STm->do_async_writes,
2103 STm->do_read_ahead);
2104 printk(KERN_INFO
2105 "%s: can bsr: %d, two FMs: %d, fast mteom: %d, auto lock: %d,\n",
2106 name, STp->can_bsr, STp->two_fm, STp->fast_mteom, STp->do_auto_lock);
2107 printk(KERN_INFO
2108 "%s: defs for wr: %d, no block limits: %d, partitions: %d, s2 log: %d\n",
2109 name, STm->defaults_for_writes, STp->omit_blklims, STp->can_partitions,
2110 STp->scsi2_logical);
2111 printk(KERN_INFO
2112 "%s: sysv: %d nowait: %d\n", name, STm->sysv, STp->immediate);
2113 printk(KERN_INFO "%s: debugging: %d\n",
2114 name, debugging);
2120 static int st_set_options(struct scsi_tape *STp, long options)
2122 int value;
2123 long code;
2124 struct st_modedef *STm;
2125 char *name = tape_name(STp);
2126 struct cdev *cd0, *cd1;
2128 STm = &(STp->modes[STp->current_mode]);
2129 if (!STm->defined) {
2130 cd0 = STm->cdevs[0]; cd1 = STm->cdevs[1];
2131 memcpy(STm, &(STp->modes[0]), sizeof(struct st_modedef));
2132 STm->cdevs[0] = cd0; STm->cdevs[1] = cd1;
2133 modes_defined = 1;
2134 DEBC(printk(ST_DEB_MSG
2135 "%s: Initialized mode %d definition from mode 0\n",
2136 name, STp->current_mode));
2139 code = options & MT_ST_OPTIONS;
2140 if (code == MT_ST_BOOLEANS) {
2141 STm->do_buffer_writes = (options & MT_ST_BUFFER_WRITES) != 0;
2142 STm->do_async_writes = (options & MT_ST_ASYNC_WRITES) != 0;
2143 STm->defaults_for_writes = (options & MT_ST_DEF_WRITES) != 0;
2144 STm->do_read_ahead = (options & MT_ST_READ_AHEAD) != 0;
2145 STp->two_fm = (options & MT_ST_TWO_FM) != 0;
2146 STp->fast_mteom = (options & MT_ST_FAST_MTEOM) != 0;
2147 STp->do_auto_lock = (options & MT_ST_AUTO_LOCK) != 0;
2148 STp->can_bsr = (options & MT_ST_CAN_BSR) != 0;
2149 STp->omit_blklims = (options & MT_ST_NO_BLKLIMS) != 0;
2150 if ((STp->device)->scsi_level >= SCSI_2)
2151 STp->can_partitions = (options & MT_ST_CAN_PARTITIONS) != 0;
2152 STp->scsi2_logical = (options & MT_ST_SCSI2LOGICAL) != 0;
2153 STp->immediate = (options & MT_ST_NOWAIT) != 0;
2154 STm->sysv = (options & MT_ST_SYSV) != 0;
2155 DEB( debugging = (options & MT_ST_DEBUGGING) != 0;
2156 st_log_options(STp, STm, name); )
2157 } else if (code == MT_ST_SETBOOLEANS || code == MT_ST_CLEARBOOLEANS) {
2158 value = (code == MT_ST_SETBOOLEANS);
2159 if ((options & MT_ST_BUFFER_WRITES) != 0)
2160 STm->do_buffer_writes = value;
2161 if ((options & MT_ST_ASYNC_WRITES) != 0)
2162 STm->do_async_writes = value;
2163 if ((options & MT_ST_DEF_WRITES) != 0)
2164 STm->defaults_for_writes = value;
2165 if ((options & MT_ST_READ_AHEAD) != 0)
2166 STm->do_read_ahead = value;
2167 if ((options & MT_ST_TWO_FM) != 0)
2168 STp->two_fm = value;
2169 if ((options & MT_ST_FAST_MTEOM) != 0)
2170 STp->fast_mteom = value;
2171 if ((options & MT_ST_AUTO_LOCK) != 0)
2172 STp->do_auto_lock = value;
2173 if ((options & MT_ST_CAN_BSR) != 0)
2174 STp->can_bsr = value;
2175 if ((options & MT_ST_NO_BLKLIMS) != 0)
2176 STp->omit_blklims = value;
2177 if ((STp->device)->scsi_level >= SCSI_2 &&
2178 (options & MT_ST_CAN_PARTITIONS) != 0)
2179 STp->can_partitions = value;
2180 if ((options & MT_ST_SCSI2LOGICAL) != 0)
2181 STp->scsi2_logical = value;
2182 if ((options & MT_ST_NOWAIT) != 0)
2183 STp->immediate = value;
2184 if ((options & MT_ST_SYSV) != 0)
2185 STm->sysv = value;
2186 DEB(
2187 if ((options & MT_ST_DEBUGGING) != 0)
2188 debugging = value;
2189 st_log_options(STp, STm, name); )
2190 } else if (code == MT_ST_WRITE_THRESHOLD) {
2191 /* Retained for compatibility */
2192 } else if (code == MT_ST_DEF_BLKSIZE) {
2193 value = (options & ~MT_ST_OPTIONS);
2194 if (value == ~MT_ST_OPTIONS) {
2195 STm->default_blksize = (-1);
2196 DEBC( printk(KERN_INFO "%s: Default block size disabled.\n", name));
2197 } else {
2198 STm->default_blksize = value;
2199 DEBC( printk(KERN_INFO "%s: Default block size set to %d bytes.\n",
2200 name, STm->default_blksize));
2201 if (STp->ready == ST_READY) {
2202 STp->blksize_changed = 0;
2203 set_mode_densblk(STp, STm);
2206 } else if (code == MT_ST_TIMEOUTS) {
2207 value = (options & ~MT_ST_OPTIONS);
2208 if ((value & MT_ST_SET_LONG_TIMEOUT) != 0) {
2209 STp->long_timeout = (value & ~MT_ST_SET_LONG_TIMEOUT) * HZ;
2210 DEBC( printk(KERN_INFO "%s: Long timeout set to %d seconds.\n", name,
2211 (value & ~MT_ST_SET_LONG_TIMEOUT)));
2212 } else {
2213 STp->device->timeout = value * HZ;
2214 DEBC( printk(KERN_INFO "%s: Normal timeout set to %d seconds.\n",
2215 name, value) );
2217 } else if (code == MT_ST_SET_CLN) {
2218 value = (options & ~MT_ST_OPTIONS) & 0xff;
2219 if (value != 0 &&
2220 value < EXTENDED_SENSE_START && value >= SCSI_SENSE_BUFFERSIZE)
2221 return (-EINVAL);
2222 STp->cln_mode = value;
2223 STp->cln_sense_mask = (options >> 8) & 0xff;
2224 STp->cln_sense_value = (options >> 16) & 0xff;
2225 printk(KERN_INFO
2226 "%s: Cleaning request mode %d, mask %02x, value %02x\n",
2227 name, value, STp->cln_sense_mask, STp->cln_sense_value);
2228 } else if (code == MT_ST_DEF_OPTIONS) {
2229 code = (options & ~MT_ST_CLEAR_DEFAULT);
2230 value = (options & MT_ST_CLEAR_DEFAULT);
2231 if (code == MT_ST_DEF_DENSITY) {
2232 if (value == MT_ST_CLEAR_DEFAULT) {
2233 STm->default_density = (-1);
2234 DEBC( printk(KERN_INFO "%s: Density default disabled.\n",
2235 name));
2236 } else {
2237 STm->default_density = value & 0xff;
2238 DEBC( printk(KERN_INFO "%s: Density default set to %x\n",
2239 name, STm->default_density));
2240 if (STp->ready == ST_READY) {
2241 STp->density_changed = 0;
2242 set_mode_densblk(STp, STm);
2245 } else if (code == MT_ST_DEF_DRVBUFFER) {
2246 if (value == MT_ST_CLEAR_DEFAULT) {
2247 STp->default_drvbuffer = 0xff;
2248 DEBC( printk(KERN_INFO
2249 "%s: Drive buffer default disabled.\n", name));
2250 } else {
2251 STp->default_drvbuffer = value & 7;
2252 DEBC( printk(KERN_INFO
2253 "%s: Drive buffer default set to %x\n",
2254 name, STp->default_drvbuffer));
2255 if (STp->ready == ST_READY)
2256 st_int_ioctl(STp, MTSETDRVBUFFER, STp->default_drvbuffer);
2258 } else if (code == MT_ST_DEF_COMPRESSION) {
2259 if (value == MT_ST_CLEAR_DEFAULT) {
2260 STm->default_compression = ST_DONT_TOUCH;
2261 DEBC( printk(KERN_INFO
2262 "%s: Compression default disabled.\n", name));
2263 } else {
2264 if ((value & 0xff00) != 0) {
2265 STp->c_algo = (value & 0xff00) >> 8;
2266 DEBC( printk(KERN_INFO "%s: Compression algorithm set to 0x%x.\n",
2267 name, STp->c_algo));
2269 if ((value & 0xff) != 0xff) {
2270 STm->default_compression = (value & 1 ? ST_YES : ST_NO);
2271 DEBC( printk(KERN_INFO "%s: Compression default set to %x\n",
2272 name, (value & 1)));
2273 if (STp->ready == ST_READY) {
2274 STp->compression_changed = 0;
2275 st_compression(STp, (STm->default_compression == ST_YES));
2280 } else
2281 return (-EIO);
2283 return 0;
2286 #define MODE_HEADER_LENGTH 4
2288 /* Mode header and page byte offsets */
2289 #define MH_OFF_DATA_LENGTH 0
2290 #define MH_OFF_MEDIUM_TYPE 1
2291 #define MH_OFF_DEV_SPECIFIC 2
2292 #define MH_OFF_BDESCS_LENGTH 3
2293 #define MP_OFF_PAGE_NBR 0
2294 #define MP_OFF_PAGE_LENGTH 1
2296 /* Mode header and page bit masks */
2297 #define MH_BIT_WP 0x80
2298 #define MP_MSK_PAGE_NBR 0x3f
2300 /* Don't return block descriptors */
2301 #define MODE_SENSE_OMIT_BDESCS 0x08
2303 #define MODE_SELECT_PAGE_FORMAT 0x10
2305 /* Read a mode page into the tape buffer. The block descriptors are included
2306 if incl_block_descs is true. The page control is ored to the page number
2307 parameter, if necessary. */
2308 static int read_mode_page(struct scsi_tape *STp, int page, int omit_block_descs)
2310 unsigned char cmd[MAX_COMMAND_SIZE];
2311 struct st_request *SRpnt = NULL;
2313 memset(cmd, 0, MAX_COMMAND_SIZE);
2314 cmd[0] = MODE_SENSE;
2315 if (omit_block_descs)
2316 cmd[1] = MODE_SENSE_OMIT_BDESCS;
2317 cmd[2] = page;
2318 cmd[4] = 255;
2320 SRpnt = st_do_scsi(SRpnt, STp, cmd, cmd[4], DMA_FROM_DEVICE,
2321 STp->device->timeout, 0, 1);
2322 if (SRpnt == NULL)
2323 return (STp->buffer)->syscall_result;
2325 st_release_request(SRpnt);
2327 return (STp->buffer)->syscall_result;
2331 /* Send the mode page in the tape buffer to the drive. Assumes that the mode data
2332 in the buffer is correctly formatted. The long timeout is used if slow is non-zero. */
2333 static int write_mode_page(struct scsi_tape *STp, int page, int slow)
2335 int pgo;
2336 unsigned char cmd[MAX_COMMAND_SIZE];
2337 struct st_request *SRpnt = NULL;
2339 memset(cmd, 0, MAX_COMMAND_SIZE);
2340 cmd[0] = MODE_SELECT;
2341 cmd[1] = MODE_SELECT_PAGE_FORMAT;
2342 pgo = MODE_HEADER_LENGTH + (STp->buffer)->b_data[MH_OFF_BDESCS_LENGTH];
2343 cmd[4] = pgo + (STp->buffer)->b_data[pgo + MP_OFF_PAGE_LENGTH] + 2;
2345 /* Clear reserved fields */
2346 (STp->buffer)->b_data[MH_OFF_DATA_LENGTH] = 0;
2347 (STp->buffer)->b_data[MH_OFF_MEDIUM_TYPE] = 0;
2348 (STp->buffer)->b_data[MH_OFF_DEV_SPECIFIC] &= ~MH_BIT_WP;
2349 (STp->buffer)->b_data[pgo + MP_OFF_PAGE_NBR] &= MP_MSK_PAGE_NBR;
2351 SRpnt = st_do_scsi(SRpnt, STp, cmd, cmd[4], DMA_TO_DEVICE,
2352 (slow ? STp->long_timeout : STp->device->timeout), 0, 1);
2353 if (SRpnt == NULL)
2354 return (STp->buffer)->syscall_result;
2356 st_release_request(SRpnt);
2358 return (STp->buffer)->syscall_result;
2362 #define COMPRESSION_PAGE 0x0f
2363 #define COMPRESSION_PAGE_LENGTH 16
2365 #define CP_OFF_DCE_DCC 2
2366 #define CP_OFF_C_ALGO 7
2368 #define DCE_MASK 0x80
2369 #define DCC_MASK 0x40
2370 #define RED_MASK 0x60
2373 /* Control the compression with mode page 15. Algorithm not changed if zero.
2375 The block descriptors are read and written because Sony SDT-7000 does not
2376 work without this (suggestion from Michael Schaefer <Michael.Schaefer@dlr.de>).
2377 Including block descriptors should not cause any harm to other drives. */
2379 static int st_compression(struct scsi_tape * STp, int state)
2381 int retval;
2382 int mpoffs; /* Offset to mode page start */
2383 unsigned char *b_data = (STp->buffer)->b_data;
2384 DEB( char *name = tape_name(STp); )
2386 if (STp->ready != ST_READY)
2387 return (-EIO);
2389 /* Read the current page contents */
2390 retval = read_mode_page(STp, COMPRESSION_PAGE, 0);
2391 if (retval) {
2392 DEBC(printk(ST_DEB_MSG "%s: Compression mode page not supported.\n",
2393 name));
2394 return (-EIO);
2397 mpoffs = MODE_HEADER_LENGTH + b_data[MH_OFF_BDESCS_LENGTH];
2398 DEBC(printk(ST_DEB_MSG "%s: Compression state is %d.\n", name,
2399 (b_data[mpoffs + CP_OFF_DCE_DCC] & DCE_MASK ? 1 : 0)));
2401 /* Check if compression can be changed */
2402 if ((b_data[mpoffs + CP_OFF_DCE_DCC] & DCC_MASK) == 0) {
2403 DEBC(printk(ST_DEB_MSG "%s: Compression not supported.\n", name));
2404 return (-EIO);
2407 /* Do the change */
2408 if (state) {
2409 b_data[mpoffs + CP_OFF_DCE_DCC] |= DCE_MASK;
2410 if (STp->c_algo != 0)
2411 b_data[mpoffs + CP_OFF_C_ALGO] = STp->c_algo;
2413 else {
2414 b_data[mpoffs + CP_OFF_DCE_DCC] &= ~DCE_MASK;
2415 if (STp->c_algo != 0)
2416 b_data[mpoffs + CP_OFF_C_ALGO] = 0; /* no compression */
2419 retval = write_mode_page(STp, COMPRESSION_PAGE, 0);
2420 if (retval) {
2421 DEBC(printk(ST_DEB_MSG "%s: Compression change failed.\n", name));
2422 return (-EIO);
2424 DEBC(printk(ST_DEB_MSG "%s: Compression state changed to %d.\n",
2425 name, state));
2427 STp->compression_changed = 1;
2428 return 0;
2432 /* Process the load and unload commands (does unload if the load code is zero) */
2433 static int do_load_unload(struct scsi_tape *STp, struct file *filp, int load_code)
2435 int retval = (-EIO), timeout;
2436 DEB( char *name = tape_name(STp); )
2437 unsigned char cmd[MAX_COMMAND_SIZE];
2438 struct st_partstat *STps;
2439 struct st_request *SRpnt;
2441 if (STp->ready != ST_READY && !load_code) {
2442 if (STp->ready == ST_NO_TAPE)
2443 return (-ENOMEDIUM);
2444 else
2445 return (-EIO);
2448 memset(cmd, 0, MAX_COMMAND_SIZE);
2449 cmd[0] = START_STOP;
2450 if (load_code)
2451 cmd[4] |= 1;
2453 * If arg >= 1 && arg <= 6 Enhanced load/unload in HP C1553A
2455 if (load_code >= 1 + MT_ST_HPLOADER_OFFSET
2456 && load_code <= 6 + MT_ST_HPLOADER_OFFSET) {
2457 DEBC(printk(ST_DEB_MSG "%s: Enhanced %sload slot %2d.\n",
2458 name, (cmd[4]) ? "" : "un",
2459 load_code - MT_ST_HPLOADER_OFFSET));
2460 cmd[3] = load_code - MT_ST_HPLOADER_OFFSET; /* MediaID field of C1553A */
2462 if (STp->immediate) {
2463 cmd[1] = 1; /* Don't wait for completion */
2464 timeout = STp->device->timeout;
2466 else
2467 timeout = STp->long_timeout;
2469 DEBC(
2470 if (!load_code)
2471 printk(ST_DEB_MSG "%s: Unloading tape.\n", name);
2472 else
2473 printk(ST_DEB_MSG "%s: Loading tape.\n", name);
2476 SRpnt = st_do_scsi(NULL, STp, cmd, 0, DMA_NONE,
2477 timeout, MAX_RETRIES, 1);
2478 if (!SRpnt)
2479 return (STp->buffer)->syscall_result;
2481 retval = (STp->buffer)->syscall_result;
2482 st_release_request(SRpnt);
2484 if (!retval) { /* SCSI command successful */
2486 if (!load_code) {
2487 STp->rew_at_close = 0;
2488 STp->ready = ST_NO_TAPE;
2490 else {
2491 STp->rew_at_close = STp->autorew_dev;
2492 retval = check_tape(STp, filp);
2493 if (retval > 0)
2494 retval = 0;
2497 else {
2498 STps = &(STp->ps[STp->partition]);
2499 STps->drv_file = STps->drv_block = (-1);
2502 return retval;
2505 #if DEBUG
2506 #define ST_DEB_FORWARD 0
2507 #define ST_DEB_BACKWARD 1
2508 static void deb_space_print(char *name, int direction, char *units, unsigned char *cmd)
2510 s32 sc;
2512 sc = cmd[2] & 0x80 ? 0xff000000 : 0;
2513 sc |= (cmd[2] << 16) | (cmd[3] << 8) | cmd[4];
2514 if (direction)
2515 sc = -sc;
2516 printk(ST_DEB_MSG "%s: Spacing tape %s over %d %s.\n", name,
2517 direction ? "backward" : "forward", sc, units);
2519 #endif
2522 /* Internal ioctl function */
2523 static int st_int_ioctl(struct scsi_tape *STp, unsigned int cmd_in, unsigned long arg)
2525 int timeout;
2526 long ltmp;
2527 int ioctl_result;
2528 int chg_eof = 1;
2529 unsigned char cmd[MAX_COMMAND_SIZE];
2530 struct st_request *SRpnt;
2531 struct st_partstat *STps;
2532 int fileno, blkno, at_sm, undone;
2533 int datalen = 0, direction = DMA_NONE;
2534 char *name = tape_name(STp);
2536 WARN_ON(STp->buffer->do_dio != 0);
2537 if (STp->ready != ST_READY) {
2538 if (STp->ready == ST_NO_TAPE)
2539 return (-ENOMEDIUM);
2540 else
2541 return (-EIO);
2543 timeout = STp->long_timeout;
2544 STps = &(STp->ps[STp->partition]);
2545 fileno = STps->drv_file;
2546 blkno = STps->drv_block;
2547 at_sm = STps->at_sm;
2549 memset(cmd, 0, MAX_COMMAND_SIZE);
2550 switch (cmd_in) {
2551 case MTFSFM:
2552 chg_eof = 0; /* Changed from the FSF after this */
2553 case MTFSF:
2554 cmd[0] = SPACE;
2555 cmd[1] = 0x01; /* Space FileMarks */
2556 cmd[2] = (arg >> 16);
2557 cmd[3] = (arg >> 8);
2558 cmd[4] = arg;
2559 DEBC(deb_space_print(name, ST_DEB_FORWARD, "filemarks", cmd);)
2560 if (fileno >= 0)
2561 fileno += arg;
2562 blkno = 0;
2563 at_sm &= (arg == 0);
2564 break;
2565 case MTBSFM:
2566 chg_eof = 0; /* Changed from the FSF after this */
2567 case MTBSF:
2568 cmd[0] = SPACE;
2569 cmd[1] = 0x01; /* Space FileMarks */
2570 ltmp = (-arg);
2571 cmd[2] = (ltmp >> 16);
2572 cmd[3] = (ltmp >> 8);
2573 cmd[4] = ltmp;
2574 DEBC(deb_space_print(name, ST_DEB_BACKWARD, "filemarks", cmd);)
2575 if (fileno >= 0)
2576 fileno -= arg;
2577 blkno = (-1); /* We can't know the block number */
2578 at_sm &= (arg == 0);
2579 break;
2580 case MTFSR:
2581 cmd[0] = SPACE;
2582 cmd[1] = 0x00; /* Space Blocks */
2583 cmd[2] = (arg >> 16);
2584 cmd[3] = (arg >> 8);
2585 cmd[4] = arg;
2586 DEBC(deb_space_print(name, ST_DEB_FORWARD, "blocks", cmd);)
2587 if (blkno >= 0)
2588 blkno += arg;
2589 at_sm &= (arg == 0);
2590 break;
2591 case MTBSR:
2592 cmd[0] = SPACE;
2593 cmd[1] = 0x00; /* Space Blocks */
2594 ltmp = (-arg);
2595 cmd[2] = (ltmp >> 16);
2596 cmd[3] = (ltmp >> 8);
2597 cmd[4] = ltmp;
2598 DEBC(deb_space_print(name, ST_DEB_BACKWARD, "blocks", cmd);)
2599 if (blkno >= 0)
2600 blkno -= arg;
2601 at_sm &= (arg == 0);
2602 break;
2603 case MTFSS:
2604 cmd[0] = SPACE;
2605 cmd[1] = 0x04; /* Space Setmarks */
2606 cmd[2] = (arg >> 16);
2607 cmd[3] = (arg >> 8);
2608 cmd[4] = arg;
2609 DEBC(deb_space_print(name, ST_DEB_FORWARD, "setmarks", cmd);)
2610 if (arg != 0) {
2611 blkno = fileno = (-1);
2612 at_sm = 1;
2614 break;
2615 case MTBSS:
2616 cmd[0] = SPACE;
2617 cmd[1] = 0x04; /* Space Setmarks */
2618 ltmp = (-arg);
2619 cmd[2] = (ltmp >> 16);
2620 cmd[3] = (ltmp >> 8);
2621 cmd[4] = ltmp;
2622 DEBC(deb_space_print(name, ST_DEB_BACKWARD, "setmarks", cmd);)
2623 if (arg != 0) {
2624 blkno = fileno = (-1);
2625 at_sm = 1;
2627 break;
2628 case MTWEOF:
2629 case MTWSM:
2630 if (STp->write_prot)
2631 return (-EACCES);
2632 cmd[0] = WRITE_FILEMARKS;
2633 if (cmd_in == MTWSM)
2634 cmd[1] = 2;
2635 cmd[2] = (arg >> 16);
2636 cmd[3] = (arg >> 8);
2637 cmd[4] = arg;
2638 timeout = STp->device->timeout;
2639 DEBC(
2640 if (cmd_in == MTWEOF)
2641 printk(ST_DEB_MSG "%s: Writing %d filemarks.\n", name,
2642 cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
2643 else
2644 printk(ST_DEB_MSG "%s: Writing %d setmarks.\n", name,
2645 cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
2647 if (fileno >= 0)
2648 fileno += arg;
2649 blkno = 0;
2650 at_sm = (cmd_in == MTWSM);
2651 break;
2652 case MTREW:
2653 cmd[0] = REZERO_UNIT;
2654 if (STp->immediate) {
2655 cmd[1] = 1; /* Don't wait for completion */
2656 timeout = STp->device->timeout;
2658 DEBC(printk(ST_DEB_MSG "%s: Rewinding tape.\n", name));
2659 fileno = blkno = at_sm = 0;
2660 break;
2661 case MTNOP:
2662 DEBC(printk(ST_DEB_MSG "%s: No op on tape.\n", name));
2663 return 0; /* Should do something ? */
2664 break;
2665 case MTRETEN:
2666 cmd[0] = START_STOP;
2667 if (STp->immediate) {
2668 cmd[1] = 1; /* Don't wait for completion */
2669 timeout = STp->device->timeout;
2671 cmd[4] = 3;
2672 DEBC(printk(ST_DEB_MSG "%s: Retensioning tape.\n", name));
2673 fileno = blkno = at_sm = 0;
2674 break;
2675 case MTEOM:
2676 if (!STp->fast_mteom) {
2677 /* space to the end of tape */
2678 ioctl_result = st_int_ioctl(STp, MTFSF, 0x7fffff);
2679 fileno = STps->drv_file;
2680 if (STps->eof >= ST_EOD_1)
2681 return 0;
2682 /* The next lines would hide the number of spaced FileMarks
2683 That's why I inserted the previous lines. I had no luck
2684 with detecting EOM with FSF, so we go now to EOM.
2685 Joerg Weule */
2686 } else
2687 fileno = (-1);
2688 cmd[0] = SPACE;
2689 cmd[1] = 3;
2690 DEBC(printk(ST_DEB_MSG "%s: Spacing to end of recorded medium.\n",
2691 name));
2692 blkno = -1;
2693 at_sm = 0;
2694 break;
2695 case MTERASE:
2696 if (STp->write_prot)
2697 return (-EACCES);
2698 cmd[0] = ERASE;
2699 cmd[1] = (arg ? 1 : 0); /* Long erase with non-zero argument */
2700 if (STp->immediate) {
2701 cmd[1] |= 2; /* Don't wait for completion */
2702 timeout = STp->device->timeout;
2704 else
2705 timeout = STp->long_timeout * 8;
2707 DEBC(printk(ST_DEB_MSG "%s: Erasing tape.\n", name));
2708 fileno = blkno = at_sm = 0;
2709 break;
2710 case MTSETBLK: /* Set block length */
2711 case MTSETDENSITY: /* Set tape density */
2712 case MTSETDRVBUFFER: /* Set drive buffering */
2713 case SET_DENS_AND_BLK: /* Set density and block size */
2714 chg_eof = 0;
2715 if (STp->dirty || (STp->buffer)->buffer_bytes != 0)
2716 return (-EIO); /* Not allowed if data in buffer */
2717 if ((cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) &&
2718 (arg & MT_ST_BLKSIZE_MASK) != 0 &&
2719 STp->max_block > 0 &&
2720 ((arg & MT_ST_BLKSIZE_MASK) < STp->min_block ||
2721 (arg & MT_ST_BLKSIZE_MASK) > STp->max_block)) {
2722 printk(KERN_WARNING "%s: Illegal block size.\n", name);
2723 return (-EINVAL);
2725 cmd[0] = MODE_SELECT;
2726 if ((STp->use_pf & USE_PF))
2727 cmd[1] = MODE_SELECT_PAGE_FORMAT;
2728 cmd[4] = datalen = 12;
2729 direction = DMA_TO_DEVICE;
2731 memset((STp->buffer)->b_data, 0, 12);
2732 if (cmd_in == MTSETDRVBUFFER)
2733 (STp->buffer)->b_data[2] = (arg & 7) << 4;
2734 else
2735 (STp->buffer)->b_data[2] =
2736 STp->drv_buffer << 4;
2737 (STp->buffer)->b_data[3] = 8; /* block descriptor length */
2738 if (cmd_in == MTSETDENSITY) {
2739 (STp->buffer)->b_data[4] = arg;
2740 STp->density_changed = 1; /* At least we tried ;-) */
2741 } else if (cmd_in == SET_DENS_AND_BLK)
2742 (STp->buffer)->b_data[4] = arg >> 24;
2743 else
2744 (STp->buffer)->b_data[4] = STp->density;
2745 if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) {
2746 ltmp = arg & MT_ST_BLKSIZE_MASK;
2747 if (cmd_in == MTSETBLK)
2748 STp->blksize_changed = 1; /* At least we tried ;-) */
2749 } else
2750 ltmp = STp->block_size;
2751 (STp->buffer)->b_data[9] = (ltmp >> 16);
2752 (STp->buffer)->b_data[10] = (ltmp >> 8);
2753 (STp->buffer)->b_data[11] = ltmp;
2754 timeout = STp->device->timeout;
2755 DEBC(
2756 if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK)
2757 printk(ST_DEB_MSG
2758 "%s: Setting block size to %d bytes.\n", name,
2759 (STp->buffer)->b_data[9] * 65536 +
2760 (STp->buffer)->b_data[10] * 256 +
2761 (STp->buffer)->b_data[11]);
2762 if (cmd_in == MTSETDENSITY || cmd_in == SET_DENS_AND_BLK)
2763 printk(ST_DEB_MSG
2764 "%s: Setting density code to %x.\n", name,
2765 (STp->buffer)->b_data[4]);
2766 if (cmd_in == MTSETDRVBUFFER)
2767 printk(ST_DEB_MSG
2768 "%s: Setting drive buffer code to %d.\n", name,
2769 ((STp->buffer)->b_data[2] >> 4) & 7);
2771 break;
2772 default:
2773 return (-ENOSYS);
2776 SRpnt = st_do_scsi(NULL, STp, cmd, datalen, direction,
2777 timeout, MAX_RETRIES, 1);
2778 if (!SRpnt)
2779 return (STp->buffer)->syscall_result;
2781 ioctl_result = (STp->buffer)->syscall_result;
2783 if (!ioctl_result) { /* SCSI command successful */
2784 st_release_request(SRpnt);
2785 SRpnt = NULL;
2786 STps->drv_block = blkno;
2787 STps->drv_file = fileno;
2788 STps->at_sm = at_sm;
2790 if (cmd_in == MTBSFM)
2791 ioctl_result = st_int_ioctl(STp, MTFSF, 1);
2792 else if (cmd_in == MTFSFM)
2793 ioctl_result = st_int_ioctl(STp, MTBSF, 1);
2795 if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) {
2796 int old_block_size = STp->block_size;
2797 STp->block_size = arg & MT_ST_BLKSIZE_MASK;
2798 if (STp->block_size != 0) {
2799 if (old_block_size == 0)
2800 normalize_buffer(STp->buffer);
2801 (STp->buffer)->buffer_blocks =
2802 (STp->buffer)->buffer_size / STp->block_size;
2804 (STp->buffer)->buffer_bytes = (STp->buffer)->read_pointer = 0;
2805 if (cmd_in == SET_DENS_AND_BLK)
2806 STp->density = arg >> MT_ST_DENSITY_SHIFT;
2807 } else if (cmd_in == MTSETDRVBUFFER)
2808 STp->drv_buffer = (arg & 7);
2809 else if (cmd_in == MTSETDENSITY)
2810 STp->density = arg;
2812 if (cmd_in == MTEOM)
2813 STps->eof = ST_EOD;
2814 else if (cmd_in == MTFSF)
2815 STps->eof = ST_FM;
2816 else if (chg_eof)
2817 STps->eof = ST_NOEOF;
2819 if (cmd_in == MTWEOF)
2820 STps->rw = ST_IDLE;
2821 } else { /* SCSI command was not completely successful. Don't return
2822 from this block without releasing the SCSI command block! */
2823 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
2825 if (cmdstatp->flags & SENSE_EOM) {
2826 if (cmd_in != MTBSF && cmd_in != MTBSFM &&
2827 cmd_in != MTBSR && cmd_in != MTBSS)
2828 STps->eof = ST_EOM_OK;
2829 STps->drv_block = 0;
2832 if (cmdstatp->remainder_valid)
2833 undone = (int)cmdstatp->uremainder64;
2834 else
2835 undone = 0;
2837 if (cmd_in == MTWEOF &&
2838 cmdstatp->have_sense &&
2839 (cmdstatp->flags & SENSE_EOM)) {
2840 if (cmdstatp->sense_hdr.sense_key == NO_SENSE ||
2841 cmdstatp->sense_hdr.sense_key == RECOVERED_ERROR) {
2842 ioctl_result = 0; /* EOF(s) written successfully at EOM */
2843 STps->eof = ST_NOEOF;
2844 } else { /* Writing EOF(s) failed */
2845 if (fileno >= 0)
2846 fileno -= undone;
2847 if (undone < arg)
2848 STps->eof = ST_NOEOF;
2850 STps->drv_file = fileno;
2851 } else if ((cmd_in == MTFSF) || (cmd_in == MTFSFM)) {
2852 if (fileno >= 0)
2853 STps->drv_file = fileno - undone;
2854 else
2855 STps->drv_file = fileno;
2856 STps->drv_block = -1;
2857 STps->eof = ST_NOEOF;
2858 } else if ((cmd_in == MTBSF) || (cmd_in == MTBSFM)) {
2859 if (arg > 0 && undone < 0) /* Some drives get this wrong */
2860 undone = (-undone);
2861 if (STps->drv_file >= 0)
2862 STps->drv_file = fileno + undone;
2863 STps->drv_block = 0;
2864 STps->eof = ST_NOEOF;
2865 } else if (cmd_in == MTFSR) {
2866 if (cmdstatp->flags & SENSE_FMK) { /* Hit filemark */
2867 if (STps->drv_file >= 0)
2868 STps->drv_file++;
2869 STps->drv_block = 0;
2870 STps->eof = ST_FM;
2871 } else {
2872 if (blkno >= undone)
2873 STps->drv_block = blkno - undone;
2874 else
2875 STps->drv_block = (-1);
2876 STps->eof = ST_NOEOF;
2878 } else if (cmd_in == MTBSR) {
2879 if (cmdstatp->flags & SENSE_FMK) { /* Hit filemark */
2880 STps->drv_file--;
2881 STps->drv_block = (-1);
2882 } else {
2883 if (arg > 0 && undone < 0) /* Some drives get this wrong */
2884 undone = (-undone);
2885 if (STps->drv_block >= 0)
2886 STps->drv_block = blkno + undone;
2888 STps->eof = ST_NOEOF;
2889 } else if (cmd_in == MTEOM) {
2890 STps->drv_file = (-1);
2891 STps->drv_block = (-1);
2892 STps->eof = ST_EOD;
2893 } else if (cmd_in == MTSETBLK ||
2894 cmd_in == MTSETDENSITY ||
2895 cmd_in == MTSETDRVBUFFER ||
2896 cmd_in == SET_DENS_AND_BLK) {
2897 if (cmdstatp->sense_hdr.sense_key == ILLEGAL_REQUEST &&
2898 !(STp->use_pf & PF_TESTED)) {
2899 /* Try the other possible state of Page Format if not
2900 already tried */
2901 STp->use_pf = !STp->use_pf | PF_TESTED;
2902 st_release_request(SRpnt);
2903 SRpnt = NULL;
2904 return st_int_ioctl(STp, cmd_in, arg);
2906 } else if (chg_eof)
2907 STps->eof = ST_NOEOF;
2909 if (cmdstatp->sense_hdr.sense_key == BLANK_CHECK)
2910 STps->eof = ST_EOD;
2912 st_release_request(SRpnt);
2913 SRpnt = NULL;
2916 return ioctl_result;
2920 /* Get the tape position. If bt == 2, arg points into a kernel space mt_loc
2921 structure. */
2923 static int get_location(struct scsi_tape *STp, unsigned int *block, int *partition,
2924 int logical)
2926 int result;
2927 unsigned char scmd[MAX_COMMAND_SIZE];
2928 struct st_request *SRpnt;
2929 DEB( char *name = tape_name(STp); )
2931 if (STp->ready != ST_READY)
2932 return (-EIO);
2934 memset(scmd, 0, MAX_COMMAND_SIZE);
2935 if ((STp->device)->scsi_level < SCSI_2) {
2936 scmd[0] = QFA_REQUEST_BLOCK;
2937 scmd[4] = 3;
2938 } else {
2939 scmd[0] = READ_POSITION;
2940 if (!logical && !STp->scsi2_logical)
2941 scmd[1] = 1;
2943 SRpnt = st_do_scsi(NULL, STp, scmd, 20, DMA_FROM_DEVICE,
2944 STp->device->timeout, MAX_READY_RETRIES, 1);
2945 if (!SRpnt)
2946 return (STp->buffer)->syscall_result;
2948 if ((STp->buffer)->syscall_result != 0 ||
2949 (STp->device->scsi_level >= SCSI_2 &&
2950 ((STp->buffer)->b_data[0] & 4) != 0)) {
2951 *block = *partition = 0;
2952 DEBC(printk(ST_DEB_MSG "%s: Can't read tape position.\n", name));
2953 result = (-EIO);
2954 } else {
2955 result = 0;
2956 if ((STp->device)->scsi_level < SCSI_2) {
2957 *block = ((STp->buffer)->b_data[0] << 16)
2958 + ((STp->buffer)->b_data[1] << 8)
2959 + (STp->buffer)->b_data[2];
2960 *partition = 0;
2961 } else {
2962 *block = ((STp->buffer)->b_data[4] << 24)
2963 + ((STp->buffer)->b_data[5] << 16)
2964 + ((STp->buffer)->b_data[6] << 8)
2965 + (STp->buffer)->b_data[7];
2966 *partition = (STp->buffer)->b_data[1];
2967 if (((STp->buffer)->b_data[0] & 0x80) &&
2968 (STp->buffer)->b_data[1] == 0) /* BOP of partition 0 */
2969 STp->ps[0].drv_block = STp->ps[0].drv_file = 0;
2971 DEBC(printk(ST_DEB_MSG "%s: Got tape pos. blk %d part %d.\n", name,
2972 *block, *partition));
2974 st_release_request(SRpnt);
2975 SRpnt = NULL;
2977 return result;
2981 /* Set the tape block and partition. Negative partition means that only the
2982 block should be set in vendor specific way. */
2983 static int set_location(struct scsi_tape *STp, unsigned int block, int partition,
2984 int logical)
2986 struct st_partstat *STps;
2987 int result, p;
2988 unsigned int blk;
2989 int timeout;
2990 unsigned char scmd[MAX_COMMAND_SIZE];
2991 struct st_request *SRpnt;
2992 DEB( char *name = tape_name(STp); )
2994 if (STp->ready != ST_READY)
2995 return (-EIO);
2996 timeout = STp->long_timeout;
2997 STps = &(STp->ps[STp->partition]);
2999 DEBC(printk(ST_DEB_MSG "%s: Setting block to %d and partition to %d.\n",
3000 name, block, partition));
3001 DEB(if (partition < 0)
3002 return (-EIO); )
3004 /* Update the location at the partition we are leaving */
3005 if ((!STp->can_partitions && partition != 0) ||
3006 partition >= ST_NBR_PARTITIONS)
3007 return (-EINVAL);
3008 if (partition != STp->partition) {
3009 if (get_location(STp, &blk, &p, 1))
3010 STps->last_block_valid = 0;
3011 else {
3012 STps->last_block_valid = 1;
3013 STps->last_block_visited = blk;
3014 DEBC(printk(ST_DEB_MSG
3015 "%s: Visited block %d for partition %d saved.\n",
3016 name, blk, STp->partition));
3020 memset(scmd, 0, MAX_COMMAND_SIZE);
3021 if ((STp->device)->scsi_level < SCSI_2) {
3022 scmd[0] = QFA_SEEK_BLOCK;
3023 scmd[2] = (block >> 16);
3024 scmd[3] = (block >> 8);
3025 scmd[4] = block;
3026 scmd[5] = 0;
3027 } else {
3028 scmd[0] = SEEK_10;
3029 scmd[3] = (block >> 24);
3030 scmd[4] = (block >> 16);
3031 scmd[5] = (block >> 8);
3032 scmd[6] = block;
3033 if (!logical && !STp->scsi2_logical)
3034 scmd[1] = 4;
3035 if (STp->partition != partition) {
3036 scmd[1] |= 2;
3037 scmd[8] = partition;
3038 DEBC(printk(ST_DEB_MSG
3039 "%s: Trying to change partition from %d to %d\n",
3040 name, STp->partition, partition));
3043 if (STp->immediate) {
3044 scmd[1] |= 1; /* Don't wait for completion */
3045 timeout = STp->device->timeout;
3048 SRpnt = st_do_scsi(NULL, STp, scmd, 0, DMA_NONE,
3049 timeout, MAX_READY_RETRIES, 1);
3050 if (!SRpnt)
3051 return (STp->buffer)->syscall_result;
3053 STps->drv_block = STps->drv_file = (-1);
3054 STps->eof = ST_NOEOF;
3055 if ((STp->buffer)->syscall_result != 0) {
3056 result = (-EIO);
3057 if (STp->can_partitions &&
3058 (STp->device)->scsi_level >= SCSI_2 &&
3059 (p = find_partition(STp)) >= 0)
3060 STp->partition = p;
3061 } else {
3062 if (STp->can_partitions) {
3063 STp->partition = partition;
3064 STps = &(STp->ps[partition]);
3065 if (!STps->last_block_valid ||
3066 STps->last_block_visited != block) {
3067 STps->at_sm = 0;
3068 STps->rw = ST_IDLE;
3070 } else
3071 STps->at_sm = 0;
3072 if (block == 0)
3073 STps->drv_block = STps->drv_file = 0;
3074 result = 0;
3077 st_release_request(SRpnt);
3078 SRpnt = NULL;
3080 return result;
3084 /* Find the current partition number for the drive status. Called from open and
3085 returns either partition number of negative error code. */
3086 static int find_partition(struct scsi_tape *STp)
3088 int i, partition;
3089 unsigned int block;
3091 if ((i = get_location(STp, &block, &partition, 1)) < 0)
3092 return i;
3093 if (partition >= ST_NBR_PARTITIONS)
3094 return (-EIO);
3095 return partition;
3099 /* Change the partition if necessary */
3100 static int switch_partition(struct scsi_tape *STp)
3102 struct st_partstat *STps;
3104 if (STp->partition == STp->new_partition)
3105 return 0;
3106 STps = &(STp->ps[STp->new_partition]);
3107 if (!STps->last_block_valid)
3108 STps->last_block_visited = 0;
3109 return set_location(STp, STps->last_block_visited, STp->new_partition, 1);
3112 /* Functions for reading and writing the medium partition mode page. */
3114 #define PART_PAGE 0x11
3115 #define PART_PAGE_FIXED_LENGTH 8
3117 #define PP_OFF_MAX_ADD_PARTS 2
3118 #define PP_OFF_NBR_ADD_PARTS 3
3119 #define PP_OFF_FLAGS 4
3120 #define PP_OFF_PART_UNITS 6
3121 #define PP_OFF_RESERVED 7
3123 #define PP_BIT_IDP 0x20
3124 #define PP_MSK_PSUM_MB 0x10
3126 /* Get the number of partitions on the tape. As a side effect reads the
3127 mode page into the tape buffer. */
3128 static int nbr_partitions(struct scsi_tape *STp)
3130 int result;
3131 DEB( char *name = tape_name(STp); )
3133 if (STp->ready != ST_READY)
3134 return (-EIO);
3136 result = read_mode_page(STp, PART_PAGE, 1);
3138 if (result) {
3139 DEBC(printk(ST_DEB_MSG "%s: Can't read medium partition page.\n",
3140 name));
3141 result = (-EIO);
3142 } else {
3143 result = (STp->buffer)->b_data[MODE_HEADER_LENGTH +
3144 PP_OFF_NBR_ADD_PARTS] + 1;
3145 DEBC(printk(ST_DEB_MSG "%s: Number of partitions %d.\n", name, result));
3148 return result;
3152 /* Partition the tape into two partitions if size > 0 or one partition if
3153 size == 0.
3155 The block descriptors are read and written because Sony SDT-7000 does not
3156 work without this (suggestion from Michael Schaefer <Michael.Schaefer@dlr.de>).
3158 My HP C1533A drive returns only one partition size field. This is used to
3159 set the size of partition 1. There is no size field for the default partition.
3160 Michael Schaefer's Sony SDT-7000 returns two descriptors and the second is
3161 used to set the size of partition 1 (this is what the SCSI-3 standard specifies).
3162 The following algorithm is used to accommodate both drives: if the number of
3163 partition size fields is greater than the maximum number of additional partitions
3164 in the mode page, the second field is used. Otherwise the first field is used.
3166 For Seagate DDS drives the page length must be 8 when no partitions is defined
3167 and 10 when 1 partition is defined (information from Eric Lee Green). This is
3168 is acceptable also to some other old drives and enforced if the first partition
3169 size field is used for the first additional partition size.
3171 static int partition_tape(struct scsi_tape *STp, int size)
3173 char *name = tape_name(STp);
3174 int result;
3175 int pgo, psd_cnt, psdo;
3176 unsigned char *bp;
3178 result = read_mode_page(STp, PART_PAGE, 0);
3179 if (result) {
3180 DEBC(printk(ST_DEB_MSG "%s: Can't read partition mode page.\n", name));
3181 return result;
3183 /* The mode page is in the buffer. Let's modify it and write it. */
3184 bp = (STp->buffer)->b_data;
3185 pgo = MODE_HEADER_LENGTH + bp[MH_OFF_BDESCS_LENGTH];
3186 DEBC(printk(ST_DEB_MSG "%s: Partition page length is %d bytes.\n",
3187 name, bp[pgo + MP_OFF_PAGE_LENGTH] + 2));
3189 psd_cnt = (bp[pgo + MP_OFF_PAGE_LENGTH] + 2 - PART_PAGE_FIXED_LENGTH) / 2;
3190 psdo = pgo + PART_PAGE_FIXED_LENGTH;
3191 if (psd_cnt > bp[pgo + PP_OFF_MAX_ADD_PARTS]) {
3192 bp[psdo] = bp[psdo + 1] = 0xff; /* Rest of the tape */
3193 psdo += 2;
3195 memset(bp + psdo, 0, bp[pgo + PP_OFF_NBR_ADD_PARTS] * 2);
3197 DEBC(printk("%s: psd_cnt %d, max.parts %d, nbr_parts %d\n", name,
3198 psd_cnt, bp[pgo + PP_OFF_MAX_ADD_PARTS],
3199 bp[pgo + PP_OFF_NBR_ADD_PARTS]));
3201 if (size <= 0) {
3202 bp[pgo + PP_OFF_NBR_ADD_PARTS] = 0;
3203 if (psd_cnt <= bp[pgo + PP_OFF_MAX_ADD_PARTS])
3204 bp[pgo + MP_OFF_PAGE_LENGTH] = 6;
3205 DEBC(printk(ST_DEB_MSG "%s: Formatting tape with one partition.\n",
3206 name));
3207 } else {
3208 bp[psdo] = (size >> 8) & 0xff;
3209 bp[psdo + 1] = size & 0xff;
3210 bp[pgo + 3] = 1;
3211 if (bp[pgo + MP_OFF_PAGE_LENGTH] < 8)
3212 bp[pgo + MP_OFF_PAGE_LENGTH] = 8;
3213 DEBC(printk(ST_DEB_MSG
3214 "%s: Formatting tape with two partitions (1 = %d MB).\n",
3215 name, size));
3217 bp[pgo + PP_OFF_PART_UNITS] = 0;
3218 bp[pgo + PP_OFF_RESERVED] = 0;
3219 bp[pgo + PP_OFF_FLAGS] = PP_BIT_IDP | PP_MSK_PSUM_MB;
3221 result = write_mode_page(STp, PART_PAGE, 1);
3222 if (result) {
3223 printk(KERN_INFO "%s: Partitioning of tape failed.\n", name);
3224 result = (-EIO);
3227 return result;
3232 /* The ioctl command */
3233 static long st_ioctl(struct file *file, unsigned int cmd_in, unsigned long arg)
3235 int i, cmd_nr, cmd_type, bt;
3236 int retval = 0;
3237 unsigned int blk;
3238 struct scsi_tape *STp = file->private_data;
3239 struct st_modedef *STm;
3240 struct st_partstat *STps;
3241 char *name = tape_name(STp);
3242 void __user *p = (void __user *)arg;
3244 if (mutex_lock_interruptible(&STp->lock))
3245 return -ERESTARTSYS;
3247 DEB(
3248 if (debugging && !STp->in_use) {
3249 printk(ST_DEB_MSG "%s: Incorrect device.\n", name);
3250 retval = (-EIO);
3251 goto out;
3252 } ) /* end DEB */
3254 STm = &(STp->modes[STp->current_mode]);
3255 STps = &(STp->ps[STp->partition]);
3258 * If we are in the middle of error recovery, don't let anyone
3259 * else try and use this device. Also, if error recovery fails, it
3260 * may try and take the device offline, in which case all further
3261 * access to the device is prohibited.
3263 retval = scsi_nonblockable_ioctl(STp->device, cmd_in, p, file);
3264 if (!scsi_block_when_processing_errors(STp->device) || retval != -ENODEV)
3265 goto out;
3266 retval = 0;
3268 cmd_type = _IOC_TYPE(cmd_in);
3269 cmd_nr = _IOC_NR(cmd_in);
3271 if (cmd_type == _IOC_TYPE(MTIOCTOP) && cmd_nr == _IOC_NR(MTIOCTOP)) {
3272 struct mtop mtc;
3274 if (_IOC_SIZE(cmd_in) != sizeof(mtc)) {
3275 retval = (-EINVAL);
3276 goto out;
3279 i = copy_from_user(&mtc, p, sizeof(struct mtop));
3280 if (i) {
3281 retval = (-EFAULT);
3282 goto out;
3285 if (mtc.mt_op == MTSETDRVBUFFER && !capable(CAP_SYS_ADMIN)) {
3286 printk(KERN_WARNING
3287 "%s: MTSETDRVBUFFER only allowed for root.\n", name);
3288 retval = (-EPERM);
3289 goto out;
3291 if (!STm->defined &&
3292 (mtc.mt_op != MTSETDRVBUFFER &&
3293 (mtc.mt_count & MT_ST_OPTIONS) == 0)) {
3294 retval = (-ENXIO);
3295 goto out;
3298 if (!STp->pos_unknown) {
3300 if (STps->eof == ST_FM_HIT) {
3301 if (mtc.mt_op == MTFSF || mtc.mt_op == MTFSFM ||
3302 mtc.mt_op == MTEOM) {
3303 mtc.mt_count -= 1;
3304 if (STps->drv_file >= 0)
3305 STps->drv_file += 1;
3306 } else if (mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM) {
3307 mtc.mt_count += 1;
3308 if (STps->drv_file >= 0)
3309 STps->drv_file += 1;
3313 if (mtc.mt_op == MTSEEK) {
3314 /* Old position must be restored if partition will be
3315 changed */
3316 i = !STp->can_partitions ||
3317 (STp->new_partition != STp->partition);
3318 } else {
3319 i = mtc.mt_op == MTREW || mtc.mt_op == MTOFFL ||
3320 mtc.mt_op == MTRETEN || mtc.mt_op == MTEOM ||
3321 mtc.mt_op == MTLOCK || mtc.mt_op == MTLOAD ||
3322 mtc.mt_op == MTFSF || mtc.mt_op == MTFSFM ||
3323 mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM ||
3324 mtc.mt_op == MTCOMPRESSION;
3326 i = flush_buffer(STp, i);
3327 if (i < 0) {
3328 retval = i;
3329 goto out;
3331 if (STps->rw == ST_WRITING &&
3332 (mtc.mt_op == MTREW || mtc.mt_op == MTOFFL ||
3333 mtc.mt_op == MTSEEK ||
3334 mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM)) {
3335 i = st_int_ioctl(STp, MTWEOF, 1);
3336 if (i < 0) {
3337 retval = i;
3338 goto out;
3340 if (mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM)
3341 mtc.mt_count++;
3342 STps->rw = ST_IDLE;
3345 } else {
3347 * If there was a bus reset, block further access
3348 * to this device. If the user wants to rewind the tape,
3349 * then reset the flag and allow access again.
3351 if (mtc.mt_op != MTREW &&
3352 mtc.mt_op != MTOFFL &&
3353 mtc.mt_op != MTRETEN &&
3354 mtc.mt_op != MTERASE &&
3355 mtc.mt_op != MTSEEK &&
3356 mtc.mt_op != MTEOM) {
3357 retval = (-EIO);
3358 goto out;
3360 reset_state(STp);
3361 /* remove this when the midlevel properly clears was_reset */
3362 STp->device->was_reset = 0;
3365 if (mtc.mt_op != MTNOP && mtc.mt_op != MTSETBLK &&
3366 mtc.mt_op != MTSETDENSITY && mtc.mt_op != MTWSM &&
3367 mtc.mt_op != MTSETDRVBUFFER && mtc.mt_op != MTSETPART)
3368 STps->rw = ST_IDLE; /* Prevent automatic WEOF and fsf */
3370 if (mtc.mt_op == MTOFFL && STp->door_locked != ST_UNLOCKED)
3371 do_door_lock(STp, 0); /* Ignore result! */
3373 if (mtc.mt_op == MTSETDRVBUFFER &&
3374 (mtc.mt_count & MT_ST_OPTIONS) != 0) {
3375 retval = st_set_options(STp, mtc.mt_count);
3376 goto out;
3379 if (mtc.mt_op == MTSETPART) {
3380 if (!STp->can_partitions ||
3381 mtc.mt_count < 0 || mtc.mt_count >= ST_NBR_PARTITIONS) {
3382 retval = (-EINVAL);
3383 goto out;
3385 if (mtc.mt_count >= STp->nbr_partitions &&
3386 (STp->nbr_partitions = nbr_partitions(STp)) < 0) {
3387 retval = (-EIO);
3388 goto out;
3390 if (mtc.mt_count >= STp->nbr_partitions) {
3391 retval = (-EINVAL);
3392 goto out;
3394 STp->new_partition = mtc.mt_count;
3395 retval = 0;
3396 goto out;
3399 if (mtc.mt_op == MTMKPART) {
3400 if (!STp->can_partitions) {
3401 retval = (-EINVAL);
3402 goto out;
3404 if ((i = st_int_ioctl(STp, MTREW, 0)) < 0 ||
3405 (i = partition_tape(STp, mtc.mt_count)) < 0) {
3406 retval = i;
3407 goto out;
3409 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
3410 STp->ps[i].rw = ST_IDLE;
3411 STp->ps[i].at_sm = 0;
3412 STp->ps[i].last_block_valid = 0;
3414 STp->partition = STp->new_partition = 0;
3415 STp->nbr_partitions = 1; /* Bad guess ?-) */
3416 STps->drv_block = STps->drv_file = 0;
3417 retval = 0;
3418 goto out;
3421 if (mtc.mt_op == MTSEEK) {
3422 i = set_location(STp, mtc.mt_count, STp->new_partition, 0);
3423 if (!STp->can_partitions)
3424 STp->ps[0].rw = ST_IDLE;
3425 retval = i;
3426 goto out;
3429 if (mtc.mt_op == MTUNLOAD || mtc.mt_op == MTOFFL) {
3430 retval = do_load_unload(STp, file, 0);
3431 goto out;
3434 if (mtc.mt_op == MTLOAD) {
3435 retval = do_load_unload(STp, file, max(1, mtc.mt_count));
3436 goto out;
3439 if (mtc.mt_op == MTLOCK || mtc.mt_op == MTUNLOCK) {
3440 retval = do_door_lock(STp, (mtc.mt_op == MTLOCK));
3441 goto out;
3444 if (STp->can_partitions && STp->ready == ST_READY &&
3445 (i = switch_partition(STp)) < 0) {
3446 retval = i;
3447 goto out;
3450 if (mtc.mt_op == MTCOMPRESSION)
3451 retval = st_compression(STp, (mtc.mt_count & 1));
3452 else
3453 retval = st_int_ioctl(STp, mtc.mt_op, mtc.mt_count);
3454 goto out;
3456 if (!STm->defined) {
3457 retval = (-ENXIO);
3458 goto out;
3461 if ((i = flush_buffer(STp, 0)) < 0) {
3462 retval = i;
3463 goto out;
3465 if (STp->can_partitions &&
3466 (i = switch_partition(STp)) < 0) {
3467 retval = i;
3468 goto out;
3471 if (cmd_type == _IOC_TYPE(MTIOCGET) && cmd_nr == _IOC_NR(MTIOCGET)) {
3472 struct mtget mt_status;
3474 if (_IOC_SIZE(cmd_in) != sizeof(struct mtget)) {
3475 retval = (-EINVAL);
3476 goto out;
3479 mt_status.mt_type = STp->tape_type;
3480 mt_status.mt_dsreg =
3481 ((STp->block_size << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK) |
3482 ((STp->density << MT_ST_DENSITY_SHIFT) & MT_ST_DENSITY_MASK);
3483 mt_status.mt_blkno = STps->drv_block;
3484 mt_status.mt_fileno = STps->drv_file;
3485 if (STp->block_size != 0) {
3486 if (STps->rw == ST_WRITING)
3487 mt_status.mt_blkno +=
3488 (STp->buffer)->buffer_bytes / STp->block_size;
3489 else if (STps->rw == ST_READING)
3490 mt_status.mt_blkno -=
3491 ((STp->buffer)->buffer_bytes +
3492 STp->block_size - 1) / STp->block_size;
3495 mt_status.mt_gstat = 0;
3496 if (STp->drv_write_prot)
3497 mt_status.mt_gstat |= GMT_WR_PROT(0xffffffff);
3498 if (mt_status.mt_blkno == 0) {
3499 if (mt_status.mt_fileno == 0)
3500 mt_status.mt_gstat |= GMT_BOT(0xffffffff);
3501 else
3502 mt_status.mt_gstat |= GMT_EOF(0xffffffff);
3504 mt_status.mt_erreg = (STp->recover_reg << MT_ST_SOFTERR_SHIFT);
3505 mt_status.mt_resid = STp->partition;
3506 if (STps->eof == ST_EOM_OK || STps->eof == ST_EOM_ERROR)
3507 mt_status.mt_gstat |= GMT_EOT(0xffffffff);
3508 else if (STps->eof >= ST_EOM_OK)
3509 mt_status.mt_gstat |= GMT_EOD(0xffffffff);
3510 if (STp->density == 1)
3511 mt_status.mt_gstat |= GMT_D_800(0xffffffff);
3512 else if (STp->density == 2)
3513 mt_status.mt_gstat |= GMT_D_1600(0xffffffff);
3514 else if (STp->density == 3)
3515 mt_status.mt_gstat |= GMT_D_6250(0xffffffff);
3516 if (STp->ready == ST_READY)
3517 mt_status.mt_gstat |= GMT_ONLINE(0xffffffff);
3518 if (STp->ready == ST_NO_TAPE)
3519 mt_status.mt_gstat |= GMT_DR_OPEN(0xffffffff);
3520 if (STps->at_sm)
3521 mt_status.mt_gstat |= GMT_SM(0xffffffff);
3522 if (STm->do_async_writes ||
3523 (STm->do_buffer_writes && STp->block_size != 0) ||
3524 STp->drv_buffer != 0)
3525 mt_status.mt_gstat |= GMT_IM_REP_EN(0xffffffff);
3526 if (STp->cleaning_req)
3527 mt_status.mt_gstat |= GMT_CLN(0xffffffff);
3529 i = copy_to_user(p, &mt_status, sizeof(struct mtget));
3530 if (i) {
3531 retval = (-EFAULT);
3532 goto out;
3535 STp->recover_reg = 0; /* Clear after read */
3536 retval = 0;
3537 goto out;
3538 } /* End of MTIOCGET */
3539 if (cmd_type == _IOC_TYPE(MTIOCPOS) && cmd_nr == _IOC_NR(MTIOCPOS)) {
3540 struct mtpos mt_pos;
3541 if (_IOC_SIZE(cmd_in) != sizeof(struct mtpos)) {
3542 retval = (-EINVAL);
3543 goto out;
3545 if ((i = get_location(STp, &blk, &bt, 0)) < 0) {
3546 retval = i;
3547 goto out;
3549 mt_pos.mt_blkno = blk;
3550 i = copy_to_user(p, &mt_pos, sizeof(struct mtpos));
3551 if (i)
3552 retval = (-EFAULT);
3553 goto out;
3555 mutex_unlock(&STp->lock);
3556 switch (cmd_in) {
3557 case SCSI_IOCTL_GET_IDLUN:
3558 case SCSI_IOCTL_GET_BUS_NUMBER:
3559 break;
3560 default:
3561 if ((cmd_in == SG_IO ||
3562 cmd_in == SCSI_IOCTL_SEND_COMMAND ||
3563 cmd_in == CDROM_SEND_PACKET) &&
3564 !capable(CAP_SYS_RAWIO))
3565 i = -EPERM;
3566 else
3567 i = scsi_cmd_ioctl(file, STp->disk->queue,
3568 STp->disk, cmd_in, p);
3569 if (i != -ENOTTY)
3570 return i;
3571 break;
3573 retval = scsi_ioctl(STp->device, cmd_in, p);
3574 if (!retval && cmd_in == SCSI_IOCTL_STOP_UNIT) { /* unload */
3575 STp->rew_at_close = 0;
3576 STp->ready = ST_NO_TAPE;
3578 return retval;
3580 out:
3581 mutex_unlock(&STp->lock);
3582 return retval;
3585 #ifdef CONFIG_COMPAT
3586 static long st_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3588 struct scsi_tape *STp = file->private_data;
3589 struct scsi_device *sdev = STp->device;
3590 int ret = -ENOIOCTLCMD;
3591 if (sdev->host->hostt->compat_ioctl) {
3593 ret = sdev->host->hostt->compat_ioctl(sdev, cmd, (void __user *)arg);
3596 return ret;
3598 #endif
3602 /* Try to allocate a new tape buffer. Calling function must not hold
3603 dev_arr_lock. */
3604 static struct st_buffer *
3605 new_tape_buffer(int from_initialization, int need_dma, int max_sg)
3607 int i, got = 0;
3608 gfp_t priority;
3609 struct st_buffer *tb;
3611 if (from_initialization)
3612 priority = GFP_ATOMIC;
3613 else
3614 priority = GFP_KERNEL;
3616 i = sizeof(struct st_buffer) + (max_sg - 1) * sizeof(struct scatterlist) +
3617 max_sg * sizeof(struct st_buf_fragment);
3618 tb = kzalloc(i, priority);
3619 if (!tb) {
3620 printk(KERN_NOTICE "st: Can't allocate new tape buffer.\n");
3621 return NULL;
3623 tb->frp_segs = tb->orig_frp_segs = 0;
3624 tb->use_sg = max_sg;
3625 tb->frp = (struct st_buf_fragment *)(&(tb->sg[0]) + max_sg);
3627 tb->dma = need_dma;
3628 tb->buffer_size = got;
3629 sg_init_table(tb->sg, max_sg);
3631 return tb;
3635 /* Try to allocate enough space in the tape buffer */
3636 static int enlarge_buffer(struct st_buffer * STbuffer, int new_size, int need_dma)
3638 int segs, nbr, max_segs, b_size, order, got;
3639 gfp_t priority;
3641 if (new_size <= STbuffer->buffer_size)
3642 return 1;
3644 if (STbuffer->buffer_size <= PAGE_SIZE)
3645 normalize_buffer(STbuffer); /* Avoid extra segment */
3647 max_segs = STbuffer->use_sg;
3648 nbr = max_segs - STbuffer->frp_segs;
3649 if (nbr <= 0)
3650 return 0;
3652 priority = GFP_KERNEL | __GFP_NOWARN;
3653 if (need_dma)
3654 priority |= GFP_DMA;
3655 for (b_size = PAGE_SIZE, order=0; order <= 6 &&
3656 b_size < new_size - STbuffer->buffer_size;
3657 order++, b_size *= 2)
3658 ; /* empty */
3660 for (segs = STbuffer->frp_segs, got = STbuffer->buffer_size;
3661 segs < max_segs && got < new_size;) {
3662 STbuffer->frp[segs].page = alloc_pages(priority, order);
3663 if (STbuffer->frp[segs].page == NULL) {
3664 if (new_size - got <= (max_segs - segs) * b_size / 2) {
3665 b_size /= 2; /* Large enough for the rest of the buffers */
3666 order--;
3667 continue;
3669 DEB(STbuffer->buffer_size = got);
3670 normalize_buffer(STbuffer);
3671 return 0;
3673 STbuffer->frp[segs].length = b_size;
3674 STbuffer->frp_segs += 1;
3675 got += b_size;
3676 STbuffer->buffer_size = got;
3677 segs++;
3679 STbuffer->b_data = page_address(STbuffer->frp[0].page);
3681 return 1;
3685 /* Release the extra buffer */
3686 static void normalize_buffer(struct st_buffer * STbuffer)
3688 int i, order;
3690 for (i = STbuffer->orig_frp_segs; i < STbuffer->frp_segs; i++) {
3691 order = get_order(STbuffer->frp[i].length);
3692 __free_pages(STbuffer->frp[i].page, order);
3693 STbuffer->buffer_size -= STbuffer->frp[i].length;
3695 STbuffer->frp_segs = STbuffer->orig_frp_segs;
3696 STbuffer->frp_sg_current = 0;
3697 STbuffer->sg_segs = 0;
3701 /* Move data from the user buffer to the tape buffer. Returns zero (success) or
3702 negative error code. */
3703 static int append_to_buffer(const char __user *ubp, struct st_buffer * st_bp, int do_count)
3705 int i, cnt, res, offset;
3707 for (i = 0, offset = st_bp->buffer_bytes;
3708 i < st_bp->frp_segs && offset >= st_bp->frp[i].length; i++)
3709 offset -= st_bp->frp[i].length;
3710 if (i == st_bp->frp_segs) { /* Should never happen */
3711 printk(KERN_WARNING "st: append_to_buffer offset overflow.\n");
3712 return (-EIO);
3714 for (; i < st_bp->frp_segs && do_count > 0; i++) {
3715 cnt = st_bp->frp[i].length - offset < do_count ?
3716 st_bp->frp[i].length - offset : do_count;
3717 res = copy_from_user(page_address(st_bp->frp[i].page) + offset, ubp, cnt);
3718 if (res)
3719 return (-EFAULT);
3720 do_count -= cnt;
3721 st_bp->buffer_bytes += cnt;
3722 ubp += cnt;
3723 offset = 0;
3725 if (do_count) /* Should never happen */
3726 return (-EIO);
3728 return 0;
3732 /* Move data from the tape buffer to the user buffer. Returns zero (success) or
3733 negative error code. */
3734 static int from_buffer(struct st_buffer * st_bp, char __user *ubp, int do_count)
3736 int i, cnt, res, offset;
3738 for (i = 0, offset = st_bp->read_pointer;
3739 i < st_bp->frp_segs && offset >= st_bp->frp[i].length; i++)
3740 offset -= st_bp->frp[i].length;
3741 if (i == st_bp->frp_segs) { /* Should never happen */
3742 printk(KERN_WARNING "st: from_buffer offset overflow.\n");
3743 return (-EIO);
3745 for (; i < st_bp->frp_segs && do_count > 0; i++) {
3746 cnt = st_bp->frp[i].length - offset < do_count ?
3747 st_bp->frp[i].length - offset : do_count;
3748 res = copy_to_user(ubp, page_address(st_bp->frp[i].page) + offset, cnt);
3749 if (res)
3750 return (-EFAULT);
3751 do_count -= cnt;
3752 st_bp->buffer_bytes -= cnt;
3753 st_bp->read_pointer += cnt;
3754 ubp += cnt;
3755 offset = 0;
3757 if (do_count) /* Should never happen */
3758 return (-EIO);
3760 return 0;
3764 /* Move data towards start of buffer */
3765 static void move_buffer_data(struct st_buffer * st_bp, int offset)
3767 int src_seg, dst_seg, src_offset = 0, dst_offset;
3768 int count, total;
3770 if (offset == 0)
3771 return;
3773 total=st_bp->buffer_bytes - offset;
3774 for (src_seg=0; src_seg < st_bp->frp_segs; src_seg++) {
3775 src_offset = offset;
3776 if (src_offset < st_bp->frp[src_seg].length)
3777 break;
3778 offset -= st_bp->frp[src_seg].length;
3781 st_bp->buffer_bytes = st_bp->read_pointer = total;
3782 for (dst_seg=dst_offset=0; total > 0; ) {
3783 count = min(st_bp->frp[dst_seg].length - dst_offset,
3784 st_bp->frp[src_seg].length - src_offset);
3785 memmove(page_address(st_bp->frp[dst_seg].page) + dst_offset,
3786 page_address(st_bp->frp[src_seg].page) + src_offset, count);
3787 src_offset += count;
3788 if (src_offset >= st_bp->frp[src_seg].length) {
3789 src_seg++;
3790 src_offset = 0;
3792 dst_offset += count;
3793 if (dst_offset >= st_bp->frp[dst_seg].length) {
3794 dst_seg++;
3795 dst_offset = 0;
3797 total -= count;
3802 /* Fill the s/g list up to the length required for this transfer */
3803 static void buf_to_sg(struct st_buffer *STbp, unsigned int length)
3805 int i;
3806 unsigned int count;
3807 struct scatterlist *sg;
3808 struct st_buf_fragment *frp;
3810 if (length == STbp->frp_sg_current)
3811 return; /* work already done */
3813 sg = &(STbp->sg[0]);
3814 frp = STbp->frp;
3815 for (i=count=0; count < length; i++) {
3816 if (length - count > frp[i].length)
3817 sg_set_page(&sg[i], frp[i].page, frp[i].length, 0);
3818 else
3819 sg_set_page(&sg[i], frp[i].page, length - count, 0);
3820 count += sg[i].length;
3822 STbp->sg_segs = i;
3823 STbp->frp_sg_current = length;
3827 /* Validate the options from command line or module parameters */
3828 static void validate_options(void)
3830 if (buffer_kbs > 0)
3831 st_fixed_buffer_size = buffer_kbs * ST_KILOBYTE;
3832 if (max_sg_segs >= ST_FIRST_SG)
3833 st_max_sg_segs = max_sg_segs;
3836 #ifndef MODULE
3837 /* Set the boot options. Syntax is defined in Documenation/scsi/st.txt.
3839 static int __init st_setup(char *str)
3841 int i, len, ints[5];
3842 char *stp;
3844 stp = get_options(str, ARRAY_SIZE(ints), ints);
3846 if (ints[0] > 0) {
3847 for (i = 0; i < ints[0] && i < ARRAY_SIZE(parms); i++)
3848 if (parms[i].val)
3849 *parms[i].val = ints[i + 1];
3850 } else {
3851 while (stp != NULL) {
3852 for (i = 0; i < ARRAY_SIZE(parms); i++) {
3853 len = strlen(parms[i].name);
3854 if (!strncmp(stp, parms[i].name, len) &&
3855 (*(stp + len) == ':' || *(stp + len) == '=')) {
3856 if (parms[i].val)
3857 *parms[i].val =
3858 simple_strtoul(stp + len + 1, NULL, 0);
3859 else
3860 printk(KERN_WARNING "st: Obsolete parameter %s\n",
3861 parms[i].name);
3862 break;
3865 if (i >= ARRAY_SIZE(parms))
3866 printk(KERN_WARNING "st: invalid parameter in '%s'\n",
3867 stp);
3868 stp = strchr(stp, ',');
3869 if (stp)
3870 stp++;
3874 validate_options();
3876 return 1;
3879 __setup("st=", st_setup);
3881 #endif
3883 static const struct file_operations st_fops =
3885 .owner = THIS_MODULE,
3886 .read = st_read,
3887 .write = st_write,
3888 .unlocked_ioctl = st_ioctl,
3889 #ifdef CONFIG_COMPAT
3890 .compat_ioctl = st_compat_ioctl,
3891 #endif
3892 .open = st_open,
3893 .flush = st_flush,
3894 .release = st_release,
3897 static int st_probe(struct device *dev)
3899 struct scsi_device *SDp = to_scsi_device(dev);
3900 struct gendisk *disk = NULL;
3901 struct cdev *cdev = NULL;
3902 struct scsi_tape *tpnt = NULL;
3903 struct st_modedef *STm;
3904 struct st_partstat *STps;
3905 struct st_buffer *buffer;
3906 int i, j, mode, dev_num, error;
3907 char *stp;
3909 if (SDp->type != TYPE_TAPE)
3910 return -ENODEV;
3911 if ((stp = st_incompatible(SDp))) {
3912 sdev_printk(KERN_INFO, SDp, "Found incompatible tape\n");
3913 printk(KERN_INFO "st: The suggested driver is %s.\n", stp);
3914 return -ENODEV;
3917 i = min(SDp->request_queue->max_hw_segments,
3918 SDp->request_queue->max_phys_segments);
3919 if (st_max_sg_segs < i)
3920 i = st_max_sg_segs;
3921 buffer = new_tape_buffer(1, (SDp->host)->unchecked_isa_dma, i);
3922 if (buffer == NULL) {
3923 printk(KERN_ERR
3924 "st: Can't allocate new tape buffer. Device not attached.\n");
3925 goto out;
3928 disk = alloc_disk(1);
3929 if (!disk) {
3930 printk(KERN_ERR "st: out of memory. Device not attached.\n");
3931 goto out_buffer_free;
3934 write_lock(&st_dev_arr_lock);
3935 if (st_nr_dev >= st_dev_max) {
3936 struct scsi_tape **tmp_da;
3937 int tmp_dev_max;
3939 tmp_dev_max = max(st_nr_dev * 2, 8);
3940 if (tmp_dev_max > ST_MAX_TAPES)
3941 tmp_dev_max = ST_MAX_TAPES;
3942 if (tmp_dev_max <= st_nr_dev) {
3943 write_unlock(&st_dev_arr_lock);
3944 printk(KERN_ERR "st: Too many tape devices (max. %d).\n",
3945 ST_MAX_TAPES);
3946 goto out_put_disk;
3949 tmp_da = kzalloc(tmp_dev_max * sizeof(struct scsi_tape *), GFP_ATOMIC);
3950 if (tmp_da == NULL) {
3951 write_unlock(&st_dev_arr_lock);
3952 printk(KERN_ERR "st: Can't extend device array.\n");
3953 goto out_put_disk;
3956 if (scsi_tapes != NULL) {
3957 memcpy(tmp_da, scsi_tapes,
3958 st_dev_max * sizeof(struct scsi_tape *));
3959 kfree(scsi_tapes);
3961 scsi_tapes = tmp_da;
3963 st_dev_max = tmp_dev_max;
3966 for (i = 0; i < st_dev_max; i++)
3967 if (scsi_tapes[i] == NULL)
3968 break;
3969 if (i >= st_dev_max)
3970 panic("scsi_devices corrupt (st)");
3972 tpnt = kzalloc(sizeof(struct scsi_tape), GFP_ATOMIC);
3973 if (tpnt == NULL) {
3974 write_unlock(&st_dev_arr_lock);
3975 printk(KERN_ERR "st: Can't allocate device descriptor.\n");
3976 goto out_put_disk;
3978 kref_init(&tpnt->kref);
3979 tpnt->disk = disk;
3980 sprintf(disk->disk_name, "st%d", i);
3981 disk->private_data = &tpnt->driver;
3982 disk->queue = SDp->request_queue;
3983 tpnt->driver = &st_template;
3984 scsi_tapes[i] = tpnt;
3985 dev_num = i;
3987 tpnt->device = SDp;
3988 if (SDp->scsi_level <= 2)
3989 tpnt->tape_type = MT_ISSCSI1;
3990 else
3991 tpnt->tape_type = MT_ISSCSI2;
3993 tpnt->buffer = buffer;
3994 tpnt->buffer->last_SRpnt = NULL;
3996 tpnt->inited = 0;
3997 tpnt->dirty = 0;
3998 tpnt->in_use = 0;
3999 tpnt->drv_buffer = 1; /* Try buffering if no mode sense */
4000 tpnt->restr_dma = (SDp->host)->unchecked_isa_dma;
4001 tpnt->use_pf = (SDp->scsi_level >= SCSI_2);
4002 tpnt->density = 0;
4003 tpnt->do_auto_lock = ST_AUTO_LOCK;
4004 tpnt->can_bsr = (SDp->scsi_level > 2 ? 1 : ST_IN_FILE_POS); /* BSR mandatory in SCSI3 */
4005 tpnt->can_partitions = 0;
4006 tpnt->two_fm = ST_TWO_FM;
4007 tpnt->fast_mteom = ST_FAST_MTEOM;
4008 tpnt->scsi2_logical = ST_SCSI2LOGICAL;
4009 tpnt->immediate = ST_NOWAIT;
4010 tpnt->default_drvbuffer = 0xff; /* No forced buffering */
4011 tpnt->partition = 0;
4012 tpnt->new_partition = 0;
4013 tpnt->nbr_partitions = 0;
4014 tpnt->device->timeout = ST_TIMEOUT;
4015 tpnt->long_timeout = ST_LONG_TIMEOUT;
4016 tpnt->try_dio = try_direct_io && !SDp->host->unchecked_isa_dma;
4018 for (i = 0; i < ST_NBR_MODES; i++) {
4019 STm = &(tpnt->modes[i]);
4020 STm->defined = 0;
4021 STm->sysv = ST_SYSV;
4022 STm->defaults_for_writes = 0;
4023 STm->do_async_writes = ST_ASYNC_WRITES;
4024 STm->do_buffer_writes = ST_BUFFER_WRITES;
4025 STm->do_read_ahead = ST_READ_AHEAD;
4026 STm->default_compression = ST_DONT_TOUCH;
4027 STm->default_blksize = (-1); /* No forced size */
4028 STm->default_density = (-1); /* No forced density */
4031 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
4032 STps = &(tpnt->ps[i]);
4033 STps->rw = ST_IDLE;
4034 STps->eof = ST_NOEOF;
4035 STps->at_sm = 0;
4036 STps->last_block_valid = 0;
4037 STps->drv_block = (-1);
4038 STps->drv_file = (-1);
4041 tpnt->current_mode = 0;
4042 tpnt->modes[0].defined = 1;
4044 tpnt->density_changed = tpnt->compression_changed =
4045 tpnt->blksize_changed = 0;
4046 mutex_init(&tpnt->lock);
4048 st_nr_dev++;
4049 write_unlock(&st_dev_arr_lock);
4051 for (mode = 0; mode < ST_NBR_MODES; ++mode) {
4052 STm = &(tpnt->modes[mode]);
4053 for (j=0; j < 2; j++) {
4054 cdev = cdev_alloc();
4055 if (!cdev) {
4056 printk(KERN_ERR
4057 "st%d: out of memory. Device not attached.\n",
4058 dev_num);
4059 goto out_free_tape;
4061 cdev->owner = THIS_MODULE;
4062 cdev->ops = &st_fops;
4064 error = cdev_add(cdev,
4065 MKDEV(SCSI_TAPE_MAJOR, TAPE_MINOR(dev_num, mode, j)),
4067 if (error) {
4068 printk(KERN_ERR "st%d: Can't add %s-rewind mode %d\n",
4069 dev_num, j ? "non" : "auto", mode);
4070 printk(KERN_ERR "st%d: Device not attached.\n", dev_num);
4071 goto out_free_tape;
4073 STm->cdevs[j] = cdev;
4076 error = do_create_class_files(tpnt, dev_num, mode);
4077 if (error)
4078 goto out_free_tape;
4081 sdev_printk(KERN_NOTICE, SDp,
4082 "Attached scsi tape %s\n", tape_name(tpnt));
4083 sdev_printk(KERN_INFO, SDp, "%s: try direct i/o: %s (alignment %d B)\n",
4084 tape_name(tpnt), tpnt->try_dio ? "yes" : "no",
4085 queue_dma_alignment(SDp->request_queue) + 1);
4087 return 0;
4089 out_free_tape:
4090 for (mode=0; mode < ST_NBR_MODES; mode++) {
4091 STm = &(tpnt->modes[mode]);
4092 sysfs_remove_link(&tpnt->device->sdev_gendev.kobj,
4093 "tape");
4094 for (j=0; j < 2; j++) {
4095 if (STm->cdevs[j]) {
4096 if (cdev == STm->cdevs[j])
4097 cdev = NULL;
4098 class_device_destroy(st_sysfs_class,
4099 MKDEV(SCSI_TAPE_MAJOR,
4100 TAPE_MINOR(i, mode, j)));
4101 cdev_del(STm->cdevs[j]);
4105 if (cdev)
4106 cdev_del(cdev);
4107 write_lock(&st_dev_arr_lock);
4108 scsi_tapes[dev_num] = NULL;
4109 st_nr_dev--;
4110 write_unlock(&st_dev_arr_lock);
4111 out_put_disk:
4112 put_disk(disk);
4113 kfree(tpnt);
4114 out_buffer_free:
4115 kfree(buffer);
4116 out:
4117 return -ENODEV;
4121 static int st_remove(struct device *dev)
4123 struct scsi_device *SDp = to_scsi_device(dev);
4124 struct scsi_tape *tpnt;
4125 int i, j, mode;
4127 write_lock(&st_dev_arr_lock);
4128 for (i = 0; i < st_dev_max; i++) {
4129 tpnt = scsi_tapes[i];
4130 if (tpnt != NULL && tpnt->device == SDp) {
4131 scsi_tapes[i] = NULL;
4132 st_nr_dev--;
4133 write_unlock(&st_dev_arr_lock);
4134 sysfs_remove_link(&tpnt->device->sdev_gendev.kobj,
4135 "tape");
4136 for (mode = 0; mode < ST_NBR_MODES; ++mode) {
4137 for (j=0; j < 2; j++) {
4138 class_device_destroy(st_sysfs_class,
4139 MKDEV(SCSI_TAPE_MAJOR,
4140 TAPE_MINOR(i, mode, j)));
4141 cdev_del(tpnt->modes[mode].cdevs[j]);
4142 tpnt->modes[mode].cdevs[j] = NULL;
4146 mutex_lock(&st_ref_mutex);
4147 kref_put(&tpnt->kref, scsi_tape_release);
4148 mutex_unlock(&st_ref_mutex);
4149 return 0;
4153 write_unlock(&st_dev_arr_lock);
4154 return 0;
4158 * scsi_tape_release - Called to free the Scsi_Tape structure
4159 * @kref: pointer to embedded kref
4161 * st_ref_mutex must be held entering this routine. Because it is
4162 * called on last put, you should always use the scsi_tape_get()
4163 * scsi_tape_put() helpers which manipulate the semaphore directly
4164 * and never do a direct kref_put().
4166 static void scsi_tape_release(struct kref *kref)
4168 struct scsi_tape *tpnt = to_scsi_tape(kref);
4169 struct gendisk *disk = tpnt->disk;
4171 tpnt->device = NULL;
4173 if (tpnt->buffer) {
4174 tpnt->buffer->orig_frp_segs = 0;
4175 normalize_buffer(tpnt->buffer);
4176 kfree(tpnt->buffer);
4179 disk->private_data = NULL;
4180 put_disk(disk);
4181 kfree(tpnt);
4182 return;
4185 static int __init init_st(void)
4187 int err;
4189 validate_options();
4191 printk(KERN_INFO "st: Version %s, fixed bufsize %d, s/g segs %d\n",
4192 verstr, st_fixed_buffer_size, st_max_sg_segs);
4194 st_sysfs_class = class_create(THIS_MODULE, "scsi_tape");
4195 if (IS_ERR(st_sysfs_class)) {
4196 printk(KERN_ERR "Unable create sysfs class for SCSI tapes\n");
4197 return PTR_ERR(st_sysfs_class);
4200 err = register_chrdev_region(MKDEV(SCSI_TAPE_MAJOR, 0),
4201 ST_MAX_TAPE_ENTRIES, "st");
4202 if (err) {
4203 printk(KERN_ERR "Unable to get major %d for SCSI tapes\n",
4204 SCSI_TAPE_MAJOR);
4205 goto err_class;
4208 err = scsi_register_driver(&st_template.gendrv);
4209 if (err)
4210 goto err_chrdev;
4212 err = do_create_sysfs_files();
4213 if (err)
4214 goto err_scsidrv;
4216 return 0;
4218 err_scsidrv:
4219 scsi_unregister_driver(&st_template.gendrv);
4220 err_chrdev:
4221 unregister_chrdev_region(MKDEV(SCSI_TAPE_MAJOR, 0),
4222 ST_MAX_TAPE_ENTRIES);
4223 err_class:
4224 class_destroy(st_sysfs_class);
4225 return err;
4228 static void __exit exit_st(void)
4230 do_remove_sysfs_files();
4231 scsi_unregister_driver(&st_template.gendrv);
4232 unregister_chrdev_region(MKDEV(SCSI_TAPE_MAJOR, 0),
4233 ST_MAX_TAPE_ENTRIES);
4234 class_destroy(st_sysfs_class);
4235 kfree(scsi_tapes);
4236 printk(KERN_INFO "st: Unloaded.\n");
4239 module_init(init_st);
4240 module_exit(exit_st);
4243 /* The sysfs driver interface. Read-only at the moment */
4244 static ssize_t st_try_direct_io_show(struct device_driver *ddp, char *buf)
4246 return snprintf(buf, PAGE_SIZE, "%d\n", try_direct_io);
4248 static DRIVER_ATTR(try_direct_io, S_IRUGO, st_try_direct_io_show, NULL);
4250 static ssize_t st_fixed_buffer_size_show(struct device_driver *ddp, char *buf)
4252 return snprintf(buf, PAGE_SIZE, "%d\n", st_fixed_buffer_size);
4254 static DRIVER_ATTR(fixed_buffer_size, S_IRUGO, st_fixed_buffer_size_show, NULL);
4256 static ssize_t st_max_sg_segs_show(struct device_driver *ddp, char *buf)
4258 return snprintf(buf, PAGE_SIZE, "%d\n", st_max_sg_segs);
4260 static DRIVER_ATTR(max_sg_segs, S_IRUGO, st_max_sg_segs_show, NULL);
4262 static ssize_t st_version_show(struct device_driver *ddd, char *buf)
4264 return snprintf(buf, PAGE_SIZE, "[%s]\n", verstr);
4266 static DRIVER_ATTR(version, S_IRUGO, st_version_show, NULL);
4268 static int do_create_sysfs_files(void)
4270 struct device_driver *sysfs = &st_template.gendrv;
4271 int err;
4273 err = driver_create_file(sysfs, &driver_attr_try_direct_io);
4274 if (err)
4275 return err;
4276 err = driver_create_file(sysfs, &driver_attr_fixed_buffer_size);
4277 if (err)
4278 goto err_try_direct_io;
4279 err = driver_create_file(sysfs, &driver_attr_max_sg_segs);
4280 if (err)
4281 goto err_attr_fixed_buf;
4282 err = driver_create_file(sysfs, &driver_attr_version);
4283 if (err)
4284 goto err_attr_max_sg;
4286 return 0;
4288 err_attr_max_sg:
4289 driver_remove_file(sysfs, &driver_attr_max_sg_segs);
4290 err_attr_fixed_buf:
4291 driver_remove_file(sysfs, &driver_attr_fixed_buffer_size);
4292 err_try_direct_io:
4293 driver_remove_file(sysfs, &driver_attr_try_direct_io);
4294 return err;
4297 static void do_remove_sysfs_files(void)
4299 struct device_driver *sysfs = &st_template.gendrv;
4301 driver_remove_file(sysfs, &driver_attr_version);
4302 driver_remove_file(sysfs, &driver_attr_max_sg_segs);
4303 driver_remove_file(sysfs, &driver_attr_fixed_buffer_size);
4304 driver_remove_file(sysfs, &driver_attr_try_direct_io);
4308 /* The sysfs simple class interface */
4309 static ssize_t st_defined_show(struct class_device *class_dev, char *buf)
4311 struct st_modedef *STm = (struct st_modedef *)class_get_devdata(class_dev);
4312 ssize_t l = 0;
4314 l = snprintf(buf, PAGE_SIZE, "%d\n", STm->defined);
4315 return l;
4318 CLASS_DEVICE_ATTR(defined, S_IRUGO, st_defined_show, NULL);
4320 static ssize_t st_defblk_show(struct class_device *class_dev, char *buf)
4322 struct st_modedef *STm = (struct st_modedef *)class_get_devdata(class_dev);
4323 ssize_t l = 0;
4325 l = snprintf(buf, PAGE_SIZE, "%d\n", STm->default_blksize);
4326 return l;
4329 CLASS_DEVICE_ATTR(default_blksize, S_IRUGO, st_defblk_show, NULL);
4331 static ssize_t st_defdensity_show(struct class_device *class_dev, char *buf)
4333 struct st_modedef *STm = (struct st_modedef *)class_get_devdata(class_dev);
4334 ssize_t l = 0;
4335 char *fmt;
4337 fmt = STm->default_density >= 0 ? "0x%02x\n" : "%d\n";
4338 l = snprintf(buf, PAGE_SIZE, fmt, STm->default_density);
4339 return l;
4342 CLASS_DEVICE_ATTR(default_density, S_IRUGO, st_defdensity_show, NULL);
4344 static ssize_t st_defcompression_show(struct class_device *class_dev, char *buf)
4346 struct st_modedef *STm = (struct st_modedef *)class_get_devdata(class_dev);
4347 ssize_t l = 0;
4349 l = snprintf(buf, PAGE_SIZE, "%d\n", STm->default_compression - 1);
4350 return l;
4353 CLASS_DEVICE_ATTR(default_compression, S_IRUGO, st_defcompression_show, NULL);
4355 static int do_create_class_files(struct scsi_tape *STp, int dev_num, int mode)
4357 int i, rew, error;
4358 char name[10];
4359 struct class_device *st_class_member;
4361 for (rew=0; rew < 2; rew++) {
4362 /* Make sure that the minor numbers corresponding to the four
4363 first modes always get the same names */
4364 i = mode << (4 - ST_NBR_MODE_BITS);
4365 snprintf(name, 10, "%s%s%s", rew ? "n" : "",
4366 STp->disk->disk_name, st_formats[i]);
4367 st_class_member =
4368 class_device_create(st_sysfs_class, NULL,
4369 MKDEV(SCSI_TAPE_MAJOR,
4370 TAPE_MINOR(dev_num, mode, rew)),
4371 &STp->device->sdev_gendev, "%s", name);
4372 if (IS_ERR(st_class_member)) {
4373 printk(KERN_WARNING "st%d: class_device_create failed\n",
4374 dev_num);
4375 error = PTR_ERR(st_class_member);
4376 goto out;
4378 class_set_devdata(st_class_member, &STp->modes[mode]);
4380 error = class_device_create_file(st_class_member,
4381 &class_device_attr_defined);
4382 if (error) goto out;
4383 error = class_device_create_file(st_class_member,
4384 &class_device_attr_default_blksize);
4385 if (error) goto out;
4386 error = class_device_create_file(st_class_member,
4387 &class_device_attr_default_density);
4388 if (error) goto out;
4389 error = class_device_create_file(st_class_member,
4390 &class_device_attr_default_compression);
4391 if (error) goto out;
4393 if (mode == 0 && rew == 0) {
4394 error = sysfs_create_link(&STp->device->sdev_gendev.kobj,
4395 &st_class_member->kobj,
4396 "tape");
4397 if (error) {
4398 printk(KERN_ERR
4399 "st%d: Can't create sysfs link from SCSI device.\n",
4400 dev_num);
4401 goto out;
4406 return 0;
4408 out:
4409 return error;
4412 /* The following functions may be useful for a larger audience. */
4413 static int sgl_map_user_pages(struct scatterlist *sgl, const unsigned int max_pages,
4414 unsigned long uaddr, size_t count, int rw)
4416 unsigned long end = (uaddr + count + PAGE_SIZE - 1) >> PAGE_SHIFT;
4417 unsigned long start = uaddr >> PAGE_SHIFT;
4418 const int nr_pages = end - start;
4419 int res, i, j;
4420 struct page **pages;
4422 /* User attempted Overflow! */
4423 if ((uaddr + count) < uaddr)
4424 return -EINVAL;
4426 /* Too big */
4427 if (nr_pages > max_pages)
4428 return -ENOMEM;
4430 /* Hmm? */
4431 if (count == 0)
4432 return 0;
4434 if ((pages = kmalloc(max_pages * sizeof(*pages), GFP_KERNEL)) == NULL)
4435 return -ENOMEM;
4437 /* Try to fault in all of the necessary pages */
4438 down_read(&current->mm->mmap_sem);
4439 /* rw==READ means read from drive, write into memory area */
4440 res = get_user_pages(
4441 current,
4442 current->mm,
4443 uaddr,
4444 nr_pages,
4445 rw == READ,
4446 0, /* don't force */
4447 pages,
4448 NULL);
4449 up_read(&current->mm->mmap_sem);
4451 /* Errors and no page mapped should return here */
4452 if (res < nr_pages)
4453 goto out_unmap;
4455 for (i=0; i < nr_pages; i++) {
4456 /* FIXME: flush superflous for rw==READ,
4457 * probably wrong function for rw==WRITE
4459 flush_dcache_page(pages[i]);
4462 /* Populate the scatter/gather list */
4463 sg_set_page(&sgl[0], pages[0], 0, uaddr & ~PAGE_MASK);
4464 if (nr_pages > 1) {
4465 sgl[0].length = PAGE_SIZE - sgl[0].offset;
4466 count -= sgl[0].length;
4467 for (i=1; i < nr_pages ; i++) {
4468 sg_set_page(&sgl[i], pages[i],
4469 count < PAGE_SIZE ? count : PAGE_SIZE, 0);;
4470 count -= PAGE_SIZE;
4473 else {
4474 sgl[0].length = count;
4477 kfree(pages);
4478 return nr_pages;
4480 out_unmap:
4481 if (res > 0) {
4482 for (j=0; j < res; j++)
4483 page_cache_release(pages[j]);
4484 res = 0;
4486 kfree(pages);
4487 return res;
4491 /* And unmap them... */
4492 static int sgl_unmap_user_pages(struct scatterlist *sgl, const unsigned int nr_pages,
4493 int dirtied)
4495 int i;
4497 for (i=0; i < nr_pages; i++) {
4498 struct page *page = sg_page(&sgl[i]);
4500 if (dirtied)
4501 SetPageDirty(page);
4502 /* FIXME: cache flush missing for rw==READ
4503 * FIXME: call the correct reference counting function
4505 page_cache_release(page);
4508 return 0;