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