Import 2.1.76
[davej-history.git] / drivers / scsi / st.c
blob61d6203f0072723d5ac1a8cb41681ff0842b9387
1 /*
2 SCSI Tape Driver for Linux version 1.1 and newer. See the accompanying
3 file README.st for more information.
5 History:
6 Rewritten from Dwayne Forsyth's SCSI tape driver by Kai Makisara.
7 Contribution and ideas from several people including (in alphabetical
8 order) Klaus Ehrenfried, Wolfgang Denk, Steve Hirsch, Andreas Koppenh"ofer,
9 Michael Leodolter, Eyal Lebedinsky, J"org Weule, and Eric Youngdale.
11 Copyright 1992 - 1997 Kai Makisara
12 email Kai.Makisara@metla.fi
14 Last modified: Wed Nov 5 23:39:52 1997 by makisara@home
15 Some small formal changes - aeb, 950809
18 #include <linux/module.h>
20 #include <linux/fs.h>
21 #include <linux/kernel.h>
22 #include <linux/sched.h>
23 #include <linux/mm.h>
24 #include <linux/init.h>
25 #include <linux/string.h>
26 #include <linux/errno.h>
27 #include <linux/mtio.h>
28 #include <linux/ioctl.h>
29 #include <linux/fcntl.h>
30 #include <asm/uaccess.h>
31 #include <asm/dma.h>
32 #include <asm/system.h>
34 /* The driver prints some debugging information on the console if DEBUG
35 is defined and non-zero. */
36 #define DEBUG 0
38 /* The message level for the debug messages is currently set to KERN_NOTICE
39 so that people can easily see the messages. Later when the debugging messages
40 in the drivers are more widely classified, this may be changed to KERN_DEBUG. */
41 #define ST_DEB_MSG KERN_NOTICE
43 #define MAJOR_NR SCSI_TAPE_MAJOR
44 #include <linux/blk.h>
46 #include"scsi.h"
47 #include"hosts.h"
48 #include <scsi/scsi_ioctl.h>
49 #include"st.h"
50 #include"constants.h"
52 #ifdef MODULE
53 MODULE_PARM(buffer_kbs,"i");
54 MODULE_PARM(write_threshold_kbs,"i");
55 MODULE_PARM(max_buffers,"i");
56 static int buffer_kbs =0;
57 static int write_threshold_kbs =0;
58 static int max_buffers =0;
59 #endif
61 /* The default definitions have been moved to st_options.h */
63 #define ST_KILOBYTE 1024
65 #include"st_options.h"
67 #define ST_BUFFER_SIZE (ST_BUFFER_BLOCKS * ST_KILOBYTE)
68 #define ST_WRITE_THRESHOLD (ST_WRITE_THRESHOLD_BLOCKS * ST_KILOBYTE)
70 /* The buffer size should fit into the 24 bits for length in the
71 6-byte SCSI read and write commands. */
72 #if ST_BUFFER_SIZE >= (2 << 24 - 1)
73 #error"Buffer size should not exceed (2 << 24 - 1) bytes!"
74 #endif
76 #if DEBUG
77 static int debugging =1;
78 #endif
80 #define MAX_RETRIES 0
81 #define MAX_WRITE_RETRIES 0
82 #define MAX_READY_RETRIES 5
83 #define NO_TAPE NOT_READY
85 #define ST_TIMEOUT (900 * HZ)
86 #define ST_LONG_TIMEOUT (14000 * HZ)
88 #define TAPE_NR(x) (MINOR(x) & ~(128 | ST_MODE_MASK))
89 #define TAPE_MODE(x) ((MINOR(x) & ST_MODE_MASK) >> ST_MODE_SHIFT)
91 /* Internal ioctl to set both density (uppermost 8 bits) and blocksize (lower
92 24 bits) */
93 #define SET_DENS_AND_BLK 0x10001
95 static int st_nbr_buffers;
96 static ST_buffer **st_buffers;
97 static int st_buffer_size = ST_BUFFER_SIZE;
98 static int st_write_threshold = ST_WRITE_THRESHOLD;
99 static int st_max_buffers = ST_MAX_BUFFERS;
101 static Scsi_Tape * scsi_tapes = NULL;
103 static int modes_defined = FALSE;
105 static ST_buffer *new_tape_buffer(int,int);
106 static intenlarge_buffer(ST_buffer *,int,int);
107 static voidnormalize_buffer(ST_buffer *);
109 static intst_init(void);
110 static intst_attach(Scsi_Device *);
111 static intst_detect(Scsi_Device *);
112 static voidst_detach(Scsi_Device *);
114 struct Scsi_Device_Template st_template = {NULL,"tape","st", NULL, TYPE_TAPE,
115 SCSI_TAPE_MAJOR,0,0,0,0,
116 st_detect, st_init,
117 NULL, st_attach, st_detach};
119 static intst_compression(Scsi_Tape *,int);
121 static intfind_partition(struct inode *);
122 static intupdate_partition(struct inode *);
124 static intst_int_ioctl(struct inode * inode,unsigned int cmd_in,
125 unsigned long arg);
130 /* Convert the result to success code */
131 static int
132 st_chk_result(Scsi_Cmnd * SCpnt)
134 int dev =TAPE_NR(SCpnt->request.rq_dev);
135 int result = SCpnt->result;
136 unsigned char* sense = SCpnt->sense_buffer, scode;
137 #if DEBUG
138 const char*stp;
139 #endif
141 if(!result /* && SCpnt->sense_buffer[0] == 0 */)
142 return0;
144 scode = sense[2] &0x0f;
146 #if DEBUG
147 if(debugging) {
148 printk(ST_DEB_MSG "st%d: Error: %x, cmd: %x %x %x %x %x %x Len: %d\n",
149 dev, result,
150 SCpnt->data_cmnd[0], SCpnt->data_cmnd[1], SCpnt->data_cmnd[2],
151 SCpnt->data_cmnd[3], SCpnt->data_cmnd[4], SCpnt->data_cmnd[5],
152 SCpnt->request_bufflen);
153 if(driver_byte(result) & DRIVER_SENSE)
154 print_sense("st", SCpnt);
155 else
156 printk("\n");
158 else
159 #endif
160 if(!(driver_byte(result) & DRIVER_SENSE) ||
161 ((sense[0] &0x70) ==0x70&&
162 scode != NO_SENSE &&
163 scode != RECOVERED_ERROR &&
164 /* scode != UNIT_ATTENTION && */
165 scode != BLANK_CHECK &&
166 scode != VOLUME_OVERFLOW &&
167 SCpnt->data_cmnd[0] != MODE_SENSE &&
168 SCpnt->data_cmnd[0] != TEST_UNIT_READY)) {/* Abnormal conditions for tape */
169 if(driver_byte(result) & DRIVER_SENSE) {
170 printk(KERN_WARNING "st%d: Error with sense data: ", dev);
171 print_sense("st", SCpnt);
173 else
174 printk(KERN_WARNING "st%d: Error %x.\n", dev, result);
177 if((sense[0] &0x70) ==0x70&&
178 scode == RECOVERED_ERROR
179 #if ST_RECOVERED_WRITE_FATAL
180 && SCpnt->data_cmnd[0] != WRITE_6
181 && SCpnt->data_cmnd[0] != WRITE_FILEMARKS
182 #endif
184 scsi_tapes[dev].recover_count++;
185 scsi_tapes[dev].mt_status->mt_erreg += (1<< MT_ST_SOFTERR_SHIFT);
186 #if DEBUG
187 if(debugging) {
188 if(SCpnt->data_cmnd[0] == READ_6)
189 stp ="read";
190 else if(SCpnt->data_cmnd[0] == WRITE_6)
191 stp ="write";
192 else
193 stp ="ioctl";
194 printk(ST_DEB_MSG "st%d: Recovered %s error (%d).\n", dev, stp,
195 scsi_tapes[dev].recover_count);
197 #endif
198 if((sense[2] &0xe0) ==0)
199 return0;
201 return(-EIO);
205 /* Wakeup from interrupt */
206 static void
207 st_sleep_done(Scsi_Cmnd * SCpnt)
209 unsigned int st_nbr;
210 int remainder;
211 Scsi_Tape * STp;
213 if((st_nbr =TAPE_NR(SCpnt->request.rq_dev)) < st_template.nr_dev) {
214 STp = &(scsi_tapes[st_nbr]);
215 if((STp->buffer)->writing &&
216 (SCpnt->sense_buffer[0] &0x70) ==0x70&&
217 (SCpnt->sense_buffer[2] &0x40)) {
218 /* EOM at write-behind, has all been written? */
219 if((SCpnt->sense_buffer[0] &0x80) !=0)
220 remainder = (SCpnt->sense_buffer[3] <<24) |
221 (SCpnt->sense_buffer[4] <<16) |
222 (SCpnt->sense_buffer[5] <<8) | SCpnt->sense_buffer[6];
223 else
224 remainder =0;
225 if((SCpnt->sense_buffer[2] &0x0f) == VOLUME_OVERFLOW ||
226 remainder >0)
227 (STp->buffer)->last_result = SCpnt->result;/* Error */
228 else
229 (STp->buffer)->last_result = INT_MAX;/* OK */
231 else
232 (STp->buffer)->last_result = SCpnt->result;
233 SCpnt->request.rq_status = RQ_SCSI_DONE;
234 (STp->buffer)->last_SCpnt = SCpnt;
236 #if DEBUG
237 STp->write_pending =0;
238 #endif
239 up(SCpnt->request.sem);
241 #if DEBUG
242 else if(debugging)
243 printk(KERN_ERR "st?: Illegal interrupt device %x\n", st_nbr);
244 #endif
248 /* Do the scsi command */
249 static Scsi_Cmnd *
250 st_do_scsi(Scsi_Cmnd *SCpnt, Scsi_Tape *STp,unsigned char*cmd,int bytes,
251 int timeout,int retries)
253 if(SCpnt == NULL)
254 if((SCpnt =scsi_allocate_device(NULL, STp->device,1)) == NULL) {
255 printk(KERN_ERR "st%d: Can't get SCSI request.\n",TAPE_NR(STp->devt));
256 return NULL;
259 cmd[1] |= (SCpnt->lun <<5) &0xe0;
260 STp->sem = MUTEX_LOCKED;
261 SCpnt->request.sem = &(STp->sem);
262 SCpnt->request.rq_status = RQ_SCSI_BUSY;
263 SCpnt->request.rq_dev = STp->devt;
265 scsi_do_cmd(SCpnt, (void*)cmd, (STp->buffer)->b_data, bytes,
266 st_sleep_done, timeout, retries);
268 down(SCpnt->request.sem);
270 (STp->buffer)->last_result_fatal =st_chk_result(SCpnt);
272 return SCpnt;
276 /* Handle the write-behind checking */
277 static void
278 write_behind_check(Scsi_Tape *STp)
280 ST_buffer * STbuffer;
281 ST_partstat * STps;
283 STbuffer = STp->buffer;
285 #if DEBUG
286 if(STp->write_pending)
287 STp->nbr_waits++;
288 else
289 STp->nbr_finished++;
290 #endif
292 down(&(STp->sem));
294 (STp->buffer)->last_result_fatal =st_chk_result((STp->buffer)->last_SCpnt);
295 scsi_release_command((STp->buffer)->last_SCpnt);
297 if(STbuffer->writing < STbuffer->buffer_bytes)
298 memcpy(STbuffer->b_data,
299 STbuffer->b_data + STbuffer->writing,
300 STbuffer->buffer_bytes - STbuffer->writing);
301 STbuffer->buffer_bytes -= STbuffer->writing;
302 STps = &(STp->ps[STp->partition]);
303 if(STps->drv_block >=0) {
304 if(STp->block_size ==0)
305 STps->drv_block++;
306 else
307 STps->drv_block += STbuffer->writing / STp->block_size;
309 STbuffer->writing =0;
311 return;
315 /* Step over EOF if it has been inadvertently crossed (ioctl not used because
316 it messes up the block number). */
317 static int
318 cross_eof(Scsi_Tape *STp,int forward)
320 Scsi_Cmnd *SCpnt;
321 unsigned char cmd[10];
323 cmd[0] = SPACE;
324 cmd[1] =0x01;/* Space FileMarks */
325 if(forward) {
326 cmd[2] = cmd[3] =0;
327 cmd[4] =1;
329 else
330 cmd[2] = cmd[3] = cmd[4] =0xff;/* -1 filemarks */
331 cmd[5] =0;
332 #if DEBUG
333 if(debugging)
334 printk(ST_DEB_MSG "st%d: Stepping over filemark %s.\n",
335 TAPE_NR(STp->devt), forward ?"forward":"backward");
336 #endif
338 SCpnt =st_do_scsi(NULL, STp, cmd,0, STp->timeout, MAX_RETRIES);
339 if(!SCpnt)
340 return(-EBUSY);
342 scsi_release_command(SCpnt);
343 SCpnt = NULL;
345 if((STp->buffer)->last_result !=0)
346 printk(KERN_ERR "st%d: Stepping over filemark %s failed.\n",
347 TAPE_NR(STp->devt), forward ?"forward":"backward");
349 return(STp->buffer)->last_result_fatal;
353 /* Flush the write buffer (never need to write if variable blocksize). */
354 static int
355 flush_write_buffer(Scsi_Tape *STp)
357 int offset, transfer, blks;
358 int result;
359 unsigned char cmd[10];
360 Scsi_Cmnd *SCpnt;
361 ST_partstat * STps;
363 if((STp->buffer)->writing) {
364 write_behind_check(STp);
365 if((STp->buffer)->last_result_fatal) {
366 #if DEBUG
367 if(debugging)
368 printk(ST_DEB_MSG "st%d: Async write error (flush) %x.\n",
369 TAPE_NR(STp->devt), (STp->buffer)->last_result);
370 #endif
371 if((STp->buffer)->last_result == INT_MAX)
372 return(-ENOSPC);
373 return(-EIO);
377 if(STp->block_size ==0)
378 return0;
380 result =0;
381 if(STp->dirty ==1) {
383 offset = (STp->buffer)->buffer_bytes;
384 transfer = ((offset + STp->block_size -1) /
385 STp->block_size) * STp->block_size;
386 #if DEBUG
387 if(debugging)
388 printk(ST_DEB_MSG "st%d: Flushing %d bytes.\n",TAPE_NR(STp->devt), transfer);
389 #endif
390 memset((STp->buffer)->b_data + offset,0, transfer - offset);
392 memset(cmd,0,10);
393 cmd[0] = WRITE_6;
394 cmd[1] =1;
395 blks = transfer / STp->block_size;
396 cmd[2] = blks >>16;
397 cmd[3] = blks >>8;
398 cmd[4] = blks;
400 SCpnt =st_do_scsi(NULL, STp, cmd, transfer, STp->timeout, MAX_WRITE_RETRIES);
401 if(!SCpnt)
402 return(-EBUSY);
404 STps = &(STp->ps[STp->partition]);
405 if((STp->buffer)->last_result_fatal !=0) {
406 if((SCpnt->sense_buffer[0] &0x70) ==0x70&&
407 (SCpnt->sense_buffer[2] &0x40) &&
408 (SCpnt->sense_buffer[2] &0x0f) == NO_SENSE) {
409 STp->dirty =0;
410 (STp->buffer)->buffer_bytes =0;
411 result = (-ENOSPC);
413 else{
414 printk(KERN_ERR "st%d: Error on flush.\n",TAPE_NR(STp->devt));
415 result = (-EIO);
417 STps->drv_block = (-1);
419 else{
420 if(STps->drv_block >=0)
421 STps->drv_block += blks;
422 STp->dirty =0;
423 (STp->buffer)->buffer_bytes =0;
425 scsi_release_command(SCpnt);
426 SCpnt = NULL;
428 return result;
432 /* Flush the tape buffer. The tape will be positioned correctly unless
433 seek_next is true. */
434 static int
435 flush_buffer(struct inode * inode,struct file * filp,int seek_next)
437 int backspace, result;
438 Scsi_Tape * STp;
439 ST_buffer * STbuffer;
440 ST_partstat * STps;
441 int dev =TAPE_NR(inode->i_rdev);
443 STp = &(scsi_tapes[dev]);
444 STbuffer = STp->buffer;
447 * If there was a bus reset, block further access
448 * to this device.
450 if( STp->device->was_reset )
451 return(-EIO);
453 if(STp->ready != ST_READY)
454 return0;
456 STps = &(STp->ps[STp->partition]);
457 if(STps->rw == ST_WRITING)/* Writing */
458 returnflush_write_buffer(STp);
460 if(STp->block_size ==0)
461 return0;
463 backspace = ((STp->buffer)->buffer_bytes +
464 (STp->buffer)->read_pointer) / STp->block_size -
465 ((STp->buffer)->read_pointer + STp->block_size -1) /
466 STp->block_size;
467 (STp->buffer)->buffer_bytes =0;
468 (STp->buffer)->read_pointer =0;
469 result =0;
470 if(!seek_next) {
471 if(STps->eof == ST_FM_HIT) {
472 result =cross_eof(STp, FALSE);/* Back over the EOF hit */
473 if(!result)
474 STps->eof = ST_NOEOF;
475 else{
476 if(STps->drv_file >=0)
477 STps->drv_file++;
478 STps->drv_block =0;
481 if(!result && backspace >0)
482 result =st_int_ioctl(inode, MTBSR, backspace);
484 else if(STps->eof == ST_FM_HIT) {
485 if(STps->drv_file >=0)
486 STps->drv_file++;
487 STps->drv_block =0;
488 STps->eof = ST_NOEOF;
491 return result;
495 \f/* Set the mode parameters */
496 static int
497 set_mode_densblk(struct inode * inode, Scsi_Tape *STp, ST_mode *STm)
499 int set_it = FALSE;
500 unsigned long arg;
501 int dev =TAPE_NR(inode->i_rdev);
503 if(!STp->density_changed &&
504 STm->default_density >=0&&
505 STm->default_density != STp->density) {
506 arg = STm->default_density;
507 set_it = TRUE;
509 else
510 arg = STp->density;
511 arg <<= MT_ST_DENSITY_SHIFT;
512 if(!STp->blksize_changed &&
513 STm->default_blksize >=0&&
514 STm->default_blksize != STp->block_size) {
515 arg |= STm->default_blksize;
516 set_it = TRUE;
518 else
519 arg |= STp->block_size;
520 if(set_it &&
521 st_int_ioctl(inode, SET_DENS_AND_BLK, arg)) {
522 printk(KERN_WARNING
523 "st%d: Can't set default block size to %d bytes and density %x.\n",
524 dev, STm->default_blksize, STm->default_density);
525 if(modes_defined)
526 return(-EINVAL);
528 return0;
532 /* Open the device */
533 static int
534 scsi_tape_open(struct inode * inode,struct file * filp)
536 unsigned short flags;
537 int i, need_dma_buffer, new_session = FALSE;
538 unsigned char cmd[10];
539 Scsi_Cmnd * SCpnt;
540 Scsi_Tape * STp;
541 ST_mode * STm;
542 ST_partstat * STps;
543 int dev =TAPE_NR(inode->i_rdev);
544 int mode =TAPE_MODE(inode->i_rdev);
546 if(dev >= st_template.dev_max || !scsi_tapes[dev].device)
547 return(-ENXIO);
549 if( !scsi_block_when_processing_errors(scsi_tapes[dev].device) )
551 return-ENXIO;
554 STp = &(scsi_tapes[dev]);
555 if(STp->in_use) {
556 #if DEBUG
557 printk(ST_DEB_MSG "st%d: Device already in use.\n", dev);
558 #endif
559 return(-EBUSY);
561 STp->rew_at_close = (MINOR(inode->i_rdev) &0x80) ==0;
563 if(mode != STp->current_mode) {
564 #if DEBUG
565 if(debugging)
566 printk(ST_DEB_MSG "st%d: Mode change from %d to %d.\n",
567 dev, STp->current_mode, mode);
568 #endif
569 new_session = TRUE;
570 STp->current_mode = mode;
572 STm = &(STp->modes[STp->current_mode]);
574 /* Allocate buffer for this user */
575 need_dma_buffer = STp->restr_dma;
576 for(i=0; i < st_nbr_buffers; i++)
577 if(!st_buffers[i]->in_use &&
578 (!need_dma_buffer || st_buffers[i]->dma))
579 break;
580 if(i >= st_nbr_buffers) {
581 STp->buffer =new_tape_buffer(FALSE, need_dma_buffer);
582 if(STp->buffer == NULL) {
583 printk(KERN_WARNING "st%d: Can't allocate tape buffer.\n", dev);
584 return(-EBUSY);
587 else
588 STp->buffer = st_buffers[i];
589 (STp->buffer)->in_use =1;
590 (STp->buffer)->writing =0;
591 (STp->buffer)->last_result_fatal =0;
593 flags = filp->f_flags;
594 STp->write_prot = ((flags & O_ACCMODE) == O_RDONLY);
596 STp->dirty =0;
597 for(i=0; i < ST_NBR_PARTITIONS; i++) {
598 STps = &(STp->ps[i]);
599 STps->rw = ST_IDLE;
601 STp->ready = ST_READY;
602 STp->recover_count =0;
603 #if DEBUG
604 STp->nbr_waits = STp->nbr_finished =0;
605 #endif
607 if(scsi_tapes[dev].device->host->hostt->module)
608 __MOD_INC_USE_COUNT(scsi_tapes[dev].device->host->hostt->module);
609 if(st_template.module)
610 __MOD_INC_USE_COUNT(st_template.module);
612 memset((void*) &cmd[0],0,10);
613 cmd[0] = TEST_UNIT_READY;
615 SCpnt =st_do_scsi(NULL, STp, cmd,0, STp->long_timeout, MAX_READY_RETRIES);
616 if(!SCpnt) {
617 if(scsi_tapes[dev].device->host->hostt->module)
618 __MOD_DEC_USE_COUNT(scsi_tapes[dev].device->host->hostt->module);
619 if(st_template.module)
620 __MOD_DEC_USE_COUNT(st_template.module);
621 return(-EBUSY);
624 if((SCpnt->sense_buffer[0] &0x70) ==0x70&&
625 (SCpnt->sense_buffer[2] &0x0f) == UNIT_ATTENTION) {/* New media? */
626 memset((void*) &cmd[0],0,10);
627 cmd[0] = TEST_UNIT_READY;
629 SCpnt =st_do_scsi(SCpnt, STp, cmd,0, STp->long_timeout, MAX_READY_RETRIES);
631 (STp->device)->was_reset =0;
632 STp->partition = STp->new_partition =0;
633 if(STp->can_partitions)
634 STp->nbr_partitions =1;/* This guess will be updated later if necessary */
635 for(i=0; i < ST_NBR_PARTITIONS; i++) {
636 STps = &(STp->ps[i]);
637 STps->rw = ST_IDLE;
638 STps->eof = ST_NOEOF;
639 STps->at_sm =0;
640 STps->last_block_valid = FALSE;
641 STps->drv_block =0;
642 STps->drv_file =0;
644 new_session = TRUE;
647 if((STp->buffer)->last_result_fatal !=0) {
648 if((STp->device)->scsi_level >= SCSI_2 &&
649 (SCpnt->sense_buffer[0] &0x70) ==0x70&&
650 (SCpnt->sense_buffer[2] &0x0f) == NOT_READY &&
651 SCpnt->sense_buffer[12] ==0x3a) {/* Check ASC */
652 STp->ready = ST_NO_TAPE;
653 }else
654 STp->ready = ST_NOT_READY;
655 scsi_release_command(SCpnt);
656 SCpnt = NULL;
657 STp->density =0;/* Clear the erroneous "residue" */
658 STp->write_prot =0;
659 STp->block_size =0;
660 STp->ps[0].drv_file = STp->ps[0].drv_block =0;
661 STp->partition = STp->new_partition =0;
662 STp->door_locked = ST_UNLOCKED;
663 STp->in_use =1;
664 return0;
667 if(STp->omit_blklims)
668 STp->min_block = STp->max_block = (-1);
669 else{
670 memset((void*) &cmd[0],0,10);
671 cmd[0] = READ_BLOCK_LIMITS;
673 SCpnt =st_do_scsi(SCpnt, STp, cmd,6, STp->timeout, MAX_READY_RETRIES);
675 if(!SCpnt->result && !SCpnt->sense_buffer[0]) {
676 STp->max_block = ((STp->buffer)->b_data[1] <<16) |
677 ((STp->buffer)->b_data[2] <<8) | (STp->buffer)->b_data[3];
678 STp->min_block = ((STp->buffer)->b_data[4] <<8) |
679 (STp->buffer)->b_data[5];
680 #if DEBUG
681 if(debugging)
682 printk(ST_DEB_MSG "st%d: Block limits %d - %d bytes.\n", dev, STp->min_block,
683 STp->max_block);
684 #endif
686 else{
687 STp->min_block = STp->max_block = (-1);
688 #if DEBUG
689 if(debugging)
690 printk(ST_DEB_MSG "st%d: Can't read block limits.\n", dev);
691 #endif
695 memset((void*) &cmd[0],0,10);
696 cmd[0] = MODE_SENSE;
697 cmd[4] =12;
699 SCpnt =st_do_scsi(SCpnt, STp, cmd,12, STp->timeout, MAX_READY_RETRIES);
701 if((STp->buffer)->last_result_fatal !=0) {
702 #if DEBUG
703 if(debugging)
704 printk(ST_DEB_MSG "st%d: No Mode Sense.\n", dev);
705 #endif
706 STp->block_size = ST_DEFAULT_BLOCK;/* Educated guess (?) */
707 (STp->buffer)->last_result_fatal =0;/* Prevent error propagation */
708 STp->drv_write_prot =0;
710 else{
712 #if DEBUG
713 if(debugging)
714 printk(ST_DEB_MSG "st%d: Mode sense. Length %d, medium %x, WBS %x, BLL %d\n",
715 dev,
716 (STp->buffer)->b_data[0], (STp->buffer)->b_data[1],
717 (STp->buffer)->b_data[2], (STp->buffer)->b_data[3]);
718 #endif
720 if((STp->buffer)->b_data[3] >=8) {
721 STp->drv_buffer = ((STp->buffer)->b_data[2] >>4) &7;
722 STp->density = (STp->buffer)->b_data[4];
723 STp->block_size = (STp->buffer)->b_data[9] *65536+
724 (STp->buffer)->b_data[10] *256+ (STp->buffer)->b_data[11];
725 #if DEBUG
726 if(debugging)
727 printk(ST_DEB_MSG "st%d: Density %x, tape length: %x, drv buffer: %d\n",
728 dev, STp->density, (STp->buffer)->b_data[5] *65536+
729 (STp->buffer)->b_data[6] *256+ (STp->buffer)->b_data[7],
730 STp->drv_buffer);
731 #endif
734 if(STp->block_size > (STp->buffer)->buffer_size &&
735 !enlarge_buffer(STp->buffer, STp->block_size, STp->restr_dma)) {
736 printk(KERN_NOTICE "st%d: Blocksize %d too large for buffer.\n", dev,
737 STp->block_size);
738 (STp->buffer)->in_use =0;
739 STp->buffer = NULL;
740 if(scsi_tapes[dev].device->host->hostt->module)
741 __MOD_DEC_USE_COUNT(scsi_tapes[dev].device->host->hostt->module);
742 if(st_template.module)
743 __MOD_DEC_USE_COUNT(st_template.module);
744 return(-EIO);
746 STp->drv_write_prot = ((STp->buffer)->b_data[2] &0x80) !=0;
748 scsi_release_command(SCpnt);
749 SCpnt = NULL;
751 if(STp->block_size >0)
752 (STp->buffer)->buffer_blocks = st_buffer_size / STp->block_size;
753 else
754 (STp->buffer)->buffer_blocks =1;
755 (STp->buffer)->buffer_bytes = (STp->buffer)->read_pointer =0;
757 #if DEBUG
758 if(debugging)
759 printk(ST_DEB_MSG "st%d: Block size: %d, buffer size: %d (%d blocks).\n", dev,
760 STp->block_size, (STp->buffer)->buffer_size,
761 (STp->buffer)->buffer_blocks);
762 #endif
764 if(STp->drv_write_prot) {
765 STp->write_prot =1;
766 #if DEBUG
767 if(debugging)
768 printk(ST_DEB_MSG "st%d: Write protected\n", dev);
769 #endif
770 if((flags & O_ACCMODE) == O_WRONLY || (flags & O_ACCMODE) == O_RDWR) {
771 (STp->buffer)->in_use =0;
772 STp->buffer = NULL;
773 if(scsi_tapes[dev].device->host->hostt->module)
774 __MOD_DEC_USE_COUNT(scsi_tapes[dev].device->host->hostt->module);
775 if(st_template.module)
776 __MOD_DEC_USE_COUNT(st_template.module);
777 return(-EROFS);
781 if(STp->can_partitions && STp->nbr_partitions <1) {
782 /* This code is reached when the device is opened for the first time
783 after the driver has been initialized with tape in the drive and the
784 partition support has been enabled. */
785 #if DEBUG
786 if(debugging)
787 printk(ST_DEB_MSG "st%d: Updating partition number in status.\n", dev);
788 #endif
789 if((STp->partition =find_partition(inode)) <0) {
790 (STp->buffer)->in_use =0;
791 STp->buffer = NULL;
792 if(scsi_tapes[dev].device->host->hostt->module)
793 __MOD_DEC_USE_COUNT(scsi_tapes[dev].device->host->hostt->module);
794 if(st_template.module)
795 __MOD_DEC_USE_COUNT(st_template.module);
796 return STp->partition;
798 STp->new_partition = STp->partition;
799 STp->nbr_partitions =1;/* This guess will be updated when necessary */
802 if(new_session) {/* Change the drive parameters for the new mode */
803 STp->density_changed = STp->blksize_changed = FALSE;
804 STp->compression_changed = FALSE;
805 if(!(STm->defaults_for_writes) &&
806 (i =set_mode_densblk(inode, STp, STm)) <0) {
807 (STp->buffer)->in_use =0;
808 STp->buffer = NULL;
809 if(scsi_tapes[dev].device->host->hostt->module)
810 __MOD_DEC_USE_COUNT(scsi_tapes[dev].device->host->hostt->module);
811 if(st_template.module)
812 __MOD_DEC_USE_COUNT(st_template.module);
813 return i;
815 if(STp->default_drvbuffer !=0xff) {
816 if(st_int_ioctl(inode, MTSETDRVBUFFER, STp->default_drvbuffer))
817 printk(KERN_WARNING "st%d: Can't set default drive buffering to %d.\n",
818 dev, STp->default_drvbuffer);
822 STp->in_use =1;
824 return0;
828 /* Close the device*/
829 static int
830 scsi_tape_close(struct inode * inode,struct file * filp)
832 int result =0, result2;
833 static unsigned char cmd[10];
834 Scsi_Cmnd * SCpnt;
835 Scsi_Tape * STp;
836 ST_mode * STm;
837 ST_partstat * STps;
839 kdev_t devt = inode->i_rdev;
840 int dev;
842 dev =TAPE_NR(devt);
843 STp = &(scsi_tapes[dev]);
844 STm = &(STp->modes[STp->current_mode]);
845 STps = &(STp->ps[STp->partition]);
847 if(STp->can_partitions &&
848 (result =update_partition(inode)) <0) {
849 #if DEBUG
850 if(debugging)
851 printk(ST_DEB_MSG "st%d: update_partition at close failed.\n", dev);
852 #endif
853 goto out;
856 if( STps->rw == ST_WRITING && !(STp->device)->was_reset) {
858 result =flush_write_buffer(STp);
860 #if DEBUG
861 if(debugging) {
862 printk(ST_DEB_MSG "st%d: File length %ld bytes.\n",
863 dev, (long)(filp->f_pos));
864 printk(ST_DEB_MSG "st%d: Async write waits %d, finished %d.\n",
865 dev, STp->nbr_waits, STp->nbr_finished);
867 #endif
869 if(result ==0|| result == (-ENOSPC)) {
871 memset(cmd,0,10);
872 cmd[0] = WRITE_FILEMARKS;
873 cmd[4] =1+ STp->two_fm;
875 SCpnt =st_do_scsi(NULL, STp, cmd,0, STp->timeout, MAX_WRITE_RETRIES);
876 if(!SCpnt)
877 goto out;
879 if((STp->buffer)->last_result_fatal !=0&&
880 ((SCpnt->sense_buffer[0] &0x70) !=0x70||
881 (SCpnt->sense_buffer[2] &0x4f) !=0x40||
882 ((SCpnt->sense_buffer[0] &0x80) !=0&&
883 (SCpnt->sense_buffer[3] | SCpnt->sense_buffer[4] |
884 SCpnt->sense_buffer[5] |
885 SCpnt->sense_buffer[6]) ==0))) {
886 /* Filter out successful write at EOM */
887 scsi_release_command(SCpnt);
888 SCpnt = NULL;
889 printk(KERN_ERR "st%d: Error on write filemark.\n", dev);
890 if(result ==0)
891 result = (-EIO);
893 else{
894 scsi_release_command(SCpnt);
895 SCpnt = NULL;
896 if(STps->drv_file >=0)
897 STps->drv_file++ ;
898 STps->drv_block =0;
899 if(STp->two_fm)
900 cross_eof(STp, FALSE);
901 STps->eof = ST_FM;
905 #if DEBUG
906 if(debugging)
907 printk(ST_DEB_MSG "st%d: Buffer flushed, %d EOF(s) written\n",
908 dev, cmd[4]);
909 #endif
911 else if(!STp->rew_at_close) {
912 STps = &(STp->ps[STp->partition]);
913 if(!STm->sysv || STps->rw != ST_READING) {
914 if(STp->can_bsr)
915 result =flush_buffer(inode, filp,0);
916 else if(STps->eof == ST_FM_HIT) {
917 result =cross_eof(STp, FALSE);
918 if(result) {
919 if(STps->drv_file >=0)
920 STps->drv_file++;
921 STps->drv_block =0;
922 STps->eof = ST_FM;
924 else
925 STps->eof = ST_NOEOF;
928 else if((STps->eof == ST_NOEOF &&
929 !(result =cross_eof(STp, TRUE))) ||
930 STps->eof == ST_FM_HIT) {
931 if(STps->drv_file >=0)
932 STps->drv_file++;
933 STps->drv_block =0;
934 STps->eof = ST_FM;
938 out:
939 if(STp->rew_at_close) {
940 result2 =st_int_ioctl(inode, MTREW,1);
941 if(result ==0)
942 result = result2;
945 if(STp->door_locked == ST_LOCKED_AUTO)
946 st_int_ioctl(inode, MTUNLOCK,0);
948 if(STp->buffer != NULL) {
949 normalize_buffer(STp->buffer);
950 (STp->buffer)->in_use =0;
953 STp->in_use =0;
954 if(scsi_tapes[dev].device->host->hostt->module)
955 __MOD_DEC_USE_COUNT(scsi_tapes[dev].device->host->hostt->module);
956 if(st_template.module)
957 __MOD_DEC_USE_COUNT(st_template.module);
959 return result;
963 /* Write command */
964 static ssize_t
965 st_write(struct file * filp,const char* buf,size_t count, loff_t *ppos)
967 struct inode *inode = filp->f_dentry->d_inode;
968 ssize_t total;
969 ssize_t i, do_count, blks, retval, transfer;
970 int write_threshold;
971 int doing_write =0;
972 static unsigned char cmd[10];
973 const char*b_point;
974 Scsi_Cmnd * SCpnt = NULL;
975 Scsi_Tape * STp;
976 ST_mode * STm;
977 ST_partstat * STps;
978 int dev =TAPE_NR(inode->i_rdev);
980 STp = &(scsi_tapes[dev]);
983 * If we are in the middle of error recovery, don't let anyone
984 * else try and use this device. Also, if error recovery fails, it
985 * may try and take the device offline, in which case all further
986 * access to the device is prohibited.
988 if( !scsi_block_when_processing_errors(STp->device) )
990 return-ENXIO;
993 if(ppos != &filp->f_pos) {
994 /* "A request was outside the capabilities of the device." */
995 return-ENXIO;
998 if(STp->ready != ST_READY) {
999 if(STp->ready == ST_NO_TAPE)
1000 return(-ENOMEDIUM);
1001 else
1002 return(-EIO);
1004 STm = &(STp->modes[STp->current_mode]);
1005 if(!STm->defined)
1006 return(-ENXIO);
1007 if(count ==0)
1008 return0;
1011 * If there was a bus reset, block further access
1012 * to this device.
1014 if( STp->device->was_reset )
1015 return(-EIO);
1017 #if DEBUG
1018 if(!STp->in_use) {
1019 printk(ST_DEB_MSG "st%d: Incorrect device.\n", dev);
1020 return(-EIO);
1022 #endif
1024 if(STp->can_partitions &&
1025 (retval =update_partition(inode)) <0)
1026 return retval;
1027 STps = &(STp->ps[STp->partition]);
1029 if(STp->write_prot)
1030 return(-EACCES);
1032 if(STp->block_size ==0&&
1033 count > (STp->buffer)->buffer_size &&
1034 !enlarge_buffer(STp->buffer, count, STp->restr_dma))
1035 return(-EOVERFLOW);
1037 if(STp->do_auto_lock && STp->door_locked == ST_UNLOCKED &&
1038 !st_int_ioctl(inode, MTLOCK,0))
1039 STp->door_locked = ST_LOCKED_AUTO;
1041 if(STps->rw == ST_READING) {
1042 retval =flush_buffer(inode, filp,0);
1043 if(retval)
1044 return retval;
1045 STps->rw = ST_WRITING;
1047 else if(STps->rw != ST_WRITING &&
1048 STps->drv_file ==0&& STps->drv_block ==0) {
1049 if((retval =set_mode_densblk(inode, STp, STm)) <0)
1050 return retval;
1051 if(STm->default_compression != ST_DONT_TOUCH &&
1052 !(STp->compression_changed)) {
1053 if(st_compression(STp, (STm->default_compression == ST_YES))) {
1054 printk(KERN_WARNING "st%d: Can't set default compression.\n",
1055 dev);
1056 if(modes_defined)
1057 return(-EINVAL);
1062 if((STp->buffer)->writing) {
1063 write_behind_check(STp);
1064 if((STp->buffer)->last_result_fatal) {
1065 #if DEBUG
1066 if(debugging)
1067 printk(ST_DEB_MSG "st%d: Async write error (write) %x.\n", dev,
1068 (STp->buffer)->last_result);
1069 #endif
1070 if((STp->buffer)->last_result == INT_MAX)
1071 STps->eof = ST_EOM_OK;
1072 else
1073 STps->eof = ST_EOM_ERROR;
1076 if(STps->eof == ST_EOM_OK)
1077 return(-ENOSPC);
1078 else if(STps->eof == ST_EOM_ERROR)
1079 return(-EIO);
1081 /* Check the buffer readability in cases where copy_user might catch
1082 the problems after some tape movement. */
1083 if(STp->block_size !=0&&
1084 (copy_from_user(&i, buf,1) !=0||
1085 copy_from_user(&i, buf + count -1,1) !=0))
1086 return(-EFAULT);
1088 if(!STm->do_buffer_writes) {
1089 if(STp->block_size !=0&& (count % STp->block_size) !=0)
1090 return(-EIO);/* Write must be integral number of blocks */
1091 write_threshold =1;
1093 else
1094 write_threshold = (STp->buffer)->buffer_blocks * STp->block_size;
1095 if(!STm->do_async_writes)
1096 write_threshold--;
1098 total = count;
1100 memset(cmd,0,10);
1101 cmd[0] = WRITE_6;
1102 cmd[1] = (STp->block_size !=0);
1104 STps->rw = ST_WRITING;
1106 b_point = buf;
1107 while((STp->block_size ==0&& !STm->do_async_writes && count >0) ||
1108 (STp->block_size !=0&&
1109 (STp->buffer)->buffer_bytes + count > write_threshold))
1111 doing_write =1;
1112 if(STp->block_size ==0)
1113 do_count = count;
1114 else{
1115 do_count = (STp->buffer)->buffer_blocks * STp->block_size -
1116 (STp->buffer)->buffer_bytes;
1117 if(do_count > count)
1118 do_count = count;
1121 i =copy_from_user((STp->buffer)->b_data +
1122 (STp->buffer)->buffer_bytes, b_point, do_count);
1123 if(i) {
1124 if(SCpnt != NULL)
1126 scsi_release_command(SCpnt);
1127 SCpnt = NULL;
1129 return(-EFAULT);
1132 if(STp->block_size ==0)
1133 blks = transfer = do_count;
1134 else{
1135 blks = ((STp->buffer)->buffer_bytes + do_count) /
1136 STp->block_size;
1137 transfer = blks * STp->block_size;
1139 cmd[2] = blks >>16;
1140 cmd[3] = blks >>8;
1141 cmd[4] = blks;
1143 SCpnt =st_do_scsi(SCpnt, STp, cmd, transfer, STp->timeout, MAX_WRITE_RETRIES);
1144 if(!SCpnt)
1145 return(-EBUSY);
1147 if((STp->buffer)->last_result_fatal !=0) {
1148 #if DEBUG
1149 if(debugging)
1150 printk(ST_DEB_MSG "st%d: Error on write:\n", dev);
1151 #endif
1152 if((SCpnt->sense_buffer[0] &0x70) ==0x70&&
1153 (SCpnt->sense_buffer[2] &0x40)) {
1154 if(STp->block_size !=0&& (SCpnt->sense_buffer[0] &0x80) !=0)
1155 transfer = (SCpnt->sense_buffer[3] <<24) |
1156 (SCpnt->sense_buffer[4] <<16) |
1157 (SCpnt->sense_buffer[5] <<8) | SCpnt->sense_buffer[6];
1158 else if(STp->block_size ==0&&
1159 (SCpnt->sense_buffer[2] &0x0f) == VOLUME_OVERFLOW)
1160 transfer = do_count;
1161 else
1162 transfer =0;
1163 if(STp->block_size !=0)
1164 transfer *= STp->block_size;
1165 if(transfer <= do_count) {
1166 filp->f_pos += do_count - transfer;
1167 count -= do_count - transfer;
1168 if(STps->drv_block >=0) {
1169 if(STp->block_size ==0&& transfer < do_count)
1170 STps->drv_block++;
1171 else if(STp->block_size !=0)
1172 STps->drv_block += (do_count - transfer) / STp->block_size;
1174 STps->eof = ST_EOM_OK;
1175 retval = (-ENOSPC);/* EOM within current request */
1176 #if DEBUG
1177 if(debugging)
1178 printk(ST_DEB_MSG "st%d: EOM with %d bytes unwritten.\n",
1179 dev, transfer);
1180 #endif
1182 else{
1183 STps->eof = ST_EOM_ERROR;
1184 STps->drv_block = (-1);/* Too cautious? */
1185 retval = (-EIO);/* EOM for old data */
1186 #if DEBUG
1187 if(debugging)
1188 printk(ST_DEB_MSG "st%d: EOM with lost data.\n", dev);
1189 #endif
1192 else{
1193 STps->drv_block = (-1);/* Too cautious? */
1194 retval = (-EIO);
1197 scsi_release_command(SCpnt);
1198 SCpnt = NULL;
1199 (STp->buffer)->buffer_bytes =0;
1200 STp->dirty =0;
1201 if(count < total)
1202 return total - count;
1203 else
1204 return retval;
1206 filp->f_pos += do_count;
1207 b_point += do_count;
1208 count -= do_count;
1209 if(STps->drv_block >=0) {
1210 if(STp->block_size ==0)
1211 STps->drv_block++;
1212 else
1213 STps->drv_block += blks;
1215 (STp->buffer)->buffer_bytes =0;
1216 STp->dirty =0;
1218 if(count !=0) {
1219 STp->dirty =1;
1220 i =copy_from_user((STp->buffer)->b_data +
1221 (STp->buffer)->buffer_bytes, b_point, count);
1222 if(i) {
1223 if(SCpnt != NULL)
1225 scsi_release_command(SCpnt);
1226 SCpnt = NULL;
1228 return(-EFAULT);
1230 filp->f_pos += count;
1231 (STp->buffer)->buffer_bytes += count;
1232 count =0;
1235 if(doing_write && (STp->buffer)->last_result_fatal !=0) {
1236 scsi_release_command(SCpnt);
1237 SCpnt = NULL;
1238 return(STp->buffer)->last_result_fatal;
1241 if(STm->do_async_writes &&
1242 (((STp->buffer)->buffer_bytes >= STp->write_threshold &&
1243 (STp->buffer)->buffer_bytes >= STp->block_size) ||
1244 STp->block_size ==0) ) {
1245 /* Schedule an asynchronous write */
1246 if(!SCpnt) {
1247 SCpnt =scsi_allocate_device(NULL, STp->device,1);
1248 if(!SCpnt)
1249 return(-EBUSY);
1251 if(STp->block_size ==0)
1252 (STp->buffer)->writing = (STp->buffer)->buffer_bytes;
1253 else
1254 (STp->buffer)->writing = ((STp->buffer)->buffer_bytes /
1255 STp->block_size) * STp->block_size;
1256 STp->dirty = !((STp->buffer)->writing ==
1257 (STp->buffer)->buffer_bytes);
1259 if(STp->block_size ==0)
1260 blks = (STp->buffer)->writing;
1261 else
1262 blks = (STp->buffer)->writing / STp->block_size;
1263 cmd[2] = blks >>16;
1264 cmd[3] = blks >>8;
1265 cmd[4] = blks;
1266 STp->sem = MUTEX_LOCKED;
1267 SCpnt->request.sem = &(STp->sem);
1268 SCpnt->request.rq_status = RQ_SCSI_BUSY;
1269 SCpnt->request.rq_dev = STp->devt;
1270 #if DEBUG
1271 STp->write_pending =1;
1272 #endif
1274 scsi_do_cmd(SCpnt,
1275 (void*) cmd, (STp->buffer)->b_data,
1276 (STp->buffer)->writing,
1277 st_sleep_done, STp->timeout, MAX_WRITE_RETRIES);
1279 else if(SCpnt != NULL)
1281 scsi_release_command(SCpnt);
1282 SCpnt = NULL;
1284 STps->at_sm &= (total ==0);
1285 if(total >0)
1286 STps->eof = ST_NOEOF;
1288 return( total);
1291 /* Read data from the tape. Returns zero in the normal case, one if the
1292 eof status has changed, and the negative error code in case of a
1293 fatal error. Otherwise updates the buffer and the eof state. */
1294 static long
1295 read_tape(struct inode *inode,long count, Scsi_Cmnd **aSCpnt)
1297 int transfer, blks, bytes;
1298 static unsigned char cmd[10];
1299 Scsi_Cmnd *SCpnt;
1300 Scsi_Tape *STp;
1301 ST_mode * STm;
1302 ST_partstat * STps;
1303 int dev =TAPE_NR(inode->i_rdev);
1304 int retval =0;
1306 if(count ==0)
1307 return0;
1309 STp = &(scsi_tapes[dev]);
1310 STm = &(STp->modes[STp->current_mode]);
1311 STps = &(STp->ps[STp->partition]);
1312 if(STps->eof == ST_FM_HIT)
1313 return1;
1315 memset(cmd,0,10);
1316 cmd[0] = READ_6;
1317 cmd[1] = (STp->block_size !=0);
1318 if(STp->block_size ==0)
1319 blks = bytes = count;
1320 else{
1321 if(STm->do_read_ahead) {
1322 blks = (STp->buffer)->buffer_blocks;
1323 bytes = blks * STp->block_size;
1325 else{
1326 bytes = count;
1327 if(bytes > (STp->buffer)->buffer_size)
1328 bytes = (STp->buffer)->buffer_size;
1329 blks = bytes / STp->block_size;
1330 bytes = blks * STp->block_size;
1333 cmd[2] = blks >>16;
1334 cmd[3] = blks >>8;
1335 cmd[4] = blks;
1337 SCpnt = *aSCpnt;
1338 SCpnt =st_do_scsi(SCpnt, STp, cmd, bytes, STp->timeout, MAX_RETRIES);
1339 *aSCpnt = SCpnt;
1340 if(!SCpnt)
1341 return(-EBUSY);
1343 (STp->buffer)->read_pointer =0;
1344 STps->at_sm =0;
1347 /* Something to check */
1348 if((STp->buffer)->last_result_fatal) {
1349 retval =1;
1350 #if DEBUG
1351 if(debugging)
1352 printk(ST_DEB_MSG "st%d: Sense: %2x %2x %2x %2x %2x %2x %2x %2x\n",
1353 dev,
1354 SCpnt->sense_buffer[0], SCpnt->sense_buffer[1],
1355 SCpnt->sense_buffer[2], SCpnt->sense_buffer[3],
1356 SCpnt->sense_buffer[4], SCpnt->sense_buffer[5],
1357 SCpnt->sense_buffer[6], SCpnt->sense_buffer[7]);
1358 #endif
1359 if((SCpnt->sense_buffer[0] &0x70) ==0x70) {/* extended sense */
1361 if((SCpnt->sense_buffer[2] &0x0f) == BLANK_CHECK)
1362 SCpnt->sense_buffer[2] &=0xcf;/* No need for EOM in this case */
1364 if((SCpnt->sense_buffer[2] &0xe0) !=0) {/* EOF, EOM, or ILI */
1365 /* Compute the residual count */
1366 if((SCpnt->sense_buffer[0] &0x80) !=0)
1367 transfer = (SCpnt->sense_buffer[3] <<24) |
1368 (SCpnt->sense_buffer[4] <<16) |
1369 (SCpnt->sense_buffer[5] <<8) | SCpnt->sense_buffer[6];
1370 else
1371 transfer =0;
1372 if(STp->block_size ==0&&
1373 (SCpnt->sense_buffer[2] &0x0f) == MEDIUM_ERROR)
1374 transfer = bytes;
1376 if(SCpnt->sense_buffer[2] &0x20) {/* ILI */
1377 if(STp->block_size ==0) {
1378 if(transfer <=0)
1379 transfer =0;
1380 (STp->buffer)->buffer_bytes = bytes - transfer;
1382 else{
1383 scsi_release_command(SCpnt);
1384 SCpnt = *aSCpnt = NULL;
1385 if(transfer == blks) {/* We did not get anything, error */
1386 printk(KERN_NOTICE "st%d: Incorrect block size.\n", dev);
1387 if(STps->drv_block >=0)
1388 STps->drv_block += blks - transfer +1;
1389 st_int_ioctl(inode, MTBSR,1);
1390 return(-EIO);
1392 /* We have some data, deliver it */
1393 (STp->buffer)->buffer_bytes = (blks - transfer) *
1394 STp->block_size;
1395 #if DEBUG
1396 if(debugging)
1397 printk(ST_DEB_MSG
1398 "st%d: ILI but enough data received %ld %d.\n",
1399 dev, count, (STp->buffer)->buffer_bytes);
1400 #endif
1401 if(STps->drv_block >=0)
1402 STps->drv_block +=1;
1403 if(st_int_ioctl(inode, MTBSR,1))
1404 return(-EIO);
1407 else if(SCpnt->sense_buffer[2] &0x80) {/* FM overrides EOM */
1408 if(STps->eof != ST_FM_HIT)
1409 STps->eof = ST_FM_HIT;
1410 else
1411 STps->eof = ST_EOD_2;
1412 if(STp->block_size ==0)
1413 (STp->buffer)->buffer_bytes =0;
1414 else
1415 (STp->buffer)->buffer_bytes =
1416 bytes - transfer * STp->block_size;
1417 #if DEBUG
1418 if(debugging)
1419 printk(ST_DEB_MSG
1420 "st%d: EOF detected (%d bytes read).\n",
1421 dev, (STp->buffer)->buffer_bytes);
1422 #endif
1424 else if(SCpnt->sense_buffer[2] &0x40) {
1425 if(STps->eof == ST_FM)
1426 STps->eof = ST_EOD_1;
1427 else
1428 STps->eof = ST_EOM_OK;
1429 if(STp->block_size ==0)
1430 (STp->buffer)->buffer_bytes = bytes - transfer;
1431 else
1432 (STp->buffer)->buffer_bytes =
1433 bytes - transfer * STp->block_size;
1434 #if DEBUG
1435 if(debugging)
1436 printk(ST_DEB_MSG "st%d: EOM detected (%d bytes read).\n",
1437 dev, (STp->buffer)->buffer_bytes);
1438 #endif
1440 }/* end of EOF, EOM, ILI test */
1441 else{/* nonzero sense key */
1442 #if DEBUG
1443 if(debugging)
1444 printk(ST_DEB_MSG "st%d: Tape error while reading.\n", dev);
1445 #endif
1446 STps->drv_block = (-1);
1447 if(STps->eof == ST_FM &&
1448 (SCpnt->sense_buffer[2] &0x0f) == BLANK_CHECK) {
1449 #if DEBUG
1450 if(debugging)
1451 printk(ST_DEB_MSG
1452 "st%d: Zero returned for first BLANK CHECK after EOF.\n",
1453 dev);
1454 #endif
1455 STps->eof = ST_EOD_2;/* First BLANK_CHECK after FM */
1457 else/* Some other extended sense code */
1458 retval = (-EIO);
1460 }/* End of extended sense test */
1461 else{/* Non-extended sense */
1462 retval = (STp->buffer)->last_result_fatal;
1465 }/* End of error handling */
1466 else/* Read successful */
1467 (STp->buffer)->buffer_bytes = bytes;
1469 if(STps->drv_block >=0) {
1470 if(STp->block_size ==0)
1471 STps->drv_block++;
1472 else
1473 STps->drv_block += (STp->buffer)->buffer_bytes / STp->block_size;
1476 return retval;
1480 /* Read command */
1481 static ssize_t
1482 st_read(struct file * filp,char* buf,size_t count, loff_t *ppos)
1484 struct inode * inode = filp->f_dentry->d_inode;
1485 ssize_t total;
1486 ssize_t i, transfer;
1487 int special;
1488 Scsi_Cmnd * SCpnt = NULL;
1489 Scsi_Tape * STp;
1490 ST_mode * STm;
1491 ST_partstat * STps;
1492 int dev =TAPE_NR(inode->i_rdev);
1494 STp = &(scsi_tapes[dev]);
1497 * If we are in the middle of error recovery, don't let anyone
1498 * else try and use this device. Also, if error recovery fails, it
1499 * may try and take the device offline, in which case all further
1500 * access to the device is prohibited.
1502 if( !scsi_block_when_processing_errors(STp->device) )
1504 return-ENXIO;
1507 if(ppos != &filp->f_pos) {
1508 /* "A request was outside the capabilities of the device." */
1509 return-ENXIO;
1512 if(STp->ready != ST_READY) {
1513 if(STp->ready == ST_NO_TAPE)
1514 return(-ENOMEDIUM);
1515 else
1516 return(-EIO);
1518 STm = &(STp->modes[STp->current_mode]);
1519 if(!STm->defined)
1520 return(-ENXIO);
1521 #if DEBUG
1522 if(!STp->in_use) {
1523 printk(ST_DEB_MSG "st%d: Incorrect device.\n", dev);
1524 return(-EIO);
1526 #endif
1528 if(STp->can_partitions &&
1529 (total =update_partition(inode)) <0)
1530 return total;
1532 if(STp->block_size ==0&&
1533 count > (STp->buffer)->buffer_size &&
1534 !enlarge_buffer(STp->buffer, count, STp->restr_dma))
1535 return(-EOVERFLOW);
1537 if(!(STm->do_read_ahead) && STp->block_size !=0&&
1538 (count % STp->block_size) !=0)
1539 return(-EIO);/* Read must be integral number of blocks */
1541 if(STp->do_auto_lock && STp->door_locked == ST_UNLOCKED &&
1542 !st_int_ioctl(inode, MTLOCK,0))
1543 STp->door_locked = ST_LOCKED_AUTO;
1545 STps = &(STp->ps[STp->partition]);
1546 if(STps->rw == ST_WRITING) {
1547 transfer =flush_buffer(inode, filp,0);
1548 if(transfer)
1549 return transfer;
1550 STps->rw = ST_READING;
1553 #if DEBUG
1554 if(debugging && STps->eof != ST_NOEOF)
1555 printk(ST_DEB_MSG "st%d: EOF/EOM flag up (%d). Bytes %d\n", dev,
1556 STps->eof, (STp->buffer)->buffer_bytes);
1557 #endif
1558 if((STp->buffer)->buffer_bytes ==0&&
1559 STps->eof >= ST_EOD_1) {
1560 if(STps->eof < ST_EOD) {
1561 STps->eof +=1;
1562 return0;
1564 return(-EIO);/* EOM or Blank Check */
1567 /* Check the buffer writability before any tape movement. Don't alter
1568 buffer data. */
1569 if(copy_from_user(&i, buf,1) !=0||
1570 copy_to_user(buf, &i,1) !=0||
1571 copy_from_user(&i, buf + count -1,1) !=0||
1572 copy_to_user(buf + count -1, &i,1) !=0)
1573 return(-EFAULT);
1575 STps->rw = ST_READING;
1578 /* Loop until enough data in buffer or a special condition found */
1579 for(total =0, special =0; total < count && !special; ) {
1581 /* Get new data if the buffer is empty */
1582 if((STp->buffer)->buffer_bytes ==0) {
1583 special =read_tape(inode, count - total, &SCpnt);
1584 if(special <0) {/* No need to continue read */
1585 if(SCpnt != NULL)
1587 scsi_release_command(SCpnt);
1589 return special;
1593 /* Move the data from driver buffer to user buffer */
1594 if((STp->buffer)->buffer_bytes >0) {
1595 #if DEBUG
1596 if(debugging && STps->eof != ST_NOEOF)
1597 printk(ST_DEB_MSG "st%d: EOF up (%d). Left %d, needed %d.\n", dev,
1598 STps->eof, (STp->buffer)->buffer_bytes, count - total);
1599 #endif
1600 transfer = (STp->buffer)->buffer_bytes < count - total ?
1601 (STp->buffer)->buffer_bytes : count - total;
1602 i =copy_to_user(buf, (STp->buffer)->b_data +
1603 (STp->buffer)->read_pointer, transfer);
1604 if(i) {
1605 if(SCpnt != NULL)
1607 scsi_release_command(SCpnt);
1608 SCpnt = NULL;
1610 return(-EFAULT);
1612 filp->f_pos += transfer;
1613 buf += transfer;
1614 total += transfer;
1615 (STp->buffer)->buffer_bytes -= transfer;
1616 (STp->buffer)->read_pointer += transfer;
1619 if(STp->block_size ==0)
1620 break;/* Read only one variable length block */
1622 }/* for (total = 0, special = 0; total < count && !special; ) */
1624 if(SCpnt != NULL)
1626 scsi_release_command(SCpnt);
1627 SCpnt = NULL;
1630 /* Change the eof state if no data from tape or buffer */
1631 if(total ==0) {
1632 if(STps->eof == ST_FM_HIT) {
1633 STps->eof = ST_FM;
1634 STps->drv_block =0;
1635 if(STps->drv_file >=0)
1636 STps->drv_file++;
1638 else if(STps->eof == ST_EOD_1) {
1639 STps->eof = ST_EOD_2;
1640 STps->drv_block =0;
1641 if(STps->drv_file >=0)
1642 STps->drv_file++;
1644 else if(STps->eof == ST_EOD_2)
1645 STps->eof = ST_EOD;
1647 else if(STps->eof == ST_FM)
1648 STps->eof = ST_NOEOF;
1650 return total;
1655 /* Set the driver options */
1656 static void
1657 st_log_options(Scsi_Tape *STp, ST_mode *STm,int dev)
1659 printk(KERN_INFO
1660 "st%d: Mode %d options: buffer writes: %d, async writes: %d, read ahead: %d\n",
1661 dev, STp->current_mode, STm->do_buffer_writes, STm->do_async_writes,
1662 STm->do_read_ahead);
1663 printk(KERN_INFO
1664 "st%d: can bsr: %d, two FMs: %d, fast mteom: %d, auto lock: %d,\n",
1665 dev, STp->can_bsr, STp->two_fm, STp->fast_mteom, STp->do_auto_lock);
1666 printk(KERN_INFO
1667 "st%d: defs for wr: %d, no block limits: %d, partitions: %d, s2 log: %d\n",
1668 dev, STm->defaults_for_writes, STp->omit_blklims, STp->can_partitions,
1669 STp->scsi2_logical);
1670 printk(KERN_INFO
1671 "st%d: sysv: %d\n", dev, STm->sysv);
1672 #if DEBUG
1673 printk(KERN_INFO
1674 "st%d: debugging: %d\n",
1675 dev, debugging);
1676 #endif
1680 static int
1681 st_set_options(struct inode * inode,long options)
1683 int value;
1684 long code;
1685 Scsi_Tape *STp;
1686 ST_mode *STm;
1687 int dev =TAPE_NR(inode->i_rdev);
1689 STp = &(scsi_tapes[dev]);
1690 STm = &(STp->modes[STp->current_mode]);
1691 if(!STm->defined) {
1692 memcpy(STm, &(STp->modes[0]),sizeof(ST_mode));
1693 modes_defined = TRUE;
1694 #if DEBUG
1695 if(debugging)
1696 printk(ST_DEB_MSG "st%d: Initialized mode %d definition from mode 0\n",
1697 dev, STp->current_mode);
1698 #endif
1701 code = options & MT_ST_OPTIONS;
1702 if(code == MT_ST_BOOLEANS) {
1703 STm->do_buffer_writes = (options & MT_ST_BUFFER_WRITES) !=0;
1704 STm->do_async_writes = (options & MT_ST_ASYNC_WRITES) !=0;
1705 STm->defaults_for_writes = (options & MT_ST_DEF_WRITES) !=0;
1706 STm->do_read_ahead = (options & MT_ST_READ_AHEAD) !=0;
1707 STp->two_fm = (options & MT_ST_TWO_FM) !=0;
1708 STp->fast_mteom = (options & MT_ST_FAST_MTEOM) !=0;
1709 STp->do_auto_lock = (options & MT_ST_AUTO_LOCK) !=0;
1710 STp->can_bsr = (options & MT_ST_CAN_BSR) !=0;
1711 STp->omit_blklims = (options & MT_ST_NO_BLKLIMS) !=0;
1712 if((STp->device)->scsi_level >= SCSI_2)
1713 STp->can_partitions = (options & MT_ST_CAN_PARTITIONS) !=0;
1714 STp->scsi2_logical = (options & MT_ST_SCSI2LOGICAL) !=0;
1715 STm->sysv = (options & MT_ST_SYSV) !=0;
1716 #if DEBUG
1717 debugging = (options & MT_ST_DEBUGGING) !=0;
1718 #endif
1719 st_log_options(STp, STm, dev);
1721 else if(code == MT_ST_SETBOOLEANS || code == MT_ST_CLEARBOOLEANS) {
1722 value = (code == MT_ST_SETBOOLEANS);
1723 if((options & MT_ST_BUFFER_WRITES) !=0)
1724 STm->do_buffer_writes = value;
1725 if((options & MT_ST_ASYNC_WRITES) !=0)
1726 STm->do_async_writes = value;
1727 if((options & MT_ST_DEF_WRITES) !=0)
1728 STm->defaults_for_writes = value;
1729 if((options & MT_ST_READ_AHEAD) !=0)
1730 STm->do_read_ahead = value;
1731 if((options & MT_ST_TWO_FM) !=0)
1732 STp->two_fm = value;
1733 if((options & MT_ST_FAST_MTEOM) !=0)
1734 STp->fast_mteom = value;
1735 if((options & MT_ST_AUTO_LOCK) !=0)
1736 STp->do_auto_lock = value;
1737 if((options & MT_ST_CAN_BSR) !=0)
1738 STp->can_bsr = value;
1739 if((options & MT_ST_NO_BLKLIMS) !=0)
1740 STp->omit_blklims = value;
1741 if((STp->device)->scsi_level >= SCSI_2 &&
1742 (options & MT_ST_CAN_PARTITIONS) !=0)
1743 STp->can_partitions = value;
1744 if((options & MT_ST_SCSI2LOGICAL) !=0)
1745 STp->scsi2_logical = value;
1746 if((options & MT_ST_SYSV) !=0)
1747 STm->sysv = value;
1748 #if DEBUG
1749 if((options & MT_ST_DEBUGGING) !=0)
1750 debugging = value;
1751 #endif
1752 st_log_options(STp, STm, dev);
1754 else if(code == MT_ST_WRITE_THRESHOLD) {
1755 value = (options & ~MT_ST_OPTIONS) * ST_KILOBYTE;
1756 if(value <1|| value > st_buffer_size) {
1757 printk(KERN_WARNING "st%d: Write threshold %d too small or too large.\n",
1758 dev, value);
1759 return(-EIO);
1761 STp->write_threshold = value;
1762 printk(KERN_INFO "st%d: Write threshold set to %d bytes.\n",
1763 dev, value);
1765 else if(code == MT_ST_DEF_BLKSIZE) {
1766 value = (options & ~MT_ST_OPTIONS);
1767 if(value == ~MT_ST_OPTIONS) {
1768 STm->default_blksize = (-1);
1769 printk(KERN_INFO "st%d: Default block size disabled.\n", dev);
1771 else{
1772 STm->default_blksize = value;
1773 printk(KERN_INFO "st%d: Default block size set to %d bytes.\n",
1774 dev, STm->default_blksize);
1777 else if(code == MT_ST_TIMEOUTS) {
1778 value = (options & ~MT_ST_OPTIONS);
1779 if((value & MT_ST_SET_LONG_TIMEOUT) !=0) {
1780 STp->long_timeout = (value & ~MT_ST_SET_LONG_TIMEOUT) * HZ;
1781 printk(KERN_INFO "st%d: Long timeout set to %d seconds.\n", dev,
1782 (value & ~MT_ST_SET_LONG_TIMEOUT));
1784 else{
1785 STp->timeout = value * HZ;
1786 printk(KERN_INFO "st%d: Normal timeout set to %d seconds.\n", dev,
1787 value);
1790 else if(code == MT_ST_DEF_OPTIONS) {
1791 code = (options & ~MT_ST_CLEAR_DEFAULT);
1792 value = (options & MT_ST_CLEAR_DEFAULT);
1793 if(code == MT_ST_DEF_DENSITY) {
1794 if(value == MT_ST_CLEAR_DEFAULT) {
1795 STm->default_density = (-1);
1796 printk(KERN_INFO "st%d: Density default disabled.\n", dev);
1798 else{
1799 STm->default_density = value &0xff;
1800 printk(KERN_INFO "st%d: Density default set to %x\n",
1801 dev, STm->default_density);
1804 else if(code == MT_ST_DEF_DRVBUFFER) {
1805 if(value == MT_ST_CLEAR_DEFAULT) {
1806 STp->default_drvbuffer =0xff;
1807 printk(KERN_INFO "st%d: Drive buffer default disabled.\n", dev);
1809 else{
1810 STp->default_drvbuffer = value &7;
1811 printk(KERN_INFO "st%d: Drive buffer default set to %x\n",
1812 dev, STp->default_drvbuffer);
1815 else if(code == MT_ST_DEF_COMPRESSION) {
1816 if(value == MT_ST_CLEAR_DEFAULT) {
1817 STm->default_compression = ST_DONT_TOUCH;
1818 printk(KERN_INFO "st%d: Compression default disabled.\n", dev);
1820 else{
1821 STm->default_compression = (value &1? ST_YES : ST_NO);
1822 printk(KERN_INFO "st%d: Compression default set to %x\n",
1823 dev, (value &1));
1827 else
1828 return(-EIO);
1830 return0;
1834 #define COMPRESSION_PAGE 0x0f
1835 #define COMPRESSION_PAGE_LENGTH 16
1837 #define MODE_HEADER_LENGTH 4
1839 #define DCE_MASK 0x80
1840 #define DCC_MASK 0x40
1841 #define RED_MASK 0x60
1844 /* Control the compression with mode page 15. Algorithm not changed if zero. */
1845 static int
1846 st_compression(Scsi_Tape * STp,int state)
1848 int dev;
1849 unsigned char cmd[10];
1850 Scsi_Cmnd * SCpnt = NULL;
1852 if(STp->ready != ST_READY)
1853 return(-EIO);
1855 /* Read the current page contents */
1856 memset(cmd,0,10);
1857 cmd[0] = MODE_SENSE;
1858 cmd[1] =8;
1859 cmd[2] = COMPRESSION_PAGE;
1860 cmd[4] = COMPRESSION_PAGE_LENGTH + MODE_HEADER_LENGTH;
1862 SCpnt =st_do_scsi(SCpnt, STp, cmd, cmd[4], STp->timeout,0);
1863 if(SCpnt == NULL)
1864 return(-EBUSY);
1865 dev =TAPE_NR(SCpnt->request.rq_dev);
1867 if((STp->buffer)->last_result_fatal !=0) {
1868 #if DEBUG
1869 if(debugging)
1870 printk(ST_DEB_MSG "st%d: Compression mode page not supported.\n", dev);
1871 #endif
1872 scsi_release_command(SCpnt);
1873 SCpnt = NULL;
1874 return(-EIO);
1876 #if DEBUG
1877 if(debugging)
1878 printk(ST_DEB_MSG "st%d: Compression state is %d.\n", dev,
1879 ((STp->buffer)->b_data[MODE_HEADER_LENGTH +2] & DCE_MASK ?1:0));
1880 #endif
1882 /* Check if compression can be changed */
1883 if(((STp->buffer)->b_data[MODE_HEADER_LENGTH +2] & DCC_MASK) ==0) {
1884 #if DEBUG
1885 if(debugging)
1886 printk(ST_DEB_MSG "st%d: Compression not supported.\n", dev);
1887 #endif
1888 scsi_release_command(SCpnt);
1889 SCpnt = NULL;
1890 return(-EIO);
1893 /* Do the change */
1894 if(state)
1895 (STp->buffer)->b_data[MODE_HEADER_LENGTH +2] |= DCE_MASK;
1896 else
1897 (STp->buffer)->b_data[MODE_HEADER_LENGTH +2] &= ~DCE_MASK;
1899 memset(cmd,0,10);
1900 cmd[0] = MODE_SELECT;
1901 cmd[1] =0x10;
1902 cmd[4] = COMPRESSION_PAGE_LENGTH + MODE_HEADER_LENGTH;
1904 (STp->buffer)->b_data[0] =0;/* Reserved data length */
1905 (STp->buffer)->b_data[1] =0;/* Reserved media type byte */
1906 (STp->buffer)->b_data[MODE_HEADER_LENGTH] &=0x3f;
1907 SCpnt =st_do_scsi(SCpnt, STp, cmd, cmd[4], STp->timeout,0);
1909 if((STp->buffer)->last_result_fatal !=0) {
1910 #if DEBUG
1911 if(debugging)
1912 printk(ST_DEB_MSG "st%d: Compression change failed.\n", dev);
1913 #endif
1914 scsi_release_command(SCpnt);
1915 SCpnt = NULL;
1916 return(-EIO);
1919 #if DEBUG
1920 if(debugging)
1921 printk(ST_DEB_MSG "st%d: Compression state changed to %d.\n",
1922 dev, state);
1923 #endif
1925 scsi_release_command(SCpnt);
1926 SCpnt = NULL;
1927 STp->compression_changed = TRUE;
1928 return0;
1932 /* Internal ioctl function */
1933 static int
1934 st_int_ioctl(struct inode * inode,
1935 unsigned int cmd_in,unsigned long arg)
1937 int timeout;
1938 long ltmp;
1939 int i, ioctl_result;
1940 int chg_eof = TRUE;
1941 unsigned char cmd[10];
1942 Scsi_Cmnd * SCpnt;
1943 Scsi_Tape * STp;
1944 ST_partstat * STps;
1945 int fileno, blkno, at_sm, undone, datalen;
1946 int dev =TAPE_NR(inode->i_rdev);
1948 STp = &(scsi_tapes[dev]);
1949 if(STp->ready != ST_READY && cmd_in != MTLOAD) {
1950 if(STp->ready == ST_NO_TAPE)
1951 return(-ENOMEDIUM);
1952 else
1953 return(-EIO);
1955 timeout = STp->long_timeout;
1956 STps = &(STp->ps[STp->partition]);
1957 fileno = STps->drv_file;
1958 blkno = STps->drv_block;
1959 at_sm = STps->at_sm;
1961 memset(cmd,0,10);
1962 datalen =0;
1963 switch(cmd_in) {
1964 case MTFSFM:
1965 chg_eof = FALSE;/* Changed from the FSF after this */
1966 case MTFSF:
1967 cmd[0] = SPACE;
1968 cmd[1] =0x01;/* Space FileMarks */
1969 cmd[2] = (arg >>16);
1970 cmd[3] = (arg >>8);
1971 cmd[4] = arg;
1972 #if DEBUG
1973 if(debugging)
1974 printk(ST_DEB_MSG "st%d: Spacing tape forward over %d filemarks.\n",
1975 dev, cmd[2] *65536+ cmd[3] *256+ cmd[4]);
1976 #endif
1977 if(fileno >=0)
1978 fileno += arg;
1979 blkno =0;
1980 at_sm &= (arg ==0);
1981 break;
1982 case MTBSFM:
1983 chg_eof = FALSE;/* Changed from the FSF after this */
1984 case MTBSF:
1985 cmd[0] = SPACE;
1986 cmd[1] =0x01;/* Space FileMarks */
1987 ltmp = (-arg);
1988 cmd[2] = (ltmp >>16);
1989 cmd[3] = (ltmp >>8);
1990 cmd[4] = ltmp;
1991 #if DEBUG
1992 if(debugging) {
1993 if(cmd[2] &0x80)
1994 ltmp =0xff000000;
1995 ltmp = ltmp | (cmd[2] <<16) | (cmd[3] <<8) | cmd[4];
1996 printk(ST_DEB_MSG "st%d: Spacing tape backward over %ld filemarks.\n",
1997 dev, (-ltmp));
1999 #endif
2000 if(fileno >=0)
2001 fileno -= arg;
2002 blkno = (-1);/* We can't know the block number */
2003 at_sm &= (arg ==0);
2004 break;
2005 case MTFSR:
2006 cmd[0] = SPACE;
2007 cmd[1] =0x00;/* Space Blocks */
2008 cmd[2] = (arg >>16);
2009 cmd[3] = (arg >>8);
2010 cmd[4] = arg;
2011 #if DEBUG
2012 if(debugging)
2013 printk(ST_DEB_MSG "st%d: Spacing tape forward %d blocks.\n", dev,
2014 cmd[2] *65536+ cmd[3] *256+ cmd[4]);
2015 #endif
2016 if(blkno >=0)
2017 blkno += arg;
2018 at_sm &= (arg ==0);
2019 break;
2020 case MTBSR:
2021 cmd[0] = SPACE;
2022 cmd[1] =0x00;/* Space Blocks */
2023 ltmp = (-arg);
2024 cmd[2] = (ltmp >>16);
2025 cmd[3] = (ltmp >>8);
2026 cmd[4] = ltmp;
2027 #if DEBUG
2028 if(debugging) {
2029 if(cmd[2] &0x80)
2030 ltmp =0xff000000;
2031 ltmp = ltmp | (cmd[2] <<16) | (cmd[3] <<8) | cmd[4];
2032 printk(ST_DEB_MSG "st%d: Spacing tape backward %ld blocks.\n", dev, (-ltmp));
2034 #endif
2035 if(blkno >=0)
2036 blkno -= arg;
2037 at_sm &= (arg ==0);
2038 break;
2039 case MTFSS:
2040 cmd[0] = SPACE;
2041 cmd[1] =0x04;/* Space Setmarks */
2042 cmd[2] = (arg >>16);
2043 cmd[3] = (arg >>8);
2044 cmd[4] = arg;
2045 #if DEBUG
2046 if(debugging)
2047 printk(ST_DEB_MSG "st%d: Spacing tape forward %d setmarks.\n", dev,
2048 cmd[2] *65536+ cmd[3] *256+ cmd[4]);
2049 #endif
2050 if(arg !=0) {
2051 blkno = fileno = (-1);
2052 at_sm =1;
2054 break;
2055 case MTBSS:
2056 cmd[0] = SPACE;
2057 cmd[1] =0x04;/* Space Setmarks */
2058 ltmp = (-arg);
2059 cmd[2] = (ltmp >>16);
2060 cmd[3] = (ltmp >>8);
2061 cmd[4] = ltmp;
2062 #if DEBUG
2063 if(debugging) {
2064 if(cmd[2] &0x80)
2065 ltmp =0xff000000;
2066 ltmp = ltmp | (cmd[2] <<16) | (cmd[3] <<8) | cmd[4];
2067 printk(ST_DEB_MSG "st%d: Spacing tape backward %ld setmarks.\n",
2068 dev, (-ltmp));
2070 #endif
2071 if(arg !=0) {
2072 blkno = fileno = (-1);
2073 at_sm =1;
2075 break;
2076 case MTWEOF:
2077 case MTWSM:
2078 if(STp->write_prot)
2079 return(-EACCES);
2080 cmd[0] = WRITE_FILEMARKS;
2081 if(cmd_in == MTWSM)
2082 cmd[1] =2;
2083 cmd[2] = (arg >>16);
2084 cmd[3] = (arg >>8);
2085 cmd[4] = arg;
2086 timeout = STp->timeout;
2087 #if DEBUG
2088 if(debugging) {
2089 if(cmd_in == MTWEOF)
2090 printk(ST_DEB_MSG "st%d: Writing %d filemarks.\n", dev,
2091 cmd[2] *65536+ cmd[3] *256+ cmd[4]);
2092 else
2093 printk(ST_DEB_MSG "st%d: Writing %d setmarks.\n", dev,
2094 cmd[2] *65536+ cmd[3] *256+ cmd[4]);
2096 #endif
2097 if(fileno >=0)
2098 fileno += arg;
2099 blkno =0;
2100 at_sm = (cmd_in == MTWSM);
2101 break;
2102 case MTREW:
2103 cmd[0] = REZERO_UNIT;
2104 #if ST_NOWAIT
2105 cmd[1] =1;/* Don't wait for completion */
2106 timeout = STp->timeout;
2107 #endif
2108 #if DEBUG
2109 if(debugging)
2110 printk(ST_DEB_MSG "st%d: Rewinding tape.\n", dev);
2111 #endif
2112 fileno = blkno = at_sm =0;
2113 break;
2114 case MTOFFL:
2115 case MTLOAD:
2116 case MTUNLOAD:
2117 cmd[0] = START_STOP;
2118 if(cmd_in == MTLOAD)
2119 cmd[4] |=1;
2121 * If arg >= 1 && arg <= 6 Enhanced load/unload in HP C1553A
2123 if(cmd_in != MTOFFL &&
2124 arg >=1+ MT_ST_HPLOADER_OFFSET
2125 && arg <=6+ MT_ST_HPLOADER_OFFSET) {
2126 #if DEBUG
2127 if(debugging) {
2128 printk(ST_DEB_MSG "st%d: Enhanced %sload slot %2ld.\n",
2129 dev, (cmd[4]) ?"":"un",
2130 arg - MT_ST_HPLOADER_OFFSET);
2132 #endif
2133 cmd[3] = arg - MT_ST_HPLOADER_OFFSET;/* MediaID field of C1553A */
2135 #if ST_NOWAIT
2136 cmd[1] =1;/* Don't wait for completion */
2137 timeout = STp->timeout;
2138 #else
2139 timeout = STp->long_timeout;
2140 #endif
2141 #if DEBUG
2142 if(debugging) {
2143 if(cmd_in != MTLOAD)
2144 printk(ST_DEB_MSG "st%d: Unloading tape.\n", dev);
2145 else
2146 printk(ST_DEB_MSG "st%d: Loading tape.\n", dev);
2148 #endif
2149 fileno = blkno = at_sm =0;
2150 break;
2151 case MTNOP:
2152 #if DEBUG
2153 if(debugging)
2154 printk(ST_DEB_MSG "st%d: No op on tape.\n", dev);
2155 #endif
2156 return0;/* Should do something ? */
2157 break;
2158 case MTRETEN:
2159 cmd[0] = START_STOP;
2160 #if ST_NOWAIT
2161 cmd[1] =1;/* Don't wait for completion */
2162 timeout = STp->timeout;
2163 #endif
2164 cmd[4] =3;
2165 #if DEBUG
2166 if(debugging)
2167 printk(ST_DEB_MSG "st%d: Retensioning tape.\n", dev);
2168 #endif
2169 fileno = blkno = at_sm =0;
2170 break;
2171 case MTEOM:
2172 if(!STp->fast_mteom) {
2173 /* space to the end of tape */
2174 ioctl_result =st_int_ioctl(inode, MTFSF,0x3fff);
2175 fileno = STps->drv_file;
2176 if(STps->eof >= ST_EOD_1)
2177 return0;
2178 /* The next lines would hide the number of spaced FileMarks
2179 That's why I inserted the previous lines. I had no luck
2180 with detecting EOM with FSF, so we go now to EOM.
2181 Joerg Weule */
2183 else
2184 fileno = (-1);
2185 cmd[0] = SPACE;
2186 cmd[1] =3;
2187 #if DEBUG
2188 if(debugging)
2189 printk(ST_DEB_MSG "st%d: Spacing to end of recorded medium.\n", dev);
2190 #endif
2191 blkno =0;
2192 at_sm =0;
2193 break;
2194 case MTERASE:
2195 if(STp->write_prot)
2196 return(-EACCES);
2197 cmd[0] = ERASE;
2198 cmd[1] =1;/* To the end of tape */
2199 #if ST_NOWAIT
2200 cmd[1] |=2;/* Don't wait for completion */
2201 timeout = STp->timeout;
2202 #else
2203 timeout = STp->long_timeout *8;
2204 #endif
2205 #if DEBUG
2206 if(debugging)
2207 printk(ST_DEB_MSG "st%d: Erasing tape.\n", dev);
2208 #endif
2209 fileno = blkno = at_sm =0;
2210 break;
2211 case MTLOCK:
2212 chg_eof = FALSE;
2213 cmd[0] = ALLOW_MEDIUM_REMOVAL;
2214 cmd[4] = SCSI_REMOVAL_PREVENT;
2215 #if DEBUG
2216 if(debugging)
2217 printk(ST_DEB_MSG "st%d: Locking drive door.\n", dev);
2218 #endif;
2219 break;
2220 case MTUNLOCK:
2221 chg_eof = FALSE;
2222 cmd[0] = ALLOW_MEDIUM_REMOVAL;
2223 cmd[4] = SCSI_REMOVAL_ALLOW;
2224 #if DEBUG
2225 if(debugging)
2226 printk(ST_DEB_MSG "st%d: Unlocking drive door.\n", dev);
2227 #endif;
2228 break;
2229 case MTSETBLK:/* Set block length */
2230 case MTSETDENSITY:/* Set tape density */
2231 case MTSETDRVBUFFER:/* Set drive buffering */
2232 case SET_DENS_AND_BLK:/* Set density and block size */
2233 chg_eof = FALSE;
2234 if(STp->dirty || (STp->buffer)->buffer_bytes !=0)
2235 return(-EIO);/* Not allowed if data in buffer */
2236 if((cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) &&
2237 (arg & MT_ST_BLKSIZE_MASK) !=0&&
2238 ((arg & MT_ST_BLKSIZE_MASK) < STp->min_block ||
2239 (arg & MT_ST_BLKSIZE_MASK) > STp->max_block ||
2240 (arg & MT_ST_BLKSIZE_MASK) > st_buffer_size)) {
2241 printk(KERN_WARNING "st%d: Illegal block size.\n", dev);
2242 return(-EINVAL);
2244 cmd[0] = MODE_SELECT;
2245 cmd[4] = datalen =12;
2247 memset((STp->buffer)->b_data,0,12);
2248 if(cmd_in == MTSETDRVBUFFER)
2249 (STp->buffer)->b_data[2] = (arg &7) <<4;
2250 else
2251 (STp->buffer)->b_data[2] =
2252 STp->drv_buffer <<4;
2253 (STp->buffer)->b_data[3] =8;/* block descriptor length */
2254 if(cmd_in == MTSETDENSITY) {
2255 (STp->buffer)->b_data[4] = arg;
2256 STp->density_changed = TRUE;/* At least we tried ;-) */
2258 else if(cmd_in == SET_DENS_AND_BLK)
2259 (STp->buffer)->b_data[4] = arg >>24;
2260 else
2261 (STp->buffer)->b_data[4] = STp->density;
2262 if(cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) {
2263 ltmp = arg & MT_ST_BLKSIZE_MASK;
2264 if(cmd_in == MTSETBLK)
2265 STp->blksize_changed = TRUE;/* At least we tried ;-) */
2267 else
2268 ltmp = STp->block_size;
2269 (STp->buffer)->b_data[9] = (ltmp >>16);
2270 (STp->buffer)->b_data[10] = (ltmp >>8);
2271 (STp->buffer)->b_data[11] = ltmp;
2272 timeout = STp->timeout;
2273 #if DEBUG
2274 if(debugging) {
2275 if(cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK)
2276 printk(ST_DEB_MSG "st%d: Setting block size to %d bytes.\n", dev,
2277 (STp->buffer)->b_data[9] *65536+
2278 (STp->buffer)->b_data[10] *256+
2279 (STp->buffer)->b_data[11]);
2280 if(cmd_in == MTSETDENSITY || cmd_in == SET_DENS_AND_BLK)
2281 printk(ST_DEB_MSG "st%d: Setting density code to %x.\n", dev,
2282 (STp->buffer)->b_data[4]);
2283 if(cmd_in == MTSETDRVBUFFER)
2284 printk(ST_DEB_MSG "st%d: Setting drive buffer code to %d.\n", dev,
2285 ((STp->buffer)->b_data[2] >>4) &7);
2287 #endif
2288 break;
2289 default:
2290 return(-ENOSYS);
2293 SCpnt =st_do_scsi(NULL, STp, cmd, datalen, timeout, MAX_RETRIES);
2294 if(!SCpnt)
2295 return(-EBUSY);
2297 ioctl_result = (STp->buffer)->last_result_fatal;
2299 if(!ioctl_result) {/* SCSI command successful */
2300 scsi_release_command(SCpnt);
2301 SCpnt = NULL;
2302 STps->drv_block = blkno;
2303 STps->drv_file = fileno;
2304 STps->at_sm = at_sm;
2306 if(cmd_in == MTLOCK)
2307 STp->door_locked = ST_LOCKED_EXPLICIT;
2308 else if(cmd_in == MTUNLOCK)
2309 STp->door_locked = ST_UNLOCKED;
2311 if(cmd_in == MTBSFM)
2312 ioctl_result =st_int_ioctl(inode, MTFSF,1);
2313 else if(cmd_in == MTFSFM)
2314 ioctl_result =st_int_ioctl(inode, MTBSF,1);
2316 if(cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) {
2317 STp->block_size = arg & MT_ST_BLKSIZE_MASK;
2318 if(STp->block_size !=0)
2319 (STp->buffer)->buffer_blocks =
2320 (STp->buffer)->buffer_size / STp->block_size;
2321 (STp->buffer)->buffer_bytes = (STp->buffer)->read_pointer =0;
2322 if(cmd_in == SET_DENS_AND_BLK)
2323 STp->density = arg >> MT_ST_DENSITY_SHIFT;
2325 else if(cmd_in == MTSETDRVBUFFER)
2326 STp->drv_buffer = (arg &7);
2327 else if(cmd_in == MTSETDENSITY)
2328 STp->density = arg;
2330 if(cmd_in == MTEOM)
2331 STps->eof = ST_EOD;
2332 else if(cmd_in == MTFSF)
2333 STps->eof = ST_FM;
2334 else if(chg_eof)
2335 STps->eof = ST_NOEOF;
2338 if(cmd_in == MTOFFL || cmd_in == MTUNLOAD)
2339 STp->rew_at_close =0;
2340 else if(cmd_in == MTLOAD) {
2341 STp->rew_at_close = (MINOR(inode->i_rdev) &0x80) ==0;
2342 for(i=0; i < ST_NBR_PARTITIONS; i++) {
2343 STp->ps[i].rw = ST_IDLE;
2344 STp->ps[i].last_block_valid = FALSE;
2346 STp->partition =0;
2349 }else{/* SCSI command was not completely successful. Don't return
2350 from this block without releasing the SCSI command block! */
2352 if(SCpnt->sense_buffer[2] &0x40) {
2353 if(cmd_in != MTBSF && cmd_in != MTBSFM &&
2354 cmd_in != MTBSR && cmd_in != MTBSS)
2355 STps->eof = ST_EOM_OK;
2356 STps->drv_block =0;
2359 undone = (
2360 (SCpnt->sense_buffer[3] <<24) +
2361 (SCpnt->sense_buffer[4] <<16) +
2362 (SCpnt->sense_buffer[5] <<8) +
2363 SCpnt->sense_buffer[6] );
2364 if(cmd_in == MTWEOF &&
2365 (SCpnt->sense_buffer[0] &0x70) ==0x70&&
2366 (SCpnt->sense_buffer[2] &0x4f) ==0x40&&
2367 ((SCpnt->sense_buffer[0] &0x80) ==0|| undone ==0)) {
2368 ioctl_result =0;/* EOF written succesfully at EOM */
2369 if(fileno >=0)
2370 fileno++;
2371 STps->drv_file = fileno;
2372 STps->eof = ST_NOEOF;
2374 else if( (cmd_in == MTFSF) || (cmd_in == MTFSFM) ) {
2375 if(fileno >=0)
2376 STps->drv_file = fileno - undone ;
2377 else
2378 STps->drv_file = fileno;
2379 STps->drv_block =0;
2380 STps->eof = ST_NOEOF;
2382 else if( (cmd_in == MTBSF) || (cmd_in == MTBSFM) ) {
2383 if(fileno >=0)
2384 STps->drv_file = fileno + undone ;
2385 else
2386 STps->drv_file = fileno;
2387 STps->drv_block =0;
2388 STps->eof = ST_NOEOF;
2390 else if(cmd_in == MTFSR) {
2391 if(SCpnt->sense_buffer[2] &0x80) {/* Hit filemark */
2392 if(STps->drv_file >=0)
2393 STps->drv_file++;
2394 STps->drv_block =0;
2395 STps->eof = ST_FM;
2397 else{
2398 if(blkno >= undone)
2399 STps->drv_block = blkno - undone;
2400 else
2401 STps->drv_block = (-1);
2402 STps->eof = ST_NOEOF;
2405 else if(cmd_in == MTBSR) {
2406 if(SCpnt->sense_buffer[2] &0x80) {/* Hit filemark */
2407 STps->drv_file--;
2408 STps->drv_block = (-1);
2410 else{
2411 if(blkno >=0)
2412 STps->drv_block = blkno + undone;
2413 else
2414 STps->drv_block = (-1);
2416 STps->eof = ST_NOEOF;
2418 else if(cmd_in == MTEOM) {
2419 STps->drv_file = (-1);
2420 STps->drv_block = (-1);
2421 STps->eof = ST_EOD;
2423 else if(chg_eof)
2424 STps->eof = ST_NOEOF;
2426 if((SCpnt->sense_buffer[2] &0x0f) == BLANK_CHECK)
2427 STps->eof = ST_EOD;
2429 if(cmd_in == MTLOCK)
2430 STp->door_locked = ST_LOCK_FAILS;
2432 scsi_release_command(SCpnt);
2433 SCpnt = NULL;
2436 return ioctl_result;
2440 \f/* Get the tape position. If bt == 2, arg points into a kernel space mt_loc
2441 structure. */
2443 static int
2444 get_location(struct inode * inode,unsigned int*block,int*partition,
2445 int logical)
2447 Scsi_Tape *STp;
2448 int dev =TAPE_NR(inode->i_rdev);
2449 int result;
2450 unsigned char scmd[10];
2451 Scsi_Cmnd *SCpnt;
2453 STp = &(scsi_tapes[dev]);
2454 if(STp->ready != ST_READY)
2455 return(-EIO);
2457 memset(scmd,0,10);
2458 if((STp->device)->scsi_level < SCSI_2) {
2459 scmd[0] = QFA_REQUEST_BLOCK;
2460 scmd[4] =3;
2462 else{
2463 scmd[0] = READ_POSITION;
2464 if(!logical && !STp->scsi2_logical)
2465 scmd[1] =1;
2467 SCpnt =st_do_scsi(NULL, STp, scmd,20, STp->timeout, MAX_READY_RETRIES);
2468 if(!SCpnt)
2469 return(-EBUSY);
2471 if((STp->buffer)->last_result_fatal !=0||
2472 (STp->device->scsi_level >= SCSI_2 &&
2473 ((STp->buffer)->b_data[0] &4) !=0)) {
2474 *block = *partition =0;
2475 #if DEBUG
2476 if(debugging)
2477 printk(ST_DEB_MSG "st%d: Can't read tape position.\n", dev);
2478 #endif
2479 result = (-EIO);
2481 else{
2482 result =0;
2483 if((STp->device)->scsi_level < SCSI_2) {
2484 *block = ((STp->buffer)->b_data[0] <<16)
2485 + ((STp->buffer)->b_data[1] <<8)
2486 + (STp->buffer)->b_data[2];
2487 *partition =0;
2489 else{
2490 *block = ((STp->buffer)->b_data[4] <<24)
2491 + ((STp->buffer)->b_data[5] <<16)
2492 + ((STp->buffer)->b_data[6] <<8)
2493 + (STp->buffer)->b_data[7];
2494 *partition = (STp->buffer)->b_data[1];
2495 if(((STp->buffer)->b_data[0] &0x80) &&
2496 (STp->buffer)->b_data[1] ==0)/* BOP of partition 0 */
2497 STp->ps[0].drv_block = STp->ps[0].drv_file =0;
2499 #if DEBUG
2500 if(debugging)
2501 printk(ST_DEB_MSG "st%d: Got tape pos. blk %d part %d.\n", dev,
2502 *block, *partition);
2503 #endif
2506 scsi_release_command(SCpnt);
2507 SCpnt = NULL;
2509 return result;
2513 /* Set the tape block and partition. Negative partition means that only the
2514 block should be set in vendor specific way. */
2515 static int
2516 set_location(struct inode * inode,unsigned int block,int partition,
2517 int logical)
2519 Scsi_Tape *STp;
2520 ST_partstat *STps;
2521 int dev =TAPE_NR(inode->i_rdev);
2522 int result, p;
2523 unsigned int blk;
2524 int timeout;
2525 unsigned char scmd[10];
2526 Scsi_Cmnd *SCpnt;
2528 STp = &(scsi_tapes[dev]);
2529 if(STp->ready != ST_READY)
2530 return(-EIO);
2531 timeout = STp->long_timeout;
2532 STps = &(STp->ps[STp->partition]);
2534 #if DEBUG
2535 if(debugging)
2536 printk(ST_DEB_MSG "st%d: Setting block to %d and partition to %d.\n",
2537 dev, block, partition);
2538 if(partition <0)
2539 return(-EIO);
2540 #endif
2542 /* Update the location at the partition we are leaving */
2543 if((!STp->can_partitions && partition !=0) ||
2544 partition >= ST_NBR_PARTITIONS)
2545 return(-EINVAL);
2546 if(partition != STp->partition) {
2547 if(get_location(inode, &blk, &p,1))
2548 STps->last_block_valid = FALSE;
2549 else{
2550 STps->last_block_valid = TRUE;
2551 STps->last_block_visited = blk;
2552 #if DEBUG
2553 if(debugging)
2554 printk(ST_DEB_MSG "st%d: Visited block %d for partition %d saved.\n",
2555 dev, blk, STp->partition);
2556 #endif
2560 memset(scmd,0,10);
2561 if((STp->device)->scsi_level < SCSI_2) {
2562 scmd[0] = QFA_SEEK_BLOCK;
2563 scmd[2] = (block >>16);
2564 scmd[3] = (block >>8);
2565 scmd[4] = block;
2566 scmd[5] =0;
2568 else{
2569 scmd[0] = SEEK_10;
2570 scmd[3] = (block >>24);
2571 scmd[4] = (block >>16);
2572 scmd[5] = (block >>8);
2573 scmd[6] = block;
2574 if(!logical && !STp->scsi2_logical)
2575 scmd[1] =4;
2576 if(STp->partition != partition) {
2577 scmd[1] |=2;
2578 scmd[8] = partition;
2579 #if DEBUG
2580 if(debugging)
2581 printk(ST_DEB_MSG "st%d: Trying to change partition from %d to %d\n",
2582 dev, STp->partition, partition);
2583 #endif
2586 #if ST_NOWAIT
2587 scmd[1] |=1;/* Don't wait for completion */
2588 timeout = STp->timeout;
2589 #endif
2591 SCpnt =st_do_scsi(NULL, STp, scmd,20, timeout, MAX_READY_RETRIES);
2592 if(!SCpnt)
2593 return(-EBUSY);
2595 STps->drv_block = STps->drv_file = (-1);
2596 STps->eof = ST_NOEOF;
2597 if((STp->buffer)->last_result_fatal !=0) {
2598 result = (-EIO);
2599 if(STp->can_partitions &&
2600 (STp->device)->scsi_level >= SCSI_2 &&
2601 (p =find_partition(inode)) >=0)
2602 STp->partition = p;
2604 else{
2605 if(STp->can_partitions) {
2606 STp->partition = partition;
2607 STps = &(STp->ps[partition]);
2608 if(!STps->last_block_valid ||
2609 STps->last_block_visited != block) {
2610 STps->at_sm =0;
2611 STps->rw = ST_IDLE;
2614 else
2615 STps->at_sm =0;
2616 if(block ==0)
2617 STps->drv_block = STps->drv_file =0;
2618 result =0;
2621 scsi_release_command(SCpnt);
2622 SCpnt = NULL;
2624 return result;
2628 /* Find the current partition number for the drive status. Called from open and
2629 returns either partition number of negative error code. */
2630 static int
2631 find_partition(struct inode *inode)
2633 int i, partition;
2634 unsigned int block;
2636 if((i =get_location(inode, &block, &partition,1)) <0)
2637 return i;
2638 if(partition >= ST_NBR_PARTITIONS)
2639 return(-EIO);
2640 return partition;
2644 /* Change the partition if necessary */
2645 static int
2646 update_partition(struct inode * inode)
2648 int dev =TAPE_NR(inode->i_rdev);
2649 Scsi_Tape *STp;
2650 ST_partstat *STps;
2652 STp = &(scsi_tapes[dev]);
2653 if(STp->partition == STp->new_partition)
2654 return0;
2655 STps = &(STp->ps[STp->new_partition]);
2656 if(!STps->last_block_valid)
2657 STps->last_block_visited =0;
2658 returnset_location(inode, STps->last_block_visited, STp->new_partition,1);
2661 \f/* Functions for reading and writing the medium partition mode page. These
2662 seem to work with Wangtek 6200HS and HP C1533A. */
2664 #define PART_PAGE 0x11
2665 #define PART_PAGE_LENGTH 10
2667 /* Get the number of partitions on the tape. As a side effect reads the
2668 mode page into the tape buffer. */
2669 static int
2670 nbr_partitions(struct inode * inode)
2672 int dev =TAPE_NR(inode->i_rdev), result;
2673 Scsi_Tape *STp;
2674 Scsi_Cmnd * SCpnt = NULL;
2675 unsigned char cmd[10];
2677 STp = &(scsi_tapes[dev]);
2678 if(STp->ready != ST_READY)
2679 return(-EIO);
2681 memset((void*) &cmd[0],0,10);
2682 cmd[0] = MODE_SENSE;
2683 cmd[1] =8;/* Page format */
2684 cmd[2] = PART_PAGE;
2685 cmd[4] =200;
2687 SCpnt =st_do_scsi(SCpnt, STp, cmd,200, STp->timeout, MAX_READY_RETRIES);
2688 if(SCpnt == NULL)
2689 return(-EBUSY);
2690 scsi_release_command(SCpnt);
2691 SCpnt = NULL;
2693 if((STp->buffer)->last_result_fatal !=0) {
2694 #if DEBUG
2695 if(debugging)
2696 printk(ST_DEB_MSG "st%d: Can't read medium partition page.\n", dev);
2697 #endif
2698 result = (-EIO);
2700 else{
2701 result = (STp->buffer)->b_data[MODE_HEADER_LENGTH +3] +1;
2702 #if DEBUG
2703 if(debugging)
2704 printk(ST_DEB_MSG "st%d: Number of partitions %d.\n", dev, result);
2705 #endif
2708 return result;
2712 /* Partition the tape into two partitions if size > 0 or one partition if
2713 size == 0 */
2714 static int
2715 partition_tape(struct inode * inode,int size)
2717 int dev =TAPE_NR(inode->i_rdev), result;
2718 int length;
2719 Scsi_Tape *STp;
2720 Scsi_Cmnd * SCpnt = NULL;
2721 unsigned char cmd[10], *bp;
2723 if((result =nbr_partitions(inode)) <0)
2724 return result;
2725 STp = &(scsi_tapes[dev]);
2727 /* The mode page is in the buffer. Let's modify it and write it. */
2728 bp = &((STp->buffer)->b_data[0]);
2729 if(size <=0) {
2730 length =8;
2731 bp[MODE_HEADER_LENGTH +3] =0;
2732 #if DEBUG
2733 if(debugging)
2734 printk(ST_DEB_MSG "st%d: Formatting tape with one partition.\n", dev);
2735 #endif
2737 else{
2738 length =10;
2739 bp[MODE_HEADER_LENGTH +3] =1;
2740 bp[MODE_HEADER_LENGTH +8] = (size >>8) &0xff;
2741 bp[MODE_HEADER_LENGTH +9] = size &0xff;
2742 #if DEBUG
2743 if(debugging)
2744 printk(ST_DEB_MSG "st%d: Formatting tape with two partition (1 = %d MB).\n",
2745 dev, size);
2746 #endif
2748 bp[MODE_HEADER_LENGTH +6] =0;
2749 bp[MODE_HEADER_LENGTH +7] =0;
2750 bp[MODE_HEADER_LENGTH +4] =0x30;/* IDP | PSUM = MB */
2752 bp[0] =0;
2753 bp[1] =0;
2754 bp[MODE_HEADER_LENGTH] &=0x3f;
2755 bp[MODE_HEADER_LENGTH +1] = length -2;
2757 memset(cmd,0,10);
2758 cmd[0] = MODE_SELECT;
2759 cmd[1] =0x10;
2760 cmd[4] = length + MODE_HEADER_LENGTH;
2762 SCpnt =st_do_scsi(SCpnt, STp, cmd, cmd[4], STp->long_timeout, MAX_READY_RETRIES);
2763 if(SCpnt == NULL)
2764 return(-EBUSY);
2765 scsi_release_command(SCpnt);
2766 SCpnt = NULL;
2768 if((STp->buffer)->last_result_fatal !=0) {
2769 printk(KERN_INFO "st%d: Partitioning of tape failed.\n", dev);
2770 result = (-EIO);
2772 else
2773 result =0;
2775 return result;
2780 /* The ioctl command */
2781 static int
2782 st_ioctl(struct inode * inode,struct file * file,
2783 unsigned int cmd_in,unsigned long arg)
2785 int i, cmd_nr, cmd_type, bt;
2786 unsigned int blk;
2787 struct mtop mtc;
2788 struct mtpos mt_pos;
2789 Scsi_Tape *STp;
2790 ST_mode *STm;
2791 ST_partstat *STps;
2792 int dev =TAPE_NR(inode->i_rdev);
2794 STp = &(scsi_tapes[dev]);
2795 #if DEBUG
2796 if(debugging && !STp->in_use) {
2797 printk(ST_DEB_MSG "st%d: Incorrect device.\n", dev);
2798 return(-EIO);
2800 #endif
2801 STm = &(STp->modes[STp->current_mode]);
2802 STps = &(STp->ps[STp->partition]);
2805 * If we are in the middle of error recovery, don't let anyone
2806 * else try and use this device. Also, if error recovery fails, it
2807 * may try and take the device offline, in which case all further
2808 * access to the device is prohibited.
2810 if( !scsi_block_when_processing_errors(STp->device) )
2812 return-ENXIO;
2815 cmd_type =_IOC_TYPE(cmd_in);
2816 cmd_nr =_IOC_NR(cmd_in);
2818 if(cmd_type ==_IOC_TYPE(MTIOCTOP) && cmd_nr ==_IOC_NR(MTIOCTOP)) {
2819 if(_IOC_SIZE(cmd_in) !=sizeof(mtc))
2820 return(-EINVAL);
2822 i =copy_from_user((char*) &mtc, (char*)arg,sizeof(struct mtop));
2823 if(i)
2824 return(-EFAULT);
2826 if(mtc.mt_op == MTSETDRVBUFFER && !suser()) {
2827 printk(KERN_WARNING "st%d: MTSETDRVBUFFER only allowed for root.\n", dev);
2828 return(-EPERM);
2830 if(!STm->defined &&
2831 (mtc.mt_op != MTSETDRVBUFFER && (mtc.mt_count & MT_ST_OPTIONS) ==0))
2832 return(-ENXIO);
2834 if(!(STp->device)->was_reset) {
2836 if(STps->eof == ST_FM_HIT) {
2837 if(mtc.mt_op == MTFSF || mtc.mt_op == MTFSFM|| mtc.mt_op == MTEOM) {
2838 mtc.mt_count -=1;
2839 if(STps->drv_file >=0)
2840 STps->drv_file +=1;
2842 else if(mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM) {
2843 mtc.mt_count +=1;
2844 if(STps->drv_file >=0)
2845 STps->drv_file +=1;
2849 i =flush_buffer(inode, file,/* mtc.mt_op == MTSEEK || */
2850 mtc.mt_op == MTREW || mtc.mt_op == MTOFFL ||
2851 mtc.mt_op == MTRETEN || mtc.mt_op == MTEOM ||
2852 mtc.mt_op == MTLOCK || mtc.mt_op == MTLOAD ||
2853 mtc.mt_op == MTCOMPRESSION);
2854 if(i <0)
2855 return i;
2857 else{
2859 * If there was a bus reset, block further access
2860 * to this device. If the user wants to rewind the tape,
2861 * then reset the flag and allow access again.
2863 if(mtc.mt_op != MTREW &&
2864 mtc.mt_op != MTOFFL &&
2865 mtc.mt_op != MTRETEN &&
2866 mtc.mt_op != MTERASE &&
2867 mtc.mt_op != MTSEEK &&
2868 mtc.mt_op != MTEOM)
2869 return(-EIO);
2870 STp->device->was_reset =0;
2871 if(STp->door_locked != ST_UNLOCKED &&
2872 STp->door_locked != ST_LOCK_FAILS) {
2873 if(st_int_ioctl(inode, MTLOCK,0)) {
2874 printk(KERN_NOTICE "st%d: Could not relock door after bus reset.\n",
2875 dev);
2876 STp->door_locked = ST_UNLOCKED;
2881 if(mtc.mt_op != MTNOP && mtc.mt_op != MTSETBLK &&
2882 mtc.mt_op != MTSETDENSITY && mtc.mt_op != MTWSM &&
2883 mtc.mt_op != MTSETDRVBUFFER && mtc.mt_op != MTSETPART)
2884 STps->rw = ST_IDLE;/* Prevent automatic WEOF and fsf */
2886 if(mtc.mt_op == MTOFFL && STp->door_locked != ST_UNLOCKED)
2887 st_int_ioctl(inode, MTUNLOCK,0);/* Ignore result! */
2889 if(mtc.mt_op == MTSETDRVBUFFER &&
2890 (mtc.mt_count & MT_ST_OPTIONS) !=0)
2891 returnst_set_options(inode, mtc.mt_count);
2892 if(mtc.mt_op == MTSETPART) {
2893 if(!STp->can_partitions ||
2894 mtc.mt_count <0|| mtc.mt_count >= ST_NBR_PARTITIONS)
2895 return(-EINVAL);
2896 if(mtc.mt_count >= STp->nbr_partitions &&
2897 (STp->nbr_partitions =nbr_partitions(inode)) <0)
2898 return(-EIO);
2899 if(mtc.mt_count >= STp->nbr_partitions)
2900 return(-EINVAL);
2901 STp->new_partition = mtc.mt_count;
2902 return0;
2904 if(mtc.mt_op == MTMKPART) {
2905 if(!STp->can_partitions)
2906 return(-EINVAL);
2907 if((i =st_int_ioctl(inode, MTREW,0)) <0||
2908 (i =partition_tape(inode, mtc.mt_count)) <0)
2909 return i;
2910 for(i=0; i < ST_NBR_PARTITIONS; i++) {
2911 STp->ps[i].rw = ST_IDLE;
2912 STp->ps[i].at_sm =0;
2913 STp->ps[i].last_block_valid = FALSE;
2915 STp->partition = STp->new_partition =0;
2916 STp->nbr_partitions =1;/* Bad guess ?-) */
2917 STps->drv_block = STps->drv_file =0;
2918 return0;
2920 if(mtc.mt_op == MTSEEK) {
2921 i =set_location(inode, mtc.mt_count, STp->new_partition,0);
2922 if(!STp->can_partitions)
2923 STp->ps[0].rw = ST_IDLE;
2924 return i;
2926 if(STp->can_partitions && STp->ready == ST_READY &&
2927 (i =update_partition(inode)) <0)
2928 return i;
2929 if(mtc.mt_op == MTCOMPRESSION)
2930 returnst_compression(STp, (mtc.mt_count &1));
2931 else
2932 returnst_int_ioctl(inode, mtc.mt_op, mtc.mt_count);
2935 if(!STm->defined)
2936 return(-ENXIO);
2938 if((i =flush_buffer(inode, file, FALSE)) <0)
2939 return i;
2940 if(STp->can_partitions &&
2941 (i =update_partition(inode)) <0)
2942 return i;
2944 if(cmd_type ==_IOC_TYPE(MTIOCGET) && cmd_nr ==_IOC_NR(MTIOCGET)) {
2946 if(_IOC_SIZE(cmd_in) !=sizeof(struct mtget))
2947 return(-EINVAL);
2949 (STp->mt_status)->mt_dsreg =
2950 ((STp->block_size << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK) |
2951 ((STp->density << MT_ST_DENSITY_SHIFT) & MT_ST_DENSITY_MASK);
2952 (STp->mt_status)->mt_blkno = STps->drv_block;
2953 (STp->mt_status)->mt_fileno = STps->drv_file;
2954 if(STp->block_size !=0) {
2955 if(STps->rw == ST_WRITING)
2956 (STp->mt_status)->mt_blkno +=
2957 (STp->buffer)->buffer_bytes / STp->block_size;
2958 else if(STps->rw == ST_READING)
2959 (STp->mt_status)->mt_blkno -= ((STp->buffer)->buffer_bytes +
2960 STp->block_size -1) / STp->block_size;
2963 (STp->mt_status)->mt_gstat =0;
2964 if(STp->drv_write_prot)
2965 (STp->mt_status)->mt_gstat |=GMT_WR_PROT(0xffffffff);
2966 if((STp->mt_status)->mt_blkno ==0) {
2967 if((STp->mt_status)->mt_fileno ==0)
2968 (STp->mt_status)->mt_gstat |=GMT_BOT(0xffffffff);
2969 else
2970 (STp->mt_status)->mt_gstat |=GMT_EOF(0xffffffff);
2972 (STp->mt_status)->mt_resid = STp->partition;
2973 if(STps->eof == ST_EOM_OK || STps->eof == ST_EOM_ERROR)
2974 (STp->mt_status)->mt_gstat |=GMT_EOT(0xffffffff);
2975 else if(STps->eof >= ST_EOM_OK)
2976 (STp->mt_status)->mt_gstat |=GMT_EOD(0xffffffff);
2977 if(STp->density ==1)
2978 (STp->mt_status)->mt_gstat |=GMT_D_800(0xffffffff);
2979 else if(STp->density ==2)
2980 (STp->mt_status)->mt_gstat |=GMT_D_1600(0xffffffff);
2981 else if(STp->density ==3)
2982 (STp->mt_status)->mt_gstat |=GMT_D_6250(0xffffffff);
2983 if(STp->ready == ST_READY)
2984 (STp->mt_status)->mt_gstat |=GMT_ONLINE(0xffffffff);
2985 if(STp->ready == ST_NO_TAPE)
2986 (STp->mt_status)->mt_gstat |=GMT_DR_OPEN(0xffffffff);
2987 if(STps->at_sm)
2988 (STp->mt_status)->mt_gstat |=GMT_SM(0xffffffff);
2989 if(STm->do_async_writes || (STm->do_buffer_writes && STp->block_size !=0) ||
2990 STp->drv_buffer !=0)
2991 (STp->mt_status)->mt_gstat |=GMT_IM_REP_EN(0xffffffff);
2993 i =copy_to_user((char*)arg, (char*)(STp->mt_status),
2994 sizeof(struct mtget));
2995 if(i)
2996 return(-EFAULT);
2998 (STp->mt_status)->mt_erreg =0;/* Clear after read */
2999 return0;
3000 }/* End of MTIOCGET */
3002 if(cmd_type ==_IOC_TYPE(MTIOCPOS) && cmd_nr ==_IOC_NR(MTIOCPOS)) {
3003 if(_IOC_SIZE(cmd_in) !=sizeof(struct mtpos))
3004 return(-EINVAL);
3005 if((i =get_location(inode, &blk, &bt,0)) <0)
3006 return i;
3007 mt_pos.mt_blkno = blk;
3008 i =copy_to_user((char*)arg, (char*) (&mt_pos),sizeof(struct mtpos));
3009 if(i)
3010 return(-EFAULT);
3011 return0;
3014 returnscsi_ioctl(STp->device, cmd_in, (void*) arg);
3018 /* Try to allocate a new tape buffer */
3019 static ST_buffer *
3020 new_tape_buffer(int from_initialization,int need_dma )
3022 int priority, a_size;
3023 ST_buffer *tb;
3025 if(st_nbr_buffers >= st_template.dev_max)
3026 return NULL;/* Should never happen */
3028 if(from_initialization) {
3029 priority = GFP_ATOMIC;
3030 a_size = st_buffer_size;
3032 else{
3033 priority = GFP_KERNEL;
3034 for(a_size = PAGE_SIZE; a_size < st_buffer_size; a_size <<=1)
3035 ;/* Make sure we allocate efficiently */
3037 tb = (ST_buffer *)scsi_init_malloc(sizeof(ST_buffer), priority);
3038 if(tb) {
3039 if(need_dma)
3040 priority |= GFP_DMA;
3041 tb->b_data = (unsigned char*)scsi_init_malloc(a_size, priority);
3042 if(!tb->b_data) {
3043 scsi_init_free((char*)tb,sizeof(ST_buffer));
3044 tb = NULL;
3047 if(!tb) {
3048 printk(KERN_NOTICE "st: Can't allocate new tape buffer (nbr %d).\n",
3049 st_nbr_buffers);
3050 return NULL;
3052 #if DEBUG
3053 if(debugging)
3054 printk(ST_DEB_MSG
3055 "st: Allocated tape buffer %d (%d bytes, dma: %d, a: %p).\n",
3056 st_nbr_buffers, a_size, need_dma, tb->b_data);
3057 #endif
3058 tb->in_use =0;
3059 tb->dma = need_dma;
3060 tb->buffer_size = a_size;
3061 tb->writing =0;
3062 tb->orig_b_data = NULL;
3063 st_buffers[st_nbr_buffers++] = tb;
3064 return tb;
3068 /* Try to allocate a temporary enlarged tape buffer */
3069 static int
3070 enlarge_buffer(ST_buffer *STbuffer,int new_size,int need_dma)
3072 int a_size, priority;
3073 unsigned char*tbd;
3075 normalize_buffer(STbuffer);
3077 for(a_size = PAGE_SIZE; a_size < new_size; a_size <<=1)
3078 ;/* Make sure that we allocate efficiently */
3080 priority = GFP_KERNEL;
3081 if(need_dma)
3082 priority |= GFP_DMA;
3083 tbd = (unsigned char*)scsi_init_malloc(a_size, priority);
3084 if(!tbd)
3085 return FALSE;
3086 #if DEBUG
3087 if(debugging)
3088 printk(ST_DEB_MSG
3089 "st: Buffer at %p enlarged to %d bytes (dma: %d, a: %p).\n",
3090 STbuffer->b_data, a_size, need_dma, tbd);
3091 #endif
3093 STbuffer->orig_b_data = STbuffer->b_data;
3094 STbuffer->orig_size = STbuffer->buffer_size;
3095 STbuffer->b_data = tbd;
3096 STbuffer->buffer_size = a_size;
3097 return TRUE;
3101 /* Release the extra buffer */
3102 static void
3103 normalize_buffer(ST_buffer *STbuffer)
3105 if(STbuffer->orig_b_data == NULL)
3106 return;
3108 scsi_init_free(STbuffer->b_data, STbuffer->buffer_size);
3109 STbuffer->b_data = STbuffer->orig_b_data;
3110 STbuffer->orig_b_data = NULL;
3111 STbuffer->buffer_size = STbuffer->orig_size;
3113 #if DEBUG
3114 if(debugging)
3115 printk(ST_DEB_MSG "st: Buffer at %p normalized to %d bytes.\n",
3116 STbuffer->b_data, STbuffer->buffer_size);
3117 #endif
3121 #ifndef MODULE
3122 /* Set the boot options. Syntax: st=xxx,yyy
3123 where xxx is buffer size in 1024 byte blocks and yyy is write threshold
3124 in 1024 byte blocks. */
3125 __initfunc(void
3126 st_setup(char*str,int*ints))
3128 if(ints[0] >0&& ints[1] >0)
3129 st_buffer_size = ints[1] * ST_KILOBYTE;
3130 if(ints[0] >1&& ints[2] >0) {
3131 st_write_threshold = ints[2] * ST_KILOBYTE;
3132 if(st_write_threshold > st_buffer_size)
3133 st_write_threshold = st_buffer_size;
3135 if(ints[0] >2&& ints[3] >0)
3136 st_max_buffers = ints[3];
3138 #endif
3141 static struct file_operations st_fops = {
3142 NULL,/* lseek - default */
3143 st_read,/* read - general block-dev read */
3144 st_write,/* write - general block-dev write */
3145 NULL,/* readdir - bad */
3146 NULL,/* select */
3147 st_ioctl,/* ioctl */
3148 NULL,/* mmap */
3149 scsi_tape_open,/* open */
3150 scsi_tape_close,/* release */
3151 NULL /* fsync */
3154 static intst_attach(Scsi_Device * SDp){
3155 Scsi_Tape * tpnt;
3156 ST_mode * STm;
3157 ST_partstat * STps;
3158 int i;
3160 if(SDp->type != TYPE_TAPE)return1;
3162 if(st_template.nr_dev >= st_template.dev_max)
3164 SDp->attached--;
3165 return1;
3168 for(tpnt = scsi_tapes, i=0; i<st_template.dev_max; i++, tpnt++)
3169 if(!tpnt->device)break;
3171 if(i >= st_template.dev_max)panic("scsi_devices corrupt (st)");
3173 scsi_tapes[i].device = SDp;
3174 if(SDp->scsi_level <=2)
3175 scsi_tapes[i].mt_status->mt_type = MT_ISSCSI1;
3176 else
3177 scsi_tapes[i].mt_status->mt_type = MT_ISSCSI2;
3179 tpnt->devt =MKDEV(SCSI_TAPE_MAJOR, i);
3180 tpnt->dirty =0;
3181 tpnt->waiting = NULL;
3182 tpnt->in_use =0;
3183 tpnt->drv_buffer =1;/* Try buffering if no mode sense */
3184 tpnt->restr_dma = (SDp->host)->unchecked_isa_dma;
3185 tpnt->density =0;
3186 tpnt->do_auto_lock = ST_AUTO_LOCK;
3187 tpnt->can_bsr = ST_IN_FILE_POS;
3188 tpnt->can_partitions =0;
3189 tpnt->two_fm = ST_TWO_FM;
3190 tpnt->fast_mteom = ST_FAST_MTEOM;
3191 tpnt->scsi2_logical = ST_SCSI2LOGICAL;
3192 tpnt->write_threshold = st_write_threshold;
3193 tpnt->default_drvbuffer =0xff;/* No forced buffering */
3194 tpnt->partition =0;
3195 tpnt->new_partition =0;
3196 tpnt->nbr_partitions =0;
3197 tpnt->timeout = ST_TIMEOUT;
3198 tpnt->long_timeout = ST_LONG_TIMEOUT;
3200 for(i=0; i < ST_NBR_MODES; i++) {
3201 STm = &(tpnt->modes[i]);
3202 STm->defined = FALSE;
3203 STm->sysv = ST_SYSV;
3204 STm->defaults_for_writes =0;
3205 STm->do_async_writes = ST_ASYNC_WRITES;
3206 STm->do_buffer_writes = ST_BUFFER_WRITES;
3207 STm->do_read_ahead = ST_READ_AHEAD;
3208 STm->default_compression = ST_DONT_TOUCH;
3209 STm->default_blksize = (-1);/* No forced size */
3210 STm->default_density = (-1);/* No forced density */
3213 for(i=0; i < ST_NBR_PARTITIONS; i++) {
3214 STps = &(tpnt->ps[i]);
3215 STps->rw = ST_IDLE;
3216 STps->eof = ST_NOEOF;
3217 STps->at_sm =0;
3218 STps->last_block_valid = FALSE;
3219 STps->drv_block =0;
3220 STps->drv_file =0;
3223 tpnt->current_mode =0;
3224 tpnt->modes[0].defined = TRUE;
3226 tpnt->density_changed = tpnt->compression_changed =
3227 tpnt->blksize_changed = FALSE;
3229 st_template.nr_dev++;
3230 return0;
3233 static intst_detect(Scsi_Device * SDp)
3235 if(SDp->type != TYPE_TAPE)return0;
3237 printk(KERN_INFO
3238 "Detected scsi tape st%d at scsi%d, channel %d, id %d, lun %d\n",
3239 st_template.dev_noticed++,
3240 SDp->host->host_no, SDp->channel, SDp->id, SDp->lun);
3242 return1;
3245 static int st_registered =0;
3247 /* Driver initialization (not __initfunc because may be called later) */
3248 static intst_init()
3250 int i;
3251 Scsi_Tape * STp;
3252 #if !ST_RUNTIME_BUFFERS
3253 int target_nbr;
3254 #endif
3256 if(st_template.dev_noticed ==0)return0;
3258 if(!st_registered) {
3259 if(register_chrdev(SCSI_TAPE_MAJOR,"st",&st_fops)) {
3260 printk(KERN_ERR "Unable to get major %d for SCSI tapes\n",MAJOR_NR);
3261 return1;
3263 st_registered++;
3266 if(scsi_tapes)return0;
3267 st_template.dev_max = st_template.dev_noticed + ST_EXTRA_DEVS;
3268 if(st_template.dev_max < ST_MAX_TAPES)
3269 st_template.dev_max = ST_MAX_TAPES;
3270 if(st_template.dev_max >128/ ST_NBR_MODES)
3271 printk(KERN_INFO "st: Only %d tapes accessible.\n",128/ ST_NBR_MODES);
3272 scsi_tapes =
3273 (Scsi_Tape *)scsi_init_malloc(st_template.dev_max *sizeof(Scsi_Tape),
3274 GFP_ATOMIC);
3275 if(scsi_tapes == NULL) {
3276 printk(KERN_ERR "Unable to allocate descriptors for SCSI tapes.\n");
3277 unregister_chrdev(SCSI_TAPE_MAJOR,"st");
3278 return1;
3281 #if DEBUG
3282 printk(ST_DEB_MSG "st: Buffer size %d bytes, write threshold %d bytes.\n",
3283 st_buffer_size, st_write_threshold);
3284 #endif
3286 memset(scsi_tapes,0, st_template.dev_max *sizeof(Scsi_Tape));
3287 for(i=0; i < st_template.dev_max; ++i) {
3288 STp = &(scsi_tapes[i]);
3289 STp->capacity =0xfffff;
3290 STp->mt_status = (struct mtget *)scsi_init_malloc(sizeof(struct mtget),
3291 GFP_ATOMIC);
3292 /* Initialize status */
3293 memset((void*) scsi_tapes[i].mt_status,0,sizeof(struct mtget));
3296 /* Allocate the buffers */
3297 st_buffers =
3298 (ST_buffer **)scsi_init_malloc(st_template.dev_max *sizeof(ST_buffer *),
3299 GFP_ATOMIC);
3300 if(st_buffers == NULL) {
3301 printk(KERN_ERR "Unable to allocate tape buffer pointers.\n");
3302 unregister_chrdev(SCSI_TAPE_MAJOR,"st");
3303 scsi_init_free((char*) scsi_tapes,
3304 st_template.dev_max *sizeof(Scsi_Tape));
3305 return1;
3308 #if ST_RUNTIME_BUFFERS
3309 st_nbr_buffers =0;
3310 #else
3311 target_nbr = st_template.dev_noticed;
3312 if(target_nbr < ST_EXTRA_DEVS)
3313 target_nbr = ST_EXTRA_DEVS;
3314 if(target_nbr > st_max_buffers)
3315 target_nbr = st_max_buffers;
3317 for(i=st_nbr_buffers=0; i < target_nbr; i++) {
3318 if(!new_tape_buffer(TRUE, TRUE)) {
3319 if(i ==0) {
3320 #if 0
3321 printk(KERN_ERR "Can't continue without at least one tape buffer.\n");
3322 unregister_chrdev(SCSI_TAPE_MAJOR,"st");
3323 scsi_init_free((char*) st_buffers,
3324 st_template.dev_max *sizeof(ST_buffer *));
3325 scsi_init_free((char*) scsi_tapes,
3326 st_template.dev_max *sizeof(Scsi_Tape));
3327 return1;
3328 #else
3329 printk(KERN_INFO "No tape buffers allocated at initialization.\n");
3330 break;
3331 #endif
3333 printk(KERN_INFO "Number of tape buffers adjusted.\n");
3334 break;
3337 #endif
3338 return0;
3341 static voidst_detach(Scsi_Device * SDp)
3343 Scsi_Tape * tpnt;
3344 int i;
3346 for(tpnt = scsi_tapes, i=0; i<st_template.dev_max; i++, tpnt++)
3347 if(tpnt->device == SDp) {
3348 tpnt->device = NULL;
3349 SDp->attached--;
3350 st_template.nr_dev--;
3351 st_template.dev_noticed--;
3352 return;
3354 return;
3358 #ifdef MODULE
3360 intinit_module(void) {
3361 int result;
3363 st_template.module = &__this_module;
3364 result =scsi_register_module(MODULE_SCSI_DEV, &st_template);
3365 if(result)
3366 return result;
3368 if(buffer_kbs >0)
3369 st_buffer_size = buffer_kbs * ST_KILOBYTE;
3370 if(write_threshold_kbs >0)
3371 st_write_threshold = write_threshold_kbs * ST_KILOBYTE;
3372 if(st_write_threshold > st_buffer_size)
3373 st_write_threshold = st_buffer_size;
3374 if(max_buffers >0)
3375 st_max_buffers = max_buffers;
3376 printk(KERN_INFO "st: bufsize %d, wrt %d, max buffers %d.\n",
3377 st_buffer_size, st_write_threshold, st_max_buffers);
3379 return0;
3382 voidcleanup_module(void)
3384 int i;
3386 scsi_unregister_module(MODULE_SCSI_DEV, &st_template);
3387 unregister_chrdev(SCSI_TAPE_MAJOR,"st");
3388 st_registered--;
3389 if(scsi_tapes != NULL) {
3390 scsi_init_free((char*) scsi_tapes,
3391 st_template.dev_max *sizeof(Scsi_Tape));
3393 if(st_buffers != NULL) {
3394 for(i=0; i < st_nbr_buffers; i++)
3395 if(st_buffers[i] != NULL) {
3396 scsi_init_free((char*) st_buffers[i]->b_data,
3397 st_buffers[i]->buffer_size);
3398 scsi_init_free((char*) st_buffers[i],sizeof(ST_buffer));
3401 scsi_init_free((char*) st_buffers,
3402 st_template.dev_max *sizeof(ST_buffer *));
3405 st_template.dev_max =0;
3406 printk(KERN_INFO "st: Unloaded.\n");
3408 #endif/* MODULE */
close