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