root/drivers/scsi/st.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. st_chk_result
  2. st_sleep_done
  3. write_behind_check
  4. back_over_eof
  5. flush_write_buffer
  6. flush_buffer
  7. scsi_tape_open
  8. scsi_tape_close
  9. st_write
  10. st_read
  11. st_set_options
  12. st_int_ioctl
  13. st_ioctl
  14. st_setup
  15. st_attach
  16. st_detect
  17. st_init
  18. st_detach
  19. init_module
  20. cleanup_module

   1 /*
   2   SCSI Tape Driver for Linux version 1.1 and newer. See the accompanying
   3   file README.st for more information.
   4 
   5   History:
   6   Rewritten from Dwayne Forsyth's SCSI tape driver by Kai Makisara.
   7   Contribution and ideas from several people including (in alphabetical
   8   order) Klaus Ehrenfried, Steve Hirsch, Wolfgang Denk, Andreas Koppenh"ofer,
   9   J"org Weule, and Eric Youngdale.
  10 
  11   Copyright 1992, 1993, 1994, 1995 Kai Makisara
  12                  email Kai.Makisara@metla.fi
  13 
  14   Last modified: Sat Feb 18 10:51:25 1995 by root@kai.home
  15 */
  16 #ifdef MODULE
  17 #include <linux/module.h>
  18 #include <linux/version.h>
  19 #endif /* MODULE */
  20 
  21 #include <linux/fs.h>
  22 #include <linux/kernel.h>
  23 #include <linux/sched.h>
  24 #include <linux/mm.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/segment.h>
  31 #include <asm/system.h>
  32 
  33 #define MAJOR_NR SCSI_TAPE_MAJOR
  34 #include "../block/blk.h"
  35 #include "scsi.h"
  36 #include "hosts.h"
  37 #include "scsi_ioctl.h"
  38 #include "st.h"
  39 #include "constants.h"
  40 
  41 /* #define DEBUG */
  42 
  43 /* #define ST_NOWAIT */
  44 
  45 /* #define ST_IN_FILE_POS */
  46 
  47 /* #define ST_RECOVERED_WRITE_FATAL */
  48 
  49 #define ST_TWO_FM 0
  50 
  51 #define ST_FAST_MTEOM 0
  52 
  53 #define ST_BUFFER_WRITES 1
  54 
  55 #define ST_ASYNC_WRITES 1
  56 
  57 #define ST_READ_AHEAD 1
  58 
  59 #define ST_BLOCK_SIZE 1024
  60 
  61 #define ST_MAX_BUFFERS (2 + ST_EXTRA_DEVS)
  62 
  63 #define ST_BUFFER_BLOCKS 32
  64 
  65 #define ST_WRITE_THRESHOLD_BLOCKS 30
  66 
  67 #define ST_BUFFER_SIZE (ST_BUFFER_BLOCKS * ST_BLOCK_SIZE)
  68 #define ST_WRITE_THRESHOLD (ST_WRITE_THRESHOLD_BLOCKS * ST_BLOCK_SIZE)
  69 
  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
  75 
  76 #ifdef DEBUG
  77 static int debugging = 1;
  78 #endif
  79 
  80 #define MAX_RETRIES 0
  81 #define MAX_WRITE_RETRIES 0
  82 #define MAX_READY_RETRIES 5
  83 #define NO_TAPE  NOT_READY
  84 
  85 #define ST_TIMEOUT 90000
  86 #define ST_LONG_TIMEOUT 200000
  87 
  88 static int st_nbr_buffers;
  89 static ST_buffer **st_buffers;
  90 static int st_buffer_size = ST_BUFFER_SIZE;
  91 static int st_write_threshold = ST_WRITE_THRESHOLD;
  92 static int st_max_buffers = ST_MAX_BUFFERS;
  93 
  94 static Scsi_Tape * scsi_tapes = NULL;
  95 
  96 static void st_init(void);
  97 static int st_attach(Scsi_Device *);
  98 static int st_detect(Scsi_Device *);
  99 static void st_detach(Scsi_Device *);
 100 
 101 struct Scsi_Device_Template st_template = {NULL, "tape", "st", NULL, TYPE_TAPE, 
 102                                              SCSI_TAPE_MAJOR, 0, 0, 0, 0,
 103                                              st_detect, st_init,
 104                                              NULL, st_attach, st_detach};
 105 
 106 static int st_int_ioctl(struct inode * inode,struct file * file,
 107              unsigned int cmd_in, unsigned long arg);
 108 
 109 
 110 
 111 
 112 /* Convert the result to success code */
 113         static int
 114 st_chk_result(Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 115 {
 116   int dev = SCpnt->request.dev;
 117   int result = SCpnt->result;
 118   unsigned char * sense = SCpnt->sense_buffer, scode;
 119   char *stp;
 120 
 121   if (!result /* && SCpnt->sense_buffer[0] == 0 */ )
 122     return 0;
 123 #ifdef DEBUG
 124   if (debugging) {
 125     printk("st%d: Error: %x, cmd: %x %x %x %x %x %x Len: %d\n", dev, result,
 126            SCpnt->data_cmnd[0], SCpnt->data_cmnd[1], SCpnt->data_cmnd[2],
 127            SCpnt->data_cmnd[3], SCpnt->data_cmnd[4], SCpnt->data_cmnd[5],
 128            SCpnt->request_bufflen);
 129     if (driver_byte(result) & DRIVER_SENSE)
 130       print_sense("st", SCpnt);
 131   }
 132 #endif
 133   scode = sense[2] & 0x0f;
 134   if (!(driver_byte(result) & DRIVER_SENSE) ||
 135       ((sense[0] & 0x70) == 0x70 &&
 136        scode != NO_SENSE &&
 137        scode != RECOVERED_ERROR &&
 138        scode != UNIT_ATTENTION &&
 139        scode != BLANK_CHECK &&
 140        scode != VOLUME_OVERFLOW)) {  /* Abnormal conditions for tape */
 141     printk("st%d: Error %x. ", dev, result);
 142     if (driver_byte(result) & DRIVER_SENSE)
 143       print_sense("st", SCpnt);
 144     else
 145       printk("\n");
 146   }
 147 
 148   if ((sense[0] & 0x70) == 0x70 &&
 149       scode == RECOVERED_ERROR
 150 #ifdef ST_RECOVERED_WRITE_FATAL
 151       && SCpnt->data_cmnd[0] != WRITE_6
 152       && SCpnt->data_cmnd[0] != WRITE_FILEMARKS
 153 #endif
 154       ) {
 155     scsi_tapes[dev].recover_count++;
 156     scsi_tapes[dev].mt_status->mt_erreg += (1 << MT_ST_SOFTERR_SHIFT);
 157     if (SCpnt->data_cmnd[0] == READ_6)
 158       stp = "read";
 159     else if (SCpnt->data_cmnd[0] == WRITE_6)
 160       stp = "write";
 161     else
 162       stp = "ioctl";
 163     printk("st%d: Recovered %s error (%d).\n", dev, stp,
 164            scsi_tapes[dev].recover_count);
 165     return 0;
 166   }
 167   return (-EIO);
 168 }
 169 
 170 
 171 /* Wakeup from interrupt */
 172         static void
 173 st_sleep_done (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 174 {
 175   int st_nbr, remainder;
 176   Scsi_Tape * STp;
 177 
 178   if ((st_nbr = SCpnt->request.dev) < st_template.nr_dev && st_nbr >= 0) {
 179     STp = &(scsi_tapes[st_nbr]);
 180     if ((STp->buffer)->writing &&
 181         (SCpnt->sense_buffer[0] & 0x70) == 0x70 &&
 182         (SCpnt->sense_buffer[2] & 0x40)) {
 183       /* EOM at write-behind, has all been written? */
 184       if ((SCpnt->sense_buffer[0] & 0x80) != 0)
 185         remainder = (SCpnt->sense_buffer[3] << 24) |
 186               (SCpnt->sense_buffer[4] << 16) |
 187                 (SCpnt->sense_buffer[5] << 8) | SCpnt->sense_buffer[6];
 188       else
 189         remainder = 0;
 190       if ((SCpnt->sense_buffer[2] & 0x0f) == VOLUME_OVERFLOW ||
 191           remainder > 0)
 192         (STp->buffer)->last_result = SCpnt->result; /* Error */
 193       else
 194         (STp->buffer)->last_result = INT_MAX; /* OK */
 195     }
 196     else
 197       (STp->buffer)->last_result = SCpnt->result;
 198     (STp->buffer)->last_result_fatal = st_chk_result(SCpnt);
 199     if ((STp->buffer)->writing)
 200       SCpnt->request.dev = -1;
 201     else
 202       SCpnt->request.dev = 0xffff;
 203     if ((STp->buffer)->writing <= 0)
 204       wake_up( &(STp->waiting) );
 205   }
 206 #ifdef DEBUG
 207   else if (debugging)
 208     printk("st?: Illegal interrupt device %x\n", st_nbr);
 209 #endif
 210 }
 211 
 212 
 213 /* Handle the write-behind checking */
 214         static void
 215 write_behind_check(int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 216 {
 217   Scsi_Tape * STp;
 218   ST_buffer * STbuffer;
 219   unsigned long flags;
 220 
 221   STp = &(scsi_tapes[dev]);
 222   STbuffer = STp->buffer;
 223 
 224   save_flags(flags);
 225   cli();
 226   if (STbuffer->last_result < 0) {
 227     STbuffer->writing = (- STbuffer->writing);
 228     sleep_on( &(STp->waiting) );
 229     STbuffer->writing = (- STbuffer->writing);
 230   }
 231   restore_flags(flags);
 232 
 233   if (STbuffer->writing < STbuffer->buffer_bytes)
 234     memcpy(STbuffer->b_data,
 235            STbuffer->b_data + STbuffer->writing,
 236            STbuffer->buffer_bytes - STbuffer->writing);
 237   STbuffer->buffer_bytes -= STbuffer->writing;
 238   if (STp->drv_block >= 0) {
 239     if (STp->block_size == 0)
 240       STp->drv_block++;
 241     else
 242       STp->drv_block += STbuffer->writing / STp->block_size;
 243   }
 244   STbuffer->writing = 0;
 245 
 246   return;
 247 }
 248 
 249 
 250 /* Back over EOF if it has been inadvertently crossed (ioctl not used because
 251    it messes up the block number). */
 252         static int
 253 back_over_eof(int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 254 {
 255   Scsi_Cmnd *SCpnt;
 256   Scsi_Tape *STp = &(scsi_tapes[dev]);
 257   unsigned char cmd[10];
 258   unsigned int flags;
 259 
 260   cmd[0] = SPACE;
 261   cmd[1] = 0x01; /* Space FileMarks */
 262   cmd[2] = cmd[3] = cmd[4] = 0xff;  /* -1 filemarks */
 263   cmd[5] = 0;
 264 
 265   SCpnt = allocate_device(NULL, STp->device, 1);
 266   SCpnt->sense_buffer[0] = 0;
 267   SCpnt->request.dev = dev;
 268   scsi_do_cmd(SCpnt,
 269               (void *) cmd, (void *) (STp->buffer)->b_data, 0,
 270               st_sleep_done, ST_TIMEOUT, MAX_RETRIES);
 271 
 272   /* need to do the check with interrupts off. -RAB */
 273   save_flags(flags);
 274   cli();
 275   if (SCpnt->request.dev == dev) sleep_on( &(STp->waiting) );
 276   restore_flags(flags);
 277   
 278   SCpnt->request.dev = -1;
 279   if ((STp->buffer)->last_result != 0) {
 280     printk("st%d: Backing over filemark failed.\n", dev);
 281     if ((STp->mt_status)->mt_fileno >= 0)
 282       (STp->mt_status)->mt_fileno += 1;
 283     (STp->mt_status)->mt_blkno = 0;
 284   }
 285 
 286   return (STp->buffer)->last_result_fatal;
 287 }
 288 
 289 
 290 /* Flush the write buffer (never need to write if variable blocksize). */
 291         static int
 292 flush_write_buffer(int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 293 {
 294   int offset, transfer, blks;
 295   int result;
 296   unsigned int flags;
 297   unsigned char cmd[10];
 298   Scsi_Cmnd *SCpnt;
 299   Scsi_Tape *STp = &(scsi_tapes[dev]);
 300 
 301   if ((STp->buffer)->writing) {
 302     write_behind_check(dev);
 303     if ((STp->buffer)->last_result_fatal) {
 304 #ifdef DEBUG
 305       if (debugging)
 306         printk("st%d: Async write error (flush) %x.\n", dev,
 307                (STp->buffer)->last_result);
 308 #endif
 309       if ((STp->buffer)->last_result == INT_MAX)
 310         return (-ENOSPC);
 311       return (-EIO);
 312     }
 313   }
 314 
 315   result = 0;
 316   if (STp->dirty == 1) {
 317     SCpnt = allocate_device(NULL, STp->device, 1);
 318 
 319     offset = (STp->buffer)->buffer_bytes;
 320     transfer = ((offset + STp->block_size - 1) /
 321                 STp->block_size) * STp->block_size;
 322 #ifdef DEBUG
 323     if (debugging)
 324       printk("st%d: Flushing %d bytes.\n", dev, transfer);
 325 #endif
 326     memset((STp->buffer)->b_data + offset, 0, transfer - offset);
 327 
 328     SCpnt->sense_buffer[0] = 0;
 329     memset(cmd, 0, 10);
 330     cmd[0] = WRITE_6;
 331     cmd[1] = 1;
 332     blks = transfer / STp->block_size;
 333     cmd[2] = blks >> 16;
 334     cmd[3] = blks >> 8;
 335     cmd[4] = blks;
 336     SCpnt->request.dev = dev;
 337     scsi_do_cmd (SCpnt,
 338                  (void *) cmd, (STp->buffer)->b_data, transfer,
 339                  st_sleep_done, ST_TIMEOUT, MAX_WRITE_RETRIES);
 340 
 341     /* this must be done with interrupts off */
 342     save_flags (flags);
 343     cli();
 344     if (SCpnt->request.dev == dev) sleep_on( &(STp->waiting) );
 345     restore_flags(flags);
 346  
 347     if ((STp->buffer)->last_result_fatal != 0) {
 348       printk("st%d: Error on flush.\n", dev);
 349       if ((SCpnt->sense_buffer[0] & 0x70) == 0x70 &&
 350           (SCpnt->sense_buffer[2] & 0x40) &&
 351           (SCpnt->sense_buffer[2] & 0x0f) != VOLUME_OVERFLOW) {
 352         STp->dirty = 0;
 353         (STp->buffer)->buffer_bytes = 0;
 354         result = (-ENOSPC);
 355       }
 356       else
 357         result = (-EIO);
 358       STp->drv_block = (-1);
 359     }
 360     else {
 361       if (STp->drv_block >= 0)
 362         STp->drv_block += blks;
 363       STp->dirty = 0;
 364       (STp->buffer)->buffer_bytes = 0;
 365     }
 366     SCpnt->request.dev = -1;  /* Mark as not busy */
 367   }
 368   return result;
 369 }
 370 
 371 
 372 /* Flush the tape buffer. The tape will be positioned correctly unless
 373    seek_next is true. */
 374         static int
 375 flush_buffer(struct inode * inode, struct file * filp, int seek_next)
     /* [previous][next][first][last][top][bottom][index][help] */
 376 {
 377   int dev;
 378   int backspace, result;
 379   Scsi_Tape * STp;
 380   ST_buffer * STbuffer;
 381 
 382   dev = MINOR(inode->i_rdev) & 127;
 383   STp = &(scsi_tapes[dev]);
 384   STbuffer = STp->buffer;
 385 
 386   if (STp->ready != ST_READY)
 387     return 0;
 388 
 389   if (STp->rw == ST_WRITING)  /* Writing */
 390     return flush_write_buffer(dev);
 391 
 392   if (STp->block_size == 0)
 393     return 0;
 394 
 395   backspace = ((STp->buffer)->buffer_bytes +
 396     (STp->buffer)->read_pointer) / STp->block_size -
 397       ((STp->buffer)->read_pointer + STp->block_size - 1) /
 398         STp->block_size;
 399   (STp->buffer)->buffer_bytes = 0;
 400   (STp->buffer)->read_pointer = 0;
 401   result = 0;
 402   if (!seek_next) {
 403     if ((STp->eof == ST_FM) && !STp->eof_hit) {
 404       result = back_over_eof(dev); /* Back over the EOF hit */
 405       if (!result) {
 406         STp->eof = ST_NOEOF;
 407         STp->eof_hit = 0;
 408       }
 409     }
 410     if (!result && backspace > 0)
 411       result = st_int_ioctl(inode, filp, MTBSR, backspace);
 412   }
 413   return result;
 414 
 415 }
 416 
 417 
 418 /* Open the device */
 419         static int
 420 scsi_tape_open(struct inode * inode, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
 421 {
 422     int dev;
 423     unsigned short flags;
 424     unsigned int processor_flags;
 425     int i;
 426     unsigned char cmd[10];
 427     Scsi_Cmnd * SCpnt;
 428     Scsi_Tape * STp;
 429 
 430     dev = MINOR(inode->i_rdev) & 127;
 431     if (dev >= st_template.dev_max || !scsi_tapes[dev].device)
 432       return (-ENXIO);
 433     STp = &(scsi_tapes[dev]);
 434     if (STp->in_use) {
 435       printk("st%d: Device already in use.\n", dev);
 436       return (-EBUSY);
 437     }
 438 
 439     /* Allocate buffer for this user */
 440     for (i=0; i < st_nbr_buffers; i++)
 441       if (!st_buffers[i]->in_use)
 442         break;
 443     if (i >= st_nbr_buffers) {
 444       printk("st%d: No free buffers.\n", dev);
 445       return (-EBUSY);
 446     }
 447     STp->buffer = st_buffers[i];
 448     (STp->buffer)->in_use = 1;
 449     (STp->buffer)->writing = 0;
 450     STp->in_use = 1;
 451 
 452     flags = filp->f_flags;
 453     STp->write_prot = ((flags & O_ACCMODE) == O_RDONLY);
 454 
 455     STp->dirty = 0;
 456     STp->rw = ST_IDLE;
 457     STp->ready = ST_READY;
 458     if (STp->eof != ST_EOD)  /* Save EOD across opens */
 459       STp->eof = ST_NOEOF;
 460     STp->eof_hit = 0;
 461     STp->recover_count = 0;
 462 
 463     SCpnt = allocate_device(NULL, STp->device, 1);
 464     if (!SCpnt) {
 465       printk("st%d: Tape request not allocated", dev);
 466       return (-EBUSY);
 467     }
 468 
 469     SCpnt->sense_buffer[0]=0;
 470     memset ((void *) &cmd[0], 0, 10);
 471     cmd[0] = TEST_UNIT_READY;
 472     SCpnt->request.dev = dev;
 473     scsi_do_cmd(SCpnt,
 474                 (void *) cmd, (void *) (STp->buffer)->b_data,
 475                 0, st_sleep_done, ST_LONG_TIMEOUT,
 476                 MAX_READY_RETRIES);
 477 
 478 
 479     /* this must be done with interrupts off */
 480     save_flags (processor_flags);
 481     cli();
 482     if (SCpnt->request.dev == dev) sleep_on( &(STp->waiting) );
 483     restore_flags(processor_flags);
 484 
 485     if ((SCpnt->sense_buffer[0] & 0x70) == 0x70 &&
 486         (SCpnt->sense_buffer[2] & 0x0f) == UNIT_ATTENTION) { /* New media? */
 487       (STp->mt_status)->mt_fileno = 0 ;
 488       SCpnt->sense_buffer[0]=0;
 489       memset ((void *) &cmd[0], 0, 10);
 490       cmd[0] = TEST_UNIT_READY;
 491       SCpnt->request.dev = dev;
 492       scsi_do_cmd(SCpnt,
 493                   (void *) cmd, (void *) (STp->buffer)->b_data,
 494                   0, st_sleep_done, ST_LONG_TIMEOUT,
 495                   MAX_READY_RETRIES);
 496 
 497 
 498     /* this must be done with interrupts off */
 499     save_flags (processor_flags);
 500     cli();
 501     if (SCpnt->request.dev == dev) sleep_on( &(STp->waiting) );
 502     restore_flags(processor_flags);
 503 
 504       (STp->mt_status)->mt_fileno = STp->drv_block = 0;
 505       STp->eof = ST_NOEOF;
 506     }
 507 
 508     if ((STp->buffer)->last_result_fatal != 0) {
 509       if ((SCpnt->sense_buffer[0] & 0x70) == 0x70 &&
 510           (SCpnt->sense_buffer[2] & 0x0f) == NO_TAPE) {
 511         (STp->mt_status)->mt_fileno = STp->drv_block = 0 ;
 512         printk("st%d: No tape.\n", dev);
 513         STp->ready = ST_NO_TAPE;
 514       } else {
 515         (STp->mt_status)->mt_fileno = STp->drv_block = (-1);
 516         STp->ready = ST_NOT_READY;
 517       }
 518       SCpnt->request.dev = -1;  /* Mark as not busy */
 519       (STp->buffer)->in_use = 0;
 520       STp->buffer = NULL;
 521       STp->density = 0;   /* Clear the erroneous "residue" */
 522       STp->write_prot = 0;
 523       STp->block_size = 0;
 524       STp->eof = ST_NOEOF;
 525       (STp->mt_status)->mt_fileno = STp->drv_block = 0;
 526       if (scsi_tapes[dev].device->host->hostt->usage_count)
 527         (*scsi_tapes[dev].device->host->hostt->usage_count)++;
 528       return 0;
 529     }
 530 
 531     SCpnt->sense_buffer[0]=0;
 532     memset ((void *) &cmd[0], 0, 10);
 533     cmd[0] = READ_BLOCK_LIMITS;
 534     SCpnt->request.dev = dev;
 535     scsi_do_cmd(SCpnt,
 536                 (void *) cmd, (void *) (STp->buffer)->b_data,
 537                 6, st_sleep_done, ST_TIMEOUT, MAX_READY_RETRIES);
 538 
 539 
 540     /* this must be done with interrupts off */
 541     save_flags (processor_flags);
 542     cli();
 543     if (SCpnt->request.dev == dev) sleep_on( &(STp->waiting) );
 544     restore_flags(processor_flags);
 545 
 546     if (!SCpnt->result && !SCpnt->sense_buffer[0]) {
 547       STp->max_block = ((STp->buffer)->b_data[1] << 16) |
 548         ((STp->buffer)->b_data[2] << 8) | (STp->buffer)->b_data[3];
 549       STp->min_block = ((STp->buffer)->b_data[4] << 8) |
 550         (STp->buffer)->b_data[5];
 551 #ifdef DEBUG
 552       if (debugging)
 553         printk("st%d: Block limits %d - %d bytes.\n", dev, STp->min_block,
 554                STp->max_block);
 555 #endif
 556     }
 557     else {
 558       STp->min_block = STp->max_block = (-1);
 559 #ifdef DEBUG
 560       if (debugging)
 561         printk("st%d: Can't read block limits.\n", dev);
 562 #endif
 563     }
 564 
 565     SCpnt->sense_buffer[0]=0;
 566     memset ((void *) &cmd[0], 0, 10);
 567     cmd[0] = MODE_SENSE;
 568     cmd[4] = 12;
 569     SCpnt->request.dev = dev;
 570     scsi_do_cmd(SCpnt,
 571                 (void *) cmd, (void *) (STp->buffer)->b_data,
 572                 12, st_sleep_done, ST_TIMEOUT, MAX_READY_RETRIES);
 573 
 574 
 575     /* this must be done with interrupts off */
 576     save_flags (processor_flags);
 577     cli();
 578     if (SCpnt->request.dev == dev) sleep_on( &(STp->waiting) );
 579     restore_flags(processor_flags);
 580 
 581     if ((STp->buffer)->last_result_fatal != 0) {
 582 #ifdef DEBUG
 583       if (debugging)
 584         printk("st%d: No Mode Sense.\n", dev);
 585 #endif
 586       (STp->buffer)->b_data[2] =
 587       (STp->buffer)->b_data[3] = 0;
 588     }
 589     SCpnt->request.dev = -1;  /* Mark as not busy */
 590 
 591 #ifdef DEBUG
 592     if (debugging)
 593       printk("st%d: Mode sense. Length %d, medium %x, WBS %x, BLL %d\n", dev,
 594              (STp->buffer)->b_data[0], (STp->buffer)->b_data[1],
 595              (STp->buffer)->b_data[2], (STp->buffer)->b_data[3]);
 596 #endif
 597 
 598     if ((STp->buffer)->b_data[3] >= 8) {
 599       STp->drv_buffer = ((STp->buffer)->b_data[2] >> 4) & 7;
 600       STp->density = (STp->buffer)->b_data[4];
 601       STp->block_size = (STp->buffer)->b_data[9] * 65536 +
 602         (STp->buffer)->b_data[10] * 256 + (STp->buffer)->b_data[11];
 603 #ifdef DEBUG
 604       if (debugging)
 605         printk("st%d: Density %x, tape length: %x, drv buffer: %d\n",
 606                dev, STp->density, (STp->buffer)->b_data[5] * 65536 +
 607                (STp->buffer)->b_data[6] * 256 + (STp->buffer)->b_data[7],
 608                STp->drv_buffer);
 609 #endif
 610       if (STp->block_size > st_buffer_size) {
 611         printk("st%d: Blocksize %d too large for buffer.\n", dev,
 612                STp->block_size);
 613         (STp->buffer)->in_use = 0;
 614         STp->in_use = 0;
 615         return (-EIO);
 616       }
 617 
 618     }
 619     else
 620       STp->block_size = 512;  /* "Educated Guess" (?) */
 621 
 622     if (STp->block_size > 0) {
 623       (STp->buffer)->buffer_blocks = st_buffer_size / STp->block_size;
 624       (STp->buffer)->buffer_size =
 625         (STp->buffer)->buffer_blocks * STp->block_size;
 626     }
 627     else {
 628       (STp->buffer)->buffer_blocks = 1;
 629       (STp->buffer)->buffer_size = st_buffer_size;
 630     }
 631     (STp->buffer)->buffer_bytes = (STp->buffer)->read_pointer = 0;
 632 
 633 #ifdef DEBUG
 634     if (debugging)
 635       printk("st%d: Block size: %d, buffer size: %d (%d blocks).\n", dev,
 636              STp->block_size, (STp->buffer)->buffer_size,
 637              (STp->buffer)->buffer_blocks);
 638 #endif
 639 
 640     STp->drv_write_prot = ((STp->buffer)->b_data[2] & 0x80) != 0;
 641     if (STp->drv_write_prot) {
 642       STp->write_prot = 1;
 643 #ifdef DEBUG
 644       if (debugging)
 645         printk( "st%d: Write protected\n", dev);
 646 #endif
 647       if ((flags & O_ACCMODE) == O_WRONLY || (flags & O_ACCMODE) == O_RDWR) {
 648         (STp->buffer)->in_use = 0;
 649         STp->buffer = 0;
 650         STp->in_use = 0;
 651         return (-EROFS);
 652       }
 653     }
 654 
 655     if (scsi_tapes[dev].device->host->hostt->usage_count)
 656       (*scsi_tapes[dev].device->host->hostt->usage_count)++;
 657     if(st_template.usage_count) (*st_template.usage_count)++;
 658 
 659     return 0;
 660 }
 661 
 662 
 663 /* Close the device*/
 664         static void
 665 scsi_tape_close(struct inode * inode, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
 666 {
 667     int dev;
 668     int result;
 669     int rewind;
 670     static unsigned char cmd[10];
 671     Scsi_Cmnd * SCpnt;
 672     Scsi_Tape * STp;
 673     unsigned int flags;
 674    
 675     dev = MINOR(inode->i_rdev);
 676     rewind = (dev & 0x80) == 0;
 677     dev = dev & 127;
 678     STp = &(scsi_tapes[dev]);
 679 
 680     if ( STp->rw == ST_WRITING) {
 681 
 682       result = flush_write_buffer(dev);
 683 
 684 #ifdef DEBUG
 685       if (debugging)
 686         printk("st%d: File length %ld bytes.\n", dev, (long)(filp->f_pos));
 687 #endif
 688 
 689       if (result == 0 || result == (-ENOSPC)) {
 690         SCpnt = allocate_device(NULL, STp->device, 1);
 691 
 692         SCpnt->sense_buffer[0] = 0;
 693         memset(cmd, 0, 10);
 694         cmd[0] = WRITE_FILEMARKS;
 695         cmd[4] = 1 + STp->two_fm;
 696         SCpnt->request.dev = dev;
 697         scsi_do_cmd( SCpnt,
 698                     (void *) cmd, (void *) (STp->buffer)->b_data,
 699                     0, st_sleep_done, ST_TIMEOUT, MAX_WRITE_RETRIES);
 700 
 701 
 702         /* this must be done with interrupts off */
 703         save_flags (flags);
 704         cli();
 705         if (SCpnt->request.dev == dev) sleep_on( &(STp->waiting) );
 706         restore_flags(flags);
 707 
 708 
 709         if ((STp->buffer)->last_result_fatal != 0) {
 710           SCpnt->request.dev = -1;  /* Mark as not busy */
 711           printk("st%d: Error on write filemark.\n", dev);
 712         }
 713         else {
 714           SCpnt->request.dev = -1;  /* Mark as not busy */
 715           if ((STp->mt_status)->mt_fileno >= 0)
 716               (STp->mt_status)->mt_fileno++ ;
 717           STp->drv_block = 0;
 718           if (STp->two_fm)
 719             back_over_eof(dev);
 720         }
 721 
 722       }
 723 
 724 #ifdef DEBUG
 725       if (debugging)
 726         printk("st%d: Buffer flushed, %d EOF(s) written\n", dev, cmd[4]);
 727 #endif
 728     }
 729     else if (!rewind) {
 730 #ifndef ST_IN_FILE_POS
 731       if ((STp->eof == ST_FM) && !STp->eof_hit)
 732         back_over_eof(dev);
 733 #else
 734       flush_buffer(inode, filp, 0);
 735 #endif
 736     }
 737 
 738     if (rewind)
 739       st_int_ioctl(inode, filp, MTREW, 1);
 740 
 741     if (STp->buffer != NULL)
 742       (STp->buffer)->in_use = 0;
 743     STp->in_use = 0;
 744 
 745     if (scsi_tapes[dev].device->host->hostt->usage_count)
 746       (*scsi_tapes[dev].device->host->hostt->usage_count)--;
 747     if(st_template.usage_count) (*st_template.usage_count)--;
 748 
 749     return;
 750 }
 751 
 752 
 753 /* Write command */
 754         static int
 755 st_write(struct inode * inode, struct file * filp, char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 756 {
 757     int dev;
 758     int total, do_count, blks, retval, transfer;
 759     int write_threshold;
 760     int doing_write = 0;
 761     static unsigned char cmd[10];
 762     char *b_point;
 763     Scsi_Cmnd * SCpnt;
 764     Scsi_Tape * STp;
 765     unsigned int flags;
 766 
 767     dev = MINOR(inode->i_rdev) & 127;
 768     STp = &(scsi_tapes[dev]);
 769     if (STp->ready != ST_READY)
 770       return (-EIO);
 771 #ifdef DEBUG
 772     if (!STp->in_use) {
 773       printk("st%d: Incorrect device.\n", dev);
 774       return (-EIO);
 775     }
 776 #endif
 777 
 778     if (STp->write_prot)
 779       return (-EACCES);
 780 
 781     if (STp->block_size == 0 && count > st_buffer_size)
 782       return (-EOVERFLOW);
 783 
 784     if (STp->rw == ST_READING) {
 785       retval = flush_buffer(inode, filp, 0);
 786       if (retval)
 787         return retval;
 788       STp->rw = ST_WRITING;
 789     }
 790 
 791     if (STp->moves_after_eof < 255)
 792       STp->moves_after_eof++;
 793 
 794     if ((STp->buffer)->writing) {
 795       write_behind_check(dev);
 796       if ((STp->buffer)->last_result_fatal) {
 797 #ifdef DEBUG
 798         if (debugging)
 799           printk("st%d: Async write error (write) %x.\n", dev,
 800                  (STp->buffer)->last_result);
 801 #endif
 802         if ((STp->buffer)->last_result == INT_MAX) {
 803           retval = (-ENOSPC);  /* All has been written */
 804           STp->eof = ST_EOM_OK;
 805         }
 806         else
 807           retval = (-EIO);
 808         return retval;
 809       }
 810     }
 811 
 812     if (STp->eof == ST_EOM_OK)
 813       return (-ENOSPC);
 814     else if (STp->eof == ST_EOM_ERROR)
 815       return (-EIO);
 816 
 817     if (!STp->do_buffer_writes) {
 818       if (STp->block_size != 0 && (count % STp->block_size) != 0)
 819         return (-EIO);   /* Write must be integral number of blocks */
 820       write_threshold = 1;
 821     }
 822     else
 823       write_threshold = (STp->buffer)->buffer_size;
 824     if (!STp->do_async_writes)
 825       write_threshold--;
 826 
 827     SCpnt = allocate_device(NULL, STp->device, 1);
 828 
 829     total = count;
 830 
 831     memset(cmd, 0, 10);
 832     cmd[0] = WRITE_6;
 833     cmd[1] = (STp->block_size != 0);
 834 
 835     STp->rw = ST_WRITING;
 836 
 837     b_point = buf;
 838     while((STp->block_size == 0 && !STp->do_async_writes && count > 0) ||
 839           (STp->block_size != 0 &&
 840            (STp->buffer)->buffer_bytes + count > write_threshold))
 841     {
 842       doing_write = 1;
 843       if (STp->block_size == 0)
 844         do_count = count;
 845       else {
 846         do_count = (STp->buffer)->buffer_size - (STp->buffer)->buffer_bytes;
 847         if (do_count > count)
 848           do_count = count;
 849       }
 850       memcpy_fromfs((STp->buffer)->b_data +
 851                     (STp->buffer)->buffer_bytes, b_point, do_count);
 852 
 853       if (STp->block_size == 0)
 854         blks = transfer = do_count;
 855       else {
 856         blks = ((STp->buffer)->buffer_bytes + do_count) /
 857           STp->block_size;
 858         transfer = blks * STp->block_size;
 859       }
 860       cmd[2] = blks >> 16;
 861       cmd[3] = blks >> 8;
 862       cmd[4] = blks;
 863       SCpnt->sense_buffer[0] = 0;
 864       SCpnt->request.dev = dev;
 865       scsi_do_cmd (SCpnt,
 866                    (void *) cmd, (STp->buffer)->b_data, transfer,
 867                    st_sleep_done, ST_TIMEOUT, MAX_WRITE_RETRIES);
 868 
 869 
 870         /* this must be done with interrupts off */
 871         save_flags (flags);
 872         cli();
 873         if (SCpnt->request.dev == dev) sleep_on( &(STp->waiting) );
 874         restore_flags(flags);
 875 
 876       if ((STp->buffer)->last_result_fatal != 0) {
 877 #ifdef DEBUG
 878         if (debugging)
 879           printk("st%d: Error on write:\n", dev);
 880 #endif
 881         if ((SCpnt->sense_buffer[0] & 0x70) == 0x70 &&
 882             (SCpnt->sense_buffer[2] & 0x40)) {
 883           if (STp->block_size != 0 && (SCpnt->sense_buffer[0] & 0x80) != 0)
 884             transfer = (SCpnt->sense_buffer[3] << 24) |
 885               (SCpnt->sense_buffer[4] << 16) |
 886                 (SCpnt->sense_buffer[5] << 8) | SCpnt->sense_buffer[6];
 887           else if (STp->block_size == 0 &&
 888                    (SCpnt->sense_buffer[2] & 0x0f) == VOLUME_OVERFLOW)
 889             transfer = do_count;
 890           else
 891             transfer = 0;
 892           if (STp->block_size != 0)
 893             transfer *= STp->block_size;
 894           if (transfer <= do_count) {
 895             filp->f_pos += do_count - transfer;
 896             count -= do_count - transfer;
 897             if (STp->drv_block >= 0) {
 898               if (STp->block_size == 0 && transfer < do_count)
 899                 STp->drv_block++;
 900               else if (STp->block_size != 0)
 901                 STp->drv_block += (do_count - transfer) / STp->block_size;
 902             }
 903             STp->eof = ST_EOM_OK;
 904             retval = (-ENOSPC); /* EOM within current request */
 905 #ifdef DEBUG
 906             if (debugging)
 907               printk("st%d: EOM with %d bytes unwritten.\n",
 908                      dev, transfer);
 909 #endif
 910           }
 911           else {
 912             STp->eof = ST_EOM_ERROR;
 913             STp->drv_block = (-1);    /* Too cautious? */
 914             retval = (-EIO); /* EOM for old data */
 915 #ifdef DEBUG
 916             if (debugging)
 917               printk("st%d: EOM with lost data.\n", dev);
 918 #endif
 919           }
 920         }
 921         else {
 922           STp->drv_block = (-1);    /* Too cautious? */
 923           retval = (-EIO);
 924         }
 925 
 926         SCpnt->request.dev = -1;  /* Mark as not busy */
 927         (STp->buffer)->buffer_bytes = 0;
 928         STp->dirty = 0;
 929         if (count < total)
 930           return total - count;
 931         else
 932           return retval;
 933       }
 934       filp->f_pos += do_count;
 935       b_point += do_count;
 936       count -= do_count;
 937       if (STp->drv_block >= 0) {
 938         if (STp->block_size == 0)
 939           STp->drv_block++;
 940         else
 941           STp->drv_block += blks;
 942       }
 943       (STp->buffer)->buffer_bytes = 0;
 944       STp->dirty = 0;
 945     }
 946     if (count != 0) {
 947       STp->dirty = 1;
 948       memcpy_fromfs((STp->buffer)->b_data +
 949                     (STp->buffer)->buffer_bytes,b_point,count);
 950       filp->f_pos += count;
 951       (STp->buffer)->buffer_bytes += count;
 952       count = 0;
 953     }
 954 
 955     if (doing_write && (STp->buffer)->last_result_fatal != 0) {
 956       SCpnt->request.dev = -1;
 957       return (STp->buffer)->last_result_fatal;
 958     }
 959 
 960     if (STp->do_async_writes &&
 961         ((STp->buffer)->buffer_bytes >= STp->write_threshold ||
 962          STp->block_size == 0) ) {
 963       /* Schedule an asynchronous write */
 964       if (STp->block_size == 0)
 965         (STp->buffer)->writing = (STp->buffer)->buffer_bytes;
 966       else
 967         (STp->buffer)->writing = ((STp->buffer)->buffer_bytes /
 968           STp->block_size) * STp->block_size;
 969       STp->dirty = !((STp->buffer)->writing ==
 970                      (STp->buffer)->buffer_bytes);
 971 
 972       if (STp->block_size == 0)
 973         blks = (STp->buffer)->writing;
 974       else
 975         blks = (STp->buffer)->writing / STp->block_size;
 976       cmd[2] = blks >> 16;
 977       cmd[3] = blks >> 8;
 978       cmd[4] = blks;
 979       SCpnt->result = (STp->buffer)->last_result = -1;
 980       SCpnt->sense_buffer[0] = 0;
 981       SCpnt->request.dev = dev;
 982       scsi_do_cmd (SCpnt,
 983                    (void *) cmd, (STp->buffer)->b_data,
 984                    (STp->buffer)->writing,
 985                    st_sleep_done, ST_TIMEOUT, MAX_WRITE_RETRIES);
 986     }
 987     else
 988       SCpnt->request.dev = -1;  /* Mark as not busy */
 989 
 990     STp->at_sm &= (total != 0);
 991     return( total);
 992 }   
 993 
 994 
 995 /* Read command */
 996         static int
 997 st_read(struct inode * inode, struct file * filp, char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 998 {
 999     int dev;
1000     int total;
1001     int transfer, blks, bytes;
1002     static unsigned char cmd[10];
1003     Scsi_Cmnd * SCpnt;
1004     Scsi_Tape * STp;
1005     unsigned int flags;
1006 
1007     dev = MINOR(inode->i_rdev) & 127;
1008     STp = &(scsi_tapes[dev]);
1009     if (STp->ready != ST_READY)
1010       return (-EIO);
1011 #ifdef DEBUG
1012     if (!STp->in_use) {
1013       printk("st%d: Incorrect device.\n", dev);
1014       return (-EIO);
1015     }
1016 #endif
1017 
1018     if (STp->block_size == 0 && count > st_buffer_size)
1019       return (-EOVERFLOW);
1020 
1021     if (!(STp->do_read_ahead) && STp->block_size != 0 &&
1022         (count % STp->block_size) != 0)
1023       return (-EIO);    /* Read must be integral number of blocks */
1024 
1025     if (STp->rw == ST_WRITING) {
1026       transfer = flush_buffer(inode, filp, 0);
1027       if (transfer)
1028         return transfer;
1029       STp->rw = ST_READING;
1030     }
1031     if (STp->moves_after_eof < 255)
1032       STp->moves_after_eof++;
1033 
1034 #ifdef DEBUG
1035     if (debugging && STp->eof != ST_NOEOF)
1036       printk("st%d: EOF flag up. Bytes %d\n", dev,
1037              (STp->buffer)->buffer_bytes);
1038 #endif
1039     if (((STp->buffer)->buffer_bytes == 0) &&
1040         (STp->eof == ST_EOM_OK || STp->eof == ST_EOD))
1041       return (-EIO);  /* EOM or Blank Check */
1042 
1043     STp->rw = ST_READING;
1044 
1045     SCpnt = allocate_device(NULL, STp->device, 1);
1046 
1047     for (total = 0; total < count; ) {
1048 
1049       if ((STp->buffer)->buffer_bytes == 0 &&
1050           STp->eof == ST_NOEOF) {
1051 
1052         memset(cmd, 0, 10);
1053         cmd[0] = READ_6;
1054         cmd[1] = (STp->block_size != 0);
1055         if (STp->block_size == 0)
1056           blks = bytes = count;
1057         else {
1058           if (STp->do_read_ahead) {
1059             blks = (STp->buffer)->buffer_blocks;
1060             bytes = blks * STp->block_size;
1061           }
1062           else {
1063             bytes = count;
1064             if (bytes > st_buffer_size)
1065               bytes = st_buffer_size;
1066             blks = bytes / STp->block_size;
1067             bytes = blks * STp->block_size;
1068           }
1069         }
1070         cmd[2] = blks >> 16;
1071         cmd[3] = blks >> 8;
1072         cmd[4] = blks;
1073 
1074         SCpnt->sense_buffer[0] = 0;
1075         SCpnt->request.dev = dev;
1076         scsi_do_cmd (SCpnt,
1077                      (void *) cmd, (STp->buffer)->b_data,
1078                      bytes, st_sleep_done, ST_TIMEOUT, MAX_RETRIES);
1079 
1080 
1081         /* this must be done with interrupts off */
1082         save_flags (flags);
1083         cli();
1084         if (SCpnt->request.dev == dev) sleep_on( &(STp->waiting) );
1085         restore_flags(flags);
1086 
1087         (STp->buffer)->read_pointer = 0;
1088         STp->eof_hit = 0;
1089         STp->at_sm = 0;
1090 
1091         if ((STp->buffer)->last_result_fatal) {
1092 #ifdef DEBUG
1093           if (debugging)
1094             printk("st%d: Sense: %2x %2x %2x %2x %2x %2x %2x %2x\n", dev,
1095                    SCpnt->sense_buffer[0], SCpnt->sense_buffer[1],
1096                    SCpnt->sense_buffer[2], SCpnt->sense_buffer[3],
1097                    SCpnt->sense_buffer[4], SCpnt->sense_buffer[5],
1098                    SCpnt->sense_buffer[6], SCpnt->sense_buffer[7]);
1099 #endif
1100           if ((SCpnt->sense_buffer[0] & 0x70) == 0x70) { /* extended sense */
1101 
1102             if ((SCpnt->sense_buffer[2] & 0xe0) != 0) { /* EOF, EOM, or ILI */
1103 
1104               if ((SCpnt->sense_buffer[0] & 0x80) != 0)
1105                 transfer = (SCpnt->sense_buffer[3] << 24) |
1106                   (SCpnt->sense_buffer[4] << 16) |
1107                     (SCpnt->sense_buffer[5] << 8) | SCpnt->sense_buffer[6];
1108               else
1109                 transfer = 0;
1110               if (STp->block_size == 0 &&
1111                   (SCpnt->sense_buffer[2] & 0x0f) == MEDIUM_ERROR)
1112                 transfer = bytes;
1113 
1114               if (SCpnt->sense_buffer[2] & 0x20) {
1115                 if (STp->block_size == 0) {
1116                   if (transfer <= 0)
1117                     transfer = 0;
1118                   (STp->buffer)->buffer_bytes = bytes - transfer;
1119                 }
1120                 else {
1121                   printk("st%d: Incorrect block size.\n", dev);
1122                   SCpnt->request.dev = -1;  /* Mark as not busy */
1123                   return (-EIO);
1124                 }
1125               }
1126               else if (SCpnt->sense_buffer[2] & 0x40) {
1127                 STp->eof = ST_EOM_OK;
1128                 if (STp->block_size == 0)
1129                   (STp->buffer)->buffer_bytes = bytes - transfer;
1130                 else
1131                   (STp->buffer)->buffer_bytes =
1132                     bytes - transfer * STp->block_size;
1133 #ifdef DEBUG
1134                 if (debugging)
1135                   printk("st%d: EOM detected (%d bytes read).\n", dev,
1136                          (STp->buffer)->buffer_bytes);
1137 #endif
1138               }
1139               else if (SCpnt->sense_buffer[2] & 0x80) {
1140                 STp->eof = ST_FM;
1141                 if (STp->block_size == 0)
1142                   (STp->buffer)->buffer_bytes = 0;
1143                 else
1144                   (STp->buffer)->buffer_bytes =
1145                     bytes - transfer * STp->block_size;
1146 #ifdef DEBUG
1147                 if (debugging)
1148                   printk(
1149                     "st%d: EOF detected (%d bytes read, transferred %d bytes).\n",
1150                          dev, (STp->buffer)->buffer_bytes, total);
1151 #endif
1152               }
1153             } /* end of EOF, EOM, ILI test */
1154             else { /* nonzero sense key */
1155 #ifdef DEBUG
1156               if (debugging)
1157                 printk("st%d: Tape error while reading.\n", dev);
1158 #endif
1159               SCpnt->request.dev = -1;
1160               STp->drv_block = (-1);
1161               if (total)
1162                 return total;
1163               else if (STp->moves_after_eof == 1 &&
1164                        (SCpnt->sense_buffer[2] & 0x0f) == BLANK_CHECK) {
1165 #ifdef DEBUG
1166                 if (debugging)
1167                   printk("st%d: Zero returned for first BLANK CHECK after EOF.\n",
1168                          dev);
1169 #endif
1170                 STp->eof = ST_EOD;
1171                 return 0; /* First BLANK_CHECK after EOF */
1172               }
1173               else
1174                 return -EIO;
1175             }
1176           } /* End of extended sense test */
1177           else {
1178             transfer = (STp->buffer)->last_result_fatal;
1179             SCpnt->request.dev = -1;  /* Mark as not busy */
1180             return transfer;
1181           }
1182         } /* End of error handling */
1183         else /* Read successful */
1184           (STp->buffer)->buffer_bytes = bytes;
1185 
1186         if (STp->drv_block >= 0) {
1187           if (STp->block_size == 0)
1188             STp->drv_block++;
1189           else
1190             STp->drv_block += (STp->buffer)->buffer_bytes / STp->block_size;
1191         }
1192 
1193       } /* if ((STp->buffer)->buffer_bytes == 0 &&
1194            STp->eof == ST_NOEOF) */
1195 
1196       if ((STp->buffer)->buffer_bytes > 0) {
1197 #ifdef DEBUG
1198         if (debugging && STp->eof != ST_NOEOF)
1199           printk("st%d: EOF up. Left %d, needed %d.\n", dev,
1200                  (STp->buffer)->buffer_bytes, count - total);
1201 #endif
1202         transfer = (STp->buffer)->buffer_bytes < count - total ?
1203           (STp->buffer)->buffer_bytes : count - total;
1204         memcpy_tofs(buf, (STp->buffer)->b_data +
1205                     (STp->buffer)->read_pointer,transfer);
1206         filp->f_pos += transfer;
1207         buf += transfer;
1208         total += transfer;
1209         (STp->buffer)->buffer_bytes -= transfer;
1210         (STp->buffer)->read_pointer += transfer;
1211       }
1212       else if (STp->eof != ST_NOEOF) {
1213         STp->eof_hit = 1;
1214         SCpnt->request.dev = -1;  /* Mark as not busy */
1215         if (total == 0 && STp->eof == ST_FM) {
1216           STp->eof = ST_NOEOF;
1217           STp->drv_block = 0;
1218           if (STp->moves_after_eof > 1)
1219             STp->moves_after_eof = 0;
1220           if ((STp->mt_status)->mt_fileno >= 0)
1221             (STp->mt_status)->mt_fileno++;
1222         }
1223         if (total == 0 && STp->eof == ST_EOM_OK)
1224           return (-EIO);  /* ST_EOM_ERROR not used in read */
1225         return total;
1226       }
1227 
1228       if (STp->block_size == 0)
1229         count = total;  /* Read only one variable length block */
1230 
1231     } /* for (total = 0; total < count; ) */
1232 
1233     SCpnt->request.dev = -1;  /* Mark as not busy */
1234 
1235     return total;
1236 }
1237 
1238 
1239 
1240 /* Set the driver options */
1241         static int
1242 st_set_options(struct inode * inode, long options)
     /* [previous][next][first][last][top][bottom][index][help] */
1243 {
1244   int dev, value;
1245   Scsi_Tape *STp;
1246 
1247   dev = MINOR(inode->i_rdev) & 127;
1248   STp = &(scsi_tapes[dev]);
1249   if ((options & MT_ST_OPTIONS) == MT_ST_BOOLEANS) {
1250     STp->do_buffer_writes = (options & MT_ST_BUFFER_WRITES) != 0;
1251     STp->do_async_writes  = (options & MT_ST_ASYNC_WRITES) != 0;
1252     STp->do_read_ahead    = (options & MT_ST_READ_AHEAD) != 0;
1253     STp->two_fm           = (options & MT_ST_TWO_FM) != 0;
1254     STp->fast_mteom       = (options & MT_ST_FAST_MTEOM) != 0;
1255 #ifdef DEBUG
1256     debugging = (options & MT_ST_DEBUGGING) != 0;
1257     printk(
1258 "st%d: options: buffer writes: %d, async writes: %d, read ahead: %d\n",
1259            dev, STp->do_buffer_writes, STp->do_async_writes,
1260            STp->do_read_ahead);
1261     printk("              two FMs: %d, fast mteom: %d debugging: %d\n",
1262            STp->two_fm, STp->fast_mteom, debugging);
1263 #endif
1264   }
1265   else if ((options & MT_ST_OPTIONS) == MT_ST_WRITE_THRESHOLD) {
1266     value = (options & ~MT_ST_OPTIONS) * ST_BLOCK_SIZE;
1267     if (value < 1 || value > st_buffer_size) {
1268       printk("st: Write threshold %d too small or too large.\n",
1269              value);
1270       return (-EIO);
1271     }
1272     STp->write_threshold = value;
1273 #ifdef DEBUG
1274     printk("st%d: Write threshold set to %d bytes.\n", dev,
1275            STp->write_threshold);
1276 #endif
1277   }
1278   else
1279     return (-EIO);
1280 
1281   return 0;
1282 }
1283 
1284 
1285 /* Internal ioctl function */
1286         static int
1287 st_int_ioctl(struct inode * inode,struct file * file,
     /* [previous][next][first][last][top][bottom][index][help] */
1288              unsigned int cmd_in, unsigned long arg)
1289 {
1290    int dev = MINOR(inode->i_rdev);
1291    int timeout = ST_LONG_TIMEOUT;
1292    long ltmp;
1293    int ioctl_result;
1294    unsigned char cmd[10];
1295    Scsi_Cmnd * SCpnt;
1296    Scsi_Tape * STp;
1297    int fileno, blkno, at_sm, undone, datalen;
1298    unsigned int flags;
1299 
1300    dev = dev & 127;
1301    STp = &(scsi_tapes[dev]);
1302    if (STp->ready != ST_READY)
1303      return (-EIO);
1304    fileno = (STp->mt_status)->mt_fileno ;
1305    blkno = STp->drv_block;
1306    at_sm = STp->at_sm;
1307 
1308    memset(cmd, 0, 10);
1309    datalen = 0;
1310    switch (cmd_in) {
1311      case MTFSF:
1312      case MTFSFM:
1313        cmd[0] = SPACE;
1314        cmd[1] = 0x01; /* Space FileMarks */
1315        cmd[2] = (arg >> 16);
1316        cmd[3] = (arg >> 8);
1317        cmd[4] = arg;
1318 #ifdef DEBUG
1319        if (debugging)
1320          printk("st%d: Spacing tape forward over %d filemarks.\n", dev,
1321                 cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
1322 #endif
1323        if (fileno >= 0)
1324          fileno += arg;
1325        blkno = 0;
1326        at_sm &= (arg != 0);
1327        break; 
1328      case MTBSF:
1329      case MTBSFM:
1330        cmd[0] = SPACE;
1331        cmd[1] = 0x01; /* Space FileMarks */
1332        ltmp = (-arg);
1333        cmd[2] = (ltmp >> 16);
1334        cmd[3] = (ltmp >> 8);
1335        cmd[4] = ltmp;
1336 #ifdef DEBUG
1337        if (debugging) {
1338          if (cmd[2] & 0x80)
1339            ltmp = 0xff000000;
1340          ltmp = ltmp | (cmd[2] << 16) | (cmd[3] << 8) | cmd[4];
1341          printk("st%d: Spacing tape backward over %ld filemarks.\n", dev, (-ltmp));
1342        }
1343 #endif
1344        if (fileno >= 0)
1345          fileno -= arg;
1346        blkno = (-1);  /* We can't know the block number */
1347        at_sm &= (arg != 0);
1348        break; 
1349      case MTFSR:
1350        cmd[0] = SPACE;
1351        cmd[1] = 0x00; /* Space Blocks */
1352        cmd[2] = (arg >> 16);
1353        cmd[3] = (arg >> 8);
1354        cmd[4] = arg;
1355 #ifdef DEBUG
1356        if (debugging)
1357          printk("st%d: Spacing tape forward %d blocks.\n", dev,
1358                 cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
1359 #endif
1360        if (blkno >= 0)
1361          blkno += arg;
1362        at_sm &= (arg != 0);
1363        break; 
1364      case MTBSR:
1365        cmd[0] = SPACE;
1366        cmd[1] = 0x00; /* Space Blocks */
1367        ltmp = (-arg);
1368        cmd[2] = (ltmp >> 16);
1369        cmd[3] = (ltmp >> 8);
1370        cmd[4] = ltmp;
1371 #ifdef DEBUG
1372        if (debugging) {
1373          if (cmd[2] & 0x80)
1374            ltmp = 0xff000000;
1375          ltmp = ltmp | (cmd[2] << 16) | (cmd[3] << 8) | cmd[4];
1376          printk("st%d: Spacing tape backward %ld blocks.\n", dev, (-ltmp));
1377        }
1378 #endif
1379        if (blkno >= 0)
1380          blkno -= arg;
1381        at_sm &= (arg != 0);
1382        break; 
1383      case MTFSS:
1384        cmd[0] = SPACE;
1385        cmd[1] = 0x04; /* Space Setmarks */
1386        cmd[2] = (arg >> 16);
1387        cmd[3] = (arg >> 8);
1388        cmd[4] = arg;
1389 #ifdef DEBUG
1390        if (debugging)
1391          printk("st%d: Spacing tape forward %d setmarks.\n", dev,
1392                 cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
1393 #endif
1394        if (arg != 0) {
1395          blkno = fileno = (-1);
1396          at_sm = 1;
1397        }
1398        break; 
1399      case MTBSS:
1400        cmd[0] = SPACE;
1401        cmd[1] = 0x04; /* Space Setmarks */
1402        ltmp = (-arg);
1403        cmd[2] = (ltmp >> 16);
1404        cmd[3] = (ltmp >> 8);
1405        cmd[4] = ltmp;
1406 #ifdef DEBUG
1407        if (debugging) {
1408          if (cmd[2] & 0x80)
1409            ltmp = 0xff000000;
1410          ltmp = ltmp | (cmd[2] << 16) | (cmd[3] << 8) | cmd[4];
1411          printk("st%d: Spacing tape backward %ld setmarks.\n", dev, (-ltmp));
1412        }
1413 #endif
1414        if (arg != 0) {
1415          blkno = fileno = (-1);
1416          at_sm = 1;
1417        }
1418        break; 
1419      case MTWEOF:
1420      case MTWSM:
1421        if (STp->write_prot)
1422          return (-EACCES);
1423        cmd[0] = WRITE_FILEMARKS;
1424        if (cmd_in == MTWSM)
1425          cmd[1] = 2;
1426        cmd[2] = (arg >> 16);
1427        cmd[3] = (arg >> 8);
1428        cmd[4] = arg;
1429        timeout = ST_TIMEOUT;
1430 #ifdef DEBUG
1431        if (debugging) {
1432          if (cmd_in == MTWEOF)
1433            printk("st%d: Writing %d filemarks.\n", dev,
1434                   cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
1435          else
1436            printk("st%d: Writing %d setmarks.\n", dev,
1437                   cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
1438        }
1439 #endif
1440        if (fileno >= 0)
1441          fileno += arg;
1442        blkno = 0;
1443        at_sm = (cmd_in == MTWSM);
1444        break; 
1445      case MTREW:
1446        cmd[0] = REZERO_UNIT;
1447 #ifdef ST_NOWAIT
1448        cmd[1] = 1;  /* Don't wait for completion */
1449        timeout = ST_TIMEOUT;
1450 #endif
1451 #ifdef DEBUG
1452        if (debugging)
1453          printk("st%d: Rewinding tape.\n", dev);
1454 #endif
1455        fileno = blkno = at_sm = 0 ;
1456        break; 
1457      case MTOFFL:
1458        cmd[0] = START_STOP;
1459 #ifdef ST_NOWAIT
1460        cmd[1] = 1;  /* Don't wait for completion */
1461        timeout = ST_TIMEOUT;
1462 #endif
1463 #ifdef DEBUG
1464        if (debugging)
1465          printk("st%d: Unloading tape.\n", dev);
1466 #endif
1467        fileno = blkno = at_sm = 0 ;
1468        break; 
1469      case MTNOP:
1470 #ifdef DEBUG
1471        if (debugging)
1472          printk("st%d: No op on tape.\n", dev);
1473 #endif
1474        return 0;  /* Should do something ? */
1475        break;
1476      case MTRETEN:
1477        cmd[0] = START_STOP;
1478 #ifdef ST_NOWAIT
1479        cmd[1] = 1;  /* Don't wait for completion */
1480        timeout = ST_TIMEOUT;
1481 #endif
1482        cmd[4] = 3;
1483 #ifdef DEBUG
1484        if (debugging)
1485          printk("st%d: Retensioning tape.\n", dev);
1486 #endif
1487        fileno = blkno = at_sm = 0;
1488        break; 
1489      case MTEOM:
1490        if (!STp->fast_mteom) {
1491          /* space to the end of tape */
1492          ioctl_result = st_int_ioctl(inode, file, MTFSF, 0x3fff);
1493          fileno = (STp->mt_status)->mt_fileno ;
1494          if (STp->eof == ST_EOD || STp->eof == ST_EOM_OK)
1495            return 0;
1496          /* The next lines would hide the number of spaced FileMarks
1497             That's why I inserted the previous lines. I had no luck
1498             with detecting EOM with FSF, so we go now to EOM.
1499             Joerg Weule */
1500        }
1501        else
1502          fileno = (-1);
1503        cmd[0] = SPACE;
1504        cmd[1] = 3;
1505 #ifdef DEBUG
1506        if (debugging)
1507          printk("st%d: Spacing to end of recorded medium.\n", dev);
1508 #endif
1509        blkno = 0;
1510        at_sm = 0;
1511        break; 
1512      case MTERASE:
1513        if (STp->write_prot)
1514          return (-EACCES);
1515        cmd[0] = ERASE;
1516        cmd[1] = 1;  /* To the end of tape */
1517 #ifdef ST_NOWAIT
1518        cmd[1] |= 2;  /* Don't wait for completion */
1519        timeout = ST_TIMEOUT;
1520 #else
1521        timeout = ST_LONG_TIMEOUT * 8;
1522 #endif
1523 #ifdef DEBUG
1524        if (debugging)
1525          printk("st%d: Erasing tape.\n", dev);
1526 #endif
1527        fileno = blkno = at_sm = 0 ;
1528        break;
1529      case MTSEEK:
1530        if ((STp->device)->scsi_level < SCSI_2) {
1531          cmd[0] = QFA_SEEK_BLOCK;
1532          cmd[2] = (arg >> 16);
1533          cmd[3] = (arg >> 8);
1534          cmd[4] = arg;
1535          cmd[5] = 0;
1536        }
1537        else {
1538          cmd[0] = SEEK_10;
1539          cmd[1] = 4;
1540          cmd[3] = (arg >> 24);
1541          cmd[4] = (arg >> 16);
1542          cmd[5] = (arg >> 8);
1543          cmd[6] = arg;
1544        }
1545 #ifdef ST_NOWAIT
1546        cmd[1] |= 1;  /* Don't wait for completion */
1547        timeout = ST_TIMEOUT;
1548 #endif
1549 #ifdef DEBUG
1550        if (debugging)
1551          printk("st%d: Seeking tape to block %ld.\n", dev, arg);
1552 #endif
1553        fileno = blkno = (-1);
1554        at_sm = 0;
1555        break;
1556      case MTSETBLK:  /* Set block length */
1557      case MTSETDENSITY: /* Set tape density */
1558      case MTSETDRVBUFFER: /* Set drive buffering */
1559        if (STp->dirty || (STp->buffer)->buffer_bytes != 0)
1560          return (-EIO);   /* Not allowed if data in buffer */
1561        if (cmd_in == MTSETBLK &&
1562            arg != 0 &&
1563            (arg < STp->min_block || arg > STp->max_block ||
1564             arg > st_buffer_size)) {
1565          printk("st%d: Illegal block size.\n", dev);
1566          return (-EINVAL);
1567        }
1568        cmd[0] = MODE_SELECT;
1569        cmd[4] = datalen = 12;
1570 
1571        memset((STp->buffer)->b_data, 0, 12);
1572        if (cmd_in == MTSETDRVBUFFER)
1573          (STp->buffer)->b_data[2] = (arg & 7) << 4;
1574        else
1575          (STp->buffer)->b_data[2] = 
1576            STp->drv_buffer << 4;
1577        (STp->buffer)->b_data[3] = 8;     /* block descriptor length */
1578        if (cmd_in == MTSETDENSITY)
1579          (STp->buffer)->b_data[4] = arg;
1580        else
1581          (STp->buffer)->b_data[4] = STp->density;
1582        if (cmd_in == MTSETBLK)
1583          ltmp = arg;
1584        else
1585          ltmp = STp->block_size;
1586        (STp->buffer)->b_data[9] = (ltmp >> 16);
1587        (STp->buffer)->b_data[10] = (ltmp >> 8);
1588        (STp->buffer)->b_data[11] = ltmp;
1589        timeout = ST_TIMEOUT;
1590 #ifdef DEBUG
1591        if (debugging) {
1592          if (cmd_in == MTSETBLK)
1593            printk("st%d: Setting block size to %d bytes.\n", dev,
1594                   (STp->buffer)->b_data[9] * 65536 +
1595                   (STp->buffer)->b_data[10] * 256 +
1596                   (STp->buffer)->b_data[11]);
1597          else if (cmd_in == MTSETDENSITY)
1598            printk("st%d: Setting density code to %x.\n", dev,
1599                   (STp->buffer)->b_data[4]);
1600          else
1601            printk("st%d: Setting drive buffer code to %d.\n", dev,
1602                   ((STp->buffer)->b_data[2] >> 4) & 7);
1603        }
1604 #endif
1605        break;
1606      default:
1607        printk("st%d: Unknown st_ioctl command %x.\n", dev, cmd_in);
1608        return (-ENOSYS);
1609      }
1610 
1611    SCpnt = allocate_device(NULL, STp->device, 1);
1612    SCpnt->sense_buffer[0] = 0;
1613    SCpnt->request.dev = dev;
1614    scsi_do_cmd(SCpnt,
1615                (void *) cmd, (void *) (STp->buffer)->b_data, datalen,
1616                st_sleep_done, timeout, MAX_RETRIES);
1617 
1618 
1619         /* this must be done with interrupts off */
1620         save_flags (flags);
1621         cli();
1622         if (SCpnt->request.dev == dev) sleep_on( &(STp->waiting) );
1623         restore_flags(flags);
1624 
1625 
1626    ioctl_result = (STp->buffer)->last_result_fatal;
1627 
1628    SCpnt->request.dev = -1;  /* Mark as not busy */
1629 
1630    if (cmd_in == MTFSF)
1631      STp->moves_after_eof = 0;
1632    else
1633      STp->moves_after_eof = 1;
1634    if (!ioctl_result) {  /* SCSI command successful */
1635      if (cmd_in != MTSEEK) {
1636        STp->drv_block = blkno;
1637        (STp->mt_status)->mt_fileno = fileno;
1638        STp->at_sm = at_sm;
1639      }
1640      else {
1641        STp->drv_block = (STp->mt_status)->mt_fileno = (-1);
1642        STp->at_sm = 0;
1643      }
1644      if (cmd_in == MTBSFM)
1645        ioctl_result = st_int_ioctl(inode, file, MTFSF, 1);
1646      else if (cmd_in == MTFSFM)
1647        ioctl_result = st_int_ioctl(inode, file, MTBSF, 1);
1648      else if (cmd_in == MTSETBLK) {
1649        STp->block_size = arg;
1650        if (arg != 0) {
1651          (STp->buffer)->buffer_blocks =
1652            st_buffer_size / STp->block_size;
1653          (STp->buffer)->buffer_size =
1654            (STp->buffer)->buffer_blocks * STp->block_size;
1655        }
1656        else {
1657          (STp->buffer)->buffer_blocks = 1;
1658          (STp->buffer)->buffer_size = st_buffer_size;
1659        }
1660        (STp->buffer)->buffer_bytes =
1661          (STp->buffer)->read_pointer = 0;
1662      }
1663      else if (cmd_in == MTSETDRVBUFFER)
1664        STp->drv_buffer = (arg & 7);
1665      else if (cmd_in == MTSETDENSITY)
1666        STp->density = arg;
1667      else if (cmd_in == MTEOM) {
1668        STp->eof = ST_EOD;
1669        STp->eof_hit = 0;
1670      }
1671      else if (cmd_in != MTSETBLK && cmd_in != MTNOP) {
1672        STp->eof = ST_NOEOF;
1673        STp->eof_hit = 0;
1674      }
1675    } else {  /* SCSI command was not completely successful */
1676      if (SCpnt->sense_buffer[2] & 0x40) {
1677        if (cmd_in != MTBSF && cmd_in != MTBSFM &&
1678            cmd_in != MTBSR && cmd_in != MTBSS)
1679          STp->eof = ST_EOM_OK;
1680        STp->eof_hit = 0;
1681        STp->drv_block = 0;
1682      }
1683      undone = (
1684           (SCpnt->sense_buffer[3] << 24) +
1685           (SCpnt->sense_buffer[4] << 16) +
1686           (SCpnt->sense_buffer[5] << 8) +
1687           SCpnt->sense_buffer[6] );
1688      if ( (cmd_in == MTFSF) || (cmd_in == MTFSFM) ) {
1689        if (fileno >= 0)
1690          (STp->mt_status)->mt_fileno = fileno - undone ;
1691        else
1692          (STp->mt_status)->mt_fileno = fileno;
1693        STp->drv_block = 0;
1694      }
1695      else if ( (cmd_in == MTBSF) || (cmd_in == MTBSFM) ) {
1696        (STp->mt_status)->mt_fileno = fileno + undone ;
1697        STp->drv_block = 0;
1698      }
1699      else if (cmd_in == MTFSR) {
1700        if (SCpnt->sense_buffer[2] & 0x80) { /* Hit filemark */
1701          (STp->mt_status)->mt_fileno++;
1702          STp->drv_block = 0;
1703        }
1704        else {
1705          if (blkno >= undone)
1706            STp->drv_block = blkno - undone;
1707          else
1708            STp->drv_block = (-1);
1709        }
1710      }
1711      else if (cmd_in == MTBSR) {
1712        if (SCpnt->sense_buffer[2] & 0x80) { /* Hit filemark */
1713          (STp->mt_status)->mt_fileno--;
1714          STp->drv_block = (-1);
1715        }
1716        else {
1717          if (blkno >= 0)
1718            STp->drv_block = blkno + undone;
1719          else
1720            STp->drv_block = (-1);
1721        }
1722      }
1723      else if (cmd_in == MTEOM || cmd_in == MTSEEK) {
1724        (STp->mt_status)->mt_fileno = (-1);
1725        STp->drv_block = (-1);
1726      }
1727      if (STp->eof == ST_NOEOF &&
1728          (SCpnt->sense_buffer[2] & 0x0f) == BLANK_CHECK)
1729        STp->eof = ST_EOD;
1730    }
1731 
1732    return ioctl_result;
1733 }
1734 
1735 
1736 
1737 /* The ioctl command */
1738         static int
1739 st_ioctl(struct inode * inode,struct file * file,
     /* [previous][next][first][last][top][bottom][index][help] */
1740          unsigned int cmd_in, unsigned long arg)
1741 {
1742    int dev = MINOR(inode->i_rdev);
1743    int i, cmd, result;
1744    struct mtop mtc;
1745    struct mtpos mt_pos;
1746    unsigned char scmd[10];
1747    Scsi_Cmnd *SCpnt;
1748    Scsi_Tape *STp;
1749    unsigned int flags;
1750 
1751    dev = dev & 127;
1752    STp = &(scsi_tapes[dev]);
1753 #ifdef DEBUG
1754    if (debugging && !STp->in_use) {
1755      printk("st%d: Incorrect device.\n", dev);
1756      return (-EIO);
1757    }
1758 #endif
1759 
1760    cmd = cmd_in & IOCCMD_MASK;
1761    if (cmd == (MTIOCTOP & IOCCMD_MASK)) {
1762 
1763      if (((cmd_in & IOCSIZE_MASK) >> IOCSIZE_SHIFT) != sizeof(mtc))
1764        return (-EINVAL);
1765 
1766      i = verify_area(VERIFY_WRITE, (void *)arg, sizeof(mtc));
1767      if (i)
1768         return i;
1769 
1770      memcpy_fromfs((char *) &mtc, (char *)arg, sizeof(struct mtop));
1771 
1772      i = flush_buffer(inode, file, mtc.mt_op == MTSEEK ||
1773                       mtc.mt_op == MTREW || mtc.mt_op == MTOFFL ||
1774                       mtc.mt_op == MTRETEN || mtc.mt_op == MTEOM);
1775      if (i < 0)
1776        return i;
1777      if (mtc.mt_op != MTNOP && mtc.mt_op != MTSETBLK &&
1778          mtc.mt_op != MTSETDENSITY && mtc.mt_op != MTWSM &&
1779          mtc.mt_op != MTSETDRVBUFFER)
1780        STp->rw = ST_IDLE;  /* Prevent automatic WEOF */
1781 
1782      if (mtc.mt_op == MTSETDRVBUFFER &&
1783          (mtc.mt_count & MT_ST_OPTIONS) != 0)
1784        return st_set_options(inode, mtc.mt_count);
1785      else
1786        return st_int_ioctl(inode, file, mtc.mt_op, mtc.mt_count);
1787    }
1788    else if (cmd == (MTIOCGET & IOCCMD_MASK)) {
1789 
1790      if (((cmd_in & IOCSIZE_MASK) >> IOCSIZE_SHIFT) != sizeof(struct mtget))
1791        return (-EINVAL);
1792      i = verify_area(VERIFY_WRITE, (void *)arg, sizeof(struct mtget));
1793      if (i)
1794        return i;
1795 
1796      (STp->mt_status)->mt_dsreg =
1797        ((STp->block_size << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK) |
1798        ((STp->density << MT_ST_DENSITY_SHIFT) & MT_ST_DENSITY_MASK);
1799      (STp->mt_status)->mt_blkno = STp->drv_block;
1800      if (STp->block_size != 0) {
1801        if (STp->rw == ST_WRITING)
1802          (STp->mt_status)->mt_blkno +=
1803            (STp->buffer)->buffer_bytes / STp->block_size;
1804        else if (STp->rw == ST_READING)
1805          (STp->mt_status)->mt_blkno -= ((STp->buffer)->buffer_bytes +
1806            STp->block_size - 1) / STp->block_size;
1807      }
1808 
1809      (STp->mt_status)->mt_gstat = 0;
1810      if (STp->drv_write_prot)
1811        (STp->mt_status)->mt_gstat |= GMT_WR_PROT(0xffffffff);
1812      if ((STp->mt_status)->mt_blkno == 0) {
1813        if ((STp->mt_status)->mt_fileno == 0)
1814          (STp->mt_status)->mt_gstat |= GMT_BOT(0xffffffff);
1815        else
1816          (STp->mt_status)->mt_gstat |= GMT_EOF(0xffffffff);
1817      }
1818      if (STp->eof == ST_EOM_OK || STp->eof == ST_EOM_ERROR)
1819        (STp->mt_status)->mt_gstat |= GMT_EOT(0xffffffff);
1820      else if (STp->eof == ST_EOD)
1821        (STp->mt_status)->mt_gstat |= GMT_EOD(0xffffffff);
1822      if (STp->density == 1)
1823        (STp->mt_status)->mt_gstat |= GMT_D_800(0xffffffff);
1824      else if (STp->density == 2)
1825        (STp->mt_status)->mt_gstat |= GMT_D_1600(0xffffffff);
1826      else if (STp->density == 3)
1827        (STp->mt_status)->mt_gstat |= GMT_D_6250(0xffffffff);
1828      if (STp->ready == ST_READY)
1829        (STp->mt_status)->mt_gstat |= GMT_ONLINE(0xffffffff);
1830      if (STp->ready == ST_NO_TAPE)
1831        (STp->mt_status)->mt_gstat |= GMT_DR_OPEN(0xffffffff);
1832      if (STp->at_sm)
1833        (STp->mt_status)->mt_gstat |= GMT_SM(0xffffffff);
1834 
1835      memcpy_tofs((char *)arg, (char *)(STp->mt_status),
1836                  sizeof(struct mtget));
1837 
1838      (STp->mt_status)->mt_erreg = 0;  /* Clear after read */
1839      return 0;
1840    }
1841    else if (cmd == (MTIOCPOS & IOCCMD_MASK)) {
1842      if (STp->ready != ST_READY)
1843        return (-EIO);
1844 #ifdef DEBUG
1845      if (debugging)
1846        printk("st%d: get tape position.\n", dev);
1847 #endif
1848      if (((cmd_in & IOCSIZE_MASK) >> IOCSIZE_SHIFT) != sizeof(struct mtpos))
1849        return (-EINVAL);
1850 
1851      i = flush_buffer(inode, file, 0);
1852      if (i < 0)
1853        return i;
1854 
1855      i = verify_area(VERIFY_WRITE, (void *)arg, sizeof(struct mtpos));
1856      if (i)
1857        return i;
1858 
1859      SCpnt = allocate_device(NULL, STp->device, 1);
1860 
1861      SCpnt->sense_buffer[0]=0;
1862      memset (scmd, 0, 10);
1863      if ((STp->device)->scsi_level < SCSI_2) {
1864        scmd[0] = QFA_REQUEST_BLOCK;
1865        scmd[4] = 3;
1866      }
1867      else {
1868        scmd[0] = READ_POSITION;
1869        scmd[1] = 1;
1870      }
1871      SCpnt->request.dev = dev;
1872      SCpnt->sense_buffer[0] = 0;
1873      scsi_do_cmd(SCpnt,
1874                  (void *) scmd, (void *) (STp->buffer)->b_data,
1875                  20, st_sleep_done, ST_TIMEOUT, MAX_READY_RETRIES);
1876 
1877 
1878         /* this must be done with interrupts off */
1879         save_flags (flags);
1880         cli();
1881         if (SCpnt->request.dev == dev) sleep_on( &(STp->waiting) );
1882         restore_flags(flags);
1883 
1884      
1885      if ((STp->buffer)->last_result_fatal != 0) {
1886        mt_pos.mt_blkno = (-1);
1887 #ifdef DEBUG
1888        if (debugging)
1889          printk("st%d: Can't read tape position.\n", dev);
1890 #endif
1891        result = (-EIO);
1892      }
1893      else {
1894        result = 0;
1895        if ((STp->device)->scsi_level < SCSI_2)
1896          mt_pos.mt_blkno = ((STp->buffer)->b_data[0] << 16) 
1897            + ((STp->buffer)->b_data[1] << 8) 
1898              + (STp->buffer)->b_data[2];
1899        else
1900          mt_pos.mt_blkno = ((STp->buffer)->b_data[4] << 24)
1901            + ((STp->buffer)->b_data[5] << 16) 
1902              + ((STp->buffer)->b_data[6] << 8) 
1903                + (STp->buffer)->b_data[7];
1904 
1905      }
1906 
1907      SCpnt->request.dev = -1;  /* Mark as not busy */
1908 
1909      memcpy_tofs((char *)arg, (char *) (&mt_pos), sizeof(struct mtpos));
1910      return result;
1911    }
1912    else if (STp->ready == ST_READY)
1913      return scsi_ioctl(STp->device, cmd_in, (void *) arg);
1914    else
1915      return (-EIO);
1916 }
1917 
1918 
1919 /* Set the boot options. Syntax: st=xxx,yyy
1920    where xxx is buffer size in 512 byte blocks and yyy is write threshold
1921    in 512 byte blocks. */
1922         void
1923 st_setup(char *str, int *ints)
     /* [previous][next][first][last][top][bottom][index][help] */
1924 {
1925   if (ints[0] > 0 && ints[1] > 0)
1926     st_buffer_size = ints[1] * ST_BLOCK_SIZE;
1927   if (ints[0] > 1 && ints[2] > 0) {
1928     st_write_threshold = ints[2] * ST_BLOCK_SIZE;
1929     if (st_write_threshold > st_buffer_size)
1930       st_write_threshold = st_buffer_size;
1931   }
1932   if (ints[0] > 2 && ints[3] > 0)
1933     st_max_buffers = ints[3];
1934 }
1935 
1936 
1937 static struct file_operations st_fops = {
1938    NULL,            /* lseek - default */
1939    st_read,         /* read - general block-dev read */
1940    st_write,        /* write - general block-dev write */
1941    NULL,            /* readdir - bad */
1942    NULL,            /* select */
1943    st_ioctl,        /* ioctl */
1944    NULL,            /* mmap */
1945    scsi_tape_open,  /* open */
1946    scsi_tape_close, /* release */
1947    NULL             /* fsync */
1948 };
1949 
1950 static int st_attach(Scsi_Device * SDp){
     /* [previous][next][first][last][top][bottom][index][help] */
1951    Scsi_Tape * tpnt;
1952    int i;
1953 
1954    if(SDp->type != TYPE_TAPE) return 1;
1955 
1956    if(st_template.nr_dev >= st_template.dev_max) 
1957      {
1958         SDp->attached--;
1959         return 1;
1960      }
1961 
1962    for(tpnt = scsi_tapes, i=0; i<st_template.dev_max; i++, tpnt++) 
1963      if(!tpnt->device) break;
1964 
1965    if(i >= st_template.dev_max) panic ("scsi_devices corrupt (st)");
1966 
1967    scsi_tapes[i].device = SDp;
1968    if (SDp->scsi_level <= 2)
1969      scsi_tapes[i].mt_status->mt_type = MT_ISSCSI1;
1970    else
1971      scsi_tapes[i].mt_status->mt_type = MT_ISSCSI2;
1972 
1973    st_template.nr_dev++;
1974    return 0;
1975 };
1976 
1977 static int st_detect(Scsi_Device * SDp)
     /* [previous][next][first][last][top][bottom][index][help] */
1978 {
1979   if(SDp->type != TYPE_TAPE) return 0;
1980 
1981   printk("Detected scsi tape st%d at scsi%d, channel %d, id %d, lun %d\n", 
1982          st_template.dev_noticed++,
1983          SDp->host->host_no, SDp->channel, SDp->id, SDp->lun); 
1984   
1985   return 1;
1986 }
1987 
1988 /* Driver initialization */
1989 static void st_init()
     /* [previous][next][first][last][top][bottom][index][help] */
1990 {
1991   int i;
1992   Scsi_Tape * STp;
1993   static int st_registered = 0;
1994 
1995   if (st_template.dev_noticed == 0) return;
1996 
1997   if(!st_registered) {
1998     if (register_chrdev(MAJOR_NR,"st",&st_fops)) {
1999       printk("Unable to get major %d for SCSI tapes\n",MAJOR_NR);
2000       return;
2001     }
2002     st_registered++;
2003   }
2004 
2005   if (scsi_tapes) return;
2006   scsi_tapes = (Scsi_Tape *) scsi_init_malloc(
2007                 (st_template.dev_noticed + ST_EXTRA_DEVS) * 
2008                                               sizeof(Scsi_Tape), GFP_ATOMIC);
2009   st_template.dev_max = st_template.dev_noticed + ST_EXTRA_DEVS;
2010 
2011 #ifdef DEBUG
2012   printk("st: Buffer size %d bytes, write threshold %d bytes.\n",
2013          st_buffer_size, st_write_threshold);
2014 #endif
2015 
2016   for (i=0; i < st_template.dev_max; ++i) {
2017     STp = &(scsi_tapes[i]);
2018     STp->device = NULL;
2019     STp->capacity = 0xfffff;
2020     STp->dirty = 0;
2021     STp->rw = ST_IDLE;
2022     STp->eof = ST_NOEOF;
2023     STp->waiting = NULL;
2024     STp->in_use = 0;
2025     STp->drv_buffer = 1;  /* Try buffering if no mode sense */
2026     STp->density = 0;
2027     STp->do_buffer_writes = ST_BUFFER_WRITES;
2028     STp->do_async_writes = ST_ASYNC_WRITES;
2029     STp->do_read_ahead = ST_READ_AHEAD;
2030     STp->two_fm = ST_TWO_FM;
2031     STp->fast_mteom = ST_FAST_MTEOM;
2032     STp->write_threshold = st_write_threshold;
2033     STp->drv_block = 0;
2034     STp->moves_after_eof = 1;
2035     STp->at_sm = 0;
2036     STp->mt_status = (struct mtget *) scsi_init_malloc(sizeof(struct mtget), GFP_ATOMIC);
2037     /* Initialize status */
2038     memset((void *) scsi_tapes[i].mt_status, 0, sizeof(struct mtget));
2039   }
2040 
2041   /* Allocate the buffers */
2042   st_nbr_buffers = st_template.dev_noticed + ST_EXTRA_DEVS;
2043   if (st_nbr_buffers > st_max_buffers)
2044     st_nbr_buffers = st_max_buffers;
2045   st_buffers = (ST_buffer **) scsi_init_malloc(st_nbr_buffers * 
2046                                                sizeof(ST_buffer *), GFP_ATOMIC);
2047   /* FIXME - if we are hitting this because we are loading a tape module
2048   as a loadable driver, we should not use kmalloc - it will allocate
2049   a 64Kb region in order to buffer about 32Kb.  Try using 31 blocks
2050   instead. */
2051   
2052   for (i=0; i < st_nbr_buffers; i++) {
2053     st_buffers[i] = (ST_buffer *) scsi_init_malloc(sizeof(ST_buffer) - 
2054                                                    1 + st_buffer_size, GFP_ATOMIC | GFP_DMA);
2055 #ifdef DEBUG
2056 /*    printk("st: Buffer address: %p\n", st_buffers[i]); */
2057 #endif
2058     st_buffers[i]->in_use = 0;
2059     st_buffers[i]->writing = 0;
2060   }
2061   return;
2062 }
2063 
2064 static void st_detach(Scsi_Device * SDp)
     /* [previous][next][first][last][top][bottom][index][help] */
2065 {
2066   Scsi_Tape * tpnt;
2067   int i;
2068   
2069   for(tpnt = scsi_tapes, i=0; i<st_template.dev_max; i++, tpnt++) 
2070     if(tpnt->device == SDp) {
2071       tpnt->device = NULL;
2072       SDp->attached--;
2073       st_template.nr_dev--;
2074       st_template.dev_noticed--;
2075       return;
2076     }
2077   return;
2078 }
2079 
2080 
2081 #ifdef MODULE
2082 char kernel_version[] = UTS_RELEASE;
2083 
2084 int init_module(void) {
     /* [previous][next][first][last][top][bottom][index][help] */
2085   st_template.usage_count = &mod_use_count_;
2086   return scsi_register_module(MODULE_SCSI_DEV, &st_template);
2087 }
2088 
2089 void cleanup_module( void) 
     /* [previous][next][first][last][top][bottom][index][help] */
2090 {
2091   int i;
2092 
2093   if (MOD_IN_USE) {
2094     printk(KERN_INFO __FILE__ ": module is in use, remove rejected\n");
2095     return;
2096   }
2097   scsi_unregister_module(MODULE_SCSI_DEV, &st_template);
2098   unregister_chrdev(SCSI_GENERIC_MAJOR, "st");
2099   if(scsi_tapes != NULL) {
2100     scsi_init_free((char *) scsi_tapes,
2101                    (st_template.dev_noticed + ST_EXTRA_DEVS) 
2102                    * sizeof(Scsi_Tape));
2103     
2104     for (i=0; i < st_nbr_buffers; i++) {
2105       scsi_init_free((char *) st_buffers[i],
2106                      sizeof(ST_buffer) - 1 + st_buffer_size);
2107     }
2108     
2109     scsi_init_free((char *) st_buffers, st_nbr_buffers * sizeof(ST_buffer *));
2110   }
2111   st_template.dev_max = 0;
2112 }
2113 #endif /* MODULE */

/* [previous][next][first][last][top][bottom][index][help] */