[SCSI] st: convert dio path to use st_scsi_execute
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / st.c
blobfec5568c711a881554bf009881a79ca2be9cd510
1 /*
2 SCSI Tape Driver for Linux version 1.1 and newer. See the accompanying
3 file Documentation/scsi/st.txt for more information.
5 History:
6 Rewritten from Dwayne Forsyth's SCSI tape driver by Kai Makisara.
7 Contribution and ideas from several people including (in alphabetical
8 order) Klaus Ehrenfried, Eugene Exarevsky, Eric Lee Green, Wolfgang Denk,
9 Steve Hirsch, Andreas Koppenh"ofer, Michael Leodolter, Eyal Lebedinsky,
10 Michael Schaefer, J"org Weule, and Eric Youngdale.
12 Copyright 1992 - 2008 Kai Makisara
13 email Kai.Makisara@kolumbus.fi
15 Some small formal changes - aeb, 950809
17 Last modified: 18-JAN-1998 Richard Gooch <rgooch@atnf.csiro.au> Devfs support
20 static const char *verstr = "20080504";
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/cdev.h>
39 #include <linux/delay.h>
40 #include <linux/mutex.h>
41 #include <linux/smp_lock.h>
43 #include <asm/uaccess.h>
44 #include <asm/dma.h>
45 #include <asm/system.h>
47 #include <scsi/scsi.h>
48 #include <scsi/scsi_dbg.h>
49 #include <scsi/scsi_device.h>
50 #include <scsi/scsi_driver.h>
51 #include <scsi/scsi_eh.h>
52 #include <scsi/scsi_host.h>
53 #include <scsi/scsi_ioctl.h>
54 #include <scsi/sg.h>
57 /* The driver prints some debugging information on the console if DEBUG
58 is defined and non-zero. */
59 #define DEBUG 0
61 #if DEBUG
62 /* The message level for the debug messages is currently set to KERN_NOTICE
63 so that people can easily see the messages. Later when the debugging messages
64 in the drivers are more widely classified, this may be changed to KERN_DEBUG. */
65 #define ST_DEB_MSG KERN_NOTICE
66 #define DEB(a) a
67 #define DEBC(a) if (debugging) { a ; }
68 #else
69 #define DEB(a)
70 #define DEBC(a)
71 #endif
73 #define ST_KILOBYTE 1024
75 #include "st_options.h"
76 #include "st.h"
78 static int buffer_kbs;
79 static int max_sg_segs;
80 static int try_direct_io = TRY_DIRECT_IO;
81 static int try_rdio = 1;
82 static int try_wdio = 1;
84 static int st_dev_max;
85 static int st_nr_dev;
87 static struct class *st_sysfs_class;
89 MODULE_AUTHOR("Kai Makisara");
90 MODULE_DESCRIPTION("SCSI tape (st) driver");
91 MODULE_LICENSE("GPL");
92 MODULE_ALIAS_CHARDEV_MAJOR(SCSI_TAPE_MAJOR);
93 MODULE_ALIAS_SCSI_DEVICE(TYPE_TAPE);
95 /* Set 'perm' (4th argument) to 0 to disable module_param's definition
96 * of sysfs parameters (which module_param doesn't yet support).
97 * Sysfs parameters defined explicitly later.
99 module_param_named(buffer_kbs, buffer_kbs, int, 0);
100 MODULE_PARM_DESC(buffer_kbs, "Default driver buffer size for fixed block mode (KB; 32)");
101 module_param_named(max_sg_segs, max_sg_segs, int, 0);
102 MODULE_PARM_DESC(max_sg_segs, "Maximum number of scatter/gather segments to use (256)");
103 module_param_named(try_direct_io, try_direct_io, int, 0);
104 MODULE_PARM_DESC(try_direct_io, "Try direct I/O between user buffer and tape drive (1)");
106 /* Extra parameters for testing */
107 module_param_named(try_rdio, try_rdio, int, 0);
108 MODULE_PARM_DESC(try_rdio, "Try direct read i/o when possible");
109 module_param_named(try_wdio, try_wdio, int, 0);
110 MODULE_PARM_DESC(try_wdio, "Try direct write i/o when possible");
112 #ifndef MODULE
113 static int write_threshold_kbs; /* retained for compatibility */
114 static struct st_dev_parm {
115 char *name;
116 int *val;
117 } parms[] __initdata = {
119 "buffer_kbs", &buffer_kbs
121 { /* Retained for compatibility with 2.4 */
122 "write_threshold_kbs", &write_threshold_kbs
125 "max_sg_segs", NULL
128 "try_direct_io", &try_direct_io
131 #endif
133 /* Restrict the number of modes so that names for all are assigned */
134 #if ST_NBR_MODES > 16
135 #error "Maximum number of modes is 16"
136 #endif
137 /* Bit reversed order to get same names for same minors with all
138 mode counts */
139 static const char *st_formats[] = {
140 "", "r", "k", "s", "l", "t", "o", "u",
141 "m", "v", "p", "x", "a", "y", "q", "z"};
143 /* The default definitions have been moved to st_options.h */
145 #define ST_FIXED_BUFFER_SIZE (ST_FIXED_BUFFER_BLOCKS * ST_KILOBYTE)
147 /* The buffer size should fit into the 24 bits for length in the
148 6-byte SCSI read and write commands. */
149 #if ST_FIXED_BUFFER_SIZE >= (2 << 24 - 1)
150 #error "Buffer size should not exceed (2 << 24 - 1) bytes!"
151 #endif
153 static int debugging = DEBUG;
155 #define MAX_RETRIES 0
156 #define MAX_WRITE_RETRIES 0
157 #define MAX_READY_RETRIES 0
158 #define NO_TAPE NOT_READY
160 #define ST_TIMEOUT (900 * HZ)
161 #define ST_LONG_TIMEOUT (14000 * HZ)
163 /* Remove mode bits and auto-rewind bit (7) */
164 #define TAPE_NR(x) ( ((iminor(x) & ~255) >> (ST_NBR_MODE_BITS + 1)) | \
165 (iminor(x) & ~(-1 << ST_MODE_SHIFT)) )
166 #define TAPE_MODE(x) ((iminor(x) & ST_MODE_MASK) >> ST_MODE_SHIFT)
168 /* Construct the minor number from the device (d), mode (m), and non-rewind (n) data */
169 #define TAPE_MINOR(d, m, n) (((d & ~(255 >> (ST_NBR_MODE_BITS + 1))) << (ST_NBR_MODE_BITS + 1)) | \
170 (d & (255 >> (ST_NBR_MODE_BITS + 1))) | (m << ST_MODE_SHIFT) | ((n != 0) << 7) )
172 /* Internal ioctl to set both density (uppermost 8 bits) and blocksize (lower
173 24 bits) */
174 #define SET_DENS_AND_BLK 0x10001
176 static DEFINE_RWLOCK(st_dev_arr_lock);
178 static int st_fixed_buffer_size = ST_FIXED_BUFFER_SIZE;
179 static int st_max_sg_segs = ST_MAX_SG;
181 static struct scsi_tape **scsi_tapes = NULL;
183 static int modes_defined;
185 static struct st_buffer *new_tape_buffer(int, int, int);
186 static int enlarge_buffer(struct st_buffer *, int, int);
187 static void clear_buffer(struct st_buffer *);
188 static void normalize_buffer(struct st_buffer *);
189 static int append_to_buffer(const char __user *, struct st_buffer *, int);
190 static int from_buffer(struct st_buffer *, char __user *, int);
191 static void move_buffer_data(struct st_buffer *, int);
192 static void buf_to_sg(struct st_buffer *, unsigned int);
194 static int sgl_map_user_pages(struct st_buffer *, const unsigned int,
195 unsigned long, size_t, int);
196 static int sgl_unmap_user_pages(struct st_buffer *, const unsigned int, int);
198 static int st_probe(struct device *);
199 static int st_remove(struct device *);
201 static int do_create_sysfs_files(void);
202 static void do_remove_sysfs_files(void);
203 static int do_create_class_files(struct scsi_tape *, int, int);
205 static struct scsi_driver st_template = {
206 .owner = THIS_MODULE,
207 .gendrv = {
208 .name = "st",
209 .probe = st_probe,
210 .remove = st_remove,
214 static int st_compression(struct scsi_tape *, int);
216 static int find_partition(struct scsi_tape *);
217 static int switch_partition(struct scsi_tape *);
219 static int st_int_ioctl(struct scsi_tape *, unsigned int, unsigned long);
221 static void scsi_tape_release(struct kref *);
223 #define to_scsi_tape(obj) container_of(obj, struct scsi_tape, kref)
225 static DEFINE_MUTEX(st_ref_mutex);
228 #include "osst_detect.h"
229 #ifndef SIGS_FROM_OSST
230 #define SIGS_FROM_OSST \
231 {"OnStream", "SC-", "", "osst"}, \
232 {"OnStream", "DI-", "", "osst"}, \
233 {"OnStream", "DP-", "", "osst"}, \
234 {"OnStream", "USB", "", "osst"}, \
235 {"OnStream", "FW-", "", "osst"}
236 #endif
238 static struct scsi_tape *scsi_tape_get(int dev)
240 struct scsi_tape *STp = NULL;
242 mutex_lock(&st_ref_mutex);
243 write_lock(&st_dev_arr_lock);
245 if (dev < st_dev_max && scsi_tapes != NULL)
246 STp = scsi_tapes[dev];
247 if (!STp) goto out;
249 kref_get(&STp->kref);
251 if (!STp->device)
252 goto out_put;
254 if (scsi_device_get(STp->device))
255 goto out_put;
257 goto out;
259 out_put:
260 kref_put(&STp->kref, scsi_tape_release);
261 STp = NULL;
262 out:
263 write_unlock(&st_dev_arr_lock);
264 mutex_unlock(&st_ref_mutex);
265 return STp;
268 static void scsi_tape_put(struct scsi_tape *STp)
270 struct scsi_device *sdev = STp->device;
272 mutex_lock(&st_ref_mutex);
273 kref_put(&STp->kref, scsi_tape_release);
274 scsi_device_put(sdev);
275 mutex_unlock(&st_ref_mutex);
278 struct st_reject_data {
279 char *vendor;
280 char *model;
281 char *rev;
282 char *driver_hint; /* Name of the correct driver, NULL if unknown */
285 static struct st_reject_data reject_list[] = {
286 /* {"XXX", "Yy-", "", NULL}, example */
287 SIGS_FROM_OSST,
288 {NULL, }};
290 /* If the device signature is on the list of incompatible drives, the
291 function returns a pointer to the name of the correct driver (if known) */
292 static char * st_incompatible(struct scsi_device* SDp)
294 struct st_reject_data *rp;
296 for (rp=&(reject_list[0]); rp->vendor != NULL; rp++)
297 if (!strncmp(rp->vendor, SDp->vendor, strlen(rp->vendor)) &&
298 !strncmp(rp->model, SDp->model, strlen(rp->model)) &&
299 !strncmp(rp->rev, SDp->rev, strlen(rp->rev))) {
300 if (rp->driver_hint)
301 return rp->driver_hint;
302 else
303 return "unknown";
305 return NULL;
309 static inline char *tape_name(struct scsi_tape *tape)
311 return tape->disk->disk_name;
315 static void st_analyze_sense(struct st_request *SRpnt, struct st_cmdstatus *s)
317 const u8 *ucp;
318 const u8 *sense = SRpnt->sense;
320 s->have_sense = scsi_normalize_sense(SRpnt->sense,
321 SCSI_SENSE_BUFFERSIZE, &s->sense_hdr);
322 s->flags = 0;
324 if (s->have_sense) {
325 s->deferred = 0;
326 s->remainder_valid =
327 scsi_get_sense_info_fld(sense, SCSI_SENSE_BUFFERSIZE, &s->uremainder64);
328 switch (sense[0] & 0x7f) {
329 case 0x71:
330 s->deferred = 1;
331 case 0x70:
332 s->fixed_format = 1;
333 s->flags = sense[2] & 0xe0;
334 break;
335 case 0x73:
336 s->deferred = 1;
337 case 0x72:
338 s->fixed_format = 0;
339 ucp = scsi_sense_desc_find(sense, SCSI_SENSE_BUFFERSIZE, 4);
340 s->flags = ucp ? (ucp[3] & 0xe0) : 0;
341 break;
347 /* Convert the result to success code */
348 static int st_chk_result(struct scsi_tape *STp, struct st_request * SRpnt)
350 int result = SRpnt->result;
351 u8 scode;
352 DEB(const char *stp;)
353 char *name = tape_name(STp);
354 struct st_cmdstatus *cmdstatp;
356 if (!result)
357 return 0;
359 cmdstatp = &STp->buffer->cmdstat;
360 st_analyze_sense(SRpnt, cmdstatp);
362 if (cmdstatp->have_sense)
363 scode = STp->buffer->cmdstat.sense_hdr.sense_key;
364 else
365 scode = 0;
367 DEB(
368 if (debugging) {
369 printk(ST_DEB_MSG "%s: Error: %x, cmd: %x %x %x %x %x %x\n",
370 name, result,
371 SRpnt->cmd[0], SRpnt->cmd[1], SRpnt->cmd[2],
372 SRpnt->cmd[3], SRpnt->cmd[4], SRpnt->cmd[5]);
373 if (cmdstatp->have_sense)
374 __scsi_print_sense(name, SRpnt->sense, SCSI_SENSE_BUFFERSIZE);
375 } ) /* end DEB */
376 if (!debugging) { /* Abnormal conditions for tape */
377 if (!cmdstatp->have_sense)
378 printk(KERN_WARNING
379 "%s: Error %x (sugg. bt 0x%x, driver bt 0x%x, host bt 0x%x).\n",
380 name, result, suggestion(result),
381 driver_byte(result) & DRIVER_MASK, host_byte(result));
382 else if (cmdstatp->have_sense &&
383 scode != NO_SENSE &&
384 scode != RECOVERED_ERROR &&
385 /* scode != UNIT_ATTENTION && */
386 scode != BLANK_CHECK &&
387 scode != VOLUME_OVERFLOW &&
388 SRpnt->cmd[0] != MODE_SENSE &&
389 SRpnt->cmd[0] != TEST_UNIT_READY) {
391 __scsi_print_sense(name, SRpnt->sense, SCSI_SENSE_BUFFERSIZE);
395 if (cmdstatp->fixed_format &&
396 STp->cln_mode >= EXTENDED_SENSE_START) { /* Only fixed format sense */
397 if (STp->cln_sense_value)
398 STp->cleaning_req |= ((SRpnt->sense[STp->cln_mode] &
399 STp->cln_sense_mask) == STp->cln_sense_value);
400 else
401 STp->cleaning_req |= ((SRpnt->sense[STp->cln_mode] &
402 STp->cln_sense_mask) != 0);
404 if (cmdstatp->have_sense &&
405 cmdstatp->sense_hdr.asc == 0 && cmdstatp->sense_hdr.ascq == 0x17)
406 STp->cleaning_req = 1; /* ASC and ASCQ => cleaning requested */
408 STp->pos_unknown |= STp->device->was_reset;
410 if (cmdstatp->have_sense &&
411 scode == RECOVERED_ERROR
412 #if ST_RECOVERED_WRITE_FATAL
413 && SRpnt->cmd[0] != WRITE_6
414 && SRpnt->cmd[0] != WRITE_FILEMARKS
415 #endif
417 STp->recover_count++;
418 STp->recover_reg++;
420 DEB(
421 if (debugging) {
422 if (SRpnt->cmd[0] == READ_6)
423 stp = "read";
424 else if (SRpnt->cmd[0] == WRITE_6)
425 stp = "write";
426 else
427 stp = "ioctl";
428 printk(ST_DEB_MSG "%s: Recovered %s error (%d).\n", name, stp,
429 STp->recover_count);
430 } ) /* end DEB */
432 if (cmdstatp->flags == 0)
433 return 0;
435 return (-EIO);
438 static struct st_request *st_allocate_request(struct scsi_tape *stp)
440 struct st_request *streq;
442 streq = kzalloc(sizeof(*streq), GFP_KERNEL);
443 if (streq)
444 streq->stp = stp;
445 else {
446 DEBC(printk(KERN_ERR "%s: Can't get SCSI request.\n",
447 tape_name(stp)););
448 if (signal_pending(current))
449 stp->buffer->syscall_result = -EINTR;
450 else
451 stp->buffer->syscall_result = -EBUSY;
454 return streq;
457 static void st_release_request(struct st_request *streq)
459 kfree(streq);
462 static void st_scsi_execute_end(struct request *req, int uptodate)
464 struct st_request *SRpnt = req->end_io_data;
465 struct scsi_tape *STp = SRpnt->stp;
467 STp->buffer->cmdstat.midlevel_result = SRpnt->result = req->errors;
468 STp->buffer->cmdstat.residual = req->data_len;
470 if (SRpnt->waiting)
471 complete(SRpnt->waiting);
473 blk_rq_unmap_user(SRpnt->bio);
474 __blk_put_request(req->q, req);
477 static int st_scsi_execute(struct st_request *SRpnt, const unsigned char *cmd,
478 int data_direction, void *buffer, unsigned bufflen,
479 int timeout, int retries)
481 struct request *req;
482 struct rq_map_data *mdata = &SRpnt->stp->buffer->map_data;
483 int err = 0;
484 int write = (data_direction == DMA_TO_DEVICE);
486 req = blk_get_request(SRpnt->stp->device->request_queue, write,
487 GFP_KERNEL);
488 if (!req)
489 return DRIVER_ERROR << 24;
491 req->cmd_type = REQ_TYPE_BLOCK_PC;
492 req->cmd_flags |= REQ_QUIET;
494 mdata->null_mapped = 1;
496 err = blk_rq_map_user(req->q, req, mdata, NULL, bufflen, GFP_KERNEL);
497 if (err) {
498 blk_put_request(req);
499 return DRIVER_ERROR << 24;
502 SRpnt->bio = req->bio;
503 req->cmd_len = COMMAND_SIZE(cmd[0]);
504 memset(req->cmd, 0, BLK_MAX_CDB);
505 memcpy(req->cmd, cmd, req->cmd_len);
506 req->sense = SRpnt->sense;
507 req->sense_len = 0;
508 req->timeout = timeout;
509 req->retries = retries;
510 req->end_io_data = SRpnt;
512 blk_execute_rq_nowait(req->q, NULL, req, 1, st_scsi_execute_end);
513 return 0;
516 /* Do the scsi command. Waits until command performed if do_wait is true.
517 Otherwise write_behind_check() is used to check that the command
518 has finished. */
519 static struct st_request *
520 st_do_scsi(struct st_request * SRpnt, struct scsi_tape * STp, unsigned char *cmd,
521 int bytes, int direction, int timeout, int retries, int do_wait)
523 struct completion *waiting;
524 struct rq_map_data *mdata = &STp->buffer->map_data;
525 int ret;
527 /* if async, make sure there's no command outstanding */
528 if (!do_wait && ((STp->buffer)->last_SRpnt)) {
529 printk(KERN_ERR "%s: Async command already active.\n",
530 tape_name(STp));
531 if (signal_pending(current))
532 (STp->buffer)->syscall_result = (-EINTR);
533 else
534 (STp->buffer)->syscall_result = (-EBUSY);
535 return NULL;
538 if (!SRpnt) {
539 SRpnt = st_allocate_request(STp);
540 if (!SRpnt)
541 return NULL;
544 /* If async IO, set last_SRpnt. This ptr tells write_behind_check
545 which IO is outstanding. It's nulled out when the IO completes. */
546 if (!do_wait)
547 (STp->buffer)->last_SRpnt = SRpnt;
549 waiting = &STp->wait;
550 init_completion(waiting);
551 SRpnt->waiting = waiting;
553 if (STp->buffer->do_dio) {
554 mdata->nr_entries = STp->buffer->sg_segs;
555 mdata->pages = STp->buffer->mapped_pages;
556 } else {
557 buf_to_sg(STp->buffer, bytes);
559 mdata->nr_entries =
560 DIV_ROUND_UP(bytes, PAGE_SIZE << mdata->page_order);
561 STp->buffer->map_data.pages = STp->buffer->reserved_pages;
562 STp->buffer->map_data.offset = 0;
565 memcpy(SRpnt->cmd, cmd, sizeof(SRpnt->cmd));
566 STp->buffer->cmdstat.have_sense = 0;
567 STp->buffer->syscall_result = 0;
569 ret = st_scsi_execute(SRpnt, cmd, direction, NULL, bytes, timeout,
570 retries);
571 if (ret) {
572 /* could not allocate the buffer or request was too large */
573 (STp->buffer)->syscall_result = (-EBUSY);
574 (STp->buffer)->last_SRpnt = NULL;
575 } else if (do_wait) {
576 wait_for_completion(waiting);
577 SRpnt->waiting = NULL;
578 (STp->buffer)->syscall_result = st_chk_result(STp, SRpnt);
581 return SRpnt;
584 static int st_scsi_kern_execute(struct st_request *streq,
585 const unsigned char *cmd, int data_direction,
586 void *buffer, unsigned bufflen, int timeout,
587 int retries)
589 struct scsi_tape *stp = streq->stp;
590 int ret, resid;
592 stp->buffer->cmdstat.have_sense = 0;
593 memcpy(streq->cmd, cmd, sizeof(streq->cmd));
595 ret = scsi_execute(stp->device, cmd, data_direction, buffer, bufflen,
596 streq->sense, timeout, retries, 0, &resid);
597 if (driver_byte(ret) & DRIVER_ERROR)
598 return -EBUSY;
600 stp->buffer->cmdstat.midlevel_result = streq->result = ret;
601 stp->buffer->cmdstat.residual = resid;
602 stp->buffer->syscall_result = st_chk_result(stp, streq);
604 return 0;
607 /* Handle the write-behind checking (waits for completion). Returns -ENOSPC if
608 write has been correct but EOM early warning reached, -EIO if write ended in
609 error or zero if write successful. Asynchronous writes are used only in
610 variable block mode. */
611 static int write_behind_check(struct scsi_tape * STp)
613 int retval = 0;
614 struct st_buffer *STbuffer;
615 struct st_partstat *STps;
616 struct st_cmdstatus *cmdstatp;
617 struct st_request *SRpnt;
619 STbuffer = STp->buffer;
620 if (!STbuffer->writing)
621 return 0;
623 DEB(
624 if (STp->write_pending)
625 STp->nbr_waits++;
626 else
627 STp->nbr_finished++;
628 ) /* end DEB */
630 wait_for_completion(&(STp->wait));
631 SRpnt = STbuffer->last_SRpnt;
632 STbuffer->last_SRpnt = NULL;
633 SRpnt->waiting = NULL;
635 (STp->buffer)->syscall_result = st_chk_result(STp, SRpnt);
636 st_release_request(SRpnt);
638 STbuffer->buffer_bytes -= STbuffer->writing;
639 STps = &(STp->ps[STp->partition]);
640 if (STps->drv_block >= 0) {
641 if (STp->block_size == 0)
642 STps->drv_block++;
643 else
644 STps->drv_block += STbuffer->writing / STp->block_size;
647 cmdstatp = &STbuffer->cmdstat;
648 if (STbuffer->syscall_result) {
649 retval = -EIO;
650 if (cmdstatp->have_sense && !cmdstatp->deferred &&
651 (cmdstatp->flags & SENSE_EOM) &&
652 (cmdstatp->sense_hdr.sense_key == NO_SENSE ||
653 cmdstatp->sense_hdr.sense_key == RECOVERED_ERROR)) {
654 /* EOM at write-behind, has all data been written? */
655 if (!cmdstatp->remainder_valid ||
656 cmdstatp->uremainder64 == 0)
657 retval = -ENOSPC;
659 if (retval == -EIO)
660 STps->drv_block = -1;
662 STbuffer->writing = 0;
664 DEB(if (debugging && retval)
665 printk(ST_DEB_MSG "%s: Async write error %x, return value %d.\n",
666 tape_name(STp), STbuffer->cmdstat.midlevel_result, retval);) /* end DEB */
668 return retval;
672 /* Step over EOF if it has been inadvertently crossed (ioctl not used because
673 it messes up the block number). */
674 static int cross_eof(struct scsi_tape * STp, int forward)
676 struct st_request *SRpnt;
677 unsigned char cmd[MAX_COMMAND_SIZE];
678 int ret;
680 cmd[0] = SPACE;
681 cmd[1] = 0x01; /* Space FileMarks */
682 if (forward) {
683 cmd[2] = cmd[3] = 0;
684 cmd[4] = 1;
685 } else
686 cmd[2] = cmd[3] = cmd[4] = 0xff; /* -1 filemarks */
687 cmd[5] = 0;
689 DEBC(printk(ST_DEB_MSG "%s: Stepping over filemark %s.\n",
690 tape_name(STp), forward ? "forward" : "backward"));
692 SRpnt = st_allocate_request(STp);
693 if (!SRpnt)
694 return STp->buffer->syscall_result;
696 ret = st_scsi_kern_execute(SRpnt, cmd, DMA_NONE, NULL, 0,
697 STp->device->request_queue->rq_timeout,
698 MAX_RETRIES);
699 if (ret)
700 goto out;
702 ret = STp->buffer->syscall_result;
704 if ((STp->buffer)->cmdstat.midlevel_result != 0)
705 printk(KERN_ERR "%s: Stepping over filemark %s failed.\n",
706 tape_name(STp), forward ? "forward" : "backward");
708 out:
709 st_release_request(SRpnt);
711 return ret;
715 /* Flush the write buffer (never need to write if variable blocksize). */
716 static int st_flush_write_buffer(struct scsi_tape * STp)
718 int transfer, blks;
719 int result;
720 unsigned char cmd[MAX_COMMAND_SIZE];
721 struct st_request *SRpnt;
722 struct st_partstat *STps;
724 result = write_behind_check(STp);
725 if (result)
726 return result;
728 result = 0;
729 if (STp->dirty == 1) {
731 transfer = STp->buffer->buffer_bytes;
732 DEBC(printk(ST_DEB_MSG "%s: Flushing %d bytes.\n",
733 tape_name(STp), transfer));
735 memset(cmd, 0, MAX_COMMAND_SIZE);
736 cmd[0] = WRITE_6;
737 cmd[1] = 1;
738 blks = transfer / STp->block_size;
739 cmd[2] = blks >> 16;
740 cmd[3] = blks >> 8;
741 cmd[4] = blks;
743 SRpnt = st_do_scsi(NULL, STp, cmd, transfer, DMA_TO_DEVICE,
744 STp->device->request_queue->rq_timeout,
745 MAX_WRITE_RETRIES, 1);
746 if (!SRpnt)
747 return (STp->buffer)->syscall_result;
749 STps = &(STp->ps[STp->partition]);
750 if ((STp->buffer)->syscall_result != 0) {
751 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
753 if (cmdstatp->have_sense && !cmdstatp->deferred &&
754 (cmdstatp->flags & SENSE_EOM) &&
755 (cmdstatp->sense_hdr.sense_key == NO_SENSE ||
756 cmdstatp->sense_hdr.sense_key == RECOVERED_ERROR) &&
757 (!cmdstatp->remainder_valid ||
758 cmdstatp->uremainder64 == 0)) { /* All written at EOM early warning */
759 STp->dirty = 0;
760 (STp->buffer)->buffer_bytes = 0;
761 if (STps->drv_block >= 0)
762 STps->drv_block += blks;
763 result = (-ENOSPC);
764 } else {
765 printk(KERN_ERR "%s: Error on flush.\n",
766 tape_name(STp));
767 STps->drv_block = (-1);
768 result = (-EIO);
770 } else {
771 if (STps->drv_block >= 0)
772 STps->drv_block += blks;
773 STp->dirty = 0;
774 (STp->buffer)->buffer_bytes = 0;
776 st_release_request(SRpnt);
777 SRpnt = NULL;
779 return result;
783 /* Flush the tape buffer. The tape will be positioned correctly unless
784 seek_next is true. */
785 static int flush_buffer(struct scsi_tape *STp, int seek_next)
787 int backspace, result;
788 struct st_buffer *STbuffer;
789 struct st_partstat *STps;
791 STbuffer = STp->buffer;
794 * If there was a bus reset, block further access
795 * to this device.
797 if (STp->pos_unknown)
798 return (-EIO);
800 if (STp->ready != ST_READY)
801 return 0;
802 STps = &(STp->ps[STp->partition]);
803 if (STps->rw == ST_WRITING) /* Writing */
804 return st_flush_write_buffer(STp);
806 if (STp->block_size == 0)
807 return 0;
809 backspace = ((STp->buffer)->buffer_bytes +
810 (STp->buffer)->read_pointer) / STp->block_size -
811 ((STp->buffer)->read_pointer + STp->block_size - 1) /
812 STp->block_size;
813 (STp->buffer)->buffer_bytes = 0;
814 (STp->buffer)->read_pointer = 0;
815 result = 0;
816 if (!seek_next) {
817 if (STps->eof == ST_FM_HIT) {
818 result = cross_eof(STp, 0); /* Back over the EOF hit */
819 if (!result)
820 STps->eof = ST_NOEOF;
821 else {
822 if (STps->drv_file >= 0)
823 STps->drv_file++;
824 STps->drv_block = 0;
827 if (!result && backspace > 0)
828 result = st_int_ioctl(STp, MTBSR, backspace);
829 } else if (STps->eof == ST_FM_HIT) {
830 if (STps->drv_file >= 0)
831 STps->drv_file++;
832 STps->drv_block = 0;
833 STps->eof = ST_NOEOF;
835 return result;
839 /* Set the mode parameters */
840 static int set_mode_densblk(struct scsi_tape * STp, struct st_modedef * STm)
842 int set_it = 0;
843 unsigned long arg;
844 char *name = tape_name(STp);
846 if (!STp->density_changed &&
847 STm->default_density >= 0 &&
848 STm->default_density != STp->density) {
849 arg = STm->default_density;
850 set_it = 1;
851 } else
852 arg = STp->density;
853 arg <<= MT_ST_DENSITY_SHIFT;
854 if (!STp->blksize_changed &&
855 STm->default_blksize >= 0 &&
856 STm->default_blksize != STp->block_size) {
857 arg |= STm->default_blksize;
858 set_it = 1;
859 } else
860 arg |= STp->block_size;
861 if (set_it &&
862 st_int_ioctl(STp, SET_DENS_AND_BLK, arg)) {
863 printk(KERN_WARNING
864 "%s: Can't set default block size to %d bytes and density %x.\n",
865 name, STm->default_blksize, STm->default_density);
866 if (modes_defined)
867 return (-EINVAL);
869 return 0;
873 /* Lock or unlock the drive door. Don't use when st_request allocated. */
874 static int do_door_lock(struct scsi_tape * STp, int do_lock)
876 int retval, cmd;
877 DEB(char *name = tape_name(STp);)
880 cmd = do_lock ? SCSI_IOCTL_DOORLOCK : SCSI_IOCTL_DOORUNLOCK;
881 DEBC(printk(ST_DEB_MSG "%s: %socking drive door.\n", name,
882 do_lock ? "L" : "Unl"));
883 retval = scsi_ioctl(STp->device, cmd, NULL);
884 if (!retval) {
885 STp->door_locked = do_lock ? ST_LOCKED_EXPLICIT : ST_UNLOCKED;
887 else {
888 STp->door_locked = ST_LOCK_FAILS;
890 return retval;
894 /* Set the internal state after reset */
895 static void reset_state(struct scsi_tape *STp)
897 int i;
898 struct st_partstat *STps;
900 STp->pos_unknown = 0;
901 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
902 STps = &(STp->ps[i]);
903 STps->rw = ST_IDLE;
904 STps->eof = ST_NOEOF;
905 STps->at_sm = 0;
906 STps->last_block_valid = 0;
907 STps->drv_block = -1;
908 STps->drv_file = -1;
910 if (STp->can_partitions) {
911 STp->partition = find_partition(STp);
912 if (STp->partition < 0)
913 STp->partition = 0;
914 STp->new_partition = STp->partition;
918 /* Test if the drive is ready. Returns either one of the codes below or a negative system
919 error code. */
920 #define CHKRES_READY 0
921 #define CHKRES_NEW_SESSION 1
922 #define CHKRES_NOT_READY 2
923 #define CHKRES_NO_TAPE 3
925 #define MAX_ATTENTIONS 10
927 static int test_ready(struct scsi_tape *STp, int do_wait)
929 int attentions, waits, max_wait, scode;
930 int retval = CHKRES_READY, new_session = 0;
931 unsigned char cmd[MAX_COMMAND_SIZE];
932 struct st_request *SRpnt;
933 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
935 SRpnt = st_allocate_request(STp);
936 if (!SRpnt)
937 return STp->buffer->syscall_result;
939 max_wait = do_wait ? ST_BLOCK_SECONDS : 0;
941 for (attentions=waits=0; ; ) {
942 memset((void *) &cmd[0], 0, MAX_COMMAND_SIZE);
943 cmd[0] = TEST_UNIT_READY;
945 retval = st_scsi_kern_execute(SRpnt, cmd, DMA_NONE, NULL, 0,
946 STp->long_timeout,
947 MAX_READY_RETRIES);
948 if (retval)
949 break;
951 if (cmdstatp->have_sense) {
953 scode = cmdstatp->sense_hdr.sense_key;
955 if (scode == UNIT_ATTENTION) { /* New media? */
956 new_session = 1;
957 if (attentions < MAX_ATTENTIONS) {
958 attentions++;
959 continue;
961 else {
962 retval = (-EIO);
963 break;
967 if (scode == NOT_READY) {
968 if (waits < max_wait) {
969 if (msleep_interruptible(1000)) {
970 retval = (-EINTR);
971 break;
973 waits++;
974 continue;
976 else {
977 if ((STp->device)->scsi_level >= SCSI_2 &&
978 cmdstatp->sense_hdr.asc == 0x3a) /* Check ASC */
979 retval = CHKRES_NO_TAPE;
980 else
981 retval = CHKRES_NOT_READY;
982 break;
987 retval = (STp->buffer)->syscall_result;
988 if (!retval)
989 retval = new_session ? CHKRES_NEW_SESSION : CHKRES_READY;
990 break;
993 st_release_request(SRpnt);
995 return retval;
999 /* See if the drive is ready and gather information about the tape. Return values:
1000 < 0 negative error code from errno.h
1001 0 drive ready
1002 1 drive not ready (possibly no tape)
1004 static int check_tape(struct scsi_tape *STp, struct file *filp)
1006 int i, retval, new_session = 0, do_wait;
1007 unsigned char cmd[MAX_COMMAND_SIZE], saved_cleaning;
1008 unsigned short st_flags = filp->f_flags;
1009 struct st_request *SRpnt = NULL;
1010 struct st_modedef *STm;
1011 struct st_partstat *STps;
1012 char *name = tape_name(STp);
1013 struct inode *inode = filp->f_path.dentry->d_inode;
1014 int mode = TAPE_MODE(inode);
1016 STp->ready = ST_READY;
1018 if (mode != STp->current_mode) {
1019 DEBC(printk(ST_DEB_MSG "%s: Mode change from %d to %d.\n",
1020 name, STp->current_mode, mode));
1021 new_session = 1;
1022 STp->current_mode = mode;
1024 STm = &(STp->modes[STp->current_mode]);
1026 saved_cleaning = STp->cleaning_req;
1027 STp->cleaning_req = 0;
1029 do_wait = ((filp->f_flags & O_NONBLOCK) == 0);
1030 retval = test_ready(STp, do_wait);
1032 if (retval < 0)
1033 goto err_out;
1035 if (retval == CHKRES_NEW_SESSION) {
1036 STp->pos_unknown = 0;
1037 STp->partition = STp->new_partition = 0;
1038 if (STp->can_partitions)
1039 STp->nbr_partitions = 1; /* This guess will be updated later
1040 if necessary */
1041 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
1042 STps = &(STp->ps[i]);
1043 STps->rw = ST_IDLE;
1044 STps->eof = ST_NOEOF;
1045 STps->at_sm = 0;
1046 STps->last_block_valid = 0;
1047 STps->drv_block = 0;
1048 STps->drv_file = 0;
1050 new_session = 1;
1052 else {
1053 STp->cleaning_req |= saved_cleaning;
1055 if (retval == CHKRES_NOT_READY || retval == CHKRES_NO_TAPE) {
1056 if (retval == CHKRES_NO_TAPE)
1057 STp->ready = ST_NO_TAPE;
1058 else
1059 STp->ready = ST_NOT_READY;
1061 STp->density = 0; /* Clear the erroneous "residue" */
1062 STp->write_prot = 0;
1063 STp->block_size = 0;
1064 STp->ps[0].drv_file = STp->ps[0].drv_block = (-1);
1065 STp->partition = STp->new_partition = 0;
1066 STp->door_locked = ST_UNLOCKED;
1067 return CHKRES_NOT_READY;
1071 SRpnt = st_allocate_request(STp);
1072 if (!SRpnt) {
1073 retval = STp->buffer->syscall_result;
1074 goto err_out;
1077 if (STp->omit_blklims)
1078 STp->min_block = STp->max_block = (-1);
1079 else {
1080 memset((void *) &cmd[0], 0, MAX_COMMAND_SIZE);
1081 cmd[0] = READ_BLOCK_LIMITS;
1083 retval = st_scsi_kern_execute(SRpnt, cmd, DMA_FROM_DEVICE,
1084 STp->buffer->b_data, 6,
1085 STp->device->request_queue->rq_timeout,
1086 MAX_READY_RETRIES);
1087 if (retval) {
1088 st_release_request(SRpnt);
1089 goto err_out;
1092 if (!SRpnt->result && !STp->buffer->cmdstat.have_sense) {
1093 STp->max_block = ((STp->buffer)->b_data[1] << 16) |
1094 ((STp->buffer)->b_data[2] << 8) | (STp->buffer)->b_data[3];
1095 STp->min_block = ((STp->buffer)->b_data[4] << 8) |
1096 (STp->buffer)->b_data[5];
1097 if ( DEB( debugging || ) !STp->inited)
1098 printk(KERN_INFO
1099 "%s: Block limits %d - %d bytes.\n", name,
1100 STp->min_block, STp->max_block);
1101 } else {
1102 STp->min_block = STp->max_block = (-1);
1103 DEBC(printk(ST_DEB_MSG "%s: Can't read block limits.\n",
1104 name));
1108 memset((void *) &cmd[0], 0, MAX_COMMAND_SIZE);
1109 cmd[0] = MODE_SENSE;
1110 cmd[4] = 12;
1112 retval = st_scsi_kern_execute(SRpnt, cmd, DMA_FROM_DEVICE,
1113 STp->buffer->b_data, 12,
1114 STp->device->request_queue->rq_timeout,
1115 MAX_READY_RETRIES);
1116 if (retval) {
1117 st_release_request(SRpnt);
1118 goto err_out;
1121 if ((STp->buffer)->syscall_result != 0) {
1122 DEBC(printk(ST_DEB_MSG "%s: No Mode Sense.\n", name));
1123 STp->block_size = ST_DEFAULT_BLOCK; /* Educated guess (?) */
1124 (STp->buffer)->syscall_result = 0; /* Prevent error propagation */
1125 STp->drv_write_prot = 0;
1126 } else {
1127 DEBC(printk(ST_DEB_MSG
1128 "%s: Mode sense. Length %d, medium %x, WBS %x, BLL %d\n",
1129 name,
1130 (STp->buffer)->b_data[0], (STp->buffer)->b_data[1],
1131 (STp->buffer)->b_data[2], (STp->buffer)->b_data[3]));
1133 if ((STp->buffer)->b_data[3] >= 8) {
1134 STp->drv_buffer = ((STp->buffer)->b_data[2] >> 4) & 7;
1135 STp->density = (STp->buffer)->b_data[4];
1136 STp->block_size = (STp->buffer)->b_data[9] * 65536 +
1137 (STp->buffer)->b_data[10] * 256 + (STp->buffer)->b_data[11];
1138 DEBC(printk(ST_DEB_MSG
1139 "%s: Density %x, tape length: %x, drv buffer: %d\n",
1140 name, STp->density, (STp->buffer)->b_data[5] * 65536 +
1141 (STp->buffer)->b_data[6] * 256 + (STp->buffer)->b_data[7],
1142 STp->drv_buffer));
1144 STp->drv_write_prot = ((STp->buffer)->b_data[2] & 0x80) != 0;
1146 st_release_request(SRpnt);
1147 SRpnt = NULL;
1148 STp->inited = 1;
1150 if (STp->block_size > 0)
1151 (STp->buffer)->buffer_blocks =
1152 (STp->buffer)->buffer_size / STp->block_size;
1153 else
1154 (STp->buffer)->buffer_blocks = 1;
1155 (STp->buffer)->buffer_bytes = (STp->buffer)->read_pointer = 0;
1157 DEBC(printk(ST_DEB_MSG
1158 "%s: Block size: %d, buffer size: %d (%d blocks).\n", name,
1159 STp->block_size, (STp->buffer)->buffer_size,
1160 (STp->buffer)->buffer_blocks));
1162 if (STp->drv_write_prot) {
1163 STp->write_prot = 1;
1165 DEBC(printk(ST_DEB_MSG "%s: Write protected\n", name));
1167 if (do_wait &&
1168 ((st_flags & O_ACCMODE) == O_WRONLY ||
1169 (st_flags & O_ACCMODE) == O_RDWR)) {
1170 retval = (-EROFS);
1171 goto err_out;
1175 if (STp->can_partitions && STp->nbr_partitions < 1) {
1176 /* This code is reached when the device is opened for the first time
1177 after the driver has been initialized with tape in the drive and the
1178 partition support has been enabled. */
1179 DEBC(printk(ST_DEB_MSG
1180 "%s: Updating partition number in status.\n", name));
1181 if ((STp->partition = find_partition(STp)) < 0) {
1182 retval = STp->partition;
1183 goto err_out;
1185 STp->new_partition = STp->partition;
1186 STp->nbr_partitions = 1; /* This guess will be updated when necessary */
1189 if (new_session) { /* Change the drive parameters for the new mode */
1190 STp->density_changed = STp->blksize_changed = 0;
1191 STp->compression_changed = 0;
1192 if (!(STm->defaults_for_writes) &&
1193 (retval = set_mode_densblk(STp, STm)) < 0)
1194 goto err_out;
1196 if (STp->default_drvbuffer != 0xff) {
1197 if (st_int_ioctl(STp, MTSETDRVBUFFER, STp->default_drvbuffer))
1198 printk(KERN_WARNING
1199 "%s: Can't set default drive buffering to %d.\n",
1200 name, STp->default_drvbuffer);
1204 return CHKRES_READY;
1206 err_out:
1207 return retval;
1211 \f/* Open the device. Needs to take the BKL only because of incrementing the SCSI host
1212 module count. */
1213 static int st_open(struct inode *inode, struct file *filp)
1215 int i, retval = (-EIO);
1216 struct scsi_tape *STp;
1217 struct st_partstat *STps;
1218 int dev = TAPE_NR(inode);
1219 char *name;
1221 lock_kernel();
1223 * We really want to do nonseekable_open(inode, filp); here, but some
1224 * versions of tar incorrectly call lseek on tapes and bail out if that
1225 * fails. So we disallow pread() and pwrite(), but permit lseeks.
1227 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
1229 if (!(STp = scsi_tape_get(dev))) {
1230 unlock_kernel();
1231 return -ENXIO;
1234 write_lock(&st_dev_arr_lock);
1235 filp->private_data = STp;
1236 name = tape_name(STp);
1238 if (STp->in_use) {
1239 write_unlock(&st_dev_arr_lock);
1240 scsi_tape_put(STp);
1241 unlock_kernel();
1242 DEB( printk(ST_DEB_MSG "%s: Device already in use.\n", name); )
1243 return (-EBUSY);
1246 STp->in_use = 1;
1247 write_unlock(&st_dev_arr_lock);
1248 STp->rew_at_close = STp->autorew_dev = (iminor(inode) & 0x80) == 0;
1250 if (!scsi_block_when_processing_errors(STp->device)) {
1251 retval = (-ENXIO);
1252 goto err_out;
1255 /* See that we have at least a one page buffer available */
1256 if (!enlarge_buffer(STp->buffer, PAGE_SIZE, STp->restr_dma)) {
1257 printk(KERN_WARNING "%s: Can't allocate one page tape buffer.\n",
1258 name);
1259 retval = (-EOVERFLOW);
1260 goto err_out;
1263 (STp->buffer)->cleared = 0;
1264 (STp->buffer)->writing = 0;
1265 (STp->buffer)->syscall_result = 0;
1267 STp->write_prot = ((filp->f_flags & O_ACCMODE) == O_RDONLY);
1269 STp->dirty = 0;
1270 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
1271 STps = &(STp->ps[i]);
1272 STps->rw = ST_IDLE;
1274 STp->try_dio_now = STp->try_dio;
1275 STp->recover_count = 0;
1276 DEB( STp->nbr_waits = STp->nbr_finished = 0;
1277 STp->nbr_requests = STp->nbr_dio = STp->nbr_pages = 0; )
1279 retval = check_tape(STp, filp);
1280 if (retval < 0)
1281 goto err_out;
1282 if ((filp->f_flags & O_NONBLOCK) == 0 &&
1283 retval != CHKRES_READY) {
1284 if (STp->ready == NO_TAPE)
1285 retval = (-ENOMEDIUM);
1286 else
1287 retval = (-EIO);
1288 goto err_out;
1290 unlock_kernel();
1291 return 0;
1293 err_out:
1294 normalize_buffer(STp->buffer);
1295 STp->in_use = 0;
1296 scsi_tape_put(STp);
1297 unlock_kernel();
1298 return retval;
1303 /* Flush the tape buffer before close */
1304 static int st_flush(struct file *filp, fl_owner_t id)
1306 int result = 0, result2;
1307 unsigned char cmd[MAX_COMMAND_SIZE];
1308 struct st_request *SRpnt;
1309 struct scsi_tape *STp = filp->private_data;
1310 struct st_modedef *STm = &(STp->modes[STp->current_mode]);
1311 struct st_partstat *STps = &(STp->ps[STp->partition]);
1312 char *name = tape_name(STp);
1314 if (file_count(filp) > 1)
1315 return 0;
1317 if (STps->rw == ST_WRITING && !STp->pos_unknown) {
1318 result = st_flush_write_buffer(STp);
1319 if (result != 0 && result != (-ENOSPC))
1320 goto out;
1323 if (STp->can_partitions &&
1324 (result2 = switch_partition(STp)) < 0) {
1325 DEBC(printk(ST_DEB_MSG
1326 "%s: switch_partition at close failed.\n", name));
1327 if (result == 0)
1328 result = result2;
1329 goto out;
1332 DEBC( if (STp->nbr_requests)
1333 printk(KERN_DEBUG "%s: Number of r/w requests %d, dio used in %d, pages %d.\n",
1334 name, STp->nbr_requests, STp->nbr_dio, STp->nbr_pages));
1336 if (STps->rw == ST_WRITING && !STp->pos_unknown) {
1337 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
1339 DEBC(printk(ST_DEB_MSG "%s: Async write waits %d, finished %d.\n",
1340 name, STp->nbr_waits, STp->nbr_finished);
1343 memset(cmd, 0, MAX_COMMAND_SIZE);
1344 cmd[0] = WRITE_FILEMARKS;
1345 cmd[4] = 1 + STp->two_fm;
1347 SRpnt = st_allocate_request(STp);
1348 if (!SRpnt) {
1349 result = STp->buffer->syscall_result;
1350 goto out;
1353 result = st_scsi_kern_execute(SRpnt, cmd, DMA_NONE, NULL, 0,
1354 STp->device->request_queue->rq_timeout,
1355 MAX_WRITE_RETRIES);
1356 if (result) {
1357 st_release_request(SRpnt);
1358 goto out;
1361 if (STp->buffer->syscall_result == 0 ||
1362 (cmdstatp->have_sense && !cmdstatp->deferred &&
1363 (cmdstatp->flags & SENSE_EOM) &&
1364 (cmdstatp->sense_hdr.sense_key == NO_SENSE ||
1365 cmdstatp->sense_hdr.sense_key == RECOVERED_ERROR) &&
1366 (!cmdstatp->remainder_valid || cmdstatp->uremainder64 == 0))) {
1367 /* Write successful at EOM */
1368 st_release_request(SRpnt);
1369 SRpnt = NULL;
1370 if (STps->drv_file >= 0)
1371 STps->drv_file++;
1372 STps->drv_block = 0;
1373 if (STp->two_fm)
1374 cross_eof(STp, 0);
1375 STps->eof = ST_FM;
1377 else { /* Write error */
1378 st_release_request(SRpnt);
1379 SRpnt = NULL;
1380 printk(KERN_ERR "%s: Error on write filemark.\n", name);
1381 if (result == 0)
1382 result = (-EIO);
1385 DEBC(printk(ST_DEB_MSG "%s: Buffer flushed, %d EOF(s) written\n",
1386 name, cmd[4]));
1387 } else if (!STp->rew_at_close) {
1388 STps = &(STp->ps[STp->partition]);
1389 if (!STm->sysv || STps->rw != ST_READING) {
1390 if (STp->can_bsr)
1391 result = flush_buffer(STp, 0);
1392 else if (STps->eof == ST_FM_HIT) {
1393 result = cross_eof(STp, 0);
1394 if (result) {
1395 if (STps->drv_file >= 0)
1396 STps->drv_file++;
1397 STps->drv_block = 0;
1398 STps->eof = ST_FM;
1399 } else
1400 STps->eof = ST_NOEOF;
1402 } else if ((STps->eof == ST_NOEOF &&
1403 !(result = cross_eof(STp, 1))) ||
1404 STps->eof == ST_FM_HIT) {
1405 if (STps->drv_file >= 0)
1406 STps->drv_file++;
1407 STps->drv_block = 0;
1408 STps->eof = ST_FM;
1412 out:
1413 if (STp->rew_at_close) {
1414 result2 = st_int_ioctl(STp, MTREW, 1);
1415 if (result == 0)
1416 result = result2;
1418 return result;
1422 /* Close the device and release it. BKL is not needed: this is the only thread
1423 accessing this tape. */
1424 static int st_release(struct inode *inode, struct file *filp)
1426 int result = 0;
1427 struct scsi_tape *STp = filp->private_data;
1429 if (STp->door_locked == ST_LOCKED_AUTO)
1430 do_door_lock(STp, 0);
1432 normalize_buffer(STp->buffer);
1433 write_lock(&st_dev_arr_lock);
1434 STp->in_use = 0;
1435 write_unlock(&st_dev_arr_lock);
1436 scsi_tape_put(STp);
1438 return result;
1441 /* The checks common to both reading and writing */
1442 static ssize_t rw_checks(struct scsi_tape *STp, struct file *filp, size_t count)
1444 ssize_t retval = 0;
1447 * If we are in the middle of error recovery, don't let anyone
1448 * else try and use this device. Also, if error recovery fails, it
1449 * may try and take the device offline, in which case all further
1450 * access to the device is prohibited.
1452 if (!scsi_block_when_processing_errors(STp->device)) {
1453 retval = (-ENXIO);
1454 goto out;
1457 if (STp->ready != ST_READY) {
1458 if (STp->ready == ST_NO_TAPE)
1459 retval = (-ENOMEDIUM);
1460 else
1461 retval = (-EIO);
1462 goto out;
1465 if (! STp->modes[STp->current_mode].defined) {
1466 retval = (-ENXIO);
1467 goto out;
1472 * If there was a bus reset, block further access
1473 * to this device.
1475 if (STp->pos_unknown) {
1476 retval = (-EIO);
1477 goto out;
1480 if (count == 0)
1481 goto out;
1483 DEB(
1484 if (!STp->in_use) {
1485 printk(ST_DEB_MSG "%s: Incorrect device.\n", tape_name(STp));
1486 retval = (-EIO);
1487 goto out;
1488 } ) /* end DEB */
1490 if (STp->can_partitions &&
1491 (retval = switch_partition(STp)) < 0)
1492 goto out;
1494 if (STp->block_size == 0 && STp->max_block > 0 &&
1495 (count < STp->min_block || count > STp->max_block)) {
1496 retval = (-EINVAL);
1497 goto out;
1500 if (STp->do_auto_lock && STp->door_locked == ST_UNLOCKED &&
1501 !do_door_lock(STp, 1))
1502 STp->door_locked = ST_LOCKED_AUTO;
1504 out:
1505 return retval;
1509 static int setup_buffering(struct scsi_tape *STp, const char __user *buf,
1510 size_t count, int is_read)
1512 int i, bufsize, retval = 0;
1513 struct st_buffer *STbp = STp->buffer;
1515 if (is_read)
1516 i = STp->try_dio_now && try_rdio;
1517 else
1518 i = STp->try_dio_now && try_wdio;
1520 if (i && ((unsigned long)buf & queue_dma_alignment(
1521 STp->device->request_queue)) == 0) {
1522 i = sgl_map_user_pages(STbp, STbp->use_sg, (unsigned long)buf,
1523 count, (is_read ? READ : WRITE));
1524 if (i > 0) {
1525 STbp->do_dio = i;
1526 STbp->buffer_bytes = 0; /* can be used as transfer counter */
1528 else
1529 STbp->do_dio = 0; /* fall back to buffering with any error */
1530 STbp->sg_segs = STbp->do_dio;
1531 STbp->frp_sg_current = 0;
1532 DEB(
1533 if (STbp->do_dio) {
1534 STp->nbr_dio++;
1535 STp->nbr_pages += STbp->do_dio;
1538 } else
1539 STbp->do_dio = 0;
1540 DEB( STp->nbr_requests++; )
1542 if (!STbp->do_dio) {
1543 if (STp->block_size)
1544 bufsize = STp->block_size > st_fixed_buffer_size ?
1545 STp->block_size : st_fixed_buffer_size;
1546 else {
1547 bufsize = count;
1548 /* Make sure that data from previous user is not leaked even if
1549 HBA does not return correct residual */
1550 if (is_read && STp->sili && !STbp->cleared)
1551 clear_buffer(STbp);
1554 if (bufsize > STbp->buffer_size &&
1555 !enlarge_buffer(STbp, bufsize, STp->restr_dma)) {
1556 printk(KERN_WARNING "%s: Can't allocate %d byte tape buffer.\n",
1557 tape_name(STp), bufsize);
1558 retval = (-EOVERFLOW);
1559 goto out;
1561 if (STp->block_size)
1562 STbp->buffer_blocks = bufsize / STp->block_size;
1565 out:
1566 return retval;
1570 /* Can be called more than once after each setup_buffer() */
1571 static void release_buffering(struct scsi_tape *STp, int is_read)
1573 struct st_buffer *STbp;
1575 STbp = STp->buffer;
1576 if (STbp->do_dio) {
1577 sgl_unmap_user_pages(STbp, STbp->do_dio, is_read);
1578 STbp->do_dio = 0;
1579 STbp->sg_segs = 0;
1584 /* Write command */
1585 static ssize_t
1586 st_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
1588 ssize_t total;
1589 ssize_t i, do_count, blks, transfer;
1590 ssize_t retval;
1591 int undone, retry_eot = 0, scode;
1592 int async_write;
1593 unsigned char cmd[MAX_COMMAND_SIZE];
1594 const char __user *b_point;
1595 struct st_request *SRpnt = NULL;
1596 struct scsi_tape *STp = filp->private_data;
1597 struct st_modedef *STm;
1598 struct st_partstat *STps;
1599 struct st_buffer *STbp;
1600 char *name = tape_name(STp);
1602 if (mutex_lock_interruptible(&STp->lock))
1603 return -ERESTARTSYS;
1605 retval = rw_checks(STp, filp, count);
1606 if (retval || count == 0)
1607 goto out;
1609 /* Write must be integral number of blocks */
1610 if (STp->block_size != 0 && (count % STp->block_size) != 0) {
1611 printk(KERN_WARNING "%s: Write not multiple of tape block size.\n",
1612 name);
1613 retval = (-EINVAL);
1614 goto out;
1617 STm = &(STp->modes[STp->current_mode]);
1618 STps = &(STp->ps[STp->partition]);
1620 if (STp->write_prot) {
1621 retval = (-EACCES);
1622 goto out;
1626 if (STps->rw == ST_READING) {
1627 retval = flush_buffer(STp, 0);
1628 if (retval)
1629 goto out;
1630 STps->rw = ST_WRITING;
1631 } else if (STps->rw != ST_WRITING &&
1632 STps->drv_file == 0 && STps->drv_block == 0) {
1633 if ((retval = set_mode_densblk(STp, STm)) < 0)
1634 goto out;
1635 if (STm->default_compression != ST_DONT_TOUCH &&
1636 !(STp->compression_changed)) {
1637 if (st_compression(STp, (STm->default_compression == ST_YES))) {
1638 printk(KERN_WARNING "%s: Can't set default compression.\n",
1639 name);
1640 if (modes_defined) {
1641 retval = (-EINVAL);
1642 goto out;
1648 STbp = STp->buffer;
1649 i = write_behind_check(STp);
1650 if (i) {
1651 if (i == -ENOSPC)
1652 STps->eof = ST_EOM_OK;
1653 else
1654 STps->eof = ST_EOM_ERROR;
1657 if (STps->eof == ST_EOM_OK) {
1658 STps->eof = ST_EOD_1; /* allow next write */
1659 retval = (-ENOSPC);
1660 goto out;
1662 else if (STps->eof == ST_EOM_ERROR) {
1663 retval = (-EIO);
1664 goto out;
1667 /* Check the buffer readability in cases where copy_user might catch
1668 the problems after some tape movement. */
1669 if (STp->block_size != 0 &&
1670 !STbp->do_dio &&
1671 (copy_from_user(&i, buf, 1) != 0 ||
1672 copy_from_user(&i, buf + count - 1, 1) != 0)) {
1673 retval = (-EFAULT);
1674 goto out;
1677 retval = setup_buffering(STp, buf, count, 0);
1678 if (retval)
1679 goto out;
1681 total = count;
1683 memset(cmd, 0, MAX_COMMAND_SIZE);
1684 cmd[0] = WRITE_6;
1685 cmd[1] = (STp->block_size != 0);
1687 STps->rw = ST_WRITING;
1689 b_point = buf;
1690 while (count > 0 && !retry_eot) {
1692 if (STbp->do_dio) {
1693 do_count = count;
1695 else {
1696 if (STp->block_size == 0)
1697 do_count = count;
1698 else {
1699 do_count = STbp->buffer_blocks * STp->block_size -
1700 STbp->buffer_bytes;
1701 if (do_count > count)
1702 do_count = count;
1705 i = append_to_buffer(b_point, STbp, do_count);
1706 if (i) {
1707 retval = i;
1708 goto out;
1711 count -= do_count;
1712 b_point += do_count;
1714 async_write = STp->block_size == 0 && !STbp->do_dio &&
1715 STm->do_async_writes && STps->eof < ST_EOM_OK;
1717 if (STp->block_size != 0 && STm->do_buffer_writes &&
1718 !(STp->try_dio_now && try_wdio) && STps->eof < ST_EOM_OK &&
1719 STbp->buffer_bytes < STbp->buffer_size) {
1720 STp->dirty = 1;
1721 /* Don't write a buffer that is not full enough. */
1722 if (!async_write && count == 0)
1723 break;
1726 retry_write:
1727 if (STp->block_size == 0)
1728 blks = transfer = do_count;
1729 else {
1730 if (!STbp->do_dio)
1731 blks = STbp->buffer_bytes;
1732 else
1733 blks = do_count;
1734 blks /= STp->block_size;
1735 transfer = blks * STp->block_size;
1737 cmd[2] = blks >> 16;
1738 cmd[3] = blks >> 8;
1739 cmd[4] = blks;
1741 SRpnt = st_do_scsi(SRpnt, STp, cmd, transfer, DMA_TO_DEVICE,
1742 STp->device->request_queue->rq_timeout,
1743 MAX_WRITE_RETRIES, !async_write);
1744 if (!SRpnt) {
1745 retval = STbp->syscall_result;
1746 goto out;
1748 if (async_write && !STbp->syscall_result) {
1749 STbp->writing = transfer;
1750 STp->dirty = !(STbp->writing ==
1751 STbp->buffer_bytes);
1752 SRpnt = NULL; /* Prevent releasing this request! */
1753 DEB( STp->write_pending = 1; )
1754 break;
1757 if (STbp->syscall_result != 0) {
1758 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
1760 DEBC(printk(ST_DEB_MSG "%s: Error on write:\n", name));
1761 if (cmdstatp->have_sense && (cmdstatp->flags & SENSE_EOM)) {
1762 scode = cmdstatp->sense_hdr.sense_key;
1763 if (cmdstatp->remainder_valid)
1764 undone = (int)cmdstatp->uremainder64;
1765 else if (STp->block_size == 0 &&
1766 scode == VOLUME_OVERFLOW)
1767 undone = transfer;
1768 else
1769 undone = 0;
1770 if (STp->block_size != 0)
1771 undone *= STp->block_size;
1772 if (undone <= do_count) {
1773 /* Only data from this write is not written */
1774 count += undone;
1775 b_point -= undone;
1776 do_count -= undone;
1777 if (STp->block_size)
1778 blks = (transfer - undone) / STp->block_size;
1779 STps->eof = ST_EOM_OK;
1780 /* Continue in fixed block mode if all written
1781 in this request but still something left to write
1782 (retval left to zero)
1784 if (STp->block_size == 0 ||
1785 undone > 0 || count == 0)
1786 retval = (-ENOSPC); /* EOM within current request */
1787 DEBC(printk(ST_DEB_MSG
1788 "%s: EOM with %d bytes unwritten.\n",
1789 name, (int)count));
1790 } else {
1791 /* EOT within data buffered earlier (possible only
1792 in fixed block mode without direct i/o) */
1793 if (!retry_eot && !cmdstatp->deferred &&
1794 (scode == NO_SENSE || scode == RECOVERED_ERROR)) {
1795 move_buffer_data(STp->buffer, transfer - undone);
1796 retry_eot = 1;
1797 if (STps->drv_block >= 0) {
1798 STps->drv_block += (transfer - undone) /
1799 STp->block_size;
1801 STps->eof = ST_EOM_OK;
1802 DEBC(printk(ST_DEB_MSG
1803 "%s: Retry write of %d bytes at EOM.\n",
1804 name, STp->buffer->buffer_bytes));
1805 goto retry_write;
1807 else {
1808 /* Either error within data buffered by driver or
1809 failed retry */
1810 count -= do_count;
1811 blks = do_count = 0;
1812 STps->eof = ST_EOM_ERROR;
1813 STps->drv_block = (-1); /* Too cautious? */
1814 retval = (-EIO); /* EOM for old data */
1815 DEBC(printk(ST_DEB_MSG
1816 "%s: EOM with lost data.\n",
1817 name));
1820 } else {
1821 count += do_count;
1822 STps->drv_block = (-1); /* Too cautious? */
1823 retval = STbp->syscall_result;
1828 if (STps->drv_block >= 0) {
1829 if (STp->block_size == 0)
1830 STps->drv_block += (do_count > 0);
1831 else
1832 STps->drv_block += blks;
1835 STbp->buffer_bytes = 0;
1836 STp->dirty = 0;
1838 if (retval || retry_eot) {
1839 if (count < total)
1840 retval = total - count;
1841 goto out;
1845 if (STps->eof == ST_EOD_1)
1846 STps->eof = ST_EOM_OK;
1847 else if (STps->eof != ST_EOM_OK)
1848 STps->eof = ST_NOEOF;
1849 retval = total - count;
1851 out:
1852 if (SRpnt != NULL)
1853 st_release_request(SRpnt);
1854 release_buffering(STp, 0);
1855 mutex_unlock(&STp->lock);
1857 return retval;
1860 /* Read data from the tape. Returns zero in the normal case, one if the
1861 eof status has changed, and the negative error code in case of a
1862 fatal error. Otherwise updates the buffer and the eof state.
1864 Does release user buffer mapping if it is set.
1866 static long read_tape(struct scsi_tape *STp, long count,
1867 struct st_request ** aSRpnt)
1869 int transfer, blks, bytes;
1870 unsigned char cmd[MAX_COMMAND_SIZE];
1871 struct st_request *SRpnt;
1872 struct st_modedef *STm;
1873 struct st_partstat *STps;
1874 struct st_buffer *STbp;
1875 int retval = 0;
1876 char *name = tape_name(STp);
1878 if (count == 0)
1879 return 0;
1881 STm = &(STp->modes[STp->current_mode]);
1882 STps = &(STp->ps[STp->partition]);
1883 if (STps->eof == ST_FM_HIT)
1884 return 1;
1885 STbp = STp->buffer;
1887 if (STp->block_size == 0)
1888 blks = bytes = count;
1889 else {
1890 if (!(STp->try_dio_now && try_rdio) && STm->do_read_ahead) {
1891 blks = (STp->buffer)->buffer_blocks;
1892 bytes = blks * STp->block_size;
1893 } else {
1894 bytes = count;
1895 if (!STbp->do_dio && bytes > (STp->buffer)->buffer_size)
1896 bytes = (STp->buffer)->buffer_size;
1897 blks = bytes / STp->block_size;
1898 bytes = blks * STp->block_size;
1902 memset(cmd, 0, MAX_COMMAND_SIZE);
1903 cmd[0] = READ_6;
1904 cmd[1] = (STp->block_size != 0);
1905 if (!cmd[1] && STp->sili)
1906 cmd[1] |= 2;
1907 cmd[2] = blks >> 16;
1908 cmd[3] = blks >> 8;
1909 cmd[4] = blks;
1911 SRpnt = *aSRpnt;
1912 SRpnt = st_do_scsi(SRpnt, STp, cmd, bytes, DMA_FROM_DEVICE,
1913 STp->device->request_queue->rq_timeout,
1914 MAX_RETRIES, 1);
1915 release_buffering(STp, 1);
1916 *aSRpnt = SRpnt;
1917 if (!SRpnt)
1918 return STbp->syscall_result;
1920 STbp->read_pointer = 0;
1921 STps->at_sm = 0;
1923 /* Something to check */
1924 if (STbp->syscall_result) {
1925 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
1927 retval = 1;
1928 DEBC(printk(ST_DEB_MSG "%s: Sense: %2x %2x %2x %2x %2x %2x %2x %2x\n",
1929 name,
1930 SRpnt->sense[0], SRpnt->sense[1],
1931 SRpnt->sense[2], SRpnt->sense[3],
1932 SRpnt->sense[4], SRpnt->sense[5],
1933 SRpnt->sense[6], SRpnt->sense[7]));
1934 if (cmdstatp->have_sense) {
1936 if (cmdstatp->sense_hdr.sense_key == BLANK_CHECK)
1937 cmdstatp->flags &= 0xcf; /* No need for EOM in this case */
1939 if (cmdstatp->flags != 0) { /* EOF, EOM, or ILI */
1940 /* Compute the residual count */
1941 if (cmdstatp->remainder_valid)
1942 transfer = (int)cmdstatp->uremainder64;
1943 else
1944 transfer = 0;
1945 if (STp->block_size == 0 &&
1946 cmdstatp->sense_hdr.sense_key == MEDIUM_ERROR)
1947 transfer = bytes;
1949 if (cmdstatp->flags & SENSE_ILI) { /* ILI */
1950 if (STp->block_size == 0) {
1951 if (transfer <= 0) {
1952 if (transfer < 0)
1953 printk(KERN_NOTICE
1954 "%s: Failed to read %d byte block with %d byte transfer.\n",
1955 name, bytes - transfer, bytes);
1956 if (STps->drv_block >= 0)
1957 STps->drv_block += 1;
1958 STbp->buffer_bytes = 0;
1959 return (-ENOMEM);
1961 STbp->buffer_bytes = bytes - transfer;
1962 } else {
1963 st_release_request(SRpnt);
1964 SRpnt = *aSRpnt = NULL;
1965 if (transfer == blks) { /* We did not get anything, error */
1966 printk(KERN_NOTICE "%s: Incorrect block size.\n", name);
1967 if (STps->drv_block >= 0)
1968 STps->drv_block += blks - transfer + 1;
1969 st_int_ioctl(STp, MTBSR, 1);
1970 return (-EIO);
1972 /* We have some data, deliver it */
1973 STbp->buffer_bytes = (blks - transfer) *
1974 STp->block_size;
1975 DEBC(printk(ST_DEB_MSG
1976 "%s: ILI but enough data received %ld %d.\n",
1977 name, count, STbp->buffer_bytes));
1978 if (STps->drv_block >= 0)
1979 STps->drv_block += 1;
1980 if (st_int_ioctl(STp, MTBSR, 1))
1981 return (-EIO);
1983 } else if (cmdstatp->flags & SENSE_FMK) { /* FM overrides EOM */
1984 if (STps->eof != ST_FM_HIT)
1985 STps->eof = ST_FM_HIT;
1986 else
1987 STps->eof = ST_EOD_2;
1988 if (STp->block_size == 0)
1989 STbp->buffer_bytes = 0;
1990 else
1991 STbp->buffer_bytes =
1992 bytes - transfer * STp->block_size;
1993 DEBC(printk(ST_DEB_MSG
1994 "%s: EOF detected (%d bytes read).\n",
1995 name, STbp->buffer_bytes));
1996 } else if (cmdstatp->flags & SENSE_EOM) {
1997 if (STps->eof == ST_FM)
1998 STps->eof = ST_EOD_1;
1999 else
2000 STps->eof = ST_EOM_OK;
2001 if (STp->block_size == 0)
2002 STbp->buffer_bytes = bytes - transfer;
2003 else
2004 STbp->buffer_bytes =
2005 bytes - transfer * STp->block_size;
2007 DEBC(printk(ST_DEB_MSG "%s: EOM detected (%d bytes read).\n",
2008 name, STbp->buffer_bytes));
2011 /* end of EOF, EOM, ILI test */
2012 else { /* nonzero sense key */
2013 DEBC(printk(ST_DEB_MSG
2014 "%s: Tape error while reading.\n", name));
2015 STps->drv_block = (-1);
2016 if (STps->eof == ST_FM &&
2017 cmdstatp->sense_hdr.sense_key == BLANK_CHECK) {
2018 DEBC(printk(ST_DEB_MSG
2019 "%s: Zero returned for first BLANK CHECK after EOF.\n",
2020 name));
2021 STps->eof = ST_EOD_2; /* First BLANK_CHECK after FM */
2022 } else /* Some other extended sense code */
2023 retval = (-EIO);
2026 if (STbp->buffer_bytes < 0) /* Caused by bogus sense data */
2027 STbp->buffer_bytes = 0;
2029 /* End of extended sense test */
2030 else { /* Non-extended sense */
2031 retval = STbp->syscall_result;
2035 /* End of error handling */
2036 else { /* Read successful */
2037 STbp->buffer_bytes = bytes;
2038 if (STp->sili) /* In fixed block mode residual is always zero here */
2039 STbp->buffer_bytes -= STp->buffer->cmdstat.residual;
2042 if (STps->drv_block >= 0) {
2043 if (STp->block_size == 0)
2044 STps->drv_block++;
2045 else
2046 STps->drv_block += STbp->buffer_bytes / STp->block_size;
2048 return retval;
2052 /* Read command */
2053 static ssize_t
2054 st_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
2056 ssize_t total;
2057 ssize_t retval = 0;
2058 ssize_t i, transfer;
2059 int special, do_dio = 0;
2060 struct st_request *SRpnt = NULL;
2061 struct scsi_tape *STp = filp->private_data;
2062 struct st_modedef *STm;
2063 struct st_partstat *STps;
2064 struct st_buffer *STbp = STp->buffer;
2065 DEB( char *name = tape_name(STp); )
2067 if (mutex_lock_interruptible(&STp->lock))
2068 return -ERESTARTSYS;
2070 retval = rw_checks(STp, filp, count);
2071 if (retval || count == 0)
2072 goto out;
2074 STm = &(STp->modes[STp->current_mode]);
2075 if (STp->block_size != 0 && (count % STp->block_size) != 0) {
2076 if (!STm->do_read_ahead) {
2077 retval = (-EINVAL); /* Read must be integral number of blocks */
2078 goto out;
2080 STp->try_dio_now = 0; /* Direct i/o can't handle split blocks */
2083 STps = &(STp->ps[STp->partition]);
2084 if (STps->rw == ST_WRITING) {
2085 retval = flush_buffer(STp, 0);
2086 if (retval)
2087 goto out;
2088 STps->rw = ST_READING;
2090 DEB(
2091 if (debugging && STps->eof != ST_NOEOF)
2092 printk(ST_DEB_MSG "%s: EOF/EOM flag up (%d). Bytes %d\n", name,
2093 STps->eof, STbp->buffer_bytes);
2094 ) /* end DEB */
2096 retval = setup_buffering(STp, buf, count, 1);
2097 if (retval)
2098 goto out;
2099 do_dio = STbp->do_dio;
2101 if (STbp->buffer_bytes == 0 &&
2102 STps->eof >= ST_EOD_1) {
2103 if (STps->eof < ST_EOD) {
2104 STps->eof += 1;
2105 retval = 0;
2106 goto out;
2108 retval = (-EIO); /* EOM or Blank Check */
2109 goto out;
2112 if (do_dio) {
2113 /* Check the buffer writability before any tape movement. Don't alter
2114 buffer data. */
2115 if (copy_from_user(&i, buf, 1) != 0 ||
2116 copy_to_user(buf, &i, 1) != 0 ||
2117 copy_from_user(&i, buf + count - 1, 1) != 0 ||
2118 copy_to_user(buf + count - 1, &i, 1) != 0) {
2119 retval = (-EFAULT);
2120 goto out;
2124 STps->rw = ST_READING;
2127 /* Loop until enough data in buffer or a special condition found */
2128 for (total = 0, special = 0; total < count && !special;) {
2130 /* Get new data if the buffer is empty */
2131 if (STbp->buffer_bytes == 0) {
2132 special = read_tape(STp, count - total, &SRpnt);
2133 if (special < 0) { /* No need to continue read */
2134 retval = special;
2135 goto out;
2139 /* Move the data from driver buffer to user buffer */
2140 if (STbp->buffer_bytes > 0) {
2141 DEB(
2142 if (debugging && STps->eof != ST_NOEOF)
2143 printk(ST_DEB_MSG
2144 "%s: EOF up (%d). Left %d, needed %d.\n", name,
2145 STps->eof, STbp->buffer_bytes,
2146 (int)(count - total));
2147 ) /* end DEB */
2148 transfer = STbp->buffer_bytes < count - total ?
2149 STbp->buffer_bytes : count - total;
2150 if (!do_dio) {
2151 i = from_buffer(STbp, buf, transfer);
2152 if (i) {
2153 retval = i;
2154 goto out;
2157 buf += transfer;
2158 total += transfer;
2161 if (STp->block_size == 0)
2162 break; /* Read only one variable length block */
2164 } /* for (total = 0, special = 0;
2165 total < count && !special; ) */
2167 /* Change the eof state if no data from tape or buffer */
2168 if (total == 0) {
2169 if (STps->eof == ST_FM_HIT) {
2170 STps->eof = ST_FM;
2171 STps->drv_block = 0;
2172 if (STps->drv_file >= 0)
2173 STps->drv_file++;
2174 } else if (STps->eof == ST_EOD_1) {
2175 STps->eof = ST_EOD_2;
2176 STps->drv_block = 0;
2177 if (STps->drv_file >= 0)
2178 STps->drv_file++;
2179 } else if (STps->eof == ST_EOD_2)
2180 STps->eof = ST_EOD;
2181 } else if (STps->eof == ST_FM)
2182 STps->eof = ST_NOEOF;
2183 retval = total;
2185 out:
2186 if (SRpnt != NULL) {
2187 st_release_request(SRpnt);
2188 SRpnt = NULL;
2190 if (do_dio) {
2191 release_buffering(STp, 1);
2192 STbp->buffer_bytes = 0;
2194 mutex_unlock(&STp->lock);
2196 return retval;
2201 DEB(
2202 /* Set the driver options */
2203 static void st_log_options(struct scsi_tape * STp, struct st_modedef * STm, char *name)
2205 if (debugging) {
2206 printk(KERN_INFO
2207 "%s: Mode %d options: buffer writes: %d, async writes: %d, read ahead: %d\n",
2208 name, STp->current_mode, STm->do_buffer_writes, STm->do_async_writes,
2209 STm->do_read_ahead);
2210 printk(KERN_INFO
2211 "%s: can bsr: %d, two FMs: %d, fast mteom: %d, auto lock: %d,\n",
2212 name, STp->can_bsr, STp->two_fm, STp->fast_mteom, STp->do_auto_lock);
2213 printk(KERN_INFO
2214 "%s: defs for wr: %d, no block limits: %d, partitions: %d, s2 log: %d\n",
2215 name, STm->defaults_for_writes, STp->omit_blklims, STp->can_partitions,
2216 STp->scsi2_logical);
2217 printk(KERN_INFO
2218 "%s: sysv: %d nowait: %d sili: %d\n", name, STm->sysv, STp->immediate,
2219 STp->sili);
2220 printk(KERN_INFO "%s: debugging: %d\n",
2221 name, debugging);
2227 static int st_set_options(struct scsi_tape *STp, long options)
2229 int value;
2230 long code;
2231 struct st_modedef *STm;
2232 char *name = tape_name(STp);
2233 struct cdev *cd0, *cd1;
2235 STm = &(STp->modes[STp->current_mode]);
2236 if (!STm->defined) {
2237 cd0 = STm->cdevs[0]; cd1 = STm->cdevs[1];
2238 memcpy(STm, &(STp->modes[0]), sizeof(struct st_modedef));
2239 STm->cdevs[0] = cd0; STm->cdevs[1] = cd1;
2240 modes_defined = 1;
2241 DEBC(printk(ST_DEB_MSG
2242 "%s: Initialized mode %d definition from mode 0\n",
2243 name, STp->current_mode));
2246 code = options & MT_ST_OPTIONS;
2247 if (code == MT_ST_BOOLEANS) {
2248 STm->do_buffer_writes = (options & MT_ST_BUFFER_WRITES) != 0;
2249 STm->do_async_writes = (options & MT_ST_ASYNC_WRITES) != 0;
2250 STm->defaults_for_writes = (options & MT_ST_DEF_WRITES) != 0;
2251 STm->do_read_ahead = (options & MT_ST_READ_AHEAD) != 0;
2252 STp->two_fm = (options & MT_ST_TWO_FM) != 0;
2253 STp->fast_mteom = (options & MT_ST_FAST_MTEOM) != 0;
2254 STp->do_auto_lock = (options & MT_ST_AUTO_LOCK) != 0;
2255 STp->can_bsr = (options & MT_ST_CAN_BSR) != 0;
2256 STp->omit_blklims = (options & MT_ST_NO_BLKLIMS) != 0;
2257 if ((STp->device)->scsi_level >= SCSI_2)
2258 STp->can_partitions = (options & MT_ST_CAN_PARTITIONS) != 0;
2259 STp->scsi2_logical = (options & MT_ST_SCSI2LOGICAL) != 0;
2260 STp->immediate = (options & MT_ST_NOWAIT) != 0;
2261 STm->sysv = (options & MT_ST_SYSV) != 0;
2262 STp->sili = (options & MT_ST_SILI) != 0;
2263 DEB( debugging = (options & MT_ST_DEBUGGING) != 0;
2264 st_log_options(STp, STm, name); )
2265 } else if (code == MT_ST_SETBOOLEANS || code == MT_ST_CLEARBOOLEANS) {
2266 value = (code == MT_ST_SETBOOLEANS);
2267 if ((options & MT_ST_BUFFER_WRITES) != 0)
2268 STm->do_buffer_writes = value;
2269 if ((options & MT_ST_ASYNC_WRITES) != 0)
2270 STm->do_async_writes = value;
2271 if ((options & MT_ST_DEF_WRITES) != 0)
2272 STm->defaults_for_writes = value;
2273 if ((options & MT_ST_READ_AHEAD) != 0)
2274 STm->do_read_ahead = value;
2275 if ((options & MT_ST_TWO_FM) != 0)
2276 STp->two_fm = value;
2277 if ((options & MT_ST_FAST_MTEOM) != 0)
2278 STp->fast_mteom = value;
2279 if ((options & MT_ST_AUTO_LOCK) != 0)
2280 STp->do_auto_lock = value;
2281 if ((options & MT_ST_CAN_BSR) != 0)
2282 STp->can_bsr = value;
2283 if ((options & MT_ST_NO_BLKLIMS) != 0)
2284 STp->omit_blklims = value;
2285 if ((STp->device)->scsi_level >= SCSI_2 &&
2286 (options & MT_ST_CAN_PARTITIONS) != 0)
2287 STp->can_partitions = value;
2288 if ((options & MT_ST_SCSI2LOGICAL) != 0)
2289 STp->scsi2_logical = value;
2290 if ((options & MT_ST_NOWAIT) != 0)
2291 STp->immediate = value;
2292 if ((options & MT_ST_SYSV) != 0)
2293 STm->sysv = value;
2294 if ((options & MT_ST_SILI) != 0)
2295 STp->sili = value;
2296 DEB(
2297 if ((options & MT_ST_DEBUGGING) != 0)
2298 debugging = value;
2299 st_log_options(STp, STm, name); )
2300 } else if (code == MT_ST_WRITE_THRESHOLD) {
2301 /* Retained for compatibility */
2302 } else if (code == MT_ST_DEF_BLKSIZE) {
2303 value = (options & ~MT_ST_OPTIONS);
2304 if (value == ~MT_ST_OPTIONS) {
2305 STm->default_blksize = (-1);
2306 DEBC( printk(KERN_INFO "%s: Default block size disabled.\n", name));
2307 } else {
2308 STm->default_blksize = value;
2309 DEBC( printk(KERN_INFO "%s: Default block size set to %d bytes.\n",
2310 name, STm->default_blksize));
2311 if (STp->ready == ST_READY) {
2312 STp->blksize_changed = 0;
2313 set_mode_densblk(STp, STm);
2316 } else if (code == MT_ST_TIMEOUTS) {
2317 value = (options & ~MT_ST_OPTIONS);
2318 if ((value & MT_ST_SET_LONG_TIMEOUT) != 0) {
2319 STp->long_timeout = (value & ~MT_ST_SET_LONG_TIMEOUT) * HZ;
2320 DEBC( printk(KERN_INFO "%s: Long timeout set to %d seconds.\n", name,
2321 (value & ~MT_ST_SET_LONG_TIMEOUT)));
2322 } else {
2323 blk_queue_rq_timeout(STp->device->request_queue,
2324 value * HZ);
2325 DEBC( printk(KERN_INFO "%s: Normal timeout set to %d seconds.\n",
2326 name, value) );
2328 } else if (code == MT_ST_SET_CLN) {
2329 value = (options & ~MT_ST_OPTIONS) & 0xff;
2330 if (value != 0 &&
2331 value < EXTENDED_SENSE_START && value >= SCSI_SENSE_BUFFERSIZE)
2332 return (-EINVAL);
2333 STp->cln_mode = value;
2334 STp->cln_sense_mask = (options >> 8) & 0xff;
2335 STp->cln_sense_value = (options >> 16) & 0xff;
2336 printk(KERN_INFO
2337 "%s: Cleaning request mode %d, mask %02x, value %02x\n",
2338 name, value, STp->cln_sense_mask, STp->cln_sense_value);
2339 } else if (code == MT_ST_DEF_OPTIONS) {
2340 code = (options & ~MT_ST_CLEAR_DEFAULT);
2341 value = (options & MT_ST_CLEAR_DEFAULT);
2342 if (code == MT_ST_DEF_DENSITY) {
2343 if (value == MT_ST_CLEAR_DEFAULT) {
2344 STm->default_density = (-1);
2345 DEBC( printk(KERN_INFO "%s: Density default disabled.\n",
2346 name));
2347 } else {
2348 STm->default_density = value & 0xff;
2349 DEBC( printk(KERN_INFO "%s: Density default set to %x\n",
2350 name, STm->default_density));
2351 if (STp->ready == ST_READY) {
2352 STp->density_changed = 0;
2353 set_mode_densblk(STp, STm);
2356 } else if (code == MT_ST_DEF_DRVBUFFER) {
2357 if (value == MT_ST_CLEAR_DEFAULT) {
2358 STp->default_drvbuffer = 0xff;
2359 DEBC( printk(KERN_INFO
2360 "%s: Drive buffer default disabled.\n", name));
2361 } else {
2362 STp->default_drvbuffer = value & 7;
2363 DEBC( printk(KERN_INFO
2364 "%s: Drive buffer default set to %x\n",
2365 name, STp->default_drvbuffer));
2366 if (STp->ready == ST_READY)
2367 st_int_ioctl(STp, MTSETDRVBUFFER, STp->default_drvbuffer);
2369 } else if (code == MT_ST_DEF_COMPRESSION) {
2370 if (value == MT_ST_CLEAR_DEFAULT) {
2371 STm->default_compression = ST_DONT_TOUCH;
2372 DEBC( printk(KERN_INFO
2373 "%s: Compression default disabled.\n", name));
2374 } else {
2375 if ((value & 0xff00) != 0) {
2376 STp->c_algo = (value & 0xff00) >> 8;
2377 DEBC( printk(KERN_INFO "%s: Compression algorithm set to 0x%x.\n",
2378 name, STp->c_algo));
2380 if ((value & 0xff) != 0xff) {
2381 STm->default_compression = (value & 1 ? ST_YES : ST_NO);
2382 DEBC( printk(KERN_INFO "%s: Compression default set to %x\n",
2383 name, (value & 1)));
2384 if (STp->ready == ST_READY) {
2385 STp->compression_changed = 0;
2386 st_compression(STp, (STm->default_compression == ST_YES));
2391 } else
2392 return (-EIO);
2394 return 0;
2397 #define MODE_HEADER_LENGTH 4
2399 /* Mode header and page byte offsets */
2400 #define MH_OFF_DATA_LENGTH 0
2401 #define MH_OFF_MEDIUM_TYPE 1
2402 #define MH_OFF_DEV_SPECIFIC 2
2403 #define MH_OFF_BDESCS_LENGTH 3
2404 #define MP_OFF_PAGE_NBR 0
2405 #define MP_OFF_PAGE_LENGTH 1
2407 /* Mode header and page bit masks */
2408 #define MH_BIT_WP 0x80
2409 #define MP_MSK_PAGE_NBR 0x3f
2411 /* Don't return block descriptors */
2412 #define MODE_SENSE_OMIT_BDESCS 0x08
2414 #define MODE_SELECT_PAGE_FORMAT 0x10
2416 /* Read a mode page into the tape buffer. The block descriptors are included
2417 if incl_block_descs is true. The page control is ored to the page number
2418 parameter, if necessary. */
2419 static int read_mode_page(struct scsi_tape *STp, int page, int omit_block_descs)
2421 unsigned char cmd[MAX_COMMAND_SIZE];
2422 struct st_request *SRpnt;
2423 int ret;
2425 memset(cmd, 0, MAX_COMMAND_SIZE);
2426 cmd[0] = MODE_SENSE;
2427 if (omit_block_descs)
2428 cmd[1] = MODE_SENSE_OMIT_BDESCS;
2429 cmd[2] = page;
2430 cmd[4] = 255;
2432 SRpnt = st_allocate_request(STp);
2433 if (!SRpnt)
2434 return STp->buffer->syscall_result;
2436 ret = st_scsi_kern_execute(SRpnt, cmd, DMA_FROM_DEVICE,
2437 STp->buffer->b_data, cmd[4],
2438 STp->device->request_queue->rq_timeout,
2439 MAX_RETRIES);
2440 st_release_request(SRpnt);
2442 return ret ? : STp->buffer->syscall_result;
2446 /* Send the mode page in the tape buffer to the drive. Assumes that the mode data
2447 in the buffer is correctly formatted. The long timeout is used if slow is non-zero. */
2448 static int write_mode_page(struct scsi_tape *STp, int page, int slow)
2450 int pgo, timeout, ret = 0;
2451 unsigned char cmd[MAX_COMMAND_SIZE];
2452 struct st_request *SRpnt;
2454 memset(cmd, 0, MAX_COMMAND_SIZE);
2455 cmd[0] = MODE_SELECT;
2456 cmd[1] = MODE_SELECT_PAGE_FORMAT;
2457 pgo = MODE_HEADER_LENGTH + (STp->buffer)->b_data[MH_OFF_BDESCS_LENGTH];
2458 cmd[4] = pgo + (STp->buffer)->b_data[pgo + MP_OFF_PAGE_LENGTH] + 2;
2460 /* Clear reserved fields */
2461 (STp->buffer)->b_data[MH_OFF_DATA_LENGTH] = 0;
2462 (STp->buffer)->b_data[MH_OFF_MEDIUM_TYPE] = 0;
2463 (STp->buffer)->b_data[MH_OFF_DEV_SPECIFIC] &= ~MH_BIT_WP;
2464 (STp->buffer)->b_data[pgo + MP_OFF_PAGE_NBR] &= MP_MSK_PAGE_NBR;
2466 SRpnt = st_allocate_request(STp);
2467 if (!SRpnt)
2468 return ret;
2470 timeout = slow ? STp->long_timeout :
2471 STp->device->request_queue->rq_timeout;
2473 ret = st_scsi_kern_execute(SRpnt, cmd, DMA_TO_DEVICE,
2474 STp->buffer->b_data, cmd[4], timeout, 0);
2475 if (!ret)
2476 ret = STp->buffer->syscall_result;
2478 st_release_request(SRpnt);
2480 return ret;
2484 #define COMPRESSION_PAGE 0x0f
2485 #define COMPRESSION_PAGE_LENGTH 16
2487 #define CP_OFF_DCE_DCC 2
2488 #define CP_OFF_C_ALGO 7
2490 #define DCE_MASK 0x80
2491 #define DCC_MASK 0x40
2492 #define RED_MASK 0x60
2495 /* Control the compression with mode page 15. Algorithm not changed if zero.
2497 The block descriptors are read and written because Sony SDT-7000 does not
2498 work without this (suggestion from Michael Schaefer <Michael.Schaefer@dlr.de>).
2499 Including block descriptors should not cause any harm to other drives. */
2501 static int st_compression(struct scsi_tape * STp, int state)
2503 int retval;
2504 int mpoffs; /* Offset to mode page start */
2505 unsigned char *b_data = (STp->buffer)->b_data;
2506 DEB( char *name = tape_name(STp); )
2508 if (STp->ready != ST_READY)
2509 return (-EIO);
2511 /* Read the current page contents */
2512 retval = read_mode_page(STp, COMPRESSION_PAGE, 0);
2513 if (retval) {
2514 DEBC(printk(ST_DEB_MSG "%s: Compression mode page not supported.\n",
2515 name));
2516 return (-EIO);
2519 mpoffs = MODE_HEADER_LENGTH + b_data[MH_OFF_BDESCS_LENGTH];
2520 DEBC(printk(ST_DEB_MSG "%s: Compression state is %d.\n", name,
2521 (b_data[mpoffs + CP_OFF_DCE_DCC] & DCE_MASK ? 1 : 0)));
2523 /* Check if compression can be changed */
2524 if ((b_data[mpoffs + CP_OFF_DCE_DCC] & DCC_MASK) == 0) {
2525 DEBC(printk(ST_DEB_MSG "%s: Compression not supported.\n", name));
2526 return (-EIO);
2529 /* Do the change */
2530 if (state) {
2531 b_data[mpoffs + CP_OFF_DCE_DCC] |= DCE_MASK;
2532 if (STp->c_algo != 0)
2533 b_data[mpoffs + CP_OFF_C_ALGO] = STp->c_algo;
2535 else {
2536 b_data[mpoffs + CP_OFF_DCE_DCC] &= ~DCE_MASK;
2537 if (STp->c_algo != 0)
2538 b_data[mpoffs + CP_OFF_C_ALGO] = 0; /* no compression */
2541 retval = write_mode_page(STp, COMPRESSION_PAGE, 0);
2542 if (retval) {
2543 DEBC(printk(ST_DEB_MSG "%s: Compression change failed.\n", name));
2544 return (-EIO);
2546 DEBC(printk(ST_DEB_MSG "%s: Compression state changed to %d.\n",
2547 name, state));
2549 STp->compression_changed = 1;
2550 return 0;
2554 /* Process the load and unload commands (does unload if the load code is zero) */
2555 static int do_load_unload(struct scsi_tape *STp, struct file *filp, int load_code)
2557 int retval = (-EIO), timeout;
2558 DEB( char *name = tape_name(STp); )
2559 unsigned char cmd[MAX_COMMAND_SIZE];
2560 struct st_partstat *STps;
2561 struct st_request *SRpnt;
2563 if (STp->ready != ST_READY && !load_code) {
2564 if (STp->ready == ST_NO_TAPE)
2565 return (-ENOMEDIUM);
2566 else
2567 return (-EIO);
2570 memset(cmd, 0, MAX_COMMAND_SIZE);
2571 cmd[0] = START_STOP;
2572 if (load_code)
2573 cmd[4] |= 1;
2575 * If arg >= 1 && arg <= 6 Enhanced load/unload in HP C1553A
2577 if (load_code >= 1 + MT_ST_HPLOADER_OFFSET
2578 && load_code <= 6 + MT_ST_HPLOADER_OFFSET) {
2579 DEBC(printk(ST_DEB_MSG "%s: Enhanced %sload slot %2d.\n",
2580 name, (cmd[4]) ? "" : "un",
2581 load_code - MT_ST_HPLOADER_OFFSET));
2582 cmd[3] = load_code - MT_ST_HPLOADER_OFFSET; /* MediaID field of C1553A */
2584 if (STp->immediate) {
2585 cmd[1] = 1; /* Don't wait for completion */
2586 timeout = STp->device->request_queue->rq_timeout;
2588 else
2589 timeout = STp->long_timeout;
2591 DEBC(
2592 if (!load_code)
2593 printk(ST_DEB_MSG "%s: Unloading tape.\n", name);
2594 else
2595 printk(ST_DEB_MSG "%s: Loading tape.\n", name);
2598 SRpnt = st_allocate_request(STp);
2599 if (!SRpnt)
2600 return STp->buffer->syscall_result;
2602 retval = st_scsi_kern_execute(SRpnt, cmd, DMA_NONE, NULL, 0, timeout,
2603 MAX_RETRIES);
2604 if (retval)
2605 goto out;
2607 retval = (STp->buffer)->syscall_result;
2609 if (!retval) { /* SCSI command successful */
2611 if (!load_code) {
2612 STp->rew_at_close = 0;
2613 STp->ready = ST_NO_TAPE;
2615 else {
2616 STp->rew_at_close = STp->autorew_dev;
2617 retval = check_tape(STp, filp);
2618 if (retval > 0)
2619 retval = 0;
2622 else {
2623 STps = &(STp->ps[STp->partition]);
2624 STps->drv_file = STps->drv_block = (-1);
2626 out:
2627 st_release_request(SRpnt);
2629 return retval;
2632 #if DEBUG
2633 #define ST_DEB_FORWARD 0
2634 #define ST_DEB_BACKWARD 1
2635 static void deb_space_print(char *name, int direction, char *units, unsigned char *cmd)
2637 s32 sc;
2639 sc = cmd[2] & 0x80 ? 0xff000000 : 0;
2640 sc |= (cmd[2] << 16) | (cmd[3] << 8) | cmd[4];
2641 if (direction)
2642 sc = -sc;
2643 printk(ST_DEB_MSG "%s: Spacing tape %s over %d %s.\n", name,
2644 direction ? "backward" : "forward", sc, units);
2646 #endif
2649 /* Internal ioctl function */
2650 static int st_int_ioctl(struct scsi_tape *STp, unsigned int cmd_in, unsigned long arg)
2652 int timeout;
2653 long ltmp;
2654 int ioctl_result;
2655 int chg_eof = 1;
2656 unsigned char cmd[MAX_COMMAND_SIZE];
2657 struct st_request *SRpnt;
2658 struct st_partstat *STps;
2659 int fileno, blkno, at_sm, undone;
2660 int datalen = 0, direction = DMA_NONE;
2661 char *name = tape_name(STp);
2663 WARN_ON(STp->buffer->do_dio != 0);
2664 if (STp->ready != ST_READY) {
2665 if (STp->ready == ST_NO_TAPE)
2666 return (-ENOMEDIUM);
2667 else
2668 return (-EIO);
2670 timeout = STp->long_timeout;
2671 STps = &(STp->ps[STp->partition]);
2672 fileno = STps->drv_file;
2673 blkno = STps->drv_block;
2674 at_sm = STps->at_sm;
2676 memset(cmd, 0, MAX_COMMAND_SIZE);
2677 switch (cmd_in) {
2678 case MTFSFM:
2679 chg_eof = 0; /* Changed from the FSF after this */
2680 case MTFSF:
2681 cmd[0] = SPACE;
2682 cmd[1] = 0x01; /* Space FileMarks */
2683 cmd[2] = (arg >> 16);
2684 cmd[3] = (arg >> 8);
2685 cmd[4] = arg;
2686 DEBC(deb_space_print(name, ST_DEB_FORWARD, "filemarks", cmd);)
2687 if (fileno >= 0)
2688 fileno += arg;
2689 blkno = 0;
2690 at_sm &= (arg == 0);
2691 break;
2692 case MTBSFM:
2693 chg_eof = 0; /* Changed from the FSF after this */
2694 case MTBSF:
2695 cmd[0] = SPACE;
2696 cmd[1] = 0x01; /* Space FileMarks */
2697 ltmp = (-arg);
2698 cmd[2] = (ltmp >> 16);
2699 cmd[3] = (ltmp >> 8);
2700 cmd[4] = ltmp;
2701 DEBC(deb_space_print(name, ST_DEB_BACKWARD, "filemarks", cmd);)
2702 if (fileno >= 0)
2703 fileno -= arg;
2704 blkno = (-1); /* We can't know the block number */
2705 at_sm &= (arg == 0);
2706 break;
2707 case MTFSR:
2708 cmd[0] = SPACE;
2709 cmd[1] = 0x00; /* Space Blocks */
2710 cmd[2] = (arg >> 16);
2711 cmd[3] = (arg >> 8);
2712 cmd[4] = arg;
2713 DEBC(deb_space_print(name, ST_DEB_FORWARD, "blocks", cmd);)
2714 if (blkno >= 0)
2715 blkno += arg;
2716 at_sm &= (arg == 0);
2717 break;
2718 case MTBSR:
2719 cmd[0] = SPACE;
2720 cmd[1] = 0x00; /* Space Blocks */
2721 ltmp = (-arg);
2722 cmd[2] = (ltmp >> 16);
2723 cmd[3] = (ltmp >> 8);
2724 cmd[4] = ltmp;
2725 DEBC(deb_space_print(name, ST_DEB_BACKWARD, "blocks", cmd);)
2726 if (blkno >= 0)
2727 blkno -= arg;
2728 at_sm &= (arg == 0);
2729 break;
2730 case MTFSS:
2731 cmd[0] = SPACE;
2732 cmd[1] = 0x04; /* Space Setmarks */
2733 cmd[2] = (arg >> 16);
2734 cmd[3] = (arg >> 8);
2735 cmd[4] = arg;
2736 DEBC(deb_space_print(name, ST_DEB_FORWARD, "setmarks", cmd);)
2737 if (arg != 0) {
2738 blkno = fileno = (-1);
2739 at_sm = 1;
2741 break;
2742 case MTBSS:
2743 cmd[0] = SPACE;
2744 cmd[1] = 0x04; /* Space Setmarks */
2745 ltmp = (-arg);
2746 cmd[2] = (ltmp >> 16);
2747 cmd[3] = (ltmp >> 8);
2748 cmd[4] = ltmp;
2749 DEBC(deb_space_print(name, ST_DEB_BACKWARD, "setmarks", cmd);)
2750 if (arg != 0) {
2751 blkno = fileno = (-1);
2752 at_sm = 1;
2754 break;
2755 case MTWEOF:
2756 case MTWSM:
2757 if (STp->write_prot)
2758 return (-EACCES);
2759 cmd[0] = WRITE_FILEMARKS;
2760 if (cmd_in == MTWSM)
2761 cmd[1] = 2;
2762 cmd[2] = (arg >> 16);
2763 cmd[3] = (arg >> 8);
2764 cmd[4] = arg;
2765 timeout = STp->device->request_queue->rq_timeout;
2766 DEBC(
2767 if (cmd_in == MTWEOF)
2768 printk(ST_DEB_MSG "%s: Writing %d filemarks.\n", name,
2769 cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
2770 else
2771 printk(ST_DEB_MSG "%s: Writing %d setmarks.\n", name,
2772 cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
2774 if (fileno >= 0)
2775 fileno += arg;
2776 blkno = 0;
2777 at_sm = (cmd_in == MTWSM);
2778 break;
2779 case MTREW:
2780 cmd[0] = REZERO_UNIT;
2781 if (STp->immediate) {
2782 cmd[1] = 1; /* Don't wait for completion */
2783 timeout = STp->device->request_queue->rq_timeout;
2785 DEBC(printk(ST_DEB_MSG "%s: Rewinding tape.\n", name));
2786 fileno = blkno = at_sm = 0;
2787 break;
2788 case MTNOP:
2789 DEBC(printk(ST_DEB_MSG "%s: No op on tape.\n", name));
2790 return 0; /* Should do something ? */
2791 break;
2792 case MTRETEN:
2793 cmd[0] = START_STOP;
2794 if (STp->immediate) {
2795 cmd[1] = 1; /* Don't wait for completion */
2796 timeout = STp->device->request_queue->rq_timeout;
2798 cmd[4] = 3;
2799 DEBC(printk(ST_DEB_MSG "%s: Retensioning tape.\n", name));
2800 fileno = blkno = at_sm = 0;
2801 break;
2802 case MTEOM:
2803 if (!STp->fast_mteom) {
2804 /* space to the end of tape */
2805 ioctl_result = st_int_ioctl(STp, MTFSF, 0x7fffff);
2806 fileno = STps->drv_file;
2807 if (STps->eof >= ST_EOD_1)
2808 return 0;
2809 /* The next lines would hide the number of spaced FileMarks
2810 That's why I inserted the previous lines. I had no luck
2811 with detecting EOM with FSF, so we go now to EOM.
2812 Joerg Weule */
2813 } else
2814 fileno = (-1);
2815 cmd[0] = SPACE;
2816 cmd[1] = 3;
2817 DEBC(printk(ST_DEB_MSG "%s: Spacing to end of recorded medium.\n",
2818 name));
2819 blkno = -1;
2820 at_sm = 0;
2821 break;
2822 case MTERASE:
2823 if (STp->write_prot)
2824 return (-EACCES);
2825 cmd[0] = ERASE;
2826 cmd[1] = (arg ? 1 : 0); /* Long erase with non-zero argument */
2827 if (STp->immediate) {
2828 cmd[1] |= 2; /* Don't wait for completion */
2829 timeout = STp->device->request_queue->rq_timeout;
2831 else
2832 timeout = STp->long_timeout * 8;
2834 DEBC(printk(ST_DEB_MSG "%s: Erasing tape.\n", name));
2835 fileno = blkno = at_sm = 0;
2836 break;
2837 case MTSETBLK: /* Set block length */
2838 case MTSETDENSITY: /* Set tape density */
2839 case MTSETDRVBUFFER: /* Set drive buffering */
2840 case SET_DENS_AND_BLK: /* Set density and block size */
2841 chg_eof = 0;
2842 if (STp->dirty || (STp->buffer)->buffer_bytes != 0)
2843 return (-EIO); /* Not allowed if data in buffer */
2844 if ((cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) &&
2845 (arg & MT_ST_BLKSIZE_MASK) != 0 &&
2846 STp->max_block > 0 &&
2847 ((arg & MT_ST_BLKSIZE_MASK) < STp->min_block ||
2848 (arg & MT_ST_BLKSIZE_MASK) > STp->max_block)) {
2849 printk(KERN_WARNING "%s: Illegal block size.\n", name);
2850 return (-EINVAL);
2852 cmd[0] = MODE_SELECT;
2853 if ((STp->use_pf & USE_PF))
2854 cmd[1] = MODE_SELECT_PAGE_FORMAT;
2855 cmd[4] = datalen = 12;
2856 direction = DMA_TO_DEVICE;
2858 memset((STp->buffer)->b_data, 0, 12);
2859 if (cmd_in == MTSETDRVBUFFER)
2860 (STp->buffer)->b_data[2] = (arg & 7) << 4;
2861 else
2862 (STp->buffer)->b_data[2] =
2863 STp->drv_buffer << 4;
2864 (STp->buffer)->b_data[3] = 8; /* block descriptor length */
2865 if (cmd_in == MTSETDENSITY) {
2866 (STp->buffer)->b_data[4] = arg;
2867 STp->density_changed = 1; /* At least we tried ;-) */
2868 } else if (cmd_in == SET_DENS_AND_BLK)
2869 (STp->buffer)->b_data[4] = arg >> 24;
2870 else
2871 (STp->buffer)->b_data[4] = STp->density;
2872 if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) {
2873 ltmp = arg & MT_ST_BLKSIZE_MASK;
2874 if (cmd_in == MTSETBLK)
2875 STp->blksize_changed = 1; /* At least we tried ;-) */
2876 } else
2877 ltmp = STp->block_size;
2878 (STp->buffer)->b_data[9] = (ltmp >> 16);
2879 (STp->buffer)->b_data[10] = (ltmp >> 8);
2880 (STp->buffer)->b_data[11] = ltmp;
2881 timeout = STp->device->request_queue->rq_timeout;
2882 DEBC(
2883 if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK)
2884 printk(ST_DEB_MSG
2885 "%s: Setting block size to %d bytes.\n", name,
2886 (STp->buffer)->b_data[9] * 65536 +
2887 (STp->buffer)->b_data[10] * 256 +
2888 (STp->buffer)->b_data[11]);
2889 if (cmd_in == MTSETDENSITY || cmd_in == SET_DENS_AND_BLK)
2890 printk(ST_DEB_MSG
2891 "%s: Setting density code to %x.\n", name,
2892 (STp->buffer)->b_data[4]);
2893 if (cmd_in == MTSETDRVBUFFER)
2894 printk(ST_DEB_MSG
2895 "%s: Setting drive buffer code to %d.\n", name,
2896 ((STp->buffer)->b_data[2] >> 4) & 7);
2898 break;
2899 default:
2900 return (-ENOSYS);
2903 SRpnt = st_allocate_request(STp);
2904 if (!SRpnt)
2905 return (STp->buffer)->syscall_result;
2907 ioctl_result = st_scsi_kern_execute(SRpnt, cmd, direction,
2908 STp->buffer->b_data, datalen,
2909 timeout, MAX_RETRIES);
2910 if (!ioctl_result)
2911 ioctl_result = (STp->buffer)->syscall_result;
2913 if (!ioctl_result) { /* SCSI command successful */
2914 st_release_request(SRpnt);
2915 SRpnt = NULL;
2916 STps->drv_block = blkno;
2917 STps->drv_file = fileno;
2918 STps->at_sm = at_sm;
2920 if (cmd_in == MTBSFM)
2921 ioctl_result = st_int_ioctl(STp, MTFSF, 1);
2922 else if (cmd_in == MTFSFM)
2923 ioctl_result = st_int_ioctl(STp, MTBSF, 1);
2925 if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) {
2926 int old_block_size = STp->block_size;
2927 STp->block_size = arg & MT_ST_BLKSIZE_MASK;
2928 if (STp->block_size != 0) {
2929 if (old_block_size == 0)
2930 normalize_buffer(STp->buffer);
2931 (STp->buffer)->buffer_blocks =
2932 (STp->buffer)->buffer_size / STp->block_size;
2934 (STp->buffer)->buffer_bytes = (STp->buffer)->read_pointer = 0;
2935 if (cmd_in == SET_DENS_AND_BLK)
2936 STp->density = arg >> MT_ST_DENSITY_SHIFT;
2937 } else if (cmd_in == MTSETDRVBUFFER)
2938 STp->drv_buffer = (arg & 7);
2939 else if (cmd_in == MTSETDENSITY)
2940 STp->density = arg;
2942 if (cmd_in == MTEOM)
2943 STps->eof = ST_EOD;
2944 else if (cmd_in == MTFSF)
2945 STps->eof = ST_FM;
2946 else if (chg_eof)
2947 STps->eof = ST_NOEOF;
2949 if (cmd_in == MTWEOF)
2950 STps->rw = ST_IDLE;
2951 } else { /* SCSI command was not completely successful. Don't return
2952 from this block without releasing the SCSI command block! */
2953 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
2955 if (cmdstatp->flags & SENSE_EOM) {
2956 if (cmd_in != MTBSF && cmd_in != MTBSFM &&
2957 cmd_in != MTBSR && cmd_in != MTBSS)
2958 STps->eof = ST_EOM_OK;
2959 STps->drv_block = 0;
2962 if (cmdstatp->remainder_valid)
2963 undone = (int)cmdstatp->uremainder64;
2964 else
2965 undone = 0;
2967 if (cmd_in == MTWEOF &&
2968 cmdstatp->have_sense &&
2969 (cmdstatp->flags & SENSE_EOM)) {
2970 if (cmdstatp->sense_hdr.sense_key == NO_SENSE ||
2971 cmdstatp->sense_hdr.sense_key == RECOVERED_ERROR) {
2972 ioctl_result = 0; /* EOF(s) written successfully at EOM */
2973 STps->eof = ST_NOEOF;
2974 } else { /* Writing EOF(s) failed */
2975 if (fileno >= 0)
2976 fileno -= undone;
2977 if (undone < arg)
2978 STps->eof = ST_NOEOF;
2980 STps->drv_file = fileno;
2981 } else if ((cmd_in == MTFSF) || (cmd_in == MTFSFM)) {
2982 if (fileno >= 0)
2983 STps->drv_file = fileno - undone;
2984 else
2985 STps->drv_file = fileno;
2986 STps->drv_block = -1;
2987 STps->eof = ST_NOEOF;
2988 } else if ((cmd_in == MTBSF) || (cmd_in == MTBSFM)) {
2989 if (arg > 0 && undone < 0) /* Some drives get this wrong */
2990 undone = (-undone);
2991 if (STps->drv_file >= 0)
2992 STps->drv_file = fileno + undone;
2993 STps->drv_block = 0;
2994 STps->eof = ST_NOEOF;
2995 } else if (cmd_in == MTFSR) {
2996 if (cmdstatp->flags & SENSE_FMK) { /* Hit filemark */
2997 if (STps->drv_file >= 0)
2998 STps->drv_file++;
2999 STps->drv_block = 0;
3000 STps->eof = ST_FM;
3001 } else {
3002 if (blkno >= undone)
3003 STps->drv_block = blkno - undone;
3004 else
3005 STps->drv_block = (-1);
3006 STps->eof = ST_NOEOF;
3008 } else if (cmd_in == MTBSR) {
3009 if (cmdstatp->flags & SENSE_FMK) { /* Hit filemark */
3010 STps->drv_file--;
3011 STps->drv_block = (-1);
3012 } else {
3013 if (arg > 0 && undone < 0) /* Some drives get this wrong */
3014 undone = (-undone);
3015 if (STps->drv_block >= 0)
3016 STps->drv_block = blkno + undone;
3018 STps->eof = ST_NOEOF;
3019 } else if (cmd_in == MTEOM) {
3020 STps->drv_file = (-1);
3021 STps->drv_block = (-1);
3022 STps->eof = ST_EOD;
3023 } else if (cmd_in == MTSETBLK ||
3024 cmd_in == MTSETDENSITY ||
3025 cmd_in == MTSETDRVBUFFER ||
3026 cmd_in == SET_DENS_AND_BLK) {
3027 if (cmdstatp->sense_hdr.sense_key == ILLEGAL_REQUEST &&
3028 !(STp->use_pf & PF_TESTED)) {
3029 /* Try the other possible state of Page Format if not
3030 already tried */
3031 STp->use_pf = !STp->use_pf | PF_TESTED;
3032 st_release_request(SRpnt);
3033 SRpnt = NULL;
3034 return st_int_ioctl(STp, cmd_in, arg);
3036 } else if (chg_eof)
3037 STps->eof = ST_NOEOF;
3039 if (cmdstatp->sense_hdr.sense_key == BLANK_CHECK)
3040 STps->eof = ST_EOD;
3042 st_release_request(SRpnt);
3043 SRpnt = NULL;
3046 return ioctl_result;
3050 /* Get the tape position. If bt == 2, arg points into a kernel space mt_loc
3051 structure. */
3053 static int get_location(struct scsi_tape *STp, unsigned int *block, int *partition,
3054 int logical)
3056 int result;
3057 unsigned char scmd[MAX_COMMAND_SIZE];
3058 struct st_request *SRpnt;
3059 DEB( char *name = tape_name(STp); )
3061 if (STp->ready != ST_READY)
3062 return (-EIO);
3064 memset(scmd, 0, MAX_COMMAND_SIZE);
3065 if ((STp->device)->scsi_level < SCSI_2) {
3066 scmd[0] = QFA_REQUEST_BLOCK;
3067 scmd[4] = 3;
3068 } else {
3069 scmd[0] = READ_POSITION;
3070 if (!logical && !STp->scsi2_logical)
3071 scmd[1] = 1;
3074 SRpnt = st_allocate_request(STp);
3075 if (!SRpnt)
3076 return STp->buffer->syscall_result;
3078 result = st_scsi_kern_execute(SRpnt, scmd, DMA_FROM_DEVICE,
3079 STp->buffer->b_data, 20,
3080 STp->device->request_queue->rq_timeout,
3081 MAX_READY_RETRIES);
3082 if (result)
3083 goto out;
3085 if ((STp->buffer)->syscall_result != 0 ||
3086 (STp->device->scsi_level >= SCSI_2 &&
3087 ((STp->buffer)->b_data[0] & 4) != 0)) {
3088 *block = *partition = 0;
3089 DEBC(printk(ST_DEB_MSG "%s: Can't read tape position.\n", name));
3090 result = (-EIO);
3091 } else {
3092 result = 0;
3093 if ((STp->device)->scsi_level < SCSI_2) {
3094 *block = ((STp->buffer)->b_data[0] << 16)
3095 + ((STp->buffer)->b_data[1] << 8)
3096 + (STp->buffer)->b_data[2];
3097 *partition = 0;
3098 } else {
3099 *block = ((STp->buffer)->b_data[4] << 24)
3100 + ((STp->buffer)->b_data[5] << 16)
3101 + ((STp->buffer)->b_data[6] << 8)
3102 + (STp->buffer)->b_data[7];
3103 *partition = (STp->buffer)->b_data[1];
3104 if (((STp->buffer)->b_data[0] & 0x80) &&
3105 (STp->buffer)->b_data[1] == 0) /* BOP of partition 0 */
3106 STp->ps[0].drv_block = STp->ps[0].drv_file = 0;
3108 DEBC(printk(ST_DEB_MSG "%s: Got tape pos. blk %d part %d.\n", name,
3109 *block, *partition));
3111 out:
3112 st_release_request(SRpnt);
3113 SRpnt = NULL;
3115 return result;
3119 /* Set the tape block and partition. Negative partition means that only the
3120 block should be set in vendor specific way. */
3121 static int set_location(struct scsi_tape *STp, unsigned int block, int partition,
3122 int logical)
3124 struct st_partstat *STps;
3125 int result, p;
3126 unsigned int blk;
3127 int timeout;
3128 unsigned char scmd[MAX_COMMAND_SIZE];
3129 struct st_request *SRpnt;
3130 DEB( char *name = tape_name(STp); )
3132 if (STp->ready != ST_READY)
3133 return (-EIO);
3134 timeout = STp->long_timeout;
3135 STps = &(STp->ps[STp->partition]);
3137 DEBC(printk(ST_DEB_MSG "%s: Setting block to %d and partition to %d.\n",
3138 name, block, partition));
3139 DEB(if (partition < 0)
3140 return (-EIO); )
3142 /* Update the location at the partition we are leaving */
3143 if ((!STp->can_partitions && partition != 0) ||
3144 partition >= ST_NBR_PARTITIONS)
3145 return (-EINVAL);
3146 if (partition != STp->partition) {
3147 if (get_location(STp, &blk, &p, 1))
3148 STps->last_block_valid = 0;
3149 else {
3150 STps->last_block_valid = 1;
3151 STps->last_block_visited = blk;
3152 DEBC(printk(ST_DEB_MSG
3153 "%s: Visited block %d for partition %d saved.\n",
3154 name, blk, STp->partition));
3158 memset(scmd, 0, MAX_COMMAND_SIZE);
3159 if ((STp->device)->scsi_level < SCSI_2) {
3160 scmd[0] = QFA_SEEK_BLOCK;
3161 scmd[2] = (block >> 16);
3162 scmd[3] = (block >> 8);
3163 scmd[4] = block;
3164 scmd[5] = 0;
3165 } else {
3166 scmd[0] = SEEK_10;
3167 scmd[3] = (block >> 24);
3168 scmd[4] = (block >> 16);
3169 scmd[5] = (block >> 8);
3170 scmd[6] = block;
3171 if (!logical && !STp->scsi2_logical)
3172 scmd[1] = 4;
3173 if (STp->partition != partition) {
3174 scmd[1] |= 2;
3175 scmd[8] = partition;
3176 DEBC(printk(ST_DEB_MSG
3177 "%s: Trying to change partition from %d to %d\n",
3178 name, STp->partition, partition));
3181 if (STp->immediate) {
3182 scmd[1] |= 1; /* Don't wait for completion */
3183 timeout = STp->device->request_queue->rq_timeout;
3186 SRpnt = st_allocate_request(STp);
3187 if (!SRpnt)
3188 return STp->buffer->syscall_result;
3190 result = st_scsi_kern_execute(SRpnt, scmd, DMA_NONE, NULL, 0,
3191 timeout, MAX_READY_RETRIES);
3192 if (result)
3193 goto out;
3195 STps->drv_block = STps->drv_file = (-1);
3196 STps->eof = ST_NOEOF;
3197 if ((STp->buffer)->syscall_result != 0) {
3198 result = (-EIO);
3199 if (STp->can_partitions &&
3200 (STp->device)->scsi_level >= SCSI_2 &&
3201 (p = find_partition(STp)) >= 0)
3202 STp->partition = p;
3203 } else {
3204 if (STp->can_partitions) {
3205 STp->partition = partition;
3206 STps = &(STp->ps[partition]);
3207 if (!STps->last_block_valid ||
3208 STps->last_block_visited != block) {
3209 STps->at_sm = 0;
3210 STps->rw = ST_IDLE;
3212 } else
3213 STps->at_sm = 0;
3214 if (block == 0)
3215 STps->drv_block = STps->drv_file = 0;
3216 result = 0;
3218 out:
3219 st_release_request(SRpnt);
3220 SRpnt = NULL;
3222 return result;
3226 /* Find the current partition number for the drive status. Called from open and
3227 returns either partition number of negative error code. */
3228 static int find_partition(struct scsi_tape *STp)
3230 int i, partition;
3231 unsigned int block;
3233 if ((i = get_location(STp, &block, &partition, 1)) < 0)
3234 return i;
3235 if (partition >= ST_NBR_PARTITIONS)
3236 return (-EIO);
3237 return partition;
3241 /* Change the partition if necessary */
3242 static int switch_partition(struct scsi_tape *STp)
3244 struct st_partstat *STps;
3246 if (STp->partition == STp->new_partition)
3247 return 0;
3248 STps = &(STp->ps[STp->new_partition]);
3249 if (!STps->last_block_valid)
3250 STps->last_block_visited = 0;
3251 return set_location(STp, STps->last_block_visited, STp->new_partition, 1);
3254 /* Functions for reading and writing the medium partition mode page. */
3256 #define PART_PAGE 0x11
3257 #define PART_PAGE_FIXED_LENGTH 8
3259 #define PP_OFF_MAX_ADD_PARTS 2
3260 #define PP_OFF_NBR_ADD_PARTS 3
3261 #define PP_OFF_FLAGS 4
3262 #define PP_OFF_PART_UNITS 6
3263 #define PP_OFF_RESERVED 7
3265 #define PP_BIT_IDP 0x20
3266 #define PP_MSK_PSUM_MB 0x10
3268 /* Get the number of partitions on the tape. As a side effect reads the
3269 mode page into the tape buffer. */
3270 static int nbr_partitions(struct scsi_tape *STp)
3272 int result;
3273 DEB( char *name = tape_name(STp); )
3275 if (STp->ready != ST_READY)
3276 return (-EIO);
3278 result = read_mode_page(STp, PART_PAGE, 1);
3280 if (result) {
3281 DEBC(printk(ST_DEB_MSG "%s: Can't read medium partition page.\n",
3282 name));
3283 result = (-EIO);
3284 } else {
3285 result = (STp->buffer)->b_data[MODE_HEADER_LENGTH +
3286 PP_OFF_NBR_ADD_PARTS] + 1;
3287 DEBC(printk(ST_DEB_MSG "%s: Number of partitions %d.\n", name, result));
3290 return result;
3294 /* Partition the tape into two partitions if size > 0 or one partition if
3295 size == 0.
3297 The block descriptors are read and written because Sony SDT-7000 does not
3298 work without this (suggestion from Michael Schaefer <Michael.Schaefer@dlr.de>).
3300 My HP C1533A drive returns only one partition size field. This is used to
3301 set the size of partition 1. There is no size field for the default partition.
3302 Michael Schaefer's Sony SDT-7000 returns two descriptors and the second is
3303 used to set the size of partition 1 (this is what the SCSI-3 standard specifies).
3304 The following algorithm is used to accommodate both drives: if the number of
3305 partition size fields is greater than the maximum number of additional partitions
3306 in the mode page, the second field is used. Otherwise the first field is used.
3308 For Seagate DDS drives the page length must be 8 when no partitions is defined
3309 and 10 when 1 partition is defined (information from Eric Lee Green). This is
3310 is acceptable also to some other old drives and enforced if the first partition
3311 size field is used for the first additional partition size.
3313 static int partition_tape(struct scsi_tape *STp, int size)
3315 char *name = tape_name(STp);
3316 int result;
3317 int pgo, psd_cnt, psdo;
3318 unsigned char *bp;
3320 result = read_mode_page(STp, PART_PAGE, 0);
3321 if (result) {
3322 DEBC(printk(ST_DEB_MSG "%s: Can't read partition mode page.\n", name));
3323 return result;
3325 /* The mode page is in the buffer. Let's modify it and write it. */
3326 bp = (STp->buffer)->b_data;
3327 pgo = MODE_HEADER_LENGTH + bp[MH_OFF_BDESCS_LENGTH];
3328 DEBC(printk(ST_DEB_MSG "%s: Partition page length is %d bytes.\n",
3329 name, bp[pgo + MP_OFF_PAGE_LENGTH] + 2));
3331 psd_cnt = (bp[pgo + MP_OFF_PAGE_LENGTH] + 2 - PART_PAGE_FIXED_LENGTH) / 2;
3332 psdo = pgo + PART_PAGE_FIXED_LENGTH;
3333 if (psd_cnt > bp[pgo + PP_OFF_MAX_ADD_PARTS]) {
3334 bp[psdo] = bp[psdo + 1] = 0xff; /* Rest of the tape */
3335 psdo += 2;
3337 memset(bp + psdo, 0, bp[pgo + PP_OFF_NBR_ADD_PARTS] * 2);
3339 DEBC(printk("%s: psd_cnt %d, max.parts %d, nbr_parts %d\n", name,
3340 psd_cnt, bp[pgo + PP_OFF_MAX_ADD_PARTS],
3341 bp[pgo + PP_OFF_NBR_ADD_PARTS]));
3343 if (size <= 0) {
3344 bp[pgo + PP_OFF_NBR_ADD_PARTS] = 0;
3345 if (psd_cnt <= bp[pgo + PP_OFF_MAX_ADD_PARTS])
3346 bp[pgo + MP_OFF_PAGE_LENGTH] = 6;
3347 DEBC(printk(ST_DEB_MSG "%s: Formatting tape with one partition.\n",
3348 name));
3349 } else {
3350 bp[psdo] = (size >> 8) & 0xff;
3351 bp[psdo + 1] = size & 0xff;
3352 bp[pgo + 3] = 1;
3353 if (bp[pgo + MP_OFF_PAGE_LENGTH] < 8)
3354 bp[pgo + MP_OFF_PAGE_LENGTH] = 8;
3355 DEBC(printk(ST_DEB_MSG
3356 "%s: Formatting tape with two partitions (1 = %d MB).\n",
3357 name, size));
3359 bp[pgo + PP_OFF_PART_UNITS] = 0;
3360 bp[pgo + PP_OFF_RESERVED] = 0;
3361 bp[pgo + PP_OFF_FLAGS] = PP_BIT_IDP | PP_MSK_PSUM_MB;
3363 result = write_mode_page(STp, PART_PAGE, 1);
3364 if (result) {
3365 printk(KERN_INFO "%s: Partitioning of tape failed.\n", name);
3366 result = (-EIO);
3369 return result;
3374 /* The ioctl command */
3375 static long st_ioctl(struct file *file, unsigned int cmd_in, unsigned long arg)
3377 int i, cmd_nr, cmd_type, bt;
3378 int retval = 0;
3379 unsigned int blk;
3380 struct scsi_tape *STp = file->private_data;
3381 struct st_modedef *STm;
3382 struct st_partstat *STps;
3383 char *name = tape_name(STp);
3384 void __user *p = (void __user *)arg;
3386 if (mutex_lock_interruptible(&STp->lock))
3387 return -ERESTARTSYS;
3389 DEB(
3390 if (debugging && !STp->in_use) {
3391 printk(ST_DEB_MSG "%s: Incorrect device.\n", name);
3392 retval = (-EIO);
3393 goto out;
3394 } ) /* end DEB */
3396 STm = &(STp->modes[STp->current_mode]);
3397 STps = &(STp->ps[STp->partition]);
3400 * If we are in the middle of error recovery, don't let anyone
3401 * else try and use this device. Also, if error recovery fails, it
3402 * may try and take the device offline, in which case all further
3403 * access to the device is prohibited.
3405 retval = scsi_nonblockable_ioctl(STp->device, cmd_in, p,
3406 file->f_flags & O_NDELAY);
3407 if (!scsi_block_when_processing_errors(STp->device) || retval != -ENODEV)
3408 goto out;
3409 retval = 0;
3411 cmd_type = _IOC_TYPE(cmd_in);
3412 cmd_nr = _IOC_NR(cmd_in);
3414 if (cmd_type == _IOC_TYPE(MTIOCTOP) && cmd_nr == _IOC_NR(MTIOCTOP)) {
3415 struct mtop mtc;
3417 if (_IOC_SIZE(cmd_in) != sizeof(mtc)) {
3418 retval = (-EINVAL);
3419 goto out;
3422 i = copy_from_user(&mtc, p, sizeof(struct mtop));
3423 if (i) {
3424 retval = (-EFAULT);
3425 goto out;
3428 if (mtc.mt_op == MTSETDRVBUFFER && !capable(CAP_SYS_ADMIN)) {
3429 printk(KERN_WARNING
3430 "%s: MTSETDRVBUFFER only allowed for root.\n", name);
3431 retval = (-EPERM);
3432 goto out;
3434 if (!STm->defined &&
3435 (mtc.mt_op != MTSETDRVBUFFER &&
3436 (mtc.mt_count & MT_ST_OPTIONS) == 0)) {
3437 retval = (-ENXIO);
3438 goto out;
3441 if (!STp->pos_unknown) {
3443 if (STps->eof == ST_FM_HIT) {
3444 if (mtc.mt_op == MTFSF || mtc.mt_op == MTFSFM ||
3445 mtc.mt_op == MTEOM) {
3446 mtc.mt_count -= 1;
3447 if (STps->drv_file >= 0)
3448 STps->drv_file += 1;
3449 } else if (mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM) {
3450 mtc.mt_count += 1;
3451 if (STps->drv_file >= 0)
3452 STps->drv_file += 1;
3456 if (mtc.mt_op == MTSEEK) {
3457 /* Old position must be restored if partition will be
3458 changed */
3459 i = !STp->can_partitions ||
3460 (STp->new_partition != STp->partition);
3461 } else {
3462 i = mtc.mt_op == MTREW || mtc.mt_op == MTOFFL ||
3463 mtc.mt_op == MTRETEN || mtc.mt_op == MTEOM ||
3464 mtc.mt_op == MTLOCK || mtc.mt_op == MTLOAD ||
3465 mtc.mt_op == MTFSF || mtc.mt_op == MTFSFM ||
3466 mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM ||
3467 mtc.mt_op == MTCOMPRESSION;
3469 i = flush_buffer(STp, i);
3470 if (i < 0) {
3471 retval = i;
3472 goto out;
3474 if (STps->rw == ST_WRITING &&
3475 (mtc.mt_op == MTREW || mtc.mt_op == MTOFFL ||
3476 mtc.mt_op == MTSEEK ||
3477 mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM)) {
3478 i = st_int_ioctl(STp, MTWEOF, 1);
3479 if (i < 0) {
3480 retval = i;
3481 goto out;
3483 if (mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM)
3484 mtc.mt_count++;
3485 STps->rw = ST_IDLE;
3488 } else {
3490 * If there was a bus reset, block further access
3491 * to this device. If the user wants to rewind the tape,
3492 * then reset the flag and allow access again.
3494 if (mtc.mt_op != MTREW &&
3495 mtc.mt_op != MTOFFL &&
3496 mtc.mt_op != MTRETEN &&
3497 mtc.mt_op != MTERASE &&
3498 mtc.mt_op != MTSEEK &&
3499 mtc.mt_op != MTEOM) {
3500 retval = (-EIO);
3501 goto out;
3503 reset_state(STp);
3504 /* remove this when the midlevel properly clears was_reset */
3505 STp->device->was_reset = 0;
3508 if (mtc.mt_op != MTNOP && mtc.mt_op != MTSETBLK &&
3509 mtc.mt_op != MTSETDENSITY && mtc.mt_op != MTWSM &&
3510 mtc.mt_op != MTSETDRVBUFFER && mtc.mt_op != MTSETPART)
3511 STps->rw = ST_IDLE; /* Prevent automatic WEOF and fsf */
3513 if (mtc.mt_op == MTOFFL && STp->door_locked != ST_UNLOCKED)
3514 do_door_lock(STp, 0); /* Ignore result! */
3516 if (mtc.mt_op == MTSETDRVBUFFER &&
3517 (mtc.mt_count & MT_ST_OPTIONS) != 0) {
3518 retval = st_set_options(STp, mtc.mt_count);
3519 goto out;
3522 if (mtc.mt_op == MTSETPART) {
3523 if (!STp->can_partitions ||
3524 mtc.mt_count < 0 || mtc.mt_count >= ST_NBR_PARTITIONS) {
3525 retval = (-EINVAL);
3526 goto out;
3528 if (mtc.mt_count >= STp->nbr_partitions &&
3529 (STp->nbr_partitions = nbr_partitions(STp)) < 0) {
3530 retval = (-EIO);
3531 goto out;
3533 if (mtc.mt_count >= STp->nbr_partitions) {
3534 retval = (-EINVAL);
3535 goto out;
3537 STp->new_partition = mtc.mt_count;
3538 retval = 0;
3539 goto out;
3542 if (mtc.mt_op == MTMKPART) {
3543 if (!STp->can_partitions) {
3544 retval = (-EINVAL);
3545 goto out;
3547 if ((i = st_int_ioctl(STp, MTREW, 0)) < 0 ||
3548 (i = partition_tape(STp, mtc.mt_count)) < 0) {
3549 retval = i;
3550 goto out;
3552 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
3553 STp->ps[i].rw = ST_IDLE;
3554 STp->ps[i].at_sm = 0;
3555 STp->ps[i].last_block_valid = 0;
3557 STp->partition = STp->new_partition = 0;
3558 STp->nbr_partitions = 1; /* Bad guess ?-) */
3559 STps->drv_block = STps->drv_file = 0;
3560 retval = 0;
3561 goto out;
3564 if (mtc.mt_op == MTSEEK) {
3565 i = set_location(STp, mtc.mt_count, STp->new_partition, 0);
3566 if (!STp->can_partitions)
3567 STp->ps[0].rw = ST_IDLE;
3568 retval = i;
3569 goto out;
3572 if (mtc.mt_op == MTUNLOAD || mtc.mt_op == MTOFFL) {
3573 retval = do_load_unload(STp, file, 0);
3574 goto out;
3577 if (mtc.mt_op == MTLOAD) {
3578 retval = do_load_unload(STp, file, max(1, mtc.mt_count));
3579 goto out;
3582 if (mtc.mt_op == MTLOCK || mtc.mt_op == MTUNLOCK) {
3583 retval = do_door_lock(STp, (mtc.mt_op == MTLOCK));
3584 goto out;
3587 if (STp->can_partitions && STp->ready == ST_READY &&
3588 (i = switch_partition(STp)) < 0) {
3589 retval = i;
3590 goto out;
3593 if (mtc.mt_op == MTCOMPRESSION)
3594 retval = st_compression(STp, (mtc.mt_count & 1));
3595 else
3596 retval = st_int_ioctl(STp, mtc.mt_op, mtc.mt_count);
3597 goto out;
3599 if (!STm->defined) {
3600 retval = (-ENXIO);
3601 goto out;
3604 if ((i = flush_buffer(STp, 0)) < 0) {
3605 retval = i;
3606 goto out;
3608 if (STp->can_partitions &&
3609 (i = switch_partition(STp)) < 0) {
3610 retval = i;
3611 goto out;
3614 if (cmd_type == _IOC_TYPE(MTIOCGET) && cmd_nr == _IOC_NR(MTIOCGET)) {
3615 struct mtget mt_status;
3617 if (_IOC_SIZE(cmd_in) != sizeof(struct mtget)) {
3618 retval = (-EINVAL);
3619 goto out;
3622 mt_status.mt_type = STp->tape_type;
3623 mt_status.mt_dsreg =
3624 ((STp->block_size << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK) |
3625 ((STp->density << MT_ST_DENSITY_SHIFT) & MT_ST_DENSITY_MASK);
3626 mt_status.mt_blkno = STps->drv_block;
3627 mt_status.mt_fileno = STps->drv_file;
3628 if (STp->block_size != 0) {
3629 if (STps->rw == ST_WRITING)
3630 mt_status.mt_blkno +=
3631 (STp->buffer)->buffer_bytes / STp->block_size;
3632 else if (STps->rw == ST_READING)
3633 mt_status.mt_blkno -=
3634 ((STp->buffer)->buffer_bytes +
3635 STp->block_size - 1) / STp->block_size;
3638 mt_status.mt_gstat = 0;
3639 if (STp->drv_write_prot)
3640 mt_status.mt_gstat |= GMT_WR_PROT(0xffffffff);
3641 if (mt_status.mt_blkno == 0) {
3642 if (mt_status.mt_fileno == 0)
3643 mt_status.mt_gstat |= GMT_BOT(0xffffffff);
3644 else
3645 mt_status.mt_gstat |= GMT_EOF(0xffffffff);
3647 mt_status.mt_erreg = (STp->recover_reg << MT_ST_SOFTERR_SHIFT);
3648 mt_status.mt_resid = STp->partition;
3649 if (STps->eof == ST_EOM_OK || STps->eof == ST_EOM_ERROR)
3650 mt_status.mt_gstat |= GMT_EOT(0xffffffff);
3651 else if (STps->eof >= ST_EOM_OK)
3652 mt_status.mt_gstat |= GMT_EOD(0xffffffff);
3653 if (STp->density == 1)
3654 mt_status.mt_gstat |= GMT_D_800(0xffffffff);
3655 else if (STp->density == 2)
3656 mt_status.mt_gstat |= GMT_D_1600(0xffffffff);
3657 else if (STp->density == 3)
3658 mt_status.mt_gstat |= GMT_D_6250(0xffffffff);
3659 if (STp->ready == ST_READY)
3660 mt_status.mt_gstat |= GMT_ONLINE(0xffffffff);
3661 if (STp->ready == ST_NO_TAPE)
3662 mt_status.mt_gstat |= GMT_DR_OPEN(0xffffffff);
3663 if (STps->at_sm)
3664 mt_status.mt_gstat |= GMT_SM(0xffffffff);
3665 if (STm->do_async_writes ||
3666 (STm->do_buffer_writes && STp->block_size != 0) ||
3667 STp->drv_buffer != 0)
3668 mt_status.mt_gstat |= GMT_IM_REP_EN(0xffffffff);
3669 if (STp->cleaning_req)
3670 mt_status.mt_gstat |= GMT_CLN(0xffffffff);
3672 i = copy_to_user(p, &mt_status, sizeof(struct mtget));
3673 if (i) {
3674 retval = (-EFAULT);
3675 goto out;
3678 STp->recover_reg = 0; /* Clear after read */
3679 retval = 0;
3680 goto out;
3681 } /* End of MTIOCGET */
3682 if (cmd_type == _IOC_TYPE(MTIOCPOS) && cmd_nr == _IOC_NR(MTIOCPOS)) {
3683 struct mtpos mt_pos;
3684 if (_IOC_SIZE(cmd_in) != sizeof(struct mtpos)) {
3685 retval = (-EINVAL);
3686 goto out;
3688 if ((i = get_location(STp, &blk, &bt, 0)) < 0) {
3689 retval = i;
3690 goto out;
3692 mt_pos.mt_blkno = blk;
3693 i = copy_to_user(p, &mt_pos, sizeof(struct mtpos));
3694 if (i)
3695 retval = (-EFAULT);
3696 goto out;
3698 mutex_unlock(&STp->lock);
3699 switch (cmd_in) {
3700 case SCSI_IOCTL_GET_IDLUN:
3701 case SCSI_IOCTL_GET_BUS_NUMBER:
3702 break;
3703 default:
3704 if ((cmd_in == SG_IO ||
3705 cmd_in == SCSI_IOCTL_SEND_COMMAND ||
3706 cmd_in == CDROM_SEND_PACKET) &&
3707 !capable(CAP_SYS_RAWIO))
3708 i = -EPERM;
3709 else
3710 i = scsi_cmd_ioctl(STp->disk->queue, STp->disk,
3711 file->f_mode, cmd_in, p);
3712 if (i != -ENOTTY)
3713 return i;
3714 break;
3716 retval = scsi_ioctl(STp->device, cmd_in, p);
3717 if (!retval && cmd_in == SCSI_IOCTL_STOP_UNIT) { /* unload */
3718 STp->rew_at_close = 0;
3719 STp->ready = ST_NO_TAPE;
3721 return retval;
3723 out:
3724 mutex_unlock(&STp->lock);
3725 return retval;
3728 #ifdef CONFIG_COMPAT
3729 static long st_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3731 struct scsi_tape *STp = file->private_data;
3732 struct scsi_device *sdev = STp->device;
3733 int ret = -ENOIOCTLCMD;
3734 if (sdev->host->hostt->compat_ioctl) {
3736 ret = sdev->host->hostt->compat_ioctl(sdev, cmd, (void __user *)arg);
3739 return ret;
3741 #endif
3745 /* Try to allocate a new tape buffer. Calling function must not hold
3746 dev_arr_lock. */
3747 static struct st_buffer *
3748 new_tape_buffer(int from_initialization, int need_dma, int max_sg)
3750 int i, got = 0;
3751 gfp_t priority;
3752 struct st_buffer *tb;
3754 if (from_initialization)
3755 priority = GFP_ATOMIC;
3756 else
3757 priority = GFP_KERNEL;
3759 i = sizeof(struct st_buffer) + (max_sg - 1) * sizeof(struct scatterlist) +
3760 max_sg * sizeof(struct st_buf_fragment);
3761 tb = kzalloc(i, priority);
3762 if (!tb) {
3763 printk(KERN_NOTICE "st: Can't allocate new tape buffer.\n");
3764 return NULL;
3766 tb->frp_segs = tb->orig_frp_segs = 0;
3767 tb->use_sg = max_sg;
3768 tb->frp = (struct st_buf_fragment *)(&(tb->sg[0]) + max_sg);
3770 tb->dma = need_dma;
3771 tb->buffer_size = got;
3772 sg_init_table(tb->sg, max_sg);
3774 tb->reserved_pages = kzalloc(max_sg * sizeof(struct page *), priority);
3775 if (!tb->reserved_pages) {
3776 kfree(tb);
3777 return NULL;
3780 return tb;
3784 /* Try to allocate enough space in the tape buffer */
3785 static int enlarge_buffer(struct st_buffer * STbuffer, int new_size, int need_dma)
3787 int segs, nbr, max_segs, b_size, order, got;
3788 gfp_t priority;
3790 if (new_size <= STbuffer->buffer_size)
3791 return 1;
3793 if (STbuffer->buffer_size <= PAGE_SIZE)
3794 normalize_buffer(STbuffer); /* Avoid extra segment */
3796 max_segs = STbuffer->use_sg;
3797 nbr = max_segs - STbuffer->frp_segs;
3798 if (nbr <= 0)
3799 return 0;
3801 priority = GFP_KERNEL | __GFP_NOWARN;
3802 if (need_dma)
3803 priority |= GFP_DMA;
3805 if (STbuffer->frp_segs) {
3806 b_size = STbuffer->frp[0].length;
3807 order = get_order(b_size);
3808 } else {
3809 for (b_size = PAGE_SIZE, order = 0;
3810 order <= 6 && b_size < new_size; order++, b_size *= 2)
3811 ; /* empty */
3814 for (segs = STbuffer->frp_segs, got = STbuffer->buffer_size;
3815 segs < max_segs && got < new_size;) {
3816 STbuffer->frp[segs].page = alloc_pages(priority, order);
3817 if (STbuffer->frp[segs].page == NULL) {
3818 DEB(STbuffer->buffer_size = got);
3819 normalize_buffer(STbuffer);
3820 return 0;
3822 STbuffer->frp[segs].length = b_size;
3823 STbuffer->frp_segs += 1;
3824 got += b_size;
3825 STbuffer->buffer_size = got;
3826 if (STbuffer->cleared)
3827 memset(page_address(STbuffer->frp[segs].page), 0, b_size);
3828 STbuffer->reserved_pages[segs] = STbuffer->frp[segs].page;
3829 segs++;
3831 STbuffer->b_data = page_address(STbuffer->frp[0].page);
3832 STbuffer->map_data.page_order = order;
3834 return 1;
3838 /* Make sure that no data from previous user is in the internal buffer */
3839 static void clear_buffer(struct st_buffer * st_bp)
3841 int i;
3843 for (i=0; i < st_bp->frp_segs; i++)
3844 memset(page_address(st_bp->frp[i].page), 0, st_bp->frp[i].length);
3845 st_bp->cleared = 1;
3849 /* Release the extra buffer */
3850 static void normalize_buffer(struct st_buffer * STbuffer)
3852 int i, order;
3854 for (i = STbuffer->orig_frp_segs; i < STbuffer->frp_segs; i++) {
3855 order = get_order(STbuffer->frp[i].length);
3856 __free_pages(STbuffer->frp[i].page, order);
3857 STbuffer->buffer_size -= STbuffer->frp[i].length;
3859 STbuffer->frp_segs = STbuffer->orig_frp_segs;
3860 STbuffer->frp_sg_current = 0;
3861 STbuffer->sg_segs = 0;
3862 STbuffer->map_data.page_order = 0;
3863 STbuffer->map_data.offset = 0;
3867 /* Move data from the user buffer to the tape buffer. Returns zero (success) or
3868 negative error code. */
3869 static int append_to_buffer(const char __user *ubp, struct st_buffer * st_bp, int do_count)
3871 int i, cnt, res, offset;
3873 for (i = 0, offset = st_bp->buffer_bytes;
3874 i < st_bp->frp_segs && offset >= st_bp->frp[i].length; i++)
3875 offset -= st_bp->frp[i].length;
3876 if (i == st_bp->frp_segs) { /* Should never happen */
3877 printk(KERN_WARNING "st: append_to_buffer offset overflow.\n");
3878 return (-EIO);
3880 for (; i < st_bp->frp_segs && do_count > 0; i++) {
3881 cnt = st_bp->frp[i].length - offset < do_count ?
3882 st_bp->frp[i].length - offset : do_count;
3883 res = copy_from_user(page_address(st_bp->frp[i].page) + offset, ubp, cnt);
3884 if (res)
3885 return (-EFAULT);
3886 do_count -= cnt;
3887 st_bp->buffer_bytes += cnt;
3888 ubp += cnt;
3889 offset = 0;
3891 if (do_count) /* Should never happen */
3892 return (-EIO);
3894 return 0;
3898 /* Move data from the tape buffer to the user buffer. Returns zero (success) or
3899 negative error code. */
3900 static int from_buffer(struct st_buffer * st_bp, char __user *ubp, int do_count)
3902 int i, cnt, res, offset;
3904 for (i = 0, offset = st_bp->read_pointer;
3905 i < st_bp->frp_segs && offset >= st_bp->frp[i].length; i++)
3906 offset -= st_bp->frp[i].length;
3907 if (i == st_bp->frp_segs) { /* Should never happen */
3908 printk(KERN_WARNING "st: from_buffer offset overflow.\n");
3909 return (-EIO);
3911 for (; i < st_bp->frp_segs && do_count > 0; i++) {
3912 cnt = st_bp->frp[i].length - offset < do_count ?
3913 st_bp->frp[i].length - offset : do_count;
3914 res = copy_to_user(ubp, page_address(st_bp->frp[i].page) + offset, cnt);
3915 if (res)
3916 return (-EFAULT);
3917 do_count -= cnt;
3918 st_bp->buffer_bytes -= cnt;
3919 st_bp->read_pointer += cnt;
3920 ubp += cnt;
3921 offset = 0;
3923 if (do_count) /* Should never happen */
3924 return (-EIO);
3926 return 0;
3930 /* Move data towards start of buffer */
3931 static void move_buffer_data(struct st_buffer * st_bp, int offset)
3933 int src_seg, dst_seg, src_offset = 0, dst_offset;
3934 int count, total;
3936 if (offset == 0)
3937 return;
3939 total=st_bp->buffer_bytes - offset;
3940 for (src_seg=0; src_seg < st_bp->frp_segs; src_seg++) {
3941 src_offset = offset;
3942 if (src_offset < st_bp->frp[src_seg].length)
3943 break;
3944 offset -= st_bp->frp[src_seg].length;
3947 st_bp->buffer_bytes = st_bp->read_pointer = total;
3948 for (dst_seg=dst_offset=0; total > 0; ) {
3949 count = min(st_bp->frp[dst_seg].length - dst_offset,
3950 st_bp->frp[src_seg].length - src_offset);
3951 memmove(page_address(st_bp->frp[dst_seg].page) + dst_offset,
3952 page_address(st_bp->frp[src_seg].page) + src_offset, count);
3953 src_offset += count;
3954 if (src_offset >= st_bp->frp[src_seg].length) {
3955 src_seg++;
3956 src_offset = 0;
3958 dst_offset += count;
3959 if (dst_offset >= st_bp->frp[dst_seg].length) {
3960 dst_seg++;
3961 dst_offset = 0;
3963 total -= count;
3968 /* Fill the s/g list up to the length required for this transfer */
3969 static void buf_to_sg(struct st_buffer *STbp, unsigned int length)
3971 int i;
3972 unsigned int count;
3973 struct scatterlist *sg;
3974 struct st_buf_fragment *frp;
3976 if (length == STbp->frp_sg_current)
3977 return; /* work already done */
3979 sg = &(STbp->sg[0]);
3980 frp = STbp->frp;
3981 for (i=count=0; count < length; i++) {
3982 if (length - count > frp[i].length)
3983 sg_set_page(&sg[i], frp[i].page, frp[i].length, 0);
3984 else
3985 sg_set_page(&sg[i], frp[i].page, length - count, 0);
3986 count += sg[i].length;
3988 STbp->sg_segs = i;
3989 STbp->frp_sg_current = length;
3993 /* Validate the options from command line or module parameters */
3994 static void validate_options(void)
3996 if (buffer_kbs > 0)
3997 st_fixed_buffer_size = buffer_kbs * ST_KILOBYTE;
3998 if (max_sg_segs >= ST_FIRST_SG)
3999 st_max_sg_segs = max_sg_segs;
4002 #ifndef MODULE
4003 /* Set the boot options. Syntax is defined in Documenation/scsi/st.txt.
4005 static int __init st_setup(char *str)
4007 int i, len, ints[5];
4008 char *stp;
4010 stp = get_options(str, ARRAY_SIZE(ints), ints);
4012 if (ints[0] > 0) {
4013 for (i = 0; i < ints[0] && i < ARRAY_SIZE(parms); i++)
4014 if (parms[i].val)
4015 *parms[i].val = ints[i + 1];
4016 } else {
4017 while (stp != NULL) {
4018 for (i = 0; i < ARRAY_SIZE(parms); i++) {
4019 len = strlen(parms[i].name);
4020 if (!strncmp(stp, parms[i].name, len) &&
4021 (*(stp + len) == ':' || *(stp + len) == '=')) {
4022 if (parms[i].val)
4023 *parms[i].val =
4024 simple_strtoul(stp + len + 1, NULL, 0);
4025 else
4026 printk(KERN_WARNING "st: Obsolete parameter %s\n",
4027 parms[i].name);
4028 break;
4031 if (i >= ARRAY_SIZE(parms))
4032 printk(KERN_WARNING "st: invalid parameter in '%s'\n",
4033 stp);
4034 stp = strchr(stp, ',');
4035 if (stp)
4036 stp++;
4040 validate_options();
4042 return 1;
4045 __setup("st=", st_setup);
4047 #endif
4049 static const struct file_operations st_fops =
4051 .owner = THIS_MODULE,
4052 .read = st_read,
4053 .write = st_write,
4054 .unlocked_ioctl = st_ioctl,
4055 #ifdef CONFIG_COMPAT
4056 .compat_ioctl = st_compat_ioctl,
4057 #endif
4058 .open = st_open,
4059 .flush = st_flush,
4060 .release = st_release,
4063 static int st_probe(struct device *dev)
4065 struct scsi_device *SDp = to_scsi_device(dev);
4066 struct gendisk *disk = NULL;
4067 struct cdev *cdev = NULL;
4068 struct scsi_tape *tpnt = NULL;
4069 struct st_modedef *STm;
4070 struct st_partstat *STps;
4071 struct st_buffer *buffer;
4072 int i, j, mode, dev_num, error;
4073 char *stp;
4075 if (SDp->type != TYPE_TAPE)
4076 return -ENODEV;
4077 if ((stp = st_incompatible(SDp))) {
4078 sdev_printk(KERN_INFO, SDp, "Found incompatible tape\n");
4079 printk(KERN_INFO "st: The suggested driver is %s.\n", stp);
4080 return -ENODEV;
4083 i = min(SDp->request_queue->max_hw_segments,
4084 SDp->request_queue->max_phys_segments);
4085 if (st_max_sg_segs < i)
4086 i = st_max_sg_segs;
4087 buffer = new_tape_buffer(1, (SDp->host)->unchecked_isa_dma, i);
4088 if (buffer == NULL) {
4089 printk(KERN_ERR
4090 "st: Can't allocate new tape buffer. Device not attached.\n");
4091 goto out;
4094 disk = alloc_disk(1);
4095 if (!disk) {
4096 printk(KERN_ERR "st: out of memory. Device not attached.\n");
4097 goto out_buffer_free;
4100 write_lock(&st_dev_arr_lock);
4101 if (st_nr_dev >= st_dev_max) {
4102 struct scsi_tape **tmp_da;
4103 int tmp_dev_max;
4105 tmp_dev_max = max(st_nr_dev * 2, 8);
4106 if (tmp_dev_max > ST_MAX_TAPES)
4107 tmp_dev_max = ST_MAX_TAPES;
4108 if (tmp_dev_max <= st_nr_dev) {
4109 write_unlock(&st_dev_arr_lock);
4110 printk(KERN_ERR "st: Too many tape devices (max. %d).\n",
4111 ST_MAX_TAPES);
4112 goto out_put_disk;
4115 tmp_da = kzalloc(tmp_dev_max * sizeof(struct scsi_tape *), GFP_ATOMIC);
4116 if (tmp_da == NULL) {
4117 write_unlock(&st_dev_arr_lock);
4118 printk(KERN_ERR "st: Can't extend device array.\n");
4119 goto out_put_disk;
4122 if (scsi_tapes != NULL) {
4123 memcpy(tmp_da, scsi_tapes,
4124 st_dev_max * sizeof(struct scsi_tape *));
4125 kfree(scsi_tapes);
4127 scsi_tapes = tmp_da;
4129 st_dev_max = tmp_dev_max;
4132 for (i = 0; i < st_dev_max; i++)
4133 if (scsi_tapes[i] == NULL)
4134 break;
4135 if (i >= st_dev_max)
4136 panic("scsi_devices corrupt (st)");
4138 tpnt = kzalloc(sizeof(struct scsi_tape), GFP_ATOMIC);
4139 if (tpnt == NULL) {
4140 write_unlock(&st_dev_arr_lock);
4141 printk(KERN_ERR "st: Can't allocate device descriptor.\n");
4142 goto out_put_disk;
4144 kref_init(&tpnt->kref);
4145 tpnt->disk = disk;
4146 sprintf(disk->disk_name, "st%d", i);
4147 disk->private_data = &tpnt->driver;
4148 disk->queue = SDp->request_queue;
4149 tpnt->driver = &st_template;
4150 scsi_tapes[i] = tpnt;
4151 dev_num = i;
4153 tpnt->device = SDp;
4154 if (SDp->scsi_level <= 2)
4155 tpnt->tape_type = MT_ISSCSI1;
4156 else
4157 tpnt->tape_type = MT_ISSCSI2;
4159 tpnt->buffer = buffer;
4160 tpnt->buffer->last_SRpnt = NULL;
4162 tpnt->inited = 0;
4163 tpnt->dirty = 0;
4164 tpnt->in_use = 0;
4165 tpnt->drv_buffer = 1; /* Try buffering if no mode sense */
4166 tpnt->restr_dma = (SDp->host)->unchecked_isa_dma;
4167 tpnt->use_pf = (SDp->scsi_level >= SCSI_2);
4168 tpnt->density = 0;
4169 tpnt->do_auto_lock = ST_AUTO_LOCK;
4170 tpnt->can_bsr = (SDp->scsi_level > 2 ? 1 : ST_IN_FILE_POS); /* BSR mandatory in SCSI3 */
4171 tpnt->can_partitions = 0;
4172 tpnt->two_fm = ST_TWO_FM;
4173 tpnt->fast_mteom = ST_FAST_MTEOM;
4174 tpnt->scsi2_logical = ST_SCSI2LOGICAL;
4175 tpnt->sili = ST_SILI;
4176 tpnt->immediate = ST_NOWAIT;
4177 tpnt->default_drvbuffer = 0xff; /* No forced buffering */
4178 tpnt->partition = 0;
4179 tpnt->new_partition = 0;
4180 tpnt->nbr_partitions = 0;
4181 blk_queue_rq_timeout(tpnt->device->request_queue, ST_TIMEOUT);
4182 tpnt->long_timeout = ST_LONG_TIMEOUT;
4183 tpnt->try_dio = try_direct_io && !SDp->host->unchecked_isa_dma;
4185 for (i = 0; i < ST_NBR_MODES; i++) {
4186 STm = &(tpnt->modes[i]);
4187 STm->defined = 0;
4188 STm->sysv = ST_SYSV;
4189 STm->defaults_for_writes = 0;
4190 STm->do_async_writes = ST_ASYNC_WRITES;
4191 STm->do_buffer_writes = ST_BUFFER_WRITES;
4192 STm->do_read_ahead = ST_READ_AHEAD;
4193 STm->default_compression = ST_DONT_TOUCH;
4194 STm->default_blksize = (-1); /* No forced size */
4195 STm->default_density = (-1); /* No forced density */
4198 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
4199 STps = &(tpnt->ps[i]);
4200 STps->rw = ST_IDLE;
4201 STps->eof = ST_NOEOF;
4202 STps->at_sm = 0;
4203 STps->last_block_valid = 0;
4204 STps->drv_block = (-1);
4205 STps->drv_file = (-1);
4208 tpnt->current_mode = 0;
4209 tpnt->modes[0].defined = 1;
4211 tpnt->density_changed = tpnt->compression_changed =
4212 tpnt->blksize_changed = 0;
4213 mutex_init(&tpnt->lock);
4215 st_nr_dev++;
4216 write_unlock(&st_dev_arr_lock);
4218 for (mode = 0; mode < ST_NBR_MODES; ++mode) {
4219 STm = &(tpnt->modes[mode]);
4220 for (j=0; j < 2; j++) {
4221 cdev = cdev_alloc();
4222 if (!cdev) {
4223 printk(KERN_ERR
4224 "st%d: out of memory. Device not attached.\n",
4225 dev_num);
4226 goto out_free_tape;
4228 cdev->owner = THIS_MODULE;
4229 cdev->ops = &st_fops;
4231 error = cdev_add(cdev,
4232 MKDEV(SCSI_TAPE_MAJOR, TAPE_MINOR(dev_num, mode, j)),
4234 if (error) {
4235 printk(KERN_ERR "st%d: Can't add %s-rewind mode %d\n",
4236 dev_num, j ? "non" : "auto", mode);
4237 printk(KERN_ERR "st%d: Device not attached.\n", dev_num);
4238 goto out_free_tape;
4240 STm->cdevs[j] = cdev;
4243 error = do_create_class_files(tpnt, dev_num, mode);
4244 if (error)
4245 goto out_free_tape;
4248 sdev_printk(KERN_NOTICE, SDp,
4249 "Attached scsi tape %s\n", tape_name(tpnt));
4250 sdev_printk(KERN_INFO, SDp, "%s: try direct i/o: %s (alignment %d B)\n",
4251 tape_name(tpnt), tpnt->try_dio ? "yes" : "no",
4252 queue_dma_alignment(SDp->request_queue) + 1);
4254 return 0;
4256 out_free_tape:
4257 for (mode=0; mode < ST_NBR_MODES; mode++) {
4258 STm = &(tpnt->modes[mode]);
4259 sysfs_remove_link(&tpnt->device->sdev_gendev.kobj,
4260 "tape");
4261 for (j=0; j < 2; j++) {
4262 if (STm->cdevs[j]) {
4263 if (cdev == STm->cdevs[j])
4264 cdev = NULL;
4265 device_destroy(st_sysfs_class,
4266 MKDEV(SCSI_TAPE_MAJOR,
4267 TAPE_MINOR(i, mode, j)));
4268 cdev_del(STm->cdevs[j]);
4272 if (cdev)
4273 cdev_del(cdev);
4274 write_lock(&st_dev_arr_lock);
4275 scsi_tapes[dev_num] = NULL;
4276 st_nr_dev--;
4277 write_unlock(&st_dev_arr_lock);
4278 out_put_disk:
4279 put_disk(disk);
4280 kfree(tpnt);
4281 out_buffer_free:
4282 kfree(buffer);
4283 out:
4284 return -ENODEV;
4288 static int st_remove(struct device *dev)
4290 struct scsi_device *SDp = to_scsi_device(dev);
4291 struct scsi_tape *tpnt;
4292 int i, j, mode;
4294 write_lock(&st_dev_arr_lock);
4295 for (i = 0; i < st_dev_max; i++) {
4296 tpnt = scsi_tapes[i];
4297 if (tpnt != NULL && tpnt->device == SDp) {
4298 scsi_tapes[i] = NULL;
4299 st_nr_dev--;
4300 write_unlock(&st_dev_arr_lock);
4301 sysfs_remove_link(&tpnt->device->sdev_gendev.kobj,
4302 "tape");
4303 for (mode = 0; mode < ST_NBR_MODES; ++mode) {
4304 for (j=0; j < 2; j++) {
4305 device_destroy(st_sysfs_class,
4306 MKDEV(SCSI_TAPE_MAJOR,
4307 TAPE_MINOR(i, mode, j)));
4308 cdev_del(tpnt->modes[mode].cdevs[j]);
4309 tpnt->modes[mode].cdevs[j] = NULL;
4313 mutex_lock(&st_ref_mutex);
4314 kref_put(&tpnt->kref, scsi_tape_release);
4315 mutex_unlock(&st_ref_mutex);
4316 return 0;
4320 write_unlock(&st_dev_arr_lock);
4321 return 0;
4325 * scsi_tape_release - Called to free the Scsi_Tape structure
4326 * @kref: pointer to embedded kref
4328 * st_ref_mutex must be held entering this routine. Because it is
4329 * called on last put, you should always use the scsi_tape_get()
4330 * scsi_tape_put() helpers which manipulate the semaphore directly
4331 * and never do a direct kref_put().
4333 static void scsi_tape_release(struct kref *kref)
4335 struct scsi_tape *tpnt = to_scsi_tape(kref);
4336 struct gendisk *disk = tpnt->disk;
4338 tpnt->device = NULL;
4340 if (tpnt->buffer) {
4341 tpnt->buffer->orig_frp_segs = 0;
4342 normalize_buffer(tpnt->buffer);
4343 kfree(tpnt->buffer->reserved_pages);
4344 kfree(tpnt->buffer);
4347 disk->private_data = NULL;
4348 put_disk(disk);
4349 kfree(tpnt);
4350 return;
4353 static int __init init_st(void)
4355 int err;
4357 validate_options();
4359 printk(KERN_INFO "st: Version %s, fixed bufsize %d, s/g segs %d\n",
4360 verstr, st_fixed_buffer_size, st_max_sg_segs);
4362 st_sysfs_class = class_create(THIS_MODULE, "scsi_tape");
4363 if (IS_ERR(st_sysfs_class)) {
4364 printk(KERN_ERR "Unable create sysfs class for SCSI tapes\n");
4365 return PTR_ERR(st_sysfs_class);
4368 err = register_chrdev_region(MKDEV(SCSI_TAPE_MAJOR, 0),
4369 ST_MAX_TAPE_ENTRIES, "st");
4370 if (err) {
4371 printk(KERN_ERR "Unable to get major %d for SCSI tapes\n",
4372 SCSI_TAPE_MAJOR);
4373 goto err_class;
4376 err = scsi_register_driver(&st_template.gendrv);
4377 if (err)
4378 goto err_chrdev;
4380 err = do_create_sysfs_files();
4381 if (err)
4382 goto err_scsidrv;
4384 return 0;
4386 err_scsidrv:
4387 scsi_unregister_driver(&st_template.gendrv);
4388 err_chrdev:
4389 unregister_chrdev_region(MKDEV(SCSI_TAPE_MAJOR, 0),
4390 ST_MAX_TAPE_ENTRIES);
4391 err_class:
4392 class_destroy(st_sysfs_class);
4393 return err;
4396 static void __exit exit_st(void)
4398 do_remove_sysfs_files();
4399 scsi_unregister_driver(&st_template.gendrv);
4400 unregister_chrdev_region(MKDEV(SCSI_TAPE_MAJOR, 0),
4401 ST_MAX_TAPE_ENTRIES);
4402 class_destroy(st_sysfs_class);
4403 kfree(scsi_tapes);
4404 printk(KERN_INFO "st: Unloaded.\n");
4407 module_init(init_st);
4408 module_exit(exit_st);
4411 /* The sysfs driver interface. Read-only at the moment */
4412 static ssize_t st_try_direct_io_show(struct device_driver *ddp, char *buf)
4414 return snprintf(buf, PAGE_SIZE, "%d\n", try_direct_io);
4416 static DRIVER_ATTR(try_direct_io, S_IRUGO, st_try_direct_io_show, NULL);
4418 static ssize_t st_fixed_buffer_size_show(struct device_driver *ddp, char *buf)
4420 return snprintf(buf, PAGE_SIZE, "%d\n", st_fixed_buffer_size);
4422 static DRIVER_ATTR(fixed_buffer_size, S_IRUGO, st_fixed_buffer_size_show, NULL);
4424 static ssize_t st_max_sg_segs_show(struct device_driver *ddp, char *buf)
4426 return snprintf(buf, PAGE_SIZE, "%d\n", st_max_sg_segs);
4428 static DRIVER_ATTR(max_sg_segs, S_IRUGO, st_max_sg_segs_show, NULL);
4430 static ssize_t st_version_show(struct device_driver *ddd, char *buf)
4432 return snprintf(buf, PAGE_SIZE, "[%s]\n", verstr);
4434 static DRIVER_ATTR(version, S_IRUGO, st_version_show, NULL);
4436 static int do_create_sysfs_files(void)
4438 struct device_driver *sysfs = &st_template.gendrv;
4439 int err;
4441 err = driver_create_file(sysfs, &driver_attr_try_direct_io);
4442 if (err)
4443 return err;
4444 err = driver_create_file(sysfs, &driver_attr_fixed_buffer_size);
4445 if (err)
4446 goto err_try_direct_io;
4447 err = driver_create_file(sysfs, &driver_attr_max_sg_segs);
4448 if (err)
4449 goto err_attr_fixed_buf;
4450 err = driver_create_file(sysfs, &driver_attr_version);
4451 if (err)
4452 goto err_attr_max_sg;
4454 return 0;
4456 err_attr_max_sg:
4457 driver_remove_file(sysfs, &driver_attr_max_sg_segs);
4458 err_attr_fixed_buf:
4459 driver_remove_file(sysfs, &driver_attr_fixed_buffer_size);
4460 err_try_direct_io:
4461 driver_remove_file(sysfs, &driver_attr_try_direct_io);
4462 return err;
4465 static void do_remove_sysfs_files(void)
4467 struct device_driver *sysfs = &st_template.gendrv;
4469 driver_remove_file(sysfs, &driver_attr_version);
4470 driver_remove_file(sysfs, &driver_attr_max_sg_segs);
4471 driver_remove_file(sysfs, &driver_attr_fixed_buffer_size);
4472 driver_remove_file(sysfs, &driver_attr_try_direct_io);
4476 /* The sysfs simple class interface */
4477 static ssize_t
4478 st_defined_show(struct device *dev, struct device_attribute *attr, char *buf)
4480 struct st_modedef *STm = dev_get_drvdata(dev);
4481 ssize_t l = 0;
4483 l = snprintf(buf, PAGE_SIZE, "%d\n", STm->defined);
4484 return l;
4487 DEVICE_ATTR(defined, S_IRUGO, st_defined_show, NULL);
4489 static ssize_t
4490 st_defblk_show(struct device *dev, struct device_attribute *attr, char *buf)
4492 struct st_modedef *STm = dev_get_drvdata(dev);
4493 ssize_t l = 0;
4495 l = snprintf(buf, PAGE_SIZE, "%d\n", STm->default_blksize);
4496 return l;
4499 DEVICE_ATTR(default_blksize, S_IRUGO, st_defblk_show, NULL);
4501 static ssize_t
4502 st_defdensity_show(struct device *dev, struct device_attribute *attr, char *buf)
4504 struct st_modedef *STm = dev_get_drvdata(dev);
4505 ssize_t l = 0;
4506 char *fmt;
4508 fmt = STm->default_density >= 0 ? "0x%02x\n" : "%d\n";
4509 l = snprintf(buf, PAGE_SIZE, fmt, STm->default_density);
4510 return l;
4513 DEVICE_ATTR(default_density, S_IRUGO, st_defdensity_show, NULL);
4515 static ssize_t
4516 st_defcompression_show(struct device *dev, struct device_attribute *attr,
4517 char *buf)
4519 struct st_modedef *STm = dev_get_drvdata(dev);
4520 ssize_t l = 0;
4522 l = snprintf(buf, PAGE_SIZE, "%d\n", STm->default_compression - 1);
4523 return l;
4526 DEVICE_ATTR(default_compression, S_IRUGO, st_defcompression_show, NULL);
4528 static ssize_t
4529 st_options_show(struct device *dev, struct device_attribute *attr, char *buf)
4531 struct st_modedef *STm = dev_get_drvdata(dev);
4532 struct scsi_tape *STp;
4533 int i, j, options;
4534 ssize_t l = 0;
4536 for (i=0; i < st_dev_max; i++) {
4537 for (j=0; j < ST_NBR_MODES; j++)
4538 if (&scsi_tapes[i]->modes[j] == STm)
4539 break;
4540 if (j < ST_NBR_MODES)
4541 break;
4543 if (i == st_dev_max)
4544 return 0; /* should never happen */
4546 STp = scsi_tapes[i];
4548 options = STm->do_buffer_writes ? MT_ST_BUFFER_WRITES : 0;
4549 options |= STm->do_async_writes ? MT_ST_ASYNC_WRITES : 0;
4550 options |= STm->do_read_ahead ? MT_ST_READ_AHEAD : 0;
4551 DEB( options |= debugging ? MT_ST_DEBUGGING : 0 );
4552 options |= STp->two_fm ? MT_ST_TWO_FM : 0;
4553 options |= STp->fast_mteom ? MT_ST_FAST_MTEOM : 0;
4554 options |= STm->defaults_for_writes ? MT_ST_DEF_WRITES : 0;
4555 options |= STp->can_bsr ? MT_ST_CAN_BSR : 0;
4556 options |= STp->omit_blklims ? MT_ST_NO_BLKLIMS : 0;
4557 options |= STp->can_partitions ? MT_ST_CAN_PARTITIONS : 0;
4558 options |= STp->scsi2_logical ? MT_ST_SCSI2LOGICAL : 0;
4559 options |= STm->sysv ? MT_ST_SYSV : 0;
4560 options |= STp->immediate ? MT_ST_NOWAIT : 0;
4561 options |= STp->sili ? MT_ST_SILI : 0;
4563 l = snprintf(buf, PAGE_SIZE, "0x%08x\n", options);
4564 return l;
4567 DEVICE_ATTR(options, S_IRUGO, st_options_show, NULL);
4569 static int do_create_class_files(struct scsi_tape *STp, int dev_num, int mode)
4571 int i, rew, error;
4572 char name[10];
4573 struct device *st_class_member;
4575 for (rew=0; rew < 2; rew++) {
4576 /* Make sure that the minor numbers corresponding to the four
4577 first modes always get the same names */
4578 i = mode << (4 - ST_NBR_MODE_BITS);
4579 snprintf(name, 10, "%s%s%s", rew ? "n" : "",
4580 STp->disk->disk_name, st_formats[i]);
4581 st_class_member =
4582 device_create(st_sysfs_class, &STp->device->sdev_gendev,
4583 MKDEV(SCSI_TAPE_MAJOR,
4584 TAPE_MINOR(dev_num, mode, rew)),
4585 &STp->modes[mode], "%s", name);
4586 if (IS_ERR(st_class_member)) {
4587 printk(KERN_WARNING "st%d: device_create failed\n",
4588 dev_num);
4589 error = PTR_ERR(st_class_member);
4590 goto out;
4593 error = device_create_file(st_class_member,
4594 &dev_attr_defined);
4595 if (error) goto out;
4596 error = device_create_file(st_class_member,
4597 &dev_attr_default_blksize);
4598 if (error) goto out;
4599 error = device_create_file(st_class_member,
4600 &dev_attr_default_density);
4601 if (error) goto out;
4602 error = device_create_file(st_class_member,
4603 &dev_attr_default_compression);
4604 if (error) goto out;
4605 error = device_create_file(st_class_member,
4606 &dev_attr_options);
4607 if (error) goto out;
4609 if (mode == 0 && rew == 0) {
4610 error = sysfs_create_link(&STp->device->sdev_gendev.kobj,
4611 &st_class_member->kobj,
4612 "tape");
4613 if (error) {
4614 printk(KERN_ERR
4615 "st%d: Can't create sysfs link from SCSI device.\n",
4616 dev_num);
4617 goto out;
4622 return 0;
4624 out:
4625 return error;
4628 /* The following functions may be useful for a larger audience. */
4629 static int sgl_map_user_pages(struct st_buffer *STbp,
4630 const unsigned int max_pages, unsigned long uaddr,
4631 size_t count, int rw)
4633 unsigned long end = (uaddr + count + PAGE_SIZE - 1) >> PAGE_SHIFT;
4634 unsigned long start = uaddr >> PAGE_SHIFT;
4635 const int nr_pages = end - start;
4636 int res, i, j;
4637 struct page **pages;
4638 struct rq_map_data *mdata = &STbp->map_data;
4640 /* User attempted Overflow! */
4641 if ((uaddr + count) < uaddr)
4642 return -EINVAL;
4644 /* Too big */
4645 if (nr_pages > max_pages)
4646 return -ENOMEM;
4648 /* Hmm? */
4649 if (count == 0)
4650 return 0;
4652 if ((pages = kmalloc(max_pages * sizeof(*pages), GFP_KERNEL)) == NULL)
4653 return -ENOMEM;
4655 /* Try to fault in all of the necessary pages */
4656 down_read(&current->mm->mmap_sem);
4657 /* rw==READ means read from drive, write into memory area */
4658 res = get_user_pages(
4659 current,
4660 current->mm,
4661 uaddr,
4662 nr_pages,
4663 rw == READ,
4664 0, /* don't force */
4665 pages,
4666 NULL);
4667 up_read(&current->mm->mmap_sem);
4669 /* Errors and no page mapped should return here */
4670 if (res < nr_pages)
4671 goto out_unmap;
4673 for (i=0; i < nr_pages; i++) {
4674 /* FIXME: flush superflous for rw==READ,
4675 * probably wrong function for rw==WRITE
4677 flush_dcache_page(pages[i]);
4680 mdata->offset = uaddr & ~PAGE_MASK;
4681 mdata->page_order = 0;
4682 STbp->mapped_pages = pages;
4684 return nr_pages;
4685 out_unmap:
4686 if (res > 0) {
4687 for (j=0; j < res; j++)
4688 page_cache_release(pages[j]);
4689 res = 0;
4691 kfree(pages);
4692 return res;
4696 /* And unmap them... */
4697 static int sgl_unmap_user_pages(struct st_buffer *STbp,
4698 const unsigned int nr_pages, int dirtied)
4700 int i;
4702 for (i=0; i < nr_pages; i++) {
4703 struct page *page = STbp->mapped_pages[i];
4705 if (dirtied)
4706 SetPageDirty(page);
4707 /* FIXME: cache flush missing for rw==READ
4708 * FIXME: call the correct reference counting function
4710 page_cache_release(page);
4712 kfree(STbp->mapped_pages);
4713 STbp->mapped_pages = NULL;
4715 return 0;