Linux 2.2.3pre3
[davej-history.git] / drivers / scsi / st.c
blobc53e2e7908a2514dfd6bf39166d80edbb1aa7649
1 /*
2 SCSI Tape Driver for Linux version 1.1 and newer. See the accompanying
3 file README.st for more information.
5 History:
6 Rewritten from Dwayne Forsyth's SCSI tape driver by Kai Makisara.
7 Contribution and ideas from several people including (in alphabetical
8 order) Klaus Ehrenfried, Wolfgang Denk, Steve Hirsch, Andreas Koppenh"ofer,
9 Michael Leodolter, Eyal Lebedinsky, J"org Weule, and Eric Youngdale.
11 Copyright 1992 - 1999 Kai Makisara
12 email Kai.Makisara@metla.fi
14 Last modified: Sun Mar 7 09:03:17 1999 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>
51 #define ST_KILOBYTE 1024
53 #include "st_options.h"
54 #include "st.h"
56 #include "constants.h"
58 #ifdef MODULE
59 MODULE_PARM(buffer_kbs, "i");
60 MODULE_PARM(write_threshold_kbs, "i");
61 MODULE_PARM(max_buffers, "i");
62 MODULE_PARM(max_sg_segs, "i");
63 static int buffer_kbs = 0;
64 static int write_threshold_kbs = 0;
65 static int max_buffers = 0;
66 static int max_sg_segs = 0;
67 #endif
69 /* The default definitions have been moved to st_options.h */
71 #define ST_BUFFER_SIZE (ST_BUFFER_BLOCKS * ST_KILOBYTE)
72 #define ST_WRITE_THRESHOLD (ST_WRITE_THRESHOLD_BLOCKS * ST_KILOBYTE)
74 /* The buffer size should fit into the 24 bits for length in the
75 6-byte SCSI read and write commands. */
76 #if ST_BUFFER_SIZE >= (2 << 24 - 1)
77 #error "Buffer size should not exceed (2 << 24 - 1) bytes!"
78 #endif
80 #if DEBUG
81 static int debugging = 1;
82 #endif
84 #define MAX_RETRIES 0
85 #define MAX_WRITE_RETRIES 0
86 #define MAX_READY_RETRIES 5
87 #define NO_TAPE NOT_READY
89 #define ST_TIMEOUT (900 * HZ)
90 #define ST_LONG_TIMEOUT (14000 * HZ)
92 #define TAPE_NR(x) (MINOR(x) & ~(128 | ST_MODE_MASK))
93 #define TAPE_MODE(x) ((MINOR(x) & ST_MODE_MASK) >> ST_MODE_SHIFT)
95 /* Internal ioctl to set both density (uppermost 8 bits) and blocksize (lower
96 24 bits) */
97 #define SET_DENS_AND_BLK 0x10001
99 static int st_nbr_buffers;
100 static ST_buffer **st_buffers;
101 static int st_buffer_size = ST_BUFFER_SIZE;
102 static int st_write_threshold = ST_WRITE_THRESHOLD;
103 static int st_max_buffers = ST_MAX_BUFFERS;
104 static int st_max_sg_segs = ST_MAX_SG;
106 static Scsi_Tape * scsi_tapes = NULL;
108 static int modes_defined = FALSE;
110 static ST_buffer *new_tape_buffer(int, int);
111 static int enlarge_buffer(ST_buffer *, int, int);
112 static void normalize_buffer(ST_buffer *);
113 static int append_to_buffer(const char *, ST_buffer *, int);
114 static int from_buffer(ST_buffer *, char *, int);
116 static int st_init(void);
117 static int st_attach(Scsi_Device *);
118 static int st_detect(Scsi_Device *);
119 static void st_detach(Scsi_Device *);
121 struct Scsi_Device_Template st_template = {NULL, "tape", "st", NULL, TYPE_TAPE,
122 SCSI_TAPE_MAJOR, 0, 0, 0, 0,
123 st_detect, st_init,
124 NULL, st_attach, st_detach};
126 static int st_compression(Scsi_Tape *, int);
128 static int find_partition(struct inode *);
129 static int update_partition(struct inode *);
131 static int st_int_ioctl(struct inode * inode, unsigned int cmd_in,
132 unsigned long arg);
137 /* Convert the result to success code */
138 static int
139 st_chk_result(Scsi_Cmnd * SCpnt)
141 int dev = TAPE_NR(SCpnt->request.rq_dev);
142 int result = SCpnt->result;
143 unsigned char * sense = SCpnt->sense_buffer, scode;
144 #if DEBUG
145 const char *stp;
146 #endif
148 if (!result /* && SCpnt->sense_buffer[0] == 0 */ )
149 return 0;
151 if (driver_byte(result) & DRIVER_SENSE)
152 scode = sense[2] & 0x0f;
153 else {
154 sense[0] = 0; /* We don't have sense data if this byte is zero */
155 scode = 0;
158 #if DEBUG
159 if (debugging) {
160 printk(ST_DEB_MSG "st%d: Error: %x, cmd: %x %x %x %x %x %x Len: %d\n",
161 dev, result,
162 SCpnt->data_cmnd[0], SCpnt->data_cmnd[1], SCpnt->data_cmnd[2],
163 SCpnt->data_cmnd[3], SCpnt->data_cmnd[4], SCpnt->data_cmnd[5],
164 SCpnt->request_bufflen);
165 if (driver_byte(result) & DRIVER_SENSE)
166 print_sense("st", SCpnt);
167 else
168 printk("\n");
170 else
171 #endif
172 if (!(driver_byte(result) & DRIVER_SENSE) ||
173 ((sense[0] & 0x70) == 0x70 &&
174 scode != NO_SENSE &&
175 scode != RECOVERED_ERROR &&
176 /* scode != UNIT_ATTENTION && */
177 scode != BLANK_CHECK &&
178 scode != VOLUME_OVERFLOW &&
179 SCpnt->data_cmnd[0] != MODE_SENSE &&
180 SCpnt->data_cmnd[0] != TEST_UNIT_READY)) { /* Abnormal conditions for tape */
181 if (driver_byte(result) & DRIVER_SENSE) {
182 printk(KERN_WARNING "st%d: Error with sense data: ", dev);
183 print_sense("st", SCpnt);
185 else
186 printk(KERN_WARNING
187 "st%d: Error %x (sugg. bt 0x%x, driver bt 0x%x, host bt 0x%x).\n",
188 dev, result, suggestion(result), driver_byte(result),
189 host_byte(result));
192 if ((sense[0] & 0x70) == 0x70 &&
193 scode == RECOVERED_ERROR
194 #if ST_RECOVERED_WRITE_FATAL
195 && SCpnt->data_cmnd[0] != WRITE_6
196 && SCpnt->data_cmnd[0] != WRITE_FILEMARKS
197 #endif
199 scsi_tapes[dev].recover_count++;
200 scsi_tapes[dev].mt_status->mt_erreg += (1 << MT_ST_SOFTERR_SHIFT);
201 #if DEBUG
202 if (debugging) {
203 if (SCpnt->data_cmnd[0] == READ_6)
204 stp = "read";
205 else if (SCpnt->data_cmnd[0] == WRITE_6)
206 stp = "write";
207 else
208 stp = "ioctl";
209 printk(ST_DEB_MSG "st%d: Recovered %s error (%d).\n", dev, stp,
210 scsi_tapes[dev].recover_count);
212 #endif
213 if ((sense[2] & 0xe0) == 0)
214 return 0;
216 return (-EIO);
220 /* Wakeup from interrupt */
221 static void
222 st_sleep_done (Scsi_Cmnd * SCpnt)
224 unsigned int st_nbr;
225 int remainder;
226 Scsi_Tape * STp;
228 if ((st_nbr = TAPE_NR(SCpnt->request.rq_dev)) < st_template.nr_dev) {
229 STp = &(scsi_tapes[st_nbr]);
230 if ((STp->buffer)->writing &&
231 (SCpnt->sense_buffer[0] & 0x70) == 0x70 &&
232 (SCpnt->sense_buffer[2] & 0x40)) {
233 /* EOM at write-behind, has all been written? */
234 if ((SCpnt->sense_buffer[0] & 0x80) != 0)
235 remainder = (SCpnt->sense_buffer[3] << 24) |
236 (SCpnt->sense_buffer[4] << 16) |
237 (SCpnt->sense_buffer[5] << 8) | SCpnt->sense_buffer[6];
238 else
239 remainder = 0;
240 if ((SCpnt->sense_buffer[2] & 0x0f) == VOLUME_OVERFLOW ||
241 remainder > 0)
242 (STp->buffer)->last_result = SCpnt->result; /* Error */
243 else
244 (STp->buffer)->last_result = INT_MAX; /* OK */
246 else
247 (STp->buffer)->last_result = SCpnt->result;
248 SCpnt->request.rq_status = RQ_SCSI_DONE;
249 (STp->buffer)->last_SCpnt = SCpnt;
251 #if DEBUG
252 STp->write_pending = 0;
253 #endif
254 up(SCpnt->request.sem);
256 #if DEBUG
257 else if (debugging)
258 printk(KERN_ERR "st?: Illegal interrupt device %x\n", st_nbr);
259 #endif
263 /* Do the scsi command. Waits until command performed if do_wait is true.
264 Otherwise write_behind_check() is used to check that the command
265 has finished. */
266 static Scsi_Cmnd *
267 st_do_scsi(Scsi_Cmnd *SCpnt, Scsi_Tape *STp, unsigned char *cmd, int bytes,
268 int timeout, int retries, int do_wait)
270 unsigned long flags;
271 unsigned char *bp;
273 spin_lock_irqsave(&io_request_lock, flags);
274 if (SCpnt == NULL)
275 if ((SCpnt = scsi_allocate_device(NULL, STp->device, 1)) == NULL) {
276 printk(KERN_ERR "st%d: Can't get SCSI request.\n", TAPE_NR(STp->devt));
277 spin_unlock_irqrestore(&io_request_lock, flags);
278 return NULL;
281 cmd[1] |= (SCpnt->lun << 5) & 0xe0;
282 STp->sem = MUTEX_LOCKED;
283 SCpnt->use_sg = (bytes > (STp->buffer)->sg[0].length) ?
284 (STp->buffer)->use_sg : 0;
285 if (SCpnt->use_sg) {
286 bp = (char *)&((STp->buffer)->sg[0]);
287 if ((STp->buffer)->sg_segs < SCpnt->use_sg)
288 SCpnt->use_sg = (STp->buffer)->sg_segs;
290 else
291 bp = (STp->buffer)->b_data;
292 SCpnt->request.sem = &(STp->sem);
293 SCpnt->request.rq_status = RQ_SCSI_BUSY;
294 SCpnt->request.rq_dev = STp->devt;
296 scsi_do_cmd(SCpnt, (void *)cmd, bp, bytes,
297 st_sleep_done, timeout, retries);
298 spin_unlock_irqrestore(&io_request_lock, flags);
300 if (do_wait) {
301 down(SCpnt->request.sem);
303 (STp->buffer)->last_result_fatal = st_chk_result(SCpnt);
306 return SCpnt;
310 /* Handle the write-behind checking (downs the semaphore) */
311 static void
312 write_behind_check(Scsi_Tape *STp)
314 ST_buffer * STbuffer;
315 ST_partstat * STps;
317 STbuffer = STp->buffer;
319 #if DEBUG
320 if (STp->write_pending)
321 STp->nbr_waits++;
322 else
323 STp->nbr_finished++;
324 #endif
326 down(&(STp->sem));
328 (STp->buffer)->last_result_fatal = st_chk_result((STp->buffer)->last_SCpnt);
329 scsi_release_command((STp->buffer)->last_SCpnt);
331 if (STbuffer->writing < STbuffer->buffer_bytes)
332 #if 0
333 memcpy(STbuffer->b_data,
334 STbuffer->b_data + STbuffer->writing,
335 STbuffer->buffer_bytes - STbuffer->writing);
336 #else
337 printk(KERN_WARNING "st: write_behind_check: something left in buffer!\n");
338 #endif
339 STbuffer->buffer_bytes -= STbuffer->writing;
340 STps = &(STp->ps[STp->partition]);
341 if (STps->drv_block >= 0) {
342 if (STp->block_size == 0)
343 STps->drv_block++;
344 else
345 STps->drv_block += STbuffer->writing / STp->block_size;
347 STbuffer->writing = 0;
349 return;
353 /* Step over EOF if it has been inadvertently crossed (ioctl not used because
354 it messes up the block number). */
355 static int
356 cross_eof(Scsi_Tape *STp, int forward)
358 Scsi_Cmnd *SCpnt;
359 unsigned char cmd[10];
361 cmd[0] = SPACE;
362 cmd[1] = 0x01; /* Space FileMarks */
363 if (forward) {
364 cmd[2] = cmd[3] = 0;
365 cmd[4] = 1;
367 else
368 cmd[2] = cmd[3] = cmd[4] = 0xff; /* -1 filemarks */
369 cmd[5] = 0;
370 #if DEBUG
371 if (debugging)
372 printk(ST_DEB_MSG "st%d: Stepping over filemark %s.\n",
373 TAPE_NR(STp->devt), forward ? "forward" : "backward");
374 #endif
376 SCpnt = st_do_scsi(NULL, STp, cmd, 0, STp->timeout, MAX_RETRIES, TRUE);
377 if (!SCpnt)
378 return (-EBUSY);
380 scsi_release_command(SCpnt);
381 SCpnt = NULL;
383 if ((STp->buffer)->last_result != 0)
384 printk(KERN_ERR "st%d: Stepping over filemark %s failed.\n",
385 TAPE_NR(STp->devt), forward ? "forward" : "backward");
387 return (STp->buffer)->last_result_fatal;
391 /* Flush the write buffer (never need to write if variable blocksize). */
392 static int
393 flush_write_buffer(Scsi_Tape *STp)
395 int offset, transfer, blks;
396 int result;
397 unsigned char cmd[10];
398 Scsi_Cmnd *SCpnt;
399 ST_partstat * STps;
401 if ((STp->buffer)->writing) {
402 write_behind_check(STp);
403 if ((STp->buffer)->last_result_fatal) {
404 #if DEBUG
405 if (debugging)
406 printk(ST_DEB_MSG "st%d: Async write error (flush) %x.\n",
407 TAPE_NR(STp->devt), (STp->buffer)->last_result);
408 #endif
409 if ((STp->buffer)->last_result == INT_MAX)
410 return (-ENOSPC);
411 return (-EIO);
415 if (STp->block_size == 0)
416 return 0;
418 result = 0;
419 if (STp->dirty == 1) {
421 offset = (STp->buffer)->buffer_bytes;
422 transfer = ((offset + STp->block_size - 1) /
423 STp->block_size) * STp->block_size;
424 #if DEBUG
425 if (debugging)
426 printk(ST_DEB_MSG "st%d: Flushing %d bytes.\n", TAPE_NR(STp->devt), transfer);
427 #endif
428 memset((STp->buffer)->b_data + offset, 0, transfer - offset);
430 memset(cmd, 0, 10);
431 cmd[0] = WRITE_6;
432 cmd[1] = 1;
433 blks = transfer / STp->block_size;
434 cmd[2] = blks >> 16;
435 cmd[3] = blks >> 8;
436 cmd[4] = blks;
438 SCpnt = st_do_scsi(NULL, STp, cmd, transfer, STp->timeout, MAX_WRITE_RETRIES,
439 TRUE);
440 if (!SCpnt)
441 return (-EBUSY);
443 STps = &(STp->ps[STp->partition]);
444 if ((STp->buffer)->last_result_fatal != 0) {
445 if ((SCpnt->sense_buffer[0] & 0x70) == 0x70 &&
446 (SCpnt->sense_buffer[2] & 0x40) &&
447 (SCpnt->sense_buffer[2] & 0x0f) == NO_SENSE) {
448 STp->dirty = 0;
449 (STp->buffer)->buffer_bytes = 0;
450 result = (-ENOSPC);
452 else {
453 printk(KERN_ERR "st%d: Error on flush.\n", TAPE_NR(STp->devt));
454 result = (-EIO);
456 STps->drv_block = (-1);
458 else {
459 if (STps->drv_block >= 0)
460 STps->drv_block += blks;
461 STp->dirty = 0;
462 (STp->buffer)->buffer_bytes = 0;
464 scsi_release_command(SCpnt);
465 SCpnt = NULL;
467 return result;
471 /* Flush the tape buffer. The tape will be positioned correctly unless
472 seek_next is true. */
473 static int
474 flush_buffer(struct inode * inode, struct file * filp, int seek_next)
476 int backspace, result;
477 Scsi_Tape * STp;
478 ST_buffer * STbuffer;
479 ST_partstat * STps;
480 int dev = TAPE_NR(inode->i_rdev);
482 STp = &(scsi_tapes[dev]);
483 STbuffer = STp->buffer;
486 * If there was a bus reset, block further access
487 * to this device.
489 if( STp->device->was_reset )
490 return (-EIO);
492 if (STp->ready != ST_READY)
493 return 0;
495 STps = &(STp->ps[STp->partition]);
496 if (STps->rw == ST_WRITING) /* Writing */
497 return flush_write_buffer(STp);
499 if (STp->block_size == 0)
500 return 0;
502 backspace = ((STp->buffer)->buffer_bytes +
503 (STp->buffer)->read_pointer) / STp->block_size -
504 ((STp->buffer)->read_pointer + STp->block_size - 1) /
505 STp->block_size;
506 (STp->buffer)->buffer_bytes = 0;
507 (STp->buffer)->read_pointer = 0;
508 result = 0;
509 if (!seek_next) {
510 if (STps->eof == ST_FM_HIT) {
511 result = cross_eof(STp, FALSE); /* Back over the EOF hit */
512 if (!result)
513 STps->eof = ST_NOEOF;
514 else {
515 if (STps->drv_file >= 0)
516 STps->drv_file++;
517 STps->drv_block = 0;
520 if (!result && backspace > 0)
521 result = st_int_ioctl(inode, MTBSR, backspace);
523 else if (STps->eof == ST_FM_HIT) {
524 if (STps->drv_file >= 0)
525 STps->drv_file++;
526 STps->drv_block = 0;
527 STps->eof = ST_NOEOF;
530 return result;
534 \f/* Set the mode parameters */
535 static int
536 set_mode_densblk(struct inode * inode, Scsi_Tape *STp, ST_mode *STm)
538 int set_it = FALSE;
539 unsigned long arg;
540 int dev = TAPE_NR(inode->i_rdev);
542 if (!STp->density_changed &&
543 STm->default_density >= 0 &&
544 STm->default_density != STp->density) {
545 arg = STm->default_density;
546 set_it = TRUE;
548 else
549 arg = STp->density;
550 arg <<= MT_ST_DENSITY_SHIFT;
551 if (!STp->blksize_changed &&
552 STm->default_blksize >= 0 &&
553 STm->default_blksize != STp->block_size) {
554 arg |= STm->default_blksize;
555 set_it = TRUE;
557 else
558 arg |= STp->block_size;
559 if (set_it &&
560 st_int_ioctl(inode, SET_DENS_AND_BLK, arg)) {
561 printk(KERN_WARNING
562 "st%d: Can't set default block size to %d bytes and density %x.\n",
563 dev, STm->default_blksize, STm->default_density);
564 if (modes_defined)
565 return (-EINVAL);
567 return 0;
571 /* Open the device */
572 static int
573 scsi_tape_open(struct inode * inode, struct file * filp)
575 unsigned short flags;
576 int i, need_dma_buffer, new_session = FALSE;
577 unsigned char cmd[10];
578 Scsi_Cmnd * SCpnt;
579 Scsi_Tape * STp;
580 ST_mode * STm;
581 ST_partstat * STps;
582 int dev = TAPE_NR(inode->i_rdev);
583 int mode = TAPE_MODE(inode->i_rdev);
585 if (dev >= st_template.dev_max || !scsi_tapes[dev].device)
586 return (-ENXIO);
588 if( !scsi_block_when_processing_errors(scsi_tapes[dev].device) ) {
589 return -ENXIO;
592 STp = &(scsi_tapes[dev]);
593 if (STp->in_use) {
594 #if DEBUG
595 printk(ST_DEB_MSG "st%d: Device already in use.\n", dev);
596 #endif
597 return (-EBUSY);
599 STp->rew_at_close = (MINOR(inode->i_rdev) & 0x80) == 0;
601 if (mode != STp->current_mode) {
602 #if DEBUG
603 if (debugging)
604 printk(ST_DEB_MSG "st%d: Mode change from %d to %d.\n",
605 dev, STp->current_mode, mode);
606 #endif
607 new_session = TRUE;
608 STp->current_mode = mode;
610 STm = &(STp->modes[STp->current_mode]);
612 /* Allocate a buffer for this user */
613 need_dma_buffer = STp->restr_dma;
614 for (i=0; i < st_nbr_buffers; i++)
615 if (!st_buffers[i]->in_use &&
616 (!need_dma_buffer || st_buffers[i]->dma))
617 break;
618 if (i >= st_nbr_buffers) {
619 STp->buffer = new_tape_buffer(FALSE, need_dma_buffer);
620 if (STp->buffer == NULL) {
621 printk(KERN_WARNING "st%d: Can't allocate tape buffer.\n", dev);
622 return (-EBUSY);
625 else
626 STp->buffer = st_buffers[i];
627 (STp->buffer)->in_use = 1;
628 (STp->buffer)->writing = 0;
629 (STp->buffer)->last_result_fatal = 0;
630 (STp->buffer)->use_sg = STp->device->host->sg_tablesize;
632 /* Compute the usable buffer size for this SCSI adapter */
633 if (!(STp->buffer)->use_sg)
634 (STp->buffer)->buffer_size = (STp->buffer)->sg[0].length;
635 else {
636 for (i=0, (STp->buffer)->buffer_size = 0; i < (STp->buffer)->use_sg &&
637 i < (STp->buffer)->sg_segs; i++)
638 (STp->buffer)->buffer_size += (STp->buffer)->sg[i].length;
641 flags = filp->f_flags;
642 STp->write_prot = ((flags & O_ACCMODE) == O_RDONLY);
644 STp->dirty = 0;
645 for (i=0; i < ST_NBR_PARTITIONS; i++) {
646 STps = &(STp->ps[i]);
647 STps->rw = ST_IDLE;
649 STp->ready = ST_READY;
650 STp->recover_count = 0;
651 #if DEBUG
652 STp->nbr_waits = STp->nbr_finished = 0;
653 #endif
655 if (scsi_tapes[dev].device->host->hostt->module)
656 __MOD_INC_USE_COUNT(scsi_tapes[dev].device->host->hostt->module);
657 if(st_template.module)
658 __MOD_INC_USE_COUNT(st_template.module);
660 memset ((void *) &cmd[0], 0, 10);
661 cmd[0] = TEST_UNIT_READY;
663 SCpnt = st_do_scsi(NULL, STp, cmd, 0, STp->long_timeout, MAX_READY_RETRIES,
664 TRUE);
665 if (!SCpnt) {
666 if (scsi_tapes[dev].device->host->hostt->module)
667 __MOD_DEC_USE_COUNT(scsi_tapes[dev].device->host->hostt->module);
668 if(st_template.module)
669 __MOD_DEC_USE_COUNT(st_template.module);
670 return (-EBUSY);
673 if ((SCpnt->sense_buffer[0] & 0x70) == 0x70 &&
674 (SCpnt->sense_buffer[2] & 0x0f) == UNIT_ATTENTION) { /* New media? */
675 memset ((void *) &cmd[0], 0, 10);
676 cmd[0] = TEST_UNIT_READY;
678 SCpnt = st_do_scsi(SCpnt, STp, cmd, 0, STp->long_timeout, MAX_READY_RETRIES,
679 TRUE);
681 (STp->device)->was_reset = 0;
682 STp->partition = STp->new_partition = 0;
683 if (STp->can_partitions)
684 STp->nbr_partitions = 1; /* This guess will be updated later if necessary */
685 for (i=0; i < ST_NBR_PARTITIONS; i++) {
686 STps = &(STp->ps[i]);
687 STps->rw = ST_IDLE;
688 STps->eof = ST_NOEOF;
689 STps->at_sm = 0;
690 STps->last_block_valid = FALSE;
691 STps->drv_block = 0;
692 STps->drv_file = 0 ;
694 new_session = TRUE;
697 if ((STp->buffer)->last_result_fatal != 0) {
698 if ((STp->device)->scsi_level >= SCSI_2 &&
699 (SCpnt->sense_buffer[0] & 0x70) == 0x70 &&
700 (SCpnt->sense_buffer[2] & 0x0f) == NOT_READY &&
701 SCpnt->sense_buffer[12] == 0x3a) { /* Check ASC */
702 STp->ready = ST_NO_TAPE;
703 } else
704 STp->ready = ST_NOT_READY;
705 scsi_release_command(SCpnt);
706 SCpnt = NULL;
707 STp->density = 0; /* Clear the erroneous "residue" */
708 STp->write_prot = 0;
709 STp->block_size = 0;
710 STp->ps[0].drv_file = STp->ps[0].drv_block = (-1);
711 STp->partition = STp->new_partition = 0;
712 STp->door_locked = ST_UNLOCKED;
713 STp->in_use = 1;
714 return 0;
717 if (STp->omit_blklims)
718 STp->min_block = STp->max_block = (-1);
719 else {
720 memset ((void *) &cmd[0], 0, 10);
721 cmd[0] = READ_BLOCK_LIMITS;
723 SCpnt = st_do_scsi(SCpnt, STp, cmd, 6, STp->timeout, MAX_READY_RETRIES, TRUE);
725 if (!SCpnt->result && !SCpnt->sense_buffer[0]) {
726 STp->max_block = ((STp->buffer)->b_data[1] << 16) |
727 ((STp->buffer)->b_data[2] << 8) | (STp->buffer)->b_data[3];
728 STp->min_block = ((STp->buffer)->b_data[4] << 8) |
729 (STp->buffer)->b_data[5];
730 #if DEBUG
731 if (debugging)
732 printk(ST_DEB_MSG "st%d: Block limits %d - %d bytes.\n", dev, STp->min_block,
733 STp->max_block);
734 #endif
736 else {
737 STp->min_block = STp->max_block = (-1);
738 #if DEBUG
739 if (debugging)
740 printk(ST_DEB_MSG "st%d: Can't read block limits.\n", dev);
741 #endif
745 memset ((void *) &cmd[0], 0, 10);
746 cmd[0] = MODE_SENSE;
747 cmd[4] = 12;
749 SCpnt = st_do_scsi(SCpnt, STp, cmd, 12, STp->timeout, MAX_READY_RETRIES, TRUE);
751 if ((STp->buffer)->last_result_fatal != 0) {
752 #if DEBUG
753 if (debugging)
754 printk(ST_DEB_MSG "st%d: No Mode Sense.\n", dev);
755 #endif
756 STp->block_size = ST_DEFAULT_BLOCK; /* Educated guess (?) */
757 (STp->buffer)->last_result_fatal = 0; /* Prevent error propagation */
758 STp->drv_write_prot = 0;
760 else {
762 #if DEBUG
763 if (debugging)
764 printk(ST_DEB_MSG "st%d: Mode sense. Length %d, medium %x, WBS %x, BLL %d\n",
765 dev,
766 (STp->buffer)->b_data[0], (STp->buffer)->b_data[1],
767 (STp->buffer)->b_data[2], (STp->buffer)->b_data[3]);
768 #endif
770 if ((STp->buffer)->b_data[3] >= 8) {
771 STp->drv_buffer = ((STp->buffer)->b_data[2] >> 4) & 7;
772 STp->density = (STp->buffer)->b_data[4];
773 STp->block_size = (STp->buffer)->b_data[9] * 65536 +
774 (STp->buffer)->b_data[10] * 256 + (STp->buffer)->b_data[11];
775 #if DEBUG
776 if (debugging)
777 printk(ST_DEB_MSG "st%d: Density %x, tape length: %x, drv buffer: %d\n",
778 dev, STp->density, (STp->buffer)->b_data[5] * 65536 +
779 (STp->buffer)->b_data[6] * 256 + (STp->buffer)->b_data[7],
780 STp->drv_buffer);
781 #endif
784 if (STp->block_size > (STp->buffer)->buffer_size &&
785 !enlarge_buffer(STp->buffer, STp->block_size, STp->restr_dma)) {
786 printk(KERN_NOTICE "st%d: Blocksize %d too large for buffer.\n", dev,
787 STp->block_size);
788 scsi_release_command(SCpnt);
789 (STp->buffer)->in_use = 0;
790 STp->buffer = NULL;
791 if (scsi_tapes[dev].device->host->hostt->module)
792 __MOD_DEC_USE_COUNT(scsi_tapes[dev].device->host->hostt->module);
793 if(st_template.module)
794 __MOD_DEC_USE_COUNT(st_template.module);
795 return (-EIO);
797 STp->drv_write_prot = ((STp->buffer)->b_data[2] & 0x80) != 0;
799 scsi_release_command(SCpnt);
800 SCpnt = NULL;
802 if (STp->block_size > 0)
803 (STp->buffer)->buffer_blocks = (STp->buffer)->buffer_size / STp->block_size;
804 else
805 (STp->buffer)->buffer_blocks = 1;
806 (STp->buffer)->buffer_bytes = (STp->buffer)->read_pointer = 0;
808 #if DEBUG
809 if (debugging)
810 printk(ST_DEB_MSG "st%d: Block size: %d, buffer size: %d (%d blocks).\n", dev,
811 STp->block_size, (STp->buffer)->buffer_size,
812 (STp->buffer)->buffer_blocks);
813 #endif
815 if (STp->drv_write_prot) {
816 STp->write_prot = 1;
817 #if DEBUG
818 if (debugging)
819 printk(ST_DEB_MSG "st%d: Write protected\n", dev);
820 #endif
821 if ((flags & O_ACCMODE) == O_WRONLY || (flags & O_ACCMODE) == O_RDWR) {
822 (STp->buffer)->in_use = 0;
823 STp->buffer = NULL;
824 if (scsi_tapes[dev].device->host->hostt->module)
825 __MOD_DEC_USE_COUNT(scsi_tapes[dev].device->host->hostt->module);
826 if(st_template.module)
827 __MOD_DEC_USE_COUNT(st_template.module);
828 return (-EROFS);
832 if (STp->can_partitions && STp->nbr_partitions < 1) {
833 /* This code is reached when the device is opened for the first time
834 after the driver has been initialized with tape in the drive and the
835 partition support has been enabled. */
836 #if DEBUG
837 if (debugging)
838 printk(ST_DEB_MSG "st%d: Updating partition number in status.\n", dev);
839 #endif
840 if ((STp->partition = find_partition(inode)) < 0) {
841 (STp->buffer)->in_use = 0;
842 STp->buffer = NULL;
843 if (scsi_tapes[dev].device->host->hostt->module)
844 __MOD_DEC_USE_COUNT(scsi_tapes[dev].device->host->hostt->module);
845 if(st_template.module)
846 __MOD_DEC_USE_COUNT(st_template.module);
847 return STp->partition;
849 STp->new_partition = STp->partition;
850 STp->nbr_partitions = 1; /* This guess will be updated when necessary */
853 if (new_session) { /* Change the drive parameters for the new mode */
854 STp->density_changed = STp->blksize_changed = FALSE;
855 STp->compression_changed = FALSE;
856 if (!(STm->defaults_for_writes) &&
857 (i = set_mode_densblk(inode, STp, STm)) < 0) {
858 (STp->buffer)->in_use = 0;
859 STp->buffer = NULL;
860 if (scsi_tapes[dev].device->host->hostt->module)
861 __MOD_DEC_USE_COUNT(scsi_tapes[dev].device->host->hostt->module);
862 if(st_template.module)
863 __MOD_DEC_USE_COUNT(st_template.module);
864 return i;
866 if (STp->default_drvbuffer != 0xff) {
867 if (st_int_ioctl(inode, MTSETDRVBUFFER, STp->default_drvbuffer))
868 printk(KERN_WARNING "st%d: Can't set default drive buffering to %d.\n",
869 dev, STp->default_drvbuffer);
873 STp->in_use = 1;
875 return 0;
879 /* Flush the tape buffer before close */
880 static int
881 scsi_tape_flush(struct file * filp)
883 int result = 0, result2;
884 static unsigned char cmd[10];
885 Scsi_Cmnd * SCpnt;
886 Scsi_Tape * STp;
887 ST_mode * STm;
888 ST_partstat * STps;
890 struct inode *inode = filp->f_dentry->d_inode;
891 kdev_t devt = inode->i_rdev;
892 int dev;
894 if (filp->f_count > 1)
895 return 0;
897 dev = TAPE_NR(devt);
898 STp = &(scsi_tapes[dev]);
899 STm = &(STp->modes[STp->current_mode]);
900 STps = &(STp->ps[STp->partition]);
902 if (STp->can_partitions &&
903 (result = update_partition(inode)) < 0) {
904 #if DEBUG
905 if (debugging)
906 printk(ST_DEB_MSG "st%d: update_partition at close failed.\n", dev);
907 #endif
908 goto out;
911 if ( STps->rw == ST_WRITING && !(STp->device)->was_reset) {
913 result = flush_write_buffer(STp);
915 #if DEBUG
916 if (debugging) {
917 printk(ST_DEB_MSG "st%d: File length %ld bytes.\n",
918 dev, (long)(filp->f_pos));
919 printk(ST_DEB_MSG "st%d: Async write waits %d, finished %d.\n",
920 dev, STp->nbr_waits, STp->nbr_finished);
922 #endif
924 if (result == 0 || result == (-ENOSPC)) {
926 memset(cmd, 0, 10);
927 cmd[0] = WRITE_FILEMARKS;
928 cmd[4] = 1 + STp->two_fm;
930 SCpnt = st_do_scsi(NULL, STp, cmd, 0, STp->timeout, MAX_WRITE_RETRIES,
931 TRUE);
932 if (!SCpnt)
933 goto out;
935 if ((STp->buffer)->last_result_fatal != 0 &&
936 ((SCpnt->sense_buffer[0] & 0x70) != 0x70 ||
937 (SCpnt->sense_buffer[2] & 0x4f) != 0x40 ||
938 ((SCpnt->sense_buffer[0] & 0x80) != 0 &&
939 (SCpnt->sense_buffer[3] | SCpnt->sense_buffer[4] |
940 SCpnt->sense_buffer[5] |
941 SCpnt->sense_buffer[6]) == 0))) {
942 /* Filter out successful write at EOM */
943 scsi_release_command(SCpnt);
944 SCpnt = NULL;
945 printk(KERN_ERR "st%d: Error on write filemark.\n", dev);
946 if (result == 0)
947 result = (-EIO);
949 else {
950 scsi_release_command(SCpnt);
951 SCpnt = NULL;
952 if (STps->drv_file >= 0)
953 STps->drv_file++ ;
954 STps->drv_block = 0;
955 if (STp->two_fm)
956 cross_eof(STp, FALSE);
957 STps->eof = ST_FM;
961 #if DEBUG
962 if (debugging)
963 printk(ST_DEB_MSG "st%d: Buffer flushed, %d EOF(s) written\n",
964 dev, cmd[4]);
965 #endif
967 else if (!STp->rew_at_close) {
968 STps = &(STp->ps[STp->partition]);
969 if (!STm->sysv || STps->rw != ST_READING) {
970 if (STp->can_bsr)
971 result = flush_buffer(inode, filp, 0);
972 else if (STps->eof == ST_FM_HIT) {
973 result = cross_eof(STp, FALSE);
974 if (result) {
975 if (STps->drv_file >= 0)
976 STps->drv_file++;
977 STps->drv_block = 0;
978 STps->eof = ST_FM;
980 else
981 STps->eof = ST_NOEOF;
984 else if ((STps->eof == ST_NOEOF &&
985 !(result = cross_eof(STp, TRUE))) ||
986 STps->eof == ST_FM_HIT) {
987 if (STps->drv_file >= 0)
988 STps->drv_file++;
989 STps->drv_block = 0;
990 STps->eof = ST_FM;
994 out:
995 if (STp->rew_at_close) {
996 result2 = st_int_ioctl(inode, MTREW, 1);
997 if (result == 0)
998 result = result2;
1001 return result;
1005 /* Close the device and release it */
1006 static int
1007 scsi_tape_close(struct inode * inode, struct file * filp)
1009 int result = 0;
1010 Scsi_Tape * STp;
1012 kdev_t devt = inode->i_rdev;
1013 int dev;
1015 dev = TAPE_NR(devt);
1016 STp = &(scsi_tapes[dev]);
1018 if (STp->door_locked == ST_LOCKED_AUTO)
1019 st_int_ioctl(inode, MTUNLOCK, 0);
1021 if (STp->buffer != NULL) {
1022 normalize_buffer(STp->buffer);
1023 (STp->buffer)->in_use = 0;
1026 STp->in_use = 0;
1027 if (scsi_tapes[dev].device->host->hostt->module)
1028 __MOD_DEC_USE_COUNT(scsi_tapes[dev].device->host->hostt->module);
1029 if(st_template.module)
1030 __MOD_DEC_USE_COUNT(st_template.module);
1032 return result;
1036 /* Write command */
1037 static ssize_t
1038 st_write(struct file * filp, const char * buf, size_t count, loff_t *ppos)
1040 struct inode *inode = filp->f_dentry->d_inode;
1041 ssize_t total;
1042 ssize_t i, do_count, blks, retval, transfer;
1043 int write_threshold;
1044 int doing_write = 0;
1045 static unsigned char cmd[10];
1046 const char *b_point;
1047 Scsi_Cmnd * SCpnt = NULL;
1048 Scsi_Tape * STp;
1049 ST_mode * STm;
1050 ST_partstat * STps;
1051 int dev = TAPE_NR(inode->i_rdev);
1053 STp = &(scsi_tapes[dev]);
1056 * If we are in the middle of error recovery, don't let anyone
1057 * else try and use this device. Also, if error recovery fails, it
1058 * may try and take the device offline, in which case all further
1059 * access to the device is prohibited.
1061 if( !scsi_block_when_processing_errors(STp->device) ) {
1062 return -ENXIO;
1065 if (ppos != &filp->f_pos) {
1066 /* "A request was outside the capabilities of the device." */
1067 return -ENXIO;
1070 if (STp->ready != ST_READY) {
1071 if (STp->ready == ST_NO_TAPE)
1072 return (-ENOMEDIUM);
1073 else
1074 return (-EIO);
1076 STm = &(STp->modes[STp->current_mode]);
1077 if (!STm->defined)
1078 return (-ENXIO);
1079 if (count == 0)
1080 return 0;
1083 * If there was a bus reset, block further access
1084 * to this device.
1086 if( STp->device->was_reset )
1087 return (-EIO);
1089 #if DEBUG
1090 if (!STp->in_use) {
1091 printk(ST_DEB_MSG "st%d: Incorrect device.\n", dev);
1092 return (-EIO);
1094 #endif
1096 /* Write must be integral number of blocks */
1097 if (STp->block_size != 0 && (count % STp->block_size) != 0) {
1098 printk(KERN_WARNING "st%d: Write not multiple of tape block size.\n",
1099 dev);
1100 return (-EIO);
1103 if (STp->can_partitions &&
1104 (retval = update_partition(inode)) < 0)
1105 return retval;
1106 STps = &(STp->ps[STp->partition]);
1108 if (STp->write_prot)
1109 return (-EACCES);
1111 if (STp->block_size == 0 &&
1112 count > (STp->buffer)->buffer_size &&
1113 !enlarge_buffer(STp->buffer, count, STp->restr_dma))
1114 return (-EOVERFLOW);
1116 if (STp->do_auto_lock && STp->door_locked == ST_UNLOCKED &&
1117 !st_int_ioctl(inode, MTLOCK, 0))
1118 STp->door_locked = ST_LOCKED_AUTO;
1120 if (STps->rw == ST_READING) {
1121 retval = flush_buffer(inode, filp, 0);
1122 if (retval)
1123 return retval;
1124 STps->rw = ST_WRITING;
1126 else if (STps->rw != ST_WRITING &&
1127 STps->drv_file == 0 && STps->drv_block == 0) {
1128 if ((retval = set_mode_densblk(inode, STp, STm)) < 0)
1129 return retval;
1130 if (STm->default_compression != ST_DONT_TOUCH &&
1131 !(STp->compression_changed)) {
1132 if (st_compression(STp, (STm->default_compression == ST_YES))) {
1133 printk(KERN_WARNING "st%d: Can't set default compression.\n",
1134 dev);
1135 if (modes_defined)
1136 return (-EINVAL);
1141 if ((STp->buffer)->writing) {
1142 write_behind_check(STp);
1143 if ((STp->buffer)->last_result_fatal) {
1144 #if DEBUG
1145 if (debugging)
1146 printk(ST_DEB_MSG "st%d: Async write error (write) %x.\n", dev,
1147 (STp->buffer)->last_result);
1148 #endif
1149 if ((STp->buffer)->last_result == INT_MAX)
1150 STps->eof = ST_EOM_OK;
1151 else
1152 STps->eof = ST_EOM_ERROR;
1155 if (STps->eof == ST_EOM_OK)
1156 return (-ENOSPC);
1157 else if (STps->eof == ST_EOM_ERROR)
1158 return (-EIO);
1160 /* Check the buffer readability in cases where copy_user might catch
1161 the problems after some tape movement. */
1162 if (STp->block_size != 0 &&
1163 (copy_from_user(&i, buf, 1) != 0 ||
1164 copy_from_user(&i, buf + count - 1, 1) != 0))
1165 return (-EFAULT);
1167 if (!STm->do_buffer_writes) {
1168 #if 0
1169 if (STp->block_size != 0 && (count % STp->block_size) != 0)
1170 return (-EIO); /* Write must be integral number of blocks */
1171 #endif
1172 write_threshold = 1;
1174 else
1175 write_threshold = (STp->buffer)->buffer_blocks * STp->block_size;
1176 if (!STm->do_async_writes)
1177 write_threshold--;
1179 total = count;
1181 memset(cmd, 0, 10);
1182 cmd[0] = WRITE_6;
1183 cmd[1] = (STp->block_size != 0);
1185 STps->rw = ST_WRITING;
1187 b_point = buf;
1188 while ((STp->block_size == 0 && !STm->do_async_writes && count > 0) ||
1189 (STp->block_size != 0 &&
1190 (STp->buffer)->buffer_bytes + count > write_threshold))
1192 doing_write = 1;
1193 if (STp->block_size == 0)
1194 do_count = count;
1195 else {
1196 do_count = (STp->buffer)->buffer_blocks * STp->block_size -
1197 (STp->buffer)->buffer_bytes;
1198 if (do_count > count)
1199 do_count = count;
1202 i = append_to_buffer(b_point, STp->buffer, do_count);
1203 if (i) {
1204 if (SCpnt != NULL) {
1205 scsi_release_command(SCpnt);
1206 SCpnt = NULL;
1208 return i;
1211 if (STp->block_size == 0)
1212 blks = transfer = do_count;
1213 else {
1214 blks = (STp->buffer)->buffer_bytes /
1215 STp->block_size;
1216 transfer = blks * STp->block_size;
1218 cmd[2] = blks >> 16;
1219 cmd[3] = blks >> 8;
1220 cmd[4] = blks;
1222 SCpnt = st_do_scsi(SCpnt, STp, cmd, transfer, STp->timeout,
1223 MAX_WRITE_RETRIES, TRUE);
1224 if (!SCpnt)
1225 return (-EBUSY);
1227 if ((STp->buffer)->last_result_fatal != 0) {
1228 #if DEBUG
1229 if (debugging)
1230 printk(ST_DEB_MSG "st%d: Error on write:\n", dev);
1231 #endif
1232 if ((SCpnt->sense_buffer[0] & 0x70) == 0x70 &&
1233 (SCpnt->sense_buffer[2] & 0x40)) {
1234 if (STp->block_size != 0 && (SCpnt->sense_buffer[0] & 0x80) != 0)
1235 transfer = (SCpnt->sense_buffer[3] << 24) |
1236 (SCpnt->sense_buffer[4] << 16) |
1237 (SCpnt->sense_buffer[5] << 8) | SCpnt->sense_buffer[6];
1238 else if (STp->block_size == 0 &&
1239 (SCpnt->sense_buffer[2] & 0x0f) == VOLUME_OVERFLOW)
1240 transfer = do_count;
1241 else
1242 transfer = 0;
1243 if (STp->block_size != 0)
1244 transfer *= STp->block_size;
1245 if (transfer <= do_count) {
1246 filp->f_pos += do_count - transfer;
1247 count -= do_count - transfer;
1248 if (STps->drv_block >= 0) {
1249 if (STp->block_size == 0 && transfer < do_count)
1250 STps->drv_block++;
1251 else if (STp->block_size != 0)
1252 STps->drv_block += (do_count - transfer) / STp->block_size;
1254 STps->eof = ST_EOM_OK;
1255 retval = (-ENOSPC); /* EOM within current request */
1256 #if DEBUG
1257 if (debugging)
1258 printk(ST_DEB_MSG "st%d: EOM with %d bytes unwritten.\n",
1259 dev, transfer);
1260 #endif
1262 else {
1263 STps->eof = ST_EOM_ERROR;
1264 STps->drv_block = (-1); /* Too cautious? */
1265 retval = (-EIO); /* EOM for old data */
1266 #if DEBUG
1267 if (debugging)
1268 printk(ST_DEB_MSG "st%d: EOM with lost data.\n", dev);
1269 #endif
1272 else {
1273 STps->drv_block = (-1); /* Too cautious? */
1274 retval = (-EIO);
1277 scsi_release_command(SCpnt);
1278 SCpnt = NULL;
1279 (STp->buffer)->buffer_bytes = 0;
1280 STp->dirty = 0;
1281 if (count < total)
1282 return total - count;
1283 else
1284 return retval;
1286 filp->f_pos += do_count;
1287 b_point += do_count;
1288 count -= do_count;
1289 if (STps->drv_block >= 0) {
1290 if (STp->block_size == 0)
1291 STps->drv_block++;
1292 else
1293 STps->drv_block += blks;
1295 (STp->buffer)->buffer_bytes = 0;
1296 STp->dirty = 0;
1298 if (count != 0) {
1299 STp->dirty = 1;
1300 i = append_to_buffer(b_point, STp->buffer, count);
1301 if (i) {
1302 if (SCpnt != NULL) {
1303 scsi_release_command(SCpnt);
1304 SCpnt = NULL;
1306 return i;
1308 filp->f_pos += count;
1309 count = 0;
1312 if (doing_write && (STp->buffer)->last_result_fatal != 0) {
1313 scsi_release_command(SCpnt);
1314 SCpnt = NULL;
1315 return (STp->buffer)->last_result_fatal;
1318 if (STm->do_async_writes &&
1319 (((STp->buffer)->buffer_bytes >= STp->write_threshold &&
1320 (STp->buffer)->buffer_bytes >= STp->block_size) ||
1321 STp->block_size == 0) ) {
1322 /* Schedule an asynchronous write */
1323 if (STp->block_size == 0)
1324 (STp->buffer)->writing = (STp->buffer)->buffer_bytes;
1325 else
1326 (STp->buffer)->writing = ((STp->buffer)->buffer_bytes /
1327 STp->block_size) * STp->block_size;
1328 STp->dirty = !((STp->buffer)->writing ==
1329 (STp->buffer)->buffer_bytes);
1331 if (STp->block_size == 0)
1332 blks = (STp->buffer)->writing;
1333 else
1334 blks = (STp->buffer)->writing / STp->block_size;
1335 cmd[2] = blks >> 16;
1336 cmd[3] = blks >> 8;
1337 cmd[4] = blks;
1338 #if DEBUG
1339 STp->write_pending = 1;
1340 #endif
1342 SCpnt = st_do_scsi(SCpnt, STp, cmd, (STp->buffer)->writing, STp->timeout,
1343 MAX_WRITE_RETRIES, FALSE);
1344 if (SCpnt == NULL)
1345 return (-EIO);
1347 else if (SCpnt != NULL) {
1348 scsi_release_command(SCpnt);
1349 SCpnt = NULL;
1351 STps->at_sm &= (total == 0);
1352 if (total > 0)
1353 STps->eof = ST_NOEOF;
1355 return( total);
1358 /* Read data from the tape. Returns zero in the normal case, one if the
1359 eof status has changed, and the negative error code in case of a
1360 fatal error. Otherwise updates the buffer and the eof state. */
1361 static long
1362 read_tape(struct inode *inode, long count, Scsi_Cmnd **aSCpnt)
1364 int transfer, blks, bytes;
1365 static unsigned char cmd[10];
1366 Scsi_Cmnd *SCpnt;
1367 Scsi_Tape *STp;
1368 ST_mode * STm;
1369 ST_partstat * STps;
1370 int dev = TAPE_NR(inode->i_rdev);
1371 int retval = 0;
1373 if (count == 0)
1374 return 0;
1376 STp = &(scsi_tapes[dev]);
1377 STm = &(STp->modes[STp->current_mode]);
1378 STps = &(STp->ps[STp->partition]);
1379 if (STps->eof == ST_FM_HIT)
1380 return 1;
1382 memset(cmd, 0, 10);
1383 cmd[0] = READ_6;
1384 cmd[1] = (STp->block_size != 0);
1385 if (STp->block_size == 0)
1386 blks = bytes = count;
1387 else {
1388 if (STm->do_read_ahead) {
1389 blks = (STp->buffer)->buffer_blocks;
1390 bytes = blks * STp->block_size;
1392 else {
1393 bytes = count;
1394 if (bytes > (STp->buffer)->buffer_size)
1395 bytes = (STp->buffer)->buffer_size;
1396 blks = bytes / STp->block_size;
1397 bytes = blks * STp->block_size;
1400 cmd[2] = blks >> 16;
1401 cmd[3] = blks >> 8;
1402 cmd[4] = blks;
1404 SCpnt = *aSCpnt;
1405 SCpnt = st_do_scsi(SCpnt, STp, cmd, bytes, STp->timeout, MAX_RETRIES, TRUE);
1406 *aSCpnt = SCpnt;
1407 if (!SCpnt)
1408 return (-EBUSY);
1410 (STp->buffer)->read_pointer = 0;
1411 STps->at_sm = 0;
1413 /* Something to check */
1414 if ((STp->buffer)->last_result_fatal) {
1415 retval = 1;
1416 #if DEBUG
1417 if (debugging)
1418 printk(ST_DEB_MSG "st%d: Sense: %2x %2x %2x %2x %2x %2x %2x %2x\n",
1419 dev,
1420 SCpnt->sense_buffer[0], SCpnt->sense_buffer[1],
1421 SCpnt->sense_buffer[2], SCpnt->sense_buffer[3],
1422 SCpnt->sense_buffer[4], SCpnt->sense_buffer[5],
1423 SCpnt->sense_buffer[6], SCpnt->sense_buffer[7]);
1424 #endif
1425 if ((SCpnt->sense_buffer[0] & 0x70) == 0x70) { /* extended sense */
1427 if ((SCpnt->sense_buffer[2] & 0x0f) == BLANK_CHECK)
1428 SCpnt->sense_buffer[2] &= 0xcf; /* No need for EOM in this case */
1430 if ((SCpnt->sense_buffer[2] & 0xe0) != 0) { /* EOF, EOM, or ILI */
1431 /* Compute the residual count */
1432 if ((SCpnt->sense_buffer[0] & 0x80) != 0)
1433 transfer = (SCpnt->sense_buffer[3] << 24) |
1434 (SCpnt->sense_buffer[4] << 16) |
1435 (SCpnt->sense_buffer[5] << 8) | SCpnt->sense_buffer[6];
1436 else
1437 transfer = 0;
1438 if (STp->block_size == 0 &&
1439 (SCpnt->sense_buffer[2] & 0x0f) == MEDIUM_ERROR)
1440 transfer = bytes;
1442 if (SCpnt->sense_buffer[2] & 0x20) { /* ILI */
1443 if (STp->block_size == 0) {
1444 if (transfer <= 0)
1445 transfer = 0;
1446 (STp->buffer)->buffer_bytes = bytes - transfer;
1448 else {
1449 scsi_release_command(SCpnt);
1450 SCpnt = *aSCpnt = NULL;
1451 if (transfer == blks) { /* We did not get anything, error */
1452 printk(KERN_NOTICE "st%d: Incorrect block size.\n", dev);
1453 if (STps->drv_block >= 0)
1454 STps->drv_block += blks - transfer + 1;
1455 st_int_ioctl(inode, MTBSR, 1);
1456 return (-EIO);
1458 /* We have some data, deliver it */
1459 (STp->buffer)->buffer_bytes = (blks - transfer) *
1460 STp->block_size;
1461 #if DEBUG
1462 if (debugging)
1463 printk(ST_DEB_MSG
1464 "st%d: ILI but enough data received %ld %d.\n",
1465 dev, count, (STp->buffer)->buffer_bytes);
1466 #endif
1467 if (STps->drv_block >= 0)
1468 STps->drv_block += 1;
1469 if (st_int_ioctl(inode, MTBSR, 1))
1470 return (-EIO);
1473 else if (SCpnt->sense_buffer[2] & 0x80) { /* FM overrides EOM */
1474 if (STps->eof != ST_FM_HIT)
1475 STps->eof = ST_FM_HIT;
1476 else
1477 STps->eof = ST_EOD_2;
1478 if (STp->block_size == 0)
1479 (STp->buffer)->buffer_bytes = 0;
1480 else
1481 (STp->buffer)->buffer_bytes =
1482 bytes - transfer * STp->block_size;
1483 #if DEBUG
1484 if (debugging)
1485 printk(ST_DEB_MSG
1486 "st%d: EOF detected (%d bytes read).\n",
1487 dev, (STp->buffer)->buffer_bytes);
1488 #endif
1490 else if (SCpnt->sense_buffer[2] & 0x40) {
1491 if (STps->eof == ST_FM)
1492 STps->eof = ST_EOD_1;
1493 else
1494 STps->eof = ST_EOM_OK;
1495 if (STp->block_size == 0)
1496 (STp->buffer)->buffer_bytes = bytes - transfer;
1497 else
1498 (STp->buffer)->buffer_bytes =
1499 bytes - transfer * STp->block_size;
1500 #if DEBUG
1501 if (debugging)
1502 printk(ST_DEB_MSG "st%d: EOM detected (%d bytes read).\n",
1503 dev, (STp->buffer)->buffer_bytes);
1504 #endif
1506 } /* end of EOF, EOM, ILI test */
1507 else { /* nonzero sense key */
1508 #if DEBUG
1509 if (debugging)
1510 printk(ST_DEB_MSG "st%d: Tape error while reading.\n", dev);
1511 #endif
1512 STps->drv_block = (-1);
1513 if (STps->eof == ST_FM &&
1514 (SCpnt->sense_buffer[2] & 0x0f) == BLANK_CHECK) {
1515 #if DEBUG
1516 if (debugging)
1517 printk(ST_DEB_MSG
1518 "st%d: Zero returned for first BLANK CHECK after EOF.\n",
1519 dev);
1520 #endif
1521 STps->eof = ST_EOD_2; /* First BLANK_CHECK after FM */
1523 else /* Some other extended sense code */
1524 retval = (-EIO);
1526 } /* End of extended sense test */
1527 else { /* Non-extended sense */
1528 retval = (STp->buffer)->last_result_fatal;
1531 } /* End of error handling */
1532 else /* Read successful */
1533 (STp->buffer)->buffer_bytes = bytes;
1535 if (STps->drv_block >= 0) {
1536 if (STp->block_size == 0)
1537 STps->drv_block++;
1538 else
1539 STps->drv_block += (STp->buffer)->buffer_bytes / STp->block_size;
1542 return retval;
1546 /* Read command */
1547 static ssize_t
1548 st_read(struct file * filp, char * buf, size_t count, loff_t *ppos)
1550 struct inode * inode = filp->f_dentry->d_inode;
1551 ssize_t total;
1552 ssize_t i, transfer;
1553 int special;
1554 Scsi_Cmnd * SCpnt = NULL;
1555 Scsi_Tape * STp;
1556 ST_mode * STm;
1557 ST_partstat * STps;
1558 int dev = TAPE_NR(inode->i_rdev);
1560 STp = &(scsi_tapes[dev]);
1563 * If we are in the middle of error recovery, don't let anyone
1564 * else try and use this device. Also, if error recovery fails, it
1565 * may try and take the device offline, in which case all further
1566 * access to the device is prohibited.
1568 if( !scsi_block_when_processing_errors(STp->device) ) {
1569 return -ENXIO;
1572 if (ppos != &filp->f_pos) {
1573 /* "A request was outside the capabilities of the device." */
1574 return -ENXIO;
1577 if (STp->ready != ST_READY) {
1578 if (STp->ready == ST_NO_TAPE)
1579 return (-ENOMEDIUM);
1580 else
1581 return (-EIO);
1583 STm = &(STp->modes[STp->current_mode]);
1584 if (!STm->defined)
1585 return (-ENXIO);
1586 #if DEBUG
1587 if (!STp->in_use) {
1588 printk(ST_DEB_MSG "st%d: Incorrect device.\n", dev);
1589 return (-EIO);
1591 #endif
1593 if (STp->can_partitions &&
1594 (total = update_partition(inode)) < 0)
1595 return total;
1597 if (STp->block_size == 0 &&
1598 count > (STp->buffer)->buffer_size &&
1599 !enlarge_buffer(STp->buffer, count, STp->restr_dma))
1600 return (-EOVERFLOW);
1602 if (!(STm->do_read_ahead) && STp->block_size != 0 &&
1603 (count % STp->block_size) != 0)
1604 return (-EIO); /* Read must be integral number of blocks */
1606 if (STp->do_auto_lock && STp->door_locked == ST_UNLOCKED &&
1607 !st_int_ioctl(inode, MTLOCK, 0))
1608 STp->door_locked = ST_LOCKED_AUTO;
1610 STps = &(STp->ps[STp->partition]);
1611 if (STps->rw == ST_WRITING) {
1612 transfer = flush_buffer(inode, filp, 0);
1613 if (transfer)
1614 return transfer;
1615 STps->rw = ST_READING;
1618 #if DEBUG
1619 if (debugging && STps->eof != ST_NOEOF)
1620 printk(ST_DEB_MSG "st%d: EOF/EOM flag up (%d). Bytes %d\n", dev,
1621 STps->eof, (STp->buffer)->buffer_bytes);
1622 #endif
1623 if ((STp->buffer)->buffer_bytes == 0 &&
1624 STps->eof >= ST_EOD_1) {
1625 if (STps->eof < ST_EOD) {
1626 STps->eof += 1;
1627 return 0;
1629 return (-EIO); /* EOM or Blank Check */
1632 /* Check the buffer writability before any tape movement. Don't alter
1633 buffer data. */
1634 if (copy_from_user(&i, buf, 1) != 0 ||
1635 copy_to_user(buf, &i, 1) != 0 ||
1636 copy_from_user(&i, buf + count - 1, 1) != 0 ||
1637 copy_to_user(buf + count - 1, &i, 1) != 0)
1638 return (-EFAULT);
1640 STps->rw = ST_READING;
1643 /* Loop until enough data in buffer or a special condition found */
1644 for (total = 0, special = 0; total < count && !special; ) {
1646 /* Get new data if the buffer is empty */
1647 if ((STp->buffer)->buffer_bytes == 0) {
1648 special = read_tape(inode, count - total, &SCpnt);
1649 if (special < 0) { /* No need to continue read */
1650 if (SCpnt != NULL) {
1651 scsi_release_command(SCpnt);
1653 return special;
1657 /* Move the data from driver buffer to user buffer */
1658 if ((STp->buffer)->buffer_bytes > 0) {
1659 #if DEBUG
1660 if (debugging && STps->eof != ST_NOEOF)
1661 printk(ST_DEB_MSG "st%d: EOF up (%d). Left %d, needed %d.\n", dev,
1662 STps->eof, (STp->buffer)->buffer_bytes, count - total);
1663 #endif
1664 transfer = (STp->buffer)->buffer_bytes < count - total ?
1665 (STp->buffer)->buffer_bytes : count - total;
1666 i = from_buffer(STp->buffer, buf, transfer);
1667 if (i) {
1668 if (SCpnt != NULL) {
1669 scsi_release_command(SCpnt);
1670 SCpnt = NULL;
1672 return i;
1674 filp->f_pos += transfer;
1675 buf += transfer;
1676 total += transfer;
1679 if (STp->block_size == 0)
1680 break; /* Read only one variable length block */
1682 } /* for (total = 0, special = 0; total < count && !special; ) */
1684 if (SCpnt != NULL) {
1685 scsi_release_command(SCpnt);
1686 SCpnt = NULL;
1689 /* Change the eof state if no data from tape or buffer */
1690 if (total == 0) {
1691 if (STps->eof == ST_FM_HIT) {
1692 STps->eof = ST_FM;
1693 STps->drv_block = 0;
1694 if (STps->drv_file >= 0)
1695 STps->drv_file++;
1697 else if (STps->eof == ST_EOD_1) {
1698 STps->eof = ST_EOD_2;
1699 STps->drv_block = 0;
1700 if (STps->drv_file >= 0)
1701 STps->drv_file++;
1703 else if (STps->eof == ST_EOD_2)
1704 STps->eof = ST_EOD;
1706 else if (STps->eof == ST_FM)
1707 STps->eof = ST_NOEOF;
1709 return total;
1714 /* Set the driver options */
1715 static void
1716 st_log_options(Scsi_Tape *STp, ST_mode *STm, int dev)
1718 printk(KERN_INFO
1719 "st%d: Mode %d options: buffer writes: %d, async writes: %d, read ahead: %d\n",
1720 dev, STp->current_mode, STm->do_buffer_writes, STm->do_async_writes,
1721 STm->do_read_ahead);
1722 printk(KERN_INFO
1723 "st%d: can bsr: %d, two FMs: %d, fast mteom: %d, auto lock: %d,\n",
1724 dev, STp->can_bsr, STp->two_fm, STp->fast_mteom, STp->do_auto_lock);
1725 printk(KERN_INFO
1726 "st%d: defs for wr: %d, no block limits: %d, partitions: %d, s2 log: %d\n",
1727 dev, STm->defaults_for_writes, STp->omit_blklims, STp->can_partitions,
1728 STp->scsi2_logical);
1729 printk(KERN_INFO
1730 "st%d: sysv: %d\n", dev, STm->sysv);
1731 #if DEBUG
1732 printk(KERN_INFO
1733 "st%d: debugging: %d\n",
1734 dev, debugging);
1735 #endif
1739 static int
1740 st_set_options(struct inode * inode, long options)
1742 int value;
1743 long code;
1744 Scsi_Tape *STp;
1745 ST_mode *STm;
1746 int dev = TAPE_NR(inode->i_rdev);
1748 STp = &(scsi_tapes[dev]);
1749 STm = &(STp->modes[STp->current_mode]);
1750 if (!STm->defined) {
1751 memcpy(STm, &(STp->modes[0]), sizeof(ST_mode));
1752 modes_defined = TRUE;
1753 #if DEBUG
1754 if (debugging)
1755 printk(ST_DEB_MSG "st%d: Initialized mode %d definition from mode 0\n",
1756 dev, STp->current_mode);
1757 #endif
1760 code = options & MT_ST_OPTIONS;
1761 if (code == MT_ST_BOOLEANS) {
1762 STm->do_buffer_writes = (options & MT_ST_BUFFER_WRITES) != 0;
1763 STm->do_async_writes = (options & MT_ST_ASYNC_WRITES) != 0;
1764 STm->defaults_for_writes = (options & MT_ST_DEF_WRITES) != 0;
1765 STm->do_read_ahead = (options & MT_ST_READ_AHEAD) != 0;
1766 STp->two_fm = (options & MT_ST_TWO_FM) != 0;
1767 STp->fast_mteom = (options & MT_ST_FAST_MTEOM) != 0;
1768 STp->do_auto_lock = (options & MT_ST_AUTO_LOCK) != 0;
1769 STp->can_bsr = (options & MT_ST_CAN_BSR) != 0;
1770 STp->omit_blklims = (options & MT_ST_NO_BLKLIMS) != 0;
1771 if ((STp->device)->scsi_level >= SCSI_2)
1772 STp->can_partitions = (options & MT_ST_CAN_PARTITIONS) != 0;
1773 STp->scsi2_logical = (options & MT_ST_SCSI2LOGICAL) != 0;
1774 STm->sysv = (options & MT_ST_SYSV) != 0;
1775 #if DEBUG
1776 debugging = (options & MT_ST_DEBUGGING) != 0;
1777 #endif
1778 st_log_options(STp, STm, dev);
1780 else if (code == MT_ST_SETBOOLEANS || code == MT_ST_CLEARBOOLEANS) {
1781 value = (code == MT_ST_SETBOOLEANS);
1782 if ((options & MT_ST_BUFFER_WRITES) != 0)
1783 STm->do_buffer_writes = value;
1784 if ((options & MT_ST_ASYNC_WRITES) != 0)
1785 STm->do_async_writes = value;
1786 if ((options & MT_ST_DEF_WRITES) != 0)
1787 STm->defaults_for_writes = value;
1788 if ((options & MT_ST_READ_AHEAD) != 0)
1789 STm->do_read_ahead = value;
1790 if ((options & MT_ST_TWO_FM) != 0)
1791 STp->two_fm = value;
1792 if ((options & MT_ST_FAST_MTEOM) != 0)
1793 STp->fast_mteom = value;
1794 if ((options & MT_ST_AUTO_LOCK) != 0)
1795 STp->do_auto_lock = value;
1796 if ((options & MT_ST_CAN_BSR) != 0)
1797 STp->can_bsr = value;
1798 if ((options & MT_ST_NO_BLKLIMS) != 0)
1799 STp->omit_blklims = value;
1800 if ((STp->device)->scsi_level >= SCSI_2 &&
1801 (options & MT_ST_CAN_PARTITIONS) != 0)
1802 STp->can_partitions = value;
1803 if ((options & MT_ST_SCSI2LOGICAL) != 0)
1804 STp->scsi2_logical = value;
1805 if ((options & MT_ST_SYSV) != 0)
1806 STm->sysv = value;
1807 #if DEBUG
1808 if ((options & MT_ST_DEBUGGING) != 0)
1809 debugging = value;
1810 #endif
1811 st_log_options(STp, STm, dev);
1813 else if (code == MT_ST_WRITE_THRESHOLD) {
1814 value = (options & ~MT_ST_OPTIONS) * ST_KILOBYTE;
1815 if (value < 1 || value > st_buffer_size) {
1816 printk(KERN_WARNING "st%d: Write threshold %d too small or too large.\n",
1817 dev, value);
1818 return (-EIO);
1820 STp->write_threshold = value;
1821 printk(KERN_INFO "st%d: Write threshold set to %d bytes.\n",
1822 dev, value);
1824 else if (code == MT_ST_DEF_BLKSIZE) {
1825 value = (options & ~MT_ST_OPTIONS);
1826 if (value == ~MT_ST_OPTIONS) {
1827 STm->default_blksize = (-1);
1828 printk(KERN_INFO "st%d: Default block size disabled.\n", dev);
1830 else {
1831 STm->default_blksize = value;
1832 printk(KERN_INFO "st%d: Default block size set to %d bytes.\n",
1833 dev, STm->default_blksize);
1836 else if (code == MT_ST_TIMEOUTS) {
1837 value = (options & ~MT_ST_OPTIONS);
1838 if ((value & MT_ST_SET_LONG_TIMEOUT) != 0) {
1839 STp->long_timeout = (value & ~MT_ST_SET_LONG_TIMEOUT) * HZ;
1840 printk(KERN_INFO "st%d: Long timeout set to %d seconds.\n", dev,
1841 (value & ~MT_ST_SET_LONG_TIMEOUT));
1843 else {
1844 STp->timeout = value * HZ;
1845 printk(KERN_INFO "st%d: Normal timeout set to %d seconds.\n", dev,
1846 value);
1849 else if (code == MT_ST_DEF_OPTIONS) {
1850 code = (options & ~MT_ST_CLEAR_DEFAULT);
1851 value = (options & MT_ST_CLEAR_DEFAULT);
1852 if (code == MT_ST_DEF_DENSITY) {
1853 if (value == MT_ST_CLEAR_DEFAULT) {
1854 STm->default_density = (-1);
1855 printk(KERN_INFO "st%d: Density default disabled.\n", dev);
1857 else {
1858 STm->default_density = value & 0xff;
1859 printk(KERN_INFO "st%d: Density default set to %x\n",
1860 dev, STm->default_density);
1863 else if (code == MT_ST_DEF_DRVBUFFER) {
1864 if (value == MT_ST_CLEAR_DEFAULT) {
1865 STp->default_drvbuffer = 0xff;
1866 printk(KERN_INFO "st%d: Drive buffer default disabled.\n", dev);
1868 else {
1869 STp->default_drvbuffer = value & 7;
1870 printk(KERN_INFO "st%d: Drive buffer default set to %x\n",
1871 dev, STp->default_drvbuffer);
1874 else if (code == MT_ST_DEF_COMPRESSION) {
1875 if (value == MT_ST_CLEAR_DEFAULT) {
1876 STm->default_compression = ST_DONT_TOUCH;
1877 printk(KERN_INFO "st%d: Compression default disabled.\n", dev);
1879 else {
1880 STm->default_compression = (value & 1 ? ST_YES : ST_NO);
1881 printk(KERN_INFO "st%d: Compression default set to %x\n",
1882 dev, (value & 1));
1886 else
1887 return (-EIO);
1889 return 0;
1893 #define COMPRESSION_PAGE 0x0f
1894 #define COMPRESSION_PAGE_LENGTH 16
1896 #define MODE_HEADER_LENGTH 4
1898 #define DCE_MASK 0x80
1899 #define DCC_MASK 0x40
1900 #define RED_MASK 0x60
1903 /* Control the compression with mode page 15. Algorithm not changed if zero. */
1904 static int
1905 st_compression(Scsi_Tape * STp, int state)
1907 int dev;
1908 unsigned char cmd[10];
1909 Scsi_Cmnd * SCpnt = NULL;
1911 if (STp->ready != ST_READY)
1912 return (-EIO);
1914 /* Read the current page contents */
1915 memset(cmd, 0, 10);
1916 cmd[0] = MODE_SENSE;
1917 cmd[1] = 8;
1918 cmd[2] = COMPRESSION_PAGE;
1919 cmd[4] = COMPRESSION_PAGE_LENGTH + MODE_HEADER_LENGTH;
1921 SCpnt = st_do_scsi(SCpnt, STp, cmd, cmd[4], STp->timeout, 0, TRUE);
1922 if (SCpnt == NULL)
1923 return (-EBUSY);
1924 dev = TAPE_NR(SCpnt->request.rq_dev);
1926 if ((STp->buffer)->last_result_fatal != 0) {
1927 #if DEBUG
1928 if (debugging)
1929 printk(ST_DEB_MSG "st%d: Compression mode page not supported.\n", dev);
1930 #endif
1931 scsi_release_command(SCpnt);
1932 SCpnt = NULL;
1933 return (-EIO);
1935 #if DEBUG
1936 if (debugging)
1937 printk(ST_DEB_MSG "st%d: Compression state is %d.\n", dev,
1938 ((STp->buffer)->b_data[MODE_HEADER_LENGTH + 2] & DCE_MASK ? 1 : 0));
1939 #endif
1941 /* Check if compression can be changed */
1942 if (((STp->buffer)->b_data[MODE_HEADER_LENGTH + 2] & DCC_MASK) == 0) {
1943 #if DEBUG
1944 if (debugging)
1945 printk(ST_DEB_MSG "st%d: Compression not supported.\n", dev);
1946 #endif
1947 scsi_release_command(SCpnt);
1948 SCpnt = NULL;
1949 return (-EIO);
1952 /* Do the change */
1953 if (state)
1954 (STp->buffer)->b_data[MODE_HEADER_LENGTH + 2] |= DCE_MASK;
1955 else
1956 (STp->buffer)->b_data[MODE_HEADER_LENGTH + 2] &= ~DCE_MASK;
1958 memset(cmd, 0, 10);
1959 cmd[0] = MODE_SELECT;
1960 cmd[1] = 0x10;
1961 cmd[4] = COMPRESSION_PAGE_LENGTH + MODE_HEADER_LENGTH;
1963 (STp->buffer)->b_data[0] = 0; /* Reserved data length */
1964 (STp->buffer)->b_data[1] = 0; /* Reserved media type byte */
1965 (STp->buffer)->b_data[MODE_HEADER_LENGTH] &= 0x3f;
1966 SCpnt = st_do_scsi(SCpnt, STp, cmd, cmd[4], STp->timeout, 0, TRUE);
1968 if ((STp->buffer)->last_result_fatal != 0) {
1969 #if DEBUG
1970 if (debugging)
1971 printk(ST_DEB_MSG "st%d: Compression change failed.\n", dev);
1972 #endif
1973 scsi_release_command(SCpnt);
1974 SCpnt = NULL;
1975 return (-EIO);
1978 #if DEBUG
1979 if (debugging)
1980 printk(ST_DEB_MSG "st%d: Compression state changed to %d.\n",
1981 dev, state);
1982 #endif
1984 scsi_release_command(SCpnt);
1985 SCpnt = NULL;
1986 STp->compression_changed = TRUE;
1987 return 0;
1991 /* Internal ioctl function */
1992 static int
1993 st_int_ioctl(struct inode * inode,
1994 unsigned int cmd_in, unsigned long arg)
1996 int timeout;
1997 long ltmp;
1998 int i, ioctl_result;
1999 int chg_eof = TRUE;
2000 unsigned char cmd[10];
2001 Scsi_Cmnd * SCpnt;
2002 Scsi_Tape * STp;
2003 ST_partstat * STps;
2004 int fileno, blkno, at_sm, undone, datalen;
2005 int dev = TAPE_NR(inode->i_rdev);
2007 STp = &(scsi_tapes[dev]);
2008 if (STp->ready != ST_READY && cmd_in != MTLOAD) {
2009 if (STp->ready == ST_NO_TAPE)
2010 return (-ENOMEDIUM);
2011 else
2012 return (-EIO);
2014 timeout = STp->long_timeout;
2015 STps = &(STp->ps[STp->partition]);
2016 fileno = STps->drv_file;
2017 blkno = STps->drv_block;
2018 at_sm = STps->at_sm;
2020 memset(cmd, 0, 10);
2021 datalen = 0;
2022 switch (cmd_in) {
2023 case MTFSFM:
2024 chg_eof = FALSE; /* Changed from the FSF after this */
2025 case MTFSF:
2026 cmd[0] = SPACE;
2027 cmd[1] = 0x01; /* Space FileMarks */
2028 cmd[2] = (arg >> 16);
2029 cmd[3] = (arg >> 8);
2030 cmd[4] = arg;
2031 #if DEBUG
2032 if (debugging)
2033 printk(ST_DEB_MSG "st%d: Spacing tape forward over %d filemarks.\n",
2034 dev, cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
2035 #endif
2036 if (fileno >= 0)
2037 fileno += arg;
2038 blkno = 0;
2039 at_sm &= (arg == 0);
2040 break;
2041 case MTBSFM:
2042 chg_eof = FALSE; /* Changed from the FSF after this */
2043 case MTBSF:
2044 cmd[0] = SPACE;
2045 cmd[1] = 0x01; /* Space FileMarks */
2046 ltmp = (-arg);
2047 cmd[2] = (ltmp >> 16);
2048 cmd[3] = (ltmp >> 8);
2049 cmd[4] = ltmp;
2050 #if DEBUG
2051 if (debugging) {
2052 if (cmd[2] & 0x80)
2053 ltmp = 0xff000000;
2054 ltmp = ltmp | (cmd[2] << 16) | (cmd[3] << 8) | cmd[4];
2055 printk(ST_DEB_MSG "st%d: Spacing tape backward over %ld filemarks.\n",
2056 dev, (-ltmp));
2058 #endif
2059 if (fileno >= 0)
2060 fileno -= arg;
2061 blkno = (-1); /* We can't know the block number */
2062 at_sm &= (arg == 0);
2063 break;
2064 case MTFSR:
2065 cmd[0] = SPACE;
2066 cmd[1] = 0x00; /* Space Blocks */
2067 cmd[2] = (arg >> 16);
2068 cmd[3] = (arg >> 8);
2069 cmd[4] = arg;
2070 #if DEBUG
2071 if (debugging)
2072 printk(ST_DEB_MSG "st%d: Spacing tape forward %d blocks.\n", dev,
2073 cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
2074 #endif
2075 if (blkno >= 0)
2076 blkno += arg;
2077 at_sm &= (arg == 0);
2078 break;
2079 case MTBSR:
2080 cmd[0] = SPACE;
2081 cmd[1] = 0x00; /* Space Blocks */
2082 ltmp = (-arg);
2083 cmd[2] = (ltmp >> 16);
2084 cmd[3] = (ltmp >> 8);
2085 cmd[4] = ltmp;
2086 #if DEBUG
2087 if (debugging) {
2088 if (cmd[2] & 0x80)
2089 ltmp = 0xff000000;
2090 ltmp = ltmp | (cmd[2] << 16) | (cmd[3] << 8) | cmd[4];
2091 printk(ST_DEB_MSG "st%d: Spacing tape backward %ld blocks.\n", dev, (-ltmp));
2093 #endif
2094 if (blkno >= 0)
2095 blkno -= arg;
2096 at_sm &= (arg == 0);
2097 break;
2098 case MTFSS:
2099 cmd[0] = SPACE;
2100 cmd[1] = 0x04; /* Space Setmarks */
2101 cmd[2] = (arg >> 16);
2102 cmd[3] = (arg >> 8);
2103 cmd[4] = arg;
2104 #if DEBUG
2105 if (debugging)
2106 printk(ST_DEB_MSG "st%d: Spacing tape forward %d setmarks.\n", dev,
2107 cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
2108 #endif
2109 if (arg != 0) {
2110 blkno = fileno = (-1);
2111 at_sm = 1;
2113 break;
2114 case MTBSS:
2115 cmd[0] = SPACE;
2116 cmd[1] = 0x04; /* Space Setmarks */
2117 ltmp = (-arg);
2118 cmd[2] = (ltmp >> 16);
2119 cmd[3] = (ltmp >> 8);
2120 cmd[4] = ltmp;
2121 #if DEBUG
2122 if (debugging) {
2123 if (cmd[2] & 0x80)
2124 ltmp = 0xff000000;
2125 ltmp = ltmp | (cmd[2] << 16) | (cmd[3] << 8) | cmd[4];
2126 printk(ST_DEB_MSG "st%d: Spacing tape backward %ld setmarks.\n",
2127 dev, (-ltmp));
2129 #endif
2130 if (arg != 0) {
2131 blkno = fileno = (-1);
2132 at_sm = 1;
2134 break;
2135 case MTWEOF:
2136 case MTWSM:
2137 if (STp->write_prot)
2138 return (-EACCES);
2139 cmd[0] = WRITE_FILEMARKS;
2140 if (cmd_in == MTWSM)
2141 cmd[1] = 2;
2142 cmd[2] = (arg >> 16);
2143 cmd[3] = (arg >> 8);
2144 cmd[4] = arg;
2145 timeout = STp->timeout;
2146 #if DEBUG
2147 if (debugging) {
2148 if (cmd_in == MTWEOF)
2149 printk(ST_DEB_MSG "st%d: Writing %d filemarks.\n", dev,
2150 cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
2151 else
2152 printk(ST_DEB_MSG "st%d: Writing %d setmarks.\n", dev,
2153 cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
2155 #endif
2156 if (fileno >= 0)
2157 fileno += arg;
2158 blkno = 0;
2159 at_sm = (cmd_in == MTWSM);
2160 break;
2161 case MTREW:
2162 cmd[0] = REZERO_UNIT;
2163 #if ST_NOWAIT
2164 cmd[1] = 1; /* Don't wait for completion */
2165 timeout = STp->timeout;
2166 #endif
2167 #if DEBUG
2168 if (debugging)
2169 printk(ST_DEB_MSG "st%d: Rewinding tape.\n", dev);
2170 #endif
2171 fileno = blkno = at_sm = 0 ;
2172 break;
2173 case MTOFFL:
2174 case MTLOAD:
2175 case MTUNLOAD:
2176 cmd[0] = START_STOP;
2177 if (cmd_in == MTLOAD)
2178 cmd[4] |= 1;
2180 * If arg >= 1 && arg <= 6 Enhanced load/unload in HP C1553A
2182 if (cmd_in != MTOFFL &&
2183 arg >= 1 + MT_ST_HPLOADER_OFFSET
2184 && arg <= 6 + MT_ST_HPLOADER_OFFSET) {
2185 #if DEBUG
2186 if (debugging) {
2187 printk(ST_DEB_MSG "st%d: Enhanced %sload slot %2ld.\n",
2188 dev, (cmd[4]) ? "" : "un",
2189 arg - MT_ST_HPLOADER_OFFSET);
2191 #endif
2192 cmd[3] = arg - MT_ST_HPLOADER_OFFSET; /* MediaID field of C1553A */
2194 #if ST_NOWAIT
2195 cmd[1] = 1; /* Don't wait for completion */
2196 timeout = STp->timeout;
2197 #else
2198 timeout = STp->long_timeout;
2199 #endif
2200 #if DEBUG
2201 if (debugging) {
2202 if (cmd_in != MTLOAD)
2203 printk(ST_DEB_MSG "st%d: Unloading tape.\n", dev);
2204 else
2205 printk(ST_DEB_MSG "st%d: Loading tape.\n", dev);
2207 #endif
2208 fileno = blkno = at_sm = 0 ;
2209 break;
2210 case MTNOP:
2211 #if DEBUG
2212 if (debugging)
2213 printk(ST_DEB_MSG "st%d: No op on tape.\n", dev);
2214 #endif
2215 return 0; /* Should do something ? */
2216 break;
2217 case MTRETEN:
2218 cmd[0] = START_STOP;
2219 #if ST_NOWAIT
2220 cmd[1] = 1; /* Don't wait for completion */
2221 timeout = STp->timeout;
2222 #endif
2223 cmd[4] = 3;
2224 #if DEBUG
2225 if (debugging)
2226 printk(ST_DEB_MSG "st%d: Retensioning tape.\n", dev);
2227 #endif
2228 fileno = blkno = at_sm = 0;
2229 break;
2230 case MTEOM:
2231 if (!STp->fast_mteom) {
2232 /* space to the end of tape */
2233 ioctl_result = st_int_ioctl(inode, MTFSF, 0x3fff);
2234 fileno = STps->drv_file;
2235 if (STps->eof >= ST_EOD_1)
2236 return 0;
2237 /* The next lines would hide the number of spaced FileMarks
2238 That's why I inserted the previous lines. I had no luck
2239 with detecting EOM with FSF, so we go now to EOM.
2240 Joerg Weule */
2242 else
2243 fileno = (-1);
2244 cmd[0] = SPACE;
2245 cmd[1] = 3;
2246 #if DEBUG
2247 if (debugging)
2248 printk(ST_DEB_MSG "st%d: Spacing to end of recorded medium.\n", dev);
2249 #endif
2250 blkno = 0;
2251 at_sm = 0;
2252 break;
2253 case MTERASE:
2254 if (STp->write_prot)
2255 return (-EACCES);
2256 cmd[0] = ERASE;
2257 cmd[1] = 1; /* To the end of tape */
2258 #if ST_NOWAIT
2259 cmd[1] |= 2; /* Don't wait for completion */
2260 timeout = STp->timeout;
2261 #else
2262 timeout = STp->long_timeout * 8;
2263 #endif
2264 #if DEBUG
2265 if (debugging)
2266 printk(ST_DEB_MSG "st%d: Erasing tape.\n", dev);
2267 #endif
2268 fileno = blkno = at_sm = 0 ;
2269 break;
2270 case MTLOCK:
2271 chg_eof = FALSE;
2272 cmd[0] = ALLOW_MEDIUM_REMOVAL;
2273 cmd[4] = SCSI_REMOVAL_PREVENT;
2274 #if DEBUG
2275 if (debugging)
2276 printk(ST_DEB_MSG "st%d: Locking drive door.\n", dev);
2277 #endif;
2278 break;
2279 case MTUNLOCK:
2280 chg_eof = FALSE;
2281 cmd[0] = ALLOW_MEDIUM_REMOVAL;
2282 cmd[4] = SCSI_REMOVAL_ALLOW;
2283 #if DEBUG
2284 if (debugging)
2285 printk(ST_DEB_MSG "st%d: Unlocking drive door.\n", dev);
2286 #endif;
2287 break;
2288 case MTSETBLK: /* Set block length */
2289 case MTSETDENSITY: /* Set tape density */
2290 case MTSETDRVBUFFER: /* Set drive buffering */
2291 case SET_DENS_AND_BLK: /* Set density and block size */
2292 chg_eof = FALSE;
2293 if (STp->dirty || (STp->buffer)->buffer_bytes != 0)
2294 return (-EIO); /* Not allowed if data in buffer */
2295 if ((cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) &&
2296 (arg & MT_ST_BLKSIZE_MASK) != 0 &&
2297 ((arg & MT_ST_BLKSIZE_MASK) < STp->min_block ||
2298 (arg & MT_ST_BLKSIZE_MASK) > STp->max_block ||
2299 (arg & MT_ST_BLKSIZE_MASK) > st_buffer_size)) {
2300 printk(KERN_WARNING "st%d: Illegal block size.\n", dev);
2301 return (-EINVAL);
2303 cmd[0] = MODE_SELECT;
2304 cmd[4] = datalen = 12;
2306 memset((STp->buffer)->b_data, 0, 12);
2307 if (cmd_in == MTSETDRVBUFFER)
2308 (STp->buffer)->b_data[2] = (arg & 7) << 4;
2309 else
2310 (STp->buffer)->b_data[2] =
2311 STp->drv_buffer << 4;
2312 (STp->buffer)->b_data[3] = 8; /* block descriptor length */
2313 if (cmd_in == MTSETDENSITY) {
2314 (STp->buffer)->b_data[4] = arg;
2315 STp->density_changed = TRUE; /* At least we tried ;-) */
2317 else if (cmd_in == SET_DENS_AND_BLK)
2318 (STp->buffer)->b_data[4] = arg >> 24;
2319 else
2320 (STp->buffer)->b_data[4] = STp->density;
2321 if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) {
2322 ltmp = arg & MT_ST_BLKSIZE_MASK;
2323 if (cmd_in == MTSETBLK)
2324 STp->blksize_changed = TRUE; /* At least we tried ;-) */
2326 else
2327 ltmp = STp->block_size;
2328 (STp->buffer)->b_data[9] = (ltmp >> 16);
2329 (STp->buffer)->b_data[10] = (ltmp >> 8);
2330 (STp->buffer)->b_data[11] = ltmp;
2331 timeout = STp->timeout;
2332 #if DEBUG
2333 if (debugging) {
2334 if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK)
2335 printk(ST_DEB_MSG "st%d: Setting block size to %d bytes.\n", dev,
2336 (STp->buffer)->b_data[9] * 65536 +
2337 (STp->buffer)->b_data[10] * 256 +
2338 (STp->buffer)->b_data[11]);
2339 if (cmd_in == MTSETDENSITY || cmd_in == SET_DENS_AND_BLK)
2340 printk(ST_DEB_MSG "st%d: Setting density code to %x.\n", dev,
2341 (STp->buffer)->b_data[4]);
2342 if (cmd_in == MTSETDRVBUFFER)
2343 printk(ST_DEB_MSG "st%d: Setting drive buffer code to %d.\n", dev,
2344 ((STp->buffer)->b_data[2] >> 4) & 7);
2346 #endif
2347 break;
2348 default:
2349 return (-ENOSYS);
2352 SCpnt = st_do_scsi(NULL, STp, cmd, datalen, timeout, MAX_RETRIES, TRUE);
2353 if (!SCpnt)
2354 return (-EBUSY);
2356 ioctl_result = (STp->buffer)->last_result_fatal;
2358 if (!ioctl_result) { /* SCSI command successful */
2359 scsi_release_command(SCpnt);
2360 SCpnt = NULL;
2361 STps->drv_block = blkno;
2362 STps->drv_file = fileno;
2363 STps->at_sm = at_sm;
2365 if (cmd_in == MTLOCK)
2366 STp->door_locked = ST_LOCKED_EXPLICIT;
2367 else if (cmd_in == MTUNLOCK)
2368 STp->door_locked = ST_UNLOCKED;
2370 if (cmd_in == MTBSFM)
2371 ioctl_result = st_int_ioctl(inode, MTFSF, 1);
2372 else if (cmd_in == MTFSFM)
2373 ioctl_result = st_int_ioctl(inode, MTBSF, 1);
2375 if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) {
2376 STp->block_size = arg & MT_ST_BLKSIZE_MASK;
2377 if (STp->block_size != 0)
2378 (STp->buffer)->buffer_blocks =
2379 (STp->buffer)->buffer_size / STp->block_size;
2380 (STp->buffer)->buffer_bytes = (STp->buffer)->read_pointer = 0;
2381 if (cmd_in == SET_DENS_AND_BLK)
2382 STp->density = arg >> MT_ST_DENSITY_SHIFT;
2384 else if (cmd_in == MTSETDRVBUFFER)
2385 STp->drv_buffer = (arg & 7);
2386 else if (cmd_in == MTSETDENSITY)
2387 STp->density = arg;
2389 if (cmd_in == MTEOM)
2390 STps->eof = ST_EOD;
2391 else if (cmd_in == MTFSF)
2392 STps->eof = ST_FM;
2393 else if (chg_eof)
2394 STps->eof = ST_NOEOF;
2397 if (cmd_in == MTOFFL || cmd_in == MTUNLOAD)
2398 STp->rew_at_close = 0;
2399 else if (cmd_in == MTLOAD) {
2400 STp->rew_at_close = (MINOR(inode->i_rdev) & 0x80) == 0;
2401 for (i=0; i < ST_NBR_PARTITIONS; i++) {
2402 STp->ps[i].rw = ST_IDLE;
2403 STp->ps[i].last_block_valid = FALSE;
2405 STp->partition = 0;
2408 } else { /* SCSI command was not completely successful. Don't return
2409 from this block without releasing the SCSI command block! */
2411 if (SCpnt->sense_buffer[2] & 0x40) {
2412 if (cmd_in != MTBSF && cmd_in != MTBSFM &&
2413 cmd_in != MTBSR && cmd_in != MTBSS)
2414 STps->eof = ST_EOM_OK;
2415 STps->drv_block = 0;
2418 undone = (
2419 (SCpnt->sense_buffer[3] << 24) +
2420 (SCpnt->sense_buffer[4] << 16) +
2421 (SCpnt->sense_buffer[5] << 8) +
2422 SCpnt->sense_buffer[6] );
2423 if (cmd_in == MTWEOF &&
2424 (SCpnt->sense_buffer[0] & 0x70) == 0x70 &&
2425 (SCpnt->sense_buffer[2] & 0x4f) == 0x40 &&
2426 ((SCpnt->sense_buffer[0] & 0x80) == 0 || undone == 0)) {
2427 ioctl_result = 0; /* EOF written succesfully at EOM */
2428 if (fileno >= 0)
2429 fileno++;
2430 STps->drv_file = fileno;
2431 STps->eof = ST_NOEOF;
2433 else if ( (cmd_in == MTFSF) || (cmd_in == MTFSFM) ) {
2434 if (fileno >= 0)
2435 STps->drv_file = fileno - undone ;
2436 else
2437 STps->drv_file = fileno;
2438 STps->drv_block = 0;
2439 STps->eof = ST_NOEOF;
2441 else if ( (cmd_in == MTBSF) || (cmd_in == MTBSFM) ) {
2442 if (fileno >= 0)
2443 STps->drv_file = fileno + undone ;
2444 else
2445 STps->drv_file = fileno;
2446 STps->drv_block = 0;
2447 STps->eof = ST_NOEOF;
2449 else if (cmd_in == MTFSR) {
2450 if (SCpnt->sense_buffer[2] & 0x80) { /* Hit filemark */
2451 if (STps->drv_file >= 0)
2452 STps->drv_file++;
2453 STps->drv_block = 0;
2454 STps->eof = ST_FM;
2456 else {
2457 if (blkno >= undone)
2458 STps->drv_block = blkno - undone;
2459 else
2460 STps->drv_block = (-1);
2461 STps->eof = ST_NOEOF;
2464 else if (cmd_in == MTBSR) {
2465 if (SCpnt->sense_buffer[2] & 0x80) { /* Hit filemark */
2466 STps->drv_file--;
2467 STps->drv_block = (-1);
2469 else {
2470 if (blkno >= 0)
2471 STps->drv_block = blkno + undone;
2472 else
2473 STps->drv_block = (-1);
2475 STps->eof = ST_NOEOF;
2477 else if (cmd_in == MTEOM) {
2478 STps->drv_file = (-1);
2479 STps->drv_block = (-1);
2480 STps->eof = ST_EOD;
2482 else if (chg_eof)
2483 STps->eof = ST_NOEOF;
2485 if ((SCpnt->sense_buffer[2] & 0x0f) == BLANK_CHECK)
2486 STps->eof = ST_EOD;
2488 if (cmd_in == MTLOCK)
2489 STp->door_locked = ST_LOCK_FAILS;
2491 scsi_release_command(SCpnt);
2492 SCpnt = NULL;
2495 return ioctl_result;
2499 \f/* Get the tape position. If bt == 2, arg points into a kernel space mt_loc
2500 structure. */
2502 static int
2503 get_location(struct inode * inode, unsigned int *block, int *partition,
2504 int logical)
2506 Scsi_Tape *STp;
2507 int dev = TAPE_NR(inode->i_rdev);
2508 int result;
2509 unsigned char scmd[10];
2510 Scsi_Cmnd *SCpnt;
2512 STp = &(scsi_tapes[dev]);
2513 if (STp->ready != ST_READY)
2514 return (-EIO);
2516 memset (scmd, 0, 10);
2517 if ((STp->device)->scsi_level < SCSI_2) {
2518 scmd[0] = QFA_REQUEST_BLOCK;
2519 scmd[4] = 3;
2521 else {
2522 scmd[0] = READ_POSITION;
2523 if (!logical && !STp->scsi2_logical)
2524 scmd[1] = 1;
2526 SCpnt = st_do_scsi(NULL, STp, scmd, 20, STp->timeout, MAX_READY_RETRIES, TRUE);
2527 if (!SCpnt)
2528 return (-EBUSY);
2530 if ((STp->buffer)->last_result_fatal != 0 ||
2531 (STp->device->scsi_level >= SCSI_2 &&
2532 ((STp->buffer)->b_data[0] & 4) != 0)) {
2533 *block = *partition = 0;
2534 #if DEBUG
2535 if (debugging)
2536 printk(ST_DEB_MSG "st%d: Can't read tape position.\n", dev);
2537 #endif
2538 result = (-EIO);
2540 else {
2541 result = 0;
2542 if ((STp->device)->scsi_level < SCSI_2) {
2543 *block = ((STp->buffer)->b_data[0] << 16)
2544 + ((STp->buffer)->b_data[1] << 8)
2545 + (STp->buffer)->b_data[2];
2546 *partition = 0;
2548 else {
2549 *block = ((STp->buffer)->b_data[4] << 24)
2550 + ((STp->buffer)->b_data[5] << 16)
2551 + ((STp->buffer)->b_data[6] << 8)
2552 + (STp->buffer)->b_data[7];
2553 *partition = (STp->buffer)->b_data[1];
2554 if (((STp->buffer)->b_data[0] & 0x80) &&
2555 (STp->buffer)->b_data[1] == 0) /* BOP of partition 0 */
2556 STp->ps[0].drv_block = STp->ps[0].drv_file = 0;
2558 #if DEBUG
2559 if (debugging)
2560 printk(ST_DEB_MSG "st%d: Got tape pos. blk %d part %d.\n", dev,
2561 *block, *partition);
2562 #endif
2565 scsi_release_command(SCpnt);
2566 SCpnt = NULL;
2568 return result;
2572 /* Set the tape block and partition. Negative partition means that only the
2573 block should be set in vendor specific way. */
2574 static int
2575 set_location(struct inode * inode, unsigned int block, int partition,
2576 int logical)
2578 Scsi_Tape *STp;
2579 ST_partstat *STps;
2580 int dev = TAPE_NR(inode->i_rdev);
2581 int result, p;
2582 unsigned int blk;
2583 int timeout;
2584 unsigned char scmd[10];
2585 Scsi_Cmnd *SCpnt;
2587 STp = &(scsi_tapes[dev]);
2588 if (STp->ready != ST_READY)
2589 return (-EIO);
2590 timeout = STp->long_timeout;
2591 STps = &(STp->ps[STp->partition]);
2593 #if DEBUG
2594 if (debugging)
2595 printk(ST_DEB_MSG "st%d: Setting block to %d and partition to %d.\n",
2596 dev, block, partition);
2597 if (partition < 0)
2598 return (-EIO);
2599 #endif
2601 /* Update the location at the partition we are leaving */
2602 if ((!STp->can_partitions && partition != 0) ||
2603 partition >= ST_NBR_PARTITIONS)
2604 return (-EINVAL);
2605 if (partition != STp->partition) {
2606 if (get_location(inode, &blk, &p, 1))
2607 STps->last_block_valid = FALSE;
2608 else {
2609 STps->last_block_valid = TRUE;
2610 STps->last_block_visited = blk;
2611 #if DEBUG
2612 if (debugging)
2613 printk(ST_DEB_MSG "st%d: Visited block %d for partition %d saved.\n",
2614 dev, blk, STp->partition);
2615 #endif
2619 memset (scmd, 0, 10);
2620 if ((STp->device)->scsi_level < SCSI_2) {
2621 scmd[0] = QFA_SEEK_BLOCK;
2622 scmd[2] = (block >> 16);
2623 scmd[3] = (block >> 8);
2624 scmd[4] = block;
2625 scmd[5] = 0;
2627 else {
2628 scmd[0] = SEEK_10;
2629 scmd[3] = (block >> 24);
2630 scmd[4] = (block >> 16);
2631 scmd[5] = (block >> 8);
2632 scmd[6] = block;
2633 if (!logical && !STp->scsi2_logical)
2634 scmd[1] = 4;
2635 if (STp->partition != partition) {
2636 scmd[1] |= 2;
2637 scmd[8] = partition;
2638 #if DEBUG
2639 if (debugging)
2640 printk(ST_DEB_MSG "st%d: Trying to change partition from %d to %d\n",
2641 dev, STp->partition, partition);
2642 #endif
2645 #if ST_NOWAIT
2646 scmd[1] |= 1; /* Don't wait for completion */
2647 timeout = STp->timeout;
2648 #endif
2650 SCpnt = st_do_scsi(NULL, STp, scmd, 20, timeout, MAX_READY_RETRIES, TRUE);
2651 if (!SCpnt)
2652 return (-EBUSY);
2654 STps->drv_block = STps->drv_file = (-1);
2655 STps->eof = ST_NOEOF;
2656 if ((STp->buffer)->last_result_fatal != 0) {
2657 result = (-EIO);
2658 if (STp->can_partitions &&
2659 (STp->device)->scsi_level >= SCSI_2 &&
2660 (p = find_partition(inode)) >= 0)
2661 STp->partition = p;
2663 else {
2664 if (STp->can_partitions) {
2665 STp->partition = partition;
2666 STps = &(STp->ps[partition]);
2667 if (!STps->last_block_valid ||
2668 STps->last_block_visited != block) {
2669 STps->at_sm = 0;
2670 STps->rw = ST_IDLE;
2673 else
2674 STps->at_sm = 0;
2675 if (block == 0)
2676 STps->drv_block = STps->drv_file = 0;
2677 result = 0;
2680 scsi_release_command(SCpnt);
2681 SCpnt = NULL;
2683 return result;
2687 /* Find the current partition number for the drive status. Called from open and
2688 returns either partition number of negative error code. */
2689 static int
2690 find_partition(struct inode *inode)
2692 int i, partition;
2693 unsigned int block;
2695 if ((i = get_location(inode, &block, &partition, 1)) < 0)
2696 return i;
2697 if (partition >= ST_NBR_PARTITIONS)
2698 return (-EIO);
2699 return partition;
2703 /* Change the partition if necessary */
2704 static int
2705 update_partition(struct inode * inode)
2707 int dev = TAPE_NR(inode->i_rdev);
2708 Scsi_Tape *STp;
2709 ST_partstat *STps;
2711 STp = &(scsi_tapes[dev]);
2712 if (STp->partition == STp->new_partition)
2713 return 0;
2714 STps = &(STp->ps[STp->new_partition]);
2715 if (!STps->last_block_valid)
2716 STps->last_block_visited = 0;
2717 return set_location(inode, STps->last_block_visited, STp->new_partition, 1);
2720 \f/* Functions for reading and writing the medium partition mode page. These
2721 seem to work with Wangtek 6200HS and HP C1533A. */
2723 #define PART_PAGE 0x11
2724 #define PART_PAGE_LENGTH 10
2726 /* Get the number of partitions on the tape. As a side effect reads the
2727 mode page into the tape buffer. */
2728 static int
2729 nbr_partitions(struct inode * inode)
2731 int dev = TAPE_NR(inode->i_rdev), result;
2732 Scsi_Tape *STp;
2733 Scsi_Cmnd * SCpnt = NULL;
2734 unsigned char cmd[10];
2736 STp = &(scsi_tapes[dev]);
2737 if (STp->ready != ST_READY)
2738 return (-EIO);
2740 memset ((void *) &cmd[0], 0, 10);
2741 cmd[0] = MODE_SENSE;
2742 cmd[1] = 8; /* Page format */
2743 cmd[2] = PART_PAGE;
2744 cmd[4] = 200;
2746 SCpnt = st_do_scsi(SCpnt, STp, cmd, 200, STp->timeout, MAX_READY_RETRIES, TRUE);
2747 if (SCpnt == NULL)
2748 return (-EBUSY);
2749 scsi_release_command(SCpnt);
2750 SCpnt = NULL;
2752 if ((STp->buffer)->last_result_fatal != 0) {
2753 #if DEBUG
2754 if (debugging)
2755 printk(ST_DEB_MSG "st%d: Can't read medium partition page.\n", dev);
2756 #endif
2757 result = (-EIO);
2759 else {
2760 result = (STp->buffer)->b_data[MODE_HEADER_LENGTH + 3] + 1;
2761 #if DEBUG
2762 if (debugging)
2763 printk(ST_DEB_MSG "st%d: Number of partitions %d.\n", dev, result);
2764 #endif
2767 return result;
2771 /* Partition the tape into two partitions if size > 0 or one partition if
2772 size == 0 */
2773 static int
2774 partition_tape(struct inode * inode, int size)
2776 int dev = TAPE_NR(inode->i_rdev), result;
2777 int length;
2778 Scsi_Tape *STp;
2779 Scsi_Cmnd * SCpnt = NULL;
2780 unsigned char cmd[10], *bp;
2782 if ((result = nbr_partitions(inode)) < 0)
2783 return result;
2784 STp = &(scsi_tapes[dev]);
2786 /* The mode page is in the buffer. Let's modify it and write it. */
2787 bp = &((STp->buffer)->b_data[0]);
2788 if (size <= 0) {
2789 length = 8;
2790 bp[MODE_HEADER_LENGTH + 3] = 0;
2791 #if DEBUG
2792 if (debugging)
2793 printk(ST_DEB_MSG "st%d: Formatting tape with one partition.\n", dev);
2794 #endif
2796 else {
2797 length = 10;
2798 bp[MODE_HEADER_LENGTH + 3] = 1;
2799 bp[MODE_HEADER_LENGTH + 8] = (size >> 8) & 0xff;
2800 bp[MODE_HEADER_LENGTH + 9] = size & 0xff;
2801 #if DEBUG
2802 if (debugging)
2803 printk(ST_DEB_MSG "st%d: Formatting tape with two partition (1 = %d MB).\n",
2804 dev, size);
2805 #endif
2807 bp[MODE_HEADER_LENGTH + 6] = 0;
2808 bp[MODE_HEADER_LENGTH + 7] = 0;
2809 bp[MODE_HEADER_LENGTH + 4] = 0x30; /* IDP | PSUM = MB */
2811 bp[0] = 0;
2812 bp[1] = 0;
2813 bp[MODE_HEADER_LENGTH] &= 0x3f;
2814 bp[MODE_HEADER_LENGTH + 1] = length - 2;
2816 memset(cmd, 0, 10);
2817 cmd[0] = MODE_SELECT;
2818 cmd[1] = 0x10;
2819 cmd[4] = length + MODE_HEADER_LENGTH;
2821 SCpnt = st_do_scsi(SCpnt, STp, cmd, cmd[4], STp->long_timeout,
2822 MAX_READY_RETRIES, TRUE);
2823 if (SCpnt == NULL)
2824 return (-EBUSY);
2825 scsi_release_command(SCpnt);
2826 SCpnt = NULL;
2828 if ((STp->buffer)->last_result_fatal != 0) {
2829 printk(KERN_INFO "st%d: Partitioning of tape failed.\n", dev);
2830 result = (-EIO);
2832 else
2833 result = 0;
2835 return result;
2840 /* The ioctl command */
2841 static int
2842 st_ioctl(struct inode * inode,struct file * file,
2843 unsigned int cmd_in, unsigned long arg)
2845 int i, cmd_nr, cmd_type, bt;
2846 unsigned int blk;
2847 struct mtop mtc;
2848 struct mtpos mt_pos;
2849 Scsi_Tape *STp;
2850 ST_mode *STm;
2851 ST_partstat *STps;
2852 int dev = TAPE_NR(inode->i_rdev);
2854 STp = &(scsi_tapes[dev]);
2855 #if DEBUG
2856 if (debugging && !STp->in_use) {
2857 printk(ST_DEB_MSG "st%d: Incorrect device.\n", dev);
2858 return (-EIO);
2860 #endif
2861 STm = &(STp->modes[STp->current_mode]);
2862 STps = &(STp->ps[STp->partition]);
2865 * If we are in the middle of error recovery, don't let anyone
2866 * else try and use this device. Also, if error recovery fails, it
2867 * may try and take the device offline, in which case all further
2868 * access to the device is prohibited.
2870 if( !scsi_block_when_processing_errors(STp->device) ) {
2871 return -ENXIO;
2874 cmd_type = _IOC_TYPE(cmd_in);
2875 cmd_nr = _IOC_NR(cmd_in);
2877 if (cmd_type == _IOC_TYPE(MTIOCTOP) && cmd_nr == _IOC_NR(MTIOCTOP)) {
2878 if (_IOC_SIZE(cmd_in) != sizeof(mtc))
2879 return (-EINVAL);
2881 i = copy_from_user((char *) &mtc, (char *)arg, sizeof(struct mtop));
2882 if (i)
2883 return (-EFAULT);
2885 if (mtc.mt_op == MTSETDRVBUFFER && !capable(CAP_SYS_ADMIN)) {
2886 printk(KERN_WARNING "st%d: MTSETDRVBUFFER only allowed for root.\n", dev);
2887 return (-EPERM);
2889 if (!STm->defined &&
2890 (mtc.mt_op != MTSETDRVBUFFER && (mtc.mt_count & MT_ST_OPTIONS) == 0))
2891 return (-ENXIO);
2893 if (!(STp->device)->was_reset) {
2895 if (STps->eof == ST_FM_HIT) {
2896 if (mtc.mt_op == MTFSF || mtc.mt_op == MTFSFM|| mtc.mt_op == MTEOM) {
2897 mtc.mt_count -= 1;
2898 if (STps->drv_file >= 0)
2899 STps->drv_file += 1;
2901 else if (mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM) {
2902 mtc.mt_count += 1;
2903 if (STps->drv_file >= 0)
2904 STps->drv_file += 1;
2908 if (mtc.mt_op == MTSEEK) {
2909 /* Old position must be restored if partition will be changed */
2910 i = !STp->can_partitions ||
2911 (STp->new_partition != STp->partition);
2913 else {
2914 i = mtc.mt_op == MTREW || mtc.mt_op == MTOFFL ||
2915 mtc.mt_op == MTRETEN || mtc.mt_op == MTEOM ||
2916 mtc.mt_op == MTLOCK || mtc.mt_op == MTLOAD ||
2917 mtc.mt_op == MTCOMPRESSION;
2919 i = flush_buffer(inode, file, i);
2920 if (i < 0)
2921 return i;
2923 else {
2925 * If there was a bus reset, block further access
2926 * to this device. If the user wants to rewind the tape,
2927 * then reset the flag and allow access again.
2929 if(mtc.mt_op != MTREW &&
2930 mtc.mt_op != MTOFFL &&
2931 mtc.mt_op != MTRETEN &&
2932 mtc.mt_op != MTERASE &&
2933 mtc.mt_op != MTSEEK &&
2934 mtc.mt_op != MTEOM)
2935 return (-EIO);
2936 STp->device->was_reset = 0;
2937 if (STp->door_locked != ST_UNLOCKED &&
2938 STp->door_locked != ST_LOCK_FAILS) {
2939 if (st_int_ioctl(inode, MTLOCK, 0)) {
2940 printk(KERN_NOTICE "st%d: Could not relock door after bus reset.\n",
2941 dev);
2942 STp->door_locked = ST_UNLOCKED;
2947 if (mtc.mt_op != MTNOP && mtc.mt_op != MTSETBLK &&
2948 mtc.mt_op != MTSETDENSITY && mtc.mt_op != MTWSM &&
2949 mtc.mt_op != MTSETDRVBUFFER && mtc.mt_op != MTSETPART)
2950 STps->rw = ST_IDLE; /* Prevent automatic WEOF and fsf */
2952 if (mtc.mt_op == MTOFFL && STp->door_locked != ST_UNLOCKED)
2953 st_int_ioctl(inode, MTUNLOCK, 0); /* Ignore result! */
2955 if (mtc.mt_op == MTSETDRVBUFFER &&
2956 (mtc.mt_count & MT_ST_OPTIONS) != 0)
2957 return st_set_options(inode, mtc.mt_count);
2958 if (mtc.mt_op == MTSETPART) {
2959 if (!STp->can_partitions ||
2960 mtc.mt_count < 0 || mtc.mt_count >= ST_NBR_PARTITIONS)
2961 return (-EINVAL);
2962 if (mtc.mt_count >= STp->nbr_partitions &&
2963 (STp->nbr_partitions = nbr_partitions(inode)) < 0)
2964 return (-EIO);
2965 if (mtc.mt_count >= STp->nbr_partitions)
2966 return (-EINVAL);
2967 STp->new_partition = mtc.mt_count;
2968 return 0;
2970 if (mtc.mt_op == MTMKPART) {
2971 if (!STp->can_partitions)
2972 return (-EINVAL);
2973 if ((i = st_int_ioctl(inode, MTREW, 0)) < 0 ||
2974 (i = partition_tape(inode, mtc.mt_count)) < 0)
2975 return i;
2976 for (i=0; i < ST_NBR_PARTITIONS; i++) {
2977 STp->ps[i].rw = ST_IDLE;
2978 STp->ps[i].at_sm = 0;
2979 STp->ps[i].last_block_valid = FALSE;
2981 STp->partition = STp->new_partition = 0;
2982 STp->nbr_partitions = 1; /* Bad guess ?-) */
2983 STps->drv_block = STps->drv_file = 0;
2984 return 0;
2986 if (mtc.mt_op == MTSEEK) {
2987 i = set_location(inode, mtc.mt_count, STp->new_partition, 0);
2988 if (!STp->can_partitions)
2989 STp->ps[0].rw = ST_IDLE;
2990 return i;
2992 if (STp->can_partitions && STp->ready == ST_READY &&
2993 (i = update_partition(inode)) < 0)
2994 return i;
2995 if (mtc.mt_op == MTCOMPRESSION)
2996 return st_compression(STp, (mtc.mt_count & 1));
2997 else
2998 return st_int_ioctl(inode, mtc.mt_op, mtc.mt_count);
3001 if (!STm->defined)
3002 return (-ENXIO);
3004 if ((i = flush_buffer(inode, file, FALSE)) < 0)
3005 return i;
3006 if (STp->can_partitions &&
3007 (i = update_partition(inode)) < 0)
3008 return i;
3010 if (cmd_type == _IOC_TYPE(MTIOCGET) && cmd_nr == _IOC_NR(MTIOCGET)) {
3012 if (_IOC_SIZE(cmd_in) != sizeof(struct mtget))
3013 return (-EINVAL);
3015 (STp->mt_status)->mt_dsreg =
3016 ((STp->block_size << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK) |
3017 ((STp->density << MT_ST_DENSITY_SHIFT) & MT_ST_DENSITY_MASK);
3018 (STp->mt_status)->mt_blkno = STps->drv_block;
3019 (STp->mt_status)->mt_fileno = STps->drv_file;
3020 if (STp->block_size != 0) {
3021 if (STps->rw == ST_WRITING)
3022 (STp->mt_status)->mt_blkno +=
3023 (STp->buffer)->buffer_bytes / STp->block_size;
3024 else if (STps->rw == ST_READING)
3025 (STp->mt_status)->mt_blkno -= ((STp->buffer)->buffer_bytes +
3026 STp->block_size - 1) / STp->block_size;
3029 (STp->mt_status)->mt_gstat = 0;
3030 if (STp->drv_write_prot)
3031 (STp->mt_status)->mt_gstat |= GMT_WR_PROT(0xffffffff);
3032 if ((STp->mt_status)->mt_blkno == 0) {
3033 if ((STp->mt_status)->mt_fileno == 0)
3034 (STp->mt_status)->mt_gstat |= GMT_BOT(0xffffffff);
3035 else
3036 (STp->mt_status)->mt_gstat |= GMT_EOF(0xffffffff);
3038 (STp->mt_status)->mt_resid = STp->partition;
3039 if (STps->eof == ST_EOM_OK || STps->eof == ST_EOM_ERROR)
3040 (STp->mt_status)->mt_gstat |= GMT_EOT(0xffffffff);
3041 else if (STps->eof >= ST_EOM_OK)
3042 (STp->mt_status)->mt_gstat |= GMT_EOD(0xffffffff);
3043 if (STp->density == 1)
3044 (STp->mt_status)->mt_gstat |= GMT_D_800(0xffffffff);
3045 else if (STp->density == 2)
3046 (STp->mt_status)->mt_gstat |= GMT_D_1600(0xffffffff);
3047 else if (STp->density == 3)
3048 (STp->mt_status)->mt_gstat |= GMT_D_6250(0xffffffff);
3049 if (STp->ready == ST_READY)
3050 (STp->mt_status)->mt_gstat |= GMT_ONLINE(0xffffffff);
3051 if (STp->ready == ST_NO_TAPE)
3052 (STp->mt_status)->mt_gstat |= GMT_DR_OPEN(0xffffffff);
3053 if (STps->at_sm)
3054 (STp->mt_status)->mt_gstat |= GMT_SM(0xffffffff);
3055 if (STm->do_async_writes || (STm->do_buffer_writes && STp->block_size != 0) ||
3056 STp->drv_buffer != 0)
3057 (STp->mt_status)->mt_gstat |= GMT_IM_REP_EN(0xffffffff);
3059 i = copy_to_user((char *)arg, (char *)(STp->mt_status),
3060 sizeof(struct mtget));
3061 if (i)
3062 return (-EFAULT);
3064 (STp->mt_status)->mt_erreg = 0; /* Clear after read */
3065 return 0;
3066 } /* End of MTIOCGET */
3068 if (cmd_type == _IOC_TYPE(MTIOCPOS) && cmd_nr == _IOC_NR(MTIOCPOS)) {
3069 if (_IOC_SIZE(cmd_in) != sizeof(struct mtpos))
3070 return (-EINVAL);
3071 if ((i = get_location(inode, &blk, &bt, 0)) < 0)
3072 return i;
3073 mt_pos.mt_blkno = blk;
3074 i = copy_to_user((char *)arg, (char *) (&mt_pos), sizeof(struct mtpos));
3075 if (i)
3076 return (-EFAULT);
3077 return 0;
3080 return scsi_ioctl(STp->device, cmd_in, (void *) arg);
3084 /* Try to allocate a new tape buffer */
3085 static ST_buffer *
3086 new_tape_buffer( int from_initialization, int need_dma )
3088 int i, priority, b_size, got = 0, segs = 0;
3089 ST_buffer *tb;
3091 if (st_nbr_buffers >= st_template.dev_max)
3092 return NULL; /* Should never happen */
3094 if (from_initialization)
3095 priority = GFP_ATOMIC;
3096 else
3097 priority = GFP_KERNEL;
3099 i = sizeof(ST_buffer) + (st_max_sg_segs - 1) * sizeof(struct scatterlist);
3100 tb = (ST_buffer *)scsi_init_malloc(i, priority);
3101 if (tb) {
3102 tb->this_size = i;
3103 if (need_dma)
3104 priority |= GFP_DMA;
3106 /* Try to allocate the first segment up to ST_FIRST_ORDER and the
3107 others big enough to reach the goal */
3108 for (b_size = PAGE_SIZE << ST_FIRST_ORDER;
3109 b_size / 2 >= st_buffer_size && b_size > PAGE_SIZE; )
3110 b_size /= 2;
3111 for ( ; b_size >= PAGE_SIZE; b_size /= 2) {
3112 tb->sg[0].address =
3113 (unsigned char *)scsi_init_malloc(b_size, priority);
3114 if (tb->sg[0].address != NULL) {
3115 tb->sg[0].alt_address = NULL;
3116 tb->sg[0].length = b_size;
3117 break;
3120 if (tb->sg[segs].address == NULL) {
3121 scsi_init_free((char *)tb, tb->this_size);
3122 tb = NULL;
3124 else { /* Got something, continue */
3126 for (b_size = PAGE_SIZE;
3127 st_buffer_size > tb->sg[0].length + (ST_FIRST_SG - 1) * b_size; )
3128 b_size *= 2;
3130 for (segs=1, got=tb->sg[0].length;
3131 got < st_buffer_size && segs < ST_FIRST_SG; ) {
3132 tb->sg[segs].address =
3133 (unsigned char *)scsi_init_malloc(b_size, priority);
3134 if (tb->sg[segs].address == NULL) {
3135 if (st_buffer_size - got <=
3136 (ST_FIRST_SG - segs) * b_size / 2) {
3137 b_size /= 2; /* Large enough for the rest of the buffers */
3138 continue;
3140 for (i=0; i < segs - 1; i++)
3141 scsi_init_free(tb->sg[i].address, tb->sg[i].length);
3142 scsi_init_free((char *)tb, tb->this_size);
3143 tb = NULL;
3144 break;
3146 tb->sg[segs].alt_address = NULL;
3147 tb->sg[segs].length = b_size;
3148 got += b_size;
3149 segs++;
3153 if (!tb) {
3154 printk(KERN_NOTICE "st: Can't allocate new tape buffer (nbr %d).\n",
3155 st_nbr_buffers);
3156 return NULL;
3158 tb->sg_segs = tb->orig_sg_segs = segs;
3159 tb->b_data = tb->sg[0].address;
3161 #if DEBUG
3162 if (debugging) {
3163 printk(ST_DEB_MSG
3164 "st: Allocated tape buffer %d (%d bytes, %d segments, dma: %d, a: %p).\n",
3165 st_nbr_buffers, got, tb->sg_segs, need_dma, tb->b_data);
3166 printk(ST_DEB_MSG
3167 "st: segment sizes: first %d, last %d bytes.\n",
3168 tb->sg[0].length, tb->sg[segs-1].length);
3170 #endif
3171 tb->in_use = 0;
3172 tb->dma = need_dma;
3173 tb->buffer_size = got;
3174 tb->writing = 0;
3175 st_buffers[st_nbr_buffers++] = tb;
3177 return tb;
3181 /* Try to allocate a temporary enlarged tape buffer */
3182 static int
3183 enlarge_buffer(ST_buffer *STbuffer, int new_size, int need_dma)
3185 int segs, nbr, max_segs, b_size, priority, got;
3187 normalize_buffer(STbuffer);
3189 max_segs = STbuffer->use_sg;
3190 if (max_segs > st_max_sg_segs)
3191 max_segs = st_max_sg_segs;
3192 nbr = max_segs - STbuffer->sg_segs;
3193 if (nbr <= 0)
3194 return FALSE;
3196 priority = GFP_KERNEL;
3197 if (need_dma)
3198 priority |= GFP_DMA;
3199 for (b_size = PAGE_SIZE; b_size * nbr < new_size - STbuffer->buffer_size; )
3200 b_size *= 2;
3202 for (segs=STbuffer->sg_segs, got=STbuffer->buffer_size;
3203 segs < max_segs && got < new_size; ) {
3204 STbuffer->sg[segs].address =
3205 (unsigned char *)scsi_init_malloc(b_size, priority);
3206 if (STbuffer->sg[segs].address == NULL) {
3207 if (new_size - got <= (max_segs - segs) * b_size / 2) {
3208 b_size /= 2; /* Large enough for the rest of the buffers */
3209 continue;
3211 printk(KERN_NOTICE "st: failed to enlarge buffer to %d bytes.\n",
3212 new_size);
3213 normalize_buffer(STbuffer);
3214 return FALSE;
3216 STbuffer->sg[segs].alt_address = NULL;
3217 STbuffer->sg[segs].length = b_size;
3218 STbuffer->sg_segs += 1;
3219 got += b_size;
3220 STbuffer->buffer_size = got;
3221 segs++;
3223 #if DEBUG
3224 if (debugging)
3225 printk(ST_DEB_MSG
3226 "st: Succeeded to enlarge buffer to %d bytes (segs %d->%d, %d).\n",
3227 got, STbuffer->orig_sg_segs, STbuffer->sg_segs, b_size);
3228 #endif
3230 return TRUE;
3234 /* Release the extra buffer */
3235 static void
3236 normalize_buffer(ST_buffer *STbuffer)
3238 int i;
3240 for (i=STbuffer->orig_sg_segs; i < STbuffer->sg_segs; i++) {
3241 scsi_init_free(STbuffer->sg[i].address, STbuffer->sg[i].length);
3242 STbuffer->buffer_size -= STbuffer->sg[i].length;
3244 #if DEBUG
3245 if (debugging && STbuffer->orig_sg_segs < STbuffer->sg_segs)
3246 printk(ST_DEB_MSG "st: Buffer at %p normalized to %d bytes (segs %d).\n",
3247 STbuffer->b_data, STbuffer->buffer_size, STbuffer->sg_segs);
3248 #endif
3249 STbuffer->sg_segs = STbuffer->orig_sg_segs;
3253 /* Move data from the user buffer to the tape buffer. Returns zero (success) or
3254 negative error code. */
3255 static int
3256 append_to_buffer(const char *ubp, ST_buffer *st_bp, int do_count)
3258 int i, cnt, res, offset;
3260 for (i=0, offset=st_bp->buffer_bytes;
3261 i < st_bp->sg_segs && offset >= st_bp->sg[i].length; i++)
3262 offset -= st_bp->sg[i].length;
3263 if (i == st_bp->sg_segs) { /* Should never happen */
3264 printk(KERN_WARNING "st: append_to_buffer offset overflow.\n");
3265 return (-EIO);
3267 for ( ; i < st_bp->sg_segs && do_count > 0; i++) {
3268 cnt = st_bp->sg[i].length - offset < do_count ?
3269 st_bp->sg[i].length - offset : do_count;
3270 res = copy_from_user(st_bp->sg[i].address + offset, ubp, cnt);
3271 if (res)
3272 return (-EFAULT);
3273 do_count -= cnt;
3274 st_bp->buffer_bytes += cnt;
3275 ubp += cnt;
3276 offset = 0;
3278 if (do_count) { /* Should never happen */
3279 printk(KERN_WARNING "st: append_to_buffer overflow (left %d).\n",
3280 do_count);
3281 return (-EIO);
3283 return 0;
3287 /* Move data from the tape buffer to the user buffer. Returns zero (success) or
3288 negative error code. */
3289 static int
3290 from_buffer(ST_buffer *st_bp, char *ubp, int do_count)
3292 int i, cnt, res, offset;
3294 for (i=0, offset=st_bp->read_pointer;
3295 i < st_bp->sg_segs && offset >= st_bp->sg[i].length; i++)
3296 offset -= st_bp->sg[i].length;
3297 if (i == st_bp->sg_segs) { /* Should never happen */
3298 printk(KERN_WARNING "st: from_buffer offset overflow.\n");
3299 return (-EIO);
3301 for ( ; i < st_bp->sg_segs && do_count > 0; i++) {
3302 cnt = st_bp->sg[i].length - offset < do_count ?
3303 st_bp->sg[i].length - offset : do_count;
3304 res = copy_to_user(ubp, st_bp->sg[i].address + offset, cnt);
3305 if (res)
3306 return (-EFAULT);
3307 do_count -= cnt;
3308 st_bp->buffer_bytes -= cnt;
3309 st_bp->read_pointer += cnt;
3310 ubp += cnt;
3311 offset = 0;
3313 if (do_count) { /* Should never happen */
3314 printk(KERN_WARNING "st: from_buffer overflow (left %d).\n",
3315 do_count);
3316 return (-EIO);
3318 return 0;
3322 #ifndef MODULE
3323 /* Set the boot options. Syntax: st=xxx,yyy
3324 where xxx is buffer size in 1024 byte blocks and yyy is write threshold
3325 in 1024 byte blocks. */
3326 __initfunc( void
3327 st_setup(char *str, int *ints))
3329 if (ints[0] > 0 && ints[1] > 0)
3330 st_buffer_size = ints[1] * ST_KILOBYTE;
3331 if (ints[0] > 1 && ints[2] > 0) {
3332 st_write_threshold = ints[2] * ST_KILOBYTE;
3333 if (st_write_threshold > st_buffer_size)
3334 st_write_threshold = st_buffer_size;
3336 if (ints[0] > 2 && ints[3] > 0)
3337 st_max_buffers = ints[3];
3339 #endif
3342 static struct file_operations st_fops = {
3343 NULL, /* lseek - default */
3344 st_read, /* read - general block-dev read */
3345 st_write, /* write - general block-dev write */
3346 NULL, /* readdir - bad */
3347 NULL, /* select */
3348 st_ioctl, /* ioctl */
3349 NULL, /* mmap */
3350 scsi_tape_open, /* open */
3351 scsi_tape_flush, /* flush */
3352 scsi_tape_close, /* release */
3353 NULL /* fsync */
3356 static int st_attach(Scsi_Device * SDp){
3357 Scsi_Tape * tpnt;
3358 ST_mode * STm;
3359 ST_partstat * STps;
3360 int i;
3362 if (SDp->type != TYPE_TAPE)
3363 return 1;
3365 if (st_template.nr_dev >= st_template.dev_max) {
3366 SDp->attached--;
3367 return 1;
3370 for(tpnt = scsi_tapes, i=0; i<st_template.dev_max; i++, tpnt++)
3371 if(!tpnt->device) break;
3373 if(i >= st_template.dev_max) panic ("scsi_devices corrupt (st)");
3375 scsi_tapes[i].device = SDp;
3376 if (SDp->scsi_level <= 2)
3377 scsi_tapes[i].mt_status->mt_type = MT_ISSCSI1;
3378 else
3379 scsi_tapes[i].mt_status->mt_type = MT_ISSCSI2;
3381 tpnt->devt = MKDEV(SCSI_TAPE_MAJOR, i);
3382 tpnt->dirty = 0;
3383 tpnt->waiting = NULL;
3384 tpnt->in_use = 0;
3385 tpnt->drv_buffer = 1; /* Try buffering if no mode sense */
3386 tpnt->restr_dma = (SDp->host)->unchecked_isa_dma;
3387 tpnt->density = 0;
3388 tpnt->do_auto_lock = ST_AUTO_LOCK;
3389 tpnt->can_bsr = ST_IN_FILE_POS;
3390 tpnt->can_partitions = 0;
3391 tpnt->two_fm = ST_TWO_FM;
3392 tpnt->fast_mteom = ST_FAST_MTEOM;
3393 tpnt->scsi2_logical = ST_SCSI2LOGICAL;
3394 tpnt->write_threshold = st_write_threshold;
3395 tpnt->default_drvbuffer = 0xff; /* No forced buffering */
3396 tpnt->partition = 0;
3397 tpnt->new_partition = 0;
3398 tpnt->nbr_partitions = 0;
3399 tpnt->timeout = ST_TIMEOUT;
3400 tpnt->long_timeout = ST_LONG_TIMEOUT;
3402 for (i=0; i < ST_NBR_MODES; i++) {
3403 STm = &(tpnt->modes[i]);
3404 STm->defined = FALSE;
3405 STm->sysv = ST_SYSV;
3406 STm->defaults_for_writes = 0;
3407 STm->do_async_writes = ST_ASYNC_WRITES;
3408 STm->do_buffer_writes = ST_BUFFER_WRITES;
3409 STm->do_read_ahead = ST_READ_AHEAD;
3410 STm->default_compression = ST_DONT_TOUCH;
3411 STm->default_blksize = (-1); /* No forced size */
3412 STm->default_density = (-1); /* No forced density */
3415 for (i=0; i < ST_NBR_PARTITIONS; i++) {
3416 STps = &(tpnt->ps[i]);
3417 STps->rw = ST_IDLE;
3418 STps->eof = ST_NOEOF;
3419 STps->at_sm = 0;
3420 STps->last_block_valid = FALSE;
3421 STps->drv_block = (-1);
3422 STps->drv_file = (-1);
3425 tpnt->current_mode = 0;
3426 tpnt->modes[0].defined = TRUE;
3428 tpnt->density_changed = tpnt->compression_changed =
3429 tpnt->blksize_changed = FALSE;
3431 st_template.nr_dev++;
3432 return 0;
3435 static int st_detect(Scsi_Device * SDp)
3437 if(SDp->type != TYPE_TAPE) return 0;
3439 printk(KERN_WARNING
3440 "Detected scsi tape st%d at scsi%d, channel %d, id %d, lun %d\n",
3441 st_template.dev_noticed++,
3442 SDp->host->host_no, SDp->channel, SDp->id, SDp->lun);
3444 return 1;
3447 static int st_registered = 0;
3449 /* Driver initialization (not __initfunc because may be called later) */
3450 static int st_init()
3452 int i;
3453 Scsi_Tape * STp;
3454 #if !ST_RUNTIME_BUFFERS
3455 int target_nbr;
3456 #endif
3458 if (st_template.dev_noticed == 0) return 0;
3460 if(!st_registered) {
3461 if (register_chrdev(SCSI_TAPE_MAJOR,"st",&st_fops)) {
3462 printk(KERN_ERR "Unable to get major %d for SCSI tapes\n",MAJOR_NR);
3463 return 1;
3465 st_registered++;
3468 if (scsi_tapes) return 0;
3469 st_template.dev_max = st_template.dev_noticed + ST_EXTRA_DEVS;
3470 if (st_template.dev_max < ST_MAX_TAPES)
3471 st_template.dev_max = ST_MAX_TAPES;
3472 if (st_template.dev_max > 128 / ST_NBR_MODES)
3473 printk(KERN_INFO "st: Only %d tapes accessible.\n", 128 / ST_NBR_MODES);
3474 scsi_tapes =
3475 (Scsi_Tape *) scsi_init_malloc(st_template.dev_max * sizeof(Scsi_Tape),
3476 GFP_ATOMIC);
3477 if (scsi_tapes == NULL) {
3478 printk(KERN_ERR "Unable to allocate descriptors for SCSI tapes.\n");
3479 unregister_chrdev(SCSI_TAPE_MAJOR, "st");
3480 return 1;
3483 #if DEBUG
3484 printk(ST_DEB_MSG "st: Buffer size %d bytes, write threshold %d bytes.\n",
3485 st_buffer_size, st_write_threshold);
3486 #endif
3488 memset(scsi_tapes, 0, st_template.dev_max * sizeof(Scsi_Tape));
3489 for (i=0; i < st_template.dev_max; ++i) {
3490 STp = &(scsi_tapes[i]);
3491 STp->capacity = 0xfffff;
3492 STp->mt_status = (struct mtget *) scsi_init_malloc(sizeof(struct mtget),
3493 GFP_ATOMIC);
3494 /* Initialize status */
3495 memset((void *) scsi_tapes[i].mt_status, 0, sizeof(struct mtget));
3498 /* Allocate the buffers */
3499 st_buffers =
3500 (ST_buffer **) scsi_init_malloc(st_template.dev_max * sizeof(ST_buffer *),
3501 GFP_ATOMIC);
3502 if (st_buffers == NULL) {
3503 printk(KERN_ERR "Unable to allocate tape buffer pointers.\n");
3504 unregister_chrdev(SCSI_TAPE_MAJOR, "st");
3505 scsi_init_free((char *) scsi_tapes,
3506 st_template.dev_max * sizeof(Scsi_Tape));
3507 return 1;
3510 #if ST_RUNTIME_BUFFERS
3511 st_nbr_buffers = 0;
3512 #else
3513 target_nbr = st_template.dev_noticed;
3514 if (target_nbr < ST_EXTRA_DEVS)
3515 target_nbr = ST_EXTRA_DEVS;
3516 if (target_nbr > st_max_buffers)
3517 target_nbr = st_max_buffers;
3519 for (i=st_nbr_buffers=0; i < target_nbr; i++) {
3520 if (!new_tape_buffer(TRUE, TRUE)) {
3521 if (i == 0) {
3522 #if 0
3523 printk(KERN_ERR "Can't continue without at least one tape buffer.\n");
3524 unregister_chrdev(SCSI_TAPE_MAJOR, "st");
3525 scsi_init_free((char *) st_buffers,
3526 st_template.dev_max * sizeof(ST_buffer *));
3527 scsi_init_free((char *) scsi_tapes,
3528 st_template.dev_max * sizeof(Scsi_Tape));
3529 return 1;
3530 #else
3531 printk(KERN_INFO "No tape buffers allocated at initialization.\n");
3532 break;
3533 #endif
3535 printk(KERN_INFO "Number of tape buffers adjusted.\n");
3536 break;
3539 #endif
3540 return 0;
3543 static void st_detach(Scsi_Device * SDp)
3545 Scsi_Tape * tpnt;
3546 int i;
3548 for(tpnt = scsi_tapes, i=0; i<st_template.dev_max; i++, tpnt++)
3549 if(tpnt->device == SDp) {
3550 tpnt->device = NULL;
3551 SDp->attached--;
3552 st_template.nr_dev--;
3553 st_template.dev_noticed--;
3554 return;
3556 return;
3560 #ifdef MODULE
3562 int init_module(void) {
3563 int result;
3565 if (buffer_kbs > 0)
3566 st_buffer_size = buffer_kbs * ST_KILOBYTE;
3567 if (write_threshold_kbs > 0)
3568 st_write_threshold = write_threshold_kbs * ST_KILOBYTE;
3569 if (st_write_threshold > st_buffer_size)
3570 st_write_threshold = st_buffer_size;
3571 if (max_buffers > 0)
3572 st_max_buffers = max_buffers;
3573 if (max_sg_segs >= ST_FIRST_SG)
3574 st_max_sg_segs = max_sg_segs;
3575 printk(KERN_INFO "st: bufsize %d, wrt %d, max buffers %d, s/g segs %d.\n",
3576 st_buffer_size, st_write_threshold, st_max_buffers, st_max_sg_segs);
3578 st_template.module = &__this_module;
3579 result = scsi_register_module(MODULE_SCSI_DEV, &st_template);
3580 if (result)
3581 return result;
3583 return 0;
3586 void cleanup_module( void)
3588 int i, j;
3590 scsi_unregister_module(MODULE_SCSI_DEV, &st_template);
3591 unregister_chrdev(SCSI_TAPE_MAJOR, "st");
3592 st_registered--;
3593 if(scsi_tapes != NULL) {
3594 scsi_init_free((char *) scsi_tapes,
3595 st_template.dev_max * sizeof(Scsi_Tape));
3597 if (st_buffers != NULL) {
3598 for (i=0; i < st_nbr_buffers; i++)
3599 if (st_buffers[i] != NULL) {
3600 for (j=0; j < st_buffers[i]->sg_segs; j++)
3601 scsi_init_free((char *) st_buffers[i]->sg[j].address,
3602 st_buffers[i]->sg[j].length);
3603 scsi_init_free((char *) st_buffers[i], st_buffers[i]->this_size);
3606 scsi_init_free((char *) st_buffers,
3607 st_template.dev_max * sizeof(ST_buffer *));
3610 st_template.dev_max = 0;
3611 printk(KERN_INFO "st: Unloaded.\n");
3613 #endif /* MODULE */