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