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

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