[PATCH] x86_64: Don't try to synchronize the TSC over CPUs on Intel CPUs at boot.
[linux-2.6/linux-loongson.git] / drivers / scsi / st.c
blobc4aade8f5345a34f949434df43f20fa847b139d1
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 - 2005 Kai Makisara
13 email Kai.Makisara@kolumbus.fi
15 Some small formal changes - aeb, 950809
17 Last modified: 18-JAN-1998 Richard Gooch <rgooch@atnf.csiro.au> Devfs support
20 static const char *verstr = "20050830";
22 #include <linux/module.h>
24 #include <linux/fs.h>
25 #include <linux/kernel.h>
26 #include <linux/sched.h>
27 #include <linux/mm.h>
28 #include <linux/init.h>
29 #include <linux/string.h>
30 #include <linux/errno.h>
31 #include <linux/mtio.h>
32 #include <linux/cdrom.h>
33 #include <linux/ioctl.h>
34 #include <linux/fcntl.h>
35 #include <linux/spinlock.h>
36 #include <linux/blkdev.h>
37 #include <linux/moduleparam.h>
38 #include <linux/devfs_fs_kernel.h>
39 #include <linux/cdev.h>
40 #include <linux/delay.h>
42 #include <asm/uaccess.h>
43 #include <asm/dma.h>
44 #include <asm/system.h>
46 #include <scsi/scsi.h>
47 #include <scsi/scsi_dbg.h>
48 #include <scsi/scsi_device.h>
49 #include <scsi/scsi_driver.h>
50 #include <scsi/scsi_eh.h>
51 #include <scsi/scsi_host.h>
52 #include <scsi/scsi_ioctl.h>
53 #include <scsi/sg.h>
56 /* The driver prints some debugging information on the console if DEBUG
57 is defined and non-zero. */
58 #define DEBUG 0
60 #if DEBUG
61 /* The message level for the debug messages is currently set to KERN_NOTICE
62 so that people can easily see the messages. Later when the debugging messages
63 in the drivers are more widely classified, this may be changed to KERN_DEBUG. */
64 #define ST_DEB_MSG KERN_NOTICE
65 #define DEB(a) a
66 #define DEBC(a) if (debugging) { a ; }
67 #else
68 #define DEB(a)
69 #define DEBC(a)
70 #endif
72 #define ST_KILOBYTE 1024
74 #include "st_options.h"
75 #include "st.h"
77 static int buffer_kbs;
78 static int max_sg_segs;
79 static int try_direct_io = TRY_DIRECT_IO;
80 static int try_rdio = 1;
81 static int try_wdio = 1;
83 static int st_dev_max;
84 static int st_nr_dev;
86 static struct class *st_sysfs_class;
88 MODULE_AUTHOR("Kai Makisara");
89 MODULE_DESCRIPTION("SCSI Tape Driver");
90 MODULE_LICENSE("GPL");
92 /* Set 'perm' (4th argument) to 0 to disable module_param's definition
93 * of sysfs parameters (which module_param doesn't yet support).
94 * Sysfs parameters defined explicitly later.
96 module_param_named(buffer_kbs, buffer_kbs, int, 0);
97 MODULE_PARM_DESC(buffer_kbs, "Default driver buffer size for fixed block mode (KB; 32)");
98 module_param_named(max_sg_segs, max_sg_segs, int, 0);
99 MODULE_PARM_DESC(max_sg_segs, "Maximum number of scatter/gather segments to use (256)");
100 module_param_named(try_direct_io, try_direct_io, int, 0);
101 MODULE_PARM_DESC(try_direct_io, "Try direct I/O between user buffer and tape drive (1)");
103 /* Extra parameters for testing */
104 module_param_named(try_rdio, try_rdio, int, 0);
105 MODULE_PARM_DESC(try_rdio, "Try direct read i/o when possible");
106 module_param_named(try_wdio, try_wdio, int, 0);
107 MODULE_PARM_DESC(try_wdio, "Try direct write i/o when possible");
109 #ifndef MODULE
110 static int write_threshold_kbs; /* retained for compatibility */
111 static struct st_dev_parm {
112 char *name;
113 int *val;
114 } parms[] __initdata = {
116 "buffer_kbs", &buffer_kbs
118 { /* Retained for compatibility with 2.4 */
119 "write_threshold_kbs", &write_threshold_kbs
122 "max_sg_segs", NULL
125 "try_direct_io", &try_direct_io
128 #endif
130 /* Restrict the number of modes so that names for all are assigned */
131 #if ST_NBR_MODES > 16
132 #error "Maximum number of modes is 16"
133 #endif
134 /* Bit reversed order to get same names for same minors with all
135 mode counts */
136 static const char *st_formats[] = {
137 "", "r", "k", "s", "l", "t", "o", "u",
138 "m", "v", "p", "x", "a", "y", "q", "z"};
140 /* The default definitions have been moved to st_options.h */
142 #define ST_FIXED_BUFFER_SIZE (ST_FIXED_BUFFER_BLOCKS * ST_KILOBYTE)
144 /* The buffer size should fit into the 24 bits for length in the
145 6-byte SCSI read and write commands. */
146 #if ST_FIXED_BUFFER_SIZE >= (2 << 24 - 1)
147 #error "Buffer size should not exceed (2 << 24 - 1) bytes!"
148 #endif
150 static int debugging = DEBUG;
152 #define MAX_RETRIES 0
153 #define MAX_WRITE_RETRIES 0
154 #define MAX_READY_RETRIES 0
155 #define NO_TAPE NOT_READY
157 #define ST_TIMEOUT (900 * HZ)
158 #define ST_LONG_TIMEOUT (14000 * HZ)
160 /* Remove mode bits and auto-rewind bit (7) */
161 #define TAPE_NR(x) ( ((iminor(x) & ~255) >> (ST_NBR_MODE_BITS + 1)) | \
162 (iminor(x) & ~(-1 << ST_MODE_SHIFT)) )
163 #define TAPE_MODE(x) ((iminor(x) & ST_MODE_MASK) >> ST_MODE_SHIFT)
165 /* Construct the minor number from the device (d), mode (m), and non-rewind (n) data */
166 #define TAPE_MINOR(d, m, n) (((d & ~(255 >> (ST_NBR_MODE_BITS + 1))) << (ST_NBR_MODE_BITS + 1)) | \
167 (d & (255 >> (ST_NBR_MODE_BITS + 1))) | (m << ST_MODE_SHIFT) | ((n != 0) << 7) )
169 /* Internal ioctl to set both density (uppermost 8 bits) and blocksize (lower
170 24 bits) */
171 #define SET_DENS_AND_BLK 0x10001
173 static DEFINE_RWLOCK(st_dev_arr_lock);
175 static int st_fixed_buffer_size = ST_FIXED_BUFFER_SIZE;
176 static int st_max_sg_segs = ST_MAX_SG;
178 static struct scsi_tape **scsi_tapes = NULL;
180 static int modes_defined;
182 static struct st_buffer *new_tape_buffer(int, int, int);
183 static int enlarge_buffer(struct st_buffer *, int, int);
184 static void normalize_buffer(struct st_buffer *);
185 static int append_to_buffer(const char __user *, struct st_buffer *, int);
186 static int from_buffer(struct st_buffer *, char __user *, int);
187 static void move_buffer_data(struct st_buffer *, int);
188 static void buf_to_sg(struct st_buffer *, unsigned int);
190 static int sgl_map_user_pages(struct scatterlist *, const unsigned int,
191 unsigned long, size_t, int);
192 static int sgl_unmap_user_pages(struct scatterlist *, const unsigned int, int);
194 static int st_probe(struct device *);
195 static int st_remove(struct device *);
196 static int st_init_command(struct scsi_cmnd *);
198 static void do_create_driverfs_files(void);
199 static void do_remove_driverfs_files(void);
200 static void do_create_class_files(struct scsi_tape *, int, int);
202 static struct scsi_driver st_template = {
203 .owner = THIS_MODULE,
204 .gendrv = {
205 .name = "st",
206 .probe = st_probe,
207 .remove = st_remove,
209 .init_command = st_init_command,
212 static int st_compression(struct scsi_tape *, int);
214 static int find_partition(struct scsi_tape *);
215 static int switch_partition(struct scsi_tape *);
217 static int st_int_ioctl(struct scsi_tape *, unsigned int, unsigned long);
219 static void scsi_tape_release(struct kref *);
221 #define to_scsi_tape(obj) container_of(obj, struct scsi_tape, kref)
223 static DECLARE_MUTEX(st_ref_sem);
226 #include "osst_detect.h"
227 #ifndef SIGS_FROM_OSST
228 #define SIGS_FROM_OSST \
229 {"OnStream", "SC-", "", "osst"}, \
230 {"OnStream", "DI-", "", "osst"}, \
231 {"OnStream", "DP-", "", "osst"}, \
232 {"OnStream", "USB", "", "osst"}, \
233 {"OnStream", "FW-", "", "osst"}
234 #endif
236 static struct scsi_tape *scsi_tape_get(int dev)
238 struct scsi_tape *STp = NULL;
240 down(&st_ref_sem);
241 write_lock(&st_dev_arr_lock);
243 if (dev < st_dev_max && scsi_tapes != NULL)
244 STp = scsi_tapes[dev];
245 if (!STp) goto out;
247 kref_get(&STp->kref);
249 if (!STp->device)
250 goto out_put;
252 if (scsi_device_get(STp->device))
253 goto out_put;
255 goto out;
257 out_put:
258 kref_put(&STp->kref, scsi_tape_release);
259 STp = NULL;
260 out:
261 write_unlock(&st_dev_arr_lock);
262 up(&st_ref_sem);
263 return STp;
266 static void scsi_tape_put(struct scsi_tape *STp)
268 struct scsi_device *sdev = STp->device;
270 down(&st_ref_sem);
271 kref_put(&STp->kref, scsi_tape_release);
272 scsi_device_put(sdev);
273 up(&st_ref_sem);
276 struct st_reject_data {
277 char *vendor;
278 char *model;
279 char *rev;
280 char *driver_hint; /* Name of the correct driver, NULL if unknown */
283 static struct st_reject_data reject_list[] = {
284 /* {"XXX", "Yy-", "", NULL}, example */
285 SIGS_FROM_OSST,
286 {NULL, }};
288 /* If the device signature is on the list of incompatible drives, the
289 function returns a pointer to the name of the correct driver (if known) */
290 static char * st_incompatible(struct scsi_device* SDp)
292 struct st_reject_data *rp;
294 for (rp=&(reject_list[0]); rp->vendor != NULL; rp++)
295 if (!strncmp(rp->vendor, SDp->vendor, strlen(rp->vendor)) &&
296 !strncmp(rp->model, SDp->model, strlen(rp->model)) &&
297 !strncmp(rp->rev, SDp->rev, strlen(rp->rev))) {
298 if (rp->driver_hint)
299 return rp->driver_hint;
300 else
301 return "unknown";
303 return NULL;
307 static inline char *tape_name(struct scsi_tape *tape)
309 return tape->disk->disk_name;
313 static void st_analyze_sense(struct st_request *SRpnt, struct st_cmdstatus *s)
315 const u8 *ucp;
316 const u8 *sense = SRpnt->sense;
318 s->have_sense = scsi_normalize_sense(SRpnt->sense,
319 SCSI_SENSE_BUFFERSIZE, &s->sense_hdr);
320 s->flags = 0;
322 if (s->have_sense) {
323 s->deferred = 0;
324 s->remainder_valid =
325 scsi_get_sense_info_fld(sense, SCSI_SENSE_BUFFERSIZE, &s->uremainder64);
326 switch (sense[0] & 0x7f) {
327 case 0x71:
328 s->deferred = 1;
329 case 0x70:
330 s->fixed_format = 1;
331 s->flags = sense[2] & 0xe0;
332 break;
333 case 0x73:
334 s->deferred = 1;
335 case 0x72:
336 s->fixed_format = 0;
337 ucp = scsi_sense_desc_find(sense, SCSI_SENSE_BUFFERSIZE, 4);
338 s->flags = ucp ? (ucp[3] & 0xe0) : 0;
339 break;
345 /* Convert the result to success code */
346 static int st_chk_result(struct scsi_tape *STp, struct st_request * SRpnt)
348 int result = SRpnt->result;
349 u8 scode;
350 DEB(const char *stp;)
351 char *name = tape_name(STp);
352 struct st_cmdstatus *cmdstatp;
354 if (!result)
355 return 0;
357 cmdstatp = &STp->buffer->cmdstat;
358 st_analyze_sense(SRpnt, cmdstatp);
360 if (cmdstatp->have_sense)
361 scode = STp->buffer->cmdstat.sense_hdr.sense_key;
362 else
363 scode = 0;
365 DEB(
366 if (debugging) {
367 printk(ST_DEB_MSG "%s: Error: %x, cmd: %x %x %x %x %x %x\n",
368 name, result,
369 SRpnt->cmd[0], SRpnt->cmd[1], SRpnt->cmd[2],
370 SRpnt->cmd[3], SRpnt->cmd[4], SRpnt->cmd[5]);
371 if (cmdstatp->have_sense)
372 __scsi_print_sense("st", SRpnt->sense, SCSI_SENSE_BUFFERSIZE);
373 } ) /* end DEB */
374 if (!debugging) { /* Abnormal conditions for tape */
375 if (!cmdstatp->have_sense)
376 printk(KERN_WARNING
377 "%s: Error %x (sugg. bt 0x%x, driver bt 0x%x, host bt 0x%x).\n",
378 name, result, suggestion(result),
379 driver_byte(result) & DRIVER_MASK, host_byte(result));
380 else if (cmdstatp->have_sense &&
381 scode != NO_SENSE &&
382 scode != RECOVERED_ERROR &&
383 /* scode != UNIT_ATTENTION && */
384 scode != BLANK_CHECK &&
385 scode != VOLUME_OVERFLOW &&
386 SRpnt->cmd[0] != MODE_SENSE &&
387 SRpnt->cmd[0] != TEST_UNIT_READY) {
388 printk(KERN_WARNING "%s: Error with sense data: ", name);
389 __scsi_print_sense("st", SRpnt->sense,
390 SCSI_SENSE_BUFFERSIZE);
394 if (cmdstatp->fixed_format &&
395 STp->cln_mode >= EXTENDED_SENSE_START) { /* Only fixed format sense */
396 if (STp->cln_sense_value)
397 STp->cleaning_req |= ((SRpnt->sense[STp->cln_mode] &
398 STp->cln_sense_mask) == STp->cln_sense_value);
399 else
400 STp->cleaning_req |= ((SRpnt->sense[STp->cln_mode] &
401 STp->cln_sense_mask) != 0);
403 if (cmdstatp->have_sense &&
404 cmdstatp->sense_hdr.asc == 0 && cmdstatp->sense_hdr.ascq == 0x17)
405 STp->cleaning_req = 1; /* ASC and ASCQ => cleaning requested */
407 STp->pos_unknown |= STp->device->was_reset;
409 if (cmdstatp->have_sense &&
410 scode == RECOVERED_ERROR
411 #if ST_RECOVERED_WRITE_FATAL
412 && SRpnt->cmd[0] != WRITE_6
413 && SRpnt->cmd[0] != WRITE_FILEMARKS
414 #endif
416 STp->recover_count++;
417 STp->recover_reg++;
419 DEB(
420 if (debugging) {
421 if (SRpnt->cmd[0] == READ_6)
422 stp = "read";
423 else if (SRpnt->cmd[0] == WRITE_6)
424 stp = "write";
425 else
426 stp = "ioctl";
427 printk(ST_DEB_MSG "%s: Recovered %s error (%d).\n", name, stp,
428 STp->recover_count);
429 } ) /* end DEB */
431 if (cmdstatp->flags == 0)
432 return 0;
434 return (-EIO);
438 /* Wakeup from interrupt */
439 static void st_sleep_done(void *data, char *sense, int result, int resid)
441 struct st_request *SRpnt = data;
442 struct scsi_tape *STp = SRpnt->stp;
444 memcpy(SRpnt->sense, sense, SCSI_SENSE_BUFFERSIZE);
445 (STp->buffer)->cmdstat.midlevel_result = SRpnt->result = result;
446 DEB( STp->write_pending = 0; )
448 if (SRpnt->waiting)
449 complete(SRpnt->waiting);
452 static struct st_request *st_allocate_request(void)
454 return kzalloc(sizeof(struct st_request), GFP_KERNEL);
457 static void st_release_request(struct st_request *streq)
459 kfree(streq);
462 /* Do the scsi command. Waits until command performed if do_wait is true.
463 Otherwise write_behind_check() is used to check that the command
464 has finished. */
465 static struct st_request *
466 st_do_scsi(struct st_request * SRpnt, struct scsi_tape * STp, unsigned char *cmd,
467 int bytes, int direction, int timeout, int retries, int do_wait)
469 struct completion *waiting;
471 /* if async, make sure there's no command outstanding */
472 if (!do_wait && ((STp->buffer)->last_SRpnt)) {
473 printk(KERN_ERR "%s: Async command already active.\n",
474 tape_name(STp));
475 if (signal_pending(current))
476 (STp->buffer)->syscall_result = (-EINTR);
477 else
478 (STp->buffer)->syscall_result = (-EBUSY);
479 return NULL;
482 if (SRpnt == NULL) {
483 SRpnt = st_allocate_request();
484 if (SRpnt == NULL) {
485 DEBC( printk(KERN_ERR "%s: Can't get SCSI request.\n",
486 tape_name(STp)); );
487 if (signal_pending(current))
488 (STp->buffer)->syscall_result = (-EINTR);
489 else
490 (STp->buffer)->syscall_result = (-EBUSY);
491 return NULL;
493 SRpnt->stp = STp;
496 /* If async IO, set last_SRpnt. This ptr tells write_behind_check
497 which IO is outstanding. It's nulled out when the IO completes. */
498 if (!do_wait)
499 (STp->buffer)->last_SRpnt = SRpnt;
501 waiting = &STp->wait;
502 init_completion(waiting);
503 SRpnt->waiting = waiting;
505 if (!STp->buffer->do_dio)
506 buf_to_sg(STp->buffer, bytes);
508 memcpy(SRpnt->cmd, cmd, sizeof(SRpnt->cmd));
509 STp->buffer->cmdstat.have_sense = 0;
510 STp->buffer->syscall_result = 0;
512 if (scsi_execute_async(STp->device, cmd, direction,
513 &((STp->buffer)->sg[0]), bytes, (STp->buffer)->sg_segs,
514 timeout, retries, SRpnt, st_sleep_done, GFP_KERNEL)) {
515 /* could not allocate the buffer or request was too large */
516 (STp->buffer)->syscall_result = (-EBUSY);
517 (STp->buffer)->last_SRpnt = NULL;
519 else if (do_wait) {
520 wait_for_completion(waiting);
521 SRpnt->waiting = NULL;
522 (STp->buffer)->syscall_result = st_chk_result(STp, SRpnt);
525 return SRpnt;
529 /* Handle the write-behind checking (waits for completion). Returns -ENOSPC if
530 write has been correct but EOM early warning reached, -EIO if write ended in
531 error or zero if write successful. Asynchronous writes are used only in
532 variable block mode. */
533 static int write_behind_check(struct scsi_tape * STp)
535 int retval = 0;
536 struct st_buffer *STbuffer;
537 struct st_partstat *STps;
538 struct st_cmdstatus *cmdstatp;
539 struct st_request *SRpnt;
541 STbuffer = STp->buffer;
542 if (!STbuffer->writing)
543 return 0;
545 DEB(
546 if (STp->write_pending)
547 STp->nbr_waits++;
548 else
549 STp->nbr_finished++;
550 ) /* end DEB */
552 wait_for_completion(&(STp->wait));
553 SRpnt = STbuffer->last_SRpnt;
554 STbuffer->last_SRpnt = NULL;
555 SRpnt->waiting = NULL;
557 (STp->buffer)->syscall_result = st_chk_result(STp, SRpnt);
558 st_release_request(SRpnt);
560 STbuffer->buffer_bytes -= STbuffer->writing;
561 STps = &(STp->ps[STp->partition]);
562 if (STps->drv_block >= 0) {
563 if (STp->block_size == 0)
564 STps->drv_block++;
565 else
566 STps->drv_block += STbuffer->writing / STp->block_size;
569 cmdstatp = &STbuffer->cmdstat;
570 if (STbuffer->syscall_result) {
571 retval = -EIO;
572 if (cmdstatp->have_sense && !cmdstatp->deferred &&
573 (cmdstatp->flags & SENSE_EOM) &&
574 (cmdstatp->sense_hdr.sense_key == NO_SENSE ||
575 cmdstatp->sense_hdr.sense_key == RECOVERED_ERROR)) {
576 /* EOM at write-behind, has all data been written? */
577 if (!cmdstatp->remainder_valid ||
578 cmdstatp->uremainder64 == 0)
579 retval = -ENOSPC;
581 if (retval == -EIO)
582 STps->drv_block = -1;
584 STbuffer->writing = 0;
586 DEB(if (debugging && retval)
587 printk(ST_DEB_MSG "%s: Async write error %x, return value %d.\n",
588 tape_name(STp), STbuffer->cmdstat.midlevel_result, retval);) /* end DEB */
590 return retval;
594 /* Step over EOF if it has been inadvertently crossed (ioctl not used because
595 it messes up the block number). */
596 static int cross_eof(struct scsi_tape * STp, int forward)
598 struct st_request *SRpnt;
599 unsigned char cmd[MAX_COMMAND_SIZE];
601 cmd[0] = SPACE;
602 cmd[1] = 0x01; /* Space FileMarks */
603 if (forward) {
604 cmd[2] = cmd[3] = 0;
605 cmd[4] = 1;
606 } else
607 cmd[2] = cmd[3] = cmd[4] = 0xff; /* -1 filemarks */
608 cmd[5] = 0;
610 DEBC(printk(ST_DEB_MSG "%s: Stepping over filemark %s.\n",
611 tape_name(STp), forward ? "forward" : "backward"));
613 SRpnt = st_do_scsi(NULL, STp, cmd, 0, DMA_NONE,
614 STp->device->timeout, MAX_RETRIES, 1);
615 if (!SRpnt)
616 return (STp->buffer)->syscall_result;
618 st_release_request(SRpnt);
619 SRpnt = NULL;
621 if ((STp->buffer)->cmdstat.midlevel_result != 0)
622 printk(KERN_ERR "%s: Stepping over filemark %s failed.\n",
623 tape_name(STp), forward ? "forward" : "backward");
625 return (STp->buffer)->syscall_result;
629 /* Flush the write buffer (never need to write if variable blocksize). */
630 static int flush_write_buffer(struct scsi_tape * STp)
632 int offset, transfer, blks;
633 int result;
634 unsigned char cmd[MAX_COMMAND_SIZE];
635 struct st_request *SRpnt;
636 struct st_partstat *STps;
638 result = write_behind_check(STp);
639 if (result)
640 return result;
642 result = 0;
643 if (STp->dirty == 1) {
645 offset = (STp->buffer)->buffer_bytes;
646 transfer = ((offset + STp->block_size - 1) /
647 STp->block_size) * STp->block_size;
648 DEBC(printk(ST_DEB_MSG "%s: Flushing %d bytes.\n",
649 tape_name(STp), transfer));
651 memset((STp->buffer)->b_data + offset, 0, transfer - offset);
653 memset(cmd, 0, MAX_COMMAND_SIZE);
654 cmd[0] = WRITE_6;
655 cmd[1] = 1;
656 blks = transfer / STp->block_size;
657 cmd[2] = blks >> 16;
658 cmd[3] = blks >> 8;
659 cmd[4] = blks;
661 SRpnt = st_do_scsi(NULL, STp, cmd, transfer, DMA_TO_DEVICE,
662 STp->device->timeout, MAX_WRITE_RETRIES, 1);
663 if (!SRpnt)
664 return (STp->buffer)->syscall_result;
666 STps = &(STp->ps[STp->partition]);
667 if ((STp->buffer)->syscall_result != 0) {
668 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
670 if (cmdstatp->have_sense && !cmdstatp->deferred &&
671 (cmdstatp->flags & SENSE_EOM) &&
672 (cmdstatp->sense_hdr.sense_key == NO_SENSE ||
673 cmdstatp->sense_hdr.sense_key == RECOVERED_ERROR) &&
674 (!cmdstatp->remainder_valid ||
675 cmdstatp->uremainder64 == 0)) { /* All written at EOM early warning */
676 STp->dirty = 0;
677 (STp->buffer)->buffer_bytes = 0;
678 if (STps->drv_block >= 0)
679 STps->drv_block += blks;
680 result = (-ENOSPC);
681 } else {
682 printk(KERN_ERR "%s: Error on flush.\n",
683 tape_name(STp));
684 STps->drv_block = (-1);
685 result = (-EIO);
687 } else {
688 if (STps->drv_block >= 0)
689 STps->drv_block += blks;
690 STp->dirty = 0;
691 (STp->buffer)->buffer_bytes = 0;
693 st_release_request(SRpnt);
694 SRpnt = NULL;
696 return result;
700 /* Flush the tape buffer. The tape will be positioned correctly unless
701 seek_next is true. */
702 static int flush_buffer(struct scsi_tape *STp, int seek_next)
704 int backspace, result;
705 struct st_buffer *STbuffer;
706 struct st_partstat *STps;
708 STbuffer = STp->buffer;
711 * If there was a bus reset, block further access
712 * to this device.
714 if (STp->pos_unknown)
715 return (-EIO);
717 if (STp->ready != ST_READY)
718 return 0;
719 STps = &(STp->ps[STp->partition]);
720 if (STps->rw == ST_WRITING) /* Writing */
721 return flush_write_buffer(STp);
723 if (STp->block_size == 0)
724 return 0;
726 backspace = ((STp->buffer)->buffer_bytes +
727 (STp->buffer)->read_pointer) / STp->block_size -
728 ((STp->buffer)->read_pointer + STp->block_size - 1) /
729 STp->block_size;
730 (STp->buffer)->buffer_bytes = 0;
731 (STp->buffer)->read_pointer = 0;
732 result = 0;
733 if (!seek_next) {
734 if (STps->eof == ST_FM_HIT) {
735 result = cross_eof(STp, 0); /* Back over the EOF hit */
736 if (!result)
737 STps->eof = ST_NOEOF;
738 else {
739 if (STps->drv_file >= 0)
740 STps->drv_file++;
741 STps->drv_block = 0;
744 if (!result && backspace > 0)
745 result = st_int_ioctl(STp, MTBSR, backspace);
746 } else if (STps->eof == ST_FM_HIT) {
747 if (STps->drv_file >= 0)
748 STps->drv_file++;
749 STps->drv_block = 0;
750 STps->eof = ST_NOEOF;
752 return result;
756 /* Set the mode parameters */
757 static int set_mode_densblk(struct scsi_tape * STp, struct st_modedef * STm)
759 int set_it = 0;
760 unsigned long arg;
761 char *name = tape_name(STp);
763 if (!STp->density_changed &&
764 STm->default_density >= 0 &&
765 STm->default_density != STp->density) {
766 arg = STm->default_density;
767 set_it = 1;
768 } else
769 arg = STp->density;
770 arg <<= MT_ST_DENSITY_SHIFT;
771 if (!STp->blksize_changed &&
772 STm->default_blksize >= 0 &&
773 STm->default_blksize != STp->block_size) {
774 arg |= STm->default_blksize;
775 set_it = 1;
776 } else
777 arg |= STp->block_size;
778 if (set_it &&
779 st_int_ioctl(STp, SET_DENS_AND_BLK, arg)) {
780 printk(KERN_WARNING
781 "%s: Can't set default block size to %d bytes and density %x.\n",
782 name, STm->default_blksize, STm->default_density);
783 if (modes_defined)
784 return (-EINVAL);
786 return 0;
790 /* Lock or unlock the drive door. Don't use when st_request allocated. */
791 static int do_door_lock(struct scsi_tape * STp, int do_lock)
793 int retval, cmd;
794 DEB(char *name = tape_name(STp);)
797 cmd = do_lock ? SCSI_IOCTL_DOORLOCK : SCSI_IOCTL_DOORUNLOCK;
798 DEBC(printk(ST_DEB_MSG "%s: %socking drive door.\n", name,
799 do_lock ? "L" : "Unl"));
800 retval = scsi_ioctl(STp->device, cmd, NULL);
801 if (!retval) {
802 STp->door_locked = do_lock ? ST_LOCKED_EXPLICIT : ST_UNLOCKED;
804 else {
805 STp->door_locked = ST_LOCK_FAILS;
807 return retval;
811 /* Set the internal state after reset */
812 static void reset_state(struct scsi_tape *STp)
814 int i;
815 struct st_partstat *STps;
817 STp->pos_unknown = 0;
818 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
819 STps = &(STp->ps[i]);
820 STps->rw = ST_IDLE;
821 STps->eof = ST_NOEOF;
822 STps->at_sm = 0;
823 STps->last_block_valid = 0;
824 STps->drv_block = -1;
825 STps->drv_file = -1;
827 if (STp->can_partitions) {
828 STp->partition = find_partition(STp);
829 if (STp->partition < 0)
830 STp->partition = 0;
831 STp->new_partition = STp->partition;
835 /* Test if the drive is ready. Returns either one of the codes below or a negative system
836 error code. */
837 #define CHKRES_READY 0
838 #define CHKRES_NEW_SESSION 1
839 #define CHKRES_NOT_READY 2
840 #define CHKRES_NO_TAPE 3
842 #define MAX_ATTENTIONS 10
844 static int test_ready(struct scsi_tape *STp, int do_wait)
846 int attentions, waits, max_wait, scode;
847 int retval = CHKRES_READY, new_session = 0;
848 unsigned char cmd[MAX_COMMAND_SIZE];
849 struct st_request *SRpnt = NULL;
850 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
852 max_wait = do_wait ? ST_BLOCK_SECONDS : 0;
854 for (attentions=waits=0; ; ) {
855 memset((void *) &cmd[0], 0, MAX_COMMAND_SIZE);
856 cmd[0] = TEST_UNIT_READY;
857 SRpnt = st_do_scsi(SRpnt, STp, cmd, 0, DMA_NONE,
858 STp->long_timeout, MAX_READY_RETRIES, 1);
860 if (!SRpnt) {
861 retval = (STp->buffer)->syscall_result;
862 break;
865 if (cmdstatp->have_sense) {
867 scode = cmdstatp->sense_hdr.sense_key;
869 if (scode == UNIT_ATTENTION) { /* New media? */
870 new_session = 1;
871 if (attentions < MAX_ATTENTIONS) {
872 attentions++;
873 continue;
875 else {
876 retval = (-EIO);
877 break;
881 if (scode == NOT_READY) {
882 if (waits < max_wait) {
883 if (msleep_interruptible(1000)) {
884 retval = (-EINTR);
885 break;
887 waits++;
888 continue;
890 else {
891 if ((STp->device)->scsi_level >= SCSI_2 &&
892 cmdstatp->sense_hdr.asc == 0x3a) /* Check ASC */
893 retval = CHKRES_NO_TAPE;
894 else
895 retval = CHKRES_NOT_READY;
896 break;
901 retval = (STp->buffer)->syscall_result;
902 if (!retval)
903 retval = new_session ? CHKRES_NEW_SESSION : CHKRES_READY;
904 break;
907 if (SRpnt != NULL)
908 st_release_request(SRpnt);
909 return retval;
913 /* See if the drive is ready and gather information about the tape. Return values:
914 < 0 negative error code from errno.h
915 0 drive ready
916 1 drive not ready (possibly no tape)
918 static int check_tape(struct scsi_tape *STp, struct file *filp)
920 int i, retval, new_session = 0, do_wait;
921 unsigned char cmd[MAX_COMMAND_SIZE], saved_cleaning;
922 unsigned short st_flags = filp->f_flags;
923 struct st_request *SRpnt = NULL;
924 struct st_modedef *STm;
925 struct st_partstat *STps;
926 char *name = tape_name(STp);
927 struct inode *inode = filp->f_dentry->d_inode;
928 int mode = TAPE_MODE(inode);
930 STp->ready = ST_READY;
932 if (mode != STp->current_mode) {
933 DEBC(printk(ST_DEB_MSG "%s: Mode change from %d to %d.\n",
934 name, STp->current_mode, mode));
935 new_session = 1;
936 STp->current_mode = mode;
938 STm = &(STp->modes[STp->current_mode]);
940 saved_cleaning = STp->cleaning_req;
941 STp->cleaning_req = 0;
943 do_wait = ((filp->f_flags & O_NONBLOCK) == 0);
944 retval = test_ready(STp, do_wait);
946 if (retval < 0)
947 goto err_out;
949 if (retval == CHKRES_NEW_SESSION) {
950 STp->pos_unknown = 0;
951 STp->partition = STp->new_partition = 0;
952 if (STp->can_partitions)
953 STp->nbr_partitions = 1; /* This guess will be updated later
954 if necessary */
955 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
956 STps = &(STp->ps[i]);
957 STps->rw = ST_IDLE;
958 STps->eof = ST_NOEOF;
959 STps->at_sm = 0;
960 STps->last_block_valid = 0;
961 STps->drv_block = 0;
962 STps->drv_file = 0;
964 new_session = 1;
966 else {
967 STp->cleaning_req |= saved_cleaning;
969 if (retval == CHKRES_NOT_READY || retval == CHKRES_NO_TAPE) {
970 if (retval == CHKRES_NO_TAPE)
971 STp->ready = ST_NO_TAPE;
972 else
973 STp->ready = ST_NOT_READY;
975 STp->density = 0; /* Clear the erroneous "residue" */
976 STp->write_prot = 0;
977 STp->block_size = 0;
978 STp->ps[0].drv_file = STp->ps[0].drv_block = (-1);
979 STp->partition = STp->new_partition = 0;
980 STp->door_locked = ST_UNLOCKED;
981 return CHKRES_NOT_READY;
985 if (STp->omit_blklims)
986 STp->min_block = STp->max_block = (-1);
987 else {
988 memset((void *) &cmd[0], 0, MAX_COMMAND_SIZE);
989 cmd[0] = READ_BLOCK_LIMITS;
991 SRpnt = st_do_scsi(SRpnt, STp, cmd, 6, DMA_FROM_DEVICE,
992 STp->device->timeout, MAX_READY_RETRIES, 1);
993 if (!SRpnt) {
994 retval = (STp->buffer)->syscall_result;
995 goto err_out;
998 if (!SRpnt->result && !STp->buffer->cmdstat.have_sense) {
999 STp->max_block = ((STp->buffer)->b_data[1] << 16) |
1000 ((STp->buffer)->b_data[2] << 8) | (STp->buffer)->b_data[3];
1001 STp->min_block = ((STp->buffer)->b_data[4] << 8) |
1002 (STp->buffer)->b_data[5];
1003 if ( DEB( debugging || ) !STp->inited)
1004 printk(KERN_WARNING
1005 "%s: Block limits %d - %d bytes.\n", name,
1006 STp->min_block, STp->max_block);
1007 } else {
1008 STp->min_block = STp->max_block = (-1);
1009 DEBC(printk(ST_DEB_MSG "%s: Can't read block limits.\n",
1010 name));
1014 memset((void *) &cmd[0], 0, MAX_COMMAND_SIZE);
1015 cmd[0] = MODE_SENSE;
1016 cmd[4] = 12;
1018 SRpnt = st_do_scsi(SRpnt, STp, cmd, 12, DMA_FROM_DEVICE,
1019 STp->device->timeout, MAX_READY_RETRIES, 1);
1020 if (!SRpnt) {
1021 retval = (STp->buffer)->syscall_result;
1022 goto err_out;
1025 if ((STp->buffer)->syscall_result != 0) {
1026 DEBC(printk(ST_DEB_MSG "%s: No Mode Sense.\n", name));
1027 STp->block_size = ST_DEFAULT_BLOCK; /* Educated guess (?) */
1028 (STp->buffer)->syscall_result = 0; /* Prevent error propagation */
1029 STp->drv_write_prot = 0;
1030 } else {
1031 DEBC(printk(ST_DEB_MSG
1032 "%s: Mode sense. Length %d, medium %x, WBS %x, BLL %d\n",
1033 name,
1034 (STp->buffer)->b_data[0], (STp->buffer)->b_data[1],
1035 (STp->buffer)->b_data[2], (STp->buffer)->b_data[3]));
1037 if ((STp->buffer)->b_data[3] >= 8) {
1038 STp->drv_buffer = ((STp->buffer)->b_data[2] >> 4) & 7;
1039 STp->density = (STp->buffer)->b_data[4];
1040 STp->block_size = (STp->buffer)->b_data[9] * 65536 +
1041 (STp->buffer)->b_data[10] * 256 + (STp->buffer)->b_data[11];
1042 DEBC(printk(ST_DEB_MSG
1043 "%s: Density %x, tape length: %x, drv buffer: %d\n",
1044 name, STp->density, (STp->buffer)->b_data[5] * 65536 +
1045 (STp->buffer)->b_data[6] * 256 + (STp->buffer)->b_data[7],
1046 STp->drv_buffer));
1048 STp->drv_write_prot = ((STp->buffer)->b_data[2] & 0x80) != 0;
1050 st_release_request(SRpnt);
1051 SRpnt = NULL;
1052 STp->inited = 1;
1054 if (STp->block_size > 0)
1055 (STp->buffer)->buffer_blocks =
1056 (STp->buffer)->buffer_size / STp->block_size;
1057 else
1058 (STp->buffer)->buffer_blocks = 1;
1059 (STp->buffer)->buffer_bytes = (STp->buffer)->read_pointer = 0;
1061 DEBC(printk(ST_DEB_MSG
1062 "%s: Block size: %d, buffer size: %d (%d blocks).\n", name,
1063 STp->block_size, (STp->buffer)->buffer_size,
1064 (STp->buffer)->buffer_blocks));
1066 if (STp->drv_write_prot) {
1067 STp->write_prot = 1;
1069 DEBC(printk(ST_DEB_MSG "%s: Write protected\n", name));
1071 if (do_wait &&
1072 ((st_flags & O_ACCMODE) == O_WRONLY ||
1073 (st_flags & O_ACCMODE) == O_RDWR)) {
1074 retval = (-EROFS);
1075 goto err_out;
1079 if (STp->can_partitions && STp->nbr_partitions < 1) {
1080 /* This code is reached when the device is opened for the first time
1081 after the driver has been initialized with tape in the drive and the
1082 partition support has been enabled. */
1083 DEBC(printk(ST_DEB_MSG
1084 "%s: Updating partition number in status.\n", name));
1085 if ((STp->partition = find_partition(STp)) < 0) {
1086 retval = STp->partition;
1087 goto err_out;
1089 STp->new_partition = STp->partition;
1090 STp->nbr_partitions = 1; /* This guess will be updated when necessary */
1093 if (new_session) { /* Change the drive parameters for the new mode */
1094 STp->density_changed = STp->blksize_changed = 0;
1095 STp->compression_changed = 0;
1096 if (!(STm->defaults_for_writes) &&
1097 (retval = set_mode_densblk(STp, STm)) < 0)
1098 goto err_out;
1100 if (STp->default_drvbuffer != 0xff) {
1101 if (st_int_ioctl(STp, MTSETDRVBUFFER, STp->default_drvbuffer))
1102 printk(KERN_WARNING
1103 "%s: Can't set default drive buffering to %d.\n",
1104 name, STp->default_drvbuffer);
1108 return CHKRES_READY;
1110 err_out:
1111 return retval;
1115 \f/* Open the device. Needs to be called with BKL only because of incrementing the SCSI host
1116 module count. */
1117 static int st_open(struct inode *inode, struct file *filp)
1119 int i, retval = (-EIO);
1120 struct scsi_tape *STp;
1121 struct st_partstat *STps;
1122 int dev = TAPE_NR(inode);
1123 char *name;
1126 * We really want to do nonseekable_open(inode, filp); here, but some
1127 * versions of tar incorrectly call lseek on tapes and bail out if that
1128 * fails. So we disallow pread() and pwrite(), but permit lseeks.
1130 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
1132 if (!(STp = scsi_tape_get(dev)))
1133 return -ENXIO;
1135 write_lock(&st_dev_arr_lock);
1136 filp->private_data = STp;
1137 name = tape_name(STp);
1139 if (STp->in_use) {
1140 write_unlock(&st_dev_arr_lock);
1141 scsi_tape_put(STp);
1142 DEB( printk(ST_DEB_MSG "%s: Device already in use.\n", name); )
1143 return (-EBUSY);
1146 STp->in_use = 1;
1147 write_unlock(&st_dev_arr_lock);
1148 STp->rew_at_close = STp->autorew_dev = (iminor(inode) & 0x80) == 0;
1150 if (!scsi_block_when_processing_errors(STp->device)) {
1151 retval = (-ENXIO);
1152 goto err_out;
1155 /* See that we have at least a one page buffer available */
1156 if (!enlarge_buffer(STp->buffer, PAGE_SIZE, STp->restr_dma)) {
1157 printk(KERN_WARNING "%s: Can't allocate one page tape buffer.\n",
1158 name);
1159 retval = (-EOVERFLOW);
1160 goto err_out;
1163 (STp->buffer)->writing = 0;
1164 (STp->buffer)->syscall_result = 0;
1166 STp->write_prot = ((filp->f_flags & O_ACCMODE) == O_RDONLY);
1168 STp->dirty = 0;
1169 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
1170 STps = &(STp->ps[i]);
1171 STps->rw = ST_IDLE;
1173 STp->recover_count = 0;
1174 DEB( STp->nbr_waits = STp->nbr_finished = 0;
1175 STp->nbr_requests = STp->nbr_dio = STp->nbr_pages = STp->nbr_combinable = 0; )
1177 retval = check_tape(STp, filp);
1178 if (retval < 0)
1179 goto err_out;
1180 if ((filp->f_flags & O_NONBLOCK) == 0 &&
1181 retval != CHKRES_READY) {
1182 retval = (-EIO);
1183 goto err_out;
1185 return 0;
1187 err_out:
1188 normalize_buffer(STp->buffer);
1189 STp->in_use = 0;
1190 scsi_tape_put(STp);
1191 return retval;
1196 /* Flush the tape buffer before close */
1197 static int st_flush(struct file *filp)
1199 int result = 0, result2;
1200 unsigned char cmd[MAX_COMMAND_SIZE];
1201 struct st_request *SRpnt;
1202 struct scsi_tape *STp = filp->private_data;
1203 struct st_modedef *STm = &(STp->modes[STp->current_mode]);
1204 struct st_partstat *STps = &(STp->ps[STp->partition]);
1205 char *name = tape_name(STp);
1207 if (file_count(filp) > 1)
1208 return 0;
1210 if (STps->rw == ST_WRITING && !STp->pos_unknown) {
1211 result = flush_write_buffer(STp);
1212 if (result != 0 && result != (-ENOSPC))
1213 goto out;
1216 if (STp->can_partitions &&
1217 (result2 = switch_partition(STp)) < 0) {
1218 DEBC(printk(ST_DEB_MSG
1219 "%s: switch_partition at close failed.\n", name));
1220 if (result == 0)
1221 result = result2;
1222 goto out;
1225 DEBC( if (STp->nbr_requests)
1226 printk(KERN_WARNING "%s: Number of r/w requests %d, dio used in %d, pages %d (%d).\n",
1227 name, STp->nbr_requests, STp->nbr_dio, STp->nbr_pages, STp->nbr_combinable));
1229 if (STps->rw == ST_WRITING && !STp->pos_unknown) {
1230 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
1232 DEBC(printk(ST_DEB_MSG "%s: Async write waits %d, finished %d.\n",
1233 name, STp->nbr_waits, STp->nbr_finished);
1236 memset(cmd, 0, MAX_COMMAND_SIZE);
1237 cmd[0] = WRITE_FILEMARKS;
1238 cmd[4] = 1 + STp->two_fm;
1240 SRpnt = st_do_scsi(NULL, STp, cmd, 0, DMA_NONE,
1241 STp->device->timeout, MAX_WRITE_RETRIES, 1);
1242 if (!SRpnt) {
1243 result = (STp->buffer)->syscall_result;
1244 goto out;
1247 if (STp->buffer->syscall_result == 0 ||
1248 (cmdstatp->have_sense && !cmdstatp->deferred &&
1249 (cmdstatp->flags & SENSE_EOM) &&
1250 (cmdstatp->sense_hdr.sense_key == NO_SENSE ||
1251 cmdstatp->sense_hdr.sense_key == RECOVERED_ERROR) &&
1252 (!cmdstatp->remainder_valid || cmdstatp->uremainder64 == 0))) {
1253 /* Write successful at EOM */
1254 st_release_request(SRpnt);
1255 SRpnt = NULL;
1256 if (STps->drv_file >= 0)
1257 STps->drv_file++;
1258 STps->drv_block = 0;
1259 if (STp->two_fm)
1260 cross_eof(STp, 0);
1261 STps->eof = ST_FM;
1263 else { /* Write error */
1264 st_release_request(SRpnt);
1265 SRpnt = NULL;
1266 printk(KERN_ERR "%s: Error on write filemark.\n", name);
1267 if (result == 0)
1268 result = (-EIO);
1271 DEBC(printk(ST_DEB_MSG "%s: Buffer flushed, %d EOF(s) written\n",
1272 name, cmd[4]));
1273 } else if (!STp->rew_at_close) {
1274 STps = &(STp->ps[STp->partition]);
1275 if (!STm->sysv || STps->rw != ST_READING) {
1276 if (STp->can_bsr)
1277 result = flush_buffer(STp, 0);
1278 else if (STps->eof == ST_FM_HIT) {
1279 result = cross_eof(STp, 0);
1280 if (result) {
1281 if (STps->drv_file >= 0)
1282 STps->drv_file++;
1283 STps->drv_block = 0;
1284 STps->eof = ST_FM;
1285 } else
1286 STps->eof = ST_NOEOF;
1288 } else if ((STps->eof == ST_NOEOF &&
1289 !(result = cross_eof(STp, 1))) ||
1290 STps->eof == ST_FM_HIT) {
1291 if (STps->drv_file >= 0)
1292 STps->drv_file++;
1293 STps->drv_block = 0;
1294 STps->eof = ST_FM;
1298 out:
1299 if (STp->rew_at_close) {
1300 result2 = st_int_ioctl(STp, MTREW, 1);
1301 if (result == 0)
1302 result = result2;
1304 return result;
1308 /* Close the device and release it. BKL is not needed: this is the only thread
1309 accessing this tape. */
1310 static int st_release(struct inode *inode, struct file *filp)
1312 int result = 0;
1313 struct scsi_tape *STp = filp->private_data;
1315 if (STp->door_locked == ST_LOCKED_AUTO)
1316 do_door_lock(STp, 0);
1318 normalize_buffer(STp->buffer);
1319 write_lock(&st_dev_arr_lock);
1320 STp->in_use = 0;
1321 write_unlock(&st_dev_arr_lock);
1322 scsi_tape_put(STp);
1324 return result;
1327 /* The checks common to both reading and writing */
1328 static ssize_t rw_checks(struct scsi_tape *STp, struct file *filp, size_t count)
1330 ssize_t retval = 0;
1333 * If we are in the middle of error recovery, don't let anyone
1334 * else try and use this device. Also, if error recovery fails, it
1335 * may try and take the device offline, in which case all further
1336 * access to the device is prohibited.
1338 if (!scsi_block_when_processing_errors(STp->device)) {
1339 retval = (-ENXIO);
1340 goto out;
1343 if (STp->ready != ST_READY) {
1344 if (STp->ready == ST_NO_TAPE)
1345 retval = (-ENOMEDIUM);
1346 else
1347 retval = (-EIO);
1348 goto out;
1351 if (! STp->modes[STp->current_mode].defined) {
1352 retval = (-ENXIO);
1353 goto out;
1358 * If there was a bus reset, block further access
1359 * to this device.
1361 if (STp->pos_unknown) {
1362 retval = (-EIO);
1363 goto out;
1366 if (count == 0)
1367 goto out;
1369 DEB(
1370 if (!STp->in_use) {
1371 printk(ST_DEB_MSG "%s: Incorrect device.\n", tape_name(STp));
1372 retval = (-EIO);
1373 goto out;
1374 } ) /* end DEB */
1376 if (STp->can_partitions &&
1377 (retval = switch_partition(STp)) < 0)
1378 goto out;
1380 if (STp->block_size == 0 && STp->max_block > 0 &&
1381 (count < STp->min_block || count > STp->max_block)) {
1382 retval = (-EINVAL);
1383 goto out;
1386 if (STp->do_auto_lock && STp->door_locked == ST_UNLOCKED &&
1387 !do_door_lock(STp, 1))
1388 STp->door_locked = ST_LOCKED_AUTO;
1390 out:
1391 return retval;
1395 static int setup_buffering(struct scsi_tape *STp, const char __user *buf,
1396 size_t count, int is_read)
1398 int i, bufsize, retval = 0;
1399 struct st_buffer *STbp = STp->buffer;
1401 if (is_read)
1402 i = STp->try_dio && try_rdio;
1403 else
1404 i = STp->try_dio && try_wdio;
1406 if (i && ((unsigned long)buf & queue_dma_alignment(
1407 STp->device->request_queue)) == 0) {
1408 i = sgl_map_user_pages(&(STbp->sg[0]), STbp->use_sg,
1409 (unsigned long)buf, count, (is_read ? READ : WRITE));
1410 if (i > 0) {
1411 STbp->do_dio = i;
1412 STbp->buffer_bytes = 0; /* can be used as transfer counter */
1414 else
1415 STbp->do_dio = 0; /* fall back to buffering with any error */
1416 STbp->sg_segs = STbp->do_dio;
1417 STbp->frp_sg_current = 0;
1418 DEB(
1419 if (STbp->do_dio) {
1420 STp->nbr_dio++;
1421 STp->nbr_pages += STbp->do_dio;
1422 for (i=1; i < STbp->do_dio; i++)
1423 if (page_to_pfn(STbp->sg[i].page) == page_to_pfn(STbp->sg[i-1].page) + 1)
1424 STp->nbr_combinable++;
1427 } else
1428 STbp->do_dio = 0;
1429 DEB( STp->nbr_requests++; )
1431 if (!STbp->do_dio) {
1432 if (STp->block_size)
1433 bufsize = STp->block_size > st_fixed_buffer_size ?
1434 STp->block_size : st_fixed_buffer_size;
1435 else
1436 bufsize = count;
1437 if (bufsize > STbp->buffer_size &&
1438 !enlarge_buffer(STbp, bufsize, STp->restr_dma)) {
1439 printk(KERN_WARNING "%s: Can't allocate %d byte tape buffer.\n",
1440 tape_name(STp), bufsize);
1441 retval = (-EOVERFLOW);
1442 goto out;
1444 if (STp->block_size)
1445 STbp->buffer_blocks = bufsize / STp->block_size;
1448 out:
1449 return retval;
1453 /* Can be called more than once after each setup_buffer() */
1454 static void release_buffering(struct scsi_tape *STp, int is_read)
1456 struct st_buffer *STbp;
1458 STbp = STp->buffer;
1459 if (STbp->do_dio) {
1460 sgl_unmap_user_pages(&(STbp->sg[0]), STbp->do_dio, is_read);
1461 STbp->do_dio = 0;
1462 STbp->sg_segs = 0;
1467 /* Write command */
1468 static ssize_t
1469 st_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
1471 ssize_t total;
1472 ssize_t i, do_count, blks, transfer;
1473 ssize_t retval;
1474 int undone, retry_eot = 0, scode;
1475 int async_write;
1476 unsigned char cmd[MAX_COMMAND_SIZE];
1477 const char __user *b_point;
1478 struct st_request *SRpnt = NULL;
1479 struct scsi_tape *STp = filp->private_data;
1480 struct st_modedef *STm;
1481 struct st_partstat *STps;
1482 struct st_buffer *STbp;
1483 char *name = tape_name(STp);
1485 if (down_interruptible(&STp->lock))
1486 return -ERESTARTSYS;
1488 retval = rw_checks(STp, filp, count);
1489 if (retval || count == 0)
1490 goto out;
1492 /* Write must be integral number of blocks */
1493 if (STp->block_size != 0 && (count % STp->block_size) != 0) {
1494 printk(KERN_WARNING "%s: Write not multiple of tape block size.\n",
1495 name);
1496 retval = (-EINVAL);
1497 goto out;
1500 STm = &(STp->modes[STp->current_mode]);
1501 STps = &(STp->ps[STp->partition]);
1503 if (STp->write_prot) {
1504 retval = (-EACCES);
1505 goto out;
1509 if (STps->rw == ST_READING) {
1510 retval = flush_buffer(STp, 0);
1511 if (retval)
1512 goto out;
1513 STps->rw = ST_WRITING;
1514 } else if (STps->rw != ST_WRITING &&
1515 STps->drv_file == 0 && STps->drv_block == 0) {
1516 if ((retval = set_mode_densblk(STp, STm)) < 0)
1517 goto out;
1518 if (STm->default_compression != ST_DONT_TOUCH &&
1519 !(STp->compression_changed)) {
1520 if (st_compression(STp, (STm->default_compression == ST_YES))) {
1521 printk(KERN_WARNING "%s: Can't set default compression.\n",
1522 name);
1523 if (modes_defined) {
1524 retval = (-EINVAL);
1525 goto out;
1531 STbp = STp->buffer;
1532 i = write_behind_check(STp);
1533 if (i) {
1534 if (i == -ENOSPC)
1535 STps->eof = ST_EOM_OK;
1536 else
1537 STps->eof = ST_EOM_ERROR;
1540 if (STps->eof == ST_EOM_OK) {
1541 STps->eof = ST_EOD_1; /* allow next write */
1542 retval = (-ENOSPC);
1543 goto out;
1545 else if (STps->eof == ST_EOM_ERROR) {
1546 retval = (-EIO);
1547 goto out;
1550 /* Check the buffer readability in cases where copy_user might catch
1551 the problems after some tape movement. */
1552 if (STp->block_size != 0 &&
1553 !STbp->do_dio &&
1554 (copy_from_user(&i, buf, 1) != 0 ||
1555 copy_from_user(&i, buf + count - 1, 1) != 0)) {
1556 retval = (-EFAULT);
1557 goto out;
1560 retval = setup_buffering(STp, buf, count, 0);
1561 if (retval)
1562 goto out;
1564 total = count;
1566 memset(cmd, 0, MAX_COMMAND_SIZE);
1567 cmd[0] = WRITE_6;
1568 cmd[1] = (STp->block_size != 0);
1570 STps->rw = ST_WRITING;
1572 b_point = buf;
1573 while (count > 0 && !retry_eot) {
1575 if (STbp->do_dio) {
1576 do_count = count;
1578 else {
1579 if (STp->block_size == 0)
1580 do_count = count;
1581 else {
1582 do_count = STbp->buffer_blocks * STp->block_size -
1583 STbp->buffer_bytes;
1584 if (do_count > count)
1585 do_count = count;
1588 i = append_to_buffer(b_point, STbp, do_count);
1589 if (i) {
1590 retval = i;
1591 goto out;
1594 count -= do_count;
1595 b_point += do_count;
1597 async_write = STp->block_size == 0 && !STbp->do_dio &&
1598 STm->do_async_writes && STps->eof < ST_EOM_OK;
1600 if (STp->block_size != 0 && STm->do_buffer_writes &&
1601 !(STp->try_dio && try_wdio) && STps->eof < ST_EOM_OK &&
1602 STbp->buffer_bytes < STbp->buffer_size) {
1603 STp->dirty = 1;
1604 /* Don't write a buffer that is not full enough. */
1605 if (!async_write && count == 0)
1606 break;
1609 retry_write:
1610 if (STp->block_size == 0)
1611 blks = transfer = do_count;
1612 else {
1613 if (!STbp->do_dio)
1614 blks = STbp->buffer_bytes;
1615 else
1616 blks = do_count;
1617 blks /= STp->block_size;
1618 transfer = blks * STp->block_size;
1620 cmd[2] = blks >> 16;
1621 cmd[3] = blks >> 8;
1622 cmd[4] = blks;
1624 SRpnt = st_do_scsi(SRpnt, STp, cmd, transfer, DMA_TO_DEVICE,
1625 STp->device->timeout, MAX_WRITE_RETRIES, !async_write);
1626 if (!SRpnt) {
1627 retval = STbp->syscall_result;
1628 goto out;
1630 if (async_write && !STbp->syscall_result) {
1631 STbp->writing = transfer;
1632 STp->dirty = !(STbp->writing ==
1633 STbp->buffer_bytes);
1634 SRpnt = NULL; /* Prevent releasing this request! */
1635 DEB( STp->write_pending = 1; )
1636 break;
1639 if (STbp->syscall_result != 0) {
1640 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
1642 DEBC(printk(ST_DEB_MSG "%s: Error on write:\n", name));
1643 if (cmdstatp->have_sense && (cmdstatp->flags & SENSE_EOM)) {
1644 scode = cmdstatp->sense_hdr.sense_key;
1645 if (cmdstatp->remainder_valid)
1646 undone = (int)cmdstatp->uremainder64;
1647 else if (STp->block_size == 0 &&
1648 scode == VOLUME_OVERFLOW)
1649 undone = transfer;
1650 else
1651 undone = 0;
1652 if (STp->block_size != 0)
1653 undone *= STp->block_size;
1654 if (undone <= do_count) {
1655 /* Only data from this write is not written */
1656 count += undone;
1657 do_count -= undone;
1658 if (STp->block_size)
1659 blks = (transfer - undone) / STp->block_size;
1660 STps->eof = ST_EOM_OK;
1661 /* Continue in fixed block mode if all written
1662 in this request but still something left to write
1663 (retval left to zero)
1665 if (STp->block_size == 0 ||
1666 undone > 0 || count == 0)
1667 retval = (-ENOSPC); /* EOM within current request */
1668 DEBC(printk(ST_DEB_MSG
1669 "%s: EOM with %d bytes unwritten.\n",
1670 name, (int)count));
1671 } else {
1672 /* EOT within data buffered earlier (possible only
1673 in fixed block mode without direct i/o) */
1674 if (!retry_eot && !cmdstatp->deferred &&
1675 (scode == NO_SENSE || scode == RECOVERED_ERROR)) {
1676 move_buffer_data(STp->buffer, transfer - undone);
1677 retry_eot = 1;
1678 if (STps->drv_block >= 0) {
1679 STps->drv_block += (transfer - undone) /
1680 STp->block_size;
1682 STps->eof = ST_EOM_OK;
1683 DEBC(printk(ST_DEB_MSG
1684 "%s: Retry write of %d bytes at EOM.\n",
1685 name, STp->buffer->buffer_bytes));
1686 goto retry_write;
1688 else {
1689 /* Either error within data buffered by driver or
1690 failed retry */
1691 count -= do_count;
1692 blks = do_count = 0;
1693 STps->eof = ST_EOM_ERROR;
1694 STps->drv_block = (-1); /* Too cautious? */
1695 retval = (-EIO); /* EOM for old data */
1696 DEBC(printk(ST_DEB_MSG
1697 "%s: EOM with lost data.\n",
1698 name));
1701 } else {
1702 count += do_count;
1703 STps->drv_block = (-1); /* Too cautious? */
1704 retval = STbp->syscall_result;
1709 if (STps->drv_block >= 0) {
1710 if (STp->block_size == 0)
1711 STps->drv_block += (do_count > 0);
1712 else
1713 STps->drv_block += blks;
1716 STbp->buffer_bytes = 0;
1717 STp->dirty = 0;
1719 if (retval || retry_eot) {
1720 if (count < total)
1721 retval = total - count;
1722 goto out;
1726 if (STps->eof == ST_EOD_1)
1727 STps->eof = ST_EOM_OK;
1728 else if (STps->eof != ST_EOM_OK)
1729 STps->eof = ST_NOEOF;
1730 retval = total - count;
1732 out:
1733 if (SRpnt != NULL)
1734 st_release_request(SRpnt);
1735 release_buffering(STp, 0);
1736 up(&STp->lock);
1738 return retval;
1741 /* Read data from the tape. Returns zero in the normal case, one if the
1742 eof status has changed, and the negative error code in case of a
1743 fatal error. Otherwise updates the buffer and the eof state.
1745 Does release user buffer mapping if it is set.
1747 static long read_tape(struct scsi_tape *STp, long count,
1748 struct st_request ** aSRpnt)
1750 int transfer, blks, bytes;
1751 unsigned char cmd[MAX_COMMAND_SIZE];
1752 struct st_request *SRpnt;
1753 struct st_modedef *STm;
1754 struct st_partstat *STps;
1755 struct st_buffer *STbp;
1756 int retval = 0;
1757 char *name = tape_name(STp);
1759 if (count == 0)
1760 return 0;
1762 STm = &(STp->modes[STp->current_mode]);
1763 STps = &(STp->ps[STp->partition]);
1764 if (STps->eof == ST_FM_HIT)
1765 return 1;
1766 STbp = STp->buffer;
1768 if (STp->block_size == 0)
1769 blks = bytes = count;
1770 else {
1771 if (!(STp->try_dio && try_rdio) && STm->do_read_ahead) {
1772 blks = (STp->buffer)->buffer_blocks;
1773 bytes = blks * STp->block_size;
1774 } else {
1775 bytes = count;
1776 if (!STbp->do_dio && bytes > (STp->buffer)->buffer_size)
1777 bytes = (STp->buffer)->buffer_size;
1778 blks = bytes / STp->block_size;
1779 bytes = blks * STp->block_size;
1783 memset(cmd, 0, MAX_COMMAND_SIZE);
1784 cmd[0] = READ_6;
1785 cmd[1] = (STp->block_size != 0);
1786 cmd[2] = blks >> 16;
1787 cmd[3] = blks >> 8;
1788 cmd[4] = blks;
1790 SRpnt = *aSRpnt;
1791 SRpnt = st_do_scsi(SRpnt, STp, cmd, bytes, DMA_FROM_DEVICE,
1792 STp->device->timeout, MAX_RETRIES, 1);
1793 release_buffering(STp, 1);
1794 *aSRpnt = SRpnt;
1795 if (!SRpnt)
1796 return STbp->syscall_result;
1798 STbp->read_pointer = 0;
1799 STps->at_sm = 0;
1801 /* Something to check */
1802 if (STbp->syscall_result) {
1803 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
1805 retval = 1;
1806 DEBC(printk(ST_DEB_MSG "%s: Sense: %2x %2x %2x %2x %2x %2x %2x %2x\n",
1807 name,
1808 SRpnt->sense[0], SRpnt->sense[1],
1809 SRpnt->sense[2], SRpnt->sense[3],
1810 SRpnt->sense[4], SRpnt->sense[5],
1811 SRpnt->sense[6], SRpnt->sense[7]));
1812 if (cmdstatp->have_sense) {
1814 if (cmdstatp->sense_hdr.sense_key == BLANK_CHECK)
1815 cmdstatp->flags &= 0xcf; /* No need for EOM in this case */
1817 if (cmdstatp->flags != 0) { /* EOF, EOM, or ILI */
1818 /* Compute the residual count */
1819 if (cmdstatp->remainder_valid)
1820 transfer = (int)cmdstatp->uremainder64;
1821 else
1822 transfer = 0;
1823 if (STp->block_size == 0 &&
1824 cmdstatp->sense_hdr.sense_key == MEDIUM_ERROR)
1825 transfer = bytes;
1827 if (cmdstatp->flags & SENSE_ILI) { /* ILI */
1828 if (STp->block_size == 0) {
1829 if (transfer <= 0) {
1830 if (transfer < 0)
1831 printk(KERN_NOTICE
1832 "%s: Failed to read %d byte block with %d byte transfer.\n",
1833 name, bytes - transfer, bytes);
1834 if (STps->drv_block >= 0)
1835 STps->drv_block += 1;
1836 STbp->buffer_bytes = 0;
1837 return (-ENOMEM);
1839 STbp->buffer_bytes = bytes - transfer;
1840 } else {
1841 st_release_request(SRpnt);
1842 SRpnt = *aSRpnt = NULL;
1843 if (transfer == blks) { /* We did not get anything, error */
1844 printk(KERN_NOTICE "%s: Incorrect block size.\n", name);
1845 if (STps->drv_block >= 0)
1846 STps->drv_block += blks - transfer + 1;
1847 st_int_ioctl(STp, MTBSR, 1);
1848 return (-EIO);
1850 /* We have some data, deliver it */
1851 STbp->buffer_bytes = (blks - transfer) *
1852 STp->block_size;
1853 DEBC(printk(ST_DEB_MSG
1854 "%s: ILI but enough data received %ld %d.\n",
1855 name, count, STbp->buffer_bytes));
1856 if (STps->drv_block >= 0)
1857 STps->drv_block += 1;
1858 if (st_int_ioctl(STp, MTBSR, 1))
1859 return (-EIO);
1861 } else if (cmdstatp->flags & SENSE_FMK) { /* FM overrides EOM */
1862 if (STps->eof != ST_FM_HIT)
1863 STps->eof = ST_FM_HIT;
1864 else
1865 STps->eof = ST_EOD_2;
1866 if (STp->block_size == 0)
1867 STbp->buffer_bytes = 0;
1868 else
1869 STbp->buffer_bytes =
1870 bytes - transfer * STp->block_size;
1871 DEBC(printk(ST_DEB_MSG
1872 "%s: EOF detected (%d bytes read).\n",
1873 name, STbp->buffer_bytes));
1874 } else if (cmdstatp->flags & SENSE_EOM) {
1875 if (STps->eof == ST_FM)
1876 STps->eof = ST_EOD_1;
1877 else
1878 STps->eof = ST_EOM_OK;
1879 if (STp->block_size == 0)
1880 STbp->buffer_bytes = bytes - transfer;
1881 else
1882 STbp->buffer_bytes =
1883 bytes - transfer * STp->block_size;
1885 DEBC(printk(ST_DEB_MSG "%s: EOM detected (%d bytes read).\n",
1886 name, STbp->buffer_bytes));
1889 /* end of EOF, EOM, ILI test */
1890 else { /* nonzero sense key */
1891 DEBC(printk(ST_DEB_MSG
1892 "%s: Tape error while reading.\n", name));
1893 STps->drv_block = (-1);
1894 if (STps->eof == ST_FM &&
1895 cmdstatp->sense_hdr.sense_key == BLANK_CHECK) {
1896 DEBC(printk(ST_DEB_MSG
1897 "%s: Zero returned for first BLANK CHECK after EOF.\n",
1898 name));
1899 STps->eof = ST_EOD_2; /* First BLANK_CHECK after FM */
1900 } else /* Some other extended sense code */
1901 retval = (-EIO);
1904 if (STbp->buffer_bytes < 0) /* Caused by bogus sense data */
1905 STbp->buffer_bytes = 0;
1907 /* End of extended sense test */
1908 else { /* Non-extended sense */
1909 retval = STbp->syscall_result;
1913 /* End of error handling */
1914 else /* Read successful */
1915 STbp->buffer_bytes = bytes;
1917 if (STps->drv_block >= 0) {
1918 if (STp->block_size == 0)
1919 STps->drv_block++;
1920 else
1921 STps->drv_block += STbp->buffer_bytes / STp->block_size;
1923 return retval;
1927 /* Read command */
1928 static ssize_t
1929 st_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
1931 ssize_t total;
1932 ssize_t retval = 0;
1933 ssize_t i, transfer;
1934 int special, do_dio = 0;
1935 struct st_request *SRpnt = NULL;
1936 struct scsi_tape *STp = filp->private_data;
1937 struct st_modedef *STm;
1938 struct st_partstat *STps;
1939 struct st_buffer *STbp = STp->buffer;
1940 DEB( char *name = tape_name(STp); )
1942 if (down_interruptible(&STp->lock))
1943 return -ERESTARTSYS;
1945 retval = rw_checks(STp, filp, count);
1946 if (retval || count == 0)
1947 goto out;
1949 STm = &(STp->modes[STp->current_mode]);
1950 if (!(STm->do_read_ahead) && STp->block_size != 0 &&
1951 (count % STp->block_size) != 0) {
1952 retval = (-EINVAL); /* Read must be integral number of blocks */
1953 goto out;
1956 STps = &(STp->ps[STp->partition]);
1957 if (STps->rw == ST_WRITING) {
1958 retval = flush_buffer(STp, 0);
1959 if (retval)
1960 goto out;
1961 STps->rw = ST_READING;
1963 DEB(
1964 if (debugging && STps->eof != ST_NOEOF)
1965 printk(ST_DEB_MSG "%s: EOF/EOM flag up (%d). Bytes %d\n", name,
1966 STps->eof, STbp->buffer_bytes);
1967 ) /* end DEB */
1969 retval = setup_buffering(STp, buf, count, 1);
1970 if (retval)
1971 goto out;
1972 do_dio = STbp->do_dio;
1974 if (STbp->buffer_bytes == 0 &&
1975 STps->eof >= ST_EOD_1) {
1976 if (STps->eof < ST_EOD) {
1977 STps->eof += 1;
1978 retval = 0;
1979 goto out;
1981 retval = (-EIO); /* EOM or Blank Check */
1982 goto out;
1985 if (do_dio) {
1986 /* Check the buffer writability before any tape movement. Don't alter
1987 buffer data. */
1988 if (copy_from_user(&i, buf, 1) != 0 ||
1989 copy_to_user(buf, &i, 1) != 0 ||
1990 copy_from_user(&i, buf + count - 1, 1) != 0 ||
1991 copy_to_user(buf + count - 1, &i, 1) != 0) {
1992 retval = (-EFAULT);
1993 goto out;
1997 STps->rw = ST_READING;
2000 /* Loop until enough data in buffer or a special condition found */
2001 for (total = 0, special = 0; total < count && !special;) {
2003 /* Get new data if the buffer is empty */
2004 if (STbp->buffer_bytes == 0) {
2005 special = read_tape(STp, count - total, &SRpnt);
2006 if (special < 0) { /* No need to continue read */
2007 retval = special;
2008 goto out;
2012 /* Move the data from driver buffer to user buffer */
2013 if (STbp->buffer_bytes > 0) {
2014 DEB(
2015 if (debugging && STps->eof != ST_NOEOF)
2016 printk(ST_DEB_MSG
2017 "%s: EOF up (%d). Left %d, needed %d.\n", name,
2018 STps->eof, STbp->buffer_bytes,
2019 (int)(count - total));
2020 ) /* end DEB */
2021 transfer = STbp->buffer_bytes < count - total ?
2022 STbp->buffer_bytes : count - total;
2023 if (!do_dio) {
2024 i = from_buffer(STbp, buf, transfer);
2025 if (i) {
2026 retval = i;
2027 goto out;
2030 buf += transfer;
2031 total += transfer;
2034 if (STp->block_size == 0)
2035 break; /* Read only one variable length block */
2037 } /* for (total = 0, special = 0;
2038 total < count && !special; ) */
2040 /* Change the eof state if no data from tape or buffer */
2041 if (total == 0) {
2042 if (STps->eof == ST_FM_HIT) {
2043 STps->eof = ST_FM;
2044 STps->drv_block = 0;
2045 if (STps->drv_file >= 0)
2046 STps->drv_file++;
2047 } else if (STps->eof == ST_EOD_1) {
2048 STps->eof = ST_EOD_2;
2049 STps->drv_block = 0;
2050 if (STps->drv_file >= 0)
2051 STps->drv_file++;
2052 } else if (STps->eof == ST_EOD_2)
2053 STps->eof = ST_EOD;
2054 } else if (STps->eof == ST_FM)
2055 STps->eof = ST_NOEOF;
2056 retval = total;
2058 out:
2059 if (SRpnt != NULL) {
2060 st_release_request(SRpnt);
2061 SRpnt = NULL;
2063 if (do_dio) {
2064 release_buffering(STp, 1);
2065 STbp->buffer_bytes = 0;
2067 up(&STp->lock);
2069 return retval;
2074 DEB(
2075 /* Set the driver options */
2076 static void st_log_options(struct scsi_tape * STp, struct st_modedef * STm, char *name)
2078 if (debugging) {
2079 printk(KERN_INFO
2080 "%s: Mode %d options: buffer writes: %d, async writes: %d, read ahead: %d\n",
2081 name, STp->current_mode, STm->do_buffer_writes, STm->do_async_writes,
2082 STm->do_read_ahead);
2083 printk(KERN_INFO
2084 "%s: can bsr: %d, two FMs: %d, fast mteom: %d, auto lock: %d,\n",
2085 name, STp->can_bsr, STp->two_fm, STp->fast_mteom, STp->do_auto_lock);
2086 printk(KERN_INFO
2087 "%s: defs for wr: %d, no block limits: %d, partitions: %d, s2 log: %d\n",
2088 name, STm->defaults_for_writes, STp->omit_blklims, STp->can_partitions,
2089 STp->scsi2_logical);
2090 printk(KERN_INFO
2091 "%s: sysv: %d nowait: %d\n", name, STm->sysv, STp->immediate);
2092 printk(KERN_INFO "%s: debugging: %d\n",
2093 name, debugging);
2099 static int st_set_options(struct scsi_tape *STp, long options)
2101 int value;
2102 long code;
2103 struct st_modedef *STm;
2104 char *name = tape_name(STp);
2105 struct cdev *cd0, *cd1;
2107 STm = &(STp->modes[STp->current_mode]);
2108 if (!STm->defined) {
2109 cd0 = STm->cdevs[0]; cd1 = STm->cdevs[1];
2110 memcpy(STm, &(STp->modes[0]), sizeof(struct st_modedef));
2111 STm->cdevs[0] = cd0; STm->cdevs[1] = cd1;
2112 modes_defined = 1;
2113 DEBC(printk(ST_DEB_MSG
2114 "%s: Initialized mode %d definition from mode 0\n",
2115 name, STp->current_mode));
2118 code = options & MT_ST_OPTIONS;
2119 if (code == MT_ST_BOOLEANS) {
2120 STm->do_buffer_writes = (options & MT_ST_BUFFER_WRITES) != 0;
2121 STm->do_async_writes = (options & MT_ST_ASYNC_WRITES) != 0;
2122 STm->defaults_for_writes = (options & MT_ST_DEF_WRITES) != 0;
2123 STm->do_read_ahead = (options & MT_ST_READ_AHEAD) != 0;
2124 STp->two_fm = (options & MT_ST_TWO_FM) != 0;
2125 STp->fast_mteom = (options & MT_ST_FAST_MTEOM) != 0;
2126 STp->do_auto_lock = (options & MT_ST_AUTO_LOCK) != 0;
2127 STp->can_bsr = (options & MT_ST_CAN_BSR) != 0;
2128 STp->omit_blklims = (options & MT_ST_NO_BLKLIMS) != 0;
2129 if ((STp->device)->scsi_level >= SCSI_2)
2130 STp->can_partitions = (options & MT_ST_CAN_PARTITIONS) != 0;
2131 STp->scsi2_logical = (options & MT_ST_SCSI2LOGICAL) != 0;
2132 STp->immediate = (options & MT_ST_NOWAIT) != 0;
2133 STm->sysv = (options & MT_ST_SYSV) != 0;
2134 DEB( debugging = (options & MT_ST_DEBUGGING) != 0;
2135 st_log_options(STp, STm, name); )
2136 } else if (code == MT_ST_SETBOOLEANS || code == MT_ST_CLEARBOOLEANS) {
2137 value = (code == MT_ST_SETBOOLEANS);
2138 if ((options & MT_ST_BUFFER_WRITES) != 0)
2139 STm->do_buffer_writes = value;
2140 if ((options & MT_ST_ASYNC_WRITES) != 0)
2141 STm->do_async_writes = value;
2142 if ((options & MT_ST_DEF_WRITES) != 0)
2143 STm->defaults_for_writes = value;
2144 if ((options & MT_ST_READ_AHEAD) != 0)
2145 STm->do_read_ahead = value;
2146 if ((options & MT_ST_TWO_FM) != 0)
2147 STp->two_fm = value;
2148 if ((options & MT_ST_FAST_MTEOM) != 0)
2149 STp->fast_mteom = value;
2150 if ((options & MT_ST_AUTO_LOCK) != 0)
2151 STp->do_auto_lock = value;
2152 if ((options & MT_ST_CAN_BSR) != 0)
2153 STp->can_bsr = value;
2154 if ((options & MT_ST_NO_BLKLIMS) != 0)
2155 STp->omit_blklims = value;
2156 if ((STp->device)->scsi_level >= SCSI_2 &&
2157 (options & MT_ST_CAN_PARTITIONS) != 0)
2158 STp->can_partitions = value;
2159 if ((options & MT_ST_SCSI2LOGICAL) != 0)
2160 STp->scsi2_logical = value;
2161 if ((options & MT_ST_NOWAIT) != 0)
2162 STp->immediate = value;
2163 if ((options & MT_ST_SYSV) != 0)
2164 STm->sysv = value;
2165 DEB(
2166 if ((options & MT_ST_DEBUGGING) != 0)
2167 debugging = value;
2168 st_log_options(STp, STm, name); )
2169 } else if (code == MT_ST_WRITE_THRESHOLD) {
2170 /* Retained for compatibility */
2171 } else if (code == MT_ST_DEF_BLKSIZE) {
2172 value = (options & ~MT_ST_OPTIONS);
2173 if (value == ~MT_ST_OPTIONS) {
2174 STm->default_blksize = (-1);
2175 DEBC( printk(KERN_INFO "%s: Default block size disabled.\n", name));
2176 } else {
2177 STm->default_blksize = value;
2178 DEBC( printk(KERN_INFO "%s: Default block size set to %d bytes.\n",
2179 name, STm->default_blksize));
2180 if (STp->ready == ST_READY) {
2181 STp->blksize_changed = 0;
2182 set_mode_densblk(STp, STm);
2185 } else if (code == MT_ST_TIMEOUTS) {
2186 value = (options & ~MT_ST_OPTIONS);
2187 if ((value & MT_ST_SET_LONG_TIMEOUT) != 0) {
2188 STp->long_timeout = (value & ~MT_ST_SET_LONG_TIMEOUT) * HZ;
2189 DEBC( printk(KERN_INFO "%s: Long timeout set to %d seconds.\n", name,
2190 (value & ~MT_ST_SET_LONG_TIMEOUT)));
2191 } else {
2192 STp->device->timeout = value * HZ;
2193 DEBC( printk(KERN_INFO "%s: Normal timeout set to %d seconds.\n",
2194 name, value) );
2196 } else if (code == MT_ST_SET_CLN) {
2197 value = (options & ~MT_ST_OPTIONS) & 0xff;
2198 if (value != 0 &&
2199 value < EXTENDED_SENSE_START && value >= SCSI_SENSE_BUFFERSIZE)
2200 return (-EINVAL);
2201 STp->cln_mode = value;
2202 STp->cln_sense_mask = (options >> 8) & 0xff;
2203 STp->cln_sense_value = (options >> 16) & 0xff;
2204 printk(KERN_INFO
2205 "%s: Cleaning request mode %d, mask %02x, value %02x\n",
2206 name, value, STp->cln_sense_mask, STp->cln_sense_value);
2207 } else if (code == MT_ST_DEF_OPTIONS) {
2208 code = (options & ~MT_ST_CLEAR_DEFAULT);
2209 value = (options & MT_ST_CLEAR_DEFAULT);
2210 if (code == MT_ST_DEF_DENSITY) {
2211 if (value == MT_ST_CLEAR_DEFAULT) {
2212 STm->default_density = (-1);
2213 DEBC( printk(KERN_INFO "%s: Density default disabled.\n",
2214 name));
2215 } else {
2216 STm->default_density = value & 0xff;
2217 DEBC( printk(KERN_INFO "%s: Density default set to %x\n",
2218 name, STm->default_density));
2219 if (STp->ready == ST_READY) {
2220 STp->density_changed = 0;
2221 set_mode_densblk(STp, STm);
2224 } else if (code == MT_ST_DEF_DRVBUFFER) {
2225 if (value == MT_ST_CLEAR_DEFAULT) {
2226 STp->default_drvbuffer = 0xff;
2227 DEBC( printk(KERN_INFO
2228 "%s: Drive buffer default disabled.\n", name));
2229 } else {
2230 STp->default_drvbuffer = value & 7;
2231 DEBC( printk(KERN_INFO
2232 "%s: Drive buffer default set to %x\n",
2233 name, STp->default_drvbuffer));
2234 if (STp->ready == ST_READY)
2235 st_int_ioctl(STp, MTSETDRVBUFFER, STp->default_drvbuffer);
2237 } else if (code == MT_ST_DEF_COMPRESSION) {
2238 if (value == MT_ST_CLEAR_DEFAULT) {
2239 STm->default_compression = ST_DONT_TOUCH;
2240 DEBC( printk(KERN_INFO
2241 "%s: Compression default disabled.\n", name));
2242 } else {
2243 if ((value & 0xff00) != 0) {
2244 STp->c_algo = (value & 0xff00) >> 8;
2245 DEBC( printk(KERN_INFO "%s: Compression algorithm set to 0x%x.\n",
2246 name, STp->c_algo));
2248 if ((value & 0xff) != 0xff) {
2249 STm->default_compression = (value & 1 ? ST_YES : ST_NO);
2250 DEBC( printk(KERN_INFO "%s: Compression default set to %x\n",
2251 name, (value & 1)));
2252 if (STp->ready == ST_READY) {
2253 STp->compression_changed = 0;
2254 st_compression(STp, (STm->default_compression == ST_YES));
2259 } else
2260 return (-EIO);
2262 return 0;
2265 #define MODE_HEADER_LENGTH 4
2267 /* Mode header and page byte offsets */
2268 #define MH_OFF_DATA_LENGTH 0
2269 #define MH_OFF_MEDIUM_TYPE 1
2270 #define MH_OFF_DEV_SPECIFIC 2
2271 #define MH_OFF_BDESCS_LENGTH 3
2272 #define MP_OFF_PAGE_NBR 0
2273 #define MP_OFF_PAGE_LENGTH 1
2275 /* Mode header and page bit masks */
2276 #define MH_BIT_WP 0x80
2277 #define MP_MSK_PAGE_NBR 0x3f
2279 /* Don't return block descriptors */
2280 #define MODE_SENSE_OMIT_BDESCS 0x08
2282 #define MODE_SELECT_PAGE_FORMAT 0x10
2284 /* Read a mode page into the tape buffer. The block descriptors are included
2285 if incl_block_descs is true. The page control is ored to the page number
2286 parameter, if necessary. */
2287 static int read_mode_page(struct scsi_tape *STp, int page, int omit_block_descs)
2289 unsigned char cmd[MAX_COMMAND_SIZE];
2290 struct st_request *SRpnt = NULL;
2292 memset(cmd, 0, MAX_COMMAND_SIZE);
2293 cmd[0] = MODE_SENSE;
2294 if (omit_block_descs)
2295 cmd[1] = MODE_SENSE_OMIT_BDESCS;
2296 cmd[2] = page;
2297 cmd[4] = 255;
2299 SRpnt = st_do_scsi(SRpnt, STp, cmd, cmd[4], DMA_FROM_DEVICE,
2300 STp->device->timeout, 0, 1);
2301 if (SRpnt == NULL)
2302 return (STp->buffer)->syscall_result;
2304 st_release_request(SRpnt);
2306 return (STp->buffer)->syscall_result;
2310 /* Send the mode page in the tape buffer to the drive. Assumes that the mode data
2311 in the buffer is correctly formatted. The long timeout is used if slow is non-zero. */
2312 static int write_mode_page(struct scsi_tape *STp, int page, int slow)
2314 int pgo;
2315 unsigned char cmd[MAX_COMMAND_SIZE];
2316 struct st_request *SRpnt = NULL;
2318 memset(cmd, 0, MAX_COMMAND_SIZE);
2319 cmd[0] = MODE_SELECT;
2320 cmd[1] = MODE_SELECT_PAGE_FORMAT;
2321 pgo = MODE_HEADER_LENGTH + (STp->buffer)->b_data[MH_OFF_BDESCS_LENGTH];
2322 cmd[4] = pgo + (STp->buffer)->b_data[pgo + MP_OFF_PAGE_LENGTH] + 2;
2324 /* Clear reserved fields */
2325 (STp->buffer)->b_data[MH_OFF_DATA_LENGTH] = 0;
2326 (STp->buffer)->b_data[MH_OFF_MEDIUM_TYPE] = 0;
2327 (STp->buffer)->b_data[MH_OFF_DEV_SPECIFIC] &= ~MH_BIT_WP;
2328 (STp->buffer)->b_data[pgo + MP_OFF_PAGE_NBR] &= MP_MSK_PAGE_NBR;
2330 SRpnt = st_do_scsi(SRpnt, STp, cmd, cmd[4], DMA_TO_DEVICE,
2331 (slow ? STp->long_timeout : STp->device->timeout), 0, 1);
2332 if (SRpnt == NULL)
2333 return (STp->buffer)->syscall_result;
2335 st_release_request(SRpnt);
2337 return (STp->buffer)->syscall_result;
2341 #define COMPRESSION_PAGE 0x0f
2342 #define COMPRESSION_PAGE_LENGTH 16
2344 #define CP_OFF_DCE_DCC 2
2345 #define CP_OFF_C_ALGO 7
2347 #define DCE_MASK 0x80
2348 #define DCC_MASK 0x40
2349 #define RED_MASK 0x60
2352 /* Control the compression with mode page 15. Algorithm not changed if zero.
2354 The block descriptors are read and written because Sony SDT-7000 does not
2355 work without this (suggestion from Michael Schaefer <Michael.Schaefer@dlr.de>).
2356 Including block descriptors should not cause any harm to other drives. */
2358 static int st_compression(struct scsi_tape * STp, int state)
2360 int retval;
2361 int mpoffs; /* Offset to mode page start */
2362 unsigned char *b_data = (STp->buffer)->b_data;
2363 DEB( char *name = tape_name(STp); )
2365 if (STp->ready != ST_READY)
2366 return (-EIO);
2368 /* Read the current page contents */
2369 retval = read_mode_page(STp, COMPRESSION_PAGE, 0);
2370 if (retval) {
2371 DEBC(printk(ST_DEB_MSG "%s: Compression mode page not supported.\n",
2372 name));
2373 return (-EIO);
2376 mpoffs = MODE_HEADER_LENGTH + b_data[MH_OFF_BDESCS_LENGTH];
2377 DEBC(printk(ST_DEB_MSG "%s: Compression state is %d.\n", name,
2378 (b_data[mpoffs + CP_OFF_DCE_DCC] & DCE_MASK ? 1 : 0)));
2380 /* Check if compression can be changed */
2381 if ((b_data[mpoffs + CP_OFF_DCE_DCC] & DCC_MASK) == 0) {
2382 DEBC(printk(ST_DEB_MSG "%s: Compression not supported.\n", name));
2383 return (-EIO);
2386 /* Do the change */
2387 if (state) {
2388 b_data[mpoffs + CP_OFF_DCE_DCC] |= DCE_MASK;
2389 if (STp->c_algo != 0)
2390 b_data[mpoffs + CP_OFF_C_ALGO] = STp->c_algo;
2392 else {
2393 b_data[mpoffs + CP_OFF_DCE_DCC] &= ~DCE_MASK;
2394 if (STp->c_algo != 0)
2395 b_data[mpoffs + CP_OFF_C_ALGO] = 0; /* no compression */
2398 retval = write_mode_page(STp, COMPRESSION_PAGE, 0);
2399 if (retval) {
2400 DEBC(printk(ST_DEB_MSG "%s: Compression change failed.\n", name));
2401 return (-EIO);
2403 DEBC(printk(ST_DEB_MSG "%s: Compression state changed to %d.\n",
2404 name, state));
2406 STp->compression_changed = 1;
2407 return 0;
2411 /* Process the load and unload commands (does unload if the load code is zero) */
2412 static int do_load_unload(struct scsi_tape *STp, struct file *filp, int load_code)
2414 int retval = (-EIO), timeout;
2415 DEB( char *name = tape_name(STp); )
2416 unsigned char cmd[MAX_COMMAND_SIZE];
2417 struct st_partstat *STps;
2418 struct st_request *SRpnt;
2420 if (STp->ready != ST_READY && !load_code) {
2421 if (STp->ready == ST_NO_TAPE)
2422 return (-ENOMEDIUM);
2423 else
2424 return (-EIO);
2427 memset(cmd, 0, MAX_COMMAND_SIZE);
2428 cmd[0] = START_STOP;
2429 if (load_code)
2430 cmd[4] |= 1;
2432 * If arg >= 1 && arg <= 6 Enhanced load/unload in HP C1553A
2434 if (load_code >= 1 + MT_ST_HPLOADER_OFFSET
2435 && load_code <= 6 + MT_ST_HPLOADER_OFFSET) {
2436 DEBC(printk(ST_DEB_MSG "%s: Enhanced %sload slot %2d.\n",
2437 name, (cmd[4]) ? "" : "un",
2438 load_code - MT_ST_HPLOADER_OFFSET));
2439 cmd[3] = load_code - MT_ST_HPLOADER_OFFSET; /* MediaID field of C1553A */
2441 if (STp->immediate) {
2442 cmd[1] = 1; /* Don't wait for completion */
2443 timeout = STp->device->timeout;
2445 else
2446 timeout = STp->long_timeout;
2448 DEBC(
2449 if (!load_code)
2450 printk(ST_DEB_MSG "%s: Unloading tape.\n", name);
2451 else
2452 printk(ST_DEB_MSG "%s: Loading tape.\n", name);
2455 SRpnt = st_do_scsi(NULL, STp, cmd, 0, DMA_NONE,
2456 timeout, MAX_RETRIES, 1);
2457 if (!SRpnt)
2458 return (STp->buffer)->syscall_result;
2460 retval = (STp->buffer)->syscall_result;
2461 st_release_request(SRpnt);
2463 if (!retval) { /* SCSI command successful */
2465 if (!load_code) {
2466 STp->rew_at_close = 0;
2467 STp->ready = ST_NO_TAPE;
2469 else {
2470 STp->rew_at_close = STp->autorew_dev;
2471 retval = check_tape(STp, filp);
2472 if (retval > 0)
2473 retval = 0;
2476 else {
2477 STps = &(STp->ps[STp->partition]);
2478 STps->drv_file = STps->drv_block = (-1);
2481 return retval;
2484 #if DEBUG
2485 #define ST_DEB_FORWARD 0
2486 #define ST_DEB_BACKWARD 1
2487 static void deb_space_print(char *name, int direction, char *units, unsigned char *cmd)
2489 s32 sc;
2491 sc = cmd[2] & 0x80 ? 0xff000000 : 0;
2492 sc |= (cmd[2] << 16) | (cmd[3] << 8) | cmd[4];
2493 if (direction)
2494 sc = -sc;
2495 printk(ST_DEB_MSG "%s: Spacing tape %s over %d %s.\n", name,
2496 direction ? "backward" : "forward", sc, units);
2498 #endif
2501 /* Internal ioctl function */
2502 static int st_int_ioctl(struct scsi_tape *STp, unsigned int cmd_in, unsigned long arg)
2504 int timeout;
2505 long ltmp;
2506 int ioctl_result;
2507 int chg_eof = 1;
2508 unsigned char cmd[MAX_COMMAND_SIZE];
2509 struct st_request *SRpnt;
2510 struct st_partstat *STps;
2511 int fileno, blkno, at_sm, undone;
2512 int datalen = 0, direction = DMA_NONE;
2513 char *name = tape_name(STp);
2515 WARN_ON(STp->buffer->do_dio != 0);
2516 if (STp->ready != ST_READY) {
2517 if (STp->ready == ST_NO_TAPE)
2518 return (-ENOMEDIUM);
2519 else
2520 return (-EIO);
2522 timeout = STp->long_timeout;
2523 STps = &(STp->ps[STp->partition]);
2524 fileno = STps->drv_file;
2525 blkno = STps->drv_block;
2526 at_sm = STps->at_sm;
2528 memset(cmd, 0, MAX_COMMAND_SIZE);
2529 switch (cmd_in) {
2530 case MTFSFM:
2531 chg_eof = 0; /* Changed from the FSF after this */
2532 case MTFSF:
2533 cmd[0] = SPACE;
2534 cmd[1] = 0x01; /* Space FileMarks */
2535 cmd[2] = (arg >> 16);
2536 cmd[3] = (arg >> 8);
2537 cmd[4] = arg;
2538 DEBC(deb_space_print(name, ST_DEB_FORWARD, "filemarks", cmd);)
2539 if (fileno >= 0)
2540 fileno += arg;
2541 blkno = 0;
2542 at_sm &= (arg == 0);
2543 break;
2544 case MTBSFM:
2545 chg_eof = 0; /* Changed from the FSF after this */
2546 case MTBSF:
2547 cmd[0] = SPACE;
2548 cmd[1] = 0x01; /* Space FileMarks */
2549 ltmp = (-arg);
2550 cmd[2] = (ltmp >> 16);
2551 cmd[3] = (ltmp >> 8);
2552 cmd[4] = ltmp;
2553 DEBC(deb_space_print(name, ST_DEB_BACKWARD, "filemarks", cmd);)
2554 if (fileno >= 0)
2555 fileno -= arg;
2556 blkno = (-1); /* We can't know the block number */
2557 at_sm &= (arg == 0);
2558 break;
2559 case MTFSR:
2560 cmd[0] = SPACE;
2561 cmd[1] = 0x00; /* Space Blocks */
2562 cmd[2] = (arg >> 16);
2563 cmd[3] = (arg >> 8);
2564 cmd[4] = arg;
2565 DEBC(deb_space_print(name, ST_DEB_FORWARD, "blocks", cmd);)
2566 if (blkno >= 0)
2567 blkno += arg;
2568 at_sm &= (arg == 0);
2569 break;
2570 case MTBSR:
2571 cmd[0] = SPACE;
2572 cmd[1] = 0x00; /* Space Blocks */
2573 ltmp = (-arg);
2574 cmd[2] = (ltmp >> 16);
2575 cmd[3] = (ltmp >> 8);
2576 cmd[4] = ltmp;
2577 DEBC(deb_space_print(name, ST_DEB_BACKWARD, "blocks", cmd);)
2578 if (blkno >= 0)
2579 blkno -= arg;
2580 at_sm &= (arg == 0);
2581 break;
2582 case MTFSS:
2583 cmd[0] = SPACE;
2584 cmd[1] = 0x04; /* Space Setmarks */
2585 cmd[2] = (arg >> 16);
2586 cmd[3] = (arg >> 8);
2587 cmd[4] = arg;
2588 DEBC(deb_space_print(name, ST_DEB_FORWARD, "setmarks", cmd);)
2589 if (arg != 0) {
2590 blkno = fileno = (-1);
2591 at_sm = 1;
2593 break;
2594 case MTBSS:
2595 cmd[0] = SPACE;
2596 cmd[1] = 0x04; /* Space Setmarks */
2597 ltmp = (-arg);
2598 cmd[2] = (ltmp >> 16);
2599 cmd[3] = (ltmp >> 8);
2600 cmd[4] = ltmp;
2601 DEBC(deb_space_print(name, ST_DEB_BACKWARD, "setmarks", cmd);)
2602 if (arg != 0) {
2603 blkno = fileno = (-1);
2604 at_sm = 1;
2606 break;
2607 case MTWEOF:
2608 case MTWSM:
2609 if (STp->write_prot)
2610 return (-EACCES);
2611 cmd[0] = WRITE_FILEMARKS;
2612 if (cmd_in == MTWSM)
2613 cmd[1] = 2;
2614 cmd[2] = (arg >> 16);
2615 cmd[3] = (arg >> 8);
2616 cmd[4] = arg;
2617 timeout = STp->device->timeout;
2618 DEBC(
2619 if (cmd_in == MTWEOF)
2620 printk(ST_DEB_MSG "%s: Writing %d filemarks.\n", name,
2621 cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
2622 else
2623 printk(ST_DEB_MSG "%s: Writing %d setmarks.\n", name,
2624 cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
2626 if (fileno >= 0)
2627 fileno += arg;
2628 blkno = 0;
2629 at_sm = (cmd_in == MTWSM);
2630 break;
2631 case MTREW:
2632 cmd[0] = REZERO_UNIT;
2633 if (STp->immediate) {
2634 cmd[1] = 1; /* Don't wait for completion */
2635 timeout = STp->device->timeout;
2637 DEBC(printk(ST_DEB_MSG "%s: Rewinding tape.\n", name));
2638 fileno = blkno = at_sm = 0;
2639 break;
2640 case MTNOP:
2641 DEBC(printk(ST_DEB_MSG "%s: No op on tape.\n", name));
2642 return 0; /* Should do something ? */
2643 break;
2644 case MTRETEN:
2645 cmd[0] = START_STOP;
2646 if (STp->immediate) {
2647 cmd[1] = 1; /* Don't wait for completion */
2648 timeout = STp->device->timeout;
2650 cmd[4] = 3;
2651 DEBC(printk(ST_DEB_MSG "%s: Retensioning tape.\n", name));
2652 fileno = blkno = at_sm = 0;
2653 break;
2654 case MTEOM:
2655 if (!STp->fast_mteom) {
2656 /* space to the end of tape */
2657 ioctl_result = st_int_ioctl(STp, MTFSF, 0x7fffff);
2658 fileno = STps->drv_file;
2659 if (STps->eof >= ST_EOD_1)
2660 return 0;
2661 /* The next lines would hide the number of spaced FileMarks
2662 That's why I inserted the previous lines. I had no luck
2663 with detecting EOM with FSF, so we go now to EOM.
2664 Joerg Weule */
2665 } else
2666 fileno = (-1);
2667 cmd[0] = SPACE;
2668 cmd[1] = 3;
2669 DEBC(printk(ST_DEB_MSG "%s: Spacing to end of recorded medium.\n",
2670 name));
2671 blkno = -1;
2672 at_sm = 0;
2673 break;
2674 case MTERASE:
2675 if (STp->write_prot)
2676 return (-EACCES);
2677 cmd[0] = ERASE;
2678 cmd[1] = (arg ? 1 : 0); /* Long erase with non-zero argument */
2679 if (STp->immediate) {
2680 cmd[1] |= 2; /* Don't wait for completion */
2681 timeout = STp->device->timeout;
2683 else
2684 timeout = STp->long_timeout * 8;
2686 DEBC(printk(ST_DEB_MSG "%s: Erasing tape.\n", name));
2687 fileno = blkno = at_sm = 0;
2688 break;
2689 case MTSETBLK: /* Set block length */
2690 case MTSETDENSITY: /* Set tape density */
2691 case MTSETDRVBUFFER: /* Set drive buffering */
2692 case SET_DENS_AND_BLK: /* Set density and block size */
2693 chg_eof = 0;
2694 if (STp->dirty || (STp->buffer)->buffer_bytes != 0)
2695 return (-EIO); /* Not allowed if data in buffer */
2696 if ((cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) &&
2697 (arg & MT_ST_BLKSIZE_MASK) != 0 &&
2698 STp->max_block > 0 &&
2699 ((arg & MT_ST_BLKSIZE_MASK) < STp->min_block ||
2700 (arg & MT_ST_BLKSIZE_MASK) > STp->max_block)) {
2701 printk(KERN_WARNING "%s: Illegal block size.\n", name);
2702 return (-EINVAL);
2704 cmd[0] = MODE_SELECT;
2705 if ((STp->use_pf & USE_PF))
2706 cmd[1] = MODE_SELECT_PAGE_FORMAT;
2707 cmd[4] = datalen = 12;
2708 direction = DMA_TO_DEVICE;
2710 memset((STp->buffer)->b_data, 0, 12);
2711 if (cmd_in == MTSETDRVBUFFER)
2712 (STp->buffer)->b_data[2] = (arg & 7) << 4;
2713 else
2714 (STp->buffer)->b_data[2] =
2715 STp->drv_buffer << 4;
2716 (STp->buffer)->b_data[3] = 8; /* block descriptor length */
2717 if (cmd_in == MTSETDENSITY) {
2718 (STp->buffer)->b_data[4] = arg;
2719 STp->density_changed = 1; /* At least we tried ;-) */
2720 } else if (cmd_in == SET_DENS_AND_BLK)
2721 (STp->buffer)->b_data[4] = arg >> 24;
2722 else
2723 (STp->buffer)->b_data[4] = STp->density;
2724 if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) {
2725 ltmp = arg & MT_ST_BLKSIZE_MASK;
2726 if (cmd_in == MTSETBLK)
2727 STp->blksize_changed = 1; /* At least we tried ;-) */
2728 } else
2729 ltmp = STp->block_size;
2730 (STp->buffer)->b_data[9] = (ltmp >> 16);
2731 (STp->buffer)->b_data[10] = (ltmp >> 8);
2732 (STp->buffer)->b_data[11] = ltmp;
2733 timeout = STp->device->timeout;
2734 DEBC(
2735 if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK)
2736 printk(ST_DEB_MSG
2737 "%s: Setting block size to %d bytes.\n", name,
2738 (STp->buffer)->b_data[9] * 65536 +
2739 (STp->buffer)->b_data[10] * 256 +
2740 (STp->buffer)->b_data[11]);
2741 if (cmd_in == MTSETDENSITY || cmd_in == SET_DENS_AND_BLK)
2742 printk(ST_DEB_MSG
2743 "%s: Setting density code to %x.\n", name,
2744 (STp->buffer)->b_data[4]);
2745 if (cmd_in == MTSETDRVBUFFER)
2746 printk(ST_DEB_MSG
2747 "%s: Setting drive buffer code to %d.\n", name,
2748 ((STp->buffer)->b_data[2] >> 4) & 7);
2750 break;
2751 default:
2752 return (-ENOSYS);
2755 SRpnt = st_do_scsi(NULL, STp, cmd, datalen, direction,
2756 timeout, MAX_RETRIES, 1);
2757 if (!SRpnt)
2758 return (STp->buffer)->syscall_result;
2760 ioctl_result = (STp->buffer)->syscall_result;
2762 if (!ioctl_result) { /* SCSI command successful */
2763 st_release_request(SRpnt);
2764 SRpnt = NULL;
2765 STps->drv_block = blkno;
2766 STps->drv_file = fileno;
2767 STps->at_sm = at_sm;
2769 if (cmd_in == MTBSFM)
2770 ioctl_result = st_int_ioctl(STp, MTFSF, 1);
2771 else if (cmd_in == MTFSFM)
2772 ioctl_result = st_int_ioctl(STp, MTBSF, 1);
2774 if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) {
2775 int old_block_size = STp->block_size;
2776 STp->block_size = arg & MT_ST_BLKSIZE_MASK;
2777 if (STp->block_size != 0) {
2778 if (old_block_size == 0)
2779 normalize_buffer(STp->buffer);
2780 (STp->buffer)->buffer_blocks =
2781 (STp->buffer)->buffer_size / STp->block_size;
2783 (STp->buffer)->buffer_bytes = (STp->buffer)->read_pointer = 0;
2784 if (cmd_in == SET_DENS_AND_BLK)
2785 STp->density = arg >> MT_ST_DENSITY_SHIFT;
2786 } else if (cmd_in == MTSETDRVBUFFER)
2787 STp->drv_buffer = (arg & 7);
2788 else if (cmd_in == MTSETDENSITY)
2789 STp->density = arg;
2791 if (cmd_in == MTEOM)
2792 STps->eof = ST_EOD;
2793 else if (cmd_in == MTFSF)
2794 STps->eof = ST_FM;
2795 else if (chg_eof)
2796 STps->eof = ST_NOEOF;
2798 if (cmd_in == MTWEOF)
2799 STps->rw = ST_IDLE;
2800 } else { /* SCSI command was not completely successful. Don't return
2801 from this block without releasing the SCSI command block! */
2802 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
2804 if (cmdstatp->flags & SENSE_EOM) {
2805 if (cmd_in != MTBSF && cmd_in != MTBSFM &&
2806 cmd_in != MTBSR && cmd_in != MTBSS)
2807 STps->eof = ST_EOM_OK;
2808 STps->drv_block = 0;
2811 if (cmdstatp->remainder_valid)
2812 undone = (int)cmdstatp->uremainder64;
2813 else
2814 undone = 0;
2816 if (cmd_in == MTWEOF &&
2817 cmdstatp->have_sense &&
2818 (cmdstatp->flags & SENSE_EOM) &&
2819 (cmdstatp->sense_hdr.sense_key == NO_SENSE ||
2820 cmdstatp->sense_hdr.sense_key == RECOVERED_ERROR) &&
2821 undone == 0) {
2822 ioctl_result = 0; /* EOF written succesfully at EOM */
2823 if (fileno >= 0)
2824 fileno++;
2825 STps->drv_file = fileno;
2826 STps->eof = ST_NOEOF;
2827 } else if ((cmd_in == MTFSF) || (cmd_in == MTFSFM)) {
2828 if (fileno >= 0)
2829 STps->drv_file = fileno - undone;
2830 else
2831 STps->drv_file = fileno;
2832 STps->drv_block = -1;
2833 STps->eof = ST_NOEOF;
2834 } else if ((cmd_in == MTBSF) || (cmd_in == MTBSFM)) {
2835 if (arg > 0 && undone < 0) /* Some drives get this wrong */
2836 undone = (-undone);
2837 if (STps->drv_file >= 0)
2838 STps->drv_file = fileno + undone;
2839 STps->drv_block = 0;
2840 STps->eof = ST_NOEOF;
2841 } else if (cmd_in == MTFSR) {
2842 if (cmdstatp->flags & SENSE_FMK) { /* Hit filemark */
2843 if (STps->drv_file >= 0)
2844 STps->drv_file++;
2845 STps->drv_block = 0;
2846 STps->eof = ST_FM;
2847 } else {
2848 if (blkno >= undone)
2849 STps->drv_block = blkno - undone;
2850 else
2851 STps->drv_block = (-1);
2852 STps->eof = ST_NOEOF;
2854 } else if (cmd_in == MTBSR) {
2855 if (cmdstatp->flags & SENSE_FMK) { /* Hit filemark */
2856 STps->drv_file--;
2857 STps->drv_block = (-1);
2858 } else {
2859 if (arg > 0 && undone < 0) /* Some drives get this wrong */
2860 undone = (-undone);
2861 if (STps->drv_block >= 0)
2862 STps->drv_block = blkno + undone;
2864 STps->eof = ST_NOEOF;
2865 } else if (cmd_in == MTEOM) {
2866 STps->drv_file = (-1);
2867 STps->drv_block = (-1);
2868 STps->eof = ST_EOD;
2869 } else if (cmd_in == MTSETBLK ||
2870 cmd_in == MTSETDENSITY ||
2871 cmd_in == MTSETDRVBUFFER ||
2872 cmd_in == SET_DENS_AND_BLK) {
2873 if (cmdstatp->sense_hdr.sense_key == ILLEGAL_REQUEST &&
2874 !(STp->use_pf & PF_TESTED)) {
2875 /* Try the other possible state of Page Format if not
2876 already tried */
2877 STp->use_pf = !STp->use_pf | PF_TESTED;
2878 st_release_request(SRpnt);
2879 SRpnt = NULL;
2880 return st_int_ioctl(STp, cmd_in, arg);
2882 } else if (chg_eof)
2883 STps->eof = ST_NOEOF;
2885 if (cmdstatp->sense_hdr.sense_key == BLANK_CHECK)
2886 STps->eof = ST_EOD;
2888 st_release_request(SRpnt);
2889 SRpnt = NULL;
2892 return ioctl_result;
2896 /* Get the tape position. If bt == 2, arg points into a kernel space mt_loc
2897 structure. */
2899 static int get_location(struct scsi_tape *STp, unsigned int *block, int *partition,
2900 int logical)
2902 int result;
2903 unsigned char scmd[MAX_COMMAND_SIZE];
2904 struct st_request *SRpnt;
2905 DEB( char *name = tape_name(STp); )
2907 if (STp->ready != ST_READY)
2908 return (-EIO);
2910 memset(scmd, 0, MAX_COMMAND_SIZE);
2911 if ((STp->device)->scsi_level < SCSI_2) {
2912 scmd[0] = QFA_REQUEST_BLOCK;
2913 scmd[4] = 3;
2914 } else {
2915 scmd[0] = READ_POSITION;
2916 if (!logical && !STp->scsi2_logical)
2917 scmd[1] = 1;
2919 SRpnt = st_do_scsi(NULL, STp, scmd, 20, DMA_FROM_DEVICE,
2920 STp->device->timeout, MAX_READY_RETRIES, 1);
2921 if (!SRpnt)
2922 return (STp->buffer)->syscall_result;
2924 if ((STp->buffer)->syscall_result != 0 ||
2925 (STp->device->scsi_level >= SCSI_2 &&
2926 ((STp->buffer)->b_data[0] & 4) != 0)) {
2927 *block = *partition = 0;
2928 DEBC(printk(ST_DEB_MSG "%s: Can't read tape position.\n", name));
2929 result = (-EIO);
2930 } else {
2931 result = 0;
2932 if ((STp->device)->scsi_level < SCSI_2) {
2933 *block = ((STp->buffer)->b_data[0] << 16)
2934 + ((STp->buffer)->b_data[1] << 8)
2935 + (STp->buffer)->b_data[2];
2936 *partition = 0;
2937 } else {
2938 *block = ((STp->buffer)->b_data[4] << 24)
2939 + ((STp->buffer)->b_data[5] << 16)
2940 + ((STp->buffer)->b_data[6] << 8)
2941 + (STp->buffer)->b_data[7];
2942 *partition = (STp->buffer)->b_data[1];
2943 if (((STp->buffer)->b_data[0] & 0x80) &&
2944 (STp->buffer)->b_data[1] == 0) /* BOP of partition 0 */
2945 STp->ps[0].drv_block = STp->ps[0].drv_file = 0;
2947 DEBC(printk(ST_DEB_MSG "%s: Got tape pos. blk %d part %d.\n", name,
2948 *block, *partition));
2950 st_release_request(SRpnt);
2951 SRpnt = NULL;
2953 return result;
2957 /* Set the tape block and partition. Negative partition means that only the
2958 block should be set in vendor specific way. */
2959 static int set_location(struct scsi_tape *STp, unsigned int block, int partition,
2960 int logical)
2962 struct st_partstat *STps;
2963 int result, p;
2964 unsigned int blk;
2965 int timeout;
2966 unsigned char scmd[MAX_COMMAND_SIZE];
2967 struct st_request *SRpnt;
2968 DEB( char *name = tape_name(STp); )
2970 if (STp->ready != ST_READY)
2971 return (-EIO);
2972 timeout = STp->long_timeout;
2973 STps = &(STp->ps[STp->partition]);
2975 DEBC(printk(ST_DEB_MSG "%s: Setting block to %d and partition to %d.\n",
2976 name, block, partition));
2977 DEB(if (partition < 0)
2978 return (-EIO); )
2980 /* Update the location at the partition we are leaving */
2981 if ((!STp->can_partitions && partition != 0) ||
2982 partition >= ST_NBR_PARTITIONS)
2983 return (-EINVAL);
2984 if (partition != STp->partition) {
2985 if (get_location(STp, &blk, &p, 1))
2986 STps->last_block_valid = 0;
2987 else {
2988 STps->last_block_valid = 1;
2989 STps->last_block_visited = blk;
2990 DEBC(printk(ST_DEB_MSG
2991 "%s: Visited block %d for partition %d saved.\n",
2992 name, blk, STp->partition));
2996 memset(scmd, 0, MAX_COMMAND_SIZE);
2997 if ((STp->device)->scsi_level < SCSI_2) {
2998 scmd[0] = QFA_SEEK_BLOCK;
2999 scmd[2] = (block >> 16);
3000 scmd[3] = (block >> 8);
3001 scmd[4] = block;
3002 scmd[5] = 0;
3003 } else {
3004 scmd[0] = SEEK_10;
3005 scmd[3] = (block >> 24);
3006 scmd[4] = (block >> 16);
3007 scmd[5] = (block >> 8);
3008 scmd[6] = block;
3009 if (!logical && !STp->scsi2_logical)
3010 scmd[1] = 4;
3011 if (STp->partition != partition) {
3012 scmd[1] |= 2;
3013 scmd[8] = partition;
3014 DEBC(printk(ST_DEB_MSG
3015 "%s: Trying to change partition from %d to %d\n",
3016 name, STp->partition, partition));
3019 if (STp->immediate) {
3020 scmd[1] |= 1; /* Don't wait for completion */
3021 timeout = STp->device->timeout;
3024 SRpnt = st_do_scsi(NULL, STp, scmd, 0, DMA_NONE,
3025 timeout, MAX_READY_RETRIES, 1);
3026 if (!SRpnt)
3027 return (STp->buffer)->syscall_result;
3029 STps->drv_block = STps->drv_file = (-1);
3030 STps->eof = ST_NOEOF;
3031 if ((STp->buffer)->syscall_result != 0) {
3032 result = (-EIO);
3033 if (STp->can_partitions &&
3034 (STp->device)->scsi_level >= SCSI_2 &&
3035 (p = find_partition(STp)) >= 0)
3036 STp->partition = p;
3037 } else {
3038 if (STp->can_partitions) {
3039 STp->partition = partition;
3040 STps = &(STp->ps[partition]);
3041 if (!STps->last_block_valid ||
3042 STps->last_block_visited != block) {
3043 STps->at_sm = 0;
3044 STps->rw = ST_IDLE;
3046 } else
3047 STps->at_sm = 0;
3048 if (block == 0)
3049 STps->drv_block = STps->drv_file = 0;
3050 result = 0;
3053 st_release_request(SRpnt);
3054 SRpnt = NULL;
3056 return result;
3060 /* Find the current partition number for the drive status. Called from open and
3061 returns either partition number of negative error code. */
3062 static int find_partition(struct scsi_tape *STp)
3064 int i, partition;
3065 unsigned int block;
3067 if ((i = get_location(STp, &block, &partition, 1)) < 0)
3068 return i;
3069 if (partition >= ST_NBR_PARTITIONS)
3070 return (-EIO);
3071 return partition;
3075 /* Change the partition if necessary */
3076 static int switch_partition(struct scsi_tape *STp)
3078 struct st_partstat *STps;
3080 if (STp->partition == STp->new_partition)
3081 return 0;
3082 STps = &(STp->ps[STp->new_partition]);
3083 if (!STps->last_block_valid)
3084 STps->last_block_visited = 0;
3085 return set_location(STp, STps->last_block_visited, STp->new_partition, 1);
3088 /* Functions for reading and writing the medium partition mode page. */
3090 #define PART_PAGE 0x11
3091 #define PART_PAGE_FIXED_LENGTH 8
3093 #define PP_OFF_MAX_ADD_PARTS 2
3094 #define PP_OFF_NBR_ADD_PARTS 3
3095 #define PP_OFF_FLAGS 4
3096 #define PP_OFF_PART_UNITS 6
3097 #define PP_OFF_RESERVED 7
3099 #define PP_BIT_IDP 0x20
3100 #define PP_MSK_PSUM_MB 0x10
3102 /* Get the number of partitions on the tape. As a side effect reads the
3103 mode page into the tape buffer. */
3104 static int nbr_partitions(struct scsi_tape *STp)
3106 int result;
3107 DEB( char *name = tape_name(STp); )
3109 if (STp->ready != ST_READY)
3110 return (-EIO);
3112 result = read_mode_page(STp, PART_PAGE, 1);
3114 if (result) {
3115 DEBC(printk(ST_DEB_MSG "%s: Can't read medium partition page.\n",
3116 name));
3117 result = (-EIO);
3118 } else {
3119 result = (STp->buffer)->b_data[MODE_HEADER_LENGTH +
3120 PP_OFF_NBR_ADD_PARTS] + 1;
3121 DEBC(printk(ST_DEB_MSG "%s: Number of partitions %d.\n", name, result));
3124 return result;
3128 /* Partition the tape into two partitions if size > 0 or one partition if
3129 size == 0.
3131 The block descriptors are read and written because Sony SDT-7000 does not
3132 work without this (suggestion from Michael Schaefer <Michael.Schaefer@dlr.de>).
3134 My HP C1533A drive returns only one partition size field. This is used to
3135 set the size of partition 1. There is no size field for the default partition.
3136 Michael Schaefer's Sony SDT-7000 returns two descriptors and the second is
3137 used to set the size of partition 1 (this is what the SCSI-3 standard specifies).
3138 The following algorithm is used to accommodate both drives: if the number of
3139 partition size fields is greater than the maximum number of additional partitions
3140 in the mode page, the second field is used. Otherwise the first field is used.
3142 For Seagate DDS drives the page length must be 8 when no partitions is defined
3143 and 10 when 1 partition is defined (information from Eric Lee Green). This is
3144 is acceptable also to some other old drives and enforced if the first partition
3145 size field is used for the first additional partition size.
3147 static int partition_tape(struct scsi_tape *STp, int size)
3149 char *name = tape_name(STp);
3150 int result;
3151 int pgo, psd_cnt, psdo;
3152 unsigned char *bp;
3154 result = read_mode_page(STp, PART_PAGE, 0);
3155 if (result) {
3156 DEBC(printk(ST_DEB_MSG "%s: Can't read partition mode page.\n", name));
3157 return result;
3159 /* The mode page is in the buffer. Let's modify it and write it. */
3160 bp = (STp->buffer)->b_data;
3161 pgo = MODE_HEADER_LENGTH + bp[MH_OFF_BDESCS_LENGTH];
3162 DEBC(printk(ST_DEB_MSG "%s: Partition page length is %d bytes.\n",
3163 name, bp[pgo + MP_OFF_PAGE_LENGTH] + 2));
3165 psd_cnt = (bp[pgo + MP_OFF_PAGE_LENGTH] + 2 - PART_PAGE_FIXED_LENGTH) / 2;
3166 psdo = pgo + PART_PAGE_FIXED_LENGTH;
3167 if (psd_cnt > bp[pgo + PP_OFF_MAX_ADD_PARTS]) {
3168 bp[psdo] = bp[psdo + 1] = 0xff; /* Rest of the tape */
3169 psdo += 2;
3171 memset(bp + psdo, 0, bp[pgo + PP_OFF_NBR_ADD_PARTS] * 2);
3173 DEBC(printk("%s: psd_cnt %d, max.parts %d, nbr_parts %d\n", name,
3174 psd_cnt, bp[pgo + PP_OFF_MAX_ADD_PARTS],
3175 bp[pgo + PP_OFF_NBR_ADD_PARTS]));
3177 if (size <= 0) {
3178 bp[pgo + PP_OFF_NBR_ADD_PARTS] = 0;
3179 if (psd_cnt <= bp[pgo + PP_OFF_MAX_ADD_PARTS])
3180 bp[pgo + MP_OFF_PAGE_LENGTH] = 6;
3181 DEBC(printk(ST_DEB_MSG "%s: Formatting tape with one partition.\n",
3182 name));
3183 } else {
3184 bp[psdo] = (size >> 8) & 0xff;
3185 bp[psdo + 1] = size & 0xff;
3186 bp[pgo + 3] = 1;
3187 if (bp[pgo + MP_OFF_PAGE_LENGTH] < 8)
3188 bp[pgo + MP_OFF_PAGE_LENGTH] = 8;
3189 DEBC(printk(ST_DEB_MSG
3190 "%s: Formatting tape with two partitions (1 = %d MB).\n",
3191 name, size));
3193 bp[pgo + PP_OFF_PART_UNITS] = 0;
3194 bp[pgo + PP_OFF_RESERVED] = 0;
3195 bp[pgo + PP_OFF_FLAGS] = PP_BIT_IDP | PP_MSK_PSUM_MB;
3197 result = write_mode_page(STp, PART_PAGE, 1);
3198 if (result) {
3199 printk(KERN_INFO "%s: Partitioning of tape failed.\n", name);
3200 result = (-EIO);
3203 return result;
3208 /* The ioctl command */
3209 static int st_ioctl(struct inode *inode, struct file *file,
3210 unsigned int cmd_in, unsigned long arg)
3212 int i, cmd_nr, cmd_type, bt;
3213 int retval = 0;
3214 unsigned int blk;
3215 struct scsi_tape *STp = file->private_data;
3216 struct st_modedef *STm;
3217 struct st_partstat *STps;
3218 char *name = tape_name(STp);
3219 void __user *p = (void __user *)arg;
3221 if (down_interruptible(&STp->lock))
3222 return -ERESTARTSYS;
3224 DEB(
3225 if (debugging && !STp->in_use) {
3226 printk(ST_DEB_MSG "%s: Incorrect device.\n", name);
3227 retval = (-EIO);
3228 goto out;
3229 } ) /* end DEB */
3231 STm = &(STp->modes[STp->current_mode]);
3232 STps = &(STp->ps[STp->partition]);
3235 * If we are in the middle of error recovery, don't let anyone
3236 * else try and use this device. Also, if error recovery fails, it
3237 * may try and take the device offline, in which case all further
3238 * access to the device is prohibited.
3240 retval = scsi_nonblockable_ioctl(STp->device, cmd_in, p, file);
3241 if (!scsi_block_when_processing_errors(STp->device) || retval != -ENODEV)
3242 goto out;
3243 retval = 0;
3245 cmd_type = _IOC_TYPE(cmd_in);
3246 cmd_nr = _IOC_NR(cmd_in);
3248 if (cmd_type == _IOC_TYPE(MTIOCTOP) && cmd_nr == _IOC_NR(MTIOCTOP)) {
3249 struct mtop mtc;
3251 if (_IOC_SIZE(cmd_in) != sizeof(mtc)) {
3252 retval = (-EINVAL);
3253 goto out;
3256 i = copy_from_user(&mtc, p, sizeof(struct mtop));
3257 if (i) {
3258 retval = (-EFAULT);
3259 goto out;
3262 if (mtc.mt_op == MTSETDRVBUFFER && !capable(CAP_SYS_ADMIN)) {
3263 printk(KERN_WARNING
3264 "%s: MTSETDRVBUFFER only allowed for root.\n", name);
3265 retval = (-EPERM);
3266 goto out;
3268 if (!STm->defined &&
3269 (mtc.mt_op != MTSETDRVBUFFER &&
3270 (mtc.mt_count & MT_ST_OPTIONS) == 0)) {
3271 retval = (-ENXIO);
3272 goto out;
3275 if (!STp->pos_unknown) {
3277 if (STps->eof == ST_FM_HIT) {
3278 if (mtc.mt_op == MTFSF || mtc.mt_op == MTFSFM ||
3279 mtc.mt_op == MTEOM) {
3280 mtc.mt_count -= 1;
3281 if (STps->drv_file >= 0)
3282 STps->drv_file += 1;
3283 } else if (mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM) {
3284 mtc.mt_count += 1;
3285 if (STps->drv_file >= 0)
3286 STps->drv_file += 1;
3290 if (mtc.mt_op == MTSEEK) {
3291 /* Old position must be restored if partition will be
3292 changed */
3293 i = !STp->can_partitions ||
3294 (STp->new_partition != STp->partition);
3295 } else {
3296 i = mtc.mt_op == MTREW || mtc.mt_op == MTOFFL ||
3297 mtc.mt_op == MTRETEN || mtc.mt_op == MTEOM ||
3298 mtc.mt_op == MTLOCK || mtc.mt_op == MTLOAD ||
3299 mtc.mt_op == MTFSF || mtc.mt_op == MTFSFM ||
3300 mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM ||
3301 mtc.mt_op == MTCOMPRESSION;
3303 i = flush_buffer(STp, i);
3304 if (i < 0) {
3305 retval = i;
3306 goto out;
3308 if (STps->rw == ST_WRITING &&
3309 (mtc.mt_op == MTREW || mtc.mt_op == MTOFFL ||
3310 mtc.mt_op == MTSEEK ||
3311 mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM)) {
3312 i = st_int_ioctl(STp, MTWEOF, 1);
3313 if (i < 0) {
3314 retval = i;
3315 goto out;
3317 if (mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM)
3318 mtc.mt_count++;
3319 STps->rw = ST_IDLE;
3322 } else {
3324 * If there was a bus reset, block further access
3325 * to this device. If the user wants to rewind the tape,
3326 * then reset the flag and allow access again.
3328 if (mtc.mt_op != MTREW &&
3329 mtc.mt_op != MTOFFL &&
3330 mtc.mt_op != MTRETEN &&
3331 mtc.mt_op != MTERASE &&
3332 mtc.mt_op != MTSEEK &&
3333 mtc.mt_op != MTEOM) {
3334 retval = (-EIO);
3335 goto out;
3337 reset_state(STp);
3338 /* remove this when the midlevel properly clears was_reset */
3339 STp->device->was_reset = 0;
3342 if (mtc.mt_op != MTNOP && mtc.mt_op != MTSETBLK &&
3343 mtc.mt_op != MTSETDENSITY && mtc.mt_op != MTWSM &&
3344 mtc.mt_op != MTSETDRVBUFFER && mtc.mt_op != MTSETPART)
3345 STps->rw = ST_IDLE; /* Prevent automatic WEOF and fsf */
3347 if (mtc.mt_op == MTOFFL && STp->door_locked != ST_UNLOCKED)
3348 do_door_lock(STp, 0); /* Ignore result! */
3350 if (mtc.mt_op == MTSETDRVBUFFER &&
3351 (mtc.mt_count & MT_ST_OPTIONS) != 0) {
3352 retval = st_set_options(STp, mtc.mt_count);
3353 goto out;
3356 if (mtc.mt_op == MTSETPART) {
3357 if (!STp->can_partitions ||
3358 mtc.mt_count < 0 || mtc.mt_count >= ST_NBR_PARTITIONS) {
3359 retval = (-EINVAL);
3360 goto out;
3362 if (mtc.mt_count >= STp->nbr_partitions &&
3363 (STp->nbr_partitions = nbr_partitions(STp)) < 0) {
3364 retval = (-EIO);
3365 goto out;
3367 if (mtc.mt_count >= STp->nbr_partitions) {
3368 retval = (-EINVAL);
3369 goto out;
3371 STp->new_partition = mtc.mt_count;
3372 retval = 0;
3373 goto out;
3376 if (mtc.mt_op == MTMKPART) {
3377 if (!STp->can_partitions) {
3378 retval = (-EINVAL);
3379 goto out;
3381 if ((i = st_int_ioctl(STp, MTREW, 0)) < 0 ||
3382 (i = partition_tape(STp, mtc.mt_count)) < 0) {
3383 retval = i;
3384 goto out;
3386 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
3387 STp->ps[i].rw = ST_IDLE;
3388 STp->ps[i].at_sm = 0;
3389 STp->ps[i].last_block_valid = 0;
3391 STp->partition = STp->new_partition = 0;
3392 STp->nbr_partitions = 1; /* Bad guess ?-) */
3393 STps->drv_block = STps->drv_file = 0;
3394 retval = 0;
3395 goto out;
3398 if (mtc.mt_op == MTSEEK) {
3399 i = set_location(STp, mtc.mt_count, STp->new_partition, 0);
3400 if (!STp->can_partitions)
3401 STp->ps[0].rw = ST_IDLE;
3402 retval = i;
3403 goto out;
3406 if (mtc.mt_op == MTUNLOAD || mtc.mt_op == MTOFFL) {
3407 retval = do_load_unload(STp, file, 0);
3408 goto out;
3411 if (mtc.mt_op == MTLOAD) {
3412 retval = do_load_unload(STp, file, max(1, mtc.mt_count));
3413 goto out;
3416 if (mtc.mt_op == MTLOCK || mtc.mt_op == MTUNLOCK) {
3417 retval = do_door_lock(STp, (mtc.mt_op == MTLOCK));
3418 goto out;
3421 if (STp->can_partitions && STp->ready == ST_READY &&
3422 (i = switch_partition(STp)) < 0) {
3423 retval = i;
3424 goto out;
3427 if (mtc.mt_op == MTCOMPRESSION)
3428 retval = st_compression(STp, (mtc.mt_count & 1));
3429 else
3430 retval = st_int_ioctl(STp, mtc.mt_op, mtc.mt_count);
3431 goto out;
3433 if (!STm->defined) {
3434 retval = (-ENXIO);
3435 goto out;
3438 if ((i = flush_buffer(STp, 0)) < 0) {
3439 retval = i;
3440 goto out;
3442 if (STp->can_partitions &&
3443 (i = switch_partition(STp)) < 0) {
3444 retval = i;
3445 goto out;
3448 if (cmd_type == _IOC_TYPE(MTIOCGET) && cmd_nr == _IOC_NR(MTIOCGET)) {
3449 struct mtget mt_status;
3451 if (_IOC_SIZE(cmd_in) != sizeof(struct mtget)) {
3452 retval = (-EINVAL);
3453 goto out;
3456 mt_status.mt_type = STp->tape_type;
3457 mt_status.mt_dsreg =
3458 ((STp->block_size << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK) |
3459 ((STp->density << MT_ST_DENSITY_SHIFT) & MT_ST_DENSITY_MASK);
3460 mt_status.mt_blkno = STps->drv_block;
3461 mt_status.mt_fileno = STps->drv_file;
3462 if (STp->block_size != 0) {
3463 if (STps->rw == ST_WRITING)
3464 mt_status.mt_blkno +=
3465 (STp->buffer)->buffer_bytes / STp->block_size;
3466 else if (STps->rw == ST_READING)
3467 mt_status.mt_blkno -=
3468 ((STp->buffer)->buffer_bytes +
3469 STp->block_size - 1) / STp->block_size;
3472 mt_status.mt_gstat = 0;
3473 if (STp->drv_write_prot)
3474 mt_status.mt_gstat |= GMT_WR_PROT(0xffffffff);
3475 if (mt_status.mt_blkno == 0) {
3476 if (mt_status.mt_fileno == 0)
3477 mt_status.mt_gstat |= GMT_BOT(0xffffffff);
3478 else
3479 mt_status.mt_gstat |= GMT_EOF(0xffffffff);
3481 mt_status.mt_erreg = (STp->recover_reg << MT_ST_SOFTERR_SHIFT);
3482 mt_status.mt_resid = STp->partition;
3483 if (STps->eof == ST_EOM_OK || STps->eof == ST_EOM_ERROR)
3484 mt_status.mt_gstat |= GMT_EOT(0xffffffff);
3485 else if (STps->eof >= ST_EOM_OK)
3486 mt_status.mt_gstat |= GMT_EOD(0xffffffff);
3487 if (STp->density == 1)
3488 mt_status.mt_gstat |= GMT_D_800(0xffffffff);
3489 else if (STp->density == 2)
3490 mt_status.mt_gstat |= GMT_D_1600(0xffffffff);
3491 else if (STp->density == 3)
3492 mt_status.mt_gstat |= GMT_D_6250(0xffffffff);
3493 if (STp->ready == ST_READY)
3494 mt_status.mt_gstat |= GMT_ONLINE(0xffffffff);
3495 if (STp->ready == ST_NO_TAPE)
3496 mt_status.mt_gstat |= GMT_DR_OPEN(0xffffffff);
3497 if (STps->at_sm)
3498 mt_status.mt_gstat |= GMT_SM(0xffffffff);
3499 if (STm->do_async_writes ||
3500 (STm->do_buffer_writes && STp->block_size != 0) ||
3501 STp->drv_buffer != 0)
3502 mt_status.mt_gstat |= GMT_IM_REP_EN(0xffffffff);
3503 if (STp->cleaning_req)
3504 mt_status.mt_gstat |= GMT_CLN(0xffffffff);
3506 i = copy_to_user(p, &mt_status, sizeof(struct mtget));
3507 if (i) {
3508 retval = (-EFAULT);
3509 goto out;
3512 STp->recover_reg = 0; /* Clear after read */
3513 retval = 0;
3514 goto out;
3515 } /* End of MTIOCGET */
3516 if (cmd_type == _IOC_TYPE(MTIOCPOS) && cmd_nr == _IOC_NR(MTIOCPOS)) {
3517 struct mtpos mt_pos;
3518 if (_IOC_SIZE(cmd_in) != sizeof(struct mtpos)) {
3519 retval = (-EINVAL);
3520 goto out;
3522 if ((i = get_location(STp, &blk, &bt, 0)) < 0) {
3523 retval = i;
3524 goto out;
3526 mt_pos.mt_blkno = blk;
3527 i = copy_to_user(p, &mt_pos, sizeof(struct mtpos));
3528 if (i)
3529 retval = (-EFAULT);
3530 goto out;
3532 up(&STp->lock);
3533 switch (cmd_in) {
3534 case SCSI_IOCTL_GET_IDLUN:
3535 case SCSI_IOCTL_GET_BUS_NUMBER:
3536 break;
3537 default:
3538 if ((cmd_in == SG_IO ||
3539 cmd_in == SCSI_IOCTL_SEND_COMMAND ||
3540 cmd_in == CDROM_SEND_PACKET) &&
3541 !capable(CAP_SYS_RAWIO))
3542 i = -EPERM;
3543 else
3544 i = scsi_cmd_ioctl(file, STp->disk, cmd_in, p);
3545 if (i != -ENOTTY)
3546 return i;
3547 break;
3549 retval = scsi_ioctl(STp->device, cmd_in, p);
3550 if (!retval && cmd_in == SCSI_IOCTL_STOP_UNIT) { /* unload */
3551 STp->rew_at_close = 0;
3552 STp->ready = ST_NO_TAPE;
3554 return retval;
3556 out:
3557 up(&STp->lock);
3558 return retval;
3561 #ifdef CONFIG_COMPAT
3562 static long st_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3564 struct scsi_tape *STp = file->private_data;
3565 struct scsi_device *sdev = STp->device;
3566 int ret = -ENOIOCTLCMD;
3567 if (sdev->host->hostt->compat_ioctl) {
3569 ret = sdev->host->hostt->compat_ioctl(sdev, cmd, (void __user *)arg);
3572 return ret;
3574 #endif
3578 /* Try to allocate a new tape buffer. Calling function must not hold
3579 dev_arr_lock. */
3580 static struct st_buffer *
3581 new_tape_buffer(int from_initialization, int need_dma, int max_sg)
3583 int i, got = 0;
3584 gfp_t priority;
3585 struct st_buffer *tb;
3587 if (from_initialization)
3588 priority = GFP_ATOMIC;
3589 else
3590 priority = GFP_KERNEL;
3592 i = sizeof(struct st_buffer) + (max_sg - 1) * sizeof(struct scatterlist) +
3593 max_sg * sizeof(struct st_buf_fragment);
3594 tb = kmalloc(i, priority);
3595 if (!tb) {
3596 printk(KERN_NOTICE "st: Can't allocate new tape buffer.\n");
3597 return NULL;
3599 memset(tb, 0, i);
3600 tb->frp_segs = tb->orig_frp_segs = 0;
3601 tb->use_sg = max_sg;
3602 tb->frp = (struct st_buf_fragment *)(&(tb->sg[0]) + max_sg);
3604 tb->in_use = 1;
3605 tb->dma = need_dma;
3606 tb->buffer_size = got;
3608 return tb;
3612 /* Try to allocate enough space in the tape buffer */
3613 static int enlarge_buffer(struct st_buffer * STbuffer, int new_size, int need_dma)
3615 int segs, nbr, max_segs, b_size, order, got;
3616 gfp_t priority;
3618 if (new_size <= STbuffer->buffer_size)
3619 return 1;
3621 if (STbuffer->buffer_size <= PAGE_SIZE)
3622 normalize_buffer(STbuffer); /* Avoid extra segment */
3624 max_segs = STbuffer->use_sg;
3625 nbr = max_segs - STbuffer->frp_segs;
3626 if (nbr <= 0)
3627 return 0;
3629 priority = GFP_KERNEL | __GFP_NOWARN;
3630 if (need_dma)
3631 priority |= GFP_DMA;
3632 for (b_size = PAGE_SIZE, order=0; order <= 6 &&
3633 b_size < new_size - STbuffer->buffer_size;
3634 order++, b_size *= 2)
3635 ; /* empty */
3637 for (segs = STbuffer->frp_segs, got = STbuffer->buffer_size;
3638 segs < max_segs && got < new_size;) {
3639 STbuffer->frp[segs].page = alloc_pages(priority, order);
3640 if (STbuffer->frp[segs].page == NULL) {
3641 if (new_size - got <= (max_segs - segs) * b_size / 2) {
3642 b_size /= 2; /* Large enough for the rest of the buffers */
3643 order--;
3644 continue;
3646 DEB(STbuffer->buffer_size = got);
3647 normalize_buffer(STbuffer);
3648 return 0;
3650 STbuffer->frp[segs].length = b_size;
3651 STbuffer->frp_segs += 1;
3652 got += b_size;
3653 STbuffer->buffer_size = got;
3654 segs++;
3656 STbuffer->b_data = page_address(STbuffer->frp[0].page);
3658 return 1;
3662 /* Release the extra buffer */
3663 static void normalize_buffer(struct st_buffer * STbuffer)
3665 int i, order;
3667 for (i = STbuffer->orig_frp_segs; i < STbuffer->frp_segs; i++) {
3668 order = get_order(STbuffer->frp[i].length);
3669 __free_pages(STbuffer->frp[i].page, order);
3670 STbuffer->buffer_size -= STbuffer->frp[i].length;
3672 STbuffer->frp_segs = STbuffer->orig_frp_segs;
3673 STbuffer->frp_sg_current = 0;
3674 STbuffer->sg_segs = 0;
3678 /* Move data from the user buffer to the tape buffer. Returns zero (success) or
3679 negative error code. */
3680 static int append_to_buffer(const char __user *ubp, struct st_buffer * st_bp, int do_count)
3682 int i, cnt, res, offset;
3684 for (i = 0, offset = st_bp->buffer_bytes;
3685 i < st_bp->frp_segs && offset >= st_bp->frp[i].length; i++)
3686 offset -= st_bp->frp[i].length;
3687 if (i == st_bp->frp_segs) { /* Should never happen */
3688 printk(KERN_WARNING "st: append_to_buffer offset overflow.\n");
3689 return (-EIO);
3691 for (; i < st_bp->frp_segs && do_count > 0; i++) {
3692 cnt = st_bp->frp[i].length - offset < do_count ?
3693 st_bp->frp[i].length - offset : do_count;
3694 res = copy_from_user(page_address(st_bp->frp[i].page) + offset, ubp, cnt);
3695 if (res)
3696 return (-EFAULT);
3697 do_count -= cnt;
3698 st_bp->buffer_bytes += cnt;
3699 ubp += cnt;
3700 offset = 0;
3702 if (do_count) /* Should never happen */
3703 return (-EIO);
3705 return 0;
3709 /* Move data from the tape buffer to the user buffer. Returns zero (success) or
3710 negative error code. */
3711 static int from_buffer(struct st_buffer * st_bp, char __user *ubp, int do_count)
3713 int i, cnt, res, offset;
3715 for (i = 0, offset = st_bp->read_pointer;
3716 i < st_bp->frp_segs && offset >= st_bp->frp[i].length; i++)
3717 offset -= st_bp->frp[i].length;
3718 if (i == st_bp->frp_segs) { /* Should never happen */
3719 printk(KERN_WARNING "st: from_buffer offset overflow.\n");
3720 return (-EIO);
3722 for (; i < st_bp->frp_segs && do_count > 0; i++) {
3723 cnt = st_bp->frp[i].length - offset < do_count ?
3724 st_bp->frp[i].length - offset : do_count;
3725 res = copy_to_user(ubp, page_address(st_bp->frp[i].page) + offset, cnt);
3726 if (res)
3727 return (-EFAULT);
3728 do_count -= cnt;
3729 st_bp->buffer_bytes -= cnt;
3730 st_bp->read_pointer += cnt;
3731 ubp += cnt;
3732 offset = 0;
3734 if (do_count) /* Should never happen */
3735 return (-EIO);
3737 return 0;
3741 /* Move data towards start of buffer */
3742 static void move_buffer_data(struct st_buffer * st_bp, int offset)
3744 int src_seg, dst_seg, src_offset = 0, dst_offset;
3745 int count, total;
3747 if (offset == 0)
3748 return;
3750 total=st_bp->buffer_bytes - offset;
3751 for (src_seg=0; src_seg < st_bp->frp_segs; src_seg++) {
3752 src_offset = offset;
3753 if (src_offset < st_bp->frp[src_seg].length)
3754 break;
3755 offset -= st_bp->frp[src_seg].length;
3758 st_bp->buffer_bytes = st_bp->read_pointer = total;
3759 for (dst_seg=dst_offset=0; total > 0; ) {
3760 count = min(st_bp->frp[dst_seg].length - dst_offset,
3761 st_bp->frp[src_seg].length - src_offset);
3762 memmove(page_address(st_bp->frp[dst_seg].page) + dst_offset,
3763 page_address(st_bp->frp[src_seg].page) + src_offset, count);
3764 src_offset += count;
3765 if (src_offset >= st_bp->frp[src_seg].length) {
3766 src_seg++;
3767 src_offset = 0;
3769 dst_offset += count;
3770 if (dst_offset >= st_bp->frp[dst_seg].length) {
3771 dst_seg++;
3772 dst_offset = 0;
3774 total -= count;
3779 /* Fill the s/g list up to the length required for this transfer */
3780 static void buf_to_sg(struct st_buffer *STbp, unsigned int length)
3782 int i;
3783 unsigned int count;
3784 struct scatterlist *sg;
3785 struct st_buf_fragment *frp;
3787 if (length == STbp->frp_sg_current)
3788 return; /* work already done */
3790 sg = &(STbp->sg[0]);
3791 frp = STbp->frp;
3792 for (i=count=0; count < length; i++) {
3793 sg[i].page = frp[i].page;
3794 if (length - count > frp[i].length)
3795 sg[i].length = frp[i].length;
3796 else
3797 sg[i].length = length - count;
3798 count += sg[i].length;
3799 sg[i].offset = 0;
3801 STbp->sg_segs = i;
3802 STbp->frp_sg_current = length;
3806 /* Validate the options from command line or module parameters */
3807 static void validate_options(void)
3809 if (buffer_kbs > 0)
3810 st_fixed_buffer_size = buffer_kbs * ST_KILOBYTE;
3811 if (max_sg_segs >= ST_FIRST_SG)
3812 st_max_sg_segs = max_sg_segs;
3815 #ifndef MODULE
3816 /* Set the boot options. Syntax is defined in Documenation/scsi/st.txt.
3818 static int __init st_setup(char *str)
3820 int i, len, ints[5];
3821 char *stp;
3823 stp = get_options(str, ARRAY_SIZE(ints), ints);
3825 if (ints[0] > 0) {
3826 for (i = 0; i < ints[0] && i < ARRAY_SIZE(parms); i++)
3827 if (parms[i].val)
3828 *parms[i].val = ints[i + 1];
3829 } else {
3830 while (stp != NULL) {
3831 for (i = 0; i < ARRAY_SIZE(parms); i++) {
3832 len = strlen(parms[i].name);
3833 if (!strncmp(stp, parms[i].name, len) &&
3834 (*(stp + len) == ':' || *(stp + len) == '=')) {
3835 if (parms[i].val)
3836 *parms[i].val =
3837 simple_strtoul(stp + len + 1, NULL, 0);
3838 else
3839 printk(KERN_WARNING "st: Obsolete parameter %s\n",
3840 parms[i].name);
3841 break;
3844 if (i >= sizeof(parms) / sizeof(struct st_dev_parm))
3845 printk(KERN_WARNING "st: invalid parameter in '%s'\n",
3846 stp);
3847 stp = strchr(stp, ',');
3848 if (stp)
3849 stp++;
3853 validate_options();
3855 return 1;
3858 __setup("st=", st_setup);
3860 #endif
3862 static struct file_operations st_fops =
3864 .owner = THIS_MODULE,
3865 .read = st_read,
3866 .write = st_write,
3867 .ioctl = st_ioctl,
3868 #ifdef CONFIG_COMPAT
3869 .compat_ioctl = st_compat_ioctl,
3870 #endif
3871 .open = st_open,
3872 .flush = st_flush,
3873 .release = st_release,
3876 static int st_probe(struct device *dev)
3878 struct scsi_device *SDp = to_scsi_device(dev);
3879 struct gendisk *disk = NULL;
3880 struct cdev *cdev = NULL;
3881 struct scsi_tape *tpnt = NULL;
3882 struct st_modedef *STm;
3883 struct st_partstat *STps;
3884 struct st_buffer *buffer;
3885 int i, j, mode, dev_num, error;
3886 char *stp;
3888 if (SDp->type != TYPE_TAPE)
3889 return -ENODEV;
3890 if ((stp = st_incompatible(SDp))) {
3891 sdev_printk(KERN_INFO, SDp, "Found incompatible tape\n");
3892 printk(KERN_INFO "st: The suggested driver is %s.\n", stp);
3893 return -ENODEV;
3896 i = min(SDp->request_queue->max_hw_segments,
3897 SDp->request_queue->max_phys_segments);
3898 if (st_max_sg_segs < i)
3899 i = st_max_sg_segs;
3900 buffer = new_tape_buffer(1, (SDp->host)->unchecked_isa_dma, i);
3901 if (buffer == NULL) {
3902 printk(KERN_ERR
3903 "st: Can't allocate new tape buffer. Device not attached.\n");
3904 goto out;
3907 disk = alloc_disk(1);
3908 if (!disk) {
3909 printk(KERN_ERR "st: out of memory. Device not attached.\n");
3910 goto out_buffer_free;
3913 write_lock(&st_dev_arr_lock);
3914 if (st_nr_dev >= st_dev_max) {
3915 struct scsi_tape **tmp_da;
3916 int tmp_dev_max;
3918 tmp_dev_max = max(st_nr_dev * 2, 8);
3919 if (tmp_dev_max > ST_MAX_TAPES)
3920 tmp_dev_max = ST_MAX_TAPES;
3921 if (tmp_dev_max <= st_nr_dev) {
3922 write_unlock(&st_dev_arr_lock);
3923 printk(KERN_ERR "st: Too many tape devices (max. %d).\n",
3924 ST_MAX_TAPES);
3925 goto out_put_disk;
3928 tmp_da = kmalloc(tmp_dev_max * sizeof(struct scsi_tape *), GFP_ATOMIC);
3929 if (tmp_da == NULL) {
3930 write_unlock(&st_dev_arr_lock);
3931 printk(KERN_ERR "st: Can't extend device array.\n");
3932 goto out_put_disk;
3935 memset(tmp_da, 0, tmp_dev_max * sizeof(struct scsi_tape *));
3936 if (scsi_tapes != NULL) {
3937 memcpy(tmp_da, scsi_tapes,
3938 st_dev_max * sizeof(struct scsi_tape *));
3939 kfree(scsi_tapes);
3941 scsi_tapes = tmp_da;
3943 st_dev_max = tmp_dev_max;
3946 for (i = 0; i < st_dev_max; i++)
3947 if (scsi_tapes[i] == NULL)
3948 break;
3949 if (i >= st_dev_max)
3950 panic("scsi_devices corrupt (st)");
3952 tpnt = kmalloc(sizeof(struct scsi_tape), GFP_ATOMIC);
3953 if (tpnt == NULL) {
3954 write_unlock(&st_dev_arr_lock);
3955 printk(KERN_ERR "st: Can't allocate device descriptor.\n");
3956 goto out_put_disk;
3958 memset(tpnt, 0, sizeof(struct scsi_tape));
3959 kref_init(&tpnt->kref);
3960 tpnt->disk = disk;
3961 sprintf(disk->disk_name, "st%d", i);
3962 disk->private_data = &tpnt->driver;
3963 disk->queue = SDp->request_queue;
3964 tpnt->driver = &st_template;
3965 scsi_tapes[i] = tpnt;
3966 dev_num = i;
3968 tpnt->device = SDp;
3969 if (SDp->scsi_level <= 2)
3970 tpnt->tape_type = MT_ISSCSI1;
3971 else
3972 tpnt->tape_type = MT_ISSCSI2;
3974 tpnt->buffer = buffer;
3975 tpnt->buffer->last_SRpnt = NULL;
3977 tpnt->inited = 0;
3978 tpnt->dirty = 0;
3979 tpnt->in_use = 0;
3980 tpnt->drv_buffer = 1; /* Try buffering if no mode sense */
3981 tpnt->restr_dma = (SDp->host)->unchecked_isa_dma;
3982 tpnt->use_pf = (SDp->scsi_level >= SCSI_2);
3983 tpnt->density = 0;
3984 tpnt->do_auto_lock = ST_AUTO_LOCK;
3985 tpnt->can_bsr = (SDp->scsi_level > 2 ? 1 : ST_IN_FILE_POS); /* BSR mandatory in SCSI3 */
3986 tpnt->can_partitions = 0;
3987 tpnt->two_fm = ST_TWO_FM;
3988 tpnt->fast_mteom = ST_FAST_MTEOM;
3989 tpnt->scsi2_logical = ST_SCSI2LOGICAL;
3990 tpnt->immediate = ST_NOWAIT;
3991 tpnt->default_drvbuffer = 0xff; /* No forced buffering */
3992 tpnt->partition = 0;
3993 tpnt->new_partition = 0;
3994 tpnt->nbr_partitions = 0;
3995 tpnt->device->timeout = ST_TIMEOUT;
3996 tpnt->long_timeout = ST_LONG_TIMEOUT;
3997 tpnt->try_dio = try_direct_io && !SDp->host->unchecked_isa_dma;
3999 for (i = 0; i < ST_NBR_MODES; i++) {
4000 STm = &(tpnt->modes[i]);
4001 STm->defined = 0;
4002 STm->sysv = ST_SYSV;
4003 STm->defaults_for_writes = 0;
4004 STm->do_async_writes = ST_ASYNC_WRITES;
4005 STm->do_buffer_writes = ST_BUFFER_WRITES;
4006 STm->do_read_ahead = ST_READ_AHEAD;
4007 STm->default_compression = ST_DONT_TOUCH;
4008 STm->default_blksize = (-1); /* No forced size */
4009 STm->default_density = (-1); /* No forced density */
4012 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
4013 STps = &(tpnt->ps[i]);
4014 STps->rw = ST_IDLE;
4015 STps->eof = ST_NOEOF;
4016 STps->at_sm = 0;
4017 STps->last_block_valid = 0;
4018 STps->drv_block = (-1);
4019 STps->drv_file = (-1);
4022 tpnt->current_mode = 0;
4023 tpnt->modes[0].defined = 1;
4025 tpnt->density_changed = tpnt->compression_changed =
4026 tpnt->blksize_changed = 0;
4027 init_MUTEX(&tpnt->lock);
4029 st_nr_dev++;
4030 write_unlock(&st_dev_arr_lock);
4032 for (mode = 0; mode < ST_NBR_MODES; ++mode) {
4033 STm = &(tpnt->modes[mode]);
4034 for (j=0; j < 2; j++) {
4035 cdev = cdev_alloc();
4036 if (!cdev) {
4037 printk(KERN_ERR
4038 "st%d: out of memory. Device not attached.\n",
4039 dev_num);
4040 goto out_free_tape;
4042 cdev->owner = THIS_MODULE;
4043 cdev->ops = &st_fops;
4045 error = cdev_add(cdev,
4046 MKDEV(SCSI_TAPE_MAJOR, TAPE_MINOR(dev_num, mode, j)),
4048 if (error) {
4049 printk(KERN_ERR "st%d: Can't add %s-rewind mode %d\n",
4050 dev_num, j ? "non" : "auto", mode);
4051 printk(KERN_ERR "st%d: Device not attached.\n", dev_num);
4052 goto out_free_tape;
4054 STm->cdevs[j] = cdev;
4057 do_create_class_files(tpnt, dev_num, mode);
4060 for (mode = 0; mode < ST_NBR_MODES; ++mode) {
4061 /* Make sure that the minor numbers corresponding to the four
4062 first modes always get the same names */
4063 i = mode << (4 - ST_NBR_MODE_BITS);
4064 /* Rewind entry */
4065 devfs_mk_cdev(MKDEV(SCSI_TAPE_MAJOR, TAPE_MINOR(dev_num, mode, 0)),
4066 S_IFCHR | S_IRUGO | S_IWUGO,
4067 "%s/mt%s", SDp->devfs_name, st_formats[i]);
4068 /* No-rewind entry */
4069 devfs_mk_cdev(MKDEV(SCSI_TAPE_MAJOR, TAPE_MINOR(dev_num, mode, 1)),
4070 S_IFCHR | S_IRUGO | S_IWUGO,
4071 "%s/mt%sn", SDp->devfs_name, st_formats[i]);
4073 disk->number = devfs_register_tape(SDp->devfs_name);
4075 sdev_printk(KERN_WARNING, SDp,
4076 "Attached scsi tape %s", tape_name(tpnt));
4077 printk(KERN_WARNING "%s: try direct i/o: %s (alignment %d B)\n",
4078 tape_name(tpnt), tpnt->try_dio ? "yes" : "no",
4079 queue_dma_alignment(SDp->request_queue) + 1);
4081 return 0;
4083 out_free_tape:
4084 for (mode=0; mode < ST_NBR_MODES; mode++) {
4085 STm = &(tpnt->modes[mode]);
4086 sysfs_remove_link(&tpnt->device->sdev_gendev.kobj,
4087 "tape");
4088 for (j=0; j < 2; j++) {
4089 if (STm->cdevs[j]) {
4090 if (cdev == STm->cdevs[j])
4091 cdev = NULL;
4092 class_device_destroy(st_sysfs_class,
4093 MKDEV(SCSI_TAPE_MAJOR,
4094 TAPE_MINOR(i, mode, j)));
4095 cdev_del(STm->cdevs[j]);
4099 if (cdev)
4100 cdev_del(cdev);
4101 write_lock(&st_dev_arr_lock);
4102 scsi_tapes[dev_num] = NULL;
4103 st_nr_dev--;
4104 write_unlock(&st_dev_arr_lock);
4105 out_put_disk:
4106 put_disk(disk);
4107 kfree(tpnt);
4108 out_buffer_free:
4109 kfree(buffer);
4110 out:
4111 return -ENODEV;
4115 static int st_remove(struct device *dev)
4117 struct scsi_device *SDp = to_scsi_device(dev);
4118 struct scsi_tape *tpnt;
4119 int i, j, mode;
4121 write_lock(&st_dev_arr_lock);
4122 for (i = 0; i < st_dev_max; i++) {
4123 tpnt = scsi_tapes[i];
4124 if (tpnt != NULL && tpnt->device == SDp) {
4125 scsi_tapes[i] = NULL;
4126 st_nr_dev--;
4127 write_unlock(&st_dev_arr_lock);
4128 devfs_unregister_tape(tpnt->disk->number);
4129 sysfs_remove_link(&tpnt->device->sdev_gendev.kobj,
4130 "tape");
4131 for (mode = 0; mode < ST_NBR_MODES; ++mode) {
4132 j = mode << (4 - ST_NBR_MODE_BITS);
4133 devfs_remove("%s/mt%s", SDp->devfs_name, st_formats[j]);
4134 devfs_remove("%s/mt%sn", SDp->devfs_name, st_formats[j]);
4135 for (j=0; j < 2; j++) {
4136 class_device_destroy(st_sysfs_class,
4137 MKDEV(SCSI_TAPE_MAJOR,
4138 TAPE_MINOR(i, mode, j)));
4139 cdev_del(tpnt->modes[mode].cdevs[j]);
4140 tpnt->modes[mode].cdevs[j] = NULL;
4144 down(&st_ref_sem);
4145 kref_put(&tpnt->kref, scsi_tape_release);
4146 up(&st_ref_sem);
4147 return 0;
4151 write_unlock(&st_dev_arr_lock);
4152 return 0;
4156 * scsi_tape_release - Called to free the Scsi_Tape structure
4157 * @kref: pointer to embedded kref
4159 * st_ref_sem must be held entering this routine. Because it is
4160 * called on last put, you should always use the scsi_tape_get()
4161 * scsi_tape_put() helpers which manipulate the semaphore directly
4162 * and never do a direct kref_put().
4164 static void scsi_tape_release(struct kref *kref)
4166 struct scsi_tape *tpnt = to_scsi_tape(kref);
4167 struct gendisk *disk = tpnt->disk;
4169 tpnt->device = NULL;
4171 if (tpnt->buffer) {
4172 tpnt->buffer->orig_frp_segs = 0;
4173 normalize_buffer(tpnt->buffer);
4174 kfree(tpnt->buffer);
4177 disk->private_data = NULL;
4178 put_disk(disk);
4179 kfree(tpnt);
4180 return;
4183 static void st_intr(struct scsi_cmnd *SCpnt)
4186 * The caller should be checking the request's errors
4187 * value.
4189 scsi_io_completion(SCpnt, SCpnt->bufflen, 0);
4193 * st_init_command: only called via the scsi_cmd_ioctl (block SG_IO)
4194 * interface for REQ_BLOCK_PC commands.
4196 static int st_init_command(struct scsi_cmnd *SCpnt)
4198 if (!(SCpnt->request->flags & REQ_BLOCK_PC))
4199 return 0;
4201 scsi_setup_blk_pc_cmnd(SCpnt);
4202 SCpnt->done = st_intr;
4203 return 1;
4206 static int __init init_st(void)
4208 validate_options();
4210 printk(KERN_INFO
4211 "st: Version %s, fixed bufsize %d, s/g segs %d\n",
4212 verstr, st_fixed_buffer_size, st_max_sg_segs);
4214 st_sysfs_class = class_create(THIS_MODULE, "scsi_tape");
4215 if (IS_ERR(st_sysfs_class)) {
4216 st_sysfs_class = NULL;
4217 printk(KERN_ERR "Unable create sysfs class for SCSI tapes\n");
4218 return 1;
4221 if (!register_chrdev_region(MKDEV(SCSI_TAPE_MAJOR, 0),
4222 ST_MAX_TAPE_ENTRIES, "st")) {
4223 if (scsi_register_driver(&st_template.gendrv) == 0) {
4224 do_create_driverfs_files();
4225 return 0;
4227 unregister_chrdev_region(MKDEV(SCSI_TAPE_MAJOR, 0),
4228 ST_MAX_TAPE_ENTRIES);
4230 class_destroy(st_sysfs_class);
4232 printk(KERN_ERR "Unable to get major %d for SCSI tapes\n", SCSI_TAPE_MAJOR);
4233 return 1;
4236 static void __exit exit_st(void)
4238 do_remove_driverfs_files();
4239 scsi_unregister_driver(&st_template.gendrv);
4240 unregister_chrdev_region(MKDEV(SCSI_TAPE_MAJOR, 0),
4241 ST_MAX_TAPE_ENTRIES);
4242 class_destroy(st_sysfs_class);
4243 kfree(scsi_tapes);
4244 printk(KERN_INFO "st: Unloaded.\n");
4247 module_init(init_st);
4248 module_exit(exit_st);
4251 /* The sysfs driver interface. Read-only at the moment */
4252 static ssize_t st_try_direct_io_show(struct device_driver *ddp, char *buf)
4254 return snprintf(buf, PAGE_SIZE, "%d\n", try_direct_io);
4256 static DRIVER_ATTR(try_direct_io, S_IRUGO, st_try_direct_io_show, NULL);
4258 static ssize_t st_fixed_buffer_size_show(struct device_driver *ddp, char *buf)
4260 return snprintf(buf, PAGE_SIZE, "%d\n", st_fixed_buffer_size);
4262 static DRIVER_ATTR(fixed_buffer_size, S_IRUGO, st_fixed_buffer_size_show, NULL);
4264 static ssize_t st_max_sg_segs_show(struct device_driver *ddp, char *buf)
4266 return snprintf(buf, PAGE_SIZE, "%d\n", st_max_sg_segs);
4268 static DRIVER_ATTR(max_sg_segs, S_IRUGO, st_max_sg_segs_show, NULL);
4270 static ssize_t st_version_show(struct device_driver *ddd, char *buf)
4272 return snprintf(buf, PAGE_SIZE, "[%s]\n", verstr);
4274 static DRIVER_ATTR(version, S_IRUGO, st_version_show, NULL);
4276 static void do_create_driverfs_files(void)
4278 struct device_driver *driverfs = &st_template.gendrv;
4280 driver_create_file(driverfs, &driver_attr_try_direct_io);
4281 driver_create_file(driverfs, &driver_attr_fixed_buffer_size);
4282 driver_create_file(driverfs, &driver_attr_max_sg_segs);
4283 driver_create_file(driverfs, &driver_attr_version);
4286 static void do_remove_driverfs_files(void)
4288 struct device_driver *driverfs = &st_template.gendrv;
4290 driver_remove_file(driverfs, &driver_attr_version);
4291 driver_remove_file(driverfs, &driver_attr_max_sg_segs);
4292 driver_remove_file(driverfs, &driver_attr_fixed_buffer_size);
4293 driver_remove_file(driverfs, &driver_attr_try_direct_io);
4297 /* The sysfs simple class interface */
4298 static ssize_t st_defined_show(struct class_device *class_dev, char *buf)
4300 struct st_modedef *STm = (struct st_modedef *)class_get_devdata(class_dev);
4301 ssize_t l = 0;
4303 l = snprintf(buf, PAGE_SIZE, "%d\n", STm->defined);
4304 return l;
4307 CLASS_DEVICE_ATTR(defined, S_IRUGO, st_defined_show, NULL);
4309 static ssize_t st_defblk_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->default_blksize);
4315 return l;
4318 CLASS_DEVICE_ATTR(default_blksize, S_IRUGO, st_defblk_show, NULL);
4320 static ssize_t st_defdensity_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;
4324 char *fmt;
4326 fmt = STm->default_density >= 0 ? "0x%02x\n" : "%d\n";
4327 l = snprintf(buf, PAGE_SIZE, fmt, STm->default_density);
4328 return l;
4331 CLASS_DEVICE_ATTR(default_density, S_IRUGO, st_defdensity_show, NULL);
4333 static ssize_t st_defcompression_show(struct class_device *class_dev, char *buf)
4335 struct st_modedef *STm = (struct st_modedef *)class_get_devdata(class_dev);
4336 ssize_t l = 0;
4338 l = snprintf(buf, PAGE_SIZE, "%d\n", STm->default_compression - 1);
4339 return l;
4342 CLASS_DEVICE_ATTR(default_compression, S_IRUGO, st_defcompression_show, NULL);
4344 static void do_create_class_files(struct scsi_tape *STp, int dev_num, int mode)
4346 int i, rew, error;
4347 char name[10];
4348 struct class_device *st_class_member;
4350 if (!st_sysfs_class)
4351 return;
4353 for (rew=0; rew < 2; rew++) {
4354 /* Make sure that the minor numbers corresponding to the four
4355 first modes always get the same names */
4356 i = mode << (4 - ST_NBR_MODE_BITS);
4357 snprintf(name, 10, "%s%s%s", rew ? "n" : "",
4358 STp->disk->disk_name, st_formats[i]);
4359 st_class_member =
4360 class_device_create(st_sysfs_class, NULL,
4361 MKDEV(SCSI_TAPE_MAJOR,
4362 TAPE_MINOR(dev_num, mode, rew)),
4363 &STp->device->sdev_gendev, "%s", name);
4364 if (IS_ERR(st_class_member)) {
4365 printk(KERN_WARNING "st%d: class_device_create failed\n",
4366 dev_num);
4367 goto out;
4369 class_set_devdata(st_class_member, &STp->modes[mode]);
4371 class_device_create_file(st_class_member,
4372 &class_device_attr_defined);
4373 class_device_create_file(st_class_member,
4374 &class_device_attr_default_blksize);
4375 class_device_create_file(st_class_member,
4376 &class_device_attr_default_density);
4377 class_device_create_file(st_class_member,
4378 &class_device_attr_default_compression);
4379 if (mode == 0 && rew == 0) {
4380 error = sysfs_create_link(&STp->device->sdev_gendev.kobj,
4381 &st_class_member->kobj,
4382 "tape");
4383 if (error) {
4384 printk(KERN_ERR
4385 "st%d: Can't create sysfs link from SCSI device.\n",
4386 dev_num);
4390 out:
4391 return;
4394 /* The following functions may be useful for a larger audience. */
4395 static int sgl_map_user_pages(struct scatterlist *sgl, const unsigned int max_pages,
4396 unsigned long uaddr, size_t count, int rw)
4398 unsigned long end = (uaddr + count + PAGE_SIZE - 1) >> PAGE_SHIFT;
4399 unsigned long start = uaddr >> PAGE_SHIFT;
4400 const int nr_pages = end - start;
4401 int res, i, j;
4402 struct page **pages;
4404 /* User attempted Overflow! */
4405 if ((uaddr + count) < uaddr)
4406 return -EINVAL;
4408 /* Too big */
4409 if (nr_pages > max_pages)
4410 return -ENOMEM;
4412 /* Hmm? */
4413 if (count == 0)
4414 return 0;
4416 if ((pages = kmalloc(max_pages * sizeof(*pages), GFP_KERNEL)) == NULL)
4417 return -ENOMEM;
4419 /* Try to fault in all of the necessary pages */
4420 down_read(&current->mm->mmap_sem);
4421 /* rw==READ means read from drive, write into memory area */
4422 res = get_user_pages(
4423 current,
4424 current->mm,
4425 uaddr,
4426 nr_pages,
4427 rw == READ,
4428 0, /* don't force */
4429 pages,
4430 NULL);
4431 up_read(&current->mm->mmap_sem);
4433 /* Errors and no page mapped should return here */
4434 if (res < nr_pages)
4435 goto out_unmap;
4437 for (i=0; i < nr_pages; i++) {
4438 /* FIXME: flush superflous for rw==READ,
4439 * probably wrong function for rw==WRITE
4441 flush_dcache_page(pages[i]);
4444 /* Populate the scatter/gather list */
4445 sgl[0].page = pages[0];
4446 sgl[0].offset = uaddr & ~PAGE_MASK;
4447 if (nr_pages > 1) {
4448 sgl[0].length = PAGE_SIZE - sgl[0].offset;
4449 count -= sgl[0].length;
4450 for (i=1; i < nr_pages ; i++) {
4451 sgl[i].offset = 0;
4452 sgl[i].page = pages[i];
4453 sgl[i].length = count < PAGE_SIZE ? count : PAGE_SIZE;
4454 count -= PAGE_SIZE;
4457 else {
4458 sgl[0].length = count;
4461 kfree(pages);
4462 return nr_pages;
4464 out_unmap:
4465 if (res > 0) {
4466 for (j=0; j < res; j++)
4467 page_cache_release(pages[j]);
4468 res = 0;
4470 kfree(pages);
4471 return res;
4475 /* And unmap them... */
4476 static int sgl_unmap_user_pages(struct scatterlist *sgl, const unsigned int nr_pages,
4477 int dirtied)
4479 int i;
4481 for (i=0; i < nr_pages; i++) {
4482 struct page *page = sgl[i].page;
4484 if (dirtied)
4485 SetPageDirty(page);
4486 /* FIXME: cache flush missing for rw==READ
4487 * FIXME: call the correct reference counting function
4489 page_cache_release(page);
4492 return 0;