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: Mon Sep 25 19:52:16 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 "../block/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   }
 127 #endif
 128   scode = sense[2] & 0x0f;
 129   if (!(driver_byte(result) & DRIVER_SENSE) ||
 130       ((sense[0] & 0x70) == 0x70 &&
 131        scode != NO_SENSE &&
 132        scode != RECOVERED_ERROR &&
 133        scode != UNIT_ATTENTION &&
 134        scode != BLANK_CHECK &&
 135        scode != VOLUME_OVERFLOW)) {  /* Abnormal conditions for tape */
 136     printk("st%d: Error %x. ", dev, result);
 137     if (driver_byte(result) & DRIVER_SENSE)
 138       print_sense("st", SCpnt);
 139     else
 140       printk("\n");
 141   }
 142 
 143   if ((sense[0] & 0x70) == 0x70 &&
 144       scode == RECOVERED_ERROR
 145 #if ST_RECOVERED_WRITE_FATAL
 146       && SCpnt->data_cmnd[0] != WRITE_6
 147       && SCpnt->data_cmnd[0] != WRITE_FILEMARKS
 148 #endif
 149       ) {
 150     scsi_tapes[dev].recover_count++;
 151     scsi_tapes[dev].mt_status->mt_erreg += (1 << MT_ST_SOFTERR_SHIFT);
 152 #if DEBUG
 153     if (debugging) {  /* This is compiled always on purpose */
 154       if (SCpnt->data_cmnd[0] == READ_6)
 155         stp = "read";
 156       else if (SCpnt->data_cmnd[0] == WRITE_6)
 157         stp = "write";
 158       else
 159         stp = "ioctl";
 160       printk("st%d: Recovered %s error (%d).\n", dev, stp,
 161              scsi_tapes[dev].recover_count);
 162     }
 163 #endif
 164     return 0;
 165   }
 166   return (-EIO);
 167 }
 168 
 169 
 170 /* Wakeup from interrupt */
 171         static void
 172 st_sleep_done (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 173 {
 174   unsigned int st_nbr;
 175   int remainder;
 176   Scsi_Tape * STp;
 177 
 178   if ((st_nbr = TAPE_NR(SCpnt->request.rq_dev)) < st_template.nr_dev) {
 179     STp = &(scsi_tapes[st_nbr]);
 180     if ((STp->buffer)->writing &&
 181         (SCpnt->sense_buffer[0] & 0x70) == 0x70 &&
 182         (SCpnt->sense_buffer[2] & 0x40)) {
 183       /* EOM at write-behind, has all been written? */
 184       if ((SCpnt->sense_buffer[0] & 0x80) != 0)
 185         remainder = (SCpnt->sense_buffer[3] << 24) |
 186               (SCpnt->sense_buffer[4] << 16) |
 187                 (SCpnt->sense_buffer[5] << 8) | SCpnt->sense_buffer[6];
 188       else
 189         remainder = 0;
 190       if ((SCpnt->sense_buffer[2] & 0x0f) == VOLUME_OVERFLOW ||
 191           remainder > 0)
 192         (STp->buffer)->last_result = SCpnt->result; /* Error */
 193       else
 194         (STp->buffer)->last_result = INT_MAX; /* OK */
 195     }
 196     else
 197       (STp->buffer)->last_result = SCpnt->result;
 198     if ((STp->buffer)->writing) {
 199       /* Process errors before releasing request */
 200       (STp->buffer)->last_result_fatal = st_chk_result(SCpnt);
 201       SCpnt->request.rq_status = RQ_INACTIVE;
 202     }
 203     else
 204       SCpnt->request.rq_status = RQ_SCSI_DONE;
 205     if (!(STp->buffer)->writing || STp->write_pending)
 206       wake_up( &(STp->waiting) );
 207     STp->write_pending = 0;
 208   }
 209 #if DEBUG
 210   else if (debugging)
 211     printk("st?: Illegal interrupt device %x\n", st_nbr);
 212 #endif
 213 }
 214 
 215 
 216 /* Do the scsi command */
 217         static Scsi_Cmnd *
 218 st_do_scsi(Scsi_Cmnd *SCpnt, Scsi_Tape *STp, unsigned char *cmd, int bytes,
     /* [previous][next][first][last][top][bottom][index][help] */
 219            int timeout, int retries)
 220 {
 221   unsigned int flags;
 222 
 223   if (SCpnt == NULL)
 224     if ((SCpnt = allocate_device(NULL, STp->device, 1)) == NULL) {
 225       printk("st%d: Can't get SCSI request.\n", TAPE_NR(STp->devt));
 226       return NULL;
 227     }
 228 
 229   cmd[1] |= (SCpnt->lun << 5) & 0xe0;
 230   SCpnt->request.rq_status = RQ_SCSI_BUSY;
 231   SCpnt->request.rq_dev = STp->devt;
 232 
 233   scsi_do_cmd(SCpnt, (void *)cmd, (STp->buffer)->b_data, bytes,
 234               st_sleep_done, timeout, retries);
 235 
 236   /* this must be done with interrupts off */
 237   save_flags (flags);
 238   cli();
 239   if (SCpnt->request.rq_status != RQ_SCSI_DONE)
 240     sleep_on( &(STp->waiting) );
 241   restore_flags(flags);
 242 
 243   (STp->buffer)->last_result_fatal = st_chk_result(SCpnt);
 244 
 245   return SCpnt;
 246 }
 247 
 248 
 249 /* Handle the write-behind checking */
 250         static void
 251 write_behind_check(Scsi_Tape *STp)
     /* [previous][next][first][last][top][bottom][index][help] */
 252 {
 253   ST_buffer * STbuffer;
 254   unsigned long flags;
 255 
 256   STbuffer = STp->buffer;
 257 
 258   save_flags(flags);
 259   cli();
 260   if (STp->write_pending) {
 261 #if DEBUG
 262     STp->nbr_waits++;
 263 #endif
 264     sleep_on( &(STp->waiting) );
 265     STp->write_pending = 0;
 266   }
 267 #if DEBUG
 268   else
 269     STp->nbr_finished++;
 270 #endif
 271   restore_flags(flags);
 272 
 273   if (STbuffer->writing < STbuffer->buffer_bytes)
 274     memcpy(STbuffer->b_data,
 275            STbuffer->b_data + STbuffer->writing,
 276            STbuffer->buffer_bytes - STbuffer->writing);
 277   STbuffer->buffer_bytes -= STbuffer->writing;
 278   if (STp->drv_block >= 0) {
 279     if (STp->block_size == 0)
 280       STp->drv_block++;
 281     else
 282       STp->drv_block += STbuffer->writing / STp->block_size;
 283   }
 284   STbuffer->writing = 0;
 285 
 286   return;
 287 }
 288 
 289 
 290 /* Back over EOF if it has been inadvertently crossed (ioctl not used because
 291    it messes up the block number). */
 292         static int
 293 back_over_eof(Scsi_Tape *STp)
     /* [previous][next][first][last][top][bottom][index][help] */
 294 {
 295   Scsi_Cmnd *SCpnt;
 296   unsigned char cmd[10];
 297 
 298   cmd[0] = SPACE;
 299   cmd[1] = 0x01; /* Space FileMarks */
 300   cmd[2] = cmd[3] = cmd[4] = 0xff;  /* -1 filemarks */
 301   cmd[5] = 0;
 302 
 303   SCpnt = st_do_scsi(NULL, STp, cmd, 0, ST_TIMEOUT, MAX_RETRIES);
 304   if (!SCpnt)
 305     return (-EBUSY);
 306 
 307   SCpnt->request.rq_status = RQ_INACTIVE;
 308   if ((STp->buffer)->last_result != 0) {
 309     printk("st%d: Backing over filemark failed.\n", TAPE_NR(STp->devt));
 310     if ((STp->mt_status)->mt_fileno >= 0)
 311       (STp->mt_status)->mt_fileno += 1;
 312     (STp->mt_status)->mt_blkno = 0;
 313   }
 314 
 315   return (STp->buffer)->last_result_fatal;
 316 }
 317 
 318 
 319 /* Flush the write buffer (never need to write if variable blocksize). */
 320         static int
 321 flush_write_buffer(Scsi_Tape *STp)
     /* [previous][next][first][last][top][bottom][index][help] */
 322 {
 323   int offset, transfer, blks;
 324   int result;
 325   unsigned char cmd[10];
 326   Scsi_Cmnd *SCpnt;
 327 
 328   if ((STp->buffer)->writing) {
 329     write_behind_check(STp);
 330     if ((STp->buffer)->last_result_fatal) {
 331 #if DEBUG
 332       if (debugging)
 333         printk("st%d: Async write error (flush) %x.\n", TAPE_NR(STp->devt),
 334                (STp->buffer)->last_result);
 335 #endif
 336       if ((STp->buffer)->last_result == INT_MAX)
 337         return (-ENOSPC);
 338       return (-EIO);
 339     }
 340   }
 341 
 342   if (STp->block_size == 0)
 343     return 0;
 344 
 345   result = 0;
 346   if (STp->dirty == 1) {
 347 
 348     offset = (STp->buffer)->buffer_bytes;
 349     transfer = ((offset + STp->block_size - 1) /
 350                 STp->block_size) * STp->block_size;
 351 #if DEBUG
 352     if (debugging)
 353       printk("st%d: Flushing %d bytes.\n", TAPE_NR(STp->devt), transfer);
 354 #endif
 355     memset((STp->buffer)->b_data + offset, 0, transfer - offset);
 356 
 357     memset(cmd, 0, 10);
 358     cmd[0] = WRITE_6;
 359     cmd[1] = 1;
 360     blks = transfer / STp->block_size;
 361     cmd[2] = blks >> 16;
 362     cmd[3] = blks >> 8;
 363     cmd[4] = blks;
 364 
 365     SCpnt = st_do_scsi(NULL, STp, cmd, transfer, ST_TIMEOUT, MAX_WRITE_RETRIES);
 366     if (!SCpnt)
 367       return (-EBUSY);
 368 
 369     if ((STp->buffer)->last_result_fatal != 0) {
 370       printk("st%d: Error on flush.\n", TAPE_NR(STp->devt));
 371       if ((SCpnt->sense_buffer[0] & 0x70) == 0x70 &&
 372           (SCpnt->sense_buffer[2] & 0x40) &&
 373           (SCpnt->sense_buffer[2] & 0x0f) != VOLUME_OVERFLOW) {
 374         STp->dirty = 0;
 375         (STp->buffer)->buffer_bytes = 0;
 376         result = (-ENOSPC);
 377       }
 378       else
 379         result = (-EIO);
 380       STp->drv_block = (-1);
 381     }
 382     else {
 383       if (STp->drv_block >= 0)
 384         STp->drv_block += blks;
 385       STp->dirty = 0;
 386       (STp->buffer)->buffer_bytes = 0;
 387     }
 388     SCpnt->request.rq_status = RQ_INACTIVE;  /* Mark as not busy */
 389   }
 390   return result;
 391 }
 392 
 393 
 394 /* Flush the tape buffer. The tape will be positioned correctly unless
 395    seek_next is true. */
 396         static int
 397 flush_buffer(struct inode * inode, struct file * filp, int seek_next)
     /* [previous][next][first][last][top][bottom][index][help] */
 398 {
 399   int backspace, result;
 400   Scsi_Tape * STp;
 401   ST_buffer * STbuffer;
 402   int dev = TAPE_NR(inode->i_rdev);
 403 
 404   STp = &(scsi_tapes[dev]);
 405   STbuffer = STp->buffer;
 406 
 407   /*
 408    * If there was a bus reset, block further access
 409    * to this device.
 410    */
 411   if( STp->device->was_reset )
 412     return (-EIO);
 413 
 414   if (STp->ready != ST_READY)
 415     return 0;
 416 
 417   if (STp->rw == ST_WRITING)  /* Writing */
 418     return flush_write_buffer(STp);
 419 
 420   if (STp->block_size == 0)
 421     return 0;
 422 
 423   backspace = ((STp->buffer)->buffer_bytes +
 424     (STp->buffer)->read_pointer) / STp->block_size -
 425       ((STp->buffer)->read_pointer + STp->block_size - 1) /
 426         STp->block_size;
 427   (STp->buffer)->buffer_bytes = 0;
 428   (STp->buffer)->read_pointer = 0;
 429   result = 0;
 430   if (!seek_next) {
 431     if ((STp->eof == ST_FM) && !STp->eof_hit) {
 432       result = back_over_eof(STp); /* Back over the EOF hit */
 433       if (!result) {
 434         STp->eof = ST_NOEOF;
 435         STp->eof_hit = 0;
 436       }
 437     }
 438     if (!result && backspace > 0)
 439       result = st_int_ioctl(inode, filp, MTBSR, backspace);
 440   }
 441   return result;
 442 
 443 }
 444 
 445 
 446 /* Open the device */
 447         static int
 448 scsi_tape_open(struct inode * inode, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
 449 {
 450     unsigned short flags;
 451     int i;
 452     unsigned char cmd[10];
 453     Scsi_Cmnd * SCpnt;
 454     Scsi_Tape * STp;
 455     int dev = TAPE_NR(inode->i_rdev);
 456 
 457     if (dev >= st_template.dev_max || !scsi_tapes[dev].device)
 458       return (-ENXIO);
 459     STp = &(scsi_tapes[dev]);
 460     if (STp->in_use) {
 461       printk("st%d: Device already in use.\n", dev);
 462       return (-EBUSY);
 463     }
 464 
 465     /* Allocate buffer for this user */
 466     for (i=0; i < st_nbr_buffers; i++)
 467       if (!st_buffers[i]->in_use)
 468         break;
 469     if (i >= st_nbr_buffers) {
 470       STp->buffer = new_tape_buffer(FALSE);
 471       if (STp->buffer == NULL) {
 472         printk("st%d: No free buffers.\n", dev);
 473         return (-EBUSY);
 474       }
 475     }
 476     else
 477       STp->buffer = st_buffers[i];
 478     (STp->buffer)->in_use = 1;
 479     (STp->buffer)->writing = 0;
 480     STp->in_use = 1;
 481 
 482     flags = filp->f_flags;
 483     STp->write_prot = ((flags & O_ACCMODE) == O_RDONLY);
 484 
 485     STp->dirty = 0;
 486     STp->write_pending = 0;
 487     STp->rw = ST_IDLE;
 488     STp->ready = ST_READY;
 489     if (STp->eof != ST_EOD)  /* Save EOD across opens */
 490       STp->eof = ST_NOEOF;
 491     STp->eof_hit = 0;
 492     STp->recover_count = 0;
 493 #if DEBUG
 494     STp->nbr_waits = STp->nbr_finished = 0;
 495 #endif
 496 
 497     memset ((void *) &cmd[0], 0, 10);
 498     cmd[0] = TEST_UNIT_READY;
 499 
 500     SCpnt = st_do_scsi(NULL, STp, cmd, 0, ST_LONG_TIMEOUT, MAX_READY_RETRIES);
 501     if (!SCpnt)
 502       return (-EBUSY);
 503 
 504     if ((SCpnt->sense_buffer[0] & 0x70) == 0x70 &&
 505         (SCpnt->sense_buffer[2] & 0x0f) == UNIT_ATTENTION) { /* New media? */
 506       (STp->mt_status)->mt_fileno = 0 ;
 507       memset ((void *) &cmd[0], 0, 10);
 508       cmd[0] = TEST_UNIT_READY;
 509 
 510       SCpnt = st_do_scsi(SCpnt, STp, cmd, 0, ST_LONG_TIMEOUT, MAX_READY_RETRIES);
 511 
 512       (STp->mt_status)->mt_fileno = STp->drv_block = 0;
 513       STp->eof = ST_NOEOF;
 514       (STp->device)->was_reset = 0;
 515     }
 516 
 517     if ((STp->buffer)->last_result_fatal != 0) {
 518       if ((SCpnt->sense_buffer[0] & 0x70) == 0x70 &&
 519           (SCpnt->sense_buffer[2] & 0x0f) == NO_TAPE) {
 520         (STp->mt_status)->mt_fileno = STp->drv_block = 0 ;
 521         printk("st%d: No tape.\n", dev);
 522         STp->ready = ST_NO_TAPE;
 523       } else {
 524         (STp->mt_status)->mt_fileno = STp->drv_block = (-1);
 525         STp->ready = ST_NOT_READY;
 526       }
 527       SCpnt->request.rq_status = RQ_INACTIVE;  /* Mark as not busy */
 528       STp->density = 0;         /* Clear the erroneous "residue" */
 529       STp->write_prot = 0;
 530       STp->block_size = 0;
 531       STp->eof = ST_NOEOF;
 532       (STp->mt_status)->mt_fileno = STp->drv_block = 0;
 533       STp->door_locked = ST_UNLOCKED;
 534       if (scsi_tapes[dev].device->host->hostt->usage_count)
 535         (*scsi_tapes[dev].device->host->hostt->usage_count)++;
 536       if(st_template.usage_count) (*st_template.usage_count)++;
 537       return 0;
 538     }
 539 
 540     memset ((void *) &cmd[0], 0, 10);
 541     cmd[0] = READ_BLOCK_LIMITS;
 542 
 543     SCpnt = st_do_scsi(SCpnt, STp, cmd, 6, ST_TIMEOUT, MAX_READY_RETRIES);
 544 
 545     if (!SCpnt->result && !SCpnt->sense_buffer[0]) {
 546       STp->max_block = ((STp->buffer)->b_data[1] << 16) |
 547         ((STp->buffer)->b_data[2] << 8) | (STp->buffer)->b_data[3];
 548       STp->min_block = ((STp->buffer)->b_data[4] << 8) |
 549         (STp->buffer)->b_data[5];
 550 #if DEBUG
 551       if (debugging)
 552         printk("st%d: Block limits %d - %d bytes.\n", dev, STp->min_block,
 553                STp->max_block);
 554 #endif
 555     }
 556     else {
 557       STp->min_block = STp->max_block = (-1);
 558 #if DEBUG
 559       if (debugging)
 560         printk("st%d: Can't read block limits.\n", dev);
 561 #endif
 562     }
 563 
 564     memset ((void *) &cmd[0], 0, 10);
 565     cmd[0] = MODE_SENSE;
 566     cmd[4] = 12;
 567 
 568     SCpnt = st_do_scsi(SCpnt, STp, cmd, 12, ST_TIMEOUT, MAX_READY_RETRIES);
 569 
 570     if ((STp->buffer)->last_result_fatal != 0) {
 571 #if DEBUG
 572       if (debugging)
 573         printk("st%d: No Mode Sense.\n", dev);
 574 #endif
 575       STp->block_size = ST_DEFAULT_BLOCK;  /* Educated guess (?) */
 576       (STp->buffer)->last_result_fatal = 0;  /* Prevent error propagation */
 577     }
 578     else {
 579 
 580 #if DEBUG
 581       if (debugging)
 582         printk("st%d: Mode sense. Length %d, medium %x, WBS %x, BLL %d\n", dev,
 583                (STp->buffer)->b_data[0], (STp->buffer)->b_data[1],
 584                (STp->buffer)->b_data[2], (STp->buffer)->b_data[3]);
 585 #endif
 586 
 587       if ((STp->buffer)->b_data[3] >= 8) {
 588         STp->drv_buffer = ((STp->buffer)->b_data[2] >> 4) & 7;
 589         STp->density = (STp->buffer)->b_data[4];
 590         STp->block_size = (STp->buffer)->b_data[9] * 65536 +
 591           (STp->buffer)->b_data[10] * 256 + (STp->buffer)->b_data[11];
 592 #if DEBUG
 593         if (debugging)
 594           printk("st%d: Density %x, tape length: %x, drv buffer: %d\n",
 595                  dev, STp->density, (STp->buffer)->b_data[5] * 65536 +
 596                  (STp->buffer)->b_data[6] * 256 + (STp->buffer)->b_data[7],
 597                  STp->drv_buffer);
 598 #endif
 599       }
 600 
 601       if (STp->block_size > (STp->buffer)->buffer_size &&
 602           !enlarge_buffer(STp->buffer, STp->block_size)) {
 603         printk("st%d: Blocksize %d too large for buffer.\n", dev,
 604                STp->block_size);
 605         (STp->buffer)->in_use = 0;
 606         STp->in_use = 0;
 607         return (-EIO);
 608       }
 609     }
 610     SCpnt->request.rq_status = RQ_INACTIVE;  /* Mark as not busy */
 611 
 612     if (STp->block_size > 0)
 613       (STp->buffer)->buffer_blocks = st_buffer_size / STp->block_size;
 614     else
 615       (STp->buffer)->buffer_blocks = 1;
 616     (STp->buffer)->buffer_bytes = (STp->buffer)->read_pointer = 0;
 617 
 618 #if DEBUG
 619     if (debugging)
 620       printk("st%d: Block size: %d, buffer size: %d (%d blocks).\n", dev,
 621              STp->block_size, (STp->buffer)->buffer_size,
 622              (STp->buffer)->buffer_blocks);
 623 #endif
 624 
 625     STp->drv_write_prot = ((STp->buffer)->b_data[2] & 0x80) != 0;
 626     if (STp->drv_write_prot) {
 627       STp->write_prot = 1;
 628 #if DEBUG
 629       if (debugging)
 630         printk( "st%d: Write protected\n", dev);
 631 #endif
 632       if ((flags & O_ACCMODE) == O_WRONLY || (flags & O_ACCMODE) == O_RDWR) {
 633         (STp->buffer)->in_use = 0;
 634         STp->buffer = 0;
 635         STp->in_use = 0;
 636         return (-EROFS);
 637       }
 638     }
 639 
 640     if (scsi_tapes[dev].device->host->hostt->usage_count)
 641       (*scsi_tapes[dev].device->host->hostt->usage_count)++;
 642     if(st_template.usage_count) (*st_template.usage_count)++;
 643 
 644     return 0;
 645 }
 646 
 647 
 648 /* Close the device*/
 649         static void
 650 scsi_tape_close(struct inode * inode, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
 651 {
 652     int result;
 653     int rewind;
 654     static unsigned char cmd[10];
 655     Scsi_Cmnd * SCpnt;
 656     Scsi_Tape * STp;
 657     kdev_t devt = inode->i_rdev;
 658     int dev;
 659 
 660     dev = TAPE_NR(devt);
 661     rewind = (MINOR(devt) & 0x80) == 0;
 662 
 663     STp = &(scsi_tapes[dev]);
 664 
 665     if ( STp->rw == ST_WRITING && !(STp->device)->was_reset) {
 666 
 667       result = flush_write_buffer(STp);
 668 
 669 #if DEBUG
 670       if (debugging) {
 671         printk("st%d: File length %ld bytes.\n", dev, (long)(filp->f_pos));
 672         printk("st%d: Async write waits %d, finished %d.\n", dev,
 673                STp->nbr_waits, STp->nbr_finished);
 674       }
 675 #endif
 676 
 677       if (result == 0 || result == (-ENOSPC)) {
 678 
 679         memset(cmd, 0, 10);
 680         cmd[0] = WRITE_FILEMARKS;
 681         cmd[4] = 1 + STp->two_fm;
 682 
 683         SCpnt = st_do_scsi(NULL, STp, cmd, 0, ST_TIMEOUT, MAX_WRITE_RETRIES);
 684         if (!SCpnt)
 685           return;
 686 
 687         if ((STp->buffer)->last_result_fatal != 0)
 688           printk("st%d: Error on write filemark.\n", dev);
 689         else {
 690           if ((STp->mt_status)->mt_fileno >= 0)
 691               (STp->mt_status)->mt_fileno++ ;
 692           STp->drv_block = 0;
 693           if (STp->two_fm)
 694             back_over_eof(STp);
 695         }
 696         SCpnt->request.rq_status = RQ_INACTIVE;  /* Mark as not busy */
 697       }
 698 
 699 #if DEBUG
 700       if (debugging)
 701         printk("st%d: Buffer flushed, %d EOF(s) written\n", dev, cmd[4]);
 702 #endif
 703     }
 704     else if (!rewind) {
 705 #if ST_IN_FILE_POS
 706       flush_buffer(inode, filp, 0);
 707 #else
 708       if ((STp->eof == ST_FM) && !STp->eof_hit)
 709         back_over_eof(STp);
 710 #endif
 711     }
 712 
 713     if (rewind)
 714       st_int_ioctl(inode, filp, MTREW, 1);
 715 
 716     if (STp->door_locked == ST_LOCKED_AUTO)
 717       st_int_ioctl(inode, filp, MTUNLOCK, 0);
 718 
 719     if (STp->buffer != NULL) {
 720       normalize_buffer(STp->buffer);
 721       (STp->buffer)->in_use = 0;
 722     }
 723     STp->in_use = 0;
 724 
 725     if (scsi_tapes[dev].device->host->hostt->usage_count)
 726       (*scsi_tapes[dev].device->host->hostt->usage_count)--;
 727     if(st_template.usage_count) (*st_template.usage_count)--;
 728 
 729     return;
 730 }
 731 
 732 
 733 /* Write command */
 734         static int
 735 st_write(struct inode * inode, struct file * filp, const char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 736 {
 737     int total, do_count, blks, retval, transfer;
 738     int write_threshold;
 739     int doing_write = 0;
 740     static unsigned char cmd[10];
 741     const char *b_point;
 742     Scsi_Cmnd * SCpnt = NULL;
 743     Scsi_Tape * STp;
 744     int dev = TAPE_NR(inode->i_rdev);
 745 
 746     STp = &(scsi_tapes[dev]);
 747     if (STp->ready != ST_READY)
 748       return (-EIO);
 749 
 750     /*
 751      * If there was a bus reset, block further access
 752      * to this device.
 753      */
 754     if( STp->device->was_reset )
 755       return (-EIO);
 756 
 757 #if DEBUG
 758     if (!STp->in_use) {
 759       printk("st%d: Incorrect device.\n", dev);
 760       return (-EIO);
 761     }
 762 #endif
 763 
 764     if (STp->write_prot)
 765       return (-EACCES);
 766 
 767     if (STp->block_size == 0 &&
 768         count > (STp->buffer)->buffer_size &&
 769         !enlarge_buffer(STp->buffer, count))
 770       return (-EOVERFLOW);
 771 
 772     if (STp->do_auto_lock && STp->door_locked == ST_UNLOCKED &&
 773         !st_int_ioctl(inode, filp, MTLOCK, 0))
 774       STp->door_locked = ST_LOCKED_AUTO;
 775 
 776     if (STp->rw == ST_READING) {
 777       retval = flush_buffer(inode, filp, 0);
 778       if (retval)
 779         return retval;
 780       STp->rw = ST_WRITING;
 781     }
 782 
 783     if (STp->moves_after_eof < 255)
 784       STp->moves_after_eof++;
 785 
 786     if ((STp->buffer)->writing) {
 787       write_behind_check(STp);
 788       if ((STp->buffer)->last_result_fatal) {
 789 #if DEBUG
 790         if (debugging)
 791           printk("st%d: Async write error (write) %x.\n", dev,
 792                  (STp->buffer)->last_result);
 793 #endif
 794         if ((STp->buffer)->last_result == INT_MAX) {
 795           retval = (-ENOSPC);  /* All has been written */
 796           STp->eof = ST_EOM_OK;
 797         }
 798         else
 799           retval = (-EIO);
 800         return retval;
 801       }
 802     }
 803 
 804     if (STp->eof == ST_EOM_OK)
 805       return (-ENOSPC);
 806     else if (STp->eof == ST_EOM_ERROR)
 807       return (-EIO);
 808 
 809     if (!STp->do_buffer_writes) {
 810       if (STp->block_size != 0 && (count % STp->block_size) != 0)
 811         return (-EIO);   /* Write must be integral number of blocks */
 812       write_threshold = 1;
 813     }
 814     else
 815       write_threshold = (STp->buffer)->buffer_blocks * STp->block_size;
 816     if (!STp->do_async_writes)
 817       write_threshold--;
 818 
 819     total = count;
 820 
 821     memset(cmd, 0, 10);
 822     cmd[0] = WRITE_6;
 823     cmd[1] = (STp->block_size != 0);
 824 
 825     STp->rw = ST_WRITING;
 826 
 827     b_point = buf;
 828     while((STp->block_size == 0 && !STp->do_async_writes && count > 0) ||
 829           (STp->block_size != 0 &&
 830            (STp->buffer)->buffer_bytes + count > write_threshold))
 831     {
 832       doing_write = 1;
 833       if (STp->block_size == 0)
 834         do_count = count;
 835       else {
 836         do_count = (STp->buffer)->buffer_blocks * STp->block_size -
 837           (STp->buffer)->buffer_bytes;
 838         if (do_count > count)
 839           do_count = count;
 840       }
 841       memcpy_fromfs((STp->buffer)->b_data +
 842                     (STp->buffer)->buffer_bytes, b_point, do_count);
 843 
 844       if (STp->block_size == 0)
 845         blks = transfer = do_count;
 846       else {
 847         blks = ((STp->buffer)->buffer_bytes + do_count) /
 848           STp->block_size;
 849         transfer = blks * STp->block_size;
 850       }
 851       cmd[2] = blks >> 16;
 852       cmd[3] = blks >> 8;
 853       cmd[4] = blks;
 854 
 855       SCpnt = st_do_scsi(SCpnt, STp, cmd, transfer, ST_TIMEOUT, MAX_WRITE_RETRIES);
 856       if (!SCpnt)
 857         return (-EBUSY);
 858 
 859       if ((STp->buffer)->last_result_fatal != 0) {
 860 #if DEBUG
 861         if (debugging)
 862           printk("st%d: Error on write:\n", dev);
 863 #endif
 864         if ((SCpnt->sense_buffer[0] & 0x70) == 0x70 &&
 865             (SCpnt->sense_buffer[2] & 0x40)) {
 866           if (STp->block_size != 0 && (SCpnt->sense_buffer[0] & 0x80) != 0)
 867             transfer = (SCpnt->sense_buffer[3] << 24) |
 868               (SCpnt->sense_buffer[4] << 16) |
 869                 (SCpnt->sense_buffer[5] << 8) | SCpnt->sense_buffer[6];
 870           else if (STp->block_size == 0 &&
 871                    (SCpnt->sense_buffer[2] & 0x0f) == VOLUME_OVERFLOW)
 872             transfer = do_count;
 873           else
 874             transfer = 0;
 875           if (STp->block_size != 0)
 876             transfer *= STp->block_size;
 877           if (transfer <= do_count) {
 878             filp->f_pos += do_count - transfer;
 879             count -= do_count - transfer;
 880             if (STp->drv_block >= 0) {
 881               if (STp->block_size == 0 && transfer < do_count)
 882                 STp->drv_block++;
 883               else if (STp->block_size != 0)
 884                 STp->drv_block += (do_count - transfer) / STp->block_size;
 885             }
 886             STp->eof = ST_EOM_OK;
 887             retval = (-ENOSPC); /* EOM within current request */
 888 #if DEBUG
 889             if (debugging)
 890               printk("st%d: EOM with %d bytes unwritten.\n",
 891                      dev, transfer);
 892 #endif
 893           }
 894           else {
 895             STp->eof = ST_EOM_ERROR;
 896             STp->drv_block = (-1);    /* Too cautious? */
 897             retval = (-EIO); /* EOM for old data */
 898 #if DEBUG
 899             if (debugging)
 900               printk("st%d: EOM with lost data.\n", dev);
 901 #endif
 902           }
 903         }
 904         else {
 905           STp->drv_block = (-1);    /* Too cautious? */
 906           retval = (-EIO);
 907         }
 908 
 909         SCpnt->request.rq_status = RQ_INACTIVE;  /* Mark as not busy */
 910         (STp->buffer)->buffer_bytes = 0;
 911         STp->dirty = 0;
 912         if (count < total)
 913           return total - count;
 914         else
 915           return retval;
 916       }
 917       filp->f_pos += do_count;
 918       b_point += do_count;
 919       count -= do_count;
 920       if (STp->drv_block >= 0) {
 921         if (STp->block_size == 0)
 922           STp->drv_block++;
 923         else
 924           STp->drv_block += blks;
 925       }
 926       (STp->buffer)->buffer_bytes = 0;
 927       STp->dirty = 0;
 928     }
 929     if (count != 0) {
 930       STp->dirty = 1;
 931       memcpy_fromfs((STp->buffer)->b_data +
 932                     (STp->buffer)->buffer_bytes,b_point,count);
 933       filp->f_pos += count;
 934       (STp->buffer)->buffer_bytes += count;
 935       count = 0;
 936     }
 937 
 938     if (doing_write && (STp->buffer)->last_result_fatal != 0) {
 939       SCpnt->request.rq_status = RQ_INACTIVE;
 940       return (STp->buffer)->last_result_fatal;
 941     }
 942 
 943     if (STp->do_async_writes &&
 944         ((STp->buffer)->buffer_bytes >= STp->write_threshold ||
 945          STp->block_size == 0) ) {
 946       /* Schedule an asynchronous write */
 947       if (!SCpnt) {
 948         SCpnt = allocate_device(NULL, STp->device, 1);
 949         if (!SCpnt)
 950           return (-EBUSY);
 951       }
 952       if (STp->block_size == 0)
 953         (STp->buffer)->writing = (STp->buffer)->buffer_bytes;
 954       else
 955         (STp->buffer)->writing = ((STp->buffer)->buffer_bytes /
 956           STp->block_size) * STp->block_size;
 957       STp->dirty = !((STp->buffer)->writing ==
 958                      (STp->buffer)->buffer_bytes);
 959 
 960       if (STp->block_size == 0)
 961         blks = (STp->buffer)->writing;
 962       else
 963         blks = (STp->buffer)->writing / STp->block_size;
 964       cmd[2] = blks >> 16;
 965       cmd[3] = blks >> 8;
 966       cmd[4] = blks;
 967       SCpnt->request.rq_status = RQ_SCSI_BUSY;
 968       SCpnt->request.rq_dev = STp->devt;
 969       STp->write_pending = 1;
 970       scsi_do_cmd (SCpnt,
 971                    (void *) cmd, (STp->buffer)->b_data,
 972                    (STp->buffer)->writing,
 973                    st_sleep_done, ST_TIMEOUT, MAX_WRITE_RETRIES);
 974     }
 975     else
 976       SCpnt->request.rq_status = RQ_INACTIVE;  /* Mark as not busy */
 977 
 978     STp->at_sm &= (total != 0);
 979     return( total);
 980 }   
 981 
 982 
 983 /* Read command */
 984         static int
 985 st_read(struct inode * inode, struct file * filp, char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 986 {
 987     int total;
 988     int transfer, blks, bytes;
 989     static unsigned char cmd[10];
 990     Scsi_Cmnd * SCpnt = NULL;
 991     Scsi_Tape * STp;
 992     int dev = TAPE_NR(inode->i_rdev);
 993 
 994     STp = &(scsi_tapes[dev]);
 995     if (STp->ready != ST_READY)
 996       return (-EIO);
 997 #if DEBUG
 998     if (!STp->in_use) {
 999       printk("st%d: Incorrect device.\n", dev);
1000       return (-EIO);
1001     }
1002 #endif
1003 
1004     if (STp->block_size == 0 &&
1005         count > (STp->buffer)->buffer_size &&
1006         !enlarge_buffer(STp->buffer, count))
1007       return (-EOVERFLOW);
1008 
1009     if (!(STp->do_read_ahead) && STp->block_size != 0 &&
1010         (count % STp->block_size) != 0)
1011       return (-EIO);    /* Read must be integral number of blocks */
1012 
1013     if (STp->do_auto_lock && STp->door_locked == ST_UNLOCKED &&
1014         !st_int_ioctl(inode, filp, MTLOCK, 0))
1015       STp->door_locked = ST_LOCKED_AUTO;
1016 
1017     if (STp->rw == ST_WRITING) {
1018       transfer = flush_buffer(inode, filp, 0);
1019       if (transfer)
1020         return transfer;
1021       STp->rw = ST_READING;
1022     }
1023     if (STp->moves_after_eof < 255)
1024       STp->moves_after_eof++;
1025 
1026 #if DEBUG
1027     if (debugging && STp->eof != ST_NOEOF)
1028       printk("st%d: EOF flag up. Bytes %d\n", dev,
1029              (STp->buffer)->buffer_bytes);
1030 #endif
1031     if (((STp->buffer)->buffer_bytes == 0) &&
1032         (STp->eof == ST_EOM_OK || STp->eof == ST_EOD))
1033       return (-EIO);  /* EOM or Blank Check */
1034 
1035     STp->rw = ST_READING;
1036 
1037     for (total = 0; total < count; ) {
1038 
1039       if ((STp->buffer)->buffer_bytes == 0 &&
1040           STp->eof == ST_NOEOF) {
1041 
1042         memset(cmd, 0, 10);
1043         cmd[0] = READ_6;
1044         cmd[1] = (STp->block_size != 0);
1045         if (STp->block_size == 0)
1046           blks = bytes = count;
1047         else {
1048           if (STp->do_read_ahead) {
1049             blks = (STp->buffer)->buffer_blocks;
1050             bytes = blks * STp->block_size;
1051           }
1052           else {
1053             bytes = count;
1054             if (bytes > (STp->buffer)->buffer_size)
1055               bytes = (STp->buffer)->buffer_size;
1056             blks = bytes / STp->block_size;
1057             bytes = blks * STp->block_size;
1058           }
1059         }
1060         cmd[2] = blks >> 16;
1061         cmd[3] = blks >> 8;
1062         cmd[4] = blks;
1063 
1064         SCpnt = st_do_scsi(SCpnt, STp, cmd, bytes, ST_TIMEOUT, MAX_RETRIES);
1065         if (!SCpnt)
1066           return (-EBUSY);
1067 
1068         (STp->buffer)->read_pointer = 0;
1069         STp->eof_hit = 0;
1070         STp->at_sm = 0;
1071 
1072         if ((STp->buffer)->last_result_fatal) {
1073 #if DEBUG
1074           if (debugging)
1075             printk("st%d: Sense: %2x %2x %2x %2x %2x %2x %2x %2x\n", dev,
1076                    SCpnt->sense_buffer[0], SCpnt->sense_buffer[1],
1077                    SCpnt->sense_buffer[2], SCpnt->sense_buffer[3],
1078                    SCpnt->sense_buffer[4], SCpnt->sense_buffer[5],
1079                    SCpnt->sense_buffer[6], SCpnt->sense_buffer[7]);
1080 #endif
1081           if ((SCpnt->sense_buffer[0] & 0x70) == 0x70) { /* extended sense */
1082 
1083             if ((SCpnt->sense_buffer[2] & 0xe0) != 0) { /* EOF, EOM, or ILI */
1084 
1085               if ((SCpnt->sense_buffer[0] & 0x80) != 0)
1086                 transfer = (SCpnt->sense_buffer[3] << 24) |
1087                   (SCpnt->sense_buffer[4] << 16) |
1088                     (SCpnt->sense_buffer[5] << 8) | SCpnt->sense_buffer[6];
1089               else
1090                 transfer = 0;
1091               if (STp->block_size == 0 &&
1092                   (SCpnt->sense_buffer[2] & 0x0f) == MEDIUM_ERROR)
1093                 transfer = bytes;
1094 
1095               if (SCpnt->sense_buffer[2] & 0x20) {
1096                 if (STp->block_size == 0) {
1097                   if (transfer <= 0)
1098                     transfer = 0;
1099                   (STp->buffer)->buffer_bytes = bytes - transfer;
1100                 }
1101                 else {
1102                   printk("st%d: Incorrect block size.\n", dev);
1103                   SCpnt->request.rq_status = RQ_INACTIVE;  /* Mark as not busy */
1104                   return (-EIO);
1105                 }
1106               }
1107               else if (SCpnt->sense_buffer[2] & 0x40) {
1108                 STp->eof = ST_EOM_OK;
1109                 if (STp->block_size == 0)
1110                   (STp->buffer)->buffer_bytes = bytes - transfer;
1111                 else
1112                   (STp->buffer)->buffer_bytes =
1113                     bytes - transfer * STp->block_size;
1114 #if DEBUG
1115                 if (debugging)
1116                   printk("st%d: EOM detected (%d bytes read).\n", dev,
1117                          (STp->buffer)->buffer_bytes);
1118 #endif
1119               }
1120               else if (SCpnt->sense_buffer[2] & 0x80) {
1121                 STp->eof = ST_FM;
1122                 if (STp->block_size == 0)
1123                   (STp->buffer)->buffer_bytes = 0;
1124                 else
1125                   (STp->buffer)->buffer_bytes =
1126                     bytes - transfer * STp->block_size;
1127 #if DEBUG
1128                 if (debugging)
1129                   printk(
1130                     "st%d: EOF detected (%d bytes read, transferred %d bytes).\n",
1131                          dev, (STp->buffer)->buffer_bytes, total);
1132 #endif
1133               }
1134             } /* end of EOF, EOM, ILI test */
1135             else { /* nonzero sense key */
1136 #if DEBUG
1137               if (debugging)
1138                 printk("st%d: Tape error while reading.\n", dev);
1139 #endif
1140               SCpnt->request.rq_status = RQ_INACTIVE;
1141               STp->drv_block = (-1);
1142               if (total)
1143                 return total;
1144               else if (STp->moves_after_eof == 1 &&
1145                        (SCpnt->sense_buffer[2] & 0x0f) == BLANK_CHECK) {
1146 #if DEBUG
1147                 if (debugging)
1148                   printk("st%d: Zero returned for first BLANK CHECK after EOF.\n",
1149                          dev);
1150 #endif
1151                 STp->eof = ST_EOD;
1152                 return 0; /* First BLANK_CHECK after EOF */
1153               }
1154               else
1155                 return -EIO;
1156             }
1157           } /* End of extended sense test */
1158           else {
1159             transfer = (STp->buffer)->last_result_fatal;
1160             SCpnt->request.rq_status = RQ_INACTIVE;  /* Mark as not busy */
1161             return transfer;
1162           }
1163         } /* End of error handling */
1164         else /* Read successful */
1165           (STp->buffer)->buffer_bytes = bytes;
1166 
1167         if (STp->drv_block >= 0) {
1168           if (STp->block_size == 0)
1169             STp->drv_block++;
1170           else
1171             STp->drv_block += (STp->buffer)->buffer_bytes / STp->block_size;
1172         }
1173 
1174       } /* if ((STp->buffer)->buffer_bytes == 0 &&
1175            STp->eof == ST_NOEOF) */
1176 
1177       if ((STp->buffer)->buffer_bytes > 0) {
1178 #if DEBUG
1179         if (debugging && STp->eof != ST_NOEOF)
1180           printk("st%d: EOF up. Left %d, needed %d.\n", dev,
1181                  (STp->buffer)->buffer_bytes, count - total);
1182 #endif
1183         transfer = (STp->buffer)->buffer_bytes < count - total ?
1184           (STp->buffer)->buffer_bytes : count - total;
1185         memcpy_tofs(buf, (STp->buffer)->b_data +
1186                     (STp->buffer)->read_pointer,transfer);
1187         filp->f_pos += transfer;
1188         buf += transfer;
1189         total += transfer;
1190         (STp->buffer)->buffer_bytes -= transfer;
1191         (STp->buffer)->read_pointer += transfer;
1192       }
1193       else if (STp->eof != ST_NOEOF) {
1194         STp->eof_hit = 1;
1195         SCpnt->request.rq_status = RQ_INACTIVE;  /* Mark as not busy */
1196         if (total == 0 && STp->eof == ST_FM) {
1197           STp->eof = ST_NOEOF;
1198           STp->drv_block = 0;
1199           if (STp->moves_after_eof > 1)
1200             STp->moves_after_eof = 0;
1201           if ((STp->mt_status)->mt_fileno >= 0)
1202             (STp->mt_status)->mt_fileno++;
1203         }
1204         if (total == 0 && STp->eof == ST_EOM_OK)
1205           return (-EIO);  /* ST_EOM_ERROR not used in read */
1206         return total;
1207       }
1208 
1209       if (STp->block_size == 0)
1210         count = total;  /* Read only one variable length block */
1211 
1212     } /* for (total = 0; total < count; ) */
1213 
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, 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 = cmd_in & IOCCMD_MASK;
1763    if (cmd == (MTIOCTOP & IOCCMD_MASK)) {
1764 
1765      if (((cmd_in & IOCSIZE_MASK) >> IOCSIZE_SHIFT) != 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 == (MTIOCGET & IOCCMD_MASK)) {
1820 
1821      if (((cmd_in & IOCSIZE_MASK) >> IOCSIZE_SHIFT) != 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 == (MTIOCPOS & IOCCMD_MASK)) {
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 (((cmd_in & IOCSIZE_MASK) >> IOCSIZE_SHIFT) != 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 char kernel_version[] = UTS_RELEASE;
2242 
2243 int init_module(void) {
     /* [previous][next][first][last][top][bottom][index][help] */
2244   st_template.usage_count = &mod_use_count_;
2245   return scsi_register_module(MODULE_SCSI_DEV, &st_template);
2246 }
2247 
2248 void cleanup_module( void) 
     /* [previous][next][first][last][top][bottom][index][help] */
2249 {
2250   int i;
2251 
2252   if (MOD_IN_USE) {
2253     printk(KERN_INFO __FILE__ ": module is in use, remove rejected\n");
2254     return;
2255   }
2256   scsi_unregister_module(MODULE_SCSI_DEV, &st_template);
2257   unregister_chrdev(SCSI_TAPE_MAJOR, "st");
2258   st_registered--;
2259   if(scsi_tapes != NULL) {
2260     scsi_init_free((char *) scsi_tapes,
2261                    st_template.dev_max * sizeof(Scsi_Tape));
2262 
2263     if (st_buffers != NULL) {
2264       for (i=0; i < st_nbr_buffers; i++)
2265         if (st_buffers[i] != NULL) {
2266           scsi_init_free((char *) st_buffers[i]->b_data,
2267                          st_buffers[i]->buffer_size);
2268           scsi_init_free((char *) st_buffers[i], sizeof(ST_buffer));
2269         }
2270 
2271       scsi_init_free((char *) st_buffers,
2272                      st_template.dev_max * sizeof(ST_buffer *));
2273     }
2274   }
2275   st_template.dev_max = 0;
2276 }
2277 #endif /* MODULE */

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