1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#define DRV_NAME "ide-tape"
19
20#define IDETAPE_VERSION "1.20"
21
22#include <linux/module.h>
23#include <linux/types.h>
24#include <linux/string.h>
25#include <linux/kernel.h>
26#include <linux/delay.h>
27#include <linux/timer.h>
28#include <linux/mm.h>
29#include <linux/interrupt.h>
30#include <linux/jiffies.h>
31#include <linux/major.h>
32#include <linux/errno.h>
33#include <linux/genhd.h>
34#include <linux/seq_file.h>
35#include <linux/slab.h>
36#include <linux/pci.h>
37#include <linux/ide.h>
38#include <linux/completion.h>
39#include <linux/bitops.h>
40#include <linux/mutex.h>
41#include <scsi/scsi.h>
42
43#include <asm/byteorder.h>
44#include <linux/uaccess.h>
45#include <linux/io.h>
46#include <asm/unaligned.h>
47#include <linux/mtio.h>
48
49
50#undef IDETAPE_DEBUG_LOG
51
52#ifdef IDETAPE_DEBUG_LOG
53#define ide_debug_log(lvl, fmt, args...) __ide_debug_log(lvl, fmt, ## args)
54#else
55#define ide_debug_log(lvl, fmt, args...) do {} while (0)
56#endif
57
58
59
60
61
62
63
64
65#define IDETAPE_MAX_PC_RETRIES 3
66
67
68
69
70
71
72
73#define IDETAPE_FIFO_THRESHOLD 2
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101#define IDETAPE_DSC_RW_MIN 5*HZ/100
102#define IDETAPE_DSC_RW_MAX 40*HZ/100
103#define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ
104#define IDETAPE_DSC_MA_FAST 2*HZ
105#define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ
106#define IDETAPE_DSC_MA_SLOW 30*HZ
107#define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ
108
109
110
111
112enum {
113 IDETAPE_DIR_NONE = (1 << 0),
114 IDETAPE_DIR_READ = (1 << 1),
115 IDETAPE_DIR_WRITE = (1 << 2),
116};
117
118
119#define DOOR_UNLOCKED 0
120#define DOOR_LOCKED 1
121#define DOOR_EXPLICITLY_LOCKED 2
122
123
124#define IDETAPE_SPACE_OVER_FILEMARK 1
125#define IDETAPE_SPACE_TO_EOD 3
126
127
128#define IDETAPE_LU_LOAD_MASK 1
129#define IDETAPE_LU_RETENSION_MASK 2
130#define IDETAPE_LU_EOT_MASK 4
131
132
133#define IDETAPE_BLOCK_DESCRIPTOR 0
134#define IDETAPE_CAPABILITIES_PAGE 0x2a
135
136
137
138
139
140typedef struct ide_tape_obj {
141 ide_drive_t *drive;
142 struct ide_driver *driver;
143 struct gendisk *disk;
144 struct device dev;
145
146
147 struct ide_atapi_pc queued_pc;
148
149
150
151
152
153
154
155
156
157 bool postponed_rq;
158
159
160 unsigned long dsc_polling_start;
161
162 struct timer_list dsc_timer;
163
164 unsigned long best_dsc_rw_freq;
165 unsigned long dsc_poll_freq;
166 unsigned long dsc_timeout;
167
168
169 u8 partition;
170
171 unsigned int first_frame;
172
173
174 u8 sense_key, asc, ascq;
175
176
177 unsigned int minor;
178
179 char name[4];
180
181 u8 chrdev_dir;
182
183
184 unsigned short blk_size;
185 int user_bs_factor;
186
187
188 u8 caps[20];
189
190
191
192
193
194
195
196
197
198
199 int buffer_size;
200
201 void *buf;
202
203 void *cur;
204
205 size_t valid;
206
207
208 unsigned long avg_time;
209 int avg_size;
210 int avg_speed;
211
212
213 int door_locked;
214
215 char drv_write_prot;
216
217 char write_prot;
218} idetape_tape_t;
219
220static DEFINE_MUTEX(ide_tape_mutex);
221static DEFINE_MUTEX(idetape_ref_mutex);
222
223static DEFINE_MUTEX(idetape_chrdev_mutex);
224
225static struct class *idetape_sysfs_class;
226
227static void ide_tape_release(struct device *);
228
229static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
230
231static struct ide_tape_obj *ide_tape_get(struct gendisk *disk, bool cdev,
232 unsigned int i)
233{
234 struct ide_tape_obj *tape = NULL;
235
236 mutex_lock(&idetape_ref_mutex);
237
238 if (cdev)
239 tape = idetape_devs[i];
240 else
241 tape = ide_drv_g(disk, ide_tape_obj);
242
243 if (tape) {
244 if (ide_device_get(tape->drive))
245 tape = NULL;
246 else
247 get_device(&tape->dev);
248 }
249
250 mutex_unlock(&idetape_ref_mutex);
251 return tape;
252}
253
254static void ide_tape_put(struct ide_tape_obj *tape)
255{
256 ide_drive_t *drive = tape->drive;
257
258 mutex_lock(&idetape_ref_mutex);
259 put_device(&tape->dev);
260 ide_device_put(drive);
261 mutex_unlock(&idetape_ref_mutex);
262}
263
264
265
266
267
268static void idetape_analyze_error(ide_drive_t *drive)
269{
270 idetape_tape_t *tape = drive->driver_data;
271 struct ide_atapi_pc *pc = drive->failed_pc;
272 struct request *rq = drive->hwif->rq;
273 u8 *sense = bio_data(rq->bio);
274
275 tape->sense_key = sense[2] & 0xF;
276 tape->asc = sense[12];
277 tape->ascq = sense[13];
278
279 ide_debug_log(IDE_DBG_FUNC,
280 "cmd: 0x%x, sense key = %x, asc = %x, ascq = %x",
281 rq->cmd[0], tape->sense_key, tape->asc, tape->ascq);
282
283
284 if (pc->flags & PC_FLAG_DMA_ERROR)
285 scsi_req(rq)->resid_len = tape->blk_size * get_unaligned_be32(&sense[3]);
286
287
288
289
290
291
292 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
293
294 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
295 if (tape->sense_key == 5) {
296
297 pc->error = 0;
298
299 pc->flags |= PC_FLAG_ABORT;
300 }
301 }
302 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
303 pc->error = IDE_DRV_ERROR_FILEMARK;
304 pc->flags |= PC_FLAG_ABORT;
305 }
306 if (pc->c[0] == WRITE_6) {
307 if ((sense[2] & 0x40) || (tape->sense_key == 0xd
308 && tape->asc == 0x0 && tape->ascq == 0x2)) {
309 pc->error = IDE_DRV_ERROR_EOD;
310 pc->flags |= PC_FLAG_ABORT;
311 }
312 }
313 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
314 if (tape->sense_key == 8) {
315 pc->error = IDE_DRV_ERROR_EOD;
316 pc->flags |= PC_FLAG_ABORT;
317 }
318 if (!(pc->flags & PC_FLAG_ABORT) &&
319 (blk_rq_bytes(rq) - scsi_req(rq)->resid_len))
320 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
321 }
322}
323
324static void ide_tape_handle_dsc(ide_drive_t *);
325
326static int ide_tape_callback(ide_drive_t *drive, int dsc)
327{
328 idetape_tape_t *tape = drive->driver_data;
329 struct ide_atapi_pc *pc = drive->pc;
330 struct request *rq = drive->hwif->rq;
331 int uptodate = pc->error ? 0 : 1;
332 int err = uptodate ? 0 : IDE_DRV_ERROR_GENERAL;
333
334 ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x, dsc: %d, err: %d", rq->cmd[0],
335 dsc, err);
336
337 if (dsc)
338 ide_tape_handle_dsc(drive);
339
340 if (drive->failed_pc == pc)
341 drive->failed_pc = NULL;
342
343 if (pc->c[0] == REQUEST_SENSE) {
344 if (uptodate)
345 idetape_analyze_error(drive);
346 else
347 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE "
348 "itself - Aborting request!\n");
349 } else if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
350 unsigned int blocks =
351 (blk_rq_bytes(rq) - scsi_req(rq)->resid_len) / tape->blk_size;
352
353 tape->avg_size += blocks * tape->blk_size;
354
355 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
356 tape->avg_speed = tape->avg_size * HZ /
357 (jiffies - tape->avg_time) / 1024;
358 tape->avg_size = 0;
359 tape->avg_time = jiffies;
360 }
361
362 tape->first_frame += blocks;
363
364 if (pc->error) {
365 uptodate = 0;
366 err = pc->error;
367 }
368 }
369 scsi_req(rq)->result = err;
370
371 return uptodate;
372}
373
374
375
376
377
378static void ide_tape_stall_queue(ide_drive_t *drive)
379{
380 idetape_tape_t *tape = drive->driver_data;
381
382 ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x, dsc_poll_freq: %lu",
383 drive->hwif->rq->cmd[0], tape->dsc_poll_freq);
384
385 tape->postponed_rq = true;
386
387 ide_stall_queue(drive, tape->dsc_poll_freq);
388}
389
390static void ide_tape_handle_dsc(ide_drive_t *drive)
391{
392 idetape_tape_t *tape = drive->driver_data;
393
394
395 tape->dsc_polling_start = jiffies;
396 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
397 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
398
399 ide_tape_stall_queue(drive);
400}
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439static ide_startstop_t ide_tape_issue_pc(ide_drive_t *drive,
440 struct ide_cmd *cmd,
441 struct ide_atapi_pc *pc)
442{
443 idetape_tape_t *tape = drive->driver_data;
444 struct request *rq = drive->hwif->rq;
445
446 if (drive->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
447 drive->failed_pc = pc;
448
449
450 drive->pc = pc;
451
452 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
453 (pc->flags & PC_FLAG_ABORT)) {
454
455
456
457
458
459
460 if (!(pc->flags & PC_FLAG_ABORT)) {
461 if (!(pc->c[0] == TEST_UNIT_READY &&
462 tape->sense_key == 2 && tape->asc == 4 &&
463 (tape->ascq == 1 || tape->ascq == 8))) {
464 printk(KERN_ERR "ide-tape: %s: I/O error, "
465 "pc = %2x, key = %2x, "
466 "asc = %2x, ascq = %2x\n",
467 tape->name, pc->c[0],
468 tape->sense_key, tape->asc,
469 tape->ascq);
470 }
471
472 pc->error = IDE_DRV_ERROR_GENERAL;
473 }
474
475 drive->failed_pc = NULL;
476 drive->pc_callback(drive, 0);
477 ide_complete_rq(drive, BLK_STS_IOERR, blk_rq_bytes(rq));
478 return ide_stopped;
479 }
480 ide_debug_log(IDE_DBG_SENSE, "retry #%d, cmd: 0x%02x", pc->retries,
481 pc->c[0]);
482
483 pc->retries++;
484
485 return ide_issue_pc(drive, cmd);
486}
487
488
489static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
490{
491 ide_init_pc(pc);
492 pc->c[0] = MODE_SENSE;
493 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
494
495 pc->c[1] = 8;
496 pc->c[2] = page_code;
497
498
499
500
501
502
503
504 pc->c[3] = 0;
505
506 pc->c[4] = 255;
507 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
508 pc->req_xfer = 12;
509 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
510 pc->req_xfer = 24;
511 else
512 pc->req_xfer = 50;
513}
514
515static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
516{
517 ide_hwif_t *hwif = drive->hwif;
518 idetape_tape_t *tape = drive->driver_data;
519 struct ide_atapi_pc *pc = drive->pc;
520 u8 stat;
521
522 stat = hwif->tp_ops->read_status(hwif);
523
524 if (stat & ATA_DSC) {
525 if (stat & ATA_ERR) {
526
527 if (pc->c[0] != TEST_UNIT_READY)
528 printk(KERN_ERR "ide-tape: %s: I/O error, ",
529 tape->name);
530
531 ide_retry_pc(drive);
532 return ide_stopped;
533 }
534 pc->error = 0;
535 } else {
536 pc->error = IDE_DRV_ERROR_GENERAL;
537 drive->failed_pc = NULL;
538 }
539 drive->pc_callback(drive, 0);
540 return ide_stopped;
541}
542
543static void ide_tape_create_rw_cmd(idetape_tape_t *tape,
544 struct ide_atapi_pc *pc, struct request *rq,
545 u8 opcode)
546{
547 unsigned int length = blk_rq_sectors(rq) / (tape->blk_size >> 9);
548
549 ide_init_pc(pc);
550 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
551 pc->c[1] = 1;
552
553 if (blk_rq_bytes(rq) == tape->buffer_size)
554 pc->flags |= PC_FLAG_DMA_OK;
555
556 if (opcode == READ_6)
557 pc->c[0] = READ_6;
558 else if (opcode == WRITE_6) {
559 pc->c[0] = WRITE_6;
560 pc->flags |= PC_FLAG_WRITING;
561 }
562
563 memcpy(scsi_req(rq)->cmd, pc->c, 12);
564}
565
566static ide_startstop_t idetape_do_request(ide_drive_t *drive,
567 struct request *rq, sector_t block)
568{
569 ide_hwif_t *hwif = drive->hwif;
570 idetape_tape_t *tape = drive->driver_data;
571 struct ide_atapi_pc *pc = NULL;
572 struct ide_cmd cmd;
573 struct scsi_request *req = scsi_req(rq);
574 u8 stat;
575
576 ide_debug_log(IDE_DBG_RQ, "cmd: 0x%x, sector: %llu, nr_sectors: %u",
577 req->cmd[0], (unsigned long long)blk_rq_pos(rq),
578 blk_rq_sectors(rq));
579
580 BUG_ON(!blk_rq_is_private(rq));
581 BUG_ON(ide_req(rq)->type != ATA_PRIV_MISC &&
582 ide_req(rq)->type != ATA_PRIV_SENSE);
583
584
585 if (drive->failed_pc && drive->pc->c[0] == REQUEST_SENSE) {
586 pc = drive->failed_pc;
587 goto out;
588 }
589
590
591
592
593
594 stat = hwif->tp_ops->read_status(hwif);
595
596 if ((drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) == 0 &&
597 (req->cmd[13] & REQ_IDETAPE_PC2) == 0)
598 drive->atapi_flags |= IDE_AFLAG_IGNORE_DSC;
599
600 if (drive->dev_flags & IDE_DFLAG_POST_RESET) {
601 drive->atapi_flags |= IDE_AFLAG_IGNORE_DSC;
602 drive->dev_flags &= ~IDE_DFLAG_POST_RESET;
603 }
604
605 if (!(drive->atapi_flags & IDE_AFLAG_IGNORE_DSC) &&
606 !(stat & ATA_DSC)) {
607 if (!tape->postponed_rq) {
608 tape->dsc_polling_start = jiffies;
609 tape->dsc_poll_freq = tape->best_dsc_rw_freq;
610 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
611 } else if (time_after(jiffies, tape->dsc_timeout)) {
612 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
613 tape->name);
614 if (req->cmd[13] & REQ_IDETAPE_PC2) {
615 idetape_media_access_finished(drive);
616 return ide_stopped;
617 } else {
618 return ide_do_reset(drive);
619 }
620 } else if (time_after(jiffies,
621 tape->dsc_polling_start +
622 IDETAPE_DSC_MA_THRESHOLD))
623 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
624 ide_tape_stall_queue(drive);
625 return ide_stopped;
626 } else {
627 drive->atapi_flags &= ~IDE_AFLAG_IGNORE_DSC;
628 tape->postponed_rq = false;
629 }
630
631 if (req->cmd[13] & REQ_IDETAPE_READ) {
632 pc = &tape->queued_pc;
633 ide_tape_create_rw_cmd(tape, pc, rq, READ_6);
634 goto out;
635 }
636 if (req->cmd[13] & REQ_IDETAPE_WRITE) {
637 pc = &tape->queued_pc;
638 ide_tape_create_rw_cmd(tape, pc, rq, WRITE_6);
639 goto out;
640 }
641 if (req->cmd[13] & REQ_IDETAPE_PC1) {
642 pc = (struct ide_atapi_pc *)ide_req(rq)->special;
643 req->cmd[13] &= ~(REQ_IDETAPE_PC1);
644 req->cmd[13] |= REQ_IDETAPE_PC2;
645 goto out;
646 }
647 if (req->cmd[13] & REQ_IDETAPE_PC2) {
648 idetape_media_access_finished(drive);
649 return ide_stopped;
650 }
651 BUG();
652
653out:
654
655 ide_prep_sense(drive, rq);
656
657 memset(&cmd, 0, sizeof(cmd));
658
659 if (rq_data_dir(rq))
660 cmd.tf_flags |= IDE_TFLAG_WRITE;
661
662 cmd.rq = rq;
663
664 ide_init_sg_cmd(&cmd, blk_rq_bytes(rq));
665 ide_map_sg(drive, &cmd);
666
667 return ide_tape_issue_pc(drive, &cmd, pc);
668}
669
670
671
672
673
674static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
675 struct ide_atapi_pc *pc, int write_filemark)
676{
677 ide_init_pc(pc);
678 pc->c[0] = WRITE_FILEMARKS;
679 pc->c[4] = write_filemark;
680 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
681}
682
683static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
684{
685 idetape_tape_t *tape = drive->driver_data;
686 struct gendisk *disk = tape->disk;
687 int load_attempted = 0;
688
689
690 set_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT), &drive->atapi_flags);
691 timeout += jiffies;
692 while (time_before(jiffies, timeout)) {
693 if (ide_do_test_unit_ready(drive, disk) == 0)
694 return 0;
695 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
696 || (tape->asc == 0x3A)) {
697
698 if (load_attempted)
699 return -ENOMEDIUM;
700 ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
701 load_attempted = 1;
702
703 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
704 (tape->ascq == 1 || tape->ascq == 8)))
705 return -EIO;
706 msleep(100);
707 }
708 return -EIO;
709}
710
711static int idetape_flush_tape_buffers(ide_drive_t *drive)
712{
713 struct ide_tape_obj *tape = drive->driver_data;
714 struct ide_atapi_pc pc;
715 int rc;
716
717 idetape_create_write_filemark_cmd(drive, &pc, 0);
718 rc = ide_queue_pc_tail(drive, tape->disk, &pc, NULL, 0);
719 if (rc)
720 return rc;
721 idetape_wait_ready(drive, 60 * 5 * HZ);
722 return 0;
723}
724
725static int ide_tape_read_position(ide_drive_t *drive)
726{
727 idetape_tape_t *tape = drive->driver_data;
728 struct ide_atapi_pc pc;
729 u8 buf[20];
730
731 ide_debug_log(IDE_DBG_FUNC, "enter");
732
733
734 ide_init_pc(&pc);
735 pc.c[0] = READ_POSITION;
736 pc.req_xfer = 20;
737
738 if (ide_queue_pc_tail(drive, tape->disk, &pc, buf, pc.req_xfer))
739 return -1;
740
741 if (!pc.error) {
742 ide_debug_log(IDE_DBG_FUNC, "BOP - %s",
743 (buf[0] & 0x80) ? "Yes" : "No");
744 ide_debug_log(IDE_DBG_FUNC, "EOP - %s",
745 (buf[0] & 0x40) ? "Yes" : "No");
746
747 if (buf[0] & 0x4) {
748 printk(KERN_INFO "ide-tape: Block location is unknown"
749 "to the tape\n");
750 clear_bit(ilog2(IDE_AFLAG_ADDRESS_VALID),
751 &drive->atapi_flags);
752 return -1;
753 } else {
754 ide_debug_log(IDE_DBG_FUNC, "Block Location: %u",
755 be32_to_cpup((__be32 *)&buf[4]));
756
757 tape->partition = buf[1];
758 tape->first_frame = be32_to_cpup((__be32 *)&buf[4]);
759 set_bit(ilog2(IDE_AFLAG_ADDRESS_VALID),
760 &drive->atapi_flags);
761 }
762 }
763
764 return tape->first_frame;
765}
766
767static void idetape_create_locate_cmd(ide_drive_t *drive,
768 struct ide_atapi_pc *pc,
769 unsigned int block, u8 partition, int skip)
770{
771 ide_init_pc(pc);
772 pc->c[0] = POSITION_TO_ELEMENT;
773 pc->c[1] = 2;
774 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
775 pc->c[8] = partition;
776 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
777}
778
779static void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
780{
781 idetape_tape_t *tape = drive->driver_data;
782
783 if (tape->chrdev_dir != IDETAPE_DIR_READ)
784 return;
785
786 clear_bit(ilog2(IDE_AFLAG_FILEMARK), &drive->atapi_flags);
787 tape->valid = 0;
788 if (tape->buf != NULL) {
789 kfree(tape->buf);
790 tape->buf = NULL;
791 }
792
793 tape->chrdev_dir = IDETAPE_DIR_NONE;
794}
795
796
797
798
799
800
801
802static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
803 u8 partition, int skip)
804{
805 idetape_tape_t *tape = drive->driver_data;
806 struct gendisk *disk = tape->disk;
807 int ret;
808 struct ide_atapi_pc pc;
809
810 if (tape->chrdev_dir == IDETAPE_DIR_READ)
811 __ide_tape_discard_merge_buffer(drive);
812 idetape_wait_ready(drive, 60 * 5 * HZ);
813 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
814 ret = ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
815 if (ret)
816 return ret;
817
818 ret = ide_tape_read_position(drive);
819 if (ret < 0)
820 return ret;
821 return 0;
822}
823
824static void ide_tape_discard_merge_buffer(ide_drive_t *drive,
825 int restore_position)
826{
827 idetape_tape_t *tape = drive->driver_data;
828 int seek, position;
829
830 __ide_tape_discard_merge_buffer(drive);
831 if (restore_position) {
832 position = ide_tape_read_position(drive);
833 seek = position > 0 ? position : 0;
834 if (idetape_position_tape(drive, seek, 0, 0)) {
835 printk(KERN_INFO "ide-tape: %s: position_tape failed in"
836 " %s\n", tape->name, __func__);
837 return;
838 }
839 }
840}
841
842
843
844
845
846static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int size)
847{
848 idetape_tape_t *tape = drive->driver_data;
849 struct request *rq;
850 int ret;
851
852 ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x, size: %d", cmd, size);
853
854 BUG_ON(cmd != REQ_IDETAPE_READ && cmd != REQ_IDETAPE_WRITE);
855 BUG_ON(size < 0 || size % tape->blk_size);
856
857 rq = blk_get_request(drive->queue, REQ_OP_DRV_IN, 0);
858 ide_req(rq)->type = ATA_PRIV_MISC;
859 scsi_req(rq)->cmd[13] = cmd;
860 rq->rq_disk = tape->disk;
861 rq->__sector = tape->first_frame;
862
863 if (size) {
864 ret = blk_rq_map_kern(drive->queue, rq, tape->buf, size,
865 GFP_NOIO);
866 if (ret)
867 goto out_put;
868 }
869
870 blk_execute_rq(drive->queue, tape->disk, rq, 0);
871
872
873 size -= scsi_req(rq)->resid_len;
874 tape->cur = tape->buf;
875 if (cmd == REQ_IDETAPE_READ)
876 tape->valid = size;
877 else
878 tape->valid = 0;
879
880 ret = size;
881 if (scsi_req(rq)->result == IDE_DRV_ERROR_GENERAL)
882 ret = -EIO;
883out_put:
884 blk_put_request(rq);
885 return ret;
886}
887
888static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
889{
890 ide_init_pc(pc);
891 pc->c[0] = INQUIRY;
892 pc->c[4] = 254;
893 pc->req_xfer = 254;
894}
895
896static void idetape_create_rewind_cmd(ide_drive_t *drive,
897 struct ide_atapi_pc *pc)
898{
899 ide_init_pc(pc);
900 pc->c[0] = REZERO_UNIT;
901 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
902}
903
904static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
905{
906 ide_init_pc(pc);
907 pc->c[0] = ERASE;
908 pc->c[1] = 1;
909 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
910}
911
912static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
913{
914 ide_init_pc(pc);
915 pc->c[0] = SPACE;
916 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
917 pc->c[1] = cmd;
918 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
919}
920
921static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
922{
923 idetape_tape_t *tape = drive->driver_data;
924
925 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
926 printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
927 " but we are not writing.\n");
928 return;
929 }
930 if (tape->buf) {
931 size_t aligned = roundup(tape->valid, tape->blk_size);
932
933 memset(tape->cur, 0, aligned - tape->valid);
934 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, aligned);
935 kfree(tape->buf);
936 tape->buf = NULL;
937 }
938 tape->chrdev_dir = IDETAPE_DIR_NONE;
939}
940
941static int idetape_init_rw(ide_drive_t *drive, int dir)
942{
943 idetape_tape_t *tape = drive->driver_data;
944 int rc;
945
946 BUG_ON(dir != IDETAPE_DIR_READ && dir != IDETAPE_DIR_WRITE);
947
948 if (tape->chrdev_dir == dir)
949 return 0;
950
951 if (tape->chrdev_dir == IDETAPE_DIR_READ)
952 ide_tape_discard_merge_buffer(drive, 1);
953 else if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
954 ide_tape_flush_merge_buffer(drive);
955 idetape_flush_tape_buffers(drive);
956 }
957
958 if (tape->buf || tape->valid) {
959 printk(KERN_ERR "ide-tape: valid should be 0 now\n");
960 tape->valid = 0;
961 }
962
963 tape->buf = kmalloc(tape->buffer_size, GFP_KERNEL);
964 if (!tape->buf)
965 return -ENOMEM;
966 tape->chrdev_dir = dir;
967 tape->cur = tape->buf;
968
969
970
971
972
973
974
975 if (drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) {
976 int cmd = dir == IDETAPE_DIR_READ ? REQ_IDETAPE_READ
977 : REQ_IDETAPE_WRITE;
978
979 rc = idetape_queue_rw_tail(drive, cmd, 0);
980 if (rc < 0) {
981 kfree(tape->buf);
982 tape->buf = NULL;
983 tape->chrdev_dir = IDETAPE_DIR_NONE;
984 return rc;
985 }
986 }
987
988 return 0;
989}
990
991static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
992{
993 idetape_tape_t *tape = drive->driver_data;
994
995 memset(tape->buf, 0, tape->buffer_size);
996
997 while (bcount) {
998 unsigned int count = min(tape->buffer_size, bcount);
999
1000 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, count);
1001 bcount -= count;
1002 }
1003}
1004
1005
1006
1007
1008
1009static int idetape_rewind_tape(ide_drive_t *drive)
1010{
1011 struct ide_tape_obj *tape = drive->driver_data;
1012 struct gendisk *disk = tape->disk;
1013 struct ide_atapi_pc pc;
1014 int ret;
1015
1016 ide_debug_log(IDE_DBG_FUNC, "enter");
1017
1018 idetape_create_rewind_cmd(drive, &pc);
1019 ret = ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
1020 if (ret)
1021 return ret;
1022
1023 ret = ide_tape_read_position(drive);
1024 if (ret < 0)
1025 return ret;
1026 return 0;
1027}
1028
1029
1030static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
1031 unsigned long arg)
1032{
1033 idetape_tape_t *tape = drive->driver_data;
1034 void __user *argp = (void __user *)arg;
1035
1036 struct idetape_config {
1037 int dsc_rw_frequency;
1038 int dsc_media_access_frequency;
1039 int nr_stages;
1040 } config;
1041
1042 ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%04x", cmd);
1043
1044 switch (cmd) {
1045 case 0x0340:
1046 if (copy_from_user(&config, argp, sizeof(config)))
1047 return -EFAULT;
1048 tape->best_dsc_rw_freq = config.dsc_rw_frequency;
1049 break;
1050 case 0x0350:
1051 memset(&config, 0, sizeof(config));
1052 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
1053 config.nr_stages = 1;
1054 if (copy_to_user(argp, &config, sizeof(config)))
1055 return -EFAULT;
1056 break;
1057 default:
1058 return -EIO;
1059 }
1060 return 0;
1061}
1062
1063static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
1064 int mt_count)
1065{
1066 idetape_tape_t *tape = drive->driver_data;
1067 struct gendisk *disk = tape->disk;
1068 struct ide_atapi_pc pc;
1069 int retval, count = 0;
1070 int sprev = !!(tape->caps[4] & 0x20);
1071
1072
1073 ide_debug_log(IDE_DBG_FUNC, "mt_op: %d, mt_count: %d", mt_op, mt_count);
1074
1075 if (mt_count == 0)
1076 return 0;
1077 if (MTBSF == mt_op || MTBSFM == mt_op) {
1078 if (!sprev)
1079 return -EIO;
1080 mt_count = -mt_count;
1081 }
1082
1083 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
1084 tape->valid = 0;
1085 if (test_and_clear_bit(ilog2(IDE_AFLAG_FILEMARK),
1086 &drive->atapi_flags))
1087 ++count;
1088 ide_tape_discard_merge_buffer(drive, 0);
1089 }
1090
1091 switch (mt_op) {
1092 case MTFSF:
1093 case MTBSF:
1094 idetape_create_space_cmd(&pc, mt_count - count,
1095 IDETAPE_SPACE_OVER_FILEMARK);
1096 return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
1097 case MTFSFM:
1098 case MTBSFM:
1099 if (!sprev)
1100 return -EIO;
1101 retval = idetape_space_over_filemarks(drive, MTFSF,
1102 mt_count - count);
1103 if (retval)
1104 return retval;
1105 count = (MTBSFM == mt_op ? 1 : -1);
1106 return idetape_space_over_filemarks(drive, MTFSF, count);
1107 default:
1108 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1109 mt_op);
1110 return -EIO;
1111 }
1112}
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
1130 size_t count, loff_t *ppos)
1131{
1132 struct ide_tape_obj *tape = file->private_data;
1133 ide_drive_t *drive = tape->drive;
1134 size_t done = 0;
1135 ssize_t ret = 0;
1136 int rc;
1137
1138 ide_debug_log(IDE_DBG_FUNC, "count %zd", count);
1139
1140 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
1141 if (test_bit(ilog2(IDE_AFLAG_DETECT_BS), &drive->atapi_flags))
1142 if (count > tape->blk_size &&
1143 (count % tape->blk_size) == 0)
1144 tape->user_bs_factor = count / tape->blk_size;
1145 }
1146
1147 rc = idetape_init_rw(drive, IDETAPE_DIR_READ);
1148 if (rc < 0)
1149 return rc;
1150
1151 while (done < count) {
1152 size_t todo;
1153
1154
1155 if (!tape->valid) {
1156
1157 if (test_bit(ilog2(IDE_AFLAG_FILEMARK),
1158 &drive->atapi_flags))
1159 break;
1160
1161 if (idetape_queue_rw_tail(drive, REQ_IDETAPE_READ,
1162 tape->buffer_size) <= 0)
1163 break;
1164 }
1165
1166
1167 todo = min_t(size_t, count - done, tape->valid);
1168 if (copy_to_user(buf + done, tape->cur, todo))
1169 ret = -EFAULT;
1170
1171 tape->cur += todo;
1172 tape->valid -= todo;
1173 done += todo;
1174 }
1175
1176 if (!done && test_bit(ilog2(IDE_AFLAG_FILEMARK), &drive->atapi_flags)) {
1177 idetape_space_over_filemarks(drive, MTFSF, 1);
1178 return 0;
1179 }
1180
1181 return ret ? ret : done;
1182}
1183
1184static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
1185 size_t count, loff_t *ppos)
1186{
1187 struct ide_tape_obj *tape = file->private_data;
1188 ide_drive_t *drive = tape->drive;
1189 size_t done = 0;
1190 ssize_t ret = 0;
1191 int rc;
1192
1193
1194 if (tape->write_prot)
1195 return -EACCES;
1196
1197 ide_debug_log(IDE_DBG_FUNC, "count %zd", count);
1198
1199
1200 rc = idetape_init_rw(drive, IDETAPE_DIR_WRITE);
1201 if (rc < 0)
1202 return rc;
1203
1204 while (done < count) {
1205 size_t todo;
1206
1207
1208 if (tape->valid == tape->buffer_size &&
1209 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
1210 tape->buffer_size) <= 0)
1211 return rc;
1212
1213
1214 todo = min_t(size_t, count - done,
1215 tape->buffer_size - tape->valid);
1216 if (copy_from_user(tape->cur, buf + done, todo))
1217 ret = -EFAULT;
1218
1219 tape->cur += todo;
1220 tape->valid += todo;
1221 done += todo;
1222 }
1223
1224 return ret ? ret : done;
1225}
1226
1227static int idetape_write_filemark(ide_drive_t *drive)
1228{
1229 struct ide_tape_obj *tape = drive->driver_data;
1230 struct ide_atapi_pc pc;
1231
1232
1233 idetape_create_write_filemark_cmd(drive, &pc, 1);
1234 if (ide_queue_pc_tail(drive, tape->disk, &pc, NULL, 0)) {
1235 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
1236 return -EIO;
1237 }
1238 return 0;
1239}
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
1255{
1256 idetape_tape_t *tape = drive->driver_data;
1257 struct gendisk *disk = tape->disk;
1258 struct ide_atapi_pc pc;
1259 int i, retval;
1260
1261 ide_debug_log(IDE_DBG_FUNC, "MTIOCTOP ioctl: mt_op: %d, mt_count: %d",
1262 mt_op, mt_count);
1263
1264 switch (mt_op) {
1265 case MTFSF:
1266 case MTFSFM:
1267 case MTBSF:
1268 case MTBSFM:
1269 if (!mt_count)
1270 return 0;
1271 return idetape_space_over_filemarks(drive, mt_op, mt_count);
1272 default:
1273 break;
1274 }
1275
1276 switch (mt_op) {
1277 case MTWEOF:
1278 if (tape->write_prot)
1279 return -EACCES;
1280 ide_tape_discard_merge_buffer(drive, 1);
1281 for (i = 0; i < mt_count; i++) {
1282 retval = idetape_write_filemark(drive);
1283 if (retval)
1284 return retval;
1285 }
1286 return 0;
1287 case MTREW:
1288 ide_tape_discard_merge_buffer(drive, 0);
1289 if (idetape_rewind_tape(drive))
1290 return -EIO;
1291 return 0;
1292 case MTLOAD:
1293 ide_tape_discard_merge_buffer(drive, 0);
1294 return ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
1295 case MTUNLOAD:
1296 case MTOFFL:
1297
1298
1299
1300
1301 if (tape->door_locked) {
1302 if (!ide_set_media_lock(drive, disk, 0))
1303 tape->door_locked = DOOR_UNLOCKED;
1304 }
1305 ide_tape_discard_merge_buffer(drive, 0);
1306 retval = ide_do_start_stop(drive, disk, !IDETAPE_LU_LOAD_MASK);
1307 if (!retval)
1308 clear_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT),
1309 &drive->atapi_flags);
1310 return retval;
1311 case MTNOP:
1312 ide_tape_discard_merge_buffer(drive, 0);
1313 return idetape_flush_tape_buffers(drive);
1314 case MTRETEN:
1315 ide_tape_discard_merge_buffer(drive, 0);
1316 return ide_do_start_stop(drive, disk,
1317 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
1318 case MTEOM:
1319 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
1320 return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
1321 case MTERASE:
1322 (void)idetape_rewind_tape(drive);
1323 idetape_create_erase_cmd(&pc);
1324 return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
1325 case MTSETBLK:
1326 if (mt_count) {
1327 if (mt_count < tape->blk_size ||
1328 mt_count % tape->blk_size)
1329 return -EIO;
1330 tape->user_bs_factor = mt_count / tape->blk_size;
1331 clear_bit(ilog2(IDE_AFLAG_DETECT_BS),
1332 &drive->atapi_flags);
1333 } else
1334 set_bit(ilog2(IDE_AFLAG_DETECT_BS),
1335 &drive->atapi_flags);
1336 return 0;
1337 case MTSEEK:
1338 ide_tape_discard_merge_buffer(drive, 0);
1339 return idetape_position_tape(drive,
1340 mt_count * tape->user_bs_factor, tape->partition, 0);
1341 case MTSETPART:
1342 ide_tape_discard_merge_buffer(drive, 0);
1343 return idetape_position_tape(drive, 0, mt_count, 0);
1344 case MTFSR:
1345 case MTBSR:
1346 case MTLOCK:
1347 retval = ide_set_media_lock(drive, disk, 1);
1348 if (retval)
1349 return retval;
1350 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
1351 return 0;
1352 case MTUNLOCK:
1353 retval = ide_set_media_lock(drive, disk, 0);
1354 if (retval)
1355 return retval;
1356 tape->door_locked = DOOR_UNLOCKED;
1357 return 0;
1358 default:
1359 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
1360 mt_op);
1361 return -EIO;
1362 }
1363}
1364
1365
1366
1367
1368
1369
1370static long do_idetape_chrdev_ioctl(struct file *file,
1371 unsigned int cmd, unsigned long arg)
1372{
1373 struct ide_tape_obj *tape = file->private_data;
1374 ide_drive_t *drive = tape->drive;
1375 struct mtop mtop;
1376 struct mtget mtget;
1377 struct mtpos mtpos;
1378 int block_offset = 0, position = tape->first_frame;
1379 void __user *argp = (void __user *)arg;
1380
1381 ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x", cmd);
1382
1383 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
1384 ide_tape_flush_merge_buffer(drive);
1385 idetape_flush_tape_buffers(drive);
1386 }
1387 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
1388 block_offset = tape->valid /
1389 (tape->blk_size * tape->user_bs_factor);
1390 position = ide_tape_read_position(drive);
1391 if (position < 0)
1392 return -EIO;
1393 }
1394 switch (cmd) {
1395 case MTIOCTOP:
1396 if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
1397 return -EFAULT;
1398 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
1399 case MTIOCGET:
1400 memset(&mtget, 0, sizeof(struct mtget));
1401 mtget.mt_type = MT_ISSCSI2;
1402 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
1403 mtget.mt_dsreg =
1404 ((tape->blk_size * tape->user_bs_factor)
1405 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
1406
1407 if (tape->drv_write_prot)
1408 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
1409
1410 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
1411 return -EFAULT;
1412 return 0;
1413 case MTIOCPOS:
1414 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
1415 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
1416 return -EFAULT;
1417 return 0;
1418 default:
1419 if (tape->chrdev_dir == IDETAPE_DIR_READ)
1420 ide_tape_discard_merge_buffer(drive, 1);
1421 return idetape_blkdev_ioctl(drive, cmd, arg);
1422 }
1423}
1424
1425static long idetape_chrdev_ioctl(struct file *file,
1426 unsigned int cmd, unsigned long arg)
1427{
1428 long ret;
1429 mutex_lock(&ide_tape_mutex);
1430 ret = do_idetape_chrdev_ioctl(file, cmd, arg);
1431 mutex_unlock(&ide_tape_mutex);
1432 return ret;
1433}
1434
1435
1436
1437
1438
1439static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
1440{
1441 idetape_tape_t *tape = drive->driver_data;
1442 struct ide_atapi_pc pc;
1443 u8 buf[12];
1444
1445 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
1446 if (ide_queue_pc_tail(drive, tape->disk, &pc, buf, pc.req_xfer)) {
1447 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
1448 if (tape->blk_size == 0) {
1449 printk(KERN_WARNING "ide-tape: Cannot deal with zero "
1450 "block size, assuming 32k\n");
1451 tape->blk_size = 32768;
1452 }
1453 return;
1454 }
1455 tape->blk_size = (buf[4 + 5] << 16) +
1456 (buf[4 + 6] << 8) +
1457 buf[4 + 7];
1458 tape->drv_write_prot = (buf[2] & 0x80) >> 7;
1459
1460 ide_debug_log(IDE_DBG_FUNC, "blk_size: %d, write_prot: %d",
1461 tape->blk_size, tape->drv_write_prot);
1462}
1463
1464static int idetape_chrdev_open(struct inode *inode, struct file *filp)
1465{
1466 unsigned int minor = iminor(inode), i = minor & ~0xc0;
1467 ide_drive_t *drive;
1468 idetape_tape_t *tape;
1469 int retval;
1470
1471 if (i >= MAX_HWIFS * MAX_DRIVES)
1472 return -ENXIO;
1473
1474 mutex_lock(&idetape_chrdev_mutex);
1475
1476 tape = ide_tape_get(NULL, true, i);
1477 if (!tape) {
1478 mutex_unlock(&idetape_chrdev_mutex);
1479 return -ENXIO;
1480 }
1481
1482 drive = tape->drive;
1483 filp->private_data = tape;
1484
1485 ide_debug_log(IDE_DBG_FUNC, "enter");
1486
1487
1488
1489
1490
1491
1492 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
1493
1494
1495 if (test_and_set_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags)) {
1496 retval = -EBUSY;
1497 goto out_put_tape;
1498 }
1499
1500 retval = idetape_wait_ready(drive, 60 * HZ);
1501 if (retval) {
1502 clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
1503 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
1504 goto out_put_tape;
1505 }
1506
1507 ide_tape_read_position(drive);
1508 if (!test_bit(ilog2(IDE_AFLAG_ADDRESS_VALID), &drive->atapi_flags))
1509 (void)idetape_rewind_tape(drive);
1510
1511
1512 ide_tape_get_bsize_from_bdesc(drive);
1513
1514
1515 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
1516 tape->write_prot = 1;
1517 else
1518 tape->write_prot = tape->drv_write_prot;
1519
1520
1521 if (tape->write_prot) {
1522 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
1523 (filp->f_flags & O_ACCMODE) == O_RDWR) {
1524 clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
1525 retval = -EROFS;
1526 goto out_put_tape;
1527 }
1528 }
1529
1530
1531 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
1532 if (!ide_set_media_lock(drive, tape->disk, 1)) {
1533 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
1534 tape->door_locked = DOOR_LOCKED;
1535 }
1536 }
1537 mutex_unlock(&idetape_chrdev_mutex);
1538
1539 return 0;
1540
1541out_put_tape:
1542 ide_tape_put(tape);
1543
1544 mutex_unlock(&idetape_chrdev_mutex);
1545
1546 return retval;
1547}
1548
1549static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
1550{
1551 idetape_tape_t *tape = drive->driver_data;
1552
1553 ide_tape_flush_merge_buffer(drive);
1554 tape->buf = kmalloc(tape->buffer_size, GFP_KERNEL);
1555 if (tape->buf != NULL) {
1556 idetape_pad_zeros(drive, tape->blk_size *
1557 (tape->user_bs_factor - 1));
1558 kfree(tape->buf);
1559 tape->buf = NULL;
1560 }
1561 idetape_write_filemark(drive);
1562 idetape_flush_tape_buffers(drive);
1563 idetape_flush_tape_buffers(drive);
1564}
1565
1566static int idetape_chrdev_release(struct inode *inode, struct file *filp)
1567{
1568 struct ide_tape_obj *tape = filp->private_data;
1569 ide_drive_t *drive = tape->drive;
1570 unsigned int minor = iminor(inode);
1571
1572 mutex_lock(&idetape_chrdev_mutex);
1573
1574 tape = drive->driver_data;
1575
1576 ide_debug_log(IDE_DBG_FUNC, "enter");
1577
1578 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
1579 idetape_write_release(drive, minor);
1580 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
1581 if (minor < 128)
1582 ide_tape_discard_merge_buffer(drive, 1);
1583 }
1584
1585 if (minor < 128 && test_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT),
1586 &drive->atapi_flags))
1587 (void) idetape_rewind_tape(drive);
1588
1589 if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
1590 if (tape->door_locked == DOOR_LOCKED) {
1591 if (!ide_set_media_lock(drive, tape->disk, 0))
1592 tape->door_locked = DOOR_UNLOCKED;
1593 }
1594 }
1595 clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
1596 ide_tape_put(tape);
1597
1598 mutex_unlock(&idetape_chrdev_mutex);
1599
1600 return 0;
1601}
1602
1603static void idetape_get_inquiry_results(ide_drive_t *drive)
1604{
1605 idetape_tape_t *tape = drive->driver_data;
1606 struct ide_atapi_pc pc;
1607 u8 pc_buf[256];
1608 char fw_rev[4], vendor_id[8], product_id[16];
1609
1610 idetape_create_inquiry_cmd(&pc);
1611 if (ide_queue_pc_tail(drive, tape->disk, &pc, pc_buf, pc.req_xfer)) {
1612 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
1613 tape->name);
1614 return;
1615 }
1616 memcpy(vendor_id, &pc_buf[8], 8);
1617 memcpy(product_id, &pc_buf[16], 16);
1618 memcpy(fw_rev, &pc_buf[32], 4);
1619
1620 ide_fixstring(vendor_id, 8, 0);
1621 ide_fixstring(product_id, 16, 0);
1622 ide_fixstring(fw_rev, 4, 0);
1623
1624 printk(KERN_INFO "ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n",
1625 drive->name, tape->name, vendor_id, product_id, fw_rev);
1626}
1627
1628
1629
1630
1631
1632static void idetape_get_mode_sense_results(ide_drive_t *drive)
1633{
1634 idetape_tape_t *tape = drive->driver_data;
1635 struct ide_atapi_pc pc;
1636 u8 buf[24], *caps;
1637 u8 speed, max_speed;
1638
1639 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
1640 if (ide_queue_pc_tail(drive, tape->disk, &pc, buf, pc.req_xfer)) {
1641 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
1642 " some default values\n");
1643 tape->blk_size = 512;
1644 put_unaligned(52, (u16 *)&tape->caps[12]);
1645 put_unaligned(540, (u16 *)&tape->caps[14]);
1646 put_unaligned(6*52, (u16 *)&tape->caps[16]);
1647 return;
1648 }
1649 caps = buf + 4 + buf[3];
1650
1651
1652 speed = be16_to_cpup((__be16 *)&caps[14]);
1653 max_speed = be16_to_cpup((__be16 *)&caps[8]);
1654
1655 *(u16 *)&caps[8] = max_speed;
1656 *(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]);
1657 *(u16 *)&caps[14] = speed;
1658 *(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]);
1659
1660 if (!speed) {
1661 printk(KERN_INFO "ide-tape: %s: invalid tape speed "
1662 "(assuming 650KB/sec)\n", drive->name);
1663 *(u16 *)&caps[14] = 650;
1664 }
1665 if (!max_speed) {
1666 printk(KERN_INFO "ide-tape: %s: invalid max_speed "
1667 "(assuming 650KB/sec)\n", drive->name);
1668 *(u16 *)&caps[8] = 650;
1669 }
1670
1671 memcpy(&tape->caps, caps, 20);
1672
1673
1674 if ((caps[6] & 1) == 0)
1675 drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
1676
1677 if (caps[7] & 0x02)
1678 tape->blk_size = 512;
1679 else if (caps[7] & 0x04)
1680 tape->blk_size = 1024;
1681}
1682
1683#ifdef CONFIG_IDE_PROC_FS
1684#define ide_tape_devset_get(name, field) \
1685static int get_##name(ide_drive_t *drive) \
1686{ \
1687 idetape_tape_t *tape = drive->driver_data; \
1688 return tape->field; \
1689}
1690
1691#define ide_tape_devset_set(name, field) \
1692static int set_##name(ide_drive_t *drive, int arg) \
1693{ \
1694 idetape_tape_t *tape = drive->driver_data; \
1695 tape->field = arg; \
1696 return 0; \
1697}
1698
1699#define ide_tape_devset_rw_field(_name, _field) \
1700ide_tape_devset_get(_name, _field) \
1701ide_tape_devset_set(_name, _field) \
1702IDE_DEVSET(_name, DS_SYNC, get_##_name, set_##_name)
1703
1704#define ide_tape_devset_r_field(_name, _field) \
1705ide_tape_devset_get(_name, _field) \
1706IDE_DEVSET(_name, 0, get_##_name, NULL)
1707
1708static int mulf_tdsc(ide_drive_t *drive) { return 1000; }
1709static int divf_tdsc(ide_drive_t *drive) { return HZ; }
1710static int divf_buffer(ide_drive_t *drive) { return 2; }
1711static int divf_buffer_size(ide_drive_t *drive) { return 1024; }
1712
1713ide_devset_rw_flag(dsc_overlap, IDE_DFLAG_DSC_OVERLAP);
1714
1715ide_tape_devset_rw_field(tdsc, best_dsc_rw_freq);
1716
1717ide_tape_devset_r_field(avg_speed, avg_speed);
1718ide_tape_devset_r_field(speed, caps[14]);
1719ide_tape_devset_r_field(buffer, caps[16]);
1720ide_tape_devset_r_field(buffer_size, buffer_size);
1721
1722static const struct ide_proc_devset idetape_settings[] = {
1723 __IDE_PROC_DEVSET(avg_speed, 0, 0xffff, NULL, NULL),
1724 __IDE_PROC_DEVSET(buffer, 0, 0xffff, NULL, divf_buffer),
1725 __IDE_PROC_DEVSET(buffer_size, 0, 0xffff, NULL, divf_buffer_size),
1726 __IDE_PROC_DEVSET(dsc_overlap, 0, 1, NULL, NULL),
1727 __IDE_PROC_DEVSET(speed, 0, 0xffff, NULL, NULL),
1728 __IDE_PROC_DEVSET(tdsc, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX,
1729 mulf_tdsc, divf_tdsc),
1730 { NULL },
1731};
1732#endif
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
1746{
1747 unsigned long t;
1748 int speed;
1749 u16 *ctl = (u16 *)&tape->caps[12];
1750
1751 ide_debug_log(IDE_DBG_FUNC, "minor: %d", minor);
1752
1753 drive->pc_callback = ide_tape_callback;
1754
1755 drive->dev_flags |= IDE_DFLAG_DSC_OVERLAP;
1756
1757 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
1758 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
1759 tape->name);
1760 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
1761 }
1762
1763
1764 if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401"))
1765 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
1766
1767 tape->minor = minor;
1768 tape->name[0] = 'h';
1769 tape->name[1] = 't';
1770 tape->name[2] = '0' + minor;
1771 tape->chrdev_dir = IDETAPE_DIR_NONE;
1772
1773 idetape_get_inquiry_results(drive);
1774 idetape_get_mode_sense_results(drive);
1775 ide_tape_get_bsize_from_bdesc(drive);
1776 tape->user_bs_factor = 1;
1777 tape->buffer_size = *ctl * tape->blk_size;
1778 while (tape->buffer_size > 0xffff) {
1779 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
1780 *ctl /= 2;
1781 tape->buffer_size = *ctl * tape->blk_size;
1782 }
1783
1784
1785 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
1786
1787 t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
1788
1789
1790
1791
1792
1793 tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
1794 IDETAPE_DSC_RW_MAX);
1795 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
1796 "%ums tDSC%s\n",
1797 drive->name, tape->name, *(u16 *)&tape->caps[14],
1798 (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
1799 tape->buffer_size / 1024,
1800 jiffies_to_msecs(tape->best_dsc_rw_freq),
1801 (drive->dev_flags & IDE_DFLAG_USING_DMA) ? ", DMA" : "");
1802
1803 ide_proc_register_driver(drive, tape->driver);
1804}
1805
1806static void ide_tape_remove(ide_drive_t *drive)
1807{
1808 idetape_tape_t *tape = drive->driver_data;
1809
1810 ide_proc_unregister_driver(drive, tape->driver);
1811 device_del(&tape->dev);
1812 ide_unregister_region(tape->disk);
1813
1814 mutex_lock(&idetape_ref_mutex);
1815 put_device(&tape->dev);
1816 mutex_unlock(&idetape_ref_mutex);
1817}
1818
1819static void ide_tape_release(struct device *dev)
1820{
1821 struct ide_tape_obj *tape = to_ide_drv(dev, ide_tape_obj);
1822 ide_drive_t *drive = tape->drive;
1823 struct gendisk *g = tape->disk;
1824
1825 BUG_ON(tape->valid);
1826
1827 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
1828 drive->driver_data = NULL;
1829 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
1830 device_destroy(idetape_sysfs_class,
1831 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
1832 idetape_devs[tape->minor] = NULL;
1833 g->private_data = NULL;
1834 put_disk(g);
1835 kfree(tape);
1836}
1837
1838#ifdef CONFIG_IDE_PROC_FS
1839static int idetape_name_proc_show(struct seq_file *m, void *v)
1840{
1841 ide_drive_t *drive = (ide_drive_t *) m->private;
1842 idetape_tape_t *tape = drive->driver_data;
1843
1844 seq_printf(m, "%s\n", tape->name);
1845 return 0;
1846}
1847
1848static ide_proc_entry_t idetape_proc[] = {
1849 { "capacity", S_IFREG|S_IRUGO, ide_capacity_proc_show },
1850 { "name", S_IFREG|S_IRUGO, idetape_name_proc_show },
1851 {}
1852};
1853
1854static ide_proc_entry_t *ide_tape_proc_entries(ide_drive_t *drive)
1855{
1856 return idetape_proc;
1857}
1858
1859static const struct ide_proc_devset *ide_tape_proc_devsets(ide_drive_t *drive)
1860{
1861 return idetape_settings;
1862}
1863#endif
1864
1865static int ide_tape_probe(ide_drive_t *);
1866
1867static struct ide_driver idetape_driver = {
1868 .gen_driver = {
1869 .owner = THIS_MODULE,
1870 .name = "ide-tape",
1871 .bus = &ide_bus_type,
1872 },
1873 .probe = ide_tape_probe,
1874 .remove = ide_tape_remove,
1875 .version = IDETAPE_VERSION,
1876 .do_request = idetape_do_request,
1877#ifdef CONFIG_IDE_PROC_FS
1878 .proc_entries = ide_tape_proc_entries,
1879 .proc_devsets = ide_tape_proc_devsets,
1880#endif
1881};
1882
1883
1884static const struct file_operations idetape_fops = {
1885 .owner = THIS_MODULE,
1886 .read = idetape_chrdev_read,
1887 .write = idetape_chrdev_write,
1888 .unlocked_ioctl = idetape_chrdev_ioctl,
1889 .open = idetape_chrdev_open,
1890 .release = idetape_chrdev_release,
1891 .llseek = noop_llseek,
1892};
1893
1894static int idetape_open(struct block_device *bdev, fmode_t mode)
1895{
1896 struct ide_tape_obj *tape;
1897
1898 mutex_lock(&ide_tape_mutex);
1899 tape = ide_tape_get(bdev->bd_disk, false, 0);
1900 mutex_unlock(&ide_tape_mutex);
1901
1902 if (!tape)
1903 return -ENXIO;
1904
1905 return 0;
1906}
1907
1908static void idetape_release(struct gendisk *disk, fmode_t mode)
1909{
1910 struct ide_tape_obj *tape = ide_drv_g(disk, ide_tape_obj);
1911
1912 mutex_lock(&ide_tape_mutex);
1913 ide_tape_put(tape);
1914 mutex_unlock(&ide_tape_mutex);
1915}
1916
1917static int idetape_ioctl(struct block_device *bdev, fmode_t mode,
1918 unsigned int cmd, unsigned long arg)
1919{
1920 struct ide_tape_obj *tape = ide_drv_g(bdev->bd_disk, ide_tape_obj);
1921 ide_drive_t *drive = tape->drive;
1922 int err;
1923
1924 mutex_lock(&ide_tape_mutex);
1925 err = generic_ide_ioctl(drive, bdev, cmd, arg);
1926 if (err == -EINVAL)
1927 err = idetape_blkdev_ioctl(drive, cmd, arg);
1928 mutex_unlock(&ide_tape_mutex);
1929
1930 return err;
1931}
1932
1933static const struct block_device_operations idetape_block_ops = {
1934 .owner = THIS_MODULE,
1935 .open = idetape_open,
1936 .release = idetape_release,
1937 .ioctl = idetape_ioctl,
1938};
1939
1940static int ide_tape_probe(ide_drive_t *drive)
1941{
1942 idetape_tape_t *tape;
1943 struct gendisk *g;
1944 int minor;
1945
1946 ide_debug_log(IDE_DBG_FUNC, "enter");
1947
1948 if (!strstr(DRV_NAME, drive->driver_req))
1949 goto failed;
1950
1951 if (drive->media != ide_tape)
1952 goto failed;
1953
1954 if ((drive->dev_flags & IDE_DFLAG_ID_READ) &&
1955 ide_check_atapi_device(drive, DRV_NAME) == 0) {
1956 printk(KERN_ERR "ide-tape: %s: not supported by this version of"
1957 " the driver\n", drive->name);
1958 goto failed;
1959 }
1960 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
1961 if (tape == NULL) {
1962 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
1963 drive->name);
1964 goto failed;
1965 }
1966
1967 g = alloc_disk(1 << PARTN_BITS);
1968 if (!g)
1969 goto out_free_tape;
1970
1971 ide_init_disk(g, drive);
1972
1973 tape->dev.parent = &drive->gendev;
1974 tape->dev.release = ide_tape_release;
1975 dev_set_name(&tape->dev, "%s", dev_name(&drive->gendev));
1976
1977 if (device_register(&tape->dev))
1978 goto out_free_disk;
1979
1980 tape->drive = drive;
1981 tape->driver = &idetape_driver;
1982 tape->disk = g;
1983
1984 g->private_data = &tape->driver;
1985
1986 drive->driver_data = tape;
1987
1988 mutex_lock(&idetape_ref_mutex);
1989 for (minor = 0; idetape_devs[minor]; minor++)
1990 ;
1991 idetape_devs[minor] = tape;
1992 mutex_unlock(&idetape_ref_mutex);
1993
1994 idetape_setup(drive, tape, minor);
1995
1996 device_create(idetape_sysfs_class, &drive->gendev,
1997 MKDEV(IDETAPE_MAJOR, minor), NULL, "%s", tape->name);
1998 device_create(idetape_sysfs_class, &drive->gendev,
1999 MKDEV(IDETAPE_MAJOR, minor + 128), NULL,
2000 "n%s", tape->name);
2001
2002 g->fops = &idetape_block_ops;
2003 ide_register_region(g);
2004
2005 return 0;
2006
2007out_free_disk:
2008 put_disk(g);
2009out_free_tape:
2010 kfree(tape);
2011failed:
2012 return -ENODEV;
2013}
2014
2015static void __exit idetape_exit(void)
2016{
2017 driver_unregister(&idetape_driver.gen_driver);
2018 class_destroy(idetape_sysfs_class);
2019 unregister_chrdev(IDETAPE_MAJOR, "ht");
2020}
2021
2022static int __init idetape_init(void)
2023{
2024 int error = 1;
2025 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
2026 if (IS_ERR(idetape_sysfs_class)) {
2027 idetape_sysfs_class = NULL;
2028 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
2029 error = -EBUSY;
2030 goto out;
2031 }
2032
2033 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
2034 printk(KERN_ERR "ide-tape: Failed to register chrdev"
2035 " interface\n");
2036 error = -EBUSY;
2037 goto out_free_class;
2038 }
2039
2040 error = driver_register(&idetape_driver.gen_driver);
2041 if (error)
2042 goto out_free_chrdev;
2043
2044 return 0;
2045
2046out_free_chrdev:
2047 unregister_chrdev(IDETAPE_MAJOR, "ht");
2048out_free_class:
2049 class_destroy(idetape_sysfs_class);
2050out:
2051 return error;
2052}
2053
2054MODULE_ALIAS("ide:*m-tape*");
2055module_init(idetape_init);
2056module_exit(idetape_exit);
2057MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
2058MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
2059MODULE_LICENSE("GPL");
2060