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

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