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

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