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

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