Import 2.1.31
[davej-history.git] / drivers / scsi / st.c
blob339ba6714502c772e89840c9a5cc260c3da91299
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 Jan 1 15:26:54 1997 by makisara@kai.makisara.fi
15 Some small formal changes - aeb, 950809
18 #include <linux/module.h>
20 #include <linux/fs.h>
21 #include <linux/kernel.h>
22 #include <linux/sched.h>
23 #include <linux/mm.h>
24 #include <linux/string.h>
25 #include <linux/errno.h>
26 #include <linux/mtio.h>
27 #include <linux/ioctl.h>
28 #include <linux/fcntl.h>
29 #include <asm/uaccess.h>
30 #include <asm/dma.h>
31 #include <asm/system.h>
33 /* The driver prints some debugging information on the console if DEBUG
34 is defined and non-zero. */
35 #define DEBUG 0
37 /* The message level for the debug messages is currently set to KERN_NOTICE
38 so that people can easily see the messages. Later when the debugging messages
39 in the drivers are more widely classified, this may be changed to KERN_DEBUG. */
40 #define ST_DEB_MSG KERN_NOTICE
42 #define MAJOR_NR SCSI_TAPE_MAJOR
43 #include <linux/blk.h>
45 #include"scsi.h"
46 #include"hosts.h"
47 #include <scsi/scsi_ioctl.h>
48 #include"st.h"
49 #include"constants.h"
51 /* The default definitions have been moved to st_options.h */
53 #define ST_BLOCK_SIZE 1024
55 #include"st_options.h"
57 #define ST_BUFFER_SIZE (ST_BUFFER_BLOCKS * ST_BLOCK_SIZE)
58 #define ST_WRITE_THRESHOLD (ST_WRITE_THRESHOLD_BLOCKS * ST_BLOCK_SIZE)
60 /* The buffer size should fit into the 24 bits for length in the
61 6-byte SCSI read and write commands. */
62 #if ST_BUFFER_SIZE >= (2 << 24 - 1)
63 #error"Buffer size should not exceed (2 << 24 - 1) bytes!"
64 #endif
66 #if DEBUG
67 static int debugging =1;
68 #endif
70 #define MAX_RETRIES 0
71 #define MAX_WRITE_RETRIES 0
72 #define MAX_READY_RETRIES 5
73 #define NO_TAPE NOT_READY
75 #define ST_TIMEOUT (900 * HZ)
76 #define ST_LONG_TIMEOUT (14000 * HZ)
78 #define TAPE_NR(x) (MINOR(x) & ~(128 | ST_MODE_MASK))
79 #define TAPE_MODE(x) ((MINOR(x) & ST_MODE_MASK) >> ST_MODE_SHIFT)
81 /* Internal ioctl to set both density (uppermost 8 bits) and blocksize (lower
82 24 bits) */
83 #define SET_DENS_AND_BLK 0x10001
85 static int st_nbr_buffers;
86 static ST_buffer **st_buffers;
87 static int st_buffer_size = ST_BUFFER_SIZE;
88 static int st_write_threshold = ST_WRITE_THRESHOLD;
89 static int st_max_buffers = ST_MAX_BUFFERS;
91 static Scsi_Tape * scsi_tapes = NULL;
93 static int modes_defined = FALSE;
95 static ST_buffer *new_tape_buffer(int,int);
96 static intenlarge_buffer(ST_buffer *,int,int);
97 static voidnormalize_buffer(ST_buffer *);
99 static intst_init(void);
100 static intst_attach(Scsi_Device *);
101 static intst_detect(Scsi_Device *);
102 static voidst_detach(Scsi_Device *);
104 struct Scsi_Device_Template st_template = {NULL,"tape","st", NULL, TYPE_TAPE,
105 SCSI_TAPE_MAJOR,0,0,0,0,
106 st_detect, st_init,
107 NULL, st_attach, st_detach};
109 static intst_compression(Scsi_Tape *,int);
111 static intfind_partition(struct inode *);
112 static intupdate_partition(struct inode *);
114 static intst_int_ioctl(struct inode * inode,unsigned int cmd_in,
115 unsigned long arg);
120 /* Convert the result to success code */
121 static int
122 st_chk_result(Scsi_Cmnd * SCpnt)
124 int dev =TAPE_NR(SCpnt->request.rq_dev);
125 int result = SCpnt->result;
126 unsigned char* sense = SCpnt->sense_buffer, scode;
127 #if DEBUG
128 const char*stp;
129 #endif
131 if(!result /* && SCpnt->sense_buffer[0] == 0 */)
132 return0;
134 scode = sense[2] &0x0f;
136 #if DEBUG
137 if(debugging) {
138 printk(ST_DEB_MSG "st%d: Error: %x, cmd: %x %x %x %x %x %x Len: %d\n",
139 dev, result,
140 SCpnt->data_cmnd[0], SCpnt->data_cmnd[1], SCpnt->data_cmnd[2],
141 SCpnt->data_cmnd[3], SCpnt->data_cmnd[4], SCpnt->data_cmnd[5],
142 SCpnt->request_bufflen);
143 if(driver_byte(result) & DRIVER_SENSE)
144 print_sense("st", SCpnt);
145 else
146 printk("\n");
148 else
149 #endif
150 if(!(driver_byte(result) & DRIVER_SENSE) ||
151 ((sense[0] &0x70) ==0x70&&
152 scode != NO_SENSE &&
153 scode != RECOVERED_ERROR &&
154 /* scode != UNIT_ATTENTION && */
155 scode != BLANK_CHECK &&
156 scode != VOLUME_OVERFLOW &&
157 SCpnt->data_cmnd[0] != MODE_SENSE &&
158 SCpnt->data_cmnd[0] != TEST_UNIT_READY)) {/* Abnormal conditions for tape */
159 if(driver_byte(result) & DRIVER_SENSE) {
160 printk(KERN_WARNING "st%d: Error with sense data: ", dev);
161 print_sense("st", SCpnt);
163 else
164 printk(KERN_WARNING "st%d: Error %x.\n", dev, result);
167 if((sense[0] &0x70) ==0x70&&
168 scode == RECOVERED_ERROR
169 #if ST_RECOVERED_WRITE_FATAL
170 && SCpnt->data_cmnd[0] != WRITE_6
171 && SCpnt->data_cmnd[0] != WRITE_FILEMARKS
172 #endif
174 scsi_tapes[dev].recover_count++;
175 scsi_tapes[dev].mt_status->mt_erreg += (1<< MT_ST_SOFTERR_SHIFT);
176 #if DEBUG
177 if(debugging) {
178 if(SCpnt->data_cmnd[0] == READ_6)
179 stp ="read";
180 else if(SCpnt->data_cmnd[0] == WRITE_6)
181 stp ="write";
182 else
183 stp ="ioctl";
184 printk(ST_DEB_MSG "st%d: Recovered %s error (%d).\n", dev, stp,
185 scsi_tapes[dev].recover_count);
187 #endif
188 if((sense[2] &0xe0) ==0)
189 return0;
191 return(-EIO);
195 /* Wakeup from interrupt */
196 static void
197 st_sleep_done(Scsi_Cmnd * SCpnt)
199 unsigned int st_nbr;
200 int remainder;
201 Scsi_Tape * STp;
203 if((st_nbr =TAPE_NR(SCpnt->request.rq_dev)) < st_template.nr_dev) {
204 STp = &(scsi_tapes[st_nbr]);
205 if((STp->buffer)->writing &&
206 (SCpnt->sense_buffer[0] &0x70) ==0x70&&
207 (SCpnt->sense_buffer[2] &0x40)) {
208 /* EOM at write-behind, has all been written? */
209 if((SCpnt->sense_buffer[0] &0x80) !=0)
210 remainder = (SCpnt->sense_buffer[3] <<24) |
211 (SCpnt->sense_buffer[4] <<16) |
212 (SCpnt->sense_buffer[5] <<8) | SCpnt->sense_buffer[6];
213 else
214 remainder =0;
215 if((SCpnt->sense_buffer[2] &0x0f) == VOLUME_OVERFLOW ||
216 remainder >0)
217 (STp->buffer)->last_result = SCpnt->result;/* Error */
218 else
219 (STp->buffer)->last_result = INT_MAX;/* OK */
221 else
222 (STp->buffer)->last_result = SCpnt->result;
223 if((STp->buffer)->writing) {
224 /* Process errors before releasing request */
225 (STp->buffer)->last_result_fatal =st_chk_result(SCpnt);
226 SCpnt->request.rq_status = RQ_INACTIVE;
228 else
229 SCpnt->request.rq_status = RQ_SCSI_DONE;
231 #if DEBUG
232 STp->write_pending =0;
233 #endif
234 up(SCpnt->request.sem);
236 #if DEBUG
237 else if(debugging)
238 printk(KERN_ERR "st?: Illegal interrupt device %x\n", st_nbr);
239 #endif
243 /* Do the scsi command */
244 static Scsi_Cmnd *
245 st_do_scsi(Scsi_Cmnd *SCpnt, Scsi_Tape *STp,unsigned char*cmd,int bytes,
246 int timeout,int retries)
248 if(SCpnt == NULL)
249 if((SCpnt =allocate_device(NULL, STp->device,1)) == NULL) {
250 printk(KERN_ERR "st%d: Can't get SCSI request.\n",TAPE_NR(STp->devt));
251 return NULL;
254 cmd[1] |= (SCpnt->lun <<5) &0xe0;
255 STp->sem = MUTEX_LOCKED;
256 SCpnt->request.sem = &(STp->sem);
257 SCpnt->request.rq_status = RQ_SCSI_BUSY;
258 SCpnt->request.rq_dev = STp->devt;
260 scsi_do_cmd(SCpnt, (void*)cmd, (STp->buffer)->b_data, bytes,
261 st_sleep_done, timeout, retries);
263 down(SCpnt->request.sem);
265 (STp->buffer)->last_result_fatal =st_chk_result(SCpnt);
267 return SCpnt;
271 /* Handle the write-behind checking */
272 static void
273 write_behind_check(Scsi_Tape *STp)
275 ST_buffer * STbuffer;
276 ST_partstat * STps;
278 STbuffer = STp->buffer;
280 #if DEBUG
281 if(STp->write_pending)
282 STp->nbr_waits++;
283 else
284 STp->nbr_finished++;
285 #endif
287 down(&(STp->sem));
289 if(STbuffer->writing < STbuffer->buffer_bytes)
290 memcpy(STbuffer->b_data,
291 STbuffer->b_data + STbuffer->writing,
292 STbuffer->buffer_bytes - STbuffer->writing);
293 STbuffer->buffer_bytes -= STbuffer->writing;
294 STps = &(STp->ps[STp->partition]);
295 if(STps->drv_block >=0) {
296 if(STp->block_size ==0)
297 STps->drv_block++;
298 else
299 STps->drv_block += STbuffer->writing / STp->block_size;
301 STbuffer->writing =0;
303 return;
307 /* Step over EOF if it has been inadvertently crossed (ioctl not used because
308 it messes up the block number). */
309 static int
310 cross_eof(Scsi_Tape *STp,int forward)
312 Scsi_Cmnd *SCpnt;
313 unsigned char cmd[10];
315 cmd[0] = SPACE;
316 cmd[1] =0x01;/* Space FileMarks */
317 if(forward) {
318 cmd[2] = cmd[3] =0;
319 cmd[4] =1;
321 else
322 cmd[2] = cmd[3] = cmd[4] =0xff;/* -1 filemarks */
323 cmd[5] =0;
324 #if DEBUG
325 if(debugging)
326 printk(ST_DEB_MSG "st%d: Stepping over filemark %s.\n",
327 TAPE_NR(STp->devt), forward ?"forward":"backward");
328 #endif
330 SCpnt =st_do_scsi(NULL, STp, cmd,0, ST_TIMEOUT, MAX_RETRIES);
331 if(!SCpnt)
332 return(-EBUSY);
334 SCpnt->request.rq_status = RQ_INACTIVE;
335 if((STp->buffer)->last_result !=0)
336 printk(KERN_ERR "st%d: Stepping over filemark %s failed.\n",
337 TAPE_NR(STp->devt), forward ?"forward":"backward");
339 return(STp->buffer)->last_result_fatal;
343 /* Flush the write buffer (never need to write if variable blocksize). */
344 static int
345 flush_write_buffer(Scsi_Tape *STp)
347 int offset, transfer, blks;
348 int result;
349 unsigned char cmd[10];
350 Scsi_Cmnd *SCpnt;
351 ST_partstat * STps;
353 if((STp->buffer)->writing) {
354 write_behind_check(STp);
355 if((STp->buffer)->last_result_fatal) {
356 #if DEBUG
357 if(debugging)
358 printk(ST_DEB_MSG "st%d: Async write error (flush) %x.\n",
359 TAPE_NR(STp->devt), (STp->buffer)->last_result);
360 #endif
361 if((STp->buffer)->last_result == INT_MAX)
362 return(-ENOSPC);
363 return(-EIO);
367 if(STp->block_size ==0)
368 return0;
370 result =0;
371 if(STp->dirty ==1) {
373 offset = (STp->buffer)->buffer_bytes;
374 transfer = ((offset + STp->block_size -1) /
375 STp->block_size) * STp->block_size;
376 #if DEBUG
377 if(debugging)
378 printk(ST_DEB_MSG "st%d: Flushing %d bytes.\n",TAPE_NR(STp->devt), transfer);
379 #endif
380 memset((STp->buffer)->b_data + offset,0, transfer - offset);
382 memset(cmd,0,10);
383 cmd[0] = WRITE_6;
384 cmd[1] =1;
385 blks = transfer / STp->block_size;
386 cmd[2] = blks >>16;
387 cmd[3] = blks >>8;
388 cmd[4] = blks;
390 SCpnt =st_do_scsi(NULL, STp, cmd, transfer, ST_TIMEOUT, MAX_WRITE_RETRIES);
391 if(!SCpnt)
392 return(-EBUSY);
394 STps = &(STp->ps[STp->partition]);
395 if((STp->buffer)->last_result_fatal !=0) {
396 if((SCpnt->sense_buffer[0] &0x70) ==0x70&&
397 (SCpnt->sense_buffer[2] &0x40) &&
398 (SCpnt->sense_buffer[2] &0x0f) == NO_SENSE) {
399 STp->dirty =0;
400 (STp->buffer)->buffer_bytes =0;
401 result = (-ENOSPC);
403 else{
404 printk(KERN_ERR "st%d: Error on flush.\n",TAPE_NR(STp->devt));
405 result = (-EIO);
407 STps->drv_block = (-1);
409 else{
410 if(STps->drv_block >=0)
411 STps->drv_block += blks;
412 STp->dirty =0;
413 (STp->buffer)->buffer_bytes =0;
415 SCpnt->request.rq_status = RQ_INACTIVE;/* Mark as not busy */
417 return result;
421 /* Flush the tape buffer. The tape will be positioned correctly unless
422 seek_next is true. */
423 static int
424 flush_buffer(struct inode * inode,struct file * filp,int seek_next)
426 int backspace, result;
427 Scsi_Tape * STp;
428 ST_buffer * STbuffer;
429 ST_partstat * STps;
430 int dev =TAPE_NR(inode->i_rdev);
432 STp = &(scsi_tapes[dev]);
433 STbuffer = STp->buffer;
436 * If there was a bus reset, block further access
437 * to this device.
439 if( STp->device->was_reset )
440 return(-EIO);
442 if(STp->ready != ST_READY)
443 return0;
445 STps = &(STp->ps[STp->partition]);
446 if(STps->rw == ST_WRITING)/* Writing */
447 returnflush_write_buffer(STp);
449 if(STp->block_size ==0)
450 return0;
452 backspace = ((STp->buffer)->buffer_bytes +
453 (STp->buffer)->read_pointer) / STp->block_size -
454 ((STp->buffer)->read_pointer + STp->block_size -1) /
455 STp->block_size;
456 (STp->buffer)->buffer_bytes =0;
457 (STp->buffer)->read_pointer =0;
458 result =0;
459 if(!seek_next) {
460 if(STps->eof == ST_FM_HIT) {
461 result =cross_eof(STp, FALSE);/* Back over the EOF hit */
462 if(!result)
463 STps->eof = ST_NOEOF;
464 else{
465 if(STps->drv_file >=0)
466 STps->drv_file++;
467 STps->drv_block =0;
470 if(!result && backspace >0)
471 result =st_int_ioctl(inode, MTBSR, backspace);
473 else if(STps->eof == ST_FM_HIT) {
474 if(STps->drv_file >=0)
475 STps->drv_file++;
476 STps->drv_block =0;
477 STps->eof = ST_NOEOF;
480 return result;
484 \f/* Set the mode parameters */
485 static int
486 set_mode_densblk(struct inode * inode, Scsi_Tape *STp, ST_mode *STm)
488 int set_it = FALSE;
489 unsigned long arg;
490 int dev =TAPE_NR(inode->i_rdev);
492 if(!STp->density_changed &&
493 STm->default_density >=0&&
494 STm->default_density != STp->density) {
495 arg = STm->default_density;
496 set_it = TRUE;
498 else
499 arg = STp->density;
500 arg <<= MT_ST_DENSITY_SHIFT;
501 if(!STp->blksize_changed &&
502 STm->default_blksize >=0&&
503 STm->default_blksize != STp->block_size) {
504 arg |= STm->default_blksize;
505 set_it = TRUE;
507 else
508 arg |= STp->block_size;
509 if(set_it &&
510 st_int_ioctl(inode, SET_DENS_AND_BLK, arg)) {
511 printk(KERN_WARNING
512 "st%d: Can't set default block size to %d bytes and density %x.\n",
513 dev, STm->default_blksize, STm->default_density);
514 if(modes_defined)
515 return(-EINVAL);
517 return0;
521 /* Open the device */
522 static int
523 scsi_tape_open(struct inode * inode,struct file * filp)
525 unsigned short flags;
526 int i, need_dma_buffer, new_session = FALSE;
527 unsigned char cmd[10];
528 Scsi_Cmnd * SCpnt;
529 Scsi_Tape * STp;
530 ST_mode * STm;
531 ST_partstat * STps;
532 int dev =TAPE_NR(inode->i_rdev);
533 int mode =TAPE_MODE(inode->i_rdev);
535 if(dev >= st_template.dev_max || !scsi_tapes[dev].device)
536 return(-ENXIO);
537 STp = &(scsi_tapes[dev]);
538 if(STp->in_use) {
539 #if DEBUG
540 printk(ST_DEB_MSG "st%d: Device already in use.\n", dev);
541 #endif
542 return(-EBUSY);
544 STp->rew_at_close = (MINOR(inode->i_rdev) &0x80) ==0;
546 if(mode != STp->current_mode) {
547 #if DEBUG
548 if(debugging)
549 printk(ST_DEB_MSG "st%d: Mode change from %d to %d.\n",
550 dev, STp->current_mode, mode);
551 #endif
552 new_session = TRUE;
553 STp->current_mode = mode;
555 STm = &(STp->modes[STp->current_mode]);
557 /* Allocate buffer for this user */
558 need_dma_buffer = STp->restr_dma;
559 for(i=0; i < st_nbr_buffers; i++)
560 if(!st_buffers[i]->in_use &&
561 (!need_dma_buffer || st_buffers[i]->dma))
562 break;
563 if(i >= st_nbr_buffers) {
564 STp->buffer =new_tape_buffer(FALSE, need_dma_buffer);
565 if(STp->buffer == NULL) {
566 printk(KERN_WARNING "st%d: Can't allocate tape buffer.\n", dev);
567 return(-EBUSY);
570 else
571 STp->buffer = st_buffers[i];
572 (STp->buffer)->in_use =1;
573 (STp->buffer)->writing =0;
575 flags = filp->f_flags;
576 STp->write_prot = ((flags & O_ACCMODE) == O_RDONLY);
578 STp->dirty =0;
579 for(i=0; i < ST_NBR_PARTITIONS; i++) {
580 STps = &(STp->ps[i]);
581 STps->rw = ST_IDLE;
583 STp->ready = ST_READY;
584 STp->recover_count =0;
585 #if DEBUG
586 STp->nbr_waits = STp->nbr_finished =0;
587 #endif
589 if(scsi_tapes[dev].device->host->hostt->module)
590 __MOD_INC_USE_COUNT(scsi_tapes[dev].device->host->hostt->module);
591 if(st_template.module)
592 __MOD_INC_USE_COUNT(st_template.module);
594 memset((void*) &cmd[0],0,10);
595 cmd[0] = TEST_UNIT_READY;
597 SCpnt =st_do_scsi(NULL, STp, cmd,0, ST_LONG_TIMEOUT, MAX_READY_RETRIES);
598 if(!SCpnt) {
599 if(scsi_tapes[dev].device->host->hostt->module)
600 __MOD_DEC_USE_COUNT(scsi_tapes[dev].device->host->hostt->module);
601 if(st_template.module)
602 __MOD_DEC_USE_COUNT(st_template.module);
603 return(-EBUSY);
606 if((SCpnt->sense_buffer[0] &0x70) ==0x70&&
607 (SCpnt->sense_buffer[2] &0x0f) == UNIT_ATTENTION) {/* New media? */
608 memset((void*) &cmd[0],0,10);
609 cmd[0] = TEST_UNIT_READY;
611 SCpnt =st_do_scsi(SCpnt, STp, cmd,0, ST_LONG_TIMEOUT, MAX_READY_RETRIES);
613 (STp->device)->was_reset =0;
614 STp->partition = STp->new_partition =0;
615 if(STp->can_partitions)
616 STp->nbr_partitions =1;/* This guess will be updated later if necessary */
617 for(i=0; i < ST_NBR_PARTITIONS; i++) {
618 STps = &(STp->ps[i]);
619 STps->rw = ST_IDLE;
620 STps->eof = ST_NOEOF;
621 STps->at_sm =0;
622 STps->last_block_valid = FALSE;
623 STps->drv_block =0;
624 STps->drv_file =0;
626 new_session = TRUE;
629 if((STp->buffer)->last_result_fatal !=0) {
630 if((SCpnt->sense_buffer[0] &0x70) ==0x70&&
631 (SCpnt->sense_buffer[2] &0x0f) == NO_TAPE) {
632 STp->ready = ST_NO_TAPE;
633 }else
634 STp->ready = ST_NOT_READY;
635 SCpnt->request.rq_status = RQ_INACTIVE;/* Mark as not busy */
636 STp->density =0;/* Clear the erroneous "residue" */
637 STp->write_prot =0;
638 STp->block_size =0;
639 STp->ps[0].drv_file = STp->ps[0].drv_block =0;
640 STp->partition = STp->new_partition =0;
641 STp->door_locked = ST_UNLOCKED;
642 STp->in_use =1;
643 return0;
646 if(STp->omit_blklims)
647 STp->min_block = STp->max_block = (-1);
648 else{
649 memset((void*) &cmd[0],0,10);
650 cmd[0] = READ_BLOCK_LIMITS;
652 SCpnt =st_do_scsi(SCpnt, STp, cmd,6, ST_TIMEOUT, MAX_READY_RETRIES);
654 if(!SCpnt->result && !SCpnt->sense_buffer[0]) {
655 STp->max_block = ((STp->buffer)->b_data[1] <<16) |
656 ((STp->buffer)->b_data[2] <<8) | (STp->buffer)->b_data[3];
657 STp->min_block = ((STp->buffer)->b_data[4] <<8) |
658 (STp->buffer)->b_data[5];
659 #if DEBUG
660 if(debugging)
661 printk(ST_DEB_MSG "st%d: Block limits %d - %d bytes.\n", dev, STp->min_block,
662 STp->max_block);
663 #endif
665 else{
666 STp->min_block = STp->max_block = (-1);
667 #if DEBUG
668 if(debugging)
669 printk(ST_DEB_MSG "st%d: Can't read block limits.\n", dev);
670 #endif
674 memset((void*) &cmd[0],0,10);
675 cmd[0] = MODE_SENSE;
676 cmd[4] =12;
678 SCpnt =st_do_scsi(SCpnt, STp, cmd,12, ST_TIMEOUT, MAX_READY_RETRIES);
680 if((STp->buffer)->last_result_fatal !=0) {
681 #if DEBUG
682 if(debugging)
683 printk(ST_DEB_MSG "st%d: No Mode Sense.\n", dev);
684 #endif
685 STp->block_size = ST_DEFAULT_BLOCK;/* Educated guess (?) */
686 (STp->buffer)->last_result_fatal =0;/* Prevent error propagation */
687 STp->drv_write_prot =0;
689 else{
691 #if DEBUG
692 if(debugging)
693 printk(ST_DEB_MSG "st%d: Mode sense. Length %d, medium %x, WBS %x, BLL %d\n",
694 dev,
695 (STp->buffer)->b_data[0], (STp->buffer)->b_data[1],
696 (STp->buffer)->b_data[2], (STp->buffer)->b_data[3]);
697 #endif
699 if((STp->buffer)->b_data[3] >=8) {
700 STp->drv_buffer = ((STp->buffer)->b_data[2] >>4) &7;
701 STp->density = (STp->buffer)->b_data[4];
702 STp->block_size = (STp->buffer)->b_data[9] *65536+
703 (STp->buffer)->b_data[10] *256+ (STp->buffer)->b_data[11];
704 #if DEBUG
705 if(debugging)
706 printk(ST_DEB_MSG "st%d: Density %x, tape length: %x, drv buffer: %d\n",
707 dev, STp->density, (STp->buffer)->b_data[5] *65536+
708 (STp->buffer)->b_data[6] *256+ (STp->buffer)->b_data[7],
709 STp->drv_buffer);
710 #endif
713 if(STp->block_size > (STp->buffer)->buffer_size &&
714 !enlarge_buffer(STp->buffer, STp->block_size, STp->restr_dma)) {
715 printk(KERN_NOTICE "st%d: Blocksize %d too large for buffer.\n", dev,
716 STp->block_size);
717 (STp->buffer)->in_use =0;
718 STp->buffer = NULL;
719 if(scsi_tapes[dev].device->host->hostt->module)
720 __MOD_DEC_USE_COUNT(scsi_tapes[dev].device->host->hostt->module);
721 if(st_template.module)
722 __MOD_DEC_USE_COUNT(st_template.module);
723 return(-EIO);
725 STp->drv_write_prot = ((STp->buffer)->b_data[2] &0x80) !=0;
727 SCpnt->request.rq_status = RQ_INACTIVE;/* Mark as not busy */
729 if(STp->block_size >0)
730 (STp->buffer)->buffer_blocks = st_buffer_size / STp->block_size;
731 else
732 (STp->buffer)->buffer_blocks =1;
733 (STp->buffer)->buffer_bytes = (STp->buffer)->read_pointer =0;
735 #if DEBUG
736 if(debugging)
737 printk(ST_DEB_MSG "st%d: Block size: %d, buffer size: %d (%d blocks).\n", dev,
738 STp->block_size, (STp->buffer)->buffer_size,
739 (STp->buffer)->buffer_blocks);
740 #endif
742 if(STp->drv_write_prot) {
743 STp->write_prot =1;
744 #if DEBUG
745 if(debugging)
746 printk(ST_DEB_MSG "st%d: Write protected\n", dev);
747 #endif
748 if((flags & O_ACCMODE) == O_WRONLY || (flags & O_ACCMODE) == O_RDWR) {
749 (STp->buffer)->in_use =0;
750 STp->buffer = NULL;
751 if(scsi_tapes[dev].device->host->hostt->module)
752 __MOD_DEC_USE_COUNT(scsi_tapes[dev].device->host->hostt->module);
753 if(st_template.module)
754 __MOD_DEC_USE_COUNT(st_template.module);
755 return(-EROFS);
759 if(STp->can_partitions && STp->nbr_partitions <1) {
760 /* This code is reached when the device is opened for the first time
761 after the driver has been initialized with tape in the drive and the
762 partition support has been enabled. */
763 #if DEBUG
764 if(debugging)
765 printk(ST_DEB_MSG "st%d: Updating partition number in status.\n", dev);
766 #endif
767 if((STp->partition =find_partition(inode)) <0) {
768 (STp->buffer)->in_use =0;
769 STp->buffer = NULL;
770 if(scsi_tapes[dev].device->host->hostt->module)
771 __MOD_DEC_USE_COUNT(scsi_tapes[dev].device->host->hostt->module);
772 if(st_template.module)
773 __MOD_DEC_USE_COUNT(st_template.module);
774 return STp->partition;
776 STp->new_partition = STp->partition;
777 STp->nbr_partitions =1;/* This guess will be updated when necessary */
780 if(new_session) {/* Change the drive parameters for the new mode */
781 STp->density_changed = STp->blksize_changed = FALSE;
782 STp->compression_changed = FALSE;
783 if(!(STm->defaults_for_writes) &&
784 (i =set_mode_densblk(inode, STp, STm)) <0) {
785 (STp->buffer)->in_use =0;
786 STp->buffer = NULL;
787 if(scsi_tapes[dev].device->host->hostt->module)
788 __MOD_DEC_USE_COUNT(scsi_tapes[dev].device->host->hostt->module);
789 if(st_template.module)
790 __MOD_DEC_USE_COUNT(st_template.module);
791 return i;
793 if(STp->default_drvbuffer !=0xff) {
794 if(st_int_ioctl(inode, MTSETDRVBUFFER, STp->default_drvbuffer))
795 printk(KERN_WARNING "st%d: Can't set default drive buffering to %d.\n",
796 dev, STp->default_drvbuffer);
800 STp->in_use =1;
802 return0;
806 /* Close the device*/
807 static int
808 scsi_tape_close(struct inode * inode,struct file * filp)
810 int result;
811 static unsigned char cmd[10];
812 Scsi_Cmnd * SCpnt;
813 Scsi_Tape * STp;
814 ST_mode * STm;
815 ST_partstat * STps;
817 kdev_t devt = inode->i_rdev;
818 int dev;
820 dev =TAPE_NR(devt);
821 STp = &(scsi_tapes[dev]);
822 STm = &(STp->modes[STp->current_mode]);
823 STps = &(STp->ps[STp->partition]);
825 if(STp->can_partitions &&
826 update_partition(inode) <0) {
827 #if DEBUG
828 if(debugging)
829 printk(ST_DEB_MSG "st%d: update_partition at close failed.\n", dev);
830 #endif
831 goto out;
834 if( STps->rw == ST_WRITING && !(STp->device)->was_reset) {
836 result =flush_write_buffer(STp);
838 #if DEBUG
839 if(debugging) {
840 printk(ST_DEB_MSG "st%d: File length %ld bytes.\n",
841 dev, (long)(filp->f_pos));
842 printk(ST_DEB_MSG "st%d: Async write waits %d, finished %d.\n",
843 dev, STp->nbr_waits, STp->nbr_finished);
845 #endif
847 if(result ==0|| result == (-ENOSPC)) {
849 memset(cmd,0,10);
850 cmd[0] = WRITE_FILEMARKS;
851 cmd[4] =1+ STp->two_fm;
853 SCpnt =st_do_scsi(NULL, STp, cmd,0, ST_TIMEOUT, MAX_WRITE_RETRIES);
854 if(!SCpnt)
855 goto out;
857 SCpnt->request.rq_status = RQ_INACTIVE;/* Mark as not busy */
859 if((STp->buffer)->last_result_fatal !=0&&
860 ((SCpnt->sense_buffer[0] &0x70) !=0x70||
861 (SCpnt->sense_buffer[2] &0x4f) !=0x40||
862 ((SCpnt->sense_buffer[0] &0x80) !=0&&
863 (SCpnt->sense_buffer[3] | SCpnt->sense_buffer[4] |
864 SCpnt->sense_buffer[5] |
865 SCpnt->sense_buffer[6]) ==0)))/* Filter out successful write at EOM */
866 printk(KERN_ERR "st%d: Error on write filemark.\n", dev);
867 else{
868 if(STps->drv_file >=0)
869 STps->drv_file++ ;
870 STps->drv_block =0;
871 if(STp->two_fm)
872 cross_eof(STp, FALSE);
873 STps->eof = ST_FM;
877 #if DEBUG
878 if(debugging)
879 printk(ST_DEB_MSG "st%d: Buffer flushed, %d EOF(s) written\n",
880 dev, cmd[4]);
881 #endif
883 else if(!STp->rew_at_close) {
884 STps = &(STp->ps[STp->partition]);
885 if(!STm->sysv || STps->rw != ST_READING) {
886 if(STp->can_bsr)
887 flush_buffer(inode, filp,0);
888 else if(STps->eof == ST_FM_HIT) {
889 if(cross_eof(STp, FALSE)) {
890 if(STps->drv_file >=0)
891 STps->drv_file++;
892 STps->drv_block =0;
893 STps->eof = ST_FM;
895 else
896 STps->eof = ST_NOEOF;
899 else if((STps->eof == ST_NOEOF && !cross_eof(STp, TRUE)) ||
900 STps->eof == ST_FM_HIT) {
901 if(STps->drv_file >=0)
902 STps->drv_file++;
903 STps->drv_block =0;
904 STps->eof = ST_FM;
908 out:
909 if(STp->rew_at_close)
910 st_int_ioctl(inode, MTREW,1);
912 if(STp->door_locked == ST_LOCKED_AUTO)
913 st_int_ioctl(inode, MTUNLOCK,0);
915 if(STp->buffer != NULL) {
916 normalize_buffer(STp->buffer);
917 (STp->buffer)->in_use =0;
920 STp->in_use =0;
921 if(scsi_tapes[dev].device->host->hostt->module)
922 __MOD_DEC_USE_COUNT(scsi_tapes[dev].device->host->hostt->module);
923 if(st_template.module)
924 __MOD_DEC_USE_COUNT(st_template.module);
926 return0;
930 /* Write command */
931 static long
932 st_write(struct inode * inode,struct file * filp,const char* buf,
933 unsigned long count)
935 long total;
936 int i, do_count, blks, retval, transfer;
937 int write_threshold;
938 int doing_write =0;
939 static unsigned char cmd[10];
940 const char*b_point;
941 Scsi_Cmnd * SCpnt = NULL;
942 Scsi_Tape * STp;
943 ST_mode * STm;
944 ST_partstat * STps;
945 int dev =TAPE_NR(inode->i_rdev);
947 STp = &(scsi_tapes[dev]);
948 if(STp->ready != ST_READY)
949 return(-EIO);
950 STm = &(STp->modes[STp->current_mode]);
951 if(!STm->defined)
952 return(-ENXIO);
953 if(count ==0)
954 return0;
957 * If there was a bus reset, block further access
958 * to this device.
960 if( STp->device->was_reset )
961 return(-EIO);
963 #if DEBUG
964 if(!STp->in_use) {
965 printk(ST_DEB_MSG "st%d: Incorrect device.\n", dev);
966 return(-EIO);
968 #endif
970 if(STp->can_partitions &&
971 (retval =update_partition(inode)) <0)
972 return retval;
973 STps = &(STp->ps[STp->partition]);
975 if(STp->write_prot)
976 return(-EACCES);
978 if(STp->block_size ==0&&
979 count > (STp->buffer)->buffer_size &&
980 !enlarge_buffer(STp->buffer, count, STp->restr_dma))
981 return(-EOVERFLOW);
983 if(STp->do_auto_lock && STp->door_locked == ST_UNLOCKED &&
984 !st_int_ioctl(inode, MTLOCK,0))
985 STp->door_locked = ST_LOCKED_AUTO;
987 if(STps->rw == ST_READING) {
988 retval =flush_buffer(inode, filp,0);
989 if(retval)
990 return retval;
991 STps->rw = ST_WRITING;
993 else if(STps->rw != ST_WRITING &&
994 STps->drv_file ==0&& STps->drv_block ==0) {
995 if((retval =set_mode_densblk(inode, STp, STm)) <0)
996 return retval;
997 if(STm->default_compression != ST_DONT_TOUCH &&
998 !(STp->compression_changed)) {
999 if(st_compression(STp, (STm->default_compression == ST_YES))) {
1000 printk(KERN_WARNING "st%d: Can't set default compression.\n",
1001 dev);
1002 if(modes_defined)
1003 return(-EINVAL);
1008 if((STp->buffer)->writing) {
1009 write_behind_check(STp);
1010 if((STp->buffer)->last_result_fatal) {
1011 #if DEBUG
1012 if(debugging)
1013 printk(ST_DEB_MSG "st%d: Async write error (write) %x.\n", dev,
1014 (STp->buffer)->last_result);
1015 #endif
1016 if((STp->buffer)->last_result == INT_MAX)
1017 STps->eof = ST_EOM_OK;
1018 else
1019 STps->eof = ST_EOM_ERROR;
1022 if(STps->eof == ST_EOM_OK)
1023 return(-ENOSPC);
1024 else if(STps->eof == ST_EOM_ERROR)
1025 return(-EIO);
1027 /* Check the buffer readability in cases where copy_user might catch
1028 the problems after some tape movement. */
1029 if(STp->block_size !=0&&
1030 (copy_from_user(&i, buf,1) !=0||
1031 copy_from_user(&i, buf + count -1,1) !=0))
1032 return(-EFAULT);
1034 if(!STm->do_buffer_writes) {
1035 if(STp->block_size !=0&& (count % STp->block_size) !=0)
1036 return(-EIO);/* Write must be integral number of blocks */
1037 write_threshold =1;
1039 else
1040 write_threshold = (STp->buffer)->buffer_blocks * STp->block_size;
1041 if(!STm->do_async_writes)
1042 write_threshold--;
1044 total = count;
1046 memset(cmd,0,10);
1047 cmd[0] = WRITE_6;
1048 cmd[1] = (STp->block_size !=0);
1050 STps->rw = ST_WRITING;
1052 b_point = buf;
1053 while((STp->block_size ==0&& !STm->do_async_writes && count >0) ||
1054 (STp->block_size !=0&&
1055 (STp->buffer)->buffer_bytes + count > write_threshold))
1057 doing_write =1;
1058 if(STp->block_size ==0)
1059 do_count = count;
1060 else{
1061 do_count = (STp->buffer)->buffer_blocks * STp->block_size -
1062 (STp->buffer)->buffer_bytes;
1063 if(do_count > count)
1064 do_count = count;
1067 i =copy_from_user((STp->buffer)->b_data +
1068 (STp->buffer)->buffer_bytes, b_point, do_count);
1069 if(i) {
1070 if(SCpnt != NULL)
1071 SCpnt->request.rq_status = RQ_INACTIVE;
1072 return(-EFAULT);
1075 if(STp->block_size ==0)
1076 blks = transfer = do_count;
1077 else{
1078 blks = ((STp->buffer)->buffer_bytes + do_count) /
1079 STp->block_size;
1080 transfer = blks * STp->block_size;
1082 cmd[2] = blks >>16;
1083 cmd[3] = blks >>8;
1084 cmd[4] = blks;
1086 SCpnt =st_do_scsi(SCpnt, STp, cmd, transfer, ST_TIMEOUT, MAX_WRITE_RETRIES);
1087 if(!SCpnt)
1088 return(-EBUSY);
1090 if((STp->buffer)->last_result_fatal !=0) {
1091 #if DEBUG
1092 if(debugging)
1093 printk(ST_DEB_MSG "st%d: Error on write:\n", dev);
1094 #endif
1095 if((SCpnt->sense_buffer[0] &0x70) ==0x70&&
1096 (SCpnt->sense_buffer[2] &0x40)) {
1097 if(STp->block_size !=0&& (SCpnt->sense_buffer[0] &0x80) !=0)
1098 transfer = (SCpnt->sense_buffer[3] <<24) |
1099 (SCpnt->sense_buffer[4] <<16) |
1100 (SCpnt->sense_buffer[5] <<8) | SCpnt->sense_buffer[6];
1101 else if(STp->block_size ==0&&
1102 (SCpnt->sense_buffer[2] &0x0f) == VOLUME_OVERFLOW)
1103 transfer = do_count;
1104 else
1105 transfer =0;
1106 if(STp->block_size !=0)
1107 transfer *= STp->block_size;
1108 if(transfer <= do_count) {
1109 filp->f_pos += do_count - transfer;
1110 count -= do_count - transfer;
1111 if(STps->drv_block >=0) {
1112 if(STp->block_size ==0&& transfer < do_count)
1113 STps->drv_block++;
1114 else if(STp->block_size !=0)
1115 STps->drv_block += (do_count - transfer) / STp->block_size;
1117 STps->eof = ST_EOM_OK;
1118 retval = (-ENOSPC);/* EOM within current request */
1119 #if DEBUG
1120 if(debugging)
1121 printk(ST_DEB_MSG "st%d: EOM with %d bytes unwritten.\n",
1122 dev, transfer);
1123 #endif
1125 else{
1126 STps->eof = ST_EOM_ERROR;
1127 STps->drv_block = (-1);/* Too cautious? */
1128 retval = (-EIO);/* EOM for old data */
1129 #if DEBUG
1130 if(debugging)
1131 printk(ST_DEB_MSG "st%d: EOM with lost data.\n", dev);
1132 #endif
1135 else{
1136 STps->drv_block = (-1);/* Too cautious? */
1137 retval = (-EIO);
1140 SCpnt->request.rq_status = RQ_INACTIVE;/* Mark as not busy */
1141 (STp->buffer)->buffer_bytes =0;
1142 STp->dirty =0;
1143 if(count < total)
1144 return total - count;
1145 else
1146 return retval;
1148 filp->f_pos += do_count;
1149 b_point += do_count;
1150 count -= do_count;
1151 if(STps->drv_block >=0) {
1152 if(STp->block_size ==0)
1153 STps->drv_block++;
1154 else
1155 STps->drv_block += blks;
1157 (STp->buffer)->buffer_bytes =0;
1158 STp->dirty =0;
1160 if(count !=0) {
1161 STp->dirty =1;
1162 i =copy_from_user((STp->buffer)->b_data +
1163 (STp->buffer)->buffer_bytes, b_point, count);
1164 if(i) {
1165 if(SCpnt != NULL)
1166 SCpnt->request.rq_status = RQ_INACTIVE;
1167 return(-EFAULT);
1169 filp->f_pos += count;
1170 (STp->buffer)->buffer_bytes += count;
1171 count =0;
1174 if(doing_write && (STp->buffer)->last_result_fatal !=0) {
1175 SCpnt->request.rq_status = RQ_INACTIVE;
1176 return(STp->buffer)->last_result_fatal;
1179 if(STm->do_async_writes &&
1180 ((STp->buffer)->buffer_bytes >= STp->write_threshold ||
1181 STp->block_size ==0) ) {
1182 /* Schedule an asynchronous write */
1183 if(!SCpnt) {
1184 SCpnt =allocate_device(NULL, STp->device,1);
1185 if(!SCpnt)
1186 return(-EBUSY);
1188 if(STp->block_size ==0)
1189 (STp->buffer)->writing = (STp->buffer)->buffer_bytes;
1190 else
1191 (STp->buffer)->writing = ((STp->buffer)->buffer_bytes /
1192 STp->block_size) * STp->block_size;
1193 STp->dirty = !((STp->buffer)->writing ==
1194 (STp->buffer)->buffer_bytes);
1196 if(STp->block_size ==0)
1197 blks = (STp->buffer)->writing;
1198 else
1199 blks = (STp->buffer)->writing / STp->block_size;
1200 cmd[2] = blks >>16;
1201 cmd[3] = blks >>8;
1202 cmd[4] = blks;
1203 STp->sem = MUTEX_LOCKED;
1204 SCpnt->request.sem = &(STp->sem);
1205 SCpnt->request.rq_status = RQ_SCSI_BUSY;
1206 SCpnt->request.rq_dev = STp->devt;
1207 #if DEBUG
1208 STp->write_pending =1;
1209 #endif
1211 scsi_do_cmd(SCpnt,
1212 (void*) cmd, (STp->buffer)->b_data,
1213 (STp->buffer)->writing,
1214 st_sleep_done, ST_TIMEOUT, MAX_WRITE_RETRIES);
1216 else if(SCpnt != NULL)
1217 SCpnt->request.rq_status = RQ_INACTIVE;/* Mark as not busy */
1219 STps->at_sm &= (total ==0);
1220 if(total >0)
1221 STps->eof = ST_NOEOF;
1223 return( total);
1226 /* Read data from the tape. Returns zero in the normal case, one if the
1227 eof status has changed, and the negative error code in case of a
1228 fatal error. Otherwise updates the buffer and the eof state. */
1229 static long
1230 read_tape(struct inode *inode,long count, Scsi_Cmnd **aSCpnt)
1232 int transfer, blks, bytes;
1233 static unsigned char cmd[10];
1234 Scsi_Cmnd *SCpnt;
1235 Scsi_Tape *STp;
1236 ST_mode * STm;
1237 ST_partstat * STps;
1238 int dev =TAPE_NR(inode->i_rdev);
1239 int retval =0;
1241 if(count ==0)
1242 return0;
1244 STp = &(scsi_tapes[dev]);
1245 STm = &(STp->modes[STp->current_mode]);
1246 STps = &(STp->ps[STp->partition]);
1247 if(STps->eof == ST_FM_HIT)
1248 return1;
1250 memset(cmd,0,10);
1251 cmd[0] = READ_6;
1252 cmd[1] = (STp->block_size !=0);
1253 if(STp->block_size ==0)
1254 blks = bytes = count;
1255 else{
1256 if(STm->do_read_ahead) {
1257 blks = (STp->buffer)->buffer_blocks;
1258 bytes = blks * STp->block_size;
1260 else{
1261 bytes = count;
1262 if(bytes > (STp->buffer)->buffer_size)
1263 bytes = (STp->buffer)->buffer_size;
1264 blks = bytes / STp->block_size;
1265 bytes = blks * STp->block_size;
1268 cmd[2] = blks >>16;
1269 cmd[3] = blks >>8;
1270 cmd[4] = blks;
1272 SCpnt = *aSCpnt;
1273 SCpnt =st_do_scsi(SCpnt, STp, cmd, bytes, ST_TIMEOUT, MAX_RETRIES);
1274 *aSCpnt = SCpnt;
1275 if(!SCpnt)
1276 return(-EBUSY);
1278 (STp->buffer)->read_pointer =0;
1279 STps->at_sm =0;
1282 /* Something to check */
1283 if((STp->buffer)->last_result_fatal) {
1284 retval =1;
1285 #if DEBUG
1286 if(debugging)
1287 printk(ST_DEB_MSG "st%d: Sense: %2x %2x %2x %2x %2x %2x %2x %2x\n",
1288 dev,
1289 SCpnt->sense_buffer[0], SCpnt->sense_buffer[1],
1290 SCpnt->sense_buffer[2], SCpnt->sense_buffer[3],
1291 SCpnt->sense_buffer[4], SCpnt->sense_buffer[5],
1292 SCpnt->sense_buffer[6], SCpnt->sense_buffer[7]);
1293 #endif
1294 if((SCpnt->sense_buffer[0] &0x70) ==0x70) {/* extended sense */
1296 if((SCpnt->sense_buffer[2] &0x0f) == BLANK_CHECK)
1297 SCpnt->sense_buffer[2] &=0xcf;/* No need for EOM in this case */
1299 if((SCpnt->sense_buffer[2] &0xe0) !=0) {/* EOF, EOM, or ILI */
1300 /* Compute the residual count */
1301 if((SCpnt->sense_buffer[0] &0x80) !=0)
1302 transfer = (SCpnt->sense_buffer[3] <<24) |
1303 (SCpnt->sense_buffer[4] <<16) |
1304 (SCpnt->sense_buffer[5] <<8) | SCpnt->sense_buffer[6];
1305 else
1306 transfer =0;
1307 if(STp->block_size ==0&&
1308 (SCpnt->sense_buffer[2] &0x0f) == MEDIUM_ERROR)
1309 transfer = bytes;
1311 if(SCpnt->sense_buffer[2] &0x20) {/* ILI */
1312 if(STp->block_size ==0) {
1313 if(transfer <=0)
1314 transfer =0;
1315 (STp->buffer)->buffer_bytes = bytes - transfer;
1317 else{
1318 SCpnt->request.rq_status = RQ_INACTIVE;/* Mark as not busy */
1319 SCpnt = NULL;
1320 if(transfer == blks) {/* We did not get anything, error */
1321 printk(KERN_NOTICE "st%d: Incorrect block size.\n", dev);
1322 if(STps->drv_block >=0)
1323 STps->drv_block += blks - transfer +1;
1324 st_int_ioctl(inode, MTBSR,1);
1325 return(-EIO);
1327 /* We have some data, deliver it */
1328 (STp->buffer)->buffer_bytes = (blks - transfer) *
1329 STp->block_size;
1330 #if DEBUG
1331 if(debugging)
1332 printk(ST_DEB_MSG
1333 "st%d: ILI but enough data received %ld %d.\n",
1334 dev, count, (STp->buffer)->buffer_bytes);
1335 #endif
1336 if(STps->drv_block >=0)
1337 STps->drv_block +=1;
1338 if(st_int_ioctl(inode, MTBSR,1))
1339 return(-EIO);
1342 else if(SCpnt->sense_buffer[2] &0x80) {/* FM overrides EOM */
1343 if(STps->eof != ST_FM_HIT)
1344 STps->eof = ST_FM_HIT;
1345 else
1346 STps->eof = ST_EOD_2;
1347 if(STp->block_size ==0)
1348 (STp->buffer)->buffer_bytes =0;
1349 else
1350 (STp->buffer)->buffer_bytes =
1351 bytes - transfer * STp->block_size;
1352 #if DEBUG
1353 if(debugging)
1354 printk(ST_DEB_MSG
1355 "st%d: EOF detected (%d bytes read).\n",
1356 dev, (STp->buffer)->buffer_bytes);
1357 #endif
1359 else if(SCpnt->sense_buffer[2] &0x40) {
1360 if(STps->eof == ST_FM)
1361 STps->eof = ST_EOD_1;
1362 else
1363 STps->eof = ST_EOM_OK;
1364 if(STp->block_size ==0)
1365 (STp->buffer)->buffer_bytes = bytes - transfer;
1366 else
1367 (STp->buffer)->buffer_bytes =
1368 bytes - transfer * STp->block_size;
1369 #if DEBUG
1370 if(debugging)
1371 printk(ST_DEB_MSG "st%d: EOM detected (%d bytes read).\n",
1372 dev, (STp->buffer)->buffer_bytes);
1373 #endif
1375 }/* end of EOF, EOM, ILI test */
1376 else{/* nonzero sense key */
1377 #if DEBUG
1378 if(debugging)
1379 printk(ST_DEB_MSG "st%d: Tape error while reading.\n", dev);
1380 #endif
1381 STps->drv_block = (-1);
1382 if(STps->eof == ST_FM &&
1383 (SCpnt->sense_buffer[2] &0x0f) == BLANK_CHECK) {
1384 #if DEBUG
1385 if(debugging)
1386 printk(ST_DEB_MSG
1387 "st%d: Zero returned for first BLANK CHECK after EOF.\n",
1388 dev);
1389 #endif
1390 STps->eof = ST_EOD_2;/* First BLANK_CHECK after FM */
1392 else/* Some other extended sense code */
1393 retval = (-EIO);
1395 }/* End of extended sense test */
1396 else{/* Non-extended sense */
1397 retval = (STp->buffer)->last_result_fatal;
1400 }/* End of error handling */
1401 else/* Read successful */
1402 (STp->buffer)->buffer_bytes = bytes;
1404 if(STps->drv_block >=0) {
1405 if(STp->block_size ==0)
1406 STps->drv_block++;
1407 else
1408 STps->drv_block += (STp->buffer)->buffer_bytes / STp->block_size;
1411 return retval;
1415 /* Read command */
1416 static long
1417 st_read(struct inode * inode,struct file * filp,char* buf,unsigned long count)
1419 long total;
1420 int i, transfer;
1421 int special;
1422 Scsi_Cmnd * SCpnt = NULL;
1423 Scsi_Tape * STp;
1424 ST_mode * STm;
1425 ST_partstat * STps;
1426 int dev =TAPE_NR(inode->i_rdev);
1428 STp = &(scsi_tapes[dev]);
1429 if(STp->ready != ST_READY)
1430 return(-EIO);
1431 STm = &(STp->modes[STp->current_mode]);
1432 if(!STm->defined)
1433 return(-ENXIO);
1434 #if DEBUG
1435 if(!STp->in_use) {
1436 printk(ST_DEB_MSG "st%d: Incorrect device.\n", dev);
1437 return(-EIO);
1439 #endif
1441 if(STp->can_partitions &&
1442 (total =update_partition(inode)) <0)
1443 return total;
1445 if(STp->block_size ==0&&
1446 count > (STp->buffer)->buffer_size &&
1447 !enlarge_buffer(STp->buffer, count, STp->restr_dma))
1448 return(-EOVERFLOW);
1450 if(!(STm->do_read_ahead) && STp->block_size !=0&&
1451 (count % STp->block_size) !=0)
1452 return(-EIO);/* Read must be integral number of blocks */
1454 if(STp->do_auto_lock && STp->door_locked == ST_UNLOCKED &&
1455 !st_int_ioctl(inode, MTLOCK,0))
1456 STp->door_locked = ST_LOCKED_AUTO;
1458 STps = &(STp->ps[STp->partition]);
1459 if(STps->rw == ST_WRITING) {
1460 transfer =flush_buffer(inode, filp,0);
1461 if(transfer)
1462 return transfer;
1463 STps->rw = ST_READING;
1466 #if DEBUG
1467 if(debugging && STps->eof != ST_NOEOF)
1468 printk(ST_DEB_MSG "st%d: EOF/EOM flag up (%d). Bytes %d\n", dev,
1469 STps->eof, (STp->buffer)->buffer_bytes);
1470 #endif
1471 if((STp->buffer)->buffer_bytes ==0&&
1472 STps->eof >= ST_EOD_1) {
1473 if(STps->eof < ST_EOD) {
1474 STps->eof +=1;
1475 return0;
1477 return(-EIO);/* EOM or Blank Check */
1480 /* Check the buffer writability before any tape movement. Don't alter
1481 buffer data. */
1482 if(copy_from_user(&i, buf,1) !=0||
1483 copy_to_user(buf, &i,1) !=0||
1484 copy_from_user(&i, buf + count -1,1) !=0||
1485 copy_to_user(buf + count -1, &i,1) !=0)
1486 return(-EFAULT);
1488 STps->rw = ST_READING;
1491 /* Loop until enough data in buffer or a special condition found */
1492 for(total =0, special =0; total < count && !special; ) {
1494 /* Get new data if the buffer is empty */
1495 if((STp->buffer)->buffer_bytes ==0) {
1496 special =read_tape(inode, count - total, &SCpnt);
1497 if(special <0) {/* No need to continue read */
1498 if(SCpnt != NULL)
1499 SCpnt->request.rq_status = RQ_INACTIVE;/* Mark as not busy */
1500 return special;
1504 /* Move the data from driver buffer to user buffer */
1505 if((STp->buffer)->buffer_bytes >0) {
1506 #if DEBUG
1507 if(debugging && STps->eof != ST_NOEOF)
1508 printk(ST_DEB_MSG "st%d: EOF up (%d). Left %d, needed %ld.\n", dev,
1509 STps->eof, (STp->buffer)->buffer_bytes, count - total);
1510 #endif
1511 transfer = (STp->buffer)->buffer_bytes < count - total ?
1512 (STp->buffer)->buffer_bytes : count - total;
1513 i =copy_to_user(buf, (STp->buffer)->b_data +
1514 (STp->buffer)->read_pointer, transfer);
1515 if(i) {
1516 if(SCpnt != NULL)
1517 SCpnt->request.rq_status = RQ_INACTIVE;
1518 return(-EFAULT);
1520 filp->f_pos += transfer;
1521 buf += transfer;
1522 total += transfer;
1523 (STp->buffer)->buffer_bytes -= transfer;
1524 (STp->buffer)->read_pointer += transfer;
1527 if(STp->block_size ==0)
1528 break;/* Read only one variable length block */
1530 }/* for (total = 0, special = 0; total < count && !special; ) */
1532 if(SCpnt != NULL)
1533 SCpnt->request.rq_status = RQ_INACTIVE;/* Mark as not busy */
1535 /* Change the eof state if no data from tape or buffer */
1536 if(total ==0) {
1537 if(STps->eof == ST_FM_HIT) {
1538 STps->eof = ST_FM;
1539 STps->drv_block =0;
1540 if(STps->drv_file >=0)
1541 STps->drv_file++;
1543 else if(STps->eof == ST_EOD_1) {
1544 STps->eof = ST_EOD_2;
1545 STps->drv_block =0;
1546 if(STps->drv_file >=0)
1547 STps->drv_file++;
1549 else if(STps->eof == ST_EOD_2)
1550 STps->eof = ST_EOD;
1552 else if(STps->eof == ST_FM)
1553 STps->eof = ST_NOEOF;
1555 return total;
1560 /* Set the driver options */
1561 static void
1562 st_log_options(Scsi_Tape *STp, ST_mode *STm,int dev)
1564 printk(KERN_INFO
1565 "st%d: Mode %d options: buffer writes: %d, async writes: %d, read ahead: %d\n",
1566 dev, STp->current_mode, STm->do_buffer_writes, STm->do_async_writes,
1567 STm->do_read_ahead);
1568 printk(KERN_INFO
1569 "st%d: can bsr: %d, two FMs: %d, fast mteom: %d, auto lock: %d,\n",
1570 dev, STp->can_bsr, STp->two_fm, STp->fast_mteom, STp->do_auto_lock);
1571 printk(KERN_INFO
1572 "st%d: defs for wr: %d, no block limits: %d, partitions: %d, s2 log: %d\n",
1573 dev, STm->defaults_for_writes, STp->omit_blklims, STp->can_partitions,
1574 STp->scsi2_logical);
1575 #if DEBUG
1576 printk(KERN_INFO
1577 "st%d: debugging: %d\n",
1578 dev, debugging);
1579 #endif
1583 static int
1584 st_set_options(struct inode * inode,long options)
1586 int value;
1587 long code;
1588 Scsi_Tape *STp;
1589 ST_mode *STm;
1590 int dev =TAPE_NR(inode->i_rdev);
1592 STp = &(scsi_tapes[dev]);
1593 STm = &(STp->modes[STp->current_mode]);
1594 if(!STm->defined) {
1595 memcpy(STm, &(STp->modes[0]),sizeof(ST_mode));
1596 modes_defined = TRUE;
1597 #if DEBUG
1598 if(debugging)
1599 printk(ST_DEB_MSG "st%d: Initialized mode %d definition from mode 0\n",
1600 dev, STp->current_mode);
1601 #endif
1604 code = options & MT_ST_OPTIONS;
1605 if(code == MT_ST_BOOLEANS) {
1606 STm->do_buffer_writes = (options & MT_ST_BUFFER_WRITES) !=0;
1607 STm->do_async_writes = (options & MT_ST_ASYNC_WRITES) !=0;
1608 STm->defaults_for_writes = (options & MT_ST_DEF_WRITES) !=0;
1609 STm->do_read_ahead = (options & MT_ST_READ_AHEAD) !=0;
1610 STp->two_fm = (options & MT_ST_TWO_FM) !=0;
1611 STp->fast_mteom = (options & MT_ST_FAST_MTEOM) !=0;
1612 STp->do_auto_lock = (options & MT_ST_AUTO_LOCK) !=0;
1613 STp->can_bsr = (options & MT_ST_CAN_BSR) !=0;
1614 STp->omit_blklims = (options & MT_ST_NO_BLKLIMS) !=0;
1615 if((STp->device)->scsi_level >= SCSI_2)
1616 STp->can_partitions = (options & MT_ST_CAN_PARTITIONS) !=0;
1617 STp->scsi2_logical = (options & MT_ST_SCSI2LOGICAL) !=0;
1618 #if DEBUG
1619 debugging = (options & MT_ST_DEBUGGING) !=0;
1620 #endif
1621 st_log_options(STp, STm, dev);
1623 else if(code == MT_ST_SETBOOLEANS || code == MT_ST_CLEARBOOLEANS) {
1624 value = (code == MT_ST_SETBOOLEANS);
1625 if((options & MT_ST_BUFFER_WRITES) !=0)
1626 STm->do_buffer_writes = value;
1627 if((options & MT_ST_ASYNC_WRITES) !=0)
1628 STm->do_async_writes = value;
1629 if((options & MT_ST_DEF_WRITES) !=0)
1630 STm->defaults_for_writes = value;
1631 if((options & MT_ST_READ_AHEAD) !=0)
1632 STm->do_read_ahead = value;
1633 if((options & MT_ST_TWO_FM) !=0)
1634 STp->two_fm = value;
1635 if((options & MT_ST_FAST_MTEOM) !=0)
1636 STp->fast_mteom = value;
1637 if((options & MT_ST_AUTO_LOCK) !=0)
1638 STp->do_auto_lock = value;
1639 if((options & MT_ST_CAN_BSR) !=0)
1640 STp->can_bsr = value;
1641 if((options & MT_ST_NO_BLKLIMS) !=0)
1642 STp->omit_blklims = value;
1643 if((STp->device)->scsi_level >= SCSI_2 &&
1644 (options & MT_ST_CAN_PARTITIONS) !=0)
1645 STp->can_partitions = value;
1646 if((options & MT_ST_SCSI2LOGICAL) !=0)
1647 STp->scsi2_logical = value;
1648 #if DEBUG
1649 if((options & MT_ST_DEBUGGING) !=0)
1650 debugging = value;
1651 #endif
1652 st_log_options(STp, STm, dev);
1654 else if(code == MT_ST_WRITE_THRESHOLD) {
1655 value = (options & ~MT_ST_OPTIONS) * ST_BLOCK_SIZE;
1656 if(value <1|| value > st_buffer_size) {
1657 printk(KERN_WARNING "st%d: Write threshold %d too small or too large.\n",
1658 dev, value);
1659 return(-EIO);
1661 STp->write_threshold = value;
1662 printk(KERN_INFO "st%d: Write threshold set to %d bytes.\n",
1663 dev, value);
1665 else if(code == MT_ST_DEF_BLKSIZE) {
1666 value = (options & ~MT_ST_OPTIONS);
1667 if(value == ~MT_ST_OPTIONS) {
1668 STm->default_blksize = (-1);
1669 printk(KERN_INFO "st%d: Default block size disabled.\n", dev);
1671 else{
1672 STm->default_blksize = value;
1673 printk(KERN_INFO "st%d: Default block size set to %d bytes.\n",
1674 dev, STm->default_blksize);
1677 else if(code == MT_ST_DEF_OPTIONS) {
1678 code = (options & ~MT_ST_CLEAR_DEFAULT);
1679 value = (options & MT_ST_CLEAR_DEFAULT);
1680 if(code == MT_ST_DEF_DENSITY) {
1681 if(value == MT_ST_CLEAR_DEFAULT) {
1682 STm->default_density = (-1);
1683 printk(KERN_INFO "st%d: Density default disabled.\n", dev);
1685 else{
1686 STm->default_density = value &0xff;
1687 printk(KERN_INFO "st%d: Density default set to %x\n",
1688 dev, STm->default_density);
1691 else if(code == MT_ST_DEF_DRVBUFFER) {
1692 if(value == MT_ST_CLEAR_DEFAULT) {
1693 STp->default_drvbuffer =0xff;
1694 printk(KERN_INFO "st%d: Drive buffer default disabled.\n", dev);
1696 else{
1697 STp->default_drvbuffer = value &7;
1698 printk(KERN_INFO "st%d: Drive buffer default set to %x\n",
1699 dev, STp->default_drvbuffer);
1702 else if(code == MT_ST_DEF_COMPRESSION) {
1703 if(value == MT_ST_CLEAR_DEFAULT) {
1704 STm->default_compression = ST_DONT_TOUCH;
1705 printk(KERN_INFO "st%d: Compression default disabled.\n", dev);
1707 else{
1708 STm->default_compression = (value &1? ST_YES : ST_NO);
1709 printk(KERN_INFO "st%d: Compression default set to %x\n",
1710 dev, (value &1));
1714 else
1715 return(-EIO);
1717 return0;
1721 #define COMPRESSION_PAGE 0x0f
1722 #define COMPRESSION_PAGE_LENGTH 16
1724 #define MODE_HEADER_LENGTH 4
1726 #define DCE_MASK 0x80
1727 #define DCC_MASK 0x40
1728 #define RED_MASK 0x60
1731 /* Control the compression with mode page 15. Algorithm not changed if zero. */
1732 static int
1733 st_compression(Scsi_Tape * STp,int state)
1735 int dev;
1736 unsigned char cmd[10];
1737 Scsi_Cmnd * SCpnt = NULL;
1739 if(STp->ready != ST_READY)
1740 return(-EIO);
1742 /* Read the current page contents */
1743 memset(cmd,0,10);
1744 cmd[0] = MODE_SENSE;
1745 cmd[1] =8;
1746 cmd[2] = COMPRESSION_PAGE;
1747 cmd[4] = COMPRESSION_PAGE_LENGTH + MODE_HEADER_LENGTH;
1749 SCpnt =st_do_scsi(SCpnt, STp, cmd, cmd[4], ST_TIMEOUT,0);
1750 if(SCpnt == NULL)
1751 return(-EBUSY);
1752 dev =TAPE_NR(SCpnt->request.rq_dev);
1754 if((STp->buffer)->last_result_fatal !=0) {
1755 #if DEBUG
1756 if(debugging)
1757 printk(ST_DEB_MSG "st%d: Compression mode page not supported.\n", dev);
1758 #endif
1759 SCpnt->request.rq_status = RQ_INACTIVE;/* Mark as not busy */
1760 return(-EIO);
1762 #if DEBUG
1763 if(debugging)
1764 printk(ST_DEB_MSG "st%d: Compression state is %d.\n", dev,
1765 ((STp->buffer)->b_data[MODE_HEADER_LENGTH +2] & DCE_MASK ?1:0));
1766 #endif
1768 /* Check if compression can be changed */
1769 if(((STp->buffer)->b_data[MODE_HEADER_LENGTH +2] & DCC_MASK) ==0) {
1770 #if DEBUG
1771 if(debugging)
1772 printk(ST_DEB_MSG "st%d: Compression not supported.\n", dev);
1773 #endif
1774 SCpnt->request.rq_status = RQ_INACTIVE;/* Mark as not busy */
1775 return(-EIO);
1778 /* Do the change */
1779 if(state)
1780 (STp->buffer)->b_data[MODE_HEADER_LENGTH +2] |= DCE_MASK;
1781 else
1782 (STp->buffer)->b_data[MODE_HEADER_LENGTH +2] &= ~DCE_MASK;
1784 memset(cmd,0,10);
1785 cmd[0] = MODE_SELECT;
1786 cmd[1] =0x10;
1787 cmd[4] = COMPRESSION_PAGE_LENGTH + MODE_HEADER_LENGTH;
1789 (STp->buffer)->b_data[0] =0;/* Reserved data length */
1790 (STp->buffer)->b_data[1] =0;/* Reserved media type byte */
1791 (STp->buffer)->b_data[MODE_HEADER_LENGTH] &=0x3f;
1792 SCpnt =st_do_scsi(SCpnt, STp, cmd, cmd[4], ST_TIMEOUT,0);
1794 if((STp->buffer)->last_result_fatal !=0) {
1795 #if DEBUG
1796 if(debugging)
1797 printk(ST_DEB_MSG "st%d: Compression change failed.\n", dev);
1798 #endif
1799 SCpnt->request.rq_status = RQ_INACTIVE;/* Mark as not busy */
1800 return(-EIO);
1803 #if DEBUG
1804 if(debugging)
1805 printk(ST_DEB_MSG "st%d: Compression state changed to %d.\n",
1806 dev, state);
1807 #endif
1809 SCpnt->request.rq_status = RQ_INACTIVE;/* Mark as not busy */
1810 STp->compression_changed = TRUE;
1811 return0;
1815 /* Internal ioctl function */
1816 static int
1817 st_int_ioctl(struct inode * inode,
1818 unsigned int cmd_in,unsigned long arg)
1820 int timeout = ST_LONG_TIMEOUT;
1821 long ltmp;
1822 int i, ioctl_result;
1823 int chg_eof = TRUE;
1824 unsigned char cmd[10];
1825 Scsi_Cmnd * SCpnt;
1826 Scsi_Tape * STp;
1827 ST_partstat * STps;
1828 int fileno, blkno, at_sm, undone, datalen;
1829 int dev =TAPE_NR(inode->i_rdev);
1831 STp = &(scsi_tapes[dev]);
1832 if(STp->ready != ST_READY && cmd_in != MTLOAD)
1833 return(-EIO);
1834 STps = &(STp->ps[STp->partition]);
1835 fileno = STps->drv_file;
1836 blkno = STps->drv_block;
1837 at_sm = STps->at_sm;
1839 memset(cmd,0,10);
1840 datalen =0;
1841 switch(cmd_in) {
1842 case MTFSFM:
1843 chg_eof = FALSE;/* Changed from the FSF after this */
1844 case MTFSF:
1845 cmd[0] = SPACE;
1846 cmd[1] =0x01;/* Space FileMarks */
1847 cmd[2] = (arg >>16);
1848 cmd[3] = (arg >>8);
1849 cmd[4] = arg;
1850 #if DEBUG
1851 if(debugging)
1852 printk(ST_DEB_MSG "st%d: Spacing tape forward over %d filemarks.\n",
1853 dev, cmd[2] *65536+ cmd[3] *256+ cmd[4]);
1854 #endif
1855 if(fileno >=0)
1856 fileno += arg;
1857 blkno =0;
1858 at_sm &= (arg ==0);
1859 break;
1860 case MTBSFM:
1861 chg_eof = FALSE;/* Changed from the FSF after this */
1862 case MTBSF:
1863 cmd[0] = SPACE;
1864 cmd[1] =0x01;/* Space FileMarks */
1865 ltmp = (-arg);
1866 cmd[2] = (ltmp >>16);
1867 cmd[3] = (ltmp >>8);
1868 cmd[4] = ltmp;
1869 #if DEBUG
1870 if(debugging) {
1871 if(cmd[2] &0x80)
1872 ltmp =0xff000000;
1873 ltmp = ltmp | (cmd[2] <<16) | (cmd[3] <<8) | cmd[4];
1874 printk(ST_DEB_MSG "st%d: Spacing tape backward over %ld filemarks.\n",
1875 dev, (-ltmp));
1877 #endif
1878 if(fileno >=0)
1879 fileno -= arg;
1880 blkno = (-1);/* We can't know the block number */
1881 at_sm &= (arg ==0);
1882 break;
1883 case MTFSR:
1884 cmd[0] = SPACE;
1885 cmd[1] =0x00;/* Space Blocks */
1886 cmd[2] = (arg >>16);
1887 cmd[3] = (arg >>8);
1888 cmd[4] = arg;
1889 #if DEBUG
1890 if(debugging)
1891 printk(ST_DEB_MSG "st%d: Spacing tape forward %d blocks.\n", dev,
1892 cmd[2] *65536+ cmd[3] *256+ cmd[4]);
1893 #endif
1894 if(blkno >=0)
1895 blkno += arg;
1896 at_sm &= (arg ==0);
1897 break;
1898 case MTBSR:
1899 cmd[0] = SPACE;
1900 cmd[1] =0x00;/* Space Blocks */
1901 ltmp = (-arg);
1902 cmd[2] = (ltmp >>16);
1903 cmd[3] = (ltmp >>8);
1904 cmd[4] = ltmp;
1905 #if DEBUG
1906 if(debugging) {
1907 if(cmd[2] &0x80)
1908 ltmp =0xff000000;
1909 ltmp = ltmp | (cmd[2] <<16) | (cmd[3] <<8) | cmd[4];
1910 printk(ST_DEB_MSG "st%d: Spacing tape backward %ld blocks.\n", dev, (-ltmp));
1912 #endif
1913 if(blkno >=0)
1914 blkno -= arg;
1915 at_sm &= (arg ==0);
1916 break;
1917 case MTFSS:
1918 cmd[0] = SPACE;
1919 cmd[1] =0x04;/* Space Setmarks */
1920 cmd[2] = (arg >>16);
1921 cmd[3] = (arg >>8);
1922 cmd[4] = arg;
1923 #if DEBUG
1924 if(debugging)
1925 printk(ST_DEB_MSG "st%d: Spacing tape forward %d setmarks.\n", dev,
1926 cmd[2] *65536+ cmd[3] *256+ cmd[4]);
1927 #endif
1928 if(arg !=0) {
1929 blkno = fileno = (-1);
1930 at_sm =1;
1932 break;
1933 case MTBSS:
1934 cmd[0] = SPACE;
1935 cmd[1] =0x04;/* Space Setmarks */
1936 ltmp = (-arg);
1937 cmd[2] = (ltmp >>16);
1938 cmd[3] = (ltmp >>8);
1939 cmd[4] = ltmp;
1940 #if DEBUG
1941 if(debugging) {
1942 if(cmd[2] &0x80)
1943 ltmp =0xff000000;
1944 ltmp = ltmp | (cmd[2] <<16) | (cmd[3] <<8) | cmd[4];
1945 printk(ST_DEB_MSG "st%d: Spacing tape backward %ld setmarks.\n",
1946 dev, (-ltmp));
1948 #endif
1949 if(arg !=0) {
1950 blkno = fileno = (-1);
1951 at_sm =1;
1953 break;
1954 case MTWEOF:
1955 case MTWSM:
1956 if(STp->write_prot)
1957 return(-EACCES);
1958 cmd[0] = WRITE_FILEMARKS;
1959 if(cmd_in == MTWSM)
1960 cmd[1] =2;
1961 cmd[2] = (arg >>16);
1962 cmd[3] = (arg >>8);
1963 cmd[4] = arg;
1964 timeout = ST_TIMEOUT;
1965 #if DEBUG
1966 if(debugging) {
1967 if(cmd_in == MTWEOF)
1968 printk(ST_DEB_MSG "st%d: Writing %d filemarks.\n", dev,
1969 cmd[2] *65536+ cmd[3] *256+ cmd[4]);
1970 else
1971 printk(ST_DEB_MSG "st%d: Writing %d setmarks.\n", dev,
1972 cmd[2] *65536+ cmd[3] *256+ cmd[4]);
1974 #endif
1975 if(fileno >=0)
1976 fileno += arg;
1977 blkno =0;
1978 at_sm = (cmd_in == MTWSM);
1979 break;
1980 case MTREW:
1981 cmd[0] = REZERO_UNIT;
1982 #if ST_NOWAIT
1983 cmd[1] =1;/* Don't wait for completion */
1984 timeout = ST_TIMEOUT;
1985 #endif
1986 #if DEBUG
1987 if(debugging)
1988 printk(ST_DEB_MSG "st%d: Rewinding tape.\n", dev);
1989 #endif
1990 fileno = blkno = at_sm =0;
1991 break;
1992 case MTOFFL:
1993 case MTLOAD:
1994 case MTUNLOAD:
1995 cmd[0] = START_STOP;
1996 if(cmd_in == MTLOAD)
1997 cmd[4] |=1;
1999 * If arg >= 1 && arg <= 6 Enhanced load/unload in HP C1553A
2001 if(cmd_in != MTOFFL &&
2002 arg >=1+ MT_ST_HPLOADER_OFFSET
2003 && arg <=6+ MT_ST_HPLOADER_OFFSET) {
2004 #if DEBUG
2005 if(debugging) {
2006 printk(ST_DEB_MSG "st%d: Enhanced %sload slot %2ld.\n",
2007 dev, (cmd[4]) ?"":"un",
2008 arg - MT_ST_HPLOADER_OFFSET);
2010 #endif
2011 cmd[3] = arg - MT_ST_HPLOADER_OFFSET;/* MediaID field of C1553A */
2013 #if ST_NOWAIT
2014 cmd[1] =1;/* Don't wait for completion */
2015 timeout = ST_TIMEOUT;
2016 #else
2017 timeout = ST_LONG_TIMEOUT *8;
2018 #endif
2019 #if DEBUG
2020 if(debugging) {
2021 if(cmd_in != MTLOAD)
2022 printk(ST_DEB_MSG "st%d: Unloading tape.\n", dev);
2023 else
2024 printk(ST_DEB_MSG "st%d: Loading tape.\n", dev);
2026 #endif
2027 fileno = blkno = at_sm =0;
2028 break;
2029 case MTNOP:
2030 #if DEBUG
2031 if(debugging)
2032 printk(ST_DEB_MSG "st%d: No op on tape.\n", dev);
2033 #endif
2034 return0;/* Should do something ? */
2035 break;
2036 case MTRETEN:
2037 cmd[0] = START_STOP;
2038 #if ST_NOWAIT
2039 cmd[1] =1;/* Don't wait for completion */
2040 timeout = ST_TIMEOUT;
2041 #endif
2042 cmd[4] =3;
2043 #if DEBUG
2044 if(debugging)
2045 printk(ST_DEB_MSG "st%d: Retensioning tape.\n", dev);
2046 #endif
2047 fileno = blkno = at_sm =0;
2048 break;
2049 case MTEOM:
2050 if(!STp->fast_mteom) {
2051 /* space to the end of tape */
2052 ioctl_result =st_int_ioctl(inode, MTFSF,0x3fff);
2053 fileno = STps->drv_file;
2054 if(STps->eof >= ST_EOD_1)
2055 return0;
2056 /* The next lines would hide the number of spaced FileMarks
2057 That's why I inserted the previous lines. I had no luck
2058 with detecting EOM with FSF, so we go now to EOM.
2059 Joerg Weule */
2061 else
2062 fileno = (-1);
2063 cmd[0] = SPACE;
2064 cmd[1] =3;
2065 #if DEBUG
2066 if(debugging)
2067 printk(ST_DEB_MSG "st%d: Spacing to end of recorded medium.\n", dev);
2068 #endif
2069 blkno =0;
2070 at_sm =0;
2071 break;
2072 case MTERASE:
2073 if(STp->write_prot)
2074 return(-EACCES);
2075 cmd[0] = ERASE;
2076 cmd[1] =1;/* To the end of tape */
2077 #if ST_NOWAIT
2078 cmd[1] |=2;/* Don't wait for completion */
2079 timeout = ST_TIMEOUT;
2080 #else
2081 timeout = ST_LONG_TIMEOUT *8;
2082 #endif
2083 #if DEBUG
2084 if(debugging)
2085 printk(ST_DEB_MSG "st%d: Erasing tape.\n", dev);
2086 #endif
2087 fileno = blkno = at_sm =0;
2088 break;
2089 case MTLOCK:
2090 chg_eof = FALSE;
2091 cmd[0] = ALLOW_MEDIUM_REMOVAL;
2092 cmd[4] = SCSI_REMOVAL_PREVENT;
2093 #if DEBUG
2094 if(debugging)
2095 printk(ST_DEB_MSG "st%d: Locking drive door.\n", dev);
2096 #endif;
2097 break;
2098 case MTUNLOCK:
2099 chg_eof = FALSE;
2100 cmd[0] = ALLOW_MEDIUM_REMOVAL;
2101 cmd[4] = SCSI_REMOVAL_ALLOW;
2102 #if DEBUG
2103 if(debugging)
2104 printk(ST_DEB_MSG "st%d: Unlocking drive door.\n", dev);
2105 #endif;
2106 break;
2107 case MTSETBLK:/* Set block length */
2108 case MTSETDENSITY:/* Set tape density */
2109 case MTSETDRVBUFFER:/* Set drive buffering */
2110 case SET_DENS_AND_BLK:/* Set density and block size */
2111 chg_eof = FALSE;
2112 if(STp->dirty || (STp->buffer)->buffer_bytes !=0)
2113 return(-EIO);/* Not allowed if data in buffer */
2114 if((cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) &&
2115 (arg & MT_ST_BLKSIZE_MASK) !=0&&
2116 ((arg & MT_ST_BLKSIZE_MASK) < STp->min_block ||
2117 (arg & MT_ST_BLKSIZE_MASK) > STp->max_block ||
2118 (arg & MT_ST_BLKSIZE_MASK) > st_buffer_size)) {
2119 printk(KERN_WARNING "st%d: Illegal block size.\n", dev);
2120 return(-EINVAL);
2122 cmd[0] = MODE_SELECT;
2123 cmd[4] = datalen =12;
2125 memset((STp->buffer)->b_data,0,12);
2126 if(cmd_in == MTSETDRVBUFFER)
2127 (STp->buffer)->b_data[2] = (arg &7) <<4;
2128 else
2129 (STp->buffer)->b_data[2] =
2130 STp->drv_buffer <<4;
2131 (STp->buffer)->b_data[3] =8;/* block descriptor length */
2132 if(cmd_in == MTSETDENSITY) {
2133 (STp->buffer)->b_data[4] = arg;
2134 STp->density_changed = TRUE;/* At least we tried ;-) */
2136 else if(cmd_in == SET_DENS_AND_BLK)
2137 (STp->buffer)->b_data[4] = arg >>24;
2138 else
2139 (STp->buffer)->b_data[4] = STp->density;
2140 if(cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) {
2141 ltmp = arg & MT_ST_BLKSIZE_MASK;
2142 if(cmd_in == MTSETBLK)
2143 STp->blksize_changed = TRUE;/* At least we tried ;-) */
2145 else
2146 ltmp = STp->block_size;
2147 (STp->buffer)->b_data[9] = (ltmp >>16);
2148 (STp->buffer)->b_data[10] = (ltmp >>8);
2149 (STp->buffer)->b_data[11] = ltmp;
2150 timeout = ST_TIMEOUT;
2151 #if DEBUG
2152 if(debugging) {
2153 if(cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK)
2154 printk(ST_DEB_MSG "st%d: Setting block size to %d bytes.\n", dev,
2155 (STp->buffer)->b_data[9] *65536+
2156 (STp->buffer)->b_data[10] *256+
2157 (STp->buffer)->b_data[11]);
2158 if(cmd_in == MTSETDENSITY || cmd_in == SET_DENS_AND_BLK)
2159 printk(ST_DEB_MSG "st%d: Setting density code to %x.\n", dev,
2160 (STp->buffer)->b_data[4]);
2161 if(cmd_in == MTSETDRVBUFFER)
2162 printk(ST_DEB_MSG "st%d: Setting drive buffer code to %d.\n", dev,
2163 ((STp->buffer)->b_data[2] >>4) &7);
2165 #endif
2166 break;
2167 default:
2168 return(-ENOSYS);
2171 SCpnt =st_do_scsi(NULL, STp, cmd, datalen, timeout, MAX_RETRIES);
2172 if(!SCpnt)
2173 return(-EBUSY);
2175 ioctl_result = (STp->buffer)->last_result_fatal;
2177 SCpnt->request.rq_status = RQ_INACTIVE;/* Mark as not busy */
2179 if(!ioctl_result) {/* SCSI command successful */
2180 STps->drv_block = blkno;
2181 STps->drv_file = fileno;
2182 STps->at_sm = at_sm;
2184 if(cmd_in == MTLOCK)
2185 STp->door_locked = ST_LOCKED_EXPLICIT;
2186 else if(cmd_in == MTUNLOCK)
2187 STp->door_locked = ST_UNLOCKED;
2189 if(cmd_in == MTBSFM)
2190 ioctl_result =st_int_ioctl(inode, MTFSF,1);
2191 else if(cmd_in == MTFSFM)
2192 ioctl_result =st_int_ioctl(inode, MTBSF,1);
2194 if(cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) {
2195 STp->block_size = arg & MT_ST_BLKSIZE_MASK;
2196 if(STp->block_size !=0)
2197 (STp->buffer)->buffer_blocks =
2198 (STp->buffer)->buffer_size / STp->block_size;
2199 (STp->buffer)->buffer_bytes = (STp->buffer)->read_pointer =0;
2200 if(cmd_in == SET_DENS_AND_BLK)
2201 STp->density = arg >> MT_ST_DENSITY_SHIFT;
2203 else if(cmd_in == MTSETDRVBUFFER)
2204 STp->drv_buffer = (arg &7);
2205 else if(cmd_in == MTSETDENSITY)
2206 STp->density = arg;
2208 if(cmd_in == MTEOM)
2209 STps->eof = ST_EOD;
2210 else if(cmd_in == MTFSF)
2211 STps->eof = ST_FM;
2212 else if(chg_eof)
2213 STps->eof = ST_NOEOF;
2216 if(cmd_in == MTOFFL || cmd_in == MTUNLOAD)
2217 STp->rew_at_close =0;
2218 else if(cmd_in == MTLOAD) {
2219 STp->rew_at_close = (MINOR(inode->i_rdev) &0x80) ==0;
2220 for(i=0; i < ST_NBR_PARTITIONS; i++) {
2221 STp->ps[i].rw = ST_IDLE;
2222 STp->ps[i].last_block_valid = FALSE;
2224 STp->partition =0;
2227 }else{/* SCSI command was not completely successful */
2229 if(SCpnt->sense_buffer[2] &0x40) {
2230 if(cmd_in != MTBSF && cmd_in != MTBSFM &&
2231 cmd_in != MTBSR && cmd_in != MTBSS)
2232 STps->eof = ST_EOM_OK;
2233 STps->drv_block =0;
2236 undone = (
2237 (SCpnt->sense_buffer[3] <<24) +
2238 (SCpnt->sense_buffer[4] <<16) +
2239 (SCpnt->sense_buffer[5] <<8) +
2240 SCpnt->sense_buffer[6] );
2241 if(cmd_in == MTWEOF &&
2242 (SCpnt->sense_buffer[0] &0x70) ==0x70&&
2243 (SCpnt->sense_buffer[2] &0x4f) ==0x40&&
2244 ((SCpnt->sense_buffer[0] &0x80) ==0|| undone ==0)) {
2245 ioctl_result =0;/* EOF written succesfully at EOM */
2246 if(fileno >=0)
2247 fileno++;
2248 STps->drv_file = fileno;
2249 STps->eof = ST_NOEOF;
2251 else if( (cmd_in == MTFSF) || (cmd_in == MTFSFM) ) {
2252 if(fileno >=0)
2253 STps->drv_file = fileno - undone ;
2254 else
2255 STps->drv_file = fileno;
2256 STps->drv_block =0;
2257 STps->eof = ST_NOEOF;
2259 else if( (cmd_in == MTBSF) || (cmd_in == MTBSFM) ) {
2260 if(fileno >=0)
2261 STps->drv_file = fileno + undone ;
2262 else
2263 STps->drv_file = fileno;
2264 STps->drv_block =0;
2265 STps->eof = ST_NOEOF;
2267 else if(cmd_in == MTFSR) {
2268 if(SCpnt->sense_buffer[2] &0x80) {/* Hit filemark */
2269 if(STps->drv_file >=0)
2270 STps->drv_file++;
2271 STps->drv_block =0;
2272 STps->eof = ST_FM;
2274 else{
2275 if(blkno >= undone)
2276 STps->drv_block = blkno - undone;
2277 else
2278 STps->drv_block = (-1);
2279 STps->eof = ST_NOEOF;
2282 else if(cmd_in == MTBSR) {
2283 if(SCpnt->sense_buffer[2] &0x80) {/* Hit filemark */
2284 STps->drv_file--;
2285 STps->drv_block = (-1);
2287 else{
2288 if(blkno >=0)
2289 STps->drv_block = blkno + undone;
2290 else
2291 STps->drv_block = (-1);
2293 STps->eof = ST_NOEOF;
2295 else if(cmd_in == MTEOM) {
2296 STps->drv_file = (-1);
2297 STps->drv_block = (-1);
2298 STps->eof = ST_EOD;
2300 else if(chg_eof)
2301 STps->eof = ST_NOEOF;
2303 if((SCpnt->sense_buffer[2] &0x0f) == BLANK_CHECK)
2304 STps->eof = ST_EOD;
2306 if(cmd_in == MTLOCK)
2307 STp->door_locked = ST_LOCK_FAILS;
2310 return ioctl_result;
2314 \f/* Get the tape position. If bt == 2, arg points into a kernel space mt_loc
2315 structure. */
2317 static int
2318 get_location(struct inode * inode,unsigned int*block,int*partition,
2319 int logical)
2321 Scsi_Tape *STp;
2322 int dev =TAPE_NR(inode->i_rdev);
2323 int result;
2324 unsigned char scmd[10];
2325 Scsi_Cmnd *SCpnt;
2327 STp = &(scsi_tapes[dev]);
2328 if(STp->ready != ST_READY)
2329 return(-EIO);
2331 memset(scmd,0,10);
2332 if((STp->device)->scsi_level < SCSI_2) {
2333 scmd[0] = QFA_REQUEST_BLOCK;
2334 scmd[4] =3;
2336 else{
2337 scmd[0] = READ_POSITION;
2338 if(!logical && !STp->scsi2_logical)
2339 scmd[1] =1;
2341 SCpnt =st_do_scsi(NULL, STp, scmd,20, ST_TIMEOUT, MAX_READY_RETRIES);
2342 if(!SCpnt)
2343 return(-EBUSY);
2345 if((STp->buffer)->last_result_fatal !=0||
2346 (STp->device->scsi_level >= SCSI_2 &&
2347 ((STp->buffer)->b_data[0] &4) !=0)) {
2348 *block = *partition =0;
2349 #if DEBUG
2350 if(debugging)
2351 printk(ST_DEB_MSG "st%d: Can't read tape position.\n", dev);
2352 #endif
2353 result = (-EIO);
2355 else{
2356 result =0;
2357 if((STp->device)->scsi_level < SCSI_2) {
2358 *block = ((STp->buffer)->b_data[0] <<16)
2359 + ((STp->buffer)->b_data[1] <<8)
2360 + (STp->buffer)->b_data[2];
2361 *partition =0;
2363 else{
2364 *block = ((STp->buffer)->b_data[4] <<24)
2365 + ((STp->buffer)->b_data[5] <<16)
2366 + ((STp->buffer)->b_data[6] <<8)
2367 + (STp->buffer)->b_data[7];
2368 *partition = (STp->buffer)->b_data[1];
2369 if(((STp->buffer)->b_data[0] &0x80) &&
2370 (STp->buffer)->b_data[1] ==0)/* BOP of partition 0 */
2371 STp->ps[0].drv_block = STp->ps[0].drv_file =0;
2373 #if DEBUG
2374 if(debugging)
2375 printk(ST_DEB_MSG "st%d: Got tape pos. blk %d part %d.\n", dev,
2376 *block, *partition);
2377 #endif
2380 SCpnt->request.rq_status = RQ_INACTIVE;/* Mark as not busy */
2382 return result;
2386 /* Set the tape block and partition. Negative partition means that only the
2387 block should be set in vendor specific way. */
2388 static int
2389 set_location(struct inode * inode,unsigned int block,int partition,
2390 int logical)
2392 Scsi_Tape *STp;
2393 ST_partstat *STps;
2394 int dev =TAPE_NR(inode->i_rdev);
2395 int result, p;
2396 unsigned int blk;
2397 int timeout = ST_LONG_TIMEOUT;
2398 unsigned char scmd[10];
2399 Scsi_Cmnd *SCpnt;
2401 STp = &(scsi_tapes[dev]);
2402 if(STp->ready != ST_READY)
2403 return(-EIO);
2404 STps = &(STp->ps[STp->partition]);
2406 #if DEBUG
2407 if(debugging)
2408 printk(ST_DEB_MSG "st%d: Setting block to %d and partition to %d.\n",
2409 dev, block, partition);
2410 if(partition <0)
2411 return(-EIO);
2412 #endif
2414 /* Update the location at the partition we are leaving */
2415 if((!STp->can_partitions && partition !=0) ||
2416 partition >= ST_NBR_PARTITIONS)
2417 return(-EINVAL);
2418 if(partition != STp->partition) {
2419 if(get_location(inode, &blk, &p,1))
2420 STps->last_block_valid = FALSE;
2421 else{
2422 STps->last_block_valid = TRUE;
2423 STps->last_block_visited = blk;
2424 #if DEBUG
2425 if(debugging)
2426 printk(ST_DEB_MSG "st%d: Visited block %d for partition %d saved.\n",
2427 dev, blk, STp->partition);
2428 #endif
2432 memset(scmd,0,10);
2433 if((STp->device)->scsi_level < SCSI_2) {
2434 scmd[0] = QFA_SEEK_BLOCK;
2435 scmd[2] = (block >>16);
2436 scmd[3] = (block >>8);
2437 scmd[4] = block;
2438 scmd[5] =0;
2440 else{
2441 scmd[0] = SEEK_10;
2442 scmd[3] = (block >>24);
2443 scmd[4] = (block >>16);
2444 scmd[5] = (block >>8);
2445 scmd[6] = block;
2446 if(!logical && !STp->scsi2_logical)
2447 scmd[1] =4;
2448 if(STp->partition != partition) {
2449 scmd[1] |=2;
2450 scmd[8] = partition;
2451 #if DEBUG
2452 if(debugging)
2453 printk(ST_DEB_MSG "st%d: Trying to change partition from %d to %d\n",
2454 dev, STp->partition, partition);
2455 #endif
2458 #if ST_NOWAIT
2459 scmd[1] |=1;/* Don't wait for completion */
2460 timeout = ST_TIMEOUT;
2461 #endif
2463 SCpnt =st_do_scsi(NULL, STp, scmd,20, timeout, MAX_READY_RETRIES);
2464 if(!SCpnt)
2465 return(-EBUSY);
2467 STps->drv_block = STps->drv_file = (-1);
2468 STps->eof = ST_NOEOF;
2469 if((STp->buffer)->last_result_fatal !=0) {
2470 result = (-EIO);
2471 if(STp->can_partitions &&
2472 (STp->device)->scsi_level >= SCSI_2 &&
2473 (p =find_partition(inode)) >=0)
2474 STp->partition = p;
2476 else{
2477 if(STp->can_partitions) {
2478 STp->partition = partition;
2479 STps = &(STp->ps[partition]);
2480 if(!STps->last_block_valid ||
2481 STps->last_block_visited != block) {
2482 STps->at_sm =0;
2483 STps->rw = ST_IDLE;
2486 else
2487 STps->at_sm =0;
2488 if(block ==0)
2489 STps->drv_block = STps->drv_file =0;
2490 result =0;
2492 SCpnt->request.rq_status = RQ_INACTIVE;/* Mark as not busy */
2494 return result;
2498 /* Find the current partition number for the drive status. Called from open and
2499 returns either partition number of negative error code. */
2500 static int
2501 find_partition(struct inode *inode)
2503 int i, partition;
2504 unsigned int block;
2506 if((i =get_location(inode, &block, &partition,1)) <0)
2507 return i;
2508 if(partition >= ST_NBR_PARTITIONS)
2509 return(-EIO);
2510 return partition;
2514 /* Change the partition if necessary */
2515 static int
2516 update_partition(struct inode * inode)
2518 int dev =TAPE_NR(inode->i_rdev);
2519 Scsi_Tape *STp;
2520 ST_partstat *STps;
2522 STp = &(scsi_tapes[dev]);
2523 if(STp->partition == STp->new_partition)
2524 return0;
2525 STps = &(STp->ps[STp->new_partition]);
2526 if(!STps->last_block_valid)
2527 STps->last_block_visited =0;
2528 returnset_location(inode, STps->last_block_visited, STp->new_partition,1);
2531 \f/* Functions for reading and writing the medium partition mode page. These
2532 seem to work with Wangtek 6200HS and HP C1533A. */
2534 #define PART_PAGE 0x11
2535 #define PART_PAGE_LENGTH 10
2537 /* Get the number of partitions on the tape. As a side effect reads the
2538 mode page into the tape buffer. */
2539 static int
2540 nbr_partitions(struct inode * inode)
2542 int dev =TAPE_NR(inode->i_rdev), result;
2543 Scsi_Tape *STp;
2544 Scsi_Cmnd * SCpnt = NULL;
2545 unsigned char cmd[10];
2547 STp = &(scsi_tapes[dev]);
2548 if(STp->ready != ST_READY)
2549 return(-EIO);
2551 memset((void*) &cmd[0],0,10);
2552 cmd[0] = MODE_SENSE;
2553 cmd[1] =8;/* Page format */
2554 cmd[2] = PART_PAGE;
2555 cmd[4] =200;
2557 SCpnt =st_do_scsi(SCpnt, STp, cmd,200, ST_TIMEOUT, MAX_READY_RETRIES);
2558 if(SCpnt == NULL)
2559 return(-EBUSY);
2560 SCpnt->request.rq_status = RQ_INACTIVE;/* Mark as not busy */
2562 if((STp->buffer)->last_result_fatal !=0) {
2563 #if DEBUG
2564 if(debugging)
2565 printk(ST_DEB_MSG "st%d: Can't read medium partition page.\n", dev);
2566 #endif
2567 result = (-EIO);
2569 else{
2570 result = (STp->buffer)->b_data[MODE_HEADER_LENGTH +3] +1;
2571 #if DEBUG
2572 if(debugging)
2573 printk(ST_DEB_MSG "st%d: Number of partitions %d.\n", dev, result);
2574 #endif
2577 return result;
2581 /* Partition the tape into two partitions if size > 0 or one partition if
2582 size == 0 */
2583 static int
2584 partition_tape(struct inode * inode,int size)
2586 int dev =TAPE_NR(inode->i_rdev), result;
2587 int length;
2588 Scsi_Tape *STp;
2589 Scsi_Cmnd * SCpnt = NULL;
2590 unsigned char cmd[10], *bp;
2592 if((result =nbr_partitions(inode)) <0)
2593 return result;
2594 STp = &(scsi_tapes[dev]);
2596 /* The mode page is in the buffer. Let's modify it and write it. */
2597 bp = &((STp->buffer)->b_data[0]);
2598 if(size <=0) {
2599 length =8;
2600 bp[MODE_HEADER_LENGTH +3] =0;
2601 #if DEBUG
2602 if(debugging)
2603 printk(ST_DEB_MSG "st%d: Formatting tape with one partition.\n", dev);
2604 #endif
2606 else{
2607 length =10;
2608 bp[MODE_HEADER_LENGTH +3] =1;
2609 bp[MODE_HEADER_LENGTH +8] = (size >>8) &0xff;
2610 bp[MODE_HEADER_LENGTH +9] = size &0xff;
2611 #if DEBUG
2612 if(debugging)
2613 printk(ST_DEB_MSG "st%d: Formatting tape with two partition (1 = %d MB).\n",
2614 dev, size);
2615 #endif
2617 bp[MODE_HEADER_LENGTH +6] =0;
2618 bp[MODE_HEADER_LENGTH +7] =0;
2619 bp[MODE_HEADER_LENGTH +4] =0x30;/* IDP | PSUM = MB */
2621 bp[0] =0;
2622 bp[1] =0;
2623 bp[MODE_HEADER_LENGTH] &=0x3f;
2624 bp[MODE_HEADER_LENGTH +1] = length -2;
2626 memset(cmd,0,10);
2627 cmd[0] = MODE_SELECT;
2628 cmd[1] =0x10;
2629 cmd[4] = length + MODE_HEADER_LENGTH;
2631 SCpnt =st_do_scsi(SCpnt, STp, cmd, cmd[4], ST_LONG_TIMEOUT, MAX_READY_RETRIES);
2632 if(SCpnt == NULL)
2633 return(-EBUSY);
2634 SCpnt->request.rq_status = RQ_INACTIVE;/* Mark as not busy */
2636 if((STp->buffer)->last_result_fatal !=0) {
2637 printk(KERN_INFO "st%d: Partitioning of tape failed.\n", dev);
2638 result = (-EIO);
2640 else
2641 result =0;
2643 return result;
2648 /* The ioctl command */
2649 static int
2650 st_ioctl(struct inode * inode,struct file * file,
2651 unsigned int cmd_in,unsigned long arg)
2653 int i, cmd_nr, cmd_type, bt;
2654 unsigned int blk;
2655 struct mtop mtc;
2656 struct mtpos mt_pos;
2657 Scsi_Tape *STp;
2658 ST_mode *STm;
2659 ST_partstat *STps;
2660 int dev =TAPE_NR(inode->i_rdev);
2662 STp = &(scsi_tapes[dev]);
2663 #if DEBUG
2664 if(debugging && !STp->in_use) {
2665 printk(ST_DEB_MSG "st%d: Incorrect device.\n", dev);
2666 return(-EIO);
2668 #endif
2669 STm = &(STp->modes[STp->current_mode]);
2670 STps = &(STp->ps[STp->partition]);
2672 cmd_type =_IOC_TYPE(cmd_in);
2673 cmd_nr =_IOC_NR(cmd_in);
2675 if(cmd_type ==_IOC_TYPE(MTIOCTOP) && cmd_nr ==_IOC_NR(MTIOCTOP)) {
2676 if(_IOC_SIZE(cmd_in) !=sizeof(mtc))
2677 return(-EINVAL);
2679 i =copy_from_user((char*) &mtc, (char*)arg,sizeof(struct mtop));
2680 if(i)
2681 return(-EFAULT);
2683 if(mtc.mt_op == MTSETDRVBUFFER && !suser()) {
2684 printk(KERN_WARNING "st%d: MTSETDRVBUFFER only allowed for root.\n", dev);
2685 return(-EPERM);
2687 if(!STm->defined &&
2688 (mtc.mt_op != MTSETDRVBUFFER && (mtc.mt_count & MT_ST_OPTIONS) ==0))
2689 return(-ENXIO);
2691 if(!(STp->device)->was_reset) {
2693 if(STps->eof == ST_FM_HIT) {
2694 if(mtc.mt_op == MTFSF || mtc.mt_op == MTFSFM|| mtc.mt_op == MTEOM) {
2695 mtc.mt_count -=1;
2696 if(STps->drv_file >=0)
2697 STps->drv_file +=1;
2699 else if(mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM) {
2700 mtc.mt_count +=1;
2701 if(STps->drv_file >=0)
2702 STps->drv_file +=1;
2706 i =flush_buffer(inode, file,/* mtc.mt_op == MTSEEK || */
2707 mtc.mt_op == MTREW || mtc.mt_op == MTOFFL ||
2708 mtc.mt_op == MTRETEN || mtc.mt_op == MTEOM ||
2709 mtc.mt_op == MTLOCK || mtc.mt_op == MTLOAD ||
2710 mtc.mt_op == MTCOMPRESSION);
2711 if(i <0)
2712 return i;
2714 else{
2716 * If there was a bus reset, block further access
2717 * to this device. If the user wants to rewind the tape,
2718 * then reset the flag and allow access again.
2720 if(mtc.mt_op != MTREW &&
2721 mtc.mt_op != MTOFFL &&
2722 mtc.mt_op != MTRETEN &&
2723 mtc.mt_op != MTERASE &&
2724 mtc.mt_op != MTSEEK &&
2725 mtc.mt_op != MTEOM)
2726 return(-EIO);
2727 STp->device->was_reset =0;
2728 if(STp->door_locked != ST_UNLOCKED &&
2729 STp->door_locked != ST_LOCK_FAILS) {
2730 if(st_int_ioctl(inode, MTLOCK,0)) {
2731 printk(KERN_NOTICE "st%d: Could not relock door after bus reset.\n",
2732 dev);
2733 STp->door_locked = ST_UNLOCKED;
2738 if(mtc.mt_op != MTNOP && mtc.mt_op != MTSETBLK &&
2739 mtc.mt_op != MTSETDENSITY && mtc.mt_op != MTWSM &&
2740 mtc.mt_op != MTSETDRVBUFFER && mtc.mt_op != MTSETPART)
2741 STps->rw = ST_IDLE;/* Prevent automatic WEOF and fsf */
2743 if(mtc.mt_op == MTOFFL && STp->door_locked != ST_UNLOCKED)
2744 st_int_ioctl(inode, MTUNLOCK,0);/* Ignore result! */
2746 if(mtc.mt_op == MTSETDRVBUFFER &&
2747 (mtc.mt_count & MT_ST_OPTIONS) !=0)
2748 returnst_set_options(inode, mtc.mt_count);
2749 if(mtc.mt_op == MTSETPART) {
2750 if(!STp->can_partitions ||
2751 mtc.mt_count <0|| mtc.mt_count >= ST_NBR_PARTITIONS)
2752 return(-EINVAL);
2753 if(mtc.mt_count >= STp->nbr_partitions &&
2754 (STp->nbr_partitions =nbr_partitions(inode)) <0)
2755 return(-EIO);
2756 if(mtc.mt_count >= STp->nbr_partitions)
2757 return(-EINVAL);
2758 STp->new_partition = mtc.mt_count;
2759 return0;
2761 if(mtc.mt_op == MTMKPART) {
2762 if(!STp->can_partitions)
2763 return(-EINVAL);
2764 if((i =st_int_ioctl(inode, MTREW,0)) <0||
2765 (i =partition_tape(inode, mtc.mt_count)) <0)
2766 return i;
2767 for(i=0; i < ST_NBR_PARTITIONS; i++) {
2768 STp->ps[i].rw = ST_IDLE;
2769 STp->ps[i].at_sm =0;
2770 STp->ps[i].last_block_valid = FALSE;
2772 STp->partition = STp->new_partition =0;
2773 STp->nbr_partitions =1;/* Bad guess ?-) */
2774 STps->drv_block = STps->drv_file =0;
2775 return0;
2777 if(mtc.mt_op == MTSEEK) {
2778 i =set_location(inode, mtc.mt_count, STp->new_partition,0);
2779 if(!STp->can_partitions)
2780 STp->ps[0].rw = ST_IDLE;
2781 return i;
2783 if(STp->can_partitions && STp->ready == ST_READY &&
2784 (i =update_partition(inode)) <0)
2785 return i;
2786 if(mtc.mt_op == MTCOMPRESSION)
2787 returnst_compression(STp, (mtc.mt_count &1));
2788 else
2789 returnst_int_ioctl(inode, mtc.mt_op, mtc.mt_count);
2792 if(!STm->defined)
2793 return(-ENXIO);
2795 if((i =flush_buffer(inode, file, FALSE)) <0)
2796 return i;
2797 if(STp->can_partitions &&
2798 (i =update_partition(inode)) <0)
2799 return i;
2801 if(cmd_type ==_IOC_TYPE(MTIOCGET) && cmd_nr ==_IOC_NR(MTIOCGET)) {
2803 if(_IOC_SIZE(cmd_in) !=sizeof(struct mtget))
2804 return(-EINVAL);
2806 (STp->mt_status)->mt_dsreg =
2807 ((STp->block_size << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK) |
2808 ((STp->density << MT_ST_DENSITY_SHIFT) & MT_ST_DENSITY_MASK);
2809 (STp->mt_status)->mt_blkno = STps->drv_block;
2810 (STp->mt_status)->mt_fileno = STps->drv_file;
2811 if(STp->block_size !=0) {
2812 if(STps->rw == ST_WRITING)
2813 (STp->mt_status)->mt_blkno +=
2814 (STp->buffer)->buffer_bytes / STp->block_size;
2815 else if(STps->rw == ST_READING)
2816 (STp->mt_status)->mt_blkno -= ((STp->buffer)->buffer_bytes +
2817 STp->block_size -1) / STp->block_size;
2820 (STp->mt_status)->mt_gstat =0;
2821 if(STp->drv_write_prot)
2822 (STp->mt_status)->mt_gstat |=GMT_WR_PROT(0xffffffff);
2823 if((STp->mt_status)->mt_blkno ==0) {
2824 if((STp->mt_status)->mt_fileno ==0)
2825 (STp->mt_status)->mt_gstat |=GMT_BOT(0xffffffff);
2826 else
2827 (STp->mt_status)->mt_gstat |=GMT_EOF(0xffffffff);
2829 (STp->mt_status)->mt_resid = STp->partition;
2830 if(STps->eof == ST_EOM_OK || STps->eof == ST_EOM_ERROR)
2831 (STp->mt_status)->mt_gstat |=GMT_EOT(0xffffffff);
2832 else if(STps->eof >= ST_EOM_OK)
2833 (STp->mt_status)->mt_gstat |=GMT_EOD(0xffffffff);
2834 if(STp->density ==1)
2835 (STp->mt_status)->mt_gstat |=GMT_D_800(0xffffffff);
2836 else if(STp->density ==2)
2837 (STp->mt_status)->mt_gstat |=GMT_D_1600(0xffffffff);
2838 else if(STp->density ==3)
2839 (STp->mt_status)->mt_gstat |=GMT_D_6250(0xffffffff);
2840 if(STp->ready == ST_READY)
2841 (STp->mt_status)->mt_gstat |=GMT_ONLINE(0xffffffff);
2842 if(STp->ready == ST_NO_TAPE)
2843 (STp->mt_status)->mt_gstat |=GMT_DR_OPEN(0xffffffff);
2844 if(STps->at_sm)
2845 (STp->mt_status)->mt_gstat |=GMT_SM(0xffffffff);
2846 if(STm->do_async_writes || (STm->do_buffer_writes && STp->block_size !=0) ||
2847 STp->drv_buffer !=0)
2848 (STp->mt_status)->mt_gstat |=GMT_IM_REP_EN(0xffffffff);
2850 i =copy_to_user((char*)arg, (char*)(STp->mt_status),
2851 sizeof(struct mtget));
2852 if(i)
2853 return(-EFAULT);
2855 (STp->mt_status)->mt_erreg =0;/* Clear after read */
2856 return0;
2857 }/* End of MTIOCGET */
2859 if(cmd_type ==_IOC_TYPE(MTIOCPOS) && cmd_nr ==_IOC_NR(MTIOCPOS)) {
2860 if(_IOC_SIZE(cmd_in) !=sizeof(struct mtpos))
2861 return(-EINVAL);
2862 if((i =get_location(inode, &blk, &bt,0)) <0)
2863 return i;
2864 mt_pos.mt_blkno = blk;
2865 i =copy_to_user((char*)arg, (char*) (&mt_pos),sizeof(struct mtpos));
2866 if(i)
2867 return(-EFAULT);
2868 return0;
2871 returnscsi_ioctl(STp->device, cmd_in, (void*) arg);
2875 /* Try to allocate a new tape buffer */
2876 static ST_buffer *
2877 new_tape_buffer(int from_initialization,int need_dma )
2879 int priority, a_size;
2880 ST_buffer *tb;
2882 if(st_nbr_buffers >= st_template.dev_max)
2883 return NULL;/* Should never happen */
2885 if(from_initialization) {
2886 priority = GFP_ATOMIC;
2887 a_size = st_buffer_size;
2889 else{
2890 priority = GFP_KERNEL;
2891 for(a_size = PAGE_SIZE; a_size < st_buffer_size; a_size <<=1)
2892 ;/* Make sure we allocate efficiently */
2894 tb = (ST_buffer *)scsi_init_malloc(sizeof(ST_buffer), priority);
2895 if(tb) {
2896 if(need_dma)
2897 priority |= GFP_DMA;
2898 tb->b_data = (unsigned char*)scsi_init_malloc(a_size, priority);
2899 if(!tb->b_data) {
2900 scsi_init_free((char*)tb,sizeof(ST_buffer));
2901 tb = NULL;
2904 if(!tb) {
2905 printk(KERN_NOTICE "st: Can't allocate new tape buffer (nbr %d).\n",
2906 st_nbr_buffers);
2907 return NULL;
2909 #if DEBUG
2910 if(debugging)
2911 printk(ST_DEB_MSG
2912 "st: Allocated tape buffer %d (%d bytes, dma: %d, a: %p).\n",
2913 st_nbr_buffers, a_size, need_dma, tb->b_data);
2914 #endif
2915 tb->in_use =0;
2916 tb->dma = need_dma;
2917 tb->buffer_size = a_size;
2918 tb->writing =0;
2919 tb->orig_b_data = NULL;
2920 st_buffers[st_nbr_buffers++] = tb;
2921 return tb;
2925 /* Try to allocate a temporary enlarged tape buffer */
2926 static int
2927 enlarge_buffer(ST_buffer *STbuffer,int new_size,int need_dma)
2929 int a_size, priority;
2930 unsigned char*tbd;
2932 normalize_buffer(STbuffer);
2934 for(a_size = PAGE_SIZE; a_size < new_size; a_size <<=1)
2935 ;/* Make sure that we allocate efficiently */
2937 priority = GFP_KERNEL;
2938 if(need_dma)
2939 priority |= GFP_DMA;
2940 tbd = (unsigned char*)scsi_init_malloc(a_size, priority);
2941 if(!tbd)
2942 return FALSE;
2943 #if DEBUG
2944 if(debugging)
2945 printk(ST_DEB_MSG
2946 "st: Buffer at %p enlarged to %d bytes (dma: %d, a: %p).\n",
2947 STbuffer->b_data, a_size, need_dma, tbd);
2948 #endif
2950 STbuffer->orig_b_data = STbuffer->b_data;
2951 STbuffer->orig_size = STbuffer->buffer_size;
2952 STbuffer->b_data = tbd;
2953 STbuffer->buffer_size = a_size;
2954 return TRUE;
2958 /* Release the extra buffer */
2959 static void
2960 normalize_buffer(ST_buffer *STbuffer)
2962 if(STbuffer->orig_b_data == NULL)
2963 return;
2965 scsi_init_free(STbuffer->b_data, STbuffer->buffer_size);
2966 STbuffer->b_data = STbuffer->orig_b_data;
2967 STbuffer->orig_b_data = NULL;
2968 STbuffer->buffer_size = STbuffer->orig_size;
2970 #if DEBUG
2971 if(debugging)
2972 printk(ST_DEB_MSG "st: Buffer at %p normalized to %d bytes.\n",
2973 STbuffer->b_data, STbuffer->buffer_size);
2974 #endif
2978 #ifndef MODULE
2979 /* Set the boot options. Syntax: st=xxx,yyy
2980 where xxx is buffer size in 1024 byte blocks and yyy is write threshold
2981 in 1024 byte blocks. */
2982 void
2983 st_setup(char*str,int*ints)
2985 if(ints[0] >0&& ints[1] >0)
2986 st_buffer_size = ints[1] * ST_BLOCK_SIZE;
2987 if(ints[0] >1&& ints[2] >0) {
2988 st_write_threshold = ints[2] * ST_BLOCK_SIZE;
2989 if(st_write_threshold > st_buffer_size)
2990 st_write_threshold = st_buffer_size;
2992 if(ints[0] >2&& ints[3] >0)
2993 st_max_buffers = ints[3];
2995 #endif
2998 static struct file_operations st_fops = {
2999 NULL,/* lseek - default */
3000 st_read,/* read - general block-dev read */
3001 st_write,/* write - general block-dev write */
3002 NULL,/* readdir - bad */
3003 NULL,/* select */
3004 st_ioctl,/* ioctl */
3005 NULL,/* mmap */
3006 scsi_tape_open,/* open */
3007 scsi_tape_close,/* release */
3008 NULL /* fsync */
3011 static intst_attach(Scsi_Device * SDp){
3012 Scsi_Tape * tpnt;
3013 ST_mode * STm;
3014 ST_partstat * STps;
3015 int i;
3017 if(SDp->type != TYPE_TAPE)return1;
3019 if(st_template.nr_dev >= st_template.dev_max)
3021 SDp->attached--;
3022 return1;
3025 for(tpnt = scsi_tapes, i=0; i<st_template.dev_max; i++, tpnt++)
3026 if(!tpnt->device)break;
3028 if(i >= st_template.dev_max)panic("scsi_devices corrupt (st)");
3030 scsi_tapes[i].device = SDp;
3031 if(SDp->scsi_level <=2)
3032 scsi_tapes[i].mt_status->mt_type = MT_ISSCSI1;
3033 else
3034 scsi_tapes[i].mt_status->mt_type = MT_ISSCSI2;
3036 tpnt->devt =MKDEV(SCSI_TAPE_MAJOR, i);
3037 tpnt->dirty =0;
3038 tpnt->waiting = NULL;
3039 tpnt->in_use =0;
3040 tpnt->drv_buffer =1;/* Try buffering if no mode sense */
3041 tpnt->restr_dma = (SDp->host)->unchecked_isa_dma;
3042 tpnt->density =0;
3043 tpnt->do_auto_lock = ST_AUTO_LOCK;
3044 tpnt->can_bsr = ST_IN_FILE_POS;
3045 tpnt->can_partitions =0;
3046 tpnt->two_fm = ST_TWO_FM;
3047 tpnt->fast_mteom = ST_FAST_MTEOM;
3048 tpnt->scsi2_logical =0;
3049 tpnt->write_threshold = st_write_threshold;
3050 tpnt->default_drvbuffer =0xff;/* No forced buffering */
3051 tpnt->partition =0;
3052 tpnt->new_partition =0;
3053 tpnt->nbr_partitions =0;
3055 for(i=0; i < ST_NBR_MODES; i++) {
3056 STm = &(tpnt->modes[i]);
3057 STm->defined = FALSE;
3058 STm->sysv = ST_SYSV;
3059 STm->defaults_for_writes =0;
3060 STm->do_async_writes = ST_ASYNC_WRITES;
3061 STm->do_buffer_writes = ST_BUFFER_WRITES;
3062 STm->do_read_ahead = ST_READ_AHEAD;
3063 STm->default_compression = ST_DONT_TOUCH;
3064 STm->default_blksize = (-1);/* No forced size */
3065 STm->default_density = (-1);/* No forced density */
3068 for(i=0; i < ST_NBR_PARTITIONS; i++) {
3069 STps = &(tpnt->ps[i]);
3070 STps->rw = ST_IDLE;
3071 STps->eof = ST_NOEOF;
3072 STps->at_sm =0;
3073 STps->last_block_valid = FALSE;
3074 STps->drv_block =0;
3075 STps->drv_file =0;
3078 tpnt->current_mode =0;
3079 tpnt->modes[0].defined = TRUE;
3081 tpnt->density_changed = tpnt->compression_changed =
3082 tpnt->blksize_changed = FALSE;
3084 st_template.nr_dev++;
3085 return0;
3088 static intst_detect(Scsi_Device * SDp)
3090 if(SDp->type != TYPE_TAPE)return0;
3092 printk(KERN_INFO
3093 "Detected scsi tape st%d at scsi%d, channel %d, id %d, lun %d\n",
3094 st_template.dev_noticed++,
3095 SDp->host->host_no, SDp->channel, SDp->id, SDp->lun);
3097 return1;
3100 static int st_registered =0;
3102 /* Driver initialization */
3103 static intst_init()
3105 int i;
3106 Scsi_Tape * STp;
3107 #if !ST_RUNTIME_BUFFERS
3108 int target_nbr;
3109 #endif
3111 if(st_template.dev_noticed ==0)return0;
3113 if(!st_registered) {
3114 if(register_chrdev(SCSI_TAPE_MAJOR,"st",&st_fops)) {
3115 printk(KERN_ERR "Unable to get major %d for SCSI tapes\n",MAJOR_NR);
3116 return1;
3118 st_registered++;
3121 if(scsi_tapes)return0;
3122 st_template.dev_max = st_template.dev_noticed + ST_EXTRA_DEVS;
3123 if(st_template.dev_max < ST_MAX_TAPES)
3124 st_template.dev_max = ST_MAX_TAPES;
3125 if(st_template.dev_max >128/ ST_NBR_MODES)
3126 printk(KERN_INFO "st: Only %d tapes accessible.\n",128/ ST_NBR_MODES);
3127 scsi_tapes =
3128 (Scsi_Tape *)scsi_init_malloc(st_template.dev_max *sizeof(Scsi_Tape),
3129 GFP_ATOMIC);
3130 if(scsi_tapes == NULL) {
3131 printk(KERN_ERR "Unable to allocate descriptors for SCSI tapes.\n");
3132 unregister_chrdev(SCSI_TAPE_MAJOR,"st");
3133 return1;
3136 #if DEBUG
3137 printk(ST_DEB_MSG "st: Buffer size %d bytes, write threshold %d bytes.\n",
3138 st_buffer_size, st_write_threshold);
3139 #endif
3141 memset(scsi_tapes,0, st_template.dev_max *sizeof(Scsi_Tape));
3142 for(i=0; i < st_template.dev_max; ++i) {
3143 STp = &(scsi_tapes[i]);
3144 STp->capacity =0xfffff;
3145 STp->mt_status = (struct mtget *)scsi_init_malloc(sizeof(struct mtget),
3146 GFP_ATOMIC);
3147 /* Initialize status */
3148 memset((void*) scsi_tapes[i].mt_status,0,sizeof(struct mtget));
3151 /* Allocate the buffers */
3152 st_buffers =
3153 (ST_buffer **)scsi_init_malloc(st_template.dev_max *sizeof(ST_buffer *),
3154 GFP_ATOMIC);
3155 if(st_buffers == NULL) {
3156 printk(KERN_ERR "Unable to allocate tape buffer pointers.\n");
3157 unregister_chrdev(SCSI_TAPE_MAJOR,"st");
3158 scsi_init_free((char*) scsi_tapes,
3159 st_template.dev_max *sizeof(Scsi_Tape));
3160 return1;
3163 #if ST_RUNTIME_BUFFERS
3164 st_nbr_buffers =0;
3165 #else
3166 target_nbr = st_template.dev_noticed;
3167 if(target_nbr < ST_EXTRA_DEVS)
3168 target_nbr = ST_EXTRA_DEVS;
3169 if(target_nbr > st_max_buffers)
3170 target_nbr = st_max_buffers;
3172 for(i=st_nbr_buffers=0; i < target_nbr; i++) {
3173 if(!new_tape_buffer(TRUE, TRUE)) {
3174 if(i ==0) {
3175 #if 0
3176 printk(KERN_ERR "Can't continue without at least one tape buffer.\n");
3177 unregister_chrdev(SCSI_TAPE_MAJOR,"st");
3178 scsi_init_free((char*) st_buffers,
3179 st_template.dev_max *sizeof(ST_buffer *));
3180 scsi_init_free((char*) scsi_tapes,
3181 st_template.dev_max *sizeof(Scsi_Tape));
3182 return1;
3183 #else
3184 printk(KERN_INFO "No tape buffers allocated at initialization.\n");
3185 break;
3186 #endif
3188 printk(KERN_INFO "Number of tape buffers adjusted.\n");
3189 break;
3192 #endif
3193 return0;
3196 static voidst_detach(Scsi_Device * SDp)
3198 Scsi_Tape * tpnt;
3199 int i;
3201 for(tpnt = scsi_tapes, i=0; i<st_template.dev_max; i++, tpnt++)
3202 if(tpnt->device == SDp) {
3203 tpnt->device = NULL;
3204 SDp->attached--;
3205 st_template.nr_dev--;
3206 st_template.dev_noticed--;
3207 return;
3209 return;
3213 #ifdef MODULE
3215 intinit_module(void) {
3216 st_template.module = &__this_module;
3217 returnscsi_register_module(MODULE_SCSI_DEV, &st_template);
3220 voidcleanup_module(void)
3222 int i;
3224 scsi_unregister_module(MODULE_SCSI_DEV, &st_template);
3225 unregister_chrdev(SCSI_TAPE_MAJOR,"st");
3226 st_registered--;
3227 if(scsi_tapes != NULL) {
3228 scsi_init_free((char*) scsi_tapes,
3229 st_template.dev_max *sizeof(Scsi_Tape));
3231 if(st_buffers != NULL) {
3232 for(i=0; i < st_nbr_buffers; i++)
3233 if(st_buffers[i] != NULL) {
3234 scsi_init_free((char*) st_buffers[i]->b_data,
3235 st_buffers[i]->buffer_size);
3236 scsi_init_free((char*) st_buffers[i],sizeof(ST_buffer));
3239 scsi_init_free((char*) st_buffers,
3240 st_template.dev_max *sizeof(ST_buffer *));
3243 st_template.dev_max =0;
3244 printk(KERN_INFO "st: Unloaded.\n");
3246 #endif/* MODULE */
close