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