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                      bytes, st_sleep_done, ST_TIMEOUT, MAX_RETRIES);
1067 
1068 
1069         /* this must be done with interrupts off */
1070         save_flags (flags);
1071         cli();
1072         if (SCpnt->request.dev == dev) sleep_on( &(STp->waiting) );
1073         restore_flags(flags);
1074 
1075         (STp->buffer)->read_pointer = 0;
1076         STp->eof_hit = 0;
1077         STp->at_sm = 0;
1078 
1079         if ((STp->buffer)->last_result_fatal) {
1080 #ifdef DEBUG
1081           if (debugging)
1082             printk("st%d: Sense: %2x %2x %2x %2x %2x %2x %2x %2x\n", dev,
1083                    SCpnt->sense_buffer[0], SCpnt->sense_buffer[1],
1084                    SCpnt->sense_buffer[2], SCpnt->sense_buffer[3],
1085                    SCpnt->sense_buffer[4], SCpnt->sense_buffer[5],
1086                    SCpnt->sense_buffer[6], SCpnt->sense_buffer[7]);
1087 #endif
1088           if ((SCpnt->sense_buffer[0] & 0x70) == 0x70) { /* extended sense */
1089 
1090             if ((SCpnt->sense_buffer[2] & 0xe0) != 0) { /* EOF, EOM, or ILI */
1091 
1092               if ((SCpnt->sense_buffer[0] & 0x80) != 0)
1093                 transfer = (SCpnt->sense_buffer[3] << 24) |
1094                   (SCpnt->sense_buffer[4] << 16) |
1095                     (SCpnt->sense_buffer[5] << 8) | SCpnt->sense_buffer[6];
1096               else
1097                 transfer = 0;
1098               if (STp->block_size == 0 &&
1099                   (SCpnt->sense_buffer[2] & 0x0f) == MEDIUM_ERROR)
1100                 transfer = bytes;
1101 
1102               if (SCpnt->sense_buffer[2] & 0x20) {
1103                 if (STp->block_size == 0) {
1104                   if (transfer <= 0)
1105                     transfer = 0;
1106                   (STp->buffer)->buffer_bytes = bytes - transfer;
1107                 }
1108                 else {
1109                   printk("st%d: Incorrect block size.\n", dev);
1110                   SCpnt->request.dev = -1;  /* Mark as not busy */
1111                   return (-EIO);
1112                 }
1113               }
1114               else if (SCpnt->sense_buffer[2] & 0x40) {
1115                 STp->eof = ST_EOM_OK;
1116                 if (STp->block_size == 0)
1117                   (STp->buffer)->buffer_bytes = bytes - transfer;
1118                 else
1119                   (STp->buffer)->buffer_bytes =
1120                     bytes - transfer * STp->block_size;
1121 #ifdef DEBUG
1122                 if (debugging)
1123                   printk("st%d: EOM detected (%d bytes read).\n", dev,
1124                          (STp->buffer)->buffer_bytes);
1125 #endif
1126               }
1127               else if (SCpnt->sense_buffer[2] & 0x80) {
1128                 STp->eof = ST_FM;
1129                 if (STp->block_size == 0)
1130                   (STp->buffer)->buffer_bytes = 0;
1131                 else
1132                   (STp->buffer)->buffer_bytes =
1133                     bytes - transfer * STp->block_size;
1134 #ifdef DEBUG
1135                 if (debugging)
1136                   printk(
1137                     "st%d: EOF detected (%d bytes read, transferred %d bytes).\n",
1138                          dev, (STp->buffer)->buffer_bytes, total);
1139 #endif
1140               }
1141             } /* end of EOF, EOM, ILI test */
1142             else { /* nonzero sense key */
1143 #ifdef DEBUG
1144               if (debugging)
1145                 printk("st%d: Tape error while reading.\n", dev);
1146 #endif
1147               SCpnt->request.dev = -1;
1148               STp->drv_block = (-1);
1149               if (total)
1150                 return total;
1151               else if (STp->moves_after_eof == 1 &&
1152                        (SCpnt->sense_buffer[2] & 0x0f) == BLANK_CHECK) {
1153 #ifdef DEBUG
1154                 if (debugging)
1155                   printk("st%d: Zero returned for first BLANK CHECK after EOF.\n",
1156                          dev);
1157 #endif
1158                 STp->eof = ST_EOD;
1159                 return 0; /* First BLANK_CHECK after EOF */
1160               }
1161               else
1162                 return -EIO;
1163             }
1164           } /* End of extended sense test */
1165           else {
1166             transfer = (STp->buffer)->last_result_fatal;
1167             SCpnt->request.dev = -1;  /* Mark as not busy */
1168             return transfer;
1169           }
1170         } /* End of error handling */
1171         else /* Read successful */
1172           (STp->buffer)->buffer_bytes = bytes;
1173 
1174         if (STp->drv_block >= 0) {
1175           if (STp->block_size == 0)
1176             STp->drv_block++;
1177           else
1178             STp->drv_block += (STp->buffer)->buffer_bytes / STp->block_size;
1179         }
1180 
1181       } /* if ((STp->buffer)->buffer_bytes == 0 &&
1182            STp->eof == ST_NOEOF) */
1183 
1184       if ((STp->buffer)->buffer_bytes > 0) {
1185 #ifdef DEBUG
1186         if (debugging && STp->eof != ST_NOEOF)
1187           printk("st%d: EOF up. Left %d, needed %d.\n", dev,
1188                  (STp->buffer)->buffer_bytes, count - total);
1189 #endif
1190         transfer = (STp->buffer)->buffer_bytes < count - total ?
1191           (STp->buffer)->buffer_bytes : count - total;
1192         memcpy_tofs(buf, (STp->buffer)->b_data +
1193                     (STp->buffer)->read_pointer,transfer);
1194         filp->f_pos += transfer;
1195         buf += transfer;
1196         total += transfer;
1197         (STp->buffer)->buffer_bytes -= transfer;
1198         (STp->buffer)->read_pointer += transfer;
1199       }
1200       else if (STp->eof != ST_NOEOF) {
1201         STp->eof_hit = 1;
1202         SCpnt->request.dev = -1;  /* Mark as not busy */
1203         if (total == 0 && STp->eof == ST_FM) {
1204           STp->eof = ST_NOEOF;
1205           STp->drv_block = 0;
1206           if (STp->moves_after_eof > 1)
1207             STp->moves_after_eof = 0;
1208           if ((STp->mt_status)->mt_fileno >= 0)
1209             (STp->mt_status)->mt_fileno++;
1210         }
1211         if (total == 0 && STp->eof == ST_EOM_OK)
1212           return (-EIO);  /* ST_EOM_ERROR not used in read */
1213         return total;
1214       }
1215 
1216       if (STp->block_size == 0)
1217         count = total;  /* Read only one variable length block */
1218 
1219     } /* for (total = 0; total < count; ) */
1220 
1221     SCpnt->request.dev = -1;  /* Mark as not busy */
1222 
1223     return total;
1224 }
1225 
1226 
1227 
1228 /* Set the driver options */
1229         static int
1230 st_set_options(struct inode * inode, long options)
     /* [previous][next][first][last][top][bottom][index][help] */
1231 {
1232   int dev, value;
1233   Scsi_Tape *STp;
1234 
1235   dev = MINOR(inode->i_rdev) & 127;
1236   STp = &(scsi_tapes[dev]);
1237   if ((options & MT_ST_OPTIONS) == MT_ST_BOOLEANS) {
1238     STp->do_buffer_writes = (options & MT_ST_BUFFER_WRITES) != 0;
1239     STp->do_async_writes  = (options & MT_ST_ASYNC_WRITES) != 0;
1240     STp->do_read_ahead    = (options & MT_ST_READ_AHEAD) != 0;
1241     STp->two_fm           = (options & MT_ST_TWO_FM) != 0;
1242     STp->fast_mteom       = (options & MT_ST_FAST_MTEOM) != 0;
1243 #ifdef DEBUG
1244     debugging = (options & MT_ST_DEBUGGING) != 0;
1245     printk(
1246 "st%d: options: buffer writes: %d, async writes: %d, read ahead: %d\n",
1247            dev, STp->do_buffer_writes, STp->do_async_writes,
1248            STp->do_read_ahead);
1249     printk("              two FMs: %d, fast mteom: %d debugging: %d\n",
1250            STp->two_fm, STp->fast_mteom, debugging);
1251 #endif
1252   }
1253   else if ((options & MT_ST_OPTIONS) == MT_ST_WRITE_THRESHOLD) {
1254     value = (options & ~MT_ST_OPTIONS) * ST_BLOCK_SIZE;
1255     if (value < 1 || value > st_buffer_size) {
1256       printk("st: Write threshold %d too small or too large.\n",
1257              value);
1258       return (-EIO);
1259     }
1260     STp->write_threshold = value;
1261 #ifdef DEBUG
1262     printk("st%d: Write threshold set to %d bytes.\n", dev,
1263            STp->write_threshold);
1264 #endif
1265   }
1266   else
1267     return (-EIO);
1268 
1269   return 0;
1270 }
1271 
1272 
1273 /* Internal ioctl function */
1274         static int
1275 st_int_ioctl(struct inode * inode,struct file * file,
     /* [previous][next][first][last][top][bottom][index][help] */
1276              unsigned int cmd_in, unsigned long arg)
1277 {
1278    int dev = MINOR(inode->i_rdev);
1279    int timeout = ST_LONG_TIMEOUT;
1280    long ltmp;
1281    int ioctl_result;
1282    unsigned char cmd[10];
1283    Scsi_Cmnd * SCpnt;
1284    Scsi_Tape * STp;
1285    int fileno, blkno, at_sm, undone, datalen;
1286    unsigned int flags;
1287 
1288    dev = dev & 127;
1289    STp = &(scsi_tapes[dev]);
1290    if (STp->ready != ST_READY)
1291      return (-EIO);
1292    fileno = (STp->mt_status)->mt_fileno ;
1293    blkno = STp->drv_block;
1294    at_sm = STp->at_sm;
1295 
1296    memset(cmd, 0, 10);
1297    datalen = 0;
1298    switch (cmd_in) {
1299      case MTFSF:
1300      case MTFSFM:
1301        cmd[0] = SPACE;
1302        cmd[1] = 0x01; /* Space FileMarks */
1303        cmd[2] = (arg >> 16);
1304        cmd[3] = (arg >> 8);
1305        cmd[4] = arg;
1306 #ifdef DEBUG
1307        if (debugging)
1308          printk("st%d: Spacing tape forward over %d filemarks.\n", dev,
1309                 cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
1310 #endif
1311        if (fileno >= 0)
1312          fileno += arg;
1313        blkno = 0;
1314        at_sm &= (arg != 0);
1315        break; 
1316      case MTBSF:
1317      case MTBSFM:
1318        cmd[0] = SPACE;
1319        cmd[1] = 0x01; /* Space FileMarks */
1320        ltmp = (-arg);
1321        cmd[2] = (ltmp >> 16);
1322        cmd[3] = (ltmp >> 8);
1323        cmd[4] = ltmp;
1324 #ifdef DEBUG
1325        if (debugging) {
1326          if (cmd[2] & 0x80)
1327            ltmp = 0xff000000;
1328          ltmp = ltmp | (cmd[2] << 16) | (cmd[3] << 8) | cmd[4];
1329          printk("st%d: Spacing tape backward over %ld filemarks.\n", dev, (-ltmp));
1330        }
1331 #endif
1332        if (fileno >= 0)
1333          fileno -= arg;
1334        blkno = (-1);  /* We can't know the block number */
1335        at_sm &= (arg != 0);
1336        break; 
1337      case MTFSR:
1338        cmd[0] = SPACE;
1339        cmd[1] = 0x00; /* Space Blocks */
1340        cmd[2] = (arg >> 16);
1341        cmd[3] = (arg >> 8);
1342        cmd[4] = arg;
1343 #ifdef DEBUG
1344        if (debugging)
1345          printk("st%d: Spacing tape forward %d blocks.\n", dev,
1346                 cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
1347 #endif
1348        if (blkno >= 0)
1349          blkno += arg;
1350        at_sm &= (arg != 0);
1351        break; 
1352      case MTBSR:
1353        cmd[0] = SPACE;
1354        cmd[1] = 0x00; /* Space Blocks */
1355        ltmp = (-arg);
1356        cmd[2] = (ltmp >> 16);
1357        cmd[3] = (ltmp >> 8);
1358        cmd[4] = ltmp;
1359 #ifdef DEBUG
1360        if (debugging) {
1361          if (cmd[2] & 0x80)
1362            ltmp = 0xff000000;
1363          ltmp = ltmp | (cmd[2] << 16) | (cmd[3] << 8) | cmd[4];
1364          printk("st%d: Spacing tape backward %ld blocks.\n", dev, (-ltmp));
1365        }
1366 #endif
1367        if (blkno >= 0)
1368          blkno -= arg;
1369        at_sm &= (arg != 0);
1370        break; 
1371      case MTFSS:
1372        cmd[0] = SPACE;
1373        cmd[1] = 0x04; /* Space Setmarks */
1374        cmd[2] = (arg >> 16);
1375        cmd[3] = (arg >> 8);
1376        cmd[4] = arg;
1377 #ifdef DEBUG
1378        if (debugging)
1379          printk("st%d: Spacing tape forward %d setmarks.\n", dev,
1380                 cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
1381 #endif
1382        if (arg != 0) {
1383          blkno = fileno = (-1);
1384          at_sm = 1;
1385        }
1386        break; 
1387      case MTBSS:
1388        cmd[0] = SPACE;
1389        cmd[1] = 0x04; /* Space Setmarks */
1390        ltmp = (-arg);
1391        cmd[2] = (ltmp >> 16);
1392        cmd[3] = (ltmp >> 8);
1393        cmd[4] = ltmp;
1394 #ifdef DEBUG
1395        if (debugging) {
1396          if (cmd[2] & 0x80)
1397            ltmp = 0xff000000;
1398          ltmp = ltmp | (cmd[2] << 16) | (cmd[3] << 8) | cmd[4];
1399          printk("st%d: Spacing tape backward %ld setmarks.\n", dev, (-ltmp));
1400        }
1401 #endif
1402        if (arg != 0) {
1403          blkno = fileno = (-1);
1404          at_sm = 1;
1405        }
1406        break; 
1407      case MTWEOF:
1408      case MTWSM:
1409        if (STp->write_prot)
1410          return (-EACCES);
1411        cmd[0] = WRITE_FILEMARKS;
1412        if (cmd_in == MTWSM)
1413          cmd[1] = 2;
1414        cmd[2] = (arg >> 16);
1415        cmd[3] = (arg >> 8);
1416        cmd[4] = arg;
1417        timeout = ST_TIMEOUT;
1418 #ifdef DEBUG
1419        if (debugging) {
1420          if (cmd_in == MTWEOF)
1421            printk("st%d: Writing %d filemarks.\n", dev,
1422                   cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
1423          else
1424            printk("st%d: Writing %d setmarks.\n", dev,
1425                   cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
1426        }
1427 #endif
1428        if (fileno >= 0)
1429          fileno += arg;
1430        blkno = 0;
1431        at_sm = (cmd_in == MTWSM);
1432        break; 
1433      case MTREW:
1434        cmd[0] = REZERO_UNIT;
1435 #ifdef ST_NOWAIT
1436        cmd[1] = 1;  /* Don't wait for completion */
1437        timeout = ST_TIMEOUT;
1438 #endif
1439 #ifdef DEBUG
1440        if (debugging)
1441          printk("st%d: Rewinding tape.\n", dev);
1442 #endif
1443        fileno = blkno = at_sm = 0 ;
1444        break; 
1445      case MTOFFL:
1446        cmd[0] = START_STOP;
1447 #ifdef ST_NOWAIT
1448        cmd[1] = 1;  /* Don't wait for completion */
1449        timeout = ST_TIMEOUT;
1450 #endif
1451 #ifdef DEBUG
1452        if (debugging)
1453          printk("st%d: Unloading tape.\n", dev);
1454 #endif
1455        fileno = blkno = at_sm = 0 ;
1456        break; 
1457      case MTNOP:
1458 #ifdef DEBUG
1459        if (debugging)
1460          printk("st%d: No op on tape.\n", dev);
1461 #endif
1462        return 0;  /* Should do something ? */
1463        break;
1464      case MTRETEN:
1465        cmd[0] = START_STOP;
1466 #ifdef ST_NOWAIT
1467        cmd[1] = 1;  /* Don't wait for completion */
1468        timeout = ST_TIMEOUT;
1469 #endif
1470        cmd[4] = 3;
1471 #ifdef DEBUG
1472        if (debugging)
1473          printk("st%d: Retensioning tape.\n", dev);
1474 #endif
1475        fileno = blkno = at_sm = 0;
1476        break; 
1477      case MTEOM:
1478        if (!STp->fast_mteom) {
1479          /* space to the end of tape */
1480          ioctl_result = st_int_ioctl(inode, file, MTFSF, 0x3fff);
1481          fileno = (STp->mt_status)->mt_fileno ;
1482          if (STp->eof == ST_EOD || STp->eof == ST_EOM_OK)
1483            return 0;
1484          /* The next lines would hide the number of spaced FileMarks
1485             That's why I inserted the previous lines. I had no luck
1486             with detecting EOM with FSF, so we go now to EOM.
1487             Joerg Weule */
1488        }
1489        else
1490          fileno = (-1);
1491        cmd[0] = SPACE;
1492        cmd[1] = 3;
1493 #ifdef DEBUG
1494        if (debugging)
1495          printk("st%d: Spacing to end of recorded medium.\n", dev);
1496 #endif
1497        blkno = 0;
1498        at_sm = 0;
1499        break; 
1500      case MTERASE:
1501        if (STp->write_prot)
1502          return (-EACCES);
1503        cmd[0] = ERASE;
1504        cmd[1] = 1;  /* To the end of tape */
1505 #ifdef ST_NOWAIT
1506        cmd[1] |= 2;  /* Don't wait for completion */
1507        timeout = ST_TIMEOUT;
1508 #else
1509        timeout = ST_LONG_TIMEOUT * 8;
1510 #endif
1511 #ifdef DEBUG
1512        if (debugging)
1513          printk("st%d: Erasing tape.\n", dev);
1514 #endif
1515        fileno = blkno = at_sm = 0 ;
1516        break;
1517      case MTSEEK:
1518        if ((STp->device)->scsi_level < SCSI_2) {
1519          cmd[0] = QFA_SEEK_BLOCK;
1520          cmd[2] = (arg >> 16);
1521          cmd[3] = (arg >> 8);
1522          cmd[4] = arg;
1523          cmd[5] = 0;
1524        }
1525        else {
1526          cmd[0] = SEEK_10;
1527          cmd[1] = 4;
1528          cmd[3] = (arg >> 24);
1529          cmd[4] = (arg >> 16);
1530          cmd[5] = (arg >> 8);
1531          cmd[6] = arg;
1532        }
1533 #ifdef ST_NOWAIT
1534        cmd[1] |= 1;  /* Don't wait for completion */
1535        timeout = ST_TIMEOUT;
1536 #endif
1537 #ifdef DEBUG
1538        if (debugging)
1539          printk("st%d: Seeking tape to block %ld.\n", dev, arg);
1540 #endif
1541        fileno = blkno = (-1);
1542        at_sm = 0;
1543        break;
1544      case MTSETBLK:  /* Set block length */
1545      case MTSETDENSITY: /* Set tape density */
1546      case MTSETDRVBUFFER: /* Set drive buffering */
1547        if (STp->dirty || (STp->buffer)->buffer_bytes != 0)
1548          return (-EIO);   /* Not allowed if data in buffer */
1549        if (cmd_in == MTSETBLK &&
1550            arg != 0 &&
1551            (arg < STp->min_block || arg > STp->max_block ||
1552             arg > st_buffer_size)) {
1553          printk("st%d: Illegal block size.\n", dev);
1554          return (-EINVAL);
1555        }
1556        cmd[0] = MODE_SELECT;
1557        cmd[4] = datalen = 12;
1558 
1559        memset((STp->buffer)->b_data, 0, 12);
1560        if (cmd_in == MTSETDRVBUFFER)
1561          (STp->buffer)->b_data[2] = (arg & 7) << 4;
1562        else
1563          (STp->buffer)->b_data[2] = 
1564            STp->drv_buffer << 4;
1565        (STp->buffer)->b_data[3] = 8;     /* block descriptor length */
1566        if (cmd_in == MTSETDENSITY)
1567          (STp->buffer)->b_data[4] = arg;
1568        else
1569          (STp->buffer)->b_data[4] = STp->density;
1570        if (cmd_in == MTSETBLK)
1571          ltmp = arg;
1572        else
1573          ltmp = STp->block_size;
1574        (STp->buffer)->b_data[9] = (ltmp >> 16);
1575        (STp->buffer)->b_data[10] = (ltmp >> 8);
1576        (STp->buffer)->b_data[11] = ltmp;
1577        timeout = ST_TIMEOUT;
1578 #ifdef DEBUG
1579        if (debugging) {
1580          if (cmd_in == MTSETBLK)
1581            printk("st%d: Setting block size to %d bytes.\n", dev,
1582                   (STp->buffer)->b_data[9] * 65536 +
1583                   (STp->buffer)->b_data[10] * 256 +
1584                   (STp->buffer)->b_data[11]);
1585          else if (cmd_in == MTSETDENSITY)
1586            printk("st%d: Setting density code to %x.\n", dev,
1587                   (STp->buffer)->b_data[4]);
1588          else
1589            printk("st%d: Setting drive buffer code to %d.\n", dev,
1590                   ((STp->buffer)->b_data[2] >> 4) & 7);
1591        }
1592 #endif
1593        break;
1594      default:
1595        printk("st%d: Unknown st_ioctl command %x.\n", dev, cmd_in);
1596        return (-ENOSYS);
1597      }
1598 
1599    SCpnt = allocate_device(NULL, STp->device, 1);
1600    SCpnt->sense_buffer[0] = 0;
1601    SCpnt->request.dev = dev;
1602    scsi_do_cmd(SCpnt,
1603                (void *) cmd, (void *) (STp->buffer)->b_data, datalen,
1604                st_sleep_done, timeout, MAX_RETRIES);
1605 
1606 
1607         /* this must be done with interrupts off */
1608         save_flags (flags);
1609         cli();
1610         if (SCpnt->request.dev == dev) sleep_on( &(STp->waiting) );
1611         restore_flags(flags);
1612 
1613 
1614    ioctl_result = (STp->buffer)->last_result_fatal;
1615 
1616    SCpnt->request.dev = -1;  /* Mark as not busy */
1617 
1618    if (cmd_in == MTFSF)
1619      STp->moves_after_eof = 0;
1620    else
1621      STp->moves_after_eof = 1;
1622    if (!ioctl_result) {  /* SCSI command successful */
1623      if (cmd_in != MTSEEK) {
1624        STp->drv_block = blkno;
1625        (STp->mt_status)->mt_fileno = fileno;
1626        STp->at_sm = at_sm;
1627      }
1628      else {
1629        STp->drv_block = (STp->mt_status)->mt_fileno = (-1);
1630        STp->at_sm = 0;
1631      }
1632      if (cmd_in == MTBSFM)
1633        ioctl_result = st_int_ioctl(inode, file, MTFSF, 1);
1634      else if (cmd_in == MTFSFM)
1635        ioctl_result = st_int_ioctl(inode, file, MTBSF, 1);
1636      else if (cmd_in == MTSETBLK) {
1637        STp->block_size = arg;
1638        if (arg != 0) {
1639          (STp->buffer)->buffer_blocks =
1640            st_buffer_size / STp->block_size;
1641          (STp->buffer)->buffer_size =
1642            (STp->buffer)->buffer_blocks * STp->block_size;
1643        }
1644        else {
1645          (STp->buffer)->buffer_blocks = 1;
1646          (STp->buffer)->buffer_size = st_buffer_size;
1647        }
1648        (STp->buffer)->buffer_bytes =
1649          (STp->buffer)->read_pointer = 0;
1650      }
1651      else if (cmd_in == MTSETDRVBUFFER)
1652        STp->drv_buffer = (arg & 7);
1653      else if (cmd_in == MTSETDENSITY)
1654        STp->density = arg;
1655      else if (cmd_in == MTEOM) {
1656        STp->eof = ST_EOD;
1657        STp->eof_hit = 0;
1658      }
1659      else if (cmd_in != MTSETBLK && cmd_in != MTNOP) {
1660        STp->eof = ST_NOEOF;
1661        STp->eof_hit = 0;
1662      }
1663    } else {  /* SCSI command was not completely successful */
1664      if (SCpnt->sense_buffer[2] & 0x40) {
1665        if (cmd_in != MTBSF && cmd_in != MTBSFM &&
1666            cmd_in != MTBSR && cmd_in != MTBSS)
1667          STp->eof = ST_EOM_OK;
1668        STp->eof_hit = 0;
1669        STp->drv_block = 0;
1670      }
1671      undone = (
1672           (SCpnt->sense_buffer[3] << 24) +
1673           (SCpnt->sense_buffer[4] << 16) +
1674           (SCpnt->sense_buffer[5] << 8) +
1675           SCpnt->sense_buffer[6] );
1676      if ( (cmd_in == MTFSF) || (cmd_in == MTFSFM) ) {
1677        if (fileno >= 0)
1678          (STp->mt_status)->mt_fileno = fileno - undone ;
1679        else
1680          (STp->mt_status)->mt_fileno = fileno;
1681        STp->drv_block = 0;
1682      }
1683      else if ( (cmd_in == MTBSF) || (cmd_in == MTBSFM) ) {
1684        (STp->mt_status)->mt_fileno = fileno + undone ;
1685        STp->drv_block = 0;
1686      }
1687      else if (cmd_in == MTFSR) {
1688        if (SCpnt->sense_buffer[2] & 0x80) { /* Hit filemark */
1689          (STp->mt_status)->mt_fileno++;
1690          STp->drv_block = 0;
1691        }
1692        else {
1693          if (blkno >= undone)
1694            STp->drv_block = blkno - undone;
1695          else
1696            STp->drv_block = (-1);
1697        }
1698      }
1699      else if (cmd_in == MTBSR) {
1700        if (SCpnt->sense_buffer[2] & 0x80) { /* Hit filemark */
1701          (STp->mt_status)->mt_fileno--;
1702          STp->drv_block = (-1);
1703        }
1704        else {
1705          if (blkno >= 0)
1706            STp->drv_block = blkno + undone;
1707          else
1708            STp->drv_block = (-1);
1709        }
1710      }
1711      else if (cmd_in == MTEOM || cmd_in == MTSEEK) {
1712        (STp->mt_status)->mt_fileno = (-1);
1713        STp->drv_block = (-1);
1714      }
1715      if (STp->eof == ST_NOEOF &&
1716          (SCpnt->sense_buffer[2] & 0x0f) == BLANK_CHECK)
1717        STp->eof = ST_EOD;
1718    }
1719 
1720    return ioctl_result;
1721 }
1722 
1723 
1724 
1725 /* The ioctl command */
1726         static int
1727 st_ioctl(struct inode * inode,struct file * file,
     /* [previous][next][first][last][top][bottom][index][help] */
1728          unsigned int cmd_in, unsigned long arg)
1729 {
1730    int dev = MINOR(inode->i_rdev);
1731    int i, cmd, result;
1732    struct mtop mtc;
1733    struct mtpos mt_pos;
1734    unsigned char scmd[10];
1735    Scsi_Cmnd *SCpnt;
1736    Scsi_Tape *STp;
1737    unsigned int flags;
1738 
1739    dev = dev & 127;
1740    STp = &(scsi_tapes[dev]);
1741 #ifdef DEBUG
1742    if (debugging && !STp->in_use) {
1743      printk("st%d: Incorrect device.\n", dev);
1744      return (-EIO);
1745    }
1746 #endif
1747 
1748    cmd = cmd_in & IOCCMD_MASK;
1749    if (cmd == (MTIOCTOP & IOCCMD_MASK)) {
1750 
1751      if (((cmd_in & IOCSIZE_MASK) >> IOCSIZE_SHIFT) != sizeof(mtc))
1752        return (-EINVAL);
1753 
1754      i = verify_area(VERIFY_WRITE, (void *)arg, sizeof(mtc));
1755      if (i)
1756         return i;
1757 
1758      memcpy_fromfs((char *) &mtc, (char *)arg, sizeof(struct mtop));
1759 
1760      i = flush_buffer(inode, file, mtc.mt_op == MTSEEK ||
1761                       mtc.mt_op == MTREW || mtc.mt_op == MTOFFL ||
1762                       mtc.mt_op == MTRETEN || mtc.mt_op == MTEOM);
1763      if (i < 0)
1764        return i;
1765      if (mtc.mt_op != MTNOP && mtc.mt_op != MTSETBLK &&
1766          mtc.mt_op != MTSETDENSITY && mtc.mt_op != MTWSM &&
1767          mtc.mt_op != MTSETDRVBUFFER)
1768        STp->rw = ST_IDLE;  /* Prevent automatic WEOF */
1769 
1770      if (mtc.mt_op == MTSETDRVBUFFER &&
1771          (mtc.mt_count & MT_ST_OPTIONS) != 0)
1772        return st_set_options(inode, mtc.mt_count);
1773      else
1774        return st_int_ioctl(inode, file, mtc.mt_op, mtc.mt_count);
1775    }
1776    else if (cmd == (MTIOCGET & IOCCMD_MASK)) {
1777 
1778      if (((cmd_in & IOCSIZE_MASK) >> IOCSIZE_SHIFT) != sizeof(struct mtget))
1779        return (-EINVAL);
1780      i = verify_area(VERIFY_WRITE, (void *)arg, sizeof(struct mtget));
1781      if (i)
1782        return i;
1783 
1784      (STp->mt_status)->mt_dsreg =
1785        ((STp->block_size << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK) |
1786        ((STp->density << MT_ST_DENSITY_SHIFT) & MT_ST_DENSITY_MASK);
1787      (STp->mt_status)->mt_blkno = STp->drv_block;
1788      if (STp->block_size != 0) {
1789        if (STp->rw == ST_WRITING)
1790          (STp->mt_status)->mt_blkno +=
1791            (STp->buffer)->buffer_bytes / STp->block_size;
1792        else if (STp->rw == ST_READING)
1793          (STp->mt_status)->mt_blkno -= ((STp->buffer)->buffer_bytes +
1794            STp->block_size - 1) / STp->block_size;
1795      }
1796 
1797      (STp->mt_status)->mt_gstat = 0;
1798      if (STp->drv_write_prot)
1799        (STp->mt_status)->mt_gstat |= GMT_WR_PROT(0xffffffff);
1800      if ((STp->mt_status)->mt_blkno == 0) {
1801        if ((STp->mt_status)->mt_fileno == 0)
1802          (STp->mt_status)->mt_gstat |= GMT_BOT(0xffffffff);
1803        else
1804          (STp->mt_status)->mt_gstat |= GMT_EOF(0xffffffff);
1805      }
1806      if (STp->eof == ST_EOM_OK || STp->eof == ST_EOM_ERROR)
1807        (STp->mt_status)->mt_gstat |= GMT_EOT(0xffffffff);
1808      else if (STp->eof == ST_EOD)
1809        (STp->mt_status)->mt_gstat |= GMT_EOD(0xffffffff);
1810      if (STp->density == 1)
1811        (STp->mt_status)->mt_gstat |= GMT_D_800(0xffffffff);
1812      else if (STp->density == 2)
1813        (STp->mt_status)->mt_gstat |= GMT_D_1600(0xffffffff);
1814      else if (STp->density == 3)
1815        (STp->mt_status)->mt_gstat |= GMT_D_6250(0xffffffff);
1816      if (STp->ready == ST_READY)
1817        (STp->mt_status)->mt_gstat |= GMT_ONLINE(0xffffffff);
1818      if (STp->ready == ST_NO_TAPE)
1819        (STp->mt_status)->mt_gstat |= GMT_DR_OPEN(0xffffffff);
1820      if (STp->at_sm)
1821        (STp->mt_status)->mt_gstat |= GMT_SM(0xffffffff);
1822 
1823      memcpy_tofs((char *)arg, (char *)(STp->mt_status),
1824                  sizeof(struct mtget));
1825 
1826      (STp->mt_status)->mt_erreg = 0;  /* Clear after read */
1827      return 0;
1828    }
1829    else if (cmd == (MTIOCPOS & IOCCMD_MASK)) {
1830      if (STp->ready != ST_READY)
1831        return (-EIO);
1832 #ifdef DEBUG
1833      if (debugging)
1834        printk("st%d: get tape position.\n", dev);
1835 #endif
1836      if (((cmd_in & IOCSIZE_MASK) >> IOCSIZE_SHIFT) != sizeof(struct mtpos))
1837        return (-EINVAL);
1838 
1839      i = flush_buffer(inode, file, 0);
1840      if (i < 0)
1841        return i;
1842 
1843      i = verify_area(VERIFY_WRITE, (void *)arg, sizeof(struct mtpos));
1844      if (i)
1845        return i;
1846 
1847      SCpnt = allocate_device(NULL, STp->device, 1);
1848 
1849      SCpnt->sense_buffer[0]=0;
1850      memset (scmd, 0, 10);
1851      if ((STp->device)->scsi_level < SCSI_2) {
1852        scmd[0] = QFA_REQUEST_BLOCK;
1853        scmd[4] = 3;
1854      }
1855      else {
1856        scmd[0] = READ_POSITION;
1857        scmd[1] = 1;
1858      }
1859      SCpnt->request.dev = dev;
1860      SCpnt->sense_buffer[0] = 0;
1861      scsi_do_cmd(SCpnt,
1862                  (void *) scmd, (void *) (STp->buffer)->b_data,
1863                  20, st_sleep_done, ST_TIMEOUT, MAX_READY_RETRIES);
1864 
1865 
1866         /* this must be done with interrupts off */
1867         save_flags (flags);
1868         cli();
1869         if (SCpnt->request.dev == dev) sleep_on( &(STp->waiting) );
1870         restore_flags(flags);
1871 
1872      
1873      if ((STp->buffer)->last_result_fatal != 0) {
1874        mt_pos.mt_blkno = (-1);
1875 #ifdef DEBUG
1876        if (debugging)
1877          printk("st%d: Can't read tape position.\n", dev);
1878 #endif
1879        result = (-EIO);
1880      }
1881      else {
1882        result = 0;
1883        if ((STp->device)->scsi_level < SCSI_2)
1884          mt_pos.mt_blkno = ((STp->buffer)->b_data[0] << 16) 
1885            + ((STp->buffer)->b_data[1] << 8) 
1886              + (STp->buffer)->b_data[2];
1887        else
1888          mt_pos.mt_blkno = ((STp->buffer)->b_data[4] << 24)
1889            + ((STp->buffer)->b_data[5] << 16) 
1890              + ((STp->buffer)->b_data[6] << 8) 
1891                + (STp->buffer)->b_data[7];
1892 
1893      }
1894 
1895      SCpnt->request.dev = -1;  /* Mark as not busy */
1896 
1897      memcpy_tofs((char *)arg, (char *) (&mt_pos), sizeof(struct mtpos));
1898      return result;
1899    }
1900    else if (STp->ready == ST_READY)
1901      return scsi_ioctl(STp->device, cmd_in, (void *) arg);
1902    else
1903      return (-EIO);
1904 }
1905 
1906 
1907 /* Set the boot options. Syntax: st=xxx,yyy
1908    where xxx is buffer size in 512 byte blocks and yyy is write threshold
1909    in 512 byte blocks. */
1910         void
1911 st_setup(char *str, int *ints)
     /* [previous][next][first][last][top][bottom][index][help] */
1912 {
1913   if (ints[0] > 0 && ints[1] > 0)
1914     st_buffer_size = ints[1] * ST_BLOCK_SIZE;
1915   if (ints[0] > 1 && ints[2] > 0) {
1916     st_write_threshold = ints[2] * ST_BLOCK_SIZE;
1917     if (st_write_threshold > st_buffer_size)
1918       st_write_threshold = st_buffer_size;
1919   }
1920   if (ints[0] > 2 && ints[3] > 0)
1921     st_max_buffers = ints[3];
1922 }
1923 
1924 
1925 static struct file_operations st_fops = {
1926    NULL,            /* lseek - default */
1927    st_read,         /* read - general block-dev read */
1928    st_write,        /* write - general block-dev write */
1929    NULL,            /* readdir - bad */
1930    NULL,            /* select */
1931    st_ioctl,        /* ioctl */
1932    NULL,            /* mmap */
1933    scsi_tape_open,  /* open */
1934    scsi_tape_close, /* release */
1935    NULL             /* fsync */
1936 };
1937 
1938 static int st_attach(Scsi_Device * SDp){
     /* [previous][next][first][last][top][bottom][index][help] */
1939    Scsi_Tape * tpnt;
1940    int i;
1941 
1942    if(SDp->type != TYPE_TAPE) return 1;
1943 
1944    if(st_template.nr_dev >= st_template.dev_max) 
1945      {
1946         SDp->attached--;
1947         return 1;
1948      }
1949 
1950    for(tpnt = scsi_tapes, i=0; i<st_template.dev_max; i++, tpnt++) 
1951      if(!tpnt->device) break;
1952 
1953    if(i >= st_template.dev_max) panic ("scsi_devices corrupt (st)");
1954 
1955    scsi_tapes[i].device = SDp;
1956    if (SDp->scsi_level <= 2)
1957      scsi_tapes[i].mt_status->mt_type = MT_ISSCSI1;
1958    else
1959      scsi_tapes[i].mt_status->mt_type = MT_ISSCSI2;
1960 
1961    st_template.nr_dev++;
1962    return 0;
1963 };
1964 
1965 static int st_detect(Scsi_Device * SDp)
     /* [previous][next][first][last][top][bottom][index][help] */
1966 {
1967   if(SDp->type != TYPE_TAPE) return 0;
1968 
1969   printk("Detected scsi tape st%d at scsi%d, id %d, lun %d\n", 
1970          st_template.dev_noticed++,
1971          SDp->host->host_no , SDp->id, SDp->lun); 
1972   
1973   return 1;
1974 }
1975 
1976 /* Driver initialization */
1977 static void st_init()
     /* [previous][next][first][last][top][bottom][index][help] */
1978 {
1979   int i;
1980   Scsi_Tape * STp;
1981   static int st_registered = 0;
1982 
1983   if (st_template.dev_noticed == 0) return;
1984 
1985   if(!st_registered) {
1986     if (register_chrdev(MAJOR_NR,"st",&st_fops)) {
1987       printk("Unable to get major %d for SCSI tapes\n",MAJOR_NR);
1988       return;
1989     }
1990     st_registered++;
1991   }
1992 
1993   if (scsi_tapes) return;
1994   scsi_tapes = (Scsi_Tape *) scsi_init_malloc(
1995                 (st_template.dev_noticed + ST_EXTRA_DEVS) * 
1996                                               sizeof(Scsi_Tape), GFP_ATOMIC);
1997   st_template.dev_max = st_template.dev_noticed + ST_EXTRA_DEVS;
1998 
1999 #ifdef DEBUG
2000   printk("st: Buffer size %d bytes, write threshold %d bytes.\n",
2001          st_buffer_size, st_write_threshold);
2002 #endif
2003 
2004   for (i=0; i < st_template.dev_max; ++i) {
2005     STp = &(scsi_tapes[i]);
2006     STp->device = NULL;
2007     STp->capacity = 0xfffff;
2008     STp->dirty = 0;
2009     STp->rw = ST_IDLE;
2010     STp->eof = ST_NOEOF;
2011     STp->waiting = NULL;
2012     STp->in_use = 0;
2013     STp->drv_buffer = 1;  /* Try buffering if no mode sense */
2014     STp->density = 0;
2015     STp->do_buffer_writes = ST_BUFFER_WRITES;
2016     STp->do_async_writes = ST_ASYNC_WRITES;
2017     STp->do_read_ahead = ST_READ_AHEAD;
2018     STp->two_fm = ST_TWO_FM;
2019     STp->fast_mteom = ST_FAST_MTEOM;
2020     STp->write_threshold = st_write_threshold;
2021     STp->drv_block = 0;
2022     STp->moves_after_eof = 1;
2023     STp->at_sm = 0;
2024     STp->mt_status = (struct mtget *) scsi_init_malloc(sizeof(struct mtget), GFP_ATOMIC);
2025     /* Initialize status */
2026     memset((void *) scsi_tapes[i].mt_status, 0, sizeof(struct mtget));
2027   }
2028 
2029   /* Allocate the buffers */
2030   st_nbr_buffers = st_template.dev_noticed + ST_EXTRA_DEVS;
2031   if (st_nbr_buffers > st_max_buffers)
2032     st_nbr_buffers = st_max_buffers;
2033   st_buffers = (ST_buffer **) scsi_init_malloc(st_nbr_buffers * 
2034                                                sizeof(ST_buffer *), GFP_ATOMIC);
2035   /* FIXME - if we are hitting this because we are loading a tape module
2036   as a loadable driver, we should not use kmalloc - it will allocate
2037   a 64Kb region in order to buffer about 32Kb.  Try using 31 blocks
2038   instead. */
2039   
2040   for (i=0; i < st_nbr_buffers; i++) {
2041     st_buffers[i] = (ST_buffer *) scsi_init_malloc(sizeof(ST_buffer) - 
2042                                                    1 + st_buffer_size, GFP_ATOMIC | GFP_DMA);
2043 #ifdef DEBUG
2044 /*    printk("st: Buffer address: %p\n", st_buffers[i]); */
2045 #endif
2046     st_buffers[i]->in_use = 0;
2047     st_buffers[i]->writing = 0;
2048   }
2049   return;
2050 }
2051 
2052 static void st_detach(Scsi_Device * SDp)
     /* [previous][next][first][last][top][bottom][index][help] */
2053 {
2054   Scsi_Tape * tpnt;
2055   int i;
2056   
2057   for(tpnt = scsi_tapes, i=0; i<st_template.dev_max; i++, tpnt++) 
2058     if(tpnt->device == SDp) {
2059       tpnt->device = NULL;
2060       SDp->attached--;
2061       st_template.nr_dev--;
2062       st_template.dev_noticed--;
2063       return;
2064     }
2065   return;
2066 }

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