Import 2.3.18pre1
[davej-history.git] / drivers / scsi / st.c
blob29042cab9e89218cf69d43bf5da93a088e840db6
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, Wolfgang Denk, Steve Hirsch, Andreas Koppenh"ofer,
9 Michael Leodolter, Eyal Lebedinsky, J"org Weule, and Eric Youngdale.
11 Copyright 1992 - 1999 Kai Makisara
12 email Kai.Makisara@metla.fi
14 Last modified: Sat Aug 7 13:54:31 1999 by makisara@kai.makisara.local
15 Some small formal changes - aeb, 950809
18 #include <linux/module.h>
20 #include <linux/fs.h>
21 #include <linux/kernel.h>
22 #include <linux/sched.h>
23 #include <linux/mm.h>
24 #include <linux/init.h>
25 #include <linux/string.h>
26 #include <linux/errno.h>
27 #include <linux/mtio.h>
28 #include <linux/ioctl.h>
29 #include <linux/fcntl.h>
30 #include <linux/spinlock.h>
31 #include <asm/uaccess.h>
32 #include <asm/dma.h>
33 #include <asm/system.h>
35 /* The driver prints some debugging information on the console if DEBUG
36 is defined and non-zero. */
37 #define DEBUG 0
39 /* The message level for the debug messages is currently set to KERN_NOTICE
40 so that people can easily see the messages. Later when the debugging messages
41 in the drivers are more widely classified, this may be changed to KERN_DEBUG. */
42 #define ST_DEB_MSG KERN_NOTICE
44 #define MAJOR_NR SCSI_TAPE_MAJOR
45 #include <linux/blk.h>
47 #include "scsi.h"
48 #include "hosts.h"
49 #include <scsi/scsi_ioctl.h>
51 #define ST_KILOBYTE 1024
53 #include "st_options.h"
54 #include "st.h"
56 #include "constants.h"
58 static int buffer_kbs = 0;
59 static int write_threshold_kbs = 0;
60 static int max_buffers = (-1);
61 static int max_sg_segs = 0;
62 #ifdef MODULE
63 MODULE_AUTHOR("Kai Makisara");
64 MODULE_DESCRIPTION("SCSI Tape Driver");
65 MODULE_PARM(buffer_kbs, "i");
66 MODULE_PARM(write_threshold_kbs, "i");
67 MODULE_PARM(max_buffers, "i");
68 MODULE_PARM(max_sg_segs, "i");
69 #else
71 static struct st_dev_parm {
72 char *name;
73 int *val;
74 } parms[] __initdata = {
76 "buffer_kbs", &buffer_kbs
79 "write_threshold_kbs", &write_threshold_kbs
82 "max_buffers", &max_buffers
85 "max_sg_segs", &max_sg_segs
89 #endif
92 /* The default definitions have been moved to st_options.h */
94 #define ST_BUFFER_SIZE (ST_BUFFER_BLOCKS * ST_KILOBYTE)
95 #define ST_WRITE_THRESHOLD (ST_WRITE_THRESHOLD_BLOCKS * ST_KILOBYTE)
97 /* The buffer size should fit into the 24 bits for length in the
98 6-byte SCSI read and write commands. */
99 #if ST_BUFFER_SIZE >= (2 << 24 - 1)
100 #error "Buffer size should not exceed (2 << 24 - 1) bytes!"
101 #endif
103 #if DEBUG
104 static int debugging = 1;
105 #endif
107 #define MAX_RETRIES 0
108 #define MAX_WRITE_RETRIES 0
109 #define MAX_READY_RETRIES 5
110 #define NO_TAPE NOT_READY
112 #define ST_TIMEOUT (900 * HZ)
113 #define ST_LONG_TIMEOUT (14000 * HZ)
115 #define TAPE_NR(x) (MINOR(x) & ~(128 | ST_MODE_MASK))
116 #define TAPE_MODE(x) ((MINOR(x) & ST_MODE_MASK) >> ST_MODE_SHIFT)
118 /* Internal ioctl to set both density (uppermost 8 bits) and blocksize (lower
119 24 bits) */
120 #define SET_DENS_AND_BLK 0x10001
122 static int st_nbr_buffers;
123 static ST_buffer **st_buffers;
124 static int st_buffer_size = ST_BUFFER_SIZE;
125 static int st_write_threshold = ST_WRITE_THRESHOLD;
126 static int st_max_buffers = ST_MAX_BUFFERS;
127 static int st_max_sg_segs = ST_MAX_SG;
129 static Scsi_Tape *scsi_tapes = NULL;
131 static int modes_defined = FALSE;
133 static ST_buffer *new_tape_buffer(int, int);
134 static int enlarge_buffer(ST_buffer *, int, int);
135 static void normalize_buffer(ST_buffer *);
136 static int append_to_buffer(const char *, ST_buffer *, int);
137 static int from_buffer(ST_buffer *, char *, int);
139 static int st_init(void);
140 static int st_attach(Scsi_Device *);
141 static int st_detect(Scsi_Device *);
142 static void st_detach(Scsi_Device *);
144 struct Scsi_Device_Template st_template =
145 {NULL, "tape", "st", NULL, TYPE_TAPE,
146 SCSI_TAPE_MAJOR, 0, 0, 0, 0,
147 st_detect, st_init,
148 NULL, st_attach, st_detach};
150 static int st_compression(Scsi_Tape *, int);
152 static int find_partition(struct inode *);
153 static int update_partition(struct inode *);
155 static int st_int_ioctl(struct inode *inode, unsigned int cmd_in,
156 unsigned long arg);
161 /* Convert the result to success code */
162 static int st_chk_result(Scsi_Cmnd * SCpnt)
164 int dev = TAPE_NR(SCpnt->request.rq_dev);
165 int result = SCpnt->result;
166 unsigned char *sense = SCpnt->sense_buffer, scode;
167 #if DEBUG
168 const char *stp;
169 #endif
171 if (!result /* && SCpnt->sense_buffer[0] == 0 */ )
172 return 0;
174 if (driver_byte(result) & DRIVER_SENSE)
175 scode = sense[2] & 0x0f;
176 else {
177 sense[0] = 0; /* We don't have sense data if this byte is zero */
178 scode = 0;
181 #if DEBUG
182 if (debugging) {
183 printk(ST_DEB_MSG "st%d: Error: %x, cmd: %x %x %x %x %x %x Len: %d\n",
184 dev, result,
185 SCpnt->data_cmnd[0], SCpnt->data_cmnd[1], SCpnt->data_cmnd[2],
186 SCpnt->data_cmnd[3], SCpnt->data_cmnd[4], SCpnt->data_cmnd[5],
187 SCpnt->request_bufflen);
188 if (driver_byte(result) & DRIVER_SENSE)
189 print_sense("st", SCpnt);
190 } else
191 #endif
192 if (!(driver_byte(result) & DRIVER_SENSE) ||
193 ((sense[0] & 0x70) == 0x70 &&
194 scode != NO_SENSE &&
195 scode != RECOVERED_ERROR &&
196 /* scode != UNIT_ATTENTION && */
197 scode != BLANK_CHECK &&
198 scode != VOLUME_OVERFLOW &&
199 SCpnt->data_cmnd[0] != MODE_SENSE &&
200 SCpnt->data_cmnd[0] != TEST_UNIT_READY)) { /* Abnormal conditions for tape */
201 if (driver_byte(result) & DRIVER_SENSE) {
202 printk(KERN_WARNING "st%d: Error with sense data: ", dev);
203 print_sense("st", SCpnt);
204 } else
205 printk(KERN_WARNING
206 "st%d: Error %x (sugg. bt 0x%x, driver bt 0x%x, host bt 0x%x).\n",
207 dev, result, suggestion(result), driver_byte(result) & DRIVER_MASK,
208 host_byte(result));
210 if ((sense[0] & 0x70) == 0x70 &&
211 scode == RECOVERED_ERROR
212 #if ST_RECOVERED_WRITE_FATAL
213 && SCpnt->data_cmnd[0] != WRITE_6
214 && SCpnt->data_cmnd[0] != WRITE_FILEMARKS
215 #endif
217 scsi_tapes[dev].recover_count++;
218 scsi_tapes[dev].mt_status->mt_erreg += (1 << MT_ST_SOFTERR_SHIFT);
219 #if DEBUG
220 if (debugging) {
221 if (SCpnt->data_cmnd[0] == READ_6)
222 stp = "read";
223 else if (SCpnt->data_cmnd[0] == WRITE_6)
224 stp = "write";
225 else
226 stp = "ioctl";
227 printk(ST_DEB_MSG "st%d: Recovered %s error (%d).\n", dev, stp,
228 scsi_tapes[dev].recover_count);
230 #endif
231 if ((sense[2] & 0xe0) == 0)
232 return 0;
234 return (-EIO);
238 /* Wakeup from interrupt */
239 static void st_sleep_done(Scsi_Cmnd * SCpnt)
241 unsigned int st_nbr;
242 int remainder;
243 Scsi_Tape *STp;
245 if ((st_nbr = TAPE_NR(SCpnt->request.rq_dev)) < st_template.nr_dev) {
246 STp = &(scsi_tapes[st_nbr]);
247 if ((STp->buffer)->writing &&
248 (SCpnt->sense_buffer[0] & 0x70) == 0x70 &&
249 (SCpnt->sense_buffer[2] & 0x40)) {
250 /* EOM at write-behind, has all been written? */
251 if ((SCpnt->sense_buffer[0] & 0x80) != 0)
252 remainder = (SCpnt->sense_buffer[3] << 24) |
253 (SCpnt->sense_buffer[4] << 16) |
254 (SCpnt->sense_buffer[5] << 8) | SCpnt->sense_buffer[6];
255 else
256 remainder = 0;
257 if ((SCpnt->sense_buffer[2] & 0x0f) == VOLUME_OVERFLOW ||
258 remainder > 0)
259 (STp->buffer)->last_result = SCpnt->result; /* Error */
260 else
261 (STp->buffer)->last_result = INT_MAX; /* OK */
262 } else
263 (STp->buffer)->last_result = SCpnt->result;
264 SCpnt->request.rq_status = RQ_SCSI_DONE;
265 (STp->buffer)->last_SCpnt = SCpnt;
267 #if DEBUG
268 STp->write_pending = 0;
269 #endif
270 up(SCpnt->request.sem);
272 #if DEBUG
273 else if (debugging)
274 printk(KERN_ERR "st?: Illegal interrupt device %x\n", st_nbr);
275 #endif
279 /* Do the scsi command. Waits until command performed if do_wait is true.
280 Otherwise write_behind_check() is used to check that the command
281 has finished. */
282 static Scsi_Cmnd *
283 st_do_scsi(Scsi_Cmnd * SCpnt, Scsi_Tape * STp, unsigned char *cmd, int bytes,
284 int timeout, int retries, int do_wait)
286 unsigned long flags;
287 unsigned char *bp;
289 spin_lock_irqsave(&io_request_lock, flags);
290 if (SCpnt == NULL)
291 if ((SCpnt = scsi_allocate_device(NULL, STp->device, 1)) == NULL) {
292 printk(KERN_ERR "st%d: Can't get SCSI request.\n", TAPE_NR(STp->devt));
293 spin_unlock_irqrestore(&io_request_lock, flags);
294 return NULL;
296 cmd[1] |= (SCpnt->lun << 5) & 0xe0;
297 init_MUTEX_LOCKED(&STp->sem);
298 SCpnt->use_sg = (bytes > (STp->buffer)->sg[0].length) ?
299 (STp->buffer)->use_sg : 0;
300 if (SCpnt->use_sg) {
301 bp = (char *) &((STp->buffer)->sg[0]);
302 if ((STp->buffer)->sg_segs < SCpnt->use_sg)
303 SCpnt->use_sg = (STp->buffer)->sg_segs;
304 } else
305 bp = (STp->buffer)->b_data;
306 SCpnt->cmd_len = 0;
307 SCpnt->request.sem = &(STp->sem);
308 SCpnt->request.rq_status = RQ_SCSI_BUSY;
309 SCpnt->request.rq_dev = STp->devt;
311 scsi_do_cmd(SCpnt, (void *) cmd, bp, bytes,
312 st_sleep_done, timeout, retries);
313 spin_unlock_irqrestore(&io_request_lock, flags);
315 if (do_wait) {
316 down(SCpnt->request.sem);
318 (STp->buffer)->last_result_fatal = st_chk_result(SCpnt);
320 return SCpnt;
324 /* Handle the write-behind checking (downs the semaphore) */
325 static void write_behind_check(Scsi_Tape * STp)
327 ST_buffer *STbuffer;
328 ST_partstat *STps;
330 STbuffer = STp->buffer;
332 #if DEBUG
333 if (STp->write_pending)
334 STp->nbr_waits++;
335 else
336 STp->nbr_finished++;
337 #endif
339 down(&(STp->sem));
341 (STp->buffer)->last_result_fatal = st_chk_result((STp->buffer)->last_SCpnt);
342 scsi_release_command((STp->buffer)->last_SCpnt);
344 if (STbuffer->writing < STbuffer->buffer_bytes)
345 #if 0
346 memcpy(STbuffer->b_data,
347 STbuffer->b_data + STbuffer->writing,
348 STbuffer->buffer_bytes - STbuffer->writing);
349 #else
350 printk(KERN_WARNING "st: write_behind_check: something left in buffer!\n");
351 #endif
352 STbuffer->buffer_bytes -= STbuffer->writing;
353 STps = &(STp->ps[STp->partition]);
354 if (STps->drv_block >= 0) {
355 if (STp->block_size == 0)
356 STps->drv_block++;
357 else
358 STps->drv_block += STbuffer->writing / STp->block_size;
360 STbuffer->writing = 0;
362 return;
366 /* Step over EOF if it has been inadvertently crossed (ioctl not used because
367 it messes up the block number). */
368 static int cross_eof(Scsi_Tape * STp, int forward)
370 Scsi_Cmnd *SCpnt;
371 unsigned char cmd[10];
373 cmd[0] = SPACE;
374 cmd[1] = 0x01; /* Space FileMarks */
375 if (forward) {
376 cmd[2] = cmd[3] = 0;
377 cmd[4] = 1;
378 } else
379 cmd[2] = cmd[3] = cmd[4] = 0xff; /* -1 filemarks */
380 cmd[5] = 0;
381 #if DEBUG
382 if (debugging)
383 printk(ST_DEB_MSG "st%d: Stepping over filemark %s.\n",
384 TAPE_NR(STp->devt), forward ? "forward" : "backward");
385 #endif
387 SCpnt = st_do_scsi(NULL, STp, cmd, 0, STp->timeout, MAX_RETRIES, TRUE);
388 if (!SCpnt)
389 return (-EBUSY);
391 scsi_release_command(SCpnt);
392 SCpnt = NULL;
394 if ((STp->buffer)->last_result != 0)
395 printk(KERN_ERR "st%d: Stepping over filemark %s failed.\n",
396 TAPE_NR(STp->devt), forward ? "forward" : "backward");
398 return (STp->buffer)->last_result_fatal;
402 /* Flush the write buffer (never need to write if variable blocksize). */
403 static int flush_write_buffer(Scsi_Tape * STp)
405 int offset, transfer, blks;
406 int result;
407 unsigned char cmd[10];
408 Scsi_Cmnd *SCpnt;
409 ST_partstat *STps;
411 if ((STp->buffer)->writing) {
412 write_behind_check(STp);
413 if ((STp->buffer)->last_result_fatal) {
414 #if DEBUG
415 if (debugging)
416 printk(ST_DEB_MSG "st%d: Async write error (flush) %x.\n",
417 TAPE_NR(STp->devt), (STp->buffer)->last_result);
418 #endif
419 if ((STp->buffer)->last_result == INT_MAX)
420 return (-ENOSPC);
421 return (-EIO);
424 if (STp->block_size == 0)
425 return 0;
427 result = 0;
428 if (STp->dirty == 1) {
430 offset = (STp->buffer)->buffer_bytes;
431 transfer = ((offset + STp->block_size - 1) /
432 STp->block_size) * STp->block_size;
433 #if DEBUG
434 if (debugging)
435 printk(ST_DEB_MSG "st%d: Flushing %d bytes.\n", TAPE_NR(STp->devt), transfer);
436 #endif
437 memset((STp->buffer)->b_data + offset, 0, transfer - offset);
439 memset(cmd, 0, 10);
440 cmd[0] = WRITE_6;
441 cmd[1] = 1;
442 blks = transfer / STp->block_size;
443 cmd[2] = blks >> 16;
444 cmd[3] = blks >> 8;
445 cmd[4] = blks;
447 SCpnt = st_do_scsi(NULL, STp, cmd, transfer, STp->timeout, MAX_WRITE_RETRIES,
448 TRUE);
449 if (!SCpnt)
450 return (-EBUSY);
452 STps = &(STp->ps[STp->partition]);
453 if ((STp->buffer)->last_result_fatal != 0) {
454 if ((SCpnt->sense_buffer[0] & 0x70) == 0x70 &&
455 (SCpnt->sense_buffer[2] & 0x40) &&
456 (SCpnt->sense_buffer[2] & 0x0f) == NO_SENSE) {
457 STp->dirty = 0;
458 (STp->buffer)->buffer_bytes = 0;
459 result = (-ENOSPC);
460 } else {
461 printk(KERN_ERR "st%d: Error on flush.\n", TAPE_NR(STp->devt));
462 result = (-EIO);
464 STps->drv_block = (-1);
465 } else {
466 if (STps->drv_block >= 0)
467 STps->drv_block += blks;
468 STp->dirty = 0;
469 (STp->buffer)->buffer_bytes = 0;
471 scsi_release_command(SCpnt);
472 SCpnt = NULL;
474 return result;
478 /* Flush the tape buffer. The tape will be positioned correctly unless
479 seek_next is true. */
480 static int flush_buffer(struct inode *inode, struct file *filp, int seek_next)
482 int backspace, result;
483 Scsi_Tape *STp;
484 ST_buffer *STbuffer;
485 ST_partstat *STps;
486 int dev = TAPE_NR(inode->i_rdev);
488 STp = &(scsi_tapes[dev]);
489 STbuffer = STp->buffer;
492 * If there was a bus reset, block further access
493 * to this device.
495 if (STp->device->was_reset)
496 return (-EIO);
498 if (STp->ready != ST_READY)
499 return 0;
501 STps = &(STp->ps[STp->partition]);
502 if (STps->rw == ST_WRITING) /* Writing */
503 return flush_write_buffer(STp);
505 if (STp->block_size == 0)
506 return 0;
508 backspace = ((STp->buffer)->buffer_bytes +
509 (STp->buffer)->read_pointer) / STp->block_size -
510 ((STp->buffer)->read_pointer + STp->block_size - 1) /
511 STp->block_size;
512 (STp->buffer)->buffer_bytes = 0;
513 (STp->buffer)->read_pointer = 0;
514 result = 0;
515 if (!seek_next) {
516 if (STps->eof == ST_FM_HIT) {
517 result = cross_eof(STp, FALSE); /* Back over the EOF hit */
518 if (!result)
519 STps->eof = ST_NOEOF;
520 else {
521 if (STps->drv_file >= 0)
522 STps->drv_file++;
523 STps->drv_block = 0;
526 if (!result && backspace > 0)
527 result = st_int_ioctl(inode, MTBSR, backspace);
528 } else if (STps->eof == ST_FM_HIT) {
529 if (STps->drv_file >= 0)
530 STps->drv_file++;
531 STps->drv_block = 0;
532 STps->eof = ST_NOEOF;
534 return result;
538 /* Set the mode parameters */
539 static int set_mode_densblk(struct inode *inode, Scsi_Tape * STp, ST_mode * STm)
541 int set_it = FALSE;
542 unsigned long arg;
543 int dev = TAPE_NR(inode->i_rdev);
545 if (!STp->density_changed &&
546 STm->default_density >= 0 &&
547 STm->default_density != STp->density) {
548 arg = STm->default_density;
549 set_it = TRUE;
550 } else
551 arg = STp->density;
552 arg <<= MT_ST_DENSITY_SHIFT;
553 if (!STp->blksize_changed &&
554 STm->default_blksize >= 0 &&
555 STm->default_blksize != STp->block_size) {
556 arg |= STm->default_blksize;
557 set_it = TRUE;
558 } else
559 arg |= STp->block_size;
560 if (set_it &&
561 st_int_ioctl(inode, SET_DENS_AND_BLK, arg)) {
562 printk(KERN_WARNING
563 "st%d: Can't set default block size to %d bytes and density %x.\n",
564 dev, STm->default_blksize, STm->default_density);
565 if (modes_defined)
566 return (-EINVAL);
568 return 0;
572 /* Open the device */
573 static int scsi_tape_open(struct inode *inode, struct file *filp)
575 unsigned short flags;
576 int i, need_dma_buffer, new_session = FALSE;
577 unsigned char cmd[10];
578 Scsi_Cmnd *SCpnt;
579 Scsi_Tape *STp;
580 ST_mode *STm;
581 ST_partstat *STps;
582 int dev = TAPE_NR(inode->i_rdev);
583 int mode = TAPE_MODE(inode->i_rdev);
585 if (dev >= st_template.dev_max || !scsi_tapes[dev].device)
586 return (-ENXIO);
588 if (!scsi_block_when_processing_errors(scsi_tapes[dev].device)) {
589 return -ENXIO;
591 STp = &(scsi_tapes[dev]);
592 if (STp->in_use) {
593 #if DEBUG
594 printk(ST_DEB_MSG "st%d: Device already in use.\n", dev);
595 #endif
596 return (-EBUSY);
598 STp->rew_at_close = (MINOR(inode->i_rdev) & 0x80) == 0;
600 if (mode != STp->current_mode) {
601 #if DEBUG
602 if (debugging)
603 printk(ST_DEB_MSG "st%d: Mode change from %d to %d.\n",
604 dev, STp->current_mode, mode);
605 #endif
606 new_session = TRUE;
607 STp->current_mode = mode;
609 STm = &(STp->modes[STp->current_mode]);
611 /* Allocate a buffer for this user */
612 need_dma_buffer = STp->restr_dma;
613 for (i = 0; i < st_nbr_buffers; i++)
614 if (!st_buffers[i]->in_use &&
615 (!need_dma_buffer || st_buffers[i]->dma))
616 break;
617 if (i >= st_nbr_buffers) {
618 STp->buffer = new_tape_buffer(FALSE, need_dma_buffer);
619 if (STp->buffer == NULL) {
620 printk(KERN_WARNING "st%d: Can't allocate tape buffer.\n", dev);
621 return (-EBUSY);
623 } else
624 STp->buffer = st_buffers[i];
625 (STp->buffer)->in_use = 1;
626 (STp->buffer)->writing = 0;
627 (STp->buffer)->last_result_fatal = 0;
628 (STp->buffer)->use_sg = STp->device->host->sg_tablesize;
630 /* Compute the usable buffer size for this SCSI adapter */
631 if (!(STp->buffer)->use_sg)
632 (STp->buffer)->buffer_size = (STp->buffer)->sg[0].length;
633 else {
634 for (i = 0, (STp->buffer)->buffer_size = 0; i < (STp->buffer)->use_sg &&
635 i < (STp->buffer)->sg_segs; i++)
636 (STp->buffer)->buffer_size += (STp->buffer)->sg[i].length;
639 flags = filp->f_flags;
640 STp->write_prot = ((flags & O_ACCMODE) == O_RDONLY);
642 STp->dirty = 0;
643 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
644 STps = &(STp->ps[i]);
645 STps->rw = ST_IDLE;
647 STp->ready = ST_READY;
648 STp->recover_count = 0;
649 #if DEBUG
650 STp->nbr_waits = STp->nbr_finished = 0;
651 #endif
653 if (scsi_tapes[dev].device->host->hostt->module)
654 __MOD_INC_USE_COUNT(scsi_tapes[dev].device->host->hostt->module);
655 if (st_template.module)
656 __MOD_INC_USE_COUNT(st_template.module);
658 memset((void *) &cmd[0], 0, 10);
659 cmd[0] = TEST_UNIT_READY;
661 SCpnt = st_do_scsi(NULL, STp, cmd, 0, STp->long_timeout, MAX_READY_RETRIES,
662 TRUE);
663 if (!SCpnt) {
664 if (scsi_tapes[dev].device->host->hostt->module)
665 __MOD_DEC_USE_COUNT(scsi_tapes[dev].device->host->hostt->module);
666 if (st_template.module)
667 __MOD_DEC_USE_COUNT(st_template.module);
668 return (-EBUSY);
670 if ((SCpnt->sense_buffer[0] & 0x70) == 0x70 &&
671 (SCpnt->sense_buffer[2] & 0x0f) == UNIT_ATTENTION) { /* New media? */
672 memset((void *) &cmd[0], 0, 10);
673 cmd[0] = TEST_UNIT_READY;
675 SCpnt = st_do_scsi(SCpnt, STp, cmd, 0, STp->long_timeout, MAX_READY_RETRIES,
676 TRUE);
678 (STp->device)->was_reset = 0;
679 STp->partition = STp->new_partition = 0;
680 if (STp->can_partitions)
681 STp->nbr_partitions = 1; /* This guess will be updated later if necessary */
682 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
683 STps = &(STp->ps[i]);
684 STps->rw = ST_IDLE;
685 STps->eof = ST_NOEOF;
686 STps->at_sm = 0;
687 STps->last_block_valid = FALSE;
688 STps->drv_block = 0;
689 STps->drv_file = 0;
691 new_session = TRUE;
693 if ((STp->buffer)->last_result_fatal != 0) {
694 if ((STp->device)->scsi_level >= SCSI_2 &&
695 (SCpnt->sense_buffer[0] & 0x70) == 0x70 &&
696 (SCpnt->sense_buffer[2] & 0x0f) == NOT_READY &&
697 SCpnt->sense_buffer[12] == 0x3a) { /* Check ASC */
698 STp->ready = ST_NO_TAPE;
699 } else
700 STp->ready = ST_NOT_READY;
701 scsi_release_command(SCpnt);
702 SCpnt = NULL;
703 STp->density = 0; /* Clear the erroneous "residue" */
704 STp->write_prot = 0;
705 STp->block_size = 0;
706 STp->ps[0].drv_file = STp->ps[0].drv_block = (-1);
707 STp->partition = STp->new_partition = 0;
708 STp->door_locked = ST_UNLOCKED;
709 STp->in_use = 1;
710 return 0;
712 if (STp->omit_blklims)
713 STp->min_block = STp->max_block = (-1);
714 else {
715 memset((void *) &cmd[0], 0, 10);
716 cmd[0] = READ_BLOCK_LIMITS;
718 SCpnt = st_do_scsi(SCpnt, STp, cmd, 6, STp->timeout, MAX_READY_RETRIES, TRUE);
720 if (!SCpnt->result && !SCpnt->sense_buffer[0]) {
721 STp->max_block = ((STp->buffer)->b_data[1] << 16) |
722 ((STp->buffer)->b_data[2] << 8) | (STp->buffer)->b_data[3];
723 STp->min_block = ((STp->buffer)->b_data[4] << 8) |
724 (STp->buffer)->b_data[5];
725 #if DEBUG
726 if (debugging)
727 printk(ST_DEB_MSG "st%d: Block limits %d - %d bytes.\n", dev, STp->min_block,
728 STp->max_block);
729 #endif
730 } else {
731 STp->min_block = STp->max_block = (-1);
732 #if DEBUG
733 if (debugging)
734 printk(ST_DEB_MSG "st%d: Can't read block limits.\n", dev);
735 #endif
739 memset((void *) &cmd[0], 0, 10);
740 cmd[0] = MODE_SENSE;
741 cmd[4] = 12;
743 SCpnt = st_do_scsi(SCpnt, STp, cmd, 12, STp->timeout, MAX_READY_RETRIES, TRUE);
745 if ((STp->buffer)->last_result_fatal != 0) {
746 #if DEBUG
747 if (debugging)
748 printk(ST_DEB_MSG "st%d: No Mode Sense.\n", dev);
749 #endif
750 STp->block_size = ST_DEFAULT_BLOCK; /* Educated guess (?) */
751 (STp->buffer)->last_result_fatal = 0; /* Prevent error propagation */
752 STp->drv_write_prot = 0;
753 } else {
755 #if DEBUG
756 if (debugging)
757 printk(ST_DEB_MSG "st%d: Mode sense. Length %d, medium %x, WBS %x, BLL %d\n",
758 dev,
759 (STp->buffer)->b_data[0], (STp->buffer)->b_data[1],
760 (STp->buffer)->b_data[2], (STp->buffer)->b_data[3]);
761 #endif
763 if ((STp->buffer)->b_data[3] >= 8) {
764 STp->drv_buffer = ((STp->buffer)->b_data[2] >> 4) & 7;
765 STp->density = (STp->buffer)->b_data[4];
766 STp->block_size = (STp->buffer)->b_data[9] * 65536 +
767 (STp->buffer)->b_data[10] * 256 + (STp->buffer)->b_data[11];
768 #if DEBUG
769 if (debugging)
770 printk(ST_DEB_MSG "st%d: Density %x, tape length: %x, drv buffer: %d\n",
771 dev, STp->density, (STp->buffer)->b_data[5] * 65536 +
772 (STp->buffer)->b_data[6] * 256 + (STp->buffer)->b_data[7],
773 STp->drv_buffer);
774 #endif
776 if (STp->block_size > (STp->buffer)->buffer_size &&
777 !enlarge_buffer(STp->buffer, STp->block_size, STp->restr_dma)) {
778 printk(KERN_NOTICE "st%d: Blocksize %d too large for buffer.\n", dev,
779 STp->block_size);
780 scsi_release_command(SCpnt);
781 (STp->buffer)->in_use = 0;
782 STp->buffer = NULL;
783 if (scsi_tapes[dev].device->host->hostt->module)
784 __MOD_DEC_USE_COUNT(scsi_tapes[dev].device->host->hostt->module);
785 if (st_template.module)
786 __MOD_DEC_USE_COUNT(st_template.module);
787 return (-EIO);
789 STp->drv_write_prot = ((STp->buffer)->b_data[2] & 0x80) != 0;
791 scsi_release_command(SCpnt);
792 SCpnt = NULL;
794 if (STp->block_size > 0)
795 (STp->buffer)->buffer_blocks = (STp->buffer)->buffer_size / STp->block_size;
796 else
797 (STp->buffer)->buffer_blocks = 1;
798 (STp->buffer)->buffer_bytes = (STp->buffer)->read_pointer = 0;
800 #if DEBUG
801 if (debugging)
802 printk(ST_DEB_MSG "st%d: Block size: %d, buffer size: %d (%d blocks).\n", dev,
803 STp->block_size, (STp->buffer)->buffer_size,
804 (STp->buffer)->buffer_blocks);
805 #endif
807 if (STp->drv_write_prot) {
808 STp->write_prot = 1;
809 #if DEBUG
810 if (debugging)
811 printk(ST_DEB_MSG "st%d: Write protected\n", dev);
812 #endif
813 if ((flags & O_ACCMODE) == O_WRONLY || (flags & O_ACCMODE) == O_RDWR) {
814 (STp->buffer)->in_use = 0;
815 STp->buffer = NULL;
816 if (scsi_tapes[dev].device->host->hostt->module)
817 __MOD_DEC_USE_COUNT(scsi_tapes[dev].device->host->hostt->module);
818 if (st_template.module)
819 __MOD_DEC_USE_COUNT(st_template.module);
820 return (-EROFS);
823 if (STp->can_partitions && STp->nbr_partitions < 1) {
824 /* This code is reached when the device is opened for the first time
825 after the driver has been initialized with tape in the drive and the
826 partition support has been enabled. */
827 #if DEBUG
828 if (debugging)
829 printk(ST_DEB_MSG "st%d: Updating partition number in status.\n", dev);
830 #endif
831 if ((STp->partition = find_partition(inode)) < 0) {
832 (STp->buffer)->in_use = 0;
833 STp->buffer = NULL;
834 if (scsi_tapes[dev].device->host->hostt->module)
835 __MOD_DEC_USE_COUNT(scsi_tapes[dev].device->host->hostt->module);
836 if (st_template.module)
837 __MOD_DEC_USE_COUNT(st_template.module);
838 return STp->partition;
840 STp->new_partition = STp->partition;
841 STp->nbr_partitions = 1; /* This guess will be updated when necessary */
843 if (new_session) { /* Change the drive parameters for the new mode */
844 STp->density_changed = STp->blksize_changed = FALSE;
845 STp->compression_changed = FALSE;
846 if (!(STm->defaults_for_writes) &&
847 (i = set_mode_densblk(inode, STp, STm)) < 0) {
848 (STp->buffer)->in_use = 0;
849 STp->buffer = NULL;
850 if (scsi_tapes[dev].device->host->hostt->module)
851 __MOD_DEC_USE_COUNT(scsi_tapes[dev].device->host->hostt->module);
852 if (st_template.module)
853 __MOD_DEC_USE_COUNT(st_template.module);
854 return i;
856 if (STp->default_drvbuffer != 0xff) {
857 if (st_int_ioctl(inode, MTSETDRVBUFFER, STp->default_drvbuffer))
858 printk(KERN_WARNING "st%d: Can't set default drive buffering to %d.\n",
859 dev, STp->default_drvbuffer);
862 STp->in_use = 1;
864 return 0;
868 /* Flush the tape buffer before close */
869 static int scsi_tape_flush(struct file *filp)
871 int result = 0, result2;
872 static unsigned char cmd[10];
873 Scsi_Cmnd *SCpnt;
874 Scsi_Tape *STp;
875 ST_mode *STm;
876 ST_partstat *STps;
878 struct inode *inode = filp->f_dentry->d_inode;
879 kdev_t devt = inode->i_rdev;
880 int dev;
882 if (file_count(filp) > 1)
883 return 0;
885 dev = TAPE_NR(devt);
886 STp = &(scsi_tapes[dev]);
887 STm = &(STp->modes[STp->current_mode]);
888 STps = &(STp->ps[STp->partition]);
890 if (STp->can_partitions &&
891 (result = update_partition(inode)) < 0) {
892 #if DEBUG
893 if (debugging)
894 printk(ST_DEB_MSG "st%d: update_partition at close failed.\n", dev);
895 #endif
896 goto out;
898 if (STps->rw == ST_WRITING && !(STp->device)->was_reset) {
900 result = flush_write_buffer(STp);
902 #if DEBUG
903 if (debugging) {
904 printk(ST_DEB_MSG "st%d: File length %ld bytes.\n",
905 dev, (long) (filp->f_pos));
906 printk(ST_DEB_MSG "st%d: Async write waits %d, finished %d.\n",
907 dev, STp->nbr_waits, STp->nbr_finished);
909 #endif
911 if (result == 0 || result == (-ENOSPC)) {
913 memset(cmd, 0, 10);
914 cmd[0] = WRITE_FILEMARKS;
915 cmd[4] = 1 + STp->two_fm;
917 SCpnt = st_do_scsi(NULL, STp, cmd, 0, STp->timeout, MAX_WRITE_RETRIES,
918 TRUE);
919 if (!SCpnt)
920 goto out;
922 if ((STp->buffer)->last_result_fatal != 0 &&
923 ((SCpnt->sense_buffer[0] & 0x70) != 0x70 ||
924 (SCpnt->sense_buffer[2] & 0x4f) != 0x40 ||
925 ((SCpnt->sense_buffer[0] & 0x80) != 0 &&
926 (SCpnt->sense_buffer[3] | SCpnt->sense_buffer[4] |
927 SCpnt->sense_buffer[5] |
928 SCpnt->sense_buffer[6]) == 0))) {
929 /* Filter out successful write at EOM */
930 scsi_release_command(SCpnt);
931 SCpnt = NULL;
932 printk(KERN_ERR "st%d: Error on write filemark.\n", dev);
933 if (result == 0)
934 result = (-EIO);
935 } else {
936 scsi_release_command(SCpnt);
937 SCpnt = NULL;
938 if (STps->drv_file >= 0)
939 STps->drv_file++;
940 STps->drv_block = 0;
941 if (STp->two_fm)
942 cross_eof(STp, FALSE);
943 STps->eof = ST_FM;
946 #if DEBUG
947 if (debugging)
948 printk(ST_DEB_MSG "st%d: Buffer flushed, %d EOF(s) written\n",
949 dev, cmd[4]);
950 #endif
951 } else if (!STp->rew_at_close) {
952 STps = &(STp->ps[STp->partition]);
953 if (!STm->sysv || STps->rw != ST_READING) {
954 if (STp->can_bsr)
955 result = flush_buffer(inode, filp, 0);
956 else if (STps->eof == ST_FM_HIT) {
957 result = cross_eof(STp, FALSE);
958 if (result) {
959 if (STps->drv_file >= 0)
960 STps->drv_file++;
961 STps->drv_block = 0;
962 STps->eof = ST_FM;
963 } else
964 STps->eof = ST_NOEOF;
966 } else if ((STps->eof == ST_NOEOF &&
967 !(result = cross_eof(STp, TRUE))) ||
968 STps->eof == ST_FM_HIT) {
969 if (STps->drv_file >= 0)
970 STps->drv_file++;
971 STps->drv_block = 0;
972 STps->eof = ST_FM;
975 out:
976 if (STp->rew_at_close) {
977 result2 = st_int_ioctl(inode, MTREW, 1);
978 if (result == 0)
979 result = result2;
981 return result;
985 /* Close the device and release it */
986 static int scsi_tape_close(struct inode *inode, struct file *filp)
988 int result = 0;
989 Scsi_Tape *STp;
991 kdev_t devt = inode->i_rdev;
992 int dev;
994 dev = TAPE_NR(devt);
995 STp = &(scsi_tapes[dev]);
997 if (STp->door_locked == ST_LOCKED_AUTO)
998 st_int_ioctl(inode, MTUNLOCK, 0);
1000 if (STp->buffer != NULL) {
1001 normalize_buffer(STp->buffer);
1002 (STp->buffer)->in_use = 0;
1004 STp->in_use = 0;
1005 if (scsi_tapes[dev].device->host->hostt->module)
1006 __MOD_DEC_USE_COUNT(scsi_tapes[dev].device->host->hostt->module);
1007 if (st_template.module)
1008 __MOD_DEC_USE_COUNT(st_template.module);
1010 return result;
1014 /* Write command */
1015 static ssize_t
1016 st_write(struct file *filp, const char *buf, size_t count, loff_t * ppos)
1018 struct inode *inode = filp->f_dentry->d_inode;
1019 ssize_t total;
1020 ssize_t i, do_count, blks, retval, transfer;
1021 int write_threshold;
1022 int doing_write = 0;
1023 static unsigned char cmd[10];
1024 const char *b_point;
1025 Scsi_Cmnd *SCpnt = NULL;
1026 Scsi_Tape *STp;
1027 ST_mode *STm;
1028 ST_partstat *STps;
1029 int dev = TAPE_NR(inode->i_rdev);
1031 STp = &(scsi_tapes[dev]);
1034 * If we are in the middle of error recovery, don't let anyone
1035 * else try and use this device. Also, if error recovery fails, it
1036 * may try and take the device offline, in which case all further
1037 * access to the device is prohibited.
1039 if (!scsi_block_when_processing_errors(STp->device)) {
1040 return -ENXIO;
1042 if (ppos != &filp->f_pos) {
1043 /* "A request was outside the capabilities of the device." */
1044 return -ENXIO;
1046 if (STp->ready != ST_READY) {
1047 if (STp->ready == ST_NO_TAPE)
1048 return (-ENOMEDIUM);
1049 else
1050 return (-EIO);
1052 STm = &(STp->modes[STp->current_mode]);
1053 if (!STm->defined)
1054 return (-ENXIO);
1055 if (count == 0)
1056 return 0;
1059 * If there was a bus reset, block further access
1060 * to this device.
1062 if (STp->device->was_reset)
1063 return (-EIO);
1065 #if DEBUG
1066 if (!STp->in_use) {
1067 printk(ST_DEB_MSG "st%d: Incorrect device.\n", dev);
1068 return (-EIO);
1070 #endif
1072 /* Write must be integral number of blocks */
1073 if (STp->block_size != 0 && (count % STp->block_size) != 0) {
1074 printk(KERN_WARNING "st%d: Write not multiple of tape block size.\n",
1075 dev);
1076 return (-EIO);
1078 if (STp->can_partitions &&
1079 (retval = update_partition(inode)) < 0)
1080 return retval;
1081 STps = &(STp->ps[STp->partition]);
1083 if (STp->write_prot)
1084 return (-EACCES);
1086 if (STp->block_size == 0 &&
1087 count > (STp->buffer)->buffer_size &&
1088 !enlarge_buffer(STp->buffer, count, STp->restr_dma))
1089 return (-EOVERFLOW);
1091 if (STp->do_auto_lock && STp->door_locked == ST_UNLOCKED &&
1092 !st_int_ioctl(inode, MTLOCK, 0))
1093 STp->door_locked = ST_LOCKED_AUTO;
1095 if (STps->rw == ST_READING) {
1096 retval = flush_buffer(inode, filp, 0);
1097 if (retval)
1098 return retval;
1099 STps->rw = ST_WRITING;
1100 } else if (STps->rw != ST_WRITING &&
1101 STps->drv_file == 0 && STps->drv_block == 0) {
1102 if ((retval = set_mode_densblk(inode, STp, STm)) < 0)
1103 return retval;
1104 if (STm->default_compression != ST_DONT_TOUCH &&
1105 !(STp->compression_changed)) {
1106 if (st_compression(STp, (STm->default_compression == ST_YES))) {
1107 printk(KERN_WARNING "st%d: Can't set default compression.\n",
1108 dev);
1109 if (modes_defined)
1110 return (-EINVAL);
1114 if ((STp->buffer)->writing) {
1115 write_behind_check(STp);
1116 if ((STp->buffer)->last_result_fatal) {
1117 #if DEBUG
1118 if (debugging)
1119 printk(ST_DEB_MSG "st%d: Async write error (write) %x.\n", dev,
1120 (STp->buffer)->last_result);
1121 #endif
1122 if ((STp->buffer)->last_result == INT_MAX)
1123 STps->eof = ST_EOM_OK;
1124 else
1125 STps->eof = ST_EOM_ERROR;
1128 if (STps->eof == ST_EOM_OK)
1129 return (-ENOSPC);
1130 else if (STps->eof == ST_EOM_ERROR)
1131 return (-EIO);
1133 /* Check the buffer readability in cases where copy_user might catch
1134 the problems after some tape movement. */
1135 if (STp->block_size != 0 &&
1136 (copy_from_user(&i, buf, 1) != 0 ||
1137 copy_from_user(&i, buf + count - 1, 1) != 0))
1138 return (-EFAULT);
1140 if (!STm->do_buffer_writes) {
1141 #if 0
1142 if (STp->block_size != 0 && (count % STp->block_size) != 0)
1143 return (-EIO); /* Write must be integral number of blocks */
1144 #endif
1145 write_threshold = 1;
1146 } else
1147 write_threshold = (STp->buffer)->buffer_blocks * STp->block_size;
1148 if (!STm->do_async_writes)
1149 write_threshold--;
1151 total = count;
1153 memset(cmd, 0, 10);
1154 cmd[0] = WRITE_6;
1155 cmd[1] = (STp->block_size != 0);
1157 STps->rw = ST_WRITING;
1159 b_point = buf;
1160 while ((STp->block_size == 0 && !STm->do_async_writes && count > 0) ||
1161 (STp->block_size != 0 &&
1162 (STp->buffer)->buffer_bytes + count > write_threshold)) {
1163 doing_write = 1;
1164 if (STp->block_size == 0)
1165 do_count = count;
1166 else {
1167 do_count = (STp->buffer)->buffer_blocks * STp->block_size -
1168 (STp->buffer)->buffer_bytes;
1169 if (do_count > count)
1170 do_count = count;
1173 i = append_to_buffer(b_point, STp->buffer, do_count);
1174 if (i) {
1175 if (SCpnt != NULL) {
1176 scsi_release_command(SCpnt);
1177 SCpnt = NULL;
1179 return i;
1181 if (STp->block_size == 0)
1182 blks = transfer = do_count;
1183 else {
1184 blks = (STp->buffer)->buffer_bytes /
1185 STp->block_size;
1186 transfer = blks * STp->block_size;
1188 cmd[2] = blks >> 16;
1189 cmd[3] = blks >> 8;
1190 cmd[4] = blks;
1192 SCpnt = st_do_scsi(SCpnt, STp, cmd, transfer, STp->timeout,
1193 MAX_WRITE_RETRIES, TRUE);
1194 if (!SCpnt)
1195 return (-EBUSY);
1197 if ((STp->buffer)->last_result_fatal != 0) {
1198 #if DEBUG
1199 if (debugging)
1200 printk(ST_DEB_MSG "st%d: Error on write:\n", dev);
1201 #endif
1202 if ((SCpnt->sense_buffer[0] & 0x70) == 0x70 &&
1203 (SCpnt->sense_buffer[2] & 0x40)) {
1204 if (STp->block_size != 0 && (SCpnt->sense_buffer[0] & 0x80) != 0)
1205 transfer = (SCpnt->sense_buffer[3] << 24) |
1206 (SCpnt->sense_buffer[4] << 16) |
1207 (SCpnt->sense_buffer[5] << 8) | SCpnt->sense_buffer[6];
1208 else if (STp->block_size == 0 &&
1209 (SCpnt->sense_buffer[2] & 0x0f) == VOLUME_OVERFLOW)
1210 transfer = do_count;
1211 else
1212 transfer = 0;
1213 if (STp->block_size != 0)
1214 transfer *= STp->block_size;
1215 if (transfer <= do_count) {
1216 filp->f_pos += do_count - transfer;
1217 count -= do_count - transfer;
1218 if (STps->drv_block >= 0) {
1219 if (STp->block_size == 0 && transfer < do_count)
1220 STps->drv_block++;
1221 else if (STp->block_size != 0)
1222 STps->drv_block += (do_count - transfer) / STp->block_size;
1224 STps->eof = ST_EOM_OK;
1225 retval = (-ENOSPC); /* EOM within current request */
1226 #if DEBUG
1227 if (debugging)
1228 printk(ST_DEB_MSG "st%d: EOM with %d bytes unwritten.\n",
1229 dev, transfer);
1230 #endif
1231 } else {
1232 STps->eof = ST_EOM_ERROR;
1233 STps->drv_block = (-1); /* Too cautious? */
1234 retval = (-EIO); /* EOM for old data */
1235 #if DEBUG
1236 if (debugging)
1237 printk(ST_DEB_MSG "st%d: EOM with lost data.\n", dev);
1238 #endif
1240 } else {
1241 STps->drv_block = (-1); /* Too cautious? */
1242 retval = (-EIO);
1245 scsi_release_command(SCpnt);
1246 SCpnt = NULL;
1247 (STp->buffer)->buffer_bytes = 0;
1248 STp->dirty = 0;
1249 if (count < total)
1250 return total - count;
1251 else
1252 return retval;
1254 filp->f_pos += do_count;
1255 b_point += do_count;
1256 count -= do_count;
1257 if (STps->drv_block >= 0) {
1258 if (STp->block_size == 0)
1259 STps->drv_block++;
1260 else
1261 STps->drv_block += blks;
1263 (STp->buffer)->buffer_bytes = 0;
1264 STp->dirty = 0;
1266 if (count != 0) {
1267 STp->dirty = 1;
1268 i = append_to_buffer(b_point, STp->buffer, count);
1269 if (i) {
1270 if (SCpnt != NULL) {
1271 scsi_release_command(SCpnt);
1272 SCpnt = NULL;
1274 return i;
1276 filp->f_pos += count;
1277 count = 0;
1279 if (doing_write && (STp->buffer)->last_result_fatal != 0) {
1280 scsi_release_command(SCpnt);
1281 SCpnt = NULL;
1282 return (STp->buffer)->last_result_fatal;
1284 if (STm->do_async_writes &&
1285 (((STp->buffer)->buffer_bytes >= STp->write_threshold &&
1286 (STp->buffer)->buffer_bytes >= STp->block_size) ||
1287 STp->block_size == 0)) {
1288 /* Schedule an asynchronous write */
1289 if (STp->block_size == 0)
1290 (STp->buffer)->writing = (STp->buffer)->buffer_bytes;
1291 else
1292 (STp->buffer)->writing = ((STp->buffer)->buffer_bytes /
1293 STp->block_size) * STp->block_size;
1294 STp->dirty = !((STp->buffer)->writing ==
1295 (STp->buffer)->buffer_bytes);
1297 if (STp->block_size == 0)
1298 blks = (STp->buffer)->writing;
1299 else
1300 blks = (STp->buffer)->writing / STp->block_size;
1301 cmd[2] = blks >> 16;
1302 cmd[3] = blks >> 8;
1303 cmd[4] = blks;
1304 #if DEBUG
1305 STp->write_pending = 1;
1306 #endif
1308 SCpnt = st_do_scsi(SCpnt, STp, cmd, (STp->buffer)->writing, STp->timeout,
1309 MAX_WRITE_RETRIES, FALSE);
1310 if (SCpnt == NULL)
1311 return (-EIO);
1312 } else if (SCpnt != NULL) {
1313 scsi_release_command(SCpnt);
1314 SCpnt = NULL;
1316 STps->at_sm &= (total == 0);
1317 if (total > 0)
1318 STps->eof = ST_NOEOF;
1320 return (total);
1323 /* Read data from the tape. Returns zero in the normal case, one if the
1324 eof status has changed, and the negative error code in case of a
1325 fatal error. Otherwise updates the buffer and the eof state. */
1326 static long read_tape(struct inode *inode, long count, Scsi_Cmnd ** aSCpnt)
1328 int transfer, blks, bytes;
1329 static unsigned char cmd[10];
1330 Scsi_Cmnd *SCpnt;
1331 Scsi_Tape *STp;
1332 ST_mode *STm;
1333 ST_partstat *STps;
1334 int dev = TAPE_NR(inode->i_rdev);
1335 int retval = 0;
1337 if (count == 0)
1338 return 0;
1340 STp = &(scsi_tapes[dev]);
1341 STm = &(STp->modes[STp->current_mode]);
1342 STps = &(STp->ps[STp->partition]);
1343 if (STps->eof == ST_FM_HIT)
1344 return 1;
1346 memset(cmd, 0, 10);
1347 cmd[0] = READ_6;
1348 cmd[1] = (STp->block_size != 0);
1349 if (STp->block_size == 0)
1350 blks = bytes = count;
1351 else {
1352 if (STm->do_read_ahead) {
1353 blks = (STp->buffer)->buffer_blocks;
1354 bytes = blks * STp->block_size;
1355 } else {
1356 bytes = count;
1357 if (bytes > (STp->buffer)->buffer_size)
1358 bytes = (STp->buffer)->buffer_size;
1359 blks = bytes / STp->block_size;
1360 bytes = blks * STp->block_size;
1363 cmd[2] = blks >> 16;
1364 cmd[3] = blks >> 8;
1365 cmd[4] = blks;
1367 SCpnt = *aSCpnt;
1368 SCpnt = st_do_scsi(SCpnt, STp, cmd, bytes, STp->timeout, MAX_RETRIES, TRUE);
1369 *aSCpnt = SCpnt;
1370 if (!SCpnt)
1371 return (-EBUSY);
1373 (STp->buffer)->read_pointer = 0;
1374 STps->at_sm = 0;
1376 /* Something to check */
1377 if ((STp->buffer)->last_result_fatal) {
1378 retval = 1;
1379 #if DEBUG
1380 if (debugging)
1381 printk(ST_DEB_MSG "st%d: Sense: %2x %2x %2x %2x %2x %2x %2x %2x\n",
1382 dev,
1383 SCpnt->sense_buffer[0], SCpnt->sense_buffer[1],
1384 SCpnt->sense_buffer[2], SCpnt->sense_buffer[3],
1385 SCpnt->sense_buffer[4], SCpnt->sense_buffer[5],
1386 SCpnt->sense_buffer[6], SCpnt->sense_buffer[7]);
1387 #endif
1388 if ((SCpnt->sense_buffer[0] & 0x70) == 0x70) { /* extended sense */
1390 if ((SCpnt->sense_buffer[2] & 0x0f) == BLANK_CHECK)
1391 SCpnt->sense_buffer[2] &= 0xcf; /* No need for EOM in this case */
1393 if ((SCpnt->sense_buffer[2] & 0xe0) != 0) { /* EOF, EOM, or ILI */
1394 /* Compute the residual count */
1395 if ((SCpnt->sense_buffer[0] & 0x80) != 0)
1396 transfer = (SCpnt->sense_buffer[3] << 24) |
1397 (SCpnt->sense_buffer[4] << 16) |
1398 (SCpnt->sense_buffer[5] << 8) | SCpnt->sense_buffer[6];
1399 else
1400 transfer = 0;
1401 if (STp->block_size == 0 &&
1402 (SCpnt->sense_buffer[2] & 0x0f) == MEDIUM_ERROR)
1403 transfer = bytes;
1405 if (SCpnt->sense_buffer[2] & 0x20) { /* ILI */
1406 if (STp->block_size == 0) {
1407 if (transfer <= 0)
1408 transfer = 0;
1409 (STp->buffer)->buffer_bytes = bytes - transfer;
1410 } else {
1411 scsi_release_command(SCpnt);
1412 SCpnt = *aSCpnt = NULL;
1413 if (transfer == blks) { /* We did not get anything, error */
1414 printk(KERN_NOTICE "st%d: Incorrect block size.\n", dev);
1415 if (STps->drv_block >= 0)
1416 STps->drv_block += blks - transfer + 1;
1417 st_int_ioctl(inode, MTBSR, 1);
1418 return (-EIO);
1420 /* We have some data, deliver it */
1421 (STp->buffer)->buffer_bytes = (blks - transfer) *
1422 STp->block_size;
1423 #if DEBUG
1424 if (debugging)
1425 printk(ST_DEB_MSG
1426 "st%d: ILI but enough data received %ld %d.\n",
1427 dev, count, (STp->buffer)->buffer_bytes);
1428 #endif
1429 if (STps->drv_block >= 0)
1430 STps->drv_block += 1;
1431 if (st_int_ioctl(inode, MTBSR, 1))
1432 return (-EIO);
1434 } else if (SCpnt->sense_buffer[2] & 0x80) { /* FM overrides EOM */
1435 if (STps->eof != ST_FM_HIT)
1436 STps->eof = ST_FM_HIT;
1437 else
1438 STps->eof = ST_EOD_2;
1439 if (STp->block_size == 0)
1440 (STp->buffer)->buffer_bytes = 0;
1441 else
1442 (STp->buffer)->buffer_bytes =
1443 bytes - transfer * STp->block_size;
1444 #if DEBUG
1445 if (debugging)
1446 printk(ST_DEB_MSG
1447 "st%d: EOF detected (%d bytes read).\n",
1448 dev, (STp->buffer)->buffer_bytes);
1449 #endif
1450 } else if (SCpnt->sense_buffer[2] & 0x40) {
1451 if (STps->eof == ST_FM)
1452 STps->eof = ST_EOD_1;
1453 else
1454 STps->eof = ST_EOM_OK;
1455 if (STp->block_size == 0)
1456 (STp->buffer)->buffer_bytes = bytes - transfer;
1457 else
1458 (STp->buffer)->buffer_bytes =
1459 bytes - transfer * STp->block_size;
1460 #if DEBUG
1461 if (debugging)
1462 printk(ST_DEB_MSG "st%d: EOM detected (%d bytes read).\n",
1463 dev, (STp->buffer)->buffer_bytes);
1464 #endif
1467 /* end of EOF, EOM, ILI test */
1468 else { /* nonzero sense key */
1469 #if DEBUG
1470 if (debugging)
1471 printk(ST_DEB_MSG "st%d: Tape error while reading.\n", dev);
1472 #endif
1473 STps->drv_block = (-1);
1474 if (STps->eof == ST_FM &&
1475 (SCpnt->sense_buffer[2] & 0x0f) == BLANK_CHECK) {
1476 #if DEBUG
1477 if (debugging)
1478 printk(ST_DEB_MSG
1479 "st%d: Zero returned for first BLANK CHECK after EOF.\n",
1480 dev);
1481 #endif
1482 STps->eof = ST_EOD_2; /* First BLANK_CHECK after FM */
1483 } else /* Some other extended sense code */
1484 retval = (-EIO);
1487 /* End of extended sense test */
1488 else { /* Non-extended sense */
1489 retval = (STp->buffer)->last_result_fatal;
1493 /* End of error handling */
1494 else /* Read successful */
1495 (STp->buffer)->buffer_bytes = bytes;
1497 if (STps->drv_block >= 0) {
1498 if (STp->block_size == 0)
1499 STps->drv_block++;
1500 else
1501 STps->drv_block += (STp->buffer)->buffer_bytes / STp->block_size;
1503 return retval;
1507 /* Read command */
1508 static ssize_t
1509 st_read(struct file *filp, char *buf, size_t count, loff_t * ppos)
1511 struct inode *inode = filp->f_dentry->d_inode;
1512 ssize_t total;
1513 ssize_t i, transfer;
1514 int special;
1515 Scsi_Cmnd *SCpnt = NULL;
1516 Scsi_Tape *STp;
1517 ST_mode *STm;
1518 ST_partstat *STps;
1519 int dev = TAPE_NR(inode->i_rdev);
1521 STp = &(scsi_tapes[dev]);
1524 * If we are in the middle of error recovery, don't let anyone
1525 * else try and use this device. Also, if error recovery fails, it
1526 * may try and take the device offline, in which case all further
1527 * access to the device is prohibited.
1529 if (!scsi_block_when_processing_errors(STp->device)) {
1530 return -ENXIO;
1532 if (ppos != &filp->f_pos) {
1533 /* "A request was outside the capabilities of the device." */
1534 return -ENXIO;
1536 if (STp->ready != ST_READY) {
1537 if (STp->ready == ST_NO_TAPE)
1538 return (-ENOMEDIUM);
1539 else
1540 return (-EIO);
1542 STm = &(STp->modes[STp->current_mode]);
1543 if (!STm->defined)
1544 return (-ENXIO);
1545 #if DEBUG
1546 if (!STp->in_use) {
1547 printk(ST_DEB_MSG "st%d: Incorrect device.\n", dev);
1548 return (-EIO);
1550 #endif
1552 if (STp->can_partitions &&
1553 (total = update_partition(inode)) < 0)
1554 return total;
1556 if (STp->block_size == 0 &&
1557 count > (STp->buffer)->buffer_size &&
1558 !enlarge_buffer(STp->buffer, count, STp->restr_dma))
1559 return (-EOVERFLOW);
1561 if (!(STm->do_read_ahead) && STp->block_size != 0 &&
1562 (count % STp->block_size) != 0)
1563 return (-EIO); /* Read must be integral number of blocks */
1565 if (STp->do_auto_lock && STp->door_locked == ST_UNLOCKED &&
1566 !st_int_ioctl(inode, MTLOCK, 0))
1567 STp->door_locked = ST_LOCKED_AUTO;
1569 STps = &(STp->ps[STp->partition]);
1570 if (STps->rw == ST_WRITING) {
1571 transfer = flush_buffer(inode, filp, 0);
1572 if (transfer)
1573 return transfer;
1574 STps->rw = ST_READING;
1576 #if DEBUG
1577 if (debugging && STps->eof != ST_NOEOF)
1578 printk(ST_DEB_MSG "st%d: EOF/EOM flag up (%d). Bytes %d\n", dev,
1579 STps->eof, (STp->buffer)->buffer_bytes);
1580 #endif
1581 if ((STp->buffer)->buffer_bytes == 0 &&
1582 STps->eof >= ST_EOD_1) {
1583 if (STps->eof < ST_EOD) {
1584 STps->eof += 1;
1585 return 0;
1587 return (-EIO); /* EOM or Blank Check */
1589 /* Check the buffer writability before any tape movement. Don't alter
1590 buffer data. */
1591 if (copy_from_user(&i, buf, 1) != 0 ||
1592 copy_to_user(buf, &i, 1) != 0 ||
1593 copy_from_user(&i, buf + count - 1, 1) != 0 ||
1594 copy_to_user(buf + count - 1, &i, 1) != 0)
1595 return (-EFAULT);
1597 STps->rw = ST_READING;
1600 /* Loop until enough data in buffer or a special condition found */
1601 for (total = 0, special = 0; total < count && !special;) {
1603 /* Get new data if the buffer is empty */
1604 if ((STp->buffer)->buffer_bytes == 0) {
1605 special = read_tape(inode, count - total, &SCpnt);
1606 if (special < 0) { /* No need to continue read */
1607 if (SCpnt != NULL) {
1608 scsi_release_command(SCpnt);
1610 return special;
1613 /* Move the data from driver buffer to user buffer */
1614 if ((STp->buffer)->buffer_bytes > 0) {
1615 #if DEBUG
1616 if (debugging && STps->eof != ST_NOEOF)
1617 printk(ST_DEB_MSG "st%d: EOF up (%d). Left %d, needed %d.\n", dev,
1618 STps->eof, (STp->buffer)->buffer_bytes, count - total);
1619 #endif
1620 transfer = (STp->buffer)->buffer_bytes < count - total ?
1621 (STp->buffer)->buffer_bytes : count - total;
1622 i = from_buffer(STp->buffer, buf, transfer);
1623 if (i) {
1624 if (SCpnt != NULL) {
1625 scsi_release_command(SCpnt);
1626 SCpnt = NULL;
1628 return i;
1630 filp->f_pos += transfer;
1631 buf += transfer;
1632 total += transfer;
1634 if (STp->block_size == 0)
1635 break; /* Read only one variable length block */
1637 } /* for (total = 0, special = 0; total < count && !special; ) */
1639 if (SCpnt != NULL) {
1640 scsi_release_command(SCpnt);
1641 SCpnt = NULL;
1643 /* Change the eof state if no data from tape or buffer */
1644 if (total == 0) {
1645 if (STps->eof == ST_FM_HIT) {
1646 STps->eof = ST_FM;
1647 STps->drv_block = 0;
1648 if (STps->drv_file >= 0)
1649 STps->drv_file++;
1650 } else if (STps->eof == ST_EOD_1) {
1651 STps->eof = ST_EOD_2;
1652 STps->drv_block = 0;
1653 if (STps->drv_file >= 0)
1654 STps->drv_file++;
1655 } else if (STps->eof == ST_EOD_2)
1656 STps->eof = ST_EOD;
1657 } else if (STps->eof == ST_FM)
1658 STps->eof = ST_NOEOF;
1660 return total;
1665 /* Set the driver options */
1666 static void st_log_options(Scsi_Tape * STp, ST_mode * STm, int dev)
1668 printk(KERN_INFO
1669 "st%d: Mode %d options: buffer writes: %d, async writes: %d, read ahead: %d\n",
1670 dev, STp->current_mode, STm->do_buffer_writes, STm->do_async_writes,
1671 STm->do_read_ahead);
1672 printk(KERN_INFO
1673 "st%d: can bsr: %d, two FMs: %d, fast mteom: %d, auto lock: %d,\n",
1674 dev, STp->can_bsr, STp->two_fm, STp->fast_mteom, STp->do_auto_lock);
1675 printk(KERN_INFO
1676 "st%d: defs for wr: %d, no block limits: %d, partitions: %d, s2 log: %d\n",
1677 dev, STm->defaults_for_writes, STp->omit_blklims, STp->can_partitions,
1678 STp->scsi2_logical);
1679 printk(KERN_INFO
1680 "st%d: sysv: %d\n", dev, STm->sysv);
1681 #if DEBUG
1682 printk(KERN_INFO
1683 "st%d: debugging: %d\n",
1684 dev, debugging);
1685 #endif
1689 static int st_set_options(struct inode *inode, long options)
1691 int value;
1692 long code;
1693 Scsi_Tape *STp;
1694 ST_mode *STm;
1695 int dev = TAPE_NR(inode->i_rdev);
1697 STp = &(scsi_tapes[dev]);
1698 STm = &(STp->modes[STp->current_mode]);
1699 if (!STm->defined) {
1700 memcpy(STm, &(STp->modes[0]), sizeof(ST_mode));
1701 modes_defined = TRUE;
1702 #if DEBUG
1703 if (debugging)
1704 printk(ST_DEB_MSG "st%d: Initialized mode %d definition from mode 0\n",
1705 dev, STp->current_mode);
1706 #endif
1708 code = options & MT_ST_OPTIONS;
1709 if (code == MT_ST_BOOLEANS) {
1710 STm->do_buffer_writes = (options & MT_ST_BUFFER_WRITES) != 0;
1711 STm->do_async_writes = (options & MT_ST_ASYNC_WRITES) != 0;
1712 STm->defaults_for_writes = (options & MT_ST_DEF_WRITES) != 0;
1713 STm->do_read_ahead = (options & MT_ST_READ_AHEAD) != 0;
1714 STp->two_fm = (options & MT_ST_TWO_FM) != 0;
1715 STp->fast_mteom = (options & MT_ST_FAST_MTEOM) != 0;
1716 STp->do_auto_lock = (options & MT_ST_AUTO_LOCK) != 0;
1717 STp->can_bsr = (options & MT_ST_CAN_BSR) != 0;
1718 STp->omit_blklims = (options & MT_ST_NO_BLKLIMS) != 0;
1719 if ((STp->device)->scsi_level >= SCSI_2)
1720 STp->can_partitions = (options & MT_ST_CAN_PARTITIONS) != 0;
1721 STp->scsi2_logical = (options & MT_ST_SCSI2LOGICAL) != 0;
1722 STm->sysv = (options & MT_ST_SYSV) != 0;
1723 #if DEBUG
1724 debugging = (options & MT_ST_DEBUGGING) != 0;
1725 #endif
1726 st_log_options(STp, STm, dev);
1727 } else if (code == MT_ST_SETBOOLEANS || code == MT_ST_CLEARBOOLEANS) {
1728 value = (code == MT_ST_SETBOOLEANS);
1729 if ((options & MT_ST_BUFFER_WRITES) != 0)
1730 STm->do_buffer_writes = value;
1731 if ((options & MT_ST_ASYNC_WRITES) != 0)
1732 STm->do_async_writes = value;
1733 if ((options & MT_ST_DEF_WRITES) != 0)
1734 STm->defaults_for_writes = value;
1735 if ((options & MT_ST_READ_AHEAD) != 0)
1736 STm->do_read_ahead = value;
1737 if ((options & MT_ST_TWO_FM) != 0)
1738 STp->two_fm = value;
1739 if ((options & MT_ST_FAST_MTEOM) != 0)
1740 STp->fast_mteom = value;
1741 if ((options & MT_ST_AUTO_LOCK) != 0)
1742 STp->do_auto_lock = value;
1743 if ((options & MT_ST_CAN_BSR) != 0)
1744 STp->can_bsr = value;
1745 if ((options & MT_ST_NO_BLKLIMS) != 0)
1746 STp->omit_blklims = value;
1747 if ((STp->device)->scsi_level >= SCSI_2 &&
1748 (options & MT_ST_CAN_PARTITIONS) != 0)
1749 STp->can_partitions = value;
1750 if ((options & MT_ST_SCSI2LOGICAL) != 0)
1751 STp->scsi2_logical = value;
1752 if ((options & MT_ST_SYSV) != 0)
1753 STm->sysv = value;
1754 #if DEBUG
1755 if ((options & MT_ST_DEBUGGING) != 0)
1756 debugging = value;
1757 #endif
1758 st_log_options(STp, STm, dev);
1759 } else if (code == MT_ST_WRITE_THRESHOLD) {
1760 value = (options & ~MT_ST_OPTIONS) * ST_KILOBYTE;
1761 if (value < 1 || value > st_buffer_size) {
1762 printk(KERN_WARNING "st%d: Write threshold %d too small or too large.\n",
1763 dev, value);
1764 return (-EIO);
1766 STp->write_threshold = value;
1767 printk(KERN_INFO "st%d: Write threshold set to %d bytes.\n",
1768 dev, value);
1769 } else if (code == MT_ST_DEF_BLKSIZE) {
1770 value = (options & ~MT_ST_OPTIONS);
1771 if (value == ~MT_ST_OPTIONS) {
1772 STm->default_blksize = (-1);
1773 printk(KERN_INFO "st%d: Default block size disabled.\n", dev);
1774 } else {
1775 STm->default_blksize = value;
1776 printk(KERN_INFO "st%d: Default block size set to %d bytes.\n",
1777 dev, STm->default_blksize);
1779 } else if (code == MT_ST_TIMEOUTS) {
1780 value = (options & ~MT_ST_OPTIONS);
1781 if ((value & MT_ST_SET_LONG_TIMEOUT) != 0) {
1782 STp->long_timeout = (value & ~MT_ST_SET_LONG_TIMEOUT) * HZ;
1783 printk(KERN_INFO "st%d: Long timeout set to %d seconds.\n", dev,
1784 (value & ~MT_ST_SET_LONG_TIMEOUT));
1785 } else {
1786 STp->timeout = value * HZ;
1787 printk(KERN_INFO "st%d: Normal timeout set to %d seconds.\n", dev,
1788 value);
1790 } else if (code == MT_ST_DEF_OPTIONS) {
1791 code = (options & ~MT_ST_CLEAR_DEFAULT);
1792 value = (options & MT_ST_CLEAR_DEFAULT);
1793 if (code == MT_ST_DEF_DENSITY) {
1794 if (value == MT_ST_CLEAR_DEFAULT) {
1795 STm->default_density = (-1);
1796 printk(KERN_INFO "st%d: Density default disabled.\n", dev);
1797 } else {
1798 STm->default_density = value & 0xff;
1799 printk(KERN_INFO "st%d: Density default set to %x\n",
1800 dev, STm->default_density);
1802 } else if (code == MT_ST_DEF_DRVBUFFER) {
1803 if (value == MT_ST_CLEAR_DEFAULT) {
1804 STp->default_drvbuffer = 0xff;
1805 printk(KERN_INFO "st%d: Drive buffer default disabled.\n", dev);
1806 } else {
1807 STp->default_drvbuffer = value & 7;
1808 printk(KERN_INFO "st%d: Drive buffer default set to %x\n",
1809 dev, STp->default_drvbuffer);
1811 } else if (code == MT_ST_DEF_COMPRESSION) {
1812 if (value == MT_ST_CLEAR_DEFAULT) {
1813 STm->default_compression = ST_DONT_TOUCH;
1814 printk(KERN_INFO "st%d: Compression default disabled.\n", dev);
1815 } else {
1816 STm->default_compression = (value & 1 ? ST_YES : ST_NO);
1817 printk(KERN_INFO "st%d: Compression default set to %x\n",
1818 dev, (value & 1));
1821 } else
1822 return (-EIO);
1824 return 0;
1828 #define COMPRESSION_PAGE 0x0f
1829 #define COMPRESSION_PAGE_LENGTH 16
1831 #define MODE_HEADER_LENGTH 4
1833 #define DCE_MASK 0x80
1834 #define DCC_MASK 0x40
1835 #define RED_MASK 0x60
1838 /* Control the compression with mode page 15. Algorithm not changed if zero. */
1839 static int st_compression(Scsi_Tape * STp, int state)
1841 int dev;
1842 unsigned char cmd[10];
1843 Scsi_Cmnd *SCpnt = NULL;
1845 if (STp->ready != ST_READY)
1846 return (-EIO);
1848 /* Read the current page contents */
1849 memset(cmd, 0, 10);
1850 cmd[0] = MODE_SENSE;
1851 cmd[1] = 8;
1852 cmd[2] = COMPRESSION_PAGE;
1853 cmd[4] = COMPRESSION_PAGE_LENGTH + MODE_HEADER_LENGTH;
1855 SCpnt = st_do_scsi(SCpnt, STp, cmd, cmd[4], STp->timeout, 0, TRUE);
1856 if (SCpnt == NULL)
1857 return (-EBUSY);
1858 dev = TAPE_NR(SCpnt->request.rq_dev);
1860 if ((STp->buffer)->last_result_fatal != 0) {
1861 #if DEBUG
1862 if (debugging)
1863 printk(ST_DEB_MSG "st%d: Compression mode page not supported.\n", dev);
1864 #endif
1865 scsi_release_command(SCpnt);
1866 SCpnt = NULL;
1867 return (-EIO);
1869 #if DEBUG
1870 if (debugging)
1871 printk(ST_DEB_MSG "st%d: Compression state is %d.\n", dev,
1872 ((STp->buffer)->b_data[MODE_HEADER_LENGTH + 2] & DCE_MASK ? 1 : 0));
1873 #endif
1875 /* Check if compression can be changed */
1876 if (((STp->buffer)->b_data[MODE_HEADER_LENGTH + 2] & DCC_MASK) == 0) {
1877 #if DEBUG
1878 if (debugging)
1879 printk(ST_DEB_MSG "st%d: Compression not supported.\n", dev);
1880 #endif
1881 scsi_release_command(SCpnt);
1882 SCpnt = NULL;
1883 return (-EIO);
1885 /* Do the change */
1886 if (state)
1887 (STp->buffer)->b_data[MODE_HEADER_LENGTH + 2] |= DCE_MASK;
1888 else
1889 (STp->buffer)->b_data[MODE_HEADER_LENGTH + 2] &= ~DCE_MASK;
1891 memset(cmd, 0, 10);
1892 cmd[0] = MODE_SELECT;
1893 cmd[1] = 0x10;
1894 cmd[4] = COMPRESSION_PAGE_LENGTH + MODE_HEADER_LENGTH;
1896 (STp->buffer)->b_data[0] = 0; /* Reserved data length */
1897 (STp->buffer)->b_data[1] = 0; /* Reserved media type byte */
1898 (STp->buffer)->b_data[MODE_HEADER_LENGTH] &= 0x3f;
1899 SCpnt = st_do_scsi(SCpnt, STp, cmd, cmd[4], STp->timeout, 0, TRUE);
1901 if ((STp->buffer)->last_result_fatal != 0) {
1902 #if DEBUG
1903 if (debugging)
1904 printk(ST_DEB_MSG "st%d: Compression change failed.\n", dev);
1905 #endif
1906 scsi_release_command(SCpnt);
1907 SCpnt = NULL;
1908 return (-EIO);
1910 #if DEBUG
1911 if (debugging)
1912 printk(ST_DEB_MSG "st%d: Compression state changed to %d.\n",
1913 dev, state);
1914 #endif
1916 scsi_release_command(SCpnt);
1917 SCpnt = NULL;
1918 STp->compression_changed = TRUE;
1919 return 0;
1923 /* Internal ioctl function */
1924 static int st_int_ioctl(struct inode *inode,
1925 unsigned int cmd_in, unsigned long arg)
1927 int timeout;
1928 long ltmp;
1929 int i, ioctl_result;
1930 int chg_eof = TRUE;
1931 unsigned char cmd[10];
1932 Scsi_Cmnd *SCpnt;
1933 Scsi_Tape *STp;
1934 ST_partstat *STps;
1935 int fileno, blkno, at_sm, undone, datalen;
1936 int dev = TAPE_NR(inode->i_rdev);
1938 STp = &(scsi_tapes[dev]);
1939 if (STp->ready != ST_READY && cmd_in != MTLOAD) {
1940 if (STp->ready == ST_NO_TAPE)
1941 return (-ENOMEDIUM);
1942 else
1943 return (-EIO);
1945 timeout = STp->long_timeout;
1946 STps = &(STp->ps[STp->partition]);
1947 fileno = STps->drv_file;
1948 blkno = STps->drv_block;
1949 at_sm = STps->at_sm;
1951 memset(cmd, 0, 10);
1952 datalen = 0;
1953 switch (cmd_in) {
1954 case MTFSFM:
1955 chg_eof = FALSE; /* Changed from the FSF after this */
1956 case MTFSF:
1957 cmd[0] = SPACE;
1958 cmd[1] = 0x01; /* Space FileMarks */
1959 cmd[2] = (arg >> 16);
1960 cmd[3] = (arg >> 8);
1961 cmd[4] = arg;
1962 #if DEBUG
1963 if (debugging)
1964 printk(ST_DEB_MSG "st%d: Spacing tape forward over %d filemarks.\n",
1965 dev, cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
1966 #endif
1967 if (fileno >= 0)
1968 fileno += arg;
1969 blkno = 0;
1970 at_sm &= (arg == 0);
1971 break;
1972 case MTBSFM:
1973 chg_eof = FALSE; /* Changed from the FSF after this */
1974 case MTBSF:
1975 cmd[0] = SPACE;
1976 cmd[1] = 0x01; /* Space FileMarks */
1977 ltmp = (-arg);
1978 cmd[2] = (ltmp >> 16);
1979 cmd[3] = (ltmp >> 8);
1980 cmd[4] = ltmp;
1981 #if DEBUG
1982 if (debugging) {
1983 if (cmd[2] & 0x80)
1984 ltmp = 0xff000000;
1985 ltmp = ltmp | (cmd[2] << 16) | (cmd[3] << 8) | cmd[4];
1986 printk(ST_DEB_MSG "st%d: Spacing tape backward over %ld filemarks.\n",
1987 dev, (-ltmp));
1989 #endif
1990 if (fileno >= 0)
1991 fileno -= arg;
1992 blkno = (-1); /* We can't know the block number */
1993 at_sm &= (arg == 0);
1994 break;
1995 case MTFSR:
1996 cmd[0] = SPACE;
1997 cmd[1] = 0x00; /* Space Blocks */
1998 cmd[2] = (arg >> 16);
1999 cmd[3] = (arg >> 8);
2000 cmd[4] = arg;
2001 #if DEBUG
2002 if (debugging)
2003 printk(ST_DEB_MSG "st%d: Spacing tape forward %d blocks.\n", dev,
2004 cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
2005 #endif
2006 if (blkno >= 0)
2007 blkno += arg;
2008 at_sm &= (arg == 0);
2009 break;
2010 case MTBSR:
2011 cmd[0] = SPACE;
2012 cmd[1] = 0x00; /* Space Blocks */
2013 ltmp = (-arg);
2014 cmd[2] = (ltmp >> 16);
2015 cmd[3] = (ltmp >> 8);
2016 cmd[4] = ltmp;
2017 #if DEBUG
2018 if (debugging) {
2019 if (cmd[2] & 0x80)
2020 ltmp = 0xff000000;
2021 ltmp = ltmp | (cmd[2] << 16) | (cmd[3] << 8) | cmd[4];
2022 printk(ST_DEB_MSG "st%d: Spacing tape backward %ld blocks.\n", dev, (-ltmp));
2024 #endif
2025 if (blkno >= 0)
2026 blkno -= arg;
2027 at_sm &= (arg == 0);
2028 break;
2029 case MTFSS:
2030 cmd[0] = SPACE;
2031 cmd[1] = 0x04; /* Space Setmarks */
2032 cmd[2] = (arg >> 16);
2033 cmd[3] = (arg >> 8);
2034 cmd[4] = arg;
2035 #if DEBUG
2036 if (debugging)
2037 printk(ST_DEB_MSG "st%d: Spacing tape forward %d setmarks.\n", dev,
2038 cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
2039 #endif
2040 if (arg != 0) {
2041 blkno = fileno = (-1);
2042 at_sm = 1;
2044 break;
2045 case MTBSS:
2046 cmd[0] = SPACE;
2047 cmd[1] = 0x04; /* Space Setmarks */
2048 ltmp = (-arg);
2049 cmd[2] = (ltmp >> 16);
2050 cmd[3] = (ltmp >> 8);
2051 cmd[4] = ltmp;
2052 #if DEBUG
2053 if (debugging) {
2054 if (cmd[2] & 0x80)
2055 ltmp = 0xff000000;
2056 ltmp = ltmp | (cmd[2] << 16) | (cmd[3] << 8) | cmd[4];
2057 printk(ST_DEB_MSG "st%d: Spacing tape backward %ld setmarks.\n",
2058 dev, (-ltmp));
2060 #endif
2061 if (arg != 0) {
2062 blkno = fileno = (-1);
2063 at_sm = 1;
2065 break;
2066 case MTWEOF:
2067 case MTWSM:
2068 if (STp->write_prot)
2069 return (-EACCES);
2070 cmd[0] = WRITE_FILEMARKS;
2071 if (cmd_in == MTWSM)
2072 cmd[1] = 2;
2073 cmd[2] = (arg >> 16);
2074 cmd[3] = (arg >> 8);
2075 cmd[4] = arg;
2076 timeout = STp->timeout;
2077 #if DEBUG
2078 if (debugging) {
2079 if (cmd_in == MTWEOF)
2080 printk(ST_DEB_MSG "st%d: Writing %d filemarks.\n", dev,
2081 cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
2082 else
2083 printk(ST_DEB_MSG "st%d: Writing %d setmarks.\n", dev,
2084 cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
2086 #endif
2087 if (fileno >= 0)
2088 fileno += arg;
2089 blkno = 0;
2090 at_sm = (cmd_in == MTWSM);
2091 break;
2092 case MTREW:
2093 cmd[0] = REZERO_UNIT;
2094 #if ST_NOWAIT
2095 cmd[1] = 1; /* Don't wait for completion */
2096 timeout = STp->timeout;
2097 #endif
2098 #if DEBUG
2099 if (debugging)
2100 printk(ST_DEB_MSG "st%d: Rewinding tape.\n", dev);
2101 #endif
2102 fileno = blkno = at_sm = 0;
2103 break;
2104 case MTOFFL:
2105 case MTLOAD:
2106 case MTUNLOAD:
2107 cmd[0] = START_STOP;
2108 if (cmd_in == MTLOAD)
2109 cmd[4] |= 1;
2111 * If arg >= 1 && arg <= 6 Enhanced load/unload in HP C1553A
2113 if (cmd_in != MTOFFL &&
2114 arg >= 1 + MT_ST_HPLOADER_OFFSET
2115 && arg <= 6 + MT_ST_HPLOADER_OFFSET) {
2116 #if DEBUG
2117 if (debugging) {
2118 printk(ST_DEB_MSG "st%d: Enhanced %sload slot %2ld.\n",
2119 dev, (cmd[4]) ? "" : "un",
2120 arg - MT_ST_HPLOADER_OFFSET);
2122 #endif
2123 cmd[3] = arg - MT_ST_HPLOADER_OFFSET; /* MediaID field of C1553A */
2125 #if ST_NOWAIT
2126 cmd[1] = 1; /* Don't wait for completion */
2127 timeout = STp->timeout;
2128 #else
2129 timeout = STp->long_timeout;
2130 #endif
2131 #if DEBUG
2132 if (debugging) {
2133 if (cmd_in != MTLOAD)
2134 printk(ST_DEB_MSG "st%d: Unloading tape.\n", dev);
2135 else
2136 printk(ST_DEB_MSG "st%d: Loading tape.\n", dev);
2138 #endif
2139 fileno = blkno = at_sm = 0;
2140 break;
2141 case MTNOP:
2142 #if DEBUG
2143 if (debugging)
2144 printk(ST_DEB_MSG "st%d: No op on tape.\n", dev);
2145 #endif
2146 return 0; /* Should do something ? */
2147 break;
2148 case MTRETEN:
2149 cmd[0] = START_STOP;
2150 #if ST_NOWAIT
2151 cmd[1] = 1; /* Don't wait for completion */
2152 timeout = STp->timeout;
2153 #endif
2154 cmd[4] = 3;
2155 #if DEBUG
2156 if (debugging)
2157 printk(ST_DEB_MSG "st%d: Retensioning tape.\n", dev);
2158 #endif
2159 fileno = blkno = at_sm = 0;
2160 break;
2161 case MTEOM:
2162 if (!STp->fast_mteom) {
2163 /* space to the end of tape */
2164 ioctl_result = st_int_ioctl(inode, MTFSF, 0x3fff);
2165 fileno = STps->drv_file;
2166 if (STps->eof >= ST_EOD_1)
2167 return 0;
2168 /* The next lines would hide the number of spaced FileMarks
2169 That's why I inserted the previous lines. I had no luck
2170 with detecting EOM with FSF, so we go now to EOM.
2171 Joerg Weule */
2172 } else
2173 fileno = (-1);
2174 cmd[0] = SPACE;
2175 cmd[1] = 3;
2176 #if DEBUG
2177 if (debugging)
2178 printk(ST_DEB_MSG "st%d: Spacing to end of recorded medium.\n", dev);
2179 #endif
2180 blkno = 0;
2181 at_sm = 0;
2182 break;
2183 case MTERASE:
2184 if (STp->write_prot)
2185 return (-EACCES);
2186 cmd[0] = ERASE;
2187 cmd[1] = 1; /* To the end of tape */
2188 #if ST_NOWAIT
2189 cmd[1] |= 2; /* Don't wait for completion */
2190 timeout = STp->timeout;
2191 #else
2192 timeout = STp->long_timeout * 8;
2193 #endif
2194 #if DEBUG
2195 if (debugging)
2196 printk(ST_DEB_MSG "st%d: Erasing tape.\n", dev);
2197 #endif
2198 fileno = blkno = at_sm = 0;
2199 break;
2200 case MTLOCK:
2201 chg_eof = FALSE;
2202 cmd[0] = ALLOW_MEDIUM_REMOVAL;
2203 cmd[4] = SCSI_REMOVAL_PREVENT;
2204 #if DEBUG
2205 if (debugging)
2206 printk(ST_DEB_MSG "st%d: Locking drive door.\n", dev);
2207 #endif /* ; */
2208 break;
2209 case MTUNLOCK:
2210 chg_eof = FALSE;
2211 cmd[0] = ALLOW_MEDIUM_REMOVAL;
2212 cmd[4] = SCSI_REMOVAL_ALLOW;
2213 #if DEBUG
2214 if (debugging)
2215 printk(ST_DEB_MSG "st%d: Unlocking drive door.\n", dev);
2216 #endif /* ; */
2217 break;
2218 case MTSETBLK: /* Set block length */
2219 case MTSETDENSITY: /* Set tape density */
2220 case MTSETDRVBUFFER: /* Set drive buffering */
2221 case SET_DENS_AND_BLK: /* Set density and block size */
2222 chg_eof = FALSE;
2223 if (STp->dirty || (STp->buffer)->buffer_bytes != 0)
2224 return (-EIO); /* Not allowed if data in buffer */
2225 if ((cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) &&
2226 (arg & MT_ST_BLKSIZE_MASK) != 0 &&
2227 ((arg & MT_ST_BLKSIZE_MASK) < STp->min_block ||
2228 (arg & MT_ST_BLKSIZE_MASK) > STp->max_block ||
2229 (arg & MT_ST_BLKSIZE_MASK) > st_buffer_size)) {
2230 printk(KERN_WARNING "st%d: Illegal block size.\n", dev);
2231 return (-EINVAL);
2233 cmd[0] = MODE_SELECT;
2234 cmd[4] = datalen = 12;
2236 memset((STp->buffer)->b_data, 0, 12);
2237 if (cmd_in == MTSETDRVBUFFER)
2238 (STp->buffer)->b_data[2] = (arg & 7) << 4;
2239 else
2240 (STp->buffer)->b_data[2] =
2241 STp->drv_buffer << 4;
2242 (STp->buffer)->b_data[3] = 8; /* block descriptor length */
2243 if (cmd_in == MTSETDENSITY) {
2244 (STp->buffer)->b_data[4] = arg;
2245 STp->density_changed = TRUE; /* At least we tried ;-) */
2246 } else if (cmd_in == SET_DENS_AND_BLK)
2247 (STp->buffer)->b_data[4] = arg >> 24;
2248 else
2249 (STp->buffer)->b_data[4] = STp->density;
2250 if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) {
2251 ltmp = arg & MT_ST_BLKSIZE_MASK;
2252 if (cmd_in == MTSETBLK)
2253 STp->blksize_changed = TRUE; /* At least we tried ;-) */
2254 } else
2255 ltmp = STp->block_size;
2256 (STp->buffer)->b_data[9] = (ltmp >> 16);
2257 (STp->buffer)->b_data[10] = (ltmp >> 8);
2258 (STp->buffer)->b_data[11] = ltmp;
2259 timeout = STp->timeout;
2260 #if DEBUG
2261 if (debugging) {
2262 if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK)
2263 printk(ST_DEB_MSG "st%d: Setting block size to %d bytes.\n", dev,
2264 (STp->buffer)->b_data[9] * 65536 +
2265 (STp->buffer)->b_data[10] * 256 +
2266 (STp->buffer)->b_data[11]);
2267 if (cmd_in == MTSETDENSITY || cmd_in == SET_DENS_AND_BLK)
2268 printk(ST_DEB_MSG "st%d: Setting density code to %x.\n", dev,
2269 (STp->buffer)->b_data[4]);
2270 if (cmd_in == MTSETDRVBUFFER)
2271 printk(ST_DEB_MSG "st%d: Setting drive buffer code to %d.\n", dev,
2272 ((STp->buffer)->b_data[2] >> 4) & 7);
2274 #endif
2275 break;
2276 default:
2277 return (-ENOSYS);
2280 SCpnt = st_do_scsi(NULL, STp, cmd, datalen, timeout, MAX_RETRIES, TRUE);
2281 if (!SCpnt)
2282 return (-EBUSY);
2284 ioctl_result = (STp->buffer)->last_result_fatal;
2286 if (!ioctl_result) { /* SCSI command successful */
2287 scsi_release_command(SCpnt);
2288 SCpnt = NULL;
2289 STps->drv_block = blkno;
2290 STps->drv_file = fileno;
2291 STps->at_sm = at_sm;
2293 if (cmd_in == MTLOCK)
2294 STp->door_locked = ST_LOCKED_EXPLICIT;
2295 else if (cmd_in == MTUNLOCK)
2296 STp->door_locked = ST_UNLOCKED;
2298 if (cmd_in == MTBSFM)
2299 ioctl_result = st_int_ioctl(inode, MTFSF, 1);
2300 else if (cmd_in == MTFSFM)
2301 ioctl_result = st_int_ioctl(inode, MTBSF, 1);
2303 if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) {
2304 STp->block_size = arg & MT_ST_BLKSIZE_MASK;
2305 if (STp->block_size != 0)
2306 (STp->buffer)->buffer_blocks =
2307 (STp->buffer)->buffer_size / STp->block_size;
2308 (STp->buffer)->buffer_bytes = (STp->buffer)->read_pointer = 0;
2309 if (cmd_in == SET_DENS_AND_BLK)
2310 STp->density = arg >> MT_ST_DENSITY_SHIFT;
2311 } else if (cmd_in == MTSETDRVBUFFER)
2312 STp->drv_buffer = (arg & 7);
2313 else if (cmd_in == MTSETDENSITY)
2314 STp->density = arg;
2316 if (cmd_in == MTEOM)
2317 STps->eof = ST_EOD;
2318 else if (cmd_in == MTFSF)
2319 STps->eof = ST_FM;
2320 else if (chg_eof)
2321 STps->eof = ST_NOEOF;
2324 if (cmd_in == MTOFFL || cmd_in == MTUNLOAD)
2325 STp->rew_at_close = 0;
2326 else if (cmd_in == MTLOAD) {
2327 STp->rew_at_close = (MINOR(inode->i_rdev) & 0x80) == 0;
2328 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
2329 STp->ps[i].rw = ST_IDLE;
2330 STp->ps[i].last_block_valid = FALSE;
2332 STp->partition = 0;
2334 } else { /* SCSI command was not completely successful. Don't return
2335 from this block without releasing the SCSI command block! */
2337 if (SCpnt->sense_buffer[2] & 0x40) {
2338 if (cmd_in != MTBSF && cmd_in != MTBSFM &&
2339 cmd_in != MTBSR && cmd_in != MTBSS)
2340 STps->eof = ST_EOM_OK;
2341 STps->drv_block = 0;
2343 undone = (
2344 (SCpnt->sense_buffer[3] << 24) +
2345 (SCpnt->sense_buffer[4] << 16) +
2346 (SCpnt->sense_buffer[5] << 8) +
2347 SCpnt->sense_buffer[6]);
2348 if (cmd_in == MTWEOF &&
2349 (SCpnt->sense_buffer[0] & 0x70) == 0x70 &&
2350 (SCpnt->sense_buffer[2] & 0x4f) == 0x40 &&
2351 ((SCpnt->sense_buffer[0] & 0x80) == 0 || undone == 0)) {
2352 ioctl_result = 0; /* EOF written succesfully at EOM */
2353 if (fileno >= 0)
2354 fileno++;
2355 STps->drv_file = fileno;
2356 STps->eof = ST_NOEOF;
2357 } else if ((cmd_in == MTFSF) || (cmd_in == MTFSFM)) {
2358 if (fileno >= 0)
2359 STps->drv_file = fileno - undone;
2360 else
2361 STps->drv_file = fileno;
2362 STps->drv_block = 0;
2363 STps->eof = ST_NOEOF;
2364 } else if ((cmd_in == MTBSF) || (cmd_in == MTBSFM)) {
2365 if (fileno >= 0)
2366 STps->drv_file = fileno + undone;
2367 else
2368 STps->drv_file = fileno;
2369 STps->drv_block = 0;
2370 STps->eof = ST_NOEOF;
2371 } else if (cmd_in == MTFSR) {
2372 if (SCpnt->sense_buffer[2] & 0x80) { /* Hit filemark */
2373 if (STps->drv_file >= 0)
2374 STps->drv_file++;
2375 STps->drv_block = 0;
2376 STps->eof = ST_FM;
2377 } else {
2378 if (blkno >= undone)
2379 STps->drv_block = blkno - undone;
2380 else
2381 STps->drv_block = (-1);
2382 STps->eof = ST_NOEOF;
2384 } else if (cmd_in == MTBSR) {
2385 if (SCpnt->sense_buffer[2] & 0x80) { /* Hit filemark */
2386 STps->drv_file--;
2387 STps->drv_block = (-1);
2388 } else {
2389 if (blkno >= 0)
2390 STps->drv_block = blkno + undone;
2391 else
2392 STps->drv_block = (-1);
2394 STps->eof = ST_NOEOF;
2395 } else if (cmd_in == MTEOM) {
2396 STps->drv_file = (-1);
2397 STps->drv_block = (-1);
2398 STps->eof = ST_EOD;
2399 } else if (chg_eof)
2400 STps->eof = ST_NOEOF;
2402 if ((SCpnt->sense_buffer[2] & 0x0f) == BLANK_CHECK)
2403 STps->eof = ST_EOD;
2405 if (cmd_in == MTLOCK)
2406 STp->door_locked = ST_LOCK_FAILS;
2408 scsi_release_command(SCpnt);
2409 SCpnt = NULL;
2412 return ioctl_result;
2416 /* Get the tape position. If bt == 2, arg points into a kernel space mt_loc
2417 structure. */
2419 static int get_location(struct inode *inode, unsigned int *block, int *partition,
2420 int logical)
2422 Scsi_Tape *STp;
2423 int dev = TAPE_NR(inode->i_rdev);
2424 int result;
2425 unsigned char scmd[10];
2426 Scsi_Cmnd *SCpnt;
2428 STp = &(scsi_tapes[dev]);
2429 if (STp->ready != ST_READY)
2430 return (-EIO);
2432 memset(scmd, 0, 10);
2433 if ((STp->device)->scsi_level < SCSI_2) {
2434 scmd[0] = QFA_REQUEST_BLOCK;
2435 scmd[4] = 3;
2436 } else {
2437 scmd[0] = READ_POSITION;
2438 if (!logical && !STp->scsi2_logical)
2439 scmd[1] = 1;
2441 SCpnt = st_do_scsi(NULL, STp, scmd, 20, STp->timeout, MAX_READY_RETRIES, TRUE);
2442 if (!SCpnt)
2443 return (-EBUSY);
2445 if ((STp->buffer)->last_result_fatal != 0 ||
2446 (STp->device->scsi_level >= SCSI_2 &&
2447 ((STp->buffer)->b_data[0] & 4) != 0)) {
2448 *block = *partition = 0;
2449 #if DEBUG
2450 if (debugging)
2451 printk(ST_DEB_MSG "st%d: Can't read tape position.\n", dev);
2452 #endif
2453 result = (-EIO);
2454 } else {
2455 result = 0;
2456 if ((STp->device)->scsi_level < SCSI_2) {
2457 *block = ((STp->buffer)->b_data[0] << 16)
2458 + ((STp->buffer)->b_data[1] << 8)
2459 + (STp->buffer)->b_data[2];
2460 *partition = 0;
2461 } else {
2462 *block = ((STp->buffer)->b_data[4] << 24)
2463 + ((STp->buffer)->b_data[5] << 16)
2464 + ((STp->buffer)->b_data[6] << 8)
2465 + (STp->buffer)->b_data[7];
2466 *partition = (STp->buffer)->b_data[1];
2467 if (((STp->buffer)->b_data[0] & 0x80) &&
2468 (STp->buffer)->b_data[1] == 0) /* BOP of partition 0 */
2469 STp->ps[0].drv_block = STp->ps[0].drv_file = 0;
2471 #if DEBUG
2472 if (debugging)
2473 printk(ST_DEB_MSG "st%d: Got tape pos. blk %d part %d.\n", dev,
2474 *block, *partition);
2475 #endif
2478 scsi_release_command(SCpnt);
2479 SCpnt = NULL;
2481 return result;
2485 /* Set the tape block and partition. Negative partition means that only the
2486 block should be set in vendor specific way. */
2487 static int set_location(struct inode *inode, unsigned int block, int partition,
2488 int logical)
2490 Scsi_Tape *STp;
2491 ST_partstat *STps;
2492 int dev = TAPE_NR(inode->i_rdev);
2493 int result, p;
2494 unsigned int blk;
2495 int timeout;
2496 unsigned char scmd[10];
2497 Scsi_Cmnd *SCpnt;
2499 STp = &(scsi_tapes[dev]);
2500 if (STp->ready != ST_READY)
2501 return (-EIO);
2502 timeout = STp->long_timeout;
2503 STps = &(STp->ps[STp->partition]);
2505 #if DEBUG
2506 if (debugging)
2507 printk(ST_DEB_MSG "st%d: Setting block to %d and partition to %d.\n",
2508 dev, block, partition);
2509 if (partition < 0)
2510 return (-EIO);
2511 #endif
2513 /* Update the location at the partition we are leaving */
2514 if ((!STp->can_partitions && partition != 0) ||
2515 partition >= ST_NBR_PARTITIONS)
2516 return (-EINVAL);
2517 if (partition != STp->partition) {
2518 if (get_location(inode, &blk, &p, 1))
2519 STps->last_block_valid = FALSE;
2520 else {
2521 STps->last_block_valid = TRUE;
2522 STps->last_block_visited = blk;
2523 #if DEBUG
2524 if (debugging)
2525 printk(ST_DEB_MSG "st%d: Visited block %d for partition %d saved.\n",
2526 dev, blk, STp->partition);
2527 #endif
2530 memset(scmd, 0, 10);
2531 if ((STp->device)->scsi_level < SCSI_2) {
2532 scmd[0] = QFA_SEEK_BLOCK;
2533 scmd[2] = (block >> 16);
2534 scmd[3] = (block >> 8);
2535 scmd[4] = block;
2536 scmd[5] = 0;
2537 } else {
2538 scmd[0] = SEEK_10;
2539 scmd[3] = (block >> 24);
2540 scmd[4] = (block >> 16);
2541 scmd[5] = (block >> 8);
2542 scmd[6] = block;
2543 if (!logical && !STp->scsi2_logical)
2544 scmd[1] = 4;
2545 if (STp->partition != partition) {
2546 scmd[1] |= 2;
2547 scmd[8] = partition;
2548 #if DEBUG
2549 if (debugging)
2550 printk(ST_DEB_MSG "st%d: Trying to change partition from %d to %d\n",
2551 dev, STp->partition, partition);
2552 #endif
2555 #if ST_NOWAIT
2556 scmd[1] |= 1; /* Don't wait for completion */
2557 timeout = STp->timeout;
2558 #endif
2560 SCpnt = st_do_scsi(NULL, STp, scmd, 20, timeout, MAX_READY_RETRIES, TRUE);
2561 if (!SCpnt)
2562 return (-EBUSY);
2564 STps->drv_block = STps->drv_file = (-1);
2565 STps->eof = ST_NOEOF;
2566 if ((STp->buffer)->last_result_fatal != 0) {
2567 result = (-EIO);
2568 if (STp->can_partitions &&
2569 (STp->device)->scsi_level >= SCSI_2 &&
2570 (p = find_partition(inode)) >= 0)
2571 STp->partition = p;
2572 } else {
2573 if (STp->can_partitions) {
2574 STp->partition = partition;
2575 STps = &(STp->ps[partition]);
2576 if (!STps->last_block_valid ||
2577 STps->last_block_visited != block) {
2578 STps->at_sm = 0;
2579 STps->rw = ST_IDLE;
2581 } else
2582 STps->at_sm = 0;
2583 if (block == 0)
2584 STps->drv_block = STps->drv_file = 0;
2585 result = 0;
2588 scsi_release_command(SCpnt);
2589 SCpnt = NULL;
2591 return result;
2595 /* Find the current partition number for the drive status. Called from open and
2596 returns either partition number of negative error code. */
2597 static int find_partition(struct inode *inode)
2599 int i, partition;
2600 unsigned int block;
2602 if ((i = get_location(inode, &block, &partition, 1)) < 0)
2603 return i;
2604 if (partition >= ST_NBR_PARTITIONS)
2605 return (-EIO);
2606 return partition;
2610 /* Change the partition if necessary */
2611 static int update_partition(struct inode *inode)
2613 int dev = TAPE_NR(inode->i_rdev);
2614 Scsi_Tape *STp;
2615 ST_partstat *STps;
2617 STp = &(scsi_tapes[dev]);
2618 if (STp->partition == STp->new_partition)
2619 return 0;
2620 STps = &(STp->ps[STp->new_partition]);
2621 if (!STps->last_block_valid)
2622 STps->last_block_visited = 0;
2623 return set_location(inode, STps->last_block_visited, STp->new_partition, 1);
2626 /* Functions for reading and writing the medium partition mode page. These
2627 seem to work with Wangtek 6200HS and HP C1533A. */
2629 #define PART_PAGE 0x11
2630 #define PART_PAGE_LENGTH 10
2632 /* Get the number of partitions on the tape. As a side effect reads the
2633 mode page into the tape buffer. */
2634 static int nbr_partitions(struct inode *inode)
2636 int dev = TAPE_NR(inode->i_rdev), result;
2637 Scsi_Tape *STp;
2638 Scsi_Cmnd *SCpnt = NULL;
2639 unsigned char cmd[10];
2641 STp = &(scsi_tapes[dev]);
2642 if (STp->ready != ST_READY)
2643 return (-EIO);
2645 memset((void *) &cmd[0], 0, 10);
2646 cmd[0] = MODE_SENSE;
2647 cmd[1] = 8; /* Page format */
2648 cmd[2] = PART_PAGE;
2649 cmd[4] = 200;
2651 SCpnt = st_do_scsi(SCpnt, STp, cmd, 200, STp->timeout, MAX_READY_RETRIES, TRUE);
2652 if (SCpnt == NULL)
2653 return (-EBUSY);
2654 scsi_release_command(SCpnt);
2655 SCpnt = NULL;
2657 if ((STp->buffer)->last_result_fatal != 0) {
2658 #if DEBUG
2659 if (debugging)
2660 printk(ST_DEB_MSG "st%d: Can't read medium partition page.\n", dev);
2661 #endif
2662 result = (-EIO);
2663 } else {
2664 result = (STp->buffer)->b_data[MODE_HEADER_LENGTH + 3] + 1;
2665 #if DEBUG
2666 if (debugging)
2667 printk(ST_DEB_MSG "st%d: Number of partitions %d.\n", dev, result);
2668 #endif
2671 return result;
2675 /* Partition the tape into two partitions if size > 0 or one partition if
2676 size == 0 */
2677 static int partition_tape(struct inode *inode, int size)
2679 int dev = TAPE_NR(inode->i_rdev), result;
2680 int length;
2681 Scsi_Tape *STp;
2682 Scsi_Cmnd *SCpnt = NULL;
2683 unsigned char cmd[10], *bp;
2685 if ((result = nbr_partitions(inode)) < 0)
2686 return result;
2687 STp = &(scsi_tapes[dev]);
2689 /* The mode page is in the buffer. Let's modify it and write it. */
2690 bp = &((STp->buffer)->b_data[0]);
2691 if (size <= 0) {
2692 length = 8;
2693 bp[MODE_HEADER_LENGTH + 3] = 0;
2694 #if DEBUG
2695 if (debugging)
2696 printk(ST_DEB_MSG "st%d: Formatting tape with one partition.\n", dev);
2697 #endif
2698 } else {
2699 length = 10;
2700 bp[MODE_HEADER_LENGTH + 3] = 1;
2701 bp[MODE_HEADER_LENGTH + 8] = (size >> 8) & 0xff;
2702 bp[MODE_HEADER_LENGTH + 9] = size & 0xff;
2703 #if DEBUG
2704 if (debugging)
2705 printk(ST_DEB_MSG "st%d: Formatting tape with two partition (1 = %d MB).\n",
2706 dev, size);
2707 #endif
2709 bp[MODE_HEADER_LENGTH + 6] = 0;
2710 bp[MODE_HEADER_LENGTH + 7] = 0;
2711 bp[MODE_HEADER_LENGTH + 4] = 0x30; /* IDP | PSUM = MB */
2713 bp[0] = 0;
2714 bp[1] = 0;
2715 bp[MODE_HEADER_LENGTH] &= 0x3f;
2716 bp[MODE_HEADER_LENGTH + 1] = length - 2;
2718 memset(cmd, 0, 10);
2719 cmd[0] = MODE_SELECT;
2720 cmd[1] = 0x10;
2721 cmd[4] = length + MODE_HEADER_LENGTH;
2723 SCpnt = st_do_scsi(SCpnt, STp, cmd, cmd[4], STp->long_timeout,
2724 MAX_READY_RETRIES, TRUE);
2725 if (SCpnt == NULL)
2726 return (-EBUSY);
2727 scsi_release_command(SCpnt);
2728 SCpnt = NULL;
2730 if ((STp->buffer)->last_result_fatal != 0) {
2731 printk(KERN_INFO "st%d: Partitioning of tape failed.\n", dev);
2732 result = (-EIO);
2733 } else
2734 result = 0;
2736 return result;
2741 /* The ioctl command */
2742 static int st_ioctl(struct inode *inode, struct file *file,
2743 unsigned int cmd_in, unsigned long arg)
2745 int i, cmd_nr, cmd_type, bt;
2746 unsigned int blk;
2747 struct mtop mtc;
2748 struct mtpos mt_pos;
2749 Scsi_Tape *STp;
2750 ST_mode *STm;
2751 ST_partstat *STps;
2752 int dev = TAPE_NR(inode->i_rdev);
2754 STp = &(scsi_tapes[dev]);
2755 #if DEBUG
2756 if (debugging && !STp->in_use) {
2757 printk(ST_DEB_MSG "st%d: Incorrect device.\n", dev);
2758 return (-EIO);
2760 #endif
2761 STm = &(STp->modes[STp->current_mode]);
2762 STps = &(STp->ps[STp->partition]);
2765 * If we are in the middle of error recovery, don't let anyone
2766 * else try and use this device. Also, if error recovery fails, it
2767 * may try and take the device offline, in which case all further
2768 * access to the device is prohibited.
2770 if (!scsi_block_when_processing_errors(STp->device)) {
2771 return -ENXIO;
2773 cmd_type = _IOC_TYPE(cmd_in);
2774 cmd_nr = _IOC_NR(cmd_in);
2776 if (cmd_type == _IOC_TYPE(MTIOCTOP) && cmd_nr == _IOC_NR(MTIOCTOP)) {
2777 if (_IOC_SIZE(cmd_in) != sizeof(mtc))
2778 return (-EINVAL);
2780 i = copy_from_user((char *) &mtc, (char *) arg, sizeof(struct mtop));
2781 if (i)
2782 return (-EFAULT);
2784 if (mtc.mt_op == MTSETDRVBUFFER && !capable(CAP_SYS_ADMIN)) {
2785 printk(KERN_WARNING "st%d: MTSETDRVBUFFER only allowed for root.\n", dev);
2786 return (-EPERM);
2788 if (!STm->defined &&
2789 (mtc.mt_op != MTSETDRVBUFFER && (mtc.mt_count & MT_ST_OPTIONS) == 0))
2790 return (-ENXIO);
2792 if (!(STp->device)->was_reset) {
2794 if (STps->eof == ST_FM_HIT) {
2795 if (mtc.mt_op == MTFSF || mtc.mt_op == MTFSFM || mtc.mt_op == MTEOM) {
2796 mtc.mt_count -= 1;
2797 if (STps->drv_file >= 0)
2798 STps->drv_file += 1;
2799 } else if (mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM) {
2800 mtc.mt_count += 1;
2801 if (STps->drv_file >= 0)
2802 STps->drv_file += 1;
2805 if (mtc.mt_op == MTSEEK) {
2806 /* Old position must be restored if partition will be changed */
2807 i = !STp->can_partitions ||
2808 (STp->new_partition != STp->partition);
2809 } else {
2810 i = mtc.mt_op == MTREW || mtc.mt_op == MTOFFL ||
2811 mtc.mt_op == MTRETEN || mtc.mt_op == MTEOM ||
2812 mtc.mt_op == MTLOCK || mtc.mt_op == MTLOAD ||
2813 mtc.mt_op == MTCOMPRESSION;
2815 i = flush_buffer(inode, file, i);
2816 if (i < 0)
2817 return i;
2818 } else {
2820 * If there was a bus reset, block further access
2821 * to this device. If the user wants to rewind the tape,
2822 * then reset the flag and allow access again.
2824 if (mtc.mt_op != MTREW &&
2825 mtc.mt_op != MTOFFL &&
2826 mtc.mt_op != MTRETEN &&
2827 mtc.mt_op != MTERASE &&
2828 mtc.mt_op != MTSEEK &&
2829 mtc.mt_op != MTEOM)
2830 return (-EIO);
2831 STp->device->was_reset = 0;
2832 if (STp->door_locked != ST_UNLOCKED &&
2833 STp->door_locked != ST_LOCK_FAILS) {
2834 if (st_int_ioctl(inode, MTLOCK, 0)) {
2835 printk(KERN_NOTICE "st%d: Could not relock door after bus reset.\n",
2836 dev);
2837 STp->door_locked = ST_UNLOCKED;
2842 if (mtc.mt_op != MTNOP && mtc.mt_op != MTSETBLK &&
2843 mtc.mt_op != MTSETDENSITY && mtc.mt_op != MTWSM &&
2844 mtc.mt_op != MTSETDRVBUFFER && mtc.mt_op != MTSETPART)
2845 STps->rw = ST_IDLE; /* Prevent automatic WEOF and fsf */
2847 if (mtc.mt_op == MTOFFL && STp->door_locked != ST_UNLOCKED)
2848 st_int_ioctl(inode, MTUNLOCK, 0); /* Ignore result! */
2850 if (mtc.mt_op == MTSETDRVBUFFER &&
2851 (mtc.mt_count & MT_ST_OPTIONS) != 0)
2852 return st_set_options(inode, mtc.mt_count);
2853 if (mtc.mt_op == MTSETPART) {
2854 if (!STp->can_partitions ||
2855 mtc.mt_count < 0 || mtc.mt_count >= ST_NBR_PARTITIONS)
2856 return (-EINVAL);
2857 if (mtc.mt_count >= STp->nbr_partitions &&
2858 (STp->nbr_partitions = nbr_partitions(inode)) < 0)
2859 return (-EIO);
2860 if (mtc.mt_count >= STp->nbr_partitions)
2861 return (-EINVAL);
2862 STp->new_partition = mtc.mt_count;
2863 return 0;
2865 if (mtc.mt_op == MTMKPART) {
2866 if (!STp->can_partitions)
2867 return (-EINVAL);
2868 if ((i = st_int_ioctl(inode, MTREW, 0)) < 0 ||
2869 (i = partition_tape(inode, mtc.mt_count)) < 0)
2870 return i;
2871 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
2872 STp->ps[i].rw = ST_IDLE;
2873 STp->ps[i].at_sm = 0;
2874 STp->ps[i].last_block_valid = FALSE;
2876 STp->partition = STp->new_partition = 0;
2877 STp->nbr_partitions = 1; /* Bad guess ?-) */
2878 STps->drv_block = STps->drv_file = 0;
2879 return 0;
2881 if (mtc.mt_op == MTSEEK) {
2882 i = set_location(inode, mtc.mt_count, STp->new_partition, 0);
2883 if (!STp->can_partitions)
2884 STp->ps[0].rw = ST_IDLE;
2885 return i;
2887 if (STp->can_partitions && STp->ready == ST_READY &&
2888 (i = update_partition(inode)) < 0)
2889 return i;
2890 if (mtc.mt_op == MTCOMPRESSION)
2891 return st_compression(STp, (mtc.mt_count & 1));
2892 else
2893 return st_int_ioctl(inode, mtc.mt_op, mtc.mt_count);
2895 if (!STm->defined)
2896 return (-ENXIO);
2898 if ((i = flush_buffer(inode, file, FALSE)) < 0)
2899 return i;
2900 if (STp->can_partitions &&
2901 (i = update_partition(inode)) < 0)
2902 return i;
2904 if (cmd_type == _IOC_TYPE(MTIOCGET) && cmd_nr == _IOC_NR(MTIOCGET)) {
2906 if (_IOC_SIZE(cmd_in) != sizeof(struct mtget))
2907 return (-EINVAL);
2909 (STp->mt_status)->mt_dsreg =
2910 ((STp->block_size << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK) |
2911 ((STp->density << MT_ST_DENSITY_SHIFT) & MT_ST_DENSITY_MASK);
2912 (STp->mt_status)->mt_blkno = STps->drv_block;
2913 (STp->mt_status)->mt_fileno = STps->drv_file;
2914 if (STp->block_size != 0) {
2915 if (STps->rw == ST_WRITING)
2916 (STp->mt_status)->mt_blkno +=
2917 (STp->buffer)->buffer_bytes / STp->block_size;
2918 else if (STps->rw == ST_READING)
2919 (STp->mt_status)->mt_blkno -= ((STp->buffer)->buffer_bytes +
2920 STp->block_size - 1) / STp->block_size;
2922 (STp->mt_status)->mt_gstat = 0;
2923 if (STp->drv_write_prot)
2924 (STp->mt_status)->mt_gstat |= GMT_WR_PROT(0xffffffff);
2925 if ((STp->mt_status)->mt_blkno == 0) {
2926 if ((STp->mt_status)->mt_fileno == 0)
2927 (STp->mt_status)->mt_gstat |= GMT_BOT(0xffffffff);
2928 else
2929 (STp->mt_status)->mt_gstat |= GMT_EOF(0xffffffff);
2931 (STp->mt_status)->mt_resid = STp->partition;
2932 if (STps->eof == ST_EOM_OK || STps->eof == ST_EOM_ERROR)
2933 (STp->mt_status)->mt_gstat |= GMT_EOT(0xffffffff);
2934 else if (STps->eof >= ST_EOM_OK)
2935 (STp->mt_status)->mt_gstat |= GMT_EOD(0xffffffff);
2936 if (STp->density == 1)
2937 (STp->mt_status)->mt_gstat |= GMT_D_800(0xffffffff);
2938 else if (STp->density == 2)
2939 (STp->mt_status)->mt_gstat |= GMT_D_1600(0xffffffff);
2940 else if (STp->density == 3)
2941 (STp->mt_status)->mt_gstat |= GMT_D_6250(0xffffffff);
2942 if (STp->ready == ST_READY)
2943 (STp->mt_status)->mt_gstat |= GMT_ONLINE(0xffffffff);
2944 if (STp->ready == ST_NO_TAPE)
2945 (STp->mt_status)->mt_gstat |= GMT_DR_OPEN(0xffffffff);
2946 if (STps->at_sm)
2947 (STp->mt_status)->mt_gstat |= GMT_SM(0xffffffff);
2948 if (STm->do_async_writes || (STm->do_buffer_writes && STp->block_size != 0) ||
2949 STp->drv_buffer != 0)
2950 (STp->mt_status)->mt_gstat |= GMT_IM_REP_EN(0xffffffff);
2952 i = copy_to_user((char *) arg, (char *) (STp->mt_status),
2953 sizeof(struct mtget));
2954 if (i)
2955 return (-EFAULT);
2957 (STp->mt_status)->mt_erreg = 0; /* Clear after read */
2958 return 0;
2959 } /* End of MTIOCGET */
2960 if (cmd_type == _IOC_TYPE(MTIOCPOS) && cmd_nr == _IOC_NR(MTIOCPOS)) {
2961 if (_IOC_SIZE(cmd_in) != sizeof(struct mtpos))
2962 return (-EINVAL);
2963 if ((i = get_location(inode, &blk, &bt, 0)) < 0)
2964 return i;
2965 mt_pos.mt_blkno = blk;
2966 i = copy_to_user((char *) arg, (char *) (&mt_pos), sizeof(struct mtpos));
2967 if (i)
2968 return (-EFAULT);
2969 return 0;
2971 return scsi_ioctl(STp->device, cmd_in, (void *) arg);
2975 /* Try to allocate a new tape buffer */
2976 static ST_buffer *
2977 new_tape_buffer(int from_initialization, int need_dma)
2979 int i, priority, b_size, got = 0, segs = 0;
2980 ST_buffer *tb;
2982 if (st_nbr_buffers >= st_template.dev_max)
2983 return NULL; /* Should never happen */
2985 if (from_initialization)
2986 priority = GFP_ATOMIC;
2987 else
2988 priority = GFP_KERNEL;
2990 i = sizeof(ST_buffer) + (st_max_sg_segs - 1) * sizeof(struct scatterlist);
2991 tb = (ST_buffer *) scsi_init_malloc(i, priority);
2992 if (tb) {
2993 tb->this_size = i;
2994 if (need_dma)
2995 priority |= GFP_DMA;
2997 /* Try to allocate the first segment up to ST_FIRST_ORDER and the
2998 others big enough to reach the goal */
2999 for (b_size = PAGE_SIZE << ST_FIRST_ORDER;
3000 b_size / 2 >= st_buffer_size && b_size > PAGE_SIZE;)
3001 b_size /= 2;
3002 for (; b_size >= PAGE_SIZE; b_size /= 2) {
3003 tb->sg[0].address =
3004 (unsigned char *) scsi_init_malloc(b_size, priority);
3005 if (tb->sg[0].address != NULL) {
3006 tb->sg[0].alt_address = NULL;
3007 tb->sg[0].length = b_size;
3008 break;
3011 if (tb->sg[segs].address == NULL) {
3012 scsi_init_free((char *) tb, tb->this_size);
3013 tb = NULL;
3014 } else { /* Got something, continue */
3016 for (b_size = PAGE_SIZE;
3017 st_buffer_size > tb->sg[0].length + (ST_FIRST_SG - 1) * b_size;)
3018 b_size *= 2;
3020 for (segs = 1, got = tb->sg[0].length;
3021 got < st_buffer_size && segs < ST_FIRST_SG;) {
3022 tb->sg[segs].address =
3023 (unsigned char *) scsi_init_malloc(b_size, priority);
3024 if (tb->sg[segs].address == NULL) {
3025 if (st_buffer_size - got <=
3026 (ST_FIRST_SG - segs) * b_size / 2) {
3027 b_size /= 2; /* Large enough for the rest of the buffers */
3028 continue;
3030 for (i = 0; i < segs - 1; i++)
3031 scsi_init_free(tb->sg[i].address, tb->sg[i].length);
3032 scsi_init_free((char *) tb, tb->this_size);
3033 tb = NULL;
3034 break;
3036 tb->sg[segs].alt_address = NULL;
3037 tb->sg[segs].length = b_size;
3038 got += b_size;
3039 segs++;
3043 if (!tb) {
3044 printk(KERN_NOTICE "st: Can't allocate new tape buffer (nbr %d).\n",
3045 st_nbr_buffers);
3046 return NULL;
3048 tb->sg_segs = tb->orig_sg_segs = segs;
3049 tb->b_data = tb->sg[0].address;
3051 #if DEBUG
3052 if (debugging) {
3053 printk(ST_DEB_MSG
3054 "st: Allocated tape buffer %d (%d bytes, %d segments, dma: %d, a: %p).\n",
3055 st_nbr_buffers, got, tb->sg_segs, need_dma, tb->b_data);
3056 printk(ST_DEB_MSG
3057 "st: segment sizes: first %d, last %d bytes.\n",
3058 tb->sg[0].length, tb->sg[segs - 1].length);
3060 #endif
3061 tb->in_use = 0;
3062 tb->dma = need_dma;
3063 tb->buffer_size = got;
3064 tb->writing = 0;
3065 st_buffers[st_nbr_buffers++] = tb;
3067 return tb;
3071 /* Try to allocate a temporary enlarged tape buffer */
3072 static int enlarge_buffer(ST_buffer * STbuffer, int new_size, int need_dma)
3074 int segs, nbr, max_segs, b_size, priority, got;
3076 normalize_buffer(STbuffer);
3078 max_segs = STbuffer->use_sg;
3079 if (max_segs > st_max_sg_segs)
3080 max_segs = st_max_sg_segs;
3081 nbr = max_segs - STbuffer->sg_segs;
3082 if (nbr <= 0)
3083 return FALSE;
3085 priority = GFP_KERNEL;
3086 if (need_dma)
3087 priority |= GFP_DMA;
3088 for (b_size = PAGE_SIZE; b_size * nbr < new_size - STbuffer->buffer_size;)
3089 b_size *= 2;
3091 for (segs = STbuffer->sg_segs, got = STbuffer->buffer_size;
3092 segs < max_segs && got < new_size;) {
3093 STbuffer->sg[segs].address =
3094 (unsigned char *) scsi_init_malloc(b_size, priority);
3095 if (STbuffer->sg[segs].address == NULL) {
3096 if (new_size - got <= (max_segs - segs) * b_size / 2) {
3097 b_size /= 2; /* Large enough for the rest of the buffers */
3098 continue;
3100 printk(KERN_NOTICE "st: failed to enlarge buffer to %d bytes.\n",
3101 new_size);
3102 normalize_buffer(STbuffer);
3103 return FALSE;
3105 STbuffer->sg[segs].alt_address = NULL;
3106 STbuffer->sg[segs].length = b_size;
3107 STbuffer->sg_segs += 1;
3108 got += b_size;
3109 STbuffer->buffer_size = got;
3110 segs++;
3112 #if DEBUG
3113 if (debugging)
3114 printk(ST_DEB_MSG
3115 "st: Succeeded to enlarge buffer to %d bytes (segs %d->%d, %d).\n",
3116 got, STbuffer->orig_sg_segs, STbuffer->sg_segs, b_size);
3117 #endif
3119 return TRUE;
3123 /* Release the extra buffer */
3124 static void normalize_buffer(ST_buffer * STbuffer)
3126 int i;
3128 for (i = STbuffer->orig_sg_segs; i < STbuffer->sg_segs; i++) {
3129 scsi_init_free(STbuffer->sg[i].address, STbuffer->sg[i].length);
3130 STbuffer->buffer_size -= STbuffer->sg[i].length;
3132 #if DEBUG
3133 if (debugging && STbuffer->orig_sg_segs < STbuffer->sg_segs)
3134 printk(ST_DEB_MSG "st: Buffer at %p normalized to %d bytes (segs %d).\n",
3135 STbuffer->b_data, STbuffer->buffer_size, STbuffer->sg_segs);
3136 #endif
3137 STbuffer->sg_segs = STbuffer->orig_sg_segs;
3141 /* Move data from the user buffer to the tape buffer. Returns zero (success) or
3142 negative error code. */
3143 static int append_to_buffer(const char *ubp, ST_buffer * st_bp, int do_count)
3145 int i, cnt, res, offset;
3147 for (i = 0, offset = st_bp->buffer_bytes;
3148 i < st_bp->sg_segs && offset >= st_bp->sg[i].length; i++)
3149 offset -= st_bp->sg[i].length;
3150 if (i == st_bp->sg_segs) { /* Should never happen */
3151 printk(KERN_WARNING "st: append_to_buffer offset overflow.\n");
3152 return (-EIO);
3154 for (; i < st_bp->sg_segs && do_count > 0; i++) {
3155 cnt = st_bp->sg[i].length - offset < do_count ?
3156 st_bp->sg[i].length - offset : do_count;
3157 res = copy_from_user(st_bp->sg[i].address + offset, ubp, cnt);
3158 if (res)
3159 return (-EFAULT);
3160 do_count -= cnt;
3161 st_bp->buffer_bytes += cnt;
3162 ubp += cnt;
3163 offset = 0;
3165 if (do_count) { /* Should never happen */
3166 printk(KERN_WARNING "st: append_to_buffer overflow (left %d).\n",
3167 do_count);
3168 return (-EIO);
3170 return 0;
3174 /* Move data from the tape buffer to the user buffer. Returns zero (success) or
3175 negative error code. */
3176 static int from_buffer(ST_buffer * st_bp, char *ubp, int do_count)
3178 int i, cnt, res, offset;
3180 for (i = 0, offset = st_bp->read_pointer;
3181 i < st_bp->sg_segs && offset >= st_bp->sg[i].length; i++)
3182 offset -= st_bp->sg[i].length;
3183 if (i == st_bp->sg_segs) { /* Should never happen */
3184 printk(KERN_WARNING "st: from_buffer offset overflow.\n");
3185 return (-EIO);
3187 for (; i < st_bp->sg_segs && do_count > 0; i++) {
3188 cnt = st_bp->sg[i].length - offset < do_count ?
3189 st_bp->sg[i].length - offset : do_count;
3190 res = copy_to_user(ubp, st_bp->sg[i].address + offset, cnt);
3191 if (res)
3192 return (-EFAULT);
3193 do_count -= cnt;
3194 st_bp->buffer_bytes -= cnt;
3195 st_bp->read_pointer += cnt;
3196 ubp += cnt;
3197 offset = 0;
3199 if (do_count) { /* Should never happen */
3200 printk(KERN_WARNING "st: from_buffer overflow (left %d).\n",
3201 do_count);
3202 return (-EIO);
3204 return 0;
3208 /* Validate the options from command line or module parameters */
3209 static void validate_options(void)
3211 if (buffer_kbs > 0)
3212 st_buffer_size = buffer_kbs * ST_KILOBYTE;
3213 if (write_threshold_kbs > 0)
3214 st_write_threshold = write_threshold_kbs * ST_KILOBYTE;
3215 else if (buffer_kbs > 0)
3216 st_write_threshold = st_buffer_size - 2048;
3217 if (st_write_threshold > st_buffer_size) {
3218 st_write_threshold = st_buffer_size;
3219 printk(KERN_WARNING "st: write_threshold limited to %d bytes.\n",
3220 st_write_threshold);
3222 if (max_buffers >= 0)
3223 st_max_buffers = max_buffers;
3224 if (max_sg_segs >= ST_FIRST_SG)
3225 st_max_sg_segs = max_sg_segs;
3228 #ifndef MODULE
3229 /* Set the boot options. Syntax is defined in README.st.
3231 static int __init st_setup(char *str)
3233 int i, len, ints[5];
3234 char *stp;
3236 stp = get_options(str, ARRAY_SIZE(ints), ints);
3238 if (ints[0] > 0) {
3239 for (i = 0; i < ints[0] && i < ARRAY_SIZE(parms); i++)
3240 *parms[i].val = ints[i + 1];
3241 } else {
3242 while (stp != NULL) {
3243 for (i = 0; i < ARRAY_SIZE(parms); i++) {
3244 len = strlen(parms[i].name);
3245 if (!strncmp(stp, parms[i].name, len) &&
3246 (*(stp + len) == ':' || *(stp + len) == '=')) {
3247 *parms[i].val = simple_strtoul(stp + len + 1, NULL, 0);
3248 break;
3251 if (i >= sizeof(parms) / sizeof(struct st_dev_parm))
3252 printk(KERN_WARNING "st: illegal parameter in '%s'\n",
3253 stp);
3254 stp = strchr(stp, ',');
3255 if (stp)
3256 stp++;
3260 validate_options();
3262 return 1;
3265 __setup("st=", st_setup);
3267 #endif
3270 static struct file_operations st_fops =
3272 NULL, /* lseek - default */
3273 st_read, /* read - general block-dev read */
3274 st_write, /* write - general block-dev write */
3275 NULL, /* readdir - bad */
3276 NULL, /* select */
3277 st_ioctl, /* ioctl */
3278 NULL, /* mmap */
3279 scsi_tape_open, /* open */
3280 scsi_tape_flush, /* flush */
3281 scsi_tape_close, /* release */
3282 NULL /* fsync */
3285 static int st_attach(Scsi_Device * SDp)
3287 Scsi_Tape *tpnt;
3288 ST_mode *STm;
3289 ST_partstat *STps;
3290 int i;
3292 if (SDp->type != TYPE_TAPE)
3293 return 1;
3295 if (st_template.nr_dev >= st_template.dev_max) {
3296 SDp->attached--;
3297 return 1;
3299 for (tpnt = scsi_tapes, i = 0; i < st_template.dev_max; i++, tpnt++)
3300 if (!tpnt->device)
3301 break;
3303 if (i >= st_template.dev_max)
3304 panic("scsi_devices corrupt (st)");
3306 scsi_tapes[i].device = SDp;
3307 if (SDp->scsi_level <= 2)
3308 scsi_tapes[i].mt_status->mt_type = MT_ISSCSI1;
3309 else
3310 scsi_tapes[i].mt_status->mt_type = MT_ISSCSI2;
3312 tpnt->devt = MKDEV(SCSI_TAPE_MAJOR, i);
3313 tpnt->dirty = 0;
3314 tpnt->in_use = 0;
3315 tpnt->drv_buffer = 1; /* Try buffering if no mode sense */
3316 tpnt->restr_dma = (SDp->host)->unchecked_isa_dma;
3317 tpnt->density = 0;
3318 tpnt->do_auto_lock = ST_AUTO_LOCK;
3319 tpnt->can_bsr = ST_IN_FILE_POS;
3320 tpnt->can_partitions = 0;
3321 tpnt->two_fm = ST_TWO_FM;
3322 tpnt->fast_mteom = ST_FAST_MTEOM;
3323 tpnt->scsi2_logical = ST_SCSI2LOGICAL;
3324 tpnt->write_threshold = st_write_threshold;
3325 tpnt->default_drvbuffer = 0xff; /* No forced buffering */
3326 tpnt->partition = 0;
3327 tpnt->new_partition = 0;
3328 tpnt->nbr_partitions = 0;
3329 tpnt->timeout = ST_TIMEOUT;
3330 tpnt->long_timeout = ST_LONG_TIMEOUT;
3332 for (i = 0; i < ST_NBR_MODES; i++) {
3333 STm = &(tpnt->modes[i]);
3334 STm->defined = FALSE;
3335 STm->sysv = ST_SYSV;
3336 STm->defaults_for_writes = 0;
3337 STm->do_async_writes = ST_ASYNC_WRITES;
3338 STm->do_buffer_writes = ST_BUFFER_WRITES;
3339 STm->do_read_ahead = ST_READ_AHEAD;
3340 STm->default_compression = ST_DONT_TOUCH;
3341 STm->default_blksize = (-1); /* No forced size */
3342 STm->default_density = (-1); /* No forced density */
3345 for (i = 0; i < ST_NBR_PARTITIONS; i++) {
3346 STps = &(tpnt->ps[i]);
3347 STps->rw = ST_IDLE;
3348 STps->eof = ST_NOEOF;
3349 STps->at_sm = 0;
3350 STps->last_block_valid = FALSE;
3351 STps->drv_block = (-1);
3352 STps->drv_file = (-1);
3355 tpnt->current_mode = 0;
3356 tpnt->modes[0].defined = TRUE;
3358 tpnt->density_changed = tpnt->compression_changed =
3359 tpnt->blksize_changed = FALSE;
3361 st_template.nr_dev++;
3362 return 0;
3365 static int st_detect(Scsi_Device * SDp)
3367 if (SDp->type != TYPE_TAPE)
3368 return 0;
3370 printk(KERN_WARNING
3371 "Detected scsi tape st%d at scsi%d, channel %d, id %d, lun %d\n",
3372 st_template.dev_noticed++,
3373 SDp->host->host_no, SDp->channel, SDp->id, SDp->lun);
3375 return 1;
3378 static int st_registered = 0;
3380 /* Driver initialization (not __init because may be called later) */
3381 static int st_init()
3383 int i;
3384 Scsi_Tape *STp;
3385 int target_nbr;
3387 if (st_template.dev_noticed == 0)
3388 return 0;
3390 printk(KERN_INFO "st: bufsize %d, wrt %d, max init. buffers %d, s/g segs %d.\n",
3391 st_buffer_size, st_write_threshold, st_max_buffers, st_max_sg_segs);
3393 if (!st_registered) {
3394 if (register_chrdev(SCSI_TAPE_MAJOR, "st", &st_fops)) {
3395 printk(KERN_ERR "Unable to get major %d for SCSI tapes\n", MAJOR_NR);
3396 return 1;
3398 st_registered++;
3400 if (scsi_tapes)
3401 return 0;
3402 st_template.dev_max = st_template.dev_noticed + ST_EXTRA_DEVS;
3403 if (st_template.dev_max < ST_MAX_TAPES)
3404 st_template.dev_max = ST_MAX_TAPES;
3405 if (st_template.dev_max > 128 / ST_NBR_MODES)
3406 printk(KERN_INFO "st: Only %d tapes accessible.\n", 128 / ST_NBR_MODES);
3407 scsi_tapes =
3408 (Scsi_Tape *) scsi_init_malloc(st_template.dev_max * sizeof(Scsi_Tape),
3409 GFP_ATOMIC);
3410 if (scsi_tapes == NULL) {
3411 printk(KERN_ERR "Unable to allocate descriptors for SCSI tapes.\n");
3412 unregister_chrdev(SCSI_TAPE_MAJOR, "st");
3413 return 1;
3415 #if DEBUG
3416 printk(ST_DEB_MSG "st: Buffer size %d bytes, write threshold %d bytes.\n",
3417 st_buffer_size, st_write_threshold);
3418 #endif
3420 memset(scsi_tapes, 0, st_template.dev_max * sizeof(Scsi_Tape));
3421 for (i = 0; i < st_template.dev_max; ++i) {
3422 STp = &(scsi_tapes[i]);
3423 STp->capacity = 0xfffff;
3424 STp->mt_status = (struct mtget *) scsi_init_malloc(sizeof(struct mtget),
3425 GFP_ATOMIC);
3426 /* Initialize status */
3427 memset((void *) scsi_tapes[i].mt_status, 0, sizeof(struct mtget));
3430 /* Allocate the buffers */
3431 st_buffers =
3432 (ST_buffer **) scsi_init_malloc(st_template.dev_max * sizeof(ST_buffer *),
3433 GFP_ATOMIC);
3434 if (st_buffers == NULL) {
3435 printk(KERN_ERR "Unable to allocate tape buffer pointers.\n");
3436 unregister_chrdev(SCSI_TAPE_MAJOR, "st");
3437 scsi_init_free((char *) scsi_tapes,
3438 st_template.dev_max * sizeof(Scsi_Tape));
3439 return 1;
3441 target_nbr = st_template.dev_noticed;
3442 if (target_nbr < ST_EXTRA_DEVS)
3443 target_nbr = ST_EXTRA_DEVS;
3444 if (target_nbr > st_max_buffers)
3445 target_nbr = st_max_buffers;
3447 for (i = st_nbr_buffers = 0; i < target_nbr; i++) {
3448 if (!new_tape_buffer(TRUE, TRUE)) {
3449 if (i == 0) {
3450 printk(KERN_INFO "No tape buffers allocated at initialization.\n");
3451 break;
3453 printk(KERN_INFO "Number of tape buffers adjusted.\n");
3454 break;
3458 return 0;
3461 static void st_detach(Scsi_Device * SDp)
3463 Scsi_Tape *tpnt;
3464 int i;
3466 for (tpnt = scsi_tapes, i = 0; i < st_template.dev_max; i++, tpnt++)
3467 if (tpnt->device == SDp) {
3468 tpnt->device = NULL;
3469 SDp->attached--;
3470 st_template.nr_dev--;
3471 st_template.dev_noticed--;
3472 return;
3474 return;
3478 #ifdef MODULE
3480 int __init init_module(void)
3482 int result;
3484 validate_options();
3486 st_template.module = &__this_module;
3487 result = scsi_register_module(MODULE_SCSI_DEV, &st_template);
3488 if (result)
3489 return result;
3491 return 0;
3494 void cleanup_module(void)
3496 int i, j;
3498 scsi_unregister_module(MODULE_SCSI_DEV, &st_template);
3499 unregister_chrdev(SCSI_TAPE_MAJOR, "st");
3500 st_registered--;
3501 if (scsi_tapes != NULL) {
3502 scsi_init_free((char *) scsi_tapes,
3503 st_template.dev_max * sizeof(Scsi_Tape));
3505 if (st_buffers != NULL) {
3506 for (i = 0; i < st_nbr_buffers; i++)
3508 if (st_buffers[i] != NULL) {
3509 for (j = 0; j < st_buffers[i]->sg_segs; j++)
3510 scsi_init_free((char *) st_buffers[i]->sg[j].address,
3511 st_buffers[i]->sg[j].length);
3512 scsi_init_free((char *) st_buffers[i], st_buffers[i]->this_size);
3515 scsi_init_free((char *) st_buffers, st_template.dev_max * sizeof(ST_buffer *));
3518 st_template.dev_max = 0;
3519 printk(KERN_INFO "st: Unloaded.\n");
3521 #endif /* MODULE */