Import 1.3.85
[davej-history.git] / drivers / scsi / st.c
blobe1f2aa2a2456bbe13f4a24051e08d07ff1f74121
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, Steve Hirsch, Wolfgang Denk, Andreas Koppenh"ofer,
9 J"org Weule, and Eric Youngdale.
11 Copyright 1992 - 1996 Kai Makisara
12 email Kai.Makisara@metla.fi
14 Last modified: Sun Apr 7 20:33:27 1996 by root@kai.makisara.fi
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/string.h>
25 #include <linux/errno.h>
26 #include <linux/mtio.h>
27 #include <linux/ioctl.h>
28 #include <linux/fcntl.h>
29 #include <asm/segment.h>
30 #include <asm/dma.h>
31 #include <asm/system.h>
33 /* The driver prints some debugging information on the console if DEBUG
34 is defined and non-zero. */
35 #define DEBUG 0
37 /* The message level for the debug messages is currently set to KERN_NOTICE
38 so that people can easily see the messages. Later when the debugging messages
39 in the drivers are more widely classified, this may be changed to KERN_DEBUG. */
40 #define ST_DEB_MSG KERN_NOTICE
42 #define MAJOR_NR SCSI_TAPE_MAJOR
43 #include <linux/blk.h>
44 #include"scsi.h"
45 #include"hosts.h"
46 #include"scsi_ioctl.h"
47 #include"st.h"
48 #include"constants.h"
50 /* The default definitions have been moved to st_options.h */
52 #define ST_BLOCK_SIZE 1024
54 #include"st_options.h"
56 #define ST_BUFFER_SIZE (ST_BUFFER_BLOCKS * ST_BLOCK_SIZE)
57 #define ST_WRITE_THRESHOLD (ST_WRITE_THRESHOLD_BLOCKS * ST_BLOCK_SIZE)
59 /* The buffer size should fit into the 24 bits for length in the
60 6-byte SCSI read and write commands. */
61 #if ST_BUFFER_SIZE >= (2 << 24 - 1)
62 #error"Buffer size should not exceed (2 << 24 - 1) bytes!"
63 #endif
65 #if DEBUG
66 static int debugging =1;
67 #endif
69 #define MAX_RETRIES 0
70 #define MAX_WRITE_RETRIES 0
71 #define MAX_READY_RETRIES 5
72 #define NO_TAPE NOT_READY
74 #define ST_TIMEOUT (900 * HZ)
75 #define ST_LONG_TIMEOUT (2000 * HZ)
77 #define TAPE_NR(x) (MINOR(x) & ~(128 | ST_MODE_MASK))
78 #define TAPE_MODE(x) ((MINOR(x) & ST_MODE_MASK) >> ST_MODE_SHIFT)
80 static int st_nbr_buffers;
81 static ST_buffer **st_buffers;
82 static int st_buffer_size = ST_BUFFER_SIZE;
83 static int st_write_threshold = ST_WRITE_THRESHOLD;
84 static int st_max_buffers = ST_MAX_BUFFERS;
86 Scsi_Tape * scsi_tapes = NULL;
88 static int modes_defined = FALSE;
90 static ST_buffer *new_tape_buffer(int,int);
91 static intenlarge_buffer(ST_buffer *,int,int);
92 static voidnormalize_buffer(ST_buffer *);
94 static intst_init(void);
95 static intst_attach(Scsi_Device *);
96 static intst_detect(Scsi_Device *);
97 static voidst_detach(Scsi_Device *);
99 struct Scsi_Device_Template st_template = {NULL,"tape","st", NULL, TYPE_TAPE,
100 SCSI_TAPE_MAJOR,0,0,0,0,
101 st_detect, st_init,
102 NULL, st_attach, st_detach};
104 static intst_compression(Scsi_Tape *,int);
106 static intst_int_ioctl(struct inode * inode,struct file * file,
107 unsigned int cmd_in,unsigned long arg);
112 /* Convert the result to success code */
113 static int
114 st_chk_result(Scsi_Cmnd * SCpnt)
116 int dev =TAPE_NR(SCpnt->request.rq_dev);
117 int result = SCpnt->result;
118 unsigned char* sense = SCpnt->sense_buffer, scode;
119 #if DEBUG
120 const char*stp;
121 #endif
123 if(!result /* && SCpnt->sense_buffer[0] == 0 */)
124 return0;
125 #if DEBUG
126 if(debugging) {
127 printk(ST_DEB_MSG "st%d: Error: %x, cmd: %x %x %x %x %x %x Len: %d\n",
128 dev, result,
129 SCpnt->data_cmnd[0], SCpnt->data_cmnd[1], SCpnt->data_cmnd[2],
130 SCpnt->data_cmnd[3], SCpnt->data_cmnd[4], SCpnt->data_cmnd[5],
131 SCpnt->request_bufflen);
132 if(driver_byte(result) & DRIVER_SENSE)
133 print_sense("st", SCpnt);
134 else
135 printk("\n");
137 #endif
138 scode = sense[2] &0x0f;
139 if(!(driver_byte(result) & DRIVER_SENSE) ||
140 ((sense[0] &0x70) ==0x70&&
141 scode != NO_SENSE &&
142 scode != RECOVERED_ERROR &&
143 /* scode != UNIT_ATTENTION && */
144 scode != BLANK_CHECK &&
145 scode != VOLUME_OVERFLOW &&
146 SCpnt->data_cmnd[0] != MODE_SENSE &&
147 SCpnt->data_cmnd[0] != TEST_UNIT_READY)) {/* Abnormal conditions for tape */
148 #if !DEBUG
149 if(driver_byte(result) & DRIVER_SENSE) {
150 printk(KERN_WARNING "st%d: Error with sense data: ", dev);
151 print_sense("st", SCpnt);
153 else
154 printk(KERN_WARNING "st%d: Error %x.\n", dev, result);
155 #endif
158 if((sense[0] &0x70) ==0x70&&
159 scode == RECOVERED_ERROR
160 #if ST_RECOVERED_WRITE_FATAL
161 && SCpnt->data_cmnd[0] != WRITE_6
162 && SCpnt->data_cmnd[0] != WRITE_FILEMARKS
163 #endif
165 scsi_tapes[dev].recover_count++;
166 scsi_tapes[dev].mt_status->mt_erreg += (1<< MT_ST_SOFTERR_SHIFT);
167 #if DEBUG
168 if(debugging) {/* This is compiled always on purpose */
169 if(SCpnt->data_cmnd[0] == READ_6)
170 stp ="read";
171 else if(SCpnt->data_cmnd[0] == WRITE_6)
172 stp ="write";
173 else
174 stp ="ioctl";
175 printk(ST_DEB_MSG "st%d: Recovered %s error (%d).\n", dev, stp,
176 scsi_tapes[dev].recover_count);
178 #endif
179 if((sense[2] &0xe0) ==0)
180 return0;
182 return(-EIO);
186 /* Wakeup from interrupt */
187 static void
188 st_sleep_done(Scsi_Cmnd * SCpnt)
190 unsigned int st_nbr;
191 int remainder;
192 Scsi_Tape * STp;
194 if((st_nbr =TAPE_NR(SCpnt->request.rq_dev)) < st_template.nr_dev) {
195 STp = &(scsi_tapes[st_nbr]);
196 if((STp->buffer)->writing &&
197 (SCpnt->sense_buffer[0] &0x70) ==0x70&&
198 (SCpnt->sense_buffer[2] &0x40)) {
199 /* EOM at write-behind, has all been written? */
200 if((SCpnt->sense_buffer[0] &0x80) !=0)
201 remainder = (SCpnt->sense_buffer[3] <<24) |
202 (SCpnt->sense_buffer[4] <<16) |
203 (SCpnt->sense_buffer[5] <<8) | SCpnt->sense_buffer[6];
204 else
205 remainder =0;
206 if((SCpnt->sense_buffer[2] &0x0f) == VOLUME_OVERFLOW ||
207 remainder >0)
208 (STp->buffer)->last_result = SCpnt->result;/* Error */
209 else
210 (STp->buffer)->last_result = INT_MAX;/* OK */
212 else
213 (STp->buffer)->last_result = SCpnt->result;
214 if((STp->buffer)->writing) {
215 /* Process errors before releasing request */
216 (STp->buffer)->last_result_fatal =st_chk_result(SCpnt);
217 SCpnt->request.rq_status = RQ_INACTIVE;
219 else
220 SCpnt->request.rq_status = RQ_SCSI_DONE;
222 #if DEBUG
223 STp->write_pending =0;
224 #endif
225 up(SCpnt->request.sem);
227 #if DEBUG
228 else if(debugging)
229 printk(KERN_ERR "st?: Illegal interrupt device %x\n", st_nbr);
230 #endif
234 /* Do the scsi command */
235 static Scsi_Cmnd *
236 st_do_scsi(Scsi_Cmnd *SCpnt, Scsi_Tape *STp,unsigned char*cmd,int bytes,
237 int timeout,int retries)
239 if(SCpnt == NULL)
240 if((SCpnt =allocate_device(NULL, STp->device,1)) == NULL) {
241 printk(KERN_ERR "st%d: Can't get SCSI request.\n",TAPE_NR(STp->devt));
242 return NULL;
245 cmd[1] |= (SCpnt->lun <<5) &0xe0;
246 STp->sem = MUTEX_LOCKED;
247 SCpnt->request.sem = &(STp->sem);
248 SCpnt->request.rq_status = RQ_SCSI_BUSY;
249 SCpnt->request.rq_dev = STp->devt;
251 scsi_do_cmd(SCpnt, (void*)cmd, (STp->buffer)->b_data, bytes,
252 st_sleep_done, timeout, retries);
254 down(SCpnt->request.sem);
256 (STp->buffer)->last_result_fatal =st_chk_result(SCpnt);
258 return SCpnt;
262 /* Handle the write-behind checking */
263 static void
264 write_behind_check(Scsi_Tape *STp)
266 ST_buffer * STbuffer;
268 STbuffer = STp->buffer;
270 #if DEBUG
271 if(STp->write_pending)
272 STp->nbr_waits++;
273 else
274 STp->nbr_finished++;
275 #endif
277 down(&(STp->sem));
279 if(STbuffer->writing < STbuffer->buffer_bytes)
280 memcpy(STbuffer->b_data,
281 STbuffer->b_data + STbuffer->writing,
282 STbuffer->buffer_bytes - STbuffer->writing);
283 STbuffer->buffer_bytes -= STbuffer->writing;
284 if(STp->drv_block >=0) {
285 if(STp->block_size ==0)
286 STp->drv_block++;
287 else
288 STp->drv_block += STbuffer->writing / STp->block_size;
290 STbuffer->writing =0;
292 return;
296 /* Back over EOF if it has been inadvertently crossed (ioctl not used because
297 it messes up the block number). */
298 static int
299 back_over_eof(Scsi_Tape *STp)
301 Scsi_Cmnd *SCpnt;
302 unsigned char cmd[10];
304 cmd[0] = SPACE;
305 cmd[1] =0x01;/* Space FileMarks */
306 cmd[2] = cmd[3] = cmd[4] =0xff;/* -1 filemarks */
307 cmd[5] =0;
309 SCpnt =st_do_scsi(NULL, STp, cmd,0, ST_TIMEOUT, MAX_RETRIES);
310 if(!SCpnt)
311 return(-EBUSY);
313 SCpnt->request.rq_status = RQ_INACTIVE;
314 if((STp->buffer)->last_result !=0) {
315 printk(KERN_ERR "st%d: Backing over filemark failed.\n",TAPE_NR(STp->devt));
316 if((STp->mt_status)->mt_fileno >=0)
317 (STp->mt_status)->mt_fileno +=1;
318 (STp->mt_status)->mt_blkno =0;
321 return(STp->buffer)->last_result_fatal;
325 /* Flush the write buffer (never need to write if variable blocksize). */
326 static int
327 flush_write_buffer(Scsi_Tape *STp)
329 int offset, transfer, blks;
330 int result;
331 unsigned char cmd[10];
332 Scsi_Cmnd *SCpnt;
334 if((STp->buffer)->writing) {
335 write_behind_check(STp);
336 if((STp->buffer)->last_result_fatal) {
337 #if DEBUG
338 if(debugging)
339 printk(ST_DEB_MSG "st%d: Async write error (flush) %x.\n",
340 TAPE_NR(STp->devt), (STp->buffer)->last_result);
341 #endif
342 if((STp->buffer)->last_result == INT_MAX)
343 return(-ENOSPC);
344 return(-EIO);
348 if(STp->block_size ==0)
349 return0;
351 result =0;
352 if(STp->dirty ==1) {
354 offset = (STp->buffer)->buffer_bytes;
355 transfer = ((offset + STp->block_size -1) /
356 STp->block_size) * STp->block_size;
357 #if DEBUG
358 if(debugging)
359 printk(ST_DEB_MSG "st%d: Flushing %d bytes.\n",TAPE_NR(STp->devt), transfer);
360 #endif
361 memset((STp->buffer)->b_data + offset,0, transfer - offset);
363 memset(cmd,0,10);
364 cmd[0] = WRITE_6;
365 cmd[1] =1;
366 blks = transfer / STp->block_size;
367 cmd[2] = blks >>16;
368 cmd[3] = blks >>8;
369 cmd[4] = blks;
371 SCpnt =st_do_scsi(NULL, STp, cmd, transfer, ST_TIMEOUT, MAX_WRITE_RETRIES);
372 if(!SCpnt)
373 return(-EBUSY);
375 if((STp->buffer)->last_result_fatal !=0) {
376 printk(KERN_ERR "st%d: Error on flush.\n",TAPE_NR(STp->devt));
377 if((SCpnt->sense_buffer[0] &0x70) ==0x70&&
378 (SCpnt->sense_buffer[2] &0x40) &&
379 (SCpnt->sense_buffer[2] &0x0f) != VOLUME_OVERFLOW) {
380 STp->dirty =0;
381 (STp->buffer)->buffer_bytes =0;
382 result = (-ENOSPC);
384 else
385 result = (-EIO);
386 STp->drv_block = (-1);
388 else{
389 if(STp->drv_block >=0)
390 STp->drv_block += blks;
391 STp->dirty =0;
392 (STp->buffer)->buffer_bytes =0;
394 SCpnt->request.rq_status = RQ_INACTIVE;/* Mark as not busy */
396 return result;
400 /* Flush the tape buffer. The tape will be positioned correctly unless
401 seek_next is true. */
402 static int
403 flush_buffer(struct inode * inode,struct file * filp,int seek_next)
405 int backspace, result;
406 Scsi_Tape * STp;
407 ST_buffer * STbuffer;
408 int dev =TAPE_NR(inode->i_rdev);
410 STp = &(scsi_tapes[dev]);
411 STbuffer = STp->buffer;
414 * If there was a bus reset, block further access
415 * to this device.
417 if( STp->device->was_reset )
418 return(-EIO);
420 if(STp->ready != ST_READY)
421 return0;
423 if(STp->rw == ST_WRITING)/* Writing */
424 returnflush_write_buffer(STp);
426 if(STp->block_size ==0)
427 return0;
429 backspace = ((STp->buffer)->buffer_bytes +
430 (STp->buffer)->read_pointer) / STp->block_size -
431 ((STp->buffer)->read_pointer + STp->block_size -1) /
432 STp->block_size;
433 (STp->buffer)->buffer_bytes =0;
434 (STp->buffer)->read_pointer =0;
435 result =0;
436 if(!seek_next) {
437 if((STp->eof == ST_FM) && !STp->eof_hit) {
438 result =back_over_eof(STp);/* Back over the EOF hit */
439 if(!result) {
440 STp->eof = ST_NOEOF;
441 STp->eof_hit =0;
444 if(!result && backspace >0)
445 result =st_int_ioctl(inode, filp, MTBSR, backspace);
447 else if((STp->eof == ST_FM) && !STp->eof_hit) {
448 (STp->mt_status)->mt_fileno++;
449 STp->drv_block =0;
452 return result;
457 /* Open the device */
458 static int
459 scsi_tape_open(struct inode * inode,struct file * filp)
461 unsigned short flags;
462 int i, need_dma_buffer, new_session = FALSE, setting_failed;
463 unsigned char cmd[10];
464 Scsi_Cmnd * SCpnt;
465 Scsi_Tape * STp;
466 ST_mode * STm;
467 int dev =TAPE_NR(inode->i_rdev);
468 int mode =TAPE_MODE(inode->i_rdev);
470 if(dev >= st_template.dev_max || !scsi_tapes[dev].device)
471 return(-ENXIO);
472 STp = &(scsi_tapes[dev]);
473 if(STp->in_use) {
474 #if DEBUG
475 printk(ST_DEB_MSG "st%d: Device already in use.\n", dev);
476 #endif
477 return(-EBUSY);
479 STp->rew_at_close = (MINOR(inode->i_rdev) &0x80) ==0;
481 if(mode != STp->current_mode) {
482 #if DEBUG
483 if(debugging)
484 printk(ST_DEB_MSG "st%d: Mode change from %d to %d.\n",
485 dev, STp->current_mode, mode);
486 #endif
487 /* if (!STp->modes[mode].defined)
488 return (-ENXIO); */
489 new_session = TRUE;
490 STp->current_mode = mode;
492 STm = &(STp->modes[STp->current_mode]);
494 /* Allocate buffer for this user */
495 need_dma_buffer = STp->restr_dma;
496 for(i=0; i < st_nbr_buffers; i++)
497 if(!st_buffers[i]->in_use &&
498 (!need_dma_buffer || st_buffers[i]->dma))
499 break;
500 if(i >= st_nbr_buffers) {
501 STp->buffer =new_tape_buffer(FALSE, need_dma_buffer);
502 if(STp->buffer == NULL) {
503 printk(KERN_WARNING "st%d: Can't allocate tape buffer.\n", dev);
504 return(-EBUSY);
507 else
508 STp->buffer = st_buffers[i];
509 (STp->buffer)->in_use =1;
510 (STp->buffer)->writing =0;
511 STp->in_use =1;
513 flags = filp->f_flags;
514 STp->write_prot = ((flags & O_ACCMODE) == O_RDONLY);
516 STp->dirty =0;
517 STp->rw = ST_IDLE;
518 STp->ready = ST_READY;
519 if(STp->eof != ST_EOD)/* Save EOD across opens */
520 STp->eof = ST_NOEOF;
521 STp->eof_hit =0;
522 STp->recover_count =0;
523 #if DEBUG
524 STp->nbr_waits = STp->nbr_finished =0;
525 #endif
527 memset((void*) &cmd[0],0,10);
528 cmd[0] = TEST_UNIT_READY;
530 SCpnt =st_do_scsi(NULL, STp, cmd,0, ST_LONG_TIMEOUT, MAX_READY_RETRIES);
531 if(!SCpnt)
532 return(-EBUSY);
534 if((SCpnt->sense_buffer[0] &0x70) ==0x70&&
535 (SCpnt->sense_buffer[2] &0x0f) == UNIT_ATTENTION) {/* New media? */
536 (STp->mt_status)->mt_fileno =0;
537 memset((void*) &cmd[0],0,10);
538 cmd[0] = TEST_UNIT_READY;
540 SCpnt =st_do_scsi(SCpnt, STp, cmd,0, ST_LONG_TIMEOUT, MAX_READY_RETRIES);
542 (STp->mt_status)->mt_fileno = STp->drv_block =0;
543 STp->eof = ST_NOEOF;
544 (STp->device)->was_reset =0;
545 new_session = TRUE;
548 if((STp->buffer)->last_result_fatal !=0) {
549 if((SCpnt->sense_buffer[0] &0x70) ==0x70&&
550 (SCpnt->sense_buffer[2] &0x0f) == NO_TAPE) {
551 (STp->mt_status)->mt_fileno = STp->drv_block =0;
552 printk(KERN_NOTICE "st%d: No tape.\n", dev);
553 STp->ready = ST_NO_TAPE;
554 }else{
555 (STp->mt_status)->mt_fileno = STp->drv_block = (-1);
556 STp->ready = ST_NOT_READY;
558 SCpnt->request.rq_status = RQ_INACTIVE;/* Mark as not busy */
559 STp->density =0;/* Clear the erroneous "residue" */
560 STp->write_prot =0;
561 STp->block_size =0;
562 STp->eof = ST_NOEOF;
563 (STp->mt_status)->mt_fileno = STp->drv_block =0;
564 STp->door_locked = ST_UNLOCKED;
565 if(scsi_tapes[dev].device->host->hostt->usage_count)
566 (*scsi_tapes[dev].device->host->hostt->usage_count)++;
567 if(st_template.usage_count) (*st_template.usage_count)++;
568 return0;
571 memset((void*) &cmd[0],0,10);
572 cmd[0] = READ_BLOCK_LIMITS;
574 SCpnt =st_do_scsi(SCpnt, STp, cmd,6, ST_TIMEOUT, MAX_READY_RETRIES);
576 if(!SCpnt->result && !SCpnt->sense_buffer[0]) {
577 STp->max_block = ((STp->buffer)->b_data[1] <<16) |
578 ((STp->buffer)->b_data[2] <<8) | (STp->buffer)->b_data[3];
579 STp->min_block = ((STp->buffer)->b_data[4] <<8) |
580 (STp->buffer)->b_data[5];
581 #if DEBUG
582 if(debugging)
583 printk(ST_DEB_MSG "st%d: Block limits %d - %d bytes.\n", dev, STp->min_block,
584 STp->max_block);
585 #endif
587 else{
588 STp->min_block = STp->max_block = (-1);
589 #if DEBUG
590 if(debugging)
591 printk(ST_DEB_MSG "st%d: Can't read block limits.\n", dev);
592 #endif
595 memset((void*) &cmd[0],0,10);
596 cmd[0] = MODE_SENSE;
597 cmd[4] =12;
599 SCpnt =st_do_scsi(SCpnt, STp, cmd,12, ST_TIMEOUT, MAX_READY_RETRIES);
601 if((STp->buffer)->last_result_fatal !=0) {
602 #if DEBUG
603 if(debugging)
604 printk(ST_DEB_MSG "st%d: No Mode Sense.\n", dev);
605 #endif
606 STp->block_size = ST_DEFAULT_BLOCK;/* Educated guess (?) */
607 (STp->buffer)->last_result_fatal =0;/* Prevent error propagation */
608 STp->drv_write_prot =0;
610 else{
612 #if DEBUG
613 if(debugging)
614 printk(ST_DEB_MSG "st%d: Mode sense. Length %d, medium %x, WBS %x, BLL %d\n",
615 dev,
616 (STp->buffer)->b_data[0], (STp->buffer)->b_data[1],
617 (STp->buffer)->b_data[2], (STp->buffer)->b_data[3]);
618 #endif
620 if((STp->buffer)->b_data[3] >=8) {
621 STp->drv_buffer = ((STp->buffer)->b_data[2] >>4) &7;
622 STp->density = (STp->buffer)->b_data[4];
623 STp->block_size = (STp->buffer)->b_data[9] *65536+
624 (STp->buffer)->b_data[10] *256+ (STp->buffer)->b_data[11];
625 #if DEBUG
626 if(debugging)
627 printk(ST_DEB_MSG "st%d: Density %x, tape length: %x, drv buffer: %d\n",
628 dev, STp->density, (STp->buffer)->b_data[5] *65536+
629 (STp->buffer)->b_data[6] *256+ (STp->buffer)->b_data[7],
630 STp->drv_buffer);
631 #endif
634 if(STp->block_size > (STp->buffer)->buffer_size &&
635 !enlarge_buffer(STp->buffer, STp->block_size, STp->restr_dma)) {
636 printk(KERN_NOTICE "st%d: Blocksize %d too large for buffer.\n", dev,
637 STp->block_size);
638 (STp->buffer)->in_use =0;
639 STp->in_use =0;
640 return(-EIO);
642 STp->drv_write_prot = ((STp->buffer)->b_data[2] &0x80) !=0;
644 SCpnt->request.rq_status = RQ_INACTIVE;/* Mark as not busy */
646 if(STp->block_size >0)
647 (STp->buffer)->buffer_blocks = st_buffer_size / STp->block_size;
648 else
649 (STp->buffer)->buffer_blocks =1;
650 (STp->buffer)->buffer_bytes = (STp->buffer)->read_pointer =0;
652 #if DEBUG
653 if(debugging)
654 printk(ST_DEB_MSG "st%d: Block size: %d, buffer size: %d (%d blocks).\n", dev,
655 STp->block_size, (STp->buffer)->buffer_size,
656 (STp->buffer)->buffer_blocks);
657 #endif
659 if(STp->drv_write_prot) {
660 STp->write_prot =1;
661 #if DEBUG
662 if(debugging)
663 printk(ST_DEB_MSG "st%d: Write protected\n", dev);
664 #endif
665 if((flags & O_ACCMODE) == O_WRONLY || (flags & O_ACCMODE) == O_RDWR) {
666 (STp->buffer)->in_use =0;
667 STp->buffer =0;
668 STp->in_use =0;
669 return(-EROFS);
673 if(new_session) {
674 STp->density_changed = STp->blksize_changed = FALSE;
675 STp->compression_changed = FALSE;
676 if(!(STm->defaults_for_writes)) {
677 setting_failed = FALSE;
678 if(STm->default_blksize >=0&&
679 STm->default_blksize != STp->block_size) {
680 if(STm->default_density >=0&&
681 STm->default_density != STp->density)
682 STp->density = STm->default_density;/* Dirty trick! */
683 if(st_int_ioctl(inode, filp, MTSETBLK, STm->default_blksize)) {
684 printk(KERN_WARNING "st%d: Can't set default block size to %d bytes.\n",
685 dev, STm->default_blksize);
686 if(modes_defined)
687 setting_failed = TRUE;
690 if(STm->default_density >=0&&
691 STm->default_density != STp->density) {
692 if(st_int_ioctl(inode, filp, MTSETDENSITY, STm->default_density)) {
693 printk(KERN_WARNING "st%d: Can't set default density to %x.\n",
694 dev, STm->default_density);
695 if(modes_defined)
696 setting_failed = TRUE;
699 if(setting_failed) {
700 (STp->buffer)->in_use =0;
701 STp->buffer =0;
702 STp->in_use =0;
703 return(-EINVAL);
706 if(STp->default_drvbuffer !=0xff) {
707 if(st_int_ioctl(inode, filp, MTSETDRVBUFFER, STp->default_drvbuffer))
708 printk(KERN_WARNING "st%d: Can't set default drive buffering to %d.\n",
709 dev, STp->default_drvbuffer);
713 if(scsi_tapes[dev].device->host->hostt->usage_count)
714 (*scsi_tapes[dev].device->host->hostt->usage_count)++;
715 if(st_template.usage_count) (*st_template.usage_count)++;
717 return0;
721 /* Close the device*/
722 static void
723 scsi_tape_close(struct inode * inode,struct file * filp)
725 int result;
726 static unsigned char cmd[10];
727 Scsi_Cmnd * SCpnt;
728 Scsi_Tape * STp;
729 kdev_t devt = inode->i_rdev;
730 int dev;
732 dev =TAPE_NR(devt);
733 STp = &(scsi_tapes[dev]);
735 if( STp->rw == ST_WRITING && !(STp->device)->was_reset) {
737 result =flush_write_buffer(STp);
739 #if DEBUG
740 if(debugging) {
741 printk(ST_DEB_MSG "st%d: File length %ld bytes.\n",
742 dev, (long)(filp->f_pos));
743 printk(ST_DEB_MSG "st%d: Async write waits %d, finished %d.\n",
744 dev, STp->nbr_waits, STp->nbr_finished);
746 #endif
748 if(result ==0|| result == (-ENOSPC)) {
750 memset(cmd,0,10);
751 cmd[0] = WRITE_FILEMARKS;
752 cmd[4] =1+ STp->two_fm;
754 SCpnt =st_do_scsi(NULL, STp, cmd,0, ST_TIMEOUT, MAX_WRITE_RETRIES);
755 if(!SCpnt)
756 return;
758 SCpnt->request.rq_status = RQ_INACTIVE;/* Mark as not busy */
760 if((STp->buffer)->last_result_fatal !=0)
761 printk(KERN_ERR "st%d: Error on write filemark.\n", dev);
762 else{
763 if((STp->mt_status)->mt_fileno >=0)
764 (STp->mt_status)->mt_fileno++ ;
765 STp->drv_block =0;
766 if(STp->two_fm)
767 back_over_eof(STp);
771 #if DEBUG
772 if(debugging)
773 printk(ST_DEB_MSG "st%d: Buffer flushed, %d EOF(s) written\n",
774 dev, cmd[4]);
775 #endif
777 else if(!STp->rew_at_close) {
778 if(STp->can_bsr)
779 flush_buffer(inode, filp,0);
780 else if((STp->eof == ST_FM) && !STp->eof_hit)
781 back_over_eof(STp);
784 if(STp->rew_at_close)
785 st_int_ioctl(inode, filp, MTREW,1);
787 if(STp->door_locked == ST_LOCKED_AUTO)
788 st_int_ioctl(inode, filp, MTUNLOCK,0);
790 if(STp->buffer != NULL) {
791 normalize_buffer(STp->buffer);
792 (STp->buffer)->in_use =0;
794 STp->in_use =0;
796 if(scsi_tapes[dev].device->host->hostt->usage_count)
797 (*scsi_tapes[dev].device->host->hostt->usage_count)--;
798 if(st_template.usage_count) (*st_template.usage_count)--;
800 return;
804 /* Write command */
805 static int
806 st_write(struct inode * inode,struct file * filp,const char* buf,int count)
808 int total, do_count, blks, retval, transfer;
809 int write_threshold;
810 int doing_write =0;
811 static unsigned char cmd[10];
812 const char*b_point;
813 Scsi_Cmnd * SCpnt = NULL;
814 Scsi_Tape * STp;
815 ST_mode * STm;
816 int dev =TAPE_NR(inode->i_rdev);
818 STp = &(scsi_tapes[dev]);
819 if(STp->ready != ST_READY)
820 return(-EIO);
821 STm = &(STp->modes[STp->current_mode]);
822 if(!STm->defined)
823 return(-ENXIO);
826 * If there was a bus reset, block further access
827 * to this device.
829 if( STp->device->was_reset )
830 return(-EIO);
832 #if DEBUG
833 if(!STp->in_use) {
834 printk(ST_DEB_MSG "st%d: Incorrect device.\n", dev);
835 return(-EIO);
837 #endif
839 if(STp->write_prot)
840 return(-EACCES);
842 if(STp->block_size ==0&&
843 count > (STp->buffer)->buffer_size &&
844 !enlarge_buffer(STp->buffer, count, STp->restr_dma))
845 return(-EOVERFLOW);
847 if(STp->do_auto_lock && STp->door_locked == ST_UNLOCKED &&
848 !st_int_ioctl(inode, filp, MTLOCK,0))
849 STp->door_locked = ST_LOCKED_AUTO;
851 if(STp->rw == ST_READING) {
852 retval =flush_buffer(inode, filp,0);
853 if(retval)
854 return retval;
855 STp->rw = ST_WRITING;
857 else if(STp->rw != ST_WRITING) {
858 if(STm->defaults_for_writes &&
859 (STp->mt_status)->mt_fileno ==0&& STp->drv_block ==0) {
860 /* Force unless explicitly changed by the user */
861 if(!(STp->blksize_changed) && STm->default_blksize >=0&&
862 STp->block_size != STm->default_blksize) {
863 if(STm->default_density >=0&&
864 STm->default_density != STp->density)
865 STp->density = STm->default_density;/* Dirty trick! */
866 if(st_int_ioctl(inode, filp, MTSETBLK, STm->default_blksize)) {
867 printk(KERN_WARNING "st%d: Can't set default block size to %d bytes.\n",
868 dev, STm->default_blksize);
869 if(modes_defined)
870 return(-EINVAL);
873 if(!(STp->density_changed) && STm->default_density >=0&&
874 STm->default_density != STp->density) {
875 if(st_int_ioctl(inode, filp, MTSETDENSITY, STm->default_density)) {
876 printk(KERN_WARNING "st%d: Can't set default density %x.\n",
877 dev, STm->default_density);
878 if(modes_defined)
879 return(-EINVAL);
883 if(STm->default_compression != ST_DONT_TOUCH &&
884 !(STp->compression_changed)) {
885 if(st_compression(STp, (STm->default_compression == ST_YES))) {
886 printk(KERN_WARNING "st%d: Can't set default compression.\n",
887 dev);
888 if(modes_defined)
889 return(-EINVAL);
894 if(STp->moves_after_eof <255)
895 STp->moves_after_eof++;
897 if((STp->buffer)->writing) {
898 write_behind_check(STp);
899 if((STp->buffer)->last_result_fatal) {
900 #if DEBUG
901 if(debugging)
902 printk(ST_DEB_MSG "st%d: Async write error (write) %x.\n", dev,
903 (STp->buffer)->last_result);
904 #endif
905 if((STp->buffer)->last_result == INT_MAX) {
906 retval = (-ENOSPC);/* All has been written */
907 STp->eof = ST_EOM_OK;
909 else
910 retval = (-EIO);
911 return retval;
914 if(STp->eof == ST_EOM_OK)
915 return(-ENOSPC);
916 else if(STp->eof == ST_EOM_ERROR)
917 return(-EIO);
919 if(!STm->do_buffer_writes) {
920 if(STp->block_size !=0&& (count % STp->block_size) !=0)
921 return(-EIO);/* Write must be integral number of blocks */
922 write_threshold =1;
924 else
925 write_threshold = (STp->buffer)->buffer_blocks * STp->block_size;
926 if(!STm->do_async_writes)
927 write_threshold--;
929 total = count;
931 memset(cmd,0,10);
932 cmd[0] = WRITE_6;
933 cmd[1] = (STp->block_size !=0);
935 STp->rw = ST_WRITING;
937 b_point = buf;
938 while((STp->block_size ==0&& !STm->do_async_writes && count >0) ||
939 (STp->block_size !=0&&
940 (STp->buffer)->buffer_bytes + count > write_threshold))
942 doing_write =1;
943 if(STp->block_size ==0)
944 do_count = count;
945 else{
946 do_count = (STp->buffer)->buffer_blocks * STp->block_size -
947 (STp->buffer)->buffer_bytes;
948 if(do_count > count)
949 do_count = count;
951 memcpy_fromfs((STp->buffer)->b_data +
952 (STp->buffer)->buffer_bytes, b_point, do_count);
954 if(STp->block_size ==0)
955 blks = transfer = do_count;
956 else{
957 blks = ((STp->buffer)->buffer_bytes + do_count) /
958 STp->block_size;
959 transfer = blks * STp->block_size;
961 cmd[2] = blks >>16;
962 cmd[3] = blks >>8;
963 cmd[4] = blks;
965 SCpnt =st_do_scsi(SCpnt, STp, cmd, transfer, ST_TIMEOUT, MAX_WRITE_RETRIES);
966 if(!SCpnt)
967 return(-EBUSY);
969 if((STp->buffer)->last_result_fatal !=0) {
970 #if DEBUG
971 if(debugging)
972 printk(ST_DEB_MSG "st%d: Error on write:\n", dev);
973 #endif
974 if((SCpnt->sense_buffer[0] &0x70) ==0x70&&
975 (SCpnt->sense_buffer[2] &0x40)) {
976 if(STp->block_size !=0&& (SCpnt->sense_buffer[0] &0x80) !=0)
977 transfer = (SCpnt->sense_buffer[3] <<24) |
978 (SCpnt->sense_buffer[4] <<16) |
979 (SCpnt->sense_buffer[5] <<8) | SCpnt->sense_buffer[6];
980 else if(STp->block_size ==0&&
981 (SCpnt->sense_buffer[2] &0x0f) == VOLUME_OVERFLOW)
982 transfer = do_count;
983 else
984 transfer =0;
985 if(STp->block_size !=0)
986 transfer *= STp->block_size;
987 if(transfer <= do_count) {
988 filp->f_pos += do_count - transfer;
989 count -= do_count - transfer;
990 if(STp->drv_block >=0) {
991 if(STp->block_size ==0&& transfer < do_count)
992 STp->drv_block++;
993 else if(STp->block_size !=0)
994 STp->drv_block += (do_count - transfer) / STp->block_size;
996 STp->eof = ST_EOM_OK;
997 retval = (-ENOSPC);/* EOM within current request */
998 #if DEBUG
999 if(debugging)
1000 printk(ST_DEB_MSG "st%d: EOM with %d bytes unwritten.\n",
1001 dev, transfer);
1002 #endif
1004 else{
1005 STp->eof = ST_EOM_ERROR;
1006 STp->drv_block = (-1);/* Too cautious? */
1007 retval = (-EIO);/* EOM for old data */
1008 #if DEBUG
1009 if(debugging)
1010 printk(ST_DEB_MSG "st%d: EOM with lost data.\n", dev);
1011 #endif
1014 else{
1015 STp->drv_block = (-1);/* Too cautious? */
1016 retval = (-EIO);
1019 SCpnt->request.rq_status = RQ_INACTIVE;/* Mark as not busy */
1020 (STp->buffer)->buffer_bytes =0;
1021 STp->dirty =0;
1022 if(count < total)
1023 return total - count;
1024 else
1025 return retval;
1027 filp->f_pos += do_count;
1028 b_point += do_count;
1029 count -= do_count;
1030 if(STp->drv_block >=0) {
1031 if(STp->block_size ==0)
1032 STp->drv_block++;
1033 else
1034 STp->drv_block += blks;
1036 (STp->buffer)->buffer_bytes =0;
1037 STp->dirty =0;
1039 if(count !=0) {
1040 STp->dirty =1;
1041 memcpy_fromfs((STp->buffer)->b_data +
1042 (STp->buffer)->buffer_bytes,b_point,count);
1043 filp->f_pos += count;
1044 (STp->buffer)->buffer_bytes += count;
1045 count =0;
1048 if(doing_write && (STp->buffer)->last_result_fatal !=0) {
1049 SCpnt->request.rq_status = RQ_INACTIVE;
1050 return(STp->buffer)->last_result_fatal;
1053 if(STm->do_async_writes &&
1054 ((STp->buffer)->buffer_bytes >= STp->write_threshold ||
1055 STp->block_size ==0) ) {
1056 /* Schedule an asynchronous write */
1057 if(!SCpnt) {
1058 SCpnt =allocate_device(NULL, STp->device,1);
1059 if(!SCpnt)
1060 return(-EBUSY);
1062 if(STp->block_size ==0)
1063 (STp->buffer)->writing = (STp->buffer)->buffer_bytes;
1064 else
1065 (STp->buffer)->writing = ((STp->buffer)->buffer_bytes /
1066 STp->block_size) * STp->block_size;
1067 STp->dirty = !((STp->buffer)->writing ==
1068 (STp->buffer)->buffer_bytes);
1070 if(STp->block_size ==0)
1071 blks = (STp->buffer)->writing;
1072 else
1073 blks = (STp->buffer)->writing / STp->block_size;
1074 cmd[2] = blks >>16;
1075 cmd[3] = blks >>8;
1076 cmd[4] = blks;
1077 STp->sem = MUTEX_LOCKED;
1078 SCpnt->request.sem = &(STp->sem);
1079 SCpnt->request.rq_status = RQ_SCSI_BUSY;
1080 SCpnt->request.rq_dev = STp->devt;
1081 #if DEBUG
1082 STp->write_pending =1;
1083 #endif
1085 scsi_do_cmd(SCpnt,
1086 (void*) cmd, (STp->buffer)->b_data,
1087 (STp->buffer)->writing,
1088 st_sleep_done, ST_TIMEOUT, MAX_WRITE_RETRIES);
1090 else if(SCpnt != NULL)
1091 SCpnt->request.rq_status = RQ_INACTIVE;/* Mark as not busy */
1093 STp->at_sm &= (total !=0);
1094 return( total);
1098 /* Read command */
1099 static int
1100 st_read(struct inode * inode,struct file * filp,char* buf,int count)
1102 int total;
1103 int transfer, blks, bytes;
1104 static unsigned char cmd[10];
1105 Scsi_Cmnd * SCpnt = NULL;
1106 Scsi_Tape * STp;
1107 int dev =TAPE_NR(inode->i_rdev);
1109 STp = &(scsi_tapes[dev]);
1110 if(STp->ready != ST_READY)
1111 return(-EIO);
1112 if(!STp->modes[STp->current_mode].defined)
1113 return(-ENXIO);
1114 #if DEBUG
1115 if(!STp->in_use) {
1116 printk(ST_DEB_MSG "st%d: Incorrect device.\n", dev);
1117 return(-EIO);
1119 #endif
1121 if(STp->block_size ==0&&
1122 count > (STp->buffer)->buffer_size &&
1123 !enlarge_buffer(STp->buffer, count, STp->restr_dma))
1124 return(-EOVERFLOW);
1126 if(!(STp->do_read_ahead) && STp->block_size !=0&&
1127 (count % STp->block_size) !=0)
1128 return(-EIO);/* Read must be integral number of blocks */
1130 if(STp->do_auto_lock && STp->door_locked == ST_UNLOCKED &&
1131 !st_int_ioctl(inode, filp, MTLOCK,0))
1132 STp->door_locked = ST_LOCKED_AUTO;
1134 if(STp->rw == ST_WRITING) {
1135 transfer =flush_buffer(inode, filp,0);
1136 if(transfer)
1137 return transfer;
1138 STp->rw = ST_READING;
1140 if(STp->moves_after_eof <255)
1141 STp->moves_after_eof++;
1143 #if DEBUG
1144 if(debugging && STp->eof != ST_NOEOF)
1145 printk(ST_DEB_MSG "st%d: EOF flag up. Bytes %d\n", dev,
1146 (STp->buffer)->buffer_bytes);
1147 #endif
1148 if(((STp->buffer)->buffer_bytes ==0) &&
1149 (STp->eof == ST_EOM_OK || STp->eof == ST_EOD))
1150 return(-EIO);/* EOM or Blank Check */
1152 STp->rw = ST_READING;
1154 for(total =0; total < count; ) {
1156 if((STp->buffer)->buffer_bytes ==0&&
1157 STp->eof == ST_NOEOF) {
1159 memset(cmd,0,10);
1160 cmd[0] = READ_6;
1161 cmd[1] = (STp->block_size !=0);
1162 if(STp->block_size ==0)
1163 blks = bytes = count;
1164 else{
1165 if(STp->do_read_ahead) {
1166 blks = (STp->buffer)->buffer_blocks;
1167 bytes = blks * STp->block_size;
1169 else{
1170 bytes = count;
1171 if(bytes > (STp->buffer)->buffer_size)
1172 bytes = (STp->buffer)->buffer_size;
1173 blks = bytes / STp->block_size;
1174 bytes = blks * STp->block_size;
1177 cmd[2] = blks >>16;
1178 cmd[3] = blks >>8;
1179 cmd[4] = blks;
1181 SCpnt =st_do_scsi(SCpnt, STp, cmd, bytes, ST_TIMEOUT, MAX_RETRIES);
1182 if(!SCpnt)
1183 return(-EBUSY);
1185 (STp->buffer)->read_pointer =0;
1186 STp->eof_hit =0;
1187 STp->at_sm =0;
1189 if((STp->buffer)->last_result_fatal) {
1190 #if DEBUG
1191 if(debugging)
1192 printk(ST_DEB_MSG "st%d: Sense: %2x %2x %2x %2x %2x %2x %2x %2x\n",
1193 dev,
1194 SCpnt->sense_buffer[0], SCpnt->sense_buffer[1],
1195 SCpnt->sense_buffer[2], SCpnt->sense_buffer[3],
1196 SCpnt->sense_buffer[4], SCpnt->sense_buffer[5],
1197 SCpnt->sense_buffer[6], SCpnt->sense_buffer[7]);
1198 #endif
1199 if((SCpnt->sense_buffer[0] &0x70) ==0x70) {/* extended sense */
1201 if((SCpnt->sense_buffer[2] &0xe0) !=0) {/* EOF, EOM, or ILI */
1203 if((SCpnt->sense_buffer[0] &0x80) !=0)
1204 transfer = (SCpnt->sense_buffer[3] <<24) |
1205 (SCpnt->sense_buffer[4] <<16) |
1206 (SCpnt->sense_buffer[5] <<8) | SCpnt->sense_buffer[6];
1207 else
1208 transfer =0;
1209 if(STp->block_size ==0&&
1210 (SCpnt->sense_buffer[2] &0x0f) == MEDIUM_ERROR)
1211 transfer = bytes;
1213 if(SCpnt->sense_buffer[2] &0x20) {
1214 if(STp->block_size ==0) {
1215 if(transfer <=0)
1216 transfer =0;
1217 (STp->buffer)->buffer_bytes = bytes - transfer;
1219 else{
1220 printk(KERN_NOTICE "st%d: Incorrect block size.\n", dev);
1221 SCpnt->request.rq_status = RQ_INACTIVE;/* Mark as not busy */
1222 return(-EIO);
1225 else if(SCpnt->sense_buffer[2] &0x40) {
1226 STp->eof = ST_EOM_OK;
1227 if(STp->block_size ==0)
1228 (STp->buffer)->buffer_bytes = bytes - transfer;
1229 else
1230 (STp->buffer)->buffer_bytes =
1231 bytes - transfer * STp->block_size;
1232 #if DEBUG
1233 if(debugging)
1234 printk(ST_DEB_MSG "st%d: EOM detected (%d bytes read).\n", dev,
1235 (STp->buffer)->buffer_bytes);
1236 #endif
1238 else if(SCpnt->sense_buffer[2] &0x80) {
1239 STp->eof = ST_FM;
1240 if(STp->block_size ==0)
1241 (STp->buffer)->buffer_bytes =0;
1242 else
1243 (STp->buffer)->buffer_bytes =
1244 bytes - transfer * STp->block_size;
1245 #if DEBUG
1246 if(debugging)
1247 printk(ST_DEB_MSG
1248 "st%d: EOF detected (%d bytes read, transferred %d bytes).\n",
1249 dev, (STp->buffer)->buffer_bytes, total);
1250 #endif
1252 }/* end of EOF, EOM, ILI test */
1253 else{/* nonzero sense key */
1254 #if DEBUG
1255 if(debugging)
1256 printk(ST_DEB_MSG "st%d: Tape error while reading.\n", dev);
1257 #endif
1258 SCpnt->request.rq_status = RQ_INACTIVE;
1259 STp->drv_block = (-1);
1260 if(total)
1261 return total;
1262 else if(STp->moves_after_eof ==1&&
1263 (SCpnt->sense_buffer[2] &0x0f) == BLANK_CHECK) {
1264 #if DEBUG
1265 if(debugging)
1266 printk(ST_DEB_MSG
1267 "st%d: Zero returned for first BLANK CHECK after EOF.\n",
1268 dev);
1269 #endif
1270 STp->eof = ST_EOD;
1271 return0;/* First BLANK_CHECK after EOF */
1273 else
1274 return-EIO;
1276 }/* End of extended sense test */
1277 else{
1278 transfer = (STp->buffer)->last_result_fatal;
1279 SCpnt->request.rq_status = RQ_INACTIVE;/* Mark as not busy */
1280 return transfer;
1282 }/* End of error handling */
1283 else/* Read successful */
1284 (STp->buffer)->buffer_bytes = bytes;
1286 if(STp->drv_block >=0) {
1287 if(STp->block_size ==0)
1288 STp->drv_block++;
1289 else
1290 STp->drv_block += (STp->buffer)->buffer_bytes / STp->block_size;
1293 }/* if ((STp->buffer)->buffer_bytes == 0 &&
1294 STp->eof == ST_NOEOF) */
1296 if((STp->buffer)->buffer_bytes >0) {
1297 #if DEBUG
1298 if(debugging && STp->eof != ST_NOEOF)
1299 printk(ST_DEB_MSG "st%d: EOF up. Left %d, needed %d.\n", dev,
1300 (STp->buffer)->buffer_bytes, count - total);
1301 #endif
1302 transfer = (STp->buffer)->buffer_bytes < count - total ?
1303 (STp->buffer)->buffer_bytes : count - total;
1304 memcpy_tofs(buf, (STp->buffer)->b_data +
1305 (STp->buffer)->read_pointer,transfer);
1306 filp->f_pos += transfer;
1307 buf += transfer;
1308 total += transfer;
1309 (STp->buffer)->buffer_bytes -= transfer;
1310 (STp->buffer)->read_pointer += transfer;
1312 else if(STp->eof != ST_NOEOF) {
1313 STp->eof_hit =1;
1314 if(SCpnt != NULL)
1315 SCpnt->request.rq_status = RQ_INACTIVE;/* Mark as not busy */
1316 if(total ==0&& STp->eof == ST_FM) {
1317 STp->eof = ST_NOEOF;
1318 STp->drv_block =0;
1319 if(STp->moves_after_eof >1)
1320 STp->moves_after_eof =0;
1321 if((STp->mt_status)->mt_fileno >=0)
1322 (STp->mt_status)->mt_fileno++;
1324 if(total ==0&& STp->eof == ST_EOM_OK)
1325 return(-EIO);/* ST_EOM_ERROR not used in read */
1326 return total;
1329 if(STp->block_size ==0)
1330 count = total;/* Read only one variable length block */
1332 }/* for (total = 0; total < count; ) */
1334 if(SCpnt != NULL)
1335 SCpnt->request.rq_status = RQ_INACTIVE;/* Mark as not busy */
1337 return total;
1342 /* Set the driver options */
1343 static void
1344 st_log_options(Scsi_Tape *STp, ST_mode *STm,int dev)
1346 printk(KERN_INFO
1347 "st%d: Mode %d options: buffer writes: %d, async writes: %d, read ahead: %d\n",
1348 dev, STp->current_mode, STm->do_buffer_writes, STm->do_async_writes,
1349 STp->do_read_ahead);
1350 printk(KERN_INFO
1351 "st%d: can bsr: %d, two FMs: %d, fast mteom: %d, auto lock: %d, defs for wr: %d\n",
1352 dev, STp->can_bsr, STp->two_fm, STp->fast_mteom, STp->do_auto_lock,
1353 STm->defaults_for_writes);
1354 #if DEBUG
1355 printk(KERN_INFO
1356 "st%d: debugging: %d\n",
1357 dev, debugging);
1358 #endif
1362 static int
1363 st_set_options(struct inode * inode,long options)
1365 int value;
1366 long code;
1367 Scsi_Tape *STp;
1368 ST_mode *STm;
1369 int dev =TAPE_NR(inode->i_rdev);
1371 STp = &(scsi_tapes[dev]);
1372 STm = &(STp->modes[STp->current_mode]);
1373 if(!STm->defined) {
1374 memcpy(STm, &(STp->modes[0]),sizeof(ST_mode));
1375 modes_defined = TRUE;
1376 #if DEBUG
1377 if(debugging)
1378 printk(ST_DEB_MSG "st%d: Initialized mode %d definition from mode 0\n",
1379 dev, STp->current_mode);
1380 #endif
1383 code = options & MT_ST_OPTIONS;
1384 if(code == MT_ST_BOOLEANS) {
1385 STm->do_buffer_writes = (options & MT_ST_BUFFER_WRITES) !=0;
1386 STm->do_async_writes = (options & MT_ST_ASYNC_WRITES) !=0;
1387 STm->defaults_for_writes = (options & MT_ST_DEF_WRITES) !=0;
1388 STp->do_read_ahead = (options & MT_ST_READ_AHEAD) !=0;
1389 STp->two_fm = (options & MT_ST_TWO_FM) !=0;
1390 STp->fast_mteom = (options & MT_ST_FAST_MTEOM) !=0;
1391 STp->do_auto_lock = (options & MT_ST_AUTO_LOCK) !=0;
1392 STp->can_bsr = (options & MT_ST_CAN_BSR) !=0;
1393 #if DEBUG
1394 debugging = (options & MT_ST_DEBUGGING) !=0;
1395 #endif
1396 st_log_options(STp, STm, dev);
1398 else if(code == MT_ST_SETBOOLEANS || code == MT_ST_CLEARBOOLEANS) {
1399 value = (code == MT_ST_SETBOOLEANS);
1400 if((options & MT_ST_BUFFER_WRITES) !=0)
1401 STm->do_buffer_writes = value;
1402 if((options & MT_ST_ASYNC_WRITES) !=0)
1403 STm->do_async_writes = value;
1404 if((options & MT_ST_DEF_WRITES) !=0)
1405 STm->defaults_for_writes = value;
1406 if((options & MT_ST_READ_AHEAD) !=0)
1407 STp->do_read_ahead = value;
1408 if((options & MT_ST_TWO_FM) !=0)
1409 STp->two_fm = value;
1410 if((options & MT_ST_FAST_MTEOM) !=0)
1411 STp->fast_mteom = value;
1412 if((options & MT_ST_AUTO_LOCK) !=0)
1413 STp->do_auto_lock = value;
1414 if((options & MT_ST_CAN_BSR) !=0)
1415 STp->can_bsr = value;
1416 #if DEBUG
1417 if((options & MT_ST_DEBUGGING) !=0)
1418 debugging = value;
1419 #endif
1420 st_log_options(STp, STm, dev);
1422 else if(code == MT_ST_WRITE_THRESHOLD) {
1423 value = (options & ~MT_ST_OPTIONS) * ST_BLOCK_SIZE;
1424 if(value <1|| value > st_buffer_size) {
1425 printk(KERN_WARNING "st%d: Write threshold %d too small or too large.\n",
1426 dev, value);
1427 return(-EIO);
1429 STp->write_threshold = value;
1430 printk(KERN_INFO "st%d: Write threshold set to %d bytes.\n",
1431 dev, value);
1433 else if(code == MT_ST_DEF_BLKSIZE) {
1434 value = (options & ~MT_ST_OPTIONS);
1435 if(value == ~MT_ST_OPTIONS) {
1436 STm->default_blksize = (-1);
1437 printk(KERN_INFO "st%d: Default block size disabled.\n", dev);
1439 else{
1440 STm->default_blksize = value;
1441 printk(KERN_INFO "st%d: Default block size set to %d bytes.\n",
1442 dev, STm->default_blksize);
1445 else if(code == MT_ST_DEF_OPTIONS) {
1446 code = (options & ~MT_ST_CLEAR_DEFAULT);
1447 value = (options & MT_ST_CLEAR_DEFAULT);
1448 if(code == MT_ST_DEF_DENSITY) {
1449 if(value == MT_ST_CLEAR_DEFAULT) {
1450 STm->default_density = (-1);
1451 printk(KERN_INFO "st%d: Density default disabled.\n", dev);
1453 else{
1454 STm->default_density = value &0xff;
1455 printk(KERN_INFO "st%d: Density default set to %x\n",
1456 dev, STm->default_density);
1459 else if(code == MT_ST_DEF_DRVBUFFER) {
1460 if(value == MT_ST_CLEAR_DEFAULT) {
1461 STp->default_drvbuffer =0xff;
1462 printk(KERN_INFO "st%d: Drive buffer default disabled.\n", dev);
1464 else{
1465 STp->default_drvbuffer = value &7;
1466 printk(KERN_INFO "st%d: Drive buffer default set to %x\n",
1467 dev, STp->default_drvbuffer);
1470 else if(code == MT_ST_DEF_COMPRESSION) {
1471 if(value == MT_ST_CLEAR_DEFAULT) {
1472 STm->default_compression = ST_DONT_TOUCH;
1473 printk(KERN_INFO "st%d: Compression default disabled.\n", dev);
1475 else{
1476 STm->default_compression = (value &1? ST_YES : ST_NO);
1477 printk(KERN_INFO "st%d: Compression default set to %x\n",
1478 dev, (value &1));
1482 else
1483 return(-EIO);
1485 return0;
1489 #define COMPRESSION_PAGE 0x0f
1490 #define COMPRESSION_PAGE_LENGTH 16
1492 #define MODE_HEADER_LENGTH 4
1494 #define DCE_MASK 0x80
1495 #define DCC_MASK 0x40
1496 #define RED_MASK 0x60
1499 /* Control the compression with mode page 15. Algorithm not changed if zero. */
1500 static int
1501 st_compression(Scsi_Tape * STp,int state)
1503 int dev;
1504 unsigned char cmd[10];
1505 Scsi_Cmnd * SCpnt = NULL;
1507 /* Read the current page contents */
1508 memset(cmd,0,10);
1509 cmd[0] = MODE_SENSE;
1510 cmd[1] =8;
1511 cmd[2] = COMPRESSION_PAGE;
1512 cmd[4] = COMPRESSION_PAGE_LENGTH + MODE_HEADER_LENGTH;
1514 SCpnt =st_do_scsi(SCpnt, STp, cmd, cmd[4], ST_TIMEOUT,0);
1515 dev =TAPE_NR(SCpnt->request.rq_dev);
1517 if((STp->buffer)->last_result_fatal !=0) {
1518 #if DEBUG
1519 if(debugging)
1520 printk(ST_DEB_MSG "st%d: Compression mode page not supported.\n", dev);
1521 #endif
1522 SCpnt->request.rq_status = RQ_INACTIVE;/* Mark as not busy */
1523 return(-EIO);
1525 #if DEBUG
1526 if(debugging)
1527 printk(ST_DEB_MSG "st%d: Compression state is %d.\n", dev,
1528 ((STp->buffer)->b_data[MODE_HEADER_LENGTH +2] & DCE_MASK ?1:0));
1529 #endif
1531 /* Check if compression can be changed */
1532 if(((STp->buffer)->b_data[MODE_HEADER_LENGTH +2] & DCC_MASK) ==0) {
1533 #if DEBUG
1534 if(debugging)
1535 printk(ST_DEB_MSG "st%d: Compression not supported.\n", dev);
1536 #endif
1537 SCpnt->request.rq_status = RQ_INACTIVE;/* Mark as not busy */
1538 return(-EIO);
1541 /* Do the change */
1542 if(state)
1543 (STp->buffer)->b_data[MODE_HEADER_LENGTH +2] |= DCE_MASK;
1544 else
1545 (STp->buffer)->b_data[MODE_HEADER_LENGTH +2] &= ~DCE_MASK;
1547 memset(cmd,0,10);
1548 cmd[0] = MODE_SELECT;
1549 cmd[1] =0x10;
1550 cmd[4] = COMPRESSION_PAGE_LENGTH + MODE_HEADER_LENGTH;
1552 (STp->buffer)->b_data[0] =0;/* Reserved data length */
1553 (STp->buffer)->b_data[1] =0;/* Reserved media type byte */
1554 (STp->buffer)->b_data[MODE_HEADER_LENGTH] &=0x3f;
1555 SCpnt =st_do_scsi(SCpnt, STp, cmd, cmd[4], ST_TIMEOUT,0);
1557 if((STp->buffer)->last_result_fatal !=0) {
1558 #if DEBUG
1559 if(debugging)
1560 printk(ST_DEB_MSG "st%d: Compression change failed.\n", dev);
1561 #endif
1562 SCpnt->request.rq_status = RQ_INACTIVE;/* Mark as not busy */
1563 return(-EIO);
1566 #if DEBUG
1567 if(debugging)
1568 printk(ST_DEB_MSG "st%d: Compression state changed to %d.\n",
1569 dev, state);
1570 #endif
1572 SCpnt->request.rq_status = RQ_INACTIVE;/* Mark as not busy */
1573 STp->compression_changed = TRUE;
1574 return0;/* Not implemented yet */
1578 /* Internal ioctl function */
1579 static int
1580 st_int_ioctl(struct inode * inode,struct file * file,
1581 unsigned int cmd_in,unsigned long arg)
1583 int timeout = ST_LONG_TIMEOUT;
1584 long ltmp;
1585 int ioctl_result;
1586 unsigned char cmd[10];
1587 Scsi_Cmnd * SCpnt;
1588 Scsi_Tape * STp;
1589 int fileno, blkno, at_sm, undone, datalen;
1590 int dev =TAPE_NR(inode->i_rdev);
1592 STp = &(scsi_tapes[dev]);
1593 if(STp->ready != ST_READY && cmd_in != MTLOAD)
1594 return(-EIO);
1595 fileno = (STp->mt_status)->mt_fileno ;
1596 blkno = STp->drv_block;
1597 at_sm = STp->at_sm;
1599 memset(cmd,0,10);
1600 datalen =0;
1601 switch(cmd_in) {
1602 case MTFSF:
1603 case MTFSFM:
1604 cmd[0] = SPACE;
1605 cmd[1] =0x01;/* Space FileMarks */
1606 cmd[2] = (arg >>16);
1607 cmd[3] = (arg >>8);
1608 cmd[4] = arg;
1609 #if DEBUG
1610 if(debugging)
1611 printk(ST_DEB_MSG "st%d: Spacing tape forward over %d filemarks.\n",
1612 dev, cmd[2] *65536+ cmd[3] *256+ cmd[4]);
1613 #endif
1614 if(fileno >=0)
1615 fileno += arg;
1616 blkno =0;
1617 at_sm &= (arg !=0);
1618 break;
1619 case MTBSF:
1620 case MTBSFM:
1621 cmd[0] = SPACE;
1622 cmd[1] =0x01;/* Space FileMarks */
1623 ltmp = (-arg);
1624 cmd[2] = (ltmp >>16);
1625 cmd[3] = (ltmp >>8);
1626 cmd[4] = ltmp;
1627 #if DEBUG
1628 if(debugging) {
1629 if(cmd[2] &0x80)
1630 ltmp =0xff000000;
1631 ltmp = ltmp | (cmd[2] <<16) | (cmd[3] <<8) | cmd[4];
1632 printk(ST_DEB_MSG "st%d: Spacing tape backward over %ld filemarks.\n",
1633 dev, (-ltmp));
1635 #endif
1636 if(fileno >=0)
1637 fileno -= arg;
1638 blkno = (-1);/* We can't know the block number */
1639 at_sm &= (arg !=0);
1640 break;
1641 case MTFSR:
1642 cmd[0] = SPACE;
1643 cmd[1] =0x00;/* Space Blocks */
1644 cmd[2] = (arg >>16);
1645 cmd[3] = (arg >>8);
1646 cmd[4] = arg;
1647 #if DEBUG
1648 if(debugging)
1649 printk(ST_DEB_MSG "st%d: Spacing tape forward %d blocks.\n", dev,
1650 cmd[2] *65536+ cmd[3] *256+ cmd[4]);
1651 #endif
1652 if(blkno >=0)
1653 blkno += arg;
1654 at_sm &= (arg !=0);
1655 break;
1656 case MTBSR:
1657 cmd[0] = SPACE;
1658 cmd[1] =0x00;/* Space Blocks */
1659 ltmp = (-arg);
1660 cmd[2] = (ltmp >>16);
1661 cmd[3] = (ltmp >>8);
1662 cmd[4] = ltmp;
1663 #if DEBUG
1664 if(debugging) {
1665 if(cmd[2] &0x80)
1666 ltmp =0xff000000;
1667 ltmp = ltmp | (cmd[2] <<16) | (cmd[3] <<8) | cmd[4];
1668 printk(ST_DEB_MSG "st%d: Spacing tape backward %ld blocks.\n", dev, (-ltmp));
1670 #endif
1671 if(blkno >=0)
1672 blkno -= arg;
1673 at_sm &= (arg !=0);
1674 break;
1675 case MTFSS:
1676 cmd[0] = SPACE;
1677 cmd[1] =0x04;/* Space Setmarks */
1678 cmd[2] = (arg >>16);
1679 cmd[3] = (arg >>8);
1680 cmd[4] = arg;
1681 #if DEBUG
1682 if(debugging)
1683 printk(ST_DEB_MSG "st%d: Spacing tape forward %d setmarks.\n", dev,
1684 cmd[2] *65536+ cmd[3] *256+ cmd[4]);
1685 #endif
1686 if(arg !=0) {
1687 blkno = fileno = (-1);
1688 at_sm =1;
1690 break;
1691 case MTBSS:
1692 cmd[0] = SPACE;
1693 cmd[1] =0x04;/* Space Setmarks */
1694 ltmp = (-arg);
1695 cmd[2] = (ltmp >>16);
1696 cmd[3] = (ltmp >>8);
1697 cmd[4] = ltmp;
1698 #if DEBUG
1699 if(debugging) {
1700 if(cmd[2] &0x80)
1701 ltmp =0xff000000;
1702 ltmp = ltmp | (cmd[2] <<16) | (cmd[3] <<8) | cmd[4];
1703 printk(ST_DEB_MSG "st%d: Spacing tape backward %ld setmarks.\n",
1704 dev, (-ltmp));
1706 #endif
1707 if(arg !=0) {
1708 blkno = fileno = (-1);
1709 at_sm =1;
1711 break;
1712 case MTWEOF:
1713 case MTWSM:
1714 if(STp->write_prot)
1715 return(-EACCES);
1716 cmd[0] = WRITE_FILEMARKS;
1717 if(cmd_in == MTWSM)
1718 cmd[1] =2;
1719 cmd[2] = (arg >>16);
1720 cmd[3] = (arg >>8);
1721 cmd[4] = arg;
1722 timeout = ST_TIMEOUT;
1723 #if DEBUG
1724 if(debugging) {
1725 if(cmd_in == MTWEOF)
1726 printk(ST_DEB_MSG "st%d: Writing %d filemarks.\n", dev,
1727 cmd[2] *65536+ cmd[3] *256+ cmd[4]);
1728 else
1729 printk(ST_DEB_MSG "st%d: Writing %d setmarks.\n", dev,
1730 cmd[2] *65536+ cmd[3] *256+ cmd[4]);
1732 #endif
1733 if(fileno >=0)
1734 fileno += arg;
1735 blkno =0;
1736 at_sm = (cmd_in == MTWSM);
1737 break;
1738 case MTREW:
1739 cmd[0] = REZERO_UNIT;
1740 #if ST_NOWAIT
1741 cmd[1] =1;/* Don't wait for completion */
1742 timeout = ST_TIMEOUT;
1743 #endif
1744 #if DEBUG
1745 if(debugging)
1746 printk(ST_DEB_MSG "st%d: Rewinding tape.\n", dev);
1747 #endif
1748 fileno = blkno = at_sm =0;
1749 break;
1750 case MTOFFL:
1751 case MTLOAD:
1752 case MTUNLOAD:
1753 cmd[0] = START_STOP;
1754 if(cmd_in == MTLOAD)
1755 cmd[4] |=1;
1756 #if ST_NOWAIT
1757 cmd[1] =1;/* Don't wait for completion */
1758 timeout = ST_TIMEOUT;
1759 #else
1760 timeout = ST_LONG_TIMEOUT *8;
1761 #endif
1762 #if DEBUG
1763 if(debugging) {
1764 if(cmd_in != MTLOAD)
1765 printk(ST_DEB_MSG "st%d: Unloading tape.\n", dev);
1766 else
1767 printk(ST_DEB_MSG "st%d: Loading tape.\n", dev);
1769 #endif
1770 fileno = blkno = at_sm =0;
1771 break;
1772 case MTNOP:
1773 #if DEBUG
1774 if(debugging)
1775 printk(ST_DEB_MSG "st%d: No op on tape.\n", dev);
1776 #endif
1777 return0;/* Should do something ? */
1778 break;
1779 case MTRETEN:
1780 cmd[0] = START_STOP;
1781 #if ST_NOWAIT
1782 cmd[1] =1;/* Don't wait for completion */
1783 timeout = ST_TIMEOUT;
1784 #endif
1785 cmd[4] =3;
1786 #if DEBUG
1787 if(debugging)
1788 printk(ST_DEB_MSG "st%d: Retensioning tape.\n", dev);
1789 #endif
1790 fileno = blkno = at_sm =0;
1791 break;
1792 case MTEOM:
1793 if(!STp->fast_mteom) {
1794 /* space to the end of tape */
1795 ioctl_result =st_int_ioctl(inode, file, MTFSF,0x3fff);
1796 fileno = (STp->mt_status)->mt_fileno ;
1797 if(STp->eof == ST_EOD || STp->eof == ST_EOM_OK)
1798 return0;
1799 /* The next lines would hide the number of spaced FileMarks
1800 That's why I inserted the previous lines. I had no luck
1801 with detecting EOM with FSF, so we go now to EOM.
1802 Joerg Weule */
1804 else
1805 fileno = (-1);
1806 cmd[0] = SPACE;
1807 cmd[1] =3;
1808 #if DEBUG
1809 if(debugging)
1810 printk(ST_DEB_MSG "st%d: Spacing to end of recorded medium.\n", dev);
1811 #endif
1812 blkno =0;
1813 at_sm =0;
1814 break;
1815 case MTERASE:
1816 if(STp->write_prot)
1817 return(-EACCES);
1818 cmd[0] = ERASE;
1819 cmd[1] =1;/* To the end of tape */
1820 #if ST_NOWAIT
1821 cmd[1] |=2;/* Don't wait for completion */
1822 timeout = ST_TIMEOUT;
1823 #else
1824 timeout = ST_LONG_TIMEOUT *8;
1825 #endif
1826 #if DEBUG
1827 if(debugging)
1828 printk(ST_DEB_MSG "st%d: Erasing tape.\n", dev);
1829 #endif
1830 fileno = blkno = at_sm =0;
1831 break;
1832 case MTLOCK:
1833 cmd[0] = ALLOW_MEDIUM_REMOVAL;
1834 cmd[4] = SCSI_REMOVAL_PREVENT;
1835 #if DEBUG
1836 if(debugging)
1837 printk(ST_DEB_MSG "st%d: Locking drive door.\n", dev);
1838 #endif;
1839 break;
1840 case MTUNLOCK:
1841 cmd[0] = ALLOW_MEDIUM_REMOVAL;
1842 cmd[4] = SCSI_REMOVAL_ALLOW;
1843 #if DEBUG
1844 if(debugging)
1845 printk(ST_DEB_MSG "st%d: Unlocking drive door.\n", dev);
1846 #endif;
1847 break;
1848 case MTSEEK:
1849 if((STp->device)->scsi_level < SCSI_2) {
1850 cmd[0] = QFA_SEEK_BLOCK;
1851 cmd[2] = (arg >>16);
1852 cmd[3] = (arg >>8);
1853 cmd[4] = arg;
1854 cmd[5] =0;
1856 else{
1857 cmd[0] = SEEK_10;
1858 cmd[1] =4;
1859 cmd[3] = (arg >>24);
1860 cmd[4] = (arg >>16);
1861 cmd[5] = (arg >>8);
1862 cmd[6] = arg;
1864 #if ST_NOWAIT
1865 cmd[1] |=1;/* Don't wait for completion */
1866 timeout = ST_TIMEOUT;
1867 #endif
1868 #if DEBUG
1869 if(debugging)
1870 printk(ST_DEB_MSG "st%d: Seeking tape to block %ld.\n", dev, arg);
1871 #endif
1872 fileno = blkno = (-1);
1873 at_sm =0;
1874 break;
1875 case MTSETBLK:/* Set block length */
1876 case MTSETDENSITY:/* Set tape density */
1877 case MTSETDRVBUFFER:/* Set drive buffering */
1878 if(STp->dirty || (STp->buffer)->buffer_bytes !=0)
1879 return(-EIO);/* Not allowed if data in buffer */
1880 if(cmd_in == MTSETBLK &&
1881 arg !=0&&
1882 (arg < STp->min_block || arg > STp->max_block ||
1883 arg > st_buffer_size)) {
1884 printk(KERN_WARNING "st%d: Illegal block size.\n", dev);
1885 return(-EINVAL);
1887 cmd[0] = MODE_SELECT;
1888 cmd[4] = datalen =12;
1890 memset((STp->buffer)->b_data,0,12);
1891 if(cmd_in == MTSETDRVBUFFER)
1892 (STp->buffer)->b_data[2] = (arg &7) <<4;
1893 else
1894 (STp->buffer)->b_data[2] =
1895 STp->drv_buffer <<4;
1896 (STp->buffer)->b_data[3] =8;/* block descriptor length */
1897 if(cmd_in == MTSETDENSITY) {
1898 (STp->buffer)->b_data[4] = arg;
1899 STp->density_changed = TRUE;/* At least we tried ;-) */
1901 else
1902 (STp->buffer)->b_data[4] = STp->density;
1903 if(cmd_in == MTSETBLK) {
1904 ltmp = arg;
1905 STp->blksize_changed = TRUE;/* At least we tried ;-) */
1907 else
1908 ltmp = STp->block_size;
1909 (STp->buffer)->b_data[9] = (ltmp >>16);
1910 (STp->buffer)->b_data[10] = (ltmp >>8);
1911 (STp->buffer)->b_data[11] = ltmp;
1912 timeout = ST_TIMEOUT;
1913 #if DEBUG
1914 if(debugging) {
1915 if(cmd_in == MTSETBLK)
1916 printk(ST_DEB_MSG "st%d: Setting block size to %d bytes.\n", dev,
1917 (STp->buffer)->b_data[9] *65536+
1918 (STp->buffer)->b_data[10] *256+
1919 (STp->buffer)->b_data[11]);
1920 else if(cmd_in == MTSETDENSITY)
1921 printk(ST_DEB_MSG "st%d: Setting density code to %x.\n", dev,
1922 (STp->buffer)->b_data[4]);
1923 else
1924 printk(ST_DEB_MSG "st%d: Setting drive buffer code to %d.\n", dev,
1925 ((STp->buffer)->b_data[2] >>4) &7);
1927 #endif
1928 break;
1929 default:
1930 return(-ENOSYS);
1933 SCpnt =st_do_scsi(NULL, STp, cmd, datalen, timeout, MAX_RETRIES);
1934 if(!SCpnt)
1935 return(-EBUSY);
1937 ioctl_result = (STp->buffer)->last_result_fatal;
1939 SCpnt->request.rq_status = RQ_INACTIVE;/* Mark as not busy */
1941 if(cmd_in == MTFSF)
1942 STp->moves_after_eof =0;
1943 else
1944 STp->moves_after_eof =1;
1945 if(!ioctl_result) {/* SCSI command successful */
1946 if(cmd_in != MTSEEK) {
1947 STp->drv_block = blkno;
1948 (STp->mt_status)->mt_fileno = fileno;
1949 STp->at_sm = at_sm;
1951 else{
1952 STp->drv_block = (STp->mt_status)->mt_fileno = (-1);
1953 STp->at_sm =0;
1955 if(cmd_in == MTLOCK)
1956 STp->door_locked = ST_LOCKED_EXPLICIT;
1957 else if(cmd_in == MTUNLOCK)
1958 STp->door_locked = ST_UNLOCKED;
1959 if(cmd_in == MTBSFM)
1960 ioctl_result =st_int_ioctl(inode, file, MTFSF,1);
1961 else if(cmd_in == MTFSFM)
1962 ioctl_result =st_int_ioctl(inode, file, MTBSF,1);
1963 else if(cmd_in == MTSETBLK) {
1964 STp->block_size = arg;
1965 if(arg !=0)
1966 (STp->buffer)->buffer_blocks =
1967 (STp->buffer)->buffer_size / STp->block_size;
1968 (STp->buffer)->buffer_bytes = (STp->buffer)->read_pointer =0;
1970 else if(cmd_in == MTSETDRVBUFFER)
1971 STp->drv_buffer = (arg &7);
1972 else if(cmd_in == MTSETDENSITY)
1973 STp->density = arg;
1974 else if(cmd_in == MTEOM) {
1975 STp->eof = ST_EOD;
1976 STp->eof_hit =0;
1978 else if(cmd_in != MTSETBLK && cmd_in != MTNOP) {
1979 STp->eof = ST_NOEOF;
1980 STp->eof_hit =0;
1982 if(cmd_in == MTOFFL || cmd_in == MTUNLOAD)
1983 STp->rew_at_close =0;
1984 else if(cmd_in == MTLOAD)
1985 STp->rew_at_close = (MINOR(inode->i_rdev) &0x80) ==0;
1986 }else{/* SCSI command was not completely successful */
1987 if(SCpnt->sense_buffer[2] &0x40) {
1988 if(cmd_in != MTBSF && cmd_in != MTBSFM &&
1989 cmd_in != MTBSR && cmd_in != MTBSS)
1990 STp->eof = ST_EOM_OK;
1991 STp->eof_hit =0;
1992 STp->drv_block =0;
1994 undone = (
1995 (SCpnt->sense_buffer[3] <<24) +
1996 (SCpnt->sense_buffer[4] <<16) +
1997 (SCpnt->sense_buffer[5] <<8) +
1998 SCpnt->sense_buffer[6] );
1999 if( (cmd_in == MTFSF) || (cmd_in == MTFSFM) ) {
2000 if(fileno >=0)
2001 (STp->mt_status)->mt_fileno = fileno - undone ;
2002 else
2003 (STp->mt_status)->mt_fileno = fileno;
2004 STp->drv_block =0;
2006 else if( (cmd_in == MTBSF) || (cmd_in == MTBSFM) ) {
2007 (STp->mt_status)->mt_fileno = fileno + undone ;
2008 STp->drv_block =0;
2010 else if(cmd_in == MTFSR) {
2011 if(SCpnt->sense_buffer[2] &0x80) {/* Hit filemark */
2012 (STp->mt_status)->mt_fileno++;
2013 STp->drv_block =0;
2015 else{
2016 if(blkno >= undone)
2017 STp->drv_block = blkno - undone;
2018 else
2019 STp->drv_block = (-1);
2022 else if(cmd_in == MTBSR) {
2023 if(SCpnt->sense_buffer[2] &0x80) {/* Hit filemark */
2024 (STp->mt_status)->mt_fileno--;
2025 STp->drv_block = (-1);
2027 else{
2028 if(blkno >=0)
2029 STp->drv_block = blkno + undone;
2030 else
2031 STp->drv_block = (-1);
2034 else if(cmd_in == MTEOM || cmd_in == MTSEEK) {
2035 (STp->mt_status)->mt_fileno = (-1);
2036 STp->drv_block = (-1);
2038 if(STp->eof == ST_NOEOF &&
2039 (SCpnt->sense_buffer[2] &0x0f) == BLANK_CHECK)
2040 STp->eof = ST_EOD;
2041 if(cmd_in == MTLOCK)
2042 STp->door_locked = ST_LOCK_FAILS;
2045 return ioctl_result;
2050 /* The ioctl command */
2051 static int
2052 st_ioctl(struct inode * inode,struct file * file,
2053 unsigned int cmd_in,unsigned long arg)
2055 int i, cmd_nr, cmd_type, result;
2056 struct mtop mtc;
2057 struct mtpos mt_pos;
2058 unsigned char scmd[10];
2059 Scsi_Cmnd *SCpnt;
2060 Scsi_Tape *STp;
2061 ST_mode *STm;
2062 int dev =TAPE_NR(inode->i_rdev);
2064 STp = &(scsi_tapes[dev]);
2065 #if DEBUG
2066 if(debugging && !STp->in_use) {
2067 printk(ST_DEB_MSG "st%d: Incorrect device.\n", dev);
2068 return(-EIO);
2070 #endif
2071 STm = &(STp->modes[STp->current_mode]);
2074 * If this is something intended for the lower layer, just pass it
2075 * through.
2077 if( cmd_in == SCSI_IOCTL_GET_IDLUN || cmd_in == SCSI_IOCTL_PROBE_HOST )
2079 returnscsi_ioctl(STp->device, cmd_in, (void*) arg);
2082 cmd_type =_IOC_TYPE(cmd_in);
2083 cmd_nr =_IOC_NR(cmd_in);
2084 if(cmd_type ==_IOC_TYPE(MTIOCTOP) && cmd_nr ==_IOC_NR(MTIOCTOP)) {
2085 if(_IOC_SIZE(cmd_in) !=sizeof(mtc))
2086 return(-EINVAL);
2088 i =verify_area(VERIFY_READ, (void*)arg,sizeof(mtc));
2089 if(i)
2090 return i;
2092 memcpy_fromfs((char*) &mtc, (char*)arg,sizeof(struct mtop));
2094 if(mtc.mt_op == MTSETDRVBUFFER && !suser()) {
2095 printk(KERN_WARNING "st%d: MTSETDRVBUFFER only allowed for root.\n", dev);
2096 return(-EPERM);
2098 if(!STm->defined &&
2099 (mtc.mt_op != MTSETDRVBUFFER && (mtc.mt_count & MT_ST_OPTIONS) ==0))
2100 return(-ENXIO);
2102 if(!(STp->device)->was_reset) {
2104 if(STp->eof_hit) {
2105 if(mtc.mt_op == MTFSF || mtc.mt_op == MTEOM) {
2106 mtc.mt_count -=1;
2107 (STp->mt_status)->mt_fileno +=1;
2109 else if(mtc.mt_op == MTBSF) {
2110 mtc.mt_count +=1;
2111 (STp->mt_status)->mt_fileno +=1;
2115 i =flush_buffer(inode, file, mtc.mt_op == MTSEEK ||
2116 mtc.mt_op == MTREW || mtc.mt_op == MTOFFL ||
2117 mtc.mt_op == MTRETEN || mtc.mt_op == MTEOM ||
2118 mtc.mt_op == MTLOCK || mtc.mt_op == MTLOAD ||
2119 mtc.mt_op == MTCOMPRESSION);
2120 if(i <0)
2121 return i;
2123 else{
2125 * If there was a bus reset, block further access
2126 * to this device. If the user wants to rewind the tape,
2127 * then reset the flag and allow access again.
2129 if(mtc.mt_op != MTREW &&
2130 mtc.mt_op != MTOFFL &&
2131 mtc.mt_op != MTRETEN &&
2132 mtc.mt_op != MTERASE &&
2133 mtc.mt_op != MTSEEK &&
2134 mtc.mt_op != MTEOM)
2135 return(-EIO);
2136 STp->device->was_reset =0;
2137 if(STp->door_locked != ST_UNLOCKED &&
2138 STp->door_locked != ST_LOCK_FAILS) {
2139 if(st_int_ioctl(inode, file, MTLOCK,0)) {
2140 printk(KERN_NOTICE "st%d: Could not relock door after bus reset.\n",
2141 dev);
2142 STp->door_locked = ST_UNLOCKED;
2147 if(mtc.mt_op != MTNOP && mtc.mt_op != MTSETBLK &&
2148 mtc.mt_op != MTSETDENSITY && mtc.mt_op != MTWSM &&
2149 mtc.mt_op != MTSETDRVBUFFER)
2150 STp->rw = ST_IDLE;/* Prevent automatic WEOF */
2152 if(mtc.mt_op == MTOFFL && STp->door_locked != ST_UNLOCKED)
2153 st_int_ioctl(inode, file, MTUNLOCK,0);/* Ignore result! */
2155 if(mtc.mt_op == MTSETDRVBUFFER &&
2156 (mtc.mt_count & MT_ST_OPTIONS) !=0)
2157 returnst_set_options(inode, mtc.mt_count);
2158 else if(mtc.mt_op == MTCOMPRESSION)
2159 returnst_compression(STp, (mtc.mt_count &1));
2160 else
2161 returnst_int_ioctl(inode, file, mtc.mt_op, mtc.mt_count);
2164 if(!STm->defined)
2165 return(-ENXIO);
2167 if(cmd_type ==_IOC_TYPE(MTIOCGET) && cmd_nr ==_IOC_NR(MTIOCGET)) {
2169 if(_IOC_SIZE(cmd_in) !=sizeof(struct mtget))
2170 return(-EINVAL);
2171 i =verify_area(VERIFY_WRITE, (void*)arg,sizeof(struct mtget));
2172 if(i)
2173 return i;
2175 i =flush_buffer(inode, file, FALSE);
2176 if(i <0)
2177 return i;
2179 (STp->mt_status)->mt_dsreg =
2180 ((STp->block_size << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK) |
2181 ((STp->density << MT_ST_DENSITY_SHIFT) & MT_ST_DENSITY_MASK);
2182 (STp->mt_status)->mt_blkno = STp->drv_block;
2183 if(STp->block_size !=0) {
2184 if(STp->rw == ST_WRITING)
2185 (STp->mt_status)->mt_blkno +=
2186 (STp->buffer)->buffer_bytes / STp->block_size;
2187 else if(STp->rw == ST_READING)
2188 (STp->mt_status)->mt_blkno -= ((STp->buffer)->buffer_bytes +
2189 STp->block_size -1) / STp->block_size;
2192 (STp->mt_status)->mt_gstat =0;
2193 if(STp->drv_write_prot)
2194 (STp->mt_status)->mt_gstat |=GMT_WR_PROT(0xffffffff);
2195 if((STp->mt_status)->mt_blkno ==0) {
2196 if((STp->mt_status)->mt_fileno ==0)
2197 (STp->mt_status)->mt_gstat |=GMT_BOT(0xffffffff);
2198 else
2199 (STp->mt_status)->mt_gstat |=GMT_EOF(0xffffffff);
2201 if(STp->eof == ST_EOM_OK || STp->eof == ST_EOM_ERROR)
2202 (STp->mt_status)->mt_gstat |=GMT_EOT(0xffffffff);
2203 else if(STp->eof == ST_EOD)
2204 (STp->mt_status)->mt_gstat |=GMT_EOD(0xffffffff);
2205 if(STp->density ==1)
2206 (STp->mt_status)->mt_gstat |=GMT_D_800(0xffffffff);
2207 else if(STp->density ==2)
2208 (STp->mt_status)->mt_gstat |=GMT_D_1600(0xffffffff);
2209 else if(STp->density ==3)
2210 (STp->mt_status)->mt_gstat |=GMT_D_6250(0xffffffff);
2211 if(STp->ready == ST_READY)
2212 (STp->mt_status)->mt_gstat |=GMT_ONLINE(0xffffffff);
2213 if(STp->ready == ST_NO_TAPE)
2214 (STp->mt_status)->mt_gstat |=GMT_DR_OPEN(0xffffffff);
2215 if(STp->at_sm)
2216 (STp->mt_status)->mt_gstat |=GMT_SM(0xffffffff);
2218 memcpy_tofs((char*)arg, (char*)(STp->mt_status),
2219 sizeof(struct mtget));
2221 (STp->mt_status)->mt_erreg =0;/* Clear after read */
2222 return0;
2224 else if(cmd_type ==_IOC_TYPE(MTIOCPOS) && cmd_nr ==_IOC_NR(MTIOCPOS)) {
2225 if(STp->ready != ST_READY)
2226 return(-EIO);
2227 #if DEBUG
2228 if(debugging)
2229 printk(ST_DEB_MSG "st%d: get tape position.\n", dev);
2230 #endif
2231 if(_IOC_SIZE(cmd_in) !=sizeof(struct mtpos))
2232 return(-EINVAL);
2234 i =flush_buffer(inode, file,0);
2235 if(i <0)
2236 return i;
2238 i =verify_area(VERIFY_WRITE, (void*)arg,sizeof(struct mtpos));
2239 if(i)
2240 return i;
2242 memset(scmd,0,10);
2243 if((STp->device)->scsi_level < SCSI_2) {
2244 scmd[0] = QFA_REQUEST_BLOCK;
2245 scmd[4] =3;
2247 else{
2248 scmd[0] = READ_POSITION;
2249 scmd[1] =1;
2251 SCpnt =st_do_scsi(NULL, STp, scmd,20, ST_TIMEOUT, MAX_READY_RETRIES);
2252 if(!SCpnt)
2253 return(-EBUSY);
2255 if((STp->buffer)->last_result_fatal !=0) {
2256 mt_pos.mt_blkno = (-1);
2257 #if DEBUG
2258 if(debugging)
2259 printk(ST_DEB_MSG "st%d: Can't read tape position.\n", dev);
2260 #endif
2261 result = (-EIO);
2263 else{
2264 result =0;
2265 if((STp->device)->scsi_level < SCSI_2)
2266 mt_pos.mt_blkno = ((STp->buffer)->b_data[0] <<16)
2267 + ((STp->buffer)->b_data[1] <<8)
2268 + (STp->buffer)->b_data[2];
2269 else
2270 mt_pos.mt_blkno = ((STp->buffer)->b_data[4] <<24)
2271 + ((STp->buffer)->b_data[5] <<16)
2272 + ((STp->buffer)->b_data[6] <<8)
2273 + (STp->buffer)->b_data[7];
2277 SCpnt->request.rq_status = RQ_INACTIVE;/* Mark as not busy */
2279 memcpy_tofs((char*)arg, (char*) (&mt_pos),sizeof(struct mtpos));
2280 return result;
2282 else
2283 returnscsi_ioctl(STp->device, cmd_in, (void*) arg);
2287 /* Try to allocate a new tape buffer */
2288 static ST_buffer *
2289 new_tape_buffer(int from_initialization,int need_dma )
2291 int priority, a_size;
2292 ST_buffer *tb;
2294 if(st_nbr_buffers >= st_template.dev_max)
2295 return NULL;/* Should never happen */
2297 if(from_initialization) {
2298 priority = GFP_ATOMIC;
2299 a_size = st_buffer_size;
2301 else{
2302 priority = GFP_KERNEL;
2303 for(a_size = PAGE_SIZE; a_size < st_buffer_size; a_size <<=1)
2304 ;/* Make sure we allocate efficiently */
2306 tb = (ST_buffer *)scsi_init_malloc(sizeof(ST_buffer), priority);
2307 if(tb) {
2308 if(need_dma)
2309 priority |= GFP_DMA;
2310 tb->b_data = (unsigned char*)scsi_init_malloc(a_size, priority);
2311 if(!tb->b_data) {
2312 scsi_init_free((char*)tb,sizeof(ST_buffer));
2313 tb = NULL;
2316 if(!tb) {
2317 printk(KERN_NOTICE "st: Can't allocate new tape buffer (nbr %d).\n",
2318 st_nbr_buffers);
2319 return NULL;
2321 #if DEBUG
2322 if(debugging)
2323 printk(ST_DEB_MSG
2324 "st: Allocated tape buffer %d (%d bytes, dma: %d, a: %p).\n",
2325 st_nbr_buffers, a_size, need_dma, tb->b_data);
2326 #endif
2327 tb->in_use =0;
2328 tb->dma = need_dma;
2329 tb->buffer_size = a_size;
2330 tb->writing =0;
2331 tb->orig_b_data = NULL;
2332 st_buffers[st_nbr_buffers++] = tb;
2333 return tb;
2337 /* Try to allocate a temporary enlarged tape buffer */
2338 static int
2339 enlarge_buffer(ST_buffer *STbuffer,int new_size,int need_dma)
2341 int a_size, priority;
2342 unsigned char*tbd;
2344 normalize_buffer(STbuffer);
2346 for(a_size = PAGE_SIZE; a_size < new_size; a_size <<=1)
2347 ;/* Make sure that we allocate efficiently */
2349 priority = GFP_KERNEL;
2350 if(need_dma)
2351 priority |= GFP_DMA;
2352 tbd = (unsigned char*)scsi_init_malloc(a_size, priority);
2353 if(!tbd)
2354 return FALSE;
2355 #if DEBUG
2356 if(debugging)
2357 printk(ST_DEB_MSG
2358 "st: Buffer at %p enlarged to %d bytes (dma: %d, a: %p).\n",
2359 STbuffer->b_data, a_size, need_dma, tbd);
2360 #endif
2362 STbuffer->orig_b_data = STbuffer->b_data;
2363 STbuffer->orig_size = STbuffer->buffer_size;
2364 STbuffer->b_data = tbd;
2365 STbuffer->buffer_size = a_size;
2366 return TRUE;
2370 /* Release the extra buffer */
2371 static void
2372 normalize_buffer(ST_buffer *STbuffer)
2374 if(STbuffer->orig_b_data == NULL)
2375 return;
2377 scsi_init_free(STbuffer->b_data, STbuffer->buffer_size);
2378 STbuffer->b_data = STbuffer->orig_b_data;
2379 STbuffer->orig_b_data = NULL;
2380 STbuffer->buffer_size = STbuffer->orig_size;
2382 #if DEBUG
2383 if(debugging)
2384 printk(ST_DEB_MSG "st: Buffer at %p normalized to %d bytes.\n",
2385 STbuffer->b_data, STbuffer->buffer_size);
2386 #endif
2390 /* Set the boot options. Syntax: st=xxx,yyy
2391 where xxx is buffer size in 1024 byte blocks and yyy is write threshold
2392 in 1024 byte blocks. */
2393 void
2394 st_setup(char*str,int*ints)
2396 if(ints[0] >0&& ints[1] >0)
2397 st_buffer_size = ints[1] * ST_BLOCK_SIZE;
2398 if(ints[0] >1&& ints[2] >0) {
2399 st_write_threshold = ints[2] * ST_BLOCK_SIZE;
2400 if(st_write_threshold > st_buffer_size)
2401 st_write_threshold = st_buffer_size;
2403 if(ints[0] >2&& ints[3] >0)
2404 st_max_buffers = ints[3];
2408 static struct file_operations st_fops = {
2409 NULL,/* lseek - default */
2410 st_read,/* read - general block-dev read */
2411 st_write,/* write - general block-dev write */
2412 NULL,/* readdir - bad */
2413 NULL,/* select */
2414 st_ioctl,/* ioctl */
2415 NULL,/* mmap */
2416 scsi_tape_open,/* open */
2417 scsi_tape_close,/* release */
2418 NULL /* fsync */
2421 static intst_attach(Scsi_Device * SDp){
2422 Scsi_Tape * tpnt;
2423 int i;
2425 if(SDp->type != TYPE_TAPE)return1;
2427 if(st_template.nr_dev >= st_template.dev_max)
2429 SDp->attached--;
2430 return1;
2433 for(tpnt = scsi_tapes, i=0; i<st_template.dev_max; i++, tpnt++)
2434 if(!tpnt->device)break;
2436 if(i >= st_template.dev_max)panic("scsi_devices corrupt (st)");
2438 scsi_tapes[i].device = SDp;
2439 if(SDp->scsi_level <=2)
2440 scsi_tapes[i].mt_status->mt_type = MT_ISSCSI1;
2441 else
2442 scsi_tapes[i].mt_status->mt_type = MT_ISSCSI2;
2444 tpnt->devt =MKDEV(SCSI_TAPE_MAJOR, i);
2445 tpnt->dirty =0;
2446 tpnt->rw = ST_IDLE;
2447 tpnt->eof = ST_NOEOF;
2448 tpnt->waiting = NULL;
2449 tpnt->in_use =0;
2450 tpnt->drv_buffer =1;/* Try buffering if no mode sense */
2451 tpnt->restr_dma = (SDp->host)->unchecked_isa_dma;
2452 tpnt->density =0;
2453 tpnt->do_read_ahead = ST_READ_AHEAD;
2454 tpnt->do_auto_lock = ST_AUTO_LOCK;
2455 tpnt->can_bsr = ST_IN_FILE_POS;
2456 tpnt->two_fm = ST_TWO_FM;
2457 tpnt->fast_mteom = ST_FAST_MTEOM;
2458 tpnt->write_threshold = st_write_threshold;
2459 tpnt->default_drvbuffer =0xff;/* No forced buffering */
2460 tpnt->drv_block = (-1);
2461 tpnt->moves_after_eof =1;
2462 tpnt->at_sm =0;
2463 (tpnt->mt_status)->mt_fileno = (tpnt->mt_status)->mt_blkno = (-1);
2465 for(i=0; i < ST_NBR_MODES; i++) {
2466 tpnt->modes[i].defined = FALSE;
2467 tpnt->modes[i].defaults_for_writes =0;
2468 tpnt->modes[i].do_async_writes = ST_ASYNC_WRITES;
2469 tpnt->modes[i].do_buffer_writes = ST_BUFFER_WRITES;
2470 tpnt->modes[i].default_compression = ST_DONT_TOUCH;
2471 tpnt->modes[i].default_blksize = (-1);/* No forced size */
2472 tpnt->modes[i].default_density = (-1);/* No forced density */
2474 tpnt->current_mode =0;
2475 tpnt->modes[0].defined = TRUE;
2477 tpnt->density_changed = tpnt->compression_changed =
2478 tpnt->blksize_changed = FALSE;
2480 st_template.nr_dev++;
2481 return0;
2484 static intst_detect(Scsi_Device * SDp)
2486 if(SDp->type != TYPE_TAPE)return0;
2488 printk("Detected scsi tape st%d at scsi%d, channel %d, id %d, lun %d\n",
2489 st_template.dev_noticed++,
2490 SDp->host->host_no, SDp->channel, SDp->id, SDp->lun);
2492 return1;
2495 static int st_registered =0;
2497 /* Driver initialization */
2498 static intst_init()
2500 int i;
2501 Scsi_Tape * STp;
2502 #if !ST_RUNTIME_BUFFERS
2503 int target_nbr;
2504 #endif
2506 if(st_template.dev_noticed ==0)return0;
2508 if(!st_registered) {
2509 if(register_chrdev(SCSI_TAPE_MAJOR,"st",&st_fops)) {
2510 printk(KERN_ERR "Unable to get major %d for SCSI tapes\n",MAJOR_NR);
2511 return1;
2513 st_registered++;
2516 if(scsi_tapes)return0;
2517 st_template.dev_max = st_template.dev_noticed + ST_EXTRA_DEVS;
2518 if(st_template.dev_max < ST_MAX_TAPES)
2519 st_template.dev_max = ST_MAX_TAPES;
2520 if(st_template.dev_max >128/ ST_NBR_MODES)
2521 printk(KERN_INFO "st: Only %d tapes accessible.\n",128/ ST_NBR_MODES);
2522 scsi_tapes =
2523 (Scsi_Tape *)scsi_init_malloc(st_template.dev_max *sizeof(Scsi_Tape),
2524 GFP_ATOMIC);
2525 if(scsi_tapes == NULL) {
2526 printk(KERN_ERR "Unable to allocate descriptors for SCSI tapes.\n");
2527 unregister_chrdev(SCSI_TAPE_MAJOR,"st");
2528 return1;
2531 #if DEBUG
2532 printk(ST_DEB_MSG "st: Buffer size %d bytes, write threshold %d bytes.\n",
2533 st_buffer_size, st_write_threshold);
2534 #endif
2536 memset(scsi_tapes,0, st_template.dev_max *sizeof(Scsi_Tape));
2537 for(i=0; i < st_template.dev_max; ++i) {
2538 STp = &(scsi_tapes[i]);
2539 STp->capacity =0xfffff;
2540 STp->mt_status = (struct mtget *)scsi_init_malloc(sizeof(struct mtget),
2541 GFP_ATOMIC);
2542 /* Initialize status */
2543 memset((void*) scsi_tapes[i].mt_status,0,sizeof(struct mtget));
2546 /* Allocate the buffers */
2547 st_buffers =
2548 (ST_buffer **)scsi_init_malloc(st_template.dev_max *sizeof(ST_buffer *),
2549 GFP_ATOMIC);
2550 if(st_buffers == NULL) {
2551 printk(KERN_ERR "Unable to allocate tape buffer pointers.\n");
2552 unregister_chrdev(SCSI_TAPE_MAJOR,"st");
2553 scsi_init_free((char*) scsi_tapes,
2554 st_template.dev_max *sizeof(Scsi_Tape));
2555 return1;
2558 #if ST_RUNTIME_BUFFERS
2559 st_nbr_buffers =0;
2560 #else
2561 target_nbr = st_template.dev_noticed;
2562 if(target_nbr < ST_EXTRA_DEVS)
2563 target_nbr = ST_EXTRA_DEVS;
2564 if(target_nbr > st_max_buffers)
2565 target_nbr = st_max_buffers;
2567 for(i=st_nbr_buffers=0; i < target_nbr; i++) {
2568 if(!new_tape_buffer(TRUE, TRUE)) {
2569 if(i ==0) {
2570 #if 0
2571 printk(KERN_ERR "Can't continue without at least one tape buffer.\n");
2572 unregister_chrdev(SCSI_TAPE_MAJOR,"st");
2573 scsi_init_free((char*) st_buffers,
2574 st_template.dev_max *sizeof(ST_buffer *));
2575 scsi_init_free((char*) scsi_tapes,
2576 st_template.dev_max *sizeof(Scsi_Tape));
2577 return1;
2578 #else
2579 printk(KERN_INFO "No tape buffers allocated at initialization.\n");
2580 break;
2581 #endif
2583 printk(KERN_INFO "Number of tape buffers adjusted.\n");
2584 break;
2587 #endif
2588 return0;
2591 static voidst_detach(Scsi_Device * SDp)
2593 Scsi_Tape * tpnt;
2594 int i;
2596 for(tpnt = scsi_tapes, i=0; i<st_template.dev_max; i++, tpnt++)
2597 if(tpnt->device == SDp) {
2598 tpnt->device = NULL;
2599 SDp->attached--;
2600 st_template.nr_dev--;
2601 st_template.dev_noticed--;
2602 return;
2604 return;
2608 #ifdef MODULE
2610 intinit_module(void) {
2611 st_template.usage_count = &mod_use_count_;
2612 returnscsi_register_module(MODULE_SCSI_DEV, &st_template);
2615 voidcleanup_module(void)
2617 int i;
2619 scsi_unregister_module(MODULE_SCSI_DEV, &st_template);
2620 unregister_chrdev(SCSI_TAPE_MAJOR,"st");
2621 st_registered--;
2622 if(scsi_tapes != NULL) {
2623 scsi_init_free((char*) scsi_tapes,
2624 st_template.dev_max *sizeof(Scsi_Tape));
2626 if(st_buffers != NULL) {
2627 for(i=0; i < st_nbr_buffers; i++)
2628 if(st_buffers[i] != NULL) {
2629 scsi_init_free((char*) st_buffers[i]->b_data,
2630 st_buffers[i]->buffer_size);
2631 scsi_init_free((char*) st_buffers[i],sizeof(ST_buffer));
2634 scsi_init_free((char*) st_buffers,
2635 st_template.dev_max *sizeof(ST_buffer *));
2638 st_template.dev_max =0;
2640 #endif/* MODULE */
close