Linux 2.4.0-test7-pre6
[davej-history.git] / drivers / scsi / st.c
blobdd5d1786651c1737343168ddc189cc3d86cc9f30
1 /*
2 SCSI Tape Driver for Linux version 1.1 and newer. See the accompanying
3 file README.st 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, Eric Lee Green, Wolfgang Denk, Steve Hirsch,
9 Andreas Koppenh"ofer, Michael Leodolter, Eyal Lebedinsky, Michael Schaefer,
10 J"org Weule, and Eric Youngdale.
12 Copyright 1992 - 2000 Kai Makisara
13 email Kai.Makisara@metla.fi
15 Last modified: Tue Aug 15 16:56:35 2000 by makisara@kai.makisara.local
16 Some small formal changes - aeb, 950809
18 Last modified: 18-JAN-1998 Richard Gooch <rgooch@atnf.csiro.au> Devfs support
20 Reminder: write_lock_irqsave() can be replaced by write_lock() when the old SCSI
21 error handling will be discarded.
24 #include <linux/module.h>
26 #include <linux/fs.h>
27 #include <linux/kernel.h>
28 #include <linux/sched.h>
29 #include <linux/mm.h>
30 #include <linux/init.h>
31 #include <linux/string.h>
32 #include <linux/errno.h>
33 #include <linux/mtio.h>
34 #include <linux/ioctl.h>
35 #include <linux/fcntl.h>
36 #include <linux/spinlock.h>
37 #include <linux/smp_lock.h>
38 #include <asm/uaccess.h>
39 #include <asm/dma.h>
40 #include <asm/system.h>
42 /* The driver prints some debugging information on the console if DEBUG
43 is defined and non-zero. */
44 #define DEBUG 0
46 #if DEBUG
47 /* The message level for the debug messages is currently set to KERN_NOTICE
48 so that people can easily see the messages. Later when the debugging messages
49 in the drivers are more widely classified, this may be changed to KERN_DEBUG. */
50 #define ST_DEB_MSG KERN_NOTICE
51 #define DEB(a) a
52 #define DEBC(a) if (debugging) { a ; }
53 #else
54 #define DEB(a)
55 #define DEBC(a)
56 #endif
58 #define MAJOR_NR SCSI_TAPE_MAJOR
59 #include <linux/blk.h>
61 #include "scsi.h"
62 #include "hosts.h"
63 #include <scsi/scsi_ioctl.h>
65 #define ST_KILOBYTE 1024
67 #include "st_options.h"
68 #include "st.h"
70 #include "constants.h"
72 static int buffer_kbs;
73 static int write_threshold_kbs;
74 static int max_buffers = (-1);
75 static int max_sg_segs;
77 MODULE_AUTHOR("Kai Makisara");
78 MODULE_DESCRIPTION("SCSI Tape Driver");
79 MODULE_PARM(buffer_kbs, "i");
80 MODULE_PARM(write_threshold_kbs, "i");
81 MODULE_PARM(max_buffers, "i");
82 MODULE_PARM(max_sg_segs, "i");
84 #ifndef MODULE
85 static struct st_dev_parm {
86 char *name;
87 int *val;
88 } parms[] __initdata = {
90 "buffer_kbs", &buffer_kbs
93 "write_threshold_kbs", &write_threshold_kbs
96 "max_buffers", &max_buffers
99 "max_sg_segs", &max_sg_segs
102 #endif
105 /* The default definitions have been moved to st_options.h */
107 #define ST_BUFFER_SIZE (ST_BUFFER_BLOCKS * ST_KILOBYTE)
108 #define ST_WRITE_THRESHOLD (ST_WRITE_THRESHOLD_BLOCKS * ST_KILOBYTE)
110 /* The buffer size should fit into the 24 bits for length in the
111 6-byte SCSI read and write commands. */
112 #if ST_BUFFER_SIZE >= (2 << 24 - 1)
113 #error "Buffer size should not exceed (2 << 24 - 1) bytes!"
114 #endif
116 DEB( static int debugging = DEBUG; )
118 #define MAX_RETRIES 0
119 #define MAX_WRITE_RETRIES 0
120 #define MAX_READY_RETRIES 5
121 #define NO_TAPE NOT_READY
123 #define ST_TIMEOUT (900 * HZ)
124 #define ST_LONG_TIMEOUT (14000 * HZ)
126 #define TAPE_NR(x) (MINOR(x) & ~(128 | ST_MODE_MASK))
127 #define TAPE_MODE(x) ((MINOR(x) & ST_MODE_MASK) >> ST_MODE_SHIFT)
129 /* Internal ioctl to set both density (uppermost 8 bits) and blocksize (lower
130 24 bits) */
131 #define SET_DENS_AND_BLK 0x10001
133 #define ST_DEV_ARR_LUMP 6
134 static rwlock_t st_dev_arr_lock = RW_LOCK_UNLOCKED;
136 static int st_nbr_buffers;
137 static ST_buffer **st_buffers = NULL;
138 static int st_buffer_size = ST_BUFFER_SIZE;
139 static int st_write_threshold = ST_WRITE_THRESHOLD;
140 static int st_max_buffers = ST_MAX_BUFFERS;
141 static int st_max_sg_segs = ST_MAX_SG;
143 static Scsi_Tape **scsi_tapes = NULL;
145 static int modes_defined;
147 static ST_buffer *new_tape_buffer(int, int, int);
148 static int enlarge_buffer(ST_buffer *, int, int);
149 static void normalize_buffer(ST_buffer *);
150 static int append_to_buffer(const char *, ST_buffer *, int);
151 static int from_buffer(ST_buffer *, char *, int);
153 static int st_init(void);
154 static int st_attach(Scsi_Device *);
155 static int st_detect(Scsi_Device *);
156 static void st_detach(Scsi_Device *);
158 struct Scsi_Device_Template st_template =
160 name:"tape",
161 tag:"st",
162 scsi_type:TYPE_TAPE,
163 major:SCSI_TAPE_MAJOR,
164 detect:st_detect,
165 init:st_init,
166 attach:st_attach,
167 detach:st_detach
170 static int st_compression(Scsi_Tape *, int);
172 static int find_partition(Scsi_Tape *);
173 static int update_partition(Scsi_Tape *);
175 static int st_int_ioctl(Scsi_Tape *, unsigned int, unsigned long);
178 /* Convert the result to success code */
179 static int st_chk_result(Scsi_Tape *STp, Scsi_Request * SRpnt)
181 int dev;
182 int result = SRpnt->sr_result;
183 unsigned char *sense = SRpnt->sr_sense_buffer, scode;
184 DEB(const char *stp;)
186 if (!result) {
187 sense[0] = 0; /* We don't have sense data if this byte is zero */
188 return 0;
191 if (driver_byte(result) & DRIVER_SENSE)
192 scode = sense[2] & 0x0f;
193 else {
194 sense[0] = 0;
195 scode = 0;
198 dev = TAPE_NR(SRpnt->sr_request.rq_dev);
199 DEB(
200 if (debugging) {
201 printk(ST_DEB_MSG "st%d: Error: %x, cmd: %x %x %x %x %x %x Len: %d\n",
202 dev, result,
203 SRpnt->sr_cmnd[0], SRpnt->sr_cmnd[1], SRpnt->sr_cmnd[2],
204 SRpnt->sr_cmnd[3], SRpnt->sr_cmnd[4], SRpnt->sr_cmnd[5],
205 SRpnt->sr_bufflen);
206 if (driver_byte(result) & DRIVER_SENSE)
207 print_req_sense("st", SRpnt);
208 } else ) /* end DEB */
209 if (!(driver_byte(result) & DRIVER_SENSE) ||
210 ((sense[0] & 0x70) == 0x70 &&
211 scode != NO_SENSE &&
212 scode != RECOVERED_ERROR &&
213 /* scode != UNIT_ATTENTION && */
214 scode != BLANK_CHECK &&
215 scode != VOLUME_OVERFLOW &&
216 SRpnt->sr_cmnd[0] != MODE_SENSE &&
217 SRpnt->sr_cmnd[0] != TEST_UNIT_READY)) { /* Abnormal conditions for tape */
218 if (driver_byte(result) & DRIVER_SENSE) {
219 printk(KERN_WARNING "st%d: Error with sense data: ", dev);
220 print_req_sense("st", SRpnt);
221 } else
222 printk(KERN_WARNING
223 "st%d: Error %x (sugg. bt 0x%x, driver bt 0x%x, host bt 0x%x).\n",
224 dev, result, suggestion(result),
225 driver_byte(result) & DRIVER_MASK, host_byte(result));
228 if ((sense[0] & 0x70) == 0x70 &&
229 scode == RECOVERED_ERROR
230 #if ST_RECOVERED_WRITE_FATAL
231 && SRpnt->sr_cmnd[0] != WRITE_6
232 && SRpnt->sr_cmnd[0] != WRITE_FILEMARKS
233 #endif
235 STp->recover_count++;
236 STp->recover_reg++;
238 DEB(
239 if (debugging) {
240 if (SRpnt->sr_cmnd[0] == READ_6)
241 stp = "read";
242 else if (SRpnt->sr_cmnd[0] == WRITE_6)
243 stp = "write";
244 else
245 stp = "ioctl";
246 printk(ST_DEB_MSG "st%d: Recovered %s error (%d).\n", dev, stp,
247 STp->recover_count);
248 } ) /* end DEB */
250 if ((sense[2] & 0xe0) == 0)
251 return 0;
253 return (-EIO);
257 /* Wakeup from interrupt */
258 static void st_sleep_done(Scsi_Cmnd * SCpnt)
260 unsigned int st_nbr;
261 int remainder;
262 Scsi_Tape *STp;
264 if ((st_nbr = TAPE_NR(SCpnt->request.rq_dev)) < st_template.nr_dev) {
265 read_lock(&st_dev_arr_lock);
266 STp = scsi_tapes[st_nbr];
267 read_unlock(&st_dev_arr_lock);
268 if ((STp->buffer)->writing &&
269 (SCpnt->sense_buffer[0] & 0x70) == 0x70 &&
270 (SCpnt->sense_buffer[2] & 0x40)) {
271 /* EOM at write-behind, has all been written? */
272 if ((SCpnt->sense_buffer[0] & 0x80) != 0)
273 remainder = (SCpnt->sense_buffer[3] << 24) |
274 (SCpnt->sense_buffer[4] << 16) |
275 (SCpnt->sense_buffer[5] << 8) |
276 SCpnt->sense_buffer[6];
277 else
278 remainder = 0;
279 if ((SCpnt->sense_buffer[2] & 0x0f) == VOLUME_OVERFLOW ||
280 remainder > 0)
281 (STp->buffer)->midlevel_result = SCpnt->result; /* Error */
282 else
283 (STp->buffer)->midlevel_result = INT_MAX; /* OK */
284 } else
285 (STp->buffer)->midlevel_result = SCpnt->result;
286 SCpnt->request.rq_status = RQ_SCSI_DONE;
287 (STp->buffer)->last_SRpnt = SCpnt->sc_request;
288 DEB( STp->write_pending = 0; )
290 up(SCpnt->request.sem);
292 DEB(
293 else if (debugging)
294 printk(KERN_ERR "st?: Illegal interrupt device %x\n", st_nbr);
295 ) /* end DEB */
299 /* Do the scsi command. Waits until command performed if do_wait is true.
300 Otherwise write_behind_check() is used to check that the command
301 has finished. */
302 static Scsi_Request *
303 st_do_scsi(Scsi_Request * SRpnt, Scsi_Tape * STp, unsigned char *cmd, int bytes,
304 int direction, int timeout, int retries, int do_wait)
306 unsigned char *bp;
308 if (SRpnt == NULL) {
309 SRpnt = scsi_allocate_request(STp->device);
310 if (SRpnt == NULL) {
311 DEBC( printk(KERN_ERR "st%d: Can't get SCSI request.\n",
312 TAPE_NR(STp->devt)); );
313 if (signal_pending(current))
314 (STp->buffer)->syscall_result = (-EINTR);
315 else
316 (STp->buffer)->syscall_result = (-EBUSY);
317 return NULL;
321 cmd[1] |= (SRpnt->sr_device->lun << 5) & 0xe0;
322 init_MUTEX_LOCKED(&STp->sem);
323 SRpnt->sr_use_sg = (bytes > (STp->buffer)->sg[0].length) ?
324 (STp->buffer)->use_sg : 0;
325 if (SRpnt->sr_use_sg) {
326 bp = (char *) &((STp->buffer)->sg[0]);
327 if ((STp->buffer)->sg_segs < SRpnt->sr_use_sg)
328 SRpnt->sr_use_sg = (STp->buffer)->sg_segs;
329 } else
330 bp = (STp->buffer)->b_data;
331 SRpnt->sr_data_direction = direction;
332 SRpnt->sr_cmd_len = 0;
333 SRpnt->sr_request.sem = &(STp->sem);
334 SRpnt->sr_request.rq_status = RQ_SCSI_BUSY;
335 SRpnt->sr_request.rq_dev = STp->devt;
337 scsi_do_req(SRpnt, (void *) cmd, bp, bytes,
338 st_sleep_done, timeout, retries);
340 if (do_wait) {
341 down(SRpnt->sr_request.sem);
342 SRpnt->sr_request.sem = NULL;
343 (STp->buffer)->syscall_result = st_chk_result(STp, SRpnt);
345 return SRpnt;
349 /* Handle the write-behind checking (downs the semaphore) */
350 static void write_behind_check(Scsi_Tape * STp)
352 ST_buffer *STbuffer;
353 ST_partstat *STps;
355 STbuffer = STp->buffer;
357 DEB(
358 if (STp->write_pending)
359 STp->nbr_waits++;
360 else
361 STp->nbr_finished++;
362 ) /* end DEB */
364 down(&(STp->sem));
365 (STp->buffer)->last_SRpnt->sr_request.sem = NULL;
367 (STp->buffer)->syscall_result = st_chk_result(STp, (STp->buffer)->last_SRpnt);
368 scsi_release_request((STp->buffer)->last_SRpnt);
370 if (STbuffer->writing < STbuffer->buffer_bytes)
371 #if 0
372 memcpy(STbuffer->b_data,
373 STbuffer->b_data + STbuffer->writing,
374 STbuffer->buffer_bytes - STbuffer->writing);
375 #else
376 printk(KERN_WARNING
377 "st: write_behind_check: something left in buffer!\n");
378 #endif
379 STbuffer->buffer_bytes -= STbuffer->writing;
380 STps = &(STp->ps[STp->partition]);
381 if (STps->drv_block >= 0) {
382 if (STp->block_size == 0)
383 STps->drv_block++;
384 else
385 STps->drv_block += STbuffer->writing / STp->block_size;
387 STbuffer->writing = 0;
389 return;
393 /* Step over EOF if it has been inadvertently crossed (ioctl not used because
394 it messes up the block number). */
395 static int cross_eof(Scsi_Tape * STp, int forward)
397 Scsi_Request *SRpnt;
398 unsigned char cmd[MAX_COMMAND_SIZE];
400 cmd[0] = SPACE;
401 cmd[1] = 0x01; /* Space FileMarks */
402 if (forward) {
403 cmd[2] = cmd[3] = 0;
404 cmd[4] = 1;
405 } else
406 cmd[2] = cmd[3] = cmd[4] = 0xff; /* -1 filemarks */
407 cmd[5] = 0;
409 DEBC(printk(ST_DEB_MSG "st%d: Stepping over filemark %s.\n",
410 TAPE_NR(STp->devt), forward ? "forward" : "backward"));
412 SRpnt = st_do_scsi(NULL, STp, cmd, 0, SCSI_DATA_NONE,
413 STp->timeout, MAX_RETRIES, TRUE);
414 if (!SRpnt)
415 return (STp->buffer)->syscall_result;
417 scsi_release_request(SRpnt);
418 SRpnt = NULL;
420 if ((STp->buffer)->midlevel_result != 0)
421 printk(KERN_ERR "st%d: Stepping over filemark %s failed.\n",
422 TAPE_NR(STp->devt), forward ? "forward" : "backward");
424 return (STp->buffer)->syscall_result;
428 /* Flush the write buffer (never need to write if variable blocksize). */
429 static int flush_write_buffer(Scsi_Tape * STp)
431 int offset, transfer, blks;
432 int result;
433 unsigned char cmd[MAX_COMMAND_SIZE];
434 Scsi_Request *SRpnt;
435 ST_partstat *STps;
437 if ((STp->buffer)->writing) {
438 write_behind_check(STp);
439 if ((STp->buffer)->syscall_result) {
440 DEBC(printk(ST_DEB_MSG
441 "st%d: Async write error (flush) %x.\n",
442 TAPE_NR(STp->devt), (STp->buffer)->midlevel_result))
443 if ((STp->buffer)->midlevel_result == INT_MAX)
444 return (-ENOSPC);
445 return (-EIO);
448 if (STp->block_size == 0)
449 return 0;
451 result = 0;
452 if (STp->dirty == 1) {
454 offset = (STp->buffer)->buffer_bytes;
455 transfer = ((offset + STp->block_size - 1) /
456 STp->block_size) * STp->block_size;
457 DEBC(printk(ST_DEB_MSG "st%d: Flushing %d bytes.\n",
458 TAPE_NR(STp->devt), transfer));
460 memset((STp->buffer)->b_data + offset, 0, transfer - offset);
462 memset(cmd, 0, MAX_COMMAND_SIZE);
463 cmd[0] = WRITE_6;
464 cmd[1] = 1;
465 blks = transfer / STp->block_size;
466 cmd[2] = blks >> 16;
467 cmd[3] = blks >> 8;
468 cmd[4] = blks;
470 SRpnt = st_do_scsi(NULL, STp, cmd, transfer, SCSI_DATA_WRITE,
471 STp->timeout, MAX_WRITE_RETRIES, TRUE);
472 if (!SRpnt)
473 return (STp->buffer)->syscall_result;
475 STps = &(STp->ps[STp->partition]);
476 if ((STp->buffer)->syscall_result != 0) {
477 if ((SRpnt->sr_sense_buffer[0] & 0x70) == 0x70 &&
478 (SRpnt->sr_sense_buffer[2] & 0x40) &&
479 (SRpnt->sr_sense_buffer[2] & 0x0f) == NO_SENSE) {
480 STp->dirty = 0;
481 (STp->buffer)->buffer_bytes = 0;
482 result = (-ENOSPC);
483 } else {
484 printk(KERN_ERR "st%d: Error on flush.\n",
485 TAPE_NR(STp->devt));
486 result = (-EIO);
488 STps->drv_block = (-1);
489 } else {
490 if (STps->drv_block >= 0)
491 STps->drv_block += blks;
492 STp->dirty = 0;
493 (STp->buffer)->buffer_bytes = 0;
495 scsi_release_request(SRpnt);
496 SRpnt = NULL;
498 return result;
502 /* Flush the tape buffer. The tape will be positioned correctly unless
503 seek_next is true. */
504 static int flush_buffer(Scsi_Tape *STp, int seek_next)
506 int backspace, result;
507 ST_buffer *STbuffer;
508 ST_partstat *STps;
510 STbuffer = STp->buffer;
513 * If there was a bus reset, block further access
514 * to this device.
516 if (STp->device->was_reset)
517 return (-EIO);
519 if (STp->ready != ST_READY)
520 return 0;
522 STps = &(STp->ps[STp->partition]);
523 if (STps->rw == ST_WRITING) /* Writing */
524 return flush_write_buffer(STp);
526 if (STp->block_size == 0)
527 return 0;
529 backspace = ((STp->buffer)->buffer_bytes +
530 (STp->buffer)->read_pointer) / STp->block_size -
531 ((STp->buffer)->read_pointer + STp->block_size - 1) /
532 STp->block_size;
533 (STp->buffer)->buffer_bytes = 0;
534 (STp->buffer)->read_pointer = 0;
535 result = 0;
536 if (!seek_next) {
537 if (STps->eof == ST_FM_HIT) {
538 result = cross_eof(STp, FALSE); /* Back over the EOF hit */
539 if (!result)
540 STps->eof = ST_NOEOF;
541 else {
542 if (STps->drv_file >= 0)
543 STps->drv_file++;
544 STps->drv_block = 0;
547 if (!result && backspace > 0)
548 result = st_int_ioctl(STp, MTBSR, backspace);
549 } else if (STps->eof == ST_FM_HIT) {
550 if (STps->drv_file >= 0)
551 STps->drv_file++;
552 STps->drv_block = 0;
553 STps->eof = ST_NOEOF;
555 return result;
559 /* Set the mode parameters */
560 static int set_mode_densblk(Scsi_Tape * STp, ST_mode * STm)
562 int set_it = FALSE;
563 unsigned long arg;
564 int dev = TAPE_NR(STp->devt);
566 if (!STp->density_changed &&
567 STm->default_density >= 0 &&
568 STm->default_density != STp->density) {
569 arg = STm->default_density;
570 set_it = TRUE;
571 } else
572 arg = STp->density;
573 arg <<= MT_ST_DENSITY_SHIFT;
574 if (!STp->blksize_changed &&
575 STm->default_blksize >= 0 &&
576 STm->default_blksize != STp->block_size) {
577 arg |= STm->default_blksize;
578 set_it = TRUE;
579 } else
580 arg |= STp->block_size;
581 if (set_it &&
582 st_int_ioctl(STp, SET_DENS_AND_BLK, arg)) {
583 printk(KERN_WARNING
584 "st%d: Can't set default block size to %d bytes and density %x.\n",
585 dev, STm->default_blksize, STm->default_density);
586 if (modes_defined)
587 return (-EINVAL);
589 return 0;
593 /* Open the device. Needs to be called with BKL only because of incrementing the SCSI host
594 module count. */
595 static int st_open(struct inode *inode, struct file *filp)
597 unsigned short st_flags;
598 int i, need_dma_buffer, new_session = FALSE;
599 int retval;
600 unsigned char cmd[MAX_COMMAND_SIZE];
601 Scsi_Request *SRpnt;
602 Scsi_Tape *STp;
603 ST_mode *STm;
604 ST_partstat *STps;
605 int dev = TAPE_NR(inode->i_rdev);
606 int mode = TAPE_MODE(inode->i_rdev);
607 unsigned long flags;
609 write_lock_irqsave(&st_dev_arr_lock, flags);
610 STp = scsi_tapes[dev];
611 if (dev >= st_template.dev_max || STp == NULL) {
612 write_unlock_irqrestore(&st_dev_arr_lock, flags);
613 return (-ENXIO);
616 if (STp->in_use) {
617 write_unlock_irqrestore(&st_dev_arr_lock, flags);
618 DEB( printk(ST_DEB_MSG "st%d: Device already in use.\n", dev); )
619 return (-EBUSY);
621 STp->in_use = 1;
622 write_unlock_irqrestore(&st_dev_arr_lock, flags);
623 STp->rew_at_close = STp->autorew_dev = (MINOR(inode->i_rdev) & 0x80) == 0;
625 if (STp->device->host->hostt->module)
626 __MOD_INC_USE_COUNT(STp->device->host->hostt->module);
628 if (!scsi_block_when_processing_errors(STp->device)) {
629 retval = (-ENXIO);
630 goto err_out;
633 if (mode != STp->current_mode) {
634 DEBC(printk(ST_DEB_MSG "st%d: Mode change from %d to %d.\n",
635 dev, STp->current_mode, mode));
636 new_session = TRUE;
637 STp->current_mode = mode;
639 STm = &(STp->modes[STp->current_mode]);
641 /* Allocate a buffer for this user */
642 need_dma_buffer = STp->restr_dma;
643 write_lock_irqsave(&st_dev_arr_lock, flags);
644 for (i = 0; i < st_nbr_buffers; i++)
645 if (!st_buffers[i]->in_use &&
646 (!need_dma_buffer || st_buffers[i]->dma)) {
647 STp->buffer = st_buffers[i];
648 (STp->buffer)->in_use = 1;
649 break;
651 write_unlock_irqrestore(&st_dev_arr_lock, flags);
652 if (i >= st_nbr_buffers) {
653 STp->buffer = new_tape_buffer(FALSE, need_dma_buffer, TRUE);
654 if (STp->buffer == NULL) {
655 printk(KERN_WARNING "st%d: Can't allocate tape buffer.\n", dev);
656 retval = (-EBUSY);
657 goto err_out;
661 (STp->buffer)->writing = 0;
662 (STp->buffer)->syscall_result = 0;
663 (STp->buffer)->use_sg = STp->device->host->sg_tablesize;
665 /* Compute the usable buffer size for this SCSI adapter */
666 if (!(STp->buffer)->use_sg)
667 (STp->buffer)->buffer_size = (STp->buffer)->sg[0].length;
668 else {
669 for (i = 0, (STp->buffer)->buffer_size = 0; i < (STp->buffer)->use_sg &&
670 i < (STp->buffer)->sg_segs; i++)
671 (STp->buffer)->buffer_size += (STp->buffer)->sg[i].length;
674 st_flags = filp->f_flags;
675 STp->write_prot = ((st_flags & O_ACCMODE) == O_RDONLY);
677 STp->dirty = 0;
678 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
679 STps = &(STp->ps[i]);
680 STps->rw = ST_IDLE;
682 STp->ready = ST_READY;
683 STp->recover_count = 0;
684 DEB( STp->nbr_waits = STp->nbr_finished = 0; )
686 memset((void *) &cmd[0], 0, MAX_COMMAND_SIZE);
687 cmd[0] = TEST_UNIT_READY;
689 SRpnt = st_do_scsi(NULL, STp, cmd, 0, SCSI_DATA_NONE, STp->long_timeout,
690 MAX_READY_RETRIES, TRUE);
691 if (!SRpnt) {
692 retval = (STp->buffer)->syscall_result;
693 goto err_out;
696 if ((SRpnt->sr_sense_buffer[0] & 0x70) == 0x70 &&
697 (SRpnt->sr_sense_buffer[2] & 0x0f) == UNIT_ATTENTION) { /* New media? */
699 /* Flush the queued UNIT ATTENTION sense data */
700 for (i=0; i < 10; i++) {
701 memset((void *) &cmd[0], 0, MAX_COMMAND_SIZE);
702 cmd[0] = TEST_UNIT_READY;
703 SRpnt = st_do_scsi(SRpnt, STp, cmd, 0, SCSI_DATA_NONE,
704 STp->long_timeout, MAX_READY_RETRIES, TRUE);
705 if ((SRpnt->sr_sense_buffer[0] & 0x70) != 0x70 ||
706 (SRpnt->sr_sense_buffer[2] & 0x0f) != UNIT_ATTENTION)
707 break;
710 (STp->device)->was_reset = 0;
711 STp->partition = STp->new_partition = 0;
712 if (STp->can_partitions)
713 STp->nbr_partitions = 1; /* This guess will be updated later
714 if necessary */
715 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
716 STps = &(STp->ps[i]);
717 STps->rw = ST_IDLE;
718 STps->eof = ST_NOEOF;
719 STps->at_sm = 0;
720 STps->last_block_valid = FALSE;
721 STps->drv_block = 0;
722 STps->drv_file = 0;
724 new_session = TRUE;
727 if ((STp->buffer)->syscall_result != 0) {
728 if ((STp->device)->scsi_level >= SCSI_2 &&
729 (SRpnt->sr_sense_buffer[0] & 0x70) == 0x70 &&
730 (SRpnt->sr_sense_buffer[2] & 0x0f) == NOT_READY &&
731 SRpnt->sr_sense_buffer[12] == 0x3a) { /* Check ASC */
732 STp->ready = ST_NO_TAPE;
733 } else
734 STp->ready = ST_NOT_READY;
735 scsi_release_request(SRpnt);
736 SRpnt = NULL;
737 STp->density = 0; /* Clear the erroneous "residue" */
738 STp->write_prot = 0;
739 STp->block_size = 0;
740 STp->ps[0].drv_file = STp->ps[0].drv_block = (-1);
741 STp->partition = STp->new_partition = 0;
742 STp->door_locked = ST_UNLOCKED;
743 return 0;
746 if (STp->omit_blklims)
747 STp->min_block = STp->max_block = (-1);
748 else {
749 memset((void *) &cmd[0], 0, MAX_COMMAND_SIZE);
750 cmd[0] = READ_BLOCK_LIMITS;
752 SRpnt = st_do_scsi(SRpnt, STp, cmd, 6, SCSI_DATA_READ, STp->timeout,
753 MAX_READY_RETRIES, TRUE);
755 if (!SRpnt->sr_result && !SRpnt->sr_sense_buffer[0]) {
756 STp->max_block = ((STp->buffer)->b_data[1] << 16) |
757 ((STp->buffer)->b_data[2] << 8) | (STp->buffer)->b_data[3];
758 STp->min_block = ((STp->buffer)->b_data[4] << 8) |
759 (STp->buffer)->b_data[5];
760 if ( DEB( debugging || ) !STp->inited)
761 printk(KERN_WARNING
762 "st%d: Block limits %d - %d bytes.\n", dev,
763 STp->min_block, STp->max_block);
764 } else {
765 STp->min_block = STp->max_block = (-1);
766 DEBC(printk(ST_DEB_MSG "st%d: Can't read block limits.\n",
767 dev));
771 memset((void *) &cmd[0], 0, MAX_COMMAND_SIZE);
772 cmd[0] = MODE_SENSE;
773 cmd[4] = 12;
775 SRpnt = st_do_scsi(SRpnt, STp, cmd, 12, SCSI_DATA_READ, STp->timeout,
776 MAX_READY_RETRIES, TRUE);
778 if ((STp->buffer)->syscall_result != 0) {
779 DEBC(printk(ST_DEB_MSG "st%d: No Mode Sense.\n", dev));
780 STp->block_size = ST_DEFAULT_BLOCK; /* Educated guess (?) */
781 (STp->buffer)->syscall_result = 0; /* Prevent error propagation */
782 STp->drv_write_prot = 0;
783 } else {
784 DEBC(printk(ST_DEB_MSG
785 "st%d: Mode sense. Length %d, medium %x, WBS %x, BLL %d\n",
786 dev,
787 (STp->buffer)->b_data[0], (STp->buffer)->b_data[1],
788 (STp->buffer)->b_data[2], (STp->buffer)->b_data[3]));
790 if ((STp->buffer)->b_data[3] >= 8) {
791 STp->drv_buffer = ((STp->buffer)->b_data[2] >> 4) & 7;
792 STp->density = (STp->buffer)->b_data[4];
793 STp->block_size = (STp->buffer)->b_data[9] * 65536 +
794 (STp->buffer)->b_data[10] * 256 + (STp->buffer)->b_data[11];
795 DEBC(printk(ST_DEB_MSG
796 "st%d: Density %x, tape length: %x, drv buffer: %d\n",
797 dev, STp->density, (STp->buffer)->b_data[5] * 65536 +
798 (STp->buffer)->b_data[6] * 256 + (STp->buffer)->b_data[7],
799 STp->drv_buffer));
801 STp->drv_write_prot = ((STp->buffer)->b_data[2] & 0x80) != 0;
803 scsi_release_request(SRpnt);
804 SRpnt = NULL;
805 STp->inited = TRUE;
807 if (STp->block_size > 0)
808 (STp->buffer)->buffer_blocks =
809 (STp->buffer)->buffer_size / STp->block_size;
810 else
811 (STp->buffer)->buffer_blocks = 1;
812 (STp->buffer)->buffer_bytes = (STp->buffer)->read_pointer = 0;
814 DEBC(printk(ST_DEB_MSG
815 "st%d: Block size: %d, buffer size: %d (%d blocks).\n", dev,
816 STp->block_size, (STp->buffer)->buffer_size,
817 (STp->buffer)->buffer_blocks));
819 if (STp->drv_write_prot) {
820 STp->write_prot = 1;
822 DEBC(printk(ST_DEB_MSG "st%d: Write protected\n", dev));
824 if ((st_flags & O_ACCMODE) == O_WRONLY || (st_flags & O_ACCMODE) == O_RDWR) {
825 retval = (-EROFS);
826 goto err_out;
830 if (STp->can_partitions && STp->nbr_partitions < 1) {
831 /* This code is reached when the device is opened for the first time
832 after the driver has been initialized with tape in the drive and the
833 partition support has been enabled. */
834 DEBC(printk(ST_DEB_MSG
835 "st%d: Updating partition number in status.\n", dev));
836 if ((STp->partition = find_partition(STp)) < 0) {
837 retval = STp->partition;
838 goto err_out;
840 STp->new_partition = STp->partition;
841 STp->nbr_partitions = 1; /* This guess will be updated when necessary */
844 if (new_session) { /* Change the drive parameters for the new mode */
845 STp->density_changed = STp->blksize_changed = FALSE;
846 STp->compression_changed = FALSE;
847 if (!(STm->defaults_for_writes) &&
848 (retval = set_mode_densblk(STp, STm)) < 0)
849 goto err_out;
851 if (STp->default_drvbuffer != 0xff) {
852 if (st_int_ioctl(STp, MTSETDRVBUFFER, STp->default_drvbuffer))
853 printk(KERN_WARNING
854 "st%d: Can't set default drive buffering to %d.\n",
855 dev, STp->default_drvbuffer);
859 return 0;
861 err_out:
862 if (STp->buffer != NULL) {
863 (STp->buffer)->in_use = 0;
864 STp->buffer = NULL;
866 STp->in_use = 0;
867 if (STp->device->host->hostt->module)
868 __MOD_DEC_USE_COUNT(STp->device->host->hostt->module);
869 return retval;
874 /* Flush the tape buffer before close */
875 static int st_flush(struct file *filp)
877 int result = 0, result2;
878 unsigned char cmd[MAX_COMMAND_SIZE];
879 Scsi_Request *SRpnt;
880 Scsi_Tape *STp;
881 ST_mode *STm;
882 ST_partstat *STps;
884 struct inode *inode = filp->f_dentry->d_inode;
885 kdev_t devt = inode->i_rdev;
886 int dev;
888 if (file_count(filp) > 1)
889 return 0;
891 dev = TAPE_NR(devt);
892 read_lock(&st_dev_arr_lock);
893 STp = scsi_tapes[dev];
894 read_unlock(&st_dev_arr_lock);
895 STm = &(STp->modes[STp->current_mode]);
896 STps = &(STp->ps[STp->partition]);
898 if (STps->rw == ST_WRITING && !(STp->device)->was_reset) {
899 result = flush_write_buffer(STp);
900 if (result != 0 && result != (-ENOSPC))
901 goto out;
904 if (STp->can_partitions &&
905 (result2 = update_partition(STp)) < 0) {
906 DEBC(printk(ST_DEB_MSG
907 "st%d: update_partition at close failed.\n", dev));
908 if (result == 0)
909 result = result2;
910 goto out;
913 if (STps->rw == ST_WRITING && !(STp->device)->was_reset) {
915 DEBC(printk(ST_DEB_MSG "st%d: File length %ld bytes.\n",
916 dev, (long) (filp->f_pos));
917 printk(ST_DEB_MSG "st%d: Async write waits %d, finished %d.\n",
918 dev, STp->nbr_waits, STp->nbr_finished);
921 memset(cmd, 0, MAX_COMMAND_SIZE);
922 cmd[0] = WRITE_FILEMARKS;
923 cmd[4] = 1 + STp->two_fm;
925 SRpnt = st_do_scsi(NULL, STp, cmd, 0, SCSI_DATA_NONE,
926 STp->timeout, MAX_WRITE_RETRIES, TRUE);
927 if (!SRpnt) {
928 result = (STp->buffer)->syscall_result;
929 goto out;
932 if ((STp->buffer)->syscall_result != 0 &&
933 ((SRpnt->sr_sense_buffer[0] & 0x70) != 0x70 ||
934 (SRpnt->sr_sense_buffer[2] & 0x4f) != 0x40 ||
935 ((SRpnt->sr_sense_buffer[0] & 0x80) != 0 &&
936 (SRpnt->sr_sense_buffer[3] | SRpnt->sr_sense_buffer[4] |
937 SRpnt->sr_sense_buffer[5] |
938 SRpnt->sr_sense_buffer[6]) == 0))) {
939 /* Filter out successful write at EOM */
940 scsi_release_request(SRpnt);
941 SRpnt = NULL;
942 printk(KERN_ERR "st%d: Error on write filemark.\n", dev);
943 if (result == 0)
944 result = (-EIO);
945 } else {
946 scsi_release_request(SRpnt);
947 SRpnt = NULL;
948 if (STps->drv_file >= 0)
949 STps->drv_file++;
950 STps->drv_block = 0;
951 if (STp->two_fm)
952 cross_eof(STp, FALSE);
953 STps->eof = ST_FM;
956 DEBC(printk(ST_DEB_MSG "st%d: Buffer flushed, %d EOF(s) written\n",
957 dev, cmd[4]));
958 } else if (!STp->rew_at_close) {
959 STps = &(STp->ps[STp->partition]);
960 if (!STm->sysv || STps->rw != ST_READING) {
961 if (STp->can_bsr)
962 result = flush_buffer(STp, 0);
963 else if (STps->eof == ST_FM_HIT) {
964 result = cross_eof(STp, FALSE);
965 if (result) {
966 if (STps->drv_file >= 0)
967 STps->drv_file++;
968 STps->drv_block = 0;
969 STps->eof = ST_FM;
970 } else
971 STps->eof = ST_NOEOF;
973 } else if ((STps->eof == ST_NOEOF &&
974 !(result = cross_eof(STp, TRUE))) ||
975 STps->eof == ST_FM_HIT) {
976 if (STps->drv_file >= 0)
977 STps->drv_file++;
978 STps->drv_block = 0;
979 STps->eof = ST_FM;
983 out:
984 if (STp->rew_at_close) {
985 result2 = st_int_ioctl(STp, MTREW, 1);
986 if (result == 0)
987 result = result2;
989 return result;
993 /* Close the device and release it. BKL is not needed: this is the only thread
994 accessing this tape. */
995 static int st_release(struct inode *inode, struct file *filp)
997 int result = 0;
998 Scsi_Tape *STp;
999 unsigned long flags;
1001 kdev_t devt = inode->i_rdev;
1002 int dev;
1004 dev = TAPE_NR(devt);
1005 read_lock(&st_dev_arr_lock);
1006 STp = scsi_tapes[dev];
1007 read_unlock(&st_dev_arr_lock);
1009 if (STp->door_locked == ST_LOCKED_AUTO)
1010 st_int_ioctl(STp, MTUNLOCK, 0);
1012 if (STp->buffer != NULL) {
1013 normalize_buffer(STp->buffer);
1014 write_lock_irqsave(&st_dev_arr_lock, flags);
1015 (STp->buffer)->in_use = 0;
1016 STp->buffer = NULL;
1018 else {
1019 write_lock_irqsave(&st_dev_arr_lock, flags);
1022 STp->in_use = 0;
1023 write_unlock_irqrestore(&st_dev_arr_lock, flags);
1024 if (STp->device->host->hostt->module)
1025 __MOD_DEC_USE_COUNT(STp->device->host->hostt->module);
1027 return result;
1031 /* Write command */
1032 static ssize_t
1033 st_write(struct file *filp, const char *buf, size_t count, loff_t * ppos)
1035 struct inode *inode = filp->f_dentry->d_inode;
1036 ssize_t total;
1037 ssize_t i, do_count, blks, transfer;
1038 ssize_t retval = 0;
1039 int write_threshold;
1040 int doing_write = 0;
1041 unsigned char cmd[MAX_COMMAND_SIZE];
1042 const char *b_point;
1043 Scsi_Request *SRpnt = NULL;
1044 Scsi_Tape *STp;
1045 ST_mode *STm;
1046 ST_partstat *STps;
1047 int dev = TAPE_NR(inode->i_rdev);
1049 read_lock(&st_dev_arr_lock);
1050 STp = scsi_tapes[dev];
1051 read_unlock(&st_dev_arr_lock);
1053 if (down_interruptible(&STp->lock))
1054 return -ERESTARTSYS;
1057 * If we are in the middle of error recovery, don't let anyone
1058 * else try and use this device. Also, if error recovery fails, it
1059 * may try and take the device offline, in which case all further
1060 * access to the device is prohibited.
1062 if (!scsi_block_when_processing_errors(STp->device)) {
1063 retval = (-ENXIO);
1064 goto out;
1067 if (ppos != &filp->f_pos) {
1068 /* "A request was outside the capabilities of the device." */
1069 retval = (-ENXIO);
1070 goto out;
1073 if (STp->ready != ST_READY) {
1074 if (STp->ready == ST_NO_TAPE)
1075 retval = (-ENOMEDIUM);
1076 else
1077 retval = (-EIO);
1078 goto out;
1081 STm = &(STp->modes[STp->current_mode]);
1082 if (!STm->defined) {
1083 retval = (-ENXIO);
1084 goto out;
1086 if (count == 0)
1087 goto out;
1090 * If there was a bus reset, block further access
1091 * to this device.
1093 if (STp->device->was_reset) {
1094 retval = (-EIO);
1095 goto out;
1098 DEB(
1099 if (!STp->in_use) {
1100 printk(ST_DEB_MSG "st%d: Incorrect device.\n", dev);
1101 retval = (-EIO);
1102 goto out;
1103 } ) /* end DEB */
1105 /* Write must be integral number of blocks */
1106 if (STp->block_size != 0 && (count % STp->block_size) != 0) {
1107 printk(KERN_WARNING "st%d: Write not multiple of tape block size.\n",
1108 dev);
1109 retval = (-EINVAL);
1110 goto out;
1113 if (STp->can_partitions &&
1114 (retval = update_partition(STp)) < 0)
1115 goto out;
1116 STps = &(STp->ps[STp->partition]);
1118 if (STp->write_prot) {
1119 retval = (-EACCES);
1120 goto out;
1123 if (STp->block_size == 0) {
1124 if (STp->max_block > 0 &&
1125 (count < STp->min_block || count > STp->max_block)) {
1126 retval = (-EINVAL);
1127 goto out;
1129 if (count > (STp->buffer)->buffer_size &&
1130 !enlarge_buffer(STp->buffer, count, STp->restr_dma)) {
1131 retval = (-EOVERFLOW);
1132 goto out;
1135 if ((STp->buffer)->buffer_blocks < 1) {
1136 /* Fixed block mode with too small buffer */
1137 if (!enlarge_buffer(STp->buffer, STp->block_size, STp->restr_dma)) {
1138 retval = (-EOVERFLOW);
1139 goto out;
1141 (STp->buffer)->buffer_blocks = 1;
1144 if (STp->do_auto_lock && STp->door_locked == ST_UNLOCKED &&
1145 !st_int_ioctl(STp, MTLOCK, 0))
1146 STp->door_locked = ST_LOCKED_AUTO;
1148 if (STps->rw == ST_READING) {
1149 retval = flush_buffer(STp, 0);
1150 if (retval)
1151 goto out;
1152 STps->rw = ST_WRITING;
1153 } else if (STps->rw != ST_WRITING &&
1154 STps->drv_file == 0 && STps->drv_block == 0) {
1155 if ((retval = set_mode_densblk(STp, STm)) < 0)
1156 goto out;
1157 if (STm->default_compression != ST_DONT_TOUCH &&
1158 !(STp->compression_changed)) {
1159 if (st_compression(STp, (STm->default_compression == ST_YES))) {
1160 printk(KERN_WARNING "st%d: Can't set default compression.\n",
1161 dev);
1162 if (modes_defined) {
1163 retval = (-EINVAL);
1164 goto out;
1170 if ((STp->buffer)->writing) {
1171 write_behind_check(STp);
1172 if ((STp->buffer)->syscall_result) {
1173 DEBC(printk(ST_DEB_MSG "st%d: Async write error (write) %x.\n",
1174 dev, (STp->buffer)->midlevel_result));
1175 if ((STp->buffer)->midlevel_result == INT_MAX)
1176 STps->eof = ST_EOM_OK;
1177 else
1178 STps->eof = ST_EOM_ERROR;
1182 if (STps->eof == ST_EOM_OK) {
1183 retval = (-ENOSPC);
1184 goto out;
1186 else if (STps->eof == ST_EOM_ERROR) {
1187 retval = (-EIO);
1188 goto out;
1191 /* Check the buffer readability in cases where copy_user might catch
1192 the problems after some tape movement. */
1193 if (STp->block_size != 0 &&
1194 (copy_from_user(&i, buf, 1) != 0 ||
1195 copy_from_user(&i, buf + count - 1, 1) != 0)) {
1196 retval = (-EFAULT);
1197 goto out;
1200 if (!STm->do_buffer_writes) {
1201 #if 0
1202 if (STp->block_size != 0 && (count % STp->block_size) != 0) {
1203 retval = (-EINVAL); /* Write must be integral number of blocks */
1204 goto out;
1206 #endif
1207 write_threshold = 1;
1208 } else
1209 write_threshold = (STp->buffer)->buffer_blocks * STp->block_size;
1210 if (!STm->do_async_writes)
1211 write_threshold--;
1213 total = count;
1215 memset(cmd, 0, MAX_COMMAND_SIZE);
1216 cmd[0] = WRITE_6;
1217 cmd[1] = (STp->block_size != 0);
1219 STps->rw = ST_WRITING;
1221 b_point = buf;
1222 while ((STp->block_size == 0 && !STm->do_async_writes && count > 0) ||
1223 (STp->block_size != 0 &&
1224 (STp->buffer)->buffer_bytes + count > write_threshold)) {
1225 doing_write = 1;
1226 if (STp->block_size == 0)
1227 do_count = count;
1228 else {
1229 do_count = (STp->buffer)->buffer_blocks * STp->block_size -
1230 (STp->buffer)->buffer_bytes;
1231 if (do_count > count)
1232 do_count = count;
1235 i = append_to_buffer(b_point, STp->buffer, do_count);
1236 if (i) {
1237 retval = i;
1238 goto out;
1241 if (STp->block_size == 0)
1242 blks = transfer = do_count;
1243 else {
1244 blks = (STp->buffer)->buffer_bytes /
1245 STp->block_size;
1246 transfer = blks * STp->block_size;
1248 cmd[2] = blks >> 16;
1249 cmd[3] = blks >> 8;
1250 cmd[4] = blks;
1252 SRpnt = st_do_scsi(SRpnt, STp, cmd, transfer, SCSI_DATA_WRITE,
1253 STp->timeout, MAX_WRITE_RETRIES, TRUE);
1254 if (!SRpnt) {
1255 retval = (STp->buffer)->syscall_result;
1256 goto out;
1259 if ((STp->buffer)->syscall_result != 0) {
1260 DEBC(printk(ST_DEB_MSG "st%d: Error on write:\n", dev));
1261 if ((SRpnt->sr_sense_buffer[0] & 0x70) == 0x70 &&
1262 (SRpnt->sr_sense_buffer[2] & 0x40)) {
1263 if (STp->block_size != 0 &&
1264 (SRpnt->sr_sense_buffer[0] & 0x80) != 0)
1265 transfer = (SRpnt->sr_sense_buffer[3] << 24) |
1266 (SRpnt->sr_sense_buffer[4] << 16) |
1267 (SRpnt->sr_sense_buffer[5] << 8) |
1268 SRpnt->sr_sense_buffer[6];
1269 else if (STp->block_size == 0 &&
1270 (SRpnt->sr_sense_buffer[2] & 0x0f) ==
1271 VOLUME_OVERFLOW)
1272 transfer = do_count;
1273 else
1274 transfer = 0;
1275 if (STp->block_size != 0)
1276 transfer *= STp->block_size;
1277 if (transfer <= do_count) {
1278 filp->f_pos += do_count - transfer;
1279 count -= do_count - transfer;
1280 if (STps->drv_block >= 0) {
1281 if (STp->block_size == 0 &&
1282 transfer < do_count)
1283 STps->drv_block++;
1284 else if (STp->block_size != 0)
1285 STps->drv_block +=
1286 (do_count - transfer) /
1287 STp->block_size;
1289 STps->eof = ST_EOM_OK;
1290 retval = (-ENOSPC); /* EOM within current request */
1291 DEBC(printk(ST_DEB_MSG
1292 "st%d: EOM with %d bytes unwritten.\n",
1293 dev, transfer));
1294 } else {
1295 STps->eof = ST_EOM_ERROR;
1296 STps->drv_block = (-1); /* Too cautious? */
1297 retval = (-EIO); /* EOM for old data */
1298 DEBC(printk(ST_DEB_MSG
1299 "st%d: EOM with lost data.\n",
1300 dev));
1302 } else {
1303 STps->drv_block = (-1); /* Too cautious? */
1304 retval = (-EIO);
1307 scsi_release_request(SRpnt);
1308 SRpnt = NULL;
1309 (STp->buffer)->buffer_bytes = 0;
1310 STp->dirty = 0;
1311 if (count < total)
1312 retval = total - count;
1313 goto out;
1315 filp->f_pos += do_count;
1316 b_point += do_count;
1317 count -= do_count;
1318 if (STps->drv_block >= 0) {
1319 if (STp->block_size == 0)
1320 STps->drv_block++;
1321 else
1322 STps->drv_block += blks;
1324 (STp->buffer)->buffer_bytes = 0;
1325 STp->dirty = 0;
1327 if (count != 0) {
1328 STp->dirty = 1;
1329 i = append_to_buffer(b_point, STp->buffer, count);
1330 if (i) {
1331 retval = i;
1332 goto out;
1334 filp->f_pos += count;
1335 count = 0;
1338 if (doing_write && (STp->buffer)->syscall_result != 0) {
1339 retval = (STp->buffer)->syscall_result;
1340 goto out;
1343 if (STm->do_async_writes &&
1344 (((STp->buffer)->buffer_bytes >= STp->write_threshold &&
1345 (STp->buffer)->buffer_bytes >= STp->block_size) ||
1346 STp->block_size == 0)) {
1347 /* Schedule an asynchronous write */
1348 if (STp->block_size == 0)
1349 (STp->buffer)->writing = (STp->buffer)->buffer_bytes;
1350 else
1351 (STp->buffer)->writing = ((STp->buffer)->buffer_bytes /
1352 STp->block_size) * STp->block_size;
1353 STp->dirty = !((STp->buffer)->writing ==
1354 (STp->buffer)->buffer_bytes);
1356 if (STp->block_size == 0)
1357 blks = (STp->buffer)->writing;
1358 else
1359 blks = (STp->buffer)->writing / STp->block_size;
1360 cmd[2] = blks >> 16;
1361 cmd[3] = blks >> 8;
1362 cmd[4] = blks;
1363 DEB( STp->write_pending = 1; )
1365 SRpnt = st_do_scsi(SRpnt, STp, cmd, (STp->buffer)->writing,
1366 SCSI_DATA_WRITE, STp->timeout,
1367 MAX_WRITE_RETRIES, FALSE);
1368 if (SRpnt == NULL) {
1369 retval = (STp->buffer)->syscall_result;
1370 goto out;
1372 SRpnt = NULL; /* Prevent releasing this request! */
1375 STps->at_sm &= (total == 0);
1376 if (total > 0)
1377 STps->eof = ST_NOEOF;
1378 retval = total;
1380 out:
1381 if (SRpnt != NULL)
1382 scsi_release_request(SRpnt);
1383 up(&STp->lock);
1385 return retval;
1388 /* Read data from the tape. Returns zero in the normal case, one if the
1389 eof status has changed, and the negative error code in case of a
1390 fatal error. Otherwise updates the buffer and the eof state. */
1391 static long read_tape(Scsi_Tape *STp, long count, Scsi_Request ** aSRpnt)
1393 int transfer, blks, bytes;
1394 unsigned char cmd[MAX_COMMAND_SIZE];
1395 Scsi_Request *SRpnt;
1396 ST_mode *STm;
1397 ST_partstat *STps;
1398 int dev = TAPE_NR(STp->devt);
1399 int retval = 0;
1401 if (count == 0)
1402 return 0;
1404 STm = &(STp->modes[STp->current_mode]);
1405 STps = &(STp->ps[STp->partition]);
1406 if (STps->eof == ST_FM_HIT)
1407 return 1;
1409 memset(cmd, 0, MAX_COMMAND_SIZE);
1410 cmd[0] = READ_6;
1411 cmd[1] = (STp->block_size != 0);
1412 if (STp->block_size == 0)
1413 blks = bytes = count;
1414 else {
1415 if (STm->do_read_ahead) {
1416 blks = (STp->buffer)->buffer_blocks;
1417 bytes = blks * STp->block_size;
1418 } else {
1419 bytes = count;
1420 if (bytes > (STp->buffer)->buffer_size)
1421 bytes = (STp->buffer)->buffer_size;
1422 blks = bytes / STp->block_size;
1423 bytes = blks * STp->block_size;
1426 cmd[2] = blks >> 16;
1427 cmd[3] = blks >> 8;
1428 cmd[4] = blks;
1430 SRpnt = *aSRpnt;
1431 SRpnt = st_do_scsi(SRpnt, STp, cmd, bytes, SCSI_DATA_READ,
1432 STp->timeout, MAX_RETRIES, TRUE);
1433 *aSRpnt = SRpnt;
1434 if (!SRpnt)
1435 return (STp->buffer)->syscall_result;
1437 (STp->buffer)->read_pointer = 0;
1438 STps->at_sm = 0;
1440 /* Something to check */
1441 if ((STp->buffer)->syscall_result) {
1442 retval = 1;
1443 DEBC(printk(ST_DEB_MSG "st%d: Sense: %2x %2x %2x %2x %2x %2x %2x %2x\n",
1444 dev,
1445 SRpnt->sr_sense_buffer[0], SRpnt->sr_sense_buffer[1],
1446 SRpnt->sr_sense_buffer[2], SRpnt->sr_sense_buffer[3],
1447 SRpnt->sr_sense_buffer[4], SRpnt->sr_sense_buffer[5],
1448 SRpnt->sr_sense_buffer[6], SRpnt->sr_sense_buffer[7]));
1449 if ((SRpnt->sr_sense_buffer[0] & 0x70) == 0x70) { /* extended sense */
1451 if ((SRpnt->sr_sense_buffer[2] & 0x0f) == BLANK_CHECK)
1452 SRpnt->sr_sense_buffer[2] &= 0xcf; /* No need for EOM in this case */
1454 if ((SRpnt->sr_sense_buffer[2] & 0xe0) != 0) { /* EOF, EOM, or ILI */
1455 /* Compute the residual count */
1456 if ((SRpnt->sr_sense_buffer[0] & 0x80) != 0)
1457 transfer = (SRpnt->sr_sense_buffer[3] << 24) |
1458 (SRpnt->sr_sense_buffer[4] << 16) |
1459 (SRpnt->sr_sense_buffer[5] << 8) |
1460 SRpnt->sr_sense_buffer[6];
1461 else
1462 transfer = 0;
1463 if (STp->block_size == 0 &&
1464 (SRpnt->sr_sense_buffer[2] & 0x0f) == MEDIUM_ERROR)
1465 transfer = bytes;
1467 if (SRpnt->sr_sense_buffer[2] & 0x20) { /* ILI */
1468 if (STp->block_size == 0) {
1469 if (transfer < 0) {
1470 if (STps->drv_block >= 0)
1471 STps->drv_block += 1;
1472 return (-ENOMEM);
1474 (STp->buffer)->buffer_bytes = bytes - transfer;
1475 } else {
1476 scsi_release_request(SRpnt);
1477 SRpnt = *aSRpnt = NULL;
1478 if (transfer == blks) { /* We did not get anything, error */
1479 printk(KERN_NOTICE "st%d: Incorrect block size.\n", dev);
1480 if (STps->drv_block >= 0)
1481 STps->drv_block += blks - transfer + 1;
1482 st_int_ioctl(STp, MTBSR, 1);
1483 return (-EIO);
1485 /* We have some data, deliver it */
1486 (STp->buffer)->buffer_bytes = (blks - transfer) *
1487 STp->block_size;
1488 DEBC(printk(ST_DEB_MSG
1489 "st%d: ILI but enough data received %ld %d.\n",
1490 dev, count, (STp->buffer)->buffer_bytes));
1491 if (STps->drv_block >= 0)
1492 STps->drv_block += 1;
1493 if (st_int_ioctl(STp, MTBSR, 1))
1494 return (-EIO);
1496 } else if (SRpnt->sr_sense_buffer[2] & 0x80) { /* FM overrides EOM */
1497 if (STps->eof != ST_FM_HIT)
1498 STps->eof = ST_FM_HIT;
1499 else
1500 STps->eof = ST_EOD_2;
1501 if (STp->block_size == 0)
1502 (STp->buffer)->buffer_bytes = 0;
1503 else
1504 (STp->buffer)->buffer_bytes =
1505 bytes - transfer * STp->block_size;
1506 DEBC(printk(ST_DEB_MSG
1507 "st%d: EOF detected (%d bytes read).\n",
1508 dev, (STp->buffer)->buffer_bytes));
1509 } else if (SRpnt->sr_sense_buffer[2] & 0x40) {
1510 if (STps->eof == ST_FM)
1511 STps->eof = ST_EOD_1;
1512 else
1513 STps->eof = ST_EOM_OK;
1514 if (STp->block_size == 0)
1515 (STp->buffer)->buffer_bytes = bytes - transfer;
1516 else
1517 (STp->buffer)->buffer_bytes =
1518 bytes - transfer * STp->block_size;
1520 DEBC(printk(ST_DEB_MSG "st%d: EOM detected (%d bytes read).\n",
1521 dev, (STp->buffer)->buffer_bytes));
1524 /* end of EOF, EOM, ILI test */
1525 else { /* nonzero sense key */
1526 DEBC(printk(ST_DEB_MSG
1527 "st%d: Tape error while reading.\n", dev));
1528 STps->drv_block = (-1);
1529 if (STps->eof == ST_FM &&
1530 (SRpnt->sr_sense_buffer[2] & 0x0f) == BLANK_CHECK) {
1531 DEBC(printk(ST_DEB_MSG
1532 "st%d: Zero returned for first BLANK CHECK after EOF.\n",
1533 dev));
1534 STps->eof = ST_EOD_2; /* First BLANK_CHECK after FM */
1535 } else /* Some other extended sense code */
1536 retval = (-EIO);
1539 /* End of extended sense test */
1540 else { /* Non-extended sense */
1541 retval = (STp->buffer)->syscall_result;
1545 /* End of error handling */
1546 else /* Read successful */
1547 (STp->buffer)->buffer_bytes = bytes;
1549 if (STps->drv_block >= 0) {
1550 if (STp->block_size == 0)
1551 STps->drv_block++;
1552 else
1553 STps->drv_block += (STp->buffer)->buffer_bytes / STp->block_size;
1555 return retval;
1559 /* Read command */
1560 static ssize_t
1561 st_read(struct file *filp, char *buf, size_t count, loff_t * ppos)
1563 struct inode *inode = filp->f_dentry->d_inode;
1564 ssize_t total;
1565 ssize_t retval = 0;
1566 ssize_t i, transfer;
1567 int special;
1568 Scsi_Request *SRpnt = NULL;
1569 Scsi_Tape *STp;
1570 ST_mode *STm;
1571 ST_partstat *STps;
1572 int dev = TAPE_NR(inode->i_rdev);
1574 read_lock(&st_dev_arr_lock);
1575 STp = scsi_tapes[dev];
1576 read_unlock(&st_dev_arr_lock);
1578 if (down_interruptible(&STp->lock))
1579 return -ERESTARTSYS;
1582 * If we are in the middle of error recovery, don't let anyone
1583 * else try and use this device. Also, if error recovery fails, it
1584 * may try and take the device offline, in which case all further
1585 * access to the device is prohibited.
1587 if (!scsi_block_when_processing_errors(STp->device)) {
1588 retval = (-ENXIO);
1589 goto out;
1592 if (ppos != &filp->f_pos) {
1593 /* "A request was outside the capabilities of the device." */
1594 retval = (-ENXIO);
1595 goto out;
1598 if (STp->ready != ST_READY) {
1599 if (STp->ready == ST_NO_TAPE)
1600 retval = (-ENOMEDIUM);
1601 else
1602 retval = (-EIO);
1603 goto out;
1605 STm = &(STp->modes[STp->current_mode]);
1606 if (!STm->defined) {
1607 retval = (-ENXIO);
1608 goto out;
1610 DEB(
1611 if (!STp->in_use) {
1612 printk(ST_DEB_MSG "st%d: Incorrect device.\n", dev);
1613 retval = (-EIO);
1614 goto out;
1615 } ) /* end DEB */
1617 if (STp->can_partitions &&
1618 (retval = update_partition(STp)) < 0)
1619 goto out;
1621 if (STp->block_size == 0) {
1622 if (STp->max_block > 0 &&
1623 (count < STp->min_block || count > STp->max_block)) {
1624 retval = (-EINVAL);
1625 goto out;
1627 if (count > (STp->buffer)->buffer_size &&
1628 !enlarge_buffer(STp->buffer, count, STp->restr_dma)) {
1629 retval = (-EOVERFLOW);
1630 goto out;
1633 if ((STp->buffer)->buffer_blocks < 1) {
1634 /* Fixed block mode with too small buffer */
1635 if (!enlarge_buffer(STp->buffer, STp->block_size, STp->restr_dma)) {
1636 retval = (-EOVERFLOW);
1637 goto out;
1639 (STp->buffer)->buffer_blocks = 1;
1642 if (!(STm->do_read_ahead) && STp->block_size != 0 &&
1643 (count % STp->block_size) != 0) {
1644 retval = (-EINVAL); /* Read must be integral number of blocks */
1645 goto out;
1648 if (STp->do_auto_lock && STp->door_locked == ST_UNLOCKED &&
1649 !st_int_ioctl(STp, MTLOCK, 0))
1650 STp->door_locked = ST_LOCKED_AUTO;
1652 STps = &(STp->ps[STp->partition]);
1653 if (STps->rw == ST_WRITING) {
1654 retval = flush_buffer(STp, 0);
1655 if (retval)
1656 goto out;
1657 STps->rw = ST_READING;
1659 DEB(
1660 if (debugging && STps->eof != ST_NOEOF)
1661 printk(ST_DEB_MSG "st%d: EOF/EOM flag up (%d). Bytes %d\n", dev,
1662 STps->eof, (STp->buffer)->buffer_bytes);
1663 ) /* end DEB */
1665 if ((STp->buffer)->buffer_bytes == 0 &&
1666 STps->eof >= ST_EOD_1) {
1667 if (STps->eof < ST_EOD) {
1668 STps->eof += 1;
1669 retval = 0;
1670 goto out;
1672 retval = (-EIO); /* EOM or Blank Check */
1673 goto out;
1676 /* Check the buffer writability before any tape movement. Don't alter
1677 buffer data. */
1678 if (copy_from_user(&i, buf, 1) != 0 ||
1679 copy_to_user(buf, &i, 1) != 0 ||
1680 copy_from_user(&i, buf + count - 1, 1) != 0 ||
1681 copy_to_user(buf + count - 1, &i, 1) != 0) {
1682 retval = (-EFAULT);
1683 goto out;
1686 STps->rw = ST_READING;
1689 /* Loop until enough data in buffer or a special condition found */
1690 for (total = 0, special = 0; total < count && !special;) {
1692 /* Get new data if the buffer is empty */
1693 if ((STp->buffer)->buffer_bytes == 0) {
1694 special = read_tape(STp, count - total, &SRpnt);
1695 if (special < 0) { /* No need to continue read */
1696 retval = special;
1697 goto out;
1701 /* Move the data from driver buffer to user buffer */
1702 if ((STp->buffer)->buffer_bytes > 0) {
1703 DEB(
1704 if (debugging && STps->eof != ST_NOEOF)
1705 printk(ST_DEB_MSG
1706 "st%d: EOF up (%d). Left %d, needed %d.\n", dev,
1707 STps->eof, (STp->buffer)->buffer_bytes,
1708 count - total);
1709 ) /* end DEB */
1710 transfer = (STp->buffer)->buffer_bytes < count - total ?
1711 (STp->buffer)->buffer_bytes : count - total;
1712 i = from_buffer(STp->buffer, buf, transfer);
1713 if (i) {
1714 retval = i;
1715 goto out;
1717 filp->f_pos += transfer;
1718 buf += transfer;
1719 total += transfer;
1722 if (STp->block_size == 0)
1723 break; /* Read only one variable length block */
1725 } /* for (total = 0, special = 0;
1726 total < count && !special; ) */
1728 /* Change the eof state if no data from tape or buffer */
1729 if (total == 0) {
1730 if (STps->eof == ST_FM_HIT) {
1731 STps->eof = ST_FM;
1732 STps->drv_block = 0;
1733 if (STps->drv_file >= 0)
1734 STps->drv_file++;
1735 } else if (STps->eof == ST_EOD_1) {
1736 STps->eof = ST_EOD_2;
1737 STps->drv_block = 0;
1738 if (STps->drv_file >= 0)
1739 STps->drv_file++;
1740 } else if (STps->eof == ST_EOD_2)
1741 STps->eof = ST_EOD;
1742 } else if (STps->eof == ST_FM)
1743 STps->eof = ST_NOEOF;
1744 retval = total;
1746 out:
1747 if (SRpnt != NULL) {
1748 scsi_release_request(SRpnt);
1749 SRpnt = NULL;
1751 up(&STp->lock);
1753 return retval;
1758 /* Set the driver options */
1759 static void st_log_options(Scsi_Tape * STp, ST_mode * STm, int dev)
1761 printk(KERN_INFO
1762 "st%d: Mode %d options: buffer writes: %d, async writes: %d, read ahead: %d\n",
1763 dev, STp->current_mode, STm->do_buffer_writes, STm->do_async_writes,
1764 STm->do_read_ahead);
1765 printk(KERN_INFO
1766 "st%d: can bsr: %d, two FMs: %d, fast mteom: %d, auto lock: %d,\n",
1767 dev, STp->can_bsr, STp->two_fm, STp->fast_mteom, STp->do_auto_lock);
1768 printk(KERN_INFO
1769 "st%d: defs for wr: %d, no block limits: %d, partitions: %d, s2 log: %d\n",
1770 dev, STm->defaults_for_writes, STp->omit_blklims, STp->can_partitions,
1771 STp->scsi2_logical);
1772 printk(KERN_INFO
1773 "st%d: sysv: %d\n", dev, STm->sysv);
1774 DEB(printk(KERN_INFO
1775 "st%d: debugging: %d\n",
1776 dev, debugging);)
1780 static int st_set_options(Scsi_Tape *STp, long options)
1782 int value;
1783 long code;
1784 ST_mode *STm;
1785 int dev = TAPE_NR(STp->devt);
1787 STm = &(STp->modes[STp->current_mode]);
1788 if (!STm->defined) {
1789 memcpy(STm, &(STp->modes[0]), sizeof(ST_mode));
1790 modes_defined = TRUE;
1791 DEBC(printk(ST_DEB_MSG
1792 "st%d: Initialized mode %d definition from mode 0\n",
1793 dev, STp->current_mode));
1796 code = options & MT_ST_OPTIONS;
1797 if (code == MT_ST_BOOLEANS) {
1798 STm->do_buffer_writes = (options & MT_ST_BUFFER_WRITES) != 0;
1799 STm->do_async_writes = (options & MT_ST_ASYNC_WRITES) != 0;
1800 STm->defaults_for_writes = (options & MT_ST_DEF_WRITES) != 0;
1801 STm->do_read_ahead = (options & MT_ST_READ_AHEAD) != 0;
1802 STp->two_fm = (options & MT_ST_TWO_FM) != 0;
1803 STp->fast_mteom = (options & MT_ST_FAST_MTEOM) != 0;
1804 STp->do_auto_lock = (options & MT_ST_AUTO_LOCK) != 0;
1805 STp->can_bsr = (options & MT_ST_CAN_BSR) != 0;
1806 STp->omit_blklims = (options & MT_ST_NO_BLKLIMS) != 0;
1807 if ((STp->device)->scsi_level >= SCSI_2)
1808 STp->can_partitions = (options & MT_ST_CAN_PARTITIONS) != 0;
1809 STp->scsi2_logical = (options & MT_ST_SCSI2LOGICAL) != 0;
1810 STm->sysv = (options & MT_ST_SYSV) != 0;
1811 DEB( debugging = (options & MT_ST_DEBUGGING) != 0; )
1812 st_log_options(STp, STm, dev);
1813 } else if (code == MT_ST_SETBOOLEANS || code == MT_ST_CLEARBOOLEANS) {
1814 value = (code == MT_ST_SETBOOLEANS);
1815 if ((options & MT_ST_BUFFER_WRITES) != 0)
1816 STm->do_buffer_writes = value;
1817 if ((options & MT_ST_ASYNC_WRITES) != 0)
1818 STm->do_async_writes = value;
1819 if ((options & MT_ST_DEF_WRITES) != 0)
1820 STm->defaults_for_writes = value;
1821 if ((options & MT_ST_READ_AHEAD) != 0)
1822 STm->do_read_ahead = value;
1823 if ((options & MT_ST_TWO_FM) != 0)
1824 STp->two_fm = value;
1825 if ((options & MT_ST_FAST_MTEOM) != 0)
1826 STp->fast_mteom = value;
1827 if ((options & MT_ST_AUTO_LOCK) != 0)
1828 STp->do_auto_lock = value;
1829 if ((options & MT_ST_CAN_BSR) != 0)
1830 STp->can_bsr = value;
1831 if ((options & MT_ST_NO_BLKLIMS) != 0)
1832 STp->omit_blklims = value;
1833 if ((STp->device)->scsi_level >= SCSI_2 &&
1834 (options & MT_ST_CAN_PARTITIONS) != 0)
1835 STp->can_partitions = value;
1836 if ((options & MT_ST_SCSI2LOGICAL) != 0)
1837 STp->scsi2_logical = value;
1838 if ((options & MT_ST_SYSV) != 0)
1839 STm->sysv = value;
1840 DEB(
1841 if ((options & MT_ST_DEBUGGING) != 0)
1842 debugging = value; )
1843 st_log_options(STp, STm, dev);
1844 } else if (code == MT_ST_WRITE_THRESHOLD) {
1845 value = (options & ~MT_ST_OPTIONS) * ST_KILOBYTE;
1846 if (value < 1 || value > st_buffer_size) {
1847 printk(KERN_WARNING
1848 "st%d: Write threshold %d too small or too large.\n",
1849 dev, value);
1850 return (-EIO);
1852 STp->write_threshold = value;
1853 printk(KERN_INFO "st%d: Write threshold set to %d bytes.\n",
1854 dev, value);
1855 } else if (code == MT_ST_DEF_BLKSIZE) {
1856 value = (options & ~MT_ST_OPTIONS);
1857 if (value == ~MT_ST_OPTIONS) {
1858 STm->default_blksize = (-1);
1859 printk(KERN_INFO "st%d: Default block size disabled.\n", dev);
1860 } else {
1861 STm->default_blksize = value;
1862 printk(KERN_INFO "st%d: Default block size set to %d bytes.\n",
1863 dev, STm->default_blksize);
1865 } else if (code == MT_ST_TIMEOUTS) {
1866 value = (options & ~MT_ST_OPTIONS);
1867 if ((value & MT_ST_SET_LONG_TIMEOUT) != 0) {
1868 STp->long_timeout = (value & ~MT_ST_SET_LONG_TIMEOUT) * HZ;
1869 printk(KERN_INFO "st%d: Long timeout set to %d seconds.\n", dev,
1870 (value & ~MT_ST_SET_LONG_TIMEOUT));
1871 } else {
1872 STp->timeout = value * HZ;
1873 printk(KERN_INFO "st%d: Normal timeout set to %d seconds.\n",
1874 dev, value);
1876 } else if (code == MT_ST_DEF_OPTIONS) {
1877 code = (options & ~MT_ST_CLEAR_DEFAULT);
1878 value = (options & MT_ST_CLEAR_DEFAULT);
1879 if (code == MT_ST_DEF_DENSITY) {
1880 if (value == MT_ST_CLEAR_DEFAULT) {
1881 STm->default_density = (-1);
1882 printk(KERN_INFO "st%d: Density default disabled.\n",
1883 dev);
1884 } else {
1885 STm->default_density = value & 0xff;
1886 printk(KERN_INFO "st%d: Density default set to %x\n",
1887 dev, STm->default_density);
1889 } else if (code == MT_ST_DEF_DRVBUFFER) {
1890 if (value == MT_ST_CLEAR_DEFAULT) {
1891 STp->default_drvbuffer = 0xff;
1892 printk(KERN_INFO
1893 "st%d: Drive buffer default disabled.\n", dev);
1894 } else {
1895 STp->default_drvbuffer = value & 7;
1896 printk(KERN_INFO
1897 "st%d: Drive buffer default set to %x\n",
1898 dev, STp->default_drvbuffer);
1900 } else if (code == MT_ST_DEF_COMPRESSION) {
1901 if (value == MT_ST_CLEAR_DEFAULT) {
1902 STm->default_compression = ST_DONT_TOUCH;
1903 printk(KERN_INFO
1904 "st%d: Compression default disabled.\n", dev);
1905 } else {
1906 STm->default_compression = (value & 1 ? ST_YES : ST_NO);
1907 printk(KERN_INFO "st%d: Compression default set to %x\n",
1908 dev, (value & 1));
1911 } else
1912 return (-EIO);
1914 return 0;
1917 #define MODE_HEADER_LENGTH 4
1919 /* Mode header and page byte offsets */
1920 #define MH_OFF_DATA_LENGTH 0
1921 #define MH_OFF_MEDIUM_TYPE 1
1922 #define MH_OFF_DEV_SPECIFIC 2
1923 #define MH_OFF_BDESCS_LENGTH 3
1924 #define MP_OFF_PAGE_NBR 0
1925 #define MP_OFF_PAGE_LENGTH 1
1927 /* Mode header and page bit masks */
1928 #define MH_BIT_WP 0x80
1929 #define MP_MSK_PAGE_NBR 0x3f
1931 /* Don't return block descriptors */
1932 #define MODE_SENSE_OMIT_BDESCS 0x08
1934 #define MODE_SELECT_PAGE_FORMAT 0x10
1936 /* Read a mode page into the tape buffer. The block descriptors are included
1937 if incl_block_descs is true. */
1938 static int read_mode_page(Scsi_Tape *STp, int page, int omit_block_descs)
1940 unsigned char cmd[MAX_COMMAND_SIZE];
1941 Scsi_Request *SRpnt = NULL;
1943 memset(cmd, 0, MAX_COMMAND_SIZE);
1944 cmd[0] = MODE_SENSE;
1945 if (omit_block_descs)
1946 cmd[1] = MODE_SENSE_OMIT_BDESCS;
1947 cmd[2] = page;
1948 cmd[4] = 255;
1950 SRpnt = st_do_scsi(SRpnt, STp, cmd, cmd[4], SCSI_DATA_READ,
1951 STp->timeout, 0, TRUE);
1952 if (SRpnt == NULL)
1953 return (STp->buffer)->syscall_result;
1955 scsi_release_request(SRpnt);
1957 return (STp->buffer)->syscall_result;
1961 /* Send the mode page in the tape buffer to the drive. Assumes that the mode data
1962 in the buffer is correctly formatted. */
1963 static int write_mode_page(Scsi_Tape *STp, int page)
1965 int pgo;
1966 unsigned char cmd[MAX_COMMAND_SIZE];
1967 Scsi_Request *SRpnt = NULL;
1969 memset(cmd, 0, MAX_COMMAND_SIZE);
1970 cmd[0] = MODE_SELECT;
1971 cmd[1] = MODE_SELECT_PAGE_FORMAT;
1972 pgo = MODE_HEADER_LENGTH + (STp->buffer)->b_data[MH_OFF_BDESCS_LENGTH];
1973 cmd[4] = pgo + (STp->buffer)->b_data[pgo + MP_OFF_PAGE_LENGTH] + 2;
1975 /* Clear reserved fields */
1976 (STp->buffer)->b_data[MH_OFF_DATA_LENGTH] = 0;
1977 (STp->buffer)->b_data[MH_OFF_MEDIUM_TYPE] = 0;
1978 (STp->buffer)->b_data[MH_OFF_DEV_SPECIFIC] &= ~MH_BIT_WP;
1979 (STp->buffer)->b_data[pgo + MP_OFF_PAGE_NBR] &= MP_MSK_PAGE_NBR;
1981 SRpnt = st_do_scsi(SRpnt, STp, cmd, cmd[4], SCSI_DATA_WRITE,
1982 STp->timeout, 0, TRUE);
1983 if (SRpnt == NULL)
1984 return (STp->buffer)->syscall_result;
1986 scsi_release_request(SRpnt);
1988 return (STp->buffer)->syscall_result;
1992 #define COMPRESSION_PAGE 0x0f
1993 #define COMPRESSION_PAGE_LENGTH 16
1995 #define CP_OFF_DCE_DCC 2
1997 #define DCE_MASK 0x80
1998 #define DCC_MASK 0x40
1999 #define RED_MASK 0x60
2002 /* Control the compression with mode page 15. Algorithm not changed if zero.
2004 The block descriptors are read and written because Sony SDT-7000 does not
2005 work without this (suggestion from Michael Schaefer <Michael.Schaefer@dlr.de>).
2006 Including block descriptors should not cause any harm to other drives. */
2008 static int st_compression(Scsi_Tape * STp, int state)
2010 int retval;
2011 int mpoffs; /* Offset to mode page start */
2012 unsigned char *b_data = (STp->buffer)->b_data;
2013 DEB( int dev = TAPE_NR(STp->devt); )
2015 if (STp->ready != ST_READY)
2016 return (-EIO);
2018 /* Read the current page contents */
2019 retval = read_mode_page(STp, COMPRESSION_PAGE, FALSE);
2020 if (retval) {
2021 DEBC(printk(ST_DEB_MSG "st%d: Compression mode page not supported.\n",
2022 dev));
2023 return (-EIO);
2026 mpoffs = MODE_HEADER_LENGTH + b_data[MH_OFF_BDESCS_LENGTH];
2027 DEBC(printk(ST_DEB_MSG "st%d: Compression state is %d.\n", dev,
2028 (b_data[mpoffs + CP_OFF_DCE_DCC] & DCE_MASK ? 1 : 0)));
2030 /* Check if compression can be changed */
2031 if ((b_data[mpoffs + 2] & DCC_MASK) == 0) {
2032 DEBC(printk(ST_DEB_MSG "st%d: Compression not supported.\n", dev));
2033 return (-EIO);
2036 /* Do the change */
2037 if (state)
2038 b_data[mpoffs + CP_OFF_DCE_DCC] |= DCE_MASK;
2039 else
2040 b_data[mpoffs + CP_OFF_DCE_DCC] &= ~DCE_MASK;
2042 retval = write_mode_page(STp, COMPRESSION_PAGE);
2043 if (retval) {
2044 DEBC(printk(ST_DEB_MSG "st%d: Compression change failed.\n", dev));
2045 return (-EIO);
2047 DEBC(printk(ST_DEB_MSG "st%d: Compression state changed to %d.\n",
2048 dev, state));
2050 STp->compression_changed = TRUE;
2051 return 0;
2055 /* Internal ioctl function */
2056 static int st_int_ioctl(Scsi_Tape *STp, unsigned int cmd_in, unsigned long arg)
2058 int timeout;
2059 long ltmp;
2060 int i, ioctl_result;
2061 int chg_eof = TRUE;
2062 unsigned char cmd[MAX_COMMAND_SIZE];
2063 Scsi_Request *SRpnt;
2064 ST_partstat *STps;
2065 int fileno, blkno, at_sm, undone;
2066 int datalen = 0, direction = SCSI_DATA_NONE;
2067 int dev = TAPE_NR(STp->devt);
2069 if (STp->ready != ST_READY && cmd_in != MTLOAD) {
2070 if (STp->ready == ST_NO_TAPE)
2071 return (-ENOMEDIUM);
2072 else
2073 return (-EIO);
2075 timeout = STp->long_timeout;
2076 STps = &(STp->ps[STp->partition]);
2077 fileno = STps->drv_file;
2078 blkno = STps->drv_block;
2079 at_sm = STps->at_sm;
2081 memset(cmd, 0, MAX_COMMAND_SIZE);
2082 switch (cmd_in) {
2083 case MTFSFM:
2084 chg_eof = FALSE; /* Changed from the FSF after this */
2085 case MTFSF:
2086 cmd[0] = SPACE;
2087 cmd[1] = 0x01; /* Space FileMarks */
2088 cmd[2] = (arg >> 16);
2089 cmd[3] = (arg >> 8);
2090 cmd[4] = arg;
2091 DEBC(printk(ST_DEB_MSG "st%d: Spacing tape forward over %d filemarks.\n",
2092 dev, cmd[2] * 65536 + cmd[3] * 256 + cmd[4]));
2093 if (fileno >= 0)
2094 fileno += arg;
2095 blkno = 0;
2096 at_sm &= (arg == 0);
2097 break;
2098 case MTBSFM:
2099 chg_eof = FALSE; /* Changed from the FSF after this */
2100 case MTBSF:
2101 cmd[0] = SPACE;
2102 cmd[1] = 0x01; /* Space FileMarks */
2103 ltmp = (-arg);
2104 cmd[2] = (ltmp >> 16);
2105 cmd[3] = (ltmp >> 8);
2106 cmd[4] = ltmp;
2107 DEBC(
2108 if (cmd[2] & 0x80)
2109 ltmp = 0xff000000;
2110 ltmp = ltmp | (cmd[2] << 16) | (cmd[3] << 8) | cmd[4];
2111 printk(ST_DEB_MSG
2112 "st%d: Spacing tape backward over %ld filemarks.\n",
2113 dev, (-ltmp));
2115 if (fileno >= 0)
2116 fileno -= arg;
2117 blkno = (-1); /* We can't know the block number */
2118 at_sm &= (arg == 0);
2119 break;
2120 case MTFSR:
2121 cmd[0] = SPACE;
2122 cmd[1] = 0x00; /* Space Blocks */
2123 cmd[2] = (arg >> 16);
2124 cmd[3] = (arg >> 8);
2125 cmd[4] = arg;
2126 DEBC(printk(ST_DEB_MSG "st%d: Spacing tape forward %d blocks.\n", dev,
2127 cmd[2] * 65536 + cmd[3] * 256 + cmd[4]));
2128 if (blkno >= 0)
2129 blkno += arg;
2130 at_sm &= (arg == 0);
2131 break;
2132 case MTBSR:
2133 cmd[0] = SPACE;
2134 cmd[1] = 0x00; /* Space Blocks */
2135 ltmp = (-arg);
2136 cmd[2] = (ltmp >> 16);
2137 cmd[3] = (ltmp >> 8);
2138 cmd[4] = ltmp;
2139 DEBC(
2140 if (cmd[2] & 0x80)
2141 ltmp = 0xff000000;
2142 ltmp = ltmp | (cmd[2] << 16) | (cmd[3] << 8) | cmd[4];
2143 printk(ST_DEB_MSG
2144 "st%d: Spacing tape backward %ld blocks.\n", dev, (-ltmp));
2146 if (blkno >= 0)
2147 blkno -= arg;
2148 at_sm &= (arg == 0);
2149 break;
2150 case MTFSS:
2151 cmd[0] = SPACE;
2152 cmd[1] = 0x04; /* Space Setmarks */
2153 cmd[2] = (arg >> 16);
2154 cmd[3] = (arg >> 8);
2155 cmd[4] = arg;
2156 DEBC(printk(ST_DEB_MSG "st%d: Spacing tape forward %d setmarks.\n", dev,
2157 cmd[2] * 65536 + cmd[3] * 256 + cmd[4]));
2158 if (arg != 0) {
2159 blkno = fileno = (-1);
2160 at_sm = 1;
2162 break;
2163 case MTBSS:
2164 cmd[0] = SPACE;
2165 cmd[1] = 0x04; /* Space Setmarks */
2166 ltmp = (-arg);
2167 cmd[2] = (ltmp >> 16);
2168 cmd[3] = (ltmp >> 8);
2169 cmd[4] = ltmp;
2170 DEBC(
2171 if (cmd[2] & 0x80)
2172 ltmp = 0xff000000;
2173 ltmp = ltmp | (cmd[2] << 16) | (cmd[3] << 8) | cmd[4];
2174 printk(ST_DEB_MSG "st%d: Spacing tape backward %ld setmarks.\n",
2175 dev, (-ltmp));
2177 if (arg != 0) {
2178 blkno = fileno = (-1);
2179 at_sm = 1;
2181 break;
2182 case MTWEOF:
2183 case MTWSM:
2184 if (STp->write_prot)
2185 return (-EACCES);
2186 cmd[0] = WRITE_FILEMARKS;
2187 if (cmd_in == MTWSM)
2188 cmd[1] = 2;
2189 cmd[2] = (arg >> 16);
2190 cmd[3] = (arg >> 8);
2191 cmd[4] = arg;
2192 timeout = STp->timeout;
2193 DEBC(
2194 if (cmd_in == MTWEOF)
2195 printk(ST_DEB_MSG "st%d: Writing %d filemarks.\n", dev,
2196 cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
2197 else
2198 printk(ST_DEB_MSG "st%d: Writing %d setmarks.\n", dev,
2199 cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
2201 if (fileno >= 0)
2202 fileno += arg;
2203 blkno = 0;
2204 at_sm = (cmd_in == MTWSM);
2205 break;
2206 case MTREW:
2207 cmd[0] = REZERO_UNIT;
2208 #if ST_NOWAIT
2209 cmd[1] = 1; /* Don't wait for completion */
2210 timeout = STp->timeout;
2211 #endif
2212 DEBC(printk(ST_DEB_MSG "st%d: Rewinding tape.\n", dev));
2213 fileno = blkno = at_sm = 0;
2214 break;
2215 case MTOFFL:
2216 case MTLOAD:
2217 case MTUNLOAD:
2218 cmd[0] = START_STOP;
2219 if (cmd_in == MTLOAD)
2220 cmd[4] |= 1;
2222 * If arg >= 1 && arg <= 6 Enhanced load/unload in HP C1553A
2224 if (cmd_in != MTOFFL &&
2225 arg >= 1 + MT_ST_HPLOADER_OFFSET
2226 && arg <= 6 + MT_ST_HPLOADER_OFFSET) {
2227 DEBC(printk(ST_DEB_MSG "st%d: Enhanced %sload slot %2ld.\n",
2228 dev, (cmd[4]) ? "" : "un",
2229 arg - MT_ST_HPLOADER_OFFSET));
2230 cmd[3] = arg - MT_ST_HPLOADER_OFFSET; /* MediaID field of C1553A */
2232 #if ST_NOWAIT
2233 cmd[1] = 1; /* Don't wait for completion */
2234 timeout = STp->timeout;
2235 #else
2236 timeout = STp->long_timeout;
2237 #endif
2238 DEBC(
2239 if (cmd_in != MTLOAD)
2240 printk(ST_DEB_MSG "st%d: Unloading tape.\n", dev);
2241 else
2242 printk(ST_DEB_MSG "st%d: Loading tape.\n", dev);
2244 fileno = blkno = at_sm = 0;
2245 break;
2246 case MTNOP:
2247 DEBC(printk(ST_DEB_MSG "st%d: No op on tape.\n", dev));
2248 return 0; /* Should do something ? */
2249 break;
2250 case MTRETEN:
2251 cmd[0] = START_STOP;
2252 #if ST_NOWAIT
2253 cmd[1] = 1; /* Don't wait for completion */
2254 timeout = STp->timeout;
2255 #endif
2256 cmd[4] = 3;
2257 DEBC(printk(ST_DEB_MSG "st%d: Retensioning tape.\n", dev));
2258 fileno = blkno = at_sm = 0;
2259 break;
2260 case MTEOM:
2261 if (!STp->fast_mteom) {
2262 /* space to the end of tape */
2263 ioctl_result = st_int_ioctl(STp, MTFSF, 0x3fff);
2264 fileno = STps->drv_file;
2265 if (STps->eof >= ST_EOD_1)
2266 return 0;
2267 /* The next lines would hide the number of spaced FileMarks
2268 That's why I inserted the previous lines. I had no luck
2269 with detecting EOM with FSF, so we go now to EOM.
2270 Joerg Weule */
2271 } else
2272 fileno = (-1);
2273 cmd[0] = SPACE;
2274 cmd[1] = 3;
2275 DEBC(printk(ST_DEB_MSG "st%d: Spacing to end of recorded medium.\n",
2276 dev));
2277 blkno = 0;
2278 at_sm = 0;
2279 break;
2280 case MTERASE:
2281 if (STp->write_prot)
2282 return (-EACCES);
2283 cmd[0] = ERASE;
2284 cmd[1] = 1; /* To the end of tape */
2285 #if ST_NOWAIT
2286 cmd[1] |= 2; /* Don't wait for completion */
2287 timeout = STp->timeout;
2288 #else
2289 timeout = STp->long_timeout * 8;
2290 #endif
2291 DEBC(printk(ST_DEB_MSG "st%d: Erasing tape.\n", dev));
2292 fileno = blkno = at_sm = 0;
2293 break;
2294 case MTLOCK:
2295 chg_eof = FALSE;
2296 cmd[0] = ALLOW_MEDIUM_REMOVAL;
2297 cmd[4] = SCSI_REMOVAL_PREVENT;
2298 DEBC(printk(ST_DEB_MSG "st%d: Locking drive door.\n", dev));
2299 break;
2300 case MTUNLOCK:
2301 chg_eof = FALSE;
2302 cmd[0] = ALLOW_MEDIUM_REMOVAL;
2303 cmd[4] = SCSI_REMOVAL_ALLOW;
2304 DEBC(printk(ST_DEB_MSG "st%d: Unlocking drive door.\n", dev));
2305 break;
2306 case MTSETBLK: /* Set block length */
2307 case MTSETDENSITY: /* Set tape density */
2308 case MTSETDRVBUFFER: /* Set drive buffering */
2309 case SET_DENS_AND_BLK: /* Set density and block size */
2310 chg_eof = FALSE;
2311 if (STp->dirty || (STp->buffer)->buffer_bytes != 0)
2312 return (-EIO); /* Not allowed if data in buffer */
2313 if ((cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) &&
2314 (arg & MT_ST_BLKSIZE_MASK) != 0 &&
2315 STp->max_block > 0 &&
2316 ((arg & MT_ST_BLKSIZE_MASK) < STp->min_block ||
2317 (arg & MT_ST_BLKSIZE_MASK) > STp->max_block)) {
2318 printk(KERN_WARNING "st%d: Illegal block size.\n", dev);
2319 return (-EINVAL);
2321 cmd[0] = MODE_SELECT;
2322 cmd[4] = datalen = 12;
2323 direction = SCSI_DATA_WRITE;
2325 memset((STp->buffer)->b_data, 0, 12);
2326 if (cmd_in == MTSETDRVBUFFER)
2327 (STp->buffer)->b_data[2] = (arg & 7) << 4;
2328 else
2329 (STp->buffer)->b_data[2] =
2330 STp->drv_buffer << 4;
2331 (STp->buffer)->b_data[3] = 8; /* block descriptor length */
2332 if (cmd_in == MTSETDENSITY) {
2333 (STp->buffer)->b_data[4] = arg;
2334 STp->density_changed = TRUE; /* At least we tried ;-) */
2335 } else if (cmd_in == SET_DENS_AND_BLK)
2336 (STp->buffer)->b_data[4] = arg >> 24;
2337 else
2338 (STp->buffer)->b_data[4] = STp->density;
2339 if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) {
2340 ltmp = arg & MT_ST_BLKSIZE_MASK;
2341 if (cmd_in == MTSETBLK)
2342 STp->blksize_changed = TRUE; /* At least we tried ;-) */
2343 } else
2344 ltmp = STp->block_size;
2345 (STp->buffer)->b_data[9] = (ltmp >> 16);
2346 (STp->buffer)->b_data[10] = (ltmp >> 8);
2347 (STp->buffer)->b_data[11] = ltmp;
2348 timeout = STp->timeout;
2349 DEBC(
2350 if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK)
2351 printk(ST_DEB_MSG
2352 "st%d: Setting block size to %d bytes.\n", dev,
2353 (STp->buffer)->b_data[9] * 65536 +
2354 (STp->buffer)->b_data[10] * 256 +
2355 (STp->buffer)->b_data[11]);
2356 if (cmd_in == MTSETDENSITY || cmd_in == SET_DENS_AND_BLK)
2357 printk(ST_DEB_MSG
2358 "st%d: Setting density code to %x.\n", dev,
2359 (STp->buffer)->b_data[4]);
2360 if (cmd_in == MTSETDRVBUFFER)
2361 printk(ST_DEB_MSG
2362 "st%d: Setting drive buffer code to %d.\n", dev,
2363 ((STp->buffer)->b_data[2] >> 4) & 7);
2365 break;
2366 default:
2367 return (-ENOSYS);
2370 SRpnt = st_do_scsi(NULL, STp, cmd, datalen, direction,
2371 timeout, MAX_RETRIES, TRUE);
2372 if (!SRpnt)
2373 return (STp->buffer)->syscall_result;
2375 ioctl_result = (STp->buffer)->syscall_result;
2377 if (!ioctl_result) { /* SCSI command successful */
2378 scsi_release_request(SRpnt);
2379 SRpnt = NULL;
2380 STps->drv_block = blkno;
2381 STps->drv_file = fileno;
2382 STps->at_sm = at_sm;
2384 if (cmd_in == MTLOCK)
2385 STp->door_locked = ST_LOCKED_EXPLICIT;
2386 else if (cmd_in == MTUNLOCK)
2387 STp->door_locked = ST_UNLOCKED;
2389 if (cmd_in == MTBSFM)
2390 ioctl_result = st_int_ioctl(STp, MTFSF, 1);
2391 else if (cmd_in == MTFSFM)
2392 ioctl_result = st_int_ioctl(STp, MTBSF, 1);
2394 if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) {
2395 STp->block_size = arg & MT_ST_BLKSIZE_MASK;
2396 if (STp->block_size != 0)
2397 (STp->buffer)->buffer_blocks =
2398 (STp->buffer)->buffer_size / STp->block_size;
2399 (STp->buffer)->buffer_bytes = (STp->buffer)->read_pointer = 0;
2400 if (cmd_in == SET_DENS_AND_BLK)
2401 STp->density = arg >> MT_ST_DENSITY_SHIFT;
2402 } else if (cmd_in == MTSETDRVBUFFER)
2403 STp->drv_buffer = (arg & 7);
2404 else if (cmd_in == MTSETDENSITY)
2405 STp->density = arg;
2407 if (cmd_in == MTEOM)
2408 STps->eof = ST_EOD;
2409 else if (cmd_in == MTFSF)
2410 STps->eof = ST_FM;
2411 else if (chg_eof)
2412 STps->eof = ST_NOEOF;
2415 if (cmd_in == MTOFFL || cmd_in == MTUNLOAD)
2416 STp->rew_at_close = 0;
2417 else if (cmd_in == MTLOAD) {
2418 STp->rew_at_close = STp->autorew_dev;
2419 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
2420 STp->ps[i].rw = ST_IDLE;
2421 STp->ps[i].last_block_valid = FALSE;
2423 STp->partition = 0;
2425 } else { /* SCSI command was not completely successful. Don't return
2426 from this block without releasing the SCSI command block! */
2428 if (SRpnt->sr_sense_buffer[2] & 0x40) {
2429 if (cmd_in != MTBSF && cmd_in != MTBSFM &&
2430 cmd_in != MTBSR && cmd_in != MTBSS)
2431 STps->eof = ST_EOM_OK;
2432 STps->drv_block = 0;
2435 undone = (
2436 (SRpnt->sr_sense_buffer[3] << 24) +
2437 (SRpnt->sr_sense_buffer[4] << 16) +
2438 (SRpnt->sr_sense_buffer[5] << 8) +
2439 SRpnt->sr_sense_buffer[6]);
2440 if (cmd_in == MTWEOF &&
2441 (SRpnt->sr_sense_buffer[0] & 0x70) == 0x70 &&
2442 (SRpnt->sr_sense_buffer[2] & 0x4f) == 0x40 &&
2443 ((SRpnt->sr_sense_buffer[0] & 0x80) == 0 || undone == 0)) {
2444 ioctl_result = 0; /* EOF written succesfully at EOM */
2445 if (fileno >= 0)
2446 fileno++;
2447 STps->drv_file = fileno;
2448 STps->eof = ST_NOEOF;
2449 } else if ((cmd_in == MTFSF) || (cmd_in == MTFSFM)) {
2450 if (fileno >= 0)
2451 STps->drv_file = fileno - undone;
2452 else
2453 STps->drv_file = fileno;
2454 STps->drv_block = 0;
2455 STps->eof = ST_NOEOF;
2456 } else if ((cmd_in == MTBSF) || (cmd_in == MTBSFM)) {
2457 if (fileno >= 0)
2458 STps->drv_file = fileno + undone;
2459 else
2460 STps->drv_file = fileno;
2461 STps->drv_block = 0;
2462 STps->eof = ST_NOEOF;
2463 } else if (cmd_in == MTFSR) {
2464 if (SRpnt->sr_sense_buffer[2] & 0x80) { /* Hit filemark */
2465 if (STps->drv_file >= 0)
2466 STps->drv_file++;
2467 STps->drv_block = 0;
2468 STps->eof = ST_FM;
2469 } else {
2470 if (blkno >= undone)
2471 STps->drv_block = blkno - undone;
2472 else
2473 STps->drv_block = (-1);
2474 STps->eof = ST_NOEOF;
2476 } else if (cmd_in == MTBSR) {
2477 if (SRpnt->sr_sense_buffer[2] & 0x80) { /* Hit filemark */
2478 STps->drv_file--;
2479 STps->drv_block = (-1);
2480 } else {
2481 if (blkno >= 0)
2482 STps->drv_block = blkno + undone;
2483 else
2484 STps->drv_block = (-1);
2486 STps->eof = ST_NOEOF;
2487 } else if (cmd_in == MTEOM) {
2488 STps->drv_file = (-1);
2489 STps->drv_block = (-1);
2490 STps->eof = ST_EOD;
2491 } else if (chg_eof)
2492 STps->eof = ST_NOEOF;
2494 if ((SRpnt->sr_sense_buffer[2] & 0x0f) == BLANK_CHECK)
2495 STps->eof = ST_EOD;
2497 if (cmd_in == MTLOCK)
2498 STp->door_locked = ST_LOCK_FAILS;
2500 scsi_release_request(SRpnt);
2501 SRpnt = NULL;
2504 return ioctl_result;
2508 /* Get the tape position. If bt == 2, arg points into a kernel space mt_loc
2509 structure. */
2511 static int get_location(Scsi_Tape *STp, unsigned int *block, int *partition,
2512 int logical)
2514 int result;
2515 unsigned char scmd[MAX_COMMAND_SIZE];
2516 Scsi_Request *SRpnt;
2517 DEB( int dev = TAPE_NR(STp->devt); )
2519 if (STp->ready != ST_READY)
2520 return (-EIO);
2522 memset(scmd, 0, MAX_COMMAND_SIZE);
2523 if ((STp->device)->scsi_level < SCSI_2) {
2524 scmd[0] = QFA_REQUEST_BLOCK;
2525 scmd[4] = 3;
2526 } else {
2527 scmd[0] = READ_POSITION;
2528 if (!logical && !STp->scsi2_logical)
2529 scmd[1] = 1;
2531 SRpnt = st_do_scsi(NULL, STp, scmd, 20, SCSI_DATA_READ, STp->timeout,
2532 MAX_READY_RETRIES, TRUE);
2533 if (!SRpnt)
2534 return (STp->buffer)->syscall_result;
2536 if ((STp->buffer)->syscall_result != 0 ||
2537 (STp->device->scsi_level >= SCSI_2 &&
2538 ((STp->buffer)->b_data[0] & 4) != 0)) {
2539 *block = *partition = 0;
2540 DEBC(printk(ST_DEB_MSG "st%d: Can't read tape position.\n", dev));
2541 result = (-EIO);
2542 } else {
2543 result = 0;
2544 if ((STp->device)->scsi_level < SCSI_2) {
2545 *block = ((STp->buffer)->b_data[0] << 16)
2546 + ((STp->buffer)->b_data[1] << 8)
2547 + (STp->buffer)->b_data[2];
2548 *partition = 0;
2549 } else {
2550 *block = ((STp->buffer)->b_data[4] << 24)
2551 + ((STp->buffer)->b_data[5] << 16)
2552 + ((STp->buffer)->b_data[6] << 8)
2553 + (STp->buffer)->b_data[7];
2554 *partition = (STp->buffer)->b_data[1];
2555 if (((STp->buffer)->b_data[0] & 0x80) &&
2556 (STp->buffer)->b_data[1] == 0) /* BOP of partition 0 */
2557 STp->ps[0].drv_block = STp->ps[0].drv_file = 0;
2559 DEBC(printk(ST_DEB_MSG "st%d: Got tape pos. blk %d part %d.\n", dev,
2560 *block, *partition));
2562 scsi_release_request(SRpnt);
2563 SRpnt = NULL;
2565 return result;
2569 /* Set the tape block and partition. Negative partition means that only the
2570 block should be set in vendor specific way. */
2571 static int set_location(Scsi_Tape *STp, unsigned int block, int partition,
2572 int logical)
2574 ST_partstat *STps;
2575 int result, p;
2576 unsigned int blk;
2577 int timeout;
2578 unsigned char scmd[MAX_COMMAND_SIZE];
2579 Scsi_Request *SRpnt;
2580 DEB( int dev = TAPE_NR(STp->devt); )
2582 if (STp->ready != ST_READY)
2583 return (-EIO);
2584 timeout = STp->long_timeout;
2585 STps = &(STp->ps[STp->partition]);
2587 DEBC(printk(ST_DEB_MSG "st%d: Setting block to %d and partition to %d.\n",
2588 dev, block, partition));
2589 DEB(if (partition < 0)
2590 return (-EIO); )
2592 /* Update the location at the partition we are leaving */
2593 if ((!STp->can_partitions && partition != 0) ||
2594 partition >= ST_NBR_PARTITIONS)
2595 return (-EINVAL);
2596 if (partition != STp->partition) {
2597 if (get_location(STp, &blk, &p, 1))
2598 STps->last_block_valid = FALSE;
2599 else {
2600 STps->last_block_valid = TRUE;
2601 STps->last_block_visited = blk;
2602 DEBC(printk(ST_DEB_MSG
2603 "st%d: Visited block %d for partition %d saved.\n",
2604 dev, blk, STp->partition));
2608 memset(scmd, 0, MAX_COMMAND_SIZE);
2609 if ((STp->device)->scsi_level < SCSI_2) {
2610 scmd[0] = QFA_SEEK_BLOCK;
2611 scmd[2] = (block >> 16);
2612 scmd[3] = (block >> 8);
2613 scmd[4] = block;
2614 scmd[5] = 0;
2615 } else {
2616 scmd[0] = SEEK_10;
2617 scmd[3] = (block >> 24);
2618 scmd[4] = (block >> 16);
2619 scmd[5] = (block >> 8);
2620 scmd[6] = block;
2621 if (!logical && !STp->scsi2_logical)
2622 scmd[1] = 4;
2623 if (STp->partition != partition) {
2624 scmd[1] |= 2;
2625 scmd[8] = partition;
2626 DEBC(printk(ST_DEB_MSG
2627 "st%d: Trying to change partition from %d to %d\n",
2628 dev, STp->partition, partition));
2631 #if ST_NOWAIT
2632 scmd[1] |= 1; /* Don't wait for completion */
2633 timeout = STp->timeout;
2634 #endif
2636 SRpnt = st_do_scsi(NULL, STp, scmd, 0, SCSI_DATA_NONE,
2637 timeout, MAX_READY_RETRIES, TRUE);
2638 if (!SRpnt)
2639 return (STp->buffer)->syscall_result;
2641 STps->drv_block = STps->drv_file = (-1);
2642 STps->eof = ST_NOEOF;
2643 if ((STp->buffer)->syscall_result != 0) {
2644 result = (-EIO);
2645 if (STp->can_partitions &&
2646 (STp->device)->scsi_level >= SCSI_2 &&
2647 (p = find_partition(STp)) >= 0)
2648 STp->partition = p;
2649 } else {
2650 if (STp->can_partitions) {
2651 STp->partition = partition;
2652 STps = &(STp->ps[partition]);
2653 if (!STps->last_block_valid ||
2654 STps->last_block_visited != block) {
2655 STps->at_sm = 0;
2656 STps->rw = ST_IDLE;
2658 } else
2659 STps->at_sm = 0;
2660 if (block == 0)
2661 STps->drv_block = STps->drv_file = 0;
2662 result = 0;
2665 scsi_release_request(SRpnt);
2666 SRpnt = NULL;
2668 return result;
2672 /* Find the current partition number for the drive status. Called from open and
2673 returns either partition number of negative error code. */
2674 static int find_partition(Scsi_Tape *STp)
2676 int i, partition;
2677 unsigned int block;
2679 if ((i = get_location(STp, &block, &partition, 1)) < 0)
2680 return i;
2681 if (partition >= ST_NBR_PARTITIONS)
2682 return (-EIO);
2683 return partition;
2687 /* Change the partition if necessary */
2688 static int update_partition(Scsi_Tape *STp)
2690 ST_partstat *STps;
2692 if (STp->partition == STp->new_partition)
2693 return 0;
2694 STps = &(STp->ps[STp->new_partition]);
2695 if (!STps->last_block_valid)
2696 STps->last_block_visited = 0;
2697 return set_location(STp, STps->last_block_visited, STp->new_partition, 1);
2700 /* Functions for reading and writing the medium partition mode page. */
2702 #define PART_PAGE 0x11
2703 #define PART_PAGE_FIXED_LENGTH 8
2705 #define PP_OFF_MAX_ADD_PARTS 2
2706 #define PP_OFF_NBR_ADD_PARTS 3
2707 #define PP_OFF_FLAGS 4
2708 #define PP_OFF_PART_UNITS 6
2709 #define PP_OFF_RESERVED 7
2711 #define PP_BIT_IDP 0x20
2712 #define PP_MSK_PSUM_MB 0x10
2714 /* Get the number of partitions on the tape. As a side effect reads the
2715 mode page into the tape buffer. */
2716 static int nbr_partitions(Scsi_Tape *STp)
2718 int result;
2719 DEB( int dev = TAPE_NR(STp->devt) );
2721 if (STp->ready != ST_READY)
2722 return (-EIO);
2724 result = read_mode_page(STp, PART_PAGE, TRUE);
2726 if (result) {
2727 DEBC(printk(ST_DEB_MSG "st%d: Can't read medium partition page.\n",
2728 dev));
2729 result = (-EIO);
2730 } else {
2731 result = (STp->buffer)->b_data[MODE_HEADER_LENGTH +
2732 PP_OFF_NBR_ADD_PARTS] + 1;
2733 DEBC(printk(ST_DEB_MSG "st%d: Number of partitions %d.\n", dev, result));
2736 return result;
2740 /* Partition the tape into two partitions if size > 0 or one partition if
2741 size == 0.
2743 The block descriptors are read and written because Sony SDT-7000 does not
2744 work without this (suggestion from Michael Schaefer <Michael.Schaefer@dlr.de>).
2746 My HP C1533A drive returns only one partition size field. This is used to
2747 set the size of partition 1. There is no size field for the default partition.
2748 Michael Schaefer's Sony SDT-7000 returns two descriptors and the second is
2749 used to set the size of partition 1 (this is what the SCSI-3 standard specifies).
2750 The following algorithm is used to accomodate both drives: if the number of
2751 partition size fields is greater than the maximum number of additional partitions
2752 in the mode page, the second field is used. Otherwise the first field is used.
2754 For Seagate DDS drives the page length must be 8 when no partitions is defined
2755 and 10 when 1 partition is defined (information from Eric Lee Green). This is
2756 is acceptable also to some other old drives and enforced if the first partition
2757 size field is used for the first additional partition size.
2759 static int partition_tape(Scsi_Tape *STp, int size)
2761 int dev = TAPE_NR(STp->devt), result;
2762 int pgo, psd_cnt, psdo;
2763 unsigned char *bp;
2765 result = read_mode_page(STp, PART_PAGE, FALSE);
2766 if (result) {
2767 DEBC(printk(ST_DEB_MSG "st%d: Can't read partition mode page.\n", dev));
2768 return result;
2770 /* The mode page is in the buffer. Let's modify it and write it. */
2771 bp = (STp->buffer)->b_data;
2772 pgo = MODE_HEADER_LENGTH + bp[MH_OFF_BDESCS_LENGTH];
2773 DEBC(printk(ST_DEB_MSG "st%d: Partition page length is %d bytes.\n",
2774 dev, bp[pgo + MP_OFF_PAGE_LENGTH] + 2));
2776 psd_cnt = (bp[pgo + MP_OFF_PAGE_LENGTH] + 2 - PART_PAGE_FIXED_LENGTH) / 2;
2777 psdo = pgo + PART_PAGE_FIXED_LENGTH;
2778 if (psd_cnt > bp[pgo + PP_OFF_MAX_ADD_PARTS]) {
2779 bp[psdo] = bp[psdo + 1] = 0xff; /* Rest of the tape */
2780 psdo += 2;
2782 memset(bp + psdo, 0, bp[pgo + PP_OFF_NBR_ADD_PARTS] * 2);
2784 DEBC(printk("st%d: psd_cnt %d, max.parts %d, nbr_parts %d\n", dev,
2785 psd_cnt, bp[pgo + PP_OFF_MAX_ADD_PARTS],
2786 bp[pgo + PP_OFF_NBR_ADD_PARTS]));
2788 if (size <= 0) {
2789 bp[pgo + PP_OFF_NBR_ADD_PARTS] = 0;
2790 if (psd_cnt <= bp[pgo + PP_OFF_MAX_ADD_PARTS])
2791 bp[pgo + MP_OFF_PAGE_LENGTH] = 6;
2792 DEBC(printk(ST_DEB_MSG "st%d: Formatting tape with one partition.\n",
2793 dev));
2794 } else {
2795 bp[psdo] = (size >> 8) & 0xff;
2796 bp[psdo + 1] = size & 0xff;
2797 bp[pgo + 3] = 1;
2798 if (bp[pgo + MP_OFF_PAGE_LENGTH] < 8)
2799 bp[pgo + MP_OFF_PAGE_LENGTH] = 8;
2800 DEBC(printk(ST_DEB_MSG
2801 "st%d: Formatting tape with two partitions (1 = %d MB).\n",
2802 dev, size));
2804 bp[pgo + PP_OFF_PART_UNITS] = 0;
2805 bp[pgo + PP_OFF_RESERVED] = 0;
2806 bp[pgo + PP_OFF_FLAGS] = PP_BIT_IDP | PP_MSK_PSUM_MB;
2808 result = write_mode_page(STp, PART_PAGE);
2809 if (result) {
2810 printk(KERN_INFO "st%d: Partitioning of tape failed.\n", dev);
2811 result = (-EIO);
2814 return result;
2819 /* The ioctl command */
2820 static int st_ioctl(struct inode *inode, struct file *file,
2821 unsigned int cmd_in, unsigned long arg)
2823 int i, cmd_nr, cmd_type, bt;
2824 int retval = 0;
2825 unsigned int blk;
2826 Scsi_Tape *STp;
2827 ST_mode *STm;
2828 ST_partstat *STps;
2829 int dev = TAPE_NR(inode->i_rdev);
2831 read_lock(&st_dev_arr_lock);
2832 STp = scsi_tapes[dev];
2833 read_unlock(&st_dev_arr_lock);
2835 if (down_interruptible(&STp->lock))
2836 return -ERESTARTSYS;
2838 DEB(
2839 if (debugging && !STp->in_use) {
2840 printk(ST_DEB_MSG "st%d: Incorrect device.\n", dev);
2841 retval = (-EIO);
2842 goto out;
2843 } ) /* end DEB */
2845 STm = &(STp->modes[STp->current_mode]);
2846 STps = &(STp->ps[STp->partition]);
2849 * If we are in the middle of error recovery, don't let anyone
2850 * else try and use this device. Also, if error recovery fails, it
2851 * may try and take the device offline, in which case all further
2852 * access to the device is prohibited.
2854 if (!scsi_block_when_processing_errors(STp->device)) {
2855 retval = (-ENXIO);
2856 goto out;
2858 cmd_type = _IOC_TYPE(cmd_in);
2859 cmd_nr = _IOC_NR(cmd_in);
2861 if (cmd_type == _IOC_TYPE(MTIOCTOP) && cmd_nr == _IOC_NR(MTIOCTOP)) {
2862 struct mtop mtc;
2864 if (_IOC_SIZE(cmd_in) != sizeof(mtc)) {
2865 retval = (-EINVAL);
2866 goto out;
2869 i = copy_from_user((char *) &mtc, (char *) arg, sizeof(struct mtop));
2870 if (i) {
2871 retval = (-EFAULT);
2872 goto out;
2875 if (mtc.mt_op == MTSETDRVBUFFER && !capable(CAP_SYS_ADMIN)) {
2876 printk(KERN_WARNING
2877 "st%d: MTSETDRVBUFFER only allowed for root.\n", dev);
2878 retval = (-EPERM);
2879 goto out;
2881 if (!STm->defined &&
2882 (mtc.mt_op != MTSETDRVBUFFER &&
2883 (mtc.mt_count & MT_ST_OPTIONS) == 0)) {
2884 retval = (-ENXIO);
2885 goto out;
2888 if (!(STp->device)->was_reset) {
2890 if (STps->eof == ST_FM_HIT) {
2891 if (mtc.mt_op == MTFSF || mtc.mt_op == MTFSFM ||
2892 mtc.mt_op == MTEOM) {
2893 mtc.mt_count -= 1;
2894 if (STps->drv_file >= 0)
2895 STps->drv_file += 1;
2896 } else if (mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM) {
2897 mtc.mt_count += 1;
2898 if (STps->drv_file >= 0)
2899 STps->drv_file += 1;
2903 if (mtc.mt_op == MTSEEK) {
2904 /* Old position must be restored if partition will be
2905 changed */
2906 i = !STp->can_partitions ||
2907 (STp->new_partition != STp->partition);
2908 } else {
2909 i = mtc.mt_op == MTREW || mtc.mt_op == MTOFFL ||
2910 mtc.mt_op == MTRETEN || mtc.mt_op == MTEOM ||
2911 mtc.mt_op == MTLOCK || mtc.mt_op == MTLOAD ||
2912 mtc.mt_op == MTFSF || mtc.mt_op == MTFSFM ||
2913 mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM ||
2914 mtc.mt_op == MTCOMPRESSION;
2916 i = flush_buffer(STp, i);
2917 if (i < 0) {
2918 retval = i;
2919 goto out;
2921 } else {
2923 * If there was a bus reset, block further access
2924 * to this device. If the user wants to rewind the tape,
2925 * then reset the flag and allow access again.
2927 if (mtc.mt_op != MTREW &&
2928 mtc.mt_op != MTOFFL &&
2929 mtc.mt_op != MTRETEN &&
2930 mtc.mt_op != MTERASE &&
2931 mtc.mt_op != MTSEEK &&
2932 mtc.mt_op != MTEOM) {
2933 retval = (-EIO);
2934 goto out;
2936 STp->device->was_reset = 0;
2937 if (STp->door_locked != ST_UNLOCKED &&
2938 STp->door_locked != ST_LOCK_FAILS) {
2939 if (st_int_ioctl(STp, MTLOCK, 0)) {
2940 printk(KERN_NOTICE
2941 "st%d: Could not relock door after bus reset.\n",
2942 dev);
2943 STp->door_locked = ST_UNLOCKED;
2948 if (mtc.mt_op != MTNOP && mtc.mt_op != MTSETBLK &&
2949 mtc.mt_op != MTSETDENSITY && mtc.mt_op != MTWSM &&
2950 mtc.mt_op != MTSETDRVBUFFER && mtc.mt_op != MTSETPART)
2951 STps->rw = ST_IDLE; /* Prevent automatic WEOF and fsf */
2953 if (mtc.mt_op == MTOFFL && STp->door_locked != ST_UNLOCKED)
2954 st_int_ioctl(STp, MTUNLOCK, 0); /* Ignore result! */
2956 if (mtc.mt_op == MTSETDRVBUFFER &&
2957 (mtc.mt_count & MT_ST_OPTIONS) != 0) {
2958 retval = st_set_options(STp, mtc.mt_count);
2959 goto out;
2962 if (mtc.mt_op == MTSETPART) {
2963 if (!STp->can_partitions ||
2964 mtc.mt_count < 0 || mtc.mt_count >= ST_NBR_PARTITIONS) {
2965 retval = (-EINVAL);
2966 goto out;
2968 if (mtc.mt_count >= STp->nbr_partitions &&
2969 (STp->nbr_partitions = nbr_partitions(STp)) < 0) {
2970 retval = (-EIO);
2971 goto out;
2973 if (mtc.mt_count >= STp->nbr_partitions) {
2974 retval = (-EINVAL);
2975 goto out;
2977 STp->new_partition = mtc.mt_count;
2978 retval = 0;
2979 goto out;
2982 if (mtc.mt_op == MTMKPART) {
2983 if (!STp->can_partitions) {
2984 retval = (-EINVAL);
2985 goto out;
2987 if ((i = st_int_ioctl(STp, MTREW, 0)) < 0 ||
2988 (i = partition_tape(STp, mtc.mt_count)) < 0) {
2989 retval = i;
2990 goto out;
2992 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
2993 STp->ps[i].rw = ST_IDLE;
2994 STp->ps[i].at_sm = 0;
2995 STp->ps[i].last_block_valid = FALSE;
2997 STp->partition = STp->new_partition = 0;
2998 STp->nbr_partitions = 1; /* Bad guess ?-) */
2999 STps->drv_block = STps->drv_file = 0;
3000 retval = 0;
3001 goto out;
3004 if (mtc.mt_op == MTSEEK) {
3005 i = set_location(STp, mtc.mt_count, STp->new_partition, 0);
3006 if (!STp->can_partitions)
3007 STp->ps[0].rw = ST_IDLE;
3008 retval = i;
3009 goto out;
3012 if (STp->can_partitions && STp->ready == ST_READY &&
3013 (i = update_partition(STp)) < 0) {
3014 retval = i;
3015 goto out;
3018 if (mtc.mt_op == MTCOMPRESSION)
3019 retval = st_compression(STp, (mtc.mt_count & 1));
3020 else
3021 retval = st_int_ioctl(STp, mtc.mt_op, mtc.mt_count);
3022 goto out;
3024 if (!STm->defined) {
3025 retval = (-ENXIO);
3026 goto out;
3029 if ((i = flush_buffer(STp, FALSE)) < 0) {
3030 retval = i;
3031 goto out;
3033 if (STp->can_partitions &&
3034 (i = update_partition(STp)) < 0) {
3035 retval = i;
3036 goto out;
3039 if (cmd_type == _IOC_TYPE(MTIOCGET) && cmd_nr == _IOC_NR(MTIOCGET)) {
3040 struct mtget mt_status;
3042 if (_IOC_SIZE(cmd_in) != sizeof(struct mtget)) {
3043 retval = (-EINVAL);
3044 goto out;
3047 mt_status.mt_type = STp->tape_type;
3048 mt_status.mt_dsreg =
3049 ((STp->block_size << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK) |
3050 ((STp->density << MT_ST_DENSITY_SHIFT) & MT_ST_DENSITY_MASK);
3051 mt_status.mt_blkno = STps->drv_block;
3052 mt_status.mt_fileno = STps->drv_file;
3053 if (STp->block_size != 0) {
3054 if (STps->rw == ST_WRITING)
3055 mt_status.mt_blkno +=
3056 (STp->buffer)->buffer_bytes / STp->block_size;
3057 else if (STps->rw == ST_READING)
3058 mt_status.mt_blkno -=
3059 ((STp->buffer)->buffer_bytes +
3060 STp->block_size - 1) / STp->block_size;
3063 mt_status.mt_gstat = 0;
3064 if (STp->drv_write_prot)
3065 mt_status.mt_gstat |= GMT_WR_PROT(0xffffffff);
3066 if (mt_status.mt_blkno == 0) {
3067 if (mt_status.mt_fileno == 0)
3068 mt_status.mt_gstat |= GMT_BOT(0xffffffff);
3069 else
3070 mt_status.mt_gstat |= GMT_EOF(0xffffffff);
3072 mt_status.mt_erreg = (STp->recover_reg << MT_ST_SOFTERR_SHIFT);
3073 mt_status.mt_resid = STp->partition;
3074 if (STps->eof == ST_EOM_OK || STps->eof == ST_EOM_ERROR)
3075 mt_status.mt_gstat |= GMT_EOT(0xffffffff);
3076 else if (STps->eof >= ST_EOM_OK)
3077 mt_status.mt_gstat |= GMT_EOD(0xffffffff);
3078 if (STp->density == 1)
3079 mt_status.mt_gstat |= GMT_D_800(0xffffffff);
3080 else if (STp->density == 2)
3081 mt_status.mt_gstat |= GMT_D_1600(0xffffffff);
3082 else if (STp->density == 3)
3083 mt_status.mt_gstat |= GMT_D_6250(0xffffffff);
3084 if (STp->ready == ST_READY)
3085 mt_status.mt_gstat |= GMT_ONLINE(0xffffffff);
3086 if (STp->ready == ST_NO_TAPE)
3087 mt_status.mt_gstat |= GMT_DR_OPEN(0xffffffff);
3088 if (STps->at_sm)
3089 mt_status.mt_gstat |= GMT_SM(0xffffffff);
3090 if (STm->do_async_writes ||
3091 (STm->do_buffer_writes && STp->block_size != 0) ||
3092 STp->drv_buffer != 0)
3093 mt_status.mt_gstat |= GMT_IM_REP_EN(0xffffffff);
3095 i = copy_to_user((char *) arg, (char *) &(mt_status),
3096 sizeof(struct mtget));
3097 if (i) {
3098 retval = (-EFAULT);
3099 goto out;
3102 STp->recover_reg = 0; /* Clear after read */
3103 retval = 0;
3104 goto out;
3105 } /* End of MTIOCGET */
3106 if (cmd_type == _IOC_TYPE(MTIOCPOS) && cmd_nr == _IOC_NR(MTIOCPOS)) {
3107 struct mtpos mt_pos;
3108 if (_IOC_SIZE(cmd_in) != sizeof(struct mtpos)) {
3109 retval = (-EINVAL);
3110 goto out;
3112 if ((i = get_location(STp, &blk, &bt, 0)) < 0) {
3113 retval = i;
3114 goto out;
3116 mt_pos.mt_blkno = blk;
3117 i = copy_to_user((char *) arg, (char *) (&mt_pos), sizeof(struct mtpos));
3118 if (i)
3119 retval = (-EFAULT);
3120 goto out;
3122 up(&STp->lock);
3123 return scsi_ioctl(STp->device, cmd_in, (void *) arg);
3125 out:
3126 up(&STp->lock);
3127 return retval;
3131 /* Try to allocate a new tape buffer. Calling function must not hold
3132 dev_arr_lock. */
3133 static ST_buffer *
3134 new_tape_buffer(int from_initialization, int need_dma, int in_use)
3136 int i, priority, b_size, order, got = 0, segs = 0;
3137 unsigned long flags;
3138 ST_buffer *tb;
3140 read_lock(&st_dev_arr_lock);
3141 if (st_nbr_buffers >= st_template.dev_max) {
3142 read_unlock(&st_dev_arr_lock);
3143 return NULL; /* Should never happen */
3145 read_unlock(&st_dev_arr_lock);
3147 if (from_initialization)
3148 priority = GFP_ATOMIC;
3149 else
3150 priority = GFP_KERNEL;
3152 i = sizeof(ST_buffer) + (st_max_sg_segs - 1) * sizeof(struct scatterlist);
3153 tb = kmalloc(i, priority);
3154 if (tb) {
3155 if (need_dma)
3156 priority |= GFP_DMA;
3158 /* Try to allocate the first segment up to ST_FIRST_ORDER and the
3159 others big enough to reach the goal */
3160 for (b_size = PAGE_SIZE, order=0;
3161 b_size < st_buffer_size && order < ST_FIRST_ORDER;
3162 order++, b_size *= 2)
3164 for ( ; b_size >= PAGE_SIZE; order--, b_size /= 2) {
3165 tb->sg[0].address =
3166 (unsigned char *) __get_free_pages(priority, order);
3167 if (tb->sg[0].address != NULL) {
3168 tb->sg[0].alt_address = NULL;
3169 tb->sg[0].length = b_size;
3170 break;
3173 if (tb->sg[segs].address == NULL) {
3174 kfree(tb);
3175 tb = NULL;
3176 } else { /* Got something, continue */
3178 for (b_size = PAGE_SIZE, order=0;
3179 st_buffer_size >
3180 tb->sg[0].length + (ST_FIRST_SG - 1) * b_size;
3181 order++, b_size *= 2)
3183 for (segs = 1, got = tb->sg[0].length;
3184 got < st_buffer_size && segs < ST_FIRST_SG;) {
3185 tb->sg[segs].address =
3186 (unsigned char *) __get_free_pages(priority,
3187 order);
3188 if (tb->sg[segs].address == NULL) {
3189 if (st_buffer_size - got <=
3190 (ST_FIRST_SG - segs) * b_size / 2) {
3191 b_size /= 2; /* Large enough for the
3192 rest of the buffers */
3193 order--;
3194 continue;
3196 tb->sg_segs = segs;
3197 tb->orig_sg_segs = 0;
3198 DEB(tb->buffer_size = got);
3199 normalize_buffer(tb);
3200 kfree(tb);
3201 tb = NULL;
3202 break;
3204 tb->sg[segs].alt_address = NULL;
3205 tb->sg[segs].length = b_size;
3206 got += b_size;
3207 segs++;
3212 if (!tb) {
3213 printk(KERN_NOTICE "st: Can't allocate new tape buffer (nbr %d).\n",
3214 st_nbr_buffers);
3215 return NULL;
3217 tb->sg_segs = tb->orig_sg_segs = segs;
3218 tb->b_data = tb->sg[0].address;
3220 DEBC(printk(ST_DEB_MSG
3221 "st: Allocated tape buffer %d (%d bytes, %d segments, dma: %d, a: %p).\n",
3222 st_nbr_buffers, got, tb->sg_segs, need_dma, tb->b_data);
3223 printk(ST_DEB_MSG
3224 "st: segment sizes: first %d, last %d bytes.\n",
3225 tb->sg[0].length, tb->sg[segs - 1].length);
3227 tb->in_use = in_use;
3228 tb->dma = need_dma;
3229 tb->buffer_size = got;
3230 tb->writing = 0;
3232 write_lock_irqsave(&st_dev_arr_lock, flags);
3233 st_buffers[st_nbr_buffers++] = tb;
3234 write_unlock_irqrestore(&st_dev_arr_lock, flags);
3236 return tb;
3240 /* Try to allocate a temporary enlarged tape buffer */
3241 static int enlarge_buffer(ST_buffer * STbuffer, int new_size, int need_dma)
3243 int segs, nbr, max_segs, b_size, priority, order, got;
3245 normalize_buffer(STbuffer);
3247 max_segs = STbuffer->use_sg;
3248 if (max_segs > st_max_sg_segs)
3249 max_segs = st_max_sg_segs;
3250 nbr = max_segs - STbuffer->sg_segs;
3251 if (nbr <= 0)
3252 return FALSE;
3254 priority = GFP_KERNEL;
3255 if (need_dma)
3256 priority |= GFP_DMA;
3257 for (b_size = PAGE_SIZE, order=0;
3258 b_size * nbr < new_size - STbuffer->buffer_size;
3259 order++, b_size *= 2)
3260 ; /* empty */
3262 for (segs = STbuffer->sg_segs, got = STbuffer->buffer_size;
3263 segs < max_segs && got < new_size;) {
3264 STbuffer->sg[segs].address =
3265 (unsigned char *) __get_free_pages(priority, order);
3266 if (STbuffer->sg[segs].address == NULL) {
3267 if (new_size - got <= (max_segs - segs) * b_size / 2) {
3268 b_size /= 2; /* Large enough for the rest of the buffers */
3269 order--;
3270 continue;
3272 printk(KERN_NOTICE "st: failed to enlarge buffer to %d bytes.\n",
3273 new_size);
3274 DEB(STbuffer->buffer_size = got);
3275 normalize_buffer(STbuffer);
3276 return FALSE;
3278 STbuffer->sg[segs].alt_address = NULL;
3279 STbuffer->sg[segs].length = b_size;
3280 STbuffer->sg_segs += 1;
3281 got += b_size;
3282 STbuffer->buffer_size = got;
3283 segs++;
3285 DEBC(printk(ST_DEB_MSG
3286 "st: Succeeded to enlarge buffer to %d bytes (segs %d->%d, %d).\n",
3287 got, STbuffer->orig_sg_segs, STbuffer->sg_segs, b_size));
3289 return TRUE;
3293 /* Release the extra buffer */
3294 static void normalize_buffer(ST_buffer * STbuffer)
3296 int i, order, b_size;
3298 for (i = STbuffer->orig_sg_segs; i < STbuffer->sg_segs; i++) {
3299 for (b_size=PAGE_SIZE, order=0; b_size < STbuffer->sg[i].length;
3300 order++, b_size *= 2)
3301 ; /* empty */
3302 free_pages((unsigned long)(STbuffer->sg[i].address), order);
3303 STbuffer->buffer_size -= STbuffer->sg[i].length;
3305 DEB(
3306 if (debugging && STbuffer->orig_sg_segs < STbuffer->sg_segs)
3307 printk(ST_DEB_MSG "st: Buffer at %p normalized to %d bytes (segs %d).\n",
3308 STbuffer->sg[0].address, STbuffer->buffer_size,
3309 STbuffer->sg_segs);
3310 ) /* end DEB */
3311 STbuffer->sg_segs = STbuffer->orig_sg_segs;
3315 /* Move data from the user buffer to the tape buffer. Returns zero (success) or
3316 negative error code. */
3317 static int append_to_buffer(const char *ubp, ST_buffer * st_bp, int do_count)
3319 int i, cnt, res, offset;
3321 for (i = 0, offset = st_bp->buffer_bytes;
3322 i < st_bp->sg_segs && offset >= st_bp->sg[i].length; i++)
3323 offset -= st_bp->sg[i].length;
3324 if (i == st_bp->sg_segs) { /* Should never happen */
3325 printk(KERN_WARNING "st: append_to_buffer offset overflow.\n");
3326 return (-EIO);
3328 for (; i < st_bp->sg_segs && do_count > 0; i++) {
3329 cnt = st_bp->sg[i].length - offset < do_count ?
3330 st_bp->sg[i].length - offset : do_count;
3331 res = copy_from_user(st_bp->sg[i].address + offset, ubp, cnt);
3332 if (res)
3333 return (-EFAULT);
3334 do_count -= cnt;
3335 st_bp->buffer_bytes += cnt;
3336 ubp += cnt;
3337 offset = 0;
3339 if (do_count) { /* Should never happen */
3340 printk(KERN_WARNING "st: append_to_buffer overflow (left %d).\n",
3341 do_count);
3342 return (-EIO);
3344 return 0;
3348 /* Move data from the tape buffer to the user buffer. Returns zero (success) or
3349 negative error code. */
3350 static int from_buffer(ST_buffer * st_bp, char *ubp, int do_count)
3352 int i, cnt, res, offset;
3354 for (i = 0, offset = st_bp->read_pointer;
3355 i < st_bp->sg_segs && offset >= st_bp->sg[i].length; i++)
3356 offset -= st_bp->sg[i].length;
3357 if (i == st_bp->sg_segs) { /* Should never happen */
3358 printk(KERN_WARNING "st: from_buffer offset overflow.\n");
3359 return (-EIO);
3361 for (; i < st_bp->sg_segs && do_count > 0; i++) {
3362 cnt = st_bp->sg[i].length - offset < do_count ?
3363 st_bp->sg[i].length - offset : do_count;
3364 res = copy_to_user(ubp, st_bp->sg[i].address + offset, cnt);
3365 if (res)
3366 return (-EFAULT);
3367 do_count -= cnt;
3368 st_bp->buffer_bytes -= cnt;
3369 st_bp->read_pointer += cnt;
3370 ubp += cnt;
3371 offset = 0;
3373 if (do_count) { /* Should never happen */
3374 printk(KERN_WARNING "st: from_buffer overflow (left %d).\n",
3375 do_count);
3376 return (-EIO);
3378 return 0;
3382 /* Validate the options from command line or module parameters */
3383 static void validate_options(void)
3385 if (buffer_kbs > 0)
3386 st_buffer_size = buffer_kbs * ST_KILOBYTE;
3387 if (write_threshold_kbs > 0)
3388 st_write_threshold = write_threshold_kbs * ST_KILOBYTE;
3389 else if (buffer_kbs > 0)
3390 st_write_threshold = st_buffer_size - 2048;
3391 if (st_write_threshold > st_buffer_size) {
3392 st_write_threshold = st_buffer_size;
3393 printk(KERN_WARNING "st: write_threshold limited to %d bytes.\n",
3394 st_write_threshold);
3396 if (max_buffers >= 0)
3397 st_max_buffers = max_buffers;
3398 if (max_sg_segs >= ST_FIRST_SG)
3399 st_max_sg_segs = max_sg_segs;
3402 #ifndef MODULE
3403 /* Set the boot options. Syntax is defined in README.st.
3405 static int __init st_setup(char *str)
3407 int i, len, ints[5];
3408 char *stp;
3410 stp = get_options(str, ARRAY_SIZE(ints), ints);
3412 if (ints[0] > 0) {
3413 for (i = 0; i < ints[0] && i < ARRAY_SIZE(parms); i++)
3414 *parms[i].val = ints[i + 1];
3415 } else {
3416 while (stp != NULL) {
3417 for (i = 0; i < ARRAY_SIZE(parms); i++) {
3418 len = strlen(parms[i].name);
3419 if (!strncmp(stp, parms[i].name, len) &&
3420 (*(stp + len) == ':' || *(stp + len) == '=')) {
3421 *parms[i].val =
3422 simple_strtoul(stp + len + 1, NULL, 0);
3423 break;
3426 if (i >= sizeof(parms) / sizeof(struct st_dev_parm))
3427 printk(KERN_WARNING "st: illegal parameter in '%s'\n",
3428 stp);
3429 stp = strchr(stp, ',');
3430 if (stp)
3431 stp++;
3435 validate_options();
3437 return 1;
3440 __setup("st=", st_setup);
3442 #endif
3445 static struct file_operations st_fops =
3447 owner: THIS_MODULE,
3448 read: st_read,
3449 write: st_write,
3450 ioctl: st_ioctl,
3451 open: st_open,
3452 flush: st_flush,
3453 release: st_release,
3456 static int st_attach(Scsi_Device * SDp)
3458 Scsi_Tape *tpnt;
3459 ST_mode *STm;
3460 ST_partstat *STps;
3461 int i, mode, target_nbr;
3462 unsigned long flags = 0;
3464 if (SDp->type != TYPE_TAPE)
3465 return 1;
3467 write_lock_irqsave(&st_dev_arr_lock, flags);
3468 if (st_template.nr_dev >= st_template.dev_max) {
3469 Scsi_Tape **tmp_da;
3470 ST_buffer **tmp_ba;
3471 int tmp_dev_max;
3473 tmp_dev_max = st_template.nr_dev + ST_DEV_ARR_LUMP;
3474 if (tmp_dev_max > ST_MAX_TAPES)
3475 tmp_dev_max = ST_MAX_TAPES;
3476 if (tmp_dev_max <= st_template.nr_dev) {
3477 SDp->attached--;
3478 write_unlock_irqrestore(&st_dev_arr_lock, flags);
3479 printk(KERN_ERR "st: Too many tape devices (max. %d).\n",
3480 ST_MAX_TAPES);
3481 return 1;
3484 tmp_da = kmalloc(tmp_dev_max * sizeof(Scsi_Tape *), GFP_ATOMIC);
3485 tmp_ba = kmalloc(tmp_dev_max * sizeof(ST_buffer *), GFP_ATOMIC);
3486 if (tmp_da == NULL || tmp_ba == NULL) {
3487 if (tmp_da != NULL)
3488 kfree(tmp_da);
3489 SDp->attached--;
3490 write_unlock_irqrestore(&st_dev_arr_lock, flags);
3491 printk(KERN_ERR "st: Can't extend device array.\n");
3492 return 1;
3495 memset(tmp_da, 0, tmp_dev_max * sizeof(Scsi_Tape *));
3496 if (scsi_tapes != NULL) {
3497 memcpy(tmp_da, scsi_tapes,
3498 st_template.dev_max * sizeof(Scsi_Tape *));
3499 kfree(scsi_tapes);
3501 scsi_tapes = tmp_da;
3503 memset(tmp_ba, 0, tmp_dev_max * sizeof(ST_buffer *));
3504 if (st_buffers != NULL) {
3505 memcpy(tmp_ba, st_buffers,
3506 st_template.dev_max * sizeof(ST_buffer *));
3507 kfree(st_buffers);
3509 st_buffers = tmp_ba;
3511 st_template.dev_max = tmp_dev_max;
3514 for (i = 0; i < st_template.dev_max; i++)
3515 if (scsi_tapes[i] == NULL)
3516 break;
3517 if (i >= st_template.dev_max)
3518 panic("scsi_devices corrupt (st)");
3520 tpnt = kmalloc(sizeof(Scsi_Tape), GFP_ATOMIC);
3521 if (tpnt == NULL) {
3522 SDp->attached--;
3523 write_unlock_irqrestore(&st_dev_arr_lock, flags);
3524 printk(KERN_ERR "st: Can't allocate device descriptor.\n");
3525 return 1;
3527 memset(tpnt, 0, sizeof(Scsi_Tape));
3528 scsi_tapes[i] = tpnt;
3530 for (mode = 0; mode < ST_NBR_MODES; ++mode) {
3531 char name[8];
3532 static char *formats[ST_NBR_MODES] ={"", "l", "m", "a"};
3534 /* Rewind entry */
3535 sprintf (name, "mt%s", formats[mode]);
3536 tpnt->de_r[mode] =
3537 devfs_register (SDp->de, name, DEVFS_FL_DEFAULT,
3538 MAJOR_NR, i + (mode << 5),
3539 S_IFCHR | S_IRUGO | S_IWUGO,
3540 &st_fops, NULL);
3541 /* No-rewind entry */
3542 sprintf (name, "mt%sn", formats[mode]);
3543 tpnt->de_n[mode] =
3544 devfs_register (SDp->de, name, DEVFS_FL_DEFAULT,
3545 MAJOR_NR, i + (mode << 5) + 128,
3546 S_IFCHR | S_IRUGO | S_IWUGO,
3547 &st_fops, NULL);
3549 devfs_register_tape (tpnt->de_r[0]);
3550 tpnt->device = SDp;
3551 if (SDp->scsi_level <= 2)
3552 tpnt->tape_type = MT_ISSCSI1;
3553 else
3554 tpnt->tape_type = MT_ISSCSI2;
3556 tpnt->inited = 0;
3557 tpnt->devt = MKDEV(SCSI_TAPE_MAJOR, i);
3558 tpnt->dirty = 0;
3559 tpnt->in_use = 0;
3560 tpnt->drv_buffer = 1; /* Try buffering if no mode sense */
3561 tpnt->restr_dma = (SDp->host)->unchecked_isa_dma;
3562 tpnt->density = 0;
3563 tpnt->do_auto_lock = ST_AUTO_LOCK;
3564 tpnt->can_bsr = ST_IN_FILE_POS;
3565 tpnt->can_partitions = 0;
3566 tpnt->two_fm = ST_TWO_FM;
3567 tpnt->fast_mteom = ST_FAST_MTEOM;
3568 tpnt->scsi2_logical = ST_SCSI2LOGICAL;
3569 tpnt->write_threshold = st_write_threshold;
3570 tpnt->default_drvbuffer = 0xff; /* No forced buffering */
3571 tpnt->partition = 0;
3572 tpnt->new_partition = 0;
3573 tpnt->nbr_partitions = 0;
3574 tpnt->timeout = ST_TIMEOUT;
3575 tpnt->long_timeout = ST_LONG_TIMEOUT;
3577 for (i = 0; i < ST_NBR_MODES; i++) {
3578 STm = &(tpnt->modes[i]);
3579 STm->defined = FALSE;
3580 STm->sysv = ST_SYSV;
3581 STm->defaults_for_writes = 0;
3582 STm->do_async_writes = ST_ASYNC_WRITES;
3583 STm->do_buffer_writes = ST_BUFFER_WRITES;
3584 STm->do_read_ahead = ST_READ_AHEAD;
3585 STm->default_compression = ST_DONT_TOUCH;
3586 STm->default_blksize = (-1); /* No forced size */
3587 STm->default_density = (-1); /* No forced density */
3590 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
3591 STps = &(tpnt->ps[i]);
3592 STps->rw = ST_IDLE;
3593 STps->eof = ST_NOEOF;
3594 STps->at_sm = 0;
3595 STps->last_block_valid = FALSE;
3596 STps->drv_block = (-1);
3597 STps->drv_file = (-1);
3600 tpnt->current_mode = 0;
3601 tpnt->modes[0].defined = TRUE;
3603 tpnt->density_changed = tpnt->compression_changed =
3604 tpnt->blksize_changed = FALSE;
3605 init_MUTEX(&tpnt->lock);
3607 st_template.nr_dev++;
3608 write_unlock_irqrestore(&st_dev_arr_lock, flags);
3610 /* See if we need to allocate more static buffers */
3611 target_nbr = st_template.nr_dev;
3612 if (target_nbr > st_max_buffers)
3613 target_nbr = st_max_buffers;
3614 for (i=st_nbr_buffers; i < target_nbr; i++)
3615 if (!new_tape_buffer(TRUE, TRUE, FALSE)) {
3616 printk(KERN_INFO "st: Unable to allocate new static buffer.\n");
3617 break;
3619 /* If the previous allocation fails, we will try again when the buffer is
3620 really needed. */
3622 return 0;
3625 static int st_detect(Scsi_Device * SDp)
3627 if (SDp->type != TYPE_TAPE)
3628 return 0;
3630 printk(KERN_WARNING
3631 "Detected scsi tape st%d at scsi%d, channel %d, id %d, lun %d\n",
3632 st_template.dev_noticed++,
3633 SDp->host->host_no, SDp->channel, SDp->id, SDp->lun);
3635 return 1;
3638 static int st_registered = 0;
3640 /* Driver initialization (not __init because may be called later) */
3641 static int st_init()
3643 unsigned long flags;
3645 if (st_template.dev_noticed == 0 || st_registered)
3646 return 0;
3648 printk(KERN_INFO "st: bufsize %d, wrt %d, max init. buffers %d, s/g segs %d.\n",
3649 st_buffer_size, st_write_threshold, st_max_buffers, st_max_sg_segs);
3651 write_lock_irqsave(&st_dev_arr_lock, flags);
3652 if (!st_registered) {
3653 if (devfs_register_chrdev(SCSI_TAPE_MAJOR, "st", &st_fops)) {
3654 write_unlock_irqrestore(&st_dev_arr_lock, flags);
3655 printk(KERN_ERR "Unable to get major %d for SCSI tapes\n",
3656 MAJOR_NR);
3657 return 1;
3659 st_registered++;
3662 st_template.dev_max = 0;
3663 st_nbr_buffers = 0;
3664 write_unlock_irqrestore(&st_dev_arr_lock, flags);
3666 return 0;
3669 static void st_detach(Scsi_Device * SDp)
3671 Scsi_Tape *tpnt;
3672 int i, mode;
3673 unsigned long flags;
3675 write_lock_irqsave(&st_dev_arr_lock, flags);
3676 for (i = 0; i < st_template.dev_max; i++) {
3677 tpnt = scsi_tapes[i];
3678 if (tpnt != NULL && tpnt->device == SDp) {
3679 tpnt->device = NULL;
3680 for (mode = 0; mode < ST_NBR_MODES; ++mode) {
3681 devfs_unregister (tpnt->de_r[mode]);
3682 tpnt->de_r[mode] = NULL;
3683 devfs_unregister (tpnt->de_n[mode]);
3684 tpnt->de_n[mode] = NULL;
3686 kfree(tpnt);
3687 scsi_tapes[i] = 0;
3688 SDp->attached--;
3689 st_template.nr_dev--;
3690 st_template.dev_noticed--;
3691 write_unlock_irqrestore(&st_dev_arr_lock, flags);
3692 return;
3696 write_unlock_irqrestore(&st_dev_arr_lock, flags);
3697 return;
3701 #ifdef MODULE
3703 int __init init_module(void)
3705 validate_options();
3707 st_template.module = &__this_module;
3708 return scsi_register_module(MODULE_SCSI_DEV, &st_template);
3711 void cleanup_module(void)
3713 int i;
3715 scsi_unregister_module(MODULE_SCSI_DEV, &st_template);
3716 devfs_unregister_chrdev(SCSI_TAPE_MAJOR, "st");
3717 st_registered--;
3718 if (scsi_tapes != NULL) {
3719 for (i=0; i < st_template.dev_max; ++i)
3720 if (scsi_tapes[i])
3721 kfree(scsi_tapes[i]);
3722 kfree(scsi_tapes);
3723 if (st_buffers != NULL) {
3724 for (i = 0; i < st_nbr_buffers; i++) {
3725 if (st_buffers[i] != NULL) {
3726 st_buffers[i]->orig_sg_segs = 0;
3727 normalize_buffer(st_buffers[i]);
3728 kfree(st_buffers[i]);
3731 kfree(st_buffers);
3734 st_template.dev_max = 0;
3735 printk(KERN_INFO "st: Unloaded.\n");
3737 #endif /* MODULE */