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->cmnd[0], SCpnt->cmnd[1], SCpnt->cmnd[2],
 123            SCpnt->cmnd[3], SCpnt->cmnd[4], SCpnt->cmnd[5],
 124            SCpnt->request_bufflen);
 125     if (driver_byte(result) & DRIVER_SENSE)
 126       print_sense("st", SCpnt);
 127   } else
 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->cmnd[0] != WRITE_6
 148       && SCpnt->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->cmnd[0] == READ_6)
 154       stp = "read";
 155     else if (SCpnt->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     }
 644 
 645     if (scsi_tapes[dev].device->host->hostt->usage_count)
 646       (*scsi_tapes[dev].device->host->hostt->usage_count)++;
 647 
 648     return 0;
 649 }
 650 
 651 
 652 /* Close the device*/
 653         static void
 654 scsi_tape_close(struct inode * inode, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
 655 {
 656     int dev;
 657     int result;
 658     int rewind;
 659     static unsigned char cmd[10];
 660     Scsi_Cmnd * SCpnt;
 661     Scsi_Tape * STp;
 662     unsigned int flags;
 663    
 664     dev = MINOR(inode->i_rdev);
 665     rewind = (dev & 0x80) == 0;
 666     dev = dev & 127;
 667     STp = &(scsi_tapes[dev]);
 668 
 669     if ( STp->rw == ST_WRITING) {
 670 
 671       result = flush_write_buffer(dev);
 672 
 673 #ifdef DEBUG
 674       if (debugging)
 675         printk("st%d: File length %ld bytes.\n", dev, (long)(filp->f_pos));
 676 #endif
 677 
 678       if (result == 0 || result == (-ENOSPC)) {
 679         SCpnt = allocate_device(NULL, STp->device, 1);
 680 
 681         SCpnt->sense_buffer[0] = 0;
 682         memset(cmd, 0, 10);
 683         cmd[0] = WRITE_FILEMARKS;
 684         cmd[4] = 1 + STp->two_fm;
 685         SCpnt->request.dev = dev;
 686         scsi_do_cmd( SCpnt,
 687                     (void *) cmd, (void *) (STp->buffer)->b_data,
 688                     0, st_sleep_done, ST_TIMEOUT, MAX_WRITE_RETRIES);
 689 
 690 
 691         /* this must be done with interrupts off */
 692         save_flags (flags);
 693         cli();
 694         if (SCpnt->request.dev == dev) sleep_on( &(STp->waiting) );
 695         restore_flags(flags);
 696 
 697 
 698         if ((STp->buffer)->last_result_fatal != 0) {
 699           SCpnt->request.dev = -1;  /* Mark as not busy */
 700           printk("st%d: Error on write filemark.\n", dev);
 701         }
 702         else {
 703           SCpnt->request.dev = -1;  /* Mark as not busy */
 704           if ((STp->mt_status)->mt_fileno >= 0)
 705               (STp->mt_status)->mt_fileno++ ;
 706           STp->drv_block = 0;
 707           if (STp->two_fm)
 708             back_over_eof(dev);
 709         }
 710 
 711       }
 712 
 713 #ifdef DEBUG
 714       if (debugging)
 715         printk("st%d: Buffer flushed, %d EOF(s) written\n", dev, cmd[4]);
 716 #endif
 717     }
 718     else if (!rewind) {
 719 #ifndef ST_IN_FILE_POS
 720       if ((STp->eof == ST_FM) && !STp->eof_hit)
 721         back_over_eof(dev);
 722 #else
 723       flush_buffer(inode, filp, 0);
 724 #endif
 725     }
 726 
 727     if (rewind)
 728       st_int_ioctl(inode, filp, MTREW, 1);
 729 
 730     if (STp->buffer != NULL)
 731       (STp->buffer)->in_use = 0;
 732     STp->in_use = 0;
 733 
 734     if (scsi_tapes[dev].device->host->hostt->usage_count)
 735       (*scsi_tapes[dev].device->host->hostt->usage_count)--;
 736 
 737     return;
 738 }
 739 
 740 
 741 /* Write command */
 742         static int
 743 st_write(struct inode * inode, struct file * filp, char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 744 {
 745     int dev;
 746     int total, do_count, blks, retval, transfer;
 747     int write_threshold;
 748     int doing_write = 0;
 749     static unsigned char cmd[10];
 750     char *b_point;
 751     Scsi_Cmnd * SCpnt;
 752     Scsi_Tape * STp;
 753     unsigned int flags;
 754 
 755     dev = MINOR(inode->i_rdev) & 127;
 756     STp = &(scsi_tapes[dev]);
 757     if (STp->ready != ST_READY)
 758       return (-EIO);
 759 #ifdef DEBUG
 760     if (!STp->in_use) {
 761       printk("st%d: Incorrect device.\n", dev);
 762       return (-EIO);
 763     }
 764 #endif
 765 
 766     if (STp->write_prot)
 767       return (-EACCES);
 768 
 769     if (STp->block_size == 0 && count > st_buffer_size)
 770       return (-EOVERFLOW);
 771 
 772     if (STp->rw == ST_READING) {
 773       retval = flush_buffer(inode, filp, 0);
 774       if (retval)
 775         return retval;
 776       STp->rw = ST_WRITING;
 777     }
 778 
 779     if (STp->moves_after_eof < 255)
 780       STp->moves_after_eof++;
 781 
 782     if ((STp->buffer)->writing) {
 783       write_behind_check(dev);
 784       if ((STp->buffer)->last_result_fatal) {
 785 #ifdef DEBUG
 786         if (debugging)
 787           printk("st%d: Async write error (write) %x.\n", dev,
 788                  (STp->buffer)->last_result);
 789 #endif
 790         if ((STp->buffer)->last_result == INT_MAX) {
 791           retval = (-ENOSPC);  /* All has been written */
 792           STp->eof = ST_EOM_OK;
 793         }
 794         else
 795           retval = (-EIO);
 796         return retval;
 797       }
 798     }
 799 
 800     if (STp->eof == ST_EOM_OK)
 801       return (-ENOSPC);
 802     else if (STp->eof == ST_EOM_ERROR)
 803       return (-EIO);
 804 
 805     if (!STp->do_buffer_writes) {
 806       if (STp->block_size != 0 && (count % STp->block_size) != 0)
 807         return (-EIO);   /* Write must be integral number of blocks */
 808       write_threshold = 1;
 809     }
 810     else
 811       write_threshold = (STp->buffer)->buffer_size;
 812     if (!STp->do_async_writes)
 813       write_threshold--;
 814 
 815     SCpnt = allocate_device(NULL, STp->device, 1);
 816 
 817     total = count;
 818 
 819     memset(cmd, 0, 10);
 820     cmd[0] = WRITE_6;
 821     cmd[1] = (STp->block_size != 0);
 822 
 823     STp->rw = ST_WRITING;
 824 
 825     b_point = buf;
 826     while((STp->block_size == 0 && !STp->do_async_writes && count > 0) ||
 827           (STp->block_size != 0 &&
 828            (STp->buffer)->buffer_bytes + count > write_threshold))
 829     {
 830       doing_write = 1;
 831       if (STp->block_size == 0)
 832         do_count = count;
 833       else {
 834         do_count = (STp->buffer)->buffer_size - (STp->buffer)->buffer_bytes;
 835         if (do_count > count)
 836           do_count = count;
 837       }
 838       memcpy_fromfs((STp->buffer)->b_data +
 839                     (STp->buffer)->buffer_bytes, b_point, do_count);
 840 
 841       if (STp->block_size == 0)
 842         blks = transfer = do_count;
 843       else {
 844         blks = ((STp->buffer)->buffer_bytes + do_count) /
 845           STp->block_size;
 846         transfer = blks * STp->block_size;
 847       }
 848       cmd[2] = blks >> 16;
 849       cmd[3] = blks >> 8;
 850       cmd[4] = blks;
 851       SCpnt->sense_buffer[0] = 0;
 852       SCpnt->request.dev = dev;
 853       scsi_do_cmd (SCpnt,
 854                    (void *) cmd, (STp->buffer)->b_data, transfer,
 855                    st_sleep_done, ST_TIMEOUT, MAX_WRITE_RETRIES);
 856 
 857 
 858         /* this must be done with interrupts off */
 859         save_flags (flags);
 860         cli();
 861         if (SCpnt->request.dev == dev) sleep_on( &(STp->waiting) );
 862         restore_flags(flags);
 863 
 864       if ((STp->buffer)->last_result_fatal != 0) {
 865 #ifdef DEBUG
 866         if (debugging)
 867           printk("st%d: Error on write:\n", dev);
 868 #endif
 869         if ((SCpnt->sense_buffer[0] & 0x70) == 0x70 &&
 870             (SCpnt->sense_buffer[2] & 0x40)) {
 871           if (STp->block_size != 0 && (SCpnt->sense_buffer[0] & 0x80) != 0)
 872             transfer = (SCpnt->sense_buffer[3] << 24) |
 873               (SCpnt->sense_buffer[4] << 16) |
 874                 (SCpnt->sense_buffer[5] << 8) | SCpnt->sense_buffer[6];
 875           else if (STp->block_size == 0 &&
 876                    (SCpnt->sense_buffer[2] & 0x0f) == VOLUME_OVERFLOW)
 877             transfer = do_count;
 878           else
 879             transfer = 0;
 880           if (STp->block_size != 0)
 881             transfer *= STp->block_size;
 882           if (transfer <= do_count) {
 883             filp->f_pos += do_count - transfer;
 884             count -= do_count - transfer;
 885             if (STp->drv_block >= 0) {
 886               if (STp->block_size == 0 && transfer < do_count)
 887                 STp->drv_block++;
 888               else if (STp->block_size != 0)
 889                 STp->drv_block += (do_count - transfer) / STp->block_size;
 890             }
 891             STp->eof = ST_EOM_OK;
 892             retval = (-ENOSPC); /* EOM within current request */
 893 #ifdef DEBUG
 894             if (debugging)
 895               printk("st%d: EOM with %d bytes unwritten.\n",
 896                      dev, transfer);
 897 #endif
 898           }
 899           else {
 900             STp->eof = ST_EOM_ERROR;
 901             STp->drv_block = (-1);    /* Too cautious? */
 902             retval = (-EIO); /* EOM for old data */
 903 #ifdef DEBUG
 904             if (debugging)
 905               printk("st%d: EOM with lost data.\n", dev);
 906 #endif
 907           }
 908         }
 909         else {
 910           STp->drv_block = (-1);    /* Too cautious? */
 911           retval = (-EIO);
 912         }
 913 
 914         SCpnt->request.dev = -1;  /* Mark as not busy */
 915         (STp->buffer)->buffer_bytes = 0;
 916         STp->dirty = 0;
 917         if (count < total)
 918           return total - count;
 919         else
 920           return retval;
 921       }
 922       filp->f_pos += do_count;
 923       b_point += do_count;
 924       count -= do_count;
 925       if (STp->drv_block >= 0) {
 926         if (STp->block_size == 0)
 927           STp->drv_block++;
 928         else
 929           STp->drv_block += blks;
 930       }
 931       (STp->buffer)->buffer_bytes = 0;
 932       STp->dirty = 0;
 933     }
 934     if (count != 0) {
 935       STp->dirty = 1;
 936       memcpy_fromfs((STp->buffer)->b_data +
 937                     (STp->buffer)->buffer_bytes,b_point,count);
 938       filp->f_pos += count;
 939       (STp->buffer)->buffer_bytes += count;
 940       count = 0;
 941     }
 942 
 943     if (doing_write && (STp->buffer)->last_result_fatal != 0) {
 944       SCpnt->request.dev = -1;
 945       return (STp->buffer)->last_result_fatal;
 946     }
 947 
 948     if (STp->do_async_writes &&
 949         ((STp->buffer)->buffer_bytes >= STp->write_threshold ||
 950          STp->block_size == 0) ) {
 951       /* Schedule an asynchronous write */
 952       if (STp->block_size == 0)
 953         (STp->buffer)->writing = (STp->buffer)->buffer_bytes;
 954       else
 955         (STp->buffer)->writing = ((STp->buffer)->buffer_bytes /
 956           STp->block_size) * STp->block_size;
 957       STp->dirty = !((STp->buffer)->writing ==
 958                      (STp->buffer)->buffer_bytes);
 959 
 960       if (STp->block_size == 0)
 961         blks = (STp->buffer)->writing;
 962       else
 963         blks = (STp->buffer)->writing / STp->block_size;
 964       cmd[2] = blks >> 16;
 965       cmd[3] = blks >> 8;
 966       cmd[4] = blks;
 967       SCpnt->result = (STp->buffer)->last_result = -1;
 968       SCpnt->sense_buffer[0] = 0;
 969       SCpnt->request.dev = dev;
 970       scsi_do_cmd (SCpnt,
 971                    (void *) cmd, (STp->buffer)->b_data,
 972                    (STp->buffer)->writing,
 973                    st_sleep_done, ST_TIMEOUT, MAX_WRITE_RETRIES);
 974     }
 975     else
 976       SCpnt->request.dev = -1;  /* Mark as not busy */
 977 
 978     STp->at_sm &= (total != 0);
 979     return( total);
 980 }   
 981 
 982 
 983 /* Read command */
 984         static int
 985 st_read(struct inode * inode, struct file * filp, char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 986 {
 987     int dev;
 988     int total;
 989     int transfer, blks, bytes;
 990     static unsigned char cmd[10];
 991     Scsi_Cmnd * SCpnt;
 992     Scsi_Tape * STp;
 993     unsigned int flags;
 994 
 995     dev = MINOR(inode->i_rdev) & 127;
 996     STp = &(scsi_tapes[dev]);
 997     if (STp->ready != ST_READY)
 998       return (-EIO);
 999 #ifdef DEBUG
1000     if (!STp->in_use) {
1001       printk("st%d: Incorrect device.\n", dev);
1002       return (-EIO);
1003     }
1004 #endif
1005 
1006     if (STp->block_size == 0 && count > st_buffer_size)
1007       return (-EOVERFLOW);
1008 
1009     if (!(STp->do_read_ahead) && STp->block_size != 0 &&
1010         (count % STp->block_size) != 0)
1011       return (-EIO);    /* Read must be integral number of blocks */
1012 
1013     if (STp->rw == ST_WRITING) {
1014       transfer = flush_buffer(inode, filp, 0);
1015       if (transfer)
1016         return transfer;
1017       STp->rw = ST_READING;
1018     }
1019     if (STp->moves_after_eof < 255)
1020       STp->moves_after_eof++;
1021 
1022 #ifdef DEBUG
1023     if (debugging && STp->eof != ST_NOEOF)
1024       printk("st%d: EOF flag up. Bytes %d\n", dev,
1025              (STp->buffer)->buffer_bytes);
1026 #endif
1027     if (((STp->buffer)->buffer_bytes == 0) &&
1028         (STp->eof == ST_EOM_OK || STp->eof == ST_EOD))
1029       return (-EIO);  /* EOM or Blank Check */
1030 
1031     STp->rw = ST_READING;
1032 
1033     SCpnt = allocate_device(NULL, STp->device, 1);
1034 
1035     for (total = 0; total < count; ) {
1036 
1037       if ((STp->buffer)->buffer_bytes == 0 &&
1038           STp->eof == ST_NOEOF) {
1039 
1040         memset(cmd, 0, 10);
1041         cmd[0] = READ_6;
1042         cmd[1] = (STp->block_size != 0);
1043         if (STp->block_size == 0)
1044           blks = bytes = count;
1045         else {
1046           if (STp->do_read_ahead) {
1047             blks = (STp->buffer)->buffer_blocks;
1048             bytes = blks * STp->block_size;
1049           }
1050           else {
1051             bytes = count;
1052             if (bytes > st_buffer_size)
1053               bytes = st_buffer_size;
1054             blks = bytes / STp->block_size;
1055             bytes = blks * STp->block_size;
1056           }
1057         }
1058         cmd[2] = blks >> 16;
1059         cmd[3] = blks >> 8;
1060         cmd[4] = blks;
1061 
1062         SCpnt->sense_buffer[0] = 0;
1063         SCpnt->request.dev = dev;
1064         scsi_do_cmd (SCpnt,
1065                      (void *) cmd, (STp->buffer)->b_data,
1066                      (STp->buffer)->buffer_size,
1067                      st_sleep_done, ST_TIMEOUT, MAX_RETRIES);
1068 
1069 
1070         /* this must be done with interrupts off */
1071         save_flags (flags);
1072         cli();
1073         if (SCpnt->request.dev == dev) sleep_on( &(STp->waiting) );
1074         restore_flags(flags);
1075 
1076         (STp->buffer)->read_pointer = 0;
1077         STp->eof_hit = 0;
1078         STp->at_sm = 0;
1079 
1080         if ((STp->buffer)->last_result_fatal) {
1081 #ifdef DEBUG
1082           if (debugging)
1083             printk("st%d: Sense: %2x %2x %2x %2x %2x %2x %2x %2x\n", dev,
1084                    SCpnt->sense_buffer[0], SCpnt->sense_buffer[1],
1085                    SCpnt->sense_buffer[2], SCpnt->sense_buffer[3],
1086                    SCpnt->sense_buffer[4], SCpnt->sense_buffer[5],
1087                    SCpnt->sense_buffer[6], SCpnt->sense_buffer[7]);
1088 #endif
1089           if ((SCpnt->sense_buffer[0] & 0x70) == 0x70) { /* extended sense */
1090 
1091             if ((SCpnt->sense_buffer[2] & 0xe0) != 0) { /* EOF, EOM, or ILI */
1092 
1093               if ((SCpnt->sense_buffer[0] & 0x80) != 0)
1094                 transfer = (SCpnt->sense_buffer[3] << 24) |
1095                   (SCpnt->sense_buffer[4] << 16) |
1096                     (SCpnt->sense_buffer[5] << 8) | SCpnt->sense_buffer[6];
1097               else
1098                 transfer = 0;
1099               if (STp->block_size == 0 &&
1100                   (SCpnt->sense_buffer[2] & 0x0f) == MEDIUM_ERROR)
1101                 transfer = bytes;
1102 
1103               if (SCpnt->sense_buffer[2] & 0x20) {
1104                 if (STp->block_size == 0) {
1105                   if (transfer <= 0)
1106                     transfer = 0;
1107                   (STp->buffer)->buffer_bytes = bytes - transfer;
1108                 }
1109                 else {
1110                   printk("st%d: Incorrect block size.\n", dev);
1111                   SCpnt->request.dev = -1;  /* Mark as not busy */
1112                   return (-EIO);
1113                 }
1114               }
1115               else if (SCpnt->sense_buffer[2] & 0x40) {
1116                 STp->eof = ST_EOM_OK;
1117                 if (STp->block_size == 0)
1118                   (STp->buffer)->buffer_bytes = bytes - transfer;
1119                 else
1120                   (STp->buffer)->buffer_bytes =
1121                     bytes - transfer * STp->block_size;
1122 #ifdef DEBUG
1123                 if (debugging)
1124                   printk("st%d: EOM detected (%d bytes read).\n", dev,
1125                          (STp->buffer)->buffer_bytes);
1126 #endif
1127               }
1128               else if (SCpnt->sense_buffer[2] & 0x80) {
1129                 STp->eof = ST_FM;
1130                 if (STp->block_size == 0)
1131                   (STp->buffer)->buffer_bytes = 0;
1132                 else
1133                   (STp->buffer)->buffer_bytes =
1134                     bytes - transfer * STp->block_size;
1135 #ifdef DEBUG
1136                 if (debugging)
1137                   printk(
1138                     "st%d: EOF detected (%d bytes read, transferred %d bytes).\n",
1139                          dev, (STp->buffer)->buffer_bytes, total);
1140 #endif
1141               }
1142             } /* end of EOF, EOM, ILI test */
1143             else { /* nonzero sense key */
1144 #ifdef DEBUG
1145               if (debugging)
1146                 printk("st%d: Tape error while reading.\n", dev);
1147 #endif
1148               SCpnt->request.dev = -1;
1149               STp->drv_block = (-1);
1150               if (total)
1151                 return total;
1152               else if (STp->moves_after_eof == 1 &&
1153                        (SCpnt->sense_buffer[2] & 0x0f) == BLANK_CHECK) {
1154 #ifdef DEBUG
1155                 if (debugging)
1156                   printk("st%d: Zero returned for first BLANK CHECK after EOF.\n",
1157                          dev);
1158 #endif
1159                 STp->eof = ST_EOD;
1160                 return 0; /* First BLANK_CHECK after EOF */
1161               }
1162               else
1163                 return -EIO;
1164             }
1165           } /* End of extended sense test */
1166           else {
1167             transfer = (STp->buffer)->last_result_fatal;
1168             SCpnt->request.dev = -1;  /* Mark as not busy */
1169             return transfer;
1170           }
1171         } /* End of error handling */
1172         else /* Read successful */
1173           (STp->buffer)->buffer_bytes = bytes;
1174 
1175         if (STp->drv_block >= 0) {
1176           if (STp->block_size == 0)
1177             STp->drv_block++;
1178           else
1179             STp->drv_block += (STp->buffer)->buffer_bytes / STp->block_size;
1180         }
1181 
1182       } /* if ((STp->buffer)->buffer_bytes == 0 &&
1183            STp->eof == ST_NOEOF) */
1184 
1185       if ((STp->buffer)->buffer_bytes > 0) {
1186 #ifdef DEBUG
1187         if (debugging && STp->eof != ST_NOEOF)
1188           printk("st%d: EOF up. Left %d, needed %d.\n", dev,
1189                  (STp->buffer)->buffer_bytes, count - total);
1190 #endif
1191         transfer = (STp->buffer)->buffer_bytes < count - total ?
1192           (STp->buffer)->buffer_bytes : count - total;
1193         memcpy_tofs(buf, (STp->buffer)->b_data +
1194                     (STp->buffer)->read_pointer,transfer);
1195         filp->f_pos += transfer;
1196         buf += transfer;
1197         total += transfer;
1198         (STp->buffer)->buffer_bytes -= transfer;
1199         (STp->buffer)->read_pointer += transfer;
1200       }
1201       else if (STp->eof != ST_NOEOF) {
1202         STp->eof_hit = 1;
1203         SCpnt->request.dev = -1;  /* Mark as not busy */
1204         if (total == 0 && STp->eof == ST_FM) {
1205           STp->eof = ST_NOEOF;
1206           STp->drv_block = 0;
1207           if (STp->moves_after_eof > 1)
1208             STp->moves_after_eof = 0;
1209           if ((STp->mt_status)->mt_fileno >= 0)
1210             (STp->mt_status)->mt_fileno++;
1211         }
1212         if (total == 0 && STp->eof == ST_EOM_OK)
1213           return (-EIO);  /* ST_EOM_ERROR not used in read */
1214         return total;
1215       }
1216 
1217       if (STp->block_size == 0)
1218         count = total;  /* Read only one variable length block */
1219 
1220     } /* for (total = 0; total < count; ) */
1221 
1222     SCpnt->request.dev = -1;  /* Mark as not busy */
1223 
1224     return total;
1225 }
1226 
1227 
1228 
1229 /* Set the driver options */
1230         static int
1231 st_set_options(struct inode * inode, long options)
     /* [previous][next][first][last][top][bottom][index][help] */
1232 {
1233   int dev, value;
1234   Scsi_Tape *STp;
1235 
1236   dev = MINOR(inode->i_rdev) & 127;
1237   STp = &(scsi_tapes[dev]);
1238   if ((options & MT_ST_OPTIONS) == MT_ST_BOOLEANS) {
1239     STp->do_buffer_writes = (options & MT_ST_BUFFER_WRITES) != 0;
1240     STp->do_async_writes  = (options & MT_ST_ASYNC_WRITES) != 0;
1241     STp->do_read_ahead    = (options & MT_ST_READ_AHEAD) != 0;
1242     STp->two_fm           = (options & MT_ST_TWO_FM) != 0;
1243     STp->fast_mteom       = (options & MT_ST_FAST_MTEOM) != 0;
1244 #ifdef DEBUG
1245     debugging = (options & MT_ST_DEBUGGING) != 0;
1246     printk(
1247 "st%d: options: buffer writes: %d, async writes: %d, read ahead: %d\n",
1248            dev, STp->do_buffer_writes, STp->do_async_writes,
1249            STp->do_read_ahead);
1250     printk("              two FMs: %d, fast mteom: %d debugging: %d\n",
1251            STp->two_fm, STp->fast_mteom, debugging);
1252 #endif
1253   }
1254   else if ((options & MT_ST_OPTIONS) == MT_ST_WRITE_THRESHOLD) {
1255     value = (options & ~MT_ST_OPTIONS) * ST_BLOCK_SIZE;
1256     if (value < 1 || value > st_buffer_size) {
1257       printk("st: Write threshold %d too small or too large.\n",
1258              value);
1259       return (-EIO);
1260     }
1261     STp->write_threshold = value;
1262 #ifdef DEBUG
1263     printk("st%d: Write threshold set to %d bytes.\n", dev,
1264            STp->write_threshold);
1265 #endif
1266   }
1267   else
1268     return (-EIO);
1269 
1270   return 0;
1271 }
1272 
1273 
1274 /* Internal ioctl function */
1275         static int
1276 st_int_ioctl(struct inode * inode,struct file * file,
     /* [previous][next][first][last][top][bottom][index][help] */
1277              unsigned int cmd_in, unsigned long arg)
1278 {
1279    int dev = MINOR(inode->i_rdev);
1280    int timeout = ST_LONG_TIMEOUT;
1281    long ltmp;
1282    int ioctl_result;
1283    unsigned char cmd[10];
1284    Scsi_Cmnd * SCpnt;
1285    Scsi_Tape * STp;
1286    int fileno, blkno, at_sm, undone, datalen;
1287    unsigned int flags;
1288 
1289    dev = dev & 127;
1290    STp = &(scsi_tapes[dev]);
1291    if (STp->ready != ST_READY)
1292      return (-EIO);
1293    fileno = (STp->mt_status)->mt_fileno ;
1294    blkno = STp->drv_block;
1295    at_sm = STp->at_sm;
1296 
1297    memset(cmd, 0, 10);
1298    datalen = 0;
1299    switch (cmd_in) {
1300      case MTFSF:
1301      case MTFSFM:
1302        cmd[0] = SPACE;
1303        cmd[1] = 0x01; /* Space FileMarks */
1304        cmd[2] = (arg >> 16);
1305        cmd[3] = (arg >> 8);
1306        cmd[4] = arg;
1307 #ifdef DEBUG
1308        if (debugging)
1309          printk("st%d: Spacing tape forward over %d filemarks.\n", dev,
1310                 cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
1311 #endif
1312        if (fileno >= 0)
1313          fileno += arg;
1314        blkno = 0;
1315        at_sm &= (arg != 0);
1316        break; 
1317      case MTBSF:
1318      case MTBSFM:
1319        cmd[0] = SPACE;
1320        cmd[1] = 0x01; /* Space FileMarks */
1321        ltmp = (-arg);
1322        cmd[2] = (ltmp >> 16);
1323        cmd[3] = (ltmp >> 8);
1324        cmd[4] = ltmp;
1325 #ifdef DEBUG
1326        if (debugging) {
1327          if (cmd[2] & 0x80)
1328            ltmp = 0xff000000;
1329          ltmp = ltmp | (cmd[2] << 16) | (cmd[3] << 8) | cmd[4];
1330          printk("st%d: Spacing tape backward over %ld filemarks.\n", dev, (-ltmp));
1331        }
1332 #endif
1333        if (fileno >= 0)
1334          fileno -= arg;
1335        blkno = (-1);  /* We can't know the block number */
1336        at_sm &= (arg != 0);
1337        break; 
1338      case MTFSR:
1339        cmd[0] = SPACE;
1340        cmd[1] = 0x00; /* Space Blocks */
1341        cmd[2] = (arg >> 16);
1342        cmd[3] = (arg >> 8);
1343        cmd[4] = arg;
1344 #ifdef DEBUG
1345        if (debugging)
1346          printk("st%d: Spacing tape forward %d blocks.\n", dev,
1347                 cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
1348 #endif
1349        if (blkno >= 0)
1350          blkno += arg;
1351        at_sm &= (arg != 0);
1352        break; 
1353      case MTBSR:
1354        cmd[0] = SPACE;
1355        cmd[1] = 0x00; /* Space Blocks */
1356        ltmp = (-arg);
1357        cmd[2] = (ltmp >> 16);
1358        cmd[3] = (ltmp >> 8);
1359        cmd[4] = ltmp;
1360 #ifdef DEBUG
1361        if (debugging) {
1362          if (cmd[2] & 0x80)
1363            ltmp = 0xff000000;
1364          ltmp = ltmp | (cmd[2] << 16) | (cmd[3] << 8) | cmd[4];
1365          printk("st%d: Spacing tape backward %ld blocks.\n", dev, (-ltmp));
1366        }
1367 #endif
1368        if (blkno >= 0)
1369          blkno -= arg;
1370        at_sm &= (arg != 0);
1371        break; 
1372      case MTFSS:
1373        cmd[0] = SPACE;
1374        cmd[1] = 0x04; /* Space Setmarks */
1375        cmd[2] = (arg >> 16);
1376        cmd[3] = (arg >> 8);
1377        cmd[4] = arg;
1378 #ifdef DEBUG
1379        if (debugging)
1380          printk("st%d: Spacing tape forward %d setmarks.\n", dev,
1381                 cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
1382 #endif
1383        if (arg != 0) {
1384          blkno = fileno = (-1);
1385          at_sm = 1;
1386        }
1387        break; 
1388      case MTBSS:
1389        cmd[0] = SPACE;
1390        cmd[1] = 0x04; /* Space Setmarks */
1391        ltmp = (-arg);
1392        cmd[2] = (ltmp >> 16);
1393        cmd[3] = (ltmp >> 8);
1394        cmd[4] = ltmp;
1395 #ifdef DEBUG
1396        if (debugging) {
1397          if (cmd[2] & 0x80)
1398            ltmp = 0xff000000;
1399          ltmp = ltmp | (cmd[2] << 16) | (cmd[3] << 8) | cmd[4];
1400          printk("st%d: Spacing tape backward %ld setmarks.\n", dev, (-ltmp));
1401        }
1402 #endif
1403        if (arg != 0) {
1404          blkno = fileno = (-1);
1405          at_sm = 1;
1406        }
1407        break; 
1408      case MTWEOF:
1409      case MTWSM:
1410        if (STp->write_prot)
1411          return (-EACCES);
1412        cmd[0] = WRITE_FILEMARKS;
1413        if (cmd_in == MTWSM)
1414          cmd[1] = 2;
1415        cmd[2] = (arg >> 16);
1416        cmd[3] = (arg >> 8);
1417        cmd[4] = arg;
1418        timeout = ST_TIMEOUT;
1419 #ifdef DEBUG
1420        if (debugging) {
1421          if (cmd_in == MTWEOF)
1422            printk("st%d: Writing %d filemarks.\n", dev,
1423                   cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
1424          else
1425            printk("st%d: Writing %d setmarks.\n", dev,
1426                   cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
1427        }
1428 #endif
1429        if (fileno >= 0)
1430          fileno += arg;
1431        blkno = 0;
1432        at_sm = (cmd_in == MTWSM);
1433        break; 
1434      case MTREW:
1435        cmd[0] = REZERO_UNIT;
1436 #ifdef ST_NOWAIT
1437        cmd[1] = 1;  /* Don't wait for completion */
1438        timeout = ST_TIMEOUT;
1439 #endif
1440 #ifdef DEBUG
1441        if (debugging)
1442          printk("st%d: Rewinding tape.\n", dev);
1443 #endif
1444        fileno = blkno = at_sm = 0 ;
1445        break; 
1446      case MTOFFL:
1447        cmd[0] = START_STOP;
1448 #ifdef ST_NOWAIT
1449        cmd[1] = 1;  /* Don't wait for completion */
1450        timeout = ST_TIMEOUT;
1451 #endif
1452 #ifdef DEBUG
1453        if (debugging)
1454          printk("st%d: Unloading tape.\n", dev);
1455 #endif
1456        fileno = blkno = at_sm = 0 ;
1457        break; 
1458      case MTNOP:
1459 #ifdef DEBUG
1460        if (debugging)
1461          printk("st%d: No op on tape.\n", dev);
1462 #endif
1463        return 0;  /* Should do something ? */
1464        break;
1465      case MTRETEN:
1466        cmd[0] = START_STOP;
1467 #ifdef ST_NOWAIT
1468        cmd[1] = 1;  /* Don't wait for completion */
1469        timeout = ST_TIMEOUT;
1470 #endif
1471        cmd[4] = 3;
1472 #ifdef DEBUG
1473        if (debugging)
1474          printk("st%d: Retensioning tape.\n", dev);
1475 #endif
1476        fileno = blkno = at_sm = 0;
1477        break; 
1478      case MTEOM:
1479        if (!STp->fast_mteom) {
1480          /* space to the end of tape */
1481          ioctl_result = st_int_ioctl(inode, file, MTFSF, 0x3fff);
1482          fileno = (STp->mt_status)->mt_fileno ;
1483          if (STp->eof == ST_EOD || STp->eof == ST_EOM_OK)
1484            return 0;
1485          /* The next lines would hide the number of spaced FileMarks
1486             That's why I inserted the previous lines. I had no luck
1487             with detecting EOM with FSF, so we go now to EOM.
1488             Joerg Weule */
1489        }
1490        else
1491          fileno = (-1);
1492        cmd[0] = SPACE;
1493        cmd[1] = 3;
1494 #ifdef DEBUG
1495        if (debugging)
1496          printk("st%d: Spacing to end of recorded medium.\n", dev);
1497 #endif
1498        blkno = 0;
1499        at_sm = 0;
1500        break; 
1501      case MTERASE:
1502        if (STp->write_prot)
1503          return (-EACCES);
1504        cmd[0] = ERASE;
1505        cmd[1] = 1;  /* To the end of tape */
1506 #ifdef ST_NOWAIT
1507        cmd[1] |= 2;  /* Don't wait for completion */
1508        timeout = ST_TIMEOUT;
1509 #else
1510        timeout = ST_LONG_TIMEOUT * 8;
1511 #endif
1512 #ifdef DEBUG
1513        if (debugging)
1514          printk("st%d: Erasing tape.\n", dev);
1515 #endif
1516        fileno = blkno = at_sm = 0 ;
1517        break;
1518      case MTSEEK:
1519        if ((STp->device)->scsi_level < SCSI_2) {
1520          cmd[0] = QFA_SEEK_BLOCK;
1521          cmd[2] = (arg >> 16);
1522          cmd[3] = (arg >> 8);
1523          cmd[4] = arg;
1524          cmd[5] = 0;
1525        }
1526        else {
1527          cmd[0] = SEEK_10;
1528          cmd[1] = 4;
1529          cmd[3] = (arg >> 24);
1530          cmd[4] = (arg >> 16);
1531          cmd[5] = (arg >> 8);
1532          cmd[6] = arg;
1533        }
1534 #ifdef ST_NOWAIT
1535        cmd[1] |= 1;  /* Don't wait for completion */
1536        timeout = ST_TIMEOUT;
1537 #endif
1538 #ifdef DEBUG
1539        if (debugging)
1540          printk("st%d: Seeking tape to block %ld.\n", dev, arg);
1541 #endif
1542        fileno = blkno = (-1);
1543        at_sm = 0;
1544        break;
1545      case MTSETBLK:  /* Set block length */
1546      case MTSETDENSITY: /* Set tape density */
1547      case MTSETDRVBUFFER: /* Set drive buffering */
1548        if (STp->dirty || (STp->buffer)->buffer_bytes != 0)
1549          return (-EIO);   /* Not allowed if data in buffer */
1550        if (cmd_in == MTSETBLK &&
1551            arg != 0 &&
1552            (arg < STp->min_block || arg > STp->max_block ||
1553             arg > st_buffer_size)) {
1554          printk("st%d: Illegal block size.\n", dev);
1555          return (-EINVAL);
1556        }
1557        cmd[0] = MODE_SELECT;
1558        cmd[4] = datalen = 12;
1559 
1560        memset((STp->buffer)->b_data, 0, 12);
1561        if (cmd_in == MTSETDRVBUFFER)
1562          (STp->buffer)->b_data[2] = (arg & 7) << 4;
1563        else
1564          (STp->buffer)->b_data[2] = 
1565            STp->drv_buffer << 4;
1566        (STp->buffer)->b_data[3] = 8;     /* block descriptor length */
1567        if (cmd_in == MTSETDENSITY)
1568          (STp->buffer)->b_data[4] = arg;
1569        else
1570          (STp->buffer)->b_data[4] = STp->density;
1571        if (cmd_in == MTSETBLK)
1572          ltmp = arg;
1573        else
1574          ltmp = STp->block_size;
1575        (STp->buffer)->b_data[9] = (ltmp >> 16);
1576        (STp->buffer)->b_data[10] = (ltmp >> 8);
1577        (STp->buffer)->b_data[11] = ltmp;
1578        timeout = ST_TIMEOUT;
1579 #ifdef DEBUG
1580        if (debugging) {
1581          if (cmd_in == MTSETBLK)
1582            printk("st%d: Setting block size to %d bytes.\n", dev,
1583                   (STp->buffer)->b_data[9] * 65536 +
1584                   (STp->buffer)->b_data[10] * 256 +
1585                   (STp->buffer)->b_data[11]);
1586          else if (cmd_in == MTSETDENSITY)
1587            printk("st%d: Setting density code to %x.\n", dev,
1588                   (STp->buffer)->b_data[4]);
1589          else
1590            printk("st%d: Setting drive buffer code to %d.\n", dev,
1591                   ((STp->buffer)->b_data[2] >> 4) & 7);
1592        }
1593 #endif
1594        break;
1595      default:
1596        printk("st%d: Unknown st_ioctl command %x.\n", dev, cmd_in);
1597        return (-ENOSYS);
1598      }
1599 
1600    SCpnt = allocate_device(NULL, STp->device, 1);
1601    SCpnt->sense_buffer[0] = 0;
1602    SCpnt->request.dev = dev;
1603    scsi_do_cmd(SCpnt,
1604                (void *) cmd, (void *) (STp->buffer)->b_data, datalen,
1605                st_sleep_done, timeout, MAX_RETRIES);
1606 
1607 
1608         /* this must be done with interrupts off */
1609         save_flags (flags);
1610         cli();
1611         if (SCpnt->request.dev == dev) sleep_on( &(STp->waiting) );
1612         restore_flags(flags);
1613 
1614 
1615    ioctl_result = (STp->buffer)->last_result_fatal;
1616 
1617    SCpnt->request.dev = -1;  /* Mark as not busy */
1618 
1619    if (cmd_in == MTFSF)
1620      STp->moves_after_eof = 0;
1621    else
1622      STp->moves_after_eof = 1;
1623    if (!ioctl_result) {  /* SCSI command successful */
1624      if (cmd_in != MTSEEK) {
1625        STp->drv_block = blkno;
1626        (STp->mt_status)->mt_fileno = fileno;
1627        STp->at_sm = at_sm;
1628      }
1629      else {
1630        STp->drv_block = (STp->mt_status)->mt_fileno = (-1);
1631        STp->at_sm = 0;
1632      }
1633      if (cmd_in == MTBSFM)
1634        ioctl_result = st_int_ioctl(inode, file, MTFSF, 1);
1635      else if (cmd_in == MTFSFM)
1636        ioctl_result = st_int_ioctl(inode, file, MTBSF, 1);
1637      else if (cmd_in == MTSETBLK) {
1638        STp->block_size = arg;
1639        if (arg != 0) {
1640          (STp->buffer)->buffer_blocks =
1641            st_buffer_size / STp->block_size;
1642          (STp->buffer)->buffer_size =
1643            (STp->buffer)->buffer_blocks * STp->block_size;
1644        }
1645        else {
1646          (STp->buffer)->buffer_blocks = 1;
1647          (STp->buffer)->buffer_size = st_buffer_size;
1648        }
1649        (STp->buffer)->buffer_bytes =
1650          (STp->buffer)->read_pointer = 0;
1651      }
1652      else if (cmd_in == MTSETDRVBUFFER)
1653        STp->drv_buffer = (arg & 7);
1654      else if (cmd_in == MTSETDENSITY)
1655        STp->density = arg;
1656      else if (cmd_in == MTEOM) {
1657        STp->eof = ST_EOD;
1658        STp->eof_hit = 0;
1659      }
1660      else if (cmd_in != MTSETBLK && cmd_in != MTNOP) {
1661        STp->eof = ST_NOEOF;
1662        STp->eof_hit = 0;
1663      }
1664    } else {  /* SCSI command was not completely successful */
1665      if (SCpnt->sense_buffer[2] & 0x40) {
1666        if (cmd_in != MTBSF && cmd_in != MTBSFM &&
1667            cmd_in != MTBSR && cmd_in != MTBSS)
1668          STp->eof = ST_EOM_OK;
1669        STp->eof_hit = 0;
1670        STp->drv_block = 0;
1671      }
1672      undone = (
1673           (SCpnt->sense_buffer[3] << 24) +
1674           (SCpnt->sense_buffer[4] << 16) +
1675           (SCpnt->sense_buffer[5] << 8) +
1676           SCpnt->sense_buffer[6] );
1677      if ( (cmd_in == MTFSF) || (cmd_in == MTFSFM) ) {
1678        if (fileno >= 0)
1679          (STp->mt_status)->mt_fileno = fileno - undone ;
1680        else
1681          (STp->mt_status)->mt_fileno = fileno;
1682        STp->drv_block = 0;
1683      }
1684      else if ( (cmd_in == MTBSF) || (cmd_in == MTBSFM) ) {
1685        (STp->mt_status)->mt_fileno = fileno + undone ;
1686        STp->drv_block = 0;
1687      }
1688      else if (cmd_in == MTFSR) {
1689        if (SCpnt->sense_buffer[2] & 0x80) { /* Hit filemark */
1690          (STp->mt_status)->mt_fileno++;
1691          STp->drv_block = 0;
1692        }
1693        else {
1694          if (blkno >= undone)
1695            STp->drv_block = blkno - undone;
1696          else
1697            STp->drv_block = (-1);
1698        }
1699      }
1700      else if (cmd_in == MTBSR) {
1701        if (SCpnt->sense_buffer[2] & 0x80) { /* Hit filemark */
1702          (STp->mt_status)->mt_fileno--;
1703          STp->drv_block = (-1);
1704        }
1705        else {
1706          if (blkno >= 0)
1707            STp->drv_block = blkno + undone;
1708          else
1709            STp->drv_block = (-1);
1710        }
1711      }
1712      else if (cmd_in == MTEOM || cmd_in == MTSEEK) {
1713        (STp->mt_status)->mt_fileno = (-1);
1714        STp->drv_block = (-1);
1715      }
1716      if (STp->eof == ST_NOEOF &&
1717          (SCpnt->sense_buffer[2] & 0x0f) == BLANK_CHECK)
1718        STp->eof = ST_EOD;
1719    }
1720 
1721    return ioctl_result;
1722 }
1723 
1724 
1725 
1726 /* The ioctl command */
1727         static int
1728 st_ioctl(struct inode * inode,struct file * file,
     /* [previous][next][first][last][top][bottom][index][help] */
1729          unsigned int cmd_in, unsigned long arg)
1730 {
1731    int dev = MINOR(inode->i_rdev);
1732    int i, cmd, result;
1733    struct mtop mtc;
1734    struct mtpos mt_pos;
1735    unsigned char scmd[10];
1736    Scsi_Cmnd *SCpnt;
1737    Scsi_Tape *STp;
1738    unsigned int flags;
1739 
1740    dev = dev & 127;
1741    STp = &(scsi_tapes[dev]);
1742 #ifdef DEBUG
1743    if (debugging && !STp->in_use) {
1744      printk("st%d: Incorrect device.\n", dev);
1745      return (-EIO);
1746    }
1747 #endif
1748 
1749    cmd = cmd_in & IOCCMD_MASK;
1750    if (cmd == (MTIOCTOP & IOCCMD_MASK)) {
1751 
1752      if (((cmd_in & IOCSIZE_MASK) >> IOCSIZE_SHIFT) != sizeof(mtc))
1753        return (-EINVAL);
1754 
1755      i = verify_area(VERIFY_WRITE, (void *)arg, sizeof(mtc));
1756      if (i)
1757         return i;
1758 
1759      memcpy_fromfs((char *) &mtc, (char *)arg, sizeof(struct mtop));
1760 
1761      i = flush_buffer(inode, file, mtc.mt_op == MTSEEK ||
1762                       mtc.mt_op == MTREW || mtc.mt_op == MTOFFL ||
1763                       mtc.mt_op == MTRETEN || mtc.mt_op == MTEOM);
1764      if (i < 0)
1765        return i;
1766      if (mtc.mt_op != MTNOP && mtc.mt_op != MTSETBLK &&
1767          mtc.mt_op != MTSETDENSITY && mtc.mt_op != MTWSM &&
1768          mtc.mt_op != MTSETDRVBUFFER)
1769        STp->rw = ST_IDLE;  /* Prevent automatic WEOF */
1770 
1771      if (mtc.mt_op == MTSETDRVBUFFER &&
1772          (mtc.mt_count & MT_ST_OPTIONS) != 0)
1773        return st_set_options(inode, mtc.mt_count);
1774      else
1775        return st_int_ioctl(inode, file, mtc.mt_op, mtc.mt_count);
1776    }
1777    else if (cmd == (MTIOCGET & IOCCMD_MASK)) {
1778 
1779      if (((cmd_in & IOCSIZE_MASK) >> IOCSIZE_SHIFT) != sizeof(struct mtget))
1780        return (-EINVAL);
1781      i = verify_area(VERIFY_WRITE, (void *)arg, sizeof(struct mtget));
1782      if (i)
1783        return i;
1784 
1785      (STp->mt_status)->mt_dsreg =
1786        ((STp->block_size << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK) |
1787        ((STp->density << MT_ST_DENSITY_SHIFT) & MT_ST_DENSITY_MASK);
1788      (STp->mt_status)->mt_blkno = STp->drv_block;
1789      if (STp->block_size != 0) {
1790        if (STp->rw == ST_WRITING)
1791          (STp->mt_status)->mt_blkno +=
1792            (STp->buffer)->buffer_bytes / STp->block_size;
1793        else if (STp->rw == ST_READING)
1794          (STp->mt_status)->mt_blkno -= ((STp->buffer)->buffer_bytes +
1795            STp->block_size - 1) / STp->block_size;
1796      }
1797 
1798      (STp->mt_status)->mt_gstat = 0;
1799      if (STp->drv_write_prot)
1800        (STp->mt_status)->mt_gstat |= GMT_WR_PROT(0xffffffff);
1801      if ((STp->mt_status)->mt_blkno == 0) {
1802        if ((STp->mt_status)->mt_fileno == 0)
1803          (STp->mt_status)->mt_gstat |= GMT_BOT(0xffffffff);
1804        else
1805          (STp->mt_status)->mt_gstat |= GMT_EOF(0xffffffff);
1806      }
1807      if (STp->eof == ST_EOM_OK || STp->eof == ST_EOM_ERROR)
1808        (STp->mt_status)->mt_gstat |= GMT_EOT(0xffffffff);
1809      else if (STp->eof == ST_EOD)
1810        (STp->mt_status)->mt_gstat |= GMT_EOD(0xffffffff);
1811      if (STp->density == 1)
1812        (STp->mt_status)->mt_gstat |= GMT_D_800(0xffffffff);
1813      else if (STp->density == 2)
1814        (STp->mt_status)->mt_gstat |= GMT_D_1600(0xffffffff);
1815      else if (STp->density == 3)
1816        (STp->mt_status)->mt_gstat |= GMT_D_6250(0xffffffff);
1817      if (STp->ready == ST_READY)
1818        (STp->mt_status)->mt_gstat |= GMT_ONLINE(0xffffffff);
1819      if (STp->ready == ST_NO_TAPE)
1820        (STp->mt_status)->mt_gstat |= GMT_DR_OPEN(0xffffffff);
1821      if (STp->at_sm)
1822        (STp->mt_status)->mt_gstat |= GMT_SM(0xffffffff);
1823 
1824      memcpy_tofs((char *)arg, (char *)(STp->mt_status),
1825                  sizeof(struct mtget));
1826 
1827      (STp->mt_status)->mt_erreg = 0;  /* Clear after read */
1828      return 0;
1829    }
1830    else if (cmd == (MTIOCPOS & IOCCMD_MASK)) {
1831      if (STp->ready != ST_READY)
1832        return (-EIO);
1833 #ifdef DEBUG
1834      if (debugging)
1835        printk("st%d: get tape position.\n", dev);
1836 #endif
1837      if (((cmd_in & IOCSIZE_MASK) >> IOCSIZE_SHIFT) != sizeof(struct mtpos))
1838        return (-EINVAL);
1839 
1840      i = flush_buffer(inode, file, 0);
1841      if (i < 0)
1842        return i;
1843 
1844      i = verify_area(VERIFY_WRITE, (void *)arg, sizeof(struct mtpos));
1845      if (i)
1846        return i;
1847 
1848      SCpnt = allocate_device(NULL, STp->device, 1);
1849 
1850      SCpnt->sense_buffer[0]=0;
1851      memset (scmd, 0, 10);
1852      if ((STp->device)->scsi_level < SCSI_2) {
1853        scmd[0] = QFA_REQUEST_BLOCK;
1854        scmd[4] = 3;
1855      }
1856      else {
1857        scmd[0] = READ_POSITION;
1858        scmd[1] = 1;
1859      }
1860      SCpnt->request.dev = dev;
1861      SCpnt->sense_buffer[0] = 0;
1862      scsi_do_cmd(SCpnt,
1863                  (void *) scmd, (void *) (STp->buffer)->b_data,
1864                  20, st_sleep_done, ST_TIMEOUT, MAX_READY_RETRIES);
1865 
1866 
1867         /* this must be done with interrupts off */
1868         save_flags (flags);
1869         cli();
1870         if (SCpnt->request.dev == dev) sleep_on( &(STp->waiting) );
1871         restore_flags(flags);
1872 
1873      
1874      if ((STp->buffer)->last_result_fatal != 0) {
1875        mt_pos.mt_blkno = (-1);
1876 #ifdef DEBUG
1877        if (debugging)
1878          printk("st%d: Can't read tape position.\n", dev);
1879 #endif
1880        result = (-EIO);
1881      }
1882      else {
1883        result = 0;
1884        if ((STp->device)->scsi_level < SCSI_2)
1885          mt_pos.mt_blkno = ((STp->buffer)->b_data[0] << 16) 
1886            + ((STp->buffer)->b_data[1] << 8) 
1887              + (STp->buffer)->b_data[2];
1888        else
1889          mt_pos.mt_blkno = ((STp->buffer)->b_data[4] << 24)
1890            + ((STp->buffer)->b_data[5] << 16) 
1891              + ((STp->buffer)->b_data[6] << 8) 
1892                + (STp->buffer)->b_data[7];
1893 
1894      }
1895 
1896      SCpnt->request.dev = -1;  /* Mark as not busy */
1897 
1898      memcpy_tofs((char *)arg, (char *) (&mt_pos), sizeof(struct mtpos));
1899      return result;
1900    }
1901    else if (STp->ready == ST_READY)
1902      return scsi_ioctl(STp->device, cmd_in, (void *) arg);
1903    else
1904      return (-EIO);
1905 }
1906 
1907 
1908 /* Set the boot options. Syntax: st=xxx,yyy
1909    where xxx is buffer size in 512 byte blocks and yyy is write threshold
1910    in 512 byte blocks. */
1911         void
1912 st_setup(char *str, int *ints)
     /* [previous][next][first][last][top][bottom][index][help] */
1913 {
1914   if (ints[0] > 0 && ints[1] > 0)
1915     st_buffer_size = ints[1] * ST_BLOCK_SIZE;
1916   if (ints[0] > 1 && ints[2] > 0) {
1917     st_write_threshold = ints[2] * ST_BLOCK_SIZE;
1918     if (st_write_threshold > st_buffer_size)
1919       st_write_threshold = st_buffer_size;
1920   }
1921   if (ints[0] > 2 && ints[3] > 0)
1922     st_max_buffers = ints[3];
1923 }
1924 
1925 
1926 static struct file_operations st_fops = {
1927    NULL,            /* lseek - default */
1928    st_read,         /* read - general block-dev read */
1929    st_write,        /* write - general block-dev write */
1930    NULL,            /* readdir - bad */
1931    NULL,            /* select */
1932    st_ioctl,        /* ioctl */
1933    NULL,            /* mmap */
1934    scsi_tape_open,  /* open */
1935    scsi_tape_close, /* release */
1936    NULL             /* fsync */
1937 };
1938 
1939 static int st_attach(Scsi_Device * SDp){
     /* [previous][next][first][last][top][bottom][index][help] */
1940    Scsi_Tape * tpnt;
1941    int i;
1942 
1943    if(SDp->type != TYPE_TAPE) return 1;
1944 
1945    if(st_template.nr_dev >= st_template.dev_max) 
1946      {
1947         SDp->attached--;
1948         return 1;
1949      }
1950 
1951    for(tpnt = scsi_tapes, i=0; i<st_template.dev_max; i++, tpnt++) 
1952      if(!tpnt->device) break;
1953 
1954    if(i >= st_template.dev_max) panic ("scsi_devices corrupt (st)");
1955 
1956    scsi_tapes[i].device = SDp;
1957    if (SDp->scsi_level <= 2)
1958      scsi_tapes[i].mt_status->mt_type = MT_ISSCSI1;
1959    else
1960      scsi_tapes[i].mt_status->mt_type = MT_ISSCSI2;
1961 
1962    st_template.nr_dev++;
1963    return 0;
1964 };
1965 
1966 static int st_detect(Scsi_Device * SDp)
     /* [previous][next][first][last][top][bottom][index][help] */
1967 {
1968   if(SDp->type != TYPE_TAPE) return 0;
1969 
1970   printk("Detected scsi tape st%d at scsi%d, id %d, lun %d\n", 
1971          st_template.dev_noticed++,
1972          SDp->host->host_no , SDp->id, SDp->lun); 
1973   
1974   return 1;
1975 }
1976 
1977 /* Driver initialization */
1978 static void st_init()
     /* [previous][next][first][last][top][bottom][index][help] */
1979 {
1980   int i;
1981   Scsi_Tape * STp;
1982   static int st_registered = 0;
1983 
1984   if (st_template.dev_noticed == 0) return;
1985 
1986   if(!st_registered) {
1987     if (register_chrdev(MAJOR_NR,"st",&st_fops)) {
1988       printk("Unable to get major %d for SCSI tapes\n",MAJOR_NR);
1989       return;
1990     }
1991     st_registered++;
1992   }
1993 
1994   if (scsi_tapes) return;
1995   scsi_tapes = (Scsi_Tape *) scsi_init_malloc(
1996                 (st_template.dev_noticed + ST_EXTRA_DEVS) * 
1997                                               sizeof(Scsi_Tape), GFP_ATOMIC);
1998   st_template.dev_max = st_template.dev_noticed + ST_EXTRA_DEVS;
1999 
2000 #ifdef DEBUG
2001   printk("st: Buffer size %d bytes, write threshold %d bytes.\n",
2002          st_buffer_size, st_write_threshold);
2003 #endif
2004 
2005   for (i=0; i < st_template.dev_max; ++i) {
2006     STp = &(scsi_tapes[i]);
2007     STp->device = NULL;
2008     STp->capacity = 0xfffff;
2009     STp->dirty = 0;
2010     STp->rw = ST_IDLE;
2011     STp->eof = ST_NOEOF;
2012     STp->waiting = NULL;
2013     STp->in_use = 0;
2014     STp->drv_buffer = 1;  /* Try buffering if no mode sense */
2015     STp->density = 0;
2016     STp->do_buffer_writes = ST_BUFFER_WRITES;
2017     STp->do_async_writes = ST_ASYNC_WRITES;
2018     STp->do_read_ahead = ST_READ_AHEAD;
2019     STp->two_fm = ST_TWO_FM;
2020     STp->fast_mteom = ST_FAST_MTEOM;
2021     STp->write_threshold = st_write_threshold;
2022     STp->drv_block = 0;
2023     STp->moves_after_eof = 1;
2024     STp->at_sm = 0;
2025     STp->mt_status = (struct mtget *) scsi_init_malloc(sizeof(struct mtget), GFP_ATOMIC);
2026     /* Initialize status */
2027     memset((void *) scsi_tapes[i].mt_status, 0, sizeof(struct mtget));
2028   }
2029 
2030   /* Allocate the buffers */
2031   st_nbr_buffers = st_template.dev_noticed + ST_EXTRA_DEVS;
2032   if (st_nbr_buffers > st_max_buffers)
2033     st_nbr_buffers = st_max_buffers;
2034   st_buffers = (ST_buffer **) scsi_init_malloc(st_nbr_buffers * 
2035                                                sizeof(ST_buffer *), GFP_ATOMIC);
2036   /* FIXME - if we are hitting this because we are loading a tape module
2037   as a loadable driver, we should not use kmalloc - it will allocate
2038   a 64Kb region in order to buffer about 32Kb.  Try using 31 blocks
2039   instead. */
2040   
2041   for (i=0; i < st_nbr_buffers; i++) {
2042     st_buffers[i] = (ST_buffer *) scsi_init_malloc(sizeof(ST_buffer) - 
2043                                                    1 + st_buffer_size, GFP_ATOMIC | GFP_DMA);
2044 #ifdef DEBUG
2045 /*    printk("st: Buffer address: %p\n", st_buffers[i]); */
2046 #endif
2047     st_buffers[i]->in_use = 0;
2048     st_buffers[i]->writing = 0;
2049   }
2050   return;
2051 }
2052 
2053 static void st_detach(Scsi_Device * SDp)
     /* [previous][next][first][last][top][bottom][index][help] */
2054 {
2055   Scsi_Tape * tpnt;
2056   int i;
2057   
2058   for(tpnt = scsi_tapes, i=0; i<st_template.dev_max; i++, tpnt++) 
2059     if(tpnt->device == SDp) {
2060       tpnt->device = NULL;
2061       SDp->attached--;
2062       st_template.nr_dev--;
2063       st_template.dev_noticed--;
2064       return;
2065     }
2066   return;
2067 }

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