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

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

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