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

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