root/drivers/scsi/st.c

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

DEFINITIONS

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

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

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