Import 2.1.118
[davej-history.git] / drivers / scsi / st.c
blob38b05c29a2094efd07bbba86451e5c0439ae5874
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 - 1997 Kai Makisara
12 email Kai.Makisara@metla.fi
14 Last modified: Wed Nov 5 23:39:52 1997 by makisara@home
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 <asm/uaccess.h>
31 #include <asm/dma.h>
32 #include <asm/system.h>
33 #include <asm/spinlock.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>
50 #include "st.h"
51 #include "constants.h"
53 #ifdef MODULE
54 MODULE_PARM(buffer_kbs, "i");
55 MODULE_PARM(write_threshold_kbs, "i");
56 MODULE_PARM(max_buffers, "i");
57 static int buffer_kbs = 0;
58 static int write_threshold_kbs = 0;
59 static int max_buffers = 0;
60 #endif
62 /* The default definitions have been moved to st_options.h */
64 #define ST_KILOBYTE 1024
66 #include "st_options.h"
68 #define ST_BUFFER_SIZE (ST_BUFFER_BLOCKS * ST_KILOBYTE)
69 #define ST_WRITE_THRESHOLD (ST_WRITE_THRESHOLD_BLOCKS * ST_KILOBYTE)
71 /* The buffer size should fit into the 24 bits for length in the
72 6-byte SCSI read and write commands. */
73 #if ST_BUFFER_SIZE >= (2 << 24 - 1)
74 #error "Buffer size should not exceed (2 << 24 - 1) bytes!"
75 #endif
77 #if DEBUG
78 static int debugging = 1;
79 #endif
81 #define MAX_RETRIES 0
82 #define MAX_WRITE_RETRIES 0
83 #define MAX_READY_RETRIES 5
84 #define NO_TAPE NOT_READY
86 #define ST_TIMEOUT (900 * HZ)
87 #define ST_LONG_TIMEOUT (14000 * HZ)
89 #define TAPE_NR(x) (MINOR(x) & ~(128 | ST_MODE_MASK))
90 #define TAPE_MODE(x) ((MINOR(x) & ST_MODE_MASK) >> ST_MODE_SHIFT)
92 /* Internal ioctl to set both density (uppermost 8 bits) and blocksize (lower
93 24 bits) */
94 #define SET_DENS_AND_BLK 0x10001
96 static int st_nbr_buffers;
97 static ST_buffer **st_buffers;
98 static int st_buffer_size = ST_BUFFER_SIZE;
99 static int st_write_threshold = ST_WRITE_THRESHOLD;
100 static int st_max_buffers = ST_MAX_BUFFERS;
102 static Scsi_Tape * scsi_tapes = NULL;
104 static int modes_defined = FALSE;
106 static ST_buffer *new_tape_buffer(int, int);
107 static int enlarge_buffer(ST_buffer *, int, int);
108 static void normalize_buffer(ST_buffer *);
110 static int st_init(void);
111 static int st_attach(Scsi_Device *);
112 static int st_detect(Scsi_Device *);
113 static void st_detach(Scsi_Device *);
115 struct Scsi_Device_Template st_template = {NULL, "tape", "st", NULL, TYPE_TAPE,
116 SCSI_TAPE_MAJOR, 0, 0, 0, 0,
117 st_detect, st_init,
118 NULL, st_attach, st_detach};
120 static int st_compression(Scsi_Tape *, int);
122 static int find_partition(struct inode *);
123 static int update_partition(struct inode *);
125 static int st_int_ioctl(struct inode * inode, unsigned int cmd_in,
126 unsigned long arg);
131 /* Convert the result to success code */
132 static int
133 st_chk_result(Scsi_Cmnd * SCpnt)
135 int dev = TAPE_NR(SCpnt->request.rq_dev);
136 int result = SCpnt->result;
137 unsigned char * sense = SCpnt->sense_buffer, scode;
138 #if DEBUG
139 const char *stp;
140 #endif
142 if (!result /* && SCpnt->sense_buffer[0] == 0 */ )
143 return 0;
145 scode = sense[2] & 0x0f;
147 #if DEBUG
148 if (debugging) {
149 printk(ST_DEB_MSG "st%d: Error: %x, cmd: %x %x %x %x %x %x Len: %d\n",
150 dev, result,
151 SCpnt->data_cmnd[0], SCpnt->data_cmnd[1], SCpnt->data_cmnd[2],
152 SCpnt->data_cmnd[3], SCpnt->data_cmnd[4], SCpnt->data_cmnd[5],
153 SCpnt->request_bufflen);
154 if (driver_byte(result) & DRIVER_SENSE)
155 print_sense("st", SCpnt);
156 else
157 printk("\n");
159 else
160 #endif
161 if (!(driver_byte(result) & DRIVER_SENSE) ||
162 ((sense[0] & 0x70) == 0x70 &&
163 scode != NO_SENSE &&
164 scode != RECOVERED_ERROR &&
165 /* scode != UNIT_ATTENTION && */
166 scode != BLANK_CHECK &&
167 scode != VOLUME_OVERFLOW &&
168 SCpnt->data_cmnd[0] != MODE_SENSE &&
169 SCpnt->data_cmnd[0] != TEST_UNIT_READY)) { /* Abnormal conditions for tape */
170 if (driver_byte(result) & DRIVER_SENSE) {
171 printk(KERN_WARNING "st%d: Error with sense data: ", dev);
172 print_sense("st", SCpnt);
174 else
175 printk(KERN_WARNING "st%d: Error %x.\n", dev, result);
178 if ((sense[0] & 0x70) == 0x70 &&
179 scode == RECOVERED_ERROR
180 #if ST_RECOVERED_WRITE_FATAL
181 && SCpnt->data_cmnd[0] != WRITE_6
182 && SCpnt->data_cmnd[0] != WRITE_FILEMARKS
183 #endif
185 scsi_tapes[dev].recover_count++;
186 scsi_tapes[dev].mt_status->mt_erreg += (1 << MT_ST_SOFTERR_SHIFT);
187 #if DEBUG
188 if (debugging) {
189 if (SCpnt->data_cmnd[0] == READ_6)
190 stp = "read";
191 else if (SCpnt->data_cmnd[0] == WRITE_6)
192 stp = "write";
193 else
194 stp = "ioctl";
195 printk(ST_DEB_MSG "st%d: Recovered %s error (%d).\n", dev, stp,
196 scsi_tapes[dev].recover_count);
198 #endif
199 if ((sense[2] & 0xe0) == 0)
200 return 0;
202 return (-EIO);
206 /* Wakeup from interrupt */
207 static void
208 st_sleep_done (Scsi_Cmnd * SCpnt)
210 unsigned int st_nbr;
211 int remainder;
212 Scsi_Tape * STp;
214 if ((st_nbr = TAPE_NR(SCpnt->request.rq_dev)) < st_template.nr_dev) {
215 STp = &(scsi_tapes[st_nbr]);
216 if ((STp->buffer)->writing &&
217 (SCpnt->sense_buffer[0] & 0x70) == 0x70 &&
218 (SCpnt->sense_buffer[2] & 0x40)) {
219 /* EOM at write-behind, has all been written? */
220 if ((SCpnt->sense_buffer[0] & 0x80) != 0)
221 remainder = (SCpnt->sense_buffer[3] << 24) |
222 (SCpnt->sense_buffer[4] << 16) |
223 (SCpnt->sense_buffer[5] << 8) | SCpnt->sense_buffer[6];
224 else
225 remainder = 0;
226 if ((SCpnt->sense_buffer[2] & 0x0f) == VOLUME_OVERFLOW ||
227 remainder > 0)
228 (STp->buffer)->last_result = SCpnt->result; /* Error */
229 else
230 (STp->buffer)->last_result = INT_MAX; /* OK */
232 else
233 (STp->buffer)->last_result = SCpnt->result;
234 SCpnt->request.rq_status = RQ_SCSI_DONE;
235 (STp->buffer)->last_SCpnt = SCpnt;
237 #if DEBUG
238 STp->write_pending = 0;
239 #endif
240 up(SCpnt->request.sem);
242 #if DEBUG
243 else if (debugging)
244 printk(KERN_ERR "st?: Illegal interrupt device %x\n", st_nbr);
245 #endif
249 /* Do the scsi command */
250 static Scsi_Cmnd *
251 st_do_scsi(Scsi_Cmnd *SCpnt, Scsi_Tape *STp, unsigned char *cmd, int bytes,
252 int timeout, int retries)
254 unsigned long flags;
256 spin_lock_irqsave(&io_request_lock, flags);
257 if (SCpnt == NULL)
258 if ((SCpnt = scsi_allocate_device(NULL, STp->device, 1)) == NULL) {
259 printk(KERN_ERR "st%d: Can't get SCSI request.\n", TAPE_NR(STp->devt));
260 return NULL;
263 cmd[1] |= (SCpnt->lun << 5) & 0xe0;
264 STp->sem = MUTEX_LOCKED;
265 SCpnt->request.sem = &(STp->sem);
266 SCpnt->request.rq_status = RQ_SCSI_BUSY;
267 SCpnt->request.rq_dev = STp->devt;
269 scsi_do_cmd(SCpnt, (void *)cmd, (STp->buffer)->b_data, bytes,
270 st_sleep_done, timeout, retries);
271 spin_unlock_irqrestore(&io_request_lock, flags);
273 down(SCpnt->request.sem);
275 (STp->buffer)->last_result_fatal = st_chk_result(SCpnt);
277 return SCpnt;
281 /* Handle the write-behind checking */
282 static void
283 write_behind_check(Scsi_Tape *STp)
285 ST_buffer * STbuffer;
286 ST_partstat * STps;
288 STbuffer = STp->buffer;
290 #if DEBUG
291 if (STp->write_pending)
292 STp->nbr_waits++;
293 else
294 STp->nbr_finished++;
295 #endif
297 down(&(STp->sem));
299 (STp->buffer)->last_result_fatal = st_chk_result((STp->buffer)->last_SCpnt);
300 scsi_release_command((STp->buffer)->last_SCpnt);
302 if (STbuffer->writing < STbuffer->buffer_bytes)
303 memcpy(STbuffer->b_data,
304 STbuffer->b_data + STbuffer->writing,
305 STbuffer->buffer_bytes - STbuffer->writing);
306 STbuffer->buffer_bytes -= STbuffer->writing;
307 STps = &(STp->ps[STp->partition]);
308 if (STps->drv_block >= 0) {
309 if (STp->block_size == 0)
310 STps->drv_block++;
311 else
312 STps->drv_block += STbuffer->writing / STp->block_size;
314 STbuffer->writing = 0;
316 return;
320 /* Step over EOF if it has been inadvertently crossed (ioctl not used because
321 it messes up the block number). */
322 static int
323 cross_eof(Scsi_Tape *STp, int forward)
325 Scsi_Cmnd *SCpnt;
326 unsigned char cmd[10];
328 cmd[0] = SPACE;
329 cmd[1] = 0x01; /* Space FileMarks */
330 if (forward) {
331 cmd[2] = cmd[3] = 0;
332 cmd[4] = 1;
334 else
335 cmd[2] = cmd[3] = cmd[4] = 0xff; /* -1 filemarks */
336 cmd[5] = 0;
337 #if DEBUG
338 if (debugging)
339 printk(ST_DEB_MSG "st%d: Stepping over filemark %s.\n",
340 TAPE_NR(STp->devt), forward ? "forward" : "backward");
341 #endif
343 SCpnt = st_do_scsi(NULL, STp, cmd, 0, STp->timeout, MAX_RETRIES);
344 if (!SCpnt)
345 return (-EBUSY);
347 scsi_release_command(SCpnt);
348 SCpnt = NULL;
350 if ((STp->buffer)->last_result != 0)
351 printk(KERN_ERR "st%d: Stepping over filemark %s failed.\n",
352 TAPE_NR(STp->devt), forward ? "forward" : "backward");
354 return (STp->buffer)->last_result_fatal;
358 /* Flush the write buffer (never need to write if variable blocksize). */
359 static int
360 flush_write_buffer(Scsi_Tape *STp)
362 int offset, transfer, blks;
363 int result;
364 unsigned char cmd[10];
365 Scsi_Cmnd *SCpnt;
366 ST_partstat * STps;
368 if ((STp->buffer)->writing) {
369 write_behind_check(STp);
370 if ((STp->buffer)->last_result_fatal) {
371 #if DEBUG
372 if (debugging)
373 printk(ST_DEB_MSG "st%d: Async write error (flush) %x.\n",
374 TAPE_NR(STp->devt), (STp->buffer)->last_result);
375 #endif
376 if ((STp->buffer)->last_result == INT_MAX)
377 return (-ENOSPC);
378 return (-EIO);
382 if (STp->block_size == 0)
383 return 0;
385 result = 0;
386 if (STp->dirty == 1) {
388 offset = (STp->buffer)->buffer_bytes;
389 transfer = ((offset + STp->block_size - 1) /
390 STp->block_size) * STp->block_size;
391 #if DEBUG
392 if (debugging)
393 printk(ST_DEB_MSG "st%d: Flushing %d bytes.\n", TAPE_NR(STp->devt), transfer);
394 #endif
395 memset((STp->buffer)->b_data + offset, 0, transfer - offset);
397 memset(cmd, 0, 10);
398 cmd[0] = WRITE_6;
399 cmd[1] = 1;
400 blks = transfer / STp->block_size;
401 cmd[2] = blks >> 16;
402 cmd[3] = blks >> 8;
403 cmd[4] = blks;
405 SCpnt = st_do_scsi(NULL, STp, cmd, transfer, STp->timeout, MAX_WRITE_RETRIES);
406 if (!SCpnt)
407 return (-EBUSY);
409 STps = &(STp->ps[STp->partition]);
410 if ((STp->buffer)->last_result_fatal != 0) {
411 if ((SCpnt->sense_buffer[0] & 0x70) == 0x70 &&
412 (SCpnt->sense_buffer[2] & 0x40) &&
413 (SCpnt->sense_buffer[2] & 0x0f) == NO_SENSE) {
414 STp->dirty = 0;
415 (STp->buffer)->buffer_bytes = 0;
416 result = (-ENOSPC);
418 else {
419 printk(KERN_ERR "st%d: Error on flush.\n", TAPE_NR(STp->devt));
420 result = (-EIO);
422 STps->drv_block = (-1);
424 else {
425 if (STps->drv_block >= 0)
426 STps->drv_block += blks;
427 STp->dirty = 0;
428 (STp->buffer)->buffer_bytes = 0;
430 scsi_release_command(SCpnt);
431 SCpnt = NULL;
433 return result;
437 /* Flush the tape buffer. The tape will be positioned correctly unless
438 seek_next is true. */
439 static int
440 flush_buffer(struct inode * inode, struct file * filp, int seek_next)
442 int backspace, result;
443 Scsi_Tape * STp;
444 ST_buffer * STbuffer;
445 ST_partstat * STps;
446 int dev = TAPE_NR(inode->i_rdev);
448 STp = &(scsi_tapes[dev]);
449 STbuffer = STp->buffer;
452 * If there was a bus reset, block further access
453 * to this device.
455 if( STp->device->was_reset )
456 return (-EIO);
458 if (STp->ready != ST_READY)
459 return 0;
461 STps = &(STp->ps[STp->partition]);
462 if (STps->rw == ST_WRITING) /* Writing */
463 return flush_write_buffer(STp);
465 if (STp->block_size == 0)
466 return 0;
468 backspace = ((STp->buffer)->buffer_bytes +
469 (STp->buffer)->read_pointer) / STp->block_size -
470 ((STp->buffer)->read_pointer + STp->block_size - 1) /
471 STp->block_size;
472 (STp->buffer)->buffer_bytes = 0;
473 (STp->buffer)->read_pointer = 0;
474 result = 0;
475 if (!seek_next) {
476 if (STps->eof == ST_FM_HIT) {
477 result = cross_eof(STp, FALSE); /* Back over the EOF hit */
478 if (!result)
479 STps->eof = ST_NOEOF;
480 else {
481 if (STps->drv_file >= 0)
482 STps->drv_file++;
483 STps->drv_block = 0;
486 if (!result && backspace > 0)
487 result = st_int_ioctl(inode, MTBSR, backspace);
489 else if (STps->eof == ST_FM_HIT) {
490 if (STps->drv_file >= 0)
491 STps->drv_file++;
492 STps->drv_block = 0;
493 STps->eof = ST_NOEOF;
496 return result;
500 \f/* Set the mode parameters */
501 static int
502 set_mode_densblk(struct inode * inode, Scsi_Tape *STp, ST_mode *STm)
504 int set_it = FALSE;
505 unsigned long arg;
506 int dev = TAPE_NR(inode->i_rdev);
508 if (!STp->density_changed &&
509 STm->default_density >= 0 &&
510 STm->default_density != STp->density) {
511 arg = STm->default_density;
512 set_it = TRUE;
514 else
515 arg = STp->density;
516 arg <<= MT_ST_DENSITY_SHIFT;
517 if (!STp->blksize_changed &&
518 STm->default_blksize >= 0 &&
519 STm->default_blksize != STp->block_size) {
520 arg |= STm->default_blksize;
521 set_it = TRUE;
523 else
524 arg |= STp->block_size;
525 if (set_it &&
526 st_int_ioctl(inode, SET_DENS_AND_BLK, arg)) {
527 printk(KERN_WARNING
528 "st%d: Can't set default block size to %d bytes and density %x.\n",
529 dev, STm->default_blksize, STm->default_density);
530 if (modes_defined)
531 return (-EINVAL);
533 return 0;
537 /* Open the device */
538 static int
539 scsi_tape_open(struct inode * inode, struct file * filp)
541 unsigned short flags;
542 int i, need_dma_buffer, new_session = FALSE;
543 unsigned char cmd[10];
544 Scsi_Cmnd * SCpnt;
545 Scsi_Tape * STp;
546 ST_mode * STm;
547 ST_partstat * STps;
548 int dev = TAPE_NR(inode->i_rdev);
549 int mode = TAPE_MODE(inode->i_rdev);
551 if (dev >= st_template.dev_max || !scsi_tapes[dev].device)
552 return (-ENXIO);
554 if( !scsi_block_when_processing_errors(scsi_tapes[dev].device) )
556 return -ENXIO;
559 STp = &(scsi_tapes[dev]);
560 if (STp->in_use) {
561 #if DEBUG
562 printk(ST_DEB_MSG "st%d: Device already in use.\n", dev);
563 #endif
564 return (-EBUSY);
566 STp->rew_at_close = (MINOR(inode->i_rdev) & 0x80) == 0;
568 if (mode != STp->current_mode) {
569 #if DEBUG
570 if (debugging)
571 printk(ST_DEB_MSG "st%d: Mode change from %d to %d.\n",
572 dev, STp->current_mode, mode);
573 #endif
574 new_session = TRUE;
575 STp->current_mode = mode;
577 STm = &(STp->modes[STp->current_mode]);
579 /* Allocate buffer for this user */
580 need_dma_buffer = STp->restr_dma;
581 for (i=0; i < st_nbr_buffers; i++)
582 if (!st_buffers[i]->in_use &&
583 (!need_dma_buffer || st_buffers[i]->dma))
584 break;
585 if (i >= st_nbr_buffers) {
586 STp->buffer = new_tape_buffer(FALSE, need_dma_buffer);
587 if (STp->buffer == NULL) {
588 printk(KERN_WARNING "st%d: Can't allocate tape buffer.\n", dev);
589 return (-EBUSY);
592 else
593 STp->buffer = st_buffers[i];
594 (STp->buffer)->in_use = 1;
595 (STp->buffer)->writing = 0;
596 (STp->buffer)->last_result_fatal = 0;
598 flags = filp->f_flags;
599 STp->write_prot = ((flags & O_ACCMODE) == O_RDONLY);
601 STp->dirty = 0;
602 for (i=0; i < ST_NBR_PARTITIONS; i++) {
603 STps = &(STp->ps[i]);
604 STps->rw = ST_IDLE;
606 STp->ready = ST_READY;
607 STp->recover_count = 0;
608 #if DEBUG
609 STp->nbr_waits = STp->nbr_finished = 0;
610 #endif
612 if (scsi_tapes[dev].device->host->hostt->module)
613 __MOD_INC_USE_COUNT(scsi_tapes[dev].device->host->hostt->module);
614 if(st_template.module)
615 __MOD_INC_USE_COUNT(st_template.module);
617 memset ((void *) &cmd[0], 0, 10);
618 cmd[0] = TEST_UNIT_READY;
620 SCpnt = st_do_scsi(NULL, STp, cmd, 0, STp->long_timeout, MAX_READY_RETRIES);
621 if (!SCpnt) {
622 if (scsi_tapes[dev].device->host->hostt->module)
623 __MOD_DEC_USE_COUNT(scsi_tapes[dev].device->host->hostt->module);
624 if(st_template.module)
625 __MOD_DEC_USE_COUNT(st_template.module);
626 return (-EBUSY);
629 if ((SCpnt->sense_buffer[0] & 0x70) == 0x70 &&
630 (SCpnt->sense_buffer[2] & 0x0f) == UNIT_ATTENTION) { /* New media? */
631 memset ((void *) &cmd[0], 0, 10);
632 cmd[0] = TEST_UNIT_READY;
634 SCpnt = st_do_scsi(SCpnt, STp, cmd, 0, STp->long_timeout, MAX_READY_RETRIES);
636 (STp->device)->was_reset = 0;
637 STp->partition = STp->new_partition = 0;
638 if (STp->can_partitions)
639 STp->nbr_partitions = 1; /* This guess will be updated later if necessary */
640 for (i=0; i < ST_NBR_PARTITIONS; i++) {
641 STps = &(STp->ps[i]);
642 STps->rw = ST_IDLE;
643 STps->eof = ST_NOEOF;
644 STps->at_sm = 0;
645 STps->last_block_valid = FALSE;
646 STps->drv_block = 0;
647 STps->drv_file = 0 ;
649 new_session = TRUE;
652 if ((STp->buffer)->last_result_fatal != 0) {
653 if ((STp->device)->scsi_level >= SCSI_2 &&
654 (SCpnt->sense_buffer[0] & 0x70) == 0x70 &&
655 (SCpnt->sense_buffer[2] & 0x0f) == NOT_READY &&
656 SCpnt->sense_buffer[12] == 0x3a) { /* Check ASC */
657 STp->ready = ST_NO_TAPE;
658 } else
659 STp->ready = ST_NOT_READY;
660 scsi_release_command(SCpnt);
661 SCpnt = NULL;
662 STp->density = 0; /* Clear the erroneous "residue" */
663 STp->write_prot = 0;
664 STp->block_size = 0;
665 STp->ps[0].drv_file = STp->ps[0].drv_block = 0;
666 STp->partition = STp->new_partition = 0;
667 STp->door_locked = ST_UNLOCKED;
668 STp->in_use = 1;
669 return 0;
672 if (STp->omit_blklims)
673 STp->min_block = STp->max_block = (-1);
674 else {
675 memset ((void *) &cmd[0], 0, 10);
676 cmd[0] = READ_BLOCK_LIMITS;
678 SCpnt = st_do_scsi(SCpnt, STp, cmd, 6, STp->timeout, MAX_READY_RETRIES);
680 if (!SCpnt->result && !SCpnt->sense_buffer[0]) {
681 STp->max_block = ((STp->buffer)->b_data[1] << 16) |
682 ((STp->buffer)->b_data[2] << 8) | (STp->buffer)->b_data[3];
683 STp->min_block = ((STp->buffer)->b_data[4] << 8) |
684 (STp->buffer)->b_data[5];
685 #if DEBUG
686 if (debugging)
687 printk(ST_DEB_MSG "st%d: Block limits %d - %d bytes.\n", dev, STp->min_block,
688 STp->max_block);
689 #endif
691 else {
692 STp->min_block = STp->max_block = (-1);
693 #if DEBUG
694 if (debugging)
695 printk(ST_DEB_MSG "st%d: Can't read block limits.\n", dev);
696 #endif
700 memset ((void *) &cmd[0], 0, 10);
701 cmd[0] = MODE_SENSE;
702 cmd[4] = 12;
704 SCpnt = st_do_scsi(SCpnt, STp, cmd, 12, STp->timeout, MAX_READY_RETRIES);
706 if ((STp->buffer)->last_result_fatal != 0) {
707 #if DEBUG
708 if (debugging)
709 printk(ST_DEB_MSG "st%d: No Mode Sense.\n", dev);
710 #endif
711 STp->block_size = ST_DEFAULT_BLOCK; /* Educated guess (?) */
712 (STp->buffer)->last_result_fatal = 0; /* Prevent error propagation */
713 STp->drv_write_prot = 0;
715 else {
717 #if DEBUG
718 if (debugging)
719 printk(ST_DEB_MSG "st%d: Mode sense. Length %d, medium %x, WBS %x, BLL %d\n",
720 dev,
721 (STp->buffer)->b_data[0], (STp->buffer)->b_data[1],
722 (STp->buffer)->b_data[2], (STp->buffer)->b_data[3]);
723 #endif
725 if ((STp->buffer)->b_data[3] >= 8) {
726 STp->drv_buffer = ((STp->buffer)->b_data[2] >> 4) & 7;
727 STp->density = (STp->buffer)->b_data[4];
728 STp->block_size = (STp->buffer)->b_data[9] * 65536 +
729 (STp->buffer)->b_data[10] * 256 + (STp->buffer)->b_data[11];
730 #if DEBUG
731 if (debugging)
732 printk(ST_DEB_MSG "st%d: Density %x, tape length: %x, drv buffer: %d\n",
733 dev, STp->density, (STp->buffer)->b_data[5] * 65536 +
734 (STp->buffer)->b_data[6] * 256 + (STp->buffer)->b_data[7],
735 STp->drv_buffer);
736 #endif
739 if (STp->block_size > (STp->buffer)->buffer_size &&
740 !enlarge_buffer(STp->buffer, STp->block_size, STp->restr_dma)) {
741 printk(KERN_NOTICE "st%d: Blocksize %d too large for buffer.\n", dev,
742 STp->block_size);
743 (STp->buffer)->in_use = 0;
744 STp->buffer = NULL;
745 if (scsi_tapes[dev].device->host->hostt->module)
746 __MOD_DEC_USE_COUNT(scsi_tapes[dev].device->host->hostt->module);
747 if(st_template.module)
748 __MOD_DEC_USE_COUNT(st_template.module);
749 return (-EIO);
751 STp->drv_write_prot = ((STp->buffer)->b_data[2] & 0x80) != 0;
753 scsi_release_command(SCpnt);
754 SCpnt = NULL;
756 if (STp->block_size > 0)
757 (STp->buffer)->buffer_blocks = st_buffer_size / STp->block_size;
758 else
759 (STp->buffer)->buffer_blocks = 1;
760 (STp->buffer)->buffer_bytes = (STp->buffer)->read_pointer = 0;
762 #if DEBUG
763 if (debugging)
764 printk(ST_DEB_MSG "st%d: Block size: %d, buffer size: %d (%d blocks).\n", dev,
765 STp->block_size, (STp->buffer)->buffer_size,
766 (STp->buffer)->buffer_blocks);
767 #endif
769 if (STp->drv_write_prot) {
770 STp->write_prot = 1;
771 #if DEBUG
772 if (debugging)
773 printk(ST_DEB_MSG "st%d: Write protected\n", dev);
774 #endif
775 if ((flags & O_ACCMODE) == O_WRONLY || (flags & O_ACCMODE) == O_RDWR) {
776 (STp->buffer)->in_use = 0;
777 STp->buffer = NULL;
778 if (scsi_tapes[dev].device->host->hostt->module)
779 __MOD_DEC_USE_COUNT(scsi_tapes[dev].device->host->hostt->module);
780 if(st_template.module)
781 __MOD_DEC_USE_COUNT(st_template.module);
782 return (-EROFS);
786 if (STp->can_partitions && STp->nbr_partitions < 1) {
787 /* This code is reached when the device is opened for the first time
788 after the driver has been initialized with tape in the drive and the
789 partition support has been enabled. */
790 #if DEBUG
791 if (debugging)
792 printk(ST_DEB_MSG "st%d: Updating partition number in status.\n", dev);
793 #endif
794 if ((STp->partition = find_partition(inode)) < 0) {
795 (STp->buffer)->in_use = 0;
796 STp->buffer = NULL;
797 if (scsi_tapes[dev].device->host->hostt->module)
798 __MOD_DEC_USE_COUNT(scsi_tapes[dev].device->host->hostt->module);
799 if(st_template.module)
800 __MOD_DEC_USE_COUNT(st_template.module);
801 return STp->partition;
803 STp->new_partition = STp->partition;
804 STp->nbr_partitions = 1; /* This guess will be updated when necessary */
807 if (new_session) { /* Change the drive parameters for the new mode */
808 STp->density_changed = STp->blksize_changed = FALSE;
809 STp->compression_changed = FALSE;
810 if (!(STm->defaults_for_writes) &&
811 (i = set_mode_densblk(inode, STp, STm)) < 0) {
812 (STp->buffer)->in_use = 0;
813 STp->buffer = NULL;
814 if (scsi_tapes[dev].device->host->hostt->module)
815 __MOD_DEC_USE_COUNT(scsi_tapes[dev].device->host->hostt->module);
816 if(st_template.module)
817 __MOD_DEC_USE_COUNT(st_template.module);
818 return i;
820 if (STp->default_drvbuffer != 0xff) {
821 if (st_int_ioctl(inode, MTSETDRVBUFFER, STp->default_drvbuffer))
822 printk(KERN_WARNING "st%d: Can't set default drive buffering to %d.\n",
823 dev, STp->default_drvbuffer);
827 STp->in_use = 1;
829 return 0;
833 /* Close the device*/
834 static int
835 scsi_tape_close(struct inode * inode, struct file * filp)
837 int result = 0, result2;
838 static unsigned char cmd[10];
839 Scsi_Cmnd * SCpnt;
840 Scsi_Tape * STp;
841 ST_mode * STm;
842 ST_partstat * STps;
844 kdev_t devt = inode->i_rdev;
845 int dev;
847 dev = TAPE_NR(devt);
848 STp = &(scsi_tapes[dev]);
849 STm = &(STp->modes[STp->current_mode]);
850 STps = &(STp->ps[STp->partition]);
852 if (STp->can_partitions &&
853 (result = update_partition(inode)) < 0) {
854 #if DEBUG
855 if (debugging)
856 printk(ST_DEB_MSG "st%d: update_partition at close failed.\n", dev);
857 #endif
858 goto out;
861 if ( STps->rw == ST_WRITING && !(STp->device)->was_reset) {
863 result = flush_write_buffer(STp);
865 #if DEBUG
866 if (debugging) {
867 printk(ST_DEB_MSG "st%d: File length %ld bytes.\n",
868 dev, (long)(filp->f_pos));
869 printk(ST_DEB_MSG "st%d: Async write waits %d, finished %d.\n",
870 dev, STp->nbr_waits, STp->nbr_finished);
872 #endif
874 if (result == 0 || result == (-ENOSPC)) {
876 memset(cmd, 0, 10);
877 cmd[0] = WRITE_FILEMARKS;
878 cmd[4] = 1 + STp->two_fm;
880 SCpnt = st_do_scsi(NULL, STp, cmd, 0, STp->timeout, MAX_WRITE_RETRIES);
881 if (!SCpnt)
882 goto out;
884 if ((STp->buffer)->last_result_fatal != 0 &&
885 ((SCpnt->sense_buffer[0] & 0x70) != 0x70 ||
886 (SCpnt->sense_buffer[2] & 0x4f) != 0x40 ||
887 ((SCpnt->sense_buffer[0] & 0x80) != 0 &&
888 (SCpnt->sense_buffer[3] | SCpnt->sense_buffer[4] |
889 SCpnt->sense_buffer[5] |
890 SCpnt->sense_buffer[6]) == 0))) {
891 /* Filter out successful write at EOM */
892 scsi_release_command(SCpnt);
893 SCpnt = NULL;
894 printk(KERN_ERR "st%d: Error on write filemark.\n", dev);
895 if (result == 0)
896 result = (-EIO);
898 else {
899 scsi_release_command(SCpnt);
900 SCpnt = NULL;
901 if (STps->drv_file >= 0)
902 STps->drv_file++ ;
903 STps->drv_block = 0;
904 if (STp->two_fm)
905 cross_eof(STp, FALSE);
906 STps->eof = ST_FM;
910 #if DEBUG
911 if (debugging)
912 printk(ST_DEB_MSG "st%d: Buffer flushed, %d EOF(s) written\n",
913 dev, cmd[4]);
914 #endif
916 else if (!STp->rew_at_close) {
917 STps = &(STp->ps[STp->partition]);
918 if (!STm->sysv || STps->rw != ST_READING) {
919 if (STp->can_bsr)
920 result = flush_buffer(inode, filp, 0);
921 else if (STps->eof == ST_FM_HIT) {
922 result = cross_eof(STp, FALSE);
923 if (result) {
924 if (STps->drv_file >= 0)
925 STps->drv_file++;
926 STps->drv_block = 0;
927 STps->eof = ST_FM;
929 else
930 STps->eof = ST_NOEOF;
933 else if ((STps->eof == ST_NOEOF &&
934 !(result = cross_eof(STp, TRUE))) ||
935 STps->eof == ST_FM_HIT) {
936 if (STps->drv_file >= 0)
937 STps->drv_file++;
938 STps->drv_block = 0;
939 STps->eof = ST_FM;
943 out:
944 if (STp->rew_at_close) {
945 result2 = st_int_ioctl(inode, MTREW, 1);
946 if (result == 0)
947 result = result2;
950 if (STp->door_locked == ST_LOCKED_AUTO)
951 st_int_ioctl(inode, MTUNLOCK, 0);
953 if (STp->buffer != NULL) {
954 normalize_buffer(STp->buffer);
955 (STp->buffer)->in_use = 0;
958 STp->in_use = 0;
959 if (scsi_tapes[dev].device->host->hostt->module)
960 __MOD_DEC_USE_COUNT(scsi_tapes[dev].device->host->hostt->module);
961 if(st_template.module)
962 __MOD_DEC_USE_COUNT(st_template.module);
964 return result;
968 /* Write command */
969 static ssize_t
970 st_write(struct file * filp, const char * buf, size_t count, loff_t *ppos)
972 struct inode *inode = filp->f_dentry->d_inode;
973 ssize_t total;
974 ssize_t i, do_count, blks, retval, transfer;
975 int write_threshold;
976 int doing_write = 0;
977 static unsigned char cmd[10];
978 const char *b_point;
979 Scsi_Cmnd * SCpnt = NULL;
980 Scsi_Tape * STp;
981 ST_mode * STm;
982 ST_partstat * STps;
983 int dev = TAPE_NR(inode->i_rdev);
984 unsigned long flags;
986 STp = &(scsi_tapes[dev]);
989 * If we are in the middle of error recovery, don't let anyone
990 * else try and use this device. Also, if error recovery fails, it
991 * may try and take the device offline, in which case all further
992 * access to the device is prohibited.
994 if( !scsi_block_when_processing_errors(STp->device) )
996 return -ENXIO;
999 if (ppos != &filp->f_pos) {
1000 /* "A request was outside the capabilities of the device." */
1001 return -ENXIO;
1004 if (STp->ready != ST_READY) {
1005 if (STp->ready == ST_NO_TAPE)
1006 return (-ENOMEDIUM);
1007 else
1008 return (-EIO);
1010 STm = &(STp->modes[STp->current_mode]);
1011 if (!STm->defined)
1012 return (-ENXIO);
1013 if (count == 0)
1014 return 0;
1017 * If there was a bus reset, block further access
1018 * to this device.
1020 if( STp->device->was_reset )
1021 return (-EIO);
1023 #if DEBUG
1024 if (!STp->in_use) {
1025 printk(ST_DEB_MSG "st%d: Incorrect device.\n", dev);
1026 return (-EIO);
1028 #endif
1030 if (STp->can_partitions &&
1031 (retval = update_partition(inode)) < 0)
1032 return retval;
1033 STps = &(STp->ps[STp->partition]);
1035 if (STp->write_prot)
1036 return (-EACCES);
1038 if (STp->block_size == 0 &&
1039 count > (STp->buffer)->buffer_size &&
1040 !enlarge_buffer(STp->buffer, count, STp->restr_dma))
1041 return (-EOVERFLOW);
1043 if (STp->do_auto_lock && STp->door_locked == ST_UNLOCKED &&
1044 !st_int_ioctl(inode, MTLOCK, 0))
1045 STp->door_locked = ST_LOCKED_AUTO;
1047 if (STps->rw == ST_READING) {
1048 retval = flush_buffer(inode, filp, 0);
1049 if (retval)
1050 return retval;
1051 STps->rw = ST_WRITING;
1053 else if (STps->rw != ST_WRITING &&
1054 STps->drv_file == 0 && STps->drv_block == 0) {
1055 if ((retval = set_mode_densblk(inode, STp, STm)) < 0)
1056 return retval;
1057 if (STm->default_compression != ST_DONT_TOUCH &&
1058 !(STp->compression_changed)) {
1059 if (st_compression(STp, (STm->default_compression == ST_YES))) {
1060 printk(KERN_WARNING "st%d: Can't set default compression.\n",
1061 dev);
1062 if (modes_defined)
1063 return (-EINVAL);
1068 if ((STp->buffer)->writing) {
1069 write_behind_check(STp);
1070 if ((STp->buffer)->last_result_fatal) {
1071 #if DEBUG
1072 if (debugging)
1073 printk(ST_DEB_MSG "st%d: Async write error (write) %x.\n", dev,
1074 (STp->buffer)->last_result);
1075 #endif
1076 if ((STp->buffer)->last_result == INT_MAX)
1077 STps->eof = ST_EOM_OK;
1078 else
1079 STps->eof = ST_EOM_ERROR;
1082 if (STps->eof == ST_EOM_OK)
1083 return (-ENOSPC);
1084 else if (STps->eof == ST_EOM_ERROR)
1085 return (-EIO);
1087 /* Check the buffer readability in cases where copy_user might catch
1088 the problems after some tape movement. */
1089 if (STp->block_size != 0 &&
1090 (copy_from_user(&i, buf, 1) != 0 ||
1091 copy_from_user(&i, buf + count - 1, 1) != 0))
1092 return (-EFAULT);
1094 if (!STm->do_buffer_writes) {
1095 if (STp->block_size != 0 && (count % STp->block_size) != 0)
1096 return (-EIO); /* Write must be integral number of blocks */
1097 write_threshold = 1;
1099 else
1100 write_threshold = (STp->buffer)->buffer_blocks * STp->block_size;
1101 if (!STm->do_async_writes)
1102 write_threshold--;
1104 total = count;
1106 memset(cmd, 0, 10);
1107 cmd[0] = WRITE_6;
1108 cmd[1] = (STp->block_size != 0);
1110 STps->rw = ST_WRITING;
1112 b_point = buf;
1113 while((STp->block_size == 0 && !STm->do_async_writes && count > 0) ||
1114 (STp->block_size != 0 &&
1115 (STp->buffer)->buffer_bytes + count > write_threshold))
1117 doing_write = 1;
1118 if (STp->block_size == 0)
1119 do_count = count;
1120 else {
1121 do_count = (STp->buffer)->buffer_blocks * STp->block_size -
1122 (STp->buffer)->buffer_bytes;
1123 if (do_count > count)
1124 do_count = count;
1127 i = copy_from_user((STp->buffer)->b_data +
1128 (STp->buffer)->buffer_bytes, b_point, do_count);
1129 if (i) {
1130 if (SCpnt != NULL)
1132 scsi_release_command(SCpnt);
1133 SCpnt = NULL;
1135 return (-EFAULT);
1138 if (STp->block_size == 0)
1139 blks = transfer = do_count;
1140 else {
1141 blks = ((STp->buffer)->buffer_bytes + do_count) /
1142 STp->block_size;
1143 transfer = blks * STp->block_size;
1145 cmd[2] = blks >> 16;
1146 cmd[3] = blks >> 8;
1147 cmd[4] = blks;
1149 SCpnt = st_do_scsi(SCpnt, STp, cmd, transfer, STp->timeout, MAX_WRITE_RETRIES);
1150 if (!SCpnt)
1151 return (-EBUSY);
1153 if ((STp->buffer)->last_result_fatal != 0) {
1154 #if DEBUG
1155 if (debugging)
1156 printk(ST_DEB_MSG "st%d: Error on write:\n", dev);
1157 #endif
1158 if ((SCpnt->sense_buffer[0] & 0x70) == 0x70 &&
1159 (SCpnt->sense_buffer[2] & 0x40)) {
1160 if (STp->block_size != 0 && (SCpnt->sense_buffer[0] & 0x80) != 0)
1161 transfer = (SCpnt->sense_buffer[3] << 24) |
1162 (SCpnt->sense_buffer[4] << 16) |
1163 (SCpnt->sense_buffer[5] << 8) | SCpnt->sense_buffer[6];
1164 else if (STp->block_size == 0 &&
1165 (SCpnt->sense_buffer[2] & 0x0f) == VOLUME_OVERFLOW)
1166 transfer = do_count;
1167 else
1168 transfer = 0;
1169 if (STp->block_size != 0)
1170 transfer *= STp->block_size;
1171 if (transfer <= do_count) {
1172 filp->f_pos += do_count - transfer;
1173 count -= do_count - transfer;
1174 if (STps->drv_block >= 0) {
1175 if (STp->block_size == 0 && transfer < do_count)
1176 STps->drv_block++;
1177 else if (STp->block_size != 0)
1178 STps->drv_block += (do_count - transfer) / STp->block_size;
1180 STps->eof = ST_EOM_OK;
1181 retval = (-ENOSPC); /* EOM within current request */
1182 #if DEBUG
1183 if (debugging)
1184 printk(ST_DEB_MSG "st%d: EOM with %d bytes unwritten.\n",
1185 dev, transfer);
1186 #endif
1188 else {
1189 STps->eof = ST_EOM_ERROR;
1190 STps->drv_block = (-1); /* Too cautious? */
1191 retval = (-EIO); /* EOM for old data */
1192 #if DEBUG
1193 if (debugging)
1194 printk(ST_DEB_MSG "st%d: EOM with lost data.\n", dev);
1195 #endif
1198 else {
1199 STps->drv_block = (-1); /* Too cautious? */
1200 retval = (-EIO);
1203 scsi_release_command(SCpnt);
1204 SCpnt = NULL;
1205 (STp->buffer)->buffer_bytes = 0;
1206 STp->dirty = 0;
1207 if (count < total)
1208 return total - count;
1209 else
1210 return retval;
1212 filp->f_pos += do_count;
1213 b_point += do_count;
1214 count -= do_count;
1215 if (STps->drv_block >= 0) {
1216 if (STp->block_size == 0)
1217 STps->drv_block++;
1218 else
1219 STps->drv_block += blks;
1221 (STp->buffer)->buffer_bytes = 0;
1222 STp->dirty = 0;
1224 if (count != 0) {
1225 STp->dirty = 1;
1226 i = copy_from_user((STp->buffer)->b_data +
1227 (STp->buffer)->buffer_bytes, b_point, count);
1228 if (i) {
1229 if (SCpnt != NULL)
1231 scsi_release_command(SCpnt);
1232 SCpnt = NULL;
1234 return (-EFAULT);
1236 filp->f_pos += count;
1237 (STp->buffer)->buffer_bytes += count;
1238 count = 0;
1241 if (doing_write && (STp->buffer)->last_result_fatal != 0) {
1242 scsi_release_command(SCpnt);
1243 SCpnt = NULL;
1244 return (STp->buffer)->last_result_fatal;
1247 if (STm->do_async_writes &&
1248 (((STp->buffer)->buffer_bytes >= STp->write_threshold &&
1249 (STp->buffer)->buffer_bytes >= STp->block_size) ||
1250 STp->block_size == 0) ) {
1251 /* Schedule an asynchronous write */
1252 if (!SCpnt) {
1253 SCpnt = scsi_allocate_device(NULL, STp->device, 1);
1254 if (!SCpnt)
1255 return (-EBUSY);
1257 if (STp->block_size == 0)
1258 (STp->buffer)->writing = (STp->buffer)->buffer_bytes;
1259 else
1260 (STp->buffer)->writing = ((STp->buffer)->buffer_bytes /
1261 STp->block_size) * STp->block_size;
1262 STp->dirty = !((STp->buffer)->writing ==
1263 (STp->buffer)->buffer_bytes);
1265 if (STp->block_size == 0)
1266 blks = (STp->buffer)->writing;
1267 else
1268 blks = (STp->buffer)->writing / STp->block_size;
1269 cmd[2] = blks >> 16;
1270 cmd[3] = blks >> 8;
1271 cmd[4] = blks;
1272 STp->sem = MUTEX_LOCKED;
1273 SCpnt->request.sem = &(STp->sem);
1274 SCpnt->request.rq_status = RQ_SCSI_BUSY;
1275 SCpnt->request.rq_dev = STp->devt;
1276 #if DEBUG
1277 STp->write_pending = 1;
1278 #endif
1280 spin_lock_irqsave(&io_request_lock, flags);
1281 scsi_do_cmd (SCpnt,
1282 (void *) cmd, (STp->buffer)->b_data,
1283 (STp->buffer)->writing,
1284 st_sleep_done, STp->timeout, MAX_WRITE_RETRIES);
1285 spin_unlock_irqrestore(&io_request_lock, flags);
1287 else if (SCpnt != NULL)
1289 scsi_release_command(SCpnt);
1290 SCpnt = NULL;
1292 STps->at_sm &= (total == 0);
1293 if (total > 0)
1294 STps->eof = ST_NOEOF;
1296 return( total);
1299 /* Read data from the tape. Returns zero in the normal case, one if the
1300 eof status has changed, and the negative error code in case of a
1301 fatal error. Otherwise updates the buffer and the eof state. */
1302 static long
1303 read_tape(struct inode *inode, long count, Scsi_Cmnd **aSCpnt)
1305 int transfer, blks, bytes;
1306 static unsigned char cmd[10];
1307 Scsi_Cmnd *SCpnt;
1308 Scsi_Tape *STp;
1309 ST_mode * STm;
1310 ST_partstat * STps;
1311 int dev = TAPE_NR(inode->i_rdev);
1312 int retval = 0;
1314 if (count == 0)
1315 return 0;
1317 STp = &(scsi_tapes[dev]);
1318 STm = &(STp->modes[STp->current_mode]);
1319 STps = &(STp->ps[STp->partition]);
1320 if (STps->eof == ST_FM_HIT)
1321 return 1;
1323 memset(cmd, 0, 10);
1324 cmd[0] = READ_6;
1325 cmd[1] = (STp->block_size != 0);
1326 if (STp->block_size == 0)
1327 blks = bytes = count;
1328 else {
1329 if (STm->do_read_ahead) {
1330 blks = (STp->buffer)->buffer_blocks;
1331 bytes = blks * STp->block_size;
1333 else {
1334 bytes = count;
1335 if (bytes > (STp->buffer)->buffer_size)
1336 bytes = (STp->buffer)->buffer_size;
1337 blks = bytes / STp->block_size;
1338 bytes = blks * STp->block_size;
1341 cmd[2] = blks >> 16;
1342 cmd[3] = blks >> 8;
1343 cmd[4] = blks;
1345 SCpnt = *aSCpnt;
1346 SCpnt = st_do_scsi(SCpnt, STp, cmd, bytes, STp->timeout, MAX_RETRIES);
1347 *aSCpnt = SCpnt;
1348 if (!SCpnt)
1349 return (-EBUSY);
1351 (STp->buffer)->read_pointer = 0;
1352 STps->at_sm = 0;
1355 /* Something to check */
1356 if ((STp->buffer)->last_result_fatal) {
1357 retval = 1;
1358 #if DEBUG
1359 if (debugging)
1360 printk(ST_DEB_MSG "st%d: Sense: %2x %2x %2x %2x %2x %2x %2x %2x\n",
1361 dev,
1362 SCpnt->sense_buffer[0], SCpnt->sense_buffer[1],
1363 SCpnt->sense_buffer[2], SCpnt->sense_buffer[3],
1364 SCpnt->sense_buffer[4], SCpnt->sense_buffer[5],
1365 SCpnt->sense_buffer[6], SCpnt->sense_buffer[7]);
1366 #endif
1367 if ((SCpnt->sense_buffer[0] & 0x70) == 0x70) { /* extended sense */
1369 if ((SCpnt->sense_buffer[2] & 0x0f) == BLANK_CHECK)
1370 SCpnt->sense_buffer[2] &= 0xcf; /* No need for EOM in this case */
1372 if ((SCpnt->sense_buffer[2] & 0xe0) != 0) { /* EOF, EOM, or ILI */
1373 /* Compute the residual count */
1374 if ((SCpnt->sense_buffer[0] & 0x80) != 0)
1375 transfer = (SCpnt->sense_buffer[3] << 24) |
1376 (SCpnt->sense_buffer[4] << 16) |
1377 (SCpnt->sense_buffer[5] << 8) | SCpnt->sense_buffer[6];
1378 else
1379 transfer = 0;
1380 if (STp->block_size == 0 &&
1381 (SCpnt->sense_buffer[2] & 0x0f) == MEDIUM_ERROR)
1382 transfer = bytes;
1384 if (SCpnt->sense_buffer[2] & 0x20) { /* ILI */
1385 if (STp->block_size == 0) {
1386 if (transfer <= 0)
1387 transfer = 0;
1388 (STp->buffer)->buffer_bytes = bytes - transfer;
1390 else {
1391 scsi_release_command(SCpnt);
1392 SCpnt = *aSCpnt = NULL;
1393 if (transfer == blks) { /* We did not get anything, error */
1394 printk(KERN_NOTICE "st%d: Incorrect block size.\n", dev);
1395 if (STps->drv_block >= 0)
1396 STps->drv_block += blks - transfer + 1;
1397 st_int_ioctl(inode, MTBSR, 1);
1398 return (-EIO);
1400 /* We have some data, deliver it */
1401 (STp->buffer)->buffer_bytes = (blks - transfer) *
1402 STp->block_size;
1403 #if DEBUG
1404 if (debugging)
1405 printk(ST_DEB_MSG
1406 "st%d: ILI but enough data received %ld %d.\n",
1407 dev, count, (STp->buffer)->buffer_bytes);
1408 #endif
1409 if (STps->drv_block >= 0)
1410 STps->drv_block += 1;
1411 if (st_int_ioctl(inode, MTBSR, 1))
1412 return (-EIO);
1415 else if (SCpnt->sense_buffer[2] & 0x80) { /* FM overrides EOM */
1416 if (STps->eof != ST_FM_HIT)
1417 STps->eof = ST_FM_HIT;
1418 else
1419 STps->eof = ST_EOD_2;
1420 if (STp->block_size == 0)
1421 (STp->buffer)->buffer_bytes = 0;
1422 else
1423 (STp->buffer)->buffer_bytes =
1424 bytes - transfer * STp->block_size;
1425 #if DEBUG
1426 if (debugging)
1427 printk(ST_DEB_MSG
1428 "st%d: EOF detected (%d bytes read).\n",
1429 dev, (STp->buffer)->buffer_bytes);
1430 #endif
1432 else if (SCpnt->sense_buffer[2] & 0x40) {
1433 if (STps->eof == ST_FM)
1434 STps->eof = ST_EOD_1;
1435 else
1436 STps->eof = ST_EOM_OK;
1437 if (STp->block_size == 0)
1438 (STp->buffer)->buffer_bytes = bytes - transfer;
1439 else
1440 (STp->buffer)->buffer_bytes =
1441 bytes - transfer * STp->block_size;
1442 #if DEBUG
1443 if (debugging)
1444 printk(ST_DEB_MSG "st%d: EOM detected (%d bytes read).\n",
1445 dev, (STp->buffer)->buffer_bytes);
1446 #endif
1448 } /* end of EOF, EOM, ILI test */
1449 else { /* nonzero sense key */
1450 #if DEBUG
1451 if (debugging)
1452 printk(ST_DEB_MSG "st%d: Tape error while reading.\n", dev);
1453 #endif
1454 STps->drv_block = (-1);
1455 if (STps->eof == ST_FM &&
1456 (SCpnt->sense_buffer[2] & 0x0f) == BLANK_CHECK) {
1457 #if DEBUG
1458 if (debugging)
1459 printk(ST_DEB_MSG
1460 "st%d: Zero returned for first BLANK CHECK after EOF.\n",
1461 dev);
1462 #endif
1463 STps->eof = ST_EOD_2; /* First BLANK_CHECK after FM */
1465 else /* Some other extended sense code */
1466 retval = (-EIO);
1468 } /* End of extended sense test */
1469 else { /* Non-extended sense */
1470 retval = (STp->buffer)->last_result_fatal;
1473 } /* End of error handling */
1474 else /* Read successful */
1475 (STp->buffer)->buffer_bytes = bytes;
1477 if (STps->drv_block >= 0) {
1478 if (STp->block_size == 0)
1479 STps->drv_block++;
1480 else
1481 STps->drv_block += (STp->buffer)->buffer_bytes / STp->block_size;
1484 return retval;
1488 /* Read command */
1489 static ssize_t
1490 st_read(struct file * filp, char * buf, size_t count, loff_t *ppos)
1492 struct inode * inode = filp->f_dentry->d_inode;
1493 ssize_t total;
1494 ssize_t i, transfer;
1495 int special;
1496 Scsi_Cmnd * SCpnt = NULL;
1497 Scsi_Tape * STp;
1498 ST_mode * STm;
1499 ST_partstat * STps;
1500 int dev = TAPE_NR(inode->i_rdev);
1502 STp = &(scsi_tapes[dev]);
1505 * If we are in the middle of error recovery, don't let anyone
1506 * else try and use this device. Also, if error recovery fails, it
1507 * may try and take the device offline, in which case all further
1508 * access to the device is prohibited.
1510 if( !scsi_block_when_processing_errors(STp->device) )
1512 return -ENXIO;
1515 if (ppos != &filp->f_pos) {
1516 /* "A request was outside the capabilities of the device." */
1517 return -ENXIO;
1520 if (STp->ready != ST_READY) {
1521 if (STp->ready == ST_NO_TAPE)
1522 return (-ENOMEDIUM);
1523 else
1524 return (-EIO);
1526 STm = &(STp->modes[STp->current_mode]);
1527 if (!STm->defined)
1528 return (-ENXIO);
1529 #if DEBUG
1530 if (!STp->in_use) {
1531 printk(ST_DEB_MSG "st%d: Incorrect device.\n", dev);
1532 return (-EIO);
1534 #endif
1536 if (STp->can_partitions &&
1537 (total = update_partition(inode)) < 0)
1538 return total;
1540 if (STp->block_size == 0 &&
1541 count > (STp->buffer)->buffer_size &&
1542 !enlarge_buffer(STp->buffer, count, STp->restr_dma))
1543 return (-EOVERFLOW);
1545 if (!(STm->do_read_ahead) && STp->block_size != 0 &&
1546 (count % STp->block_size) != 0)
1547 return (-EIO); /* Read must be integral number of blocks */
1549 if (STp->do_auto_lock && STp->door_locked == ST_UNLOCKED &&
1550 !st_int_ioctl(inode, MTLOCK, 0))
1551 STp->door_locked = ST_LOCKED_AUTO;
1553 STps = &(STp->ps[STp->partition]);
1554 if (STps->rw == ST_WRITING) {
1555 transfer = flush_buffer(inode, filp, 0);
1556 if (transfer)
1557 return transfer;
1558 STps->rw = ST_READING;
1561 #if DEBUG
1562 if (debugging && STps->eof != ST_NOEOF)
1563 printk(ST_DEB_MSG "st%d: EOF/EOM flag up (%d). Bytes %d\n", dev,
1564 STps->eof, (STp->buffer)->buffer_bytes);
1565 #endif
1566 if ((STp->buffer)->buffer_bytes == 0 &&
1567 STps->eof >= ST_EOD_1) {
1568 if (STps->eof < ST_EOD) {
1569 STps->eof += 1;
1570 return 0;
1572 return (-EIO); /* EOM or Blank Check */
1575 /* Check the buffer writability before any tape movement. Don't alter
1576 buffer data. */
1577 if (copy_from_user(&i, buf, 1) != 0 ||
1578 copy_to_user(buf, &i, 1) != 0 ||
1579 copy_from_user(&i, buf + count - 1, 1) != 0 ||
1580 copy_to_user(buf + count - 1, &i, 1) != 0)
1581 return (-EFAULT);
1583 STps->rw = ST_READING;
1586 /* Loop until enough data in buffer or a special condition found */
1587 for (total = 0, special = 0; total < count && !special; ) {
1589 /* Get new data if the buffer is empty */
1590 if ((STp->buffer)->buffer_bytes == 0) {
1591 special = read_tape(inode, count - total, &SCpnt);
1592 if (special < 0) { /* No need to continue read */
1593 if (SCpnt != NULL)
1595 scsi_release_command(SCpnt);
1597 return special;
1601 /* Move the data from driver buffer to user buffer */
1602 if ((STp->buffer)->buffer_bytes > 0) {
1603 #if DEBUG
1604 if (debugging && STps->eof != ST_NOEOF)
1605 printk(ST_DEB_MSG "st%d: EOF up (%d). Left %d, needed %d.\n", dev,
1606 STps->eof, (STp->buffer)->buffer_bytes, count - total);
1607 #endif
1608 transfer = (STp->buffer)->buffer_bytes < count - total ?
1609 (STp->buffer)->buffer_bytes : count - total;
1610 i = copy_to_user(buf, (STp->buffer)->b_data +
1611 (STp->buffer)->read_pointer, transfer);
1612 if (i) {
1613 if (SCpnt != NULL)
1615 scsi_release_command(SCpnt);
1616 SCpnt = NULL;
1618 return (-EFAULT);
1620 filp->f_pos += transfer;
1621 buf += transfer;
1622 total += transfer;
1623 (STp->buffer)->buffer_bytes -= transfer;
1624 (STp->buffer)->read_pointer += transfer;
1627 if (STp->block_size == 0)
1628 break; /* Read only one variable length block */
1630 } /* for (total = 0, special = 0; total < count && !special; ) */
1632 if (SCpnt != NULL)
1634 scsi_release_command(SCpnt);
1635 SCpnt = NULL;
1638 /* Change the eof state if no data from tape or buffer */
1639 if (total == 0) {
1640 if (STps->eof == ST_FM_HIT) {
1641 STps->eof = ST_FM;
1642 STps->drv_block = 0;
1643 if (STps->drv_file >= 0)
1644 STps->drv_file++;
1646 else if (STps->eof == ST_EOD_1) {
1647 STps->eof = ST_EOD_2;
1648 STps->drv_block = 0;
1649 if (STps->drv_file >= 0)
1650 STps->drv_file++;
1652 else if (STps->eof == ST_EOD_2)
1653 STps->eof = ST_EOD;
1655 else if (STps->eof == ST_FM)
1656 STps->eof = ST_NOEOF;
1658 return total;
1663 /* Set the driver options */
1664 static void
1665 st_log_options(Scsi_Tape *STp, ST_mode *STm, int dev)
1667 printk(KERN_INFO
1668 "st%d: Mode %d options: buffer writes: %d, async writes: %d, read ahead: %d\n",
1669 dev, STp->current_mode, STm->do_buffer_writes, STm->do_async_writes,
1670 STm->do_read_ahead);
1671 printk(KERN_INFO
1672 "st%d: can bsr: %d, two FMs: %d, fast mteom: %d, auto lock: %d,\n",
1673 dev, STp->can_bsr, STp->two_fm, STp->fast_mteom, STp->do_auto_lock);
1674 printk(KERN_INFO
1675 "st%d: defs for wr: %d, no block limits: %d, partitions: %d, s2 log: %d\n",
1676 dev, STm->defaults_for_writes, STp->omit_blklims, STp->can_partitions,
1677 STp->scsi2_logical);
1678 printk(KERN_INFO
1679 "st%d: sysv: %d\n", dev, STm->sysv);
1680 #if DEBUG
1681 printk(KERN_INFO
1682 "st%d: debugging: %d\n",
1683 dev, debugging);
1684 #endif
1688 static int
1689 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
1709 code = options & MT_ST_OPTIONS;
1710 if (code == MT_ST_BOOLEANS) {
1711 STm->do_buffer_writes = (options & MT_ST_BUFFER_WRITES) != 0;
1712 STm->do_async_writes = (options & MT_ST_ASYNC_WRITES) != 0;
1713 STm->defaults_for_writes = (options & MT_ST_DEF_WRITES) != 0;
1714 STm->do_read_ahead = (options & MT_ST_READ_AHEAD) != 0;
1715 STp->two_fm = (options & MT_ST_TWO_FM) != 0;
1716 STp->fast_mteom = (options & MT_ST_FAST_MTEOM) != 0;
1717 STp->do_auto_lock = (options & MT_ST_AUTO_LOCK) != 0;
1718 STp->can_bsr = (options & MT_ST_CAN_BSR) != 0;
1719 STp->omit_blklims = (options & MT_ST_NO_BLKLIMS) != 0;
1720 if ((STp->device)->scsi_level >= SCSI_2)
1721 STp->can_partitions = (options & MT_ST_CAN_PARTITIONS) != 0;
1722 STp->scsi2_logical = (options & MT_ST_SCSI2LOGICAL) != 0;
1723 STm->sysv = (options & MT_ST_SYSV) != 0;
1724 #if DEBUG
1725 debugging = (options & MT_ST_DEBUGGING) != 0;
1726 #endif
1727 st_log_options(STp, STm, dev);
1729 else if (code == MT_ST_SETBOOLEANS || code == MT_ST_CLEARBOOLEANS) {
1730 value = (code == MT_ST_SETBOOLEANS);
1731 if ((options & MT_ST_BUFFER_WRITES) != 0)
1732 STm->do_buffer_writes = value;
1733 if ((options & MT_ST_ASYNC_WRITES) != 0)
1734 STm->do_async_writes = value;
1735 if ((options & MT_ST_DEF_WRITES) != 0)
1736 STm->defaults_for_writes = value;
1737 if ((options & MT_ST_READ_AHEAD) != 0)
1738 STm->do_read_ahead = value;
1739 if ((options & MT_ST_TWO_FM) != 0)
1740 STp->two_fm = value;
1741 if ((options & MT_ST_FAST_MTEOM) != 0)
1742 STp->fast_mteom = value;
1743 if ((options & MT_ST_AUTO_LOCK) != 0)
1744 STp->do_auto_lock = value;
1745 if ((options & MT_ST_CAN_BSR) != 0)
1746 STp->can_bsr = value;
1747 if ((options & MT_ST_NO_BLKLIMS) != 0)
1748 STp->omit_blklims = value;
1749 if ((STp->device)->scsi_level >= SCSI_2 &&
1750 (options & MT_ST_CAN_PARTITIONS) != 0)
1751 STp->can_partitions = value;
1752 if ((options & MT_ST_SCSI2LOGICAL) != 0)
1753 STp->scsi2_logical = value;
1754 if ((options & MT_ST_SYSV) != 0)
1755 STm->sysv = value;
1756 #if DEBUG
1757 if ((options & MT_ST_DEBUGGING) != 0)
1758 debugging = value;
1759 #endif
1760 st_log_options(STp, STm, dev);
1762 else if (code == MT_ST_WRITE_THRESHOLD) {
1763 value = (options & ~MT_ST_OPTIONS) * ST_KILOBYTE;
1764 if (value < 1 || value > st_buffer_size) {
1765 printk(KERN_WARNING "st%d: Write threshold %d too small or too large.\n",
1766 dev, value);
1767 return (-EIO);
1769 STp->write_threshold = value;
1770 printk(KERN_INFO "st%d: Write threshold set to %d bytes.\n",
1771 dev, value);
1773 else if (code == MT_ST_DEF_BLKSIZE) {
1774 value = (options & ~MT_ST_OPTIONS);
1775 if (value == ~MT_ST_OPTIONS) {
1776 STm->default_blksize = (-1);
1777 printk(KERN_INFO "st%d: Default block size disabled.\n", dev);
1779 else {
1780 STm->default_blksize = value;
1781 printk(KERN_INFO "st%d: Default block size set to %d bytes.\n",
1782 dev, STm->default_blksize);
1785 else if (code == MT_ST_TIMEOUTS) {
1786 value = (options & ~MT_ST_OPTIONS);
1787 if ((value & MT_ST_SET_LONG_TIMEOUT) != 0) {
1788 STp->long_timeout = (value & ~MT_ST_SET_LONG_TIMEOUT) * HZ;
1789 printk(KERN_INFO "st%d: Long timeout set to %d seconds.\n", dev,
1790 (value & ~MT_ST_SET_LONG_TIMEOUT));
1792 else {
1793 STp->timeout = value * HZ;
1794 printk(KERN_INFO "st%d: Normal timeout set to %d seconds.\n", dev,
1795 value);
1798 else if (code == MT_ST_DEF_OPTIONS) {
1799 code = (options & ~MT_ST_CLEAR_DEFAULT);
1800 value = (options & MT_ST_CLEAR_DEFAULT);
1801 if (code == MT_ST_DEF_DENSITY) {
1802 if (value == MT_ST_CLEAR_DEFAULT) {
1803 STm->default_density = (-1);
1804 printk(KERN_INFO "st%d: Density default disabled.\n", dev);
1806 else {
1807 STm->default_density = value & 0xff;
1808 printk(KERN_INFO "st%d: Density default set to %x\n",
1809 dev, STm->default_density);
1812 else if (code == MT_ST_DEF_DRVBUFFER) {
1813 if (value == MT_ST_CLEAR_DEFAULT) {
1814 STp->default_drvbuffer = 0xff;
1815 printk(KERN_INFO "st%d: Drive buffer default disabled.\n", dev);
1817 else {
1818 STp->default_drvbuffer = value & 7;
1819 printk(KERN_INFO "st%d: Drive buffer default set to %x\n",
1820 dev, STp->default_drvbuffer);
1823 else if (code == MT_ST_DEF_COMPRESSION) {
1824 if (value == MT_ST_CLEAR_DEFAULT) {
1825 STm->default_compression = ST_DONT_TOUCH;
1826 printk(KERN_INFO "st%d: Compression default disabled.\n", dev);
1828 else {
1829 STm->default_compression = (value & 1 ? ST_YES : ST_NO);
1830 printk(KERN_INFO "st%d: Compression default set to %x\n",
1831 dev, (value & 1));
1835 else
1836 return (-EIO);
1838 return 0;
1842 #define COMPRESSION_PAGE 0x0f
1843 #define COMPRESSION_PAGE_LENGTH 16
1845 #define MODE_HEADER_LENGTH 4
1847 #define DCE_MASK 0x80
1848 #define DCC_MASK 0x40
1849 #define RED_MASK 0x60
1852 /* Control the compression with mode page 15. Algorithm not changed if zero. */
1853 static int
1854 st_compression(Scsi_Tape * STp, int state)
1856 int dev;
1857 unsigned char cmd[10];
1858 Scsi_Cmnd * SCpnt = NULL;
1860 if (STp->ready != ST_READY)
1861 return (-EIO);
1863 /* Read the current page contents */
1864 memset(cmd, 0, 10);
1865 cmd[0] = MODE_SENSE;
1866 cmd[1] = 8;
1867 cmd[2] = COMPRESSION_PAGE;
1868 cmd[4] = COMPRESSION_PAGE_LENGTH + MODE_HEADER_LENGTH;
1870 SCpnt = st_do_scsi(SCpnt, STp, cmd, cmd[4], STp->timeout, 0);
1871 if (SCpnt == NULL)
1872 return (-EBUSY);
1873 dev = TAPE_NR(SCpnt->request.rq_dev);
1875 if ((STp->buffer)->last_result_fatal != 0) {
1876 #if DEBUG
1877 if (debugging)
1878 printk(ST_DEB_MSG "st%d: Compression mode page not supported.\n", dev);
1879 #endif
1880 scsi_release_command(SCpnt);
1881 SCpnt = NULL;
1882 return (-EIO);
1884 #if DEBUG
1885 if (debugging)
1886 printk(ST_DEB_MSG "st%d: Compression state is %d.\n", dev,
1887 ((STp->buffer)->b_data[MODE_HEADER_LENGTH + 2] & DCE_MASK ? 1 : 0));
1888 #endif
1890 /* Check if compression can be changed */
1891 if (((STp->buffer)->b_data[MODE_HEADER_LENGTH + 2] & DCC_MASK) == 0) {
1892 #if DEBUG
1893 if (debugging)
1894 printk(ST_DEB_MSG "st%d: Compression not supported.\n", dev);
1895 #endif
1896 scsi_release_command(SCpnt);
1897 SCpnt = NULL;
1898 return (-EIO);
1901 /* Do the change */
1902 if (state)
1903 (STp->buffer)->b_data[MODE_HEADER_LENGTH + 2] |= DCE_MASK;
1904 else
1905 (STp->buffer)->b_data[MODE_HEADER_LENGTH + 2] &= ~DCE_MASK;
1907 memset(cmd, 0, 10);
1908 cmd[0] = MODE_SELECT;
1909 cmd[1] = 0x10;
1910 cmd[4] = COMPRESSION_PAGE_LENGTH + MODE_HEADER_LENGTH;
1912 (STp->buffer)->b_data[0] = 0; /* Reserved data length */
1913 (STp->buffer)->b_data[1] = 0; /* Reserved media type byte */
1914 (STp->buffer)->b_data[MODE_HEADER_LENGTH] &= 0x3f;
1915 SCpnt = st_do_scsi(SCpnt, STp, cmd, cmd[4], STp->timeout, 0);
1917 if ((STp->buffer)->last_result_fatal != 0) {
1918 #if DEBUG
1919 if (debugging)
1920 printk(ST_DEB_MSG "st%d: Compression change failed.\n", dev);
1921 #endif
1922 scsi_release_command(SCpnt);
1923 SCpnt = NULL;
1924 return (-EIO);
1927 #if DEBUG
1928 if (debugging)
1929 printk(ST_DEB_MSG "st%d: Compression state changed to %d.\n",
1930 dev, state);
1931 #endif
1933 scsi_release_command(SCpnt);
1934 SCpnt = NULL;
1935 STp->compression_changed = TRUE;
1936 return 0;
1940 /* Internal ioctl function */
1941 static int
1942 st_int_ioctl(struct inode * inode,
1943 unsigned int cmd_in, unsigned long arg)
1945 int timeout;
1946 long ltmp;
1947 int i, ioctl_result;
1948 int chg_eof = TRUE;
1949 unsigned char cmd[10];
1950 Scsi_Cmnd * SCpnt;
1951 Scsi_Tape * STp;
1952 ST_partstat * STps;
1953 int fileno, blkno, at_sm, undone, datalen;
1954 int dev = TAPE_NR(inode->i_rdev);
1956 STp = &(scsi_tapes[dev]);
1957 if (STp->ready != ST_READY && cmd_in != MTLOAD) {
1958 if (STp->ready == ST_NO_TAPE)
1959 return (-ENOMEDIUM);
1960 else
1961 return (-EIO);
1963 timeout = STp->long_timeout;
1964 STps = &(STp->ps[STp->partition]);
1965 fileno = STps->drv_file;
1966 blkno = STps->drv_block;
1967 at_sm = STps->at_sm;
1969 memset(cmd, 0, 10);
1970 datalen = 0;
1971 switch (cmd_in) {
1972 case MTFSFM:
1973 chg_eof = FALSE; /* Changed from the FSF after this */
1974 case MTFSF:
1975 cmd[0] = SPACE;
1976 cmd[1] = 0x01; /* Space FileMarks */
1977 cmd[2] = (arg >> 16);
1978 cmd[3] = (arg >> 8);
1979 cmd[4] = arg;
1980 #if DEBUG
1981 if (debugging)
1982 printk(ST_DEB_MSG "st%d: Spacing tape forward over %d filemarks.\n",
1983 dev, cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
1984 #endif
1985 if (fileno >= 0)
1986 fileno += arg;
1987 blkno = 0;
1988 at_sm &= (arg == 0);
1989 break;
1990 case MTBSFM:
1991 chg_eof = FALSE; /* Changed from the FSF after this */
1992 case MTBSF:
1993 cmd[0] = SPACE;
1994 cmd[1] = 0x01; /* Space FileMarks */
1995 ltmp = (-arg);
1996 cmd[2] = (ltmp >> 16);
1997 cmd[3] = (ltmp >> 8);
1998 cmd[4] = ltmp;
1999 #if DEBUG
2000 if (debugging) {
2001 if (cmd[2] & 0x80)
2002 ltmp = 0xff000000;
2003 ltmp = ltmp | (cmd[2] << 16) | (cmd[3] << 8) | cmd[4];
2004 printk(ST_DEB_MSG "st%d: Spacing tape backward over %ld filemarks.\n",
2005 dev, (-ltmp));
2007 #endif
2008 if (fileno >= 0)
2009 fileno -= arg;
2010 blkno = (-1); /* We can't know the block number */
2011 at_sm &= (arg == 0);
2012 break;
2013 case MTFSR:
2014 cmd[0] = SPACE;
2015 cmd[1] = 0x00; /* Space Blocks */
2016 cmd[2] = (arg >> 16);
2017 cmd[3] = (arg >> 8);
2018 cmd[4] = arg;
2019 #if DEBUG
2020 if (debugging)
2021 printk(ST_DEB_MSG "st%d: Spacing tape forward %d blocks.\n", dev,
2022 cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
2023 #endif
2024 if (blkno >= 0)
2025 blkno += arg;
2026 at_sm &= (arg == 0);
2027 break;
2028 case MTBSR:
2029 cmd[0] = SPACE;
2030 cmd[1] = 0x00; /* Space Blocks */
2031 ltmp = (-arg);
2032 cmd[2] = (ltmp >> 16);
2033 cmd[3] = (ltmp >> 8);
2034 cmd[4] = ltmp;
2035 #if DEBUG
2036 if (debugging) {
2037 if (cmd[2] & 0x80)
2038 ltmp = 0xff000000;
2039 ltmp = ltmp | (cmd[2] << 16) | (cmd[3] << 8) | cmd[4];
2040 printk(ST_DEB_MSG "st%d: Spacing tape backward %ld blocks.\n", dev, (-ltmp));
2042 #endif
2043 if (blkno >= 0)
2044 blkno -= arg;
2045 at_sm &= (arg == 0);
2046 break;
2047 case MTFSS:
2048 cmd[0] = SPACE;
2049 cmd[1] = 0x04; /* Space Setmarks */
2050 cmd[2] = (arg >> 16);
2051 cmd[3] = (arg >> 8);
2052 cmd[4] = arg;
2053 #if DEBUG
2054 if (debugging)
2055 printk(ST_DEB_MSG "st%d: Spacing tape forward %d setmarks.\n", dev,
2056 cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
2057 #endif
2058 if (arg != 0) {
2059 blkno = fileno = (-1);
2060 at_sm = 1;
2062 break;
2063 case MTBSS:
2064 cmd[0] = SPACE;
2065 cmd[1] = 0x04; /* Space Setmarks */
2066 ltmp = (-arg);
2067 cmd[2] = (ltmp >> 16);
2068 cmd[3] = (ltmp >> 8);
2069 cmd[4] = ltmp;
2070 #if DEBUG
2071 if (debugging) {
2072 if (cmd[2] & 0x80)
2073 ltmp = 0xff000000;
2074 ltmp = ltmp | (cmd[2] << 16) | (cmd[3] << 8) | cmd[4];
2075 printk(ST_DEB_MSG "st%d: Spacing tape backward %ld setmarks.\n",
2076 dev, (-ltmp));
2078 #endif
2079 if (arg != 0) {
2080 blkno = fileno = (-1);
2081 at_sm = 1;
2083 break;
2084 case MTWEOF:
2085 case MTWSM:
2086 if (STp->write_prot)
2087 return (-EACCES);
2088 cmd[0] = WRITE_FILEMARKS;
2089 if (cmd_in == MTWSM)
2090 cmd[1] = 2;
2091 cmd[2] = (arg >> 16);
2092 cmd[3] = (arg >> 8);
2093 cmd[4] = arg;
2094 timeout = STp->timeout;
2095 #if DEBUG
2096 if (debugging) {
2097 if (cmd_in == MTWEOF)
2098 printk(ST_DEB_MSG "st%d: Writing %d filemarks.\n", dev,
2099 cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
2100 else
2101 printk(ST_DEB_MSG "st%d: Writing %d setmarks.\n", dev,
2102 cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
2104 #endif
2105 if (fileno >= 0)
2106 fileno += arg;
2107 blkno = 0;
2108 at_sm = (cmd_in == MTWSM);
2109 break;
2110 case MTREW:
2111 cmd[0] = REZERO_UNIT;
2112 #if ST_NOWAIT
2113 cmd[1] = 1; /* Don't wait for completion */
2114 timeout = STp->timeout;
2115 #endif
2116 #if DEBUG
2117 if (debugging)
2118 printk(ST_DEB_MSG "st%d: Rewinding tape.\n", dev);
2119 #endif
2120 fileno = blkno = at_sm = 0 ;
2121 break;
2122 case MTOFFL:
2123 case MTLOAD:
2124 case MTUNLOAD:
2125 cmd[0] = START_STOP;
2126 if (cmd_in == MTLOAD)
2127 cmd[4] |= 1;
2129 * If arg >= 1 && arg <= 6 Enhanced load/unload in HP C1553A
2131 if (cmd_in != MTOFFL &&
2132 arg >= 1 + MT_ST_HPLOADER_OFFSET
2133 && arg <= 6 + MT_ST_HPLOADER_OFFSET) {
2134 #if DEBUG
2135 if (debugging) {
2136 printk(ST_DEB_MSG "st%d: Enhanced %sload slot %2ld.\n",
2137 dev, (cmd[4]) ? "" : "un",
2138 arg - MT_ST_HPLOADER_OFFSET);
2140 #endif
2141 cmd[3] = arg - MT_ST_HPLOADER_OFFSET; /* MediaID field of C1553A */
2143 #if ST_NOWAIT
2144 cmd[1] = 1; /* Don't wait for completion */
2145 timeout = STp->timeout;
2146 #else
2147 timeout = STp->long_timeout;
2148 #endif
2149 #if DEBUG
2150 if (debugging) {
2151 if (cmd_in != MTLOAD)
2152 printk(ST_DEB_MSG "st%d: Unloading tape.\n", dev);
2153 else
2154 printk(ST_DEB_MSG "st%d: Loading tape.\n", dev);
2156 #endif
2157 fileno = blkno = at_sm = 0 ;
2158 break;
2159 case MTNOP:
2160 #if DEBUG
2161 if (debugging)
2162 printk(ST_DEB_MSG "st%d: No op on tape.\n", dev);
2163 #endif
2164 return 0; /* Should do something ? */
2165 break;
2166 case MTRETEN:
2167 cmd[0] = START_STOP;
2168 #if ST_NOWAIT
2169 cmd[1] = 1; /* Don't wait for completion */
2170 timeout = STp->timeout;
2171 #endif
2172 cmd[4] = 3;
2173 #if DEBUG
2174 if (debugging)
2175 printk(ST_DEB_MSG "st%d: Retensioning tape.\n", dev);
2176 #endif
2177 fileno = blkno = at_sm = 0;
2178 break;
2179 case MTEOM:
2180 if (!STp->fast_mteom) {
2181 /* space to the end of tape */
2182 ioctl_result = st_int_ioctl(inode, MTFSF, 0x3fff);
2183 fileno = STps->drv_file;
2184 if (STps->eof >= ST_EOD_1)
2185 return 0;
2186 /* The next lines would hide the number of spaced FileMarks
2187 That's why I inserted the previous lines. I had no luck
2188 with detecting EOM with FSF, so we go now to EOM.
2189 Joerg Weule */
2191 else
2192 fileno = (-1);
2193 cmd[0] = SPACE;
2194 cmd[1] = 3;
2195 #if DEBUG
2196 if (debugging)
2197 printk(ST_DEB_MSG "st%d: Spacing to end of recorded medium.\n", dev);
2198 #endif
2199 blkno = 0;
2200 at_sm = 0;
2201 break;
2202 case MTERASE:
2203 if (STp->write_prot)
2204 return (-EACCES);
2205 cmd[0] = ERASE;
2206 cmd[1] = 1; /* To the end of tape */
2207 #if ST_NOWAIT
2208 cmd[1] |= 2; /* Don't wait for completion */
2209 timeout = STp->timeout;
2210 #else
2211 timeout = STp->long_timeout * 8;
2212 #endif
2213 #if DEBUG
2214 if (debugging)
2215 printk(ST_DEB_MSG "st%d: Erasing tape.\n", dev);
2216 #endif
2217 fileno = blkno = at_sm = 0 ;
2218 break;
2219 case MTLOCK:
2220 chg_eof = FALSE;
2221 cmd[0] = ALLOW_MEDIUM_REMOVAL;
2222 cmd[4] = SCSI_REMOVAL_PREVENT;
2223 #if DEBUG
2224 if (debugging)
2225 printk(ST_DEB_MSG "st%d: Locking drive door.\n", dev);
2226 #endif;
2227 break;
2228 case MTUNLOCK:
2229 chg_eof = FALSE;
2230 cmd[0] = ALLOW_MEDIUM_REMOVAL;
2231 cmd[4] = SCSI_REMOVAL_ALLOW;
2232 #if DEBUG
2233 if (debugging)
2234 printk(ST_DEB_MSG "st%d: Unlocking drive door.\n", dev);
2235 #endif;
2236 break;
2237 case MTSETBLK: /* Set block length */
2238 case MTSETDENSITY: /* Set tape density */
2239 case MTSETDRVBUFFER: /* Set drive buffering */
2240 case SET_DENS_AND_BLK: /* Set density and block size */
2241 chg_eof = FALSE;
2242 if (STp->dirty || (STp->buffer)->buffer_bytes != 0)
2243 return (-EIO); /* Not allowed if data in buffer */
2244 if ((cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) &&
2245 (arg & MT_ST_BLKSIZE_MASK) != 0 &&
2246 ((arg & MT_ST_BLKSIZE_MASK) < STp->min_block ||
2247 (arg & MT_ST_BLKSIZE_MASK) > STp->max_block ||
2248 (arg & MT_ST_BLKSIZE_MASK) > st_buffer_size)) {
2249 printk(KERN_WARNING "st%d: Illegal block size.\n", dev);
2250 return (-EINVAL);
2252 cmd[0] = MODE_SELECT;
2253 cmd[4] = datalen = 12;
2255 memset((STp->buffer)->b_data, 0, 12);
2256 if (cmd_in == MTSETDRVBUFFER)
2257 (STp->buffer)->b_data[2] = (arg & 7) << 4;
2258 else
2259 (STp->buffer)->b_data[2] =
2260 STp->drv_buffer << 4;
2261 (STp->buffer)->b_data[3] = 8; /* block descriptor length */
2262 if (cmd_in == MTSETDENSITY) {
2263 (STp->buffer)->b_data[4] = arg;
2264 STp->density_changed = TRUE; /* At least we tried ;-) */
2266 else if (cmd_in == SET_DENS_AND_BLK)
2267 (STp->buffer)->b_data[4] = arg >> 24;
2268 else
2269 (STp->buffer)->b_data[4] = STp->density;
2270 if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) {
2271 ltmp = arg & MT_ST_BLKSIZE_MASK;
2272 if (cmd_in == MTSETBLK)
2273 STp->blksize_changed = TRUE; /* At least we tried ;-) */
2275 else
2276 ltmp = STp->block_size;
2277 (STp->buffer)->b_data[9] = (ltmp >> 16);
2278 (STp->buffer)->b_data[10] = (ltmp >> 8);
2279 (STp->buffer)->b_data[11] = ltmp;
2280 timeout = STp->timeout;
2281 #if DEBUG
2282 if (debugging) {
2283 if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK)
2284 printk(ST_DEB_MSG "st%d: Setting block size to %d bytes.\n", dev,
2285 (STp->buffer)->b_data[9] * 65536 +
2286 (STp->buffer)->b_data[10] * 256 +
2287 (STp->buffer)->b_data[11]);
2288 if (cmd_in == MTSETDENSITY || cmd_in == SET_DENS_AND_BLK)
2289 printk(ST_DEB_MSG "st%d: Setting density code to %x.\n", dev,
2290 (STp->buffer)->b_data[4]);
2291 if (cmd_in == MTSETDRVBUFFER)
2292 printk(ST_DEB_MSG "st%d: Setting drive buffer code to %d.\n", dev,
2293 ((STp->buffer)->b_data[2] >> 4) & 7);
2295 #endif
2296 break;
2297 default:
2298 return (-ENOSYS);
2301 SCpnt = st_do_scsi(NULL, STp, cmd, datalen, timeout, MAX_RETRIES);
2302 if (!SCpnt)
2303 return (-EBUSY);
2305 ioctl_result = (STp->buffer)->last_result_fatal;
2307 if (!ioctl_result) { /* SCSI command successful */
2308 scsi_release_command(SCpnt);
2309 SCpnt = NULL;
2310 STps->drv_block = blkno;
2311 STps->drv_file = fileno;
2312 STps->at_sm = at_sm;
2314 if (cmd_in == MTLOCK)
2315 STp->door_locked = ST_LOCKED_EXPLICIT;
2316 else if (cmd_in == MTUNLOCK)
2317 STp->door_locked = ST_UNLOCKED;
2319 if (cmd_in == MTBSFM)
2320 ioctl_result = st_int_ioctl(inode, MTFSF, 1);
2321 else if (cmd_in == MTFSFM)
2322 ioctl_result = st_int_ioctl(inode, MTBSF, 1);
2324 if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) {
2325 STp->block_size = arg & MT_ST_BLKSIZE_MASK;
2326 if (STp->block_size != 0)
2327 (STp->buffer)->buffer_blocks =
2328 (STp->buffer)->buffer_size / STp->block_size;
2329 (STp->buffer)->buffer_bytes = (STp->buffer)->read_pointer = 0;
2330 if (cmd_in == SET_DENS_AND_BLK)
2331 STp->density = arg >> MT_ST_DENSITY_SHIFT;
2333 else if (cmd_in == MTSETDRVBUFFER)
2334 STp->drv_buffer = (arg & 7);
2335 else if (cmd_in == MTSETDENSITY)
2336 STp->density = arg;
2338 if (cmd_in == MTEOM)
2339 STps->eof = ST_EOD;
2340 else if (cmd_in == MTFSF)
2341 STps->eof = ST_FM;
2342 else if (chg_eof)
2343 STps->eof = ST_NOEOF;
2346 if (cmd_in == MTOFFL || cmd_in == MTUNLOAD)
2347 STp->rew_at_close = 0;
2348 else if (cmd_in == MTLOAD) {
2349 STp->rew_at_close = (MINOR(inode->i_rdev) & 0x80) == 0;
2350 for (i=0; i < ST_NBR_PARTITIONS; i++) {
2351 STp->ps[i].rw = ST_IDLE;
2352 STp->ps[i].last_block_valid = FALSE;
2354 STp->partition = 0;
2357 } else { /* SCSI command was not completely successful. Don't return
2358 from this block without releasing the SCSI command block! */
2360 if (SCpnt->sense_buffer[2] & 0x40) {
2361 if (cmd_in != MTBSF && cmd_in != MTBSFM &&
2362 cmd_in != MTBSR && cmd_in != MTBSS)
2363 STps->eof = ST_EOM_OK;
2364 STps->drv_block = 0;
2367 undone = (
2368 (SCpnt->sense_buffer[3] << 24) +
2369 (SCpnt->sense_buffer[4] << 16) +
2370 (SCpnt->sense_buffer[5] << 8) +
2371 SCpnt->sense_buffer[6] );
2372 if (cmd_in == MTWEOF &&
2373 (SCpnt->sense_buffer[0] & 0x70) == 0x70 &&
2374 (SCpnt->sense_buffer[2] & 0x4f) == 0x40 &&
2375 ((SCpnt->sense_buffer[0] & 0x80) == 0 || undone == 0)) {
2376 ioctl_result = 0; /* EOF written succesfully at EOM */
2377 if (fileno >= 0)
2378 fileno++;
2379 STps->drv_file = fileno;
2380 STps->eof = ST_NOEOF;
2382 else if ( (cmd_in == MTFSF) || (cmd_in == MTFSFM) ) {
2383 if (fileno >= 0)
2384 STps->drv_file = fileno - undone ;
2385 else
2386 STps->drv_file = fileno;
2387 STps->drv_block = 0;
2388 STps->eof = ST_NOEOF;
2390 else if ( (cmd_in == MTBSF) || (cmd_in == MTBSFM) ) {
2391 if (fileno >= 0)
2392 STps->drv_file = fileno + undone ;
2393 else
2394 STps->drv_file = fileno;
2395 STps->drv_block = 0;
2396 STps->eof = ST_NOEOF;
2398 else if (cmd_in == MTFSR) {
2399 if (SCpnt->sense_buffer[2] & 0x80) { /* Hit filemark */
2400 if (STps->drv_file >= 0)
2401 STps->drv_file++;
2402 STps->drv_block = 0;
2403 STps->eof = ST_FM;
2405 else {
2406 if (blkno >= undone)
2407 STps->drv_block = blkno - undone;
2408 else
2409 STps->drv_block = (-1);
2410 STps->eof = ST_NOEOF;
2413 else if (cmd_in == MTBSR) {
2414 if (SCpnt->sense_buffer[2] & 0x80) { /* Hit filemark */
2415 STps->drv_file--;
2416 STps->drv_block = (-1);
2418 else {
2419 if (blkno >= 0)
2420 STps->drv_block = blkno + undone;
2421 else
2422 STps->drv_block = (-1);
2424 STps->eof = ST_NOEOF;
2426 else if (cmd_in == MTEOM) {
2427 STps->drv_file = (-1);
2428 STps->drv_block = (-1);
2429 STps->eof = ST_EOD;
2431 else if (chg_eof)
2432 STps->eof = ST_NOEOF;
2434 if ((SCpnt->sense_buffer[2] & 0x0f) == BLANK_CHECK)
2435 STps->eof = ST_EOD;
2437 if (cmd_in == MTLOCK)
2438 STp->door_locked = ST_LOCK_FAILS;
2440 scsi_release_command(SCpnt);
2441 SCpnt = NULL;
2444 return ioctl_result;
2448 \f/* Get the tape position. If bt == 2, arg points into a kernel space mt_loc
2449 structure. */
2451 static int
2452 get_location(struct inode * inode, unsigned int *block, int *partition,
2453 int logical)
2455 Scsi_Tape *STp;
2456 int dev = TAPE_NR(inode->i_rdev);
2457 int result;
2458 unsigned char scmd[10];
2459 Scsi_Cmnd *SCpnt;
2461 STp = &(scsi_tapes[dev]);
2462 if (STp->ready != ST_READY)
2463 return (-EIO);
2465 memset (scmd, 0, 10);
2466 if ((STp->device)->scsi_level < SCSI_2) {
2467 scmd[0] = QFA_REQUEST_BLOCK;
2468 scmd[4] = 3;
2470 else {
2471 scmd[0] = READ_POSITION;
2472 if (!logical && !STp->scsi2_logical)
2473 scmd[1] = 1;
2475 SCpnt = st_do_scsi(NULL, STp, scmd, 20, STp->timeout, MAX_READY_RETRIES);
2476 if (!SCpnt)
2477 return (-EBUSY);
2479 if ((STp->buffer)->last_result_fatal != 0 ||
2480 (STp->device->scsi_level >= SCSI_2 &&
2481 ((STp->buffer)->b_data[0] & 4) != 0)) {
2482 *block = *partition = 0;
2483 #if DEBUG
2484 if (debugging)
2485 printk(ST_DEB_MSG "st%d: Can't read tape position.\n", dev);
2486 #endif
2487 result = (-EIO);
2489 else {
2490 result = 0;
2491 if ((STp->device)->scsi_level < SCSI_2) {
2492 *block = ((STp->buffer)->b_data[0] << 16)
2493 + ((STp->buffer)->b_data[1] << 8)
2494 + (STp->buffer)->b_data[2];
2495 *partition = 0;
2497 else {
2498 *block = ((STp->buffer)->b_data[4] << 24)
2499 + ((STp->buffer)->b_data[5] << 16)
2500 + ((STp->buffer)->b_data[6] << 8)
2501 + (STp->buffer)->b_data[7];
2502 *partition = (STp->buffer)->b_data[1];
2503 if (((STp->buffer)->b_data[0] & 0x80) &&
2504 (STp->buffer)->b_data[1] == 0) /* BOP of partition 0 */
2505 STp->ps[0].drv_block = STp->ps[0].drv_file = 0;
2507 #if DEBUG
2508 if (debugging)
2509 printk(ST_DEB_MSG "st%d: Got tape pos. blk %d part %d.\n", dev,
2510 *block, *partition);
2511 #endif
2514 scsi_release_command(SCpnt);
2515 SCpnt = NULL;
2517 return result;
2521 /* Set the tape block and partition. Negative partition means that only the
2522 block should be set in vendor specific way. */
2523 static int
2524 set_location(struct inode * inode, unsigned int block, int partition,
2525 int logical)
2527 Scsi_Tape *STp;
2528 ST_partstat *STps;
2529 int dev = TAPE_NR(inode->i_rdev);
2530 int result, p;
2531 unsigned int blk;
2532 int timeout;
2533 unsigned char scmd[10];
2534 Scsi_Cmnd *SCpnt;
2536 STp = &(scsi_tapes[dev]);
2537 if (STp->ready != ST_READY)
2538 return (-EIO);
2539 timeout = STp->long_timeout;
2540 STps = &(STp->ps[STp->partition]);
2542 #if DEBUG
2543 if (debugging)
2544 printk(ST_DEB_MSG "st%d: Setting block to %d and partition to %d.\n",
2545 dev, block, partition);
2546 if (partition < 0)
2547 return (-EIO);
2548 #endif
2550 /* Update the location at the partition we are leaving */
2551 if ((!STp->can_partitions && partition != 0) ||
2552 partition >= ST_NBR_PARTITIONS)
2553 return (-EINVAL);
2554 if (partition != STp->partition) {
2555 if (get_location(inode, &blk, &p, 1))
2556 STps->last_block_valid = FALSE;
2557 else {
2558 STps->last_block_valid = TRUE;
2559 STps->last_block_visited = blk;
2560 #if DEBUG
2561 if (debugging)
2562 printk(ST_DEB_MSG "st%d: Visited block %d for partition %d saved.\n",
2563 dev, blk, STp->partition);
2564 #endif
2568 memset (scmd, 0, 10);
2569 if ((STp->device)->scsi_level < SCSI_2) {
2570 scmd[0] = QFA_SEEK_BLOCK;
2571 scmd[2] = (block >> 16);
2572 scmd[3] = (block >> 8);
2573 scmd[4] = block;
2574 scmd[5] = 0;
2576 else {
2577 scmd[0] = SEEK_10;
2578 scmd[3] = (block >> 24);
2579 scmd[4] = (block >> 16);
2580 scmd[5] = (block >> 8);
2581 scmd[6] = block;
2582 if (!logical && !STp->scsi2_logical)
2583 scmd[1] = 4;
2584 if (STp->partition != partition) {
2585 scmd[1] |= 2;
2586 scmd[8] = partition;
2587 #if DEBUG
2588 if (debugging)
2589 printk(ST_DEB_MSG "st%d: Trying to change partition from %d to %d\n",
2590 dev, STp->partition, partition);
2591 #endif
2594 #if ST_NOWAIT
2595 scmd[1] |= 1; /* Don't wait for completion */
2596 timeout = STp->timeout;
2597 #endif
2599 SCpnt = st_do_scsi(NULL, STp, scmd, 20, timeout, MAX_READY_RETRIES);
2600 if (!SCpnt)
2601 return (-EBUSY);
2603 STps->drv_block = STps->drv_file = (-1);
2604 STps->eof = ST_NOEOF;
2605 if ((STp->buffer)->last_result_fatal != 0) {
2606 result = (-EIO);
2607 if (STp->can_partitions &&
2608 (STp->device)->scsi_level >= SCSI_2 &&
2609 (p = find_partition(inode)) >= 0)
2610 STp->partition = p;
2612 else {
2613 if (STp->can_partitions) {
2614 STp->partition = partition;
2615 STps = &(STp->ps[partition]);
2616 if (!STps->last_block_valid ||
2617 STps->last_block_visited != block) {
2618 STps->at_sm = 0;
2619 STps->rw = ST_IDLE;
2622 else
2623 STps->at_sm = 0;
2624 if (block == 0)
2625 STps->drv_block = STps->drv_file = 0;
2626 result = 0;
2629 scsi_release_command(SCpnt);
2630 SCpnt = NULL;
2632 return result;
2636 /* Find the current partition number for the drive status. Called from open and
2637 returns either partition number of negative error code. */
2638 static int
2639 find_partition(struct inode *inode)
2641 int i, partition;
2642 unsigned int block;
2644 if ((i = get_location(inode, &block, &partition, 1)) < 0)
2645 return i;
2646 if (partition >= ST_NBR_PARTITIONS)
2647 return (-EIO);
2648 return partition;
2652 /* Change the partition if necessary */
2653 static int
2654 update_partition(struct inode * inode)
2656 int dev = TAPE_NR(inode->i_rdev);
2657 Scsi_Tape *STp;
2658 ST_partstat *STps;
2660 STp = &(scsi_tapes[dev]);
2661 if (STp->partition == STp->new_partition)
2662 return 0;
2663 STps = &(STp->ps[STp->new_partition]);
2664 if (!STps->last_block_valid)
2665 STps->last_block_visited = 0;
2666 return set_location(inode, STps->last_block_visited, STp->new_partition, 1);
2669 \f/* Functions for reading and writing the medium partition mode page. These
2670 seem to work with Wangtek 6200HS and HP C1533A. */
2672 #define PART_PAGE 0x11
2673 #define PART_PAGE_LENGTH 10
2675 /* Get the number of partitions on the tape. As a side effect reads the
2676 mode page into the tape buffer. */
2677 static int
2678 nbr_partitions(struct inode * inode)
2680 int dev = TAPE_NR(inode->i_rdev), result;
2681 Scsi_Tape *STp;
2682 Scsi_Cmnd * SCpnt = NULL;
2683 unsigned char cmd[10];
2685 STp = &(scsi_tapes[dev]);
2686 if (STp->ready != ST_READY)
2687 return (-EIO);
2689 memset ((void *) &cmd[0], 0, 10);
2690 cmd[0] = MODE_SENSE;
2691 cmd[1] = 8; /* Page format */
2692 cmd[2] = PART_PAGE;
2693 cmd[4] = 200;
2695 SCpnt = st_do_scsi(SCpnt, STp, cmd, 200, STp->timeout, MAX_READY_RETRIES);
2696 if (SCpnt == NULL)
2697 return (-EBUSY);
2698 scsi_release_command(SCpnt);
2699 SCpnt = NULL;
2701 if ((STp->buffer)->last_result_fatal != 0) {
2702 #if DEBUG
2703 if (debugging)
2704 printk(ST_DEB_MSG "st%d: Can't read medium partition page.\n", dev);
2705 #endif
2706 result = (-EIO);
2708 else {
2709 result = (STp->buffer)->b_data[MODE_HEADER_LENGTH + 3] + 1;
2710 #if DEBUG
2711 if (debugging)
2712 printk(ST_DEB_MSG "st%d: Number of partitions %d.\n", dev, result);
2713 #endif
2716 return result;
2720 /* Partition the tape into two partitions if size > 0 or one partition if
2721 size == 0 */
2722 static int
2723 partition_tape(struct inode * inode, int size)
2725 int dev = TAPE_NR(inode->i_rdev), result;
2726 int length;
2727 Scsi_Tape *STp;
2728 Scsi_Cmnd * SCpnt = NULL;
2729 unsigned char cmd[10], *bp;
2731 if ((result = nbr_partitions(inode)) < 0)
2732 return result;
2733 STp = &(scsi_tapes[dev]);
2735 /* The mode page is in the buffer. Let's modify it and write it. */
2736 bp = &((STp->buffer)->b_data[0]);
2737 if (size <= 0) {
2738 length = 8;
2739 bp[MODE_HEADER_LENGTH + 3] = 0;
2740 #if DEBUG
2741 if (debugging)
2742 printk(ST_DEB_MSG "st%d: Formatting tape with one partition.\n", dev);
2743 #endif
2745 else {
2746 length = 10;
2747 bp[MODE_HEADER_LENGTH + 3] = 1;
2748 bp[MODE_HEADER_LENGTH + 8] = (size >> 8) & 0xff;
2749 bp[MODE_HEADER_LENGTH + 9] = size & 0xff;
2750 #if DEBUG
2751 if (debugging)
2752 printk(ST_DEB_MSG "st%d: Formatting tape with two partition (1 = %d MB).\n",
2753 dev, size);
2754 #endif
2756 bp[MODE_HEADER_LENGTH + 6] = 0;
2757 bp[MODE_HEADER_LENGTH + 7] = 0;
2758 bp[MODE_HEADER_LENGTH + 4] = 0x30; /* IDP | PSUM = MB */
2760 bp[0] = 0;
2761 bp[1] = 0;
2762 bp[MODE_HEADER_LENGTH] &= 0x3f;
2763 bp[MODE_HEADER_LENGTH + 1] = length - 2;
2765 memset(cmd, 0, 10);
2766 cmd[0] = MODE_SELECT;
2767 cmd[1] = 0x10;
2768 cmd[4] = length + MODE_HEADER_LENGTH;
2770 SCpnt = st_do_scsi(SCpnt, STp, cmd, cmd[4], STp->long_timeout, MAX_READY_RETRIES);
2771 if (SCpnt == NULL)
2772 return (-EBUSY);
2773 scsi_release_command(SCpnt);
2774 SCpnt = NULL;
2776 if ((STp->buffer)->last_result_fatal != 0) {
2777 printk(KERN_INFO "st%d: Partitioning of tape failed.\n", dev);
2778 result = (-EIO);
2780 else
2781 result = 0;
2783 return result;
2788 /* The ioctl command */
2789 static int
2790 st_ioctl(struct inode * inode,struct file * file,
2791 unsigned int cmd_in, unsigned long arg)
2793 int i, cmd_nr, cmd_type, bt;
2794 unsigned int blk;
2795 struct mtop mtc;
2796 struct mtpos mt_pos;
2797 Scsi_Tape *STp;
2798 ST_mode *STm;
2799 ST_partstat *STps;
2800 int dev = TAPE_NR(inode->i_rdev);
2802 STp = &(scsi_tapes[dev]);
2803 #if DEBUG
2804 if (debugging && !STp->in_use) {
2805 printk(ST_DEB_MSG "st%d: Incorrect device.\n", dev);
2806 return (-EIO);
2808 #endif
2809 STm = &(STp->modes[STp->current_mode]);
2810 STps = &(STp->ps[STp->partition]);
2813 * If we are in the middle of error recovery, don't let anyone
2814 * else try and use this device. Also, if error recovery fails, it
2815 * may try and take the device offline, in which case all further
2816 * access to the device is prohibited.
2818 if( !scsi_block_when_processing_errors(STp->device) )
2820 return -ENXIO;
2823 cmd_type = _IOC_TYPE(cmd_in);
2824 cmd_nr = _IOC_NR(cmd_in);
2826 if (cmd_type == _IOC_TYPE(MTIOCTOP) && cmd_nr == _IOC_NR(MTIOCTOP)) {
2827 if (_IOC_SIZE(cmd_in) != sizeof(mtc))
2828 return (-EINVAL);
2830 i = copy_from_user((char *) &mtc, (char *)arg, sizeof(struct mtop));
2831 if (i)
2832 return (-EFAULT);
2834 if (mtc.mt_op == MTSETDRVBUFFER && !capable(CAP_SYS_ADMIN)) {
2835 printk(KERN_WARNING "st%d: MTSETDRVBUFFER only allowed for root.\n", dev);
2836 return (-EPERM);
2838 if (!STm->defined &&
2839 (mtc.mt_op != MTSETDRVBUFFER && (mtc.mt_count & MT_ST_OPTIONS) == 0))
2840 return (-ENXIO);
2842 if (!(STp->device)->was_reset) {
2844 if (STps->eof == ST_FM_HIT) {
2845 if (mtc.mt_op == MTFSF || mtc.mt_op == MTFSFM|| mtc.mt_op == MTEOM) {
2846 mtc.mt_count -= 1;
2847 if (STps->drv_file >= 0)
2848 STps->drv_file += 1;
2850 else if (mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM) {
2851 mtc.mt_count += 1;
2852 if (STps->drv_file >= 0)
2853 STps->drv_file += 1;
2857 if (mtc.mt_op == MTSEEK) {
2858 /* Old position must be restored if partition will be changed */
2859 i = !STp->can_partitions ||
2860 (STp->new_partition != STp->partition);
2862 else {
2863 i = mtc.mt_op == MTREW || mtc.mt_op == MTOFFL ||
2864 mtc.mt_op == MTRETEN || mtc.mt_op == MTEOM ||
2865 mtc.mt_op == MTLOCK || mtc.mt_op == MTLOAD ||
2866 mtc.mt_op == MTCOMPRESSION;
2868 i = flush_buffer(inode, file, i);
2869 if (i < 0)
2870 return i;
2872 else {
2874 * If there was a bus reset, block further access
2875 * to this device. If the user wants to rewind the tape,
2876 * then reset the flag and allow access again.
2878 if(mtc.mt_op != MTREW &&
2879 mtc.mt_op != MTOFFL &&
2880 mtc.mt_op != MTRETEN &&
2881 mtc.mt_op != MTERASE &&
2882 mtc.mt_op != MTSEEK &&
2883 mtc.mt_op != MTEOM)
2884 return (-EIO);
2885 STp->device->was_reset = 0;
2886 if (STp->door_locked != ST_UNLOCKED &&
2887 STp->door_locked != ST_LOCK_FAILS) {
2888 if (st_int_ioctl(inode, MTLOCK, 0)) {
2889 printk(KERN_NOTICE "st%d: Could not relock door after bus reset.\n",
2890 dev);
2891 STp->door_locked = ST_UNLOCKED;
2896 if (mtc.mt_op != MTNOP && mtc.mt_op != MTSETBLK &&
2897 mtc.mt_op != MTSETDENSITY && mtc.mt_op != MTWSM &&
2898 mtc.mt_op != MTSETDRVBUFFER && mtc.mt_op != MTSETPART)
2899 STps->rw = ST_IDLE; /* Prevent automatic WEOF and fsf */
2901 if (mtc.mt_op == MTOFFL && STp->door_locked != ST_UNLOCKED)
2902 st_int_ioctl(inode, MTUNLOCK, 0); /* Ignore result! */
2904 if (mtc.mt_op == MTSETDRVBUFFER &&
2905 (mtc.mt_count & MT_ST_OPTIONS) != 0)
2906 return st_set_options(inode, mtc.mt_count);
2907 if (mtc.mt_op == MTSETPART) {
2908 if (!STp->can_partitions ||
2909 mtc.mt_count < 0 || mtc.mt_count >= ST_NBR_PARTITIONS)
2910 return (-EINVAL);
2911 if (mtc.mt_count >= STp->nbr_partitions &&
2912 (STp->nbr_partitions = nbr_partitions(inode)) < 0)
2913 return (-EIO);
2914 if (mtc.mt_count >= STp->nbr_partitions)
2915 return (-EINVAL);
2916 STp->new_partition = mtc.mt_count;
2917 return 0;
2919 if (mtc.mt_op == MTMKPART) {
2920 if (!STp->can_partitions)
2921 return (-EINVAL);
2922 if ((i = st_int_ioctl(inode, MTREW, 0)) < 0 ||
2923 (i = partition_tape(inode, mtc.mt_count)) < 0)
2924 return i;
2925 for (i=0; i < ST_NBR_PARTITIONS; i++) {
2926 STp->ps[i].rw = ST_IDLE;
2927 STp->ps[i].at_sm = 0;
2928 STp->ps[i].last_block_valid = FALSE;
2930 STp->partition = STp->new_partition = 0;
2931 STp->nbr_partitions = 1; /* Bad guess ?-) */
2932 STps->drv_block = STps->drv_file = 0;
2933 return 0;
2935 if (mtc.mt_op == MTSEEK) {
2936 i = set_location(inode, mtc.mt_count, STp->new_partition, 0);
2937 if (!STp->can_partitions)
2938 STp->ps[0].rw = ST_IDLE;
2939 return i;
2941 if (STp->can_partitions && STp->ready == ST_READY &&
2942 (i = update_partition(inode)) < 0)
2943 return i;
2944 if (mtc.mt_op == MTCOMPRESSION)
2945 return st_compression(STp, (mtc.mt_count & 1));
2946 else
2947 return st_int_ioctl(inode, mtc.mt_op, mtc.mt_count);
2950 if (!STm->defined)
2951 return (-ENXIO);
2953 if ((i = flush_buffer(inode, file, FALSE)) < 0)
2954 return i;
2955 if (STp->can_partitions &&
2956 (i = update_partition(inode)) < 0)
2957 return i;
2959 if (cmd_type == _IOC_TYPE(MTIOCGET) && cmd_nr == _IOC_NR(MTIOCGET)) {
2961 if (_IOC_SIZE(cmd_in) != sizeof(struct mtget))
2962 return (-EINVAL);
2964 (STp->mt_status)->mt_dsreg =
2965 ((STp->block_size << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK) |
2966 ((STp->density << MT_ST_DENSITY_SHIFT) & MT_ST_DENSITY_MASK);
2967 (STp->mt_status)->mt_blkno = STps->drv_block;
2968 (STp->mt_status)->mt_fileno = STps->drv_file;
2969 if (STp->block_size != 0) {
2970 if (STps->rw == ST_WRITING)
2971 (STp->mt_status)->mt_blkno +=
2972 (STp->buffer)->buffer_bytes / STp->block_size;
2973 else if (STps->rw == ST_READING)
2974 (STp->mt_status)->mt_blkno -= ((STp->buffer)->buffer_bytes +
2975 STp->block_size - 1) / STp->block_size;
2978 (STp->mt_status)->mt_gstat = 0;
2979 if (STp->drv_write_prot)
2980 (STp->mt_status)->mt_gstat |= GMT_WR_PROT(0xffffffff);
2981 if ((STp->mt_status)->mt_blkno == 0) {
2982 if ((STp->mt_status)->mt_fileno == 0)
2983 (STp->mt_status)->mt_gstat |= GMT_BOT(0xffffffff);
2984 else
2985 (STp->mt_status)->mt_gstat |= GMT_EOF(0xffffffff);
2987 (STp->mt_status)->mt_resid = STp->partition;
2988 if (STps->eof == ST_EOM_OK || STps->eof == ST_EOM_ERROR)
2989 (STp->mt_status)->mt_gstat |= GMT_EOT(0xffffffff);
2990 else if (STps->eof >= ST_EOM_OK)
2991 (STp->mt_status)->mt_gstat |= GMT_EOD(0xffffffff);
2992 if (STp->density == 1)
2993 (STp->mt_status)->mt_gstat |= GMT_D_800(0xffffffff);
2994 else if (STp->density == 2)
2995 (STp->mt_status)->mt_gstat |= GMT_D_1600(0xffffffff);
2996 else if (STp->density == 3)
2997 (STp->mt_status)->mt_gstat |= GMT_D_6250(0xffffffff);
2998 if (STp->ready == ST_READY)
2999 (STp->mt_status)->mt_gstat |= GMT_ONLINE(0xffffffff);
3000 if (STp->ready == ST_NO_TAPE)
3001 (STp->mt_status)->mt_gstat |= GMT_DR_OPEN(0xffffffff);
3002 if (STps->at_sm)
3003 (STp->mt_status)->mt_gstat |= GMT_SM(0xffffffff);
3004 if (STm->do_async_writes || (STm->do_buffer_writes && STp->block_size != 0) ||
3005 STp->drv_buffer != 0)
3006 (STp->mt_status)->mt_gstat |= GMT_IM_REP_EN(0xffffffff);
3008 i = copy_to_user((char *)arg, (char *)(STp->mt_status),
3009 sizeof(struct mtget));
3010 if (i)
3011 return (-EFAULT);
3013 (STp->mt_status)->mt_erreg = 0; /* Clear after read */
3014 return 0;
3015 } /* End of MTIOCGET */
3017 if (cmd_type == _IOC_TYPE(MTIOCPOS) && cmd_nr == _IOC_NR(MTIOCPOS)) {
3018 if (_IOC_SIZE(cmd_in) != sizeof(struct mtpos))
3019 return (-EINVAL);
3020 if ((i = get_location(inode, &blk, &bt, 0)) < 0)
3021 return i;
3022 mt_pos.mt_blkno = blk;
3023 i = copy_to_user((char *)arg, (char *) (&mt_pos), sizeof(struct mtpos));
3024 if (i)
3025 return (-EFAULT);
3026 return 0;
3029 return scsi_ioctl(STp->device, cmd_in, (void *) arg);
3033 /* Try to allocate a new tape buffer */
3034 static ST_buffer *
3035 new_tape_buffer( int from_initialization, int need_dma )
3037 int priority, a_size;
3038 ST_buffer *tb;
3040 if (st_nbr_buffers >= st_template.dev_max)
3041 return NULL; /* Should never happen */
3043 if (from_initialization) {
3044 priority = GFP_ATOMIC;
3045 a_size = st_buffer_size;
3047 else {
3048 priority = GFP_KERNEL;
3049 for (a_size = PAGE_SIZE; a_size < st_buffer_size; a_size <<= 1)
3050 ; /* Make sure we allocate efficiently */
3052 tb = (ST_buffer *)scsi_init_malloc(sizeof(ST_buffer), priority);
3053 if (tb) {
3054 if (need_dma)
3055 priority |= GFP_DMA;
3056 tb->b_data = (unsigned char *)scsi_init_malloc(a_size, priority);
3057 if (!tb->b_data) {
3058 scsi_init_free((char *)tb, sizeof(ST_buffer));
3059 tb = NULL;
3062 if (!tb) {
3063 printk(KERN_NOTICE "st: Can't allocate new tape buffer (nbr %d).\n",
3064 st_nbr_buffers);
3065 return NULL;
3067 #if DEBUG
3068 if (debugging)
3069 printk(ST_DEB_MSG
3070 "st: Allocated tape buffer %d (%d bytes, dma: %d, a: %p).\n",
3071 st_nbr_buffers, a_size, need_dma, tb->b_data);
3072 #endif
3073 tb->in_use = 0;
3074 tb->dma = need_dma;
3075 tb->buffer_size = a_size;
3076 tb->writing = 0;
3077 tb->orig_b_data = NULL;
3078 st_buffers[st_nbr_buffers++] = tb;
3079 return tb;
3083 /* Try to allocate a temporary enlarged tape buffer */
3084 static int
3085 enlarge_buffer(ST_buffer *STbuffer, int new_size, int need_dma)
3087 int a_size, priority;
3088 unsigned char *tbd;
3090 normalize_buffer(STbuffer);
3092 for (a_size = PAGE_SIZE; a_size < new_size; a_size <<= 1)
3093 ; /* Make sure that we allocate efficiently */
3095 priority = GFP_KERNEL;
3096 if (need_dma)
3097 priority |= GFP_DMA;
3098 tbd = (unsigned char *)scsi_init_malloc(a_size, priority);
3099 if (!tbd)
3100 return FALSE;
3101 #if DEBUG
3102 if (debugging)
3103 printk(ST_DEB_MSG
3104 "st: Buffer at %p enlarged to %d bytes (dma: %d, a: %p).\n",
3105 STbuffer->b_data, a_size, need_dma, tbd);
3106 #endif
3108 STbuffer->orig_b_data = STbuffer->b_data;
3109 STbuffer->orig_size = STbuffer->buffer_size;
3110 STbuffer->b_data = tbd;
3111 STbuffer->buffer_size = a_size;
3112 return TRUE;
3116 /* Release the extra buffer */
3117 static void
3118 normalize_buffer(ST_buffer *STbuffer)
3120 if (STbuffer->orig_b_data == NULL)
3121 return;
3123 scsi_init_free(STbuffer->b_data, STbuffer->buffer_size);
3124 STbuffer->b_data = STbuffer->orig_b_data;
3125 STbuffer->orig_b_data = NULL;
3126 STbuffer->buffer_size = STbuffer->orig_size;
3128 #if DEBUG
3129 if (debugging)
3130 printk(ST_DEB_MSG "st: Buffer at %p normalized to %d bytes.\n",
3131 STbuffer->b_data, STbuffer->buffer_size);
3132 #endif
3136 #ifndef MODULE
3137 /* Set the boot options. Syntax: st=xxx,yyy
3138 where xxx is buffer size in 1024 byte blocks and yyy is write threshold
3139 in 1024 byte blocks. */
3140 __initfunc( void
3141 st_setup(char *str, int *ints))
3143 if (ints[0] > 0 && ints[1] > 0)
3144 st_buffer_size = ints[1] * ST_KILOBYTE;
3145 if (ints[0] > 1 && ints[2] > 0) {
3146 st_write_threshold = ints[2] * ST_KILOBYTE;
3147 if (st_write_threshold > st_buffer_size)
3148 st_write_threshold = st_buffer_size;
3150 if (ints[0] > 2 && ints[3] > 0)
3151 st_max_buffers = ints[3];
3153 #endif
3156 static struct file_operations st_fops = {
3157 NULL, /* lseek - default */
3158 st_read, /* read - general block-dev read */
3159 st_write, /* write - general block-dev write */
3160 NULL, /* readdir - bad */
3161 NULL, /* select */
3162 st_ioctl, /* ioctl */
3163 NULL, /* mmap */
3164 scsi_tape_open, /* open */
3165 NULL, /* flush */
3166 scsi_tape_close, /* release */
3167 NULL /* fsync */
3170 static int st_attach(Scsi_Device * SDp){
3171 Scsi_Tape * tpnt;
3172 ST_mode * STm;
3173 ST_partstat * STps;
3174 int i;
3176 if(SDp->type != TYPE_TAPE) return 1;
3178 if(st_template.nr_dev >= st_template.dev_max)
3180 SDp->attached--;
3181 return 1;
3184 for(tpnt = scsi_tapes, i=0; i<st_template.dev_max; i++, tpnt++)
3185 if(!tpnt->device) break;
3187 if(i >= st_template.dev_max) panic ("scsi_devices corrupt (st)");
3189 scsi_tapes[i].device = SDp;
3190 if (SDp->scsi_level <= 2)
3191 scsi_tapes[i].mt_status->mt_type = MT_ISSCSI1;
3192 else
3193 scsi_tapes[i].mt_status->mt_type = MT_ISSCSI2;
3195 tpnt->devt = MKDEV(SCSI_TAPE_MAJOR, i);
3196 tpnt->dirty = 0;
3197 tpnt->waiting = NULL;
3198 tpnt->in_use = 0;
3199 tpnt->drv_buffer = 1; /* Try buffering if no mode sense */
3200 tpnt->restr_dma = (SDp->host)->unchecked_isa_dma;
3201 tpnt->density = 0;
3202 tpnt->do_auto_lock = ST_AUTO_LOCK;
3203 tpnt->can_bsr = ST_IN_FILE_POS;
3204 tpnt->can_partitions = 0;
3205 tpnt->two_fm = ST_TWO_FM;
3206 tpnt->fast_mteom = ST_FAST_MTEOM;
3207 tpnt->scsi2_logical = ST_SCSI2LOGICAL;
3208 tpnt->write_threshold = st_write_threshold;
3209 tpnt->default_drvbuffer = 0xff; /* No forced buffering */
3210 tpnt->partition = 0;
3211 tpnt->new_partition = 0;
3212 tpnt->nbr_partitions = 0;
3213 tpnt->timeout = ST_TIMEOUT;
3214 tpnt->long_timeout = ST_LONG_TIMEOUT;
3216 for (i=0; i < ST_NBR_MODES; i++) {
3217 STm = &(tpnt->modes[i]);
3218 STm->defined = FALSE;
3219 STm->sysv = ST_SYSV;
3220 STm->defaults_for_writes = 0;
3221 STm->do_async_writes = ST_ASYNC_WRITES;
3222 STm->do_buffer_writes = ST_BUFFER_WRITES;
3223 STm->do_read_ahead = ST_READ_AHEAD;
3224 STm->default_compression = ST_DONT_TOUCH;
3225 STm->default_blksize = (-1); /* No forced size */
3226 STm->default_density = (-1); /* No forced density */
3229 for (i=0; i < ST_NBR_PARTITIONS; i++) {
3230 STps = &(tpnt->ps[i]);
3231 STps->rw = ST_IDLE;
3232 STps->eof = ST_NOEOF;
3233 STps->at_sm = 0;
3234 STps->last_block_valid = FALSE;
3235 STps->drv_block = 0;
3236 STps->drv_file = 0;
3239 tpnt->current_mode = 0;
3240 tpnt->modes[0].defined = TRUE;
3242 tpnt->density_changed = tpnt->compression_changed =
3243 tpnt->blksize_changed = FALSE;
3245 st_template.nr_dev++;
3246 return 0;
3249 static int st_detect(Scsi_Device * SDp)
3251 if(SDp->type != TYPE_TAPE) return 0;
3253 printk(KERN_INFO
3254 "Detected scsi tape st%d at scsi%d, channel %d, id %d, lun %d\n",
3255 st_template.dev_noticed++,
3256 SDp->host->host_no, SDp->channel, SDp->id, SDp->lun);
3258 return 1;
3261 static int st_registered = 0;
3263 /* Driver initialization (not __initfunc because may be called later) */
3264 static int st_init()
3266 int i;
3267 Scsi_Tape * STp;
3268 #if !ST_RUNTIME_BUFFERS
3269 int target_nbr;
3270 #endif
3272 if (st_template.dev_noticed == 0) return 0;
3274 if(!st_registered) {
3275 if (register_chrdev(SCSI_TAPE_MAJOR,"st",&st_fops)) {
3276 printk(KERN_ERR "Unable to get major %d for SCSI tapes\n",MAJOR_NR);
3277 return 1;
3279 st_registered++;
3282 if (scsi_tapes) return 0;
3283 st_template.dev_max = st_template.dev_noticed + ST_EXTRA_DEVS;
3284 if (st_template.dev_max < ST_MAX_TAPES)
3285 st_template.dev_max = ST_MAX_TAPES;
3286 if (st_template.dev_max > 128 / ST_NBR_MODES)
3287 printk(KERN_INFO "st: Only %d tapes accessible.\n", 128 / ST_NBR_MODES);
3288 scsi_tapes =
3289 (Scsi_Tape *) scsi_init_malloc(st_template.dev_max * sizeof(Scsi_Tape),
3290 GFP_ATOMIC);
3291 if (scsi_tapes == NULL) {
3292 printk(KERN_ERR "Unable to allocate descriptors for SCSI tapes.\n");
3293 unregister_chrdev(SCSI_TAPE_MAJOR, "st");
3294 return 1;
3297 #if DEBUG
3298 printk(ST_DEB_MSG "st: Buffer size %d bytes, write threshold %d bytes.\n",
3299 st_buffer_size, st_write_threshold);
3300 #endif
3302 memset(scsi_tapes, 0, st_template.dev_max * sizeof(Scsi_Tape));
3303 for (i=0; i < st_template.dev_max; ++i) {
3304 STp = &(scsi_tapes[i]);
3305 STp->capacity = 0xfffff;
3306 STp->mt_status = (struct mtget *) scsi_init_malloc(sizeof(struct mtget),
3307 GFP_ATOMIC);
3308 /* Initialize status */
3309 memset((void *) scsi_tapes[i].mt_status, 0, sizeof(struct mtget));
3312 /* Allocate the buffers */
3313 st_buffers =
3314 (ST_buffer **) scsi_init_malloc(st_template.dev_max * sizeof(ST_buffer *),
3315 GFP_ATOMIC);
3316 if (st_buffers == NULL) {
3317 printk(KERN_ERR "Unable to allocate tape buffer pointers.\n");
3318 unregister_chrdev(SCSI_TAPE_MAJOR, "st");
3319 scsi_init_free((char *) scsi_tapes,
3320 st_template.dev_max * sizeof(Scsi_Tape));
3321 return 1;
3324 #if ST_RUNTIME_BUFFERS
3325 st_nbr_buffers = 0;
3326 #else
3327 target_nbr = st_template.dev_noticed;
3328 if (target_nbr < ST_EXTRA_DEVS)
3329 target_nbr = ST_EXTRA_DEVS;
3330 if (target_nbr > st_max_buffers)
3331 target_nbr = st_max_buffers;
3333 for (i=st_nbr_buffers=0; i < target_nbr; i++) {
3334 if (!new_tape_buffer(TRUE, TRUE)) {
3335 if (i == 0) {
3336 #if 0
3337 printk(KERN_ERR "Can't continue without at least one tape buffer.\n");
3338 unregister_chrdev(SCSI_TAPE_MAJOR, "st");
3339 scsi_init_free((char *) st_buffers,
3340 st_template.dev_max * sizeof(ST_buffer *));
3341 scsi_init_free((char *) scsi_tapes,
3342 st_template.dev_max * sizeof(Scsi_Tape));
3343 return 1;
3344 #else
3345 printk(KERN_INFO "No tape buffers allocated at initialization.\n");
3346 break;
3347 #endif
3349 printk(KERN_INFO "Number of tape buffers adjusted.\n");
3350 break;
3353 #endif
3354 return 0;
3357 static void st_detach(Scsi_Device * SDp)
3359 Scsi_Tape * tpnt;
3360 int i;
3362 for(tpnt = scsi_tapes, i=0; i<st_template.dev_max; i++, tpnt++)
3363 if(tpnt->device == SDp) {
3364 tpnt->device = NULL;
3365 SDp->attached--;
3366 st_template.nr_dev--;
3367 st_template.dev_noticed--;
3368 return;
3370 return;
3374 #ifdef MODULE
3376 int init_module(void) {
3377 int result;
3379 st_template.module = &__this_module;
3380 result = scsi_register_module(MODULE_SCSI_DEV, &st_template);
3381 if (result)
3382 return result;
3384 if (buffer_kbs > 0)
3385 st_buffer_size = buffer_kbs * ST_KILOBYTE;
3386 if (write_threshold_kbs > 0)
3387 st_write_threshold = write_threshold_kbs * ST_KILOBYTE;
3388 if (st_write_threshold > st_buffer_size)
3389 st_write_threshold = st_buffer_size;
3390 if (max_buffers > 0)
3391 st_max_buffers = max_buffers;
3392 printk(KERN_INFO "st: bufsize %d, wrt %d, max buffers %d.\n",
3393 st_buffer_size, st_write_threshold, st_max_buffers);
3395 return 0;
3398 void cleanup_module( void)
3400 int i;
3402 scsi_unregister_module(MODULE_SCSI_DEV, &st_template);
3403 unregister_chrdev(SCSI_TAPE_MAJOR, "st");
3404 st_registered--;
3405 if(scsi_tapes != NULL) {
3406 scsi_init_free((char *) scsi_tapes,
3407 st_template.dev_max * sizeof(Scsi_Tape));
3409 if (st_buffers != NULL) {
3410 for (i=0; i < st_nbr_buffers; i++)
3411 if (st_buffers[i] != NULL) {
3412 scsi_init_free((char *) st_buffers[i]->b_data,
3413 st_buffers[i]->buffer_size);
3414 scsi_init_free((char *) st_buffers[i], sizeof(ST_buffer));
3417 scsi_init_free((char *) st_buffers,
3418 st_template.dev_max * sizeof(ST_buffer *));
3421 st_template.dev_max = 0;
3422 printk(KERN_INFO "st: Unloaded.\n");
3424 #endif /* MODULE */