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

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