OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rc203soc/] [sw/] [uClinux/] [drivers/] [scsi/] [st.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1626 jcastillo
/*
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, Wolfgang Denk, Steve Hirsch, Andreas Koppenh"ofer,
9
  Eyal Lebedinsky, J"org Weule, and Eric Youngdale.
10
 
11
  Copyright 1992 - 1996 Kai Makisara
12
                 email Kai.Makisara@metla.fi
13
 
14
  Last modified: Tue Oct  1 22:53:51 1996 by makisara@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
 
45
#include "scsi.h"
46
#include "hosts.h"
47
#include <scsi/scsi_ioctl.h>
48
#include "st.h"
49
#include "constants.h"
50
 
51
/* The default definitions have been moved to st_options.h */
52
 
53
#define ST_BLOCK_SIZE 1024
54
 
55
#include "st_options.h"
56
 
57
#define ST_BUFFER_SIZE (ST_BUFFER_BLOCKS * ST_BLOCK_SIZE)
58
#define ST_WRITE_THRESHOLD (ST_WRITE_THRESHOLD_BLOCKS * ST_BLOCK_SIZE)
59
 
60
/* The buffer size should fit into the 24 bits for length in the
61
   6-byte SCSI read and write commands. */
62
#if ST_BUFFER_SIZE >= (2 << 24 - 1)
63
#error "Buffer size should not exceed (2 << 24 - 1) bytes!"
64
#endif
65
 
66
#if DEBUG
67
static int debugging = 1;
68
#endif
69
 
70
#define MAX_RETRIES 0
71
#define MAX_WRITE_RETRIES 0
72
#define MAX_READY_RETRIES 5
73
#define NO_TAPE  NOT_READY
74
 
75
#define ST_TIMEOUT (900 * HZ)
76
#define ST_LONG_TIMEOUT (14000 * HZ)
77
 
78
#define TAPE_NR(x) (MINOR(x) & ~(128 | ST_MODE_MASK))
79
#define TAPE_MODE(x) ((MINOR(x) & ST_MODE_MASK) >> ST_MODE_SHIFT)
80
 
81
/* Internal ioctl to set both density (uppermost 8 bits) and blocksize (lower
82
   24 bits) */
83
#define SET_DENS_AND_BLK 0x10001
84
 
85
static int st_nbr_buffers;
86
static ST_buffer **st_buffers;
87
static int st_buffer_size = ST_BUFFER_SIZE;
88
static int st_write_threshold = ST_WRITE_THRESHOLD;
89
static int st_max_buffers = ST_MAX_BUFFERS;
90
 
91
Scsi_Tape * scsi_tapes = NULL;
92
 
93
static int modes_defined = FALSE;
94
 
95
static ST_buffer *new_tape_buffer(int, int);
96
static int enlarge_buffer(ST_buffer *, int, int);
97
static void normalize_buffer(ST_buffer *);
98
 
99
static int st_init(void);
100
static int st_attach(Scsi_Device *);
101
static int st_detect(Scsi_Device *);
102
static void st_detach(Scsi_Device *);
103
 
104
struct Scsi_Device_Template st_template = {NULL, "tape", "st", NULL, TYPE_TAPE,
105
                                             SCSI_TAPE_MAJOR, 0, 0, 0, 0,
106
                                             st_detect, st_init,
107
                                             NULL, st_attach, st_detach};
108
 
109
static int st_compression(Scsi_Tape *, int);
110
 
111
static int find_partition(struct inode *);
112
static int update_partition(struct inode *);
113
 
114
static int st_int_ioctl(struct inode * inode, unsigned int cmd_in,
115
                        unsigned long arg);
116
 
117
 
118
 
119
 
120
/* Convert the result to success code */
121
        static int
122
st_chk_result(Scsi_Cmnd * SCpnt)
123
{
124
  int dev = TAPE_NR(SCpnt->request.rq_dev);
125
  int result = SCpnt->result;
126
  unsigned char * sense = SCpnt->sense_buffer, scode;
127
#if DEBUG
128
  const char *stp;
129
#endif
130
 
131
  if (!result /* && SCpnt->sense_buffer[0] == 0 */ )
132
    return 0;
133
#if DEBUG
134
  if (debugging) {
135
    printk(ST_DEB_MSG "st%d: Error: %x, cmd: %x %x %x %x %x %x Len: %d\n",
136
           dev, result,
137
           SCpnt->data_cmnd[0], SCpnt->data_cmnd[1], SCpnt->data_cmnd[2],
138
           SCpnt->data_cmnd[3], SCpnt->data_cmnd[4], SCpnt->data_cmnd[5],
139
           SCpnt->request_bufflen);
140
    if (driver_byte(result) & DRIVER_SENSE)
141
      print_sense("st", SCpnt);
142
    else
143
      printk("\n");
144
  }
145
#endif
146
  scode = sense[2] & 0x0f;
147
 
148
#if !DEBUG
149
  if (!(driver_byte(result) & DRIVER_SENSE) ||
150
      ((sense[0] & 0x70) == 0x70 &&
151
       scode != NO_SENSE &&
152
       scode != RECOVERED_ERROR &&
153
/*       scode != UNIT_ATTENTION && */
154
       scode != BLANK_CHECK &&
155
       scode != VOLUME_OVERFLOW &&
156
       SCpnt->data_cmnd[0] != MODE_SENSE &&
157
       SCpnt->data_cmnd[0] != TEST_UNIT_READY)) { /* Abnormal conditions for tape */
158
    if (driver_byte(result) & DRIVER_SENSE) {
159
      printk(KERN_WARNING "st%d: Error with sense data: ", dev);
160
      print_sense("st", SCpnt);
161
    }
162
    else
163
      printk(KERN_WARNING "st%d: Error %x.\n", dev, result);
164
  }
165
#endif
166
 
167
  if ((sense[0] & 0x70) == 0x70 &&
168
      scode == RECOVERED_ERROR
169
#if ST_RECOVERED_WRITE_FATAL
170
      && SCpnt->data_cmnd[0] != WRITE_6
171
      && SCpnt->data_cmnd[0] != WRITE_FILEMARKS
172
#endif
173
      ) {
174
    scsi_tapes[dev].recover_count++;
175
    scsi_tapes[dev].mt_status->mt_erreg += (1 << MT_ST_SOFTERR_SHIFT);
176
#if DEBUG
177
    if (debugging) {
178
      if (SCpnt->data_cmnd[0] == READ_6)
179
        stp = "read";
180
      else if (SCpnt->data_cmnd[0] == WRITE_6)
181
        stp = "write";
182
      else
183
        stp = "ioctl";
184
      printk(ST_DEB_MSG "st%d: Recovered %s error (%d).\n", dev, stp,
185
             scsi_tapes[dev].recover_count);
186
    }
187
#endif
188
    if ((sense[2] & 0xe0) == 0)
189
      return 0;
190
  }
191
  return (-EIO);
192
}
193
 
194
 
195
/* Wakeup from interrupt */
196
        static void
197
st_sleep_done (Scsi_Cmnd * SCpnt)
198
{
199
  unsigned int st_nbr;
200
  int remainder;
201
  Scsi_Tape * STp;
202
 
203
  if ((st_nbr = TAPE_NR(SCpnt->request.rq_dev)) < st_template.nr_dev) {
204
    STp = &(scsi_tapes[st_nbr]);
205
    if ((STp->buffer)->writing &&
206
        (SCpnt->sense_buffer[0] & 0x70) == 0x70 &&
207
        (SCpnt->sense_buffer[2] & 0x40)) {
208
      /* EOM at write-behind, has all been written? */
209
      if ((SCpnt->sense_buffer[0] & 0x80) != 0)
210
        remainder = (SCpnt->sense_buffer[3] << 24) |
211
              (SCpnt->sense_buffer[4] << 16) |
212
                (SCpnt->sense_buffer[5] << 8) | SCpnt->sense_buffer[6];
213
      else
214
        remainder = 0;
215
      if ((SCpnt->sense_buffer[2] & 0x0f) == VOLUME_OVERFLOW ||
216
          remainder > 0)
217
        (STp->buffer)->last_result = SCpnt->result; /* Error */
218
      else
219
        (STp->buffer)->last_result = INT_MAX; /* OK */
220
    }
221
    else
222
      (STp->buffer)->last_result = SCpnt->result;
223
    if ((STp->buffer)->writing) {
224
      /* Process errors before releasing request */
225
      (STp->buffer)->last_result_fatal = st_chk_result(SCpnt);
226
      SCpnt->request.rq_status = RQ_INACTIVE;
227
    }
228
    else
229
      SCpnt->request.rq_status = RQ_SCSI_DONE;
230
 
231
#if DEBUG
232
    STp->write_pending = 0;
233
#endif
234
    up(SCpnt->request.sem);
235
  }
236
#if DEBUG
237
  else if (debugging)
238
    printk(KERN_ERR "st?: Illegal interrupt device %x\n", st_nbr);
239
#endif
240
}
241
 
242
 
243
/* Do the scsi command */
244
        static Scsi_Cmnd *
245
st_do_scsi(Scsi_Cmnd *SCpnt, Scsi_Tape *STp, unsigned char *cmd, int bytes,
246
           int timeout, int retries)
247
{
248
  if (SCpnt == NULL)
249
    if ((SCpnt = allocate_device(NULL, STp->device, 1)) == NULL) {
250
      printk(KERN_ERR "st%d: Can't get SCSI request.\n", TAPE_NR(STp->devt));
251
      return NULL;
252
    }
253
 
254
  cmd[1] |= (SCpnt->lun << 5) & 0xe0;
255
  STp->sem = MUTEX_LOCKED;
256
  SCpnt->request.sem = &(STp->sem);
257
  SCpnt->request.rq_status = RQ_SCSI_BUSY;
258
  SCpnt->request.rq_dev = STp->devt;
259
 
260
  scsi_do_cmd(SCpnt, (void *)cmd, (STp->buffer)->b_data, bytes,
261
              st_sleep_done, timeout, retries);
262
 
263
  down(SCpnt->request.sem);
264
 
265
  (STp->buffer)->last_result_fatal = st_chk_result(SCpnt);
266
 
267
  return SCpnt;
268
}
269
 
270
 
271
/* Handle the write-behind checking */
272
        static void
273
write_behind_check(Scsi_Tape *STp)
274
{
275
  ST_buffer * STbuffer;
276
 
277
  STbuffer = STp->buffer;
278
 
279
#if DEBUG
280
  if (STp->write_pending)
281
    STp->nbr_waits++;
282
  else
283
    STp->nbr_finished++;
284
#endif
285
 
286
  down(&(STp->sem));
287
 
288
  if (STbuffer->writing < STbuffer->buffer_bytes)
289
    memcpy(STbuffer->b_data,
290
           STbuffer->b_data + STbuffer->writing,
291
           STbuffer->buffer_bytes - STbuffer->writing);
292
  STbuffer->buffer_bytes -= STbuffer->writing;
293
  if (STp->drv_block >= 0) {
294
    if (STp->block_size == 0)
295
      STp->drv_block++;
296
    else
297
      STp->drv_block += STbuffer->writing / STp->block_size;
298
  }
299
  STbuffer->writing = 0;
300
 
301
  return;
302
}
303
 
304
 
305
/* Back over EOF if it has been inadvertently crossed (ioctl not used because
306
   it messes up the block number). */
307
        static int
308
back_over_eof(Scsi_Tape *STp)
309
{
310
  Scsi_Cmnd *SCpnt;
311
  unsigned char cmd[10];
312
 
313
  cmd[0] = SPACE;
314
  cmd[1] = 0x01; /* Space FileMarks */
315
  cmd[2] = cmd[3] = cmd[4] = 0xff;  /* -1 filemarks */
316
  cmd[5] = 0;
317
 
318
  SCpnt = st_do_scsi(NULL, STp, cmd, 0, ST_TIMEOUT, MAX_RETRIES);
319
  if (!SCpnt)
320
    return (-EBUSY);
321
 
322
  SCpnt->request.rq_status = RQ_INACTIVE;
323
  if ((STp->buffer)->last_result != 0) {
324
    printk(KERN_ERR "st%d: Backing over filemark failed.\n", TAPE_NR(STp->devt));
325
    if ((STp->mt_status)->mt_fileno >= 0)
326
      (STp->mt_status)->mt_fileno += 1;
327
    (STp->mt_status)->mt_blkno = 0;
328
  }
329
 
330
  return (STp->buffer)->last_result_fatal;
331
}
332
 
333
 
334
/* Flush the write buffer (never need to write if variable blocksize). */
335
        static int
336
flush_write_buffer(Scsi_Tape *STp)
337
{
338
  int offset, transfer, blks;
339
  int result;
340
  unsigned char cmd[10];
341
  Scsi_Cmnd *SCpnt;
342
 
343
  if ((STp->buffer)->writing) {
344
    write_behind_check(STp);
345
    if ((STp->buffer)->last_result_fatal) {
346
#if DEBUG
347
      if (debugging)
348
        printk(ST_DEB_MSG "st%d: Async write error (flush) %x.\n",
349
               TAPE_NR(STp->devt), (STp->buffer)->last_result);
350
#endif
351
      if ((STp->buffer)->last_result == INT_MAX)
352
        return (-ENOSPC);
353
      return (-EIO);
354
    }
355
  }
356
 
357
  if (STp->block_size == 0)
358
    return 0;
359
 
360
  result = 0;
361
  if (STp->dirty == 1) {
362
 
363
    offset = (STp->buffer)->buffer_bytes;
364
    transfer = ((offset + STp->block_size - 1) /
365
                STp->block_size) * STp->block_size;
366
#if DEBUG
367
    if (debugging)
368
      printk(ST_DEB_MSG "st%d: Flushing %d bytes.\n", TAPE_NR(STp->devt), transfer);
369
#endif
370
    memset((STp->buffer)->b_data + offset, 0, transfer - offset);
371
 
372
    memset(cmd, 0, 10);
373
    cmd[0] = WRITE_6;
374
    cmd[1] = 1;
375
    blks = transfer / STp->block_size;
376
    cmd[2] = blks >> 16;
377
    cmd[3] = blks >> 8;
378
    cmd[4] = blks;
379
 
380
    SCpnt = st_do_scsi(NULL, STp, cmd, transfer, ST_TIMEOUT, MAX_WRITE_RETRIES);
381
    if (!SCpnt)
382
      return (-EBUSY);
383
 
384
    if ((STp->buffer)->last_result_fatal != 0) {
385
      if ((SCpnt->sense_buffer[0] & 0x70) == 0x70 &&
386
          (SCpnt->sense_buffer[2] & 0x40) &&
387
          (SCpnt->sense_buffer[2] & 0x0f) == NO_SENSE) {
388
        STp->dirty = 0;
389
        (STp->buffer)->buffer_bytes = 0;
390
        result = (-ENOSPC);
391
      }
392
      else {
393
        printk(KERN_ERR "st%d: Error on flush.\n", TAPE_NR(STp->devt));
394
        result = (-EIO);
395
      }
396
      STp->drv_block = (-1);
397
    }
398
    else {
399
      if (STp->drv_block >= 0)
400
        STp->drv_block += blks;
401
      STp->dirty = 0;
402
      (STp->buffer)->buffer_bytes = 0;
403
    }
404
    SCpnt->request.rq_status = RQ_INACTIVE;  /* Mark as not busy */
405
  }
406
  return result;
407
}
408
 
409
 
410
/* Flush the tape buffer. The tape will be positioned correctly unless
411
   seek_next is true. */
412
        static int
413
flush_buffer(struct inode * inode, struct file * filp, int seek_next)
414
{
415
  int backspace, result;
416
  Scsi_Tape * STp;
417
  ST_buffer * STbuffer;
418
  int dev = TAPE_NR(inode->i_rdev);
419
 
420
  STp = &(scsi_tapes[dev]);
421
  STbuffer = STp->buffer;
422
 
423
  /*
424
   * If there was a bus reset, block further access
425
   * to this device.
426
   */
427
  if( STp->device->was_reset )
428
    return (-EIO);
429
 
430
  if (STp->ready != ST_READY)
431
    return 0;
432
 
433
  if (STp->ps[STp->partition].rw == ST_WRITING)  /* Writing */
434
    return flush_write_buffer(STp);
435
 
436
  if (STp->block_size == 0) {
437
    STp->eof = ST_NOEOF;
438
    STp->eof_hit = 0;
439
    return 0;
440
  }
441
 
442
  backspace = ((STp->buffer)->buffer_bytes +
443
    (STp->buffer)->read_pointer) / STp->block_size -
444
      ((STp->buffer)->read_pointer + STp->block_size - 1) /
445
        STp->block_size;
446
  (STp->buffer)->buffer_bytes = 0;
447
  (STp->buffer)->read_pointer = 0;
448
  result = 0;
449
  if (!seek_next) {
450
    if ((STp->eof == ST_FM) && !STp->eof_hit) {
451
      result = back_over_eof(STp); /* Back over the EOF hit */
452
      if (!result) {
453
        STp->eof = ST_NOEOF;
454
        STp->eof_hit = 0;
455
      }
456
    }
457
    if (!result && backspace > 0)
458
      result = st_int_ioctl(inode, MTBSR, backspace);
459
  }
460
  else if ((STp->eof == ST_FM) && !STp->eof_hit) {
461
    if ((STp->mt_status)->mt_fileno >= 0)
462
      (STp->mt_status)->mt_fileno++;
463
    STp->drv_block = 0;
464
  }
465
 
466
  return result;
467
 
468
}
469
 
470
/* Set the mode parameters */
471
        static int
472
set_mode_densblk(struct inode * inode, Scsi_Tape *STp, ST_mode *STm)
473
{
474
    int set_it = FALSE;
475
    unsigned long arg;
476
    int dev = TAPE_NR(inode->i_rdev);
477
 
478
    if (!STp->density_changed &&
479
        STm->default_density >= 0 &&
480
        STm->default_density != STp->density) {
481
      arg = STm->default_density;
482
      set_it = TRUE;
483
    }
484
    else
485
      arg = STp->density;
486
    arg <<= MT_ST_DENSITY_SHIFT;
487
    if (!STp->blksize_changed &&
488
        STm->default_blksize >= 0 &&
489
        STm->default_blksize != STp->block_size) {
490
      arg |= STm->default_blksize;
491
      set_it = TRUE;
492
    }
493
    else
494
      arg |= STp->block_size;
495
    if (set_it &&
496
        st_int_ioctl(inode, SET_DENS_AND_BLK, arg)) {
497
      printk(KERN_WARNING
498
             "st%d: Can't set default block size to %d bytes and density %x.\n",
499
             dev, STm->default_blksize, STm->default_density);
500
      if (modes_defined)
501
        return (-EINVAL);
502
    }
503
    return 0;
504
}
505
 
506
 
507
/* Open the device */
508
        static int
509
scsi_tape_open(struct inode * inode, struct file * filp)
510
{
511
    unsigned short flags;
512
    int i, need_dma_buffer, new_session = FALSE;
513
    unsigned char cmd[10];
514
    Scsi_Cmnd * SCpnt;
515
    Scsi_Tape * STp;
516
    ST_mode * STm;
517
    int dev = TAPE_NR(inode->i_rdev);
518
    int mode = TAPE_MODE(inode->i_rdev);
519
 
520
    if (dev >= st_template.dev_max || !scsi_tapes[dev].device)
521
      return (-ENXIO);
522
    STp = &(scsi_tapes[dev]);
523
    if (STp->in_use) {
524
#if DEBUG
525
      printk(ST_DEB_MSG "st%d: Device already in use.\n", dev);
526
#endif
527
      return (-EBUSY);
528
    }
529
    STp->rew_at_close = (MINOR(inode->i_rdev) & 0x80) == 0;
530
 
531
    if (mode != STp->current_mode) {
532
#if DEBUG
533
      if (debugging)
534
        printk(ST_DEB_MSG "st%d: Mode change from %d to %d.\n",
535
               dev, STp->current_mode, mode);
536
#endif
537
      new_session = TRUE;
538
      STp->current_mode = mode;
539
    }
540
    STm = &(STp->modes[STp->current_mode]);
541
 
542
    /* Allocate buffer for this user */
543
    need_dma_buffer = STp->restr_dma;
544
    for (i=0; i < st_nbr_buffers; i++)
545
      if (!st_buffers[i]->in_use &&
546
          (!need_dma_buffer || st_buffers[i]->dma))
547
        break;
548
    if (i >= st_nbr_buffers) {
549
      STp->buffer = new_tape_buffer(FALSE, need_dma_buffer);
550
      if (STp->buffer == NULL) {
551
        printk(KERN_WARNING "st%d: Can't allocate tape buffer.\n", dev);
552
        return (-EBUSY);
553
      }
554
    }
555
    else
556
      STp->buffer = st_buffers[i];
557
    (STp->buffer)->in_use = 1;
558
    (STp->buffer)->writing = 0;
559
    (STp->buffer)->last_result_fatal = 0;
560
 
561
    flags = filp->f_flags;
562
    STp->write_prot = ((flags & O_ACCMODE) == O_RDONLY);
563
 
564
    STp->dirty = 0;
565
    for (i=0; i < ST_NBR_PARTITIONS; i++)
566
      STp->ps[i].rw = ST_IDLE;
567
    STp->ready = ST_READY;
568
    if (STp->eof != ST_EOD)  /* Save EOD across opens */
569
      STp->eof = ST_NOEOF;
570
    STp->eof_hit = 0;
571
    STp->recover_count = 0;
572
#if DEBUG
573
    STp->nbr_waits = STp->nbr_finished = 0;
574
#endif
575
 
576
    if (scsi_tapes[dev].device->host->hostt->usage_count)
577
        (*scsi_tapes[dev].device->host->hostt->usage_count)++;
578
    if(st_template.usage_count) (*st_template.usage_count)++;
579
 
580
    memset ((void *) &cmd[0], 0, 10);
581
    cmd[0] = TEST_UNIT_READY;
582
 
583
    SCpnt = st_do_scsi(NULL, STp, cmd, 0, ST_LONG_TIMEOUT, MAX_READY_RETRIES);
584
    if (!SCpnt) {
585
      if (scsi_tapes[dev].device->host->hostt->usage_count)
586
          (*scsi_tapes[dev].device->host->hostt->usage_count)--;
587
      if(st_template.usage_count) (*st_template.usage_count)--;
588
      return (-EBUSY);
589
    }
590
 
591
    if ((SCpnt->sense_buffer[0] & 0x70) == 0x70 &&
592
        (SCpnt->sense_buffer[2] & 0x0f) == UNIT_ATTENTION) { /* New media? */
593
      (STp->mt_status)->mt_fileno = 0 ;
594
      memset ((void *) &cmd[0], 0, 10);
595
      cmd[0] = TEST_UNIT_READY;
596
 
597
      SCpnt = st_do_scsi(SCpnt, STp, cmd, 0, ST_LONG_TIMEOUT, MAX_READY_RETRIES);
598
 
599
      (STp->mt_status)->mt_fileno = STp->drv_block = 0;
600
      STp->eof = ST_NOEOF;
601
      (STp->device)->was_reset = 0;
602
      STp->partition = STp->new_partition = 0;
603
      if (STp->can_partitions)
604
        STp->nbr_partitions = 1;  /* This guess will be updated later if necessary */
605
      for (i=0; i < ST_NBR_PARTITIONS; i++) {
606
        STp->ps[i].rw = ST_IDLE;
607
        STp->ps[i].moves_after_eof = 1;
608
        STp->ps[i].at_sm = 0;
609
        STp->ps[i].last_block_valid = FALSE;
610
      }
611
      new_session = TRUE;
612
    }
613
 
614
    if ((STp->buffer)->last_result_fatal != 0) {
615
      if ((SCpnt->sense_buffer[0] & 0x70) == 0x70 &&
616
          (SCpnt->sense_buffer[2] & 0x0f) == NO_TAPE) {
617
        (STp->mt_status)->mt_fileno = STp->drv_block = 0 ;
618
        STp->ready = ST_NO_TAPE;
619
      } else {
620
        (STp->mt_status)->mt_fileno = STp->drv_block = (-1);
621
        STp->ready = ST_NOT_READY;
622
      }
623
      SCpnt->request.rq_status = RQ_INACTIVE;  /* Mark as not busy */
624
      STp->density = 0;          /* Clear the erroneous "residue" */
625
      STp->write_prot = 0;
626
      STp->block_size = 0;
627
      STp->eof = ST_NOEOF;
628
      (STp->mt_status)->mt_fileno = STp->drv_block = 0;
629
      STp->partition = STp->new_partition = 0;
630
      STp->door_locked = ST_UNLOCKED;
631
      STp->in_use = 1;
632
      return 0;
633
    }
634
 
635
    if (STp->omit_blklims)
636
      STp->min_block = STp->max_block = (-1);
637
    else {
638
      memset ((void *) &cmd[0], 0, 10);
639
      cmd[0] = READ_BLOCK_LIMITS;
640
 
641
      SCpnt = st_do_scsi(SCpnt, STp, cmd, 6, ST_TIMEOUT, MAX_READY_RETRIES);
642
 
643
      if (!SCpnt->result && !SCpnt->sense_buffer[0]) {
644
        STp->max_block = ((STp->buffer)->b_data[1] << 16) |
645
          ((STp->buffer)->b_data[2] << 8) | (STp->buffer)->b_data[3];
646
        STp->min_block = ((STp->buffer)->b_data[4] << 8) |
647
          (STp->buffer)->b_data[5];
648
#if DEBUG
649
        if (debugging)
650
          printk(ST_DEB_MSG "st%d: Block limits %d - %d bytes.\n", dev, STp->min_block,
651
                 STp->max_block);
652
#endif
653
      }
654
      else {
655
        STp->min_block = STp->max_block = (-1);
656
#if DEBUG
657
        if (debugging)
658
          printk(ST_DEB_MSG "st%d: Can't read block limits.\n", dev);
659
#endif
660
      }
661
    }
662
 
663
    memset ((void *) &cmd[0], 0, 10);
664
    cmd[0] = MODE_SENSE;
665
    cmd[4] = 12;
666
 
667
    SCpnt = st_do_scsi(SCpnt, STp, cmd, 12, ST_TIMEOUT, MAX_READY_RETRIES);
668
 
669
    if ((STp->buffer)->last_result_fatal != 0) {
670
#if DEBUG
671
      if (debugging)
672
        printk(ST_DEB_MSG "st%d: No Mode Sense.\n", dev);
673
#endif
674
      STp->block_size = ST_DEFAULT_BLOCK;  /* Educated guess (?) */
675
      (STp->buffer)->last_result_fatal = 0;  /* Prevent error propagation */
676
      STp->drv_write_prot = 0;
677
    }
678
    else {
679
 
680
#if DEBUG
681
      if (debugging)
682
        printk(ST_DEB_MSG "st%d: Mode sense. Length %d, medium %x, WBS %x, BLL %d\n",
683
               dev,
684
               (STp->buffer)->b_data[0], (STp->buffer)->b_data[1],
685
               (STp->buffer)->b_data[2], (STp->buffer)->b_data[3]);
686
#endif
687
 
688
      if ((STp->buffer)->b_data[3] >= 8) {
689
        STp->drv_buffer = ((STp->buffer)->b_data[2] >> 4) & 7;
690
        STp->density = (STp->buffer)->b_data[4];
691
        STp->block_size = (STp->buffer)->b_data[9] * 65536 +
692
          (STp->buffer)->b_data[10] * 256 + (STp->buffer)->b_data[11];
693
#if DEBUG
694
        if (debugging)
695
          printk(ST_DEB_MSG "st%d: Density %x, tape length: %x, drv buffer: %d\n",
696
                 dev, STp->density, (STp->buffer)->b_data[5] * 65536 +
697
                 (STp->buffer)->b_data[6] * 256 + (STp->buffer)->b_data[7],
698
                 STp->drv_buffer);
699
#endif
700
      }
701
 
702
      if (STp->block_size > (STp->buffer)->buffer_size &&
703
          !enlarge_buffer(STp->buffer, STp->block_size, STp->restr_dma)) {
704
        printk(KERN_NOTICE "st%d: Blocksize %d too large for buffer.\n", dev,
705
               STp->block_size);
706
        (STp->buffer)->in_use = 0;
707
        STp->buffer = NULL;
708
        if (scsi_tapes[dev].device->host->hostt->usage_count)
709
            (*scsi_tapes[dev].device->host->hostt->usage_count)--;
710
        if(st_template.usage_count) (*st_template.usage_count)--;
711
        return (-EIO);
712
      }
713
      STp->drv_write_prot = ((STp->buffer)->b_data[2] & 0x80) != 0;
714
    }
715
    SCpnt->request.rq_status = RQ_INACTIVE;  /* Mark as not busy */
716
 
717
    if (STp->block_size > 0)
718
      (STp->buffer)->buffer_blocks = st_buffer_size / STp->block_size;
719
    else
720
      (STp->buffer)->buffer_blocks = 1;
721
    (STp->buffer)->buffer_bytes = (STp->buffer)->read_pointer = 0;
722
 
723
#if DEBUG
724
    if (debugging)
725
      printk(ST_DEB_MSG "st%d: Block size: %d, buffer size: %d (%d blocks).\n", dev,
726
             STp->block_size, (STp->buffer)->buffer_size,
727
             (STp->buffer)->buffer_blocks);
728
#endif
729
 
730
    if (STp->drv_write_prot) {
731
      STp->write_prot = 1;
732
#if DEBUG
733
      if (debugging)
734
        printk(ST_DEB_MSG "st%d: Write protected\n", dev);
735
#endif
736
      if ((flags & O_ACCMODE) == O_WRONLY || (flags & O_ACCMODE) == O_RDWR) {
737
        (STp->buffer)->in_use = 0;
738
        STp->buffer = NULL;
739
        if (scsi_tapes[dev].device->host->hostt->usage_count)
740
            (*scsi_tapes[dev].device->host->hostt->usage_count)--;
741
        if(st_template.usage_count) (*st_template.usage_count)--;
742
        return (-EROFS);
743
      }
744
    }
745
 
746
    if (STp->can_partitions && STp->nbr_partitions < 1) {
747
      /* This code is reached when the device is opened for the first time
748
         after the driver has been initialized with tape in the drive and the
749
         partition support has been enabled. */
750
#if DEBUG
751
      if (debugging)
752
        printk(ST_DEB_MSG "st%d: Updating partition number in status.\n", dev);
753
#endif
754
      if ((STp->partition = find_partition(inode)) < 0) {
755
        (STp->buffer)->in_use = 0;
756
        STp->buffer = NULL;
757
        if (scsi_tapes[dev].device->host->hostt->usage_count)
758
            (*scsi_tapes[dev].device->host->hostt->usage_count)--;
759
        if(st_template.usage_count) (*st_template.usage_count)--;
760
        return STp->partition;
761
      }
762
      STp->new_partition = STp->partition;
763
      STp->nbr_partitions = 1;  /* This guess will be updated when necessary */
764
    }
765
 
766
    if (new_session) {  /* Change the drive parameters for the new mode */
767
      STp->density_changed = STp->blksize_changed = FALSE;
768
      STp->compression_changed = FALSE;
769
      if (!(STm->defaults_for_writes) &&
770
          (i = set_mode_densblk(inode, STp, STm)) < 0) {
771
        (STp->buffer)->in_use = 0;
772
        STp->buffer = NULL;
773
        if (scsi_tapes[dev].device->host->hostt->usage_count)
774
            (*scsi_tapes[dev].device->host->hostt->usage_count)--;
775
        if(st_template.usage_count) (*st_template.usage_count)--;
776
        return i;
777
      }
778
      if (STp->default_drvbuffer != 0xff) {
779
        if (st_int_ioctl(inode, MTSETDRVBUFFER, STp->default_drvbuffer))
780
          printk(KERN_WARNING "st%d: Can't set default drive buffering to %d.\n",
781
                 dev, STp->default_drvbuffer);
782
      }
783
    }
784
 
785
    STp->in_use = 1;
786
 
787
    return 0;
788
}
789
 
790
 
791
/* Close the device*/
792
        static void
793
scsi_tape_close(struct inode * inode, struct file * filp)
794
{
795
    int result;
796
    static unsigned char cmd[10];
797
    Scsi_Cmnd * SCpnt;
798
    Scsi_Tape * STp;
799
    kdev_t devt = inode->i_rdev;
800
    int dev;
801
 
802
    dev = TAPE_NR(devt);
803
    STp = &(scsi_tapes[dev]);
804
 
805
    if (STp->can_partitions &&
806
        update_partition(inode) < 0) {
807
#if DEBUG
808
      if (debugging)
809
        printk(ST_DEB_MSG "st%d: update_partition at close failed.\n", dev);
810
#endif
811
      goto out;
812
    }
813
 
814
    if ( STp->ps[STp->partition].rw == ST_WRITING && !(STp->device)->was_reset) {
815
 
816
      result = flush_write_buffer(STp);
817
 
818
#if DEBUG
819
      if (debugging) {
820
        printk(ST_DEB_MSG "st%d: File length %ld bytes.\n",
821
               dev, (long)(filp->f_pos));
822
        printk(ST_DEB_MSG "st%d: Async write waits %d, finished %d.\n",
823
               dev, STp->nbr_waits, STp->nbr_finished);
824
      }
825
#endif
826
 
827
      if (result == 0 || result == (-ENOSPC)) {
828
 
829
        memset(cmd, 0, 10);
830
        cmd[0] = WRITE_FILEMARKS;
831
        cmd[4] = 1 + STp->two_fm;
832
 
833
        SCpnt = st_do_scsi(NULL, STp, cmd, 0, ST_TIMEOUT, MAX_WRITE_RETRIES);
834
        if (!SCpnt)
835
          goto out;
836
 
837
        SCpnt->request.rq_status = RQ_INACTIVE;  /* Mark as not busy */
838
 
839
        if ((STp->buffer)->last_result_fatal != 0 &&
840
            ((SCpnt->sense_buffer[0] & 0x70) != 0x70 ||
841
             (SCpnt->sense_buffer[2] & 0x4f) != 0x40 ||
842
             ((SCpnt->sense_buffer[0] & 0x80) != 0 &&
843
              (SCpnt->sense_buffer[3] | SCpnt->sense_buffer[4] |
844
               SCpnt->sense_buffer[5] |
845
               SCpnt->sense_buffer[6]) == 0)))  /* Filter out successful write at EOM */
846
          printk(KERN_ERR "st%d: Error on write filemark.\n", dev);
847
        else {
848
          if ((STp->mt_status)->mt_fileno >= 0)
849
              (STp->mt_status)->mt_fileno++ ;
850
          STp->drv_block = 0;
851
          if (STp->two_fm)
852
            back_over_eof(STp);
853
        }
854
      }
855
 
856
#if DEBUG
857
      if (debugging)
858
        printk(ST_DEB_MSG "st%d: Buffer flushed, %d EOF(s) written\n",
859
               dev, cmd[4]);
860
#endif
861
    }
862
    else if (!STp->rew_at_close) {
863
      if (STp->can_bsr)
864
        flush_buffer(inode, filp, 0);
865
      else if ((STp->eof == ST_FM) && !STp->eof_hit)
866
        back_over_eof(STp);
867
    }
868
 
869
out:
870
    if (STp->rew_at_close)
871
      st_int_ioctl(inode, MTREW, 1);
872
 
873
    if (STp->door_locked == ST_LOCKED_AUTO)
874
      st_int_ioctl(inode, MTUNLOCK, 0);
875
 
876
    if (STp->buffer != NULL) {
877
      normalize_buffer(STp->buffer);
878
      (STp->buffer)->in_use = 0;
879
    }
880
 
881
    STp->in_use = 0;
882
    if (scsi_tapes[dev].device->host->hostt->usage_count)
883
      (*scsi_tapes[dev].device->host->hostt->usage_count)--;
884
    if(st_template.usage_count) (*st_template.usage_count)--;
885
 
886
    return;
887
}
888
 
889
 
890
/* Write command */
891
        static int
892
st_write(struct inode * inode, struct file * filp, const char * buf, int count)
893
{
894
    int total, do_count, blks, retval, transfer;
895
    int write_threshold;
896
    int doing_write = 0;
897
    static unsigned char cmd[10];
898
    const char *b_point;
899
    Scsi_Cmnd * SCpnt = NULL;
900
    Scsi_Tape * STp;
901
    ST_mode * STm;
902
    ST_partstat * STps;
903
    int dev = TAPE_NR(inode->i_rdev);
904
 
905
    STp = &(scsi_tapes[dev]);
906
    if (STp->ready != ST_READY)
907
      return (-EIO);
908
    STm = &(STp->modes[STp->current_mode]);
909
    if (!STm->defined)
910
      return (-ENXIO);
911
 
912
    /*
913
     * If there was a bus reset, block further access
914
     * to this device.
915
     */
916
    if( STp->device->was_reset )
917
      return (-EIO);
918
 
919
#if DEBUG
920
    if (!STp->in_use) {
921
      printk(ST_DEB_MSG "st%d: Incorrect device.\n", dev);
922
      return (-EIO);
923
    }
924
#endif
925
 
926
    if (STp->can_partitions &&
927
        (retval = update_partition(inode)) < 0)
928
      return retval;
929
    STps = &(STp->ps[STp->partition]);
930
 
931
    if (STp->write_prot)
932
      return (-EACCES);
933
 
934
    if (STp->block_size == 0 &&
935
       count > (STp->buffer)->buffer_size &&
936
       !enlarge_buffer(STp->buffer, count, STp->restr_dma))
937
      return (-EOVERFLOW);
938
 
939
    if (STp->do_auto_lock && STp->door_locked == ST_UNLOCKED &&
940
       !st_int_ioctl(inode, MTLOCK, 0))
941
      STp->door_locked = ST_LOCKED_AUTO;
942
 
943
    if (STps->rw == ST_READING) {
944
      retval = flush_buffer(inode, filp, 0);
945
      if (retval)
946
        return retval;
947
      STps->rw = ST_WRITING;
948
    }
949
    else if (STps->rw != ST_WRITING &&
950
             (STp->mt_status)->mt_fileno == 0 && STp->drv_block == 0) {
951
      if ((retval = set_mode_densblk(inode, STp, STm)) < 0)
952
        return retval;
953
      if (STm->default_compression != ST_DONT_TOUCH &&
954
          !(STp->compression_changed)) {
955
        if (st_compression(STp, (STm->default_compression == ST_YES))) {
956
          printk(KERN_WARNING "st%d: Can't set default compression.\n",
957
                 dev);
958
          if (modes_defined)
959
            return (-EINVAL);
960
        }
961
      }
962
    }
963
 
964
    if (STps->moves_after_eof < 255)
965
      STps->moves_after_eof++;
966
 
967
    if ((STp->buffer)->writing) {
968
      write_behind_check(STp);
969
      if ((STp->buffer)->last_result_fatal) {
970
#if DEBUG
971
        if (debugging)
972
          printk(ST_DEB_MSG "st%d: Async write error (write) %x.\n", dev,
973
                 (STp->buffer)->last_result);
974
#endif
975
        if ((STp->buffer)->last_result == INT_MAX) {
976
          retval = (-ENOSPC);  /* All has been written */
977
          STp->eof = ST_EOM_OK;
978
        }
979
        else
980
          retval = (-EIO);
981
        return retval;
982
      }
983
    }
984
    if (STp->eof == ST_EOM_OK)
985
      return (-ENOSPC);
986
    else if (STp->eof == ST_EOM_ERROR)
987
      return (-EIO);
988
 
989
    if (!STm->do_buffer_writes) {
990
      if (STp->block_size != 0 && (count % STp->block_size) != 0)
991
        return (-EIO);   /* Write must be integral number of blocks */
992
      write_threshold = 1;
993
    }
994
    else
995
      write_threshold = (STp->buffer)->buffer_blocks * STp->block_size;
996
    if (!STm->do_async_writes)
997
      write_threshold--;
998
 
999
    total = count;
1000
 
1001
    memset(cmd, 0, 10);
1002
    cmd[0] = WRITE_6;
1003
    cmd[1] = (STp->block_size != 0);
1004
 
1005
    STps->rw = ST_WRITING;
1006
 
1007
    b_point = buf;
1008
    while((STp->block_size == 0 && !STm->do_async_writes && count > 0) ||
1009
          (STp->block_size != 0 &&
1010
           (STp->buffer)->buffer_bytes + count > write_threshold))
1011
    {
1012
      doing_write = 1;
1013
      if (STp->block_size == 0)
1014
        do_count = count;
1015
      else {
1016
        do_count = (STp->buffer)->buffer_blocks * STp->block_size -
1017
          (STp->buffer)->buffer_bytes;
1018
        if (do_count > count)
1019
          do_count = count;
1020
      }
1021
      memcpy_fromfs((STp->buffer)->b_data +
1022
                    (STp->buffer)->buffer_bytes, b_point, do_count);
1023
 
1024
      if (STp->block_size == 0)
1025
        blks = transfer = do_count;
1026
      else {
1027
        blks = ((STp->buffer)->buffer_bytes + do_count) /
1028
          STp->block_size;
1029
        transfer = blks * STp->block_size;
1030
      }
1031
      cmd[2] = blks >> 16;
1032
      cmd[3] = blks >> 8;
1033
      cmd[4] = blks;
1034
 
1035
      SCpnt = st_do_scsi(SCpnt, STp, cmd, transfer, ST_TIMEOUT, MAX_WRITE_RETRIES);
1036
      if (!SCpnt)
1037
        return (-EBUSY);
1038
 
1039
      if ((STp->buffer)->last_result_fatal != 0) {
1040
#if DEBUG
1041
        if (debugging)
1042
          printk(ST_DEB_MSG "st%d: Error on write:\n", dev);
1043
#endif
1044
        if ((SCpnt->sense_buffer[0] & 0x70) == 0x70 &&
1045
            (SCpnt->sense_buffer[2] & 0x40)) {
1046
          if (STp->block_size != 0 && (SCpnt->sense_buffer[0] & 0x80) != 0)
1047
            transfer = (SCpnt->sense_buffer[3] << 24) |
1048
              (SCpnt->sense_buffer[4] << 16) |
1049
                (SCpnt->sense_buffer[5] << 8) | SCpnt->sense_buffer[6];
1050
          else if (STp->block_size == 0 &&
1051
                   (SCpnt->sense_buffer[2] & 0x0f) == VOLUME_OVERFLOW)
1052
            transfer = do_count;
1053
          else
1054
            transfer = 0;
1055
          if (STp->block_size != 0)
1056
            transfer *= STp->block_size;
1057
          if (transfer <= do_count) {
1058
            filp->f_pos += do_count - transfer;
1059
            count -= do_count - transfer;
1060
            if (STp->drv_block >= 0) {
1061
              if (STp->block_size == 0 && transfer < do_count)
1062
                STp->drv_block++;
1063
              else if (STp->block_size != 0)
1064
                STp->drv_block += (do_count - transfer) / STp->block_size;
1065
            }
1066
            STp->eof = ST_EOM_OK;
1067
            retval = (-ENOSPC); /* EOM within current request */
1068
#if DEBUG
1069
            if (debugging)
1070
              printk(ST_DEB_MSG "st%d: EOM with %d bytes unwritten.\n",
1071
                     dev, transfer);
1072
#endif
1073
          }
1074
          else {
1075
            STp->eof = ST_EOM_ERROR;
1076
            STp->drv_block = (-1);    /* Too cautious? */
1077
            retval = (-EIO); /* EOM for old data */
1078
#if DEBUG
1079
            if (debugging)
1080
              printk(ST_DEB_MSG "st%d: EOM with lost data.\n", dev);
1081
#endif
1082
          }
1083
        }
1084
        else {
1085
          STp->drv_block = (-1);    /* Too cautious? */
1086
          retval = (-EIO);
1087
        }
1088
 
1089
        SCpnt->request.rq_status = RQ_INACTIVE;  /* Mark as not busy */
1090
        (STp->buffer)->buffer_bytes = 0;
1091
        STp->dirty = 0;
1092
        if (count < total)
1093
          return total - count;
1094
        else
1095
          return retval;
1096
      }
1097
      filp->f_pos += do_count;
1098
      b_point += do_count;
1099
      count -= do_count;
1100
      if (STp->drv_block >= 0) {
1101
        if (STp->block_size == 0)
1102
          STp->drv_block++;
1103
        else
1104
          STp->drv_block += blks;
1105
      }
1106
      (STp->buffer)->buffer_bytes = 0;
1107
      STp->dirty = 0;
1108
    }
1109
    if (count != 0) {
1110
      STp->dirty = 1;
1111
      memcpy_fromfs((STp->buffer)->b_data +
1112
                    (STp->buffer)->buffer_bytes,b_point,count);
1113
      filp->f_pos += count;
1114
      (STp->buffer)->buffer_bytes += count;
1115
      count = 0;
1116
    }
1117
 
1118
    if (doing_write && (STp->buffer)->last_result_fatal != 0) {
1119
      SCpnt->request.rq_status = RQ_INACTIVE;
1120
      return (STp->buffer)->last_result_fatal;
1121
    }
1122
 
1123
    if (STm->do_async_writes &&
1124
        (((STp->buffer)->buffer_bytes >= STp->write_threshold &&
1125
          (STp->buffer)->buffer_bytes >= STp->block_size) ||
1126
         STp->block_size == 0) ) {
1127
      /* Schedule an asynchronous write */
1128
      if (!SCpnt) {
1129
        SCpnt = allocate_device(NULL, STp->device, 1);
1130
        if (!SCpnt)
1131
          return (-EBUSY);
1132
      }
1133
      if (STp->block_size == 0)
1134
        (STp->buffer)->writing = (STp->buffer)->buffer_bytes;
1135
      else
1136
        (STp->buffer)->writing = ((STp->buffer)->buffer_bytes /
1137
          STp->block_size) * STp->block_size;
1138
      STp->dirty = !((STp->buffer)->writing ==
1139
                     (STp->buffer)->buffer_bytes);
1140
 
1141
      if (STp->block_size == 0)
1142
        blks = (STp->buffer)->writing;
1143
      else
1144
        blks = (STp->buffer)->writing / STp->block_size;
1145
      cmd[2] = blks >> 16;
1146
      cmd[3] = blks >> 8;
1147
      cmd[4] = blks;
1148
      STp->sem = MUTEX_LOCKED;
1149
      SCpnt->request.sem = &(STp->sem);
1150
      SCpnt->request.rq_status = RQ_SCSI_BUSY;
1151
      SCpnt->request.rq_dev = STp->devt;
1152
#if DEBUG
1153
      STp->write_pending = 1;
1154
#endif
1155
 
1156
      scsi_do_cmd (SCpnt,
1157
                   (void *) cmd, (STp->buffer)->b_data,
1158
                   (STp->buffer)->writing,
1159
                   st_sleep_done, ST_TIMEOUT, MAX_WRITE_RETRIES);
1160
    }
1161
    else if (SCpnt != NULL)
1162
      SCpnt->request.rq_status = RQ_INACTIVE;  /* Mark as not busy */
1163
 
1164
    STps->at_sm &= (total == 0);
1165
    return( total);
1166
}
1167
 
1168
 
1169
/* Read command */
1170
        static int
1171
st_read(struct inode * inode, struct file * filp, char * buf, int count)
1172
{
1173
    int total;
1174
    int transfer, blks, bytes;
1175
    static unsigned char cmd[10];
1176
    Scsi_Cmnd * SCpnt = NULL;
1177
    Scsi_Tape * STp;
1178
    ST_mode * STm;
1179
    ST_partstat * STps;
1180
    int dev = TAPE_NR(inode->i_rdev);
1181
 
1182
    STp = &(scsi_tapes[dev]);
1183
    if (STp->ready != ST_READY)
1184
      return (-EIO);
1185
    STm = &(STp->modes[STp->current_mode]);
1186
    if (!STm->defined)
1187
      return (-ENXIO);
1188
#if DEBUG
1189
    if (!STp->in_use) {
1190
      printk(ST_DEB_MSG "st%d: Incorrect device.\n", dev);
1191
      return (-EIO);
1192
    }
1193
#endif
1194
 
1195
    if (STp->can_partitions &&
1196
        (total = update_partition(inode)) < 0)
1197
      return total;
1198
    STps = &(STp->ps[STp->partition]);
1199
 
1200
    if (STp->block_size == 0 &&
1201
        count > (STp->buffer)->buffer_size &&
1202
        !enlarge_buffer(STp->buffer, count, STp->restr_dma))
1203
      return (-EOVERFLOW);
1204
 
1205
    if (!(STm->do_read_ahead) && STp->block_size != 0 &&
1206
        (count % STp->block_size) != 0)
1207
      return (-EIO);    /* Read must be integral number of blocks */
1208
 
1209
    if (STp->do_auto_lock && STp->door_locked == ST_UNLOCKED &&
1210
        !st_int_ioctl(inode, MTLOCK, 0))
1211
      STp->door_locked = ST_LOCKED_AUTO;
1212
 
1213
    if (STps->rw == ST_WRITING) {
1214
      transfer = flush_buffer(inode, filp, 0);
1215
      if (transfer)
1216
        return transfer;
1217
      STps->rw = ST_READING;
1218
    }
1219
    if (STps->moves_after_eof < 255)
1220
      STps->moves_after_eof++;
1221
 
1222
#if DEBUG
1223
    if (debugging && STp->eof != ST_NOEOF)
1224
      printk(ST_DEB_MSG "st%d: EOF flag up. Bytes %d\n", dev,
1225
             (STp->buffer)->buffer_bytes);
1226
#endif
1227
    if (((STp->buffer)->buffer_bytes == 0) &&
1228
        (STp->eof == ST_EOM_OK || STp->eof == ST_EOD))
1229
      return (-EIO);  /* EOM or Blank Check */
1230
 
1231
    STps->rw = ST_READING;
1232
 
1233
    for (total = 0; total < count; ) {
1234
 
1235
      if ((STp->buffer)->buffer_bytes == 0 &&
1236
          STp->eof == ST_NOEOF) {
1237
 
1238
        memset(cmd, 0, 10);
1239
        cmd[0] = READ_6;
1240
        cmd[1] = (STp->block_size != 0);
1241
        if (STp->block_size == 0)
1242
          blks = bytes = count;
1243
        else {
1244
          if (STm->do_read_ahead) {
1245
            blks = (STp->buffer)->buffer_blocks;
1246
            bytes = blks * STp->block_size;
1247
          }
1248
          else {
1249
            bytes = count - total;
1250
            if (bytes > (STp->buffer)->buffer_size)
1251
              bytes = (STp->buffer)->buffer_size;
1252
            blks = bytes / STp->block_size;
1253
            bytes = blks * STp->block_size;
1254
          }
1255
        }
1256
        cmd[2] = blks >> 16;
1257
        cmd[3] = blks >> 8;
1258
        cmd[4] = blks;
1259
 
1260
        SCpnt = st_do_scsi(SCpnt, STp, cmd, bytes, ST_TIMEOUT, MAX_RETRIES);
1261
        if (!SCpnt)
1262
          return (-EBUSY);
1263
 
1264
        (STp->buffer)->read_pointer = 0;
1265
        STp->eof_hit = 0;
1266
        STps->at_sm = 0;
1267
 
1268
        if ((STp->buffer)->last_result_fatal) {
1269
#if DEBUG
1270
          if (debugging)
1271
            printk(ST_DEB_MSG "st%d: Sense: %2x %2x %2x %2x %2x %2x %2x %2x\n",
1272
                   dev,
1273
                   SCpnt->sense_buffer[0], SCpnt->sense_buffer[1],
1274
                   SCpnt->sense_buffer[2], SCpnt->sense_buffer[3],
1275
                   SCpnt->sense_buffer[4], SCpnt->sense_buffer[5],
1276
                   SCpnt->sense_buffer[6], SCpnt->sense_buffer[7]);
1277
#endif
1278
          if ((SCpnt->sense_buffer[0] & 0x70) == 0x70) { /* extended sense */
1279
 
1280
            if ((SCpnt->sense_buffer[2] & 0x0f) == BLANK_CHECK)
1281
                SCpnt->sense_buffer[2] &= 0xcf; /* No need for EOM in this case */
1282
 
1283
            if ((SCpnt->sense_buffer[2] & 0xe0) != 0) { /* EOF, EOM, or ILI */
1284
 
1285
              if ((SCpnt->sense_buffer[0] & 0x80) != 0)
1286
                transfer = (SCpnt->sense_buffer[3] << 24) |
1287
                  (SCpnt->sense_buffer[4] << 16) |
1288
                    (SCpnt->sense_buffer[5] << 8) | SCpnt->sense_buffer[6];
1289
              else
1290
                transfer = 0;
1291
              if (STp->block_size == 0 &&
1292
                  (SCpnt->sense_buffer[2] & 0x0f) == MEDIUM_ERROR)
1293
                transfer = bytes;
1294
 
1295
              if (SCpnt->sense_buffer[2] & 0x20) {
1296
                if (STp->block_size == 0) {
1297
                  if (transfer <= 0)
1298
                    transfer = 0;
1299
                  (STp->buffer)->buffer_bytes = bytes - transfer;
1300
                }
1301
                else {
1302
                  SCpnt->request.rq_status = RQ_INACTIVE;  /* Mark as not busy */
1303
                  if (transfer == blks) {  /* We did not get anything, signal error */
1304
                    printk(KERN_NOTICE "st%d: Incorrect block size.\n", dev);
1305
                    if (STp->drv_block >= 0)
1306
                      STp->drv_block += blks - transfer + 1;
1307
                    st_int_ioctl(inode, MTBSR, 1);
1308
                    return (-EIO);
1309
                  }
1310
                  /* We have some data, deliver it */
1311
                  (STp->buffer)->buffer_bytes = (blks - transfer) * STp->block_size;
1312
#if DEBUG
1313
                  if (debugging)
1314
                    printk(ST_DEB_MSG "st%d: ILI but enough data received %d %d.\n",
1315
                           dev, count - total, (STp->buffer)->buffer_bytes);
1316
#endif
1317
                  if (count - total > (STp->buffer)->buffer_bytes)
1318
                    count = total + (STp->buffer)->buffer_bytes;
1319
                  if (STp->drv_block >= 0)
1320
                    STp->drv_block += 1;
1321
                  if (st_int_ioctl(inode, MTBSR, 1))
1322
                    return (-EIO);
1323
                  SCpnt = NULL;
1324
                }
1325
              }
1326
              else if (SCpnt->sense_buffer[2] & 0x80) { /* FM overrides EOM */
1327
                STp->eof = ST_FM;
1328
                if (STp->block_size == 0)
1329
                  (STp->buffer)->buffer_bytes = 0;
1330
                else
1331
                  (STp->buffer)->buffer_bytes =
1332
                    bytes - transfer * STp->block_size;
1333
#if DEBUG
1334
                if (debugging)
1335
                  printk(ST_DEB_MSG
1336
                    "st%d: EOF detected (%d bytes read, transferred %d bytes).\n",
1337
                         dev, (STp->buffer)->buffer_bytes, total);
1338
#endif
1339
              }
1340
              else if (SCpnt->sense_buffer[2] & 0x40) {
1341
                STp->eof = ST_EOM_OK;
1342
                if (STp->block_size == 0)
1343
                  (STp->buffer)->buffer_bytes = bytes - transfer;
1344
                else
1345
                  (STp->buffer)->buffer_bytes =
1346
                    bytes - transfer * STp->block_size;
1347
#if DEBUG
1348
                if (debugging)
1349
                  printk(ST_DEB_MSG "st%d: EOM detected (%d bytes read).\n", dev,
1350
                         (STp->buffer)->buffer_bytes);
1351
#endif
1352
              }
1353
            } /* end of EOF, EOM, ILI test */
1354
            else { /* nonzero sense key */
1355
#if DEBUG
1356
              if (debugging)
1357
                printk(ST_DEB_MSG "st%d: Tape error while reading.\n", dev);
1358
#endif
1359
              SCpnt->request.rq_status = RQ_INACTIVE;
1360
              STp->drv_block = (-1);
1361
              if (total)
1362
                return total;
1363
              else if (STps->moves_after_eof == 1 &&
1364
                       (SCpnt->sense_buffer[2] & 0x0f) == BLANK_CHECK) {
1365
#if DEBUG
1366
                if (debugging)
1367
                  printk(ST_DEB_MSG
1368
                         "st%d: Zero returned for first BLANK CHECK after EOF.\n",
1369
                         dev);
1370
#endif
1371
                STp->eof = ST_EOD;
1372
                return 0; /* First BLANK_CHECK after EOF */
1373
              }
1374
              else
1375
                return -EIO;
1376
            }
1377
          } /* End of extended sense test */
1378
          else {
1379
            transfer = (STp->buffer)->last_result_fatal;
1380
            SCpnt->request.rq_status = RQ_INACTIVE;  /* Mark as not busy */
1381
            return transfer;
1382
          }
1383
        } /* End of error handling */
1384
        else /* Read successful */
1385
          (STp->buffer)->buffer_bytes = bytes;
1386
 
1387
        if (STp->drv_block >= 0) {
1388
          if (STp->block_size == 0)
1389
            STp->drv_block++;
1390
          else
1391
            STp->drv_block += (STp->buffer)->buffer_bytes / STp->block_size;
1392
        }
1393
 
1394
      } /* if ((STp->buffer)->buffer_bytes == 0 &&
1395
           STp->eof == ST_NOEOF) */
1396
 
1397
      if ((STp->buffer)->buffer_bytes > 0) {
1398
#if DEBUG
1399
        if (debugging && STp->eof != ST_NOEOF)
1400
          printk(ST_DEB_MSG "st%d: EOF up. Left %d, needed %d.\n", dev,
1401
                 (STp->buffer)->buffer_bytes, count - total);
1402
#endif
1403
        transfer = (STp->buffer)->buffer_bytes < count - total ?
1404
          (STp->buffer)->buffer_bytes : count - total;
1405
        memcpy_tofs(buf, (STp->buffer)->b_data +
1406
                    (STp->buffer)->read_pointer,transfer);
1407
        filp->f_pos += transfer;
1408
        buf += transfer;
1409
        total += transfer;
1410
        (STp->buffer)->buffer_bytes -= transfer;
1411
        (STp->buffer)->read_pointer += transfer;
1412
      }
1413
      else if (STp->eof != ST_NOEOF) {
1414
        STp->eof_hit = 1;
1415
        if (SCpnt != NULL)
1416
          SCpnt->request.rq_status = RQ_INACTIVE;  /* Mark as not busy */
1417
        if (total == 0 && STp->eof == ST_FM) {
1418
          STp->eof = ST_NOEOF;
1419
          STp->eof_hit = 0;
1420
          STp->drv_block = 0;
1421
          if (STps->moves_after_eof > 1)
1422
            STps->moves_after_eof = 0;
1423
          if ((STp->mt_status)->mt_fileno >= 0)
1424
            (STp->mt_status)->mt_fileno++;
1425
        }
1426
        if (total == 0 && STp->eof == ST_EOM_OK)
1427
          return (-ENOSPC);  /* ST_EOM_ERROR not used in read */
1428
        return total;
1429
      }
1430
 
1431
      if (STp->block_size == 0)
1432
        count = total;  /* Read only one variable length block */
1433
 
1434
    } /* for (total = 0; total < count; ) */
1435
 
1436
    if (SCpnt != NULL)
1437
      SCpnt->request.rq_status = RQ_INACTIVE;  /* Mark as not busy */
1438
 
1439
    return total;
1440
}
1441
 
1442
 
1443
 
1444
/* Set the driver options */
1445
        static void
1446
st_log_options(Scsi_Tape *STp, ST_mode *STm, int dev)
1447
{
1448
  printk(KERN_INFO
1449
"st%d: Mode %d options: buffer writes: %d, async writes: %d, read ahead: %d\n",
1450
         dev, STp->current_mode, STm->do_buffer_writes, STm->do_async_writes,
1451
         STm->do_read_ahead);
1452
  printk(KERN_INFO
1453
"st%d:    can bsr: %d, two FMs: %d, fast mteom: %d, auto lock: %d,\n",
1454
         dev, STp->can_bsr, STp->two_fm, STp->fast_mteom, STp->do_auto_lock);
1455
  printk(KERN_INFO
1456
"st%d:    defs for wr: %d, no block limits: %d, partitions: %d, s2 log: %d\n",
1457
         dev, STm->defaults_for_writes, STp->omit_blklims, STp->can_partitions,
1458
         STp->scsi2_logical);
1459
#if DEBUG
1460
  printk(KERN_INFO
1461
         "st%d:    debugging: %d\n",
1462
         dev, debugging);
1463
#endif
1464
}
1465
 
1466
 
1467
        static int
1468
st_set_options(struct inode * inode, long options)
1469
{
1470
  int value;
1471
  long code;
1472
  Scsi_Tape *STp;
1473
  ST_mode *STm;
1474
  int dev = TAPE_NR(inode->i_rdev);
1475
 
1476
  STp = &(scsi_tapes[dev]);
1477
  STm = &(STp->modes[STp->current_mode]);
1478
  if (!STm->defined) {
1479
    memcpy(STm, &(STp->modes[0]), sizeof(ST_mode));
1480
    modes_defined = TRUE;
1481
#if DEBUG
1482
    if (debugging)
1483
      printk(ST_DEB_MSG "st%d: Initialized mode %d definition from mode 0\n",
1484
             dev, STp->current_mode);
1485
#endif
1486
  }
1487
 
1488
  code = options & MT_ST_OPTIONS;
1489
  if (code == MT_ST_BOOLEANS) {
1490
    STm->do_buffer_writes = (options & MT_ST_BUFFER_WRITES) != 0;
1491
    STm->do_async_writes  = (options & MT_ST_ASYNC_WRITES) != 0;
1492
    STm->defaults_for_writes = (options & MT_ST_DEF_WRITES) != 0;
1493
    STm->do_read_ahead    = (options & MT_ST_READ_AHEAD) != 0;
1494
    STp->two_fm           = (options & MT_ST_TWO_FM) != 0;
1495
    STp->fast_mteom       = (options & MT_ST_FAST_MTEOM) != 0;
1496
    STp->do_auto_lock     = (options & MT_ST_AUTO_LOCK) != 0;
1497
    STp->can_bsr          = (options & MT_ST_CAN_BSR) != 0;
1498
    STp->omit_blklims     = (options & MT_ST_NO_BLKLIMS) != 0;
1499
    if ((STp->device)->scsi_level >= SCSI_2)
1500
      STp->can_partitions = (options & MT_ST_CAN_PARTITIONS) != 0;
1501
    STp->scsi2_logical    = (options & MT_ST_SCSI2LOGICAL) != 0;
1502
#if DEBUG
1503
    debugging = (options & MT_ST_DEBUGGING) != 0;
1504
#endif
1505
    st_log_options(STp, STm, dev);
1506
  }
1507
  else if (code == MT_ST_SETBOOLEANS || code == MT_ST_CLEARBOOLEANS) {
1508
    value = (code == MT_ST_SETBOOLEANS);
1509
    if ((options & MT_ST_BUFFER_WRITES) != 0)
1510
      STm->do_buffer_writes = value;
1511
    if ((options & MT_ST_ASYNC_WRITES) != 0)
1512
      STm->do_async_writes = value;
1513
    if ((options & MT_ST_DEF_WRITES) != 0)
1514
      STm->defaults_for_writes = value;
1515
    if ((options & MT_ST_READ_AHEAD) != 0)
1516
      STm->do_read_ahead = value;
1517
    if ((options & MT_ST_TWO_FM) != 0)
1518
      STp->two_fm = value;
1519
    if ((options & MT_ST_FAST_MTEOM) != 0)
1520
      STp->fast_mteom = value;
1521
    if ((options & MT_ST_AUTO_LOCK) != 0)
1522
      STp->do_auto_lock = value;
1523
    if ((options & MT_ST_CAN_BSR) != 0)
1524
      STp->can_bsr = value;
1525
    if ((options & MT_ST_NO_BLKLIMS) != 0)
1526
      STp->omit_blklims = value;
1527
    if ((STp->device)->scsi_level >= SCSI_2 &&
1528
        (options & MT_ST_CAN_PARTITIONS) != 0)
1529
      STp->can_partitions = value;
1530
    if ((options & MT_ST_SCSI2LOGICAL) != 0)
1531
      STp->scsi2_logical = value;
1532
#if DEBUG
1533
    if ((options & MT_ST_DEBUGGING) != 0)
1534
      debugging = value;
1535
#endif
1536
    st_log_options(STp, STm, dev);
1537
  }
1538
  else if (code == MT_ST_WRITE_THRESHOLD) {
1539
    value = (options & ~MT_ST_OPTIONS) * ST_BLOCK_SIZE;
1540
    if (value < 1 || value > st_buffer_size) {
1541
      printk(KERN_WARNING "st%d: Write threshold %d too small or too large.\n",
1542
             dev, value);
1543
      return (-EIO);
1544
    }
1545
    STp->write_threshold = value;
1546
    printk(KERN_INFO "st%d: Write threshold set to %d bytes.\n",
1547
           dev, value);
1548
  }
1549
  else if (code == MT_ST_DEF_BLKSIZE) {
1550
    value = (options & ~MT_ST_OPTIONS);
1551
    if (value == ~MT_ST_OPTIONS) {
1552
      STm->default_blksize = (-1);
1553
      printk(KERN_INFO "st%d: Default block size disabled.\n", dev);
1554
    }
1555
    else {
1556
      STm->default_blksize = value;
1557
      printk(KERN_INFO "st%d: Default block size set to %d bytes.\n",
1558
             dev, STm->default_blksize);
1559
    }
1560
  }
1561
  else if (code == MT_ST_DEF_OPTIONS) {
1562
    code = (options & ~MT_ST_CLEAR_DEFAULT);
1563
    value = (options & MT_ST_CLEAR_DEFAULT);
1564
    if (code == MT_ST_DEF_DENSITY) {
1565
      if (value == MT_ST_CLEAR_DEFAULT) {
1566
        STm->default_density = (-1);
1567
        printk(KERN_INFO "st%d: Density default disabled.\n", dev);
1568
      }
1569
      else {
1570
        STm->default_density = value & 0xff;
1571
        printk(KERN_INFO "st%d: Density default set to %x\n",
1572
               dev, STm->default_density);
1573
      }
1574
    }
1575
    else if (code == MT_ST_DEF_DRVBUFFER) {
1576
      if (value == MT_ST_CLEAR_DEFAULT) {
1577
        STp->default_drvbuffer = 0xff;
1578
        printk(KERN_INFO "st%d: Drive buffer default disabled.\n", dev);
1579
      }
1580
      else {
1581
        STp->default_drvbuffer = value & 7;
1582
        printk(KERN_INFO "st%d: Drive buffer default set to %x\n",
1583
               dev, STp->default_drvbuffer);
1584
      }
1585
    }
1586
    else if (code == MT_ST_DEF_COMPRESSION) {
1587
      if (value == MT_ST_CLEAR_DEFAULT) {
1588
        STm->default_compression = ST_DONT_TOUCH;
1589
        printk(KERN_INFO "st%d: Compression default disabled.\n", dev);
1590
      }
1591
      else {
1592
        STm->default_compression = (value & 1 ? ST_YES : ST_NO);
1593
        printk(KERN_INFO "st%d: Compression default set to %x\n",
1594
               dev, (value & 1));
1595
      }
1596
    }
1597
  }
1598
  else
1599
    return (-EIO);
1600
 
1601
  return 0;
1602
}
1603
 
1604
 
1605
#define COMPRESSION_PAGE        0x0f
1606
#define COMPRESSION_PAGE_LENGTH 16
1607
 
1608
#define MODE_HEADER_LENGTH  4
1609
 
1610
#define DCE_MASK  0x80
1611
#define DCC_MASK  0x40
1612
#define RED_MASK  0x60
1613
 
1614
 
1615
/* Control the compression with mode page 15. Algorithm not changed if zero. */
1616
        static int
1617
st_compression(Scsi_Tape * STp, int state)
1618
{
1619
  int dev;
1620
  unsigned char cmd[10];
1621
  Scsi_Cmnd * SCpnt = NULL;
1622
 
1623
  if (STp->ready != ST_READY)
1624
    return (-EIO);
1625
 
1626
  /* Read the current page contents */
1627
  memset(cmd, 0, 10);
1628
  cmd[0] = MODE_SENSE;
1629
  cmd[1] = 8;
1630
  cmd[2] = COMPRESSION_PAGE;
1631
  cmd[4] = COMPRESSION_PAGE_LENGTH + MODE_HEADER_LENGTH;
1632
 
1633
  SCpnt = st_do_scsi(SCpnt, STp, cmd, cmd[4], ST_TIMEOUT, 0);
1634
  if (SCpnt == NULL)
1635
    return (-EBUSY);
1636
  dev = TAPE_NR(SCpnt->request.rq_dev);
1637
 
1638
  if ((STp->buffer)->last_result_fatal != 0) {
1639
#if DEBUG
1640
    if (debugging)
1641
      printk(ST_DEB_MSG "st%d: Compression mode page not supported.\n", dev);
1642
#endif
1643
    SCpnt->request.rq_status = RQ_INACTIVE;  /* Mark as not busy */
1644
    return (-EIO);
1645
  }
1646
#if DEBUG
1647
  if (debugging)
1648
    printk(ST_DEB_MSG "st%d: Compression state is %d.\n", dev,
1649
           ((STp->buffer)->b_data[MODE_HEADER_LENGTH + 2] & DCE_MASK ? 1 : 0));
1650
#endif
1651
 
1652
  /* Check if compression can be changed */
1653
  if (((STp->buffer)->b_data[MODE_HEADER_LENGTH + 2] & DCC_MASK) == 0) {
1654
#if DEBUG
1655
    if (debugging)
1656
      printk(ST_DEB_MSG "st%d: Compression not supported.\n", dev);
1657
#endif
1658
    SCpnt->request.rq_status = RQ_INACTIVE;  /* Mark as not busy */
1659
    return (-EIO);
1660
  }
1661
 
1662
  /* Do the change */
1663
  if (state)
1664
    (STp->buffer)->b_data[MODE_HEADER_LENGTH + 2] |= DCE_MASK;
1665
  else
1666
    (STp->buffer)->b_data[MODE_HEADER_LENGTH + 2] &= ~DCE_MASK;
1667
 
1668
  memset(cmd, 0, 10);
1669
  cmd[0] = MODE_SELECT;
1670
  cmd[1] = 0x10;
1671
  cmd[4] = COMPRESSION_PAGE_LENGTH + MODE_HEADER_LENGTH;
1672
 
1673
  (STp->buffer)->b_data[0] = 0;  /* Reserved data length */
1674
  (STp->buffer)->b_data[1] = 0;  /* Reserved media type byte */
1675
  (STp->buffer)->b_data[MODE_HEADER_LENGTH] &= 0x3f;
1676
  SCpnt = st_do_scsi(SCpnt, STp, cmd, cmd[4], ST_TIMEOUT, 0);
1677
 
1678
  if ((STp->buffer)->last_result_fatal != 0) {
1679
#if DEBUG
1680
    if (debugging)
1681
      printk(ST_DEB_MSG "st%d: Compression change failed.\n", dev);
1682
#endif
1683
    SCpnt->request.rq_status = RQ_INACTIVE;  /* Mark as not busy */
1684
    return (-EIO);
1685
  }
1686
 
1687
#if DEBUG
1688
  if (debugging)
1689
    printk(ST_DEB_MSG "st%d: Compression state changed to %d.\n",
1690
           dev, state);
1691
#endif
1692
 
1693
  SCpnt->request.rq_status = RQ_INACTIVE;  /* Mark as not busy */
1694
  STp->compression_changed = TRUE;
1695
  return 0;
1696
}
1697
 
1698
 
1699
/* Internal ioctl function */
1700
        static int
1701
st_int_ioctl(struct inode * inode,
1702
             unsigned int cmd_in, unsigned long arg)
1703
{
1704
   int timeout = ST_LONG_TIMEOUT;
1705
   long ltmp;
1706
   int i, ioctl_result;
1707
   unsigned char cmd[10];
1708
   Scsi_Cmnd * SCpnt;
1709
   Scsi_Tape * STp;
1710
   ST_partstat * STps;
1711
   int fileno, blkno, at_sm, undone, datalen;
1712
   int dev = TAPE_NR(inode->i_rdev);
1713
 
1714
   STp = &(scsi_tapes[dev]);
1715
   if (STp->ready != ST_READY && cmd_in != MTLOAD)
1716
     return (-EIO);
1717
   STps = &(STp->ps[STp->partition]);
1718
   fileno = (STp->mt_status)->mt_fileno ;
1719
   blkno = STp->drv_block;
1720
   at_sm = STps->at_sm;
1721
 
1722
   memset(cmd, 0, 10);
1723
   datalen = 0;
1724
   switch (cmd_in) {
1725
     case MTFSF:
1726
     case MTFSFM:
1727
       cmd[0] = SPACE;
1728
       cmd[1] = 0x01; /* Space FileMarks */
1729
       cmd[2] = (arg >> 16);
1730
       cmd[3] = (arg >> 8);
1731
       cmd[4] = arg;
1732
#if DEBUG
1733
       if (debugging)
1734
         printk(ST_DEB_MSG "st%d: Spacing tape forward over %d filemarks.\n",
1735
                dev, cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
1736
#endif
1737
       if (fileno >= 0)
1738
         fileno += arg;
1739
       blkno = 0;
1740
       at_sm &= (arg == 0);
1741
       break;
1742
     case MTBSF:
1743
     case MTBSFM:
1744
       cmd[0] = SPACE;
1745
       cmd[1] = 0x01; /* Space FileMarks */
1746
       ltmp = (-arg);
1747
       cmd[2] = (ltmp >> 16);
1748
       cmd[3] = (ltmp >> 8);
1749
       cmd[4] = ltmp;
1750
#if DEBUG
1751
       if (debugging) {
1752
         if (cmd[2] & 0x80)
1753
           ltmp = 0xff000000;
1754
         ltmp = ltmp | (cmd[2] << 16) | (cmd[3] << 8) | cmd[4];
1755
         printk(ST_DEB_MSG "st%d: Spacing tape backward over %ld filemarks.\n",
1756
                dev, (-ltmp));
1757
       }
1758
#endif
1759
       if (fileno >= 0)
1760
         fileno -= arg;
1761
       blkno = (-1);  /* We can't know the block number */
1762
       at_sm &= (arg == 0);
1763
       break;
1764
     case MTFSR:
1765
       cmd[0] = SPACE;
1766
       cmd[1] = 0x00; /* Space Blocks */
1767
       cmd[2] = (arg >> 16);
1768
       cmd[3] = (arg >> 8);
1769
       cmd[4] = arg;
1770
#if DEBUG
1771
       if (debugging)
1772
         printk(ST_DEB_MSG "st%d: Spacing tape forward %d blocks.\n", dev,
1773
                cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
1774
#endif
1775
       if (blkno >= 0)
1776
         blkno += arg;
1777
       at_sm &= (arg == 0);
1778
       break;
1779
     case MTBSR:
1780
       cmd[0] = SPACE;
1781
       cmd[1] = 0x00; /* Space Blocks */
1782
       ltmp = (-arg);
1783
       cmd[2] = (ltmp >> 16);
1784
       cmd[3] = (ltmp >> 8);
1785
       cmd[4] = ltmp;
1786
#if DEBUG
1787
       if (debugging) {
1788
         if (cmd[2] & 0x80)
1789
           ltmp = 0xff000000;
1790
         ltmp = ltmp | (cmd[2] << 16) | (cmd[3] << 8) | cmd[4];
1791
         printk(ST_DEB_MSG "st%d: Spacing tape backward %ld blocks.\n", dev, (-ltmp));
1792
       }
1793
#endif
1794
       if (blkno >= 0)
1795
         blkno -= arg;
1796
       at_sm &= (arg == 0);
1797
       break;
1798
     case MTFSS:
1799
       cmd[0] = SPACE;
1800
       cmd[1] = 0x04; /* Space Setmarks */
1801
       cmd[2] = (arg >> 16);
1802
       cmd[3] = (arg >> 8);
1803
       cmd[4] = arg;
1804
#if DEBUG
1805
       if (debugging)
1806
         printk(ST_DEB_MSG "st%d: Spacing tape forward %d setmarks.\n", dev,
1807
                cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
1808
#endif
1809
       if (arg != 0) {
1810
         blkno = fileno = (-1);
1811
         at_sm = 1;
1812
       }
1813
       break;
1814
     case MTBSS:
1815
       cmd[0] = SPACE;
1816
       cmd[1] = 0x04; /* Space Setmarks */
1817
       ltmp = (-arg);
1818
       cmd[2] = (ltmp >> 16);
1819
       cmd[3] = (ltmp >> 8);
1820
       cmd[4] = ltmp;
1821
#if DEBUG
1822
       if (debugging) {
1823
         if (cmd[2] & 0x80)
1824
           ltmp = 0xff000000;
1825
         ltmp = ltmp | (cmd[2] << 16) | (cmd[3] << 8) | cmd[4];
1826
         printk(ST_DEB_MSG "st%d: Spacing tape backward %ld setmarks.\n",
1827
                dev, (-ltmp));
1828
       }
1829
#endif
1830
       if (arg != 0) {
1831
         blkno = fileno = (-1);
1832
         at_sm = 1;
1833
       }
1834
       break;
1835
     case MTWEOF:
1836
     case MTWSM:
1837
       if (STp->write_prot)
1838
         return (-EACCES);
1839
       cmd[0] = WRITE_FILEMARKS;
1840
       if (cmd_in == MTWSM)
1841
         cmd[1] = 2;
1842
       cmd[2] = (arg >> 16);
1843
       cmd[3] = (arg >> 8);
1844
       cmd[4] = arg;
1845
       timeout = ST_TIMEOUT;
1846
#if DEBUG
1847
       if (debugging) {
1848
         if (cmd_in == MTWEOF)
1849
           printk(ST_DEB_MSG "st%d: Writing %d filemarks.\n", dev,
1850
                  cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
1851
         else
1852
           printk(ST_DEB_MSG "st%d: Writing %d setmarks.\n", dev,
1853
                  cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
1854
       }
1855
#endif
1856
       if (fileno >= 0)
1857
         fileno += arg;
1858
       blkno = 0;
1859
       at_sm = (cmd_in == MTWSM);
1860
       break;
1861
     case MTREW:
1862
       cmd[0] = REZERO_UNIT;
1863
#if ST_NOWAIT
1864
       cmd[1] = 1;  /* Don't wait for completion */
1865
       timeout = ST_TIMEOUT;
1866
#endif
1867
#if DEBUG
1868
       if (debugging)
1869
         printk(ST_DEB_MSG "st%d: Rewinding tape.\n", dev);
1870
#endif
1871
       fileno = blkno = at_sm = 0 ;
1872
       break;
1873
     case MTOFFL:
1874
     case MTLOAD:
1875
     case MTUNLOAD:
1876
       cmd[0] = START_STOP;
1877
       if (cmd_in == MTLOAD)
1878
         cmd[4] |= 1;
1879
#if ST_NOWAIT
1880
       cmd[1] = 1;  /* Don't wait for completion */
1881
       timeout = ST_TIMEOUT;
1882
#else
1883
       timeout = ST_LONG_TIMEOUT * 8;
1884
#endif
1885
#if DEBUG
1886
       if (debugging) {
1887
         if (cmd_in != MTLOAD)
1888
           printk(ST_DEB_MSG "st%d: Unloading tape.\n", dev);
1889
         else
1890
           printk(ST_DEB_MSG "st%d: Loading tape.\n", dev);
1891
       }
1892
#endif
1893
       fileno = blkno = at_sm = 0 ;
1894
       break;
1895
     case MTNOP:
1896
#if DEBUG
1897
       if (debugging)
1898
         printk(ST_DEB_MSG "st%d: No op on tape.\n", dev);
1899
#endif
1900
       return 0;  /* Should do something ? */
1901
       break;
1902
     case MTRETEN:
1903
       cmd[0] = START_STOP;
1904
#if ST_NOWAIT
1905
       cmd[1] = 1;  /* Don't wait for completion */
1906
       timeout = ST_TIMEOUT;
1907
#endif
1908
       cmd[4] = 3;
1909
#if DEBUG
1910
       if (debugging)
1911
         printk(ST_DEB_MSG "st%d: Retensioning tape.\n", dev);
1912
#endif
1913
       fileno = blkno = at_sm = 0;
1914
       break;
1915
     case MTEOM:
1916
       if (!STp->fast_mteom) {
1917
         /* space to the end of tape */
1918
         ioctl_result = st_int_ioctl(inode, MTFSF, 0x3fff);
1919
         fileno = (STp->mt_status)->mt_fileno ;
1920
         if (STp->eof == ST_EOD || STp->eof == ST_EOM_OK)
1921
           return 0;
1922
         /* The next lines would hide the number of spaced FileMarks
1923
            That's why I inserted the previous lines. I had no luck
1924
            with detecting EOM with FSF, so we go now to EOM.
1925
            Joerg Weule */
1926
       }
1927
       else
1928
         fileno = (-1);
1929
       cmd[0] = SPACE;
1930
       cmd[1] = 3;
1931
#if DEBUG
1932
       if (debugging)
1933
         printk(ST_DEB_MSG "st%d: Spacing to end of recorded medium.\n", dev);
1934
#endif
1935
       blkno = 0;
1936
       at_sm = 0;
1937
       break;
1938
     case MTERASE:
1939
       if (STp->write_prot)
1940
         return (-EACCES);
1941
       cmd[0] = ERASE;
1942
       cmd[1] = 1;  /* To the end of tape */
1943
#if ST_NOWAIT
1944
       cmd[1] |= 2;  /* Don't wait for completion */
1945
       timeout = ST_TIMEOUT;
1946
#else
1947
       timeout = ST_LONG_TIMEOUT * 8;
1948
#endif
1949
#if DEBUG
1950
       if (debugging)
1951
         printk(ST_DEB_MSG "st%d: Erasing tape.\n", dev);
1952
#endif
1953
       fileno = blkno = at_sm = 0 ;
1954
       break;
1955
     case MTLOCK:
1956
       cmd[0] = ALLOW_MEDIUM_REMOVAL;
1957
       cmd[4] = SCSI_REMOVAL_PREVENT;
1958
#if DEBUG
1959
       if (debugging)
1960
         printk(ST_DEB_MSG "st%d: Locking drive door.\n", dev);
1961
#endif;
1962
       break;
1963
     case MTUNLOCK:
1964
       cmd[0] = ALLOW_MEDIUM_REMOVAL;
1965
       cmd[4] = SCSI_REMOVAL_ALLOW;
1966
#if DEBUG
1967
       if (debugging)
1968
         printk(ST_DEB_MSG "st%d: Unlocking drive door.\n", dev);
1969
#endif;
1970
       break;
1971
     case MTSETBLK:  /* Set block length */
1972
     case MTSETDENSITY: /* Set tape density */
1973
     case MTSETDRVBUFFER: /* Set drive buffering */
1974
     case SET_DENS_AND_BLK: /* Set density and block size */
1975
       if (STp->dirty || (STp->buffer)->buffer_bytes != 0)
1976
         return (-EIO);   /* Not allowed if data in buffer */
1977
       if ((cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) &&
1978
           (arg & MT_ST_BLKSIZE_MASK) != 0 &&
1979
           ((arg & MT_ST_BLKSIZE_MASK) < STp->min_block ||
1980
            (arg & MT_ST_BLKSIZE_MASK) > STp->max_block ||
1981
            (arg & MT_ST_BLKSIZE_MASK) > st_buffer_size)) {
1982
         printk(KERN_WARNING "st%d: Illegal block size.\n", dev);
1983
         return (-EINVAL);
1984
       }
1985
       cmd[0] = MODE_SELECT;
1986
       cmd[4] = datalen = 12;
1987
 
1988
       memset((STp->buffer)->b_data, 0, 12);
1989
       if (cmd_in == MTSETDRVBUFFER)
1990
         (STp->buffer)->b_data[2] = (arg & 7) << 4;
1991
       else
1992
         (STp->buffer)->b_data[2] =
1993
           STp->drv_buffer << 4;
1994
       (STp->buffer)->b_data[3] = 8;     /* block descriptor length */
1995
       if (cmd_in == MTSETDENSITY) {
1996
         (STp->buffer)->b_data[4] = arg;
1997
         STp->density_changed = TRUE;  /* At least we tried ;-) */
1998
       }
1999
       else if (cmd_in == SET_DENS_AND_BLK)
2000
         (STp->buffer)->b_data[4] = arg >> 24;
2001
       else
2002
         (STp->buffer)->b_data[4] = STp->density;
2003
       if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) {
2004
         ltmp = arg & MT_ST_BLKSIZE_MASK;
2005
         if (cmd_in == MTSETBLK)
2006
           STp->blksize_changed = TRUE;  /* At least we tried ;-) */
2007
       }
2008
       else
2009
         ltmp = STp->block_size;
2010
       (STp->buffer)->b_data[9] = (ltmp >> 16);
2011
       (STp->buffer)->b_data[10] = (ltmp >> 8);
2012
       (STp->buffer)->b_data[11] = ltmp;
2013
       timeout = ST_TIMEOUT;
2014
#if DEBUG
2015
       if (debugging) {
2016
         if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK)
2017
           printk(ST_DEB_MSG "st%d: Setting block size to %d bytes.\n", dev,
2018
                  (STp->buffer)->b_data[9] * 65536 +
2019
                  (STp->buffer)->b_data[10] * 256 +
2020
                  (STp->buffer)->b_data[11]);
2021
         if (cmd_in == MTSETDENSITY || cmd_in == SET_DENS_AND_BLK)
2022
           printk(ST_DEB_MSG "st%d: Setting density code to %x.\n", dev,
2023
                  (STp->buffer)->b_data[4]);
2024
         if (cmd_in == MTSETDRVBUFFER)
2025
           printk(ST_DEB_MSG "st%d: Setting drive buffer code to %d.\n", dev,
2026
                  ((STp->buffer)->b_data[2] >> 4) & 7);
2027
       }
2028
#endif
2029
       break;
2030
     default:
2031
       return (-ENOSYS);
2032
     }
2033
 
2034
   SCpnt = st_do_scsi(NULL, STp, cmd, datalen, timeout, MAX_RETRIES);
2035
   if (!SCpnt)
2036
     return (-EBUSY);
2037
 
2038
   ioctl_result = (STp->buffer)->last_result_fatal;
2039
 
2040
   SCpnt->request.rq_status = RQ_INACTIVE;  /* Mark as not busy */
2041
 
2042
   if (cmd_in == MTFSF)
2043
     STps->moves_after_eof = 0;
2044
   else if (cmd_in != MTLOAD && cmd_in != MTLOCK && cmd_in != MTUNLOCK &&
2045
            cmd_in != MTSETBLK && cmd_in != MTSETDENSITY &&
2046
            cmd_in != MTSETDRVBUFFER)
2047
     STps->moves_after_eof = 1;
2048
   if (!ioctl_result) {  /* SCSI command successful */
2049
     STp->drv_block = blkno;
2050
     (STp->mt_status)->mt_fileno = fileno;
2051
     STps->at_sm = at_sm;
2052
     if (cmd_in == MTLOCK)
2053
       STp->door_locked = ST_LOCKED_EXPLICIT;
2054
     else if (cmd_in == MTUNLOCK)
2055
       STp->door_locked = ST_UNLOCKED;
2056
     if (cmd_in == MTBSFM)
2057
       ioctl_result = st_int_ioctl(inode, MTFSF, 1);
2058
     else if (cmd_in == MTFSFM)
2059
       ioctl_result = st_int_ioctl(inode, MTBSF, 1);
2060
     else if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) {
2061
       STp->block_size = arg & MT_ST_BLKSIZE_MASK;
2062
       if (STp->block_size != 0)
2063
         (STp->buffer)->buffer_blocks =
2064
           (STp->buffer)->buffer_size / STp->block_size;
2065
       (STp->buffer)->buffer_bytes = (STp->buffer)->read_pointer = 0;
2066
     }
2067
     else if (cmd_in == MTSETDRVBUFFER)
2068
       STp->drv_buffer = (arg & 7);
2069
     else if (cmd_in == MTSETDENSITY)
2070
       STp->density = arg;
2071
     else if (cmd_in == MTEOM) {
2072
       STp->eof = ST_EOD;
2073
       STp->eof_hit = 0;
2074
     }
2075
     else if (cmd_in != MTSETBLK && cmd_in != MTNOP) {
2076
       STp->eof = ST_NOEOF;
2077
       STp->eof_hit = 0;
2078
     }
2079
     if (cmd_in == SET_DENS_AND_BLK)
2080
       STp->density = arg >> MT_ST_DENSITY_SHIFT;
2081
     if (cmd_in == MTOFFL || cmd_in == MTUNLOAD)
2082
       STp->rew_at_close = 0;
2083
     else if (cmd_in == MTLOAD) {
2084
       STp->rew_at_close = (MINOR(inode->i_rdev) & 0x80) == 0;
2085
       for (i=0; i < ST_NBR_PARTITIONS; i++) {
2086
         STp->ps[i].rw = ST_IDLE;
2087
         STp->ps[i].moves_after_eof = 1;
2088
         STp->ps[i].last_block_valid = FALSE;
2089
       }
2090
       STp->partition = 0;
2091
     }
2092
   } else {  /* SCSI command was not completely successful */
2093
     if (SCpnt->sense_buffer[2] & 0x40) {
2094
       if (cmd_in != MTBSF && cmd_in != MTBSFM &&
2095
           cmd_in != MTBSR && cmd_in != MTBSS)
2096
         STp->eof = ST_EOM_OK;
2097
       STp->eof_hit = 0;
2098
       STp->drv_block = 0;
2099
     }
2100
     undone = (
2101
          (SCpnt->sense_buffer[3] << 24) +
2102
          (SCpnt->sense_buffer[4] << 16) +
2103
          (SCpnt->sense_buffer[5] << 8) +
2104
          SCpnt->sense_buffer[6] );
2105
     if (cmd_in == MTWEOF &&
2106
       (SCpnt->sense_buffer[0] & 0x70) == 0x70 &&
2107
       (SCpnt->sense_buffer[2] & 0x4f) == 0x40 &&
2108
       ((SCpnt->sense_buffer[0] & 0x80) == 0 || undone == 0)) {
2109
       ioctl_result = 0;  /* EOF written succesfully at EOM */
2110
       if (fileno >= 0)
2111
         fileno++;
2112
       (STp->mt_status)->mt_fileno = fileno;
2113
     }
2114
     else if ( (cmd_in == MTFSF) || (cmd_in == MTFSFM) ) {
2115
       if (fileno >= 0)
2116
         (STp->mt_status)->mt_fileno = fileno - undone ;
2117
       else
2118
         (STp->mt_status)->mt_fileno = fileno;
2119
       STp->drv_block = 0;
2120
     }
2121
     else if ( (cmd_in == MTBSF) || (cmd_in == MTBSFM) ) {
2122
       (STp->mt_status)->mt_fileno = fileno + undone ;
2123
       STp->drv_block = 0;
2124
     }
2125
     else if (cmd_in == MTFSR) {
2126
       if (SCpnt->sense_buffer[2] & 0x80) { /* Hit filemark */
2127
         (STp->mt_status)->mt_fileno++;
2128
         STp->drv_block = 0;
2129
       }
2130
       else {
2131
         if (blkno >= undone)
2132
           STp->drv_block = blkno - undone;
2133
         else
2134
           STp->drv_block = (-1);
2135
       }
2136
     }
2137
     else if (cmd_in == MTBSR) {
2138
       if (SCpnt->sense_buffer[2] & 0x80) { /* Hit filemark */
2139
         (STp->mt_status)->mt_fileno--;
2140
         STp->drv_block = (-1);
2141
       }
2142
       else {
2143
         if (blkno >= 0)
2144
           STp->drv_block = blkno + undone;
2145
         else
2146
           STp->drv_block = (-1);
2147
       }
2148
     }
2149
     else if (cmd_in == MTEOM) {
2150
       (STp->mt_status)->mt_fileno = (-1);
2151
       STp->drv_block = (-1);
2152
     }
2153
     if (STp->eof == ST_NOEOF &&
2154
         (SCpnt->sense_buffer[2] & 0x0f) == BLANK_CHECK)
2155
       STp->eof = ST_EOD;
2156
     if (cmd_in == MTLOCK)
2157
       STp->door_locked = ST_LOCK_FAILS;
2158
   }
2159
 
2160
   return ioctl_result;
2161
}
2162
 
2163
 
2164
/* Get the tape position. If bt == 2, arg points into a kernel space mt_loc
2165
    structure. */
2166
 
2167
        static int
2168
get_location(struct inode * inode, unsigned int *block, int *partition,
2169
             int logical)
2170
{
2171
    Scsi_Tape *STp;
2172
    int dev = TAPE_NR(inode->i_rdev);
2173
    int result;
2174
    unsigned char scmd[10];
2175
    Scsi_Cmnd *SCpnt;
2176
 
2177
    STp = &(scsi_tapes[dev]);
2178
    if (STp->ready != ST_READY)
2179
      return (-EIO);
2180
 
2181
    memset (scmd, 0, 10);
2182
    if ((STp->device)->scsi_level < SCSI_2) {
2183
      scmd[0] = QFA_REQUEST_BLOCK;
2184
      scmd[4] = 3;
2185
    }
2186
    else {
2187
      scmd[0] = READ_POSITION;
2188
      if (!logical && !STp->scsi2_logical)
2189
        scmd[1] = 1;
2190
    }
2191
    SCpnt = st_do_scsi(NULL, STp, scmd, 20, ST_TIMEOUT, MAX_READY_RETRIES);
2192
    if (!SCpnt)
2193
      return (-EBUSY);
2194
 
2195
    if ((STp->buffer)->last_result_fatal != 0 ||
2196
        (STp->device->scsi_level >= SCSI_2 &&
2197
         ((STp->buffer)->b_data[0] & 4) != 0)) {
2198
      *block = *partition = 0;
2199
#if DEBUG
2200
      if (debugging)
2201
        printk(ST_DEB_MSG "st%d: Can't read tape position.\n", dev);
2202
#endif
2203
      result = (-EIO);
2204
    }
2205
    else {
2206
      result = 0;
2207
      if ((STp->device)->scsi_level < SCSI_2) {
2208
        *block = ((STp->buffer)->b_data[0] << 16)
2209
        + ((STp->buffer)->b_data[1] << 8)
2210
        + (STp->buffer)->b_data[2];
2211
        *partition = 0;
2212
      }
2213
      else {
2214
        *block = ((STp->buffer)->b_data[4] << 24)
2215
          + ((STp->buffer)->b_data[5] << 16)
2216
          + ((STp->buffer)->b_data[6] << 8)
2217
          + (STp->buffer)->b_data[7];
2218
        *partition = (STp->buffer)->b_data[1];
2219
        if (((STp->buffer)->b_data[0] & 0x80) &&
2220
            (STp->buffer)->b_data[1] == 0) /* BOP of partition 0 */
2221
          STp->drv_block = (STp->mt_status)->mt_fileno = 0;
2222
      }
2223
#if DEBUG
2224
      if (debugging)
2225
        printk(ST_DEB_MSG "st%d: Got tape pos. blk %d part %d.\n", dev,
2226
               *block, *partition);
2227
#endif
2228
 
2229
    }
2230
    SCpnt->request.rq_status = RQ_INACTIVE;  /* Mark as not busy */
2231
 
2232
    return result;
2233
}
2234
 
2235
 
2236
/* Set the tape block and partition. Negative partition means that only the
2237
   block should be set in vendor specific way. */
2238
        static int
2239
set_location(struct inode * inode, unsigned int block, int partition,
2240
             int logical)
2241
{
2242
    Scsi_Tape *STp;
2243
    ST_partstat *STps;
2244
    int dev = TAPE_NR(inode->i_rdev);
2245
    int result, p;
2246
    unsigned int blk;
2247
    int timeout = ST_LONG_TIMEOUT;
2248
    unsigned char scmd[10];
2249
    Scsi_Cmnd *SCpnt;
2250
 
2251
    STp = &(scsi_tapes[dev]);
2252
    if (STp->ready != ST_READY)
2253
      return (-EIO);
2254
    STps = &(STp->ps[STp->partition]);
2255
 
2256
#if DEBUG
2257
    if (debugging)
2258
      printk(ST_DEB_MSG "st%d: Setting block to %d and partition to %d.\n",
2259
             dev, block, partition);
2260
    if (partition < 0)
2261
      return (-EIO);
2262
#endif
2263
 
2264
    /* Update the location at the partition we are leaving */
2265
    if ((!STp->can_partitions && partition != 0) ||
2266
        partition >= ST_NBR_PARTITIONS)
2267
      return (-EINVAL);
2268
    if (partition != STp->partition) {
2269
      if (get_location(inode, &blk, &p, 1))
2270
        STps->last_block_valid = FALSE;
2271
      else {
2272
        STps->last_block_valid = TRUE;
2273
        STps->last_block_visited = blk;
2274
#if DEBUG
2275
        if (debugging)
2276
          printk(ST_DEB_MSG "st%d: Visited block %d for partition %d saved.\n",
2277
                 dev, blk, STp->partition);
2278
#endif
2279
      }
2280
    }
2281
 
2282
    memset (scmd, 0, 10);
2283
    if ((STp->device)->scsi_level < SCSI_2) {
2284
      scmd[0] = QFA_SEEK_BLOCK;
2285
      scmd[2] = (block >> 16);
2286
      scmd[3] = (block >> 8);
2287
      scmd[4] = block;
2288
      scmd[5] = 0;
2289
    }
2290
    else {
2291
      scmd[0] = SEEK_10;
2292
      scmd[3] = (block >> 24);
2293
      scmd[4] = (block >> 16);
2294
      scmd[5] = (block >> 8);
2295
      scmd[6] = block;
2296
      if (!logical && !STp->scsi2_logical)
2297
        scmd[1] = 4;
2298
      if (STp->partition != partition) {
2299
        scmd[1] |= 2;
2300
        scmd[8] = partition;
2301
#if DEBUG
2302
        if (debugging)
2303
          printk(ST_DEB_MSG "st%d: Trying to change partition from %d to %d\n",
2304
                 dev, STp->partition, partition);
2305
#endif
2306
      }
2307
     }
2308
#if ST_NOWAIT
2309
    scmd[1] |= 1;  /* Don't wait for completion */
2310
    timeout = ST_TIMEOUT;
2311
#endif
2312
 
2313
    SCpnt = st_do_scsi(NULL, STp, scmd, 20, timeout, MAX_READY_RETRIES);
2314
    if (!SCpnt)
2315
      return (-EBUSY);
2316
 
2317
    STp->drv_block = (STp->mt_status)->mt_fileno = (-1);
2318
    STp->eof = ST_NOEOF;
2319
    if ((STp->buffer)->last_result_fatal != 0) {
2320
      result = (-EIO);
2321
      if (STp->can_partitions &&
2322
          (STp->device)->scsi_level >= SCSI_2 &&
2323
          (p = find_partition(inode)) >= 0)
2324
        STp->partition = p;
2325
    }
2326
    else {
2327
      if (STp->can_partitions) {
2328
        STp->partition = partition;
2329
        STps = &(STp->ps[partition]);
2330
        if (!STps->last_block_valid ||
2331
            STps->last_block_visited != block) {
2332
          STps->moves_after_eof = 1;
2333
          STps->at_sm = 0;
2334
          STps->rw = ST_IDLE;
2335
        }
2336
      }
2337
      else
2338
        STps->at_sm = 0;
2339
      if (block == 0)
2340
        STp->drv_block = (STp->mt_status)->mt_fileno = 0;
2341
      result = 0;
2342
    }
2343
    SCpnt->request.rq_status = RQ_INACTIVE;  /* Mark as not busy */
2344
 
2345
    return result;
2346
}
2347
 
2348
 
2349
/* Find the current partition number for the drive status. Called from open and
2350
   returns either partition number of negative error code. */
2351
        static int
2352
find_partition(struct inode *inode)
2353
{
2354
    int i, partition;
2355
    unsigned int block;
2356
 
2357
    if ((i = get_location(inode, &block, &partition, 1)) < 0)
2358
      return i;
2359
    if (partition >= ST_NBR_PARTITIONS)
2360
      return (-EIO);
2361
    return partition;
2362
}
2363
 
2364
 
2365
/* Change the partition if necessary */
2366
        static int
2367
update_partition(struct inode * inode)
2368
{
2369
    int dev = TAPE_NR(inode->i_rdev);
2370
    Scsi_Tape *STp;
2371
    ST_partstat *STps;
2372
 
2373
    STp = &(scsi_tapes[dev]);
2374
    if (STp->partition == STp->new_partition)
2375
      return 0;
2376
    STps = &(STp->ps[STp->new_partition]);
2377
    if (!STps->last_block_valid)
2378
      STps->last_block_visited = 0;
2379
    return set_location(inode, STps->last_block_visited, STp->new_partition, 1);
2380
}
2381
 
2382
/* Functions for reading and writing the medium partition mode page. These
2383
   seem to work with Wangtek 6200HS and HP C1533A. */
2384
 
2385
#define PART_PAGE   0x11
2386
#define PART_PAGE_LENGTH 10
2387
 
2388
/* Get the number of partitions on the tape. As a side effect reads the
2389
   mode page into the tape buffer. */
2390
        static int
2391
nbr_partitions(struct inode * inode)
2392
{
2393
    int dev = TAPE_NR(inode->i_rdev), result;
2394
    Scsi_Tape *STp;
2395
    Scsi_Cmnd * SCpnt = NULL;
2396
    unsigned char cmd[10];
2397
 
2398
    STp = &(scsi_tapes[dev]);
2399
    if (STp->ready != ST_READY)
2400
      return (-EIO);
2401
 
2402
    memset ((void *) &cmd[0], 0, 10);
2403
    cmd[0] = MODE_SENSE;
2404
    cmd[1] = 8;   /* Page format */
2405
    cmd[2] = PART_PAGE;
2406
    cmd[4] = 200;
2407
 
2408
    SCpnt = st_do_scsi(SCpnt, STp, cmd, 200, ST_TIMEOUT, MAX_READY_RETRIES);
2409
    if (SCpnt == NULL)
2410
      return (-EBUSY);
2411
    SCpnt->request.rq_status = RQ_INACTIVE;  /* Mark as not busy */
2412
 
2413
    if ((STp->buffer)->last_result_fatal != 0) {
2414
#if DEBUG
2415
      if (debugging)
2416
        printk(ST_DEB_MSG "st%d: Can't read medium partition page.\n", dev);
2417
#endif
2418
      result = (-EIO);
2419
    }
2420
    else {
2421
      result = (STp->buffer)->b_data[MODE_HEADER_LENGTH + 3] + 1;
2422
#if DEBUG
2423
      if (debugging)
2424
        printk(ST_DEB_MSG "st%d: Number of partitions %d.\n", dev, result);
2425
#endif
2426
    }
2427
 
2428
    return result;
2429
}
2430
 
2431
 
2432
/* Partition the tape into two partitions if size > 0 or one partition if
2433
   size == 0 */
2434
        static int
2435
partition_tape(struct inode * inode, int size)
2436
{
2437
    int dev = TAPE_NR(inode->i_rdev), result;
2438
    int length;
2439
    Scsi_Tape *STp;
2440
    Scsi_Cmnd * SCpnt = NULL;
2441
    unsigned char cmd[10], *bp;
2442
 
2443
    if ((result = nbr_partitions(inode)) < 0)
2444
      return result;
2445
    STp = &(scsi_tapes[dev]);
2446
 
2447
    /* The mode page is in the buffer. Let's modify it and write it. */
2448
    bp = &((STp->buffer)->b_data[0]);
2449
    if (size <= 0) {
2450
      length = 8;
2451
      bp[MODE_HEADER_LENGTH + 3] = 0;
2452
#if DEBUG
2453
      if (debugging)
2454
        printk(ST_DEB_MSG "st%d: Formatting tape with one partition.\n", dev);
2455
#endif
2456
    }
2457
    else {
2458
      length = 10;
2459
      bp[MODE_HEADER_LENGTH + 3] = 1;
2460
      bp[MODE_HEADER_LENGTH + 8] = (size >> 8) & 0xff;
2461
      bp[MODE_HEADER_LENGTH + 9] = size & 0xff;
2462
#if DEBUG
2463
      if (debugging)
2464
        printk(ST_DEB_MSG "st%d: Formatting tape with two partition (1 = %d MB).\n",
2465
               dev, size);
2466
#endif
2467
    }
2468
    bp[MODE_HEADER_LENGTH + 6] = 0;
2469
    bp[MODE_HEADER_LENGTH + 7] = 0;
2470
    bp[MODE_HEADER_LENGTH + 4] = 0x30;   /* IDP | PSUM = MB */
2471
 
2472
    bp[0] = 0;
2473
    bp[1] = 0;
2474
    bp[MODE_HEADER_LENGTH] &= 0x3f;
2475
    bp[MODE_HEADER_LENGTH + 1] = length - 2;
2476
 
2477
    memset(cmd, 0, 10);
2478
    cmd[0] = MODE_SELECT;
2479
    cmd[1] = 0x10;
2480
    cmd[4] = length + MODE_HEADER_LENGTH;
2481
 
2482
    SCpnt = st_do_scsi(SCpnt, STp, cmd, cmd[4], ST_LONG_TIMEOUT, MAX_READY_RETRIES);
2483
    if (SCpnt == NULL)
2484
      return (-EBUSY);
2485
    SCpnt->request.rq_status = RQ_INACTIVE;  /* Mark as not busy */
2486
 
2487
    if ((STp->buffer)->last_result_fatal != 0) {
2488
      printk(KERN_INFO "st%d: Partitioning of tape failed.\n", dev);
2489
      result = (-EIO);
2490
    }
2491
    else
2492
      result = 0;
2493
 
2494
    return result;
2495
}
2496
 
2497
 
2498
 
2499
/* The ioctl command */
2500
        static int
2501
st_ioctl(struct inode * inode,struct file * file,
2502
         unsigned int cmd_in, unsigned long arg)
2503
{
2504
   int i, cmd_nr, cmd_type, bt;
2505
   unsigned int blk;
2506
   struct mtop mtc;
2507
   struct mtpos mt_pos;
2508
   Scsi_Tape *STp;
2509
   ST_mode *STm;
2510
   ST_partstat *STps;
2511
   int dev = TAPE_NR(inode->i_rdev);
2512
 
2513
   STp = &(scsi_tapes[dev]);
2514
#if DEBUG
2515
   if (debugging && !STp->in_use) {
2516
     printk(ST_DEB_MSG "st%d: Incorrect device.\n", dev);
2517
     return (-EIO);
2518
   }
2519
#endif
2520
   STm = &(STp->modes[STp->current_mode]);
2521
   STps = &(STp->ps[STp->partition]);
2522
 
2523
   cmd_type = _IOC_TYPE(cmd_in);
2524
   cmd_nr   = _IOC_NR(cmd_in);
2525
 
2526
   if (cmd_type == _IOC_TYPE(MTIOCTOP) && cmd_nr == _IOC_NR(MTIOCTOP)) {
2527
     if (_IOC_SIZE(cmd_in) != sizeof(mtc))
2528
       return (-EINVAL);
2529
 
2530
     i = verify_area(VERIFY_READ, (void *)arg, sizeof(mtc));
2531
     if (i)
2532
        return i;
2533
 
2534
     memcpy_fromfs((char *) &mtc, (char *)arg, sizeof(struct mtop));
2535
 
2536
     if (mtc.mt_op == MTSETDRVBUFFER && !suser()) {
2537
       printk(KERN_WARNING "st%d: MTSETDRVBUFFER only allowed for root.\n", dev);
2538
       return (-EPERM);
2539
     }
2540
     if (!STm->defined &&
2541
         (mtc.mt_op != MTSETDRVBUFFER && (mtc.mt_count & MT_ST_OPTIONS) == 0))
2542
       return (-ENXIO);
2543
 
2544
     if (!(STp->device)->was_reset) {
2545
 
2546
       if (STp->eof_hit) {
2547
         if (mtc.mt_op == MTFSF || mtc.mt_op == MTFSFM|| mtc.mt_op == MTEOM) {
2548
           mtc.mt_count -= 1;
2549
           if ((STp->mt_status)->mt_fileno >= 0)
2550
             (STp->mt_status)->mt_fileno += 1;
2551
         }
2552
         else if (mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM) {
2553
           mtc.mt_count += 1;
2554
           if ((STp->mt_status)->mt_fileno >= 0)
2555
             (STp->mt_status)->mt_fileno += 1;
2556
         }
2557
       }
2558
 
2559
       i = flush_buffer(inode, file, /* mtc.mt_op == MTSEEK || */
2560
                        mtc.mt_op == MTREW || mtc.mt_op == MTOFFL ||
2561
                        mtc.mt_op == MTRETEN || mtc.mt_op == MTEOM ||
2562
                        mtc.mt_op == MTLOCK || mtc.mt_op == MTLOAD ||
2563
                        mtc.mt_op == MTCOMPRESSION);
2564
       if (i < 0)
2565
         return i;
2566
     }
2567
     else {
2568
       /*
2569
        * If there was a bus reset, block further access
2570
        * to this device.  If the user wants to rewind the tape,
2571
        * then reset the flag and allow access again.
2572
        */
2573
       if(mtc.mt_op != MTREW &&
2574
          mtc.mt_op != MTOFFL &&
2575
          mtc.mt_op != MTRETEN &&
2576
          mtc.mt_op != MTERASE &&
2577
          mtc.mt_op != MTSEEK &&
2578
          mtc.mt_op != MTEOM)
2579
         return (-EIO);
2580
       STp->device->was_reset = 0;
2581
       if (STp->door_locked != ST_UNLOCKED &&
2582
           STp->door_locked != ST_LOCK_FAILS) {
2583
         if (st_int_ioctl(inode, MTLOCK, 0)) {
2584
           printk(KERN_NOTICE "st%d: Could not relock door after bus reset.\n",
2585
                  dev);
2586
           STp->door_locked = ST_UNLOCKED;
2587
         }
2588
       }
2589
     }
2590
 
2591
     if (mtc.mt_op != MTNOP && mtc.mt_op != MTSETBLK &&
2592
         mtc.mt_op != MTSETDENSITY && mtc.mt_op != MTWSM &&
2593
         mtc.mt_op != MTSETDRVBUFFER && mtc.mt_op != MTSEEK &&
2594
         mtc.mt_op != MTSETPART)
2595
       STps->rw = ST_IDLE;  /* Prevent automatic WEOF */
2596
 
2597
     if (mtc.mt_op == MTOFFL && STp->door_locked != ST_UNLOCKED)
2598
       st_int_ioctl(inode, MTUNLOCK, 0);  /* Ignore result! */
2599
 
2600
     if (mtc.mt_op == MTSETDRVBUFFER &&
2601
         (mtc.mt_count & MT_ST_OPTIONS) != 0)
2602
       return st_set_options(inode, mtc.mt_count);
2603
     if (mtc.mt_op == MTSETPART) {
2604
       if (!STp->can_partitions ||
2605
           mtc.mt_count < 0 || mtc.mt_count >= ST_NBR_PARTITIONS)
2606
         return (-EINVAL);
2607
       if (mtc.mt_count >= STp->nbr_partitions &&
2608
           (STp->nbr_partitions = nbr_partitions(inode)) < 0)
2609
         return (-EIO);
2610
       if (mtc.mt_count >= STp->nbr_partitions)
2611
         return (-EINVAL);
2612
       STp->new_partition = mtc.mt_count;
2613
       return 0;
2614
     }
2615
     if (mtc.mt_op == MTMKPART) {
2616
       if (!STp->can_partitions)
2617
         return (-EINVAL);
2618
       if ((i = st_int_ioctl(inode, MTREW, 0)) < 0 ||
2619
           (i = partition_tape(inode, mtc.mt_count)) < 0)
2620
         return i;
2621
       for (i=0; i < ST_NBR_PARTITIONS; i++) {
2622
         STp->ps[i].rw = ST_IDLE;
2623
         STp->ps[i].moves_after_eof = 1;
2624
         STp->ps[i].at_sm = 0;
2625
         STp->ps[i].last_block_valid = FALSE;
2626
       }
2627
       STp->partition = STp->new_partition = 0;
2628
       STp->nbr_partitions = 1;  /* Bad guess ?-) */
2629
       STp->drv_block = (STp->mt_status)->mt_fileno = 0;
2630
       return 0;
2631
     }
2632
     if (mtc.mt_op == MTSEEK) {
2633
       i = set_location(inode, mtc.mt_count, STp->new_partition, 0);
2634
       if (!STp->can_partitions)
2635
           STp->ps[0].rw = ST_IDLE;
2636
       return i;
2637
     }
2638
     if (STp->can_partitions && STp->ready == ST_READY &&
2639
         (i = update_partition(inode)) < 0)
2640
       return i;
2641
     if (mtc.mt_op == MTCOMPRESSION)
2642
       return st_compression(STp, (mtc.mt_count & 1));
2643
     else
2644
       return st_int_ioctl(inode, mtc.mt_op, mtc.mt_count);
2645
   }
2646
 
2647
   if (!STm->defined)
2648
     return (-ENXIO);
2649
 
2650
   if ((i = flush_buffer(inode, file, FALSE)) < 0)
2651
     return i;
2652
   if (STp->can_partitions &&
2653
       (i = update_partition(inode)) < 0)
2654
     return i;
2655
 
2656
   if (cmd_type == _IOC_TYPE(MTIOCGET) && cmd_nr == _IOC_NR(MTIOCGET)) {
2657
 
2658
     if (_IOC_SIZE(cmd_in) != sizeof(struct mtget))
2659
       return (-EINVAL);
2660
     i = verify_area(VERIFY_WRITE, (void *)arg, sizeof(struct mtget));
2661
     if (i)
2662
       return i;
2663
 
2664
     (STp->mt_status)->mt_dsreg =
2665
       ((STp->block_size << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK) |
2666
       ((STp->density << MT_ST_DENSITY_SHIFT) & MT_ST_DENSITY_MASK);
2667
     (STp->mt_status)->mt_blkno = STp->drv_block;
2668
     if (STp->block_size != 0) {
2669
       if (STps->rw == ST_WRITING)
2670
         (STp->mt_status)->mt_blkno +=
2671
           (STp->buffer)->buffer_bytes / STp->block_size;
2672
       else if (STps->rw == ST_READING)
2673
         (STp->mt_status)->mt_blkno -= ((STp->buffer)->buffer_bytes +
2674
           STp->block_size - 1) / STp->block_size;
2675
     }
2676
 
2677
     (STp->mt_status)->mt_gstat = 0;
2678
     if (STp->drv_write_prot)
2679
       (STp->mt_status)->mt_gstat |= GMT_WR_PROT(0xffffffff);
2680
     if ((STp->mt_status)->mt_blkno == 0) {
2681
       if ((STp->mt_status)->mt_fileno == 0)
2682
         (STp->mt_status)->mt_gstat |= GMT_BOT(0xffffffff);
2683
       else
2684
         (STp->mt_status)->mt_gstat |= GMT_EOF(0xffffffff);
2685
     }
2686
     (STp->mt_status)->mt_resid = STp->partition;
2687
     if (STp->eof == ST_EOM_OK || STp->eof == ST_EOM_ERROR)
2688
       (STp->mt_status)->mt_gstat |= GMT_EOT(0xffffffff);
2689
     else if (STp->eof == ST_EOD)
2690
       (STp->mt_status)->mt_gstat |= GMT_EOD(0xffffffff);
2691
     if (STp->density == 1)
2692
       (STp->mt_status)->mt_gstat |= GMT_D_800(0xffffffff);
2693
     else if (STp->density == 2)
2694
       (STp->mt_status)->mt_gstat |= GMT_D_1600(0xffffffff);
2695
     else if (STp->density == 3)
2696
       (STp->mt_status)->mt_gstat |= GMT_D_6250(0xffffffff);
2697
     if (STp->ready == ST_READY)
2698
       (STp->mt_status)->mt_gstat |= GMT_ONLINE(0xffffffff);
2699
     if (STp->ready == ST_NO_TAPE)
2700
       (STp->mt_status)->mt_gstat |= GMT_DR_OPEN(0xffffffff);
2701
     if (STps->at_sm)
2702
       (STp->mt_status)->mt_gstat |= GMT_SM(0xffffffff);
2703
     if (STm->do_async_writes || (STm->do_buffer_writes && STp->block_size != 0) ||
2704
         STp->drv_buffer != 0)
2705
       (STp->mt_status)->mt_gstat |= GMT_IM_REP_EN(0xffffffff);
2706
 
2707
     memcpy_tofs((char *)arg, (char *)(STp->mt_status),
2708
                 sizeof(struct mtget));
2709
 
2710
     (STp->mt_status)->mt_erreg = 0;  /* Clear after read */
2711
     return 0;
2712
   } /* End of MTIOCGET */
2713
 
2714
   if (cmd_type == _IOC_TYPE(MTIOCPOS) && cmd_nr == _IOC_NR(MTIOCPOS)) {
2715
     if (_IOC_SIZE(cmd_in) != sizeof(struct mtpos))
2716
       return (-EINVAL);
2717
     if ((i = get_location(inode, &blk, &bt, 0)) < 0)
2718
       return i;
2719
     i = verify_area(VERIFY_WRITE, (void *)arg, sizeof(mt_pos));
2720
     if (i)
2721
        return i;
2722
     mt_pos.mt_blkno = blk;
2723
     memcpy_tofs((char *)arg, (char *) (&mt_pos), sizeof(struct mtpos));
2724
     return 0;
2725
   }
2726
 
2727
   return scsi_ioctl(STp->device, cmd_in, (void *) arg);
2728
}
2729
 
2730
 
2731
/* Try to allocate a new tape buffer */
2732
        static ST_buffer *
2733
new_tape_buffer( int from_initialization, int need_dma )
2734
{
2735
  int priority, a_size;
2736
  ST_buffer *tb;
2737
 
2738
  if (st_nbr_buffers >= st_template.dev_max)
2739
    return NULL;  /* Should never happen */
2740
 
2741
  if (from_initialization) {
2742
    priority = GFP_ATOMIC;
2743
    a_size = st_buffer_size;
2744
  }
2745
  else {
2746
    priority = GFP_KERNEL;
2747
    for (a_size = PAGE_SIZE; a_size < st_buffer_size; a_size <<= 1)
2748
      ; /* Make sure we allocate efficiently */
2749
  }
2750
  tb = (ST_buffer *)scsi_init_malloc(sizeof(ST_buffer), priority);
2751
  if (tb) {
2752
    if (need_dma)
2753
      priority |= GFP_DMA;
2754
    tb->b_data = (unsigned char *)scsi_init_malloc(a_size, priority);
2755
    if (!tb->b_data) {
2756
      scsi_init_free((char *)tb, sizeof(ST_buffer));
2757
      tb = NULL;
2758
    }
2759
  }
2760
  if (!tb) {
2761
    printk(KERN_NOTICE "st: Can't allocate new tape buffer (nbr %d).\n",
2762
           st_nbr_buffers);
2763
    return NULL;
2764
  }
2765
#if DEBUG
2766
  if (debugging)
2767
    printk(ST_DEB_MSG
2768
           "st: Allocated tape buffer %d (%d bytes, dma: %d, a: %p).\n",
2769
           st_nbr_buffers, a_size, need_dma, tb->b_data);
2770
#endif
2771
  tb->in_use = 0;
2772
  tb->dma = need_dma;
2773
  tb->buffer_size = a_size;
2774
  tb->writing = 0;
2775
  tb->orig_b_data = NULL;
2776
  st_buffers[st_nbr_buffers++] = tb;
2777
  return tb;
2778
}
2779
 
2780
 
2781
/* Try to allocate a temporary enlarged tape buffer */
2782
        static int
2783
enlarge_buffer(ST_buffer *STbuffer, int new_size, int need_dma)
2784
{
2785
  int a_size, priority;
2786
  unsigned char *tbd;
2787
 
2788
  normalize_buffer(STbuffer);
2789
 
2790
  for (a_size = PAGE_SIZE; a_size < new_size; a_size <<= 1)
2791
    ;  /* Make sure that we allocate efficiently */
2792
 
2793
  priority = GFP_KERNEL;
2794
  if (need_dma)
2795
    priority |= GFP_DMA;
2796
  tbd = (unsigned char *)scsi_init_malloc(a_size, priority);
2797
  if (!tbd)
2798
    return FALSE;
2799
#if DEBUG
2800
  if (debugging)
2801
    printk(ST_DEB_MSG
2802
           "st: Buffer at %p enlarged to %d bytes (dma: %d, a: %p).\n",
2803
           STbuffer->b_data, a_size, need_dma, tbd);
2804
#endif
2805
 
2806
  STbuffer->orig_b_data = STbuffer->b_data;
2807
  STbuffer->orig_size = STbuffer->buffer_size;
2808
  STbuffer->b_data = tbd;
2809
  STbuffer->buffer_size = a_size;
2810
  return TRUE;
2811
}
2812
 
2813
 
2814
/* Release the extra buffer */
2815
        static void
2816
normalize_buffer(ST_buffer *STbuffer)
2817
{
2818
  if (STbuffer->orig_b_data == NULL)
2819
    return;
2820
 
2821
  scsi_init_free(STbuffer->b_data, STbuffer->buffer_size);
2822
  STbuffer->b_data = STbuffer->orig_b_data;
2823
  STbuffer->orig_b_data = NULL;
2824
  STbuffer->buffer_size = STbuffer->orig_size;
2825
 
2826
#if DEBUG
2827
  if (debugging)
2828
    printk(ST_DEB_MSG "st: Buffer at %p normalized to %d bytes.\n",
2829
           STbuffer->b_data, STbuffer->buffer_size);
2830
#endif
2831
}
2832
 
2833
 
2834
/* Set the boot options. Syntax: st=xxx,yyy
2835
   where xxx is buffer size in 1024 byte blocks and yyy is write threshold
2836
   in 1024 byte blocks. */
2837
        void
2838
st_setup(char *str, int *ints)
2839
{
2840
  if (ints[0] > 0 && ints[1] > 0)
2841
    st_buffer_size = ints[1] * ST_BLOCK_SIZE;
2842
  if (ints[0] > 1 && ints[2] > 0) {
2843
    st_write_threshold = ints[2] * ST_BLOCK_SIZE;
2844
    if (st_write_threshold > st_buffer_size)
2845
      st_write_threshold = st_buffer_size;
2846
  }
2847
  if (ints[0] > 2 && ints[3] > 0)
2848
    st_max_buffers = ints[3];
2849
}
2850
 
2851
 
2852
static struct file_operations st_fops = {
2853
   NULL,            /* lseek - default */
2854
   st_read,         /* read - general block-dev read */
2855
   st_write,        /* write - general block-dev write */
2856
   NULL,            /* readdir - bad */
2857
   NULL,            /* select */
2858
   st_ioctl,        /* ioctl */
2859
   NULL,            /* mmap */
2860
   scsi_tape_open,  /* open */
2861
   scsi_tape_close, /* release */
2862
   NULL             /* fsync */
2863
};
2864
 
2865
static int st_attach(Scsi_Device * SDp){
2866
   Scsi_Tape * tpnt;
2867
   ST_mode * STm;
2868
   ST_partstat * STps;
2869
   int i;
2870
 
2871
   if(SDp->type != TYPE_TAPE) return 1;
2872
 
2873
   if(st_template.nr_dev >= st_template.dev_max)
2874
     {
2875
        SDp->attached--;
2876
        return 1;
2877
     }
2878
 
2879
   for(tpnt = scsi_tapes, i=0; i<st_template.dev_max; i++, tpnt++)
2880
     if(!tpnt->device) break;
2881
 
2882
   if(i >= st_template.dev_max) panic ("scsi_devices corrupt (st)");
2883
 
2884
   scsi_tapes[i].device = SDp;
2885
   if (SDp->scsi_level <= 2)
2886
     scsi_tapes[i].mt_status->mt_type = MT_ISSCSI1;
2887
   else
2888
     scsi_tapes[i].mt_status->mt_type = MT_ISSCSI2;
2889
 
2890
   tpnt->devt = MKDEV(SCSI_TAPE_MAJOR, i);
2891
   tpnt->dirty = 0;
2892
   tpnt->eof = ST_NOEOF;
2893
   tpnt->waiting = NULL;
2894
   tpnt->in_use = 0;
2895
   tpnt->drv_buffer = 1;  /* Try buffering if no mode sense */
2896
   tpnt->restr_dma = (SDp->host)->unchecked_isa_dma;
2897
   tpnt->density = 0;
2898
   tpnt->do_auto_lock = ST_AUTO_LOCK;
2899
   tpnt->can_bsr = ST_IN_FILE_POS;
2900
   tpnt->can_partitions = 0;
2901
   tpnt->two_fm = ST_TWO_FM;
2902
   tpnt->fast_mteom = ST_FAST_MTEOM;
2903
   tpnt->scsi2_logical = 0;
2904
   tpnt->write_threshold = st_write_threshold;
2905
   tpnt->default_drvbuffer = 0xff; /* No forced buffering */
2906
   tpnt->partition = 0;
2907
   tpnt->new_partition = 0;
2908
   tpnt->nbr_partitions = 0;
2909
   tpnt->drv_block = (-1);
2910
   (tpnt->mt_status)->mt_fileno = (tpnt->mt_status)->mt_blkno = (-1);
2911
 
2912
   for (i=0; i < ST_NBR_MODES; i++) {
2913
     STm = &(tpnt->modes[i]);
2914
     STm->defined = FALSE;
2915
     STm->defaults_for_writes = 0;
2916
     STm->do_async_writes = ST_ASYNC_WRITES;
2917
     STm->do_buffer_writes = ST_BUFFER_WRITES;
2918
     STm->do_read_ahead = ST_READ_AHEAD;
2919
     STm->default_compression = ST_DONT_TOUCH;
2920
     STm->default_blksize = (-1);  /* No forced size */
2921
     STm->default_density = (-1);  /* No forced density */
2922
   }
2923
 
2924
   for (i=0; i < ST_NBR_PARTITIONS; i++) {
2925
     STps = &(tpnt->ps[i]);
2926
     STps->rw = ST_IDLE;
2927
     STps->moves_after_eof = 1;
2928
     STps->at_sm = 0;
2929
     STps->last_block_valid = FALSE;
2930
   }
2931
 
2932
   tpnt->current_mode = 0;
2933
   tpnt->modes[0].defined = TRUE;
2934
 
2935
   tpnt->density_changed = tpnt->compression_changed =
2936
     tpnt->blksize_changed = FALSE;
2937
 
2938
   st_template.nr_dev++;
2939
   return 0;
2940
};
2941
 
2942
static int st_detect(Scsi_Device * SDp)
2943
{
2944
  if(SDp->type != TYPE_TAPE) return 0;
2945
 
2946
  printk(KERN_INFO
2947
         "Detected scsi tape st%d at scsi%d, channel %d, id %d, lun %d\n",
2948
         st_template.dev_noticed++,
2949
         SDp->host->host_no, SDp->channel, SDp->id, SDp->lun);
2950
 
2951
  return 1;
2952
}
2953
 
2954
static int st_registered = 0;
2955
 
2956
/* Driver initialization */
2957
static int st_init()
2958
{
2959
  int i;
2960
  Scsi_Tape * STp;
2961
#if !ST_RUNTIME_BUFFERS
2962
  int target_nbr;
2963
#endif
2964
 
2965
  if (st_template.dev_noticed == 0) return 0;
2966
 
2967
  if(!st_registered) {
2968
    if (register_chrdev(SCSI_TAPE_MAJOR,"st",&st_fops)) {
2969
      printk(KERN_ERR "Unable to get major %d for SCSI tapes\n",MAJOR_NR);
2970
      return 1;
2971
    }
2972
    st_registered++;
2973
  }
2974
 
2975
  if (scsi_tapes) return 0;
2976
  st_template.dev_max = st_template.dev_noticed + ST_EXTRA_DEVS;
2977
  if (st_template.dev_max < ST_MAX_TAPES)
2978
    st_template.dev_max = ST_MAX_TAPES;
2979
  if (st_template.dev_max > 128 / ST_NBR_MODES)
2980
    printk(KERN_INFO "st: Only %d tapes accessible.\n", 128 / ST_NBR_MODES);
2981
  scsi_tapes =
2982
    (Scsi_Tape *) scsi_init_malloc(st_template.dev_max * sizeof(Scsi_Tape),
2983
                                   GFP_ATOMIC);
2984
  if (scsi_tapes == NULL) {
2985
    printk(KERN_ERR "Unable to allocate descriptors for SCSI tapes.\n");
2986
    unregister_chrdev(SCSI_TAPE_MAJOR, "st");
2987
    return 1;
2988
  }
2989
 
2990
#if DEBUG
2991
  printk(ST_DEB_MSG "st: Buffer size %d bytes, write threshold %d bytes.\n",
2992
         st_buffer_size, st_write_threshold);
2993
#endif
2994
 
2995
  memset(scsi_tapes, 0, st_template.dev_max * sizeof(Scsi_Tape));
2996
  for (i=0; i < st_template.dev_max; ++i) {
2997
    STp = &(scsi_tapes[i]);
2998
    STp->capacity = 0xfffff;
2999
    STp->mt_status = (struct mtget *) scsi_init_malloc(sizeof(struct mtget),
3000
                                                       GFP_ATOMIC);
3001
    /* Initialize status */
3002
    memset((void *) scsi_tapes[i].mt_status, 0, sizeof(struct mtget));
3003
  }
3004
 
3005
  /* Allocate the buffers */
3006
  st_buffers =
3007
    (ST_buffer **) scsi_init_malloc(st_template.dev_max * sizeof(ST_buffer *),
3008
                                    GFP_ATOMIC);
3009
  if (st_buffers == NULL) {
3010
    printk(KERN_ERR "Unable to allocate tape buffer pointers.\n");
3011
    unregister_chrdev(SCSI_TAPE_MAJOR, "st");
3012
    scsi_init_free((char *) scsi_tapes,
3013
                   st_template.dev_max * sizeof(Scsi_Tape));
3014
    return 1;
3015
  }
3016
 
3017
#if ST_RUNTIME_BUFFERS
3018
  st_nbr_buffers = 0;
3019
#else
3020
  target_nbr = st_template.dev_noticed;
3021
  if (target_nbr < ST_EXTRA_DEVS)
3022
    target_nbr = ST_EXTRA_DEVS;
3023
  if (target_nbr > st_max_buffers)
3024
    target_nbr = st_max_buffers;
3025
 
3026
  for (i=st_nbr_buffers=0; i < target_nbr; i++) {
3027
    if (!new_tape_buffer(TRUE, TRUE)) {
3028
      if (i == 0) {
3029
#if 0
3030
        printk(KERN_ERR "Can't continue without at least one tape buffer.\n");
3031
        unregister_chrdev(SCSI_TAPE_MAJOR, "st");
3032
        scsi_init_free((char *) st_buffers,
3033
                       st_template.dev_max * sizeof(ST_buffer *));
3034
        scsi_init_free((char *) scsi_tapes,
3035
                       st_template.dev_max * sizeof(Scsi_Tape));
3036
        return 1;
3037
#else
3038
        printk(KERN_INFO "No tape buffers allocated at initialization.\n");
3039
        break;
3040
#endif
3041
      }
3042
      printk(KERN_INFO "Number of tape buffers adjusted.\n");
3043
      break;
3044
    }
3045
  }
3046
#endif
3047
  return 0;
3048
}
3049
 
3050
static void st_detach(Scsi_Device * SDp)
3051
{
3052
  Scsi_Tape * tpnt;
3053
  int i;
3054
 
3055
  for(tpnt = scsi_tapes, i=0; i<st_template.dev_max; i++, tpnt++)
3056
    if(tpnt->device == SDp) {
3057
      tpnt->device = NULL;
3058
      SDp->attached--;
3059
      st_template.nr_dev--;
3060
      st_template.dev_noticed--;
3061
      return;
3062
    }
3063
  return;
3064
}
3065
 
3066
 
3067
#ifdef MODULE
3068
 
3069
int init_module(void) {
3070
  st_template.usage_count = &mod_use_count_;
3071
  return scsi_register_module(MODULE_SCSI_DEV, &st_template);
3072
}
3073
 
3074
void cleanup_module( void)
3075
{
3076
  int i;
3077
 
3078
  scsi_unregister_module(MODULE_SCSI_DEV, &st_template);
3079
  unregister_chrdev(SCSI_TAPE_MAJOR, "st");
3080
  st_registered--;
3081
  if(scsi_tapes != NULL) {
3082
    scsi_init_free((char *) scsi_tapes,
3083
                   st_template.dev_max * sizeof(Scsi_Tape));
3084
 
3085
    if (st_buffers != NULL) {
3086
      for (i=0; i < st_nbr_buffers; i++)
3087
        if (st_buffers[i] != NULL) {
3088
          scsi_init_free((char *) st_buffers[i]->b_data,
3089
                         st_buffers[i]->buffer_size);
3090
          scsi_init_free((char *) st_buffers[i], sizeof(ST_buffer));
3091
        }
3092
 
3093
      scsi_init_free((char *) st_buffers,
3094
                     st_template.dev_max * sizeof(ST_buffer *));
3095
    }
3096
  }
3097
  st_template.dev_max = 0;
3098
  printk(KERN_INFO "st: Unloaded.\n");
3099
}
3100
#endif /* MODULE */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.